2022-09-10 09:56:25 +00:00
|
|
|
#ifndef COSMOPOLITAN_LIBC_THREAD_TLS2_H_
|
|
|
|
#define COSMOPOLITAN_LIBC_THREAD_TLS2_H_
|
2022-06-12 18:47:20 +00:00
|
|
|
#include "libc/dce.h"
|
2022-09-10 09:56:25 +00:00
|
|
|
#include "libc/thread/tls.h"
|
2022-06-12 18:47:20 +00:00
|
|
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
|
|
|
COSMOPOLITAN_C_START_
|
|
|
|
#if defined(__GNUC__) && defined(__x86_64__) && !defined(__STRICT_ANSI__)
|
2022-10-13 20:44:41 +00:00
|
|
|
|
2022-07-18 10:33:32 +00:00
|
|
|
/**
|
|
|
|
* Returns location of thread information block.
|
|
|
|
*
|
|
|
|
* This should be favored over __get_tls() for .privileged code that
|
|
|
|
* can't be self-modified by __enable_tls().
|
|
|
|
*/
|
2023-07-26 20:54:49 +00:00
|
|
|
privileged static inline struct CosmoTib *__get_tls_privileged(void) {
|
2022-06-12 18:47:20 +00:00
|
|
|
char *tib, *lin = (char *)0x30;
|
2022-08-13 23:14:02 +00:00
|
|
|
if (IsLinux() || IsFreebsd() || IsNetbsd() || IsOpenbsd() || IsMetal()) {
|
2022-06-12 18:47:20 +00:00
|
|
|
asm("mov\t%%fs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory");
|
|
|
|
} else {
|
|
|
|
asm("mov\t%%gs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory");
|
|
|
|
if (IsWindows()) {
|
|
|
|
tib = *(char **)(tib + 0x1480 + __tls_index * 8);
|
|
|
|
}
|
|
|
|
}
|
2022-09-10 09:56:25 +00:00
|
|
|
return (struct CosmoTib *)tib;
|
2022-06-12 18:47:20 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 20:54:49 +00:00
|
|
|
static dontasan inline struct CosmoTib *__get_tls_win32(void) {
|
2022-10-13 20:44:41 +00:00
|
|
|
char *tib, *lin = (char *)0x30;
|
|
|
|
asm("mov\t%%gs:(%1),%0" : "=a"(tib) : "r"(lin) : "memory");
|
|
|
|
tib = *(char **)(tib + 0x1480 + __tls_index * 8);
|
|
|
|
return (struct CosmoTib *)tib;
|
|
|
|
}
|
|
|
|
|
2023-07-26 20:54:49 +00:00
|
|
|
static dontasan inline void __set_tls_win32(void *tls) {
|
2022-10-13 20:44:41 +00:00
|
|
|
asm("mov\t%1,%%gs:%0" : "=m"(*((long *)0x1480 + __tls_index)) : "r"(tls));
|
|
|
|
}
|
|
|
|
|
2023-05-02 02:43:59 +00:00
|
|
|
#elif defined(__aarch64__)
|
2023-05-10 05:41:57 +00:00
|
|
|
#define __get_tls_privileged() __get_tls()
|
2023-05-02 02:43:59 +00:00
|
|
|
#define __get_tls_win32() ((struct CosmoTib *)0)
|
|
|
|
#define __set_tls_win32(tls) (void)0
|
2022-10-13 20:44:41 +00:00
|
|
|
#endif /* GNU x86-64 */
|
2022-06-12 18:47:20 +00:00
|
|
|
COSMOPOLITAN_C_END_
|
|
|
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
2022-09-10 09:56:25 +00:00
|
|
|
#endif /* COSMOPOLITAN_LIBC_THREAD_TLS2_H_ */
|