| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#ifndef _I386_TLS_H |
| 20 |
#define _I386_TLS_H |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include <sysdeps/mach/hurd/tls.h> |
| 25 |
|
| 26 |
|
| 27 |
#ifndef __ASSEMBLER__ |
| 28 |
# include <dl-dtv.h> |
| 29 |
|
| 30 |
|
| 31 |
typedef struct |
| 32 |
{ |
| 33 |
void *tcb; |
| 34 |
dtv_t *dtv; |
| 35 |
thread_t self_do_not_use; |
| 36 |
int multiple_threads; |
| 37 |
uintptr_t sysinfo; |
| 38 |
uintptr_t stack_guard; |
| 39 |
uintptr_t pointer_guard; |
| 40 |
int gscope_flag; |
| 41 |
unsigned int feature_1; |
| 42 |
|
| 43 |
void *__private_tm[3]; |
| 44 |
|
| 45 |
void *__private_ss; |
| 46 |
void *__glibc_padding1; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
mach_port_t reply_port; |
| 51 |
struct hurd_sigstate *_hurd_sigstate; |
| 52 |
|
| 53 |
|
| 54 |
struct rtld_catch *rtld_catch; |
| 55 |
} tcbhead_t; |
| 56 |
|
| 57 |
|
| 58 |
_Static_assert (offsetof (tcbhead_t, stack_guard) == 0x14, |
| 59 |
"stack guard offset"); |
| 60 |
|
| 61 |
_Static_assert (offsetof (tcbhead_t, __private_ss) == 0x30, |
| 62 |
"split stack pointer offset"); |
| 63 |
|
| 64 |
|
| 65 |
# define HURD_DESC_TLS(desc) \ |
| 66 |
({ \ |
| 67 |
(tcbhead_t *) ( (desc->low_word >> 16) \ |
| 68 |
| ((desc->high_word & 0xff) << 16) \ |
| 69 |
| (desc->high_word & 0xff000000)); \ |
| 70 |
}) |
| 71 |
|
| 72 |
#endif |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
#define TLS_TCB_AT_TP 1 |
| 77 |
#define TLS_DTV_AT_TP 0 |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
#define TCB_ALIGNMENT 64 |
| 87 |
|
| 88 |
#ifndef __ASSEMBLER__ |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
# include <mach/i386/mach_i386.h> |
| 93 |
|
| 94 |
# ifndef HAVE_I386_SET_GDT |
| 95 |
# define __i386_set_gdt(thr, sel, desc) ((void) (thr), (void) (sel), (void) (desc), MIG_BAD_ID) |
| 96 |
# endif |
| 97 |
|
| 98 |
# include <errno.h> |
| 99 |
# include <assert.h> |
| 100 |
|
| 101 |
# define HURD_TLS_DESC_DECL(desc, tcb) \ |
| 102 |
struct descriptor desc = \ |
| 103 |
{ \ |
| 104 |
0xffff \ |
| 105 |
| (((unsigned int) (tcb)) << 16) \ |
| 106 |
, \ |
| 107 |
((((unsigned int) (tcb)) >> 16) & 0xff) \ |
| 108 |
| ((0x12 | 0x60 | 0x80) << 8) \ |
| 109 |
| (0xf << 16) \ |
| 110 |
| ((4 | 8) << 20) \ |
| 111 |
| (((unsigned int) (tcb)) & 0xff000000) \ |
| 112 |
} |
| 113 |
|
| 114 |
# define HURD_SEL_LDT(sel) (__builtin_expect ((sel) & 4, 0)) |
| 115 |
|
| 116 |
#ifndef SHARED |
| 117 |
extern unsigned short __init1_desc; |
| 118 |
# define __HURD_DESC_INITIAL(gs, ds) ((gs) == (ds) || (gs) == __init1_desc) |
| 119 |
#else |
| 120 |
# define __HURD_DESC_INITIAL(gs, ds) ((gs) == (ds)) |
| 121 |
#endif |
| 122 |
|
| 123 |
#if !defined (SHARED) || IS_IN (rtld) |
| 124 |
|
| 125 |
extern inline bool __attribute__ ((unused)) |
| 126 |
__LIBC_NO_TLS (void) |
| 127 |
{ |
| 128 |
unsigned short ds, gs; |
| 129 |
asm ("movw %%ds, %w0\n" |
| 130 |
"movw %%gs, %w1" |
| 131 |
: "=q" (ds), "=q" (gs)); |
| 132 |
return __glibc_unlikely (__HURD_DESC_INITIAL (gs, ds)); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
static inline bool __attribute__ ((unused)) |
| 139 |
_hurd_tls_init (tcbhead_t *tcb, bool full) |
| 140 |
{ |
| 141 |
HURD_TLS_DESC_DECL (desc, tcb); |
| 142 |
thread_t self = __mach_thread_self (); |
| 143 |
bool success = true; |
| 144 |
extern mach_port_t __hurd_reply_port0; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
tcb->tcb = tcb; |
| 149 |
|
| 150 |
tcb->multiple_threads = 1; |
| 151 |
if (full) |
| 152 |
|
| 153 |
tcb->reply_port = __hurd_reply_port0; |
| 154 |
|
| 155 |
|
| 156 |
int sel = -1; |
| 157 |
error_t err = __i386_set_gdt (self, &sel, desc); |
| 158 |
if (err == MIG_BAD_ID) |
| 159 |
{ |
| 160 |
|
| 161 |
sel = 0x27; |
| 162 |
err = __i386_set_ldt (self, sel, &desc, 1); |
| 163 |
assert_perror (err); |
| 164 |
if (err) |
| 165 |
{ |
| 166 |
success = false; |
| 167 |
goto out; |
| 168 |
} |
| 169 |
} |
| 170 |
else if (err) |
| 171 |
{ |
| 172 |
assert_perror (err); |
| 173 |
success = false; |
| 174 |
goto out; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
asm volatile ("mov %w0, %%gs" :: "q" (sel)); |
| 179 |
if (full) |
| 180 |
|
| 181 |
__hurd_reply_port0 = MACH_PORT_NULL; |
| 182 |
#ifndef SHARED |
| 183 |
else |
| 184 |
__init1_desc = sel; |
| 185 |
#endif |
| 186 |
|
| 187 |
out: |
| 188 |
__mach_port_deallocate (__mach_task_self (), self); |
| 189 |
return success; |
| 190 |
} |
| 191 |
|
| 192 |
# define TLS_INIT_TP(descr) _hurd_tls_init ((tcbhead_t *) (descr), 1) |
| 193 |
#else |
| 194 |
# define __LIBC_NO_TLS() 0 |
| 195 |
#endif |
| 196 |
|
| 197 |
|
| 198 |
# define THREAD_SELF \ |
| 199 |
(*(tcbhead_t * __seg_gs *) offsetof (tcbhead_t, tcb)) |
| 200 |
# define THREAD_GETMEM(descr, member) \ |
| 201 |
(*(__typeof (descr->member) __seg_gs *) offsetof (tcbhead_t, member)) |
| 202 |
# define THREAD_GETMEM_NC(descr, member, idx) \ |
| 203 |
(*(__typeof (descr->member[0]) __seg_gs *) \ |
| 204 |
(offsetof (tcbhead_t, member) + (idx) * sizeof (descr->member[0]))) |
| 205 |
# define THREAD_SETMEM(descr, member, value) \ |
| 206 |
(*(__typeof (descr->member) __seg_gs *) offsetof (tcbhead_t, member) = value) |
| 207 |
# define THREAD_SETMEM_NC(descr, member, index, value) \ |
| 208 |
(*(__typeof (descr->member[0]) __seg_gs *) \ |
| 209 |
(offsetof (tcbhead_t, member) + (idx) * sizeof (descr->member[0]))) |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
# define THREAD_TCB(thread, thread_state) \ |
| 215 |
({ int __sel = (thread_state)->basic.gs; \ |
| 216 |
struct descriptor __desc, *___desc = &__desc; \ |
| 217 |
unsigned int __count = 1; \ |
| 218 |
kern_return_t __err; \ |
| 219 |
if (HURD_SEL_LDT (__sel)) \ |
| 220 |
__err = __i386_get_ldt ((thread), __sel, 1, &___desc, &__count); \ |
| 221 |
else \ |
| 222 |
__err = __i386_get_gdt ((thread), __sel, &__desc); \ |
| 223 |
assert_perror (__err); \ |
| 224 |
assert (__count == 1); \ |
| 225 |
HURD_DESC_TLS (___desc);}) |
| 226 |
|
| 227 |
|
| 228 |
# define INSTALL_NEW_DTV(dtvp) THREAD_SETMEM (THREAD_SELF, dtv, dtvp) |
| 229 |
|
| 230 |
|
| 231 |
# define THREAD_DTV() THREAD_GETMEM (THREAD_SELF, dtv) |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
#define THREAD_SET_STACK_GUARD(value) \ |
| 236 |
THREAD_SETMEM (THREAD_SELF, stack_guard, value) |
| 237 |
#define THREAD_COPY_STACK_GUARD(descr) \ |
| 238 |
((descr)->stack_guard \ |
| 239 |
= THREAD_GETMEM (THREAD_SELF, stack_guard)) |
| 240 |
|
| 241 |
|
| 242 |
#define THREAD_SET_POINTER_GUARD(value) \ |
| 243 |
THREAD_SETMEM (THREAD_SELF, pointer_guard, value) |
| 244 |
#define THREAD_COPY_POINTER_GUARD(descr) \ |
| 245 |
((descr)->pointer_guard \ |
| 246 |
= THREAD_GETMEM (THREAD_SELF, pointer_guard)) |
| 247 |
|
| 248 |
|
| 249 |
# include <mach/machine/thread_status.h> |
| 250 |
|
| 251 |
|
| 252 |
static inline kern_return_t __attribute__ ((unused)) |
| 253 |
_hurd_tls_fork (thread_t child, thread_t orig, struct i386_thread_state *state) |
| 254 |
{ |
| 255 |
|
| 256 |
int sel; |
| 257 |
asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0)); |
| 258 |
if (sel == state->ds) |
| 259 |
return 0; |
| 260 |
|
| 261 |
struct descriptor desc, *_desc = &desc; |
| 262 |
error_t err; |
| 263 |
unsigned int count = 1; |
| 264 |
|
| 265 |
if (HURD_SEL_LDT (sel)) |
| 266 |
err = __i386_get_ldt (orig, sel, 1, &_desc, &count); |
| 267 |
else |
| 268 |
err = __i386_get_gdt (orig, sel, &desc); |
| 269 |
|
| 270 |
assert_perror (err); |
| 271 |
if (err) |
| 272 |
return err; |
| 273 |
|
| 274 |
if (HURD_SEL_LDT (sel)) |
| 275 |
err = __i386_set_ldt (child, sel, &desc, 1); |
| 276 |
else |
| 277 |
err = __i386_set_gdt (child, &sel, desc); |
| 278 |
|
| 279 |
state->gs = sel; |
| 280 |
return err; |
| 281 |
} |
| 282 |
|
| 283 |
static inline kern_return_t __attribute__ ((unused)) |
| 284 |
_hurd_tls_new (thread_t child, tcbhead_t *tcb) |
| 285 |
{ |
| 286 |
error_t err; |
| 287 |
|
| 288 |
struct i386_thread_state state; |
| 289 |
mach_msg_type_number_t state_count = i386_THREAD_STATE_COUNT; |
| 290 |
err = __thread_get_state (child, i386_REGS_SEGS_STATE, |
| 291 |
(thread_state_t) &state, |
| 292 |
&state_count); |
| 293 |
if (err) |
| 294 |
return err; |
| 295 |
assert (state_count == i386_THREAD_STATE_COUNT); |
| 296 |
|
| 297 |
int sel; |
| 298 |
asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0)); |
| 299 |
if (sel == state.ds) |
| 300 |
return 0; |
| 301 |
|
| 302 |
HURD_TLS_DESC_DECL (desc, tcb); |
| 303 |
|
| 304 |
tcb->tcb = tcb; |
| 305 |
|
| 306 |
if (HURD_SEL_LDT (sel)) |
| 307 |
err = __i386_set_ldt (child, sel, &desc, 1); |
| 308 |
else |
| 309 |
err = __i386_set_gdt (child, &sel, desc); |
| 310 |
|
| 311 |
if (err) |
| 312 |
return err; |
| 313 |
|
| 314 |
|
| 315 |
state.gs = sel; |
| 316 |
return __thread_set_state (child, i386_REGS_SEGS_STATE, |
| 317 |
(thread_state_t) &state, |
| 318 |
state_count); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
# define THREAD_GSCOPE_FLAG_UNUSED 0 |
| 323 |
# define THREAD_GSCOPE_FLAG_USED 1 |
| 324 |
# define THREAD_GSCOPE_FLAG_WAIT 2 |
| 325 |
|
| 326 |
# define THREAD_GSCOPE_SET_FLAG() \ |
| 327 |
THREAD_SETMEM (THREAD_SELF, gscope_flag, THREAD_GSCOPE_FLAG_USED) |
| 328 |
|
| 329 |
# define THREAD_GSCOPE_RESET_FLAG() \ |
| 330 |
({ \ |
| 331 |
int __flag; \ |
| 332 |
asm volatile ("xchgl %0, %%gs:%P1" \ |
| 333 |
: "=r" (__flag) \ |
| 334 |
: "i" (offsetof (tcbhead_t, gscope_flag)), \ |
| 335 |
"0" (THREAD_GSCOPE_FLAG_UNUSED)); \ |
| 336 |
if (__flag == THREAD_GSCOPE_FLAG_WAIT) \ |
| 337 |
lll_wake (THREAD_SELF->gscope_flag, LLL_PRIVATE); \ |
| 338 |
}) |
| 339 |
|
| 340 |
#endif |
| 341 |
|
| 342 |
#endif |