]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/thread.h
xnu-3789.31.2.tar.gz
[apple/xnu.git] / osfmk / kern / thread.h
1 /*
2 * Copyright (c) 2000-2016 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 Software.Distribution@CS.CMU.EDU
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 csl-dist@cs.utah.edu 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/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>
95
96 #include <kern/kern_types.h>
97
98 #include <sys/cdefs.h>
99
100 #ifdef MACH_KERNEL_PRIVATE
101
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 #include <kern/timer.h>
112 #include <kern/simple_lock.h>
113 #include <kern/locks.h>
114 #include <kern/sched.h>
115 #include <kern/sched_prim.h>
116 #include <mach/sfi_class.h>
117 #include <kern/thread_call.h>
118 #include <kern/timer_call.h>
119 #include <kern/task.h>
120 #include <kern/exception.h>
121 #include <kern/affinity.h>
122
123 #include <kern/waitq.h>
124
125 #include <ipc/ipc_kmsg.h>
126
127 #include <machine/cpu_data.h>
128 #include <machine/thread.h>
129
130
131 struct thread {
132
133 #if MACH_ASSERT
134 #define THREAD_MAGIC 0x1234ABCDDCBA4321ULL
135 /* Ensure nothing uses &thread as a queue entry */
136 uint64_t thread_magic;
137 #endif /* MACH_ASSERT */
138
139 /*
140 * NOTE: The runq field in the thread structure has an unusual
141 * locking protocol. If its value is PROCESSOR_NULL, then it is
142 * locked by the thread_lock, but if its value is something else
143 * then it is locked by the associated run queue lock. It is
144 * set to PROCESSOR_NULL without holding the thread lock, but the
145 * transition from PROCESSOR_NULL to non-null must be done
146 * under the thread lock and the run queue lock.
147 *
148 * New waitq APIs allow the 'links' and 'runq' fields to be
149 * anywhere in the thread structure.
150 */
151 union {
152 queue_chain_t runq_links; /* run queue links */
153 queue_chain_t wait_links; /* wait queue links */
154 };
155
156 processor_t runq; /* run queue assignment */
157
158 event64_t wait_event; /* wait queue event */
159 struct waitq *waitq; /* wait queue this thread is enqueued on */
160
161 /* Data updated during assert_wait/thread_wakeup */
162 #if __SMP__
163 decl_simple_lock_data(,sched_lock) /* scheduling lock (thread_lock()) */
164 decl_simple_lock_data(,wake_lock) /* for thread stop / wait (wake_lock()) */
165 #endif
166 integer_t options; /* options set by thread itself */
167 #define TH_OPT_INTMASK 0x0003 /* interrupt / abort level */
168 #define TH_OPT_VMPRIV 0x0004 /* may allocate reserved memory */
169 #define TH_OPT_DTRACE 0x0008 /* executing under dtrace_probe */
170 #define TH_OPT_SYSTEM_CRITICAL 0x0010 /* Thread must always be allowed to run - even under heavy load */
171 #define TH_OPT_PROC_CPULIMIT 0x0020 /* Thread has a task-wide CPU limit applied to it */
172 #define TH_OPT_PRVT_CPULIMIT 0x0040 /* Thread has a thread-private CPU limit applied to it */
173 #define TH_OPT_IDLE_THREAD 0x0080 /* Thread is a per-processor idle thread */
174 #define TH_OPT_GLOBAL_FORCED_IDLE 0x0100 /* Thread performs forced idle for thermal control */
175 #define TH_OPT_SCHED_VM_GROUP 0x0200 /* Thread belongs to special scheduler VM group */
176 #define TH_OPT_HONOR_QLIMIT 0x0400 /* Thread will honor qlimit while sending mach_msg, regardless of MACH_SEND_ALWAYS */
177 #define TH_OPT_SEND_IMPORTANCE 0x0800 /* Thread will allow importance donation from kernel rpc */
178
179 boolean_t wake_active; /* wake event on stop */
180 int at_safe_point; /* thread_abort_safely allowed */
181 ast_t reason; /* why we blocked */
182 uint32_t quantum_remaining;
183 wait_result_t wait_result; /* outcome of wait -
184 * may be examined by this thread
185 * WITHOUT locking */
186 thread_continue_t continuation; /* continue here next dispatch */
187 void *parameter; /* continuation parameter */
188
189 /* Data updated/used in thread_invoke */
190 vm_offset_t kernel_stack; /* current kernel stack */
191 vm_offset_t reserved_stack; /* reserved kernel stack */
192
193 /* Thread state: */
194 int state;
195 /*
196 * Thread states [bits or'ed]
197 */
198 #define TH_WAIT 0x01 /* queued for waiting */
199 #define TH_SUSP 0x02 /* stopped or requested to stop */
200 #define TH_RUN 0x04 /* running or on runq */
201 #define TH_UNINT 0x08 /* waiting uninteruptibly */
202 #define TH_TERMINATE 0x10 /* halted at termination */
203 #define TH_TERMINATE2 0x20 /* added to termination queue */
204
205 #define TH_IDLE 0x80 /* idling processor */
206
207 /* Scheduling information */
208 sched_mode_t sched_mode; /* scheduling mode */
209 sched_mode_t saved_mode; /* saved mode during forced mode demotion */
210
211 /* This thread's contribution to global sched counters */
212 sched_bucket_t th_sched_bucket;
213
214 sfi_class_id_t sfi_class; /* SFI class (XXX Updated on CSW/QE/AST) */
215 sfi_class_id_t sfi_wait_class; /* Currently in SFI wait for this class, protected by sfi_lock */
216
217
218 uint32_t sched_flags; /* current flag bits */
219 /* TH_SFLAG_FAIRSHARE_TRIPPED (unused) 0x0001 */
220 #define TH_SFLAG_FAILSAFE 0x0002 /* fail-safe has tripped */
221 #define TH_SFLAG_THROTTLED 0x0004 /* throttled thread forced to timeshare mode (may be applied in addition to failsafe) */
222 #define TH_SFLAG_DEMOTED_MASK (TH_SFLAG_THROTTLED | TH_SFLAG_FAILSAFE) /* saved_mode contains previous sched_mode */
223
224 #define TH_SFLAG_PROMOTED 0x0008 /* sched pri has been promoted */
225 #define TH_SFLAG_ABORT 0x0010 /* abort interruptible waits */
226 #define TH_SFLAG_ABORTSAFELY 0x0020 /* ... but only those at safe point */
227 #define TH_SFLAG_ABORTED_MASK (TH_SFLAG_ABORT | TH_SFLAG_ABORTSAFELY)
228 #define TH_SFLAG_DEPRESS 0x0040 /* normal depress yield */
229 #define TH_SFLAG_POLLDEPRESS 0x0080 /* polled depress yield */
230 #define TH_SFLAG_DEPRESSED_MASK (TH_SFLAG_DEPRESS | TH_SFLAG_POLLDEPRESS)
231 /* unused TH_SFLAG_PRI_UPDATE 0x0100 */
232 #define TH_SFLAG_EAGERPREEMPT 0x0200 /* Any preemption of this thread should be treated as if AST_URGENT applied */
233 #define TH_SFLAG_RW_PROMOTED 0x0400 /* sched pri has been promoted due to blocking with RW lock held */
234 /* unused TH_SFLAG_THROTTLE_DEMOTED 0x0800 */
235 #define TH_SFLAG_WAITQ_PROMOTED 0x1000 /* sched pri promoted from waitq wakeup (generally for IPC receive) */
236 #define TH_SFLAG_PROMOTED_MASK (TH_SFLAG_PROMOTED | TH_SFLAG_RW_PROMOTED | TH_SFLAG_WAITQ_PROMOTED)
237
238 #define TH_SFLAG_RW_PROMOTED_BIT (10) /* 0x400 */
239
240 int16_t sched_pri; /* scheduled (current) priority */
241 int16_t base_pri; /* base priority */
242 int16_t max_priority; /* copy of max base priority */
243 int16_t task_priority; /* copy of task base priority */
244
245 #if defined(CONFIG_SCHED_GRRR)
246 #if 0
247 uint16_t grrr_deficit; /* fixed point (1/1000th quantum) fractional deficit */
248 #endif
249 #endif
250
251 int16_t promotions; /* level of promotion */
252 int16_t pending_promoter_index;
253 uint32_t ref_count; /* number of references to me */
254 void *pending_promoter[2];
255
256 uint32_t rwlock_count; /* Number of lck_rw_t locks held by thread */
257
258 integer_t importance; /* task-relative importance */
259 uint32_t was_promoted_on_wakeup;
260
261 /* Priority depression expiration */
262 integer_t depress_timer_active;
263 timer_call_data_t depress_timer;
264 /* real-time parameters */
265 struct { /* see mach/thread_policy.h */
266 uint32_t period;
267 uint32_t computation;
268 uint32_t constraint;
269 boolean_t preemptible;
270 uint64_t deadline;
271 } realtime;
272
273 uint64_t last_run_time; /* time when thread was switched away from */
274 uint64_t last_made_runnable_time; /* time when thread was unblocked or preempted */
275
276 #if defined(CONFIG_SCHED_MULTIQ)
277 sched_group_t sched_group;
278 #endif /* defined(CONFIG_SCHED_MULTIQ) */
279
280 /* Data used during setrun/dispatch */
281 timer_data_t system_timer; /* system mode timer */
282 processor_t bound_processor; /* bound to a processor? */
283 processor_t last_processor; /* processor last dispatched on */
284 processor_t chosen_processor; /* Where we want to run this thread */
285
286 /* Fail-safe computation since last unblock or qualifying yield */
287 uint64_t computation_metered;
288 uint64_t computation_epoch;
289 uint64_t safe_release; /* when to release fail-safe */
290
291 /* Call out from scheduler */
292 void (*sched_call)(
293 int type,
294 thread_t thread);
295 #if defined(CONFIG_SCHED_PROTO)
296 uint32_t runqueue_generation; /* last time runqueue was drained */
297 #endif
298
299 /* Statistics and timesharing calculations */
300 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
301 natural_t sched_stamp; /* last scheduler tick */
302 natural_t sched_usage; /* timesharing cpu usage [sched] */
303 natural_t pri_shift; /* usage -> priority from pset */
304 natural_t cpu_usage; /* instrumented cpu usage [%cpu] */
305 natural_t cpu_delta; /* accumulated cpu_usage delta */
306 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
307
308 uint32_t c_switch; /* total context switches */
309 uint32_t p_switch; /* total processor switches */
310 uint32_t ps_switch; /* total pset switches */
311
312 integer_t mutex_count; /* total count of locks held */
313 /* Timing data structures */
314 int precise_user_kernel_time; /* precise user/kernel enabled for this thread */
315 timer_data_t user_timer; /* user mode timer */
316 uint64_t user_timer_save; /* saved user timer value */
317 uint64_t system_timer_save; /* saved system timer value */
318 uint64_t vtimer_user_save; /* saved values for vtimers */
319 uint64_t vtimer_prof_save;
320 uint64_t vtimer_rlim_save;
321 uint64_t vtimer_qos_save;
322
323 #if CONFIG_SCHED_SFI
324 /* Timing for wait state */
325 uint64_t wait_sfi_begin_time; /* start time for thread waiting in SFI */
326 #endif
327
328 /* Timed wait expiration */
329 timer_call_data_t wait_timer;
330 integer_t wait_timer_active;
331 boolean_t wait_timer_is_set;
332
333
334 /*
335 * Processor/cache affinity
336 * - affinity_threads links task threads with the same affinity set
337 */
338 affinity_set_t affinity_set;
339 queue_chain_t affinity_threads;
340
341 /* Various bits of stashed state */
342 union {
343 struct {
344 mach_msg_return_t state; /* receive state */
345 mach_port_seqno_t seqno; /* seqno of recvd message */
346 ipc_object_t object; /* object received on */
347 mach_vm_address_t msg_addr; /* receive buffer pointer */
348 mach_msg_size_t rsize; /* max size for recvd msg */
349 mach_msg_size_t msize; /* actual size for recvd msg */
350 mach_msg_option_t option; /* options for receive */
351 mach_port_name_t receiver_name; /* the receive port name */
352 union {
353 struct ipc_kmsg *kmsg; /* received message */
354 struct ipc_mqueue *peekq; /* mqueue to peek at */
355 struct {
356 mach_msg_priority_t qos; /* received message qos */
357 mach_msg_priority_t oqos; /* override qos for message */
358 } received_qos;
359 };
360 mach_msg_continue_t continuation;
361 } receive;
362 struct {
363 struct semaphore *waitsemaphore; /* semaphore ref */
364 struct semaphore *signalsemaphore; /* semaphore ref */
365 int options; /* semaphore options */
366 kern_return_t result; /* primary result */
367 mach_msg_continue_t continuation;
368 } sema;
369 struct {
370 int option; /* switch option */
371 boolean_t reenable_workq_callback; /* on entry, callbacks were suspended */
372 } swtch;
373 int misc; /* catch-all for other state */
374 } saved;
375
376 /* Structure to save information about guard exception */
377 struct {
378 unsigned type; /* EXC_GUARD reason/type */
379 mach_exception_data_type_t code; /* Exception code */
380 mach_exception_data_type_t subcode; /* Exception sub-code */
381 } guard_exc_info;
382
383 /* Kernel holds on this thread */
384 int16_t suspend_count;
385 /* User level suspensions */
386 int16_t user_stop_count;
387
388 /* IPC data structures */
389 #if IMPORTANCE_INHERITANCE
390 natural_t ith_assertions; /* assertions pending drop */
391 #endif
392 struct ipc_kmsg_queue ith_messages; /* messages to reap */
393 mach_port_t ith_rpc_reply; /* reply port for kernel RPCs */
394
395 /* Ast/Halt data structures */
396 vm_offset_t recover; /* page fault recover(copyin/out) */
397
398 queue_chain_t threads; /* global list of all threads */
399
400 /* Activation */
401 queue_chain_t task_threads;
402
403 /* Task membership */
404 struct task *task;
405 vm_map_t map;
406
407 decl_lck_mtx_data(,mutex)
408
409
410 /* Pending thread ast(s) */
411 ast_t ast;
412
413 /* Miscellaneous bits guarded by mutex */
414 uint32_t
415 active:1, /* Thread is active and has not been terminated */
416 started:1, /* Thread has been started after creation */
417 static_param:1, /* Disallow policy parameter changes */
418 inspection:1, /* TRUE when task is being inspected by crash reporter */
419 policy_reset:1, /* Disallow policy parameter changes on terminating threads */
420 suspend_parked:1, /* thread parked in thread_suspended */
421 corpse_dup:1, /* TRUE when thread is an inactive duplicate in a corpse */
422 :0;
423
424 /* Ports associated with this thread */
425 struct ipc_port *ith_self; /* not a right, doesn't hold ref */
426 struct ipc_port *ith_sself; /* a send right */
427 struct exception_action *exc_actions;
428
429 #ifdef MACH_BSD
430 void *uthread;
431 #endif
432
433 #if CONFIG_DTRACE
434 uint32_t t_dtrace_flags; /* DTrace thread states */
435 #define TH_DTRACE_EXECSUCCESS 0x01
436 uint32_t t_dtrace_predcache;/* DTrace per thread predicate value hint */
437 int64_t t_dtrace_tracing; /* Thread time under dtrace_probe() */
438 int64_t t_dtrace_vtime;
439 #endif
440
441 clock_sec_t t_page_creation_time;
442 uint32_t t_page_creation_count;
443 uint32_t t_page_creation_throttled;
444 #if (DEVELOPMENT || DEBUG)
445 uint64_t t_page_creation_throttled_hard;
446 uint64_t t_page_creation_throttled_soft;
447 #endif /* DEVELOPMENT || DEBUG */
448
449 #ifdef KPERF
450 /* The high 7 bits are the number of frames to sample of a user callstack. */
451 #define T_KPERF_CALLSTACK_DEPTH_OFFSET (25)
452 #define T_KPERF_SET_CALLSTACK_DEPTH(DEPTH) (((uint32_t)(DEPTH)) << T_KPERF_CALLSTACK_DEPTH_OFFSET)
453 #define T_KPERF_GET_CALLSTACK_DEPTH(FLAGS) ((FLAGS) >> T_KPERF_CALLSTACK_DEPTH_OFFSET)
454 #endif
455
456 #define T_KPERF_AST_CALLSTACK (1U << 0) /* dump a callstack on thread's next AST */
457 #define T_KPERF_AST_DISPATCH (1U << 1) /* dump a name on thread's next AST */
458 #define T_KPC_ALLOC (1U << 2) /* thread needs a kpc_buf allocated */
459 /* only go up to T_KPERF_CALLSTACK_DEPTH_OFFSET - 1 */
460
461 #ifdef KPERF
462 uint32_t kperf_flags;
463 uint32_t kperf_pet_gen; /* last generation of PET that sampled this thread*/
464 uint32_t kperf_c_switch; /* last dispatch detection */
465 uint32_t kperf_pet_cnt; /* how many times a thread has been sampled by PET */
466 #endif
467
468 #ifdef KPC
469 /* accumulated performance counters for this thread */
470 uint64_t *kpc_buf;
471 #endif
472
473 #if HYPERVISOR
474 /* hypervisor virtual CPU object associated with this thread */
475 void *hv_thread_target;
476 #endif /* HYPERVISOR */
477
478 uint64_t thread_id; /*system wide unique thread-id*/
479
480 /* Statistics accumulated per-thread and aggregated per-task */
481 uint32_t syscalls_unix;
482 uint32_t syscalls_mach;
483 ledger_t t_ledger;
484 ledger_t t_threadledger; /* per thread ledger */
485 #ifdef CONFIG_BANK
486 ledger_t t_bankledger; /* ledger to charge someone */
487 uint64_t t_deduct_bank_ledger_time; /* cpu time to be deducted from bank ledger */
488 #endif
489
490 /* policy is protected by the thread mutex */
491 struct thread_requested_policy requested_policy;
492 struct thread_effective_policy effective_policy;
493
494 /* usynch override is protected by the task lock, eventually will be thread mutex */
495 struct thread_qos_override {
496 struct thread_qos_override *override_next;
497 uint32_t override_contended_resource_count;
498 int16_t override_qos;
499 int16_t override_resource_type;
500 user_addr_t override_resource;
501 } *overrides;
502
503 uint32_t ipc_overrides;
504 uint32_t user_promotions;
505 uint16_t user_promotion_basepri;
506
507 int iotier_override; /* atomic operations to set, cleared on ret to user */
508 io_stat_info_t thread_io_stats; /* per-thread I/O statistics */
509
510
511 uint32_t thread_callout_interrupt_wakeups;
512 uint32_t thread_callout_platform_idle_wakeups;
513 uint32_t thread_timer_wakeups_bin_1;
514 uint32_t thread_timer_wakeups_bin_2;
515 uint16_t thread_tag;
516 uint16_t callout_woken_from_icontext:1,
517 callout_woken_from_platform_idle:1,
518 callout_woke_thread:1,
519 thread_bitfield_unused:13;
520
521 mach_port_name_t ith_voucher_name;
522 ipc_voucher_t ith_voucher;
523 #if CONFIG_IOSCHED
524 void *decmp_upl;
525 #endif /* CONFIG_IOSCHED */
526
527 /* work interval ID (if any) associated with the thread. Uses thread mutex */
528 uint64_t work_interval_id;
529
530 /*** Machine-dependent state ***/
531 struct machine_thread machine;
532
533 #if SCHED_TRACE_THREAD_WAKEUPS
534 uintptr_t thread_wakeup_bt[64];
535 #endif
536 };
537
538 #define ith_state saved.receive.state
539 #define ith_object saved.receive.object
540 #define ith_msg_addr saved.receive.msg_addr
541 #define ith_rsize saved.receive.rsize
542 #define ith_msize saved.receive.msize
543 #define ith_option saved.receive.option
544 #define ith_receiver_name saved.receive.receiver_name
545 #define ith_continuation saved.receive.continuation
546 #define ith_kmsg saved.receive.kmsg
547 #define ith_peekq saved.receive.peekq
548 #define ith_qos saved.receive.received_qos.qos
549 #define ith_qos_override saved.receive.received_qos.oqos
550 #define ith_seqno saved.receive.seqno
551
552 #define sth_waitsemaphore saved.sema.waitsemaphore
553 #define sth_signalsemaphore saved.sema.signalsemaphore
554 #define sth_options saved.sema.options
555 #define sth_result saved.sema.result
556 #define sth_continuation saved.sema.continuation
557
558 #if MACH_ASSERT
559 #define assert_thread_magic(thread) assertf((thread)->thread_magic == THREAD_MAGIC, \
560 "bad thread magic 0x%llx for thread %p, expected 0x%llx", \
561 (thread)->thread_magic, (thread), THREAD_MAGIC)
562 #else
563 #define assert_thread_magic(thread) do { (void)(thread); } while (0)
564 #endif
565
566 extern void thread_bootstrap(void);
567
568 extern void thread_init(void);
569
570 extern void thread_daemon_init(void);
571
572 #define thread_reference_internal(thread) \
573 (void)hw_atomic_add(&(thread)->ref_count, 1)
574
575 #define thread_reference(thread) \
576 MACRO_BEGIN \
577 if ((thread) != THREAD_NULL) \
578 thread_reference_internal(thread); \
579 MACRO_END
580
581 extern void thread_deallocate(
582 thread_t thread);
583
584 extern void thread_deallocate_safe(
585 thread_t thread);
586
587 extern void thread_terminate_self(void);
588
589 extern kern_return_t thread_terminate_internal(
590 thread_t thread);
591
592 extern void thread_start(
593 thread_t thread) __attribute__ ((noinline));
594
595 extern void thread_start_in_assert_wait(
596 thread_t thread,
597 event_t event,
598 wait_interrupt_t interruptible) __attribute__ ((noinline));
599
600 extern void thread_terminate_enqueue(
601 thread_t thread);
602
603 extern void thread_exception_enqueue(
604 task_t task,
605 thread_t thread);
606
607 extern void thread_copy_resource_info(
608 thread_t dst_thread,
609 thread_t src_thread);
610
611 extern void thread_terminate_crashed_threads(void);
612
613 extern void thread_stack_enqueue(
614 thread_t thread);
615
616 extern void thread_hold(
617 thread_t thread);
618
619 extern void thread_release(
620 thread_t thread);
621
622 extern void thread_corpse_continue(void);
623
624 extern boolean_t thread_is_active(thread_t thread);
625
626 /* Locking for scheduler state, always acquired with interrupts disabled (splsched()) */
627 #if __SMP__
628 #define thread_lock_init(th) simple_lock_init(&(th)->sched_lock, 0)
629 #define thread_lock(th) simple_lock(&(th)->sched_lock)
630 #define thread_unlock(th) simple_unlock(&(th)->sched_lock)
631
632 #define wake_lock_init(th) simple_lock_init(&(th)->wake_lock, 0)
633 #define wake_lock(th) simple_lock(&(th)->wake_lock)
634 #define wake_unlock(th) simple_unlock(&(th)->wake_lock)
635 #else
636 #define thread_lock_init(th) do { (void)th; } while(0)
637 #define thread_lock(th) do { (void)th; } while(0)
638 #define thread_unlock(th) do { (void)th; } while(0)
639
640 #define wake_lock_init(th) do { (void)th; } while(0)
641 #define wake_lock(th) do { (void)th; } while(0)
642 #define wake_unlock(th) do { (void)th; } while(0)
643 #endif
644
645 #define thread_should_halt_fast(thread) (!(thread)->active)
646
647 extern void stack_alloc(
648 thread_t thread);
649
650 extern void stack_handoff(
651 thread_t from,
652 thread_t to);
653
654 extern void stack_free(
655 thread_t thread);
656
657 extern void stack_free_reserved(
658 thread_t thread);
659
660 extern boolean_t stack_alloc_try(
661 thread_t thread);
662
663 extern void stack_collect(void);
664
665 extern void stack_init(void);
666
667
668 extern kern_return_t thread_info_internal(
669 thread_t thread,
670 thread_flavor_t flavor,
671 thread_info_t thread_info_out,
672 mach_msg_type_number_t *thread_info_count);
673
674
675
676 extern kern_return_t kernel_thread_create(
677 thread_continue_t continuation,
678 void *parameter,
679 integer_t priority,
680 thread_t *new_thread);
681
682 extern kern_return_t kernel_thread_start_priority(
683 thread_continue_t continuation,
684 void *parameter,
685 integer_t priority,
686 thread_t *new_thread);
687
688 extern void machine_stack_attach(
689 thread_t thread,
690 vm_offset_t stack);
691
692 extern vm_offset_t machine_stack_detach(
693 thread_t thread);
694
695 extern void machine_stack_handoff(
696 thread_t old,
697 thread_t new);
698
699 extern thread_t machine_switch_context(
700 thread_t old_thread,
701 thread_continue_t continuation,
702 thread_t new_thread);
703
704 extern void machine_load_context(
705 thread_t thread);
706
707 extern kern_return_t machine_thread_state_initialize(
708 thread_t thread);
709
710 extern kern_return_t machine_thread_set_state(
711 thread_t thread,
712 thread_flavor_t flavor,
713 thread_state_t state,
714 mach_msg_type_number_t count);
715
716 extern kern_return_t machine_thread_get_state(
717 thread_t thread,
718 thread_flavor_t flavor,
719 thread_state_t state,
720 mach_msg_type_number_t *count);
721
722 extern kern_return_t machine_thread_dup(
723 thread_t self,
724 thread_t target);
725
726 extern void machine_thread_init(void);
727
728 extern kern_return_t machine_thread_create(
729 thread_t thread,
730 task_t task);
731 extern void machine_thread_switch_addrmode(
732 thread_t thread);
733
734 extern void machine_thread_destroy(
735 thread_t thread);
736
737 extern void machine_set_current_thread(
738 thread_t thread);
739
740 extern kern_return_t machine_thread_get_kern_state(
741 thread_t thread,
742 thread_flavor_t flavor,
743 thread_state_t tstate,
744 mach_msg_type_number_t *count);
745
746 extern kern_return_t machine_thread_inherit_taskwide(
747 thread_t thread,
748 task_t parent_task);
749
750 extern kern_return_t machine_thread_set_tsd_base(
751 thread_t thread,
752 mach_vm_offset_t tsd_base);
753
754 #define thread_mtx_lock(thread) lck_mtx_lock(&(thread)->mutex)
755 #define thread_mtx_try(thread) lck_mtx_try_lock(&(thread)->mutex)
756 #define thread_mtx_unlock(thread) lck_mtx_unlock(&(thread)->mutex)
757
758 extern void thread_apc_ast(thread_t thread);
759
760 extern void thread_update_qos_cpu_time(thread_t thread);
761
762 void act_machine_sv_free(thread_t, int);
763
764 vm_offset_t min_valid_stack_address(void);
765 vm_offset_t max_valid_stack_address(void);
766
767 static inline uint16_t thread_set_tag_internal(thread_t thread, uint16_t tag) {
768 return __sync_fetch_and_or(&thread->thread_tag, tag);
769 }
770
771 static inline uint16_t thread_get_tag_internal(thread_t thread) {
772 return thread->thread_tag;
773 }
774
775
776 extern void thread_set_options(uint32_t thopt);
777
778 #else /* MACH_KERNEL_PRIVATE */
779
780 __BEGIN_DECLS
781
782 extern thread_t current_thread(void);
783
784 extern void thread_reference(
785 thread_t thread);
786
787 extern void thread_deallocate(
788 thread_t thread);
789
790 __END_DECLS
791
792 #endif /* MACH_KERNEL_PRIVATE */
793
794 #ifdef KERNEL_PRIVATE
795
796 __BEGIN_DECLS
797
798 extern uint64_t thread_dispatchqaddr(
799 thread_t thread);
800
801 __END_DECLS
802
803 #endif /* KERNEL_PRIVATE */
804
805 #ifdef KERNEL
806 __BEGIN_DECLS
807
808 extern uint64_t thread_tid(thread_t thread);
809
810 __END_DECLS
811
812 #endif /* KERNEL */
813
814 __BEGIN_DECLS
815
816 #ifdef XNU_KERNEL_PRIVATE
817
818 /*
819 * Thread tags; for easy identification.
820 */
821 #define THREAD_TAG_MAINTHREAD 0x1
822 #define THREAD_TAG_CALLOUT 0x2
823 #define THREAD_TAG_IOWORKLOOP 0x4
824
825 #define THREAD_TAG_PTHREAD 0x10
826 #define THREAD_TAG_WORKQUEUE 0x20
827
828 uint16_t thread_set_tag(thread_t, uint16_t);
829 uint16_t thread_get_tag(thread_t);
830
831 extern kern_return_t thread_state_initialize(
832 thread_t thread);
833
834 extern kern_return_t thread_setstatus(
835 thread_t thread,
836 int flavor,
837 thread_state_t tstate,
838 mach_msg_type_number_t count);
839
840 extern kern_return_t thread_getstatus(
841 thread_t thread,
842 int flavor,
843 thread_state_t tstate,
844 mach_msg_type_number_t *count);
845
846 extern kern_return_t thread_create_with_continuation(
847 task_t task,
848 thread_t *new_thread,
849 thread_continue_t continuation);
850
851 extern kern_return_t thread_create_waiting(task_t task,
852 thread_continue_t continuation,
853 event_t event,
854 thread_t *new_thread);
855
856 extern kern_return_t thread_create_workq(
857 task_t task,
858 thread_continue_t thread_return,
859 thread_t *new_thread);
860
861 extern kern_return_t thread_create_workq_waiting(
862 task_t task,
863 thread_continue_t thread_return,
864 event_t event,
865 thread_t *new_thread);
866
867 extern void thread_yield_internal(
868 mach_msg_timeout_t interval);
869
870 extern void thread_yield_to_preemption(void);
871
872 /*
873 * Thread-private CPU limits: apply a private CPU limit to this thread only. Available actions are:
874 *
875 * 1) Block. Prevent CPU consumption of the thread from exceeding the limit.
876 * 2) Exception. Generate a resource consumption exception when the limit is exceeded.
877 * 3) Disable. Remove any existing CPU limit.
878 */
879 #define THREAD_CPULIMIT_BLOCK 0x1
880 #define THREAD_CPULIMIT_EXCEPTION 0x2
881 #define THREAD_CPULIMIT_DISABLE 0x3
882
883 struct _thread_ledger_indices {
884 int cpu_time;
885 };
886
887 extern struct _thread_ledger_indices thread_ledgers;
888
889 extern int thread_get_cpulimit(int *action, uint8_t *percentage, uint64_t *interval_ns);
890 extern int thread_set_cpulimit(int action, uint8_t percentage, uint64_t interval_ns);
891
892 extern void thread_read_times(
893 thread_t thread,
894 time_value_t *user_time,
895 time_value_t *system_time);
896
897 extern uint64_t thread_get_runtime_self(void);
898
899 extern void thread_setuserstack(
900 thread_t thread,
901 mach_vm_offset_t user_stack);
902
903 extern uint64_t thread_adjuserstack(
904 thread_t thread,
905 int adjust);
906
907 extern void thread_setentrypoint(
908 thread_t thread,
909 mach_vm_offset_t entry);
910
911 extern kern_return_t thread_set_tsd_base(
912 thread_t thread,
913 mach_vm_offset_t tsd_base);
914
915 extern kern_return_t thread_setsinglestep(
916 thread_t thread,
917 int on);
918
919 extern kern_return_t thread_userstack(
920 thread_t,
921 int,
922 thread_state_t,
923 unsigned int,
924 mach_vm_offset_t *,
925 int *,
926 boolean_t);
927
928 extern kern_return_t thread_entrypoint(
929 thread_t,
930 int,
931 thread_state_t,
932 unsigned int,
933 mach_vm_offset_t *);
934
935 extern kern_return_t thread_userstackdefault(
936 mach_vm_offset_t *,
937 boolean_t);
938
939 extern kern_return_t thread_wire_internal(
940 host_priv_t host_priv,
941 thread_t thread,
942 boolean_t wired,
943 boolean_t *prev_state);
944
945
946 extern kern_return_t thread_dup(thread_t);
947
948 extern kern_return_t thread_dup2(thread_t, thread_t);
949
950 typedef void (*sched_call_t)(
951 int type,
952 thread_t thread);
953
954 #define SCHED_CALL_BLOCK 0x1
955 #define SCHED_CALL_UNBLOCK 0x2
956
957 extern void thread_sched_call(
958 thread_t thread,
959 sched_call_t call);
960
961 extern sched_call_t thread_disable_sched_call(
962 thread_t thread,
963 sched_call_t call);
964
965 extern void thread_reenable_sched_call(
966 thread_t thread,
967 sched_call_t call);
968
969 extern void thread_static_param(
970 thread_t thread,
971 boolean_t state);
972
973 extern boolean_t thread_is_static_param(
974 thread_t thread);
975
976 extern task_t get_threadtask(thread_t);
977 #define thread_is_64bit(thd) \
978 task_has_64BitAddr(get_threadtask(thd))
979
980
981 extern void *get_bsdthread_info(thread_t);
982 extern void set_bsdthread_info(thread_t, void *);
983 extern void *uthread_alloc(task_t, thread_t, int);
984 extern void uthread_cleanup_name(void *uthread);
985 extern void uthread_cleanup(task_t, void *, void *);
986 extern void uthread_zone_free(void *);
987 extern void uthread_cred_free(void *);
988
989 #if PROC_REF_DEBUG
990 extern int uthread_get_proc_refcount(void *);
991 extern void uthread_reset_proc_refcount(void *);
992 extern int proc_ref_tracking_disabled;
993 #endif
994
995 extern boolean_t thread_should_halt(
996 thread_t thread);
997
998 extern boolean_t thread_should_abort(
999 thread_t);
1000
1001 extern int is_64signalregset(void);
1002
1003 extern void act_set_kperf(thread_t);
1004 extern void set_astledger(thread_t thread);
1005 extern void act_set_io_telemetry_ast(thread_t);
1006
1007 extern uint32_t dtrace_get_thread_predcache(thread_t);
1008 extern int64_t dtrace_get_thread_vtime(thread_t);
1009 extern int64_t dtrace_get_thread_tracing(thread_t);
1010 extern boolean_t dtrace_get_thread_reentering(thread_t);
1011 extern int dtrace_get_thread_last_cpu_id(thread_t);
1012 extern vm_offset_t dtrace_get_kernel_stack(thread_t);
1013 extern void dtrace_set_thread_predcache(thread_t, uint32_t);
1014 extern void dtrace_set_thread_vtime(thread_t, int64_t);
1015 extern void dtrace_set_thread_tracing(thread_t, int64_t);
1016 extern void dtrace_set_thread_reentering(thread_t, boolean_t);
1017 extern vm_offset_t dtrace_set_thread_recover(thread_t, vm_offset_t);
1018 extern void dtrace_thread_bootstrap(void);
1019 extern void dtrace_thread_didexec(thread_t);
1020
1021 extern int64_t dtrace_calc_thread_recent_vtime(thread_t);
1022
1023
1024 extern kern_return_t thread_set_wq_state32(
1025 thread_t thread,
1026 thread_state_t tstate);
1027
1028 extern kern_return_t thread_set_wq_state64(
1029 thread_t thread,
1030 thread_state_t tstate);
1031
1032 extern vm_offset_t kernel_stack_mask;
1033 extern vm_offset_t kernel_stack_size;
1034 extern vm_offset_t kernel_stack_depth_max;
1035
1036 void guard_ast(thread_t thread);
1037 extern void fd_guard_ast(thread_t thread);
1038 extern void mach_port_guard_ast(thread_t thread);
1039 extern void thread_guard_violation(thread_t thread, unsigned type);
1040 extern void thread_update_io_stats(thread_t thread, int size, int io_flags);
1041
1042 extern kern_return_t thread_set_voucher_name(mach_port_name_t name);
1043 extern kern_return_t thread_get_current_voucher_origin_pid(int32_t *pid);
1044
1045 extern void set_thread_rwlock_boost(void);
1046 extern void clear_thread_rwlock_boost(void);
1047
1048 /*! @function thread_has_thread_name
1049 @abstract Checks if a thread has a name.
1050 @discussion This function takes one input, a thread, and returns a boolean value indicating if that thread already has a name associated with it.
1051 @param th The thread to inspect.
1052 @result TRUE if the thread has a name, FALSE otherwise.
1053 */
1054 extern boolean_t thread_has_thread_name(thread_t th);
1055
1056 /*! @function thread_set_thread_name
1057 @abstract Set a thread's name.
1058 @discussion This function takes two input parameters: a thread to name, and the name to apply to the thread. The name will be attached to the thread in order to better identify the thread.
1059 @param th The thread to be named.
1060 @param name The name to apply to the thread.
1061 */
1062 extern void thread_set_thread_name(thread_t th, const char* name);
1063
1064 extern void thread_enable_send_importance(thread_t thread, boolean_t enable);
1065
1066 /* Get a backtrace for a threads kernel or user stack (user_p), with pc and optionally
1067 * frame pointer (getfp). Returns bytes added to buffer, and kThreadTruncatedBT in
1068 * thread_trace_flags if a user page is not present after kdp_lightweight_fault() is
1069 * called.
1070 */
1071
1072 extern int machine_trace_thread(
1073 thread_t thread,
1074 char *tracepos,
1075 char *tracebound,
1076 int nframes,
1077 boolean_t user_p,
1078 boolean_t getfp,
1079 uint32_t *thread_trace_flags);
1080
1081 extern int machine_trace_thread64(thread_t thread,
1082 char *tracepos,
1083 char *tracebound,
1084 int nframes,
1085 boolean_t user_p,
1086 boolean_t getfp,
1087 uint32_t *thread_trace_flags);
1088
1089 #endif /* XNU_KERNEL_PRIVATE */
1090
1091
1092 /*! @function kernel_thread_start
1093 @abstract Create a kernel thread.
1094 @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).
1095 @param continuation A C-function pointer where the thread will begin execution.
1096 @param parameter Caller specified data to be passed to the new thread.
1097 @param new_thread Reference to the new thread is returned in this parameter.
1098 @result Returns KERN_SUCCESS on success or an appropriate kernel code type.
1099 */
1100
1101 extern kern_return_t kernel_thread_start(
1102 thread_continue_t continuation,
1103 void *parameter,
1104 thread_t *new_thread);
1105 #ifdef KERNEL_PRIVATE
1106 void thread_set_eager_preempt(thread_t thread);
1107 void thread_clear_eager_preempt(thread_t thread);
1108 extern ipc_port_t convert_thread_to_port(thread_t);
1109 extern boolean_t is_vm_privileged(void);
1110 extern boolean_t set_vm_privilege(boolean_t);
1111 #endif /* KERNEL_PRIVATE */
1112
1113 __END_DECLS
1114
1115 #endif /* _KERN_THREAD_H_ */