On Mon, May 12, 2025 at 03:52:44AM -0700, Ganapatrao Kulkarni wrote:
> Add simple test to run guest code with NV enabled. With NV enabled,
> guest code runs in vEL2 context.
> 
> Signed-off-by: Ganapatrao Kulkarni <[email protected]>
> ---
>  .../selftests/kvm/arm64/nv_guest_hypervisor.c | 68 +++++++++++++++++++
>  1 file changed, 68 insertions(+)
>  create mode 100644 tools/testing/selftests/kvm/arm64/nv_guest_hypervisor.c
> 
> diff --git a/tools/testing/selftests/kvm/arm64/nv_guest_hypervisor.c 
> b/tools/testing/selftests/kvm/arm64/nv_guest_hypervisor.c
> new file mode 100644
> index 000000000000..7d7b3944e229
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/arm64/nv_guest_hypervisor.c
> @@ -0,0 +1,68 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (c) 2025 Ampere Computing LLC
> + */
> +#include <kvm_util.h>
> +#include <nv_util.h>
> +#include <processor.h>
> +
> +static void guest_code(void)
> +{
> +     if (read_sysreg(CurrentEL) == CurrentEL_EL2)
> +             GUEST_PRINTF("Test PASS\n");
> +     else
> +             GUEST_FAIL("Fail to run in vEL2\n");
> +
> +     GUEST_DONE();
> +}
> +
> +static void guest_undef_handler(struct ex_regs *regs)
> +{
> +     GUEST_FAIL("Unexpected exception far_el1 = 0x%lx", 
> read_sysreg(far_el1));
> +}
> +
> +static void test_run_vcpu(struct kvm_vcpu *vcpu)
> +{
> +     struct ucall uc;
> +
> +     do {
> +             vcpu_run(vcpu);
> +
> +             switch (get_ucall(vcpu, &uc)) {
> +             case UCALL_ABORT:
> +                     REPORT_GUEST_ASSERT(uc);
> +                     break;
> +             case UCALL_PRINTF:
> +                     printf("%s", uc.buffer);
> +                     break;
> +             case UCALL_DONE:
> +                     break;
> +             default:
> +                     TEST_FAIL("Unknown ucall %lu", uc.cmd);
> +             }
> +     } while (uc.cmd != UCALL_DONE);
> +}
> +
> +static void test_nv_guest_hypervisor(void)
> +{
> +     struct kvm_vcpu *vcpu;
> +     struct kvm_vm *vm;
> +     int gic_fd = -1;
> +
> +     vm = nv_vm_create_with_vcpus_gic(1, &vcpu, &gic_fd, guest_code);
> +     vm_init_descriptor_tables(vm);
> +     vcpu_init_descriptor_tables(vcpu);
> +     vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
> +                             ESR_ELx_EC_UNKNOWN, guest_undef_handler);
> +
> +     test_run_vcpu(vcpu);
> +
> +     vgic_v3_close(gic_fd);
> +     kvm_vm_free(vm);
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +     test_nv_guest_hypervisor();
> +     return 0;
> +}

The kernel this v2 series applied is based off of the latest kvmarm-next 
and tested this on QEMU TCG mode with the kvm-arm.mode=nested option added at 
boot.

Tested-by: Itaru Kitayama <[email protected]>

> -- 
> 2.48.1
> 

Reply via email to