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 debug_task
;
168 int thread_max
= CONFIG_THREAD_MAX
; /* Max number of threads */
169 int task_threadmax
= CONFIG_THREAD_MAX
;
171 static uint64_t thread_unique_id
= 0;
173 struct _thread_ledger_indices thread_ledgers
= { -1 };
174 static ledger_template_t thread_ledger_template
= NULL
;
175 void init_thread_ledgers(void);
176 int task_disable_cpumon(task_t task
);
179 * Level (in terms of percentage of the limit) at which the CPU usage monitor triggers telemetry.
181 * (ie when any thread's CPU consumption exceeds 70% of the limit, start taking user
182 * stacktraces, aka micro-stackshots)
184 #define CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT 70
186 int cpumon_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
187 void __attribute__((noinline
)) THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void);
190 * The smallest interval over which we support limiting CPU consumption is 1ms
192 #define MINIMUM_CPULIMIT_INTERVAL_MS 1
195 thread_bootstrap(void)
198 * Fill in a template thread for fast initialization.
201 thread_template
.runq
= PROCESSOR_NULL
;
203 thread_template
.ref_count
= 2;
205 thread_template
.reason
= AST_NONE
;
206 thread_template
.at_safe_point
= FALSE
;
207 thread_template
.wait_event
= NO_EVENT64
;
208 thread_template
.wait_queue
= WAIT_QUEUE_NULL
;
209 thread_template
.wait_result
= THREAD_WAITING
;
210 thread_template
.options
= THREAD_ABORTSAFE
;
211 thread_template
.state
= TH_WAIT
| TH_UNINT
;
212 thread_template
.wake_active
= FALSE
;
213 thread_template
.continuation
= THREAD_CONTINUE_NULL
;
214 thread_template
.parameter
= NULL
;
216 thread_template
.importance
= 0;
217 thread_template
.sched_mode
= TH_MODE_NONE
;
218 thread_template
.sched_flags
= 0;
219 thread_template
.saved_mode
= TH_MODE_NONE
;
220 thread_template
.safe_release
= 0;
222 thread_template
.priority
= 0;
223 thread_template
.sched_pri
= 0;
224 thread_template
.max_priority
= 0;
225 thread_template
.task_priority
= 0;
226 thread_template
.promotions
= 0;
227 thread_template
.pending_promoter_index
= 0;
228 thread_template
.pending_promoter
[0] =
229 thread_template
.pending_promoter
[1] = NULL
;
230 thread_template
.rwlock_count
= 0;
232 thread_template
.realtime
.deadline
= UINT64_MAX
;
234 thread_template
.current_quantum
= 0;
235 thread_template
.last_run_time
= 0;
236 thread_template
.last_quantum_refill_time
= 0;
238 thread_template
.computation_metered
= 0;
239 thread_template
.computation_epoch
= 0;
241 #if defined(CONFIG_SCHED_TRADITIONAL)
242 thread_template
.sched_stamp
= 0;
243 thread_template
.pri_shift
= INT8_MAX
;
244 thread_template
.sched_usage
= 0;
245 thread_template
.cpu_usage
= thread_template
.cpu_delta
= 0;
247 thread_template
.c_switch
= thread_template
.p_switch
= thread_template
.ps_switch
= 0;
249 thread_template
.bound_processor
= PROCESSOR_NULL
;
250 thread_template
.last_processor
= PROCESSOR_NULL
;
252 thread_template
.sched_call
= sched_call_null
;
254 timer_init(&thread_template
.user_timer
);
255 timer_init(&thread_template
.system_timer
);
256 thread_template
.user_timer_save
= 0;
257 thread_template
.system_timer_save
= 0;
258 thread_template
.vtimer_user_save
= 0;
259 thread_template
.vtimer_prof_save
= 0;
260 thread_template
.vtimer_rlim_save
= 0;
262 thread_template
.wait_timer_is_set
= FALSE
;
263 thread_template
.wait_timer_active
= 0;
265 thread_template
.depress_timer_active
= 0;
267 thread_template
.special_handler
.handler
= special_handler
;
268 thread_template
.special_handler
.next
= NULL
;
270 thread_template
.funnel_lock
= THR_FUNNEL_NULL
;
271 thread_template
.funnel_state
= 0;
272 thread_template
.recover
= (vm_offset_t
)NULL
;
274 thread_template
.map
= VM_MAP_NULL
;
277 thread_template
.t_dtrace_predcache
= 0;
278 thread_template
.t_dtrace_vtime
= 0;
279 thread_template
.t_dtrace_tracing
= 0;
280 #endif /* CONFIG_DTRACE */
283 thread_template
.kpc_buf
= NULL
;
286 thread_template
.t_chud
= 0;
287 thread_template
.t_page_creation_count
= 0;
288 thread_template
.t_page_creation_time
= 0;
290 thread_template
.affinity_set
= NULL
;
292 thread_template
.syscalls_unix
= 0;
293 thread_template
.syscalls_mach
= 0;
295 thread_template
.t_ledger
= LEDGER_NULL
;
296 thread_template
.t_threadledger
= LEDGER_NULL
;
298 thread_template
.requested_policy
= default_task_requested_policy
;
299 thread_template
.effective_policy
= default_task_effective_policy
;
300 thread_template
.pended_policy
= default_task_pended_policy
;
302 thread_template
.iotier_override
= THROTTLE_LEVEL_NONE
;
304 thread_template
.thread_callout_interrupt_wakeups
= thread_template
.thread_callout_platform_idle_wakeups
= 0;
306 thread_template
.thread_timer_wakeups_bin_1
= thread_template
.thread_timer_wakeups_bin_2
= 0;
307 thread_template
.callout_woken_from_icontext
= thread_template
.callout_woken_from_platform_idle
= 0;
309 thread_template
.thread_tag
= 0;
311 init_thread
= thread_template
;
312 machine_set_current_thread(&init_thread
);
319 sizeof(struct thread
),
320 thread_max
* sizeof(struct thread
),
321 THREAD_CHUNK
* sizeof(struct thread
),
324 lck_grp_attr_setdefault(&thread_lck_grp_attr
);
325 lck_grp_init(&thread_lck_grp
, "thread", &thread_lck_grp_attr
);
326 lck_attr_setdefault(&thread_lck_attr
);
331 * Initialize any machine-dependent
332 * per-thread structures necessary.
334 machine_thread_init();
336 if (!PE_parse_boot_argn("cpumon_ustackshots_trigger_pct", &cpumon_ustackshots_trigger_pct
,
337 sizeof (cpumon_ustackshots_trigger_pct
))) {
338 cpumon_ustackshots_trigger_pct
= CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT
;
341 init_thread_ledgers();
345 thread_terminate_continue(void)
347 panic("thread_terminate_continue");
352 * thread_terminate_self:
355 thread_terminate_self(void)
357 thread_t thread
= current_thread();
363 pal_thread_terminate_self(thread
);
365 DTRACE_PROC(lwp__exit
);
367 thread_mtx_lock(thread
);
369 ipc_thread_disable(thread
);
371 thread_mtx_unlock(thread
);
377 * Cancel priority depression, wait for concurrent expirations
378 * on other processors.
380 if (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) {
381 thread
->sched_flags
&= ~TH_SFLAG_DEPRESSED_MASK
;
383 if (timer_call_cancel(&thread
->depress_timer
))
384 thread
->depress_timer_active
--;
387 while (thread
->depress_timer_active
> 0) {
388 thread_unlock(thread
);
397 thread_sched_call(thread
, NULL
);
399 thread_unlock(thread
);
402 thread_policy_reset(thread
);
406 uthread_cleanup(task
, thread
->uthread
, task
->bsd_info
);
407 threadcnt
= hw_atomic_sub(&task
->active_thread_count
, 1);
410 * If we are the last thread to terminate and the task is
411 * associated with a BSD process, perform BSD process exit.
413 if (threadcnt
== 0 && task
->bsd_info
!= NULL
)
414 proc_exit(task
->bsd_info
);
416 uthread_cred_free(thread
->uthread
);
422 * Cancel wait timer, and wait for
423 * concurrent expirations.
425 if (thread
->wait_timer_is_set
) {
426 thread
->wait_timer_is_set
= FALSE
;
428 if (timer_call_cancel(&thread
->wait_timer
))
429 thread
->wait_timer_active
--;
432 while (thread
->wait_timer_active
> 0) {
433 thread_unlock(thread
);
443 * If there is a reserved stack, release it.
445 if (thread
->reserved_stack
!= 0) {
446 stack_free_reserved(thread
);
447 thread
->reserved_stack
= 0;
451 * Mark thread as terminating, and block.
453 thread
->state
|= TH_TERMINATE
;
454 thread_mark_wait_locked(thread
, THREAD_UNINT
);
455 assert(thread
->promotions
== 0);
456 assert(thread
->rwlock_count
== 0);
457 thread_unlock(thread
);
460 thread_block((thread_continue_t
)thread_terminate_continue
);
470 if (thread
== THREAD_NULL
)
473 if (thread_deallocate_internal(thread
) > 0)
476 if(!(thread
->state
& TH_TERMINATE2
))
477 panic("thread_deallocate: thread not properly terminated\n");
480 kpc_thread_destroy(thread
);
484 ipc_thread_terminate(thread
);
490 void *ut
= thread
->uthread
;
492 thread
->uthread
= NULL
;
493 uthread_zone_free(ut
);
495 #endif /* MACH_BSD */
497 if (thread
->t_ledger
)
498 ledger_dereference(thread
->t_ledger
);
499 if (thread
->t_threadledger
)
500 ledger_dereference(thread
->t_threadledger
);
502 if (thread
->kernel_stack
!= 0)
505 lck_mtx_destroy(&thread
->mutex
, &thread_lck_grp
);
506 machine_thread_destroy(thread
);
508 task_deallocate(task
);
510 zfree(thread_zone
, thread
);
514 * thread_terminate_daemon:
516 * Perform final clean up for terminating threads.
519 thread_terminate_daemon(void)
521 thread_t self
, thread
;
524 self
= current_thread();
525 self
->options
|= TH_OPT_SYSTEM_CRITICAL
;
528 simple_lock(&thread_terminate_lock
);
530 while ((thread
= (thread_t
)dequeue_head(&thread_terminate_queue
)) != THREAD_NULL
) {
531 simple_unlock(&thread_terminate_lock
);
537 task
->total_user_time
+= timer_grab(&thread
->user_timer
);
538 if (thread
->precise_user_kernel_time
) {
539 task
->total_system_time
+= timer_grab(&thread
->system_timer
);
541 task
->total_user_time
+= timer_grab(&thread
->system_timer
);
544 task
->c_switch
+= thread
->c_switch
;
545 task
->p_switch
+= thread
->p_switch
;
546 task
->ps_switch
+= thread
->ps_switch
;
548 task
->syscalls_unix
+= thread
->syscalls_unix
;
549 task
->syscalls_mach
+= thread
->syscalls_mach
;
551 task
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
552 task
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
553 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
554 task
->thread_count
--;
557 * If the task is being halted, and there is only one thread
558 * left in the task after this one, then wakeup that thread.
560 if (task
->thread_count
== 1 && task
->halting
)
561 thread_wakeup((event_t
)&task
->halting
);
565 lck_mtx_lock(&tasks_threads_lock
);
566 queue_remove(&threads
, thread
, thread_t
, threads
);
568 lck_mtx_unlock(&tasks_threads_lock
);
570 thread_deallocate(thread
);
573 simple_lock(&thread_terminate_lock
);
576 assert_wait((event_t
)&thread_terminate_queue
, THREAD_UNINT
);
577 simple_unlock(&thread_terminate_lock
);
580 self
->options
&= ~TH_OPT_SYSTEM_CRITICAL
;
581 thread_block((thread_continue_t
)thread_terminate_daemon
);
586 * thread_terminate_enqueue:
588 * Enqueue a terminating thread for final disposition.
590 * Called at splsched.
593 thread_terminate_enqueue(
596 simple_lock(&thread_terminate_lock
);
597 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)thread
);
598 simple_unlock(&thread_terminate_lock
);
600 thread_wakeup((event_t
)&thread_terminate_queue
);
604 * thread_stack_daemon:
606 * Perform stack allocation as required due to
610 thread_stack_daemon(void)
616 simple_lock(&thread_stack_lock
);
618 while ((thread
= (thread_t
)dequeue_head(&thread_stack_queue
)) != THREAD_NULL
) {
619 simple_unlock(&thread_stack_lock
);
622 /* allocate stack with interrupts enabled so that we can call into VM */
627 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
628 thread_unlock(thread
);
630 simple_lock(&thread_stack_lock
);
633 assert_wait((event_t
)&thread_stack_queue
, THREAD_UNINT
);
634 simple_unlock(&thread_stack_lock
);
637 thread_block((thread_continue_t
)thread_stack_daemon
);
642 * thread_stack_enqueue:
644 * Enqueue a thread for stack allocation.
646 * Called at splsched.
649 thread_stack_enqueue(
652 simple_lock(&thread_stack_lock
);
653 enqueue_tail(&thread_stack_queue
, (queue_entry_t
)thread
);
654 simple_unlock(&thread_stack_lock
);
656 thread_wakeup((event_t
)&thread_stack_queue
);
660 thread_daemon_init(void)
662 kern_return_t result
;
663 thread_t thread
= NULL
;
665 simple_lock_init(&thread_terminate_lock
, 0);
666 queue_init(&thread_terminate_queue
);
668 result
= kernel_thread_start_priority((thread_continue_t
)thread_terminate_daemon
, NULL
, MINPRI_KERNEL
, &thread
);
669 if (result
!= KERN_SUCCESS
)
670 panic("thread_daemon_init: thread_terminate_daemon");
672 thread_deallocate(thread
);
674 simple_lock_init(&thread_stack_lock
, 0);
675 queue_init(&thread_stack_queue
);
677 result
= kernel_thread_start_priority((thread_continue_t
)thread_stack_daemon
, NULL
, BASEPRI_PREEMPT
, &thread
);
678 if (result
!= KERN_SUCCESS
)
679 panic("thread_daemon_init: thread_stack_daemon");
681 thread_deallocate(thread
);
685 * Create a new thread.
686 * Doesn't start the thread running.
689 thread_create_internal(
692 thread_continue_t continuation
,
694 #define TH_OPTION_NONE 0x00
695 #define TH_OPTION_NOCRED 0x01
696 #define TH_OPTION_NOSUSP 0x02
697 thread_t
*out_thread
)
700 static thread_t first_thread
;
703 * Allocate a thread and initialize static fields
705 if (first_thread
== THREAD_NULL
)
706 new_thread
= first_thread
= current_thread();
708 new_thread
= (thread_t
)zalloc(thread_zone
);
709 if (new_thread
== THREAD_NULL
)
710 return (KERN_RESOURCE_SHORTAGE
);
712 if (new_thread
!= first_thread
)
713 *new_thread
= thread_template
;
716 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
, (options
& TH_OPTION_NOCRED
) != 0);
717 if (new_thread
->uthread
== NULL
) {
718 zfree(thread_zone
, new_thread
);
719 return (KERN_RESOURCE_SHORTAGE
);
721 #endif /* MACH_BSD */
723 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
725 void *ut
= new_thread
->uthread
;
727 new_thread
->uthread
= NULL
;
728 /* cred free may not be necessary */
729 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
730 uthread_cred_free(ut
);
731 uthread_zone_free(ut
);
732 #endif /* MACH_BSD */
734 zfree(thread_zone
, new_thread
);
735 return (KERN_FAILURE
);
738 new_thread
->task
= parent_task
;
740 thread_lock_init(new_thread
);
741 wake_lock_init(new_thread
);
743 lck_mtx_init(&new_thread
->mutex
, &thread_lck_grp
, &thread_lck_attr
);
745 ipc_thread_init(new_thread
);
747 new_thread
->continuation
= continuation
;
749 lck_mtx_lock(&tasks_threads_lock
);
750 task_lock(parent_task
);
752 if ( !parent_task
->active
|| parent_task
->halting
||
753 ((options
& TH_OPTION_NOSUSP
) != 0 &&
754 parent_task
->suspend_count
> 0) ||
755 (parent_task
->thread_count
>= task_threadmax
&&
756 parent_task
!= kernel_task
) ) {
757 task_unlock(parent_task
);
758 lck_mtx_unlock(&tasks_threads_lock
);
762 void *ut
= new_thread
->uthread
;
764 new_thread
->uthread
= NULL
;
765 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
766 /* cred free may not be necessary */
767 uthread_cred_free(ut
);
768 uthread_zone_free(ut
);
770 #endif /* MACH_BSD */
771 ipc_thread_disable(new_thread
);
772 ipc_thread_terminate(new_thread
);
773 lck_mtx_destroy(&new_thread
->mutex
, &thread_lck_grp
);
774 machine_thread_destroy(new_thread
);
775 zfree(thread_zone
, new_thread
);
776 return (KERN_FAILURE
);
779 /* New threads inherit any default state on the task */
780 machine_thread_inherit_taskwide(new_thread
, parent_task
);
782 task_reference_internal(parent_task
);
784 if (new_thread
->task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
786 * This task has a per-thread CPU limit; make sure this new thread
787 * gets its limit set too, before it gets out of the kernel.
789 set_astledger(new_thread
);
791 new_thread
->t_threadledger
= LEDGER_NULL
; /* per thread ledger is not inherited */
792 new_thread
->t_ledger
= new_thread
->task
->ledger
;
793 if (new_thread
->t_ledger
)
794 ledger_reference(new_thread
->t_ledger
);
796 /* Cache the task's map */
797 new_thread
->map
= parent_task
->map
;
799 /* Chain the thread onto the task's list */
800 queue_enter(&parent_task
->threads
, new_thread
, thread_t
, task_threads
);
801 parent_task
->thread_count
++;
803 /* So terminating threads don't need to take the task lock to decrement */
804 hw_atomic_add(&parent_task
->active_thread_count
, 1);
806 /* Protected by the tasks_threads_lock */
807 new_thread
->thread_id
= ++thread_unique_id
;
809 queue_enter(&threads
, new_thread
, thread_t
, threads
);
812 timer_call_setup(&new_thread
->wait_timer
, thread_timer_expire
, new_thread
);
813 timer_call_setup(&new_thread
->depress_timer
, thread_depress_expire
, new_thread
);
817 * If parent task has any reservations, they need to be propagated to this
820 new_thread
->t_chud
= (TASK_PMC_FLAG
== (parent_task
->t_chud
& TASK_PMC_FLAG
)) ?
821 THREAD_PMC_FLAG
: 0U;
824 kpc_thread_create(new_thread
);
827 /* Only need to update policies pushed from task to thread */
828 new_thread
->requested_policy
.bg_iotier
= parent_task
->effective_policy
.bg_iotier
;
829 new_thread
->requested_policy
.terminated
= parent_task
->effective_policy
.terminated
;
831 /* Set the thread's scheduling parameters */
832 new_thread
->sched_mode
= SCHED(initial_thread_sched_mode
)(parent_task
);
833 new_thread
->sched_flags
= 0;
834 new_thread
->max_priority
= parent_task
->max_priority
;
835 new_thread
->task_priority
= parent_task
->priority
;
836 new_thread
->priority
= (priority
< 0)? parent_task
->priority
: priority
;
837 if (new_thread
->priority
> new_thread
->max_priority
)
838 new_thread
->priority
= new_thread
->max_priority
;
839 new_thread
->importance
= new_thread
->priority
- new_thread
->task_priority
;
840 new_thread
->saved_importance
= new_thread
->importance
;
842 #if defined(CONFIG_SCHED_TRADITIONAL)
843 new_thread
->sched_stamp
= sched_tick
;
844 new_thread
->pri_shift
= sched_pri_shift
;
846 SCHED(compute_priority
)(new_thread
, FALSE
);
848 new_thread
->active
= TRUE
;
850 *out_thread
= new_thread
;
853 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
855 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
857 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
858 TRACEDBG_CODE(DBG_TRACE_DATA
, 1) | DBG_FUNC_NONE
,
859 (vm_address_t
)(uintptr_t)thread_tid(new_thread
), dbg_arg2
, 0, 0, 0);
861 kdbg_trace_string(parent_task
->bsd_info
,
862 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
864 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
865 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
866 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
869 DTRACE_PROC1(lwp__create
, thread_t
, *out_thread
);
871 return (KERN_SUCCESS
);
875 thread_create_internal2(
877 thread_t
*new_thread
,
880 kern_return_t result
;
883 if (task
== TASK_NULL
|| task
== kernel_task
)
884 return (KERN_INVALID_ARGUMENT
);
886 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
887 if (result
!= KERN_SUCCESS
)
890 thread
->user_stop_count
= 1;
892 if (task
->suspend_count
> 0)
896 extmod_statistics_incr_thread_create(task
);
899 lck_mtx_unlock(&tasks_threads_lock
);
901 *new_thread
= thread
;
903 return (KERN_SUCCESS
);
906 /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
910 thread_t
*new_thread
);
915 thread_t
*new_thread
)
917 return thread_create_internal2(task
, new_thread
, FALSE
);
921 thread_create_from_user(
923 thread_t
*new_thread
)
925 return thread_create_internal2(task
, new_thread
, TRUE
);
929 thread_create_running_internal2(
930 register task_t task
,
932 thread_state_t new_state
,
933 mach_msg_type_number_t new_state_count
,
934 thread_t
*new_thread
,
937 register kern_return_t result
;
940 if (task
== TASK_NULL
|| task
== kernel_task
)
941 return (KERN_INVALID_ARGUMENT
);
943 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
944 if (result
!= KERN_SUCCESS
)
947 result
= machine_thread_set_state(
948 thread
, flavor
, new_state
, new_state_count
);
949 if (result
!= KERN_SUCCESS
) {
951 lck_mtx_unlock(&tasks_threads_lock
);
953 thread_terminate(thread
);
954 thread_deallocate(thread
);
958 thread_mtx_lock(thread
);
959 thread_start_internal(thread
);
960 thread_mtx_unlock(thread
);
963 extmod_statistics_incr_thread_create(task
);
966 lck_mtx_unlock(&tasks_threads_lock
);
968 *new_thread
= thread
;
973 /* Prototype, see justification above */
975 thread_create_running(
976 register task_t task
,
978 thread_state_t new_state
,
979 mach_msg_type_number_t new_state_count
,
980 thread_t
*new_thread
);
983 thread_create_running(
984 register task_t task
,
986 thread_state_t new_state
,
987 mach_msg_type_number_t new_state_count
,
988 thread_t
*new_thread
)
990 return thread_create_running_internal2(
991 task
, flavor
, new_state
, new_state_count
,
996 thread_create_running_from_user(
997 register task_t task
,
999 thread_state_t new_state
,
1000 mach_msg_type_number_t new_state_count
,
1001 thread_t
*new_thread
)
1003 return thread_create_running_internal2(
1004 task
, flavor
, new_state
, new_state_count
,
1009 thread_create_workq(
1011 thread_continue_t thread_return
,
1012 thread_t
*new_thread
)
1014 kern_return_t result
;
1017 if (task
== TASK_NULL
|| task
== kernel_task
)
1018 return (KERN_INVALID_ARGUMENT
);
1020 result
= thread_create_internal(task
, -1, thread_return
, TH_OPTION_NOCRED
| TH_OPTION_NOSUSP
, &thread
);
1021 if (result
!= KERN_SUCCESS
)
1024 thread
->user_stop_count
= 1;
1025 thread_hold(thread
);
1026 if (task
->suspend_count
> 0)
1027 thread_hold(thread
);
1030 lck_mtx_unlock(&tasks_threads_lock
);
1032 *new_thread
= thread
;
1034 return (KERN_SUCCESS
);
1038 * kernel_thread_create:
1040 * Create a thread in the kernel task
1041 * to execute in kernel context.
1044 kernel_thread_create(
1045 thread_continue_t continuation
,
1048 thread_t
*new_thread
)
1050 kern_return_t result
;
1052 task_t task
= kernel_task
;
1054 result
= thread_create_internal(task
, priority
, continuation
, TH_OPTION_NONE
, &thread
);
1055 if (result
!= KERN_SUCCESS
)
1059 lck_mtx_unlock(&tasks_threads_lock
);
1061 stack_alloc(thread
);
1062 assert(thread
->kernel_stack
!= 0);
1063 thread
->reserved_stack
= thread
->kernel_stack
;
1065 thread
->parameter
= parameter
;
1068 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread
, continuation
);
1069 *new_thread
= thread
;
1075 kernel_thread_start_priority(
1076 thread_continue_t continuation
,
1079 thread_t
*new_thread
)
1081 kern_return_t result
;
1084 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
1085 if (result
!= KERN_SUCCESS
)
1088 *new_thread
= thread
;
1090 thread_mtx_lock(thread
);
1091 thread_start_internal(thread
);
1092 thread_mtx_unlock(thread
);
1098 kernel_thread_start(
1099 thread_continue_t continuation
,
1101 thread_t
*new_thread
)
1103 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
1108 thread_info_internal(
1109 register thread_t thread
,
1110 thread_flavor_t flavor
,
1111 thread_info_t thread_info_out
, /* ptr to OUT array */
1112 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
1117 if (thread
== THREAD_NULL
)
1118 return (KERN_INVALID_ARGUMENT
);
1120 if (flavor
== THREAD_BASIC_INFO
) {
1121 register thread_basic_info_t basic_info
;
1123 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
1124 return (KERN_INVALID_ARGUMENT
);
1126 basic_info
= (thread_basic_info_t
) thread_info_out
;
1129 thread_lock(thread
);
1133 thread_read_times(thread
, &basic_info
->user_time
,
1134 &basic_info
->system_time
);
1137 * Update lazy-evaluated scheduler info because someone wants it.
1139 if (SCHED(can_update_priority
)(thread
))
1140 SCHED(update_priority
)(thread
);
1142 basic_info
->sleep_time
= 0;
1145 * To calculate cpu_usage, first correct for timer rate,
1146 * then for 5/8 ageing. The correction factor [3/5] is
1149 basic_info
->cpu_usage
= 0;
1150 #if defined(CONFIG_SCHED_TRADITIONAL)
1151 if (sched_tick_interval
) {
1152 basic_info
->cpu_usage
= (integer_t
)(((uint64_t)thread
->cpu_usage
1153 * TH_USAGE_SCALE
) / sched_tick_interval
);
1154 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
1158 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
)
1159 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
1161 basic_info
->policy
= ((thread
->sched_mode
== TH_MODE_TIMESHARE
)?
1162 POLICY_TIMESHARE
: POLICY_RR
);
1165 if (thread
->options
& TH_OPT_IDLE_THREAD
)
1166 flags
|= TH_FLAGS_IDLE
;
1168 if (!thread
->kernel_stack
)
1169 flags
|= TH_FLAGS_SWAPPED
;
1172 if (thread
->state
& TH_TERMINATE
)
1173 state
= TH_STATE_HALTED
;
1175 if (thread
->state
& TH_RUN
)
1176 state
= TH_STATE_RUNNING
;
1178 if (thread
->state
& TH_UNINT
)
1179 state
= TH_STATE_UNINTERRUPTIBLE
;
1181 if (thread
->state
& TH_SUSP
)
1182 state
= TH_STATE_STOPPED
;
1184 if (thread
->state
& TH_WAIT
)
1185 state
= TH_STATE_WAITING
;
1187 basic_info
->run_state
= state
;
1188 basic_info
->flags
= flags
;
1190 basic_info
->suspend_count
= thread
->user_stop_count
;
1192 thread_unlock(thread
);
1195 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
1197 return (KERN_SUCCESS
);
1200 if (flavor
== THREAD_IDENTIFIER_INFO
) {
1201 register thread_identifier_info_t identifier_info
;
1203 if (*thread_info_count
< THREAD_IDENTIFIER_INFO_COUNT
)
1204 return (KERN_INVALID_ARGUMENT
);
1206 identifier_info
= (thread_identifier_info_t
) thread_info_out
;
1209 thread_lock(thread
);
1211 identifier_info
->thread_id
= thread
->thread_id
;
1212 identifier_info
->thread_handle
= thread
->machine
.cthread_self
;
1213 if(thread
->task
->bsd_info
) {
1214 identifier_info
->dispatch_qaddr
= identifier_info
->thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1216 thread_unlock(thread
);
1218 return KERN_INVALID_ARGUMENT
;
1221 thread_unlock(thread
);
1223 return KERN_SUCCESS
;
1226 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
1227 policy_timeshare_info_t ts_info
;
1229 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
1230 return (KERN_INVALID_ARGUMENT
);
1232 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
1235 thread_lock(thread
);
1237 if (thread
->sched_mode
!= TH_MODE_TIMESHARE
) {
1238 thread_unlock(thread
);
1241 return (KERN_INVALID_POLICY
);
1244 ts_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1245 if (ts_info
->depressed
) {
1246 ts_info
->base_priority
= DEPRESSPRI
;
1247 ts_info
->depress_priority
= thread
->priority
;
1250 ts_info
->base_priority
= thread
->priority
;
1251 ts_info
->depress_priority
= -1;
1254 ts_info
->cur_priority
= thread
->sched_pri
;
1255 ts_info
->max_priority
= thread
->max_priority
;
1257 thread_unlock(thread
);
1260 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
1262 return (KERN_SUCCESS
);
1265 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
1266 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
1267 return (KERN_INVALID_ARGUMENT
);
1269 return (KERN_INVALID_POLICY
);
1272 if (flavor
== THREAD_SCHED_RR_INFO
) {
1273 policy_rr_info_t rr_info
;
1274 uint32_t quantum_time
;
1275 uint64_t quantum_ns
;
1277 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
1278 return (KERN_INVALID_ARGUMENT
);
1280 rr_info
= (policy_rr_info_t
) thread_info_out
;
1283 thread_lock(thread
);
1285 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
1286 thread_unlock(thread
);
1289 return (KERN_INVALID_POLICY
);
1292 rr_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1293 if (rr_info
->depressed
) {
1294 rr_info
->base_priority
= DEPRESSPRI
;
1295 rr_info
->depress_priority
= thread
->priority
;
1298 rr_info
->base_priority
= thread
->priority
;
1299 rr_info
->depress_priority
= -1;
1302 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
1303 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
1305 rr_info
->max_priority
= thread
->max_priority
;
1306 rr_info
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
1308 thread_unlock(thread
);
1311 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1313 return (KERN_SUCCESS
);
1316 return (KERN_INVALID_ARGUMENT
);
1322 time_value_t
*user_time
,
1323 time_value_t
*system_time
)
1327 uint64_t tval_user
, tval_system
;
1329 tval_user
= timer_grab(&thread
->user_timer
);
1330 tval_system
= timer_grab(&thread
->system_timer
);
1332 if (thread
->precise_user_kernel_time
) {
1333 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1334 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1335 user_time
->microseconds
= usecs
;
1337 absolutetime_to_microtime(tval_system
, &secs
, &usecs
);
1338 system_time
->seconds
= (typeof(system_time
->seconds
))secs
;
1339 system_time
->microseconds
= usecs
;
1341 /* system_timer may represent either sys or user */
1342 tval_user
+= tval_system
;
1343 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1344 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1345 user_time
->microseconds
= usecs
;
1347 system_time
->seconds
= 0;
1348 system_time
->microseconds
= 0;
1354 __unused thread_t thread
,
1355 __unused processor_set_t new_pset
)
1357 return (KERN_FAILURE
);
1361 * thread_assign_default:
1363 * Special version of thread_assign for assigning threads to default
1367 thread_assign_default(
1370 return (thread_assign(thread
, &pset0
));
1374 * thread_get_assignment
1376 * Return current assignment for this thread.
1379 thread_get_assignment(
1381 processor_set_t
*pset
)
1384 return (KERN_INVALID_ARGUMENT
);
1388 return (KERN_SUCCESS
);
1392 * thread_wire_internal:
1394 * Specify that the target thread must always be able
1395 * to run and to allocate memory.
1398 thread_wire_internal(
1399 host_priv_t host_priv
,
1402 boolean_t
*prev_state
)
1404 if (host_priv
== NULL
|| thread
!= current_thread())
1405 return (KERN_INVALID_ARGUMENT
);
1407 assert(host_priv
== &realhost
);
1410 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
1413 if (!(thread
->options
& TH_OPT_VMPRIV
))
1414 vm_page_free_reserve(1); /* XXX */
1415 thread
->options
|= TH_OPT_VMPRIV
;
1418 if (thread
->options
& TH_OPT_VMPRIV
)
1419 vm_page_free_reserve(-1); /* XXX */
1420 thread
->options
&= ~TH_OPT_VMPRIV
;
1423 return (KERN_SUCCESS
);
1430 * User-api wrapper for thread_wire_internal()
1434 host_priv_t host_priv
,
1438 return (thread_wire_internal(host_priv
, thread
, wired
, NULL
));
1443 * XXX assuming current thread only, for now...
1446 thread_guard_violation(thread_t thread
, unsigned type
)
1448 assert(thread
== current_thread());
1450 spl_t s
= splsched();
1452 * Use the saved state area of the thread structure
1453 * to store all info required to handle the AST when
1454 * returning to userspace
1456 thread
->guard_exc_info
.type
= type
;
1457 thread_ast_set(thread
, AST_GUARD
);
1458 ast_propagate(thread
->ast
);
1466 * Handle AST_GUARD for a thread. This routine looks at the
1467 * state saved in the thread structure to determine the cause
1468 * of this exception. Based on this value, it invokes the
1469 * appropriate routine which determines other exception related
1470 * info and raises the exception.
1473 guard_ast(thread_t thread
)
1475 if (thread
->guard_exc_info
.type
== GUARD_TYPE_MACH_PORT
)
1476 mach_port_guard_ast(thread
);
1478 fd_guard_ast(thread
);
1482 thread_cputime_callback(int warning
, __unused
const void *arg0
, __unused
const void *arg1
)
1484 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
1485 #if CONFIG_TELEMETRY
1487 * This thread is in danger of violating the CPU usage monitor. Enable telemetry
1488 * on the entire task so there are micro-stackshots available if and when
1489 * EXC_RESOURCE is triggered. We could have chosen to enable micro-stackshots
1490 * for this thread only; but now that this task is suspect, knowing what all of
1491 * its threads are up to will be useful.
1493 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 1);
1498 #if CONFIG_TELEMETRY
1500 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
1501 * exceeded the limit, turn telemetry off for the task.
1503 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 0);
1507 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE();
1511 void __attribute__((noinline
))
1512 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void)
1515 task_t task
= current_task();
1516 thread_t thread
= current_thread();
1517 uint64_t tid
= thread
->thread_id
;
1518 char *procname
= (char *) "unknown";
1519 time_value_t thread_total_time
= {0, 0};
1520 time_value_t thread_system_time
;
1521 time_value_t thread_user_time
;
1524 uint32_t limit_percent
;
1525 uint32_t usage_percent
;
1526 uint32_t interval_sec
;
1527 uint64_t interval_ns
;
1528 uint64_t balance_ns
;
1529 boolean_t fatal
= FALSE
;
1531 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1532 struct ledger_entry_info lei
;
1534 assert(thread
->t_threadledger
!= LEDGER_NULL
);
1537 * Now that a thread has tripped the monitor, disable it for the entire task.
1540 if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) {
1542 * The CPU usage monitor has been disabled on our task, so some other
1543 * thread must have gotten here first. We only send one exception per
1544 * task lifetime, so there's nothing left for us to do here.
1549 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_CPUMON
) {
1552 task_disable_cpumon(task
);
1556 pid
= proc_selfpid();
1557 if (task
->bsd_info
!= NULL
)
1558 procname
= proc_name_address(task
->bsd_info
);
1561 thread_get_cpulimit(&action
, &percentage
, &interval_ns
);
1563 interval_sec
= (uint32_t)(interval_ns
/ NSEC_PER_SEC
);
1565 thread_read_times(thread
, &thread_user_time
, &thread_system_time
);
1566 time_value_add(&thread_total_time
, &thread_user_time
);
1567 time_value_add(&thread_total_time
, &thread_system_time
);
1569 ledger_get_entry_info(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &lei
);
1571 absolutetime_to_nanoseconds(lei
.lei_balance
, &balance_ns
);
1572 usage_percent
= (uint32_t) ((balance_ns
* 100ULL) / lei
.lei_last_refill
);
1574 /* Show refill period in the same units as balance, limit, etc */
1575 nanoseconds_to_absolutetime(lei
.lei_refill_period
, &lei
.lei_refill_period
);
1577 limit_percent
= (uint32_t) ((lei
.lei_limit
* 100ULL) / lei
.lei_refill_period
);
1579 /* TODO: show task total runtime as well? see TASK_ABSOLUTETIME_INFO */
1581 if (disable_exc_resource
) {
1582 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1583 "supressed by a boot-arg\n", procname
, pid
, tid
);
1587 printf("process %s[%d] thread %llu caught burning CPU! "
1588 "It used more than %d%% CPU (Actual recent usage: %d%%) over %d seconds. "
1589 "thread lifetime cpu usage %d.%06d seconds, (%d.%06d user, %d.%06d system) "
1590 "ledger info: balance: %lld credit: %lld debit: %lld limit: %llu (%d%%) "
1591 "period: %llu time since last refill (ns): %llu \n",
1593 percentage
, usage_percent
, interval_sec
,
1594 thread_total_time
.seconds
, thread_total_time
.microseconds
,
1595 thread_user_time
.seconds
, thread_user_time
.microseconds
,
1596 thread_system_time
.seconds
, thread_system_time
.microseconds
,
1598 lei
.lei_credit
, lei
.lei_debit
,
1599 lei
.lei_limit
, limit_percent
,
1600 lei
.lei_refill_period
, lei
.lei_last_refill
);
1603 code
[0] = code
[1] = 0;
1604 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_CPU
);
1605 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_CPU_MONITOR
);
1606 EXC_RESOURCE_CPUMONITOR_ENCODE_INTERVAL(code
[0], interval_sec
);
1607 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[0], limit_percent
);
1608 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[1], usage_percent
);
1609 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
1612 task_terminate_internal(task
);
1617 init_thread_ledgers(void) {
1618 ledger_template_t t
;
1621 assert(thread_ledger_template
== NULL
);
1623 if ((t
= ledger_template_create("Per-thread ledger")) == NULL
)
1624 panic("couldn't create thread ledger template");
1626 if ((idx
= ledger_entry_add(t
, "cpu_time", "sched", "ns")) < 0) {
1627 panic("couldn't create cpu_time entry for thread ledger template");
1630 if (ledger_set_callback(t
, idx
, thread_cputime_callback
, NULL
, NULL
) < 0) {
1631 panic("couldn't set thread ledger callback for cpu_time entry");
1634 thread_ledgers
.cpu_time
= idx
;
1635 thread_ledger_template
= t
;
1639 * Returns currently applied CPU usage limit, or 0/0 if none is applied.
1642 thread_get_cpulimit(int *action
, uint8_t *percentage
, uint64_t *interval_ns
)
1644 int64_t abstime
= 0;
1645 uint64_t limittime
= 0;
1646 thread_t thread
= current_thread();
1652 if (thread
->t_threadledger
== LEDGER_NULL
) {
1654 * This thread has no per-thread ledger, so it can't possibly
1655 * have a CPU limit applied.
1657 return (KERN_SUCCESS
);
1660 ledger_get_period(thread
->t_threadledger
, thread_ledgers
.cpu_time
, interval_ns
);
1661 ledger_get_limit(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &abstime
);
1663 if ((abstime
== LEDGER_LIMIT_INFINITY
) || (*interval_ns
== 0)) {
1665 * This thread's CPU time ledger has no period or limit; so it
1666 * doesn't have a CPU limit applied.
1668 return (KERN_SUCCESS
);
1672 * This calculation is the converse to the one in thread_set_cpulimit().
1674 absolutetime_to_nanoseconds(abstime
, &limittime
);
1675 *percentage
= (limittime
* 100ULL) / *interval_ns
;
1676 assert(*percentage
<= 100);
1678 if (thread
->options
& TH_OPT_PROC_CPULIMIT
) {
1679 assert((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0);
1681 *action
= THREAD_CPULIMIT_BLOCK
;
1682 } else if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
1683 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0);
1685 *action
= THREAD_CPULIMIT_EXCEPTION
;
1687 *action
= THREAD_CPULIMIT_DISABLE
;
1690 return (KERN_SUCCESS
);
1694 * Set CPU usage limit on a thread.
1696 * Calling with percentage of 0 will unset the limit for this thread.
1699 thread_set_cpulimit(int action
, uint8_t percentage
, uint64_t interval_ns
)
1701 thread_t thread
= current_thread();
1703 uint64_t limittime
= 0;
1704 uint64_t abstime
= 0;
1706 assert(percentage
<= 100);
1708 if (action
== THREAD_CPULIMIT_DISABLE
) {
1710 * Remove CPU limit, if any exists.
1712 if (thread
->t_threadledger
!= LEDGER_NULL
) {
1713 l
= thread
->t_threadledger
;
1715 * The only way to get a per-thread ledger is via CPU limits.
1717 assert(thread
->options
& (TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
));
1718 thread
->t_threadledger
= NULL
;
1719 ledger_dereference(l
);
1720 thread
->options
&= ~(TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
);
1726 if (interval_ns
< MINIMUM_CPULIMIT_INTERVAL_MS
* NSEC_PER_MSEC
) {
1727 return (KERN_INVALID_ARGUMENT
);
1730 l
= thread
->t_threadledger
;
1731 if (l
== LEDGER_NULL
) {
1733 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
1735 if ((l
= ledger_instantiate(thread_ledger_template
, LEDGER_CREATE_INACTIVE_ENTRIES
)) == LEDGER_NULL
)
1736 return (KERN_RESOURCE_SHORTAGE
);
1739 * We are the first to create this thread's ledger, so only activate our entry.
1741 ledger_entry_setactive(l
, thread_ledgers
.cpu_time
);
1742 thread
->t_threadledger
= l
;
1746 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
1747 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
1749 limittime
= (interval_ns
* percentage
) / 100;
1750 nanoseconds_to_absolutetime(limittime
, &abstime
);
1751 ledger_set_limit(l
, thread_ledgers
.cpu_time
, abstime
, cpumon_ustackshots_trigger_pct
);
1753 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
1755 ledger_set_period(l
, thread_ledgers
.cpu_time
, interval_ns
);
1757 if (action
== THREAD_CPULIMIT_EXCEPTION
) {
1759 * We don't support programming the CPU usage monitor on a task if any of its
1760 * threads have a per-thread blocking CPU limit configured.
1762 if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
1763 panic("CPU usage monitor activated, but blocking thread limit exists");
1767 * Make a note that this thread's CPU limit is being used for the task-wide CPU
1768 * usage monitor. We don't have to arm the callback which will trigger the
1769 * exception, because that was done for us in ledger_instantiate (because the
1770 * ledger template used has a default callback).
1772 thread
->options
|= TH_OPT_PROC_CPULIMIT
;
1775 * We deliberately override any CPU limit imposed by a task-wide limit (eg
1776 * CPU usage monitor).
1778 thread
->options
&= ~TH_OPT_PROC_CPULIMIT
;
1780 thread
->options
|= TH_OPT_PRVT_CPULIMIT
;
1781 /* The per-thread ledger template by default has a callback for CPU time */
1782 ledger_disable_callback(l
, thread_ledgers
.cpu_time
);
1783 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_BLOCK
);
1789 int split_funnel_off
= 0;
1790 lck_grp_t
*funnel_lck_grp
= LCK_GRP_NULL
;
1791 lck_grp_attr_t
*funnel_lck_grp_attr
;
1792 lck_attr_t
*funnel_lck_attr
;
1801 if (funnel_lck_grp
== LCK_GRP_NULL
) {
1802 funnel_lck_grp_attr
= lck_grp_attr_alloc_init();
1804 funnel_lck_grp
= lck_grp_alloc_init("Funnel", funnel_lck_grp_attr
);
1806 funnel_lck_attr
= lck_attr_alloc_init();
1808 if ((fnl
= (funnel_t
*)kalloc(sizeof(funnel_t
))) != 0){
1809 bzero((void *)fnl
, sizeof(funnel_t
));
1810 if ((m
= lck_mtx_alloc_init(funnel_lck_grp
, funnel_lck_attr
)) == (lck_mtx_t
*)NULL
) {
1811 kfree(fnl
, sizeof(funnel_t
));
1812 return(THR_FUNNEL_NULL
);
1815 fnl
->fnl_type
= type
;
1824 lck_mtx_free(fnl
->fnl_mutex
, funnel_lck_grp
);
1825 if (fnl
->fnl_oldmutex
)
1826 lck_mtx_free(fnl
->fnl_oldmutex
, funnel_lck_grp
);
1827 kfree(fnl
, sizeof(funnel_t
));
1834 lck_mtx_lock(fnl
->fnl_mutex
);
1835 fnl
->fnl_mtxholder
= current_thread();
1842 lck_mtx_unlock(fnl
->fnl_mutex
);
1843 fnl
->fnl_mtxholder
= NULL
;
1844 fnl
->fnl_mtxrelease
= current_thread();
1851 thread_t th
= current_thread();
1853 if (th
->funnel_state
& TH_FN_OWNED
) {
1854 return(th
->funnel_lock
);
1856 return(THR_FUNNEL_NULL
);
1864 thread_t cur_thread
;
1865 boolean_t funnel_state_prev
;
1868 cur_thread
= current_thread();
1869 funnel_state_prev
= ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
);
1871 if (funnel_state_prev
!= funneled
) {
1872 intr
= ml_set_interrupts_enabled(FALSE
);
1874 if (funneled
== TRUE
) {
1875 if (cur_thread
->funnel_lock
)
1876 panic("Funnel lock called when holding one %p", cur_thread
->funnel_lock
);
1877 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
,
1880 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
,
1882 cur_thread
->funnel_state
|= TH_FN_OWNED
;
1883 cur_thread
->funnel_lock
= fnl
;
1885 if(cur_thread
->funnel_lock
->fnl_mutex
!= fnl
->fnl_mutex
)
1886 panic("Funnel unlock when not holding funnel");
1887 cur_thread
->funnel_state
&= ~TH_FN_OWNED
;
1888 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
,
1891 cur_thread
->funnel_lock
= THR_FUNNEL_NULL
;
1894 (void)ml_set_interrupts_enabled(intr
);
1896 /* if we are trying to acquire funnel recursively
1897 * check for funnel to be held already
1899 if (funneled
&& (fnl
->fnl_mutex
!= cur_thread
->funnel_lock
->fnl_mutex
)) {
1900 panic("thread_funnel_set: already holding a different funnel");
1903 return(funnel_state_prev
);
1909 __unused thread_t thread
)
1919 thread
->sched_call
= (call
!= NULL
)? call
: sched_call_null
;
1923 thread_static_param(
1927 thread_mtx_lock(thread
);
1928 thread
->static_param
= state
;
1929 thread_mtx_unlock(thread
);
1936 return (thread
!= THREAD_NULL
? thread
->thread_id
: 0);
1939 uint16_t thread_set_tag(thread_t th
, uint16_t tag
) {
1940 return thread_set_tag_internal(th
, tag
);
1942 uint16_t thread_get_tag(thread_t th
) {
1943 return thread_get_tag_internal(th
);
1947 thread_dispatchqaddr(
1950 uint64_t dispatchqueue_addr
= 0;
1951 uint64_t thread_handle
= 0;
1953 if (thread
!= THREAD_NULL
) {
1954 thread_handle
= thread
->machine
.cthread_self
;
1956 if (thread
->task
->bsd_info
)
1957 dispatchqueue_addr
= thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1960 return (dispatchqueue_addr
);
1964 * Export routines to other components for things that are done as macros
1965 * within the osfmk component.
1968 #undef thread_reference
1969 void thread_reference(thread_t thread
);
1974 if (thread
!= THREAD_NULL
)
1975 thread_reference_internal(thread
);
1978 #undef thread_should_halt
1984 return (thread_should_halt_fast(th
));
1988 uint32_t dtrace_get_thread_predcache(thread_t thread
)
1990 if (thread
!= THREAD_NULL
)
1991 return thread
->t_dtrace_predcache
;
1996 int64_t dtrace_get_thread_vtime(thread_t thread
)
1998 if (thread
!= THREAD_NULL
)
1999 return thread
->t_dtrace_vtime
;
2004 int64_t dtrace_get_thread_tracing(thread_t thread
)
2006 if (thread
!= THREAD_NULL
)
2007 return thread
->t_dtrace_tracing
;
2012 boolean_t
dtrace_get_thread_reentering(thread_t thread
)
2014 if (thread
!= THREAD_NULL
)
2015 return (thread
->options
& TH_OPT_DTRACE
) ? TRUE
: FALSE
;
2020 vm_offset_t
dtrace_get_kernel_stack(thread_t thread
)
2022 if (thread
!= THREAD_NULL
)
2023 return thread
->kernel_stack
;
2028 int64_t dtrace_calc_thread_recent_vtime(thread_t thread
)
2030 if (thread
!= THREAD_NULL
) {
2031 processor_t processor
= current_processor();
2032 uint64_t abstime
= mach_absolute_time();
2035 timer
= PROCESSOR_DATA(processor
, thread_timer
);
2037 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
)) +
2038 (abstime
- timer
->tstamp
); /* XXX need interrupts off to prevent missed time? */
2043 void dtrace_set_thread_predcache(thread_t thread
, uint32_t predcache
)
2045 if (thread
!= THREAD_NULL
)
2046 thread
->t_dtrace_predcache
= predcache
;
2049 void dtrace_set_thread_vtime(thread_t thread
, int64_t vtime
)
2051 if (thread
!= THREAD_NULL
)
2052 thread
->t_dtrace_vtime
= vtime
;
2055 void dtrace_set_thread_tracing(thread_t thread
, int64_t accum
)
2057 if (thread
!= THREAD_NULL
)
2058 thread
->t_dtrace_tracing
= accum
;
2061 void dtrace_set_thread_reentering(thread_t thread
, boolean_t vbool
)
2063 if (thread
!= THREAD_NULL
) {
2065 thread
->options
|= TH_OPT_DTRACE
;
2067 thread
->options
&= (~TH_OPT_DTRACE
);
2071 vm_offset_t
dtrace_set_thread_recover(thread_t thread
, vm_offset_t recover
)
2073 vm_offset_t prev
= 0;
2075 if (thread
!= THREAD_NULL
) {
2076 prev
= thread
->recover
;
2077 thread
->recover
= recover
;
2082 void dtrace_thread_bootstrap(void)
2084 task_t task
= current_task();
2086 if (task
->thread_count
== 1) {
2087 thread_t thread
= current_thread();
2088 if (thread
->t_dtrace_flags
& TH_DTRACE_EXECSUCCESS
) {
2089 thread
->t_dtrace_flags
&= ~TH_DTRACE_EXECSUCCESS
;
2090 DTRACE_PROC(exec__success
);
2094 DTRACE_PROC(lwp__start
);
2099 dtrace_thread_didexec(thread_t thread
)
2101 thread
->t_dtrace_flags
|= TH_DTRACE_EXECSUCCESS
;
2103 #endif /* CONFIG_DTRACE */