All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Radim Krčmář" <rkrcmar@redhat.com>
To: kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>, Bandan Das <bsd@redhat.com>,
	Nadav Amit <nadav.amit@gmail.com>
Subject: [kvm-unit-tests PATCH] x86: realmode: add FXSR tests
Date: Wed,  2 Nov 2016 20:53:20 +0100	[thread overview]
Message-ID: <20161102195320.31671-1-rkrcmar@redhat.com> (raw)

Even unrestricted_guest will fail fxrstor when the host and guest don't
match deprecation of CS and DS in CPUID, which is the current behavior
at least on Haswell (and not Sandy Bridge).

I have added a fixup to detect other errors -- the second fxrstor should
pass.

Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
---
 x86/realmode.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)

diff --git a/x86/realmode.c b/x86/realmode.c
index 64116543ad72..02e1bb4df908 100644
--- a/x86/realmode.c
+++ b/x86/realmode.c
@@ -26,6 +26,20 @@ static int strlen(const char *str)
 	return n;
 }
 
+static void memset(char *mem, char byte, unsigned size)
+{
+	while (size--)
+		*mem++ = byte;
+}
+
+static int memcmp(const char *s1, const char *s2, unsigned size)
+{
+	while (size--)
+		if (*s1++ != *s2++)
+			return *--s1 - *--s2;
+	return 0;
+}
+
 static void outb(u8 data, u16 port)
 {
 	asm volatile("out %0, %1" : : "a"(data), "d"(port));
@@ -1645,6 +1659,89 @@ static void test_perf_memory_rmw(void)
 	print_serial(" cycles/emulated memory RMW instruction\n");
 }
 
+/* I have not tested that AMD also zeroes reserved bytes. */
+static char fx_expect[512] = {
+	0x7f, 0x1f, 0x7f, 0x7f, 0xff, 0x00, 0xff, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+};
+static char fx_buffer[512] __attribute__((aligned(16)));
+
+void fxsr_expect_deprecated(void) {
+	fx_expect[12] = 0;
+	fx_expect[13] = 0;
+	fx_expect[20] = 0;
+	fx_expect[21] = 0;
+}
+
+static void test_fxsr(void)
+{
+	int eax, ebx, ecx, edx;
+	int res;
+
+	MK_INSN(set_osfxsr, "movl %cr4, %eax\n"
+	                    "orl $0x200, %eax\n"
+	                    "movl %eax, %cr4\n");
+	MK_INSN(fxsave, "fxsave (%edi)");
+	MK_INSN(fxrstor, "fxrstor (%edi)");
+
+	/* check FXSR */
+	eax = 1; ecx = 0;
+	asm("cpuid" : "+a"(eax), "=b"(ebx), "+c"(ecx), "=d"(edx));
+
+	if (!(edx & (1 << 24)))
+		return;
+
+	/* Set OSFXSR to force save/restore of XMM registers 0-7. */
+	exec_in_big_real_mode(&insn_set_osfxsr);
+	report("set cr4.osfxsr", R_AX, 1);
+
+	inregs.edi = (u32)&fx_buffer;
+
+	fx_buffer[5] = 0xaa;
+	exec_in_big_real_mode(&insn_fxsave);
+	report("fxsave", 0, fx_buffer[5] == 0);
+
+	memset(fx_buffer, 0xff, 512);
+	/* Upper half of MXCSR must be 0. */
+	fx_buffer[26] = 0;
+	fx_buffer[27] = 0;
+
+	exec_in_big_real_mode(&insn_fxrstor);
+
+	/* Taint the buffer to detect changes. */
+	memset(fx_buffer, 0xee, 512);
+	/* fx_expect has first 160 bytes manually filled.
+	 * XMM registers 0-7 should be restored. */
+	memset(fx_expect + 160, 0xff, 8 * 16);
+	/* The rest should remain untouched. */
+	memset(fx_expect + 288, 0xee, 512 - 288);
+
+	/* Deprecated CS and DS are not saved. */
+	eax = 7; ecx = 0;
+	asm("cpuid" : "+a"(eax), "=b"(ebx), "+c"(ecx), "=d"(edx));
+	if (ebx & (1 << 13))
+		fxsr_expect_deprecated();
+
+	exec_in_big_real_mode(&insn_fxsave);
+
+	res = memcmp(fx_buffer, fx_expect, sizeof(fx_expect));
+	report("fxrstor", 0, !res);
+
+	if (res) {
+		fxsr_expect_deprecated();
+		res = memcmp(fx_buffer, fx_expect, sizeof(fx_expect));
+		report("fxrstor with deprecated fixup", 0, !res);
+	}
+}
+
 void test_dr_mod(void)
 {
 	MK_INSN(drmod, "movl %ebx, %dr0\n\t"
@@ -1726,6 +1823,7 @@ void realmode_start(void)
 	test_smsw();
 	test_nopl();
 	test_xadd();
+	test_fxsr();
 	test_perf_loop();
 	test_perf_mov();
 	test_perf_arith();
-- 
2.10.1


             reply	other threads:[~2016-11-02 19:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-02 19:53 Radim Krčmář [this message]
2016-11-02 20:52 ` [kvm-unit-tests PATCH] x86: realmode: add FXSR tests Radim Krčmář

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=20161102195320.31671-1-rkrcmar@redhat.com \
    --to=rkrcmar@redhat.com \
    --cc=bsd@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=nadav.amit@gmail.com \
    --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.