Skip to contentSkip to conversation

KVM: arm64: selftests: Tests for protected VMs

PATCH Superseded Fuad Tabba 2 participants 23 messages Nov 4 – Nov 6

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

cover KVM: arm64: selftests: Tests for protected VMs

Hi folks,

This series adds selftests for protected VMs. A test skips on a host without pKVM, and so does any case where the host doesn't have the required feature, e.g., PtrAuth, SVE or hugetlb pages.

A couple of points:

- Each area gets its own test rather than cases folded into existing tests such as psci_test, hypercalls, set_id_regs, etc. A protected guest requires its own boot path, and has to share the ucall pool before its first ucall. On top of that, a protected guest gets the opposite result for many of those tests' checks.

- pkvm_exceptions skips its breakpoint, watchpoint and single-step cases while a protected guest's ID_AA64DFR0_EL1.DebugVer is 0. pkvm_id_regs reads the same field to choose between the hidden and the exposed debug view. With the patch that adds self-hosted debug to protected guests [1], both tests run the debug cases with no change.

The series is structured as follows:

01: external_aborts: an abort injected before a vCPU's first run.
02-04: Generic selftest changes: creating a protected VM, the pKVM
function IDs and the ucall pool's address.
05-06: Protected-VM library: boot trampoline and helpers. 07-16: The tests, one per area.

Based on Linux 7.4-rc1 (6628e7d4c6b25).

Cheers,
/fuad

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

Fuad Tabba (16):
KVM: arm64: selftests: Inject an abort before a vCPU's first run
KVM: selftests: Allow arm64 tests to create protected VMs
tools: arm-smccc: Add the pKVM memory sharing function IDs
KVM: selftests: Export the ucall pool's guest physical address
KVM: arm64: selftests: Add protected-VM boot trampoline
KVM: arm64: selftests: Add protected-VM library helpers
KVM: arm64: selftests: Add pkvm_host_abi host-interface test
KVM: arm64: selftests: Add pkvm_psci multi-vCPU PSCI test
KVM: arm64: selftests: Add pkvm_mem_share hypercall test
KVM: arm64: selftests: Add pkvm_smccc hypercall test
KVM: arm64: selftests: Add pkvm_id_regs register and trap test
KVM: arm64: selftests: Add pkvm_vcpu_regs register-state test
KVM: arm64: selftests: Add pkvm_irq interrupt test
KVM: arm64: selftests: Add pkvm_isolation host-access test
KVM: arm64: selftests: Add pkvm_lifecycle teardown test
KVM: arm64: selftests: Add pkvm_exceptions debug and SError test

 tools/include/linux/arm-smccc.h               |  22 +
 tools/testing/selftests/kvm/Makefile.kvm      |  12 +
 .../selftests/kvm/arm64/external_aborts.c     |  21 +
 .../selftests/kvm/arm64/pkvm_exceptions.c     | 261 ++++++++
 .../selftests/kvm/arm64/pkvm_host_abi.c       | 428 ++++++++++++
 .../selftests/kvm/arm64/pkvm_id_regs.c        | 306 +++++++++
 tools/testing/selftests/kvm/arm64/pkvm_irq.c  | 268 ++++++++
 .../selftests/kvm/arm64/pkvm_isolation.c      | 225 +++++++
 .../selftests/kvm/arm64/pkvm_lifecycle.c      | 110 +++
 .../selftests/kvm/arm64/pkvm_mem_share.c      | 394 +++++++++++
 tools/testing/selftests/kvm/arm64/pkvm_psci.c | 632 ++++++++++++++++++
 .../testing/selftests/kvm/arm64/pkvm_smccc.c  | 117 ++++
 .../selftests/kvm/arm64/pkvm_vcpu_regs.c      | 221 ++++++
 .../selftests/kvm/include/arm64/pkvm.h        |  74 ++
 .../selftests/kvm/include/ucall_common.h      |   1 +
 tools/testing/selftests/kvm/lib/arm64/pkvm.c  | 245 +++++++
 .../selftests/kvm/lib/arm64/pvm-entry.S       |  58 ++
 tools/testing/selftests/kvm/lib/kvm_util.c    |   5 +-
 .../testing/selftests/kvm/lib/ucall_common.c  |   9 +-
 19 files changed, 3406 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_exceptions.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_host_abi.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_id_regs.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_irq.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_isolation.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_lifecycle.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_mem_share.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_psci.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_smccc.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_vcpu_regs.c
 create mode 100644 tools/testing/selftests/kvm/include/arm64/pkvm.h
 create mode 100644 tools/testing/selftests/kvm/lib/arm64/pkvm.c
 create mode 100644 tools/testing/selftests/kvm/lib/arm64/pvm-entry.S

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.47.3
author reviewer
#1 lore raw

PATCH 1/16 KVM: arm64: selftests: Inject an abort before a vCPU's first run

KVM commits an external abort injected with KVM_SET_VCPU_EVENTS immediately, even before the vCPU's first run. For a normal VM on a pKVM host that path panicked the hypervisor until commit a2bb5f5c67a95 ("KVM: arm64: Pin the host vCPU before adjusting its PC under pKVM"). No test injects before the first run.

Add one, checking that the guest takes the abort at its entry point.

---
 .../selftests/kvm/arm64/external_aborts.c     | 21 +++++++++++++++++++
 1 file changed, 21 insertions(+)
tools/testing/selftests/kvm/arm64/external_aborts.c +21 -0
index 1a858d0d82968..45c9300b2ca0e 100644
@@ -113,6 +113,26 @@ static void vcpu_run_expect_sync(struct kvm_vcpu *vcpu)
 	__vcpu_run_expect(vcpu, UCALL_SYNC);
 }
 
+static void test_sea_before_run_guest(void)
+{
+	GUEST_FAIL("Guest ran before taking the injected abort");
+}
+
+/* Test that an abort injected before the vCPU's first run is taken at its entry point. */
+static void test_sea_before_run(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm = vm_create_with_dabt_handler(&vcpu, test_sea_before_run_guest,
+							expect_sea_handler);
+
+	expected_abort_pc = (u64)test_sea_before_run_guest;
+	sync_global_to_guest(vm, expected_abort_pc);
+
+	vcpu_inject_sea(vcpu);
+	vcpu_run_expect_done(vcpu);
+	kvm_vm_free(vm);
+}
+
 extern char test_mmio_abort_insn;
 
 static noinline void test_mmio_abort_guest(void)
@@ -420,6 +440,7 @@ static void test_serror_amo(void)
 
 int main(void)
 {
+	test_sea_before_run();
 	test_mmio_abort();
 	test_mmio_nisv();
 	test_mmio_nisv_abort();
Signature · 1 line
2.47.3
author reviewer
#2 lore raw

PATCH 2/16 KVM: selftests: Allow arm64 tests to create protected VMs

In reply to Fuad Tabba

On arm64, the VM type passed to KVM_CREATE_VM carries the IPA size, which the selftest library fills in. The library rejects any type a test provides in vm_shape.type. A protected VM is created by setting KVM_VM_TYPE_ARM_PROTECTED in that type, so a test can't create one.

Accept KVM_VM_TYPE_ARM_PROTECTED from the test, and add the IPA size to it as for any other VM.

---
 tools/testing/selftests/kvm/lib/kvm_util.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +3 -2
index d42e201eba763..96c1e70dba3d3 100644
@@ -400,9 +400,10 @@ struct kvm_vm *____vm_create(struct vm_shape shape)
 	}
 
 #ifdef __aarch64__
-	TEST_ASSERT(!vm->type, "ARM doesn't support test-provided types");
+	TEST_ASSERT(!(vm->type & ~KVM_VM_TYPE_ARM_PROTECTED),
+		    "ARM supports only KVM_VM_TYPE_ARM_PROTECTED as a test-provided type");
 	if (vm->pa_bits != 40)
-		vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
+		vm->type |= KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
 #endif
 
 	vm_open(vm);
Signature · 1 line
2.47.3
author reviewer
#4 lore raw

PATCH 3/16 tools: arm-smccc: Add the pKVM memory sharing function IDs

In reply to Fuad Tabba

The tools copy of arm-smccc.h lacks the pKVM guest hypercalls that the protected-VM selftests make.

The kernel's arm-smccc.h defines the vendor-hyp UID with UUID_INIT(), which requires linux/uuid.h, and tools doesn't have one. So rather than sync the whole header, copy the function numbers and IDs of HYP_MEMINFO, MEM_SHARE and MEM_UNSHARE from include/linux/arm-smccc.h.

