2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <kern/ux_handler.h>
30 #include <sys/ux_exception.h>
32 #include <mach/exception.h>
33 #include <mach/kern_return.h>
34 #include <mach/port.h>
35 #include <mach/mach_port.h>
36 #include <mach/mig_errors.h>
38 #include <kern/thread.h>
39 #include <kern/task.h>
40 #include <kern/ipc_kobject.h>
41 #include <kern/ipc_tt.h>
43 #include <ipc/ipc_port.h>
45 #include <mach/host_priv.h>
46 #include <kern/host.h>
48 #include <mach/exc_server.h>
49 #include <mach/mach_exc_server.h>
51 #include <libkern/section_keywords.h>
54 * Mach kobject port to reflect Mach exceptions into Unix signals.
56 * This is the default Mach exception handler for initproc, which
57 * then filters to all subprocesses as the host level exception handler for
58 * most Mach exceptions.
61 static SECURITY_READ_ONLY_LATE(const void *) ux_handler_kobject
= NULL
;
62 SECURITY_READ_ONLY_LATE(ipc_port_t
) ux_handler_port
= IP_NULL
;
65 * init is called early in Mach initialization
66 * when we can initialize read-only memory
71 ux_handler_port
= ipc_kobject_alloc_port((ipc_kobject_t
)&ux_handler_kobject
,
72 IKOT_UX_HANDLER
, IPC_KOBJECT_ALLOC_NONE
);
76 * setup is called late in BSD initialization from initproc's context
77 * so the MAC hook goo inside host_set_exception_ports will be able to
78 * set up labels without falling over.
81 ux_handler_setup(void)
83 ipc_port_t ux_handler_send_right
= ipc_port_make_send(ux_handler_port
);
85 if (!IP_VALID(ux_handler_send_right
)) {
86 panic("Couldn't allocate send right for ux_handler_port!\n");
89 kern_return_t kr
= KERN_SUCCESS
;
92 * Consumes 1 send right.
94 * Instruments uses the RPC_ALERT port, so don't register for that.
96 kr
= host_set_exception_ports(host_priv_self(),
97 EXC_MASK_ALL
& ~(EXC_MASK_RPC_ALERT
),
98 ux_handler_send_right
,
99 EXCEPTION_DEFAULT
| MACH_EXCEPTION_CODES
,
102 if (kr
!= KERN_SUCCESS
) {
103 panic("host_set_exception_ports failed to set ux_handler! %d", kr
);
108 * Is this port the ux_handler?
109 * If so, it's safe to send an exception without checking labels.
112 is_ux_handler_port(mach_port_t port
)
114 if (ux_handler_port
== port
) {
122 catch_mach_exception_raise(
123 mach_port_t exception_port
,
124 mach_port_t thread_port
,
125 mach_port_t task_port
,
126 exception_type_t exception
,
127 mach_exception_data_t code
,
128 __unused mach_msg_type_number_t codeCnt
)
130 if (exception_port
!= ux_handler_port
) {
134 kern_return_t kr
= KERN_SUCCESS
;
136 thread_t target_thread
= THREAD_NULL
;
137 task_t target_task
= TASK_NULL
;
139 if ((target_thread
= convert_port_to_thread(thread_port
)) == THREAD_NULL
) {
140 kr
= KERN_INVALID_ARGUMENT
;
144 if ((target_task
= convert_port_to_task(task_port
)) == TASK_NULL
) {
145 kr
= KERN_INVALID_ARGUMENT
;
149 kr
= handle_ux_exception(target_thread
, exception
, code
[0], code
[1]);
152 if (kr
== KERN_SUCCESS
) {
154 * Following the MIG 'consume on success' protocol,
155 * consume references to the port arguments.
156 * (but NOT the exception_port, as the first argument is borrowed)
158 * If we return non-success, the kobject server will eat the port
162 ipc_port_release_send(thread_port
);
163 ipc_port_release_send(task_port
);
166 thread_deallocate(target_thread
);
167 task_deallocate(target_task
);
173 catch_exception_raise(
174 mach_port_t exception_port
,
177 exception_type_t exception
,
178 exception_data_t code
,
179 mach_msg_type_number_t codeCnt
)
181 if (exception_port
!= ux_handler_port
) {
185 mach_exception_data_type_t big_code
[EXCEPTION_CODE_MAX
] = {
190 return catch_mach_exception_raise(exception_port
,
199 catch_exception_raise_state(
200 __unused mach_port_t exception_port
,
201 __unused exception_type_t exception
,
202 __unused
const exception_data_t code
,
203 __unused mach_msg_type_number_t codeCnt
,
204 __unused
int *flavor
,
205 __unused
const thread_state_t old_state
,
206 __unused mach_msg_type_number_t old_stateCnt
,
207 __unused thread_state_t new_state
,
208 __unused mach_msg_type_number_t
*new_stateCnt
)
210 return KERN_INVALID_ARGUMENT
;
214 catch_mach_exception_raise_state(
215 __unused mach_port_t exception_port
,
216 __unused exception_type_t exception
,
217 __unused
const mach_exception_data_t code
,
218 __unused mach_msg_type_number_t codeCnt
,
219 __unused
int *flavor
,
220 __unused
const thread_state_t old_state
,
221 __unused mach_msg_type_number_t old_stateCnt
,
222 __unused thread_state_t new_state
,
223 __unused mach_msg_type_number_t
*new_stateCnt
)
225 return KERN_INVALID_ARGUMENT
;
229 catch_exception_raise_state_identity(
230 __unused mach_port_t exception_port
,
231 __unused mach_port_t thread
,
232 __unused mach_port_t task
,
233 __unused exception_type_t exception
,
234 __unused exception_data_t code
,
235 __unused mach_msg_type_number_t codeCnt
,
236 __unused
int *flavor
,
237 __unused thread_state_t old_state
,
238 __unused mach_msg_type_number_t old_stateCnt
,
239 __unused thread_state_t new_state
,
240 __unused mach_msg_type_number_t
*new_stateCnt
)
242 return KERN_INVALID_ARGUMENT
;
246 catch_mach_exception_raise_state_identity(
247 __unused mach_port_t exception_port
,
248 __unused mach_port_t thread
,
249 __unused mach_port_t task
,
250 __unused exception_type_t exception
,
251 __unused mach_exception_data_t code
,
252 __unused mach_msg_type_number_t codeCnt
,
253 __unused
int *flavor
,
254 __unused thread_state_t old_state
,
255 __unused mach_msg_type_number_t old_stateCnt
,
256 __unused thread_state_t new_state
,
257 __unused mach_msg_type_number_t
*new_stateCnt
)
259 return KERN_INVALID_ARGUMENT
;