2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Kernel stack management routines.
27 #include <mach/mach_host.h>
28 #include <mach/mach_types.h>
29 #include <mach/processor_set.h>
31 #include <kern/kern_types.h>
32 #include <kern/mach_param.h>
33 #include <kern/processor.h>
34 #include <kern/thread.h>
35 #include <kern/zalloc.h>
36 #include <kern/kalloc.h>
38 #include <vm/vm_map.h>
39 #include <vm/vm_kern.h>
41 #include <mach_debug.h>
44 * We allocate stacks from generic kernel VM.
46 * The stack_free_list can only be accessed at splsched,
47 * because stack_alloc_try/thread_invoke operate at splsched.
50 decl_simple_lock_data(static,stack_lock_data
)
51 #define stack_lock() simple_lock(&stack_lock_data)
52 #define stack_unlock() simple_unlock(&stack_lock_data)
54 #define STACK_CACHE_SIZE 2
56 static vm_map_t stack_map
;
57 static vm_offset_t stack_free_list
;
59 static unsigned int stack_free_count
, stack_free_hiwat
; /* free list count */
60 static unsigned int stack_total
, stack_hiwat
; /* current total count */
62 static unsigned int stack_free_target
;
63 static int stack_free_delta
;
65 static unsigned int stack_new_count
; /* total new stack allocations */
67 static vm_offset_t stack_addr_mask
;
70 * The next field is at the base of the stack,
71 * so the low end is left unsullied.
73 #define stack_next(stack) \
74 (*((vm_offset_t *)((stack) + KERNEL_STACK_SIZE) - 1))
79 vm_offset_t stacks
, boundary
;
80 vm_map_offset_t map_addr
;
82 simple_lock_init(&stack_lock_data
, 0);
84 if (KERNEL_STACK_SIZE
< round_page(KERNEL_STACK_SIZE
))
85 panic("stack_init: stack size %d not a multiple of page size %d\n", KERNEL_STACK_SIZE
, PAGE_SIZE
);
87 for (boundary
= PAGE_SIZE
; boundary
<= KERNEL_STACK_SIZE
; )
90 stack_addr_mask
= boundary
- 1;
92 if (kmem_suballoc(kernel_map
, &stacks
, (boundary
* (2 * THREAD_MAX
+ 64)),
93 FALSE
, VM_FLAGS_ANYWHERE
, &stack_map
) != KERN_SUCCESS
)
94 panic("stack_init: kmem_suballoc");
96 map_addr
= vm_map_min(stack_map
);
97 if (vm_map_enter(stack_map
, &map_addr
, vm_map_round_page(PAGE_SIZE
), 0, VM_FLAGS_FIXED
,
98 VM_OBJECT_NULL
, 0, FALSE
, VM_PROT_NONE
, VM_PROT_NONE
, VM_INHERIT_DEFAULT
) != KERN_SUCCESS
)
99 panic("stack_init: vm_map_enter");
105 * Allocate a stack for a thread, may
115 assert(thread
->kernel_stack
== 0);
119 stack
= stack_free_list
;
121 stack_free_list
= stack_next(stack
);
125 if (++stack_total
> stack_hiwat
)
126 stack_hiwat
= stack_total
;
134 if (kernel_memory_allocate(stack_map
, &stack
, KERNEL_STACK_SIZE
, stack_addr_mask
, KMA_KOBJECT
) != KERN_SUCCESS
)
135 panic("stack_alloc: kernel_memory_allocate");
138 machine_stack_attach(thread
, stack
);
144 * Detach and free the stack for a thread.
150 vm_offset_t stack
= machine_stack_detach(thread
);
153 if (stack
!= thread
->reserved_stack
) {
154 struct stack_cache
*cache
;
158 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
159 if (cache
->count
< STACK_CACHE_SIZE
) {
160 stack_next(stack
) = cache
->free
;
166 stack_next(stack
) = stack_free_list
;
167 stack_free_list
= stack
;
168 if (++stack_free_count
> stack_free_hiwat
)
169 stack_free_hiwat
= stack_free_count
;
181 struct stack_cache
*cache
;
185 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
186 if (cache
->count
< STACK_CACHE_SIZE
) {
187 stack_next(stack
) = cache
->free
;
193 stack_next(stack
) = stack_free_list
;
194 stack_free_list
= stack
;
195 if (++stack_free_count
> stack_free_hiwat
)
196 stack_free_hiwat
= stack_free_count
;
206 * Non-blocking attempt to allocate a
207 * stack for a thread.
209 * Returns TRUE on success.
211 * Called at splsched.
217 struct stack_cache
*cache
;
220 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
223 cache
->free
= stack_next(stack
);
227 if (stack_free_list
!= 0) {
229 stack
= stack_free_list
;
231 stack_free_list
= stack_next(stack
);
239 if (stack
!= 0 || (stack
= thread
->reserved_stack
) != 0) {
240 machine_stack_attach(thread
, stack
);
247 static unsigned int stack_collect_tick
, last_stack_tick
;
252 * Free excess kernel stacks, may
258 if (stack_collect_tick
!= last_stack_tick
) {
266 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
267 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
269 while (stack_free_count
> target
) {
270 stack
= stack_free_list
;
271 stack_free_list
= stack_next(stack
);
272 stack_free_count
--; stack_total
--;
276 if (vm_map_remove(stack_map
, vm_map_trunc_page(stack
),
277 vm_map_round_page(stack
+ KERNEL_STACK_SIZE
), VM_MAP_REMOVE_KUNWIRE
) != KERN_SUCCESS
)
278 panic("stack_collect: vm_map_remove");
283 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
284 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
287 last_stack_tick
= stack_collect_tick
;
295 * compute_stack_target:
297 * Computes a new target free list count
298 * based on recent alloc / free activity.
300 * Limits stack collection to once per
301 * computation period.
304 compute_stack_target(
312 if (stack_free_target
> 5)
313 stack_free_target
= (4 * stack_free_target
) / 5;
315 if (stack_free_target
> 0)
318 stack_free_target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
320 stack_free_delta
= 0;
321 stack_collect_tick
++;
328 stack_fake_zone_info(int *count
, vm_size_t
*cur_size
, vm_size_t
*max_size
, vm_size_t
*elem_size
,
329 vm_size_t
*alloc_size
, int *collectable
, int *exhaustable
)
331 unsigned int total
, hiwat
, free
;
338 free
= stack_free_count
;
342 *count
= total
- free
;
343 *cur_size
= KERNEL_STACK_SIZE
* total
;
344 *max_size
= KERNEL_STACK_SIZE
* hiwat
;
345 *elem_size
= KERNEL_STACK_SIZE
;
346 *alloc_size
= KERNEL_STACK_SIZE
;
352 void stack_privilege(
357 __unused thread_t thread
)
363 * Return info on stack usage for threads in a specific processor set
366 processor_set_stack_usage(
367 processor_set_t pset
,
368 unsigned int *totalp
,
370 vm_size_t
*residentp
,
371 vm_size_t
*maxusagep
,
372 vm_offset_t
*maxstackp
)
375 return KERN_NOT_SUPPORTED
;
379 vm_offset_t maxstack
;
381 register thread_t
*threads
;
382 register thread_t thread
;
384 unsigned int actual
; /* this many things */
387 vm_size_t size
, size_needed
;
390 if (pset
== PROCESSOR_SET_NULL
)
391 return KERN_INVALID_ARGUMENT
;
399 return KERN_INVALID_ARGUMENT
;
402 actual
= pset
->thread_count
;
404 /* do we have the memory we need? */
406 size_needed
= actual
* sizeof(thread_t
);
407 if (size_needed
<= size
)
410 /* unlock the pset and allocate more memory */
416 assert(size_needed
> 0);
421 return KERN_RESOURCE_SHORTAGE
;
424 /* OK, have memory and the processor_set is locked & active */
425 threads
= (thread_t
*) addr
;
426 for (i
= 0, thread
= (thread_t
) queue_first(&pset
->threads
);
427 !queue_end(&pset
->threads
, (queue_entry_t
) thread
);
428 thread
= (thread_t
) queue_next(&thread
->pset_threads
)) {
429 thread_reference_internal(thread
);
430 threads
[i
++] = thread
;
434 /* can unlock processor set now that we have the thread refs */
437 /* calculate maxusage and free thread references */
443 thread_t threadref
= threads
[--i
];
445 if (threadref
->kernel_stack
!= 0)
448 thread_deallocate(threadref
);
455 *residentp
= *spacep
= total
* round_page(KERNEL_STACK_SIZE
);
456 *maxusagep
= maxusage
;
457 *maxstackp
= maxstack
;
460 #endif /* MACH_DEBUG */
463 vm_offset_t
min_valid_stack_address(void)
465 return vm_map_min(stack_map
);
468 vm_offset_t
max_valid_stack_address(void)
470 return vm_map_max(stack_map
);