4bd0997db76f26e7682554d88ce506140cf96d3d
[ust.git] / include / ust / processor.h
1 #ifndef UST_PROCESSOR_H
2 #define UST_PROCESSOR_H
3
4 #include <stddef.h>
5 #include <string.h>
6
7 extern __thread long ust_reg_stack[500];
8 extern volatile __thread long *ust_reg_stack_ptr;
9
10 #ifndef __x86_64
11
12 struct registers {
13 short ss;
14 short cs;
15 long esi;
16 long ebp;
17 long edx;
18 long edi;
19 long ecx;
20 long ebx;
21 long eax;
22 long eflags;
23 long esp;
24 };
25
26 #ifdef CONFIG_UST_GDB_INTEGRATION
27
28
29 #define save_registers(regsptr) \
30 asm volatile ( \
31 /* save original esp */ \
32 "pushl %%esp\n\t" \
33 /* push original eflags */ \
34 "pushfl\n\t" \
35 /* eax will hold the ptr to the private stack bottom */ \
36 "pushl %%eax\n\t" \
37 /* ebx is used for TLS access */ \
38 "pushl %%ebx\n\t" \
39 /* ecx will be used to temporarily hold the stack bottom addr */ \
40 "pushl %%ecx\n\t" \
41 /* rdi is the input to __tls_get_addr, and also a temp var */ \
42 "pushl %%edi\n\t" \
43 /* Get GOT address */ \
44 "call __i686.get_pc_thunk.bx\n\t" \
45 "addl $_GLOBAL_OFFSET_TABLE_, %%ebx\n\t" \
46 /* Save registers before call (not using ecx yet but we must preserve \
47 the original value of edx. */ \
48 "pushl %%edx\n\t" \
49 /* Start TLS access of private reg stack pointer */ \
50 "leal ust_reg_stack_ptr@tlsgd(,%%ebx,1),%%eax\n\t" \
51 "call ___tls_get_addr@plt\n\t" \
52 /* --- End TLS access */ \
53 "popl %%edx\n\t" \
54 /* check if ust_reg_stack_ptr has been initialized */ \
55 "movl (%%eax),%%ecx\n\t" \
56 "testl %%ecx,%%ecx\n\t" \
57 "jne 1f\n\t" \
58 "movl %%eax,%%ecx\n\t" \
59 /* Save registers before call (using ecx and we must preserve \
60 the original value of edx. */ \
61 "pushl %%ecx\n\t" \
62 "pushl %%edx\n\t" \
63 /* Start TLS access of private reg stack */ \
64 "leal ust_reg_stack@tlsgd(,%%ebx,1),%%eax\n\t" \
65 "call ___tls_get_addr@plt\n\t" \
66 /* --- End TLS access */ \
67 "popl %%edx\n\t" \
68 "popl %%ecx\n\t" \
69 "addl $500,%%eax\n\t" \
70 "movl %%eax,(%%ecx)\n\t" \
71 "movl %%ecx,%%eax\n\t" \
72 /* now the pointer to the private stack is in eax. \
73 must add stack size so the ptr points to the stack bottom. */ \
74 "1:\n\t" \
75 /* Manually push esp to private stack */ \
76 "addl $-4,(%%eax)\n\t" \
77 "movl 20(%%esp), %%edi\n\t" \
78 "movl (%%eax), %%ebx\n\t" \
79 "movl %%edi, (%%ebx)\n\t" \
80 /* Manually push eflags to private stack */ \
81 "addl $-4,(%%eax)\n\t" \
82 "movl 16(%%esp), %%edi\n\t" \
83 "movl (%%eax), %%ebx\n\t" \
84 "movl %%edi, (%%ebx)\n\t" \
85 /* Manually push eax to private stack */ \
86 "addl $-4,(%%eax)\n\t" \
87 "movl 12(%%esp), %%edi\n\t" \
88 "movl (%%eax), %%ebx\n\t" \
89 "movl %%edi, (%%ebx)\n\t" \
90 /* Manually push ebx to private stack */ \
91 "addl $-4,(%%eax)\n\t" \
92 "movl 8(%%esp), %%edi\n\t" \
93 "movl (%%eax), %%ebx\n\t" \
94 "movl %%edi, (%%ebx)\n\t" \
95 /* Manually push ecx to private stack */ \
96 "addl $-4,(%%eax)\n\t" \
97 "movl 4(%%esp), %%edi\n\t" \
98 "movl (%%eax), %%ebx\n\t" \
99 "movl %%edi, (%%ebx)\n\t" \
100 /* Manually push edi to private stack */ \
101 "addl $-4,(%%eax)\n\t" \
102 "movl 0(%%esp), %%edi\n\t" \
103 "movl (%%eax), %%ebx\n\t" \
104 "movl %%edi, (%%ebx)\n\t" \
105 /* now push regs to tls */ \
106 /* -- esp already pushed -- */ \
107 /* -- eax already pushed -- */ \
108 /* -- ebx already pushed -- */ \
109 /* -- ecx already pushed -- */ \
110 /* -- edi already pushed -- */ \
111 "addl $-4,(%%eax)\n\t" \
112 "movl (%%eax), %%ebx\n\t" \
113 "movl %%edx,(%%ebx)\n\t" \
114 "addl $-4,(%%eax)\n\t" \
115 "movl (%%eax), %%ebx\n\t" \
116 "movl %%ebp,(%%ebx)\n\t" \
117 "addl $-4,(%%eax)\n\t" \
118 "movl (%%eax), %%ebx\n\t" \
119 "movl %%esi,(%%ebx)\n\t" \
120 /* push cs */ \
121 "addl $-2,(%%eax)\n\t" \
122 "movl (%%eax), %%ebx\n\t" \
123 "movw %%cs, (%%ebx)\n\t" \
124 /* push ss */ \
125 "addl $-2,(%%eax)\n\t" \
126 "movl (%%eax), %%ebx\n\t" \
127 "movw %%ss, (%%ebx)\n\t" \
128 /* restore original values of regs that were used internally */ \
129 "popl %%edi\n\t" \
130 "popl %%ecx\n\t" \
131 "popl %%ebx\n\t" \
132 "popl %%eax\n\t" \
133 /* cancel push of rsp */ \
134 "addl $4,%%esp\n\t" \
135 /* cancel push of eflags */ \
136 "addl $4,%%esp\n\t" \
137 ::: "memory"); \
138 memcpy(regsptr, (void *)ust_reg_stack_ptr, sizeof(struct registers)); \
139 ust_reg_stack_ptr = (void *)(((long)ust_reg_stack_ptr) + sizeof(struct registers));
140
141 #else /* CONFIG_UST_GDB_INTEGRATION */
142
143 #define save_registers(a)
144
145 #endif /* CONFIG_UST_GDB_INTEGRATION */
146
147 #define RELATIVE_ADDRESS(__rel_label__) __rel_label__
148
149 #define _ASM_PTR ".long "
150
151 #else /* below is code for x86-64 */
152
153 struct registers {
154 int padding; /* 4 bytes */
155 short ss;
156 short cs;
157 unsigned long r15;
158 unsigned long r14;
159 unsigned long r13;
160 unsigned long r12;
161 unsigned long r11;
162 unsigned long r10;
163 unsigned long r9;
164 unsigned long r8;
165 unsigned long rsi;
166 unsigned long rbp;
167 unsigned long rdx;
168 unsigned long rcx;
169 unsigned long rdi;
170 unsigned long rbx;
171 unsigned long rax;
172 unsigned long rflags;
173 unsigned long rsp;
174 };
175
176 #ifdef CONFIG_UST_GDB_INTEGRATION
177
178 #define save_registers(regsptr) \
179 asm volatile ( \
180 /* save original rsp */ \
181 "pushq %%rsp\n\t" \
182 /* push original rflags */ \
183 "pushfq\n\t" \
184 /* rax will hold the ptr to the private stack bottom */ \
185 "pushq %%rax\n\t" \
186 /* rbx will be used to temporarily hold the stack bottom addr */ \
187 "pushq %%rbx\n\t" \
188 /* rdi is the input to __tls_get_addr, and also a temp var */ \
189 "pushq %%rdi\n\t" \
190 /* Start TLS access of private reg stack pointer */ \
191 ".byte 0x66\n\t" \
192 "leaq ust_reg_stack_ptr@tlsgd(%%rip), %%rdi\n\t" \
193 ".word 0x6666\n\t" \
194 "rex64\n\t" \
195 "call __tls_get_addr@plt\n\t" \
196 /* --- End TLS access */ \
197 /* check if ust_reg_stack_ptr has been initialized */ \
198 "movq (%%rax),%%rbx\n\t" \
199 "testq %%rbx,%%rbx\n\t" \
200 "jne 1f\n\t" \
201 "movq %%rax,%%rbx\n\t" \
202 /* Start TLS access of private reg stack */ \
203 ".byte 0x66\n\t" \
204 "leaq ust_reg_stack@tlsgd(%%rip), %%rdi\n\t" \
205 ".word 0x6666\n\t" \
206 "rex64\n\t" \
207 "call __tls_get_addr@plt\n\t" \
208 /* --- End TLS access */ \
209 "addq $500,%%rax\n\t" \
210 "movq %%rax,(%%rbx)\n\t" \
211 "movq %%rbx,%%rax\n\t" \
212 /* now the pointer to the private stack is in rax.
213 must add stack size so the ptr points to the stack bottom. */ \
214 "1:\n\t" \
215 /* Manually push rsp to private stack */ \
216 "addq $-8,(%%rax)\n\t" \
217 "movq 32(%%rsp), %%rdi\n\t" \
218 "movq (%%rax), %%rbx\n\t" \
219 "movq %%rdi, (%%rbx)\n\t" \
220 /* Manually push eflags to private stack */ \
221 "addq $-8,(%%rax)\n\t" \
222 "movq 24(%%rsp), %%rdi\n\t" \
223 "movq (%%rax), %%rbx\n\t" \
224 "movq %%rdi, (%%rbx)\n\t" \
225 /* Manually push rax to private stack */ \
226 "addq $-8,(%%rax)\n\t" \
227 "movq 16(%%rsp), %%rdi\n\t" \
228 "movq (%%rax), %%rbx\n\t" \
229 "movq %%rdi, (%%rbx)\n\t" \
230 /* Manually push rbx to private stack */ \
231 "addq $-8,(%%rax)\n\t" \
232 "movq 8(%%rsp), %%rdi\n\t" \
233 "movq (%%rax), %%rbx\n\t" \
234 "movq %%rdi, (%%rbx)\n\t" \
235 /* Manually push rdi to private stack */ \
236 "addq $-8,(%%rax)\n\t" \
237 "movq 0(%%rsp), %%rdi\n\t" \
238 "movq (%%rax), %%rbx\n\t" \
239 "movq %%rdi, (%%rbx)\n\t" \
240 /* now push regs to tls */ \
241 /* -- rsp already pushed -- */ \
242 /* -- rax already pushed -- */ \
243 /* -- rbx already pushed -- */ \
244 /* -- rdi already pushed -- */ \
245 "addq $-8,(%%rax)\n\t" \
246 "movq (%%rax), %%rbx\n\t" \
247 "movq %%rcx,(%%rbx)\n\t" \
248 "addq $-8,(%%rax)\n\t" \
249 "movq (%%rax), %%rbx\n\t" \
250 "movq %%rdx,(%%rbx)\n\t" \
251 "addq $-8,(%%rax)\n\t" \
252 "movq (%%rax), %%rbx\n\t" \
253 "movq %%rbp,(%%rbx)\n\t" \
254 "addq $-8,(%%rax)\n\t" \
255 "movq (%%rax), %%rbx\n\t" \
256 "movq %%rsi,(%%rbx)\n\t" \
257 "addq $-8,(%%rax)\n\t" \
258 "movq (%%rax), %%rbx\n\t" \
259 "movq %%r8,(%%rbx)\n\t" \
260 "addq $-8,(%%rax)\n\t" \
261 "movq (%%rax), %%rbx\n\t" \
262 "movq %%r9,(%%rbx)\n\t" \
263 "addq $-8,(%%rax)\n\t" \
264 "movq (%%rax), %%rbx\n\t" \
265 "movq %%r10,(%%rbx)\n\t" \
266 "addq $-8,(%%rax)\n\t" \
267 "movq (%%rax), %%rbx\n\t" \
268 "movq %%r11,(%%rbx)\n\t" \
269 "addq $-8,(%%rax)\n\t" \
270 "movq (%%rax), %%rbx\n\t" \
271 "movq %%r12,(%%rbx)\n\t" \
272 "addq $-8,(%%rax)\n\t" \
273 "movq (%%rax), %%rbx\n\t" \
274 "movq %%r13,(%%rbx)\n\t" \
275 "addq $-8,(%%rax)\n\t" \
276 "movq (%%rax), %%rbx\n\t" \
277 "movq %%r14,(%%rbx)\n\t" \
278 "addq $-8,(%%rax)\n\t" \
279 "movq (%%rax), %%rbx\n\t" \
280 "movq %%r15,(%%rbx)\n\t" \
281 /* push cs */ \
282 "addq $-2,(%%rax)\n\t" \
283 "movq (%%rax), %%rbx\n\t" \
284 "movw %%cs, (%%rbx)\n\t" \
285 /* push ss */ \
286 "addq $-2,(%%rax)\n\t" \
287 "movq (%%rax), %%rbx\n\t" \
288 "movw %%ss, (%%rbx)\n\t" \
289 /* add padding for struct registers */ \
290 "addq $-4,(%%rax)\n\t" \
291 /* restore original values of regs that were used internally */ \
292 "popq %%rdi\n\t" \
293 "popq %%rbx\n\t" \
294 "popq %%rax\n\t" \
295 /* cancel push of rsp */ \
296 "addq $8,%%rsp\n\t" \
297 /* cancel push of rflags */ \
298 "addq $8,%%rsp\n\t" \
299 ::); \
300 memcpy(regsptr, (void *)ust_reg_stack_ptr, sizeof(struct registers)); \
301 ust_reg_stack_ptr = (void *)(((long)ust_reg_stack_ptr) + sizeof(struct registers));
302
303 #else /* CONFIG_UST_GDB_INTEGRATION */
304
305 #define save_registers(a)
306
307 #endif /* CONFIG_UST_GDB_INTEGRATION */
308
309 /* Macro to insert the address of a relative jump in an assembly stub,
310 * in a relocatable way. On x86-64, this uses a special (%rip) notation. */
311 #define RELATIVE_ADDRESS(__rel_label__) __rel_label__(%%rip)
312
313 #define _ASM_PTR ".quad "
314
315 #endif
316
317 #endif /* UST_PROCESSOR_H */
This page took 0.03534 seconds and 3 git commands to generate.