2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
59 * Routines to implement host ports.
61 #include <mach/message.h>
62 #include <mach/mach_traps.h>
63 #include <mach/etap_events.h>
64 #include <mach/mach_host_server.h>
65 #include <kern/host.h>
66 #include <kern/processor.h>
67 #include <kern/lock.h>
68 #include <kern/task.h>
69 #include <kern/thread.h>
70 #include <kern/ipc_host.h>
71 #include <kern/ipc_kobject.h>
72 #include <kern/misc_protos.h>
74 #include <ipc/ipc_port.h>
75 #include <ipc/ipc_space.h>
78 * Forward declarations
82 ipc_processor_terminate(
83 processor_t processor
);
86 ipc_processor_disable(
87 processor_t processor
);
91 ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
);
94 * ipc_host_init: set up various things.
97 void ipc_host_init(void)
102 mutex_init(&realhost
.lock
, ETAP_MISC_MASTER
);
105 * Allocate and set up the two host ports.
107 port
= ipc_port_alloc_kernel();
109 panic("ipc_host_init");
111 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_SECURITY
);
112 kernel_set_special_port(&realhost
, HOST_SECURITY_PORT
,
113 ipc_port_make_send(port
));
115 port
= ipc_port_alloc_kernel();
117 panic("ipc_host_init");
119 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST
);
120 kernel_set_special_port(&realhost
, HOST_PORT
,
121 ipc_port_make_send(port
));
123 port
= ipc_port_alloc_kernel();
125 panic("ipc_host_init");
127 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_PRIV
);
128 kernel_set_special_port(&realhost
, HOST_PRIV_PORT
,
129 ipc_port_make_send(port
));
131 /* the rest of the special ports will be set up later */
133 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
134 realhost
.exc_actions
[i
].port
= IP_NULL
;
138 * Set up ipc for default processor set.
140 ipc_pset_init(&default_pset
);
141 ipc_pset_enable(&default_pset
);
144 * And for master processor
146 ipc_processor_init(master_processor
);
147 ipc_processor_enable(master_processor
);
151 * Routine: host_self_trap [mach trap]
153 * Give the caller send rights for his own host port.
157 * MACH_PORT_NULL if there are any resource failures
166 sright
= ipc_port_copy_send(current_task()->itk_host
);
167 return 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
)
733 if (host_priv
== HOST_PRIV_NULL
)
734 return KERN_INVALID_ARGUMENT
;
736 if (exception_mask
& ~EXC_MASK_ALL
) {
737 return KERN_INVALID_ARGUMENT
;
740 assert (host_priv
== &realhost
);
742 host_lock(host_priv
);
746 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
747 if (exception_mask
& (1 << i
)) {
748 for (j
= 0; j
< count
; j
++) {
750 * search for an identical entry, if found
751 * set corresponding mask for this exception.
753 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
754 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
755 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
757 masks
[j
] |= (1 << i
);
764 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
765 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
766 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
768 if (count
> *CountCnt
) {
774 host_unlock(host_priv
);
781 host_swap_exception_ports(
782 host_priv_t host_priv
,
783 exception_mask_t exception_mask
,
785 exception_behavior_t new_behavior
,
786 thread_state_flavor_t new_flavor
,
787 exception_mask_array_t masks
,
788 mach_msg_type_number_t
* CountCnt
,
789 exception_port_array_t ports
,
790 exception_behavior_array_t behaviors
,
791 thread_state_flavor_array_t flavors
)
796 ipc_port_t old_port
[EXC_TYPES_COUNT
];
798 if (host_priv
== HOST_PRIV_NULL
)
799 return KERN_INVALID_ARGUMENT
;
801 if (exception_mask
& ~EXC_MASK_ALL
) {
802 return KERN_INVALID_ARGUMENT
;
805 if (IP_VALID(new_port
)) {
806 switch (new_behavior
) {
807 case EXCEPTION_DEFAULT
:
808 case EXCEPTION_STATE
:
809 case EXCEPTION_STATE_IDENTITY
:
812 return KERN_INVALID_ARGUMENT
;
815 /* Cannot easily check "new_flavor", but that just means that
816 * the flavor in the generated exception message might be garbage:
819 host_lock(host_priv
);
823 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
824 if (exception_mask
& (1 << i
)) {
825 for (j
= 0; j
< count
; j
++) {
827 * search for an identical entry, if found
828 * set corresponding mask for this exception.
830 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
831 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
832 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
834 masks
[j
] |= (1 << i
);
841 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
842 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
843 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
846 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
847 host_priv
->exc_actions
[i
].port
=
848 ipc_port_copy_send(new_port
);
849 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
850 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
851 if (count
> *CountCnt
) {
855 old_port
[i
] = IP_NULL
;
857 host_unlock(host_priv
);
860 * Consume send rights without any lock held.
862 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
863 if (IP_VALID(old_port
[i
]))
864 ipc_port_release_send(old_port
[i
]);
865 if (IP_VALID(new_port
)) /* consume send right */
866 ipc_port_release_send(new_port
);