Skip to content

Commit

Permalink
Implement some limited flag handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed May 13, 2018
1 parent 816ed11 commit 7a3a99a
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 57 deletions.
18 changes: 14 additions & 4 deletions emu/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,20 @@ struct cpu_state {
// whether the true flag values are in the above struct, or computed from
// the stored result and operands
dword_t res, op1, op2;
bits pf_res:1;
bits zf_res:1;
bits sf_res:1;
bits af_ops:1;
union {
struct {
bits pf_res:1;
bits zf_res:1;
bits sf_res:1;
bits af_ops:1;
};
// for asm
#define PF_RES (1 << 0)
#define ZF_RES (1 << 1)
#define SF_RES (1 << 2)
#define AF_OPS (1 << 3)
byte_t flags_res;
};

// fpu
extFloat80_t fp[8];
Expand Down
13 changes: 13 additions & 0 deletions emu/gadgets-x86/math.S
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@
movl %_tmp, \arg
.else
\op\()l \arg, %_tmp
.irp da_op, add,sub; .ifc \op,\da_op
seto CPU_of(%_cpu)
setc CPU_cf(%_cpu)
movl \arg, %r14d
movl %r14d, CPU_op1(%_cpu)
movl %_tmp, CPU_op2(%_cpu)
orl $AF_OPS, CPU_flags_res(%_cpu)
.endif; .endr
movl %_tmp, CPU_res(%_cpu)
orl $(PF_RES|ZF_RES|SF_RES), CPU_flags_res(%_cpu)
.endif; .endif
.endm

.macro oc_flags
.endm

.irp op, load,store,add,and,sub,xor
.macro x reg
do_op \op, %\reg
Expand Down
20 changes: 0 additions & 20 deletions emu/gadgets-x86/offsets.c

This file was deleted.

14 changes: 8 additions & 6 deletions emu/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ static inline void gen_op(struct gen_state *state, gadget_t *gadgets, enum arg a

#define load(thing) op(load, thing)
#define store(thing) op(store, thing)
#define oop(o, src, dst) load(dst); op(o, src); store(dst)
// load-op-store
#define los(o, src, dst) load(dst); op(o, src); store(dst)
#define lo(o, src, dst) load(dst); op(o, src)

#define DECLARE_LOCALS \
dword_t addr_offset = 0;
Expand All @@ -97,14 +99,14 @@ static inline void gen_op(struct gen_state *state, gadget_t *gadgets, enum arg a
#define MOVSX(src, dst,zs,zd) UNDEFINED
#define XCHG(src, dst,z) UNDEFINED

#define ADD(src, dst,z) oop(add, src, dst)
#define ADD(src, dst,z) los(add, src, dst)
#define OR(src, dst,z) UNDEFINED
#define ADC(src, dst,z) UNDEFINED
#define SBB(src, dst,z) UNDEFINED
#define AND(src, dst,z) oop(and, src, dst)
#define SUB(src, dst,z) oop(sub, src, dst)
#define XOR(src, dst,z) oop(xor, src, dst)
#define CMP(src, dst,z) UNDEFINED
#define AND(src, dst,z) los(and, src, dst)
#define SUB(src, dst,z) los(sub, src, dst)
#define XOR(src, dst,z) los(xor, src, dst)
#define CMP(src, dst,z) lo(sub, src, dst)
#define TEST(src, dst,z) UNDEFINED
#define NOT(val,z) UNDEFINED
#define NEG(val,z) UNDEFINED
Expand Down
57 changes: 34 additions & 23 deletions emu/offsets.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,40 @@
#include "emu/jit.h"

void cpu() {
OFFSET(CPU_eax, struct cpu_state, eax);
OFFSET(CPU_ebx, struct cpu_state, ebx);
OFFSET(CPU_ecx, struct cpu_state, ecx);
OFFSET(CPU_edx, struct cpu_state, edx);
OFFSET(CPU_esi, struct cpu_state, esi);
OFFSET(CPU_edi, struct cpu_state, edi);
OFFSET(CPU_ebp, struct cpu_state, ebp);
OFFSET(CPU_esp, struct cpu_state, esp);
OFFSET(CPU_ax, struct cpu_state, ax);
OFFSET(CPU_bx, struct cpu_state, bx);
OFFSET(CPU_cx, struct cpu_state, cx);
OFFSET(CPU_dx, struct cpu_state, dx);
OFFSET(CPU_si, struct cpu_state, si);
OFFSET(CPU_di, struct cpu_state, di);
OFFSET(CPU_bp, struct cpu_state, bp);
OFFSET(CPU_sp, struct cpu_state, sp);
OFFSET(CPU_eip, struct cpu_state, eip);
OFFSET(CPU, cpu_state, eax);
OFFSET(CPU, cpu_state, ebx);
OFFSET(CPU, cpu_state, ecx);
OFFSET(CPU, cpu_state, edx);
OFFSET(CPU, cpu_state, esi);
OFFSET(CPU, cpu_state, edi);
OFFSET(CPU, cpu_state, ebp);
OFFSET(CPU, cpu_state, esp);
OFFSET(CPU, cpu_state, ax);
OFFSET(CPU, cpu_state, bx);
OFFSET(CPU, cpu_state, cx);
OFFSET(CPU, cpu_state, dx);
OFFSET(CPU, cpu_state, si);
OFFSET(CPU, cpu_state, di);
OFFSET(CPU, cpu_state, bp);
OFFSET(CPU, cpu_state, sp);
OFFSET(CPU, cpu_state, eip);

OFFSET(JIT_BLOCK_code, struct jit_block, code);
OFFSET(CPU, cpu_state, of);
OFFSET(CPU, cpu_state, cf);
OFFSET(CPU, cpu_state, res);
OFFSET(CPU, cpu_state, op1);
OFFSET(CPU, cpu_state, op2);
OFFSET(CPU, cpu_state, flags_res);
MACRO(PF_RES);
MACRO(ZF_RES);
MACRO(SF_RES);
MACRO(AF_OPS);

OFFSET(TLB_entries, struct tlb, entries);
OFFSET(TLB_dirty_page, struct tlb, dirty_page);
OFFSET(TLB_ENTRY_page, struct tlb_entry, page);
OFFSET(TLB_ENTRY_page_if_writable, struct tlb_entry, page_if_writable);
OFFSET(TLB_ENTRY_data_minus_addr, struct tlb_entry, data_minus_addr);
OFFSET(JIT_BLOCK, jit_block, code);

OFFSET(TLB, tlb, entries);
OFFSET(TLB, tlb, dirty_page);
OFFSET(TLB_ENTRY, tlb_entry, page);
OFFSET(TLB_ENTRY, tlb_entry, page_if_writable);
OFFSET(TLB_ENTRY, tlb_entry, data_minus_addr);
}
9 changes: 9 additions & 0 deletions ish-gdb.gdb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ if subprocess.call('ninja') != 0:
raise gdb.CommandError('compilation failed')
end
end

define hook-stop
python
if gdb.selected_frame().find_sal().symtab.filename.endswith('.S'):
gdb.execute('set disassemble-next-line on')
else:
gdb.execute('set disassemble-next-line auto')
end
end
2 changes: 0 additions & 2 deletions tools/ptraceomatic.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ static int compare_cpus(struct cpu_state *cpu, struct tlb *tlb, int pid, int und
CHECK_REG(rsp, esp);
CHECK_REG(rbp, ebp);
CHECK_REG(rip, eip);
#if 0
undefined_flags |= (1 << 8); // treat trap flag as undefined
regs.eflags = (regs.eflags & ~undefined_flags) | (cpu->eflags & undefined_flags);
// give a nice visual representation of the flags
Expand All @@ -91,7 +90,6 @@ static int compare_cpus(struct cpu_state *cpu, struct tlb *tlb, int pid, int und
debugger;
return -1;
}
#endif
#define CHECK_XMMREG(i) \
CHECK(*(uint64_t *) &fpregs.xmm_space[i * 2], cpu->xmm[i].qw[0], "xmm" #i " low") \
CHECK(*(uint64_t *) &fpregs.xmm_space[(i+1)*2], cpu->xmm[i].qw[1], "xmm" #i " high")
Expand Down
9 changes: 7 additions & 2 deletions tools/staticdefine.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// credit goes to include/linux/kbuild.h
#define _DEFINE(sym, val) \
asm volatile("\n.ascii \"->" sym " %0 " #val "\"" : : "i" (val))
#define DEFINE(sym, val) \
asm volatile("\n.ascii \"->" #sym " %0 " #val "\"" : : "i" (val))
_DEFINE(#sym, val)

#define BLANK() asm volatile("\n.ascii \"->\"" : : )

#define OFFSET(sym, str, mem) \
DEFINE(sym, offsetof(str, mem))
DEFINE(sym##_##mem, offsetof(struct str, mem))

#define MACRO(macro) \
_DEFINE(#macro, macro)

#define COMMENT(x) \
asm volatile("\n.ascii \"->#" x "\"")

0 comments on commit 7a3a99a

Please sign in to comment.