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 task
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
510 task
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
511 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
512 task
->thread_count
--;
515 * If the task is being halted, and there is only one thread
516 * left in the task after this one, then wakeup that thread.
518 if (task
->thread_count
== 1 && task
->halting
)
519 thread_wakeup((event_t
)&task
->halting
);
523 lck_mtx_lock(&tasks_threads_lock
);
524 queue_remove(&threads
, thread
, thread_t
, threads
);
526 lck_mtx_unlock(&tasks_threads_lock
);
528 thread_deallocate(thread
);
531 simple_lock(&thread_terminate_lock
);
534 assert_wait((event_t
)&thread_terminate_queue
, THREAD_UNINT
);
535 simple_unlock(&thread_terminate_lock
);
538 self
->options
&= ~TH_OPT_SYSTEM_CRITICAL
;
539 thread_block((thread_continue_t
)thread_terminate_daemon
);
544 * thread_terminate_enqueue:
546 * Enqueue a terminating thread for final disposition.
548 * Called at splsched.
551 thread_terminate_enqueue(
554 simple_lock(&thread_terminate_lock
);
555 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)thread
);
556 simple_unlock(&thread_terminate_lock
);
558 thread_wakeup((event_t
)&thread_terminate_queue
);
562 * thread_stack_daemon:
564 * Perform stack allocation as required due to
568 thread_stack_daemon(void)
572 simple_lock(&thread_stack_lock
);
574 while ((thread
= (thread_t
)dequeue_head(&thread_stack_queue
)) != THREAD_NULL
) {
575 simple_unlock(&thread_stack_lock
);
581 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
582 thread_unlock(thread
);
585 simple_lock(&thread_stack_lock
);
588 assert_wait((event_t
)&thread_stack_queue
, THREAD_UNINT
);
589 simple_unlock(&thread_stack_lock
);
591 thread_block((thread_continue_t
)thread_stack_daemon
);
596 * thread_stack_enqueue:
598 * Enqueue a thread for stack allocation.
600 * Called at splsched.
603 thread_stack_enqueue(
606 simple_lock(&thread_stack_lock
);
607 enqueue_tail(&thread_stack_queue
, (queue_entry_t
)thread
);
608 simple_unlock(&thread_stack_lock
);
610 thread_wakeup((event_t
)&thread_stack_queue
);
614 thread_daemon_init(void)
616 kern_return_t result
;
617 thread_t thread
= NULL
;
619 simple_lock_init(&thread_terminate_lock
, 0);
620 queue_init(&thread_terminate_queue
);
622 result
= kernel_thread_start_priority((thread_continue_t
)thread_terminate_daemon
, NULL
, MINPRI_KERNEL
, &thread
);
623 if (result
!= KERN_SUCCESS
)
624 panic("thread_daemon_init: thread_terminate_daemon");
626 thread_deallocate(thread
);
628 simple_lock_init(&thread_stack_lock
, 0);
629 queue_init(&thread_stack_queue
);
631 result
= kernel_thread_start_priority((thread_continue_t
)thread_stack_daemon
, NULL
, BASEPRI_PREEMPT
, &thread
);
632 if (result
!= KERN_SUCCESS
)
633 panic("thread_daemon_init: thread_stack_daemon");
635 thread_deallocate(thread
);
639 * Create a new thread.
640 * Doesn't start the thread running.
643 thread_create_internal(
646 thread_continue_t continuation
,
648 #define TH_OPTION_NONE 0x00
649 #define TH_OPTION_NOCRED 0x01
650 #define TH_OPTION_NOSUSP 0x02
651 thread_t
*out_thread
)
654 static thread_t first_thread
;
657 * Allocate a thread and initialize static fields
659 if (first_thread
== THREAD_NULL
)
660 new_thread
= first_thread
= current_thread();
662 new_thread
= (thread_t
)zalloc(thread_zone
);
663 if (new_thread
== THREAD_NULL
)
664 return (KERN_RESOURCE_SHORTAGE
);
666 if (new_thread
!= first_thread
)
667 *new_thread
= thread_template
;
670 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
, (options
& TH_OPTION_NOCRED
) != 0);
671 if (new_thread
->uthread
== NULL
) {
672 zfree(thread_zone
, new_thread
);
673 return (KERN_RESOURCE_SHORTAGE
);
675 #endif /* MACH_BSD */
677 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
679 void *ut
= new_thread
->uthread
;
681 new_thread
->uthread
= NULL
;
682 /* cred free may not be necessary */
683 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
684 uthread_cred_free(ut
);
685 uthread_zone_free(ut
);
686 #endif /* MACH_BSD */
688 zfree(thread_zone
, new_thread
);
689 return (KERN_FAILURE
);
692 new_thread
->task
= parent_task
;
694 thread_lock_init(new_thread
);
695 wake_lock_init(new_thread
);
697 lck_mtx_init(&new_thread
->mutex
, &thread_lck_grp
, &thread_lck_attr
);
699 ipc_thread_init(new_thread
);
700 queue_init(&new_thread
->held_ulocks
);
702 new_thread
->continuation
= continuation
;
704 lck_mtx_lock(&tasks_threads_lock
);
705 task_lock(parent_task
);
707 if ( !parent_task
->active
|| parent_task
->halting
||
708 ((options
& TH_OPTION_NOSUSP
) != 0 &&
709 parent_task
->suspend_count
> 0) ||
710 (parent_task
->thread_count
>= task_threadmax
&&
711 parent_task
!= kernel_task
) ) {
712 task_unlock(parent_task
);
713 lck_mtx_unlock(&tasks_threads_lock
);
717 void *ut
= new_thread
->uthread
;
719 new_thread
->uthread
= NULL
;
720 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
721 /* cred free may not be necessary */
722 uthread_cred_free(ut
);
723 uthread_zone_free(ut
);
725 #endif /* MACH_BSD */
726 ipc_thread_disable(new_thread
);
727 ipc_thread_terminate(new_thread
);
728 lck_mtx_destroy(&new_thread
->mutex
, &thread_lck_grp
);
729 machine_thread_destroy(new_thread
);
730 zfree(thread_zone
, new_thread
);
731 return (KERN_FAILURE
);
734 /* New threads inherit any default state on the task */
735 machine_thread_inherit_taskwide(new_thread
, parent_task
);
737 task_reference_internal(parent_task
);
739 if (new_thread
->task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
741 * This task has a per-thread CPU limit; make sure this new thread
742 * gets its limit set too, before it gets out of the kernel.
744 set_astledger(new_thread
);
746 new_thread
->t_threadledger
= LEDGER_NULL
; /* per thread ledger is not inherited */
747 new_thread
->t_ledger
= new_thread
->task
->ledger
;
748 if (new_thread
->t_ledger
)
749 ledger_reference(new_thread
->t_ledger
);
751 /* Cache the task's map */
752 new_thread
->map
= parent_task
->map
;
754 /* Chain the thread onto the task's list */
755 queue_enter(&parent_task
->threads
, new_thread
, thread_t
, task_threads
);
756 parent_task
->thread_count
++;
758 /* So terminating threads don't need to take the task lock to decrement */
759 hw_atomic_add(&parent_task
->active_thread_count
, 1);
761 /* Protected by the tasks_threads_lock */
762 new_thread
->thread_id
= ++thread_unique_id
;
764 queue_enter(&threads
, new_thread
, thread_t
, threads
);
767 timer_call_setup(&new_thread
->wait_timer
, thread_timer_expire
, new_thread
);
768 timer_call_setup(&new_thread
->depress_timer
, thread_depress_expire
, new_thread
);
772 * If parent task has any reservations, they need to be propagated to this
775 new_thread
->t_chud
= (TASK_PMC_FLAG
== (parent_task
->t_chud
& TASK_PMC_FLAG
)) ?
776 THREAD_PMC_FLAG
: 0U;
779 /* Set the thread's scheduling parameters */
780 new_thread
->sched_mode
= SCHED(initial_thread_sched_mode
)(parent_task
);
781 new_thread
->sched_flags
= 0;
782 new_thread
->max_priority
= parent_task
->max_priority
;
783 new_thread
->task_priority
= parent_task
->priority
;
784 new_thread
->priority
= (priority
< 0)? parent_task
->priority
: priority
;
785 if (new_thread
->priority
> new_thread
->max_priority
)
786 new_thread
->priority
= new_thread
->max_priority
;
788 if (new_thread
->priority
< MAXPRI_THROTTLE
) {
789 new_thread
->priority
= MAXPRI_THROTTLE
;
791 #endif /* CONFIG_EMBEDDED */
792 new_thread
->importance
=
793 new_thread
->priority
- new_thread
->task_priority
;
795 new_thread
->saved_importance
= new_thread
->importance
;
796 /* apple ios daemon starts all threads in darwin background */
797 if (parent_task
->ext_appliedstate
.apptype
== PROC_POLICY_IOS_APPLE_DAEMON
) {
798 /* Cannot use generic routines here so apply darwin bacground directly */
799 new_thread
->policystate
.hw_bg
= TASK_POLICY_BACKGROUND_ATTRIBUTE_ALL
;
800 /* set thread self backgrounding */
801 new_thread
->appliedstate
.hw_bg
= new_thread
->policystate
.hw_bg
;
802 /* priority will get recomputed suitably bit later */
803 new_thread
->importance
= INT_MIN
;
804 /* to avoid changes to many pri compute routines, set the effect of those here */
805 new_thread
->priority
= MAXPRI_THROTTLE
;
807 #endif /* CONFIG_EMBEDDED */
809 #if defined(CONFIG_SCHED_TRADITIONAL)
810 new_thread
->sched_stamp
= sched_tick
;
811 new_thread
->pri_shift
= sched_pri_shift
;
813 SCHED(compute_priority
)(new_thread
, FALSE
);
815 new_thread
->active
= TRUE
;
817 *out_thread
= new_thread
;
820 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
822 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
824 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
825 TRACEDBG_CODE(DBG_TRACE_DATA
, 1) | DBG_FUNC_NONE
,
826 (vm_address_t
)(uintptr_t)thread_tid(new_thread
), dbg_arg2
, 0, 0, 0);
828 kdbg_trace_string(parent_task
->bsd_info
,
829 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
831 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
832 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
833 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
836 DTRACE_PROC1(lwp__create
, thread_t
, *out_thread
);
838 return (KERN_SUCCESS
);
842 thread_create_internal2(
844 thread_t
*new_thread
,
847 kern_return_t result
;
850 if (task
== TASK_NULL
|| task
== kernel_task
)
851 return (KERN_INVALID_ARGUMENT
);
853 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
854 if (result
!= KERN_SUCCESS
)
857 thread
->user_stop_count
= 1;
859 if (task
->suspend_count
> 0)
863 extmod_statistics_incr_thread_create(task
);
866 lck_mtx_unlock(&tasks_threads_lock
);
868 *new_thread
= thread
;
870 return (KERN_SUCCESS
);
873 /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
877 thread_t
*new_thread
);
882 thread_t
*new_thread
)
884 return thread_create_internal2(task
, new_thread
, FALSE
);
888 thread_create_from_user(
890 thread_t
*new_thread
)
892 return thread_create_internal2(task
, new_thread
, TRUE
);
896 thread_create_running_internal2(
897 register task_t task
,
899 thread_state_t new_state
,
900 mach_msg_type_number_t new_state_count
,
901 thread_t
*new_thread
,
904 register kern_return_t result
;
907 if (task
== TASK_NULL
|| task
== kernel_task
)
908 return (KERN_INVALID_ARGUMENT
);
910 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
911 if (result
!= KERN_SUCCESS
)
914 result
= machine_thread_set_state(
915 thread
, flavor
, new_state
, new_state_count
);
916 if (result
!= KERN_SUCCESS
) {
918 lck_mtx_unlock(&tasks_threads_lock
);
920 thread_terminate(thread
);
921 thread_deallocate(thread
);
925 thread_mtx_lock(thread
);
926 thread_start_internal(thread
);
927 thread_mtx_unlock(thread
);
930 extmod_statistics_incr_thread_create(task
);
933 lck_mtx_unlock(&tasks_threads_lock
);
935 *new_thread
= thread
;
940 /* Prototype, see justification above */
942 thread_create_running(
943 register task_t task
,
945 thread_state_t new_state
,
946 mach_msg_type_number_t new_state_count
,
947 thread_t
*new_thread
);
950 thread_create_running(
951 register task_t task
,
953 thread_state_t new_state
,
954 mach_msg_type_number_t new_state_count
,
955 thread_t
*new_thread
)
957 return thread_create_running_internal2(
958 task
, flavor
, new_state
, new_state_count
,
963 thread_create_running_from_user(
964 register task_t task
,
966 thread_state_t new_state
,
967 mach_msg_type_number_t new_state_count
,
968 thread_t
*new_thread
)
970 return thread_create_running_internal2(
971 task
, flavor
, new_state
, new_state_count
,
978 thread_continue_t thread_return
,
979 thread_t
*new_thread
)
981 kern_return_t result
;
984 if (task
== TASK_NULL
|| task
== kernel_task
)
985 return (KERN_INVALID_ARGUMENT
);
987 result
= thread_create_internal(task
, -1, thread_return
, TH_OPTION_NOCRED
| TH_OPTION_NOSUSP
, &thread
);
988 if (result
!= KERN_SUCCESS
)
991 thread
->user_stop_count
= 1;
993 if (task
->suspend_count
> 0)
997 lck_mtx_unlock(&tasks_threads_lock
);
999 *new_thread
= thread
;
1001 return (KERN_SUCCESS
);
1005 * kernel_thread_create:
1007 * Create a thread in the kernel task
1008 * to execute in kernel context.
1011 kernel_thread_create(
1012 thread_continue_t continuation
,
1015 thread_t
*new_thread
)
1017 kern_return_t result
;
1019 task_t task
= kernel_task
;
1021 result
= thread_create_internal(task
, priority
, continuation
, TH_OPTION_NONE
, &thread
);
1022 if (result
!= KERN_SUCCESS
)
1026 lck_mtx_unlock(&tasks_threads_lock
);
1028 stack_alloc(thread
);
1029 assert(thread
->kernel_stack
!= 0);
1031 if (priority
> BASEPRI_KERNEL
)
1033 thread
->reserved_stack
= thread
->kernel_stack
;
1035 thread
->parameter
= parameter
;
1038 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread
, continuation
);
1039 *new_thread
= thread
;
1045 kernel_thread_start_priority(
1046 thread_continue_t continuation
,
1049 thread_t
*new_thread
)
1051 kern_return_t result
;
1054 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
1055 if (result
!= KERN_SUCCESS
)
1058 *new_thread
= thread
;
1060 thread_mtx_lock(thread
);
1061 thread_start_internal(thread
);
1062 thread_mtx_unlock(thread
);
1068 kernel_thread_start(
1069 thread_continue_t continuation
,
1071 thread_t
*new_thread
)
1073 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
1076 #if defined(__i386__)
1081 void (*start
)(void))
1083 kern_return_t result
;
1086 if (task
!= kernel_task
)
1087 panic("kernel_thread");
1089 result
= kernel_thread_start_priority((thread_continue_t
)start
, NULL
, -1, &thread
);
1090 if (result
!= KERN_SUCCESS
)
1091 return (THREAD_NULL
);
1093 thread_deallocate(thread
);
1098 #endif /* defined(__i386__) */
1101 thread_info_internal(
1102 register thread_t thread
,
1103 thread_flavor_t flavor
,
1104 thread_info_t thread_info_out
, /* ptr to OUT array */
1105 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
1110 if (thread
== THREAD_NULL
)
1111 return (KERN_INVALID_ARGUMENT
);
1113 if (flavor
== THREAD_BASIC_INFO
) {
1114 register thread_basic_info_t basic_info
;
1116 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
1117 return (KERN_INVALID_ARGUMENT
);
1119 basic_info
= (thread_basic_info_t
) thread_info_out
;
1122 thread_lock(thread
);
1126 thread_read_times(thread
, &basic_info
->user_time
,
1127 &basic_info
->system_time
);
1130 * Update lazy-evaluated scheduler info because someone wants it.
1132 if (SCHED(can_update_priority
)(thread
))
1133 SCHED(update_priority
)(thread
);
1135 basic_info
->sleep_time
= 0;
1138 * To calculate cpu_usage, first correct for timer rate,
1139 * then for 5/8 ageing. The correction factor [3/5] is
1142 basic_info
->cpu_usage
= 0;
1143 #if defined(CONFIG_SCHED_TRADITIONAL)
1144 if (sched_tick_interval
) {
1145 basic_info
->cpu_usage
= (integer_t
)(((uint64_t)thread
->cpu_usage
1146 * TH_USAGE_SCALE
) / sched_tick_interval
);
1147 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
1151 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
)
1152 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
1154 basic_info
->policy
= ((thread
->sched_mode
== TH_MODE_TIMESHARE
)?
1155 POLICY_TIMESHARE
: POLICY_RR
);
1158 if (thread
->bound_processor
!= PROCESSOR_NULL
&& thread
->bound_processor
->idle_thread
== thread
)
1159 flags
|= TH_FLAGS_IDLE
;
1161 if (!thread
->kernel_stack
)
1162 flags
|= TH_FLAGS_SWAPPED
;
1165 if (thread
->state
& TH_TERMINATE
)
1166 state
= TH_STATE_HALTED
;
1168 if (thread
->state
& TH_RUN
)
1169 state
= TH_STATE_RUNNING
;
1171 if (thread
->state
& TH_UNINT
)
1172 state
= TH_STATE_UNINTERRUPTIBLE
;
1174 if (thread
->state
& TH_SUSP
)
1175 state
= TH_STATE_STOPPED
;
1177 if (thread
->state
& TH_WAIT
)
1178 state
= TH_STATE_WAITING
;
1180 basic_info
->run_state
= state
;
1181 basic_info
->flags
= flags
;
1183 basic_info
->suspend_count
= thread
->user_stop_count
;
1185 thread_unlock(thread
);
1188 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
1190 return (KERN_SUCCESS
);
1193 if (flavor
== THREAD_IDENTIFIER_INFO
) {
1194 register thread_identifier_info_t identifier_info
;
1196 if (*thread_info_count
< THREAD_IDENTIFIER_INFO_COUNT
)
1197 return (KERN_INVALID_ARGUMENT
);
1199 identifier_info
= (thread_identifier_info_t
) thread_info_out
;
1202 thread_lock(thread
);
1204 identifier_info
->thread_id
= thread
->thread_id
;
1205 identifier_info
->thread_handle
= thread
->machine
.cthread_self
;
1206 if(thread
->task
->bsd_info
) {
1207 identifier_info
->dispatch_qaddr
= identifier_info
->thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1209 thread_unlock(thread
);
1211 return KERN_INVALID_ARGUMENT
;
1214 thread_unlock(thread
);
1216 return KERN_SUCCESS
;
1219 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
1220 policy_timeshare_info_t ts_info
;
1222 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
1223 return (KERN_INVALID_ARGUMENT
);
1225 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
1228 thread_lock(thread
);
1230 if (thread
->sched_mode
!= TH_MODE_TIMESHARE
) {
1231 thread_unlock(thread
);
1234 return (KERN_INVALID_POLICY
);
1237 ts_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1238 if (ts_info
->depressed
) {
1239 ts_info
->base_priority
= DEPRESSPRI
;
1240 ts_info
->depress_priority
= thread
->priority
;
1243 ts_info
->base_priority
= thread
->priority
;
1244 ts_info
->depress_priority
= -1;
1247 ts_info
->cur_priority
= thread
->sched_pri
;
1248 ts_info
->max_priority
= thread
->max_priority
;
1250 thread_unlock(thread
);
1253 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
1255 return (KERN_SUCCESS
);
1258 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
1259 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
1260 return (KERN_INVALID_ARGUMENT
);
1262 return (KERN_INVALID_POLICY
);
1265 if (flavor
== THREAD_SCHED_RR_INFO
) {
1266 policy_rr_info_t rr_info
;
1267 uint32_t quantum_time
;
1268 uint64_t quantum_ns
;
1270 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
1271 return (KERN_INVALID_ARGUMENT
);
1273 rr_info
= (policy_rr_info_t
) thread_info_out
;
1276 thread_lock(thread
);
1278 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
1279 thread_unlock(thread
);
1282 return (KERN_INVALID_POLICY
);
1285 rr_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1286 if (rr_info
->depressed
) {
1287 rr_info
->base_priority
= DEPRESSPRI
;
1288 rr_info
->depress_priority
= thread
->priority
;
1291 rr_info
->base_priority
= thread
->priority
;
1292 rr_info
->depress_priority
= -1;
1295 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
1296 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
1298 rr_info
->max_priority
= thread
->max_priority
;
1299 rr_info
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
1301 thread_unlock(thread
);
1304 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1306 return (KERN_SUCCESS
);
1309 return (KERN_INVALID_ARGUMENT
);
1315 time_value_t
*user_time
,
1316 time_value_t
*system_time
)
1320 uint64_t tval_user
, tval_system
;
1322 tval_user
= timer_grab(&thread
->user_timer
);
1323 tval_system
= timer_grab(&thread
->system_timer
);
1325 if (thread
->precise_user_kernel_time
) {
1326 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1327 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1328 user_time
->microseconds
= usecs
;
1330 absolutetime_to_microtime(tval_system
, &secs
, &usecs
);
1331 system_time
->seconds
= (typeof(system_time
->seconds
))secs
;
1332 system_time
->microseconds
= usecs
;
1334 /* system_timer may represent either sys or user */
1335 tval_user
+= tval_system
;
1336 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1337 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1338 user_time
->microseconds
= usecs
;
1340 system_time
->seconds
= 0;
1341 system_time
->microseconds
= 0;
1347 __unused thread_t thread
,
1348 __unused processor_set_t new_pset
)
1350 return (KERN_FAILURE
);
1354 * thread_assign_default:
1356 * Special version of thread_assign for assigning threads to default
1360 thread_assign_default(
1363 return (thread_assign(thread
, &pset0
));
1367 * thread_get_assignment
1369 * Return current assignment for this thread.
1372 thread_get_assignment(
1374 processor_set_t
*pset
)
1377 return (KERN_INVALID_ARGUMENT
);
1381 return (KERN_SUCCESS
);
1385 * thread_wire_internal:
1387 * Specify that the target thread must always be able
1388 * to run and to allocate memory.
1391 thread_wire_internal(
1392 host_priv_t host_priv
,
1395 boolean_t
*prev_state
)
1397 if (host_priv
== NULL
|| thread
!= current_thread())
1398 return (KERN_INVALID_ARGUMENT
);
1400 assert(host_priv
== &realhost
);
1403 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
1406 if (!(thread
->options
& TH_OPT_VMPRIV
))
1407 vm_page_free_reserve(1); /* XXX */
1408 thread
->options
|= TH_OPT_VMPRIV
;
1411 if (thread
->options
& TH_OPT_VMPRIV
)
1412 vm_page_free_reserve(-1); /* XXX */
1413 thread
->options
&= ~TH_OPT_VMPRIV
;
1416 return (KERN_SUCCESS
);
1423 * User-api wrapper for thread_wire_internal()
1427 host_priv_t host_priv
,
1431 return (thread_wire_internal(host_priv
, thread
, wired
, NULL
));
1435 thread_resource_exception(const void *arg0
, __unused
const void *arg1
)
1437 thread_t thread
= current_thread();
1438 int code
= (int)((uintptr_t)arg0
& ((int)-1));
1440 assert(thread
->t_threadledger
!= LEDGER_NULL
);
1443 * Disable the exception notification so we don't overwhelm
1444 * the listener with an endless stream of redundant exceptions.
1446 ledger_set_action(thread
->t_threadledger
, thread_ledgers
.cpu_time
,
1447 LEDGER_ACTION_IGNORE
);
1448 ledger_disable_callback(thread
->t_threadledger
, thread_ledgers
.cpu_time
);
1450 /* XXX code should eventually be a user-exported namespace of resources */
1451 (void) task_exception_notify(EXC_RESOURCE
, code
, 0);
1455 init_thread_ledgers(void) {
1456 ledger_template_t t
;
1459 assert(thread_ledger_template
== NULL
);
1461 if ((t
= ledger_template_create("Per-thread ledger")) == NULL
)
1462 panic("couldn't create thread ledger template");
1464 if ((idx
= ledger_entry_add(t
, "cpu_time", "sched", "ns")) < 0) {
1465 panic("couldn't create cpu_time entry for thread ledger template");
1468 if (ledger_set_callback(t
, idx
, thread_resource_exception
,
1469 (void *)(uintptr_t)idx
, NULL
) < 0) {
1470 panic("couldn't set thread ledger callback for cpu_time entry");
1473 thread_ledgers
.cpu_time
= idx
;
1474 thread_ledger_template
= t
;
1478 * Set CPU usage limit on a thread.
1480 * Calling with percentage of 0 will unset the limit for this thread.
1484 thread_set_cpulimit(int action
, uint8_t percentage
, uint64_t interval_ns
)
1486 thread_t thread
= current_thread();
1488 uint64_t limittime
= 0;
1489 uint64_t abstime
= 0;
1491 assert(percentage
<= 100);
1493 if (percentage
== 0) {
1495 * Remove CPU limit, if any exists.
1497 if (thread
->t_threadledger
!= LEDGER_NULL
) {
1499 * The only way to get a per-thread ledger is via CPU limits.
1501 assert(thread
->options
& (TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
));
1502 ledger_dereference(thread
->t_threadledger
);
1503 thread
->t_threadledger
= LEDGER_NULL
;
1504 thread
->options
&= ~(TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
);
1510 l
= thread
->t_threadledger
;
1511 if (l
== LEDGER_NULL
) {
1513 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
1515 if ((l
= ledger_instantiate(thread_ledger_template
, LEDGER_CREATE_INACTIVE_ENTRIES
)) == LEDGER_NULL
)
1516 return (KERN_RESOURCE_SHORTAGE
);
1519 * We are the first to create this thread's ledger, so only activate our entry.
1521 ledger_entry_setactive(l
, thread_ledgers
.cpu_time
);
1522 thread
->t_threadledger
= l
;
1526 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
1527 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
1529 limittime
= (interval_ns
* percentage
) / 100;
1530 nanoseconds_to_absolutetime(limittime
, &abstime
);
1531 ledger_set_limit(l
, thread_ledgers
.cpu_time
, abstime
);
1533 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
1535 ledger_set_period(l
, thread_ledgers
.cpu_time
, interval_ns
);
1538 * Ledgers supports multiple actions for one ledger entry, so we do too.
1540 if (action
== THREAD_CPULIMIT_EXCEPTION
) {
1541 thread
->options
|= TH_OPT_PROC_CPULIMIT
;
1542 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_EXCEPTION
);
1545 if (action
== THREAD_CPULIMIT_BLOCK
) {
1546 thread
->options
|= TH_OPT_PRVT_CPULIMIT
;
1547 /* The per-thread ledger template by default has a callback for CPU time */
1548 ledger_disable_callback(l
, thread_ledgers
.cpu_time
);
1549 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_BLOCK
);
1552 thread
->t_threadledger
= l
;
1556 int split_funnel_off
= 0;
1557 lck_grp_t
*funnel_lck_grp
= LCK_GRP_NULL
;
1558 lck_grp_attr_t
*funnel_lck_grp_attr
;
1559 lck_attr_t
*funnel_lck_attr
;
1568 if (funnel_lck_grp
== LCK_GRP_NULL
) {
1569 funnel_lck_grp_attr
= lck_grp_attr_alloc_init();
1571 funnel_lck_grp
= lck_grp_alloc_init("Funnel", funnel_lck_grp_attr
);
1573 funnel_lck_attr
= lck_attr_alloc_init();
1575 if ((fnl
= (funnel_t
*)kalloc(sizeof(funnel_t
))) != 0){
1576 bzero((void *)fnl
, sizeof(funnel_t
));
1577 if ((m
= lck_mtx_alloc_init(funnel_lck_grp
, funnel_lck_attr
)) == (lck_mtx_t
*)NULL
) {
1578 kfree(fnl
, sizeof(funnel_t
));
1579 return(THR_FUNNEL_NULL
);
1582 fnl
->fnl_type
= type
;
1591 lck_mtx_free(fnl
->fnl_mutex
, funnel_lck_grp
);
1592 if (fnl
->fnl_oldmutex
)
1593 lck_mtx_free(fnl
->fnl_oldmutex
, funnel_lck_grp
);
1594 kfree(fnl
, sizeof(funnel_t
));
1601 lck_mtx_lock(fnl
->fnl_mutex
);
1602 fnl
->fnl_mtxholder
= current_thread();
1609 lck_mtx_unlock(fnl
->fnl_mutex
);
1610 fnl
->fnl_mtxholder
= NULL
;
1611 fnl
->fnl_mtxrelease
= current_thread();
1618 thread_t th
= current_thread();
1620 if (th
->funnel_state
& TH_FN_OWNED
) {
1621 return(th
->funnel_lock
);
1623 return(THR_FUNNEL_NULL
);
1631 thread_t cur_thread
;
1632 boolean_t funnel_state_prev
;
1635 cur_thread
= current_thread();
1636 funnel_state_prev
= ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
);
1638 if (funnel_state_prev
!= funneled
) {
1639 intr
= ml_set_interrupts_enabled(FALSE
);
1641 if (funneled
== TRUE
) {
1642 if (cur_thread
->funnel_lock
)
1643 panic("Funnel lock called when holding one %p", cur_thread
->funnel_lock
);
1644 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
,
1647 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
,
1649 cur_thread
->funnel_state
|= TH_FN_OWNED
;
1650 cur_thread
->funnel_lock
= fnl
;
1652 if(cur_thread
->funnel_lock
->fnl_mutex
!= fnl
->fnl_mutex
)
1653 panic("Funnel unlock when not holding funnel");
1654 cur_thread
->funnel_state
&= ~TH_FN_OWNED
;
1655 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
,
1658 cur_thread
->funnel_lock
= THR_FUNNEL_NULL
;
1661 (void)ml_set_interrupts_enabled(intr
);
1663 /* if we are trying to acquire funnel recursively
1664 * check for funnel to be held already
1666 if (funneled
&& (fnl
->fnl_mutex
!= cur_thread
->funnel_lock
->fnl_mutex
)) {
1667 panic("thread_funnel_set: already holding a different funnel");
1670 return(funnel_state_prev
);
1676 __unused thread_t thread
)
1686 thread
->sched_call
= (call
!= NULL
)? call
: sched_call_null
;
1690 thread_static_param(
1694 thread_mtx_lock(thread
);
1695 thread
->static_param
= state
;
1696 thread_mtx_unlock(thread
);
1703 return (thread
!= THREAD_NULL
? thread
->thread_id
: 0);
1707 thread_set_tag(thread_t th
, uint16_t tag
) {
1708 return thread_set_tag_internal(th
, tag
);
1711 thread_get_tag(thread_t th
) {
1712 return thread_get_tag_internal(th
);
1716 thread_dispatchqaddr(
1719 uint64_t dispatchqueue_addr
= 0;
1720 uint64_t thread_handle
= 0;
1722 if (thread
!= THREAD_NULL
) {
1723 thread_handle
= thread
->machine
.cthread_self
;
1725 if (thread
->task
->bsd_info
)
1726 dispatchqueue_addr
= thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1729 return (dispatchqueue_addr
);
1733 * Export routines to other components for things that are done as macros
1734 * within the osfmk component.
1737 #undef thread_reference
1738 void thread_reference(thread_t thread
);
1743 if (thread
!= THREAD_NULL
)
1744 thread_reference_internal(thread
);
1747 #undef thread_should_halt
1753 return (thread_should_halt_fast(th
));
1757 uint32_t dtrace_get_thread_predcache(thread_t thread
)
1759 if (thread
!= THREAD_NULL
)
1760 return thread
->t_dtrace_predcache
;
1765 int64_t dtrace_get_thread_vtime(thread_t thread
)
1767 if (thread
!= THREAD_NULL
)
1768 return thread
->t_dtrace_vtime
;
1773 int64_t dtrace_get_thread_tracing(thread_t thread
)
1775 if (thread
!= THREAD_NULL
)
1776 return thread
->t_dtrace_tracing
;
1781 boolean_t
dtrace_get_thread_reentering(thread_t thread
)
1783 if (thread
!= THREAD_NULL
)
1784 return (thread
->options
& TH_OPT_DTRACE
) ? TRUE
: FALSE
;
1789 vm_offset_t
dtrace_get_kernel_stack(thread_t thread
)
1791 if (thread
!= THREAD_NULL
)
1792 return thread
->kernel_stack
;
1797 int64_t dtrace_calc_thread_recent_vtime(thread_t thread
)
1799 if (thread
!= THREAD_NULL
) {
1800 processor_t processor
= current_processor();
1801 uint64_t abstime
= mach_absolute_time();
1804 timer
= PROCESSOR_DATA(processor
, thread_timer
);
1806 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
)) +
1807 (abstime
- timer
->tstamp
); /* XXX need interrupts off to prevent missed time? */
1812 void dtrace_set_thread_predcache(thread_t thread
, uint32_t predcache
)
1814 if (thread
!= THREAD_NULL
)
1815 thread
->t_dtrace_predcache
= predcache
;
1818 void dtrace_set_thread_vtime(thread_t thread
, int64_t vtime
)
1820 if (thread
!= THREAD_NULL
)
1821 thread
->t_dtrace_vtime
= vtime
;
1824 void dtrace_set_thread_tracing(thread_t thread
, int64_t accum
)
1826 if (thread
!= THREAD_NULL
)
1827 thread
->t_dtrace_tracing
= accum
;
1830 void dtrace_set_thread_reentering(thread_t thread
, boolean_t vbool
)
1832 if (thread
!= THREAD_NULL
) {
1834 thread
->options
|= TH_OPT_DTRACE
;
1836 thread
->options
&= (~TH_OPT_DTRACE
);
1840 vm_offset_t
dtrace_set_thread_recover(thread_t thread
, vm_offset_t recover
)
1842 vm_offset_t prev
= 0;
1844 if (thread
!= THREAD_NULL
) {
1845 prev
= thread
->recover
;
1846 thread
->recover
= recover
;
1851 void dtrace_thread_bootstrap(void)
1853 task_t task
= current_task();
1854 if(task
->thread_count
== 1) {
1857 DTRACE_PROC(lwp__start
);
1860 #endif /* CONFIG_DTRACE */