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@
23 * @OSF_FREE_COPYRIGHT@
26 * Copyright (c) 1993 The University of Utah and
27 * the Center for Software Science (CSS). All rights reserved.
29 * Permission to use, copy, modify and distribute this software and its
30 * documentation is hereby granted, provided that both the copyright
31 * notice and this permission notice appear in all copies of the
32 * software, derivative works or modified versions, and any portions
33 * thereof, and that both notices appear in supporting documentation.
35 * THE UNIVERSITY OF UTAH AND CSS ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
36 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSS DISCLAIM ANY LIABILITY OF
37 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 * CSS requests users of this software to return to css-dist@cs.utah.edu any
40 * improvements that they make and grant CSS redistribution rights.
42 * Author: Bryan Ford, University of Utah CSS
44 * Thread_Activation management routines
48 #include <task_swapper.h>
49 #include <mach/kern_return.h>
50 #include <mach/alert.h>
51 #include <kern/etap_macros.h>
52 #include <kern/mach_param.h>
53 #include <kern/zalloc.h>
54 #include <kern/thread.h>
55 #include <kern/thread_swap.h>
56 #include <kern/task.h>
57 #include <kern/task_swap.h>
58 #include <kern/thread_act.h>
59 #include <kern/thread_pool.h>
60 #include <kern/sched_prim.h>
61 #include <kern/misc_protos.h>
62 #include <kern/assert.h>
63 #include <kern/exception.h>
64 #include <kern/ipc_mig.h>
65 #include <kern/ipc_tt.h>
66 #include <kern/profile.h>
67 #include <kern/machine.h>
69 #include <kern/syscall_subr.h>
70 #include <kern/sync_lock.h>
72 #include <kern/mk_sp.h> /*** ??? fix so this can be removed ***/
73 #include <mach_prof.h>
77 * Debugging printf control
80 unsigned int watchacts
= 0 /* WA_ALL */
81 ; /* Do-it-yourself & patchable */
85 * Track the number of times we need to swapin a thread to deallocate it.
87 int act_free_swapin
= 0;
90 * Forward declarations for functions local to this file.
92 kern_return_t
act_abort( thread_act_t
, int);
93 void special_handler(ReturnHandler
*, thread_act_t
);
94 void nudge(thread_act_t
);
95 kern_return_t
act_set_state_locked(thread_act_t
, int,
97 mach_msg_type_number_t
);
98 kern_return_t
act_get_state_locked(thread_act_t
, int,
100 mach_msg_type_number_t
*);
101 void act_set_apc(thread_act_t
);
102 void act_clr_apc(thread_act_t
);
103 void act_user_to_kernel(thread_act_t
);
104 void act_ulock_release_all(thread_act_t thr_act
);
106 void install_special_handler_locked(thread_act_t
);
108 static zone_t thr_act_zone
;
111 * Thread interfaces accessed via a thread_activation:
116 * Internal routine to terminate a thread.
117 * Called with task locked.
120 thread_terminate_internal(
121 register thread_act_t thr_act
)
125 struct ipc_port
*iplock
;
129 thread_swap_disable(thr_act
);
130 #endif /* THREAD_SWAPPER */
132 thread
= act_lock_thread(thr_act
);
133 if (!thr_act
->active
) {
134 act_unlock_thread(thr_act
);
135 return(KERN_TERMINATED
);
138 act_disable_task_locked(thr_act
);
139 ret
= act_abort(thr_act
,FALSE
);
143 * Make sure this thread enters the kernel
145 if (thread
!= current_thread()) {
146 thread_hold(thr_act
);
147 act_unlock_thread(thr_act
);
149 if (thread_stop_wait(thread
))
150 thread_unstop(thread
);
154 (void)act_lock_thread(thr_act
);
155 thread_release(thr_act
);
157 #endif /* NCPUS > 1 */
159 act_unlock_thread(thr_act
);
164 * Terminate a thread. Called with nothing locked.
169 register thread_act_t thr_act
)
174 if (thr_act
== THR_ACT_NULL
)
175 return KERN_INVALID_ARGUMENT
;
177 task
= thr_act
->task
;
178 if (((task
== kernel_task
) || (thr_act
->kernel_loaded
== TRUE
))
179 && (current_act() != thr_act
)) {
180 return(KERN_FAILURE
);
184 * Take the task lock and then call the internal routine
185 * that terminates a thread (it needs the task locked).
188 ret
= thread_terminate_internal(thr_act
);
192 * If a kernel thread is terminating itself, force an AST here.
193 * Kernel threads don't normally pass through the AST checking
194 * code - and all threads finish their own termination in the
195 * special handler APC.
197 if (((thr_act
->task
== kernel_task
) || (thr_act
->kernel_loaded
== TRUE
))
198 && (current_act() == thr_act
)) {
199 ast_taken(FALSE
, AST_APC
, 0);
200 panic("thread_terminate(): returning from ast_taken() for %x kernel activation\n", thr_act
);
209 * Suspend execution of the specified thread.
210 * This is a recursive-style suspension of the thread, a count of
211 * suspends is maintained.
213 * Called with thr_act locked "appropriately" for synchrony with
214 * RPC (see act_lock_thread()). Returns same way.
218 register thread_act_t thr_act
)
220 if (thr_act
->suspend_count
++ == 0) {
221 install_special_handler(thr_act
);
227 * Decrement internal suspension count for thr_act, setting thread
228 * runnable when count falls to zero.
230 * Called with thr_act locked "appropriately" for synchrony
231 * with RPC (see act_lock_thread()).
235 register thread_act_t thr_act
)
237 if( thr_act
->suspend_count
&&
238 (--thr_act
->suspend_count
== 0) )
244 register thread_act_t thr_act
)
248 if (thr_act
== THR_ACT_NULL
) {
249 return(KERN_INVALID_ARGUMENT
);
251 thread
= act_lock_thread(thr_act
);
252 if (!thr_act
->active
) {
253 act_unlock_thread(thr_act
);
254 return(KERN_TERMINATED
);
256 if (thr_act
->user_stop_count
++ == 0 &&
257 thr_act
->suspend_count
++ == 0 ) {
258 install_special_handler(thr_act
);
260 thr_act
== thread
->top_act
&& thread
!= current_thread()) {
262 act_unlock_thread(thr_act
);
263 (void)thread_wait(thread
);
267 * No need to wait for target thread
269 act_unlock_thread(thr_act
);
274 * Thread is already suspended
276 act_unlock_thread(thr_act
);
278 return(KERN_SUCCESS
);
283 register thread_act_t thr_act
)
285 register kern_return_t ret
;
289 if (thr_act
== THR_ACT_NULL
)
290 return(KERN_INVALID_ARGUMENT
);
291 thread
= act_lock_thread(thr_act
);
294 if (thr_act
->active
) {
295 if (thr_act
->user_stop_count
> 0) {
296 if( --thr_act
->user_stop_count
== 0 ) {
297 --thr_act
->suspend_count
;
305 ret
= KERN_TERMINATED
;
306 act_unlock_thread( thr_act
);
311 * This routine walks toward the head of an RPC chain starting at
312 * a specified thread activation. An alert bit is set and a special
313 * handler is installed for each thread it encounters.
315 * The target thread act and thread shuttle are already locked.
319 register thread_act_t thr_act
,
320 unsigned alert_bits
)
326 * Chase the chain, setting alert bits and installing
327 * special handlers for each thread act.
329 /*** Not yet SMP safe ***/
330 /*** Worse, where's the activation locking as the chain is walked? ***/
331 for (next
= thr_act
; next
!= THR_ACT_NULL
; next
= next
->higher
) {
332 next
->alerts
|= alert_bits
;
333 install_special_handler_locked(next
);
336 return(KERN_SUCCESS
);
340 * thread_depress_abort:
342 * Prematurely abort priority depression if there is one.
345 thread_depress_abort(
346 register thread_act_t thr_act
)
348 register thread_t thread
;
349 kern_return_t result
;
350 sched_policy_t
*policy
;
353 if (thr_act
== THR_ACT_NULL
)
354 return (KERN_INVALID_ARGUMENT
);
356 thread
= act_lock_thread(thr_act
);
357 /* if activation is terminating, this operation is not meaningful */
358 if (!thr_act
->active
) {
359 act_unlock_thread(thr_act
);
361 return (KERN_TERMINATED
);
366 policy
= &sched_policy
[thread
->policy
];
367 thread_unlock(thread
);
370 result
= policy
->sp_ops
.sp_thread_depress_abort(policy
, thread
);
372 act_unlock_thread(thr_act
);
379 * Already locked: all RPC-related locks for thr_act (see
380 * act_lock_thread()).
383 act_abort( thread_act_t thr_act
, int chain_break
)
387 struct ipc_port
*iplock
= thr_act
->pool_port
;
390 etap_data_t probe_data
;
392 ETAP_DATA_LOAD(probe_data
[0], thr_act
);
393 ETAP_DATA_LOAD(probe_data
[1], thr_act
->thread
);
394 ETAP_PROBE_DATA(ETAP_P_ACT_ABORT
,
401 * If the target thread activation is not the head...
403 if ( thr_act
->thread
->top_act
!= thr_act
) {
405 * mark the activation for abort,
406 * update the suspend count,
407 * always install the special handler
409 install_special_handler(thr_act
);
411 #ifdef AGRESSIVE_ABORT
412 /* release state buffer for target's outstanding invocation */
413 if (unwind_invoke_state(thr_act
) != KERN_SUCCESS
) {
414 panic("unwind_invoke_state failure");
417 /* release state buffer for target's incoming invocation */
418 if (thr_act
->lower
!= THR_ACT_NULL
) {
419 if (unwind_invoke_state(thr_act
->lower
)
421 panic("unwind_invoke_state failure");
425 /* unlink target thread activation from shuttle chain */
426 if ( thr_act
->lower
== THR_ACT_NULL
) {
428 * This is the root thread activation of the chain.
429 * Unlink the root thread act from the bottom of
432 thr_act
->higher
->lower
= THR_ACT_NULL
;
435 * This thread act is in the middle of the chain.
436 * Unlink the thread act from the middle of the chain.
438 thr_act
->higher
->lower
= thr_act
->lower
;
439 thr_act
->lower
->higher
= thr_act
->higher
;
441 /* set the terminated bit for RPC return processing */
442 thr_act
->lower
->alerts
|= SERVER_TERMINATED
;
445 orphan
= thr_act
->higher
;
447 /* remove the activation from its thread pool */
448 /* (note: this is okay for "rooted threads," too) */
449 act_locked_act_set_thread_pool(thr_act
, IP_NULL
);
451 /* (just to be thorough) release the IP lock */
452 if (iplock
!= IP_NULL
) ip_unlock(iplock
);
454 /* release one more reference for a rooted thread */
455 if (iplock
== IP_NULL
) act_locked_act_deallocate(thr_act
);
457 /* Presumably, the only reference to this activation is
458 * now held by the caller of this routine. */
459 assert(thr_act
->ref_count
== 1);
460 #else /*AGRESSIVE_ABORT*/
461 /* If there is a lower activation in the RPC chain... */
462 if (thr_act
->lower
!= THR_ACT_NULL
) {
463 /* ...indicate the server activation was terminated */
464 thr_act
->lower
->alerts
|= SERVER_TERMINATED
;
466 /* Mark (and process) any orphaned activations */
467 orphan
= thr_act
->higher
;
468 #endif /*AGRESSIVE_ABORT*/
470 /* indicate client of orphaned chain has been terminated */
471 orphan
->alerts
|= CLIENT_TERMINATED
;
474 * Set up posting of alert to headward portion of
477 /*** fix me -- orphan act is not locked ***/
478 post_alert(orphan
, ORPHANED
);
481 * Get attention of head of RPC chain.
483 nudge(thr_act
->thread
->top_act
);
484 return (KERN_SUCCESS
);
488 * If the target thread is the end of the chain, the thread
489 * has to be marked for abort and rip it out of any wait.
492 thread_lock(thr_act
->thread
);
493 if (thr_act
->thread
->top_act
== thr_act
) {
494 thr_act
->thread
->state
|= TH_ABORT
;
495 clear_wait_internal(thr_act
->thread
, THREAD_INTERRUPTED
);
496 thread_unlock(thr_act
->thread
);
498 install_special_handler(thr_act
);
506 register thread_act_t thr_act
)
511 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_act())
512 return (KERN_INVALID_ARGUMENT
);
514 * Lock the target thread and the current thread now,
515 * in case thread_halt() ends up being called below.
517 thread
= act_lock_thread(thr_act
);
518 if (!thr_act
->active
) {
519 act_unlock_thread(thr_act
);
520 return(KERN_TERMINATED
);
523 ret
= act_abort( thr_act
, FALSE
);
524 act_unlock_thread( thr_act
);
530 register thread_act_t thr_act
)
535 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_act())
536 return(KERN_INVALID_ARGUMENT
);
538 thread
= act_lock_thread(thr_act
);
539 if (!thr_act
->active
) {
540 act_unlock_thread(thr_act
);
541 return(KERN_TERMINATED
);
543 if (thread
->top_act
!= thr_act
) {
544 act_unlock_thread(thr_act
);
545 return(KERN_FAILURE
);
550 if ( thread
->at_safe_point
) {
552 * It's an abortable wait, clear it, then
553 * let the thread go and return successfully.
555 clear_wait_internal(thread
, THREAD_INTERRUPTED
);
556 thread_unlock(thread
);
557 act_unlock_thread(thr_act
);
563 * if not stopped at a safepoint, just let it go and return failure.
565 thread_unlock(thread
);
566 act_unlock_thread(thr_act
);
571 /*** backward compatibility hacks ***/
572 #include <mach/thread_info.h>
573 #include <mach/thread_special_ports.h>
574 #include <ipc/ipc_port.h>
575 #include <mach/thread_act_server.h>
579 thread_act_t thr_act
,
580 thread_flavor_t flavor
,
581 thread_info_t thread_info_out
,
582 mach_msg_type_number_t
*thread_info_count
)
584 register thread_t thread
;
585 kern_return_t result
;
587 if (thr_act
== THR_ACT_NULL
)
588 return (KERN_INVALID_ARGUMENT
);
590 thread
= act_lock_thread(thr_act
);
591 if (!thr_act
->active
) {
592 act_unlock_thread(thr_act
);
594 return (KERN_TERMINATED
);
597 result
= thread_info_shuttle(thr_act
, flavor
,
598 thread_info_out
, thread_info_count
);
600 act_unlock_thread(thr_act
);
606 * Routine: thread_get_special_port [kernel call]
608 * Clones a send right for one of the thread's
613 * KERN_SUCCESS Extracted a send right.
614 * KERN_INVALID_ARGUMENT The thread is null.
615 * KERN_FAILURE The thread is dead.
616 * KERN_INVALID_ARGUMENT Invalid special port.
620 thread_get_special_port(
621 thread_act_t thr_act
,
630 if (watchacts
& WA_PORT
)
631 printf("thread_get_special_port(thr_act=%x, which=%x port@%x=%x\n",
632 thr_act
, which
, portp
, (portp
? *portp
: 0));
633 #endif /* MACH_ASSERT */
636 return KERN_INVALID_ARGUMENT
;
637 thread
= act_lock_thread(thr_act
);
639 case THREAD_KERNEL_PORT
:
640 whichp
= &thr_act
->ith_sself
;
644 act_unlock_thread(thr_act
);
645 return KERN_INVALID_ARGUMENT
;
648 if (!thr_act
->active
) {
649 act_unlock_thread(thr_act
);
653 port
= ipc_port_copy_send(*whichp
);
654 act_unlock_thread(thr_act
);
661 * Routine: thread_set_special_port [kernel call]
663 * Changes one of the thread's special ports,
664 * setting it to the supplied send right.
666 * Nothing locked. If successful, consumes
667 * the supplied send right.
669 * KERN_SUCCESS Changed the special port.
670 * KERN_INVALID_ARGUMENT The thread is null.
671 * KERN_FAILURE The thread is dead.
672 * KERN_INVALID_ARGUMENT Invalid special port.
676 thread_set_special_port(
677 thread_act_t thr_act
,
686 if (watchacts
& WA_PORT
)
687 printf("thread_set_special_port(thr_act=%x,which=%x,port=%x\n",
688 thr_act
, which
, port
);
689 #endif /* MACH_ASSERT */
692 return KERN_INVALID_ARGUMENT
;
694 thread
= act_lock_thread(thr_act
);
696 case THREAD_KERNEL_PORT
:
697 whichp
= &thr_act
->ith_self
;
701 act_unlock_thread(thr_act
);
702 return KERN_INVALID_ARGUMENT
;
705 if (!thr_act
->active
) {
706 act_unlock_thread(thr_act
);
712 act_unlock_thread(thr_act
);
715 ipc_port_release_send(old
);
720 * thread state should always be accessible by locking the thread
721 * and copying it. The activation messes things up so for right
722 * now if it's not the top of the chain, use a special handler to
723 * get the information when the shuttle returns to the activation.
727 register thread_act_t thr_act
,
729 thread_state_t state
, /* pointer to OUT array */
730 mach_msg_type_number_t
*state_count
) /*IN/OUT*/
733 thread_t thread
, nthread
;
735 #if 0 /* Grenoble - why?? */
736 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_act())
738 if (thr_act
== THR_ACT_NULL
)
740 return (KERN_INVALID_ARGUMENT
);
742 thread
= act_lock_thread(thr_act
);
743 if (!thr_act
->active
) {
744 act_unlock_thread(thr_act
);
745 return(KERN_TERMINATED
);
748 thread_hold(thr_act
);
750 if (!thread
|| thr_act
!= thread
->top_act
)
752 act_unlock_thread(thr_act
);
753 (void)thread_stop_wait(thread
);
754 nthread
= act_lock_thread(thr_act
);
755 if (nthread
== thread
)
757 thread_unstop(thread
);
760 ret
= act_machine_get_state(thr_act
, flavor
,
762 if (thread
&& thr_act
== thread
->top_act
)
763 thread_unstop(thread
);
764 thread_release(thr_act
);
765 act_unlock_thread(thr_act
);
771 * Change thread's machine-dependent state. Called with nothing
772 * locked. Returns same way.
776 register thread_act_t thr_act
,
778 thread_state_t state
,
779 mach_msg_type_number_t state_count
)
782 thread_t thread
, nthread
;
784 #if 0 /* Grenoble - why?? */
785 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_act())
787 if (thr_act
== THR_ACT_NULL
)
789 return (KERN_INVALID_ARGUMENT
);
791 * We have no kernel activations, so Utah's MO fails for signals etc.
793 * If we're blocked in the kernel, use non-blocking method, else
794 * pass locked thr_act+thread in to "normal" act_[gs]et_state().
797 thread
= act_lock_thread(thr_act
);
798 if (!thr_act
->active
) {
799 act_unlock_thread(thr_act
);
800 return(KERN_TERMINATED
);
803 thread_hold(thr_act
);
805 if (!thread
|| thr_act
!= thread
->top_act
)
807 act_unlock_thread(thr_act
);
808 (void)thread_stop_wait(thread
);
809 nthread
= act_lock_thread(thr_act
);
810 if (nthread
== thread
)
812 thread_unstop(thread
);
815 ret
= act_machine_set_state(thr_act
, flavor
,
817 if (thread
&& thr_act
== thread
->top_act
)
818 thread_unstop(thread
);
819 thread_release(thr_act
);
820 act_unlock_thread(thr_act
);
826 * Kernel-internal "thread" interfaces used outside this file:
831 thread_act_t source_thr_act
,
832 thread_act_t target_thr_act
)
835 thread_t thread
, nthread
;
837 if (target_thr_act
== THR_ACT_NULL
|| target_thr_act
== current_act())
838 return (KERN_INVALID_ARGUMENT
);
840 thread
= act_lock_thread(target_thr_act
);
841 if (!target_thr_act
->active
) {
842 act_unlock_thread(target_thr_act
);
843 return(KERN_TERMINATED
);
846 thread_hold(target_thr_act
);
848 if (!thread
|| target_thr_act
!= thread
->top_act
)
850 act_unlock_thread(target_thr_act
);
851 (void)thread_stop_wait(thread
);
852 nthread
= act_lock_thread(target_thr_act
);
853 if (nthread
== thread
)
855 thread_unstop(thread
);
858 ret
= act_thread_dup(source_thr_act
, target_thr_act
);
859 if (thread
&& target_thr_act
== thread
->top_act
)
860 thread_unstop(thread
);
861 thread_release(target_thr_act
);
862 act_unlock_thread(target_thr_act
);
871 * Set the status of the specified thread.
872 * Called with (and returns with) no locks held.
876 thread_act_t thr_act
,
878 thread_state_t tstate
,
879 mach_msg_type_number_t count
)
884 thread
= act_lock_thread(thr_act
);
886 assert(thread
->top_act
== thr_act
);
887 kr
= act_machine_set_state(thr_act
, flavor
, tstate
, count
);
888 act_unlock_thread(thr_act
);
895 * Get the status of the specified thread.
899 thread_act_t thr_act
,
901 thread_state_t tstate
,
902 mach_msg_type_number_t
*count
)
907 thread
= act_lock_thread(thr_act
);
909 assert(thread
->top_act
== thr_act
);
910 kr
= act_machine_get_state(thr_act
, flavor
, tstate
, count
);
911 act_unlock_thread(thr_act
);
916 * Kernel-internal thread_activation interfaces used outside this file:
920 * act_init() - Initialize activation handling code
925 thr_act_zone
= zinit(
926 sizeof(struct thread_activation
),
927 ACT_MAX
* sizeof(struct thread_activation
), /* XXX */
928 ACT_CHUNK
* sizeof(struct thread_activation
),
935 * act_create - Create a new activation in a specific task.
938 act_create(task_t task
,
939 thread_act_t
*new_act
)
941 thread_act_t thr_act
;
945 thr_act
= (thread_act_t
)zalloc(thr_act_zone
);
947 return(KERN_RESOURCE_SHORTAGE
);
950 if (watchacts
& WA_ACT_LNK
)
951 printf("act_create(task=%x,thr_act@%x=%x)\n",
952 task
, new_act
, thr_act
);
953 #endif /* MACH_ASSERT */
955 /* Start by zeroing everything; then init non-zero items only */
956 bzero((char *)thr_act
, sizeof(*thr_act
));
961 * Take care of the uthread allocation
962 * do it early in order to make KERN_RESOURCE_SHORTAGE
964 * uthread_alloc() will bzero the storage allocated.
966 extern void *uthread_alloc(void);
967 thr_act
->uthread
= uthread_alloc();
968 if(thr_act
->uthread
== 0) {
969 /* Put the thr_act back on the thr_act zone */
970 zfree(thr_act_zone
, (vm_offset_t
)thr_act
);
971 return(KERN_RESOURCE_SHORTAGE
);
974 #endif /* MACH_BSD */
977 * Start with one reference for the caller and one for the
980 act_lock_init(thr_act
);
981 thr_act
->ref_count
= 2;
983 /* Latch onto the task. */
984 thr_act
->task
= task
;
985 task_reference(task
);
987 /* Initialize sigbufp for High-Watermark buffer allocation */
988 thr_act
->r_sigbufp
= (routine_descriptor_t
) &thr_act
->r_sigbuf
;
989 thr_act
->r_sigbuf_size
= sizeof(thr_act
->r_sigbuf
);
992 thr_act
->swap_state
= TH_SW_IN
;
994 thr_act
->kernel_stack_swapped_in
= TRUE
;
995 #endif /* MACH_ASSERT */
996 #endif /* THREAD_SWAPPER */
998 /* special_handler will always be last on the returnhandlers list. */
999 thr_act
->special_handler
.next
= 0;
1000 thr_act
->special_handler
.handler
= special_handler
;
1003 thr_act
->act_profiled
= FALSE
;
1004 thr_act
->act_profiled_own
= FALSE
;
1005 thr_act
->profil_buffer
= NULLPROFDATA
;
1008 /* Initialize the held_ulocks queue as empty */
1009 queue_init(&thr_act
->held_ulocks
);
1011 /* Inherit the profiling status of the parent task */
1012 act_prof_init(thr_act
, task
);
1014 ipc_thr_act_init(task
, thr_act
);
1015 act_machine_create(task
, thr_act
);
1018 * If thr_act created in kernel-loaded task, alter its saved
1019 * state to so indicate
1021 if (task
->kernel_loaded
) {
1022 act_user_to_kernel(thr_act
);
1025 /* Cache the task's map and take a reference to it */
1029 /* Inline vm_map_reference cause we don't want to increment res_count */
1030 mutex_lock(&map
->s_lock
);
1032 assert(map
->res_count
> 0);
1033 assert(map
->ref_count
>= map
->res_count
);
1034 #endif /* TASK_SWAPPER */
1036 mutex_unlock(&map
->s_lock
);
1039 return KERN_SUCCESS
;
1043 * act_free - called when an thr_act's ref_count drops to zero.
1045 * This can only happen after the activation has been reaped, and
1046 * all other references to it have gone away. We can now release
1047 * the last critical resources, unlink the activation from the
1048 * task, and release the reference on the thread shuttle itself.
1050 * Called with activation locked.
1053 int dangerous_bzero
= 1; /* paranoia & safety */
1057 act_free(thread_act_t thr_act
)
1065 if (watchacts
& WA_EXIT
)
1066 printf("act_free(%x(%d)) thr=%x tsk=%x(%d) pport=%x%sactive\n",
1067 thr_act
, thr_act
->ref_count
, thr_act
->thread
,
1069 thr_act
->task
? thr_act
->task
->ref_count
: 0,
1071 thr_act
->active
? " " : " !");
1072 #endif /* MACH_ASSERT */
1076 assert(thr_act
->kernel_stack_swapped_in
);
1077 #endif /* THREAD_SWAPPER */
1079 assert(!thr_act
->active
);
1080 assert(!thr_act
->pool_port
);
1082 task
= thr_act
->task
;
1085 if (thr
= thr_act
->thread
) {
1086 time_value_t user_time
, system_time
;
1088 thread_read_times(thr
, &user_time
, &system_time
);
1089 time_value_add(&task
->total_user_time
, &user_time
);
1090 time_value_add(&task
->total_system_time
, &system_time
);
1092 /* Unlink the thr_act from the task's thr_act list,
1093 * so it doesn't appear in calls to task_threads and such.
1094 * The thr_act still keeps its ref on the task, however.
1096 queue_remove(&task
->thr_acts
, thr_act
, thread_act_t
, thr_acts
);
1097 thr_act
->thr_acts
.next
= NULL
;
1098 task
->thr_act_count
--;
1102 * Thread is supposed to be unswappable by now...
1104 assert(thr_act
->swap_state
== TH_SW_UNSWAPPABLE
||
1105 !thread_swap_unwire_stack
);
1106 #endif /* THREAD_SWAPPER */
1108 task
->res_act_count
--;
1110 task_deallocate(task
);
1111 thread_deallocate(thr
);
1112 act_machine_destroy(thr_act
);
1115 * Must have never really gotten started
1116 * no unlinking from the task and no need
1117 * to free the shuttle.
1120 task_deallocate(task
);
1123 sigbuf_dealloc(thr_act
);
1124 act_prof_deallocate(thr_act
);
1125 ipc_thr_act_terminate(thr_act
);
1128 * Drop the cached map reference.
1129 * Inline version of vm_map_deallocate() because we
1130 * don't want to decrement the map's residence count here.
1133 mutex_lock(&map
->s_lock
);
1135 assert(map
->res_count
>= 0);
1136 assert(map
->ref_count
> map
->res_count
);
1137 #endif /* TASK_SWAPPER */
1138 ref
= --map
->ref_count
;
1139 mutex_unlock(&map
->s_lock
);
1141 vm_map_destroy(map
);
1146 * Free uthread BEFORE the bzero.
1147 * Not doing so will result in a leak.
1149 extern void uthread_free(void *);
1150 void *ut
= thr_act
->uthread
;
1151 thr_act
->uthread
= 0;
1154 #endif /* MACH_BSD */
1157 if (dangerous_bzero
) /* dangerous if we're still using it! */
1158 bzero((char *)thr_act
, sizeof(*thr_act
));
1159 #endif /* MACH_ASSERT */
1160 /* Put the thr_act back on the thr_act zone */
1161 zfree(thr_act_zone
, (vm_offset_t
)thr_act
);
1166 * act_attach - Attach an thr_act to the top of a thread ("push the stack").
1168 * The thread_shuttle must be either the current one or a brand-new one.
1169 * Assumes the thr_act is active but not in use, also, that if it is
1170 * attached to an thread_pool (i.e. the thread_pool pointer is nonzero),
1171 * the thr_act has already been taken off the thread_pool's list.
1173 * Already locked: thr_act plus "appropriate" thread-related locks
1174 * (see act_lock_thread()).
1178 thread_act_t thr_act
,
1180 unsigned init_alert_mask
)
1185 assert(thread
== current_thread() || thread
->top_act
== THR_ACT_NULL
);
1186 if (watchacts
& WA_ACT_LNK
)
1187 printf("act_attach(thr_act %x(%d) thread %x(%d) mask %d)\n",
1188 thr_act
, thr_act
->ref_count
, thread
, thread
->ref_count
,
1190 #endif /* MACH_ASSERT */
1193 * Chain the thr_act onto the thread's thr_act stack.
1194 * Set mask and auto-propagate alerts from below.
1196 thr_act
->ref_count
++;
1197 thr_act
->thread
= thread
;
1198 thr_act
->higher
= THR_ACT_NULL
; /*safety*/
1199 thr_act
->alerts
= 0;
1200 thr_act
->alert_mask
= init_alert_mask
;
1201 lower
= thr_act
->lower
= thread
->top_act
;
1203 if (lower
!= THR_ACT_NULL
) {
1204 lower
->higher
= thr_act
;
1205 thr_act
->alerts
= (lower
->alerts
& init_alert_mask
);
1208 thread
->top_act
= thr_act
;
1214 * Remove the current thr_act from the top of the current thread, i.e.
1215 * "pop the stack". Assumes already locked: thr_act plus "appropriate"
1216 * thread-related locks (see act_lock_thread).
1220 thread_act_t cur_act
)
1222 thread_t cur_thread
= cur_act
->thread
;
1225 if (watchacts
& (WA_EXIT
|WA_ACT_LNK
))
1226 printf("act_detach: thr_act %x(%d), thrd %x(%d) task=%x(%d)\n",
1227 cur_act
, cur_act
->ref_count
,
1228 cur_thread
, cur_thread
->ref_count
,
1230 cur_act
->task
? cur_act
->task
->ref_count
: 0);
1231 #endif /* MACH_ASSERT */
1233 /* Unlink the thr_act from the thread's thr_act stack */
1234 cur_thread
->top_act
= cur_act
->lower
;
1235 cur_act
->thread
= 0;
1236 cur_act
->ref_count
--;
1237 assert(cur_act
->ref_count
> 0);
1239 thread_pool_put_act(cur_act
);
1242 cur_act
->lower
= cur_act
->higher
= THR_ACT_NULL
;
1243 if (cur_thread
->top_act
)
1244 cur_thread
->top_act
->higher
= THR_ACT_NULL
;
1245 #endif /* MACH_ASSERT */
1252 * Synchronize a thread operation with RPC. Called with nothing
1253 * locked. Returns with thr_act locked, plus one of four
1254 * combinations of other locks held:
1255 * none - for new activation not yet associated with thread_pool
1257 * rpc_lock(thr_act->thread) only - for base activation (one
1258 * without pool_port)
1259 * ip_lock(thr_act->pool_port) only - for empty activation (one
1260 * with no associated shuttle)
1261 * both locks - for "active" activation (has shuttle, lives
1263 * If thr_act has an associated shuttle, this function returns
1264 * its address. Otherwise it returns zero.
1268 thread_act_t thr_act
)
1273 * Allow the shuttle cloning code (q.v., when it
1274 * exists :-}) to obtain ip_lock()'s while holding
1279 pport
= thr_act
->pool_port
;
1280 if (!pport
|| ip_lock_try(pport
)) {
1281 if (!thr_act
->thread
)
1283 if (rpc_lock_try(thr_act
->thread
))
1288 act_unlock(thr_act
);
1291 return (thr_act
->thread
);
1295 * Unsynchronize with RPC (i.e., undo an act_lock_thread() call).
1296 * Called with thr_act locked, plus thread locks held that are
1297 * "correct" for thr_act's state. Returns with nothing locked.
1300 act_unlock_thread(thread_act_t thr_act
)
1302 if (thr_act
->thread
)
1303 rpc_unlock(thr_act
->thread
);
1304 if (thr_act
->pool_port
)
1305 ip_unlock(thr_act
->pool_port
);
1306 act_unlock(thr_act
);
1310 * Synchronize with RPC given a pointer to a shuttle (instead of an
1311 * activation). Called with nothing locked; returns with all
1312 * "appropriate" thread-related locks held (see act_lock_thread()).
1318 thread_act_t thr_act
;
1322 thr_act
= thread
->top_act
;
1325 if (!act_lock_try(thr_act
)) {
1330 if (thr_act
->pool_port
&&
1331 !ip_lock_try(thr_act
->pool_port
)) {
1333 act_unlock(thr_act
);
1343 * Unsynchronize with RPC starting from a pointer to a shuttle.
1344 * Called with RPC-related locks held that are appropriate to
1345 * shuttle's state; any activation is also locked.
1351 thread_act_t thr_act
;
1353 if (thr_act
= thread
->top_act
) {
1354 if (thr_act
->pool_port
)
1355 ip_unlock(thr_act
->pool_port
);
1356 act_unlock(thr_act
);
1364 * If a new activation is given, switch to it. If not,
1365 * switch to the lower activation (pop). Returns the old
1366 * activation. This is for RPC support.
1373 thread_act_t old
, new;
1378 disable_preemption();
1381 thread
= current_thread();
1384 * Find the old and new activation for switch.
1386 old
= thread
->top_act
;
1390 new->thread
= thread
;
1396 assert(new != THR_ACT_NULL
);
1398 assert(new->swap_state
!= TH_SW_OUT
&&
1399 new->swap_state
!= TH_SW_COMING_IN
);
1400 #endif /* THREAD_SWAPPER */
1402 assert(cpu_data
[cpu
].active_thread
== thread
);
1403 active_kloaded
[cpu
] = (new->kernel_loaded
) ? new : 0;
1405 /* This is where all the work happens */
1406 machine_switch_act(thread
, old
, new, cpu
);
1409 * Push or pop an activation on the chain.
1412 act_attach(new, thread
, 0);
1418 enable_preemption();
1424 * install_special_handler
1425 * Install the special returnhandler that handles suspension and
1426 * termination, if it hasn't been installed already.
1428 * Already locked: RPC-related locks for thr_act, but not
1429 * scheduling lock (thread_lock()) of the associated thread.
1432 install_special_handler(
1433 thread_act_t thr_act
)
1436 thread_t thread
= thr_act
->thread
;
1439 if (watchacts
& WA_ACT_HDLR
)
1440 printf("act_%x: install_special_hdlr(%x)\n",current_act(),thr_act
);
1441 #endif /* MACH_ASSERT */
1444 thread_lock(thread
);
1445 install_special_handler_locked(thr_act
);
1446 thread_unlock(thread
);
1451 * install_special_handler_locked
1452 * Do the work of installing the special_handler.
1454 * Already locked: RPC-related locks for thr_act, plus the
1455 * scheduling lock (thread_lock()) of the associated thread.
1458 install_special_handler_locked(
1459 thread_act_t thr_act
)
1462 thread_t thread
= thr_act
->thread
;
1464 /* The work handler must always be the last ReturnHandler on the list,
1465 because it can do tricky things like detach the thr_act. */
1466 for (rh
= &thr_act
->handlers
; *rh
; rh
= &(*rh
)->next
)
1468 if (rh
!= &thr_act
->special_handler
.next
) {
1469 *rh
= &thr_act
->special_handler
;
1471 if (thread
&& thr_act
== thread
->top_act
) {
1473 * Temporarily undepress, so target has
1474 * a chance to do locking required to
1475 * block itself in special_handler().
1477 if (thread
->depress_priority
>= 0) {
1478 thread
->priority
= thread
->depress_priority
;
1481 * Use special value -2 to indicate need
1482 * to redepress priority in special_handler
1485 thread
->depress_priority
= -2;
1486 compute_priority(thread
, FALSE
);
1489 act_set_apc(thr_act
);
1494 * These two routines will be enhanced over time to call the general handler registration
1495 * mechanism used by special handlers and alerts. They are hack in for now to avoid
1496 * having to export the gory details of ASTs to the BSD code right now.
1498 extern thread_apc_handler_t bsd_ast
;
1502 thread_act_t thr_act
,
1503 thread_apc_handler_t apc
)
1505 assert(apc
== bsd_ast
);
1506 thread_ast_set(thr_act
, AST_BSD
);
1507 if (thr_act
== current_act())
1508 ast_propagate(thr_act
->ast
);
1509 return KERN_SUCCESS
;
1514 thread_act_t thr_act
,
1515 thread_apc_handler_t apc
)
1517 assert(apc
== bsd_ast
);
1518 thread_ast_clear(thr_act
, AST_BSD
);
1519 if (thr_act
== current_act())
1521 return KERN_SUCCESS
;
1525 * act_set_thread_pool - Assign an activation to a specific thread_pool.
1526 * Fails if the activation is already assigned to another pool.
1527 * If thread_pool == 0, we remove the thr_act from its thread_pool.
1529 * Called the port containing thread_pool already locked.
1530 * Returns the same way.
1532 kern_return_t
act_set_thread_pool(
1533 thread_act_t thr_act
,
1534 ipc_port_t pool_port
)
1536 thread_pool_t thread_pool
;
1539 if (watchacts
& WA_ACT_LNK
)
1540 printf("act_set_thread_pool: %x(%d) -> %x\n",
1541 thr_act
, thr_act
->ref_count
, thread_pool
);
1542 #endif /* MACH_ASSERT */
1544 if (pool_port
== 0) {
1547 if (thr_act
->pool_port
== 0)
1548 return KERN_SUCCESS
;
1549 thread_pool
= &thr_act
->pool_port
->ip_thread_pool
;
1551 for (lact
= &thread_pool
->thr_acts
; *lact
;
1552 lact
= &((*lact
)->thread_pool_next
)) {
1553 if (thr_act
== *lact
) {
1554 *lact
= thr_act
->thread_pool_next
;
1559 thr_act
->pool_port
= 0;
1560 thr_act
->thread_pool_next
= 0;
1561 act_unlock(thr_act
);
1562 act_deallocate(thr_act
);
1563 return KERN_SUCCESS
;
1565 if (thr_act
->pool_port
!= pool_port
) {
1566 thread_pool
= &pool_port
->ip_thread_pool
;
1567 if (thr_act
->pool_port
!= 0) {
1569 if (watchacts
& WA_ACT_LNK
)
1570 printf("act_set_thread_pool found %x!\n",
1571 thr_act
->pool_port
);
1572 #endif /* MACH_ASSERT */
1573 return(KERN_FAILURE
);
1576 thr_act
->pool_port
= pool_port
;
1578 /* The pool gets a ref to the activation -- have
1579 * to inline operation because thr_act is already
1582 act_locked_act_reference(thr_act
);
1584 /* If it is available,
1585 * add it to the thread_pool's available-activation list.
1587 if ((thr_act
->thread
== 0) && (thr_act
->suspend_count
== 0)) {
1588 thr_act
->thread_pool_next
= thread_pool
->thr_acts
;
1589 pool_port
->ip_thread_pool
.thr_acts
= thr_act
;
1590 if (thread_pool
->waiting
)
1591 thread_pool_wakeup(thread_pool
);
1593 act_unlock(thr_act
);
1596 return KERN_SUCCESS
;
1600 * act_locked_act_set_thread_pool- Assign activation to a specific thread_pool.
1601 * Fails if the activation is already assigned to another pool.
1602 * If thread_pool == 0, we remove the thr_act from its thread_pool.
1604 * Called the port containing thread_pool already locked.
1605 * Also called with the thread activation locked.
1606 * Returns the same way.
1608 * This routine is the same as `act_set_thread_pool()' except that it does
1609 * not call `act_deallocate(),' which unconditionally tries to obtain the
1610 * thread activation lock.
1612 kern_return_t
act_locked_act_set_thread_pool(
1613 thread_act_t thr_act
,
1614 ipc_port_t pool_port
)
1616 thread_pool_t thread_pool
;
1619 if (watchacts
& WA_ACT_LNK
)
1620 printf("act_set_thread_pool: %x(%d) -> %x\n",
1621 thr_act
, thr_act
->ref_count
, thread_pool
);
1622 #endif /* MACH_ASSERT */
1624 if (pool_port
== 0) {
1627 if (thr_act
->pool_port
== 0)
1628 return KERN_SUCCESS
;
1629 thread_pool
= &thr_act
->pool_port
->ip_thread_pool
;
1631 for (lact
= &thread_pool
->thr_acts
; *lact
;
1632 lact
= &((*lact
)->thread_pool_next
)) {
1633 if (thr_act
== *lact
) {
1634 *lact
= thr_act
->thread_pool_next
;
1639 thr_act
->pool_port
= 0;
1640 thr_act
->thread_pool_next
= 0;
1641 act_locked_act_deallocate(thr_act
);
1642 return KERN_SUCCESS
;
1644 if (thr_act
->pool_port
!= pool_port
) {
1645 thread_pool
= &pool_port
->ip_thread_pool
;
1646 if (thr_act
->pool_port
!= 0) {
1648 if (watchacts
& WA_ACT_LNK
)
1649 printf("act_set_thread_pool found %x!\n",
1650 thr_act
->pool_port
);
1651 #endif /* MACH_ASSERT */
1652 return(KERN_FAILURE
);
1654 thr_act
->pool_port
= pool_port
;
1656 /* The pool gets a ref to the activation -- have
1657 * to inline operation because thr_act is already
1660 act_locked_act_reference(thr_act
);
1662 /* If it is available,
1663 * add it to the thread_pool's available-activation list.
1665 if ((thr_act
->thread
== 0) && (thr_act
->suspend_count
== 0)) {
1666 thr_act
->thread_pool_next
= thread_pool
->thr_acts
;
1667 pool_port
->ip_thread_pool
.thr_acts
= thr_act
;
1668 if (thread_pool
->waiting
)
1669 thread_pool_wakeup(thread_pool
);
1673 return KERN_SUCCESS
;
1677 * Activation control support routines internal to this file:
1681 * act_execute_returnhandlers() - does just what the name says
1683 * This is called by system-dependent code when it detects that
1684 * thr_act->handlers is non-null while returning into user mode.
1685 * Activations linked onto an thread_pool always have null thr_act->handlers,
1686 * so RPC entry paths need not check it.
1688 void act_execute_returnhandlers(
1693 thread_act_t thr_act
= current_act();
1696 if (watchacts
& WA_ACT_HDLR
)
1697 printf("execute_rtn_hdlrs: thr_act=%x\n", thr_act
);
1698 #endif /* MACH_ASSERT */
1701 act_clr_apc(thr_act
);
1706 /* Grab the next returnhandler */
1707 thread
= act_lock_thread(thr_act
);
1709 thread_lock(thread
);
1710 rh
= thr_act
->handlers
;
1712 thread_unlock(thread
);
1714 act_unlock_thread(thr_act
);
1717 thr_act
->handlers
= rh
->next
;
1718 thread_unlock(thread
);
1720 act_unlock_thread(thr_act
);
1723 if (watchacts
& WA_ACT_HDLR
)
1724 printf( (rh
== &thr_act
->special_handler
) ?
1725 "\tspecial_handler\n" : "\thandler=%x\n",
1727 #endif /* MACH_ASSERT */
1730 (*rh
->handler
)(rh
, thr_act
);
1735 * special_handler_continue
1737 * Continuation routine for the special handler blocks. It checks
1738 * to see whether there has been any new suspensions. If so, it
1739 * installs the special handler again. Otherwise, it checks to see
1740 * if the current depression needs to be re-instated (it may have
1741 * been temporarily removed in order to get to this point in a hurry).
1744 special_handler_continue(void)
1746 thread_act_t cur_act
= current_act();
1747 thread_t thread
= cur_act
->thread
;
1750 if (cur_act
->suspend_count
)
1751 install_special_handler(cur_act
);
1754 thread_lock(thread
);
1755 if (thread
->depress_priority
== -2) {
1757 * We were temporarily undepressed by
1758 * install_special_handler; restore priority
1761 thread
->depress_priority
= thread
->priority
;
1762 thread
->priority
= thread
->sched_pri
= DEPRESSPRI
;
1764 thread_unlock(thread
);
1767 thread_exception_return();
1771 * special_handler - handles suspension, termination. Called
1772 * with nothing locked. Returns (if it returns) the same way.
1777 thread_act_t cur_act
)
1781 thread_t thread
= act_lock_thread(cur_act
);
1782 unsigned alert_bits
;
1783 exception_data_type_t
1784 codes
[EXCEPTION_CODE_MAX
];
1786 kern_return_t exc_kr
;
1788 assert(thread
!= THREAD_NULL
);
1790 if (watchacts
& WA_ACT_HDLR
)
1791 printf("\t\tspecial_handler(thr_act=%x(%d))\n", cur_act
,
1792 (cur_act
? cur_act
->ref_count
: 0));
1793 #endif /* MACH_ASSERT */
1797 thread_lock(thread
);
1798 thread
->state
&= ~TH_ABORT
; /* clear any aborts */
1799 thread_unlock(thread
);
1803 * If someone has killed this invocation,
1804 * invoke the return path with a terminated exception.
1806 if (!cur_act
->active
) {
1807 act_unlock_thread(cur_act
);
1808 act_machine_return(KERN_TERMINATED
);
1811 #ifdef CALLOUT_RPC_MODEL
1813 * JMM - We don't intend to support this RPC model in Darwin.
1814 * We will support inheritance through chains of activations
1815 * on shuttles, but it will be universal and not just for RPC.
1816 * As such, each activation will always have a base shuttle.
1817 * Our RPC model will probably even support the notion of
1818 * alerts (thrown up the chain of activations to affect the
1819 * work done on our behalf), but the unlinking of the shuttles
1820 * will be completely difference because we will never have
1824 /* strip server terminated bit */
1825 alert_bits
= cur_act
->alerts
& (~SERVER_TERMINATED
);
1827 /* clear server terminated bit */
1828 cur_act
->alerts
&= ~SERVER_TERMINATED
;
1832 * currently necessary to coordinate with the exception
1835 act_unlock_thread(cur_act
);
1837 /* upcall exception/alert port */
1838 codes
[0] = alert_bits
;
1841 * Exception makes a lot of assumptions. If there is no
1842 * exception handler or the exception reply is broken, the
1843 * thread will be terminated and exception will not return. If
1844 * we decide we don't like that behavior, we need to check
1845 * for the existence of an exception port before we call
1848 exc_kr
= exception( EXC_RPC_ALERT
, codes
, 1 );
1850 /* clear the orphaned and time constraint indications */
1851 cur_act
->alerts
&= ~(ORPHANED
| TIME_CONSTRAINT_UNSATISFIED
);
1853 /* if this orphaned activation should be terminated... */
1854 if (exc_kr
== KERN_RPC_TERMINATE_ORPHAN
) {
1856 * ... terminate the activation
1858 * This is done in two steps. First, the activation is
1859 * disabled (prepared for termination); second, the
1860 * `special_handler()' is executed again -- this time
1861 * to terminate the activation.
1862 * (`act_disable_task_locked()' arranges for the
1863 * additional execution of the `special_handler().')
1867 thread_swap_disable(cur_act
);
1868 #endif /* THREAD_SWAPPER */
1870 /* acquire appropriate locks */
1871 task_lock(cur_act
->task
);
1872 act_lock_thread(cur_act
);
1874 /* detach the activation from its task */
1875 kr
= act_disable_task_locked(cur_act
);
1876 assert( kr
== KERN_SUCCESS
);
1879 task_unlock(cur_act
->task
);
1882 /* acquire activation lock again (released below) */
1883 act_lock_thread(cur_act
);
1885 thread_lock(thread
);
1886 if (thread
->depress_priority
== -2) {
1888 * We were temporarily undepressed by
1889 * install_special_handler; restore priority
1892 thread
->depress_priority
= thread
->priority
;
1893 thread
->priority
= thread
->sched_pri
= DEPRESSPRI
;
1895 thread_unlock(thread
);
1899 #endif /* CALLOUT_RPC_MODEL */
1902 * If we're suspended, go to sleep and wait for someone to wake us up.
1904 if (cur_act
->suspend_count
) {
1905 if( cur_act
->handlers
== NULL
) {
1906 assert_wait((event_t
)&cur_act
->suspend_count
,
1908 act_unlock_thread(cur_act
);
1909 thread_block(special_handler_continue
);
1912 special_handler_continue();
1915 act_unlock_thread(cur_act
);
1919 * Try to nudge a thr_act into executing its returnhandler chain.
1920 * Ensures that the activation will execute its returnhandlers
1921 * before it next executes any of its user-level code.
1923 * Called with thr_act's act_lock() and "appropriate" thread-related
1924 * locks held. (See act_lock_thread().) Returns same way.
1927 nudge(thread_act_t thr_act
)
1930 if (watchacts
& WA_ACT_HDLR
)
1931 printf("\tact_%x: nudge(%x)\n", current_act(), thr_act
);
1932 #endif /* MACH_ASSERT */
1935 * Don't need to do anything at all if this thr_act isn't the topmost.
1937 if (thr_act
->thread
&& thr_act
->thread
->top_act
== thr_act
) {
1939 * If it's suspended, wake it up.
1940 * This should nudge it even on another CPU.
1942 thread_wakeup((event_t
)&thr_act
->suspend_count
);
1947 * Update activation that belongs to a task created via kernel_task_create().
1951 thread_act_t thr_act
)
1953 pcb_user_to_kernel(thr_act
);
1954 thr_act
->kernel_loading
= TRUE
;
1958 * Already locked: thr_act->task, RPC-related locks for thr_act
1960 * Detach an activation from its task, and prepare it to terminate
1964 act_disable_task_locked(
1965 thread_act_t thr_act
)
1967 thread_t thread
= thr_act
->thread
;
1968 task_t task
= thr_act
->task
;
1971 if (watchacts
& WA_EXIT
) {
1972 printf("act_%x: act_disable_tl(thr_act=%x(%d))%sactive task=%x(%d)",
1973 current_act(), thr_act
, thr_act
->ref_count
,
1974 (thr_act
->active
? " " : " !"),
1975 thr_act
->task
, thr_act
->task
? thr_act
->task
->ref_count
: 0);
1976 if (thr_act
->pool_port
)
1977 printf(", pool_port %x", thr_act
->pool_port
);
1979 (void) dump_act(thr_act
);
1981 #endif /* MACH_ASSERT */
1983 /* This will allow no more control ops on this thr_act. */
1984 thr_act
->active
= 0;
1985 ipc_thr_act_disable(thr_act
);
1987 /* Clean-up any ulocks that are still owned by the thread
1988 * activation (acquired but not released or handed-off).
1990 act_ulock_release_all(thr_act
);
1992 /* When the special_handler gets executed,
1993 * it will see the terminated condition and exit
1996 install_special_handler(thr_act
);
1999 /* If the target happens to be suspended,
2000 * give it a nudge so it can exit.
2002 if (thr_act
->suspend_count
)
2005 /* Drop the thr_act reference taken for being active.
2006 * (There is still at least one reference left:
2007 * the one we were passed.)
2008 * Inline the deallocate because thr_act is locked.
2010 act_locked_act_deallocate(thr_act
);
2012 return(KERN_SUCCESS
);
2016 * act_alert - Register an alert from this activation.
2018 * Each set bit is propagated upward from (but not including) this activation,
2019 * until the top of the chain is reached or the bit is masked.
2022 act_alert(thread_act_t thr_act
, unsigned alerts
)
2024 thread_t thread
= act_lock_thread(thr_act
);
2027 if (watchacts
& WA_ACT_LNK
)
2028 printf("act_alert %x: %x\n", thr_act
, alerts
);
2029 #endif /* MACH_ASSERT */
2032 thread_act_t act_up
= thr_act
;
2033 while ((alerts
) && (act_up
!= thread
->top_act
)) {
2034 act_up
= act_up
->higher
;
2035 alerts
&= act_up
->alert_mask
;
2036 act_up
->alerts
|= alerts
;
2039 * XXXX If we reach the top, and it is blocked in glue
2040 * code, do something to kick it. XXXX
2043 act_unlock_thread(thr_act
);
2045 return KERN_SUCCESS
;
2048 kern_return_t
act_alert_mask(thread_act_t thr_act
, unsigned alert_mask
)
2050 panic("act_alert_mask NOT YET IMPLEMENTED\n");
2051 return KERN_SUCCESS
;
2054 typedef struct GetSetState
{
2055 struct ReturnHandler rh
;
2062 /* Local Forward decls */
2063 kern_return_t
get_set_state(
2064 thread_act_t thr_act
, int flavor
,
2065 thread_state_t state
, int *pcount
,
2066 void (*handler
)(ReturnHandler
*rh
, thread_act_t thr_act
));
2067 void get_state_handler(ReturnHandler
*rh
, thread_act_t thr_act
);
2068 void set_state_handler(ReturnHandler
*rh
, thread_act_t thr_act
);
2071 * get_set_state(thr_act ...)
2073 * General code to install g/set_state handler.
2074 * Called with thr_act's act_lock() and "appropriate"
2075 * thread-related locks held. (See act_lock_thread().)
2078 get_set_state(thread_act_t thr_act
, int flavor
, thread_state_t state
, int *pcount
,
2079 void (*handler
)(ReturnHandler
*rh
, thread_act_t thr_act
))
2084 /* Initialize a small parameter structure */
2085 gss
.rh
.handler
= handler
;
2086 gss
.flavor
= flavor
;
2088 gss
.pcount
= pcount
;
2089 gss
.result
= KERN_ABORTED
; /* iff wait below is interrupted */
2091 /* Add it to the thr_act's return handler list */
2092 gss
.rh
.next
= thr_act
->handlers
;
2093 thr_act
->handlers
= &gss
.rh
;
2096 act_set_apc(thr_act
);
2100 if (watchacts
& WA_ACT_HDLR
) {
2101 printf("act_%x: get_set_state(thr_act=%x flv=%x state=%x ptr@%x=%x)",
2102 current_act(), thr_act
, flavor
, state
,
2103 pcount
, (pcount
? *pcount
: 0));
2104 printf((handler
== get_state_handler
? "get_state_hdlr\n" :
2105 (handler
== set_state_handler
? "set_state_hdlr\n" :
2106 "hndler=%x\n")), handler
);
2108 #endif /* MACH_ASSERT */
2110 assert(thr_act
->thread
); /* Callers must ensure these */
2111 assert(thr_act
!= current_act());
2115 * Wait must be interruptible to avoid deadlock (e.g.) with
2116 * task_suspend() when caller and target of get_set_state()
2119 assert_wait((event_t
)&gss
, THREAD_ABORTSAFE
);
2120 act_unlock_thread(thr_act
);
2121 thread_block((void (*)(void))0);
2122 if (gss
.result
!= KERN_ABORTED
)
2124 if (current_act()->handlers
)
2125 act_execute_returnhandlers();
2126 act_lock_thread(thr_act
);
2130 if (watchacts
& WA_ACT_HDLR
)
2131 printf("act_%x: get_set_state returns %x\n",
2132 current_act(), gss
.result
);
2133 #endif /* MACH_ASSERT */
2139 set_state_handler(ReturnHandler
*rh
, thread_act_t thr_act
)
2141 GetSetState
*gss
= (GetSetState
*)rh
;
2144 if (watchacts
& WA_ACT_HDLR
)
2145 printf("act_%x: set_state_handler(rh=%x,thr_act=%x)\n",
2146 current_act(), rh
, thr_act
);
2147 #endif /* MACH_ASSERT */
2149 gss
->result
= act_machine_set_state(thr_act
, gss
->flavor
,
2150 gss
->state
, *gss
->pcount
);
2151 thread_wakeup((event_t
)gss
);
2155 get_state_handler(ReturnHandler
*rh
, thread_act_t thr_act
)
2157 GetSetState
*gss
= (GetSetState
*)rh
;
2160 if (watchacts
& WA_ACT_HDLR
)
2161 printf("act_%x: get_state_handler(rh=%x,thr_act=%x)\n",
2162 current_act(), rh
, thr_act
);
2163 #endif /* MACH_ASSERT */
2165 gss
->result
= act_machine_get_state(thr_act
, gss
->flavor
,
2167 (mach_msg_type_number_t
*) gss
->pcount
);
2168 thread_wakeup((event_t
)gss
);
2172 act_get_state_locked(thread_act_t thr_act
, int flavor
, thread_state_t state
,
2173 mach_msg_type_number_t
*pcount
)
2176 if (watchacts
& WA_ACT_HDLR
)
2177 printf("act_%x: act_get_state_L(thr_act=%x,flav=%x,st=%x,pcnt@%x=%x)\n",
2178 current_act(), thr_act
, flavor
, state
, pcount
,
2179 (pcount
? *pcount
: 0));
2180 #endif /* MACH_ASSERT */
2182 return(get_set_state(thr_act
, flavor
, state
, (int*)pcount
, get_state_handler
));
2186 act_set_state_locked(thread_act_t thr_act
, int flavor
, thread_state_t state
,
2187 mach_msg_type_number_t count
)
2190 if (watchacts
& WA_ACT_HDLR
)
2191 printf("act_%x: act_set_state_L(thr_act=%x,flav=%x,st=%x,pcnt@%x=%x)\n",
2192 current_act(), thr_act
, flavor
, state
, count
, count
);
2193 #endif /* MACH_ASSERT */
2195 return(get_set_state(thr_act
, flavor
, state
, (int*)&count
, set_state_handler
));
2199 act_set_state(thread_act_t thr_act
, int flavor
, thread_state_t state
,
2200 mach_msg_type_number_t count
)
2202 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_act())
2203 return(KERN_INVALID_ARGUMENT
);
2205 act_lock_thread(thr_act
);
2206 return(act_set_state_locked(thr_act
, flavor
, state
, count
));
2211 act_get_state(thread_act_t thr_act
, int flavor
, thread_state_t state
,
2212 mach_msg_type_number_t
*pcount
)
2214 if (thr_act
== THR_ACT_NULL
|| thr_act
== current_act())
2215 return(KERN_INVALID_ARGUMENT
);
2217 act_lock_thread(thr_act
);
2218 return(act_get_state_locked(thr_act
, flavor
, state
, pcount
));
2222 * These two should be called at splsched()
2223 * Set/clear indicator to run APC (layered on ASTs)
2226 act_set_apc(thread_act_t thr_act
)
2228 thread_ast_set(thr_act
, AST_APC
);
2229 if (thr_act
== current_act()) {
2230 mp_disable_preemption();
2231 ast_propagate(thr_act
->ast
);
2232 mp_enable_preemption();
2237 act_clr_apc(thread_act_t thr_act
)
2239 thread_ast_clear(thr_act
, AST_APC
);
2243 act_ulock_release_all(thread_act_t thr_act
)
2247 while (!queue_empty(&thr_act
->held_ulocks
)) {
2248 ulock
= (ulock_t
) queue_first(&thr_act
->held_ulocks
);
2249 (void) lock_make_unstable(ulock
, thr_act
);
2250 (void) lock_release_internal(ulock
, thr_act
);
2255 * Provide routines (for export to other components) of things that
2256 * are implemented as macros insternally.
2262 return(current_act_fast());
2268 thread_act_t self
= current_act_fast();
2270 act_reference(self
);
2275 mach_thread_self(void)
2277 thread_act_t self
= current_act_fast();
2279 act_reference(self
);
2283 #undef act_reference
2286 thread_act_t thr_act
)
2288 act_reference_fast(thr_act
);
2291 #undef act_deallocate
2294 thread_act_t thr_act
)
2296 act_deallocate_fast(thr_act
);