Skip to contentSkip to conversation

KVM: selftests: add powerpc support

PATCH Superseded Nicholas Piggin 3 participants 9 messages Jun 8, 2023 – Aug 2, 2023

Subsystems (5)
KVM Kselftest RISC-V KVM PowerPC +1 more subsystems

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Nicholas Piggin

author reviewer
#0 lore raw

v3 cover KVM: selftests: add powerpc support

This series adds initial KVM selftests support for powerpc (64-bit, BookS, radix MMU).

Since v2:
- Added a couple of new tests (patch 5,6)
- Make default page size match host page size.
- Check for radix MMU capability.
- Build a few more of the generic tests.

Since v1:
- Update MAINTAINERS KVM PPC entry to include kvm selftests.
- Fixes and cleanups from Sean's review including new patch 1.
- Add 4K guest page support requiring new patch 2.

Thanks,
Nick

Nicholas Piggin (6):
KVM: selftests: Move pgd_created check into virt_pgd_alloc
KVM: selftests: Add aligned guest physical page allocator
KVM: PPC: selftests: add support for powerpc
KVM: PPC: selftests: add selftests sanity tests
KVM: PPC: selftests: Add a TLBIEL virtualisation tester
KVM: PPC: selftests: Add interrupt performance tester

 MAINTAINERS                                   |   2 +
 tools/testing/selftests/kvm/Makefile          |  23 +
 .../selftests/kvm/include/kvm_util_base.h     |  29 +
 .../selftests/kvm/include/powerpc/hcall.h     |  21 +
 .../selftests/kvm/include/powerpc/ppc_asm.h   |  32 ++
 .../selftests/kvm/include/powerpc/processor.h |  46 ++
 .../selftests/kvm/lib/aarch64/processor.c     |   4 -
 tools/testing/selftests/kvm/lib/guest_modes.c |  27 +-
 tools/testing/selftests/kvm/lib/kvm_util.c    |  56 +-
 .../selftests/kvm/lib/powerpc/handlers.S      |  93 +++
 .../testing/selftests/kvm/lib/powerpc/hcall.c |  45 ++
 .../selftests/kvm/lib/powerpc/processor.c     | 541 ++++++++++++++++++
 .../testing/selftests/kvm/lib/powerpc/ucall.c |  30 +
 .../selftests/kvm/lib/riscv/processor.c       |   4 -
 .../selftests/kvm/lib/s390x/processor.c       |   4 -
 .../selftests/kvm/lib/x86_64/processor.c      |   7 +-
 tools/testing/selftests/kvm/powerpc/helpers.h |  46 ++
 .../selftests/kvm/powerpc/interrupt_perf.c    | 199 +++++++
 .../testing/selftests/kvm/powerpc/null_test.c | 166 ++++++
 .../selftests/kvm/powerpc/rtas_hcall.c        | 136 +++++
 .../selftests/kvm/powerpc/tlbiel_test.c       | 508 ++++++++++++++++
 21 files changed, 1981 insertions(+), 38 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/include/powerpc/hcall.h
 create mode 100644 tools/testing/selftests/kvm/include/powerpc/ppc_asm.h
 create mode 100644 tools/testing/selftests/kvm/include/powerpc/processor.h
 create mode 100644 tools/testing/selftests/kvm/lib/powerpc/handlers.S
 create mode 100644 tools/testing/selftests/kvm/lib/powerpc/hcall.c
 create mode 100644 tools/testing/selftests/kvm/lib/powerpc/processor.c
 create mode 100644 tools/testing/selftests/kvm/lib/powerpc/ucall.c
 create mode 100644 tools/testing/selftests/kvm/powerpc/helpers.h
 create mode 100644 tools/testing/selftests/kvm/powerpc/interrupt_perf.c
 create mode 100644 tools/testing/selftests/kvm/powerpc/null_test.c
 create mode 100644 tools/testing/selftests/kvm/powerpc/rtas_hcall.c
 create mode 100644 tools/testing/selftests/kvm/powerpc/tlbiel_test.c
Signature · 1 line
2.40.1
author reviewer
#1 lore raw

PATCH v3 1/6 KVM: selftests: Move pgd_created check into virt_pgd_alloc

virt_arch_pgd_alloc all do the same test and set of pgd_created. Move this into common code.

---
 tools/testing/selftests/kvm/include/kvm_util_base.h | 5 +++++
 tools/testing/selftests/kvm/lib/aarch64/processor.c | 4 ----
 tools/testing/selftests/kvm/lib/riscv/processor.c   | 4 ----
 tools/testing/selftests/kvm/lib/s390x/processor.c   | 4 ----
 tools/testing/selftests/kvm/lib/x86_64/processor.c  | 7 ++-----
 5 files changed, 7 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +5 -0
