summaryrefslogtreecommitdiffhomepage
path: root/arch/nt64/pthread_arch.h
blob: c99da3ad4f9dc65fbb25166ed32750b2d60d41db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stddef.h>

#define	TP_ADJ(p)	(p)
#define	CANCEL_REG_IP	0x1F
#define MC_PC           uc_rip

extern uintptr_t __teb_sys_idx;
extern uintptr_t __teb_libc_idx;

struct __os_tib {
	void *	exception_list;
	void *	stack_base;
	void *	stack_limit;
};

static __inline__ void * __os_get_teb_address(void)
{
	void * ptrRet;
	__asm__ __volatile__ (
		"mov %%gs:0x30, %0\n\t"
		: "=r" (ptrRet) : :
		);
	return ptrRet;
}


static inline void __pthread_convert(void)
{
	/* (third-party thread support) */
	__asm__ __volatile__ (
		"push %rax\n\t"
		"movq __psx_vtbl,%rax\n\t"
		"sub $0x28,%rsp\n\t"
		"call *(%rax)\n\t"
		"add $0x28,%rsp\n\t"
		"pop  %rax\n\t"
		);
}


static inline struct pthread ** __psx_tlca(void)
{
	struct pthread **	ptlca;
	struct __os_tib *	tib;
	void **			slots;
	void ***		xslots;
	uintptr_t		sys_idx;

	tib = __os_get_teb_address();
	sys_idx = __teb_sys_idx;

	if (sys_idx < 64) {
		slots = (void **)((uintptr_t)tib + 0x1480);
		ptlca = (struct pthread **)(slots[sys_idx]);
	} else {
		xslots = (void ***)((uintptr_t)tib + 0x1780);
		slots  = *xslots;
		ptlca = (struct pthread **)(slots[sys_idx - 64]);
	}

	return ptlca;
}


#ifdef __MUSL_PRE___GET_TP
static inline struct pthread * __pthread_self(void)
#else
static inline struct pthread * __get_tp(void)
#endif
{
	struct pthread ** ptlca;

	ptlca = __psx_tlca();
	if (ptlca) return *ptlca;

	/* (third-party thread) */
	__pthread_convert();
	ptlca = __psx_tlca();
	return *ptlca;
}