]>
Commit | Line | Data |
---|---|---|
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 | 128 | struct 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 */ |
1c79356b | 150 | |
9bccf70c | 151 | /* Data updated during assert_wait/thread_wakeup */ |
55e303ae | 152 | decl_simple_lock_data(,sched_lock) /* scheduling lock (thread_lock()) */ |
2d21ac55 A |
153 | decl_simple_lock_data(,wake_lock) /* for thread stop / wait (wake_lock()) */ |
154 | boolean_t wake_active; /* wake event on stop */ | |
9bccf70c A |
155 | int at_safe_point; /* thread_abort_safely allowed */ |
156 | ast_t reason; /* why we blocked */ | |
157 | wait_result_t wait_result; /* outcome of wait - | |
0b4e3aa0 A |
158 | * may be examined by this thread |
159 | * WITHOUT locking */ | |
91447636 A |
160 | thread_continue_t continuation; /* continue here next dispatch */ |
161 | void *parameter; /* continuation parameter */ | |
0b4e3aa0 | 162 | |
9bccf70c A |
163 | /* Data updated/used in thread_invoke */ |
164 | struct funnel_lock *funnel_lock; /* Non-reentrancy funnel */ | |
165 | int funnel_state; | |
166 | #define TH_FN_OWNED 0x1 /* we own the funnel */ | |
167 | #define TH_FN_REFUNNEL 0x2 /* re-acquire funnel on dispatch */ | |
0b4e3aa0 | 168 | |
9bccf70c | 169 | vm_offset_t kernel_stack; /* current kernel stack */ |
55e303ae | 170 | vm_offset_t reserved_stack; /* reserved kernel stack */ |
0b4e3aa0 A |
171 | |
172 | /* Thread state: */ | |
9bccf70c | 173 | int state; |
1c79356b A |
174 | /* |
175 | * Thread states [bits or'ed] | |
176 | */ | |
55e303ae A |
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 */ | |
b7266188 | 182 | #define TH_TERMINATE2 0x20 /* added to termination queue */ |
1c79356b | 183 | |
2d21ac55 | 184 | #define TH_IDLE 0x80 /* idling processor */ |
1c79356b | 185 | |
9bccf70c | 186 | /* Scheduling information */ |
6d2010ae A |
187 | sched_mode_t sched_mode; /* scheduling mode */ |
188 | sched_mode_t saved_mode; /* saved mode during forced mode demotion */ | |
189 | ||
190 | unsigned int sched_flags; /* current flag bits */ | |
191 | #define TH_SFLAG_FAIRSHARE_TRIPPED 0x0001 /* fairshare scheduling activated */ | |
192 | #define TH_SFLAG_FAILSAFE 0x0002 /* fail-safe has tripped */ | |
193 | #define TH_SFLAG_THROTTLED 0x0004 /* owner task in throttled state */ | |
194 | #define TH_SFLAG_DEMOTED_MASK (TH_SFLAG_THROTTLED | TH_SFLAG_FAILSAFE | TH_SFLAG_FAIRSHARE_TRIPPED) | |
195 | ||
196 | #define TH_SFLAG_PROMOTED 0x0008 /* sched pri has been promoted */ | |
197 | #define TH_SFLAG_ABORT 0x0010 /* abort interruptible waits */ | |
198 | #define TH_SFLAG_ABORTSAFELY 0x0020 /* ... but only those at safe point */ | |
199 | #define TH_SFLAG_ABORTED_MASK (TH_SFLAG_ABORT | TH_SFLAG_ABORTSAFELY) | |
200 | #define TH_SFLAG_DEPRESS 0x0040 /* normal depress yield */ | |
201 | #define TH_SFLAG_POLLDEPRESS 0x0080 /* polled depress yield */ | |
202 | #define TH_SFLAG_DEPRESSED_MASK (TH_SFLAG_DEPRESS | TH_SFLAG_POLLDEPRESS) | |
203 | #define TH_SFLAG_PRI_UPDATE 0x0100 /* Updating priority */ | |
204 | #define TH_SFLAG_EAGERPREEMPT 0x0200 /* Any preemption of this thread should be treated as if AST_URGENT applied */ | |
205 | ||
9bccf70c A |
206 | |
207 | integer_t sched_pri; /* scheduled (current) priority */ | |
208 | integer_t priority; /* base priority */ | |
209 | integer_t max_priority; /* max base priority */ | |
210 | integer_t task_priority; /* copy of task base priority */ | |
211 | ||
6d2010ae A |
212 | #if defined(CONFIG_SCHED_GRRR) |
213 | #if 0 | |
214 | uint16_t grrr_deficit; /* fixed point (1/1000th quantum) fractional deficit */ | |
215 | #endif | |
216 | #endif | |
217 | ||
9bccf70c A |
218 | integer_t promotions; /* level of promotion */ |
219 | integer_t pending_promoter_index; | |
220 | void *pending_promoter[2]; | |
1c79356b | 221 | |
9bccf70c | 222 | integer_t importance; /* task-relative importance */ |
1c79356b | 223 | |
55e303ae | 224 | /* real-time parameters */ |
9bccf70c | 225 | struct { /* see mach/thread_policy.h */ |
0b4e3aa0 A |
226 | uint32_t period; |
227 | uint32_t computation; | |
228 | uint32_t constraint; | |
1c79356b | 229 | boolean_t preemptible; |
55e303ae A |
230 | |
231 | uint64_t deadline; | |
1c79356b A |
232 | } realtime; |
233 | ||
6d2010ae | 234 | uint32_t was_promoted_on_wakeup; |
0b4e3aa0 | 235 | uint32_t current_quantum; /* duration of current quantum */ |
6d2010ae A |
236 | uint64_t last_run_time; /* time when thread was switched away from */ |
237 | uint64_t last_quantum_refill_time; /* time when current_quantum was refilled after expiration */ | |
0b4e3aa0 | 238 | |
9bccf70c A |
239 | /* Data used during setrun/dispatch */ |
240 | timer_data_t system_timer; /* system mode timer */ | |
9bccf70c A |
241 | processor_t bound_processor; /* bound to a processor? */ |
242 | processor_t last_processor; /* processor last dispatched on */ | |
6d2010ae | 243 | processor_t chosen_processor; /* Where we want to run this thread */ |
9bccf70c | 244 | |
0b4e3aa0 | 245 | /* Fail-safe computation since last unblock or qualifying yield */ |
9bccf70c | 246 | uint64_t computation_metered; |
0b4e3aa0 | 247 | uint64_t computation_epoch; |
6d2010ae | 248 | uint64_t safe_release; /* when to release fail-safe */ |
1c79356b | 249 | |
2d21ac55 A |
250 | /* Call out from scheduler */ |
251 | void (*sched_call)( | |
252 | int type, | |
253 | thread_t thread); | |
6d2010ae A |
254 | #if defined(CONFIG_SCHED_PROTO) |
255 | uint32_t runqueue_generation; /* last time runqueue was drained */ | |
256 | #endif | |
257 | ||
55e303ae | 258 | /* Statistics and timesharing calculations */ |
6d2010ae | 259 | #if defined(CONFIG_SCHED_TRADITIONAL) |
91447636 A |
260 | natural_t sched_stamp; /* last scheduler tick */ |
261 | natural_t sched_usage; /* timesharing cpu usage [sched] */ | |
262 | natural_t pri_shift; /* usage -> priority from pset */ | |
263 | natural_t cpu_usage; /* instrumented cpu usage [%cpu] */ | |
264 | natural_t cpu_delta; /* accumulated cpu_usage delta */ | |
6d2010ae | 265 | #endif |
2d21ac55 A |
266 | uint32_t c_switch; /* total context switches */ |
267 | uint32_t p_switch; /* total processor switches */ | |
268 | uint32_t ps_switch; /* total pset switches */ | |
1c79356b | 269 | |
9bccf70c | 270 | /* Timing data structures */ |
91447636 | 271 | timer_data_t user_timer; /* user mode timer */ |
91447636 | 272 | uint64_t user_timer_save; /* saved user timer value */ |
2d21ac55 A |
273 | uint64_t system_timer_save; /* saved system timer value */ |
274 | uint64_t vtimer_user_save; /* saved values for vtimers */ | |
275 | uint64_t vtimer_prof_save; | |
276 | uint64_t vtimer_rlim_save; | |
1c79356b | 277 | |
9bccf70c | 278 | /* Timed wait expiration */ |
91447636 A |
279 | timer_call_data_t wait_timer; |
280 | integer_t wait_timer_active; | |
281 | boolean_t wait_timer_is_set; | |
1c79356b | 282 | |
9bccf70c | 283 | /* Priority depression expiration */ |
91447636 A |
284 | timer_call_data_t depress_timer; |
285 | integer_t depress_timer_active; | |
1c79356b | 286 | |
2d21ac55 A |
287 | /* |
288 | * Processor/cache affinity | |
289 | * - affinity_threads links task threads with the same affinity set | |
290 | */ | |
291 | affinity_set_t affinity_set; | |
292 | queue_chain_t affinity_threads; | |
293 | ||
1c79356b A |
294 | /* Various bits of stashed state */ |
295 | union { | |
296 | struct { | |
0b4e3aa0 A |
297 | mach_msg_return_t state; /* receive state */ |
298 | ipc_object_t object; /* object received on */ | |
91447636 | 299 | mach_vm_address_t msg_addr; /* receive buffer pointer */ |
0b4e3aa0 A |
300 | mach_msg_size_t msize; /* max size for recvd msg */ |
301 | mach_msg_option_t option; /* options for receive */ | |
302 | mach_msg_size_t slist_size; /* scatter list size */ | |
b0d623f7 | 303 | mach_port_name_t receiver_name; /* the receive port name */ |
0b4e3aa0 A |
304 | struct ipc_kmsg *kmsg; /* received message */ |
305 | mach_port_seqno_t seqno; /* seqno of recvd message */ | |
9bccf70c A |
306 | mach_msg_continue_t continuation; |
307 | } receive; | |
1c79356b | 308 | struct { |
0b4e3aa0 A |
309 | struct semaphore *waitsemaphore; /* semaphore ref */ |
310 | struct semaphore *signalsemaphore; /* semaphore ref */ | |
311 | int options; /* semaphore options */ | |
312 | kern_return_t result; /* primary result */ | |
9bccf70c A |
313 | mach_msg_continue_t continuation; |
314 | } sema; | |
1c79356b | 315 | struct { |
9bccf70c A |
316 | int option; /* switch option */ |
317 | } swtch; | |
318 | int misc; /* catch-all for other state */ | |
319 | } saved; | |
1c79356b | 320 | |
9bccf70c A |
321 | /* IPC data structures */ |
322 | struct ipc_kmsg_queue ith_messages; | |
9bccf70c | 323 | mach_port_t ith_rpc_reply; /* reply port for kernel RPCs */ |
1c79356b A |
324 | |
325 | /* Ast/Halt data structures */ | |
b0d623f7 | 326 | vm_offset_t recover; /* page fault recover(copyin/out) */ |
2d21ac55 | 327 | uint32_t ref_count; /* number of references to me */ |
1c79356b | 328 | |
b0d623f7 | 329 | queue_chain_t threads; /* global list of all threads */ |
1c79356b | 330 | |
55e303ae A |
331 | /* Activation */ |
332 | queue_chain_t task_threads; | |
333 | ||
334 | /*** Machine-dependent state ***/ | |
91447636 | 335 | struct machine_thread machine; |
55e303ae A |
336 | |
337 | /* Task membership */ | |
338 | struct task *task; | |
339 | vm_map_t map; | |
340 | ||
b0d623f7 | 341 | decl_lck_mtx_data(,mutex) |
55e303ae A |
342 | |
343 | /* Kernel holds on this thread */ | |
344 | int suspend_count; | |
345 | ||
346 | /* User level suspensions */ | |
347 | int user_stop_count; | |
348 | ||
349 | /* Pending thread ast(s) */ | |
350 | ast_t ast; | |
351 | ||
91447636 | 352 | /* Miscellaneous bits guarded by mutex */ |
55e303ae | 353 | uint32_t |
2d21ac55 A |
354 | active:1, /* Thread is active and has not been terminated */ |
355 | started:1, /* Thread has been started after creation */ | |
356 | static_param:1, /* Disallow policy parameter changes */ | |
357 | :0; | |
55e303ae A |
358 | |
359 | /* Return Handers */ | |
360 | struct ReturnHandler { | |
361 | struct ReturnHandler *next; | |
362 | void (*handler)( | |
363 | struct ReturnHandler *rh, | |
91447636 | 364 | struct thread *thread); |
55e303ae A |
365 | } *handlers, special_handler; |
366 | ||
367 | /* Ports associated with this thread */ | |
368 | struct ipc_port *ith_self; /* not a right, doesn't hold ref */ | |
369 | struct ipc_port *ith_sself; /* a send right */ | |
370 | struct exception_action exc_actions[EXC_TYPES_COUNT]; | |
371 | ||
372 | /* Owned ulocks (a lock set element) */ | |
373 | queue_head_t held_ulocks; | |
374 | ||
55e303ae A |
375 | #ifdef MACH_BSD |
376 | void *uthread; | |
377 | #endif | |
2d21ac55 A |
378 | |
379 | #if CONFIG_DTRACE | |
380 | uint32_t t_dtrace_predcache;/* DTrace per thread predicate value hint */ | |
381 | int64_t t_dtrace_tracing; /* Thread time under dtrace_probe() */ | |
382 | int64_t t_dtrace_vtime; | |
383 | #endif | |
b0d623f7 A |
384 | |
385 | #define T_CHUD_MARKED 0x1 /* this thread is marked by CHUD */ | |
386 | #define T_IN_CHUD 0x2 /* this thread is already in a CHUD handler */ | |
387 | #define THREAD_PMC_FLAG 0x4 /* Bit in "t_chud" signifying PMC interest */ | |
388 | uint32_t t_page_creation_count; | |
389 | clock_sec_t t_page_creation_time; | |
390 | ||
2d21ac55 | 391 | uint32_t t_chud; /* CHUD flags, used for Shark */ |
6d2010ae A |
392 | |
393 | integer_t mutex_count; /* total count of locks held */ | |
394 | ||
b0d623f7 | 395 | uint64_t thread_id; /*system wide unique thread-id*/ |
6d2010ae A |
396 | |
397 | /* Statistics accumulated per-thread and aggregated per-task */ | |
398 | uint32_t syscalls_unix; | |
399 | uint32_t syscalls_mach; | |
400 | zinfo_usage_store_t tkm_private; /* private kernel memory allocs/frees */ | |
401 | zinfo_usage_store_t tkm_shared; /* shared kernel memory allocs/frees */ | |
402 | struct process_policy ext_actionstate; /* externally applied actions */ | |
403 | struct process_policy ext_policystate; /* externally defined process policy states*/ | |
404 | struct process_policy actionstate; /* self applied acions */ | |
405 | struct process_policy policystate; /* process wide policy states */ | |
0b4e3aa0 | 406 | }; |
1c79356b | 407 | |
1c79356b A |
408 | #define ith_state saved.receive.state |
409 | #define ith_object saved.receive.object | |
91447636 | 410 | #define ith_msg_addr saved.receive.msg_addr |
1c79356b A |
411 | #define ith_msize saved.receive.msize |
412 | #define ith_option saved.receive.option | |
413 | #define ith_scatter_list_size saved.receive.slist_size | |
b0d623f7 | 414 | #define ith_receiver_name saved.receive.receiver_name |
1c79356b A |
415 | #define ith_continuation saved.receive.continuation |
416 | #define ith_kmsg saved.receive.kmsg | |
417 | #define ith_seqno saved.receive.seqno | |
418 | ||
419 | #define sth_waitsemaphore saved.sema.waitsemaphore | |
420 | #define sth_signalsemaphore saved.sema.signalsemaphore | |
421 | #define sth_options saved.sema.options | |
422 | #define sth_result saved.sema.result | |
423 | #define sth_continuation saved.sema.continuation | |
424 | ||
2d21ac55 | 425 | extern void thread_bootstrap(void) __attribute__((section("__TEXT, initcode"))); |
1c79356b | 426 | |
2d21ac55 | 427 | extern void thread_init(void) __attribute__((section("__TEXT, initcode"))); |
9bccf70c | 428 | |
91447636 | 429 | extern void thread_daemon_init(void); |
1c79356b | 430 | |
91447636 | 431 | #define thread_reference_internal(thread) \ |
2d21ac55 | 432 | (void)hw_atomic_add(&(thread)->ref_count, 1) |
91447636 A |
433 | |
434 | #define thread_deallocate_internal(thread) \ | |
435 | hw_atomic_sub(&(thread)->ref_count, 1) | |
436 | ||
437 | #define thread_reference(thread) \ | |
438 | MACRO_BEGIN \ | |
439 | if ((thread) != THREAD_NULL) \ | |
2d21ac55 | 440 | thread_reference_internal(thread); \ |
91447636 | 441 | MACRO_END |
1c79356b | 442 | |
55e303ae A |
443 | extern void thread_deallocate( |
444 | thread_t thread); | |
1c79356b | 445 | |
55e303ae | 446 | extern void thread_terminate_self(void); |
1c79356b | 447 | |
91447636 A |
448 | extern kern_return_t thread_terminate_internal( |
449 | thread_t thread); | |
450 | ||
2d21ac55 A |
451 | extern void thread_start_internal( |
452 | thread_t thread) __attribute__ ((noinline)); | |
453 | ||
91447636 A |
454 | extern void thread_terminate_enqueue( |
455 | thread_t thread); | |
456 | ||
457 | extern void thread_stack_enqueue( | |
458 | thread_t thread); | |
459 | ||
55e303ae | 460 | extern void thread_hold( |
91447636 | 461 | thread_t thread); |
1c79356b | 462 | |
55e303ae | 463 | extern void thread_release( |
91447636 | 464 | thread_t thread); |
1c79356b | 465 | |
b0d623f7 | 466 | |
91447636 | 467 | #define thread_lock_init(th) simple_lock_init(&(th)->sched_lock, 0) |
55e303ae A |
468 | #define thread_lock(th) simple_lock(&(th)->sched_lock) |
469 | #define thread_unlock(th) simple_unlock(&(th)->sched_lock) | |
1c79356b | 470 | |
91447636 A |
471 | #define wake_lock_init(th) simple_lock_init(&(th)->wake_lock, 0) |
472 | #define wake_lock(th) simple_lock(&(th)->wake_lock) | |
473 | #define wake_unlock(th) simple_unlock(&(th)->wake_lock) | |
2d21ac55 A |
474 | |
475 | #define thread_should_halt_fast(thread) (!(thread)->active) | |
1c79356b | 476 | |
91447636 A |
477 | extern void stack_alloc( |
478 | thread_t thread); | |
1c79356b | 479 | |
6d2010ae A |
480 | extern void stack_handoff( |
481 | thread_t from, | |
482 | thread_t to); | |
483 | ||
55e303ae A |
484 | extern void stack_free( |
485 | thread_t thread); | |
1c79356b | 486 | |
6d2010ae A |
487 | extern void stack_free_reserved( |
488 | thread_t thread); | |
1c79356b | 489 | |
91447636 A |
490 | extern boolean_t stack_alloc_try( |
491 | thread_t thread); | |
492 | ||
55e303ae | 493 | extern void stack_collect(void); |
1c79356b | 494 | |
2d21ac55 | 495 | extern void stack_init(void) __attribute__((section("__TEXT, initcode"))); |
91447636 | 496 | |
6d2010ae | 497 | |
91447636 A |
498 | extern kern_return_t thread_state_initialize( |
499 | thread_t thread); | |
500 | ||
1c79356b | 501 | extern kern_return_t thread_setstatus( |
91447636 | 502 | thread_t thread, |
1c79356b A |
503 | int flavor, |
504 | thread_state_t tstate, | |
505 | mach_msg_type_number_t count); | |
506 | ||
507 | extern kern_return_t thread_getstatus( | |
91447636 | 508 | thread_t thread, |
1c79356b A |
509 | int flavor, |
510 | thread_state_t tstate, | |
511 | mach_msg_type_number_t *count); | |
512 | ||
91447636 A |
513 | extern kern_return_t thread_info_internal( |
514 | thread_t thread, | |
1c79356b A |
515 | thread_flavor_t flavor, |
516 | thread_info_t thread_info_out, | |
517 | mach_msg_type_number_t *thread_info_count); | |
518 | ||
55e303ae A |
519 | extern void thread_task_priority( |
520 | thread_t thread, | |
521 | integer_t priority, | |
522 | integer_t max_priority); | |
1c79356b | 523 | |
91447636 A |
524 | extern void thread_policy_reset( |
525 | thread_t thread); | |
1c79356b | 526 | |
91447636 A |
527 | extern kern_return_t kernel_thread_create( |
528 | thread_continue_t continuation, | |
529 | void *parameter, | |
530 | integer_t priority, | |
531 | thread_t *new_thread); | |
1c79356b | 532 | |
91447636 A |
533 | extern kern_return_t kernel_thread_start_priority( |
534 | thread_continue_t continuation, | |
535 | void *parameter, | |
536 | integer_t priority, | |
537 | thread_t *new_thread); | |
1c79356b | 538 | |
55e303ae A |
539 | extern void machine_stack_attach( |
540 | thread_t thread, | |
91447636 | 541 | vm_offset_t stack); |
1c79356b | 542 | |
55e303ae A |
543 | extern vm_offset_t machine_stack_detach( |
544 | thread_t thread); | |
545 | ||
546 | extern void machine_stack_handoff( | |
547 | thread_t old, | |
548 | thread_t new); | |
549 | ||
550 | extern thread_t machine_switch_context( | |
551 | thread_t old_thread, | |
552 | thread_continue_t continuation, | |
553 | thread_t new_thread); | |
554 | ||
555 | extern void machine_load_context( | |
556 | thread_t thread); | |
557 | ||
91447636 A |
558 | extern kern_return_t machine_thread_state_initialize( |
559 | thread_t thread); | |
9bccf70c | 560 | |
55e303ae | 561 | extern kern_return_t machine_thread_set_state( |
91447636 | 562 | thread_t thread, |
55e303ae A |
563 | thread_flavor_t flavor, |
564 | thread_state_t state, | |
565 | mach_msg_type_number_t count); | |
566 | ||
567 | extern kern_return_t machine_thread_get_state( | |
91447636 | 568 | thread_t thread, |
55e303ae A |
569 | thread_flavor_t flavor, |
570 | thread_state_t state, | |
571 | mach_msg_type_number_t *count); | |
572 | ||
573 | extern kern_return_t machine_thread_dup( | |
91447636 A |
574 | thread_t self, |
575 | thread_t target); | |
55e303ae A |
576 | |
577 | extern void machine_thread_init(void); | |
578 | ||
579 | extern kern_return_t machine_thread_create( | |
580 | thread_t thread, | |
581 | task_t task); | |
0c530ab8 | 582 | extern void machine_thread_switch_addrmode( |
2d21ac55 | 583 | thread_t thread); |
55e303ae A |
584 | |
585 | extern void machine_thread_destroy( | |
586 | thread_t thread); | |
587 | ||
91447636 A |
588 | extern void machine_set_current_thread( |
589 | thread_t thread); | |
55e303ae A |
590 | |
591 | extern void machine_thread_terminate_self(void); | |
592 | ||
91447636 A |
593 | extern kern_return_t machine_thread_get_kern_state( |
594 | thread_t thread, | |
595 | thread_flavor_t flavor, | |
596 | thread_state_t tstate, | |
597 | mach_msg_type_number_t *count); | |
598 | ||
b0d623f7 A |
599 | extern kern_return_t machine_thread_inherit_taskwide( |
600 | thread_t thread, | |
601 | task_t parent_task); | |
91447636 | 602 | |
55e303ae A |
603 | /* |
604 | * XXX Funnel locks XXX | |
605 | */ | |
606 | ||
607 | struct funnel_lock { | |
608 | int fnl_type; /* funnel type */ | |
91447636 | 609 | lck_mtx_t *fnl_mutex; /* underlying mutex for the funnel */ |
55e303ae A |
610 | void * fnl_mtxholder; /* thread (last)holdng mutex */ |
611 | void * fnl_mtxrelease; /* thread (last)releasing mutex */ | |
91447636 | 612 | lck_mtx_t *fnl_oldmutex; /* Mutex before collapsing split funnel */ |
55e303ae A |
613 | }; |
614 | ||
55e303ae A |
615 | typedef struct ReturnHandler ReturnHandler; |
616 | ||
b0d623f7 A |
617 | #define thread_mtx_lock(thread) lck_mtx_lock(&(thread)->mutex) |
618 | #define thread_mtx_try(thread) lck_mtx_try_lock(&(thread)->mutex) | |
619 | #define thread_mtx_unlock(thread) lck_mtx_unlock(&(thread)->mutex) | |
55e303ae A |
620 | |
621 | extern void act_execute_returnhandlers(void); | |
622 | ||
623 | extern void install_special_handler( | |
91447636 | 624 | thread_t thread); |
55e303ae A |
625 | |
626 | extern void special_handler( | |
627 | ReturnHandler *rh, | |
91447636 | 628 | thread_t thread); |
1c79356b | 629 | |
2d21ac55 A |
630 | void act_machine_sv_free(thread_t, int); |
631 | ||
b0d623f7 A |
632 | vm_offset_t min_valid_stack_address(void); |
633 | vm_offset_t max_valid_stack_address(void); | |
634 | ||
635 | extern void funnel_lock( | |
636 | struct funnel_lock *lock); | |
637 | ||
638 | extern void funnel_unlock( | |
639 | struct funnel_lock *lock); | |
640 | ||
9bccf70c | 641 | #else /* MACH_KERNEL_PRIVATE */ |
1c79356b | 642 | |
91447636 | 643 | __BEGIN_DECLS |
1c79356b | 644 | |
91447636 A |
645 | extern thread_t current_thread(void); |
646 | ||
647 | extern void thread_reference( | |
648 | thread_t thread); | |
55e303ae | 649 | |
91447636 A |
650 | extern void thread_deallocate( |
651 | thread_t thread); | |
55e303ae | 652 | |
91447636 | 653 | __END_DECLS |
1c79356b | 654 | |
9bccf70c | 655 | #endif /* MACH_KERNEL_PRIVATE */ |
1c79356b | 656 | |
91447636 | 657 | #ifdef KERNEL_PRIVATE |
1c79356b | 658 | |
91447636 | 659 | __BEGIN_DECLS |
55e303ae | 660 | |
b0d623f7 | 661 | #ifndef __LP64__ |
55e303ae | 662 | |
91447636 A |
663 | extern thread_t kernel_thread( |
664 | task_t task, | |
665 | void (*start)(void)); | |
55e303ae | 666 | |
b0d623f7 A |
667 | #endif /* __LP64__ */ |
668 | ||
669 | extern uint64_t thread_tid( | |
670 | thread_t thread); | |
671 | ||
672 | extern uint64_t thread_dispatchqaddr( | |
673 | thread_t thread); | |
674 | ||
91447636 | 675 | __END_DECLS |
55e303ae | 676 | |
91447636 | 677 | #endif /* KERNEL_PRIVATE */ |
55e303ae | 678 | |
91447636 | 679 | __BEGIN_DECLS |
55e303ae | 680 | |
91447636 | 681 | #ifdef XNU_KERNEL_PRIVATE |
9bccf70c | 682 | |
b0d623f7 A |
683 | extern kern_return_t thread_create_workq( |
684 | task_t task, | |
b7266188 | 685 | thread_continue_t thread_return, |
b0d623f7 A |
686 | thread_t *new_thread); |
687 | ||
2d21ac55 A |
688 | extern void thread_yield_internal( |
689 | mach_msg_timeout_t interval); | |
690 | ||
b0d623f7 | 691 | typedef struct funnel_lock funnel_t; |
55e303ae | 692 | |
9bccf70c | 693 | #define THR_FUNNEL_NULL (funnel_t *)0 |
1c79356b | 694 | |
55e303ae A |
695 | extern funnel_t *funnel_alloc( |
696 | int type); | |
1c79356b | 697 | |
91447636 A |
698 | extern void funnel_free( |
699 | funnel_t *lock); | |
1c79356b | 700 | |
b0d623f7 A |
701 | extern funnel_t *thread_funnel_get(void); |
702 | ||
703 | extern boolean_t thread_funnel_set( | |
704 | funnel_t *lock, | |
705 | boolean_t funneled); | |
706 | ||
91447636 A |
707 | extern void thread_read_times( |
708 | thread_t thread, | |
709 | time_value_t *user_time, | |
710 | time_value_t *system_time); | |
1c79356b | 711 | |
91447636 A |
712 | extern void thread_setuserstack( |
713 | thread_t thread, | |
714 | mach_vm_offset_t user_stack); | |
1c79356b | 715 | |
91447636 A |
716 | extern uint64_t thread_adjuserstack( |
717 | thread_t thread, | |
718 | int adjust); | |
55e303ae | 719 | |
91447636 | 720 | extern void thread_setentrypoint( |
55e303ae | 721 | thread_t thread, |
91447636 A |
722 | mach_vm_offset_t entry); |
723 | ||
2d21ac55 | 724 | extern kern_return_t thread_setsinglestep( |
0c530ab8 A |
725 | thread_t thread, |
726 | int on); | |
727 | ||
6d2010ae A |
728 | extern kern_return_t thread_userstack( |
729 | thread_t, | |
730 | int, | |
731 | thread_state_t, | |
732 | unsigned int, | |
733 | mach_vm_offset_t *, | |
734 | int *); | |
735 | ||
736 | kern_return_t thread_entrypoint( | |
737 | thread_t, | |
738 | int, | |
739 | thread_state_t, | |
740 | unsigned int, | |
741 | mach_vm_offset_t *); | |
742 | ||
743 | ||
91447636 A |
744 | extern kern_return_t thread_wire_internal( |
745 | host_priv_t host_priv, | |
746 | thread_t thread, | |
747 | boolean_t wired, | |
748 | boolean_t *prev_state); | |
749 | ||
91447636 A |
750 | extern kern_return_t thread_dup(thread_t); |
751 | ||
2d21ac55 A |
752 | typedef void (*sched_call_t)( |
753 | int type, | |
754 | thread_t thread); | |
755 | ||
756 | #define SCHED_CALL_BLOCK 0x1 | |
757 | #define SCHED_CALL_UNBLOCK 0x2 | |
758 | ||
759 | extern void thread_sched_call( | |
760 | thread_t thread, | |
761 | sched_call_t call); | |
762 | ||
763 | extern void thread_static_param( | |
764 | thread_t thread, | |
765 | boolean_t state); | |
766 | ||
b0d623f7 A |
767 | extern kern_return_t thread_policy_set_internal( |
768 | thread_t thread, | |
769 | thread_policy_flavor_t flavor, | |
770 | thread_policy_t policy_info, | |
771 | mach_msg_type_number_t count); | |
772 | ||
773 | ||
91447636 | 774 | extern task_t get_threadtask(thread_t); |
0c530ab8 A |
775 | #define thread_is_64bit(thd) \ |
776 | task_has_64BitAddr(get_threadtask(thd)) | |
777 | ||
91447636 A |
778 | |
779 | extern void *get_bsdthread_info(thread_t); | |
780 | extern void set_bsdthread_info(thread_t, void *); | |
b0d623f7 | 781 | extern void *uthread_alloc(task_t, thread_t, int); |
2d21ac55 A |
782 | extern void uthread_cleanup(task_t, void *, void *); |
783 | extern void uthread_zone_free(void *); | |
784 | extern void uthread_cred_free(void *); | |
91447636 A |
785 | |
786 | extern boolean_t thread_should_halt( | |
787 | thread_t thread); | |
788 | ||
2d21ac55 A |
789 | extern int is_64signalregset(void); |
790 | ||
791 | void act_set_apc(thread_t); | |
792 | ||
793 | extern uint32_t dtrace_get_thread_predcache(thread_t); | |
794 | extern int64_t dtrace_get_thread_vtime(thread_t); | |
795 | extern int64_t dtrace_get_thread_tracing(thread_t); | |
796 | extern boolean_t dtrace_get_thread_reentering(thread_t); | |
797 | extern vm_offset_t dtrace_get_kernel_stack(thread_t); | |
798 | extern void dtrace_set_thread_predcache(thread_t, uint32_t); | |
799 | extern void dtrace_set_thread_vtime(thread_t, int64_t); | |
800 | extern void dtrace_set_thread_tracing(thread_t, int64_t); | |
801 | extern void dtrace_set_thread_reentering(thread_t, boolean_t); | |
802 | extern vm_offset_t dtrace_set_thread_recover(thread_t, vm_offset_t); | |
b0d623f7 | 803 | extern void dtrace_thread_bootstrap(void); |
2d21ac55 A |
804 | |
805 | extern int64_t dtrace_calc_thread_recent_vtime(thread_t); | |
806 | ||
807 | ||
808 | extern void thread_set_wq_state32( | |
809 | thread_t thread, | |
810 | thread_state_t tstate); | |
811 | ||
812 | extern void thread_set_wq_state64( | |
813 | thread_t thread, | |
814 | thread_state_t tstate); | |
815 | ||
b0d623f7 A |
816 | extern vm_offset_t kernel_stack_mask; |
817 | extern vm_offset_t kernel_stack_size; | |
818 | extern vm_offset_t kernel_stack_depth_max; | |
819 | ||
91447636 A |
820 | #endif /* XNU_KERNEL_PRIVATE */ |
821 | ||
b0d623f7 A |
822 | /*! @function kernel_thread_start |
823 | @abstract Create a kernel thread. | |
824 | @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). | |
825 | @param continuation A C-function pointer where the thread will begin execution. | |
826 | @param parameter Caller specified data to be passed to the new thread. | |
827 | @param new_thread Reference to the new thread is returned in this parameter. | |
828 | @result Returns KERN_SUCCESS on success or an appropriate kernel code type. | |
829 | */ | |
830 | ||
91447636 A |
831 | extern kern_return_t kernel_thread_start( |
832 | thread_continue_t continuation, | |
833 | void *parameter, | |
834 | thread_t *new_thread); | |
6d2010ae A |
835 | #ifdef KERNEL_PRIVATE |
836 | void thread_set_eager_preempt(thread_t thread); | |
837 | void thread_clear_eager_preempt(thread_t thread); | |
838 | #endif /* KERNEL_PRIVATE */ | |
55e303ae | 839 | |
91447636 | 840 | __END_DECLS |
55e303ae | 841 | |
1c79356b | 842 | #endif /* _KERN_THREAD_H_ */ |