KVM: arm64: Provide guest support for GCS
Subsystems (5)
v22 cover KVM: arm64: Provide guest support for GCS
The arm64 Guarded Control Stack (GCS) feature provides support for hardware protected stacks of return addresses, intended to provide hardening against return oriented programming (ROP) attacks and to make it easier to gather call stacks for applications such as profiling.
When GCS is active a secondary stack called the Guarded Control Stack is maintained, protected with a memory attribute which means that it can only be written with specific GCS operations. The current GCS pointer can not be directly written to by userspace. When a BL is executed the value stored in LR is also pushed onto the GCS, and when a RET is executed the top of the GCS is popped and compared to LR with a fault being raised if the values do not match. GCS operations may only be performed on GCS pages, a data abort is generated if they are not.
The combination of hardware enforcement and lack of extra instructions in the function entry and exit paths should result in something which has less overhead and is more difficult to attack than a purely software implementation like clang's shadow stacks.
This series implements support for managing GCS for KVM guests.
---
Changes in v22:
- Rebase onto v7.4-rc5, there are dependencies on several fixes merged
in this -rc.
- Drop handling of EXLOCK for SPSR_EL1 and ELR_EL1, this could only be reached with guest HCR_EL2.NV1 set but as of 0f1bdabb82be ("KVM:
arm64: Force HCR_EL2.NV1 to RES0 unconditionally") that can never happen. Instead drop the unreachable accessors.
- Add GCS specific handling to the stage 1 walker.
- Handle ISS2.GCS for memory faults.
- Don't be quite so enthusiastic about issuing GCSB DSYNC.
- Ensure we don't inherit a bad EXLOCK configuration from other guests.
- Further simplify the EXLOCK emulation code to a minimal set.
- Cover more cases in the EXLOCK test case, using HVC to force handling through the hypervisor and covering more host injected cases.
- Run the guest before injecting exceptions in gcs_exceptions.c.
- Refresh the esr.h sync.
- Clarifications and corrections to commit logs.
- Link to v21: https://patch.msgid.link/[email protected]
Changes in v21:
- Drop "KVM: arm64: Fix FGT mapping for HFGITR_EL2.nGCSEPP" which was applied as 089e4f3c4862.
- Reject creation of guests with GCS without S1PIE, or S1PIE/S1POE
without TCR2. Add selftest coverage for this.
- Flip order of GCSPR_EL2 and GCSCR_EL2 in el2_reg in get-reg-list.
- Remove ineffective selftest cases with HFGITR_EL2.ERET set.
- Formatting and clarity tweaks.
- Link to v20: https://patch.msgid.link/[email protected]
Changes in v20:
- Rebase onto v7.3-rc1.
- Relax GCSB DSYNCs, remove them when loading the vCPU and move them to vcpu_put() on the outgoing path.
- Make kvm_check_illegal_exlock_returns() mode change checks more
similar to compute_exlock().
- Use kvm_has_gcs() in ctxt_has_gcs().
- Move some EL2 register saves under S1PIE checks.
- Link to v19: https://patch.msgid.link/[email protected]
Changes in v19:
- Fix accessor for GCSCR_EL2 in emulation.
- Correct locate_direct_register() for EL0 registers.
- Don't check for GCS without S1PIE when restoring vEL2.
- Move barriers to align better with nearby dsbs.
- Link to v18: https://patch.msgid.link/[email protected]
Changes in v18:
- Reinject all GCS exceptions from nested guests.
- Include GCSCRE0_EL1 and GCSPR_EL0 in the live CPU register accessors.
- Sort el2_regs in get-reg-list.
- Link to v17: https://patch.msgid.link/[email protected]
Changes in v17:
- Rebase onto v7.2-rc4.
- Fix mapping of HFGITR_EL2.nGCSEPP.
- Implement a test for NV emulation around EXLOCK.
- Fix NV exception emulation issues.
- Add GCS[PC]R_EL1 to locate_direct_register().
- Add barriers.
- Add ID_AA64PFR1_EL1.GCS to set_id_regs.
- Comment updates.
- Link to v16: https://lore.kernel.org/r/[email protected]
Changes in v16:
- Rebase onto v6.17-rc3.
- Also expose the feature to nested guests.
- Implement emulation of EXLOCK when returning from nested guests.
- Rename enter_exception_gcs() to compute_exlock().
- Move all ID_AA64PFR1_EL1 handling to the final kernel patch.
- Drop unneeded forwarding of GCS exceptions.
- Commit and cover message updates.
- Link to v15: https://lore.kernel.org/r/[email protected]
Changes in v15:
- Rebase onto v6.17-rc1.
- Link to v14: https://lore.kernel.org/r/[email protected]
Changes in v14:
- Rebase onto arm64/for-next/gcs which includes all the non-KVM support.
- Manage the fine grained traps for GCS instructions.
- Manage PSTATE.EXLOCK when delivering exceptions to KVM guests.
- Link to v13: https://lore.kernel.org/r/[email protected]
Changes in v13:
- Rebase onto v6.12-rc1.
- Allocate VM_HIGH_ARCH_6 since protection keys used all the existing
bits.
- Implement mm_release() and free transparently allocated GCSs there.
- Use bit 32 of AT_HWCAP for GCS due to AT_HWCAP2 being filled.
- Since we now only set GCSCRE0_EL1 on change ensure that it is
initialised with GCSPR_EL0 accessible to EL0.
- Fix OOM handling on thread copy.
- Link to v12: https://lore.kernel.org/r/[email protected]
Changes in v12:
- Clarify and simplify the signal handling code so we work with the register state.
- When checking for write aborts to shadow stack pages ensure the fault
is a data abort.
- Depend on !UPROBES.
- Comment cleanups.
- Link to v11: https://lore.kernel.org/r/[email protected]
Changes in v11:
- Remove the dependency on the addition of clone3() support for shadow stacks, rebasing onto v6.11-rc3.
- Make ID_AA64PFR1_EL1.GCS writeable in KVM.
- Hide GCS registers when GCS is not enabled for KVM guests.
- Require HCRX_EL2.GCSEn if booting at EL1.
- Require that GCSCR_EL1 and GCSCRE0_EL1 be initialised regardless of
if we boot at EL2 or EL1.
- Remove some stray use of bit 63 in signal cap tokens.
- Warn if we see a GCS with VM_SHARED.
- Remove rdundant check for VM_WRITE in fault handling.
- Cleanups and clarifications in the ABI document.
- Clean up and improve documentation of some sync placement.
- Only set the EL0 GCS mode if it's actually changed.
- Various minor fixes and tweaks.
- Link to v10: https://lore.kernel.org/r/[email protected]
Changes in v10:
- Fix issues with THP.
- Tighten up requirements for initialising GCSCR*.
- Only generate GCS signal frames for threads using GCS.
- Only context switch EL1 GCS registers if S1PIE is enabled.
- Move context switch of GCSCRE0_EL1 to EL0 context switch.
- Make GCS registers unconditionally visible to userspace.
- Use FHU infrastructure.
- Don't change writability of ID_AA64PFR1_EL1 for KVM.
- Remove unused arguments from alloc_gcs().
- Typo fixes.
- Link to v9: https://lore.kernel.org/r/[email protected]
Changes in v9:
- Rebase onto v6.10-rc3.
- Restructure and clarify memory management fault handling.
- Fix up basic-gcs for the latest clone3() changes.
- Convert to newly merged KVM ID register based feature configuration.
- Fixes for NV traps.
- Link to v8: https://lore.kernel.org/r/[email protected]
Changes in v8:
- Invalidate signal cap token on stack when consuming.
- Typo and other trivial fixes.
- Don't try to use process_vm_write() on GCS, it intentionally does not work.
- Fix leak of thread GCSs.
- Rebase onto latest clone3() series.
- Link to v7: https://lore.kernel.org/r/[email protected]
Changes in v7:
- Rebase onto v6.7-rc2 via the clone3() patch series.
- Change the token used to cap the stack during signal handling to be compatible with GCSPOPM.
- Fix flags for new page types.
- Fold in support for clone3().
- Replace copy_to_user_gcs() with put_user_gcs().
- Link to v6: https://lore.kernel.org/r/[email protected]
Changes in v6:
- Rebase onto v6.6-rc3.
- Add some more gcsb_dsync() barriers following spec clarifications.
- Due to ongoing discussion around clone()/clone3() I've not updated anything there, the behaviour is the same as on previous versions.
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- Don't map any permissions for user GCSs, we always use EL0 accessors
or use a separate mapping of the page.
- Reduce the standard size of the GCS to RLIMIT_STACK/2.
- Enforce a PAGE_SIZE alignment requirement on map_shadow_stack().
- Clarifications and fixes to documentation.
- More tests.
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Implement flags for map_shadow_stack() allowing the cap and end of stack marker to be enabled independently or not at all.
- Relax size and alignment requirements for map_shadow_stack().
- Add more blurb explaining the advantages of hardware enforcement.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Rebase onto v6.5-rc4.
- Add a GCS barrier on context switch.
- Add a GCS stress test.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Rebase onto v6.5-rc3.
- Rework prctl() interface to allow each bit to be locked independently.
- map_shadow_stack() now places the cap token based on the size
requested by the caller not the actual space allocated.
- Mode changes other than enable via ptrace are now supported.
- Expand test coverage.
- Various smaller fixes and adjustments.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Mark Brown (16):
arm64/gcs: Ensure FGTs for EL1 GCS instructions are disabled
KVM: arm64: Drop the SPSR_EL1 and ELR_EL1 trap accessors
KVM: arm64: Manage GCS access and registers for guests
KVM: arm64: Ensure GCS memory effects are visible
KVM: arm64: Set PSTATE.EXLOCK when entering an exception
KVM: arm64: Validate GCS exception lock when emulating ERET
KVM: arm64: Forward GCS exceptions to nested guests
KVM: arm64: Preserve ISS2.GCS when injecting a Data Abort
KVM: arm64: Fully implement GCS in the stage-1 walker
KVM: arm64: Allow GCS to be enabled for guests
KVM: selftests: arm64: Check invalid feature combination handling
KVM: selftests: arm64: Add GCS registers to get-reg-list
KVM: selftests: arm64: Add GCS to set_id_regs
KVM: selftests: arm64: Only restore SPSR_EL1 and ELR_EL1 if they change
tools: Synchronise the kernel esr.h
KVM: selftests: arm64: Add GCS EXLOCK exception emulation test
arch/arm64/include/asm/el2_setup.h | 5 + arch/arm64/include/asm/kvm_emulate.h | 13 + arch/arm64/include/asm/kvm_host.h | 14 + arch/arm64/include/asm/kvm_nested.h | 32 ++ arch/arm64/include/asm/vncr_mapping.h | 2 + arch/arm64/include/uapi/asm/ptrace.h | 1 + arch/arm64/kvm/arm.c | 18 + arch/arm64/kvm/at.c | 4 +- arch/arm64/kvm/config.c | 14 + arch/arm64/kvm/emulate-nested.c | 11 +- arch/arm64/kvm/handle_exit.c | 18 +- arch/arm64/kvm/hyp/exception.c | 40 +- arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 39 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 + arch/arm64/kvm/hyp/vhe/switch.c | 4 + arch/arm64/kvm/hyp/vhe/sysreg-sr.c | 20 + arch/arm64/kvm/inject_fault.c | 6 +- arch/arm64/kvm/nested.c | 6 +- arch/arm64/kvm/sys_regs.c | 72 +-- tools/arch/arm64/include/asm/esr.h | 146 +++++- tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/arm64/gcs_exceptions.c | 500 +++++++++++++++++++++ tools/testing/selftests/kvm/arm64/get-reg-list.c | 12 + tools/testing/selftests/kvm/arm64/set_id_regs.c | 105 +++++ tools/testing/selftests/kvm/lib/arm64/handlers.S | 10 + 25 files changed, 1048 insertions(+), 52 deletions(-)
---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git fs-current
change-id: 20230303-arm64-gcs-e311ab0d8729
Best regards,
Signature · 1 line
Mark Brown <[email protected]>
PATCH v22 1/16 arm64/gcs: Ensure FGTs for EL1 GCS instructions are disabled
The initial EL2 setup for GCS did not include disabling of EL1 usage of GCS instructions, also disable these traps.
--- arch/arm64/include/asm/el2_setup.h | 5 +++++ 1 file changed, 5 insertions(+)
arch/arm64/include/asm/el2_setup.h +5 -0
@@ -393,6 +393,11 @@ orr x0, x0, #HFGRTR_EL2_nGCS_EL1_MASK orr x0, x0, #HFGRTR_EL2_nGCS_EL0_MASK +/* Disable traps of GCS instructions at EL1 */ + orr x2, x2, #HFGITR_EL2_nGCSEPP_MASK + orr x2, x2, #HFGITR_EL2_nGCSSTR_EL1_MASK + orr x2, x2, #HFGITR_EL2_nGCSPUSHM_EL1_MASK + .Lskip_gce_fgt_\@: .Lset_fgt_\@:
Signature · 1 line
2.47.3
PATCH v22 2/16 KVM: arm64: Drop the SPSR_EL1 and ELR_EL1 trap accessors
In reply to Mark BrownAn EL1 access to SPSR_EL1 or ELR_EL1 traps to EL2 only when the effective HCR_EL2.{NV2,NV1,NV} is 011. The hypervisor itself never runs a guest with this mode, and since commit 0f1bdabb82be ("KVM: arm64:
Force HCR_EL2.NV1 to RES0 unconditionally") guests cannot configure it either so we do not need to emulate it.
Remove the unreachable accessors and their sysreg descriptors, they are redundant and if this were not the case would need to be extended to enforce EXLOCK once support for GCS guests is added. Userspace accesses both registers through the core register interface so is unaffected.
--- arch/arm64/kvm/sys_regs.c | 27 --------------------------- 1 file changed, 27 deletions(-)
arch/arm64/kvm/sys_regs.c +0 -27
@@ -2852,30 +2852,6 @@ static bool access_sp_el1(struct kvm_vcpu *vcpu,return true; } -static bool access_elr (struct kvm_vcpu *vcpu, -struct sys_reg_params *p, -const struct sys_reg_desc *r) -{ -if (p->is_write) -vcpu_write_sys_reg (vcpu, p->regval,ELR_EL1 ); -else - p->regval =vcpu_read_sys_reg (vcpu,ELR_EL1 ); - -return true; -} - -static bool access_spsr (struct kvm_vcpu *vcpu, -struct sys_reg_params *p, -const struct sys_reg_desc *r) -{ -if (p->is_write) -__vcpu_assign_sys_reg (vcpu,SPSR_EL1 , p->regval); -else - p->regval =__vcpu_sys_reg (vcpu,SPSR_EL1 ); - -return true; -} -static bool access_cntkctl_el12 (struct kvm_vcpu *vcpu,struct sys_reg_params *p,const struct sys_reg_desc *r)
@@ -3510,9 +3486,6 @@ static const struct sys_reg_desc sys_reg_descs[] = {PTRAUTH_KEY (APDB ),PTRAUTH_KEY (APGA ), - { SYS_DESC(SYS_SPSR_EL1), access_spsr}, - { SYS_DESC(SYS_ELR_EL1), access_elr}, - { SYS_DESC(SYS_ICC_PMR_EL1), undef_access }, { SYS_DESC(SYS_AFSR0_EL1), access_vm_reg, reset_unknown, AFSR0_EL1 },
Signature · 1 line
2.47.3
PATCH v22 3/16 KVM: arm64: Manage GCS access and registers for guests
In reply to Mark BrownGCS introduces a number of system registers, on systems with GCS we need to context switch them and expose them to VMMs to allow guests to use GCS, including forwarding of traps for the EL2 registers with nested guests.
Since the effects of GCSCR_ELx.EXLOCKEN are not controlled by HCRX_EL2.GCSEn we always load GCSCR_EL1 for guests, even if they are not using GCS.
In order to allow guests to use GCS we also need to configure HCRX_EL2.GCSEn, if this is not set GCS instructions will be noops and CHKFEAT will report GCS as disabled.
--- arch/arm64/include/asm/kvm_emulate.h | 3 +++ arch/arm64/include/asm/kvm_host.h | 14 ++++++++++ arch/arm64/include/asm/vncr_mapping.h | 2 ++ arch/arm64/kvm/emulate-nested.c | 2 ++ arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 39 +++++++++++++++++++++++++++ arch/arm64/kvm/hyp/vhe/sysreg-sr.c | 20 ++++++++++++++ arch/arm64/kvm/sys_regs.c | 42 ++++++++++++++++++++++++++++++ 7 files changed, 122 insertions(+)
arch/arm64/include/asm/kvm_emulate.h +3 -0
@@ -743,6 +743,9 @@ static inline void vcpu_set_hcrx(struct kvm_vcpu *vcpu)if (kvm_has_feat (kvm,ID_AA64ISAR1_EL1 ,LS64 ,LS64_ACCDATA )) vcpu->arch.hcrx_el2 |= HCRX_EL2_EnAS0; +if (kvm_has_gcs (kvm)) + vcpu->arch.hcrx_el2 |= HCRX_EL2_GCSEn; +/* * NV3 is a host-specific extension, and we always use * it when present and that the guest uses NV. It may
arch/arm64/include/asm/kvm_host.h +14 -0
@@ -587,6 +587,10 @@ enum vcpu_sysreg {POR_EL0 ,/* Permission Overlay Register 0 (EL0) */ +/* Guarded Control Stack registers */ +GCSCRE0_EL1 ,/* Guarded Control Stack Control (EL0) */ +GCSPR_EL0 ,/* Guarded Control Stack Pointer (EL0) */ +/* FP/SIMD/SVE */ SVCR , FPMR,
@@ -606,6 +610,8 @@ enum vcpu_sysreg {PIRE0_EL2 ,/* Permission Indirection Register 0 (EL2) */ PIR_EL2 ,/* Permission Indirection Register 1 (EL2) */ POR_EL2 ,/* Permission Overlay Register 2 (EL2) */ +GCSCR_EL2 ,/* Guarded Control Stack Control Register (EL2) */ +GCSPR_EL2 ,/* Guarded Control Stack Pointer Register (EL2) */ SPSR_EL2 ,/* EL2 saved program status register */ ELR_EL2 ,/* EL2 exception link register */ AFSR0_EL2, /* Auxiliary Fault Status Register 0 (EL2) */
@@ -679,6 +685,10 @@ enum vcpu_sysreg {VNCR (VDISR_EL2 ),VNCR (VSESR_EL2 ), +/* Guarded Control Stack registers */ +VNCR (GCSPR_EL1 ),/* Guarded Control Stack Pointer (EL1) */ +VNCR (GCSCR_EL1 ),/* Guarded Control Stack Control (EL1) */ +VNCR (HFGRTR_EL2 ),VNCR (HFGWTR_EL2 ), VNCR(HFGITR_EL2),
@@ -1745,6 +1755,10 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);#define kvm_has_sctlr2 (k) \ (kvm_has_feat((k), ID_AA64MMFR3_EL1, SCTLRX, IMP)) +#define kvm_has_gcs (k) \ + (system_supports_gcs() && \ + kvm_has_feat((k), ID_AA64PFR1_EL1, GCS, IMP)) +static inline bool kvm_arch_has_irq_bypass (void ) {return true;
arch/arm64/include/asm/vncr_mapping.h +2 -0
@@ -97,6 +97,8 @@#define VNCR_PMSNEVFR_EL1 0x850#define VNCR_PMSDSFR_EL1 0x858#define VNCR_TRFCR_EL1 0x880 +#define VNCR_GCSPR_EL1 0x8C0 +#define VNCR_GCSCR_EL1 0x8D0#define VNCR_MPAM1_EL1 0x900#define VNCR_MPAMHCR_EL2 0x930#define VNCR_MPAMVPMV_EL2 0x938
arch/arm64/kvm/emulate-nested.c +2 -0
@@ -873,6 +873,8 @@ static const struct encoding_to_trap_config encoding_to_cgt[] __initconst = {SR_TRAP (SYS_VTTBR_EL2 ,CGT_HCR_NV ),SR_TRAP (SYS_VTCR_EL2 ,CGT_HCR_NV ),SR_TRAP (SYS_VNCR_EL2 ,CGT_HCR_NV ), +SR_TRAP (SYS_GCSCR_EL2 ,CGT_HCR_NV ), +SR_TRAP (SYS_GCSPR_EL2 ,CGT_HCR_NV ),SR_RANGE_TRAP (SYS_HDFGRTR_EL2 , SYS_HAFGRTR_EL2, CGT_HCR_NV),/* Skip the SP_EL1 encoding... */
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +39 -0
@@ -18,6 +18,7 @@static inline bool ctxt_has_s1poe (struct kvm_cpu_context *ctxt);static inline bool ctxt_has_accdata (struct kvm_cpu_context *ctxt); +static inline bool ctxt_has_gcs (struct kvm_cpu_context *ctxt);static inline struct kvm_vcpu *ctxt_to_vcpu (struct kvm_cpu_context *ctxt) {
@@ -75,6 +76,11 @@ static inline void __sysreg_save_user_state(struct kvm_cpu_context *ctxt)*/ if (ctxt_has_accdata (ctxt))ctxt_sys_reg (ctxt,ACCDATA_EL1 ) =read_sysreg_s (SYS_ACCDATA_EL1 ); + +if (ctxt_has_gcs (ctxt)) { +ctxt_sys_reg (ctxt,GCSPR_EL0 ) =read_sysreg_s (SYS_GCSPR_EL0 ); +ctxt_sys_reg (ctxt,GCSCRE0_EL1 ) =read_sysreg_s (SYS_GCSCRE0_EL1 ); + } }static inline bool ctxt_has_mte (struct kvm_cpu_context *ctxt)
@@ -151,6 +157,13 @@ static inline bool ctxt_has_accdata(struct kvm_cpu_context *ctxt)return kvm_has_feat (kern_hyp_va (vcpu->kvm),ID_AA64ISAR1_EL1 ,LS64 ,LS64_ACCDATA ); } +static inline bool ctxt_has_gcs (struct kvm_cpu_context *ctxt) +{ +struct kvm_vcpu *vcpu =ctxt_to_vcpu (ctxt); + +return kvm_has_gcs (kern_hyp_va (vcpu->kvm)); +} +static inline void __sysreg_save_el1_state (struct kvm_cpu_context *ctxt) {ctxt_sys_reg (ctxt,SCTLR_EL1 ) =read_sysreg_el1 (SYS_SCTLR );
@@ -164,6 +177,10 @@ static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt)if (ctxt_has_s1pie (ctxt)) {ctxt_sys_reg (ctxt,PIR_EL1 ) =read_sysreg_el1 (SYS_PIR );ctxt_sys_reg (ctxt,PIRE0_EL1 ) =read_sysreg_el1 (SYS_PIRE0 ); +if (ctxt_has_gcs (ctxt)) { +ctxt_sys_reg (ctxt,GCSPR_EL1 ) =read_sysreg_el1 (SYS_GCSPR ); +ctxt_sys_reg (ctxt,GCSCR_EL1 ) =read_sysreg_el1 (SYS_GCSCR ); + } }if (ctxt_has_s1poe (ctxt))
@@ -233,6 +250,12 @@ static inline void __sysreg_restore_user_state(struct kvm_cpu_context *ctxt)if (ctxt_has_accdata (ctxt))write_sysreg_s (ctxt_sys_reg (ctxt,ACCDATA_EL1 ),SYS_ACCDATA_EL1 ); + +if (ctxt_has_gcs (ctxt)) { +write_sysreg_s (ctxt_sys_reg (ctxt,GCSPR_EL0 ),SYS_GCSPR_EL0 ); +write_sysreg_s (ctxt_sys_reg (ctxt,GCSCRE0_EL1 ), +SYS_GCSCRE0_EL1 ); + } } static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt,
@@ -266,11 +289,27 @@ static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt,if (ctxt_has_s1pie (ctxt)) {write_sysreg_el1 (ctxt_sys_reg (ctxt,PIR_EL1 ),SYS_PIR );write_sysreg_el1 (ctxt_sys_reg (ctxt,PIRE0_EL1 ),SYS_PIRE0 ); + +if (ctxt_has_gcs (ctxt)) +write_sysreg_el1 (ctxt_sys_reg (ctxt,GCSPR_EL1 ),SYS_GCSPR ); }if (ctxt_has_s1poe (ctxt))write_sysreg_el1 (ctxt_sys_reg (ctxt,POR_EL1 ),SYS_POR ); } + +/* +* EXLOCK is not suppressed by HCRX_EL2.GCSEn, ensure we don't +* inherit a bad value from another guest with GCS. +*/ +if (system_supports_gcs ()) { +u64 val =0 ; + +if (ctxt_has_gcs (ctxt)) + val =ctxt_sys_reg (ctxt,GCSCR_EL1 ); +write_sysreg_el1 (val,SYS_GCSCR ); + } +write_sysreg_el1 (ctxt_sys_reg (ctxt,ESR_EL1 ),SYS_ESR );write_sysreg_el1 (ctxt_sys_reg (ctxt,AFSR0_EL1 ),SYS_AFSR0 );write_sysreg_el1 (ctxt_sys_reg (ctxt,AFSR1_EL1 ),SYS_AFSR1 );
arch/arm64/kvm/hyp/vhe/sysreg-sr.c +20 -0
@@ -59,6 +59,9 @@ static void __sysreg_save_vel2_state(struct kvm_vcpu *vcpu)if (ctxt_has_s1pie (&vcpu->arch.ctxt)) {__vcpu_assign_sys_reg (vcpu,PIRE0_EL2 ,read_sysreg_el1 (SYS_PIRE0 ));__vcpu_assign_sys_reg (vcpu,PIR_EL2 ,read_sysreg_el1 (SYS_PIR )); + +if (ctxt_has_gcs (&vcpu->arch.ctxt)) +__vcpu_assign_sys_reg (vcpu,GCSCR_EL2 ,read_sysreg_el1 (SYS_GCSCR )); }if (ctxt_has_s1poe (&vcpu->arch.ctxt))
@@ -86,6 +89,8 @@ static void __sysreg_save_vel2_state(struct kvm_vcpu *vcpu)__vcpu_assign_sys_reg (vcpu,SP_EL2 ,read_sysreg (sp_el1));__vcpu_assign_sys_reg (vcpu,ELR_EL2 ,read_sysreg_el1 (SYS_ELR ));__vcpu_assign_sys_reg (vcpu,SPSR_EL2 ,read_sysreg_el1 (SYS_SPSR )); +if (ctxt_has_gcs (&vcpu->arch.ctxt)) +__vcpu_assign_sys_reg (vcpu,GCSPR_EL2 ,read_sysreg_el1 (SYS_GCSPR ));if (ctxt_has_sctlr2 (&vcpu->arch.ctxt))__vcpu_assign_sys_reg (vcpu,SCTLR2_EL2 ,read_sysreg_el1 (SYS_SCTLR2 ));
@@ -138,12 +143,27 @@ static void __sysreg_restore_vel2_state(struct kvm_vcpu *vcpu)if (ctxt_has_s1pie (&vcpu->arch.ctxt)) {write_sysreg_el1 (__vcpu_sys_reg (vcpu,PIR_EL2 ),SYS_PIR );write_sysreg_el1 (__vcpu_sys_reg (vcpu,PIRE0_EL2 ),SYS_PIRE0 ); + +if (ctxt_has_gcs (&vcpu->arch.ctxt)) +write_sysreg_el1 (__vcpu_sys_reg (vcpu,GCSPR_EL2 ),SYS_GCSPR ); }if (ctxt_has_s1poe (&vcpu->arch.ctxt))write_sysreg_el1 (__vcpu_sys_reg (vcpu,POR_EL2 ),SYS_POR ); } +/* +* EXLOCK is not suppressed by HCRX_EL2.GCSEn, ensure we don't +* inherit a bad value from another guest with GCS. +*/ +if (system_supports_gcs ()) { +u64 val =0 ; + +if (ctxt_has_gcs (&vcpu->arch.ctxt)) + val =__vcpu_sys_reg (vcpu,GCSCR_EL2 ); +write_sysreg_el1 (val,SYS_GCSCR ); + } +write_sysreg_el1 (__vcpu_sys_reg (vcpu,ESR_EL2 ),SYS_ESR );write_sysreg_el1 (__vcpu_sys_reg (vcpu,AFSR0_EL2 ),SYS_AFSR0 );write_sysreg_el1 (__vcpu_sys_reg (vcpu,AFSR1_EL2 ),SYS_AFSR1 );
arch/arm64/kvm/sys_regs.c +42 -0
96 lines not on this page · raw message
Signature · 1 line
2.47.3
Re: [PATCH v22 03/16] KVM: arm64: Manage GCS access and registers for guests
Quoted 9 lines · Mark Brown
GCS introduces a number of system registers, on systems with GCS we need to context switch them and expose them to VMMs to allow guests to use GCS, including forwarding of traps for the EL2 registers with nested guests.
Since the effects of GCSCR_ELx.EXLOCKEN are not controlled by HCRX_EL2.GCSEn we always load
GCSCR_EL1for guests, even if they are not using GCS.
Quoted 5 lines
In order to allow guests to use GCS we also need to configure HCRX_EL2.GCSEn, if this is not set GCS instructions will be noops and CHKFEAT will report GCS as disabled.
Signed-off-by Mark Brown <[email protected]>
As per [1], feel free to add my R-b.
Thx,
Leo
Re: [PATCH v22 03/16] KVM: arm64: Manage GCS access and registers for guests
Quoted 8 lines · Leonardo Bras
Quoted 4 lines · Mark Brown
GCS introduces a number of system registers, on systems with GCS we need to context switch them and expose them to VMMs to allow guests to use GCS, including forwarding of traps for the EL2 registers with nested guests.
As per [1], feel free to add my R-b.
Thanks, I felt like I'd changed the patch enough to want confirmation that you were still OK with it.
PATCH v22 4/16 KVM: arm64: Ensure GCS memory effects are visible
In reply to Mark BrownIn order to allow efficient implementation of GCS implementations are permitted to do GCS specific caching, with barriers implicit in stack switch operations and GCSB DSYNC as an explicit barrier. Since we do not use GCS in the hypervisor or host kernel the hypervisor cannot rely on any barriers being present in vCPU context switches other than those it explicitly inserts. Add explicit barriers when saving vCPU state, unless we're doing it due to a nested EL transition in which case there is no need since the guest will not change.
--- arch/arm64/kvm/arm.c | 18 ++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 +++++++ 2 files changed, 25 insertions(+)
arch/arm64/kvm/arm.c +18 -0
@@ -826,6 +826,16 @@ static void vhe_vcpu_put(struct kvm_vcpu *vcpu)if (vcpu_has_nv (vcpu))kvm_vcpu_put_hw_mmu (vcpu); +/* +* Ensure any GCS memory effects from the outgoing vCPU are +* visible elsewhere, nested guests transitioning ELs do not +* need this. +*/ +if (kvm_has_gcs (vcpu->kvm) && + !vcpu_get_flag (vcpu,IN_NESTED_ERET ) && + !vcpu_get_flag (vcpu,IN_NESTED_EXCEPTION )) +gcsb_dsync (); +vcpu_put_mmu (vcpu); }
@@ -836,6 +846,14 @@ static void nvhe_vcpu_put(struct kvm_vcpu *vcpu)kvm_timer_vcpu_put (vcpu);kvm_vgic_put (vcpu);kvm_vcpu_pmu_restore_host (vcpu); + +/* +* Ensure any GCS memory effects from the outgoing vCPU are +* visible elsewhere. +*/ +if (kvm_has_gcs (vcpu->kvm)) +gcsb_dsync (); +vcpu_put_mmu (vcpu); }
arch/arm64/kvm/hyp/nvhe/hyp-main.c +7 -0
@@ -871,6 +871,13 @@ DEFINE_KVM_HOST_HCALL0(void, __pkvm_vcpu_put)sync_hyp_vcpu_state (hyp_vcpu); } +/* +* Ensure any GCS memory effects from the outgoing vCPU are +* visible elsewhere even if the host skips syncing. +*/ +if (kvm_has_gcs (hyp_vcpu->vcpu.kvm)) +gcsb_dsync (); +pkvm_put_hyp_vcpu (hyp_vcpu); } }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v22 04/16] KVM: arm64: Ensure GCS memory effects are visible
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: arm64: missing gcsb_dsync() on vCPU load paths across migration --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: arm64: missing gcsb_dsync() on vCPU load paths across migration
--
commit fd91d0eaded40090a370f05ec0ab5e596e118250
Author: Mark Brown <[email protected]>
KVM: arm64: Ensure GCS memory effects are visible
This commit adds gcsb_dsync() barriers when putting a vCPU in VHE, nVHE, and pKVM modes so that GCS memory effects from an outgoing vCPU are visible elsewhere.
Quoted 44 lines
arch/arm64/kvm/arm.c +18
-0@@ -826,6 +826,16 @@ static void vhe_vcpu_put(struct kvm_vcpu *vcpu)if (vcpu_has_nv (vcpu))kvm_vcpu_put_hw_mmu (vcpu); +/* +* Ensure any GCS memory effects from the outgoing vCPU are +* visible elsewhere, nested guests transitioning ELs do not +* need this. +*/ +if (kvm_has_gcs (vcpu->kvm) && + !vcpu_get_flag (vcpu,IN_NESTED_ERET ) && + !vcpu_get_flag (vcpu,IN_NESTED_EXCEPTION )) +gcsb_dsync (); +vcpu_put_mmu (vcpu); }@@ -836,6 +846,14 @@ static void nvhe_vcpu_put(struct kvm_vcpu *vcpu)kvm_timer_vcpu_put (vcpu);kvm_vgic_put (vcpu);kvm_vcpu_pmu_restore_host (vcpu); + +/* +* Ensure any GCS memory effects from the outgoing vCPU are +* visible elsewhere. +*/ +if (kvm_has_gcs (vcpu->kvm)) +gcsb_dsync (); +vcpu_put_mmu (vcpu); }arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0@@ -871,6 +871,13 @@ DEFINE_KVM_HOST_HCALL0(void, __pkvm_vcpu_put)sync_hyp_vcpu_state (hyp_vcpu); }
arch/arm64/kvm/hyp/nvhe/hyp-main.c DEFINE_KVM_HOST_HCALL0(void, __pkvm_vcpu_put) · line 883
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +7
-0+/* +* Ensure any GCS memory effects from the outgoing vCPU are +* visible elsewhere even if the host skips syncing. +*/ +if (kvm_has_gcs (hyp_vcpu->vcpu.kvm)) +gcsb_dsync (); +pkvm_put_hyp_vcpu (hyp_vcpu); } }
[Severity: Medium]
Should vhe_vcpu_load(), nvhe_vcpu_load(), and __pkvm_vcpu_load() also issue gcsb_dsync() for incoming GCS-enabled vCPUs?
As noted in gcs_thread_switch(), making GCS memory effects visible across PEs on migration requires gcsb_dsync() on both sides of the switch: on the outgoing PE to order prior GCS memory effects before release memory accesses, and on the incoming PE to order acquire memory accesses before subsequent GCS memory effects:
arch/arm64/kernel/process.c:gcs_thread_switch() {
/*
* Ensure that GCS memory effects of the 'prev' thread are
* ordered before other memory accesses with release semantics
* (or preceded by a DMB) on the current PE. In addition, any
* memory accesses with acquire semantics (or succeeded by a
* DMB) are ordered before GCS memory effects of the 'next'
* thread. This will ensure that the GCS memory effects are
* visible to other PEs in case of migration.
*/
if (task_gcs_el0_enabled(current) || task_gcs_el0_enabled(next))
gcsb_dsync();
}
Because the host VMM thread running the vCPU may not have host EL0 GCS enabled, migrating the vCPU thread to another PE will not trigger gcsb_dsync() in gcs_thread_switch() on the destination PE, leaving the migrated guest vCPU able to observe stale GCS memory.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v22 5/16 KVM: arm64: Set PSTATE.EXLOCK when entering an exception
In reply to Mark BrownAs per DDI 0487 R_WTXBY we need to manage PSTATE.EXLOCK when entering an exception, when the exception is entered from a lower EL the bit is cleared while if entering from the same EL it is set to GCSCR_ELx.EXLOCKEN. Implement this behaviour in enter_exception64().
--- arch/arm64/include/uapi/asm/ptrace.h | 1 + arch/arm64/kvm/hyp/exception.c | 40 +++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-)
arch/arm64/include/uapi/asm/ptrace.h +1 -0
@@ -56,6 +56,7 @@#define PSR_C_BIT 0x20000000#define PSR_Z_BIT 0x40000000#define PSR_N_BIT 0x80000000 +#define PSR_EXLOCK_BIT 0x400000000#define PSR_BTYPE_SHIFT 10
arch/arm64/kvm/hyp/exception.c +39 -1
@@ -49,6 +49,37 @@ static void __vcpu_write_spsr_und(struct kvm_vcpu *vcpu, u64 val) vcpu->arch.ctxt.spsr_und = val; } +static bool compute_exlock (struct kvm *kvm,struct kvm_vcpu *vcpu, +unsigned long mode,unsigned long target_mode) +{ +u64 gcscr; + +if (!(kvm &&kvm_has_gcs (kvm))) +return false; + +/* GCS can't be enabled for 32 bit */ +if (mode &PSR_MODE32_BIT ) +return false; + +/* When taking an exception to a higher EL EXLOCK is cleared. */ +if ((mode |PSR_MODE_THREAD_BIT ) < target_mode) +return false; + +/* +* When taking an exception to the same EL EXLOCK is set to +* the effective value of GCSCR_ELx.EXLOCKEN. +*/ +if (is_hyp_ctxt (vcpu)) + gcscr =vcpu_read_sys_reg (vcpu,GCSCR_EL2 ); +else + gcscr =vcpu_read_sys_reg (vcpu,GCSCR_EL1 ); + +if (gcscr & GCSCR_ELx_EXLOCKEN) +return true; + +return false; +} +/* * This performs the exception entry at a given EL (@target_mode), stashing PC * and PSTATE into ELR and SPSR respectively, and compute the new PC/PSTATE.
@@ -113,7 +144,6 @@ static void enter_exception64(struct kvm_vcpu *vcpu, unsigned long target_mode, kvm =vcpu_get_kvm (vcpu);if (kvm &&kvm_has_mte (kvm)) new |=PSR_TCO_BIT ; -vcpu_put_kvm (vcpu, kvm); new |= (old &PSR_DIT_BIT );
@@ -141,6 +171,12 @@ static void enter_exception64(struct kvm_vcpu *vcpu, unsigned long target_mode,// PSTATE.BTYPE is set to zero upon any exception to AArch64 // See ARM DDI 0487E.a, pages D1-2293 to D1-2294. +// PSTATE.EXLOCK is set to 0 upon any exception to a higher +// EL, or to GCSCR_ELx.EXLOCKEN for an exception to the same +// exception level. See ARM DDI 0487 R_WTXBY. +if (compute_exlock (kvm, vcpu, mode, target_mode)) + new |=PSR_EXLOCK_BIT ; + new |=PSR_D_BIT ; new |=PSR_A_BIT ; new |=PSR_I_BIT ;
@@ -150,6 +186,8 @@ static void enter_exception64(struct kvm_vcpu *vcpu, unsigned long target_mode, *vcpu_cpsr (vcpu) = new;__vcpu_write_spsr (vcpu, target_mode, old); + +vcpu_put_kvm (vcpu, kvm); }/*
Signature · 1 line
2.47.3
PATCH v22 6/16 KVM: arm64: Validate GCS exception lock when emulating ERET
In reply to Mark BrownAs per DDI0487 R_TYTWB GCS adds an additional case where an illegal exception return can be generated. If all of:
- PSTATE.EXLOCK is 0.
- The EL is not being changed by the ERET.
- GCSCR_ELx.EXLOCKEN is 1.
are true then the return is illegal. Emulate this behaviour when emulating ERET for nested guests, while we're at it using the symbolic definition for EXLOCK in SPSR.
--- arch/arm64/include/asm/kvm_nested.h | 32 ++++++++++++++++++++++++++++++++ arch/arm64/kvm/emulate-nested.c | 7 +++++-- arch/arm64/kvm/hyp/vhe/switch.c | 4 ++++ 3 files changed, 41 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_nested.h +32 -0
@@ -268,6 +268,38 @@ static inline bool kvm_auth_eretax(struct kvm_vcpu *vcpu, u64 *elr) }#endif +#ifdef CONFIG_ARM64_GCS +/* See IllegalExceptionReturn() pseudocode */ +static inline bool kvm_check_illegal_exlock_return (struct kvm_vcpu *vcpu, +u64 spsr) +{ +u64 pstate, cur_mode, target_mode; + +if (!kvm_has_gcs (vcpu->kvm)) +return false; + +if (vcpu->arch.ctxt.regs.pstate &PSR_EXLOCK_BIT ) +return false; + +/* Check the EL only, ignore thread mode */ + pstate = vcpu->arch.ctxt.regs.pstate; + cur_mode = (pstate &PSR_MODE_MASK ) |PSR_MODE_THREAD_BIT ; + target_mode = (spsr &PSR_MODE_MASK ) |PSR_MODE_THREAD_BIT ; + +if (cur_mode != target_mode) +return false; + +return vcpu_read_sys_reg (vcpu,GCSCR_EL2 ) & GCSCR_ELx_EXLOCKEN; +} + +#else +static inline bool kvm_check_illegal_exlock_return (struct kvm_vcpu *vcpu, +u64 spsr) +{ +return false; +} +#endif +#define KVM_NV_GUEST_MAP_SZ (KVM_PGTABLE_PROT_SW1 | KVM_PGTABLE_PROT_SW0)static inline u64 kvm_encode_nested_level (struct kvm_s2_trans *trans)
arch/arm64/kvm/emulate-nested.c +5 -2
@@ -2755,10 +2755,13 @@ static u64 kvm_check_illegal_exception_return(struct kvm_vcpu *vcpu, u64 spsr)* - trying to return to an illegal M value * - trying to return to a 32bit EL * - trying to return to EL1 with HCR_EL2.TGE set +* - GCSCR_ELx.EXLOCKEN is 1 and PSTATE.EXLOCK is 0 when attempting +* to return to the same EL. */ if (mode == PSR_MODE_EL3t || mode == PSR_MODE_EL3h || mode ==0b00001 || (mode &BIT (1 )) || (spsr &PSR_MODE32_BIT ) || +kvm_check_illegal_exlock_return (vcpu, spsr) || (vcpu_el2_tge_is_set (vcpu) && (mode == PSR_MODE_EL1t || mode == PSR_MODE_EL1h))) {u64 mask;
@@ -2784,8 +2787,8 @@ static u64 kvm_check_illegal_exception_return(struct kvm_vcpu *vcpu, u64 spsr) spsr &= mask; mask =PSR_MODE_MASK |PSR_MODE32_BIT ; -if (kvm_has_feat (vcpu->kvm,ID_AA64PFR1_EL1 ,GCS ,IMP )) - mask |=BIT_ULL (34 );/* PSTATE.EXLOCK */ +if (kvm_has_gcs (vcpu->kvm)) + mask |=PSR_EXLOCK_BIT ; spsr |= *vcpu_cpsr (vcpu) & mask; spsr |=PSR_IL_BIT ;
arch/arm64/kvm/hyp/vhe/switch.c +4 -0
@@ -395,6 +395,10 @@ static bool kvm_hyp_handle_eret(struct kvm_vcpu *vcpu, u64 *exit_code)return false; } +/* Push GCS exception lock failures into the slow path */ +if (kvm_check_illegal_exlock_return (vcpu, spsr)) +return false; +/* If ERETAx fails, take the slow path */ if (esr_iss_is_eretax (esr)) {if (!(vcpu_has_ptrauth (vcpu) &&kvm_auth_eretax (vcpu, &elr)))
Signature · 1 line
2.47.3
PATCH v22 7/16 KVM: arm64: Forward GCS exceptions to nested guests
In reply to Mark BrownFor nested guests where HFGITR_EL2.nGCSSTR_EL1 is clear or when there are L2 GCS data check exceptions we need to forward the exception to the guest. Add handling to do so.
--- arch/arm64/kvm/handle_exit.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
arch/arm64/kvm/handle_exit.c +15 -3
@@ -304,12 +304,24 @@ static int handle_svc(struct kvm_vcpu *vcpu)return 1 ; } +/* +* We might get GCS exceptions that need to be forwarded to the +* hypervisor when a nested guest has HFGITR_EL2.nGCSSTR_EL1 clear, or +* for a GCS data check exception for a L2 guest. +*/ static int kvm_handle_gcs (struct kvm_vcpu *vcpu) { -/* We don't expect GCS, so treat it with contempt */ -if (kvm_has_feat (vcpu->kvm,ID_AA64PFR1_EL1 ,GCS ,IMP )) -WARN_ON_ONCE (1 ); +if (!kvm_has_gcs (vcpu->kvm)) { +kvm_inject_undefined (vcpu); +return 1 ; + } +if (vcpu_has_nv (vcpu)) { +kvm_inject_nested_sync (vcpu,kvm_vcpu_get_esr (vcpu)); +return 1 ; + } + +WARN_ON_ONCE (1 );kvm_inject_undefined (vcpu);return 1 ; }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v22 07/16] KVM: arm64: Forward GCS exceptions to nested guests
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] KVM: arm64: question on ESR_ELx_EC_GCS trap conditions in kvm_handle_gcs() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] KVM: arm64: question on ESR_ELx_EC_GCS trap conditions in kvm_handle_gcs()
--
commit 63d9782733faf57fed4105f2f5d68c07cc781c5c
Author: Mark Brown <[email protected]>
KVM: arm64: Forward GCS exceptions to nested guests
This commit forwards ESR_ELx_EC_GCS exits to virtual EL2 via kvm_inject_nested_sync() when nested virtualization is enabled on a vCPU with FEAT_GCS.
Quoted 22 lines
arch/arm64/kvm/handle_exit.c +9
-3@@ -304,12 +304,24 @@ static int handle_svc(struct kvm_vcpu *vcpu)return 1 ; } +/* +* We might get GCS exceptions that need to be forwarded to the +* hypervisor when a nested guest has HFGITR_EL2.nGCSSTR_EL1 clear, or +* for a GCS data check exception for a L2 guest. +*/ static int kvm_handle_gcs (struct kvm_vcpu *vcpu) { -/* We don't expect GCS, so treat it with contempt */ -if (kvm_has_feat (vcpu->kvm,ID_AA64PFR1_EL1 ,GCS ,IMP )) -WARN_ON_ONCE (1 ); +if (!kvm_has_gcs (vcpu->kvm)) { +kvm_inject_undefined (vcpu); +return 1 ; + }
arch/arm64/kvm/handle_exit.c static int handle_svc(struct kvm_vcpu *vcpu) · line 327
Quoted 10 lines
arch/arm64/kvm/handle_exit.c +6
-0+if (vcpu_has_nv (vcpu)) { +kvm_inject_nested_sync (vcpu,kvm_vcpu_get_esr (vcpu)); +return 1 ; + } + +WARN_ON_ONCE (1 );kvm_inject_undefined (vcpu);return 1 ; }
[Severity: Low]
This question rests on an ARM architecture detail that could not be confirmed from the kernel source: when physical HCR_EL2.TGE is 0 and physical HCRX_EL2.GCSEn is 1, can an ESR_ELx_EC_GCS exception trap to physical EL2 only from a nested guest context (is_nested_ctxt(vcpu)) when virtual EL2 is the architectural target, or can a GCS data check exception in an L2 guest also trap to physical EL2 when virtual EL1 is the intended target?
In __compute_fgt() and __activate_traps_common(), physical HFGITR_EL2.nGCSSTR_EL1 and physical HCRX_EL2.GCSEn are both set to 1 while is_hyp_ctxt(vcpu) is true, and are only cleared in is_nested_ctxt(vcpu) when the L1 guest hypervisor clears them. If valid traps to physical EL2 can only occur in is_nested_ctxt(vcpu), gating on vcpu_has_nv(vcpu) forwards an unexpected trap in is_hyp_ctxt(vcpu) into virtual EL2 instead of hitting WARN_ON_ONCE(1) and injecting UNDEFINED; if an L2 GCS data check exception can trap to physical EL2 when L2 virtual EL1 is the target, kvm_inject_nested_sync() delivers it to L1 virtual EL2 instead of L2 virtual EL1.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v22 8/16 KVM: arm64: Preserve ISS2.GCS when injecting a Data Abort
In reply to Mark BrownA Data Abort caused by a guarded control stack access sets bit 40 in ESR_ELx. KVM loses this bit when it forwards a nested stage-2 fault using the walk result, or when it constructs a fresh syndrome for an external abort. The guest then sees an ordinary data access.
Recover ISS2.GCS from the original Data Abort when injecting the fault into a guest that exposes GCS. Take only that bit from ISS2 so that other host syndrome fields do not leak into the synthesized exception.
--- arch/arm64/include/asm/kvm_emulate.h | 10 ++++++++++ arch/arm64/kvm/emulate-nested.c | 2 ++ arch/arm64/kvm/inject_fault.c | 6 ++++-- arch/arm64/kvm/nested.c | 3 +++ 4 files changed, 19 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +10 -0
@@ -367,6 +367,16 @@ static __always_inline u64 kvm_vcpu_get_esr(const struct kvm_vcpu *vcpu)return vcpu->arch.fault.esr_el2; } +static inline u64 kvm_vcpu_dabt_iss2 (const struct kvm_vcpu *vcpu) +{ +u64 esr =kvm_vcpu_get_esr (vcpu); + +if (esr_is_data_abort (esr) &&kvm_has_gcs (vcpu->kvm)) +return esr & (ESR_ELx_GCS << ESR_ELx_ISS2_SHIFT); + +return 0 ; +} +static inline bool guest_hyp_wfx_traps_enabled (const struct kvm_vcpu *vcpu) {u64 esr =kvm_vcpu_get_esr (vcpu);
arch/arm64/kvm/emulate-nested.c +2 -0
@@ -2969,6 +2969,8 @@ int kvm_inject_nested_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr)u64 esr =FIELD_PREP (ESR_ELx_EC_MASK, iabt ? ESR_ELx_EC_IABT_LOW : ESR_ELx_EC_DABT_LOW); esr |= ESR_ELx_FSC_EXTABT | ESR_ELx_IL; +if (!iabt) + esr |=kvm_vcpu_dabt_iss2 (vcpu);vcpu_write_sys_reg (vcpu, addr,FAR_EL2 );
arch/arm64/kvm/inject_fault.c +4 -2
@@ -152,8 +152,10 @@ static void inject_abt64(struct kvm_vcpu *vcpu, bool is_iabt, unsigned long addrelse esr |= (ESR_ELx_EC_IABT_CUR << ESR_ELx_EC_SHIFT); -if (!is_iabt) +if (!is_iabt) { esr |= ESR_ELx_EC_DABT_LOW << ESR_ELx_EC_SHIFT; + esr |=kvm_vcpu_dabt_iss2 (vcpu); + } esr |= fsc;
@@ -253,7 +255,7 @@ static int kvm_inject_nested_excl_atomic(struct kvm_vcpu *vcpu, u64 addr) {u64 esr =FIELD_PREP (ESR_ELx_EC_MASK, ESR_ELx_EC_DABT_LOW) |FIELD_PREP (ESR_ELx_FSC, ESR_ELx_FSC_EXCL_ATOMIC) | - ESR_ELx_IL; + ESR_ELx_IL |kvm_vcpu_dabt_iss2 (vcpu);vcpu_write_sys_reg (vcpu, addr,FAR_EL2 );return kvm_inject_nested_sync (vcpu, esr);
arch/arm64/kvm/nested.c +3 -0
@@ -1081,6 +1081,9 @@ int kvm_s2_handle_perm_fault(struct kvm_vcpu *vcpu, struct kvm_s2_trans *trans)int kvm_inject_s2_fault (struct kvm_vcpu *vcpu,u64 esr_el2) { +/* The stage-2 walker supplies EC and ISS. Recover ISS2 from the fault. */ + esr_el2 |=kvm_vcpu_dabt_iss2 (vcpu); +vcpu_write_sys_reg (vcpu, vcpu->arch.fault.far_el2,FAR_EL2 );vcpu_write_sys_reg (vcpu, vcpu->arch.fault.hpfar_el2,HPFAR_EL2 );
Signature · 1 line
2.47.3
PATCH v22 9/16 KVM: arm64: Fully implement GCS in the stage-1 walker
In reply to Mark BrownWhen the VM exposes FEAT_GCS the GCS encoding 0b1001 grants read and GCS access. In the EL1&0 and EL2&0 translation regimes, DDI0487 R_VFPJF removes all base permissions when privileged execute or GCS access coincides with unprivileged write or GCS access. Include it on both sides of that check so software AT reports the same permission fault as hardware.
Note that (as with the current code's handling of readability for GCS pages) we don't gate anything on the guest having FEAT_GCS, this matches the hardware behaviour if a guest uses this encoding on a host with GCS. A guest that does not see FEAT_GCS should not be creating any pages with GCS permissions and the effects are only visible within the guest.
--- arch/arm64/kvm/at.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/at.c +2 -2
@@ -1128,7 +1128,7 @@ static void compute_s1_hierarchical_permissions(struct kvm_vcpu *vcpu, (wr)->uwxn = (v); \ }while (0 ) -/* Similar to AArch64.S1IndirectBasePermissions(), without GCS */ +/* Decode the read, write and execute bits of S1IndirectBasePermissions(). */#define set_perms (w, wr, ip) \ do { \/* R_LLZDZ */ \
@@ -1222,7 +1222,7 @@ static void compute_s1_indirect_permissions(struct kvm_vcpu *vcpu, wr->uov = wi->e0poe && !(up &BIT (3 ));/* R_VFPJF */ -if (wr->px && wr->uw) { +if ((wr->px || pp ==PIE_GCS ) && (wr->uw || up ==PIE_GCS )) {set_priv_perms (wr, false, false, false);set_unpriv_perms (wr, false, false, false); }
Signature · 1 line
2.47.3
PATCH v22 10/16 KVM: arm64: Allow GCS to be enabled for guests
In reply to Mark BrownNow that required functionality for GCS is in place expose ID_AA64PFR1_EL1.GCS, allowing guests to be given the feature. Enforce the architectural dependency on S1PIE.
--- arch/arm64/kvm/config.c | 14 ++++++++++++++ arch/arm64/kvm/nested.c | 3 +-- arch/arm64/kvm/sys_regs.c | 5 +++-- 3 files changed, 18 insertions(+), 4 deletions(-)
arch/arm64/kvm/config.c +14 -0
@@ -330,6 +330,12 @@ static bool feat_s1poe(struct kvm *kvm) kvm_has_feat(kvm, FEAT_HPDS)); } +static bool feat_gcs (struct kvm *kvm) +{ +return (kvm_has_feat (kvm,FEAT_GCS ) && +kvm_has_feat (kvm,FEAT_S1PIE )); +} +static bool feat_sme_smps (struct kvm *kvm) {/*
@@ -1442,6 +1448,13 @@ static const struct reg_bits_to_feat_map id_aa64mmfr3_el1_feat_map[] = { static const DECLARE_IDREG_FEAT_MAP(id_aa64mmfr3_el1_desc, ID_AA64MMFR3_EL1, id_aa64mmfr3_el1_feat_map, FEAT_AA64EL1); +static const struct reg_bits_to_feat_map id_aa64pfr1_el1_feat_map[] = { +NEEDS_FEAT (ID_AA64PFR1_EL1_GCS , feat_gcs), +}; + +static const DECLARE_IDREG_FEAT_MAP(id_aa64pfr1_el1_desc, ID_AA64PFR1_EL1, + id_aa64pfr1_el1_feat_map, FEAT_AA64EL1); +static const struct reg_bits_to_feat_map ich_hfgrtr_feat_map[] = { NEEDS_FEAT(ICH_HFGRTR_EL2_ICC_APR_EL1 | ICH_HFGRTR_EL2_ICC_IDRn_EL1 |
@@ -1555,6 +1568,7 @@ void __init check_feature_map(void)static const struct reg_feat_map_desc *idreg_feat_maps[] = { &id_aa64mmfr3_el1_desc, + &id_aa64pfr1_el1_desc, };static bool idreg_apply_deps (struct kvm *kvm,const struct reg_feat_map_desc *r)
arch/arm64/kvm/nested.c +1 -2
@@ -1857,11 +1857,10 @@ u64 limit_nv_id_reg(struct kvm *kvm, u32 reg, u64 val)break ;case SYS_ID_AA64PFR1_EL1 : -/* Only support BTI, SSBS, CSV2_frac */ +/* Only support GCS, BTI, SSBS, CSV2_frac */ val &= ~(ID_AA64PFR1_EL1_PFAR | ID_AA64PFR1_EL1_MTEX | ID_AA64PFR1_EL1_THE | - ID_AA64PFR1_EL1_GCS | ID_AA64PFR1_EL1_MTE_frac | ID_AA64PFR1_EL1_NMI | ID_AA64PFR1_EL1_SME |
arch/arm64/kvm/sys_regs.c +3 -2
@@ -2192,10 +2192,12 @@ static u64 sanitise_id_aa64pfr1_el1(const struct kvm_vcpu *vcpu, u64 val) SYS_FIELD_GET(ID_AA64PFR0_EL1, RAS, pfr0) == ID_AA64PFR0_EL1_RAS_IMP)) val &= ~ID_AA64PFR1_EL1_RAS_frac; +if (!system_supports_gcs ()) + val &= ~ID_AA64PFR1_EL1_GCS ; + val &= ~ID_AA64PFR1_EL1_SME ; val &= ~ID_AA64PFR1_EL1_RNDR_trap; val &= ~ID_AA64PFR1_EL1_NMI ; - val &= ~ID_AA64PFR1_EL1_GCS ; val &= ~ID_AA64PFR1_EL1_THE ; val &= ~ID_AA64PFR1_EL1_MTEX ; val &= ~ID_AA64PFR1_EL1_PFAR ;
@@ -3403,7 +3405,6 @@ static const struct sys_reg_desc sys_reg_descs[] = { ~(ID_AA64PFR1_EL1_PFAR | ID_AA64PFR1_EL1_MTEX | ID_AA64PFR1_EL1_THE | - ID_AA64PFR1_EL1_GCS | ID_AA64PFR1_EL1_MTE_frac | ID_AA64PFR1_EL1_NMI | ID_AA64PFR1_EL1_RNDR_trap |
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v22 10/16] KVM: arm64: Allow GCS to be enabled for guests
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] KVM: arm64: mismatched GCSPR_EL2 gating in __sysreg_save_vel2_state() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] KVM: arm64: mismatched GCSPR_EL2 gating in __sysreg_save_vel2_state()
--
commit f6054f7b916e4b0fccaad0bac682292e2674014d
Author: Mark Brown <[email protected]>
KVM: arm64: Allow GCS to be enabled for guests
This commit exposes ID_AA64PFR1_EL1.GCS to guests, including nested virtualization guests, and registers its dependency on FEAT_S1PIE.
arch/arm64/kvm/config.c static bool feat_s1poe(struct kvm *kvm) · line 337
Quoted 13 lines
arch/arm64/kvm/config.c +5
-0@@ -330,6 +330,12 @@ static bool feat_s1poe(struct kvm *kvm) kvm_has_feat(kvm, FEAT_HPDS)); } +static bool feat_gcs (struct kvm *kvm) +{ +return (kvm_has_feat (kvm,FEAT_GCS ) && +kvm_has_feat (kvm,FEAT_S1PIE )); +}
[Severity: High]
Can a userspace VMM leak another guest VM's physical SYS_GCSPR_EL1 value into vcpu->arch.ctxt.sys_regs[GCSPR_EL2] before ID register dependencies are finalized on first KVM_RUN?
feat_gcs() is only enforced by kvm_idregs_enforce_dependencies() during kvm_vm_finalize_sys_regs() on the first KVM_RUN, while ctxt_has_gcs() checks only ID_AA64PFR1_EL1.GCS. In arch/arm64/kvm/hyp/vhe/sysreg-sr.c, the save and restore paths for GCSPR_EL2 use different conditions:
arch/arm64/kvm/hyp/vhe/sysreg-sr.c:__sysreg_save_vel2_state() {
...
if (ctxt_has_gcs(&vcpu->arch.ctxt))
__vcpu_assign_sys_reg(vcpu, GCSPR_EL2, read_sysreg_el1(SYS_GCSPR));
}arch/arm64/kvm/hyp/vhe/sysreg-sr.c:__sysreg_restore_vel2_state() {
...
if (ctxt_has_tcrx(&vcpu->arch.ctxt)) {
write_sysreg_el1(__vcpu_sys_reg(vcpu, TCR2_EL2), SYS_TCR2);
if (ctxt_has_s1pie(&vcpu->arch.ctxt)) {
write_sysreg_el1(__vcpu_sys_reg(vcpu, PIR_EL2), SYS_PIR);
write_sysreg_el1(__vcpu_sys_reg(vcpu, PIRE0_EL2), SYS_PIRE0);
if (ctxt_has_gcs(&vcpu->arch.ctxt))
write_sysreg_el1(__vcpu_sys_reg(vcpu, GCSPR_EL2), SYS_GCSPR);
}
}If userspace initializes a vCPU with KVM_ARM_VCPU_HAS_EL2, clears ID_AA64MMFR3_EL1.S1PIE or ID_AA64MMFR3_EL1.TCRX via KVM_SET_ONE_REG while leaving ID_AA64PFR1_EL1.GCS enabled, and calls KVM_SET_VCPU_EVENTS before KVM_RUN, vcpu_load() skips writing SYS_GCSPR in __sysreg_restore_vel2_state() and leaves the previous guest's value in the hardware register. The paired vcpu_put() then executes __sysreg_save_vel2_state(), which saves that stale hardware SYS_GCSPR value into GCSPR_EL2 where userspace can read it via KVM_GET_ONE_REG.
Should __sysreg_save_vel2_state() also gate saving GCSPR_EL2 on ctxt_has_tcrx() and ctxt_has_s1pie() (or kvm_has_gcs() check kvm_has_s1pie()) to match __sysreg_restore_vel2_state()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
PATCH v22 11/16 KVM: selftests: arm64: Check invalid feature combination handling
In reply to Mark BrownIn order to optimise fast paths KVM explicitly suppresses configurations with S1PIE or S1POE but not TCR2, add coverage of this in the set_id_regs test.
We have a list of invalid configurations, for each of them we try to run a VM and fail the test if after running the VM any of the feature fields involved are non-zero. This will need revisiting if we start filtering more complex combinations but is fine for now.
We do feature detection by validating that we can write the fields with failing values.
--- tools/testing/selftests/kvm/arm64/set_id_regs.c | 97 +++++++++++++++++++++++++ 1 file changed, 97 insertions(+)
tools/testing/selftests/kvm/arm64/set_id_regs.c +97 -0
@@ -916,6 +916,100 @@ static void test_idreg_frozen_after_failed_run(void)ksft_test_result_pass ("%s\n" , __func__); } +struct reg_ftr_val { +const char *name; +u64 reg; +u64 mask; +u64 val; +}; + +#define REG_FTR_VAL (r, f, v) \ + { .name = #r "." #f, \ + .reg = ARM64_SYS_REG(sys_reg_Op0(SYS_ ## r), \ + sys_reg_Op1(SYS_ ## r), \ + sys_reg_CRn(SYS_ ## r), \ + sys_reg_CRm(SYS_ ## r), \ + sys_reg_Op2(SYS_ ## r)), \ + .mask = r ## _ ## f ## _MASK, \ + .val = (r ## _ ## f ## _ ## v << r ## _ ## f ## _SHIFT) } + +static const struct reg_ftr_val s1pie_no_tcr2[] = { +REG_FTR_VAL (ID_AA64MMFR3_EL1 ,TCRX ,NI ), +REG_FTR_VAL (ID_AA64MMFR3_EL1 ,S1PIE ,IMP ), + { } +}; + +static const struct reg_ftr_val s1poe_no_tcr2[] = { +REG_FTR_VAL (ID_AA64MMFR3_EL1 ,TCRX ,NI ), +REG_FTR_VAL (ID_AA64MMFR3_EL1 ,S1POE ,IMP ), + { } +}; + +struct ftr_config { +const char *name; +const struct reg_ftr_val *regs; +}; + +static const struct ftr_config invalid_configs[] = { + { .name ="S1PIE without TCRX" , .regs = s1pie_no_tcr2 }, + { .name ="S1POE without TCRX" , .regs = s1poe_no_tcr2 }, +}; + +static void test_invalid_config (const struct ftr_config *config) +{ +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +const struct reg_ftr_val *field; +u64 val; +bool pass = true; + + vm =vm_create (1 ); +vm_enable_cap (vm,KVM_CAP_ARM_WRITABLE_IMP_ID_REGS ,0 ); + vcpu =vm_vcpu_add (vm,0 , guest_code); +kvm_arch_vm_finalize_vcpus (vm); + +/* +* If we don't manage to set any of the fields assume the +* system does not support the feature and skip the test. +*/ +for (field = config->regs; field->reg; field++) { + val =vcpu_get_reg (vcpu, field->reg); + val &= ~field->mask; + val |= field->val; +__vcpu_set_reg (vcpu, field->reg, val); + +if (vcpu_get_reg (vcpu, field->reg) != val) { +ksft_print_msg ("Test setup not supported\n" ); +ksft_test_result_skip ("filter out %s\n" , config->name); + goto out; + } + } + +vcpu_run (vcpu); + +for (field = config->regs; field->reg; field++) { +/* For now all invalid values reset to 0 */ + val =vcpu_get_reg (vcpu, field->reg) & field->mask; +if (val) { +ksft_print_msg ("%s is %lx not 0\n" , field->name, val); + pass = false; + } + } + +ksft_test_result (pass,"filter out %s\n" , + config->name); +out: +kvm_vm_free (vm); +} + +static void test_invalid_configs (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (invalid_configs); i++) +test_invalid_config (&invalid_configs[i]); +} +int main (void ) {struct kvm_vcpu *vcpu;
@@ -942,6 +1036,7 @@ int main(void)ksft_print_header (); test_cnt =4 +MPAM_IDREG_TEST +MTE_IDREG_TEST ; + test_cnt +=ARRAY_SIZE (invalid_configs);for (i =0 ; i <ARRAY_SIZE (test_regs); i++)for (j =0 ; test_regs[i].ftr_bits[j].type !=FTR_END ; j++) test_cnt++;
@@ -962,5 +1057,7 @@ int main(void)test_idreg_frozen_after_failed_run (); +test_invalid_configs (); +ksft_finished (); }
Signature · 1 line
2.47.3
PATCH v22 12/16 KVM: selftests: arm64: Add GCS registers to get-reg-list
In reply to Mark BrownGCS adds new registers GCSCR_ELx, GCSCRE0_EL1 and GCSPR_ELx. Add these to those validated by get-reg-list.
--- tools/testing/selftests/kvm/arm64/get-reg-list.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tools/testing/selftests/kvm/arm64/get-reg-list.c +12 -0
@@ -42,6 +42,12 @@ struct feature_id_reg {static struct feature_id_reg feat_id_regs[] = {REG_FEAT (TCR2_EL1 ,ID_AA64MMFR3_EL1 ,TCRX ,IMP ),REG_FEAT (TCR2_EL2 ,ID_AA64MMFR3_EL1 ,TCRX ,IMP ), +REG_FEAT (GCSPR_EL0 ,ID_AA64PFR1_EL1 ,GCS ,IMP ), +REG_FEAT (GCSPR_EL1 ,ID_AA64PFR1_EL1 ,GCS ,IMP ), +REG_FEAT (GCSPR_EL2 ,ID_AA64PFR1_EL1 ,GCS ,IMP ), +REG_FEAT (GCSCRE0_EL1 ,ID_AA64PFR1_EL1 ,GCS ,IMP ), +REG_FEAT (GCSCR_EL1 ,ID_AA64PFR1_EL1 ,GCS ,IMP ), +REG_FEAT (GCSCR_EL2 ,ID_AA64PFR1_EL1 ,GCS ,IMP ),REG_FEAT (PIRE0_EL1 ,ID_AA64MMFR3_EL1 ,S1PIE ,IMP ),REG_FEAT (PIRE0_EL2 ,ID_AA64MMFR3_EL1 ,S1PIE ,IMP ),REG_FEAT (PIR_EL1 ,ID_AA64MMFR3_EL1 ,S1PIE ,IMP ),
@@ -504,6 +510,9 @@ static __u64 base_regs[] = {ARM64_SYS_REG (3 ,0 ,2 ,0 ,1 ),/* TTBR1_EL1 */ ARM64_SYS_REG (3 ,0 ,2 ,0 ,2 ),/* TCR_EL1 */ ARM64_SYS_REG (3 ,0 ,2 ,0 ,3 ),/* TCR2_EL1 */ +ARM64_SYS_REG (3 ,0 ,2 ,5 ,0 ),/* GCSCR_EL1 */ +ARM64_SYS_REG (3 ,0 ,2 ,5 ,1 ),/* GCSPR_EL1 */ +ARM64_SYS_REG (3 ,0 ,2 ,5 ,2 ),/* GCSCRE0_EL1 */ ARM64_SYS_REG (3 ,0 ,5 ,1 ,0 ),/* AFSR0_EL1 */ ARM64_SYS_REG (3 ,0 ,5 ,1 ,1 ),/* AFSR1_EL1 */ ARM64_SYS_REG(3, 0, 5, 2, 0), /* ESR_EL1 */
@@ -521,6 +530,7 @@ static __u64 base_regs[] = {ARM64_SYS_REG (3 ,0 ,13 ,0 ,5 ),/* ACCDATA_EL1 */ ARM64_SYS_REG (3 ,0 ,14 ,1 ,0 ),/* CNTKCTL_EL1 */ ARM64_SYS_REG (3 ,2 ,0 ,0 ,0 ),/* CSSELR_EL1 */ +ARM64_SYS_REG (3 ,3 ,2 ,5 ,1 ),/* GCSPR_EL0 */ ARM64_SYS_REG (3 ,3 ,10 ,2 ,4 ),/* POR_EL0 */ ARM64_SYS_REG (3 ,3 ,13 ,0 ,2 ),/* TPIDR_EL0 */ ARM64_SYS_REG(3, 3, 13, 0, 3), /* TPIDRRO_EL0 */
@@ -742,6 +752,8 @@ static __u64 el2_regs[] = {SYS_REG (VTTBR_EL2 ),SYS_REG (VTCR_EL2 ),SYS_REG (VNCR_EL2 ), +SYS_REG (GCSCR_EL2 ), +SYS_REG (GCSPR_EL2 ),SYS_REG (HDFGRTR2_EL2 ),SYS_REG (HDFGWTR2_EL2 ), SYS_REG(HFGRTR2_EL2),
Signature · 1 line
2.47.3
PATCH v22 13/16 KVM: selftests: arm64: Add GCS to set_id_regs
In reply to Mark BrownNow that GCS is configurable via the ID registers update set_id_regs to cover ID_AA64PFR1_EL1.GCS.
--- tools/testing/selftests/kvm/arm64/set_id_regs.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/testing/selftests/kvm/arm64/set_id_regs.c +8 -0
@@ -155,6 +155,7 @@ static const struct reg_ftr_bits ftr_id_aa64pfr0_el1[] = {static const struct reg_ftr_bits ftr_id_aa64pfr1_el1[] = {REG_FTR_BITS (FTR_LOWER_SAFE ,ID_AA64PFR1_EL1 ,DF2 ,0 ), +REG_FTR_BITS (FTR_LOWER_SAFE ,ID_AA64PFR1_EL1 ,GCS ,0 ),REG_FTR_BITS (FTR_LOWER_SAFE ,ID_AA64PFR1_EL1 , CSV2_frac,0 ),REG_FTR_BITS (FTR_LOWER_SAFE ,ID_AA64PFR1_EL1 ,SSBS ,ID_AA64PFR1_EL1_SSBS_NI ),REG_FTR_BITS (FTR_LOWER_SAFE ,ID_AA64PFR1_EL1 ,BT ,0 ),
@@ -933,6 +934,12 @@ struct reg_ftr_val { .mask = r ## _ ## f ## _MASK, \ .val = (r ## _ ## f ## _ ## v << r ## _ ## f ## _SHIFT) } +static const struct reg_ftr_val gcs_no_s1pie[] = { +REG_FTR_VAL (ID_AA64PFR1_EL1 ,GCS ,IMP ), +REG_FTR_VAL (ID_AA64MMFR3_EL1 ,S1PIE ,NI ), + { } +}; +static const struct reg_ftr_val s1pie_no_tcr2[] = {REG_FTR_VAL (ID_AA64MMFR3_EL1 ,TCRX ,NI ),REG_FTR_VAL (ID_AA64MMFR3_EL1 ,S1PIE ,IMP ),
@@ -951,6 +958,7 @@ struct ftr_config { };static const struct ftr_config invalid_configs[] = { + { .name ="GCS without S1PIE" , .regs = gcs_no_s1pie }, { .name ="S1PIE without TCRX" , .regs = s1pie_no_tcr2 }, { .name ="S1POE without TCRX" , .regs = s1poe_no_tcr2 }, };
Signature · 1 line
2.47.3
PATCH v22 14/16 KVM: selftests: arm64: Only restore SPSR_EL1 and ELR_EL1 if they change
In reply to Mark BrownWhen EXLOCK is enabled for ELx writes to SPSR_ELx and ELR_ELx generate GCS exceptions. Currently the selftest helpers unconditionally reload the saved values for these registers regardless of if the handler didn't change them. This gets in the way of EXLOCK testing, making it hard to generate contexts which will generate an invalid ERET since we never get as far as doing an exception return while EXLOCK is enabled.
In order to facilitate testing of nested virtualisation emulation of EXLOCK change our reloads into read/modify/write cycles, suppressing the writes when they would not change the value in the register.
--- tools/testing/selftests/kvm/lib/arm64/handlers.S | 10 ++++++++++ 1 file changed, 10 insertions(+)
tools/testing/selftests/kvm/lib/arm64/handlers.S +10 -0
@@ -33,8 +33,18 @@ .macro restore_registers ldp x1, x2, [sp, #16 * 16] /* PC, PSTATE */ + + /* Suppress redundant writes to support GCS exception lock testing */ + mrs x3, elr_el1 + cmp x1, x3 + beq .Lskip_elr_\@ msr elr_el1, x1 +.Lskip_elr_\@: + mrs x3, spsr_el1 + cmp x2, x3 + beq .Lskip_spsr_\@ msr spsr_el1, x2 +.Lskip_spsr_\@: /* sp is not restored */ ldp x30, xzr, [sp, #16 * 15] /* x30, SP */
Signature · 1 line
2.47.3
PATCH v22 15/16 tools: Synchronise the kernel esr.h
In reply to Mark BrownTo pick up definitions for GCS.
--- tools/arch/arm64/include/asm/esr.h | 146 ++++++++++++++++++++++++++++++++++--- 1 file changed, 134 insertions(+), 12 deletions(-)
tools/arch/arm64/include/asm/esr.h +134 -12
@@ -3,10 +3,12 @@* Copyright (C) 2013 - ARM Ltd * Author: Marc Zyngier <[email protected]> */ +/* Whole file is shared with s390 */ #ifndef __ASM_ESR_H#define __ASM_ESR_H +#include <asm/brk-imm.h> #include <asm/sysreg.h> #define ESR_ELx_EC_UNKNOWN UL(0x00)
@@ -19,7 +21,8 @@#define ESR_ELx_EC_FP_ASIMD UL(0x07)#define ESR_ELx_EC_CP10_ID UL(0x08)/* EL2 only */ #define ESR_ELx_EC_PAC UL(0x09)/* EL2 and above */ -/* Unallocated EC: 0x0A - 0x0B */ +#define ESR_ELx_EC_OTHER UL(0x0A) +/* Unallocated EC: 0x0B */ #define ESR_ELx_EC_CP14_64 UL(0x0C)#define ESR_ELx_EC_BTI UL(0x0D)#define ESR_ELx_EC_ILL UL(0x0E)
@@ -50,7 +53,8 @@#define ESR_ELx_EC_FP_EXC32 UL(0x28)/* Unallocated EC: 0x29 - 0x2B */ #define ESR_ELx_EC_FP_EXC64 UL(0x2C) -/* Unallocated EC: 0x2D - 0x2E */ +#define ESR_ELx_EC_GCS UL(0x2D) +/* Unallocated EC: 0x2E */ #define ESR_ELx_EC_SERROR UL(0x2F)#define ESR_ELx_EC_BREAKPT_LOW UL(0x30)#define ESR_ELx_EC_BREAKPT_CUR UL(0x31)
@@ -97,6 +101,8 @@#define ESR_ELx_AET_CE (UL(6) << ESR_ELx_AET_SHIFT)/* Shared ISS field definitions for Data/Instruction aborts */ +#define ESR_ELx_VNCR_SHIFT (13) +#define ESR_ELx_VNCR (UL(1) << ESR_ELx_VNCR_SHIFT)#define ESR_ELx_SET_SHIFT (11)#define ESR_ELx_SET_MASK (UL(3) << ESR_ELx_SET_SHIFT)#define ESR_ELx_FnV_SHIFT (10)
@@ -119,6 +125,16 @@#define ESR_ELx_FSC_SEA_TTW (n) (0x14 + (n))#define ESR_ELx_FSC_SECC (0x18)#define ESR_ELx_FSC_SECC_TTW (n) (0x1c + (n)) +#define ESR_ELx_FSC_EXCL_ATOMIC (0x35) +#define ESR_ELx_FSC_ADDRSZ (0x00) + +/* +* Annoyingly, the negative levels for Address size faults aren't laid out +* contiguously (or in the desired order) +*/ +#define ESR_ELx_FSC_ADDRSZ_nL (n) ((n) == -1 ? 0x29 : 0x2C) +#define ESR_ELx_FSC_ADDRSZ_L (n) ((n) < 0 ? ESR_ELx_FSC_ADDRSZ_nL(n) : \ + (ESR_ELx_FSC_ADDRSZ + (n)))/* Status codes for individual page table levels */ #define ESR_ELx_FSC_ACCESS_L (n) (ESR_ELx_FSC_ACCESS + (n))
@@ -141,12 +157,12 @@#define ESR_ELx_SF (UL(1) << ESR_ELx_SF_SHIFT)#define ESR_ELx_AR_SHIFT (14)#define ESR_ELx_AR (UL(1) << ESR_ELx_AR_SHIFT) -#define ESR_ELx_VNCR_SHIFT (13) -#define ESR_ELx_VNCR (UL(1) << ESR_ELx_VNCR_SHIFT)#define ESR_ELx_CM_SHIFT (8)#define ESR_ELx_CM (UL(1) << ESR_ELx_CM_SHIFT)/* ISS2 field definitions for Data Aborts */ +#define ESR_ELx_HDBSSF_SHIFT (11) +#define ESR_ELx_HDBSSF (UL(1) << ESR_ELx_HDBSSF_SHIFT)#define ESR_ELx_TnD_SHIFT (10)#define ESR_ELx_TnD (UL(1) << ESR_ELx_TnD_SHIFT)#define ESR_ELx_TagAccess_SHIFT (9)
@@ -161,8 +177,6 @@#define ESR_ELx_Xs_MASK (GENMASK_ULL(4, 0))/* ISS field definitions for exceptions taken in to Hyp */ -#define ESR_ELx_FSC_ADDRSZ (0x00) -#define ESR_ELx_FSC_ADDRSZ_L (n) (ESR_ELx_FSC_ADDRSZ + (n))#define ESR_ELx_CV (UL(1) << 24)#define ESR_ELx_COND_SHIFT (20)#define ESR_ELx_COND_MASK (UL(0xF) << ESR_ELx_COND_SHIFT)
@@ -174,6 +188,13 @@#define ESR_ELx_WFx_ISS_WFE (UL(1) << 0)#define ESR_ELx_xVC_IMM_MASK ((UL(1) << 16) - 1) +/* ISS definitions for LD64B/ST64B/{T,P}SBCSYNC instructions */ +#define ESR_ELx_ISS_OTHER_ST64BV (0) +#define ESR_ELx_ISS_OTHER_ST64BV0 (1) +#define ESR_ELx_ISS_OTHER_LDST64B (2) +#define ESR_ELx_ISS_OTHER_TSBCSYNC (3) +#define ESR_ELx_ISS_OTHER_PSBCSYNC (4) +#define DISR_EL1_IDS (UL(1) << 24)/* * DISR_EL1 and ESR_ELx share the bottom 13 bits, but the RES0 bits may mean
@@ -371,12 +392,14 @@/* * ISS values for SME traps */ +#define ESR_ELx_SME_ISS_SMTC_MASK GENMASK(2, 0) +#define ESR_ELx_SME_ISS_SMTC (esr) ((esr) & ESR_ELx_SME_ISS_SMTC_MASK) -#define ESR_ELx_SME_ISS_SME_DISABLED 0 -#define ESR_ELx_SME_ISS_ILL 1 -#define ESR_ELx_SME_ISS_SM_DISABLED 2 -#define ESR_ELx_SME_ISS_ZA_DISABLED 3 -#define ESR_ELx_SME_ISS_ZT_DISABLED 4 +#define ESR_ELx_SME_ISS_SMTC_SME_DISABLED 0 +#define ESR_ELx_SME_ISS_SMTC_ILL 1 +#define ESR_ELx_SME_ISS_SMTC_SM_DISABLED 2 +#define ESR_ELx_SME_ISS_SMTC_ZA_DISABLED 3 +#define ESR_ELx_SME_ISS_SMTC_ZT_DISABLED 4/* ISS field definitions for MOPS exceptions */ #define ESR_ELx_MOPS_ISS_MEM_INST (UL(1) << 24)
@@ -387,8 +410,62 @@#define ESR_ELx_MOPS_ISS_SRCREG (esr) (((esr) & (UL(0x1f) << 5)) >> 5)#define ESR_ELx_MOPS_ISS_SIZEREG (esr) (((esr) & (UL(0x1f) << 0)) >> 0) +/* ISS field definitions for GCS */ +#define ESR_ELx_ExType_SHIFT (20) +#define ESR_ELx_ExType_MASK GENMASK(23, 20) +#define ESR_ELx_Raddr_SHIFT (10) +#define ESR_ELx_Raddr_MASK GENMASK(14, 10) +#define ESR_ELx_Rn_SHIFT (5) +#define ESR_ELx_Rn_MASK GENMASK(9, 5) +#define ESR_ELx_Rvalue_SHIFT 5 +#define ESR_ELx_Rvalue_MASK GENMASK(9, 5) +#define ESR_ELx_IT_SHIFT (0) +#define ESR_ELx_IT_MASK GENMASK(4, 0) + +#define ESR_ELx_ExType_DATA_CHECK 0 +#define ESR_ELx_ExType_EXLOCK 1 +#define ESR_ELx_ExType_STR 2 + +#define ESR_ELx_IT_RET 0 +#define ESR_ELx_IT_GCSPOPM 1 +#define ESR_ELx_IT_RET_KEYA 2 +#define ESR_ELx_IT_RET_KEYB 3 +#define ESR_ELx_IT_GCSSS1 4 +#define ESR_ELx_IT_GCSSS2 5 +#define ESR_ELx_IT_GCSPOPCX 6 +#define ESR_ELx_IT_GCSPOPX 7 +#ifndef __ASSEMBLER__ -#include <asm/types.h> +#include <linux/types.h> + +static __always_inline bool esr_trap_is_iabt(unsigned long esr) +{ +return ESR_ELx_EC (esr) == ESR_ELx_EC_IABT_LOW; +} + +/* Always check for S1PTW *before* using this. */ +static __always_inline bool esr_dabt_is_write(unsigned long esr) +{ +return esr & ESR_ELx_WNR; +} + +static __always_inline bool esr_dabt_is_cm(unsigned long esr) +{ +return esr & ESR_ELx_CM; +} + +static __always_inline bool esr_abt_is_sea(unsigned long esr) +{ +switch (esr & ESR_ELx_FSC) { +case ESR_ELx_FSC_EXTABT: + case ESR_ELx_FSC_SEA_TTW(-1) ... ESR_ELx_FSC_SEA_TTW(3): +case ESR_ELx_FSC_SECC: + case ESR_ELx_FSC_SECC_TTW(-1) ... ESR_ELx_FSC_SECC_TTW(3): +return true; +default : +return false; + } +}static inline unsigned long esr_brk_comment (unsigned long esr) {
@@ -408,6 +485,11 @@ static inline bool esr_is_cfi_brk(unsigned long esr) (esr_brk_comment (esr) & ~CFI_BRK_IMM_MASK ) ==CFI_BRK_IMM_BASE ; } +static inline bool esr_is_ubsan_brk (unsigned long esr) +{ +return (esr_brk_comment (esr) & ~UBSAN_BRK_MASK ) ==UBSAN_BRK_IMM ; +} +static inline bool esr_fsc_is_translation_fault (unsigned long esr) { esr = esr & ESR_ELx_FSC;
@@ -439,6 +521,46 @@ static inline bool esr_fsc_is_access_flag_fault(unsigned long esr) (esr ==ESR_ELx_FSC_ACCESS_L (0 )); } +static inline bool esr_fsc_is_excl_atomic_fault (unsigned long esr) +{ + esr = esr & ESR_ELx_FSC; + +return esr == ESR_ELx_FSC_EXCL_ATOMIC; +} + +static inline bool esr_fsc_is_addr_sz_fault (unsigned long esr) +{ + esr &= ESR_ELx_FSC; + +return (esr ==ESR_ELx_FSC_ADDRSZ_L (3 )) || + (esr ==ESR_ELx_FSC_ADDRSZ_L (2 )) || + (esr ==ESR_ELx_FSC_ADDRSZ_L (1 )) || + (esr ==ESR_ELx_FSC_ADDRSZ_L (0 )) || + (esr ==ESR_ELx_FSC_ADDRSZ_L (-1 )); +} + +static inline bool esr_fsc_is_sea_ttw (unsigned long esr) +{ + esr = esr & ESR_ELx_FSC; + +return (esr ==ESR_ELx_FSC_SEA_TTW (3 )) || + (esr ==ESR_ELx_FSC_SEA_TTW (2 )) || + (esr ==ESR_ELx_FSC_SEA_TTW (1 )) || + (esr ==ESR_ELx_FSC_SEA_TTW (0 )) || + (esr ==ESR_ELx_FSC_SEA_TTW (-1 )); +} + +static inline bool esr_fsc_is_secc_ttw (unsigned long esr) +{ + esr = esr & ESR_ELx_FSC; + +return (esr ==ESR_ELx_FSC_SECC_TTW (3 )) || + (esr ==ESR_ELx_FSC_SECC_TTW (2 )) || + (esr ==ESR_ELx_FSC_SECC_TTW (1 )) || + (esr ==ESR_ELx_FSC_SECC_TTW (0 )) || + (esr ==ESR_ELx_FSC_SECC_TTW (-1 )); +} +/* Indicate whether ESR.EC==0x1A is for an ERETAx instruction */ static inline bool esr_iss_is_eretax (unsigned long esr) {
Signature · 1 line
2.47.3
PATCH v22 16/16 KVM: selftests: arm64: Add GCS EXLOCK exception emulation test
In reply to Mark BrownWhen running in vEL2 ERET is emulated, this means that we must emulate the behaviour of GCSCR_EL2.EXLOCKEN. As per DDI0487 R_WTXBY PSTATE.EXLOCK is set to the value of GCSCR_ELx.EXLOCKEN when taking an exception to the same exception level. As per DDI0487 R_TYTWB if on exception return to the same EL, GCSCR_ELx.EXLOCKEN is 1 and PSTATE.EXLOCK is not 1 then the exception return is illegal, and as per the pseudocode for the registers writes to SPSR_ELx and ELR_ELx will generate exceptions.
Add a test that validates that we set PSTATE.EXLOCK, checking that behaviour is seen by vEL2 consistent with it being set.
We nest handling of two exceptions to allow us to both observe and modify PSTATE.EXLOCK, checking that expected exceptions are generated for ERET. In order to cover both hardware programming and emulation the first exception is done with both UDF (for hardware) and HVC (for emulation).
We also attempt to write to both SPSR_ELx and ELR_ELx, again using a UDF or HVC handler to control PSTATE.EXLOCK. We do all this both with and without having enabled EXLOCKEN, though only for code running at EL2 due to framework limitations.
For completeness we also inject a range of exceptions with GCSCR_ELx.EXLOCKEN set, ensuring that PSTATE.EXLOCK is set appropriately.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/arm64/gcs_exceptions.c | 500 +++++++++++++++++++++ 2 files changed, 501 insertions(+)
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -186,6 +186,7 @@ TEST_GEN_PROGS_arm64 += arm64/host_sve TEST_GEN_PROGS_arm64 += arm64/hypercalls TEST_GEN_PROGS_arm64 += arm64/nv_pre_fault_memory_test TEST_GEN_PROGS_arm64 += arm64/external_aborts +TEST_GEN_PROGS_arm64 += arm64/gcs_exceptions TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext TEST_GEN_PROGS_arm64 += arm64/page_fault_test TEST_GEN_PROGS_arm64 += arm64/psci_test
tools/testing/selftests/kvm/arm64/gcs_exceptions.c new file +500 -0
500 lines not on this page · raw message
Signature · 1 line
2.47.3