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 | ||
e003d6ee | 10 | #ifndef __x86_64 |
d98a01c6 PMF |
11 | |
12 | struct 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 | |
55c5b393 PMF |
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 | */ | |
7756d65a PMF |
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" \ | |
f2496f58 | 58 | /* ebx is used for TLS access */ \ |
7756d65a | 59 | "pushl %%ebx\n\t" \ |
55c5b393 PMF |
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" \ | |
f2496f58 PMF |
83 | /* Get GOT address */ \ |
84 | "call __i686.get_pc_thunk.bx\n\t" \ | |
85 | "addl $_GLOBAL_OFFSET_TABLE_, %%ebx\n\t" \ | |
7756d65a PMF |
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 */ \ | |
f2496f58 PMF |
91 | "movl (%%eax),%%ecx\n\t" \ |
92 | "testl %%ecx,%%ecx\n\t" \ | |
7756d65a | 93 | "jne 1f\n\t" \ |
f2496f58 | 94 | "movl %%eax,%%ecx\n\t" \ |
55c5b393 | 95 | /* Save ecx because we are using it. */ \ |
f2496f58 | 96 | "pushl %%ecx\n\t" \ |
7756d65a PMF |
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 */ \ | |
f2496f58 | 101 | "popl %%ecx\n\t" \ |
7756d65a | 102 | "addl $500,%%eax\n\t" \ |
f2496f58 PMF |
103 | "movl %%eax,(%%ecx)\n\t" \ |
104 | "movl %%ecx,%%eax\n\t" \ | |
7756d65a PMF |
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" \ | |
55c5b393 PMF |
108 | /* edx was pushed for function calls */ \ |
109 | "popl %%edx\n\t" \ | |
7756d65a PMF |
110 | /* Manually push esp to private stack */ \ |
111 | "addl $-4,(%%eax)\n\t" \ | |
f2496f58 | 112 | "movl 20(%%esp), %%edi\n\t" \ |
7756d65a PMF |
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" \ | |
f2496f58 | 117 | "movl 16(%%esp), %%edi\n\t" \ |
7756d65a PMF |
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" \ | |
f2496f58 | 122 | "movl 12(%%esp), %%edi\n\t" \ |
7756d65a PMF |
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" \ | |
f2496f58 PMF |
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" \ | |
7756d65a PMF |
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 -- */ \ | |
f2496f58 | 144 | /* -- ecx already pushed -- */ \ |
7756d65a PMF |
145 | /* -- edi already pushed -- */ \ |
146 | "addl $-4,(%%eax)\n\t" \ | |
147 | "movl (%%eax), %%ebx\n\t" \ | |
7756d65a PMF |
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" \ | |
f2496f58 | 165 | "popl %%ecx\n\t" \ |
7756d65a PMF |
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)); | |
defa46a7 | 175 | |
e003d6ee | 176 | #else /* CONFIG_UST_GDB_INTEGRATION */ |
defa46a7 | 177 | |
defa46a7 PMF |
178 | #define save_registers(a) |
179 | ||
e003d6ee | 180 | #endif /* CONFIG_UST_GDB_INTEGRATION */ |
d98a01c6 | 181 | |
9e8f4f52 PMF |
182 | #define RELATIVE_ADDRESS(__rel_label__) __rel_label__ |
183 | ||
184 | #define _ASM_PTR ".long " | |
185 | ||
defa46a7 | 186 | #else /* below is code for x86-64 */ |
d98a01c6 PMF |
187 | |
188 | struct registers { | |
636ca5d6 PMF |
189 | int padding; /* 4 bytes */ |
190 | short ss; | |
191 | short cs; | |
636ca5d6 PMF |
192 | unsigned long r15; |
193 | unsigned long r14; | |
194 | unsigned long r13; | |
195 | unsigned long r12; | |
196 | unsigned long r11; | |
197 | unsigned long r10; | |
198 | unsigned long r9; | |
199 | unsigned long r8; | |
d98a01c6 | 200 | unsigned long rsi; |
636ca5d6 PMF |
201 | unsigned long rbp; |
202 | unsigned long rdx; | |
203 | unsigned long rcx; | |
d98a01c6 | 204 | unsigned long rdi; |
636ca5d6 PMF |
205 | unsigned long rbx; |
206 | unsigned long rax; | |
a5850bc4 | 207 | unsigned long rflags; |
636ca5d6 | 208 | unsigned long rsp; |
d98a01c6 PMF |
209 | }; |
210 | ||
e003d6ee | 211 | #ifdef CONFIG_UST_GDB_INTEGRATION |
defa46a7 | 212 | |
8524c98d | 213 | #define save_registers(regsptr) \ |
636ca5d6 PMF |
214 | asm volatile ( \ |
215 | /* save original rsp */ \ | |
216 | "pushq %%rsp\n\t" \ | |
a5850bc4 PMF |
217 | /* push original rflags */ \ |
218 | "pushfq\n\t" \ | |
636ca5d6 PMF |
219 | /* rax will hold the ptr to the private stack bottom */ \ |
220 | "pushq %%rax\n\t" \ | |
221 | /* rbx will be used to temporarily hold the stack bottom addr */ \ | |
222 | "pushq %%rbx\n\t" \ | |
223 | /* rdi is the input to __tls_get_addr, and also a temp var */ \ | |
224 | "pushq %%rdi\n\t" \ | |
55c5b393 PMF |
225 | /* For TLS access, we have to do function calls. However, \ |
226 | * we must not lose the original value of: \ | |
227 | * rsp, rflags, rax, rbx, rcx, rdx, rsi, rdi, rbp, r8, r9 \ | |
228 | * r10, r11, r12, r13, r14, r15, cs, ss \ | |
229 | * \ | |
230 | * Some registers' original values have already been saved: \ | |
231 | * rsp, rflags, rax, rbx, rdi \ | |
232 | * \ | |
233 | * In addition, the x86-64 ABI says the following registers \ | |
234 | * belong to the caller function: \ | |
235 | * rbp, rbx, r12, r13, r14, r15 \ | |
236 | * \ | |
237 | * The following registers should not be changed by the callee: \ | |
238 | * cs, ss \ | |
239 | * \ | |
240 | * Therefore, the following registers must be explicitly \ | |
241 | * preserved: \ | |
242 | * rcx, rdx, rsi, r8, r9, r10, r11 \ | |
243 | */ \ | |
244 | "pushq %%rcx\n\t" \ | |
245 | "pushq %%rdx\n\t" \ | |
246 | "pushq %%rsi\n\t" \ | |
247 | "pushq %%r8\n\t" \ | |
248 | "pushq %%r9\n\t" \ | |
249 | "pushq %%r10\n\t" \ | |
250 | "pushq %%r11\n\t" \ | |
a5850bc4 | 251 | /* Start TLS access of private reg stack pointer */ \ |
636ca5d6 PMF |
252 | ".byte 0x66\n\t" \ |
253 | "leaq ust_reg_stack_ptr@tlsgd(%%rip), %%rdi\n\t" \ | |
254 | ".word 0x6666\n\t" \ | |
255 | "rex64\n\t" \ | |
256 | "call __tls_get_addr@plt\n\t" \ | |
257 | /* --- End TLS access */ \ | |
a5850bc4 PMF |
258 | /* check if ust_reg_stack_ptr has been initialized */ \ |
259 | "movq (%%rax),%%rbx\n\t" \ | |
260 | "testq %%rbx,%%rbx\n\t" \ | |
261 | "jne 1f\n\t" \ | |
262 | "movq %%rax,%%rbx\n\t" \ | |
263 | /* Start TLS access of private reg stack */ \ | |
264 | ".byte 0x66\n\t" \ | |
265 | "leaq ust_reg_stack@tlsgd(%%rip), %%rdi\n\t" \ | |
266 | ".word 0x6666\n\t" \ | |
267 | "rex64\n\t" \ | |
268 | "call __tls_get_addr@plt\n\t" \ | |
269 | /* --- End TLS access */ \ | |
270 | "addq $500,%%rax\n\t" \ | |
271 | "movq %%rax,(%%rbx)\n\t" \ | |
272 | "movq %%rbx,%%rax\n\t" \ | |
273 | /* now the pointer to the private stack is in rax. | |
274 | must add stack size so the ptr points to the stack bottom. */ \ | |
275 | "1:\n\t" \ | |
55c5b393 PMF |
276 | /* Pop regs that were pushed for function calls */ \ |
277 | "popq %%r11\n\t" \ | |
278 | "popq %%r10\n\t" \ | |
279 | "popq %%r9\n\t" \ | |
280 | "popq %%r8\n\t" \ | |
281 | "popq %%rsi\n\t" \ | |
282 | "popq %%rdx\n\t" \ | |
283 | "popq %%rcx\n\t" \ | |
636ca5d6 PMF |
284 | /* Manually push rsp to private stack */ \ |
285 | "addq $-8,(%%rax)\n\t" \ | |
a5850bc4 PMF |
286 | "movq 32(%%rsp), %%rdi\n\t" \ |
287 | "movq (%%rax), %%rbx\n\t" \ | |
288 | "movq %%rdi, (%%rbx)\n\t" \ | |
289 | /* Manually push eflags to private stack */ \ | |
290 | "addq $-8,(%%rax)\n\t" \ | |
636ca5d6 PMF |
291 | "movq 24(%%rsp), %%rdi\n\t" \ |
292 | "movq (%%rax), %%rbx\n\t" \ | |
293 | "movq %%rdi, (%%rbx)\n\t" \ | |
294 | /* Manually push rax to private stack */ \ | |
295 | "addq $-8,(%%rax)\n\t" \ | |
296 | "movq 16(%%rsp), %%rdi\n\t" \ | |
297 | "movq (%%rax), %%rbx\n\t" \ | |
298 | "movq %%rdi, (%%rbx)\n\t" \ | |
299 | /* Manually push rbx to private stack */ \ | |
300 | "addq $-8,(%%rax)\n\t" \ | |
301 | "movq 8(%%rsp), %%rdi\n\t" \ | |
302 | "movq (%%rax), %%rbx\n\t" \ | |
303 | "movq %%rdi, (%%rbx)\n\t" \ | |
304 | /* Manually push rdi to private stack */ \ | |
305 | "addq $-8,(%%rax)\n\t" \ | |
306 | "movq 0(%%rsp), %%rdi\n\t" \ | |
307 | "movq (%%rax), %%rbx\n\t" \ | |
308 | "movq %%rdi, (%%rbx)\n\t" \ | |
309 | /* now push regs to tls */ \ | |
310 | /* -- rsp already pushed -- */ \ | |
311 | /* -- rax already pushed -- */ \ | |
312 | /* -- rbx already pushed -- */ \ | |
313 | /* -- rdi already pushed -- */ \ | |
314 | "addq $-8,(%%rax)\n\t" \ | |
315 | "movq (%%rax), %%rbx\n\t" \ | |
316 | "movq %%rcx,(%%rbx)\n\t" \ | |
317 | "addq $-8,(%%rax)\n\t" \ | |
318 | "movq (%%rax), %%rbx\n\t" \ | |
319 | "movq %%rdx,(%%rbx)\n\t" \ | |
320 | "addq $-8,(%%rax)\n\t" \ | |
321 | "movq (%%rax), %%rbx\n\t" \ | |
322 | "movq %%rbp,(%%rbx)\n\t" \ | |
323 | "addq $-8,(%%rax)\n\t" \ | |
324 | "movq (%%rax), %%rbx\n\t" \ | |
325 | "movq %%rsi,(%%rbx)\n\t" \ | |
326 | "addq $-8,(%%rax)\n\t" \ | |
327 | "movq (%%rax), %%rbx\n\t" \ | |
328 | "movq %%r8,(%%rbx)\n\t" \ | |
329 | "addq $-8,(%%rax)\n\t" \ | |
330 | "movq (%%rax), %%rbx\n\t" \ | |
331 | "movq %%r9,(%%rbx)\n\t" \ | |
332 | "addq $-8,(%%rax)\n\t" \ | |
333 | "movq (%%rax), %%rbx\n\t" \ | |
334 | "movq %%r10,(%%rbx)\n\t" \ | |
335 | "addq $-8,(%%rax)\n\t" \ | |
336 | "movq (%%rax), %%rbx\n\t" \ | |
337 | "movq %%r11,(%%rbx)\n\t" \ | |
338 | "addq $-8,(%%rax)\n\t" \ | |
339 | "movq (%%rax), %%rbx\n\t" \ | |
340 | "movq %%r12,(%%rbx)\n\t" \ | |
341 | "addq $-8,(%%rax)\n\t" \ | |
342 | "movq (%%rax), %%rbx\n\t" \ | |
343 | "movq %%r13,(%%rbx)\n\t" \ | |
344 | "addq $-8,(%%rax)\n\t" \ | |
345 | "movq (%%rax), %%rbx\n\t" \ | |
346 | "movq %%r14,(%%rbx)\n\t" \ | |
347 | "addq $-8,(%%rax)\n\t" \ | |
348 | "movq (%%rax), %%rbx\n\t" \ | |
349 | "movq %%r15,(%%rbx)\n\t" \ | |
636ca5d6 PMF |
350 | /* push cs */ \ |
351 | "addq $-2,(%%rax)\n\t" \ | |
352 | "movq (%%rax), %%rbx\n\t" \ | |
353 | "movw %%cs, (%%rbx)\n\t" \ | |
354 | /* push ss */ \ | |
355 | "addq $-2,(%%rax)\n\t" \ | |
356 | "movq (%%rax), %%rbx\n\t" \ | |
357 | "movw %%ss, (%%rbx)\n\t" \ | |
358 | /* add padding for struct registers */ \ | |
359 | "addq $-4,(%%rax)\n\t" \ | |
360 | /* restore original values of regs that were used internally */ \ | |
361 | "popq %%rdi\n\t" \ | |
362 | "popq %%rbx\n\t" \ | |
363 | "popq %%rax\n\t" \ | |
364 | /* cancel push of rsp */ \ | |
365 | "addq $8,%%rsp\n\t" \ | |
a5850bc4 PMF |
366 | /* cancel push of rflags */ \ |
367 | "addq $8,%%rsp\n\t" \ | |
636ca5d6 PMF |
368 | ::); \ |
369 | memcpy(regsptr, (void *)ust_reg_stack_ptr, sizeof(struct registers)); \ | |
370 | ust_reg_stack_ptr = (void *)(((long)ust_reg_stack_ptr) + sizeof(struct registers)); | |
d98a01c6 | 371 | |
fc1f31ab PMF |
372 | #else /* CONFIG_UST_GDB_INTEGRATION */ |
373 | ||
fc1f31ab PMF |
374 | #define save_registers(a) |
375 | ||
e003d6ee | 376 | #endif /* CONFIG_UST_GDB_INTEGRATION */ |
defa46a7 | 377 | |
9e8f4f52 PMF |
378 | /* Macro to insert the address of a relative jump in an assembly stub, |
379 | * in a relocatable way. On x86-64, this uses a special (%rip) notation. */ | |
380 | #define RELATIVE_ADDRESS(__rel_label__) __rel_label__(%%rip) | |
381 | ||
382 | #define _ASM_PTR ".quad " | |
383 | ||
d98a01c6 PMF |
384 | #endif |
385 | ||
5af57e62 | 386 | #endif /* UST_PROCESSOR_H */ |