save registers: fix small problems
[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
11 #ifdef X86_32
12
13 struct 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
32 static inline save_registers(struct registers *regs)
33 {
34 }
35
36 #define RELATIVE_ADDRESS(__rel_label__) __rel_label__
37
38 #define _ASM_PTR ".long "
39
40 #else
41
42 struct registers {
43 int padding; /* 4 bytes */
44 short ss;
45 short cs;
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;
54 unsigned long rsi;
55 unsigned long rbp;
56 unsigned long rdx;
57 unsigned long rcx;
58 unsigned long rdi;
59 unsigned long rbx;
60 unsigned long rax;
61 unsigned long rflags;
62 unsigned long rsp;
63 };
64
65 #define save_registers(regsptr) \
66 asm volatile ( \
67 /* save original rsp */ \
68 "pushq %%rsp\n\t" \
69 /* push original rflags */ \
70 "pushfq\n\t" \
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" \
77 /* Start TLS access of private reg stack pointer */ \
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 */ \
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" \
102 /* Manually push rsp to private stack */ \
103 "addq $-8,(%%rax)\n\t" \
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" \
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" \
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" \
184 /* cancel push of rflags */ \
185 "addq $8,%%rsp\n\t" \
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));
189
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
196 #endif
197
198 #endif /* UST_PROCESSOR_H */
This page took 0.03419 seconds and 5 git commands to generate.