From 14e9bfa8fe88e6350016240194199cefbd2c3d47 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 26 Oct 2001 21:19:22 +0000 Subject: Make libc_r check the kern.usrstack sysctl instead of using internal kernel #defines to figure out where the stack is located. This stops libc_r from exploding when the kernel is compiled with a different KVM size. IMHO this is all kinda bogus, it would be better to just check %esp and work from that. --- lib/libc_r/uthread/pthread_private.h | 7 +++++++ lib/libc_r/uthread/uthread_init.c | 11 ++++++++--- lib/libc_r/uthread/uthread_stack.c | 8 +++++--- lib/libkse/thread/thr_init.c | 11 ++++++++--- lib/libkse/thread/thr_private.h | 7 +++++++ lib/libkse/thread/thr_stack.c | 8 +++++--- lib/libpthread/thread/thr_init.c | 11 ++++++++--- lib/libpthread/thread/thr_private.h | 7 +++++++ lib/libpthread/thread/thr_stack.c | 8 +++++--- 9 files changed, 60 insertions(+), 18 deletions(-) diff --git a/lib/libc_r/uthread/pthread_private.h b/lib/libc_r/uthread/pthread_private.h index 9af2090..151fd03 100644 --- a/lib/libc_r/uthread/pthread_private.h +++ b/lib/libc_r/uthread/pthread_private.h @@ -881,6 +881,13 @@ struct pthread { * Global variables for the uthread kernel. */ +SCLASS void *_usrstack +#ifdef GLOBAL_PTHREAD_PRIVATE += (void *) USRSTACK; +#else +; +#endif + /* Kernel thread structure used when there are no running threads: */ SCLASS struct pthread _thread_kern_thread; diff --git a/lib/libc_r/uthread/uthread_init.c b/lib/libc_r/uthread/uthread_init.c index 7bf09e5..30ec765 100644 --- a/lib/libc_r/uthread/uthread_init.c +++ b/lib/libc_r/uthread/uthread_init.c @@ -266,6 +266,12 @@ _thread_init(void) memcpy((void *) &_thread_initial->attr, &pthread_attr_default, sizeof(struct pthread_attr)); + /* Find the stack top */ + mib[0] = CTL_KERN; + mib[1] = KERN_USRSTACK; + len = sizeof (int); + if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1) + _usrstack = USRSTACK; /* * Create a red zone below the main stack. All other stacks are * constrained to a maximum size by the paramters passed to @@ -273,14 +279,13 @@ _thread_init(void) * this stack needs an explicitly mapped red zone to protect the * thread stack that is just beyond. */ - if (mmap((void *) USRSTACK - PTHREAD_STACK_INITIAL - + if (mmap(_usrstack - PTHREAD_STACK_INITIAL - PTHREAD_GUARD_DEFAULT, PTHREAD_GUARD_DEFAULT, 0, MAP_ANON, -1, 0) == MAP_FAILED) PANIC("Cannot allocate red zone for initial thread"); /* Set the main thread stack pointer. */ - _thread_initial->stack = (void *) USRSTACK - - PTHREAD_STACK_INITIAL; + _thread_initial->stack = _usrstack - PTHREAD_STACK_INITIAL; /* Set the stack attributes: */ _thread_initial->attr.stackaddr_attr = _thread_initial->stack; diff --git a/lib/libc_r/uthread/uthread_stack.c b/lib/libc_r/uthread/uthread_stack.c index 055af07..86f0a2d 100644 --- a/lib/libc_r/uthread/uthread_stack.c +++ b/lib/libc_r/uthread/uthread_stack.c @@ -112,8 +112,7 @@ static LIST_HEAD(, stack) _mstackq = LIST_HEAD_INITIALIZER(_mstackq); * high memory * */ -static void * last_stack = (void *) USRSTACK - PTHREAD_STACK_INITIAL - - PTHREAD_GUARD_DEFAULT; +static void * last_stack; void * _thread_stack_alloc(size_t stacksize, size_t guardsize) @@ -186,8 +185,11 @@ _thread_stack_alloc(size_t stacksize, size_t guardsize) /* Check if a stack was not allocated from a stack cache: */ if (stack == NULL) { - /* Allocate a new stack. */ + if (last_stack == NULL) + last_stack = _usrstack - PTHREAD_STACK_INITIAL - + PTHREAD_GUARD_DEFAULT; + /* Allocate a new stack. */ stack = last_stack - stack_size; /* diff --git a/lib/libkse/thread/thr_init.c b/lib/libkse/thread/thr_init.c index 7bf09e5..30ec765 100644 --- a/lib/libkse/thread/thr_init.c +++ b/lib/libkse/thread/thr_init.c @@ -266,6 +266,12 @@ _thread_init(void) memcpy((void *) &_thread_initial->attr, &pthread_attr_default, sizeof(struct pthread_attr)); + /* Find the stack top */ + mib[0] = CTL_KERN; + mib[1] = KERN_USRSTACK; + len = sizeof (int); + if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1) + _usrstack = USRSTACK; /* * Create a red zone below the main stack. All other stacks are * constrained to a maximum size by the paramters passed to @@ -273,14 +279,13 @@ _thread_init(void) * this stack needs an explicitly mapped red zone to protect the * thread stack that is just beyond. */ - if (mmap((void *) USRSTACK - PTHREAD_STACK_INITIAL - + if (mmap(_usrstack - PTHREAD_STACK_INITIAL - PTHREAD_GUARD_DEFAULT, PTHREAD_GUARD_DEFAULT, 0, MAP_ANON, -1, 0) == MAP_FAILED) PANIC("Cannot allocate red zone for initial thread"); /* Set the main thread stack pointer. */ - _thread_initial->stack = (void *) USRSTACK - - PTHREAD_STACK_INITIAL; + _thread_initial->stack = _usrstack - PTHREAD_STACK_INITIAL; /* Set the stack attributes: */ _thread_initial->attr.stackaddr_attr = _thread_initial->stack; diff --git a/lib/libkse/thread/thr_private.h b/lib/libkse/thread/thr_private.h index 9af2090..151fd03 100644 --- a/lib/libkse/thread/thr_private.h +++ b/lib/libkse/thread/thr_private.h @@ -881,6 +881,13 @@ struct pthread { * Global variables for the uthread kernel. */ +SCLASS void *_usrstack +#ifdef GLOBAL_PTHREAD_PRIVATE += (void *) USRSTACK; +#else +; +#endif + /* Kernel thread structure used when there are no running threads: */ SCLASS struct pthread _thread_kern_thread; diff --git a/lib/libkse/thread/thr_stack.c b/lib/libkse/thread/thr_stack.c index 055af07..86f0a2d 100644 --- a/lib/libkse/thread/thr_stack.c +++ b/lib/libkse/thread/thr_stack.c @@ -112,8 +112,7 @@ static LIST_HEAD(, stack) _mstackq = LIST_HEAD_INITIALIZER(_mstackq); * high memory * */ -static void * last_stack = (void *) USRSTACK - PTHREAD_STACK_INITIAL - - PTHREAD_GUARD_DEFAULT; +static void * last_stack; void * _thread_stack_alloc(size_t stacksize, size_t guardsize) @@ -186,8 +185,11 @@ _thread_stack_alloc(size_t stacksize, size_t guardsize) /* Check if a stack was not allocated from a stack cache: */ if (stack == NULL) { - /* Allocate a new stack. */ + if (last_stack == NULL) + last_stack = _usrstack - PTHREAD_STACK_INITIAL - + PTHREAD_GUARD_DEFAULT; + /* Allocate a new stack. */ stack = last_stack - stack_size; /* diff --git a/lib/libpthread/thread/thr_init.c b/lib/libpthread/thread/thr_init.c index 7bf09e5..30ec765 100644 --- a/lib/libpthread/thread/thr_init.c +++ b/lib/libpthread/thread/thr_init.c @@ -266,6 +266,12 @@ _thread_init(void) memcpy((void *) &_thread_initial->attr, &pthread_attr_default, sizeof(struct pthread_attr)); + /* Find the stack top */ + mib[0] = CTL_KERN; + mib[1] = KERN_USRSTACK; + len = sizeof (int); + if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1) + _usrstack = USRSTACK; /* * Create a red zone below the main stack. All other stacks are * constrained to a maximum size by the paramters passed to @@ -273,14 +279,13 @@ _thread_init(void) * this stack needs an explicitly mapped red zone to protect the * thread stack that is just beyond. */ - if (mmap((void *) USRSTACK - PTHREAD_STACK_INITIAL - + if (mmap(_usrstack - PTHREAD_STACK_INITIAL - PTHREAD_GUARD_DEFAULT, PTHREAD_GUARD_DEFAULT, 0, MAP_ANON, -1, 0) == MAP_FAILED) PANIC("Cannot allocate red zone for initial thread"); /* Set the main thread stack pointer. */ - _thread_initial->stack = (void *) USRSTACK - - PTHREAD_STACK_INITIAL; + _thread_initial->stack = _usrstack - PTHREAD_STACK_INITIAL; /* Set the stack attributes: */ _thread_initial->attr.stackaddr_attr = _thread_initial->stack; diff --git a/lib/libpthread/thread/thr_private.h b/lib/libpthread/thread/thr_private.h index 9af2090..151fd03 100644 --- a/lib/libpthread/thread/thr_private.h +++ b/lib/libpthread/thread/thr_private.h @@ -881,6 +881,13 @@ struct pthread { * Global variables for the uthread kernel. */ +SCLASS void *_usrstack +#ifdef GLOBAL_PTHREAD_PRIVATE += (void *) USRSTACK; +#else +; +#endif + /* Kernel thread structure used when there are no running threads: */ SCLASS struct pthread _thread_kern_thread; diff --git a/lib/libpthread/thread/thr_stack.c b/lib/libpthread/thread/thr_stack.c index 055af07..86f0a2d 100644 --- a/lib/libpthread/thread/thr_stack.c +++ b/lib/libpthread/thread/thr_stack.c @@ -112,8 +112,7 @@ static LIST_HEAD(, stack) _mstackq = LIST_HEAD_INITIALIZER(_mstackq); * high memory * */ -static void * last_stack = (void *) USRSTACK - PTHREAD_STACK_INITIAL - - PTHREAD_GUARD_DEFAULT; +static void * last_stack; void * _thread_stack_alloc(size_t stacksize, size_t guardsize) @@ -186,8 +185,11 @@ _thread_stack_alloc(size_t stacksize, size_t guardsize) /* Check if a stack was not allocated from a stack cache: */ if (stack == NULL) { - /* Allocate a new stack. */ + if (last_stack == NULL) + last_stack = _usrstack - PTHREAD_STACK_INITIAL - + PTHREAD_GUARD_DEFAULT; + /* Allocate a new stack. */ stack = last_stack - stack_size; /* -- cgit v1.1