index a089c356f354..d630a0a1877c 100644
@@ -822,7 +822,12 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm);
 
 static inline void virt_pgd_alloc(struct kvm_vm *vm)
 {
+	if (vm->pgd_created)
+		return;
+
 	virt_arch_pgd_alloc(vm);
+
+	vm->pgd_created = true;
 }
 
 /*
tools/testing/selftests/kvm/lib/aarch64/processor.c +0 -4
index 3a0259e25335..3da3ec7c5b23 100644
@@ -96,13 +96,9 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)
 {
 	size_t nr_pages = page_align(vm, ptrs_per_pgd(vm) * 8) / vm->page_size;
 
-	if (vm->pgd_created)
-		return;
-
 	vm->pgd = vm_phy_pages_alloc(vm, nr_pages,
 				     KVM_GUEST_PAGE_TABLE_MIN_PADDR,
 				     vm->memslots[MEM_REGION_PT]);
-	vm->pgd_created = true;
 }
 
 static void _virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
tools/testing/selftests/kvm/lib/riscv/processor.c +0 -4
index d146ca71e0c0..7695ba2cd369 100644
@@ -57,13 +57,9 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)
 {
 	size_t nr_pages = page_align(vm, ptrs_per_pte(vm) * 8) / vm->page_size;
 
-	if (vm->pgd_created)
-		return;
-
 	vm->pgd = vm_phy_pages_alloc(vm, nr_pages,
 				     KVM_GUEST_PAGE_TABLE_MIN_PADDR,
 				     vm->memslots[MEM_REGION_PT]);
-	vm->pgd_created = true;
 }
 
 void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
tools/testing/selftests/kvm/lib/s390x/processor.c +0 -4
index 15945121daf1..358e03f09c7a 100644
@@ -17,16 +17,12 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)
 	TEST_ASSERT(vm->page_size == 4096, "Unsupported page size: 0x%x",
 		    vm->page_size);
 
-	if (vm->pgd_created)
-		return;
-
 	paddr = vm_phy_pages_alloc(vm, PAGES_PER_REGION,
 				   KVM_GUEST_PAGE_TABLE_MIN_PADDR,
 				   vm->memslots[MEM_REGION_PT]);
 	memset(addr_gpa2hva(vm, paddr), 0xff, PAGES_PER_REGION * vm->page_size);
 
 	vm->pgd = paddr;
-	vm->pgd_created = true;
 }
 
 /*
tools/testing/selftests/kvm/lib/x86_64/processor.c +2 -5
index d4a0b504b1e0..d4deb2718e86 100644
@@ -127,11 +127,8 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)
 	TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
 		"unknown or unsupported guest mode, mode: 0x%x", vm->mode);
 
-	/* If needed, create page map l4 table. */
-	if (!vm->pgd_created) {
-		vm->pgd = vm_alloc_page_table(vm);
-		vm->pgd_created = true;
-	}
+	/* Create page map l4 table. */
+	vm->pgd = vm_alloc_page_table(vm);
 }
 
 static void *virt_get_pte(struct kvm_vm *vm, uint64_t *parent_pte,
Signature · 1 line
2.40.1
author reviewer
#2 lore raw

PATCH v3 2/6 KVM: selftests: Add aligned guest physical page allocator

In reply to Nicholas Piggin

powerpc will require this to allocate MMU tables in guest memory that are larger than guest base page size.

---
 .../selftests/kvm/include/kvm_util_base.h     |  2 +
 tools/testing/selftests/kvm/lib/kvm_util.c    | 44 ++++++++++++-------
 2 files changed, 29 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
index d630a0a1877c..42d03ae08ecb 100644
@@ -680,6 +680,8 @@ const char *exit_reason_str(unsigned int exit_reason);
 
 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
 			     uint32_t memslot);
+vm_paddr_t vm_phy_pages_alloc_align(struct kvm_vm *vm, size_t num, size_t align,
+			      vm_paddr_t paddr_min, uint32_t memslot);
 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
 			      vm_paddr_t paddr_min, uint32_t memslot);
 vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm);
tools/testing/selftests/kvm/lib/kvm_util.c +27 -17

70 lines not on this page · raw message

Signature · 1 line
2.40.1
author reviewer
#3 lore raw

PATCH v3 3/6 KVM: PPC: selftests: add support for powerpc

In reply to Nicholas Piggin

Implement KVM selftests support for powerpc (Book3S-64).

ucalls are implemented with an unsuppored PAPR hcall number which will always cause KVM to exit to userspace.

Virtual memory is implemented for the radix MMU, and only a base page size is supported (both 4K and 64K).

Guest interrupts are taken in real-mode, so require a page allocated at gRA 0x0. Interrupt entry is complicated because gVA:gRA is not 1:1 mapped (like the kernel is), so the MMU can not just just be switched on and off.

