|
| 1 | +#include <unicorn/unicorn.h> |
| 2 | +#include <asm/ptrace.h> |
| 3 | +#include <asm/page.h> |
| 4 | + |
| 5 | +#include "unicorn.h" |
| 6 | + |
| 7 | +#include <emu/exec.h> |
| 8 | +#include <emu/kernel.h> |
| 9 | +#include <emu/emu.h> |
| 10 | + |
| 11 | +#define check(err) __check(err, __FUNCTION__, __LINE__) |
| 12 | +static void __check(uc_err err, const char *function, int line) |
| 13 | +{ |
| 14 | + if (err != UC_ERR_OK) { |
| 15 | + panic("%s:%d: %s", function, line, uc_strerror(err)); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +#define uc_reg(op, uc, reg_id, field) \ |
| 20 | + check(uc_reg_##op##2((uc), (reg_id), (field), (size_t[]){sizeof(*(field))})) |
| 21 | + |
| 22 | +////////////////////// |
| 23 | + |
| 24 | +struct gdt_desc { |
| 25 | + unsigned limit1:16; |
| 26 | + unsigned base1:16; |
| 27 | + unsigned base2:8; |
| 28 | + unsigned type:4; |
| 29 | + unsigned system:1; |
| 30 | + unsigned dpl:2; |
| 31 | + unsigned present:1; |
| 32 | + unsigned limit2:4; |
| 33 | + unsigned avl:1; |
| 34 | + unsigned _:1; |
| 35 | + unsigned db:1; |
| 36 | + unsigned granularity:1; |
| 37 | + unsigned base3:8; |
| 38 | +} __attribute__((packed)); |
| 39 | + |
| 40 | +static const uint32_t gdt_base = 0xfffff000; |
| 41 | +static const uint32_t gdt_size = 16 * sizeof(struct gdt_desc); |
| 42 | + |
| 43 | +static void install_gdt_segment(uc_engine *uc, int segment, uint32_t base, int dpl) |
| 44 | +{ |
| 45 | + struct gdt_desc desc = { |
| 46 | + .limit1 = 0xffff, |
| 47 | + .limit2 = 0xf, |
| 48 | + .base1 = (base & 0x0000ffff) >> 0, |
| 49 | + .base2 = (base & 0x00ff0000) >> 16, |
| 50 | + .base3 = (base & 0xff000000) >> 24, |
| 51 | + .type = 3, // read & write |
| 52 | + .system = 1, // user |
| 53 | + .dpl = dpl, |
| 54 | + .present = 1, |
| 55 | + .db = 1, // 32 bit code |
| 56 | + .granularity = 1, |
| 57 | + }; |
| 58 | + check(uc_mem_write(uc, gdt_base + (segment * sizeof(desc)), &desc, sizeof(desc))); |
| 59 | +} |
| 60 | + |
| 61 | +/////////////////// |
| 62 | + |
| 63 | +static void load_regs(struct emu *emu) |
| 64 | +{ |
| 65 | + uc_engine *uc = emu->uc; |
| 66 | + struct pt_regs *regs = emu->regs; |
| 67 | + uc_reg(write, uc, UC_X86_REG_EAX, ®s->ax); |
| 68 | + uc_reg(write, uc, UC_X86_REG_EBX, ®s->bx); |
| 69 | + uc_reg(write, uc, UC_X86_REG_ECX, ®s->cx); |
| 70 | + uc_reg(write, uc, UC_X86_REG_EDX, ®s->dx); |
| 71 | + uc_reg(write, uc, UC_X86_REG_ESI, ®s->si); |
| 72 | + uc_reg(write, uc, UC_X86_REG_EDI, ®s->di); |
| 73 | + uc_reg(write, uc, UC_X86_REG_EBP, ®s->bp); |
| 74 | + uc_reg(write, uc, UC_X86_REG_ESP, ®s->sp); |
| 75 | + uc_reg(write, uc, UC_X86_REG_EIP, ®s->ip); |
| 76 | + uc_reg(write, uc, UC_X86_REG_FLAGS, ®s->flags); |
| 77 | + |
| 78 | + if (emu->tls_ptr != regs->tls) { |
| 79 | + emu->tls_ptr = regs->tls; |
| 80 | + install_gdt_segment(uc, 0xc, emu->tls_ptr, 3); |
| 81 | + } |
| 82 | + unsigned long mm_change_count = __atomic_load_n(&emu->mm->change_count, __ATOMIC_SEQ_CST); |
| 83 | + if (emu->mm_change_count != mm_change_count) { |
| 84 | + check(uc_ctl_flush_tlb(uc)); |
| 85 | + emu->mm_change_count = mm_change_count; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +static void save_regs(struct emu *emu) |
| 90 | +{ |
| 91 | + uc_engine *uc = emu->uc; |
| 92 | + struct pt_regs *regs = emu->regs; |
| 93 | + regs->ax = 0; |
| 94 | + uc_reg(read, uc, UC_X86_REG_EAX, ®s->ax); |
| 95 | + uc_reg(read, uc, UC_X86_REG_EBX, ®s->bx); |
| 96 | + uc_reg(read, uc, UC_X86_REG_ECX, ®s->cx); |
| 97 | + uc_reg(read, uc, UC_X86_REG_EDX, ®s->dx); |
| 98 | + uc_reg(read, uc, UC_X86_REG_ESI, ®s->si); |
| 99 | + uc_reg(read, uc, UC_X86_REG_EDI, ®s->di); |
| 100 | + uc_reg(read, uc, UC_X86_REG_EBP, ®s->bp); |
| 101 | + uc_reg(read, uc, UC_X86_REG_ESP, ®s->sp); |
| 102 | + uc_reg(read, uc, UC_X86_REG_EIP, ®s->ip); |
| 103 | + uc_reg(read, uc, UC_X86_REG_FLAGS, ®s->flags); |
| 104 | +} |
| 105 | + |
| 106 | +static void do_trap(struct emu *emu, int trap_nr) |
| 107 | +{ |
| 108 | + save_regs(emu); |
| 109 | + emu->regs->trap_nr = trap_nr; |
| 110 | + handle_cpu_trap(); |
| 111 | + load_regs(emu); |
| 112 | +} |
| 113 | + |
| 114 | +//////////////// |
| 115 | + |
| 116 | +static bool mem_type_is_write(uc_mem_type type) |
| 117 | +{ |
| 118 | + switch (type) { |
| 119 | + case UC_MEM_READ: |
| 120 | + case UC_MEM_READ_UNMAPPED: |
| 121 | + case UC_MEM_READ_PROT: |
| 122 | + case UC_MEM_READ_AFTER: |
| 123 | + case UC_MEM_FETCH: |
| 124 | + case UC_MEM_FETCH_UNMAPPED: |
| 125 | + case UC_MEM_FETCH_PROT: |
| 126 | + return false; |
| 127 | + case UC_MEM_WRITE: |
| 128 | + case UC_MEM_WRITE_UNMAPPED: |
| 129 | + case UC_MEM_WRITE_PROT: |
| 130 | + return true; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +static bool hook_tlb_fill(uc_engine *uc, uint64_t vaddr, uc_mem_type type, uc_tlb_entry *result, void *user_data) |
| 135 | +{ |
| 136 | + struct emu *emu = user_data; |
| 137 | + |
| 138 | + if (vaddr == gdt_base) { |
| 139 | + result->paddr = vaddr; |
| 140 | + result->perms = UC_PROT_READ | UC_PROT_WRITE; |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + bool is_write = mem_type_is_write(type); |
| 145 | + bool writable; |
| 146 | + void *kernel_addr = user_to_kernel_emu(emu->mm, vaddr, &writable); |
| 147 | + |
| 148 | + if (kernel_addr == NULL || (is_write && !writable)) { |
| 149 | + emu->regs->cr2 = vaddr; |
| 150 | + emu->regs->error_code = mem_type_is_write(type) ? 2 : 0; |
| 151 | + do_trap(emu, 13); |
| 152 | + kernel_addr = user_to_kernel_emu(emu->mm, vaddr, &writable); |
| 153 | + } |
| 154 | + if (kernel_addr == NULL) { |
| 155 | + uc_emu_stop(uc); |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + unsigned long paddr = __pa(kernel_addr); |
| 160 | + result->paddr = __pa(kernel_addr); |
| 161 | + result->perms = UC_PROT_READ | UC_PROT_EXEC | (writable ? UC_PROT_WRITE : 0); |
| 162 | + return true; |
| 163 | +} |
| 164 | + |
| 165 | +static void hook_intr(uc_engine *uc, uint32_t intno, void *user_data) |
| 166 | +{ |
| 167 | + struct emu *emu = user_data; |
| 168 | + do_trap(emu, intno); |
| 169 | +} |
| 170 | + |
| 171 | +static bool hook_trace_code(uc_engine *uc, uint64_t address, size_t size, void *user_data) |
| 172 | +{ |
| 173 | + struct emu *emu = user_data; |
| 174 | + void *ptr = user_to_kernel_emu(emu->mm, address, NULL); |
| 175 | + uint32_t sp; |
| 176 | + uc_reg(read, uc, UC_X86_REG_ESP, &sp); |
| 177 | + extern int current_pid(); |
| 178 | + printk("%d code %#llx+%ld %*ph\n", current_pid(), address, size, (int) size, ptr); |
| 179 | + return true; |
| 180 | +} |
| 181 | + |
| 182 | +///////////// |
| 183 | + |
| 184 | +static void create_unicorn(struct emu *emu) |
| 185 | +{ |
| 186 | + uc_hook hh; |
| 187 | + check(uc_open(UC_ARCH_X86, UC_MODE_32, &emu->uc)); |
| 188 | + |
| 189 | + check(uc_mem_map_ptr(emu->uc, 0x0, ish_phys_size, UC_PROT_READ | UC_PROT_WRITE | UC_PROT_EXEC, (void *) ish_phys_base)); |
| 190 | + check(uc_ctl_tlb_mode(emu->uc, UC_TLB_VIRTUAL)); |
| 191 | + check(uc_hook_add(emu->uc, &hh, UC_HOOK_TLB_FILL, hook_tlb_fill, emu, 1, 0)); |
| 192 | + |
| 193 | + check(uc_mem_map(emu->uc, gdt_base, 0x1000, UC_PROT_READ | UC_PROT_WRITE)); |
| 194 | + struct uc_x86_mmr gdtr = {.base = gdt_base, .limit = gdt_size}; |
| 195 | + uc_reg(write, emu->uc, UC_X86_REG_GDTR, &gdtr); |
| 196 | + // unicorn bug (maybe): if you load any segment register other than ss, sp suddenly becomes 16 bit. can be fixed by loading ss correctly |
| 197 | + install_gdt_segment(emu->uc, 1, 0, 0); |
| 198 | + int seg = (1 << 3) | 0; // ring 0? why? |
| 199 | + uc_reg(write, emu->uc, UC_X86_REG_SS, &seg); |
| 200 | + |
| 201 | + check(uc_hook_add(emu->uc, &hh, UC_HOOK_INTR, hook_intr, emu, 1, 0)); |
| 202 | + |
| 203 | + if (unicorn_trace) { |
| 204 | + check(uc_hook_add(emu->uc, &hh, UC_HOOK_BLOCK, hook_trace_code, emu, 1, 0)); |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +void emu_run(struct emu *emu, struct pt_regs *regs) |
| 209 | +{ |
| 210 | + if (!emu->uc) { |
| 211 | + create_unicorn(emu); |
| 212 | + } |
| 213 | + |
| 214 | + emu->regs = regs; |
| 215 | + |
| 216 | + load_regs(emu); |
| 217 | + |
| 218 | + for (;;) { |
| 219 | + check(uc_emu_start(emu->uc, regs->ip, 0, 0, 0)); |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +void emu_finish_fork(struct emu *emu, struct emu *next) |
| 224 | +{ |
| 225 | + if (emu->uc == NULL) { |
| 226 | + return; |
| 227 | + } |
| 228 | + |
| 229 | + create_unicorn(next); |
| 230 | + |
| 231 | + uint8_t gdt_buf[gdt_size]; |
| 232 | + check(uc_mem_read(emu->uc, gdt_base, gdt_buf, gdt_size)); |
| 233 | + check(uc_mem_write(next->uc, gdt_base, gdt_buf, gdt_size)); |
| 234 | + |
| 235 | + uc_context *ctx; |
| 236 | + check(uc_context_alloc(emu->uc, &ctx)); |
| 237 | + check(uc_context_save(emu->uc, ctx)); |
| 238 | + check(uc_context_restore(next->uc, ctx)); |
| 239 | + check(uc_context_free(ctx)); |
| 240 | +} |
| 241 | + |
| 242 | +void emu_destroy(struct emu *emu) |
| 243 | +{ |
| 244 | + check(uc_close(emu->uc)); |
| 245 | + emu->uc = NULL; |
| 246 | +} |
| 247 | + |
| 248 | +void emu_poke_cpu(int cpu) {} |
| 249 | + |
| 250 | +void emu_mmu_init(struct emu_mm *mm) |
| 251 | +{ |
| 252 | +} |
| 253 | +void emu_mmu_destroy(struct emu_mm *mm) |
| 254 | +{ |
| 255 | +} |
| 256 | +void emu_switch_mm(struct emu *emu, struct emu_mm *mm) |
| 257 | +{ |
| 258 | + emu->mm = mm; |
| 259 | +} |
| 260 | +void emu_flush_tlb_local(struct emu_mm *mm, unsigned long start, unsigned long end) |
| 261 | +{ |
| 262 | + __atomic_fetch_add(&mm->change_count, 1, __ATOMIC_SEQ_CST); |
| 263 | +} |
0 commit comments