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
;
139 * Set up ipc for default processor set.
141 ipc_pset_init(&pset0
);
142 ipc_pset_enable(&pset0
);
145 * And for master processor
147 ipc_processor_init(master_processor
);
148 ipc_processor_enable(master_processor
);
152 * Routine: host_self_trap [mach trap]
154 * Give the caller send rights for his own host port.
158 * MACH_PORT_NULL if there are any resource failures
164 __unused
struct host_self_trap_args
*args
)
167 mach_port_name_t name
;
169 sright
= ipc_port_copy_send(current_task()->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();
188 panic("ipc_processor_init");
189 processor
->processor_self
= port
;
193 * ipc_processor_enable:
195 * Enable ipc control of processor by setting port object.
198 ipc_processor_enable(
199 processor_t processor
)
203 myport
= processor
->processor_self
;
204 ipc_kobject_set(myport
, (ipc_kobject_t
) processor
, IKOT_PROCESSOR
);
210 * Initialize ipc control of a processor set by allocating its ports.
215 processor_set_t pset
)
219 port
= ipc_port_alloc_kernel();
221 panic("ipc_pset_init");
222 pset
->pset_self
= port
;
224 port
= ipc_port_alloc_kernel();
226 panic("ipc_pset_init");
227 pset
->pset_name_self
= port
;
233 * Enable ipc access to a processor set.
237 processor_set_t pset
)
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
);
244 * processor_set_default:
246 * Return ports for manipulating default_processor set.
249 processor_set_default(
251 processor_set_t
*pset
)
253 if (host
== HOST_NULL
)
254 return(KERN_INVALID_ARGUMENT
);
258 return (KERN_SUCCESS
);
262 * Routine: convert_port_to_host
264 * Convert from a port to a host.
265 * Doesn't consume the port ref; the host produced may be null.
271 convert_port_to_host(
274 host_t host
= HOST_NULL
;
276 if (IP_VALID(port
)) {
278 if (ip_active(port
) &&
279 ((ip_kotype(port
) == IKOT_HOST
) ||
280 (ip_kotype(port
) == IKOT_HOST_PRIV
)
282 host
= (host_t
) port
->ip_kobject
;
290 * Routine: convert_port_to_host_priv
292 * Convert from a port to a host.
293 * Doesn't consume the port ref; the host produced may be null.
299 convert_port_to_host_priv(
302 host_t host
= HOST_NULL
;
304 if (IP_VALID(port
)) {
306 if (ip_active(port
) &&
307 (ip_kotype(port
) == IKOT_HOST_PRIV
))
308 host
= (host_t
) port
->ip_kobject
;
316 * Routine: convert_port_to_processor
318 * Convert from a port to a processor.
319 * Doesn't consume the port ref;
320 * the processor produced may be null.
326 convert_port_to_processor(
329 processor_t processor
= PROCESSOR_NULL
;
331 if (IP_VALID(port
)) {
333 if (ip_active(port
) &&
334 (ip_kotype(port
) == IKOT_PROCESSOR
))
335 processor
= (processor_t
) port
->ip_kobject
;
343 * Routine: convert_port_to_pset
345 * Convert from a port to a pset.
346 * Doesn't consume the port ref; produces a pset ref,
353 convert_port_to_pset(
357 processor_set_t pset
= PROCESSOR_SET_NULL
;
360 while (!r
&& IP_VALID(port
)) {
362 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
369 * Routine: convert_port_to_pset_name
371 * Convert from a port to a pset.
372 * Doesn't consume the port ref; produces a pset ref,
379 convert_port_to_pset_name(
383 processor_set_t pset
= PROCESSOR_SET_NULL
;
386 while (!r
&& IP_VALID(port
)) {
388 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
395 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
397 processor_set_t pset
;
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
;
413 * Routine: convert_host_to_port
415 * Convert from a host to a port.
416 * Produces a naked send right which may be invalid.
422 convert_host_to_port(
427 host_get_host_port(host
, &port
);
432 * Routine: convert_processor_to_port
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.
442 convert_processor_to_port(
443 processor_t processor
)
445 ipc_port_t port
= processor
->processor_self
;
448 port
= ipc_port_make_send(port
);
453 * Routine: convert_pset_to_port
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.
463 convert_pset_to_port(
464 processor_set_t pset
)
466 ipc_port_t port
= pset
->pset_self
;
469 port
= ipc_port_make_send(port
);
475 * Routine: convert_pset_name_to_port
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.
485 convert_pset_name_to_port(
486 processor_set_name_t pset
)
488 ipc_port_t port
= pset
->pset_name_self
;
491 port
= ipc_port_make_send(port
);
497 * Routine: convert_port_to_host_security
499 * Convert from a port to a host security.
500 * Doesn't consume the port ref; the port produced may be null.
506 convert_port_to_host_security(
509 host_t host
= HOST_NULL
;
511 if (IP_VALID(port
)) {
513 if (ip_active(port
) &&
514 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
515 host
= (host_t
) port
->ip_kobject
;
523 * Routine: host_set_exception_ports [kernel call]
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
530 * Nothing locked. If successful, consumes
531 * the supplied send right.
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
539 host_set_exception_ports(
540 host_priv_t host_priv
,
541 exception_mask_t exception_mask
,
543 exception_behavior_t new_behavior
,
544 thread_state_flavor_t new_flavor
)
547 ipc_port_t old_port
[EXC_TYPES_COUNT
];
549 if (host_priv
== HOST_PRIV_NULL
) {
550 return KERN_INVALID_ARGUMENT
;
553 if (exception_mask
& ~EXC_MASK_VALID
) {
554 return KERN_INVALID_ARGUMENT
;
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
:
564 return KERN_INVALID_ARGUMENT
;
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
573 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
))
574 return (KERN_INVALID_ARGUMENT
);
577 if (mac_task_check_set_host_exception_ports(current_task(), exception_mask
) != 0)
578 return KERN_NO_ACCESS
;
581 assert(host_priv
== &realhost
);
583 host_lock(host_priv
);
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
;
593 old_port
[i
] = IP_NULL
;
597 * Consume send rights without any lock held.
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
);
610 * Routine: host_get_exception_ports [kernel call]
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.
616 * Returns upto [in} CountCnt elements.
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.
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
)
637 unsigned int i
, j
, count
;
639 if (host_priv
== HOST_PRIV_NULL
)
640 return KERN_INVALID_ARGUMENT
;
642 if (exception_mask
& ~EXC_MASK_VALID
) {
643 return KERN_INVALID_ARGUMENT
;
646 assert (host_priv
== &realhost
);
648 host_lock(host_priv
);
652 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
653 if (exception_mask
& (1 << i
)) {
654 for (j
= 0; j
< count
; j
++) {
656 * search for an identical entry, if found
657 * set corresponding mask for this exception.
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
])
663 masks
[j
] |= (1 << i
);
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
;
674 if (count
> *CountCnt
) {
680 host_unlock(host_priv
);
687 host_swap_exception_ports(
688 host_priv_t host_priv
,
689 exception_mask_t exception_mask
,
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
)
702 ipc_port_t old_port
[EXC_TYPES_COUNT
];
704 if (host_priv
== HOST_PRIV_NULL
)
705 return KERN_INVALID_ARGUMENT
;
707 if (exception_mask
& ~EXC_MASK_VALID
) {
708 return KERN_INVALID_ARGUMENT
;
711 if (IP_VALID(new_port
)) {
712 switch (new_behavior
) {
713 case EXCEPTION_DEFAULT
:
714 case EXCEPTION_STATE
:
715 case EXCEPTION_STATE_IDENTITY
:
718 return KERN_INVALID_ARGUMENT
;
722 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
))
723 return (KERN_INVALID_ARGUMENT
);
726 if (mac_task_check_set_host_exception_ports(current_task(), exception_mask
) != 0)
727 return KERN_NO_ACCESS
;
728 #endif /* CONFIG_MACF */
730 host_lock(host_priv
);
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
++) {
737 * search for an identical entry, if found
738 * set corresponding mask for this exception.
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
])
744 masks
[j
] |= (1 << i
);
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
;
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
;
762 old_port
[i
] = IP_NULL
;
764 host_unlock(host_priv
);
767 * Consume send rights without any lock held.
769 while (--i
>= FIRST_EXCEPTION
) {
770 if (IP_VALID(old_port
[i
]))
771 ipc_port_release_send(old_port
[i
]);
774 if (IP_VALID(new_port
)) /* consume send right */
775 ipc_port_release_send(new_port
);