2 * Copyright (c) 2000-2020 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/extmod_statistics.h>
101 #include <kern/ipc_mig.h>
102 #include <kern/ipc_tt.h>
103 #include <kern/mach_param.h>
104 #include <kern/machine.h>
105 #include <kern/misc_protos.h>
106 #include <kern/processor.h>
107 #include <kern/queue.h>
108 #include <kern/sched.h>
109 #include <kern/sched_prim.h>
110 #include <kern/sync_lock.h>
111 #include <kern/syscall_subr.h>
112 #include <kern/task.h>
113 #include <kern/thread.h>
114 #include <kern/thread_group.h>
115 #include <kern/coalition.h>
116 #include <kern/host.h>
117 #include <kern/zalloc.h>
118 #include <kern/assert.h>
119 #include <kern/exc_resource.h>
120 #include <kern/exc_guard.h>
121 #include <kern/telemetry.h>
122 #include <kern/policy_internal.h>
123 #include <kern/turnstile.h>
124 #include <kern/sched_clutch.h>
126 #include <corpses/task_corpse.h>
128 #include <kern/kpc.h>
132 #include <kern/monotonic.h>
133 #include <machine/monotonic.h>
134 #endif /* MONOTONIC */
136 #include <ipc/ipc_kmsg.h>
137 #include <ipc/ipc_port.h>
138 #include <bank/bank_types.h>
140 #include <vm/vm_kern.h>
141 #include <vm/vm_pageout.h>
143 #include <sys/kdebug.h>
144 #include <sys/bsdtask_info.h>
145 #include <mach/sdt.h>
146 #include <san/kasan.h>
148 #include <san/ksancov.h>
151 #include <stdatomic.h>
153 #if defined(HAS_APPLE_PAC)
155 #include <arm64/proc_reg.h>
156 #endif /* defined(HAS_APPLE_PAC) */
159 * Exported interfaces
161 #include <mach/task_server.h>
162 #include <mach/thread_act_server.h>
163 #include <mach/mach_host_server.h>
164 #include <mach/host_priv_server.h>
165 #include <mach/mach_voucher_server.h>
166 #include <kern/policy_internal.h>
169 #include <security/mac_mach_internal.h>
172 LCK_GRP_DECLARE(thread_lck_grp
, "thread");
174 ZONE_DECLARE(thread_zone
, "threads", sizeof(struct thread
), ZC_ZFREE_CLEARMEM
);
176 ZONE_DECLARE(thread_qos_override_zone
, "thread qos override",
177 sizeof(struct thread_qos_override
), ZC_NOENCRYPT
);
179 static struct mpsc_daemon_queue thread_stack_queue
;
180 static struct mpsc_daemon_queue thread_terminate_queue
;
181 static struct mpsc_daemon_queue thread_deallocate_queue
;
182 static struct mpsc_daemon_queue thread_exception_queue
;
184 decl_simple_lock_data(static, crashed_threads_lock
);
185 static queue_head_t crashed_threads_queue
;
187 struct thread_exception_elt
{
188 struct mpsc_queue_chain link
;
189 exception_type_t exception_type
;
190 task_t exception_task
;
191 thread_t exception_thread
;
194 static SECURITY_READ_ONLY_LATE(struct thread
) thread_template
= {
196 .thread_magic
= THREAD_MAGIC
,
197 #endif /* MACH_ASSERT */
198 .wait_result
= THREAD_WAITING
,
199 .options
= THREAD_ABORTSAFE
,
200 .state
= TH_WAIT
| TH_UNINT
,
201 .th_sched_bucket
= TH_BUCKET_RUN
,
202 .base_pri
= BASEPRI_DEFAULT
,
203 .realtime
.deadline
= UINT64_MAX
,
204 .last_made_runnable_time
= THREAD_NOT_RUNNABLE
,
205 .last_basepri_change_time
= THREAD_NOT_RUNNABLE
,
206 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
207 .pri_shift
= INT8_MAX
,
209 /* timers are initialized in thread_bootstrap */
212 static struct thread init_thread
;
213 static void thread_deallocate_enqueue(thread_t thread
);
214 static void thread_deallocate_complete(thread_t thread
);
217 extern void proc_exit(void *);
218 extern mach_exception_data_type_t
proc_encode_exit_exception_code(void *);
219 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
220 extern uint64_t get_return_to_kernel_offset_from_proc(void *p
);
221 extern int proc_selfpid(void);
222 extern void proc_name(int, char*, int);
223 extern char * proc_name_address(void *p
);
224 #endif /* MACH_BSD */
226 extern int disable_exc_resource
;
227 extern int audio_active
;
228 extern int debug_task
;
229 int thread_max
= CONFIG_THREAD_MAX
; /* Max number of threads */
230 int task_threadmax
= CONFIG_THREAD_MAX
;
232 static uint64_t thread_unique_id
= 100;
234 struct _thread_ledger_indices thread_ledgers
= { .cpu_time
= -1 };
235 static ledger_template_t thread_ledger_template
= NULL
;
236 static void init_thread_ledgers(void);
239 void jetsam_on_ledger_cpulimit_exceeded(void);
242 extern int task_thread_soft_limit
;
243 extern int exc_via_corpse_forking
;
245 #if DEVELOPMENT || DEBUG
246 extern int exc_resource_threads_enabled
;
247 #endif /* DEVELOPMENT || DEBUG */
250 * Level (in terms of percentage of the limit) at which the CPU usage monitor triggers telemetry.
252 * (ie when any thread's CPU consumption exceeds 70% of the limit, start taking user
253 * stacktraces, aka micro-stackshots)
255 #define CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT 70
257 /* Percentage. Level at which we start gathering telemetry. */
258 static TUNABLE(uint8_t, cpumon_ustackshots_trigger_pct
,
259 "cpumon_ustackshots_trigger_pct", CPUMON_USTACKSHOTS_TRIGGER_DEFAULT_PCT
);
260 void __attribute__((noinline
)) SENDING_NOTIFICATION__THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU(void);
261 #if DEVELOPMENT || DEBUG
262 void __attribute__((noinline
)) SENDING_NOTIFICATION__TASK_HAS_TOO_MANY_THREADS(task_t
, int);
263 #endif /* DEVELOPMENT || DEBUG */
266 * The smallest interval over which we support limiting CPU consumption is 1ms
268 #define MINIMUM_CPULIMIT_INTERVAL_MS 1
270 os_refgrp_decl(static, thread_refgrp
, "thread", NULL
);
273 init_thread_from_template(thread_t thread
)
276 * In general, struct thread isn't trivially-copyable, since it may
277 * contain pointers to thread-specific state. This may be enforced at
278 * compile time on architectures that store authed + diversified
279 * pointers in machine_thread.
281 * In this specific case, where we're initializing a new thread from a
282 * thread_template, we know all diversified pointers are NULL; these are
283 * safe to bitwise copy.
285 #pragma clang diagnostic push
286 #pragma clang diagnostic ignored "-Wnontrivial-memaccess"
287 memcpy(thread
, &thread_template
, sizeof(*thread
));
288 #pragma clang diagnostic pop
292 thread_bootstrap(void)
295 * Fill in a template thread for fast initialization.
297 timer_init(&thread_template
.user_timer
);
298 timer_init(&thread_template
.system_timer
);
299 timer_init(&thread_template
.ptime
);
300 timer_init(&thread_template
.runnable_timer
);
302 init_thread_from_template(&init_thread
);
303 /* fiddle with init thread to skip asserts in set_sched_pri */
304 init_thread
.sched_pri
= MAXPRI_KERNEL
;
305 #if DEBUG || DEVELOPMENT
306 queue_init(&init_thread
.t_temp_alloc_list
);
307 #endif /* DEBUG || DEVELOPMENT */
313 thread_machine_init_template(void)
315 machine_thread_template_init(&thread_template
);
323 thread_policy_init();
326 * Initialize any machine-dependent
327 * per-thread structures necessary.
329 machine_thread_init();
331 init_thread_ledgers();
335 thread_is_active(thread_t thread
)
337 return thread
->active
;
341 thread_corpse_continue(void)
343 thread_t thread
= current_thread();
345 thread_terminate_internal(thread
, TH_TERMINATE_OPTION_NONE
);
348 * Handle the thread termination directly
349 * here instead of returning to userspace.
351 assert(thread
->active
== FALSE
);
352 thread_ast_clear(thread
, AST_APC
);
353 thread_apc_ast(thread
);
355 panic("thread_corpse_continue");
361 thread_terminate_continue(void)
363 panic("thread_terminate_continue");
368 * thread_terminate_self:
371 thread_terminate_self(void)
373 thread_t thread
= current_thread();
377 if (thread
->t_temp_alloc_count
) {
378 kheap_temp_leak_panic(thread
);
381 pal_thread_terminate_self(thread
);
383 DTRACE_PROC(lwp__exit
);
385 thread_mtx_lock(thread
);
387 ipc_thread_disable(thread
);
389 thread_mtx_unlock(thread
);
391 thread_sched_call(thread
, NULL
);
393 spl_t s
= splsched();
396 thread_depress_abort_locked(thread
);
398 thread_unlock(thread
);
402 thead_remove_taskwatch(thread
);
403 #endif /* CONFIG_TASKWATCH */
405 work_interval_thread_terminate(thread
);
407 thread_mtx_lock(thread
);
409 thread_policy_reset(thread
);
411 thread_mtx_unlock(thread
);
413 assert(thread
->th_work_interval
== NULL
);
415 bank_swap_thread_bank_ledger(thread
, NULL
);
417 if (kdebug_enable
&& bsd_hasthreadname(thread
->uthread
)) {
418 char threadname
[MAXTHREADNAMESIZE
];
419 bsd_getthreadname(thread
->uthread
, threadname
);
420 kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV
, threadname
);
424 uthread_cleanup(task
, thread
->uthread
, task
->bsd_info
);
426 if (kdebug_enable
&& task
->bsd_info
&& !task_is_exec_copy(task
)) {
427 /* trace out pid before we sign off */
431 kdbg_trace_data(thread
->task
->bsd_info
, &dbg_arg1
, &dbg_arg2
);
433 if (kdebug_debugid_enabled(DBG_MT_INSTRS_CYCLES_THR_EXIT
)) {
434 uint64_t counts
[MT_CORE_NFIXED
];
435 uint64_t thread_user_time
;
436 uint64_t thread_system_time
;
437 thread_user_time
= timer_grab(&thread
->user_timer
);
438 thread_system_time
= timer_grab(&thread
->system_timer
);
439 mt_fixed_thread_counts(thread
, counts
);
440 KDBG_RELEASE(DBG_MT_INSTRS_CYCLES_THR_EXIT
,
441 #ifdef MT_CORE_INSTRS
442 counts
[MT_CORE_INSTRS
],
443 #else /* defined(MT_CORE_INSTRS) */
445 #endif/* !defined(MT_CORE_INSTRS) */
446 counts
[MT_CORE_CYCLES
],
447 thread_system_time
, thread_user_time
);
449 #endif/* MONOTONIC */
450 KDBG_RELEASE(TRACE_DATA_THREAD_TERMINATE_PID
, dbg_arg1
, dbg_arg2
);
454 * After this subtraction, this thread should never access
455 * task->bsd_info unless it got 0 back from the os_atomic_dec. It
456 * could be racing with other threads to be the last thread in the
457 * process, and the last thread in the process will tear down the proc
458 * structure and zero-out task->bsd_info.
460 threadcnt
= os_atomic_dec(&task
->active_thread_count
, relaxed
);
463 * If we are the last thread to terminate and the task is
464 * associated with a BSD process, perform BSD process exit.
466 if (threadcnt
== 0 && task
->bsd_info
!= NULL
&& !task_is_exec_copy(task
)) {
467 mach_exception_data_type_t subcode
= 0;
469 /* since we're the last thread in this process, trace out the command name too */
471 kdbg_trace_string(thread
->task
->bsd_info
, &args
[0], &args
[1], &args
[2], &args
[3]);
473 if (kdebug_debugid_enabled(DBG_MT_INSTRS_CYCLES_PROC_EXIT
)) {
474 uint64_t counts
[MT_CORE_NFIXED
];
475 uint64_t task_user_time
;
476 uint64_t task_system_time
;
477 mt_fixed_task_counts(task
, counts
);
478 /* since the thread time is not yet added to the task */
479 task_user_time
= task
->total_user_time
+ timer_grab(&thread
->user_timer
);
480 task_system_time
= task
->total_system_time
+ timer_grab(&thread
->system_timer
);
481 KDBG_RELEASE((DBG_MT_INSTRS_CYCLES_PROC_EXIT
),
482 #ifdef MT_CORE_INSTRS
483 counts
[MT_CORE_INSTRS
],
484 #else /* defined(MT_CORE_INSTRS) */
486 #endif/* !defined(MT_CORE_INSTRS) */
487 counts
[MT_CORE_CYCLES
],
488 task_system_time
, task_user_time
);
490 #endif/* MONOTONIC */
491 KDBG_RELEASE(TRACE_STRING_PROC_EXIT
, args
[0], args
[1], args
[2], args
[3]);
494 /* Get the exit reason before proc_exit */
495 subcode
= proc_encode_exit_exception_code(task
->bsd_info
);
496 proc_exit(task
->bsd_info
);
498 * if there is crash info in task
499 * then do the deliver action since this is
500 * last thread for this task.
502 if (task
->corpse_info
) {
503 task_deliver_crash_notification(task
, current_thread(), EXC_RESOURCE
, subcode
);
507 if (threadcnt
== 0) {
509 if (task_is_a_corpse_fork(task
)) {
510 thread_wakeup((event_t
)&task
->active_thread_count
);
515 uthread_cred_free(thread
->uthread
);
521 * Ensure that the depress timer is no longer enqueued,
522 * so the timer (stored in the thread) can be safely deallocated
524 * TODO: build timer_call_cancel_wait
527 assert((thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) == 0);
529 uint32_t delay_us
= 1;
531 while (thread
->depress_timer_active
> 0) {
532 thread_unlock(thread
);
537 if (delay_us
> USEC_PER_SEC
) {
538 panic("depress timer failed to inactivate!"
539 "thread: %p depress_timer_active: %d",
540 thread
, thread
->depress_timer_active
);
548 * Cancel wait timer, and wait for
549 * concurrent expirations.
551 if (thread
->wait_timer_is_set
) {
552 thread
->wait_timer_is_set
= FALSE
;
554 if (timer_call_cancel(&thread
->wait_timer
)) {
555 thread
->wait_timer_active
--;
561 while (thread
->wait_timer_active
> 0) {
562 thread_unlock(thread
);
567 if (delay_us
> USEC_PER_SEC
) {
568 panic("wait timer failed to inactivate!"
569 "thread: %p wait_timer_active: %d",
570 thread
, thread
->wait_timer_active
);
578 * If there is a reserved stack, release it.
580 if (thread
->reserved_stack
!= 0) {
581 stack_free_reserved(thread
);
582 thread
->reserved_stack
= 0;
586 * Mark thread as terminating, and block.
588 thread
->state
|= TH_TERMINATE
;
589 thread_mark_wait_locked(thread
, THREAD_UNINT
);
591 assert((thread
->sched_flags
& TH_SFLAG_WAITQ_PROMOTED
) == 0);
592 assert((thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
) == 0);
593 assert((thread
->sched_flags
& TH_SFLAG_EXEC_PROMOTED
) == 0);
594 assert((thread
->sched_flags
& TH_SFLAG_PROMOTED
) == 0);
595 assert((thread
->sched_flags
& TH_SFLAG_THREAD_GROUP_AUTO_JOIN
) == 0);
596 assert(thread
->th_work_interval_flags
== TH_WORK_INTERVAL_FLAGS_NONE
);
597 assert(thread
->kern_promotion_schedpri
== 0);
598 assert(thread
->waiting_for_mutex
== NULL
);
599 assert(thread
->rwlock_count
== 0);
600 assert(thread
->handoff_thread
== THREAD_NULL
);
601 assert(thread
->th_work_interval
== NULL
);
603 thread_unlock(thread
);
606 thread_block((thread_continue_t
)thread_terminate_continue
);
611 thread_ref_release(thread_t thread
)
613 if (thread
== THREAD_NULL
) {
617 assert_thread_magic(thread
);
619 return os_ref_release(&thread
->ref_count
) == 0;
622 /* Drop a thread refcount safely without triggering a zfree */
624 thread_deallocate_safe(thread_t thread
)
626 if (__improbable(thread_ref_release(thread
))) {
627 /* enqueue the thread for thread deallocate deamon to call thread_deallocate_complete */
628 thread_deallocate_enqueue(thread
);
633 thread_deallocate(thread_t thread
)
635 if (__improbable(thread_ref_release(thread
))) {
636 thread_deallocate_complete(thread
);
641 thread_deallocate_complete(
646 assert_thread_magic(thread
);
648 assert(os_ref_get_count(&thread
->ref_count
) == 0);
650 if (!(thread
->state
& TH_TERMINATE2
)) {
651 panic("thread_deallocate: thread not properly terminated\n");
654 assert(thread
->runq
== PROCESSOR_NULL
);
657 kpc_thread_destroy(thread
);
660 ipc_thread_terminate(thread
);
662 proc_thread_qos_deallocate(thread
);
668 void *ut
= thread
->uthread
;
670 thread
->uthread
= NULL
;
671 uthread_zone_free(ut
);
673 #endif /* MACH_BSD */
675 if (thread
->t_ledger
) {
676 ledger_dereference(thread
->t_ledger
);
678 if (thread
->t_threadledger
) {
679 ledger_dereference(thread
->t_threadledger
);
682 assert(thread
->turnstile
!= TURNSTILE_NULL
);
683 if (thread
->turnstile
) {
684 turnstile_deallocate(thread
->turnstile
);
687 if (IPC_VOUCHER_NULL
!= thread
->ith_voucher
) {
688 ipc_voucher_release(thread
->ith_voucher
);
691 if (thread
->thread_io_stats
) {
692 kheap_free(KHEAP_DATA_BUFFERS
, thread
->thread_io_stats
,
693 sizeof(struct io_stat_info
));
696 if (thread
->kernel_stack
!= 0) {
700 lck_mtx_destroy(&thread
->mutex
, &thread_lck_grp
);
701 machine_thread_destroy(thread
);
703 task_deallocate(task
);
706 assert_thread_magic(thread
);
707 thread
->thread_magic
= 0;
708 #endif /* MACH_ASSERT */
710 lck_mtx_lock(&tasks_threads_lock
);
711 assert(terminated_threads_count
> 0);
712 queue_remove(&terminated_threads
, thread
, thread_t
, threads
);
713 terminated_threads_count
--;
714 lck_mtx_unlock(&tasks_threads_lock
);
716 zfree(thread_zone
, thread
);
720 * thread_inspect_deallocate:
722 * Drop a thread inspection reference.
725 thread_inspect_deallocate(
726 thread_inspect_t thread_inspect
)
728 return thread_deallocate((thread_t
)thread_inspect
);
732 * thread_read_deallocate:
734 * Drop a reference on thread read port.
737 thread_read_deallocate(
738 thread_read_t thread_read
)
740 return thread_deallocate((thread_t
)thread_read
);
745 * thread_exception_queue_invoke:
747 * Deliver EXC_{RESOURCE,GUARD} exception
750 thread_exception_queue_invoke(mpsc_queue_chain_t elm
,
751 __assert_only mpsc_daemon_queue_t dq
)
753 struct thread_exception_elt
*elt
;
756 exception_type_t etype
;
758 assert(dq
== &thread_exception_queue
);
759 elt
= mpsc_queue_element(elm
, struct thread_exception_elt
, link
);
761 etype
= elt
->exception_type
;
762 task
= elt
->exception_task
;
763 thread
= elt
->exception_thread
;
764 assert_thread_magic(thread
);
766 kfree(elt
, sizeof(*elt
));
768 /* wait for all the threads in the task to terminate */
770 task_wait_till_threads_terminate_locked(task
);
773 /* Consumes the task ref returned by task_generate_corpse_internal */
774 task_deallocate(task
);
775 /* Consumes the thread ref returned by task_generate_corpse_internal */
776 thread_deallocate(thread
);
778 /* Deliver the notification, also clears the corpse. */
779 task_deliver_crash_notification(task
, thread
, etype
, 0);
783 * thread_exception_enqueue:
785 * Enqueue a corpse port to be delivered an EXC_{RESOURCE,GUARD}.
788 thread_exception_enqueue(
791 exception_type_t etype
)
793 assert(EXC_RESOURCE
== etype
|| EXC_GUARD
== etype
);
794 struct thread_exception_elt
*elt
= kalloc(sizeof(*elt
));
795 elt
->exception_type
= etype
;
796 elt
->exception_task
= task
;
797 elt
->exception_thread
= thread
;
799 mpsc_daemon_enqueue(&thread_exception_queue
, &elt
->link
,
800 MPSC_QUEUE_DISABLE_PREEMPTION
);
804 * thread_copy_resource_info
806 * Copy the resource info counters from source
807 * thread to destination thread.
810 thread_copy_resource_info(
814 dst_thread
->c_switch
= src_thread
->c_switch
;
815 dst_thread
->p_switch
= src_thread
->p_switch
;
816 dst_thread
->ps_switch
= src_thread
->ps_switch
;
817 dst_thread
->precise_user_kernel_time
= src_thread
->precise_user_kernel_time
;
818 dst_thread
->user_timer
= src_thread
->user_timer
;
819 dst_thread
->user_timer_save
= src_thread
->user_timer_save
;
820 dst_thread
->system_timer
= src_thread
->system_timer
;
821 dst_thread
->system_timer_save
= src_thread
->system_timer_save
;
822 dst_thread
->runnable_timer
= src_thread
->runnable_timer
;
823 dst_thread
->vtimer_user_save
= src_thread
->vtimer_user_save
;
824 dst_thread
->vtimer_prof_save
= src_thread
->vtimer_prof_save
;
825 dst_thread
->vtimer_rlim_save
= src_thread
->vtimer_rlim_save
;
826 dst_thread
->vtimer_qos_save
= src_thread
->vtimer_qos_save
;
827 dst_thread
->syscalls_unix
= src_thread
->syscalls_unix
;
828 dst_thread
->syscalls_mach
= src_thread
->syscalls_mach
;
829 ledger_rollup(dst_thread
->t_threadledger
, src_thread
->t_threadledger
);
830 *dst_thread
->thread_io_stats
= *src_thread
->thread_io_stats
;
834 thread_terminate_queue_invoke(mpsc_queue_chain_t e
,
835 __assert_only mpsc_daemon_queue_t dq
)
837 thread_t thread
= mpsc_queue_element(e
, struct thread
, mpsc_links
);
838 task_t task
= thread
->task
;
840 assert(dq
== &thread_terminate_queue
);
845 * if marked for crash reporting, skip reaping.
846 * The corpse delivery thread will clear bit and enqueue
847 * for reaping when done
849 * Note: the inspection field is set under the task lock
851 * FIXME[mad]: why enqueue for termination before `inspection` is false ?
853 if (__improbable(thread
->inspection
)) {
854 simple_lock(&crashed_threads_lock
, &thread_lck_grp
);
857 enqueue_tail(&crashed_threads_queue
, &thread
->runq_links
);
858 simple_unlock(&crashed_threads_lock
);
863 task
->total_user_time
+= timer_grab(&thread
->user_timer
);
864 task
->total_ptime
+= timer_grab(&thread
->ptime
);
865 task
->total_runnable_time
+= timer_grab(&thread
->runnable_timer
);
866 if (thread
->precise_user_kernel_time
) {
867 task
->total_system_time
+= timer_grab(&thread
->system_timer
);
869 task
->total_user_time
+= timer_grab(&thread
->system_timer
);
872 task
->c_switch
+= thread
->c_switch
;
873 task
->p_switch
+= thread
->p_switch
;
874 task
->ps_switch
+= thread
->ps_switch
;
876 task
->syscalls_unix
+= thread
->syscalls_unix
;
877 task
->syscalls_mach
+= thread
->syscalls_mach
;
879 task
->task_timer_wakeups_bin_1
+= thread
->thread_timer_wakeups_bin_1
;
880 task
->task_timer_wakeups_bin_2
+= thread
->thread_timer_wakeups_bin_2
;
881 task
->task_gpu_ns
+= ml_gpu_stat(thread
);
882 task
->task_energy
+= ml_energy_stat(thread
);
883 task
->decompressions
+= thread
->decompressions
;
886 mt_terminate_update(task
, thread
);
887 #endif /* MONOTONIC */
889 thread_update_qos_cpu_time(thread
);
891 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
892 task
->thread_count
--;
895 * If the task is being halted, and there is only one thread
896 * left in the task after this one, then wakeup that thread.
898 if (task
->thread_count
== 1 && task
->halting
) {
899 thread_wakeup((event_t
)&task
->halting
);
904 lck_mtx_lock(&tasks_threads_lock
);
905 queue_remove(&threads
, thread
, thread_t
, threads
);
907 queue_enter(&terminated_threads
, thread
, thread_t
, threads
);
908 terminated_threads_count
++;
909 lck_mtx_unlock(&tasks_threads_lock
);
911 thread_deallocate(thread
);
915 thread_deallocate_queue_invoke(mpsc_queue_chain_t e
,
916 __assert_only mpsc_daemon_queue_t dq
)
918 thread_t thread
= mpsc_queue_element(e
, struct thread
, mpsc_links
);
920 assert(dq
== &thread_deallocate_queue
);
922 thread_deallocate_complete(thread
);
926 * thread_terminate_enqueue:
928 * Enqueue a terminating thread for final disposition.
930 * Called at splsched.
933 thread_terminate_enqueue(
936 KDBG_RELEASE(TRACE_DATA_THREAD_TERMINATE
, thread
->thread_id
);
938 mpsc_daemon_enqueue(&thread_terminate_queue
, &thread
->mpsc_links
,
939 MPSC_QUEUE_DISABLE_PREEMPTION
);
943 * thread_deallocate_enqueue:
945 * Enqueue a thread for final deallocation.
948 thread_deallocate_enqueue(
951 mpsc_daemon_enqueue(&thread_deallocate_queue
, &thread
->mpsc_links
,
952 MPSC_QUEUE_DISABLE_PREEMPTION
);
956 * thread_terminate_crashed_threads:
957 * walk the list of crashed threads and put back set of threads
958 * who are no longer being inspected.
961 thread_terminate_crashed_threads(void)
965 simple_lock(&crashed_threads_lock
, &thread_lck_grp
);
967 * loop through the crashed threads queue
968 * to put any threads that are not being inspected anymore
971 qe_foreach_element_safe(th_remove
, &crashed_threads_queue
, runq_links
) {
972 /* make sure current_thread is never in crashed queue */
973 assert(th_remove
!= current_thread());
975 if (th_remove
->inspection
== FALSE
) {
976 remqueue(&th_remove
->runq_links
);
977 mpsc_daemon_enqueue(&thread_terminate_queue
, &th_remove
->mpsc_links
,
982 simple_unlock(&crashed_threads_lock
);
986 * thread_stack_queue_invoke:
988 * Perform stack allocation as required due to
992 thread_stack_queue_invoke(mpsc_queue_chain_t elm
,
993 __assert_only mpsc_daemon_queue_t dq
)
995 thread_t thread
= mpsc_queue_element(elm
, struct thread
, mpsc_links
);
997 assert(dq
== &thread_stack_queue
);
999 /* allocate stack with interrupts enabled so that we can call into VM */
1000 stack_alloc(thread
);
1002 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_WAIT
) | DBG_FUNC_END
, thread_tid(thread
), 0, 0, 0, 0);
1004 spl_t s
= splsched();
1005 thread_lock(thread
);
1006 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
1007 thread_unlock(thread
);
1012 * thread_stack_enqueue:
1014 * Enqueue a thread for stack allocation.
1016 * Called at splsched.
1019 thread_stack_enqueue(
1022 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_WAIT
) | DBG_FUNC_START
, thread_tid(thread
), 0, 0, 0, 0);
1023 assert_thread_magic(thread
);
1025 mpsc_daemon_enqueue(&thread_stack_queue
, &thread
->mpsc_links
,
1026 MPSC_QUEUE_DISABLE_PREEMPTION
);
1030 thread_daemon_init(void)
1032 kern_return_t result
;
1034 thread_deallocate_daemon_init();
1036 thread_deallocate_daemon_register_queue(&thread_terminate_queue
,
1037 thread_terminate_queue_invoke
);
1039 thread_deallocate_daemon_register_queue(&thread_deallocate_queue
,
1040 thread_deallocate_queue_invoke
);
1042 simple_lock_init(&crashed_threads_lock
, 0);
1043 queue_init(&crashed_threads_queue
);
1045 result
= mpsc_daemon_queue_init_with_thread(&thread_stack_queue
,
1046 thread_stack_queue_invoke
, BASEPRI_PREEMPT_HIGH
,
1047 "daemon.thread-stack");
1048 if (result
!= KERN_SUCCESS
) {
1049 panic("thread_daemon_init: thread_stack_daemon");
1052 result
= mpsc_daemon_queue_init_with_thread(&thread_exception_queue
,
1053 thread_exception_queue_invoke
, MINPRI_KERNEL
,
1054 "daemon.thread-exception");
1055 if (result
!= KERN_SUCCESS
) {
1056 panic("thread_daemon_init: thread_exception_daemon");
1060 __options_decl(thread_create_internal_options_t
, uint32_t, {
1061 TH_OPTION_NONE
= 0x00,
1062 TH_OPTION_NOCRED
= 0x01,
1063 TH_OPTION_NOSUSP
= 0x02,
1064 TH_OPTION_WORKQ
= 0x04,
1065 TH_OPTION_IMMOVABLE
= 0x08,
1066 TH_OPTION_PINNED
= 0x10,
1070 * Create a new thread.
1071 * Doesn't start the thread running.
1073 * Task and tasks_threads_lock are returned locked on success.
1075 static kern_return_t
1076 thread_create_internal(
1079 thread_continue_t continuation
,
1081 thread_create_internal_options_t options
,
1082 thread_t
*out_thread
)
1084 thread_t new_thread
;
1085 static thread_t first_thread
;
1086 ipc_thread_init_options_t init_options
= IPC_THREAD_INIT_NONE
;
1089 * Allocate a thread and initialize static fields
1091 if (first_thread
== THREAD_NULL
) {
1092 new_thread
= first_thread
= current_thread();
1094 new_thread
= (thread_t
)zalloc(thread_zone
);
1096 if (new_thread
== THREAD_NULL
) {
1097 return KERN_RESOURCE_SHORTAGE
;
1100 if (new_thread
!= first_thread
) {
1101 init_thread_from_template(new_thread
);
1104 if (options
& TH_OPTION_PINNED
) {
1105 init_options
|= IPC_THREAD_INIT_PINNED
;
1108 if (options
& TH_OPTION_IMMOVABLE
) {
1109 init_options
|= IPC_THREAD_INIT_IMMOVABLE
;
1112 os_ref_init_count(&new_thread
->ref_count
, &thread_refgrp
, 2);
1113 #if DEBUG || DEVELOPMENT
1114 queue_init(&new_thread
->t_temp_alloc_list
);
1115 #endif /* DEBUG || DEVELOPMENT */
1118 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
, (options
& TH_OPTION_NOCRED
) != 0);
1119 if (new_thread
->uthread
== NULL
) {
1121 new_thread
->thread_magic
= 0;
1122 #endif /* MACH_ASSERT */
1124 zfree(thread_zone
, new_thread
);
1125 return KERN_RESOURCE_SHORTAGE
;
1127 #endif /* MACH_BSD */
1129 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
1131 void *ut
= new_thread
->uthread
;
1133 new_thread
->uthread
= NULL
;
1134 /* cred free may not be necessary */
1135 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
1136 uthread_cred_free(ut
);
1137 uthread_zone_free(ut
);
1138 #endif /* MACH_BSD */
1141 new_thread
->thread_magic
= 0;
1142 #endif /* MACH_ASSERT */
1144 zfree(thread_zone
, new_thread
);
1145 return KERN_FAILURE
;
1148 new_thread
->task
= parent_task
;
1150 thread_lock_init(new_thread
);
1151 wake_lock_init(new_thread
);
1153 lck_mtx_init(&new_thread
->mutex
, &thread_lck_grp
, LCK_ATTR_NULL
);
1155 ipc_thread_init(new_thread
, init_options
);
1157 new_thread
->continuation
= continuation
;
1158 new_thread
->parameter
= parameter
;
1159 new_thread
->inheritor_flags
= TURNSTILE_UPDATE_FLAGS_NONE
;
1160 priority_queue_init(&new_thread
->sched_inheritor_queue
);
1161 priority_queue_init(&new_thread
->base_inheritor_queue
);
1162 #if CONFIG_SCHED_CLUTCH
1163 priority_queue_entry_init(&new_thread
->th_clutch_runq_link
);
1164 priority_queue_entry_init(&new_thread
->th_clutch_pri_link
);
1165 #endif /* CONFIG_SCHED_CLUTCH */
1167 #if CONFIG_SCHED_EDGE
1168 new_thread
->th_bound_cluster_enqueued
= false;
1169 #endif /* CONFIG_SCHED_EDGE */
1171 /* Allocate I/O Statistics structure */
1172 new_thread
->thread_io_stats
= kheap_alloc(KHEAP_DATA_BUFFERS
,
1173 sizeof(struct io_stat_info
), Z_WAITOK
| Z_ZERO
);
1174 assert(new_thread
->thread_io_stats
!= NULL
);
1177 kasan_init_thread(&new_thread
->kasan_data
);
1181 new_thread
->ksancov_data
= NULL
;
1185 /* Clear out the I/O Scheduling info for AppleFSCompression */
1186 new_thread
->decmp_upl
= NULL
;
1187 #endif /* CONFIG_IOSCHED */
1189 new_thread
->thread_region_page_shift
= 0;
1191 #if DEVELOPMENT || DEBUG
1192 task_lock(parent_task
);
1193 uint16_t thread_limit
= parent_task
->task_thread_limit
;
1194 if (exc_resource_threads_enabled
&&
1196 parent_task
->thread_count
>= thread_limit
&&
1197 !parent_task
->task_has_crossed_thread_limit
&&
1198 !(parent_task
->t_flags
& TF_CORPSE
)) {
1199 int thread_count
= parent_task
->thread_count
;
1200 parent_task
->task_has_crossed_thread_limit
= TRUE
;
1201 task_unlock(parent_task
);
1202 SENDING_NOTIFICATION__TASK_HAS_TOO_MANY_THREADS(parent_task
, thread_count
);
1204 task_unlock(parent_task
);
1208 lck_mtx_lock(&tasks_threads_lock
);
1209 task_lock(parent_task
);
1212 * Fail thread creation if parent task is being torn down or has too many threads
1213 * If the caller asked for TH_OPTION_NOSUSP, also fail if the parent task is suspended
1215 if (parent_task
->active
== 0 || parent_task
->halting
||
1216 (parent_task
->suspend_count
> 0 && (options
& TH_OPTION_NOSUSP
) != 0) ||
1217 (parent_task
->thread_count
>= task_threadmax
&& parent_task
!= kernel_task
)) {
1218 task_unlock(parent_task
);
1219 lck_mtx_unlock(&tasks_threads_lock
);
1223 void *ut
= new_thread
->uthread
;
1225 new_thread
->uthread
= NULL
;
1226 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
1227 /* cred free may not be necessary */
1228 uthread_cred_free(ut
);
1229 uthread_zone_free(ut
);
1231 #endif /* MACH_BSD */
1232 ipc_thread_disable(new_thread
);
1233 ipc_thread_terminate(new_thread
);
1234 kheap_free(KHEAP_DATA_BUFFERS
, new_thread
->thread_io_stats
,
1235 sizeof(struct io_stat_info
));
1236 lck_mtx_destroy(&new_thread
->mutex
, &thread_lck_grp
);
1237 machine_thread_destroy(new_thread
);
1238 zfree(thread_zone
, new_thread
);
1239 return KERN_FAILURE
;
1242 /* Protected by the tasks_threads_lock */
1243 new_thread
->thread_id
= ++thread_unique_id
;
1245 /* New threads inherit any default state on the task */
1246 machine_thread_inherit_taskwide(new_thread
, parent_task
);
1248 task_reference_internal(parent_task
);
1250 if (new_thread
->task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_PERTHR_LIMIT
) {
1252 * This task has a per-thread CPU limit; make sure this new thread
1253 * gets its limit set too, before it gets out of the kernel.
1255 act_set_astledger(new_thread
);
1258 /* Instantiate a thread ledger. Do not fail thread creation if ledger creation fails. */
1259 if ((new_thread
->t_threadledger
= ledger_instantiate(thread_ledger_template
,
1260 LEDGER_CREATE_INACTIVE_ENTRIES
)) != LEDGER_NULL
) {
1261 ledger_entry_setactive(new_thread
->t_threadledger
, thread_ledgers
.cpu_time
);
1264 new_thread
->t_bankledger
= LEDGER_NULL
;
1265 new_thread
->t_deduct_bank_ledger_time
= 0;
1266 new_thread
->t_deduct_bank_ledger_energy
= 0;
1268 new_thread
->t_ledger
= new_thread
->task
->ledger
;
1269 if (new_thread
->t_ledger
) {
1270 ledger_reference(new_thread
->t_ledger
);
1273 #if defined(CONFIG_SCHED_MULTIQ)
1274 /* Cache the task's sched_group */
1275 new_thread
->sched_group
= parent_task
->sched_group
;
1276 #endif /* defined(CONFIG_SCHED_MULTIQ) */
1278 /* Cache the task's map */
1279 new_thread
->map
= parent_task
->map
;
1281 timer_call_setup(&new_thread
->wait_timer
, thread_timer_expire
, new_thread
);
1282 timer_call_setup(&new_thread
->depress_timer
, thread_depress_expire
, new_thread
);
1285 kpc_thread_create(new_thread
);
1288 /* Set the thread's scheduling parameters */
1289 new_thread
->sched_mode
= SCHED(initial_thread_sched_mode
)(parent_task
);
1290 new_thread
->max_priority
= parent_task
->max_priority
;
1291 new_thread
->task_priority
= parent_task
->priority
;
1293 #if CONFIG_THREAD_GROUPS
1294 thread_group_init_thread(new_thread
, parent_task
);
1295 #endif /* CONFIG_THREAD_GROUPS */
1297 int new_priority
= (priority
< 0) ? parent_task
->priority
: priority
;
1298 new_priority
= (priority
< 0)? parent_task
->priority
: priority
;
1299 if (new_priority
> new_thread
->max_priority
) {
1300 new_priority
= new_thread
->max_priority
;
1302 #if !defined(XNU_TARGET_OS_OSX)
1303 if (new_priority
< MAXPRI_THROTTLE
) {
1304 new_priority
= MAXPRI_THROTTLE
;
1306 #endif /* !defined(XNU_TARGET_OS_OSX) */
1308 new_thread
->importance
= new_priority
- new_thread
->task_priority
;
1310 sched_set_thread_base_priority(new_thread
, new_priority
);
1312 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
1313 new_thread
->sched_stamp
= sched_tick
;
1314 #if CONFIG_SCHED_CLUTCH
1315 new_thread
->pri_shift
= sched_clutch_thread_pri_shift(new_thread
, new_thread
->th_sched_bucket
);
1316 #else /* CONFIG_SCHED_CLUTCH */
1317 new_thread
->pri_shift
= sched_pri_shifts
[new_thread
->th_sched_bucket
];
1318 #endif /* CONFIG_SCHED_CLUTCH */
1319 #endif /* defined(CONFIG_SCHED_TIMESHARE_CORE) */
1321 if (parent_task
->max_priority
<= MAXPRI_THROTTLE
) {
1322 sched_thread_mode_demote(new_thread
, TH_SFLAG_THROTTLED
);
1325 thread_policy_create(new_thread
);
1327 /* Chain the thread onto the task's list */
1328 queue_enter(&parent_task
->threads
, new_thread
, thread_t
, task_threads
);
1329 parent_task
->thread_count
++;
1331 /* So terminating threads don't need to take the task lock to decrement */
1332 os_atomic_inc(&parent_task
->active_thread_count
, relaxed
);
1334 queue_enter(&threads
, new_thread
, thread_t
, threads
);
1337 new_thread
->active
= TRUE
;
1338 if (task_is_a_corpse_fork(parent_task
)) {
1339 /* Set the inspection bit if the task is a corpse fork */
1340 new_thread
->inspection
= TRUE
;
1342 new_thread
->inspection
= FALSE
;
1344 new_thread
->corpse_dup
= FALSE
;
1345 new_thread
->turnstile
= turnstile_alloc();
1348 *out_thread
= new_thread
;
1350 if (kdebug_enable
) {
1353 kdbg_trace_data(parent_task
->bsd_info
, &args
[1], &args
[3]);
1356 * Starting with 26604425, exec'ing creates a new task/thread.
1358 * NEWTHREAD in the current process has two possible meanings:
1360 * 1) Create a new thread for this process.
1361 * 2) Create a new thread for the future process this will become in an
1364 * To disambiguate these, arg3 will be set to TRUE for case #2.
1366 * The value we need to find (TPF_EXEC_COPY) is stable in the case of a
1367 * task exec'ing. The read of t_procflags does not take the proc_lock.
1369 args
[2] = task_is_exec_copy(parent_task
) ? 1 : 0;
1371 KDBG_RELEASE(TRACE_DATA_NEWTHREAD
, (uintptr_t)thread_tid(new_thread
),
1372 args
[1], args
[2], args
[3]);
1374 kdbg_trace_string(parent_task
->bsd_info
, &args
[0], &args
[1],
1375 &args
[2], &args
[3]);
1376 KDBG_RELEASE(TRACE_STRING_NEWTHREAD
, args
[0], args
[1], args
[2],
1380 DTRACE_PROC1(lwp__create
, thread_t
, *out_thread
);
1382 return KERN_SUCCESS
;
1385 static kern_return_t
1386 thread_create_with_options_internal(
1388 thread_t
*new_thread
,
1389 boolean_t from_user
,
1390 thread_create_internal_options_t options
,
1391 thread_continue_t continuation
)
1393 kern_return_t result
;
1396 if (task
== TASK_NULL
|| task
== kernel_task
) {
1397 return KERN_INVALID_ARGUMENT
;
1401 if (from_user
&& current_task() != task
&&
1402 mac_proc_check_remote_thread_create(task
, -1, NULL
, 0) != 0) {
1407 result
= thread_create_internal(task
, -1, continuation
, NULL
, options
, &thread
);
1408 if (result
!= KERN_SUCCESS
) {
1412 thread
->user_stop_count
= 1;
1413 thread_hold(thread
);
1414 if (task
->suspend_count
> 0) {
1415 thread_hold(thread
);
1419 extmod_statistics_incr_thread_create(task
);
1423 lck_mtx_unlock(&tasks_threads_lock
);
1425 *new_thread
= thread
;
1427 return KERN_SUCCESS
;
1430 /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
1434 thread_t
*new_thread
);
1439 thread_t
*new_thread
)
1441 return thread_create_with_options_internal(task
, new_thread
, FALSE
, TH_OPTION_NONE
,
1442 (thread_continue_t
)thread_bootstrap_return
);
1446 * Create a thread that has its itk_self pinned
1447 * Deprecated, should be cleanup once rdar://70892168 lands
1450 thread_create_pinned(
1452 thread_t
*new_thread
)
1454 return thread_create_with_options_internal(task
, new_thread
, FALSE
,
1455 TH_OPTION_PINNED
| TH_OPTION_IMMOVABLE
, (thread_continue_t
)thread_bootstrap_return
);
1459 thread_create_immovable(
1461 thread_t
*new_thread
)
1463 return thread_create_with_options_internal(task
, new_thread
, FALSE
,
1464 TH_OPTION_IMMOVABLE
, (thread_continue_t
)thread_bootstrap_return
);
1468 thread_create_from_user(
1470 thread_t
*new_thread
)
1472 return thread_create_with_options_internal(task
, new_thread
, TRUE
, TH_OPTION_NONE
,
1473 (thread_continue_t
)thread_bootstrap_return
);
1477 thread_create_with_continuation(
1479 thread_t
*new_thread
,
1480 thread_continue_t continuation
)
1482 return thread_create_with_options_internal(task
, new_thread
, FALSE
, TH_OPTION_NONE
, continuation
);
1486 * Create a thread that is already started, but is waiting on an event
1488 static kern_return_t
1489 thread_create_waiting_internal(
1491 thread_continue_t continuation
,
1493 block_hint_t block_hint
,
1495 thread_t
*new_thread
)
1497 kern_return_t result
;
1500 if (task
== TASK_NULL
|| task
== kernel_task
) {
1501 return KERN_INVALID_ARGUMENT
;
1504 result
= thread_create_internal(task
, -1, continuation
, NULL
,
1506 if (result
!= KERN_SUCCESS
) {
1510 /* note no user_stop_count or thread_hold here */
1512 if (task
->suspend_count
> 0) {
1513 thread_hold(thread
);
1516 thread_mtx_lock(thread
);
1517 thread_set_pending_block_hint(thread
, block_hint
);
1518 if (options
& TH_OPTION_WORKQ
) {
1519 thread
->static_param
= true;
1520 event
= workq_thread_init_and_wq_lock(task
, thread
);
1522 thread_start_in_assert_wait(thread
, event
, THREAD_INTERRUPTIBLE
);
1523 thread_mtx_unlock(thread
);
1526 lck_mtx_unlock(&tasks_threads_lock
);
1528 *new_thread
= thread
;
1530 return KERN_SUCCESS
;
1534 thread_create_waiting(
1536 thread_continue_t continuation
,
1538 th_create_waiting_options_t options
,
1539 thread_t
*new_thread
)
1541 thread_create_internal_options_t ci_options
= TH_OPTION_NONE
;
1543 assert((options
& ~TH_CREATE_WAITING_OPTION_MASK
) == 0);
1544 if (options
& TH_CREATE_WAITING_OPTION_PINNED
) {
1545 ci_options
|= TH_OPTION_PINNED
;
1547 if (options
& TH_CREATE_WAITING_OPTION_IMMOVABLE
) {
1548 ci_options
|= TH_OPTION_IMMOVABLE
;
1551 return thread_create_waiting_internal(task
, continuation
, event
,
1552 kThreadWaitNone
, ci_options
, new_thread
);
1556 static kern_return_t
1557 thread_create_running_internal2(
1560 thread_state_t new_state
,
1561 mach_msg_type_number_t new_state_count
,
1562 thread_t
*new_thread
,
1563 boolean_t from_user
)
1565 kern_return_t result
;
1568 if (task
== TASK_NULL
|| task
== kernel_task
) {
1569 return KERN_INVALID_ARGUMENT
;
1573 if (from_user
&& current_task() != task
&&
1574 mac_proc_check_remote_thread_create(task
, flavor
, new_state
, new_state_count
) != 0) {
1579 result
= thread_create_internal(task
, -1,
1580 (thread_continue_t
)thread_bootstrap_return
, NULL
,
1581 TH_OPTION_NONE
, &thread
);
1582 if (result
!= KERN_SUCCESS
) {
1586 if (task
->suspend_count
> 0) {
1587 thread_hold(thread
);
1591 result
= machine_thread_state_convert_from_user(thread
, flavor
,
1592 new_state
, new_state_count
);
1594 if (result
== KERN_SUCCESS
) {
1595 result
= machine_thread_set_state(thread
, flavor
, new_state
,
1598 if (result
!= KERN_SUCCESS
) {
1600 lck_mtx_unlock(&tasks_threads_lock
);
1602 thread_terminate(thread
);
1603 thread_deallocate(thread
);
1607 thread_mtx_lock(thread
);
1608 thread_start(thread
);
1609 thread_mtx_unlock(thread
);
1612 extmod_statistics_incr_thread_create(task
);
1616 lck_mtx_unlock(&tasks_threads_lock
);
1618 *new_thread
= thread
;
1623 /* Prototype, see justification above */
1625 thread_create_running(
1628 thread_state_t new_state
,
1629 mach_msg_type_number_t new_state_count
,
1630 thread_t
*new_thread
);
1633 thread_create_running(
1636 thread_state_t new_state
,
1637 mach_msg_type_number_t new_state_count
,
1638 thread_t
*new_thread
)
1640 return thread_create_running_internal2(
1641 task
, flavor
, new_state
, new_state_count
,
1646 thread_create_running_from_user(
1649 thread_state_t new_state
,
1650 mach_msg_type_number_t new_state_count
,
1651 thread_t
*new_thread
)
1653 return thread_create_running_internal2(
1654 task
, flavor
, new_state
, new_state_count
,
1659 thread_create_workq_waiting(
1661 thread_continue_t continuation
,
1662 thread_t
*new_thread
)
1665 * Create thread, but don't pin control port just yet, in case someone calls
1666 * task_threads() and deallocates pinned port before kernel copyout happens,
1667 * which will result in pinned port guard exception. Instead, pin and make
1668 * it immovable atomically at copyout during workq_setup_and_run().
1670 int options
= TH_OPTION_NOCRED
| TH_OPTION_NOSUSP
| TH_OPTION_WORKQ
| TH_OPTION_IMMOVABLE
;
1671 return thread_create_waiting_internal(task
, continuation
, NULL
,
1672 kThreadWaitParkedWorkQueue
, options
, new_thread
);
1676 * kernel_thread_create:
1678 * Create a thread in the kernel task
1679 * to execute in kernel context.
1682 kernel_thread_create(
1683 thread_continue_t continuation
,
1686 thread_t
*new_thread
)
1688 kern_return_t result
;
1690 task_t task
= kernel_task
;
1692 result
= thread_create_internal(task
, priority
, continuation
, parameter
,
1693 TH_OPTION_NOCRED
| TH_OPTION_NONE
, &thread
);
1694 if (result
!= KERN_SUCCESS
) {
1699 lck_mtx_unlock(&tasks_threads_lock
);
1701 stack_alloc(thread
);
1702 assert(thread
->kernel_stack
!= 0);
1703 #if !defined(XNU_TARGET_OS_OSX)
1704 if (priority
> BASEPRI_KERNEL
)
1706 thread
->reserved_stack
= thread
->kernel_stack
;
1708 if (debug_task
& 1) {
1709 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread
, continuation
);
1711 *new_thread
= thread
;
1717 kernel_thread_start_priority(
1718 thread_continue_t continuation
,
1721 thread_t
*new_thread
)
1723 kern_return_t result
;
1726 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
1727 if (result
!= KERN_SUCCESS
) {
1731 *new_thread
= thread
;
1733 thread_mtx_lock(thread
);
1734 thread_start(thread
);
1735 thread_mtx_unlock(thread
);
1741 kernel_thread_start(
1742 thread_continue_t continuation
,
1744 thread_t
*new_thread
)
1746 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
1749 /* Separated into helper function so it can be used by THREAD_BASIC_INFO and THREAD_EXTENDED_INFO */
1750 /* it is assumed that the thread is locked by the caller */
1752 retrieve_thread_basic_info(thread_t thread
, thread_basic_info_t basic_info
)
1758 thread_read_times(thread
, &basic_info
->user_time
,
1759 &basic_info
->system_time
, NULL
);
1762 * Update lazy-evaluated scheduler info because someone wants it.
1764 if (SCHED(can_update_priority
)(thread
)) {
1765 SCHED(update_priority
)(thread
);
1768 basic_info
->sleep_time
= 0;
1771 * To calculate cpu_usage, first correct for timer rate,
1772 * then for 5/8 ageing. The correction factor [3/5] is
1775 basic_info
->cpu_usage
= 0;
1776 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
1777 if (sched_tick_interval
) {
1778 basic_info
->cpu_usage
= (integer_t
)(((uint64_t)thread
->cpu_usage
1779 * TH_USAGE_SCALE
) / sched_tick_interval
);
1780 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
1784 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
) {
1785 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
1788 basic_info
->policy
= ((thread
->sched_mode
== TH_MODE_TIMESHARE
)?
1789 POLICY_TIMESHARE
: POLICY_RR
);
1792 if (thread
->options
& TH_OPT_IDLE_THREAD
) {
1793 flags
|= TH_FLAGS_IDLE
;
1796 if (thread
->options
& TH_OPT_GLOBAL_FORCED_IDLE
) {
1797 flags
|= TH_FLAGS_GLOBAL_FORCED_IDLE
;
1800 if (!thread
->kernel_stack
) {
1801 flags
|= TH_FLAGS_SWAPPED
;
1805 if (thread
->state
& TH_TERMINATE
) {
1806 state
= TH_STATE_HALTED
;
1807 } else if (thread
->state
& TH_RUN
) {
1808 state
= TH_STATE_RUNNING
;
1809 } else if (thread
->state
& TH_UNINT
) {
1810 state
= TH_STATE_UNINTERRUPTIBLE
;
1811 } else if (thread
->state
& TH_SUSP
) {
1812 state
= TH_STATE_STOPPED
;
1813 } else if (thread
->state
& TH_WAIT
) {
1814 state
= TH_STATE_WAITING
;
1817 basic_info
->run_state
= state
;
1818 basic_info
->flags
= flags
;
1820 basic_info
->suspend_count
= thread
->user_stop_count
;
1826 thread_info_internal(
1828 thread_flavor_t flavor
,
1829 thread_info_t thread_info_out
, /* ptr to OUT array */
1830 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
1834 if (thread
== THREAD_NULL
) {
1835 return KERN_INVALID_ARGUMENT
;
1838 if (flavor
== THREAD_BASIC_INFO
) {
1839 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
) {
1840 return KERN_INVALID_ARGUMENT
;
1844 thread_lock(thread
);
1846 retrieve_thread_basic_info(thread
, (thread_basic_info_t
) thread_info_out
);
1848 thread_unlock(thread
);
1851 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
1853 return KERN_SUCCESS
;
1854 } else if (flavor
== THREAD_IDENTIFIER_INFO
) {
1855 thread_identifier_info_t identifier_info
;
1857 if (*thread_info_count
< THREAD_IDENTIFIER_INFO_COUNT
) {
1858 return KERN_INVALID_ARGUMENT
;
1861 identifier_info
= __IGNORE_WCASTALIGN((thread_identifier_info_t
)thread_info_out
);
1864 thread_lock(thread
);
1866 identifier_info
->thread_id
= thread
->thread_id
;
1867 identifier_info
->thread_handle
= thread
->machine
.cthread_self
;
1868 identifier_info
->dispatch_qaddr
= thread_dispatchqaddr(thread
);
1870 thread_unlock(thread
);
1872 return KERN_SUCCESS
;
1873 } else if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
1874 policy_timeshare_info_t ts_info
;
1876 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
) {
1877 return KERN_INVALID_ARGUMENT
;
1880 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
1883 thread_lock(thread
);
1885 if (thread
->sched_mode
!= TH_MODE_TIMESHARE
) {
1886 thread_unlock(thread
);
1888 return KERN_INVALID_POLICY
;
1891 ts_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1892 if (ts_info
->depressed
) {
1893 ts_info
->base_priority
= DEPRESSPRI
;
1894 ts_info
->depress_priority
= thread
->base_pri
;
1896 ts_info
->base_priority
= thread
->base_pri
;
1897 ts_info
->depress_priority
= -1;
1900 ts_info
->cur_priority
= thread
->sched_pri
;
1901 ts_info
->max_priority
= thread
->max_priority
;
1903 thread_unlock(thread
);
1906 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
1908 return KERN_SUCCESS
;
1909 } else if (flavor
== THREAD_SCHED_FIFO_INFO
) {
1910 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
) {
1911 return KERN_INVALID_ARGUMENT
;
1914 return KERN_INVALID_POLICY
;
1915 } else if (flavor
== THREAD_SCHED_RR_INFO
) {
1916 policy_rr_info_t rr_info
;
1917 uint32_t quantum_time
;
1918 uint64_t quantum_ns
;
1920 if (*thread_info_count
< POLICY_RR_INFO_COUNT
) {
1921 return KERN_INVALID_ARGUMENT
;
1924 rr_info
= (policy_rr_info_t
) thread_info_out
;
1927 thread_lock(thread
);
1929 if (thread
->sched_mode
== TH_MODE_TIMESHARE
) {
1930 thread_unlock(thread
);
1933 return KERN_INVALID_POLICY
;
1936 rr_info
->depressed
= (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) != 0;
1937 if (rr_info
->depressed
) {
1938 rr_info
->base_priority
= DEPRESSPRI
;
1939 rr_info
->depress_priority
= thread
->base_pri
;
1941 rr_info
->base_priority
= thread
->base_pri
;
1942 rr_info
->depress_priority
= -1;
1945 quantum_time
= SCHED(initial_quantum_size
)(THREAD_NULL
);
1946 absolutetime_to_nanoseconds(quantum_time
, &quantum_ns
);
1948 rr_info
->max_priority
= thread
->max_priority
;
1949 rr_info
->quantum
= (uint32_t)(quantum_ns
/ 1000 / 1000);
1951 thread_unlock(thread
);
1954 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1956 return KERN_SUCCESS
;
1957 } else if (flavor
== THREAD_EXTENDED_INFO
) {
1958 thread_basic_info_data_t basic_info
;
1959 thread_extended_info_t extended_info
= __IGNORE_WCASTALIGN((thread_extended_info_t
)thread_info_out
);
1961 if (*thread_info_count
< THREAD_EXTENDED_INFO_COUNT
) {
1962 return KERN_INVALID_ARGUMENT
;
1966 thread_lock(thread
);
1968 /* NOTE: This mimics fill_taskthreadinfo(), which is the function used by proc_pidinfo() for
1969 * the PROC_PIDTHREADINFO flavor (which can't be used on corpses)
1971 retrieve_thread_basic_info(thread
, &basic_info
);
1972 extended_info
->pth_user_time
= (((uint64_t)basic_info
.user_time
.seconds
* NSEC_PER_SEC
) + ((uint64_t)basic_info
.user_time
.microseconds
* NSEC_PER_USEC
));
1973 extended_info
->pth_system_time
= (((uint64_t)basic_info
.system_time
.seconds
* NSEC_PER_SEC
) + ((uint64_t)basic_info
.system_time
.microseconds
* NSEC_PER_USEC
));
1975 extended_info
->pth_cpu_usage
= basic_info
.cpu_usage
;
1976 extended_info
->pth_policy
= basic_info
.policy
;
1977 extended_info
->pth_run_state
= basic_info
.run_state
;
1978 extended_info
->pth_flags
= basic_info
.flags
;
1979 extended_info
->pth_sleep_time
= basic_info
.sleep_time
;
1980 extended_info
->pth_curpri
= thread
->sched_pri
;
1981 extended_info
->pth_priority
= thread
->base_pri
;
1982 extended_info
->pth_maxpriority
= thread
->max_priority
;
1984 bsd_getthreadname(thread
->uthread
, extended_info
->pth_name
);
1986 thread_unlock(thread
);
1989 *thread_info_count
= THREAD_EXTENDED_INFO_COUNT
;
1991 return KERN_SUCCESS
;
1992 } else if (flavor
== THREAD_DEBUG_INFO_INTERNAL
) {
1993 #if DEVELOPMENT || DEBUG
1994 thread_debug_info_internal_t dbg_info
;
1995 if (*thread_info_count
< THREAD_DEBUG_INFO_INTERNAL_COUNT
) {
1996 return KERN_NOT_SUPPORTED
;
1999 if (thread_info_out
== NULL
) {
2000 return KERN_INVALID_ARGUMENT
;
2003 dbg_info
= __IGNORE_WCASTALIGN((thread_debug_info_internal_t
)thread_info_out
);
2004 dbg_info
->page_creation_count
= thread
->t_page_creation_count
;
2006 *thread_info_count
= THREAD_DEBUG_INFO_INTERNAL_COUNT
;
2007 return KERN_SUCCESS
;
2008 #endif /* DEVELOPMENT || DEBUG */
2009 return KERN_NOT_SUPPORTED
;
2012 return KERN_INVALID_ARGUMENT
;
2018 time_value_t
*user_time
,
2019 time_value_t
*system_time
,
2020 time_value_t
*runnable_time
)
2024 uint64_t tval_user
, tval_system
;
2026 tval_user
= timer_grab(&thread
->user_timer
);
2027 tval_system
= timer_grab(&thread
->system_timer
);
2029 if (thread
->precise_user_kernel_time
) {
2030 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
2031 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
2032 user_time
->microseconds
= usecs
;
2034 absolutetime_to_microtime(tval_system
, &secs
, &usecs
);
2035 system_time
->seconds
= (typeof(system_time
->seconds
))secs
;
2036 system_time
->microseconds
= usecs
;
2038 /* system_timer may represent either sys or user */
2039 tval_user
+= tval_system
;
2040 absolutetime_to_microtime(tval_user
, &secs
, &usecs
);
2041 user_time
->seconds
= (typeof(user_time
->seconds
))secs
;
2042 user_time
->microseconds
= usecs
;
2044 system_time
->seconds
= 0;
2045 system_time
->microseconds
= 0;
2048 if (runnable_time
) {
2049 uint64_t tval_runnable
= timer_grab(&thread
->runnable_timer
);
2050 absolutetime_to_microtime(tval_runnable
, &secs
, &usecs
);
2051 runnable_time
->seconds
= (typeof(runnable_time
->seconds
))secs
;
2052 runnable_time
->microseconds
= usecs
;
2057 thread_get_runtime_self(void)
2059 boolean_t interrupt_state
;
2061 thread_t thread
= NULL
;
2062 processor_t processor
= NULL
;
2064 thread
= current_thread();
2066 /* Not interrupt safe, as the scheduler may otherwise update timer values underneath us */
2067 interrupt_state
= ml_set_interrupts_enabled(FALSE
);
2068 processor
= current_processor();
2069 timer_update(processor
->thread_timer
, mach_absolute_time());
2070 runtime
= (timer_grab(&thread
->user_timer
) + timer_grab(&thread
->system_timer
));
2071 ml_set_interrupts_enabled(interrupt_state
);
2078 __unused thread_t thread
,
2079 __unused processor_set_t new_pset
)
2081 return KERN_FAILURE
;
2085 * thread_assign_default:
2087 * Special version of thread_assign for assigning threads to default
2091 thread_assign_default(
2094 return thread_assign(thread
, &pset0
);
2098 * thread_get_assignment
2100 * Return current assignment for this thread.
2103 thread_get_assignment(
2105 processor_set_t
*pset
)
2107 if (thread
== NULL
) {
2108 return KERN_INVALID_ARGUMENT
;
2113 return KERN_SUCCESS
;
2117 * thread_wire_internal:
2119 * Specify that the target thread must always be able
2120 * to run and to allocate memory.
2123 thread_wire_internal(
2124 host_priv_t host_priv
,
2127 boolean_t
*prev_state
)
2129 if (host_priv
== NULL
|| thread
!= current_thread()) {
2130 return KERN_INVALID_ARGUMENT
;
2134 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
2138 if (!(thread
->options
& TH_OPT_VMPRIV
)) {
2139 vm_page_free_reserve(1); /* XXX */
2141 thread
->options
|= TH_OPT_VMPRIV
;
2143 if (thread
->options
& TH_OPT_VMPRIV
) {
2144 vm_page_free_reserve(-1); /* XXX */
2146 thread
->options
&= ~TH_OPT_VMPRIV
;
2149 return KERN_SUCCESS
;
2156 * User-api wrapper for thread_wire_internal()
2160 host_priv_t host_priv
,
2164 return thread_wire_internal(host_priv
, thread
, wired
, NULL
);
2169 is_vm_privileged(void)
2171 return current_thread()->options
& TH_OPT_VMPRIV
? TRUE
: FALSE
;
2175 set_vm_privilege(boolean_t privileged
)
2177 boolean_t was_vmpriv
;
2179 if (current_thread()->options
& TH_OPT_VMPRIV
) {
2185 if (privileged
!= FALSE
) {
2186 current_thread()->options
|= TH_OPT_VMPRIV
;
2188 current_thread()->options
&= ~TH_OPT_VMPRIV
;
2195 set_thread_rwlock_boost(void)
2197 current_thread()->rwlock_count
++;
2201 clear_thread_rwlock_boost(void)
2203 thread_t thread
= current_thread();
2205 if ((thread
->rwlock_count
-- == 1) && (thread
->sched_flags
& TH_SFLAG_RW_PROMOTED
)) {
2206 lck_rw_clear_promotion(thread
, 0);
2211 * XXX assuming current thread only, for now...
2214 thread_guard_violation(thread_t thread
,
2215 mach_exception_data_type_t code
, mach_exception_data_type_t subcode
, boolean_t fatal
)
2217 assert(thread
== current_thread());
2219 /* Don't set up the AST for kernel threads; this check is needed to ensure
2220 * that the guard_exc_* fields in the thread structure are set only by the
2221 * current thread and therefore, don't require a lock.
2223 if (thread
->task
== kernel_task
) {
2227 assert(EXC_GUARD_DECODE_GUARD_TYPE(code
));
2230 * Use the saved state area of the thread structure
2231 * to store all info required to handle the AST when
2232 * returning to userspace. It's possible that there is
2233 * already a pending guard exception. If it's non-fatal,
2234 * it can only be over-written by a fatal exception code.
2236 if (thread
->guard_exc_info
.code
&& (thread
->guard_exc_fatal
|| !fatal
)) {
2240 thread
->guard_exc_info
.code
= code
;
2241 thread
->guard_exc_info
.subcode
= subcode
;
2242 thread
->guard_exc_fatal
= fatal
? 1 : 0;
2244 spl_t s
= splsched();
2245 thread_ast_set(thread
, AST_GUARD
);
2246 ast_propagate(thread
);
2253 * Handle AST_GUARD for a thread. This routine looks at the
2254 * state saved in the thread structure to determine the cause
2255 * of this exception. Based on this value, it invokes the
2256 * appropriate routine which determines other exception related
2257 * info and raises the exception.
2260 guard_ast(thread_t t
)
2262 const mach_exception_data_type_t
2263 code
= t
->guard_exc_info
.code
,
2264 subcode
= t
->guard_exc_info
.subcode
;
2266 t
->guard_exc_info
.code
= 0;
2267 t
->guard_exc_info
.subcode
= 0;
2268 t
->guard_exc_fatal
= 0;
2270 switch (EXC_GUARD_DECODE_GUARD_TYPE(code
)) {
2271 case GUARD_TYPE_NONE
:
2272 /* lingering AST_GUARD on the processor? */
2274 case GUARD_TYPE_MACH_PORT
:
2275 mach_port_guard_ast(t
, code
, subcode
);
2278 fd_guard_ast(t
, code
, subcode
);
2282 vn_guard_ast(t
, code
, subcode
);
2285 case GUARD_TYPE_VIRT_MEMORY
:
2286 virt_memory_guard_ast(t
, code
, subcode
);
2289 panic("guard_exc_info %llx %llx", code
, subcode
);
2294 thread_cputime_callback(int warning
, __unused
const void *arg0
, __unused
const void *arg1
)
2296 if (warning
== LEDGER_WARNING_ROSE_ABOVE
) {
2297 #if CONFIG_TELEMETRY
2299 * This thread is in danger of violating the CPU usage monitor. Enable telemetry
2300 * on the entire task so there are micro-stackshots available if and when
2301 * EXC_RESOURCE is triggered. We could have chosen to enable micro-stackshots
2302 * for this thread only; but now that this task is suspect, knowing what all of
2303 * its threads are up to will be useful.
2305 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 1);
2310 #if CONFIG_TELEMETRY
2312 * If the balance has dipped below the warning level (LEDGER_WARNING_DIPPED_BELOW) or
2313 * exceeded the limit, turn telemetry off for the task.
2315 telemetry_task_ctl(current_task(), TF_CPUMON_WARNING
, 0);
2319 SENDING_NOTIFICATION__THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU();
2323 void __attribute__((noinline
))
2324 SENDING_NOTIFICATION__THIS_THREAD_IS_CONSUMING_TOO_MUCH_CPU(void)
2327 task_t task
= current_task();
2328 thread_t thread
= current_thread();
2329 uint64_t tid
= thread
->thread_id
;
2330 const char *procname
= "unknown";
2331 time_value_t thread_total_time
= {0, 0};
2332 time_value_t thread_system_time
;
2333 time_value_t thread_user_time
;
2336 uint32_t usage_percent
= 0;
2337 uint32_t interval_sec
;
2338 uint64_t interval_ns
;
2339 uint64_t balance_ns
;
2340 boolean_t fatal
= FALSE
;
2341 boolean_t send_exc_resource
= TRUE
; /* in addition to RESOURCE_NOTIFY */
2344 #ifdef EXC_RESOURCE_MONITORS
2345 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
];
2346 #endif /* EXC_RESOURCE_MONITORS */
2347 struct ledger_entry_info lei
;
2349 assert(thread
->t_threadledger
!= LEDGER_NULL
);
2352 * Extract the fatal bit and suspend the monitor (which clears the bit).
2355 if (task
->rusage_cpu_flags
& TASK_RUSECPU_FLAGS_FATAL_CPUMON
) {
2357 send_exc_resource
= TRUE
;
2359 /* Only one thread can be here at a time. Whichever makes it through
2360 * first will successfully suspend the monitor and proceed to send the
2361 * notification. Other threads will get an error trying to suspend the
2362 * monitor and give up on sending the notification. In the first release,
2363 * the monitor won't be resumed for a number of seconds, but we may
2364 * eventually need to handle low-latency resume.
2366 kr
= task_suspend_cpumon(task
);
2368 if (kr
== KERN_INVALID_ARGUMENT
) {
2373 pid
= proc_selfpid();
2374 if (task
->bsd_info
!= NULL
) {
2375 procname
= proc_name_address(task
->bsd_info
);
2379 thread_get_cpulimit(&action
, &percentage
, &interval_ns
);
2381 interval_sec
= (uint32_t)(interval_ns
/ NSEC_PER_SEC
);
2383 thread_read_times(thread
, &thread_user_time
, &thread_system_time
, NULL
);
2384 time_value_add(&thread_total_time
, &thread_user_time
);
2385 time_value_add(&thread_total_time
, &thread_system_time
);
2386 ledger_get_entry_info(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &lei
);
2388 /* credit/debit/balance/limit are in absolute time units;
2389 * the refill info is in nanoseconds. */
2390 absolutetime_to_nanoseconds(lei
.lei_balance
, &balance_ns
);
2391 if (lei
.lei_last_refill
> 0) {
2392 usage_percent
= (uint32_t)((balance_ns
* 100ULL) / lei
.lei_last_refill
);
2395 /* TODO: show task total runtime (via TASK_ABSOLUTETIME_INFO)? */
2396 printf("process %s[%d] thread %llu caught burning CPU! It used more than %d%% CPU over %u seconds\n",
2397 procname
, pid
, tid
, percentage
, interval_sec
);
2398 printf(" (actual recent usage: %d%% over ~%llu seconds)\n",
2399 usage_percent
, (lei
.lei_last_refill
+ NSEC_PER_SEC
/ 2) / NSEC_PER_SEC
);
2400 printf(" Thread lifetime cpu usage %d.%06ds, (%d.%06d user, %d.%06d sys)\n",
2401 thread_total_time
.seconds
, thread_total_time
.microseconds
,
2402 thread_user_time
.seconds
, thread_user_time
.microseconds
,
2403 thread_system_time
.seconds
, thread_system_time
.microseconds
);
2404 printf(" Ledger balance: %lld; mabs credit: %lld; mabs debit: %lld\n",
2405 lei
.lei_balance
, lei
.lei_credit
, lei
.lei_debit
);
2406 printf(" mabs limit: %llu; mabs period: %llu ns; last refill: %llu ns%s.\n",
2407 lei
.lei_limit
, lei
.lei_refill_period
, lei
.lei_last_refill
,
2408 (fatal
? " [fatal violation]" : ""));
2411 * For now, send RESOURCE_NOTIFY in parallel with EXC_RESOURCE. Once
2412 * we have logging parity, we will stop sending EXC_RESOURCE (24508922).
2415 /* RESOURCE_NOTIFY MIG specifies nanoseconds of CPU time */
2416 lei
.lei_balance
= balance_ns
;
2417 absolutetime_to_nanoseconds(lei
.lei_limit
, &lei
.lei_limit
);
2418 trace_resource_violation(RMON_CPUUSAGE_VIOLATED
, &lei
);
2419 kr
= send_resource_violation(send_cpu_usage_violation
, task
, &lei
,
2420 fatal
? kRNFatalLimitFlag
: 0);
2422 printf("send_resource_violation(CPU usage, ...): error %#x\n", kr
);
2425 #ifdef EXC_RESOURCE_MONITORS
2426 if (send_exc_resource
) {
2427 if (disable_exc_resource
) {
2428 printf("process %s[%d] thread %llu caught burning CPU! "
2429 "EXC_RESOURCE%s supressed by a boot-arg\n",
2430 procname
, pid
, tid
, fatal
? " (and termination)" : "");
2435 printf("process %s[%d] thread %llu caught burning CPU! "
2436 "EXC_RESOURCE & termination supressed due to audio playback\n",
2437 procname
, pid
, tid
);
2443 if (send_exc_resource
) {
2444 code
[0] = code
[1] = 0;
2445 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_CPU
);
2447 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_CPU_MONITOR_FATAL
);
2449 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_CPU_MONITOR
);
2451 EXC_RESOURCE_CPUMONITOR_ENCODE_INTERVAL(code
[0], interval_sec
);
2452 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[0], percentage
);
2453 EXC_RESOURCE_CPUMONITOR_ENCODE_PERCENTAGE(code
[1], usage_percent
);
2454 exception_triage(EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
);
2456 #endif /* EXC_RESOURCE_MONITORS */
2460 jetsam_on_ledger_cpulimit_exceeded();
2462 task_terminate_internal(task
);
2467 #if DEVELOPMENT || DEBUG
2468 void __attribute__((noinline
))
2469 SENDING_NOTIFICATION__TASK_HAS_TOO_MANY_THREADS(task_t task
, int thread_count
)
2471 mach_exception_data_type_t code
[EXCEPTION_CODE_MAX
] = {0};
2472 int pid
= task_pid(task
);
2473 char procname
[MAXCOMLEN
+ 1] = "unknown";
2477 * Cannot suspend launchd
2482 proc_name(pid
, procname
, sizeof(procname
));
2484 if (disable_exc_resource
) {
2485 printf("process %s[%d] crossed thread count high watermark (%d), EXC_RESOURCE "
2486 "supressed by a boot-arg. \n", procname
, pid
, thread_count
);
2491 printf("process %s[%d] crossed thread count high watermark (%d), EXC_RESOURCE "
2492 "supressed due to audio playback.\n", procname
, pid
, thread_count
);
2496 if (exc_via_corpse_forking
== 0) {
2497 printf("process %s[%d] crossed thread count high watermark (%d), EXC_RESOURCE "
2498 "supressed due to corpse forking being disabled.\n", procname
, pid
,
2503 printf("process %s[%d] crossed thread count high watermark (%d), sending "
2504 "EXC_RESOURCE\n", procname
, pid
, thread_count
);
2506 EXC_RESOURCE_ENCODE_TYPE(code
[0], RESOURCE_TYPE_THREADS
);
2507 EXC_RESOURCE_ENCODE_FLAVOR(code
[0], FLAVOR_THREADS_HIGH_WATERMARK
);
2508 EXC_RESOURCE_THREADS_ENCODE_THREADS(code
[0], thread_count
);
2510 task_enqueue_exception_with_corpse(task
, EXC_RESOURCE
, code
, EXCEPTION_CODE_MAX
, NULL
);
2512 #endif /* DEVELOPMENT || DEBUG */
2515 thread_update_io_stats(thread_t thread
, int size
, int io_flags
)
2519 if (thread
->thread_io_stats
== NULL
|| thread
->task
->task_io_stats
== NULL
) {
2523 if (io_flags
& DKIO_READ
) {
2524 UPDATE_IO_STATS(thread
->thread_io_stats
->disk_reads
, size
);
2525 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->disk_reads
, size
);
2528 if (io_flags
& DKIO_META
) {
2529 UPDATE_IO_STATS(thread
->thread_io_stats
->metadata
, size
);
2530 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->metadata
, size
);
2533 if (io_flags
& DKIO_PAGING
) {
2534 UPDATE_IO_STATS(thread
->thread_io_stats
->paging
, size
);
2535 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->paging
, size
);
2538 io_tier
= ((io_flags
& DKIO_TIER_MASK
) >> DKIO_TIER_SHIFT
);
2539 assert(io_tier
< IO_NUM_PRIORITIES
);
2541 UPDATE_IO_STATS(thread
->thread_io_stats
->io_priority
[io_tier
], size
);
2542 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->io_priority
[io_tier
], size
);
2544 /* Update Total I/O Counts */
2545 UPDATE_IO_STATS(thread
->thread_io_stats
->total_io
, size
);
2546 UPDATE_IO_STATS_ATOMIC(thread
->task
->task_io_stats
->total_io
, size
);
2548 if (!(io_flags
& DKIO_READ
)) {
2549 DTRACE_IO3(physical_writes
, struct task
*, thread
->task
, uint32_t, size
, int, io_flags
);
2550 ledger_credit(thread
->task
->ledger
, task_ledgers
.physical_writes
, size
);
2555 init_thread_ledgers(void)
2557 ledger_template_t t
;
2560 assert(thread_ledger_template
== NULL
);
2562 if ((t
= ledger_template_create("Per-thread ledger")) == NULL
) {
2563 panic("couldn't create thread ledger template");
2566 if ((idx
= ledger_entry_add(t
, "cpu_time", "sched", "ns")) < 0) {
2567 panic("couldn't create cpu_time entry for thread ledger template");
2570 if (ledger_set_callback(t
, idx
, thread_cputime_callback
, NULL
, NULL
) < 0) {
2571 panic("couldn't set thread ledger callback for cpu_time entry");
2574 thread_ledgers
.cpu_time
= idx
;
2576 ledger_template_complete(t
);
2577 thread_ledger_template
= t
;
2581 * Returns currently applied CPU usage limit, or 0/0 if none is applied.
2584 thread_get_cpulimit(int *action
, uint8_t *percentage
, uint64_t *interval_ns
)
2586 int64_t abstime
= 0;
2587 uint64_t limittime
= 0;
2588 thread_t thread
= current_thread();
2594 if (thread
->t_threadledger
== LEDGER_NULL
) {
2596 * This thread has no per-thread ledger, so it can't possibly
2597 * have a CPU limit applied.
2599 return KERN_SUCCESS
;
2602 ledger_get_period(thread
->t_threadledger
, thread_ledgers
.cpu_time
, interval_ns
);
2603 ledger_get_limit(thread
->t_threadledger
, thread_ledgers
.cpu_time
, &abstime
);
2605 if ((abstime
== LEDGER_LIMIT_INFINITY
) || (*interval_ns
== 0)) {
2607 * This thread's CPU time ledger has no period or limit; so it
2608 * doesn't have a CPU limit applied.
2610 return KERN_SUCCESS
;
2614 * This calculation is the converse to the one in thread_set_cpulimit().
2616 absolutetime_to_nanoseconds(abstime
, &limittime
);
2617 *percentage
= (uint8_t)((limittime
* 100ULL) / *interval_ns
);
2618 assert(*percentage
<= 100);
2620 if (thread
->options
& TH_OPT_PROC_CPULIMIT
) {
2621 assert((thread
->options
& TH_OPT_PRVT_CPULIMIT
) == 0);
2623 *action
= THREAD_CPULIMIT_BLOCK
;
2624 } else if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
2625 assert((thread
->options
& TH_OPT_PROC_CPULIMIT
) == 0);
2627 *action
= THREAD_CPULIMIT_EXCEPTION
;
2629 *action
= THREAD_CPULIMIT_DISABLE
;
2632 return KERN_SUCCESS
;
2636 * Set CPU usage limit on a thread.
2638 * Calling with percentage of 0 will unset the limit for this thread.
2641 thread_set_cpulimit(int action
, uint8_t percentage
, uint64_t interval_ns
)
2643 thread_t thread
= current_thread();
2645 uint64_t limittime
= 0;
2646 uint64_t abstime
= 0;
2648 assert(percentage
<= 100);
2650 if (action
== THREAD_CPULIMIT_DISABLE
) {
2652 * Remove CPU limit, if any exists.
2654 if (thread
->t_threadledger
!= LEDGER_NULL
) {
2655 l
= thread
->t_threadledger
;
2656 ledger_set_limit(l
, thread_ledgers
.cpu_time
, LEDGER_LIMIT_INFINITY
, 0);
2657 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_IGNORE
);
2658 thread
->options
&= ~(TH_OPT_PROC_CPULIMIT
| TH_OPT_PRVT_CPULIMIT
);
2664 if (interval_ns
< MINIMUM_CPULIMIT_INTERVAL_MS
* NSEC_PER_MSEC
) {
2665 return KERN_INVALID_ARGUMENT
;
2668 l
= thread
->t_threadledger
;
2669 if (l
== LEDGER_NULL
) {
2671 * This thread doesn't yet have a per-thread ledger; so create one with the CPU time entry active.
2673 if ((l
= ledger_instantiate(thread_ledger_template
, LEDGER_CREATE_INACTIVE_ENTRIES
)) == LEDGER_NULL
) {
2674 return KERN_RESOURCE_SHORTAGE
;
2678 * We are the first to create this thread's ledger, so only activate our entry.
2680 ledger_entry_setactive(l
, thread_ledgers
.cpu_time
);
2681 thread
->t_threadledger
= l
;
2685 * The limit is specified as a percentage of CPU over an interval in nanoseconds.
2686 * Calculate the amount of CPU time that the thread needs to consume in order to hit the limit.
2688 limittime
= (interval_ns
* percentage
) / 100;
2689 nanoseconds_to_absolutetime(limittime
, &abstime
);
2690 ledger_set_limit(l
, thread_ledgers
.cpu_time
, abstime
, cpumon_ustackshots_trigger_pct
);
2692 * Refill the thread's allotted CPU time every interval_ns nanoseconds.
2694 ledger_set_period(l
, thread_ledgers
.cpu_time
, interval_ns
);
2696 if (action
== THREAD_CPULIMIT_EXCEPTION
) {
2698 * We don't support programming the CPU usage monitor on a task if any of its
2699 * threads have a per-thread blocking CPU limit configured.
2701 if (thread
->options
& TH_OPT_PRVT_CPULIMIT
) {
2702 panic("CPU usage monitor activated, but blocking thread limit exists");
2706 * Make a note that this thread's CPU limit is being used for the task-wide CPU
2707 * usage monitor. We don't have to arm the callback which will trigger the
2708 * exception, because that was done for us in ledger_instantiate (because the
2709 * ledger template used has a default callback).
2711 thread
->options
|= TH_OPT_PROC_CPULIMIT
;
2714 * We deliberately override any CPU limit imposed by a task-wide limit (eg
2715 * CPU usage monitor).
2717 thread
->options
&= ~TH_OPT_PROC_CPULIMIT
;
2719 thread
->options
|= TH_OPT_PRVT_CPULIMIT
;
2720 /* The per-thread ledger template by default has a callback for CPU time */
2721 ledger_disable_callback(l
, thread_ledgers
.cpu_time
);
2722 ledger_set_action(l
, thread_ledgers
.cpu_time
, LEDGER_ACTION_BLOCK
);
2733 assert((thread
->state
& TH_WAIT_REPORT
) == 0);
2734 thread
->sched_call
= call
;
2741 return thread
!= THREAD_NULL
? thread
->thread_id
: 0;
2745 thread_set_tag(thread_t th
, uint16_t tag
)
2747 return thread_set_tag_internal(th
, tag
);
2751 thread_get_tag(thread_t th
)
2753 return thread_get_tag_internal(th
);
2757 thread_last_run_time(thread_t th
)
2759 return th
->last_run_time
;
2763 thread_dispatchqaddr(
2766 uint64_t dispatchqueue_addr
;
2767 uint64_t thread_handle
;
2769 if (thread
== THREAD_NULL
) {
2773 thread_handle
= thread
->machine
.cthread_self
;
2774 if (thread_handle
== 0) {
2778 if (thread
->inspection
== TRUE
) {
2779 dispatchqueue_addr
= thread_handle
+ get_task_dispatchqueue_offset(thread
->task
);
2780 } else if (thread
->task
->bsd_info
) {
2781 dispatchqueue_addr
= thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
2783 dispatchqueue_addr
= 0;
2786 return dispatchqueue_addr
;
2790 thread_rettokern_addr(
2793 uint64_t rettokern_addr
;
2794 uint64_t rettokern_offset
;
2795 uint64_t thread_handle
;
2797 if (thread
== THREAD_NULL
) {
2801 thread_handle
= thread
->machine
.cthread_self
;
2802 if (thread_handle
== 0) {
2806 if (thread
->task
->bsd_info
) {
2807 rettokern_offset
= get_return_to_kernel_offset_from_proc(thread
->task
->bsd_info
);
2809 /* Return 0 if return to kernel offset is not initialized. */
2810 if (rettokern_offset
== 0) {
2813 rettokern_addr
= thread_handle
+ rettokern_offset
;
2819 return rettokern_addr
;
2823 * Export routines to other components for things that are done as macros
2824 * within the osfmk component.
2827 #undef thread_mtx_lock
2828 void thread_mtx_lock(thread_t thread
);
2830 thread_mtx_lock(thread_t thread
)
2832 lck_mtx_lock(&thread
->mutex
);
2835 #undef thread_mtx_unlock
2836 void thread_mtx_unlock(thread_t thread
);
2838 thread_mtx_unlock(thread_t thread
)
2840 lck_mtx_unlock(&thread
->mutex
);
2843 #undef thread_reference
2844 void thread_reference(thread_t thread
);
2849 if (thread
!= THREAD_NULL
) {
2850 thread_reference_internal(thread
);
2854 #undef thread_should_halt
2860 return thread_should_halt_fast(th
);
2864 * thread_set_voucher_name - reset the voucher port name bound to this thread
2866 * Conditions: nothing locked
2870 thread_set_voucher_name(mach_port_name_t voucher_name
)
2872 thread_t thread
= current_thread();
2873 ipc_voucher_t new_voucher
= IPC_VOUCHER_NULL
;
2874 ipc_voucher_t voucher
;
2875 ledger_t bankledger
= NULL
;
2876 struct thread_group
*banktg
= NULL
;
2877 uint32_t persona_id
= 0;
2879 if (MACH_PORT_DEAD
== voucher_name
) {
2880 return KERN_INVALID_RIGHT
;
2884 * agressively convert to voucher reference
2886 if (MACH_PORT_VALID(voucher_name
)) {
2887 new_voucher
= convert_port_name_to_voucher(voucher_name
);
2888 if (IPC_VOUCHER_NULL
== new_voucher
) {
2889 return KERN_INVALID_ARGUMENT
;
2892 bank_get_bank_ledger_thread_group_and_persona(new_voucher
, &bankledger
, &banktg
, &persona_id
);
2894 thread_mtx_lock(thread
);
2895 voucher
= thread
->ith_voucher
;
2896 thread
->ith_voucher_name
= voucher_name
;
2897 thread
->ith_voucher
= new_voucher
;
2898 thread_mtx_unlock(thread
);
2900 bank_swap_thread_bank_ledger(thread
, bankledger
);
2901 #if CONFIG_THREAD_GROUPS
2902 thread_group_set_bank(thread
, banktg
);
2903 #endif /* CONFIG_THREAD_GROUPS */
2905 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
2906 MACHDBG_CODE(DBG_MACH_IPC
, MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
2907 (uintptr_t)thread_tid(thread
),
2908 (uintptr_t)voucher_name
,
2909 VM_KERNEL_ADDRPERM((uintptr_t)new_voucher
),
2912 if (IPC_VOUCHER_NULL
!= voucher
) {
2913 ipc_voucher_release(voucher
);
2916 return KERN_SUCCESS
;
2920 * thread_get_mach_voucher - return a voucher reference for the specified thread voucher
2922 * Conditions: nothing locked
2924 * NOTE: At the moment, there is no distinction between the current and effective
2925 * vouchers because we only set them at the thread level currently.
2928 thread_get_mach_voucher(
2929 thread_act_t thread
,
2930 mach_voucher_selector_t __unused which
,
2931 ipc_voucher_t
*voucherp
)
2933 ipc_voucher_t voucher
;
2935 if (THREAD_NULL
== thread
) {
2936 return KERN_INVALID_ARGUMENT
;
2939 thread_mtx_lock(thread
);
2940 voucher
= thread
->ith_voucher
;
2942 if (IPC_VOUCHER_NULL
!= voucher
) {
2943 ipc_voucher_reference(voucher
);
2944 thread_mtx_unlock(thread
);
2945 *voucherp
= voucher
;
2946 return KERN_SUCCESS
;
2949 thread_mtx_unlock(thread
);
2951 *voucherp
= IPC_VOUCHER_NULL
;
2952 return KERN_SUCCESS
;
2956 * thread_set_mach_voucher - set a voucher reference for the specified thread voucher
2958 * Conditions: callers holds a reference on the voucher.
2961 * We grab another reference to the voucher and bind it to the thread.
2962 * The old voucher reference associated with the thread is
2966 thread_set_mach_voucher(
2968 ipc_voucher_t voucher
)
2970 ipc_voucher_t old_voucher
;
2971 ledger_t bankledger
= NULL
;
2972 struct thread_group
*banktg
= NULL
;
2973 uint32_t persona_id
= 0;
2975 if (THREAD_NULL
== thread
) {
2976 return KERN_INVALID_ARGUMENT
;
2979 bank_get_bank_ledger_thread_group_and_persona(voucher
, &bankledger
, &banktg
, &persona_id
);
2981 thread_mtx_lock(thread
);
2983 * Once the thread is started, we will look at `ith_voucher` without
2986 * Setting the voucher hence can only be done by current_thread() or
2987 * before it started. "started" flips under the thread mutex and must be
2988 * tested under it too.
2990 if (thread
!= current_thread() && thread
->started
) {
2991 thread_mtx_unlock(thread
);
2992 return KERN_INVALID_ARGUMENT
;
2995 ipc_voucher_reference(voucher
);
2996 old_voucher
= thread
->ith_voucher
;
2997 thread
->ith_voucher
= voucher
;
2998 thread
->ith_voucher_name
= MACH_PORT_NULL
;
2999 thread_mtx_unlock(thread
);
3001 bank_swap_thread_bank_ledger(thread
, bankledger
);
3002 #if CONFIG_THREAD_GROUPS
3003 thread_group_set_bank(thread
, banktg
);
3004 #endif /* CONFIG_THREAD_GROUPS */
3006 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
3007 MACHDBG_CODE(DBG_MACH_IPC
, MACH_THREAD_SET_VOUCHER
) | DBG_FUNC_NONE
,
3008 (uintptr_t)thread_tid(thread
),
3009 (uintptr_t)MACH_PORT_NULL
,
3010 VM_KERNEL_ADDRPERM((uintptr_t)voucher
),
3013 ipc_voucher_release(old_voucher
);
3015 return KERN_SUCCESS
;
3019 * thread_swap_mach_voucher - swap a voucher reference for the specified thread voucher
3021 * Conditions: callers holds a reference on the new and presumed old voucher(s).
3024 * This function is no longer supported.
3027 thread_swap_mach_voucher(
3028 __unused thread_t thread
,
3029 __unused ipc_voucher_t new_voucher
,
3030 ipc_voucher_t
*in_out_old_voucher
)
3033 * Currently this function is only called from a MIG generated
3034 * routine which doesn't release the reference on the voucher
3035 * addressed by in_out_old_voucher. To avoid leaking this reference,
3036 * a call to release it has been added here.
3038 ipc_voucher_release(*in_out_old_voucher
);
3039 return KERN_NOT_SUPPORTED
;
3043 * thread_get_current_voucher_origin_pid - get the pid of the originator of the current voucher.
3046 thread_get_current_voucher_origin_pid(
3051 thread_t thread
= current_thread();
3053 buf_size
= sizeof(*pid
);
3054 kr
= mach_voucher_attr_command(thread
->ith_voucher
,
3055 MACH_VOUCHER_ATTR_KEY_BANK
,
3056 BANK_ORIGINATOR_PID
,
3059 (mach_voucher_attr_content_t
)pid
,
3065 #if CONFIG_THREAD_GROUPS
3067 * Returns the current thread's voucher-carried thread group
3069 * Reference is borrowed from this being the current voucher, so it does NOT
3070 * return a reference to the group.
3072 struct thread_group
*
3073 thread_get_current_voucher_thread_group(thread_t thread
)
3075 assert(thread
== current_thread());
3077 if (thread
->ith_voucher
== NULL
) {
3081 ledger_t bankledger
= NULL
;
3082 struct thread_group
*banktg
= NULL
;
3084 bank_get_bank_ledger_thread_group_and_persona(thread
->ith_voucher
, &bankledger
, &banktg
, NULL
);
3089 #endif /* CONFIG_THREAD_GROUPS */
3092 thread_has_thread_name(thread_t th
)
3094 if ((th
) && (th
->uthread
)) {
3095 return bsd_hasthreadname(th
->uthread
);
3099 * This is an odd case; clients may set the thread name based on the lack of
3100 * a name, but in this context there is no uthread to attach the name to.
3106 thread_set_thread_name(thread_t th
, const char* name
)
3108 if ((th
) && (th
->uthread
) && name
) {
3109 bsd_setthreadname(th
->uthread
, name
);
3114 thread_get_thread_name(thread_t th
, char* name
)
3119 if ((th
) && (th
->uthread
)) {
3120 bsd_getthreadname(th
->uthread
, name
);
3127 thread_set_honor_qlimit(thread_t thread
)
3129 thread
->options
|= TH_OPT_HONOR_QLIMIT
;
3133 thread_clear_honor_qlimit(thread_t thread
)
3135 thread
->options
&= (~TH_OPT_HONOR_QLIMIT
);
3139 * thread_enable_send_importance - set/clear the SEND_IMPORTANCE thread option bit.
3142 thread_enable_send_importance(thread_t thread
, boolean_t enable
)
3144 if (enable
== TRUE
) {
3145 thread
->options
|= TH_OPT_SEND_IMPORTANCE
;
3147 thread
->options
&= ~TH_OPT_SEND_IMPORTANCE
;
3152 * thread_set_allocation_name - .
3155 kern_allocation_name_t
3156 thread_set_allocation_name(kern_allocation_name_t new_name
)
3158 kern_allocation_name_t ret
;
3159 thread_kernel_state_t kstate
= thread_get_kernel_state(current_thread());
3160 ret
= kstate
->allocation_name
;
3162 if (!new_name
|| !kstate
->allocation_name
) {
3163 kstate
->allocation_name
= new_name
;
3169 thread_iokit_tls_get(uint32_t index
)
3171 assert(index
< THREAD_SAVE_IOKIT_TLS_COUNT
);
3172 return current_thread()->saved
.iokit
.tls
[index
];
3176 thread_iokit_tls_set(uint32_t index
, void * data
)
3178 assert(index
< THREAD_SAVE_IOKIT_TLS_COUNT
);
3179 current_thread()->saved
.iokit
.tls
[index
] = data
;
3183 thread_get_last_wait_duration(thread_t thread
)
3185 return thread
->last_made_runnable_time
- thread
->last_run_time
;
3189 thread_kern_get_pri(thread_t thr
)
3191 return thr
->base_pri
;
3195 thread_kern_set_pri(thread_t thr
, integer_t pri
)
3197 sched_set_kernel_thread_priority(thr
, pri
);
3201 thread_kern_get_kernel_maxpri(void)
3203 return MAXPRI_KERNEL
;
3206 * thread_port_with_flavor_notify
3208 * Called whenever the Mach port system detects no-senders on
3209 * the thread inspect or read port. These ports are allocated lazily and
3210 * should be deallocated here when there are no senders remaining.
3213 thread_port_with_flavor_notify(mach_msg_header_t
*msg
)
3215 mach_no_senders_notification_t
*notification
= (void *)msg
;
3216 ipc_port_t port
= notification
->not_header
.msgh_remote_port
;
3218 mach_thread_flavor_t flavor
;
3219 ipc_kobject_type_t kotype
;
3222 if (port
->ip_srights
> 0) {
3226 thread
= (thread_t
)ipc_kobject_get(port
);
3227 kotype
= ip_kotype(port
);
3228 if (thread
!= THREAD_NULL
) {
3229 assert((IKOT_THREAD_READ
== kotype
) || (IKOT_THREAD_INSPECT
== kotype
));
3230 thread_reference_internal(thread
);
3234 if (thread
== THREAD_NULL
) {
3235 /* The thread is exiting or disabled; it will eventually deallocate the port */
3239 if (kotype
== IKOT_THREAD_READ
) {
3240 flavor
= THREAD_FLAVOR_READ
;
3242 flavor
= THREAD_FLAVOR_INSPECT
;
3245 thread_mtx_lock(thread
);
3248 * If the port is no longer active, then ipc_thread_terminate() ran
3249 * and destroyed the kobject already. Just deallocate the task
3250 * ref we took and go away.
3252 * It is also possible that several nsrequests are in flight,
3253 * only one shall NULL-out the port entry, and this is the one
3254 * that gets to dealloc the port.
3256 * Check for a stale no-senders notification. A call to any function
3257 * that vends out send rights to this port could resurrect it between
3258 * this notification being generated and actually being handled here.
3260 if (!ip_active(port
) ||
3261 thread
->ith_thread_ports
[flavor
] != port
||
3262 port
->ip_srights
> 0) {
3264 thread_mtx_unlock(thread
);
3265 thread_deallocate(thread
);
3269 assert(thread
->ith_thread_ports
[flavor
] == port
);
3270 thread
->ith_thread_ports
[flavor
] = IP_NULL
;
3271 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
3273 thread_mtx_unlock(thread
);
3274 thread_deallocate(thread
);
3276 ipc_port_dealloc_kernel(port
);
3280 * The 'thread_region_page_shift' is used by footprint
3281 * to specify the page size that it will use to
3282 * accomplish its accounting work on the task being
3283 * inspected. Since footprint uses a thread for each
3284 * task that it works on, we need to keep the page_shift
3285 * on a per-thread basis.
3289 thread_self_region_page_shift(void)
3292 * Return the page shift that this thread
3293 * would like to use for its accounting work.
3295 return current_thread()->thread_region_page_shift
;
3299 thread_self_region_page_shift_set(
3303 * Set the page shift that this thread
3304 * would like to use for its accounting work
3305 * when dealing with a task.
3307 current_thread()->thread_region_page_shift
= pgshift
;
3312 dtrace_get_thread_predcache(thread_t thread
)
3314 if (thread
!= THREAD_NULL
) {
3315 return thread
->t_dtrace_predcache
;
3322 dtrace_get_thread_vtime(thread_t thread
)
3324 if (thread
!= THREAD_NULL
) {
3325 return thread
->t_dtrace_vtime
;
3332 dtrace_get_thread_last_cpu_id(thread_t thread
)
3334 if ((thread
!= THREAD_NULL
) && (thread
->last_processor
!= PROCESSOR_NULL
)) {
3335 return thread
->last_processor
->cpu_id
;
3342 dtrace_get_thread_tracing(thread_t thread
)
3344 if (thread
!= THREAD_NULL
) {
3345 return thread
->t_dtrace_tracing
;
3352 dtrace_get_thread_inprobe(thread_t thread
)
3354 if (thread
!= THREAD_NULL
) {
3355 return thread
->t_dtrace_inprobe
;
3362 dtrace_get_kernel_stack(thread_t thread
)
3364 if (thread
!= THREAD_NULL
) {
3365 return thread
->kernel_stack
;
3372 struct kasan_thread_data
*
3373 kasan_get_thread_data(thread_t thread
)
3375 return &thread
->kasan_data
;
3381 __sanitizer_get_thread_data(thread_t thread
)
3383 return &thread
->ksancov_data
;
3388 dtrace_calc_thread_recent_vtime(thread_t thread
)
3390 if (thread
!= THREAD_NULL
) {
3391 processor_t processor
= current_processor();
3392 uint64_t abstime
= mach_absolute_time();
3395 timer
= processor
->thread_timer
;
3397 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
)) +
3398 (abstime
- timer
->tstamp
); /* XXX need interrupts off to prevent missed time? */
3405 dtrace_set_thread_predcache(thread_t thread
, uint32_t predcache
)
3407 if (thread
!= THREAD_NULL
) {
3408 thread
->t_dtrace_predcache
= predcache
;
3413 dtrace_set_thread_vtime(thread_t thread
, int64_t vtime
)
3415 if (thread
!= THREAD_NULL
) {
3416 thread
->t_dtrace_vtime
= vtime
;
3421 dtrace_set_thread_tracing(thread_t thread
, int64_t accum
)
3423 if (thread
!= THREAD_NULL
) {
3424 thread
->t_dtrace_tracing
= accum
;
3429 dtrace_set_thread_inprobe(thread_t thread
, uint16_t inprobe
)
3431 if (thread
!= THREAD_NULL
) {
3432 thread
->t_dtrace_inprobe
= inprobe
;
3437 dtrace_set_thread_recover(thread_t thread
, vm_offset_t recover
)
3439 vm_offset_t prev
= 0;
3441 if (thread
!= THREAD_NULL
) {
3442 prev
= thread
->recover
;
3443 thread
->recover
= recover
;
3449 dtrace_sign_and_set_thread_recover(thread_t thread
, vm_offset_t recover
)
3451 #if defined(HAS_APPLE_PAC)
3452 return dtrace_set_thread_recover(thread
,
3453 (vm_address_t
)ptrauth_sign_unauthenticated((void *)recover
,
3454 ptrauth_key_function_pointer
,
3455 ptrauth_blend_discriminator(&thread
->recover
, PAC_DISCRIMINATOR_RECOVER
)));
3456 #else /* defined(HAS_APPLE_PAC) */
3457 return dtrace_set_thread_recover(thread
, recover
);
3458 #endif /* defined(HAS_APPLE_PAC) */
3462 dtrace_thread_bootstrap(void)
3464 task_t task
= current_task();
3466 if (task
->thread_count
== 1) {
3467 thread_t thread
= current_thread();
3468 if (thread
->t_dtrace_flags
& TH_DTRACE_EXECSUCCESS
) {
3469 thread
->t_dtrace_flags
&= ~TH_DTRACE_EXECSUCCESS
;
3470 DTRACE_PROC(exec__success
);
3471 KDBG(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_EXEC
),
3476 DTRACE_PROC(lwp__start
);
3480 dtrace_thread_didexec(thread_t thread
)
3482 thread
->t_dtrace_flags
|= TH_DTRACE_EXECSUCCESS
;
3484 #endif /* CONFIG_DTRACE */