]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
b0d623f7 | 2 | * Copyright (c) 2000-2009 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
35 | * | |
36 | * Permission to use, copy, modify and distribute this software and its | |
37 | * documentation is hereby granted, provided that both the copyright | |
38 | * notice and this permission notice appear in all copies of the | |
39 | * software, derivative works or modified versions, and any portions | |
40 | * thereof, and that both notices appear in supporting documentation. | |
41 | * | |
42 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
43 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
44 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
45 | * | |
46 | * Carnegie Mellon requests users of this software to return to | |
47 | * | |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
52 | * | |
53 | * any improvements or extensions that they make and grant Carnegie Mellon | |
54 | * the rights to redistribute these changes. | |
55 | */ | |
56 | /* | |
57 | */ | |
58 | ||
59 | /* | |
60 | * kern/ipc_host.c | |
61 | * | |
62 | * Routines to implement host ports. | |
63 | */ | |
64 | #include <mach/message.h> | |
65 | #include <mach/mach_traps.h> | |
66 | #include <mach/mach_host_server.h> | |
91447636 | 67 | #include <mach/host_priv_server.h> |
1c79356b A |
68 | #include <kern/host.h> |
69 | #include <kern/processor.h> | |
70 | #include <kern/task.h> | |
71 | #include <kern/thread.h> | |
72 | #include <kern/ipc_host.h> | |
73 | #include <kern/ipc_kobject.h> | |
74 | #include <kern/misc_protos.h> | |
75 | #include <kern/spl.h> | |
76 | #include <ipc/ipc_port.h> | |
77 | #include <ipc/ipc_space.h> | |
78 | ||
3e170ce0 A |
79 | #if CONFIG_MACF |
80 | #include <security/mac_mach_internal.h> | |
81 | #endif | |
82 | ||
1c79356b A |
83 | /* |
84 | * Forward declarations | |
85 | */ | |
86 | ||
1c79356b A |
87 | boolean_t |
88 | ref_pset_port_locked( | |
89 | ipc_port_t port, boolean_t matchn, processor_set_t *ppset); | |
90 | ||
91 | /* | |
92 | * ipc_host_init: set up various things. | |
93 | */ | |
94 | ||
b0d623f7 A |
95 | extern lck_grp_t host_notify_lock_grp; |
96 | extern lck_attr_t host_notify_lock_attr; | |
97 | ||
1c79356b A |
98 | void ipc_host_init(void) |
99 | { | |
100 | ipc_port_t port; | |
101 | int i; | |
102 | ||
b0d623f7 | 103 | lck_mtx_init(&realhost.lock, &host_notify_lock_grp, &host_notify_lock_attr); |
0b4e3aa0 | 104 | |
1c79356b A |
105 | /* |
106 | * Allocate and set up the two host ports. | |
107 | */ | |
108 | port = ipc_port_alloc_kernel(); | |
109 | if (port == IP_NULL) | |
110 | panic("ipc_host_init"); | |
111 | ||
55e303ae A |
112 | ipc_kobject_set(port, (ipc_kobject_t) &realhost, IKOT_HOST_SECURITY); |
113 | kernel_set_special_port(&realhost, HOST_SECURITY_PORT, | |
114 | ipc_port_make_send(port)); | |
1c79356b A |
115 | |
116 | port = ipc_port_alloc_kernel(); | |
117 | if (port == IP_NULL) | |
118 | panic("ipc_host_init"); | |
119 | ||
55e303ae A |
120 | ipc_kobject_set(port, (ipc_kobject_t) &realhost, IKOT_HOST); |
121 | kernel_set_special_port(&realhost, HOST_PORT, | |
122 | ipc_port_make_send(port)); | |
1c79356b A |
123 | |
124 | port = ipc_port_alloc_kernel(); | |
125 | if (port == IP_NULL) | |
126 | panic("ipc_host_init"); | |
127 | ||
55e303ae A |
128 | ipc_kobject_set(port, (ipc_kobject_t) &realhost, IKOT_HOST_PRIV); |
129 | kernel_set_special_port(&realhost, HOST_PRIV_PORT, | |
130 | ipc_port_make_send(port)); | |
1c79356b | 131 | |
55e303ae | 132 | /* the rest of the special ports will be set up later */ |
1c79356b A |
133 | |
134 | for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) { | |
135 | realhost.exc_actions[i].port = IP_NULL; | |
136 | }/* for */ | |
137 | ||
138 | /* | |
139 | * Set up ipc for default processor set. | |
140 | */ | |
2d21ac55 A |
141 | ipc_pset_init(&pset0); |
142 | ipc_pset_enable(&pset0); | |
1c79356b A |
143 | |
144 | /* | |
145 | * And for master processor | |
146 | */ | |
147 | ipc_processor_init(master_processor); | |
148 | ipc_processor_enable(master_processor); | |
149 | } | |
150 | ||
151 | /* | |
152 | * Routine: host_self_trap [mach trap] | |
153 | * Purpose: | |
154 | * Give the caller send rights for his own host port. | |
155 | * Conditions: | |
156 | * Nothing locked. | |
157 | * Returns: | |
158 | * MACH_PORT_NULL if there are any resource failures | |
159 | * or other errors. | |
160 | */ | |
161 | ||
162 | mach_port_name_t | |
91447636 A |
163 | host_self_trap( |
164 | __unused struct host_self_trap_args *args) | |
1c79356b A |
165 | { |
166 | ipc_port_t sright; | |
91447636 | 167 | mach_port_name_t name; |
1c79356b A |
168 | |
169 | sright = ipc_port_copy_send(current_task()->itk_host); | |
91447636 A |
170 | name = ipc_port_copyout_send(sright, current_space()); |
171 | return name; | |
1c79356b A |
172 | } |
173 | ||
174 | /* | |
175 | * ipc_processor_init: | |
176 | * | |
177 | * Initialize ipc access to processor by allocating port. | |
178 | */ | |
179 | ||
180 | void | |
181 | ipc_processor_init( | |
182 | processor_t processor) | |
183 | { | |
184 | ipc_port_t port; | |
185 | ||
186 | port = ipc_port_alloc_kernel(); | |
187 | if (port == IP_NULL) | |
188 | panic("ipc_processor_init"); | |
189 | processor->processor_self = port; | |
190 | } | |
191 | ||
192 | /* | |
193 | * ipc_processor_enable: | |
194 | * | |
195 | * Enable ipc control of processor by setting port object. | |
196 | */ | |
197 | void | |
198 | ipc_processor_enable( | |
199 | processor_t processor) | |
200 | { | |
201 | ipc_port_t myport; | |
202 | ||
203 | myport = processor->processor_self; | |
204 | ipc_kobject_set(myport, (ipc_kobject_t) processor, IKOT_PROCESSOR); | |
205 | } | |
1c79356b A |
206 | |
207 | /* | |
208 | * ipc_pset_init: | |
209 | * | |
210 | * Initialize ipc control of a processor set by allocating its ports. | |
211 | */ | |
212 | ||
213 | void | |
214 | ipc_pset_init( | |
215 | processor_set_t pset) | |
216 | { | |
217 | ipc_port_t port; | |
218 | ||
219 | port = ipc_port_alloc_kernel(); | |
220 | if (port == IP_NULL) | |
221 | panic("ipc_pset_init"); | |
222 | pset->pset_self = port; | |
223 | ||
224 | port = ipc_port_alloc_kernel(); | |
225 | if (port == IP_NULL) | |
226 | panic("ipc_pset_init"); | |
227 | pset->pset_name_self = port; | |
228 | } | |
229 | ||
230 | /* | |
231 | * ipc_pset_enable: | |
232 | * | |
233 | * Enable ipc access to a processor set. | |
234 | */ | |
235 | void | |
236 | ipc_pset_enable( | |
237 | processor_set_t pset) | |
238 | { | |
2d21ac55 A |
239 | ipc_kobject_set(pset->pset_self, (ipc_kobject_t) pset, IKOT_PSET); |
240 | ipc_kobject_set(pset->pset_name_self, (ipc_kobject_t) pset, IKOT_PSET_NAME); | |
1c79356b A |
241 | } |
242 | ||
243 | /* | |
2d21ac55 | 244 | * processor_set_default: |
1c79356b | 245 | * |
2d21ac55 | 246 | * Return ports for manipulating default_processor set. |
1c79356b A |
247 | */ |
248 | kern_return_t | |
249 | processor_set_default( | |
250 | host_t host, | |
251 | processor_set_t *pset) | |
252 | { | |
253 | if (host == HOST_NULL) | |
254 | return(KERN_INVALID_ARGUMENT); | |
255 | ||
2d21ac55 A |
256 | *pset = &pset0; |
257 | ||
258 | return (KERN_SUCCESS); | |
1c79356b A |
259 | } |
260 | ||
261 | /* | |
262 | * Routine: convert_port_to_host | |
263 | * Purpose: | |
264 | * Convert from a port to a host. | |
265 | * Doesn't consume the port ref; the host produced may be null. | |
266 | * Conditions: | |
267 | * Nothing locked. | |
268 | */ | |
269 | ||
270 | host_t | |
271 | convert_port_to_host( | |
272 | ipc_port_t port) | |
273 | { | |
274 | host_t host = HOST_NULL; | |
275 | ||
276 | if (IP_VALID(port)) { | |
277 | ip_lock(port); | |
278 | if (ip_active(port) && | |
279 | ((ip_kotype(port) == IKOT_HOST) || | |
280 | (ip_kotype(port) == IKOT_HOST_PRIV) | |
281 | )) | |
282 | host = (host_t) port->ip_kobject; | |
283 | ip_unlock(port); | |
284 | } | |
285 | ||
286 | return host; | |
287 | } | |
288 | ||
289 | /* | |
290 | * Routine: convert_port_to_host_priv | |
291 | * Purpose: | |
292 | * Convert from a port to a host. | |
293 | * Doesn't consume the port ref; the host produced may be null. | |
294 | * Conditions: | |
295 | * Nothing locked. | |
296 | */ | |
297 | ||
298 | host_t | |
299 | convert_port_to_host_priv( | |
300 | ipc_port_t port) | |
301 | { | |
302 | host_t host = HOST_NULL; | |
303 | ||
304 | if (IP_VALID(port)) { | |
305 | ip_lock(port); | |
306 | if (ip_active(port) && | |
307 | (ip_kotype(port) == IKOT_HOST_PRIV)) | |
308 | host = (host_t) port->ip_kobject; | |
309 | ip_unlock(port); | |
310 | } | |
311 | ||
312 | return host; | |
313 | } | |
314 | ||
315 | /* | |
316 | * Routine: convert_port_to_processor | |
317 | * Purpose: | |
318 | * Convert from a port to a processor. | |
319 | * Doesn't consume the port ref; | |
320 | * the processor produced may be null. | |
321 | * Conditions: | |
322 | * Nothing locked. | |
323 | */ | |
324 | ||
325 | processor_t | |
326 | convert_port_to_processor( | |
327 | ipc_port_t port) | |
328 | { | |
329 | processor_t processor = PROCESSOR_NULL; | |
330 | ||
331 | if (IP_VALID(port)) { | |
332 | ip_lock(port); | |
333 | if (ip_active(port) && | |
334 | (ip_kotype(port) == IKOT_PROCESSOR)) | |
335 | processor = (processor_t) port->ip_kobject; | |
336 | ip_unlock(port); | |
337 | } | |
338 | ||
339 | return processor; | |
340 | } | |
341 | ||
342 | /* | |
343 | * Routine: convert_port_to_pset | |
344 | * Purpose: | |
345 | * Convert from a port to a pset. | |
346 | * Doesn't consume the port ref; produces a pset ref, | |
347 | * which may be null. | |
348 | * Conditions: | |
349 | * Nothing locked. | |
350 | */ | |
351 | ||
352 | processor_set_t | |
353 | convert_port_to_pset( | |
354 | ipc_port_t port) | |
355 | { | |
356 | boolean_t r; | |
357 | processor_set_t pset = PROCESSOR_SET_NULL; | |
358 | ||
359 | r = FALSE; | |
360 | while (!r && IP_VALID(port)) { | |
361 | ip_lock(port); | |
362 | r = ref_pset_port_locked(port, FALSE, &pset); | |
363 | /* port unlocked */ | |
364 | } | |
365 | return pset; | |
366 | } | |
367 | ||
368 | /* | |
369 | * Routine: convert_port_to_pset_name | |
370 | * Purpose: | |
371 | * Convert from a port to a pset. | |
372 | * Doesn't consume the port ref; produces a pset ref, | |
373 | * which may be null. | |
374 | * Conditions: | |
375 | * Nothing locked. | |
376 | */ | |
377 | ||
378 | processor_set_name_t | |
379 | convert_port_to_pset_name( | |
380 | ipc_port_t port) | |
381 | { | |
382 | boolean_t r; | |
383 | processor_set_t pset = PROCESSOR_SET_NULL; | |
384 | ||
385 | r = FALSE; | |
386 | while (!r && IP_VALID(port)) { | |
387 | ip_lock(port); | |
388 | r = ref_pset_port_locked(port, TRUE, &pset); | |
389 | /* port unlocked */ | |
390 | } | |
391 | return pset; | |
392 | } | |
393 | ||
394 | boolean_t | |
395 | ref_pset_port_locked(ipc_port_t port, boolean_t matchn, processor_set_t *ppset) | |
396 | { | |
397 | processor_set_t pset; | |
398 | ||
399 | pset = PROCESSOR_SET_NULL; | |
400 | if (ip_active(port) && | |
401 | ((ip_kotype(port) == IKOT_PSET) || | |
2d21ac55 | 402 | (matchn && (ip_kotype(port) == IKOT_PSET_NAME)))) { |
1c79356b | 403 | pset = (processor_set_t) port->ip_kobject; |
1c79356b | 404 | } |
2d21ac55 | 405 | |
1c79356b A |
406 | *ppset = pset; |
407 | ip_unlock(port); | |
2d21ac55 | 408 | |
1c79356b A |
409 | return (TRUE); |
410 | } | |
411 | ||
412 | /* | |
413 | * Routine: convert_host_to_port | |
414 | * Purpose: | |
415 | * Convert from a host to a port. | |
416 | * Produces a naked send right which may be invalid. | |
417 | * Conditions: | |
418 | * Nothing locked. | |
419 | */ | |
420 | ||
421 | ipc_port_t | |
422 | convert_host_to_port( | |
423 | host_t host) | |
424 | { | |
425 | ipc_port_t port; | |
426 | ||
55e303ae | 427 | host_get_host_port(host, &port); |
1c79356b A |
428 | return port; |
429 | } | |
430 | ||
431 | /* | |
432 | * Routine: convert_processor_to_port | |
433 | * Purpose: | |
434 | * Convert from a processor to a port. | |
435 | * Produces a naked send right which may be invalid. | |
b0d623f7 | 436 | * Processors are not reference counted, so nothing to release. |
1c79356b A |
437 | * Conditions: |
438 | * Nothing locked. | |
439 | */ | |
440 | ||
441 | ipc_port_t | |
442 | convert_processor_to_port( | |
443 | processor_t processor) | |
444 | { | |
b0d623f7 | 445 | ipc_port_t port = processor->processor_self; |
1c79356b | 446 | |
b0d623f7 A |
447 | if (port != IP_NULL) |
448 | port = ipc_port_make_send(port); | |
1c79356b A |
449 | return port; |
450 | } | |
451 | ||
452 | /* | |
453 | * Routine: convert_pset_to_port | |
454 | * Purpose: | |
455 | * Convert from a pset to a port. | |
b0d623f7 A |
456 | * Produces a naked send right which may be invalid. |
457 | * Processor sets are not reference counted, so nothing to release. | |
1c79356b A |
458 | * Conditions: |
459 | * Nothing locked. | |
460 | */ | |
461 | ||
462 | ipc_port_t | |
463 | convert_pset_to_port( | |
464 | processor_set_t pset) | |
465 | { | |
2d21ac55 | 466 | ipc_port_t port = pset->pset_self; |
1c79356b | 467 | |
2d21ac55 A |
468 | if (port != IP_NULL) |
469 | port = ipc_port_make_send(port); | |
1c79356b | 470 | |
1c79356b A |
471 | return port; |
472 | } | |
473 | ||
474 | /* | |
475 | * Routine: convert_pset_name_to_port | |
476 | * Purpose: | |
477 | * Convert from a pset to a port. | |
b0d623f7 A |
478 | * Produces a naked send right which may be invalid. |
479 | * Processor sets are not reference counted, so nothing to release. | |
1c79356b A |
480 | * Conditions: |
481 | * Nothing locked. | |
482 | */ | |
483 | ||
484 | ipc_port_t | |
485 | convert_pset_name_to_port( | |
486 | processor_set_name_t pset) | |
487 | { | |
2d21ac55 | 488 | ipc_port_t port = pset->pset_name_self; |
1c79356b | 489 | |
2d21ac55 A |
490 | if (port != IP_NULL) |
491 | port = ipc_port_make_send(port); | |
1c79356b | 492 | |
1c79356b A |
493 | return port; |
494 | } | |
495 | ||
496 | /* | |
497 | * Routine: convert_port_to_host_security | |
498 | * Purpose: | |
499 | * Convert from a port to a host security. | |
500 | * Doesn't consume the port ref; the port produced may be null. | |
501 | * Conditions: | |
502 | * Nothing locked. | |
503 | */ | |
504 | ||
505 | host_t | |
506 | convert_port_to_host_security( | |
507 | ipc_port_t port) | |
508 | { | |
509 | host_t host = HOST_NULL; | |
510 | ||
511 | if (IP_VALID(port)) { | |
512 | ip_lock(port); | |
513 | if (ip_active(port) && | |
514 | (ip_kotype(port) == IKOT_HOST_SECURITY)) | |
515 | host = (host_t) port->ip_kobject; | |
516 | ip_unlock(port); | |
517 | } | |
518 | ||
519 | return host; | |
520 | } | |
521 | ||
522 | /* | |
523 | * Routine: host_set_exception_ports [kernel call] | |
524 | * Purpose: | |
525 | * Sets the host exception port, flavor and | |
526 | * behavior for the exception types specified by the mask. | |
527 | * There will be one send right per exception per valid | |
528 | * port. | |
529 | * Conditions: | |
530 | * Nothing locked. If successful, consumes | |
531 | * the supplied send right. | |
532 | * Returns: | |
533 | * KERN_SUCCESS Changed the special port. | |
534 | * KERN_INVALID_ARGUMENT The host_priv is not valid, | |
535 | * Illegal mask bit set. | |
536 | * Illegal exception behavior | |
537 | */ | |
538 | kern_return_t | |
539 | host_set_exception_ports( | |
3e170ce0 | 540 | host_priv_t host_priv, |
1c79356b A |
541 | exception_mask_t exception_mask, |
542 | ipc_port_t new_port, | |
543 | exception_behavior_t new_behavior, | |
544 | thread_state_flavor_t new_flavor) | |
545 | { | |
546 | register int i; | |
547 | ipc_port_t old_port[EXC_TYPES_COUNT]; | |
548 | ||
549 | if (host_priv == HOST_PRIV_NULL) { | |
550 | return KERN_INVALID_ARGUMENT; | |
551 | } | |
552 | ||
b0d623f7 | 553 | if (exception_mask & ~EXC_MASK_VALID) { |
1c79356b A |
554 | return KERN_INVALID_ARGUMENT; |
555 | } | |
556 | ||
557 | if (IP_VALID(new_port)) { | |
2d21ac55 | 558 | switch (new_behavior & ~MACH_EXCEPTION_CODES) { |
1c79356b A |
559 | case EXCEPTION_DEFAULT: |
560 | case EXCEPTION_STATE: | |
561 | case EXCEPTION_STATE_IDENTITY: | |
562 | break; | |
563 | default: | |
564 | return KERN_INVALID_ARGUMENT; | |
565 | } | |
566 | } | |
fe8ab488 A |
567 | |
568 | /* | |
569 | * Check the validity of the thread_state_flavor by calling the | |
570 | * VALID_THREAD_STATE_FLAVOR architecture dependent macro defined in | |
571 | * osfmk/mach/ARCHITECTURE/thread_status.h | |
1c79356b | 572 | */ |
fe8ab488 A |
573 | if (new_flavor != 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor)) |
574 | return (KERN_INVALID_ARGUMENT); | |
575 | ||
3e170ce0 A |
576 | #if CONFIG_MACF |
577 | if (mac_task_check_set_host_exception_ports(current_task(), exception_mask) != 0) | |
578 | return KERN_NO_ACCESS; | |
579 | #endif | |
580 | ||
581 | assert(host_priv == &realhost); | |
582 | ||
1c79356b A |
583 | host_lock(host_priv); |
584 | ||
585 | for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) { | |
586 | if (exception_mask & (1 << i)) { | |
587 | old_port[i] = host_priv->exc_actions[i].port; | |
588 | host_priv->exc_actions[i].port = | |
589 | ipc_port_copy_send(new_port); | |
590 | host_priv->exc_actions[i].behavior = new_behavior; | |
591 | host_priv->exc_actions[i].flavor = new_flavor; | |
592 | } else | |
593 | old_port[i] = IP_NULL; | |
594 | }/* for */ | |
595 | ||
596 | /* | |
597 | * Consume send rights without any lock held. | |
598 | */ | |
599 | host_unlock(host_priv); | |
600 | for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) | |
601 | if (IP_VALID(old_port[i])) | |
602 | ipc_port_release_send(old_port[i]); | |
603 | if (IP_VALID(new_port)) /* consume send right */ | |
604 | ipc_port_release_send(new_port); | |
605 | ||
606 | return KERN_SUCCESS; | |
607 | } | |
608 | ||
609 | /* | |
610 | * Routine: host_get_exception_ports [kernel call] | |
611 | * Purpose: | |
612 | * Clones a send right for each of the host's exception | |
613 | * ports specified in the mask and returns the behaviour | |
614 | * and flavor of said port. | |
615 | * | |
616 | * Returns upto [in} CountCnt elements. | |
617 | * | |
618 | * Conditions: | |
619 | * Nothing locked. | |
620 | * Returns: | |
621 | * KERN_SUCCESS Extracted a send right. | |
622 | * KERN_INVALID_ARGUMENT Invalid host_priv specified, | |
623 | * Invalid special port, | |
624 | * Illegal mask bit set. | |
625 | * KERN_FAILURE The thread is dead. | |
626 | */ | |
627 | kern_return_t | |
628 | host_get_exception_ports( | |
629 | host_priv_t host_priv, | |
630 | exception_mask_t exception_mask, | |
631 | exception_mask_array_t masks, | |
632 | mach_msg_type_number_t * CountCnt, | |
633 | exception_port_array_t ports, | |
634 | exception_behavior_array_t behaviors, | |
635 | thread_state_flavor_array_t flavors ) | |
636 | { | |
91447636 | 637 | unsigned int i, j, count; |
1c79356b A |
638 | |
639 | if (host_priv == HOST_PRIV_NULL) | |
640 | return KERN_INVALID_ARGUMENT; | |
641 | ||
b0d623f7 | 642 | if (exception_mask & ~EXC_MASK_VALID) { |
1c79356b A |
643 | return KERN_INVALID_ARGUMENT; |
644 | } | |
645 | ||
646 | assert (host_priv == &realhost); | |
647 | ||
648 | host_lock(host_priv); | |
649 | ||
650 | count = 0; | |
651 | ||
652 | for (i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT; i++) { | |
653 | if (exception_mask & (1 << i)) { | |
654 | for (j = 0; j < count; j++) { | |
655 | /* | |
656 | * search for an identical entry, if found | |
657 | * set corresponding mask for this exception. | |
658 | */ | |
659 | if (host_priv->exc_actions[i].port == ports[j] && | |
660 | host_priv->exc_actions[i].behavior == behaviors[j] | |
661 | && host_priv->exc_actions[i].flavor == flavors[j]) | |
662 | { | |
663 | masks[j] |= (1 << i); | |
664 | break; | |
665 | } | |
666 | }/* for */ | |
667 | if (j == count) { | |
668 | masks[j] = (1 << i); | |
669 | ports[j] = | |
670 | ipc_port_copy_send(host_priv->exc_actions[i].port); | |
671 | behaviors[j] = host_priv->exc_actions[i].behavior; | |
672 | flavors[j] = host_priv->exc_actions[i].flavor; | |
673 | count++; | |
674 | if (count > *CountCnt) { | |
675 | break; | |
676 | } | |
677 | } | |
678 | } | |
679 | }/* for */ | |
680 | host_unlock(host_priv); | |
681 | ||
682 | *CountCnt = count; | |
683 | return KERN_SUCCESS; | |
684 | } | |
685 | ||
686 | kern_return_t | |
687 | host_swap_exception_ports( | |
3e170ce0 | 688 | host_priv_t host_priv, |
1c79356b A |
689 | exception_mask_t exception_mask, |
690 | ipc_port_t new_port, | |
691 | exception_behavior_t new_behavior, | |
692 | thread_state_flavor_t new_flavor, | |
693 | exception_mask_array_t masks, | |
694 | mach_msg_type_number_t * CountCnt, | |
695 | exception_port_array_t ports, | |
696 | exception_behavior_array_t behaviors, | |
697 | thread_state_flavor_array_t flavors ) | |
698 | { | |
91447636 | 699 | unsigned int i, |
1c79356b A |
700 | j, |
701 | count; | |
702 | ipc_port_t old_port[EXC_TYPES_COUNT]; | |
703 | ||
704 | if (host_priv == HOST_PRIV_NULL) | |
705 | return KERN_INVALID_ARGUMENT; | |
706 | ||
b0d623f7 | 707 | if (exception_mask & ~EXC_MASK_VALID) { |
1c79356b A |
708 | return KERN_INVALID_ARGUMENT; |
709 | } | |
710 | ||
711 | if (IP_VALID(new_port)) { | |
712 | switch (new_behavior) { | |
713 | case EXCEPTION_DEFAULT: | |
714 | case EXCEPTION_STATE: | |
715 | case EXCEPTION_STATE_IDENTITY: | |
716 | break; | |
717 | default: | |
718 | return KERN_INVALID_ARGUMENT; | |
719 | } | |
720 | } | |
39236c6e | 721 | |
fe8ab488 A |
722 | if (new_flavor != 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor)) |
723 | return (KERN_INVALID_ARGUMENT); | |
1c79356b | 724 | |
3e170ce0 A |
725 | #if CONFIG_MACF |
726 | if (mac_task_check_set_host_exception_ports(current_task(), exception_mask) != 0) | |
727 | return KERN_NO_ACCESS; | |
728 | #endif /* CONFIG_MACF */ | |
729 | ||
1c79356b A |
730 | host_lock(host_priv); |
731 | ||
39236c6e A |
732 | assert(EXC_TYPES_COUNT > FIRST_EXCEPTION); |
733 | for (count=0, i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT && count < *CountCnt; i++) { | |
1c79356b A |
734 | if (exception_mask & (1 << i)) { |
735 | for (j = 0; j < count; j++) { | |
736 | /* | |
737 | * search for an identical entry, if found | |
738 | * set corresponding mask for this exception. | |
739 | */ | |
740 | if (host_priv->exc_actions[i].port == ports[j] && | |
741 | host_priv->exc_actions[i].behavior == behaviors[j] | |
742 | && host_priv->exc_actions[i].flavor == flavors[j]) | |
743 | { | |
744 | masks[j] |= (1 << i); | |
745 | break; | |
746 | } | |
747 | }/* for */ | |
748 | if (j == count) { | |
749 | masks[j] = (1 << i); | |
750 | ports[j] = | |
751 | ipc_port_copy_send(host_priv->exc_actions[i].port); | |
752 | behaviors[j] = host_priv->exc_actions[i].behavior; | |
753 | flavors[j] = host_priv->exc_actions[i].flavor; | |
754 | count++; | |
755 | } | |
756 | old_port[i] = host_priv->exc_actions[i].port; | |
757 | host_priv->exc_actions[i].port = | |
758 | ipc_port_copy_send(new_port); | |
759 | host_priv->exc_actions[i].behavior = new_behavior; | |
760 | host_priv->exc_actions[i].flavor = new_flavor; | |
1c79356b A |
761 | } else |
762 | old_port[i] = IP_NULL; | |
763 | }/* for */ | |
764 | host_unlock(host_priv); | |
765 | ||
766 | /* | |
767 | * Consume send rights without any lock held. | |
768 | */ | |
39236c6e | 769 | while (--i >= FIRST_EXCEPTION) { |
1c79356b A |
770 | if (IP_VALID(old_port[i])) |
771 | ipc_port_release_send(old_port[i]); | |
39236c6e A |
772 | } |
773 | ||
1c79356b A |
774 | if (IP_VALID(new_port)) /* consume send right */ |
775 | ipc_port_release_send(new_port); | |
776 | *CountCnt = count; | |
777 | ||
778 | return KERN_SUCCESS; | |
779 | } |