2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Kernel stack management routines.
26 #include <mach/mach_host.h>
27 #include <mach/mach_types.h>
28 #include <mach/processor_set.h>
30 #include <kern/kern_types.h>
31 #include <kern/mach_param.h>
32 #include <kern/processor.h>
33 #include <kern/thread.h>
34 #include <kern/zalloc.h>
35 #include <kern/kalloc.h>
37 #include <vm/vm_map.h>
38 #include <vm/vm_kern.h>
40 #include <mach_debug.h>
43 * We allocate stacks from generic kernel VM.
45 * The stack_free_list can only be accessed at splsched,
46 * because stack_alloc_try/thread_invoke operate at splsched.
49 decl_simple_lock_data(static,stack_lock_data
)
50 #define stack_lock() simple_lock(&stack_lock_data)
51 #define stack_unlock() simple_unlock(&stack_lock_data)
53 #define STACK_CACHE_SIZE 2
55 static vm_map_t stack_map
;
56 static vm_offset_t stack_free_list
;
58 static unsigned int stack_free_count
, stack_free_hiwat
; /* free list count */
59 static unsigned int stack_total
, stack_hiwat
; /* current total count */
61 static unsigned int stack_free_target
;
62 static int stack_free_delta
;
64 static unsigned int stack_new_count
; /* total new stack allocations */
66 static vm_offset_t stack_addr_mask
;
69 * The next field is at the base of the stack,
70 * so the low end is left unsullied.
72 #define stack_next(stack) \
73 (*((vm_offset_t *)((stack) + KERNEL_STACK_SIZE) - 1))
78 vm_offset_t stacks
, boundary
;
79 vm_map_offset_t map_addr
;
81 simple_lock_init(&stack_lock_data
, 0);
83 if (KERNEL_STACK_SIZE
< round_page(KERNEL_STACK_SIZE
))
84 panic("stack_init: stack size %d not a multiple of page size %d\n", KERNEL_STACK_SIZE
, PAGE_SIZE
);
86 for (boundary
= PAGE_SIZE
; boundary
<= KERNEL_STACK_SIZE
; )
89 stack_addr_mask
= boundary
- 1;
91 if (kmem_suballoc(kernel_map
, &stacks
, (boundary
* (2 * THREAD_MAX
+ 64)),
92 FALSE
, VM_FLAGS_ANYWHERE
, &stack_map
) != KERN_SUCCESS
)
93 panic("stack_init: kmem_suballoc");
95 map_addr
= vm_map_min(stack_map
);
96 if (vm_map_enter(stack_map
, &map_addr
, vm_map_round_page(PAGE_SIZE
), 0, VM_FLAGS_FIXED
,
97 VM_OBJECT_NULL
, 0, FALSE
, VM_PROT_NONE
, VM_PROT_NONE
, VM_INHERIT_DEFAULT
) != KERN_SUCCESS
)
98 panic("stack_init: vm_map_enter");
104 * Allocate a stack for a thread, may
114 assert(thread
->kernel_stack
== 0);
118 stack
= stack_free_list
;
120 stack_free_list
= stack_next(stack
);
124 if (++stack_total
> stack_hiwat
)
125 stack_hiwat
= stack_total
;
133 if (kernel_memory_allocate(stack_map
, &stack
, KERNEL_STACK_SIZE
, stack_addr_mask
, KMA_KOBJECT
) != KERN_SUCCESS
)
134 panic("stack_alloc: kernel_memory_allocate");
137 machine_stack_attach(thread
, stack
);
143 * Detach and free the stack for a thread.
149 vm_offset_t stack
= machine_stack_detach(thread
);
152 if (stack
!= thread
->reserved_stack
) {
153 struct stack_cache
*cache
;
157 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
158 if (cache
->count
< STACK_CACHE_SIZE
) {
159 stack_next(stack
) = cache
->free
;
165 stack_next(stack
) = stack_free_list
;
166 stack_free_list
= stack
;
167 if (++stack_free_count
> stack_free_hiwat
)
168 stack_free_hiwat
= stack_free_count
;
180 struct stack_cache
*cache
;
184 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
185 if (cache
->count
< STACK_CACHE_SIZE
) {
186 stack_next(stack
) = cache
->free
;
192 stack_next(stack
) = stack_free_list
;
193 stack_free_list
= stack
;
194 if (++stack_free_count
> stack_free_hiwat
)
195 stack_free_hiwat
= stack_free_count
;
205 * Non-blocking attempt to allocate a
206 * stack for a thread.
208 * Returns TRUE on success.
210 * Called at splsched.
216 struct stack_cache
*cache
;
219 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
222 cache
->free
= stack_next(stack
);
226 if (stack_free_list
!= 0) {
228 stack
= stack_free_list
;
230 stack_free_list
= stack_next(stack
);
238 if (stack
!= 0 || (stack
= thread
->reserved_stack
) != 0) {
239 machine_stack_attach(thread
, stack
);
246 static unsigned int stack_collect_tick
, last_stack_tick
;
251 * Free excess kernel stacks, may
257 if (stack_collect_tick
!= last_stack_tick
) {
265 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
266 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
268 while (stack_free_count
> target
) {
269 stack
= stack_free_list
;
270 stack_free_list
= stack_next(stack
);
271 stack_free_count
--; stack_total
--;
275 if (vm_map_remove(stack_map
, vm_map_trunc_page(stack
),
276 vm_map_round_page(stack
+ KERNEL_STACK_SIZE
), VM_MAP_REMOVE_KUNWIRE
) != KERN_SUCCESS
)
277 panic("stack_collect: vm_map_remove");
282 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
283 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
286 last_stack_tick
= stack_collect_tick
;
294 * compute_stack_target:
296 * Computes a new target free list count
297 * based on recent alloc / free activity.
299 * Limits stack collection to once per
300 * computation period.
303 compute_stack_target(
311 if (stack_free_target
> 5)
312 stack_free_target
= (4 * stack_free_target
) / 5;
314 if (stack_free_target
> 0)
317 stack_free_target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
319 stack_free_delta
= 0;
320 stack_collect_tick
++;
327 stack_fake_zone_info(int *count
, vm_size_t
*cur_size
, vm_size_t
*max_size
, vm_size_t
*elem_size
,
328 vm_size_t
*alloc_size
, int *collectable
, int *exhaustable
)
330 unsigned int total
, hiwat
, free
;
337 free
= stack_free_count
;
341 *count
= total
- free
;
342 *cur_size
= KERNEL_STACK_SIZE
* total
;
343 *max_size
= KERNEL_STACK_SIZE
* hiwat
;
344 *elem_size
= KERNEL_STACK_SIZE
;
345 *alloc_size
= KERNEL_STACK_SIZE
;
351 void stack_privilege(
356 __unused thread_t thread
)
362 * Return info on stack usage for threads in a specific processor set
365 processor_set_stack_usage(
366 processor_set_t pset
,
367 unsigned int *totalp
,
369 vm_size_t
*residentp
,
370 vm_size_t
*maxusagep
,
371 vm_offset_t
*maxstackp
)
374 return KERN_NOT_SUPPORTED
;
378 vm_offset_t maxstack
;
380 register thread_t
*threads
;
381 register thread_t thread
;
383 unsigned int actual
; /* this many things */
386 vm_size_t size
, size_needed
;
389 if (pset
== PROCESSOR_SET_NULL
)
390 return KERN_INVALID_ARGUMENT
;
398 return KERN_INVALID_ARGUMENT
;
401 actual
= pset
->thread_count
;
403 /* do we have the memory we need? */
405 size_needed
= actual
* sizeof(thread_t
);
406 if (size_needed
<= size
)
409 /* unlock the pset and allocate more memory */
415 assert(size_needed
> 0);
420 return KERN_RESOURCE_SHORTAGE
;
423 /* OK, have memory and the processor_set is locked & active */
424 threads
= (thread_t
*) addr
;
425 for (i
= 0, thread
= (thread_t
) queue_first(&pset
->threads
);
426 !queue_end(&pset
->threads
, (queue_entry_t
) thread
);
427 thread
= (thread_t
) queue_next(&thread
->pset_threads
)) {
428 thread_reference_internal(thread
);
429 threads
[i
++] = thread
;
433 /* can unlock processor set now that we have the thread refs */
436 /* calculate maxusage and free thread references */
442 thread_t threadref
= threads
[--i
];
444 if (threadref
->kernel_stack
!= 0)
447 thread_deallocate(threadref
);
454 *residentp
= *spacep
= total
* round_page(KERNEL_STACK_SIZE
);
455 *maxusagep
= maxusage
;
456 *maxstackp
= maxstack
;
459 #endif /* MACH_DEBUG */
462 vm_offset_t
min_valid_stack_address(void)
464 return vm_map_min(stack_map
);
467 vm_offset_t
max_valid_stack_address(void)
469 return vm_map_max(stack_map
);