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
This commit is contained in:
Konstantin Belousov 2022-09-14 01:14:24 +03:00
parent e2879ece43
commit e03c7f5005
3 changed files with 46 additions and 36 deletions

View File

@ -434,12 +434,43 @@ init_main_thread(struct pthread *thread)
/* Others cleared to zero by thr_alloc() */
}
bool
__thr_get_main_stack_base(char **base)
{
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)
{
struct rlimit rlim;
size_t len;
int mib[2];
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)

View File

@ -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

View File

@ -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