first take on the save ip on x86-32
[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 ecx;
19 long edi;
20 long ebx;
21 long eax;
22 long eflags;
23 long esp;
24 };
25
26 #ifdef CONFIG_UST_GDB_INTEGRATION
27
28 //#error "GDB integration not supported for x86-32 yet."
29
30 #define save_registers(regsptr) \
31 asm volatile ( \
32 /* save original esp */ \
33 "pushl %%esp\n\t" \
34 /* push original eflags */ \
35 "pushfl\n\t" \
36 /* eax will hold the ptr to the private stack bottom */ \
37 "pushl %%eax\n\t" \
38 /* ebx will be used to temporarily hold the stack bottom addr */ \
39 "pushl %%ebx\n\t" \
40 /* rdi is the input to __tls_get_addr, and also a temp var */ \
41 "pushl %%edi\n\t" \
42 /* Start TLS access of private reg stack pointer */ \
43 "leal ust_reg_stack_ptr@tlsgd(,%%ebx,1),%%eax\n\t" \
44 "call ___tls_get_addr@plt\n\t" \
45 /* --- End TLS access */ \
46 /* check if ust_reg_stack_ptr has been initialized */ \
47 "movl (%%eax),%%ebx\n\t" \
48 "testl %%ebx,%%ebx\n\t" \
49 "jne 1f\n\t" \
50 "movl %%eax,%%ebx\n\t" \
51 /* Start TLS access of private reg stack */ \
52 "leal ust_reg_stack@tlsgd(,%%ebx,1),%%eax\n\t" \
53 "call ___tls_get_addr@plt\n\t" \
54 /* --- End TLS access */ \
55 "addl $500,%%eax\n\t" \
56 "movl %%eax,(%%ebx)\n\t" \
57 "movl %%ebx,%%eax\n\t" \
58 /* now the pointer to the private stack is in eax. \
59 must add stack size so the ptr points to the stack bottom. */ \
60 "1:\n\t" \
61 /* Manually push esp to private stack */ \
62 "addl $-4,(%%eax)\n\t" \
63 "movl 16(%%esp), %%edi\n\t" \
64 "movl (%%eax), %%ebx\n\t" \
65 "movl %%edi, (%%ebx)\n\t" \
66 /* Manually push eflags to private stack */ \
67 "addl $-4,(%%eax)\n\t" \
68 "movl 12(%%esp), %%edi\n\t" \
69 "movl (%%eax), %%ebx\n\t" \
70 "movl %%edi, (%%ebx)\n\t" \
71 /* Manually push eax to private stack */ \
72 "addl $-4,(%%eax)\n\t" \
73 "movl 8(%%esp), %%edi\n\t" \
74 "movl (%%eax), %%ebx\n\t" \
75 "movl %%edi, (%%ebx)\n\t" \
76 /* Manually push ebx to private stack */ \
77 "addl $-4,(%%eax)\n\t" \
78 "movl 4(%%esp), %%edi\n\t" \
79 "movl (%%eax), %%ebx\n\t" \
80 "movl %%edi, (%%ebx)\n\t" \
81 /* Manually push edi to private stack */ \
82 "addl $-4,(%%eax)\n\t" \
83 "movl 0(%%esp), %%edi\n\t" \
84 "movl (%%eax), %%ebx\n\t" \
85 "movl %%edi, (%%ebx)\n\t" \
86 /* now push regs to tls */ \
87 /* -- esp already pushed -- */ \
88 /* -- eax already pushed -- */ \
89 /* -- ebx already pushed -- */ \
90 /* -- edi already pushed -- */ \
91 "addl $-4,(%%eax)\n\t" \
92 "movl (%%eax), %%ebx\n\t" \
93 "movl %%ecx,(%%ebx)\n\t" \
94 "addl $-4,(%%eax)\n\t" \
95 "movl (%%eax), %%ebx\n\t" \
96 "movl %%edx,(%%ebx)\n\t" \
97 "addl $-4,(%%eax)\n\t" \
98 "movl (%%eax), %%ebx\n\t" \
99 "movl %%ebp,(%%ebx)\n\t" \
100 "addl $-4,(%%eax)\n\t" \
101 "movl (%%eax), %%ebx\n\t" \
102 "movl %%esi,(%%ebx)\n\t" \
103 /* push cs */ \
104 "addl $-2,(%%eax)\n\t" \
105 "movl (%%eax), %%ebx\n\t" \
106 "movw %%cs, (%%ebx)\n\t" \
107 /* push ss */ \
108 "addl $-2,(%%eax)\n\t" \
109 "movl (%%eax), %%ebx\n\t" \
110 "movw %%ss, (%%ebx)\n\t" \
111 /* restore original values of regs that were used internally */ \
112 "popl %%edi\n\t" \
113 "popl %%ebx\n\t" \
114 "popl %%eax\n\t" \
115 /* cancel push of rsp */ \
116 "addl $4,%%esp\n\t" \
117 /* cancel push of eflags */ \
118 "addl $4,%%esp\n\t" \
119 ::: "memory"); \
120 memcpy(regsptr, (void *)ust_reg_stack_ptr, sizeof(struct registers)); \
121 ust_reg_stack_ptr = (void *)(((long)ust_reg_stack_ptr) + sizeof(struct registers));
122
123 #define save_ip(channel,name)
124
125 #else /* CONFIG_UST_GDB_INTEGRATION */
126
127 #define save_registers(a)
128
129 #endif /* CONFIG_UST_GDB_INTEGRATION */
130
131 #define RELATIVE_ADDRESS(__rel_label__) __rel_label__
132
133 #define _ASM_PTR ".long "
134
135 #else /* below is code for x86-64 */
136
137 struct registers {
138 int padding; /* 4 bytes */
139 short ss;
140 short cs;
141 unsigned long r15;
142 unsigned long r14;
143 unsigned long r13;
144 unsigned long r12;
145 unsigned long r11;
146 unsigned long r10;
147 unsigned long r9;
148 unsigned long r8;
149 unsigned long rsi;
150 unsigned long rbp;
151 unsigned long rdx;
152 unsigned long rcx;
153 unsigned long rdi;
154 unsigned long rbx;
155 unsigned long rax;
156 unsigned long rflags;
157 unsigned long rsp;
158 };
159
160 #ifdef CONFIG_UST_GDB_INTEGRATION
161
162 #define save_registers(regsptr) \
163 asm volatile ( \
164 /* save original rsp */ \
165 "pushq %%rsp\n\t" \
166 /* push original rflags */ \
167 "pushfq\n\t" \
168 /* rax will hold the ptr to the private stack bottom */ \
169 "pushq %%rax\n\t" \
170 /* rbx will be used to temporarily hold the stack bottom addr */ \
171 "pushq %%rbx\n\t" \
172 /* rdi is the input to __tls_get_addr, and also a temp var */ \
173 "pushq %%rdi\n\t" \
174 /* Start TLS access of private reg stack pointer */ \
175 ".byte 0x66\n\t" \
176 "leaq ust_reg_stack_ptr@tlsgd(%%rip), %%rdi\n\t" \
177 ".word 0x6666\n\t" \
178 "rex64\n\t" \
179 "call __tls_get_addr@plt\n\t" \
180 /* --- End TLS access */ \
181 /* check if ust_reg_stack_ptr has been initialized */ \
182 "movq (%%rax),%%rbx\n\t" \
183 "testq %%rbx,%%rbx\n\t" \
184 "jne 1f\n\t" \
185 "movq %%rax,%%rbx\n\t" \
186 /* Start TLS access of private reg stack */ \
187 ".byte 0x66\n\t" \
188 "leaq ust_reg_stack@tlsgd(%%rip), %%rdi\n\t" \
189 ".word 0x6666\n\t" \
190 "rex64\n\t" \
191 "call __tls_get_addr@plt\n\t" \
192 /* --- End TLS access */ \
193 "addq $500,%%rax\n\t" \
194 "movq %%rax,(%%rbx)\n\t" \
195 "movq %%rbx,%%rax\n\t" \
196 /* now the pointer to the private stack is in rax.
197 must add stack size so the ptr points to the stack bottom. */ \
198 "1:\n\t" \
199 /* Manually push rsp to private stack */ \
200 "addq $-8,(%%rax)\n\t" \
201 "movq 32(%%rsp), %%rdi\n\t" \
202 "movq (%%rax), %%rbx\n\t" \
203 "movq %%rdi, (%%rbx)\n\t" \
204 /* Manually push eflags to private stack */ \
205 "addq $-8,(%%rax)\n\t" \
206 "movq 24(%%rsp), %%rdi\n\t" \
207 "movq (%%rax), %%rbx\n\t" \
208 "movq %%rdi, (%%rbx)\n\t" \
209 /* Manually push rax to private stack */ \
210 "addq $-8,(%%rax)\n\t" \
211 "movq 16(%%rsp), %%rdi\n\t" \
212 "movq (%%rax), %%rbx\n\t" \
213 "movq %%rdi, (%%rbx)\n\t" \
214 /* Manually push rbx to private stack */ \
215 "addq $-8,(%%rax)\n\t" \
216 "movq 8(%%rsp), %%rdi\n\t" \
217 "movq (%%rax), %%rbx\n\t" \
218 "movq %%rdi, (%%rbx)\n\t" \
219 /* Manually push rdi to private stack */ \
220 "addq $-8,(%%rax)\n\t" \
221 "movq 0(%%rsp), %%rdi\n\t" \
222 "movq (%%rax), %%rbx\n\t" \
223 "movq %%rdi, (%%rbx)\n\t" \
224 /* now push regs to tls */ \
225 /* -- rsp already pushed -- */ \
226 /* -- rax already pushed -- */ \
227 /* -- rbx already pushed -- */ \
228 /* -- rdi already pushed -- */ \
229 "addq $-8,(%%rax)\n\t" \
230 "movq (%%rax), %%rbx\n\t" \
231 "movq %%rcx,(%%rbx)\n\t" \
232 "addq $-8,(%%rax)\n\t" \
233 "movq (%%rax), %%rbx\n\t" \
234 "movq %%rdx,(%%rbx)\n\t" \
235 "addq $-8,(%%rax)\n\t" \
236 "movq (%%rax), %%rbx\n\t" \
237 "movq %%rbp,(%%rbx)\n\t" \
238 "addq $-8,(%%rax)\n\t" \
239 "movq (%%rax), %%rbx\n\t" \
240 "movq %%rsi,(%%rbx)\n\t" \
241 "addq $-8,(%%rax)\n\t" \
242 "movq (%%rax), %%rbx\n\t" \
243 "movq %%r8,(%%rbx)\n\t" \
244 "addq $-8,(%%rax)\n\t" \
245 "movq (%%rax), %%rbx\n\t" \
246 "movq %%r9,(%%rbx)\n\t" \
247 "addq $-8,(%%rax)\n\t" \
248 "movq (%%rax), %%rbx\n\t" \
249 "movq %%r10,(%%rbx)\n\t" \
250 "addq $-8,(%%rax)\n\t" \
251 "movq (%%rax), %%rbx\n\t" \
252 "movq %%r11,(%%rbx)\n\t" \
253 "addq $-8,(%%rax)\n\t" \
254 "movq (%%rax), %%rbx\n\t" \
255 "movq %%r12,(%%rbx)\n\t" \
256 "addq $-8,(%%rax)\n\t" \
257 "movq (%%rax), %%rbx\n\t" \
258 "movq %%r13,(%%rbx)\n\t" \
259 "addq $-8,(%%rax)\n\t" \
260 "movq (%%rax), %%rbx\n\t" \
261 "movq %%r14,(%%rbx)\n\t" \
262 "addq $-8,(%%rax)\n\t" \
263 "movq (%%rax), %%rbx\n\t" \
264 "movq %%r15,(%%rbx)\n\t" \
265 /* push cs */ \
266 "addq $-2,(%%rax)\n\t" \
267 "movq (%%rax), %%rbx\n\t" \
268 "movw %%cs, (%%rbx)\n\t" \
269 /* push ss */ \
270 "addq $-2,(%%rax)\n\t" \
271 "movq (%%rax), %%rbx\n\t" \
272 "movw %%ss, (%%rbx)\n\t" \
273 /* add padding for struct registers */ \
274 "addq $-4,(%%rax)\n\t" \
275 /* restore original values of regs that were used internally */ \
276 "popq %%rdi\n\t" \
277 "popq %%rbx\n\t" \
278 "popq %%rax\n\t" \
279 /* cancel push of rsp */ \
280 "addq $8,%%rsp\n\t" \
281 /* cancel push of rflags */ \
282 "addq $8,%%rsp\n\t" \
283 ::); \
284 memcpy(regsptr, (void *)ust_reg_stack_ptr, sizeof(struct registers)); \
285 ust_reg_stack_ptr = (void *)(((long)ust_reg_stack_ptr) + sizeof(struct registers));
286
287 #else /* CONFIG_UST_GDB_INTEGRATION */
288
289 #define save_registers(a)
290
291 #endif /* CONFIG_UST_GDB_INTEGRATION */
292
293 /* Macro to insert the address of a relative jump in an assembly stub,
294 * in a relocatable way. On x86-64, this uses a special (%rip) notation. */
295 #define RELATIVE_ADDRESS(__rel_label__) __rel_label__(%%rip)
296
297 #define _ASM_PTR ".quad "
298
299 #endif
300
301 #endif /* UST_PROCESSOR_H */
This page took 0.036388 seconds and 5 git commands to generate.