2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
62 * Routines to implement host ports.
64 #include <mach/message.h>
65 #include <mach/mach_traps.h>
66 #include <mach/mach_host_server.h>
67 #include <mach/host_priv_server.h>
68 #include <kern/host.h>
69 #include <kern/processor.h>
70 #include <kern/lock.h>
71 #include <kern/task.h>
72 #include <kern/thread.h>
73 #include <kern/ipc_host.h>
74 #include <kern/ipc_kobject.h>
75 #include <kern/misc_protos.h>
77 #include <ipc/ipc_port.h>
78 #include <ipc/ipc_space.h>
81 * Forward declarations
85 ipc_processor_terminate(
86 processor_t processor
);
89 ipc_processor_disable(
90 processor_t processor
);
94 ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
);
97 * ipc_host_init: set up various things.
100 void ipc_host_init(void)
105 mutex_init(&realhost
.lock
, 0);
108 * Allocate and set up the two host ports.
110 port
= ipc_port_alloc_kernel();
112 panic("ipc_host_init");
114 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_SECURITY
);
115 kernel_set_special_port(&realhost
, HOST_SECURITY_PORT
,
116 ipc_port_make_send(port
));
118 port
= ipc_port_alloc_kernel();
120 panic("ipc_host_init");
122 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST
);
123 kernel_set_special_port(&realhost
, HOST_PORT
,
124 ipc_port_make_send(port
));
126 port
= ipc_port_alloc_kernel();
128 panic("ipc_host_init");
130 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_PRIV
);
131 kernel_set_special_port(&realhost
, HOST_PRIV_PORT
,
132 ipc_port_make_send(port
));
134 /* the rest of the special ports will be set up later */
136 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
137 realhost
.exc_actions
[i
].port
= IP_NULL
;
141 * Set up ipc for default processor set.
143 ipc_pset_init(&default_pset
);
144 ipc_pset_enable(&default_pset
);
147 * And for master processor
149 ipc_processor_init(master_processor
);
150 ipc_processor_enable(master_processor
);
154 * Routine: host_self_trap [mach trap]
156 * Give the caller send rights for his own host port.
160 * MACH_PORT_NULL if there are any resource failures
166 __unused
struct host_self_trap_args
*args
)
169 mach_port_name_t name
;
171 sright
= ipc_port_copy_send(current_task()->itk_host
);
172 name
= ipc_port_copyout_send(sright
, current_space());
177 * ipc_processor_init:
179 * Initialize ipc access to processor by allocating port.
184 processor_t processor
)
188 port
= ipc_port_alloc_kernel();
190 panic("ipc_processor_init");
191 processor
->processor_self
= port
;
195 * ipc_processor_enable:
197 * Enable ipc control of processor by setting port object.
200 ipc_processor_enable(
201 processor_t processor
)
205 myport
= processor
->processor_self
;
206 ipc_kobject_set(myport
, (ipc_kobject_t
) processor
, IKOT_PROCESSOR
);
210 * ipc_processor_disable:
212 * Disable ipc control of processor by clearing port object.
215 ipc_processor_disable(
216 processor_t processor
)
220 myport
= processor
->processor_self
;
221 if (myport
== IP_NULL
)
223 ipc_kobject_set(myport
, IKO_NULL
, IKOT_NONE
);
227 * ipc_processor_terminate:
229 * Processor is off-line. Destroy ipc control port.
232 ipc_processor_terminate(
233 processor_t processor
)
239 processor_lock(processor
);
240 myport
= processor
->processor_self
;
241 if (myport
== IP_NULL
) {
242 processor_unlock(processor
);
247 processor
->processor_self
= IP_NULL
;
248 processor_unlock(processor
);
251 ipc_port_dealloc_kernel(myport
);
257 * Initialize ipc control of a processor set by allocating its ports.
262 processor_set_t pset
)
266 port
= ipc_port_alloc_kernel();
268 panic("ipc_pset_init");
269 pset
->pset_self
= port
;
271 port
= ipc_port_alloc_kernel();
273 panic("ipc_pset_init");
274 pset
->pset_name_self
= port
;
280 * Enable ipc access to a processor set.
284 processor_set_t pset
)
288 ipc_kobject_set(pset
->pset_self
,
289 (ipc_kobject_t
) pset
, IKOT_PSET
);
290 ipc_kobject_set(pset
->pset_name_self
,
291 (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
292 pset
->ref_count
+= 2;
300 * Disable ipc access to a processor set by clearing the port objects.
301 * Caller must hold pset lock and a reference to the pset. Ok to
302 * just decrement pset reference count as a result.
306 processor_set_t pset
)
308 ipc_kobject_set(pset
->pset_self
, IKO_NULL
, IKOT_NONE
);
309 ipc_kobject_set(pset
->pset_name_self
, IKO_NULL
, IKOT_NONE
);
310 pset
->ref_count
-= 2;
314 * ipc_pset_terminate:
316 * Processor set is dead. Deallocate the ipc control structures.
320 processor_set_t pset
)
322 ipc_port_dealloc_kernel(pset
->pset_self
);
323 ipc_port_dealloc_kernel(pset
->pset_name_self
);
327 * processor_set_default, processor_set_default_priv:
329 * Return ports for manipulating default_processor set. MiG code
330 * differentiates between these two routines.
333 processor_set_default(
335 processor_set_t
*pset
)
337 if (host
== HOST_NULL
)
338 return(KERN_INVALID_ARGUMENT
);
340 *pset
= &default_pset
;
341 pset_reference(*pset
);
342 return(KERN_SUCCESS
);
346 * Routine: convert_port_to_host
348 * Convert from a port to a host.
349 * Doesn't consume the port ref; the host produced may be null.
355 convert_port_to_host(
358 host_t host
= HOST_NULL
;
360 if (IP_VALID(port
)) {
362 if (ip_active(port
) &&
363 ((ip_kotype(port
) == IKOT_HOST
) ||
364 (ip_kotype(port
) == IKOT_HOST_PRIV
)
366 host
= (host_t
) port
->ip_kobject
;
374 * Routine: convert_port_to_host_priv
376 * Convert from a port to a host.
377 * Doesn't consume the port ref; the host produced may be null.
383 convert_port_to_host_priv(
386 host_t host
= HOST_NULL
;
388 if (IP_VALID(port
)) {
390 if (ip_active(port
) &&
391 (ip_kotype(port
) == IKOT_HOST_PRIV
))
392 host
= (host_t
) port
->ip_kobject
;
400 * Routine: convert_port_to_processor
402 * Convert from a port to a processor.
403 * Doesn't consume the port ref;
404 * the processor produced may be null.
410 convert_port_to_processor(
413 processor_t processor
= PROCESSOR_NULL
;
415 if (IP_VALID(port
)) {
417 if (ip_active(port
) &&
418 (ip_kotype(port
) == IKOT_PROCESSOR
))
419 processor
= (processor_t
) port
->ip_kobject
;
427 * Routine: convert_port_to_pset
429 * Convert from a port to a pset.
430 * Doesn't consume the port ref; produces a pset ref,
437 convert_port_to_pset(
441 processor_set_t pset
= PROCESSOR_SET_NULL
;
444 while (!r
&& IP_VALID(port
)) {
446 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
453 * Routine: convert_port_to_pset_name
455 * Convert from a port to a pset.
456 * Doesn't consume the port ref; produces a pset ref,
463 convert_port_to_pset_name(
467 processor_set_t pset
= PROCESSOR_SET_NULL
;
470 while (!r
&& IP_VALID(port
)) {
472 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
479 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
481 processor_set_t pset
;
483 pset
= PROCESSOR_SET_NULL
;
484 if (ip_active(port
) &&
485 ((ip_kotype(port
) == IKOT_PSET
) ||
486 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
487 pset
= (processor_set_t
) port
->ip_kobject
;
488 if (!pset_lock_try(pset
)) {
502 * Routine: convert_host_to_port
504 * Convert from a host to a port.
505 * Produces a naked send right which may be invalid.
511 convert_host_to_port(
516 host_get_host_port(host
, &port
);
521 * Routine: convert_processor_to_port
523 * Convert from a processor to a port.
524 * Produces a naked send right which may be invalid.
530 convert_processor_to_port(
531 processor_t processor
)
537 processor_lock(processor
);
539 if (processor
->processor_self
!= IP_NULL
)
540 port
= ipc_port_make_send(processor
->processor_self
);
544 processor_unlock(processor
);
551 * Routine: convert_pset_to_port
553 * Convert from a pset to a port.
554 * Consumes a pset ref; produces a naked send right
555 * which may be invalid.
561 convert_pset_to_port(
562 processor_set_t pset
)
568 port
= ipc_port_make_send(pset
->pset_self
);
573 pset_deallocate(pset
);
578 * Routine: convert_pset_name_to_port
580 * Convert from a pset to a port.
581 * Consumes a pset ref; produces a naked send right
582 * which may be invalid.
588 convert_pset_name_to_port(
589 processor_set_name_t pset
)
595 port
= ipc_port_make_send(pset
->pset_name_self
);
600 pset_deallocate(pset
);
605 * Routine: convert_port_to_host_security
607 * Convert from a port to a host security.
608 * Doesn't consume the port ref; the port produced may be null.
614 convert_port_to_host_security(
617 host_t host
= HOST_NULL
;
619 if (IP_VALID(port
)) {
621 if (ip_active(port
) &&
622 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
623 host
= (host_t
) port
->ip_kobject
;
631 * Routine: host_set_exception_ports [kernel call]
633 * Sets the host exception port, flavor and
634 * behavior for the exception types specified by the mask.
635 * There will be one send right per exception per valid
638 * Nothing locked. If successful, consumes
639 * the supplied send right.
641 * KERN_SUCCESS Changed the special port.
642 * KERN_INVALID_ARGUMENT The host_priv is not valid,
643 * Illegal mask bit set.
644 * Illegal exception behavior
647 host_set_exception_ports(
648 host_priv_t host_priv
,
649 exception_mask_t exception_mask
,
651 exception_behavior_t new_behavior
,
652 thread_state_flavor_t new_flavor
)
655 ipc_port_t old_port
[EXC_TYPES_COUNT
];
657 if (host_priv
== HOST_PRIV_NULL
) {
658 return KERN_INVALID_ARGUMENT
;
661 assert(host_priv
== &realhost
);
663 if (exception_mask
& ~EXC_MASK_ALL
) {
664 return KERN_INVALID_ARGUMENT
;
667 if (IP_VALID(new_port
)) {
668 switch (new_behavior
) {
669 case EXCEPTION_DEFAULT
:
670 case EXCEPTION_STATE
:
671 case EXCEPTION_STATE_IDENTITY
:
674 return KERN_INVALID_ARGUMENT
;
677 /* Cannot easily check "new_flavor", but that just means that
678 * the flavor in the generated exception message might be garbage:
681 host_lock(host_priv
);
683 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
684 if (exception_mask
& (1 << i
)) {
685 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
686 host_priv
->exc_actions
[i
].port
=
687 ipc_port_copy_send(new_port
);
688 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
689 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
691 old_port
[i
] = IP_NULL
;
695 * Consume send rights without any lock held.
697 host_unlock(host_priv
);
698 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
699 if (IP_VALID(old_port
[i
]))
700 ipc_port_release_send(old_port
[i
]);
701 if (IP_VALID(new_port
)) /* consume send right */
702 ipc_port_release_send(new_port
);
708 * Routine: host_get_exception_ports [kernel call]
710 * Clones a send right for each of the host's exception
711 * ports specified in the mask and returns the behaviour
712 * and flavor of said port.
714 * Returns upto [in} CountCnt elements.
719 * KERN_SUCCESS Extracted a send right.
720 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
721 * Invalid special port,
722 * Illegal mask bit set.
723 * KERN_FAILURE The thread is dead.
726 host_get_exception_ports(
727 host_priv_t host_priv
,
728 exception_mask_t exception_mask
,
729 exception_mask_array_t masks
,
730 mach_msg_type_number_t
* CountCnt
,
731 exception_port_array_t ports
,
732 exception_behavior_array_t behaviors
,
733 thread_state_flavor_array_t flavors
)
735 unsigned int i
, j
, count
;
737 if (host_priv
== HOST_PRIV_NULL
)
738 return KERN_INVALID_ARGUMENT
;
740 if (exception_mask
& ~EXC_MASK_ALL
) {
741 return KERN_INVALID_ARGUMENT
;
744 assert (host_priv
== &realhost
);
746 host_lock(host_priv
);
750 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
751 if (exception_mask
& (1 << i
)) {
752 for (j
= 0; j
< count
; j
++) {
754 * search for an identical entry, if found
755 * set corresponding mask for this exception.
757 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
758 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
759 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
761 masks
[j
] |= (1 << i
);
768 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
769 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
770 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
772 if (count
> *CountCnt
) {
778 host_unlock(host_priv
);
785 host_swap_exception_ports(
786 host_priv_t host_priv
,
787 exception_mask_t exception_mask
,
789 exception_behavior_t new_behavior
,
790 thread_state_flavor_t new_flavor
,
791 exception_mask_array_t masks
,
792 mach_msg_type_number_t
* CountCnt
,
793 exception_port_array_t ports
,
794 exception_behavior_array_t behaviors
,
795 thread_state_flavor_array_t flavors
)
800 ipc_port_t old_port
[EXC_TYPES_COUNT
];
802 if (host_priv
== HOST_PRIV_NULL
)
803 return KERN_INVALID_ARGUMENT
;
805 if (exception_mask
& ~EXC_MASK_ALL
) {
806 return KERN_INVALID_ARGUMENT
;
809 if (IP_VALID(new_port
)) {
810 switch (new_behavior
) {
811 case EXCEPTION_DEFAULT
:
812 case EXCEPTION_STATE
:
813 case EXCEPTION_STATE_IDENTITY
:
816 return KERN_INVALID_ARGUMENT
;
819 /* Cannot easily check "new_flavor", but that just means that
820 * the flavor in the generated exception message might be garbage:
823 host_lock(host_priv
);
827 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
828 if (exception_mask
& (1 << i
)) {
829 for (j
= 0; j
< count
; j
++) {
831 * search for an identical entry, if found
832 * set corresponding mask for this exception.
834 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
835 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
836 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
838 masks
[j
] |= (1 << i
);
845 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
846 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
847 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
850 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
851 host_priv
->exc_actions
[i
].port
=
852 ipc_port_copy_send(new_port
);
853 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
854 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
855 if (count
> *CountCnt
) {
859 old_port
[i
] = IP_NULL
;
861 host_unlock(host_priv
);
864 * Consume send rights without any lock held.
866 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
867 if (IP_VALID(old_port
[i
]))
868 ipc_port_release_send(old_port
[i
]);
869 if (IP_VALID(new_port
)) /* consume send right */
870 ipc_port_release_send(new_port
);