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,1987 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 * Task and thread related IPC functions.
59 #include <mach/boolean.h>
61 #include <mach/kern_return.h>
62 #include <mach/mach_param.h>
63 #include <mach/task_special_ports.h>
64 #include <mach/thread_special_ports.h>
65 #include <mach/thread_status.h>
66 #include <mach/exception_types.h>
67 #include <mach/mach_traps.h>
68 #include <mach/task_server.h>
69 #include <mach/thread_act_server.h>
70 #include <mach/mach_host_server.h>
71 #include <mach/vm_map_server.h>
72 #include <kern/host.h>
73 #include <kern/ipc_tt.h>
74 #include <kern/thread_act.h>
75 #include <kern/misc_protos.h>
76 #include <vm/vm_pageout.h>
79 * Routine: ipc_task_init
81 * Initialize a task's IPC state.
83 * If non-null, some state will be inherited from the parent.
84 * The parent must be appropriately initialized.
100 kr
= ipc_space_create(&ipc_table_entries
[0], &space
);
101 if (kr
!= KERN_SUCCESS
)
102 panic("ipc_task_init");
105 kport
= ipc_port_alloc_kernel();
106 if (kport
== IP_NULL
)
107 panic("ipc_task_init");
110 task
->itk_self
= kport
;
111 task
->itk_sself
= ipc_port_make_send(kport
);
112 task
->itk_space
= space
;
113 space
->is_fast
= task
->kernel_loaded
;
115 if (parent
== TASK_NULL
) {
116 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
117 task
->exc_actions
[i
].port
= IP_NULL
;
119 task
->exc_actions
[EXC_MACH_SYSCALL
].port
=
120 ipc_port_make_send(realhost
.host_self
);
121 task
->itk_host
= ipc_port_make_send(realhost
.host_self
);
122 task
->itk_bootstrap
= IP_NULL
;
123 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++)
124 task
->itk_registered
[i
] = IP_NULL
;
127 assert(parent
->itk_self
!= IP_NULL
);
129 /* inherit registered ports */
131 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++)
132 task
->itk_registered
[i
] =
133 ipc_port_copy_send(parent
->itk_registered
[i
]);
135 /* inherit exception and bootstrap ports */
137 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
138 task
->exc_actions
[i
].port
=
139 ipc_port_copy_send(parent
->exc_actions
[i
].port
);
140 task
->exc_actions
[i
].flavor
=
141 parent
->exc_actions
[i
].flavor
;
142 task
->exc_actions
[i
].behavior
=
143 parent
->exc_actions
[i
].behavior
;
146 ipc_port_copy_send(parent
->itk_host
);
148 task
->itk_bootstrap
=
149 ipc_port_copy_send(parent
->itk_bootstrap
);
156 * Routine: ipc_task_enable
158 * Enable a task for IPC access.
170 kport
= task
->itk_self
;
171 if (kport
!= IP_NULL
)
172 ipc_kobject_set(kport
, (ipc_kobject_t
) task
, IKOT_TASK
);
177 * Routine: ipc_task_disable
179 * Disable IPC access to a task.
191 kport
= task
->itk_self
;
192 if (kport
!= IP_NULL
)
193 ipc_kobject_set(kport
, IKO_NULL
, IKOT_NONE
);
198 * Routine: ipc_task_terminate
200 * Clean up and destroy a task's IPC state.
202 * Nothing locked. The task must be suspended.
203 * (Or the current thread must be in the task.)
214 kport
= task
->itk_self
;
216 if (kport
== IP_NULL
) {
217 /* the task is already terminated (can this happen?) */
222 task
->itk_self
= IP_NULL
;
225 /* release the naked send rights */
227 if (IP_VALID(task
->itk_sself
))
228 ipc_port_release_send(task
->itk_sself
);
230 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
231 if (IP_VALID(task
->exc_actions
[i
].port
)) {
232 ipc_port_release_send(task
->exc_actions
[i
].port
);
235 if (IP_VALID(task
->itk_host
))
236 ipc_port_release_send(task
->itk_host
);
238 if (IP_VALID(task
->itk_bootstrap
))
239 ipc_port_release_send(task
->itk_bootstrap
);
241 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++)
242 if (IP_VALID(task
->itk_registered
[i
]))
243 ipc_port_release_send(task
->itk_registered
[i
]);
245 ipc_port_release_send(task
->wired_ledger_port
);
246 ipc_port_release_send(task
->paged_ledger_port
);
248 /* destroy the kernel port */
249 ipc_port_dealloc_kernel(kport
);
253 * Routine: ipc_thread_init
255 * Initialize a thread's IPC state.
264 ipc_kmsg_queue_init(&thread
->ith_messages
);
265 thread
->ith_mig_reply
= MACH_PORT_NULL
;
266 thread
->ith_rpc_reply
= IP_NULL
;
270 * Routine: ipc_thread_terminate
272 * Clean up and destroy a thread's IPC state.
274 * Nothing locked. The thread must be suspended.
275 * (Or be the current thread.)
279 ipc_thread_terminate(
282 assert(ipc_kmsg_queue_empty(&thread
->ith_messages
));
284 if (thread
->ith_rpc_reply
!= IP_NULL
)
285 ipc_port_dealloc_reply(thread
->ith_rpc_reply
);
286 thread
->ith_rpc_reply
= IP_NULL
;
290 * Routine: ipc_thr_act_init
292 * Initialize an thr_act's IPC state.
298 ipc_thr_act_init(task_t task
, thread_act_t thr_act
)
300 ipc_port_t kport
; int i
;
302 kport
= ipc_port_alloc_kernel();
303 if (kport
== IP_NULL
)
304 panic("ipc_thr_act_init");
306 thr_act
->ith_self
= kport
;
307 thr_act
->ith_sself
= ipc_port_make_send(kport
);
309 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
310 thr_act
->exc_actions
[i
].port
= IP_NULL
;
312 thr_act
->exc_actions
[EXC_MACH_SYSCALL
].port
=
313 ipc_port_make_send(realhost
.host_self
);
315 ipc_kobject_set(kport
, (ipc_kobject_t
) thr_act
, IKOT_ACT
);
319 ipc_thr_act_disable(thread_act_t thr_act
)
324 kport
= thr_act
->ith_self
;
326 if (kport
!= IP_NULL
)
327 ipc_kobject_set(kport
, IKO_NULL
, IKOT_NONE
);
331 ipc_thr_act_terminate(thread_act_t thr_act
)
333 ipc_port_t kport
; int i
;
335 kport
= thr_act
->ith_self
;
337 if (kport
== IP_NULL
) {
338 /* the thread is already terminated (can this happen?) */
342 thr_act
->ith_self
= IP_NULL
;
344 /* release the naked send rights */
346 if (IP_VALID(thr_act
->ith_sself
))
347 ipc_port_release_send(thr_act
->ith_sself
);
348 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
349 if (IP_VALID(thr_act
->exc_actions
[i
].port
))
350 ipc_port_release_send(thr_act
->exc_actions
[i
].port
);
353 /* destroy the kernel port */
354 ipc_port_dealloc_kernel(kport
);
358 * Routine: retrieve_task_self_fast
360 * Optimized version of retrieve_task_self,
361 * that only works for the current task.
363 * Return a send right (possibly null/dead)
364 * for the task's user-visible self port.
370 retrieve_task_self_fast(
371 register task_t task
)
373 register ipc_port_t port
;
375 assert(task
== current_task());
378 assert(task
->itk_self
!= IP_NULL
);
380 if ((port
= task
->itk_sself
) == task
->itk_self
) {
384 assert(ip_active(port
));
389 port
= ipc_port_copy_send(port
);
396 * Routine: retrieve_act_self_fast
398 * Optimized version of retrieve_thread_self,
399 * that only works for the current thread.
401 * Return a send right (possibly null/dead)
402 * for the thread's user-visible self port.
408 retrieve_act_self_fast(thread_act_t thr_act
)
410 register ipc_port_t port
;
412 assert(thr_act
== current_act());
414 assert(thr_act
->ith_self
!= IP_NULL
);
416 if ((port
= thr_act
->ith_sself
) == thr_act
->ith_self
) {
420 assert(ip_active(port
));
425 port
= ipc_port_copy_send(port
);
432 * Routine: task_self_trap [mach trap]
434 * Give the caller send rights for his own task port.
438 * MACH_PORT_NULL if there are any resource failures
445 task_t task
= current_task();
448 sright
= retrieve_task_self_fast(task
);
449 return ipc_port_copyout_send(sright
, task
->itk_space
);
453 * Routine: thread_self_trap [mach trap]
455 * Give the caller send rights for his own thread port.
459 * MACH_PORT_NULL if there are any resource failures
464 thread_self_trap(void)
466 thread_act_t thr_act
= current_act();
467 task_t task
= thr_act
->task
;
470 sright
= retrieve_act_self_fast(thr_act
);
471 return ipc_port_copyout_send(sright
, task
->itk_space
);
475 * Routine: mach_reply_port [mach trap]
477 * Allocate a port for the caller.
481 * MACH_PORT_NULL if there are any resource failures
486 mach_reply_port(void)
489 mach_port_name_t name
;
492 kr
= ipc_port_alloc(current_task()->itk_space
, &name
, &port
);
493 if (kr
== KERN_SUCCESS
)
496 name
= MACH_PORT_NULL
;
502 * Routine: task_get_special_port [kernel call]
504 * Clones a send right for one of the task's
509 * KERN_SUCCESS Extracted a send right.
510 * KERN_INVALID_ARGUMENT The task is null.
511 * KERN_FAILURE The task/space is dead.
512 * KERN_INVALID_ARGUMENT Invalid special port.
516 task_get_special_port(
524 if (task
== TASK_NULL
)
525 return KERN_INVALID_ARGUMENT
;
528 case TASK_KERNEL_PORT
:
529 whichp
= &task
->itk_sself
;
533 whichp
= &task
->itk_host
;
536 case TASK_BOOTSTRAP_PORT
:
537 whichp
= &task
->itk_bootstrap
;
540 case TASK_WIRED_LEDGER_PORT
:
541 whichp
= &task
->wired_ledger_port
;
544 case TASK_PAGED_LEDGER_PORT
:
545 whichp
= &task
->paged_ledger_port
;
549 return KERN_INVALID_ARGUMENT
;
553 if (task
->itk_self
== IP_NULL
) {
558 port
= ipc_port_copy_send(*whichp
);
566 * Routine: task_set_special_port [kernel call]
568 * Changes one of the task's special ports,
569 * setting it to the supplied send right.
571 * Nothing locked. If successful, consumes
572 * the supplied send right.
574 * KERN_SUCCESS Changed the special port.
575 * KERN_INVALID_ARGUMENT The task is null.
576 * KERN_FAILURE The task/space is dead.
577 * KERN_INVALID_ARGUMENT Invalid special port.
581 task_set_special_port(
589 if (task
== TASK_NULL
)
590 return KERN_INVALID_ARGUMENT
;
593 case TASK_KERNEL_PORT
:
594 whichp
= &task
->itk_sself
;
598 whichp
= &task
->itk_host
;
601 case TASK_BOOTSTRAP_PORT
:
602 whichp
= &task
->itk_bootstrap
;
605 case TASK_WIRED_LEDGER_PORT
:
606 whichp
= &task
->wired_ledger_port
;
609 case TASK_PAGED_LEDGER_PORT
:
610 whichp
= &task
->paged_ledger_port
;
614 return KERN_INVALID_ARGUMENT
;
618 if (task
->itk_self
== IP_NULL
) {
628 ipc_port_release_send(old
);
634 * Routine: mach_ports_register [kernel call]
636 * Stash a handful of port send rights in the task.
637 * Child tasks will inherit these rights, but they
638 * must use mach_ports_lookup to acquire them.
640 * The rights are supplied in a (wired) kalloc'd segment.
641 * Rights which aren't supplied are assumed to be null.
643 * Nothing locked. If successful, consumes
644 * the supplied rights and memory.
646 * KERN_SUCCESS Stashed the port rights.
647 * KERN_INVALID_ARGUMENT The task is null.
648 * KERN_INVALID_ARGUMENT The task is dead.
649 * KERN_INVALID_ARGUMENT Too many port rights supplied.
655 mach_port_array_t memory
,
656 mach_msg_type_number_t portsCnt
)
658 ipc_port_t ports
[TASK_PORT_REGISTER_MAX
];
661 if ((task
== TASK_NULL
) ||
662 (portsCnt
> TASK_PORT_REGISTER_MAX
))
663 return KERN_INVALID_ARGUMENT
;
666 * Pad the port rights with nulls.
669 for (i
= 0; i
< portsCnt
; i
++)
670 ports
[i
] = memory
[i
];
671 for (; i
< TASK_PORT_REGISTER_MAX
; i
++)
675 if (task
->itk_self
== IP_NULL
) {
677 return KERN_INVALID_ARGUMENT
;
681 * Replace the old send rights with the new.
682 * Release the old rights after unlocking.
685 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++) {
688 old
= task
->itk_registered
[i
];
689 task
->itk_registered
[i
] = ports
[i
];
695 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++)
696 if (IP_VALID(ports
[i
]))
697 ipc_port_release_send(ports
[i
]);
700 * Now that the operation is known to be successful,
701 * we can free the memory.
705 kfree((vm_offset_t
) memory
,
706 (vm_size_t
) (portsCnt
* sizeof(mach_port_t
)));
712 * Routine: mach_ports_lookup [kernel call]
714 * Retrieves (clones) the stashed port send rights.
716 * Nothing locked. If successful, the caller gets
719 * KERN_SUCCESS Retrieved the send rights.
720 * KERN_INVALID_ARGUMENT The task is null.
721 * KERN_INVALID_ARGUMENT The task is dead.
722 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
728 mach_port_array_t
*portsp
,
729 mach_msg_type_number_t
*portsCnt
)
738 if (task
== TASK_NULL
)
739 return KERN_INVALID_ARGUMENT
;
741 size
= (vm_size_t
) (TASK_PORT_REGISTER_MAX
* sizeof(ipc_port_t
));
743 memory
= kalloc(size
);
745 return KERN_RESOURCE_SHORTAGE
;
748 if (task
->itk_self
== IP_NULL
) {
752 return KERN_INVALID_ARGUMENT
;
755 ports
= (ipc_port_t
*) memory
;
758 * Clone port rights. Because kalloc'd memory
759 * is wired, we won't fault while holding the task lock.
762 for (i
= 0; i
< TASK_PORT_REGISTER_MAX
; i
++)
763 ports
[i
] = ipc_port_copy_send(task
->itk_registered
[i
]);
767 *portsp
= (mach_port_array_t
) ports
;
768 *portsCnt
= TASK_PORT_REGISTER_MAX
;
773 * Routine: convert_port_to_locked_task
775 * Internal helper routine to convert from a port to a locked
776 * task. Used by several routines that try to convert from a
777 * task port to a reference on some task related object.
779 * Nothing locked, blocking OK.
782 convert_port_to_locked_task(ipc_port_t port
)
784 while (IP_VALID(port
)) {
788 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_TASK
)) {
792 task
= (task_t
) port
->ip_kobject
;
793 assert(task
!= TASK_NULL
);
796 * Normal lock ordering puts task_lock() before ip_lock().
797 * Attempt out-of-order locking here.
799 if (task_lock_try(task
)) {
811 * Routine: convert_port_to_task
813 * Convert from a port to a task.
814 * Doesn't consume the port ref; produces a task ref,
820 convert_port_to_task(
825 task
= convert_port_to_locked_task(port
);
834 * Routine: convert_port_to_space
836 * Convert from a port to a space.
837 * Doesn't consume the port ref; produces a space ref,
843 convert_port_to_space(
849 task
= convert_port_to_locked_task(port
);
851 if (task
== TASK_NULL
)
852 return IPC_SPACE_NULL
;
856 return IPC_SPACE_NULL
;
859 space
= task
->itk_space
;
872 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UPL
)) {
876 upl
= (upl_t
) port
->ip_kobject
;
885 * Routine: convert_port_entry_to_map
887 * Convert from a port specifying an entry or a task
888 * to a map. Doesn't consume the port ref; produces a map ref,
889 * which may be null. Unlike convert_port_to_map, the
890 * port may be task or a named entry backed.
897 convert_port_entry_to_map(
902 vm_named_entry_t named_entry
;
904 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
907 if(ip_active(port
) && (ip_kotype(port
)
908 == IKOT_NAMED_ENTRY
)) {
910 (vm_named_entry_t
)port
->ip_kobject
;
911 if (!(mutex_try(&(named_entry
)->Lock
))) {
916 named_entry
->ref_count
++;
917 mutex_unlock(&(named_entry
)->Lock
);
919 if ((named_entry
->is_sub_map
) &&
920 (named_entry
->protection
922 map
= named_entry
->backing
.map
;
924 mach_destroy_memory_entry(port
);
927 vm_map_reference_swap(map
);
928 mach_destroy_memory_entry(port
);
937 task
= convert_port_to_locked_task(port
);
939 if (task
== TASK_NULL
)
948 vm_map_reference_swap(map
);
956 * Routine: convert_port_entry_to_object
958 * Convert from a port specifying a named entry to an
959 * object. Doesn't consume the port ref; produces a map ref,
967 convert_port_entry_to_object(
971 vm_named_entry_t named_entry
;
973 if(IP_VALID(port
) && (ip_kotype(port
) == IKOT_NAMED_ENTRY
)) {
976 if(ip_active(port
) && (ip_kotype(port
)
977 == IKOT_NAMED_ENTRY
)) {
979 (vm_named_entry_t
)port
->ip_kobject
;
980 if (!(mutex_try(&(named_entry
)->Lock
))) {
985 named_entry
->ref_count
++;
986 mutex_unlock(&(named_entry
)->Lock
);
988 if ((!named_entry
->is_sub_map
) &&
989 (named_entry
->protection
991 object
= named_entry
->object
;
993 mach_destroy_memory_entry(port
);
994 return (vm_object_t
)NULL
;
996 vm_object_reference(named_entry
->object
);
997 mach_destroy_memory_entry(port
);
1001 return (vm_object_t
)NULL
;
1004 return (vm_object_t
)NULL
;
1011 * Routine: convert_port_to_map
1013 * Convert from a port to a map.
1014 * Doesn't consume the port ref; produces a map ref,
1015 * which may be null.
1021 convert_port_to_map(
1027 task
= convert_port_to_locked_task(port
);
1029 if (task
== TASK_NULL
)
1032 if (!task
->active
) {
1038 vm_map_reference_swap(map
);
1045 * Routine: convert_port_to_act
1047 * Convert from a port to a thr_act.
1048 * Doesn't consume the port ref; produces an thr_act ref,
1049 * which may be null.
1055 convert_port_to_act( ipc_port_t port
)
1058 thread_act_t thr_act
= 0;
1061 while (!r
&& IP_VALID(port
)) {
1063 r
= ref_act_port_locked(port
, &thr_act
);
1070 ref_act_port_locked( ipc_port_t port
, thread_act_t
*pthr_act
)
1072 thread_act_t thr_act
;
1075 if (ip_active(port
) &&
1076 (ip_kotype(port
) == IKOT_ACT
)) {
1077 thr_act
= (thread_act_t
) port
->ip_kobject
;
1078 assert(thr_act
!= THR_ACT_NULL
);
1081 * Normal lock ordering is act_lock(), then ip_lock().
1082 * Allow out-of-order locking here, using
1083 * act_reference_act_locked() to accomodate it.
1085 if (!act_lock_try(thr_act
)) {
1090 act_locked_act_reference(thr_act
);
1091 act_unlock(thr_act
);
1093 *pthr_act
= thr_act
;
1099 * Routine: port_name_to_act
1101 * Convert from a port name to an act reference
1102 * A name of MACH_PORT_NULL is valid for the null act
1108 mach_port_name_t name
)
1110 thread_act_t thr_act
= THR_ACT_NULL
;
1111 ipc_port_t kern_port
;
1114 if (MACH_PORT_VALID(name
)) {
1115 kr
= ipc_object_copyin(current_space(), name
,
1116 MACH_MSG_TYPE_COPY_SEND
,
1117 (ipc_object_t
*) &kern_port
);
1118 if (kr
!= KERN_SUCCESS
)
1119 return THR_ACT_NULL
;
1121 thr_act
= convert_port_to_act(kern_port
);
1123 if (IP_VALID(kern_port
))
1124 ipc_port_release_send(kern_port
);
1131 mach_port_name_t name
)
1133 ipc_port_t kern_port
;
1135 task_t task
= TASK_NULL
;
1137 if (MACH_PORT_VALID(name
)) {
1138 kr
= ipc_object_copyin(current_space(), name
,
1139 MACH_MSG_TYPE_COPY_SEND
,
1140 (ipc_object_t
*) &kern_port
);
1141 if (kr
!= KERN_SUCCESS
)
1144 task
= convert_port_to_task(kern_port
);
1146 if (IP_VALID(kern_port
))
1147 ipc_port_release_send(kern_port
);
1153 * Routine: convert_task_to_port
1155 * Convert from a task to a port.
1156 * Consumes a task ref; produces a naked send right
1157 * which may be invalid.
1163 convert_task_to_port(
1169 if (task
->itk_self
!= IP_NULL
)
1171 if (task
->map
== VM_MAP_NULL
)
1172 /* norma placeholder task */
1173 port
= ipc_port_copy_send(task
->itk_self
);
1175 #endif /* NORMA_TASK */
1176 port
= ipc_port_make_send(task
->itk_self
);
1181 task_deallocate(task
);
1186 * Routine: convert_act_to_port
1188 * Convert from a thr_act to a port.
1189 * Consumes an thr_act ref; produces a naked send right
1190 * which may be invalid.
1196 convert_act_to_port(thr_act
)
1197 thread_act_t thr_act
;
1202 if (thr_act
->ith_self
!= IP_NULL
)
1203 port
= ipc_port_make_send(thr_act
->ith_self
);
1206 act_unlock(thr_act
);
1208 act_deallocate(thr_act
);
1213 * Routine: space_deallocate
1215 * Deallocate a space ref produced by convert_port_to_space.
1224 if (space
!= IS_NULL
)
1229 * Routine: thread/task_set_exception_ports [kernel call]
1231 * Sets the thread/task exception port, flavor and
1232 * behavior for the exception types specified by the mask.
1233 * There will be one send right per exception per valid
1236 * Nothing locked. If successful, consumes
1237 * the supplied send right.
1239 * KERN_SUCCESS Changed the special port.
1240 * KERN_INVALID_ARGUMENT The thread is null,
1241 * Illegal mask bit set.
1242 * Illegal exception behavior
1243 * KERN_FAILURE The thread is dead.
1247 thread_set_exception_ports(
1248 thread_act_t thr_act
,
1249 exception_mask_t exception_mask
,
1250 ipc_port_t new_port
,
1251 exception_behavior_t new_behavior
,
1252 thread_state_flavor_t new_flavor
)
1255 ipc_port_t old_port
[EXC_TYPES_COUNT
];
1258 return KERN_INVALID_ARGUMENT
;
1260 if (exception_mask
& ~EXC_MASK_ALL
)
1261 return KERN_INVALID_ARGUMENT
;
1263 if (IP_VALID(new_port
)) {
1264 switch (new_behavior
) {
1265 case EXCEPTION_DEFAULT
:
1266 case EXCEPTION_STATE
:
1267 case EXCEPTION_STATE_IDENTITY
:
1270 return KERN_INVALID_ARGUMENT
;
1275 * Check the validity of the thread_state_flavor by calling the
1276 * VALID_THREAD_STATE_FLAVOR architecture dependent macro defined in
1277 * osfmk/mach/ARCHITECTURE/thread_status.h
1279 if (!VALID_THREAD_STATE_FLAVOR(new_flavor
)) {
1280 return KERN_INVALID_ARGUMENT
;
1284 if (!thr_act
->active
) {
1285 act_unlock(thr_act
);
1286 return KERN_FAILURE
;
1289 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
1290 if (exception_mask
& (1 << i
)) {
1291 old_port
[i
] = thr_act
->exc_actions
[i
].port
;
1292 thr_act
->exc_actions
[i
].port
=
1293 ipc_port_copy_send(new_port
);
1294 thr_act
->exc_actions
[i
].behavior
= new_behavior
;
1295 thr_act
->exc_actions
[i
].flavor
= new_flavor
;
1297 old_port
[i
] = IP_NULL
;
1300 * Consume send rights without any lock held.
1302 act_unlock(thr_act
);
1303 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
1304 if (IP_VALID(old_port
[i
]))
1305 ipc_port_release_send(old_port
[i
]);
1306 if (IP_VALID(new_port
)) /* consume send right */
1307 ipc_port_release_send(new_port
);
1309 return KERN_SUCCESS
;
1310 }/* thread_set_exception_port */
1313 task_set_exception_ports(
1315 exception_mask_t exception_mask
,
1316 ipc_port_t new_port
,
1317 exception_behavior_t new_behavior
,
1318 thread_state_flavor_t new_flavor
)
1321 ipc_port_t old_port
[EXC_TYPES_COUNT
];
1323 if (task
== TASK_NULL
) {
1324 return KERN_INVALID_ARGUMENT
;
1327 if (exception_mask
& ~EXC_MASK_ALL
) {
1328 return KERN_INVALID_ARGUMENT
;
1331 if (IP_VALID(new_port
)) {
1332 switch (new_behavior
) {
1333 case EXCEPTION_DEFAULT
:
1334 case EXCEPTION_STATE
:
1335 case EXCEPTION_STATE_IDENTITY
:
1338 return KERN_INVALID_ARGUMENT
;
1341 /* Cannot easily check "new_flavor", but that just means that
1342 * the flavor in the generated exception message might be garbage:
1346 if (task
->itk_self
== IP_NULL
) {
1348 return KERN_FAILURE
;
1351 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
1352 if (exception_mask
& (1 << i
)) {
1353 old_port
[i
] = task
->exc_actions
[i
].port
;
1354 task
->exc_actions
[i
].port
=
1355 ipc_port_copy_send(new_port
);
1356 task
->exc_actions
[i
].behavior
= new_behavior
;
1357 task
->exc_actions
[i
].flavor
= new_flavor
;
1359 old_port
[i
] = IP_NULL
;
1363 * Consume send rights without any lock held.
1366 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
1367 if (IP_VALID(old_port
[i
]))
1368 ipc_port_release_send(old_port
[i
]);
1369 if (IP_VALID(new_port
)) /* consume send right */
1370 ipc_port_release_send(new_port
);
1372 return KERN_SUCCESS
;
1373 }/* task_set_exception_port */
1376 * Routine: thread/task_swap_exception_ports [kernel call]
1378 * Sets the thread/task exception port, flavor and
1379 * behavior for the exception types specified by the
1382 * The old ports, behavior and flavors are returned
1383 * Count specifies the array sizes on input and
1384 * the number of returned ports etc. on output. The
1385 * arrays must be large enough to hold all the returned
1386 * data, MIG returnes an error otherwise. The masks
1387 * array specifies the corresponding exception type(s).
1390 * Nothing locked. If successful, consumes
1391 * the supplied send right.
1393 * Returns upto [in} CountCnt elements.
1395 * KERN_SUCCESS Changed the special port.
1396 * KERN_INVALID_ARGUMENT The thread is null,
1397 * Illegal mask bit set.
1398 * Illegal exception behavior
1399 * KERN_FAILURE The thread is dead.
1403 thread_swap_exception_ports(
1404 thread_act_t thr_act
,
1405 exception_mask_t exception_mask
,
1406 ipc_port_t new_port
,
1407 exception_behavior_t new_behavior
,
1408 thread_state_flavor_t new_flavor
,
1409 exception_mask_array_t masks
,
1410 mach_msg_type_number_t
* CountCnt
,
1411 exception_port_array_t ports
,
1412 exception_behavior_array_t behaviors
,
1413 thread_state_flavor_array_t flavors
)
1418 ipc_port_t old_port
[EXC_TYPES_COUNT
];
1421 return KERN_INVALID_ARGUMENT
;
1423 if (exception_mask
& ~EXC_MASK_ALL
) {
1424 return KERN_INVALID_ARGUMENT
;
1427 if (IP_VALID(new_port
)) {
1428 switch (new_behavior
) {
1429 case EXCEPTION_DEFAULT
:
1430 case EXCEPTION_STATE
:
1431 case EXCEPTION_STATE_IDENTITY
:
1434 return KERN_INVALID_ARGUMENT
;
1437 /* Cannot easily check "new_flavor", but that just means that
1438 * the flavor in the generated exception message might be garbage:
1442 if (!thr_act
->active
) {
1443 act_unlock(thr_act
);
1444 return KERN_FAILURE
;
1449 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
1450 if (exception_mask
& (1 << i
)) {
1451 for (j
= 0; j
< count
; j
++) {
1453 * search for an identical entry, if found
1454 * set corresponding mask for this exception.
1456 if (thr_act
->exc_actions
[i
].port
== ports
[j
] &&
1457 thr_act
->exc_actions
[i
].behavior
==behaviors
[j
]
1458 && thr_act
->exc_actions
[i
].flavor
==flavors
[j
])
1460 masks
[j
] |= (1 << i
);
1465 masks
[j
] = (1 << i
);
1467 ipc_port_copy_send(thr_act
->exc_actions
[i
].port
);
1469 behaviors
[j
] = thr_act
->exc_actions
[i
].behavior
;
1470 flavors
[j
] = thr_act
->exc_actions
[i
].flavor
;
1474 old_port
[i
] = thr_act
->exc_actions
[i
].port
;
1475 thr_act
->exc_actions
[i
].port
=
1476 ipc_port_copy_send(new_port
);
1477 thr_act
->exc_actions
[i
].behavior
= new_behavior
;
1478 thr_act
->exc_actions
[i
].flavor
= new_flavor
;
1479 if (count
> *CountCnt
) {
1483 old_port
[i
] = IP_NULL
;
1487 * Consume send rights without any lock held.
1489 act_unlock(thr_act
);
1490 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
1491 if (IP_VALID(old_port
[i
]))
1492 ipc_port_release_send(old_port
[i
]);
1493 if (IP_VALID(new_port
)) /* consume send right */
1494 ipc_port_release_send(new_port
);
1496 return KERN_SUCCESS
;
1497 }/* thread_swap_exception_ports */
1500 task_swap_exception_ports(
1502 exception_mask_t exception_mask
,
1503 ipc_port_t new_port
,
1504 exception_behavior_t new_behavior
,
1505 thread_state_flavor_t new_flavor
,
1506 exception_mask_array_t masks
,
1507 mach_msg_type_number_t
* CountCnt
,
1508 exception_port_array_t ports
,
1509 exception_behavior_array_t behaviors
,
1510 thread_state_flavor_array_t flavors
)
1515 ipc_port_t old_port
[EXC_TYPES_COUNT
];
1517 if (task
== TASK_NULL
)
1518 return KERN_INVALID_ARGUMENT
;
1520 if (exception_mask
& ~EXC_MASK_ALL
) {
1521 return KERN_INVALID_ARGUMENT
;
1524 if (IP_VALID(new_port
)) {
1525 switch (new_behavior
) {
1526 case EXCEPTION_DEFAULT
:
1527 case EXCEPTION_STATE
:
1528 case EXCEPTION_STATE_IDENTITY
:
1531 return KERN_INVALID_ARGUMENT
;
1534 /* Cannot easily check "new_flavor", but that just means that
1535 * the flavor in the generated exception message might be garbage:
1539 if (task
->itk_self
== IP_NULL
) {
1541 return KERN_FAILURE
;
1546 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
1547 if (exception_mask
& (1 << i
)) {
1548 for (j
= 0; j
< count
; j
++) {
1550 * search for an identical entry, if found
1551 * set corresponding mask for this exception.
1553 if (task
->exc_actions
[i
].port
== ports
[j
] &&
1554 task
->exc_actions
[i
].behavior
== behaviors
[j
]
1555 && task
->exc_actions
[i
].flavor
== flavors
[j
])
1557 masks
[j
] |= (1 << i
);
1562 masks
[j
] = (1 << i
);
1564 ipc_port_copy_send(task
->exc_actions
[i
].port
);
1565 behaviors
[j
] = task
->exc_actions
[i
].behavior
;
1566 flavors
[j
] = task
->exc_actions
[i
].flavor
;
1569 old_port
[i
] = task
->exc_actions
[i
].port
;
1570 task
->exc_actions
[i
].port
=
1571 ipc_port_copy_send(new_port
);
1572 task
->exc_actions
[i
].behavior
= new_behavior
;
1573 task
->exc_actions
[i
].flavor
= new_flavor
;
1574 if (count
> *CountCnt
) {
1578 old_port
[i
] = IP_NULL
;
1583 * Consume send rights without any lock held.
1586 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++)
1587 if (IP_VALID(old_port
[i
]))
1588 ipc_port_release_send(old_port
[i
]);
1589 if (IP_VALID(new_port
)) /* consume send right */
1590 ipc_port_release_send(new_port
);
1593 return KERN_SUCCESS
;
1594 }/* task_swap_exception_ports */
1597 * Routine: thread/task_get_exception_ports [kernel call]
1599 * Clones a send right for each of the thread/task's exception
1600 * ports specified in the mask and returns the behaviour
1601 * and flavor of said port.
1603 * Returns upto [in} CountCnt elements.
1608 * KERN_SUCCESS Extracted a send right.
1609 * KERN_INVALID_ARGUMENT The thread is null,
1610 * Invalid special port,
1611 * Illegal mask bit set.
1612 * KERN_FAILURE The thread is dead.
1616 thread_get_exception_ports(
1617 thread_act_t thr_act
,
1618 exception_mask_t exception_mask
,
1619 exception_mask_array_t masks
,
1620 mach_msg_type_number_t
* CountCnt
,
1621 exception_port_array_t ports
,
1622 exception_behavior_array_t behaviors
,
1623 thread_state_flavor_array_t flavors
)
1630 return KERN_INVALID_ARGUMENT
;
1632 if (exception_mask
& ~EXC_MASK_ALL
) {
1633 return KERN_INVALID_ARGUMENT
;
1637 if (!thr_act
->active
) {
1638 act_unlock(thr_act
);
1639 return KERN_FAILURE
;
1644 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
1645 if (exception_mask
& (1 << i
)) {
1646 for (j
= 0; j
< count
; j
++) {
1648 * search for an identical entry, if found
1649 * set corresponding mask for this exception.
1651 if (thr_act
->exc_actions
[i
].port
== ports
[j
] &&
1652 thr_act
->exc_actions
[i
].behavior
==behaviors
[j
]
1653 && thr_act
->exc_actions
[i
].flavor
== flavors
[j
])
1655 masks
[j
] |= (1 << i
);
1660 masks
[j
] = (1 << i
);
1662 ipc_port_copy_send(thr_act
->exc_actions
[i
].port
);
1663 behaviors
[j
] = thr_act
->exc_actions
[i
].behavior
;
1664 flavors
[j
] = thr_act
->exc_actions
[i
].flavor
;
1666 if (count
>= *CountCnt
) {
1673 act_unlock(thr_act
);
1676 return KERN_SUCCESS
;
1677 }/* thread_get_exception_ports */
1680 task_get_exception_ports(
1682 exception_mask_t exception_mask
,
1683 exception_mask_array_t masks
,
1684 mach_msg_type_number_t
* CountCnt
,
1685 exception_port_array_t ports
,
1686 exception_behavior_array_t behaviors
,
1687 thread_state_flavor_array_t flavors
)
1693 if (task
== TASK_NULL
)
1694 return KERN_INVALID_ARGUMENT
;
1696 if (exception_mask
& ~EXC_MASK_ALL
) {
1697 return KERN_INVALID_ARGUMENT
;
1701 if (task
->itk_self
== IP_NULL
) {
1703 return KERN_FAILURE
;
1708 for (i
= FIRST_EXCEPTION
; i
< EXC_TYPES_COUNT
; i
++) {
1709 if (exception_mask
& (1 << i
)) {
1710 for (j
= 0; j
< count
; j
++) {
1712 * search for an identical entry, if found
1713 * set corresponding mask for this exception.
1715 if (task
->exc_actions
[i
].port
== ports
[j
] &&
1716 task
->exc_actions
[i
].behavior
== behaviors
[j
]
1717 && task
->exc_actions
[i
].flavor
== flavors
[j
])
1719 masks
[j
] |= (1 << i
);
1724 masks
[j
] = (1 << i
);
1726 ipc_port_copy_send(task
->exc_actions
[i
].port
);
1727 behaviors
[j
] = task
->exc_actions
[i
].behavior
;
1728 flavors
[j
] = task
->exc_actions
[i
].flavor
;
1730 if (count
> *CountCnt
) {
1740 return KERN_SUCCESS
;
1741 }/* task_get_exception_ports */