2 * Copyright (c) 2003-2004 Apple Computer, 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 * Kernel stack management routines.
32 #include <mach/mach_host.h>
33 #include <mach/mach_types.h>
34 #include <mach/processor_set.h>
36 #include <kern/kern_types.h>
37 #include <kern/mach_param.h>
38 #include <kern/processor.h>
39 #include <kern/thread.h>
40 #include <kern/zalloc.h>
41 #include <kern/kalloc.h>
43 #include <vm/vm_map.h>
44 #include <vm/vm_kern.h>
46 #include <mach_debug.h>
49 * We allocate stacks from generic kernel VM.
51 * The stack_free_list can only be accessed at splsched,
52 * because stack_alloc_try/thread_invoke operate at splsched.
55 decl_simple_lock_data(static,stack_lock_data
)
56 #define stack_lock() simple_lock(&stack_lock_data)
57 #define stack_unlock() simple_unlock(&stack_lock_data)
59 #define STACK_CACHE_SIZE 2
61 static vm_map_t stack_map
;
62 static vm_offset_t stack_free_list
;
64 static unsigned int stack_free_count
, stack_free_hiwat
; /* free list count */
65 static unsigned int stack_total
, stack_hiwat
; /* current total count */
67 static unsigned int stack_free_target
;
68 static int stack_free_delta
;
70 static unsigned int stack_new_count
; /* total new stack allocations */
72 static vm_offset_t stack_addr_mask
;
75 * The next field is at the base of the stack,
76 * so the low end is left unsullied.
78 #define stack_next(stack) \
79 (*((vm_offset_t *)((stack) + KERNEL_STACK_SIZE) - 1))
84 vm_offset_t stacks
, boundary
;
85 vm_map_offset_t map_addr
;
87 simple_lock_init(&stack_lock_data
, 0);
89 if (KERNEL_STACK_SIZE
< round_page(KERNEL_STACK_SIZE
))
90 panic("stack_init: stack size %d not a multiple of page size %d\n", KERNEL_STACK_SIZE
, PAGE_SIZE
);
92 for (boundary
= PAGE_SIZE
; boundary
<= KERNEL_STACK_SIZE
; )
95 stack_addr_mask
= boundary
- 1;
97 if (kmem_suballoc(kernel_map
, &stacks
, (boundary
* (2 * THREAD_MAX
+ 64)),
98 FALSE
, VM_FLAGS_ANYWHERE
, &stack_map
) != KERN_SUCCESS
)
99 panic("stack_init: kmem_suballoc");
101 map_addr
= vm_map_min(stack_map
);
102 if (vm_map_enter(stack_map
, &map_addr
, vm_map_round_page(PAGE_SIZE
), 0, VM_FLAGS_FIXED
,
103 VM_OBJECT_NULL
, 0, FALSE
, VM_PROT_NONE
, VM_PROT_NONE
, VM_INHERIT_DEFAULT
) != KERN_SUCCESS
)
104 panic("stack_init: vm_map_enter");
110 * Allocate a stack for a thread, may
120 assert(thread
->kernel_stack
== 0);
124 stack
= stack_free_list
;
126 stack_free_list
= stack_next(stack
);
130 if (++stack_total
> stack_hiwat
)
131 stack_hiwat
= stack_total
;
139 if (kernel_memory_allocate(stack_map
, &stack
, KERNEL_STACK_SIZE
, stack_addr_mask
, KMA_KOBJECT
) != KERN_SUCCESS
)
140 panic("stack_alloc: kernel_memory_allocate");
143 machine_stack_attach(thread
, stack
);
149 * Detach and free the stack for a thread.
155 vm_offset_t stack
= machine_stack_detach(thread
);
158 if (stack
!= thread
->reserved_stack
) {
159 struct stack_cache
*cache
;
163 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
164 if (cache
->count
< STACK_CACHE_SIZE
) {
165 stack_next(stack
) = cache
->free
;
171 stack_next(stack
) = stack_free_list
;
172 stack_free_list
= stack
;
173 if (++stack_free_count
> stack_free_hiwat
)
174 stack_free_hiwat
= stack_free_count
;
186 struct stack_cache
*cache
;
190 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
191 if (cache
->count
< STACK_CACHE_SIZE
) {
192 stack_next(stack
) = cache
->free
;
198 stack_next(stack
) = stack_free_list
;
199 stack_free_list
= stack
;
200 if (++stack_free_count
> stack_free_hiwat
)
201 stack_free_hiwat
= stack_free_count
;
211 * Non-blocking attempt to allocate a
212 * stack for a thread.
214 * Returns TRUE on success.
216 * Called at splsched.
222 struct stack_cache
*cache
;
225 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
228 cache
->free
= stack_next(stack
);
232 if (stack_free_list
!= 0) {
234 stack
= stack_free_list
;
236 stack_free_list
= stack_next(stack
);
244 if (stack
!= 0 || (stack
= thread
->reserved_stack
) != 0) {
245 machine_stack_attach(thread
, stack
);
252 static unsigned int stack_collect_tick
, last_stack_tick
;
257 * Free excess kernel stacks, may
263 if (stack_collect_tick
!= last_stack_tick
) {
271 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
272 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
274 while (stack_free_count
> target
) {
275 stack
= stack_free_list
;
276 stack_free_list
= stack_next(stack
);
277 stack_free_count
--; stack_total
--;
281 if (vm_map_remove(stack_map
, vm_map_trunc_page(stack
),
282 vm_map_round_page(stack
+ KERNEL_STACK_SIZE
), VM_MAP_REMOVE_KUNWIRE
) != KERN_SUCCESS
)
283 panic("stack_collect: vm_map_remove");
288 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
289 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
292 last_stack_tick
= stack_collect_tick
;
300 * compute_stack_target:
302 * Computes a new target free list count
303 * based on recent alloc / free activity.
305 * Limits stack collection to once per
306 * computation period.
309 compute_stack_target(
317 if (stack_free_target
> 5)
318 stack_free_target
= (4 * stack_free_target
) / 5;
320 if (stack_free_target
> 0)
323 stack_free_target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
325 stack_free_delta
= 0;
326 stack_collect_tick
++;
333 stack_fake_zone_info(int *count
, vm_size_t
*cur_size
, vm_size_t
*max_size
, vm_size_t
*elem_size
,
334 vm_size_t
*alloc_size
, int *collectable
, int *exhaustable
)
336 unsigned int total
, hiwat
, free
;
343 free
= stack_free_count
;
347 *count
= total
- free
;
348 *cur_size
= KERNEL_STACK_SIZE
* total
;
349 *max_size
= KERNEL_STACK_SIZE
* hiwat
;
350 *elem_size
= KERNEL_STACK_SIZE
;
351 *alloc_size
= KERNEL_STACK_SIZE
;
357 void stack_privilege(
362 __unused thread_t thread
)
368 * Return info on stack usage for threads in a specific processor set
371 processor_set_stack_usage(
372 processor_set_t pset
,
373 unsigned int *totalp
,
375 vm_size_t
*residentp
,
376 vm_size_t
*maxusagep
,
377 vm_offset_t
*maxstackp
)
380 return KERN_NOT_SUPPORTED
;
384 vm_offset_t maxstack
;
386 register thread_t
*threads
;
387 register thread_t thread
;
389 unsigned int actual
; /* this many things */
392 vm_size_t size
, size_needed
;
395 if (pset
== PROCESSOR_SET_NULL
)
396 return KERN_INVALID_ARGUMENT
;
404 return KERN_INVALID_ARGUMENT
;
407 actual
= pset
->thread_count
;
409 /* do we have the memory we need? */
411 size_needed
= actual
* sizeof(thread_t
);
412 if (size_needed
<= size
)
415 /* unlock the pset and allocate more memory */
421 assert(size_needed
> 0);
426 return KERN_RESOURCE_SHORTAGE
;
429 /* OK, have memory and the processor_set is locked & active */
430 threads
= (thread_t
*) addr
;
431 for (i
= 0, thread
= (thread_t
) queue_first(&pset
->threads
);
432 !queue_end(&pset
->threads
, (queue_entry_t
) thread
);
433 thread
= (thread_t
) queue_next(&thread
->pset_threads
)) {
434 thread_reference_internal(thread
);
435 threads
[i
++] = thread
;
439 /* can unlock processor set now that we have the thread refs */
442 /* calculate maxusage and free thread references */
448 thread_t threadref
= threads
[--i
];
450 if (threadref
->kernel_stack
!= 0)
453 thread_deallocate(threadref
);
460 *residentp
= *spacep
= total
* round_page(KERNEL_STACK_SIZE
);
461 *maxusagep
= maxusage
;
462 *maxstackp
= maxstack
;
465 #endif /* MACH_DEBUG */
468 vm_offset_t
min_valid_stack_address(void)
470 return vm_map_min(stack_map
);
473 vm_offset_t
max_valid_stack_address(void)
475 return vm_map_max(stack_map
);