2 * Copyright (c) 2000-2007 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
;
136 decl_simple_lock_data(static,thread_stack_lock
)
137 static queue_head_t thread_stack_queue
;
139 decl_simple_lock_data(static,thread_terminate_lock
)
140 static queue_head_t thread_terminate_queue
;
142 static struct thread thread_template
, init_thread
;
144 static void sched_call_null(
148 extern void proc_exit(void *);
149 #endif /* MACH_BSD */
150 extern int debug_task
;
153 thread_bootstrap(void)
156 * Fill in a template thread for fast initialization.
159 thread_template
.runq
= PROCESSOR_NULL
;
161 thread_template
.ref_count
= 2;
163 thread_template
.reason
= AST_NONE
;
164 thread_template
.at_safe_point
= FALSE
;
165 thread_template
.wait_event
= NO_EVENT64
;
166 thread_template
.wait_queue
= WAIT_QUEUE_NULL
;
167 thread_template
.wait_result
= THREAD_WAITING
;
168 thread_template
.options
= THREAD_ABORTSAFE
;
169 thread_template
.state
= TH_WAIT
| TH_UNINT
;
170 thread_template
.wake_active
= FALSE
;
171 thread_template
.continuation
= THREAD_CONTINUE_NULL
;
172 thread_template
.parameter
= NULL
;
174 thread_template
.importance
= 0;
175 thread_template
.sched_mode
= 0;
176 thread_template
.safe_mode
= 0;
177 thread_template
.safe_release
= 0;
179 thread_template
.priority
= 0;
180 thread_template
.sched_pri
= 0;
181 thread_template
.max_priority
= 0;
182 thread_template
.task_priority
= 0;
183 thread_template
.promotions
= 0;
184 thread_template
.pending_promoter_index
= 0;
185 thread_template
.pending_promoter
[0] =
186 thread_template
.pending_promoter
[1] = NULL
;
188 thread_template
.realtime
.deadline
= UINT64_MAX
;
190 thread_template
.current_quantum
= 0;
192 thread_template
.computation_metered
= 0;
193 thread_template
.computation_epoch
= 0;
195 thread_template
.sched_stamp
= 0;
196 thread_template
.sched_usage
= 0;
197 thread_template
.pri_shift
= INT8_MAX
;
198 thread_template
.cpu_usage
= thread_template
.cpu_delta
= 0;
199 thread_template
.c_switch
= thread_template
.p_switch
= thread_template
.ps_switch
= 0;
201 thread_template
.bound_processor
= PROCESSOR_NULL
;
202 thread_template
.last_processor
= PROCESSOR_NULL
;
203 thread_template
.last_switch
= 0;
205 thread_template
.sched_call
= sched_call_null
;
207 timer_init(&thread_template
.user_timer
);
208 timer_init(&thread_template
.system_timer
);
209 thread_template
.user_timer_save
= 0;
210 thread_template
.system_timer_save
= 0;
211 thread_template
.vtimer_user_save
= 0;
212 thread_template
.vtimer_prof_save
= 0;
213 thread_template
.vtimer_rlim_save
= 0;
215 thread_template
.wait_timer_is_set
= FALSE
;
216 thread_template
.wait_timer_active
= 0;
218 thread_template
.depress_timer_active
= 0;
220 thread_template
.special_handler
.handler
= special_handler
;
221 thread_template
.special_handler
.next
= NULL
;
223 thread_template
.funnel_lock
= THR_FUNNEL_NULL
;
224 thread_template
.funnel_state
= 0;
225 thread_template
.recover
= (vm_offset_t
)NULL
;
227 thread_template
.map
= VM_MAP_NULL
;
230 thread_template
.t_dtrace_predcache
= 0;
231 thread_template
.t_dtrace_vtime
= 0;
232 thread_template
.t_dtrace_tracing
= 0;
233 #endif /* CONFIG_DTRACE */
235 thread_template
.t_chud
= 0;
237 thread_template
.affinity_set
= NULL
;
239 init_thread
= thread_template
;
240 machine_set_current_thread(&init_thread
);
247 sizeof(struct thread
),
248 THREAD_MAX
* sizeof(struct thread
),
249 THREAD_CHUNK
* sizeof(struct thread
),
255 * Initialize any machine-dependent
256 * per-thread structures necessary.
258 machine_thread_init();
262 thread_terminate_continue(void)
264 panic("thread_terminate_continue");
269 * thread_terminate_self:
272 thread_terminate_self(void)
274 thread_t thread
= current_thread();
279 thread_mtx_lock(thread
);
281 ulock_release_all(thread
);
283 ipc_thread_disable(thread
);
285 thread_mtx_unlock(thread
);
291 * Cancel priority depression, wait for concurrent expirations
292 * on other processors.
294 if (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) {
295 thread
->sched_mode
&= ~TH_MODE_ISDEPRESSED
;
297 if (timer_call_cancel(&thread
->depress_timer
))
298 thread
->depress_timer_active
--;
301 while (thread
->depress_timer_active
> 0) {
302 thread_unlock(thread
);
311 thread_unlock(thread
);
314 thread_policy_reset(thread
);
317 * If we are the last thread to terminate and the task is
318 * associated with a BSD process, perform BSD process exit.
321 uthread_cleanup(task
, thread
->uthread
, task
->bsd_info
);
322 if (hw_atomic_sub(&task
->active_thread_count
, 1) == 0 &&
323 task
->bsd_info
!= NULL
) {
328 proc_exit(task
->bsd_info
);
330 uthread_cred_free(thread
->uthread
);
336 * Cancel wait timer, and wait for
337 * concurrent expirations.
339 if (thread
->wait_timer_is_set
) {
340 thread
->wait_timer_is_set
= FALSE
;
342 if (timer_call_cancel(&thread
->wait_timer
))
343 thread
->wait_timer_active
--;
346 while (thread
->wait_timer_active
> 0) {
347 thread_unlock(thread
);
357 * If there is a reserved stack, release it.
359 if (thread
->reserved_stack
!= 0) {
360 if (thread
->reserved_stack
!= thread
->kernel_stack
)
361 stack_free_stack(thread
->reserved_stack
);
362 thread
->reserved_stack
= 0;
366 * Mark thread as terminating, and block.
368 thread
->state
|= TH_TERMINATE
;
369 thread_mark_wait_locked(thread
, THREAD_UNINT
);
370 assert(thread
->promotions
== 0);
371 thread_unlock(thread
);
374 thread_block((thread_continue_t
)thread_terminate_continue
);
384 if (thread
== THREAD_NULL
)
387 if (thread_deallocate_internal(thread
) > 0)
390 ipc_thread_terminate(thread
);
396 void *ut
= thread
->uthread
;
398 thread
->uthread
= NULL
;
399 uthread_zone_free(ut
);
401 #endif /* MACH_BSD */
403 task_deallocate(task
);
405 if (thread
->kernel_stack
!= 0)
408 machine_thread_destroy(thread
);
410 zfree(thread_zone
, thread
);
414 * thread_terminate_daemon:
416 * Perform final clean up for terminating threads.
419 thread_terminate_daemon(void)
425 simple_lock(&thread_terminate_lock
);
427 while ((thread
= (thread_t
)dequeue_head(&thread_terminate_queue
)) != THREAD_NULL
) {
428 simple_unlock(&thread_terminate_lock
);
434 task
->total_user_time
+= timer_grab(&thread
->user_timer
);
435 task
->total_system_time
+= timer_grab(&thread
->system_timer
);
437 task
->c_switch
+= thread
->c_switch
;
438 task
->p_switch
+= thread
->p_switch
;
439 task
->ps_switch
+= thread
->ps_switch
;
441 queue_remove(&task
->threads
, thread
, thread_t
, task_threads
);
442 task
->thread_count
--;
445 mutex_lock(&tasks_threads_lock
);
446 queue_remove(&threads
, thread
, thread_t
, threads
);
448 mutex_unlock(&tasks_threads_lock
);
450 thread_deallocate(thread
);
453 simple_lock(&thread_terminate_lock
);
456 assert_wait((event_t
)&thread_terminate_queue
, THREAD_UNINT
);
457 simple_unlock(&thread_terminate_lock
);
460 thread_block((thread_continue_t
)thread_terminate_daemon
);
465 * thread_terminate_enqueue:
467 * Enqueue a terminating thread for final disposition.
469 * Called at splsched.
472 thread_terminate_enqueue(
475 simple_lock(&thread_terminate_lock
);
476 enqueue_tail(&thread_terminate_queue
, (queue_entry_t
)thread
);
477 simple_unlock(&thread_terminate_lock
);
479 thread_wakeup((event_t
)&thread_terminate_queue
);
483 * thread_stack_daemon:
485 * Perform stack allocation as required due to
489 thread_stack_daemon(void)
493 simple_lock(&thread_stack_lock
);
495 while ((thread
= (thread_t
)dequeue_head(&thread_stack_queue
)) != THREAD_NULL
) {
496 simple_unlock(&thread_stack_lock
);
502 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
503 thread_unlock(thread
);
506 simple_lock(&thread_stack_lock
);
509 assert_wait((event_t
)&thread_stack_queue
, THREAD_UNINT
);
510 simple_unlock(&thread_stack_lock
);
512 thread_block((thread_continue_t
)thread_stack_daemon
);
517 * thread_stack_enqueue:
519 * Enqueue a thread for stack allocation.
521 * Called at splsched.
524 thread_stack_enqueue(
527 simple_lock(&thread_stack_lock
);
528 enqueue_tail(&thread_stack_queue
, (queue_entry_t
)thread
);
529 simple_unlock(&thread_stack_lock
);
531 thread_wakeup((event_t
)&thread_stack_queue
);
535 thread_daemon_init(void)
537 kern_return_t result
;
540 simple_lock_init(&thread_terminate_lock
, 0);
541 queue_init(&thread_terminate_queue
);
543 result
= kernel_thread_start_priority((thread_continue_t
)thread_terminate_daemon
, NULL
, MINPRI_KERNEL
, &thread
);
544 if (result
!= KERN_SUCCESS
)
545 panic("thread_daemon_init: thread_terminate_daemon");
547 thread_deallocate(thread
);
549 simple_lock_init(&thread_stack_lock
, 0);
550 queue_init(&thread_stack_queue
);
552 result
= kernel_thread_start_priority((thread_continue_t
)thread_stack_daemon
, NULL
, BASEPRI_PREEMPT
, &thread
);
553 if (result
!= KERN_SUCCESS
)
554 panic("thread_daemon_init: thread_stack_daemon");
556 thread_deallocate(thread
);
560 * Create a new thread.
561 * Doesn't start the thread running.
564 thread_create_internal(
567 thread_continue_t continuation
,
568 thread_t
*out_thread
)
571 static thread_t first_thread
;
574 * Allocate a thread and initialize static fields
576 if (first_thread
== NULL
)
577 new_thread
= first_thread
= current_thread();
579 new_thread
= (thread_t
)zalloc(thread_zone
);
580 if (new_thread
== NULL
)
581 return (KERN_RESOURCE_SHORTAGE
);
583 if (new_thread
!= first_thread
)
584 *new_thread
= thread_template
;
588 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
);
589 if (new_thread
->uthread
== NULL
) {
590 zfree(thread_zone
, new_thread
);
591 return (KERN_RESOURCE_SHORTAGE
);
594 #endif /* MACH_BSD */
596 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
599 void *ut
= new_thread
->uthread
;
601 new_thread
->uthread
= NULL
;
602 /* cred free may not be necessary */
603 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
604 uthread_cred_free(ut
);
605 uthread_zone_free(ut
);
607 #endif /* MACH_BSD */
608 zfree(thread_zone
, new_thread
);
609 return (KERN_FAILURE
);
612 new_thread
->task
= parent_task
;
614 thread_lock_init(new_thread
);
615 wake_lock_init(new_thread
);
617 mutex_init(&new_thread
->mutex
, 0);
619 ipc_thread_init(new_thread
);
620 queue_init(&new_thread
->held_ulocks
);
622 new_thread
->continuation
= continuation
;
624 mutex_lock(&tasks_threads_lock
);
625 task_lock(parent_task
);
627 if ( !parent_task
->active
||
628 (parent_task
->thread_count
>= THREAD_MAX
&&
629 parent_task
!= kernel_task
)) {
630 task_unlock(parent_task
);
631 mutex_unlock(&tasks_threads_lock
);
635 void *ut
= new_thread
->uthread
;
637 new_thread
->uthread
= NULL
;
638 uthread_cleanup(parent_task
, ut
, parent_task
->bsd_info
);
639 /* cred free may not be necessary */
640 uthread_cred_free(ut
);
641 uthread_zone_free(ut
);
643 #endif /* MACH_BSD */
644 ipc_thread_disable(new_thread
);
645 ipc_thread_terminate(new_thread
);
646 machine_thread_destroy(new_thread
);
647 zfree(thread_zone
, new_thread
);
648 return (KERN_FAILURE
);
651 task_reference_internal(parent_task
);
653 /* Cache the task's map */
654 new_thread
->map
= parent_task
->map
;
656 /* Chain the thread onto the task's list */
657 queue_enter(&parent_task
->threads
, new_thread
, thread_t
, task_threads
);
658 parent_task
->thread_count
++;
660 /* So terminating threads don't need to take the task lock to decrement */
661 hw_atomic_add(&parent_task
->active_thread_count
, 1);
663 queue_enter(&threads
, new_thread
, thread_t
, threads
);
666 timer_call_setup(&new_thread
->wait_timer
, thread_timer_expire
, new_thread
);
667 timer_call_setup(&new_thread
->depress_timer
, thread_depress_expire
, new_thread
);
669 /* Set the thread's scheduling parameters */
670 if (parent_task
!= kernel_task
)
671 new_thread
->sched_mode
|= TH_MODE_TIMESHARE
;
672 new_thread
->max_priority
= parent_task
->max_priority
;
673 new_thread
->task_priority
= parent_task
->priority
;
674 new_thread
->priority
= (priority
< 0)? parent_task
->priority
: priority
;
675 if (new_thread
->priority
> new_thread
->max_priority
)
676 new_thread
->priority
= new_thread
->max_priority
;
677 new_thread
->importance
=
678 new_thread
->priority
- new_thread
->task_priority
;
679 new_thread
->sched_stamp
= sched_tick
;
680 new_thread
->pri_shift
= sched_pri_shift
;
681 compute_priority(new_thread
, FALSE
);
683 new_thread
->active
= TRUE
;
685 *out_thread
= new_thread
;
688 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
690 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
692 KERNEL_DEBUG_CONSTANT(
693 TRACEDBG_CODE(DBG_TRACE_DATA
, 1) | DBG_FUNC_NONE
,
694 (vm_address_t
)new_thread
, dbg_arg2
, 0, 0, 0);
696 kdbg_trace_string(parent_task
->bsd_info
,
697 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
699 KERNEL_DEBUG_CONSTANT(
700 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
701 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
704 DTRACE_PROC1(lwp__create
, thread_t
, *out_thread
);
706 return (KERN_SUCCESS
);
712 thread_t
*new_thread
)
714 kern_return_t result
;
717 if (task
== TASK_NULL
|| task
== kernel_task
)
718 return (KERN_INVALID_ARGUMENT
);
720 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, &thread
);
721 if (result
!= KERN_SUCCESS
)
724 thread
->user_stop_count
= 1;
726 if (task
->suspend_count
> 0)
730 mutex_unlock(&tasks_threads_lock
);
732 *new_thread
= thread
;
734 return (KERN_SUCCESS
);
738 thread_create_running(
739 register task_t task
,
741 thread_state_t new_state
,
742 mach_msg_type_number_t new_state_count
,
743 thread_t
*new_thread
)
745 register kern_return_t result
;
748 if (task
== TASK_NULL
|| task
== kernel_task
)
749 return (KERN_INVALID_ARGUMENT
);
751 result
= thread_create_internal(task
, -1, (thread_continue_t
)thread_bootstrap_return
, &thread
);
752 if (result
!= KERN_SUCCESS
)
755 result
= machine_thread_set_state(
756 thread
, flavor
, new_state
, new_state_count
);
757 if (result
!= KERN_SUCCESS
) {
759 mutex_unlock(&tasks_threads_lock
);
761 thread_terminate(thread
);
762 thread_deallocate(thread
);
766 thread_mtx_lock(thread
);
767 thread_start_internal(thread
);
768 thread_mtx_unlock(thread
);
771 mutex_unlock(&tasks_threads_lock
);
773 *new_thread
= thread
;
779 * kernel_thread_create:
781 * Create a thread in the kernel task
782 * to execute in kernel context.
785 kernel_thread_create(
786 thread_continue_t continuation
,
789 thread_t
*new_thread
)
791 kern_return_t result
;
793 task_t task
= kernel_task
;
795 result
= thread_create_internal(task
, priority
, continuation
, &thread
);
796 if (result
!= KERN_SUCCESS
)
800 mutex_unlock(&tasks_threads_lock
);
803 assert(thread
->kernel_stack
!= 0);
805 if (priority
> BASEPRI_KERNEL
)
807 thread
->reserved_stack
= thread
->kernel_stack
;
809 thread
->parameter
= parameter
;
812 kprintf("kernel_thread_create: thread = %p continuation = %p\n", thread
, continuation
);
813 *new_thread
= thread
;
819 kernel_thread_start_priority(
820 thread_continue_t continuation
,
823 thread_t
*new_thread
)
825 kern_return_t result
;
828 result
= kernel_thread_create(continuation
, parameter
, priority
, &thread
);
829 if (result
!= KERN_SUCCESS
)
832 thread_mtx_lock(thread
);
833 thread_start_internal(thread
);
834 thread_mtx_unlock(thread
);
836 *new_thread
= thread
;
843 thread_continue_t continuation
,
845 thread_t
*new_thread
)
847 return kernel_thread_start_priority(continuation
, parameter
, -1, new_thread
);
855 kern_return_t result
;
858 if (task
!= kernel_task
)
859 panic("kernel_thread");
861 result
= kernel_thread_start_priority((thread_continue_t
)start
, NULL
, -1, &thread
);
862 if (result
!= KERN_SUCCESS
)
863 return (THREAD_NULL
);
865 thread_deallocate(thread
);
871 thread_info_internal(
872 register thread_t thread
,
873 thread_flavor_t flavor
,
874 thread_info_t thread_info_out
, /* ptr to OUT array */
875 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
880 if (thread
== THREAD_NULL
)
881 return (KERN_INVALID_ARGUMENT
);
883 if (flavor
== THREAD_BASIC_INFO
) {
884 register thread_basic_info_t basic_info
;
886 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
887 return (KERN_INVALID_ARGUMENT
);
889 basic_info
= (thread_basic_info_t
) thread_info_out
;
896 thread_read_times(thread
, &basic_info
->user_time
,
897 &basic_info
->system_time
);
900 * Update lazy-evaluated scheduler info because someone wants it.
902 if (thread
->sched_stamp
!= sched_tick
)
903 update_priority(thread
);
905 basic_info
->sleep_time
= 0;
908 * To calculate cpu_usage, first correct for timer rate,
909 * then for 5/8 ageing. The correction factor [3/5] is
912 basic_info
->cpu_usage
= ((uint64_t)thread
->cpu_usage
913 * TH_USAGE_SCALE
) / sched_tick_interval
;
914 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
916 if (basic_info
->cpu_usage
> TH_USAGE_SCALE
)
917 basic_info
->cpu_usage
= TH_USAGE_SCALE
;
919 basic_info
->policy
= ((thread
->sched_mode
& TH_MODE_TIMESHARE
)?
920 POLICY_TIMESHARE
: POLICY_RR
);
923 if (thread
->bound_processor
!= PROCESSOR_NULL
&& thread
->bound_processor
->idle_thread
== thread
)
924 flags
|= TH_FLAGS_IDLE
;
926 if (!thread
->kernel_stack
)
927 flags
|= TH_FLAGS_SWAPPED
;
930 if (thread
->state
& TH_TERMINATE
)
931 state
= TH_STATE_HALTED
;
933 if (thread
->state
& TH_RUN
)
934 state
= TH_STATE_RUNNING
;
936 if (thread
->state
& TH_UNINT
)
937 state
= TH_STATE_UNINTERRUPTIBLE
;
939 if (thread
->state
& TH_SUSP
)
940 state
= TH_STATE_STOPPED
;
942 if (thread
->state
& TH_WAIT
)
943 state
= TH_STATE_WAITING
;
945 basic_info
->run_state
= state
;
946 basic_info
->flags
= flags
;
948 basic_info
->suspend_count
= thread
->user_stop_count
;
950 thread_unlock(thread
);
953 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
955 return (KERN_SUCCESS
);
958 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
959 policy_timeshare_info_t ts_info
;
961 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
962 return (KERN_INVALID_ARGUMENT
);
964 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
969 if (!(thread
->sched_mode
& TH_MODE_TIMESHARE
)) {
970 thread_unlock(thread
);
973 return (KERN_INVALID_POLICY
);
976 ts_info
->depressed
= (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) != 0;
977 if (ts_info
->depressed
) {
978 ts_info
->base_priority
= DEPRESSPRI
;
979 ts_info
->depress_priority
= thread
->priority
;
982 ts_info
->base_priority
= thread
->priority
;
983 ts_info
->depress_priority
= -1;
986 ts_info
->cur_priority
= thread
->sched_pri
;
987 ts_info
->max_priority
= thread
->max_priority
;
989 thread_unlock(thread
);
992 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
994 return (KERN_SUCCESS
);
997 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
998 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
999 return (KERN_INVALID_ARGUMENT
);
1001 return (KERN_INVALID_POLICY
);
1004 if (flavor
== THREAD_SCHED_RR_INFO
) {
1005 policy_rr_info_t rr_info
;
1007 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
1008 return (KERN_INVALID_ARGUMENT
);
1010 rr_info
= (policy_rr_info_t
) thread_info_out
;
1013 thread_lock(thread
);
1015 if (thread
->sched_mode
& TH_MODE_TIMESHARE
) {
1016 thread_unlock(thread
);
1019 return (KERN_INVALID_POLICY
);
1022 rr_info
->depressed
= (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) != 0;
1023 if (rr_info
->depressed
) {
1024 rr_info
->base_priority
= DEPRESSPRI
;
1025 rr_info
->depress_priority
= thread
->priority
;
1028 rr_info
->base_priority
= thread
->priority
;
1029 rr_info
->depress_priority
= -1;
1032 rr_info
->max_priority
= thread
->max_priority
;
1033 rr_info
->quantum
= std_quantum_us
/ 1000;
1035 thread_unlock(thread
);
1038 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1040 return (KERN_SUCCESS
);
1043 return (KERN_INVALID_ARGUMENT
);
1049 time_value_t
*user_time
,
1050 time_value_t
*system_time
)
1052 absolutetime_to_microtime(timer_grab(&thread
->user_timer
),
1053 (unsigned *)&user_time
->seconds
,
1054 (unsigned *)&user_time
->microseconds
);
1056 absolutetime_to_microtime(timer_grab(&thread
->system_timer
),
1057 (unsigned *)&system_time
->seconds
,
1058 (unsigned *)&system_time
->microseconds
);
1063 __unused thread_t thread
,
1064 __unused processor_set_t new_pset
)
1066 return (KERN_FAILURE
);
1070 * thread_assign_default:
1072 * Special version of thread_assign for assigning threads to default
1076 thread_assign_default(
1079 return (thread_assign(thread
, &pset0
));
1083 * thread_get_assignment
1085 * Return current assignment for this thread.
1088 thread_get_assignment(
1090 processor_set_t
*pset
)
1093 return (KERN_INVALID_ARGUMENT
);
1097 return (KERN_SUCCESS
);
1101 * thread_wire_internal:
1103 * Specify that the target thread must always be able
1104 * to run and to allocate memory.
1107 thread_wire_internal(
1108 host_priv_t host_priv
,
1111 boolean_t
*prev_state
)
1113 if (host_priv
== NULL
|| thread
!= current_thread())
1114 return (KERN_INVALID_ARGUMENT
);
1116 assert(host_priv
== &realhost
);
1119 *prev_state
= (thread
->options
& TH_OPT_VMPRIV
) != 0;
1122 if (!(thread
->options
& TH_OPT_VMPRIV
))
1123 vm_page_free_reserve(1); /* XXX */
1124 thread
->options
|= TH_OPT_VMPRIV
;
1127 if (thread
->options
& TH_OPT_VMPRIV
)
1128 vm_page_free_reserve(-1); /* XXX */
1129 thread
->options
&= ~TH_OPT_VMPRIV
;
1132 return (KERN_SUCCESS
);
1139 * User-api wrapper for thread_wire_internal()
1143 host_priv_t host_priv
,
1147 return (thread_wire_internal(host_priv
, thread
, wired
, NULL
));
1150 int split_funnel_off
= 0;
1151 lck_grp_t
*funnel_lck_grp
= LCK_GRP_NULL
;
1152 lck_grp_attr_t
*funnel_lck_grp_attr
;
1153 lck_attr_t
*funnel_lck_attr
;
1162 if (funnel_lck_grp
== LCK_GRP_NULL
) {
1163 funnel_lck_grp_attr
= lck_grp_attr_alloc_init();
1165 funnel_lck_grp
= lck_grp_alloc_init("Funnel", funnel_lck_grp_attr
);
1167 funnel_lck_attr
= lck_attr_alloc_init();
1169 if ((fnl
= (funnel_t
*)kalloc(sizeof(funnel_t
))) != 0){
1170 bzero((void *)fnl
, sizeof(funnel_t
));
1171 if ((m
= lck_mtx_alloc_init(funnel_lck_grp
, funnel_lck_attr
)) == (lck_mtx_t
*)NULL
) {
1172 kfree(fnl
, sizeof(funnel_t
));
1173 return(THR_FUNNEL_NULL
);
1176 fnl
->fnl_type
= type
;
1185 lck_mtx_free(fnl
->fnl_mutex
, funnel_lck_grp
);
1186 if (fnl
->fnl_oldmutex
)
1187 lck_mtx_free(fnl
->fnl_oldmutex
, funnel_lck_grp
);
1188 kfree(fnl
, sizeof(funnel_t
));
1195 lck_mtx_lock(fnl
->fnl_mutex
);
1196 fnl
->fnl_mtxholder
= current_thread();
1203 lck_mtx_unlock(fnl
->fnl_mutex
);
1204 fnl
->fnl_mtxholder
= NULL
;
1205 fnl
->fnl_mtxrelease
= current_thread();
1212 thread_t th
= current_thread();
1214 if (th
->funnel_state
& TH_FN_OWNED
) {
1215 return(th
->funnel_lock
);
1217 return(THR_FUNNEL_NULL
);
1225 thread_t cur_thread
;
1226 boolean_t funnel_state_prev
;
1229 cur_thread
= current_thread();
1230 funnel_state_prev
= ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
);
1232 if (funnel_state_prev
!= funneled
) {
1233 intr
= ml_set_interrupts_enabled(FALSE
);
1235 if (funneled
== TRUE
) {
1236 if (cur_thread
->funnel_lock
)
1237 panic("Funnel lock called when holding one %p", cur_thread
->funnel_lock
);
1238 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
,
1241 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
,
1243 cur_thread
->funnel_state
|= TH_FN_OWNED
;
1244 cur_thread
->funnel_lock
= fnl
;
1246 if(cur_thread
->funnel_lock
->fnl_mutex
!= fnl
->fnl_mutex
)
1247 panic("Funnel unlock when not holding funnel");
1248 cur_thread
->funnel_state
&= ~TH_FN_OWNED
;
1249 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
,
1252 cur_thread
->funnel_lock
= THR_FUNNEL_NULL
;
1255 (void)ml_set_interrupts_enabled(intr
);
1257 /* if we are trying to acquire funnel recursively
1258 * check for funnel to be held already
1260 if (funneled
&& (fnl
->fnl_mutex
!= cur_thread
->funnel_lock
->fnl_mutex
)) {
1261 panic("thread_funnel_set: already holding a different funnel");
1264 return(funnel_state_prev
);
1270 __unused thread_t thread
)
1280 thread
->sched_call
= (call
!= NULL
)? call
: sched_call_null
;
1284 thread_static_param(
1288 thread_mtx_lock(thread
);
1289 thread
->static_param
= state
;
1290 thread_mtx_unlock(thread
);
1294 * Export routines to other components for things that are done as macros
1295 * within the osfmk component.
1298 #undef thread_reference
1299 void thread_reference(thread_t thread
);
1304 if (thread
!= THREAD_NULL
)
1305 thread_reference_internal(thread
);
1308 #undef thread_should_halt
1314 return (thread_should_halt_fast(th
));
1318 uint32_t dtrace_get_thread_predcache(thread_t thread
)
1320 if (thread
!= THREAD_NULL
)
1321 return thread
->t_dtrace_predcache
;
1326 int64_t dtrace_get_thread_vtime(thread_t thread
)
1328 if (thread
!= THREAD_NULL
)
1329 return thread
->t_dtrace_vtime
;
1334 int64_t dtrace_get_thread_tracing(thread_t thread
)
1336 if (thread
!= THREAD_NULL
)
1337 return thread
->t_dtrace_tracing
;
1342 boolean_t
dtrace_get_thread_reentering(thread_t thread
)
1344 if (thread
!= THREAD_NULL
)
1345 return (thread
->options
& TH_OPT_DTRACE
) ? TRUE
: FALSE
;
1350 vm_offset_t
dtrace_get_kernel_stack(thread_t thread
)
1352 if (thread
!= THREAD_NULL
)
1353 return thread
->kernel_stack
;
1358 int64_t dtrace_calc_thread_recent_vtime(thread_t thread
)
1361 if (thread
!= THREAD_NULL
) {
1362 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
));
1366 if (thread
!= THREAD_NULL
) {
1367 processor_t processor
= current_processor();
1368 uint64_t abstime
= mach_absolute_time();
1371 timer
= PROCESSOR_DATA(processor
, thread_timer
);
1373 return timer_grab(&(thread
->system_timer
)) + timer_grab(&(thread
->user_timer
)) +
1374 (abstime
- timer
->tstamp
); /* XXX need interrupts off to prevent missed time? */
1380 void dtrace_set_thread_predcache(thread_t thread
, uint32_t predcache
)
1382 if (thread
!= THREAD_NULL
)
1383 thread
->t_dtrace_predcache
= predcache
;
1386 void dtrace_set_thread_vtime(thread_t thread
, int64_t vtime
)
1388 if (thread
!= THREAD_NULL
)
1389 thread
->t_dtrace_vtime
= vtime
;
1392 void dtrace_set_thread_tracing(thread_t thread
, int64_t accum
)
1394 if (thread
!= THREAD_NULL
)
1395 thread
->t_dtrace_tracing
= accum
;
1398 void dtrace_set_thread_reentering(thread_t thread
, boolean_t vbool
)
1400 if (thread
!= THREAD_NULL
) {
1402 thread
->options
|= TH_OPT_DTRACE
;
1404 thread
->options
&= (~TH_OPT_DTRACE
);
1408 vm_offset_t
dtrace_set_thread_recover(thread_t thread
, vm_offset_t recover
)
1410 vm_offset_t prev
= 0;
1412 if (thread
!= THREAD_NULL
) {
1413 prev
= thread
->recover
;
1414 thread
->recover
= recover
;
1419 #endif /* CONFIG_DTRACE */