2 * Copyright (c) 2000-2010 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@
29 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 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.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub
63 * Thread management primitives implementation.
66 * Copyright (c) 1993 The University of Utah and
67 * the Computer Systems Laboratory (CSL). All rights reserved.
69 * Permission to use, copy, modify and distribute this software and its
70 * documentation is hereby granted, provided that both the copyright
71 * notice and this permission notice appear in all copies of the
72 * software, derivative works or modified versions, and any portions
73 * thereof, and that both notices appear in supporting documentation.
75 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
76 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
77 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
79 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
80 * improvements that they make and grant CSL redistribution rights.
84 #include <mach/mach_types.h>
85 #include <mach/boolean.h>
86 #include <mach/policy.h>
87 #include <mach/thread_info.h>
88 #include <mach/thread_special_ports.h>
89 #include <mach/thread_status.h>
90 #include <mach/time_value.h>
91 #include <mach/vm_param.h>
93 #include <machine/thread.h>
94 #include <machine/pal_routines.h>
95 #include <machine/limits.h>
97 #include <kern/kern_types.h>
98 #include <kern/kalloc.h>
99 #include <kern/cpu_data.h>
100 #include <kern/counters.h>
101 #include <kern/extmod_statistics.h>
102 #include <kern/ipc_mig.h>
103 #include <kern/ipc_tt.h>
104 #include <kern/mach_param.h>
105 #include <kern/machine.h>
106 #include <kern/misc_protos.h>
107 #include <kern/processor.h>
108 #include <kern/queue.h>
109 #include <kern/sched.h>
110 #include <kern/sched_prim.h>
111 #include <kern/sync_lock.h>
112 #include <kern/syscall_subr.h>
113 #include <kern/task.h>
114 #include <kern/thread.h>
115 #include <kern/host.h>
116 #include <kern/zalloc.h>
117 #include <kern/assert.h>
119 #include <ipc/ipc_kmsg.h>
120 #include <ipc/ipc_port.h>
122 #include <vm/vm_kern.h>
123 #include <vm/vm_pageout.h>
125 #include <sys/kdebug.h>
127 #include <mach/sdt.h>
130 * Exported interfaces
132 #include <mach/task_server.h>
133 #include <mach/thread_act_server.h>
134 #include <mach/mach_host_server.h>
135 #include <mach/host_priv_server.h>
137 static struct zone
*thread_zone
;
138 static lck_grp_attr_t thread_lck_grp_attr
;
139 lck_attr_t thread_lck_attr
;
140 lck_grp_t thread_lck_grp
;
142 decl_simple_lock_data(static,thread_stack_lock
)
143 static queue_head_t thread_stack_queue
;
145 decl_simple_lock_data(static,thread_terminate_lock
)
146 static queue_head_t thread_terminate_queue
;
148 static struct thread thread_template
, init_thread
;
150 static void sched_call_null(
155 extern void proc_exit(void *);
156 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
157 #endif /* MACH_BSD */
159 extern int debug_task
;
160 int thread_max
= CONFIG_THREAD_MAX
; /* Max number of threads */
161 int task_threadmax
= CONFIG_THREAD_MAX
;
163 static uint64_t thread_unique_id
= 0;
165 struct _thread_ledger_indices thread_ledgers
= { -1 };
166 static ledger_template_t thread_ledger_template
= NULL
;
167 void init_thread_ledgers(void);
170 thread_bootstrap(void)
173 * Fill in a template thread for fast initialization.
176 thread_template
.runq
= PROCESSOR_NULL
;
178 thread_template
.ref_count
= 2;
180 thread_template
.reason
= AST_NONE
;
181 thread_template
.at_safe_point
= FALSE
;
182 thread_template
.wait_event
= NO_EVENT64
;
183 thread_template
.wait_queue
= WAIT_QUEUE_NULL
;
184 thread_template
.wait_result
= THREAD_WAITING
;
185 thread_template
.options
= THREAD_ABORTSAFE
;
186 thread_template
.state
= TH_WAIT
| TH_UNINT
;
187 thread_template
.wake_active
= FALSE
;
188 thread_template
.continuation
= THREAD_CONTINUE_NULL
;
189 thread_template
.parameter
= NULL
;
191 thread_template
.importance
= 0;
192 thread_template
.sched_mode
= TH_MODE_NONE
;
193 thread_template
.sched_flags
= 0;
194 thread_template
.saved_mode
= TH_MODE_NONE
;
195 thread_template
.safe_release
= 0;
197 thread_template
.priority
= 0;
198 thread_template
.sched_pri
= 0;
199 thread_template
.max_priority
= 0;
200 thread_template
.task_priority
= 0;
201 thread_template
.promotions
= 0;
202 thread_template
.pending_promoter_index
= 0;
203 thread_template
.pending_promoter
[0] =
204 thread_template
.pending_promoter
[1] = NULL
;
206 thread_template
.realtime
.deadline
= UINT64_MAX
;
208 thread_template
.current_quantum
= 0;
209 thread_template
.last_run_time
= 0;
210 thread_template
.last_quantum_refill_time
= 0;
212 thread_template
.computation_metered
= 0;
213 thread_template
.computation_epoch
= 0;
215 #if defined(CONFIG_SCHED_TRADITIONAL)
216 thread_template
.sched_stamp
= 0;
217 thread_template
.pri_shift
= INT8_MAX
;
218 thread_template
.sched_usage
= 0;
219 thread_template
.cpu_usage
= thread_template
.cpu_delta
= 0;
221 thread_template
.c_switch
= thread_template
.p_switch
= thread_template
.ps_switch
= 0;
223 thread_template
.bound_processor
= PROCESSOR_NULL
;
224 thread_template
.last_processor
= PROCESSOR_NULL
;
226 thread_template
.sched_call
= sched_call_null
;
228 timer_init(&thread_template
.user_timer
);
229 timer_init(&thread_template
.system_timer
);
230 thread_template
.user_timer_save
= 0;
231 thread_template
.system_timer_save
= 0;
232 thread_template
.vtimer_user_save
= 0;
233 thread_template
.vtimer_prof_save
= 0;
234 thread_template
.vtimer_rlim_save
= 0;
236 thread_template
.wait_timer_is_set
= FALSE
;
237 thread_template
.wait_timer_active
= 0;
239 thread_template
.depress_timer_active
= 0;
241 thread_template
.special_handler
.handler
= special_handler
;
242 thread_template
.special_handler
.next
= NULL
;
244 thread_template
.funnel_lock
= THR_FUNNEL_NULL
;
245 thread_template
.funnel_state
= 0;
246 thread_template
.recover
= (vm_offset_t
)NULL
;
248 thread_template
.map
= VM_MAP_NULL
;
251 thread_template
.t_dtrace_predcache
= 0;
252 thread_template
.t_dtrace_vtime
= 0;
253 thread_template
.t_dtrace_tracing
= 0;
254 #endif /* CONFIG_DTRACE */
256 thread_template
.t_chud
= 0;
257 thread_template
.t_page_creation_count
= 0;
258 thread_template
.t_page_creation_time
= 0;
260 thread_template
.affinity_set
= NULL
;
262 thread_template
.syscalls_unix
= 0;
263 thread_template
.syscalls_mach
= 0;
265 thread_template
.t_ledger
= LEDGER_NULL
;
266 thread_template
.t_threadledger
= LEDGER_NULL
;
268 thread_template
.appliedstate
= default_task_null_policy
;
269 thread_template
.ext_appliedstate
= default_task_null_policy
;
270 thread_template
.policystate
= default_task_proc_policy
;
271 thread_template
.ext_policystate
= default_task_proc_policy
;
273 thread_template
.taskwatch
= NULL
;
274 thread_template
.saved_importance
= 0;
275 #endif /* CONFIG_EMBEDDED */
277 init_thread
= thread_template
;
278 machine_set_current_thread(&init_thread
);
285 sizeof(struct thread
),
286 thread_max
* sizeof(struct thread
),
287 THREAD_CHUNK
* sizeof(struct thread
),
290 lck_grp_attr_setdefault(&thread_lck_grp_attr
);
291 lck_grp_init(&thread_lck_grp
, "thread", &thread_lck_grp_attr
);
292 lck_attr_setdefault(&thread_lck_attr
);
297 * Initialize any machine-dependent
298 * per-thread structures necessary.
300 machine_thread_init();
302 init_thread_ledgers();
306 thread_terminate_continue(void)
308 panic("thread_terminate_continue");
313 * thread_terminate_self:
316 thread_terminate_self(void)
318 thread_t thread
= current_thread();
324 pal_thread_terminate_self(thread
);
326 DTRACE_PROC(lwp__exit
);
328 thread_mtx_lock(thread
);
330 ulock_release_all(thread
);
332 ipc_thread_disable(thread
);
334 thread_mtx_unlock(thread
);
340 * Cancel priority depression, wait for concurrent expirations
341 * on other processors.
343 if (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) {
344 thread
->sched_flags
&= ~TH_SFLAG_DEPRESSED_MASK
;
346 if (timer_call_cancel(&thread
->depress_timer
))
347 thread
->depress_timer_active
--;
350 while (thread
->depress_timer_active
> 0) {
351 thread_unlock(thread
);
360 thread_sched_call(thread
, NULL
);
362 thread_unlock(thread
);
365 thread_policy_reset(thread
);
368 thead_remove_taskwatch(thread
);
369 #endif /* CONFIG_EMBEDDED */
372 uthread_cleanup(task
, thread
->uthread
, task
->bsd_info
);
373 threadcnt
= hw_atomic_sub(&task
->active_thread_count
, 1);
376 * If we are the last thread to terminate and the task is
377 * associated with a BSD process, perform BSD process exit.
379 if (threadcnt
== 0 && task
->bsd_info
!= NULL
)
380 proc_exit(task
->bsd_info
);
382 uthread_cred_free(thread
->uthread
);
388 * Cancel wait timer, and wait for
389 * concurrent expirations.
391 if (thread
->wait_timer_is_set
) {
392 thread
->wait_timer_is_set
= FALSE
;
394 if (timer_call_cancel(&thread
->wait_timer
))
395 thread
->wait_timer_active
--;
398 while (thread
->wait_timer_active
> 0) {
399 thread_unlock(thread
);
409 * If there is a reserved stack, release it.
411 if (thread
->reserved_stack
!= 0) {
412 stack_free_reserved(thread
);
413 thread
->reserved_stack
= 0;
417 * Mark thread as terminating, and block.
419 thread
->state
|= TH_TERMINATE
;
420 thread_mark_wait_locked(thread
, THREAD_UNINT
);
421 assert(thread
->promotions
== 0);
422 thread_unlock(thread
);
425 thread_block((thread_continue_t
)thread_terminate_continue
);
435 if (thread
== THREAD_NULL
)
438 if (thread_deallocate_internal(thread
) > 0)
442 ipc_thread_terminate(thread
);
448 void *ut
= thread
->uthread
;
450 thread
->uthread
= NULL
;
451 uthread_zone_free(ut
);
453 #endif /* MACH_BSD */
455 if (thread
->t_ledger
)
456 ledger_dereference(thread
->t_ledger
);
457 if (thread
->t_threadledger
)
458 ledger_dereference(thread
->t_threadledger
);
460 if (thread
->kernel_stack
!= 0)
463 lck_mtx_destroy(&thread
->mutex
, &thread_lck_grp
);
464 machine_thread_destroy(thread
);
466 task_deallocate(task
);
468 zfree(thread_zone
, thread
);
472 * thread_terminate_daemon:
474 * Perform final clean up for terminating threads.
477 thread_terminate_daemon(void)
479 thread_t self
, thread
;
482 self
= current_thread();
483 self
->options
|= TH_OPT_SYSTEM_CRITICAL
;
486 simple_lock(&thread_terminate_lock
);
488 while ((thread
= (thread_t
)dequeue_head(&thread_terminate_queue
)) != THREAD_NULL
) {
489 simple_unlock(&thread_terminate_lock
);
495 task
->total_user_time
+= timer_grab(&thread
->user_timer
);
496 if (thread
->precise_user_kernel_time
) {
497 task
->total_system_time
+= timer_grab(&thread
->system_timer
);
499 task
->total_user_time
+= timer_grab(&thread
->system_timer
);
502 task
->c_switch
+= thread
->c_switch
;
503 task
->p_switch
+= thread
->p_switch
;
504 task
->ps_switch
+= thread
->ps_switch
;
506 task
->syscalls_unix
+= thread
->syscalls_unix
;
507 task
->syscalls_mach
+= thread
->syscalls_mach
;
509 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
510 task
->thread_count
--;
513 * If the task is being halted, and there is only one thread
514 * left in the task after this one, then wakeup that thread.
516 if (task
->thread_count
== 1 && task
->halting
)
517 thread_wakeup((event_t
)&task
->halting
);
521 lck_mtx_lock(&tasks_threads_lock
);
522 queue_remove(&threads
, thread
, thread_t
, threads
);
524 lck_mtx_unlock(&tasks_threads_lock
);
526 thread_deallocate(thread
);
529 simple_lock(&thread_terminate_lock
);
532 assert_wait((event_t
)&thread_terminate_queue
, THREAD_UNINT
);
533 simple_unlock(&thread_terminate_lock
);
536 self
->options
&= ~TH_OPT_SYSTEM_CRITICAL
;
537 thread_block((thread_continue_t
)thread_terminate_daemon
);
542 * thread_terminate_enqueue:
544 * Enqueue a terminating thread for final disposition.
546 * Called at splsched.
549 thread_terminate_enqueue(
552 simple_lock(&thread_terminate_lock
);
553 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)thread
);
554 simple_unlock(&thread_terminate_lock
);
556 thread_wakeup((event_t
)&thread_terminate_queue
);
560 * thread_stack_daemon:
562 * Perform stack allocation as required due to
566 thread_stack_daemon(void)
570 simple_lock(&thread_stack_lock
);
572 while ((thread
= (thread_t
)dequeue_head(&thread_stack_queue
)) != THREAD_NULL
) {
573 simple_unlock(&thread_stack_lock
);
579 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
580 thread_unlock(thread
);
583 simple_lock(&thread_stack_lock
);
586 assert_wait((event_t
)&thread_stack_queue
, THREAD_UNINT
);
587 simple_unlock(&thread_stack_lock
);
589 thread_block((thread_continue_t
)thread_stack_daemon
);
594 * thread_stack_enqueue:
596 * Enqueue a thread for stack allocation.
598 * Called at splsched.
601 thread_stack_enqueue(
604 simple_lock(&thread_stack_lock
);
605 enqueue_tail(&thread_stack_queue
, (queue_entry_t
)thread
);
606 simple_unlock(&thread_stack_lock
);
608 thread_wakeup((event_t
)&thread_stack_queue
);
612 thread_daemon_init(void)
614 kern_return_t result
;
615 thread_t thread
= NULL
;
617 simple_lock_init(&thread_terminate_lock
, 0);
618 queue_init(&thread_terminate_queue
);
620 result
= kernel_thread_start_priority((thread_continue_t
)thread_terminate_daemon
, NULL
, MINPRI_KERNEL
, &thread
);
621 if (result
!= KERN_SUCCESS
)
622 panic("thread_daemon_init: thread_terminate_daemon");
624 thread_deallocate(thread
);
626 simple_lock_init(&thread_stack_lock
, 0);
627 queue_init(&thread_stack_queue
);
629 result
= kernel_thread_start_priority((thread_continue_t
)thread_stack_daemon
, NULL
, BASEPRI_PREEMPT
, &thread
);
630 if (result
!= KERN_SUCCESS
)
631 panic("thread_daemon_init: thread_stack_daemon");
633 thread_deallocate(thread
);
637 * Create a new thread.
638 * Doesn't start the thread running.
641 thread_create_internal(
644 thread_continue_t continuation
,
646 #define TH_OPTION_NONE 0x00
647 #define TH_OPTION_NOCRED 0x01
648 #define TH_OPTION_NOSUSP 0x02
649 thread_t
*out_thread
)
652 static thread_t first_thread
;
655 * Allocate a thread and initialize static fields
657 if (first_thread
== THREAD_NULL
)
658 new_thread
= first_thread
= current_thread();
660 new_thread
= (thread_t
)zalloc(thread_zone
);
661 if (new_thread
== THREAD_NULL
)
662 return (KERN_RESOURCE_SHORTAGE
);
664 if (new_thread
!= first_thread
)
665 *new_thread
= thread_template
;
668 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
, (options
& TH_OPTION_NOCRED
) != 0);
669 if (new_thread
->uthread
== NULL
) {
670 zfree(thread_zone
, new_thread
);
671 return (KERN_RESOURCE_SHORTAGE
);
673 #endif /* MACH_BSD */
675 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
677 void *ut
= new_thread
->uthread
;
679 new_thread
->uthread
= NULL
;
680 /* cred free may not be necessary */
681 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
682 uthread_cred_free(ut
);
683 uthread_zone_free(ut
);
684 #endif /* MACH_BSD */
686 zfree(thread_zone
, new_thread
);
687 return (KERN_FAILURE
);
690 new_thread
->task
= parent_task
;
692 thread_lock_init(new_thread
);
693 wake_lock_init(new_thread
);
695 lck_mtx_init(&new_thread
->mutex
, &thread_lck_grp
, &thread_lck_attr
);
697 ipc_thread_init(new_thread
);
698 queue_init(&new_thread
->held_ulocks
);
700 new_thread
->continuation
= continuation
;
702 lck_mtx_lock(&tasks_threads_lock
);
703 task_lock(parent_task
);
705 if ( !parent_task
->active
|| parent_task
->halting
||
706 ((options
& TH_OPTION_NOSUSP
) != 0 &&
707 parent_task
->suspend_count
> 0) ||
708 (parent_task
->thread_count
>= task_threadmax
&&
709 parent_task
!= kernel_task
) ) {
710 task_unlock(parent_task
);
711 lck_mtx_unlock(&tasks_threads_lock
);
715 void *ut
= new_thread
->uthread
;
717 new_thread
->uthread
= NULL
;
718 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
719 /* cred free may not be necessary */
720 uthread_cred_free(ut
);
721 uthread_zone_free(ut
);
723 #endif /* MACH_BSD */
724 ipc_thread_disable(new_thread
);
725 ipc_thread_terminate(new_thread
);
726 lck_mtx_destroy(&new_thread
->mutex
, &thread_lck_grp
);
727 machine_thread_destroy(new_thread
);
728 zfree(thread_zone
, new_thread
);
729 return (KERN_FAILURE
);
732 /* New threads inherit any default state on the task */
733 machine_thread_inherit_taskwide(new_thread
, parent_task
);
735 task_reference_internal(parent_task
);
737 if (new_thread
->task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
739 * This task has a per-thread CPU limit; make sure this new thread
740 * gets its limit set too, before it gets out of the kernel.
742 set_astledger(new_thread
);
744 new_thread
->t_threadledger
= LEDGER_NULL
; /* per thread ledger is not inherited */
745 new_thread
->t_ledger
= new_thread
->task
->ledger
;
746 if (new_thread
->t_ledger
)
747 ledger_reference(new_thread
->t_ledger
);
749 /* Cache the task's map */
750 new_thread
->map
= parent_task
->map
;
752 /* Chain the thread onto the task's list */
753 queue_enter(&parent_task
->threads
, new_thread
, thread_t
, task_threads
);
754 parent_task
->thread_count
++;
756 /* So terminating threads don't need to take the task lock to decrement */
757 hw_atomic_add(&parent_task
->active_thread_count
, 1);
759 /* Protected by the tasks_threads_lock */
760 new_thread
->thread_id
= ++thread_unique_id
;
762 queue_enter(&threads
, new_thread
, thread_t
, threads
);
765 timer_call_setup(&new_thread
->wait_timer
, thread_timer_expire
, new_thread
);
766 timer_call_setup(&new_thread
->depress_timer
, thread_depress_expire
, new_thread
);
770 * If parent task has any reservations, they need to be propagated to this
773 new_thread
->t_chud
= (TASK_PMC_FLAG
== (parent_task
->t_chud
& TASK_PMC_FLAG
)) ?
774 THREAD_PMC_FLAG
: 0U;
777 /* Set the thread's scheduling parameters */
778 new_thread
->sched_mode
= SCHED(initial_thread_sched_mode
)(parent_task
);
779 new_thread
->sched_flags
= 0;
780 new_thread
->max_priority
= parent_task
->max_priority
;
781 new_thread
->task_priority
= parent_task
->priority
;
782 new_thread
->priority
= (priority
< 0)? parent_task
->priority
: priority
;
783 if (new_thread
->priority
> new_thread
->max_priority
)
784 new_thread
->priority
= new_thread
->max_priority
;
786 if (new_thread
->priority
< MAXPRI_THROTTLE
) {
787 new_thread
->priority
= MAXPRI_THROTTLE
;
789 #endif /* CONFIG_EMBEDDED */
790 new_thread
->importance
=
791 new_thread
->priority
- new_thread
->task_priority
;
793 new_thread
->saved_importance
= new_thread
->importance
;
794 /* apple ios daemon starts all threads in darwin background */
795 if (parent_task
->ext_appliedstate
.apptype
== PROC_POLICY_IOS_APPLE_DAEMON
) {
796 /* Cannot use generic routines here so apply darwin bacground directly */
797 new_thread
->policystate
.hw_bg
= TASK_POLICY_BACKGROUND_ATTRIBUTE_ALL
;
798 /* set thread self backgrounding */
799 new_thread
->appliedstate
.hw_bg
= new_thread
->policystate
.hw_bg
;
800 /* priority will get recomputed suitably bit later */
801 new_thread
->importance
= INT_MIN
;
802 /* to avoid changes to many pri compute routines, set the effect of those here */
803 new_thread
->priority
= MAXPRI_THROTTLE
;
805 #endif /* CONFIG_EMBEDDED */
807 #if defined(CONFIG_SCHED_TRADITIONAL)
808 new_thread
->sched_stamp
= sched_tick
;
809 new_thread
->pri_shift
= sched_pri_shift
;
811 SCHED(compute_priority
)(new_thread
, FALSE
);
813 new_thread
->active
= TRUE
;
815 *out_thread
= new_thread
;
818 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
820 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
822 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
823 TRACEDBG_CODE(DBG_TRACE_DATA
, 1) | DBG_FUNC_NONE
,
824 (vm_address_t
)(uintptr_t)thread_tid(new_thread
), dbg_arg2
, 0, 0, 0);
826 kdbg_trace_string(parent_task
->bsd_info
,
827 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
829 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
830 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
831 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
834 DTRACE_PROC1(lwp__create
, thread_t
, *out_thread
);
836 return (KERN_SUCCESS
);
840 thread_create_internal2(
842 thread_t
*new_thread
,
845 kern_return_t result
;
848 if (task
== TASK_NULL
|| task
== kernel_task
)
849 return (KERN_INVALID_ARGUMENT
);
851 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
852 if (result
!= KERN_SUCCESS
)
855 thread
->user_stop_count
= 1;
857 if (task
->suspend_count
> 0)
861 extmod_statistics_incr_thread_create(task
);
864 lck_mtx_unlock(&tasks_threads_lock
);
866 *new_thread
= thread
;
868 return (KERN_SUCCESS
);
871 /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
875 thread_t
*new_thread
);
880 thread_t
*new_thread
)
882 return thread_create_internal2(task
, new_thread
, FALSE
);
886 thread_create_from_user(
888 thread_t
*new_thread
)
890 return thread_create_internal2(task
, new_thread
, TRUE
);
894 thread_create_running_internal2(
895 register task_t task
,
897 thread_state_t new_state
,
898 mach_msg_type_number_t new_state_count
,
899 thread_t
*new_thread
,
902 register kern_return_t result
;
905 if (task
== TASK_NULL
|| task
== kernel_task
)
906 return (KERN_INVALID_ARGUMENT
);
908 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
909 if (result
!= KERN_SUCCESS
)
912 result
= machine_thread_set_state(
913 thread
, flavor
, new_state
, new_state_count
);
914 if (result
!= KERN_SUCCESS
) {
916 lck_mtx_unlock(&tasks_threads_lock
);
918 thread_terminate(thread
);
919 thread_deallocate(thread
);
923 thread_mtx_lock(thread
);
924 thread_start_internal(thread
);
925 thread_mtx_unlock(thread
);
928 extmod_statistics_incr_thread_create(task
);
931 lck_mtx_unlock(&tasks_threads_lock
);
933 *new_thread
= thread
;
938 /* Prototype, see justification above */
940 thread_create_running(
941 register task_t task
,
943 thread_state_t new_state
,
944 mach_msg_type_number_t new_state_count
,
945 thread_t
*new_thread
);
948 thread_create_running(
949 register task_t task
,
951 thread_state_t new_state
,
952 mach_msg_type_number_t new_state_count
,
953 thread_t
*new_thread
)
955 return thread_create_running_internal2(
956 task
, flavor
, new_state
, new_state_count
,
961 thread_create_running_from_user(
962 register task_t task
,
964 thread_state_t new_state
,
965 mach_msg_type_number_t new_state_count
,
966 thread_t
*new_thread
)
968 return thread_create_running_internal2(
969 task
, flavor
, new_state
, new_state_count
,
976 thread_continue_t thread_return
,
977 thread_t
*new_thread
)
979 kern_return_t result
;
982 if (task
== TASK_NULL
|| task
== kernel_task
)
983 return (KERN_INVALID_ARGUMENT
);
985 result
= thread_create_internal(task
, -1, thread_return
, TH_OPTION_NOCRED
| TH_OPTION_NOSUSP
, &thread
);
986 if (result
!= KERN_SUCCESS
)
989 thread
->user_stop_count
= 1;
991 if (task
->suspend_count
> 0)
995 lck_mtx_unlock(&tasks_threads_lock
);
997 *new_thread
= thread
;
999 return (KERN_SUCCESS
);
1003 * kernel_thread_create:
1005 * Create a thread in the kernel task
1006 * to execute in kernel context.
1009 kernel_thread_create(
1010 thread_continue_t continuation
,
1013 thread_t
*new_thread
)
1015 kern_return_t result
;
1017 task_t task
= kernel_task
;
1019 result
= thread_create_internal(task
, priority
, continuation
, TH_OPTION_NONE
, &thread
);
1020 if (result
!= KERN_SUCCESS
)
1024 lck_mtx_unlock(&tasks_threads_lock
);
1026 stack_alloc(thread
);
1027 assert(thread
->kernel_stack
!= 0);
1029 if (priority
> BASEPRI_KERNEL
)
1031 thread
->reserved_stack
= thread
->kernel_stack
;
1033 thread
->parameter
= parameter
;
1036 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread
, continuation
);
1037 *new_thread
= thread
;
1043 kernel_thread_start_priority(
1044 thread_continue_t continuation
,
1047 thread_t
*new_thread
)
1049 kern_return_t result
;
1052 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
1053 if (result
!= KERN_SUCCESS
)
1056 *new_thread
= thread
;
1058 thread_mtx_lock(thread
);
1059 thread_start_internal(thread
);
1060 thread_mtx_unlock(thread
);
1066 kernel_thread_start(
1067 thread_continue_t continuation
,
1069 thread_t
*new_thread
)
1071 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
1074 #if defined(__i386__)
1079 void (*start
)(void))
1081 kern_return_t result
;
1084 if (task
!= kernel_task
)
1085 panic("kernel_thread");
1087 result
= kernel_thread_start_priority((thread_continue_t
)start
, NULL
, -1, &thread
);
1088 if (result
!= KERN_SUCCESS
)
1089 return (THREAD_NULL
);
1091 thread_deallocate(thread
);
1096 #endif /* defined(__i386__) */
1099 thread_info_internal(
1100 register thread_t thread
,
1101 thread_flavor_t flavor
,
1102 thread_info_t thread_info_out
, /* ptr to OUT array */
1103 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
1108 if (thread
== THREAD_NULL
)
1109 return (KERN_INVALID_ARGUMENT
);
1111 if (flavor
== THREAD_BASIC_INFO
) {
1112 register thread_basic_info_t basic_info
;
1114 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
1115 return (KERN_INVALID_ARGUMENT
);
1117 basic_info
= (thread_basic_info_t
) thread_info_out
;
1120 thread_lock(thread
);
1124 thread_read_times(thread
, &basic_info
->user_time
,
1125 &basic_info
->system_time
);
1128 * Update lazy-evaluated scheduler info because someone wants it.
1130 if (SCHED(can_update_priority
)(thread
))
1131 SCHED(update_priority
)(thread
);
1133 basic_info
->sleep_time
= 0;
1136 * To calculate cpu_usage, first correct for timer rate,
1137 * then for 5/8 ageing. The correction factor [3/5] is
1140 basic_info
->cpu_usage
= 0;
1141 #if defined(CONFIG_SCHED_TRADITIONAL)
1142 if (sched_tick_interval
) {
1143 basic_info
->cpu_usage
= (integer_t
)(((uint64_t)thread
->cpu_usage
1144 * TH_USAGE_SCALE
) / sched_tick_interval
);
1145 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
1149 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
)
1150 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
1152 basic_info
->policy
= ((thread
->sched_mode
== TH_MODE_TIMESHARE
)?
1153 POLICY_TIMESHARE
: POLICY_RR
);
1156 if (thread
->bound_processor
!= PROCESSOR_NULL
&& thread
->bound_processor
->idle_thread
== thread
)
1157 flags
|= TH_FLAGS_IDLE
;
1159 if (!thread
->kernel_stack
)
1160 flags
|= TH_FLAGS_SWAPPED
;
1163 if (thread
->state
& TH_TERMINATE
)
1164 state
= TH_STATE_HALTED
;
1166 if (thread
->state
& TH_RUN
)
1167 state
= TH_STATE_RUNNING
;
1169 if (thread
->state
& TH_UNINT
)
1170 state
= TH_STATE_UNINTERRUPTIBLE
;
1172 if (thread
->state
& TH_SUSP
)
1173 state
= TH_STATE_STOPPED
;
1175 if (thread
->state
& TH_WAIT
)
1176 state
= TH_STATE_WAITING
;
1178 basic_info
->run_state
= state
;
1179 basic_info
->flags
= flags
;
1181 basic_info
->suspend_count
= thread
->user_stop_count
;
1183 thread_unlock(thread
);
1186 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
1188 return (KERN_SUCCESS
);
1191 if (flavor
== THREAD_IDENTIFIER_INFO
) {
1192 register thread_identifier_info_t identifier_info
;
1194 if (*thread_info_count
< THREAD_IDENTIFIER_INFO_COUNT
)
1195 return (KERN_INVALID_ARGUMENT
);
1197 identifier_info
= (thread_identifier_info_t
) thread_info_out
;
1200 thread_lock(thread
);
1202 identifier_info
->thread_id
= thread
->thread_id
;
1203 identifier_info
->thread_handle
= thread
->machine
.cthread_self
;
1204 if(thread
->task
->bsd_info
) {
1205 identifier_info
->dispatch_qaddr
= identifier_info
->thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1207 thread_unlock(thread
);
1209 return KERN_INVALID_ARGUMENT
;
1212 thread_unlock(thread
);
1214 return KERN_SUCCESS
;
1217 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
1218 policy_timeshare_info_t ts_info
;
1220 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
1221 return (KERN_INVALID_ARGUMENT
);
1223 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
1226 thread_lock(thread
);
1228 if (thread
->sched_mode
!= TH_MODE_TIMESHARE
) {
1229 thread_unlock(thread
);
1232 return (KERN_INVALID_POLICY
);
1235 ts_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1236 if (ts_info
->depressed
) {
1237 ts_info
->base_priority
= DEPRESSPRI
;
1238 ts_info
->depress_priority
= thread
->priority
;
1241 ts_info
->base_priority
= thread
->priority
;
1242 ts_info
->depress_priority
= -1;
1245 ts_info
->cur_priority
= thread
->sched_pri
;
1246 ts_info
->max_priority
= thread
->max_priority
;
1248 thread_unlock(thread
);
1251 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
1253 return (KERN_SUCCESS
);
1256 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
1257 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
1258 return (KERN_INVALID_ARGUMENT
);
1260 return (KERN_INVALID_POLICY
);
1263 if (flavor
== THREAD_SCHED_RR_INFO
) {
1264 policy_rr_info_t rr_info
;
1265 uint32_t quantum_time
;
1266 uint64_t quantum_ns
;
1268 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
1269 return (KERN_INVALID_ARGUMENT
);
1271 rr_info
= (policy_rr_info_t
) thread_info_out
;
1274 thread_lock(thread
);
1276 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
1277 thread_unlock(thread
);
1280 return (KERN_INVALID_POLICY
);
1283 rr_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1284 if (rr_info
->depressed
) {
1285 rr_info
->base_priority
= DEPRESSPRI
;
1286 rr_info
->depress_priority
= thread
->priority
;
1289 rr_info
->base_priority
= thread
->priority
;
1290 rr_info
->depress_priority
= -1;
1293 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
1294 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
1296 rr_info
->max_priority
= thread
->max_priority
;
1297 rr_info
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
1299 thread_unlock(thread
);
1302 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1304 return (KERN_SUCCESS
);
1307 return (KERN_INVALID_ARGUMENT
);
1313 time_value_t
*user_time
,
1314 time_value_t
*system_time
)
1318 uint64_t tval_user
, tval_system
;
1320 tval_user
= timer_grab(&thread
->user_timer
);
1321 tval_system
= timer_grab(&thread
->system_timer
);
1323 if (thread
->precise_user_kernel_time
) {
1324 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1325 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1326 user_time
->microseconds
= usecs
;
1328 absolutetime_to_microtime(tval_system
, &secs
, &usecs
);
1329 system_time
->seconds
= (typeof(system_time
->seconds
))secs
;
1330 system_time
->microseconds
= usecs
;
1332 /* system_timer may represent either sys or user */
1333 tval_user
+= tval_system
;
1334 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1335 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1336 user_time
->microseconds
= usecs
;
1338 system_time
->seconds
= 0;
1339 system_time
->microseconds
= 0;
1345 __unused thread_t thread
,
1346 __unused processor_set_t new_pset
)
1348 return (KERN_FAILURE
);
1352 * thread_assign_default:
1354 * Special version of thread_assign for assigning threads to default
1358 thread_assign_default(
1361 return (thread_assign(thread
, &pset0
));
1365 * thread_get_assignment
1367 * Return current assignment for this thread.
1370 thread_get_assignment(
1372 processor_set_t
*pset
)
1375 return (KERN_INVALID_ARGUMENT
);
1379 return (KERN_SUCCESS
);
1383 * thread_wire_internal:
1385 * Specify that the target thread must always be able
1386 * to run and to allocate memory.
1389 thread_wire_internal(
1390 host_priv_t host_priv
,
1393 boolean_t
*prev_state
)
1395 if (host_priv
== NULL
|| thread
!= current_thread())
1396 return (KERN_INVALID_ARGUMENT
);
1398 assert(host_priv
== &realhost
);
1401 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
1404 if (!(thread
->options
& TH_OPT_VMPRIV
))
1405 vm_page_free_reserve(1); /* XXX */
1406 thread
->options
|= TH_OPT_VMPRIV
;
1409 if (thread
->options
& TH_OPT_VMPRIV
)
1410 vm_page_free_reserve(-1); /* XXX */
1411 thread
->options
&= ~TH_OPT_VMPRIV
;
1414 return (KERN_SUCCESS
);
1421 * User-api wrapper for thread_wire_internal()
1425 host_priv_t host_priv
,
1429 return (thread_wire_internal(host_priv
, thread
, wired
, NULL
));
1433 thread_resource_exception(const void *arg0
, __unused
const void *arg1
)
1435 thread_t thread
= current_thread();
1436 int code
= (int)((uintptr_t)arg0
& ((int)-1));
1438 assert(thread
->t_threadledger
!= LEDGER_NULL
);
1441 * Disable the exception notification so we don't overwhelm
1442 * the listener with an endless stream of redundant exceptions.
1444 ledger_set_action(thread
->t_threadledger
, thread_ledgers
.cpu_time
,
1445 LEDGER_ACTION_IGNORE
);
1446 ledger_disable_callback(thread
->t_threadledger
, thread_ledgers
.cpu_time
);
1448 /* XXX code should eventually be a user-exported namespace of resources */
1449 (void) task_exception_notify(EXC_RESOURCE
, code
, 0);
1453 init_thread_ledgers(void) {
1454 ledger_template_t t
;
1457 assert(thread_ledger_template
== NULL
);
1459 if ((t
= ledger_template_create("Per-thread ledger")) == NULL
)
1460 panic("couldn't create thread ledger template");
1462 if ((idx
= ledger_entry_add(t
, "cpu_time", "sched", "ns")) < 0) {
1463 panic("couldn't create cpu_time entry for thread ledger template");
1466 if (ledger_set_callback(t
, idx
, thread_resource_exception
,
1467 (void *)(uintptr_t)idx
, NULL
) < 0) {
1468 panic("couldn't set thread ledger callback for cpu_time entry");
1471 thread_ledgers
.cpu_time
= idx
;
1472 thread_ledger_template
= t
;
1476 * Set CPU usage limit on a thread.
1478 * Calling with percentage of 0 will unset the limit for this thread.
1482 thread_set_cpulimit(int action
, uint8_t percentage
, uint64_t interval_ns
)
1484 thread_t thread
= current_thread();
1486 uint64_t limittime
= 0;
1487 uint64_t abstime
= 0;
1489 assert(percentage
<= 100);
1491 if (percentage
== 0) {
1493 * Remove CPU limit, if any exists.
1495 if (thread
->t_threadledger
!= LEDGER_NULL
) {
1497 * The only way to get a per-thread ledger is via CPU limits.
1499 assert(thread
->options
& (TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
));
1500 ledger_dereference(thread
->t_threadledger
);
1501 thread
->t_threadledger
= LEDGER_NULL
;
1502 thread
->options
&= ~(TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
);
1508 l
= thread
->t_threadledger
;
1509 if (l
== LEDGER_NULL
) {
1511 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
1513 if ((l
= ledger_instantiate(thread_ledger_template
, LEDGER_CREATE_INACTIVE_ENTRIES
)) == LEDGER_NULL
)
1514 return (KERN_RESOURCE_SHORTAGE
);
1517 * We are the first to create this thread's ledger, so only activate our entry.
1519 ledger_entry_setactive(l
, thread_ledgers
.cpu_time
);
1520 thread
->t_threadledger
= l
;
1524 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
1525 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
1527 limittime
= (interval_ns
* percentage
) / 100;
1528 nanoseconds_to_absolutetime(limittime
, &abstime
);
1529 ledger_set_limit(l
, thread_ledgers
.cpu_time
, abstime
);
1531 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
1533 ledger_set_period(l
, thread_ledgers
.cpu_time
, interval_ns
);
1536 * Ledgers supports multiple actions for one ledger entry, so we do too.
1538 if (action
== THREAD_CPULIMIT_EXCEPTION
) {
1539 thread
->options
|= TH_OPT_PROC_CPULIMIT
;
1540 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_EXCEPTION
);
1543 if (action
== THREAD_CPULIMIT_BLOCK
) {
1544 thread
->options
|= TH_OPT_PRVT_CPULIMIT
;
1545 /* The per-thread ledger template by default has a callback for CPU time */
1546 ledger_disable_callback(l
, thread_ledgers
.cpu_time
);
1547 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_BLOCK
);
1550 thread
->t_threadledger
= l
;
1554 int split_funnel_off
= 0;
1555 lck_grp_t
*funnel_lck_grp
= LCK_GRP_NULL
;
1556 lck_grp_attr_t
*funnel_lck_grp_attr
;
1557 lck_attr_t
*funnel_lck_attr
;
1566 if (funnel_lck_grp
== LCK_GRP_NULL
) {
1567 funnel_lck_grp_attr
= lck_grp_attr_alloc_init();
1569 funnel_lck_grp
= lck_grp_alloc_init("Funnel", funnel_lck_grp_attr
);
1571 funnel_lck_attr
= lck_attr_alloc_init();
1573 if ((fnl
= (funnel_t
*)kalloc(sizeof(funnel_t
))) != 0){
1574 bzero((void *)fnl
, sizeof(funnel_t
));
1575 if ((m
= lck_mtx_alloc_init(funnel_lck_grp
, funnel_lck_attr
)) == (lck_mtx_t
*)NULL
) {
1576 kfree(fnl
, sizeof(funnel_t
));
1577 return(THR_FUNNEL_NULL
);
1580 fnl
->fnl_type
= type
;
1589 lck_mtx_free(fnl
->fnl_mutex
, funnel_lck_grp
);
1590 if (fnl
->fnl_oldmutex
)
1591 lck_mtx_free(fnl
->fnl_oldmutex
, funnel_lck_grp
);
1592 kfree(fnl
, sizeof(funnel_t
));
1599 lck_mtx_lock(fnl
->fnl_mutex
);
1600 fnl
->fnl_mtxholder
= current_thread();
1607 lck_mtx_unlock(fnl
->fnl_mutex
);
1608 fnl
->fnl_mtxholder
= NULL
;
1609 fnl
->fnl_mtxrelease
= current_thread();
1616 thread_t th
= current_thread();
1618 if (th
->funnel_state
& TH_FN_OWNED
) {
1619 return(th
->funnel_lock
);
1621 return(THR_FUNNEL_NULL
);
1629 thread_t cur_thread
;
1630 boolean_t funnel_state_prev
;
1633 cur_thread
= current_thread();
1634 funnel_state_prev
= ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
);
1636 if (funnel_state_prev
!= funneled
) {
1637 intr
= ml_set_interrupts_enabled(FALSE
);
1639 if (funneled
== TRUE
) {
1640 if (cur_thread
->funnel_lock
)
1641 panic("Funnel lock called when holding one %p", cur_thread
->funnel_lock
);
1642 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
,
1645 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
,
1647 cur_thread
->funnel_state
|= TH_FN_OWNED
;
1648 cur_thread
->funnel_lock
= fnl
;
1650 if(cur_thread
->funnel_lock
->fnl_mutex
!= fnl
->fnl_mutex
)
1651 panic("Funnel unlock when not holding funnel");
1652 cur_thread
->funnel_state
&= ~TH_FN_OWNED
;
1653 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
,
1656 cur_thread
->funnel_lock
= THR_FUNNEL_NULL
;
1659 (void)ml_set_interrupts_enabled(intr
);
1661 /* if we are trying to acquire funnel recursively
1662 * check for funnel to be held already
1664 if (funneled
&& (fnl
->fnl_mutex
!= cur_thread
->funnel_lock
->fnl_mutex
)) {
1665 panic("thread_funnel_set: already holding a different funnel");
1668 return(funnel_state_prev
);
1674 __unused thread_t thread
)
1684 thread
->sched_call
= (call
!= NULL
)? call
: sched_call_null
;
1688 thread_static_param(
1692 thread_mtx_lock(thread
);
1693 thread
->static_param
= state
;
1694 thread_mtx_unlock(thread
);
1701 return (thread
!= THREAD_NULL
? thread
->thread_id
: 0);
1705 thread_dispatchqaddr(
1708 uint64_t dispatchqueue_addr
= 0;
1709 uint64_t thread_handle
= 0;
1711 if (thread
!= THREAD_NULL
) {
1712 thread_handle
= thread
->machine
.cthread_self
;
1714 if (thread
->task
->bsd_info
)
1715 dispatchqueue_addr
= thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1718 return (dispatchqueue_addr
);
1722 * Export routines to other components for things that are done as macros
1723 * within the osfmk component.
1726 #undef thread_reference
1727 void thread_reference(thread_t thread
);
1732 if (thread
!= THREAD_NULL
)
1733 thread_reference_internal(thread
);
1736 #undef thread_should_halt
1742 return (thread_should_halt_fast(th
));
1746 uint32_t dtrace_get_thread_predcache(thread_t thread
)
1748 if (thread
!= THREAD_NULL
)
1749 return thread
->t_dtrace_predcache
;
1754 int64_t dtrace_get_thread_vtime(thread_t thread
)
1756 if (thread
!= THREAD_NULL
)
1757 return thread
->t_dtrace_vtime
;
1762 int64_t dtrace_get_thread_tracing(thread_t thread
)
1764 if (thread
!= THREAD_NULL
)
1765 return thread
->t_dtrace_tracing
;
1770 boolean_t
dtrace_get_thread_reentering(thread_t thread
)
1772 if (thread
!= THREAD_NULL
)
1773 return (thread
->options
& TH_OPT_DTRACE
) ? TRUE
: FALSE
;
1778 vm_offset_t
dtrace_get_kernel_stack(thread_t thread
)
1780 if (thread
!= THREAD_NULL
)
1781 return thread
->kernel_stack
;
1786 int64_t dtrace_calc_thread_recent_vtime(thread_t thread
)
1788 if (thread
!= THREAD_NULL
) {
1789 processor_t processor
= current_processor();
1790 uint64_t abstime
= mach_absolute_time();
1793 timer
= PROCESSOR_DATA(processor
, thread_timer
);
1795 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
)) +
1796 (abstime
- timer
->tstamp
); /* XXX need interrupts off to prevent missed time? */
1801 void dtrace_set_thread_predcache(thread_t thread
, uint32_t predcache
)
1803 if (thread
!= THREAD_NULL
)
1804 thread
->t_dtrace_predcache
= predcache
;
1807 void dtrace_set_thread_vtime(thread_t thread
, int64_t vtime
)
1809 if (thread
!= THREAD_NULL
)
1810 thread
->t_dtrace_vtime
= vtime
;
1813 void dtrace_set_thread_tracing(thread_t thread
, int64_t accum
)
1815 if (thread
!= THREAD_NULL
)
1816 thread
->t_dtrace_tracing
= accum
;
1819 void dtrace_set_thread_reentering(thread_t thread
, boolean_t vbool
)
1821 if (thread
!= THREAD_NULL
) {
1823 thread
->options
|= TH_OPT_DTRACE
;
1825 thread
->options
&= (~TH_OPT_DTRACE
);
1829 vm_offset_t
dtrace_set_thread_recover(thread_t thread
, vm_offset_t recover
)
1831 vm_offset_t prev
= 0;
1833 if (thread
!= THREAD_NULL
) {
1834 prev
= thread
->recover
;
1835 thread
->recover
= recover
;
1840 void dtrace_thread_bootstrap(void)
1842 task_t task
= current_task();
1843 if(task
->thread_count
== 1) {
1846 DTRACE_PROC(lwp__start
);
1849 #endif /* CONFIG_DTRACE */