123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- typedef union dtv
- {
- size_t counter;
- struct
- {
- void *val;
- bool is_static;
- } pointer;
- } dtv_t;
- typedef struct
- {
- void *tcb;
- dtv_t *dtv;
- void *self;
- int multiple_threads;
- uintptr_t sysinfo;
- uintptr_t stack_guard;
- uintptr_t pointer_guard;
- int gscope_flag;
- int private_futex;
- int __unused1;
-
- void *__private_tm[5];
- } tcbhead_t;
- struct user_desc
- {
- unsigned int entry_number;
- unsigned long int base_addr;
- unsigned int limit;
- unsigned int seg_32bit:1;
- unsigned int contents:2;
- unsigned int read_exec_only:1;
- unsigned int limit_in_pages:1;
- unsigned int seg_not_present:1;
- unsigned int useable:1;
- unsigned int empty:25;
- };
- union user_desc_init
- {
- struct user_desc desc;
- unsigned int vals[4];
- };
- ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
- /* Install new dtv for current thread. */
- ({ struct pthread *__pd; \
- THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
- (((tcbhead_t *) (descr))->dtv)
- #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
- #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
- /* Macros to load from and store into segment registers. */
- # ifndef TLS_GET_GS
- # define TLS_GET_GS() \
- ({ int __seg; __asm__ ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
- # endif
- # ifndef TLS_SET_GS
- # define TLS_SET_GS(val) \
- __asm__ ("movw %w0, %%gs" :: "q" (val))
- # endif
- # ifndef __NR_set_thread_area
- # define __NR_set_thread_area 243
- # endif
- # ifndef TLS_FLAG_WRITABLE
- # define TLS_FLAG_WRITABLE 0x00000001
- # endif
- // XXX Enable for the real world.
- #if 0
- # ifndef __ASSUME_SET_THREAD_AREA
- # error "we need set_thread_area"
- # endif
- #endif
- # ifdef __PIC__
- # define TLS_EBX_ARG "r"
- # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
- # else
- # define TLS_EBX_ARG "b"
- # define TLS_LOAD_EBX
- # endif
- #if defined NEED_DL_SYSINFO
- # define INIT_SYSINFO \
- _head->sysinfo = GLRO(dl_sysinfo)
- ({ void *_thrdescr = (thrdescr); \
- tcbhead_t *_head = _thrdescr; \
- union user_desc_init _segdescr; \
- int _result; \
- \
- _head->tcb = _thrdescr; \
- \
- _head->self = _thrdescr; \
- \
- INIT_SYSINFO; \
- \
- \
- if (secondcall) \
- _segdescr.vals[0] = TLS_GET_GS () >> 3; \
- else \
- _segdescr.vals[0] = -1; \
- \
- _segdescr.vals[1] = (unsigned long int) _thrdescr; \
- \
- _segdescr.vals[2] = 0xfffff; \
-
- \
- _segdescr.vals[3] = 0x51; \
- \
- \
- __asm__ volatile (TLS_LOAD_EBX \
- "int $0x80\n\t" \
- TLS_LOAD_EBX \
- : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
- : "0" (__NR_set_thread_area), \
- TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
- \
- if (_result == 0) \
-
- \
- TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
- \
- _result == 0 ? NULL \
- : "set_thread_area failed when setting up thread-local storage\n"; })
- ({ struct pthread *__pd; \
- THREAD_GETMEM (__pd, header.dtv); })
- ({ struct pthread *__self; \
- __asm__ ("movl %%gs:%c1,%0" : "=r" (__self) \
- : "i" (offsetof (struct pthread, header.self))); \
- __self;})
- REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
- REGISTER_THREAD_AREA (64, 26 * 8, 3)
- ({ __typeof (descr->member) __value; \
- if (sizeof (__value) == 1) \
- __asm__ volatile ("movb %%gs:%P2,%b0" \
- : "=q" (__value) \
- : "0" (0), "i" (offsetof (struct pthread, member))); \
- else if (sizeof (__value) == 4) \
- __asm__ volatile ("movl %%gs:%P1,%0" \
- : "=r" (__value) \
- : "i" (offsetof (struct pthread, member))); \
- else \
- { \
- if (sizeof (__value) != 8) \
-
- \
- abort (); \
- \
- __asm__ volatile ("movl %%gs:%P1,%%eax\n\t" \
- "movl %%gs:%P2,%%edx" \
- : "=A" (__value) \
- : "i" (offsetof (struct pthread, member)), \
- "i" (offsetof (struct pthread, member) + 4)); \
- } \
- __value; })
- ({ __typeof (descr->member[0]) __value; \
- if (sizeof (__value) == 1) \
- __asm__ volatile ("movb %%gs:%P2(%3),%b0" \
- : "=q" (__value) \
- : "0" (0), "i" (offsetof (struct pthread, member[0])), \
- "r" (idx)); \
- else if (sizeof (__value) == 4) \
- __asm__ volatile ("movl %%gs:%P1(,%2,4),%0" \
- : "=r" (__value) \
- : "i" (offsetof (struct pthread, member[0])), \
- "r" (idx)); \
- else \
- { \
- if (sizeof (__value) != 8) \
-
- \
- abort (); \
- \
- __asm__ volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
- "movl %%gs:4+%P1(,%2,8),%%edx" \
- : "=&A" (__value) \
- : "i" (offsetof (struct pthread, member[0])), \
- "r" (idx)); \
- } \
- __value; })
- ({ if (sizeof (descr->member) == 1) \
- __asm__ volatile ("movb %b0,%%gs:%P1" : \
- : "iq" (value), \
- "i" (offsetof (struct pthread, member))); \
- else if (sizeof (descr->member) == 4) \
- __asm__ volatile ("movl %0,%%gs:%P1" : \
- : "ir" (value), \
- "i" (offsetof (struct pthread, member))); \
- else \
- { \
- if (sizeof (descr->member) != 8) \
-
- \
- abort (); \
- \
- __asm__ volatile ("movl %%eax,%%gs:%P1\n\t" \
- "movl %%edx,%%gs:%P2" : \
- : "A" (value), \
- "i" (offsetof (struct pthread, member)), \
- "i" (offsetof (struct pthread, member) + 4)); \
- }})
- ({ if (sizeof (descr->member[0]) == 1) \
- __asm__ volatile ("movb %b0,%%gs:%P1(%2)" : \
- : "iq" (value), \
- "i" (offsetof (struct pthread, member)), \
- "r" (idx)); \
- else if (sizeof (descr->member[0]) == 4) \
- __asm__ volatile ("movl %0,%%gs:%P1(,%2,4)" : \
- : "ir" (value), \
- "i" (offsetof (struct pthread, member)), \
- "r" (idx)); \
- else \
- { \
- if (sizeof (descr->member[0]) != 8) \
-
- \
- abort (); \
- \
- __asm__ volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
- "movl %%edx,%%gs:4+%P1(,%2,8)" : \
- : "A" (value), \
- "i" (offsetof (struct pthread, member)), \
- "r" (idx)); \
- }})
- ({ __typeof (descr->member) __ret; \
- __typeof (oldval) __old = (oldval); \
- if (sizeof (descr->member) == 4) \
- __asm__ volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
- : "=a" (__ret) \
- : "0" (__old), "r" (newval), \
- "i" (offsetof (struct pthread, member))); \
- else \
- \
- abort (); \
- __ret; })
- (void) ({ if (sizeof ((descr)->member) == 4) \
- __asm__ volatile (LOCK_PREFIX "andl %1, %%gs:%P0" \
- :: "i" (offsetof (struct pthread, member)), \
- "ir" (val)); \
- else \
- /* Not necessary for other sizes in the moment. */ \
- abort (); })
- /* Atomic set bit. */
- #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
- (void) ({ if (sizeof ((descr)->member) == 4) \
- __asm__ volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
- :: "i" (offsetof (struct pthread, member)), \
- "ir" (1 << (bit))); \
- else \
- /* Not necessary for other sizes in the moment. */ \
- abort (); })
- /* Call the user-provided thread function. */
- #define CALL_THREAD_FCT(descr) \
- ({ void *__res; \
- int __ignore1, __ignore2; \
- __asm__ volatile ("pushl %%eax\n\t" \
- "pushl %%eax\n\t" \
- "pushl %%eax\n\t" \
- "pushl %%gs:%P4\n\t" \
- "call *%%gs:%P3\n\t" \
- "addl $16, %%esp" \
- : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
- : "i" (offsetof (struct pthread, start_routine)), \
- "i" (offsetof (struct pthread, arg))); \
- __res; })
- /* Set the stack guard field in TCB head. */
- #define THREAD_SET_STACK_GUARD(value) \
- THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
- #define THREAD_COPY_STACK_GUARD(descr) \
- ((descr)->header.stack_guard \
- = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
- /* Set the pointer guard field in the TCB head. */
- #define THREAD_SET_POINTER_GUARD(value) \
- THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
- #define THREAD_COPY_POINTER_GUARD(descr) \
- ((descr)->header.pointer_guard \
- = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
- /* Get and set the global scope generation counter in the TCB head. */
- #define THREAD_GSCOPE_FLAG_UNUSED 0
- #define THREAD_GSCOPE_FLAG_USED 1
- #define THREAD_GSCOPE_FLAG_WAIT 2
- #define THREAD_GSCOPE_RESET_FLAG() \
- do \
- { int __res; \
- __asm__ volatile ("xchgl %0, %%gs:%P1" \
- : "=r" (__res) \
- : "i" (offsetof (struct pthread, header.gscope_flag)), \
- "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
- if (__res == THREAD_GSCOPE_FLAG_WAIT) \
- lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
- } \
- while (0)
- #define THREAD_GSCOPE_SET_FLAG() \
- THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
- #define THREAD_GSCOPE_WAIT() \
- GL(dl_wait_lookup_done) ()
- #endif /* __ASSEMBLER__ */
- #endif /* tls.h */
|