Commit | Line | Data |
---|---|---|
5af57e62 PMF |
1 | #ifndef UST_PROCESSOR_H |
2 | #define UST_PROCESSOR_H | |
d98a01c6 PMF |
3 | |
4 | #include <stddef.h> | |
636ca5d6 PMF |
5 | #include <string.h> |
6 | ||
7 | extern __thread long ust_reg_stack[500]; | |
8 | extern volatile __thread long *ust_reg_stack_ptr; | |
9 | ||
d98a01c6 | 10 | |
a2dc02e0 PMF |
11 | #ifdef x86_32 |
12 | #error "32-bit support broken" | |
d98a01c6 PMF |
13 | |
14 | struct registers { | |
15 | long eax; | |
16 | long ebx; | |
17 | long ecx; | |
18 | long edx; | |
19 | long ebp; | |
20 | long esp; | |
21 | long esi; | |
22 | long edi; | |
23 | int xds; | |
24 | int xes; | |
25 | int xfs; | |
26 | int xgs; | |
27 | long eip; | |
28 | int xcs; | |
29 | long eflags; | |
30 | int xss; | |
31 | }; | |
32 | ||
33 | static inline save_registers(struct registers *regs) | |
34 | { | |
35 | } | |
36 | ||
9e8f4f52 PMF |
37 | #define RELATIVE_ADDRESS(__rel_label__) __rel_label__ |
38 | ||
39 | #define _ASM_PTR ".long " | |
40 | ||
d98a01c6 PMF |
41 | #else |
42 | ||
43 | struct registers { | |
636ca5d6 PMF |
44 | int padding; /* 4 bytes */ |
45 | short ss; | |
46 | short cs; | |
636ca5d6 PMF |
47 | unsigned long r15; |
48 | unsigned long r14; | |
49 | unsigned long r13; | |
50 | unsigned long r12; | |
51 | unsigned long r11; | |
52 | unsigned long r10; | |
53 | unsigned long r9; | |
54 | unsigned long r8; | |
d98a01c6 | 55 | unsigned long rsi; |
636ca5d6 PMF |
56 | unsigned long rbp; |
57 | unsigned long rdx; | |
58 | unsigned long rcx; | |
d98a01c6 | 59 | unsigned long rdi; |
636ca5d6 PMF |
60 | unsigned long rbx; |
61 | unsigned long rax; | |
a5850bc4 | 62 | unsigned long rflags; |
636ca5d6 | 63 | unsigned long rsp; |
d98a01c6 PMF |
64 | }; |
65 | ||
8524c98d | 66 | #define save_registers(regsptr) \ |
636ca5d6 PMF |
67 | asm volatile ( \ |
68 | /* save original rsp */ \ | |
69 | "pushq %%rsp\n\t" \ | |
a5850bc4 PMF |
70 | /* push original rflags */ \ |
71 | "pushfq\n\t" \ | |
636ca5d6 PMF |
72 | /* rax will hold the ptr to the private stack bottom */ \ |
73 | "pushq %%rax\n\t" \ | |
74 | /* rbx will be used to temporarily hold the stack bottom addr */ \ | |
75 | "pushq %%rbx\n\t" \ | |
76 | /* rdi is the input to __tls_get_addr, and also a temp var */ \ | |
77 | "pushq %%rdi\n\t" \ | |
a5850bc4 | 78 | /* Start TLS access of private reg stack pointer */ \ |
636ca5d6 PMF |
79 | ".byte 0x66\n\t" \ |
80 | "leaq ust_reg_stack_ptr@tlsgd(%%rip), %%rdi\n\t" \ | |
81 | ".word 0x6666\n\t" \ | |
82 | "rex64\n\t" \ | |
83 | "call __tls_get_addr@plt\n\t" \ | |
84 | /* --- End TLS access */ \ | |
a5850bc4 PMF |
85 | /* check if ust_reg_stack_ptr has been initialized */ \ |
86 | "movq (%%rax),%%rbx\n\t" \ | |
87 | "testq %%rbx,%%rbx\n\t" \ | |
88 | "jne 1f\n\t" \ | |
89 | "movq %%rax,%%rbx\n\t" \ | |
90 | /* Start TLS access of private reg stack */ \ | |
91 | ".byte 0x66\n\t" \ | |
92 | "leaq ust_reg_stack@tlsgd(%%rip), %%rdi\n\t" \ | |
93 | ".word 0x6666\n\t" \ | |
94 | "rex64\n\t" \ | |
95 | "call __tls_get_addr@plt\n\t" \ | |
96 | /* --- End TLS access */ \ | |
97 | "addq $500,%%rax\n\t" \ | |
98 | "movq %%rax,(%%rbx)\n\t" \ | |
99 | "movq %%rbx,%%rax\n\t" \ | |
100 | /* now the pointer to the private stack is in rax. | |
101 | must add stack size so the ptr points to the stack bottom. */ \ | |
102 | "1:\n\t" \ | |
636ca5d6 PMF |
103 | /* Manually push rsp to private stack */ \ |
104 | "addq $-8,(%%rax)\n\t" \ | |
a5850bc4 PMF |
105 | "movq 32(%%rsp), %%rdi\n\t" \ |
106 | "movq (%%rax), %%rbx\n\t" \ | |
107 | "movq %%rdi, (%%rbx)\n\t" \ | |
108 | /* Manually push eflags to private stack */ \ | |
109 | "addq $-8,(%%rax)\n\t" \ | |
636ca5d6 PMF |
110 | "movq 24(%%rsp), %%rdi\n\t" \ |
111 | "movq (%%rax), %%rbx\n\t" \ | |
112 | "movq %%rdi, (%%rbx)\n\t" \ | |
113 | /* Manually push rax to private stack */ \ | |
114 | "addq $-8,(%%rax)\n\t" \ | |
115 | "movq 16(%%rsp), %%rdi\n\t" \ | |
116 | "movq (%%rax), %%rbx\n\t" \ | |
117 | "movq %%rdi, (%%rbx)\n\t" \ | |
118 | /* Manually push rbx to private stack */ \ | |
119 | "addq $-8,(%%rax)\n\t" \ | |
120 | "movq 8(%%rsp), %%rdi\n\t" \ | |
121 | "movq (%%rax), %%rbx\n\t" \ | |
122 | "movq %%rdi, (%%rbx)\n\t" \ | |
123 | /* Manually push rdi to private stack */ \ | |
124 | "addq $-8,(%%rax)\n\t" \ | |
125 | "movq 0(%%rsp), %%rdi\n\t" \ | |
126 | "movq (%%rax), %%rbx\n\t" \ | |
127 | "movq %%rdi, (%%rbx)\n\t" \ | |
128 | /* now push regs to tls */ \ | |
129 | /* -- rsp already pushed -- */ \ | |
130 | /* -- rax already pushed -- */ \ | |
131 | /* -- rbx already pushed -- */ \ | |
132 | /* -- rdi already pushed -- */ \ | |
133 | "addq $-8,(%%rax)\n\t" \ | |
134 | "movq (%%rax), %%rbx\n\t" \ | |
135 | "movq %%rcx,(%%rbx)\n\t" \ | |
136 | "addq $-8,(%%rax)\n\t" \ | |
137 | "movq (%%rax), %%rbx\n\t" \ | |
138 | "movq %%rdx,(%%rbx)\n\t" \ | |
139 | "addq $-8,(%%rax)\n\t" \ | |
140 | "movq (%%rax), %%rbx\n\t" \ | |
141 | "movq %%rbp,(%%rbx)\n\t" \ | |
142 | "addq $-8,(%%rax)\n\t" \ | |
143 | "movq (%%rax), %%rbx\n\t" \ | |
144 | "movq %%rsi,(%%rbx)\n\t" \ | |
145 | "addq $-8,(%%rax)\n\t" \ | |
146 | "movq (%%rax), %%rbx\n\t" \ | |
147 | "movq %%r8,(%%rbx)\n\t" \ | |
148 | "addq $-8,(%%rax)\n\t" \ | |
149 | "movq (%%rax), %%rbx\n\t" \ | |
150 | "movq %%r9,(%%rbx)\n\t" \ | |
151 | "addq $-8,(%%rax)\n\t" \ | |
152 | "movq (%%rax), %%rbx\n\t" \ | |
153 | "movq %%r10,(%%rbx)\n\t" \ | |
154 | "addq $-8,(%%rax)\n\t" \ | |
155 | "movq (%%rax), %%rbx\n\t" \ | |
156 | "movq %%r11,(%%rbx)\n\t" \ | |
157 | "addq $-8,(%%rax)\n\t" \ | |
158 | "movq (%%rax), %%rbx\n\t" \ | |
159 | "movq %%r12,(%%rbx)\n\t" \ | |
160 | "addq $-8,(%%rax)\n\t" \ | |
161 | "movq (%%rax), %%rbx\n\t" \ | |
162 | "movq %%r13,(%%rbx)\n\t" \ | |
163 | "addq $-8,(%%rax)\n\t" \ | |
164 | "movq (%%rax), %%rbx\n\t" \ | |
165 | "movq %%r14,(%%rbx)\n\t" \ | |
166 | "addq $-8,(%%rax)\n\t" \ | |
167 | "movq (%%rax), %%rbx\n\t" \ | |
168 | "movq %%r15,(%%rbx)\n\t" \ | |
636ca5d6 PMF |
169 | /* push cs */ \ |
170 | "addq $-2,(%%rax)\n\t" \ | |
171 | "movq (%%rax), %%rbx\n\t" \ | |
172 | "movw %%cs, (%%rbx)\n\t" \ | |
173 | /* push ss */ \ | |
174 | "addq $-2,(%%rax)\n\t" \ | |
175 | "movq (%%rax), %%rbx\n\t" \ | |
176 | "movw %%ss, (%%rbx)\n\t" \ | |
177 | /* add padding for struct registers */ \ | |
178 | "addq $-4,(%%rax)\n\t" \ | |
179 | /* restore original values of regs that were used internally */ \ | |
180 | "popq %%rdi\n\t" \ | |
181 | "popq %%rbx\n\t" \ | |
182 | "popq %%rax\n\t" \ | |
183 | /* cancel push of rsp */ \ | |
184 | "addq $8,%%rsp\n\t" \ | |
a5850bc4 PMF |
185 | /* cancel push of rflags */ \ |
186 | "addq $8,%%rsp\n\t" \ | |
636ca5d6 PMF |
187 | ::); \ |
188 | memcpy(regsptr, (void *)ust_reg_stack_ptr, sizeof(struct registers)); \ | |
189 | ust_reg_stack_ptr = (void *)(((long)ust_reg_stack_ptr) + sizeof(struct registers)); | |
d98a01c6 | 190 | |
9e8f4f52 PMF |
191 | /* Macro to insert the address of a relative jump in an assembly stub, |
192 | * in a relocatable way. On x86-64, this uses a special (%rip) notation. */ | |
193 | #define RELATIVE_ADDRESS(__rel_label__) __rel_label__(%%rip) | |
194 | ||
195 | #define _ASM_PTR ".quad " | |
196 | ||
d98a01c6 PMF |
197 | #endif |
198 | ||
5af57e62 | 199 | #endif /* UST_PROCESSOR_H */ |