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>
42 #include <kern/ledger.h>
44 #include <vm/vm_map.h>
45 #include <vm/vm_kern.h>
47 #include <mach_debug.h>
50 * We allocate stacks from generic kernel VM.
52 * The stack_free_list can only be accessed at splsched,
53 * because stack_alloc_try/thread_invoke operate at splsched.
56 decl_simple_lock_data(static,stack_lock_data
)
57 #define stack_lock() simple_lock(&stack_lock_data)
58 #define stack_unlock() simple_unlock(&stack_lock_data)
60 #define STACK_CACHE_SIZE 2
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_hiwat
;
66 unsigned int stack_total
; /* current total count */
67 unsigned long long stack_allocs
; /* total count of allocations */
69 static int stack_fake_zone_index
= -1; /* index in zone_info array */
71 static unsigned int stack_free_target
;
72 static int stack_free_delta
;
74 static unsigned int stack_new_count
; /* total new stack allocations */
76 static vm_offset_t stack_addr_mask
;
78 unsigned int kernel_stack_pages
= KERNEL_STACK_SIZE
/ PAGE_SIZE
;
79 vm_offset_t kernel_stack_size
= KERNEL_STACK_SIZE
;
80 vm_offset_t kernel_stack_mask
= -KERNEL_STACK_SIZE
;
81 vm_offset_t kernel_stack_depth_max
= 0;
84 STACK_ZINFO_PALLOC(thread_t thread
)
89 ledger_credit(thread
->t_ledger
, task_ledgers
.tkm_private
, kernel_stack_size
);
91 if (stack_fake_zone_index
!= -1 &&
92 (task
= thread
->task
) != NULL
&& (zinfo
= task
->tkm_zinfo
) != NULL
)
93 OSAddAtomic64(kernel_stack_size
,
94 (int64_t *)&zinfo
[stack_fake_zone_index
].alloc
);
98 STACK_ZINFO_PFREE(thread_t thread
)
103 ledger_debit(thread
->t_ledger
, task_ledgers
.tkm_private
, kernel_stack_size
);
105 if (stack_fake_zone_index
!= -1 &&
106 (task
= thread
->task
) != NULL
&& (zinfo
= task
->tkm_zinfo
) != NULL
)
107 OSAddAtomic64(kernel_stack_size
,
108 (int64_t *)&zinfo
[stack_fake_zone_index
].free
);
112 STACK_ZINFO_HANDOFF(thread_t from
, thread_t to
)
114 ledger_debit(from
->t_ledger
, task_ledgers
.tkm_private
, kernel_stack_size
);
115 ledger_credit(to
->t_ledger
, task_ledgers
.tkm_private
, kernel_stack_size
);
117 if (stack_fake_zone_index
!= -1) {
121 if ((task
= from
->task
) != NULL
&& (zinfo
= task
->tkm_zinfo
) != NULL
)
122 OSAddAtomic64(kernel_stack_size
,
123 (int64_t *)&zinfo
[stack_fake_zone_index
].free
);
125 if ((task
= to
->task
) != NULL
&& (zinfo
= task
->tkm_zinfo
) != NULL
)
126 OSAddAtomic64(kernel_stack_size
,
127 (int64_t *)&zinfo
[stack_fake_zone_index
].alloc
);
132 * The next field is at the base of the stack,
133 * so the low end is left unsullied.
135 #define stack_next(stack) \
136 (*((vm_offset_t *)((stack) + kernel_stack_size) - 1))
139 log2(vm_offset_t size
)
142 for (result
= 0; size
> 0; result
++)
147 static inline vm_offset_t
148 roundup_pow2(vm_offset_t size
)
150 return 1UL << (log2(size
- 1) + 1);
153 static vm_offset_t
stack_alloc_internal(void);
154 static void stack_free_stack(vm_offset_t
);
159 simple_lock_init(&stack_lock_data
, 0);
161 if (PE_parse_boot_argn("kernel_stack_pages",
163 sizeof (kernel_stack_pages
))) {
164 kernel_stack_size
= kernel_stack_pages
* PAGE_SIZE
;
165 printf("stack_init: kernel_stack_pages=%d kernel_stack_size=%p\n",
166 kernel_stack_pages
, (void *) kernel_stack_size
);
169 if (kernel_stack_size
< round_page(kernel_stack_size
))
170 panic("stack_init: stack size %p not a multiple of page size %d\n",
171 (void *) kernel_stack_size
, PAGE_SIZE
);
173 stack_addr_mask
= roundup_pow2(kernel_stack_size
) - 1;
174 kernel_stack_mask
= ~stack_addr_mask
;
180 * Allocate a stack for a thread, may
185 stack_alloc_internal(void)
194 stack
= stack_free_list
;
196 stack_free_list
= stack_next(stack
);
200 if (++stack_total
> stack_hiwat
)
201 stack_hiwat
= stack_total
;
211 * Request guard pages on either side of the stack. Ask
212 * kernel_memory_allocate() for two extra pages to account
216 guard_flags
= KMA_GUARD_FIRST
| KMA_GUARD_LAST
;
217 if (kernel_memory_allocate(kernel_map
, &stack
,
218 kernel_stack_size
+ (2*PAGE_SIZE
),
220 KMA_KSTACK
| KMA_KOBJECT
| guard_flags
)
222 panic("stack_alloc: kernel_memory_allocate");
225 * The stack address that comes back is the address of the lower
226 * guard page. Skip past it to get the actual stack base address.
239 assert(thread
->kernel_stack
== 0);
240 machine_stack_attach(thread
, stack_alloc_internal());
241 STACK_ZINFO_PALLOC(thread
);
245 stack_handoff(thread_t from
, thread_t to
)
247 assert(from
== current_thread());
248 machine_stack_handoff(from
, to
);
249 STACK_ZINFO_HANDOFF(from
, to
);
255 * Detach and free the stack for a thread.
261 vm_offset_t stack
= machine_stack_detach(thread
);
264 if (stack
!= thread
->reserved_stack
) {
265 STACK_ZINFO_PFREE(thread
);
266 stack_free_stack(stack
);
274 if (thread
->reserved_stack
!= thread
->kernel_stack
) {
275 stack_free_stack(thread
->reserved_stack
);
276 STACK_ZINFO_PFREE(thread
);
284 struct stack_cache
*cache
;
288 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
289 if (cache
->count
< STACK_CACHE_SIZE
) {
290 stack_next(stack
) = cache
->free
;
296 stack_next(stack
) = stack_free_list
;
297 stack_free_list
= stack
;
298 if (++stack_free_count
> stack_free_hiwat
)
299 stack_free_hiwat
= stack_free_count
;
309 * Non-blocking attempt to allocate a
310 * stack for a thread.
312 * Returns TRUE on success.
314 * Called at splsched.
320 struct stack_cache
*cache
;
323 cache
= &PROCESSOR_DATA(current_processor(), stack_cache
);
326 STACK_ZINFO_PALLOC(thread
);
327 cache
->free
= stack_next(stack
);
331 if (stack_free_list
!= 0) {
333 stack
= stack_free_list
;
335 STACK_ZINFO_PALLOC(thread
);
336 stack_free_list
= stack_next(stack
);
344 if (stack
!= 0 || (stack
= thread
->reserved_stack
) != 0) {
345 machine_stack_attach(thread
, stack
);
352 static unsigned int stack_collect_tick
, last_stack_tick
;
357 * Free excess kernel stacks, may
363 if (stack_collect_tick
!= last_stack_tick
) {
371 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
372 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
374 while (stack_free_count
> target
) {
375 stack
= stack_free_list
;
376 stack_free_list
= stack_next(stack
);
377 stack_free_count
--; stack_total
--;
382 * Get the stack base address, then decrement by one page
383 * to account for the lower guard page. Add two extra pages
384 * to the size to account for the guard pages on both ends
385 * that were originally requested when the stack was allocated
386 * back in stack_alloc().
389 stack
= (vm_offset_t
)vm_map_trunc_page(stack
);
394 stack
+ kernel_stack_size
+(2*PAGE_SIZE
),
395 VM_MAP_REMOVE_KUNWIRE
)
397 panic("stack_collect: vm_map_remove");
403 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
404 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
407 last_stack_tick
= stack_collect_tick
;
415 * compute_stack_target:
417 * Computes a new target free list count
418 * based on recent alloc / free activity.
420 * Limits stack collection to once per
421 * computation period.
424 compute_stack_target(
432 if (stack_free_target
> 5)
433 stack_free_target
= (4 * stack_free_target
) / 5;
435 if (stack_free_target
> 0)
438 stack_free_target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
440 stack_free_delta
= 0;
441 stack_collect_tick
++;
448 stack_fake_zone_init(int zone_index
)
450 stack_fake_zone_index
= zone_index
;
454 stack_fake_zone_info(int *count
,
455 vm_size_t
*cur_size
, vm_size_t
*max_size
, vm_size_t
*elem_size
, vm_size_t
*alloc_size
,
456 uint64_t *sum_size
, int *collectable
, int *exhaustable
, int *caller_acct
)
458 unsigned int total
, hiwat
, free
;
459 unsigned long long all
;
467 free
= stack_free_count
;
471 *count
= total
- free
;
472 *cur_size
= kernel_stack_size
* total
;
473 *max_size
= kernel_stack_size
* hiwat
;
474 *elem_size
= kernel_stack_size
;
475 *alloc_size
= kernel_stack_size
;
476 *sum_size
= all
* kernel_stack_size
;
484 void stack_privilege(
489 __unused thread_t thread
)
495 * Return info on stack usage for threads in a specific processor set
498 processor_set_stack_usage(
499 processor_set_t pset
,
500 unsigned int *totalp
,
502 vm_size_t
*residentp
,
503 vm_size_t
*maxusagep
,
504 vm_offset_t
*maxstackp
)
507 return KERN_NOT_SUPPORTED
;
511 vm_offset_t maxstack
;
513 register thread_t
*thread_list
;
514 register thread_t thread
;
516 unsigned int actual
; /* this many things */
519 vm_size_t size
, size_needed
;
522 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
523 return KERN_INVALID_ARGUMENT
;
529 lck_mtx_lock(&tasks_threads_lock
);
531 actual
= threads_count
;
533 /* do we have the memory we need? */
535 size_needed
= actual
* sizeof(thread_t
);
536 if (size_needed
<= size
)
539 lck_mtx_unlock(&tasks_threads_lock
);
544 assert(size_needed
> 0);
549 return KERN_RESOURCE_SHORTAGE
;
552 /* OK, have memory and list is locked */
553 thread_list
= (thread_t
*) addr
;
554 for (i
= 0, thread
= (thread_t
) queue_first(&threads
);
555 !queue_end(&threads
, (queue_entry_t
) thread
);
556 thread
= (thread_t
) queue_next(&thread
->threads
)) {
557 thread_reference_internal(thread
);
558 thread_list
[i
++] = thread
;
562 lck_mtx_unlock(&tasks_threads_lock
);
564 /* calculate maxusage and free thread references */
570 thread_t threadref
= thread_list
[--i
];
572 if (threadref
->kernel_stack
!= 0)
575 thread_deallocate(threadref
);
582 *residentp
= *spacep
= total
* round_page(kernel_stack_size
);
583 *maxusagep
= maxusage
;
584 *maxstackp
= maxstack
;
587 #endif /* MACH_DEBUG */
590 vm_offset_t
min_valid_stack_address(void)
592 return (vm_offset_t
)vm_map_min(kernel_map
);
595 vm_offset_t
max_valid_stack_address(void)
597 return (vm_offset_t
)vm_map_max(kernel_map
);