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