From 72a79166ea4881001b4751d99e66af14b101ba09 Mon Sep 17 00:00:00 2001 From: Peter Wemm Date: Thu, 14 Apr 2005 00:13:20 +0000 Subject: [PATCH] Use the i386_set_gsbase() syscall if it is implemented in the kernel. This is a little hairy here because the allocation and usage of this functionality is split into two places in libpthread. --- lib/libkse/arch/i386/i386/pthread_md.c | 47 +++++++++++++------ lib/libkse/arch/i386/include/pthread_md.h | 10 +++- lib/libpthread/arch/i386/i386/pthread_md.c | 47 +++++++++++++------ lib/libpthread/arch/i386/include/pthread_md.h | 10 +++- 4 files changed, 82 insertions(+), 32 deletions(-) diff --git a/lib/libkse/arch/i386/i386/pthread_md.c b/lib/libkse/arch/i386/i386/pthread_md.c index cca433995ebb..96cdac047f4a 100644 --- a/lib/libkse/arch/i386/i386/pthread_md.c +++ b/lib/libkse/arch/i386/i386/pthread_md.c @@ -42,6 +42,8 @@ __FBSDID("$FreeBSD$"); #include "rtld_tls.h" #include "pthread_md.h" +int _thr_using_setbase; + struct tcb * _tcb_ctor(struct pthread *thread, int initial) { @@ -78,8 +80,10 @@ _kcb_ctor(struct kse *kse) { #ifndef COMPAT_32BIT union descriptor ldt; + void *base; #endif struct kcb *kcb; + int error; kcb = malloc(sizeof(struct kcb)); if (kcb != NULL) { @@ -87,20 +91,35 @@ _kcb_ctor(struct kse *kse) kcb->kcb_self = kcb; kcb->kcb_kse = kse; #ifndef COMPAT_32BIT - ldt.sd.sd_hibase = (unsigned int)kcb >> 24; - ldt.sd.sd_lobase = (unsigned int)kcb & 0xFFFFFF; - ldt.sd.sd_hilimit = (sizeof(struct kcb) >> 16) & 0xF; - ldt.sd.sd_lolimit = sizeof(struct kcb) & 0xFFFF; - ldt.sd.sd_type = SDT_MEMRWA; - ldt.sd.sd_dpl = SEL_UPL; - ldt.sd.sd_p = 1; - ldt.sd.sd_xx = 0; - ldt.sd.sd_def32 = 1; - ldt.sd.sd_gran = 0; /* no more than 1M */ - kcb->kcb_ldt = i386_set_ldt(LDT_AUTO_ALLOC, &ldt, 1); - if (kcb->kcb_ldt < 0) { - free(kcb); - return (NULL); + switch (_thr_using_setbase) { + case 1: /* use i386_set_gsbase() in _kcb_set */ + kcb->kcb_ldt = -1; + break; + case 0: /* Untested, try the get/set_gsbase routines once */ + error = i386_get_gsbase(&base); + if (error == 0) { + _thr_using_setbase = 1; + break; + } + /* fall through */ + case 2: /* Use the user_ldt code, we must have an old kernel */ + _thr_using_setbase = 2; + ldt.sd.sd_hibase = (unsigned int)kcb >> 24; + ldt.sd.sd_lobase = (unsigned int)kcb & 0xFFFFFF; + ldt.sd.sd_hilimit = (sizeof(struct kcb) >> 16) & 0xF; + ldt.sd.sd_lolimit = sizeof(struct kcb) & 0xFFFF; + ldt.sd.sd_type = SDT_MEMRWA; + ldt.sd.sd_dpl = SEL_UPL; + ldt.sd.sd_p = 1; + ldt.sd.sd_xx = 0; + ldt.sd.sd_def32 = 1; + ldt.sd.sd_gran = 0; /* no more than 1M */ + kcb->kcb_ldt = i386_set_ldt(LDT_AUTO_ALLOC, &ldt, 1); + if (kcb->kcb_ldt < 0) { + free(kcb); + return (NULL); + } + break; } #endif } diff --git a/lib/libkse/arch/i386/include/pthread_md.h b/lib/libkse/arch/i386/include/pthread_md.h index 92d4275d44b2..e339391a626d 100644 --- a/lib/libkse/arch/i386/include/pthread_md.h +++ b/lib/libkse/arch/i386/include/pthread_md.h @@ -40,6 +40,8 @@ extern int _thr_setcontext(mcontext_t *, intptr_t, intptr_t *); extern int _thr_getcontext(mcontext_t *); +extern int _thr_using_setbase; + #define KSE_STACKSIZE 16384 #define DTV_OFFSET offsetof(struct tcb, tcb_dtv) @@ -155,8 +157,12 @@ _kcb_set(struct kcb *kcb) #ifndef COMPAT_32BIT int val; - val = (kcb->kcb_ldt << 3) | 7; - __asm __volatile("movl %0, %%gs" : : "r" (val)); + if (_thr_using_setbase == 1) { + i386_set_gsbase(kcb); + } else { + val = (kcb->kcb_ldt << 3) | 7; + __asm __volatile("movl %0, %%gs" : : "r" (val)); + } #else _amd64_set_gsbase(kcb); #endif diff --git a/lib/libpthread/arch/i386/i386/pthread_md.c b/lib/libpthread/arch/i386/i386/pthread_md.c index cca433995ebb..96cdac047f4a 100644 --- a/lib/libpthread/arch/i386/i386/pthread_md.c +++ b/lib/libpthread/arch/i386/i386/pthread_md.c @@ -42,6 +42,8 @@ __FBSDID("$FreeBSD$"); #include "rtld_tls.h" #include "pthread_md.h" +int _thr_using_setbase; + struct tcb * _tcb_ctor(struct pthread *thread, int initial) { @@ -78,8 +80,10 @@ _kcb_ctor(struct kse *kse) { #ifndef COMPAT_32BIT union descriptor ldt; + void *base; #endif struct kcb *kcb; + int error; kcb = malloc(sizeof(struct kcb)); if (kcb != NULL) { @@ -87,20 +91,35 @@ _kcb_ctor(struct kse *kse) kcb->kcb_self = kcb; kcb->kcb_kse = kse; #ifndef COMPAT_32BIT - ldt.sd.sd_hibase = (unsigned int)kcb >> 24; - ldt.sd.sd_lobase = (unsigned int)kcb & 0xFFFFFF; - ldt.sd.sd_hilimit = (sizeof(struct kcb) >> 16) & 0xF; - ldt.sd.sd_lolimit = sizeof(struct kcb) & 0xFFFF; - ldt.sd.sd_type = SDT_MEMRWA; - ldt.sd.sd_dpl = SEL_UPL; - ldt.sd.sd_p = 1; - ldt.sd.sd_xx = 0; - ldt.sd.sd_def32 = 1; - ldt.sd.sd_gran = 0; /* no more than 1M */ - kcb->kcb_ldt = i386_set_ldt(LDT_AUTO_ALLOC, &ldt, 1); - if (kcb->kcb_ldt < 0) { - free(kcb); - return (NULL); + switch (_thr_using_setbase) { + case 1: /* use i386_set_gsbase() in _kcb_set */ + kcb->kcb_ldt = -1; + break; + case 0: /* Untested, try the get/set_gsbase routines once */ + error = i386_get_gsbase(&base); + if (error == 0) { + _thr_using_setbase = 1; + break; + } + /* fall through */ + case 2: /* Use the user_ldt code, we must have an old kernel */ + _thr_using_setbase = 2; + ldt.sd.sd_hibase = (unsigned int)kcb >> 24; + ldt.sd.sd_lobase = (unsigned int)kcb & 0xFFFFFF; + ldt.sd.sd_hilimit = (sizeof(struct kcb) >> 16) & 0xF; + ldt.sd.sd_lolimit = sizeof(struct kcb) & 0xFFFF; + ldt.sd.sd_type = SDT_MEMRWA; + ldt.sd.sd_dpl = SEL_UPL; + ldt.sd.sd_p = 1; + ldt.sd.sd_xx = 0; + ldt.sd.sd_def32 = 1; + ldt.sd.sd_gran = 0; /* no more than 1M */ + kcb->kcb_ldt = i386_set_ldt(LDT_AUTO_ALLOC, &ldt, 1); + if (kcb->kcb_ldt < 0) { + free(kcb); + return (NULL); + } + break; } #endif } diff --git a/lib/libpthread/arch/i386/include/pthread_md.h b/lib/libpthread/arch/i386/include/pthread_md.h index 92d4275d44b2..e339391a626d 100644 --- a/lib/libpthread/arch/i386/include/pthread_md.h +++ b/lib/libpthread/arch/i386/include/pthread_md.h @@ -40,6 +40,8 @@ extern int _thr_setcontext(mcontext_t *, intptr_t, intptr_t *); extern int _thr_getcontext(mcontext_t *); +extern int _thr_using_setbase; + #define KSE_STACKSIZE 16384 #define DTV_OFFSET offsetof(struct tcb, tcb_dtv) @@ -155,8 +157,12 @@ _kcb_set(struct kcb *kcb) #ifndef COMPAT_32BIT int val; - val = (kcb->kcb_ldt << 3) | 7; - __asm __volatile("movl %0, %%gs" : : "r" (val)); + if (_thr_using_setbase == 1) { + i386_set_gsbase(kcb); + } else { + val = (kcb->kcb_ldt << 3) | 7; + __asm __volatile("movl %0, %%gs" : : "r" (val)); + } #else _amd64_set_gsbase(kcb); #endif