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