---
 tools/include/linux/arm-smccc.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
tools/include/linux/arm-smccc.h +22 -0
index 63ce9bebccd32..8c8f4ad639018 100644
@@ -111,6 +111,10 @@
 /* KVM "vendor specific" services */
 #define ARM_SMCCC_KVM_FUNC_FEATURES		0
 #define ARM_SMCCC_KVM_FUNC_PTP			1
+/* Start of pKVM hypercall range */
+#define ARM_SMCCC_KVM_FUNC_HYP_MEMINFO		2
+#define ARM_SMCCC_KVM_FUNC_MEM_SHARE		3
+#define ARM_SMCCC_KVM_FUNC_MEM_UNSHARE		4
 #define ARM_SMCCC_KVM_FUNC_FEATURES_2		127
 #define ARM_SMCCC_KVM_NUM_FUNCS			128
 
@@ -133,6 +137,24 @@
 			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
 			   ARM_SMCCC_KVM_FUNC_PTP)
 
+#define ARM_SMCCC_VENDOR_HYP_KVM_HYP_MEMINFO_FUNC_ID			\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
+			   ARM_SMCCC_SMC_64,				\
+			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
+			   ARM_SMCCC_KVM_FUNC_HYP_MEMINFO)
+
+#define ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID			\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
+			   ARM_SMCCC_SMC_64,				\
+			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
+			   ARM_SMCCC_KVM_FUNC_MEM_SHARE)
+
+#define ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID			\
+	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,				\
+			   ARM_SMCCC_SMC_64,				\
+			   ARM_SMCCC_OWNER_VENDOR_HYP,			\
+			   ARM_SMCCC_KVM_FUNC_MEM_UNSHARE)
+
 /* ptp_kvm counter type ID */
 #define KVM_PTP_VIRT_COUNTER			0
 #define KVM_PTP_PHYS_COUNTER			1
Signature · 1 line
2.47.3
author reviewer
#5 lore raw

PATCH 4/16 KVM: selftests: Export the ucall pool's guest physical address

In reply to Fuad Tabba

A protected guest's memory is private until the guest itself shares it, so the guest has to share the ucall pool before its first ucall. The host can't do that for it. Only the guest's copy of ucall_pool holds the pool's address.

Add get_ucall_pool_gpa(), which reads that copy, so that the host can pass the address to the guest.

---
 tools/testing/selftests/kvm/include/ucall_common.h | 1 +
 tools/testing/selftests/kvm/lib/ucall_common.c     | 9 ++++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/ucall_common.h +1 -0
