hurd: i386 TLS tweaks

* Micro-optimize TLS access using GCC's native support for gs-based
  addressing when available;
* Just use THREAD_GETMEM and THREAD_SETMEM instead of more inline
  assembly;
* Sync tcbhead_t layout with NPTL, in particular update/fix __private_ss
  offset;
* Statically assert that the two offsets that are a part of ABI are what
  we expect them to be.

Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
Message-Id: <20230214173722.428140-2-bugaevc@gmail.com>
This commit is contained in:
Sergey Bugaev 2023-02-14 20:37:20 +03:00 committed by Samuel Thibault
parent e7223fa1e8
commit 748511f0bb

View file

@ -38,11 +38,12 @@ typedef struct
uintptr_t stack_guard; uintptr_t stack_guard;
uintptr_t pointer_guard; uintptr_t pointer_guard;
int gscope_flag; int gscope_flag;
int private_futex; unsigned int feature_1;
/* Reservation of some values for the TM ABI. */ /* Reservation of some values for the TM ABI. */
void *__private_tm[4]; void *__private_tm[3];
/* GCC split stack support. */ /* GCC split stack support. */
void *__private_ss; void *__private_ss;
void *__glibc_padding1;
/* Keep these fields last, so offsets of fields above can continue being /* Keep these fields last, so offsets of fields above can continue being
compatible with the i386 Linux version. */ compatible with the i386 Linux version. */
@ -53,6 +54,13 @@ typedef struct
struct rtld_catch *rtld_catch; struct rtld_catch *rtld_catch;
} tcbhead_t; } tcbhead_t;
/* GCC generates %gs:0x14 to access the stack guard. */
_Static_assert (offsetof (tcbhead_t, stack_guard) == 0x14,
"stack guard offset");
/* libgcc uses %gs:0x30 to access the split stack pointer. */
_Static_assert (offsetof (tcbhead_t, __private_ss) == 0x30,
"split stack pointer offset");
/* Return tcbhead_t from a TLS segment descriptor. */ /* Return tcbhead_t from a TLS segment descriptor. */
# define HURD_DESC_TLS(desc) \ # define HURD_DESC_TLS(desc) \
({ \ ({ \
@ -166,6 +174,23 @@ out:
# define TLS_INIT_TP(descr) \ # define TLS_INIT_TP(descr) \
_hurd_tls_init ((tcbhead_t *) (descr)) _hurd_tls_init ((tcbhead_t *) (descr))
# if __GNUC_PREREQ (6, 0)
# define THREAD_SELF \
(*(tcbhead_t * __seg_gs *) offsetof (tcbhead_t, tcb))
# define THREAD_GETMEM(descr, member) \
(*(__typeof (descr->member) __seg_gs *) offsetof (tcbhead_t, member))
# define THREAD_GETMEM_NC(descr, member, idx) \
(*(__typeof (descr->member[0]) __seg_gs *) \
(offsetof (tcbhead_t, member) + (idx) * sizeof (descr->member[0])))
# define THREAD_SETMEM(descr, member, value) \
(*(__typeof (descr->member) __seg_gs *) offsetof (tcbhead_t, member) = value)
# define THREAD_SETMEM_NC(descr, member, index, value) \
(*(__typeof (descr->member[0]) __seg_gs *) \
(offsetof (tcbhead_t, member) + (idx) * sizeof (descr->member[0])))
# else
/* Return the TCB address of the current thread. */ /* Return the TCB address of the current thread. */
# define THREAD_SELF \ # define THREAD_SELF \
({ tcbhead_t *__tcb; \ ({ tcbhead_t *__tcb; \
@ -279,6 +304,8 @@ out:
"r" (idx)); \ "r" (idx)); \
}}) }})
# endif /* __GNUC_PREREQ (6, 0) */
/* Return the TCB address of a thread given its state. /* Return the TCB address of a thread given its state.
Note: this is expensive. */ Note: this is expensive. */
# define THREAD_TCB(thread, thread_state) \ # define THREAD_TCB(thread, thread_state) \
@ -295,15 +322,10 @@ out:
HURD_DESC_TLS (___desc);}) HURD_DESC_TLS (___desc);})
/* Install new dtv for current thread. */ /* Install new dtv for current thread. */
# define INSTALL_NEW_DTV(dtvp) \ # define INSTALL_NEW_DTV(dtvp) THREAD_SETMEM (THREAD_SELF, dtv, dtvp)
({ asm volatile ("movl %0,%%gs:%P1" \
: : "ir" (dtvp), "i" (offsetof (tcbhead_t, dtv))); })
/* Return the address of the dtv for the current thread. */ /* Return the address of the dtv for the current thread. */
# define THREAD_DTV() \ # define THREAD_DTV() THREAD_GETMEM (THREAD_SELF, dtv)
({ dtv_t *_dtv; \
asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, dtv)));\
_dtv; })
/* Set the stack guard field in TCB head. */ /* Set the stack guard field in TCB head. */