---
 MAINTAINERS                                   |   2 +
 tools/testing/selftests/kvm/Makefile          |  19 +
 .../selftests/kvm/include/kvm_util_base.h     |  22 +
 .../selftests/kvm/include/powerpc/hcall.h     |  19 +
 .../selftests/kvm/include/powerpc/ppc_asm.h   |  32 ++
 .../selftests/kvm/include/powerpc/processor.h |  39 ++
 tools/testing/selftests/kvm/lib/guest_modes.c |  27 +-
 tools/testing/selftests/kvm/lib/kvm_util.c    |  12 +
 .../selftests/kvm/lib/powerpc/handlers.S      |  93 ++++
 .../testing/selftests/kvm/lib/powerpc/hcall.c |  45 ++
 .../selftests/kvm/lib/powerpc/processor.c     | 439 ++++++++++++++++++
 .../testing/selftests/kvm/lib/powerpc/ucall.c |  30 ++
 tools/testing/selftests/kvm/powerpc/helpers.h |  46 ++
 13 files changed, 821 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/include/powerpc/hcall.h
 create mode 100644 tools/testing/selftests/kvm/include/powerpc/ppc_asm.h
 create mode 100644 tools/testing/selftests/kvm/include/powerpc/processor.h
 create mode 100644 tools/testing/selftests/kvm/lib/powerpc/handlers.S
 create mode 100644 tools/testing/selftests/kvm/lib/powerpc/hcall.c
 create mode 100644 tools/testing/selftests/kvm/lib/powerpc/processor.c
 create mode 100644 tools/testing/selftests/kvm/lib/powerpc/ucall.c
 create mode 100644 tools/testing/selftests/kvm/powerpc/helpers.h
MAINTAINERS +2 -0
index 44417acd2936..39afb356369e 100644
@@ -11391,6 +11391,8 @@ F:	arch/powerpc/include/asm/kvm*
 F:	arch/powerpc/include/uapi/asm/kvm*
 F:	arch/powerpc/kernel/kvm*
 F:	arch/powerpc/kvm/
+F:	tools/testing/selftests/kvm/*/powerpc/
+F:	tools/testing/selftests/kvm/powerpc/
 
 KERNEL VIRTUAL MACHINE FOR RISC-V (KVM/riscv)
 M:	Anup Patel <[email protected]>
tools/testing/selftests/kvm/Makefile +19 -0
index 4761b768b773..53cd3ce63dec 100644
@@ -55,6 +55,11 @@ LIBKVM_s390x += lib/s390x/ucall.c
 LIBKVM_riscv += lib/riscv/processor.c
 LIBKVM_riscv += lib/riscv/ucall.c
 
+LIBKVM_powerpc += lib/powerpc/handlers.S
+LIBKVM_powerpc += lib/powerpc/processor.c
+LIBKVM_powerpc += lib/powerpc/ucall.c
+LIBKVM_powerpc += lib/powerpc/hcall.c
+
 # Non-compiled test targets
 TEST_PROGS_x86_64 += x86_64/nx_huge_pages_test.sh
 
@@ -179,6 +184,20 @@ TEST_GEN_PROGS_riscv += kvm_page_table_test
 TEST_GEN_PROGS_riscv += set_memory_region_test
 TEST_GEN_PROGS_riscv += kvm_binary_stats_test
 
+TEST_GEN_PROGS_powerpc += access_tracking_perf_test
+TEST_GEN_PROGS_powerpc += demand_paging_test
+TEST_GEN_PROGS_powerpc += dirty_log_test
+TEST_GEN_PROGS_powerpc += dirty_log_perf_test
+TEST_GEN_PROGS_powerpc += hardware_disable_test
+TEST_GEN_PROGS_powerpc += kvm_create_max_vcpus
+TEST_GEN_PROGS_powerpc += kvm_page_table_test
+TEST_GEN_PROGS_powerpc += max_guest_memory_test
+TEST_GEN_PROGS_powerpc += memslot_modification_stress_test
+TEST_GEN_PROGS_powerpc += memslot_perf_test
+TEST_GEN_PROGS_powerpc += rseq_test
+TEST_GEN_PROGS_powerpc += set_memory_region_test
+TEST_GEN_PROGS_powerpc += kvm_binary_stats_test
+
 TEST_PROGS += $(TEST_PROGS_$(ARCH_DIR))
 TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(ARCH_DIR))
 TEST_GEN_PROGS_EXTENDED += $(TEST_GEN_PROGS_EXTENDED_$(ARCH_DIR))
tools/testing/selftests/kvm/include/kvm_util_base.h +22 -0
index 42d03ae08ecb..17b80709b894 100644
@@ -105,6 +105,7 @@ struct kvm_vm {
 	bool pgd_created;
 	vm_paddr_t ucall_mmio_addr;
 	vm_paddr_t pgd;
+	vm_paddr_t prtb; // powerpc process table
 	vm_vaddr_t gdt;
 	vm_vaddr_t tss;
 	vm_vaddr_t idt;
@@ -160,6 +161,8 @@ enum vm_guest_mode {
 	VM_MODE_PXXV48_4K,	/* For 48bits VA but ANY bits PA */
 	VM_MODE_P47V64_4K,
 	VM_MODE_P44V64_4K,
+	VM_MODE_P52V52_4K,
+	VM_MODE_P52V52_64K,
 	VM_MODE_P36V48_4K,
 	VM_MODE_P36V48_16K,
 	VM_MODE_P36V48_64K,
@@ -197,6 +200,25 @@ extern enum vm_guest_mode vm_mode_default;
 #define MIN_PAGE_SHIFT			12U
 #define ptes_per_page(page_size)	((page_size) / 8)
 