index cbdcb0a50c4f9..3a6cdf9aa0464 100644
@@ -41,6 +41,7 @@ __printf(5, 6) void ucall_assert(u64 cmd, const char *exp,
 u64 get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
 void ucall_init(struct kvm_vm *vm, gpa_t mmio_gpa);
 int ucall_nr_pages_required(u64 page_size);
+gpa_t get_ucall_pool_gpa(struct kvm_vm *vm);
 
 /*
  * Perform userspace call without any associated data.  This bare call avoids
tools/testing/selftests/kvm/lib/ucall_common.c +8 -1
index 029ce21f9f2fa..aa1f0f92c6d1d 100644
@@ -21,10 +21,17 @@ int ucall_nr_pages_required(u64 page_size)
 
 /*
  * ucall_pool holds per-VM values (global data is duplicated by each VM), it
- * must not be accessed from host code.
+ * must not be accessed from host code. The host reads only a VM's copy.
  */
 static struct ucall_header *ucall_pool;
 
+gpa_t get_ucall_pool_gpa(struct kvm_vm *vm)
+{
+	struct ucall_header **pool = addr_gva2hva(vm, (gva_t)&ucall_pool);
+
+	return addr_gva2gpa(vm, (gva_t)*pool);
+}
+
 void ucall_init(struct kvm_vm *vm, gpa_t mmio_gpa)
 {
 	struct ucall_header *hdr;
Signature · 1 line
2.47.3
author reviewer
#6 lore raw

PATCH 5/16 KVM: arm64: selftests: Add protected-VM boot trampoline

In reply to Fuad Tabba

EL2 takes only PC and X0 from the host when it boots a protected vCPU, and resets everything else. So the registers the selftest library sets before the first run never reach the guest.

Boot a protected guest through a trampoline that restores X0-X7, seven EL1 system registers and the stack pointer from a block X0 points to, then enables the MMU and branches to the guest's entry point.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../selftests/kvm/include/arm64/pkvm.h        | 50 ++++++++++++++++
 .../selftests/kvm/lib/arm64/pvm-entry.S       | 58 +++++++++++++++++++
 3 files changed, 109 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/include/arm64/pkvm.h
 create mode 100644 tools/testing/selftests/kvm/lib/arm64/pvm-entry.S
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 17a4256e90819..c254e82f9a786 100644
@@ -35,6 +35,7 @@ LIBKVM_arm64 += lib/arm64/gic_v3.c
 LIBKVM_arm64 += lib/arm64/gic_v3_its.c
 LIBKVM_arm64 += lib/arm64/handlers.S
 LIBKVM_arm64 += lib/arm64/processor.c
+LIBKVM_arm64 += lib/arm64/pvm-entry.S
 LIBKVM_arm64 += lib/arm64/spinlock.c
 LIBKVM_arm64 += lib/arm64/ucall.c
 LIBKVM_arm64 += lib/arm64/vgic.c
tools/testing/selftests/kvm/include/arm64/pkvm.h new file +50 -0

50 lines not on this page · raw message

tools/testing/selftests/kvm/lib/arm64/pvm-entry.S new file +58 -0

58 lines not on this page · raw message

Signature · 1 line
2.47.3
author reviewer
#8 lore raw

PATCH 6/16 KVM: arm64: selftests: Add protected-VM library helpers

In reply to Fuad Tabba

A protected-VM test has to create the VM with the protected type and the PSCI 0.2 vCPU feature, boot each vCPU through the trampoline from an identity-mapped page, and have the guest share the ucall pool before its first ucall. Secondaries start powered off for the guest to boot with CPU_ON. A vCPU becomes a valid CPU_ON target only at its first KVM_RUN.

Add lib/arm64/pkvm.c to do this for the tests. pkvm_vcpu_setup_boot_secondary() runs a secondary once with immediate_exit set, so that it becomes a CPU_ON target. The library also has a run loop, an SMCCC call for the guest, and a reader of the process's locked-memory count. The host adds a protected VM's pinned pages to that count. No capability advertises pKVM, so pkvm_supported() tries to create a protected VM.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/include/arm64/pkvm.h        |  24 ++
 tools/testing/selftests/kvm/lib/arm64/pkvm.c  | 245 ++++++++++++++++++
 3 files changed, 270 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/lib/arm64/pkvm.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index c254e82f9a786..e173a9adad3f2 100644
@@ -34,6 +34,7 @@ LIBKVM_arm64 += lib/arm64/gic.c
 LIBKVM_arm64 += lib/arm64/gic_v3.c
 LIBKVM_arm64 += lib/arm64/gic_v3_its.c
 LIBKVM_arm64 += lib/arm64/handlers.S
+LIBKVM_arm64 += lib/arm64/pkvm.c
 LIBKVM_arm64 += lib/arm64/processor.c
 LIBKVM_arm64 += lib/arm64/pvm-entry.S
 LIBKVM_arm64 += lib/arm64/spinlock.c
tools/testing/selftests/kvm/include/arm64/pkvm.h +24 -0
index 6ef769b8ff977..413c7ac4a9f16 100644
@@ -9,6 +9,9 @@
 #define SELFTEST_KVM_ARM64_PKVM_H
 
 #ifndef __ASSEMBLER__
+#include "kvm_util.h"
+#include "ucall_common.h"
+
 /*
  * Register state the boot trampoline (pvm-entry.S) restores. Only PC and X0
  * survive to a protected guest's first run, so X0 points at this block.
@@ -27,6 +30,27 @@ struct pvm_boot_args {
 	u64	tpidr_el1;
 	u64	sp_el1;
 };
+
+/* Memslot of the trampoline's page. Test memslots start above it. */
+#define PKVM_IDMAP_MEMSLOT	1
+
+bool pkvm_supported(void);
+struct kvm_vm *pkvm_vm_create(unsigned int nr_vcpus, u32 features, struct kvm_vcpu *vcpus[]);
+struct kvm_vm *pkvm_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, void *guest_code);
+
+/* Call after all register setup: later register writes don't reach the guest. */
+void pkvm_vcpu_setup_boot(struct kvm_vcpu *vcpu, void *guest_code);
+
+/* Call before the primary's first run. */
+gpa_t pkvm_vcpu_setup_boot_secondary(struct kvm_vcpu *vcpu, void *guest_code);
+gpa_t pkvm_idmap_gpa(struct kvm_vm *vm);
+
+void pkvm_run_loop(struct kvm_vcpu *vcpu, void (*on_sync)(struct kvm_vcpu *vcpu, struct ucall *uc));
+size_t pkvm_host_locked_vm(void);
+
+unsigned long pkvm_hvc(u32 func, u64 arg0, u64 arg1, u64 arg2);
+
+void pkvm_guest_init(void);
 #endif
 
 #define PVM_BOOT_ARGS_OFF_JUMP_TGT	0
tools/testing/selftests/kvm/lib/arm64/pkvm.c new file +245 -0

245 lines not on this page · raw message

Signature · 1 line
2.47.3
author reviewer
#11 lore raw

PATCH 7/16 KVM: arm64: selftests: Add pkvm_host_abi host-interface test

In reply to Fuad Tabba

The host isn't trusted with a protected guest's state, so once a protected vCPU has run, KVM rejects the ioctls that would read or change that state. The host can still inject a virtual SError and read back whether one is pending. The VM gets only the capabilities and vCPU features pKVM supports.

Check what the host's KVM interface allows and rejects for a protected VM, before and after the first run, and that the host accounts the VM's memory as locked until the VM is freed.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/pkvm_host_abi.c       | 428 ++++++++++++++++++
 2 files changed, 429 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_host_abi.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index e173a9adad3f2..42e530e0e573b 100644
@@ -190,6 +190,7 @@ TEST_GEN_PROGS_arm64 += arm64/nv_pre_fault_memory_test
 TEST_GEN_PROGS_arm64 += arm64/external_aborts
 TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext
 TEST_GEN_PROGS_arm64 += arm64/page_fault_test
+TEST_GEN_PROGS_arm64 += arm64/pkvm_host_abi
 TEST_GEN_PROGS_arm64 += arm64/psci_test
 TEST_GEN_PROGS_arm64 += arm64/sea_to_user
 TEST_GEN_PROGS_arm64 += arm64/set_id_regs
tools/testing/selftests/kvm/arm64/pkvm_host_abi.c new file +428 -0

428 lines not on this page · raw message

Signature · 1 line
2.47.3
author reviewer
#12 lore raw

PATCH 8/16 KVM: arm64: selftests: Add pkvm_psci multi-vCPU PSCI test

In reply to Fuad Tabba

EL2 handles a protected guest's CPU_ON and CPU_OFF itself, so the host can't power on a vCPU the guest hasn't, or change where it boots. EL2 also handles PSCI_VERSION and PSCI_FEATURES, and returns NOT_SUPPORTED for unknown calls. It forwards the rest to the host.

Test CPU_ON, CPU_OFF, AFFINITY_INFO, PSCI_VERSION, PSCI_FEATURES, SYSTEM_OFF, SYSTEM_RESET and SYSTEM_RESET2. Also check that the host can't make a second vCPU runnable without a CPU_ON from the guest. Run the secondaries on their own threads to race CPU_ON against CPU_OFF and against another CPU_ON. EL2 marks a vCPU off at its CPU_OFF and returns to the host. Until the host powers that vCPU off, a CPU_ON of it returns ALREADY_ON.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 tools/testing/selftests/kvm/arm64/pkvm_psci.c | 632 ++++++++++++++++++
 2 files changed, 633 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_psci.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 42e530e0e573b..aa256fb66d226 100644
@@ -191,6 +191,7 @@ TEST_GEN_PROGS_arm64 += arm64/external_aborts
 TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext
 TEST_GEN_PROGS_arm64 += arm64/page_fault_test
 TEST_GEN_PROGS_arm64 += arm64/pkvm_host_abi
+TEST_GEN_PROGS_arm64 += arm64/pkvm_psci
 TEST_GEN_PROGS_arm64 += arm64/psci_test
 TEST_GEN_PROGS_arm64 += arm64/sea_to_user
 TEST_GEN_PROGS_arm64 += arm64/set_id_regs
tools/testing/selftests/kvm/arm64/pkvm_psci.c new file +632 -0

632 lines not on this page · raw message

Signature · 1 line
2.47.3
author reviewer
#13 lore raw

PATCH 9/16 KVM: arm64: selftests: Add pkvm_mem_share hypercall test

In reply to Fuad Tabba

A protected guest gives the host access to one of its pages with MEM_SHARE and takes it back with MEM_UNSHARE. The page is a host page, whose size HYP_MEMINFO returns. At teardown EL2 zeroes every page the guest hasn't left shared.

Exercise the three calls, the host's access to shared and private pages, and the zeroing. Also check that KVM_RUN fails with EIO on hugetlb-backed memory, that sharing an IPA outside every memslot makes the guest take an external abort, and that faults each requiring new stage-2 tables succeed.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/pkvm_mem_share.c      | 394 ++++++++++++++++++
 2 files changed, 395 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_mem_share.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index aa256fb66d226..88c1159cb3bf1 100644
@@ -191,6 +191,7 @@ TEST_GEN_PROGS_arm64 += arm64/external_aborts
 TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext
 TEST_GEN_PROGS_arm64 += arm64/page_fault_test
 TEST_GEN_PROGS_arm64 += arm64/pkvm_host_abi
+TEST_GEN_PROGS_arm64 += arm64/pkvm_mem_share
 TEST_GEN_PROGS_arm64 += arm64/pkvm_psci
 TEST_GEN_PROGS_arm64 += arm64/psci_test
 TEST_GEN_PROGS_arm64 += arm64/sea_to_user
tools/testing/selftests/kvm/arm64/pkvm_mem_share.c new file +394 -0

394 lines not on this page · raw message

Signature · 1 line
2.47.3
author reviewer
#14 lore raw

PATCH 10/16 KVM: arm64: selftests: Add pkvm_smccc hypercall test

In reply to Fuad Tabba

EL2 handles a protected guest's SMCCC calls other than PSCI rather than forwarding them to the host. The vendor-hyp feature bitmap advertises only FEATURES and the three memory calls.

Check SMCCC_VERSION, the vendor-hyp UID and feature bitmap, that the calls EL2 doesn't implement (FF-A, TRNG, an unknown function) return NOT_SUPPORTED, and that an SMC makes the guest take an UNDEF.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../testing/selftests/kvm/arm64/pkvm_smccc.c  | 117 ++++++++++++++++++
 2 files changed, 118 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_smccc.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 88c1159cb3bf1..6bb436bd36205 100644
@@ -193,6 +193,7 @@ TEST_GEN_PROGS_arm64 += arm64/page_fault_test
 TEST_GEN_PROGS_arm64 += arm64/pkvm_host_abi
 TEST_GEN_PROGS_arm64 += arm64/pkvm_mem_share
 TEST_GEN_PROGS_arm64 += arm64/pkvm_psci
+TEST_GEN_PROGS_arm64 += arm64/pkvm_smccc
 TEST_GEN_PROGS_arm64 += arm64/psci_test
 TEST_GEN_PROGS_arm64 += arm64/sea_to_user
 TEST_GEN_PROGS_arm64 += arm64/set_id_regs
tools/testing/selftests/kvm/arm64/pkvm_smccc.c new file +117 -0

117 lines not on this page · raw message

Signature · 1 line
2.47.3
author reviewer
#15 lore raw

PATCH 11/16 KVM: arm64: selftests: Add pkvm_id_regs register and trap test

In reply to Fuad Tabba

pKVM gives a protected guest its own view of the ID registers, which hides the features pKVM doesn't support. The registers of those features UNDEF in the guest, except some of the hidden debug registers, which read as zero and ignore writes.

Test that view and those UNDEFs from the guest, along with the registers EL2 emulates with fixed values. The debug checks read ID_AA64DFR0_EL1.DebugVer to choose between the hidden view and the exposed one. PtrAuth, which a protected VM can be granted, gets its own case.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/pkvm_id_regs.c        | 306 ++++++++++++++++++
 2 files changed, 307 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_id_regs.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 6bb436bd36205..8855c9abb2fd0 100644
@@ -191,6 +191,7 @@ TEST_GEN_PROGS_arm64 += arm64/external_aborts
 TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext
 TEST_GEN_PROGS_arm64 += arm64/page_fault_test
 TEST_GEN_PROGS_arm64 += arm64/pkvm_host_abi
+TEST_GEN_PROGS_arm64 += arm64/pkvm_id_regs
 TEST_GEN_PROGS_arm64 += arm64/pkvm_mem_share
 TEST_GEN_PROGS_arm64 += arm64/pkvm_psci
 TEST_GEN_PROGS_arm64 += arm64/pkvm_smccc
tools/testing/selftests/kvm/arm64/pkvm_id_regs.c new file +306 -0

306 lines not on this page · raw message

Signature · 1 line
2.47.3
author reviewer
#16 lore raw

PATCH 12/16 KVM: arm64: selftests: Add pkvm_vcpu_regs register-state test

In reply to Fuad Tabba

A protected vCPU's registers are held at EL2, which handles some exits itself and returns others to the host. The host can move the vCPU's thread to another CPU at any exit. The guest's registers have to keep their values through all of that.

Verify that x19-x28 and v0-v31 keep the values the guest loaded across each kind of exit, and across a host CPU migration at every MMIO exit.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/pkvm_vcpu_regs.c      | 221 ++++++++++++++++++
 2 files changed, 222 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_vcpu_regs.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 8855c9abb2fd0..1e40e793aa6f3 100644
@@ -195,6 +195,7 @@ TEST_GEN_PROGS_arm64 += arm64/pkvm_id_regs
 TEST_GEN_PROGS_arm64 += arm64/pkvm_mem_share
 TEST_GEN_PROGS_arm64 += arm64/pkvm_psci
 TEST_GEN_PROGS_arm64 += arm64/pkvm_smccc
+TEST_GEN_PROGS_arm64 += arm64/pkvm_vcpu_regs
 TEST_GEN_PROGS_arm64 += arm64/psci_test
 TEST_GEN_PROGS_arm64 += arm64/sea_to_user
 TEST_GEN_PROGS_arm64 += arm64/set_id_regs
tools/testing/selftests/kvm/arm64/pkvm_vcpu_regs.c new file +221 -0

221 lines not on this page · raw message

Signature · 1 line
2.47.3
author reviewer
#17 lore raw

PATCH 13/16 KVM: arm64: selftests: Add pkvm_irq interrupt test

In reply to Fuad Tabba

The host emulates a protected guest's vGIC, so the list registers are copied between the host and EL2 each time the host runs the vCPU. If the copy back is lost, the host never sees the guest's EOI and presents the interrupt again. EL2 keeps its own copy of the guest's virtual timer state, which the host can't change, and restores it each time the host runs the vCPU.

Check that the virtual timer's PPI arrives exactly once, with CVAL and CTL kept across exits before and while it's pending, that SGIs pass between two vCPUs, and that the timer wakes a vCPU in WFI.

---
 tools/testing/selftests/kvm/Makefile.kvm     |   1 +
 tools/testing/selftests/kvm/arm64/pkvm_irq.c | 268 +++++++++++++++++++
 2 files changed, 269 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_irq.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 1e40e793aa6f3..13998964facd5 100644
@@ -192,6 +192,7 @@ TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext
 TEST_GEN_PROGS_arm64 += arm64/page_fault_test
 TEST_GEN_PROGS_arm64 += arm64/pkvm_host_abi
 TEST_GEN_PROGS_arm64 += arm64/pkvm_id_regs
+TEST_GEN_PROGS_arm64 += arm64/pkvm_irq
 TEST_GEN_PROGS_arm64 += arm64/pkvm_mem_share
 TEST_GEN_PROGS_arm64 += arm64/pkvm_psci
 TEST_GEN_PROGS_arm64 += arm64/pkvm_smccc
tools/testing/selftests/kvm/arm64/pkvm_irq.c new file +268 -0

268 lines not on this page · raw message

Signature · 1 line
2.47.3
author reviewer
#18 lore raw

PATCH 14/16 KVM: arm64: selftests: Add pkvm_isolation host-access test

In reply to Fuad Tabba

When a protected guest faults its memory in, the host donates its pages through EL2 and keeps its userspace mapping of them. Nothing the host does through that mapping exposes the guest's data. The host kernel's copy_to_user() and copy_from_user() on that mapping fail. When the host kernel pins such a page and accesses it through its own mapping, as process_vm_readv() does, EL2 reclaims the page instead. EL2 zeroes the page before giving it back, and the guest loses it.

Test that the host kernel's copies to and from that memory fail with EFAULT, that process_vm_readv() of it reads zeros and KVM_RUN then fails at the guest's next access to the page, that MADV_DONTNEED and munmap() leave the guest's pages pinned and accounted, and that a normal VM's fault on them fails.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/pkvm_isolation.c      | 225 ++++++++++++++++++
 2 files changed, 226 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_isolation.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 13998964facd5..68bdd01f62270 100644
@@ -193,6 +193,7 @@ TEST_GEN_PROGS_arm64 += arm64/page_fault_test
 TEST_GEN_PROGS_arm64 += arm64/pkvm_host_abi
 TEST_GEN_PROGS_arm64 += arm64/pkvm_id_regs
 TEST_GEN_PROGS_arm64 += arm64/pkvm_irq
+TEST_GEN_PROGS_arm64 += arm64/pkvm_isolation
 TEST_GEN_PROGS_arm64 += arm64/pkvm_mem_share
 TEST_GEN_PROGS_arm64 += arm64/pkvm_psci
 TEST_GEN_PROGS_arm64 += arm64/pkvm_smccc
tools/testing/selftests/kvm/arm64/pkvm_isolation.c new file +225 -0

225 lines not on this page · raw message

Signature · 1 line
2.47.3
author reviewer
#19 lore raw

PATCH 15/16 KVM: arm64: selftests: Add pkvm_lifecycle teardown test

In reply to Fuad Tabba

Each protected VM holds an entry in EL2's table of KVM_MAX_PVMS (255) VMs until teardown has reclaimed its pages, so if teardown leaks entries, KVM_CREATE_VM eventually fails.

Create, run and free twice that many VMs, four alive at once and freed in random order. Check that each VM's memory survives the others' teardown, and that freeing a VM drops the process's locked memory by what its runs added.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/pkvm_lifecycle.c      | 110 ++++++++++++++++++
 2 files changed, 111 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_lifecycle.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 68bdd01f62270..214f38e3f67a7 100644
@@ -194,6 +194,7 @@ TEST_GEN_PROGS_arm64 += arm64/pkvm_host_abi
 TEST_GEN_PROGS_arm64 += arm64/pkvm_id_regs
 TEST_GEN_PROGS_arm64 += arm64/pkvm_irq
 TEST_GEN_PROGS_arm64 += arm64/pkvm_isolation
+TEST_GEN_PROGS_arm64 += arm64/pkvm_lifecycle
 TEST_GEN_PROGS_arm64 += arm64/pkvm_mem_share
 TEST_GEN_PROGS_arm64 += arm64/pkvm_psci
 TEST_GEN_PROGS_arm64 += arm64/pkvm_smccc
tools/testing/selftests/kvm/arm64/pkvm_lifecycle.c new file +110 -0

110 lines not on this page · raw message

Signature · 1 line
2.47.3
author reviewer
#20 lore raw

PATCH 16/16 KVM: arm64: selftests: Add pkvm_exceptions debug and SError test

In reply to Fuad Tabba

pKVM can hide self-hosted debug from a protected guest, which then reads ID_AA64DFR0_EL1.DebugVer as 0. A virtual SError the host injects has to be delivered by EL2, since the host can't see whether the guest has it masked.

Verify that the guest's own handler takes a hardware breakpoint, a watchpoint and a single step, and that an injected SError is pending in ISR_EL1.A while PSTATE.A masks it and is taken once unmasked. The debug cases skip while ID_AA64DFR0_EL1.DebugVer is 0.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/pkvm_exceptions.c     | 261 ++++++++++++++++++
 2 files changed, 262 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_exceptions.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 214f38e3f67a7..1045136aacc16 100644
@@ -190,6 +190,7 @@ TEST_GEN_PROGS_arm64 += arm64/nv_pre_fault_memory_test
 TEST_GEN_PROGS_arm64 += arm64/external_aborts
 TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext
 TEST_GEN_PROGS_arm64 += arm64/page_fault_test
+TEST_GEN_PROGS_arm64 += arm64/pkvm_exceptions
 TEST_GEN_PROGS_arm64 += arm64/pkvm_host_abi
 TEST_GEN_PROGS_arm64 += arm64/pkvm_id_regs
 TEST_GEN_PROGS_arm64 += arm64/pkvm_irq
tools/testing/selftests/kvm/arm64/pkvm_exceptions.c new file +261 -0

261 lines not on this page · raw message

Signature · 1 line
2.47.3
reviewer
#21 lore raw
In reply to Fuad Tabba
Quoted 22 lines · Fuad Tabba

Hi folks,

This series adds selftests for protected VMs. A test skips on a host without pKVM, and so does any case where the host doesn't have the required feature, e.g., PtrAuth, SVE or hugetlb pages.

A couple of points:

- Each area gets its own test rather than cases folded into existing tests such as psci_test, hypercalls, set_id_regs, etc. A protected guest requires its own boot path, and has to share the ucall pool before its first ucall. On top of that, a protected guest gets the opposite result for many of those tests' checks.

- pkvm_exceptions skips its breakpoint, watchpoint and single-step cases while a protected guest's ID_AA64DFR0_EL1.DebugVer is 0. pkvm_id_regs reads the same field to choose between the hidden and the exposed debug view. With the patch that adds self-hosted debug to protected guests [1], both tests run the debug cases with no change.

The series is structured as follows:

Quoted 7 lines

01: external_aborts: an abort injected before a vCPU's first run.
02-04: Generic selftest changes: creating a protected VM, the pKVM
function IDs and the ucall pool's address.
05-06: Protected-VM library: boot trampoline and helpers. 07-16: The tests, one per area.

Based on Linux 7.4-rc1 (6628e7d4c6b25).

Hi!

I started to also write some realm tests a bit, you can check it out here:
https://gitlab.arm.com/linux-arm/linux-jg/-/tree/cca-kselftest

It's WIP/prototype, but it may be good to have some sharing, if possible.
For example I also added a 'pre-boot' stub, which looks like your pvm-entry.S:

	https://gitlab.arm.com/linux-arm/linux-jg/-/commit/cd3ae35d89974b02b7051a0c45cc68b4df8f9d3e

However I extracted the registers needed for the stub like this:

	https://gitlab.arm.com/linux-arm/linux-jg/-/commit/f559d5b61fa1a37e2d7b23704bd367e079cf8eae

This is because the current Realm branch I'm working on top of rejects writes to those system registers (TCR_EL1, CPACR_EL1 etc). Which is why I had to modify these functions, along with __aarch64_vcpu_add() and some others.

I'm not sure how much there is to gain by sharing overall, but good to bring it up early!

Thanks,
Joey

Quoted 63 lines

Cheers,
/fuad

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

Fuad Tabba (16):
KVM: arm64: selftests: Inject an abort before a vCPU's first run
KVM: selftests: Allow arm64 tests to create protected VMs
tools: arm-smccc: Add the pKVM memory sharing function IDs
KVM: selftests: Export the ucall pool's guest physical address
KVM: arm64: selftests: Add protected-VM boot trampoline
KVM: arm64: selftests: Add protected-VM library helpers
KVM: arm64: selftests: Add pkvm_host_abi host-interface test
KVM: arm64: selftests: Add pkvm_psci multi-vCPU PSCI test
KVM: arm64: selftests: Add pkvm_mem_share hypercall test
KVM: arm64: selftests: Add pkvm_smccc hypercall test
KVM: arm64: selftests: Add pkvm_id_regs register and trap test
KVM: arm64: selftests: Add pkvm_vcpu_regs register-state test
KVM: arm64: selftests: Add pkvm_irq interrupt test
KVM: arm64: selftests: Add pkvm_isolation host-access test
KVM: arm64: selftests: Add pkvm_lifecycle teardown test
KVM: arm64: selftests: Add pkvm_exceptions debug and SError test

 tools/include/linux/arm-smccc.h               |  22 +
 tools/testing/selftests/kvm/Makefile.kvm      |  12 +
 .../selftests/kvm/arm64/external_aborts.c     |  21 +
 .../selftests/kvm/arm64/pkvm_exceptions.c     | 261 ++++++++
 .../selftests/kvm/arm64/pkvm_host_abi.c       | 428 ++++++++++++
 .../selftests/kvm/arm64/pkvm_id_regs.c        | 306 +++++++++
 tools/testing/selftests/kvm/arm64/pkvm_irq.c  | 268 ++++++++
 .../selftests/kvm/arm64/pkvm_isolation.c      | 225 +++++++
 .../selftests/kvm/arm64/pkvm_lifecycle.c      | 110 +++
 .../selftests/kvm/arm64/pkvm_mem_share.c      | 394 +++++++++++
 tools/testing/selftests/kvm/arm64/pkvm_psci.c | 632 ++++++++++++++++++
 .../testing/selftests/kvm/arm64/pkvm_smccc.c  | 117 ++++
 .../selftests/kvm/arm64/pkvm_vcpu_regs.c      | 221 ++++++
 .../selftests/kvm/include/arm64/pkvm.h        |  74 ++
 .../selftests/kvm/include/ucall_common.h      |   1 +
 tools/testing/selftests/kvm/lib/arm64/pkvm.c  | 245 +++++++
 .../selftests/kvm/lib/arm64/pvm-entry.S       |  58 ++
 tools/testing/selftests/kvm/lib/kvm_util.c    |   5 +-
 .../testing/selftests/kvm/lib/ucall_common.c  |   9 +-
 19 files changed, 3406 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_exceptions.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_host_abi.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_id_regs.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_irq.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_isolation.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_lifecycle.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_mem_share.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_psci.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_smccc.c
 create mode 100644 tools/testing/selftests/kvm/arm64/pkvm_vcpu_regs.c
 create mode 100644 tools/testing/selftests/kvm/include/arm64/pkvm.h
 create mode 100644 tools/testing/selftests/kvm/lib/arm64/pkvm.c
 create mode 100644 tools/testing/selftests/kvm/lib/arm64/pvm-entry.S

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.47.3
author reviewer
#22 lore raw

Hi Joey,

Thanks for sharing this.

On Wed, 04 Nov 2026 14:10:45 +0000, Joey Gouly <[email protected]> wrote:
[...]

Quoted 5 lines

For example I also added a 'pre-boot' stub, which looks like your pvm-entry.S:

        https://gitlab.arm.com/linux-arm/linux-jg/-/commit/cd3ae35d89974b02b7051a0c45cc68b4df8f9d3e
Quoted 8 lines

However I extracted the registers needed for the stub like this:

        https://gitlab.arm.com/linux-arm/linux-jg/-/commit/f559d5b61fa1a37e2d7b23704bd367e079cf8eae

This is because the current Realm branch I'm working on top of rejects writes to those system registers (TCR_EL1, CPACR_EL1 etc). Which is why I had to modify these functions, along with __aarch64_vcpu_add() and some others.

When EL2 boots a protected guest, it takes only PC and X0 from the host, so pvm-entry.S reads the registers from a block that X0 points to, rather than from X0-X7 like your stub. I think a block would work for a realm too, since your test sets X0. The trampoline also sets VBAR_EL1 and passes X0-X7 on to the guest code, for tests that install exception handlers or take arguments.

The host side of pKVM accepts those register writes, so pkvm.c creates the vCPUs with aarch64_vcpu_add() as for any VM and reads the registers back, SP_EL1 and VBAR_EL1 included.

Quoted 2 lines

I'm not sure how much there is to gain by sharing overall, but good to bring it up early!

Thanks again for raising it. I'd keep this series as it is for now. When you post the realm tests, would it make sense for you to move the trampoline and its boot-args block to neutral names and boot the realm tests through them, with your register extraction in the same series? Patch 2 is close to your "Allow VM types for arm64", but allows only KVM_VM_TYPE_ARM_PROTECTED, so you'd only need to add the realm type to that check.

Cheers,
/fuad

reviewer Reviewed-by on v1 2/16
#3 lore raw

Re: [PATCH 02/16] KVM: selftests: Allow arm64 tests to create protected VMs

In reply to Fuad Tabba
Quoted 9 lines · Fuad Tabba

On arm64, the VM type passed to KVM_CREATE_VM carries the IPA size, which the selftest library fills in. The library rejects any type a test provides in vm_shape.type. A protected VM is created by setting KVM_VM_TYPE_ARM_PROTECTED in that type, so a test can't create one.

Accept KVM_VM_TYPE_ARM_PROTECTED from the test, and add the IPA size to it as for any other VM.

Similar to what I did for realms locally, and will have to expand to support KVM_VM_TYPE_ARM_REALM.

Quoted 24 lines
---
 tools/testing/selftests/kvm/lib/kvm_util.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +3 -2
index d42e201eba763..96c1e70dba3d3 100644
@@ -400,9 +400,10 @@ struct kvm_vm *____vm_create(struct vm_shape shape)
 	}
 
 #ifdef __aarch64__
