1 /* Copyright (C) 2009 Pierre-Marc Fournier
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.
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.
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
18 #ifndef UST_PROCESSOR_H
19 #define UST_PROCESSOR_H
24 extern __thread
long ust_reg_stack
[500];
25 extern volatile __thread
long *ust_reg_stack_ptr
;
43 #ifdef CONFIG_UST_GDB_INTEGRATION
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.
49 * Saving all registers without losing any of their values is
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.
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.
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.
67 #define save_registers(regsptr) \
69 /* save original esp */ \
71 /* push original eflags */ \
73 /* eax will hold the ptr to the private stack bottom */ \
75 /* ebx is used for TLS access */ \
77 /* ecx will be used to temporarily hold the stack bottom addr */\
79 /* rdi is the input to __tls_get_addr, and also a temp var */ \
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 \
85 * Some registers' original values have already been saved: \
86 * esp, eflags, eax, ebx, ecx, edi \
88 * In addition, the i386 ABI says the following registers belong\
89 * to the caller function: \
90 * esp, ebp, esi, edi, ebx \
92 * The following registers should not be changed by the callee: \
95 * Therefore, the following registers must be explicitly \
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" \
111 "movl %%eax,%%ecx\n\t" \
112 /* Save ecx because we are using it. */ \
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 */ \
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. */ \
125 /* edx was pushed for function calls */ \
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" \
173 "addl $-2,(%%eax)\n\t" \
174 "movl (%%eax), %%ebx\n\t" \
175 "movw %%cs, (%%ebx)\n\t" \
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 */ \
185 /* cancel push of rsp */ \
186 "addl $4,%%esp\n\t" \
187 /* cancel push of eflags */ \
188 "addl $4,%%esp\n\t" \
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));
193 #else /* CONFIG_UST_GDB_INTEGRATION */
195 #define save_registers(a)
197 #endif /* CONFIG_UST_GDB_INTEGRATION */
199 #define RELATIVE_ADDRESS(__rel_label__) __rel_label__
201 #define ARCH_COPY_ADDR(src, dst) "lea " src "," dst
203 #define _ASM_PTR ".long "
205 #else /* below is code for x86-64 */
208 int padding
; /* 4 bytes */
226 unsigned long rflags
;
230 #ifdef CONFIG_UST_GDB_INTEGRATION
232 #define save_registers(regsptr) \
234 /* save original rsp */ \
236 /* push original rflags */ \
238 /* rax will hold the ptr to the private stack bottom */ \
240 /* rbx will be used to temporarily hold the stack bottom addr */ \
242 /* rdi is the input to __tls_get_addr, and also a temp var */ \
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 \
249 * Some registers' original values have already been saved: \
250 * rsp, rflags, rax, rbx, rdi \
252 * In addition, the x86-64 ABI says the following registers \
253 * belong to the caller function: \
254 * rbp, rbx, r12, r13, r14, r15 \
256 * The following registers should not be changed by the callee: \
259 * Therefore, the following registers must be explicitly \
261 * rcx, rdx, rsi, r8, r9, r10, r11 \
270 /* Start TLS access of private reg stack pointer */ \
272 "leaq ust_reg_stack_ptr@tlsgd(%%rip), %%rdi\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" \
281 "movq %%rax,%%rbx\n\t" \
282 /* Start TLS access of private reg stack */ \
284 "leaq ust_reg_stack@tlsgd(%%rip), %%rdi\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. */ \
295 /* Pop regs that were pushed for function calls */ \
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" \
370 "addq $-2,(%%rax)\n\t" \
371 "movq (%%rax), %%rbx\n\t" \
372 "movw %%cs, (%%rbx)\n\t" \
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 */ \
383 /* cancel push of rsp */ \
384 "addq $8,%%rsp\n\t" \
385 /* cancel push of rflags */ \
386 "addq $8,%%rsp\n\t" \
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));
391 #else /* CONFIG_UST_GDB_INTEGRATION */
393 #define save_registers(a)
395 #endif /* CONFIG_UST_GDB_INTEGRATION */
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)
401 #define ARCH_COPY_ADDR(src, dst) "lea " src "(%%rip)," dst
403 #define _ASM_PTR ".quad "
407 #endif /* UST_PROCESSOR_H */
This page took 0.049221 seconds and 4 git commands to generate.