* [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS
@ 2026-11-30 16:35 Mark Brown
2026-11-30 16:35 ` [PATCH v22 01/16] arm64/gcs: Ensure FGTs for EL1 GCS instructions are disabled Mark Brown
` (15 more replies)
0 siblings, 16 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown, Lorenzo Stoakes (ARM),
Thiago Jung Bauermann
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.
Signed-off-by: Mark Brown <broonie@kernel.org>
---
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/20260930-arm64-gcs-v21-0-3556644cd927@kernel.org
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/20260901-arm64-gcs-v20-0-f31750bdfadb@kernel.org
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/20260812-arm64-gcs-v19-0-9105afd828ac@kernel.org
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/20260803-arm64-gcs-v18-0-fd8c40f5ad88@kernel.org
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/20260731-arm64-gcs-v17-0-5e39ca01b14e@kernel.org
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/20250912-arm64-gcs-v16-0-6435e5ec37db@kernel.org
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/20250820-arm64-gcs-v15-0-5e334da18b84@kernel.org
Changes in v15:
- Rebase onto v6.17-rc1.
- Link to v14: https://lore.kernel.org/r/20241005-arm64-gcs-v14-0-59060cd6092b@kernel.org
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/20241001-arm64-gcs-v13-0-222b78d87eee@kernel.org
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/20240829-arm64-gcs-v12-0-42fec947436a@kernel.org
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/20240822-arm64-gcs-v11-0-41b81947ecb5@kernel.org
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/20240801-arm64-gcs-v10-0-699e2bd2190b@kernel.org
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/20240625-arm64-gcs-v9-0-0f634469b8f0@kernel.org
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/20240203-arm64-gcs-v8-0-c9fec77673ef@kernel.org
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/20231122-arm64-gcs-v7-0-201c483bd775@kernel.org
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/20231009-arm64-gcs-v6-0-78e55deaa4dd@kernel.org
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/20230822-arm64-gcs-v5-0-9ef181dd6324@kernel.org
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/20230807-arm64-gcs-v4-0-68cfa37f9069@kernel.org
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/20230731-arm64-gcs-v3-0-cddf9f980d98@kernel.org
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/20230724-arm64-gcs-v2-0-dc2c1d44c2eb@kernel.org
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/20230716-arm64-gcs-v1-0-bf567f93bba6@kernel.org
---
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,
--
Mark Brown <broonie@kernel.org>
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 01/16] arm64/gcs: Ensure FGTs for EL1 GCS instructions are disabled
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 02/16] KVM: arm64: Drop the SPSR_EL1 and ELR_EL1 trap accessors Mark Brown
` (14 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown, Lorenzo Stoakes (ARM)
The initial EL2 setup for GCS did not include disabling of EL1 usage of
GCS instructions, also disable these traps.
Fixes: ff5181d8a2a8 ("arm64/gcs: Provide basic EL2 setup to allow GCS usage at EL0 and EL1")
Reviewed-by: Leonardo Bras <leo.bras@arm.com>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
arch/arm64/include/asm/el2_setup.h | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/arch/arm64/include/asm/el2_setup.h b/arch/arm64/include/asm/el2_setup.h
index 12a500181291..93ad6913ac80 100644
--- a/arch/arm64/include/asm/el2_setup.h
+++ b/arch/arm64/include/asm/el2_setup.h
@@ -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_\@:
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 02/16] KVM: arm64: Drop the SPSR_EL1 and ELR_EL1 trap accessors
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
2026-11-30 16:35 ` [PATCH v22 01/16] arm64/gcs: Ensure FGTs for EL1 GCS instructions are disabled Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 03/16] KVM: arm64: Manage GCS access and registers for guests Mark Brown
` (13 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown
An 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.
Signed-off-by: Mark Brown <broonie@kernel.org>
---
arch/arm64/kvm/sys_regs.c | 27 ---------------------------
1 file changed, 27 deletions(-)
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index 5555e17a7710..399dfcf102ae 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -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 },
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 03/16] KVM: arm64: Manage GCS access and registers for guests
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
2026-11-30 16:35 ` [PATCH v22 01/16] arm64/gcs: Ensure FGTs for EL1 GCS instructions are disabled Mark Brown
2026-11-30 16:35 ` [PATCH v22 02/16] KVM: arm64: Drop the SPSR_EL1 and ELR_EL1 trap accessors Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:49 ` Leonardo Bras
2026-11-30 16:35 ` [PATCH v22 04/16] KVM: arm64: Ensure GCS memory effects are visible Mark Brown
` (12 subsequent siblings)
15 siblings, 1 reply; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, 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_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.
Signed-off-by: Mark Brown <broonie@kernel.org>
---
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(+)
diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h
index 2d7c82cfb8a4..f5b0b0393f9c 100644
--- a/arch/arm64/include/asm/kvm_emulate.h
+++ b/arch/arm64/include/asm/kvm_emulate.h
@@ -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
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 0614c3b3a40d..5130e34fae07 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -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;
diff --git a/arch/arm64/include/asm/vncr_mapping.h b/arch/arm64/include/asm/vncr_mapping.h
index 9e8a49fa8b63..e589adccc2f3 100644
--- a/arch/arm64/include/asm/vncr_mapping.h
+++ b/arch/arm64/include/asm/vncr_mapping.h
@@ -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
diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c
index 39136a89d74b..cb9bf3b9340a 100644
--- a/arch/arm64/kvm/emulate-nested.c
+++ b/arch/arm64/kvm/emulate-nested.c
@@ -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... */
diff --git a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
index 4a008c8a696b..bdaaa3c85a91 100644
--- a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
+++ b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
@@ -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);
diff --git a/arch/arm64/kvm/hyp/vhe/sysreg-sr.c b/arch/arm64/kvm/hyp/vhe/sysreg-sr.c
index 0c4ef1ce32ae..a961fe1e91ae 100644
--- a/arch/arm64/kvm/hyp/vhe/sysreg-sr.c
+++ b/arch/arm64/kvm/hyp/vhe/sysreg-sr.c
@@ -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);
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index 399dfcf102ae..3fc9e47a3c39 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -109,6 +109,8 @@ static enum sr_loc_attr locate_direct_register(const struct kvm_vcpu *vcpu,
case PIR_EL1:
case PIRE0_EL1:
case POR_EL1:
+ case GCSCR_EL1:
+ case GCSPR_EL1:
case ESR_EL1:
case AFSR0_EL1:
case AFSR1_EL1:
@@ -135,6 +137,8 @@ static enum sr_loc_attr locate_direct_register(const struct kvm_vcpu *vcpu,
case DACR32_EL2:
case IFSR32_EL2:
case DBGVCR32_EL2:
+ case GCSCRE0_EL1:
+ case GCSPR_EL0:
/* These registers are always loaded, no matter what */
return SR_LOC_LOADED;
@@ -198,6 +202,8 @@ static void locate_register(const struct kvm_vcpu *vcpu, enum vcpu_sysreg reg,
MAPPED_EL2_SYSREG(PIR_EL2, PIR_EL1, NULL );
MAPPED_EL2_SYSREG(PIRE0_EL2, PIRE0_EL1, NULL );
MAPPED_EL2_SYSREG(POR_EL2, POR_EL1, NULL );
+ MAPPED_EL2_SYSREG(GCSCR_EL2, GCSCR_EL1, NULL );
+ MAPPED_EL2_SYSREG(GCSPR_EL2, GCSPR_EL1, NULL );
MAPPED_EL2_SYSREG(AMAIR_EL2, AMAIR_EL1, NULL );
MAPPED_EL2_SYSREG(ELR_EL2, ELR_EL1, NULL );
MAPPED_EL2_SYSREG(SPSR_EL2, SPSR_EL1, NULL );
@@ -253,6 +259,10 @@ static u64 read_sr_from_cpu(enum vcpu_sysreg reg)
case TCR2_EL1: val = read_sysreg_s(SYS_TCR2_EL12); break;
case PIR_EL1: val = read_sysreg_s(SYS_PIR_EL12); break;
case PIRE0_EL1: val = read_sysreg_s(SYS_PIRE0_EL12); break;
+ case GCSPR_EL0: val = read_sysreg_s(SYS_GCSPR_EL0); break;
+ case GCSCRE0_EL1: val = read_sysreg_s(SYS_GCSCRE0_EL1); break;
+ case GCSCR_EL1: val = read_sysreg_s(SYS_GCSCR_EL12); break;
+ case GCSPR_EL1: val = read_sysreg_s(SYS_GCSPR_EL12); break;
case POR_EL1: val = read_sysreg_s(SYS_POR_EL12); break;
case ESR_EL1: val = read_sysreg_s(SYS_ESR_EL12); break;
case AFSR0_EL1: val = read_sysreg_s(SYS_AFSR0_EL12); break;
@@ -293,6 +303,10 @@ static void write_sr_to_cpu(enum vcpu_sysreg reg, u64 val)
case PIR_EL1: write_sysreg_s(val, SYS_PIR_EL12); break;
case PIRE0_EL1: write_sysreg_s(val, SYS_PIRE0_EL12); break;
case POR_EL1: write_sysreg_s(val, SYS_POR_EL12); break;
+ case GCSCRE0_EL1: write_sysreg_s(val, SYS_GCSCRE0_EL1); break;
+ case GCSPR_EL0: write_sysreg_s(val, SYS_GCSPR_EL0); break;
+ case GCSCR_EL1: write_sysreg_s(val, SYS_GCSCR_EL12); break;
+ case GCSPR_EL1: write_sysreg_s(val, SYS_GCSPR_EL12); break;
case ESR_EL1: write_sysreg_s(val, SYS_ESR_EL12); break;
case AFSR0_EL1: write_sysreg_s(val, SYS_AFSR0_EL12); break;
case AFSR1_EL1: write_sysreg_s(val, SYS_AFSR1_EL12); break;
@@ -3074,6 +3088,21 @@ static unsigned int cnthv_visibility(const struct kvm_vcpu *vcpu,
return REG_HIDDEN;
}
+static unsigned int gcs_visibility(const struct kvm_vcpu *vcpu,
+ const struct sys_reg_desc *r)
+{
+ if (kvm_has_gcs(vcpu->kvm))
+ return 0;
+
+ return REG_HIDDEN;
+}
+
+static unsigned int gcs_el2_visibility(const struct kvm_vcpu *vcpu,
+ const struct sys_reg_desc *rd)
+{
+ return __el2_visibility(vcpu, rd, gcs_visibility);
+}
+
static bool access_mdcr(struct kvm_vcpu *vcpu,
struct sys_reg_params *p,
const struct sys_reg_desc *r)
@@ -3486,6 +3515,13 @@ static const struct sys_reg_desc sys_reg_descs[] = {
PTRAUTH_KEY(APDB),
PTRAUTH_KEY(APGA),
+ { SYS_DESC(SYS_GCSCR_EL1), NULL, reset_val, GCSCR_EL1, 0,
+ .visibility = gcs_visibility },
+ { SYS_DESC(SYS_GCSPR_EL1), NULL, reset_unknown, GCSPR_EL1,
+ .visibility = gcs_visibility },
+ { SYS_DESC(SYS_GCSCRE0_EL1), NULL, reset_val, GCSCRE0_EL1, 0,
+ .visibility = gcs_visibility },
+
{ SYS_DESC(SYS_ICC_PMR_EL1), undef_access },
{ SYS_DESC(SYS_AFSR0_EL1), access_vm_reg, reset_unknown, AFSR0_EL1 },
@@ -3609,6 +3645,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
CTR_EL0_DminLine_MASK |
CTR_EL0_L1Ip_MASK |
CTR_EL0_IminLine_MASK),
+ { SYS_DESC(SYS_GCSPR_EL0), NULL, reset_unknown, GCSPR_EL0,
+ .visibility = gcs_visibility },
{ SYS_DESC(SYS_SVCR), undef_access, reset_val, SVCR, 0, .visibility = sme_visibility },
{ SYS_DESC(SYS_FPMR), undef_access, reset_val, FPMR, 0, .visibility = fp8_visibility },
@@ -3852,6 +3890,10 @@ static const struct sys_reg_desc sys_reg_descs[] = {
EL2_REG_FILTERED(VNCR_EL2, bad_vncr_trap, reset_val, 0,
vncr_el2_visibility),
+ EL2_REG_FILTERED(GCSCR_EL2, access_rw, reset_val, 0,
+ gcs_el2_visibility),
+ EL2_REG_FILTERED(GCSPR_EL2, access_rw, reset_val, 0,
+ gcs_el2_visibility),
{ SYS_DESC(SYS_DACR32_EL2), undef_access, reset_unknown, DACR32_EL2 },
EL2_REG_VNCR_FILT(HDFGRTR2_EL2, fgt2_visibility),
EL2_REG_VNCR_FILT(HDFGWTR2_EL2, fgt2_visibility),
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 04/16] KVM: arm64: Ensure GCS memory effects are visible
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (2 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 03/16] KVM: arm64: Manage GCS access and registers for guests Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 05/16] KVM: arm64: Set PSTATE.EXLOCK when entering an exception Mark Brown
` (11 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown, Lorenzo Stoakes (ARM)
In 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.
Reviewed-by: Yuan Yao <yaoyuan@linux.alibaba.com>
Reviewed-by: Leonardo Bras <leo.bras@arm.com>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
arch/arm64/kvm/arm.c | 18 ++++++++++++++++++
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 +++++++
2 files changed, 25 insertions(+)
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index a148e80d0aef..2064cff8e92a 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -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);
}
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 0c023ee0cf42..18030dd0dc0a 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -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);
}
}
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 05/16] KVM: arm64: Set PSTATE.EXLOCK when entering an exception
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (3 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 04/16] KVM: arm64: Ensure GCS memory effects are visible Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 06/16] KVM: arm64: Validate GCS exception lock when emulating ERET Mark Brown
` (10 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown, Lorenzo Stoakes (ARM)
As 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().
Reviewed-by: Yuan Yao <yaoyuan@linux.alibaba.com>
Reviewed-by: Leonardo Bras <leo.bras@arm.com>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
arch/arm64/include/uapi/asm/ptrace.h | 1 +
arch/arm64/kvm/hyp/exception.c | 40 +++++++++++++++++++++++++++++++++++-
2 files changed, 40 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/include/uapi/asm/ptrace.h b/arch/arm64/include/uapi/asm/ptrace.h
index 0c8c6a126c3b..ac38744951c7 100644
--- a/arch/arm64/include/uapi/asm/ptrace.h
+++ b/arch/arm64/include/uapi/asm/ptrace.h
@@ -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
diff --git a/arch/arm64/kvm/hyp/exception.c b/arch/arm64/kvm/hyp/exception.c
index 7521bddf73e4..ce482c5dedfc 100644
--- a/arch/arm64/kvm/hyp/exception.c
+++ b/arch/arm64/kvm/hyp/exception.c
@@ -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);
}
/*
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 06/16] KVM: arm64: Validate GCS exception lock when emulating ERET
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (4 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 05/16] KVM: arm64: Set PSTATE.EXLOCK when entering an exception Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 07/16] KVM: arm64: Forward GCS exceptions to nested guests Mark Brown
` (9 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown
As 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.
Reviewed-by: Leonardo Bras <leo.bras@arm.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
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(-)
diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h
index 27ec73da02c6..a158de5f4f92 100644
--- a/arch/arm64/include/asm/kvm_nested.h
+++ b/arch/arm64/include/asm/kvm_nested.h
@@ -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)
diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c
index cb9bf3b9340a..c4a33f1f6ad4 100644
--- a/arch/arm64/kvm/emulate-nested.c
+++ b/arch/arm64/kvm/emulate-nested.c
@@ -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;
diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c
index 5f7a46c4f516..12e9762e1a86 100644
--- a/arch/arm64/kvm/hyp/vhe/switch.c
+++ b/arch/arm64/kvm/hyp/vhe/switch.c
@@ -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)))
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 07/16] KVM: arm64: Forward GCS exceptions to nested guests
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (5 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 06/16] KVM: arm64: Validate GCS exception lock when emulating ERET Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 08/16] KVM: arm64: Preserve ISS2.GCS when injecting a Data Abort Mark Brown
` (8 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown, Lorenzo Stoakes (ARM)
For 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.
Reviewed-by: Leonardo Bras <leo.bras@arm.com>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
arch/arm64/kvm/handle_exit.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
index 9931192735d4..acd97a435910 100644
--- a/arch/arm64/kvm/handle_exit.c
+++ b/arch/arm64/kvm/handle_exit.c
@@ -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;
}
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 08/16] KVM: arm64: Preserve ISS2.GCS when injecting a Data Abort
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (6 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 07/16] KVM: arm64: Forward GCS exceptions to nested guests Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 09/16] KVM: arm64: Fully implement GCS in the stage-1 walker Mark Brown
` (7 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown
A 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.
Signed-off-by: Mark Brown <broonie@kernel.org>
---
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(-)
diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h
index f5b0b0393f9c..09d16ea97d08 100644
--- a/arch/arm64/include/asm/kvm_emulate.h
+++ b/arch/arm64/include/asm/kvm_emulate.h
@@ -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);
diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c
index c4a33f1f6ad4..c6ebb557ed9b 100644
--- a/arch/arm64/kvm/emulate-nested.c
+++ b/arch/arm64/kvm/emulate-nested.c
@@ -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);
diff --git a/arch/arm64/kvm/inject_fault.c b/arch/arm64/kvm/inject_fault.c
index aa2011cc0d5a..108b76373bd2 100644
--- a/arch/arm64/kvm/inject_fault.c
+++ b/arch/arm64/kvm/inject_fault.c
@@ -152,8 +152,10 @@ static void inject_abt64(struct kvm_vcpu *vcpu, bool is_iabt, unsigned long addr
else
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);
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 78d5b18dbd2d..ed10c8b41cc8 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -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);
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 09/16] KVM: arm64: Fully implement GCS in the stage-1 walker
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (7 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 08/16] KVM: arm64: Preserve ISS2.GCS when injecting a Data Abort Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 10/16] KVM: arm64: Allow GCS to be enabled for guests Mark Brown
` (6 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown
When 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.
Signed-off-by: Mark Brown <broonie@kernel.org>
---
arch/arm64/kvm/at.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/kvm/at.c b/arch/arm64/kvm/at.c
index 30d8f062b2c2..42180f694093 100644
--- a/arch/arm64/kvm/at.c
+++ b/arch/arm64/kvm/at.c
@@ -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);
}
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 10/16] KVM: arm64: Allow GCS to be enabled for guests
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (8 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 09/16] KVM: arm64: Fully implement GCS in the stage-1 walker Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 11/16] KVM: selftests: arm64: Check invalid feature combination handling Mark Brown
` (5 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown
Now 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.
Signed-off-by: Mark Brown <broonie@kernel.org>
---
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(-)
diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index 6f668148ccc1..4e9c4cbba9af 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -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)
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index ed10c8b41cc8..bea2816b6218 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -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 |
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index 3fc9e47a3c39..16311d87661b 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -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 |
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 11/16] KVM: selftests: arm64: Check invalid feature combination handling
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (9 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 10/16] KVM: arm64: Allow GCS to be enabled for guests Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 12/16] KVM: selftests: arm64: Add GCS registers to get-reg-list Mark Brown
` (4 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown
In 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.
Signed-off-by: Mark Brown <broonie@kernel.org>
---
tools/testing/selftests/kvm/arm64/set_id_regs.c | 97 +++++++++++++++++++++++++
1 file changed, 97 insertions(+)
diff --git a/tools/testing/selftests/kvm/arm64/set_id_regs.c b/tools/testing/selftests/kvm/arm64/set_id_regs.c
index 1d2cc25cb3f4..bea4dab7496c 100644
--- a/tools/testing/selftests/kvm/arm64/set_id_regs.c
+++ b/tools/testing/selftests/kvm/arm64/set_id_regs.c
@@ -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();
}
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 12/16] KVM: selftests: arm64: Add GCS registers to get-reg-list
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (10 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 11/16] KVM: selftests: arm64: Check invalid feature combination handling Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 13/16] KVM: selftests: arm64: Add GCS to set_id_regs Mark Brown
` (3 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown, Thiago Jung Bauermann,
Lorenzo Stoakes (ARM)
GCS adds new registers GCSCR_ELx, GCSCRE0_EL1 and GCSPR_ELx. Add
these to those validated by get-reg-list.
Reviewed-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
tools/testing/selftests/kvm/arm64/get-reg-list.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/tools/testing/selftests/kvm/arm64/get-reg-list.c b/tools/testing/selftests/kvm/arm64/get-reg-list.c
index 912590f9b0c2..993c137e4e4c 100644
--- a/tools/testing/selftests/kvm/arm64/get-reg-list.c
+++ b/tools/testing/selftests/kvm/arm64/get-reg-list.c
@@ -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),
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 13/16] KVM: selftests: arm64: Add GCS to set_id_regs
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (11 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 12/16] KVM: selftests: arm64: Add GCS registers to get-reg-list Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 14/16] KVM: selftests: arm64: Only restore SPSR_EL1 and ELR_EL1 if they change Mark Brown
` (2 subsequent siblings)
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown, Lorenzo Stoakes (ARM)
Now that GCS is configurable via the ID registers update set_id_regs to
cover ID_AA64PFR1_EL1.GCS.
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
tools/testing/selftests/kvm/arm64/set_id_regs.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/tools/testing/selftests/kvm/arm64/set_id_regs.c b/tools/testing/selftests/kvm/arm64/set_id_regs.c
index bea4dab7496c..abcb84fd2fe7 100644
--- a/tools/testing/selftests/kvm/arm64/set_id_regs.c
+++ b/tools/testing/selftests/kvm/arm64/set_id_regs.c
@@ -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 },
};
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 14/16] KVM: selftests: arm64: Only restore SPSR_EL1 and ELR_EL1 if they change
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (12 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 13/16] KVM: selftests: arm64: Add GCS to set_id_regs Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 15/16] tools: Synchronise the kernel esr.h Mark Brown
2026-11-30 16:35 ` [PATCH v22 16/16] KVM: selftests: arm64: Add GCS EXLOCK exception emulation test Mark Brown
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown, Lorenzo Stoakes (ARM)
When 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.
Reviewed-by: Leonardo Bras <leo.bras@arm.com>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
tools/testing/selftests/kvm/lib/arm64/handlers.S | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/tools/testing/selftests/kvm/lib/arm64/handlers.S b/tools/testing/selftests/kvm/lib/arm64/handlers.S
index 0e443eadfac6..367c22d61cba 100644
--- a/tools/testing/selftests/kvm/lib/arm64/handlers.S
+++ b/tools/testing/selftests/kvm/lib/arm64/handlers.S
@@ -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 */
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 15/16] tools: Synchronise the kernel esr.h
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (13 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 14/16] KVM: selftests: arm64: Only restore SPSR_EL1 and ELR_EL1 if they change Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 16/16] KVM: selftests: arm64: Add GCS EXLOCK exception emulation test Mark Brown
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown
To pick up definitions for GCS.
Reviewed-by: Leonardo Bras <leo.bras@arm.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
tools/arch/arm64/include/asm/esr.h | 146 ++++++++++++++++++++++++++++++++++---
1 file changed, 134 insertions(+), 12 deletions(-)
diff --git a/tools/arch/arm64/include/asm/esr.h b/tools/arch/arm64/include/asm/esr.h
index f3c6403e5ef2..a584f88dbaf9 100644
--- a/tools/arch/arm64/include/asm/esr.h
+++ b/tools/arch/arm64/include/asm/esr.h
@@ -3,10 +3,12 @@
* Copyright (C) 2013 - ARM Ltd
* Author: Marc Zyngier <marc.zyngier@arm.com>
*/
+/* 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)
{
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v22 16/16] KVM: selftests: arm64: Add GCS EXLOCK exception emulation test
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
` (14 preceding siblings ...)
2026-11-30 16:35 ` [PATCH v22 15/16] tools: Synchronise the kernel esr.h Mark Brown
@ 2026-11-30 16:35 ` Mark Brown
15 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:35 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba
Cc: Peter Maydell, Leonardo Bras, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel, Mark Brown
When 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.
Signed-off-by: Mark Brown <broonie@kernel.org>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
tools/testing/selftests/kvm/arm64/gcs_exceptions.c | 500 +++++++++++++++++++++
2 files changed, 501 insertions(+)
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 283c59d7649c..d6e7d049ff99 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -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
diff --git a/tools/testing/selftests/kvm/arm64/gcs_exceptions.c b/tools/testing/selftests/kvm/arm64/gcs_exceptions.c
new file mode 100644
index 000000000000..bffd913e7530
--- /dev/null
+++ b/tools/testing/selftests/kvm/arm64/gcs_exceptions.c
@@ -0,0 +1,500 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Test KVM's handling of Guarded Control Stack exception state.
+ */
+
+#include "kvm_util.h"
+#include "processor.h"
+#include "test_util.h"
+#include "ucall.h"
+
+#include <asm/ptrace.h>
+#include <asm/sysreg.h>
+
+#define PSTATE_IL_BIT BIT(20)
+
+static void require_guest_gcs(struct kvm_vcpu *vcpu)
+{
+ u64 pfr1 = vcpu_get_reg(vcpu,
+ KVM_ARM64_SYS_REG(SYS_ID_AA64PFR1_EL1));
+
+ TEST_REQUIRE(SYS_FIELD_GET(ID_AA64PFR1_EL1, GCS, pfr1) >=
+ ID_AA64PFR1_EL1_GCS_IMP);
+}
+
+static bool expect_exlock;
+static unsigned int undef_count;
+static unsigned int hvc_count;
+static unsigned int illegal_count;
+static unsigned int gcs_count;
+static u64 handler_pstate;
+static u64 illegal_pstate;
+
+static u64 expected_illegal_pc;
+static enum udf_mode {
+ UDF_COLLECT, /* Initial UDF we collect PSTATE from */
+ UDF_VALIDATE, /* Secondary UDF that collects and validates */
+ UDF_REPAIR, /* UDF to clear EXLOCK */
+ UDF_ELR_EL1, /* Write to ELR_EL1 with EXLOCK */
+ UDF_ELR_EL2, /* Write to ELR_EL2 with EXLOCK */
+ UDF_SPSR_EL1, /* Write to SPSR_EL1 with EXLOCK */
+ UDF_SPSR_EL2, /* Write to SPSR_EL2 with EXLOCK */
+} udf_mode;
+
+/*
+ * A UDF at vEL2 is taken by the CPU without involving the host, the
+ * host only sees the ERET from the handler. A HVC traps to the host
+ * which then emulates taking the exception to vEL2, so it is KVM that
+ * has to set PSTATE.EXLOCK for the handler.
+ */
+static void take_exception(bool hvc)
+{
+ if (hvc)
+ asm volatile("hvc #0" ::: "memory");
+ else
+ asm volatile("udf #0" ::: "memory");
+}
+
+static void guest_exception_handler(struct ex_regs *regs)
+{
+ u64 esr = read_sysreg(esr_el2);
+ u64 val;
+
+ switch (ESR_ELx_EC(esr)) {
+ case ESR_ELx_EC_UNKNOWN:
+ undef_count++;
+
+ /* Just skip the instruction either way. */
+ GUEST_ASSERT_EQ(esr, ESR_ELx_IL);
+ regs->pc += 4;
+ break;
+
+ case ESR_ELx_EC_HVC64:
+ hvc_count++;
+
+ /* We return to the instruction after the HVC, don't skip. */
+ GUEST_ASSERT_EQ(esr, (ESR_ELx_EC_HVC64 << ESR_ELx_EC_SHIFT) |
+ ESR_ELx_IL);
+ break;
+
+ default:
+ GUEST_FAIL("Unexpected exception, ESR %#lx", esr);
+ }
+
+ switch (udf_mode) {
+ case UDF_COLLECT:
+ GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN,
+ expect_exlock ? GCSCR_ELx_EXLOCKEN : 0);
+
+ /*
+ * If we want the nested exception handler to change
+ * PSTATE it can't have EXLOCK set.
+ */
+ write_sysreg_s(0, SYS_GCSCR_EL2);
+ isb();
+
+ /*
+ * Take another exception without returning so we can
+ * collect the EXLOCK state we're seeing here and make
+ * our return illegal.
+ */
+ expected_illegal_pc = regs->pc;
+ udf_mode = UDF_VALIDATE;
+ asm volatile("udf #0" ::: "memory");
+
+ /* Reenable so PSTATE.EXLOCK is checked */
+ write_sysreg_s(expect_exlock ? GCSCR_ELx_EXLOCKEN : 0,
+ SYS_GCSCR_EL2);
+ isb();
+ break;
+
+ case UDF_VALIDATE:
+ GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN,
+ 0);
+
+ /* Validate the EXLOCK of the original exception. */
+ handler_pstate = regs->pstate;
+ GUEST_ASSERT_EQ(!!(regs->pstate & PSR_EXLOCK_BIT),
+ expect_exlock);
+
+ /* Clearing PSTATE.EXLOCK triggers an illegal ERET. */
+ regs->pstate &= ~PSR_EXLOCK_BIT;
+ break;
+
+ case UDF_REPAIR:
+ GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN,
+ 0);
+
+ /* Disable EXLOCK for the requesting context */
+ regs->pstate &= ~PSR_EXLOCK_BIT;
+ break;
+
+ case UDF_ELR_EL1:
+ GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN,
+ expect_exlock ? GCSCR_ELx_EXLOCKEN : 0);
+
+ asm volatile("mrs %0, elr_el1 \n\
+ msr elr_el1, %0"
+ : "=r"(val) :: "memory");
+ break;
+
+ case UDF_ELR_EL2:
+ GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN,
+ expect_exlock ? GCSCR_ELx_EXLOCKEN : 0);
+
+ asm volatile("mrs %0, elr_el2 \n\
+ msr elr_el2, %0"
+ : "=r"(val) :: "memory");
+ break;
+
+ case UDF_SPSR_EL1:
+ GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN,
+ expect_exlock ? GCSCR_ELx_EXLOCKEN : 0);
+
+ asm volatile("mrs %0, spsr_el1 \n\
+ msr spsr_el1, %0"
+ : "=r"(val) :: "memory");
+ break;
+
+ case UDF_SPSR_EL2:
+ GUEST_ASSERT_EQ(read_sysreg_s(SYS_GCSCR_EL2) & GCSCR_ELx_EXLOCKEN,
+ expect_exlock ? GCSCR_ELx_EXLOCKEN : 0);
+
+ asm volatile("mrs %0, spsr_el2 \n\
+ msr spsr_el2, %0"
+ : "=r"(val) :: "memory");
+ break;
+
+ default:
+ GUEST_FAIL("Invalid udf_mode");
+ }
+}
+
+static void guest_illegal_handler(struct ex_regs *regs)
+{
+ u64 esr = read_sysreg(esr_el2);
+
+ illegal_count++;
+ illegal_pstate = regs->pstate;
+
+ GUEST_ASSERT_EQ(ESR_ELx_EC(esr), ESR_ELx_EC_ILL);
+ GUEST_ASSERT(esr & ESR_ELx_IL);
+ GUEST_ASSERT(regs->pstate & PSTATE_IL_BIT);
+ GUEST_ASSERT(!(regs->pstate & PSR_EXLOCK_BIT));
+ GUEST_ASSERT_EQ(regs->pc, expected_illegal_pc);
+
+ /* Disable exception locking so we can repair the return state. */
+ write_sysreg_s(0, SYS_GCSCR_EL2);
+ isb();
+
+ /* We need our own EXLOCK clearing so we can repair the original */
+ udf_mode = UDF_REPAIR;
+ asm volatile("udf #0" ::: "memory");
+
+ /* Disable EXLOCK for the generating context, making things legal */
+ regs->pstate &= ~(PSTATE_IL_BIT | PSR_EXLOCK_BIT);
+}
+
+static void guest_gcs_handler(struct ex_regs *regs)
+{
+ u64 esr = read_sysreg(esr_el2);
+
+ gcs_count++;
+
+ GUEST_ASSERT_EQ(ESR_ELx_EC(esr), ESR_ELx_EC_GCS);
+ GUEST_ASSERT_EQ(FIELD_GET(ESR_ELx_ExType_MASK, esr),
+ ESR_ELx_ExType_EXLOCK);
+
+ /* Disabling EXLOCKEN makes sysreg writes legal. */
+ write_sysreg_s(0, SYS_GCSCR_EL2);
+ isb();
+}
+
+static noinline void test_exception(bool hvc, bool enable_exlock)
+{
+ unsigned int initial_undef = undef_count;
+ unsigned int initial_hvc = hvc_count;
+ unsigned int initial_illegal = illegal_count;
+ unsigned int initial_gcs = gcs_count;
+
+ expect_exlock = enable_exlock;
+ write_sysreg_s(enable_exlock ? GCSCR_ELx_EXLOCKEN : 0,
+ SYS_GCSCR_EL2);
+ isb();
+
+ udf_mode = UDF_COLLECT;
+ take_exception(hvc);
+
+ /* Leave GCS disabled even if a broken ERET skipped the ILL handler. */
+ write_sysreg_s(0, SYS_GCSCR_EL2);
+ isb();
+
+ /* The nested exceptions are always UDFs */
+ GUEST_ASSERT_EQ(hvc_count, initial_hvc + hvc);
+ GUEST_ASSERT_EQ(undef_count, initial_undef + !hvc + 1 + enable_exlock);
+ GUEST_ASSERT_EQ(illegal_count, initial_illegal + enable_exlock);
+ GUEST_ASSERT_EQ(!!(handler_pstate & PSR_EXLOCK_BIT), enable_exlock);
+ if (enable_exlock)
+ GUEST_ASSERT(illegal_pstate & PSTATE_IL_BIT);
+ GUEST_ASSERT_EQ(initial_gcs, gcs_count);
+}
+
+static noinline void test_sysreg(bool hvc, enum udf_mode reg,
+ bool enable_exlock)
+{
+ unsigned int initial_undef = undef_count;
+ unsigned int initial_hvc = hvc_count;
+ unsigned int initial_gcs = gcs_count;
+
+ expect_exlock = enable_exlock;
+
+ write_sysreg_s(enable_exlock ? GCSCR_ELx_EXLOCKEN : 0,
+ SYS_GCSCR_EL2);
+ isb();
+
+ udf_mode = reg;
+ take_exception(hvc);
+
+ /* Ensure EXLOCK is disabled */
+ write_sysreg_s(0, SYS_GCSCR_EL2);
+ isb();
+
+ GUEST_ASSERT_EQ(gcs_count, initial_gcs + expect_exlock);
+ GUEST_ASSERT_EQ(undef_count, initial_undef + !hvc);
+ GUEST_ASSERT_EQ(hvc_count, initial_hvc + hvc);
+}
+
+static void test_exceptions(bool hvc)
+{
+ /*
+ * Try straightforwardly generating an exception without and
+ * with EXLOCK.
+ */
+ test_exception(hvc, false);
+ test_sysreg(hvc, UDF_ELR_EL1, false);
+ test_sysreg(hvc, UDF_ELR_EL2, false);
+ test_sysreg(hvc, UDF_SPSR_EL1, false);
+ test_sysreg(hvc, UDF_SPSR_EL2, false);
+
+ test_exception(hvc, true);
+ test_sysreg(hvc, UDF_ELR_EL1, true);
+ test_sysreg(hvc, UDF_ELR_EL2, true);
+ test_sysreg(hvc, UDF_SPSR_EL1, true);
+ test_sysreg(hvc, UDF_SPSR_EL2, true);
+}
+
+static void guest_code(void)
+{
+ GUEST_ASSERT_EQ(get_current_el(), 2);
+
+ /* Exceptions taken to vEL2 by the CPU */
+ test_exceptions(false);
+
+ /* Exceptions injected into vEL2 by KVM */
+ test_exceptions(true);
+
+ GUEST_DONE();
+}
+
+static void test_exception_from_same_el(void)
+{
+ struct kvm_vcpu_init init;
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+ struct ucall uc;
+
+ 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);
+ require_guest_gcs(vcpu);
+
+ vm_init_descriptor_tables(vm);
+ vcpu_init_descriptor_tables(vcpu);
+ vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
+ ESR_ELx_EC_UNKNOWN, guest_exception_handler);
+ vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
+ ESR_ELx_EC_HVC64, guest_exception_handler);
+ vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
+ ESR_ELx_EC_ILL, guest_illegal_handler);
+ vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
+ ESR_ELx_EC_GCS, guest_gcs_handler);
+ kvm_arch_vm_finalize_vcpus(vm);
+
+ vcpu_run(vcpu);
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_DONE:
+ break;
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ break;
+ default:
+ TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
+ }
+
+ kvm_vm_free(vm);
+}
+
+/*
+ * The host injects an external abort on an MMIO exit. When the
+ * exception is taken to the EL it came from PSTATE.EXLOCK should be set
+ * to the EXLOCKEN for that EL, when it is taken from a lower EL it
+ * should be cleared. Either way the old value should be preserved in
+ * the SPSR. Where only one of the ELs is involved the EXLOCKEN for
+ * the other is given the opposite value in order to check that the
+ * right one is used.
+ */
+static const struct inject_test {
+ const char *name;
+ u64 pstate;
+ bool tge;
+ u64 gcscr_el1;
+ u64 gcscr_el2;
+ unsigned int target_mode;
+ bool exlock;
+} inject_tests[] = {
+ {
+ .name = "EL2h to EL2 with EXLOCKEN",
+ .pstate = PSR_MODE_EL2h,
+ .tge = true,
+ .gcscr_el2 = GCSCR_ELx_EXLOCKEN,
+ .target_mode = PSR_MODE_EL2h,
+ .exlock = true,
+ },
+ {
+ .name = "EL2h to EL2 without EXLOCKEN",
+ .pstate = PSR_MODE_EL2h | PSR_EXLOCK_BIT,
+ .tge = true,
+ .gcscr_el1 = GCSCR_ELx_EXLOCKEN,
+ .target_mode = PSR_MODE_EL2h,
+ .exlock = false,
+ },
+ {
+ .name = "EL2t to EL2 with EXLOCKEN",
+ .pstate = PSR_MODE_EL2t,
+ .tge = true,
+ .gcscr_el2 = GCSCR_ELx_EXLOCKEN,
+ .target_mode = PSR_MODE_EL2h,
+ .exlock = true,
+ },
+ {
+ .name = "EL1h to EL1 with EXLOCKEN",
+ .pstate = PSR_MODE_EL1h,
+ .gcscr_el1 = GCSCR_ELx_EXLOCKEN,
+ .target_mode = PSR_MODE_EL1h,
+ .exlock = true,
+ },
+ {
+ .name = "EL1h to EL1 without EXLOCKEN",
+ .pstate = PSR_MODE_EL1h | PSR_EXLOCK_BIT,
+ .gcscr_el2 = GCSCR_ELx_EXLOCKEN,
+ .target_mode = PSR_MODE_EL1h,
+ .exlock = false,
+ },
+ {
+ .name = "EL0 to EL1",
+ .pstate = PSR_MODE_EL0t | PSR_EXLOCK_BIT,
+ .gcscr_el1 = GCSCR_ELx_EXLOCKEN,
+ .gcscr_el2 = GCSCR_ELx_EXLOCKEN,
+ .target_mode = PSR_MODE_EL1h,
+ .exlock = false,
+ },
+ {
+ .name = "EL0 to EL2",
+ .pstate = PSR_MODE_EL0t | PSR_EXLOCK_BIT,
+ .tge = true,
+ .gcscr_el1 = GCSCR_ELx_EXLOCKEN,
+ .gcscr_el2 = GCSCR_ELx_EXLOCKEN,
+ .target_mode = PSR_MODE_EL2h,
+ .exlock = false,
+ },
+};
+
+/* ldr x0, [x0] runs at any EL without a stack or BTI landing pad. */
+static const __le32 guest_mmio_read = cpu_to_le32(0xf9400000);
+
+static void test_injected_exception(const struct inject_test *test)
+{
+ struct kvm_vcpu_events events = {};
+ struct kvm_vcpu_init init;
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+ u64 hcr, pstate, spsr;
+ gpa_t fault_pc, mmio_gpa;
+
+ vm = vm_create(1);
+ kvm_get_default_vcpu_target(vm, &init);
+ init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2);
+
+ /* Leave stage 1 disabled so every source EL can run the load by IPA. */
+ vcpu = __vm_vcpu_add(vm, 0);
+ vcpu_ioctl(vcpu, KVM_ARM_VCPU_INIT, &init);
+ require_guest_gcs(vcpu);
+ kvm_arch_vm_finalize_vcpus(vm);
+
+ /*
+ * With TGE set exceptions from EL0 go to vEL2, without it
+ * exceptions from EL0 and EL1 go to vEL1.
+ */
+ hcr = HCR_EL2_RW | HCR_EL2_E2H;
+ if (test->tge)
+ hcr |= HCR_EL2_TGE;
+ vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2), hcr);
+
+ fault_pc = addr_gva2gpa(vm, (gva_t)&guest_mmio_read);
+ vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pc), fault_pc);
+
+ /* Use the unbacked ucall page, past the address reserved for ucalls. */
+ mmio_gpa = vm->ucall_mmio_addr + sizeof(u64);
+ vcpu_args_set(vcpu, 1, mmio_gpa);
+
+ vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_GCSCR_EL1), test->gcscr_el1);
+ vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_GCSCR_EL2), test->gcscr_el2);
+ vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pstate), test->pstate);
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_MMIO);
+ TEST_ASSERT_EQ(vcpu->run->mmio.phys_addr, mmio_gpa);
+ TEST_ASSERT_EQ(vcpu->run->mmio.len, sizeof(u64));
+ TEST_ASSERT(!vcpu->run->mmio.is_write, "%s: expected an MMIO read", test->name);
+ TEST_ASSERT_EQ(vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pc)), fault_pc);
+ pstate = vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pstate));
+ TEST_ASSERT((pstate & (PSR_MODE_MASK | PSR_EXLOCK_BIT)) == test->pstate,
+ "%s: unexpected PSTATE %#lx at MMIO exit", test->name, pstate);
+
+ events.exception.ext_dabt_pending = true;
+ vcpu_events_set(vcpu, &events);
+
+ pstate = vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pstate));
+ TEST_ASSERT((pstate & PSR_MODE_MASK) == test->target_mode,
+ "%s: exception taken to mode %#x, expected %#x",
+ test->name, (unsigned int)(pstate & PSR_MODE_MASK),
+ test->target_mode);
+ TEST_ASSERT(!!(pstate & PSR_EXLOCK_BIT) == test->exlock,
+ "%s: PSTATE.EXLOCK %s by the exception",
+ test->name, test->exlock ? "not set" : "not cleared");
+
+ if (test->target_mode == PSR_MODE_EL2h)
+ spsr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_SPSR_EL2));
+ else
+ spsr = vcpu_get_reg(vcpu, ARM64_CORE_REG(spsr[KVM_SPSR_EL1]));
+ TEST_ASSERT((spsr & PSR_MODE_MASK) == (test->pstate & PSR_MODE_MASK),
+ "%s: source mode not preserved in SPSR", test->name);
+ TEST_ASSERT((spsr & PSR_EXLOCK_BIT) == (test->pstate & PSR_EXLOCK_BIT),
+ "%s: PSTATE.EXLOCK not preserved in SPSR", test->name);
+
+ kvm_vm_free(vm);
+}
+
+int main(void)
+{
+ int i;
+
+ TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_EL2));
+ test_exception_from_same_el();
+ TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_INJECT_EXT_DABT));
+ for (i = 0; i < ARRAY_SIZE(inject_tests); i++)
+ test_injected_exception(&inject_tests[i]);
+
+ return 0;
+}
--
2.47.3
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v22 03/16] KVM: arm64: Manage GCS access and registers for guests
2026-11-30 16:35 ` [PATCH v22 03/16] KVM: arm64: Manage GCS access and registers for guests Mark Brown
@ 2026-11-30 16:49 ` Leonardo Bras
2026-11-30 16:59 ` Mark Brown
0 siblings, 1 reply; 19+ messages in thread
From: Leonardo Bras @ 2026-11-30 16:49 UTC (permalink / raw)
To: Mark Brown
Cc: Leonardo Bras, Catalin Marinas, Will Deacon, Marc Zyngier,
Joey Gouly, Suzuki K Poulose, Shuah Khan, Oliver Upton,
Fuad Tabba, Peter Maydell, Wei-Lin Chang, Yao Yuan,
linux-arm-kernel, linux-doc, kvmarm, linux-kselftest,
linux-kernel
On Mon, Nov 30, 2026 at 04:35:44PM +0000, Mark Brown wrote:
> 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_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.
>
> Signed-off-by: Mark Brown <broonie@kernel.org>
As per [1], feel free to add my R-b.
Thx,
Leo
[1]https://lore.kernel.org/all/aphSYqKrgUq5Qycu@LeoBrasDK/
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v22 03/16] KVM: arm64: Manage GCS access and registers for guests
2026-11-30 16:49 ` Leonardo Bras
@ 2026-11-30 16:59 ` Mark Brown
0 siblings, 0 replies; 19+ messages in thread
From: Mark Brown @ 2026-11-30 16:59 UTC (permalink / raw)
To: Leonardo Bras
Cc: Catalin Marinas, Will Deacon, Marc Zyngier, Joey Gouly,
Suzuki K Poulose, Shuah Khan, Oliver Upton, Fuad Tabba,
Peter Maydell, Wei-Lin Chang, Yao Yuan, linux-arm-kernel,
linux-doc, kvmarm, linux-kselftest, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 505 bytes --]
On Mon, Nov 30, 2026 at 04:49:24PM +0000, Leonardo Bras wrote:
> On Mon, Nov 30, 2026 at 04:35:44PM +0000, Mark Brown wrote:
> > 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.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2026-11-30 17:00 UTC | newest]
Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 16:35 [PATCH v22 00/16] KVM: arm64: Provide guest support for GCS Mark Brown
2026-11-30 16:35 ` [PATCH v22 01/16] arm64/gcs: Ensure FGTs for EL1 GCS instructions are disabled Mark Brown
2026-11-30 16:35 ` [PATCH v22 02/16] KVM: arm64: Drop the SPSR_EL1 and ELR_EL1 trap accessors Mark Brown
2026-11-30 16:35 ` [PATCH v22 03/16] KVM: arm64: Manage GCS access and registers for guests Mark Brown
2026-11-30 16:49 ` Leonardo Bras
2026-11-30 16:59 ` Mark Brown
2026-11-30 16:35 ` [PATCH v22 04/16] KVM: arm64: Ensure GCS memory effects are visible Mark Brown
2026-11-30 16:35 ` [PATCH v22 05/16] KVM: arm64: Set PSTATE.EXLOCK when entering an exception Mark Brown
2026-11-30 16:35 ` [PATCH v22 06/16] KVM: arm64: Validate GCS exception lock when emulating ERET Mark Brown
2026-11-30 16:35 ` [PATCH v22 07/16] KVM: arm64: Forward GCS exceptions to nested guests Mark Brown
2026-11-30 16:35 ` [PATCH v22 08/16] KVM: arm64: Preserve ISS2.GCS when injecting a Data Abort Mark Brown
2026-11-30 16:35 ` [PATCH v22 09/16] KVM: arm64: Fully implement GCS in the stage-1 walker Mark Brown
2026-11-30 16:35 ` [PATCH v22 10/16] KVM: arm64: Allow GCS to be enabled for guests Mark Brown
2026-11-30 16:35 ` [PATCH v22 11/16] KVM: selftests: arm64: Check invalid feature combination handling Mark Brown
2026-11-30 16:35 ` [PATCH v22 12/16] KVM: selftests: arm64: Add GCS registers to get-reg-list Mark Brown
2026-11-30 16:35 ` [PATCH v22 13/16] KVM: selftests: arm64: Add GCS to set_id_regs Mark Brown
2026-11-30 16:35 ` [PATCH v22 14/16] KVM: selftests: arm64: Only restore SPSR_EL1 and ELR_EL1 if they change Mark Brown
2026-11-30 16:35 ` [PATCH v22 15/16] tools: Synchronise the kernel esr.h Mark Brown
2026-11-30 16:35 ` [PATCH v22 16/16] KVM: selftests: arm64: Add GCS EXLOCK exception emulation test Mark Brown
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®