2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
59 * Routines to implement host ports.
61 #include <mach/message.h>
62 #include <mach/mach_traps.h>
63 #include <mach/etap_events.h>
64 #include <mach/mach_host_server.h>
65 #include <kern/host.h>
66 #include <kern/processor.h>
67 #include <kern/lock.h>
68 #include <kern/task.h>
69 #include <kern/thread.h>
70 #include <kern/ipc_host.h>
71 #include <kern/ipc_kobject.h>
72 #include <kern/misc_protos.h>
74 #include <ipc/ipc_port.h>
75 #include <ipc/ipc_space.h>
78 * Forward declarations
82 ipc_processor_terminate(
83 processor_t processor
);
86 ipc_processor_disable(
87 processor_t processor
);
91 ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
);
94 * ipc_host_init: set up various things.
97 void ipc_host_init(void)
102 mutex_init(&realhost
.lock
, ETAP_MISC_MASTER
);
105 * Allocate and set up the two host ports.
107 port
= ipc_port_alloc_kernel();
109 panic("ipc_host_init");
111 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST
);
112 realhost
.host_self
= port
;
114 port
= ipc_port_alloc_kernel();
116 panic("ipc_host_init");
118 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_PRIV
);
119 realhost
.host_priv_self
= port
;
121 port
= ipc_port_alloc_kernel();
123 panic("ipc_host_init");
125 ipc_kobject_set(port
, (ipc_kobject_t
) &realhost
, IKOT_HOST_SECURITY
);
126 realhost
.host_security_self
= port
;
128 realhost
.io_master
= IP_NULL
;
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 port
= ipc_port_make_send(host
->host_self
);
513 * Routine: convert_processor_to_port
515 * Convert from a processor to a port.
516 * Produces a naked send right which may be invalid.
522 convert_processor_to_port(
523 processor_t processor
)
529 processor_lock(processor
);
531 if (processor
->processor_self
!= IP_NULL
)
532 port
= ipc_port_make_send(processor
->processor_self
);
536 processor_unlock(processor
);
543 * Routine: convert_pset_to_port
545 * Convert from a pset to a port.
546 * Consumes a pset ref; produces a naked send right
547 * which may be invalid.
553 convert_pset_to_port(
554 processor_set_t pset
)
560 port
= ipc_port_make_send(pset
->pset_self
);
565 pset_deallocate(pset
);
570 * Routine: convert_pset_name_to_port
572 * Convert from a pset to a port.
573 * Consumes a pset ref; produces a naked send right
574 * which may be invalid.
580 convert_pset_name_to_port(
581 processor_set_name_t pset
)
587 port
= ipc_port_make_send(pset
->pset_name_self
);
592 pset_deallocate(pset
);
597 * Routine: convert_port_to_host_security
599 * Convert from a port to a host security.
600 * Doesn't consume the port ref; the port produced may be null.
606 convert_port_to_host_security(
609 host_t host
= HOST_NULL
;
611 if (IP_VALID(port
)) {
613 if (ip_active(port
) &&
614 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
615 host
= (host_t
) port
->ip_kobject
;
623 * Routine: host_set_exception_ports [kernel call]
625 * Sets the host exception port, flavor and
626 * behavior for the exception types specified by the mask.
627 * There will be one send right per exception per valid
630 * Nothing locked. If successful, consumes
631 * the supplied send right.
633 * KERN_SUCCESS Changed the special port.
634 * KERN_INVALID_ARGUMENT The host_priv is not valid,
635 * Illegal mask bit set.
636 * Illegal exception behavior
639 host_set_exception_ports(
640 host_priv_t host_priv
,
641 exception_mask_t exception_mask
,
643 exception_behavior_t new_behavior
,
644 thread_state_flavor_t new_flavor
)
647 ipc_port_t old_port
[EXC_TYPES_COUNT
];
649 if (host_priv
== HOST_PRIV_NULL
) {
650 return KERN_INVALID_ARGUMENT
;
653 assert(host_priv
== &realhost
);
655 if (exception_mask
& ~EXC_MASK_ALL
) {
656 return KERN_INVALID_ARGUMENT
;
659 if (IP_VALID(new_port
)) {
660 switch (new_behavior
) {
661 case EXCEPTION_DEFAULT
:
662 case EXCEPTION_STATE
:
663 case EXCEPTION_STATE_IDENTITY
:
666 return KERN_INVALID_ARGUMENT
;
669 /* Cannot easily check "new_flavor", but that just means that
670 * the flavor in the generated exception message might be garbage:
673 host_lock(host_priv
);
675 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
676 if (exception_mask
& (1 << i
)) {
677 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
678 host_priv
->exc_actions
[i
].port
=
679 ipc_port_copy_send(new_port
);
680 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
681 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
683 old_port
[i
] = IP_NULL
;
687 * Consume send rights without any lock held.
689 host_unlock(host_priv
);
690 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
691 if (IP_VALID(old_port
[i
]))
692 ipc_port_release_send(old_port
[i
]);
693 if (IP_VALID(new_port
)) /* consume send right */
694 ipc_port_release_send(new_port
);
700 * Routine: host_get_exception_ports [kernel call]
702 * Clones a send right for each of the host's exception
703 * ports specified in the mask and returns the behaviour
704 * and flavor of said port.
706 * Returns upto [in} CountCnt elements.
711 * KERN_SUCCESS Extracted a send right.
712 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
713 * Invalid special port,
714 * Illegal mask bit set.
715 * KERN_FAILURE The thread is dead.
718 host_get_exception_ports(
719 host_priv_t host_priv
,
720 exception_mask_t exception_mask
,
721 exception_mask_array_t masks
,
722 mach_msg_type_number_t
* CountCnt
,
723 exception_port_array_t ports
,
724 exception_behavior_array_t behaviors
,
725 thread_state_flavor_array_t flavors
)
731 if (host_priv
== HOST_PRIV_NULL
)
732 return KERN_INVALID_ARGUMENT
;
734 if (exception_mask
& ~EXC_MASK_ALL
) {
735 return KERN_INVALID_ARGUMENT
;
738 assert (host_priv
== &realhost
);
740 host_lock(host_priv
);
744 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
745 if (exception_mask
& (1 << i
)) {
746 for (j
= 0; j
< count
; j
++) {
748 * search for an identical entry, if found
749 * set corresponding mask for this exception.
751 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
752 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
753 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
755 masks
[j
] |= (1 << i
);
762 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
763 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
764 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
766 if (count
> *CountCnt
) {
772 host_unlock(host_priv
);
779 host_swap_exception_ports(
780 host_priv_t host_priv
,
781 exception_mask_t exception_mask
,
783 exception_behavior_t new_behavior
,
784 thread_state_flavor_t new_flavor
,
785 exception_mask_array_t masks
,
786 mach_msg_type_number_t
* CountCnt
,
787 exception_port_array_t ports
,
788 exception_behavior_array_t behaviors
,
789 thread_state_flavor_array_t flavors
)
794 ipc_port_t old_port
[EXC_TYPES_COUNT
];
796 if (host_priv
== HOST_PRIV_NULL
)
797 return KERN_INVALID_ARGUMENT
;
799 if (exception_mask
& ~EXC_MASK_ALL
) {
800 return KERN_INVALID_ARGUMENT
;
803 if (IP_VALID(new_port
)) {
804 switch (new_behavior
) {
805 case EXCEPTION_DEFAULT
:
806 case EXCEPTION_STATE
:
807 case EXCEPTION_STATE_IDENTITY
:
810 return KERN_INVALID_ARGUMENT
;
813 /* Cannot easily check "new_flavor", but that just means that
814 * the flavor in the generated exception message might be garbage:
817 host_lock(host_priv
);
821 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
822 if (exception_mask
& (1 << i
)) {
823 for (j
= 0; j
< count
; j
++) {
825 * search for an identical entry, if found
826 * set corresponding mask for this exception.
828 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
829 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
830 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
832 masks
[j
] |= (1 << i
);
839 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
840 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
841 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
844 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
845 host_priv
->exc_actions
[i
].port
=
846 ipc_port_copy_send(new_port
);
847 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
848 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
849 if (count
> *CountCnt
) {
853 old_port
[i
] = IP_NULL
;
855 host_unlock(host_priv
);
858 * Consume send rights without any lock held.
860 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
861 if (IP_VALID(old_port
[i
]))
862 ipc_port_release_send(old_port
[i
]);
863 if (IP_VALID(new_port
)) /* consume send right */
864 ipc_port_release_send(new_port
);