]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/thread.h
b497fa3facfd3e7999dd50c42d37bc9e5726bf38
[apple/xnu.git] / osfmk / kern / thread.h
1 /*
2 * Copyright (c) 2000-2009 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 <cputypes.h>
103
104 #include <mach_assert.h>
105 #include <mach_ldebug.h>
106
107 #include <ipc/ipc_types.h>
108
109 #include <mach/port.h>
110 #include <kern/cpu_number.h>
111 #include <kern/queue.h>
112 #include <kern/timer.h>
113 #include <kern/lock.h>
114 #include <kern/locks.h>
115 #include <kern/sched.h>
116 #include <kern/sched_prim.h>
117 #include <kern/thread_call.h>
118 #include <kern/timer_call.h>
119 #include <kern/task.h>
120 #include <kern/exception.h>
121 #include <kern/affinity.h>
122
123 #include <ipc/ipc_kmsg.h>
124
125 #include <machine/cpu_data.h>
126 #include <machine/thread.h>
127
128 struct thread {
129 /*
130 * NOTE: The runq field in the thread structure has an unusual
131 * locking protocol. If its value is PROCESSOR_NULL, then it is
132 * locked by the thread_lock, but if its value is something else
133 * then it is locked by the associated run queue lock.
134 *
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).
139 */
140 /* Items examined often, modified infrequently */
141 queue_chain_t links; /* run/wait queue links */
142 processor_t runq; /* run queue assignment */
143 wait_queue_t wait_queue; /* wait queue we are currently on */
144 event64_t wait_event; /* wait queue event */
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 */
148 #define TH_OPT_DTRACE 0x08 /* executing under dtrace_probe */
149 #define TH_OPT_SYSTEM_CRITICAL 0x10 /* Thread must always be allowed to run - even under heavy load */
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 */
152
153 /* Data updated during assert_wait/thread_wakeup */
154 decl_simple_lock_data(,sched_lock) /* scheduling lock (thread_lock()) */
155 decl_simple_lock_data(,wake_lock) /* for thread stop / wait (wake_lock()) */
156 boolean_t wake_active; /* wake event on stop */
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 -
160 * may be examined by this thread
161 * WITHOUT locking */
162 thread_continue_t continuation; /* continue here next dispatch */
163 void *parameter; /* continuation parameter */
164
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 */
170
171 vm_offset_t kernel_stack; /* current kernel stack */
172 vm_offset_t reserved_stack; /* reserved kernel stack */
173
174 /* Thread state: */
175 int state;
176 /*
177 * Thread states [bits or'ed]
178 */
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 */
184 #define TH_TERMINATE2 0x20 /* added to termination queue */
185
186 #define TH_IDLE 0x80 /* idling processor */
187
188 /* Scheduling information */
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
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)
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
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
227 integer_t promotions; /* level of promotion */
228 integer_t pending_promoter_index;
229 void *pending_promoter[2];
230
231 integer_t importance; /* task-relative importance */
232
233 /* real-time parameters */
234 struct { /* see mach/thread_policy.h */
235 uint32_t period;
236 uint32_t computation;
237 uint32_t constraint;
238 boolean_t preemptible;
239
240 uint64_t deadline;
241 } realtime;
242
243 uint32_t was_promoted_on_wakeup;
244 uint32_t current_quantum; /* duration of current quantum */
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 */
247
248 /* Data used during setrun/dispatch */
249 timer_data_t system_timer; /* system mode timer */
250 processor_t bound_processor; /* bound to a processor? */
251 processor_t last_processor; /* processor last dispatched on */
252 processor_t chosen_processor; /* Where we want to run this thread */
253
254 /* Fail-safe computation since last unblock or qualifying yield */
255 uint64_t computation_metered;
256 uint64_t computation_epoch;
257 uint64_t safe_release; /* when to release fail-safe */
258
259 /* Call out from scheduler */
260 void (*sched_call)(
261 int type,
262 thread_t thread);
263 #if defined(CONFIG_SCHED_PROTO)
264 uint32_t runqueue_generation; /* last time runqueue was drained */
265 #endif
266
267 /* Statistics and timesharing calculations */
268 #if defined(CONFIG_SCHED_TRADITIONAL)
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 */
274 #endif
275 uint32_t c_switch; /* total context switches */
276 uint32_t p_switch; /* total processor switches */
277 uint32_t ps_switch; /* total pset switches */
278
279 /* Timing data structures */
280 int precise_user_kernel_time; /* precise user/kernel enabled for this thread */
281 timer_data_t user_timer; /* user mode timer */
282 uint64_t user_timer_save; /* saved user timer value */
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;
287
288 /* Timed wait expiration */
289 timer_call_data_t wait_timer;
290 integer_t wait_timer_active;
291 boolean_t wait_timer_is_set;
292
293 /* Priority depression expiration */
294 timer_call_data_t depress_timer;
295 integer_t depress_timer_active;
296
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
304 /* Various bits of stashed state */
305 union {
306 struct {
307 mach_msg_return_t state; /* receive state */
308 ipc_object_t object; /* object received on */
309 mach_vm_address_t msg_addr; /* receive buffer pointer */
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 */
313 mach_port_name_t receiver_name; /* the receive port name */
314 struct ipc_kmsg *kmsg; /* received message */
315 mach_port_seqno_t seqno; /* seqno of recvd message */
316 mach_msg_continue_t continuation;
317 } receive;
318 struct {
319 struct semaphore *waitsemaphore; /* semaphore ref */
320 struct semaphore *signalsemaphore; /* semaphore ref */
321 int options; /* semaphore options */
322 kern_return_t result; /* primary result */
323 mach_msg_continue_t continuation;
324 } sema;
325 struct {
326 int option; /* switch option */
327 } swtch;
328 int misc; /* catch-all for other state */
329 } saved;
330
331 /* IPC data structures */
332 struct ipc_kmsg_queue ith_messages;
333 mach_port_t ith_rpc_reply; /* reply port for kernel RPCs */
334
335 /* Ast/Halt data structures */
336 vm_offset_t recover; /* page fault recover(copyin/out) */
337 uint32_t ref_count; /* number of references to me */
338
339 queue_chain_t threads; /* global list of all threads */
340
341 /* Activation */
342 queue_chain_t task_threads;
343
344 /*** Machine-dependent state ***/
345 struct machine_thread machine;
346
347 /* Task membership */
348 struct task *task;
349 vm_map_t map;
350
351 decl_lck_mtx_data(,mutex)
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
362 /* Miscellaneous bits guarded by mutex */
363 uint32_t
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;
368
369 /* Return Handers */
370 struct ReturnHandler {
371 struct ReturnHandler *next;
372 void (*handler)(
373 struct ReturnHandler *rh,
374 struct thread *thread);
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
385 #ifdef MACH_BSD
386 void *uthread;
387 #endif
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
394
395 uint32_t t_page_creation_count;
396 clock_sec_t t_page_creation_time;
397
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
410 uint32_t t_chud; /* CHUD flags, used for Shark */
411 uint32_t chud_c_switch; /* last dispatch detection */
412
413 integer_t mutex_count; /* total count of locks held */
414
415 uint64_t thread_id; /*system wide unique thread-id*/
416
417 /* Statistics accumulated per-thread and aggregated per-task */
418 uint32_t syscalls_unix;
419 uint32_t syscalls_mach;
420 ledger_t t_ledger;
421 ledger_t t_threadledger; /* per thread ledger */
422 struct process_policy ext_appliedstate; /* externally applied actions */
423 struct process_policy ext_policystate; /* externally defined process policy states*/
424 struct process_policy appliedstate; /* self applied acions */
425 struct process_policy policystate; /* process wide policy states */
426 #if CONFIG_EMBEDDED
427 task_watch_t * taskwatch; /* task watch */
428 integer_t saved_importance; /* saved task-relative importance */
429 #endif /* CONFIG_EMBEDDED */
430 };
431
432 #define ith_state saved.receive.state
433 #define ith_object saved.receive.object
434 #define ith_msg_addr saved.receive.msg_addr
435 #define ith_msize saved.receive.msize
436 #define ith_option saved.receive.option
437 #define ith_scatter_list_size saved.receive.slist_size
438 #define ith_receiver_name saved.receive.receiver_name
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
449 extern void thread_bootstrap(void) __attribute__((section("__TEXT, initcode")));
450
451 extern void thread_init(void) __attribute__((section("__TEXT, initcode")));
452
453 extern void thread_daemon_init(void);
454
455 #define thread_reference_internal(thread) \
456 (void)hw_atomic_add(&(thread)->ref_count, 1)
457
458 #define thread_deallocate_internal(thread) \
459 hw_atomic_sub(&(thread)->ref_count, 1)
460
461 #define thread_reference(thread) \
462 MACRO_BEGIN \
463 if ((thread) != THREAD_NULL) \
464 thread_reference_internal(thread); \
465 MACRO_END
466
467 extern void thread_deallocate(
468 thread_t thread);
469
470 extern void thread_terminate_self(void);
471
472 extern kern_return_t thread_terminate_internal(
473 thread_t thread);
474
475 extern void thread_start_internal(
476 thread_t thread) __attribute__ ((noinline));
477
478 extern void thread_terminate_enqueue(
479 thread_t thread);
480
481 extern void thread_stack_enqueue(
482 thread_t thread);
483
484 extern void thread_hold(
485 thread_t thread);
486
487 extern void thread_release(
488 thread_t thread);
489
490
491 #define thread_lock_init(th) simple_lock_init(&(th)->sched_lock, 0)
492 #define thread_lock(th) simple_lock(&(th)->sched_lock)
493 #define thread_unlock(th) simple_unlock(&(th)->sched_lock)
494
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)
498
499 #define thread_should_halt_fast(thread) (!(thread)->active)
500
501 extern void stack_alloc(
502 thread_t thread);
503
504 extern void stack_handoff(
505 thread_t from,
506 thread_t to);
507
508 extern void stack_free(
509 thread_t thread);
510
511 extern void stack_free_reserved(
512 thread_t thread);
513
514 extern boolean_t stack_alloc_try(
515 thread_t thread);
516
517 extern void stack_collect(void);
518
519 extern void stack_init(void) __attribute__((section("__TEXT, initcode")));
520
521
522 extern kern_return_t thread_info_internal(
523 thread_t thread,
524 thread_flavor_t flavor,
525 thread_info_t thread_info_out,
526 mach_msg_type_number_t *thread_info_count);
527
528 extern void thread_task_priority(
529 thread_t thread,
530 integer_t priority,
531 integer_t max_priority);
532
533 extern void thread_policy_reset(
534 thread_t thread);
535
536 extern kern_return_t kernel_thread_create(
537 thread_continue_t continuation,
538 void *parameter,
539 integer_t priority,
540 thread_t *new_thread);
541
542 extern kern_return_t kernel_thread_start_priority(
543 thread_continue_t continuation,
544 void *parameter,
545 integer_t priority,
546 thread_t *new_thread);
547
548 extern void machine_stack_attach(
549 thread_t thread,
550 vm_offset_t stack);
551
552 extern vm_offset_t machine_stack_detach(
553 thread_t thread);
554
555 extern void machine_stack_handoff(
556 thread_t old,
557 thread_t new);
558
559 extern thread_t machine_switch_context(
560 thread_t old_thread,
561 thread_continue_t continuation,
562 thread_t new_thread);
563
564 extern void machine_load_context(
565 thread_t thread);
566
567 extern kern_return_t machine_thread_state_initialize(
568 thread_t thread);
569
570 extern kern_return_t machine_thread_set_state(
571 thread_t thread,
572 thread_flavor_t flavor,
573 thread_state_t state,
574 mach_msg_type_number_t count);
575
576 extern kern_return_t machine_thread_get_state(
577 thread_t thread,
578 thread_flavor_t flavor,
579 thread_state_t state,
580 mach_msg_type_number_t *count);
581
582 extern kern_return_t machine_thread_dup(
583 thread_t self,
584 thread_t target);
585
586 extern void machine_thread_init(void);
587
588 extern kern_return_t machine_thread_create(
589 thread_t thread,
590 task_t task);
591 extern void machine_thread_switch_addrmode(
592 thread_t thread);
593
594 extern void machine_thread_destroy(
595 thread_t thread);
596
597 extern void machine_set_current_thread(
598 thread_t thread);
599
600 extern 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
606 extern kern_return_t machine_thread_inherit_taskwide(
607 thread_t thread,
608 task_t parent_task);
609
610 /*
611 * XXX Funnel locks XXX
612 */
613
614 struct funnel_lock {
615 int fnl_type; /* funnel type */
616 lck_mtx_t *fnl_mutex; /* underlying mutex for the funnel */
617 void * fnl_mtxholder; /* thread (last)holdng mutex */
618 void * fnl_mtxrelease; /* thread (last)releasing mutex */
619 lck_mtx_t *fnl_oldmutex; /* Mutex before collapsing split funnel */
620 };
621
622 typedef struct ReturnHandler ReturnHandler;
623
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)
627
628 extern void act_execute_returnhandlers(void);
629
630 extern void install_special_handler(
631 thread_t thread);
632
633 extern void special_handler(
634 ReturnHandler *rh,
635 thread_t thread);
636
637 void act_machine_sv_free(thread_t, int);
638
639 vm_offset_t min_valid_stack_address(void);
640 vm_offset_t max_valid_stack_address(void);
641
642 extern void funnel_lock(
643 struct funnel_lock *lock);
644
645 extern void funnel_unlock(
646 struct funnel_lock *lock);
647
648 #else /* MACH_KERNEL_PRIVATE */
649
650 __BEGIN_DECLS
651
652 extern thread_t current_thread(void);
653
654 extern void thread_reference(
655 thread_t thread);
656
657 extern void thread_deallocate(
658 thread_t thread);
659
660 __END_DECLS
661
662 #endif /* MACH_KERNEL_PRIVATE */
663
664 #ifdef KERNEL_PRIVATE
665
666 __BEGIN_DECLS
667
668 #if defined(__i386__)
669
670 extern thread_t kernel_thread(
671 task_t task,
672 void (*start)(void));
673
674 #endif /* defined(__i386__) */
675
676 extern uint64_t thread_tid(
677 thread_t thread);
678
679 extern uint64_t thread_dispatchqaddr(
680 thread_t thread);
681
682 __END_DECLS
683
684 #endif /* KERNEL_PRIVATE */
685
686 __BEGIN_DECLS
687
688 #ifdef XNU_KERNEL_PRIVATE
689
690 extern kern_return_t thread_state_initialize(
691 thread_t thread);
692
693 extern 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
699 extern 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
705 extern kern_return_t thread_create_workq(
706 task_t task,
707 thread_continue_t thread_return,
708 thread_t *new_thread);
709
710 extern void thread_yield_internal(
711 mach_msg_timeout_t interval);
712
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
722 struct _thread_ledger_indices {
723 int cpu_time;
724 };
725
726 extern struct _thread_ledger_indices thread_ledgers;
727
728 extern int thread_set_cpulimit(int action, uint8_t percentage, uint64_t interval_ns);
729
730 typedef struct funnel_lock funnel_t;
731
732 #define THR_FUNNEL_NULL (funnel_t *)0
733
734 extern funnel_t *funnel_alloc(
735 int type);
736
737 extern void funnel_free(
738 funnel_t *lock);
739
740 extern funnel_t *thread_funnel_get(void);
741
742 extern boolean_t thread_funnel_set(
743 funnel_t *lock,
744 boolean_t funneled);
745
746 extern void thread_read_times(
747 thread_t thread,
748 time_value_t *user_time,
749 time_value_t *system_time);
750
751 extern void thread_setuserstack(
752 thread_t thread,
753 mach_vm_offset_t user_stack);
754
755 extern uint64_t thread_adjuserstack(
756 thread_t thread,
757 int adjust);
758
759 extern void thread_setentrypoint(
760 thread_t thread,
761 mach_vm_offset_t entry);
762
763 extern kern_return_t thread_setsinglestep(
764 thread_t thread,
765 int on);
766
767 extern 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
775 extern kern_return_t thread_entrypoint(
776 thread_t,
777 int,
778 thread_state_t,
779 unsigned int,
780 mach_vm_offset_t *);
781
782 extern kern_return_t thread_userstackdefault(
783 thread_t,
784 mach_vm_offset_t *);
785
786 extern 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
792 extern kern_return_t thread_dup(thread_t);
793
794 typedef 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
801 extern void thread_sched_call(
802 thread_t thread,
803 sched_call_t call);
804
805 extern void thread_static_param(
806 thread_t thread,
807 boolean_t state);
808
809 extern 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
816 extern task_t get_threadtask(thread_t);
817 #define thread_is_64bit(thd) \
818 task_has_64BitAddr(get_threadtask(thd))
819
820
821 extern void *get_bsdthread_info(thread_t);
822 extern void set_bsdthread_info(thread_t, void *);
823 extern void *uthread_alloc(task_t, thread_t, int);
824 extern void uthread_cleanup(task_t, void *, void *);
825 extern void uthread_zone_free(void *);
826 extern void uthread_cred_free(void *);
827
828 extern boolean_t thread_should_halt(
829 thread_t thread);
830
831 extern boolean_t thread_should_abort(
832 thread_t);
833
834 extern int is_64signalregset(void);
835
836 void act_set_apc(thread_t);
837 void act_set_kperf(thread_t);
838
839 extern uint32_t dtrace_get_thread_predcache(thread_t);
840 extern int64_t dtrace_get_thread_vtime(thread_t);
841 extern int64_t dtrace_get_thread_tracing(thread_t);
842 extern boolean_t dtrace_get_thread_reentering(thread_t);
843 extern vm_offset_t dtrace_get_kernel_stack(thread_t);
844 extern void dtrace_set_thread_predcache(thread_t, uint32_t);
845 extern void dtrace_set_thread_vtime(thread_t, int64_t);
846 extern void dtrace_set_thread_tracing(thread_t, int64_t);
847 extern void dtrace_set_thread_reentering(thread_t, boolean_t);
848 extern vm_offset_t dtrace_set_thread_recover(thread_t, vm_offset_t);
849 extern void dtrace_thread_bootstrap(void);
850
851 extern int64_t dtrace_calc_thread_recent_vtime(thread_t);
852
853
854 extern void thread_set_wq_state32(
855 thread_t thread,
856 thread_state_t tstate);
857
858 extern void thread_set_wq_state64(
859 thread_t thread,
860 thread_state_t tstate);
861
862 extern vm_offset_t kernel_stack_mask;
863 extern vm_offset_t kernel_stack_size;
864 extern vm_offset_t kernel_stack_depth_max;
865
866 #endif /* XNU_KERNEL_PRIVATE */
867
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
877 extern kern_return_t kernel_thread_start(
878 thread_continue_t continuation,
879 void *parameter,
880 thread_t *new_thread);
881 #ifdef KERNEL_PRIVATE
882 void thread_set_eager_preempt(thread_t thread);
883 void thread_clear_eager_preempt(thread_t thread);
884 extern ipc_port_t convert_thread_to_port(thread_t);
885 #endif /* KERNEL_PRIVATE */
886
887 __END_DECLS
888
889 #endif /* _KERN_THREAD_H_ */