Skip to content

Commit

Permalink
Implement non-globally-atomic cmpxchg8b
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Jan 6, 2020
1 parent 8d005b1 commit 5f308a7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions emu/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ __no_instrument DECODER_RET glue(DECODER_NAME, OP_SIZE)(DECODER_ARGS) {
case 0xc1: TRACEI("xadd reg, modrm");
READMODRM; XADD(modrm_reg, modrm_val,oz); break;

case 0xc7: READMODRM_MEM; switch (modrm.opcode) {
case 1: TRACEI("cmpxchg8b modrm");
CMPXCHG8B(modrm_val,64); break;
default: UNDEFINED;
};

#if OP_SIZE != 16
case 0xc8: TRACEI("bswap eax");
BSWAP(reg_a); break;
Expand Down
26 changes: 26 additions & 0 deletions jit/gadgets-aarch64/misc.S
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@
gret 1
write_bullshit 64, atomic_cmpxchg8b

.gadget cmpxchg8b
write_prep 64, cmpxchg8b
mov w9, eax
bfi x9, xdx, 32, 32
mov w10, ebx
bfi x10, xcx, 32, 32

ldr x8, [_xaddr]
cmp x9, x8
csel x9, x8, x9, ne
csel x8, x10, x8, eq
cset w11, eq
str x8, [_xaddr]
write_done 64, cmpxchg8b
ubfx xax, x9, 0, 32
ubfx xdx, x9, 32, 32

ldr w8, [_cpu, CPU_flags_res]
ldr w9, [_cpu, CPU_eflags]
and w8, w8, ~ZF_RES
bfi w9, w11, 6, 1
str w8, [_cpu, CPU_flags_res]
str w9, [_cpu, CPU_eflags]
gret 1
write_bullshit 64, cmpxchg8b

.macro do_helper type, size=
.gadget helper_\type\size
.ifin(\type, read,write)
Expand Down
11 changes: 11 additions & 0 deletions jit/gadgets-x86_64/misc.S
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@
orb %r15b, CPU_eflags(%_cpu)
gret 1

.gadget cmpxchg8b
write_prep 64, cmpxchg8b
cmpxchg8b (%_addrq)
setz %r15b
write_done 64, cmpxchg8b
andl $~ZF_RES, CPU_flags_res(%_cpu)
andl $~ZF_FLAG, CPU_eflags(%_cpu)
shlb $6, %r15b
orb %r15b, CPU_eflags(%_cpu)
gret 1

.macro do_helper type, size=
.gadget helper_\type\size
.ifin(\type, read,write)
Expand Down
1 change: 1 addition & 0 deletions jit/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ static inline bool gen_op(struct gen_state *state, gadget_t *gadgets, enum arg a
#define REPNZ(op, z) strop(op, repnz, z)

#define CMPXCHG(src, dst,z) load(src, z); op(cmpxchg, dst, z)
#define CMPXCHG8B(dst,z) g_addr(); gg(cmpxchg8b, saved_ip)
#define XADD(src, dst,z) XCHG(src, dst,z); ADD(src, dst,z)

void helper_rdtsc(struct cpu_state *cpu);
Expand Down

0 comments on commit 5f308a7

Please sign in to comment.