+#elif defined(__powerpc64__)
+
+extern enum vm_guest_mode vm_mode_default;
+
+#define VM_MODE_DEFAULT			vm_mode_default
+
+/*
+ * XXX: This is a hack to allocate more memory for page tables because we
+ * don't pack "fragments" well with 64K page sizes. Should rework generic
+ * code to allow more flexible page table memory estimation (and fix our
+ * page table allocation).
+ */
+#define MIN_PAGE_SHIFT			12U
+#define ptes_per_page(page_size)	((page_size) / 8)
+
+#else
+
+#error "KVM selftests not implemented for architecture"
+
 #endif
 
 #define MIN_PAGE_SIZE		(1U << MIN_PAGE_SHIFT)
tools/testing/selftests/kvm/include/powerpc/hcall.h new file +19 -0
new file mode 100644
index 000000000000..ba119f5a3fef
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * powerpc hcall defines
+ */
+#ifndef SELFTEST_KVM_HCALL_H
+#define SELFTEST_KVM_HCALL_H
+
+#include <linux/compiler.h>
+
+/* Ucalls use unimplemented PAPR hcall 0 which exits KVM */
+#define H_UCALL	0
+#define UCALL_R4_UCALL	0x5715 // regular ucall, r5 contains ucall pointer
+#define UCALL_R4_SIMPLE	0x0000 // simple exit usable by asm with no ucall data
+
+int64_t hcall0(uint64_t token);
+int64_t hcall1(uint64_t token, uint64_t arg1);
+int64_t hcall2(uint64_t token, uint64_t arg1, uint64_t arg2);
+
+#endif
tools/testing/selftests/kvm/include/powerpc/ppc_asm.h new file +32 -0
new file mode 100644
index 000000000000..b9df64659792
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * powerpc asm specific defines
+ */
+#ifndef SELFTEST_KVM_PPC_ASM_H
+#define SELFTEST_KVM_PPC_ASM_H
+
+#define STACK_FRAME_MIN_SIZE	112 /* Could be 32 on ELFv2 */
+#define STACK_REDZONE_SIZE	512
+
+#define INT_FRAME_SIZE		(STACK_FRAME_MIN_SIZE + STACK_REDZONE_SIZE)
+
+#define SPR_SRR0	0x01a
+#define SPR_SRR1	0x01b
+#define SPR_CFAR	0x01c
+
+#define MSR_SF		0x8000000000000000ULL
+#define MSR_HV		0x1000000000000000ULL
+#define MSR_VEC		0x0000000002000000ULL
+#define MSR_VSX		0x0000000000800000ULL
+#define MSR_EE		0x0000000000008000ULL
+#define MSR_PR		0x0000000000004000ULL
+#define MSR_FP		0x0000000000002000ULL
+#define MSR_ME		0x0000000000001000ULL
+#define MSR_IR		0x0000000000000020ULL
+#define MSR_DR		0x0000000000000010ULL
+#define MSR_RI		0x0000000000000002ULL
+#define MSR_LE		0x0000000000000001ULL
+
+#define LPCR_ILE	0x0000000002000000ULL
+
+#endif
tools/testing/selftests/kvm/include/powerpc/processor.h new file +39 -0
new file mode 100644
index 000000000000..ce5a23525dbd
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * powerpc processor specific defines
+ */
+#ifndef SELFTEST_KVM_PROCESSOR_H
+#define SELFTEST_KVM_PROCESSOR_H
+
+#include <linux/compiler.h>
+#include "ppc_asm.h"
+
+extern unsigned char __interrupts_start[];
+extern unsigned char __interrupts_end[];
+
+struct kvm_vm;
+struct kvm_vcpu;
+extern bool (*interrupt_handler)(struct kvm_vcpu *vcpu, unsigned trap);
+
+struct ex_regs {
+	uint64_t	gprs[32];
+	uint64_t	nia;
+	uint64_t	msr;
+	uint64_t	cfar;
+	uint64_t	lr;
+	uint64_t	ctr;
+	uint64_t	xer;
+	uint32_t	cr;
+	uint32_t	trap;
+	uint64_t	vaddr; /* vaddr of this struct */
+};
+
+void vm_install_exception_handler(struct kvm_vm *vm, int vector,
+			void (*handler)(struct ex_regs *));
+
+static inline void cpu_relax(void)
+{
+	asm volatile("" ::: "memory");
+}
+
+#endif
tools/testing/selftests/kvm/lib/guest_modes.c +23 -4
index 1df3ce4b16fd..4dfaed1706d9 100644
@@ -4,7 +4,11 @@
  */
 #include "guest_modes.h"
 
-#ifdef __aarch64__
+#if defined(__powerpc__)
+#include <unistd.h>
+#endif
+
+#if defined(__aarch64__) || defined(__powerpc__)
 #include "processor.h"
 enum vm_guest_mode vm_mode_default;
 #endif
