95e1164af5ad2085527ddaf8d4579c0708fb8bf7
[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 /* save_registers - saves most of the processor's registers so
29 * they are available to the probe. gdb uses this to give the
30 * value of local variables.
31 *
32 * Saving all registers without losing any of their values is
33 * tricky.
34 *
35 * We cannot pass to the asm stub the address of a registers structure
36 * on the stack, because it will use a register and override its value.
37 *
38 * We don't want to use a stub to push the regs on the stack and then
39 * another stub to copy them to a structure because changing %sp in asm
40 * and then returning to C (even briefly) can have unexpected results.
41 * Also, gcc might modify %sp between the stubs in reaction to the
42 * register needs of the second stub that needs to know where to copy
43 * the register values.
44 *
45 * So the chosen approach is to use another stack, declared in thread-
46 * local storage, to push the registers. They are subsequently copied
47 * to the stack, by C code.
48 */
49
50 #define save_registers(regsptr) \
51 asm volatile ( \
52 /* save original esp */ \
53 "pushl %%esp\n\t" \
54 /* push original eflags */ \
55 "pushfl\n\t" \
56 /* eax will hold the ptr to the private stack bottom */ \
57 "pushl %%eax\n\t" \
58 /* ebx is used for TLS access */ \
59 "pushl %%ebx\n\t" \
60 /* ecx will be used to temporarily hold the stack bottom addr */\
61 "pushl %%ecx\n\t" \
62 /* rdi is the input to __tls_get_addr, and also a temp var */ \
63 "pushl %%edi\n\t" \
64 /* For TLS access, we have to do function calls. However, \
65 * we must not lose the original value of: \
66 * esp, eflags, eax, ebx, ecx, edx, esi, edi, ebp, cs, ss \
67 * \
68 * Some registers' original values have already been saved: \
69 * esp, eflags, eax, ebx, ecx, edi \
70 * \
71 * In addition, the i386 ABI says the following registers belong\
72 * to the caller function: \
73 * esp, ebp, esi, edi, ebx \
74 * \
75 * The following registers should not be changed by the callee: \
76 * cs, ss \
77 * \
78 * Therefore, the following registers must be explicitly \
79 * preserved: \
80 * edx \
81 */ \
82 "pushl %%edx\n\t" \
83 /* Get GOT address */ \
84 "call __i686.get_pc_thunk.bx\n\t" \
85 "addl $_GLOBAL_OFFSET_TABLE_, %%ebx\n\t" \
86 /* Start TLS access of private reg stack pointer */ \
87 "leal ust_reg_stack_ptr@tlsgd(,%%ebx,1),%%eax\n\t" \
88 "call ___tls_get_addr@plt\n\t" \
89 /* --- End TLS access */ \
90 /* check if ust_reg_stack_ptr has been initialized */ \
91 "movl (%%eax),%%ecx\n\t" \
92 "testl %%ecx,%%ecx\n\t" \
93 "jne 1f\n\t" \
94 "movl %%eax,%%ecx\n\t" \
95 /* Save ecx because we are using it. */ \
96 "pushl %%ecx\n\t" \
97 /* Start TLS access of private reg stack */ \
98 "leal ust_reg_stack@tlsgd(,%%ebx,1),%%eax\n\t" \
99 "call ___tls_get_addr@plt\n\t" \
100 /* --- End TLS access */ \
101 "popl %%ecx\n\t" \
102 "addl $500,%%eax\n\t" \
103 "movl %%eax,(%%ecx)\n\t" \
104 "movl %%ecx,%%eax\n\t" \
105 /* now the pointer to the private stack is in eax. \
106 must add stack size so the ptr points to the stack bottom. */ \
107 "1:\n\t" \
108 /* edx was pushed for function calls */ \
109 "popl %%edx\n\t" \
110 /* Manually push esp to private stack */ \
111 "addl $-4,(%%eax)\n\t" \
112 "movl 20(%%esp), %%edi\n\t" \
113 "movl (%%eax), %%ebx\n\t" \
114 "movl %%edi, (%%ebx)\n\t" \
115 /* Manually push eflags to private stack */ \
116 "addl $-4,(%%eax)\n\t" \
117 "movl 16(%%esp), %%edi\n\t" \
118 "movl (%%eax), %%ebx\n\t" \
119 "movl %%edi, (%%ebx)\n\t" \
120 /* Manually push eax to private stack */ \
121 "addl $-4,(%%eax)\n\t" \
122 "movl 12(%%esp), %%edi\n\t" \
123 "movl (%%eax), %%ebx\n\t" \
124 "movl %%edi, (%%ebx)\n\t" \
125 /* Manually push ebx to private stack */ \
126 "addl $-4,(%%eax)\n\t" \
127 "movl 8(%%esp), %%edi\n\t" \
128 "movl (%%eax), %%ebx\n\t" \
129 "movl %%edi, (%%ebx)\n\t" \
130 /* Manually push ecx to private stack */ \
131 "addl $-4,(%%eax)\n\t" \
132 "movl 4(%%esp), %%edi\n\t" \
133 "movl (%%eax), %%ebx\n\t" \
134 "movl %%edi, (%%ebx)\n\t" \
135 /* Manually push edi to private stack */ \
136 "addl $-4,(%%eax)\n\t" \
137 "movl 0(%%esp), %%edi\n\t" \
138 "movl (%%eax), %%ebx\n\t" \
139 "movl %%edi, (%%ebx)\n\t" \
140 /* now push regs to tls */ \
141 /* -- esp already pushed -- */ \
142 /* -- eax already pushed -- */ \
143 /* -- ebx already pushed -- */ \
144 /* -- ecx already pushed -- */ \
145 /* -- edi already pushed -- */ \
146 "addl $-4,(%%eax)\n\t" \
147 "movl (%%eax), %%ebx\n\t" \
148 "movl %%edx,(%%ebx)\n\t" \
149 "addl $-4,(%%eax)\n\t" \
150 "movl (%%eax), %%ebx\n\t" \
151 "movl %%ebp,(%%ebx)\n\t" \
152 "addl $-4,(%%eax)\n\t" \
153 "movl (%%eax), %%ebx\n\t" \
154 "movl %%esi,(%%ebx)\n\t" \
155 /* push cs */ \
156 "addl $-2,(%%eax)\n\t" \
157 "movl (%%eax), %%ebx\n\t" \
158 "movw %%cs, (%%ebx)\n\t" \
159 /* push ss */ \
160 "addl $-2,(%%eax)\n\t" \
161 "movl (%%eax), %%ebx\n\t" \
162 "movw %%ss, (%%ebx)\n\t" \
163 /* restore original values of regs that were used internally */ \
164 "popl %%edi\n\t" \
165 "popl %%ecx\n\t" \
166 "popl %%ebx\n\t" \
167 "popl %%eax\n\t" \
168 /* cancel push of rsp */ \
169 "addl $4,%%esp\n\t" \
170 /* cancel push of eflags */ \
171 "addl $4,%%esp\n\t" \
172 ::: "memory"); \
173 memcpy(regsptr, (void *)ust_reg_stack_ptr, sizeof(struct registers)); \
174 ust_reg_stack_ptr = (void *)(((long)ust_reg_stack_ptr) + sizeof(struct registers));
175
176 #else /* CONFIG_UST_GDB_INTEGRATION */
177
178 #define save_registers(a)
179
180 #endif /* CONFIG_UST_GDB_INTEGRATION */
181
182 #define RELATIVE_ADDRESS(__rel_label__) __rel_label__
183
184 #define ARCH_COPY_ADDR(src, dst) "lea " src "," dst
185
186 #define _ASM_PTR ".long "
187
188 #else /* below is code for x86-64 */
189
190 struct registers {
191 int padding; /* 4 bytes */
192 short ss;
193 short cs;
194 unsigned long r15;
195 unsigned long r14;
196 unsigned long r13;
197 unsigned long r12;
198 unsigned long r11;
199 unsigned long r10;
200 unsigned long r9;
201 unsigned long r8;
202 unsigned long rsi;
203 unsigned long rbp;
204 unsigned long rdx;
205 unsigned long rcx;
206 unsigned long rdi;
207 unsigned long rbx;
208 unsigned long rax;
209 unsigned long rflags;
210 unsigned long rsp;
211 };
212
213 #ifdef CONFIG_UST_GDB_INTEGRATION
214
215 #define save_registers(regsptr) \
216 asm volatile ( \
217 /* save original rsp */ \
218 "pushq %%rsp\n\t" \
219 /* push original rflags */ \
220 "pushfq\n\t" \
221 /* rax will hold the ptr to the private stack bottom */ \
222 "pushq %%rax\n\t" \
223 /* rbx will be used to temporarily hold the stack bottom addr */ \
224 "pushq %%rbx\n\t" \
225 /* rdi is the input to __tls_get_addr, and also a temp var */ \
226 "pushq %%rdi\n\t" \
227 /* For TLS access, we have to do function calls. However, \
228 * we must not lose the original value of: \
229 * rsp, rflags, rax, rbx, rcx, rdx, rsi, rdi, rbp, r8, r9 \
230 * r10, r11, r12, r13, r14, r15, cs, ss \
231 * \
232 * Some registers' original values have already been saved: \
233 * rsp, rflags, rax, rbx, rdi \
234 * \
235 * In addition, the x86-64 ABI says the following registers \
236 * belong to the caller function: \
237 * rbp, rbx, r12, r13, r14, r15 \
238 * \
239 * The following registers should not be changed by the callee: \
240 * cs, ss \
241 * \
242 * Therefore, the following registers must be explicitly \
243 * preserved: \
244 * rcx, rdx, rsi, r8, r9, r10, r11 \
245 */ \
246 "pushq %%rcx\n\t" \
247 "pushq %%rdx\n\t" \
248 "pushq %%rsi\n\t" \
249 "pushq %%r8\n\t" \
250 "pushq %%r9\n\t" \
251 "pushq %%r10\n\t" \
252 "pushq %%r11\n\t" \
253 /* Start TLS access of private reg stack pointer */ \
254 ".byte 0x66\n\t" \
255 "leaq ust_reg_stack_ptr@tlsgd(%%rip), %%rdi\n\t" \
256 ".word 0x6666\n\t" \
257 "rex64\n\t" \
258 "call __tls_get_addr@plt\n\t" \
259 /* --- End TLS access */ \
260 /* check if ust_reg_stack_ptr has been initialized */ \
261 "movq (%%rax),%%rbx\n\t" \
262 "testq %%rbx,%%rbx\n\t" \
263 "jne 1f\n\t" \
264 "movq %%rax,%%rbx\n\t" \
265 /* Start TLS access of private reg stack */ \
266 ".byte 0x66\n\t" \
267 "leaq ust_reg_stack@tlsgd(%%rip), %%rdi\n\t" \
268 ".word 0x6666\n\t" \
269 "rex64\n\t" \
270 "call __tls_get_addr@plt\n\t" \
271 /* --- End TLS access */ \
272 "addq $500,%%rax\n\t" \
273 "movq %%rax,(%%rbx)\n\t" \
274 "movq %%rbx,%%rax\n\t" \
275 /* now the pointer to the private stack is in rax.
276 must add stack size so the ptr points to the stack bottom. */ \
277 "1:\n\t" \
278 /* Pop regs that were pushed for function calls */ \
279 "popq %%r11\n\t" \
280 "popq %%r10\n\t" \
281 "popq %%r9\n\t" \
282 "popq %%r8\n\t" \
283 "popq %%rsi\n\t" \
284 "popq %%rdx\n\t" \
285 "popq %%rcx\n\t" \
286 /* Manually push rsp to private stack */ \
287 "addq $-8,(%%rax)\n\t" \
288 "movq 32(%%rsp), %%rdi\n\t" \
289 "movq (%%rax), %%rbx\n\t" \
290 "movq %%rdi, (%%rbx)\n\t" \
291 /* Manually push eflags to private stack */ \
292 "addq $-8,(%%rax)\n\t" \
293 "movq 24(%%rsp), %%rdi\n\t" \
294 "movq (%%rax), %%rbx\n\t" \
295 "movq %%rdi, (%%rbx)\n\t" \
296 /* Manually push rax to private stack */ \
297 "addq $-8,(%%rax)\n\t" \
298 "movq 16(%%rsp), %%rdi\n\t" \
299 "movq (%%rax), %%rbx\n\t" \
300 "movq %%rdi, (%%rbx)\n\t" \
301 /* Manually push rbx to private stack */ \
302 "addq $-8,(%%rax)\n\t" \
303 "movq 8(%%rsp), %%rdi\n\t" \
304 "movq (%%rax), %%rbx\n\t" \
305 "movq %%rdi, (%%rbx)\n\t" \
306 /* Manually push rdi to private stack */ \
307 "addq $-8,(%%rax)\n\t" \
308 "movq 0(%%rsp), %%rdi\n\t" \
309 "movq (%%rax), %%rbx\n\t" \
310 "movq %%rdi, (%%rbx)\n\t" \
311 /* now push regs to tls */ \
312 /* -- rsp already pushed -- */ \
313 /* -- rax already pushed -- */ \
314 /* -- rbx already pushed -- */ \
315 /* -- rdi already pushed -- */ \
316 "addq $-8,(%%rax)\n\t" \
317 "movq (%%rax), %%rbx\n\t" \
318 "movq %%rcx,(%%rbx)\n\t" \
319 "addq $-8,(%%rax)\n\t" \
320 "movq (%%rax), %%rbx\n\t" \
321 "movq %%rdx,(%%rbx)\n\t" \
322 "addq $-8,(%%rax)\n\t" \
323 "movq (%%rax), %%rbx\n\t" \
324 "movq %%rbp,(%%rbx)\n\t" \
325 "addq $-8,(%%rax)\n\t" \
326 "movq (%%rax), %%rbx\n\t" \
327 "movq %%rsi,(%%rbx)\n\t" \
328 "addq $-8,(%%rax)\n\t" \
329 "movq (%%rax), %%rbx\n\t" \
330 "movq %%r8,(%%rbx)\n\t" \
331 "addq $-8,(%%rax)\n\t" \
332 "movq (%%rax), %%rbx\n\t" \
333 "movq %%r9,(%%rbx)\n\t" \
334 "addq $-8,(%%rax)\n\t" \
335 "movq (%%rax), %%rbx\n\t" \
336 "movq %%r10,(%%rbx)\n\t" \
337 "addq $-8,(%%rax)\n\t" \
338 "movq (%%rax), %%rbx\n\t" \
339 "movq %%r11,(%%rbx)\n\t" \
340 "addq $-8,(%%rax)\n\t" \
341 "movq (%%rax), %%rbx\n\t" \
342 "movq %%r12,(%%rbx)\n\t" \
343 "addq $-8,(%%rax)\n\t" \
344 "movq (%%rax), %%rbx\n\t" \
345 "movq %%r13,(%%rbx)\n\t" \
346 "addq $-8,(%%rax)\n\t" \
347 "movq (%%rax), %%rbx\n\t" \
348 "movq %%r14,(%%rbx)\n\t" \
349 "addq $-8,(%%rax)\n\t" \
350 "movq (%%rax), %%rbx\n\t" \
351 "movq %%r15,(%%rbx)\n\t" \
352 /* push cs */ \
353 "addq $-2,(%%rax)\n\t" \
354 "movq (%%rax), %%rbx\n\t" \
355 "movw %%cs, (%%rbx)\n\t" \
356 /* push ss */ \
357 "addq $-2,(%%rax)\n\t" \
358 "movq (%%rax), %%rbx\n\t" \
359 "movw %%ss, (%%rbx)\n\t" \
360 /* add padding for struct registers */ \
361 "addq $-4,(%%rax)\n\t" \
362 /* restore original values of regs that were used internally */ \
363 "popq %%rdi\n\t" \
364 "popq %%rbx\n\t" \
365 "popq %%rax\n\t" \
366 /* cancel push of rsp */ \
367 "addq $8,%%rsp\n\t" \
368 /* cancel push of rflags */ \
369 "addq $8,%%rsp\n\t" \
370 ::); \
371 memcpy(regsptr, (void *)ust_reg_stack_ptr, sizeof(struct registers)); \
372 ust_reg_stack_ptr = (void *)(((long)ust_reg_stack_ptr) + sizeof(struct registers));
373
374 #else /* CONFIG_UST_GDB_INTEGRATION */
375
376 #define save_registers(a)
377
378 #endif /* CONFIG_UST_GDB_INTEGRATION */
379
380 /* Macro to insert the address of a relative jump in an assembly stub,
381 * in a relocatable way. On x86-64, this uses a special (%rip) notation. */
382 #define RELATIVE_ADDRESS(__rel_label__) __rel_label__(%%rip)
383
384 #define ARCH_COPY_ADDR(src, dst) "lea " src "(%%rip)," dst
385
386 #define _ASM_PTR ".quad "
387
388 #endif
389
390 #endif /* UST_PROCESSOR_H */
This page took 0.046963 seconds and 4 git commands to generate.