2 * Copyright (c) 2003-2019 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/lock_group.h>
38 #include <kern/mach_param.h>
39 #include <kern/percpu.h>
40 #include <kern/processor.h>
41 #include <kern/thread.h>
42 #include <kern/zalloc.h>
43 #include <kern/kalloc.h>
44 #include <kern/ledger.h>
46 #include <vm/vm_map.h>
47 #include <vm/vm_kern.h>
49 #include <mach_debug.h>
50 #include <san/kasan.h>
53 * We allocate stacks from generic kernel VM.
55 * The stack_free_list can only be accessed at splsched,
56 * because stack_alloc_try/thread_invoke operate at splsched.
59 decl_simple_lock_data(static, stack_lock_data
);
60 #define stack_lock() simple_lock(&stack_lock_data, LCK_GRP_NULL)
61 #define stack_unlock() simple_unlock(&stack_lock_data)
63 #define STACK_CACHE_SIZE 2
65 static vm_offset_t stack_free_list
;
67 static unsigned int stack_free_count
, stack_free_hiwat
; /* free list count */
68 static unsigned int stack_hiwat
;
69 unsigned int stack_total
; /* current total count */
70 unsigned long long stack_allocs
; /* total count of allocations */
72 static unsigned int stack_free_target
;
73 static int stack_free_delta
;
75 static unsigned int stack_new_count
; /* total new stack allocations */
77 static vm_offset_t stack_addr_mask
;
79 unsigned int kernel_stack_pages
;
80 vm_offset_t kernel_stack_size
;
81 vm_offset_t kernel_stack_mask
;
82 vm_offset_t kernel_stack_depth_max
;
88 static struct stack_cache
PERCPU_DATA(stack_cache
);
91 * The next field is at the base of the stack,
92 * so the low end is left unsullied.
94 #define stack_next(stack) \
95 (*((vm_offset_t *)((stack) + kernel_stack_size) - 1))
98 log2(vm_offset_t size
)
101 for (result
= 0; size
> 0; result
++) {
107 static inline vm_offset_t
108 roundup_pow2(vm_offset_t size
)
110 return 1UL << (log2(size
- 1) + 1);
113 static vm_offset_t
stack_alloc_internal(void);
114 static void stack_free_stack(vm_offset_t
);
119 simple_lock_init(&stack_lock_data
, 0);
121 kernel_stack_pages
= KERNEL_STACK_SIZE
/ PAGE_SIZE
;
122 kernel_stack_size
= KERNEL_STACK_SIZE
;
123 kernel_stack_mask
= -KERNEL_STACK_SIZE
;
124 kernel_stack_depth_max
= 0;
126 if (PE_parse_boot_argn("kernel_stack_pages",
128 sizeof(kernel_stack_pages
))) {
129 kernel_stack_size
= kernel_stack_pages
* PAGE_SIZE
;
130 printf("stack_init: kernel_stack_pages=%d kernel_stack_size=%p\n",
131 kernel_stack_pages
, (void *) kernel_stack_size
);
134 if (kernel_stack_size
< round_page(kernel_stack_size
)) {
135 panic("stack_init: stack size %p not a multiple of page size %d\n",
136 (void *) kernel_stack_size
, PAGE_SIZE
);
139 stack_addr_mask
= roundup_pow2(kernel_stack_size
) - 1;
140 kernel_stack_mask
= ~stack_addr_mask
;
146 * Allocate a stack for a thread, may
151 stack_alloc_internal(void)
153 vm_offset_t stack
= 0;
156 kern_return_t kr
= KERN_SUCCESS
;
161 stack
= stack_free_list
;
163 stack_free_list
= stack_next(stack
);
166 if (++stack_total
> stack_hiwat
) {
167 stack_hiwat
= stack_total
;
177 * Request guard pages on either side of the stack. Ask
178 * kernel_memory_allocate() for two extra pages to account
182 flags
= KMA_GUARD_FIRST
| KMA_GUARD_LAST
| KMA_KSTACK
| KMA_KOBJECT
| KMA_ZERO
;
183 kr
= kernel_memory_allocate(kernel_map
, &stack
,
184 kernel_stack_size
+ (2 * PAGE_SIZE
),
187 VM_KERN_MEMORY_STACK
);
188 if (kr
!= KERN_SUCCESS
) {
189 panic("stack_alloc: kernel_memory_allocate(size:0x%llx, mask: 0x%llx, flags: 0x%x) failed with %d\n", (uint64_t)(kernel_stack_size
+ (2 * PAGE_SIZE
)), (uint64_t)stack_addr_mask
, flags
, kr
);
193 * The stack address that comes back is the address of the lower
194 * guard page. Skip past it to get the actual stack base address.
206 assert(thread
->kernel_stack
== 0);
207 machine_stack_attach(thread
, stack_alloc_internal());
211 stack_handoff(thread_t from
, thread_t to
)
213 assert(from
== current_thread());
214 machine_stack_handoff(from
, to
);
220 * Detach and free the stack for a thread.
226 vm_offset_t stack
= machine_stack_detach(thread
);
229 if (stack
!= thread
->reserved_stack
) {
230 stack_free_stack(stack
);
238 if (thread
->reserved_stack
!= thread
->kernel_stack
) {
239 stack_free_stack(thread
->reserved_stack
);
247 struct stack_cache
*cache
;
251 /* Sanity check - stack should be unpoisoned by now */
252 assert(kasan_check_shadow(stack
, kernel_stack_size
, 0));
256 cache
= PERCPU_GET(stack_cache
);
257 if (cache
->count
< STACK_CACHE_SIZE
) {
258 stack_next(stack
) = cache
->free
;
263 stack_next(stack
) = stack_free_list
;
264 stack_free_list
= stack
;
265 if (++stack_free_count
> stack_free_hiwat
) {
266 stack_free_hiwat
= stack_free_count
;
277 * Non-blocking attempt to allocate a
278 * stack for a thread.
280 * Returns TRUE on success.
282 * Called at splsched.
288 struct stack_cache
*cache
;
291 cache
= PERCPU_GET(stack_cache
);
294 cache
->free
= stack_next(stack
);
297 if (stack_free_list
!= 0) {
299 stack
= stack_free_list
;
301 stack_free_list
= stack_next(stack
);
309 if (stack
!= 0 || (stack
= thread
->reserved_stack
) != 0) {
310 machine_stack_attach(thread
, stack
);
317 static unsigned int stack_collect_tick
, last_stack_tick
;
322 * Free excess kernel stacks, may
328 if (stack_collect_tick
!= last_stack_tick
) {
336 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
337 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
339 while (stack_free_count
> target
) {
340 stack
= stack_free_list
;
341 stack_free_list
= stack_next(stack
);
342 stack_free_count
--; stack_total
--;
347 * Get the stack base address, then decrement by one page
348 * to account for the lower guard page. Add two extra pages
349 * to the size to account for the guard pages on both ends
350 * that were originally requested when the stack was allocated
351 * back in stack_alloc().
354 stack
= (vm_offset_t
)vm_map_trunc_page(
356 VM_MAP_PAGE_MASK(kernel_map
));
361 stack
+ kernel_stack_size
+ (2 * PAGE_SIZE
),
362 VM_MAP_REMOVE_KUNWIRE
)
364 panic("stack_collect: vm_map_remove");
371 target
= stack_free_target
+ (STACK_CACHE_SIZE
* processor_count
);
372 target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
375 last_stack_tick
= stack_collect_tick
;
383 * compute_stack_target:
385 * Computes a new target free list count
386 * based on recent alloc / free activity.
388 * Limits stack collection to once per
389 * computation period.
392 compute_stack_target(
400 if (stack_free_target
> 5) {
401 stack_free_target
= (4 * stack_free_target
) / 5;
402 } else if (stack_free_target
> 0) {
406 stack_free_target
+= (stack_free_delta
>= 0)? stack_free_delta
: -stack_free_delta
;
408 stack_free_delta
= 0;
409 stack_collect_tick
++;
416 void stack_privilege(
421 __unused thread_t thread
)
427 * Return info on stack usage for threads in a specific processor set
430 processor_set_stack_usage(
431 processor_set_t pset
,
432 unsigned int *totalp
,
434 vm_size_t
*residentp
,
435 vm_size_t
*maxusagep
,
436 vm_offset_t
*maxstackp
)
439 return KERN_NOT_SUPPORTED
;
443 vm_offset_t maxstack
;
445 thread_t
*thread_list
;
448 unsigned int actual
; /* this many things */
451 vm_size_t size
, size_needed
;
454 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
) {
455 return KERN_INVALID_ARGUMENT
;
462 lck_mtx_lock(&tasks_threads_lock
);
464 actual
= threads_count
;
466 /* do we have the memory we need? */
468 size_needed
= actual
* sizeof(thread_t
);
469 if (size_needed
<= size
) {
473 lck_mtx_unlock(&tasks_threads_lock
);
476 kheap_free(KHEAP_TEMP
, addr
, size
);
479 assert(size_needed
> 0);
482 addr
= kheap_alloc(KHEAP_TEMP
, size
, Z_WAITOK
);
484 return KERN_RESOURCE_SHORTAGE
;
488 /* OK, have memory and list is locked */
489 thread_list
= (thread_t
*) addr
;
490 for (i
= 0, thread
= (thread_t
)(void *) queue_first(&threads
);
491 !queue_end(&threads
, (queue_entry_t
) thread
);
492 thread
= (thread_t
)(void *) queue_next(&thread
->threads
)) {
493 thread_reference_internal(thread
);
494 thread_list
[i
++] = thread
;
498 lck_mtx_unlock(&tasks_threads_lock
);
500 /* calculate maxusage and free thread references */
506 thread_t threadref
= thread_list
[--i
];
508 if (threadref
->kernel_stack
!= 0) {
512 thread_deallocate(threadref
);
516 kheap_free(KHEAP_TEMP
, addr
, size
);
520 *residentp
= *spacep
= total
* round_page(kernel_stack_size
);
521 *maxusagep
= maxusage
;
522 *maxstackp
= maxstack
;
525 #endif /* MACH_DEBUG */
529 min_valid_stack_address(void)
531 return (vm_offset_t
)vm_map_min(kernel_map
);
535 max_valid_stack_address(void)
537 return (vm_offset_t
)vm_map_max(kernel_map
);