2 * Copyright (c) 2000-2012 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>
118 #include <kern/exc_resource.h>
119 #include <kern/telemetry.h>
121 #include <kern/kpc.h>
124 #include <ipc/ipc_kmsg.h>
125 #include <ipc/ipc_port.h>
127 #include <vm/vm_kern.h>
128 #include <vm/vm_pageout.h>
130 #include <sys/kdebug.h>
132 #include <mach/sdt.h>
135 * Exported interfaces
137 #include <mach/task_server.h>
138 #include <mach/thread_act_server.h>
139 #include <mach/mach_host_server.h>
140 #include <mach/host_priv_server.h>
142 static struct zone
*thread_zone
;
143 static lck_grp_attr_t thread_lck_grp_attr
;
144 lck_attr_t thread_lck_attr
;
145 lck_grp_t thread_lck_grp
;
147 decl_simple_lock_data(static,thread_stack_lock
)
148 static queue_head_t thread_stack_queue
;
150 decl_simple_lock_data(static,thread_terminate_lock
)
151 static queue_head_t thread_terminate_queue
;
153 static struct thread thread_template
, init_thread
;
155 static void sched_call_null(
160 extern void proc_exit(void *);
161 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
162 extern int proc_selfpid(void);
163 extern char * proc_name_address(void *p
);
164 #endif /* MACH_BSD */
166 extern int disable_exc_resource
;
167 extern int audio_active
;
168 extern int debug_task
;
169 int thread_max
= CONFIG_THREAD_MAX
; /* Max number of threads */
170 int task_threadmax
= CONFIG_THREAD_MAX
;
172 static uint64_t thread_unique_id
= 0;
174 struct _thread_ledger_indices thread_ledgers
= { -1 };
175 static ledger_template_t thread_ledger_template
= NULL
;
176 void init_thread_ledgers(void);
177 int task_disable_cpumon(task_t task
);
180 * Level (in terms of percentage of the limit) at which the CPU usage monitor triggers telemetry.
182 * (ie when any thread's CPU consumption exceeds 70% of the limit, start taking user
183 * stacktraces, aka micro-stackshots)
185 #define CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT 70
187 int cpumon_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
188 void __attribute__((noinline
)) THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void);
191 * The smallest interval over which we support limiting CPU consumption is 1ms
193 #define MINIMUM_CPULIMIT_INTERVAL_MS 1
196 thread_bootstrap(void)
199 * Fill in a template thread for fast initialization.
202 thread_template
.runq
= PROCESSOR_NULL
;
204 thread_template
.ref_count
= 2;
206 thread_template
.reason
= AST_NONE
;
207 thread_template
.at_safe_point
= FALSE
;
208 thread_template
.wait_event
= NO_EVENT64
;
209 thread_template
.wait_queue
= WAIT_QUEUE_NULL
;
210 thread_template
.wait_result
= THREAD_WAITING
;
211 thread_template
.options
= THREAD_ABORTSAFE
;
212 thread_template
.state
= TH_WAIT
| TH_UNINT
;
213 thread_template
.wake_active
= FALSE
;
214 thread_template
.continuation
= THREAD_CONTINUE_NULL
;
215 thread_template
.parameter
= NULL
;
217 thread_template
.importance
= 0;
218 thread_template
.sched_mode
= TH_MODE_NONE
;
219 thread_template
.sched_flags
= 0;
220 thread_template
.saved_mode
= TH_MODE_NONE
;
221 thread_template
.safe_release
= 0;
223 thread_template
.priority
= 0;
224 thread_template
.sched_pri
= 0;
225 thread_template
.max_priority
= 0;
226 thread_template
.task_priority
= 0;
227 thread_template
.promotions
= 0;
228 thread_template
.pending_promoter_index
= 0;
229 thread_template
.pending_promoter
[0] =
230 thread_template
.pending_promoter
[1] = NULL
;
231 thread_template
.rwlock_count
= 0;
233 thread_template
.realtime
.deadline
= UINT64_MAX
;
235 thread_template
.current_quantum
= 0;
236 thread_template
.last_run_time
= 0;
237 thread_template
.last_quantum_refill_time
= 0;
239 thread_template
.computation_metered
= 0;
240 thread_template
.computation_epoch
= 0;
242 #if defined(CONFIG_SCHED_TRADITIONAL)
243 thread_template
.sched_stamp
= 0;
244 thread_template
.pri_shift
= INT8_MAX
;
245 thread_template
.sched_usage
= 0;
246 thread_template
.cpu_usage
= thread_template
.cpu_delta
= 0;
248 thread_template
.c_switch
= thread_template
.p_switch
= thread_template
.ps_switch
= 0;
250 thread_template
.bound_processor
= PROCESSOR_NULL
;
251 thread_template
.last_processor
= PROCESSOR_NULL
;
253 thread_template
.sched_call
= sched_call_null
;
255 timer_init(&thread_template
.user_timer
);
256 timer_init(&thread_template
.system_timer
);
257 thread_template
.user_timer_save
= 0;
258 thread_template
.system_timer_save
= 0;
259 thread_template
.vtimer_user_save
= 0;
260 thread_template
.vtimer_prof_save
= 0;
261 thread_template
.vtimer_rlim_save
= 0;
263 thread_template
.wait_timer_is_set
= FALSE
;
264 thread_template
.wait_timer_active
= 0;
266 thread_template
.depress_timer_active
= 0;
268 thread_template
.special_handler
.handler
= special_handler
;
269 thread_template
.special_handler
.next
= NULL
;
271 thread_template
.funnel_lock
= THR_FUNNEL_NULL
;
272 thread_template
.funnel_state
= 0;
273 thread_template
.recover
= (vm_offset_t
)NULL
;
275 thread_template
.map
= VM_MAP_NULL
;
278 thread_template
.t_dtrace_predcache
= 0;
279 thread_template
.t_dtrace_vtime
= 0;
280 thread_template
.t_dtrace_tracing
= 0;
281 #endif /* CONFIG_DTRACE */
284 thread_template
.kpc_buf
= NULL
;
287 thread_template
.t_chud
= 0;
288 thread_template
.t_page_creation_count
= 0;
289 thread_template
.t_page_creation_time
= 0;
291 thread_template
.affinity_set
= NULL
;
293 thread_template
.syscalls_unix
= 0;
294 thread_template
.syscalls_mach
= 0;
296 thread_template
.t_ledger
= LEDGER_NULL
;
297 thread_template
.t_threadledger
= LEDGER_NULL
;
299 thread_template
.requested_policy
= default_task_requested_policy
;
300 thread_template
.effective_policy
= default_task_effective_policy
;
301 thread_template
.pended_policy
= default_task_pended_policy
;
303 thread_template
.iotier_override
= THROTTLE_LEVEL_NONE
;
305 thread_template
.thread_callout_interrupt_wakeups
= thread_template
.thread_callout_platform_idle_wakeups
= 0;
307 thread_template
.thread_timer_wakeups_bin_1
= thread_template
.thread_timer_wakeups_bin_2
= 0;
308 thread_template
.callout_woken_from_icontext
= thread_template
.callout_woken_from_platform_idle
= 0;
310 thread_template
.thread_tag
= 0;
312 init_thread
= thread_template
;
313 machine_set_current_thread(&init_thread
);
320 sizeof(struct thread
),
321 thread_max
* sizeof(struct thread
),
322 THREAD_CHUNK
* sizeof(struct thread
),
325 lck_grp_attr_setdefault(&thread_lck_grp_attr
);
326 lck_grp_init(&thread_lck_grp
, "thread", &thread_lck_grp_attr
);
327 lck_attr_setdefault(&thread_lck_attr
);
332 * Initialize any machine-dependent
333 * per-thread structures necessary.
335 machine_thread_init();
337 if (!PE_parse_boot_argn("cpumon_ustackshots_trigger_pct", &cpumon_ustackshots_trigger_pct
,
338 sizeof (cpumon_ustackshots_trigger_pct
))) {
339 cpumon_ustackshots_trigger_pct
= CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT
;
342 init_thread_ledgers();
346 thread_terminate_continue(void)
348 panic("thread_terminate_continue");
353 * thread_terminate_self:
356 thread_terminate_self(void)
358 thread_t thread
= current_thread();
364 pal_thread_terminate_self(thread
);
366 DTRACE_PROC(lwp__exit
);
368 thread_mtx_lock(thread
);
370 ipc_thread_disable(thread
);
372 thread_mtx_unlock(thread
);
378 * Cancel priority depression, wait for concurrent expirations
379 * on other processors.
381 if (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) {
382 thread
->sched_flags
&= ~TH_SFLAG_DEPRESSED_MASK
;
384 if (timer_call_cancel(&thread
->depress_timer
))
385 thread
->depress_timer_active
--;
388 while (thread
->depress_timer_active
> 0) {
389 thread_unlock(thread
);
398 thread_sched_call(thread
, NULL
);
400 thread_unlock(thread
);
403 thread_policy_reset(thread
);
407 uthread_cleanup(task
, thread
->uthread
, task
->bsd_info
);
408 threadcnt
= hw_atomic_sub(&task
->active_thread_count
, 1);
411 * If we are the last thread to terminate and the task is
412 * associated with a BSD process, perform BSD process exit.
414 if (threadcnt
== 0 && task
->bsd_info
!= NULL
)
415 proc_exit(task
->bsd_info
);
417 uthread_cred_free(thread
->uthread
);
423 * Cancel wait timer, and wait for
424 * concurrent expirations.
426 if (thread
->wait_timer_is_set
) {
427 thread
->wait_timer_is_set
= FALSE
;
429 if (timer_call_cancel(&thread
->wait_timer
))
430 thread
->wait_timer_active
--;
433 while (thread
->wait_timer_active
> 0) {
434 thread_unlock(thread
);
444 * If there is a reserved stack, release it.
446 if (thread
->reserved_stack
!= 0) {
447 stack_free_reserved(thread
);
448 thread
->reserved_stack
= 0;
452 * Mark thread as terminating, and block.
454 thread
->state
|= TH_TERMINATE
;
455 thread_mark_wait_locked(thread
, THREAD_UNINT
);
456 assert(thread
->promotions
== 0);
457 assert(thread
->rwlock_count
== 0);
458 thread_unlock(thread
);
461 thread_block((thread_continue_t
)thread_terminate_continue
);
471 if (thread
== THREAD_NULL
)
474 if (thread_deallocate_internal(thread
) > 0)
477 if(!(thread
->state
& TH_TERMINATE2
))
478 panic("thread_deallocate: thread not properly terminated\n");
481 kpc_thread_destroy(thread
);
485 ipc_thread_terminate(thread
);
491 void *ut
= thread
->uthread
;
493 thread
->uthread
= NULL
;
494 uthread_zone_free(ut
);
496 #endif /* MACH_BSD */
498 if (thread
->t_ledger
)
499 ledger_dereference(thread
->t_ledger
);
500 if (thread
->t_threadledger
)
501 ledger_dereference(thread
->t_threadledger
);
503 if (thread
->kernel_stack
!= 0)
506 lck_mtx_destroy(&thread
->mutex
, &thread_lck_grp
);
507 machine_thread_destroy(thread
);
509 task_deallocate(task
);
511 zfree(thread_zone
, thread
);
515 * thread_terminate_daemon:
517 * Perform final clean up for terminating threads.
520 thread_terminate_daemon(void)
522 thread_t self
, thread
;
525 self
= current_thread();
526 self
->options
|= TH_OPT_SYSTEM_CRITICAL
;
529 simple_lock(&thread_terminate_lock
);
531 while ((thread
= (thread_t
)dequeue_head(&thread_terminate_queue
)) != THREAD_NULL
) {
532 simple_unlock(&thread_terminate_lock
);
538 task
->total_user_time
+= timer_grab(&thread
->user_timer
);
539 if (thread
->precise_user_kernel_time
) {
540 task
->total_system_time
+= timer_grab(&thread
->system_timer
);
542 task
->total_user_time
+= timer_grab(&thread
->system_timer
);
545 task
->c_switch
+= thread
->c_switch
;
546 task
->p_switch
+= thread
->p_switch
;
547 task
->ps_switch
+= thread
->ps_switch
;
549 task
->syscalls_unix
+= thread
->syscalls_unix
;
550 task
->syscalls_mach
+= thread
->syscalls_mach
;
552 task
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
553 task
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
554 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
555 task
->thread_count
--;
558 * If the task is being halted, and there is only one thread
559 * left in the task after this one, then wakeup that thread.
561 if (task
->thread_count
== 1 && task
->halting
)
562 thread_wakeup((event_t
)&task
->halting
);
566 lck_mtx_lock(&tasks_threads_lock
);
567 queue_remove(&threads
, thread
, thread_t
, threads
);
569 lck_mtx_unlock(&tasks_threads_lock
);
571 thread_deallocate(thread
);
574 simple_lock(&thread_terminate_lock
);
577 assert_wait((event_t
)&thread_terminate_queue
, THREAD_UNINT
);
578 simple_unlock(&thread_terminate_lock
);
581 self
->options
&= ~TH_OPT_SYSTEM_CRITICAL
;
582 thread_block((thread_continue_t
)thread_terminate_daemon
);
587 * thread_terminate_enqueue:
589 * Enqueue a terminating thread for final disposition.
591 * Called at splsched.
594 thread_terminate_enqueue(
597 simple_lock(&thread_terminate_lock
);
598 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)thread
);
599 simple_unlock(&thread_terminate_lock
);
601 thread_wakeup((event_t
)&thread_terminate_queue
);
605 * thread_stack_daemon:
607 * Perform stack allocation as required due to
611 thread_stack_daemon(void)
617 simple_lock(&thread_stack_lock
);
619 while ((thread
= (thread_t
)dequeue_head(&thread_stack_queue
)) != THREAD_NULL
) {
620 simple_unlock(&thread_stack_lock
);
623 /* allocate stack with interrupts enabled so that we can call into VM */
628 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
629 thread_unlock(thread
);
631 simple_lock(&thread_stack_lock
);
634 assert_wait((event_t
)&thread_stack_queue
, THREAD_UNINT
);
635 simple_unlock(&thread_stack_lock
);
638 thread_block((thread_continue_t
)thread_stack_daemon
);
643 * thread_stack_enqueue:
645 * Enqueue a thread for stack allocation.
647 * Called at splsched.
650 thread_stack_enqueue(
653 simple_lock(&thread_stack_lock
);
654 enqueue_tail(&thread_stack_queue
, (queue_entry_t
)thread
);
655 simple_unlock(&thread_stack_lock
);
657 thread_wakeup((event_t
)&thread_stack_queue
);
661 thread_daemon_init(void)
663 kern_return_t result
;
664 thread_t thread
= NULL
;
666 simple_lock_init(&thread_terminate_lock
, 0);
667 queue_init(&thread_terminate_queue
);
669 result
= kernel_thread_start_priority((thread_continue_t
)thread_terminate_daemon
, NULL
, MINPRI_KERNEL
, &thread
);
670 if (result
!= KERN_SUCCESS
)
671 panic("thread_daemon_init: thread_terminate_daemon");
673 thread_deallocate(thread
);
675 simple_lock_init(&thread_stack_lock
, 0);
676 queue_init(&thread_stack_queue
);
678 result
= kernel_thread_start_priority((thread_continue_t
)thread_stack_daemon
, NULL
, BASEPRI_PREEMPT
, &thread
);
679 if (result
!= KERN_SUCCESS
)
680 panic("thread_daemon_init: thread_stack_daemon");
682 thread_deallocate(thread
);
686 * Create a new thread.
687 * Doesn't start the thread running.
690 thread_create_internal(
693 thread_continue_t continuation
,
695 #define TH_OPTION_NONE 0x00
696 #define TH_OPTION_NOCRED 0x01
697 #define TH_OPTION_NOSUSP 0x02
698 thread_t
*out_thread
)
701 static thread_t first_thread
;
704 * Allocate a thread and initialize static fields
706 if (first_thread
== THREAD_NULL
)
707 new_thread
= first_thread
= current_thread();
709 new_thread
= (thread_t
)zalloc(thread_zone
);
710 if (new_thread
== THREAD_NULL
)
711 return (KERN_RESOURCE_SHORTAGE
);
713 if (new_thread
!= first_thread
)
714 *new_thread
= thread_template
;
717 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
, (options
& TH_OPTION_NOCRED
) != 0);
718 if (new_thread
->uthread
== NULL
) {
719 zfree(thread_zone
, new_thread
);
720 return (KERN_RESOURCE_SHORTAGE
);
722 #endif /* MACH_BSD */
724 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
726 void *ut
= new_thread
->uthread
;
728 new_thread
->uthread
= NULL
;
729 /* cred free may not be necessary */
730 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
731 uthread_cred_free(ut
);
732 uthread_zone_free(ut
);
733 #endif /* MACH_BSD */
735 zfree(thread_zone
, new_thread
);
736 return (KERN_FAILURE
);
739 new_thread
->task
= parent_task
;
741 thread_lock_init(new_thread
);
742 wake_lock_init(new_thread
);
744 lck_mtx_init(&new_thread
->mutex
, &thread_lck_grp
, &thread_lck_attr
);
746 ipc_thread_init(new_thread
);
748 new_thread
->continuation
= continuation
;
750 lck_mtx_lock(&tasks_threads_lock
);
751 task_lock(parent_task
);
753 if ( !parent_task
->active
|| parent_task
->halting
||
754 ((options
& TH_OPTION_NOSUSP
) != 0 &&
755 parent_task
->suspend_count
> 0) ||
756 (parent_task
->thread_count
>= task_threadmax
&&
757 parent_task
!= kernel_task
) ) {
758 task_unlock(parent_task
);
759 lck_mtx_unlock(&tasks_threads_lock
);
763 void *ut
= new_thread
->uthread
;
765 new_thread
->uthread
= NULL
;
766 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
767 /* cred free may not be necessary */
768 uthread_cred_free(ut
);
769 uthread_zone_free(ut
);
771 #endif /* MACH_BSD */
772 ipc_thread_disable(new_thread
);
773 ipc_thread_terminate(new_thread
);
774 lck_mtx_destroy(&new_thread
->mutex
, &thread_lck_grp
);
775 machine_thread_destroy(new_thread
);
776 zfree(thread_zone
, new_thread
);
777 return (KERN_FAILURE
);
780 /* New threads inherit any default state on the task */
781 machine_thread_inherit_taskwide(new_thread
, parent_task
);
783 task_reference_internal(parent_task
);
785 if (new_thread
->task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
787 * This task has a per-thread CPU limit; make sure this new thread
788 * gets its limit set too, before it gets out of the kernel.
790 set_astledger(new_thread
);
792 new_thread
->t_threadledger
= LEDGER_NULL
; /* per thread ledger is not inherited */
793 new_thread
->t_ledger
= new_thread
->task
->ledger
;
794 if (new_thread
->t_ledger
)
795 ledger_reference(new_thread
->t_ledger
);
797 /* Cache the task's map */
798 new_thread
->map
= parent_task
->map
;
800 /* Chain the thread onto the task's list */
801 queue_enter(&parent_task
->threads
, new_thread
, thread_t
, task_threads
);
802 parent_task
->thread_count
++;
804 /* So terminating threads don't need to take the task lock to decrement */
805 hw_atomic_add(&parent_task
->active_thread_count
, 1);
807 /* Protected by the tasks_threads_lock */
808 new_thread
->thread_id
= ++thread_unique_id
;
810 queue_enter(&threads
, new_thread
, thread_t
, threads
);
813 timer_call_setup(&new_thread
->wait_timer
, thread_timer_expire
, new_thread
);
814 timer_call_setup(&new_thread
->depress_timer
, thread_depress_expire
, new_thread
);
818 * If parent task has any reservations, they need to be propagated to this
821 new_thread
->t_chud
= (TASK_PMC_FLAG
== (parent_task
->t_chud
& TASK_PMC_FLAG
)) ?
822 THREAD_PMC_FLAG
: 0U;
825 kpc_thread_create(new_thread
);
828 /* Only need to update policies pushed from task to thread */
829 new_thread
->requested_policy
.bg_iotier
= parent_task
->effective_policy
.bg_iotier
;
830 new_thread
->requested_policy
.terminated
= parent_task
->effective_policy
.terminated
;
832 /* Set the thread's scheduling parameters */
833 new_thread
->sched_mode
= SCHED(initial_thread_sched_mode
)(parent_task
);
834 new_thread
->sched_flags
= 0;
835 new_thread
->max_priority
= parent_task
->max_priority
;
836 new_thread
->task_priority
= parent_task
->priority
;
837 new_thread
->priority
= (priority
< 0)? parent_task
->priority
: priority
;
838 if (new_thread
->priority
> new_thread
->max_priority
)
839 new_thread
->priority
= new_thread
->max_priority
;
840 new_thread
->importance
= new_thread
->priority
- new_thread
->task_priority
;
841 new_thread
->saved_importance
= new_thread
->importance
;
843 #if defined(CONFIG_SCHED_TRADITIONAL)
844 new_thread
->sched_stamp
= sched_tick
;
845 new_thread
->pri_shift
= sched_pri_shift
;
847 SCHED(compute_priority
)(new_thread
, FALSE
);
849 new_thread
->active
= TRUE
;
851 *out_thread
= new_thread
;
854 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
856 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
858 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
859 TRACEDBG_CODE(DBG_TRACE_DATA
, 1) | DBG_FUNC_NONE
,
860 (vm_address_t
)(uintptr_t)thread_tid(new_thread
), dbg_arg2
, 0, 0, 0);
862 kdbg_trace_string(parent_task
->bsd_info
,
863 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
865 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
866 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
867 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
870 DTRACE_PROC1(lwp__create
, thread_t
, *out_thread
);
872 return (KERN_SUCCESS
);
876 thread_create_internal2(
878 thread_t
*new_thread
,
881 kern_return_t result
;
884 if (task
== TASK_NULL
|| task
== kernel_task
)
885 return (KERN_INVALID_ARGUMENT
);
887 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
888 if (result
!= KERN_SUCCESS
)
891 thread
->user_stop_count
= 1;
893 if (task
->suspend_count
> 0)
897 extmod_statistics_incr_thread_create(task
);
900 lck_mtx_unlock(&tasks_threads_lock
);
902 *new_thread
= thread
;
904 return (KERN_SUCCESS
);
907 /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
911 thread_t
*new_thread
);
916 thread_t
*new_thread
)
918 return thread_create_internal2(task
, new_thread
, FALSE
);
922 thread_create_from_user(
924 thread_t
*new_thread
)
926 return thread_create_internal2(task
, new_thread
, TRUE
);
930 thread_create_running_internal2(
931 register task_t task
,
933 thread_state_t new_state
,
934 mach_msg_type_number_t new_state_count
,
935 thread_t
*new_thread
,
938 register kern_return_t result
;
941 if (task
== TASK_NULL
|| task
== kernel_task
)
942 return (KERN_INVALID_ARGUMENT
);
944 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
945 if (result
!= KERN_SUCCESS
)
948 result
= machine_thread_set_state(
949 thread
, flavor
, new_state
, new_state_count
);
950 if (result
!= KERN_SUCCESS
) {
952 lck_mtx_unlock(&tasks_threads_lock
);
954 thread_terminate(thread
);
955 thread_deallocate(thread
);
959 thread_mtx_lock(thread
);
960 thread_start_internal(thread
);
961 thread_mtx_unlock(thread
);
964 extmod_statistics_incr_thread_create(task
);
967 lck_mtx_unlock(&tasks_threads_lock
);
969 *new_thread
= thread
;
974 /* Prototype, see justification above */
976 thread_create_running(
977 register task_t task
,
979 thread_state_t new_state
,
980 mach_msg_type_number_t new_state_count
,
981 thread_t
*new_thread
);
984 thread_create_running(
985 register task_t task
,
987 thread_state_t new_state
,
988 mach_msg_type_number_t new_state_count
,
989 thread_t
*new_thread
)
991 return thread_create_running_internal2(
992 task
, flavor
, new_state
, new_state_count
,
997 thread_create_running_from_user(
998 register task_t task
,
1000 thread_state_t new_state
,
1001 mach_msg_type_number_t new_state_count
,
1002 thread_t
*new_thread
)
1004 return thread_create_running_internal2(
1005 task
, flavor
, new_state
, new_state_count
,
1010 thread_create_workq(
1012 thread_continue_t thread_return
,
1013 thread_t
*new_thread
)
1015 kern_return_t result
;
1018 if (task
== TASK_NULL
|| task
== kernel_task
)
1019 return (KERN_INVALID_ARGUMENT
);
1021 result
= thread_create_internal(task
, -1, thread_return
, TH_OPTION_NOCRED
| TH_OPTION_NOSUSP
, &thread
);
1022 if (result
!= KERN_SUCCESS
)
1025 thread
->user_stop_count
= 1;
1026 thread_hold(thread
);
1027 if (task
->suspend_count
> 0)
1028 thread_hold(thread
);
1031 lck_mtx_unlock(&tasks_threads_lock
);
1033 *new_thread
= thread
;
1035 return (KERN_SUCCESS
);
1039 * kernel_thread_create:
1041 * Create a thread in the kernel task
1042 * to execute in kernel context.
1045 kernel_thread_create(
1046 thread_continue_t continuation
,
1049 thread_t
*new_thread
)
1051 kern_return_t result
;
1053 task_t task
= kernel_task
;
1055 result
= thread_create_internal(task
, priority
, continuation
, TH_OPTION_NONE
, &thread
);
1056 if (result
!= KERN_SUCCESS
)
1060 lck_mtx_unlock(&tasks_threads_lock
);
1062 stack_alloc(thread
);
1063 assert(thread
->kernel_stack
!= 0);
1064 thread
->reserved_stack
= thread
->kernel_stack
;
1066 thread
->parameter
= parameter
;
1069 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread
, continuation
);
1070 *new_thread
= thread
;
1076 kernel_thread_start_priority(
1077 thread_continue_t continuation
,
1080 thread_t
*new_thread
)
1082 kern_return_t result
;
1085 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
1086 if (result
!= KERN_SUCCESS
)
1089 *new_thread
= thread
;
1091 thread_mtx_lock(thread
);
1092 thread_start_internal(thread
);
1093 thread_mtx_unlock(thread
);
1099 kernel_thread_start(
1100 thread_continue_t continuation
,
1102 thread_t
*new_thread
)
1104 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
1109 thread_info_internal(
1110 register thread_t thread
,
1111 thread_flavor_t flavor
,
1112 thread_info_t thread_info_out
, /* ptr to OUT array */
1113 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
1118 if (thread
== THREAD_NULL
)
1119 return (KERN_INVALID_ARGUMENT
);
1121 if (flavor
== THREAD_BASIC_INFO
) {
1122 register thread_basic_info_t basic_info
;
1124 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
1125 return (KERN_INVALID_ARGUMENT
);
1127 basic_info
= (thread_basic_info_t
) thread_info_out
;
1130 thread_lock(thread
);
1134 thread_read_times(thread
, &basic_info
->user_time
,
1135 &basic_info
->system_time
);
1138 * Update lazy-evaluated scheduler info because someone wants it.
1140 if (SCHED(can_update_priority
)(thread
))
1141 SCHED(update_priority
)(thread
);
1143 basic_info
->sleep_time
= 0;
1146 * To calculate cpu_usage, first correct for timer rate,
1147 * then for 5/8 ageing. The correction factor [3/5] is
1150 basic_info
->cpu_usage
= 0;
1151 #if defined(CONFIG_SCHED_TRADITIONAL)
1152 if (sched_tick_interval
) {
1153 basic_info
->cpu_usage
= (integer_t
)(((uint64_t)thread
->cpu_usage
1154 * TH_USAGE_SCALE
) / sched_tick_interval
);
1155 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
1159 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
)
1160 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
1162 basic_info
->policy
= ((thread
->sched_mode
== TH_MODE_TIMESHARE
)?
1163 POLICY_TIMESHARE
: POLICY_RR
);
1166 if (thread
->options
& TH_OPT_IDLE_THREAD
)
1167 flags
|= TH_FLAGS_IDLE
;
1169 if (!thread
->kernel_stack
)
1170 flags
|= TH_FLAGS_SWAPPED
;
1173 if (thread
->state
& TH_TERMINATE
)
1174 state
= TH_STATE_HALTED
;
1176 if (thread
->state
& TH_RUN
)
1177 state
= TH_STATE_RUNNING
;
1179 if (thread
->state
& TH_UNINT
)
1180 state
= TH_STATE_UNINTERRUPTIBLE
;
1182 if (thread
->state
& TH_SUSP
)
1183 state
= TH_STATE_STOPPED
;
1185 if (thread
->state
& TH_WAIT
)
1186 state
= TH_STATE_WAITING
;
1188 basic_info
->run_state
= state
;
1189 basic_info
->flags
= flags
;
1191 basic_info
->suspend_count
= thread
->user_stop_count
;
1193 thread_unlock(thread
);
1196 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
1198 return (KERN_SUCCESS
);
1201 if (flavor
== THREAD_IDENTIFIER_INFO
) {
1202 register thread_identifier_info_t identifier_info
;
1204 if (*thread_info_count
< THREAD_IDENTIFIER_INFO_COUNT
)
1205 return (KERN_INVALID_ARGUMENT
);
1207 identifier_info
= (thread_identifier_info_t
) thread_info_out
;
1210 thread_lock(thread
);
1212 identifier_info
->thread_id
= thread
->thread_id
;
1213 identifier_info
->thread_handle
= thread
->machine
.cthread_self
;
1214 if(thread
->task
->bsd_info
) {
1215 identifier_info
->dispatch_qaddr
= identifier_info
->thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1217 thread_unlock(thread
);
1219 return KERN_INVALID_ARGUMENT
;
1222 thread_unlock(thread
);
1224 return KERN_SUCCESS
;
1227 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
1228 policy_timeshare_info_t ts_info
;
1230 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
1231 return (KERN_INVALID_ARGUMENT
);
1233 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
1236 thread_lock(thread
);
1238 if (thread
->sched_mode
!= TH_MODE_TIMESHARE
) {
1239 thread_unlock(thread
);
1242 return (KERN_INVALID_POLICY
);
1245 ts_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1246 if (ts_info
->depressed
) {
1247 ts_info
->base_priority
= DEPRESSPRI
;
1248 ts_info
->depress_priority
= thread
->priority
;
1251 ts_info
->base_priority
= thread
->priority
;
1252 ts_info
->depress_priority
= -1;
1255 ts_info
->cur_priority
= thread
->sched_pri
;
1256 ts_info
->max_priority
= thread
->max_priority
;
1258 thread_unlock(thread
);
1261 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
1263 return (KERN_SUCCESS
);
1266 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
1267 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
1268 return (KERN_INVALID_ARGUMENT
);
1270 return (KERN_INVALID_POLICY
);
1273 if (flavor
== THREAD_SCHED_RR_INFO
) {
1274 policy_rr_info_t rr_info
;
1275 uint32_t quantum_time
;
1276 uint64_t quantum_ns
;
1278 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
1279 return (KERN_INVALID_ARGUMENT
);
1281 rr_info
= (policy_rr_info_t
) thread_info_out
;
1284 thread_lock(thread
);
1286 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
1287 thread_unlock(thread
);
1290 return (KERN_INVALID_POLICY
);
1293 rr_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1294 if (rr_info
->depressed
) {
1295 rr_info
->base_priority
= DEPRESSPRI
;
1296 rr_info
->depress_priority
= thread
->priority
;
1299 rr_info
->base_priority
= thread
->priority
;
1300 rr_info
->depress_priority
= -1;
1303 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
1304 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
1306 rr_info
->max_priority
= thread
->max_priority
;
1307 rr_info
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
1309 thread_unlock(thread
);
1312 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1314 return (KERN_SUCCESS
);
1317 return (KERN_INVALID_ARGUMENT
);
1323 time_value_t
*user_time
,
1324 time_value_t
*system_time
)
1328 uint64_t tval_user
, tval_system
;
1330 tval_user
= timer_grab(&thread
->user_timer
);
1331 tval_system
= timer_grab(&thread
->system_timer
);
1333 if (thread
->precise_user_kernel_time
) {
1334 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1335 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1336 user_time
->microseconds
= usecs
;
1338 absolutetime_to_microtime(tval_system
, &secs
, &usecs
);
1339 system_time
->seconds
= (typeof(system_time
->seconds
))secs
;
1340 system_time
->microseconds
= usecs
;
1342 /* system_timer may represent either sys or user */
1343 tval_user
+= tval_system
;
1344 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1345 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1346 user_time
->microseconds
= usecs
;
1348 system_time
->seconds
= 0;
1349 system_time
->microseconds
= 0;
1355 __unused thread_t thread
,
1356 __unused processor_set_t new_pset
)
1358 return (KERN_FAILURE
);
1362 * thread_assign_default:
1364 * Special version of thread_assign for assigning threads to default
1368 thread_assign_default(
1371 return (thread_assign(thread
, &pset0
));
1375 * thread_get_assignment
1377 * Return current assignment for this thread.
1380 thread_get_assignment(
1382 processor_set_t
*pset
)
1385 return (KERN_INVALID_ARGUMENT
);
1389 return (KERN_SUCCESS
);
1393 * thread_wire_internal:
1395 * Specify that the target thread must always be able
1396 * to run and to allocate memory.
1399 thread_wire_internal(
1400 host_priv_t host_priv
,
1403 boolean_t
*prev_state
)
1405 if (host_priv
== NULL
|| thread
!= current_thread())
1406 return (KERN_INVALID_ARGUMENT
);
1408 assert(host_priv
== &realhost
);
1411 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
1414 if (!(thread
->options
& TH_OPT_VMPRIV
))
1415 vm_page_free_reserve(1); /* XXX */
1416 thread
->options
|= TH_OPT_VMPRIV
;
1419 if (thread
->options
& TH_OPT_VMPRIV
)
1420 vm_page_free_reserve(-1); /* XXX */
1421 thread
->options
&= ~TH_OPT_VMPRIV
;
1424 return (KERN_SUCCESS
);
1431 * User-api wrapper for thread_wire_internal()
1435 host_priv_t host_priv
,
1439 return (thread_wire_internal(host_priv
, thread
, wired
, NULL
));
1444 * XXX assuming current thread only, for now...
1447 thread_guard_violation(thread_t thread
, unsigned type
)
1449 assert(thread
== current_thread());
1451 spl_t s
= splsched();
1453 * Use the saved state area of the thread structure
1454 * to store all info required to handle the AST when
1455 * returning to userspace
1457 thread
->guard_exc_info
.type
= type
;
1458 thread_ast_set(thread
, AST_GUARD
);
1459 ast_propagate(thread
->ast
);
1467 * Handle AST_GUARD for a thread. This routine looks at the
1468 * state saved in the thread structure to determine the cause
1469 * of this exception. Based on this value, it invokes the
1470 * appropriate routine which determines other exception related
1471 * info and raises the exception.
1474 guard_ast(thread_t thread
)
1476 if (thread
->guard_exc_info
.type
== GUARD_TYPE_MACH_PORT
)
1477 mach_port_guard_ast(thread
);
1479 fd_guard_ast(thread
);
1483 thread_cputime_callback(int warning
, __unused
const void *arg0
, __unused
const void *arg1
)
1485 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
1486 #if CONFIG_TELEMETRY
1488 * This thread is in danger of violating the CPU usage monitor. Enable telemetry
1489 * on the entire task so there are micro-stackshots available if and when
1490 * EXC_RESOURCE is triggered. We could have chosen to enable micro-stackshots
1491 * for this thread only; but now that this task is suspect, knowing what all of
1492 * its threads are up to will be useful.
1494 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 1);
1499 #if CONFIG_TELEMETRY
1501 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
1502 * exceeded the limit, turn telemetry off for the task.
1504 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 0);
1508 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE();
1512 void __attribute__((noinline
))
1513 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void)
1516 task_t task
= current_task();
1517 thread_t thread
= current_thread();
1518 uint64_t tid
= thread
->thread_id
;
1519 char *procname
= (char *) "unknown";
1520 time_value_t thread_total_time
= {0, 0};
1521 time_value_t thread_system_time
;
1522 time_value_t thread_user_time
;
1525 uint32_t limit_percent
;
1526 uint32_t usage_percent
;
1527 uint32_t interval_sec
;
1528 uint64_t interval_ns
;
1529 uint64_t balance_ns
;
1530 boolean_t fatal
= FALSE
;
1532 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1533 struct ledger_entry_info lei
;
1535 assert(thread
->t_threadledger
!= LEDGER_NULL
);
1538 * Now that a thread has tripped the monitor, disable it for the entire task.
1541 if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) {
1543 * The CPU usage monitor has been disabled on our task, so some other
1544 * thread must have gotten here first. We only send one exception per
1545 * task lifetime, so there's nothing left for us to do here.
1550 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_CPUMON
) {
1553 task_disable_cpumon(task
);
1557 pid
= proc_selfpid();
1558 if (task
->bsd_info
!= NULL
)
1559 procname
= proc_name_address(task
->bsd_info
);
1562 thread_get_cpulimit(&action
, &percentage
, &interval_ns
);
1564 interval_sec
= (uint32_t)(interval_ns
/ NSEC_PER_SEC
);
1566 thread_read_times(thread
, &thread_user_time
, &thread_system_time
);
1567 time_value_add(&thread_total_time
, &thread_user_time
);
1568 time_value_add(&thread_total_time
, &thread_system_time
);
1570 ledger_get_entry_info(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &lei
);
1572 absolutetime_to_nanoseconds(lei
.lei_balance
, &balance_ns
);
1573 usage_percent
= (uint32_t) ((balance_ns
* 100ULL) / lei
.lei_last_refill
);
1575 /* Show refill period in the same units as balance, limit, etc */
1576 nanoseconds_to_absolutetime(lei
.lei_refill_period
, &lei
.lei_refill_period
);
1578 limit_percent
= (uint32_t) ((lei
.lei_limit
* 100ULL) / lei
.lei_refill_period
);
1580 /* TODO: show task total runtime as well? see TASK_ABSOLUTETIME_INFO */
1582 if (disable_exc_resource
) {
1583 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1584 "supressed by a boot-arg\n", procname
, pid
, tid
);
1589 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1590 "supressed due to audio playback\n", procname
, pid
, tid
);
1593 printf("process %s[%d] thread %llu caught burning CPU! "
1594 "It used more than %d%% CPU (Actual recent usage: %d%%) over %d seconds. "
1595 "thread lifetime cpu usage %d.%06d seconds, (%d.%06d user, %d.%06d system) "
1596 "ledger info: balance: %lld credit: %lld debit: %lld limit: %llu (%d%%) "
1597 "period: %llu time since last refill (ns): %llu \n",
1599 percentage
, usage_percent
, interval_sec
,
1600 thread_total_time
.seconds
, thread_total_time
.microseconds
,
1601 thread_user_time
.seconds
, thread_user_time
.microseconds
,
1602 thread_system_time
.seconds
, thread_system_time
.microseconds
,
1604 lei
.lei_credit
, lei
.lei_debit
,
1605 lei
.lei_limit
, limit_percent
,
1606 lei
.lei_refill_period
, lei
.lei_last_refill
);
1609 code
[0] = code
[1] = 0;
1610 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_CPU
);
1611 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_CPU_MONITOR
);
1612 EXC_RESOURCE_CPUMONITOR_ENCODE_INTERVAL(code
[0], interval_sec
);
1613 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[0], limit_percent
);
1614 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[1], usage_percent
);
1615 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
1618 task_terminate_internal(task
);
1623 init_thread_ledgers(void) {
1624 ledger_template_t t
;
1627 assert(thread_ledger_template
== NULL
);
1629 if ((t
= ledger_template_create("Per-thread ledger")) == NULL
)
1630 panic("couldn't create thread ledger template");
1632 if ((idx
= ledger_entry_add(t
, "cpu_time", "sched", "ns")) < 0) {
1633 panic("couldn't create cpu_time entry for thread ledger template");
1636 if (ledger_set_callback(t
, idx
, thread_cputime_callback
, NULL
, NULL
) < 0) {
1637 panic("couldn't set thread ledger callback for cpu_time entry");
1640 thread_ledgers
.cpu_time
= idx
;
1641 thread_ledger_template
= t
;
1645 * Returns currently applied CPU usage limit, or 0/0 if none is applied.
1648 thread_get_cpulimit(int *action
, uint8_t *percentage
, uint64_t *interval_ns
)
1650 int64_t abstime
= 0;
1651 uint64_t limittime
= 0;
1652 thread_t thread
= current_thread();
1658 if (thread
->t_threadledger
== LEDGER_NULL
) {
1660 * This thread has no per-thread ledger, so it can't possibly
1661 * have a CPU limit applied.
1663 return (KERN_SUCCESS
);
1666 ledger_get_period(thread
->t_threadledger
, thread_ledgers
.cpu_time
, interval_ns
);
1667 ledger_get_limit(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &abstime
);
1669 if ((abstime
== LEDGER_LIMIT_INFINITY
) || (*interval_ns
== 0)) {
1671 * This thread's CPU time ledger has no period or limit; so it
1672 * doesn't have a CPU limit applied.
1674 return (KERN_SUCCESS
);
1678 * This calculation is the converse to the one in thread_set_cpulimit().
1680 absolutetime_to_nanoseconds(abstime
, &limittime
);
1681 *percentage
= (limittime
* 100ULL) / *interval_ns
;
1682 assert(*percentage
<= 100);
1684 if (thread
->options
& TH_OPT_PROC_CPULIMIT
) {
1685 assert((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0);
1687 *action
= THREAD_CPULIMIT_BLOCK
;
1688 } else if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
1689 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0);
1691 *action
= THREAD_CPULIMIT_EXCEPTION
;
1693 *action
= THREAD_CPULIMIT_DISABLE
;
1696 return (KERN_SUCCESS
);
1700 * Set CPU usage limit on a thread.
1702 * Calling with percentage of 0 will unset the limit for this thread.
1705 thread_set_cpulimit(int action
, uint8_t percentage
, uint64_t interval_ns
)
1707 thread_t thread
= current_thread();
1709 uint64_t limittime
= 0;
1710 uint64_t abstime
= 0;
1712 assert(percentage
<= 100);
1714 if (action
== THREAD_CPULIMIT_DISABLE
) {
1716 * Remove CPU limit, if any exists.
1718 if (thread
->t_threadledger
!= LEDGER_NULL
) {
1719 l
= thread
->t_threadledger
;
1721 * The only way to get a per-thread ledger is via CPU limits.
1723 assert(thread
->options
& (TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
));
1724 thread
->t_threadledger
= NULL
;
1725 ledger_dereference(l
);
1726 thread
->options
&= ~(TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
);
1732 if (interval_ns
< MINIMUM_CPULIMIT_INTERVAL_MS
* NSEC_PER_MSEC
) {
1733 return (KERN_INVALID_ARGUMENT
);
1736 l
= thread
->t_threadledger
;
1737 if (l
== LEDGER_NULL
) {
1739 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
1741 if ((l
= ledger_instantiate(thread_ledger_template
, LEDGER_CREATE_INACTIVE_ENTRIES
)) == LEDGER_NULL
)
1742 return (KERN_RESOURCE_SHORTAGE
);
1745 * We are the first to create this thread's ledger, so only activate our entry.
1747 ledger_entry_setactive(l
, thread_ledgers
.cpu_time
);
1748 thread
->t_threadledger
= l
;
1752 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
1753 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
1755 limittime
= (interval_ns
* percentage
) / 100;
1756 nanoseconds_to_absolutetime(limittime
, &abstime
);
1757 ledger_set_limit(l
, thread_ledgers
.cpu_time
, abstime
, cpumon_ustackshots_trigger_pct
);
1759 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
1761 ledger_set_period(l
, thread_ledgers
.cpu_time
, interval_ns
);
1763 if (action
== THREAD_CPULIMIT_EXCEPTION
) {
1765 * We don't support programming the CPU usage monitor on a task if any of its
1766 * threads have a per-thread blocking CPU limit configured.
1768 if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
1769 panic("CPU usage monitor activated, but blocking thread limit exists");
1773 * Make a note that this thread's CPU limit is being used for the task-wide CPU
1774 * usage monitor. We don't have to arm the callback which will trigger the
1775 * exception, because that was done for us in ledger_instantiate (because the
1776 * ledger template used has a default callback).
1778 thread
->options
|= TH_OPT_PROC_CPULIMIT
;
1781 * We deliberately override any CPU limit imposed by a task-wide limit (eg
1782 * CPU usage monitor).
1784 thread
->options
&= ~TH_OPT_PROC_CPULIMIT
;
1786 thread
->options
|= TH_OPT_PRVT_CPULIMIT
;
1787 /* The per-thread ledger template by default has a callback for CPU time */
1788 ledger_disable_callback(l
, thread_ledgers
.cpu_time
);
1789 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_BLOCK
);
1795 int split_funnel_off
= 0;
1796 lck_grp_t
*funnel_lck_grp
= LCK_GRP_NULL
;
1797 lck_grp_attr_t
*funnel_lck_grp_attr
;
1798 lck_attr_t
*funnel_lck_attr
;
1807 if (funnel_lck_grp
== LCK_GRP_NULL
) {
1808 funnel_lck_grp_attr
= lck_grp_attr_alloc_init();
1810 funnel_lck_grp
= lck_grp_alloc_init("Funnel", funnel_lck_grp_attr
);
1812 funnel_lck_attr
= lck_attr_alloc_init();
1814 if ((fnl
= (funnel_t
*)kalloc(sizeof(funnel_t
))) != 0){
1815 bzero((void *)fnl
, sizeof(funnel_t
));
1816 if ((m
= lck_mtx_alloc_init(funnel_lck_grp
, funnel_lck_attr
)) == (lck_mtx_t
*)NULL
) {
1817 kfree(fnl
, sizeof(funnel_t
));
1818 return(THR_FUNNEL_NULL
);
1821 fnl
->fnl_type
= type
;
1830 lck_mtx_free(fnl
->fnl_mutex
, funnel_lck_grp
);
1831 if (fnl
->fnl_oldmutex
)
1832 lck_mtx_free(fnl
->fnl_oldmutex
, funnel_lck_grp
);
1833 kfree(fnl
, sizeof(funnel_t
));
1840 lck_mtx_lock(fnl
->fnl_mutex
);
1841 fnl
->fnl_mtxholder
= current_thread();
1848 lck_mtx_unlock(fnl
->fnl_mutex
);
1849 fnl
->fnl_mtxholder
= NULL
;
1850 fnl
->fnl_mtxrelease
= current_thread();
1857 thread_t th
= current_thread();
1859 if (th
->funnel_state
& TH_FN_OWNED
) {
1860 return(th
->funnel_lock
);
1862 return(THR_FUNNEL_NULL
);
1870 thread_t cur_thread
;
1871 boolean_t funnel_state_prev
;
1874 cur_thread
= current_thread();
1875 funnel_state_prev
= ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
);
1877 if (funnel_state_prev
!= funneled
) {
1878 intr
= ml_set_interrupts_enabled(FALSE
);
1880 if (funneled
== TRUE
) {
1881 if (cur_thread
->funnel_lock
)
1882 panic("Funnel lock called when holding one %p", cur_thread
->funnel_lock
);
1883 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
,
1886 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
,
1888 cur_thread
->funnel_state
|= TH_FN_OWNED
;
1889 cur_thread
->funnel_lock
= fnl
;
1891 if(cur_thread
->funnel_lock
->fnl_mutex
!= fnl
->fnl_mutex
)
1892 panic("Funnel unlock when not holding funnel");
1893 cur_thread
->funnel_state
&= ~TH_FN_OWNED
;
1894 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
,
1897 cur_thread
->funnel_lock
= THR_FUNNEL_NULL
;
1900 (void)ml_set_interrupts_enabled(intr
);
1902 /* if we are trying to acquire funnel recursively
1903 * check for funnel to be held already
1905 if (funneled
&& (fnl
->fnl_mutex
!= cur_thread
->funnel_lock
->fnl_mutex
)) {
1906 panic("thread_funnel_set: already holding a different funnel");
1909 return(funnel_state_prev
);
1915 __unused thread_t thread
)
1925 thread
->sched_call
= (call
!= NULL
)? call
: sched_call_null
;
1929 thread_static_param(
1933 thread_mtx_lock(thread
);
1934 thread
->static_param
= state
;
1935 thread_mtx_unlock(thread
);
1942 return (thread
!= THREAD_NULL
? thread
->thread_id
: 0);
1945 uint16_t thread_set_tag(thread_t th
, uint16_t tag
) {
1946 return thread_set_tag_internal(th
, tag
);
1948 uint16_t thread_get_tag(thread_t th
) {
1949 return thread_get_tag_internal(th
);
1953 thread_dispatchqaddr(
1956 uint64_t dispatchqueue_addr
= 0;
1957 uint64_t thread_handle
= 0;
1959 if (thread
!= THREAD_NULL
) {
1960 thread_handle
= thread
->machine
.cthread_self
;
1962 if (thread
->task
->bsd_info
)
1963 dispatchqueue_addr
= thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1966 return (dispatchqueue_addr
);
1970 * Export routines to other components for things that are done as macros
1971 * within the osfmk component.
1974 #undef thread_reference
1975 void thread_reference(thread_t thread
);
1980 if (thread
!= THREAD_NULL
)
1981 thread_reference_internal(thread
);
1984 #undef thread_should_halt
1990 return (thread_should_halt_fast(th
));
1994 uint32_t dtrace_get_thread_predcache(thread_t thread
)
1996 if (thread
!= THREAD_NULL
)
1997 return thread
->t_dtrace_predcache
;
2002 int64_t dtrace_get_thread_vtime(thread_t thread
)
2004 if (thread
!= THREAD_NULL
)
2005 return thread
->t_dtrace_vtime
;
2010 int64_t dtrace_get_thread_tracing(thread_t thread
)
2012 if (thread
!= THREAD_NULL
)
2013 return thread
->t_dtrace_tracing
;
2018 boolean_t
dtrace_get_thread_reentering(thread_t thread
)
2020 if (thread
!= THREAD_NULL
)
2021 return (thread
->options
& TH_OPT_DTRACE
) ? TRUE
: FALSE
;
2026 vm_offset_t
dtrace_get_kernel_stack(thread_t thread
)
2028 if (thread
!= THREAD_NULL
)
2029 return thread
->kernel_stack
;
2034 int64_t dtrace_calc_thread_recent_vtime(thread_t thread
)
2036 if (thread
!= THREAD_NULL
) {
2037 processor_t processor
= current_processor();
2038 uint64_t abstime
= mach_absolute_time();
2041 timer
= PROCESSOR_DATA(processor
, thread_timer
);
2043 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
)) +
2044 (abstime
- timer
->tstamp
); /* XXX need interrupts off to prevent missed time? */
2049 void dtrace_set_thread_predcache(thread_t thread
, uint32_t predcache
)
2051 if (thread
!= THREAD_NULL
)
2052 thread
->t_dtrace_predcache
= predcache
;
2055 void dtrace_set_thread_vtime(thread_t thread
, int64_t vtime
)
2057 if (thread
!= THREAD_NULL
)
2058 thread
->t_dtrace_vtime
= vtime
;
2061 void dtrace_set_thread_tracing(thread_t thread
, int64_t accum
)
2063 if (thread
!= THREAD_NULL
)
2064 thread
->t_dtrace_tracing
= accum
;
2067 void dtrace_set_thread_reentering(thread_t thread
, boolean_t vbool
)
2069 if (thread
!= THREAD_NULL
) {
2071 thread
->options
|= TH_OPT_DTRACE
;
2073 thread
->options
&= (~TH_OPT_DTRACE
);
2077 vm_offset_t
dtrace_set_thread_recover(thread_t thread
, vm_offset_t recover
)
2079 vm_offset_t prev
= 0;
2081 if (thread
!= THREAD_NULL
) {
2082 prev
= thread
->recover
;
2083 thread
->recover
= recover
;
2088 void dtrace_thread_bootstrap(void)
2090 task_t task
= current_task();
2092 if (task
->thread_count
== 1) {
2093 thread_t thread
= current_thread();
2094 if (thread
->t_dtrace_flags
& TH_DTRACE_EXECSUCCESS
) {
2095 thread
->t_dtrace_flags
&= ~TH_DTRACE_EXECSUCCESS
;
2096 DTRACE_PROC(exec__success
);
2100 DTRACE_PROC(lwp__start
);
2105 dtrace_thread_didexec(thread_t thread
)
2107 thread
->t_dtrace_flags
|= TH_DTRACE_EXECSUCCESS
;
2109 #endif /* CONFIG_DTRACE */