2 * Copyright (c) 2000-2015 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>
120 #include <corpses/task_corpse.h>
122 #include <kern/kpc.h>
125 #include <ipc/ipc_kmsg.h>
126 #include <ipc/ipc_port.h>
127 #include <bank/bank_types.h>
129 #include <vm/vm_kern.h>
130 #include <vm/vm_pageout.h>
132 #include <sys/kdebug.h>
133 #include <sys/bsdtask_info.h>
134 #include <mach/sdt.h>
137 * Exported interfaces
139 #include <mach/task_server.h>
140 #include <mach/thread_act_server.h>
141 #include <mach/mach_host_server.h>
142 #include <mach/host_priv_server.h>
143 #include <mach/mach_voucher_server.h>
145 static struct zone
*thread_zone
;
146 static lck_grp_attr_t thread_lck_grp_attr
;
147 lck_attr_t thread_lck_attr
;
148 lck_grp_t thread_lck_grp
;
150 struct zone
*thread_qos_override_zone
;
152 decl_simple_lock_data(static,thread_stack_lock
)
153 static queue_head_t thread_stack_queue
;
155 decl_simple_lock_data(static,thread_terminate_lock
)
156 static queue_head_t thread_terminate_queue
;
158 static queue_head_t crashed_threads_queue
;
160 static struct thread thread_template
, init_thread
;
162 static void sched_call_null(
167 extern void proc_exit(void *);
168 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
169 extern int proc_selfpid(void);
170 extern char * proc_name_address(void *p
);
171 #endif /* MACH_BSD */
173 extern int disable_exc_resource
;
174 extern int audio_active
;
175 extern int debug_task
;
176 int thread_max
= CONFIG_THREAD_MAX
; /* Max number of threads */
177 int task_threadmax
= CONFIG_THREAD_MAX
;
179 static uint64_t thread_unique_id
= 100;
181 struct _thread_ledger_indices thread_ledgers
= { -1 };
182 static ledger_template_t thread_ledger_template
= NULL
;
183 void init_thread_ledgers(void);
184 int task_disable_cpumon(task_t task
);
187 void jetsam_on_ledger_cpulimit_exceeded(void);
191 * Level (in terms of percentage of the limit) at which the CPU usage monitor triggers telemetry.
193 * (ie when any thread's CPU consumption exceeds 70% of the limit, start taking user
194 * stacktraces, aka micro-stackshots)
196 #define CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT 70
198 int cpumon_ustackshots_trigger_pct
; /* Percentage. Level at which we start gathering telemetry. */
199 void __attribute__((noinline
)) THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void);
202 * The smallest interval over which we support limiting CPU consumption is 1ms
204 #define MINIMUM_CPULIMIT_INTERVAL_MS 1
207 thread_bootstrap(void)
210 * Fill in a template thread for fast initialization.
213 thread_template
.runq
= PROCESSOR_NULL
;
215 thread_template
.ref_count
= 2;
217 thread_template
.reason
= AST_NONE
;
218 thread_template
.at_safe_point
= FALSE
;
219 thread_template
.wait_event
= NO_EVENT64
;
220 thread_template
.waitq
= NULL
;
221 thread_template
.wait_result
= THREAD_WAITING
;
222 thread_template
.options
= THREAD_ABORTSAFE
;
223 thread_template
.state
= TH_WAIT
| TH_UNINT
;
224 thread_template
.wake_active
= FALSE
;
225 thread_template
.continuation
= THREAD_CONTINUE_NULL
;
226 thread_template
.parameter
= NULL
;
228 thread_template
.importance
= 0;
229 thread_template
.sched_mode
= TH_MODE_NONE
;
230 thread_template
.sched_flags
= 0;
231 thread_template
.saved_mode
= TH_MODE_NONE
;
232 thread_template
.safe_release
= 0;
234 thread_template
.sfi_class
= SFI_CLASS_UNSPECIFIED
;
235 thread_template
.sfi_wait_class
= SFI_CLASS_UNSPECIFIED
;
237 thread_template
.active
= 0;
238 thread_template
.started
= 0;
239 thread_template
.static_param
= 0;
240 thread_template
.policy_reset
= 0;
242 thread_template
.base_pri
= 0;
243 thread_template
.sched_pri
= 0;
244 thread_template
.max_priority
= 0;
245 thread_template
.task_priority
= 0;
246 thread_template
.promotions
= 0;
247 thread_template
.pending_promoter_index
= 0;
248 thread_template
.pending_promoter
[0] = NULL
;
249 thread_template
.pending_promoter
[1] = NULL
;
250 thread_template
.rwlock_count
= 0;
253 thread_template
.SHARE_COUNT
= 0;
254 thread_template
.BG_COUNT
= 0;
255 #endif /* MACH_ASSERT */
257 thread_template
.realtime
.deadline
= UINT64_MAX
;
259 thread_template
.quantum_remaining
= 0;
260 thread_template
.last_run_time
= 0;
261 thread_template
.last_made_runnable_time
= 0;
263 thread_template
.computation_metered
= 0;
264 thread_template
.computation_epoch
= 0;
266 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
267 thread_template
.sched_stamp
= 0;
268 thread_template
.pri_shift
= INT8_MAX
;
269 thread_template
.sched_usage
= 0;
270 thread_template
.cpu_usage
= thread_template
.cpu_delta
= 0;
272 thread_template
.c_switch
= thread_template
.p_switch
= thread_template
.ps_switch
= 0;
274 thread_template
.bound_processor
= PROCESSOR_NULL
;
275 thread_template
.last_processor
= PROCESSOR_NULL
;
277 thread_template
.sched_call
= sched_call_null
;
279 timer_init(&thread_template
.user_timer
);
280 timer_init(&thread_template
.system_timer
);
281 thread_template
.user_timer_save
= 0;
282 thread_template
.system_timer_save
= 0;
283 thread_template
.vtimer_user_save
= 0;
284 thread_template
.vtimer_prof_save
= 0;
285 thread_template
.vtimer_rlim_save
= 0;
288 thread_template
.wait_sfi_begin_time
= 0;
291 thread_template
.wait_timer_is_set
= FALSE
;
292 thread_template
.wait_timer_active
= 0;
294 thread_template
.depress_timer_active
= 0;
296 thread_template
.recover
= (vm_offset_t
)NULL
;
298 thread_template
.map
= VM_MAP_NULL
;
301 thread_template
.t_dtrace_predcache
= 0;
302 thread_template
.t_dtrace_vtime
= 0;
303 thread_template
.t_dtrace_tracing
= 0;
304 #endif /* CONFIG_DTRACE */
307 thread_template
.kpc_buf
= NULL
;
311 thread_template
.hv_thread_target
= NULL
;
312 #endif /* HYPERVISOR */
314 thread_template
.t_chud
= 0;
316 #if (DEVELOPMENT || DEBUG)
317 thread_template
.t_page_creation_throttled_hard
= 0;
318 thread_template
.t_page_creation_throttled_soft
= 0;
319 #endif /* DEVELOPMENT || DEBUG */
320 thread_template
.t_page_creation_throttled
= 0;
321 thread_template
.t_page_creation_count
= 0;
322 thread_template
.t_page_creation_time
= 0;
324 thread_template
.affinity_set
= NULL
;
326 thread_template
.syscalls_unix
= 0;
327 thread_template
.syscalls_mach
= 0;
329 thread_template
.t_ledger
= LEDGER_NULL
;
330 thread_template
.t_threadledger
= LEDGER_NULL
;
332 thread_template
.t_bankledger
= LEDGER_NULL
;
333 thread_template
.t_deduct_bank_ledger_time
= 0;
336 thread_template
.requested_policy
= default_task_requested_policy
;
337 thread_template
.effective_policy
= default_task_effective_policy
;
338 thread_template
.pended_policy
= default_task_pended_policy
;
340 bzero(&thread_template
.overrides
, sizeof(thread_template
.overrides
));
342 thread_template
.iotier_override
= THROTTLE_LEVEL_NONE
;
343 thread_template
.thread_io_stats
= NULL
;
344 thread_template
.thread_callout_interrupt_wakeups
= thread_template
.thread_callout_platform_idle_wakeups
= 0;
346 thread_template
.thread_timer_wakeups_bin_1
= thread_template
.thread_timer_wakeups_bin_2
= 0;
347 thread_template
.callout_woken_from_icontext
= thread_template
.callout_woken_from_platform_idle
= 0;
349 thread_template
.thread_tag
= 0;
351 thread_template
.ith_voucher_name
= MACH_PORT_NULL
;
352 thread_template
.ith_voucher
= IPC_VOUCHER_NULL
;
354 thread_template
.work_interval_id
= 0;
356 init_thread
= thread_template
;
357 machine_set_current_thread(&init_thread
);
360 extern boolean_t allow_qos_policy_set
;
366 sizeof(struct thread
),
367 thread_max
* sizeof(struct thread
),
368 THREAD_CHUNK
* sizeof(struct thread
),
371 thread_qos_override_zone
= zinit(
372 sizeof(struct thread_qos_override
),
373 4 * thread_max
* sizeof(struct thread_qos_override
),
375 "thread qos override");
376 zone_change(thread_qos_override_zone
, Z_EXPAND
, TRUE
);
377 zone_change(thread_qos_override_zone
, Z_COLLECT
, TRUE
);
378 zone_change(thread_qos_override_zone
, Z_CALLERACCT
, FALSE
);
379 zone_change(thread_qos_override_zone
, Z_NOENCRYPT
, TRUE
);
381 lck_grp_attr_setdefault(&thread_lck_grp_attr
);
382 lck_grp_init(&thread_lck_grp
, "thread", &thread_lck_grp_attr
);
383 lck_attr_setdefault(&thread_lck_attr
);
388 * Initialize any machine-dependent
389 * per-thread structures necessary.
391 machine_thread_init();
393 if (!PE_parse_boot_argn("cpumon_ustackshots_trigger_pct", &cpumon_ustackshots_trigger_pct
,
394 sizeof (cpumon_ustackshots_trigger_pct
))) {
395 cpumon_ustackshots_trigger_pct
= CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT
;
398 PE_parse_boot_argn("-qos-policy-allow", &allow_qos_policy_set
, sizeof(allow_qos_policy_set
));
400 init_thread_ledgers();
404 thread_terminate_continue(void)
406 panic("thread_terminate_continue");
411 * thread_terminate_self:
414 thread_terminate_self(void)
416 thread_t thread
= current_thread();
421 pal_thread_terminate_self(thread
);
423 DTRACE_PROC(lwp__exit
);
425 thread_mtx_lock(thread
);
427 ipc_thread_disable(thread
);
429 thread_mtx_unlock(thread
);
434 assert_thread_sched_count(thread
);
437 * Cancel priority depression, wait for concurrent expirations
438 * on other processors.
440 if (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) {
441 thread
->sched_flags
&= ~TH_SFLAG_DEPRESSED_MASK
;
443 /* If our priority was low because of a depressed yield, restore it in case we block below */
444 thread_recompute_sched_pri(thread
, FALSE
);
446 if (timer_call_cancel(&thread
->depress_timer
))
447 thread
->depress_timer_active
--;
450 while (thread
->depress_timer_active
> 0) {
451 thread_unlock(thread
);
460 thread_sched_call(thread
, NULL
);
462 thread_unlock(thread
);
466 thread_mtx_lock(thread
);
468 thread_policy_reset(thread
);
470 thread_mtx_unlock(thread
);
473 uthread_cleanup(task
, thread
->uthread
, task
->bsd_info
, thread
->inspection
== 1 ? TRUE
: FALSE
);
474 threadcnt
= hw_atomic_sub(&task
->active_thread_count
, 1);
477 * If we are the last thread to terminate and the task is
478 * associated with a BSD process, perform BSD process exit.
480 if (threadcnt
== 0 && task
->bsd_info
!= NULL
) {
481 proc_exit(task
->bsd_info
);
483 * if there is crash info in task
484 * then do the deliver action since this is
485 * last thread for this task.
487 if (task
->corpse_info
) {
488 task_deliver_crash_notification(task
);
491 uthread_cred_free(thread
->uthread
);
497 * Cancel wait timer, and wait for
498 * concurrent expirations.
500 if (thread
->wait_timer_is_set
) {
501 thread
->wait_timer_is_set
= FALSE
;
503 if (timer_call_cancel(&thread
->wait_timer
))
504 thread
->wait_timer_active
--;
507 while (thread
->wait_timer_active
> 0) {
508 thread_unlock(thread
);
518 * If there is a reserved stack, release it.
520 if (thread
->reserved_stack
!= 0) {
521 stack_free_reserved(thread
);
522 thread
->reserved_stack
= 0;
526 * Mark thread as terminating, and block.
528 thread
->state
|= TH_TERMINATE
;
529 thread_mark_wait_locked(thread
, THREAD_UNINT
);
530 assert((thread
->sched_flags
& TH_SFLAG_PROMOTED
) == 0);
531 assert(thread
->promotions
== 0);
532 assert(!(thread
->sched_flags
& TH_SFLAG_WAITQ_PROMOTED
));
533 assert(thread
->rwlock_count
== 0);
534 thread_unlock(thread
);
537 thread_block((thread_continue_t
)thread_terminate_continue
);
541 /* Drop a thread refcount that definitely isn't the last one. */
543 thread_deallocate_safe(thread_t thread
)
545 if (__improbable(hw_atomic_sub(&(thread
)->ref_count
, 1) == 0))
546 panic("bad thread refcount!");
555 if (thread
== THREAD_NULL
)
558 if (__probable(hw_atomic_sub(&(thread
)->ref_count
, 1) > 0))
561 if(!(thread
->state
& TH_TERMINATE2
))
562 panic("thread_deallocate: thread not properly terminated\n");
564 assert(thread
->runq
== PROCESSOR_NULL
);
567 kpc_thread_destroy(thread
);
570 ipc_thread_terminate(thread
);
572 proc_thread_qos_deallocate(thread
);
578 void *ut
= thread
->uthread
;
580 thread
->uthread
= NULL
;
581 uthread_zone_free(ut
);
583 #endif /* MACH_BSD */
585 if (thread
->t_ledger
)
586 ledger_dereference(thread
->t_ledger
);
587 if (thread
->t_threadledger
)
588 ledger_dereference(thread
->t_threadledger
);
590 if (IPC_VOUCHER_NULL
!= thread
->ith_voucher
)
591 ipc_voucher_release(thread
->ith_voucher
);
593 if (thread
->thread_io_stats
)
594 kfree(thread
->thread_io_stats
, sizeof(struct io_stat_info
));
596 if (thread
->kernel_stack
!= 0)
599 lck_mtx_destroy(&thread
->mutex
, &thread_lck_grp
);
600 machine_thread_destroy(thread
);
602 task_deallocate(task
);
604 zfree(thread_zone
, thread
);
608 * thread_terminate_daemon:
610 * Perform final clean up for terminating threads.
613 thread_terminate_daemon(void)
615 thread_t self
, thread
;
618 self
= current_thread();
619 self
->options
|= TH_OPT_SYSTEM_CRITICAL
;
622 simple_lock(&thread_terminate_lock
);
624 while ((thread
= (thread_t
)dequeue_head(&thread_terminate_queue
)) != THREAD_NULL
) {
627 * if marked for crash reporting, skip reaping.
628 * The corpse delivery thread will clear bit and enqueue
629 * for reaping when done
631 if (thread
->inspection
){
632 enqueue_tail(&crashed_threads_queue
, (queue_entry_t
)thread
);
636 simple_unlock(&thread_terminate_lock
);
639 assert(thread
->SHARE_COUNT
== 0);
640 assert(thread
->BG_COUNT
== 0);
645 task
->total_user_time
+= timer_grab(&thread
->user_timer
);
646 if (thread
->precise_user_kernel_time
) {
647 task
->total_system_time
+= timer_grab(&thread
->system_timer
);
649 task
->total_user_time
+= timer_grab(&thread
->system_timer
);
652 task
->c_switch
+= thread
->c_switch
;
653 task
->p_switch
+= thread
->p_switch
;
654 task
->ps_switch
+= thread
->ps_switch
;
656 task
->syscalls_unix
+= thread
->syscalls_unix
;
657 task
->syscalls_mach
+= thread
->syscalls_mach
;
659 task
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
660 task
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
661 task
->task_gpu_ns
+= ml_gpu_stat(thread
);
663 thread_update_qos_cpu_time(thread
, FALSE
);
664 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
665 task
->thread_count
--;
668 * If the task is being halted, and there is only one thread
669 * left in the task after this one, then wakeup that thread.
671 if (task
->thread_count
== 1 && task
->halting
)
672 thread_wakeup((event_t
)&task
->halting
);
676 lck_mtx_lock(&tasks_threads_lock
);
677 queue_remove(&threads
, thread
, thread_t
, threads
);
679 lck_mtx_unlock(&tasks_threads_lock
);
681 thread_deallocate(thread
);
684 simple_lock(&thread_terminate_lock
);
687 assert_wait((event_t
)&thread_terminate_queue
, THREAD_UNINT
);
688 simple_unlock(&thread_terminate_lock
);
691 self
->options
&= ~TH_OPT_SYSTEM_CRITICAL
;
692 thread_block((thread_continue_t
)thread_terminate_daemon
);
697 * thread_terminate_enqueue:
699 * Enqueue a terminating thread for final disposition.
701 * Called at splsched.
704 thread_terminate_enqueue(
707 KERNEL_DEBUG_CONSTANT(TRACE_DATA_THREAD_TERMINATE
| DBG_FUNC_NONE
, thread
->thread_id
, 0, 0, 0, 0);
709 simple_lock(&thread_terminate_lock
);
710 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)thread
);
711 simple_unlock(&thread_terminate_lock
);
713 thread_wakeup((event_t
)&thread_terminate_queue
);
717 * thread_terminate_crashed_threads:
718 * walk the list of crashed therds and put back set of threads
719 * who are no longer being inspected.
722 thread_terminate_crashed_threads()
724 thread_t th_iter
, th_remove
;
725 boolean_t should_wake_terminate_queue
= FALSE
;
727 simple_lock(&thread_terminate_lock
);
729 * loop through the crashed threads queue
730 * to put any threads that are not being inspected anymore
732 th_iter
= (thread_t
)queue_first(&crashed_threads_queue
);
733 while (!queue_end(&crashed_threads_queue
, (queue_entry_t
)th_iter
)) {
735 th_iter
= (thread_t
)queue_next(&th_iter
->links
);
737 /* make sure current_thread is never in crashed queue */
738 assert(th_remove
!= current_thread());
739 if (th_remove
->inspection
!= TRUE
){
740 remque((queue_entry_t
)th_remove
);
741 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)th_remove
);
742 should_wake_terminate_queue
= TRUE
;
746 simple_unlock(&thread_terminate_lock
);
747 if (should_wake_terminate_queue
== TRUE
) {
748 thread_wakeup((event_t
)&thread_terminate_queue
);
753 * thread_stack_daemon:
755 * Perform stack allocation as required due to
759 thread_stack_daemon(void)
765 simple_lock(&thread_stack_lock
);
767 while ((thread
= (thread_t
)dequeue_head(&thread_stack_queue
)) != THREAD_NULL
) {
768 simple_unlock(&thread_stack_lock
);
771 /* allocate stack with interrupts enabled so that we can call into VM */
774 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_STACK_WAIT
) | DBG_FUNC_END
, thread_tid(thread
), 0, 0, 0, 0);
778 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
779 thread_unlock(thread
);
781 simple_lock(&thread_stack_lock
);
784 assert_wait((event_t
)&thread_stack_queue
, THREAD_UNINT
);
785 simple_unlock(&thread_stack_lock
);
788 thread_block((thread_continue_t
)thread_stack_daemon
);
793 * thread_stack_enqueue:
795 * Enqueue a thread for stack allocation.
797 * Called at splsched.
800 thread_stack_enqueue(
803 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_STACK_WAIT
) | DBG_FUNC_START
, thread_tid(thread
), 0, 0, 0, 0);
805 simple_lock(&thread_stack_lock
);
806 enqueue_tail(&thread_stack_queue
, (queue_entry_t
)thread
);
807 simple_unlock(&thread_stack_lock
);
809 thread_wakeup((event_t
)&thread_stack_queue
);
813 thread_daemon_init(void)
815 kern_return_t result
;
816 thread_t thread
= NULL
;
818 simple_lock_init(&thread_terminate_lock
, 0);
819 queue_init(&thread_terminate_queue
);
820 queue_init(&crashed_threads_queue
);
822 result
= kernel_thread_start_priority((thread_continue_t
)thread_terminate_daemon
, NULL
, MINPRI_KERNEL
, &thread
);
823 if (result
!= KERN_SUCCESS
)
824 panic("thread_daemon_init: thread_terminate_daemon");
826 thread_deallocate(thread
);
828 simple_lock_init(&thread_stack_lock
, 0);
829 queue_init(&thread_stack_queue
);
831 result
= kernel_thread_start_priority((thread_continue_t
)thread_stack_daemon
, NULL
, BASEPRI_PREEMPT
, &thread
);
832 if (result
!= KERN_SUCCESS
)
833 panic("thread_daemon_init: thread_stack_daemon");
835 thread_deallocate(thread
);
838 #define TH_OPTION_NONE 0x00
839 #define TH_OPTION_NOCRED 0x01
840 #define TH_OPTION_NOSUSP 0x02
842 * Create a new thread.
843 * Doesn't start the thread running.
845 * Task and tasks_threads_lock are returned locked on success.
848 thread_create_internal(
851 thread_continue_t continuation
,
853 thread_t
*out_thread
)
856 static thread_t first_thread
;
859 * Allocate a thread and initialize static fields
861 if (first_thread
== THREAD_NULL
)
862 new_thread
= first_thread
= current_thread();
864 new_thread
= (thread_t
)zalloc(thread_zone
);
865 if (new_thread
== THREAD_NULL
)
866 return (KERN_RESOURCE_SHORTAGE
);
868 if (new_thread
!= first_thread
)
869 *new_thread
= thread_template
;
872 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
, (options
& TH_OPTION_NOCRED
) != 0);
873 if (new_thread
->uthread
== NULL
) {
874 zfree(thread_zone
, new_thread
);
875 return (KERN_RESOURCE_SHORTAGE
);
877 #endif /* MACH_BSD */
879 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
881 void *ut
= new_thread
->uthread
;
883 new_thread
->uthread
= NULL
;
884 /* cred free may not be necessary */
885 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
, FALSE
);
886 uthread_cred_free(ut
);
887 uthread_zone_free(ut
);
888 #endif /* MACH_BSD */
890 zfree(thread_zone
, new_thread
);
891 return (KERN_FAILURE
);
894 new_thread
->task
= parent_task
;
896 thread_lock_init(new_thread
);
897 wake_lock_init(new_thread
);
899 lck_mtx_init(&new_thread
->mutex
, &thread_lck_grp
, &thread_lck_attr
);
901 ipc_thread_init(new_thread
);
903 new_thread
->continuation
= continuation
;
905 /* Allocate I/O Statistics structure */
906 new_thread
->thread_io_stats
= (io_stat_info_t
)kalloc(sizeof(struct io_stat_info
));
907 assert(new_thread
->thread_io_stats
!= NULL
);
908 bzero(new_thread
->thread_io_stats
, sizeof(struct io_stat_info
));
911 /* Clear out the I/O Scheduling info for AppleFSCompression */
912 new_thread
->decmp_upl
= NULL
;
913 #endif /* CONFIG_IOSCHED */
915 lck_mtx_lock(&tasks_threads_lock
);
916 task_lock(parent_task
);
918 if ( !parent_task
->active
|| parent_task
->halting
||
919 ((options
& TH_OPTION_NOSUSP
) != 0 &&
920 parent_task
->suspend_count
> 0) ||
921 (parent_task
->thread_count
>= task_threadmax
&&
922 parent_task
!= kernel_task
) ) {
923 task_unlock(parent_task
);
924 lck_mtx_unlock(&tasks_threads_lock
);
928 void *ut
= new_thread
->uthread
;
930 new_thread
->uthread
= NULL
;
931 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
, FALSE
);
932 /* cred free may not be necessary */
933 uthread_cred_free(ut
);
934 uthread_zone_free(ut
);
936 #endif /* MACH_BSD */
937 ipc_thread_disable(new_thread
);
938 ipc_thread_terminate(new_thread
);
939 kfree(new_thread
->thread_io_stats
, sizeof(struct io_stat_info
));
940 lck_mtx_destroy(&new_thread
->mutex
, &thread_lck_grp
);
941 machine_thread_destroy(new_thread
);
942 zfree(thread_zone
, new_thread
);
943 return (KERN_FAILURE
);
946 /* New threads inherit any default state on the task */
947 machine_thread_inherit_taskwide(new_thread
, parent_task
);
949 task_reference_internal(parent_task
);
951 if (new_thread
->task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
953 * This task has a per-thread CPU limit; make sure this new thread
954 * gets its limit set too, before it gets out of the kernel.
956 set_astledger(new_thread
);
959 /* Instantiate a thread ledger. Do not fail thread creation if ledger creation fails. */
960 if ((new_thread
->t_threadledger
= ledger_instantiate(thread_ledger_template
,
961 LEDGER_CREATE_INACTIVE_ENTRIES
)) != LEDGER_NULL
) {
963 ledger_entry_setactive(new_thread
->t_threadledger
, thread_ledgers
.cpu_time
);
966 new_thread
->cpu_time_last_qos
= 0;
968 new_thread
->t_bankledger
= LEDGER_NULL
;
969 new_thread
->t_deduct_bank_ledger_time
= 0;
972 new_thread
->t_ledger
= new_thread
->task
->ledger
;
973 if (new_thread
->t_ledger
)
974 ledger_reference(new_thread
->t_ledger
);
976 #if defined(CONFIG_SCHED_MULTIQ)
977 /* Cache the task's sched_group */
978 new_thread
->sched_group
= parent_task
->sched_group
;
979 #endif /* defined(CONFIG_SCHED_MULTIQ) */
981 /* Cache the task's map */
982 new_thread
->map
= parent_task
->map
;
984 timer_call_setup(&new_thread
->wait_timer
, thread_timer_expire
, new_thread
);
985 timer_call_setup(&new_thread
->depress_timer
, thread_depress_expire
, new_thread
);
988 kpc_thread_create(new_thread
);
991 /* Only need to update policies pushed from task to thread */
992 new_thread
->requested_policy
.bg_iotier
= parent_task
->effective_policy
.bg_iotier
;
993 new_thread
->requested_policy
.terminated
= parent_task
->effective_policy
.terminated
;
995 /* Set the thread's scheduling parameters */
996 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
997 new_thread
->sched_stamp
= sched_tick
;
998 new_thread
->pri_shift
= sched_pri_shift
;
999 #endif /* defined(CONFIG_SCHED_TIMESHARE_CORE) */
1001 new_thread
->sched_mode
= SCHED(initial_thread_sched_mode
)(parent_task
);
1002 new_thread
->sched_flags
= 0;
1003 new_thread
->max_priority
= parent_task
->max_priority
;
1004 new_thread
->task_priority
= parent_task
->priority
;
1006 int new_priority
= (priority
< 0) ? parent_task
->priority
: priority
;
1007 new_priority
= (priority
< 0)? parent_task
->priority
: priority
;
1008 if (new_priority
> new_thread
->max_priority
)
1009 new_priority
= new_thread
->max_priority
;
1011 new_thread
->importance
= new_priority
- new_thread
->task_priority
;
1012 new_thread
->saved_importance
= new_thread
->importance
;
1014 if (parent_task
->max_priority
<= MAXPRI_THROTTLE
) {
1015 sched_set_thread_throttled(new_thread
, TRUE
);
1018 sched_set_thread_base_priority(new_thread
, new_priority
);
1020 thread_policy_create(new_thread
);
1022 /* Chain the thread onto the task's list */
1023 queue_enter(&parent_task
->threads
, new_thread
, thread_t
, task_threads
);
1024 parent_task
->thread_count
++;
1026 /* So terminating threads don't need to take the task lock to decrement */
1027 hw_atomic_add(&parent_task
->active_thread_count
, 1);
1029 /* Protected by the tasks_threads_lock */
1030 new_thread
->thread_id
= ++thread_unique_id
;
1032 queue_enter(&threads
, new_thread
, thread_t
, threads
);
1035 new_thread
->active
= TRUE
;
1036 new_thread
->inspection
= FALSE
;
1037 *out_thread
= new_thread
;
1040 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
1042 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
1044 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1045 TRACEDBG_CODE(DBG_TRACE_DATA
, 1) | DBG_FUNC_NONE
,
1046 (vm_address_t
)(uintptr_t)thread_tid(new_thread
), dbg_arg2
, 0, 0, 0);
1048 kdbg_trace_string(parent_task
->bsd_info
,
1049 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
1051 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
1052 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
1053 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
1056 DTRACE_PROC1(lwp__create
, thread_t
, *out_thread
);
1058 return (KERN_SUCCESS
);
1061 static kern_return_t
1062 thread_create_internal2(
1064 thread_t
*new_thread
,
1065 boolean_t from_user
,
1066 thread_continue_t continuation
)
1068 kern_return_t result
;
1071 if (task
== TASK_NULL
|| task
== kernel_task
)
1072 return (KERN_INVALID_ARGUMENT
);
1074 result
= thread_create_internal(task
, -1, continuation
, TH_OPTION_NONE
, &thread
);
1075 if (result
!= KERN_SUCCESS
)
1078 thread
->user_stop_count
= 1;
1079 thread_hold(thread
);
1080 if (task
->suspend_count
> 0)
1081 thread_hold(thread
);
1084 extmod_statistics_incr_thread_create(task
);
1087 lck_mtx_unlock(&tasks_threads_lock
);
1089 *new_thread
= thread
;
1091 return (KERN_SUCCESS
);
1094 /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
1098 thread_t
*new_thread
);
1103 thread_t
*new_thread
)
1105 return thread_create_internal2(task
, new_thread
, FALSE
, (thread_continue_t
)thread_bootstrap_return
);
1109 thread_create_from_user(
1111 thread_t
*new_thread
)
1113 return thread_create_internal2(task
, new_thread
, TRUE
, (thread_continue_t
)thread_bootstrap_return
);
1117 thread_create_with_continuation(
1119 thread_t
*new_thread
,
1120 thread_continue_t continuation
)
1122 return thread_create_internal2(task
, new_thread
, FALSE
, continuation
);
1125 static kern_return_t
1126 thread_create_running_internal2(
1127 register task_t task
,
1129 thread_state_t new_state
,
1130 mach_msg_type_number_t new_state_count
,
1131 thread_t
*new_thread
,
1132 boolean_t from_user
)
1134 register kern_return_t result
;
1137 if (task
== TASK_NULL
|| task
== kernel_task
)
1138 return (KERN_INVALID_ARGUMENT
);
1140 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
1141 if (result
!= KERN_SUCCESS
)
1144 result
= machine_thread_set_state(thread
, flavor
, new_state
, new_state_count
);
1145 if (result
!= KERN_SUCCESS
) {
1147 lck_mtx_unlock(&tasks_threads_lock
);
1149 thread_terminate(thread
);
1150 thread_deallocate(thread
);
1154 thread_mtx_lock(thread
);
1155 thread_start_internal(thread
);
1156 thread_mtx_unlock(thread
);
1159 extmod_statistics_incr_thread_create(task
);
1162 lck_mtx_unlock(&tasks_threads_lock
);
1164 *new_thread
= thread
;
1169 /* Prototype, see justification above */
1171 thread_create_running(
1172 register task_t task
,
1174 thread_state_t new_state
,
1175 mach_msg_type_number_t new_state_count
,
1176 thread_t
*new_thread
);
1179 thread_create_running(
1180 register task_t task
,
1182 thread_state_t new_state
,
1183 mach_msg_type_number_t new_state_count
,
1184 thread_t
*new_thread
)
1186 return thread_create_running_internal2(
1187 task
, flavor
, new_state
, new_state_count
,
1192 thread_create_running_from_user(
1193 register task_t task
,
1195 thread_state_t new_state
,
1196 mach_msg_type_number_t new_state_count
,
1197 thread_t
*new_thread
)
1199 return thread_create_running_internal2(
1200 task
, flavor
, new_state
, new_state_count
,
1205 thread_create_workq(
1207 thread_continue_t thread_return
,
1208 thread_t
*new_thread
)
1210 kern_return_t result
;
1213 if (task
== TASK_NULL
|| task
== kernel_task
)
1214 return (KERN_INVALID_ARGUMENT
);
1216 result
= thread_create_internal(task
, -1, thread_return
, TH_OPTION_NOCRED
| TH_OPTION_NOSUSP
, &thread
);
1217 if (result
!= KERN_SUCCESS
)
1220 thread
->user_stop_count
= 1;
1221 thread_hold(thread
);
1222 if (task
->suspend_count
> 0)
1223 thread_hold(thread
);
1226 lck_mtx_unlock(&tasks_threads_lock
);
1228 *new_thread
= thread
;
1230 return (KERN_SUCCESS
);
1234 * kernel_thread_create:
1236 * Create a thread in the kernel task
1237 * to execute in kernel context.
1240 kernel_thread_create(
1241 thread_continue_t continuation
,
1244 thread_t
*new_thread
)
1246 kern_return_t result
;
1248 task_t task
= kernel_task
;
1250 result
= thread_create_internal(task
, priority
, continuation
, TH_OPTION_NONE
, &thread
);
1251 if (result
!= KERN_SUCCESS
)
1255 lck_mtx_unlock(&tasks_threads_lock
);
1257 stack_alloc(thread
);
1258 assert(thread
->kernel_stack
!= 0);
1259 thread
->reserved_stack
= thread
->kernel_stack
;
1261 thread
->parameter
= parameter
;
1264 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread
, continuation
);
1265 *new_thread
= thread
;
1271 kernel_thread_start_priority(
1272 thread_continue_t continuation
,
1275 thread_t
*new_thread
)
1277 kern_return_t result
;
1280 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
1281 if (result
!= KERN_SUCCESS
)
1284 *new_thread
= thread
;
1286 thread_mtx_lock(thread
);
1287 thread_start_internal(thread
);
1288 thread_mtx_unlock(thread
);
1294 kernel_thread_start(
1295 thread_continue_t continuation
,
1297 thread_t
*new_thread
)
1299 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
1302 /* Separated into helper function so it can be used by THREAD_BASIC_INFO and THREAD_EXTENDED_INFO */
1303 /* it is assumed that the thread is locked by the caller */
1305 retrieve_thread_basic_info(thread_t thread
, thread_basic_info_t basic_info
)
1311 thread_read_times(thread
, &basic_info
->user_time
,
1312 &basic_info
->system_time
);
1315 * Update lazy-evaluated scheduler info because someone wants it.
1317 if (SCHED(can_update_priority
)(thread
))
1318 SCHED(update_priority
)(thread
);
1320 basic_info
->sleep_time
= 0;
1323 * To calculate cpu_usage, first correct for timer rate,
1324 * then for 5/8 ageing. The correction factor [3/5] is
1327 basic_info
->cpu_usage
= 0;
1328 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
1329 if (sched_tick_interval
) {
1330 basic_info
->cpu_usage
= (integer_t
)(((uint64_t)thread
->cpu_usage
1331 * TH_USAGE_SCALE
) / sched_tick_interval
);
1332 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
1336 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
)
1337 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
1339 basic_info
->policy
= ((thread
->sched_mode
== TH_MODE_TIMESHARE
)?
1340 POLICY_TIMESHARE
: POLICY_RR
);
1343 if (thread
->options
& TH_OPT_IDLE_THREAD
)
1344 flags
|= TH_FLAGS_IDLE
;
1346 if (thread
->options
& TH_OPT_GLOBAL_FORCED_IDLE
) {
1347 flags
|= TH_FLAGS_GLOBAL_FORCED_IDLE
;
1350 if (!thread
->kernel_stack
)
1351 flags
|= TH_FLAGS_SWAPPED
;
1354 if (thread
->state
& TH_TERMINATE
)
1355 state
= TH_STATE_HALTED
;
1357 if (thread
->state
& TH_RUN
)
1358 state
= TH_STATE_RUNNING
;
1360 if (thread
->state
& TH_UNINT
)
1361 state
= TH_STATE_UNINTERRUPTIBLE
;
1363 if (thread
->state
& TH_SUSP
)
1364 state
= TH_STATE_STOPPED
;
1366 if (thread
->state
& TH_WAIT
)
1367 state
= TH_STATE_WAITING
;
1369 basic_info
->run_state
= state
;
1370 basic_info
->flags
= flags
;
1372 basic_info
->suspend_count
= thread
->user_stop_count
;
1378 thread_info_internal(
1379 register thread_t thread
,
1380 thread_flavor_t flavor
,
1381 thread_info_t thread_info_out
, /* ptr to OUT array */
1382 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
1386 if (thread
== THREAD_NULL
)
1387 return (KERN_INVALID_ARGUMENT
);
1389 if (flavor
== THREAD_BASIC_INFO
) {
1391 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
1392 return (KERN_INVALID_ARGUMENT
);
1395 thread_lock(thread
);
1397 retrieve_thread_basic_info(thread
, (thread_basic_info_t
) thread_info_out
);
1399 thread_unlock(thread
);
1402 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
1404 return (KERN_SUCCESS
);
1407 if (flavor
== THREAD_IDENTIFIER_INFO
) {
1408 register thread_identifier_info_t identifier_info
;
1410 if (*thread_info_count
< THREAD_IDENTIFIER_INFO_COUNT
)
1411 return (KERN_INVALID_ARGUMENT
);
1413 identifier_info
= (thread_identifier_info_t
) thread_info_out
;
1416 thread_lock(thread
);
1418 identifier_info
->thread_id
= thread
->thread_id
;
1419 identifier_info
->thread_handle
= thread
->machine
.cthread_self
;
1420 identifier_info
->dispatch_qaddr
= thread_dispatchqaddr(thread
);
1422 thread_unlock(thread
);
1424 return KERN_SUCCESS
;
1427 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
1428 policy_timeshare_info_t ts_info
;
1430 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
1431 return (KERN_INVALID_ARGUMENT
);
1433 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
1436 thread_lock(thread
);
1438 if (thread
->sched_mode
!= TH_MODE_TIMESHARE
) {
1439 thread_unlock(thread
);
1441 return (KERN_INVALID_POLICY
);
1444 ts_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1445 if (ts_info
->depressed
) {
1446 ts_info
->base_priority
= DEPRESSPRI
;
1447 ts_info
->depress_priority
= thread
->base_pri
;
1450 ts_info
->base_priority
= thread
->base_pri
;
1451 ts_info
->depress_priority
= -1;
1454 ts_info
->cur_priority
= thread
->sched_pri
;
1455 ts_info
->max_priority
= thread
->max_priority
;
1457 thread_unlock(thread
);
1460 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
1462 return (KERN_SUCCESS
);
1465 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
1466 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
1467 return (KERN_INVALID_ARGUMENT
);
1469 return (KERN_INVALID_POLICY
);
1472 if (flavor
== THREAD_SCHED_RR_INFO
) {
1473 policy_rr_info_t rr_info
;
1474 uint32_t quantum_time
;
1475 uint64_t quantum_ns
;
1477 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
1478 return (KERN_INVALID_ARGUMENT
);
1480 rr_info
= (policy_rr_info_t
) thread_info_out
;
1483 thread_lock(thread
);
1485 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
1486 thread_unlock(thread
);
1489 return (KERN_INVALID_POLICY
);
1492 rr_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1493 if (rr_info
->depressed
) {
1494 rr_info
->base_priority
= DEPRESSPRI
;
1495 rr_info
->depress_priority
= thread
->base_pri
;
1498 rr_info
->base_priority
= thread
->base_pri
;
1499 rr_info
->depress_priority
= -1;
1502 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
1503 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
1505 rr_info
->max_priority
= thread
->max_priority
;
1506 rr_info
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
1508 thread_unlock(thread
);
1511 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1513 return (KERN_SUCCESS
);
1516 if (flavor
== THREAD_EXTENDED_INFO
) {
1517 thread_basic_info_data_t basic_info
;
1518 thread_extended_info_t extended_info
= (thread_extended_info_t
) thread_info_out
;
1520 if (*thread_info_count
< THREAD_EXTENDED_INFO_COUNT
) {
1521 return (KERN_INVALID_ARGUMENT
);
1525 thread_lock(thread
);
1527 /* NOTE: This mimics fill_taskthreadinfo(), which is the function used by proc_pidinfo() for
1528 * the PROC_PIDTHREADINFO flavor (which can't be used on corpses)
1530 retrieve_thread_basic_info(thread
, &basic_info
);
1531 extended_info
->pth_user_time
= ((basic_info
.user_time
.seconds
* (integer_t
)NSEC_PER_SEC
) + (basic_info
.user_time
.microseconds
* (integer_t
)NSEC_PER_USEC
));
1532 extended_info
->pth_system_time
= ((basic_info
.system_time
.seconds
* (integer_t
)NSEC_PER_SEC
) + (basic_info
.system_time
.microseconds
* (integer_t
)NSEC_PER_USEC
));
1534 extended_info
->pth_cpu_usage
= basic_info
.cpu_usage
;
1535 extended_info
->pth_policy
= basic_info
.policy
;
1536 extended_info
->pth_run_state
= basic_info
.run_state
;
1537 extended_info
->pth_flags
= basic_info
.flags
;
1538 extended_info
->pth_sleep_time
= basic_info
.sleep_time
;
1539 extended_info
->pth_curpri
= thread
->sched_pri
;
1540 extended_info
->pth_priority
= thread
->base_pri
;
1541 extended_info
->pth_maxpriority
= thread
->max_priority
;
1543 bsd_getthreadname(thread
->uthread
,extended_info
->pth_name
);
1545 thread_unlock(thread
);
1548 *thread_info_count
= THREAD_EXTENDED_INFO_COUNT
;
1550 return (KERN_SUCCESS
);
1553 if (flavor
== THREAD_DEBUG_INFO_INTERNAL
) {
1554 #if DEVELOPMENT || DEBUG
1555 thread_debug_info_internal_t dbg_info
;
1556 if (*thread_info_count
< THREAD_DEBUG_INFO_INTERNAL_COUNT
)
1557 return (KERN_NOT_SUPPORTED
);
1559 if (thread_info_out
== NULL
)
1560 return (KERN_INVALID_ARGUMENT
);
1562 dbg_info
= (thread_debug_info_internal_t
) thread_info_out
;
1563 dbg_info
->page_creation_count
= thread
->t_page_creation_count
;
1565 *thread_info_count
= THREAD_DEBUG_INFO_INTERNAL_COUNT
;
1566 return (KERN_SUCCESS
);
1567 #endif /* DEVELOPMENT || DEBUG */
1568 return (KERN_NOT_SUPPORTED
);
1571 return (KERN_INVALID_ARGUMENT
);
1577 time_value_t
*user_time
,
1578 time_value_t
*system_time
)
1582 uint64_t tval_user
, tval_system
;
1584 tval_user
= timer_grab(&thread
->user_timer
);
1585 tval_system
= timer_grab(&thread
->system_timer
);
1587 if (thread
->precise_user_kernel_time
) {
1588 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1589 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1590 user_time
->microseconds
= usecs
;
1592 absolutetime_to_microtime(tval_system
, &secs
, &usecs
);
1593 system_time
->seconds
= (typeof(system_time
->seconds
))secs
;
1594 system_time
->microseconds
= usecs
;
1596 /* system_timer may represent either sys or user */
1597 tval_user
+= tval_system
;
1598 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
1599 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
1600 user_time
->microseconds
= usecs
;
1602 system_time
->seconds
= 0;
1603 system_time
->microseconds
= 0;
1607 uint64_t thread_get_runtime_self(void)
1609 boolean_t interrupt_state
;
1611 thread_t thread
= NULL
;
1612 processor_t processor
= NULL
;
1614 thread
= current_thread();
1616 /* Not interrupt safe, as the scheduler may otherwise update timer values underneath us */
1617 interrupt_state
= ml_set_interrupts_enabled(FALSE
);
1618 processor
= current_processor();
1619 timer_switch(PROCESSOR_DATA(processor
, thread_timer
), mach_absolute_time(), PROCESSOR_DATA(processor
, thread_timer
));
1620 runtime
= (timer_grab(&thread
->user_timer
) + timer_grab(&thread
->system_timer
));
1621 ml_set_interrupts_enabled(interrupt_state
);
1628 __unused thread_t thread
,
1629 __unused processor_set_t new_pset
)
1631 return (KERN_FAILURE
);
1635 * thread_assign_default:
1637 * Special version of thread_assign for assigning threads to default
1641 thread_assign_default(
1644 return (thread_assign(thread
, &pset0
));
1648 * thread_get_assignment
1650 * Return current assignment for this thread.
1653 thread_get_assignment(
1655 processor_set_t
*pset
)
1658 return (KERN_INVALID_ARGUMENT
);
1662 return (KERN_SUCCESS
);
1666 * thread_wire_internal:
1668 * Specify that the target thread must always be able
1669 * to run and to allocate memory.
1672 thread_wire_internal(
1673 host_priv_t host_priv
,
1676 boolean_t
*prev_state
)
1678 if (host_priv
== NULL
|| thread
!= current_thread())
1679 return (KERN_INVALID_ARGUMENT
);
1681 assert(host_priv
== &realhost
);
1684 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
1687 if (!(thread
->options
& TH_OPT_VMPRIV
))
1688 vm_page_free_reserve(1); /* XXX */
1689 thread
->options
|= TH_OPT_VMPRIV
;
1692 if (thread
->options
& TH_OPT_VMPRIV
)
1693 vm_page_free_reserve(-1); /* XXX */
1694 thread
->options
&= ~TH_OPT_VMPRIV
;
1697 return (KERN_SUCCESS
);
1704 * User-api wrapper for thread_wire_internal()
1708 host_priv_t host_priv
,
1712 return (thread_wire_internal(host_priv
, thread
, wired
, NULL
));
1717 set_vm_privilege(boolean_t privileged
)
1719 boolean_t was_vmpriv
;
1721 if (current_thread()->options
& TH_OPT_VMPRIV
)
1726 if (privileged
!= FALSE
)
1727 current_thread()->options
|= TH_OPT_VMPRIV
;
1729 current_thread()->options
&= ~TH_OPT_VMPRIV
;
1731 return (was_vmpriv
);
1736 * XXX assuming current thread only, for now...
1739 thread_guard_violation(thread_t thread
, unsigned type
)
1741 assert(thread
== current_thread());
1743 spl_t s
= splsched();
1745 * Use the saved state area of the thread structure
1746 * to store all info required to handle the AST when
1747 * returning to userspace
1749 thread
->guard_exc_info
.type
= type
;
1750 thread_ast_set(thread
, AST_GUARD
);
1751 ast_propagate(thread
->ast
);
1759 * Handle AST_GUARD for a thread. This routine looks at the
1760 * state saved in the thread structure to determine the cause
1761 * of this exception. Based on this value, it invokes the
1762 * appropriate routine which determines other exception related
1763 * info and raises the exception.
1766 guard_ast(thread_t thread
)
1768 if (thread
->guard_exc_info
.type
== GUARD_TYPE_MACH_PORT
)
1769 mach_port_guard_ast(thread
);
1771 fd_guard_ast(thread
);
1775 thread_cputime_callback(int warning
, __unused
const void *arg0
, __unused
const void *arg1
)
1777 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
1778 #if CONFIG_TELEMETRY
1780 * This thread is in danger of violating the CPU usage monitor. Enable telemetry
1781 * on the entire task so there are micro-stackshots available if and when
1782 * EXC_RESOURCE is triggered. We could have chosen to enable micro-stackshots
1783 * for this thread only; but now that this task is suspect, knowing what all of
1784 * its threads are up to will be useful.
1786 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 1);
1791 #if CONFIG_TELEMETRY
1793 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
1794 * exceeded the limit, turn telemetry off for the task.
1796 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 0);
1800 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE();
1804 void __attribute__((noinline
))
1805 THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU__SENDING_EXC_RESOURCE(void)
1808 task_t task
= current_task();
1809 thread_t thread
= current_thread();
1810 uint64_t tid
= thread
->thread_id
;
1811 const char *procname
= "unknown";
1812 time_value_t thread_total_time
= {0, 0};
1813 time_value_t thread_system_time
;
1814 time_value_t thread_user_time
;
1817 uint32_t limit_percent
;
1818 uint32_t usage_percent
;
1819 uint32_t interval_sec
;
1820 uint64_t interval_ns
;
1821 uint64_t balance_ns
;
1822 boolean_t fatal
= FALSE
;
1824 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
1825 struct ledger_entry_info lei
;
1827 assert(thread
->t_threadledger
!= LEDGER_NULL
);
1830 * Now that a thread has tripped the monitor, disable it for the entire task.
1833 if ((task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) == 0) {
1835 * The CPU usage monitor has been disabled on our task, so some other
1836 * thread must have gotten here first. We only send one exception per
1837 * task lifetime, so there's nothing left for us to do here.
1842 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_CPUMON
) {
1845 task_disable_cpumon(task
);
1849 pid
= proc_selfpid();
1850 if (task
->bsd_info
!= NULL
)
1851 procname
= proc_name_address(task
->bsd_info
);
1854 thread_get_cpulimit(&action
, &percentage
, &interval_ns
);
1856 interval_sec
= (uint32_t)(interval_ns
/ NSEC_PER_SEC
);
1858 thread_read_times(thread
, &thread_user_time
, &thread_system_time
);
1859 time_value_add(&thread_total_time
, &thread_user_time
);
1860 time_value_add(&thread_total_time
, &thread_system_time
);
1862 ledger_get_entry_info(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &lei
);
1864 absolutetime_to_nanoseconds(lei
.lei_balance
, &balance_ns
);
1865 usage_percent
= (uint32_t) ((balance_ns
* 100ULL) / lei
.lei_last_refill
);
1867 /* Show refill period in the same units as balance, limit, etc */
1868 nanoseconds_to_absolutetime(lei
.lei_refill_period
, &lei
.lei_refill_period
);
1870 limit_percent
= (uint32_t) ((lei
.lei_limit
* 100ULL) / lei
.lei_refill_period
);
1872 /* TODO: show task total runtime as well? see TASK_ABSOLUTETIME_INFO */
1874 if (disable_exc_resource
) {
1875 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1876 "supressed by a boot-arg\n", procname
, pid
, tid
);
1881 printf("process %s[%d] thread %llu caught burning CPU!; EXC_RESOURCE "
1882 "supressed due to audio playback\n", procname
, pid
, tid
);
1885 printf("process %s[%d] thread %llu caught burning CPU! "
1886 "It used more than %d%% CPU (Actual recent usage: %d%%) over %d seconds. "
1887 "thread lifetime cpu usage %d.%06d seconds, (%d.%06d user, %d.%06d system) "
1888 "ledger info: balance: %lld credit: %lld debit: %lld limit: %llu (%d%%) "
1889 "period: %llu time since last refill (ns): %llu %s\n",
1891 percentage
, usage_percent
, interval_sec
,
1892 thread_total_time
.seconds
, thread_total_time
.microseconds
,
1893 thread_user_time
.seconds
, thread_user_time
.microseconds
,
1894 thread_system_time
.seconds
, thread_system_time
.microseconds
,
1896 lei
.lei_credit
, lei
.lei_debit
,
1897 lei
.lei_limit
, limit_percent
,
1898 lei
.lei_refill_period
, lei
.lei_last_refill
,
1899 (fatal
? "[fatal violation]" : ""));
1902 code
[0] = code
[1] = 0;
1903 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_CPU
);
1905 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_CPU_MONITOR_FATAL
);
1907 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_CPU_MONITOR
);
1909 EXC_RESOURCE_CPUMONITOR_ENCODE_INTERVAL(code
[0], interval_sec
);
1910 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[0], limit_percent
);
1911 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[1], usage_percent
);
1912 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
1916 jetsam_on_ledger_cpulimit_exceeded();
1918 task_terminate_internal(task
);
1923 #define UPDATE_IO_STATS(info, size) \
1926 info.size += size; \
1929 #define UPDATE_IO_STATS_ATOMIC(info, size) \
1931 OSIncrementAtomic64((SInt64 *)&(info.count)); \
1932 OSAddAtomic64(size, (SInt64 *)&(info.size)); \
1935 void thread_update_io_stats(thread_t thread
, int size
, int io_flags
)
1939 if (thread
->thread_io_stats
== NULL
|| thread
->task
->task_io_stats
== NULL
)
1942 if (io_flags
& DKIO_READ
) {
1943 UPDATE_IO_STATS(thread
->thread_io_stats
->disk_reads
, size
);
1944 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->disk_reads
, size
);
1947 if (io_flags
& DKIO_META
) {
1948 UPDATE_IO_STATS(thread
->thread_io_stats
->metadata
, size
);
1949 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->metadata
, size
);
1952 if (io_flags
& DKIO_PAGING
) {
1953 UPDATE_IO_STATS(thread
->thread_io_stats
->paging
, size
);
1954 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->paging
, size
);
1957 io_tier
= ((io_flags
& DKIO_TIER_MASK
) >> DKIO_TIER_SHIFT
);
1958 assert (io_tier
< IO_NUM_PRIORITIES
);
1960 UPDATE_IO_STATS(thread
->thread_io_stats
->io_priority
[io_tier
], size
);
1961 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->io_priority
[io_tier
], size
);
1963 /* Update Total I/O Counts */
1964 UPDATE_IO_STATS(thread
->thread_io_stats
->total_io
, size
);
1965 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->total_io
, size
);
1970 init_thread_ledgers(void) {
1971 ledger_template_t t
;
1974 assert(thread_ledger_template
== NULL
);
1976 if ((t
= ledger_template_create("Per-thread ledger")) == NULL
)
1977 panic("couldn't create thread ledger template");
1979 if ((idx
= ledger_entry_add(t
, "cpu_time", "sched", "ns")) < 0) {
1980 panic("couldn't create cpu_time entry for thread ledger template");
1983 if (ledger_set_callback(t
, idx
, thread_cputime_callback
, NULL
, NULL
) < 0) {
1984 panic("couldn't set thread ledger callback for cpu_time entry");
1987 thread_ledgers
.cpu_time
= idx
;
1989 thread_ledger_template
= t
;
1993 * Returns currently applied CPU usage limit, or 0/0 if none is applied.
1996 thread_get_cpulimit(int *action
, uint8_t *percentage
, uint64_t *interval_ns
)
1998 int64_t abstime
= 0;
1999 uint64_t limittime
= 0;
2000 thread_t thread
= current_thread();
2006 if (thread
->t_threadledger
== LEDGER_NULL
) {
2008 * This thread has no per-thread ledger, so it can't possibly
2009 * have a CPU limit applied.
2011 return (KERN_SUCCESS
);
2014 ledger_get_period(thread
->t_threadledger
, thread_ledgers
.cpu_time
, interval_ns
);
2015 ledger_get_limit(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &abstime
);
2017 if ((abstime
== LEDGER_LIMIT_INFINITY
) || (*interval_ns
== 0)) {
2019 * This thread's CPU time ledger has no period or limit; so it
2020 * doesn't have a CPU limit applied.
2022 return (KERN_SUCCESS
);
2026 * This calculation is the converse to the one in thread_set_cpulimit().
2028 absolutetime_to_nanoseconds(abstime
, &limittime
);
2029 *percentage
= (limittime
* 100ULL) / *interval_ns
;
2030 assert(*percentage
<= 100);
2032 if (thread
->options
& TH_OPT_PROC_CPULIMIT
) {
2033 assert((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0);
2035 *action
= THREAD_CPULIMIT_BLOCK
;
2036 } else if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
2037 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0);
2039 *action
= THREAD_CPULIMIT_EXCEPTION
;
2041 *action
= THREAD_CPULIMIT_DISABLE
;
2044 return (KERN_SUCCESS
);
2048 * Set CPU usage limit on a thread.
2050 * Calling with percentage of 0 will unset the limit for this thread.
2053 thread_set_cpulimit(int action
, uint8_t percentage
, uint64_t interval_ns
)
2055 thread_t thread
= current_thread();
2057 uint64_t limittime
= 0;
2058 uint64_t abstime
= 0;
2060 assert(percentage
<= 100);
2062 if (action
== THREAD_CPULIMIT_DISABLE
) {
2064 * Remove CPU limit, if any exists.
2066 if (thread
->t_threadledger
!= LEDGER_NULL
) {
2067 l
= thread
->t_threadledger
;
2068 ledger_set_limit(l
, thread_ledgers
.cpu_time
, LEDGER_LIMIT_INFINITY
, 0);
2069 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_IGNORE
);
2070 thread
->options
&= ~(TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
);
2076 if (interval_ns
< MINIMUM_CPULIMIT_INTERVAL_MS
* NSEC_PER_MSEC
) {
2077 return (KERN_INVALID_ARGUMENT
);
2080 l
= thread
->t_threadledger
;
2081 if (l
== LEDGER_NULL
) {
2083 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
2085 if ((l
= ledger_instantiate(thread_ledger_template
, LEDGER_CREATE_INACTIVE_ENTRIES
)) == LEDGER_NULL
)
2086 return (KERN_RESOURCE_SHORTAGE
);
2089 * We are the first to create this thread's ledger, so only activate our entry.
2091 ledger_entry_setactive(l
, thread_ledgers
.cpu_time
);
2092 thread
->t_threadledger
= l
;
2096 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
2097 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
2099 limittime
= (interval_ns
* percentage
) / 100;
2100 nanoseconds_to_absolutetime(limittime
, &abstime
);
2101 ledger_set_limit(l
, thread_ledgers
.cpu_time
, abstime
, cpumon_ustackshots_trigger_pct
);
2103 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
2105 ledger_set_period(l
, thread_ledgers
.cpu_time
, interval_ns
);
2107 if (action
== THREAD_CPULIMIT_EXCEPTION
) {
2109 * We don't support programming the CPU usage monitor on a task if any of its
2110 * threads have a per-thread blocking CPU limit configured.
2112 if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
2113 panic("CPU usage monitor activated, but blocking thread limit exists");
2117 * Make a note that this thread's CPU limit is being used for the task-wide CPU
2118 * usage monitor. We don't have to arm the callback which will trigger the
2119 * exception, because that was done for us in ledger_instantiate (because the
2120 * ledger template used has a default callback).
2122 thread
->options
|= TH_OPT_PROC_CPULIMIT
;
2125 * We deliberately override any CPU limit imposed by a task-wide limit (eg
2126 * CPU usage monitor).
2128 thread
->options
&= ~TH_OPT_PROC_CPULIMIT
;
2130 thread
->options
|= TH_OPT_PRVT_CPULIMIT
;
2131 /* The per-thread ledger template by default has a callback for CPU time */
2132 ledger_disable_callback(l
, thread_ledgers
.cpu_time
);
2133 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_BLOCK
);
2142 __unused thread_t thread
)
2152 thread
->sched_call
= (call
!= NULL
)? call
: sched_call_null
;
2156 thread_static_param(
2160 thread_mtx_lock(thread
);
2161 thread
->static_param
= state
;
2162 thread_mtx_unlock(thread
);
2169 return (thread
!= THREAD_NULL
? thread
->thread_id
: 0);
2172 uint16_t thread_set_tag(thread_t th
, uint16_t tag
) {
2173 return thread_set_tag_internal(th
, tag
);
2175 uint16_t thread_get_tag(thread_t th
) {
2176 return thread_get_tag_internal(th
);
2180 thread_dispatchqaddr(
2183 uint64_t dispatchqueue_addr
= 0;
2184 uint64_t thread_handle
= 0;
2186 if (thread
!= THREAD_NULL
) {
2187 thread_handle
= thread
->machine
.cthread_self
;
2189 if (thread
->inspection
== TRUE
)
2190 dispatchqueue_addr
= thread_handle
+ get_task_dispatchqueue_offset(thread
->task
);
2191 else if (thread
->task
->bsd_info
)
2192 dispatchqueue_addr
= thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
2195 return (dispatchqueue_addr
);
2199 * Export routines to other components for things that are done as macros
2200 * within the osfmk component.
2203 #undef thread_reference
2204 void thread_reference(thread_t thread
);
2209 if (thread
!= THREAD_NULL
)
2210 thread_reference_internal(thread
);
2213 #undef thread_should_halt
2219 return (thread_should_halt_fast(th
));
2223 * thread_set_voucher_name - reset the voucher port name bound to this thread
2225 * Conditions: nothing locked
2227 * If we already converted the previous name to a cached voucher
2228 * reference, then we discard that reference here. The next lookup
2229 * will cache it again.
2233 thread_set_voucher_name(mach_port_name_t voucher_name
)
2235 thread_t thread
= current_thread();
2236 ipc_voucher_t new_voucher
= IPC_VOUCHER_NULL
;
2237 ipc_voucher_t voucher
;
2239 ledger_t bankledger
= NULL
;
2242 if (MACH_PORT_DEAD
== voucher_name
)
2243 return KERN_INVALID_RIGHT
;
2246 * agressively convert to voucher reference
2248 if (MACH_PORT_VALID(voucher_name
)) {
2249 new_voucher
= convert_port_name_to_voucher(voucher_name
);
2250 if (IPC_VOUCHER_NULL
== new_voucher
)
2251 return KERN_INVALID_ARGUMENT
;
2254 bankledger
= bank_get_voucher_ledger(new_voucher
);
2257 thread_mtx_lock(thread
);
2258 voucher
= thread
->ith_voucher
;
2259 thread
->ith_voucher_name
= voucher_name
;
2260 thread
->ith_voucher
= new_voucher
;
2262 bank_swap_thread_bank_ledger(thread
, bankledger
);
2264 thread_mtx_unlock(thread
);
2266 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2267 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2268 (uintptr_t)thread_tid(thread
),
2269 (uintptr_t)voucher_name
,
2270 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher
),
2273 if (IPC_VOUCHER_NULL
!= voucher
)
2274 ipc_voucher_release(voucher
);
2276 return KERN_SUCCESS
;
2280 * thread_get_mach_voucher - return a voucher reference for the specified thread voucher
2282 * Conditions: nothing locked
2284 * A reference to the voucher may be lazily pending, if someone set the voucher name
2285 * but nobody has done a lookup yet. In that case, we'll have to do the equivalent
2288 * NOTE: At the moment, there is no distinction between the current and effective
2289 * vouchers because we only set them at the thread level currently.
2292 thread_get_mach_voucher(
2293 thread_act_t thread
,
2294 mach_voucher_selector_t __unused which
,
2295 ipc_voucher_t
*voucherp
)
2297 ipc_voucher_t voucher
;
2298 mach_port_name_t voucher_name
;
2300 if (THREAD_NULL
== thread
)
2301 return KERN_INVALID_ARGUMENT
;
2303 thread_mtx_lock(thread
);
2304 voucher
= thread
->ith_voucher
;
2306 /* if already cached, just return a ref */
2307 if (IPC_VOUCHER_NULL
!= voucher
) {
2308 ipc_voucher_reference(voucher
);
2309 thread_mtx_unlock(thread
);
2310 *voucherp
= voucher
;
2311 return KERN_SUCCESS
;
2314 voucher_name
= thread
->ith_voucher_name
;
2316 /* convert the name to a port, then voucher reference */
2317 if (MACH_PORT_VALID(voucher_name
)) {
2321 ipc_object_copyin(thread
->task
->itk_space
, voucher_name
,
2322 MACH_MSG_TYPE_COPY_SEND
, (ipc_object_t
*)&port
)) {
2323 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2324 thread_mtx_unlock(thread
);
2325 *voucherp
= IPC_VOUCHER_NULL
;
2326 return KERN_SUCCESS
;
2329 /* convert to a voucher ref to return, and cache a ref on thread */
2330 voucher
= convert_port_to_voucher(port
);
2331 ipc_voucher_reference(voucher
);
2332 thread
->ith_voucher
= voucher
;
2333 thread_mtx_unlock(thread
);
2335 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2336 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2337 (uintptr_t)thread_tid(thread
),
2339 VM_KERNEL_ADDRPERM((uintptr_t)voucher
),
2343 ipc_port_release_send(port
);
2345 thread_mtx_unlock(thread
);
2347 *voucherp
= voucher
;
2348 return KERN_SUCCESS
;
2352 * thread_set_mach_voucher - set a voucher reference for the specified thread voucher
2354 * Conditions: callers holds a reference on the voucher.
2357 * We grab another reference to the voucher and bind it to the thread. Any lazy
2358 * binding is erased. The old voucher reference associated with the thread is
2362 thread_set_mach_voucher(
2364 ipc_voucher_t voucher
)
2366 ipc_voucher_t old_voucher
;
2368 ledger_t bankledger
= NULL
;
2371 if (THREAD_NULL
== thread
)
2372 return KERN_INVALID_ARGUMENT
;
2374 if (thread
!= current_thread() || thread
->started
)
2375 return KERN_INVALID_ARGUMENT
;
2378 ipc_voucher_reference(voucher
);
2380 bankledger
= bank_get_voucher_ledger(voucher
);
2382 thread_mtx_lock(thread
);
2383 old_voucher
= thread
->ith_voucher
;
2384 thread
->ith_voucher
= voucher
;
2385 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2387 bank_swap_thread_bank_ledger(thread
, bankledger
);
2389 thread_mtx_unlock(thread
);
2391 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2392 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2393 (uintptr_t)thread_tid(thread
),
2394 (uintptr_t)MACH_PORT_NULL
,
2395 VM_KERNEL_ADDRPERM((uintptr_t)voucher
),
2398 ipc_voucher_release(old_voucher
);
2400 return KERN_SUCCESS
;
2404 * thread_swap_mach_voucher - swap a voucher reference for the specified thread voucher
2406 * Conditions: callers holds a reference on the new and presumed old voucher(s).
2409 * If the old voucher is still the same as passed in, replace it with new voucher
2410 * and discard the old (and the reference passed in). Otherwise, discard the new
2411 * and return an updated old voucher.
2414 thread_swap_mach_voucher(
2416 ipc_voucher_t new_voucher
,
2417 ipc_voucher_t
*in_out_old_voucher
)
2419 mach_port_name_t old_voucher_name
;
2420 ipc_voucher_t old_voucher
;
2422 ledger_t bankledger
= NULL
;
2425 if (THREAD_NULL
== thread
)
2426 return KERN_INVALID_TASK
;
2428 if (thread
!= current_thread() || thread
->started
)
2429 return KERN_INVALID_ARGUMENT
;
2432 bankledger
= bank_get_voucher_ledger(new_voucher
);
2435 thread_mtx_lock(thread
);
2437 old_voucher
= thread
->ith_voucher
;
2439 if (IPC_VOUCHER_NULL
== old_voucher
) {
2440 old_voucher_name
= thread
->ith_voucher_name
;
2442 /* perform lazy binding if needed */
2443 if (MACH_PORT_VALID(old_voucher_name
)) {
2444 old_voucher
= convert_port_name_to_voucher(old_voucher_name
);
2445 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2446 thread
->ith_voucher
= old_voucher
;
2448 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2449 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2450 (uintptr_t)thread_tid(thread
),
2451 (uintptr_t)old_voucher_name
,
2452 VM_KERNEL_ADDRPERM((uintptr_t)old_voucher
),
2458 /* swap in new voucher, if old voucher matches the one supplied */
2459 if (old_voucher
== *in_out_old_voucher
) {
2460 ipc_voucher_reference(new_voucher
);
2461 thread
->ith_voucher
= new_voucher
;
2462 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2464 bank_swap_thread_bank_ledger(thread
, bankledger
);
2466 thread_mtx_unlock(thread
);
2468 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2469 MACHDBG_CODE(DBG_MACH_IPC
,MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2470 (uintptr_t)thread_tid(thread
),
2471 (uintptr_t)MACH_PORT_NULL
,
2472 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher
),
2475 ipc_voucher_release(old_voucher
);
2477 *in_out_old_voucher
= IPC_VOUCHER_NULL
;
2478 return KERN_SUCCESS
;
2481 /* Otherwise, just return old voucher reference */
2482 ipc_voucher_reference(old_voucher
);
2483 thread_mtx_unlock(thread
);
2484 *in_out_old_voucher
= old_voucher
;
2485 return KERN_SUCCESS
;
2489 * thread_get_current_voucher_origin_pid - get the pid of the originator of the current voucher.
2492 thread_get_current_voucher_origin_pid(
2497 thread_t thread
= current_thread();
2499 buf_size
= sizeof(*pid
);
2500 kr
= mach_voucher_attr_command(thread
->ith_voucher
,
2501 MACH_VOUCHER_ATTR_KEY_BANK
,
2502 BANK_ORIGINATOR_PID
,
2505 (mach_voucher_attr_content_t
)pid
,
2512 uint32_t dtrace_get_thread_predcache(thread_t thread
)
2514 if (thread
!= THREAD_NULL
)
2515 return thread
->t_dtrace_predcache
;
2520 int64_t dtrace_get_thread_vtime(thread_t thread
)
2522 if (thread
!= THREAD_NULL
)
2523 return thread
->t_dtrace_vtime
;
2528 int dtrace_get_thread_last_cpu_id(thread_t thread
)
2530 if ((thread
!= THREAD_NULL
) && (thread
->last_processor
!= PROCESSOR_NULL
)) {
2531 return thread
->last_processor
->cpu_id
;
2537 int64_t dtrace_get_thread_tracing(thread_t thread
)
2539 if (thread
!= THREAD_NULL
)
2540 return thread
->t_dtrace_tracing
;
2545 boolean_t
dtrace_get_thread_reentering(thread_t thread
)
2547 if (thread
!= THREAD_NULL
)
2548 return (thread
->options
& TH_OPT_DTRACE
) ? TRUE
: FALSE
;
2553 vm_offset_t
dtrace_get_kernel_stack(thread_t thread
)
2555 if (thread
!= THREAD_NULL
)
2556 return thread
->kernel_stack
;
2561 int64_t dtrace_calc_thread_recent_vtime(thread_t thread
)
2563 if (thread
!= THREAD_NULL
) {
2564 processor_t processor
= current_processor();
2565 uint64_t abstime
= mach_absolute_time();
2568 timer
= PROCESSOR_DATA(processor
, thread_timer
);
2570 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
)) +
2571 (abstime
- timer
->tstamp
); /* XXX need interrupts off to prevent missed time? */
2576 void dtrace_set_thread_predcache(thread_t thread
, uint32_t predcache
)
2578 if (thread
!= THREAD_NULL
)
2579 thread
->t_dtrace_predcache
= predcache
;
2582 void dtrace_set_thread_vtime(thread_t thread
, int64_t vtime
)
2584 if (thread
!= THREAD_NULL
)
2585 thread
->t_dtrace_vtime
= vtime
;
2588 void dtrace_set_thread_tracing(thread_t thread
, int64_t accum
)
2590 if (thread
!= THREAD_NULL
)
2591 thread
->t_dtrace_tracing
= accum
;
2594 void dtrace_set_thread_reentering(thread_t thread
, boolean_t vbool
)
2596 if (thread
!= THREAD_NULL
) {
2598 thread
->options
|= TH_OPT_DTRACE
;
2600 thread
->options
&= (~TH_OPT_DTRACE
);
2604 vm_offset_t
dtrace_set_thread_recover(thread_t thread
, vm_offset_t recover
)
2606 vm_offset_t prev
= 0;
2608 if (thread
!= THREAD_NULL
) {
2609 prev
= thread
->recover
;
2610 thread
->recover
= recover
;
2615 void dtrace_thread_bootstrap(void)
2617 task_t task
= current_task();
2619 if (task
->thread_count
== 1) {
2620 thread_t thread
= current_thread();
2621 if (thread
->t_dtrace_flags
& TH_DTRACE_EXECSUCCESS
) {
2622 thread
->t_dtrace_flags
&= ~TH_DTRACE_EXECSUCCESS
;
2623 DTRACE_PROC(exec__success
);
2627 DTRACE_PROC(lwp__start
);
2632 dtrace_thread_didexec(thread_t thread
)
2634 thread
->t_dtrace_flags
|= TH_DTRACE_EXECSUCCESS
;
2636 #endif /* CONFIG_DTRACE */