]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_host.c
xnu-3247.1.106.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_host.c
1 /*
2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
67 #include <mach/host_priv_server.h>
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
79 #if CONFIG_MACF
80 #include <security/mac_mach_internal.h>
81 #endif
82
83 /*
84 * Forward declarations
85 */
86
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
95 extern lck_grp_t host_notify_lock_grp;
96 extern lck_attr_t host_notify_lock_attr;
97
98 void ipc_host_init(void)
99 {
100 ipc_port_t port;
101 int i;
102
103 lck_mtx_init(&realhost.lock, &host_notify_lock_grp, &host_notify_lock_attr);
104
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
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));
115
116 port = ipc_port_alloc_kernel();
117 if (port == IP_NULL)
118 panic("ipc_host_init");
119
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));
123
124 port = ipc_port_alloc_kernel();
125 if (port == IP_NULL)
126 panic("ipc_host_init");
127
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));
131
132 /* the rest of the special ports will be set up later */
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 */
141 ipc_pset_init(&pset0);
142 ipc_pset_enable(&pset0);
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
163 host_self_trap(
164 __unused struct host_self_trap_args *args)
165 {
166 ipc_port_t sright;
167 mach_port_name_t name;
168
169 sright = ipc_port_copy_send(current_task()->itk_host);
170 name = ipc_port_copyout_send(sright, current_space());
171 return name;
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 }
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 {
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);
241 }
242
243 /*
244 * processor_set_default:
245 *
246 * Return ports for manipulating default_processor set.
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
256 *pset = &pset0;
257
258 return (KERN_SUCCESS);
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) ||
402 (matchn && (ip_kotype(port) == IKOT_PSET_NAME)))) {
403 pset = (processor_set_t) port->ip_kobject;
404 }
405
406 *ppset = pset;
407 ip_unlock(port);
408
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
427 host_get_host_port(host, &port);
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.
436 * Processors are not reference counted, so nothing to release.
437 * Conditions:
438 * Nothing locked.
439 */
440
441 ipc_port_t
442 convert_processor_to_port(
443 processor_t processor)
444 {
445 ipc_port_t port = processor->processor_self;
446
447 if (port != IP_NULL)
448 port = ipc_port_make_send(port);
449 return port;
450 }
451
452 /*
453 * Routine: convert_pset_to_port
454 * Purpose:
455 * Convert from a pset to a port.
456 * Produces a naked send right which may be invalid.
457 * Processor sets are not reference counted, so nothing to release.
458 * Conditions:
459 * Nothing locked.
460 */
461
462 ipc_port_t
463 convert_pset_to_port(
464 processor_set_t pset)
465 {
466 ipc_port_t port = pset->pset_self;
467
468 if (port != IP_NULL)
469 port = ipc_port_make_send(port);
470
471 return port;
472 }
473
474 /*
475 * Routine: convert_pset_name_to_port
476 * Purpose:
477 * Convert from a pset to a port.
478 * Produces a naked send right which may be invalid.
479 * Processor sets are not reference counted, so nothing to release.
480 * Conditions:
481 * Nothing locked.
482 */
483
484 ipc_port_t
485 convert_pset_name_to_port(
486 processor_set_name_t pset)
487 {
488 ipc_port_t port = pset->pset_name_self;
489
490 if (port != IP_NULL)
491 port = ipc_port_make_send(port);
492
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(
540 host_priv_t host_priv,
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
553 if (exception_mask & ~EXC_MASK_VALID) {
554 return KERN_INVALID_ARGUMENT;
555 }
556
557 if (IP_VALID(new_port)) {
558 switch (new_behavior & ~MACH_EXCEPTION_CODES) {
559 case EXCEPTION_DEFAULT:
560 case EXCEPTION_STATE:
561 case EXCEPTION_STATE_IDENTITY:
562 break;
563 default:
564 return KERN_INVALID_ARGUMENT;
565 }
566 }
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
572 */
573 if (new_flavor != 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor))
574 return (KERN_INVALID_ARGUMENT);
575
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
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 {
637 unsigned int i, j, count;
638
639 if (host_priv == HOST_PRIV_NULL)
640 return KERN_INVALID_ARGUMENT;
641
642 if (exception_mask & ~EXC_MASK_VALID) {
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(
688 host_priv_t host_priv,
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 {
699 unsigned int i,
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
707 if (exception_mask & ~EXC_MASK_VALID) {
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 }
721
722 if (new_flavor != 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor))
723 return (KERN_INVALID_ARGUMENT);
724
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
730 host_lock(host_priv);
731
732 assert(EXC_TYPES_COUNT > FIRST_EXCEPTION);
733 for (count=0, i = FIRST_EXCEPTION; i < EXC_TYPES_COUNT && count < *CountCnt; i++) {
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;
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 */
769 while (--i >= FIRST_EXCEPTION) {
770 if (IP_VALID(old_port[i]))
771 ipc_port_release_send(old_port[i]);
772 }
773
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 }