15e53a4f9SPedro F. Giffuni /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 35e53a4f9SPedro F. Giffuni * 4bb535300SJeff Roberson * Copyright (c) 2001 Daniel Eischen <deischen@freebsd.org> 5bb535300SJeff Roberson * Copyright (c) 2000-2001 Jason Evans <jasone@freebsd.org> 6bb535300SJeff Roberson * All rights reserved. 7bb535300SJeff Roberson * 8bb535300SJeff Roberson * Redistribution and use in source and binary forms, with or without 9bb535300SJeff Roberson * modification, are permitted provided that the following conditions 10bb535300SJeff Roberson * are met: 11bb535300SJeff Roberson * 1. Redistributions of source code must retain the above copyright 12bb535300SJeff Roberson * notice, this list of conditions and the following disclaimer. 13bb535300SJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 14bb535300SJeff Roberson * notice, this list of conditions and the following disclaimer in the 15bb535300SJeff Roberson * documentation and/or other materials provided with the distribution. 16bb535300SJeff Roberson * 17bb535300SJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18bb535300SJeff Roberson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19bb535300SJeff Roberson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20bb535300SJeff Roberson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21bb535300SJeff Roberson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22bb535300SJeff Roberson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23bb535300SJeff Roberson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24bb535300SJeff Roberson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25bb535300SJeff Roberson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26bb535300SJeff Roberson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27bb535300SJeff Roberson * SUCH DAMAGE. 28bb535300SJeff Roberson */ 29a091d823SDavid Xu 30ebf7a015SKonstantin Belousov #include <sys/param.h> 31e2879eceSKonstantin Belousov #include <sys/auxv.h> 32bb535300SJeff Roberson #include <sys/mman.h> 33bb535300SJeff Roberson #include <sys/queue.h> 34fad128dbSKonstantin Belousov #include <sys/resource.h> 35fad128dbSKonstantin Belousov #include <sys/sysctl.h> 36bb535300SJeff Roberson #include <stdlib.h> 37bb535300SJeff Roberson #include <pthread.h> 38da2fcff7SKonstantin Belousov #include <link.h> 39a091d823SDavid Xu 40bb535300SJeff Roberson #include "thr_private.h" 41bb535300SJeff Roberson 42bb535300SJeff Roberson /* Spare thread stack. */ 43bb535300SJeff Roberson struct stack { 44bb535300SJeff Roberson LIST_ENTRY(stack) qe; /* Stack queue linkage. */ 45bb535300SJeff Roberson size_t stacksize; /* Stack size (rounded up). */ 46bb535300SJeff Roberson size_t guardsize; /* Guard size. */ 47bb535300SJeff Roberson void *stackaddr; /* Stack address. */ 48bb535300SJeff Roberson }; 49bb535300SJeff Roberson 50bb535300SJeff Roberson /* 51a091d823SDavid Xu * Default sized (stack and guard) spare stack queue. Stacks are cached 52a091d823SDavid Xu * to avoid additional complexity managing mmap()ed stack regions. Spare 53a091d823SDavid Xu * stacks are used in LIFO order to increase cache locality. 54bb535300SJeff Roberson */ 55a091d823SDavid Xu static LIST_HEAD(, stack) dstackq = LIST_HEAD_INITIALIZER(dstackq); 56bb535300SJeff Roberson 57bb535300SJeff Roberson /* 58bb535300SJeff Roberson * Miscellaneous sized (non-default stack and/or guard) spare stack queue. 59a091d823SDavid Xu * Stacks are cached to avoid additional complexity managing mmap()ed 60a091d823SDavid Xu * stack regions. This list is unordered, since ordering on both stack 61a091d823SDavid Xu * size and guard size would be more trouble than it's worth. Stacks are 62a091d823SDavid Xu * allocated from this cache on a first size match basis. 63bb535300SJeff Roberson */ 64a091d823SDavid Xu static LIST_HEAD(, stack) mstackq = LIST_HEAD_INITIALIZER(mstackq); 65bb535300SJeff Roberson 66bb535300SJeff Roberson /** 67a091d823SDavid Xu * Base address of the last stack allocated (including its red zone, if 68a091d823SDavid Xu * there is one). Stacks are allocated contiguously, starting beyond the 69a091d823SDavid Xu * top of the main stack. When a new stack is created, a red zone is 70a091d823SDavid Xu * typically created (actually, the red zone is mapped with PROT_NONE) above 71a091d823SDavid Xu * the top of the stack, such that the stack will not be able to grow all 72a091d823SDavid Xu * the way to the bottom of the next stack. This isn't fool-proof. It is 73a091d823SDavid Xu * possible for a stack to grow by a large amount, such that it grows into 74a091d823SDavid Xu * the next stack, and as long as the memory within the red zone is never 75a091d823SDavid Xu * accessed, nothing will prevent one thread stack from trouncing all over 76a091d823SDavid Xu * the next. 77bb535300SJeff Roberson * 78bb535300SJeff Roberson * low memory 79bb535300SJeff Roberson * . . . . . . . . . . . . . . . . . . 80bb535300SJeff Roberson * | | 81bb535300SJeff Roberson * | stack 3 | start of 3rd thread stack 82bb535300SJeff Roberson * +-----------------------------------+ 83bb535300SJeff Roberson * | | 84bb535300SJeff Roberson * | Red Zone (guard page) | red zone for 2nd thread 85bb535300SJeff Roberson * | | 86bb535300SJeff Roberson * +-----------------------------------+ 87a091d823SDavid Xu * | stack 2 - _thr_stack_default | top of 2nd thread stack 88bb535300SJeff Roberson * | | 89bb535300SJeff Roberson * | | 90bb535300SJeff Roberson * | | 91bb535300SJeff Roberson * | | 92bb535300SJeff Roberson * | stack 2 | 93bb535300SJeff Roberson * +-----------------------------------+ <-- start of 2nd thread stack 94bb535300SJeff Roberson * | | 95bb535300SJeff Roberson * | Red Zone | red zone for 1st thread 96bb535300SJeff Roberson * | | 97bb535300SJeff Roberson * +-----------------------------------+ 98a091d823SDavid Xu * | stack 1 - _thr_stack_default | top of 1st thread stack 99bb535300SJeff Roberson * | | 100bb535300SJeff Roberson * | | 101bb535300SJeff Roberson * | | 102bb535300SJeff Roberson * | | 103bb535300SJeff Roberson * | stack 1 | 104bb535300SJeff Roberson * +-----------------------------------+ <-- start of 1st thread stack 105bb535300SJeff Roberson * | | (initial value of last_stack) 106bb535300SJeff Roberson * | Red Zone | 107bb535300SJeff Roberson * | | red zone for main thread 108bb535300SJeff Roberson * +-----------------------------------+ 109a091d823SDavid Xu * | USRSTACK - _thr_stack_initial | top of main thread stack 110bb535300SJeff Roberson * | | ^ 111bb535300SJeff Roberson * | | | 112bb535300SJeff Roberson * | | | 113bb535300SJeff Roberson * | | | stack growth 114bb535300SJeff Roberson * | | 115bb535300SJeff Roberson * +-----------------------------------+ <-- start of main thread stack 116bb535300SJeff Roberson * (USRSTACK) 117bb535300SJeff Roberson * high memory 118bb535300SJeff Roberson * 119bb535300SJeff Roberson */ 12037a6356bSDavid Xu static char *last_stack = NULL; 121bb535300SJeff Roberson 122bb535300SJeff Roberson /* 123a091d823SDavid Xu * Round size up to the nearest multiple of 124a091d823SDavid Xu * _thr_page_size. 125bb535300SJeff Roberson */ 126a091d823SDavid Xu static inline size_t 127a091d823SDavid Xu round_up(size_t size) 128a091d823SDavid Xu { 129*31f688a2SKonstantin Belousov return (roundup2(size, _thr_page_size)); 130a091d823SDavid Xu } 131bb535300SJeff Roberson 132da2fcff7SKonstantin Belousov void 133da2fcff7SKonstantin Belousov _thr_stack_fix_protection(struct pthread *thrd) 134da2fcff7SKonstantin Belousov { 135da2fcff7SKonstantin Belousov 136da2fcff7SKonstantin Belousov mprotect((char *)thrd->attr.stackaddr_attr + 137da2fcff7SKonstantin Belousov round_up(thrd->attr.guardsize_attr), 138da2fcff7SKonstantin Belousov round_up(thrd->attr.stacksize_attr), 139da2fcff7SKonstantin Belousov _rtld_get_stack_prot()); 140da2fcff7SKonstantin Belousov } 141da2fcff7SKonstantin Belousov 142fad128dbSKonstantin Belousov static void 143fad128dbSKonstantin Belousov singlethread_map_stacks_exec(void) 144fad128dbSKonstantin Belousov { 145e03c7f50SKonstantin Belousov char *usrstack; 146e03c7f50SKonstantin Belousov size_t stacksz; 147fad128dbSKonstantin Belousov 148e03c7f50SKonstantin Belousov if (!__thr_get_main_stack_base(&usrstack) || 149e03c7f50SKonstantin Belousov !__thr_get_main_stack_lim(&stacksz)) 150fad128dbSKonstantin Belousov return; 151e03c7f50SKonstantin Belousov mprotect(usrstack - stacksz, stacksz, _rtld_get_stack_prot()); 152fad128dbSKonstantin Belousov } 153fad128dbSKonstantin Belousov 154da2fcff7SKonstantin Belousov void 155bd43f069SKonstantin Belousov __thr_map_stacks_exec(void) 156da2fcff7SKonstantin Belousov { 157da2fcff7SKonstantin Belousov struct pthread *curthread, *thrd; 158da2fcff7SKonstantin Belousov struct stack *st; 159da2fcff7SKonstantin Belousov 160fad128dbSKonstantin Belousov if (!_thr_is_inited()) { 161fad128dbSKonstantin Belousov singlethread_map_stacks_exec(); 162fad128dbSKonstantin Belousov return; 163fad128dbSKonstantin Belousov } 164da2fcff7SKonstantin Belousov curthread = _get_curthread(); 165da2fcff7SKonstantin Belousov THREAD_LIST_RDLOCK(curthread); 166da2fcff7SKonstantin Belousov LIST_FOREACH(st, &mstackq, qe) 167da2fcff7SKonstantin Belousov mprotect((char *)st->stackaddr + st->guardsize, st->stacksize, 168da2fcff7SKonstantin Belousov _rtld_get_stack_prot()); 169da2fcff7SKonstantin Belousov LIST_FOREACH(st, &dstackq, qe) 170da2fcff7SKonstantin Belousov mprotect((char *)st->stackaddr + st->guardsize, st->stacksize, 171da2fcff7SKonstantin Belousov _rtld_get_stack_prot()); 172da2fcff7SKonstantin Belousov TAILQ_FOREACH(thrd, &_thread_gc_list, gcle) 173da2fcff7SKonstantin Belousov _thr_stack_fix_protection(thrd); 174da2fcff7SKonstantin Belousov TAILQ_FOREACH(thrd, &_thread_list, tle) 175da2fcff7SKonstantin Belousov _thr_stack_fix_protection(thrd); 176da2fcff7SKonstantin Belousov THREAD_LIST_UNLOCK(curthread); 177da2fcff7SKonstantin Belousov } 178da2fcff7SKonstantin Belousov 179a091d823SDavid Xu int 180a091d823SDavid Xu _thr_stack_alloc(struct pthread_attr *attr) 181a091d823SDavid Xu { 182a091d823SDavid Xu struct pthread *curthread = _get_curthread(); 183a091d823SDavid Xu struct stack *spare_stack; 184a091d823SDavid Xu size_t stacksize; 185a091d823SDavid Xu size_t guardsize; 186a091d823SDavid Xu char *stackaddr; 187a091d823SDavid Xu 188a091d823SDavid Xu /* 189a091d823SDavid Xu * Round up stack size to nearest multiple of _thr_page_size so 190a091d823SDavid Xu * that mmap() * will work. If the stack size is not an even 191a091d823SDavid Xu * multiple, we end up initializing things such that there is 192a091d823SDavid Xu * unused space above the beginning of the stack, so the stack 193a091d823SDavid Xu * sits snugly against its guard. 194a091d823SDavid Xu */ 195a091d823SDavid Xu stacksize = round_up(attr->stacksize_attr); 196a091d823SDavid Xu guardsize = round_up(attr->guardsize_attr); 197a091d823SDavid Xu 198a091d823SDavid Xu attr->stackaddr_attr = NULL; 199a091d823SDavid Xu attr->flags &= ~THR_STACK_USER; 200a091d823SDavid Xu 201a091d823SDavid Xu /* 202a091d823SDavid Xu * Use the garbage collector lock for synchronization of the 203f75b1ff6SMark Johnston * spare stack lists and allocations from usrstack. 204a091d823SDavid Xu */ 205a9b764e2SDavid Xu THREAD_LIST_WRLOCK(curthread); 206bb535300SJeff Roberson /* 207bb535300SJeff Roberson * If the stack and guard sizes are default, try to allocate a stack 208bb535300SJeff Roberson * from the default-size stack cache: 209bb535300SJeff Roberson */ 210a091d823SDavid Xu if ((stacksize == THR_STACK_DEFAULT) && 211a091d823SDavid Xu (guardsize == _thr_guard_default)) { 212a091d823SDavid Xu if ((spare_stack = LIST_FIRST(&dstackq)) != NULL) { 213bb535300SJeff Roberson /* Use the spare stack. */ 214bb535300SJeff Roberson LIST_REMOVE(spare_stack, qe); 215a091d823SDavid Xu attr->stackaddr_attr = spare_stack->stackaddr; 216bb535300SJeff Roberson } 217bb535300SJeff Roberson } 218bb535300SJeff Roberson /* 219bb535300SJeff Roberson * The user specified a non-default stack and/or guard size, so try to 220bb535300SJeff Roberson * allocate a stack from the non-default size stack cache, using the 221bb535300SJeff Roberson * rounded up stack size (stack_size) in the search: 222bb535300SJeff Roberson */ 223bb535300SJeff Roberson else { 224a091d823SDavid Xu LIST_FOREACH(spare_stack, &mstackq, qe) { 225a091d823SDavid Xu if (spare_stack->stacksize == stacksize && 226bb535300SJeff Roberson spare_stack->guardsize == guardsize) { 227bb535300SJeff Roberson LIST_REMOVE(spare_stack, qe); 228a091d823SDavid Xu attr->stackaddr_attr = spare_stack->stackaddr; 229bb535300SJeff Roberson break; 230bb535300SJeff Roberson } 231bb535300SJeff Roberson } 232bb535300SJeff Roberson } 233a091d823SDavid Xu if (attr->stackaddr_attr != NULL) { 234a091d823SDavid Xu /* A cached stack was found. Release the lock. */ 235a091d823SDavid Xu THREAD_LIST_UNLOCK(curthread); 236a091d823SDavid Xu } 237a091d823SDavid Xu else { 2386c8ce3bfSKonstantin Belousov /* 239f75b1ff6SMark Johnston * Allocate a stack from or below usrstack, depending 2406c8ce3bfSKonstantin Belousov * on the LIBPTHREAD_BIGSTACK_MAIN env variable. 2416c8ce3bfSKonstantin Belousov */ 242bb535300SJeff Roberson if (last_stack == NULL) 243f75b1ff6SMark Johnston last_stack = _usrstack - _thr_stack_initial - 244a091d823SDavid Xu _thr_guard_default; 245bb535300SJeff Roberson 246bb535300SJeff Roberson /* Allocate a new stack. */ 247a091d823SDavid Xu stackaddr = last_stack - stacksize - guardsize; 248bb535300SJeff Roberson 249bb535300SJeff Roberson /* 250a091d823SDavid Xu * Even if stack allocation fails, we don't want to try to 251a091d823SDavid Xu * use this location again, so unconditionally decrement 252bb535300SJeff Roberson * last_stack. Under normal operating conditions, the most 253a091d823SDavid Xu * likely reason for an mmap() error is a stack overflow of 254a091d823SDavid Xu * the adjacent thread stack. 255bb535300SJeff Roberson */ 256a091d823SDavid Xu last_stack -= (stacksize + guardsize); 257bb535300SJeff Roberson 258a091d823SDavid Xu /* Release the lock before mmap'ing it. */ 259a091d823SDavid Xu THREAD_LIST_UNLOCK(curthread); 260a091d823SDavid Xu 261a091d823SDavid Xu /* Map the stack and guard page together, and split guard 262a091d823SDavid Xu page from allocated space: */ 263a091d823SDavid Xu if ((stackaddr = mmap(stackaddr, stacksize + guardsize, 264da2fcff7SKonstantin Belousov _rtld_get_stack_prot(), MAP_STACK, 265a091d823SDavid Xu -1, 0)) != MAP_FAILED && 266a091d823SDavid Xu (guardsize == 0 || 267a091d823SDavid Xu mprotect(stackaddr, guardsize, PROT_NONE) == 0)) { 268a091d823SDavid Xu stackaddr += guardsize; 269a091d823SDavid Xu } else { 270a091d823SDavid Xu if (stackaddr != MAP_FAILED) 271a091d823SDavid Xu munmap(stackaddr, stacksize + guardsize); 272a091d823SDavid Xu stackaddr = NULL; 273a091d823SDavid Xu } 274a091d823SDavid Xu attr->stackaddr_attr = stackaddr; 275a091d823SDavid Xu } 276a091d823SDavid Xu if (attr->stackaddr_attr != NULL) 277a091d823SDavid Xu return (0); 278a091d823SDavid Xu else 279a091d823SDavid Xu return (-1); 280bb535300SJeff Roberson } 281bb535300SJeff Roberson 282a091d823SDavid Xu /* This function must be called with _thread_list_lock held. */ 283bb535300SJeff Roberson void 284a091d823SDavid Xu _thr_stack_free(struct pthread_attr *attr) 285bb535300SJeff Roberson { 286bb535300SJeff Roberson struct stack *spare_stack; 287bb535300SJeff Roberson 288a091d823SDavid Xu if ((attr != NULL) && ((attr->flags & THR_STACK_USER) == 0) 289a091d823SDavid Xu && (attr->stackaddr_attr != NULL)) { 29037a6356bSDavid Xu spare_stack = (struct stack *) 29137a6356bSDavid Xu ((char *)attr->stackaddr_attr + 29237a6356bSDavid Xu attr->stacksize_attr - sizeof(struct stack)); 293a091d823SDavid Xu spare_stack->stacksize = round_up(attr->stacksize_attr); 294a091d823SDavid Xu spare_stack->guardsize = round_up(attr->guardsize_attr); 295a091d823SDavid Xu spare_stack->stackaddr = attr->stackaddr_attr; 296bb535300SJeff Roberson 297a091d823SDavid Xu if (spare_stack->stacksize == THR_STACK_DEFAULT && 298a091d823SDavid Xu spare_stack->guardsize == _thr_guard_default) { 299bb535300SJeff Roberson /* Default stack/guard size. */ 300a091d823SDavid Xu LIST_INSERT_HEAD(&dstackq, spare_stack, qe); 301bb535300SJeff Roberson } else { 302bb535300SJeff Roberson /* Non-default stack/guard size. */ 303a091d823SDavid Xu LIST_INSERT_HEAD(&mstackq, spare_stack, qe); 304a091d823SDavid Xu } 305a091d823SDavid Xu attr->stackaddr_attr = NULL; 306bb535300SJeff Roberson } 307bb535300SJeff Roberson } 308