2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * @OSF_FREE_COPYRIGHT@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub
63 * Thread management primitives implementation.
66 * Copyright (c) 1993 The University of Utah and
67 * the Computer Systems Laboratory (CSL). All rights reserved.
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.
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.
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.
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>
93 #include <machine/thread.h>
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>
116 #include <ipc/ipc_kmsg.h>
117 #include <ipc/ipc_port.h>
119 #include <vm/vm_kern.h>
120 #include <vm/vm_pageout.h>
122 #include <sys/kdebug.h>
124 #include <mach/sdt.h>
127 * Exported interfaces
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>
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
;
139 decl_simple_lock_data(static,thread_stack_lock
)
140 static queue_head_t thread_stack_queue
;
142 decl_simple_lock_data(static,thread_terminate_lock
)
143 static queue_head_t thread_terminate_queue
;
145 static struct thread thread_template
, init_thread
;
147 static void sched_call_null(
152 extern void proc_exit(void *);
153 extern uint64_t get_dispatchqueue_offset_from_proc(void *);
154 #endif /* MACH_BSD */
156 extern int debug_task
;
157 int thread_max
= CONFIG_THREAD_MAX
; /* Max number of threads */
158 int task_threadmax
= CONFIG_THREAD_MAX
;
160 static uint64_t thread_unique_id
= 0;
163 thread_bootstrap(void)
166 * Fill in a template thread for fast initialization.
169 thread_template
.runq
= PROCESSOR_NULL
;
171 thread_template
.ref_count
= 2;
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
;
184 thread_template
.importance
= 0;
185 thread_template
.sched_mode
= 0;
186 thread_template
.safe_mode
= 0;
187 thread_template
.safe_release
= 0;
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
;
198 thread_template
.realtime
.deadline
= UINT64_MAX
;
200 thread_template
.current_quantum
= 0;
202 thread_template
.computation_metered
= 0;
203 thread_template
.computation_epoch
= 0;
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;
211 thread_template
.bound_processor
= PROCESSOR_NULL
;
212 thread_template
.last_processor
= PROCESSOR_NULL
;
214 thread_template
.sched_call
= sched_call_null
;
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;
224 thread_template
.wait_timer_is_set
= FALSE
;
225 thread_template
.wait_timer_active
= 0;
227 thread_template
.depress_timer_active
= 0;
229 thread_template
.special_handler
.handler
= special_handler
;
230 thread_template
.special_handler
.next
= NULL
;
232 thread_template
.funnel_lock
= THR_FUNNEL_NULL
;
233 thread_template
.funnel_state
= 0;
234 thread_template
.recover
= (vm_offset_t
)NULL
;
236 thread_template
.map
= VM_MAP_NULL
;
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 */
244 thread_template
.t_chud
= 0;
245 thread_template
.t_page_creation_count
= 0;
246 thread_template
.t_page_creation_time
= 0;
248 thread_template
.affinity_set
= NULL
;
250 init_thread
= thread_template
;
251 machine_set_current_thread(&init_thread
);
258 sizeof(struct thread
),
259 thread_max
* sizeof(struct thread
),
260 THREAD_CHUNK
* sizeof(struct thread
),
262 zone_change(thread_zone
, Z_NOENCRYPT
, TRUE
);
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
);
271 * Initialize any machine-dependent
272 * per-thread structures necessary.
274 machine_thread_init();
278 thread_terminate_continue(void)
280 panic("thread_terminate_continue");
285 * thread_terminate_self:
288 thread_terminate_self(void)
290 thread_t thread
= current_thread();
295 DTRACE_PROC(lwp__exit
);
297 thread_mtx_lock(thread
);
299 ulock_release_all(thread
);
301 ipc_thread_disable(thread
);
303 thread_mtx_unlock(thread
);
309 * Cancel priority depression, wait for concurrent expirations
310 * on other processors.
312 if (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) {
313 thread
->sched_mode
&= ~TH_MODE_ISDEPRESSED
;
315 if (timer_call_cancel(&thread
->depress_timer
))
316 thread
->depress_timer_active
--;
319 while (thread
->depress_timer_active
> 0) {
320 thread_unlock(thread
);
329 thread_sched_call(thread
, NULL
);
331 thread_unlock(thread
);
334 thread_policy_reset(thread
);
337 uthread_cleanup(task
, thread
->uthread
, task
->bsd_info
);
338 threadcnt
= hw_atomic_sub(&task
->active_thread_count
, 1);
341 * If we are the last thread to terminate and the task is
342 * associated with a BSD process, perform BSD process exit.
344 if (threadcnt
== 0 && task
->bsd_info
!= NULL
)
345 proc_exit(task
->bsd_info
);
347 uthread_cred_free(thread
->uthread
);
353 * Cancel wait timer, and wait for
354 * concurrent expirations.
356 if (thread
->wait_timer_is_set
) {
357 thread
->wait_timer_is_set
= FALSE
;
359 if (timer_call_cancel(&thread
->wait_timer
))
360 thread
->wait_timer_active
--;
363 while (thread
->wait_timer_active
> 0) {
364 thread_unlock(thread
);
374 * If there is a reserved stack, release it.
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;
383 * Mark thread as terminating, and block.
385 thread
->state
|= TH_TERMINATE
;
386 thread_mark_wait_locked(thread
, THREAD_UNINT
);
387 assert(thread
->promotions
== 0);
388 thread_unlock(thread
);
391 thread_block((thread_continue_t
)thread_terminate_continue
);
401 if (thread
== THREAD_NULL
)
404 if (thread_deallocate_internal(thread
) > 0)
407 ipc_thread_terminate(thread
);
413 void *ut
= thread
->uthread
;
415 thread
->uthread
= NULL
;
416 uthread_zone_free(ut
);
418 #endif /* MACH_BSD */
420 task_deallocate(task
);
422 if (thread
->kernel_stack
!= 0)
425 lck_mtx_destroy(&thread
->mutex
, &thread_lck_grp
);
426 machine_thread_destroy(thread
);
428 zfree(thread_zone
, thread
);
432 * thread_terminate_daemon:
434 * Perform final clean up for terminating threads.
437 thread_terminate_daemon(void)
443 simple_lock(&thread_terminate_lock
);
445 while ((thread
= (thread_t
)dequeue_head(&thread_terminate_queue
)) != THREAD_NULL
) {
446 simple_unlock(&thread_terminate_lock
);
452 task
->total_user_time
+= timer_grab(&thread
->user_timer
);
453 task
->total_system_time
+= timer_grab(&thread
->system_timer
);
455 task
->c_switch
+= thread
->c_switch
;
456 task
->p_switch
+= thread
->p_switch
;
457 task
->ps_switch
+= thread
->ps_switch
;
459 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
460 task
->thread_count
--;
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.
466 if (task
->thread_count
== 1 && task
->halting
)
467 thread_wakeup((event_t
)&task
->halting
);
471 lck_mtx_lock(&tasks_threads_lock
);
472 queue_remove(&threads
, thread
, thread_t
, threads
);
474 lck_mtx_unlock(&tasks_threads_lock
);
476 thread_deallocate(thread
);
479 simple_lock(&thread_terminate_lock
);
482 assert_wait((event_t
)&thread_terminate_queue
, THREAD_UNINT
);
483 simple_unlock(&thread_terminate_lock
);
486 thread_block((thread_continue_t
)thread_terminate_daemon
);
491 * thread_terminate_enqueue:
493 * Enqueue a terminating thread for final disposition.
495 * Called at splsched.
498 thread_terminate_enqueue(
501 simple_lock(&thread_terminate_lock
);
502 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)thread
);
503 simple_unlock(&thread_terminate_lock
);
505 thread_wakeup((event_t
)&thread_terminate_queue
);
509 * thread_stack_daemon:
511 * Perform stack allocation as required due to
515 thread_stack_daemon(void)
519 simple_lock(&thread_stack_lock
);
521 while ((thread
= (thread_t
)dequeue_head(&thread_stack_queue
)) != THREAD_NULL
) {
522 simple_unlock(&thread_stack_lock
);
528 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
529 thread_unlock(thread
);
532 simple_lock(&thread_stack_lock
);
535 assert_wait((event_t
)&thread_stack_queue
, THREAD_UNINT
);
536 simple_unlock(&thread_stack_lock
);
538 thread_block((thread_continue_t
)thread_stack_daemon
);
543 * thread_stack_enqueue:
545 * Enqueue a thread for stack allocation.
547 * Called at splsched.
550 thread_stack_enqueue(
553 simple_lock(&thread_stack_lock
);
554 enqueue_tail(&thread_stack_queue
, (queue_entry_t
)thread
);
555 simple_unlock(&thread_stack_lock
);
557 thread_wakeup((event_t
)&thread_stack_queue
);
561 thread_daemon_init(void)
563 kern_return_t result
;
566 simple_lock_init(&thread_terminate_lock
, 0);
567 queue_init(&thread_terminate_queue
);
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");
573 thread_deallocate(thread
);
575 simple_lock_init(&thread_stack_lock
, 0);
576 queue_init(&thread_stack_queue
);
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");
582 thread_deallocate(thread
);
586 * Create a new thread.
587 * Doesn't start the thread running.
590 thread_create_internal(
593 thread_continue_t continuation
,
595 #define TH_OPTION_NONE 0x00
596 #define TH_OPTION_NOCRED 0x01
597 #define TH_OPTION_NOSUSP 0x02
598 thread_t
*out_thread
)
601 static thread_t first_thread
;
604 * Allocate a thread and initialize static fields
606 if (first_thread
== THREAD_NULL
)
607 new_thread
= first_thread
= current_thread();
609 new_thread
= (thread_t
)zalloc(thread_zone
);
610 if (new_thread
== THREAD_NULL
)
611 return (KERN_RESOURCE_SHORTAGE
);
613 if (new_thread
!= first_thread
)
614 *new_thread
= thread_template
;
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
);
622 #endif /* MACH_BSD */
624 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
626 void *ut
= new_thread
->uthread
;
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 */
635 zfree(thread_zone
, new_thread
);
636 return (KERN_FAILURE
);
639 new_thread
->task
= parent_task
;
641 thread_lock_init(new_thread
);
642 wake_lock_init(new_thread
);
644 lck_mtx_init(&new_thread
->mutex
, &thread_lck_grp
, &thread_lck_attr
);
646 ipc_thread_init(new_thread
);
647 queue_init(&new_thread
->held_ulocks
);
649 new_thread
->continuation
= continuation
;
651 lck_mtx_lock(&tasks_threads_lock
);
652 task_lock(parent_task
);
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
);
664 void *ut
= new_thread
->uthread
;
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
);
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
);
681 /* New threads inherit any default state on the task */
682 machine_thread_inherit_taskwide(new_thread
, parent_task
);
684 task_reference_internal(parent_task
);
686 /* Cache the task's map */
687 new_thread
->map
= parent_task
->map
;
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
++;
693 /* So terminating threads don't need to take the task lock to decrement */
694 hw_atomic_add(&parent_task
->active_thread_count
, 1);
696 /* Protected by the tasks_threads_lock */
697 new_thread
->thread_id
= ++thread_unique_id
;
699 queue_enter(&threads
, new_thread
, thread_t
, threads
);
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
);
707 * If parent task has any reservations, they need to be propagated to this
710 new_thread
->t_chud
= (TASK_PMC_FLAG
== (parent_task
->t_chud
& TASK_PMC_FLAG
)) ?
711 THREAD_PMC_FLAG
: 0U;
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
);
728 new_thread
->active
= TRUE
;
730 *out_thread
= new_thread
;
733 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
735 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
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);
741 kdbg_trace_string(parent_task
->bsd_info
,
742 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
744 KERNEL_DEBUG_CONSTANT(
745 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
746 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
749 DTRACE_PROC1(lwp__create
, thread_t
, *out_thread
);
751 return (KERN_SUCCESS
);
757 thread_t
*new_thread
)
759 kern_return_t result
;
762 if (task
== TASK_NULL
|| task
== kernel_task
)
763 return (KERN_INVALID_ARGUMENT
);
765 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
766 if (result
!= KERN_SUCCESS
)
769 thread
->user_stop_count
= 1;
771 if (task
->suspend_count
> 0)
775 lck_mtx_unlock(&tasks_threads_lock
);
777 *new_thread
= thread
;
779 return (KERN_SUCCESS
);
783 thread_create_running(
784 register task_t task
,
786 thread_state_t new_state
,
787 mach_msg_type_number_t new_state_count
,
788 thread_t
*new_thread
)
790 register kern_return_t result
;
793 if (task
== TASK_NULL
|| task
== kernel_task
)
794 return (KERN_INVALID_ARGUMENT
);
796 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, TH_OPTION_NONE
, &thread
);
797 if (result
!= KERN_SUCCESS
)
800 result
= machine_thread_set_state(
801 thread
, flavor
, new_state
, new_state_count
);
802 if (result
!= KERN_SUCCESS
) {
804 lck_mtx_unlock(&tasks_threads_lock
);
806 thread_terminate(thread
);
807 thread_deallocate(thread
);
811 thread_mtx_lock(thread
);
812 thread_start_internal(thread
);
813 thread_mtx_unlock(thread
);
816 lck_mtx_unlock(&tasks_threads_lock
);
818 *new_thread
= thread
;
826 thread_continue_t thread_return
,
827 thread_t
*new_thread
)
829 kern_return_t result
;
832 if (task
== TASK_NULL
|| task
== kernel_task
)
833 return (KERN_INVALID_ARGUMENT
);
835 result
= thread_create_internal(task
, -1, thread_return
, TH_OPTION_NOCRED
| TH_OPTION_NOSUSP
, &thread
);
836 if (result
!= KERN_SUCCESS
)
839 thread
->user_stop_count
= 1;
841 if (task
->suspend_count
> 0)
845 lck_mtx_unlock(&tasks_threads_lock
);
847 *new_thread
= thread
;
849 return (KERN_SUCCESS
);
853 * kernel_thread_create:
855 * Create a thread in the kernel task
856 * to execute in kernel context.
859 kernel_thread_create(
860 thread_continue_t continuation
,
863 thread_t
*new_thread
)
865 kern_return_t result
;
867 task_t task
= kernel_task
;
869 result
= thread_create_internal(task
, priority
, continuation
, TH_OPTION_NONE
, &thread
);
870 if (result
!= KERN_SUCCESS
)
874 lck_mtx_unlock(&tasks_threads_lock
);
877 assert(thread
->kernel_stack
!= 0);
879 if (priority
> BASEPRI_KERNEL
)
881 thread
->reserved_stack
= thread
->kernel_stack
;
883 thread
->parameter
= parameter
;
886 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread
, continuation
);
887 *new_thread
= thread
;
893 kernel_thread_start_priority(
894 thread_continue_t continuation
,
897 thread_t
*new_thread
)
899 kern_return_t result
;
902 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
903 if (result
!= KERN_SUCCESS
)
906 *new_thread
= thread
;
908 thread_mtx_lock(thread
);
909 thread_start_internal(thread
);
910 thread_mtx_unlock(thread
);
917 thread_continue_t continuation
,
919 thread_t
*new_thread
)
921 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
931 kern_return_t result
;
934 if (task
!= kernel_task
)
935 panic("kernel_thread");
937 result
= kernel_thread_start_priority((thread_continue_t
)start
, NULL
, -1, &thread
);
938 if (result
!= KERN_SUCCESS
)
939 return (THREAD_NULL
);
941 thread_deallocate(thread
);
946 #endif /* __LP64__ */
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*/
958 if (thread
== THREAD_NULL
)
959 return (KERN_INVALID_ARGUMENT
);
961 if (flavor
== THREAD_BASIC_INFO
) {
962 register thread_basic_info_t basic_info
;
964 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
965 return (KERN_INVALID_ARGUMENT
);
967 basic_info
= (thread_basic_info_t
) thread_info_out
;
974 thread_read_times(thread
, &basic_info
->user_time
,
975 &basic_info
->system_time
);
978 * Update lazy-evaluated scheduler info because someone wants it.
980 if (thread
->sched_stamp
!= sched_tick
)
981 update_priority(thread
);
983 basic_info
->sleep_time
= 0;
986 * To calculate cpu_usage, first correct for timer rate,
987 * then for 5/8 ageing. The correction factor [3/5] is
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;
994 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
)
995 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
997 basic_info
->policy
= ((thread
->sched_mode
& TH_MODE_TIMESHARE
)?
998 POLICY_TIMESHARE
: POLICY_RR
);
1001 if (thread
->bound_processor
!= PROCESSOR_NULL
&& thread
->bound_processor
->idle_thread
== thread
)
1002 flags
|= TH_FLAGS_IDLE
;
1004 if (!thread
->kernel_stack
)
1005 flags
|= TH_FLAGS_SWAPPED
;
1008 if (thread
->state
& TH_TERMINATE
)
1009 state
= TH_STATE_HALTED
;
1011 if (thread
->state
& TH_RUN
)
1012 state
= TH_STATE_RUNNING
;
1014 if (thread
->state
& TH_UNINT
)
1015 state
= TH_STATE_UNINTERRUPTIBLE
;
1017 if (thread
->state
& TH_SUSP
)
1018 state
= TH_STATE_STOPPED
;
1020 if (thread
->state
& TH_WAIT
)
1021 state
= TH_STATE_WAITING
;
1023 basic_info
->run_state
= state
;
1024 basic_info
->flags
= flags
;
1026 basic_info
->suspend_count
= thread
->user_stop_count
;
1028 thread_unlock(thread
);
1031 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
1033 return (KERN_SUCCESS
);
1036 if (flavor
== THREAD_IDENTIFIER_INFO
) {
1037 register thread_identifier_info_t identifier_info
;
1039 if (*thread_info_count
< THREAD_IDENTIFIER_INFO_COUNT
)
1040 return (KERN_INVALID_ARGUMENT
);
1042 identifier_info
= (thread_identifier_info_t
) thread_info_out
;
1045 thread_lock(thread
);
1047 identifier_info
->thread_id
= thread
->thread_id
;
1048 #if defined(__ppc__) || defined(__arm__)
1049 identifier_info
->thread_handle
= thread
->machine
.cthread_self
;
1051 identifier_info
->thread_handle
= thread
->machine
.pcb
->cthread_self
;
1053 if(thread
->task
->bsd_info
) {
1054 identifier_info
->dispatch_qaddr
= identifier_info
->thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1056 thread_unlock(thread
);
1058 return KERN_INVALID_ARGUMENT
;
1061 thread_unlock(thread
);
1063 return KERN_SUCCESS
;
1066 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
1067 policy_timeshare_info_t ts_info
;
1069 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
1070 return (KERN_INVALID_ARGUMENT
);
1072 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
1075 thread_lock(thread
);
1077 if (!(thread
->sched_mode
& TH_MODE_TIMESHARE
)) {
1078 thread_unlock(thread
);
1081 return (KERN_INVALID_POLICY
);
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
;
1090 ts_info
->base_priority
= thread
->priority
;
1091 ts_info
->depress_priority
= -1;
1094 ts_info
->cur_priority
= thread
->sched_pri
;
1095 ts_info
->max_priority
= thread
->max_priority
;
1097 thread_unlock(thread
);
1100 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
1102 return (KERN_SUCCESS
);
1105 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
1106 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
1107 return (KERN_INVALID_ARGUMENT
);
1109 return (KERN_INVALID_POLICY
);
1112 if (flavor
== THREAD_SCHED_RR_INFO
) {
1113 policy_rr_info_t rr_info
;
1115 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
1116 return (KERN_INVALID_ARGUMENT
);
1118 rr_info
= (policy_rr_info_t
) thread_info_out
;
1121 thread_lock(thread
);
1123 if (thread
->sched_mode
& TH_MODE_TIMESHARE
) {
1124 thread_unlock(thread
);
1127 return (KERN_INVALID_POLICY
);
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
;
1136 rr_info
->base_priority
= thread
->priority
;
1137 rr_info
->depress_priority
= -1;
1140 rr_info
->max_priority
= thread
->max_priority
;
1141 rr_info
->quantum
= std_quantum_us
/ 1000;
1143 thread_unlock(thread
);
1146 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1148 return (KERN_SUCCESS
);
1151 return (KERN_INVALID_ARGUMENT
);
1157 time_value_t
*user_time
,
1158 time_value_t
*system_time
)
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
;
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
;
1174 __unused thread_t thread
,
1175 __unused processor_set_t new_pset
)
1177 return (KERN_FAILURE
);
1181 * thread_assign_default:
1183 * Special version of thread_assign for assigning threads to default
1187 thread_assign_default(
1190 return (thread_assign(thread
, &pset0
));
1194 * thread_get_assignment
1196 * Return current assignment for this thread.
1199 thread_get_assignment(
1201 processor_set_t
*pset
)
1204 return (KERN_INVALID_ARGUMENT
);
1208 return (KERN_SUCCESS
);
1212 * thread_wire_internal:
1214 * Specify that the target thread must always be able
1215 * to run and to allocate memory.
1218 thread_wire_internal(
1219 host_priv_t host_priv
,
1222 boolean_t
*prev_state
)
1224 if (host_priv
== NULL
|| thread
!= current_thread())
1225 return (KERN_INVALID_ARGUMENT
);
1227 assert(host_priv
== &realhost
);
1230 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
1233 if (!(thread
->options
& TH_OPT_VMPRIV
))
1234 vm_page_free_reserve(1); /* XXX */
1235 thread
->options
|= TH_OPT_VMPRIV
;
1238 if (thread
->options
& TH_OPT_VMPRIV
)
1239 vm_page_free_reserve(-1); /* XXX */
1240 thread
->options
&= ~TH_OPT_VMPRIV
;
1243 return (KERN_SUCCESS
);
1250 * User-api wrapper for thread_wire_internal()
1254 host_priv_t host_priv
,
1258 return (thread_wire_internal(host_priv
, thread
, wired
, NULL
));
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
;
1273 if (funnel_lck_grp
== LCK_GRP_NULL
) {
1274 funnel_lck_grp_attr
= lck_grp_attr_alloc_init();
1276 funnel_lck_grp
= lck_grp_alloc_init("Funnel", funnel_lck_grp_attr
);
1278 funnel_lck_attr
= lck_attr_alloc_init();
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
);
1287 fnl
->fnl_type
= type
;
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
));
1306 lck_mtx_lock(fnl
->fnl_mutex
);
1307 fnl
->fnl_mtxholder
= current_thread();
1314 lck_mtx_unlock(fnl
->fnl_mutex
);
1315 fnl
->fnl_mtxholder
= NULL
;
1316 fnl
->fnl_mtxrelease
= current_thread();
1323 thread_t th
= current_thread();
1325 if (th
->funnel_state
& TH_FN_OWNED
) {
1326 return(th
->funnel_lock
);
1328 return(THR_FUNNEL_NULL
);
1336 thread_t cur_thread
;
1337 boolean_t funnel_state_prev
;
1340 cur_thread
= current_thread();
1341 funnel_state_prev
= ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
);
1343 if (funnel_state_prev
!= funneled
) {
1344 intr
= ml_set_interrupts_enabled(FALSE
);
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
,
1352 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
,
1354 cur_thread
->funnel_state
|= TH_FN_OWNED
;
1355 cur_thread
->funnel_lock
= fnl
;
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
,
1363 cur_thread
->funnel_lock
= THR_FUNNEL_NULL
;
1366 (void)ml_set_interrupts_enabled(intr
);
1368 /* if we are trying to acquire funnel recursively
1369 * check for funnel to be held already
1371 if (funneled
&& (fnl
->fnl_mutex
!= cur_thread
->funnel_lock
->fnl_mutex
)) {
1372 panic("thread_funnel_set: already holding a different funnel");
1375 return(funnel_state_prev
);
1381 __unused thread_t thread
)
1391 thread
->sched_call
= (call
!= NULL
)? call
: sched_call_null
;
1395 thread_static_param(
1399 thread_mtx_lock(thread
);
1400 thread
->static_param
= state
;
1401 thread_mtx_unlock(thread
);
1408 return (thread
!= THREAD_NULL
? thread
->thread_id
: 0);
1412 thread_dispatchqaddr(
1415 uint64_t dispatchqueue_addr
= 0;
1416 uint64_t thread_handle
= 0;
1418 if (thread
!= THREAD_NULL
) {
1419 #if defined(__ppc__) || defined(__arm__)
1420 thread_handle
= thread
->machine
.cthread_self
;
1422 thread_handle
= thread
->machine
.pcb
->cthread_self
;
1425 if (thread
->task
->bsd_info
)
1426 dispatchqueue_addr
= thread_handle
+ get_dispatchqueue_offset_from_proc(thread
->task
->bsd_info
);
1429 return (dispatchqueue_addr
);
1433 * Export routines to other components for things that are done as macros
1434 * within the osfmk component.
1437 #undef thread_reference
1438 void thread_reference(thread_t thread
);
1443 if (thread
!= THREAD_NULL
)
1444 thread_reference_internal(thread
);
1447 #undef thread_should_halt
1453 return (thread_should_halt_fast(th
));
1457 uint32_t dtrace_get_thread_predcache(thread_t thread
)
1459 if (thread
!= THREAD_NULL
)
1460 return thread
->t_dtrace_predcache
;
1465 int64_t dtrace_get_thread_vtime(thread_t thread
)
1467 if (thread
!= THREAD_NULL
)
1468 return thread
->t_dtrace_vtime
;
1473 int64_t dtrace_get_thread_tracing(thread_t thread
)
1475 if (thread
!= THREAD_NULL
)
1476 return thread
->t_dtrace_tracing
;
1481 boolean_t
dtrace_get_thread_reentering(thread_t thread
)
1483 if (thread
!= THREAD_NULL
)
1484 return (thread
->options
& TH_OPT_DTRACE
) ? TRUE
: FALSE
;
1489 vm_offset_t
dtrace_get_kernel_stack(thread_t thread
)
1491 if (thread
!= THREAD_NULL
)
1492 return thread
->kernel_stack
;
1497 int64_t dtrace_calc_thread_recent_vtime(thread_t thread
)
1500 if (thread
!= THREAD_NULL
) {
1501 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
));
1505 if (thread
!= THREAD_NULL
) {
1506 processor_t processor
= current_processor();
1507 uint64_t abstime
= mach_absolute_time();
1510 timer
= PROCESSOR_DATA(processor
, thread_timer
);
1512 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
)) +
1513 (abstime
- timer
->tstamp
); /* XXX need interrupts off to prevent missed time? */
1519 void dtrace_set_thread_predcache(thread_t thread
, uint32_t predcache
)
1521 if (thread
!= THREAD_NULL
)
1522 thread
->t_dtrace_predcache
= predcache
;
1525 void dtrace_set_thread_vtime(thread_t thread
, int64_t vtime
)
1527 if (thread
!= THREAD_NULL
)
1528 thread
->t_dtrace_vtime
= vtime
;
1531 void dtrace_set_thread_tracing(thread_t thread
, int64_t accum
)
1533 if (thread
!= THREAD_NULL
)
1534 thread
->t_dtrace_tracing
= accum
;
1537 void dtrace_set_thread_reentering(thread_t thread
, boolean_t vbool
)
1539 if (thread
!= THREAD_NULL
) {
1541 thread
->options
|= TH_OPT_DTRACE
;
1543 thread
->options
&= (~TH_OPT_DTRACE
);
1547 vm_offset_t
dtrace_set_thread_recover(thread_t thread
, vm_offset_t recover
)
1549 vm_offset_t prev
= 0;
1551 if (thread
!= THREAD_NULL
) {
1552 prev
= thread
->recover
;
1553 thread
->recover
= recover
;
1558 void dtrace_thread_bootstrap(void)
1560 task_t task
= current_task();
1561 if(task
->thread_count
== 1) {
1564 DTRACE_PROC(lwp__start
);
1567 #endif /* CONFIG_DTRACE */