2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * @OSF_FREE_COPYRIGHT@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
57 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub
60 * Thread/thread_shuttle management primitives implementation.
63 * Copyright (c) 1993 The University of Utah and
64 * the Computer Systems Laboratory (CSL). All rights reserved.
66 * Permission to use, copy, modify and distribute this software and its
67 * documentation is hereby granted, provided that both the copyright
68 * notice and this permission notice appear in all copies of the
69 * software, derivative works or modified versions, and any portions
70 * thereof, and that both notices appear in supporting documentation.
72 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
73 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
74 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
76 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
77 * improvements that they make and grant CSL redistribution rights.
82 #include <mach_host.h>
83 #include <simple_clock.h>
84 #include <mach_debug.h>
85 #include <mach_prof.h>
87 #include <mach/boolean.h>
88 #include <mach/policy.h>
89 #include <mach/thread_info.h>
90 #include <mach/thread_special_ports.h>
91 #include <mach/thread_status.h>
92 #include <mach/time_value.h>
93 #include <mach/vm_param.h>
95 #include <kern/cpu_data.h>
96 #include <kern/counters.h>
97 #include <kern/etap_macros.h>
98 #include <kern/ipc_mig.h>
99 #include <kern/ipc_tt.h>
100 #include <kern/mach_param.h>
101 #include <kern/machine.h>
102 #include <kern/misc_protos.h>
103 #include <kern/processor.h>
104 #include <kern/queue.h>
105 #include <kern/sched.h>
106 #include <kern/sched_prim.h>
107 #include <kern/mk_sp.h> /*** ??? fix so this can be removed ***/
108 #include <kern/task.h>
109 #include <kern/thread.h>
110 #include <kern/thread_act.h>
111 #include <kern/thread_swap.h>
112 #include <kern/host.h>
113 #include <kern/zalloc.h>
114 #include <vm/vm_kern.h>
115 #include <ipc/ipc_kmsg.h>
116 #include <ipc/ipc_port.h>
117 #include <machine/thread.h> /* for MACHINE_STACK */
118 #include <kern/profile.h>
119 #include <kern/assert.h>
120 #include <sys/kdebug.h>
123 * Exported interfaces
126 #include <mach/thread_act_server.h>
127 #include <mach/mach_host_server.h>
129 static struct zone
*thread_zone
;
131 static queue_head_t reaper_queue
;
132 decl_simple_lock_data(static,reaper_lock
)
137 static struct thread thread_template
, init_thread
;
142 extern void stack_statistics(
143 unsigned int *totalp
,
144 vm_size_t
*maxusagep
);
145 #endif /* MACHINE_STACK */
146 #endif /* MACH_DEBUG */
150 * Machine-dependent code must define:
159 #else /* MACHINE_STACK */
161 * We allocate stacks from generic kernel VM.
162 * Machine-dependent code must define:
163 * machine_kernel_stack_init
165 * The stack_free_list can only be accessed at splsched,
166 * because stack_alloc_try/thread_invoke operate at splsched.
169 decl_simple_lock_data(static,stack_lock_data
)
170 #define stack_lock() simple_lock(&stack_lock_data)
171 #define stack_unlock() simple_unlock(&stack_lock_data)
173 static vm_map_t stack_map
;
174 static vm_offset_t stack_free_list
;
176 static vm_offset_t stack_free_cache
[NCPUS
];
178 unsigned int stack_free_max
= 0;
179 unsigned int stack_free_count
= 0; /* splsched only */
180 unsigned int stack_free_limit
= 1; /* Arbitrary */
182 unsigned int stack_cache_hits
= 0; /* debugging */
184 unsigned int stack_alloc_hits
= 0; /* debugging */
185 unsigned int stack_alloc_misses
= 0; /* debugging */
187 unsigned int stack_alloc_total
= 0;
188 unsigned int stack_alloc_hiwater
= 0;
189 unsigned int stack_alloc_bndry
= 0;
193 * The next field is at the base of the stack,
194 * so the low end is left unsullied.
197 #define stack_next(stack) (*((vm_offset_t *)((stack) + KERNEL_STACK_SIZE) - 1))
202 * Allocate a kernel stack for a thread.
208 void (*start_pos
)(thread_t
))
210 vm_offset_t stack
= thread
->kernel_stack
;
218 stack
= stack_free_list
;
220 stack_free_list
= stack_next(stack
);
227 machine_stack_attach(thread
, stack
, start_pos
);
231 if (kernel_memory_allocate(
233 KERNEL_STACK_SIZE
, stack_alloc_bndry
- 1,
234 KMA_KOBJECT
) != KERN_SUCCESS
)
235 panic("stack_alloc: no space left for stack maps");
238 if (stack_alloc_total
> stack_alloc_hiwater
)
239 stack_alloc_hiwater
= stack_alloc_total
;
241 machine_stack_attach(thread
, stack
, start_pos
);
248 * Free a kernel stack.
255 vm_offset_t stack
= machine_stack_detach(thread
);
258 if (stack
!= thread
->reserved_stack
) {
259 spl_t s
= splsched();
262 cache
= &stack_free_cache
[cpu_number()];
271 stack_next(stack
) = stack_free_list
;
272 stack_free_list
= stack
;
273 if (++stack_free_count
> stack_free_max
)
274 stack_free_max
= stack_free_count
;
284 spl_t s
= splsched();
287 cache
= &stack_free_cache
[cpu_number()];
296 stack_next(stack
) = stack_free_list
;
297 stack_free_list
= stack
;
298 if (++stack_free_count
> stack_free_max
)
299 stack_free_max
= stack_free_count
;
307 * Free excess kernel stacks.
314 spl_t s
= splsched();
317 while (stack_free_count
> stack_free_limit
) {
318 vm_offset_t stack
= stack_free_list
;
320 stack_free_list
= stack_next(stack
);
326 stack_map
, stack
, stack
+ KERNEL_STACK_SIZE
,
327 VM_MAP_REMOVE_KUNWIRE
) != KERN_SUCCESS
)
328 panic("stack_collect: vm_map_remove failed");
341 * Non-blocking attempt to allocate a kernel stack.
342 * Called at splsched with the thread locked.
345 boolean_t
stack_alloc_try(
347 void (*start
)(thread_t
))
349 register vm_offset_t stack
, *cache
;
351 cache
= &stack_free_cache
[cpu_number()];
352 if (stack
= *cache
) {
354 machine_stack_attach(thread
, stack
, start
);
361 stack
= stack_free_list
;
362 if (stack
!= (vm_offset_t
)0) {
363 stack_free_list
= stack_next(stack
);
369 stack
= thread
->reserved_stack
;
372 machine_stack_attach(thread
, stack
, start
);
378 stack_alloc_misses
++;
388 * Return statistics on cached kernel stacks.
389 * *maxusagep must be initialized by the caller.
394 unsigned int *totalp
,
395 vm_size_t
*maxusagep
)
402 *totalp
= stack_free_count
;
408 #endif /* MACH_DEBUG */
410 #endif /* MACHINE_STACK */
413 stack_fake_zone_info(int *count
, vm_size_t
*cur_size
, vm_size_t
*max_size
, vm_size_t
*elem_size
,
414 vm_size_t
*alloc_size
, int *collectable
, int *exhaustable
)
416 *count
= stack_alloc_total
- stack_free_count
;
417 *cur_size
= KERNEL_STACK_SIZE
* stack_alloc_total
;
418 *max_size
= KERNEL_STACK_SIZE
* stack_alloc_hiwater
;
419 *elem_size
= KERNEL_STACK_SIZE
;
420 *alloc_size
= KERNEL_STACK_SIZE
;
427 register thread_t thread
)
433 thread_bootstrap(void)
436 * Fill in a template thread for fast initialization.
439 thread_template
.runq
= RUN_QUEUE_NULL
;
441 thread_template
.ref_count
= 1;
443 thread_template
.reason
= AST_NONE
;
444 thread_template
.at_safe_point
= FALSE
;
445 thread_template
.wait_event
= NO_EVENT64
;
446 thread_template
.wait_queue
= WAIT_QUEUE_NULL
;
447 thread_template
.wait_result
= THREAD_WAITING
;
448 thread_template
.interrupt_level
= THREAD_ABORTSAFE
;
449 thread_template
.state
= TH_STACK_HANDOFF
| TH_WAIT
| TH_UNINT
;
450 thread_template
.wake_active
= FALSE
;
451 thread_template
.active_callout
= FALSE
;
452 thread_template
.continuation
= (void (*)(void))0;
453 thread_template
.top_act
= THR_ACT_NULL
;
455 thread_template
.importance
= 0;
456 thread_template
.sched_mode
= 0;
457 thread_template
.safe_mode
= 0;
459 thread_template
.priority
= 0;
460 thread_template
.sched_pri
= 0;
461 thread_template
.max_priority
= 0;
462 thread_template
.task_priority
= 0;
463 thread_template
.promotions
= 0;
464 thread_template
.pending_promoter_index
= 0;
465 thread_template
.pending_promoter
[0] =
466 thread_template
.pending_promoter
[1] = NULL
;
468 thread_template
.realtime
.deadline
= UINT64_MAX
;
470 thread_template
.current_quantum
= 0;
472 thread_template
.computation_metered
= 0;
473 thread_template
.computation_epoch
= 0;
475 thread_template
.cpu_usage
= 0;
476 thread_template
.cpu_delta
= 0;
477 thread_template
.sched_usage
= 0;
478 thread_template
.sched_delta
= 0;
479 thread_template
.sched_stamp
= 0;
480 thread_template
.sleep_stamp
= 0;
481 thread_template
.safe_release
= 0;
483 thread_template
.bound_processor
= PROCESSOR_NULL
;
484 thread_template
.last_processor
= PROCESSOR_NULL
;
485 thread_template
.last_switch
= 0;
487 thread_template
.vm_privilege
= FALSE
;
489 timer_init(&(thread_template
.user_timer
));
490 timer_init(&(thread_template
.system_timer
));
491 thread_template
.user_timer_save
.low
= 0;
492 thread_template
.user_timer_save
.high
= 0;
493 thread_template
.system_timer_save
.low
= 0;
494 thread_template
.system_timer_save
.high
= 0;
496 thread_template
.processor_set
= PROCESSOR_SET_NULL
;
498 thread_template
.act_ref_count
= 2;
500 thread_template
.special_handler
.handler
= special_handler
;
501 thread_template
.special_handler
.next
= 0;
504 thread_template
.may_assign
= TRUE
;
505 thread_template
.assign_active
= FALSE
;
506 #endif /* MACH_HOST */
507 thread_template
.funnel_lock
= THR_FUNNEL_NULL
;
508 thread_template
.funnel_state
= 0;
510 thread_template
.mutex_count
= 0;
511 #endif /* MACH_LDEBUG */
513 init_thread
= thread_template
;
515 init_thread
.top_act
= &init_thread
;
516 init_thread
.thread
= &init_thread
;
517 machine_thread_set_current(&init_thread
);
527 sizeof(struct thread
),
528 THREAD_MAX
* sizeof(struct thread
),
529 THREAD_CHUNK
* sizeof(struct thread
),
533 * Initialize other data structures used in
537 queue_init(&reaper_queue
);
538 simple_lock_init(&reaper_lock
, ETAP_THREAD_REAPER
);
540 #ifndef MACHINE_STACK
541 simple_lock_init(&stack_lock_data
, ETAP_THREAD_STACK
); /* Initialize the stack lock */
543 if (KERNEL_STACK_SIZE
< round_page_32(KERNEL_STACK_SIZE
)) { /* Kernel stacks must be multiples of pages */
544 panic("thread_init: kernel stack size (%08X) must be a multiple of page size (%08X)\n",
545 KERNEL_STACK_SIZE
, PAGE_SIZE
);
548 for(stack_alloc_bndry
= PAGE_SIZE
; stack_alloc_bndry
<= KERNEL_STACK_SIZE
; stack_alloc_bndry
<<= 1); /* Find next power of 2 above stack size */
550 ret
= kmem_suballoc(kernel_map
, /* Suballocate from the kernel map */
553 (stack_alloc_bndry
* (2*THREAD_MAX
+ 64)), /* Allocate enough for all of it */
554 FALSE
, /* Say not pageable so that it is wired */
555 TRUE
, /* Allocate from anywhere */
556 &stack_map
); /* Allocate a submap */
558 if(ret
!= KERN_SUCCESS
) { /* Did we get one? */
559 panic("thread_init: kmem_suballoc for stacks failed - ret = %d\n", ret
); /* Die */
561 stack
= vm_map_min(stack_map
); /* Make sure we skip the first hunk */
562 ret
= vm_map_enter(stack_map
, &stack
, PAGE_SIZE
, 0, /* Make sure there is nothing at the start */
563 0, /* Force it at start */
564 VM_OBJECT_NULL
, 0, /* No object yet */
566 VM_PROT_NONE
, /* Allow no access */
567 VM_PROT_NONE
, /* Allow no access */
568 VM_INHERIT_DEFAULT
); /* Just be normal */
570 if(ret
!= KERN_SUCCESS
) { /* Did it work? */
571 panic("thread_init: dummy alignment allocation failed; ret = %d\n", ret
);
574 #endif /* MACHINE_STACK */
577 * Initialize any machine-dependent
578 * per-thread structures necessary.
580 machine_thread_init();
584 * Called at splsched.
587 thread_reaper_enqueue(
590 simple_lock(&reaper_lock
);
591 enqueue_tail(&reaper_queue
, (queue_entry_t
)thread
);
592 simple_unlock(&reaper_lock
);
594 thread_wakeup((event_t
)&reaper_queue
);
598 thread_termination_continue(void)
600 panic("thread_termination_continue");
605 * Routine: thread_terminate_self
607 * This routine is called by a thread which has unwound from
608 * its current RPC and kernel contexts and found that it's
609 * root activation has been marked for extinction. This lets
610 * it clean up the last few things that can only be cleaned
611 * up in this context and then impale itself on the reaper
614 * When the reaper gets the thread, it will deallocate the
615 * thread_act's reference on itself, which in turn will release
616 * its own reference on this thread. By doing things in that
617 * order, a thread_act will always have a valid thread - but the
618 * thread may persist beyond having a thread_act (but must never
622 thread_terminate_self(void)
624 thread_act_t thr_act
= current_act();
626 task_t task
= thr_act
->task
;
631 * We should be at the base of the inheritance chain.
633 thread
= act_lock_thread(thr_act
);
634 assert(thr_act
->thread
== thread
);
636 /* This will allow no more control ops on this thr_act. */
637 ipc_thr_act_disable(thr_act
);
639 /* Clean-up any ulocks that are still owned by the thread
640 * activation (acquired but not released or handed-off).
642 act_ulock_release_all(thr_act
);
644 act_unlock_thread(thr_act
);
646 _mk_sp_thread_depress_abort(thread
, TRUE
);
649 * Check to see if this is the last active activation. By
650 * this we mean the last activation to call thread_terminate_self.
651 * If so, and the task is associated with a BSD process, we
652 * need to call BSD and let them clean up.
654 active_acts
= hw_atomic_sub(&task
->active_thread_count
, 1);
656 if (active_acts
== 0 && task
->bsd_info
)
657 proc_exit(task
->bsd_info
);
659 /* JMM - for now, no migration */
660 assert(!thr_act
->lower
);
662 thread_timer_terminate();
664 ipc_thread_terminate(thread
);
668 thread
->state
|= TH_TERMINATE
;
669 assert((thread
->state
& TH_UNINT
) == 0);
670 thread_mark_wait_locked(thread
, THREAD_UNINT
);
671 assert(thread
->promotions
== 0);
672 thread_unlock(thread
);
675 ETAP_SET_REASON(thread
, BLOCKED_ON_TERMINATION
);
676 thread_block(thread_termination_continue
);
681 * Create a new thread.
682 * Doesn't start the thread running.
685 thread_create_internal(
689 thread_t
*out_thread
)
692 processor_set_t pset
;
693 static thread_t first_thread
;
696 * Allocate a thread and initialize static fields
698 if (first_thread
== NULL
)
699 new_thread
= first_thread
= current_act();
701 new_thread
= (thread_t
)zalloc(thread_zone
);
702 if (new_thread
== NULL
)
703 return (KERN_RESOURCE_SHORTAGE
);
705 if (new_thread
!= first_thread
)
706 *new_thread
= thread_template
;
710 extern void *uthread_alloc(task_t
, thread_act_t
);
712 new_thread
->uthread
= uthread_alloc(parent_task
, new_thread
);
713 if (new_thread
->uthread
== NULL
) {
714 zfree(thread_zone
, (vm_offset_t
)new_thread
);
715 return (KERN_RESOURCE_SHORTAGE
);
718 #endif /* MACH_BSD */
720 if (machine_thread_create(new_thread
, parent_task
) != KERN_SUCCESS
) {
723 extern void uthread_free(task_t
, void *, void *);
724 void *ut
= new_thread
->uthread
;
726 new_thread
->uthread
= NULL
;
727 uthread_free(parent_task
, ut
, parent_task
->bsd_info
);
729 #endif /* MACH_BSD */
730 zfree(thread_zone
, (vm_offset_t
)new_thread
);
731 return (KERN_FAILURE
);
734 new_thread
->task
= parent_task
;
736 thread_lock_init(new_thread
);
737 wake_lock_init(new_thread
);
739 mutex_init(&new_thread
->lock
, ETAP_THREAD_ACT
);
741 ipc_thr_act_init(parent_task
, new_thread
);
743 ipc_thread_init(new_thread
);
744 queue_init(&new_thread
->held_ulocks
);
745 act_prof_init(new_thread
, parent_task
);
747 new_thread
->continuation
= start
;
748 new_thread
->sleep_stamp
= sched_tick
;
750 pset
= parent_task
->processor_set
;
751 assert(pset
== &default_pset
);
754 task_lock(parent_task
);
755 assert(parent_task
->processor_set
== pset
);
757 if ( !parent_task
->active
||
758 (parent_task
->thread_count
>= THREAD_MAX
&&
759 parent_task
!= kernel_task
)) {
760 task_unlock(parent_task
);
765 extern void uthread_free(task_t
, void *, void *);
766 void *ut
= new_thread
->uthread
;
768 new_thread
->uthread
= NULL
;
769 uthread_free(parent_task
, ut
, parent_task
->bsd_info
);
771 #endif /* MACH_BSD */
772 act_prof_deallocate(new_thread
);
773 ipc_thr_act_terminate(new_thread
);
774 machine_thread_destroy(new_thread
);
775 zfree(thread_zone
, (vm_offset_t
) new_thread
);
776 return (KERN_FAILURE
);
779 act_attach(new_thread
, new_thread
);
781 task_reference_locked(parent_task
);
783 /* Cache the task's map */
784 new_thread
->map
= parent_task
->map
;
786 /* Chain the thread onto the task's list */
787 queue_enter(&parent_task
->threads
, new_thread
, thread_act_t
, task_threads
);
788 parent_task
->thread_count
++;
789 parent_task
->res_thread_count
++;
791 /* So terminating threads don't need to take the task lock to decrement */
792 hw_atomic_add(&parent_task
->active_thread_count
, 1);
794 /* Associate the thread with the processor set */
795 pset_add_thread(pset
, new_thread
);
797 thread_timer_setup(new_thread
);
799 /* Set the thread's scheduling parameters */
800 if (parent_task
!= kernel_task
)
801 new_thread
->sched_mode
|= TH_MODE_TIMESHARE
;
802 new_thread
->max_priority
= parent_task
->max_priority
;
803 new_thread
->task_priority
= parent_task
->priority
;
804 new_thread
->priority
= (priority
< 0)? parent_task
->priority
: priority
;
805 if (new_thread
->priority
> new_thread
->max_priority
)
806 new_thread
->priority
= new_thread
->max_priority
;
807 new_thread
->importance
=
808 new_thread
->priority
- new_thread
->task_priority
;
809 new_thread
->sched_stamp
= sched_tick
;
810 compute_priority(new_thread
, FALSE
);
812 #if ETAP_EVENT_MONITOR
813 new_thread
->etap_reason
= 0;
814 new_thread
->etap_trace
= FALSE
;
815 #endif /* ETAP_EVENT_MONITOR */
817 new_thread
->active
= TRUE
;
819 *out_thread
= new_thread
;
822 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
824 kdbg_trace_data(parent_task
->bsd_info
, &dbg_arg2
);
826 KERNEL_DEBUG_CONSTANT(
827 TRACEDBG_CODE(DBG_TRACE_DATA
, 1) | DBG_FUNC_NONE
,
828 (vm_address_t
)new_thread
, dbg_arg2
, 0, 0, 0);
830 kdbg_trace_string(parent_task
->bsd_info
,
831 &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
833 KERNEL_DEBUG_CONSTANT(
834 TRACEDBG_CODE(DBG_TRACE_STRING
, 1) | DBG_FUNC_NONE
,
835 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
838 return (KERN_SUCCESS
);
841 extern void thread_bootstrap_return(void);
846 thread_act_t
*new_thread
)
848 kern_return_t result
;
851 if (task
== TASK_NULL
|| task
== kernel_task
)
852 return (KERN_INVALID_ARGUMENT
);
854 result
= thread_create_internal(task
, -1, thread_bootstrap_return
, &thread
);
855 if (result
!= KERN_SUCCESS
)
858 thread
->user_stop_count
= 1;
860 if (task
->suspend_count
> 0)
863 pset_unlock(task
->processor_set
);
866 *new_thread
= thread
;
868 return (KERN_SUCCESS
);
872 thread_create_running(
873 register task_t task
,
875 thread_state_t new_state
,
876 mach_msg_type_number_t new_state_count
,
877 thread_act_t
*new_thread
)
879 register kern_return_t result
;
882 if (task
== TASK_NULL
|| task
== kernel_task
)
883 return (KERN_INVALID_ARGUMENT
);
885 result
= thread_create_internal(task
, -1, thread_bootstrap_return
, &thread
);
886 if (result
!= KERN_SUCCESS
)
889 result
= machine_thread_set_state(thread
, flavor
, new_state
, new_state_count
);
890 if (result
!= KERN_SUCCESS
) {
891 pset_unlock(task
->processor_set
);
894 thread_terminate(thread
);
895 act_deallocate(thread
);
900 clear_wait(thread
, THREAD_AWAKENED
);
901 thread
->started
= TRUE
;
903 pset_unlock(task
->processor_set
);
906 *new_thread
= thread
;
914 * Create a thread in the kernel task
915 * to execute in kernel context.
918 kernel_thread_create(
922 kern_return_t result
;
923 task_t task
= kernel_task
;
926 result
= thread_create_internal(task
, priority
, start
, &thread
);
927 if (result
!= KERN_SUCCESS
)
928 return (THREAD_NULL
);
930 pset_unlock(task
->processor_set
);
933 thread_doswapin(thread
);
934 assert(thread
->kernel_stack
!= 0);
935 thread
->reserved_stack
= thread
->kernel_stack
;
937 act_deallocate(thread
);
943 kernel_thread_with_priority(
949 thread
= kernel_thread_create(start
, priority
);
950 if (thread
== THREAD_NULL
)
951 return (THREAD_NULL
);
954 clear_wait(thread
, THREAD_AWAKENED
);
955 thread
->started
= TRUE
;
959 thread_bind(thread
, master_processor
);
969 if (task
!= kernel_task
)
970 panic("kernel_thread");
972 return kernel_thread_with_priority(start
, -1);
975 unsigned int c_weird_pset_ref_exit
= 0; /* pset code raced us */
978 /* Preclude thread processor set assignement */
979 #define thread_freeze(thread) assert((thread)->processor_set == &default_pset)
981 /* Allow thread processor set assignement */
982 #define thread_unfreeze(thread) assert((thread)->processor_set == &default_pset)
984 #endif /* MACH_HOST */
991 processor_set_t pset
;
995 if (thread
== THREAD_NULL
)
999 * First, check for new count > 0 (the common case).
1000 * Only the thread needs to be locked.
1003 thread_lock(thread
);
1004 refs
= --thread
->ref_count
;
1005 thread_unlock(thread
);
1011 if (thread
== current_thread())
1012 panic("thread_deallocate");
1015 * There is a dangling pointer to the thread from the
1016 * processor_set. To clean it up, we freeze the thread
1017 * in the pset (because pset destruction can cause even
1018 * reference-less threads to be reassigned to the default
1019 * pset) and then remove it.
1023 thread_freeze(thread
);
1026 pset
= thread
->processor_set
;
1028 pset_remove_thread(pset
, thread
);
1032 thread_unfreeze(thread
);
1035 pset_deallocate(pset
);
1037 if (thread
->reserved_stack
!= 0) {
1038 if (thread
->reserved_stack
!= thread
->kernel_stack
)
1039 stack_free_stack(thread
->reserved_stack
);
1040 thread
->reserved_stack
= 0;
1043 if (thread
->kernel_stack
!= 0)
1046 machine_thread_destroy(thread
);
1048 zfree(thread_zone
, (vm_offset_t
) thread
);
1057 if (thread
== THREAD_NULL
)
1061 thread_lock(thread
);
1062 thread_reference_locked(thread
);
1063 thread_unlock(thread
);
1068 * Called with "appropriate" thread-related locks held on
1069 * thread and its top_act for synchrony with RPC (see
1070 * act_lock_thread()).
1073 thread_info_shuttle(
1074 register thread_act_t thr_act
,
1075 thread_flavor_t flavor
,
1076 thread_info_t thread_info_out
, /* ptr to OUT array */
1077 mach_msg_type_number_t
*thread_info_count
) /*IN/OUT*/
1079 register thread_t thread
= thr_act
->thread
;
1083 if (thread
== THREAD_NULL
)
1084 return (KERN_INVALID_ARGUMENT
);
1086 if (flavor
== THREAD_BASIC_INFO
) {
1087 register thread_basic_info_t basic_info
;
1089 if (*thread_info_count
< THREAD_BASIC_INFO_COUNT
)
1090 return (KERN_INVALID_ARGUMENT
);
1092 basic_info
= (thread_basic_info_t
) thread_info_out
;
1095 thread_lock(thread
);
1099 thread_read_times(thread
, &basic_info
->user_time
,
1100 &basic_info
->system_time
);
1103 * Update lazy-evaluated scheduler info because someone wants it.
1105 if (thread
->sched_stamp
!= sched_tick
)
1106 update_priority(thread
);
1108 basic_info
->sleep_time
= 0;
1111 * To calculate cpu_usage, first correct for timer rate,
1112 * then for 5/8 ageing. The correction factor [3/5] is
1115 basic_info
->cpu_usage
= (thread
->cpu_usage
<< SCHED_TICK_SHIFT
) /
1116 (TIMER_RATE
/ TH_USAGE_SCALE
);
1117 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 3) / 5;
1120 * Clock drift compensation.
1122 basic_info
->cpu_usage
= (basic_info
->cpu_usage
* 1000000) / sched_usec
;
1123 #endif /* SIMPLE_CLOCK */
1125 basic_info
->policy
= ((thread
->sched_mode
& TH_MODE_TIMESHARE
)?
1126 POLICY_TIMESHARE
: POLICY_RR
);
1129 if (thread
->state
& TH_IDLE
)
1130 flags
|= TH_FLAGS_IDLE
;
1132 if (thread
->state
& TH_STACK_HANDOFF
)
1133 flags
|= TH_FLAGS_SWAPPED
;
1136 if (thread
->state
& TH_TERMINATE
)
1137 state
= TH_STATE_HALTED
;
1139 if (thread
->state
& TH_RUN
)
1140 state
= TH_STATE_RUNNING
;
1142 if (thread
->state
& TH_UNINT
)
1143 state
= TH_STATE_UNINTERRUPTIBLE
;
1145 if (thread
->state
& TH_SUSP
)
1146 state
= TH_STATE_STOPPED
;
1148 if (thread
->state
& TH_WAIT
)
1149 state
= TH_STATE_WAITING
;
1151 basic_info
->run_state
= state
;
1152 basic_info
->flags
= flags
;
1154 basic_info
->suspend_count
= thr_act
->user_stop_count
;
1156 thread_unlock(thread
);
1159 *thread_info_count
= THREAD_BASIC_INFO_COUNT
;
1161 return (KERN_SUCCESS
);
1164 if (flavor
== THREAD_SCHED_TIMESHARE_INFO
) {
1165 policy_timeshare_info_t ts_info
;
1167 if (*thread_info_count
< POLICY_TIMESHARE_INFO_COUNT
)
1168 return (KERN_INVALID_ARGUMENT
);
1170 ts_info
= (policy_timeshare_info_t
)thread_info_out
;
1173 thread_lock(thread
);
1175 if (!(thread
->sched_mode
& TH_MODE_TIMESHARE
)) {
1176 thread_unlock(thread
);
1179 return (KERN_INVALID_POLICY
);
1182 ts_info
->depressed
= (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) != 0;
1183 if (ts_info
->depressed
) {
1184 ts_info
->base_priority
= DEPRESSPRI
;
1185 ts_info
->depress_priority
= thread
->priority
;
1188 ts_info
->base_priority
= thread
->priority
;
1189 ts_info
->depress_priority
= -1;
1192 ts_info
->cur_priority
= thread
->sched_pri
;
1193 ts_info
->max_priority
= thread
->max_priority
;
1195 thread_unlock(thread
);
1198 *thread_info_count
= POLICY_TIMESHARE_INFO_COUNT
;
1200 return (KERN_SUCCESS
);
1203 if (flavor
== THREAD_SCHED_FIFO_INFO
) {
1204 if (*thread_info_count
< POLICY_FIFO_INFO_COUNT
)
1205 return (KERN_INVALID_ARGUMENT
);
1207 return (KERN_INVALID_POLICY
);
1210 if (flavor
== THREAD_SCHED_RR_INFO
) {
1211 policy_rr_info_t rr_info
;
1213 if (*thread_info_count
< POLICY_RR_INFO_COUNT
)
1214 return (KERN_INVALID_ARGUMENT
);
1216 rr_info
= (policy_rr_info_t
) thread_info_out
;
1219 thread_lock(thread
);
1221 if (thread
->sched_mode
& TH_MODE_TIMESHARE
) {
1222 thread_unlock(thread
);
1225 return (KERN_INVALID_POLICY
);
1228 rr_info
->depressed
= (thread
->sched_mode
& TH_MODE_ISDEPRESSED
) != 0;
1229 if (rr_info
->depressed
) {
1230 rr_info
->base_priority
= DEPRESSPRI
;
1231 rr_info
->depress_priority
= thread
->priority
;
1234 rr_info
->base_priority
= thread
->priority
;
1235 rr_info
->depress_priority
= -1;
1238 rr_info
->max_priority
= thread
->max_priority
;
1239 rr_info
->quantum
= std_quantum_us
/ 1000;
1241 thread_unlock(thread
);
1244 *thread_info_count
= POLICY_RR_INFO_COUNT
;
1246 return (KERN_SUCCESS
);
1249 return (KERN_INVALID_ARGUMENT
);
1254 register thread_t thread
)
1256 thread_act_t thr_act
;
1259 thr_act
= thread_lock_act(thread
);
1260 assert(thr_act
&& thr_act
->thread
== thread
);
1262 act_reference_locked(thr_act
);
1265 * Replace `act_unlock_thread()' with individual
1266 * calls. (`act_detach()' can change fields used
1267 * to determine which locks are held, confusing
1268 * `act_unlock_thread()'.)
1270 act_unlock(thr_act
);
1272 /* Remove the reference held by a rooted thread */
1273 act_deallocate(thr_act
);
1275 /* Remove the reference held by the thread: */
1276 act_deallocate(thr_act
);
1282 * This kernel thread runs forever looking for terminating
1283 * threads, releasing their "self" references.
1286 reaper_thread_continue(void)
1288 register thread_t thread
;
1291 simple_lock(&reaper_lock
);
1293 while ((thread
= (thread_t
) dequeue_head(&reaper_queue
)) != THREAD_NULL
) {
1294 simple_unlock(&reaper_lock
);
1297 thread_doreap(thread
);
1300 simple_lock(&reaper_lock
);
1303 assert_wait((event_t
)&reaper_queue
, THREAD_UNINT
);
1304 simple_unlock(&reaper_lock
);
1307 thread_block(reaper_thread_continue
);
1314 reaper_thread_continue();
1319 thread_reaper_init(void)
1321 kernel_thread_with_priority(reaper_thread
, MINPRI_KERNEL
);
1326 thread_act_t thr_act
,
1327 processor_set_t new_pset
)
1329 return(KERN_FAILURE
);
1333 * thread_assign_default:
1335 * Special version of thread_assign for assigning threads to default
1339 thread_assign_default(
1340 thread_act_t thr_act
)
1342 return (thread_assign(thr_act
, &default_pset
));
1346 * thread_get_assignment
1348 * Return current assignment for this thread.
1351 thread_get_assignment(
1352 thread_act_t thr_act
,
1353 processor_set_t
*pset
)
1357 if (thr_act
== THR_ACT_NULL
)
1358 return(KERN_INVALID_ARGUMENT
);
1359 thread
= act_lock_thread(thr_act
);
1360 if (thread
== THREAD_NULL
) {
1361 act_unlock_thread(thr_act
);
1362 return(KERN_INVALID_ARGUMENT
);
1364 *pset
= thread
->processor_set
;
1365 act_unlock_thread(thr_act
);
1366 pset_reference(*pset
);
1367 return(KERN_SUCCESS
);
1371 * thread_wire_internal:
1373 * Specify that the target thread must always be able
1374 * to run and to allocate memory.
1377 thread_wire_internal(
1378 host_priv_t host_priv
,
1379 thread_act_t thr_act
,
1381 boolean_t
*prev_state
)
1385 extern void vm_page_free_reserve(int pages
);
1387 if (thr_act
== THR_ACT_NULL
|| host_priv
== HOST_PRIV_NULL
)
1388 return (KERN_INVALID_ARGUMENT
);
1390 assert(host_priv
== &realhost
);
1392 thread
= act_lock_thread(thr_act
);
1393 if (thread
==THREAD_NULL
) {
1394 act_unlock_thread(thr_act
);
1395 return(KERN_INVALID_ARGUMENT
);
1399 * This implementation only works for the current thread.
1401 if (thr_act
!= current_act())
1402 return KERN_INVALID_ARGUMENT
;
1405 thread_lock(thread
);
1408 *prev_state
= thread
->vm_privilege
;
1412 if (thread
->vm_privilege
== FALSE
)
1413 vm_page_free_reserve(1); /* XXX */
1414 thread
->vm_privilege
= TRUE
;
1416 if (thread
->vm_privilege
== TRUE
)
1417 vm_page_free_reserve(-1); /* XXX */
1418 thread
->vm_privilege
= FALSE
;
1421 thread_unlock(thread
);
1423 act_unlock_thread(thr_act
);
1425 return KERN_SUCCESS
;
1432 * User-api wrapper for thread_wire_internal()
1436 host_priv_t host_priv
,
1437 thread_act_t thr_act
,
1441 return thread_wire_internal(host_priv
, thr_act
, wired
, NULL
);
1447 vm_size_t
*reservedp
,
1448 unsigned int *totalp
,
1450 vm_size_t
*residentp
,
1451 vm_size_t
*maxusagep
,
1452 vm_offset_t
*maxstackp
)
1455 return KERN_NOT_SUPPORTED
;
1460 if (host
== HOST_NULL
)
1461 return KERN_INVALID_HOST
;
1465 stack_statistics(&total
, &maxusage
);
1469 *spacep
= *residentp
= total
* round_page_32(KERNEL_STACK_SIZE
);
1470 *maxusagep
= maxusage
;
1472 return KERN_SUCCESS
;
1474 #endif /* MACH_DEBUG */
1478 * Return info on stack usage for threads in a specific processor set
1481 processor_set_stack_usage(
1482 processor_set_t pset
,
1483 unsigned int *totalp
,
1485 vm_size_t
*residentp
,
1486 vm_size_t
*maxusagep
,
1487 vm_offset_t
*maxstackp
)
1490 return KERN_NOT_SUPPORTED
;
1494 vm_offset_t maxstack
;
1496 register thread_t
*threads
;
1497 register thread_t thread
;
1499 unsigned int actual
; /* this many things */
1502 vm_size_t size
, size_needed
;
1507 if (pset
== PROCESSOR_SET_NULL
)
1508 return KERN_INVALID_ARGUMENT
;
1514 if (!pset
->active
) {
1516 return KERN_INVALID_ARGUMENT
;
1519 actual
= pset
->thread_count
;
1521 /* do we have the memory we need? */
1523 size_needed
= actual
* sizeof(thread_t
);
1524 if (size_needed
<= size
)
1527 /* unlock the pset and allocate more memory */
1533 assert(size_needed
> 0);
1536 addr
= kalloc(size
);
1538 return KERN_RESOURCE_SHORTAGE
;
1541 /* OK, have memory and the processor_set is locked & active */
1543 threads
= (thread_t
*) addr
;
1544 for (i
= 0, thread
= (thread_t
) queue_first(&pset
->threads
);
1545 !queue_end(&pset
->threads
, (queue_entry_t
) thread
);
1546 thread
= (thread_t
) queue_next(&thread
->pset_threads
)) {
1547 thread_lock(thread
);
1548 if (thread
->ref_count
> 0) {
1549 thread_reference_locked(thread
);
1550 threads
[i
++] = thread
;
1552 thread_unlock(thread
);
1555 assert(i
<= actual
);
1557 /* can unlock processor set now that we have the thread refs */
1560 /* calculate maxusage and free thread references */
1566 thread_t thread
= threads
[--i
];
1568 if (thread
->kernel_stack
!= 0)
1571 thread_deallocate(thread
);
1578 *residentp
= *spacep
= total
* round_page_32(KERNEL_STACK_SIZE
);
1579 *maxusagep
= maxusage
;
1580 *maxstackp
= maxstack
;
1581 return KERN_SUCCESS
;
1583 #endif /* MACH_DEBUG */
1586 int split_funnel_off
= 0;
1593 if ((fnl
= (funnel_t
*)kalloc(sizeof(funnel_t
))) != 0){
1594 bzero((void *)fnl
, sizeof(funnel_t
));
1595 if ((m
= mutex_alloc(0)) == (mutex_t
*)NULL
) {
1596 kfree((vm_offset_t
)fnl
, sizeof(funnel_t
));
1597 return(THR_FUNNEL_NULL
);
1600 fnl
->fnl_type
= type
;
1609 mutex_free(fnl
->fnl_mutex
);
1610 if (fnl
->fnl_oldmutex
)
1611 mutex_free(fnl
->fnl_oldmutex
);
1612 kfree((vm_offset_t
)fnl
, sizeof(funnel_t
));
1624 fnl
->fnl_mtxholder
= current_thread();
1625 if (split_funnel_off
&& (m
!= fnl
->fnl_mutex
)) {
1636 mutex_unlock(fnl
->fnl_mutex
);
1637 fnl
->fnl_mtxrelease
= current_thread();
1640 int refunnel_hint_enabled
= 0;
1645 wait_result_t wresult
)
1647 if ( !(thread
->funnel_state
& TH_FN_REFUNNEL
) ||
1648 wresult
!= THREAD_AWAKENED
)
1651 if (!refunnel_hint_enabled
)
1654 return (mutex_preblock(thread
->funnel_lock
->fnl_mutex
, thread
));
1661 thread_t th
= current_thread();
1663 if (th
->funnel_state
& TH_FN_OWNED
) {
1664 return(th
->funnel_lock
);
1666 return(THR_FUNNEL_NULL
);
1674 thread_t cur_thread
;
1675 boolean_t funnel_state_prev
;
1678 cur_thread
= current_thread();
1679 funnel_state_prev
= ((cur_thread
->funnel_state
& TH_FN_OWNED
) == TH_FN_OWNED
);
1681 if (funnel_state_prev
!= funneled
) {
1682 intr
= ml_set_interrupts_enabled(FALSE
);
1684 if (funneled
== TRUE
) {
1685 if (cur_thread
->funnel_lock
)
1686 panic("Funnel lock called when holding one %x", cur_thread
->funnel_lock
);
1687 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
,
1690 KERNEL_DEBUG(0x6032434 | DBG_FUNC_NONE
,
1692 cur_thread
->funnel_state
|= TH_FN_OWNED
;
1693 cur_thread
->funnel_lock
= fnl
;
1695 if(cur_thread
->funnel_lock
->fnl_mutex
!= fnl
->fnl_mutex
)
1696 panic("Funnel unlock when not holding funnel");
1697 cur_thread
->funnel_state
&= ~TH_FN_OWNED
;
1698 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
,
1701 cur_thread
->funnel_lock
= THR_FUNNEL_NULL
;
1704 (void)ml_set_interrupts_enabled(intr
);
1706 /* if we are trying to acquire funnel recursively
1707 * check for funnel to be held already
1709 if (funneled
&& (fnl
->fnl_mutex
!= cur_thread
->funnel_lock
->fnl_mutex
)) {
1710 panic("thread_funnel_set: already holding a different funnel");
1713 return(funnel_state_prev
);
1717 thread_funnel_merge(
1719 funnel_t
* otherfnl
)
1724 extern int disable_funnel
;
1726 if ((gfnl
= thread_funnel_get()) == THR_FUNNEL_NULL
)
1727 panic("thread_funnel_merge called with no funnels held");
1729 if (gfnl
->fnl_type
!= 1)
1730 panic("thread_funnel_merge called from non kernel funnel");
1733 panic("thread_funnel_merge incorrect invocation");
1735 if (disable_funnel
|| split_funnel_off
)
1736 return (KERN_FAILURE
);
1739 otherm
= otherfnl
->fnl_mutex
;
1741 /* Acquire other funnel mutex */
1743 split_funnel_off
= 1;
1745 otherfnl
->fnl_mutex
= m
;
1746 otherfnl
->fnl_type
= fnl
->fnl_type
;
1747 otherfnl
->fnl_oldmutex
= otherm
; /* save this for future use */
1749 mutex_unlock(otherm
);
1750 return(KERN_SUCCESS
);
1754 thread_set_cont_arg(
1757 thread_t self
= current_thread();
1759 self
->saved
.misc
= arg
;
1763 thread_get_cont_arg(void)
1765 thread_t self
= current_thread();
1767 return (self
->saved
.misc
);
1771 * Export routines to other components for things that are done as macros
1772 * within the osfmk component.
1774 #undef thread_should_halt
1779 return(thread_should_halt_fast(th
));
1782 vm_offset_t
min_valid_stack_address(void)
1784 return vm_map_min(stack_map
);
1787 vm_offset_t
max_valid_stack_address(void)
1789 return vm_map_max(stack_map
);