From: Peter Feiner <pfeiner@google.com>
To: kvm@vger.kernel.org, drjones@redhat.com, pbonzini@redhat.com
Cc: pfeiner@google.com
Subject: [kvm-unit-tests v2 1/8] x86: emulator: asm fixes
Date: Wed, 2 Mar 2016 17:09:31 -0800 [thread overview]
Message-ID: <1456967378-6367-2-git-send-email-pfeiner@google.com> (raw)
In-Reply-To: <1456967378-6367-1-git-send-email-pfeiner@google.com>
Found several problems in the emulator test when trying to compile
with various combinations of -fno-omit-frame-pointer and -O{0,1}.
Fixed register clobbering in several tests. It's not good enough to
save & restore a register with I/O operands because the compiler can
use the register to effect the I/O! Fixing %rbp was tricky because it
can't be listed in the clobber list when -fno-omit-frame-pointer is in
effect; thus %rbp has to be saved manually using a scratch register
that's declared as clobbered.
The length of the jmp instruction in test_jmp_noncanonical was 2, not
3. This patch makes the exception handler more robust by using the
computed instruction length.
Signed-off-by: Peter Feiner <pfeiner@google.com>
---
x86/emulator.c | 60 +++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 36 insertions(+), 24 deletions(-)
diff --git a/x86/emulator.c b/x86/emulator.c
index e5c1c6b..d990961 100644
--- a/x86/emulator.c
+++ b/x86/emulator.c
@@ -260,24 +260,30 @@ void test_pop(void *mem)
report("ret", 1);
stack_top[-1] = 0x778899;
- asm volatile("mov %%rsp, %[tmp] \n\t"
- "mov %%rbp, %[tmp3] \n\t"
- "mov %[stack_top], %%rbp \n\t"
+ asm volatile("mov %[stack_top], %%r8 \n\t"
+ "mov %%rsp, %%r9 \n\t"
+ "xchg %%rbp, %%r8 \n\t"
"leave \n\t"
- "xchg %%rsp, %[tmp] \n\t"
- "xchg %%rbp, %[tmp3]"
+ "xchg %%rsp, %%r9 \n\t"
+ "xchg %%rbp, %%r8 \n\t"
+ "mov %%r9, %[tmp] \n\t"
+ "mov %%r8, %[tmp3]"
: [tmp]"=&r"(tmp), [tmp3]"=&r"(tmp3) : [stack_top]"r"(stack_top-1)
- : "memory");
+ : "memory", "r8", "r9");
report("leave", tmp == (ulong)stack_top && tmp3 == 0x778899);
rbp = 0xaa55aa55bb66bb66ULL;
rsp = (unsigned long)stack_top;
- asm volatile("xchg %%rsp, %[rsp] \n\t"
- "xchg %%rbp, %[rbp] \n\t"
+ asm volatile("mov %[rsp], %%r8 \n\t"
+ "mov %[rbp], %%r9 \n\t"
+ "xchg %%rsp, %%r8 \n\t"
+ "xchg %%rbp, %%r9 \n\t"
"enter $0x1238, $0 \n\t"
- "xchg %%rsp, %[rsp] \n\t"
- "xchg %%rbp, %[rbp]"
- : [rsp]"+a"(rsp), [rbp]"+b"(rbp) : : "memory");
+ "xchg %%rsp, %%r8 \n\t"
+ "xchg %%rbp, %%r9 \n\t"
+ "xchg %%r8, %[rsp] \n\t"
+ "xchg %%r9, %[rbp]"
+ : [rsp]"+a"(rsp), [rbp]"+b"(rbp) : : "memory", "r8", "r9");
report("enter",
rsp == (unsigned long)stack_top - 8 - 0x1238
&& rbp == (unsigned long)stack_top - 8
@@ -405,7 +411,7 @@ void test_xchg(void *mem)
"mov %%rax, %[rax]\n\t"
: [rax]"=r"(rax)
: [memq]"r"(memq)
- : "memory");
+ : "memory", "rax");
report("xchg reg, r/m (1)",
rax == 0xfedcba98765432ef && *memq == 0x123456789abcd10);
@@ -416,7 +422,7 @@ void test_xchg(void *mem)
"mov %%rax, %[rax]\n\t"
: [rax]"=r"(rax)
: [memq]"r"(memq)
- : "memory");
+ : "memory", "rax");
report("xchg reg, r/m (2)",
rax == 0xfedcba987654cdef && *memq == 0x123456789ab3210);
@@ -427,7 +433,7 @@ void test_xchg(void *mem)
"mov %%rax, %[rax]\n\t"
: [rax]"=r"(rax)
: [memq]"r"(memq)
- : "memory");
+ : "memory", "rax");
report("xchg reg, r/m (3)",
rax == 0x89abcdef && *memq == 0x123456776543210);
@@ -438,7 +444,7 @@ void test_xchg(void *mem)
"mov %%rax, %[rax]\n\t"
: [rax]"=r"(rax)
: [memq]"r"(memq)
- : "memory");
+ : "memory", "rax");
report("xchg reg, r/m (4)",
rax == 0x123456789abcdef && *memq == 0xfedcba9876543210);
}
@@ -455,7 +461,7 @@ void test_xadd(void *mem)
"mov %%rax, %[rax]\n\t"
: [rax]"=r"(rax)
: [memq]"r"(memq)
- : "memory");
+ : "memory", "rax");
report("xadd reg, r/m (1)",
rax == 0xfedcba98765432ef && *memq == 0x123456789abcdff);
@@ -466,7 +472,7 @@ void test_xadd(void *mem)
"mov %%rax, %[rax]\n\t"
: [rax]"=r"(rax)
: [memq]"r"(memq)
- : "memory");
+ : "memory", "rax");
report("xadd reg, r/m (2)",
rax == 0xfedcba987654cdef && *memq == 0x123456789abffff);
@@ -477,7 +483,7 @@ void test_xadd(void *mem)
"mov %%rax, %[rax]\n\t"
: [rax]"=r"(rax)
: [memq]"r"(memq)
- : "memory");
+ : "memory", "rax");
report("xadd reg, r/m (3)",
rax == 0x89abcdef && *memq == 0x1234567ffffffff);
@@ -488,7 +494,7 @@ void test_xadd(void *mem)
"mov %%rax, %[rax]\n\t"
: [rax]"=r"(rax)
: [memq]"r"(memq)
- : "memory");
+ : "memory", "rax");
report("xadd reg, r/m (4)",
rax == 0x123456789abcdef && *memq == 0xffffffffffffffff);
}
@@ -804,10 +810,12 @@ static void trap_emulator(uint64_t *mem, void *alt_insn_page,
outregs = save;
}
-static void advance_rip_by_3_and_note_exception(struct ex_regs *regs)
+static unsigned long rip_advance;
+
+static void advance_rip_and_note_exception(struct ex_regs *regs)
{
++exceptions;
- regs->rip += 3;
+ regs->rip += rip_advance;
}
static void test_mmx_movq_mf(uint64_t *mem, uint8_t *insn_page,
@@ -819,11 +827,12 @@ static void test_mmx_movq_mf(uint64_t *mem, uint8_t *insn_page,
write_cr0(read_cr0() & ~6); /* TS, EM */
exceptions = 0;
- handle_exception(MF_VECTOR, advance_rip_by_3_and_note_exception);
+ handle_exception(MF_VECTOR, advance_rip_and_note_exception);
asm volatile("fninit; fldcw %0" : : "m"(fcw));
asm volatile("fldz; fldz; fdivp"); /* generate exception */
MK_INSN(mmx_movq_mf, "movq %mm0, (%rax) \n\t");
+ rip_advance = insn_mmx_movq_mf.len;
inregs = (struct regs){ .rsp=(u64)stack+1024 };
trap_emulator(mem, alt_insn_page, &insn_mmx_movq_mf);
/* exit MMX mode */
@@ -834,11 +843,14 @@ static void test_mmx_movq_mf(uint64_t *mem, uint8_t *insn_page,
static void test_jmp_noncanonical(uint64_t *mem)
{
+ extern char nc_jmp_start, nc_jmp_end;
+
*mem = 0x1111111111111111ul;
exceptions = 0;
- handle_exception(GP_VECTOR, advance_rip_by_3_and_note_exception);
- asm volatile ("jmp *%0" : : "m"(*mem));
+ rip_advance = &nc_jmp_end - &nc_jmp_start;
+ handle_exception(GP_VECTOR, advance_rip_and_note_exception);
+ asm volatile ("nc_jmp_start: jmp *%0; nc_jmp_end:" : : "m"(*mem));
report("jump to non-canonical address", exceptions == 1);
handle_exception(GP_VECTOR, 0);
}
--
2.7.0.rc3.207.g0ac5344
next prev parent reply other threads:[~2016-03-03 1:09 UTC|newest]
Thread overview: 65+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-01 21:27 [kvm-unit-tests 0/5] Debugging aids Peter Feiner
2016-03-01 21:27 ` [kvm-unit-tests 1/5] lib: print failing assert cond Peter Feiner
2016-03-02 15:04 ` Andrew Jones
2016-03-01 21:27 ` [kvm-unit-tests 2/5] lib: backtrace printing Peter Feiner
2016-03-01 22:58 ` Peter Feiner
2016-03-01 23:07 ` Peter Feiner
2016-03-01 21:27 ` [kvm-unit-tests 3/5] x86: lib: debug dump on unhandled exceptions Peter Feiner
2016-03-01 21:27 ` [kvm-unit-tests 4/5] lib: dump stack on abort() Peter Feiner
2016-03-01 21:29 ` Peter Feiner
2016-03-01 21:27 ` [kvm-unit-tests 5/5] scripts: pretty print stack traces Peter Feiner
2016-03-01 21:34 ` Paolo Bonzini
2016-03-03 9:35 ` Andrew Jones
2016-03-03 12:57 ` Paolo Bonzini
2016-03-03 13:38 ` Andrew Jones
2016-03-03 1:09 ` [kvm-unit-tests v2 0/8] Debugging aids Peter Feiner
2016-03-03 1:09 ` Peter Feiner [this message]
2016-03-03 1:09 ` [kvm-unit-tests v2 2/8] x86: emulator: disable test_lldt Peter Feiner
2016-03-03 1:09 ` [kvm-unit-tests v2 3/8] x86: realmode: fix test_sgdt_sidt overflow Peter Feiner
2016-03-03 1:09 ` [kvm-unit-tests v2 4/8] x86: eventinj: make test work with -O0 Peter Feiner
2016-03-03 12:53 ` Paolo Bonzini
2016-03-03 1:09 ` [kvm-unit-tests v2 5/8] lib: backtrace printing Peter Feiner
2016-03-03 9:17 ` Andrew Jones
2016-03-03 17:01 ` Peter Feiner
2016-03-03 17:56 ` Andrew Jones
2016-03-03 1:09 ` [kvm-unit-tests v2 6/8] x86: lib: debug dump on unhandled exceptions Peter Feiner
2016-03-03 1:09 ` [kvm-unit-tests v2 7/8] lib: dump stack on abort() Peter Feiner
2016-03-03 9:19 ` Andrew Jones
2016-03-03 1:09 ` [kvm-unit-tests v2 8/8] scripts: pretty print stack traces Peter Feiner
2016-03-03 9:54 ` Andrew Jones
2016-03-03 12:58 ` [kvm-unit-tests v2 0/8] Debugging aids Paolo Bonzini
2016-03-03 20:48 ` [kvm-unit-tests v3 0/4] " Peter Feiner
2016-03-03 20:48 ` [kvm-unit-tests v3 1/4] lib: backtrace printing Peter Feiner
2016-03-04 10:15 ` Andrew Jones
2016-03-03 20:48 ` [kvm-unit-tests v3 2/4] x86: lib: debug dump on unhandled exceptions Peter Feiner
2016-03-03 20:48 ` [kvm-unit-tests v3 3/4] lib: dump stack on failed assert() Peter Feiner
2016-03-04 10:25 ` Andrew Jones
2016-03-03 20:48 ` [kvm-unit-tests v3 4/4] scripts: pretty print stack traces Peter Feiner
2016-03-04 10:24 ` Andrew Jones
2016-03-04 16:55 ` Peter Feiner
2016-03-04 18:43 ` Andrew Jones
2016-03-04 19:33 ` [PATCH kvm-unit-tests v4 0/6] Debugging aids Peter Feiner
2016-03-04 19:33 ` [PATCH kvm-unit-tests v4 1/5] lib: backtrace printing Peter Feiner
2016-03-04 19:33 ` [PATCH kvm-unit-tests v4 2/5] x86: lib: debug dump on unhandled exceptions Peter Feiner
2016-03-04 19:33 ` [PATCH kvm-unit-tests v4 3/5] lib: dump stack on failed assert() Peter Feiner
2016-03-04 19:34 ` [PATCH kvm-unit-tests v4 4/5] scripts: pretty print stack traces Peter Feiner
2016-03-04 19:34 ` [PATCH kvm-unit-tests v4 5/5] scripts: automatically pretty print stacks Peter Feiner
2016-03-05 11:29 ` Andrew Jones
2016-03-07 17:48 ` Peter Feiner
2016-03-04 19:37 ` [PATCH kvm-unit-tests v4 0/6] Debugging aids Peter Feiner
2016-03-07 17:46 ` [PATCH kvm-unit-tests v5 0/5] " Peter Feiner
2016-03-07 17:46 ` [PATCH kvm-unit-tests v5 1/5] lib: backtrace printing Peter Feiner
2016-03-08 4:24 ` Andrew Jones
2016-03-11 0:31 ` Peter Feiner
2016-03-07 17:46 ` [PATCH kvm-unit-tests v5 2/5] x86: lib: debug dump on unhandled exceptions Peter Feiner
2016-03-07 17:46 ` [PATCH kvm-unit-tests v5 3/5] lib: dump stack on failed assert() Peter Feiner
2016-03-07 17:46 ` [PATCH kvm-unit-tests v5 4/5] scripts: pretty print stack traces Peter Feiner
2016-03-07 17:46 ` [PATCH kvm-unit-tests v5 5/5] scripts: automatically pretty print stacks Peter Feiner
2016-03-08 4:31 ` [PATCH kvm-unit-tests v5 0/5] Debugging aids Andrew Jones
2016-03-11 0:47 ` [PATCH kvm-unit-tests v6 " Peter Feiner
2016-03-11 0:47 ` [PATCH kvm-unit-tests v6 1/5] lib: backtrace printing Peter Feiner
2016-03-11 0:47 ` [PATCH kvm-unit-tests v6 2/5] x86: lib: debug dump on unhandled exceptions Peter Feiner
2016-03-11 0:47 ` [PATCH kvm-unit-tests v6 3/5] lib: dump stack on failed assert() Peter Feiner
2016-03-11 0:47 ` [PATCH kvm-unit-tests v6 4/5] scripts: pretty print stack traces Peter Feiner
2016-03-11 0:47 ` [PATCH kvm-unit-tests v6 5/5] scripts: automatically pretty print stacks Peter Feiner
2016-03-11 2:41 ` [PATCH kvm-unit-tests v6 0/5] Debugging aids Andrew Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1456967378-6367-2-git-send-email-pfeiner@google.com \
--to=pfeiner@google.com \
--cc=drjones@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.