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 * Forward declarations
85 ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
);
88 * ipc_host_init: set up various things.
91 extern lck_grp_t host_notify_lock_grp
;
92 extern lck_attr_t host_notify_lock_attr
;
94 void ipc_host_init(void)
99 lck_mtx_init(&realhost
.lock
, &host_notify_lock_grp
, &host_notify_lock_attr
);
102 * Allocate and set up the two host ports.
104 port
= ipc_port_alloc_kernel();
106 panic("ipc_host_init");
108 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_SECURITY
);
109 kernel_set_special_port(&realhost
, HOST_SECURITY_PORT
,
110 ipc_port_make_send(port
));
112 port
= ipc_port_alloc_kernel();
114 panic("ipc_host_init");
116 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST
);
117 kernel_set_special_port(&realhost
, HOST_PORT
,
118 ipc_port_make_send(port
));
120 port
= ipc_port_alloc_kernel();
122 panic("ipc_host_init");
124 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_PRIV
);
125 kernel_set_special_port(&realhost
, HOST_PRIV_PORT
,
126 ipc_port_make_send(port
));
128 /* the rest of the special ports will be set up later */
130 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
131 realhost
.exc_actions
[i
].port
= IP_NULL
;
135 * Set up ipc for default processor set.
137 ipc_pset_init(&pset0
);
138 ipc_pset_enable(&pset0
);
141 * And for master processor
143 ipc_processor_init(master_processor
);
144 ipc_processor_enable(master_processor
);
148 * Routine: host_self_trap [mach trap]
150 * Give the caller send rights for his own host port.
154 * MACH_PORT_NULL if there are any resource failures
160 __unused
struct host_self_trap_args
*args
)
163 mach_port_name_t name
;
165 sright
= ipc_port_copy_send(current_task()->itk_host
);
166 name
= ipc_port_copyout_send(sright
, current_space());
171 * ipc_processor_init:
173 * Initialize ipc access to processor by allocating port.
178 processor_t processor
)
182 port
= ipc_port_alloc_kernel();
184 panic("ipc_processor_init");
185 processor
->processor_self
= port
;
189 * ipc_processor_enable:
191 * Enable ipc control of processor by setting port object.
194 ipc_processor_enable(
195 processor_t processor
)
199 myport
= processor
->processor_self
;
200 ipc_kobject_set(myport
, (ipc_kobject_t
) processor
, IKOT_PROCESSOR
);
206 * Initialize ipc control of a processor set by allocating its ports.
211 processor_set_t pset
)
215 port
= ipc_port_alloc_kernel();
217 panic("ipc_pset_init");
218 pset
->pset_self
= port
;
220 port
= ipc_port_alloc_kernel();
222 panic("ipc_pset_init");
223 pset
->pset_name_self
= port
;
229 * Enable ipc access to a processor set.
233 processor_set_t pset
)
235 ipc_kobject_set(pset
->pset_self
, (ipc_kobject_t
) pset
, IKOT_PSET
);
236 ipc_kobject_set(pset
->pset_name_self
, (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
240 * processor_set_default:
242 * Return ports for manipulating default_processor set.
245 processor_set_default(
247 processor_set_t
*pset
)
249 if (host
== HOST_NULL
)
250 return(KERN_INVALID_ARGUMENT
);
254 return (KERN_SUCCESS
);
258 * Routine: convert_port_to_host
260 * Convert from a port to a host.
261 * Doesn't consume the port ref; the host produced may be null.
267 convert_port_to_host(
270 host_t host
= HOST_NULL
;
272 if (IP_VALID(port
)) {
274 if (ip_active(port
) &&
275 ((ip_kotype(port
) == IKOT_HOST
) ||
276 (ip_kotype(port
) == IKOT_HOST_PRIV
)
278 host
= (host_t
) port
->ip_kobject
;
286 * Routine: convert_port_to_host_priv
288 * Convert from a port to a host.
289 * Doesn't consume the port ref; the host produced may be null.
295 convert_port_to_host_priv(
298 host_t host
= HOST_NULL
;
300 if (IP_VALID(port
)) {
302 if (ip_active(port
) &&
303 (ip_kotype(port
) == IKOT_HOST_PRIV
))
304 host
= (host_t
) port
->ip_kobject
;
312 * Routine: convert_port_to_processor
314 * Convert from a port to a processor.
315 * Doesn't consume the port ref;
316 * the processor produced may be null.
322 convert_port_to_processor(
325 processor_t processor
= PROCESSOR_NULL
;
327 if (IP_VALID(port
)) {
329 if (ip_active(port
) &&
330 (ip_kotype(port
) == IKOT_PROCESSOR
))
331 processor
= (processor_t
) port
->ip_kobject
;
339 * Routine: convert_port_to_pset
341 * Convert from a port to a pset.
342 * Doesn't consume the port ref; produces a pset ref,
349 convert_port_to_pset(
353 processor_set_t pset
= PROCESSOR_SET_NULL
;
356 while (!r
&& IP_VALID(port
)) {
358 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
365 * Routine: convert_port_to_pset_name
367 * Convert from a port to a pset.
368 * Doesn't consume the port ref; produces a pset ref,
375 convert_port_to_pset_name(
379 processor_set_t pset
= PROCESSOR_SET_NULL
;
382 while (!r
&& IP_VALID(port
)) {
384 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
391 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
393 processor_set_t pset
;
395 pset
= PROCESSOR_SET_NULL
;
396 if (ip_active(port
) &&
397 ((ip_kotype(port
) == IKOT_PSET
) ||
398 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
399 pset
= (processor_set_t
) port
->ip_kobject
;
409 * Routine: convert_host_to_port
411 * Convert from a host to a port.
412 * Produces a naked send right which may be invalid.
418 convert_host_to_port(
423 host_get_host_port(host
, &port
);
428 * Routine: convert_processor_to_port
430 * Convert from a processor to a port.
431 * Produces a naked send right which may be invalid.
432 * Processors are not reference counted, so nothing to release.
438 convert_processor_to_port(
439 processor_t processor
)
441 ipc_port_t port
= processor
->processor_self
;
444 port
= ipc_port_make_send(port
);
449 * Routine: convert_pset_to_port
451 * Convert from a pset to a port.
452 * Produces a naked send right which may be invalid.
453 * Processor sets are not reference counted, so nothing to release.
459 convert_pset_to_port(
460 processor_set_t pset
)
462 ipc_port_t port
= pset
->pset_self
;
465 port
= ipc_port_make_send(port
);
471 * Routine: convert_pset_name_to_port
473 * Convert from a pset to a port.
474 * Produces a naked send right which may be invalid.
475 * Processor sets are not reference counted, so nothing to release.
481 convert_pset_name_to_port(
482 processor_set_name_t pset
)
484 ipc_port_t port
= pset
->pset_name_self
;
487 port
= ipc_port_make_send(port
);
493 * Routine: convert_port_to_host_security
495 * Convert from a port to a host security.
496 * Doesn't consume the port ref; the port produced may be null.
502 convert_port_to_host_security(
505 host_t host
= HOST_NULL
;
507 if (IP_VALID(port
)) {
509 if (ip_active(port
) &&
510 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
511 host
= (host_t
) port
->ip_kobject
;
519 * Routine: host_set_exception_ports [kernel call]
521 * Sets the host exception port, flavor and
522 * behavior for the exception types specified by the mask.
523 * There will be one send right per exception per valid
526 * Nothing locked. If successful, consumes
527 * the supplied send right.
529 * KERN_SUCCESS Changed the special port.
530 * KERN_INVALID_ARGUMENT The host_priv is not valid,
531 * Illegal mask bit set.
532 * Illegal exception behavior
535 host_set_exception_ports(
536 host_priv_t host_priv
,
537 exception_mask_t exception_mask
,
539 exception_behavior_t new_behavior
,
540 thread_state_flavor_t new_flavor
)
543 ipc_port_t old_port
[EXC_TYPES_COUNT
];
545 if (host_priv
== HOST_PRIV_NULL
) {
546 return KERN_INVALID_ARGUMENT
;
549 assert(host_priv
== &realhost
);
551 if (exception_mask
& ~EXC_MASK_VALID
) {
552 return KERN_INVALID_ARGUMENT
;
555 if (IP_VALID(new_port
)) {
556 switch (new_behavior
& ~MACH_EXCEPTION_CODES
) {
557 case EXCEPTION_DEFAULT
:
558 case EXCEPTION_STATE
:
559 case EXCEPTION_STATE_IDENTITY
:
562 return KERN_INVALID_ARGUMENT
;
567 * Check the validity of the thread_state_flavor by calling the
568 * VALID_THREAD_STATE_FLAVOR architecture dependent macro defined in
569 * osfmk/mach/ARCHITECTURE/thread_status.h
571 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
))
572 return (KERN_INVALID_ARGUMENT
);
574 host_lock(host_priv
);
576 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
577 if (exception_mask
& (1 << i
)) {
578 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
579 host_priv
->exc_actions
[i
].port
=
580 ipc_port_copy_send(new_port
);
581 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
582 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
584 old_port
[i
] = IP_NULL
;
588 * Consume send rights without any lock held.
590 host_unlock(host_priv
);
591 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
592 if (IP_VALID(old_port
[i
]))
593 ipc_port_release_send(old_port
[i
]);
594 if (IP_VALID(new_port
)) /* consume send right */
595 ipc_port_release_send(new_port
);
601 * Routine: host_get_exception_ports [kernel call]
603 * Clones a send right for each of the host's exception
604 * ports specified in the mask and returns the behaviour
605 * and flavor of said port.
607 * Returns upto [in} CountCnt elements.
612 * KERN_SUCCESS Extracted a send right.
613 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
614 * Invalid special port,
615 * Illegal mask bit set.
616 * KERN_FAILURE The thread is dead.
619 host_get_exception_ports(
620 host_priv_t host_priv
,
621 exception_mask_t exception_mask
,
622 exception_mask_array_t masks
,
623 mach_msg_type_number_t
* CountCnt
,
624 exception_port_array_t ports
,
625 exception_behavior_array_t behaviors
,
626 thread_state_flavor_array_t flavors
)
628 unsigned int i
, j
, count
;
630 if (host_priv
== HOST_PRIV_NULL
)
631 return KERN_INVALID_ARGUMENT
;
633 if (exception_mask
& ~EXC_MASK_VALID
) {
634 return KERN_INVALID_ARGUMENT
;
637 assert (host_priv
== &realhost
);
639 host_lock(host_priv
);
643 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
644 if (exception_mask
& (1 << i
)) {
645 for (j
= 0; j
< count
; j
++) {
647 * search for an identical entry, if found
648 * set corresponding mask for this exception.
650 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
651 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
652 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
654 masks
[j
] |= (1 << i
);
661 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
662 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
663 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
665 if (count
> *CountCnt
) {
671 host_unlock(host_priv
);
678 host_swap_exception_ports(
679 host_priv_t host_priv
,
680 exception_mask_t exception_mask
,
682 exception_behavior_t new_behavior
,
683 thread_state_flavor_t new_flavor
,
684 exception_mask_array_t masks
,
685 mach_msg_type_number_t
* CountCnt
,
686 exception_port_array_t ports
,
687 exception_behavior_array_t behaviors
,
688 thread_state_flavor_array_t flavors
)
693 ipc_port_t old_port
[EXC_TYPES_COUNT
];
695 if (host_priv
== HOST_PRIV_NULL
)
696 return KERN_INVALID_ARGUMENT
;
698 if (exception_mask
& ~EXC_MASK_VALID
) {
699 return KERN_INVALID_ARGUMENT
;
702 if (IP_VALID(new_port
)) {
703 switch (new_behavior
) {
704 case EXCEPTION_DEFAULT
:
705 case EXCEPTION_STATE
:
706 case EXCEPTION_STATE_IDENTITY
:
709 return KERN_INVALID_ARGUMENT
;
713 if (new_flavor
!= 0 && !VALID_THREAD_STATE_FLAVOR(new_flavor
))
714 return (KERN_INVALID_ARGUMENT
);
716 host_lock(host_priv
);
718 assert(EXC_TYPES_COUNT
> FIRST_EXCEPTION
);
719 for (count
=0, i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
&& count
< *CountCnt
; i
++) {
720 if (exception_mask
& (1 << i
)) {
721 for (j
= 0; j
< count
; j
++) {
723 * search for an identical entry, if found
724 * set corresponding mask for this exception.
726 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
727 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
728 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
730 masks
[j
] |= (1 << i
);
737 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
738 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
739 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
742 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
743 host_priv
->exc_actions
[i
].port
=
744 ipc_port_copy_send(new_port
);
745 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
746 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
748 old_port
[i
] = IP_NULL
;
750 host_unlock(host_priv
);
753 * Consume send rights without any lock held.
755 while (--i
>= FIRST_EXCEPTION
) {
756 if (IP_VALID(old_port
[i
]))
757 ipc_port_release_send(old_port
[i
]);
760 if (IP_VALID(new_port
)) /* consume send right */
761 ipc_port_release_send(new_port
);