2006-02-12 14:41:53 +03:00
|
|
|
/* $NetBSD: pthread_stack.c,v 1.17 2006/02/12 11:41:53 yamt Exp $ */
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2001 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Nathan J. Williams.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*/
|
|
|
|
|
2003-03-08 11:03:34 +03:00
|
|
|
#include <sys/cdefs.h>
|
2006-02-12 14:41:53 +03:00
|
|
|
__RCSID("$NetBSD: pthread_stack.c,v 1.17 2006/02/12 11:41:53 yamt Exp $");
|
2003-03-08 11:03:34 +03:00
|
|
|
|
2004-07-19 01:24:52 +04:00
|
|
|
#define __EXPOSE_STACK 1
|
|
|
|
#include <sys/param.h>
|
2003-01-18 13:32:11 +03:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
2004-01-02 22:14:00 +03:00
|
|
|
#include <stddef.h>
|
2003-01-18 13:32:11 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ucontext.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/mman.h>
|
2003-11-27 19:30:54 +03:00
|
|
|
#include <sys/resource.h>
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
#include <sched.h>
|
|
|
|
#include "pthread.h"
|
|
|
|
#include "pthread_int.h"
|
|
|
|
|
2006-02-12 14:41:53 +03:00
|
|
|
static int pthread__stackid_setup(void *, size_t, pthread_t *);
|
2003-11-27 19:30:54 +03:00
|
|
|
|
|
|
|
#ifndef PT_FIXEDSTACKSIZE_LG
|
|
|
|
/*
|
2004-03-14 04:20:01 +03:00
|
|
|
* We have to initialize the pthread_stack* variables here because
|
|
|
|
* mutexes are used before pthread_init() and thus pthread__initmain()
|
|
|
|
* are called. Since mutexes only save the stack pointer and not a
|
2003-11-27 19:30:54 +03:00
|
|
|
* pointer to the thread data, it is safe to change the mapping from
|
|
|
|
* stack pointer to thread data afterwards.
|
|
|
|
*/
|
|
|
|
#define _STACKSIZE_LG 18
|
2004-02-02 23:36:18 +03:00
|
|
|
int pthread_stacksize_lg = _STACKSIZE_LG;
|
|
|
|
size_t pthread_stacksize = 1 << _STACKSIZE_LG;
|
|
|
|
vaddr_t pthread_stackmask = (1 << _STACKSIZE_LG) - 1;
|
2003-11-27 19:30:54 +03:00
|
|
|
#undef _STACKSIZE_LG
|
|
|
|
#endif /* !PT_FIXEDSTACKSIZE_LG */
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
|
2003-07-18 01:07:39 +04:00
|
|
|
/*
|
|
|
|
* Allocate a stack for a thread, and set it up. It needs to be aligned, so
|
2003-01-18 13:32:11 +03:00
|
|
|
* that a thread can find itself by its stack pointer.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pthread__stackalloc(pthread_t *newt)
|
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
|
2003-07-18 01:07:39 +04:00
|
|
|
addr = mmap(NULL, PT_STACKSIZE, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_ANON|MAP_PRIVATE | MAP_ALIGNED(PT_STACKSIZE_LG), -1, (off_t)0);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
|
|
|
if (addr == MAP_FAILED)
|
|
|
|
return ENOMEM;
|
|
|
|
|
2003-07-18 01:07:39 +04:00
|
|
|
pthread__assert(((intptr_t)addr & PT_STACKMASK) == 0);
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2006-02-12 14:41:53 +03:00
|
|
|
return pthread__stackid_setup(addr, PT_STACKSIZE, newt);
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the slightly special stack for the "initial" thread, which
|
|
|
|
* runs on the normal system stack, and thus gets slightly different
|
|
|
|
* treatment.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pthread__initmain(pthread_t *newt)
|
|
|
|
{
|
2004-08-17 18:16:00 +04:00
|
|
|
pthread_t t;
|
2003-01-18 13:32:11 +03:00
|
|
|
void *base;
|
2004-07-26 03:22:43 +04:00
|
|
|
size_t size;
|
2006-02-12 14:41:53 +03:00
|
|
|
int error;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2003-11-27 19:30:54 +03:00
|
|
|
#ifndef PT_FIXEDSTACKSIZE_LG
|
|
|
|
struct rlimit slimit;
|
|
|
|
size_t pagesize;
|
|
|
|
char *value;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pagesize = (size_t)sysconf(_SC_PAGESIZE);
|
2004-02-02 23:36:18 +03:00
|
|
|
pthread_stacksize = 0;
|
2003-11-27 19:30:54 +03:00
|
|
|
ret = getrlimit(RLIMIT_STACK, &slimit);
|
|
|
|
if (ret == -1)
|
|
|
|
err(1, "Couldn't get stack resource consumption limits");
|
|
|
|
value = getenv("PTHREAD_STACKSIZE");
|
|
|
|
if (value) {
|
2004-02-02 23:36:18 +03:00
|
|
|
pthread_stacksize = atoi(value) * 1024;
|
|
|
|
if (pthread_stacksize > slimit.rlim_cur)
|
|
|
|
pthread_stacksize = (size_t)slimit.rlim_cur;
|
2003-11-27 19:30:54 +03:00
|
|
|
}
|
2004-02-02 23:36:18 +03:00
|
|
|
if (pthread_stacksize == 0)
|
|
|
|
pthread_stacksize = (size_t)slimit.rlim_cur;
|
|
|
|
if (pthread_stacksize < 4 * pagesize)
|
2003-11-27 19:30:54 +03:00
|
|
|
errx(1, "Stacksize limit is too low, minimum %zd kbyte.",
|
|
|
|
4 * pagesize / 1024);
|
|
|
|
|
2004-02-02 23:36:18 +03:00
|
|
|
pthread_stacksize_lg = -1;
|
|
|
|
while (pthread_stacksize) {
|
|
|
|
pthread_stacksize >>= 1;
|
|
|
|
pthread_stacksize_lg++;
|
2003-11-27 19:30:54 +03:00
|
|
|
}
|
|
|
|
|
2004-02-02 23:36:18 +03:00
|
|
|
pthread_stacksize = (1 << pthread_stacksize_lg);
|
|
|
|
pthread_stackmask = pthread_stacksize - 1;
|
2004-07-26 03:22:43 +04:00
|
|
|
#endif /* PT_FIXEDSTACKSIZE_LG */
|
2003-11-27 19:30:54 +03:00
|
|
|
|
2004-08-17 18:16:00 +04:00
|
|
|
base = (void *)(pthread__sp() & ~PT_STACKMASK);
|
|
|
|
size = PT_STACKSIZE;
|
|
|
|
|
2006-02-12 14:41:53 +03:00
|
|
|
error = pthread__stackid_setup(base, size, &t);
|
|
|
|
if (error) {
|
|
|
|
/* XXX */
|
|
|
|
errx(2, "failed to setup main thread: error=%d", error);
|
|
|
|
}
|
2004-08-17 18:16:00 +04:00
|
|
|
|
2003-11-27 19:30:54 +03:00
|
|
|
/*
|
2004-08-17 18:16:00 +04:00
|
|
|
* The "safe" area chosen below isn't safe for the initial thread stack
|
2004-07-26 03:22:43 +04:00
|
|
|
* because we don't control the end of the stack.
|
|
|
|
* For example, on i386 the stack usually ends at 0xbfc00000,
|
2004-08-17 18:16:00 +04:00
|
|
|
* so for requested sizes >=8MB, the last 4MB of stack isn't available.
|
2004-07-26 03:22:43 +04:00
|
|
|
* Also, we don't want to clobber the argv, environment, etc.
|
2004-08-17 18:16:00 +04:00
|
|
|
* Reset the initial pt_uc pointer to be safe for the initial thread.
|
2003-11-27 19:30:54 +03:00
|
|
|
*/
|
2004-08-17 18:16:00 +04:00
|
|
|
|
2004-07-26 03:22:43 +04:00
|
|
|
#ifdef __MACHINE_STACK_GROWS_UP
|
2004-08-17 18:16:00 +04:00
|
|
|
t->pt_uc = (ucontext_t *)
|
|
|
|
((char *)t->pt_stack.ss_sp + t->pt_stack.ss_size -
|
|
|
|
sizeof (ucontext_t));
|
2004-07-26 03:22:43 +04:00
|
|
|
#else
|
2004-08-17 18:16:00 +04:00
|
|
|
t->pt_uc = (ucontext_t *)t->pt_stack.ss_sp;
|
2004-07-26 03:22:43 +04:00
|
|
|
#endif
|
2004-08-17 18:16:00 +04:00
|
|
|
*newt = t;
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
2006-02-12 14:41:53 +03:00
|
|
|
static int
|
2003-01-18 21:40:52 +03:00
|
|
|
/*ARGSUSED*/
|
2006-02-12 14:41:53 +03:00
|
|
|
pthread__stackid_setup(void *base, size_t size, pthread_t *tp)
|
2003-01-18 13:32:11 +03:00
|
|
|
{
|
|
|
|
pthread_t t;
|
2004-07-19 01:24:52 +04:00
|
|
|
void *redaddr;
|
2003-01-18 21:40:52 +03:00
|
|
|
size_t pagesize;
|
2004-07-20 05:51:49 +04:00
|
|
|
caddr_t sp;
|
2003-01-18 21:40:52 +03:00
|
|
|
int ret;
|
2003-01-18 13:32:11 +03:00
|
|
|
|
2004-07-19 01:24:52 +04:00
|
|
|
t = base;
|
2003-01-18 21:40:52 +03:00
|
|
|
pagesize = (size_t)sysconf(_SC_PAGESIZE);
|
2004-07-19 01:24:52 +04:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/*
|
|
|
|
* Put a pointer to the pthread in the bottom (but
|
|
|
|
* redzone-protected section) of the stack.
|
|
|
|
*/
|
2004-07-19 01:24:52 +04:00
|
|
|
|
|
|
|
redaddr = STACK_SHRINK(STACK_MAX(base, size), pagesize);
|
2004-07-26 03:22:43 +04:00
|
|
|
t->pt_stack.ss_size = size - 2 * pagesize;
|
2004-07-19 01:24:52 +04:00
|
|
|
#ifdef __MACHINE_STACK_GROWS_UP
|
|
|
|
t->pt_stack.ss_sp = (char *)base + pagesize;
|
2004-07-20 05:51:49 +04:00
|
|
|
sp = t->pt_stack.ss_sp;
|
2004-07-19 01:24:52 +04:00
|
|
|
#else
|
2003-01-18 13:32:11 +03:00
|
|
|
t->pt_stack.ss_sp = (char *)base + 2 * pagesize;
|
2004-07-20 05:51:49 +04:00
|
|
|
sp = (caddr_t)t->pt_stack.ss_sp + t->pt_stack.ss_size;
|
2004-07-19 01:24:52 +04:00
|
|
|
#endif
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/* Set up an initial ucontext pointer to a "safe" area */
|
2004-07-19 01:24:52 +04:00
|
|
|
t->pt_uc = (ucontext_t *)
|
2004-07-20 05:51:49 +04:00
|
|
|
STACK_ALIGN(STACK_GROW(sp, pagesize / 2), ~_UC_UCONTEXT_ALIGN);
|
2004-07-19 01:24:52 +04:00
|
|
|
|
|
|
|
/* Protect the next-to-bottom stack page as a red zone. */
|
|
|
|
ret = mprotect(redaddr, pagesize, PROT_NONE);
|
2006-02-12 14:41:53 +03:00
|
|
|
if (ret == -1) {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
#ifdef PTHREAD_MLOCK_KLUDGE
|
|
|
|
ret = mlock(t, sizeof(struct __pthread_st));
|
|
|
|
if (ret < 0) {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
*tp = t;
|
|
|
|
return 0;
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
2004-01-02 22:14:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
pthread__stackinfo_offset()
|
|
|
|
{
|
|
|
|
size_t pagesize;
|
|
|
|
|
|
|
|
pagesize = (size_t)sysconf(_SC_PAGESIZE);
|
2004-07-19 01:24:52 +04:00
|
|
|
|
|
|
|
#ifdef __MACHINE_STACK_GROWS_UP
|
|
|
|
return (-pagesize + offsetof(struct __pthread_st, pt_stackinfo));
|
|
|
|
#else
|
|
|
|
return (-(2 * pagesize) + offsetof(struct __pthread_st, pt_stackinfo));
|
|
|
|
#endif
|
2004-01-02 22:14:00 +03:00
|
|
|
}
|