@@ -13,9 +17,7 @@ struct guest_mode guest_modes[NUM_VM_MODES];
 
 void guest_modes_append_default(void)
 {
-#ifndef __aarch64__
-	guest_mode_append(VM_MODE_DEFAULT, true, true);
-#else
+#ifdef __aarch64__
 	{
 		unsigned int limit = kvm_check_cap(KVM_CAP_ARM_VM_IPA_SIZE);
 		bool ps4k, ps16k, ps64k;
@@ -70,6 +72,8 @@ void guest_modes_append_default(void)
 				    KVM_S390_VM_CPU_PROCESSOR, &info);
 		close(vm_fd);
 		close(kvm_fd);
+
+		guest_mode_append(VM_MODE_DEFAULT, true, true);
 		/* Starting with z13 we have 47bits of physical address */
 		if (info.ibc >= 0x30)
 			guest_mode_append(VM_MODE_P47V64_4K, true, true);
@@ -79,12 +83,27 @@ void guest_modes_append_default(void)
 	{
 		unsigned int sz = kvm_check_cap(KVM_CAP_VM_GPA_BITS);
 
+		guest_mode_append(VM_MODE_DEFAULT, true, true);
 		if (sz >= 52)
 			guest_mode_append(VM_MODE_P52V48_4K, true, true);
 		if (sz >= 48)
 			guest_mode_append(VM_MODE_P48V48_4K, true, true);
 	}
 #endif
+#ifdef __powerpc__
+	{
+		TEST_ASSERT(kvm_check_cap(KVM_CAP_PPC_MMU_RADIX),
+			    "Radix MMU not available, KVM selftests "
+			    "does not support Hash MMU!");
+		/* Radix guest EA and RA are 52-bit on POWER9 and POWER10 */
+		if (sysconf(_SC_PAGESIZE) == 4096)
+			vm_mode_default = VM_MODE_P52V52_4K;
+		else
+			vm_mode_default = VM_MODE_P52V52_64K;
+		guest_mode_append(VM_MODE_P52V52_4K, true, true);
+		guest_mode_append(VM_MODE_P52V52_64K, true, true);
+	}
+#endif
 }
 
 void for_each_guest_mode(void (*func)(enum vm_guest_mode, void *), void *arg)
tools/testing/selftests/kvm/lib/kvm_util.c +12 -0
index 68558d60f949..696989a22c5a 100644
@@ -158,6 +158,8 @@ const char *vm_guest_mode_string(uint32_t i)
 		[VM_MODE_PXXV48_4K]	= "PA-bits:ANY, VA-bits:48,  4K pages",
 		[VM_MODE_P47V64_4K]	= "PA-bits:47,  VA-bits:64,  4K pages",
 		[VM_MODE_P44V64_4K]	= "PA-bits:44,  VA-bits:64,  4K pages",
+		[VM_MODE_P52V52_4K]	= "PA-bits:52,  VA-bits:52,  4K pages",
+		[VM_MODE_P52V52_64K]	= "PA-bits:52,  VA-bits:52, 64K pages",
 		[VM_MODE_P36V48_4K]	= "PA-bits:36,  VA-bits:48,  4K pages",
 		[VM_MODE_P36V48_16K]	= "PA-bits:36,  VA-bits:48, 16K pages",
 		[VM_MODE_P36V48_64K]	= "PA-bits:36,  VA-bits:48, 64K pages",
@@ -183,6 +185,8 @@ const struct vm_guest_mode_params vm_guest_mode_params[] = {
 	[VM_MODE_PXXV48_4K]	= {  0,  0,  0x1000, 12 },
 	[VM_MODE_P47V64_4K]	= { 47, 64,  0x1000, 12 },
 	[VM_MODE_P44V64_4K]	= { 44, 64,  0x1000, 12 },
+	[VM_MODE_P52V52_4K]	= { 52, 52,  0x1000, 12 },
+	[VM_MODE_P52V52_64K]	= { 52, 52, 0x10000, 16 },
 	[VM_MODE_P36V48_4K]	= { 36, 48,  0x1000, 12 },
 	[VM_MODE_P36V48_16K]	= { 36, 48,  0x4000, 14 },
 	[VM_MODE_P36V48_64K]	= { 36, 48, 0x10000, 16 },
@@ -284,6 +288,14 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode)
 	case VM_MODE_P44V64_4K:
 		vm->pgtable_levels = 5;
 		break;
+#ifdef __powerpc__
+	case VM_MODE_P52V52_64K:
+		vm->pgtable_levels = 4;
+		break;
+	case VM_MODE_P52V52_4K:
+		vm->pgtable_levels = 4;
+		break;
+#endif
 	default:
 		TEST_FAIL("Unknown guest mode, mode: 0x%x", mode);
 	}
tools/testing/selftests/kvm/lib/powerpc/handlers.S new file +93 -0

93 lines not on this page · raw message

tools/testing/selftests/kvm/lib/powerpc/hcall.c new file +45 -0

45 lines not on this page · raw message

tools/testing/selftests/kvm/lib/powerpc/processor.c new file +439 -0

439 lines not on this page · raw message

