Add some sanity to save_registers on x86-32
[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 //#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" \
38 /* ebx is used for TLS access */ \
39 "pushl %%ebx\n\t" \
40 /* ecx will be used to temporarily hold the stack bottom addr */ \
41 "pushl %%ecx\n\t" \
42 /* rdi is the input to __tls_get_addr, and also a temp var */ \
43 "pushl %%edi\n\t" \
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" \
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 */ \
54 "popl %%edx\n\t" \
55 /* check if ust_reg_stack_ptr has been initialized */ \
56 "movl (%%eax),%%ecx\n\t" \
57 "testl %%ecx,%%ecx\n\t" \
58 "jne 1f\n\t" \
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" \
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 */ \
68 "popl %%edx\n\t" \
69 "popl %%ecx\n\t" \
70 "addl $500,%%eax\n\t" \
71 "movl %%eax,(%%ecx)\n\t" \
72 "movl %%ecx,%%eax\n\t" \
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" \
78 "movl 20(%%esp), %%edi\n\t" \
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" \
83 "movl 16(%%esp), %%edi\n\t" \
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" \
88 "movl 12(%%esp), %%edi\n\t" \
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" \
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" \
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 -- */ \
110 /* -- ecx already pushed -- */ \
111 /* -- edi already pushed -- */ \
112 "addl $-4,(%%eax)\n\t" \
113 "movl (%%eax), %%ebx\n\t" \
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" \
131 "popl %%ecx\n\t" \
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));
141
142 #else /* CONFIG_UST_GDB_INTEGRATION */
143
144 #define save_registers(a)
145
146 #endif /* CONFIG_UST_GDB_INTEGRATION */
147
148 #define RELATIVE_ADDRESS(__rel_label__) __rel_label__
149
150 #define _ASM_PTR ".long "
151
152 #else /* below is code for x86-64 */
153
154 struct registers {
155 int padding; /* 4 bytes */
156 short ss;
157 short cs;
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;
166 unsigned long rsi;
167 unsigned long rbp;
168 unsigned long rdx;
169 unsigned long rcx;
170 unsigned long rdi;
171 unsigned long rbx;
172 unsigned long rax;
173 unsigned long rflags;
174 unsigned long rsp;
175 };
176
177 #ifdef CONFIG_UST_GDB_INTEGRATION
178
179 #define save_registers(regsptr) \
180 asm volatile ( \
181 /* save original rsp */ \
182 "pushq %%rsp\n\t" \
183 /* push original rflags */ \
184 "pushfq\n\t" \
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" \
191 /* Start TLS access of private reg stack pointer */ \
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 */ \
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" \
216 /* Manually push rsp to private stack */ \
217 "addq $-8,(%%rax)\n\t" \
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" \
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" \
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" \
298 /* cancel push of rflags */ \
299 "addq $8,%%rsp\n\t" \
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));
303
304 #else /* CONFIG_UST_GDB_INTEGRATION */
305
306 #define save_registers(a)
307
308 #endif /* CONFIG_UST_GDB_INTEGRATION */
309
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
316 #endif
317
318 #endif /* UST_PROCESSOR_H */
This page took 0.035931 seconds and 5 git commands to generate.