Skip to contentSkip to conversation

Fixes for clang builds

PATCH Superseded [email protected] 4 participants 47 messages Feb 26, 2020 – Feb 28, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Fixes for clang builds

From: Bill Wendling <[email protected]>

This is a series of patches which allow clang to build the kvm-unit-tests.

Bill Wendling (7):
x86: emulator: use "SSE2" for the target
pci: use uint32_t for unsigned long values
x86: realmode: syscall: add explicit size suffix to ambiguous
instructions
svm: change operand to output-only for matching constraint
svm: convert neg shift to unsigned shift
x86: VMX: use inline asm to get stack pointer
x86: VMX: the "noclone" attribute is gcc-specific

 lib/linux/pci_regs.h |  4 ++--
 x86/emulator.c       |  2 +-
 x86/realmode.c       |  6 +++---
 x86/svm.c            |  6 +++---
 x86/syscall.c        |  2 +-
 x86/vmx_tests.c      | 11 ++++++++---
 6 files changed, 18 insertions(+), 13 deletions(-)
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#1 lore raw

PATCH 1/7 x86: emulator: use "SSE2" for the target

From: Bill Wendling <[email protected]>

The movdqu and movapd instructions are SSE2 instructions. Clang interprets the __attribute__((target("sse"))) as allowing SSE only instructions. Using SSE2 instructions cause an error.

---
 x86/emulator.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/emulator.c +1 -1
index 8fe03b8..2990550 100644
@@ -658,7 +658,7 @@ static bool sseeq(sse_union *v1, sse_union *v2)
     return ok;
 }
 
