2 * Copyright (c) 2001 Daniel Eischen <deischen@freebsd.org>
3 * Copyright (c) 2000-2001 Jason Evans <jasone@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
32 #include <sys/queue.h>
33 #include <sys/resource.h>
34 #include <sys/sysctl.h>
39 #include "thr_private.h"
41 /* Spare thread stack. */
43 LIST_ENTRY(stack) qe; /* Stack queue linkage. */
44 size_t stacksize; /* Stack size (rounded up). */
45 size_t guardsize; /* Guard size. */
46 void *stackaddr; /* Stack address. */
50 * Default sized (stack and guard) spare stack queue. Stacks are cached
51 * to avoid additional complexity managing mmap()ed stack regions. Spare
52 * stacks are used in LIFO order to increase cache locality.
54 static LIST_HEAD(, stack) dstackq = LIST_HEAD_INITIALIZER(dstackq);
57 * Miscellaneous sized (non-default stack and/or guard) spare stack queue.
58 * Stacks are cached to avoid additional complexity managing mmap()ed
59 * stack regions. This list is unordered, since ordering on both stack
60 * size and guard size would be more trouble than it's worth. Stacks are
61 * allocated from this cache on a first size match basis.
63 static LIST_HEAD(, stack) mstackq = LIST_HEAD_INITIALIZER(mstackq);
66 * Base address of the last stack allocated (including its red zone, if
67 * there is one). Stacks are allocated contiguously, starting beyond the
68 * top of the main stack. When a new stack is created, a red zone is
69 * typically created (actually, the red zone is mapped with PROT_NONE) above
70 * the top of the stack, such that the stack will not be able to grow all
71 * the way to the bottom of the next stack. This isn't fool-proof. It is
72 * possible for a stack to grow by a large amount, such that it grows into
73 * the next stack, and as long as the memory within the red zone is never
74 * accessed, nothing will prevent one thread stack from trouncing all over
78 * . . . . . . . . . . . . . . . . . .
80 * | stack 3 | start of 3rd thread stack
81 * +-----------------------------------+
83 * | Red Zone (guard page) | red zone for 2nd thread
85 * +-----------------------------------+
86 * | stack 2 - _thr_stack_default | top of 2nd thread stack
92 * +-----------------------------------+ <-- start of 2nd thread stack
94 * | Red Zone | red zone for 1st thread
96 * +-----------------------------------+
97 * | stack 1 - _thr_stack_default | top of 1st thread stack
103 * +-----------------------------------+ <-- start of 1st thread stack
104 * | | (initial value of last_stack)
106 * | | red zone for main thread
107 * +-----------------------------------+
108 * | USRSTACK - _thr_stack_initial | top of main thread stack
114 * +-----------------------------------+ <-- start of main thread stack
119 static char *last_stack = NULL;
122 * Round size up to the nearest multiple of
126 round_up(size_t size)
128 if (size % _thr_page_size != 0)
129 size = ((size / _thr_page_size) + 1) *
135 _thr_stack_fix_protection(struct pthread *thrd)
138 mprotect((char *)thrd->attr.stackaddr_attr +
139 round_up(thrd->attr.guardsize_attr),
140 round_up(thrd->attr.stacksize_attr),
141 _rtld_get_stack_prot());
145 singlethread_map_stacks_exec(void)
153 mib[1] = KERN_USRSTACK;
154 len = sizeof(usrstack);
155 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &usrstack, &len, NULL, 0)
158 if (getrlimit(RLIMIT_STACK, &rlim) == -1)
160 mprotect((void *)(uintptr_t)(usrstack - rlim.rlim_cur),
161 rlim.rlim_cur, _rtld_get_stack_prot());
165 __thr_map_stacks_exec(void)
167 struct pthread *curthread, *thrd;
170 if (!_thr_is_inited()) {
171 singlethread_map_stacks_exec();
174 curthread = _get_curthread();
175 THREAD_LIST_RDLOCK(curthread);
176 LIST_FOREACH(st, &mstackq, qe)
177 mprotect((char *)st->stackaddr + st->guardsize, st->stacksize,
178 _rtld_get_stack_prot());
179 LIST_FOREACH(st, &dstackq, qe)
180 mprotect((char *)st->stackaddr + st->guardsize, st->stacksize,
181 _rtld_get_stack_prot());
182 TAILQ_FOREACH(thrd, &_thread_gc_list, gcle)
183 _thr_stack_fix_protection(thrd);
184 TAILQ_FOREACH(thrd, &_thread_list, tle)
185 _thr_stack_fix_protection(thrd);
186 THREAD_LIST_UNLOCK(curthread);
190 _thr_stack_alloc(struct pthread_attr *attr)
192 struct pthread *curthread = _get_curthread();
193 struct stack *spare_stack;
199 * Round up stack size to nearest multiple of _thr_page_size so
200 * that mmap() * will work. If the stack size is not an even
201 * multiple, we end up initializing things such that there is
202 * unused space above the beginning of the stack, so the stack
203 * sits snugly against its guard.
205 stacksize = round_up(attr->stacksize_attr);
206 guardsize = round_up(attr->guardsize_attr);
208 attr->stackaddr_attr = NULL;
209 attr->flags &= ~THR_STACK_USER;
212 * Use the garbage collector lock for synchronization of the
213 * spare stack lists and allocations from usrstack.
215 THREAD_LIST_WRLOCK(curthread);
217 * If the stack and guard sizes are default, try to allocate a stack
218 * from the default-size stack cache:
220 if ((stacksize == THR_STACK_DEFAULT) &&
221 (guardsize == _thr_guard_default)) {
222 if ((spare_stack = LIST_FIRST(&dstackq)) != NULL) {
223 /* Use the spare stack. */
224 LIST_REMOVE(spare_stack, qe);
225 attr->stackaddr_attr = spare_stack->stackaddr;
229 * The user specified a non-default stack and/or guard size, so try to
230 * allocate a stack from the non-default size stack cache, using the
231 * rounded up stack size (stack_size) in the search:
234 LIST_FOREACH(spare_stack, &mstackq, qe) {
235 if (spare_stack->stacksize == stacksize &&
236 spare_stack->guardsize == guardsize) {
237 LIST_REMOVE(spare_stack, qe);
238 attr->stackaddr_attr = spare_stack->stackaddr;
243 if (attr->stackaddr_attr != NULL) {
244 /* A cached stack was found. Release the lock. */
245 THREAD_LIST_UNLOCK(curthread);
249 * Allocate a stack from or below usrstack, depending
250 * on the LIBPTHREAD_BIGSTACK_MAIN env variable.
252 if (last_stack == NULL)
253 last_stack = _usrstack - _thr_stack_initial -
256 /* Allocate a new stack. */
257 stackaddr = last_stack - stacksize - guardsize;
260 * Even if stack allocation fails, we don't want to try to
261 * use this location again, so unconditionally decrement
262 * last_stack. Under normal operating conditions, the most
263 * likely reason for an mmap() error is a stack overflow of
264 * the adjacent thread stack.
266 last_stack -= (stacksize + guardsize);
268 /* Release the lock before mmap'ing it. */
269 THREAD_LIST_UNLOCK(curthread);
271 /* Map the stack and guard page together, and split guard
272 page from allocated space: */
273 if ((stackaddr = mmap(stackaddr, stacksize + guardsize,
274 _rtld_get_stack_prot(), MAP_STACK,
275 -1, 0)) != MAP_FAILED &&
277 mprotect(stackaddr, guardsize, PROT_NONE) == 0)) {
278 stackaddr += guardsize;
280 if (stackaddr != MAP_FAILED)
281 munmap(stackaddr, stacksize + guardsize);
284 attr->stackaddr_attr = stackaddr;
286 if (attr->stackaddr_attr != NULL)
292 /* This function must be called with _thread_list_lock held. */
294 _thr_stack_free(struct pthread_attr *attr)
296 struct stack *spare_stack;
298 if ((attr != NULL) && ((attr->flags & THR_STACK_USER) == 0)
299 && (attr->stackaddr_attr != NULL)) {
300 spare_stack = (struct stack *)
301 ((char *)attr->stackaddr_attr +
302 attr->stacksize_attr - sizeof(struct stack));
303 spare_stack->stacksize = round_up(attr->stacksize_attr);
304 spare_stack->guardsize = round_up(attr->guardsize_attr);
305 spare_stack->stackaddr = attr->stackaddr_attr;
307 if (spare_stack->stacksize == THR_STACK_DEFAULT &&
308 spare_stack->guardsize == _thr_guard_default) {
309 /* Default stack/guard size. */
310 LIST_INSERT_HEAD(&dstackq, spare_stack, qe);
312 /* Non-default stack/guard size. */
313 LIST_INSERT_HEAD(&mstackq, spare_stack, qe);
315 attr->stackaddr_attr = NULL;