2 * Copyright (c) 2000-2003 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/etap_events.h>
61 #include <mach/mach_host_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
, ETAP_MISC_MASTER
);
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
163 sright
= ipc_port_copy_send(current_task()->itk_host
);
164 return ipc_port_copyout_send(sright
, current_space());
168 * ipc_processor_init:
170 * Initialize ipc access to processor by allocating port.
175 processor_t processor
)
179 port
= ipc_port_alloc_kernel();
181 panic("ipc_processor_init");
182 processor
->processor_self
= port
;
186 * ipc_processor_enable:
188 * Enable ipc control of processor by setting port object.
191 ipc_processor_enable(
192 processor_t processor
)
196 myport
= processor
->processor_self
;
197 ipc_kobject_set(myport
, (ipc_kobject_t
) processor
, IKOT_PROCESSOR
);
201 * ipc_processor_disable:
203 * Disable ipc control of processor by clearing port object.
206 ipc_processor_disable(
207 processor_t processor
)
211 myport
= processor
->processor_self
;
212 if (myport
== IP_NULL
)
214 ipc_kobject_set(myport
, IKO_NULL
, IKOT_NONE
);
218 * ipc_processor_terminate:
220 * Processor is off-line. Destroy ipc control port.
223 ipc_processor_terminate(
224 processor_t processor
)
230 processor_lock(processor
);
231 myport
= processor
->processor_self
;
232 if (myport
== IP_NULL
) {
233 processor_unlock(processor
);
238 processor
->processor_self
= IP_NULL
;
239 processor_unlock(processor
);
242 ipc_port_dealloc_kernel(myport
);
248 * Initialize ipc control of a processor set by allocating its ports.
253 processor_set_t pset
)
257 port
= ipc_port_alloc_kernel();
259 panic("ipc_pset_init");
260 pset
->pset_self
= port
;
262 port
= ipc_port_alloc_kernel();
264 panic("ipc_pset_init");
265 pset
->pset_name_self
= port
;
271 * Enable ipc access to a processor set.
275 processor_set_t pset
)
279 ipc_kobject_set(pset
->pset_self
,
280 (ipc_kobject_t
) pset
, IKOT_PSET
);
281 ipc_kobject_set(pset
->pset_name_self
,
282 (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
283 pset
->ref_count
+= 2;
291 * Disable ipc access to a processor set by clearing the port objects.
292 * Caller must hold pset lock and a reference to the pset. Ok to
293 * just decrement pset reference count as a result.
297 processor_set_t pset
)
299 ipc_kobject_set(pset
->pset_self
, IKO_NULL
, IKOT_NONE
);
300 ipc_kobject_set(pset
->pset_name_self
, IKO_NULL
, IKOT_NONE
);
301 pset
->ref_count
-= 2;
305 * ipc_pset_terminate:
307 * Processor set is dead. Deallocate the ipc control structures.
311 processor_set_t pset
)
313 ipc_port_dealloc_kernel(pset
->pset_self
);
314 ipc_port_dealloc_kernel(pset
->pset_name_self
);
318 * processor_set_default, processor_set_default_priv:
320 * Return ports for manipulating default_processor set. MiG code
321 * differentiates between these two routines.
324 processor_set_default(
326 processor_set_t
*pset
)
328 if (host
== HOST_NULL
)
329 return(KERN_INVALID_ARGUMENT
);
331 *pset
= &default_pset
;
332 pset_reference(*pset
);
333 return(KERN_SUCCESS
);
337 * Routine: convert_port_to_host
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(
349 host_t host
= HOST_NULL
;
351 if (IP_VALID(port
)) {
353 if (ip_active(port
) &&
354 ((ip_kotype(port
) == IKOT_HOST
) ||
355 (ip_kotype(port
) == IKOT_HOST_PRIV
)
357 host
= (host_t
) port
->ip_kobject
;
365 * Routine: convert_port_to_host_priv
367 * Convert from a port to a host.
368 * Doesn't consume the port ref; the host produced may be null.
374 convert_port_to_host_priv(
377 host_t host
= HOST_NULL
;
379 if (IP_VALID(port
)) {
381 if (ip_active(port
) &&
382 (ip_kotype(port
) == IKOT_HOST_PRIV
))
383 host
= (host_t
) port
->ip_kobject
;
391 * Routine: convert_port_to_processor
393 * Convert from a port to a processor.
394 * Doesn't consume the port ref;
395 * the processor produced may be null.
401 convert_port_to_processor(
404 processor_t processor
= PROCESSOR_NULL
;
406 if (IP_VALID(port
)) {
408 if (ip_active(port
) &&
409 (ip_kotype(port
) == IKOT_PROCESSOR
))
410 processor
= (processor_t
) port
->ip_kobject
;
418 * Routine: convert_port_to_pset
420 * Convert from a port to a pset.
421 * Doesn't consume the port ref; produces a pset ref,
428 convert_port_to_pset(
432 processor_set_t pset
= PROCESSOR_SET_NULL
;
435 while (!r
&& IP_VALID(port
)) {
437 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
444 * Routine: convert_port_to_pset_name
446 * Convert from a port to a pset.
447 * Doesn't consume the port ref; produces a pset ref,
454 convert_port_to_pset_name(
458 processor_set_t pset
= PROCESSOR_SET_NULL
;
461 while (!r
&& IP_VALID(port
)) {
463 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
470 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
472 processor_set_t pset
;
474 pset
= PROCESSOR_SET_NULL
;
475 if (ip_active(port
) &&
476 ((ip_kotype(port
) == IKOT_PSET
) ||
477 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
478 pset
= (processor_set_t
) port
->ip_kobject
;
479 if (!pset_lock_try(pset
)) {
493 * Routine: convert_host_to_port
495 * Convert from a host to a port.
496 * Produces a naked send right which may be invalid.
502 convert_host_to_port(
507 host_get_host_port(host
, &port
);
512 * Routine: convert_processor_to_port
514 * Convert from a processor to a port.
515 * Produces a naked send right which may be invalid.
521 convert_processor_to_port(
522 processor_t processor
)
528 processor_lock(processor
);
530 if (processor
->processor_self
!= IP_NULL
)
531 port
= ipc_port_make_send(processor
->processor_self
);
535 processor_unlock(processor
);
542 * Routine: convert_pset_to_port
544 * Convert from a pset to a port.
545 * Consumes a pset ref; produces a naked send right
546 * which may be invalid.
552 convert_pset_to_port(
553 processor_set_t pset
)
559 port
= ipc_port_make_send(pset
->pset_self
);
564 pset_deallocate(pset
);
569 * Routine: convert_pset_name_to_port
571 * Convert from a pset to a port.
572 * Consumes a pset ref; produces a naked send right
573 * which may be invalid.
579 convert_pset_name_to_port(
580 processor_set_name_t pset
)
586 port
= ipc_port_make_send(pset
->pset_name_self
);
591 pset_deallocate(pset
);
596 * Routine: convert_port_to_host_security
598 * Convert from a port to a host security.
599 * Doesn't consume the port ref; the port produced may be null.
605 convert_port_to_host_security(
608 host_t host
= HOST_NULL
;
610 if (IP_VALID(port
)) {
612 if (ip_active(port
) &&
613 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
614 host
= (host_t
) port
->ip_kobject
;
622 * Routine: host_set_exception_ports [kernel call]
624 * Sets the host exception port, flavor and
625 * behavior for the exception types specified by the mask.
626 * There will be one send right per exception per valid
629 * Nothing locked. If successful, consumes
630 * the supplied send right.
632 * KERN_SUCCESS Changed the special port.
633 * KERN_INVALID_ARGUMENT The host_priv is not valid,
634 * Illegal mask bit set.
635 * Illegal exception behavior
638 host_set_exception_ports(
639 host_priv_t host_priv
,
640 exception_mask_t exception_mask
,
642 exception_behavior_t new_behavior
,
643 thread_state_flavor_t new_flavor
)
646 ipc_port_t old_port
[EXC_TYPES_COUNT
];
648 if (host_priv
== HOST_PRIV_NULL
) {
649 return KERN_INVALID_ARGUMENT
;
652 assert(host_priv
== &realhost
);
654 if (exception_mask
& ~EXC_MASK_ALL
) {
655 return KERN_INVALID_ARGUMENT
;
658 if (IP_VALID(new_port
)) {
659 switch (new_behavior
) {
660 case EXCEPTION_DEFAULT
:
661 case EXCEPTION_STATE
:
662 case EXCEPTION_STATE_IDENTITY
:
665 return KERN_INVALID_ARGUMENT
;
668 /* Cannot easily check "new_flavor", but that just means that
669 * the flavor in the generated exception message might be garbage:
672 host_lock(host_priv
);
674 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
675 if (exception_mask
& (1 << i
)) {
676 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
677 host_priv
->exc_actions
[i
].port
=
678 ipc_port_copy_send(new_port
);
679 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
680 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
682 old_port
[i
] = IP_NULL
;
686 * Consume send rights without any lock held.
688 host_unlock(host_priv
);
689 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
690 if (IP_VALID(old_port
[i
]))
691 ipc_port_release_send(old_port
[i
]);
692 if (IP_VALID(new_port
)) /* consume send right */
693 ipc_port_release_send(new_port
);
699 * Routine: host_get_exception_ports [kernel call]
701 * Clones a send right for each of the host's exception
702 * ports specified in the mask and returns the behaviour
703 * and flavor of said port.
705 * Returns upto [in} CountCnt elements.
710 * KERN_SUCCESS Extracted a send right.
711 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
712 * Invalid special port,
713 * Illegal mask bit set.
714 * KERN_FAILURE The thread is dead.
717 host_get_exception_ports(
718 host_priv_t host_priv
,
719 exception_mask_t exception_mask
,
720 exception_mask_array_t masks
,
721 mach_msg_type_number_t
* CountCnt
,
722 exception_port_array_t ports
,
723 exception_behavior_array_t behaviors
,
724 thread_state_flavor_array_t flavors
)
730 if (host_priv
== HOST_PRIV_NULL
)
731 return KERN_INVALID_ARGUMENT
;
733 if (exception_mask
& ~EXC_MASK_ALL
) {
734 return KERN_INVALID_ARGUMENT
;
737 assert (host_priv
== &realhost
);
739 host_lock(host_priv
);
743 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
744 if (exception_mask
& (1 << i
)) {
745 for (j
= 0; j
< count
; j
++) {
747 * search for an identical entry, if found
748 * set corresponding mask for this exception.
750 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
751 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
752 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
754 masks
[j
] |= (1 << i
);
761 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
762 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
763 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
765 if (count
> *CountCnt
) {
771 host_unlock(host_priv
);
778 host_swap_exception_ports(
779 host_priv_t host_priv
,
780 exception_mask_t exception_mask
,
782 exception_behavior_t new_behavior
,
783 thread_state_flavor_t new_flavor
,
784 exception_mask_array_t masks
,
785 mach_msg_type_number_t
* CountCnt
,
786 exception_port_array_t ports
,
787 exception_behavior_array_t behaviors
,
788 thread_state_flavor_array_t flavors
)
793 ipc_port_t old_port
[EXC_TYPES_COUNT
];
795 if (host_priv
== HOST_PRIV_NULL
)
796 return KERN_INVALID_ARGUMENT
;
798 if (exception_mask
& ~EXC_MASK_ALL
) {
799 return KERN_INVALID_ARGUMENT
;
802 if (IP_VALID(new_port
)) {
803 switch (new_behavior
) {
804 case EXCEPTION_DEFAULT
:
805 case EXCEPTION_STATE
:
806 case EXCEPTION_STATE_IDENTITY
:
809 return KERN_INVALID_ARGUMENT
;
812 /* Cannot easily check "new_flavor", but that just means that
813 * the flavor in the generated exception message might be garbage:
816 host_lock(host_priv
);
820 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
821 if (exception_mask
& (1 << i
)) {
822 for (j
= 0; j
< count
; j
++) {
824 * search for an identical entry, if found
825 * set corresponding mask for this exception.
827 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
828 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
829 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
831 masks
[j
] |= (1 << i
);
838 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
839 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
840 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
843 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
844 host_priv
->exc_actions
[i
].port
=
845 ipc_port_copy_send(new_port
);
846 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
847 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
848 if (count
> *CountCnt
) {
852 old_port
[i
] = IP_NULL
;
854 host_unlock(host_priv
);
857 * Consume send rights without any lock held.
859 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
860 if (IP_VALID(old_port
[i
]))
861 ipc_port_release_send(old_port
[i
]);
862 if (IP_VALID(new_port
)) /* consume send right */
863 ipc_port_release_send(new_port
);