KVM: arm64: selftests: Basic nested guest support
v2 cover KVM: arm64: selftests: Basic nested guest support
Hi,
This is v2 of adding basic support for running nested guests (L2) in kselftest. After getting feedback from v1 [1], I mostly started over from scratch. Therefore you won't lose any context if you start here. Nonetheless, I still compiled the broad changes.
Patch 1 adds GPR save/restore code for guest, and vEL2 exception vectors.
Patch 2 adds other hypervisor helpers.
Patch 3 adds the hello_nested selftest to jump from vEL2 -> EL1 -> vEL2.
Patch 4 enhances the hello_nested selftest so that vEL1 handles a hypercall from EL1.
* Changes from v1 [1]:
- Set HCR_EL2.E2H for the guest.
- Pivoted from "userspace setting up everything" to "make L1 more like a proper hypervisor". Guest EL2 exception vectors, and GPR save/restore are added. There is also some infrastructure to save/restore system registers, right now only SP_EL1 is saved/restored to give L2 a stack. More system registers can be added in the future.
- Removed the stage-2 page table generator. The stage-2 page table generator was bad, and the changes needed for the previous point alone is already making the series larger, so I decided to not add any guest stage-2 code in this iteration.
Thanks!
[1]: https://lore.kernel.org/kvmarm/[email protected]/
Wei-Lin Chang (4):
KVM: arm64: selftests: Add GPR save/restore functions for NV
KVM: arm64: sefltests: Add helpers for guest hypervisors
KVM: arm64: sefltests: Add basic NV selftest
KVM: arm64: selftests: Enhance hello_nested test
tools/testing/selftests/kvm/Makefile.kvm | 4 + .../selftests/kvm/arm64/hello_nested.c | 132 +++++++++++++++++ .../selftests/kvm/include/arm64/nested.h | 62 ++++++++ tools/testing/selftests/kvm/lib/arm64/entry.S | 137 ++++++++++++++++++ .../selftests/kvm/lib/arm64/hyp-entry.S | 77 ++++++++++ .../testing/selftests/kvm/lib/arm64/nested.c | 58 ++++++++ 6 files changed, 470 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/hello_nested.c create mode 100644 tools/testing/selftests/kvm/include/arm64/nested.h create mode 100644 tools/testing/selftests/kvm/lib/arm64/entry.S create mode 100644 tools/testing/selftests/kvm/lib/arm64/hyp-entry.S create mode 100644 tools/testing/selftests/kvm/lib/arm64/nested.c
Signature · 1 line
2.43.0
PATCH v2 1/4 KVM: arm64: selftests: Add GPR save/restore functions for NV
Adapt entry.S and hyp-entry.S from arch/arm64/kvm/hyp so that guest hypervisors can save and restore GPRs, and provide exception handlers to regain control after the nested guest exits. Other system register save/restore will be added later on demand.
--- tools/testing/selftests/kvm/Makefile.kvm | 3 + .../selftests/kvm/include/arm64/nested.h | 45 ++++++ tools/testing/selftests/kvm/lib/arm64/entry.S | 132 ++++++++++++++++++ .../selftests/kvm/lib/arm64/hyp-entry.S | 77 ++++++++++ .../testing/selftests/kvm/lib/arm64/nested.c | 12 ++ 5 files changed, 269 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/arm64/nested.h create mode 100644 tools/testing/selftests/kvm/lib/arm64/entry.S create mode 100644 tools/testing/selftests/kvm/lib/arm64/hyp-entry.S create mode 100644 tools/testing/selftests/kvm/lib/arm64/nested.c
tools/testing/selftests/kvm/Makefile.kvm +3 -0
@@ -30,10 +30,13 @@ LIBKVM_x86 += lib/x86/svm.c LIBKVM_x86 += lib/x86/ucall.c LIBKVM_x86 += lib/x86/vmx.c +LIBKVM_arm64 += lib/arm64/entry.S 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/hyp-entry.S +LIBKVM_arm64 += lib/arm64/nested.c LIBKVM_arm64 += lib/arm64/processor.c LIBKVM_arm64 += lib/arm64/spinlock.c LIBKVM_arm64 += lib/arm64/ucall.c
tools/testing/selftests/kvm/include/arm64/nested.h new file +45 -0
45 lines not on this page · raw message
tools/testing/selftests/kvm/lib/arm64/entry.S new file +132 -0
132 lines not on this page · raw message
tools/testing/selftests/kvm/lib/arm64/hyp-entry.S new file +77 -0
77 lines not on this page · raw message
tools/testing/selftests/kvm/lib/arm64/nested.c new file +12 -0
@@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* ARM64 Nested virtualization helpers +*/ + +#include "nested.h" +#include "test_util.h" + +void __hyp_exception (u64 type) +{ +GUEST_FAIL ("Unexpected hyp exception! type: %lx\n" , type); +}
Signature · 1 line
2.43.0
PATCH v2 2/4 KVM: arm64: sefltests: Add helpers for guest hypervisors
In reply to Wei-Lin ChangAdd helpers so that guest hypervisors can run nested guests. SP_EL1 save/restore is added to allow nested guests to use a stack.
--- .../selftests/kvm/include/arm64/nested.h | 17 +++++++ tools/testing/selftests/kvm/lib/arm64/entry.S | 5 ++ .../testing/selftests/kvm/lib/arm64/nested.c | 46 +++++++++++++++++++ 3 files changed, 68 insertions(+)
tools/testing/selftests/kvm/include/arm64/nested.h +17 -0
@@ -21,8 +21,17 @@extern char hyp_vectors[]; +enum vcpu_sysreg { + __INVALID_SYSREG__,/* 0 is reserved as an invalid value */ + +SP_EL1 , + +NR_SYS_REGS +}; +struct cpu_context {struct user_pt_regs regs;/* sp = sp_el0 */ +u64 sys_regs[NR_SYS_REGS ]; };struct vcpu {
@@ -37,9 +46,17 @@ struct hyp_data {struct cpu_context hyp_context; }; +void prepare_hyp (void ); +void init_vcpu (struct vcpu *vcpu,vm_paddr_t l2_pc,vm_paddr_t l2_stack_top); +int run_l2 (struct vcpu *vcpu,struct hyp_data *hyp_data); + +void do_hvc (void );u64 __guest_enter (struct vcpu *vcpu,struct cpu_context *hyp_context);void __hyp_exception (u64 type); +void __sysreg_save_el1_state (struct cpu_context *ctxt); +void __sysreg_restore_el1_state (struct cpu_context *ctxt); +#endif /* !__ASSEMBLER__ */ #endif /* SELFTEST_KVM_NESTED_H */
tools/testing/selftests/kvm/lib/arm64/entry.S +5 -0
@@ -3,6 +3,11 @@ * adapted from arch/arm64/kvm/hyp/entry.S */ + .globl do_hvc + do_hvc: + hvc #0 + ret + /* * Manually define these for now */
tools/testing/selftests/kvm/lib/arm64/nested.c +46 -0
53 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 2/4] KVM: arm64: sefltests: Add helpers for guest hypervisors
Quoted 33 lines · Wei-Lin Chang
Add helpers so that guest hypervisors can run nested guests.
SP_EL1save/restore is added to allow nested guests to use a stack.Signed-off-by Wei-Lin Chang <[email protected]>--- .../selftests/kvm/include/arm64/nested.h | 17 +++++++ tools/testing/selftests/kvm/lib/arm64/entry.S | 5 ++ .../testing/selftests/kvm/lib/arm64/nested.c | 46 +++++++++++++++++++ 3 files changed, 68 insertions(+)tools/testing/selftests/kvm/include/arm64/nested.h +9
-0@@ -21,8 +21,17 @@extern char hyp_vectors[]; +enum vcpu_sysreg { + __INVALID_SYSREG__,/* 0 is reserved as an invalid value */ + +SP_EL1 , + +NR_SYS_REGS +}; +struct cpu_context {struct user_pt_regs regs;/* sp = sp_el0 */ +u64 sys_regs[NR_SYS_REGS ]; };struct vcpu {
tools/testing/selftests/kvm/include/arm64/nested.h struct hyp_data { · line 47
Quoted 2 lines
tools/testing/selftests/kvm/include/arm64/nested.h +0
-0@@ -37,9 +46,17 @@ struct hyp_data {struct cpu_context hyp_context; };
I am not sure of these structs you introduced only for nested guest feature testing, as the KVM arm64 code they are quite complex and involved, extracring part of those and add members as hello_nested or simliar tests evolve, then add test cases to me seems fragile.
But if you have strong reason to add these would you mind explaining a bit?
Thanks,
Itaru.
Quoted 92 lines
tools/testing/selftests/kvm/include/arm64/nested.h +8
-0+void prepare_hyp (void ); +void init_vcpu (struct vcpu *vcpu,vm_paddr_t l2_pc,vm_paddr_t l2_stack_top); +int run_l2 (struct vcpu *vcpu,struct hyp_data *hyp_data); + +void do_hvc (void );u64 __guest_enter (struct vcpu *vcpu,struct cpu_context *hyp_context);void __hyp_exception (u64 type); +void __sysreg_save_el1_state (struct cpu_context *ctxt); +void __sysreg_restore_el1_state (struct cpu_context *ctxt); +#endif /* !__ASSEMBLER__ */ #endif /* SELFTEST_KVM_NESTED_H */ tools/testing/selftests/kvm/lib/arm64/entry.S +5
-0@@ -3,6 +3,11 @@ * adapted from arch/arm64/kvm/hyp/entry.S */ + .globl do_hvc + do_hvc: + hvc #0 + ret + /* * Manually define these for now */tools/testing/selftests/kvm/lib/arm64/nested.c +46
-0@@ -4,7 +4,53 @@*/ #include "nested.h" +#include "processor.h" #include "test_util.h" +#include <asm/sysreg.h> + +void prepare_hyp (void ) +{ +write_sysreg (HCR_EL2_E2H |HCR_EL2_RW , hcr_el2); +write_sysreg (hyp_vectors, vbar_el2); +isb (); +} + +void init_vcpu (struct vcpu *vcpu,vm_paddr_t l2_pc,vm_paddr_t l2_stack_top) +{ +memset (vcpu,0 ,sizeof (*vcpu)); + vcpu->context.regs.pc = l2_pc; + vcpu->context.regs.pstate = PSR_MODE_EL1h |PSR_D_BIT |PSR_A_BIT |PSR_I_BIT |PSR_F_BIT ; + vcpu->context.sys_regs[SP_EL1 ] = l2_stack_top; +} + +void __sysreg_save_el1_state (struct cpu_context *ctxt) +{ + ctxt->sys_regs[SP_EL1 ] =read_sysreg (sp_el1); +} + +void __sysreg_restore_el1_state (struct cpu_context *ctxt) +{ +write_sysreg (ctxt->sys_regs[SP_EL1 ], sp_el1); +} + +int run_l2 (struct vcpu *vcpu,struct hyp_data *hyp_data) +{ +u64 ret; + +__sysreg_restore_el1_state (&vcpu->context); + +write_sysreg (vcpu->context.regs.pstate, spsr_el2); +write_sysreg (vcpu->context.regs.pc, elr_el2); + + ret =__guest_enter (vcpu, &hyp_data->hyp_context); + + vcpu->context.regs.pc =read_sysreg (elr_el2); + vcpu->context.regs.pstate =read_sysreg (spsr_el2); + +__sysreg_save_el1_state (&vcpu->context); + +return ret; +}void __hyp_exception (u64 type) {Signature · 1 line
2.43.0
Re: [PATCH v2 2/4] KVM: arm64: sefltests: Add helpers for guest hypervisors
Quoted 32 lines · Itaru Kitayama
Quoted 31 lines · Wei-Lin Chang
Add helpers so that guest hypervisors can run nested guests.
SP_EL1save/restore is added to allow nested guests to use a stack.Signed-off-by Wei-Lin Chang <[email protected]>--- .../selftests/kvm/include/arm64/nested.h | 17 +++++++ tools/testing/selftests/kvm/lib/arm64/entry.S | 5 ++ .../testing/selftests/kvm/lib/arm64/nested.c | 46 +++++++++++++++++++ 3 files changed, 68 insertions(+)tools/testing/selftests/kvm/include/arm64/nested.h +9
-0@@ -21,8 +21,17 @@extern char hyp_vectors[]; +enum vcpu_sysreg { + __INVALID_SYSREG__,/* 0 is reserved as an invalid value */ + +SP_EL1 , + +NR_SYS_REGS +}; +struct cpu_context {struct user_pt_regs regs;/* sp = sp_el0 */ +u64 sys_regs[NR_SYS_REGS ]; };
Quoted 10 lines
tools/testing/selftests/kvm/include/arm64/nested.h struct hyp_data { · line 47
Quoted 4 lines
tools/testing/selftests/kvm/include/arm64/nested.h +0
-0struct vcpu {@@ -37,9 +46,17 @@ struct hyp_data {struct cpu_context hyp_context; };I am not sure of these structs you introduced only for nested guest feature testing, as the KVM arm64 code they are quite complex and involved, extracring part of those and add members as
hello_nestedor simliar tests evolve, then add test cases to me seems fragile.
But if you have strong reason to add these would you mind explaining a bit?
Sorry, I don't quite get all of your points. I understand your argument being evolving these structs as time goes is fragile. For this didn't KVM itself evolve like this?
As for having these structs, how can we make L1 a small hypervisor without them?
Thanks,
Wei-Lin Chang
Quoted 96 lines
Thanks,
Itaru.Quoted 92 lines
tools/testing/selftests/kvm/include/arm64/nested.h +8
-0+void prepare_hyp (void ); +void init_vcpu (struct vcpu *vcpu,vm_paddr_t l2_pc,vm_paddr_t l2_stack_top); +int run_l2 (struct vcpu *vcpu,struct hyp_data *hyp_data); + +void do_hvc (void );u64 __guest_enter (struct vcpu *vcpu,struct cpu_context *hyp_context);void __hyp_exception (u64 type); +void __sysreg_save_el1_state (struct cpu_context *ctxt); +void __sysreg_restore_el1_state (struct cpu_context *ctxt); +#endif /* !__ASSEMBLER__ */ #endif /* SELFTEST_KVM_NESTED_H */ tools/testing/selftests/kvm/lib/arm64/entry.S +5
-0@@ -3,6 +3,11 @@ * adapted from arch/arm64/kvm/hyp/entry.S */ + .globl do_hvc + do_hvc: + hvc #0 + ret + /* * Manually define these for now */tools/testing/selftests/kvm/lib/arm64/nested.c +46
-0@@ -4,7 +4,53 @@*/ #include "nested.h" +#include "processor.h" #include "test_util.h" +#include <asm/sysreg.h> + +void prepare_hyp (void ) +{ +write_sysreg (HCR_EL2_E2H |HCR_EL2_RW , hcr_el2); +write_sysreg (hyp_vectors, vbar_el2); +isb (); +} + +void init_vcpu (struct vcpu *vcpu,vm_paddr_t l2_pc,vm_paddr_t l2_stack_top) +{ +memset (vcpu,0 ,sizeof (*vcpu)); + vcpu->context.regs.pc = l2_pc; + vcpu->context.regs.pstate = PSR_MODE_EL1h |PSR_D_BIT |PSR_A_BIT |PSR_I_BIT |PSR_F_BIT ; + vcpu->context.sys_regs[SP_EL1 ] = l2_stack_top; +} + +void __sysreg_save_el1_state (struct cpu_context *ctxt) +{ + ctxt->sys_regs[SP_EL1 ] =read_sysreg (sp_el1); +} + +void __sysreg_restore_el1_state (struct cpu_context *ctxt) +{ +write_sysreg (ctxt->sys_regs[SP_EL1 ], sp_el1); +} + +int run_l2 (struct vcpu *vcpu,struct hyp_data *hyp_data) +{ +u64 ret; + +__sysreg_restore_el1_state (&vcpu->context); + +write_sysreg (vcpu->context.regs.pstate, spsr_el2); +write_sysreg (vcpu->context.regs.pc, elr_el2); + + ret =__guest_enter (vcpu, &hyp_data->hyp_context); + + vcpu->context.regs.pc =read_sysreg (elr_el2); + vcpu->context.regs.pstate =read_sysreg (spsr_el2); + +__sysreg_save_el1_state (&vcpu->context); + +return ret; +}void __hyp_exception (u64 type) {Signature · 1 line
2.43.0
Re: [PATCH v2 2/4] KVM: arm64: sefltests: Add helpers for guest hypervisors
Quoted 44 lines · Wei-Lin Chang
Quoted 32 lines · Itaru Kitayama
Quoted 31 lines · Wei-Lin Chang
Add helpers so that guest hypervisors can run nested guests.
SP_EL1save/restore is added to allow nested guests to use a stack.Signed-off-by Wei-Lin Chang <[email protected]>--- .../selftests/kvm/include/arm64/nested.h | 17 +++++++ tools/testing/selftests/kvm/lib/arm64/entry.S | 5 ++ .../testing/selftests/kvm/lib/arm64/nested.c | 46 +++++++++++++++++++ 3 files changed, 68 insertions(+)tools/testing/selftests/kvm/include/arm64/nested.h +9
-0@@ -21,8 +21,17 @@extern char hyp_vectors[]; +enum vcpu_sysreg { + __INVALID_SYSREG__,/* 0 is reserved as an invalid value */ + +SP_EL1 , + +NR_SYS_REGS +}; +struct cpu_context {struct user_pt_regs regs;/* sp = sp_el0 */ +u64 sys_regs[NR_SYS_REGS ]; };Quoted 10 lines
tools/testing/selftests/kvm/include/arm64/nested.h struct hyp_data { · line 47
Quoted 4 lines
tools/testing/selftests/kvm/include/arm64/nested.h +0
-0struct vcpu {@@ -37,9 +46,17 @@ struct hyp_data {struct cpu_context hyp_context; };I am not sure of these structs you introduced only for nested guest feature testing, as the KVM arm64 code they are quite complex and involved, extracring part of those and add members as
hello_nestedor simliar tests evolve, then add test cases to me seems fragile.
But if you have strong reason to add these would you mind explaining a bit?
Quoted 6 lines
Sorry, I don't quite get all of your points. I understand your argument being evolving these structs as time goes is fragile. For this didn't KVM itself evolve like this?
As for having these structs, how can we make L1 a small hypervisor without them?
You're correct and I was wrong. We will just have to change the structs for nested virtualization selftests as we add more test cases.
Itaru.
Quoted 100 lines
Thanks,
Wei-Lin ChangQuoted 96 lines
Thanks,
Itaru.Quoted 92 lines
tools/testing/selftests/kvm/include/arm64/nested.h +8
-0+void prepare_hyp (void ); +void init_vcpu (struct vcpu *vcpu,vm_paddr_t l2_pc,vm_paddr_t l2_stack_top); +int run_l2 (struct vcpu *vcpu,struct hyp_data *hyp_data); + +void do_hvc (void );u64 __guest_enter (struct vcpu *vcpu,struct cpu_context *hyp_context);void __hyp_exception (u64 type); +void __sysreg_save_el1_state (struct cpu_context *ctxt); +void __sysreg_restore_el1_state (struct cpu_context *ctxt); +#endif /* !__ASSEMBLER__ */ #endif /* SELFTEST_KVM_NESTED_H */ tools/testing/selftests/kvm/lib/arm64/entry.S +5
-0@@ -3,6 +3,11 @@ * adapted from arch/arm64/kvm/hyp/entry.S */ + .globl do_hvc + do_hvc: + hvc #0 + ret + /* * Manually define these for now */tools/testing/selftests/kvm/lib/arm64/nested.c +46
-0@@ -4,7 +4,53 @@*/ #include "nested.h" +#include "processor.h" #include "test_util.h" +#include <asm/sysreg.h> + +void prepare_hyp (void ) +{ +write_sysreg (HCR_EL2_E2H |HCR_EL2_RW , hcr_el2); +write_sysreg (hyp_vectors, vbar_el2); +isb (); +} + +void init_vcpu (struct vcpu *vcpu,vm_paddr_t l2_pc,vm_paddr_t l2_stack_top) +{ +memset (vcpu,0 ,sizeof (*vcpu)); + vcpu->context.regs.pc = l2_pc; + vcpu->context.regs.pstate = PSR_MODE_EL1h |PSR_D_BIT |PSR_A_BIT |PSR_I_BIT |PSR_F_BIT ; + vcpu->context.sys_regs[SP_EL1 ] = l2_stack_top; +} + +void __sysreg_save_el1_state (struct cpu_context *ctxt) +{ + ctxt->sys_regs[SP_EL1 ] =read_sysreg (sp_el1); +} + +void __sysreg_restore_el1_state (struct cpu_context *ctxt) +{ +write_sysreg (ctxt->sys_regs[SP_EL1 ], sp_el1); +} + +int run_l2 (struct vcpu *vcpu,struct hyp_data *hyp_data) +{ +u64 ret; + +__sysreg_restore_el1_state (&vcpu->context); + +write_sysreg (vcpu->context.regs.pstate, spsr_el2); +write_sysreg (vcpu->context.regs.pc, elr_el2); + + ret =__guest_enter (vcpu, &hyp_data->hyp_context); + + vcpu->context.regs.pc =read_sysreg (elr_el2); + vcpu->context.regs.pstate =read_sysreg (spsr_el2); + +__sysreg_save_el1_state (&vcpu->context); + +return ret; +}void __hyp_exception (u64 type) {Signature · 1 line
2.43.0
PATCH v2 3/4 KVM: arm64: sefltests: Add basic NV selftest
In reply to Wei-Lin ChangThis selftest simply starts an L1, which starts its own guest (L2). L2 runs without stage-1 and 2 translations, it calls an HVC to jump back to L1.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/hello_nested.c | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/hello_nested.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -168,6 +168,7 @@ TEST_GEN_PROGS_arm64 += arm64/arch_timer_edge_cases TEST_GEN_PROGS_arm64 += arm64/at TEST_GEN_PROGS_arm64 += arm64/debug-exceptions TEST_GEN_PROGS_arm64 += arm64/hello_el2 +TEST_GEN_PROGS_arm64 += arm64/hello_nested TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls TEST_GEN_PROGS_arm64 += arm64/external_aborts
tools/testing/selftests/kvm/arm64/hello_nested.c new file +103 -0
103 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 3/4] KVM: arm64: sefltests: Add basic NV selftest
Quoted 3 lines · Wei-Lin Chang
This selftest simply starts an L1, which starts its own guest (L2). L2 runs without stage-1 and 2 translations, it calls an HVC to jump back to L1.
How do you disable both the nested guest (L2)'s MMU and stage 2 translations?
Itaru.
Quoted 132 lines
Signed-off-by Wei-Lin Chang <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/hello_nested.c | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/hello_nested.ctools/testing/selftests/kvm/Makefile.kvm +1
-0@@ -168,6 +168,7 @@ TEST_GEN_PROGS_arm64 += arm64/arch_timer_edge_cases TEST_GEN_PROGS_arm64 += arm64/at TEST_GEN_PROGS_arm64 += arm64/debug-exceptions TEST_GEN_PROGS_arm64 += arm64/hello_el2 +TEST_GEN_PROGS_arm64 += arm64/hello_nested TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls TEST_GEN_PROGS_arm64 += arm64/external_abortstools/testing/selftests/kvm/arm64/hello_nested.c new file +103
-0@@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* hello_nested - Go from vEL2 to EL1 then back +*/ + +#include "nested.h" +#include "processor.h" +#include "test_util.h" +#include "ucall.h" + +#define XLATE2GPA (0xABCD) +#define L2STACKSZ (0x100) + +/* +* TPIDR_EL2 is used to store vcpu id, so save and restore it. +*/ +static vm_paddr_t ucall_translate_to_gpa (void *gva) +{ +vm_paddr_t gpa; +u64 vcpu_id =read_sysreg (tpidr_el2); + +GUEST_SYNC2 (XLATE2GPA , gva); + +/* get the result from userspace */ + gpa =read_sysreg (tpidr_el2); + +write_sysreg (vcpu_id, tpidr_el2); + +return gpa; +} + +static void l2_guest_code (void ) +{ +do_hvc (); +} + +static void guest_code (void ) +{ +struct vcpu vcpu; +struct hyp_data hyp_data; +int ret; +vm_paddr_t l2_pc, l2_stack_top; +/* force 16-byte alignment for the stack pointer */ + u8 l2_stack[L2STACKSZ] __attribute__((aligned(16))); + +GUEST_ASSERT_EQ (get_current_el (),2 ); +GUEST_PRINTF ("vEL2 entry\n" ); + + l2_pc =ucall_translate_to_gpa (l2_guest_code); + l2_stack_top =ucall_translate_to_gpa (&l2_stack[L2STACKSZ ]); + +init_vcpu (&vcpu, l2_pc, l2_stack_top); +prepare_hyp (); + + ret =run_l2 (&vcpu, &hyp_data); +GUEST_ASSERT_EQ (ret,ARM_EXCEPTION_TRAP ); +GUEST_DONE (); +} + +int main (void ) +{ +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +vm_paddr_t gpa; + +TEST_REQUIRE (kvm_check_cap (KVM_CAP_ARM_EL2 )); + 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); + +while (true) { +vcpu_run (vcpu); + +switch (get_ucall (vcpu, &uc)) { +case UCALL_SYNC : +if (uc.args[0 ] ==XLATE2GPA ) { + gpa =addr_gva2gpa (vm, (vm_vaddr_t )uc.args[1 ]); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_TPIDR_EL2 ), gpa); + } +break ; +case UCALL_PRINTF : +pr_info ("%s" , uc.buffer); +break ; +case UCALL_DONE : +pr_info ("DONE!\n" ); + goto end; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); + fallthrough; +default : +TEST_FAIL ("Unhandled ucall: %ld\n" , uc.cmd); + } + } + +end: +kvm_vm_free (vm); +return 0 ; +}Signature · 1 line
2.43.0
Re: [PATCH v2 3/4] KVM: arm64: sefltests: Add basic NV selftest
Hi Itaru,
Quoted 7 lines · Itaru Kitayama
Quoted 3 lines · Wei-Lin Chang
This selftest simply starts an L1, which starts its own guest (L2). L2 runs without stage-1 and 2 translations, it calls an HVC to jump back to L1.
How do you disable both the nested guest (L2)'s MMU and stage 2 translations?
Guest stage-2 is disabled by not setting HCR_EL2.VM in prepare_hyp(), and stage-1 is disabled by not writing to SCTLR_EL12 in init_vcpu(), effectively using the default value set by L0. However since SCTLR_EL1 has many architecturally UNKNOWN bits (including SCTLR_EL1.M), it should be better to write a value before running L2 I suppose...
Thanks,
Wei-Lin Chang
Quoted 135 lines
Itaru.
Quoted 132 lines
Signed-off-by Wei-Lin Chang <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/hello_nested.c | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/hello_nested.ctools/testing/selftests/kvm/Makefile.kvm +1
-0@@ -168,6 +168,7 @@ TEST_GEN_PROGS_arm64 += arm64/arch_timer_edge_cases TEST_GEN_PROGS_arm64 += arm64/at TEST_GEN_PROGS_arm64 += arm64/debug-exceptions TEST_GEN_PROGS_arm64 += arm64/hello_el2 +TEST_GEN_PROGS_arm64 += arm64/hello_nested TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls TEST_GEN_PROGS_arm64 += arm64/external_abortstools/testing/selftests/kvm/arm64/hello_nested.c new file +103
-0@@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* hello_nested - Go from vEL2 to EL1 then back +*/ + +#include "nested.h" +#include "processor.h" +#include "test_util.h" +#include "ucall.h" + +#define XLATE2GPA (0xABCD) +#define L2STACKSZ (0x100) + +/* +* TPIDR_EL2 is used to store vcpu id, so save and restore it. +*/ +static vm_paddr_t ucall_translate_to_gpa (void *gva) +{ +vm_paddr_t gpa; +u64 vcpu_id =read_sysreg (tpidr_el2); + +GUEST_SYNC2 (XLATE2GPA , gva); + +/* get the result from userspace */ + gpa =read_sysreg (tpidr_el2); + +write_sysreg (vcpu_id, tpidr_el2); + +return gpa; +} + +static void l2_guest_code (void ) +{ +do_hvc (); +} + +static void guest_code (void ) +{ +struct vcpu vcpu; +struct hyp_data hyp_data; +int ret; +vm_paddr_t l2_pc, l2_stack_top; +/* force 16-byte alignment for the stack pointer */ + u8 l2_stack[L2STACKSZ] __attribute__((aligned(16))); + +GUEST_ASSERT_EQ (get_current_el (),2 ); +GUEST_PRINTF ("vEL2 entry\n" ); + + l2_pc =ucall_translate_to_gpa (l2_guest_code); + l2_stack_top =ucall_translate_to_gpa (&l2_stack[L2STACKSZ ]); + +init_vcpu (&vcpu, l2_pc, l2_stack_top); +prepare_hyp (); + + ret =run_l2 (&vcpu, &hyp_data); +GUEST_ASSERT_EQ (ret,ARM_EXCEPTION_TRAP ); +GUEST_DONE (); +} + +int main (void ) +{ +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +vm_paddr_t gpa; + +TEST_REQUIRE (kvm_check_cap (KVM_CAP_ARM_EL2 )); + 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); + +while (true) { +vcpu_run (vcpu); + +switch (get_ucall (vcpu, &uc)) { +case UCALL_SYNC : +if (uc.args[0 ] ==XLATE2GPA ) { + gpa =addr_gva2gpa (vm, (vm_vaddr_t )uc.args[1 ]); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_TPIDR_EL2 ), gpa); + } +break ; +case UCALL_PRINTF : +pr_info ("%s" , uc.buffer); +break ; +case UCALL_DONE : +pr_info ("DONE!\n" ); + goto end; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); + fallthrough; +default : +TEST_FAIL ("Unhandled ucall: %ld\n" , uc.cmd); + } + } + +end: +kvm_vm_free (vm); +return 0 ; +}Signature · 1 line
2.43.0
Re: [PATCH v2 3/4] KVM: arm64: sefltests: Add basic NV selftest
Quoted 8 lines · Wei-Lin Chang
Hi Itaru,
Quoted 5 lines · Itaru Kitayama
Quoted 3 lines · Wei-Lin Chang
This selftest simply starts an L1, which starts its own guest (L2). L2 runs without stage-1 and 2 translations, it calls an HVC to jump back to L1.
Quoted 8 lines
Quoted 2 lines
How do you disable both the nested guest (L2)'s MMU and stage 2 translations?
Guest stage-2 is disabled by not setting HCR_EL2.VM in
prepare_hyp(), and stage-1 is disabled by not writing toSCTLR_EL12ininit_vcpu(), effectively using the default value set by L0. However sinceSCTLR_EL1has many architecturally UNKNOWN bits (including SCTLR_EL1.M), it should be better to write a value before running L2 I suppose...
Thanks. What do you think of using copy_el2_to_el1() macro in at.c, so we can prepare in guest_code() to manipulate the SCTLR_EL12 System register with the sensible programmed values?
Itaru.
Quoted 139 lines
Thanks,
Wei-Lin ChangQuoted 135 lines
Itaru.
Quoted 132 lines
Signed-off-by Wei-Lin Chang <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/hello_nested.c | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/hello_nested.ctools/testing/selftests/kvm/Makefile.kvm +1
-0@@ -168,6 +168,7 @@ TEST_GEN_PROGS_arm64 += arm64/arch_timer_edge_cases TEST_GEN_PROGS_arm64 += arm64/at TEST_GEN_PROGS_arm64 += arm64/debug-exceptions TEST_GEN_PROGS_arm64 += arm64/hello_el2 +TEST_GEN_PROGS_arm64 += arm64/hello_nested TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls TEST_GEN_PROGS_arm64 += arm64/external_abortstools/testing/selftests/kvm/arm64/hello_nested.c new file +103
-0@@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* hello_nested - Go from vEL2 to EL1 then back +*/ + +#include "nested.h" +#include "processor.h" +#include "test_util.h" +#include "ucall.h" + +#define XLATE2GPA (0xABCD) +#define L2STACKSZ (0x100) + +/* +* TPIDR_EL2 is used to store vcpu id, so save and restore it. +*/ +static vm_paddr_t ucall_translate_to_gpa (void *gva) +{ +vm_paddr_t gpa; +u64 vcpu_id =read_sysreg (tpidr_el2); + +GUEST_SYNC2 (XLATE2GPA , gva); + +/* get the result from userspace */ + gpa =read_sysreg (tpidr_el2); + +write_sysreg (vcpu_id, tpidr_el2); + +return gpa; +} + +static void l2_guest_code (void ) +{ +do_hvc (); +} + +static void guest_code (void ) +{ +struct vcpu vcpu; +struct hyp_data hyp_data; +int ret; +vm_paddr_t l2_pc, l2_stack_top; +/* force 16-byte alignment for the stack pointer */ + u8 l2_stack[L2STACKSZ] __attribute__((aligned(16))); + +GUEST_ASSERT_EQ (get_current_el (),2 ); +GUEST_PRINTF ("vEL2 entry\n" ); + + l2_pc =ucall_translate_to_gpa (l2_guest_code); + l2_stack_top =ucall_translate_to_gpa (&l2_stack[L2STACKSZ ]); + +init_vcpu (&vcpu, l2_pc, l2_stack_top); +prepare_hyp (); + + ret =run_l2 (&vcpu, &hyp_data); +GUEST_ASSERT_EQ (ret,ARM_EXCEPTION_TRAP ); +GUEST_DONE (); +} + +int main (void ) +{ +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +vm_paddr_t gpa; + +TEST_REQUIRE (kvm_check_cap (KVM_CAP_ARM_EL2 )); + 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); + +while (true) { +vcpu_run (vcpu); + +switch (get_ucall (vcpu, &uc)) { +case UCALL_SYNC : +if (uc.args[0 ] ==XLATE2GPA ) { + gpa =addr_gva2gpa (vm, (vm_vaddr_t )uc.args[1 ]); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_TPIDR_EL2 ), gpa); + } +break ; +case UCALL_PRINTF : +pr_info ("%s" , uc.buffer); +break ; +case UCALL_DONE : +pr_info ("DONE!\n" ); + goto end; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); + fallthrough; +default : +TEST_FAIL ("Unhandled ucall: %ld\n" , uc.cmd); + } + } + +end: +kvm_vm_free (vm); +return 0 ; +}Signature · 1 line
2.43.0
Re: [PATCH v2 3/4] KVM: arm64: sefltests: Add basic NV selftest
Quoted 18 lines · Itaru Kitayama
Quoted 8 lines · Wei-Lin Chang
Hi Itaru,
Quoted 5 lines · Itaru Kitayama
Quoted 3 lines · Wei-Lin Chang
This selftest simply starts an L1, which starts its own guest (L2). L2 runs without stage-1 and 2 translations, it calls an HVC to jump back to L1.
Quoted 8 lines
Quoted 2 lines
How do you disable both the nested guest (L2)'s MMU and stage 2 translations?
Guest stage-2 is disabled by not setting HCR_EL2.VM in
prepare_hyp(), and stage-1 is disabled by not writing toSCTLR_EL12ininit_vcpu(), effectively using the default value set by L0. However sinceSCTLR_EL1has many architecturally UNKNOWN bits (including SCTLR_EL1.M), it should be better to write a value before running L2 I suppose...
Quoted 3 lines
Thanks. What do you think of using
copy_el2_to_el1()macro inat.c, so we can prepare inguest_code()to manipulate theSCTLR_EL12System register with the sensible programmed values?
Yes, using copy_el2_to_el1() can give us an L2 stage-1 that is identical to the L1's stage-1. But what I was considering was if guest stage-2 is enabled (which we plan to implement), then those stage-1 page tables will have to be mapped for L2, and its base address translated to L2IPA. It's doable but seems like extra complexity when stage-1 is not so interesting for KVM (except for AT?), it lets the guest do whatever it likes and let the hardware do the translation.
Let me know if you have reasons to want stage-1 for L2, there could be something I should consider but did not.
Thanks,
Wei-Lin Chang
Quoted 142 lines
Itaru.
Quoted 139 lines
Thanks,
Wei-Lin ChangQuoted 135 lines
Itaru.
Quoted 132 lines
Signed-off-by Wei-Lin Chang <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/hello_nested.c | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/hello_nested.ctools/testing/selftests/kvm/Makefile.kvm +1
-0@@ -168,6 +168,7 @@ TEST_GEN_PROGS_arm64 += arm64/arch_timer_edge_cases TEST_GEN_PROGS_arm64 += arm64/at TEST_GEN_PROGS_arm64 += arm64/debug-exceptions TEST_GEN_PROGS_arm64 += arm64/hello_el2 +TEST_GEN_PROGS_arm64 += arm64/hello_nested TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls TEST_GEN_PROGS_arm64 += arm64/external_abortstools/testing/selftests/kvm/arm64/hello_nested.c new file +103
-0@@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* hello_nested - Go from vEL2 to EL1 then back +*/ + +#include "nested.h" +#include "processor.h" +#include "test_util.h" +#include "ucall.h" + +#define XLATE2GPA (0xABCD) +#define L2STACKSZ (0x100) + +/* +* TPIDR_EL2 is used to store vcpu id, so save and restore it. +*/ +static vm_paddr_t ucall_translate_to_gpa (void *gva) +{ +vm_paddr_t gpa; +u64 vcpu_id =read_sysreg (tpidr_el2); + +GUEST_SYNC2 (XLATE2GPA , gva); + +/* get the result from userspace */ + gpa =read_sysreg (tpidr_el2); + +write_sysreg (vcpu_id, tpidr_el2); + +return gpa; +} + +static void l2_guest_code (void ) +{ +do_hvc (); +} + +static void guest_code (void ) +{ +struct vcpu vcpu; +struct hyp_data hyp_data; +int ret; +vm_paddr_t l2_pc, l2_stack_top; +/* force 16-byte alignment for the stack pointer */ + u8 l2_stack[L2STACKSZ] __attribute__((aligned(16))); + +GUEST_ASSERT_EQ (get_current_el (),2 ); +GUEST_PRINTF ("vEL2 entry\n" ); + + l2_pc =ucall_translate_to_gpa (l2_guest_code); + l2_stack_top =ucall_translate_to_gpa (&l2_stack[L2STACKSZ ]); + +init_vcpu (&vcpu, l2_pc, l2_stack_top); +prepare_hyp (); + + ret =run_l2 (&vcpu, &hyp_data); +GUEST_ASSERT_EQ (ret,ARM_EXCEPTION_TRAP ); +GUEST_DONE (); +} + +int main (void ) +{ +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +vm_paddr_t gpa; + +TEST_REQUIRE (kvm_check_cap (KVM_CAP_ARM_EL2 )); + 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); + +while (true) { +vcpu_run (vcpu); + +switch (get_ucall (vcpu, &uc)) { +case UCALL_SYNC : +if (uc.args[0 ] ==XLATE2GPA ) { + gpa =addr_gva2gpa (vm, (vm_vaddr_t )uc.args[1 ]); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_TPIDR_EL2 ), gpa); + } +break ; +case UCALL_PRINTF : +pr_info ("%s" , uc.buffer); +break ; +case UCALL_DONE : +pr_info ("DONE!\n" ); + goto end; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); + fallthrough; +default : +TEST_FAIL ("Unhandled ucall: %ld\n" , uc.cmd); + } + } + +end: +kvm_vm_free (vm); +return 0 ; +}Signature · 1 line
2.43.0
Re: [PATCH v2 3/4] KVM: arm64: sefltests: Add basic NV selftest
Quoted 31 lines · Wei-Lin Chang
Quoted 18 lines · Itaru Kitayama
Quoted 8 lines · Wei-Lin Chang
Hi Itaru,
Quoted 5 lines · Itaru Kitayama
Quoted 3 lines · Wei-Lin Chang
This selftest simply starts an L1, which starts its own guest (L2). L2 runs without stage-1 and 2 translations, it calls an HVC to jump back to L1.
Quoted 8 lines
Quoted 2 lines
How do you disable both the nested guest (L2)'s MMU and stage 2 translations?
Guest stage-2 is disabled by not setting HCR_EL2.VM in
prepare_hyp(), and stage-1 is disabled by not writing toSCTLR_EL12ininit_vcpu(), effectively using the default value set by L0. However sinceSCTLR_EL1has many architecturally UNKNOWN bits (including SCTLR_EL1.M), it should be better to write a value before running L2 I suppose...Quoted 3 lines
Thanks. What do you think of using
copy_el2_to_el1()macro inat.c, so we can prepare inguest_code()to manipulate theSCTLR_EL12System register with the sensible programmed values?Yes, using
copy_el2_to_el1()can give us an L2 stage-1 that is identical to the L1's stage-1. But what I was considering was if guest stage-2 is enabled (which we plan to implement), then those stage-1 page tables will have to be mapped for L2, and its base address translated to L2IPA. It's doable but seems like extra complexity when stage-1 is not so interesting for KVM (except for AT?), it lets the guest do whatever it likes and let the hardware do the translation.
Quoted 2 lines
Let me know if you have reasons to want stage-1 for L2, there could be something I should consider but did not.
By keeping nested guest's MMU enabled, we can exercise the shadow stage 2 on the host. But I am fine with you starting nested guest's IPA and I hope Marc and Oliver approve this seris and merge upstream.
Thanks,
Itaru.
Quoted 146 lines
Thanks,
Wei-Lin ChangQuoted 142 lines
Itaru.
Quoted 139 lines
Thanks,
Wei-Lin ChangQuoted 135 lines
Itaru.
Quoted 132 lines
Signed-off-by Wei-Lin Chang <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/hello_nested.c | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/hello_nested.ctools/testing/selftests/kvm/Makefile.kvm +1
-0@@ -168,6 +168,7 @@ TEST_GEN_PROGS_arm64 += arm64/arch_timer_edge_cases TEST_GEN_PROGS_arm64 += arm64/at TEST_GEN_PROGS_arm64 += arm64/debug-exceptions TEST_GEN_PROGS_arm64 += arm64/hello_el2 +TEST_GEN_PROGS_arm64 += arm64/hello_nested TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls TEST_GEN_PROGS_arm64 += arm64/external_abortstools/testing/selftests/kvm/arm64/hello_nested.c new file +103
-0@@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* hello_nested - Go from vEL2 to EL1 then back +*/ + +#include "nested.h" +#include "processor.h" +#include "test_util.h" +#include "ucall.h" + +#define XLATE2GPA (0xABCD) +#define L2STACKSZ (0x100) + +/* +* TPIDR_EL2 is used to store vcpu id, so save and restore it. +*/ +static vm_paddr_t ucall_translate_to_gpa (void *gva) +{ +vm_paddr_t gpa; +u64 vcpu_id =read_sysreg (tpidr_el2); + +GUEST_SYNC2 (XLATE2GPA , gva); + +/* get the result from userspace */ + gpa =read_sysreg (tpidr_el2); + +write_sysreg (vcpu_id, tpidr_el2); + +return gpa; +} + +static void l2_guest_code (void ) +{ +do_hvc (); +} + +static void guest_code (void ) +{ +struct vcpu vcpu; +struct hyp_data hyp_data; +int ret; +vm_paddr_t l2_pc, l2_stack_top; +/* force 16-byte alignment for the stack pointer */ + u8 l2_stack[L2STACKSZ] __attribute__((aligned(16))); + +GUEST_ASSERT_EQ (get_current_el (),2 ); +GUEST_PRINTF ("vEL2 entry\n" ); + + l2_pc =ucall_translate_to_gpa (l2_guest_code); + l2_stack_top =ucall_translate_to_gpa (&l2_stack[L2STACKSZ ]); + +init_vcpu (&vcpu, l2_pc, l2_stack_top); +prepare_hyp (); + + ret =run_l2 (&vcpu, &hyp_data); +GUEST_ASSERT_EQ (ret,ARM_EXCEPTION_TRAP ); +GUEST_DONE (); +} + +int main (void ) +{ +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +vm_paddr_t gpa; + +TEST_REQUIRE (kvm_check_cap (KVM_CAP_ARM_EL2 )); + 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); + +while (true) { +vcpu_run (vcpu); + +switch (get_ucall (vcpu, &uc)) { +case UCALL_SYNC : +if (uc.args[0 ] ==XLATE2GPA ) { + gpa =addr_gva2gpa (vm, (vm_vaddr_t )uc.args[1 ]); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_TPIDR_EL2 ), gpa); + } +break ; +case UCALL_PRINTF : +pr_info ("%s" , uc.buffer); +break ; +case UCALL_DONE : +pr_info ("DONE!\n" ); + goto end; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); + fallthrough; +default : +TEST_FAIL ("Unhandled ucall: %ld\n" , uc.cmd); + } + } + +end: +kvm_vm_free (vm); +return 0 ; +}Signature · 1 line
2.43.0
Re: [PATCH v2 3/4] KVM: arm64: sefltests: Add basic NV selftest
Quoted 32 lines · Itaru Kitayama
Quoted 31 lines · Wei-Lin Chang
Quoted 18 lines · Itaru Kitayama
Quoted 8 lines · Wei-Lin Chang
Hi Itaru,
Quoted 5 lines · Itaru Kitayama
Quoted 3 lines · Wei-Lin Chang
This selftest simply starts an L1, which starts its own guest (L2). L2 runs without stage-1 and 2 translations, it calls an HVC to jump back to L1.
Quoted 8 lines
Quoted 2 lines
How do you disable both the nested guest (L2)'s MMU and stage 2 translations?
Guest stage-2 is disabled by not setting HCR_EL2.VM in
prepare_hyp(), and stage-1 is disabled by not writing toSCTLR_EL12ininit_vcpu(), effectively using the default value set by L0. However sinceSCTLR_EL1has many architecturally UNKNOWN bits (including SCTLR_EL1.M), it should be better to write a value before running L2 I suppose...Quoted 3 lines
Thanks. What do you think of using
copy_el2_to_el1()macro inat.c, so we can prepare inguest_code()to manipulate theSCTLR_EL12System register with the sensible programmed values?Yes, using
copy_el2_to_el1()can give us an L2 stage-1 that is identical to the L1's stage-1. But what I was considering was if guest stage-2 is enabled (which we plan to implement), then those stage-1 page tables will have to be mapped for L2, and its base address translated to L2IPA. It's doable but seems like extra complexity when stage-1 is not so interesting for KVM (except for AT?), it lets the guest do whatever it likes and let the hardware do the translation.
Quoted 6 lines
Quoted 2 lines
Let me know if you have reasons to want stage-1 for L2, there could be something I should consider but did not.
By keeping nested guest's MMU enabled, we can exercise the shadow stage 2 on the host. But I am fine with you starting nested guest's IPA and I hope Marc and Oliver approve this seris and merge upstream.
I think you have guest stage-1 and guest stage-2 confused. Whether the nested guest's stage-1 MMU is enabled or not does not affect what KVM is doing with the shadow page tables. Stage-1 MMU translates L2VA -> L2IPA. Shadow page tables store the combined translation of L2IPA -> L1IPA (stage-2 PTs L1 built for L2) and L1IPA -> host PA (stage-2 PTs host built for L1).
Additionally, stage-2 not enabled for L2 does not mean shadow stage-2 is not exercised, there is still a distince shadow stage-2 for it doing the work, albeit simple (the stored mapping is the same as the canonical stage-2).
All in all, if we want to make the shadow page tables more interesting, what we should do is build a stage-2 for L2, and enable it in L1, not just turn on L2's stage-1 MMU.
Thanks,
Wei-Lin Chang
Quoted 150 lines
Thanks,
Itaru.Quoted 146 lines
Thanks,
Wei-Lin ChangQuoted 142 lines
Itaru.
Quoted 139 lines
Thanks,
Wei-Lin ChangQuoted 135 lines
Itaru.
Quoted 132 lines
Signed-off-by Wei-Lin Chang <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/hello_nested.c | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/hello_nested.ctools/testing/selftests/kvm/Makefile.kvm +1
-0@@ -168,6 +168,7 @@ TEST_GEN_PROGS_arm64 += arm64/arch_timer_edge_cases TEST_GEN_PROGS_arm64 += arm64/at TEST_GEN_PROGS_arm64 += arm64/debug-exceptions TEST_GEN_PROGS_arm64 += arm64/hello_el2 +TEST_GEN_PROGS_arm64 += arm64/hello_nested TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls TEST_GEN_PROGS_arm64 += arm64/external_abortstools/testing/selftests/kvm/arm64/hello_nested.c new file +103
-0@@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* hello_nested - Go from vEL2 to EL1 then back +*/ + +#include "nested.h" +#include "processor.h" +#include "test_util.h" +#include "ucall.h" + +#define XLATE2GPA (0xABCD) +#define L2STACKSZ (0x100) + +/* +* TPIDR_EL2 is used to store vcpu id, so save and restore it. +*/ +static vm_paddr_t ucall_translate_to_gpa (void *gva) +{ +vm_paddr_t gpa; +u64 vcpu_id =read_sysreg (tpidr_el2); + +GUEST_SYNC2 (XLATE2GPA , gva); + +/* get the result from userspace */ + gpa =read_sysreg (tpidr_el2); + +write_sysreg (vcpu_id, tpidr_el2); + +return gpa; +} + +static void l2_guest_code (void ) +{ +do_hvc (); +} + +static void guest_code (void ) +{ +struct vcpu vcpu; +struct hyp_data hyp_data; +int ret; +vm_paddr_t l2_pc, l2_stack_top; +/* force 16-byte alignment for the stack pointer */ + u8 l2_stack[L2STACKSZ] __attribute__((aligned(16))); + +GUEST_ASSERT_EQ (get_current_el (),2 ); +GUEST_PRINTF ("vEL2 entry\n" ); + + l2_pc =ucall_translate_to_gpa (l2_guest_code); + l2_stack_top =ucall_translate_to_gpa (&l2_stack[L2STACKSZ ]); + +init_vcpu (&vcpu, l2_pc, l2_stack_top); +prepare_hyp (); + + ret =run_l2 (&vcpu, &hyp_data); +GUEST_ASSERT_EQ (ret,ARM_EXCEPTION_TRAP ); +GUEST_DONE (); +} + +int main (void ) +{ +struct kvm_vcpu_init init; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +vm_paddr_t gpa; + +TEST_REQUIRE (kvm_check_cap (KVM_CAP_ARM_EL2 )); + 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); + +while (true) { +vcpu_run (vcpu); + +switch (get_ucall (vcpu, &uc)) { +case UCALL_SYNC : +if (uc.args[0 ] ==XLATE2GPA ) { + gpa =addr_gva2gpa (vm, (vm_vaddr_t )uc.args[1 ]); +vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_TPIDR_EL2 ), gpa); + } +break ; +case UCALL_PRINTF : +pr_info ("%s" , uc.buffer); +break ; +case UCALL_DONE : +pr_info ("DONE!\n" ); + goto end; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); + fallthrough; +default : +TEST_FAIL ("Unhandled ucall: %ld\n" , uc.cmd); + } + } + +end: +kvm_vm_free (vm); +return 0 ; +}Signature · 1 line
2.43.0
PATCH v2 4/4 KVM: arm64: selftests: Enhance hello_nested test
In reply to Wei-Lin ChangHandle an "add" hypercall in L1 to add 2 numbers passed by L2, and return the result. This better tests our save/restore functionality.
--- .../selftests/kvm/arm64/hello_nested.c | 31 ++++++++++++++++++- .../selftests/kvm/include/arm64/nested.h | 2 +- 2 files changed, 31 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/arm64/hello_nested.c +30 -1
@@ -11,6 +11,10 @@#define XLATE2GPA (0xABCD)#define L2STACKSZ (0x100) +#define L2SUCCESS (0x0) +#define L2FAILED (0x1) +#define L2ADD (0x2) +/* * TPIDR_EL2 is used to store vcpu id, so save and restore it. */
@@ -31,7 +35,14 @@ static vm_paddr_t ucall_translate_to_gpa(void *gva)static void l2_guest_code (void ) { -do_hvc (); +int ans =0 ; + + ans =do_hvc (L2ADD ,2 ,3 ); + +if (ans ==5 ) +do_hvc (L2SUCCESS ,0 ,0 ); +else +do_hvc (L2FAILED ,0 ,0 ); }static void guest_code (void )
@@ -42,6 +53,7 @@ static void guest_code(void)vm_paddr_t l2_pc, l2_stack_top;/* force 16-byte alignment for the stack pointer */ u8 l2_stack[L2STACKSZ] __attribute__((aligned(16))); +u64 arg1, arg2;GUEST_ASSERT_EQ (get_current_el (),2 );GUEST_PRINTF ("vEL2 entry\n" );
@@ -54,6 +66,23 @@ static void guest_code(void) ret =run_l2 (&vcpu, &hyp_data);GUEST_ASSERT_EQ (ret,ARM_EXCEPTION_TRAP ); + +if (vcpu.context.regs.regs[0 ] ==L2ADD ) { + arg1 = vcpu.context.regs.regs[1 ]; + arg2 = vcpu.context.regs.regs[2 ]; +GUEST_PRINTF ("L2 add request, arg1: %lx, arg2: %lx\n" , arg1, arg2); + vcpu.context.regs.regs[0 ] = arg1 + arg2; + }else { +GUEST_FAIL ("Unexpected hvc action\n" ); + } + + ret =run_l2 (&vcpu, &hyp_data); +GUEST_ASSERT_EQ (ret,ARM_EXCEPTION_TRAP ); + +if (vcpu.context.regs.regs[0 ] !=L2SUCCESS ) +GUEST_FAIL ("L2 failed\n" ); + +GUEST_PRINTF ("L2 success!\n" );GUEST_DONE (); }
tools/testing/selftests/kvm/include/arm64/nested.h +1 -1
@@ -50,7 +50,7 @@ void prepare_hyp(void);void init_vcpu (struct vcpu *vcpu,vm_paddr_t l2_pc,vm_paddr_t l2_stack_top);int run_l2 (struct vcpu *vcpu,struct hyp_data *hyp_data); -void do_hvc (void ); +u64 do_hvc (u64 action,u64 arg1,u64 arg2);u64 __guest_enter (struct vcpu *vcpu,struct cpu_context *hyp_context);void __hyp_exception (u64 type);
Signature · 1 line
2.43.0