2 * Copyright (c) 2000 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 <kern/host.h>
62 #include <kern/processor.h>
63 #include <kern/task.h>
64 #include <kern/thread.h>
65 #include <kern/ipc_host.h>
66 #include <kern/ipc_kobject.h>
67 #include <kern/misc_protos.h>
69 #include <ipc/ipc_port.h>
70 #include <ipc/ipc_space.h>
73 * Forward declarations
77 ipc_processor_terminate(
78 processor_t processor
);
81 ipc_processor_disable(
82 processor_t processor
);
86 ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
);
89 * ipc_host_init: set up various things.
92 void ipc_host_init(void)
98 * Allocate and set up the two host ports.
100 port
= ipc_port_alloc_kernel();
102 panic("ipc_host_init");
104 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST
);
105 realhost
.host_self
= port
;
107 port
= ipc_port_alloc_kernel();
109 panic("ipc_host_init");
111 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_PRIV
);
112 realhost
.host_priv_self
= port
;
114 port
= ipc_port_alloc_kernel();
116 panic("ipc_host_init");
118 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_SECURITY
);
119 realhost
.host_security_self
= port
;
121 realhost
.io_master
= IP_NULL
;
123 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
124 realhost
.exc_actions
[i
].port
= IP_NULL
;
128 * Set up ipc for default processor set.
130 ipc_pset_init(&default_pset
);
131 ipc_pset_enable(&default_pset
);
134 * And for master processor
136 ipc_processor_init(master_processor
);
137 ipc_processor_enable(master_processor
);
141 * Routine: host_self_trap [mach trap]
143 * Give the caller send rights for his own host port.
147 * MACH_PORT_NULL if there are any resource failures
156 sright
= ipc_port_copy_send(current_task()->itk_host
);
157 return ipc_port_copyout_send(sright
, current_space());
161 * ipc_processor_init:
163 * Initialize ipc access to processor by allocating port.
168 processor_t processor
)
172 port
= ipc_port_alloc_kernel();
174 panic("ipc_processor_init");
175 processor
->processor_self
= port
;
179 * ipc_processor_enable:
181 * Enable ipc control of processor by setting port object.
184 ipc_processor_enable(
185 processor_t processor
)
189 myport
= processor
->processor_self
;
190 ipc_kobject_set(myport
, (ipc_kobject_t
) processor
, IKOT_PROCESSOR
);
194 * ipc_processor_disable:
196 * Disable ipc control of processor by clearing port object.
199 ipc_processor_disable(
200 processor_t processor
)
204 myport
= processor
->processor_self
;
205 if (myport
== IP_NULL
)
207 ipc_kobject_set(myport
, IKO_NULL
, IKOT_NONE
);
211 * ipc_processor_terminate:
213 * Processor is off-line. Destroy ipc control port.
216 ipc_processor_terminate(
217 processor_t processor
)
223 processor_lock(processor
);
224 myport
= processor
->processor_self
;
225 if (myport
== IP_NULL
) {
226 processor_unlock(processor
);
231 processor
->processor_self
= IP_NULL
;
232 processor_unlock(processor
);
235 ipc_port_dealloc_kernel(myport
);
241 * Initialize ipc control of a processor set by allocating its ports.
246 processor_set_t pset
)
250 port
= ipc_port_alloc_kernel();
252 panic("ipc_pset_init");
253 pset
->pset_self
= port
;
255 port
= ipc_port_alloc_kernel();
257 panic("ipc_pset_init");
258 pset
->pset_name_self
= port
;
264 * Enable ipc access to a processor set.
268 processor_set_t pset
)
272 ipc_kobject_set(pset
->pset_self
,
273 (ipc_kobject_t
) pset
, IKOT_PSET
);
274 ipc_kobject_set(pset
->pset_name_self
,
275 (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
276 pset
->ref_count
+= 2;
284 * Disable ipc access to a processor set by clearing the port objects.
285 * Caller must hold pset lock and a reference to the pset. Ok to
286 * just decrement pset reference count as a result.
290 processor_set_t pset
)
292 ipc_kobject_set(pset
->pset_self
, IKO_NULL
, IKOT_NONE
);
293 ipc_kobject_set(pset
->pset_name_self
, IKO_NULL
, IKOT_NONE
);
294 pset
->ref_count
-= 2;
298 * ipc_pset_terminate:
300 * Processor set is dead. Deallocate the ipc control structures.
304 processor_set_t pset
)
306 ipc_port_dealloc_kernel(pset
->pset_self
);
307 ipc_port_dealloc_kernel(pset
->pset_name_self
);
311 * processor_set_default, processor_set_default_priv:
313 * Return ports for manipulating default_processor set. MiG code
314 * differentiates between these two routines.
317 processor_set_default(
319 processor_set_t
*pset
)
321 if (host
== HOST_NULL
)
322 return(KERN_INVALID_ARGUMENT
);
324 *pset
= &default_pset
;
325 pset_reference(*pset
);
326 return(KERN_SUCCESS
);
330 * Routine: convert_port_to_host
332 * Convert from a port to a host.
333 * Doesn't consume the port ref; the host produced may be null.
339 convert_port_to_host(
342 host_t host
= HOST_NULL
;
344 if (IP_VALID(port
)) {
346 if (ip_active(port
) &&
347 ((ip_kotype(port
) == IKOT_HOST
) ||
348 (ip_kotype(port
) == IKOT_HOST_PRIV
)
350 host
= (host_t
) port
->ip_kobject
;
358 * Routine: convert_port_to_host_priv
360 * Convert from a port to a host.
361 * Doesn't consume the port ref; the host produced may be null.
367 convert_port_to_host_priv(
370 host_t host
= HOST_NULL
;
372 if (IP_VALID(port
)) {
374 if (ip_active(port
) &&
375 (ip_kotype(port
) == IKOT_HOST_PRIV
))
376 host
= (host_t
) port
->ip_kobject
;
384 * Routine: convert_port_to_processor
386 * Convert from a port to a processor.
387 * Doesn't consume the port ref;
388 * the processor produced may be null.
394 convert_port_to_processor(
397 processor_t processor
= PROCESSOR_NULL
;
399 if (IP_VALID(port
)) {
401 if (ip_active(port
) &&
402 (ip_kotype(port
) == IKOT_PROCESSOR
))
403 processor
= (processor_t
) port
->ip_kobject
;
411 * Routine: convert_port_to_pset
413 * Convert from a port to a pset.
414 * Doesn't consume the port ref; produces a pset ref,
421 convert_port_to_pset(
425 processor_set_t pset
= PROCESSOR_SET_NULL
;
428 while (!r
&& IP_VALID(port
)) {
430 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
437 * Routine: convert_port_to_pset_name
439 * Convert from a port to a pset.
440 * Doesn't consume the port ref; produces a pset ref,
447 convert_port_to_pset_name(
451 processor_set_t pset
= PROCESSOR_SET_NULL
;
454 while (!r
&& IP_VALID(port
)) {
456 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
463 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
465 processor_set_t pset
;
467 pset
= PROCESSOR_SET_NULL
;
468 if (ip_active(port
) &&
469 ((ip_kotype(port
) == IKOT_PSET
) ||
470 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
471 pset
= (processor_set_t
) port
->ip_kobject
;
472 if (!pset_lock_try(pset
)) {
486 * Routine: convert_host_to_port
488 * Convert from a host to a port.
489 * Produces a naked send right which may be invalid.
495 convert_host_to_port(
500 port
= ipc_port_make_send(host
->host_self
);
506 * Routine: convert_processor_to_port
508 * Convert from a processor to a port.
509 * Produces a naked send right which may be invalid.
515 convert_processor_to_port(
516 processor_t processor
)
522 processor_lock(processor
);
524 if (processor
->processor_self
!= IP_NULL
)
525 port
= ipc_port_make_send(processor
->processor_self
);
529 processor_unlock(processor
);
536 * Routine: convert_pset_to_port
538 * Convert from a pset to a port.
539 * Consumes a pset ref; produces a naked send right
540 * which may be invalid.
546 convert_pset_to_port(
547 processor_set_t pset
)
553 port
= ipc_port_make_send(pset
->pset_self
);
558 pset_deallocate(pset
);
563 * Routine: convert_pset_name_to_port
565 * Convert from a pset to a port.
566 * Consumes a pset ref; produces a naked send right
567 * which may be invalid.
573 convert_pset_name_to_port(
574 processor_set_name_t pset
)
580 port
= ipc_port_make_send(pset
->pset_name_self
);
585 pset_deallocate(pset
);
590 * Routine: convert_port_to_host_security
592 * Convert from a port to a host security.
593 * Doesn't consume the port ref; the port produced may be null.
599 convert_port_to_host_security(
602 host_t host
= HOST_NULL
;
604 if (IP_VALID(port
)) {
606 if (ip_active(port
) &&
607 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
608 host
= (host_t
) port
->ip_kobject
;
616 * Routine: host_set_exception_ports [kernel call]
618 * Sets the host exception port, flavor and
619 * behavior for the exception types specified by the mask.
620 * There will be one send right per exception per valid
623 * Nothing locked. If successful, consumes
624 * the supplied send right.
626 * KERN_SUCCESS Changed the special port.
627 * KERN_INVALID_ARGUMENT The host_priv is not valid,
628 * Illegal mask bit set.
629 * Illegal exception behavior
632 host_set_exception_ports(
633 host_priv_t host_priv
,
634 exception_mask_t exception_mask
,
636 exception_behavior_t new_behavior
,
637 thread_state_flavor_t new_flavor
)
640 ipc_port_t old_port
[EXC_TYPES_COUNT
];
642 if (host_priv
== HOST_PRIV_NULL
) {
643 return KERN_INVALID_ARGUMENT
;
646 assert(host_priv
== &realhost
);
648 if (exception_mask
& ~EXC_MASK_ALL
) {
649 return KERN_INVALID_ARGUMENT
;
652 if (IP_VALID(new_port
)) {
653 switch (new_behavior
) {
654 case EXCEPTION_DEFAULT
:
655 case EXCEPTION_STATE
:
656 case EXCEPTION_STATE_IDENTITY
:
659 return KERN_INVALID_ARGUMENT
;
662 /* Cannot easily check "new_flavor", but that just means that
663 * the flavor in the generated exception message might be garbage:
666 host_lock(host_priv
);
668 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
669 if (exception_mask
& (1 << i
)) {
670 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
671 host_priv
->exc_actions
[i
].port
=
672 ipc_port_copy_send(new_port
);
673 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
674 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
676 old_port
[i
] = IP_NULL
;
680 * Consume send rights without any lock held.
682 host_unlock(host_priv
);
683 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
684 if (IP_VALID(old_port
[i
]))
685 ipc_port_release_send(old_port
[i
]);
686 if (IP_VALID(new_port
)) /* consume send right */
687 ipc_port_release_send(new_port
);
693 * Routine: host_get_exception_ports [kernel call]
695 * Clones a send right for each of the host's exception
696 * ports specified in the mask and returns the behaviour
697 * and flavor of said port.
699 * Returns upto [in} CountCnt elements.
704 * KERN_SUCCESS Extracted a send right.
705 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
706 * Invalid special port,
707 * Illegal mask bit set.
708 * KERN_FAILURE The thread is dead.
711 host_get_exception_ports(
712 host_priv_t host_priv
,
713 exception_mask_t exception_mask
,
714 exception_mask_array_t masks
,
715 mach_msg_type_number_t
* CountCnt
,
716 exception_port_array_t ports
,
717 exception_behavior_array_t behaviors
,
718 thread_state_flavor_array_t flavors
)
724 if (host_priv
== HOST_PRIV_NULL
)
725 return KERN_INVALID_ARGUMENT
;
727 if (exception_mask
& ~EXC_MASK_ALL
) {
728 return KERN_INVALID_ARGUMENT
;
731 assert (host_priv
== &realhost
);
733 host_lock(host_priv
);
737 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
738 if (exception_mask
& (1 << i
)) {
739 for (j
= 0; j
< count
; j
++) {
741 * search for an identical entry, if found
742 * set corresponding mask for this exception.
744 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
745 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
746 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
748 masks
[j
] |= (1 << i
);
755 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
756 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
757 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
759 if (count
> *CountCnt
) {
765 host_unlock(host_priv
);
772 host_swap_exception_ports(
773 host_priv_t host_priv
,
774 exception_mask_t exception_mask
,
776 exception_behavior_t new_behavior
,
777 thread_state_flavor_t new_flavor
,
778 exception_mask_array_t masks
,
779 mach_msg_type_number_t
* CountCnt
,
780 exception_port_array_t ports
,
781 exception_behavior_array_t behaviors
,
782 thread_state_flavor_array_t flavors
)
787 ipc_port_t old_port
[EXC_TYPES_COUNT
];
789 if (host_priv
== HOST_PRIV_NULL
)
790 return KERN_INVALID_ARGUMENT
;
792 if (exception_mask
& ~EXC_MASK_ALL
) {
793 return KERN_INVALID_ARGUMENT
;
796 if (IP_VALID(new_port
)) {
797 switch (new_behavior
) {
798 case EXCEPTION_DEFAULT
:
799 case EXCEPTION_STATE
:
800 case EXCEPTION_STATE_IDENTITY
:
803 return KERN_INVALID_ARGUMENT
;
806 /* Cannot easily check "new_flavor", but that just means that
807 * the flavor in the generated exception message might be garbage:
810 host_lock(host_priv
);
814 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
815 if (exception_mask
& (1 << i
)) {
816 for (j
= 0; j
< count
; j
++) {
818 * search for an identical entry, if found
819 * set corresponding mask for this exception.
821 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
822 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
823 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
825 masks
[j
] |= (1 << i
);
832 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
833 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
834 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
837 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
838 host_priv
->exc_actions
[i
].port
=
839 ipc_port_copy_send(new_port
);
840 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
841 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
842 if (count
> *CountCnt
) {
846 old_port
[i
] = IP_NULL
;
848 host_unlock(host_priv
);
851 * Consume send rights without any lock held.
853 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
854 if (IP_VALID(old_port
[i
]))
855 ipc_port_release_send(old_port
[i
]);
856 if (IP_VALID(new_port
)) /* consume send right */
857 ipc_port_release_send(new_port
);