[kvm-unit-tests PATCH] x86: realmode: add FXSR tests

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 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

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux