aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKonstantin Belousov <kib@FreeBSD.org>2022-09-13 22:14:24 +0000
committerKonstantin Belousov <kib@FreeBSD.org>2022-09-16 20:24:12 +0000
commite03c7f50054182549e097f9fbd9387d8c55ef521 (patch)
treea1a96e768aa92f53c598202013b3ba6023dab9e0 /lib
parente2879ece4314eed2d22fe484bd4adfcbb1009685 (diff)
downloadsrc-e03c7f50054182549e097f9fbd9387d8c55ef521.tar.gz
src-e03c7f50054182549e097f9fbd9387d8c55ef521.zip
libthr: extract code to get main stack base and size into helpers
Reviewed by: brooks, imp (previous version) Discussed with: markj Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D36540
Diffstat (limited to 'lib')
-rw-r--r--lib/libthr/thread/thr_init.c56
-rw-r--r--lib/libthr/thread/thr_private.h2
-rw-r--r--lib/libthr/thread/thr_stack.c24
3 files changed, 46 insertions, 36 deletions
diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c
index b9d3265a05dc..1c4867cb8185 100644
--- a/lib/libthr/thread/thr_init.c
+++ b/lib/libthr/thread/thr_init.c
@@ -434,12 +434,43 @@ init_main_thread(struct pthread *thread)
/* Others cleared to zero by thr_alloc() */
}
-static void
-init_private(void)
+bool
+__thr_get_main_stack_base(char **base)
{
- struct rlimit rlim;
size_t len;
int mib[2];
+
+ if (elf_aux_info(AT_USRSTACKBASE, base, sizeof(*base)) == 0)
+ return (true);
+
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_USRSTACK;
+ len = sizeof(*base);
+ if (sysctl(mib, nitems(mib), base, &len, NULL, 0) == 0)
+ return (true);
+
+ return (false);
+}
+
+bool
+__thr_get_main_stack_lim(size_t *lim)
+{
+ struct rlimit rlim;
+
+ if (elf_aux_info(AT_USRSTACKLIM, lim, sizeof(*lim)) == 0)
+ return (true);
+
+ if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
+ *lim = rlim.rlim_cur;
+ return (true);
+ }
+
+ return (false);
+}
+
+static void
+init_private(void)
+{
char *env, *env_bigstack, *env_splitstack;
_thr_umutex_init(&_mutex_static_lock);
@@ -465,24 +496,13 @@ init_private(void)
__thr_malloc_init();
/* Find the stack top */
- if (elf_aux_info(AT_USRSTACKBASE, &_usrstack,
- sizeof(_usrstack)) != 0) {
- mib[0] = CTL_KERN;
- mib[1] = KERN_USRSTACK;
- len = sizeof (_usrstack);
- if (sysctl(mib, nitems(mib), &_usrstack, &len,
- NULL, 0) == -1)
- PANIC("Cannot get kern.usrstack from sysctl");
- }
+ if (!__thr_get_main_stack_base(&_usrstack))
+ PANIC("Cannot get kern.usrstack");
env_bigstack = getenv("LIBPTHREAD_BIGSTACK_MAIN");
env_splitstack = getenv("LIBPTHREAD_SPLITSTACK_MAIN");
if (env_bigstack != NULL || env_splitstack == NULL) {
- if (elf_aux_info(AT_USRSTACKLIM, &_thr_stack_initial,
- sizeof(_thr_stack_initial)) != 0) {
- if (getrlimit(RLIMIT_STACK, &rlim) == -1)
- PANIC("Cannot get stack rlimit");
- _thr_stack_initial = rlim.rlim_cur;
- }
+ if (!__thr_get_main_stack_lim(&_thr_stack_initial))
+ PANIC("Cannot get stack rlimit");
}
_thr_is_smp = sysconf(_SC_NPROCESSORS_CONF);
if (_thr_is_smp == -1)
diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h
index a5bbc5997d30..0e88c6711817 100644
--- a/lib/libthr/thread/thr_private.h
+++ b/lib/libthr/thread/thr_private.h
@@ -1102,6 +1102,8 @@ int _thr_mutex_destroy(pthread_mutex_t *);
int _thr_mutex_unlock(pthread_mutex_t *);
int __Tthr_mutex_lock(pthread_mutex_t *);
int __Tthr_mutex_trylock(pthread_mutex_t *);
+bool __thr_get_main_stack_base(char **base);
+bool __thr_get_main_stack_lim(size_t *lim);
__END_DECLS
__NULLABILITY_PRAGMA_POP
diff --git a/lib/libthr/thread/thr_stack.c b/lib/libthr/thread/thr_stack.c
index 34536ecfad2e..3befb740639e 100644
--- a/lib/libthr/thread/thr_stack.c
+++ b/lib/libthr/thread/thr_stack.c
@@ -148,25 +148,13 @@ _thr_stack_fix_protection(struct pthread *thrd)
static void
singlethread_map_stacks_exec(void)
{
- int mib[2];
- struct rlimit rlim;
- u_long usrstack, stacksz;
- size_t len;
+ char *usrstack;
+ size_t stacksz;
- if (elf_aux_info(AT_USRSTACKBASE, &usrstack, sizeof(usrstack)) != 0) {
- mib[0] = CTL_KERN;
- mib[1] = KERN_USRSTACK;
- len = sizeof(usrstack);
- if (sysctl(mib, nitems(mib), &usrstack, &len, NULL, 0) == -1)
- return;
- }
- if (elf_aux_info(AT_USRSTACKLIM, &stacksz, sizeof(stacksz)) != 0) {
- if (getrlimit(RLIMIT_STACK, &rlim) == -1)
- return;
- stacksz = rlim.rlim_cur;
- }
- mprotect((void *)(uintptr_t)(usrstack - stacksz), stacksz,
- _rtld_get_stack_prot());
+ if (!__thr_get_main_stack_base(&usrstack) ||
+ !__thr_get_main_stack_lim(&stacksz))
+ return;
+ mprotect(usrstack - stacksz, stacksz, _rtld_get_stack_prot());
}
void