2 * Copyright (c) 2003-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 * 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_offset_t stack_free_list
;
63 static unsigned int stack_free_count
, stack_free_hiwat
; /* free list count */
64 static unsigned int stack_hiwat
;
65 unsigned int stack_total
; /* 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
;
74 unsigned int kernel_stack_pages
= KERNEL_STACK_SIZE
/ PAGE_SIZE
;
75 vm_offset_t kernel_stack_size
= KERNEL_STACK_SIZE
;
76 vm_offset_t kernel_stack_mask
= -KERNEL_STACK_SIZE
;
77 vm_offset_t kernel_stack_depth_max
= 0;
80 * The next field is at the base of the stack,
81 * so the low end is left unsullied.
83 #define stack_next(stack) \
84 (*((vm_offset_t *)((stack) + kernel_stack_size) - 1))
87 log2(vm_offset_t size
)
90 for (result
= 0; size
> 0; result
++)
95 static inline vm_offset_t
96 roundup_pow2(vm_offset_t size
)
98 return 1UL << (log2(size
- 1) + 1);
104 simple_lock_init(&stack_lock_data
, 0);
106 if (PE_parse_boot_argn("kernel_stack_pages",
108 sizeof (kernel_stack_pages
))) {
109 kernel_stack_size
= kernel_stack_pages
* PAGE_SIZE
;
110 printf("stack_init: kernel_stack_pages=%d kernel_stack_size=%p\n",
111 kernel_stack_pages
, (void *) kernel_stack_size
);
114 if (kernel_stack_size
< round_page(kernel_stack_size
))
115 panic("stack_init: stack size %p not a multiple of page size %d\n",
116 (void *) kernel_stack_size
, PAGE_SIZE
);
118 stack_addr_mask
= roundup_pow2(kernel_stack_size
) - 1;
119 kernel_stack_mask
= ~stack_addr_mask
;
125 * Allocate a stack for a thread, may
136 assert(thread
->kernel_stack
== 0);
140 stack
= stack_free_list
;
142 stack_free_list
= stack_next(stack
);
146 if (++stack_total
> stack_hiwat
)
147 stack_hiwat
= stack_total
;
157 * Request guard pages on either side of the stack. Ask
158 * kernel_memory_allocate() for two extra pages to account
162 guard_flags
= KMA_GUARD_FIRST
| KMA_GUARD_LAST
;
163 if (kernel_memory_allocate(kernel_map
, &stack
,
164 kernel_stack_size
+ (2*PAGE_SIZE
),
166 KMA_KOBJECT
| guard_flags
)
168 panic("stack_alloc: kernel_memory_allocate");
171 * The stack address that comes back is the address of the lower
172 * guard page. Skip past it to get the actual stack base address.
178 machine_stack_attach(thread
, stack
);
184 * Detach and free the stack for a thread.
190 vm_offset_t stack
= machine_stack_detach(thread
);
193 if (stack
!= thread
->reserved_stack
)
194 stack_free_stack(stack
);
201 struct stack_cache
*cache
;
205 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
206 if (cache
->count
< STACK_CACHE_SIZE
) {
207 stack_next(stack
) = cache
->free
;
213 stack_next(stack
) = stack_free_list
;
214 stack_free_list
= stack
;
215 if (++stack_free_count
> stack_free_hiwat
)
216 stack_free_hiwat
= stack_free_count
;
226 * Non-blocking attempt to allocate a
227 * stack for a thread.
229 * Returns TRUE on success.
231 * Called at splsched.
237 struct stack_cache
*cache
;
240 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
243 cache
->free
= stack_next(stack
);
247 if (stack_free_list
!= 0) {
249 stack
= stack_free_list
;
251 stack_free_list
= stack_next(stack
);
259 if (stack
!= 0 || (stack
= thread
->reserved_stack
) != 0) {
260 machine_stack_attach(thread
, stack
);
267 static unsigned int stack_collect_tick
, last_stack_tick
;
272 * Free excess kernel stacks, may
278 if (stack_collect_tick
!= last_stack_tick
) {
286 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
287 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
289 while (stack_free_count
> target
) {
290 stack
= stack_free_list
;
291 stack_free_list
= stack_next(stack
);
292 stack_free_count
--; stack_total
--;
297 * Get the stack base address, then decrement by one page
298 * to account for the lower guard page. Add two extra pages
299 * to the size to account for the guard pages on both ends
300 * that were originally requested when the stack was allocated
301 * back in stack_alloc().
304 stack
= (vm_offset_t
)vm_map_trunc_page(stack
);
309 stack
+ kernel_stack_size
+(2*PAGE_SIZE
),
310 VM_MAP_REMOVE_KUNWIRE
)
312 panic("stack_collect: vm_map_remove");
318 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
319 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
322 last_stack_tick
= stack_collect_tick
;
330 * compute_stack_target:
332 * Computes a new target free list count
333 * based on recent alloc / free activity.
335 * Limits stack collection to once per
336 * computation period.
339 compute_stack_target(
347 if (stack_free_target
> 5)
348 stack_free_target
= (4 * stack_free_target
) / 5;
350 if (stack_free_target
> 0)
353 stack_free_target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
355 stack_free_delta
= 0;
356 stack_collect_tick
++;
363 stack_fake_zone_info(int *count
, vm_size_t
*cur_size
, vm_size_t
*max_size
, vm_size_t
*elem_size
,
364 vm_size_t
*alloc_size
, int *collectable
, int *exhaustable
)
366 unsigned int total
, hiwat
, free
;
373 free
= stack_free_count
;
377 *count
= total
- free
;
378 *cur_size
= kernel_stack_size
* total
;
379 *max_size
= kernel_stack_size
* hiwat
;
380 *elem_size
= kernel_stack_size
;
381 *alloc_size
= kernel_stack_size
;
387 void stack_privilege(
392 __unused thread_t thread
)
398 * Return info on stack usage for threads in a specific processor set
401 processor_set_stack_usage(
402 processor_set_t pset
,
403 unsigned int *totalp
,
405 vm_size_t
*residentp
,
406 vm_size_t
*maxusagep
,
407 vm_offset_t
*maxstackp
)
410 return KERN_NOT_SUPPORTED
;
414 vm_offset_t maxstack
;
416 register thread_t
*thread_list
;
417 register thread_t thread
;
419 unsigned int actual
; /* this many things */
422 vm_size_t size
, size_needed
;
425 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
426 return KERN_INVALID_ARGUMENT
;
432 lck_mtx_lock(&tasks_threads_lock
);
434 actual
= threads_count
;
436 /* do we have the memory we need? */
438 size_needed
= actual
* sizeof(thread_t
);
439 if (size_needed
<= size
)
442 lck_mtx_unlock(&tasks_threads_lock
);
447 assert(size_needed
> 0);
452 return KERN_RESOURCE_SHORTAGE
;
455 /* OK, have memory and list is locked */
456 thread_list
= (thread_t
*) addr
;
457 for (i
= 0, thread
= (thread_t
) queue_first(&threads
);
458 !queue_end(&threads
, (queue_entry_t
) thread
);
459 thread
= (thread_t
) queue_next(&thread
->threads
)) {
460 thread_reference_internal(thread
);
461 thread_list
[i
++] = thread
;
465 lck_mtx_unlock(&tasks_threads_lock
);
467 /* calculate maxusage and free thread references */
473 thread_t threadref
= thread_list
[--i
];
475 if (threadref
->kernel_stack
!= 0)
478 thread_deallocate(threadref
);
485 *residentp
= *spacep
= total
* round_page(kernel_stack_size
);
486 *maxusagep
= maxusage
;
487 *maxstackp
= maxstack
;
490 #endif /* MACH_DEBUG */
493 vm_offset_t
min_valid_stack_address(void)
495 return (vm_offset_t
)vm_map_min(kernel_map
);
498 vm_offset_t
max_valid_stack_address(void)
500 return (vm_offset_t
)vm_map_max(kernel_map
);