1 /*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_FREE_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or [email protected]
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: thread.h
60 * Author: Avadis Tevanian, Jr.
61 *
62 * This file contains the structure definitions for threads.
63 *
64 */
65 /*
66 * Copyright (c) 1993 The University of Utah and
67 * the Computer Systems Laboratory (CSL). All rights reserved.
68 *
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.
74 *
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.
78 *
79 * CSL requests users of this software to return to [email protected] any
80 * improvements that they make and grant CSL redistribution rights.
81 *
82 */
83
84 #ifndef _KERN_THREAD_H_
85 #define _KERN_THREAD_H_
86
87 #include <mach/kern_return.h>
88 #include <mach/mach_types.h>
89 #include <mach/mach_param.h>
90 #include <mach/message.h>
91 #include <mach/boolean.h>
92 #include <mach/vm_param.h>
93 #include <mach/thread_info.h>
94 #include <mach/thread_status.h>
95 #include <mach/exception_types.h>
96
97 #include <kern/kern_types.h>
98 #include <vm/vm_kern.h>
99 #include <sys/cdefs.h>
100
101 #ifdef MACH_KERNEL_PRIVATE
102 #include <mach_assert.h>
103 #include <mach_ldebug.h>
104
105 #include <ipc/ipc_types.h>
106
107 #include <mach/port.h>
108 #include <kern/cpu_number.h>
109 #include <kern/smp.h>
110 #include <kern/queue.h>
111
112 #include <kern/timer.h>
113 #include <kern/simple_lock.h>
114 #include <kern/locks.h>
115 #include <kern/sched.h>
116 #include <kern/sched_prim.h>
117 #include <mach/sfi_class.h>
118 #include <kern/thread_call.h>
119 #include <kern/thread_group.h>
120 #include <kern/timer_call.h>
121 #include <kern/task.h>
122 #include <kern/exception.h>
123 #include <kern/affinity.h>
124 #include <kern/debug.h>
125 #include <kern/block_hint.h>
126 #include <kern/turnstile.h>
127 #include <kern/mpsc_queue.h>
128
129 #include <kern/waitq.h>
130 #include <san/kasan.h>
131 #include <san/kcov_data.h>
132 #include <os/refcnt.h>
133
134 #include <ipc/ipc_kmsg.h>
135
136 #include <machine/atomic.h>
137 #include <machine/cpu_data.h>
138 #include <machine/thread.h>
139
140 #if MONOTONIC
141 #include <stdatomic.h>
142 #include <machine/monotonic.h>
143 #endif /* MONOTONIC */
144 #endif /* MACH_KERNEL_PRIVATE */
145 #ifdef XNU_KERNEL_PRIVATE
146 /* priority queue static asserts fail for __ARM64_ARCH_8_32__ kext builds */
147 #include <kern/priority_queue.h>
148 #endif /* XNU_KERNEL_PRIVATE */
149
150 __BEGIN_DECLS
151
152 #ifdef XNU_KERNEL_PRIVATE
153 #if CONFIG_TASKWATCH
154 /* Taskwatch related. TODO: find this a better home */
155 typedef struct task_watcher task_watch_t;
156 #endif /* CONFIG_TASKWATCH */
157
158 /* Thread tags; for easy identification. */
159 __options_closed_decl(thread_tag_t, uint16_t, {
160 THREAD_TAG_MAINTHREAD = 0x01,
161 THREAD_TAG_CALLOUT = 0x02,
162 THREAD_TAG_IOWORKLOOP = 0x04,
163 THREAD_TAG_PTHREAD = 0x10,
164 THREAD_TAG_WORKQUEUE = 0x20,
165 THREAD_TAG_USER_JOIN = 0x40,
166 });
167
168 __options_closed_decl(thread_ro_flags_t, uint16_t, {
169 TRO_NONE = 0x0000,
170 TRO_SETUID = 0x0001,
171 });
172
173 typedef struct thread_ro *thread_ro_t;
174
175 /*!
176 * @struct thread_ro
177 *
178 * @brief
179 * A structure allocated in a read only zone that safely
180 * represents the linkages of a thread to its cred, proc, task, ...
181 *
182 * @discussion
183 * The lifetime of a @c thread_ro structure is 1:1 with that
184 * of a @c thread_t or a @c uthread_t and holding a thread reference
185 * always allows to dereference this structure safely.
186 */
187 struct thread_ro {
188 struct thread *tro_owner;
189 #if MACH_BSD
190 struct ucred *tro_cred;
191 struct proc *tro_proc;
192 struct proc_ro *tro_proc_ro;
193 #endif
194 struct task *tro_task;
195 thread_ro_flags_t tro_flags;
196
197 struct ipc_port *tro_self_port;
198 struct ipc_port *tro_settable_self_port; /* send right */
199 struct ipc_port *tro_ports[THREAD_SELF_PORT_COUNT]; /* no right */
200
201 struct exception_action *tro_exc_actions;
202 };
203
204 /*
205 * Flags for `thread set status`.
206 */
207 __options_decl(thread_set_status_flags_t, uint32_t, {
208 TSSF_FLAGS_NONE = 0,
209
210 /* Translate the state to user. */
211 TSSF_TRANSLATE_TO_USER = 0x01,
212
213 /* Translate the state to user. Preserve flags */
214 TSSF_PRESERVE_FLAGS = 0x02,
215
216 /* Check kernel signed flag */
217 TSSF_CHECK_USER_FLAGS = 0x04,
218
219 /* Allow only user state PTRS */
220 TSSF_ALLOW_ONLY_USER_PTRS = 0x08,
221
222 /* Generate random diversifier and stash it */
223 TSSF_RANDOM_USER_DIV = 0x10,
224
225 /* Stash sigreturn token */
226 TSSF_STASH_SIGRETURN_TOKEN = 0x20,
227
228 /* Check sigreturn token */
229 TSSF_CHECK_SIGRETURN_TOKEN = 0x40,
230
231 /* Allow only matching sigreturn token */
232 TSSF_ALLOW_ONLY_MATCHING_TOKEN = 0x80,
233
234 /* Stash diversifier from thread */
235 TSSF_THREAD_USER_DIV = 0x100,
236 });
237
238 #endif /* XNU_KERNEL_PRIVATE */
239 #ifdef MACH_KERNEL_PRIVATE
240
241 extern zone_t thread_ro_zone;
242
243 __options_decl(thread_work_interval_flags_t, uint32_t, {
244 TH_WORK_INTERVAL_FLAGS_NONE = 0x0,
245 #if CONFIG_SCHED_AUTO_JOIN
246 /* Flags to indicate status about work interval thread is currently part of */
247 TH_WORK_INTERVAL_FLAGS_AUTO_JOIN_LEAK = 0x1,
248 #endif /* CONFIG_SCHED_AUTO_JOIN */
249 });
250
251 typedef union thread_rr_state {
252 uint32_t trr_value;
253 struct {
254 #define TRR_FAULT_NONE 0
255 #define TRR_FAULT_PENDING 1
256 #define TRR_FAULT_OBSERVED 2
257 /*
258 * Set to TRR_FAULT_PENDING with interrupts disabled
259 * by the thread when it is entering a user fault codepath.
260 *
261 * Moved to TRR_FAULT_OBSERVED from TRR_FAULT_PENDING:
262 * - by the thread if at IPI time,
263 * - or by task_restartable_ranges_synchronize() if the thread
264 * is interrupted (under the thread lock)
265 *
266 * Cleared by the thread when returning from a user fault
267 * codepath.
268 */
269 uint8_t trr_fault_state;
270
271 /*
272 * Set by task_restartable_ranges_synchronize()
273 * if trr_fault_state is TRR_FAULT_OBSERVED
274 * and a rendez vous at the AST is required.
275 *
276 * Set atomically if trr_fault_state == TRR_FAULT_OBSERVED,
277 * and trr_ipi_ack_pending == 0
278 */
279 uint8_t trr_sync_waiting;
280
281 /*
282 * Updated under the thread_lock(),
283 * set by task_restartable_ranges_synchronize()
284 * when the thread was IPIed and the caller is waiting
285 * for an ACK.
286 */
287 uint16_t trr_ipi_ack_pending;
288 };
289 } thread_rr_state_t;
290
291 struct thread {
292 #if MACH_ASSERT
293 #define THREAD_MAGIC 0x1234ABCDDCBA4321ULL
294 /* Ensure nothing uses &thread as a queue entry */
295 uint64_t thread_magic;
296 #endif /* MACH_ASSERT */
297
298 /*
299 * NOTE: The runq field in the thread structure has an unusual
300 * locking protocol. If its value is PROCESSOR_NULL, then it is
301 * locked by the thread_lock, but if its value is something else
302 * then it is locked by the associated run queue lock. It is
303 * set to PROCESSOR_NULL without holding the thread lock, but the
304 * transition from PROCESSOR_NULL to non-null must be done
305 * under the thread lock and the run queue lock.
306 *
307 * New waitq APIs allow the 'links' and 'runq' fields to be
308 * anywhere in the thread structure.
309 */
310 union {
311 queue_chain_t runq_links; /* run queue links */
312 queue_chain_t wait_links; /* wait queue links */
313 struct mpsc_queue_chain mpsc_links; /* thread daemon mpsc links */
314 struct priority_queue_entry_sched wait_prioq_links; /* priority ordered waitq links */
315 };
316
317 event64_t wait_event; /* wait queue event */
318 processor_t runq; /* run queue assignment */
319 waitq_t waitq; /* wait queue this thread is enqueued on */
320 struct turnstile *turnstile; /* thread's turnstile, protected by primitives interlock */
321 void *inheritor; /* inheritor of the primitive the thread will block on */
322 struct priority_queue_sched_max sched_inheritor_queue; /* Inheritor queue for kernel promotion */
323 struct priority_queue_sched_max base_inheritor_queue; /* Inheritor queue for user promotion */
324
325 #if CONFIG_SCHED_EDGE
326 bool th_bound_cluster_enqueued;
327 bool th_shared_rsrc_enqueued[CLUSTER_SHARED_RSRC_TYPE_COUNT];
328 bool th_shared_rsrc_heavy_user[CLUSTER_SHARED_RSRC_TYPE_COUNT];
329 bool th_shared_rsrc_heavy_perf_control[CLUSTER_SHARED_RSRC_TYPE_COUNT];
330 #endif /* CONFIG_SCHED_EDGE */
331
332 #if CONFIG_SCHED_CLUTCH
333 /*
334 * In the clutch scheduler, the threads are maintained in runqs at the clutch_bucket
335 * level (clutch_bucket defines a unique thread group and scheduling bucket pair). The
336 * thread is linked via a couple of linkages in the clutch bucket:
337 *
338 * - A stable priority queue linkage which is the main runqueue (based on sched_pri) for the clutch bucket
339 * - A regular priority queue linkage which is based on thread's base/promoted pri (used for clutch bucket priority calculation)
340 * - A queue linkage used for timesharing operations of threads at the scheduler tick
341 */
342 struct priority_queue_entry_stable th_clutch_runq_link;
343 struct priority_queue_entry_sched th_clutch_pri_link;
344 queue_chain_t th_clutch_timeshare_link;
345 #endif /* CONFIG_SCHED_CLUTCH */
346
347 /* Data updated during assert_wait/thread_wakeup */
348 decl_simple_lock_data(, sched_lock); /* scheduling lock (thread_lock()) */
349 decl_simple_lock_data(, wake_lock); /* for thread stop / wait (wake_lock()) */
350 uint16_t options; /* options set by thread itself */
351 #define TH_OPT_INTMASK 0x0003 /* interrupt / abort level */
352 #define TH_OPT_VMPRIV 0x0004 /* may allocate reserved memory */
353 #define TH_OPT_SYSTEM_CRITICAL 0x0010 /* Thread must always be allowed to run - even under heavy load */
354 #define TH_OPT_PROC_CPULIMIT 0x0020 /* Thread has a task-wide CPU limit applied to it */
355 #define TH_OPT_PRVT_CPULIMIT 0x0040 /* Thread has a thread-private CPU limit applied to it */
356 #define TH_OPT_IDLE_THREAD 0x0080 /* Thread is a per-processor idle thread */
357 #define TH_OPT_GLOBAL_FORCED_IDLE 0x0100 /* Thread performs forced idle for thermal control */
358 #define TH_OPT_SCHED_VM_GROUP 0x0200 /* Thread belongs to special scheduler VM group */
359 #define TH_OPT_HONOR_QLIMIT 0x0400 /* Thread will honor qlimit while sending mach_msg, regardless of MACH_SEND_ALWAYS */
360 #define TH_OPT_SEND_IMPORTANCE 0x0800 /* Thread will allow importance donation from kernel rpc */
361 #define TH_OPT_ZONE_PRIV 0x1000 /* Thread may use the zone replenish reserve */
362 #define TH_OPT_IPC_TG_BLOCKED 0x2000 /* Thread blocked in sync IPC and has made the thread group blocked callout */
363
364 bool wake_active; /* wake event on stop */
365 bool at_safe_point; /* thread_abort_safely allowed */
366 uint8_t sched_saved_run_weight;
367 #if DEVELOPMENT || DEBUG
368 bool pmap_footprint_suspended;
369 #endif /* DEVELOPMENT || DEBUG */
370 ast_t reason; /* why we blocked */
371 uint32_t quantum_remaining;
372 wait_result_t wait_result; /* outcome of wait -
373 * may be examined by this thread
374 * WITHOUT locking */
375 thread_rr_state_t t_rr_state; /* state for restartable ranges */
376 thread_continue_t continuation; /* continue here next dispatch */
377 void *parameter; /* continuation parameter */
378
379 /* Data updated/used in thread_invoke */
380 vm_offset_t kernel_stack; /* current kernel stack */
381 vm_offset_t reserved_stack; /* reserved kernel stack */
382
383 /*** Machine-dependent state ***/
384 struct machine_thread machine;
385
386 #if KASAN
387 struct kasan_thread_data kasan_data;
388 #endif
389 #if CONFIG_KCOV
390 kcov_thread_data_t kcov_data;
391 #endif
392
393 /* Thread state: */
394 int state;
395 /*
396 * Thread states [bits or'ed]
397 * All but TH_WAIT_REPORT are encoded in SS_TH_FLAGS
398 * All are encoded in kcdata.py ('ths_state')
399 */
400 #define TH_WAIT 0x01 /* queued for waiting */
401 #define TH_SUSP 0x02 /* stopped or requested to stop */
402 #define TH_RUN 0x04 /* running or on runq */
403 #define TH_UNINT 0x08 /* waiting uninteruptibly */
404 #define TH_TERMINATE 0x10 /* halted at termination */
405 #define TH_TERMINATE2 0x20 /* added to termination queue */
406 #define TH_WAIT_REPORT 0x40 /* the wait is using the sched_call,
407 * only set if TH_WAIT is also set */
408 #define TH_IDLE 0x80 /* idling processor */
409
410 /* Scheduling information */
411 sched_mode_t sched_mode; /* scheduling mode */
412 sched_mode_t saved_mode; /* saved mode during forced mode demotion */
413
414 /* This thread's contribution to global sched counters */
415 sched_bucket_t th_sched_bucket;
416
417 sfi_class_id_t sfi_class; /* SFI class (XXX Updated on CSW/QE/AST) */
418 sfi_class_id_t sfi_wait_class; /* Currently in SFI wait for this class, protected by sfi_lock */
419
420 uint32_t sched_flags; /* current flag bits */
421 #define TH_SFLAG_NO_SMT 0x0001 /* On an SMT CPU, this thread must be scheduled alone */
422 #define TH_SFLAG_FAILSAFE 0x0002 /* fail-safe has tripped */
423 #define TH_SFLAG_THROTTLED 0x0004 /* throttled thread forced to timeshare mode (may be applied in addition to failsafe) */
424 #define TH_SFLAG_DEMOTED_MASK (TH_SFLAG_THROTTLED | TH_SFLAG_FAILSAFE) /* saved_mode contains previous sched_mode */
425
426 #define TH_SFLAG_PROMOTED 0x0008 /* sched pri has been promoted by kernel mutex priority promotion */
427 #define TH_SFLAG_ABORT 0x0010 /* abort interruptible waits */
428 #define TH_SFLAG_ABORTSAFELY 0x0020 /* ... but only those at safe point */
429 #define TH_SFLAG_ABORTED_MASK (TH_SFLAG_ABORT | TH_SFLAG_ABORTSAFELY)
430 #define TH_SFLAG_DEPRESS 0x0040 /* normal depress yield */
431 #define TH_SFLAG_POLLDEPRESS 0x0080 /* polled depress yield */
432 #define TH_SFLAG_DEPRESSED_MASK (TH_SFLAG_DEPRESS | TH_SFLAG_POLLDEPRESS)
433 /* unused TH_SFLAG_PRI_UPDATE 0x0100 */
434 #define TH_SFLAG_EAGERPREEMPT 0x0200 /* Any preemption of this thread should be treated as if AST_URGENT applied */
435 #define TH_SFLAG_RW_PROMOTED 0x0400 /* promote reason: blocking with RW lock held */
436 #define TH_SFLAG_BASE_PRI_FROZEN 0x0800 /* (effective) base_pri is frozen */
437 #define TH_SFLAG_WAITQ_PROMOTED 0x1000 /* promote reason: waitq wakeup (generally for IPC receive) */
438
439 #if __AMP__
440 #define TH_SFLAG_ECORE_ONLY 0x2000 /* (unused) Bind thread to E core processor set */
441 #define TH_SFLAG_PCORE_ONLY 0x4000 /* (unused) Bind thread to P core processor set */
442 #endif
443
444 #define TH_SFLAG_EXEC_PROMOTED 0x8000 /* promote reason: thread is in an exec */
445
446 #define TH_SFLAG_THREAD_GROUP_AUTO_JOIN 0x10000 /* thread has been auto-joined to thread group */
447 #if __AMP__
448 #define TH_SFLAG_BOUND_SOFT 0x20000 /* thread is soft bound to a cluster; can run anywhere if bound cluster unavailable */
449 #endif /* __AMP__ */
450
451 #if CONFIG_PREADOPT_TG
452 #define TH_SFLAG_REEVALUTE_TG_HIERARCHY_LATER 0x40000 /* thread needs to reevaluate its TG hierarchy */
453 #endif
454
455 #define TH_SFLAG_FLOOR_PROMOTED 0x80000 /* promote reason: boost requested */
456
457 /* 'promote reasons' that request a priority floor only, not a custom priority */
458 #define TH_SFLAG_PROMOTE_REASON_MASK (TH_SFLAG_RW_PROMOTED | TH_SFLAG_WAITQ_PROMOTED | TH_SFLAG_EXEC_PROMOTED | TH_SFLAG_FLOOR_PROMOTED)
459
460 int16_t sched_pri; /* scheduled (current) priority */
461 int16_t base_pri; /* effective base priority (equal to req_base_pri unless TH_SFLAG_BASE_PRI_FROZEN) */
462 int16_t req_base_pri; /* requested base priority */
463 int16_t max_priority; /* copy of max base priority */
464 int16_t task_priority; /* copy of task base priority */
465 int16_t promotion_priority; /* priority thread is currently promoted to */
466 uint16_t priority_floor_count; /* number of push to boost the floor priority */
467 int16_t suspend_count; /* Kernel holds on this thread */
468
469 int iotier_override; /* atomic operations to set, cleared on ret to user */
470 os_ref_atomic_t ref_count; /* number of references to me */
471
472 uint32_t rwlock_count; /* Number of lck_rw_t locks held by thread */
473 #ifdef DEBUG_RW
474 rw_lock_debug_t rw_lock_held; /* rw_locks currently held by the thread */
475 #endif /* DEBUG_RW */
476
477 integer_t importance; /* task-relative importance */
478
479 /* Priority depression expiration */
480 integer_t depress_timer_active;
481 timer_call_t depress_timer;
482
483 /* real-time parameters */
484 struct { /* see mach/thread_policy.h */
485 uint32_t period;
486 uint32_t computation;
487 uint32_t constraint;
488 bool preemptible;
489 uint8_t priority_offset; /* base_pri = BASEPRI_RTQUEUES + priority_offset */
490 uint64_t deadline;
491 } realtime;
492
493 uint64_t last_run_time; /* time when thread was switched away from */
494 uint64_t last_made_runnable_time; /* time when thread was unblocked or preempted */
495 uint64_t last_basepri_change_time; /* time when thread was last changed in basepri while runnable */
496 uint64_t same_pri_latency;
497 /*
498 * workq_quantum_deadline is the workq thread's next runtime deadline. This
499 * value is set to 0 if the thread has no such deadline applicable to it.
500 *
501 * The synchronization for this field is due to how this field is modified
502 * 1) This field is always modified on the thread by itself or on the thread
503 * when it is not running/runnable
504 * 2) Change of this field is immediately followed by a
505 * corresponding change to the AST_KEVENT to either set or clear the
506 * AST_KEVENT_WORKQ_QUANTUM_EXPIRED bit
507 *
508 * workq_quantum_deadline can be modified by the thread on itself during
509 * interrupt context. However, due to (2) and due to the fact that the
510 * change to the AST_KEVENT is volatile, this forces the compiler to
511 * guarantee the order between the write to workq_quantum_deadline and the
512 * kevent field and therefore guarantees the correct synchronization.
513 */
514 uint64_t workq_quantum_deadline;
515
516 #if WORKQ_QUANTUM_HISTORY_DEBUG
517
518 #define WORKQ_QUANTUM_HISTORY_COUNT 16
519 struct workq_quantum_history {
520 uint64_t time;
521 uint64_t deadline;
522 bool arm;
523 } workq_quantum_history[WORKQ_QUANTUM_HISTORY_COUNT];
524 uint64_t workq_quantum_history_index;
525
526 #define WORKQ_QUANTUM_HISTORY_WRITE_ENTRY(thread, ...) ({\
527 thread_t __th = (thread); \
528 uint64_t __index = os_atomic_inc_orig(&thread->workq_quantum_history_index, relaxed); \
529 struct workq_quantum_history _wq_quantum_history = { mach_approximate_time(), __VA_ARGS__}; \
530 __th->workq_quantum_history[__index % WORKQ_QUANTUM_HISTORY_COUNT] = \
531 (struct workq_quantum_history) _wq_quantum_history; \
532 })
533 #else /* WORKQ_QUANTUM_HISTORY_DEBUG */
534 #define WORKQ_QUANTUM_HISTORY_WRITE_ENTRY(thread, ...)
535 #endif /* WORKQ_QUANTUM_HISTORY_DEBUG */
536
537 #define THREAD_NOT_RUNNABLE (~0ULL)
538
539 #if CONFIG_THREAD_GROUPS
540 struct thread_group *thread_group;
541 #endif
542
543 #if defined(CONFIG_SCHED_MULTIQ)
544 sched_group_t sched_group;
545 #endif /* defined(CONFIG_SCHED_MULTIQ) */
546
547 /* Data used during setrun/dispatch */
548 timer_data_t system_timer; /* system mode timer */
549 processor_t bound_processor; /* bound to a processor? */
550 processor_t last_processor; /* processor last dispatched on */
551 processor_t chosen_processor; /* Where we want to run this thread */
552
553 /* Fail-safe computation since last unblock or qualifying yield */
554 uint64_t computation_metered;
555 uint64_t computation_epoch;
556 uint64_t safe_release; /* when to release fail-safe */
557
558 /* Call out from scheduler */
559 void (*sched_call)(int type, thread_t thread);
560
561 #if defined(CONFIG_SCHED_PROTO)
562 uint32_t runqueue_generation; /* last time runqueue was drained */
563 #endif
564
565 /* Statistics and timesharing calculations */
566 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
567 natural_t sched_stamp; /* last scheduler tick */
568 natural_t sched_usage; /* timesharing cpu usage [sched] */
569 natural_t pri_shift; /* usage -> priority from pset */
570 natural_t cpu_usage; /* instrumented cpu usage [%cpu] */
571 natural_t cpu_delta; /* accumulated cpu_usage delta */
572 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
573
574 uint32_t c_switch; /* total context switches */
575 uint32_t p_switch; /* total processor switches */
576 uint32_t ps_switch; /* total pset switches */
577
578 integer_t mutex_count; /* total count of locks held */
579 /* Timing data structures */
580 int precise_user_kernel_time; /* precise user/kernel enabled for this thread */
581 timer_data_t user_timer; /* user mode timer */
582 uint64_t user_timer_save; /* saved user timer value */
583 uint64_t system_timer_save; /* saved system timer value */
584 uint64_t vtimer_user_save; /* saved values for vtimers */
585 uint64_t vtimer_prof_save;
586 uint64_t vtimer_rlim_save;
587 uint64_t vtimer_qos_save;
588
589 timer_data_t ptime; /* time executing in P mode */
590 timer_data_t runnable_timer; /* time the thread is runnable (including running) */
591
592 #if CONFIG_SCHED_SFI
593 /* Timing for wait state */
594 uint64_t wait_sfi_begin_time; /* start time for thread waiting in SFI */
595 #endif
596
597 /*
598 * Processor/cache affinity
599 * - affinity_threads links task threads with the same affinity set
600 */
601 queue_chain_t affinity_threads;
602 affinity_set_t affinity_set;
603
604 #if CONFIG_TASKWATCH
605 task_watch_t *taskwatch; /* task watch */
606 #endif /* CONFIG_TASKWATCH */
607
608 /* Various bits of state to stash across a continuation, exclusive to the current thread block point */
609 union {
610 struct {
611 mach_msg_return_t state; /* receive state */
612 mach_port_seqno_t seqno; /* seqno of recvd message */
613 ipc_object_t object; /* object received on */
614 vm_address_t msg_addr; /* receive buffer pointer */
615 mach_msg_size_t rsize; /* max size for recvd msg */
616 mach_msg_size_t msize; /* actual size for recvd msg */
617 mach_msg_option_t option; /* options for receive */
618 mach_port_name_t receiver_name; /* the receive port name */
619 struct knote *knote; /* knote fired for rcv */
620 union {
621 struct ipc_kmsg *kmsg; /* received message */
622 struct ipc_mqueue *peekq; /* mqueue to peek at */
623 struct {
624 uint32_t ppri; /* received message pthread_priority_t */
625 mach_msg_qos_t oqos; /* override qos for message */
626 } received_qos;
627 };
628 mach_msg_continue_t continuation;
629 } receive;
630 struct {
631 struct semaphore *waitsemaphore; /* semaphore ref */
632 struct semaphore *signalsemaphore; /* semaphore ref */
633 int options; /* semaphore options */
634 kern_return_t result; /* primary result */
635 mach_msg_continue_t continuation;
636 } sema;
637 struct {
638 #define THREAD_SAVE_IOKIT_TLS_COUNT 8
639 void *tls[THREAD_SAVE_IOKIT_TLS_COUNT];
640 } iokit;
641 } saved;
642
643 /* Only user threads can cause guard exceptions, only kernel threads can be thread call threads */
644 union {
645 /* Thread call thread's state structure, stored on its stack */
646 struct thread_call_thread_state *thc_state;
647
648 /* Structure to save information about guard exception */
649 struct {
650 mach_exception_code_t code;
651 mach_exception_subcode_t subcode;
652 } guard_exc_info;
653 };
654
655 /* User level suspensions */
656 int32_t user_stop_count;
657
658 /* IPC data structures */
659 #if IMPORTANCE_INHERITANCE
660 natural_t ith_assertions; /* assertions pending drop */
661 #endif
662 struct ipc_kmsg_queue ith_messages; /* messages to reap */
663 mach_port_t ith_kernel_reply_port; /* reply port for kernel RPCs */
664
665 /* Pending thread ast(s) */
666 os_atomic(ast_t) ast;
667
668 queue_chain_t threads; /* global list of all threads */
669
670 /* Activation */
671 queue_chain_t task_threads;
672
673 /* Task membership */
674 #if __x86_64__ || __arm__
675 struct task *t_task;
676 #endif
677 struct thread_ro *t_tro;
678 vm_map_t map;
679 thread_t handoff_thread;
680
681 /* Timed wait expiration */
682 timer_call_t wait_timer;
683 uint16_t wait_timer_active;
684 bool wait_timer_is_set;
685
686 /* Miscellaneous bits guarded by mutex */
687 uint32_t
688 active:1, /* Thread is active and has not been terminated */
689 ipc_active:1, /* IPC with the thread ports is allowed */
690 started:1, /* Thread has been started after creation */
691 static_param:1, /* Disallow policy parameter changes */
692 inspection:1, /* TRUE when task is being inspected by crash reporter */
693 policy_reset:1, /* Disallow policy parameter changes on terminating threads */
694 suspend_parked:1, /* thread parked in thread_suspended */
695 corpse_dup:1, /* TRUE when thread is an inactive duplicate in a corpse */
696 :0;
697
698 decl_lck_mtx_data(, mutex);
699
700 struct ipc_port *ith_special_reply_port; /* ref to special reply port */
701
702 #if CONFIG_DTRACE
703 uint16_t t_dtrace_flags; /* DTrace thread states */
704 #define TH_DTRACE_EXECSUCCESS 0x01
705 uint16_t t_dtrace_inprobe; /* Executing under dtrace_probe */
706 uint32_t t_dtrace_predcache; /* DTrace per thread predicate value hint */
707 int64_t t_dtrace_tracing; /* Thread time under dtrace_probe() */
708 int64_t t_dtrace_vtime;
709 #endif
710
711 clock_sec_t t_page_creation_time;
712 uint32_t t_page_creation_count;
713 uint32_t t_page_creation_throttled;
714 #if (DEVELOPMENT || DEBUG)
715 uint64_t t_page_creation_throttled_hard;
716 uint64_t t_page_creation_throttled_soft;
717 #endif /* DEVELOPMENT || DEBUG */
718 int t_pagein_error; /* for vm_fault(), holds error from vnop_pagein() */
719
720 #ifdef KPERF
721 /* The high 8 bits are the number of frames to sample of a user callstack. */
722 #define T_KPERF_CALLSTACK_DEPTH_OFFSET (24)
723 #define T_KPERF_SET_CALLSTACK_DEPTH(DEPTH) (((uint32_t)(DEPTH)) << T_KPERF_CALLSTACK_DEPTH_OFFSET)
724 #define T_KPERF_GET_CALLSTACK_DEPTH(FLAGS) ((FLAGS) >> T_KPERF_CALLSTACK_DEPTH_OFFSET)
725 #define T_KPERF_ACTIONID_OFFSET (18)
726 #define T_KPERF_SET_ACTIONID(AID) (((uint32_t)(AID)) << T_KPERF_ACTIONID_OFFSET)
727 #define T_KPERF_GET_ACTIONID(FLAGS) ((FLAGS) >> T_KPERF_ACTIONID_OFFSET)
728 #endif
729
730 #define T_KPERF_AST_CALLSTACK 0x1 /* dump a callstack on thread's next AST */
731 #define T_KPERF_AST_DISPATCH 0x2 /* dump a name on thread's next AST */
732 #define T_KPC_ALLOC 0x4 /* thread needs a kpc_buf allocated */
733
734 #define T_KPERF_AST_ALL \
735 (T_KPERF_AST_CALLSTACK | T_KPERF_AST_DISPATCH | T_KPC_ALLOC)
736 /* only go up to T_KPERF_ACTIONID_OFFSET - 1 */
737
738 #ifdef KPERF
739 uint32_t kperf_ast;
740 uint32_t kperf_pet_gen; /* last generation of PET that sampled this thread*/
741 uint32_t kperf_c_switch; /* last dispatch detection */
742 uint32_t kperf_pet_cnt; /* how many times a thread has been sampled by PET */
743 #endif
744
745 #ifdef KPC
746 /* accumulated performance counters for this thread */
747 uint64_t *kpc_buf;
748 #endif
749
750 #if HYPERVISOR
751 /* hypervisor virtual CPU object associated with this thread */
752 void *hv_thread_target;
753 #endif /* HYPERVISOR */
754
755 /* Statistics accumulated per-thread and aggregated per-task */
756 uint32_t syscalls_unix;
757 uint32_t syscalls_mach;
758 ledger_t t_ledger;
759 ledger_t t_threadledger; /* per thread ledger */
760 ledger_t t_bankledger; /* ledger to charge someone */
761 uint64_t t_deduct_bank_ledger_time; /* cpu time to be deducted from bank ledger */
762 uint64_t t_deduct_bank_ledger_energy; /* energy to be deducted from bank ledger */
763
764 uint64_t thread_id; /*system wide unique thread-id*/
765
766 #if MONOTONIC
767 struct mt_thread t_monotonic;
768 #endif /* MONOTONIC */
769
770 /* policy is protected by the thread mutex */
771 struct thread_requested_policy requested_policy;
772 struct thread_effective_policy effective_policy;
773
774 /* usynch override is protected by the task lock, eventually will be thread mutex */
775 struct thread_qos_override {
776 struct thread_qos_override *override_next;
777 uint32_t override_contended_resource_count;
778 int16_t override_qos;
779 int16_t override_resource_type;
780 user_addr_t override_resource;
781 } *overrides;
782
783 uint32_t kevent_overrides;
784 uint8_t user_promotion_basepri;
785 uint8_t kern_promotion_schedpri;
786 _Atomic uint16_t kevent_ast_bits;
787
788 io_stat_info_t thread_io_stats; /* per-thread I/O statistics */
789
790 uint32_t thread_callout_interrupt_wakeups;
791 uint32_t thread_callout_platform_idle_wakeups;
792 uint32_t thread_timer_wakeups_bin_1;
793 uint32_t thread_timer_wakeups_bin_2;
794 thread_tag_t thread_tag;
795
796 /*
797 * callout_* fields are only set for thread call threads whereas guard_exc_fatal is set
798 * by user threads on themselves while taking a guard exception. So it's okay for them to
799 * share this bitfield.
800 */
801 uint16_t
802 callout_woken_from_icontext:1,
803 callout_woken_from_platform_idle:1,
804 callout_woke_thread:1,
805 guard_exc_fatal:1,
806 thread_bitfield_unused:12;
807
808 mach_port_name_t ith_voucher_name;
809 ipc_voucher_t ith_voucher;
810
811 #if CONFIG_THREAD_GROUPS
812 #if CONFIG_PREADOPT_TG
813 /* The preadopt thread group is set on the thread
814 *
815 * a) By another thread when it is a creator and it is scheduled with the
816 * thread group on the TR
817 * b) On itself when it binds a thread request and becomes a
818 * servicer or when it rebinds to the thread request
819 * c) On itself when it processes knotes and finds the first
820 * EVFILT_MACHPORT event to deliver to userspace
821 *
822 * Note that this is a full reference owned by the thread_t and not a
823 * borrowed reference.
824 *
825 * This reference is cleared from the thread_t by the thread itself at the
826 * following times:
827 * a) When it explicitly adopts a work interval or a bank voucher
828 * b) If it still exists on the thread, after it has unbound and is about
829 * to park
830 * c) During thread termination if one still exists
831 * d) When a different preadoption thread group is set on the thread
832 *
833 * It is modified under the thread lock.
834 */
835 struct thread_group *preadopt_thread_group;
836
837 /* This field here is present in order to make sure that the t->thread_group
838 * is always pointing to a valid thread group and isn't a dangling pointer.
839 *
840 * Consider the following scenario:
841 * a) t->thread_group points to the preadoption thread group
842 * b) The preadoption thread group is modified on the thread but we are
843 * unable to resolve the hierarchy immediately due to the current state of
844 * the thread
845 *
846 * In order to make sure that t->thread_group points to a valid thread
847 * group until we can resolve the hierarchy again, we save the existing
848 * thread_group it points to in old_preadopt_thread_group. The next time a
849 * hierarchy resolution is done, we know that t->thread_group will not point
850 * to this field anymore so we can clear it.
851 *
852 * This field is always going to take the reference that was previously in
853 * preadopt_thread_group so it will have a full +1
854 */
855 struct thread_group *old_preadopt_thread_group;
856 #endif /* CONFIG_PREADOPT_TG */
857
858 /* This is a borrowed reference to the TG from the ith_voucher and is saved
859 * here since we may not always be in the right context to able to do the
860 * lookups.
861 *
862 * It is set always set on self under the thread lock */
863 struct thread_group *bank_thread_group;
864
865 /* Whether this is the autojoin thread group or the work interval thread
866 * group depends on whether the thread's sched_flags has the
867 * TH_SFLAG_THREAD_GROUP_AUTO_JOIN bit set */
868 union {
869 /* This is a borrowed reference to the auto join thread group from the
870 * work_interval. It is set with the thread lock held */
871 struct thread_group *auto_join_thread_group;
872 /* This is a borrowed reference to the explicit work_interval thread group
873 * and is always set on self */
874 struct thread_group *work_interval_thread_group;
875 };
876 #endif /* CONFIG_THREAD_GROUPS */
877
878 /* work interval (if any) associated with the thread. Only modified by
879 * current thread on itself or when another thread when the thread is held
880 * off of runq */
881 struct work_interval *th_work_interval;
882 thread_work_interval_flags_t th_work_interval_flags;
883
884 #define THREAD_BOUND_CLUSTER_NONE (UINT32_MAX)
885 uint32_t th_bound_cluster_id;
886
887 #if SCHED_TRACE_THREAD_WAKEUPS
888 uintptr_t thread_wakeup_bt[64];
889 #endif
890 turnstile_update_flags_t inheritor_flags; /* inheritor flags for inheritor field */
891 block_hint_t pending_block_hint;
892 block_hint_t block_hint; /* What type of primitive last caused us to block. */
893 uint32_t decompressions; /* Per-thread decompressions counter to be added to per-task decompressions counter */
894 int thread_region_page_shift; /* Page shift that this thread would like to use when */
895 /* introspecting a task. This is currently being used */
896 /* by footprint which uses a thread for each task being inspected. */
897 #if CONFIG_IOSCHED
898 void *decmp_upl;
899 #endif /* CONFIG_IOSCHED */
900 };
901
902 #define ith_state saved.receive.state
903 #define ith_object saved.receive.object
904 #define ith_msg_addr saved.receive.msg_addr
905 #define ith_rsize saved.receive.rsize
906 #define ith_msize saved.receive.msize
907 #define ith_option saved.receive.option
908 #define ith_receiver_name saved.receive.receiver_name
909 #define ith_continuation saved.receive.continuation
910 #define ith_kmsg saved.receive.kmsg
911 #define ith_peekq saved.receive.peekq
912 #define ith_knote saved.receive.knote
913 #define ith_ppriority saved.receive.received_qos.ppri
914 #define ith_qos_override saved.receive.received_qos.oqos
915 #define ith_seqno saved.receive.seqno
916
917 #define sth_waitsemaphore saved.sema.waitsemaphore
918 #define sth_signalsemaphore saved.sema.signalsemaphore
919 #define sth_options saved.sema.options
920 #define sth_result saved.sema.result
921 #define sth_continuation saved.sema.continuation
922
923 #define ITH_KNOTE_NULL ((void *)NULL)
924 #define ITH_KNOTE_PSEUDO ((void *)0xdeadbeef)
925 /*
926 * The ith_knote is used during message delivery, and can safely be interpreted
927 * only when used for one of these codepaths, which the test for the msgt_name
928 * being RECEIVE or SEND_ONCE is about.
929 */
930 #define ITH_KNOTE_VALID(kn, msgt_name) \
931 (((kn) != ITH_KNOTE_NULL && (kn) != ITH_KNOTE_PSEUDO) && \
932 ((msgt_name) == MACH_MSG_TYPE_PORT_RECEIVE || \
933 (msgt_name) == MACH_MSG_TYPE_PORT_SEND_ONCE))
934
935 #if MACH_ASSERT
936 #define assert_thread_magic(thread) assertf((thread)->thread_magic == THREAD_MAGIC, \
937 "bad thread magic 0x%llx for thread %p, expected 0x%llx", \
938 (thread)->thread_magic, (thread), THREAD_MAGIC)
939 #else
940 #define assert_thread_magic(thread) do { (void)(thread); } while (0)
941 #endif
942
943 extern thread_t thread_bootstrap(void);
944
945 extern void thread_machine_init_template(void);
946
947 extern void thread_init(void);
948
949 extern void thread_daemon_init(void);
950
951 extern void thread_reference(
952 thread_t thread);
953
954 extern void thread_deallocate(
955 thread_t thread);
956
957 extern void thread_inspect_deallocate(
958 thread_inspect_t thread);
959
960 extern void thread_read_deallocate(
961 thread_read_t thread);
962
963 extern void thread_terminate_self(void);
964
965 extern kern_return_t thread_terminate_internal(
966 thread_t thread);
967
968 extern void thread_start(
969 thread_t thread) __attribute__ ((noinline));
970
971 extern void thread_start_in_assert_wait(
972 thread_t thread,
973 event_t event,
974 wait_interrupt_t interruptible) __attribute__ ((noinline));
975
976 extern void thread_terminate_enqueue(
977 thread_t thread);
978
979 extern void thread_exception_enqueue(
980 task_t task,
981 thread_t thread,
982 exception_type_t etype);
983
984 extern void thread_copy_resource_info(
985 thread_t dst_thread,
986 thread_t src_thread);
987
988 extern void thread_terminate_crashed_threads(void);
989
990 extern void thread_stack_enqueue(
991 thread_t thread);
992
993 extern void thread_hold(
994 thread_t thread);
995
996 extern void thread_release(
997 thread_t thread);
998
999 extern void thread_corpse_continue(void) __dead2;
1000
1001 extern boolean_t thread_is_active(thread_t thread);
1002
1003 extern lck_grp_t thread_lck_grp;
1004
1005 /* Locking for scheduler state, always acquired with interrupts disabled (splsched()) */
1006 #define thread_lock_init(th) simple_lock_init(&(th)->sched_lock, 0)
1007 #define thread_lock(th) simple_lock(&(th)->sched_lock, &thread_lck_grp)
1008 #define thread_unlock(th) simple_unlock(&(th)->sched_lock)
1009
1010 #define wake_lock_init(th) simple_lock_init(&(th)->wake_lock, 0)
1011 #define wake_lock(th) simple_lock(&(th)->wake_lock, &thread_lck_grp)
1012 #define wake_unlock(th) simple_unlock(&(th)->wake_lock)
1013
1014 #define thread_should_halt_fast(thread) (!(thread)->active)
1015
1016 extern void stack_alloc(
1017 thread_t thread);
1018
1019 extern void stack_handoff(
1020 thread_t from,
1021 thread_t to);
1022
1023 extern void stack_free(
1024 thread_t thread);
1025
1026 extern void stack_free_reserved(
1027 thread_t thread);
1028
1029 extern boolean_t stack_alloc_try(
1030 thread_t thread);
1031
1032 extern void stack_collect(void);
1033
1034 extern kern_return_t thread_info_internal(
1035 thread_t thread,
1036 thread_flavor_t flavor,
1037 thread_info_t thread_info_out,
1038 mach_msg_type_number_t *thread_info_count);
1039
1040
1041
1042 extern kern_return_t kernel_thread_create(
1043 thread_continue_t continuation,
1044 void *parameter,
1045 integer_t priority,
1046 thread_t *new_thread);
1047
1048 extern kern_return_t kernel_thread_start_priority(
1049 thread_continue_t continuation,
1050 void *parameter,
1051 integer_t priority,
1052 thread_t *new_thread);
1053
1054 extern void machine_stack_attach(
1055 thread_t thread,
1056 vm_offset_t stack);
1057
1058 extern vm_offset_t machine_stack_detach(
1059 thread_t thread);
1060
1061 extern void machine_stack_handoff(
1062 thread_t old,
1063 thread_t new);
1064
1065 extern thread_t machine_switch_context(
1066 thread_t old_thread,
1067 thread_continue_t continuation,
1068 thread_t new_thread);
1069
1070 extern void machine_load_context(
1071 thread_t thread) __attribute__((noreturn));
1072
1073 extern void machine_thread_state_initialize(
1074 thread_t thread);
1075
1076 extern kern_return_t machine_thread_set_state(
1077 thread_t thread,
1078 thread_flavor_t flavor,
1079 thread_state_t state,
1080 mach_msg_type_number_t count);
1081
1082 extern mach_vm_address_t machine_thread_pc(
1083 thread_t thread);
1084
1085 extern void machine_thread_reset_pc(
1086 thread_t thread,
1087 mach_vm_address_t pc);
1088
1089 extern boolean_t machine_thread_on_core(
1090 thread_t thread);
1091
1092 extern kern_return_t machine_thread_get_state(
1093 thread_t thread,
1094 thread_flavor_t flavor,
1095 thread_state_t state,
1096 mach_msg_type_number_t *count);
1097
1098 extern kern_return_t machine_thread_state_convert_from_user(
1099 thread_t thread,
1100 thread_flavor_t flavor,
1101 thread_state_t tstate,
1102 mach_msg_type_number_t count,
1103 thread_state_t old_tstate,
1104 mach_msg_type_number_t old_count,
1105 thread_set_status_flags_t tssf_flags);
1106
1107 extern kern_return_t machine_thread_state_convert_to_user(
1108 thread_t thread,
1109 thread_flavor_t flavor,
1110 thread_state_t tstate,
1111 mach_msg_type_number_t *count,
1112 thread_set_status_flags_t tssf_flags);
1113
1114 extern kern_return_t machine_thread_dup(
1115 thread_t self,
1116 thread_t target,
1117 boolean_t is_corpse);
1118
1119 extern void machine_thread_init(void);
1120
1121 extern void machine_thread_template_init(thread_t thr_template);
1122
1123
1124 extern void machine_thread_create(
1125 thread_t thread,
1126 task_t task,
1127 bool first_thread);
1128 extern void machine_thread_switch_addrmode(
1129 thread_t thread);
1130
1131 extern void machine_thread_destroy(
1132 thread_t thread);
1133
1134 extern void machine_set_current_thread(
1135 thread_t thread);
1136
1137 extern kern_return_t machine_thread_get_kern_state(
1138 thread_t thread,
1139 thread_flavor_t flavor,
1140 thread_state_t tstate,
1141 mach_msg_type_number_t *count);
1142
1143 extern kern_return_t machine_thread_inherit_taskwide(
1144 thread_t thread,
1145 task_t parent_task);
1146
1147 extern kern_return_t machine_thread_set_tsd_base(
1148 thread_t thread,
1149 mach_vm_offset_t tsd_base);
1150
1151 #define thread_mtx_try(thread) lck_mtx_try_lock(&(thread)->mutex)
1152 #define thread_mtx_held(thread) lck_mtx_assert(&(thread)->mutex, LCK_MTX_ASSERT_OWNED)
1153
1154 extern void thread_apc_ast(thread_t thread);
1155
1156 extern void thread_update_qos_cpu_time(thread_t thread);
1157
1158 void act_machine_sv_free(thread_t, int);
1159
1160 vm_offset_t min_valid_stack_address(void);
1161 vm_offset_t max_valid_stack_address(void);
1162
1163 extern bool thread_no_smt(thread_t thread);
1164 extern bool processor_active_thread_no_smt(processor_t processor);
1165
1166 extern void thread_set_options(uint32_t thopt);
1167
1168 #if CONFIG_THREAD_GROUPS
1169 struct thread_group *thread_get_current_voucher_thread_group(thread_t thread);
1170 #endif /* CONFIG_THREAD_GROUPS */
1171
1172 #endif /* MACH_KERNEL_PRIVATE */
1173 #if BSD_KERNEL_PRIVATE
1174
1175 /* Duplicated from osfmk/kern/ipc_tt.h */
1176 __options_decl(port_intrans_options_t, uint32_t, {
1177 PORT_INTRANS_OPTIONS_NONE = 0x0000,
1178 PORT_INTRANS_THREAD_IN_CURRENT_TASK = 0x0001,
1179 PORT_INTRANS_THREAD_NOT_CURRENT_THREAD = 0x0002,
1180
1181 PORT_INTRANS_SKIP_TASK_EVAL = 0x0004,
1182 PORT_INTRANS_ALLOW_CORPSE_TASK = 0x0008,
1183 });
1184
1185 extern thread_t port_name_to_thread(
1186 mach_port_name_t port_name,
1187 port_intrans_options_t options);
1188
1189 #endif /* BSD_KERNEL_PRIVATE */
1190 #ifdef XNU_KERNEL_PRIVATE
1191
1192 extern void thread_require(
1193 thread_t thread);
1194
1195 extern void thread_deallocate_safe(
1196 thread_t thread);
1197
1198 extern uint64_t thread_rettokern_addr(
1199 thread_t thread);
1200
1201 extern uint64_t thread_wqquantum_addr(
1202 thread_t thread);
1203
1204 extern integer_t thread_kern_get_pri(thread_t thr) __pure2;
1205
1206 extern void thread_kern_set_pri(thread_t thr, integer_t pri);
1207
1208 extern integer_t thread_kern_get_kernel_maxpri(void) __pure2;
1209
1210 uint16_t thread_set_tag(thread_t thread, uint16_t tag);
1211 uint16_t thread_get_tag(thread_t thread);
1212
1213 __options_decl(shared_rsrc_policy_agent_t, uint32_t, {
1214 SHARED_RSRC_POLICY_AGENT_DISPATCH = 0,
1215 SHARED_RSRC_POLICY_AGENT_SYSCTL = 1,
1216 SHARED_RSRC_POLICY_AGENT_PERFCTL_CSW = 2,
1217 SHARED_RSRC_POLICY_AGENT_PERFCTL_QUANTUM = 3,
1218 });
1219
1220 boolean_t thread_shared_rsrc_policy_get(thread_t thread, cluster_shared_rsrc_type_t type);
1221 kern_return_t thread_shared_rsrc_policy_set(thread_t thread, uint32_t index, cluster_shared_rsrc_type_t type, shared_rsrc_policy_agent_t agent);
1222 kern_return_t thread_shared_rsrc_policy_clear(thread_t thread, cluster_shared_rsrc_type_t type, shared_rsrc_policy_agent_t agent);
1223
1224 #ifdef MACH_KERNEL_PRIVATE
1225 static inline thread_tag_t
thread_set_tag_internal(thread_t thread,thread_tag_t tag)1226 thread_set_tag_internal(thread_t thread, thread_tag_t tag)
1227 {
1228 return os_atomic_or_orig(&thread->thread_tag, tag, relaxed);
1229 }
1230
1231 static inline thread_tag_t
thread_get_tag_internal(thread_t thread)1232 thread_get_tag_internal(thread_t thread)
1233 {
1234 return thread->thread_tag;
1235 }
1236 #endif /* MACH_KERNEL_PRIVATE */
1237
1238 uint64_t thread_last_run_time(thread_t thread);
1239
1240 extern kern_return_t thread_state_initialize(
1241 thread_t thread);
1242
1243 extern kern_return_t thread_setstatus(
1244 thread_t thread,
1245 int flavor,
1246 thread_state_t tstate,
1247 mach_msg_type_number_t count);
1248
1249 extern kern_return_t thread_setstatus_from_user(
1250 thread_t thread,
1251 int flavor,
1252 thread_state_t tstate,
1253 mach_msg_type_number_t count,
1254 thread_state_t old_tstate,
1255 mach_msg_type_number_t old_count,
1256 thread_set_status_flags_t flags);
1257
1258 extern kern_return_t thread_getstatus(
1259 thread_t thread,
1260 int flavor,
1261 thread_state_t tstate,
1262 mach_msg_type_number_t *count);
1263
1264 extern void main_thread_set_immovable_pinned(thread_t thread);
1265
1266 extern kern_return_t thread_getstatus_to_user(
1267 thread_t thread,
1268 int flavor,
1269 thread_state_t tstate,
1270 mach_msg_type_number_t *count,
1271 thread_set_status_flags_t flags);
1272
1273 extern kern_return_t thread_create_with_continuation(
1274 task_t task,
1275 thread_t *new_thread,
1276 thread_continue_t continuation);
1277
1278 extern kern_return_t main_thread_create_waiting(task_t task,
1279 thread_continue_t continuation,
1280 event_t event,
1281 thread_t *new_thread);
1282
1283 extern kern_return_t thread_create_workq_waiting(
1284 task_t task,
1285 thread_continue_t thread_return,
1286 thread_t *new_thread);
1287
1288 extern void thread_yield_internal(
1289 mach_msg_timeout_t interval);
1290
1291 extern void thread_yield_to_preemption(void);
1292
1293 extern void thread_depress_timer_setup(thread_t self);
1294
1295 /*
1296 * Thread-private CPU limits: apply a private CPU limit to this thread only. Available actions are:
1297 *
1298 * 1) Block. Prevent CPU consumption of the thread from exceeding the limit.
1299 * 2) Exception. Generate a resource consumption exception when the limit is exceeded.
1300 * 3) Disable. Remove any existing CPU limit.
1301 */
1302 #define THREAD_CPULIMIT_BLOCK 0x1
1303 #define THREAD_CPULIMIT_EXCEPTION 0x2
1304 #define THREAD_CPULIMIT_DISABLE 0x3
1305
1306 struct _thread_ledger_indices {
1307 int cpu_time;
1308 };
1309
1310 extern struct _thread_ledger_indices thread_ledgers;
1311
1312 extern int thread_get_cpulimit(int *action, uint8_t *percentage, uint64_t *interval_ns);
1313 extern int thread_set_cpulimit(int action, uint8_t percentage, uint64_t interval_ns);
1314
1315 extern void thread_read_times(
1316 thread_t thread,
1317 time_value_t *user_time,
1318 time_value_t *system_time,
1319 time_value_t *runnable_time);
1320
1321 extern uint64_t thread_get_runtime_self(void);
1322
1323 extern void thread_setuserstack(
1324 thread_t thread,
1325 mach_vm_offset_t user_stack);
1326
1327 extern user_addr_t thread_adjuserstack(
1328 thread_t thread,
1329 int adjust);
1330
1331
1332 extern void thread_setentrypoint(
1333 thread_t thread,
1334 mach_vm_offset_t entry);
1335
1336 extern kern_return_t thread_set_tsd_base(
1337 thread_t thread,
1338 mach_vm_offset_t tsd_base);
1339
1340 extern kern_return_t thread_setsinglestep(
1341 thread_t thread,
1342 int on);
1343
1344 extern kern_return_t thread_userstack(
1345 thread_t,
1346 int,
1347 thread_state_t,
1348 unsigned int,
1349 mach_vm_offset_t *,
1350 int *,
1351 boolean_t);
1352
1353 extern kern_return_t thread_entrypoint(
1354 thread_t,
1355 int,
1356 thread_state_t,
1357 unsigned int,
1358 mach_vm_offset_t *);
1359
1360 extern kern_return_t thread_userstackdefault(
1361 mach_vm_offset_t *,
1362 boolean_t);
1363
1364 extern kern_return_t thread_wire_internal(
1365 host_priv_t host_priv,
1366 thread_t thread,
1367 boolean_t wired,
1368 boolean_t *prev_state);
1369
1370
1371 extern kern_return_t thread_dup(thread_t);
1372
1373 extern kern_return_t thread_dup2(thread_t, thread_t);
1374
1375 #if !defined(_SCHED_CALL_T_DEFINED)
1376 #define _SCHED_CALL_T_DEFINED
1377 typedef void (*sched_call_t)(
1378 int type,
1379 thread_t thread);
1380 #endif
1381
1382 #define SCHED_CALL_BLOCK 0x1
1383 #define SCHED_CALL_UNBLOCK 0x2
1384
1385 extern void thread_sched_call(
1386 thread_t thread,
1387 sched_call_t call);
1388
1389 extern boolean_t thread_is_static_param(
1390 thread_t thread);
1391
1392 extern task_t get_threadtask(thread_t) __pure2;
1393
1394 extern task_t get_threadtask_early(thread_t) __pure2;
1395
1396 /*
1397 * Thread is running within a 64-bit address space.
1398 */
1399 #define thread_is_64bit_addr(thd) \
1400 task_has_64Bit_addr(get_threadtask(thd))
1401
1402 /*
1403 * Thread is using 64-bit machine state.
1404 */
1405 #define thread_is_64bit_data(thd) \
1406 task_has_64Bit_data(get_threadtask(thd))
1407
1408 struct uthread;
1409
1410 #if defined(__x86_64__)
1411 extern int thread_task_has_ldt(thread_t);
1412 #endif
1413 extern void set_thread_pagein_error(thread_t, int);
1414 extern event_t workq_thread_init_and_wq_lock(task_t, thread_t); // bsd/pthread/
1415
1416 struct proc;
1417 struct uthread;
1418 extern const size_t uthread_size;
1419 extern thread_ro_t get_thread_ro_unchecked(thread_t) __pure2;
1420 extern thread_ro_t get_thread_ro(thread_t) __pure2;
1421 extern thread_ro_t current_thread_ro_unchecked(void) __pure2;
1422 extern thread_ro_t current_thread_ro(void) __pure2;
1423 extern void clear_thread_ro_proc(thread_t);
1424 extern struct uthread *get_bsdthread_info(thread_t) __pure2;
1425 extern thread_t get_machthread(struct uthread *) __pure2;
1426 extern uint64_t uthread_tid(struct uthread *) __pure2;
1427 extern user_addr_t thread_get_sigreturn_token(thread_t thread);
1428 extern uint32_t thread_get_sigreturn_diversifier(thread_t thread);
1429 extern void uthread_init(task_t, struct uthread *, thread_ro_t, int);
1430 extern void uthread_cleanup_name(struct uthread *uthread);
1431 extern void uthread_cleanup(struct uthread *, thread_ro_t);
1432 extern void uthread_cred_ref(struct ucred *);
1433 extern void uthread_cred_free(struct ucred *);
1434 extern void uthread_destroy(struct uthread *);
1435 extern void uthread_reset_proc_refcount(struct uthread *);
1436 extern void thread_ro_update_cred(thread_ro_t, struct ucred *);
1437 extern void thread_ro_update_flags(thread_ro_t, thread_ro_flags_t add, thread_ro_flags_t clr);
1438 extern bool uthread_is64bit(struct uthread *uth) __pure2;
1439 #if PROC_REF_DEBUG
1440 extern void uthread_init_proc_refcount(struct uthread *);
1441 extern void uthread_destroy_proc_refcount(struct uthread *);
1442 extern void uthread_assert_zero_proc_refcount(struct uthread *);
1443 #else
1444 #define uthread_init_proc_refcount(uth) ((void)(uth))
1445 #define uthread_destroy_proc_refcount(uth) ((void)(uth))
1446 #define uthread_assert_zero_proc_refcount(uth) ((void)(uth))
1447 #endif
1448 #if CONFIG_DEBUG_SYSCALL_REJECTION
1449 extern uint64_t *uthread_get_syscall_rejection_mask(void *);
1450 #endif /* CONFIG_DEBUG_SYSCALL_REJECTION */
1451 extern mach_port_name_t uthread_joiner_port(struct uthread *);
1452 extern user_addr_t uthread_joiner_address(struct uthread *);
1453 extern void uthread_joiner_wake(task_t task, struct uthread *);
1454
1455 extern boolean_t thread_should_halt(
1456 thread_t thread);
1457
1458 extern boolean_t thread_should_abort(
1459 thread_t);
1460
1461 extern int is_64signalregset(void);
1462
1463 extern void act_set_kperf(thread_t);
1464 extern void act_set_astledger(thread_t thread);
1465 extern void act_set_astledger_async(thread_t thread);
1466 extern void act_set_io_telemetry_ast(thread_t);
1467 extern void act_set_macf_telemetry_ast(thread_t);
1468 extern void act_set_astproc_resource(thread_t);
1469
1470 extern vm_offset_t thread_get_kernel_stack(thread_t);
1471
1472 extern uint32_t dtrace_get_thread_predcache(thread_t);
1473 extern int64_t dtrace_get_thread_vtime(thread_t);
1474 extern int64_t dtrace_get_thread_tracing(thread_t);
1475 extern uint16_t dtrace_get_thread_inprobe(thread_t);
1476 extern int dtrace_get_thread_last_cpu_id(thread_t);
1477 extern vm_offset_t dtrace_get_kernel_stack(thread_t);
1478 #define dtrace_get_kernel_stack thread_get_kernel_stack
1479 extern void dtrace_set_thread_predcache(thread_t, uint32_t);
1480 extern void dtrace_set_thread_vtime(thread_t, int64_t);
1481 extern void dtrace_set_thread_tracing(thread_t, int64_t);
1482 extern void dtrace_set_thread_inprobe(thread_t, uint16_t);
1483 extern void dtrace_thread_bootstrap(void);
1484 extern void dtrace_thread_didexec(thread_t);
1485
1486 extern int64_t dtrace_calc_thread_recent_vtime(thread_t);
1487
1488
1489 extern kern_return_t thread_set_wq_state32(
1490 thread_t thread,
1491 thread_state_t tstate);
1492
1493 extern kern_return_t thread_set_wq_state64(
1494 thread_t thread,
1495 thread_state_t tstate);
1496
1497 extern vm_offset_t kernel_stack_mask;
1498 extern vm_offset_t kernel_stack_size;
1499 extern vm_offset_t kernel_stack_depth_max;
1500
1501 extern void guard_ast(thread_t);
1502 extern void fd_guard_ast(thread_t,
1503 mach_exception_code_t, mach_exception_subcode_t);
1504 #if CONFIG_VNGUARD
1505 extern void vn_guard_ast(thread_t,
1506 mach_exception_code_t, mach_exception_subcode_t);
1507 #endif
1508 extern void mach_port_guard_ast(thread_t,
1509 mach_exception_code_t, mach_exception_subcode_t);
1510 extern void virt_memory_guard_ast(thread_t,
1511 mach_exception_code_t, mach_exception_subcode_t);
1512 extern void thread_guard_violation(thread_t,
1513 mach_exception_code_t, mach_exception_subcode_t, boolean_t);
1514 extern void thread_update_io_stats(thread_t, int size, int io_flags);
1515
1516 extern kern_return_t thread_set_voucher_name(mach_port_name_t name);
1517 extern kern_return_t thread_get_current_voucher_origin_pid(int32_t *pid);
1518
1519 extern void thread_enable_send_importance(thread_t thread, boolean_t enable);
1520
1521 /*
1522 * Translate signal context data pointer to userspace representation
1523 */
1524
1525 extern kern_return_t machine_thread_siguctx_pointer_convert_to_user(
1526 thread_t thread,
1527 user_addr_t *uctxp);
1528
1529 extern void machine_tecs(thread_t thr);
1530
1531 typedef enum cpuvn {
1532 CPUVN_CI = 1
1533 } cpuvn_e;
1534
1535 extern int machine_csv(cpuvn_e cve);
1536 #if defined(__x86_64__)
1537 extern void machine_thread_set_insn_copy_optout(thread_t thr);
1538 #endif
1539
1540 /*
1541 * Translate array of function pointer syscall arguments from userspace representation
1542 */
1543
1544 extern kern_return_t machine_thread_function_pointers_convert_from_user(
1545 thread_t thread,
1546 user_addr_t *fptrs,
1547 uint32_t count);
1548
1549 /*
1550 * Get the duration of the given thread's last wait.
1551 */
1552 uint64_t thread_get_last_wait_duration(thread_t thread);
1553
1554 extern bool thread_get_no_smt(void);
1555 #if defined(__x86_64__)
1556 extern bool curtask_get_insn_copy_optout(void);
1557 extern void curtask_set_insn_copy_optout(void);
1558 #endif /* defined(__x86_64__) */
1559
1560 #endif /* XNU_KERNEL_PRIVATE */
1561 #ifdef KERNEL_PRIVATE
1562
1563 typedef struct thread_pri_floor {
1564 thread_t thread;
1565 } thread_pri_floor_t;
1566
1567 #ifdef MACH_KERNEL_PRIVATE
1568 extern void thread_floor_boost_ast(thread_t thread);
1569 extern void thread_floor_boost_set_promotion_locked(thread_t thread);
1570 #endif /* MACH_KERNEL_PRIVATE */
1571
1572 /*! @function thread_priority_floor_start
1573 * @abstract boost the current thread priority to floor.
1574 * @discussion Increase the priority of the current thread to at least MINPRI_FLOOR.
1575 * The boost will be mantained until a corresponding thread_priority_floor_end()
1576 * is called. Every call of thread_priority_floor_start() needs to have a corresponding
1577 * call to thread_priority_floor_end() from the same thread.
1578 * No thread can return to userspace before calling thread_priority_floor_end().
1579 *
1580 * NOTE: avoid to use this function. Try to use gate_t or sleep_with_inheritor()
1581 * instead.
1582 * @result a token to be given to the corresponding thread_priority_floor_end()
1583 */
1584 extern thread_pri_floor_t thread_priority_floor_start(void);
1585 /*! @function thread_priority_floor_end
1586 * @abstract ends the floor boost.
1587 * @param token the token obtained from thread_priority_floor_start()
1588 * @discussion ends the priority floor boost started with thread_priority_floor_start()
1589 */
1590 extern void thread_priority_floor_end(thread_pri_floor_t *token);
1591
1592 extern void thread_set_no_smt(bool set);
1593
1594 extern void thread_mtx_lock(thread_t thread);
1595
1596 extern void thread_mtx_unlock(thread_t thread);
1597
1598 extern uint64_t thread_dispatchqaddr(
1599 thread_t thread);
1600
1601 bool thread_is_eager_preempt(thread_t thread);
1602 void thread_set_eager_preempt(thread_t thread);
1603 void thread_clear_eager_preempt(thread_t thread);
1604 void thread_set_honor_qlimit(thread_t thread);
1605 void thread_clear_honor_qlimit(thread_t thread);
1606 extern ipc_port_t convert_thread_to_port(thread_t);
1607 extern ipc_port_t convert_thread_to_port_pinned(thread_t);
1608 extern ipc_port_t convert_thread_inspect_to_port(thread_inspect_t);
1609 extern ipc_port_t convert_thread_read_to_port(thread_read_t);
1610 extern boolean_t is_external_pageout_thread(void);
1611 extern boolean_t is_vm_privileged(void);
1612 extern boolean_t set_vm_privilege(boolean_t);
1613 extern kern_allocation_name_t thread_set_allocation_name(kern_allocation_name_t new_name);
1614 extern void *thread_iokit_tls_get(uint32_t index);
1615 extern void thread_iokit_tls_set(uint32_t index, void * data);
1616 extern int thread_self_region_page_shift(void);
1617 extern void thread_self_region_page_shift_set(int pgshift);
1618 extern kern_return_t thread_create_immovable(task_t task, thread_t *new_thread);
1619 extern kern_return_t thread_terminate_pinned(thread_t thread);
1620
1621 struct thread_attr_for_ipc_propagation;
1622 extern kern_return_t thread_get_ipc_propagate_attr(thread_t thread, struct thread_attr_for_ipc_propagation *attr);
1623
1624 #endif /* KERNEL_PRIVATE */
1625 #ifdef XNU_KERNEL_PRIVATE
1626
1627 extern void
1628 thread_get_thread_name(thread_t th, char* name);
1629
1630 extern bool thread_supports_cooperative_workqueue(thread_t thread);
1631 extern void thread_arm_workqueue_quantum(thread_t thread);
1632 extern void thread_disarm_workqueue_quantum(thread_t thread);
1633
1634 extern void thread_evaluate_workqueue_quantum_expiry(thread_t thread);
1635 extern bool thread_has_expired_workqueue_quantum(thread_t thread, bool should_trace);
1636
1637 #endif /* XNU_KERNEL_PRIVATE */
1638
1639 /*! @function thread_has_thread_name
1640 * @abstract Checks if a thread has a name.
1641 * @discussion This function takes one input, a thread, and returns
1642 * a boolean value indicating if that thread already has a name associated
1643 * with it.
1644 * @param th The thread to inspect.
1645 * @result TRUE if the thread has a name, FALSE otherwise.
1646 */
1647 extern boolean_t thread_has_thread_name(thread_t th);
1648
1649 /*! @function thread_set_thread_name
1650 * @abstract Set a thread's name.
1651 * @discussion This function takes two input parameters: a thread to name,
1652 * and the name to apply to the thread. The name will be copied over to
1653 * the thread in order to better identify the thread. If the name is
1654 * longer than MAXTHREADNAMESIZE - 1, it will be truncated.
1655 * @param th The thread to be named.
1656 * @param name The name to apply to the thread.
1657 */
1658 extern void thread_set_thread_name(thread_t th, const char* name);
1659
1660 extern thread_t current_thread(void) __pure2;
1661
1662 extern uint64_t thread_tid(thread_t thread) __pure2;
1663
1664 extern void thread_reference(
1665 thread_t thread);
1666
1667 extern void thread_deallocate(
1668 thread_t thread);
1669
1670 /*! @function kernel_thread_start
1671 * @abstract Create a kernel thread.
1672 * @discussion This function takes three input parameters, namely reference
1673 * to the function that the thread should execute, caller specified data
1674 * and a reference which is used to return the newly created kernel
1675 * thread. The function returns KERN_SUCCESS on success or an appropriate
1676 * kernel code type indicating the error. It may be noted that the caller
1677 * is responsible for explicitly releasing the reference to the created
1678 * thread when no longer needed. This should be done by calling
1679 * thread_deallocate(new_thread).
1680 * @param continuation A C-function pointer where the thread will begin execution.
1681 * @param parameter Caller specified data to be passed to the new thread.
1682 * @param new_thread Reference to the new thread is returned in this parameter.
1683 * @result Returns KERN_SUCCESS on success or an appropriate kernel code type.
1684 */
1685
1686 extern kern_return_t kernel_thread_start(
1687 thread_continue_t continuation,
1688 void *parameter,
1689 thread_t *new_thread);
1690
1691 __END_DECLS
1692
1693 #endif /* _KERN_THREAD_H_ */
1694