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