2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
64 * Routines to implement host ports.
66 #include <mach/message.h>
67 #include <mach/mach_traps.h>
68 #include <mach/mach_host_server.h>
69 #include <mach/host_priv_server.h>
70 #include <kern/host.h>
71 #include <kern/processor.h>
72 #include <kern/lock.h>
73 #include <kern/task.h>
74 #include <kern/thread.h>
75 #include <kern/ipc_host.h>
76 #include <kern/ipc_kobject.h>
77 #include <kern/misc_protos.h>
79 #include <ipc/ipc_port.h>
80 #include <ipc/ipc_space.h>
83 * Forward declarations
87 ipc_processor_terminate(
88 processor_t processor
);
91 ipc_processor_disable(
92 processor_t processor
);
96 ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
);
99 * ipc_host_init: set up various things.
102 void ipc_host_init(void)
107 mutex_init(&realhost
.lock
, 0);
110 * Allocate and set up the two host ports.
112 port
= ipc_port_alloc_kernel();
114 panic("ipc_host_init");
116 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_SECURITY
);
117 kernel_set_special_port(&realhost
, HOST_SECURITY_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
);
125 kernel_set_special_port(&realhost
, HOST_PORT
,
126 ipc_port_make_send(port
));
128 port
= ipc_port_alloc_kernel();
130 panic("ipc_host_init");
132 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_PRIV
);
133 kernel_set_special_port(&realhost
, HOST_PRIV_PORT
,
134 ipc_port_make_send(port
));
136 /* the rest of the special ports will be set up later */
138 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
139 realhost
.exc_actions
[i
].port
= IP_NULL
;
143 * Set up ipc for default processor set.
145 ipc_pset_init(&default_pset
);
146 ipc_pset_enable(&default_pset
);
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
)
171 mach_port_name_t name
;
173 sright
= ipc_port_copy_send(current_task()->itk_host
);
174 name
= ipc_port_copyout_send(sright
, current_space());
179 * ipc_processor_init:
181 * Initialize ipc access to processor by allocating port.
186 processor_t processor
)
190 port
= ipc_port_alloc_kernel();
192 panic("ipc_processor_init");
193 processor
->processor_self
= port
;
197 * ipc_processor_enable:
199 * Enable ipc control of processor by setting port object.
202 ipc_processor_enable(
203 processor_t processor
)
207 myport
= processor
->processor_self
;
208 ipc_kobject_set(myport
, (ipc_kobject_t
) processor
, IKOT_PROCESSOR
);
212 * ipc_processor_disable:
214 * Disable ipc control of processor by clearing port object.
217 ipc_processor_disable(
218 processor_t processor
)
222 myport
= processor
->processor_self
;
223 if (myport
== IP_NULL
)
225 ipc_kobject_set(myport
, IKO_NULL
, IKOT_NONE
);
229 * ipc_processor_terminate:
231 * Processor is off-line. Destroy ipc control port.
234 ipc_processor_terminate(
235 processor_t processor
)
241 processor_lock(processor
);
242 myport
= processor
->processor_self
;
243 if (myport
== IP_NULL
) {
244 processor_unlock(processor
);
249 processor
->processor_self
= IP_NULL
;
250 processor_unlock(processor
);
253 ipc_port_dealloc_kernel(myport
);
259 * Initialize ipc control of a processor set by allocating its ports.
264 processor_set_t pset
)
268 port
= ipc_port_alloc_kernel();
270 panic("ipc_pset_init");
271 pset
->pset_self
= port
;
273 port
= ipc_port_alloc_kernel();
275 panic("ipc_pset_init");
276 pset
->pset_name_self
= port
;
282 * Enable ipc access to a processor set.
286 processor_set_t pset
)
290 ipc_kobject_set(pset
->pset_self
,
291 (ipc_kobject_t
) pset
, IKOT_PSET
);
292 ipc_kobject_set(pset
->pset_name_self
,
293 (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
294 pset
->ref_count
+= 2;
302 * Disable ipc access to a processor set by clearing the port objects.
303 * Caller must hold pset lock and a reference to the pset. Ok to
304 * just decrement pset reference count as a result.
308 processor_set_t pset
)
310 ipc_kobject_set(pset
->pset_self
, IKO_NULL
, IKOT_NONE
);
311 ipc_kobject_set(pset
->pset_name_self
, IKO_NULL
, IKOT_NONE
);
312 pset
->ref_count
-= 2;
316 * ipc_pset_terminate:
318 * Processor set is dead. Deallocate the ipc control structures.
322 processor_set_t pset
)
324 ipc_port_dealloc_kernel(pset
->pset_self
);
325 ipc_port_dealloc_kernel(pset
->pset_name_self
);
329 * processor_set_default, processor_set_default_priv:
331 * Return ports for manipulating default_processor set. MiG code
332 * differentiates between these two routines.
335 processor_set_default(
337 processor_set_t
*pset
)
339 if (host
== HOST_NULL
)
340 return(KERN_INVALID_ARGUMENT
);
342 *pset
= &default_pset
;
343 pset_reference(*pset
);
344 return(KERN_SUCCESS
);
348 * Routine: convert_port_to_host
350 * Convert from a port to a host.
351 * Doesn't consume the port ref; the host produced may be null.
357 convert_port_to_host(
360 host_t host
= HOST_NULL
;
362 if (IP_VALID(port
)) {
364 if (ip_active(port
) &&
365 ((ip_kotype(port
) == IKOT_HOST
) ||
366 (ip_kotype(port
) == IKOT_HOST_PRIV
)
368 host
= (host_t
) port
->ip_kobject
;
376 * Routine: convert_port_to_host_priv
378 * Convert from a port to a host.
379 * Doesn't consume the port ref; the host produced may be null.
385 convert_port_to_host_priv(
388 host_t host
= HOST_NULL
;
390 if (IP_VALID(port
)) {
392 if (ip_active(port
) &&
393 (ip_kotype(port
) == IKOT_HOST_PRIV
))
394 host
= (host_t
) port
->ip_kobject
;
402 * Routine: convert_port_to_processor
404 * Convert from a port to a processor.
405 * Doesn't consume the port ref;
406 * the processor produced may be null.
412 convert_port_to_processor(
415 processor_t processor
= PROCESSOR_NULL
;
417 if (IP_VALID(port
)) {
419 if (ip_active(port
) &&
420 (ip_kotype(port
) == IKOT_PROCESSOR
))
421 processor
= (processor_t
) port
->ip_kobject
;
429 * Routine: convert_port_to_pset
431 * Convert from a port to a pset.
432 * Doesn't consume the port ref; produces a pset ref,
439 convert_port_to_pset(
443 processor_set_t pset
= PROCESSOR_SET_NULL
;
446 while (!r
&& IP_VALID(port
)) {
448 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
455 * Routine: convert_port_to_pset_name
457 * Convert from a port to a pset.
458 * Doesn't consume the port ref; produces a pset ref,
465 convert_port_to_pset_name(
469 processor_set_t pset
= PROCESSOR_SET_NULL
;
472 while (!r
&& IP_VALID(port
)) {
474 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
481 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
483 processor_set_t pset
;
485 pset
= PROCESSOR_SET_NULL
;
486 if (ip_active(port
) &&
487 ((ip_kotype(port
) == IKOT_PSET
) ||
488 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
489 pset
= (processor_set_t
) port
->ip_kobject
;
490 if (!pset_lock_try(pset
)) {
504 * Routine: convert_host_to_port
506 * Convert from a host to a port.
507 * Produces a naked send right which may be invalid.
513 convert_host_to_port(
518 host_get_host_port(host
, &port
);
523 * Routine: convert_processor_to_port
525 * Convert from a processor to a port.
526 * Produces a naked send right which may be invalid.
532 convert_processor_to_port(
533 processor_t processor
)
539 processor_lock(processor
);
541 if (processor
->processor_self
!= IP_NULL
)
542 port
= ipc_port_make_send(processor
->processor_self
);
546 processor_unlock(processor
);
553 * Routine: convert_pset_to_port
555 * Convert from a pset to a port.
556 * Consumes a pset ref; produces a naked send right
557 * which may be invalid.
563 convert_pset_to_port(
564 processor_set_t pset
)
570 port
= ipc_port_make_send(pset
->pset_self
);
575 pset_deallocate(pset
);
580 * Routine: convert_pset_name_to_port
582 * Convert from a pset to a port.
583 * Consumes a pset ref; produces a naked send right
584 * which may be invalid.
590 convert_pset_name_to_port(
591 processor_set_name_t pset
)
597 port
= ipc_port_make_send(pset
->pset_name_self
);
602 pset_deallocate(pset
);
607 * Routine: convert_port_to_host_security
609 * Convert from a port to a host security.
610 * Doesn't consume the port ref; the port produced may be null.
616 convert_port_to_host_security(
619 host_t host
= HOST_NULL
;
621 if (IP_VALID(port
)) {
623 if (ip_active(port
) &&
624 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
625 host
= (host_t
) port
->ip_kobject
;
633 * Routine: host_set_exception_ports [kernel call]
635 * Sets the host exception port, flavor and
636 * behavior for the exception types specified by the mask.
637 * There will be one send right per exception per valid
640 * Nothing locked. If successful, consumes
641 * the supplied send right.
643 * KERN_SUCCESS Changed the special port.
644 * KERN_INVALID_ARGUMENT The host_priv is not valid,
645 * Illegal mask bit set.
646 * Illegal exception behavior
649 host_set_exception_ports(
650 host_priv_t host_priv
,
651 exception_mask_t exception_mask
,
653 exception_behavior_t new_behavior
,
654 thread_state_flavor_t new_flavor
)
657 ipc_port_t old_port
[EXC_TYPES_COUNT
];
659 if (host_priv
== HOST_PRIV_NULL
) {
660 return KERN_INVALID_ARGUMENT
;
663 assert(host_priv
== &realhost
);
665 if (exception_mask
& ~EXC_MASK_ALL
) {
666 return KERN_INVALID_ARGUMENT
;
669 if (IP_VALID(new_port
)) {
670 switch (new_behavior
) {
671 case EXCEPTION_DEFAULT
:
672 case EXCEPTION_STATE
:
673 case EXCEPTION_STATE_IDENTITY
:
676 return KERN_INVALID_ARGUMENT
;
679 /* Cannot easily check "new_flavor", but that just means that
680 * the flavor in the generated exception message might be garbage:
683 host_lock(host_priv
);
685 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
686 if (exception_mask
& (1 << i
)) {
687 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
688 host_priv
->exc_actions
[i
].port
=
689 ipc_port_copy_send(new_port
);
690 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
691 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
693 old_port
[i
] = IP_NULL
;
697 * Consume send rights without any lock held.
699 host_unlock(host_priv
);
700 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
701 if (IP_VALID(old_port
[i
]))
702 ipc_port_release_send(old_port
[i
]);
703 if (IP_VALID(new_port
)) /* consume send right */
704 ipc_port_release_send(new_port
);
710 * Routine: host_get_exception_ports [kernel call]
712 * Clones a send right for each of the host's exception
713 * ports specified in the mask and returns the behaviour
714 * and flavor of said port.
716 * Returns upto [in} CountCnt elements.
721 * KERN_SUCCESS Extracted a send right.
722 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
723 * Invalid special port,
724 * Illegal mask bit set.
725 * KERN_FAILURE The thread is dead.
728 host_get_exception_ports(
729 host_priv_t host_priv
,
730 exception_mask_t exception_mask
,
731 exception_mask_array_t masks
,
732 mach_msg_type_number_t
* CountCnt
,
733 exception_port_array_t ports
,
734 exception_behavior_array_t behaviors
,
735 thread_state_flavor_array_t flavors
)
737 unsigned int i
, j
, count
;
739 if (host_priv
== HOST_PRIV_NULL
)
740 return KERN_INVALID_ARGUMENT
;
742 if (exception_mask
& ~EXC_MASK_ALL
) {
743 return KERN_INVALID_ARGUMENT
;
746 assert (host_priv
== &realhost
);
748 host_lock(host_priv
);
752 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
753 if (exception_mask
& (1 << i
)) {
754 for (j
= 0; j
< count
; j
++) {
756 * search for an identical entry, if found
757 * set corresponding mask for this exception.
759 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
760 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
761 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
763 masks
[j
] |= (1 << i
);
770 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
771 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
772 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
774 if (count
> *CountCnt
) {
780 host_unlock(host_priv
);
787 host_swap_exception_ports(
788 host_priv_t host_priv
,
789 exception_mask_t exception_mask
,
791 exception_behavior_t new_behavior
,
792 thread_state_flavor_t new_flavor
,
793 exception_mask_array_t masks
,
794 mach_msg_type_number_t
* CountCnt
,
795 exception_port_array_t ports
,
796 exception_behavior_array_t behaviors
,
797 thread_state_flavor_array_t flavors
)
802 ipc_port_t old_port
[EXC_TYPES_COUNT
];
804 if (host_priv
== HOST_PRIV_NULL
)
805 return KERN_INVALID_ARGUMENT
;
807 if (exception_mask
& ~EXC_MASK_ALL
) {
808 return KERN_INVALID_ARGUMENT
;
811 if (IP_VALID(new_port
)) {
812 switch (new_behavior
) {
813 case EXCEPTION_DEFAULT
:
814 case EXCEPTION_STATE
:
815 case EXCEPTION_STATE_IDENTITY
:
818 return KERN_INVALID_ARGUMENT
;
821 /* Cannot easily check "new_flavor", but that just means that
822 * the flavor in the generated exception message might be garbage:
825 host_lock(host_priv
);
829 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
830 if (exception_mask
& (1 << i
)) {
831 for (j
= 0; j
< count
; j
++) {
833 * search for an identical entry, if found
834 * set corresponding mask for this exception.
836 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
837 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
838 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
840 masks
[j
] |= (1 << i
);
847 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
848 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
849 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
852 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
853 host_priv
->exc_actions
[i
].port
=
854 ipc_port_copy_send(new_port
);
855 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
856 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
857 if (count
> *CountCnt
) {
861 old_port
[i
] = IP_NULL
;
863 host_unlock(host_priv
);
866 * Consume send rights without any lock held.
868 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
869 if (IP_VALID(old_port
[i
]))
870 ipc_port_release_send(old_port
[i
]);
871 if (IP_VALID(new_port
)) /* consume send right */
872 ipc_port_release_send(new_port
);