2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
57 * Routines to implement host ports.
59 #include <mach/message.h>
60 #include <mach/mach_traps.h>
61 #include <mach/mach_host_server.h>
62 #include <mach/host_priv_server.h>
63 #include <kern/host.h>
64 #include <kern/processor.h>
65 #include <kern/lock.h>
66 #include <kern/task.h>
67 #include <kern/thread.h>
68 #include <kern/ipc_host.h>
69 #include <kern/ipc_kobject.h>
70 #include <kern/misc_protos.h>
72 #include <ipc/ipc_port.h>
73 #include <ipc/ipc_space.h>
76 * Forward declarations
80 ipc_processor_terminate(
81 processor_t processor
);
84 ipc_processor_disable(
85 processor_t processor
);
89 ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
);
92 * ipc_host_init: set up various things.
95 void ipc_host_init(void)
100 mutex_init(&realhost
.lock
, 0);
103 * Allocate and set up the two host ports.
105 port
= ipc_port_alloc_kernel();
107 panic("ipc_host_init");
109 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_SECURITY
);
110 kernel_set_special_port(&realhost
, HOST_SECURITY_PORT
,
111 ipc_port_make_send(port
));
113 port
= ipc_port_alloc_kernel();
115 panic("ipc_host_init");
117 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST
);
118 kernel_set_special_port(&realhost
, HOST_PORT
,
119 ipc_port_make_send(port
));
121 port
= ipc_port_alloc_kernel();
123 panic("ipc_host_init");
125 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_PRIV
);
126 kernel_set_special_port(&realhost
, HOST_PRIV_PORT
,
127 ipc_port_make_send(port
));
129 /* the rest of the special ports will be set up later */
131 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
132 realhost
.exc_actions
[i
].port
= IP_NULL
;
136 * Set up ipc for default processor set.
138 ipc_pset_init(&default_pset
);
139 ipc_pset_enable(&default_pset
);
142 * And for master processor
144 ipc_processor_init(master_processor
);
145 ipc_processor_enable(master_processor
);
149 * Routine: host_self_trap [mach trap]
151 * Give the caller send rights for his own host port.
155 * MACH_PORT_NULL if there are any resource failures
161 __unused
struct host_self_trap_args
*args
)
164 mach_port_name_t name
;
166 sright
= ipc_port_copy_send(current_task()->itk_host
);
167 name
= ipc_port_copyout_send(sright
, current_space());
172 * ipc_processor_init:
174 * Initialize ipc access to processor by allocating port.
179 processor_t processor
)
183 port
= ipc_port_alloc_kernel();
185 panic("ipc_processor_init");
186 processor
->processor_self
= port
;
190 * ipc_processor_enable:
192 * Enable ipc control of processor by setting port object.
195 ipc_processor_enable(
196 processor_t processor
)
200 myport
= processor
->processor_self
;
201 ipc_kobject_set(myport
, (ipc_kobject_t
) processor
, IKOT_PROCESSOR
);
205 * ipc_processor_disable:
207 * Disable ipc control of processor by clearing port object.
210 ipc_processor_disable(
211 processor_t processor
)
215 myport
= processor
->processor_self
;
216 if (myport
== IP_NULL
)
218 ipc_kobject_set(myport
, IKO_NULL
, IKOT_NONE
);
222 * ipc_processor_terminate:
224 * Processor is off-line. Destroy ipc control port.
227 ipc_processor_terminate(
228 processor_t processor
)
234 processor_lock(processor
);
235 myport
= processor
->processor_self
;
236 if (myport
== IP_NULL
) {
237 processor_unlock(processor
);
242 processor
->processor_self
= IP_NULL
;
243 processor_unlock(processor
);
246 ipc_port_dealloc_kernel(myport
);
252 * Initialize ipc control of a processor set by allocating its ports.
257 processor_set_t pset
)
261 port
= ipc_port_alloc_kernel();
263 panic("ipc_pset_init");
264 pset
->pset_self
= port
;
266 port
= ipc_port_alloc_kernel();
268 panic("ipc_pset_init");
269 pset
->pset_name_self
= port
;
275 * Enable ipc access to a processor set.
279 processor_set_t pset
)
283 ipc_kobject_set(pset
->pset_self
,
284 (ipc_kobject_t
) pset
, IKOT_PSET
);
285 ipc_kobject_set(pset
->pset_name_self
,
286 (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
287 pset
->ref_count
+= 2;
295 * Disable ipc access to a processor set by clearing the port objects.
296 * Caller must hold pset lock and a reference to the pset. Ok to
297 * just decrement pset reference count as a result.
301 processor_set_t pset
)
303 ipc_kobject_set(pset
->pset_self
, IKO_NULL
, IKOT_NONE
);
304 ipc_kobject_set(pset
->pset_name_self
, IKO_NULL
, IKOT_NONE
);
305 pset
->ref_count
-= 2;
309 * ipc_pset_terminate:
311 * Processor set is dead. Deallocate the ipc control structures.
315 processor_set_t pset
)
317 ipc_port_dealloc_kernel(pset
->pset_self
);
318 ipc_port_dealloc_kernel(pset
->pset_name_self
);
322 * processor_set_default, processor_set_default_priv:
324 * Return ports for manipulating default_processor set. MiG code
325 * differentiates between these two routines.
328 processor_set_default(
330 processor_set_t
*pset
)
332 if (host
== HOST_NULL
)
333 return(KERN_INVALID_ARGUMENT
);
335 *pset
= &default_pset
;
336 pset_reference(*pset
);
337 return(KERN_SUCCESS
);
341 * Routine: convert_port_to_host
343 * Convert from a port to a host.
344 * Doesn't consume the port ref; the host produced may be null.
350 convert_port_to_host(
353 host_t host
= HOST_NULL
;
355 if (IP_VALID(port
)) {
357 if (ip_active(port
) &&
358 ((ip_kotype(port
) == IKOT_HOST
) ||
359 (ip_kotype(port
) == IKOT_HOST_PRIV
)
361 host
= (host_t
) port
->ip_kobject
;
369 * Routine: convert_port_to_host_priv
371 * Convert from a port to a host.
372 * Doesn't consume the port ref; the host produced may be null.
378 convert_port_to_host_priv(
381 host_t host
= HOST_NULL
;
383 if (IP_VALID(port
)) {
385 if (ip_active(port
) &&
386 (ip_kotype(port
) == IKOT_HOST_PRIV
))
387 host
= (host_t
) port
->ip_kobject
;
395 * Routine: convert_port_to_processor
397 * Convert from a port to a processor.
398 * Doesn't consume the port ref;
399 * the processor produced may be null.
405 convert_port_to_processor(
408 processor_t processor
= PROCESSOR_NULL
;
410 if (IP_VALID(port
)) {
412 if (ip_active(port
) &&
413 (ip_kotype(port
) == IKOT_PROCESSOR
))
414 processor
= (processor_t
) port
->ip_kobject
;
422 * Routine: convert_port_to_pset
424 * Convert from a port to a pset.
425 * Doesn't consume the port ref; produces a pset ref,
432 convert_port_to_pset(
436 processor_set_t pset
= PROCESSOR_SET_NULL
;
439 while (!r
&& IP_VALID(port
)) {
441 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
448 * Routine: convert_port_to_pset_name
450 * Convert from a port to a pset.
451 * Doesn't consume the port ref; produces a pset ref,
458 convert_port_to_pset_name(
462 processor_set_t pset
= PROCESSOR_SET_NULL
;
465 while (!r
&& IP_VALID(port
)) {
467 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
474 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
476 processor_set_t pset
;
478 pset
= PROCESSOR_SET_NULL
;
479 if (ip_active(port
) &&
480 ((ip_kotype(port
) == IKOT_PSET
) ||
481 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
482 pset
= (processor_set_t
) port
->ip_kobject
;
483 if (!pset_lock_try(pset
)) {
497 * Routine: convert_host_to_port
499 * Convert from a host to a port.
500 * Produces a naked send right which may be invalid.
506 convert_host_to_port(
511 host_get_host_port(host
, &port
);
516 * Routine: convert_processor_to_port
518 * Convert from a processor to a port.
519 * Produces a naked send right which may be invalid.
525 convert_processor_to_port(
526 processor_t processor
)
532 processor_lock(processor
);
534 if (processor
->processor_self
!= IP_NULL
)
535 port
= ipc_port_make_send(processor
->processor_self
);
539 processor_unlock(processor
);
546 * Routine: convert_pset_to_port
548 * Convert from a pset to a port.
549 * Consumes a pset ref; produces a naked send right
550 * which may be invalid.
556 convert_pset_to_port(
557 processor_set_t pset
)
563 port
= ipc_port_make_send(pset
->pset_self
);
568 pset_deallocate(pset
);
573 * Routine: convert_pset_name_to_port
575 * Convert from a pset to a port.
576 * Consumes a pset ref; produces a naked send right
577 * which may be invalid.
583 convert_pset_name_to_port(
584 processor_set_name_t pset
)
590 port
= ipc_port_make_send(pset
->pset_name_self
);
595 pset_deallocate(pset
);
600 * Routine: convert_port_to_host_security
602 * Convert from a port to a host security.
603 * Doesn't consume the port ref; the port produced may be null.
609 convert_port_to_host_security(
612 host_t host
= HOST_NULL
;
614 if (IP_VALID(port
)) {
616 if (ip_active(port
) &&
617 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
618 host
= (host_t
) port
->ip_kobject
;
626 * Routine: host_set_exception_ports [kernel call]
628 * Sets the host exception port, flavor and
629 * behavior for the exception types specified by the mask.
630 * There will be one send right per exception per valid
633 * Nothing locked. If successful, consumes
634 * the supplied send right.
636 * KERN_SUCCESS Changed the special port.
637 * KERN_INVALID_ARGUMENT The host_priv is not valid,
638 * Illegal mask bit set.
639 * Illegal exception behavior
642 host_set_exception_ports(
643 host_priv_t host_priv
,
644 exception_mask_t exception_mask
,
646 exception_behavior_t new_behavior
,
647 thread_state_flavor_t new_flavor
)
650 ipc_port_t old_port
[EXC_TYPES_COUNT
];
652 if (host_priv
== HOST_PRIV_NULL
) {
653 return KERN_INVALID_ARGUMENT
;
656 assert(host_priv
== &realhost
);
658 if (exception_mask
& ~EXC_MASK_ALL
) {
659 return KERN_INVALID_ARGUMENT
;
662 if (IP_VALID(new_port
)) {
663 switch (new_behavior
) {
664 case EXCEPTION_DEFAULT
:
665 case EXCEPTION_STATE
:
666 case EXCEPTION_STATE_IDENTITY
:
669 return KERN_INVALID_ARGUMENT
;
672 /* Cannot easily check "new_flavor", but that just means that
673 * the flavor in the generated exception message might be garbage:
676 host_lock(host_priv
);
678 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
679 if (exception_mask
& (1 << i
)) {
680 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
681 host_priv
->exc_actions
[i
].port
=
682 ipc_port_copy_send(new_port
);
683 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
684 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
686 old_port
[i
] = IP_NULL
;
690 * Consume send rights without any lock held.
692 host_unlock(host_priv
);
693 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
694 if (IP_VALID(old_port
[i
]))
695 ipc_port_release_send(old_port
[i
]);
696 if (IP_VALID(new_port
)) /* consume send right */
697 ipc_port_release_send(new_port
);
703 * Routine: host_get_exception_ports [kernel call]
705 * Clones a send right for each of the host's exception
706 * ports specified in the mask and returns the behaviour
707 * and flavor of said port.
709 * Returns upto [in} CountCnt elements.
714 * KERN_SUCCESS Extracted a send right.
715 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
716 * Invalid special port,
717 * Illegal mask bit set.
718 * KERN_FAILURE The thread is dead.
721 host_get_exception_ports(
722 host_priv_t host_priv
,
723 exception_mask_t exception_mask
,
724 exception_mask_array_t masks
,
725 mach_msg_type_number_t
* CountCnt
,
726 exception_port_array_t ports
,
727 exception_behavior_array_t behaviors
,
728 thread_state_flavor_array_t flavors
)
730 unsigned int i
, j
, count
;
732 if (host_priv
== HOST_PRIV_NULL
)
733 return KERN_INVALID_ARGUMENT
;
735 if (exception_mask
& ~EXC_MASK_ALL
) {
736 return KERN_INVALID_ARGUMENT
;
739 assert (host_priv
== &realhost
);
741 host_lock(host_priv
);
745 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
746 if (exception_mask
& (1 << i
)) {
747 for (j
= 0; j
< count
; j
++) {
749 * search for an identical entry, if found
750 * set corresponding mask for this exception.
752 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
753 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
754 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
756 masks
[j
] |= (1 << i
);
763 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
764 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
765 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
767 if (count
> *CountCnt
) {
773 host_unlock(host_priv
);
780 host_swap_exception_ports(
781 host_priv_t host_priv
,
782 exception_mask_t exception_mask
,
784 exception_behavior_t new_behavior
,
785 thread_state_flavor_t new_flavor
,
786 exception_mask_array_t masks
,
787 mach_msg_type_number_t
* CountCnt
,
788 exception_port_array_t ports
,
789 exception_behavior_array_t behaviors
,
790 thread_state_flavor_array_t flavors
)
795 ipc_port_t old_port
[EXC_TYPES_COUNT
];
797 if (host_priv
== HOST_PRIV_NULL
)
798 return KERN_INVALID_ARGUMENT
;
800 if (exception_mask
& ~EXC_MASK_ALL
) {
801 return KERN_INVALID_ARGUMENT
;
804 if (IP_VALID(new_port
)) {
805 switch (new_behavior
) {
806 case EXCEPTION_DEFAULT
:
807 case EXCEPTION_STATE
:
808 case EXCEPTION_STATE_IDENTITY
:
811 return KERN_INVALID_ARGUMENT
;
814 /* Cannot easily check "new_flavor", but that just means that
815 * the flavor in the generated exception message might be garbage:
818 host_lock(host_priv
);
822 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
823 if (exception_mask
& (1 << i
)) {
824 for (j
= 0; j
< count
; j
++) {
826 * search for an identical entry, if found
827 * set corresponding mask for this exception.
829 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
830 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
831 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
833 masks
[j
] |= (1 << i
);
840 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
841 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
842 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
845 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
846 host_priv
->exc_actions
[i
].port
=
847 ipc_port_copy_send(new_port
);
848 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
849 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
850 if (count
> *CountCnt
) {
854 old_port
[i
] = IP_NULL
;
856 host_unlock(host_priv
);
859 * Consume send rights without any lock held.
861 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
862 if (IP_VALID(old_port
[i
]))
863 ipc_port_release_send(old_port
[i
]);
864 if (IP_VALID(new_port
)) /* consume send right */
865 ipc_port_release_send(new_port
);