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