2 * Copyright (c) 2000-2009 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@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
62 * Routines to implement host ports.
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>
76 #include <ipc/ipc_port.h>
77 #include <ipc/ipc_space.h>
80 #include <security/mac_mach_internal.h>
84 * Forward declarations
89 ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
);
92 * ipc_host_init: set up various things.
95 extern lck_grp_t host_notify_lock_grp
;
103 lck_mtx_init(&realhost
.lock
, &host_notify_lock_grp
, LCK_ATTR_NULL
);
106 * Allocate and set up the two host ports.
108 port
= ipc_kobject_alloc_port((ipc_kobject_t
) &realhost
, IKOT_HOST_SECURITY
,
109 IPC_KOBJECT_ALLOC_MAKE_SEND
);
110 kernel_set_special_port(&realhost
, HOST_SECURITY_PORT
, port
);
112 port
= ipc_kobject_alloc_port((ipc_kobject_t
) &realhost
, IKOT_HOST
,
113 IPC_KOBJECT_ALLOC_MAKE_SEND
);
114 kernel_set_special_port(&realhost
, HOST_PORT
, port
);
116 port
= ipc_kobject_alloc_port((ipc_kobject_t
) &realhost
, IKOT_HOST_PRIV
,
117 IPC_KOBJECT_ALLOC_MAKE_SEND
);
118 kernel_set_special_port(&realhost
, HOST_PRIV_PORT
, port
);
120 /* the rest of the special ports will be set up later */
122 bzero(&realhost
.exc_actions
[0], sizeof(realhost
.exc_actions
[0]));
123 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
124 realhost
.exc_actions
[i
].port
= IP_NULL
;
125 /* The mac framework is not yet initialized, so we defer
126 * initializing the labels to later, when they are set
127 * for the first time. */
128 realhost
.exc_actions
[i
].label
= NULL
;
129 /* initialize the entire exception action struct */
130 realhost
.exc_actions
[i
].behavior
= 0;
131 realhost
.exc_actions
[i
].flavor
= 0;
132 realhost
.exc_actions
[i
].privileged
= FALSE
;
136 * Set up ipc for default processor set.
138 ipc_pset_init(&pset0
);
139 ipc_pset_enable(&pset0
);
142 * And for master processor
144 ipc_processor_init(master_processor
);
145 ipc_processor_enable(master_processor
);
149 * Routine: host_self_trap [mach trap]
151 * Give the caller send rights for his own host port.
155 * MACH_PORT_NULL if there are any resource failures
161 __unused
struct host_self_trap_args
*args
)
163 task_t self
= current_task();
165 mach_port_name_t name
;
168 sright
= ipc_port_copy_send(self
->itk_host
);
170 name
= ipc_port_copyout_send(sright
, current_space());
175 * ipc_processor_init:
177 * Initialize ipc access to processor by allocating port.
182 processor_t processor
)
186 port
= ipc_port_alloc_kernel();
187 if (port
== IP_NULL
) {
188 panic("ipc_processor_init");
190 processor
->processor_self
= port
;
194 * ipc_processor_enable:
196 * Enable ipc control of processor by setting port object.
199 ipc_processor_enable(
200 processor_t processor
)
204 myport
= processor
->processor_self
;
205 ipc_kobject_set(myport
, (ipc_kobject_t
) processor
, IKOT_PROCESSOR
);
211 * Initialize ipc control of a processor set by allocating its ports.
216 processor_set_t pset
)
220 port
= ipc_port_alloc_kernel();
221 if (port
== IP_NULL
) {
222 panic("ipc_pset_init");
224 pset
->pset_self
= port
;
226 port
= ipc_port_alloc_kernel();
227 if (port
== IP_NULL
) {
228 panic("ipc_pset_init");
230 pset
->pset_name_self
= port
;
236 * Enable ipc access to a processor set.
240 processor_set_t pset
)
242 ipc_kobject_set(pset
->pset_self
, (ipc_kobject_t
) pset
, IKOT_PSET
);
243 ipc_kobject_set(pset
->pset_name_self
, (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
247 * processor_set_default:
249 * Return ports for manipulating default_processor set.
252 processor_set_default(
254 processor_set_t
*pset
)
256 if (host
== HOST_NULL
) {
257 return KERN_INVALID_ARGUMENT
;
266 * Routine: convert_port_to_host
268 * Convert from a port to a host.
269 * Doesn't consume the port ref; the host produced may be null.
275 convert_port_to_host(
278 host_t host
= HOST_NULL
;
280 if (IP_VALID(port
)) {
281 if (ip_kotype(port
) == IKOT_HOST
||
282 ip_kotype(port
) == IKOT_HOST_PRIV
) {
283 host
= (host_t
) ip_get_kobject(port
);
284 require_ip_active(port
);
291 * Routine: convert_port_to_host_priv
293 * Convert from a port to a host.
294 * Doesn't consume the port ref; the host produced may be null.
300 convert_port_to_host_priv(
303 host_t host
= HOST_NULL
;
305 /* reject translation if itk_host is not host_priv */
306 if (port
!= current_task()->itk_host
) {
310 if (IP_VALID(port
)) {
312 if (ip_active(port
) &&
313 (ip_kotype(port
) == IKOT_HOST_PRIV
)) {
314 assert(ip_get_kobject(port
) == &realhost
);
324 * Routine: convert_port_to_processor
326 * Convert from a port to a processor.
327 * Doesn't consume the port ref;
328 * the processor produced may be null.
334 convert_port_to_processor(
337 processor_t processor
= PROCESSOR_NULL
;
339 if (IP_VALID(port
)) {
341 if (ip_active(port
) &&
342 (ip_kotype(port
) == IKOT_PROCESSOR
)) {
343 processor
= (processor_t
) ip_get_kobject(port
);
352 * Routine: convert_port_to_pset
354 * Convert from a port to a pset.
355 * Doesn't consume the port ref; produces a pset ref,
362 convert_port_to_pset(
366 processor_set_t pset
= PROCESSOR_SET_NULL
;
369 while (!r
&& IP_VALID(port
)) {
371 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
378 * Routine: convert_port_to_pset_name
380 * Convert from a port to a pset.
381 * Doesn't consume the port ref; produces a pset ref,
388 convert_port_to_pset_name(
392 processor_set_t pset
= PROCESSOR_SET_NULL
;
395 while (!r
&& IP_VALID(port
)) {
397 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
404 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
406 processor_set_t pset
;
408 pset
= PROCESSOR_SET_NULL
;
409 if (ip_active(port
) &&
410 ((ip_kotype(port
) == IKOT_PSET
) ||
411 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
412 pset
= (processor_set_t
) ip_get_kobject(port
);
422 * Routine: convert_host_to_port
424 * Convert from a host to a port.
425 * Produces a naked send right which may be invalid.
431 convert_host_to_port(
436 host_get_host_port(host
, &port
);
441 * Routine: convert_processor_to_port
443 * Convert from a processor to a port.
444 * Produces a naked send right which may be invalid.
445 * Processors are not reference counted, so nothing to release.
451 convert_processor_to_port(
452 processor_t processor
)
454 ipc_port_t port
= processor
->processor_self
;
456 if (port
!= IP_NULL
) {
457 port
= ipc_port_make_send(port
);
463 * Routine: convert_pset_to_port
465 * Convert from a pset to a port.
466 * Produces a naked send right which may be invalid.
467 * Processor sets are not reference counted, so nothing to release.
473 convert_pset_to_port(
474 processor_set_t pset
)
476 ipc_port_t port
= pset
->pset_self
;
478 if (port
!= IP_NULL
) {
479 port
= ipc_port_make_send(port
);
486 * Routine: convert_pset_name_to_port
488 * Convert from a pset to a port.
489 * Produces a naked send right which may be invalid.
490 * Processor sets are not reference counted, so nothing to release.
496 convert_pset_name_to_port(
497 processor_set_name_t pset
)
499 ipc_port_t port
= pset
->pset_name_self
;
501 if (port
!= IP_NULL
) {
502 port
= ipc_port_make_send(port
);
509 * Routine: convert_port_to_host_security
511 * Convert from a port to a host security.
512 * Doesn't consume the port ref; the port produced may be null.
518 convert_port_to_host_security(
521 host_t host
= HOST_NULL
;
523 if (IP_VALID(port
)) {
525 if (ip_active(port
) &&
526 (ip_kotype(port
) == IKOT_HOST_SECURITY
)) {
527 host
= (host_t
) ip_get_kobject(port
);
536 * Routine: host_set_exception_ports [kernel call]
538 * Sets the host exception port, flavor and
539 * behavior for the exception types specified by the mask.
540 * There will be one send right per exception per valid
543 * Nothing locked. If successful, consumes
544 * the supplied send right.
546 * KERN_SUCCESS Changed the special port.
547 * KERN_INVALID_ARGUMENT The host_priv is not valid,
548 * Illegal mask bit set.
549 * Illegal exception behavior
552 host_set_exception_ports(
553 host_priv_t host_priv
,
554 exception_mask_t exception_mask
,
556 exception_behavior_t new_behavior
,
557 thread_state_flavor_t new_flavor
)
560 ipc_port_t old_port
[EXC_TYPES_COUNT
];
563 struct label
*deferred_labels
[EXC_TYPES_COUNT
];
564 struct label
*new_label
;
567 if (host_priv
== HOST_PRIV_NULL
) {
568 return KERN_INVALID_ARGUMENT
;
571 if (exception_mask
& ~EXC_MASK_VALID
) {
572 return KERN_INVALID_ARGUMENT
;
575 if (IP_VALID(new_port
)) {
576 switch (new_behavior
& ~MACH_EXCEPTION_MASK
) {
577 case EXCEPTION_DEFAULT
:
578 case EXCEPTION_STATE
:
579 case EXCEPTION_STATE_IDENTITY
:
582 return KERN_INVALID_ARGUMENT
;
587 * Check the validity of the thread_state_flavor by calling the
588 * VALID_THREAD_STATE_FLAVOR architecture dependent macro defined in
589 * osfmk/mach/ARCHITECTURE/thread_status.h
591 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
)) {
592 return KERN_INVALID_ARGUMENT
;
596 if (mac_task_check_set_host_exception_ports(current_task(), exception_mask
) != 0) {
597 return KERN_NO_ACCESS
;
600 new_label
= mac_exc_create_label_for_current_proc();
602 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
603 if (host_priv
->exc_actions
[i
].label
== NULL
) {
604 deferred_labels
[i
] = mac_exc_create_label();
606 deferred_labels
[i
] = NULL
;
611 host_lock(host_priv
);
613 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
615 if (host_priv
->exc_actions
[i
].label
== NULL
) {
616 // Lazy initialization (see ipc_port_init).
617 mac_exc_associate_action_label(&host_priv
->exc_actions
[i
], deferred_labels
[i
]);
618 deferred_labels
[i
] = NULL
; // Label is used, do not free.
622 if ((exception_mask
& (1 << i
))
624 && mac_exc_update_action_label(&host_priv
->exc_actions
[i
], new_label
) == 0
627 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
629 host_priv
->exc_actions
[i
].port
=
630 ipc_port_copy_send(new_port
);
631 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
632 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
634 old_port
[i
] = IP_NULL
;
639 * Consume send rights without any lock held.
641 host_unlock(host_priv
);
644 mac_exc_free_label(new_label
);
647 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
648 if (IP_VALID(old_port
[i
])) {
649 ipc_port_release_send(old_port
[i
]);
652 if (deferred_labels
[i
] != NULL
) {
653 /* Deferred label went unused: Another thread has completed the lazy initialization. */
654 mac_exc_free_label(deferred_labels
[i
]);
658 if (IP_VALID(new_port
)) { /* consume send right */
659 ipc_port_release_send(new_port
);
666 * Routine: host_get_exception_ports [kernel call]
668 * Clones a send right for each of the host's exception
669 * ports specified in the mask and returns the behaviour
670 * and flavor of said port.
672 * Returns upto [in} CountCnt elements.
677 * KERN_SUCCESS Extracted a send right.
678 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
679 * Invalid special port,
680 * Illegal mask bit set.
681 * KERN_FAILURE The thread is dead.
684 host_get_exception_ports(
685 host_priv_t host_priv
,
686 exception_mask_t exception_mask
,
687 exception_mask_array_t masks
,
688 mach_msg_type_number_t
* CountCnt
,
689 exception_port_array_t ports
,
690 exception_behavior_array_t behaviors
,
691 thread_state_flavor_array_t flavors
)
693 unsigned int i
, j
, count
;
695 if (host_priv
== HOST_PRIV_NULL
) {
696 return KERN_INVALID_ARGUMENT
;
699 if (exception_mask
& ~EXC_MASK_VALID
) {
700 return KERN_INVALID_ARGUMENT
;
703 host_lock(host_priv
);
707 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
708 if (exception_mask
& (1 << i
)) {
709 for (j
= 0; j
< count
; j
++) {
711 * search for an identical entry, if found
712 * set corresponding mask for this exception.
714 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
715 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
716 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
]) {
717 masks
[j
] |= (1 << i
);
721 if (j
== count
&& count
< *CountCnt
) {
724 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
725 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
726 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
731 host_unlock(host_priv
);
738 host_swap_exception_ports(
739 host_priv_t host_priv
,
740 exception_mask_t exception_mask
,
742 exception_behavior_t new_behavior
,
743 thread_state_flavor_t new_flavor
,
744 exception_mask_array_t masks
,
745 mach_msg_type_number_t
* CountCnt
,
746 exception_port_array_t ports
,
747 exception_behavior_array_t behaviors
,
748 thread_state_flavor_array_t flavors
)
753 ipc_port_t old_port
[EXC_TYPES_COUNT
];
756 struct label
*deferred_labels
[EXC_TYPES_COUNT
];
757 struct label
*new_label
;
760 if (host_priv
== HOST_PRIV_NULL
) {
761 return KERN_INVALID_ARGUMENT
;
764 if (exception_mask
& ~EXC_MASK_VALID
) {
765 return KERN_INVALID_ARGUMENT
;
768 if (IP_VALID(new_port
)) {
769 switch (new_behavior
) {
770 case EXCEPTION_DEFAULT
:
771 case EXCEPTION_STATE
:
772 case EXCEPTION_STATE_IDENTITY
:
775 return KERN_INVALID_ARGUMENT
;
779 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
)) {
780 return KERN_INVALID_ARGUMENT
;
784 if (mac_task_check_set_host_exception_ports(current_task(), exception_mask
) != 0) {
785 return KERN_NO_ACCESS
;
788 new_label
= mac_exc_create_label_for_current_proc();
790 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
791 if (host_priv
->exc_actions
[i
].label
== NULL
) {
792 deferred_labels
[i
] = mac_exc_create_label();
794 deferred_labels
[i
] = NULL
;
797 #endif /* CONFIG_MACF */
799 host_lock(host_priv
);
801 assert(EXC_TYPES_COUNT
> FIRST_EXCEPTION
);
802 for (count
= 0, i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
&& count
< *CountCnt
; i
++) {
804 if (host_priv
->exc_actions
[i
].label
== NULL
) {
805 // Lazy initialization (see ipc_port_init).
806 mac_exc_associate_action_label(&host_priv
->exc_actions
[i
], deferred_labels
[i
]);
807 deferred_labels
[i
] = NULL
; // Label is used, do not free.
811 if ((exception_mask
& (1 << i
))
813 && mac_exc_update_action_label(&host_priv
->exc_actions
[i
], new_label
) == 0
816 for (j
= 0; j
< count
; j
++) {
818 * search for an identical entry, if found
819 * set corresponding mask for this exception.
821 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
822 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
823 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
]) {
824 masks
[j
] |= (1 << i
);
831 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
832 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
833 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
836 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
837 host_priv
->exc_actions
[i
].port
=
838 ipc_port_copy_send(new_port
);
839 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
840 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
842 old_port
[i
] = IP_NULL
;
845 host_unlock(host_priv
);
848 mac_exc_free_label(new_label
);
852 * Consume send rights without any lock held.
854 while (--i
>= FIRST_EXCEPTION
) {
855 if (IP_VALID(old_port
[i
])) {
856 ipc_port_release_send(old_port
[i
]);
859 if (deferred_labels
[i
] != NULL
) {
860 mac_exc_free_label(deferred_labels
[i
]); // Label unused.
865 if (IP_VALID(new_port
)) { /* consume send right */
866 ipc_port_release_send(new_port
);