2017-11-26 02:00:33 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2003-04-01 03:46:29 +00:00
|
|
|
* Copyright (c) 2001 Daniel Eischen <deischen@freebsd.org>
|
|
|
|
* Copyright (c) 2000-2001 Jason Evans <jasone@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2016-04-08 11:15:26 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/queue.h>
|
2011-01-10 16:10:25 +00:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/sysctl.h>
|
2003-04-01 03:46:29 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
2011-01-09 12:38:40 +00:00
|
|
|
#include <link.h>
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
#include "thr_private.h"
|
|
|
|
|
|
|
|
/* Spare thread stack. */
|
|
|
|
struct stack {
|
|
|
|
LIST_ENTRY(stack) qe; /* Stack queue linkage. */
|
|
|
|
size_t stacksize; /* Stack size (rounded up). */
|
|
|
|
size_t guardsize; /* Guard size. */
|
|
|
|
void *stackaddr; /* Stack address. */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2005-04-02 01:20:00 +00:00
|
|
|
* Default sized (stack and guard) spare stack queue. Stacks are cached
|
|
|
|
* to avoid additional complexity managing mmap()ed stack regions. Spare
|
|
|
|
* stacks are used in LIFO order to increase cache locality.
|
2003-04-01 03:46:29 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
static LIST_HEAD(, stack) dstackq = LIST_HEAD_INITIALIZER(dstackq);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Miscellaneous sized (non-default stack and/or guard) spare stack queue.
|
2005-04-02 01:20:00 +00:00
|
|
|
* Stacks are cached to avoid additional complexity managing mmap()ed
|
|
|
|
* stack regions. This list is unordered, since ordering on both stack
|
|
|
|
* size and guard size would be more trouble than it's worth. Stacks are
|
|
|
|
* allocated from this cache on a first size match basis.
|
2003-04-01 03:46:29 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
static LIST_HEAD(, stack) mstackq = LIST_HEAD_INITIALIZER(mstackq);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
/**
|
2005-04-02 01:20:00 +00:00
|
|
|
* Base address of the last stack allocated (including its red zone, if
|
|
|
|
* there is one). Stacks are allocated contiguously, starting beyond the
|
|
|
|
* top of the main stack. When a new stack is created, a red zone is
|
|
|
|
* typically created (actually, the red zone is mapped with PROT_NONE) above
|
|
|
|
* the top of the stack, such that the stack will not be able to grow all
|
|
|
|
* the way to the bottom of the next stack. This isn't fool-proof. It is
|
|
|
|
* possible for a stack to grow by a large amount, such that it grows into
|
|
|
|
* the next stack, and as long as the memory within the red zone is never
|
|
|
|
* accessed, nothing will prevent one thread stack from trouncing all over
|
|
|
|
* the next.
|
2003-04-01 03:46:29 +00:00
|
|
|
*
|
|
|
|
* low memory
|
|
|
|
* . . . . . . . . . . . . . . . . . .
|
|
|
|
* | |
|
|
|
|
* | stack 3 | start of 3rd thread stack
|
|
|
|
* +-----------------------------------+
|
|
|
|
* | |
|
|
|
|
* | Red Zone (guard page) | red zone for 2nd thread
|
|
|
|
* | |
|
|
|
|
* +-----------------------------------+
|
2005-04-02 01:20:00 +00:00
|
|
|
* | stack 2 - _thr_stack_default | top of 2nd thread stack
|
2003-04-01 03:46:29 +00:00
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* | stack 2 |
|
|
|
|
* +-----------------------------------+ <-- start of 2nd thread stack
|
|
|
|
* | |
|
|
|
|
* | Red Zone | red zone for 1st thread
|
|
|
|
* | |
|
|
|
|
* +-----------------------------------+
|
2005-04-02 01:20:00 +00:00
|
|
|
* | stack 1 - _thr_stack_default | top of 1st thread stack
|
2003-04-01 03:46:29 +00:00
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* | stack 1 |
|
|
|
|
* +-----------------------------------+ <-- start of 1st thread stack
|
|
|
|
* | | (initial value of last_stack)
|
|
|
|
* | Red Zone |
|
|
|
|
* | | red zone for main thread
|
|
|
|
* +-----------------------------------+
|
2005-04-02 01:20:00 +00:00
|
|
|
* | USRSTACK - _thr_stack_initial | top of main thread stack
|
2003-04-01 03:46:29 +00:00
|
|
|
* | | ^
|
|
|
|
* | | |
|
|
|
|
* | | |
|
|
|
|
* | | | stack growth
|
|
|
|
* | |
|
|
|
|
* +-----------------------------------+ <-- start of main thread stack
|
|
|
|
* (USRSTACK)
|
|
|
|
* high memory
|
|
|
|
*
|
|
|
|
*/
|
2006-04-04 02:57:49 +00:00
|
|
|
static char *last_stack = NULL;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Round size up to the nearest multiple of
|
|
|
|
* _thr_page_size.
|
|
|
|
*/
|
|
|
|
static inline size_t
|
|
|
|
round_up(size_t size)
|
|
|
|
{
|
|
|
|
if (size % _thr_page_size != 0)
|
|
|
|
size = ((size / _thr_page_size) + 1) *
|
|
|
|
_thr_page_size;
|
|
|
|
return size;
|
|
|
|
}
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2011-01-09 12:38:40 +00:00
|
|
|
void
|
|
|
|
_thr_stack_fix_protection(struct pthread *thrd)
|
|
|
|
{
|
|
|
|
|
|
|
|
mprotect((char *)thrd->attr.stackaddr_attr +
|
|
|
|
round_up(thrd->attr.guardsize_attr),
|
|
|
|
round_up(thrd->attr.stacksize_attr),
|
|
|
|
_rtld_get_stack_prot());
|
|
|
|
}
|
|
|
|
|
2011-01-10 16:10:25 +00:00
|
|
|
static void
|
|
|
|
singlethread_map_stacks_exec(void)
|
|
|
|
{
|
|
|
|
int mib[2];
|
|
|
|
struct rlimit rlim;
|
|
|
|
u_long usrstack;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_USRSTACK;
|
|
|
|
len = sizeof(usrstack);
|
|
|
|
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &usrstack, &len, NULL, 0)
|
|
|
|
== -1)
|
|
|
|
return;
|
|
|
|
if (getrlimit(RLIMIT_STACK, &rlim) == -1)
|
|
|
|
return;
|
|
|
|
mprotect((void *)(uintptr_t)(usrstack - rlim.rlim_cur),
|
|
|
|
rlim.rlim_cur, _rtld_get_stack_prot());
|
|
|
|
}
|
|
|
|
|
2011-01-09 12:38:40 +00:00
|
|
|
void
|
2016-02-08 19:24:13 +00:00
|
|
|
__thr_map_stacks_exec(void)
|
2011-01-09 12:38:40 +00:00
|
|
|
{
|
|
|
|
struct pthread *curthread, *thrd;
|
|
|
|
struct stack *st;
|
|
|
|
|
2011-01-10 16:10:25 +00:00
|
|
|
if (!_thr_is_inited()) {
|
|
|
|
singlethread_map_stacks_exec();
|
|
|
|
return;
|
|
|
|
}
|
2011-01-09 12:38:40 +00:00
|
|
|
curthread = _get_curthread();
|
|
|
|
THREAD_LIST_RDLOCK(curthread);
|
|
|
|
LIST_FOREACH(st, &mstackq, qe)
|
|
|
|
mprotect((char *)st->stackaddr + st->guardsize, st->stacksize,
|
|
|
|
_rtld_get_stack_prot());
|
|
|
|
LIST_FOREACH(st, &dstackq, qe)
|
|
|
|
mprotect((char *)st->stackaddr + st->guardsize, st->stacksize,
|
|
|
|
_rtld_get_stack_prot());
|
|
|
|
TAILQ_FOREACH(thrd, &_thread_gc_list, gcle)
|
|
|
|
_thr_stack_fix_protection(thrd);
|
|
|
|
TAILQ_FOREACH(thrd, &_thread_list, tle)
|
|
|
|
_thr_stack_fix_protection(thrd);
|
|
|
|
THREAD_LIST_UNLOCK(curthread);
|
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
int
|
|
|
|
_thr_stack_alloc(struct pthread_attr *attr)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
struct stack *spare_stack;
|
|
|
|
size_t stacksize;
|
|
|
|
size_t guardsize;
|
|
|
|
char *stackaddr;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
/*
|
2005-04-02 01:20:00 +00:00
|
|
|
* Round up stack size to nearest multiple of _thr_page_size so
|
|
|
|
* that mmap() * will work. If the stack size is not an even
|
|
|
|
* multiple, we end up initializing things such that there is
|
|
|
|
* unused space above the beginning of the stack, so the stack
|
|
|
|
* sits snugly against its guard.
|
2003-04-01 03:46:29 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
stacksize = round_up(attr->stacksize_attr);
|
|
|
|
guardsize = round_up(attr->guardsize_attr);
|
|
|
|
|
|
|
|
attr->stackaddr_attr = NULL;
|
|
|
|
attr->flags &= ~THR_STACK_USER;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
/*
|
|
|
|
* Use the garbage collector lock for synchronization of the
|
|
|
|
* spare stack lists and allocations from usrstack.
|
|
|
|
*/
|
2010-09-13 07:03:01 +00:00
|
|
|
THREAD_LIST_WRLOCK(curthread);
|
2003-04-01 03:46:29 +00:00
|
|
|
/*
|
|
|
|
* If the stack and guard sizes are default, try to allocate a stack
|
|
|
|
* from the default-size stack cache:
|
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
if ((stacksize == THR_STACK_DEFAULT) &&
|
|
|
|
(guardsize == _thr_guard_default)) {
|
|
|
|
if ((spare_stack = LIST_FIRST(&dstackq)) != NULL) {
|
|
|
|
/* Use the spare stack. */
|
2003-04-01 03:46:29 +00:00
|
|
|
LIST_REMOVE(spare_stack, qe);
|
2005-04-02 01:20:00 +00:00
|
|
|
attr->stackaddr_attr = spare_stack->stackaddr;
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* The user specified a non-default stack and/or guard size, so try to
|
|
|
|
* allocate a stack from the non-default size stack cache, using the
|
|
|
|
* rounded up stack size (stack_size) in the search:
|
|
|
|
*/
|
|
|
|
else {
|
2005-04-02 01:20:00 +00:00
|
|
|
LIST_FOREACH(spare_stack, &mstackq, qe) {
|
|
|
|
if (spare_stack->stacksize == stacksize &&
|
2003-04-01 03:46:29 +00:00
|
|
|
spare_stack->guardsize == guardsize) {
|
|
|
|
LIST_REMOVE(spare_stack, qe);
|
2005-04-02 01:20:00 +00:00
|
|
|
attr->stackaddr_attr = spare_stack->stackaddr;
|
2003-04-01 03:46:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
if (attr->stackaddr_attr != NULL) {
|
|
|
|
/* A cached stack was found. Release the lock. */
|
|
|
|
THREAD_LIST_UNLOCK(curthread);
|
|
|
|
}
|
|
|
|
else {
|
2014-08-13 05:53:41 +00:00
|
|
|
/*
|
|
|
|
* Allocate a stack from or below usrstack, depending
|
|
|
|
* on the LIBPTHREAD_BIGSTACK_MAIN env variable.
|
|
|
|
*/
|
2003-04-01 03:46:29 +00:00
|
|
|
if (last_stack == NULL)
|
2005-04-02 01:20:00 +00:00
|
|
|
last_stack = _usrstack - _thr_stack_initial -
|
|
|
|
_thr_guard_default;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
/* Allocate a new stack. */
|
2005-04-02 01:20:00 +00:00
|
|
|
stackaddr = last_stack - stacksize - guardsize;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
/*
|
2005-04-02 01:20:00 +00:00
|
|
|
* Even if stack allocation fails, we don't want to try to
|
|
|
|
* use this location again, so unconditionally decrement
|
2003-04-01 03:46:29 +00:00
|
|
|
* last_stack. Under normal operating conditions, the most
|
2005-04-02 01:20:00 +00:00
|
|
|
* likely reason for an mmap() error is a stack overflow of
|
|
|
|
* the adjacent thread stack.
|
2003-04-01 03:46:29 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
last_stack -= (stacksize + guardsize);
|
|
|
|
|
|
|
|
/* Release the lock before mmap'ing it. */
|
|
|
|
THREAD_LIST_UNLOCK(curthread);
|
|
|
|
|
|
|
|
/* Map the stack and guard page together, and split guard
|
|
|
|
page from allocated space: */
|
2014-08-13 05:47:49 +00:00
|
|
|
if ((stackaddr = mmap(stackaddr, stacksize + guardsize,
|
2011-01-09 12:38:40 +00:00
|
|
|
_rtld_get_stack_prot(), MAP_STACK,
|
2005-04-02 01:20:00 +00:00
|
|
|
-1, 0)) != MAP_FAILED &&
|
|
|
|
(guardsize == 0 ||
|
|
|
|
mprotect(stackaddr, guardsize, PROT_NONE) == 0)) {
|
|
|
|
stackaddr += guardsize;
|
|
|
|
} else {
|
|
|
|
if (stackaddr != MAP_FAILED)
|
|
|
|
munmap(stackaddr, stacksize + guardsize);
|
|
|
|
stackaddr = NULL;
|
|
|
|
}
|
|
|
|
attr->stackaddr_attr = stackaddr;
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
if (attr->stackaddr_attr != NULL)
|
|
|
|
return (0);
|
|
|
|
else
|
|
|
|
return (-1);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
/* This function must be called with _thread_list_lock held. */
|
2003-04-01 03:46:29 +00:00
|
|
|
void
|
2005-04-02 01:20:00 +00:00
|
|
|
_thr_stack_free(struct pthread_attr *attr)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct stack *spare_stack;
|
|
|
|
|
|
|
|
if ((attr != NULL) && ((attr->flags & THR_STACK_USER) == 0)
|
|
|
|
&& (attr->stackaddr_attr != NULL)) {
|
2006-04-04 02:57:49 +00:00
|
|
|
spare_stack = (struct stack *)
|
|
|
|
((char *)attr->stackaddr_attr +
|
|
|
|
attr->stacksize_attr - sizeof(struct stack));
|
2005-04-02 01:20:00 +00:00
|
|
|
spare_stack->stacksize = round_up(attr->stacksize_attr);
|
|
|
|
spare_stack->guardsize = round_up(attr->guardsize_attr);
|
|
|
|
spare_stack->stackaddr = attr->stackaddr_attr;
|
|
|
|
|
|
|
|
if (spare_stack->stacksize == THR_STACK_DEFAULT &&
|
|
|
|
spare_stack->guardsize == _thr_guard_default) {
|
|
|
|
/* Default stack/guard size. */
|
|
|
|
LIST_INSERT_HEAD(&dstackq, spare_stack, qe);
|
|
|
|
} else {
|
|
|
|
/* Non-default stack/guard size. */
|
|
|
|
LIST_INSERT_HEAD(&mstackq, spare_stack, qe);
|
|
|
|
}
|
|
|
|
attr->stackaddr_attr = NULL;
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
}
|