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/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
);
109 realhost
.host_self
= port
;
111 port
= ipc_port_alloc_kernel();
113 panic("ipc_host_init");
115 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_PRIV
);
116 realhost
.host_priv_self
= port
;
118 port
= ipc_port_alloc_kernel();
120 panic("ipc_host_init");
122 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_SECURITY
);
123 realhost
.host_security_self
= port
;
125 realhost
.io_master
= IP_NULL
;
127 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
128 realhost
.exc_actions
[i
].port
= IP_NULL
;
132 * Set up ipc for default processor set.
134 ipc_pset_init(&default_pset
);
135 ipc_pset_enable(&default_pset
);
138 * And for master processor
140 ipc_processor_init(master_processor
);
141 ipc_processor_enable(master_processor
);
145 * Routine: host_self_trap [mach trap]
147 * Give the caller send rights for his own host port.
151 * MACH_PORT_NULL if there are any resource failures
160 sright
= ipc_port_copy_send(current_task()->itk_host
);
161 return ipc_port_copyout_send(sright
, current_space());
165 * ipc_processor_init:
167 * Initialize ipc access to processor by allocating port.
172 processor_t processor
)
176 port
= ipc_port_alloc_kernel();
178 panic("ipc_processor_init");
179 processor
->processor_self
= port
;
183 * ipc_processor_enable:
185 * Enable ipc control of processor by setting port object.
188 ipc_processor_enable(
189 processor_t processor
)
193 myport
= processor
->processor_self
;
194 ipc_kobject_set(myport
, (ipc_kobject_t
) processor
, IKOT_PROCESSOR
);
198 * ipc_processor_disable:
200 * Disable ipc control of processor by clearing port object.
203 ipc_processor_disable(
204 processor_t processor
)
208 myport
= processor
->processor_self
;
209 if (myport
== IP_NULL
)
211 ipc_kobject_set(myport
, IKO_NULL
, IKOT_NONE
);
215 * ipc_processor_terminate:
217 * Processor is off-line. Destroy ipc control port.
220 ipc_processor_terminate(
221 processor_t processor
)
227 processor_lock(processor
);
228 myport
= processor
->processor_self
;
229 if (myport
== IP_NULL
) {
230 processor_unlock(processor
);
235 processor
->processor_self
= IP_NULL
;
236 processor_unlock(processor
);
239 ipc_port_dealloc_kernel(myport
);
245 * Initialize ipc control of a processor set by allocating its ports.
250 processor_set_t pset
)
254 port
= ipc_port_alloc_kernel();
256 panic("ipc_pset_init");
257 pset
->pset_self
= port
;
259 port
= ipc_port_alloc_kernel();
261 panic("ipc_pset_init");
262 pset
->pset_name_self
= port
;
268 * Enable ipc access to a processor set.
272 processor_set_t pset
)
276 ipc_kobject_set(pset
->pset_self
,
277 (ipc_kobject_t
) pset
, IKOT_PSET
);
278 ipc_kobject_set(pset
->pset_name_self
,
279 (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
280 pset
->ref_count
+= 2;
288 * Disable ipc access to a processor set by clearing the port objects.
289 * Caller must hold pset lock and a reference to the pset. Ok to
290 * just decrement pset reference count as a result.
294 processor_set_t pset
)
296 ipc_kobject_set(pset
->pset_self
, IKO_NULL
, IKOT_NONE
);
297 ipc_kobject_set(pset
->pset_name_self
, IKO_NULL
, IKOT_NONE
);
298 pset
->ref_count
-= 2;
302 * ipc_pset_terminate:
304 * Processor set is dead. Deallocate the ipc control structures.
308 processor_set_t pset
)
310 ipc_port_dealloc_kernel(pset
->pset_self
);
311 ipc_port_dealloc_kernel(pset
->pset_name_self
);
315 * processor_set_default, processor_set_default_priv:
317 * Return ports for manipulating default_processor set. MiG code
318 * differentiates between these two routines.
321 processor_set_default(
323 processor_set_t
*pset
)
325 if (host
== HOST_NULL
)
326 return(KERN_INVALID_ARGUMENT
);
328 *pset
= &default_pset
;
329 pset_reference(*pset
);
330 return(KERN_SUCCESS
);
334 * Routine: convert_port_to_host
336 * Convert from a port to a host.
337 * Doesn't consume the port ref; the host produced may be null.
343 convert_port_to_host(
346 host_t host
= HOST_NULL
;
348 if (IP_VALID(port
)) {
350 if (ip_active(port
) &&
351 ((ip_kotype(port
) == IKOT_HOST
) ||
352 (ip_kotype(port
) == IKOT_HOST_PRIV
)
354 host
= (host_t
) port
->ip_kobject
;
362 * Routine: convert_port_to_host_priv
364 * Convert from a port to a host.
365 * Doesn't consume the port ref; the host produced may be null.
371 convert_port_to_host_priv(
374 host_t host
= HOST_NULL
;
376 if (IP_VALID(port
)) {
378 if (ip_active(port
) &&
379 (ip_kotype(port
) == IKOT_HOST_PRIV
))
380 host
= (host_t
) port
->ip_kobject
;
388 * Routine: convert_port_to_processor
390 * Convert from a port to a processor.
391 * Doesn't consume the port ref;
392 * the processor produced may be null.
398 convert_port_to_processor(
401 processor_t processor
= PROCESSOR_NULL
;
403 if (IP_VALID(port
)) {
405 if (ip_active(port
) &&
406 (ip_kotype(port
) == IKOT_PROCESSOR
))
407 processor
= (processor_t
) port
->ip_kobject
;
415 * Routine: convert_port_to_pset
417 * Convert from a port to a pset.
418 * Doesn't consume the port ref; produces a pset ref,
425 convert_port_to_pset(
429 processor_set_t pset
= PROCESSOR_SET_NULL
;
432 while (!r
&& IP_VALID(port
)) {
434 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
441 * Routine: convert_port_to_pset_name
443 * Convert from a port to a pset.
444 * Doesn't consume the port ref; produces a pset ref,
451 convert_port_to_pset_name(
455 processor_set_t pset
= PROCESSOR_SET_NULL
;
458 while (!r
&& IP_VALID(port
)) {
460 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
467 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
469 processor_set_t pset
;
471 pset
= PROCESSOR_SET_NULL
;
472 if (ip_active(port
) &&
473 ((ip_kotype(port
) == IKOT_PSET
) ||
474 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
475 pset
= (processor_set_t
) port
->ip_kobject
;
476 if (!pset_lock_try(pset
)) {
490 * Routine: convert_host_to_port
492 * Convert from a host to a port.
493 * Produces a naked send right which may be invalid.
499 convert_host_to_port(
504 port
= ipc_port_make_send(host
->host_self
);
510 * Routine: convert_processor_to_port
512 * Convert from a processor to a port.
513 * Produces a naked send right which may be invalid.
519 convert_processor_to_port(
520 processor_t processor
)
526 processor_lock(processor
);
528 if (processor
->processor_self
!= IP_NULL
)
529 port
= ipc_port_make_send(processor
->processor_self
);
533 processor_unlock(processor
);
540 * Routine: convert_pset_to_port
542 * Convert from a pset to a port.
543 * Consumes a pset ref; produces a naked send right
544 * which may be invalid.
550 convert_pset_to_port(
551 processor_set_t pset
)
557 port
= ipc_port_make_send(pset
->pset_self
);
562 pset_deallocate(pset
);
567 * Routine: convert_pset_name_to_port
569 * Convert from a pset to a port.
570 * Consumes a pset ref; produces a naked send right
571 * which may be invalid.
577 convert_pset_name_to_port(
578 processor_set_name_t pset
)
584 port
= ipc_port_make_send(pset
->pset_name_self
);
589 pset_deallocate(pset
);
594 * Routine: convert_port_to_host_security
596 * Convert from a port to a host security.
597 * Doesn't consume the port ref; the port produced may be null.
603 convert_port_to_host_security(
606 host_t host
= HOST_NULL
;
608 if (IP_VALID(port
)) {
610 if (ip_active(port
) &&
611 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
612 host
= (host_t
) port
->ip_kobject
;
620 * Routine: host_set_exception_ports [kernel call]
622 * Sets the host exception port, flavor and
623 * behavior for the exception types specified by the mask.
624 * There will be one send right per exception per valid
627 * Nothing locked. If successful, consumes
628 * the supplied send right.
630 * KERN_SUCCESS Changed the special port.
631 * KERN_INVALID_ARGUMENT The host_priv is not valid,
632 * Illegal mask bit set.
633 * Illegal exception behavior
636 host_set_exception_ports(
637 host_priv_t host_priv
,
638 exception_mask_t exception_mask
,
640 exception_behavior_t new_behavior
,
641 thread_state_flavor_t new_flavor
)
644 ipc_port_t old_port
[EXC_TYPES_COUNT
];
646 if (host_priv
== HOST_PRIV_NULL
) {
647 return KERN_INVALID_ARGUMENT
;
650 assert(host_priv
== &realhost
);
652 if (exception_mask
& ~EXC_MASK_ALL
) {
653 return KERN_INVALID_ARGUMENT
;
656 if (IP_VALID(new_port
)) {
657 switch (new_behavior
) {
658 case EXCEPTION_DEFAULT
:
659 case EXCEPTION_STATE
:
660 case EXCEPTION_STATE_IDENTITY
:
663 return KERN_INVALID_ARGUMENT
;
666 /* Cannot easily check "new_flavor", but that just means that
667 * the flavor in the generated exception message might be garbage:
670 host_lock(host_priv
);
672 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
673 if (exception_mask
& (1 << i
)) {
674 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
675 host_priv
->exc_actions
[i
].port
=
676 ipc_port_copy_send(new_port
);
677 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
678 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
680 old_port
[i
] = IP_NULL
;
684 * Consume send rights without any lock held.
686 host_unlock(host_priv
);
687 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
688 if (IP_VALID(old_port
[i
]))
689 ipc_port_release_send(old_port
[i
]);
690 if (IP_VALID(new_port
)) /* consume send right */
691 ipc_port_release_send(new_port
);
697 * Routine: host_get_exception_ports [kernel call]
699 * Clones a send right for each of the host's exception
700 * ports specified in the mask and returns the behaviour
701 * and flavor of said port.
703 * Returns upto [in} CountCnt elements.
708 * KERN_SUCCESS Extracted a send right.
709 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
710 * Invalid special port,
711 * Illegal mask bit set.
712 * KERN_FAILURE The thread is dead.
715 host_get_exception_ports(
716 host_priv_t host_priv
,
717 exception_mask_t exception_mask
,
718 exception_mask_array_t masks
,
719 mach_msg_type_number_t
* CountCnt
,
720 exception_port_array_t ports
,
721 exception_behavior_array_t behaviors
,
722 thread_state_flavor_array_t flavors
)
728 if (host_priv
== HOST_PRIV_NULL
)
729 return KERN_INVALID_ARGUMENT
;
731 if (exception_mask
& ~EXC_MASK_ALL
) {
732 return KERN_INVALID_ARGUMENT
;
735 assert (host_priv
== &realhost
);
737 host_lock(host_priv
);
741 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
742 if (exception_mask
& (1 << i
)) {
743 for (j
= 0; j
< count
; j
++) {
745 * search for an identical entry, if found
746 * set corresponding mask for this exception.
748 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
749 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
750 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
752 masks
[j
] |= (1 << i
);
759 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
760 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
761 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
763 if (count
> *CountCnt
) {
769 host_unlock(host_priv
);
776 host_swap_exception_ports(
777 host_priv_t host_priv
,
778 exception_mask_t exception_mask
,
780 exception_behavior_t new_behavior
,
781 thread_state_flavor_t new_flavor
,
782 exception_mask_array_t masks
,
783 mach_msg_type_number_t
* CountCnt
,
784 exception_port_array_t ports
,
785 exception_behavior_array_t behaviors
,
786 thread_state_flavor_array_t flavors
)
791 ipc_port_t old_port
[EXC_TYPES_COUNT
];
793 if (host_priv
== HOST_PRIV_NULL
)
794 return KERN_INVALID_ARGUMENT
;
796 if (exception_mask
& ~EXC_MASK_ALL
) {
797 return KERN_INVALID_ARGUMENT
;
800 if (IP_VALID(new_port
)) {
801 switch (new_behavior
) {
802 case EXCEPTION_DEFAULT
:
803 case EXCEPTION_STATE
:
804 case EXCEPTION_STATE_IDENTITY
:
807 return KERN_INVALID_ARGUMENT
;
810 /* Cannot easily check "new_flavor", but that just means that
811 * the flavor in the generated exception message might be garbage:
814 host_lock(host_priv
);
818 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
819 if (exception_mask
& (1 << i
)) {
820 for (j
= 0; j
< count
; j
++) {
822 * search for an identical entry, if found
823 * set corresponding mask for this exception.
825 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
826 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
827 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
829 masks
[j
] |= (1 << i
);
836 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
837 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
838 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
841 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
842 host_priv
->exc_actions
[i
].port
=
843 ipc_port_copy_send(new_port
);
844 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
845 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
846 if (count
> *CountCnt
) {
850 old_port
[i
] = IP_NULL
;
852 host_unlock(host_priv
);
855 * Consume send rights without any lock held.
857 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
858 if (IP_VALID(old_port
[i
]))
859 ipc_port_release_send(old_port
[i
]);
860 if (IP_VALID(new_port
)) /* consume send right */
861 ipc_port_release_send(new_port
);