2 * Copyright (c) 2000-2009 Apple 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
86 ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
);
89 * ipc_host_init: set up various things.
92 extern lck_grp_t host_notify_lock_grp
;
93 extern lck_attr_t host_notify_lock_attr
;
95 void ipc_host_init(void)
100 lck_mtx_init(&realhost
.lock
, &host_notify_lock_grp
, &host_notify_lock_attr
);
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(&pset0
);
139 ipc_pset_enable(&pset0
);
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
);
207 * Initialize ipc control of a processor set by allocating its ports.
212 processor_set_t pset
)
216 port
= ipc_port_alloc_kernel();
218 panic("ipc_pset_init");
219 pset
->pset_self
= port
;
221 port
= ipc_port_alloc_kernel();
223 panic("ipc_pset_init");
224 pset
->pset_name_self
= port
;
230 * Enable ipc access to a processor set.
234 processor_set_t pset
)
236 ipc_kobject_set(pset
->pset_self
, (ipc_kobject_t
) pset
, IKOT_PSET
);
237 ipc_kobject_set(pset
->pset_name_self
, (ipc_kobject_t
) pset
, IKOT_PSET_NAME
);
241 * processor_set_default:
243 * Return ports for manipulating default_processor set.
246 processor_set_default(
248 processor_set_t
*pset
)
250 if (host
== HOST_NULL
)
251 return(KERN_INVALID_ARGUMENT
);
255 return (KERN_SUCCESS
);
259 * Routine: convert_port_to_host
261 * Convert from a port to a host.
262 * Doesn't consume the port ref; the host produced may be null.
268 convert_port_to_host(
271 host_t host
= HOST_NULL
;
273 if (IP_VALID(port
)) {
275 if (ip_active(port
) &&
276 ((ip_kotype(port
) == IKOT_HOST
) ||
277 (ip_kotype(port
) == IKOT_HOST_PRIV
)
279 host
= (host_t
) port
->ip_kobject
;
287 * Routine: convert_port_to_host_priv
289 * Convert from a port to a host.
290 * Doesn't consume the port ref; the host produced may be null.
296 convert_port_to_host_priv(
299 host_t host
= HOST_NULL
;
301 if (IP_VALID(port
)) {
303 if (ip_active(port
) &&
304 (ip_kotype(port
) == IKOT_HOST_PRIV
))
305 host
= (host_t
) port
->ip_kobject
;
313 * Routine: convert_port_to_processor
315 * Convert from a port to a processor.
316 * Doesn't consume the port ref;
317 * the processor produced may be null.
323 convert_port_to_processor(
326 processor_t processor
= PROCESSOR_NULL
;
328 if (IP_VALID(port
)) {
330 if (ip_active(port
) &&
331 (ip_kotype(port
) == IKOT_PROCESSOR
))
332 processor
= (processor_t
) port
->ip_kobject
;
340 * Routine: convert_port_to_pset
342 * Convert from a port to a pset.
343 * Doesn't consume the port ref; produces a pset ref,
350 convert_port_to_pset(
354 processor_set_t pset
= PROCESSOR_SET_NULL
;
357 while (!r
&& IP_VALID(port
)) {
359 r
= ref_pset_port_locked(port
, FALSE
, &pset
);
366 * Routine: convert_port_to_pset_name
368 * Convert from a port to a pset.
369 * Doesn't consume the port ref; produces a pset ref,
376 convert_port_to_pset_name(
380 processor_set_t pset
= PROCESSOR_SET_NULL
;
383 while (!r
&& IP_VALID(port
)) {
385 r
= ref_pset_port_locked(port
, TRUE
, &pset
);
392 ref_pset_port_locked(ipc_port_t port
, boolean_t matchn
, processor_set_t
*ppset
)
394 processor_set_t pset
;
396 pset
= PROCESSOR_SET_NULL
;
397 if (ip_active(port
) &&
398 ((ip_kotype(port
) == IKOT_PSET
) ||
399 (matchn
&& (ip_kotype(port
) == IKOT_PSET_NAME
)))) {
400 pset
= (processor_set_t
) port
->ip_kobject
;
410 * Routine: convert_host_to_port
412 * Convert from a host to a port.
413 * Produces a naked send right which may be invalid.
419 convert_host_to_port(
424 host_get_host_port(host
, &port
);
429 * Routine: convert_processor_to_port
431 * Convert from a processor to a port.
432 * Produces a naked send right which may be invalid.
433 * Processors are not reference counted, so nothing to release.
439 convert_processor_to_port(
440 processor_t processor
)
442 ipc_port_t port
= processor
->processor_self
;
445 port
= ipc_port_make_send(port
);
450 * Routine: convert_pset_to_port
452 * Convert from a pset to a port.
453 * Produces a naked send right which may be invalid.
454 * Processor sets are not reference counted, so nothing to release.
460 convert_pset_to_port(
461 processor_set_t pset
)
463 ipc_port_t port
= pset
->pset_self
;
466 port
= ipc_port_make_send(port
);
472 * Routine: convert_pset_name_to_port
474 * Convert from a pset to a port.
475 * Produces a naked send right which may be invalid.
476 * Processor sets are not reference counted, so nothing to release.
482 convert_pset_name_to_port(
483 processor_set_name_t pset
)
485 ipc_port_t port
= pset
->pset_name_self
;
488 port
= ipc_port_make_send(port
);
494 * Routine: convert_port_to_host_security
496 * Convert from a port to a host security.
497 * Doesn't consume the port ref; the port produced may be null.
503 convert_port_to_host_security(
506 host_t host
= HOST_NULL
;
508 if (IP_VALID(port
)) {
510 if (ip_active(port
) &&
511 (ip_kotype(port
) == IKOT_HOST_SECURITY
))
512 host
= (host_t
) port
->ip_kobject
;
520 * Routine: host_set_exception_ports [kernel call]
522 * Sets the host exception port, flavor and
523 * behavior for the exception types specified by the mask.
524 * There will be one send right per exception per valid
527 * Nothing locked. If successful, consumes
528 * the supplied send right.
530 * KERN_SUCCESS Changed the special port.
531 * KERN_INVALID_ARGUMENT The host_priv is not valid,
532 * Illegal mask bit set.
533 * Illegal exception behavior
536 host_set_exception_ports(
537 host_priv_t host_priv
,
538 exception_mask_t exception_mask
,
540 exception_behavior_t new_behavior
,
541 thread_state_flavor_t new_flavor
)
544 ipc_port_t old_port
[EXC_TYPES_COUNT
];
546 if (host_priv
== HOST_PRIV_NULL
) {
547 return KERN_INVALID_ARGUMENT
;
550 assert(host_priv
== &realhost
);
552 if (exception_mask
& ~EXC_MASK_VALID
) {
553 return KERN_INVALID_ARGUMENT
;
556 if (IP_VALID(new_port
)) {
557 switch (new_behavior
& ~MACH_EXCEPTION_CODES
) {
558 case EXCEPTION_DEFAULT
:
559 case EXCEPTION_STATE
:
560 case EXCEPTION_STATE_IDENTITY
:
563 return KERN_INVALID_ARGUMENT
;
566 /* Cannot easily check "new_flavor", but that just means that
567 * the flavor in the generated exception message might be garbage:
570 host_lock(host_priv
);
572 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
573 if (exception_mask
& (1 << i
)) {
574 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
575 host_priv
->exc_actions
[i
].port
=
576 ipc_port_copy_send(new_port
);
577 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
578 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
580 old_port
[i
] = IP_NULL
;
584 * Consume send rights without any lock held.
586 host_unlock(host_priv
);
587 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
588 if (IP_VALID(old_port
[i
]))
589 ipc_port_release_send(old_port
[i
]);
590 if (IP_VALID(new_port
)) /* consume send right */
591 ipc_port_release_send(new_port
);
597 * Routine: host_get_exception_ports [kernel call]
599 * Clones a send right for each of the host's exception
600 * ports specified in the mask and returns the behaviour
601 * and flavor of said port.
603 * Returns upto [in} CountCnt elements.
608 * KERN_SUCCESS Extracted a send right.
609 * KERN_INVALID_ARGUMENT Invalid host_priv specified,
610 * Invalid special port,
611 * Illegal mask bit set.
612 * KERN_FAILURE The thread is dead.
615 host_get_exception_ports(
616 host_priv_t host_priv
,
617 exception_mask_t exception_mask
,
618 exception_mask_array_t masks
,
619 mach_msg_type_number_t
* CountCnt
,
620 exception_port_array_t ports
,
621 exception_behavior_array_t behaviors
,
622 thread_state_flavor_array_t flavors
)
624 unsigned int i
, j
, count
;
626 if (host_priv
== HOST_PRIV_NULL
)
627 return KERN_INVALID_ARGUMENT
;
629 if (exception_mask
& ~EXC_MASK_VALID
) {
630 return KERN_INVALID_ARGUMENT
;
633 assert (host_priv
== &realhost
);
635 host_lock(host_priv
);
639 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
640 if (exception_mask
& (1 << i
)) {
641 for (j
= 0; j
< count
; j
++) {
643 * search for an identical entry, if found
644 * set corresponding mask for this exception.
646 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
647 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
648 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
650 masks
[j
] |= (1 << i
);
657 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
658 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
659 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
661 if (count
> *CountCnt
) {
667 host_unlock(host_priv
);
674 host_swap_exception_ports(
675 host_priv_t host_priv
,
676 exception_mask_t exception_mask
,
678 exception_behavior_t new_behavior
,
679 thread_state_flavor_t new_flavor
,
680 exception_mask_array_t masks
,
681 mach_msg_type_number_t
* CountCnt
,
682 exception_port_array_t ports
,
683 exception_behavior_array_t behaviors
,
684 thread_state_flavor_array_t flavors
)
689 ipc_port_t old_port
[EXC_TYPES_COUNT
];
691 if (host_priv
== HOST_PRIV_NULL
)
692 return KERN_INVALID_ARGUMENT
;
694 if (exception_mask
& ~EXC_MASK_VALID
) {
695 return KERN_INVALID_ARGUMENT
;
698 if (IP_VALID(new_port
)) {
699 switch (new_behavior
) {
700 case EXCEPTION_DEFAULT
:
701 case EXCEPTION_STATE
:
702 case EXCEPTION_STATE_IDENTITY
:
705 return KERN_INVALID_ARGUMENT
;
708 /* Cannot easily check "new_flavor", but that just means that
709 * the flavor in the generated exception message might be garbage:
712 host_lock(host_priv
);
716 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
717 if (exception_mask
& (1 << i
)) {
718 for (j
= 0; j
< count
; j
++) {
720 * search for an identical entry, if found
721 * set corresponding mask for this exception.
723 if (host_priv
->exc_actions
[i
].port
== ports
[j
] &&
724 host_priv
->exc_actions
[i
].behavior
== behaviors
[j
]
725 && host_priv
->exc_actions
[i
].flavor
== flavors
[j
])
727 masks
[j
] |= (1 << i
);
734 ipc_port_copy_send(host_priv
->exc_actions
[i
].port
);
735 behaviors
[j
] = host_priv
->exc_actions
[i
].behavior
;
736 flavors
[j
] = host_priv
->exc_actions
[i
].flavor
;
739 old_port
[i
] = host_priv
->exc_actions
[i
].port
;
740 host_priv
->exc_actions
[i
].port
=
741 ipc_port_copy_send(new_port
);
742 host_priv
->exc_actions
[i
].behavior
= new_behavior
;
743 host_priv
->exc_actions
[i
].flavor
= new_flavor
;
744 if (count
> *CountCnt
) {
748 old_port
[i
] = IP_NULL
;
750 host_unlock(host_priv
);
753 * Consume send rights without any lock held.
755 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
756 if (IP_VALID(old_port
[i
]))
757 ipc_port_release_send(old_port
[i
]);
758 if (IP_VALID(new_port
)) /* consume send right */
759 ipc_port_release_send(new_port
);