tools/testing/selftests/kvm/lib/powerpc/ucall.c new file +30 -0
new file mode 100644
index 000000000000..ce0ddde45fef
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ucall support. A ucall is a "hypercall to host userspace".
+ */
+#include "kvm_util.h"
+#include "hcall.h"
+
+void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa)
+{
+}
+
+void ucall_arch_do_ucall(vm_vaddr_t uc)
+{
+	hcall2(H_UCALL, UCALL_R4_UCALL, (uintptr_t)(uc));
+}
+
+void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
+{
+	struct kvm_run *run = vcpu->run;
+
+	if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
+	    run->papr_hcall.nr == H_UCALL) {
+		struct kvm_regs regs;
+
+		vcpu_regs_get(vcpu, &regs);
+		if (regs.gpr[4] == UCALL_R4_UCALL)
+			return (void *)regs.gpr[5];
+	}
+	return NULL;
+}
tools/testing/selftests/kvm/powerpc/helpers.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.40.1
author reviewer
#6 lore raw

PATCH v3 4/6 KVM: PPC: selftests: add selftests sanity tests

In reply to Nicholas Piggin

Add tests that exercise very basic functions of the kvm selftests framework, guest creation, ucalls, hcalls, copying data between guest and host, interrupts and page faults.

These don't stress KVM so much as being useful when developing support for powerpc.

---
 tools/testing/selftests/kvm/Makefile          |   2 +
 .../selftests/kvm/include/powerpc/hcall.h     |   2 +
 .../testing/selftests/kvm/powerpc/null_test.c | 166 ++++++++++++++++++
 .../selftests/kvm/powerpc/rtas_hcall.c        | 136 ++++++++++++++
 4 files changed, 306 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/powerpc/null_test.c
 create mode 100644 tools/testing/selftests/kvm/powerpc/rtas_hcall.c
tools/testing/selftests/kvm/Makefile +2 -0
index 53cd3ce63dec..efb8700b9752 100644
@@ -184,6 +184,8 @@ TEST_GEN_PROGS_riscv += kvm_page_table_test
 TEST_GEN_PROGS_riscv += set_memory_region_test
 TEST_GEN_PROGS_riscv += kvm_binary_stats_test
 
+TEST_GEN_PROGS_powerpc += powerpc/null_test
+TEST_GEN_PROGS_powerpc += powerpc/rtas_hcall
 TEST_GEN_PROGS_powerpc += access_tracking_perf_test
 TEST_GEN_PROGS_powerpc += demand_paging_test
 TEST_GEN_PROGS_powerpc += dirty_log_test
tools/testing/selftests/kvm/include/powerpc/hcall.h +2 -0
index ba119f5a3fef..04c7d2d13020 100644
@@ -12,6 +12,8 @@
 #define UCALL_R4_UCALL	0x5715 // regular ucall, r5 contains ucall pointer
 #define UCALL_R4_SIMPLE	0x0000 // simple exit usable by asm with no ucall data
 
+#define H_RTAS		0xf000
+
 int64_t hcall0(uint64_t token);
 int64_t hcall1(uint64_t token, uint64_t arg1);
 int64_t hcall2(uint64_t token, uint64_t arg1, uint64_t arg2);
tools/testing/selftests/kvm/powerpc/null_test.c new file +166 -0

166 lines not on this page · raw message

tools/testing/selftests/kvm/powerpc/rtas_hcall.c new file +136 -0

136 lines not on this page · raw message

Signature · 1 line
2.40.1
author reviewer
#7 lore raw

PATCH v3 5/6 KVM: PPC: selftests: Add a TLBIEL virtualisation tester

In reply to Nicholas Piggin

TLBIEL virtualisation has been a source of difficulty. The TLBIEL instruction operates on the TLB of the hardware thread which executes it, but the behaviour expected by the guest environment

---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/include/powerpc/processor.h |   7 +
 .../selftests/kvm/lib/powerpc/processor.c     | 108 +++-
 .../selftests/kvm/powerpc/tlbiel_test.c       | 508 ++++++++++++++++++
 4 files changed, 621 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/powerpc/tlbiel_test.c
tools/testing/selftests/kvm/Makefile +1 -0
index efb8700b9752..aa3a8ca676c2 100644
@@ -186,6 +186,7 @@ TEST_GEN_PROGS_riscv += kvm_binary_stats_test
 
 TEST_GEN_PROGS_powerpc += powerpc/null_test
 TEST_GEN_PROGS_powerpc += powerpc/rtas_hcall
+TEST_GEN_PROGS_powerpc += powerpc/tlbiel_test
 TEST_GEN_PROGS_powerpc += access_tracking_perf_test
 TEST_GEN_PROGS_powerpc += demand_paging_test
 TEST_GEN_PROGS_powerpc += dirty_log_test
tools/testing/selftests/kvm/include/powerpc/processor.h +7 -0
index ce5a23525dbd..92ef6476a9ef 100644
@@ -7,6 +7,7 @@
 
 #include <linux/compiler.h>
 #include "ppc_asm.h"
+#include "kvm_util_base.h"
 
 extern unsigned char __interrupts_start[];
 extern unsigned char __interrupts_end[];
