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