2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
62 * This file contains the structure definitions for threads.
66 * Copyright (c) 1993 The University of Utah and
67 * the Computer Systems Laboratory (CSL). All rights reserved.
69 * Permission to use, copy, modify and distribute this software and its
70 * documentation is hereby granted, provided that both the copyright
71 * notice and this permission notice appear in all copies of the
72 * software, derivative works or modified versions, and any portions
73 * thereof, and that both notices appear in supporting documentation.
75 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
76 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
77 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
79 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
80 * improvements that they make and grant CSL redistribution rights.
84 #ifndef _KERN_THREAD_H_
85 #define _KERN_THREAD_H_
87 #include <mach/kern_return.h>
88 #include <mach/mach_types.h>
89 #include <mach/message.h>
90 #include <mach/boolean.h>
91 #include <mach/vm_param.h>
92 #include <mach/thread_info.h>
93 #include <mach/thread_status.h>
94 #include <mach/exception_types.h>
96 #include <kern/kern_types.h>
98 #include <sys/cdefs.h>
100 #ifdef MACH_KERNEL_PRIVATE
102 #include <mach_assert.h>
103 #include <mach_ldebug.h>
105 #include <ipc/ipc_types.h>
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 #include <kern/debug.h>
123 #include <kern/block_hint.h>
125 #include <kern/waitq.h>
127 #include <ipc/ipc_kmsg.h>
129 #include <machine/cpu_data.h>
130 #include <machine/thread.h>
136 #define THREAD_MAGIC 0x1234ABCDDCBA4321ULL
137 /* Ensure nothing uses &thread as a queue entry */
138 uint64_t thread_magic
;
139 #endif /* MACH_ASSERT */
142 * NOTE: The runq field in the thread structure has an unusual
143 * locking protocol. If its value is PROCESSOR_NULL, then it is
144 * locked by the thread_lock, but if its value is something else
145 * then it is locked by the associated run queue lock. It is
146 * set to PROCESSOR_NULL without holding the thread lock, but the
147 * transition from PROCESSOR_NULL to non-null must be done
148 * under the thread lock and the run queue lock.
150 * New waitq APIs allow the 'links' and 'runq' fields to be
151 * anywhere in the thread structure.
154 queue_chain_t runq_links
; /* run queue links */
155 queue_chain_t wait_links
; /* wait queue links */
158 processor_t runq
; /* run queue assignment */
160 event64_t wait_event
; /* wait queue event */
161 struct waitq
*waitq
; /* wait queue this thread is enqueued on */
163 /* Data updated during assert_wait/thread_wakeup */
165 decl_simple_lock_data(,sched_lock
) /* scheduling lock (thread_lock()) */
166 decl_simple_lock_data(,wake_lock
) /* for thread stop / wait (wake_lock()) */
168 integer_t options
; /* options set by thread itself */
169 #define TH_OPT_INTMASK 0x0003 /* interrupt / abort level */
170 #define TH_OPT_VMPRIV 0x0004 /* may allocate reserved memory */
171 #define TH_OPT_DTRACE 0x0008 /* executing under dtrace_probe */
172 #define TH_OPT_SYSTEM_CRITICAL 0x0010 /* Thread must always be allowed to run - even under heavy load */
173 #define TH_OPT_PROC_CPULIMIT 0x0020 /* Thread has a task-wide CPU limit applied to it */
174 #define TH_OPT_PRVT_CPULIMIT 0x0040 /* Thread has a thread-private CPU limit applied to it */
175 #define TH_OPT_IDLE_THREAD 0x0080 /* Thread is a per-processor idle thread */
176 #define TH_OPT_GLOBAL_FORCED_IDLE 0x0100 /* Thread performs forced idle for thermal control */
177 #define TH_OPT_SCHED_VM_GROUP 0x0200 /* Thread belongs to special scheduler VM group */
178 #define TH_OPT_HONOR_QLIMIT 0x0400 /* Thread will honor qlimit while sending mach_msg, regardless of MACH_SEND_ALWAYS */
179 #define TH_OPT_SEND_IMPORTANCE 0x0800 /* Thread will allow importance donation from kernel rpc */
181 boolean_t wake_active
; /* wake event on stop */
182 int at_safe_point
; /* thread_abort_safely allowed */
183 ast_t reason
; /* why we blocked */
184 uint32_t quantum_remaining
;
185 wait_result_t wait_result
; /* outcome of wait -
186 * may be examined by this thread
188 thread_continue_t continuation
; /* continue here next dispatch */
189 void *parameter
; /* continuation parameter */
191 /* Data updated/used in thread_invoke */
192 vm_offset_t kernel_stack
; /* current kernel stack */
193 vm_offset_t reserved_stack
; /* reserved kernel stack */
198 * Thread states [bits or'ed]
200 #define TH_WAIT 0x01 /* queued for waiting */
201 #define TH_SUSP 0x02 /* stopped or requested to stop */
202 #define TH_RUN 0x04 /* running or on runq */
203 #define TH_UNINT 0x08 /* waiting uninteruptibly */
204 #define TH_TERMINATE 0x10 /* halted at termination */
205 #define TH_TERMINATE2 0x20 /* added to termination queue */
207 #define TH_IDLE 0x80 /* idling processor */
209 /* Scheduling information */
210 sched_mode_t sched_mode
; /* scheduling mode */
211 sched_mode_t saved_mode
; /* saved mode during forced mode demotion */
213 /* This thread's contribution to global sched counters */
214 sched_bucket_t th_sched_bucket
;
216 sfi_class_id_t sfi_class
; /* SFI class (XXX Updated on CSW/QE/AST) */
217 sfi_class_id_t sfi_wait_class
; /* Currently in SFI wait for this class, protected by sfi_lock */
220 uint32_t sched_flags
; /* current flag bits */
221 /* TH_SFLAG_FAIRSHARE_TRIPPED (unused) 0x0001 */
222 #define TH_SFLAG_FAILSAFE 0x0002 /* fail-safe has tripped */
223 #define TH_SFLAG_THROTTLED 0x0004 /* throttled thread forced to timeshare mode (may be applied in addition to failsafe) */
224 #define TH_SFLAG_DEMOTED_MASK (TH_SFLAG_THROTTLED | TH_SFLAG_FAILSAFE) /* saved_mode contains previous sched_mode */
226 #define TH_SFLAG_PROMOTED 0x0008 /* sched pri has been promoted */
227 #define TH_SFLAG_ABORT 0x0010 /* abort interruptible waits */
228 #define TH_SFLAG_ABORTSAFELY 0x0020 /* ... but only those at safe point */
229 #define TH_SFLAG_ABORTED_MASK (TH_SFLAG_ABORT | TH_SFLAG_ABORTSAFELY)
230 #define TH_SFLAG_DEPRESS 0x0040 /* normal depress yield */
231 #define TH_SFLAG_POLLDEPRESS 0x0080 /* polled depress yield */
232 #define TH_SFLAG_DEPRESSED_MASK (TH_SFLAG_DEPRESS | TH_SFLAG_POLLDEPRESS)
233 /* unused TH_SFLAG_PRI_UPDATE 0x0100 */
234 #define TH_SFLAG_EAGERPREEMPT 0x0200 /* Any preemption of this thread should be treated as if AST_URGENT applied */
235 #define TH_SFLAG_RW_PROMOTED 0x0400 /* sched pri has been promoted due to blocking with RW lock held */
236 /* unused TH_SFLAG_THROTTLE_DEMOTED 0x0800 */
237 #define TH_SFLAG_WAITQ_PROMOTED 0x1000 /* sched pri promoted from waitq wakeup (generally for IPC receive) */
238 #define TH_SFLAG_PROMOTED_MASK (TH_SFLAG_PROMOTED | TH_SFLAG_RW_PROMOTED | TH_SFLAG_WAITQ_PROMOTED)
240 #define TH_SFLAG_RW_PROMOTED_BIT (10) /* 0x400 */
242 int16_t sched_pri
; /* scheduled (current) priority */
243 int16_t base_pri
; /* base priority */
244 int16_t max_priority
; /* copy of max base priority */
245 int16_t task_priority
; /* copy of task base priority */
247 #if defined(CONFIG_SCHED_GRRR)
249 uint16_t grrr_deficit
; /* fixed point (1/1000th quantum) fractional deficit */
253 int16_t promotions
; /* level of promotion */
254 int16_t pending_promoter_index
;
255 uint32_t ref_count
; /* number of references to me */
256 void *pending_promoter
[2];
258 uint32_t rwlock_count
; /* Number of lck_rw_t locks held by thread */
260 integer_t importance
; /* task-relative importance */
261 uint32_t was_promoted_on_wakeup
;
263 /* Priority depression expiration */
264 integer_t depress_timer_active
;
265 timer_call_data_t depress_timer
;
266 /* real-time parameters */
267 struct { /* see mach/thread_policy.h */
269 uint32_t computation
;
271 boolean_t preemptible
;
275 uint64_t last_run_time
; /* time when thread was switched away from */
276 uint64_t last_made_runnable_time
; /* time when thread was unblocked or preempted */
278 #if defined(CONFIG_SCHED_MULTIQ)
279 sched_group_t sched_group
;
280 #endif /* defined(CONFIG_SCHED_MULTIQ) */
282 /* Data used during setrun/dispatch */
283 timer_data_t system_timer
; /* system mode timer */
284 processor_t bound_processor
; /* bound to a processor? */
285 processor_t last_processor
; /* processor last dispatched on */
286 processor_t chosen_processor
; /* Where we want to run this thread */
288 /* Fail-safe computation since last unblock or qualifying yield */
289 uint64_t computation_metered
;
290 uint64_t computation_epoch
;
291 uint64_t safe_release
; /* when to release fail-safe */
293 /* Call out from scheduler */
297 #if defined(CONFIG_SCHED_PROTO)
298 uint32_t runqueue_generation
; /* last time runqueue was drained */
301 /* Statistics and timesharing calculations */
302 #if defined(CONFIG_SCHED_TIMESHARE_CORE)
303 natural_t sched_stamp
; /* last scheduler tick */
304 natural_t sched_usage
; /* timesharing cpu usage [sched] */
305 natural_t pri_shift
; /* usage -> priority from pset */
306 natural_t cpu_usage
; /* instrumented cpu usage [%cpu] */
307 natural_t cpu_delta
; /* accumulated cpu_usage delta */
308 #endif /* CONFIG_SCHED_TIMESHARE_CORE */
310 uint32_t c_switch
; /* total context switches */
311 uint32_t p_switch
; /* total processor switches */
312 uint32_t ps_switch
; /* total pset switches */
314 integer_t mutex_count
; /* total count of locks held */
315 /* Timing data structures */
316 int precise_user_kernel_time
; /* precise user/kernel enabled for this thread */
317 timer_data_t user_timer
; /* user mode timer */
318 uint64_t user_timer_save
; /* saved user timer value */
319 uint64_t system_timer_save
; /* saved system timer value */
320 uint64_t vtimer_user_save
; /* saved values for vtimers */
321 uint64_t vtimer_prof_save
;
322 uint64_t vtimer_rlim_save
;
323 uint64_t vtimer_qos_save
;
326 /* Timing for wait state */
327 uint64_t wait_sfi_begin_time
; /* start time for thread waiting in SFI */
330 /* Timed wait expiration */
331 timer_call_data_t wait_timer
;
332 integer_t wait_timer_active
;
333 boolean_t wait_timer_is_set
;
337 * Processor/cache affinity
338 * - affinity_threads links task threads with the same affinity set
340 affinity_set_t affinity_set
;
341 queue_chain_t affinity_threads
;
343 /* Various bits of stashed state */
346 mach_msg_return_t state
; /* receive state */
347 mach_port_seqno_t seqno
; /* seqno of recvd message */
348 ipc_object_t object
; /* object received on */
349 mach_vm_address_t msg_addr
; /* receive buffer pointer */
350 mach_msg_size_t rsize
; /* max size for recvd msg */
351 mach_msg_size_t msize
; /* actual size for recvd msg */
352 mach_msg_option_t option
; /* options for receive */
353 mach_port_name_t receiver_name
; /* the receive port name */
355 struct ipc_kmsg
*kmsg
; /* received message */
356 struct ipc_mqueue
*peekq
; /* mqueue to peek at */
358 mach_msg_priority_t qos
; /* received message qos */
359 mach_msg_priority_t oqos
; /* override qos for message */
362 mach_msg_continue_t continuation
;
365 struct semaphore
*waitsemaphore
; /* semaphore ref */
366 struct semaphore
*signalsemaphore
; /* semaphore ref */
367 int options
; /* semaphore options */
368 kern_return_t result
; /* primary result */
369 mach_msg_continue_t continuation
;
372 int option
; /* switch option */
373 boolean_t reenable_workq_callback
; /* on entry, callbacks were suspended */
375 int misc
; /* catch-all for other state */
378 /* Structure to save information about guard exception */
380 unsigned type
; /* EXC_GUARD reason/type */
381 mach_exception_data_type_t code
; /* Exception code */
382 mach_exception_data_type_t subcode
; /* Exception sub-code */
385 /* Kernel holds on this thread */
386 int16_t suspend_count
;
387 /* User level suspensions */
388 int16_t user_stop_count
;
390 /* IPC data structures */
391 #if IMPORTANCE_INHERITANCE
392 natural_t ith_assertions
; /* assertions pending drop */
394 struct ipc_kmsg_queue ith_messages
; /* messages to reap */
395 mach_port_t ith_rpc_reply
; /* reply port for kernel RPCs */
397 /* Ast/Halt data structures */
398 vm_offset_t recover
; /* page fault recover(copyin/out) */
400 queue_chain_t threads
; /* global list of all threads */
403 queue_chain_t task_threads
;
405 /* Task membership */
409 decl_lck_mtx_data(,mutex
)
412 /* Pending thread ast(s) */
415 /* Miscellaneous bits guarded by mutex */
417 active
:1, /* Thread is active and has not been terminated */
418 started
:1, /* Thread has been started after creation */
419 static_param
:1, /* Disallow policy parameter changes */
420 inspection
:1, /* TRUE when task is being inspected by crash reporter */
421 policy_reset
:1, /* Disallow policy parameter changes on terminating threads */
422 suspend_parked
:1, /* thread parked in thread_suspended */
423 corpse_dup
:1, /* TRUE when thread is an inactive duplicate in a corpse */
426 /* Ports associated with this thread */
427 struct ipc_port
*ith_self
; /* not a right, doesn't hold ref */
428 struct ipc_port
*ith_sself
; /* a send right */
429 struct exception_action
*exc_actions
;
436 uint32_t t_dtrace_flags
; /* DTrace thread states */
437 #define TH_DTRACE_EXECSUCCESS 0x01
438 uint32_t t_dtrace_predcache
;/* DTrace per thread predicate value hint */
439 int64_t t_dtrace_tracing
; /* Thread time under dtrace_probe() */
440 int64_t t_dtrace_vtime
;
443 clock_sec_t t_page_creation_time
;
444 uint32_t t_page_creation_count
;
445 uint32_t t_page_creation_throttled
;
446 #if (DEVELOPMENT || DEBUG)
447 uint64_t t_page_creation_throttled_hard
;
448 uint64_t t_page_creation_throttled_soft
;
449 #endif /* DEVELOPMENT || DEBUG */
452 /* The high 7 bits are the number of frames to sample of a user callstack. */
453 #define T_KPERF_CALLSTACK_DEPTH_OFFSET (25)
454 #define T_KPERF_SET_CALLSTACK_DEPTH(DEPTH) (((uint32_t)(DEPTH)) << T_KPERF_CALLSTACK_DEPTH_OFFSET)
455 #define T_KPERF_GET_CALLSTACK_DEPTH(FLAGS) ((FLAGS) >> T_KPERF_CALLSTACK_DEPTH_OFFSET)
458 #define T_KPERF_AST_CALLSTACK (1U << 0) /* dump a callstack on thread's next AST */
459 #define T_KPERF_AST_DISPATCH (1U << 1) /* dump a name on thread's next AST */
460 #define T_KPC_ALLOC (1U << 2) /* thread needs a kpc_buf allocated */
461 /* only go up to T_KPERF_CALLSTACK_DEPTH_OFFSET - 1 */
464 uint32_t kperf_flags
;
465 uint32_t kperf_pet_gen
; /* last generation of PET that sampled this thread*/
466 uint32_t kperf_c_switch
; /* last dispatch detection */
467 uint32_t kperf_pet_cnt
; /* how many times a thread has been sampled by PET */
471 /* accumulated performance counters for this thread */
476 /* hypervisor virtual CPU object associated with this thread */
477 void *hv_thread_target
;
478 #endif /* HYPERVISOR */
480 uint64_t thread_id
; /*system wide unique thread-id*/
482 /* Statistics accumulated per-thread and aggregated per-task */
483 uint32_t syscalls_unix
;
484 uint32_t syscalls_mach
;
486 ledger_t t_threadledger
; /* per thread ledger */
488 ledger_t t_bankledger
; /* ledger to charge someone */
489 uint64_t t_deduct_bank_ledger_time
; /* cpu time to be deducted from bank ledger */
492 /* policy is protected by the thread mutex */
493 struct thread_requested_policy requested_policy
;
494 struct thread_effective_policy effective_policy
;
496 /* usynch override is protected by the task lock, eventually will be thread mutex */
497 struct thread_qos_override
{
498 struct thread_qos_override
*override_next
;
499 uint32_t override_contended_resource_count
;
500 int16_t override_qos
;
501 int16_t override_resource_type
;
502 user_addr_t override_resource
;
505 uint32_t ipc_overrides
;
506 uint32_t user_promotions
;
507 uint16_t user_promotion_basepri
;
509 block_hint_t pending_block_hint
;
510 block_hint_t block_hint
; /* What type of primitive last caused us to block. */
512 int iotier_override
; /* atomic operations to set, cleared on ret to user */
513 io_stat_info_t thread_io_stats
; /* per-thread I/O statistics */
516 uint32_t thread_callout_interrupt_wakeups
;
517 uint32_t thread_callout_platform_idle_wakeups
;
518 uint32_t thread_timer_wakeups_bin_1
;
519 uint32_t thread_timer_wakeups_bin_2
;
521 uint16_t callout_woken_from_icontext
:1,
522 callout_woken_from_platform_idle
:1,
523 callout_woke_thread
:1,
524 thread_bitfield_unused
:13;
526 mach_port_name_t ith_voucher_name
;
527 ipc_voucher_t ith_voucher
;
530 #endif /* CONFIG_IOSCHED */
532 /* work interval ID (if any) associated with the thread. Uses thread mutex */
533 uint64_t work_interval_id
;
535 /*** Machine-dependent state ***/
536 struct machine_thread machine
;
538 #if SCHED_TRACE_THREAD_WAKEUPS
539 uintptr_t thread_wakeup_bt
[64];
543 #define ith_state saved.receive.state
544 #define ith_object saved.receive.object
545 #define ith_msg_addr saved.receive.msg_addr
546 #define ith_rsize saved.receive.rsize
547 #define ith_msize saved.receive.msize
548 #define ith_option saved.receive.option
549 #define ith_receiver_name saved.receive.receiver_name
550 #define ith_continuation saved.receive.continuation
551 #define ith_kmsg saved.receive.kmsg
552 #define ith_peekq saved.receive.peekq
553 #define ith_qos saved.receive.received_qos.qos
554 #define ith_qos_override saved.receive.received_qos.oqos
555 #define ith_seqno saved.receive.seqno
557 #define sth_waitsemaphore saved.sema.waitsemaphore
558 #define sth_signalsemaphore saved.sema.signalsemaphore
559 #define sth_options saved.sema.options
560 #define sth_result saved.sema.result
561 #define sth_continuation saved.sema.continuation
564 #define assert_thread_magic(thread) assertf((thread)->thread_magic == THREAD_MAGIC, \
565 "bad thread magic 0x%llx for thread %p, expected 0x%llx", \
566 (thread)->thread_magic, (thread), THREAD_MAGIC)
568 #define assert_thread_magic(thread) do { (void)(thread); } while (0)
571 extern void thread_bootstrap(void);
573 extern void thread_init(void);
575 extern void thread_daemon_init(void);
577 #define thread_reference_internal(thread) \
578 (void)hw_atomic_add(&(thread)->ref_count, 1)
580 #define thread_reference(thread) \
582 if ((thread) != THREAD_NULL) \
583 thread_reference_internal(thread); \
586 extern void thread_deallocate(
589 extern void thread_deallocate_safe(
592 extern void thread_inspect_deallocate(
593 thread_inspect_t thread
);
595 extern void thread_terminate_self(void);
597 extern kern_return_t
thread_terminate_internal(
600 extern void thread_start(
601 thread_t thread
) __attribute__ ((noinline
));
603 extern void thread_start_in_assert_wait(
606 wait_interrupt_t interruptible
) __attribute__ ((noinline
));
608 extern void thread_terminate_enqueue(
611 extern void thread_exception_enqueue(
615 extern void thread_copy_resource_info(
617 thread_t src_thread
);
619 extern void thread_terminate_crashed_threads(void);
621 extern void thread_stack_enqueue(
624 extern void thread_hold(
627 extern void thread_release(
630 extern void thread_corpse_continue(void);
632 extern boolean_t
thread_is_active(thread_t thread
);
634 /* Locking for scheduler state, always acquired with interrupts disabled (splsched()) */
636 #define thread_lock_init(th) simple_lock_init(&(th)->sched_lock, 0)
637 #define thread_lock(th) simple_lock(&(th)->sched_lock)
638 #define thread_unlock(th) simple_unlock(&(th)->sched_lock)
640 #define wake_lock_init(th) simple_lock_init(&(th)->wake_lock, 0)
641 #define wake_lock(th) simple_lock(&(th)->wake_lock)
642 #define wake_unlock(th) simple_unlock(&(th)->wake_lock)
644 #define thread_lock_init(th) do { (void)th; } while(0)
645 #define thread_lock(th) do { (void)th; } while(0)
646 #define thread_unlock(th) do { (void)th; } while(0)
648 #define wake_lock_init(th) do { (void)th; } while(0)
649 #define wake_lock(th) do { (void)th; } while(0)
650 #define wake_unlock(th) do { (void)th; } while(0)
653 #define thread_should_halt_fast(thread) (!(thread)->active)
655 extern void stack_alloc(
658 extern void stack_handoff(
662 extern void stack_free(
665 extern void stack_free_reserved(
668 extern boolean_t
stack_alloc_try(
671 extern void stack_collect(void);
673 extern void stack_init(void);
676 extern kern_return_t
thread_info_internal(
678 thread_flavor_t flavor
,
679 thread_info_t thread_info_out
,
680 mach_msg_type_number_t
*thread_info_count
);
684 extern kern_return_t
kernel_thread_create(
685 thread_continue_t continuation
,
688 thread_t
*new_thread
);
690 extern kern_return_t
kernel_thread_start_priority(
691 thread_continue_t continuation
,
694 thread_t
*new_thread
);
696 extern void machine_stack_attach(
700 extern vm_offset_t
machine_stack_detach(
703 extern void machine_stack_handoff(
707 extern thread_t
machine_switch_context(
709 thread_continue_t continuation
,
710 thread_t new_thread
);
712 extern void machine_load_context(
715 extern kern_return_t
machine_thread_state_initialize(
718 extern kern_return_t
machine_thread_set_state(
720 thread_flavor_t flavor
,
721 thread_state_t state
,
722 mach_msg_type_number_t count
);
724 extern kern_return_t
machine_thread_get_state(
726 thread_flavor_t flavor
,
727 thread_state_t state
,
728 mach_msg_type_number_t
*count
);
730 extern kern_return_t
machine_thread_dup(
734 extern void machine_thread_init(void);
736 extern kern_return_t
machine_thread_create(
739 extern void machine_thread_switch_addrmode(
742 extern void machine_thread_destroy(
745 extern void machine_set_current_thread(
748 extern kern_return_t
machine_thread_get_kern_state(
750 thread_flavor_t flavor
,
751 thread_state_t tstate
,
752 mach_msg_type_number_t
*count
);
754 extern kern_return_t
machine_thread_inherit_taskwide(
758 extern kern_return_t
machine_thread_set_tsd_base(
760 mach_vm_offset_t tsd_base
);
762 #define thread_mtx_lock(thread) lck_mtx_lock(&(thread)->mutex)
763 #define thread_mtx_try(thread) lck_mtx_try_lock(&(thread)->mutex)
764 #define thread_mtx_unlock(thread) lck_mtx_unlock(&(thread)->mutex)
766 extern void thread_apc_ast(thread_t thread
);
768 extern void thread_update_qos_cpu_time(thread_t thread
);
770 void act_machine_sv_free(thread_t
, int);
772 vm_offset_t
min_valid_stack_address(void);
773 vm_offset_t
max_valid_stack_address(void);
775 static inline uint16_t thread_set_tag_internal(thread_t thread
, uint16_t tag
) {
776 return __sync_fetch_and_or(&thread
->thread_tag
, tag
);
779 static inline uint16_t thread_get_tag_internal(thread_t thread
) {
780 return thread
->thread_tag
;
784 extern void thread_set_options(uint32_t thopt
);
786 #else /* MACH_KERNEL_PRIVATE */
790 extern thread_t
current_thread(void);
792 extern void thread_reference(
795 extern void thread_deallocate(
800 #endif /* MACH_KERNEL_PRIVATE */
802 #ifdef KERNEL_PRIVATE
806 extern uint64_t thread_dispatchqaddr(
811 #endif /* KERNEL_PRIVATE */
816 extern uint64_t thread_tid(thread_t thread
);
824 #ifdef XNU_KERNEL_PRIVATE
827 * Thread tags; for easy identification.
829 #define THREAD_TAG_MAINTHREAD 0x1
830 #define THREAD_TAG_CALLOUT 0x2
831 #define THREAD_TAG_IOWORKLOOP 0x4
833 #define THREAD_TAG_PTHREAD 0x10
834 #define THREAD_TAG_WORKQUEUE 0x20
836 uint16_t thread_set_tag(thread_t
, uint16_t);
837 uint16_t thread_get_tag(thread_t
);
839 extern kern_return_t
thread_state_initialize(
842 extern kern_return_t
thread_setstatus(
845 thread_state_t tstate
,
846 mach_msg_type_number_t count
);
848 extern kern_return_t
thread_getstatus(
851 thread_state_t tstate
,
852 mach_msg_type_number_t
*count
);
854 extern kern_return_t
thread_create_with_continuation(
856 thread_t
*new_thread
,
857 thread_continue_t continuation
);
859 extern kern_return_t
thread_create_waiting(task_t task
,
860 thread_continue_t continuation
,
862 thread_t
*new_thread
);
864 extern kern_return_t
thread_create_workq(
866 thread_continue_t thread_return
,
867 thread_t
*new_thread
);
869 extern kern_return_t
thread_create_workq_waiting(
871 thread_continue_t thread_return
,
873 thread_t
*new_thread
);
875 extern void thread_yield_internal(
876 mach_msg_timeout_t interval
);
878 extern void thread_yield_to_preemption(void);
881 * Thread-private CPU limits: apply a private CPU limit to this thread only. Available actions are:
883 * 1) Block. Prevent CPU consumption of the thread from exceeding the limit.
884 * 2) Exception. Generate a resource consumption exception when the limit is exceeded.
885 * 3) Disable. Remove any existing CPU limit.
887 #define THREAD_CPULIMIT_BLOCK 0x1
888 #define THREAD_CPULIMIT_EXCEPTION 0x2
889 #define THREAD_CPULIMIT_DISABLE 0x3
891 struct _thread_ledger_indices
{
895 extern struct _thread_ledger_indices thread_ledgers
;
897 extern int thread_get_cpulimit(int *action
, uint8_t *percentage
, uint64_t *interval_ns
);
898 extern int thread_set_cpulimit(int action
, uint8_t percentage
, uint64_t interval_ns
);
900 extern void thread_read_times(
902 time_value_t
*user_time
,
903 time_value_t
*system_time
);
905 extern uint64_t thread_get_runtime_self(void);
907 extern void thread_setuserstack(
909 mach_vm_offset_t user_stack
);
911 extern uint64_t thread_adjuserstack(
915 extern void thread_setentrypoint(
917 mach_vm_offset_t entry
);
919 extern kern_return_t
thread_set_tsd_base(
921 mach_vm_offset_t tsd_base
);
923 extern kern_return_t
thread_setsinglestep(
927 extern kern_return_t
thread_userstack(
936 extern kern_return_t
thread_entrypoint(
943 extern kern_return_t
thread_userstackdefault(
947 extern kern_return_t
thread_wire_internal(
948 host_priv_t host_priv
,
951 boolean_t
*prev_state
);
954 extern kern_return_t
thread_dup(thread_t
);
956 extern kern_return_t
thread_dup2(thread_t
, thread_t
);
958 typedef void (*sched_call_t
)(
962 #define SCHED_CALL_BLOCK 0x1
963 #define SCHED_CALL_UNBLOCK 0x2
965 extern void thread_sched_call(
969 extern sched_call_t
thread_disable_sched_call(
973 extern void thread_reenable_sched_call(
977 extern void thread_static_param(
981 extern boolean_t
thread_is_static_param(
984 extern task_t
get_threadtask(thread_t
);
985 #define thread_is_64bit(thd) \
986 task_has_64BitAddr(get_threadtask(thd))
989 extern void *get_bsdthread_info(thread_t
);
990 extern void set_bsdthread_info(thread_t
, void *);
991 extern void *uthread_alloc(task_t
, thread_t
, int);
992 extern void uthread_cleanup_name(void *uthread
);
993 extern void uthread_cleanup(task_t
, void *, void *);
994 extern void uthread_zone_free(void *);
995 extern void uthread_cred_free(void *);
997 extern void uthread_reset_proc_refcount(void *);
999 extern int uthread_get_proc_refcount(void *);
1000 extern int proc_ref_tracking_disabled
;
1003 extern boolean_t
thread_should_halt(
1006 extern boolean_t
thread_should_abort(
1009 extern int is_64signalregset(void);
1011 extern void act_set_kperf(thread_t
);
1012 extern void set_astledger(thread_t thread
);
1013 extern void act_set_io_telemetry_ast(thread_t
);
1015 extern uint32_t dtrace_get_thread_predcache(thread_t
);
1016 extern int64_t dtrace_get_thread_vtime(thread_t
);
1017 extern int64_t dtrace_get_thread_tracing(thread_t
);
1018 extern boolean_t
dtrace_get_thread_reentering(thread_t
);
1019 extern int dtrace_get_thread_last_cpu_id(thread_t
);
1020 extern vm_offset_t
dtrace_get_kernel_stack(thread_t
);
1021 extern void dtrace_set_thread_predcache(thread_t
, uint32_t);
1022 extern void dtrace_set_thread_vtime(thread_t
, int64_t);
1023 extern void dtrace_set_thread_tracing(thread_t
, int64_t);
1024 extern void dtrace_set_thread_reentering(thread_t
, boolean_t
);
1025 extern vm_offset_t
dtrace_set_thread_recover(thread_t
, vm_offset_t
);
1026 extern void dtrace_thread_bootstrap(void);
1027 extern void dtrace_thread_didexec(thread_t
);
1029 extern int64_t dtrace_calc_thread_recent_vtime(thread_t
);
1032 extern kern_return_t
thread_set_wq_state32(
1034 thread_state_t tstate
);
1036 extern kern_return_t
thread_set_wq_state64(
1038 thread_state_t tstate
);
1040 extern vm_offset_t kernel_stack_mask
;
1041 extern vm_offset_t kernel_stack_size
;
1042 extern vm_offset_t kernel_stack_depth_max
;
1044 void guard_ast(thread_t thread
);
1045 extern void fd_guard_ast(thread_t thread
);
1046 extern void mach_port_guard_ast(thread_t thread
);
1047 extern void thread_guard_violation(thread_t thread
, unsigned type
);
1048 extern void thread_update_io_stats(thread_t thread
, int size
, int io_flags
);
1050 extern kern_return_t
thread_set_voucher_name(mach_port_name_t name
);
1051 extern kern_return_t
thread_get_current_voucher_origin_pid(int32_t *pid
);
1053 extern void set_thread_rwlock_boost(void);
1054 extern void clear_thread_rwlock_boost(void);
1056 /*! @function thread_has_thread_name
1057 @abstract Checks if a thread has a name.
1058 @discussion This function takes one input, a thread, and returns a boolean value indicating if that thread already has a name associated with it.
1059 @param th The thread to inspect.
1060 @result TRUE if the thread has a name, FALSE otherwise.
1062 extern boolean_t
thread_has_thread_name(thread_t th
);
1064 /*! @function thread_set_thread_name
1065 @abstract Set a thread's name.
1066 @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.
1067 @param th The thread to be named.
1068 @param name The name to apply to the thread.
1070 extern void thread_set_thread_name(thread_t th
, const char* name
);
1072 extern void thread_enable_send_importance(thread_t thread
, boolean_t enable
);
1074 /* Get a backtrace for a threads kernel or user stack (user_p), with pc and optionally
1075 * frame pointer (getfp). Returns bytes added to buffer, and kThreadTruncatedBT in
1076 * thread_trace_flags if a user page is not present after kdp_lightweight_fault() is
1080 extern int machine_trace_thread(
1087 uint32_t *thread_trace_flags
);
1089 extern int machine_trace_thread64(thread_t thread
,
1095 uint32_t *thread_trace_flags
);
1097 #endif /* XNU_KERNEL_PRIVATE */
1100 /*! @function kernel_thread_start
1101 @abstract Create a kernel thread.
1102 @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).
1103 @param continuation A C-function pointer where the thread will begin execution.
1104 @param parameter Caller specified data to be passed to the new thread.
1105 @param new_thread Reference to the new thread is returned in this parameter.
1106 @result Returns KERN_SUCCESS on success or an appropriate kernel code type.
1109 extern kern_return_t
kernel_thread_start(
1110 thread_continue_t continuation
,
1112 thread_t
*new_thread
);
1113 #ifdef KERNEL_PRIVATE
1114 void thread_set_eager_preempt(thread_t thread
);
1115 void thread_clear_eager_preempt(thread_t thread
);
1116 extern ipc_port_t
convert_thread_to_port(thread_t
);
1117 extern ipc_port_t
convert_thread_inspect_to_port(thread_inspect_t
);
1118 extern boolean_t
is_vm_privileged(void);
1119 extern boolean_t
set_vm_privilege(boolean_t
);
1120 #endif /* KERNEL_PRIVATE */
1124 #endif /* _KERN_THREAD_H_ */