2 * Copyright (c) 2000-2019 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.
62 * This file contains the structure definitions for threads.
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 #ifndef _KERN_THREAD_H_
85 #define _KERN_THREAD_H_
87 #include <mach/kern_return.h>
88 #include <mach/mach_types.h>
89 #include <mach/message.h>
90 #include <mach/boolean.h>
91 #include <mach/vm_param.h>
92 #include <mach/thread_info.h>
93 #include <mach/thread_status.h>
94 #include <mach/exception_types.h>
96 #include <kern/kern_types.h>
97 #include <vm/vm_kern.h>
99 #include <sys/cdefs.h>
101 #ifdef MACH_KERNEL_PRIVATE
103 #include <mach_assert.h>
104 #include <mach_ldebug.h>
106 #include <ipc/ipc_types.h>
108 #include <mach/port.h>
109 #include <kern/cpu_number.h>
110 #include <kern/smp.h>
111 #include <kern/queue.h>
113 #include <kern/timer.h>
114 #include <kern/simple_lock.h>
115 #include <kern/locks.h>
116 #include <kern/sched.h>
117 #include <kern/sched_prim.h>
118 #include <mach/sfi_class.h>
119 #include <kern/thread_call.h>
120 #include <kern/thread_group.h>
121 #include <kern/timer_call.h>
122 #include <kern/task.h>
123 #include <kern/exception.h>
124 #include <kern/affinity.h>
125 #include <kern/debug.h>
126 #include <kern/block_hint.h>
127 #include <kern/turnstile.h>
128 #include <kern/mpsc_queue.h>
130 #include <kern/waitq.h>
131 #include <san/kasan.h>
132 #include <os/refcnt.h>
134 #include <ipc/ipc_kmsg.h>
136 #include <machine/atomic.h>
137 #include <machine/cpu_data.h>
138 #include <machine/thread.h>
140 #ifdef XNU_KERNEL_PRIVATE
141 /* priority queue static asserts fail for __ARM64_ARCH_8_32__ kext builds */
142 #include <kern/priority_queue.h>
143 #endif /* XNU_KERNEL_PRIVATE */
146 #include <stdatomic.h>
147 #include <machine/monotonic.h>
148 #endif /* MONOTONIC */
151 /* Taskwatch related. TODO: find this a better home */
152 typedef struct task_watcher task_watch_t
;
153 #endif /* CONFIG_EMBEDDED */
157 #define THREAD_MAGIC 0x1234ABCDDCBA4321ULL
158 /* Ensure nothing uses &thread as a queue entry */
159 uint64_t thread_magic
;
160 #endif /* MACH_ASSERT */
163 * NOTE: The runq field in the thread structure has an unusual
164 * locking protocol. If its value is PROCESSOR_NULL, then it is
165 * locked by the thread_lock, but if its value is something else
166 * then it is locked by the associated run queue lock. It is
167 * set to PROCESSOR_NULL without holding the thread lock, but the
168 * transition from PROCESSOR_NULL to non-null must be done
169 * under the thread lock and the run queue lock.
171 * New waitq APIs allow the 'links' and 'runq' fields to be
172 * anywhere in the thread structure.
175 queue_chain_t runq_links
; /* run queue links */
176 queue_chain_t wait_links
; /* wait queue links */
177 struct mpsc_queue_chain mpsc_links
; /* thread daemon mpsc links */
178 struct priority_queue_entry wait_prioq_links
; /* priority ordered waitq links */
181 event64_t wait_event
; /* wait queue event */
182 processor_t runq
; /* run queue assignment */
183 struct waitq
*waitq
; /* wait queue this thread is enqueued on */
184 struct turnstile
*turnstile
; /* thread's turnstile, protected by primitives interlock */
185 void *inheritor
; /* inheritor of the primitive the thread will block on */
186 struct priority_queue sched_inheritor_queue
; /* Inheritor queue for kernel promotion */
187 struct priority_queue base_inheritor_queue
; /* Inheritor queue for user promotion */
189 #if CONFIG_SCHED_CLUTCH
191 * In the clutch scheduler, the threads are maintained in runqs at the clutch_bucket
192 * level (clutch_bucket defines a unique thread group and scheduling bucket pair). In
193 * order to determine the priority of the clutch bucket as a whole, it is necessary to
194 * find the highest thread in it. The thread could be present in the clutch bucket due
195 * to its base_pri or its promoted pri. This link is used to maintain that queue.
197 struct priority_queue_entry sched_clutchpri_link
;
199 #endif /* CONFIG_SCHED_CLUTCH */
201 /* Data updated during assert_wait/thread_wakeup */
203 decl_simple_lock_data(, sched_lock
); /* scheduling lock (thread_lock()) */
204 decl_simple_lock_data(, wake_lock
); /* for thread stop / wait (wake_lock()) */
206 uint16_t options
; /* options set by thread itself */
207 #define TH_OPT_INTMASK 0x0003 /* interrupt / abort level */
208 #define TH_OPT_VMPRIV 0x0004 /* may allocate reserved memory */
209 #define TH_OPT_SYSTEM_CRITICAL 0x0010 /* Thread must always be allowed to run - even under heavy load */
210 #define TH_OPT_PROC_CPULIMIT 0x0020 /* Thread has a task-wide CPU limit applied to it */
211 #define TH_OPT_PRVT_CPULIMIT 0x0040 /* Thread has a thread-private CPU limit applied to it */
212 #define TH_OPT_IDLE_THREAD 0x0080 /* Thread is a per-processor idle thread */
213 #define TH_OPT_GLOBAL_FORCED_IDLE 0x0100 /* Thread performs forced idle for thermal control */
214 #define TH_OPT_SCHED_VM_GROUP 0x0200 /* Thread belongs to special scheduler VM group */
215 #define TH_OPT_HONOR_QLIMIT 0x0400 /* Thread will honor qlimit while sending mach_msg, regardless of MACH_SEND_ALWAYS */
216 #define TH_OPT_SEND_IMPORTANCE 0x0800 /* Thread will allow importance donation from kernel rpc */
217 #define TH_OPT_ZONE_GC 0x1000 /* zone_gc() called on this thread */
219 bool wake_active
; /* wake event on stop */
220 bool at_safe_point
; /* thread_abort_safely allowed */
221 ast_t reason
; /* why we blocked */
222 uint32_t quantum_remaining
;
223 wait_result_t wait_result
; /* outcome of wait -
224 * may be examined by this thread
226 thread_continue_t continuation
; /* continue here next dispatch */
227 void *parameter
; /* continuation parameter */
229 /* Data updated/used in thread_invoke */
230 vm_offset_t kernel_stack
; /* current kernel stack */
231 vm_offset_t reserved_stack
; /* reserved kernel stack */
234 struct kasan_thread_data kasan_data
;
244 * Thread states [bits or'ed]
246 #define TH_WAIT 0x01 /* queued for waiting */
247 #define TH_SUSP 0x02 /* stopped or requested to stop */
248 #define TH_RUN 0x04 /* running or on runq */
249 #define TH_UNINT 0x08 /* waiting uninteruptibly */
250 #define TH_TERMINATE 0x10 /* halted at termination */
251 #define TH_TERMINATE2 0x20 /* added to termination queue */
252 #define TH_WAIT_REPORT 0x40 /* the wait is using the sched_call,
253 * only set if TH_WAIT is also set */
254 #define TH_IDLE 0x80 /* idling processor */
256 /* Scheduling information */
257 sched_mode_t sched_mode
; /* scheduling mode */
258 sched_mode_t saved_mode
; /* saved mode during forced mode demotion */
260 /* This thread's contribution to global sched counters */
261 sched_bucket_t th_sched_bucket
;
263 sfi_class_id_t sfi_class
; /* SFI class (XXX Updated on CSW/QE/AST) */
264 sfi_class_id_t sfi_wait_class
; /* Currently in SFI wait for this class, protected by sfi_lock */
267 uint32_t sched_flags
; /* current flag bits */
268 #define TH_SFLAG_NO_SMT 0x0001 /* On an SMT CPU, this thread must be scheduled alone */
269 #define TH_SFLAG_FAILSAFE 0x0002 /* fail-safe has tripped */
270 #define TH_SFLAG_THROTTLED 0x0004 /* throttled thread forced to timeshare mode (may be applied in addition to failsafe) */
271 #define TH_SFLAG_DEMOTED_MASK (TH_SFLAG_THROTTLED | TH_SFLAG_FAILSAFE) /* saved_mode contains previous sched_mode */
273 #define TH_SFLAG_PROMOTED 0x0008 /* sched pri has been promoted by kernel mutex priority promotion */
274 #define TH_SFLAG_ABORT 0x0010 /* abort interruptible waits */
275 #define TH_SFLAG_ABORTSAFELY 0x0020 /* ... but only those at safe point */
276 #define TH_SFLAG_ABORTED_MASK (TH_SFLAG_ABORT | TH_SFLAG_ABORTSAFELY)
277 #define TH_SFLAG_DEPRESS 0x0040 /* normal depress yield */
278 #define TH_SFLAG_POLLDEPRESS 0x0080 /* polled depress yield */
279 #define TH_SFLAG_DEPRESSED_MASK (TH_SFLAG_DEPRESS | TH_SFLAG_POLLDEPRESS)
280 /* unused TH_SFLAG_PRI_UPDATE 0x0100 */
281 #define TH_SFLAG_EAGERPREEMPT 0x0200 /* Any preemption of this thread should be treated as if AST_URGENT applied */
282 #define TH_SFLAG_RW_PROMOTED 0x0400 /* promote reason: blocking with RW lock held */
283 #define TH_SFLAG_BASE_PRI_FROZEN 0x0800 /* (effective) base_pri is frozen */
284 #define TH_SFLAG_WAITQ_PROMOTED 0x1000 /* promote reason: waitq wakeup (generally for IPC receive) */
287 #define TH_SFLAG_ECORE_ONLY 0x2000 /* Bind thread to E core processor set */
288 #define TH_SFLAG_PCORE_ONLY 0x4000 /* Bind thread to P core processor set */
291 #define TH_SFLAG_EXEC_PROMOTED 0x8000 /* promote reason: thread is in an exec */
293 /* 'promote reasons' that request a priority floor only, not a custom priority */
294 #define TH_SFLAG_PROMOTE_REASON_MASK (TH_SFLAG_RW_PROMOTED | TH_SFLAG_WAITQ_PROMOTED | TH_SFLAG_EXEC_PROMOTED)
296 #define TH_SFLAG_RW_PROMOTED_BIT (10) /* 0x400 */
298 int16_t sched_pri
; /* scheduled (current) priority */
299 int16_t base_pri
; /* effective base priority (equal to req_base_pri unless TH_SFLAG_BASE_PRI_FROZEN) */
300 int16_t req_base_pri
; /* requested base priority */
301 int16_t max_priority
; /* copy of max base priority */
302 int16_t task_priority
; /* copy of task base priority */
303 int16_t promotion_priority
; /* priority thread is currently promoted to */
305 #if defined(CONFIG_SCHED_GRRR)
307 uint16_t grrr_deficit
; /* fixed point (1/1000th quantum) fractional deficit */
311 int iotier_override
; /* atomic operations to set, cleared on ret to user */
312 os_refcnt_t ref_count
; /* number of references to me */
314 lck_mtx_t
* waiting_for_mutex
; /* points to mutex we're waiting for until we acquire it */
316 uint32_t rwlock_count
; /* Number of lck_rw_t locks held by thread */
318 integer_t importance
; /* task-relative importance */
320 /* Priority depression expiration */
321 integer_t depress_timer_active
;
322 timer_call_data_t depress_timer
;
323 /* real-time parameters */
324 struct { /* see mach/thread_policy.h */
326 uint32_t computation
;
328 boolean_t preemptible
;
332 uint64_t last_run_time
; /* time when thread was switched away from */
333 uint64_t last_made_runnable_time
; /* time when thread was unblocked or preempted */
334 uint64_t last_basepri_change_time
; /* time when thread was last changed in basepri while runnable */
335 uint64_t same_pri_latency
;
336 #define THREAD_NOT_RUNNABLE (~0ULL)
339 #if defined(CONFIG_SCHED_MULTIQ)
340 sched_group_t sched_group
;
341 #endif /* defined(CONFIG_SCHED_MULTIQ) */
343 /* Data used during setrun/dispatch */
344 timer_data_t system_timer
; /* system mode timer */
345 processor_t bound_processor
; /* bound to a processor? */
346 processor_t last_processor
; /* processor last dispatched on */
347 processor_t chosen_processor
; /* Where we want to run this thread */
349 /* Fail-safe computation since last unblock or qualifying yield */
350 uint64_t computation_metered
;
351 uint64_t computation_epoch
;
352 uint64_t safe_release
; /* when to release fail-safe */
354 /* Call out from scheduler */
355 void (*sched_call
)(int type
, thread_t thread
);
357 #if defined(CONFIG_SCHED_PROTO)
358 uint32_t runqueue_generation
; /* last time runqueue was drained */
361 /* Statistics and timesharing calculations */
362 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
363 natural_t sched_stamp
; /* last scheduler tick */
364 natural_t sched_usage
; /* timesharing cpu usage [sched] */
365 natural_t pri_shift
; /* usage -> priority from pset */
366 natural_t cpu_usage
; /* instrumented cpu usage [%cpu] */
367 natural_t cpu_delta
; /* accumulated cpu_usage delta */
368 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
370 uint32_t c_switch
; /* total context switches */
371 uint32_t p_switch
; /* total processor switches */
372 uint32_t ps_switch
; /* total pset switches */
374 integer_t mutex_count
; /* total count of locks held */
375 /* Timing data structures */
376 int precise_user_kernel_time
; /* precise user/kernel enabled for this thread */
377 timer_data_t user_timer
; /* user mode timer */
378 uint64_t user_timer_save
; /* saved user timer value */
379 uint64_t system_timer_save
; /* saved system timer value */
380 uint64_t vtimer_user_save
; /* saved values for vtimers */
381 uint64_t vtimer_prof_save
;
382 uint64_t vtimer_rlim_save
;
383 uint64_t vtimer_qos_save
;
385 timer_data_t ptime
; /* time executing in P mode */
386 timer_data_t runnable_timer
; /* time the thread is runnable (including running) */
389 /* Timing for wait state */
390 uint64_t wait_sfi_begin_time
; /* start time for thread waiting in SFI */
394 * Processor/cache affinity
395 * - affinity_threads links task threads with the same affinity set
397 queue_chain_t affinity_threads
;
398 affinity_set_t affinity_set
;
401 task_watch_t
* taskwatch
; /* task watch */
402 #endif /* CONFIG_EMBEDDED */
404 /* Various bits of state to stash across a continuation, exclusive to the current thread block point */
407 mach_msg_return_t state
; /* receive state */
408 mach_port_seqno_t seqno
; /* seqno of recvd message */
409 ipc_object_t object
; /* object received on */
410 vm_address_t msg_addr
; /* receive buffer pointer */
411 mach_msg_size_t rsize
; /* max size for recvd msg */
412 mach_msg_size_t msize
; /* actual size for recvd msg */
413 mach_msg_option_t option
; /* options for receive */
414 mach_port_name_t receiver_name
; /* the receive port name */
415 struct knote
*knote
; /* knote fired for rcv */
417 struct ipc_kmsg
*kmsg
; /* received message */
418 struct ipc_mqueue
*peekq
; /* mqueue to peek at */
420 mach_msg_priority_t qos
; /* received message qos */
421 mach_msg_priority_t oqos
; /* override qos for message */
424 mach_msg_continue_t continuation
;
427 struct semaphore
*waitsemaphore
; /* semaphore ref */
428 struct semaphore
*signalsemaphore
; /* semaphore ref */
429 int options
; /* semaphore options */
430 kern_return_t result
; /* primary result */
431 mach_msg_continue_t continuation
;
434 #define THREAD_SAVE_IOKIT_TLS_COUNT 8
435 void *tls
[THREAD_SAVE_IOKIT_TLS_COUNT
];
439 /* Only user threads can cause guard exceptions, only kernel threads can be thread call threads */
441 /* Group and call this thread is working on behalf of */
443 struct thread_call_group
* thc_group
;
444 struct thread_call
* thc_call
; /* debug only, may be deallocated */
447 /* Structure to save information about guard exception */
449 mach_exception_code_t code
;
450 mach_exception_subcode_t subcode
;
454 /* Kernel holds on this thread */
455 int16_t suspend_count
;
456 /* User level suspensions */
457 int16_t user_stop_count
;
459 /* IPC data structures */
460 #if IMPORTANCE_INHERITANCE
461 natural_t ith_assertions
; /* assertions pending drop */
463 struct ipc_kmsg_queue ith_messages
; /* messages to reap */
464 mach_port_t ith_rpc_reply
; /* reply port for kernel RPCs */
466 /* Pending thread ast(s) */
469 /* Ast/Halt data structures */
470 vm_offset_t recover
; /* page fault recover(copyin/out) */
472 queue_chain_t threads
; /* global list of all threads */
475 queue_chain_t task_threads
;
477 /* Task membership */
480 #if DEVELOPMENT || DEBUG
481 bool pmap_footprint_suspended
;
482 #endif /* DEVELOPMENT || DEBUG */
484 /* Timed wait expiration */
485 timer_call_data_t wait_timer
;
486 uint16_t wait_timer_active
;
487 bool wait_timer_is_set
;
489 /* Miscellaneous bits guarded by mutex */
491 active
:1, /* Thread is active and has not been terminated */
492 started
:1, /* Thread has been started after creation */
493 static_param
:1, /* Disallow policy parameter changes */
494 inspection
:1, /* TRUE when task is being inspected by crash reporter */
495 policy_reset
:1, /* Disallow policy parameter changes on terminating threads */
496 suspend_parked
:1, /* thread parked in thread_suspended */
497 corpse_dup
:1, /* TRUE when thread is an inactive duplicate in a corpse */
500 decl_lck_mtx_data(, mutex
);
502 /* Ports associated with this thread */
503 struct ipc_port
*ith_self
; /* not a right, doesn't hold ref */
504 struct ipc_port
*ith_sself
; /* a send right */
505 struct ipc_port
*ith_special_reply_port
; /* ref to special reply port */
506 struct exception_action
*exc_actions
;
513 uint16_t t_dtrace_flags
; /* DTrace thread states */
514 #define TH_DTRACE_EXECSUCCESS 0x01
515 uint16_t t_dtrace_inprobe
; /* Executing under dtrace_probe */
516 uint32_t t_dtrace_predcache
; /* DTrace per thread predicate value hint */
517 int64_t t_dtrace_tracing
; /* Thread time under dtrace_probe() */
518 int64_t t_dtrace_vtime
;
521 clock_sec_t t_page_creation_time
;
522 uint32_t t_page_creation_count
;
523 uint32_t t_page_creation_throttled
;
524 #if (DEVELOPMENT || DEBUG)
525 uint64_t t_page_creation_throttled_hard
;
526 uint64_t t_page_creation_throttled_soft
;
527 #endif /* DEVELOPMENT || DEBUG */
528 int t_pagein_error
; /* for vm_fault(), holds error from vnop_pagein() */
531 /* The high 8 bits are the number of frames to sample of a user callstack. */
532 #define T_KPERF_CALLSTACK_DEPTH_OFFSET (24)
533 #define T_KPERF_SET_CALLSTACK_DEPTH(DEPTH) (((uint32_t)(DEPTH)) << T_KPERF_CALLSTACK_DEPTH_OFFSET)
534 #define T_KPERF_GET_CALLSTACK_DEPTH(FLAGS) ((FLAGS) >> T_KPERF_CALLSTACK_DEPTH_OFFSET)
535 #define T_KPERF_ACTIONID_OFFSET (18)
536 #define T_KPERF_SET_ACTIONID(AID) (((uint32_t)(AID)) << T_KPERF_ACTIONID_OFFSET)
537 #define T_KPERF_GET_ACTIONID(FLAGS) ((FLAGS) >> T_KPERF_ACTIONID_OFFSET)
540 #define T_KPERF_AST_CALLSTACK 0x1 /* dump a callstack on thread's next AST */
541 #define T_KPERF_AST_DISPATCH 0x2 /* dump a name on thread's next AST */
542 #define T_KPC_ALLOC 0x4 /* thread needs a kpc_buf allocated */
544 #define T_KPERF_AST_ALL \
545 (T_KPERF_AST_CALLSTACK | T_KPERF_AST_DISPATCH | T_KPC_ALLOC)
546 /* only go up to T_KPERF_ACTIONID_OFFSET - 1 */
550 uint32_t kperf_pet_gen
; /* last generation of PET that sampled this thread*/
551 uint32_t kperf_c_switch
; /* last dispatch detection */
552 uint32_t kperf_pet_cnt
; /* how many times a thread has been sampled by PET */
556 /* accumulated performance counters for this thread */
561 /* hypervisor virtual CPU object associated with this thread */
562 void *hv_thread_target
;
563 #endif /* HYPERVISOR */
565 /* Statistics accumulated per-thread and aggregated per-task */
566 uint32_t syscalls_unix
;
567 uint32_t syscalls_mach
;
569 ledger_t t_threadledger
; /* per thread ledger */
570 ledger_t t_bankledger
; /* ledger to charge someone */
571 uint64_t t_deduct_bank_ledger_time
; /* cpu time to be deducted from bank ledger */
572 uint64_t t_deduct_bank_ledger_energy
; /* energy to be deducted from bank ledger */
574 uint64_t thread_id
; /*system wide unique thread-id*/
577 struct mt_thread t_monotonic
;
578 #endif /* MONOTONIC */
580 /*** Machine-dependent state ***/
581 struct machine_thread machine
;
583 /* policy is protected by the thread mutex */
584 struct thread_requested_policy requested_policy
;
585 struct thread_effective_policy effective_policy
;
587 /* usynch override is protected by the task lock, eventually will be thread mutex */
588 struct thread_qos_override
{
589 struct thread_qos_override
*override_next
;
590 uint32_t override_contended_resource_count
;
591 int16_t override_qos
;
592 int16_t override_resource_type
;
593 user_addr_t override_resource
;
596 uint32_t kevent_overrides
;
597 uint8_t user_promotion_basepri
;
598 uint8_t kern_promotion_schedpri
;
599 _Atomic
uint16_t kevent_ast_bits
;
601 io_stat_info_t thread_io_stats
; /* per-thread I/O statistics */
603 uint32_t thread_callout_interrupt_wakeups
;
604 uint32_t thread_callout_platform_idle_wakeups
;
605 uint32_t thread_timer_wakeups_bin_1
;
606 uint32_t thread_timer_wakeups_bin_2
;
609 * callout_* fields are only set for thread call threads whereas guard_exc_fatal is set
610 * by user threads on themselves while taking a guard exception. So it's okay for them to
611 * share this bitfield.
613 uint16_t callout_woken_from_icontext
:1,
614 callout_woken_from_platform_idle
:1,
615 callout_woke_thread
:1,
617 thread_bitfield_unused
:12;
619 mach_port_name_t ith_voucher_name
;
620 ipc_voucher_t ith_voucher
;
623 #endif /* CONFIG_IOSCHED */
625 /* work interval (if any) associated with the thread. Uses thread mutex */
626 struct work_interval
*th_work_interval
;
628 #if SCHED_TRACE_THREAD_WAKEUPS
629 uintptr_t thread_wakeup_bt
[64];
631 turnstile_update_flags_t inheritor_flags
; /* inheritor flags for inheritor field */
632 block_hint_t pending_block_hint
;
633 block_hint_t block_hint
; /* What type of primitive last caused us to block. */
634 integer_t decompressions
; /* Per-thread decompressions counter to be added to per-task decompressions counter */
637 #define ith_state saved.receive.state
638 #define ith_object saved.receive.object
639 #define ith_msg_addr saved.receive.msg_addr
640 #define ith_rsize saved.receive.rsize
641 #define ith_msize saved.receive.msize
642 #define ith_option saved.receive.option
643 #define ith_receiver_name saved.receive.receiver_name
644 #define ith_continuation saved.receive.continuation
645 #define ith_kmsg saved.receive.kmsg
646 #define ith_peekq saved.receive.peekq
647 #define ith_knote saved.receive.knote
648 #define ith_qos saved.receive.received_qos.qos
649 #define ith_qos_override saved.receive.received_qos.oqos
650 #define ith_seqno saved.receive.seqno
652 #define sth_waitsemaphore saved.sema.waitsemaphore
653 #define sth_signalsemaphore saved.sema.signalsemaphore
654 #define sth_options saved.sema.options
655 #define sth_result saved.sema.result
656 #define sth_continuation saved.sema.continuation
658 #define ITH_KNOTE_NULL ((void *)NULL)
659 #define ITH_KNOTE_PSEUDO ((void *)0xdeadbeef)
661 * The ith_knote is used during message delivery, and can safely be interpreted
662 * only when used for one of these codepaths, which the test for the msgt_name
663 * being RECEIVE or SEND_ONCE is about.
665 #define ITH_KNOTE_VALID(kn, msgt_name) \
666 (((kn) != ITH_KNOTE_NULL && (kn) != ITH_KNOTE_PSEUDO) && \
667 ((msgt_name) == MACH_MSG_TYPE_PORT_RECEIVE || \
668 (msgt_name) == MACH_MSG_TYPE_PORT_SEND_ONCE))
671 #define assert_thread_magic(thread) assertf((thread)->thread_magic == THREAD_MAGIC, \
672 "bad thread magic 0x%llx for thread %p, expected 0x%llx", \
673 (thread)->thread_magic, (thread), THREAD_MAGIC)
675 #define assert_thread_magic(thread) do { (void)(thread); } while (0)
678 extern thread_t
thread_bootstrap(void);
680 extern void thread_machine_init_template(void);
682 extern void thread_init(void);
684 extern void thread_daemon_init(void);
686 #define thread_reference_internal(thread) \
687 os_ref_retain(&(thread)->ref_count);
689 #define thread_reference(thread) \
691 if ((thread) != THREAD_NULL) \
692 thread_reference_internal(thread); \
695 extern void thread_deallocate(
698 extern void thread_inspect_deallocate(
699 thread_inspect_t thread
);
701 extern void thread_terminate_self(void);
703 extern kern_return_t
thread_terminate_internal(
706 extern void thread_start(
707 thread_t thread
) __attribute__ ((noinline
));
709 extern void thread_start_in_assert_wait(
712 wait_interrupt_t interruptible
) __attribute__ ((noinline
));
714 extern void thread_terminate_enqueue(
717 extern void thread_exception_enqueue(
720 exception_type_t etype
);
722 extern void thread_copy_resource_info(
724 thread_t src_thread
);
726 extern void thread_terminate_crashed_threads(void);
728 extern void thread_stack_enqueue(
731 extern void thread_hold(
734 extern void thread_release(
737 extern void thread_corpse_continue(void) __dead2
;
739 extern boolean_t
thread_is_active(thread_t thread
);
741 extern lck_grp_t thread_lck_grp
;
743 /* Locking for scheduler state, always acquired with interrupts disabled (splsched()) */
745 #define thread_lock_init(th) simple_lock_init(&(th)->sched_lock, 0)
746 #define thread_lock(th) simple_lock(&(th)->sched_lock, &thread_lck_grp)
747 #define thread_unlock(th) simple_unlock(&(th)->sched_lock)
749 #define wake_lock_init(th) simple_lock_init(&(th)->wake_lock, 0)
750 #define wake_lock(th) simple_lock(&(th)->wake_lock, &thread_lck_grp)
751 #define wake_unlock(th) simple_unlock(&(th)->wake_lock)
753 #define thread_lock_init(th) do { (void)th; } while(0)
754 #define thread_lock(th) do { (void)th; } while(0)
755 #define thread_unlock(th) do { (void)th; } while(0)
757 #define wake_lock_init(th) do { (void)th; } while(0)
758 #define wake_lock(th) do { (void)th; } while(0)
759 #define wake_unlock(th) do { (void)th; } while(0)
762 #define thread_should_halt_fast(thread) (!(thread)->active)
764 extern void stack_alloc(
767 extern void stack_handoff(
771 extern void stack_free(
774 extern void stack_free_reserved(
777 extern boolean_t
stack_alloc_try(
780 extern void stack_collect(void);
782 extern void stack_init(void);
785 extern kern_return_t
thread_info_internal(
787 thread_flavor_t flavor
,
788 thread_info_t thread_info_out
,
789 mach_msg_type_number_t
*thread_info_count
);
793 extern kern_return_t
kernel_thread_create(
794 thread_continue_t continuation
,
797 thread_t
*new_thread
);
799 extern kern_return_t
kernel_thread_start_priority(
800 thread_continue_t continuation
,
803 thread_t
*new_thread
);
805 extern void machine_stack_attach(
809 extern vm_offset_t
machine_stack_detach(
812 extern void machine_stack_handoff(
816 extern thread_t
machine_switch_context(
818 thread_continue_t continuation
,
819 thread_t new_thread
);
821 extern void machine_load_context(
822 thread_t thread
) __attribute__((noreturn
));
824 extern kern_return_t
machine_thread_state_initialize(
827 extern kern_return_t
machine_thread_set_state(
829 thread_flavor_t flavor
,
830 thread_state_t state
,
831 mach_msg_type_number_t count
);
833 extern mach_vm_address_t
machine_thread_pc(
836 extern void machine_thread_reset_pc(
838 mach_vm_address_t pc
);
840 extern boolean_t
machine_thread_on_core(
843 extern kern_return_t
machine_thread_get_state(
845 thread_flavor_t flavor
,
846 thread_state_t state
,
847 mach_msg_type_number_t
*count
);
849 extern kern_return_t
machine_thread_state_convert_from_user(
851 thread_flavor_t flavor
,
852 thread_state_t tstate
,
853 mach_msg_type_number_t count
);
855 extern kern_return_t
machine_thread_state_convert_to_user(
857 thread_flavor_t flavor
,
858 thread_state_t tstate
,
859 mach_msg_type_number_t
*count
);
861 extern kern_return_t
machine_thread_dup(
864 boolean_t is_corpse
);
866 extern void machine_thread_init(void);
868 extern void machine_thread_template_init(thread_t thr_template
);
870 extern kern_return_t
machine_thread_create(
873 extern void machine_thread_switch_addrmode(
876 extern void machine_thread_destroy(
879 extern void machine_set_current_thread(
882 extern kern_return_t
machine_thread_get_kern_state(
884 thread_flavor_t flavor
,
885 thread_state_t tstate
,
886 mach_msg_type_number_t
*count
);
888 extern kern_return_t
machine_thread_inherit_taskwide(
892 extern kern_return_t
machine_thread_set_tsd_base(
894 mach_vm_offset_t tsd_base
);
896 #define thread_mtx_lock(thread) lck_mtx_lock(&(thread)->mutex)
897 #define thread_mtx_try(thread) lck_mtx_try_lock(&(thread)->mutex)
898 #define thread_mtx_unlock(thread) lck_mtx_unlock(&(thread)->mutex)
900 extern void thread_apc_ast(thread_t thread
);
902 extern void thread_update_qos_cpu_time(thread_t thread
);
904 void act_machine_sv_free(thread_t
, int);
906 vm_offset_t
min_valid_stack_address(void);
907 vm_offset_t
max_valid_stack_address(void);
909 static inline uint16_t
910 thread_set_tag_internal(thread_t thread
, uint16_t tag
)
912 return os_atomic_or_orig(&thread
->thread_tag
, tag
, relaxed
);
915 static inline uint16_t
916 thread_get_tag_internal(thread_t thread
)
918 return thread
->thread_tag
;
921 extern bool thread_no_smt(thread_t thread
);
922 extern bool processor_active_thread_no_smt(processor_t processor
);
924 extern void thread_set_options(uint32_t thopt
);
927 #else /* MACH_KERNEL_PRIVATE */
931 extern void thread_mtx_lock(thread_t thread
);
933 extern void thread_mtx_unlock(thread_t thread
);
935 extern thread_t
current_thread(void) __attribute__((const));
937 extern void thread_reference(
940 extern void thread_deallocate(
943 #if BSD_KERNEL_PRIVATE
944 /* Duplicated from osfmk/kern/ipc_tt.h */
945 __options_decl(port_to_thread_options_t
, uint32_t, {
946 PORT_TO_THREAD_NONE
= 0x0000,
947 PORT_TO_THREAD_IN_CURRENT_TASK
= 0x0001,
948 PORT_TO_THREAD_NOT_CURRENT_THREAD
= 0x0002,
951 extern thread_t
port_name_to_thread(
952 mach_port_name_t port_name
,
953 port_to_thread_options_t options
);
954 #endif /* BSD_KERNEL_PRIVATE */
958 #endif /* MACH_KERNEL_PRIVATE */
960 #ifdef KERNEL_PRIVATE
964 extern void thread_deallocate_safe(
967 extern uint64_t thread_dispatchqaddr(
970 extern uint64_t thread_rettokern_addr(
973 extern integer_t
thread_kern_get_pri(thread_t thr
) __attribute__((const));
975 extern void thread_kern_set_pri(thread_t thr
, integer_t pri
);
977 extern integer_t
thread_kern_get_kernel_maxpri(void) __attribute__((const));
981 #endif /* KERNEL_PRIVATE */
986 extern uint64_t thread_tid(thread_t thread
);
994 #ifdef XNU_KERNEL_PRIVATE
997 * Thread tags; for easy identification.
999 #define THREAD_TAG_MAINTHREAD 0x1
1000 #define THREAD_TAG_CALLOUT 0x2
1001 #define THREAD_TAG_IOWORKLOOP 0x4
1003 #define THREAD_TAG_PTHREAD 0x10
1004 #define THREAD_TAG_WORKQUEUE 0x20
1006 uint16_t thread_set_tag(thread_t
, uint16_t);
1007 uint16_t thread_get_tag(thread_t
);
1008 uint64_t thread_last_run_time(thread_t
);
1010 extern kern_return_t
thread_state_initialize(
1013 extern kern_return_t
thread_setstatus(
1016 thread_state_t tstate
,
1017 mach_msg_type_number_t count
);
1019 extern kern_return_t
thread_setstatus_from_user(
1022 thread_state_t tstate
,
1023 mach_msg_type_number_t count
);
1025 extern kern_return_t
thread_getstatus(
1028 thread_state_t tstate
,
1029 mach_msg_type_number_t
*count
);
1031 extern kern_return_t
thread_getstatus_to_user(
1034 thread_state_t tstate
,
1035 mach_msg_type_number_t
*count
);
1037 extern kern_return_t
thread_create_with_continuation(
1039 thread_t
*new_thread
,
1040 thread_continue_t continuation
);
1042 extern kern_return_t
thread_create_waiting(task_t task
,
1043 thread_continue_t continuation
,
1045 thread_t
*new_thread
);
1047 extern kern_return_t
thread_create_workq_waiting(
1049 thread_continue_t thread_return
,
1050 thread_t
*new_thread
);
1052 extern void thread_yield_internal(
1053 mach_msg_timeout_t interval
);
1055 extern void thread_yield_to_preemption(void);
1058 * Thread-private CPU limits: apply a private CPU limit to this thread only. Available actions are:
1060 * 1) Block. Prevent CPU consumption of the thread from exceeding the limit.
1061 * 2) Exception. Generate a resource consumption exception when the limit is exceeded.
1062 * 3) Disable. Remove any existing CPU limit.
1064 #define THREAD_CPULIMIT_BLOCK 0x1
1065 #define THREAD_CPULIMIT_EXCEPTION 0x2
1066 #define THREAD_CPULIMIT_DISABLE 0x3
1068 struct _thread_ledger_indices
{
1072 extern struct _thread_ledger_indices thread_ledgers
;
1074 extern int thread_get_cpulimit(int *action
, uint8_t *percentage
, uint64_t *interval_ns
);
1075 extern int thread_set_cpulimit(int action
, uint8_t percentage
, uint64_t interval_ns
);
1077 extern void thread_read_times(
1079 time_value_t
*user_time
,
1080 time_value_t
*system_time
,
1081 time_value_t
*runnable_time
);
1083 extern uint64_t thread_get_runtime_self(void);
1085 extern void thread_setuserstack(
1087 mach_vm_offset_t user_stack
);
1089 extern uint64_t thread_adjuserstack(
1093 extern void thread_setentrypoint(
1095 mach_vm_offset_t entry
);
1097 extern kern_return_t
thread_set_tsd_base(
1099 mach_vm_offset_t tsd_base
);
1101 extern kern_return_t
thread_setsinglestep(
1105 extern kern_return_t
thread_userstack(
1114 extern kern_return_t
thread_entrypoint(
1119 mach_vm_offset_t
*);
1121 extern kern_return_t
thread_userstackdefault(
1125 extern kern_return_t
thread_wire_internal(
1126 host_priv_t host_priv
,
1129 boolean_t
*prev_state
);
1132 extern kern_return_t
thread_dup(thread_t
);
1134 extern kern_return_t
thread_dup2(thread_t
, thread_t
);
1136 #if !defined(_SCHED_CALL_T_DEFINED)
1137 #define _SCHED_CALL_T_DEFINED
1138 typedef void (*sched_call_t
)(
1143 #define SCHED_CALL_BLOCK 0x1
1144 #define SCHED_CALL_UNBLOCK 0x2
1146 extern void thread_sched_call(
1150 extern boolean_t
thread_is_static_param(
1153 extern task_t
get_threadtask(thread_t
);
1156 * Thread is running within a 64-bit address space.
1158 #define thread_is_64bit_addr(thd) \
1159 task_has_64Bit_addr(get_threadtask(thd))
1162 * Thread is using 64-bit machine state.
1164 #define thread_is_64bit_data(thd) \
1165 task_has_64Bit_data(get_threadtask(thd))
1167 #if defined(__x86_64__)
1168 extern int thread_task_has_ldt(thread_t
);
1170 extern void *get_bsdthread_info(thread_t
);
1171 extern void set_bsdthread_info(thread_t
, void *);
1172 extern void set_thread_pagein_error(thread_t
, int);
1173 extern void *uthread_alloc(task_t
, thread_t
, int);
1174 extern event_t
workq_thread_init_and_wq_lock(task_t
, thread_t
); // bsd/pthread/
1175 extern void uthread_cleanup_name(void *uthread
);
1176 extern void uthread_cleanup(task_t
, void *, void *);
1177 extern void uthread_zone_free(void *);
1178 extern void uthread_cred_free(void *);
1180 extern void uthread_reset_proc_refcount(void *);
1182 extern int uthread_get_proc_refcount(void *);
1183 extern int proc_ref_tracking_disabled
;
1186 extern boolean_t
thread_should_halt(
1189 extern boolean_t
thread_should_abort(
1192 extern int is_64signalregset(void);
1194 extern void act_set_kperf(thread_t
);
1195 extern void act_set_astledger(thread_t thread
);
1196 extern void act_set_astledger_async(thread_t thread
);
1197 extern void act_set_io_telemetry_ast(thread_t
);
1199 extern uint32_t dtrace_get_thread_predcache(thread_t
);
1200 extern int64_t dtrace_get_thread_vtime(thread_t
);
1201 extern int64_t dtrace_get_thread_tracing(thread_t
);
1202 extern uint16_t dtrace_get_thread_inprobe(thread_t
);
1203 extern int dtrace_get_thread_last_cpu_id(thread_t
);
1204 extern vm_offset_t
dtrace_get_kernel_stack(thread_t
);
1205 extern void dtrace_set_thread_predcache(thread_t
, uint32_t);
1206 extern void dtrace_set_thread_vtime(thread_t
, int64_t);
1207 extern void dtrace_set_thread_tracing(thread_t
, int64_t);
1208 extern void dtrace_set_thread_inprobe(thread_t
, uint16_t);
1209 extern vm_offset_t
dtrace_set_thread_recover(thread_t
, vm_offset_t
);
1210 extern vm_offset_t
dtrace_sign_and_set_thread_recover(thread_t
, vm_offset_t
);
1211 extern void dtrace_thread_bootstrap(void);
1212 extern void dtrace_thread_didexec(thread_t
);
1214 extern int64_t dtrace_calc_thread_recent_vtime(thread_t
);
1217 extern kern_return_t
thread_set_wq_state32(
1219 thread_state_t tstate
);
1221 extern kern_return_t
thread_set_wq_state64(
1223 thread_state_t tstate
);
1225 extern vm_offset_t kernel_stack_mask
;
1226 extern vm_offset_t kernel_stack_size
;
1227 extern vm_offset_t kernel_stack_depth_max
;
1229 extern void guard_ast(thread_t
);
1230 extern void fd_guard_ast(thread_t
,
1231 mach_exception_code_t
, mach_exception_subcode_t
);
1233 extern void vn_guard_ast(thread_t
,
1234 mach_exception_code_t
, mach_exception_subcode_t
);
1236 extern void mach_port_guard_ast(thread_t
,
1237 mach_exception_code_t
, mach_exception_subcode_t
);
1238 extern void virt_memory_guard_ast(thread_t
,
1239 mach_exception_code_t
, mach_exception_subcode_t
);
1240 extern void thread_guard_violation(thread_t
,
1241 mach_exception_code_t
, mach_exception_subcode_t
, boolean_t
);
1242 extern void thread_update_io_stats(thread_t
, int size
, int io_flags
);
1244 extern kern_return_t
thread_set_voucher_name(mach_port_name_t name
);
1245 extern kern_return_t
thread_get_current_voucher_origin_pid(int32_t *pid
);
1247 extern void set_thread_rwlock_boost(void);
1248 extern void clear_thread_rwlock_boost(void);
1250 extern void thread_enable_send_importance(thread_t thread
, boolean_t enable
);
1253 * Translate signal context data pointer to userspace representation
1256 extern kern_return_t
machine_thread_siguctx_pointer_convert_to_user(
1258 user_addr_t
*uctxp
);
1260 extern void machine_tecs(thread_t thr
);
1262 typedef enum cpuvn
{
1266 extern int machine_csv(cpuvn_e cve
);
1269 * Translate array of function pointer syscall arguments from userspace representation
1272 extern kern_return_t
machine_thread_function_pointers_convert_from_user(
1277 /* Get a backtrace for a threads kernel or user stack (user_p), with pc and optionally
1278 * frame pointer (getfp). Returns bytes added to buffer, and kThreadTruncatedBT in
1279 * thread_trace_flags if a user page is not present after kdp_lightweight_fault() is
1283 extern int machine_trace_thread(
1290 uint32_t *thread_trace_flags
);
1292 extern int machine_trace_thread64(thread_t thread
,
1298 uint32_t *thread_trace_flags
,
1302 * Get the duration of the given thread's last wait.
1304 uint64_t thread_get_last_wait_duration(thread_t thread
);
1306 extern void thread_set_no_smt(bool set
);
1307 extern bool thread_get_no_smt(void);
1309 #endif /* XNU_KERNEL_PRIVATE */
1311 /*! @function thread_has_thread_name
1312 * @abstract Checks if a thread has a name.
1313 * @discussion This function takes one input, a thread, and returns a boolean value indicating if that thread already has a name associated with it.
1314 * @param th The thread to inspect.
1315 * @result TRUE if the thread has a name, FALSE otherwise.
1317 extern boolean_t
thread_has_thread_name(thread_t th
);
1319 /*! @function thread_set_thread_name
1320 * @abstract Set a thread's name.
1321 * @discussion This function takes two input parameters: a thread to name, and the name to apply to the thread. The name will be copied over to the thread in order to better identify the thread. If the name is longer than MAXTHREADNAMESIZE - 1, it will be truncated.
1322 * @param th The thread to be named.
1323 * @param name The name to apply to the thread.
1325 extern void thread_set_thread_name(thread_t th
, const char* name
);
1327 /*! @function kernel_thread_start
1328 * @abstract Create a kernel thread.
1329 * @discussion This function takes three input parameters, namely reference to the function that the thread should execute, caller specified data and a reference which is used to return the newly created kernel thread. The function returns KERN_SUCCESS on success or an appropriate kernel code type indicating the error. It may be noted that the caller is responsible for explicitly releasing the reference to the created thread when no longer needed. This should be done by calling thread_deallocate(new_thread).
1330 * @param continuation A C-function pointer where the thread will begin execution.
1331 * @param parameter Caller specified data to be passed to the new thread.
1332 * @param new_thread Reference to the new thread is returned in this parameter.
1333 * @result Returns KERN_SUCCESS on success or an appropriate kernel code type.
1336 extern kern_return_t
kernel_thread_start(
1337 thread_continue_t continuation
,
1339 thread_t
*new_thread
);
1341 #ifdef KERNEL_PRIVATE
1342 void thread_set_eager_preempt(thread_t thread
);
1343 void thread_clear_eager_preempt(thread_t thread
);
1344 void thread_set_honor_qlimit(thread_t thread
);
1345 void thread_clear_honor_qlimit(thread_t thread
);
1346 extern ipc_port_t
convert_thread_to_port(thread_t
);
1347 extern ipc_port_t
convert_thread_inspect_to_port(thread_inspect_t
);
1348 extern boolean_t
is_vm_privileged(void);
1349 extern boolean_t
set_vm_privilege(boolean_t
);
1350 extern kern_allocation_name_t
thread_set_allocation_name(kern_allocation_name_t new_name
);
1351 extern void *thread_iokit_tls_get(uint32_t index
);
1352 extern void thread_iokit_tls_set(uint32_t index
, void * data
);
1353 #endif /* KERNEL_PRIVATE */
1357 #endif /* _KERN_THREAD_H_ */