-static __attribute__((target("sse"))) void test_sse(sse_union *mem)
+static __attribute__((target("sse2"))) void test_sse(sse_union *mem)
 {
     sse_union v;
 
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#2 lore raw

PATCH 2/7 pci: use uint32_t for unsigned long values

In reply to [email protected]

From: Bill Wendling <[email protected]>

The "pci_bar_*" functions use 64-bit masks, but the results are assigned to 32-bit variables. Use 32-bit masks, since we're interested only in the least significant 4-bits.

---
 lib/linux/pci_regs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/linux/pci_regs.h +2 -2
index 1becea8..3bc2b92 100644
@@ -96,8 +96,8 @@
 #define  PCI_BASE_ADDRESS_MEM_TYPE_1M	0x02	/* Below 1M [obsolete] */
 #define  PCI_BASE_ADDRESS_MEM_TYPE_64	0x04	/* 64 bit address */
 #define  PCI_BASE_ADDRESS_MEM_PREFETCH	0x08	/* prefetchable? */
-#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fUL)
-#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03UL)
+#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fU)
+#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03U)
 /* bit 1 is reserved if address_space = 1 */
 
 /* Header type 0 (normal devices) */
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#3 lore raw

Re: [kvm-unit-tests PATCH 2/7] pci: use uint32_t for unsigned long values

Quoted 29 lines · On Tue, Feb 25, 2020 at 11:44:22PM -0800, [email protected] wrote:

From: Bill Wendling <[email protected]>

The "pci_bar_*" functions use 64-bit masks, but the results are assigned to 32-bit variables. Use 32-bit masks, since we're interested only in the least significant 4-bits.

---
 lib/linux/pci_regs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/linux/pci_regs.h +2 -2
index 1becea8..3bc2b92 100644
@@ -96,8 +96,8 @@
 #define  PCI_BASE_ADDRESS_MEM_TYPE_1M	0x02	/* Below 1M [obsolete] */
 #define  PCI_BASE_ADDRESS_MEM_TYPE_64	0x04	/* 64 bit address */
 #define  PCI_BASE_ADDRESS_MEM_PREFETCH	0x08	/* prefetchable? */
-#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fUL)
-#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03UL)
+#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fU)
+#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03U)
 /* bit 1 is reserved if address_space = 1 */
 
 /* Header type 0 (normal devices) */
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog

This file comes directly from the Linux source. If it's not changed there, then it shouldn't change here.

Thanks,
drew

#4 lore raw

Re: [kvm-unit-tests PATCH 2/7] pci: use uint32_t for unsigned long values

I suspect then that how it's used in the kvm-unit-tests isn't correct. Or at least it's not how it's used in the Linux source code. Would it be better to cast all uses of these masks to uint32_t?

-bw

Quoted 38 lines · Andrew Jones
Quoted 27 lines · On Tue, Feb 25, 2020 at 11:44:22PM -0800, [email protected] wrote:

From: Bill Wendling <[email protected]>

The "pci_bar_*" functions use 64-bit masks, but the results are assigned to 32-bit variables. Use 32-bit masks, since we're interested only in the least significant 4-bits.

---
 lib/linux/pci_regs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/linux/pci_regs.h +2 -3
index 1becea8..3bc2b92 100644
@@ -96,8 +96,8 @@
 #define  PCI_BASE_ADDRESS_MEM_TYPE_1M        0x02    /* Below 1M [obsolete] */
 #define  PCI_BASE_ADDRESS_MEM_TYPE_64        0x04    /* 64 bit address */
 #define  PCI_BASE_ADDRESS_MEM_PREFETCH       0x08    /* prefetchable? */
-#define  PCI_BASE_ADDRESS_MEM_MASK   (~0x0fUL)
-#define  PCI_BASE_ADDRESS_IO_MASK    (~0x03UL)
+#define  PCI_BASE_ADDRESS_MEM_MASK   (~0x0fU)
+#define  PCI_BASE_ADDRESS_IO_MASK    (~0x03U)
 /* bit 1 is reserved if address_space = 1 */
 
 /* Header type 0 (normal devices) */
--
Quoted 2 lines

2.25.0.265.gbab2e86ba0-goog

This file comes directly from the Linux source. If it's not changed there, then it shouldn't change here.

Thanks,
drew

#5 lore raw

Re: [kvm-unit-tests PATCH 2/7] pci: use uint32_t for unsigned long values

Quoted 3 lines · Bill Wendling

I suspect then that how it's used in the kvm-unit-tests isn't correct. Or at least it's not how it's used in the Linux source code. Would it be better to cast all uses of these masks to uint32_t?

Yup. If clang is upset about kvm-unit-test's use of these masks, then we need to change how they're used, as we can't change how they're defined.

There's only one occurrence of each though, and in the same function, pci_bar_mask(). I'd probably just change the return type of that function to uint64_t, rather than add casts.

Thanks,
drew

Quoted 44 lines

-bw

Quoted 38 lines · Andrew Jones
Quoted 27 lines · On Tue, Feb 25, 2020 at 11:44:22PM -0800, [email protected] wrote:

From: Bill Wendling <[email protected]>

The "pci_bar_*" functions use 64-bit masks, but the results are assigned to 32-bit variables. Use 32-bit masks, since we're interested only in the least significant 4-bits.

---
 lib/linux/pci_regs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/linux/pci_regs.h +2 -3
index 1becea8..3bc2b92 100644
@@ -96,8 +96,8 @@
 #define  PCI_BASE_ADDRESS_MEM_TYPE_1M        0x02    /* Below 1M [obsolete] */
 #define  PCI_BASE_ADDRESS_MEM_TYPE_64        0x04    /* 64 bit address */
 #define  PCI_BASE_ADDRESS_MEM_PREFETCH       0x08    /* prefetchable? */
-#define  PCI_BASE_ADDRESS_MEM_MASK   (~0x0fUL)
-#define  PCI_BASE_ADDRESS_IO_MASK    (~0x03UL)
+#define  PCI_BASE_ADDRESS_MEM_MASK   (~0x0fU)
+#define  PCI_BASE_ADDRESS_IO_MASK    (~0x03U)
 /* bit 1 is reserved if address_space = 1 */
 
 /* Header type 0 (normal devices) */
--
Quoted 2 lines

2.25.0.265.gbab2e86ba0-goog

This file comes directly from the Linux source. If it's not changed there, then it shouldn't change here.

Thanks,
drew

#6 lore raw

PATCH 3/7 x86: realmode: syscall: add explicit size suffix to ambiguous instructions

In reply to [email protected]

From: Bill Wendling <[email protected]>

Clang requires explicit size suffixes for potentially ambiguous instructions:

x86/realmode.c:1647:2: error: ambiguous instructions require an explicit suffix (could be 'cmpb', 'cmpw', or 'cmpl')

        MK_INSN_PERF(perf_memory_load, "cmp $0, (%edi)");
        ^
x86/realmode.c:1591:10: note: expanded from macro 'MK_INSN_PERF'
                      "1:" insn "\n"                            \
                       ^

<inline asm>:8:3: note: instantiated into assembly here 1:cmp $0, (%edi)
^

The 'w' and 'l' suffixes generate code that's identical to the gcc version without them.

---
 x86/realmode.c | 6 +++---
 x86/syscall.c  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
x86/realmode.c +3 -3
index f5967ef..31f84d0 100644
@@ -1644,7 +1644,7 @@ static void test_perf_memory_load(void)
 {
 	u32 cyc, tmp;
 
-	MK_INSN_PERF(perf_memory_load, "cmp $0, (%edi)");
+	MK_INSN_PERF(perf_memory_load, "cmpw $0, (%edi)");
 
 	init_inregs(&(struct regs){ .edi = (u32)&tmp });
 
@@ -1657,7 +1657,7 @@ static void test_perf_memory_store(void)
 {
 	u32 cyc, tmp;
 
-	MK_INSN_PERF(perf_memory_store, "mov %ax, (%edi)");
+	MK_INSN_PERF(perf_memory_store, "movw %ax, (%edi)");
 
 	init_inregs(&(struct regs){ .edi = (u32)&tmp });
 
@@ -1670,7 +1670,7 @@ static void test_perf_memory_rmw(void)
 {
 	u32 cyc, tmp;
 
-	MK_INSN_PERF(perf_memory_rmw, "add $1, (%edi)");
+	MK_INSN_PERF(perf_memory_rmw, "addw $1, (%edi)");
 
 	init_inregs(&(struct regs){ .edi = (u32)&tmp });
 
x86/syscall.c +1 -1
index b4f5ac0..b7e29d6 100644
@@ -38,7 +38,7 @@ static void handle_db(struct ex_regs *regs)
 
 /* expects desired ring 3 flags in rax */
 asm("syscall32_target:\n"
-    "   cmp $0, code_segment_upon_db(%rip)\n"
+    "   cmpl $0, code_segment_upon_db(%rip)\n"
     "   jne back_to_test\n"
     "   mov %eax,%r11d\n"
     "   sysretl\n");
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#7 lore raw

PATCH 4/7 svm: change operand to output-only for matching constraint

In reply to [email protected]

From: Bill Wendling <[email protected]>

According to GNU extended asm documentation, "the two operands [of matching constraints] must include one input-only operand and one output-only operand." So remove the read/write modifier from the output constraint.

---
 x86/svm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
x86/svm.c +2 -2
index aa3d995..ae85194 100644
@@ -288,8 +288,8 @@ static void test_run(struct test *test, struct vmcb *vmcb)
             "cli \n\t"
             "stgi"
             : // inputs clobbered by the guest:
-	      "+D" (the_test),            // first argument register
-	      "+b" (the_vmcb)             // callee save register!
+	      "=D" (the_test),            // first argument register
+	      "=b" (the_vmcb)             // callee save register!
             : [test] "0" (the_test),
 	      [vmcb_phys] "1"(the_vmcb),
 	      [PREPARE_GIF_CLEAR] "i" (offsetof(struct test, prepare_gif_clear))
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#8 lore raw

PATCH 5/7 svm: convert neg shift to unsigned shift

In reply to [email protected]

From: Bill Wendling <[email protected]>

Shifting a negative signed value is undefined. Use a shift of an unsigned value instead.

---
 x86/svm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/svm.c +1 -1
index ae85194..17be4b0 100644
@@ -1148,7 +1148,7 @@ static bool npt_rw_l1mmio_check(struct test *test)
 }
 
 #define TSC_ADJUST_VALUE    (1ll << 32)
-#define TSC_OFFSET_VALUE    (-1ll << 48)
+#define TSC_OFFSET_VALUE    (~0ull << 48)
 static bool ok;
 
 static void tsc_adjust_prepare(struct test *test)
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#9 lore raw

PATCH 6/7 x86: VMX: use inline asm to get stack pointer

In reply to [email protected]

From: Bill Wendling <[email protected]>

The only supported use of a local register variable is to specify registers for input and output operands when calling Extended asm. Using it to automatically collect the value in the register isn't supported as the contents of the register aren't guaranteed. Instead use inline asm to get the stack pointer explicitly.

---
 x86/vmx_tests.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
x86/vmx_tests.c +4 -2
index a7abd63..ad8c002 100644
@@ -2165,8 +2165,9 @@ static void into_guest_main(void)
 		.offset = (uintptr_t)&&into,
 		.selector = KERNEL_CS32,
 	};
-	register uintptr_t rsp asm("rsp");
+	uintptr_t rsp = 0;
 
+	asm volatile ("mov %%rsp, %0" : "=r" (rsp));
 	if (fp.offset != (uintptr_t)&&into) {
 		printf("Code address too high.\n");
 		return;
@@ -3261,8 +3262,9 @@ static void try_compat_invvpid(void *unused)
 		.offset = (uintptr_t)&&invvpid,
 		.selector = KERNEL_CS32,
 	};
-	register uintptr_t rsp asm("rsp");
+	register uintptr_t rsp = 0;
 
+	asm volatile ("mov %%rsp, %0" : "=r" (rsp));
 	TEST_ASSERT_MSG(fp.offset == (uintptr_t)&&invvpid,
 			"Code address too high.");
 	TEST_ASSERT_MSG(rsp == (u32)rsp, "Stack address too high.");
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#10 lore raw

PATCH 7/7 x86: VMX: the "noclone" attribute is gcc-specific

In reply to [email protected]

From: Bill Wendling <[email protected]>

Don't use the "noclone" attribute for clang as it's not supported.

---
 x86/vmx_tests.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
x86/vmx_tests.c +4 -1
index ad8c002..ec88016 100644
@@ -4976,7 +4976,10 @@ extern unsigned char test_mtf1;
 extern unsigned char test_mtf2;
 extern unsigned char test_mtf3;
 
-__attribute__((noclone)) static void test_mtf_guest(void)
+#ifndef __clang__
+__attribute__((noclone))
+#endif
+static void test_mtf_guest(void)
 {
 	asm ("vmcall;\n\t"
 	     "out %al, $0x80;\n\t"
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#11 lore raw

Re: [kvm-unit-tests PATCH 7/7] x86: VMX: the "noclone" attribute is gcc-specific

Quoted 6 lines · On Tue, Feb 25, 2020 at 11:45 PM <[email protected]> wrote:

From: Bill Wendling <[email protected]>

Don't use the "noclone" attribute for clang as it's not supported.

Quoted 23 lines
---
 x86/vmx_tests.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
x86/vmx_tests.c +4 -2
index ad8c002..ec88016 100644
@@ -4976,7 +4976,10 @@ extern unsigned char test_mtf1;
 extern unsigned char test_mtf2;
 extern unsigned char test_mtf3;
 
-__attribute__((noclone)) static void test_mtf_guest(void)
+#ifndef __clang__
+__attribute__((noclone))
+#endif
+static void test_mtf_guest(void)
 {
        asm ("vmcall;\n\t"
             "out %al, $0x80;\n\t"
--

2.25.0.265.gbab2e86ba0-goog

#12 lore raw

Re: [kvm-unit-tests PATCH 7/7] x86: VMX: the "noclone" attribute is gcc-specific

Blech...

Quoted 9 lines · Oliver Upton
Quoted 6 lines · On Tue, Feb 25, 2020 at 11:45 PM <[email protected]> wrote:

From: Bill Wendling <[email protected]>

Don't use the "noclone" attribute for clang as it's not supported.

s/Signed-off-by/Reviewed-by/

Quoted 23 lines
Quoted 23 lines
---
 x86/vmx_tests.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
x86/vmx_tests.c +4 -2
index ad8c002..ec88016 100644
@@ -4976,7 +4976,10 @@ extern unsigned char test_mtf1;
 extern unsigned char test_mtf2;
 extern unsigned char test_mtf3;
 
-__attribute__((noclone)) static void test_mtf_guest(void)
+#ifndef __clang__
+__attribute__((noclone))
+#endif
+static void test_mtf_guest(void)
 {
        asm ("vmcall;\n\t"
             "out %al, $0x80;\n\t"
--

2.25.0.265.gbab2e86ba0-goog

#13 lore raw

v2 cover Fixes for clang builds

In reply to [email protected]

Changes to "pci: cast masks to uint32_t for unsigned long values" to cast the masks instead of changing the values in the header.

Bill Wendling (2):
x86: emulator: use "SSE2" for the target
pci: cast masks to uint32_t for unsigned long values

Eric Hankland (2):
x86: pmu: Test WRMSR on a running counter
x86: pmu: Test perfctr overflow after WRMSR on a running counter

Oliver Upton (1):
x86: VMX: Add tests for monitor trap flag

Paolo Bonzini (2):
x86: provide enabled and disabled variation of the PCID test
vmx: tweak XFAILS for #DB test

Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#14 lore raw

PATCH 1/7 x86: emulator: use "SSE2" for the target

The movdqu and movapd instructions are SSE2 instructions. Clang interprets the __attribute__((target("sse"))) as allowing SSE only instructions. Using SSE2 instructions cause an error.

---
 x86/emulator.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/emulator.c +1 -1
index 8fe03b8..2990550 100644
@@ -658,7 +658,7 @@ static bool sseeq(sse_union *v1, sse_union *v2)
     return ok;
 }
 
-static __attribute__((target("sse"))) void test_sse(sse_union *mem)
+static __attribute__((target("sse2"))) void test_sse(sse_union *mem)
 {
     sse_union v;
 
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#15 lore raw

PATCH v2 1/7 x86: pmu: Test WRMSR on a running counter

In reply to Bill Wendling

From: Eric Hankland <[email protected]>

Ensure that the value of the counter was successfully set to 0 after writing it while the counter was running.

---
 x86/pmu.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
x86/pmu.c +16 -0
index a4e483b..c8096b8 100644
@@ -420,6 +420,21 @@ static void check_rdpmc(void)
 	report_prefix_pop();
 }
 
+static void check_running_counter_wrmsr(void)
+{
+	pmu_counter_t evt = {
+		.ctr = MSR_IA32_PERFCTR0,
+		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
+		.count = 0,
+	};
+
+	start_event(&evt);
+	loop();
+	wrmsr(MSR_IA32_PERFCTR0, 0);
+	stop_event(&evt);
+	report(evt.count < gp_events[1].min, "running counter wrmsr");
+}
+
 int main(int ac, char **av)
 {
 	struct cpuid id = cpuid(10);
@@ -454,6 +469,7 @@ int main(int ac, char **av)
 	check_counters_many();
 	check_counter_overflow();
 	check_gp_counter_cmask();
+	check_running_counter_wrmsr();
 
 	return report_summary();
 }
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#16 lore raw

PATCH 2/7 pci: use uint32_t for unsigned long values

In reply to Bill Wendling

The "pci_bar_*" functions use 64-bit masks, but the results are assigned to 32-bit variables. Use 32-bit masks, since we're interested only in the least significant 4-bits.

---
 lib/linux/pci_regs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/linux/pci_regs.h +2 -2
index 1becea8..3bc2b92 100644
@@ -96,8 +96,8 @@
 #define  PCI_BASE_ADDRESS_MEM_TYPE_1M	0x02	/* Below 1M [obsolete] */
 #define  PCI_BASE_ADDRESS_MEM_TYPE_64	0x04	/* 64 bit address */
 #define  PCI_BASE_ADDRESS_MEM_PREFETCH	0x08	/* prefetchable? */
-#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fUL)
-#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03UL)
+#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fU)
+#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03U)
 /* bit 1 is reserved if address_space = 1 */
 
 /* Header type 0 (normal devices) */
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
maintainer
#17 lore raw

Re: [kvm-unit-tests PATCH 2/7] pci: use uint32_t for unsigned long values

Quoted 15 lines · Bill Wendling

The "pci_bar_*" functions use 64-bit masks, but the results are assigned to 32-bit variables. Use 32-bit masks, since we're interested only in the least significant 4-bits.

---
 lib/linux/pci_regs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/linux/pci_regs.h +0 -0
index 1becea8..3bc2b92 100644

lib/linux/pci_regs.h line 103

Quoted 10 lines
lib/linux/pci_regs.h +2 -2
@@ -96,8 +96,8 @@
 #define  PCI_BASE_ADDRESS_MEM_TYPE_1M	0x02	/* Below 1M [obsolete] */
 #define  PCI_BASE_ADDRESS_MEM_TYPE_64	0x04	/* 64 bit address */
 #define  PCI_BASE_ADDRESS_MEM_PREFETCH	0x08	/* prefetchable? */
-#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fUL)
-#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03UL)
+#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fU)
+#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03U)
 /* bit 1 is reserved if address_space = 1 */
 
 /* Header type 0 (normal devices) */

Removing the "U" is even better because it will then sign-extend automatically.

Paolo

#18 lore raw

Re: [kvm-unit-tests PATCH 2/7] pci: use uint32_t for unsigned long values

Quoted 24 lines · Paolo Bonzini
Quoted 23 lines · Bill Wendling

The "pci_bar_*" functions use 64-bit masks, but the results are assigned to 32-bit variables. Use 32-bit masks, since we're interested only in the least significant 4-bits.

---
 lib/linux/pci_regs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/linux/pci_regs.h +2 -2
index 1becea8..3bc2b92 100644
@@ -96,8 +96,8 @@
 #define  PCI_BASE_ADDRESS_MEM_TYPE_1M	0x02	/* Below 1M [obsolete] */
 #define  PCI_BASE_ADDRESS_MEM_TYPE_64	0x04	/* 64 bit address */
 #define  PCI_BASE_ADDRESS_MEM_PREFETCH	0x08	/* prefetchable? */
-#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fUL)
-#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03UL)
+#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fU)
+#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03U)
 /* bit 1 is reserved if address_space = 1 */
 
Quoted 6 lines

lib/linux/pci_regs.h line 103

Quoted 2 lines
lib/linux/pci_regs.h +0 -0
 /* Header type 0 (normal devices) */

Removing the "U" is even better because it will then sign-extend automatically.

We don't want this patch at all though. We shouldn't change pci_regs.h since it comes from linux and someday we may update again and lose any changes we make. We should change how these masks are used instead.

Thanks,
drew

maintainer
#19 lore raw

Re: [kvm-unit-tests PATCH 2/7] pci: use uint32_t for unsigned long values

Quoted 32 lines · Andrew Jones
Quoted 24 lines · Paolo Bonzini
Quoted 23 lines · Bill Wendling

The "pci_bar_*" functions use 64-bit masks, but the results are assigned to 32-bit variables. Use 32-bit masks, since we're interested only in the least significant 4-bits.

---
 lib/linux/pci_regs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/linux/pci_regs.h +2 -2
index 1becea8..3bc2b92 100644
@@ -96,8 +96,8 @@
 #define  PCI_BASE_ADDRESS_MEM_TYPE_1M	0x02	/* Below 1M [obsolete] */
 #define  PCI_BASE_ADDRESS_MEM_TYPE_64	0x04	/* 64 bit address */
 #define  PCI_BASE_ADDRESS_MEM_PREFETCH	0x08	/* prefetchable? */
-#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fUL)
-#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03UL)
+#define  PCI_BASE_ADDRESS_MEM_MASK	(~0x0fU)
+#define  PCI_BASE_ADDRESS_IO_MASK	(~0x03U)
 /* bit 1 is reserved if address_space = 1 */
 
Quoted 6 lines

lib/linux/pci_regs.h line 103

Quoted 2 lines
lib/linux/pci_regs.h +0 -0
 /* Header type 0 (normal devices) */

Removing the "U" is even better because it will then sign-extend automatically.

Quoted 3 lines

We don't want this patch at all though. We shouldn't change pci_regs.h since it comes from linux and someday we may update again and lose any changes we make. We should change how these masks are used instead.

I will try to get the change into Linux.

Paolo

#20 lore raw

PATCH v2 2/7 x86: provide enabled and disabled variation of the PCID test

In reply to Bill Wendling

From: Paolo Bonzini <[email protected]>

The PCID test checks for exceptions when PCID=0 or INVPCID=0 in CPUID. Cover that by adding a separate testcase with different CPUID.

---
 x86/unittests.cfg | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
x86/unittests.cfg +6 -1
index aae1523..f2401eb 100644
@@ -228,7 +228,12 @@ extra_params = --append "10000000 `date +%s`"
 
 [pcid]
 file = pcid.flat
-extra_params = -cpu qemu64,+pcid
+extra_params = -cpu qemu64,+pcid,+invpcid
+arch = x86_64
+
+[pcid-disabled]
+file = pcid.flat
+extra_params = -cpu qemu64,-pcid,-invpcid
 arch = x86_64
 
 [rdpru]
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#21 lore raw

PATCH v2 3/7 x86: pmu: Test perfctr overflow after WRMSR on a running counter

In reply to Bill Wendling

From: Eric Hankland <[email protected]>

Ensure that a WRMSR on a running counter will correctly update when the counter should overflow (similar to the existing overflow test case but with the counter being written to while it is running).

---
 x86/pmu.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
x86/pmu.c +18 -1
index c8096b8..f45621a 100644
@@ -422,17 +422,34 @@ static void check_rdpmc(void)
 
 static void check_running_counter_wrmsr(void)
 {
+	uint64_t status;
 	pmu_counter_t evt = {
 		.ctr = MSR_IA32_PERFCTR0,
 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
 		.count = 0,
 	};
 
+	report_prefix_push("running counter wrmsr");
+
 	start_event(&evt);
 	loop();
 	wrmsr(MSR_IA32_PERFCTR0, 0);
 	stop_event(&evt);
-	report(evt.count < gp_events[1].min, "running counter wrmsr");
+	report(evt.count < gp_events[1].min, "cntr");
+
+	/* clear status before overflow test */
+	wrmsr(MSR_CORE_PERF_GLOBAL_OVF_CTRL,
+	      rdmsr(MSR_CORE_PERF_GLOBAL_STATUS));
+
+	evt.count = 0;
+	start_event(&evt);
+	wrmsr(MSR_IA32_PERFCTR0, -1);
+	loop();
+	stop_event(&evt);
+	status = rdmsr(MSR_CORE_PERF_GLOBAL_STATUS);
+	report(status & 1, "status");
+
+	report_prefix_pop();
 }
 
 int main(int ac, char **av)
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#22 lore raw

PATCH 3/7 x86: realmode: syscall: add explicit size suffix to ambiguous instructions

In reply to Bill Wendling

Clang requires explicit size suffixes for potentially ambiguous instructions:

x86/realmode.c:1647:2: error: ambiguous instructions require an explicit suffix (could be 'cmpb', 'cmpw', or 'cmpl')

        MK_INSN_PERF(perf_memory_load, "cmp $0, (%edi)");
        ^
x86/realmode.c:1591:10: note: expanded from macro 'MK_INSN_PERF'
                      "1:" insn "\n"                            \
                       ^

<inline asm>:8:3: note: instantiated into assembly here 1:cmp $0, (%edi)
^

The 'w' and 'l' suffixes generate code that's identical to the gcc version without them.

---
 x86/realmode.c | 6 +++---
 x86/syscall.c  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
x86/realmode.c +3 -3
index f5967ef..31f84d0 100644
@@ -1644,7 +1644,7 @@ static void test_perf_memory_load(void)
 {
 	u32 cyc, tmp;
 
-	MK_INSN_PERF(perf_memory_load, "cmp $0, (%edi)");
+	MK_INSN_PERF(perf_memory_load, "cmpw $0, (%edi)");
 
 	init_inregs(&(struct regs){ .edi = (u32)&tmp });
 
@@ -1657,7 +1657,7 @@ static void test_perf_memory_store(void)
 {
 	u32 cyc, tmp;
 
-	MK_INSN_PERF(perf_memory_store, "mov %ax, (%edi)");
+	MK_INSN_PERF(perf_memory_store, "movw %ax, (%edi)");
 
 	init_inregs(&(struct regs){ .edi = (u32)&tmp });
 
@@ -1670,7 +1670,7 @@ static void test_perf_memory_rmw(void)
 {
 	u32 cyc, tmp;
 
-	MK_INSN_PERF(perf_memory_rmw, "add $1, (%edi)");
+	MK_INSN_PERF(perf_memory_rmw, "addw $1, (%edi)");
 
 	init_inregs(&(struct regs){ .edi = (u32)&tmp });
 
x86/syscall.c +1 -1
index b4f5ac0..b7e29d6 100644
@@ -38,7 +38,7 @@ static void handle_db(struct ex_regs *regs)
 
 /* expects desired ring 3 flags in rax */
 asm("syscall32_target:\n"
-    "   cmp $0, code_segment_upon_db(%rip)\n"
+    "   cmpl $0, code_segment_upon_db(%rip)\n"
     "   jne back_to_test\n"
     "   mov %eax,%r11d\n"
     "   sysretl\n");
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#23 lore raw

PATCH 4/7 svm: change operand to output-only for matching constraint

In reply to Bill Wendling

According to GNU extended asm documentation, "the two operands [of matching constraints] must include one input-only operand and one output-only operand." So remove the read/write modifier from the output constraint.

---
 x86/svm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
x86/svm.c +2 -2
index aa3d995..ae85194 100644
@@ -288,8 +288,8 @@ static void test_run(struct test *test, struct vmcb *vmcb)
             "cli \n\t"
             "stgi"
             : // inputs clobbered by the guest:
-	      "+D" (the_test),            // first argument register
-	      "+b" (the_vmcb)             // callee save register!
+	      "=D" (the_test),            // first argument register
+	      "=b" (the_vmcb)             // callee save register!
             : [test] "0" (the_test),
 	      [vmcb_phys] "1"(the_vmcb),
 	      [PREPARE_GIF_CLEAR] "i" (offsetof(struct test, prepare_gif_clear))
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#24 lore raw

PATCH v2 4/7 vmx: tweak XFAILS for #DB test

In reply to Bill Wendling

From: Paolo Bonzini <[email protected]>

These were already fixed by KVM_CAP_EXCEPTION_PAYLOAD, but they were failing on old QEMUs that did not support it. The recent KVM patch "KVM: x86: Deliver exception payload on KVM_GET_VCPU_EVENTS" however fixed them even there, so it is about time to flip the arguments to check_db_exit and avoid ugly XPASS results with newer versions of QEMU.

---
 x86/vmx_tests.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
x86/vmx_tests.c +2 -2
index b31c360..1323dc5 100644
@@ -8332,7 +8332,7 @@ static void vmx_db_test(void)
 	 * modified DR6, but fails miserably.
 	 */
 	single_step_guest("Software synthesized single-step", starting_dr6, 0);
-	check_db_exit(true, true, false, &post_wbinvd, DR_STEP, starting_dr6);
+	check_db_exit(false, false, false, &post_wbinvd, DR_STEP, starting_dr6);
 
 	/*
 	 * L0 synthesized #DB trap for single-step in MOVSS shadow is
@@ -8342,7 +8342,7 @@ static void vmx_db_test(void)
 	 */
 	single_step_guest("Software synthesized single-step in MOVSS shadow",
 			  starting_dr6, BIT(12) | DR_STEP | DR_TRAP0);
-	check_db_exit(true, true, true, &post_movss_wbinvd, DR_STEP | DR_TRAP0,
+	check_db_exit(true, false, true, &post_movss_wbinvd, DR_STEP | DR_TRAP0,
 		      starting_dr6);
 
 	/*
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#25 lore raw

PATCH 5/7 svm: convert neg shift to unsigned shift

In reply to Bill Wendling

Shifting a negative signed value is undefined. Use a shift of an unsigned value instead.

---
 x86/svm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/svm.c +1 -1
index ae85194..17be4b0 100644
@@ -1148,7 +1148,7 @@ static bool npt_rw_l1mmio_check(struct test *test)
 }
 
 #define TSC_ADJUST_VALUE    (1ll << 32)
-#define TSC_OFFSET_VALUE    (-1ll << 48)
+#define TSC_OFFSET_VALUE    (~0ull << 48)
 static bool ok;
 
 static void tsc_adjust_prepare(struct test *test)
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
maintainer
#26 lore raw

Re: [kvm-unit-tests PATCH 5/7] svm: convert neg shift to unsigned shift

Quoted 4 lines · Bill Wendling

Shifting a negative signed value is undefined. Use a shift of an unsigned value instead.

In practice only UBSAN complains; GCC even documents that it does not treat it as undefined (which would be insane, especially if the multiplication by 2 does not overflow as in this case):

GCC does not use the latitude given in C99 only to treat certain aspects of signed '<<' as undefined, but this is subject to change.

But in the interest of purity we should instead compile with -fwrapv.

Paolo

Quoted 18 lines
---
 x86/svm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/svm.c +1 -1
index ae85194..17be4b0 100644
@@ -1148,7 +1148,7 @@ static bool npt_rw_l1mmio_check(struct test *test)
 }
 
 #define TSC_ADJUST_VALUE    (1ll << 32)
-#define TSC_OFFSET_VALUE    (-1ll << 48)
+#define TSC_OFFSET_VALUE    (~0ull << 48)
 static bool ok;
 
 static void tsc_adjust_prepare(struct test *test)
#27 lore raw

PATCH v2 5/7 x86: VMX: Add tests for monitor trap flag

In reply to Bill Wendling

From: Oliver Upton <[email protected]>

Test to verify that MTF VM-exits into host are synthesized when the 'monitor trap flag' processor-based VM-execution control is set under various conditions.

Expect an MTF VM-exit if instruction execution produces no events other than MTF. Should instruction execution produce a concurrent debug-trap and MTF event, expect an MTF VM-exit with the 'pending debug exceptions' VMCS field set. Expect an MTF VM-exit to follow event delivery should instruction execution generate a higher-priority event, such as a general-protection fault. Lastly, expect an MTF VM-exit to follow delivery of a debug-trap software exception (INT1/INT3/INTO/INT n).

---
 x86/vmx.h       |   1 +
 x86/vmx_tests.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 158 insertions(+)
x86/vmx.h +1 -0
index 6214400..6adf091 100644
@@ -399,6 +399,7 @@ enum Ctrl0 {
 	CPU_NMI_WINDOW		= 1ul << 22,
 	CPU_IO			= 1ul << 24,
 	CPU_IO_BITMAP		= 1ul << 25,
+	CPU_MTF			= 1ul << 27,
 	CPU_MSR_BITMAP		= 1ul << 28,
 	CPU_MONITOR		= 1ul << 29,
 	CPU_PAUSE		= 1ul << 30,
x86/vmx_tests.c +157 -0

168 lines not on this page · raw message

Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#28 lore raw

PATCH v2 6/7 x86: emulator: use "SSE2" for the target

In reply to Bill Wendling

The movdqu and movapd instructions are SSE2 instructions. Clang interprets the __attribute__((target("sse"))) as allowing SSE only instructions. Using SSE2 instructions cause an error.

---
 x86/emulator.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/emulator.c +1 -1
index 8fe03b8..2990550 100644
@@ -658,7 +658,7 @@ static bool sseeq(sse_union *v1, sse_union *v2)
     return ok;
 }
 
-static __attribute__((target("sse"))) void test_sse(sse_union *mem)
+static __attribute__((target("sse2"))) void test_sse(sse_union *mem)
 {
     sse_union v;
 
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#29 lore raw

PATCH 6/7 x86: VMX: use inline asm to get stack pointer

In reply to Bill Wendling

The only supported use of a local register variable is to specify registers for input and output operands when calling Extended asm. Using it to automatically collect the value in the register isn't supported as the contents of the register aren't guaranteed. Instead use inline asm to get the stack pointer explicitly.

---
 x86/vmx_tests.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
x86/vmx_tests.c +4 -2
index a7abd63..ad8c002 100644
@@ -2165,8 +2165,9 @@ static void into_guest_main(void)
 		.offset = (uintptr_t)&&into,
 		.selector = KERNEL_CS32,
 	};
-	register uintptr_t rsp asm("rsp");
+	uintptr_t rsp = 0;
 
+	asm volatile ("mov %%rsp, %0" : "=r" (rsp));
 	if (fp.offset != (uintptr_t)&&into) {
 		printf("Code address too high.\n");
 		return;
@@ -3261,8 +3262,9 @@ static void try_compat_invvpid(void *unused)
 		.offset = (uintptr_t)&&invvpid,
 		.selector = KERNEL_CS32,
 	};
-	register uintptr_t rsp asm("rsp");
+	register uintptr_t rsp = 0;
 
+	asm volatile ("mov %%rsp, %0" : "=r" (rsp));
 	TEST_ASSERT_MSG(fp.offset == (uintptr_t)&&invvpid,
 			"Code address too high.");
 	TEST_ASSERT_MSG(rsp == (u32)rsp, "Stack address too high.");
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#30 lore raw

PATCH v2 7/7 pci: cast masks to uint32_t for unsigned long values

In reply to Bill Wendling

The "pci_bar_*" functions use 64-bit masks, but the results are assigned to 32-bit variables. Cast mask usages to 32-bit, since we're interested only in the least significant 4-bits.

---
 lib/pci.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/pci.c +2 -1
index daa33e1..1b85411 100644
@@ -107,7 +107,8 @@ pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id)
 uint32_t pci_bar_mask(uint32_t bar)
 {
 	return (bar & PCI_BASE_ADDRESS_SPACE_IO) ?
-		PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK;
+		(uint32_t)PCI_BASE_ADDRESS_IO_MASK :
+		(uint32_t)PCI_BASE_ADDRESS_MEM_MASK;
 }
 
 uint32_t pci_bar_get(struct pci_dev *dev, int bar_num)
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#31 lore raw

PATCH 7/7 x86: VMX: the "noclone" attribute is gcc-specific

In reply to Bill Wendling

Don't use the "noclone" attribute for clang as it's not supported.

---
 x86/vmx_tests.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
x86/vmx_tests.c +4 -1
index ad8c002..ec88016 100644
@@ -4976,7 +4976,10 @@ extern unsigned char test_mtf1;
 extern unsigned char test_mtf2;
 extern unsigned char test_mtf3;
 
-__attribute__((noclone)) static void test_mtf_guest(void)
+#ifndef __clang__
+__attribute__((noclone))
+#endif
+static void test_mtf_guest(void)
 {
 	asm ("vmcall;\n\t"
 	     "out %al, $0x80;\n\t"
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
maintainer
#32 lore raw

Re: [kvm-unit-tests PATCH 7/7] x86: VMX: the "noclone" attribute is gcc-specific

Quoted 14 lines · Bill Wendling

Don't use the "noclone" attribute for clang as it's not supported.

---
 x86/vmx_tests.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
x86/vmx_tests.c +0 -0
index ad8c002..ec88016 100644
@@ -4976,7 +4976,10 @@ extern unsigned char test_mtf1;
 extern unsigned char test_mtf2;

x86/vmx_tests.c extern unsigned char test_mtf1; · line 4985

Quoted 10 lines
x86/vmx_tests.c +4 -1
 extern unsigned char test_mtf3;
 
-__attribute__((noclone)) static void test_mtf_guest(void)
+#ifndef __clang__
+__attribute__((noclone))
+#endif
+static void test_mtf_guest(void)
 {
 	asm ("vmcall;\n\t"
 	     "out %al, $0x80;\n\t"

It's not needed at all, let's drop it.

Paolo

#33 lore raw

Re: [kvm-unit-tests PATCH 7/7] x86: VMX: the "noclone" attribute is gcc-specific

Quoted 29 lines · Paolo Bonzini
Quoted 14 lines · Bill Wendling

Don't use the "noclone" attribute for clang as it's not supported.

---
 x86/vmx_tests.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
x86/vmx_tests.c +0 -0
index ad8c002..ec88016 100644
@@ -4976,7 +4976,10 @@ extern unsigned char test_mtf1;
 extern unsigned char test_mtf2;

x86/vmx_tests.c extern unsigned char test_mtf1; · line 4985

Quoted 10 lines
x86/vmx_tests.c +4 -1
 extern unsigned char test_mtf3;
 
-__attribute__((noclone)) static void test_mtf_guest(void)
+#ifndef __clang__
+__attribute__((noclone))
+#endif
+static void test_mtf_guest(void)
 {
      asm ("vmcall;\n\t"
           "out %al, $0x80;\n\t"

It's not needed at all, let's drop it.

Quoted 2 lines

Paolo

If we wanted to be absolutely certain that the extern labels used for assertions about the guest RIP are correct, we may still want it. Alternatively, I could rewrite the test such that the guest will report the instruction boundary where it anticipates MTF whenever it makes the vmcall to request the host turns on MTF.

--
Thanks,
Oliver

maintainer
#34 lore raw

Re: [kvm-unit-tests PATCH 7/7] x86: VMX: the "noclone" attribute is gcc-specific

Quoted 5 lines · Oliver Upton

If we wanted to be absolutely certain that the extern labels used for assertions about the guest RIP are correct, we may still want it. Alternatively, I could rewrite the test such that the guest will report the instruction boundary where it anticipates MTF whenever it makes the vmcall to request the host turns on MTF.

Right, but it's used only once, and as a function pointer at that, so there's only so much that the compiler can do to "optimize". Let's think about how to fix it if it breaks.

Paolo

#35 lore raw

Re: [kvm-unit-tests PATCH 7/7] x86: VMX: the "noclone" attribute is gcc-specific

Quoted 8 lines · Paolo Bonzini
Quoted 5 lines · Oliver Upton

If we wanted to be absolutely certain that the extern labels used for assertions about the guest RIP are correct, we may still want it. Alternatively, I could rewrite the test such that the guest will report the instruction boundary where it anticipates MTF whenever it makes the vmcall to request the host turns on MTF.

Quoted 6 lines

Right, but it's used only once, and as a function pointer at that, so there's only so much that the compiler can do to "optimize". Let's think about how to fix it if it breaks.

Paolo

Very true. Unchecked paranoia of the compiler isn't warranted here.

Thanks :)

--
Best,
Oliver

#36 lore raw
In reply to Bill Wendling

Hi Bill,

Quoted 12 lines · Bill Wendling

Changes to "pci: cast masks to uint32_t for unsigned long values" to cast the masks instead of changing the values in the header.

Bill Wendling (2):
x86: emulator: use "SSE2" for the target
pci: cast masks to uint32_t for unsigned long values

Eric Hankland (2):
x86: pmu: Test WRMSR on a running counter
x86: pmu: Test perfctr overflow after WRMSR on a running counter

Quoted 7 lines

Oliver Upton (1):
x86: VMX: Add tests for monitor trap flag

Paolo Bonzini (2):
x86: provide enabled and disabled variation of the PCID test
vmx: tweak XFAILS for #DB test

These patches have already been pushed. Was there any reason to squash your v1 patches into the commits that introduced the clang issues? Otherwise, I'd strongly suggest re-posting the v1 series with just the clang fixes.

Thanks!

--
Best,
Oliver

#37 lore raw

v3 cover Fixes for clang builds

In reply to Bill Wendling

Changes to "pci: cast masks to uint32_t for unsigned long values" to cast the masks instead of changing the values in the header.

This is a re-send of the v2 patches, which included patches that shouldn't have been included.

Bill Wendling (7):
x86: emulator: use "SSE2" for the target
pci: cast masks to uint32_t for unsigned long values
x86: realmode: syscall: add explicit size suffix to ambiguous
instructions
svm: change operand to output-only for matching constraint
svm: convert neg shift to unsigned shift
x86: VMX: use inline asm to get stack pointer
x86: VMX: the "noclone" attribute is gcc-specific

 lib/pci.c       |  3 ++-
 x86/emulator.c  |  2 +-
 x86/realmode.c  |  6 +++---
 x86/svm.c       |  6 +++---
 x86/syscall.c   |  2 +-
 x86/vmx_tests.c | 11 ++++++++---
 6 files changed, 18 insertions(+), 12 deletions(-)
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
#38 lore raw

PATCH v3 1/7 x86: emulator: use "SSE2" for the target

The movdqu and movapd instructions are SSE2 instructions. Clang interprets the __attribute__((target("sse"))) as allowing SSE only instructions. Using SSE2 instructions cause an error.

---
 x86/emulator.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/emulator.c +1 -1
index 8fe03b8..2990550 100644
@@ -658,7 +658,7 @@ static bool sseeq(sse_union *v1, sse_union *v2)
     return ok;
 }
 
-static __attribute__((target("sse"))) void test_sse(sse_union *mem)
+static __attribute__((target("sse2"))) void test_sse(sse_union *mem)
 {
     sse_union v;
 
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
#39 lore raw

PATCH v3 2/7 pci: cast masks to uint32_t for unsigned long values

In reply to Bill Wendling

The "pci_bar_*" functions use 64-bit masks, but the results are assigned to 32-bit variables. Cast mask usages to 32-bit, since we're interested only in the least significant 4-bits.

---
 lib/pci.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/pci.c +2 -1
index daa33e1..1b85411 100644
@@ -107,7 +107,8 @@ pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id)
 uint32_t pci_bar_mask(uint32_t bar)
 {
 	return (bar & PCI_BASE_ADDRESS_SPACE_IO) ?
-		PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK;
+		(uint32_t)PCI_BASE_ADDRESS_IO_MASK :
+		(uint32_t)PCI_BASE_ADDRESS_MEM_MASK;
 }
 
 uint32_t pci_bar_get(struct pci_dev *dev, int bar_num)
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
#40 lore raw

PATCH v3 3/7 x86: realmode: syscall: add explicit size suffix to ambiguous instructions

In reply to Bill Wendling

Clang requires explicit size suffixes for potentially ambiguous instructions:

x86/realmode.c:1647:2: error: ambiguous instructions require an explicit suffix (could be 'cmpb', 'cmpw', or 'cmpl')

        MK_INSN_PERF(perf_memory_load, "cmp $0, (%edi)");
        ^
x86/realmode.c:1591:10: note: expanded from macro 'MK_INSN_PERF'
                      "1:" insn "\n"                            \
                       ^

<inline asm>:8:3: note: instantiated into assembly here 1:cmp $0, (%edi)
^

The 'w' and 'l' suffixes generate code that's identical to the gcc version without them.

---
 x86/realmode.c | 6 +++---
 x86/syscall.c  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
x86/realmode.c +3 -3
index f5967ef..31f84d0 100644
@@ -1644,7 +1644,7 @@ static void test_perf_memory_load(void)
 {
 	u32 cyc, tmp;
 
-	MK_INSN_PERF(perf_memory_load, "cmp $0, (%edi)");
+	MK_INSN_PERF(perf_memory_load, "cmpw $0, (%edi)");
 
 	init_inregs(&(struct regs){ .edi = (u32)&tmp });
 
@@ -1657,7 +1657,7 @@ static void test_perf_memory_store(void)
 {
 	u32 cyc, tmp;
 
-	MK_INSN_PERF(perf_memory_store, "mov %ax, (%edi)");
+	MK_INSN_PERF(perf_memory_store, "movw %ax, (%edi)");
 
 	init_inregs(&(struct regs){ .edi = (u32)&tmp });
 
@@ -1670,7 +1670,7 @@ static void test_perf_memory_rmw(void)
 {
 	u32 cyc, tmp;
 
-	MK_INSN_PERF(perf_memory_rmw, "add $1, (%edi)");
+	MK_INSN_PERF(perf_memory_rmw, "addw $1, (%edi)");
 
 	init_inregs(&(struct regs){ .edi = (u32)&tmp });
 
x86/syscall.c +1 -1
index b4f5ac0..b7e29d6 100644
@@ -38,7 +38,7 @@ static void handle_db(struct ex_regs *regs)
 
 /* expects desired ring 3 flags in rax */
 asm("syscall32_target:\n"
-    "   cmp $0, code_segment_upon_db(%rip)\n"
+    "   cmpl $0, code_segment_upon_db(%rip)\n"
     "   jne back_to_test\n"
     "   mov %eax,%r11d\n"
     "   sysretl\n");
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
#41 lore raw

PATCH v3 4/7 svm: change operand to output-only for matching constraint

In reply to Bill Wendling

According to GNU extended asm documentation, "the two operands [of matching constraints] must include one input-only operand and one output-only operand." So remove the read/write modifier from the output constraint.

---
 x86/svm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
x86/svm.c +2 -2
index aa3d995..ae85194 100644
@@ -288,8 +288,8 @@ static void test_run(struct test *test, struct vmcb *vmcb)
             "cli \n\t"
             "stgi"
             : // inputs clobbered by the guest:
-	      "+D" (the_test),            // first argument register
-	      "+b" (the_vmcb)             // callee save register!
+	      "=D" (the_test),            // first argument register
+	      "=b" (the_vmcb)             // callee save register!
             : [test] "0" (the_test),
 	      [vmcb_phys] "1"(the_vmcb),
 	      [PREPARE_GIF_CLEAR] "i" (offsetof(struct test, prepare_gif_clear))
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
#42 lore raw

PATCH v3 5/7 svm: convert neg shift to unsigned shift

In reply to Bill Wendling

Shifting a negative signed value is undefined. Use a shift of an unsigned value instead.

---
 x86/svm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/svm.c +1 -1
index ae85194..17be4b0 100644
@@ -1148,7 +1148,7 @@ static bool npt_rw_l1mmio_check(struct test *test)
 }
 
 #define TSC_ADJUST_VALUE    (1ll << 32)
-#define TSC_OFFSET_VALUE    (-1ll << 48)
+#define TSC_OFFSET_VALUE    (~0ull << 48)
 static bool ok;
 
 static void tsc_adjust_prepare(struct test *test)
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
#43 lore raw

PATCH v3 6/7 x86: VMX: use inline asm to get stack pointer

In reply to Bill Wendling

The only supported use of a local register variable is to specify registers for input and output operands when calling Extended asm. Using it to automatically collect the value in the register isn't supported as the contents of the register aren't guaranteed. Instead use inline asm to get the stack pointer explicitly.

---
 x86/vmx_tests.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
x86/vmx_tests.c +4 -2
index a7abd63..ad8c002 100644
@@ -2165,8 +2165,9 @@ static void into_guest_main(void)
 		.offset = (uintptr_t)&&into,
 		.selector = KERNEL_CS32,
 	};
-	register uintptr_t rsp asm("rsp");
+	uintptr_t rsp = 0;
 
+	asm volatile ("mov %%rsp, %0" : "=r" (rsp));
 	if (fp.offset != (uintptr_t)&&into) {
 		printf("Code address too high.\n");
 		return;
@@ -3261,8 +3262,9 @@ static void try_compat_invvpid(void *unused)
 		.offset = (uintptr_t)&&invvpid,
 		.selector = KERNEL_CS32,
 	};
-	register uintptr_t rsp asm("rsp");
+	register uintptr_t rsp = 0;
 
+	asm volatile ("mov %%rsp, %0" : "=r" (rsp));
 	TEST_ASSERT_MSG(fp.offset == (uintptr_t)&&invvpid,
 			"Code address too high.");
 	TEST_ASSERT_MSG(rsp == (u32)rsp, "Stack address too high.");
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
#44 lore raw

PATCH v3 7/7 x86: VMX: the "noclone" attribute is gcc-specific

In reply to Bill Wendling

Don't use the "noclone" attribute for clang as it's not supported.

---
 x86/vmx_tests.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
x86/vmx_tests.c +4 -1
index ad8c002..ec88016 100644
@@ -4976,7 +4976,10 @@ extern unsigned char test_mtf1;
 extern unsigned char test_mtf2;
 extern unsigned char test_mtf3;
 
-__attribute__((noclone)) static void test_mtf_guest(void)
+#ifndef __clang__
+__attribute__((noclone))
+#endif
+static void test_mtf_guest(void)
 {
 	asm ("vmcall;\n\t"
 	     "out %al, $0x80;\n\t"
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
#45 lore raw

Re: [kvm-unit-tests PATCH v3 7/7] x86: VMX: the "noclone" attribute is gcc-specific

Hi Bill,

Perhaps it would be better to drill down the noclone attribute by GCC version as well. May I suggest the following?

lib/compiler.h new file +12 -0
new file mode 100644
index 000000000000..5cbcda94b0fe
@@ -0,0 +1,12 @@
+#ifndef _LIB_COMPILER_H_
+#define _LIB_COMPILER_H_
+
+#if GCC_VERSION >= 40500
+#define __noclone      __attribute__((__noclone__, __optimize__("no-tracer")))
+#endif /* GCC_VERSION >= 40500 */
+
+#if !defined(__noclone)
+#define __noclone
+#endif
+
+#endif /* _LIB_COMPILER_H_ */
x86/vmx_tests.c +2 -1
index 0e2c2f8a7d34..2dfc010d5d49 100644
@@ -5,6 +5,7 @@
  */
 
 #include <asm/debugreg.h>
+#include <compiler.h>
 
 #include "vmx.h"
 #include "msr.h"
@@ -4974,7 +4975,7 @@ extern unsigned char test_mtf1;
 extern unsigned char test_mtf2;
 extern unsigned char test_mtf3;
 
-__attribute__((noclone)) static void test_mtf_guest(void)
+__noclone static void test_mtf_guest(void)
 {
        asm ("vmcall;\n\t"
             "out %al, $0x80;\n\t"
Signature · 2 lines
Thanks,
Oliver
Quoted 27 lines · Bill Wendling

Don't use the "noclone" attribute for clang as it's not supported.

---
 x86/vmx_tests.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
x86/vmx_tests.c +4 -2
index ad8c002..ec88016 100644
@@ -4976,7 +4976,10 @@ extern unsigned char test_mtf1;
 extern unsigned char test_mtf2;
 extern unsigned char test_mtf3;
 
-__attribute__((noclone)) static void test_mtf_guest(void)
+#ifndef __clang__
+__attribute__((noclone))
+#endif
+static void test_mtf_guest(void)
 {
        asm ("vmcall;\n\t"
             "out %al, $0x80;\n\t"
--

2.25.1.481.gfbce0eb801-goog

maintainer
#46 lore raw
In reply to Bill Wendling
Quoted 2 lines · Bill Wendling

Changes to "pci: cast masks to uint32_t for unsigned long values" to cast the masks instead of changing the values in the header.

Applied 1-3-4 and a tweaked version of 2 and 7.

For 5, let's use -fwrapv instead.

For 6, clang people should stop implementing GCC extensions in half and declaring themselves compatible. It's just a sanity check, you can wrap it all with #ifndef __clang__.

Paolo