save registers: fix small problems
[ust.git] / include / ust / processor.h
CommitLineData
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
7extern __thread long ust_reg_stack[500];
8extern volatile __thread long *ust_reg_stack_ptr;
9
d98a01c6
PMF
10
11#ifdef X86_32
12
13struct registers {
14 long eax;
15 long ebx;
16 long ecx;
17 long edx;
18 long ebp;
19 long esp;
20 long esi;
21 long edi;
22 int xds;
23 int xes;
24 int xfs;
25 int xgs;
26 long eip;
27 int xcs;
28 long eflags;
29 int xss;
30};
31
32static inline save_registers(struct registers *regs)
33{
34}
35
9e8f4f52
PMF
36#define RELATIVE_ADDRESS(__rel_label__) __rel_label__
37
38#define _ASM_PTR ".long "
39
d98a01c6
PMF
40#else
41
42struct registers {
636ca5d6
PMF
43 int padding; /* 4 bytes */
44 short ss;
45 short cs;
636ca5d6
PMF
46 unsigned long r15;
47 unsigned long r14;
48 unsigned long r13;
49 unsigned long r12;
50 unsigned long r11;
51 unsigned long r10;
52 unsigned long r9;
53 unsigned long r8;
d98a01c6 54 unsigned long rsi;
636ca5d6
PMF
55 unsigned long rbp;
56 unsigned long rdx;
57 unsigned long rcx;
d98a01c6 58 unsigned long rdi;
636ca5d6
PMF
59 unsigned long rbx;
60 unsigned long rax;
a5850bc4 61 unsigned long rflags;
636ca5d6 62 unsigned long rsp;
d98a01c6
PMF
63};
64
8524c98d 65#define save_registers(regsptr) \
636ca5d6
PMF
66 asm volatile ( \
67 /* save original rsp */ \
68 "pushq %%rsp\n\t" \
a5850bc4
PMF
69 /* push original rflags */ \
70 "pushfq\n\t" \
636ca5d6
PMF
71 /* rax will hold the ptr to the private stack bottom */ \
72 "pushq %%rax\n\t" \
73 /* rbx will be used to temporarily hold the stack bottom addr */ \
74 "pushq %%rbx\n\t" \
75 /* rdi is the input to __tls_get_addr, and also a temp var */ \
76 "pushq %%rdi\n\t" \
a5850bc4 77 /* Start TLS access of private reg stack pointer */ \
636ca5d6
PMF
78 ".byte 0x66\n\t" \
79 "leaq ust_reg_stack_ptr@tlsgd(%%rip), %%rdi\n\t" \
80 ".word 0x6666\n\t" \
81 "rex64\n\t" \
82 "call __tls_get_addr@plt\n\t" \
83 /* --- End TLS access */ \
a5850bc4
PMF
84 /* check if ust_reg_stack_ptr has been initialized */ \
85 "movq (%%rax),%%rbx\n\t" \
86 "testq %%rbx,%%rbx\n\t" \
87 "jne 1f\n\t" \
88 "movq %%rax,%%rbx\n\t" \
89 /* Start TLS access of private reg stack */ \
90 ".byte 0x66\n\t" \
91 "leaq ust_reg_stack@tlsgd(%%rip), %%rdi\n\t" \
92 ".word 0x6666\n\t" \
93 "rex64\n\t" \
94 "call __tls_get_addr@plt\n\t" \
95 /* --- End TLS access */ \
96 "addq $500,%%rax\n\t" \
97 "movq %%rax,(%%rbx)\n\t" \
98 "movq %%rbx,%%rax\n\t" \
99 /* now the pointer to the private stack is in rax.
100 must add stack size so the ptr points to the stack bottom. */ \
101 "1:\n\t" \
636ca5d6
PMF
102 /* Manually push rsp to private stack */ \
103 "addq $-8,(%%rax)\n\t" \
a5850bc4
PMF
104 "movq 32(%%rsp), %%rdi\n\t" \
105 "movq (%%rax), %%rbx\n\t" \
106 "movq %%rdi, (%%rbx)\n\t" \
107 /* Manually push eflags to private stack */ \
108 "addq $-8,(%%rax)\n\t" \
636ca5d6
PMF
109 "movq 24(%%rsp), %%rdi\n\t" \
110 "movq (%%rax), %%rbx\n\t" \
111 "movq %%rdi, (%%rbx)\n\t" \
112 /* Manually push rax to private stack */ \
113 "addq $-8,(%%rax)\n\t" \
114 "movq 16(%%rsp), %%rdi\n\t" \
115 "movq (%%rax), %%rbx\n\t" \
116 "movq %%rdi, (%%rbx)\n\t" \
117 /* Manually push rbx to private stack */ \
118 "addq $-8,(%%rax)\n\t" \
119 "movq 8(%%rsp), %%rdi\n\t" \
120 "movq (%%rax), %%rbx\n\t" \
121 "movq %%rdi, (%%rbx)\n\t" \
122 /* Manually push rdi to private stack */ \
123 "addq $-8,(%%rax)\n\t" \
124 "movq 0(%%rsp), %%rdi\n\t" \
125 "movq (%%rax), %%rbx\n\t" \
126 "movq %%rdi, (%%rbx)\n\t" \
127 /* now push regs to tls */ \
128 /* -- rsp already pushed -- */ \
129 /* -- rax already pushed -- */ \
130 /* -- rbx already pushed -- */ \
131 /* -- rdi already pushed -- */ \
132 "addq $-8,(%%rax)\n\t" \
133 "movq (%%rax), %%rbx\n\t" \
134 "movq %%rcx,(%%rbx)\n\t" \
135 "addq $-8,(%%rax)\n\t" \
136 "movq (%%rax), %%rbx\n\t" \
137 "movq %%rdx,(%%rbx)\n\t" \
138 "addq $-8,(%%rax)\n\t" \
139 "movq (%%rax), %%rbx\n\t" \
140 "movq %%rbp,(%%rbx)\n\t" \
141 "addq $-8,(%%rax)\n\t" \
142 "movq (%%rax), %%rbx\n\t" \
143 "movq %%rsi,(%%rbx)\n\t" \
144 "addq $-8,(%%rax)\n\t" \
145 "movq (%%rax), %%rbx\n\t" \
146 "movq %%r8,(%%rbx)\n\t" \
147 "addq $-8,(%%rax)\n\t" \
148 "movq (%%rax), %%rbx\n\t" \
149 "movq %%r9,(%%rbx)\n\t" \
150 "addq $-8,(%%rax)\n\t" \
151 "movq (%%rax), %%rbx\n\t" \
152 "movq %%r10,(%%rbx)\n\t" \
153 "addq $-8,(%%rax)\n\t" \
154 "movq (%%rax), %%rbx\n\t" \
155 "movq %%r11,(%%rbx)\n\t" \
156 "addq $-8,(%%rax)\n\t" \
157 "movq (%%rax), %%rbx\n\t" \
158 "movq %%r12,(%%rbx)\n\t" \
159 "addq $-8,(%%rax)\n\t" \
160 "movq (%%rax), %%rbx\n\t" \
161 "movq %%r13,(%%rbx)\n\t" \
162 "addq $-8,(%%rax)\n\t" \
163 "movq (%%rax), %%rbx\n\t" \
164 "movq %%r14,(%%rbx)\n\t" \
165 "addq $-8,(%%rax)\n\t" \
166 "movq (%%rax), %%rbx\n\t" \
167 "movq %%r15,(%%rbx)\n\t" \
636ca5d6
PMF
168 /* push cs */ \
169 "addq $-2,(%%rax)\n\t" \
170 "movq (%%rax), %%rbx\n\t" \
171 "movw %%cs, (%%rbx)\n\t" \
172 /* push ss */ \
173 "addq $-2,(%%rax)\n\t" \
174 "movq (%%rax), %%rbx\n\t" \
175 "movw %%ss, (%%rbx)\n\t" \
176 /* add padding for struct registers */ \
177 "addq $-4,(%%rax)\n\t" \
178 /* restore original values of regs that were used internally */ \
179 "popq %%rdi\n\t" \
180 "popq %%rbx\n\t" \
181 "popq %%rax\n\t" \
182 /* cancel push of rsp */ \
183 "addq $8,%%rsp\n\t" \
a5850bc4
PMF
184 /* cancel push of rflags */ \
185 "addq $8,%%rsp\n\t" \
636ca5d6
PMF
186 ::); \
187 memcpy(regsptr, (void *)ust_reg_stack_ptr, sizeof(struct registers)); \
188 ust_reg_stack_ptr = (void *)(((long)ust_reg_stack_ptr) + sizeof(struct registers));
d98a01c6 189
9e8f4f52
PMF
190/* Macro to insert the address of a relative jump in an assembly stub,
191 * in a relocatable way. On x86-64, this uses a special (%rip) notation. */
192#define RELATIVE_ADDRESS(__rel_label__) __rel_label__(%%rip)
193
194#define _ASM_PTR ".quad "
195
d98a01c6
PMF
196#endif
197
5af57e62 198#endif /* UST_PROCESSOR_H */
This page took 0.030173 seconds and 4 git commands to generate.