From: Arthur Chunqi Li Subject: [PATCH 1/2] kvm-unit-tests: Add a func to run instruction in emulator Date: Wed, 19 Jun 2013 23:00:56 +0800 Message-ID: <1371654057-17169-1-git-send-email-yzt356@gmail.com> Cc: gleb@redhat.com, pbonzini@redhat.com, jan.kiszka@web.de, Arthur Chunqi Li To: kvm@vger.kernel.org Return-path: Received: from mail-pb0-f49.google.com ([209.85.160.49]:59330 "EHLO mail-pb0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756767Ab3FSPBO (ORCPT ); Wed, 19 Jun 2013 11:01:14 -0400 Received: by mail-pb0-f49.google.com with SMTP id jt11so5175298pbb.36 for ; Wed, 19 Jun 2013 08:01:13 -0700 (PDT) Sender: kvm-owner@vger.kernel.org List-ID: Add a function trap_emulator to run an instruction in emulator. Set inregs first (%rax is invalid because it is used as return address), put instruction codec in alt_insn and call func with alt_insn_length. Get results in outregs. Signed-off-by: Arthur Chunqi Li --- x86/emulator.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) mode change 100644 => 100755 x86/emulator.c diff --git a/x86/emulator.c b/x86/emulator.c old mode 100644 new mode 100755 index 96576e5..48d45c8 --- a/x86/emulator.c +++ b/x86/emulator.c @@ -11,6 +11,15 @@ int fails, tests; static int exceptions; +struct regs { + u64 rax, rbx, rcx, rdx; + u64 rsi, rdi, rsp, rbp; + u64 r8, r9, r10, r11; + u64 r12, r13, r14, r15; + u64 rip, rflags; +}; +struct regs inregs, outregs, save; + void report(const char *name, int result) { ++tests; @@ -685,6 +694,107 @@ static void test_shld_shrd(u32 *mem) report("shrd (cl)", *mem == ((0x12345678 >> 3) | (5u << 29))); } +#define INSN_SAVE \ + "ret\n\t" \ + "pushf\n\t" \ + "push 136+save \n\t" \ + "popf \n\t" \ + "xchg %rax, 0+save \n\t" \ + "xchg %rbx, 8+save \n\t" \ + "xchg %rcx, 16+save \n\t" \ + "xchg %rdx, 24+save \n\t" \ + "xchg %rsi, 32+save \n\t" \ + "xchg %rdi, 40+save \n\t" \ + "xchg %rsp, 48+save \n\t" \ + "xchg %rbp, 56+save \n\t" \ + "xchg %r8, 64+save \n\t" \ + "xchg %r9, 72+save \n\t" \ + "xchg %r10, 80+save \n\t" \ + "xchg %r11, 88+save \n\t" \ + "xchg %r12, 96+save \n\t" \ + "xchg %r13, 104+save \n\t" \ + "xchg %r14, 112+save \n\t" \ + "xchg %r15, 120+save \n\t" \ + +#define INSN_RESTORE \ + "xchg %rax, 0+save \n\t" \ + "xchg %rbx, 8+save \n\t" \ + "xchg %rcx, 16+save \n\t" \ + "xchg %rdx, 24+save \n\t" \ + "xchg %rsi, 32+save \n\t" \ + "xchg %rdi, 40+save \n\t" \ + "xchg %rsp, 48+save \n\t" \ + "xchg %rbp, 56+save \n\t" \ + "xchg %r8, 64+save \n\t" \ + "xchg %r9, 72+save \n\t" \ + "xchg %r10, 80+save \n\t" \ + "xchg %r11, 88+save \n\t" \ + "xchg %r12, 96+save \n\t" \ + "xchg %r13, 104+save \n\t" \ + "xchg %r14, 112+save \n\t" \ + "xchg %r15, 120+save \n\t" \ + "pushf \n\t" \ + "pop 136+save \n\t" \ + "popf \n\t" \ + "ret \n\t" \ + +#define INSN_TRAP \ + "in (%dx),%al\n\t" \ + ". = . + 31\n\t" \ + +asm( + ".align 4096\n\t" + "insn_page:\n\t" + INSN_SAVE + "test_insn:\n\t" + INSN_TRAP + "test_insn_end:\n\t" + INSN_RESTORE + "insn_page_end:\n\t" + ".align 4096\n\t" + + "alt_insn_page:\n\t" + INSN_SAVE + "alt_test_insn:\n\t" + INSN_TRAP + "alt_test_insn_end:\n\t" + INSN_RESTORE + "alt_insn_page_end:\n\t" + ".align 4096\n\t" +); + +static void trap_emulator(uint64_t *mem, uint8_t* alt_insn, int alt_insn_length) +{ + ulong *cr3 = (ulong *)read_cr3(); + void *insn_ram; + int i; + extern u8 insn_page[], test_insn[], test_insn_end[]; + extern u8 alt_insn_page[], alt_test_insn[]; + + insn_ram = vmap(virt_to_phys(insn_page), 4096); + for (i=1; i