2 * Copyright (c) 2000-2007 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/lock.h>
71 #include <kern/task.h>
72 #include <kern/thread.h>
73 #include <kern/ipc_host.h>
74 #include <kern/ipc_kobject.h>
75 #include <kern/misc_protos.h>
77 #include <ipc/ipc_port.h>
78 #include <ipc/ipc_space.h>
81 * Forward declarations
85 ipc_processor_terminate(
86 processor_t processor
);
89 ipc_processor_disable(
90 processor_t processor
);
94 ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
);
97 * ipc_host_init: set up various things.
100 void ipc_host_init(void)
105 mutex_init(&realhost
.lock
, 0);
108 * Allocate and set up the two host ports.
110 port
= ipc_port_alloc_kernel();
112 panic("ipc_host_init");
114 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_SECURITY
);
115 kernel_set_special_port(&realhost
, HOST_SECURITY_PORT
,
116 ipc_port_make_send(port
));
118 port
= ipc_port_alloc_kernel();
120 panic("ipc_host_init");
122 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST
);
123 kernel_set_special_port(&realhost
, HOST_PORT
,
124 ipc_port_make_send(port
));
126 port
= ipc_port_alloc_kernel();
128 panic("ipc_host_init");
130 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_PRIV
);
131 kernel_set_special_port(&realhost
, HOST_PRIV_PORT
,
132 ipc_port_make_send(port
));
134 /* the rest of the special ports will be set up later */
136 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
137 realhost
.exc_actions
[i
].port
= IP_NULL
;
141 * Set up ipc for default processor set.
143 ipc_pset_init(&pset0
);
144 ipc_pset_enable(&pset0
);
147 * And for master processor
149 ipc_processor_init(master_processor
);
150 ipc_processor_enable(master_processor
);
154 * Routine: host_self_trap [mach trap]
156 * Give the caller send rights for his own host port.
160 * MACH_PORT_NULL if there are any resource failures
166 __unused
struct host_self_trap_args
*args
)
169 mach_port_name_t name
;
171 sright
= ipc_port_copy_send(current_task()->itk_host
);
172 name
= ipc_port_copyout_send(sright
, current_space());
177 * ipc_processor_init:
179 * Initialize ipc access to processor by allocating port.
184 processor_t processor
)
188 port
= ipc_port_alloc_kernel();
190 panic("ipc_processor_init");
191 processor
->processor_self
= port
;
195 * ipc_processor_enable:
197 * Enable ipc control of processor by setting port object.
200 ipc_processor_enable(
201 processor_t processor
)
205 myport
= processor
->processor_self
;
206 ipc_kobject_set(myport
, (ipc_kobject_t
) processor
, IKOT_PROCESSOR
);
210 * ipc_processor_disable:
212 * Disable ipc control of processor by clearing port object.
215 ipc_processor_disable(
216 processor_t processor
)
220 myport
= processor
->processor_self
;
221 if (myport
== IP_NULL
)
223 ipc_kobject_set(myport
, IKO_NULL
, IKOT_NONE
);
227 * ipc_processor_terminate:
229 * Processor is off-line. Destroy ipc control port.
232 ipc_processor_terminate(
233 processor_t processor
)
239 processor_lock(processor
);
240 myport
= processor
->processor_self
;
241 if (myport
== IP_NULL
) {
242 processor_unlock(processor
);
247 processor
->processor_self
= IP_NULL
;
248 processor_unlock(processor
);
251 ipc_port_dealloc_kernel(myport
);
257 * Initialize ipc control of a processor set by allocating its ports.
262 processor_set_t pset
)
266 port
= ipc_port_alloc_kernel();
268 panic("ipc_pset_init");
269 pset
->pset_self
= port
;
271 port
= ipc_port_alloc_kernel();
273 panic("ipc_pset_init");
274 pset
->pset_name_self
= port
;
280 * Enable ipc access to a processor set.
284 processor_set_t pset
)
286 ipc_kobject_set(pset
->pset_self
, (ipc_kobject_t
) pset
, IKOT_PSET
);
287 ipc_kobject_set(pset
->pset_name_self
, (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
291 * processor_set_default:
293 * Return ports for manipulating default_processor set.
296 processor_set_default(
298 processor_set_t
*pset
)
300 if (host
== HOST_NULL
)
301 return(KERN_INVALID_ARGUMENT
);
305 return (KERN_SUCCESS
);
309 * Routine: convert_port_to_host
311 * Convert from a port to a host.
312 * Doesn't consume the port ref; the host produced may be null.
318 convert_port_to_host(
321 host_t host
= HOST_NULL
;
323 if (IP_VALID(port
)) {
325 if (ip_active(port
) &&
326 ((ip_kotype(port
) == IKOT_HOST
) ||
327 (ip_kotype(port
) == IKOT_HOST_PRIV
)
329 host
= (host_t
) port
->ip_kobject
;
337 * Routine: convert_port_to_host_priv
339 * Convert from a port to a host.
340 * Doesn't consume the port ref; the host produced may be null.
346 convert_port_to_host_priv(
349 host_t host
= HOST_NULL
;
351 if (IP_VALID(port
)) {
353 if (ip_active(port
) &&
354 (ip_kotype(port
) == IKOT_HOST_PRIV
))
355 host
= (host_t
) port
->ip_kobject
;
363 * Routine: convert_port_to_processor
365 * Convert from a port to a processor.
366 * Doesn't consume the port ref;
367 * the processor produced may be null.
373 convert_port_to_processor(
376 processor_t processor
= PROCESSOR_NULL
;
378 if (IP_VALID(port
)) {
380 if (ip_active(port
) &&
381 (ip_kotype(port
) == IKOT_PROCESSOR
))
382 processor
= (processor_t
) port
->ip_kobject
;
390 * Routine: convert_port_to_pset
392 * Convert from a port to a pset.
393 * Doesn't consume the port ref; produces a pset ref,
400 convert_port_to_pset(
404 processor_set_t pset
= PROCESSOR_SET_NULL
;
407 while (!r
&& IP_VALID(port
)) {
409 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
416 * Routine: convert_port_to_pset_name
418 * Convert from a port to a pset.
419 * Doesn't consume the port ref; produces a pset ref,
426 convert_port_to_pset_name(
430 processor_set_t pset
= PROCESSOR_SET_NULL
;
433 while (!r
&& IP_VALID(port
)) {
435 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
442 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
444 processor_set_t pset
;
446 pset
= PROCESSOR_SET_NULL
;
447 if (ip_active(port
) &&
448 ((ip_kotype(port
) == IKOT_PSET
) ||
449 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
450 pset
= (processor_set_t
) port
->ip_kobject
;
460 * Routine: convert_host_to_port
462 * Convert from a host to a port.
463 * Produces a naked send right which may be invalid.
469 convert_host_to_port(
474 host_get_host_port(host
, &port
);
479 * Routine: convert_processor_to_port
481 * Convert from a processor to a port.
482 * Produces a naked send right which may be invalid.
488 convert_processor_to_port(
489 processor_t processor
)
495 processor_lock(processor
);
497 if (processor
->processor_self
!= IP_NULL
)
498 port
= ipc_port_make_send(processor
->processor_self
);
502 processor_unlock(processor
);
509 * Routine: convert_pset_to_port
511 * Convert from a pset to a port.
512 * Produces a naked send right
513 * which may be invalid.
519 convert_pset_to_port(
520 processor_set_t pset
)
522 ipc_port_t port
= pset
->pset_self
;
525 port
= ipc_port_make_send(port
);
531 * Routine: convert_pset_name_to_port
533 * Convert from a pset to a port.
534 * Produces a naked send right
535 * which may be invalid.
541 convert_pset_name_to_port(
542 processor_set_name_t pset
)
544 ipc_port_t port
= pset
->pset_name_self
;
547 port
= ipc_port_make_send(port
);
553 * Routine: convert_port_to_host_security
555 * Convert from a port to a host security.
556 * Doesn't consume the port ref; the port produced may be null.
562 convert_port_to_host_security(
565 host_t host
= HOST_NULL
;
567 if (IP_VALID(port
)) {
569 if (ip_active(port
) &&
570 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
571 host
= (host_t
) port
->ip_kobject
;
579 * Routine: host_set_exception_ports [kernel call]
581 * Sets the host exception port, flavor and
582 * behavior for the exception types specified by the mask.
583 * There will be one send right per exception per valid
586 * Nothing locked. If successful, consumes
587 * the supplied send right.
589 * KERN_SUCCESS Changed the special port.
590 * KERN_INVALID_ARGUMENT The host_priv is not valid,
591 * Illegal mask bit set.
592 * Illegal exception behavior
595 host_set_exception_ports(
596 host_priv_t host_priv
,
597 exception_mask_t exception_mask
,
599 exception_behavior_t new_behavior
,
600 thread_state_flavor_t new_flavor
)
603 ipc_port_t old_port
[EXC_TYPES_COUNT
];
605 if (host_priv
== HOST_PRIV_NULL
) {
606 return KERN_INVALID_ARGUMENT
;
609 assert(host_priv
== &realhost
);
611 if (exception_mask
& ~EXC_MASK_ALL
) {
612 return KERN_INVALID_ARGUMENT
;
615 if (IP_VALID(new_port
)) {
616 switch (new_behavior
& ~MACH_EXCEPTION_CODES
) {
617 case EXCEPTION_DEFAULT
:
618 case EXCEPTION_STATE
:
619 case EXCEPTION_STATE_IDENTITY
:
622 return KERN_INVALID_ARGUMENT
;
625 /* Cannot easily check "new_flavor", but that just means that
626 * the flavor in the generated exception message might be garbage:
629 host_lock(host_priv
);
631 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
632 if (exception_mask
& (1 << i
)) {
633 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
634 host_priv
->exc_actions
[i
].port
=
635 ipc_port_copy_send(new_port
);
636 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
637 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
639 old_port
[i
] = IP_NULL
;
643 * Consume send rights without any lock held.
645 host_unlock(host_priv
);
646 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
647 if (IP_VALID(old_port
[i
]))
648 ipc_port_release_send(old_port
[i
]);
649 if (IP_VALID(new_port
)) /* consume send right */
650 ipc_port_release_send(new_port
);
656 * Routine: host_get_exception_ports [kernel call]
658 * Clones a send right for each of the host's exception
659 * ports specified in the mask and returns the behaviour
660 * and flavor of said port.
662 * Returns upto [in} CountCnt elements.
667 * KERN_SUCCESS Extracted a send right.
668 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
669 * Invalid special port,
670 * Illegal mask bit set.
671 * KERN_FAILURE The thread is dead.
674 host_get_exception_ports(
675 host_priv_t host_priv
,
676 exception_mask_t exception_mask
,
677 exception_mask_array_t masks
,
678 mach_msg_type_number_t
* CountCnt
,
679 exception_port_array_t ports
,
680 exception_behavior_array_t behaviors
,
681 thread_state_flavor_array_t flavors
)
683 unsigned int i
, j
, count
;
685 if (host_priv
== HOST_PRIV_NULL
)
686 return KERN_INVALID_ARGUMENT
;
688 if (exception_mask
& ~EXC_MASK_ALL
) {
689 return KERN_INVALID_ARGUMENT
;
692 assert (host_priv
== &realhost
);
694 host_lock(host_priv
);
698 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
699 if (exception_mask
& (1 << i
)) {
700 for (j
= 0; j
< count
; j
++) {
702 * search for an identical entry, if found
703 * set corresponding mask for this exception.
705 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
706 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
707 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
709 masks
[j
] |= (1 << i
);
716 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
717 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
718 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
720 if (count
> *CountCnt
) {
726 host_unlock(host_priv
);
733 host_swap_exception_ports(
734 host_priv_t host_priv
,
735 exception_mask_t exception_mask
,
737 exception_behavior_t new_behavior
,
738 thread_state_flavor_t new_flavor
,
739 exception_mask_array_t masks
,
740 mach_msg_type_number_t
* CountCnt
,
741 exception_port_array_t ports
,
742 exception_behavior_array_t behaviors
,
743 thread_state_flavor_array_t flavors
)
748 ipc_port_t old_port
[EXC_TYPES_COUNT
];
750 if (host_priv
== HOST_PRIV_NULL
)
751 return KERN_INVALID_ARGUMENT
;
753 if (exception_mask
& ~EXC_MASK_ALL
) {
754 return KERN_INVALID_ARGUMENT
;
757 if (IP_VALID(new_port
)) {
758 switch (new_behavior
) {
759 case EXCEPTION_DEFAULT
:
760 case EXCEPTION_STATE
:
761 case EXCEPTION_STATE_IDENTITY
:
764 return KERN_INVALID_ARGUMENT
;
767 /* Cannot easily check "new_flavor", but that just means that
768 * the flavor in the generated exception message might be garbage:
771 host_lock(host_priv
);
775 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
776 if (exception_mask
& (1 << i
)) {
777 for (j
= 0; j
< count
; j
++) {
779 * search for an identical entry, if found
780 * set corresponding mask for this exception.
782 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
783 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
784 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
786 masks
[j
] |= (1 << i
);
793 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
794 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
795 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
798 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
799 host_priv
->exc_actions
[i
].port
=
800 ipc_port_copy_send(new_port
);
801 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
802 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
803 if (count
> *CountCnt
) {
807 old_port
[i
] = IP_NULL
;
809 host_unlock(host_priv
);
812 * Consume send rights without any lock held.
814 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
815 if (IP_VALID(old_port
[i
]))
816 ipc_port_release_send(old_port
[i
]);
817 if (IP_VALID(new_port
)) /* consume send right */
818 ipc_port_release_send(new_port
);