| 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 rflags; |
| 47 | unsigned long r15; |
| 48 | unsigned long r14; |
| 49 | unsigned long r13; |
| 50 | unsigned long r12; |
| 51 | unsigned long r11; |
| 52 | unsigned long r10; |
| 53 | unsigned long r9; |
| 54 | unsigned long r8; |
| 55 | unsigned long rsi; |
| 56 | unsigned long rbp; |
| 57 | unsigned long rdx; |
| 58 | unsigned long rcx; |
| 59 | unsigned long rdi; |
| 60 | unsigned long rbx; |
| 61 | unsigned long rax; |
| 62 | unsigned long rsp; |
| 63 | }; |
| 64 | |
| 65 | #define save_registers(regsptr) \ |
| 66 | if(ust_reg_stack_ptr == NULL) { \ |
| 67 | ust_reg_stack_ptr = (long*)((long)ust_reg_stack)+500; \ |
| 68 | } \ |
| 69 | asm volatile ( \ |
| 70 | /* save original rsp */ \ |
| 71 | "pushq %%rsp\n\t" \ |
| 72 | /* rax will hold the ptr to the private stack bottom */ \ |
| 73 | "pushq %%rax\n\t" \ |
| 74 | /* rbx will be used to temporarily hold the stack bottom addr */ \ |
| 75 | "pushq %%rbx\n\t" \ |
| 76 | /* rdi is the input to __tls_get_addr, and also a temp var */ \ |
| 77 | "pushq %%rdi\n\t" \ |
| 78 | /* Start TLS access of private reg stack */ \ |
| 79 | ".byte 0x66\n\t" \ |
| 80 | "leaq ust_reg_stack_ptr@tlsgd(%%rip), %%rdi\n\t" \ |
| 81 | ".word 0x6666\n\t" \ |
| 82 | "rex64\n\t" \ |
| 83 | "call __tls_get_addr@plt\n\t" \ |
| 84 | /* --- End TLS access */ \ |
| 85 | /* Manually push rsp to private stack */ \ |
| 86 | "addq $-8,(%%rax)\n\t" \ |
| 87 | "movq 24(%%rsp), %%rdi\n\t" \ |
| 88 | "movq (%%rax), %%rbx\n\t" \ |
| 89 | "movq %%rdi, (%%rbx)\n\t" \ |
| 90 | /* Manually push rax to private stack */ \ |
| 91 | "addq $-8,(%%rax)\n\t" \ |
| 92 | "movq 16(%%rsp), %%rdi\n\t" \ |
| 93 | "movq (%%rax), %%rbx\n\t" \ |
| 94 | "movq %%rdi, (%%rbx)\n\t" \ |
| 95 | /* Manually push rbx to private stack */ \ |
| 96 | "addq $-8,(%%rax)\n\t" \ |
| 97 | "movq 8(%%rsp), %%rdi\n\t" \ |
| 98 | "movq (%%rax), %%rbx\n\t" \ |
| 99 | "movq %%rdi, (%%rbx)\n\t" \ |
| 100 | /* Manually push rdi to private stack */ \ |
| 101 | "addq $-8,(%%rax)\n\t" \ |
| 102 | "movq 0(%%rsp), %%rdi\n\t" \ |
| 103 | "movq (%%rax), %%rbx\n\t" \ |
| 104 | "movq %%rdi, (%%rbx)\n\t" \ |
| 105 | /* now push regs to tls */ \ |
| 106 | /* -- rsp already pushed -- */ \ |
| 107 | /* -- rax already pushed -- */ \ |
| 108 | /* -- rbx already pushed -- */ \ |
| 109 | /* -- rdi already pushed -- */ \ |
| 110 | "addq $-8,(%%rax)\n\t" \ |
| 111 | "movq (%%rax), %%rbx\n\t" \ |
| 112 | "movq %%rcx,(%%rbx)\n\t" \ |
| 113 | "addq $-8,(%%rax)\n\t" \ |
| 114 | "movq (%%rax), %%rbx\n\t" \ |
| 115 | "movq %%rdx,(%%rbx)\n\t" \ |
| 116 | "addq $-8,(%%rax)\n\t" \ |
| 117 | "movq (%%rax), %%rbx\n\t" \ |
| 118 | "movq %%rbp,(%%rbx)\n\t" \ |
| 119 | "addq $-8,(%%rax)\n\t" \ |
| 120 | "movq (%%rax), %%rbx\n\t" \ |
| 121 | "movq %%rsi,(%%rbx)\n\t" \ |
| 122 | "addq $-8,(%%rax)\n\t" \ |
| 123 | "movq (%%rax), %%rbx\n\t" \ |
| 124 | "movq %%r8,(%%rbx)\n\t" \ |
| 125 | "addq $-8,(%%rax)\n\t" \ |
| 126 | "movq (%%rax), %%rbx\n\t" \ |
| 127 | "movq %%r9,(%%rbx)\n\t" \ |
| 128 | "addq $-8,(%%rax)\n\t" \ |
| 129 | "movq (%%rax), %%rbx\n\t" \ |
| 130 | "movq %%r10,(%%rbx)\n\t" \ |
| 131 | "addq $-8,(%%rax)\n\t" \ |
| 132 | "movq (%%rax), %%rbx\n\t" \ |
| 133 | "movq %%r11,(%%rbx)\n\t" \ |
| 134 | "addq $-8,(%%rax)\n\t" \ |
| 135 | "movq (%%rax), %%rbx\n\t" \ |
| 136 | "movq %%r12,(%%rbx)\n\t" \ |
| 137 | "addq $-8,(%%rax)\n\t" \ |
| 138 | "movq (%%rax), %%rbx\n\t" \ |
| 139 | "movq %%r13,(%%rbx)\n\t" \ |
| 140 | "addq $-8,(%%rax)\n\t" \ |
| 141 | "movq (%%rax), %%rbx\n\t" \ |
| 142 | "movq %%r14,(%%rbx)\n\t" \ |
| 143 | "addq $-8,(%%rax)\n\t" \ |
| 144 | "movq (%%rax), %%rbx\n\t" \ |
| 145 | "movq %%r15,(%%rbx)\n\t" \ |
| 146 | /* deal with rflags */ \ |
| 147 | "pushfq\n\t" /* push rflags on stack */ \ |
| 148 | "addq $-8,(%%rax)\n\t" \ |
| 149 | "movq (%%rax), %%rbx\n\t" \ |
| 150 | "popq (%%rbx)\n\t" \ |
| 151 | /* push cs */ \ |
| 152 | "addq $-2,(%%rax)\n\t" \ |
| 153 | "movq (%%rax), %%rbx\n\t" \ |
| 154 | "movw %%cs, (%%rbx)\n\t" \ |
| 155 | /* push ss */ \ |
| 156 | "addq $-2,(%%rax)\n\t" \ |
| 157 | "movq (%%rax), %%rbx\n\t" \ |
| 158 | "movw %%ss, (%%rbx)\n\t" \ |
| 159 | /* add padding for struct registers */ \ |
| 160 | "addq $-4,(%%rax)\n\t" \ |
| 161 | /* restore original values of regs that were used internally */ \ |
| 162 | "popq %%rdi\n\t" \ |
| 163 | "popq %%rbx\n\t" \ |
| 164 | "popq %%rax\n\t" \ |
| 165 | /* cancel push of rsp */ \ |
| 166 | "addq $8,%%rsp\n\t" \ |
| 167 | ::); \ |
| 168 | memcpy(regsptr, (void *)ust_reg_stack_ptr, sizeof(struct registers)); \ |
| 169 | ust_reg_stack_ptr = (void *)(((long)ust_reg_stack_ptr) + sizeof(struct registers)); |
| 170 | |
| 171 | /* Macro to insert the address of a relative jump in an assembly stub, |
| 172 | * in a relocatable way. On x86-64, this uses a special (%rip) notation. */ |
| 173 | #define RELATIVE_ADDRESS(__rel_label__) __rel_label__(%%rip) |
| 174 | |
| 175 | #define _ASM_PTR ".quad " |
| 176 | |
| 177 | #endif |
| 178 | |
| 179 | #endif /* UST_PROCESSOR_H */ |