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