]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/thread.c
xnu-1699.32.7.tar.gz
[apple/xnu.git] / osfmk / kern / thread.c
1 /*
2 * Copyright (c) 2000-2010 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: kern/thread.c
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub
61 * Date: 1986
62 *
63 * Thread management primitives implementation.
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 #include <mach/mach_types.h>
85 #include <mach/boolean.h>
86 #include <mach/policy.h>
87 #include <mach/thread_info.h>
88 #include <mach/thread_special_ports.h>
89 #include <mach/thread_status.h>
90 #include <mach/time_value.h>
91 #include <mach/vm_param.h>
92
93 #include <machine/thread.h>
94 #include <machine/pal_routines.h>
95
96 #include <kern/kern_types.h>
97 #include <kern/kalloc.h>
98 #include <kern/cpu_data.h>
99 #include <kern/counters.h>
100 #include <kern/extmod_statistics.h>
101 #include <kern/ipc_mig.h>
102 #include <kern/ipc_tt.h>
103 #include <kern/mach_param.h>
104 #include <kern/machine.h>
105 #include <kern/misc_protos.h>
106 #include <kern/processor.h>
107 #include <kern/queue.h>
108 #include <kern/sched.h>
109 #include <kern/sched_prim.h>
110 #include <kern/sync_lock.h>
111 #include <kern/syscall_subr.h>
112 #include <kern/task.h>
113 #include <kern/thread.h>
114 #include <kern/host.h>
115 #include <kern/zalloc.h>
116 #include <kern/assert.h>
117
118 #include <ipc/ipc_kmsg.h>
119 #include <ipc/ipc_port.h>
120
121 #include <vm/vm_kern.h>
122 #include <vm/vm_pageout.h>
123
124 #include <sys/kdebug.h>
125
126 #include <mach/sdt.h>
127
128 /*
129 * Exported interfaces
130 */
131 #include <mach/task_server.h>
132 #include <mach/thread_act_server.h>
133 #include <mach/mach_host_server.h>
134 #include <mach/host_priv_server.h>
135
136 static struct zone *thread_zone;
137 static lck_grp_attr_t thread_lck_grp_attr;
138 lck_attr_t thread_lck_attr;
139 lck_grp_t thread_lck_grp;
140
141 decl_simple_lock_data(static,thread_stack_lock)
142 static queue_head_t thread_stack_queue;
143
144 decl_simple_lock_data(static,thread_terminate_lock)
145 static queue_head_t thread_terminate_queue;
146
147 static struct thread thread_template, init_thread;
148
149 static void sched_call_null(
150 int type,
151 thread_t thread);
152
153 #ifdef MACH_BSD
154 extern void proc_exit(void *);
155 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
156 #endif /* MACH_BSD */
157
158 extern int debug_task;
159 int thread_max = CONFIG_THREAD_MAX; /* Max number of threads */
160 int task_threadmax = CONFIG_THREAD_MAX;
161
162 static uint64_t thread_unique_id = 0;
163
164 void
165 thread_bootstrap(void)
166 {
167 /*
168 * Fill in a template thread for fast initialization.
169 */
170
171 thread_template.runq = PROCESSOR_NULL;
172
173 thread_template.ref_count = 2;
174
175 thread_template.reason = AST_NONE;
176 thread_template.at_safe_point = FALSE;
177 thread_template.wait_event = NO_EVENT64;
178 thread_template.wait_queue = WAIT_QUEUE_NULL;
179 thread_template.wait_result = THREAD_WAITING;
180 thread_template.options = THREAD_ABORTSAFE;
181 thread_template.state = TH_WAIT | TH_UNINT;
182 thread_template.wake_active = FALSE;
183 thread_template.continuation = THREAD_CONTINUE_NULL;
184 thread_template.parameter = NULL;
185
186 thread_template.importance = 0;
187 thread_template.sched_mode = TH_MODE_NONE;
188 thread_template.sched_flags = 0;
189 thread_template.saved_mode = TH_MODE_NONE;
190 thread_template.safe_release = 0;
191
192 thread_template.priority = 0;
193 thread_template.sched_pri = 0;
194 thread_template.max_priority = 0;
195 thread_template.task_priority = 0;
196 thread_template.promotions = 0;
197 thread_template.pending_promoter_index = 0;
198 thread_template.pending_promoter[0] =
199 thread_template.pending_promoter[1] = NULL;
200
201 thread_template.realtime.deadline = UINT64_MAX;
202
203 thread_template.current_quantum = 0;
204 thread_template.last_run_time = 0;
205 thread_template.last_quantum_refill_time = 0;
206
207 thread_template.computation_metered = 0;
208 thread_template.computation_epoch = 0;
209
210 #if defined(CONFIG_SCHED_TRADITIONAL)
211 thread_template.sched_stamp = 0;
212 thread_template.pri_shift = INT8_MAX;
213 thread_template.sched_usage = 0;
214 thread_template.cpu_usage = thread_template.cpu_delta = 0;
215 #endif
216 thread_template.c_switch = thread_template.p_switch = thread_template.ps_switch = 0;
217
218 thread_template.bound_processor = PROCESSOR_NULL;
219 thread_template.last_processor = PROCESSOR_NULL;
220
221 thread_template.sched_call = sched_call_null;
222
223 timer_init(&thread_template.user_timer);
224 timer_init(&thread_template.system_timer);
225 thread_template.user_timer_save = 0;
226 thread_template.system_timer_save = 0;
227 thread_template.vtimer_user_save = 0;
228 thread_template.vtimer_prof_save = 0;
229 thread_template.vtimer_rlim_save = 0;
230
231 thread_template.wait_timer_is_set = FALSE;
232 thread_template.wait_timer_active = 0;
233
234 thread_template.depress_timer_active = 0;
235
236 thread_template.special_handler.handler = special_handler;
237 thread_template.special_handler.next = NULL;
238
239 thread_template.funnel_lock = THR_FUNNEL_NULL;
240 thread_template.funnel_state = 0;
241 thread_template.recover = (vm_offset_t)NULL;
242
243 thread_template.map = VM_MAP_NULL;
244
245 #if CONFIG_DTRACE
246 thread_template.t_dtrace_predcache = 0;
247 thread_template.t_dtrace_vtime = 0;
248 thread_template.t_dtrace_tracing = 0;
249 #endif /* CONFIG_DTRACE */
250
251 thread_template.t_chud = 0;
252 thread_template.t_page_creation_count = 0;
253 thread_template.t_page_creation_time = 0;
254
255 thread_template.affinity_set = NULL;
256
257 thread_template.syscalls_unix = 0;
258 thread_template.syscalls_mach = 0;
259
260 thread_template.tkm_private.alloc = 0;
261 thread_template.tkm_private.free = 0;
262 thread_template.tkm_shared.alloc = 0;
263 thread_template.tkm_shared.free = 0;
264 thread_template.actionstate = default_task_null_policy;
265 thread_template.ext_actionstate = default_task_null_policy;
266 thread_template.policystate = default_task_proc_policy;
267 thread_template.ext_policystate = default_task_proc_policy;
268
269 init_thread = thread_template;
270 machine_set_current_thread(&init_thread);
271 }
272
273 void
274 thread_init(void)
275 {
276 thread_zone = zinit(
277 sizeof(struct thread),
278 thread_max * sizeof(struct thread),
279 THREAD_CHUNK * sizeof(struct thread),
280 "threads");
281
282 lck_grp_attr_setdefault(&thread_lck_grp_attr);
283 lck_grp_init(&thread_lck_grp, "thread", &thread_lck_grp_attr);
284 lck_attr_setdefault(&thread_lck_attr);
285
286 stack_init();
287
288 /*
289 * Initialize any machine-dependent
290 * per-thread structures necessary.
291 */
292 machine_thread_init();
293 }
294
295 static void
296 thread_terminate_continue(void)
297 {
298 panic("thread_terminate_continue");
299 /*NOTREACHED*/
300 }
301
302 /*
303 * thread_terminate_self:
304 */
305 void
306 thread_terminate_self(void)
307 {
308 thread_t thread = current_thread();
309
310 task_t task;
311 spl_t s;
312 int threadcnt;
313
314 pal_thread_terminate_self(thread);
315
316 DTRACE_PROC(lwp__exit);
317
318 thread_mtx_lock(thread);
319
320 ulock_release_all(thread);
321
322 ipc_thread_disable(thread);
323
324 thread_mtx_unlock(thread);
325
326 s = splsched();
327 thread_lock(thread);
328
329 /*
330 * Cancel priority depression, wait for concurrent expirations
331 * on other processors.
332 */
333 if (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) {
334 thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK;
335
336 if (timer_call_cancel(&thread->depress_timer))
337 thread->depress_timer_active--;
338 }
339
340 while (thread->depress_timer_active > 0) {
341 thread_unlock(thread);
342 splx(s);
343
344 delay(1);
345
346 s = splsched();
347 thread_lock(thread);
348 }
349
350 thread_sched_call(thread, NULL);
351
352 thread_unlock(thread);
353 splx(s);
354
355 thread_policy_reset(thread);
356
357 task = thread->task;
358 uthread_cleanup(task, thread->uthread, task->bsd_info);
359 threadcnt = hw_atomic_sub(&task->active_thread_count, 1);
360
361 /*
362 * If we are the last thread to terminate and the task is
363 * associated with a BSD process, perform BSD process exit.
364 */
365 if (threadcnt == 0 && task->bsd_info != NULL)
366 proc_exit(task->bsd_info);
367
368 uthread_cred_free(thread->uthread);
369
370 s = splsched();
371 thread_lock(thread);
372
373 /*
374 * Cancel wait timer, and wait for
375 * concurrent expirations.
376 */
377 if (thread->wait_timer_is_set) {
378 thread->wait_timer_is_set = FALSE;
379
380 if (timer_call_cancel(&thread->wait_timer))
381 thread->wait_timer_active--;
382 }
383
384 while (thread->wait_timer_active > 0) {
385 thread_unlock(thread);
386 splx(s);
387
388 delay(1);
389
390 s = splsched();
391 thread_lock(thread);
392 }
393
394 /*
395 * If there is a reserved stack, release it.
396 */
397 if (thread->reserved_stack != 0) {
398 stack_free_reserved(thread);
399 thread->reserved_stack = 0;
400 }
401
402 /*
403 * Mark thread as terminating, and block.
404 */
405 thread->state |= TH_TERMINATE;
406 thread_mark_wait_locked(thread, THREAD_UNINT);
407 assert(thread->promotions == 0);
408 thread_unlock(thread);
409 /* splsched */
410
411 thread_block((thread_continue_t)thread_terminate_continue);
412 /*NOTREACHED*/
413 }
414
415 void
416 thread_deallocate(
417 thread_t thread)
418 {
419 task_t task;
420
421 if (thread == THREAD_NULL)
422 return;
423
424 if (thread_deallocate_internal(thread) > 0)
425 return;
426
427
428 ipc_thread_terminate(thread);
429
430 task = thread->task;
431
432 #ifdef MACH_BSD
433 {
434 void *ut = thread->uthread;
435
436 thread->uthread = NULL;
437 uthread_zone_free(ut);
438 }
439 #endif /* MACH_BSD */
440
441 if (thread->kernel_stack != 0)
442 stack_free(thread);
443
444 lck_mtx_destroy(&thread->mutex, &thread_lck_grp);
445 machine_thread_destroy(thread);
446
447 task_deallocate(task);
448
449 zfree(thread_zone, thread);
450 }
451
452 /*
453 * thread_terminate_daemon:
454 *
455 * Perform final clean up for terminating threads.
456 */
457 static void
458 thread_terminate_daemon(void)
459 {
460 thread_t self, thread;
461 task_t task;
462
463 self = current_thread();
464 self->options |= TH_OPT_SYSTEM_CRITICAL;
465
466 (void)splsched();
467 simple_lock(&thread_terminate_lock);
468
469 while ((thread = (thread_t)dequeue_head(&thread_terminate_queue)) != THREAD_NULL) {
470 simple_unlock(&thread_terminate_lock);
471 (void)spllo();
472
473 task = thread->task;
474
475 task_lock(task);
476 task->total_user_time += timer_grab(&thread->user_timer);
477 task->total_system_time += timer_grab(&thread->system_timer);
478
479 task->c_switch += thread->c_switch;
480 task->p_switch += thread->p_switch;
481 task->ps_switch += thread->ps_switch;
482
483 task->syscalls_unix += thread->syscalls_unix;
484 task->syscalls_mach += thread->syscalls_mach;
485
486 task->tkm_private.alloc += thread->tkm_private.alloc;
487 task->tkm_private.free += thread->tkm_private.free;
488 task->tkm_shared.alloc += thread->tkm_shared.alloc;
489 task->tkm_shared.free += thread->tkm_shared.free;
490
491 queue_remove(&task->threads, thread, thread_t, task_threads);
492 task->thread_count--;
493
494 /*
495 * If the task is being halted, and there is only one thread
496 * left in the task after this one, then wakeup that thread.
497 */
498 if (task->thread_count == 1 && task->halting)
499 thread_wakeup((event_t)&task->halting);
500
501 task_unlock(task);
502
503 lck_mtx_lock(&tasks_threads_lock);
504 queue_remove(&threads, thread, thread_t, threads);
505 threads_count--;
506 lck_mtx_unlock(&tasks_threads_lock);
507
508 thread_deallocate(thread);
509
510 (void)splsched();
511 simple_lock(&thread_terminate_lock);
512 }
513
514 assert_wait((event_t)&thread_terminate_queue, THREAD_UNINT);
515 simple_unlock(&thread_terminate_lock);
516 /* splsched */
517
518 self->options &= ~TH_OPT_SYSTEM_CRITICAL;
519 thread_block((thread_continue_t)thread_terminate_daemon);
520 /*NOTREACHED*/
521 }
522
523 /*
524 * thread_terminate_enqueue:
525 *
526 * Enqueue a terminating thread for final disposition.
527 *
528 * Called at splsched.
529 */
530 void
531 thread_terminate_enqueue(
532 thread_t thread)
533 {
534 simple_lock(&thread_terminate_lock);
535 enqueue_tail(&thread_terminate_queue, (queue_entry_t)thread);
536 simple_unlock(&thread_terminate_lock);
537
538 thread_wakeup((event_t)&thread_terminate_queue);
539 }
540
541 /*
542 * thread_stack_daemon:
543 *
544 * Perform stack allocation as required due to
545 * invoke failures.
546 */
547 static void
548 thread_stack_daemon(void)
549 {
550 thread_t thread;
551
552 simple_lock(&thread_stack_lock);
553
554 while ((thread = (thread_t)dequeue_head(&thread_stack_queue)) != THREAD_NULL) {
555 simple_unlock(&thread_stack_lock);
556
557 stack_alloc(thread);
558
559 (void)splsched();
560 thread_lock(thread);
561 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
562 thread_unlock(thread);
563 (void)spllo();
564
565 simple_lock(&thread_stack_lock);
566 }
567
568 assert_wait((event_t)&thread_stack_queue, THREAD_UNINT);
569 simple_unlock(&thread_stack_lock);
570
571 thread_block((thread_continue_t)thread_stack_daemon);
572 /*NOTREACHED*/
573 }
574
575 /*
576 * thread_stack_enqueue:
577 *
578 * Enqueue a thread for stack allocation.
579 *
580 * Called at splsched.
581 */
582 void
583 thread_stack_enqueue(
584 thread_t thread)
585 {
586 simple_lock(&thread_stack_lock);
587 enqueue_tail(&thread_stack_queue, (queue_entry_t)thread);
588 simple_unlock(&thread_stack_lock);
589
590 thread_wakeup((event_t)&thread_stack_queue);
591 }
592
593 void
594 thread_daemon_init(void)
595 {
596 kern_return_t result;
597 thread_t thread = NULL;
598
599 simple_lock_init(&thread_terminate_lock, 0);
600 queue_init(&thread_terminate_queue);
601
602 result = kernel_thread_start_priority((thread_continue_t)thread_terminate_daemon, NULL, MINPRI_KERNEL, &thread);
603 if (result != KERN_SUCCESS)
604 panic("thread_daemon_init: thread_terminate_daemon");
605
606 thread_deallocate(thread);
607
608 simple_lock_init(&thread_stack_lock, 0);
609 queue_init(&thread_stack_queue);
610
611 result = kernel_thread_start_priority((thread_continue_t)thread_stack_daemon, NULL, BASEPRI_PREEMPT, &thread);
612 if (result != KERN_SUCCESS)
613 panic("thread_daemon_init: thread_stack_daemon");
614
615 thread_deallocate(thread);
616 }
617
618 /*
619 * Create a new thread.
620 * Doesn't start the thread running.
621 */
622 static kern_return_t
623 thread_create_internal(
624 task_t parent_task,
625 integer_t priority,
626 thread_continue_t continuation,
627 int options,
628 #define TH_OPTION_NONE 0x00
629 #define TH_OPTION_NOCRED 0x01
630 #define TH_OPTION_NOSUSP 0x02
631 thread_t *out_thread)
632 {
633 thread_t new_thread;
634 static thread_t first_thread;
635
636 /*
637 * Allocate a thread and initialize static fields
638 */
639 if (first_thread == THREAD_NULL)
640 new_thread = first_thread = current_thread();
641 else
642 new_thread = (thread_t)zalloc(thread_zone);
643 if (new_thread == THREAD_NULL)
644 return (KERN_RESOURCE_SHORTAGE);
645
646 if (new_thread != first_thread)
647 *new_thread = thread_template;
648
649 #ifdef MACH_BSD
650 new_thread->uthread = uthread_alloc(parent_task, new_thread, (options & TH_OPTION_NOCRED) != 0);
651 if (new_thread->uthread == NULL) {
652 zfree(thread_zone, new_thread);
653 return (KERN_RESOURCE_SHORTAGE);
654 }
655 #endif /* MACH_BSD */
656
657 if (machine_thread_create(new_thread, parent_task) != KERN_SUCCESS) {
658 #ifdef MACH_BSD
659 void *ut = new_thread->uthread;
660
661 new_thread->uthread = NULL;
662 /* cred free may not be necessary */
663 uthread_cleanup(parent_task, ut, parent_task->bsd_info);
664 uthread_cred_free(ut);
665 uthread_zone_free(ut);
666 #endif /* MACH_BSD */
667
668 zfree(thread_zone, new_thread);
669 return (KERN_FAILURE);
670 }
671
672 new_thread->task = parent_task;
673
674 thread_lock_init(new_thread);
675 wake_lock_init(new_thread);
676
677 lck_mtx_init(&new_thread->mutex, &thread_lck_grp, &thread_lck_attr);
678
679 ipc_thread_init(new_thread);
680 queue_init(&new_thread->held_ulocks);
681
682 new_thread->continuation = continuation;
683
684 lck_mtx_lock(&tasks_threads_lock);
685 task_lock(parent_task);
686
687 if ( !parent_task->active || parent_task->halting ||
688 ((options & TH_OPTION_NOSUSP) != 0 &&
689 parent_task->suspend_count > 0) ||
690 (parent_task->thread_count >= task_threadmax &&
691 parent_task != kernel_task) ) {
692 task_unlock(parent_task);
693 lck_mtx_unlock(&tasks_threads_lock);
694
695 #ifdef MACH_BSD
696 {
697 void *ut = new_thread->uthread;
698
699 new_thread->uthread = NULL;
700 uthread_cleanup(parent_task, ut, parent_task->bsd_info);
701 /* cred free may not be necessary */
702 uthread_cred_free(ut);
703 uthread_zone_free(ut);
704 }
705 #endif /* MACH_BSD */
706 ipc_thread_disable(new_thread);
707 ipc_thread_terminate(new_thread);
708 lck_mtx_destroy(&new_thread->mutex, &thread_lck_grp);
709 machine_thread_destroy(new_thread);
710 zfree(thread_zone, new_thread);
711 return (KERN_FAILURE);
712 }
713
714 /* New threads inherit any default state on the task */
715 machine_thread_inherit_taskwide(new_thread, parent_task);
716
717 task_reference_internal(parent_task);
718
719 /* Cache the task's map */
720 new_thread->map = parent_task->map;
721
722 /* Chain the thread onto the task's list */
723 queue_enter(&parent_task->threads, new_thread, thread_t, task_threads);
724 parent_task->thread_count++;
725
726 /* So terminating threads don't need to take the task lock to decrement */
727 hw_atomic_add(&parent_task->active_thread_count, 1);
728
729 /* Protected by the tasks_threads_lock */
730 new_thread->thread_id = ++thread_unique_id;
731
732 queue_enter(&threads, new_thread, thread_t, threads);
733 threads_count++;
734
735 timer_call_setup(&new_thread->wait_timer, thread_timer_expire, new_thread);
736 timer_call_setup(&new_thread->depress_timer, thread_depress_expire, new_thread);
737
738 #if CONFIG_COUNTERS
739 /*
740 * If parent task has any reservations, they need to be propagated to this
741 * thread.
742 */
743 new_thread->t_chud = (TASK_PMC_FLAG == (parent_task->t_chud & TASK_PMC_FLAG)) ?
744 THREAD_PMC_FLAG : 0U;
745 #endif
746
747 /* Set the thread's scheduling parameters */
748 new_thread->sched_mode = SCHED(initial_thread_sched_mode)(parent_task);
749 new_thread->sched_flags = 0;
750 new_thread->max_priority = parent_task->max_priority;
751 new_thread->task_priority = parent_task->priority;
752 new_thread->priority = (priority < 0)? parent_task->priority: priority;
753 if (new_thread->priority > new_thread->max_priority)
754 new_thread->priority = new_thread->max_priority;
755 #if CONFIG_EMBEDDED
756 if (new_thread->priority < MAXPRI_THROTTLE) {
757 new_thread->priority = MAXPRI_THROTTLE;
758 }
759 #endif /* CONFIG_EMBEDDED */
760 new_thread->importance =
761 new_thread->priority - new_thread->task_priority;
762 #if defined(CONFIG_SCHED_TRADITIONAL)
763 new_thread->sched_stamp = sched_tick;
764 new_thread->pri_shift = sched_pri_shift;
765 #endif
766 SCHED(compute_priority)(new_thread, FALSE);
767
768 new_thread->active = TRUE;
769
770 *out_thread = new_thread;
771
772 {
773 long dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4;
774
775 kdbg_trace_data(parent_task->bsd_info, &dbg_arg2);
776
777 KERNEL_DEBUG_CONSTANT(
778 TRACEDBG_CODE(DBG_TRACE_DATA, 1) | DBG_FUNC_NONE,
779 (vm_address_t)(uintptr_t)thread_tid(new_thread), dbg_arg2, 0, 0, 0);
780
781 kdbg_trace_string(parent_task->bsd_info,
782 &dbg_arg1, &dbg_arg2, &dbg_arg3, &dbg_arg4);
783
784 KERNEL_DEBUG_CONSTANT(
785 TRACEDBG_CODE(DBG_TRACE_STRING, 1) | DBG_FUNC_NONE,
786 dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, 0);
787 }
788
789 DTRACE_PROC1(lwp__create, thread_t, *out_thread);
790
791 return (KERN_SUCCESS);
792 }
793
794 static kern_return_t
795 thread_create_internal2(
796 task_t task,
797 thread_t *new_thread,
798 boolean_t from_user)
799 {
800 kern_return_t result;
801 thread_t thread;
802
803 if (task == TASK_NULL || task == kernel_task)
804 return (KERN_INVALID_ARGUMENT);
805
806 result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
807 if (result != KERN_SUCCESS)
808 return (result);
809
810 thread->user_stop_count = 1;
811 thread_hold(thread);
812 if (task->suspend_count > 0)
813 thread_hold(thread);
814
815 if (from_user)
816 extmod_statistics_incr_thread_create(task);
817
818 task_unlock(task);
819 lck_mtx_unlock(&tasks_threads_lock);
820
821 *new_thread = thread;
822
823 return (KERN_SUCCESS);
824 }
825
826 /* No prototype, since task_server.h has the _from_user version if KERNEL_SERVER */
827 kern_return_t
828 thread_create(
829 task_t task,
830 thread_t *new_thread);
831
832 kern_return_t
833 thread_create(
834 task_t task,
835 thread_t *new_thread)
836 {
837 return thread_create_internal2(task, new_thread, FALSE);
838 }
839
840 kern_return_t
841 thread_create_from_user(
842 task_t task,
843 thread_t *new_thread)
844 {
845 return thread_create_internal2(task, new_thread, TRUE);
846 }
847
848 static kern_return_t
849 thread_create_running_internal2(
850 register task_t task,
851 int flavor,
852 thread_state_t new_state,
853 mach_msg_type_number_t new_state_count,
854 thread_t *new_thread,
855 boolean_t from_user)
856 {
857 register kern_return_t result;
858 thread_t thread;
859
860 if (task == TASK_NULL || task == kernel_task)
861 return (KERN_INVALID_ARGUMENT);
862
863 result = thread_create_internal(task, -1, (thread_continue_t)thread_bootstrap_return, TH_OPTION_NONE, &thread);
864 if (result != KERN_SUCCESS)
865 return (result);
866
867 result = machine_thread_set_state(
868 thread, flavor, new_state, new_state_count);
869 if (result != KERN_SUCCESS) {
870 task_unlock(task);
871 lck_mtx_unlock(&tasks_threads_lock);
872
873 thread_terminate(thread);
874 thread_deallocate(thread);
875 return (result);
876 }
877
878 thread_mtx_lock(thread);
879 thread_start_internal(thread);
880 thread_mtx_unlock(thread);
881
882 if (from_user)
883 extmod_statistics_incr_thread_create(task);
884
885 task_unlock(task);
886 lck_mtx_unlock(&tasks_threads_lock);
887
888 *new_thread = thread;
889
890 return (result);
891 }
892
893 /* Prototype, see justification above */
894 kern_return_t
895 thread_create_running(
896 register task_t task,
897 int flavor,
898 thread_state_t new_state,
899 mach_msg_type_number_t new_state_count,
900 thread_t *new_thread);
901
902 kern_return_t
903 thread_create_running(
904 register task_t task,
905 int flavor,
906 thread_state_t new_state,
907 mach_msg_type_number_t new_state_count,
908 thread_t *new_thread)
909 {
910 return thread_create_running_internal2(
911 task, flavor, new_state, new_state_count,
912 new_thread, FALSE);
913 }
914
915 kern_return_t
916 thread_create_running_from_user(
917 register task_t task,
918 int flavor,
919 thread_state_t new_state,
920 mach_msg_type_number_t new_state_count,
921 thread_t *new_thread)
922 {
923 return thread_create_running_internal2(
924 task, flavor, new_state, new_state_count,
925 new_thread, TRUE);
926 }
927
928 kern_return_t
929 thread_create_workq(
930 task_t task,
931 thread_continue_t thread_return,
932 thread_t *new_thread)
933 {
934 kern_return_t result;
935 thread_t thread;
936
937 if (task == TASK_NULL || task == kernel_task)
938 return (KERN_INVALID_ARGUMENT);
939
940 result = thread_create_internal(task, -1, thread_return, TH_OPTION_NOCRED | TH_OPTION_NOSUSP, &thread);
941 if (result != KERN_SUCCESS)
942 return (result);
943
944 thread->user_stop_count = 1;
945 thread_hold(thread);
946 if (task->suspend_count > 0)
947 thread_hold(thread);
948
949 task_unlock(task);
950 lck_mtx_unlock(&tasks_threads_lock);
951
952 *new_thread = thread;
953
954 return (KERN_SUCCESS);
955 }
956
957 /*
958 * kernel_thread_create:
959 *
960 * Create a thread in the kernel task
961 * to execute in kernel context.
962 */
963 kern_return_t
964 kernel_thread_create(
965 thread_continue_t continuation,
966 void *parameter,
967 integer_t priority,
968 thread_t *new_thread)
969 {
970 kern_return_t result;
971 thread_t thread;
972 task_t task = kernel_task;
973
974 result = thread_create_internal(task, priority, continuation, TH_OPTION_NONE, &thread);
975 if (result != KERN_SUCCESS)
976 return (result);
977
978 task_unlock(task);
979 lck_mtx_unlock(&tasks_threads_lock);
980
981 stack_alloc(thread);
982 assert(thread->kernel_stack != 0);
983 #if CONFIG_EMBEDDED
984 if (priority > BASEPRI_KERNEL)
985 #endif
986 thread->reserved_stack = thread->kernel_stack;
987
988 thread->parameter = parameter;
989
990 if(debug_task & 1)
991 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread, continuation);
992 *new_thread = thread;
993
994 return (result);
995 }
996
997 kern_return_t
998 kernel_thread_start_priority(
999 thread_continue_t continuation,
1000 void *parameter,
1001 integer_t priority,
1002 thread_t *new_thread)
1003 {
1004 kern_return_t result;
1005 thread_t thread;
1006
1007 result = kernel_thread_create(continuation, parameter, priority, &thread);
1008 if (result != KERN_SUCCESS)
1009 return (result);
1010
1011 *new_thread = thread;
1012
1013 thread_mtx_lock(thread);
1014 thread_start_internal(thread);
1015 thread_mtx_unlock(thread);
1016
1017 return (result);
1018 }
1019
1020 kern_return_t
1021 kernel_thread_start(
1022 thread_continue_t continuation,
1023 void *parameter,
1024 thread_t *new_thread)
1025 {
1026 return kernel_thread_start_priority(continuation, parameter, -1, new_thread);
1027 }
1028
1029 #ifndef __LP64__
1030
1031 thread_t
1032 kernel_thread(
1033 task_t task,
1034 void (*start)(void))
1035 {
1036 kern_return_t result;
1037 thread_t thread;
1038
1039 if (task != kernel_task)
1040 panic("kernel_thread");
1041
1042 result = kernel_thread_start_priority((thread_continue_t)start, NULL, -1, &thread);
1043 if (result != KERN_SUCCESS)
1044 return (THREAD_NULL);
1045
1046 thread_deallocate(thread);
1047
1048 return (thread);
1049 }
1050
1051 #endif /* __LP64__ */
1052
1053 kern_return_t
1054 thread_info_internal(
1055 register thread_t thread,
1056 thread_flavor_t flavor,
1057 thread_info_t thread_info_out, /* ptr to OUT array */
1058 mach_msg_type_number_t *thread_info_count) /*IN/OUT*/
1059 {
1060 int state, flags;
1061 spl_t s;
1062
1063 if (thread == THREAD_NULL)
1064 return (KERN_INVALID_ARGUMENT);
1065
1066 if (flavor == THREAD_BASIC_INFO) {
1067 register thread_basic_info_t basic_info;
1068
1069 if (*thread_info_count < THREAD_BASIC_INFO_COUNT)
1070 return (KERN_INVALID_ARGUMENT);
1071
1072 basic_info = (thread_basic_info_t) thread_info_out;
1073
1074 s = splsched();
1075 thread_lock(thread);
1076
1077 /* fill in info */
1078
1079 thread_read_times(thread, &basic_info->user_time,
1080 &basic_info->system_time);
1081
1082 /*
1083 * Update lazy-evaluated scheduler info because someone wants it.
1084 */
1085 if (SCHED(can_update_priority)(thread))
1086 SCHED(update_priority)(thread);
1087
1088 basic_info->sleep_time = 0;
1089
1090 /*
1091 * To calculate cpu_usage, first correct for timer rate,
1092 * then for 5/8 ageing. The correction factor [3/5] is
1093 * (1/(5/8) - 1).
1094 */
1095 basic_info->cpu_usage = 0;
1096 #if defined(CONFIG_SCHED_TRADITIONAL)
1097 if (sched_tick_interval) {
1098 basic_info->cpu_usage = (integer_t)(((uint64_t)thread->cpu_usage
1099 * TH_USAGE_SCALE) / sched_tick_interval);
1100 basic_info->cpu_usage = (basic_info->cpu_usage * 3) / 5;
1101 }
1102 #endif
1103
1104 if (basic_info->cpu_usage > TH_USAGE_SCALE)
1105 basic_info->cpu_usage = TH_USAGE_SCALE;
1106
1107 basic_info->policy = ((thread->sched_mode == TH_MODE_TIMESHARE)?
1108 POLICY_TIMESHARE: POLICY_RR);
1109
1110 flags = 0;
1111 if (thread->bound_processor != PROCESSOR_NULL && thread->bound_processor->idle_thread == thread)
1112 flags |= TH_FLAGS_IDLE;
1113
1114 if (!thread->kernel_stack)
1115 flags |= TH_FLAGS_SWAPPED;
1116
1117 state = 0;
1118 if (thread->state & TH_TERMINATE)
1119 state = TH_STATE_HALTED;
1120 else
1121 if (thread->state & TH_RUN)
1122 state = TH_STATE_RUNNING;
1123 else
1124 if (thread->state & TH_UNINT)
1125 state = TH_STATE_UNINTERRUPTIBLE;
1126 else
1127 if (thread->state & TH_SUSP)
1128 state = TH_STATE_STOPPED;
1129 else
1130 if (thread->state & TH_WAIT)
1131 state = TH_STATE_WAITING;
1132
1133 basic_info->run_state = state;
1134 basic_info->flags = flags;
1135
1136 basic_info->suspend_count = thread->user_stop_count;
1137
1138 thread_unlock(thread);
1139 splx(s);
1140
1141 *thread_info_count = THREAD_BASIC_INFO_COUNT;
1142
1143 return (KERN_SUCCESS);
1144 }
1145 else
1146 if (flavor == THREAD_IDENTIFIER_INFO) {
1147 register thread_identifier_info_t identifier_info;
1148
1149 if (*thread_info_count < THREAD_IDENTIFIER_INFO_COUNT)
1150 return (KERN_INVALID_ARGUMENT);
1151
1152 identifier_info = (thread_identifier_info_t) thread_info_out;
1153
1154 s = splsched();
1155 thread_lock(thread);
1156
1157 identifier_info->thread_id = thread->thread_id;
1158 identifier_info->thread_handle = thread->machine.cthread_self;
1159 if(thread->task->bsd_info) {
1160 identifier_info->dispatch_qaddr = identifier_info->thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
1161 } else {
1162 thread_unlock(thread);
1163 splx(s);
1164 return KERN_INVALID_ARGUMENT;
1165 }
1166
1167 thread_unlock(thread);
1168 splx(s);
1169 return KERN_SUCCESS;
1170 }
1171 else
1172 if (flavor == THREAD_SCHED_TIMESHARE_INFO) {
1173 policy_timeshare_info_t ts_info;
1174
1175 if (*thread_info_count < POLICY_TIMESHARE_INFO_COUNT)
1176 return (KERN_INVALID_ARGUMENT);
1177
1178 ts_info = (policy_timeshare_info_t)thread_info_out;
1179
1180 s = splsched();
1181 thread_lock(thread);
1182
1183 if (thread->sched_mode != TH_MODE_TIMESHARE) {
1184 thread_unlock(thread);
1185 splx(s);
1186
1187 return (KERN_INVALID_POLICY);
1188 }
1189
1190 ts_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
1191 if (ts_info->depressed) {
1192 ts_info->base_priority = DEPRESSPRI;
1193 ts_info->depress_priority = thread->priority;
1194 }
1195 else {
1196 ts_info->base_priority = thread->priority;
1197 ts_info->depress_priority = -1;
1198 }
1199
1200 ts_info->cur_priority = thread->sched_pri;
1201 ts_info->max_priority = thread->max_priority;
1202
1203 thread_unlock(thread);
1204 splx(s);
1205
1206 *thread_info_count = POLICY_TIMESHARE_INFO_COUNT;
1207
1208 return (KERN_SUCCESS);
1209 }
1210 else
1211 if (flavor == THREAD_SCHED_FIFO_INFO) {
1212 if (*thread_info_count < POLICY_FIFO_INFO_COUNT)
1213 return (KERN_INVALID_ARGUMENT);
1214
1215 return (KERN_INVALID_POLICY);
1216 }
1217 else
1218 if (flavor == THREAD_SCHED_RR_INFO) {
1219 policy_rr_info_t rr_info;
1220 uint32_t quantum_time;
1221 uint64_t quantum_ns;
1222
1223 if (*thread_info_count < POLICY_RR_INFO_COUNT)
1224 return (KERN_INVALID_ARGUMENT);
1225
1226 rr_info = (policy_rr_info_t) thread_info_out;
1227
1228 s = splsched();
1229 thread_lock(thread);
1230
1231 if (thread->sched_mode == TH_MODE_TIMESHARE) {
1232 thread_unlock(thread);
1233 splx(s);
1234
1235 return (KERN_INVALID_POLICY);
1236 }
1237
1238 rr_info->depressed = (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != 0;
1239 if (rr_info->depressed) {
1240 rr_info->base_priority = DEPRESSPRI;
1241 rr_info->depress_priority = thread->priority;
1242 }
1243 else {
1244 rr_info->base_priority = thread->priority;
1245 rr_info->depress_priority = -1;
1246 }
1247
1248 quantum_time = SCHED(initial_quantum_size)(THREAD_NULL);
1249 absolutetime_to_nanoseconds(quantum_time, &quantum_ns);
1250
1251 rr_info->max_priority = thread->max_priority;
1252 rr_info->quantum = (uint32_t)(quantum_ns / 1000 / 1000);
1253
1254 thread_unlock(thread);
1255 splx(s);
1256
1257 *thread_info_count = POLICY_RR_INFO_COUNT;
1258
1259 return (KERN_SUCCESS);
1260 }
1261
1262 return (KERN_INVALID_ARGUMENT);
1263 }
1264
1265 void
1266 thread_read_times(
1267 thread_t thread,
1268 time_value_t *user_time,
1269 time_value_t *system_time)
1270 {
1271 clock_sec_t secs;
1272 clock_usec_t usecs;
1273
1274 absolutetime_to_microtime(timer_grab(&thread->user_timer), &secs, &usecs);
1275 user_time->seconds = (typeof(user_time->seconds))secs;
1276 user_time->microseconds = usecs;
1277
1278 absolutetime_to_microtime(timer_grab(&thread->system_timer), &secs, &usecs);
1279 system_time->seconds = (typeof(system_time->seconds))secs;
1280 system_time->microseconds = usecs;
1281 }
1282
1283 kern_return_t
1284 thread_assign(
1285 __unused thread_t thread,
1286 __unused processor_set_t new_pset)
1287 {
1288 return (KERN_FAILURE);
1289 }
1290
1291 /*
1292 * thread_assign_default:
1293 *
1294 * Special version of thread_assign for assigning threads to default
1295 * processor set.
1296 */
1297 kern_return_t
1298 thread_assign_default(
1299 thread_t thread)
1300 {
1301 return (thread_assign(thread, &pset0));
1302 }
1303
1304 /*
1305 * thread_get_assignment
1306 *
1307 * Return current assignment for this thread.
1308 */
1309 kern_return_t
1310 thread_get_assignment(
1311 thread_t thread,
1312 processor_set_t *pset)
1313 {
1314 if (thread == NULL)
1315 return (KERN_INVALID_ARGUMENT);
1316
1317 *pset = &pset0;
1318
1319 return (KERN_SUCCESS);
1320 }
1321
1322 /*
1323 * thread_wire_internal:
1324 *
1325 * Specify that the target thread must always be able
1326 * to run and to allocate memory.
1327 */
1328 kern_return_t
1329 thread_wire_internal(
1330 host_priv_t host_priv,
1331 thread_t thread,
1332 boolean_t wired,
1333 boolean_t *prev_state)
1334 {
1335 if (host_priv == NULL || thread != current_thread())
1336 return (KERN_INVALID_ARGUMENT);
1337
1338 assert(host_priv == &realhost);
1339
1340 if (prev_state)
1341 *prev_state = (thread->options & TH_OPT_VMPRIV) != 0;
1342
1343 if (wired) {
1344 if (!(thread->options & TH_OPT_VMPRIV))
1345 vm_page_free_reserve(1); /* XXX */
1346 thread->options |= TH_OPT_VMPRIV;
1347 }
1348 else {
1349 if (thread->options & TH_OPT_VMPRIV)
1350 vm_page_free_reserve(-1); /* XXX */
1351 thread->options &= ~TH_OPT_VMPRIV;
1352 }
1353
1354 return (KERN_SUCCESS);
1355 }
1356
1357
1358 /*
1359 * thread_wire:
1360 *
1361 * User-api wrapper for thread_wire_internal()
1362 */
1363 kern_return_t
1364 thread_wire(
1365 host_priv_t host_priv,
1366 thread_t thread,
1367 boolean_t wired)
1368 {
1369 return (thread_wire_internal(host_priv, thread, wired, NULL));
1370 }
1371
1372 int split_funnel_off = 0;
1373 lck_grp_t *funnel_lck_grp = LCK_GRP_NULL;
1374 lck_grp_attr_t *funnel_lck_grp_attr;
1375 lck_attr_t *funnel_lck_attr;
1376
1377 funnel_t *
1378 funnel_alloc(
1379 int type)
1380 {
1381 lck_mtx_t *m;
1382 funnel_t *fnl;
1383
1384 if (funnel_lck_grp == LCK_GRP_NULL) {
1385 funnel_lck_grp_attr = lck_grp_attr_alloc_init();
1386
1387 funnel_lck_grp = lck_grp_alloc_init("Funnel", funnel_lck_grp_attr);
1388
1389 funnel_lck_attr = lck_attr_alloc_init();
1390 }
1391 if ((fnl = (funnel_t *)kalloc(sizeof(funnel_t))) != 0){
1392 bzero((void *)fnl, sizeof(funnel_t));
1393 if ((m = lck_mtx_alloc_init(funnel_lck_grp, funnel_lck_attr)) == (lck_mtx_t *)NULL) {
1394 kfree(fnl, sizeof(funnel_t));
1395 return(THR_FUNNEL_NULL);
1396 }
1397 fnl->fnl_mutex = m;
1398 fnl->fnl_type = type;
1399 }
1400 return(fnl);
1401 }
1402
1403 void
1404 funnel_free(
1405 funnel_t * fnl)
1406 {
1407 lck_mtx_free(fnl->fnl_mutex, funnel_lck_grp);
1408 if (fnl->fnl_oldmutex)
1409 lck_mtx_free(fnl->fnl_oldmutex, funnel_lck_grp);
1410 kfree(fnl, sizeof(funnel_t));
1411 }
1412
1413 void
1414 funnel_lock(
1415 funnel_t * fnl)
1416 {
1417 lck_mtx_lock(fnl->fnl_mutex);
1418 fnl->fnl_mtxholder = current_thread();
1419 }
1420
1421 void
1422 funnel_unlock(
1423 funnel_t * fnl)
1424 {
1425 lck_mtx_unlock(fnl->fnl_mutex);
1426 fnl->fnl_mtxholder = NULL;
1427 fnl->fnl_mtxrelease = current_thread();
1428 }
1429
1430 funnel_t *
1431 thread_funnel_get(
1432 void)
1433 {
1434 thread_t th = current_thread();
1435
1436 if (th->funnel_state & TH_FN_OWNED) {
1437 return(th->funnel_lock);
1438 }
1439 return(THR_FUNNEL_NULL);
1440 }
1441
1442 boolean_t
1443 thread_funnel_set(
1444 funnel_t * fnl,
1445 boolean_t funneled)
1446 {
1447 thread_t cur_thread;
1448 boolean_t funnel_state_prev;
1449 boolean_t intr;
1450
1451 cur_thread = current_thread();
1452 funnel_state_prev = ((cur_thread->funnel_state & TH_FN_OWNED) == TH_FN_OWNED);
1453
1454 if (funnel_state_prev != funneled) {
1455 intr = ml_set_interrupts_enabled(FALSE);
1456
1457 if (funneled == TRUE) {
1458 if (cur_thread->funnel_lock)
1459 panic("Funnel lock called when holding one %p", cur_thread->funnel_lock);
1460 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE,
1461 fnl, 1, 0, 0, 0);
1462 funnel_lock(fnl);
1463 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE,
1464 fnl, 1, 0, 0, 0);
1465 cur_thread->funnel_state |= TH_FN_OWNED;
1466 cur_thread->funnel_lock = fnl;
1467 } else {
1468 if(cur_thread->funnel_lock->fnl_mutex != fnl->fnl_mutex)
1469 panic("Funnel unlock when not holding funnel");
1470 cur_thread->funnel_state &= ~TH_FN_OWNED;
1471 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE,
1472 fnl, 1, 0, 0, 0);
1473
1474 cur_thread->funnel_lock = THR_FUNNEL_NULL;
1475 funnel_unlock(fnl);
1476 }
1477 (void)ml_set_interrupts_enabled(intr);
1478 } else {
1479 /* if we are trying to acquire funnel recursively
1480 * check for funnel to be held already
1481 */
1482 if (funneled && (fnl->fnl_mutex != cur_thread->funnel_lock->fnl_mutex)) {
1483 panic("thread_funnel_set: already holding a different funnel");
1484 }
1485 }
1486 return(funnel_state_prev);
1487 }
1488
1489 static void
1490 sched_call_null(
1491 __unused int type,
1492 __unused thread_t thread)
1493 {
1494 return;
1495 }
1496
1497 void
1498 thread_sched_call(
1499 thread_t thread,
1500 sched_call_t call)
1501 {
1502 thread->sched_call = (call != NULL)? call: sched_call_null;
1503 }
1504
1505 void
1506 thread_static_param(
1507 thread_t thread,
1508 boolean_t state)
1509 {
1510 thread_mtx_lock(thread);
1511 thread->static_param = state;
1512 thread_mtx_unlock(thread);
1513 }
1514
1515 uint64_t
1516 thread_tid(
1517 thread_t thread)
1518 {
1519 return (thread != THREAD_NULL? thread->thread_id: 0);
1520 }
1521
1522 uint64_t
1523 thread_dispatchqaddr(
1524 thread_t thread)
1525 {
1526 uint64_t dispatchqueue_addr = 0;
1527 uint64_t thread_handle = 0;
1528
1529 if (thread != THREAD_NULL) {
1530 thread_handle = thread->machine.cthread_self;
1531
1532 if (thread->task->bsd_info)
1533 dispatchqueue_addr = thread_handle + get_dispatchqueue_offset_from_proc(thread->task->bsd_info);
1534 }
1535
1536 return (dispatchqueue_addr);
1537 }
1538
1539 /*
1540 * Export routines to other components for things that are done as macros
1541 * within the osfmk component.
1542 */
1543
1544 #undef thread_reference
1545 void thread_reference(thread_t thread);
1546 void
1547 thread_reference(
1548 thread_t thread)
1549 {
1550 if (thread != THREAD_NULL)
1551 thread_reference_internal(thread);
1552 }
1553
1554 #undef thread_should_halt
1555
1556 boolean_t
1557 thread_should_halt(
1558 thread_t th)
1559 {
1560 return (thread_should_halt_fast(th));
1561 }
1562
1563 #if CONFIG_DTRACE
1564 uint32_t dtrace_get_thread_predcache(thread_t thread)
1565 {
1566 if (thread != THREAD_NULL)
1567 return thread->t_dtrace_predcache;
1568 else
1569 return 0;
1570 }
1571
1572 int64_t dtrace_get_thread_vtime(thread_t thread)
1573 {
1574 if (thread != THREAD_NULL)
1575 return thread->t_dtrace_vtime;
1576 else
1577 return 0;
1578 }
1579
1580 int64_t dtrace_get_thread_tracing(thread_t thread)
1581 {
1582 if (thread != THREAD_NULL)
1583 return thread->t_dtrace_tracing;
1584 else
1585 return 0;
1586 }
1587
1588 boolean_t dtrace_get_thread_reentering(thread_t thread)
1589 {
1590 if (thread != THREAD_NULL)
1591 return (thread->options & TH_OPT_DTRACE) ? TRUE : FALSE;
1592 else
1593 return 0;
1594 }
1595
1596 vm_offset_t dtrace_get_kernel_stack(thread_t thread)
1597 {
1598 if (thread != THREAD_NULL)
1599 return thread->kernel_stack;
1600 else
1601 return 0;
1602 }
1603
1604 int64_t dtrace_calc_thread_recent_vtime(thread_t thread)
1605 {
1606 #if STAT_TIME
1607 if (thread != THREAD_NULL) {
1608 return timer_grab(&(thread->system_timer)) + timer_grab(&(thread->user_timer));
1609 } else
1610 return 0;
1611 #else
1612 if (thread != THREAD_NULL) {
1613 processor_t processor = current_processor();
1614 uint64_t abstime = mach_absolute_time();
1615 timer_t timer;
1616
1617 timer = PROCESSOR_DATA(processor, thread_timer);
1618
1619 return timer_grab(&(thread->system_timer)) + timer_grab(&(thread->user_timer)) +
1620 (abstime - timer->tstamp); /* XXX need interrupts off to prevent missed time? */
1621 } else
1622 return 0;
1623 #endif
1624 }
1625
1626 void dtrace_set_thread_predcache(thread_t thread, uint32_t predcache)
1627 {
1628 if (thread != THREAD_NULL)
1629 thread->t_dtrace_predcache = predcache;
1630 }
1631
1632 void dtrace_set_thread_vtime(thread_t thread, int64_t vtime)
1633 {
1634 if (thread != THREAD_NULL)
1635 thread->t_dtrace_vtime = vtime;
1636 }
1637
1638 void dtrace_set_thread_tracing(thread_t thread, int64_t accum)
1639 {
1640 if (thread != THREAD_NULL)
1641 thread->t_dtrace_tracing = accum;
1642 }
1643
1644 void dtrace_set_thread_reentering(thread_t thread, boolean_t vbool)
1645 {
1646 if (thread != THREAD_NULL) {
1647 if (vbool)
1648 thread->options |= TH_OPT_DTRACE;
1649 else
1650 thread->options &= (~TH_OPT_DTRACE);
1651 }
1652 }
1653
1654 vm_offset_t dtrace_set_thread_recover(thread_t thread, vm_offset_t recover)
1655 {
1656 vm_offset_t prev = 0;
1657
1658 if (thread != THREAD_NULL) {
1659 prev = thread->recover;
1660 thread->recover = recover;
1661 }
1662 return prev;
1663 }
1664
1665 void dtrace_thread_bootstrap(void)
1666 {
1667 task_t task = current_task();
1668 if(task->thread_count == 1) {
1669 DTRACE_PROC(start);
1670 }
1671 DTRACE_PROC(lwp__start);
1672
1673 }
1674 #endif /* CONFIG_DTRACE */