-	TEST_ASSERT(!vm->type, "ARM doesn't support test-provided types");
+	TEST_ASSERT(!(vm->type & ~KVM_VM_TYPE_ARM_PROTECTED),
+		    "ARM supports only KVM_VM_TYPE_ARM_PROTECTED as a test-provided type");
 	if (vm->pa_bits != 40)
-		vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
+		vm->type |= KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
 #endif
 
 	vm_open(vm);
Signature · 1 line
2.47.3
reviewer Reviewed-by on v1 5/16
#7 lore raw

Re: [PATCH 05/16] KVM: arm64: selftests: Add protected-VM boot trampoline

In reply to Fuad Tabba
Quoted 11 lines · Fuad Tabba

EL2 takes only PC and X0 from the host when it boots a protected vCPU, and resets everything else. So the registers the selftest library sets before the first run never reach the guest.

Boot a protected guest through a trampoline that restores X0-X7, seven EL1 system registers and the stack pointer from a block X0 points to, then enables the MMU and branches to the guest's entry point.

Similar idea to the 'realm-boot' I added, and mentioned previously. I think it should work for Realms too.

Quoted 143 lines
---
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../selftests/kvm/include/arm64/pkvm.h        | 50 ++++++++++++++++
 .../selftests/kvm/lib/arm64/pvm-entry.S       | 58 +++++++++++++++++++
 3 files changed, 109 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/include/arm64/pkvm.h
 create mode 100644 tools/testing/selftests/kvm/lib/arm64/pvm-entry.S
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 17a4256e90819..c254e82f9a786 100644
@@ -35,6 +35,7 @@ LIBKVM_arm64 += lib/arm64/gic_v3.c
 LIBKVM_arm64 += lib/arm64/gic_v3_its.c
 LIBKVM_arm64 += lib/arm64/handlers.S
 LIBKVM_arm64 += lib/arm64/processor.c
