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
;
96 extern lck_attr_t host_notify_lock_attr
;
98 void ipc_host_init(void)
103 lck_mtx_init(&realhost
.lock
, &host_notify_lock_grp
, &host_notify_lock_attr
);
106 * Allocate and set up the two host ports.
108 port
= ipc_port_alloc_kernel();
110 panic("ipc_host_init");
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
));
116 port
= ipc_port_alloc_kernel();
118 panic("ipc_host_init");
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
));
124 port
= ipc_port_alloc_kernel();
126 panic("ipc_host_init");
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
));
132 /* the rest of the special ports will be set up later */
134 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
135 realhost
.exc_actions
[i
].port
= IP_NULL
;
136 realhost
.exc_actions
[i
].label
= NULL
;
137 /* The mac framework is not yet initialized, so we defer
138 * initializing the labels to later, when they are set
139 * for the first time. */
143 * Set up ipc for default processor set.
145 ipc_pset_init(&pset0
);
146 ipc_pset_enable(&pset0
);
149 * And for master processor
151 ipc_processor_init(master_processor
);
152 ipc_processor_enable(master_processor
);
156 * Routine: host_self_trap [mach trap]
158 * Give the caller send rights for his own host port.
162 * MACH_PORT_NULL if there are any resource failures
168 __unused
struct host_self_trap_args
*args
)
170 task_t self
= current_task();
172 mach_port_name_t name
;
175 sright
= ipc_port_copy_send(self
->itk_host
);
177 name
= ipc_port_copyout_send(sright
, current_space());
182 * ipc_processor_init:
184 * Initialize ipc access to processor by allocating port.
189 processor_t processor
)
193 port
= ipc_port_alloc_kernel();
195 panic("ipc_processor_init");
196 processor
->processor_self
= port
;
200 * ipc_processor_enable:
202 * Enable ipc control of processor by setting port object.
205 ipc_processor_enable(
206 processor_t processor
)
210 myport
= processor
->processor_self
;
211 ipc_kobject_set(myport
, (ipc_kobject_t
) processor
, IKOT_PROCESSOR
);
217 * Initialize ipc control of a processor set by allocating its ports.
222 processor_set_t pset
)
226 port
= ipc_port_alloc_kernel();
228 panic("ipc_pset_init");
229 pset
->pset_self
= port
;
231 port
= ipc_port_alloc_kernel();
233 panic("ipc_pset_init");
234 pset
->pset_name_self
= port
;
240 * Enable ipc access to a processor set.
244 processor_set_t pset
)
246 ipc_kobject_set(pset
->pset_self
, (ipc_kobject_t
) pset
, IKOT_PSET
);
247 ipc_kobject_set(pset
->pset_name_self
, (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
251 * processor_set_default:
253 * Return ports for manipulating default_processor set.
256 processor_set_default(
258 processor_set_t
*pset
)
260 if (host
== HOST_NULL
)
261 return(KERN_INVALID_ARGUMENT
);
265 return (KERN_SUCCESS
);
269 * Routine: convert_port_to_host
271 * Convert from a port to a host.
272 * Doesn't consume the port ref; the host produced may be null.
278 convert_port_to_host(
281 host_t host
= HOST_NULL
;
283 if (IP_VALID(port
)) {
284 if (ip_kotype(port
) == IKOT_HOST
||
285 ip_kotype(port
) == IKOT_HOST_PRIV
) {
286 host
= (host_t
) port
->ip_kobject
;
287 assert(ip_active(port
));
294 * Routine: convert_port_to_host_priv
296 * Convert from a port to a host.
297 * Doesn't consume the port ref; the host produced may be null.
303 convert_port_to_host_priv(
306 host_t host
= HOST_NULL
;
308 if (IP_VALID(port
)) {
310 if (ip_active(port
) &&
311 (ip_kotype(port
) == IKOT_HOST_PRIV
))
312 host
= (host_t
) port
->ip_kobject
;
320 * Routine: convert_port_to_processor
322 * Convert from a port to a processor.
323 * Doesn't consume the port ref;
324 * the processor produced may be null.
330 convert_port_to_processor(
333 processor_t processor
= PROCESSOR_NULL
;
335 if (IP_VALID(port
)) {
337 if (ip_active(port
) &&
338 (ip_kotype(port
) == IKOT_PROCESSOR
))
339 processor
= (processor_t
) port
->ip_kobject
;
347 * Routine: convert_port_to_pset
349 * Convert from a port to a pset.
350 * Doesn't consume the port ref; produces a pset ref,
357 convert_port_to_pset(
361 processor_set_t pset
= PROCESSOR_SET_NULL
;
364 while (!r
&& IP_VALID(port
)) {
366 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
373 * Routine: convert_port_to_pset_name
375 * Convert from a port to a pset.
376 * Doesn't consume the port ref; produces a pset ref,
383 convert_port_to_pset_name(
387 processor_set_t pset
= PROCESSOR_SET_NULL
;
390 while (!r
&& IP_VALID(port
)) {
392 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
399 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
401 processor_set_t pset
;
403 pset
= PROCESSOR_SET_NULL
;
404 if (ip_active(port
) &&
405 ((ip_kotype(port
) == IKOT_PSET
) ||
406 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
407 pset
= (processor_set_t
) port
->ip_kobject
;
417 * Routine: convert_host_to_port
419 * Convert from a host to a port.
420 * Produces a naked send right which may be invalid.
426 convert_host_to_port(
431 host_get_host_port(host
, &port
);
436 * Routine: convert_processor_to_port
438 * Convert from a processor to a port.
439 * Produces a naked send right which may be invalid.
440 * Processors are not reference counted, so nothing to release.
446 convert_processor_to_port(
447 processor_t processor
)
449 ipc_port_t port
= processor
->processor_self
;
452 port
= ipc_port_make_send(port
);
457 * Routine: convert_pset_to_port
459 * Convert from a pset to a port.
460 * Produces a naked send right which may be invalid.
461 * Processor sets are not reference counted, so nothing to release.
467 convert_pset_to_port(
468 processor_set_t pset
)
470 ipc_port_t port
= pset
->pset_self
;
473 port
= ipc_port_make_send(port
);
479 * Routine: convert_pset_name_to_port
481 * Convert from a pset to a port.
482 * Produces a naked send right which may be invalid.
483 * Processor sets are not reference counted, so nothing to release.
489 convert_pset_name_to_port(
490 processor_set_name_t pset
)
492 ipc_port_t port
= pset
->pset_name_self
;
495 port
= ipc_port_make_send(port
);
501 * Routine: convert_port_to_host_security
503 * Convert from a port to a host security.
504 * Doesn't consume the port ref; the port produced may be null.
510 convert_port_to_host_security(
513 host_t host
= HOST_NULL
;
515 if (IP_VALID(port
)) {
517 if (ip_active(port
) &&
518 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
519 host
= (host_t
) port
->ip_kobject
;
527 * Routine: host_set_exception_ports [kernel call]
529 * Sets the host exception port, flavor and
530 * behavior for the exception types specified by the mask.
531 * There will be one send right per exception per valid
534 * Nothing locked. If successful, consumes
535 * the supplied send right.
537 * KERN_SUCCESS Changed the special port.
538 * KERN_INVALID_ARGUMENT The host_priv is not valid,
539 * Illegal mask bit set.
540 * Illegal exception behavior
543 host_set_exception_ports(
544 host_priv_t host_priv
,
545 exception_mask_t exception_mask
,
547 exception_behavior_t new_behavior
,
548 thread_state_flavor_t new_flavor
)
551 ipc_port_t old_port
[EXC_TYPES_COUNT
];
553 if (host_priv
== HOST_PRIV_NULL
) {
554 return KERN_INVALID_ARGUMENT
;
557 if (exception_mask
& ~EXC_MASK_VALID
) {
558 return KERN_INVALID_ARGUMENT
;
561 if (IP_VALID(new_port
)) {
562 switch (new_behavior
& ~MACH_EXCEPTION_CODES
) {
563 case EXCEPTION_DEFAULT
:
564 case EXCEPTION_STATE
:
565 case EXCEPTION_STATE_IDENTITY
:
568 return KERN_INVALID_ARGUMENT
;
573 * Check the validity of the thread_state_flavor by calling the
574 * VALID_THREAD_STATE_FLAVOR architecture dependent macro defined in
575 * osfmk/mach/ARCHITECTURE/thread_status.h
577 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
))
578 return (KERN_INVALID_ARGUMENT
);
581 if (mac_task_check_set_host_exception_ports(current_task(), exception_mask
) != 0)
582 return KERN_NO_ACCESS
;
585 assert(host_priv
== &realhost
);
587 host_lock(host_priv
);
589 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
591 if (host_priv
->exc_actions
[i
].label
== NULL
) {
592 // Lazy initialization (see ipc_port_init).
593 mac_exc_action_label_init(host_priv
->exc_actions
+ i
);
597 if ((exception_mask
& (1 << i
))
599 && mac_exc_action_label_update(current_task(), host_priv
->exc_actions
+ i
) == 0
602 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
604 host_priv
->exc_actions
[i
].port
=
605 ipc_port_copy_send(new_port
);
606 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
607 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
609 old_port
[i
] = IP_NULL
;
614 * Consume send rights without any lock held.
616 host_unlock(host_priv
);
617 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
618 if (IP_VALID(old_port
[i
]))
619 ipc_port_release_send(old_port
[i
]);
620 if (IP_VALID(new_port
)) /* consume send right */
621 ipc_port_release_send(new_port
);
627 * Routine: host_get_exception_ports [kernel call]
629 * Clones a send right for each of the host's exception
630 * ports specified in the mask and returns the behaviour
631 * and flavor of said port.
633 * Returns upto [in} CountCnt elements.
638 * KERN_SUCCESS Extracted a send right.
639 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
640 * Invalid special port,
641 * Illegal mask bit set.
642 * KERN_FAILURE The thread is dead.
645 host_get_exception_ports(
646 host_priv_t host_priv
,
647 exception_mask_t exception_mask
,
648 exception_mask_array_t masks
,
649 mach_msg_type_number_t
* CountCnt
,
650 exception_port_array_t ports
,
651 exception_behavior_array_t behaviors
,
652 thread_state_flavor_array_t flavors
)
654 unsigned int i
, j
, count
;
656 if (host_priv
== HOST_PRIV_NULL
)
657 return KERN_INVALID_ARGUMENT
;
659 if (exception_mask
& ~EXC_MASK_VALID
) {
660 return KERN_INVALID_ARGUMENT
;
663 assert (host_priv
== &realhost
);
665 host_lock(host_priv
);
669 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
671 if (host_priv
->exc_actions
[i
].label
== NULL
) {
672 // Lazy initialization (see ipc_port_init).
673 mac_exc_action_label_init(host_priv
->exc_actions
+ i
);
677 if (exception_mask
& (1 << i
)) {
678 for (j
= 0; j
< count
; j
++) {
680 * search for an identical entry, if found
681 * set corresponding mask for this exception.
683 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
684 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
685 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
687 masks
[j
] |= (1 << i
);
694 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
695 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
696 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
698 if (count
> *CountCnt
) {
704 host_unlock(host_priv
);
711 host_swap_exception_ports(
712 host_priv_t host_priv
,
713 exception_mask_t exception_mask
,
715 exception_behavior_t new_behavior
,
716 thread_state_flavor_t new_flavor
,
717 exception_mask_array_t masks
,
718 mach_msg_type_number_t
* CountCnt
,
719 exception_port_array_t ports
,
720 exception_behavior_array_t behaviors
,
721 thread_state_flavor_array_t flavors
)
726 ipc_port_t old_port
[EXC_TYPES_COUNT
];
728 if (host_priv
== HOST_PRIV_NULL
)
729 return KERN_INVALID_ARGUMENT
;
731 if (exception_mask
& ~EXC_MASK_VALID
) {
732 return KERN_INVALID_ARGUMENT
;
735 if (IP_VALID(new_port
)) {
736 switch (new_behavior
) {
737 case EXCEPTION_DEFAULT
:
738 case EXCEPTION_STATE
:
739 case EXCEPTION_STATE_IDENTITY
:
742 return KERN_INVALID_ARGUMENT
;
746 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
))
747 return (KERN_INVALID_ARGUMENT
);
750 if (mac_task_check_set_host_exception_ports(current_task(), exception_mask
) != 0)
751 return KERN_NO_ACCESS
;
752 #endif /* CONFIG_MACF */
754 host_lock(host_priv
);
756 assert(EXC_TYPES_COUNT
> FIRST_EXCEPTION
);
757 for (count
=0, i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
&& count
< *CountCnt
; i
++) {
759 if (host_priv
->exc_actions
[i
].label
== NULL
) {
760 // Lazy initialization (see ipc_port_init).
761 mac_exc_action_label_init(host_priv
->exc_actions
+ i
);
765 if ((exception_mask
& (1 << i
))
767 && mac_exc_action_label_update(current_task(), host_priv
->exc_actions
+ i
) == 0
770 for (j
= 0; j
< count
; j
++) {
772 * search for an identical entry, if found
773 * set corresponding mask for this exception.
775 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
776 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
777 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
779 masks
[j
] |= (1 << i
);
786 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
787 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
788 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
791 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
792 host_priv
->exc_actions
[i
].port
=
793 ipc_port_copy_send(new_port
);
794 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
795 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
797 old_port
[i
] = IP_NULL
;
799 host_unlock(host_priv
);
802 * Consume send rights without any lock held.
804 while (--i
>= FIRST_EXCEPTION
) {
805 if (IP_VALID(old_port
[i
]))
806 ipc_port_release_send(old_port
[i
]);
809 if (IP_VALID(new_port
)) /* consume send right */
810 ipc_port_release_send(new_port
);