2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
56 * Routines to implement host ports.
58 #include <mach/message.h>
59 #include <mach/mach_traps.h>
60 #include <mach/mach_host_server.h>
61 #include <mach/host_priv_server.h>
62 #include <kern/host.h>
63 #include <kern/processor.h>
64 #include <kern/lock.h>
65 #include <kern/task.h>
66 #include <kern/thread.h>
67 #include <kern/ipc_host.h>
68 #include <kern/ipc_kobject.h>
69 #include <kern/misc_protos.h>
71 #include <ipc/ipc_port.h>
72 #include <ipc/ipc_space.h>
75 * Forward declarations
79 ipc_processor_terminate(
80 processor_t processor
);
83 ipc_processor_disable(
84 processor_t processor
);
88 ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
);
91 * ipc_host_init: set up various things.
94 void ipc_host_init(void)
99 mutex_init(&realhost
.lock
, 0);
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(&default_pset
);
138 ipc_pset_enable(&default_pset
);
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
);
204 * ipc_processor_disable:
206 * Disable ipc control of processor by clearing port object.
209 ipc_processor_disable(
210 processor_t processor
)
214 myport
= processor
->processor_self
;
215 if (myport
== IP_NULL
)
217 ipc_kobject_set(myport
, IKO_NULL
, IKOT_NONE
);
221 * ipc_processor_terminate:
223 * Processor is off-line. Destroy ipc control port.
226 ipc_processor_terminate(
227 processor_t processor
)
233 processor_lock(processor
);
234 myport
= processor
->processor_self
;
235 if (myport
== IP_NULL
) {
236 processor_unlock(processor
);
241 processor
->processor_self
= IP_NULL
;
242 processor_unlock(processor
);
245 ipc_port_dealloc_kernel(myport
);
251 * Initialize ipc control of a processor set by allocating its ports.
256 processor_set_t pset
)
260 port
= ipc_port_alloc_kernel();
262 panic("ipc_pset_init");
263 pset
->pset_self
= port
;
265 port
= ipc_port_alloc_kernel();
267 panic("ipc_pset_init");
268 pset
->pset_name_self
= port
;
274 * Enable ipc access to a processor set.
278 processor_set_t pset
)
282 ipc_kobject_set(pset
->pset_self
,
283 (ipc_kobject_t
) pset
, IKOT_PSET
);
284 ipc_kobject_set(pset
->pset_name_self
,
285 (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
286 pset
->ref_count
+= 2;
294 * Disable ipc access to a processor set by clearing the port objects.
295 * Caller must hold pset lock and a reference to the pset. Ok to
296 * just decrement pset reference count as a result.
300 processor_set_t pset
)
302 ipc_kobject_set(pset
->pset_self
, IKO_NULL
, IKOT_NONE
);
303 ipc_kobject_set(pset
->pset_name_self
, IKO_NULL
, IKOT_NONE
);
304 pset
->ref_count
-= 2;
308 * ipc_pset_terminate:
310 * Processor set is dead. Deallocate the ipc control structures.
314 processor_set_t pset
)
316 ipc_port_dealloc_kernel(pset
->pset_self
);
317 ipc_port_dealloc_kernel(pset
->pset_name_self
);
321 * processor_set_default, processor_set_default_priv:
323 * Return ports for manipulating default_processor set. MiG code
324 * differentiates between these two routines.
327 processor_set_default(
329 processor_set_t
*pset
)
331 if (host
== HOST_NULL
)
332 return(KERN_INVALID_ARGUMENT
);
334 *pset
= &default_pset
;
335 pset_reference(*pset
);
336 return(KERN_SUCCESS
);
340 * Routine: convert_port_to_host
342 * Convert from a port to a host.
343 * Doesn't consume the port ref; the host produced may be null.
349 convert_port_to_host(
352 host_t host
= HOST_NULL
;
354 if (IP_VALID(port
)) {
356 if (ip_active(port
) &&
357 ((ip_kotype(port
) == IKOT_HOST
) ||
358 (ip_kotype(port
) == IKOT_HOST_PRIV
)
360 host
= (host_t
) port
->ip_kobject
;
368 * Routine: convert_port_to_host_priv
370 * Convert from a port to a host.
371 * Doesn't consume the port ref; the host produced may be null.
377 convert_port_to_host_priv(
380 host_t host
= HOST_NULL
;
382 if (IP_VALID(port
)) {
384 if (ip_active(port
) &&
385 (ip_kotype(port
) == IKOT_HOST_PRIV
))
386 host
= (host_t
) port
->ip_kobject
;
394 * Routine: convert_port_to_processor
396 * Convert from a port to a processor.
397 * Doesn't consume the port ref;
398 * the processor produced may be null.
404 convert_port_to_processor(
407 processor_t processor
= PROCESSOR_NULL
;
409 if (IP_VALID(port
)) {
411 if (ip_active(port
) &&
412 (ip_kotype(port
) == IKOT_PROCESSOR
))
413 processor
= (processor_t
) port
->ip_kobject
;
421 * Routine: convert_port_to_pset
423 * Convert from a port to a pset.
424 * Doesn't consume the port ref; produces a pset ref,
431 convert_port_to_pset(
435 processor_set_t pset
= PROCESSOR_SET_NULL
;
438 while (!r
&& IP_VALID(port
)) {
440 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
447 * Routine: convert_port_to_pset_name
449 * Convert from a port to a pset.
450 * Doesn't consume the port ref; produces a pset ref,
457 convert_port_to_pset_name(
461 processor_set_t pset
= PROCESSOR_SET_NULL
;
464 while (!r
&& IP_VALID(port
)) {
466 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
473 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
475 processor_set_t pset
;
477 pset
= PROCESSOR_SET_NULL
;
478 if (ip_active(port
) &&
479 ((ip_kotype(port
) == IKOT_PSET
) ||
480 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
481 pset
= (processor_set_t
) port
->ip_kobject
;
482 if (!pset_lock_try(pset
)) {
496 * Routine: convert_host_to_port
498 * Convert from a host to a port.
499 * Produces a naked send right which may be invalid.
505 convert_host_to_port(
510 host_get_host_port(host
, &port
);
515 * Routine: convert_processor_to_port
517 * Convert from a processor to a port.
518 * Produces a naked send right which may be invalid.
524 convert_processor_to_port(
525 processor_t processor
)
531 processor_lock(processor
);
533 if (processor
->processor_self
!= IP_NULL
)
534 port
= ipc_port_make_send(processor
->processor_self
);
538 processor_unlock(processor
);
545 * Routine: convert_pset_to_port
547 * Convert from a pset to a port.
548 * Consumes a pset ref; produces a naked send right
549 * which may be invalid.
555 convert_pset_to_port(
556 processor_set_t pset
)
562 port
= ipc_port_make_send(pset
->pset_self
);
567 pset_deallocate(pset
);
572 * Routine: convert_pset_name_to_port
574 * Convert from a pset to a port.
575 * Consumes a pset ref; produces a naked send right
576 * which may be invalid.
582 convert_pset_name_to_port(
583 processor_set_name_t pset
)
589 port
= ipc_port_make_send(pset
->pset_name_self
);
594 pset_deallocate(pset
);
599 * Routine: convert_port_to_host_security
601 * Convert from a port to a host security.
602 * Doesn't consume the port ref; the port produced may be null.
608 convert_port_to_host_security(
611 host_t host
= HOST_NULL
;
613 if (IP_VALID(port
)) {
615 if (ip_active(port
) &&
616 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
617 host
= (host_t
) port
->ip_kobject
;
625 * Routine: host_set_exception_ports [kernel call]
627 * Sets the host exception port, flavor and
628 * behavior for the exception types specified by the mask.
629 * There will be one send right per exception per valid
632 * Nothing locked. If successful, consumes
633 * the supplied send right.
635 * KERN_SUCCESS Changed the special port.
636 * KERN_INVALID_ARGUMENT The host_priv is not valid,
637 * Illegal mask bit set.
638 * Illegal exception behavior
641 host_set_exception_ports(
642 host_priv_t host_priv
,
643 exception_mask_t exception_mask
,
645 exception_behavior_t new_behavior
,
646 thread_state_flavor_t new_flavor
)
649 ipc_port_t old_port
[EXC_TYPES_COUNT
];
651 if (host_priv
== HOST_PRIV_NULL
) {
652 return KERN_INVALID_ARGUMENT
;
655 assert(host_priv
== &realhost
);
657 if (exception_mask
& ~EXC_MASK_ALL
) {
658 return KERN_INVALID_ARGUMENT
;
661 if (IP_VALID(new_port
)) {
662 switch (new_behavior
) {
663 case EXCEPTION_DEFAULT
:
664 case EXCEPTION_STATE
:
665 case EXCEPTION_STATE_IDENTITY
:
668 return KERN_INVALID_ARGUMENT
;
671 /* Cannot easily check "new_flavor", but that just means that
672 * the flavor in the generated exception message might be garbage:
675 host_lock(host_priv
);
677 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
678 if (exception_mask
& (1 << i
)) {
679 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
680 host_priv
->exc_actions
[i
].port
=
681 ipc_port_copy_send(new_port
);
682 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
683 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
685 old_port
[i
] = IP_NULL
;
689 * Consume send rights without any lock held.
691 host_unlock(host_priv
);
692 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
693 if (IP_VALID(old_port
[i
]))
694 ipc_port_release_send(old_port
[i
]);
695 if (IP_VALID(new_port
)) /* consume send right */
696 ipc_port_release_send(new_port
);
702 * Routine: host_get_exception_ports [kernel call]
704 * Clones a send right for each of the host's exception
705 * ports specified in the mask and returns the behaviour
706 * and flavor of said port.
708 * Returns upto [in} CountCnt elements.
713 * KERN_SUCCESS Extracted a send right.
714 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
715 * Invalid special port,
716 * Illegal mask bit set.
717 * KERN_FAILURE The thread is dead.
720 host_get_exception_ports(
721 host_priv_t host_priv
,
722 exception_mask_t exception_mask
,
723 exception_mask_array_t masks
,
724 mach_msg_type_number_t
* CountCnt
,
725 exception_port_array_t ports
,
726 exception_behavior_array_t behaviors
,
727 thread_state_flavor_array_t flavors
)
729 unsigned int i
, j
, count
;
731 if (host_priv
== HOST_PRIV_NULL
)
732 return KERN_INVALID_ARGUMENT
;
734 if (exception_mask
& ~EXC_MASK_ALL
) {
735 return KERN_INVALID_ARGUMENT
;
738 assert (host_priv
== &realhost
);
740 host_lock(host_priv
);
744 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
745 if (exception_mask
& (1 << i
)) {
746 for (j
= 0; j
< count
; j
++) {
748 * search for an identical entry, if found
749 * set corresponding mask for this exception.
751 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
752 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
753 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
755 masks
[j
] |= (1 << i
);
762 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
763 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
764 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
766 if (count
> *CountCnt
) {
772 host_unlock(host_priv
);
779 host_swap_exception_ports(
780 host_priv_t host_priv
,
781 exception_mask_t exception_mask
,
783 exception_behavior_t new_behavior
,
784 thread_state_flavor_t new_flavor
,
785 exception_mask_array_t masks
,
786 mach_msg_type_number_t
* CountCnt
,
787 exception_port_array_t ports
,
788 exception_behavior_array_t behaviors
,
789 thread_state_flavor_array_t flavors
)
794 ipc_port_t old_port
[EXC_TYPES_COUNT
];
796 if (host_priv
== HOST_PRIV_NULL
)
797 return KERN_INVALID_ARGUMENT
;
799 if (exception_mask
& ~EXC_MASK_ALL
) {
800 return KERN_INVALID_ARGUMENT
;
803 if (IP_VALID(new_port
)) {
804 switch (new_behavior
) {
805 case EXCEPTION_DEFAULT
:
806 case EXCEPTION_STATE
:
807 case EXCEPTION_STATE_IDENTITY
:
810 return KERN_INVALID_ARGUMENT
;
813 /* Cannot easily check "new_flavor", but that just means that
814 * the flavor in the generated exception message might be garbage:
817 host_lock(host_priv
);
821 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
822 if (exception_mask
& (1 << i
)) {
823 for (j
= 0; j
< count
; j
++) {
825 * search for an identical entry, if found
826 * set corresponding mask for this exception.
828 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
829 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
830 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
832 masks
[j
] |= (1 << i
);
839 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
840 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
841 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
844 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
845 host_priv
->exc_actions
[i
].port
=
846 ipc_port_copy_send(new_port
);
847 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
848 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
849 if (count
> *CountCnt
) {
853 old_port
[i
] = IP_NULL
;
855 host_unlock(host_priv
);
858 * Consume send rights without any lock held.
860 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
861 if (IP_VALID(old_port
[i
]))
862 ipc_port_release_send(old_port
[i
]);
863 if (IP_VALID(new_port
)) /* consume send right */
864 ipc_port_release_send(new_port
);