+LIBKVM_arm64 += lib/arm64/pvm-entry.S
 LIBKVM_arm64 += lib/arm64/spinlock.c
 LIBKVM_arm64 += lib/arm64/ucall.c
 LIBKVM_arm64 += lib/arm64/vgic.c
tools/testing/selftests/kvm/include/arm64/pkvm.h new file +50 -0
new file mode 100644
index 0000000000000..6ef769b8ff977
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2025 Google LLC
+ * Author: Fuad Tabba <[email protected]>
+ * Author: Will Deacon <[email protected]>
+ */
+
+#ifndef SELFTEST_KVM_ARM64_PKVM_H
+#define SELFTEST_KVM_ARM64_PKVM_H
+
+#ifndef __ASSEMBLER__
+/*
+ * Register state the boot trampoline (pvm-entry.S) restores. Only PC and X0
+ * survive to a protected guest's first run, so X0 points at this block.
+ */
+struct pvm_boot_args {
+	u64	jump_tgt;
+
+	u64	regs[8];
+	u64	cpacr_el1;
+	u64	sctlr_el1;
+	u64	tcr_el1;
+	u64	mair_el1;
+	u64	ttbr0_el1;
+	u64	vbar_el1;
+
+	u64	tpidr_el1;
+	u64	sp_el1;
+};
+#endif
+
+#define PVM_BOOT_ARGS_OFF_JUMP_TGT	0
+#define PVM_BOOT_ARGS_OFF_X0		(PVM_BOOT_ARGS_OFF_JUMP_TGT + 8)
+#define PVM_BOOT_ARGS_OFF_X1		(PVM_BOOT_ARGS_OFF_X0 + 8)
+#define PVM_BOOT_ARGS_OFF_X2		(PVM_BOOT_ARGS_OFF_X1 + 8)
+#define PVM_BOOT_ARGS_OFF_X3		(PVM_BOOT_ARGS_OFF_X2 + 8)
+#define PVM_BOOT_ARGS_OFF_X4		(PVM_BOOT_ARGS_OFF_X3 + 8)
+#define PVM_BOOT_ARGS_OFF_X5		(PVM_BOOT_ARGS_OFF_X4 + 8)
+#define PVM_BOOT_ARGS_OFF_X6		(PVM_BOOT_ARGS_OFF_X5 + 8)
+#define PVM_BOOT_ARGS_OFF_X7		(PVM_BOOT_ARGS_OFF_X6 + 8)
+#define PVM_BOOT_ARGS_OFF_CPACR_EL1	(PVM_BOOT_ARGS_OFF_X7 + 8)
+#define PVM_BOOT_ARGS_OFF_SCTLR_EL1	(PVM_BOOT_ARGS_OFF_CPACR_EL1 + 8)
+#define PVM_BOOT_ARGS_OFF_TCR_EL1	(PVM_BOOT_ARGS_OFF_SCTLR_EL1 + 8)
+#define PVM_BOOT_ARGS_OFF_MAIR_EL1	(PVM_BOOT_ARGS_OFF_TCR_EL1 + 8)
+#define PVM_BOOT_ARGS_OFF_TTBR0_EL1	(PVM_BOOT_ARGS_OFF_MAIR_EL1 + 8)
+#define PVM_BOOT_ARGS_OFF_VBAR_EL1	(PVM_BOOT_ARGS_OFF_TTBR0_EL1 + 8)
+#define PVM_BOOT_ARGS_OFF_TPIDR_EL1	(PVM_BOOT_ARGS_OFF_VBAR_EL1 + 8)
+#define PVM_BOOT_ARGS_OFF_SP_EL1	(PVM_BOOT_ARGS_OFF_TPIDR_EL1 + 8)
+
+#endif /* SELFTEST_KVM_ARM64_PKVM_H */
tools/testing/selftests/kvm/lib/arm64/pvm-entry.S new file +58 -0
new file mode 100644
index 0000000000000..a0974cde78da6
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2025 Google LLC
+ * Author: Fuad Tabba <[email protected]>
+ * Author: Will Deacon <[email protected]>
+ */
+
+#include "pkvm.h"
+
+	.text
+	.balign 16
+	.globl	pvm_entry_start
+pvm_entry_start:
+	mov	x30, x0
+
+	ldr	x0, [x30, #PVM_BOOT_ARGS_OFF_X0]
+	ldr	x1, [x30, #PVM_BOOT_ARGS_OFF_X1]
+	ldr	x2, [x30, #PVM_BOOT_ARGS_OFF_X2]
+	ldr	x3, [x30, #PVM_BOOT_ARGS_OFF_X3]
+	ldr	x4, [x30, #PVM_BOOT_ARGS_OFF_X4]
+	ldr	x5, [x30, #PVM_BOOT_ARGS_OFF_X5]
+	ldr	x6, [x30, #PVM_BOOT_ARGS_OFF_X6]
+	ldr	x7, [x30, #PVM_BOOT_ARGS_OFF_X7]
+
+	ldr	x8, [x30, #PVM_BOOT_ARGS_OFF_JUMP_TGT]
+
+	ldr	x9, [x30, #PVM_BOOT_ARGS_OFF_CPACR_EL1]
+	msr	cpacr_el1, x9
+
+	ldr	x9, [x30, #PVM_BOOT_ARGS_OFF_TCR_EL1]
+	msr	tcr_el1, x9
+
+	ldr	x9, [x30, #PVM_BOOT_ARGS_OFF_MAIR_EL1]
+	msr	mair_el1, x9
+
+	ldr	x9, [x30, #PVM_BOOT_ARGS_OFF_TTBR0_EL1]
+	msr	ttbr0_el1, x9
+
+	ldr	x9, [x30, #PVM_BOOT_ARGS_OFF_VBAR_EL1]
+	msr	vbar_el1, x9
+
+	ldr	x9, [x30, #PVM_BOOT_ARGS_OFF_TPIDR_EL1]
+	msr	tpidr_el1, x9
+
+	ldr	x9, [x30, #PVM_BOOT_ARGS_OFF_SP_EL1]
+	mov	sp, x9
+
+	/* No stale TLB entries, and the writes above in effect, before the MMU is on. */
+	tlbi	vmalle1
+	dsb	nsh
+	isb
+	ldr	x9, [x30, #PVM_BOOT_ARGS_OFF_SCTLR_EL1]
+	msr	sctlr_el1, x9
+	isb
+
+	br	x8
+	.globl	pvm_entry_end
+pvm_entry_end:
Signature · 1 line
2.47.3
reviewer Reviewed-by on v1 6/16
#9 lore raw

Re: [PATCH 06/16] KVM: arm64: selftests: Add protected-VM library helpers

In reply to Fuad Tabba
Quoted 14 lines · Fuad Tabba

A protected-VM test has to create the VM with the protected type and the PSCI 0.2 vCPU feature, boot each vCPU through the trampoline from an identity-mapped page, and have the guest share the ucall pool before its first ucall. Secondaries start powered off for the guest to boot with CPU_ON. A vCPU becomes a valid CPU_ON target only at its first KVM_RUN.

Add lib/arm64/pkvm.c to do this for the tests. pkvm_vcpu_setup_boot_secondary() runs a secondary once with immediate_exit set, so that it becomes a CPU_ON target. The library also has a run loop, an SMCCC call for the guest, and a reader of the process's locked-memory count. The host adds a protected VM's pinned pages to that count. No capability advertises pKVM, so pkvm_supported() tries to create a protected VM.

Quoted 3 lines

One minor thing below, that could be addressed or not.

Quoted 45 lines
---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/include/arm64/pkvm.h        |  24 ++
 tools/testing/selftests/kvm/lib/arm64/pkvm.c  | 245 ++++++++++++++++++
 3 files changed, 270 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/lib/arm64/pkvm.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index c254e82f9a786..e173a9adad3f2 100644
@@ -34,6 +34,7 @@ LIBKVM_arm64 += lib/arm64/gic.c
 LIBKVM_arm64 += lib/arm64/gic_v3.c
 LIBKVM_arm64 += lib/arm64/gic_v3_its.c
 LIBKVM_arm64 += lib/arm64/handlers.S
+LIBKVM_arm64 += lib/arm64/pkvm.c
 LIBKVM_arm64 += lib/arm64/processor.c
 LIBKVM_arm64 += lib/arm64/pvm-entry.S
 LIBKVM_arm64 += lib/arm64/spinlock.c
tools/testing/selftests/kvm/include/arm64/pkvm.h +11 -0
index 6ef769b8ff977..413c7ac4a9f16 100644
@@ -9,6 +9,9 @@
 #define SELFTEST_KVM_ARM64_PKVM_H
 
 #ifndef __ASSEMBLER__
+#include "kvm_util.h"
+#include "ucall_common.h"
+
 /*
  * Register state the boot trampoline (pvm-entry.S) restores. Only PC and X0
  * survive to a protected guest's first run, so X0 points at this block.
@@ -27,6 +30,27 @@ struct pvm_boot_args {
 	u64	tpidr_el1;
 	u64	sp_el1;
 };
+
+/* Memslot of the trampoline's page. Test memslots start above it. */
+#define PKVM_IDMAP_MEMSLOT	1
+
+bool pkvm_supported(void);
+struct kvm_vm *pkvm_vm_create(unsigned int nr_vcpus, u32 features, struct kvm_vcpu *vcpus[]);
+struct kvm_vm *pkvm_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, void *guest_code);
+

tools/testing/selftests/kvm/include/arm64/pkvm.h struct pvm_boot_args { · line 50

Quoted 10 lines
tools/testing/selftests/kvm/include/arm64/pkvm.h +10 -0
+/* Call after all register setup: later register writes don't reach the guest. */
+void pkvm_vcpu_setup_boot(struct kvm_vcpu *vcpu, void *guest_code);
+
+/* Call before the primary's first run. */
+gpa_t pkvm_vcpu_setup_boot_secondary(struct kvm_vcpu *vcpu, void *guest_code);
+gpa_t pkvm_idmap_gpa(struct kvm_vm *vm);
+
+void pkvm_run_loop(struct kvm_vcpu *vcpu, void (*on_sync)(struct kvm_vcpu *vcpu, struct ucall *uc));
+size_t pkvm_host_locked_vm(void);
+

Could add a comment here:

// Called from the guest

As you have those 'Call' / contract comments above.

Quoted 260 lines
tools/testing/selftests/kvm/include/arm64/pkvm.h +3 -0
+unsigned long pkvm_hvc(u32 func, u64 arg0, u64 arg1, u64 arg2);
+
+void pkvm_guest_init(void);
 #endif
 
 #define PVM_BOOT_ARGS_OFF_JUMP_TGT	0
tools/testing/selftests/kvm/lib/arm64/pkvm.c new file +245 -0
new file mode 100644
index 0000000000000..f2d4cf48966e8
@@ -0,0 +1,245 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Protected-VM support for the arm64 KVM selftest library.
+ *
+ * Copyright (C) 2025 Google LLC
+ * Author: Fuad Tabba <[email protected]>
+ * Author: Will Deacon <[email protected]>
+ */
+
+#include <errno.h>
+
+#include <linux/arm-smccc.h>
+#include <linux/kernel.h>
+
+#include "kvm_util.h"
+#include "pkvm.h"
+#include "processor.h"
+#include "ucall_common.h"
+
+/* Set in the guest's copy only, by pkvm_vm_create(). */
+static gpa_t pkvm_ucall_pool_gpa;
+static size_t pkvm_ucall_pool_size;
+
+extern char pvm_entry_start[], pvm_entry_end[];
+
+#define ASSERT_BOOT_ARGS_OFF(field, off)				\
+	static_assert(offsetof(struct pvm_boot_args, field) == PVM_BOOT_ARGS_OFF_##off, #off)
+
+ASSERT_BOOT_ARGS_OFF(jump_tgt, JUMP_TGT);
+ASSERT_BOOT_ARGS_OFF(regs[0], X0);
+ASSERT_BOOT_ARGS_OFF(regs[1], X1);
+ASSERT_BOOT_ARGS_OFF(regs[2], X2);
+ASSERT_BOOT_ARGS_OFF(regs[3], X3);
+ASSERT_BOOT_ARGS_OFF(regs[4], X4);
+ASSERT_BOOT_ARGS_OFF(regs[5], X5);
+ASSERT_BOOT_ARGS_OFF(regs[6], X6);
+ASSERT_BOOT_ARGS_OFF(regs[7], X7);
+ASSERT_BOOT_ARGS_OFF(cpacr_el1, CPACR_EL1);
+ASSERT_BOOT_ARGS_OFF(sctlr_el1, SCTLR_EL1);
+ASSERT_BOOT_ARGS_OFF(tcr_el1, TCR_EL1);
+ASSERT_BOOT_ARGS_OFF(mair_el1, MAIR_EL1);
+ASSERT_BOOT_ARGS_OFF(ttbr0_el1, TTBR0_EL1);
+ASSERT_BOOT_ARGS_OFF(vbar_el1, VBAR_EL1);
+ASSERT_BOOT_ARGS_OFF(tpidr_el1, TPIDR_EL1);
+ASSERT_BOOT_ARGS_OFF(sp_el1, SP_EL1);
+
+unsigned long pkvm_hvc(u32 func, u64 arg0, u64 arg1, u64 arg2)
+{
+	struct arm_smccc_res res;
+
+	smccc_hvc(func, arg0, arg1, arg2, 0, 0, 0, 0, &res);
+	return res.a0;
+}
+
+/* Shares whole granules, so pages next to the pool are shared with it. */
+void pkvm_guest_init(void)
+{
+	u64 granule = pkvm_hvc(ARM_SMCCC_VENDOR_HYP_KVM_HYP_MEMINFO_FUNC_ID, 0, 0, 0);
+	gpa_t end = pkvm_ucall_pool_gpa + pkvm_ucall_pool_size;
+	gpa_t gpa;
+
+	for (gpa = pkvm_ucall_pool_gpa & ~(granule - 1); gpa < end; gpa += granule)
+		GUEST_ASSERT_EQ(pkvm_hvc(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID, gpa, 0, 0), 0);
+}
+
+bool pkvm_supported(void)
+{
+	int kvm_fd, vm_fd;
+
+	kvm_fd = open_kvm_dev_path_or_exit();
+	vm_fd = __kvm_ioctl(kvm_fd, KVM_CREATE_VM, (void *)KVM_VM_TYPE_ARM_PROTECTED);
+
+	if (vm_fd >= 0)
+		close(vm_fd);
+	close(kvm_fd);
+
+	return vm_fd >= 0;
+}
+
+/*
+ * The trampoline turns the MMU on, so it runs from an identity-mapped page:
+ * the first guest page of a host page at the top of the IPA space.
+ */
+gpa_t pkvm_idmap_gpa(struct kvm_vm *vm)
+{
+	unsigned int npages = vm_adjust_num_guest_pages(vm->mode, 1);
+
+	return (vm_compute_max_gfn(vm) + 1 - npages) * vm->page_size;
+}
+
+struct kvm_vm *pkvm_vm_create(unsigned int nr_vcpus, u32 features, struct kvm_vcpu *vcpus[])
+{
+	struct vm_shape shape = VM_SHAPE_DEFAULT;
+	struct kvm_vm *vm;
+	gpa_t idmap_gpa;
+	unsigned int i;
+
+	TEST_ASSERT(pvm_entry_end - pvm_entry_start +
+		    nr_vcpus * sizeof(struct pvm_boot_args) <= MIN_PAGE_SIZE,
+		    "trampoline and %u boot-args blocks overflow the idmap page", nr_vcpus);
+
+	shape.type = KVM_VM_TYPE_ARM_PROTECTED;
+	vm = __vm_create(shape, nr_vcpus, 0);
+
+	for (i = 0; i < nr_vcpus; i++) {
+		struct kvm_vcpu_init init;
+
+		kvm_get_default_vcpu_target(vm, &init);
+		init.features[0] |= features | BIT(KVM_ARM_VCPU_PSCI_0_2);
+		if (i > 0)
+			init.features[0] |= BIT(KVM_ARM_VCPU_POWER_OFF);
+
+		vcpus[i] = aarch64_vcpu_add(vm, i, &init, NULL);
+	}
+	kvm_arch_vm_finalize_vcpus(vm);
+
+	idmap_gpa = pkvm_idmap_gpa(vm);
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, idmap_gpa, PKVM_IDMAP_MEMSLOT,
+				    vm_adjust_num_guest_pages(vm->mode, 1), 0);
+	memcpy(addr_gpa2hva(vm, idmap_gpa), pvm_entry_start, pvm_entry_end - pvm_entry_start);
+	virt_map(vm, idmap_gpa, idmap_gpa, 1);
+
+	vm_init_descriptor_tables(vm);
+	for (i = 0; i < nr_vcpus; i++) {
+		vcpu_set_reg(vcpus[i], ARM64_CORE_REG(regs.pc), idmap_gpa);
+		vcpu_init_descriptor_tables(vcpus[i]);
+	}
+
+	write_guest_global(vm, pkvm_ucall_pool_gpa, get_ucall_pool_gpa(vm));
+	write_guest_global(vm, pkvm_ucall_pool_size,
+			   ucall_nr_pages_required(vm->page_size) * vm->page_size);
+
+	return vm;
+}
+
+/* Every block must be written before the primary's first run, when the host donates the page. */
+static gpa_t pkvm_boot_args_gpa(struct kvm_vcpu *vcpu)
+{
+	struct kvm_vm *vm = vcpu->vm;
+
+	return pkvm_idmap_gpa(vm) + vm->page_size - (vcpu->id + 1) * sizeof(struct pvm_boot_args);
+}
+
+static void pkvm_snapshot_boot_args(struct kvm_vcpu *vcpu, void *guest_code)
+{
+	struct pvm_boot_args *args = addr_gpa2hva(vcpu->vm, pkvm_boot_args_gpa(vcpu));
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(args->regs); i++)
+		args->regs[i] = vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.regs[i]));
+
+	args->jump_tgt  = (u64)guest_code;
+	args->cpacr_el1 = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_CPACR_EL1));
+	args->sctlr_el1 = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_SCTLR_EL1));
+	args->tcr_el1   = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TCR_EL1));
+	args->mair_el1  = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MAIR_EL1));
+	args->ttbr0_el1 = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TTBR0_EL1));
+	args->vbar_el1  = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_VBAR_EL1));
+	args->tpidr_el1 = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TPIDR_EL1));
+	args->sp_el1    = vcpu_get_reg(vcpu, ARM64_CORE_REG(sp_el1));
+}
+
+void pkvm_vcpu_setup_boot(struct kvm_vcpu *vcpu, void *guest_code)
+{
+	pkvm_snapshot_boot_args(vcpu, guest_code);
+	vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.regs[0]), pkvm_boot_args_gpa(vcpu));
+}
+
+gpa_t pkvm_vcpu_setup_boot_secondary(struct kvm_vcpu *vcpu, void *guest_code)
+{
+	int ret;
+
+	pkvm_snapshot_boot_args(vcpu, guest_code);
+
+	/*
+	 * A guest's CPU_ON requires the target's hyp vCPU, which KVM_RUN creates.
+	 * With immediate_exit set, KVM_RUN returns before the vCPU runs.
+	 */
+	vcpu->run->immediate_exit = 1;
+	ret = __vcpu_run(vcpu);
+	vcpu->run->immediate_exit = 0;
+	TEST_ASSERT(ret == -1 && errno == EINTR,
+		    "secondary hyp-vCPU registration: rc %d errno %d", ret, errno);
+
+	return pkvm_boot_args_gpa(vcpu);
+}
+
+struct kvm_vm *pkvm_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, void *guest_code)
+{
+	struct kvm_vm *vm = pkvm_vm_create(1, 0, vcpu);
+
+	pkvm_vcpu_setup_boot(*vcpu, guest_code);
+	return vm;
+}
+
+void pkvm_run_loop(struct kvm_vcpu *vcpu, void (*on_sync)(struct kvm_vcpu *vcpu, struct ucall *uc))
+{
+	bool done = false;
+	struct ucall uc;
+
+	while (!done) {
+		u64 cmd;
+
+		vcpu_run(vcpu);
+
+		switch (cmd = get_ucall(vcpu, &uc)) {
+		case UCALL_SYNC:
+			if (on_sync)
+				on_sync(vcpu, &uc);
+			break;
+		case UCALL_DONE:
+			done = true;
+			break;
+		case UCALL_ABORT:
+			REPORT_GUEST_ASSERT(uc);
+			break;
+		case UCALL_PRINTF:
+			REPORT_GUEST_PRINTF(uc);
+			break;
+		default:
+			TEST_FAIL("Unexpected guest exit: ucall %lu, arg %lu", cmd, uc.args[1]);
+		}
+	}
+}
+
+/* Bytes the current process has locked, per RLIMIT_MEMLOCK accounting. */
+size_t pkvm_host_locked_vm(void)
+{
+	size_t size = 0, locked_kb = 0;
+	char *line = NULL;
+	FILE *f;
+
+	f = fopen("/proc/self/status", "r");
+	TEST_ASSERT(f, "fopen /proc/self/status");
+
+	while (getline(&line, &size, f) > 0) {
+		if (sscanf(line, "VmLck:\t%zu kB", &locked_kb) == 1)
+			break;
+	}
+
+	free(line);
+	fclose(f);
+
+	return locked_kb << 10;
+}
Signature · 1 line
2.47.3
author reviewer
#10 lore raw

Re: [PATCH 06/16] KVM: arm64: selftests: Add protected-VM library helpers

On Fri, 6 Nov 2026 at 14:50, Joey Gouly <[email protected]> wrote:
[...]

Quoted 3 lines

One minor thing below, that could be addressed or not.

Thanks!

[...]

Quoted 9 lines

tools/testing/selftests/kvm/include/arm64/pkvm.h struct pvm_boot_args { · line 50

Quoted 3 lines
tools/testing/selftests/kvm/include/arm64/pkvm.h +3 -0
+void pkvm_run_loop(struct kvm_vcpu *vcpu, void (*on_sync)(struct kvm_vcpu *vcpu, struct ucall *uc));
+size_t pkvm_host_locked_vm(void);
+

Could add a comment here:

// Called from the guest

As you have those 'Call' / contract comments above.

Ack.

Cheers,
/fuad