Hi,
On Tue, 25 Aug 2026 at 17:02, Lorenzo Stoakes (ARM) <[email protected]> wrote:
..
> +int main(void)
> +{
> + struct nested_s2_state s2;
> + struct kvm_vcpu_init init;
> + struct kvm_vcpu *vcpu;
> + struct kvm_vm *vm;
> + struct ucall uc;
> + u64 npages;
> +
> + TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_EL2));
Would test_supports_el2() work better here? It honours the NV=0
opt-out, and since HAS_EL2 is set unconditionally below, NV=0 doesn't
disable this test as things stand.
Cheers,
/fuad
> + TEST_REQUIRE(kvm_check_cap(KVM_CAP_PRE_FAULT_MEMORY));
> +
> + vm = vm_create(1);
> +
> + kvm_get_default_vcpu_target(vm, &init);
> + init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2);
> + vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code);
> + kvm_arch_vm_finalize_vcpus(vm);
> +
> + npages = TEST_MEM_SIZE / vm->page_size;
> + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, TEST_MEM_GPA,
> + TEST_MEM_SLOT, npages, 0);
> + virt_map(vm, TEST_MEM_GPA, TEST_MEM_GPA, npages);
> +
> + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
> + NESTED_S2_ROOT_GPA, NESTED_S2_ROOT_SLOT,
> + vm_adjust_num_guest_pages(vm->mode, 1),
> 0);
> +
> + /* Run the guest until it has ERET'd from vEL2 to vEL1. */
> + vcpu_run(vcpu);
> + switch (get_ucall(vcpu, &uc)) {
> + case UCALL_SYNC:
> + TEST_ASSERT_EQ(uc.args[1], 1);
> + break;
> + case UCALL_ABORT:
> + REPORT_GUEST_ASSERT(uc);
> + break;
> + default:
> + TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
> + }
> +
> + /*
> + * The vCPU's last-run context is vEL1, backed by a shadow stage-2
> + * MMU. Enable nested stage-2 with an empty root so that the ioctl
> + * fails if it tries to interpret the userspace GPA as an L2 IPA.
> + *
> + * Prefault in two halves so that the second ioctl exercises a
> + * repeated shadow-MMU attach and canonical stage-2 swap.
> + *
> + * (Note that an implementation that wrongly populates shadow
> + * stage-2 page tables would not be caught as userland can't
> + * inspect these.)
> + */
> + s2 = enable_empty_nested_s2(vcpu);
> + pre_fault(vcpu, TEST_MEM_GPA, TEST_MEM_SIZE / 2);
> + pre_fault(vcpu, TEST_MEM_GPA + TEST_MEM_SIZE / 2, TEST_MEM_SIZE / 2);
> + restore_nested_s2(vcpu, &s2);
> +
> + /* Resume at vEL1 and touch the prefaulted range. */
> + vcpu_run(vcpu);
> + switch (get_ucall(vcpu, &uc)) {
> + case UCALL_DONE:
> + break;
> + case UCALL_ABORT:
> + REPORT_GUEST_ASSERT(uc);
> + break;
> + default:
> + TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
> + }
> +
> + kvm_vm_free(vm);
> + return 0;
> +}
>
> --
> 2.55.0
>