diff mbox series

[kvm-unit-tests] x86: check that clflushopt of an MMIO address succeeds

Message ID 20201103160855.261881-1-david.edmondson@oracle.com (mailing list archive)
State New, archived
Headers show
Series [kvm-unit-tests] x86: check that clflushopt of an MMIO address succeeds | expand

Commit Message

David Edmondson Nov. 3, 2020, 4:08 p.m. UTC
Verify that the clflushopt instruction succeeds when applied to an
MMIO address at both cpl0 and cpl3.

Suggested-by: Joao Martins <joao.m.martins@oracle.com>
Signed-off-by: David Edmondson <david.edmondson@oracle.com>
---
This is a test for the fix included in:
https://lore.kernel.org/r/20201103120400.240882-1-david.edmondson@oracle.com

 x86/Makefile.common   |  3 ++-
 x86/clflushopt_mmio.c | 58 +++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg     |  5 ++++
 3 files changed, 65 insertions(+), 1 deletion(-)
 create mode 100644 x86/clflushopt_mmio.c

Comments

Nadav Amit Nov. 5, 2020, 7:53 a.m. UTC | #1
> On Nov 3, 2020, at 8:08 AM, David Edmondson <david.edmondson@oracle.com> wrote:
> 
> Verify that the clflushopt instruction succeeds when applied to an
> MMIO address at both cpl0 and cpl3.
> 
> Suggested-by: Joao Martins <joao.m.martins@oracle.com>
> Signed-off-by: David Edmondson <david.edmondson@oracle.com>
> 

[snip]

> +	ret = pci_find_dev(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_TEST);
> +	if (ret != PCIDEVADDR_INVALID) {
> +		pci_dev_init(&pcidev, ret);

Just wondering, and perhaps this question is more general: does this test
really need the Red-Hat test device?

I know it is an emulated device, but can’t we use some other MMIO address
(e.g., PIT) that is also available on bare-metal?
David Edmondson Nov. 5, 2020, 10:20 a.m. UTC | #2
On Wednesday, 2020-11-04 at 23:53:01 -08, Nadav Amit wrote:

>> On Nov 3, 2020, at 8:08 AM, David Edmondson <david.edmondson@oracle.com> wrote:
>> 
>> Verify that the clflushopt instruction succeeds when applied to an
>> MMIO address at both cpl0 and cpl3.
>> 
>> Suggested-by: Joao Martins <joao.m.martins@oracle.com>
>> Signed-off-by: David Edmondson <david.edmondson@oracle.com>
>> 
>
> [snip]
>
>> +	ret = pci_find_dev(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_TEST);
>> +	if (ret != PCIDEVADDR_INVALID) {
>> +		pci_dev_init(&pcidev, ret);
>
> Just wondering, and perhaps this question is more general: does this test
> really need the Red-Hat test device?
>
> I know it is an emulated device, but can’t we use some other MMIO address
> (e.g., PIT) that is also available on bare-metal?

If it's acceptable to assume that HPET is present and at 0xfed00000,
then it could be used.

dme.
diff mbox series

Patch

diff --git a/x86/Makefile.common b/x86/Makefile.common
index b942086..e11666a 100644
--- a/x86/Makefile.common
+++ b/x86/Makefile.common
@@ -62,7 +62,8 @@  tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \
                $(TEST_DIR)/init.flat $(TEST_DIR)/smap.flat \
                $(TEST_DIR)/hyperv_synic.flat $(TEST_DIR)/hyperv_stimer.flat \
                $(TEST_DIR)/hyperv_connections.flat \
-               $(TEST_DIR)/umip.flat $(TEST_DIR)/tsx-ctrl.flat
+               $(TEST_DIR)/umip.flat $(TEST_DIR)/tsx-ctrl.flat \
+               $(TEST_DIR)/clflushopt_mmio.flat
 
 test_cases: $(tests-common) $(tests)
 
diff --git a/x86/clflushopt_mmio.c b/x86/clflushopt_mmio.c
new file mode 100644
index 0000000..06c1b32
--- /dev/null
+++ b/x86/clflushopt_mmio.c
@@ -0,0 +1,58 @@ 
+#include "libcflat.h"
+#include "usermode.h"
+#include "pci.h"
+#include "x86/asm/pci.h"
+
+static volatile int ud;
+static void *memaddr;
+
+static void handle_ud(struct ex_regs *regs)
+{
+	ud = 1;
+	regs->rip += 4;
+}
+
+static void try_clflushopt(const char *comment)
+{
+	int expected = !this_cpu_has(X86_FEATURE_CLFLUSHOPT);
+
+	ud = 0;
+	/* clflushopt (%rbx): */
+	asm volatile(".byte 0x66, 0x0f, 0xae, 0x3b" : : "b" (memaddr));
+
+	report(ud == expected, comment, expected ? "ABSENT" : "present");
+}
+
+static uint64_t user_clflushopt(void)
+{
+	try_clflushopt("clflushopt-mmio@cpl3 (%s)");
+
+	return 0;
+}
+
+int main(int ac, char **av)
+{
+	int ret;
+	struct pci_dev pcidev;
+	unsigned long membar = 0;
+	bool raised;
+
+	setup_vm();
+
+	ret = pci_find_dev(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_TEST);
+	if (ret != PCIDEVADDR_INVALID) {
+		pci_dev_init(&pcidev, ret);
+		assert(pci_bar_is_memory(&pcidev, PCI_TESTDEV_BAR_MEM));
+		membar = pcidev.resource[PCI_TESTDEV_BAR_MEM];
+		memaddr = ioremap(membar, PAGE_SIZE);
+		printf("pci-testdev at %#x membar %lx (@%p)\n",
+		       pcidev.bdf, membar, memaddr);
+
+		handle_exception(UD_VECTOR, handle_ud);
+
+		(void) run_in_user(user_clflushopt, false, 0, 0, 0, 0, &raised);
+		try_clflushopt("clflushopt-mmio@cpl0 (%s)");
+	}
+
+	return report_summary();
+}
diff --git a/x86/unittests.cfg b/x86/unittests.cfg
index 872d679..35bedf8 100644
--- a/x86/unittests.cfg
+++ b/x86/unittests.cfg
@@ -359,3 +359,8 @@  extra_params = -M q35,kernel-irqchip=split -device intel-iommu,intremap=on,eim=o
 file = tsx-ctrl.flat
 extra_params = -cpu host
 groups = tsx-ctrl
+
+[clflushopt_mmio]
+file = clflushopt_mmio.flat
+extra_params = -cpu host
+arch = x86_64