]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/thread.h
xnu-2050.9.2.tar.gz
[apple/xnu.git] / osfmk / kern / thread.h
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_FREE_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58/*
59 * File: thread.h
60 * Author: Avadis Tevanian, Jr.
61 *
62 * This file contains the structure definitions for threads.
63 *
64 */
65/*
66 * Copyright (c) 1993 The University of Utah and
67 * the Computer Systems Laboratory (CSL). All rights reserved.
68 *
69 * Permission to use, copy, modify and distribute this software and its
70 * documentation is hereby granted, provided that both the copyright
71 * notice and this permission notice appear in all copies of the
72 * software, derivative works or modified versions, and any portions
73 * thereof, and that both notices appear in supporting documentation.
74 *
75 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
76 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
77 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
78 *
79 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
80 * improvements that they make and grant CSL redistribution rights.
81 *
82 */
83
84#ifndef _KERN_THREAD_H_
85#define _KERN_THREAD_H_
86
87#include <mach/kern_return.h>
88#include <mach/mach_types.h>
89#include <mach/message.h>
90#include <mach/boolean.h>
55e303ae 91#include <mach/vm_param.h>
1c79356b
A
92#include <mach/thread_info.h>
93#include <mach/thread_status.h>
55e303ae 94#include <mach/exception_types.h>
9bccf70c 95
1c79356b
A
96#include <kern/kern_types.h>
97
91447636 98#include <sys/cdefs.h>
1c79356b 99
9bccf70c 100#ifdef MACH_KERNEL_PRIVATE
1c79356b 101
55e303ae
A
102#include <cputypes.h>
103
104#include <mach_assert.h>
1c79356b
A
105#include <mach_ldebug.h>
106
91447636
A
107#include <ipc/ipc_types.h>
108
1c79356b 109#include <mach/port.h>
1c79356b
A
110#include <kern/cpu_number.h>
111#include <kern/queue.h>
1c79356b
A
112#include <kern/timer.h>
113#include <kern/lock.h>
91447636 114#include <kern/locks.h>
1c79356b
A
115#include <kern/sched.h>
116#include <kern/sched_prim.h>
1c79356b
A
117#include <kern/thread_call.h>
118#include <kern/timer_call.h>
119#include <kern/task.h>
55e303ae 120#include <kern/exception.h>
2d21ac55 121#include <kern/affinity.h>
91447636 122
1c79356b 123#include <ipc/ipc_kmsg.h>
55e303ae 124
91447636 125#include <machine/cpu_data.h>
1c79356b
A
126#include <machine/thread.h>
127
55e303ae 128struct thread {
1c79356b 129 /*
9bccf70c 130 * NOTE: The runq field in the thread structure has an unusual
2d21ac55 131 * locking protocol. If its value is PROCESSOR_NULL, then it is
9bccf70c 132 * locked by the thread_lock, but if its value is something else
2d21ac55 133 * then it is locked by the associated run queue lock.
9bccf70c 134 *
91447636
A
135 * When the thread is on a wait queue, these first three fields
136 * are treated as an unofficial union with a wait_queue_element.
137 * If you change these, you must change that definition as well
138 * (kern/wait_queue.h).
1c79356b 139 */
9bccf70c
A
140 /* Items examined often, modified infrequently */
141 queue_chain_t links; /* run/wait queue links */
2d21ac55 142 processor_t runq; /* run queue assignment */
9bccf70c
A
143 wait_queue_t wait_queue; /* wait queue we are currently on */
144 event64_t wait_event; /* wait queue event */
91447636
A
145 integer_t options; /* options set by thread itself */
146#define TH_OPT_INTMASK 0x03 /* interrupt / abort level */
147#define TH_OPT_VMPRIV 0x04 /* may allocate reserved memory */
2d21ac55 148#define TH_OPT_DTRACE 0x08 /* executing under dtrace_probe */
6d2010ae 149#define TH_OPT_SYSTEM_CRITICAL 0x10 /* Thread must always be allowed to run - even under heavy load */
316670eb
A
150#define TH_OPT_PROC_CPULIMIT 0x20 /* Thread has a task-wide CPU limit applied to it */
151#define TH_OPT_PRVT_CPULIMIT 0x40 /* Thread has a thread-private CPU limit applied to it */
1c79356b 152
9bccf70c 153 /* Data updated during assert_wait/thread_wakeup */
55e303ae 154 decl_simple_lock_data(,sched_lock) /* scheduling lock (thread_lock()) */
2d21ac55
A
155 decl_simple_lock_data(,wake_lock) /* for thread stop / wait (wake_lock()) */
156 boolean_t wake_active; /* wake event on stop */
9bccf70c
A
157 int at_safe_point; /* thread_abort_safely allowed */
158 ast_t reason; /* why we blocked */
159 wait_result_t wait_result; /* outcome of wait -
0b4e3aa0
A
160 * may be examined by this thread
161 * WITHOUT locking */
91447636
A
162 thread_continue_t continuation; /* continue here next dispatch */
163 void *parameter; /* continuation parameter */
0b4e3aa0 164
9bccf70c
A
165 /* Data updated/used in thread_invoke */
166 struct funnel_lock *funnel_lock; /* Non-reentrancy funnel */
167 int funnel_state;
168#define TH_FN_OWNED 0x1 /* we own the funnel */
169#define TH_FN_REFUNNEL 0x2 /* re-acquire funnel on dispatch */
0b4e3aa0 170
9bccf70c 171 vm_offset_t kernel_stack; /* current kernel stack */
55e303ae 172 vm_offset_t reserved_stack; /* reserved kernel stack */
0b4e3aa0
A
173
174 /* Thread state: */
9bccf70c 175 int state;
1c79356b
A
176/*
177 * Thread states [bits or'ed]
178 */
55e303ae
A
179#define TH_WAIT 0x01 /* queued for waiting */
180#define TH_SUSP 0x02 /* stopped or requested to stop */
181#define TH_RUN 0x04 /* running or on runq */
182#define TH_UNINT 0x08 /* waiting uninteruptibly */
183#define TH_TERMINATE 0x10 /* halted at termination */
b7266188 184#define TH_TERMINATE2 0x20 /* added to termination queue */
1c79356b 185
2d21ac55 186#define TH_IDLE 0x80 /* idling processor */
1c79356b 187
9bccf70c 188 /* Scheduling information */
6d2010ae
A
189 sched_mode_t sched_mode; /* scheduling mode */
190 sched_mode_t saved_mode; /* saved mode during forced mode demotion */
191
192 unsigned int sched_flags; /* current flag bits */
193#define TH_SFLAG_FAIRSHARE_TRIPPED 0x0001 /* fairshare scheduling activated */
194#define TH_SFLAG_FAILSAFE 0x0002 /* fail-safe has tripped */
195#define TH_SFLAG_THROTTLED 0x0004 /* owner task in throttled state */
196#define TH_SFLAG_DEMOTED_MASK (TH_SFLAG_THROTTLED | TH_SFLAG_FAILSAFE | TH_SFLAG_FAIRSHARE_TRIPPED)
197
198#define TH_SFLAG_PROMOTED 0x0008 /* sched pri has been promoted */
199#define TH_SFLAG_ABORT 0x0010 /* abort interruptible waits */
200#define TH_SFLAG_ABORTSAFELY 0x0020 /* ... but only those at safe point */
201#define TH_SFLAG_ABORTED_MASK (TH_SFLAG_ABORT | TH_SFLAG_ABORTSAFELY)
202#define TH_SFLAG_DEPRESS 0x0040 /* normal depress yield */
203#define TH_SFLAG_POLLDEPRESS 0x0080 /* polled depress yield */
204#define TH_SFLAG_DEPRESSED_MASK (TH_SFLAG_DEPRESS | TH_SFLAG_POLLDEPRESS)
205#define TH_SFLAG_PRI_UPDATE 0x0100 /* Updating priority */
206#define TH_SFLAG_EAGERPREEMPT 0x0200 /* Any preemption of this thread should be treated as if AST_URGENT applied */
207
316670eb
A
208/*
209 * A thread can either be completely unthrottled, about to be throttled,
210 * throttled (TH_SFLAG_THROTTLED), or about to be unthrottled
211 */
212#define TH_SFLAG_PENDING_THROTTLE_DEMOTION 0x1000 /* Pending sched_mode demotion */
213#define TH_SFLAG_PENDING_THROTTLE_PROMOTION 0x2000 /* Pending sched_mode promition */
214#define TH_SFLAG_PENDING_THROTTLE_MASK (TH_SFLAG_PENDING_THROTTLE_DEMOTION | TH_SFLAG_PENDING_THROTTLE_PROMOTION)
9bccf70c
A
215
216 integer_t sched_pri; /* scheduled (current) priority */
217 integer_t priority; /* base priority */
218 integer_t max_priority; /* max base priority */
219 integer_t task_priority; /* copy of task base priority */
220
6d2010ae
A
221#if defined(CONFIG_SCHED_GRRR)
222#if 0
223 uint16_t grrr_deficit; /* fixed point (1/1000th quantum) fractional deficit */
224#endif
225#endif
226
9bccf70c
A
227 integer_t promotions; /* level of promotion */
228 integer_t pending_promoter_index;
229 void *pending_promoter[2];
1c79356b 230
9bccf70c 231 integer_t importance; /* task-relative importance */
1c79356b 232
55e303ae 233 /* real-time parameters */
9bccf70c 234 struct { /* see mach/thread_policy.h */
0b4e3aa0
A
235 uint32_t period;
236 uint32_t computation;
237 uint32_t constraint;
1c79356b 238 boolean_t preemptible;
55e303ae
A
239
240 uint64_t deadline;
1c79356b
A
241 } realtime;
242
6d2010ae 243 uint32_t was_promoted_on_wakeup;
0b4e3aa0 244 uint32_t current_quantum; /* duration of current quantum */
6d2010ae
A
245 uint64_t last_run_time; /* time when thread was switched away from */
246 uint64_t last_quantum_refill_time; /* time when current_quantum was refilled after expiration */
0b4e3aa0 247
9bccf70c
A
248 /* Data used during setrun/dispatch */
249 timer_data_t system_timer; /* system mode timer */
9bccf70c
A
250 processor_t bound_processor; /* bound to a processor? */
251 processor_t last_processor; /* processor last dispatched on */
6d2010ae 252 processor_t chosen_processor; /* Where we want to run this thread */
9bccf70c 253
0b4e3aa0 254 /* Fail-safe computation since last unblock or qualifying yield */
9bccf70c 255 uint64_t computation_metered;
0b4e3aa0 256 uint64_t computation_epoch;
6d2010ae 257 uint64_t safe_release; /* when to release fail-safe */
1c79356b 258
2d21ac55
A
259 /* Call out from scheduler */
260 void (*sched_call)(
261 int type,
262 thread_t thread);
6d2010ae
A
263#if defined(CONFIG_SCHED_PROTO)
264 uint32_t runqueue_generation; /* last time runqueue was drained */
265#endif
266
55e303ae 267 /* Statistics and timesharing calculations */
6d2010ae 268#if defined(CONFIG_SCHED_TRADITIONAL)
91447636
A
269 natural_t sched_stamp; /* last scheduler tick */
270 natural_t sched_usage; /* timesharing cpu usage [sched] */
271 natural_t pri_shift; /* usage -> priority from pset */
272 natural_t cpu_usage; /* instrumented cpu usage [%cpu] */
273 natural_t cpu_delta; /* accumulated cpu_usage delta */
6d2010ae 274#endif
2d21ac55
A
275 uint32_t c_switch; /* total context switches */
276 uint32_t p_switch; /* total processor switches */
277 uint32_t ps_switch; /* total pset switches */
1c79356b 278
9bccf70c 279 /* Timing data structures */
316670eb 280 int precise_user_kernel_time; /* precise user/kernel enabled for this thread */
91447636 281 timer_data_t user_timer; /* user mode timer */
91447636 282 uint64_t user_timer_save; /* saved user timer value */
2d21ac55
A
283 uint64_t system_timer_save; /* saved system timer value */
284 uint64_t vtimer_user_save; /* saved values for vtimers */
285 uint64_t vtimer_prof_save;
286 uint64_t vtimer_rlim_save;
1c79356b 287
9bccf70c 288 /* Timed wait expiration */
91447636
A
289 timer_call_data_t wait_timer;
290 integer_t wait_timer_active;
291 boolean_t wait_timer_is_set;
1c79356b 292
9bccf70c 293 /* Priority depression expiration */
91447636
A
294 timer_call_data_t depress_timer;
295 integer_t depress_timer_active;
1c79356b 296
2d21ac55
A
297 /*
298 * Processor/cache affinity
299 * - affinity_threads links task threads with the same affinity set
300 */
301 affinity_set_t affinity_set;
302 queue_chain_t affinity_threads;
303
1c79356b
A
304 /* Various bits of stashed state */
305 union {
306 struct {
0b4e3aa0
A
307 mach_msg_return_t state; /* receive state */
308 ipc_object_t object; /* object received on */
91447636 309 mach_vm_address_t msg_addr; /* receive buffer pointer */
0b4e3aa0
A
310 mach_msg_size_t msize; /* max size for recvd msg */
311 mach_msg_option_t option; /* options for receive */
312 mach_msg_size_t slist_size; /* scatter list size */
b0d623f7 313 mach_port_name_t receiver_name; /* the receive port name */
0b4e3aa0
A
314 struct ipc_kmsg *kmsg; /* received message */
315 mach_port_seqno_t seqno; /* seqno of recvd message */
9bccf70c
A
316 mach_msg_continue_t continuation;
317 } receive;
1c79356b 318 struct {
0b4e3aa0
A
319 struct semaphore *waitsemaphore; /* semaphore ref */
320 struct semaphore *signalsemaphore; /* semaphore ref */
321 int options; /* semaphore options */
322 kern_return_t result; /* primary result */
9bccf70c
A
323 mach_msg_continue_t continuation;
324 } sema;
1c79356b 325 struct {
9bccf70c
A
326 int option; /* switch option */
327 } swtch;
328 int misc; /* catch-all for other state */
329 } saved;
1c79356b 330
9bccf70c
A
331 /* IPC data structures */
332 struct ipc_kmsg_queue ith_messages;
9bccf70c 333 mach_port_t ith_rpc_reply; /* reply port for kernel RPCs */
1c79356b
A
334
335 /* Ast/Halt data structures */
b0d623f7 336 vm_offset_t recover; /* page fault recover(copyin/out) */
2d21ac55 337 uint32_t ref_count; /* number of references to me */
1c79356b 338
b0d623f7 339 queue_chain_t threads; /* global list of all threads */
1c79356b 340
55e303ae
A
341 /* Activation */
342 queue_chain_t task_threads;
343
344 /*** Machine-dependent state ***/
91447636 345 struct machine_thread machine;
55e303ae
A
346
347 /* Task membership */
348 struct task *task;
349 vm_map_t map;
350
b0d623f7 351 decl_lck_mtx_data(,mutex)
55e303ae
A
352
353 /* Kernel holds on this thread */
354 int suspend_count;
355
356 /* User level suspensions */
357 int user_stop_count;
358
359 /* Pending thread ast(s) */
360 ast_t ast;
361
91447636 362 /* Miscellaneous bits guarded by mutex */
55e303ae 363 uint32_t
2d21ac55
A
364 active:1, /* Thread is active and has not been terminated */
365 started:1, /* Thread has been started after creation */
366 static_param:1, /* Disallow policy parameter changes */
367 :0;
55e303ae
A
368
369 /* Return Handers */
370 struct ReturnHandler {
371 struct ReturnHandler *next;
372 void (*handler)(
373 struct ReturnHandler *rh,
91447636 374 struct thread *thread);
55e303ae
A
375 } *handlers, special_handler;
376
377 /* Ports associated with this thread */
378 struct ipc_port *ith_self; /* not a right, doesn't hold ref */
379 struct ipc_port *ith_sself; /* a send right */
380 struct exception_action exc_actions[EXC_TYPES_COUNT];
381
382 /* Owned ulocks (a lock set element) */
383 queue_head_t held_ulocks;
384
55e303ae
A
385#ifdef MACH_BSD
386 void *uthread;
387#endif
2d21ac55
A
388
389#if CONFIG_DTRACE
390 uint32_t t_dtrace_predcache;/* DTrace per thread predicate value hint */
391 int64_t t_dtrace_tracing; /* Thread time under dtrace_probe() */
392 int64_t t_dtrace_vtime;
393#endif
b0d623f7 394
b0d623f7
A
395 uint32_t t_page_creation_count;
396 clock_sec_t t_page_creation_time;
397
316670eb
A
398#define T_CHUD_MARKED 0x01 /* this thread is marked by CHUD */
399#define T_IN_CHUD 0x02 /* this thread is already in a CHUD handler */
400#define THREAD_PMC_FLAG 0x04 /* Bit in "t_chud" signifying PMC interest */
401#define T_AST_CALLSTACK 0x08 /* Thread scheduled to dump a
402 * callstack on its next
403 * AST */
404#define T_AST_NAME 0x10 /* Thread scheduled to dump
405 * its name on its next
406 * AST */
407#define T_NAME_DONE 0x20 /* Thread has previously
408 * recorded its name */
409
2d21ac55 410 uint32_t t_chud; /* CHUD flags, used for Shark */
316670eb 411 uint32_t chud_c_switch; /* last dispatch detection */
6d2010ae
A
412
413 integer_t mutex_count; /* total count of locks held */
414
b0d623f7 415 uint64_t thread_id; /*system wide unique thread-id*/
6d2010ae
A
416
417 /* Statistics accumulated per-thread and aggregated per-task */
418 uint32_t syscalls_unix;
419 uint32_t syscalls_mach;
316670eb
A
420 ledger_t t_ledger;
421 ledger_t t_threadledger; /* per thread ledger */
422 struct process_policy ext_appliedstate; /* externally applied actions */
6d2010ae 423 struct process_policy ext_policystate; /* externally defined process policy states*/
316670eb 424 struct process_policy appliedstate; /* self applied acions */
6d2010ae 425 struct process_policy policystate; /* process wide policy states */
316670eb
A
426#if CONFIG_EMBEDDED
427 task_watch_t * taskwatch; /* task watch */
428 integer_t saved_importance; /* saved task-relative importance */
429#endif /* CONFIG_EMBEDDED */
0b4e3aa0 430};
1c79356b 431
1c79356b
A
432#define ith_state saved.receive.state
433#define ith_object saved.receive.object
91447636 434#define ith_msg_addr saved.receive.msg_addr
1c79356b
A
435#define ith_msize saved.receive.msize
436#define ith_option saved.receive.option
437#define ith_scatter_list_size saved.receive.slist_size
b0d623f7 438#define ith_receiver_name saved.receive.receiver_name
1c79356b
A
439#define ith_continuation saved.receive.continuation
440#define ith_kmsg saved.receive.kmsg
441#define ith_seqno saved.receive.seqno
442
443#define sth_waitsemaphore saved.sema.waitsemaphore
444#define sth_signalsemaphore saved.sema.signalsemaphore
445#define sth_options saved.sema.options
446#define sth_result saved.sema.result
447#define sth_continuation saved.sema.continuation
448
2d21ac55 449extern void thread_bootstrap(void) __attribute__((section("__TEXT, initcode")));
1c79356b 450
2d21ac55 451extern void thread_init(void) __attribute__((section("__TEXT, initcode")));
9bccf70c 452
91447636 453extern void thread_daemon_init(void);
1c79356b 454
91447636 455#define thread_reference_internal(thread) \
2d21ac55 456 (void)hw_atomic_add(&(thread)->ref_count, 1)
91447636
A
457
458#define thread_deallocate_internal(thread) \
459 hw_atomic_sub(&(thread)->ref_count, 1)
460
461#define thread_reference(thread) \
462MACRO_BEGIN \
463 if ((thread) != THREAD_NULL) \
2d21ac55 464 thread_reference_internal(thread); \
91447636 465MACRO_END
1c79356b 466
55e303ae
A
467extern void thread_deallocate(
468 thread_t thread);
1c79356b 469
55e303ae 470extern void thread_terminate_self(void);
1c79356b 471
91447636
A
472extern kern_return_t thread_terminate_internal(
473 thread_t thread);
474
2d21ac55
A
475extern void thread_start_internal(
476 thread_t thread) __attribute__ ((noinline));
477
91447636
A
478extern void thread_terminate_enqueue(
479 thread_t thread);
480
481extern void thread_stack_enqueue(
482 thread_t thread);
483
55e303ae 484extern void thread_hold(
91447636 485 thread_t thread);
1c79356b 486
55e303ae 487extern void thread_release(
91447636 488 thread_t thread);
1c79356b 489
b0d623f7 490
91447636 491#define thread_lock_init(th) simple_lock_init(&(th)->sched_lock, 0)
55e303ae
A
492#define thread_lock(th) simple_lock(&(th)->sched_lock)
493#define thread_unlock(th) simple_unlock(&(th)->sched_lock)
1c79356b 494
91447636
A
495#define wake_lock_init(th) simple_lock_init(&(th)->wake_lock, 0)
496#define wake_lock(th) simple_lock(&(th)->wake_lock)
497#define wake_unlock(th) simple_unlock(&(th)->wake_lock)
2d21ac55
A
498
499#define thread_should_halt_fast(thread) (!(thread)->active)
1c79356b 500
91447636
A
501extern void stack_alloc(
502 thread_t thread);
1c79356b 503
6d2010ae
A
504extern void stack_handoff(
505 thread_t from,
506 thread_t to);
507
55e303ae
A
508extern void stack_free(
509 thread_t thread);
1c79356b 510
6d2010ae
A
511extern void stack_free_reserved(
512 thread_t thread);
1c79356b 513
91447636
A
514extern boolean_t stack_alloc_try(
515 thread_t thread);
516
55e303ae 517extern void stack_collect(void);
1c79356b 518
2d21ac55 519extern void stack_init(void) __attribute__((section("__TEXT, initcode")));
91447636 520
6d2010ae 521
91447636
A
522extern kern_return_t thread_info_internal(
523 thread_t thread,
1c79356b
A
524 thread_flavor_t flavor,
525 thread_info_t thread_info_out,
526 mach_msg_type_number_t *thread_info_count);
527
55e303ae
A
528extern void thread_task_priority(
529 thread_t thread,
530 integer_t priority,
531 integer_t max_priority);
1c79356b 532
91447636
A
533extern void thread_policy_reset(
534 thread_t thread);
1c79356b 535
91447636
A
536extern kern_return_t kernel_thread_create(
537 thread_continue_t continuation,
538 void *parameter,
539 integer_t priority,
540 thread_t *new_thread);
1c79356b 541
91447636
A
542extern kern_return_t kernel_thread_start_priority(
543 thread_continue_t continuation,
544 void *parameter,
545 integer_t priority,
546 thread_t *new_thread);
1c79356b 547
55e303ae
A
548extern void machine_stack_attach(
549 thread_t thread,
91447636 550 vm_offset_t stack);
1c79356b 551
55e303ae
A
552extern vm_offset_t machine_stack_detach(
553 thread_t thread);
554
555extern void machine_stack_handoff(
556 thread_t old,
557 thread_t new);
558
559extern thread_t machine_switch_context(
560 thread_t old_thread,
561 thread_continue_t continuation,
562 thread_t new_thread);
563
564extern void machine_load_context(
565 thread_t thread);
566
91447636
A
567extern kern_return_t machine_thread_state_initialize(
568 thread_t thread);
9bccf70c 569
55e303ae 570extern kern_return_t machine_thread_set_state(
91447636 571 thread_t thread,
55e303ae
A
572 thread_flavor_t flavor,
573 thread_state_t state,
574 mach_msg_type_number_t count);
575
576extern kern_return_t machine_thread_get_state(
91447636 577 thread_t thread,
55e303ae
A
578 thread_flavor_t flavor,
579 thread_state_t state,
580 mach_msg_type_number_t *count);
581
582extern kern_return_t machine_thread_dup(
91447636
A
583 thread_t self,
584 thread_t target);
55e303ae
A
585
586extern void machine_thread_init(void);
587
588extern kern_return_t machine_thread_create(
589 thread_t thread,
590 task_t task);
0c530ab8 591extern void machine_thread_switch_addrmode(
2d21ac55 592 thread_t thread);
55e303ae
A
593
594extern void machine_thread_destroy(
595 thread_t thread);
596
91447636
A
597extern void machine_set_current_thread(
598 thread_t thread);
55e303ae 599
91447636
A
600extern kern_return_t machine_thread_get_kern_state(
601 thread_t thread,
602 thread_flavor_t flavor,
603 thread_state_t tstate,
604 mach_msg_type_number_t *count);
605
b0d623f7
A
606extern kern_return_t machine_thread_inherit_taskwide(
607 thread_t thread,
608 task_t parent_task);
91447636 609
55e303ae
A
610/*
611 * XXX Funnel locks XXX
612 */
613
614struct funnel_lock {
615 int fnl_type; /* funnel type */
91447636 616 lck_mtx_t *fnl_mutex; /* underlying mutex for the funnel */
55e303ae
A
617 void * fnl_mtxholder; /* thread (last)holdng mutex */
618 void * fnl_mtxrelease; /* thread (last)releasing mutex */
91447636 619 lck_mtx_t *fnl_oldmutex; /* Mutex before collapsing split funnel */
55e303ae
A
620};
621
55e303ae
A
622typedef struct ReturnHandler ReturnHandler;
623
b0d623f7
A
624#define thread_mtx_lock(thread) lck_mtx_lock(&(thread)->mutex)
625#define thread_mtx_try(thread) lck_mtx_try_lock(&(thread)->mutex)
626#define thread_mtx_unlock(thread) lck_mtx_unlock(&(thread)->mutex)
55e303ae
A
627
628extern void act_execute_returnhandlers(void);
629
630extern void install_special_handler(
91447636 631 thread_t thread);
55e303ae
A
632
633extern void special_handler(
634 ReturnHandler *rh,
91447636 635 thread_t thread);
1c79356b 636
2d21ac55
A
637void act_machine_sv_free(thread_t, int);
638
b0d623f7
A
639vm_offset_t min_valid_stack_address(void);
640vm_offset_t max_valid_stack_address(void);
641
642extern void funnel_lock(
643 struct funnel_lock *lock);
644
645extern void funnel_unlock(
646 struct funnel_lock *lock);
647
9bccf70c 648#else /* MACH_KERNEL_PRIVATE */
1c79356b 649
91447636 650__BEGIN_DECLS
1c79356b 651
91447636
A
652extern thread_t current_thread(void);
653
654extern void thread_reference(
655 thread_t thread);
55e303ae 656
91447636
A
657extern void thread_deallocate(
658 thread_t thread);
55e303ae 659
91447636 660__END_DECLS
1c79356b 661
9bccf70c 662#endif /* MACH_KERNEL_PRIVATE */
1c79356b 663
91447636 664#ifdef KERNEL_PRIVATE
1c79356b 665
91447636 666__BEGIN_DECLS
55e303ae 667
316670eb 668#if defined(__i386__)
55e303ae 669
91447636
A
670extern thread_t kernel_thread(
671 task_t task,
672 void (*start)(void));
55e303ae 673
316670eb 674#endif /* defined(__i386__) */
b0d623f7
A
675
676extern uint64_t thread_tid(
677 thread_t thread);
678
679extern uint64_t thread_dispatchqaddr(
680 thread_t thread);
681
91447636 682__END_DECLS
55e303ae 683
91447636 684#endif /* KERNEL_PRIVATE */
55e303ae 685
91447636 686__BEGIN_DECLS
55e303ae 687
91447636 688#ifdef XNU_KERNEL_PRIVATE
9bccf70c 689
316670eb
A
690extern kern_return_t thread_state_initialize(
691 thread_t thread);
692
693extern kern_return_t thread_setstatus(
694 thread_t thread,
695 int flavor,
696 thread_state_t tstate,
697 mach_msg_type_number_t count);
698
699extern kern_return_t thread_getstatus(
700 thread_t thread,
701 int flavor,
702 thread_state_t tstate,
703 mach_msg_type_number_t *count);
704
b0d623f7
A
705extern kern_return_t thread_create_workq(
706 task_t task,
b7266188 707 thread_continue_t thread_return,
b0d623f7
A
708 thread_t *new_thread);
709
2d21ac55
A
710extern void thread_yield_internal(
711 mach_msg_timeout_t interval);
712
316670eb
A
713/*
714 * Thread-private CPU limits: apply a private CPU limit to this thread only. Available actions are:
715 *
716 * 1) Block. Prevent CPU consumption of the thread from exceeding the limit.
717 * 2) Exception. Generate a resource consumption exception when the limit is exceeded.
718 */
719#define THREAD_CPULIMIT_BLOCK 0x1
720#define THREAD_CPULIMIT_EXCEPTION 0x2
721
722struct _thread_ledger_indices {
723 int cpu_time;
724};
725
726extern struct _thread_ledger_indices thread_ledgers;
727
728extern int thread_set_cpulimit(int action, uint8_t percentage, uint64_t interval_ns);
729
b0d623f7 730typedef struct funnel_lock funnel_t;
55e303ae 731
9bccf70c 732#define THR_FUNNEL_NULL (funnel_t *)0
1c79356b 733
55e303ae
A
734extern funnel_t *funnel_alloc(
735 int type);
1c79356b 736
91447636
A
737extern void funnel_free(
738 funnel_t *lock);
1c79356b 739
b0d623f7
A
740extern funnel_t *thread_funnel_get(void);
741
742extern boolean_t thread_funnel_set(
743 funnel_t *lock,
744 boolean_t funneled);
745
91447636
A
746extern void thread_read_times(
747 thread_t thread,
748 time_value_t *user_time,
749 time_value_t *system_time);
1c79356b 750
91447636
A
751extern void thread_setuserstack(
752 thread_t thread,
753 mach_vm_offset_t user_stack);
1c79356b 754
91447636
A
755extern uint64_t thread_adjuserstack(
756 thread_t thread,
757 int adjust);
55e303ae 758
91447636 759extern void thread_setentrypoint(
55e303ae 760 thread_t thread,
91447636
A
761 mach_vm_offset_t entry);
762
2d21ac55 763extern kern_return_t thread_setsinglestep(
0c530ab8
A
764 thread_t thread,
765 int on);
766
6d2010ae
A
767extern kern_return_t thread_userstack(
768 thread_t,
769 int,
770 thread_state_t,
771 unsigned int,
772 mach_vm_offset_t *,
773 int *);
774
316670eb
A
775extern kern_return_t thread_entrypoint(
776 thread_t,
777 int,
778 thread_state_t,
779 unsigned int,
780 mach_vm_offset_t *);
6d2010ae 781
316670eb
A
782extern kern_return_t thread_userstackdefault(
783 thread_t,
784 mach_vm_offset_t *);
6d2010ae 785
91447636
A
786extern kern_return_t thread_wire_internal(
787 host_priv_t host_priv,
788 thread_t thread,
789 boolean_t wired,
790 boolean_t *prev_state);
791
91447636
A
792extern kern_return_t thread_dup(thread_t);
793
2d21ac55
A
794typedef void (*sched_call_t)(
795 int type,
796 thread_t thread);
797
798#define SCHED_CALL_BLOCK 0x1
799#define SCHED_CALL_UNBLOCK 0x2
800
801extern void thread_sched_call(
802 thread_t thread,
803 sched_call_t call);
804
805extern void thread_static_param(
806 thread_t thread,
807 boolean_t state);
808
b0d623f7
A
809extern kern_return_t thread_policy_set_internal(
810 thread_t thread,
811 thread_policy_flavor_t flavor,
812 thread_policy_t policy_info,
813 mach_msg_type_number_t count);
814
815
91447636 816extern task_t get_threadtask(thread_t);
0c530ab8
A
817#define thread_is_64bit(thd) \
818 task_has_64BitAddr(get_threadtask(thd))
819
91447636
A
820
821extern void *get_bsdthread_info(thread_t);
822extern void set_bsdthread_info(thread_t, void *);
b0d623f7 823extern void *uthread_alloc(task_t, thread_t, int);
2d21ac55
A
824extern void uthread_cleanup(task_t, void *, void *);
825extern void uthread_zone_free(void *);
826extern void uthread_cred_free(void *);
91447636
A
827
828extern boolean_t thread_should_halt(
829 thread_t thread);
830
316670eb
A
831extern boolean_t thread_should_abort(
832 thread_t);
833
2d21ac55
A
834extern int is_64signalregset(void);
835
836void act_set_apc(thread_t);
316670eb 837void act_set_kperf(thread_t);
2d21ac55
A
838
839extern uint32_t dtrace_get_thread_predcache(thread_t);
840extern int64_t dtrace_get_thread_vtime(thread_t);
841extern int64_t dtrace_get_thread_tracing(thread_t);
842extern boolean_t dtrace_get_thread_reentering(thread_t);
843extern vm_offset_t dtrace_get_kernel_stack(thread_t);
844extern void dtrace_set_thread_predcache(thread_t, uint32_t);
845extern void dtrace_set_thread_vtime(thread_t, int64_t);
846extern void dtrace_set_thread_tracing(thread_t, int64_t);
847extern void dtrace_set_thread_reentering(thread_t, boolean_t);
848extern vm_offset_t dtrace_set_thread_recover(thread_t, vm_offset_t);
b0d623f7 849extern void dtrace_thread_bootstrap(void);
2d21ac55
A
850
851extern int64_t dtrace_calc_thread_recent_vtime(thread_t);
852
853
854extern void thread_set_wq_state32(
855 thread_t thread,
856 thread_state_t tstate);
857
858extern void thread_set_wq_state64(
859 thread_t thread,
860 thread_state_t tstate);
861
b0d623f7
A
862extern vm_offset_t kernel_stack_mask;
863extern vm_offset_t kernel_stack_size;
864extern vm_offset_t kernel_stack_depth_max;
865
91447636
A
866#endif /* XNU_KERNEL_PRIVATE */
867
b0d623f7
A
868/*! @function kernel_thread_start
869 @abstract Create a kernel thread.
870 @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).
871 @param continuation A C-function pointer where the thread will begin execution.
872 @param parameter Caller specified data to be passed to the new thread.
873 @param new_thread Reference to the new thread is returned in this parameter.
874 @result Returns KERN_SUCCESS on success or an appropriate kernel code type.
875*/
876
91447636
A
877extern kern_return_t kernel_thread_start(
878 thread_continue_t continuation,
879 void *parameter,
880 thread_t *new_thread);
6d2010ae
A
881#ifdef KERNEL_PRIVATE
882void thread_set_eager_preempt(thread_t thread);
883void thread_clear_eager_preempt(thread_t thread);
316670eb 884extern ipc_port_t convert_thread_to_port(thread_t);
6d2010ae 885#endif /* KERNEL_PRIVATE */
55e303ae 886
91447636 887__END_DECLS
55e303ae 888
1c79356b 889#endif /* _KERN_THREAD_H_ */