@@ -31,6 +32,12 @@ struct ex_regs {
 void vm_install_exception_handler(struct kvm_vm *vm, int vector,
 			void (*handler)(struct ex_regs *));
 
+vm_paddr_t virt_pt_duplicate(struct kvm_vm *vm);
+void set_radix_proc_table(struct kvm_vm *vm, int pid, vm_paddr_t pgd);
+bool virt_wrprotect_pte(struct kvm_vm *vm, uint64_t gva);
+bool virt_wrenable_pte(struct kvm_vm *vm, uint64_t gva);
+bool virt_remap_pte(struct kvm_vm *vm, uint64_t gva, vm_paddr_t gpa);
+
 static inline void cpu_relax(void)
 {
 	asm volatile("" ::: "memory");
tools/testing/selftests/kvm/lib/powerpc/processor.c +105 -3

134 lines not on this page · raw message

tools/testing/selftests/kvm/powerpc/tlbiel_test.c new file +508 -0

508 lines not on this page · raw message

Signature · 1 line
2.40.1
author reviewer
#8 lore raw

PATCH v3 6/6 KVM: PPC: selftests: Add interrupt performance tester

In reply to Nicholas Piggin

Add a little perf tester for interrupts that go to guest, host, and userspace.

---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/powerpc/interrupt_perf.c    | 199 ++++++++++++++++++
 2 files changed, 200 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/powerpc/interrupt_perf.c
tools/testing/selftests/kvm/Makefile +1 -0
index aa3a8ca676c2..834f98971b0c 100644
@@ -184,6 +184,7 @@ TEST_GEN_PROGS_riscv += kvm_page_table_test
 TEST_GEN_PROGS_riscv += set_memory_region_test
 TEST_GEN_PROGS_riscv += kvm_binary_stats_test
 
+TEST_GEN_PROGS_powerpc += powerpc/interrupt_perf
 TEST_GEN_PROGS_powerpc += powerpc/null_test
 TEST_GEN_PROGS_powerpc += powerpc/rtas_hcall
 TEST_GEN_PROGS_powerpc += powerpc/tlbiel_test
tools/testing/selftests/kvm/powerpc/interrupt_perf.c new file +199 -0

199 lines not on this page · raw message

Signature · 1 line
2.40.1
#4 lore raw

Re: [PATCH v3 3/6] KVM: PPC: selftests: add support for powerpc

In reply to Nicholas Piggin
Quoted 6 lines · Nicholas Piggin

Implement KVM selftests support for powerpc (Book3S-64).

ucalls are implemented with an unsuppored PAPR hcall number which will always cause KVM to exit to userspace.

Quoted 7 lines

Virtual memory is implemented for the radix MMU, and only a base page size is supported (both 4K and 64K).

Guest interrupts are taken in real-mode, so require a page allocated at gRA 0x0. Interrupt entry is complicated because gVA:gRA is not 1:1 mapped (like the kernel is), so the MMU can not just just be switched on and off.

I saw a few failures on a power9 running Ubuntu's 6.2.0-20-generic kernel:

# selftests: kvm: kvm_create_max_vcpus
# KVM_CAP_MAX_VCPU_ID: 16384
# KVM_CAP_MAX_VCPUS: 2048
# Testing creating 2048 vCPUs, with IDs 0...2047. # Testing creating 2048 vCPUs, with IDs 14336...16383. # ==== Test Assertion Failure ====
# lib/kvm_util.c:1221: vcpu->fd >= 0
# pid=40390 tid=40390 errno=22 - Invalid argument

#      1    0x0000000010006903: __vm_vcpu_add at kvm_util.c:1221
#      2    0x0000000010002e53: test_vcpu_creation at
kvm_create_max_vcpus.c:35 (discriminator 3)
#      3    0x0000000010002953: main at kvm_create_max_vcpus.c:90
#      4    0x0000795fb3224c23: ?? ??:0
#      5    0x0000795fb3224e6b: ?? ??:0

# KVM_CREATE_VCPU failed, rc: -1 errno: 22 (Invalid argument)
not ok 10 selftests: kvm: kvm_create_max_vcpus # exit=254

# selftests: kvm: max_guest_memory_test
# No guest physical pages available, paddr_min: 0x180000 page_size:
0x10000 memslot: 0 num_pages: 1 align: 1
# ---- vm dump ----

#   mode: 0xc
#   fd: 6
#   page_size: 0x10000
#   Mem Regions:

# guest_phys: 0x0 size: 0x4200000 host_virt: 0x7ce4c0800000
# unused_phy_pages: 0x1
# Mapped Virtual Pages:
# 0x1, 0xac:0x183, 0x1000:0x1003
# pgd_created: 1
# Virtual Translation Tables:
# Virtual Translation Tables:
# PDE1[0] gVA:0x0000000000000000
# PDE2[0] gVA:0x0000000000000000
# PDE3[0] gVA:0x0000000000000000
# PTE[1] gVA:0x0000000000010000 -> gRA:0x0000000000060000
# PDE3[5] gVA:0x0000000000a00000
# PTE[12] gVA:0x0000000000ac0000 -> gRA:0x0000000000070000

and then finally:

# ==== Test Assertion Failure ====
# lib/kvm_util.c:1962: false
# pid=40446 tid=40446 errno=0 - Success

#      1    0x0000000010008f83: vm_phy_pages_alloc_align at kvm_util.c:1962
#      2    0x00000000100110d3: __virt_arch_pg_map at processor.c:232
#      3    0x0000000010002bcf: virt_pg_map at kvm_util_base.h:877
(discriminator 3)
#      4     (inlined by) main at max_guest_memory_test.c:249 (discriminator 3)
#      5    0x00007ce4c4a24c23: ?? ??:0
#      6    0x00007ce4c4a24e6b: ?? ??:0

# false
not ok 12 selftests: kvm: max_guest_memory_test # exit=254

# selftests: kvm: rseq_test
# ==== Test Assertion Failure ====
# rseq_test.c:258: i > (NR_TASK_MIGRATIONS / 2)
# pid=40529 tid=40529 errno=4 - Interrupted system call

#      1    0x0000000010002e57: main at rseq_test.c:258
#      2    0x00007369e2824c23: ?? ??:0
#      3    0x00007369e2824e6b: ?? ??:0

# Only performed 32590 KVM_RUNs, task stalled too much?
#
not ok 15 selftests: kvm: rseq_test # exit=254

I have attached the log from the full test run.

tools/testing/selftests/kvm/lib/guest_modes.c void guest_modes_append_default(void) · line 97

Quoted 9 lines
tools/testing/selftests/kvm/lib/guest_modes.c +5 -0
+#ifdef __powerpc__
+       {
+               TEST_ASSERT(kvm_check_cap(KVM_CAP_PPC_MMU_RADIX),
+                           "Radix MMU not available, KVM selftests "
+                           "does not support Hash MMU!");

Back on the power8 system, this produces a backtrace along with the warning:

TAP version 13
1..17
# selftests: kvm: interrupt_perf
# ==== Test Assertion Failure ====
# lib/guest_modes.c:95: kvm_check_cap(KVM_CAP_PPC_MMU_RADIX)
# pid=3800487 tid=3800487 errno=0 - Success

#      1    0x0000000010003d57: guest_modes_append_default at guest_modes.c:95
#      2    0x0000000010011d67: kvm_selftest_arch_init at processor.c:540
#      3    0x00000000100029af: kvm_selftest_init at kvm_util.c:2178
#      4    0x000000001001325b: __libc_csu_init at ??:?
#      5    0x0000742d64f54c5b: ?? ??:0
#      6    0x0000742d64f54ea3: ?? ??:0

# Radix MMU not available, KVM selftests does not support Hash MMU!
not ok 1 selftests: kvm: interrupt_perf # exit=254

You could instead use TEST_REQUIRE:

TAP version 131..17
# selftests: kvm: interrupt_perf
# 1..0 # SKIP - Requirement not met: kvm_check_cap(KVM_CAP_PPC_MMU_RADIX) ok 1 selftests: kvm: interrupt_perf # SKIP

Quoted 10 lines
tools/testing/selftests/kvm/lib/guest_modes.c +9 -0
+               /* Radix guest EA and RA are 52-bit on POWER9 and POWER10 */
+               if (sysconf(_SC_PAGESIZE) == 4096)
+                       vm_mode_default = VM_MODE_P52V52_4K;
+               else
+                       vm_mode_default = VM_MODE_P52V52_64K;
+               guest_mode_append(VM_MODE_P52V52_4K, true, true);
+               guest_mode_append(VM_MODE_P52V52_64K, true, true);
+       }
+#endif
 }
#5 lore raw

Re: [PATCH v3 3/6] KVM: PPC: selftests: add support for powerpc

In reply to Nicholas Piggin
Quoted 11 lines · Nicholas Piggin
tools/testing/selftests/kvm/lib/powerpc/ucall.c new file +5 -0
new file mode 100644
index 000000000000..ce0ddde45fef
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ucall support. A ucall is a "hypercall to host userspace".
+ */
+#include "kvm_util.h"

tools/testing/selftests/kvm/lib/powerpc/ucall.c line 15

Quoted 10 lines
tools/testing/selftests/kvm/lib/powerpc/ucall.c new file +10 -0
+#include "hcall.h"
+
+void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa)
+{
+}
+
+void ucall_arch_do_ucall(vm_vaddr_t uc)
+{
+	hcall2(H_UCALL, UCALL_R4_UCALL, (uintptr_t)(uc));
+}

FYI, the ucall stuff will silently conflict with treewide (where KVM selftests is the treechanges that I've queued[*]. It probably makes sense for the initial PPC support to go through the KVM tree anyways, so I'd be more than happy to grab this series via kvm-x86/selftests if you're willing to do the code changes (should be minor, knock wood). Alternatively, the immutable tag I'm planning on creating could be merged into the PPC tree, but that seems like overkill.

Either way, please Cc me on the next version (assuming there is a next version), if only so that I can give you an early heads up if/when the next treewide change alongs ;-)

[*] https://lore.kernel.org/all/[email protected]