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