Skip to contentSkip to conversation

KVM: arm64 on s390 System Register Handling

PATCH Superseded Steffen Eiden 4 participants 53 messages Aug 31 – Sep 2

Subsystems (6)
KVM arm64 s390 KVM arm64 +2 more subsystems

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Steffen Eiden

author reviewer
#0 lore raw

v2 cover KVM: arm64 on s390 System Register Handling

Add system register handling for KVM/arm64 on s390. Share KVM/arm64 code, introduce ARM guest management functions for s390 hosts, and implement host sysreg & exception handling.

This is a complete rework of v1 using sharing markers and not moving code to a shared location.

Changes in detail:

arm64:

Refactor the id register fields in arch_vcpu into a dedicated structure such that it can be reused by s390. Refactor detection of the number of sysregs. Mark sysreg related code as shared.

s390:

Add s390 instruction support for ARM guest management: easr/sasr for system register access, QAAF for feature queries, and ptff extensions for guest time handling.

Implement complete sysreg handling for s390 including feature sanitisation, register enumeration and access, exception injection, and finalized page fault handling.

The series builds upon the foundation established in the first series and requires the first series v7[1] as base. A branch at kernel.org is available [2].

Some in-between steps will trigger compile warnings (mostly unused warnings in shared code) but of course always compile. After the last commit of this series will trigger no compiler warnings.

Steffen

[1] https://lore.kernel.org/all/[email protected]/
[2] https://git.kernel.org/pub/scm/linux/kernel/git/kvms390/linux.git/log/?h=sae

Steffen Eiden (20):
KVM: arm64: Refactor idreg caching into dedicated structure
KVM: arm64: Extract number of sys_reg_desc into a constant
arm64: sysreg: Define OSLSR_EL1_OSLK_MASK
arm64: Share more arm64 headers with s390
KVM: s390: arm64: Prepare for sharing more arm64 code
KVM: arm64: Prepare sys_regs.c for sharing with s390
KVM: arm64: Share more arm64 code with s390
s390: tools: Allow sharing arm64/kvm headers
s390: Introduce read/write ARM sysreg instructions
s390: Add functions to query arm guest time
KVM: s390: arm64: Query Available Arm features
KVM: s390: arm64: Implement feature sanitisation
KVM: s390: arm64: Implement arm sysreg managing infrastructure
KVM: s390: arm64: Integrate sysreg into the host
KVM: s390: arm64: Use QAAF init save area
KVM: s390: arm64: Implement exception injection
KVM: s390: arm64: Finalize page fault handling
KVM: s390: arm64: Implement SVE for arm guests
KVM: s390: arm64: Promote PTRAUTH capability
s390: Report AEF features to sysfs

 arch/arm64/include/asm/cache.h               |   3 +
 arch/arm64/include/asm/cputype.h             |   3 +
 arch/arm64/include/asm/kvm_emulate.h         |   5 +-
 arch/arm64/include/asm/kvm_host.h            |  68 +-
 arch/arm64/include/asm/spectre.h             |   7 +
 arch/arm64/include/asm/sysreg.h              |   1 +
 arch/arm64/kvm/arm.c                         |   3 +
 arch/arm64/kvm/config.c                      |   2 +-
 arch/arm64/kvm/guest.c                       |  11 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c               |   7 +-
 arch/arm64/kvm/hyp/nvhe/sys_regs.c           |   4 +-
 arch/arm64/kvm/sys_regs.c                    | 164 ++--
 arch/arm64/kvm/sys_regs.h                    |   1 +
 arch/s390/include/arm64/kvm_hyp.h            |  18 +
 arch/s390/include/arm64/kvm_nested.h         |   5 +
 arch/s390/include/arm64/sys_regs.h           |   9 +
 arch/s390/include/asm/aef.h                  |  28 +
 arch/s390/include/asm/kvm_host_arm64.h       | 237 +++++-
 arch/s390/include/asm/kvm_host_arm64_types.h | 111 +++
 arch/s390/include/asm/sae-asm.h              |  48 ++
 arch/s390/include/asm/sae.h                  |  83 ++
 arch/s390/include/asm/timex.h                |  49 ++
 arch/s390/kernel/Makefile                    |   2 +-
 arch/s390/kernel/aef.c                       | 187 +++++
 arch/s390/kernel/dis.c                       |   1 +
 arch/s390/kvm/arm64/Makefile                 |   5 +
 arch/s390/kvm/arm64/Makefile.gen             |   1 +
 arch/s390/kvm/arm64/arm.c                    |  73 +-
 arch/s390/kvm/arm64/exception.c              | 105 +++
 arch/s390/kvm/arm64/feature.c                | 190 +++++
 arch/s390/kvm/arm64/feature.h                | 102 +++
 arch/s390/kvm/arm64/guest.c                  | 222 +++++-
 arch/s390/kvm/arm64/handle_exit.c            |   1 +
 arch/s390/kvm/arm64/inject_fault.c           |  73 +-
 arch/s390/kvm/arm64/mmu.c                    |  68 +-
 arch/s390/kvm/arm64/qaaf.c                   |  74 ++
 arch/s390/kvm/arm64/qaaf.h                   |  11 +
 arch/s390/kvm/arm64/reset.c                  |  69 +-
 arch/s390/kvm/arm64/spectre.c                |  14 +
 arch/s390/kvm/arm64/sys_regs.c               | 750 +++++++++++++++++++
 arch/s390/kvm/arm64/trace.h                  |  51 ++
 arch/s390/tools/Makefile.arm64h              |  11 +-
 arch/s390/tools/opcodes.txt                  |   3 +
 43 files changed, 2724 insertions(+), 156 deletions(-)
 create mode 100644 arch/s390/include/arm64/kvm_hyp.h
 create mode 100644 arch/s390/include/arm64/sys_regs.h
 create mode 100644 arch/s390/include/asm/aef.h
 create mode 100644 arch/s390/include/asm/sae-asm.h
 create mode 100644 arch/s390/kernel/aef.c
 create mode 100644 arch/s390/kvm/arm64/exception.c
 create mode 100644 arch/s390/kvm/arm64/feature.c
 create mode 100644 arch/s390/kvm/arm64/feature.h
 create mode 100644 arch/s390/kvm/arm64/qaaf.c
 create mode 100644 arch/s390/kvm/arm64/qaaf.h
 create mode 100644 arch/s390/kvm/arm64/spectre.c
 create mode 100644 arch/s390/kvm/arm64/sys_regs.c

base-commit: ab80750cee6e09e0036ac43f257059122a05a71d

Signature · 1 line
2.53.0
author reviewer
#1 lore raw

PATCH v2 1/20 KVM: arm64: Refactor idreg caching into dedicated structure

Move VM-wide ID register emulation fields from struct kvm_arch into a new struct kvm_vm_id_regs to prepare future sharing of these fields and functions using them. Update all users to use the new structure. No functional changes.

---
 arch/arm64/include/asm/kvm_host.h  | 50 ++++++++++++++++--------------
 arch/arm64/kvm/config.c            |  2 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c     |  7 +++--
 arch/arm64/kvm/hyp/nvhe/sys_regs.c |  4 +--
 arch/arm64/kvm/sys_regs.c          |  2 +-
 5 files changed, 35 insertions(+), 30 deletions(-)
arch/arm64/include/asm/kvm_host.h +27 -23
index a8ddce3921b8..f9e90ef9ee4b 100644
@@ -349,6 +349,25 @@ enum fgt_group_id {
 	__NR_FGT_GROUP_IDS__
 };
 
+struct kvm_vm_id_regs {
+	/*
+	 * Emulated CPU ID registers per VM
+	 * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
+	 * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
+	 *
+	 * These emulated idregs are VM-wide, but accessed from the context of a vCPU.
+	 * Atomic access to multiple idregs are guarded by kvm_arch.config_lock.
+	 */
+#define IDREG_IDX(id)		(((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
+#define KVM_ARM_ID_REG_NUM	(IDREG_IDX(sys_reg(3, 0, 0, 7, 7)) + 1)
+	u64 ftr_reg[KVM_ARM_ID_REG_NUM];
+
+	u64 midr_el1;
+	u64 revidr_el1;
+	u64 aidr_el1;
+	u64 ctr_el0;
+};
+
 struct kvm_arch {
 	struct kvm_s2_mmu mmu;
 
@@ -408,22 +427,7 @@ struct kvm_arch {
 	struct kvm_smccc_features smccc_feat;
 	struct maple_tree smccc_filter;
 
-	/*
-	 * Emulated CPU ID registers per VM
-	 * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
-	 * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
-	 *
-	 * These emulated idregs are VM-wide, but accessed from the context of a vCPU.
-	 * Atomic access to multiple idregs are guarded by kvm_arch.config_lock.
-	 */
-#define IDREG_IDX(id)		(((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
-#define KVM_ARM_ID_REG_NUM	(IDREG_IDX(sys_reg(3, 0, 0, 7, 7)) + 1)
-	u64 id_regs[KVM_ARM_ID_REG_NUM];
-
-	u64 midr_el1;
-	u64 revidr_el1;
-	u64 aidr_el1;
-	u64 ctr_el0;
+	struct kvm_vm_id_regs id_regs;
 
 	/* Masks for VNCR-backed and general EL2 sysregs */
 	struct kvm_sysreg_masks	*sysreg_masks;
@@ -1563,19 +1567,19 @@ static inline void kvm_hyp_reserve(void) { }
 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
 
-static inline u64 *__vm_id_reg(struct kvm_arch *ka, u32 reg)
+static inline u64 *__vm_id_reg(struct kvm_vm_id_regs *id_regs, u32 reg)
 {
 	switch (reg) {
 	case sys_reg(3, 0, 0, 1, 0) ... sys_reg(3, 0, 0, 7, 7):
-		return &ka->id_regs[IDREG_IDX(reg)];
+		return &id_regs->ftr_reg[IDREG_IDX(reg)];
 	case SYS_CTR_EL0:
-		return &ka->ctr_el0;
+		return &id_regs->ctr_el0;
 	case SYS_MIDR_EL1:
-		return &ka->midr_el1;
+		return &id_regs->midr_el1;
 	case SYS_REVIDR_EL1:
-		return &ka->revidr_el1;
+		return &id_regs->revidr_el1;
 	case SYS_AIDR_EL1:
-		return &ka->aidr_el1;
+		return &id_regs->aidr_el1;
 	default:
 		WARN_ON_ONCE(1);
 		return NULL;
@@ -1583,7 +1587,7 @@ static inline u64 *__vm_id_reg(struct kvm_arch *ka, u32 reg)
 }
 
 #define kvm_read_vm_id_reg(kvm, reg)					\
-	({ u64 __val = *__vm_id_reg(&(kvm)->arch, reg); __val; })
+	({ u64 __val = *__vm_id_reg(&(kvm)->arch.id_regs, reg); __val; })
 
 void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
 
arch/arm64/kvm/config.c +1 -1
index 1053676551af..de56656237f4 100644
@@ -1410,7 +1410,7 @@ void __init check_feature_map(void)
 
 static bool idreg_feat_match(struct kvm *kvm, const struct reg_bits_to_feat_map *map)
 {
-	u64 regval = kvm->arch.id_regs[map->regidx];
+	u64 regval = kvm->arch.id_regs.ftr_reg[map->regidx];
 	u64 regfld = (regval >> map->shift) & GENMASK(map->width - 1, 0);
 
 	if (map->sign) {
arch/arm64/kvm/hyp/nvhe/pkvm.c +4 -3
index 459bd9eb7e4b..bb11561de05e 100644
@@ -343,7 +343,7 @@ static void pkvm_init_features_from_host(struct pkvm_hyp_vm *hyp_vm, const struc
 	DECLARE_BITMAP(allowed_features, KVM_VCPU_MAX_FEATURES);
 
 	/* CTR_EL0 is always under host control, even for protected VMs. */
-	hyp_vm->kvm.arch.ctr_el0 = host_kvm->arch.ctr_el0;
+	hyp_vm->kvm.arch.id_regs.ctr_el0 = host_kvm->arch.id_regs.ctr_el0;
 
 	/* Preserve the vgic model so that GICv3 emulation works */
 	hyp_vm->kvm.arch.vgic.vgic_model = host_kvm->arch.vgic.vgic_model;
@@ -358,7 +358,7 @@ static void pkvm_init_features_from_host(struct pkvm_hyp_vm *hyp_vm, const struc
 			    KVM_VCPU_MAX_FEATURES);
 
 		if (test_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &host_arch_flags))
-			hyp_vm->kvm.arch.midr_el1 = host_kvm->arch.midr_el1;
+			hyp_vm->kvm.arch.id_regs.midr_el1 = host_kvm->arch.id_regs.midr_el1;
 
 		return;
 	}
@@ -492,7 +492,8 @@ static int vm_copy_id_regs(struct pkvm_hyp_vcpu *hyp_vcpu)
 	if (test_and_set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags))
 		return 0;
 
-	memcpy(kvm->arch.id_regs, host_kvm->arch.id_regs, sizeof(kvm->arch.id_regs));
+	memcpy(kvm->arch.id_regs.ftr_reg, host_kvm->arch.id_regs.ftr_reg,
+	       sizeof(kvm->arch.id_regs.ftr_reg));
 
 	return 0;
 }
arch/arm64/kvm/hyp/nvhe/sys_regs.c +2 -2
index 8758c6801776..3e70b71d8f4f 100644
@@ -293,7 +293,7 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu,
 		return 0;
 
 	if (reg >= sys_reg(3, 0, 0, 1, 0) && reg <= sys_reg(3, 0, 0, 7, 7))
-		return kvm->arch.id_regs[IDREG_IDX(reg)];
+		return kvm->arch.id_regs.ftr_reg[IDREG_IDX(reg)];
 
 	return 0;
 }
@@ -544,7 +544,7 @@ void kvm_init_pvm_id_regs(struct kvm_vcpu *vcpu)
 	 * for protected VMs.
 	 */
 	for (r = sys_reg(3, 0, 0, 4, 0); r <= sys_reg(3, 0, 0, 7, 7); r += sys_reg(0, 0, 0, 0, 1))
-		ka->id_regs[IDREG_IDX(r)] = pvm_calc_id_reg(vcpu, r);
+		ka->id_regs.ftr_reg[IDREG_IDX(r)] = pvm_calc_id_reg(vcpu, r);
 
 	set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags);
 }
arch/arm64/kvm/sys_regs.c +1 -1
index 44aae52c473d..06dc8ec7a883 100644
@@ -2543,7 +2543,7 @@ static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 
 void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val)
 {
-	u64 *p = __vm_id_reg(&kvm->arch, reg);
+	u64 *p = __vm_id_reg(&kvm->arch.id_regs, reg);
 
 	lockdep_assert_held(&kvm->arch.config_lock);
 
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v2 01/20] KVM: arm64: Refactor idreg caching into dedicated structure

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

Move VM-wide ID register emulation fields from struct kvm_arch into a new struct kvm_vm_id_regs to prepare future sharing of these fields and functions using them. Update all users to use the new structure. No functional changes.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#3 lore raw

PATCH v2 2/20 KVM: arm64: Extract number of sys_reg_desc into a constant

In reply to Steffen Eiden

Define the constant num_sys_reg_descs which stores the number of elements in the sys_reg_descs array. Replace all occurrences of ARRAY_SIZE(sys_reg_descs) with the new constant.

This refactoring eases the integration of handling system registers for arm on s390.

---
 arch/arm64/kvm/sys_regs.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
arch/arm64/kvm/sys_regs.c +14 -12
index 06dc8ec7a883..7d0047b542f4 100644
@@ -3976,6 +3976,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	EL2_REG(SP_EL2, NULL, reset_unknown, 0),
 };
 
+static const size_t num_sys_reg_descs = ARRAY_SIZE(sys_reg_descs);
+
 static bool handle_at_s1e01(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 			    const struct sys_reg_desc *r)
 {
@@ -5187,7 +5189,7 @@ static bool emulate_sys_reg(struct kvm_vcpu *vcpu,
 {
 	const struct sys_reg_desc *r;
 
-	r = find_reg(params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
+	r = find_reg(params, sys_reg_descs, num_sys_reg_descs);
 	if (likely(r)) {
 		perform_access(vcpu, params, r);
 		return true;
@@ -5205,7 +5207,7 @@ static const struct sys_reg_desc *idregs_debug_find(struct kvm *kvm, loff_t pos)
 {
 	unsigned long i, idreg_idx = 0;
 
-	for (i = 0; i < ARRAY_SIZE(sys_reg_descs); i++) {
+	for (i = 0; i < num_sys_reg_descs; i++) {
 		const struct sys_reg_desc *r = &sys_reg_descs[i];
 
 		if (!is_vm_ftr_id_reg(reg_to_encoding(r)))
@@ -5268,7 +5270,7 @@ static const struct sys_reg_desc *sr_resx_find(struct kvm *kvm, loff_t pos)
 {
 	unsigned long i, sr_idx = 0;
 
-	for (i = 0; i < ARRAY_SIZE(sys_reg_descs); i++) {
+	for (i = 0; i < num_sys_reg_descs; i++) {
 		const struct sys_reg_desc *r = &sys_reg_descs[i];
 
 		if (r->reg < __SANITISED_REG_START__)
@@ -5370,7 +5372,7 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
 	struct kvm *kvm = vcpu->kvm;
 	unsigned long i;
 
-	for (i = 0; i < ARRAY_SIZE(sys_reg_descs); i++) {
+	for (i = 0; i < num_sys_reg_descs; i++) {
 		const struct sys_reg_desc *r = &sys_reg_descs[i];
 
 		if (!r->reset)
@@ -5594,8 +5596,8 @@ int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg
 	if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
 		return demux_c15_get(vcpu, reg->id, uaddr);
 
-	return kvm_sys_reg_get_user(vcpu, reg,
-				    sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
+	return kvm_sys_reg_get_user(vcpu, reg, sys_reg_descs,
+				    num_sys_reg_descs);
 }
 
 int kvm_sys_reg_set_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
@@ -5634,8 +5636,8 @@ int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg
 	if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
 		return demux_c15_set(vcpu, reg->id, uaddr);
 
-	return kvm_sys_reg_set_user(vcpu, reg,
-				    sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
+	return kvm_sys_reg_set_user(vcpu, reg, sys_reg_descs,
+				    num_sys_reg_descs);
 }
 
 static unsigned int num_demux_regs(void)
@@ -5723,7 +5725,7 @@ static int walk_sys_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
 	int err;
 
 	i2 = sys_reg_descs;
-	end2 = sys_reg_descs + ARRAY_SIZE(sys_reg_descs);
+	end2 = sys_reg_descs + num_sys_reg_descs;
 
 	while (i2 != end2) {
 		err = walk_one_sys_reg(vcpu, i2++, &uind, &total);
@@ -5772,7 +5774,7 @@ int kvm_vm_ioctl_get_reg_writable_masks(struct kvm *kvm, struct reg_mask_range *
 	if (clear_user(masks, KVM_ARM_FEATURE_ID_RANGE_SIZE * sizeof(__u64)))
 		return -EFAULT;
 
-	for (int i = 0; i < ARRAY_SIZE(sys_reg_descs); i++) {
+	for (int i = 0; i < num_sys_reg_descs; i++) {
 		const struct sys_reg_desc *reg = &sys_reg_descs[i];
 		u32 encoding = reg_to_encoding(reg);
 		u64 val;
@@ -5939,7 +5941,7 @@ int __init kvm_sys_reg_table_init(void)
 	int ret = 0;
 
 	/* Make sure tables are unique and in order. */
-	valid &= check_sysreg_table(sys_reg_descs, ARRAY_SIZE(sys_reg_descs), true);
+	valid &= check_sysreg_table(sys_reg_descs, num_sys_reg_descs, true);
 	valid &= check_sysreg_table(cp14_regs, ARRAY_SIZE(cp14_regs), false);
 	valid &= check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs), false);
 	valid &= check_sysreg_table(cp15_regs, ARRAY_SIZE(cp15_regs), false);
@@ -5958,7 +5960,7 @@ int __init kvm_sys_reg_table_init(void)
 
 	check_feature_map();
 
-	for (i = 0; !ret && i < ARRAY_SIZE(sys_reg_descs); i++)
+	for (i = 0; !ret && i < num_sys_reg_descs; i++)
 		ret = populate_sysreg_config(sys_reg_descs + i, i);
 
 	for (i = 0; !ret && i < ARRAY_SIZE(sys_insn_descs); i++)
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH v2 02/20] KVM: arm64: Extract number of sys_reg_desc into a constant

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

Define the constant num_sys_reg_descs which stores the number of elements in the sys_reg_descs array. Replace all occurrences of ARRAY_SIZE(sys_reg_descs) with the new constant.

This refactoring eases the integration of handling system registers for arm on s390.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#5 lore raw

Re: [PATCH v2 02/20] KVM: arm64: Extract number of sys_reg_desc into a constant

In reply to Steffen Eiden
Quoted 7 lines · Steffen Eiden

Define the constant num_sys_reg_descs which stores the number of elements in the sys_reg_descs array. Replace all occurrences of ARRAY_SIZE(sys_reg_descs) with the new constant.

This refactoring eases the integration of handling system registers for arm on s390.

How? What is so different in the way you are handling the sysreg array that you need some arbitrary constant that is not the array size? Why is this the only array you care about? Why not the sys_insn_descs array, for example?

It feels like you are papering over some other design issue, and I'd like to understand that.

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#6 lore raw

PATCH v2 3/20 arm64: sysreg: Define OSLSR_EL1_OSLK_MASK

In reply to Steffen Eiden

Define the missing mask for the OLSK field of OSLSR_EL1 which is required to safely write a value to that field.

---
 arch/arm64/include/asm/sysreg.h | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/include/asm/sysreg.h +1 -0
index 1c5c4df260be..5a439853b721 100644
@@ -200,6 +200,7 @@
 #define OSLSR_EL1_OSLM_NI		0
 #define OSLSR_EL1_OSLM_IMPLEMENTED	BIT(3)
 #define OSLSR_EL1_OSLK			BIT(1)
+#define OSLSR_EL1_OSLK_MASK		BIT(1)
 
 #define SYS_OSDLR_EL1			sys_reg(2, 0, 1, 3, 4)
 #define SYS_DBGPRCR_EL1			sys_reg(2, 0, 1, 4, 4)
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCH v2 03/20] arm64: sysreg: Define OSLSR_EL1_OSLK_MASK

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Typo in the commit message: 'OLSK' instead of 'OSLK' --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Typo in the commit message: 'OLSK' instead of 'OSLK'
--

commit 834d9af2283801afa096e24f8b7cc277f61cf317
Author: Steffen Eiden <[email protected]>

arm64: sysreg: Define OSLSR_EL1_OSLK_MASK

This commit adds a missing mask definition for the OSLK field in the OSLSR_EL1 register to allow safe writes to that field.

Quoted 2 lines

Define the missing mask for the OLSK field of OSLSR_EL1 which is required to safely write a value to that field.

[Severity: Low]
This isn't a bug, but is "OLSK" a typo in the commit message here? The architectural OS Lock Status Register field is named "OSLK", which matches the newly added OSLSR_EL1_OSLK_MASK macro.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer
#8 lore raw

Re: [PATCH v2 03/20] arm64: sysreg: Define OSLSR_EL1_OSLK_MASK

In reply to Steffen Eiden
Quoted 14 lines · Steffen Eiden

Define the missing mask for the OLSK field of OSLSR_EL1 which is required to safely write a value to that field.

---
 arch/arm64/include/asm/sysreg.h | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/include/asm/sysreg.h +0 -0
index 1c5c4df260be..5a439853b721 100644

arch/arm64/include/asm/sysreg.h line 203

Quoted 4 lines
arch/arm64/include/asm/sysreg.h +1 -0
@@ -200,6 +200,7 @@
 #define OSLSR_EL1_OSLM_NI		0
 #define OSLSR_EL1_OSLM_IMPLEMENTED	BIT(3)
 #define OSLSR_EL1_OSLK			BIT(1)
+#define OSLSR_EL1_OSLK_MASK		BIT(1)

Just move the required sysregs to the sysreg file, with the full bit description. This will sort this once and for all.

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#9 lore raw

Re: [PATCH v2 03/20] arm64: sysreg: Define OSLSR_EL1_OSLK_MASK

Quoted 11 lines · Marc Zyngier
Quoted 9 lines · Steffen Eiden

Define the missing mask for the OLSK field of OSLSR_EL1 which is required to safely write a value to that field.

---
 arch/arm64/include/asm/sysreg.h | 1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

arch/arm64/include/asm/sysreg.h line 203

Quoted 9 lines
arch/arm64/include/asm/sysreg.h +1 -0
index 1c5c4df260be..5a439853b721 100644
@@ -200,6 +200,7 @@
 #define OSLSR_EL1_OSLM_NI		0
 #define OSLSR_EL1_OSLM_IMPLEMENTED	BIT(3)
 #define OSLSR_EL1_OSLK			BIT(1)
+#define OSLSR_EL1_OSLK_MASK		BIT(1)

Just move the required sysregs to the sysreg file, with the full bit description. This will sort this once and for all.

You mean to the tools/syreg file to generate the descriptions?

I wanted to do that but OSLSR has a split field (OSLM in bit 3 and 0) which the generation tooling cannot handle. (right?)

Now that I am rethinking this:

I could move the stuff to the tools/sysregs and in addtion define the OSLM field here manually but this time using the generated constansts. probably sth. like:

#define OSLSR_EL1_OSLM_MASK (OSLSR_EL1_OSLM0_MASK | OSLSR_EL1_OSLM3_MASK)
#define OSLSR_EL1_OSLM_NI OSLSR_EL1_OSLM0_NI
#define OSLSR_EL1_OSLM_IMPLEMENTED OSLSR_EL1_OSLM3_IMPLEMENTED

Shall I do this?

Steffen

maintainer
#10 lore raw

Re: [PATCH v2 03/20] arm64: sysreg: Define OSLSR_EL1_OSLK_MASK

Quoted 26 lines · Steffen Eiden
Quoted 11 lines · Marc Zyngier
Quoted 9 lines · Steffen Eiden

Define the missing mask for the OLSK field of OSLSR_EL1 which is required to safely write a value to that field.

---
 arch/arm64/include/asm/sysreg.h | 1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

arch/arm64/include/asm/sysreg.h line 203

Quoted 9 lines
arch/arm64/include/asm/sysreg.h +1 -0
index 1c5c4df260be..5a439853b721 100644
@@ -200,6 +200,7 @@
 #define OSLSR_EL1_OSLM_NI		0
 #define OSLSR_EL1_OSLM_IMPLEMENTED	BIT(3)
 #define OSLSR_EL1_OSLK			BIT(1)
+#define OSLSR_EL1_OSLK_MASK		BIT(1)

Just move the required sysregs to the sysreg file, with the full bit description. This will sort this once and for all.

Quoted 4 lines

You mean to the tools/syreg file to generate the descriptions?

I wanted to do that but OSLSR has a split field (OSLM in bit 3 and 0) which the generation tooling cannot handle. (right?)

Composite fields are nothing new. We have those everywhere (page tables, for a start).

Quoted 7 lines

Now that I am rethinking this:

I could move the stuff to the tools/sysregs and in addtion define the OSLM field here manually but this time using the generated constansts. probably sth. like:

Quoted 5 lines

#define OSLSR_EL1_OSLM_MASK (OSLSR_EL1_OSLM0_MASK | OSLSR_EL1_OSLM3_MASK)
#define OSLSR_EL1_OSLM_NI OSLSR_EL1_OSLM0_NI
#define OSLSR_EL1_OSLM_IMPLEMENTED OSLSR_EL1_OSLM3_IMPLEMENTED

Shall I do this?

Yes. Except that OSLM3 should really be OSLM1, being bit 1 of the OSLM field. And it would make a lot more sense to express NI and IMPLEMENTED in terms of the two bits, just like the JSON describes it:

OSLSR_EL1 [2,0,1,1,4] MRS
# Reg cond: IsFeatureImplemented(FEAT_AA64)
Res0 63:4

UnsignedEnum    3       OSLM
UnsignedEnum    0       OSLM
        0b00    VAL_00
        0b10    VAL_10

EndEnum
UnsignedEnum 2 nTT
EndEnum
Field 1 OSLK

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#11 lore raw

PATCH v2 4/20 arm64: Share more arm64 headers with s390

In reply to Steffen Eiden

Mark more headers as shared or move definitions to an already shared location if the context is similar. Most of them are required to implement system register handling for the host.

---
 arch/arm64/include/asm/cache.h       |  3 +++
 arch/arm64/include/asm/cputype.h     |  3 +++
 arch/arm64/include/asm/kvm_emulate.h |  5 ++++-
 arch/arm64/include/asm/kvm_host.h    | 18 +++++++++++++-----
 arch/arm64/include/asm/spectre.h     |  7 +++++++
 arch/arm64/kvm/sys_regs.h            |  1 +
 6 files changed, 31 insertions(+), 6 deletions(-)
arch/arm64/include/asm/cache.h +3 -0
index 10a7ffadee3d..f9dbc8143dc9 100644
@@ -8,6 +8,7 @@
 #define L1_CACHE_SHIFT		(6)
 #define L1_CACHE_BYTES		(1 << L1_CACHE_SHIFT)
 
+#ifdef ARM64_S390_COMMON
 #define CLIDR_LOUU_SHIFT	27
 #define CLIDR_LOC_SHIFT		24
 #define CLIDR_LOUIS_SHIFT	21
@@ -25,6 +26,8 @@
 /* Ttypen, bits [2(n - 1) + 34 : 2(n - 1) + 33], for n = 1 to 7 */
 #define CLIDR_TTYPE_SHIFT(level)	(2 * ((level) - 1) + CLIDR_EL1_Ttypen_SHIFT)
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * Memory returned by kmalloc() may be used for DMA, so we must make
  * sure that all such allocations are cache aligned. Otherwise,
arch/arm64/include/asm/cputype.h +3 -0
index 1fa29616e586..a5fd2b749b3b 100644
@@ -5,6 +5,7 @@
 #ifndef __ASM_CPUTYPE_H
 #define __ASM_CPUTYPE_H
 
+#ifdef ARM64_S390_COMMON
 #define INVALID_HWID		ULONG_MAX
 
 #define MPIDR_UP_BITMASK	(0x1 << 30)
@@ -259,6 +260,8 @@
 #define MIDR_FUJITSU_ERRATUM_010001_MASK	(~MIDR_CPU_VAR_REV(1, 0))
 #define TCR_CLEAR_FUJITSU_ERRATUM_010001	(TCR_EL1_NFD1 | TCR_EL1_NFD0)
 
+#endif /* ARM64_S390_COMMON */
+
 #ifndef __ASSEMBLER__
 
 #include <asm/sysreg.h>
arch/arm64/include/asm/kvm_emulate.h +4 -1
index 09f04a5686a1..6cd9b244d82e 100644
@@ -23,6 +23,7 @@
 #include <asm/cputype.h>
 #include <asm/virt.h>
 
+#ifdef ARM64_S390_COMMON
 #define CURRENT_EL_SP_EL0_VECTOR	0x0
 #define CURRENT_EL_SP_ELx_VECTOR	0x200
 #define LOWER_EL_AArch64_VECTOR		0x400
@@ -35,6 +36,8 @@ enum exception_type {
 	except_type_serror	= 0x180,
 };
 
+#endif /* ARM64_S390_COMMON */
+
 #define kvm_exception_type_names		\
 	{ except_type_sync,	"SYNC"   },	\
 	{ except_type_irq,	"IRQ"    },	\
@@ -525,13 +528,13 @@ static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu)
 	}
 }
 
+#ifdef ARM64_S390_COMMON
 static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)
 {
 	u64 esr = kvm_vcpu_get_esr(vcpu);
 	return ESR_ELx_SYS64_ISS_RT(esr);
 }
 
-#ifdef ARM64_S390_COMMON
 static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
 {
 	if (kvm_vcpu_abt_iss1tw(vcpu)) {
arch/arm64/include/asm/kvm_host.h +13 -5
index f9e90ef9ee4b..2ca5fbc39f15 100644
@@ -349,6 +349,7 @@ enum fgt_group_id {
 	__NR_FGT_GROUP_IDS__
 };
 
+#ifdef ARM64_S390_COMMON
 struct kvm_vm_id_regs {
 	/*
 	 * Emulated CPU ID registers per VM
@@ -368,6 +369,8 @@ struct kvm_vm_id_regs {
 	u64 ctr_el0;
 };
 
+#endif /* ARM64_S390_COMMON */
+
 struct kvm_arch {
 	struct kvm_s2_mmu mmu;
 
@@ -1240,9 +1243,6 @@ u64 kvm_vcpu_apply_reg_masks(const struct kvm_vcpu *, enum vcpu_sysreg, u64);
 		__v;							\
 	})
 
-u64 vcpu_read_sys_reg(const struct kvm_vcpu *, enum vcpu_sysreg);
-void vcpu_write_sys_reg(struct kvm_vcpu *, u64, enum vcpu_sysreg);
-
 struct kvm_vm_stat {
 	struct kvm_vm_stat_generic generic;
 };
@@ -1322,11 +1322,9 @@ int kvm_handle_cp14_32(struct kvm_vcpu *vcpu);
 int kvm_handle_cp14_64(struct kvm_vcpu *vcpu);
 int kvm_handle_cp15_32(struct kvm_vcpu *vcpu);
 int kvm_handle_cp15_64(struct kvm_vcpu *vcpu);
-int kvm_handle_sys_reg(struct kvm_vcpu *vcpu);
 int kvm_handle_cp10_id(struct kvm_vcpu *vcpu);
 
 void kvm_sys_regs_create_debugfs(struct kvm *kvm);
-void kvm_reset_sys_regs(struct kvm_vcpu *vcpu);
 
 int __init kvm_sys_reg_table_init(void);
 struct sys_reg_desc;
@@ -1337,6 +1335,11 @@ int __init populate_nv_trap_config(void);
 void kvm_calculate_traps(struct kvm_vcpu *vcpu);
 
 #ifdef ARM64_S390_COMMON
+u64 vcpu_read_sys_reg(const struct kvm_vcpu *, enum vcpu_sysreg);
+void vcpu_write_sys_reg(struct kvm_vcpu *, u64, enum vcpu_sysreg);
+int kvm_handle_sys_reg(struct kvm_vcpu *vcpu);
+void kvm_reset_sys_regs(struct kvm_vcpu *vcpu);
+
 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
@@ -1538,8 +1541,10 @@ struct kvm *kvm_arch_alloc_vm(void);
 	(system_supports_32bit_el0() &&				\
 	 !static_branch_unlikely(&arm64_mismatched_32bit_el0))
 
+#ifdef ARM64_S390_COMMON
 #define kvm_vm_has_ran_once(kvm)					\
 	(test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &(kvm)->arch.flags))
+#endif /* ARM64_S390_COMMON */
 
 static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature)
 {
@@ -1567,6 +1572,7 @@ static inline void kvm_hyp_reserve(void) { }
 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
 
+#ifdef ARM64_S390_COMMON
 static inline u64 *__vm_id_reg(struct kvm_vm_id_regs *id_regs, u32 reg)
 {
 	switch (reg) {
@@ -1640,6 +1646,8 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
 	(kvm_cmp_feat(kvm, id, fld, >=, min) &&				\
 	kvm_cmp_feat(kvm, id, fld, <=, max))
 
+#endif /* ARM64_S390_COMMON */
+
 /* Check for a given level of PAuth support */
 #define kvm_has_pauth(k, l)						\
 	({								\
arch/arm64/include/asm/spectre.h +7 -0
index 296ae3420bfd..e1c13797ea7c 100644
@@ -19,6 +19,7 @@
 #include <asm/cpufeature.h>
 #include <asm/virt.h>
 
+#ifdef ARM64_S390_COMMON
 /* Watch out, ordering is important here. */
 enum mitigation_state {
 	SPECTRE_UNAFFECTED,
@@ -26,6 +27,8 @@ enum mitigation_state {
 	SPECTRE_VULNERABLE,
 };
 
+#endif /* ARM64_S390_COMMON */
+
 struct pt_regs;
 struct task_struct;
 
@@ -81,7 +84,9 @@ static __always_inline void arm64_apply_bp_hardening(void)
 		d->fn();
 }
 
+#ifdef ARM64_S390_COMMON
 enum mitigation_state arm64_get_spectre_v2_state(void);
+#endif /* ARM64_S390_COMMON */
 bool has_spectre_v2(const struct arm64_cpu_capabilities *cap, int scope);
 void spectre_v2_enable_mitigation(const struct arm64_cpu_capabilities *__unused);
 
@@ -93,7 +98,9 @@ bool has_spectre_v4(const struct arm64_cpu_capabilities *cap, int scope);
 void spectre_v4_enable_mitigation(const struct arm64_cpu_capabilities *__unused);
 void spectre_v4_enable_task_mitigation(struct task_struct *tsk);
 
+#ifdef ARM64_S390_COMMON
 enum mitigation_state arm64_get_meltdown_state(void);
+#endif /* ARM64_S390_COMMON */
 
 enum mitigation_state arm64_get_spectre_bhb_state(void);
 bool is_spectre_bhb_affected(const struct arm64_cpu_capabilities *entry, int scope);
arch/arm64/kvm/sys_regs.h +1 -0
index bd56a45abbf9..44665ef10732 100644
@@ -7,6 +7,7 @@
  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  * Authors: Christoffer Dall <[email protected]>
  */
+/* Whole file is shared with s390 */
 
 #ifndef __ARM64_KVM_SYS_REGS_LOCAL_H__
 #define __ARM64_KVM_SYS_REGS_LOCAL_H__
Signature · 1 line
2.53.0
#12 lore raw

Re: [PATCH v2 04/20] arm64: Share more arm64 headers with s390

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

Mark more headers as shared or move definitions to an already shared location if the context is similar. Most of them are required to implement system register handling for the host.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer
#13 lore raw

Re: [PATCH v2 04/20] arm64: Share more arm64 headers with s390

In reply to Steffen Eiden
Quoted 80 lines · Steffen Eiden

Mark more headers as shared or move definitions to an already shared location if the context is similar. Most of them are required to implement system register handling for the host.

---
 arch/arm64/include/asm/cache.h       |  3 +++
 arch/arm64/include/asm/cputype.h     |  3 +++
 arch/arm64/include/asm/kvm_emulate.h |  5 ++++-
 arch/arm64/include/asm/kvm_host.h    | 18 +++++++++++++-----
 arch/arm64/include/asm/spectre.h     |  7 +++++++
 arch/arm64/kvm/sys_regs.h            |  1 +
 6 files changed, 31 insertions(+), 6 deletions(-)
arch/arm64/include/asm/cache.h +3 -0
index 10a7ffadee3d..f9dbc8143dc9 100644
@@ -8,6 +8,7 @@
 #define L1_CACHE_SHIFT		(6)
 #define L1_CACHE_BYTES		(1 << L1_CACHE_SHIFT)
 
+#ifdef ARM64_S390_COMMON
 #define CLIDR_LOUU_SHIFT	27
 #define CLIDR_LOC_SHIFT		24
 #define CLIDR_LOUIS_SHIFT	21
@@ -25,6 +26,8 @@
 /* Ttypen, bits [2(n - 1) + 34 : 2(n - 1) + 33], for n = 1 to 7 */
 #define CLIDR_TTYPE_SHIFT(level)	(2 * ((level) - 1) + CLIDR_EL1_Ttypen_SHIFT)
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * Memory returned by kmalloc() may be used for DMA, so we must make
  * sure that all such allocations are cache aligned. Otherwise,
arch/arm64/include/asm/cputype.h +3 -0
index 1fa29616e586..a5fd2b749b3b 100644
@@ -5,6 +5,7 @@
 #ifndef __ASM_CPUTYPE_H
 #define __ASM_CPUTYPE_H
 
+#ifdef ARM64_S390_COMMON
 #define INVALID_HWID		ULONG_MAX
 
 #define MPIDR_UP_BITMASK	(0x1 << 30)
@@ -259,6 +260,8 @@
 #define MIDR_FUJITSU_ERRATUM_010001_MASK	(~MIDR_CPU_VAR_REV(1, 0))
 #define TCR_CLEAR_FUJITSU_ERRATUM_010001	(TCR_EL1_NFD1 | TCR_EL1_NFD0)
 
+#endif /* ARM64_S390_COMMON */
+
 #ifndef __ASSEMBLER__
 
 #include <asm/sysreg.h>
arch/arm64/include/asm/kvm_emulate.h +3 -0
index 09f04a5686a1..6cd9b244d82e 100644
@@ -23,6 +23,7 @@
 #include <asm/cputype.h>
 #include <asm/virt.h>
 
+#ifdef ARM64_S390_COMMON
 #define CURRENT_EL_SP_EL0_VECTOR	0x0
 #define CURRENT_EL_SP_ELx_VECTOR	0x200
 #define LOWER_EL_AArch64_VECTOR		0x400
@@ -35,6 +36,8 @@ enum exception_type {
 	except_type_serror	= 0x180,
 };
 
+#endif /* ARM64_S390_COMMON */
+
 #define kvm_exception_type_names		\
 	{ except_type_sync,	"SYNC"   },	\
 	{ except_type_irq,	"IRQ"    },	\
@@ -525,13 +528,13 @@ static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu)
 	}

arch/arm64/include/asm/kvm_emulate.h static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu) · line 534

Quoted 10 lines
arch/arm64/include/asm/kvm_emulate.h +1 -1
 }
 
+#ifdef ARM64_S390_COMMON
 static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu)
 {
 	u64 esr = kvm_vcpu_get_esr(vcpu);
 	return ESR_ELx_SYS64_ISS_RT(esr);
 }
 
-#ifdef ARM64_S390_COMMON

This could have been done from the beginning, in the previous series. I keep asking you to reduce the churn...

Quoted 16 lines
arch/arm64/include/asm/kvm_emulate.h +0 -0
 static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
 {
 	if (kvm_vcpu_abt_iss1tw(vcpu)) {
arch/arm64/include/asm/kvm_host.h +1 -0
index f9e90ef9ee4b..2ca5fbc39f15 100644
@@ -349,6 +349,7 @@ enum fgt_group_id {
 	__NR_FGT_GROUP_IDS__
 };
 
+#ifdef ARM64_S390_COMMON
 struct kvm_vm_id_regs {
 	/*
 	 * Emulated CPU ID registers per VM

arch/arm64/include/asm/kvm_host.h struct kvm_vm_id_regs { · line 373

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -368,6 +369,8 @@ struct kvm_vm_id_regs {
 	u64 ctr_el0;
 };
 
+#endif /* ARM64_S390_COMMON */
+

That's another example of pointless churn. You could have added this structure at a convenient spot, where you already have S390 markers.

Quoted 97 lines
arch/arm64/include/asm/kvm_host.h +10 -5
 struct kvm_arch {
 	struct kvm_s2_mmu mmu;
 
@@ -1240,9 +1243,6 @@ u64 kvm_vcpu_apply_reg_masks(const struct kvm_vcpu *, enum vcpu_sysreg, u64);
 		__v;							\
 	})
 
-u64 vcpu_read_sys_reg(const struct kvm_vcpu *, enum vcpu_sysreg);
-void vcpu_write_sys_reg(struct kvm_vcpu *, u64, enum vcpu_sysreg);
-
 struct kvm_vm_stat {
 	struct kvm_vm_stat_generic generic;
 };
@@ -1322,11 +1322,9 @@ int kvm_handle_cp14_32(struct kvm_vcpu *vcpu);
 int kvm_handle_cp14_64(struct kvm_vcpu *vcpu);
 int kvm_handle_cp15_32(struct kvm_vcpu *vcpu);
 int kvm_handle_cp15_64(struct kvm_vcpu *vcpu);
-int kvm_handle_sys_reg(struct kvm_vcpu *vcpu);
 int kvm_handle_cp10_id(struct kvm_vcpu *vcpu);
 
 void kvm_sys_regs_create_debugfs(struct kvm *kvm);
-void kvm_reset_sys_regs(struct kvm_vcpu *vcpu);
 
 int __init kvm_sys_reg_table_init(void);
 struct sys_reg_desc;
@@ -1337,6 +1335,11 @@ int __init populate_nv_trap_config(void);
 void kvm_calculate_traps(struct kvm_vcpu *vcpu);
 
 #ifdef ARM64_S390_COMMON
+u64 vcpu_read_sys_reg(const struct kvm_vcpu *, enum vcpu_sysreg);
+void vcpu_write_sys_reg(struct kvm_vcpu *, u64, enum vcpu_sysreg);
+int kvm_handle_sys_reg(struct kvm_vcpu *vcpu);
+void kvm_reset_sys_regs(struct kvm_vcpu *vcpu);
+
 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
@@ -1538,8 +1541,10 @@ struct kvm *kvm_arch_alloc_vm(void);
 	(system_supports_32bit_el0() &&				\
 	 !static_branch_unlikely(&arm64_mismatched_32bit_el0))
 
+#ifdef ARM64_S390_COMMON
 #define kvm_vm_has_ran_once(kvm)					\
 	(test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &(kvm)->arch.flags))
+#endif /* ARM64_S390_COMMON */
 
 static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature)
 {
@@ -1567,6 +1572,7 @@ static inline void kvm_hyp_reserve(void) { }
 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
 
+#ifdef ARM64_S390_COMMON
 static inline u64 *__vm_id_reg(struct kvm_vm_id_regs *id_regs, u32 reg)
 {
 	switch (reg) {
@@ -1640,6 +1646,8 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
 	(kvm_cmp_feat(kvm, id, fld, >=, min) &&				\
 	kvm_cmp_feat(kvm, id, fld, <=, max))
 
+#endif /* ARM64_S390_COMMON */
+
 /* Check for a given level of PAuth support */
 #define kvm_has_pauth(k, l)						\
 	({								\
arch/arm64/include/asm/spectre.h +5 -0
index 296ae3420bfd..e1c13797ea7c 100644
@@ -19,6 +19,7 @@
 #include <asm/cpufeature.h>
 #include <asm/virt.h>
 
+#ifdef ARM64_S390_COMMON
 /* Watch out, ordering is important here. */
 enum mitigation_state {
 	SPECTRE_UNAFFECTED,
@@ -26,6 +27,8 @@ enum mitigation_state {
 	SPECTRE_VULNERABLE,
 };
 
+#endif /* ARM64_S390_COMMON */
+
 struct pt_regs;
 struct task_struct;
 
@@ -81,7 +84,9 @@ static __always_inline void arm64_apply_bp_hardening(void)
 		d->fn();
 }
 
+#ifdef ARM64_S390_COMMON
 enum mitigation_state arm64_get_spectre_v2_state(void);
+#endif /* ARM64_S390_COMMON */
 bool has_spectre_v2(const struct arm64_cpu_capabilities *cap, int scope);
 void spectre_v2_enable_mitigation(const struct arm64_cpu_capabilities *__unused);
 

arch/arm64/include/asm/spectre.h bool has_spectre_v4(const struct arm64_cpu_capabilities *cap, int scope); · line 103

Quoted 6 lines
arch/arm64/include/asm/spectre.h +2 -0
@@ -93,7 +98,9 @@ bool has_spectre_v4(const struct arm64_cpu_capabilities *cap, int scope);
 void spectre_v4_enable_mitigation(const struct arm64_cpu_capabilities *__unused);
 void spectre_v4_enable_task_mitigation(struct task_struct *tsk);
 
+#ifdef ARM64_S390_COMMON
 enum mitigation_state arm64_get_meltdown_state(void);
+#endif /* ARM64_S390_COMMON */

Again: how about grouping things in a logical way so that you can minimise the number of annotations? The two prototypes you single out are clearly related to the enum at the beginning of the file, and don't depend on anything else.

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#14 lore raw

PATCH v2 5/20 KVM: s390: arm64: Prepare for sharing more arm64 code

In reply to Steffen Eiden

Add required SVE definitions to the arm64 on s390 host code. The definitions are required to be able to build shared SVE related code.

---
 arch/s390/kvm/arm64/guest.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/s390/kvm/arm64/guest.c +6 -0
index 4ea6fe2c270d..8b5ee9e79a4f 100644
@@ -5,6 +5,12 @@
 #include <arm64/kvm_emulate.h>
 #include <arm64/kvm_nested.h>
 
+#define SVE_VQ_MIN	__SVE_VQ_MIN
+#define SVE_NUM_ZREGS	KVM_ARM64_SVE_NUM_ZREGS
+#define SVE_NUM_PREGS	KVM_ARM64_SVE_NUM_PREGS
+
+#define vcpu_sve_slices(_vcpu) 1
+
 #define __INCL_GEN_ARM_FILE
 #include "generated/guest.inc"
 #undef __INCL_GEN_ARM_FILE
Signature · 1 line
2.53.0
#15 lore raw

Re: [PATCH v2 05/20] KVM: s390: arm64: Prepare for sharing more arm64 code

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

Add required SVE definitions to the arm64 on s390 host code. The definitions are required to be able to build shared SVE related code.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
maintainer
#16 lore raw

Re: [PATCH v2 05/20] KVM: s390: arm64: Prepare for sharing more arm64 code

In reply to Steffen Eiden
Quoted 5 lines · Steffen Eiden

Add required SVE definitions to the arm64 on s390 host code. The definitions are required to be able to build shared SVE related code.

The ordering feels wrong. Please keep the KVM/arm64 as a prefix for the S390 patches as much as possible.

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#17 lore raw

PATCH v2 6/20 KVM: arm64: Prepare sys_regs.c for sharing with s390

In reply to Steffen Eiden

Prepare sharing of sys_regs.c with s390 by moving functions to another location inside sys_regs.c. This reduces the number of shared regions.

---
 arch/arm64/kvm/sys_regs.c | 100 +++++++++++++++++++-------------------
 arch/s390/kvm/arm64/arm.c |   2 +
 2 files changed, 52 insertions(+), 50 deletions(-)
arch/arm64/kvm/sys_regs.c +50 -50
index 7d0047b542f4..53d096803d9d 100644
@@ -2043,13 +2043,6 @@ static inline bool is_vcpu_ftr_id_reg(u32 id)
 	return is_feature_id_reg(id) && !is_vm_ftr_id_reg(id);
 }
 
-static inline bool is_aa32_id_reg(u32 id)
-{
-	return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 &&
-		sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 &&
-		sys_reg_CRm(id) <= 3);
-}
-
 static unsigned int id_visibility(const struct kvm_vcpu *vcpu,
 				  const struct sys_reg_desc *r)
 {
@@ -2065,20 +2058,6 @@ static unsigned int id_visibility(const struct kvm_vcpu *vcpu,
 	return 0;
 }
 
-static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu,
-				       const struct sys_reg_desc *r)
-{
-	/*
-	 * AArch32 ID registers are UNKNOWN if AArch32 isn't implemented at any
-	 * EL. Promote to RAZ/WI in order to guarantee consistency between
-	 * systems.
-	 */
-	if (!kvm_supports_32bit_el0())
-		return REG_RAZ | REG_USER_WI;
-
-	return id_visibility(vcpu, r);
-}
-
 static unsigned int raz_visibility(const struct kvm_vcpu *vcpu,
 				   const struct sys_reg_desc *r)
 {
@@ -2099,6 +2078,27 @@ static bool access_id_reg(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu,
+				       const struct sys_reg_desc *r)
+{
+	/*
+	 * AArch32 ID registers are UNKNOWN if AArch32 isn't implemented at any
+	 * EL. Promote to RAZ/WI in order to guarantee consistency between
+	 * systems.
+	 */
+	if (!kvm_supports_32bit_el0())
+		return REG_RAZ | REG_USER_WI;
+
+	return id_visibility(vcpu, r);
+}
+
+static inline bool is_aa32_id_reg(u32 id)
+{
+	return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 &&
+		sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 &&
+		sys_reg_CRm(id) <= 3);
+}
+
 /* Visibility overrides for SVE-specific control registers */
 static unsigned int sve_visibility(const struct kvm_vcpu *vcpu,
 				   const struct sys_reg_desc *rd)
@@ -2553,29 +2553,6 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val)
 	*p = val;
 }
 
-static int get_raz_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
-		       u64 *val)
-{
-	*val = 0;
-	return 0;
-}
-
-static int set_wi_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
-		      u64 val)
-{
-	return 0;
-}
-
-static bool access_ctr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
-		       const struct sys_reg_desc *r)
-{
-	if (p->is_write)
-		return write_to_read_only(vcpu, p, r);
-
-	p->regval = kvm_read_vm_id_reg(vcpu->kvm, SYS_CTR_EL0);
-	return true;
-}
-
 static bool access_clidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 			 const struct sys_reg_desc *r)
 {
@@ -2684,6 +2661,29 @@ static bool access_ccsidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 	return true;
 }
 
+static int get_raz_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
+		       u64 *val)
+{
+	*val = 0;
+	return 0;
+}
+
+static int set_wi_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
+		      u64 val)
+{
+	return 0;
+}
+
+static bool access_ctr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+		       const struct sys_reg_desc *r)
+{
+	if (p->is_write)
+		return write_to_read_only(vcpu, p, r);
+
+	p->regval = kvm_read_vm_id_reg(vcpu->kvm, SYS_CTR_EL0);
+	return true;
+}
+
 static unsigned int mte_visibility(const struct kvm_vcpu *vcpu,
 				   const struct sys_reg_desc *rd)
 {
@@ -4855,12 +4855,6 @@ static bool check_sysreg_table(const struct sys_reg_desc *table, unsigned int n,
 	return true;
 }
 
-int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu)
-{
-	kvm_inject_undefined(vcpu);
-	return 1;
-}
-
 static void perform_access(struct kvm_vcpu *vcpu,
 			   struct sys_reg_params *params,
 			   const struct sys_reg_desc *r)
@@ -4944,6 +4938,12 @@ static void unhandled_cp_access(struct kvm_vcpu *vcpu,
 	kvm_inject_undefined(vcpu);
 }
 
+int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu)
+{
+	kvm_inject_undefined(vcpu);
+	return 1;
+}
+
 /**
  * kvm_handle_cp_64 -- handles a mrrc/mcrr trap on a guest CP14/CP15 access
  * @vcpu: The VCPU pointer
arch/s390/kvm/arm64/arm.c +2 -0
index 3caa72f780a5..2740ee3b703e 100644
@@ -9,6 +9,7 @@
 #include <linux/kvm_host.h>
 #include <linux/cleanup.h>
 #include <linux/fpu.h>
+#include <linux/bitfield.h>
 
 #include <asm/access-regs.h>
 
@@ -25,6 +26,7 @@
 #include "trace.h"
 
 static unsigned long system_supported_vcpu_features(void);
+#define read_sanitised_ftr_reg(_id) 0
 
 #define __INCL_GEN_ARM_FILE
 #include "generated/arm.inc"
Signature · 1 line
2.53.0
#18 lore raw

Re: [PATCH v2 06/20] KVM: arm64: Prepare sys_regs.c for sharing with s390

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

Prepare sharing of sys_regs.c with s390 by moving functions to another location inside sys_regs.c. This reduces the number of shared regions.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
maintainer
#19 lore raw

Re: [PATCH v2 06/20] KVM: arm64: Prepare sys_regs.c for sharing with s390

In reply to Steffen Eiden
Quoted 176 lines · Steffen Eiden

Prepare sharing of sys_regs.c with s390 by moving functions to another location inside sys_regs.c. This reduces the number of shared regions.

---
 arch/arm64/kvm/sys_regs.c | 100 +++++++++++++++++++-------------------
 arch/s390/kvm/arm64/arm.c |   2 +
 2 files changed, 52 insertions(+), 50 deletions(-)
arch/arm64/kvm/sys_regs.c +50 -50
index 7d0047b542f4..53d096803d9d 100644
@@ -2043,13 +2043,6 @@ static inline bool is_vcpu_ftr_id_reg(u32 id)
 	return is_feature_id_reg(id) && !is_vm_ftr_id_reg(id);
 }
 
-static inline bool is_aa32_id_reg(u32 id)
-{
-	return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 &&
-		sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 &&
-		sys_reg_CRm(id) <= 3);
-}
-
 static unsigned int id_visibility(const struct kvm_vcpu *vcpu,
 				  const struct sys_reg_desc *r)
 {
@@ -2065,20 +2058,6 @@ static unsigned int id_visibility(const struct kvm_vcpu *vcpu,
 	return 0;
 }
 
-static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu,
-				       const struct sys_reg_desc *r)
-{
-	/*
-	 * AArch32 ID registers are UNKNOWN if AArch32 isn't implemented at any
-	 * EL. Promote to RAZ/WI in order to guarantee consistency between
-	 * systems.
-	 */
-	if (!kvm_supports_32bit_el0())
-		return REG_RAZ | REG_USER_WI;
-
-	return id_visibility(vcpu, r);
-}
-
 static unsigned int raz_visibility(const struct kvm_vcpu *vcpu,
 				   const struct sys_reg_desc *r)
 {
@@ -2099,6 +2078,27 @@ static bool access_id_reg(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu,
+				       const struct sys_reg_desc *r)
+{
+	/*
+	 * AArch32 ID registers are UNKNOWN if AArch32 isn't implemented at any
+	 * EL. Promote to RAZ/WI in order to guarantee consistency between
+	 * systems.
+	 */
+	if (!kvm_supports_32bit_el0())
+		return REG_RAZ | REG_USER_WI;
+
+	return id_visibility(vcpu, r);
+}
+
+static inline bool is_aa32_id_reg(u32 id)
+{
+	return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 &&
+		sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 &&
+		sys_reg_CRm(id) <= 3);
+}
+
 /* Visibility overrides for SVE-specific control registers */
 static unsigned int sve_visibility(const struct kvm_vcpu *vcpu,
 				   const struct sys_reg_desc *rd)
@@ -2553,29 +2553,6 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val)
 	*p = val;
 }
 
-static int get_raz_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
-		       u64 *val)
-{
-	*val = 0;
-	return 0;
-}
-
-static int set_wi_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
-		      u64 val)
-{
-	return 0;
-}
-
-static bool access_ctr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
-		       const struct sys_reg_desc *r)
-{
-	if (p->is_write)
-		return write_to_read_only(vcpu, p, r);
-
-	p->regval = kvm_read_vm_id_reg(vcpu->kvm, SYS_CTR_EL0);
-	return true;
-}
-
 static bool access_clidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 			 const struct sys_reg_desc *r)
 {
@@ -2684,6 +2661,29 @@ static bool access_ccsidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 	return true;
 }
 
+static int get_raz_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
+		       u64 *val)
+{
+	*val = 0;
+	return 0;
+}
+
+static int set_wi_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
+		      u64 val)
+{
+	return 0;
+}
+
+static bool access_ctr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
+		       const struct sys_reg_desc *r)
+{
+	if (p->is_write)
+		return write_to_read_only(vcpu, p, r);
+
+	p->regval = kvm_read_vm_id_reg(vcpu->kvm, SYS_CTR_EL0);
+	return true;
+}
+
 static unsigned int mte_visibility(const struct kvm_vcpu *vcpu,
 				   const struct sys_reg_desc *rd)
 {
@@ -4855,12 +4855,6 @@ static bool check_sysreg_table(const struct sys_reg_desc *table, unsigned int n,
 	return true;
 }
 
-int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu)
-{
-	kvm_inject_undefined(vcpu);
-	return 1;
-}
-
 static void perform_access(struct kvm_vcpu *vcpu,
 			   struct sys_reg_params *params,
 			   const struct sys_reg_desc *r)
@@ -4944,6 +4938,12 @@ static void unhandled_cp_access(struct kvm_vcpu *vcpu,
 	kvm_inject_undefined(vcpu);
 }
 
+int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu)
+{
+	kvm_inject_undefined(vcpu);
+	return 1;
+}
+
 /**
  * kvm_handle_cp_64 -- handles a mrrc/mcrr trap on a guest CP14/CP15 access
  * @vcpu: The VCPU pointer
arch/s390/kvm/arm64/arm.c +1 -0
index 3caa72f780a5..2740ee3b703e 100644
@@ -9,6 +9,7 @@
 #include <linux/kvm_host.h>
 #include <linux/cleanup.h>
 #include <linux/fpu.h>
+#include <linux/bitfield.h>
 
 #include <asm/access-regs.h>
 

arch/s390/kvm/arm64/arm.c line 32

Quoted 7 lines
arch/s390/kvm/arm64/arm.c +1 -0
@@ -25,6 +26,7 @@
 #include "trace.h"
 
 static unsigned long system_supported_vcpu_features(void);
+#define read_sanitised_ftr_reg(_id) 0
 
 #define __INCL_GEN_ARM_FILE
 #include "generated/arm.inc"

Please split this. arm64 refactor and S390 changes should not be part of the same patch.

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#20 lore raw

Re: [PATCH v2 06/20] KVM: arm64: Prepare sys_regs.c for sharing with s390

Quoted 11 lines · Marc Zyngier
Quoted 9 lines · Steffen Eiden

Prepare sharing of sys_regs.c with s390 by moving functions to another location inside sys_regs.c. This reduces the number of shared regions.

---
 arch/arm64/kvm/sys_regs.c | 100 +++++++++++++++++++-------------------
 arch/s390/kvm/arm64/arm.c |   2 +
 2 files changed, 52 insertions(+), 50 deletions(-)

...

Quoted 3 lines

Please split this. arm64 refactor and S390 changes should not be part of the same patch.

Sorry, this was a mistake.

Steffen

author reviewer
#21 lore raw

PATCH v2 7/20 KVM: arm64: Share more arm64 code with s390

In reply to Steffen Eiden

Mark functions that s390 can use to implement arm on s390 as shared functions.

---
 arch/arm64/kvm/arm.c      |  3 +++
 arch/arm64/kvm/guest.c    | 11 +++++++----
 arch/arm64/kvm/sys_regs.c | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+), 4 deletions(-)
arch/arm64/kvm/arm.c +3 -0
index 6b92a3c1d490..62e8b32ee74e 100644
@@ -340,6 +340,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 	kvm_arm_teardown_hypercalls(kvm);
 }
 
+#ifdef ARM64_S390_COMMON
 static bool kvm_has_full_ptr_auth(void)
 {
 	bool apa, gpa, api, gpi, apa3, gpa3;
@@ -374,6 +375,8 @@ static bool kvm_has_full_ptr_auth(void)
 		(apa + api + apa3) == 1);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 {
 	int r;
arch/arm64/kvm/guest.c +7 -4
index 6ca5a9f357cd..e978b2fbeb76 100644
@@ -307,12 +307,12 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	return err;
 }
 
-#endif /* ARM64_S390_COMMON */
-
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
 #define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
 #define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
 
+#endif /* ARM64_S390_COMMON */
+
 static int get_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
 	unsigned int max_vq, vq;
@@ -383,6 +383,7 @@ static int set_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	return 0;
 }
 
+#ifdef ARM64_S390_COMMON
 #define SVE_REG_SLICE_SHIFT	0
 #define SVE_REG_SLICE_BITS	5
 #define SVE_REG_ID_SHIFT	(SVE_REG_SLICE_SHIFT + SVE_REG_SLICE_BITS)
@@ -399,6 +400,8 @@ static int set_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 #define KVM_SVE_ZREG_SIZE KVM_REG_SIZE(KVM_REG_ARM64_SVE_ZREG(0, 0))
 #define KVM_SVE_PREG_SIZE KVM_REG_SIZE(KVM_REG_ARM64_SVE_PREG(0, 0))
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * Number of register slices required to cover each whole SVE register.
  * NOTE: Only the first slice every exists, for now.
@@ -595,8 +598,6 @@ static unsigned long num_core_regs(const struct kvm_vcpu *vcpu)
 	return copy_core_reg_indices(vcpu, NULL);
 }
 
-#endif /* ARM64_S390_COMMON */
-
 static unsigned long num_sve_regs(const struct kvm_vcpu *vcpu)
 {
 	const unsigned int slices = vcpu_sve_slices(vcpu);
@@ -658,6 +659,8 @@ static int copy_sve_reg_indices(const struct kvm_vcpu *vcpu,
 	return num_regs;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /**
  * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  * @vcpu: the vCPU pointer
arch/arm64/kvm/sys_regs.c +36 -0
index 53d096803d9d..b167f9d06fd4 100644
@@ -45,6 +45,7 @@
  * 64bit interface.
  */
 
+#ifdef ARM64_S390_COMMON
 static u64 sys_reg_to_index(const struct sys_reg_desc *reg);
 static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 		      u64 val);
@@ -82,6 +83,8 @@ static bool write_to_read_only(struct kvm_vcpu *vcpu,
 			"sys_reg write to read-only register");
 }
 
+#endif /* ARM64_S390_COMMON */
+
 enum sr_loc_attr {
 	SR_LOC_MEMORY	= 0,	  /* Register definitely in memory */
 	SR_LOC_LOADED	= BIT(0), /* Register on CPU, unless it cannot */
@@ -428,6 +431,7 @@ void vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, enum vcpu_sysreg reg)
 	__vcpu_assign_sys_reg(vcpu, reg, val);
 }
 
+#ifdef ARM64_S390_COMMON
 /* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
 #define CSSELR_MAX 14
 
@@ -533,6 +537,8 @@ static bool access_rw(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
  */
@@ -800,6 +806,7 @@ static bool access_gicv5_ppi_enabler(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+#ifdef ARM64_S390_COMMON
 static bool trap_raz_wi(struct kvm_vcpu *vcpu,
 			struct sys_reg_params *p,
 			const struct sys_reg_desc *r)
@@ -853,6 +860,8 @@ static bool trap_oslsr_el1(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static int set_oslsr_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 			 u64 val)
 {
@@ -1011,6 +1020,7 @@ static u64 reset_actlr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	return actlr;
 }
 
+#ifdef ARM64_S390_COMMON
 static u64 reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	u64 mpidr = kvm_calculate_mpidr(vcpu);
@@ -1019,6 +1029,8 @@ static u64 reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	return mpidr;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static unsigned int hidden_visibility(const struct kvm_vcpu *vcpu,
 				      const struct sys_reg_desc *r)
 {
@@ -1922,6 +1934,7 @@ static u8 pmuver_to_perfmon(u8 pmuver)
 	}
 }
 
+#ifdef ARM64_S390_COMMON
 static u64 sanitise_id_aa64pfr0_el1(const struct kvm_vcpu *vcpu, u64 val);
 static u64 sanitise_id_aa64pfr1_el1(const struct kvm_vcpu *vcpu, u64 val);
 static u64 sanitise_id_aa64pfr2_el1(const struct kvm_vcpu *vcpu, u64 val);
@@ -2078,6 +2091,8 @@ static bool access_id_reg(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu,
 				       const struct sys_reg_desc *r)
 {
@@ -2127,6 +2142,7 @@ static unsigned int fp8_visibility(const struct kvm_vcpu *vcpu,
 	return REG_HIDDEN;
 }
 
+#ifdef ARM64_S390_COMMON
 static u64 sanitise_id_aa64pfr0_el1(const struct kvm_vcpu *vcpu, u64 val)
 {
 	if (!vcpu_has_sve(vcpu))
@@ -2288,6 +2304,8 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
 	return set_id_reg(vcpu, rd, val);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static u64 read_sanitised_id_dfr0_el1(struct kvm_vcpu *vcpu,
 				      const struct sys_reg_desc *rd)
 {
@@ -2332,6 +2350,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	return set_id_reg(vcpu, rd, val);
 }
 
+#ifdef ARM64_S390_COMMON
 static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 			       const struct sys_reg_desc *rd, u64 user_val)
 {
@@ -2661,6 +2680,8 @@ static bool access_ccsidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 	return true;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static int get_raz_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 		       u64 *val)
 {
@@ -2764,6 +2785,7 @@ static bool bad_redir_trap(struct kvm_vcpu *vcpu,
 	SYS_REG_USER_FILTER(name, access_arch_timer, reset_val, 0, \
 			    arch_timer_get_user, arch_timer_set_user, vis)
 
+#ifdef ARM64_S390_COMMON
 /*
  * Since reset() callback and field val are not used for idregs, they will be
  * used for specific purposes for idregs.
@@ -2840,6 +2862,8 @@ static bool bad_redir_trap(struct kvm_vcpu *vcpu,
 	.val = 0,				\
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static bool access_sp_el1(struct kvm_vcpu *vcpu,
 			  struct sys_reg_params *p,
 			  const struct sys_reg_desc *r)
@@ -3212,6 +3236,7 @@ static void init_imp_id_regs(void)
 	boot_cpu_aidr_val = read_sysreg(aidr_el1);
 }
 
+#ifdef ARM64_S390_COMMON
 static u64 reset_imp_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	switch (reg_to_encoding(r)) {
@@ -3269,6 +3294,8 @@ static int set_imp_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
 	.val = mask,					\
 	}
 
+#endif /* ARM64_S390_COMMON */
+
 static u64 reset_mdcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	__vcpu_assign_sys_reg(vcpu, r->reg, vcpu->kvm->arch.nr_pmu_counters);
@@ -4833,6 +4860,7 @@ static const struct sys_reg_desc cp15_64_regs[] = {
 	{ SYS_DESC(SYS_AARCH32_CNTVCTSS),     access_arch_timer },
 };
 
+#ifdef ARM64_S390_COMMON
 static bool check_sysreg_table(const struct sys_reg_desc *table, unsigned int n,
 			       bool reset_check)
 {
@@ -4882,6 +4910,8 @@ static void perform_access(struct kvm_vcpu *vcpu,
 		kvm_incr_pc(vcpu);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * emulate_cp --  tries to match a sys_reg access in a handling table, and
  *                call the corresponding trap handler.
@@ -5340,6 +5370,7 @@ void kvm_sys_regs_create_debugfs(struct kvm *kvm)
 			    &sr_resx_fops);
 }
 
+#ifdef ARM64_S390_COMMON
 static void reset_vm_ftr_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *reg)
 {
 	u32 id = reg_to_encoding(reg);
@@ -5360,6 +5391,8 @@ static void reset_vcpu_ftr_id_reg(struct kvm_vcpu *vcpu,
 	reg->reset(vcpu, reg);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /**
  * kvm_reset_sys_regs - sets system registers to reset value
  * @vcpu: The VCPU pointer
@@ -5432,6 +5465,7 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+#ifdef ARM64_S390_COMMON
 /******************************************************************************
  * Userspace API
  *****************************************************************************/
@@ -5753,6 +5787,8 @@ int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
 	return write_demux_regids(uindices);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 #define KVM_ARM_FEATURE_ID_RANGE_INDEX(r)			\
 	KVM_ARM_FEATURE_ID_RANGE_IDX(sys_reg_Op0(r),		\
 		sys_reg_Op1(r),					\
Signature · 1 line
2.53.0
#22 lore raw

Re: [PATCH v2 07/20] KVM: arm64: Share more arm64 code with s390

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Guest-reachable WARN_ONCE in bad_trap() via read_from_write_only() and write_to_read_only()
- [High] Guest-reachable WARN_ON_ONCE in access_gicv5_ppi_enabler() triggered by a guest read. --

commit feaf451c9f33eb68d8400a6395fedbe94a88f53b

Author: Steffen Eiden <[email protected]>
Date:   Mon Aug 31 14:55:22 2026 +0000

KVM: arm64: Share more arm64 code with s390

This commit marks several functions in the KVM arm64 subsystem as shared so they can be reused by s390. It achieves this by wrapping the relevant code blocks in #ifdef ARM64_S390_COMMON.

arch/arm64/kvm/sys_regs.c static bool write_to_read_only(struct kvm_vcpu *vcpu, · line 87

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +2 -0
index 53d096803d9dc..b167f9d06fd46 100644
@@ -82,6 +83,8 @@ static bool write_to_read_only(struct kvm_vcpu *vcpu,
 			"sys_reg write to read-only register");
 }
 
+#endif /* ARM64_S390_COMMON */
+

[Severity: High]
This is a pre-existing issue, but doesn't bad_trap() allow a guest to trigger a host DoS?

The bad_trap() function calls WARN_ONCE() which will crash the host if panic_on_warn is enabled:

arch/arm64/kvm/sys_regs.c:bad_trap() {
	WARN_ONCE(1, "Unexpected %s\n", msg);
	...
}

This is unconditionally reached if a system register handler calls read_from_write_only() or write_to_read_only().

Because KVM's sysreg dispatcher does not validate the direction (read vs. write) of an access before calling a register's trap handler, a malicious guest can deliberately execute a read (e.g., MRS) to a write-only register that traps to EL2.

The dispatcher routes it to the handler with p->is_write == false, the handler checks !p->is_write, calls read_from_write_only(), and triggers the host warning.

[ ... ]

arch/arm64/kvm/sys_regs.c static bool access_gicv5_ppi_enabler(struct kvm_vcpu *vcpu, · line 810

Quoted 6 lines
arch/arm64/kvm/sys_regs.c +1 -0
@@ -800,6 +806,7 @@ static bool access_gicv5_ppi_enabler(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+#ifdef ARM64_S390_COMMON
 static bool trap_raz_wi(struct kvm_vcpu *vcpu,

[Severity: High]
This isn't a bug introduced by this patch, but doesn't the access_gicv5_ppi_enabler() trap handler also allow a malicious guest to crash the host?

The handler executes a WARN_ON_ONCE unconditionally upon entry:

arch/arm64/kvm/sys_regs.c:access_gicv5_ppi_enabler() {
	...
	/* We never expect to get here with a read! */
	if (WARN_ON_ONCE(!p->is_write))
		return undef_access(vcpu, p, r);
	...
}

Similar to the issue above, the system register dispatcher does not intrinsically filter read vs. write accesses before routing the trap to the handler.

A guest could execute an MRS (read) instruction targeting ICC_PPI_ENABLER0_EL1, which would route to this handler with p->is_write == false, triggering the warning and potentially causing a host DoS.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
maintainer
#23 lore raw

Re: [PATCH v2 07/20] KVM: arm64: Share more arm64 code with s390

In reply to Steffen Eiden
Quoted 3 lines · Steffen Eiden

Mark functions that s390 can use to implement arm on s390 as shared functions.

More of what? For what purpose? What is the logic behind exposing snippets of code to S390? This patch tags some PAuth stuff, a bunch of SVE definitions, and a whole lot of sysreg helpers.

What is the rationale for this organisation? I can't see any, and it just looks like a very random approach to the problem.

I want to see logic, a progression. Something I can follow.

Quoted 21 lines
---
 arch/arm64/kvm/arm.c      |  3 +++
 arch/arm64/kvm/guest.c    | 11 +++++++----
 arch/arm64/kvm/sys_regs.c | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+), 4 deletions(-)
arch/arm64/kvm/arm.c +1 -0
index 6b92a3c1d490..62e8b32ee74e 100644
@@ -340,6 +340,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 	kvm_arm_teardown_hypercalls(kvm);
 }
 
+#ifdef ARM64_S390_COMMON
 static bool kvm_has_full_ptr_auth(void)
 {
 	bool apa, gpa, api, gpi, apa3, gpa3;

arch/arm64/kvm/arm.c static bool kvm_has_full_ptr_auth(void) · line 379

Quoted 5 lines
arch/arm64/kvm/arm.c +2 -0
@@ -374,6 +375,8 @@ static bool kvm_has_full_ptr_auth(void)
 		(apa + api + apa3) == 1);
 }
 
+#endif /* ARM64_S390_COMMON */
+

This could have been done much earlier.

Quoted 9 lines
arch/arm64/kvm/arm.c +0 -0
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 {
 	int r;
arch/arm64/kvm/guest.c +0 -0
index 6ca5a9f357cd..e978b2fbeb76 100644
@@ -307,12 +307,12 @@ static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	return err;

arch/arm64/kvm/guest.c static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) · line 315

Quoted 10 lines
arch/arm64/kvm/guest.c +2 -2
 }
 
-#endif /* ARM64_S390_COMMON */
-
 #define vq_word(vq) (((vq) - SVE_VQ_MIN) / 64)
 #define vq_mask(vq) ((u64)1 << ((vq) - SVE_VQ_MIN) % 64)
 #define vq_present(vqs, vq) (!!((vqs)[vq_word(vq)] & vq_mask(vq)))
 
+#endif /* ARM64_S390_COMMON */
+

Pointless churn.

Quoted 238 lines
arch/arm64/kvm/guest.c +5 -2
 static int get_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 {
 	unsigned int max_vq, vq;
@@ -383,6 +383,7 @@ static int set_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	return 0;
 }
 
+#ifdef ARM64_S390_COMMON
 #define SVE_REG_SLICE_SHIFT	0
 #define SVE_REG_SLICE_BITS	5
 #define SVE_REG_ID_SHIFT	(SVE_REG_SLICE_SHIFT + SVE_REG_SLICE_BITS)
@@ -399,6 +400,8 @@ static int set_sve_vls(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 #define KVM_SVE_ZREG_SIZE KVM_REG_SIZE(KVM_REG_ARM64_SVE_ZREG(0, 0))
 #define KVM_SVE_PREG_SIZE KVM_REG_SIZE(KVM_REG_ARM64_SVE_PREG(0, 0))
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * Number of register slices required to cover each whole SVE register.
  * NOTE: Only the first slice every exists, for now.
@@ -595,8 +598,6 @@ static unsigned long num_core_regs(const struct kvm_vcpu *vcpu)
 	return copy_core_reg_indices(vcpu, NULL);
 }
 
-#endif /* ARM64_S390_COMMON */
-
 static unsigned long num_sve_regs(const struct kvm_vcpu *vcpu)
 {
 	const unsigned int slices = vcpu_sve_slices(vcpu);
@@ -658,6 +659,8 @@ static int copy_sve_reg_indices(const struct kvm_vcpu *vcpu,
 	return num_regs;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /**
  * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
  * @vcpu: the vCPU pointer
arch/arm64/kvm/sys_regs.c +34 -0
index 53d096803d9d..b167f9d06fd4 100644
@@ -45,6 +45,7 @@
  * 64bit interface.
  */
 
+#ifdef ARM64_S390_COMMON
 static u64 sys_reg_to_index(const struct sys_reg_desc *reg);
 static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 		      u64 val);
@@ -82,6 +83,8 @@ static bool write_to_read_only(struct kvm_vcpu *vcpu,
 			"sys_reg write to read-only register");
 }
 
+#endif /* ARM64_S390_COMMON */
+
 enum sr_loc_attr {
 	SR_LOC_MEMORY	= 0,	  /* Register definitely in memory */
 	SR_LOC_LOADED	= BIT(0), /* Register on CPU, unless it cannot */
@@ -428,6 +431,7 @@ void vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, enum vcpu_sysreg reg)
 	__vcpu_assign_sys_reg(vcpu, reg, val);
 }
 
+#ifdef ARM64_S390_COMMON
 /* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
 #define CSSELR_MAX 14
 
@@ -533,6 +537,8 @@ static bool access_rw(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
  */
@@ -800,6 +806,7 @@ static bool access_gicv5_ppi_enabler(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+#ifdef ARM64_S390_COMMON
 static bool trap_raz_wi(struct kvm_vcpu *vcpu,
 			struct sys_reg_params *p,
 			const struct sys_reg_desc *r)
@@ -853,6 +860,8 @@ static bool trap_oslsr_el1(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static int set_oslsr_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 			 u64 val)
 {
@@ -1011,6 +1020,7 @@ static u64 reset_actlr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	return actlr;
 }
 
+#ifdef ARM64_S390_COMMON
 static u64 reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	u64 mpidr = kvm_calculate_mpidr(vcpu);
@@ -1019,6 +1029,8 @@ static u64 reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	return mpidr;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static unsigned int hidden_visibility(const struct kvm_vcpu *vcpu,
 				      const struct sys_reg_desc *r)
 {
@@ -1922,6 +1934,7 @@ static u8 pmuver_to_perfmon(u8 pmuver)
 	}
 }
 
+#ifdef ARM64_S390_COMMON
 static u64 sanitise_id_aa64pfr0_el1(const struct kvm_vcpu *vcpu, u64 val);
 static u64 sanitise_id_aa64pfr1_el1(const struct kvm_vcpu *vcpu, u64 val);
 static u64 sanitise_id_aa64pfr2_el1(const struct kvm_vcpu *vcpu, u64 val);
@@ -2078,6 +2091,8 @@ static bool access_id_reg(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu,
 				       const struct sys_reg_desc *r)
 {
@@ -2127,6 +2142,7 @@ static unsigned int fp8_visibility(const struct kvm_vcpu *vcpu,
 	return REG_HIDDEN;
 }
 
+#ifdef ARM64_S390_COMMON
 static u64 sanitise_id_aa64pfr0_el1(const struct kvm_vcpu *vcpu, u64 val)
 {
 	if (!vcpu_has_sve(vcpu))
@@ -2288,6 +2304,8 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
 	return set_id_reg(vcpu, rd, val);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static u64 read_sanitised_id_dfr0_el1(struct kvm_vcpu *vcpu,
 				      const struct sys_reg_desc *rd)
 {
@@ -2332,6 +2350,7 @@ static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
 	return set_id_reg(vcpu, rd, val);
 }
 
+#ifdef ARM64_S390_COMMON
 static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
 			       const struct sys_reg_desc *rd, u64 user_val)
 {
@@ -2661,6 +2680,8 @@ static bool access_ccsidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
 	return true;
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static int get_raz_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 		       u64 *val)
 {
@@ -2764,6 +2785,7 @@ static bool bad_redir_trap(struct kvm_vcpu *vcpu,
 	SYS_REG_USER_FILTER(name, access_arch_timer, reset_val, 0, \
 			    arch_timer_get_user, arch_timer_set_user, vis)
 
+#ifdef ARM64_S390_COMMON
 /*
  * Since reset() callback and field val are not used for idregs, they will be
  * used for specific purposes for idregs.
@@ -2840,6 +2862,8 @@ static bool bad_redir_trap(struct kvm_vcpu *vcpu,
 	.val = 0,				\
 }
 
+#endif /* ARM64_S390_COMMON */
+
 static bool access_sp_el1(struct kvm_vcpu *vcpu,
 			  struct sys_reg_params *p,
 			  const struct sys_reg_desc *r)
@@ -3212,6 +3236,7 @@ static void init_imp_id_regs(void)
 	boot_cpu_aidr_val = read_sysreg(aidr_el1);
 }
 
+#ifdef ARM64_S390_COMMON
 static u64 reset_imp_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	switch (reg_to_encoding(r)) {
@@ -3269,6 +3294,8 @@ static int set_imp_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
 	.val = mask,					\
 	}
 
+#endif /* ARM64_S390_COMMON */
+
 static u64 reset_mdcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	__vcpu_assign_sys_reg(vcpu, r->reg, vcpu->kvm->arch.nr_pmu_counters);
@@ -4833,6 +4860,7 @@ static const struct sys_reg_desc cp15_64_regs[] = {
 	{ SYS_DESC(SYS_AARCH32_CNTVCTSS),     access_arch_timer },
 };
 
+#ifdef ARM64_S390_COMMON
 static bool check_sysreg_table(const struct sys_reg_desc *table, unsigned int n,
 			       bool reset_check)
 {
@@ -4882,6 +4910,8 @@ static void perform_access(struct kvm_vcpu *vcpu,
 		kvm_incr_pc(vcpu);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /*
  * emulate_cp --  tries to match a sys_reg access in a handling table, and
  *                call the corresponding trap handler.
@@ -5340,6 +5370,7 @@ void kvm_sys_regs_create_debugfs(struct kvm *kvm)
 			    &sr_resx_fops);
 }
 
+#ifdef ARM64_S390_COMMON
 static void reset_vm_ftr_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *reg)
 {
 	u32 id = reg_to_encoding(reg);
@@ -5360,6 +5391,8 @@ static void reset_vcpu_ftr_id_reg(struct kvm_vcpu *vcpu,
 	reg->reset(vcpu, reg);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 /**
  * kvm_reset_sys_regs - sets system registers to reset value
  * @vcpu: The VCPU pointer
@@ -5432,6 +5465,7 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+#ifdef ARM64_S390_COMMON
 /******************************************************************************
  * Userspace API
  *****************************************************************************/

arch/arm64/kvm/sys_regs.c int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) · line 5794

Quoted 8 lines
arch/arm64/kvm/sys_regs.c +2 -0
@@ -5753,6 +5787,8 @@ int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
 	return write_demux_regids(uindices);
 }
 
+#endif /* ARM64_S390_COMMON */
+
 #define KVM_ARM_FEATURE_ID_RANGE_INDEX(r)			\
 	KVM_ARM_FEATURE_ID_RANGE_IDX(sys_reg_Op0(r),		\
 		sys_reg_Op1(r),					\

The way you pick and chose random helpers feels *VERY* wrong. I really want to understand the rationale for not using most of it, *including* the sysreg arrays as a whole. Everything in this file is (or at leats aims to be) architectural, and should be directly reused.

It should only be a matter of providing the required indirection to access the S390 representation of the arm64 state.

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#24 lore raw

PATCH v2 8/20 s390: tools: Allow sharing arm64/kvm headers

In reply to Steffen Eiden

In addition to sharing headers in arch/arm64/include/{asm, uapi/asm} also allow to share headers in arch/arm64/kvm which provide local KVM definitions.

---
 arch/s390/tools/Makefile.arm64h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/s390/tools/Makefile.arm64h +5 -2
index 069473ea335d..49f344636e83 100644
@@ -7,10 +7,11 @@ arm64api := $(objtree)/arch/$(ARCH)/include/generated/arm64
 arm64uapi := $(objtree)/arch/$(ARCH)/include/generated/uapi/arm64
 
 # List of ARM64 headers to share with s390 KVM
-# Format: [uapi:]header.h or header-gen.h
+# Format: [uapi:|kvm:]header.h or header-gen.h
 #
 # * header.h - from arch/arm64/include/asm/ to arm64/
 # * uapi:header.h - from arch/arm64/include/uapi/asm/ to uapi/arm64/
+# * kvm:header.h - from arch/arm64/kvm/ to arm64/
 # * header-gen.h - generated as header-gen.h from ARM64 header header.h
 # If a header contains partial markers (#ifdef ARM64_S390_COMMON) only this part will be copied.
 # If the file does not contain any markers the whole file will be copied.
@@ -37,7 +38,9 @@ arm64-type = $(firstword $(subst :, ,$(1)))
 arm64-base = $(or $(word 2,$(subst :, ,$(1))),$(1))
 arm64-src-base = $(patsubst %-gen.h,%.h,$(call arm64-base,$(1)))
 
-arm64-src = $(srctree)/arch/arm64/include/$(if $(filter uapi,$(call arm64-type,$(1))),uapi/)asm/$(call arm64-src-base,$(1))
+arm64-src = $(if $(filter kvm,$(call arm64-type,$(1))),\
+		$(srctree)/arch/arm64/kvm/$(call arm64-src-base,$(1)),\
+		$(srctree)/arch/arm64/include/$(if $(filter uapi,$(call arm64-type,$(1))),uapi/)asm/$(call arm64-src-base,$(1)))
 arm64-dst = $(if $(filter uapi,$(call arm64-type,$(1))),$(arm64uapi),$(arm64api))/$(call arm64-base,$(1))
 
 $(foreach hdr,$(ARM64_SHARED_HEADERS),\
Signature · 1 line
2.53.0
#25 lore raw

Re: [PATCH v2 08/20] s390: tools: Allow sharing arm64/kvm headers

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

In addition to sharing headers in arch/arm64/include/{asm, uapi/asm} also allow to share headers in arch/arm64/kvm which provide local KVM definitions.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
author reviewer
#26 lore raw

PATCH v2 9/20 s390: Introduce read/write ARM sysreg instructions

In reply to Steffen Eiden

Introduce Extract Arm System Register and Store Arm System Register to enable s390 hosts to read and write system registers for arm64 guests. The new instructions use the new RIE_H instruction format. Add assembler macros to create instructions in RIE_H format manually. Add Support for disassembling the new instructions.

---
 arch/s390/include/asm/sae-asm.h | 48 +++++++++++++++++++++++++++
 arch/s390/include/asm/sae.h     | 58 +++++++++++++++++++++++++++++++++
 arch/s390/kernel/dis.c          |  1 +
 arch/s390/tools/opcodes.txt     |  2 ++
 4 files changed, 109 insertions(+)
 create mode 100644 arch/s390/include/asm/sae-asm.h
arch/s390/include/asm/sae-asm.h new file +48 -0
new file mode 100644
index 000000000000..d81ed89eb4ed
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_S390_SAE_ASM_H
+#define __ASM_S390_SAE_ASM_H
+
+#ifdef __ASSEMBLER__
+
+.macro	GPR_NUM	opd gr
+	\opd = 255
+	.irp rs,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
+	.ifc \gr,%r\rs
+		\opd = \rs
+	.endif
+	.endr
+	.if \opd == 255
+		\opd = \gr
+	.endif
+.endm
+
+/*
+ * RIE_H - RIE-h instruction format
+ *
+ * RIE-h format: <insn> R1, R3, I2, M4
+ *    +--------+----+----+----+-----------------+----+--------+
+ *    | OpCode | R1 |////| R3 |        I2       | M4 | Opcode |
+ *    +--------+----+----+----+-----------------+----+--------+
+ *    0        8    12   16   20                36   40      47
+ */
+.macro RIE_H	opc, gr1, gr3, imm2, m4
+	GPR_NUM	r1, \gr1
+	GPR_NUM	r3, \gr3
+	.byte	(\opc & 0xff00) >> 8
+	.byte	r1 << 4
+	.byte	(r3 << 4) | ((\imm2 & 0xf000) >> 12)
+	.byte	((\imm2 & 0x0ff0) >> 4)
+	.byte	((\imm2 & 0x000f) << 4) | (\m4 & 0xf)
+	.byte	\opc & 0xff
+.endm
+
+.macro SASR r1, r3, i2, m4
+	RIE_H 0xed99, \r1, \r3, \i2, \m4,
+.endm
+
+.macro EASR r1, r3, i2, m4
+	RIE_H 0xed9b, \r1, \r3, \i2, \m4,
+.endm
+
+#endif /* __ASSEMBLER__ */
+#endif /* __ASM_S390_SAE_ASM_H */
arch/s390/include/asm/sae.h +58 -0

76 lines not on this page · raw message

arch/s390/kernel/dis.c +1 -0
index 1cec93895b3a..6ff8fd09d5bc 100644
@@ -208,6 +208,7 @@ static const unsigned char formats[][6] = {
 	[INSTR_RIE_RUI0]     = { R_8, I16_16, U4_12, 0, 0, 0 },
 	[INSTR_RIE_RUPI]     = { R_8, I8_32, U4_12, J16_16, 0, 0 },
 	[INSTR_RIE_RUPU]     = { R_8, U8_32, U4_12, J16_16, 0, 0 },
+	[INSTR_RIE_R0RIU]    = { R_8, R_16, U16_20, U4_36, 0, 0 },
 	[INSTR_RIL_RI]	     = { R_8, I32_16, 0, 0, 0, 0 },
 	[INSTR_RIL_RP]	     = { R_8, J32_16, 0, 0, 0, 0 },
 	[INSTR_RIL_RU]	     = { R_8, U32_16, 0, 0, 0, 0 },
arch/s390/tools/opcodes.txt +2 -0
index 0e4773c94af0..18af14071290 100644
@@ -1255,6 +1255,8 @@ ed64	ley	RXY_FRRD
 ed65	ldy	RXY_FRRD
 ed66	stey	RXY_FRRD
 ed67	stdy	RXY_FRRD
+ed99	sasr	RIE_R0RIU
+ed9b	easr	RIE_R0RIU
 eda8	czdt	RSL_LRDFU
 eda9	czxt	RSL_LRDFU
 edaa	cdzt	RSL_LRDFU
Signature · 1 line
2.53.0
#27 lore raw

Re: [PATCH v2 09/20] s390: Introduce read/write ARM sysreg instructions

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Casting the physical address 'sdo' to a virtual pointer in inline assembly constraints breaks…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Casting the physical address 'sdo' to a virtual pointer in inline assembly constraints breaks compiler alias analysis.
- [Medium] Speculative: Inconsistent physical vs virtual address usage for SAE hardware structures.
- [Medium] Speculative uninitialized memory leak if EASR fails and sets a condition code instead of a hard trap.
- [Low] Trailing comma in SASR and EASR assembler macros introduces an invalid empty argument.
- [Low] Silent bitwise truncation of the flags parameter in the RIE_H assembler macro.
--

commit 60f79315be3a103c198fd6153c59e6ac32172d24
Author: Steffen Eiden <[email protected]>

s390: Introduce read/write ARM sysreg instructions

Introduce Extract Arm System Register and Store Arm System Register to enable s390 hosts to read and write system registers for arm64 guests. The new instructions use the new RIE_H instruction format.

Quoted 4 lines
arch/s390/include/asm/sae-asm.h new file +0 -0
index 0000000000000..d81ed89eb4ed7

[ ... ]

Quoted 7 lines
arch/s390/include/asm/sae-asm.h new file +7 -0
+/*
+ * RIE_H - RIE-h instruction format
+ *
+ * RIE-h format: <insn> R1, R3, I2, M4
+ *    +--------+----+----+----+-----------------+----+--------+
+ *    | OpCode | R1 |////| R3 |        I2       | M4 | Opcode |
+ *    +--------+----+----+----+-----------------+----+--------+

arch/s390/include/asm/sae-asm.h line 35

Quoted 10 lines
arch/s390/include/asm/sae-asm.h new file +10 -0
+ *    0        8    12   16   20                36   40      47
+ */
+.macro RIE_H	opc, gr1, gr3, imm2, m4
+	GPR_NUM	r1, \gr1
+	GPR_NUM	r3, \gr3
+	.byte	(\opc & 0xff00) >> 8
+	.byte	r1 << 4
+	.byte	(r3 << 4) | ((\imm2 & 0xf000) >> 12)
+	.byte	((\imm2 & 0x0ff0) >> 4)
+	.byte	((\imm2 & 0x000f) << 4) | (\m4 & 0xf)

[Severity: Low]
Does this bitwise AND silently truncate flags exceeding 4 bits?

The inline functions sasr() and easr() use the "I" constraint for the flags parameter, which permits up to 8-bit unsigned constants (0-255). If a flag greater than or equal to 16 is passed, it will compile successfully but the upper bits will be silently discarded here.

arch/s390/include/asm/sae-asm.h line 40

Quoted 5 lines
arch/s390/include/asm/sae-asm.h new file +5 -0
+	.byte	\opc & 0xff
+.endm
+
+.macro SASR r1, r3, i2, m4
+	RIE_H 0xed99, \r1, \r3, \i2, \m4,

[Severity: Low]
Is this trailing comma intentional?

It passes 6 arguments to RIE_H, which only declares 5 parameters. In GNU Assembler, this creates an empty 6th argument, which can trigger a 'too many positional arguments' error.

arch/s390/include/asm/sae-asm.h line 44

Quoted 4 lines

+.endm

arch/s390/include/asm/sae-asm.h new file +3 -0
+
+.macro EASR r1, r3, i2, m4
+	RIE_H 0xed9b, \r1, \r3, \i2, \m4,

[Severity: Low]
Is this trailing comma intentional?

Like SASR above, this passes an empty 6th argument to RIE_H.

Quoted 6 lines

+.endm

arch/s390/include/asm/sae.h +0 -0
index 389c5ac2a36c0..6580536544eef 100644

[ ... ]

Quoted 4 lines
arch/s390/include/asm/sae.h +4 -0
+/**
+ * sasr() - Set Arm System Register
+ * @arm_reg: ARM system register identifier; compile-time constant
+ * @val: Value to set

arch/s390/include/asm/sae.h static inline void sae64a(struct kvm_sae_block *sae_block) · line 44

Quoted 10 lines
arch/s390/include/asm/sae.h +10 -0
+ * @save_area: Pointer to SAE save area
+ * @flags: Operation flags; compile-time constant
+ *
+ * Sets an ARM system register value.
+ */
+static __always_inline void sasr(unsigned int arm_reg, u64 val,
+				 struct kvm_sae_save_area *save_area,
+				 u64 flags)
+{
+	struct kvm_sae_save_area *sdo = (void *)save_area->sdo;

[Severity: High]
Does casting the physical address sdo to a virtual pointer without phys_to_virt() break compiler alias analysis?

The save_area->sdo field holds a physical address. Casting it directly to a virtual pointer and passing it to the inline assembly memory constraints below ("+m" (*sdo)) misinforms the compiler about which memory is accessed.

Since the compiler is given a pointer missing the identity base offset, it does not register that the actual virtual memory of sae_block is being modified, allowing it to incorrectly reorder C-level reads/writes or cache stale values in registers.

arch/s390/include/asm/sae.h static inline void sae64a(struct kvm_sae_block *sae_block) · line 51

Quoted 7 lines
arch/s390/include/asm/sae.h +7 -0
+	u16 reg = arm_reg >> _SAE_ASR_REG_SHIFT;
+
+	asm volatile (
+		"	SASR	%[r1],%[r3],%[i2],%[m4]\n"
+		: "+m" (*save_area), "+m" (*sdo)
+		: [r1] "d" (val),
+		  [r3] "a" (save_area), [i2] "K" (reg), [m4] "I" (flags)

[Severity: Medium]
Does the SASR instruction expect a physical address here?

The sae64a() function explicitly converts sae_block to a physical address before passing it to the hardware instruction, and save_area->sdo stores a physical address. However, this inline assembly passes the save_area pointer directly as a virtual address.

If SASR requires a physical address, passing a virtual address will cause a hardware addressing exception when KASLR is active.

Quoted 14 lines
arch/s390/include/asm/sae.h +14 -0
+	);
+}
+
+/**
+ * easr() - Extract Arm System Register
+ * @arm_reg: ARM system register identifier; compile-time constant
+ * @save_area: Pointer to SAE save area
+ * @flags: Operation flags; compile-time constant
+ *
+ * Reads an ARM system register value.
+ *
+ * Return: Register value
+ */
+static __always_inline u64 easr(unsigned int arm_reg,

arch/s390/include/asm/sae.h static inline void sae64a(struct kvm_sae_block *sae_block) · line 75

Quoted 10 lines
arch/s390/include/asm/sae.h +10 -0
+				const struct kvm_sae_save_area *save_area,
+				u64 flags)
+{
+	struct kvm_sae_save_area *sdo = (void *)save_area->sdo;
+	u16 reg = arm_reg >> _SAE_ASR_REG_SHIFT;
+	u64 val;
+
+	asm volatile(
+		"	EASR	%[r1],%[r3],%[i2],%[m4]\n"
+		: [r1] "=d"(val)

[Severity: Medium]
Can this leak uninitialized stack or register data if EASR fails?

If EASR sets a condition code to report failure instead of injecting a program check, the target register remains unmodified. Because the code unconditionally returns val without checking a condition code and does not initialize it, uninitialized memory could be returned.

Quoted 5 lines
arch/s390/include/asm/sae.h +5 -0
+		: "m"(*save_area),
+		  "m"(*sdo), [r3] "a"(save_area), [i2] "K"(reg), [m4] "I"(flags)
+	);
+	return val;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
author reviewer
#28 lore raw

PATCH v2 10/20 s390: Add functions to query arm guest time

In reply to Steffen Eiden

Add functions to convert between ARM guest time (LSB0) and s390 host time (MSB0) using new ptff function codes.

---
 arch/s390/include/asm/timex.h | 49 +++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
arch/s390/include/asm/timex.h +49 -0
index 23421b6eb225..e3a2b13022c7 100644
@@ -81,6 +81,8 @@ extern unsigned char ptff_function_mask[16];
 #define PTFF_QSI	0x02	/* query steering information */
 #define PTFF_QPT	0x03	/* query physical clock */
 #define PTFF_QUI	0x04	/* query UTC information */
+#define PTFF_QAGTO	0x10	/* query arm guest time offset */
+#define PTFF_QAGPT	0x11	/* query arm guest physical time offset */
 #define PTFF_ATO	0x40	/* adjust tod offset */
 #define PTFF_STO	0x41	/* set tod offset */
 #define PTFF_SFS	0x42	/* set fine steering rate */
@@ -118,6 +120,17 @@ struct ptff_qui {
 	unsigned int pad_0x5c[41];
 } __packed;
 
+/*
+ * Query Arm Guest Time
+ * used for:
+ *	- Query Arm Guest Time Offset
+ *	- Query Arm Guest Physical Time
+ */
+struct ptff_qagt {
+	u64 in;
+	u64 out;
+};
+
 /*
  * ptff - Perform timing facility function
  * @ptff_block: Pointer to ptff parameter block
@@ -266,4 +279,40 @@ static inline int tod_after_eq(unsigned long a, unsigned long b)
 	return a >= b;
 }
 
+/*
+ * ptff_qagto() -  Query Arm Guest Time Offset
+ *
+ * @physical_time: Arm guest physical time in MSb 0
+ *
+ * Converts Arm guest physical time in MSb 0 bit ordering
+ * into the Arm guest offset in LSb 0 bit ordering.
+ *
+ * Return: Arm guest time offset in LSb 0
+ */
+static inline u64 ptff_qagto(u64 physical_time)
+{
+	struct ptff_qagt qagto = { .in = physical_time };
+
+	ptff(&qagto, sizeof(qagto), PTFF_QAGTO);
+	return qagto.out;
+}
+
+/*
+ * ptff_qagpt() - Query Arm Guest Physical Time
+ *
+ * @guest_time_offset: Arm guest time offset in MSb 0
+ *
+ * Converts Arm guest offset in MSb 0 bit ordering
+ * into the Arm guest physical time in LSb 0 bit ordering.
+ *
+ * Return: Arm guest physical time in LSb 0
+ */
+static inline u64 ptff_qagpt(u64 guest_time_offset)
+{
+	struct ptff_qagt qagpt = { .in = guest_time_offset };
+
+	ptff(&qagpt, sizeof(qagpt), PTFF_QAGPT);
+	return qagpt.out;
+}
+
 #endif
Signature · 1 line
2.53.0
#29 lore raw

Re: [PATCH v2 10/20] s390: Add functions to query arm guest time

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

Add functions to convert between ARM guest time (LSB0) and s390 host time (MSB0) using new ptff function codes.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
author reviewer
#30 lore raw

PATCH v2 11/20 KVM: s390: arm64: Query Available Arm features

In reply to Steffen Eiden

QAAF enables s390 hosts to gain information about the support and handling of various arm features supported by the machine.

Function code 1 provides general information about available formats, machine defined content of system/id register, and other various information for running arm guests.

Function code 2 provides a initialised save area.

---
 arch/s390/include/asm/aef.h                  |  28 +++++
 arch/s390/include/asm/kvm_host_arm64_types.h | 111 +++++++++++++++++++
 arch/s390/include/asm/sae.h                  |  25 +++++
 arch/s390/kernel/Makefile                    |   2 +-
 arch/s390/kernel/aef.c                       | 101 +++++++++++++++++
 arch/s390/kvm/arm64/Makefile                 |   1 +
 arch/s390/kvm/arm64/arm.c                    |   3 +-
 arch/s390/kvm/arm64/qaaf.c                   |  69 ++++++++++++
 arch/s390/kvm/arm64/qaaf.h                   |  10 ++
 arch/s390/tools/opcodes.txt                  |   1 +
 10 files changed, 349 insertions(+), 2 deletions(-)
 create mode 100644 arch/s390/include/asm/aef.h
 create mode 100644 arch/s390/kernel/aef.c
 create mode 100644 arch/s390/kvm/arm64/qaaf.c
 create mode 100644 arch/s390/kvm/arm64/qaaf.h
arch/s390/include/asm/aef.h new file +28 -0
new file mode 100644
index 000000000000..2f29e3875d4e
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _ASM_S390_AEF_H
+#define _ASM_S390_AEF_H
+
+#include <asm/kvm_host_arm64_types.h>
+
+/**
+ * kvm_init_save_area() - Initialize Guest Save Area
+ * @save_area: Pointer to kvm_sae_save_area structure to initialize
+ *
+ * Context: Must be called before using the save area with lasrm/stiasrm instructions.
+ */
+void kvm_vcpu_init_save_area(struct kvm_sae_save_area *sa);
+struct qaaf_qmc_block *aef_qmc(void);
+
+struct aef_info {
+	unsigned long arm_guest_supp;
+	unsigned long sae_avail;
+	unsigned long ptff_avail;
+	unsigned long supp_state_desc_formats;
+	unsigned long supp_save_area_formats;
+	unsigned long max_num_vcpu;
+};
+
+const struct aef_info *aef_info(void);
+
+#endif /* _ASM_S390_AEF_H */
arch/s390/include/asm/kvm_host_arm64_types.h +111 -0

115 lines not on this page · raw message

arch/s390/include/asm/sae.h +25 -0
index 6580536544ee..f88d15b8f1e9 100644
@@ -111,5 +111,30 @@ static __always_inline void lasrm(struct kvm_sae_save_area *save_area)
 	);
 }
 
+/**
+ * qaaf() - Query Available Arm Features
+ * @gr0: QAAF function code, placed in greg 0
+ * @qaaf_block: Pointer to the page for the output
+ *
+ * Perform QAAF. The result ins written to qaaf_block.
+ */
+static __always_inline int qaaf(u64 gr0, union qaaf_block *qaaf_block)
+{
+	int exception = 1;
+
+	asm volatile(
+		"	lgr	0,%[r0]\n"
+		"	.insn	rre,0xb9ad0000,%[r1],0\n"
+		"0:	lhi	%[exc],0\n"
+		"1:\n"
+		EX_TABLE(0b, 1b)
+		: "=m"(*qaaf_block), [exc] "+d"(exception)
+		: [r1] "a"(qaaf_block), [r0] "d"(gr0)
+		: "r0"
+	);
+
+	return exception;
+}
+
 #endif /* !__ASSEMBLER__ */
 #endif /* __ASM_S390_SAE_H */
arch/s390/kernel/Makefile +1 -1
index f84b8fb41331..0f3b9fcd1885 100644
@@ -46,7 +46,7 @@ obj-y	+= runtime_instr.o cache.o fpu.o dumpstack.o guarded_storage.o sthyi.o
 obj-y	+= entry.o reipl.o kdebugfs.o alternative.o skey.o
 obj-y	+= nospec-branch.o ipl_vmparm.o machine_kexec_reloc.o unwind_bc.o
 obj-y	+= smp.o text_amode31.o stacktrace.o abs_lowcore.o facility.o uv.o wti.o
-obj-y	+= diag/
+obj-y	+= diag/ aef.o
 
 always-$(KBUILD_BUILTIN)	+= vmlinux.lds
 
arch/s390/kernel/aef.c new file +101 -0

101 lines not on this page · raw message

arch/s390/kvm/arm64/Makefile +1 -0
index ae614a62f7b5..577b0653a68e 100644
@@ -14,6 +14,7 @@ kvm-arm64-obj := \
 	inject_fault.o \
 	mmio.o \
 	mmu.o \
+	qaaf.o \
 	reset.o \
 
 kvm-arm64-obj += $(patsubst %.o,%-arm64.o,$(kvm-y))
arch/s390/kvm/arm64/arm.c +2 -1
index 2740ee3b703e..664fea5cf6b5 100644
@@ -12,6 +12,7 @@
 #include <linux/bitfield.h>
 
 #include <asm/access-regs.h>
+#include <asm/aef.h>
 
 #include <arm64/kvm_emulate.h>
 #include <arm64/sysreg.h>
@@ -744,7 +745,7 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
 static int __init kvm_s390_arm64_init(void)
 {
-	if (!sclp.has_aef)
+	if (!aef_info()->arm_guest_supp)
 		return -ENXIO;
 
 	return kvm_init_with_dev(sizeof(struct kvm_vcpu), 0, THIS_MODULE,
arch/s390/kvm/arm64/qaaf.c new file +69 -0

69 lines not on this page · raw message

arch/s390/kvm/arm64/qaaf.h new file +10 -0
new file mode 100644
index 000000000000..8fed7f795ce0
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef ARCH_S390_KVM_ARM64_QAAF_H
+#define ARCH_S390_KVM_ARM64_QAAF_H
+
+#include <asm/aef.h>
+
+u64 kvm_qaaf_read_ftr_reg(u32 id);
+
+#endif
arch/s390/tools/opcodes.txt +1 -0
index 18af14071290..fd5483107961 100644
@@ -600,6 +600,7 @@ b9a7	stiasrm	RRE_R0
 b9aa	lptea	RRF_RURR2
 b9ab	essa	RRF_U0RR
 b9ac	irbm	RRE_RR
+b9ad	qaaf	RRE_R0
 b9ae	rrbm	RRE_RR
 b9af	pfmf	RRE_RR
 b9b0	cu14	RRF_U0RR
Signature · 1 line
2.53.0
#31 lore raw

Re: [PATCH v2 11/20] KVM: s390: arm64: Query Available Arm features

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] The EX_TABLE entry in the `qaaf` inline assembly incorrectly places the exception fixup label…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] The EX_TABLE entry in the qaaf inline assembly incorrectly places the exception fixup label on the non-faulting lhi instruction instead of the potentially faulting .insn instruction, which will cause unhandled hardware exceptions to panic the kernel.
- [High] aef_sysfs_init() unconditionally calls aef_init_save_area(), failing to skip it when the AEF facility is not present, causing a crash on older hardware.
- [High] Off-by-two index error when accessing the QAAF registers array in kvm_qaaf_read_ftr_reg(), causing KVM to read incorrect memory offsets for all features.
- [High] If QAAF initialization fails, the global capability flag info.arm_guest_supp is not cleared, leading KVM to initialize using uninitialized hardware capability data.
--

commit a7cb3705acf7ea96c94b5937368fbc6655fb99e7
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Query Available Arm features

This commit introduces the QAAF (Query Available Arm Features) instruction support for s390 hosts. It allows KVM to query machine-supported ARM features and provides an initialized save area for running ARM guests.

Quoted 4 lines
arch/s390/include/asm/kvm_host_arm64_types.h +0 -0
index 2c922ef478d13..93fd649a26f47 100644

[ ... ]

Quoted 10 lines
arch/s390/include/asm/kvm_host_arm64_types.h +10 -0
+/* QAAF Query Model Capabilities */
+struct qaaf_qmc_block {
+	u64	_0000;			/* 0x0000 */
+	u8	ssdf;			/* 0x0008 */
+	u8	_0009;			/* 0x0009 */
+	u8	ssaf;			/* 0x000a */
+	u8	_000b[3];		/* 0x000b */
+	u16	maxncpu;		/* 0x000e */
+	u64	regs[0x1fe];		/* 0x0010 */
+} __aligned(PAGE_SIZE);

[ ... ]

Quoted 2 lines
arch/s390/include/asm/kvm_host_arm64_types.h +2 -0
+enum {
+	QAAF_REG_MIDR_EL1		= 0x02,

[ ... ]

Quoted 1 line

+};

[ ... ]

Quoted 4 lines
arch/s390/include/asm/sae.h +0 -0
index 6580536544eef..f88d15b8f1e90 100644

[ ... ]

Quoted 8 lines
arch/s390/include/asm/sae.h +8 -0
+static __always_inline int qaaf(u64 gr0, union qaaf_block *qaaf_block)
+{
+	int exception = 1;
+
+	asm volatile(
+		"	lgr	0,%[r0]\n"
+		"	.insn	rre,0xb9ad0000,%[r1],0\n"
+		"0:	lhi	%[exc],0\n"

arch/s390/include/asm/sae.h static __always_inline void lasrm(struct kvm_sae_save_area *save_area) · line 138

Quoted 10 lines
arch/s390/include/asm/sae.h +10 -0
+		"1:\n"
+		EX_TABLE(0b, 1b)
+		: "=m"(*qaaf_block), [exc] "+d"(exception)
+		: [r1] "a"(qaaf_block), [r0] "d"(gr0)
+		: "r0"
+	);
+
+	return exception;
+}
+

[Severity: Critical]
Does the exception table entry point to the wrong instruction?

The EX_TABLE(0b, 1b) targets the lhi instruction at label 0:, but the instruction that would actually fault is the preceding .insn instruction. If this executes on hardware without the AEF feature, won't this result in an unhandled hardware exception and a kernel panic?

[ ... ]

Quoted 5 lines
arch/s390/kernel/aef.c new file +0 -0
new file mode 100644
index 0000000000000..825e2643a8921

[ ... ]

Quoted 6 lines
arch/s390/kernel/aef.c new file +6 -0
+static int  __init aef_query_info(void)
+{
+	int ret;
+
+	if (IS_ENABLED(CONFIG_KVM_S390_ARM64)) {
+		info.sae_avail = sclp.has_aef;

arch/s390/kernel/aef.c line 74

Quoted 10 lines
arch/s390/kernel/aef.c new file +10 -0
+		info.ptff_avail = ptff_query(PTFF_QAGTO) &&
+				  ptff_query(PTFF_QAGPT);
+		info.arm_guest_supp = info.sae_avail && info.ptff_avail;
+	}
+	if (!info.sae_avail)
+		return 0;
+
+	ret = qaaf(QAAF_FC_QMC, (union qaaf_block *)&qmc);
+	if (ret)
+		return ret;

[Severity: High]
If the qaaf instruction fails and returns an error here, info.arm_guest_supp remains set to true.

Because aef_sysfs_init() is called via an arch_initcall, the initialization failure is ultimately ignored by the kernel. Later, kvm_s390_arm64_init() checks aef_info()->arm_guest_supp and will proceed to initialize KVM using the uninitialized qmc block.

Should info.arm_guest_supp be cleared before returning when qaaf() fails?

[ ... ]

Quoted 4 lines
arch/s390/kernel/aef.c new file +4 -0
+static int __init aef_sysfs_init(void)
+{
+	int rc = -ENOMEM;
+

arch/s390/kernel/aef.c line 96

Quoted 10 lines
arch/s390/kernel/aef.c new file +10 -0
+	if (!IS_ENABLED(CONFIG_KVM_S390_ARM64))
+		return 0;
+
+	rc = aef_query_info();
+	if (rc)
+		return rc;
+
+	rc = aef_init_save_area();
+	if (rc)
+		return rc;

[Severity: High]
Will this execute the new qaaf instruction unconditionally on all hardware?

aef_query_info() correctly returns 0 if the AEF facility is absent (since info.sae_avail would be false). However, aef_init_save_area() is called immediately after without checking if the facility is actually supported.

Could this trigger an operation exception on older hardware?

[ ... ]

arch/s390/kvm/arm64/qaaf.c line 7

Quoted 13 lines
arch/s390/kvm/arm64/qaaf.c new file +7 -0
new file mode 100644
index 0000000000000..c3124d38afc23
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/kvm_host.h>
+#include <asm/aef.h>
+#include "qaaf.h"
+
+#define _qaaf_reg_case(id) case SYS_##id: return aef_qmc()->regs[QAAF_REG_##id]

[Severity: High]
Does this index into the regs array using the wrong offset?

Looking at struct qaaf_qmc_block in asm/kvm_host_arm64_types.h, the regs array already starts at offset 0x0010:

    u64 regs[0x1fe];        /* 0x0010 */

However, the QAAF_REG_* enums represent the absolute doubleword offset from the start of the block (e.g., QAAF_REG_MIDR_EL1 is 0x02, which represents byte offset 0x0010).

By using the enum to index into the regs array, doesn't this double-apply the 0x0010 offset, causing KVM to read incorrect values for all feature registers?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
maintainer
#32 lore raw

Re: [PATCH v2 11/20] KVM: s390: arm64: Query Available Arm features

Quoted 6 lines · On 8/31/26 9:46 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] The EX_TABLE entry in the qaaf inline assembly incorrectly places the exception fixup label on the non-faulting lhi instruction instead of the potentially faulting .insn instruction, which will cause unhandled hardware exceptions to panic the kernel.
- [High] aef_sysfs_init() unconditionally calls aef_init_save_area(), failing to skip it when the AEF facility is not present, causing a crash on older hardware.
- [High] Off-by-two index error when accessing the QAAF registers array in kvm_qaaf_read_ftr_reg(), causing KVM to read incorrect memory offsets for all features.
- [High] If QAAF initialization fails, the global capability flag info.arm_guest_supp is not cleared, leading KVM to initialize using uninitialized hardware capability data.
--

[...]

Quoted 27 lines
Quoted 5 lines
arch/s390/kernel/aef.c new file +0 -0
new file mode 100644
index 0000000000000..825e2643a8921

[ ... ]

Quoted 6 lines
arch/s390/kernel/aef.c new file +6 -0
+static int  __init aef_query_info(void)
+{
+	int ret;
+
+	if (IS_ENABLED(CONFIG_KVM_S390_ARM64)) {
+		info.sae_avail = sclp.has_aef;

arch/s390/kernel/aef.c line 74

Quoted 10 lines
arch/s390/kernel/aef.c new file +10 -0
+		info.ptff_avail = ptff_query(PTFF_QAGTO) &&
+				  ptff_query(PTFF_QAGPT);
+		info.arm_guest_supp = info.sae_avail && info.ptff_avail;
+	}
+	if (!info.sae_avail)
+		return 0;
+
+	ret = qaaf(QAAF_FC_QMC, (union qaaf_block *)&qmc);
+	if (ret)
+		return ret;

[Severity: High]
If the qaaf instruction fails and returns an error here, info.arm_guest_supp remains set to true.

Quoted 6 lines

Because aef_sysfs_init() is called via an arch_initcall, the initialization failure is ultimately ignored by the kernel. Later, kvm_s390_arm64_init() checks aef_info()->arm_guest_supp and will proceed to initialize KVM using the uninitialized qmc block.

Should info.arm_guest_supp be cleared before returning when qaaf() fails?

No, we can instead check the sclp bit for sae_avail and early return before the assignments.
I don't see a gain in having this information shadowed considering the two patchsets that are floating around. Maybe you need it at a later time?

sae_avail is also used in patch #20 where it makes even less sense.
Either the machine and KVM have sae capabilities or we don't need the sysfs files. So why even add it as a file?

It's not like UV where we need to opt in, it's either supported or not. If it's not supported there's no need to have sysfs files.

Quoted 3 lines

[ ... ]

Quoted 1 line

+static int __init aef_sysfs_init(void)

aef_info_init()?

Quoted 24 lines

arch/s390/kernel/aef.c line 96

Quoted 13 lines
arch/s390/kernel/aef.c new file +13 -0
+{
+	int rc = -ENOMEM;
+
+	if (!IS_ENABLED(CONFIG_KVM_S390_ARM64))
+		return 0;
+
+	rc = aef_query_info();
+	if (rc)
+		return rc;
+
+	rc = aef_init_save_area();
+	if (rc)
+		return rc;

[Severity: High]
Will this execute the new qaaf instruction unconditionally on all hardware?

aef_query_info() correctly returns 0 if the AEF facility is absent (since info.sae_avail would be false). However, aef_init_save_area() is called immediately after without checking if the facility is actually supported.

Could this trigger an operation exception on older hardware?

author reviewer
#33 lore raw

Re: [PATCH v2 11/20] KVM: s390: arm64: Query Available Arm features

Quoted 42 lines · Janosch Frank
Quoted 6 lines · On 8/31/26 9:46 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] The EX_TABLE entry in the qaaf inline assembly incorrectly places the exception fixup label on the non-faulting lhi instruction instead of the potentially faulting .insn instruction, which will cause unhandled hardware exceptions to panic the kernel.
- [High] aef_sysfs_init() unconditionally calls aef_init_save_area(), failing to skip it when the AEF facility is not present, causing a crash on older hardware.
- [High] Off-by-two index error when accessing the QAAF registers array in kvm_qaaf_read_ftr_reg(), causing KVM to read incorrect memory offsets for all features.
- [High] If QAAF initialization fails, the global capability flag info.arm_guest_supp is not cleared, leading KVM to initialize using uninitialized hardware capability data.
--

[...]

Quoted 32 lines
Quoted 5 lines
arch/s390/kernel/aef.c new file +0 -0
new file mode 100644
index 0000000000000..825e2643a8921

[ ... ]

Quoted 6 lines
arch/s390/kernel/aef.c new file +6 -0
+static int  __init aef_query_info(void)
+{
+	int ret;
+
+	if (IS_ENABLED(CONFIG_KVM_S390_ARM64)) {
+		info.sae_avail = sclp.has_aef;

arch/s390/kernel/aef.c line 74

Quoted 10 lines
arch/s390/kernel/aef.c new file +10 -0
+		info.ptff_avail = ptff_query(PTFF_QAGTO) &&
+				  ptff_query(PTFF_QAGPT);
+		info.arm_guest_supp = info.sae_avail && info.ptff_avail;
+	}
+	if (!info.sae_avail)
+		return 0;
+
+	ret = qaaf(QAAF_FC_QMC, (union qaaf_block *)&qmc);
+	if (ret)
+		return ret;

[Severity: High]
If the qaaf instruction fails and returns an error here, info.arm_guest_supp remains set to true.

Because aef_sysfs_init() is called via an arch_initcall, the initialization failure is ultimately ignored by the kernel. Later, kvm_s390_arm64_init() checks aef_info()->arm_guest_supp and will proceed to initialize KVM using the uninitialized qmc block.

Quoted 6 lines
Quoted 1 line

Should info.arm_guest_supp be cleared before returning when qaaf() fails?

No, we can instead check the sclp bit for sae_avail and early return before the assignments.
I don't see a gain in having this information shadowed considering the two patchsets that are floating around. Maybe you need it at a later time?

I'll dump it into sysfs and use it in the init of kvm-arm64.

The main use is to have a single stop check in the sysfs to find out if arm-on-s390 is supported bu the machine. The check in init is just a second use.

Quoted 9 lines

sae_avail is also used in patch #20 where it makes even less sense. Either the machine and KVM have sae capabilities or we don't need the sysfs files. So why even add it as a file?

It's not like UV where we need to opt in, it's either supported or not. If it's not supported there's no need to have sysfs files.

The idea behind this is to provide some information on why kvm-arm64 is not available:
It might be because ptff has not all the supported calls or aef is not there. Both must be present.

Quoted 30 lines
Quoted 4 lines

[ ... ]

Quoted 1 line

+static int __init aef_sysfs_init(void)

aef_info_init()?

Quoted 22 lines

arch/s390/kernel/aef.c line 96

Quoted 13 lines
arch/s390/kernel/aef.c new file +13 -0
+{
+	int rc = -ENOMEM;
+
+	if (!IS_ENABLED(CONFIG_KVM_S390_ARM64))
+		return 0;
+
+	rc = aef_query_info();
+	if (rc)
+		return rc;
+
+	rc = aef_init_save_area();
+	if (rc)
+		return rc;

[Severity: High]
Will this execute the new qaaf instruction unconditionally on all hardware?

aef_query_info() correctly returns 0 if the AEF facility is absent (since info.sae_avail would be false). However, aef_init_save_area() is called immediately after without checking if the facility is actually supported.

Could this trigger an operation exception on older hardware?

FWIW: this is a valid finding.

Steffen

maintainer
#34 lore raw

Re: [PATCH v2 11/20] KVM: s390: arm64: Query Available Arm features

Quoted 17 lines · Steffen Eiden
Quoted 16 lines · Janosch Frank
Quoted 6 lines · On 8/31/26 9:46 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] The EX_TABLE entry in the qaaf inline assembly incorrectly places the exception fixup label on the non-faulting lhi instruction instead of the potentially faulting .insn instruction, which will cause unhandled hardware exceptions to panic the kernel.
- [High] aef_sysfs_init() unconditionally calls aef_init_save_area(), failing to skip it when the AEF facility is not present, causing a crash on older hardware.
- [High] Off-by-two index error when accessing the QAAF registers array in kvm_qaaf_read_ftr_reg(), causing KVM to read incorrect memory offsets for all features.
- [High] If QAAF initialization fails, the global capability flag info.arm_guest_supp is not cleared, leading KVM to initialize using uninitialized hardware capability data.
--

[...]

Quoted 6 lines
Quoted 5 lines
arch/s390/kernel/aef.c new file +0 -0
new file mode 100644
index 0000000000000..825e2643a8921

[ ... ]

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

arch/s390/kernel/aef.c line 68

Quoted 10 lines
arch/s390/kernel/aef.c new file +10 -0
+static int  __init aef_query_info(void)
+{
+	int ret;
+
+	if (IS_ENABLED(CONFIG_KVM_S390_ARM64)) {
+		info.sae_avail = sclp.has_aef;
+		info.ptff_avail = ptff_query(PTFF_QAGTO) &&
+				  ptff_query(PTFF_QAGPT);
+		info.arm_guest_supp = info.sae_avail && info.ptff_avail;
+	}

Wait, aef_sysfs_init() already early returns if !enabled. We'll never end up in this function if !enabled.

Generally I'd like to keep it similar to the kernel UV code. If QAAF is available create the sysfs files and provide qaaf data.

KVM support is separate from the QAAF data.
I'll have a second look tomorrow.

author reviewer
#35 lore raw

PATCH v2 12/20 KVM: s390: arm64: Implement feature sanitisation

In reply to Steffen Eiden

Implement feature detection and sanitization for arm64 KVM guests on s390.

Query hardware capabilities using QAAF instruction and sanitize arm64 ID registers. Provide accessor functions for queried capabilities and integrate feature detection into module initialization sequence.

---
 arch/s390/include/asm/kvm_host_arm64.h |  13 ++
 arch/s390/kvm/arm64/Makefile           |   1 +
 arch/s390/kvm/arm64/arm.c              |   5 +-
 arch/s390/kvm/arm64/feature.c          | 165 +++++++++++++++++++++++++
 arch/s390/kvm/arm64/feature.h          |  87 +++++++++++++
 arch/s390/kvm/arm64/qaaf.c             |   7 +-
 arch/s390/kvm/arm64/qaaf.h             |   1 +
 7 files changed, 277 insertions(+), 2 deletions(-)
 create mode 100644 arch/s390/kvm/arm64/feature.c
 create mode 100644 arch/s390/kvm/arm64/feature.h
arch/s390/include/asm/kvm_host_arm64.h +13 -0
index b8a689c649bd..22e4bf788fa2 100644
@@ -14,6 +14,19 @@
 
 #include <asm/sae.h>
 
+enum {
+	ARM64_HAS_WFXT,
+	ARM64_HAS_RASV1P1_EXTN,
+	ARM64_HAS_STAGE2_FWB,
+	ARM64_HAS_RAS_EXTN,
+	ARM64_HAS_EVT,
+	ARM64_HAS_HCR_NV1,
+	ARM64_MISMATCHED_CACHE_TYPE,
+	ARM64_HAS_ECV_CNTPOFF,
+};
+
+bool cpus_have_final_cap(unsigned int num);
+
 #define KVM_HAVE_MMU_RWLOCK
 #define KVM_MAX_VCPUS 1
 #define KVM_S390_ARM64_IMPL_FEATURES \
arch/s390/kvm/arm64/Makefile +1 -0
index 577b0653a68e..8c7110956573 100644
@@ -9,6 +9,7 @@ ccflags-y += -I $(src) -I $(objtree)/$(obj) -I$(srctree)/arch/s390/kvm/gmap -DKV
 
 kvm-arm64-obj := \
 	arm.o \
+	feature.o \
 	guest.o \
 	handle_exit.o \
 	inject_fault.o \
arch/s390/kvm/arm64/arm.c +4 -1
index 664fea5cf6b5..c573a3da5766 100644
@@ -22,12 +22,13 @@
 
 #include "arm.h"
 #include "handle_exit.h"
+#include "qaaf.h"
+#include "feature.h"
 
 #define CREATE_TRACE_POINTS
 #include "trace.h"
 
 static unsigned long system_supported_vcpu_features(void);
-#define read_sanitised_ftr_reg(_id) 0
 
 #define __INCL_GEN_ARM_FILE
 #include "generated/arm.inc"
@@ -748,6 +749,8 @@ static int __init kvm_s390_arm64_init(void)
 	if (!aef_info()->arm_guest_supp)
 		return -ENXIO;
 
+	kvm_init_qaaf();
+
 	return kvm_init_with_dev(sizeof(struct kvm_vcpu), 0, THIS_MODULE,
 				 KVM_DEV_NAME, MISC_DYNAMIC_MINOR);
 }
arch/s390/kvm/arm64/feature.c new file +165 -0

165 lines not on this page · raw message

arch/s390/kvm/arm64/feature.h new file +87 -0

87 lines not on this page · raw message

arch/s390/kvm/arm64/qaaf.c +6 -1
index c3124d38afc2..228e9cdcd684 100644
@@ -3,9 +3,14 @@
 #include <linux/kvm_host.h>
 #include <asm/aef.h>
 #include "qaaf.h"
+#include "feature.h"
 
-#define _qaaf_reg_case(id) case SYS_##id: return aef_qmc()->regs[QAAF_REG_##id]
+void __init kvm_init_qaaf(void)
+{
+	kvm_arm_host_sanitize_features(aef_qmc());
+}
 
+#define _qaaf_reg_case(id) case SYS_##id: return aef_qmc()->regs[QAAF_REG_##id]
 u64 kvm_qaaf_read_ftr_reg(u32 id)
 {
 	switch (id) {
arch/s390/kvm/arm64/qaaf.h +1 -0
index 8fed7f795ce0..87042f81e882 100644
@@ -5,6 +5,7 @@
 
 #include <asm/aef.h>
 
+__init void kvm_init_qaaf(void);
 u64 kvm_qaaf_read_ftr_reg(u32 id);
 
 #endif
Signature · 1 line
2.53.0
#36 lore raw

Re: [PATCH v2 12/20] KVM: s390: arm64: Implement feature sanitisation

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] Hardware Feature Mis-mapping: ARM64_HAS_ECV_CNTPOFF maps to PAN instead of ECV - [High] Hardware…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] Hardware Feature Mis-mapping: ARM64_HAS_ECV_CNTPOFF maps to PAN instead of ECV
- [High] Hardware Feature Mis-mapping: ARM64_HAS_HCR_NV1 fails to account for CPUs that remove NV1
- [High] Incomplete Feature Check: ARM64_HAS_RASV1P1_EXTN omits the primary fallback check
- [High] Feature Leakage: MASK_RESERVED uses a blacklist approach, leaking unknown features to the guest
- [Low] Undefined reference to __qaaf_qmp causing potential linker errors
- [Low] Integer overflow in kvm_sae_max_vcpus due to u16 return type
--

commit ea4286d0301a7cdfe0535f60a7c1ca1b65c64103
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Implement feature sanitisation

Implement feature detection and sanitization for arm64 KVM guests on s390. Queries hardware capabilities using the QAAF instruction and provides accessor functions for capability mapping.

Quoted 6 lines
arch/s390/kvm/arm64/feature.c new file +0 -0
new file mode 100644
index 0000000000000..39c8ae7e951cf
@@ -0,0 +1,165 @@

[ ... ]

arch/s390/kvm/arm64/feature.c line 17

Quoted 5 lines
arch/s390/kvm/arm64/feature.c new file +5 -0
+#define MASK_RESERVED(_qaafp, _id)				\
+	({							\
+		u64 *_reg = &((_qaafp)->regs[QAAF_REG_##_id]);	\
+		*_reg = (*_reg & ~_id##_RES0) | _id##_RES1;	\
+	})

[Severity: High]
Does this blacklist approach inadvertently leak unknown features to the guest?

By clearing only the explicitly unarchitected RES0 bits, any valid CPU features that are not recognized or handled by KVM will be passed through unconditionally. This goes against the KVM invariants that new guest-visible features should default off, which could allow a guest to use unsupported features and lead to hardware traps or hypervisor state corruption.

[ ... ]

Quoted 5 lines
arch/s390/kvm/arm64/feature.c new file +5 -0
+bool cpus_have_final_cap(unsigned int num)
+{
+	u64 reg_val;
+
+	switch (num) {

[ ... ]

arch/s390/kvm/arm64/feature.c line 138

Quoted 4 lines
arch/s390/kvm/arm64/feature.c new file +4 -0
+	case ARM64_HAS_RASV1P1_EXTN:
+		reg_val = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
+		return SYS_FIELD_GET(ID_AA64PFR1_EL1, RAS_frac, reg_val) ==
+		       ID_AA64PFR1_EL1_RAS_frac_RASv1p1;

[Severity: High]
Is this missing the primary check for the RAS extension?

The check looks exclusively at the fractional extension field RAS_frac in ID_AA64PFR1_EL1, but ignores the primary ID_AA64PFR0_EL1.RAS field. On modern CPUs that correctly advertise V1P1 support in ID_AA64PFR0_EL1, this will return false and break feature discovery.

arch/s390/kvm/arm64/feature.c line 143

Quoted 5 lines
arch/s390/kvm/arm64/feature.c new file +5 -0
+
+	case ARM64_HAS_HCR_NV1:
+		reg_val = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
+		return SYS_FIELD_GET(ID_AA64MMFR2_EL1, NV, reg_val) >=
+		       ID_AA64MMFR2_EL1_NV_IMP;

[Severity: High]
Will this misidentify NV2-only systems as having NV1 support?

This check verifies if Nested Virtualization is implemented but ignores the ID_AA64MMFR4_EL1.E2H0 field. On newer hosts that implement NV2 but explicitly remove NV1 (such as the Apple M2 series), this function will return true and attempt to use unsupported NV1 features.

[ ... ]

arch/s390/kvm/arm64/feature.c line 158

Quoted 4 lines
arch/s390/kvm/arm64/feature.c new file +4 -0
+	case ARM64_HAS_ECV_CNTPOFF:
+		reg_val = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
+		return SYS_FIELD_GET(ID_AA64MMFR1_EL1, PAN, reg_val) >=
+		       ID_AA64MMFR1_EL1_PAN_IMP;

[Severity: High]
Does this check query the wrong register and field for Enhanced Counter Virtualization (ECV)?

It is querying ID_AA64MMFR1_EL1.PAN instead of ID_AA64MMFR0_EL1.ECV. Because PAN is widely supported on modern ARM64 CPUs, KVM will incorrectly assume ECV is present, potentially causing undefined instruction exceptions or timer misbehavior if those features are used.

Quoted 6 lines
arch/s390/kvm/arm64/feature.h new file +0 -0
new file mode 100644
index 0000000000000..9c902df1f26c6
@@ -0,0 +1,87 @@

[ ... ]

arch/s390/kvm/arm64/feature.h line 45

Quoted 6 lines
arch/s390/kvm/arm64/feature.h new file +6 -0
+static inline u32 kvm_sae_supported_sd_formats(void)
+{
+	extern struct qaaf_qmc_block __qaaf_qmp;
+
+	return __qaaf_qmp.ssdf;
+}

[Severity: Low]
Is this referencing an undefined symbol?

The symbol __qaaf_qmp does not exist anywhere in the kernel (the actual structure seems to be exposed via aef_qmc()). While this inline function is currently dead code, calling it will cause a linker error.

[ ... ]

arch/s390/kvm/arm64/feature.h line 71

Quoted 7 lines
arch/s390/kvm/arm64/feature.h new file +7 -0
+static inline u16 kvm_sae_max_vcpus(void)
+{
+	extern struct qaaf_qmc_block __qaaf_qmp;
+
+	/* QAAF QMP reports the max id not the max num */
+	return __qaaf_qmp.maxncpu + 1;
+}

[Severity: Low]
Can this result in an integer overflow?

The maxncpu field can reach 0xFFFF. Returning maxncpu + 1 cast to a u16 will overflow the 16-bit integer boundary and wrap around to 0. (Also, as noted above, __qaaf_qmp is an unresolved symbol).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
author reviewer
#37 lore raw

PATCH v2 13/20 KVM: s390: arm64: Implement arm sysreg managing infrastructure

In reply to Steffen Eiden

Implement the infrastructures for the s390 host to read and write the arm64 guest system register. The host can access and modify (non-id) system registers through the new instructions EASR/SASR or their multiple variant. Guest accesses to ID registers are trapped and handled by KVM. KVM gets the data form the result of QAAF and sanitizes it using the shared infrastructure with arm64.

---
 arch/s390/include/arm64/kvm_hyp.h      |  18 +
 arch/s390/include/arm64/kvm_nested.h   |   5 +
 arch/s390/include/arm64/sys_regs.h     |   9 +
 arch/s390/include/asm/kvm_host_arm64.h | 204 ++++++-
 arch/s390/kvm/arm64/Makefile           |   2 +
 arch/s390/kvm/arm64/Makefile.gen       |   1 +
 arch/s390/kvm/arm64/spectre.c          |  14 +
 arch/s390/kvm/arm64/sys_regs.c         | 750 +++++++++++++++++++++++++
 arch/s390/kvm/arm64/trace.h            |  51 ++
 arch/s390/tools/Makefile.arm64h        |   4 +
 10 files changed, 1057 insertions(+), 1 deletion(-)
 create mode 100644 arch/s390/include/arm64/kvm_hyp.h
 create mode 100644 arch/s390/include/arm64/sys_regs.h
 create mode 100644 arch/s390/kvm/arm64/spectre.c
 create mode 100644 arch/s390/kvm/arm64/sys_regs.c
arch/s390/include/arm64/kvm_hyp.h new file +18 -0
new file mode 100644
index 000000000000..e425b7c3c80d
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __S390_ARM64_KVM_HYP_H__
+#define __S390_ARM64_KVM_HYP_H__
+
+#include <linux/compiler.h>
+
+static __always_inline bool has_vhe(void)
+{
+	return true;
+}
+
+static __always_inline bool is_protected_kvm_enabled(void)
+{
+	return false;
+}
+
+#endif /* __S390_ARM64_KVM_HYP_H__ */
arch/s390/include/arm64/kvm_nested.h +5 -0
index e950b1a10c41..9c02ac5c62cc 100644
@@ -8,4 +8,9 @@ static inline bool vcpu_has_nv(const struct kvm_vcpu *vcpu)
 	return false;
 }
 
+static inline u64 limit_nv_id_reg(struct kvm *kvm, u32 id, u64 val)
+{
+	return val;
+}
+
 #endif /* ASM_KVM_NESTED_H */
arch/s390/include/arm64/sys_regs.h new file +9 -0
new file mode 100644
index 000000000000..dafb090ee1af
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __S390_ARM64_SYS_REGS_H__
+#define __S390_ARM64_SYS_REGS_H__
+
+#include <arm64/cputype.h>
+#include <arm64/sys_regs-gen.h>
+
+#endif /* __S390_ARM64_SYS_REGS_H__ */
arch/s390/include/asm/kvm_host_arm64.h +203 -1

247 lines not on this page · raw message

arch/s390/kvm/arm64/Makefile +2 -0
index 8c7110956573..e19040cdc27d 100644
@@ -17,6 +17,8 @@ kvm-arm64-obj := \
 	mmu.o \
 	qaaf.o \
 	reset.o \
+	spectre.o \
+	sys_regs.o \
 
 kvm-arm64-obj += $(patsubst %.o,%-arm64.o,$(kvm-y))
 kvm-arm64-obj += $(patsubst %.o,%-arm64.o,$(gmap-y))
arch/s390/kvm/arm64/Makefile.gen +1 -0
index 6ac7602b57cb..99298dc49dcc 100644
@@ -8,6 +8,7 @@ ARM64_CFILES := \
 	arm.c \
 	guest.c \
 	handle_exit.c \
+	sys_regs.c \
 	mmio.c \
 
 quiet_cmd_extract_inc = GEN     $@
arch/s390/kvm/arm64/spectre.c new file +14 -0
new file mode 100644
index 000000000000..dfe5b4be54d6
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <arm64/spectre.h>
+
+enum mitigation_state arm64_get_spectre_v2_state(void)
+{
+	return SPECTRE_UNAFFECTED;
+}
+
+enum mitigation_state arm64_get_meltdown_state(void)
+{
+	return SPECTRE_UNAFFECTED;
+}
+
arch/s390/kvm/arm64/sys_regs.c new file +750 -0

750 lines not on this page · raw message

arch/s390/kvm/arm64/trace.h +51 -0

63 lines not on this page · raw message

arch/s390/tools/Makefile.arm64h +4 -0
index 49f344636e83..28103cdc243c 100644
@@ -17,12 +17,16 @@ arm64uapi := $(objtree)/arch/$(ARCH)/include/generated/uapi/arm64
 # If the file does not contain any markers the whole file will be copied.
 ARM64_SHARED_HEADERS := \
 	brk-imm.h \
+	cache.h \
+	cputype.h \
 	esr.h \
 	kvm_arm.h \
 	kvm_emulate-gen.h \
 	kvm_host.h \
 	ptrace-gen.h \
 	sysreg-gen.h \
+	spectre.h \
+	kvm:sys_regs-gen.h \
 	uapi:hwcap.h \
 	uapi:kvm.h \
 	uapi:ptrace.h \
Signature · 1 line
2.53.0
#38 lore raw

Re: [PATCH v2 13/20] KVM: s390: arm64: Implement arm sysreg managing infrastructure

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] The s390-specific definition of `__vcpu_rmw_sys_reg` incorrectly swaps the register ID (`R`)…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] The s390-specific definition of __vcpu_rmw_sys_reg incorrectly swaps the register ID (R) and value (V) parameters compared to the shared ARM64 implementation.
- [Critical] Time-of-Check to Time-of-Use (TOCTOU) race with vCPU preemption and illegal use of smp_processor_id() in a preemptible context causes a host BUG_ON panic.
- [High] Missing trap handler for MIDR_EL1 causes guest crashes with an UNDEF exception on boot.
- [High] Guest-reachable WARN(true) in kvm_handle_sys_reg allows a malicious guest to spam the host kernel log.
--

commit 1e6a91a5668b6ea306fd97aa5d57b2154d91120f
Author: Steffen Eiden <[email protected]>
KVM: s390: arm64: Implement arm sysreg managing infrastructure

This commit implements the infrastructure for the s390 host to manage arm64 guest system registers. It allows the host to read and write non-ID system registers using new EASR/SASR instructions, while trapping and handling guest accesses to ID registers.

Quoted 8 lines
arch/s390/include/asm/kvm_host_arm64.h +1 -0
@@ -221,4 +244,183 @@ static inline void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu)
 
 #define kvm_supports_32bit_el0() false
 
+#define __vcpu_sys_reg(__vcpu, __reg) \

arch/s390/include/asm/kvm_host_arm64.h static inline void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu) · line 257

Quoted 10 lines
arch/s390/include/asm/kvm_host_arm64.h +10 -0
+	vcpu_read_sys_reg(__vcpu, __reg)
+
+#define __vcpu_assign_sys_reg(__vcpu, __reg, __val) \
+	vcpu_write_sys_reg(__vcpu, __val, __reg)
+
+#define __vcpu_rmw_sys_reg(C, V, OP, R)		\
+({						\
+	u64 __val = vcpu_read_sys_reg(C, R);	\
+	__val OP V;				\
+	vcpu_write_sys_reg(C, __val, R);	\

+})

[Severity: Critical]
Does this override transpose the register ID and value arguments compared to the shared ARM64 implementation?

The shared ARM64 code expects this macro to receive (vcpu, reg, op, val). This s390 override defines it as (C, V, OP, R). Any read-modify-write system register operation using this macro (for instance, PMU updates) will attempt to read from a register ID equal to the bitmask value, and use the actual register ID as the bitmask, causing data corruption and host crashes.

Quoted 22 lines
arch/s390/include/asm/kvm_host_arm64.h +22 -0
+/**
+ * _vcpu_read_sys_reg() - read a guest sysreg with easr
+ * - R - sysreg id; must be readable by easr; must be compile time constant
+ *
+ *   if SYSREGS_ON_CPU: proceed with flags = 0
+ *   otherwise:         proceed with either
+ *                         read:  flags = EASR_FLAG_SA
+ *                         write: flags = SASR_FLAG_INITIALIZED
+ *
+ */
+#define _vcpu_read_sys_reg(C, R) \
+	({	BUILD_BUG_ON(!__builtin_constant_p((R))); \
+		BUG_ON(vcpu_is_loaded(C) && smp_processor_id() != (C)->cpu); \
+		(vcpu_is_loaded(C)) \
+			? __vcpu_read_sr((C), (R), 0) \
+			: __vcpu_read_sr((C), (R), EASR_FLAG_SA); })
+
+/**
+ * _vcpu_write_sys_reg() - write a guest sysreg with sasr
+ * - R - sysreg id; must be readable by sasr; must be compile time constant
+
+ *   if SYSREGS_ON_CPU: proceed with flags = 0

arch/s390/include/asm/kvm_host_arm64.h static inline void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu) · line 291

Quoted 10 lines
arch/s390/include/asm/kvm_host_arm64.h +10 -0
+ *   otherwise:         proceed with either
+ *                         read:  flags = EASR_FLAG_SA
+ *                         write: flags = SASR_FLAG_INITIALIZED
+ */
+#define _vcpu_write_sys_reg(C, V, R) \
+	({	BUILD_BUG_ON(!__builtin_constant_p((R))); \
+		BUG_ON(vcpu_is_loaded(C) && smp_processor_id() != (C)->cpu); \
+		(vcpu_is_loaded(C)) \
+			? __vcpu_write_sr((C), (V), (R), 0) \
+			: __vcpu_write_sr((C), (V), (R), SASR_FLAG_INITIALIZED); })

[Severity: Critical]
Is it safe to call smp_processor_id() in these macros?

These macros are invoked by kvm_handle_sys_reg(), which executes in a preemptible context. Calling smp_processor_id() here will trigger a kernel warning.

Furthermore, if the vCPU thread is preempted and migrated between evaluating smp_processor_id() and checking (C)->cpu, this BUG_ON() will panic the host. A guest could repeatedly trigger sysreg exits to intentionally cause a host denial of service.

[ ... ]

Quoted 4 lines
arch/s390/kvm/arm64/sys_regs.c new file +0 -0
@@ -0,0 +1,750 @@

[ ... ]

arch/s390/kvm/arm64/sys_regs.c line 467

Quoted 13 lines
arch/s390/kvm/arm64/sys_regs.c new file +13 -0
+static const struct sys_reg_desc sys_reg_descs[] = {
+	/* Op0 = 2 */
+	{ SYS_DESC(SYS_OSLAR_EL1), trap_oslar_el1 },
+	{ SYS_DESC(SYS_OSLSR_EL1), trap_oslsr_el1, reset_oslsr_el1, OSLSR_EL1,
+	  OSLSR_EL1_OSLM_IMPLEMENTED, NULL, set_oslsr_el1 },
+
+	/* Op0 = 3 */
+	/* Op1 = 0 */
+	/* CRn = 0 */
+	/* CRm = 0 */
+
+	{ SYS_DESC(SYS_MIDR_EL1), NULL, reset_midr, 0, GENMASK_ULL(31, 0),
+	  get_id_reg, set_imp_id_reg },

[Severity: High]
Does this missing access handler cause guest boot failures?

The access handler here is set to NULL instead of access_imp_id_reg. When a guest reads MIDR_EL1 (which Linux does early in boot), kvm_handle_sys_reg() uses perform_access(). Since the handler is NULL, this will fall back to bad_trap(), injecting an undefined instruction exception into the guest and printing a WARN_ONCE on the host.

[ ... ]

Quoted 7 lines
arch/s390/kvm/arm64/sys_regs.c new file +7 -0
+int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
+{
+	const struct sys_reg_desc *desc = NULL;
+	struct sys_reg_params params;
+	unsigned long esr = kvm_vcpu_get_esr(vcpu);
+	int Rt = kvm_vcpu_sys_get_rt(vcpu);
+

arch/s390/kvm/arm64/sys_regs.c line 704

Quoted 10 lines
arch/s390/kvm/arm64/sys_regs.c new file +10 -0
+	trace_kvm_handle_sys_reg(esr);
+
+	params = esr_sys64_to_params(esr);
+	params.regval = vcpu_get_reg(vcpu, Rt);
+
+	/* System registers have Op0=={2,3}, as per DDI487 J.a C5.1.2 */
+	if (params.Op0 == 2 || params.Op0 == 3)
+		desc = find_reg(&params, sys_reg_descs, num_sys_reg_descs);
+	else
+		WARN(true, "system instruction handling not supported");

[Severity: High]
Can a malicious guest trigger this warning to flood the host kernel log?

If a guest intentionally executes a trapped cache maintenance instruction (like DC CIVAC), it generates an exception with Op0 == 1. This hits the unconditional WARN() here, violating KVM guidelines against guest-reachable assertions and allowing a denial of service via log spam.

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
author reviewer
#39 lore raw

PATCH v2 14/20 KVM: s390: arm64: Integrate sysreg into the host

In reply to Steffen Eiden

Integrate the previously established system register managing infrastructure into the host. Initialize sysregs during vcpu init. Finalize sysregs on PID change, Initialize sysreg table on kvm init, report availability to userspace and let userspace read&modify guest system register.

---
 arch/s390/include/asm/kvm_host_arm64.h |  3 ++
 arch/s390/kvm/arm64/arm.c              | 27 +++++++++++++++++
 arch/s390/kvm/arm64/guest.c            | 12 +++++---
 arch/s390/kvm/arm64/handle_exit.c      |  1 +
 arch/s390/kvm/arm64/reset.c            | 42 ++++++++++++++++++++++++++
 5 files changed, 81 insertions(+), 4 deletions(-)
arch/s390/include/asm/kvm_host_arm64.h +3 -0
index 65e5e0c48b07..0bf396ef54c2 100644
@@ -156,6 +156,9 @@ struct kvm_arch {
 
 	/* Per-VM ID register storage */
 	struct kvm_vm_id_regs id_regs;
+
+	/* The EPD as it should be on all VCPUs. */
+	u64 epd;
 };
 
 static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature)
arch/s390/kvm/arm64/arm.c +27 -0
index c573a3da5766..3a0bba6e0a06 100644
@@ -16,6 +16,7 @@
 
 #include <arm64/kvm_emulate.h>
 #include <arm64/sysreg.h>
+#include <arm64/sys_regs.h>
 
 #include <gmap.h>
 #include <kvm_mmu.h>
@@ -136,6 +137,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 		goto out_err;
 	kvm->arch.mem_limit = kvm->arch.guest_phys_size;
 
+	kvm->arch.epd = ptff_qagto(0);
+
 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
 	return 0;
 
@@ -191,6 +194,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	sae_block->mso = 0L;
 	sae_block->msl = vcpu->kvm->arch.mem_limit;
 	sae_block->save_area = virt_to_phys(save_area);
+	sae_block->gpto = vcpu->kvm->arch.epd;
 
 	VM_EVENT(vcpu->kvm, 3,
 		 "create cpu %d at 0x%p, sae block at 0x%p, satellite at 0x%p",
@@ -259,6 +263,7 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
 int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
 {
 	struct kvm *kvm = vcpu->kvm;
+	int ret;
 
 	if (!kvm_vcpu_initialized(vcpu))
 		return -ENOEXEC;
@@ -272,6 +277,10 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
 	scoped_guard(mutex, &kvm->arch.config_lock)
 		set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
 
+	ret = kvm_finalize_sys_regs(vcpu);
+	if (ret)
+		return ret;
+
 	return 0;
 }
 
@@ -390,10 +399,20 @@ static void arm_vcpu_run(struct kvm_vcpu *vcpu)
 	guest_state_enter_irqoff();
 	local_irq_enable();
 
+	vcpu_write_host_sys_reg(vcpu, vcpu->arch.hcr_elz, SYS_HCR_EL2);
+	vcpu_write_host_sys_reg(vcpu, vcpu->arch.hcrx_elz, SYS_HCRX_EL2);
+	vcpu_write_host_sys_reg(vcpu, vcpu->arch.mpidr, SYS_VMPIDR_EL2);
+
+	_vcpu_write_sys_reg(vcpu, vcpu->arch.ctxt.elr_el1, SYS_ELR_EL1);
+	_vcpu_write_sys_reg(vcpu, vcpu->arch.ctxt.spsr_el1, SYS_SPSR_EL1);
+
 	sae_block->icptr = 0;
 
 	sae64a(sae_block);
 
+	vcpu->arch.ctxt.elr_el1 = _vcpu_read_sys_reg(vcpu, SYS_ELR_EL1);
+	vcpu->arch.ctxt.spsr_el1 = _vcpu_read_sys_reg(vcpu, SYS_SPSR_EL1);
+
 	local_irq_disable();
 	guest_state_exit_irqoff();
 	guest_timing_exit_irqoff();
@@ -746,11 +765,19 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
 static int __init kvm_s390_arm64_init(void)
 {
+	int ret;
+
 	if (!aef_info()->arm_guest_supp)
 		return -ENXIO;
 
 	kvm_init_qaaf();
 
+	ret = kvm_sys_reg_table_init();
+	if (ret) {
+		kvm_info("Error initializing system register tables");
+		return ret;
+	}
+
 	return kvm_init_with_dev(sizeof(struct kvm_vcpu), 0, THIS_MODULE,
 				 KVM_DEV_NAME, MISC_DYNAMIC_MINOR);
 }
arch/s390/kvm/arm64/guest.c +8 -4
index 8b5ee9e79a4f..047460975936 100644
@@ -4,6 +4,7 @@
 
 #include <arm64/kvm_emulate.h>
 #include <arm64/kvm_nested.h>
+#include <arm64/sys_regs.h>
 
 #define SVE_VQ_MIN	__SVE_VQ_MIN
 #define SVE_NUM_ZREGS	KVM_ARM64_SVE_NUM_ZREGS
@@ -60,12 +61,15 @@ int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
 		return ret;
 	uindices += ret;
 
-	return 0;
+	return kvm_arm_copy_sys_reg_indices(vcpu, uindices);
 }
 
 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
 {
-	return num_core_regs(vcpu);
+	unsigned long num = num_core_regs(vcpu);
+
+	num += kvm_arm_num_sys_reg_descs(vcpu);
+	return num;
 }
 
 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
@@ -78,7 +82,7 @@ int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	case KVM_REG_ARM_CORE:
 		return get_core_reg(vcpu, reg);
 	default:
-		return -EINVAL;
+		return kvm_arm_sys_reg_get_reg(vcpu, reg);
 	}
 }
 
@@ -92,7 +96,7 @@ int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
 	case KVM_REG_ARM_CORE:
 		return set_core_reg(vcpu, reg);
 	default:
-		return -EINVAL;
+		return kvm_arm_sys_reg_set_reg(vcpu, reg);
 	}
 }
 
arch/s390/kvm/arm64/handle_exit.c +1 -0
index 8b03e8ee5acf..b6cb4612675a 100644
@@ -55,6 +55,7 @@ static int handle_hvc(struct kvm_vcpu *vcpu)
 static exit_handle_fn arm_exit_handlers[] = {
 	[0 ... ESR_ELx_EC_MAX]	= kvm_handle_unknown_ec,
 	[ESR_ELx_EC_HVC64]	= handle_hvc,
+	[ESR_ELx_EC_SYS64]	= kvm_handle_sys_reg,
 	[ESR_ELx_EC_IABT_LOW]	= kvm_handle_guest_abort,
 	[ESR_ELx_EC_DABT_LOW]	= kvm_handle_guest_abort,
 };
arch/s390/kvm/arm64/reset.c +42 -0

63 lines not on this page · raw message

Signature · 1 line
2.53.0
#40 lore raw

Re: [PATCH v2 14/20] KVM: s390: arm64: Integrate sysreg into the host

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Preemption and interrupts are erroneously re-enabled prior to guest entry and host sysreg writes, causing unstable smp_processor_id() usage and race conditions.

Pre-existing issues:
- [High] Missed KVM vCPU request (lost IPI) due to checking requests with interrupts enabled before entering guest mode. --

commit 1f3a8a06b031d71852642f3d9a86276d457c62ad
Author: Steffen Eiden <[email protected]>
Subject: KVM: s390: arm64: Integrate sysreg into the host

This commit integrates the system register managing infrastructure into the host. It initializes sysregs during vcpu init, finalizes them on PID change, and lets userspace read and modify guest system registers.

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +0 -0
index c573a3da5766e..3a0bba6e0a062 100644

[ ... ]

arch/s390/kvm/arm64/arm.c static void arm_vcpu_run(struct kvm_vcpu *vcpu) · line 407

Quoted 10 lines
arch/s390/kvm/arm64/arm.c +6 -0
@@ -390,10 +399,20 @@ static void arm_vcpu_run(struct kvm_vcpu *vcpu)
 	guest_state_enter_irqoff();
 	local_irq_enable();
 
+	vcpu_write_host_sys_reg(vcpu, vcpu->arch.hcr_elz, SYS_HCR_EL2);
+	vcpu_write_host_sys_reg(vcpu, vcpu->arch.hcrx_elz, SYS_HCRX_EL2);
+	vcpu_write_host_sys_reg(vcpu, vcpu->arch.mpidr, SYS_VMPIDR_EL2);
+
+	_vcpu_write_sys_reg(vcpu, vcpu->arch.ctxt.elr_el1, SYS_ELR_EL1);
+	_vcpu_write_sys_reg(vcpu, vcpu->arch.ctxt.spsr_el1, SYS_SPSR_EL1);

[Severity: High]
Does calling _vcpu_write_sys_reg() here cause problems with preemption being enabled?

Because local_irq_enable() is called immediately before these sysreg writes, the thread is fully preemptible. The _vcpu_write_sys_reg() macro invokes smp_processor_id(), which triggers a CONFIG_DEBUG_PREEMPT BUG when used in preemptible code.

Additionally, if the thread is preempted and migrated to another CPU during this window, could this race condition cause the sysregs to be written to the wrong physical CPU?

arch/s390/kvm/arm64/arm.c static void arm_vcpu_run(struct kvm_vcpu *vcpu)

Quoted 11 lines
arch/s390/kvm/arm64/arm.c +3 -0
 	sae_block->icptr = 0;
 
 	sae64a(sae_block);
 
+	vcpu->arch.ctxt.elr_el1 = _vcpu_read_sys_reg(vcpu, SYS_ELR_EL1);
+	vcpu->arch.ctxt.spsr_el1 = _vcpu_read_sys_reg(vcpu, SYS_SPSR_EL1);
+
 	local_irq_disable();
 	guest_state_exit_irqoff();
 	guest_timing_exit_irqoff();
 	local_irq_enable();

[Severity: High]
Does this sequence leave the actual guest execution window completely preemptible?

It appears the logic disables IRQs only to satisfy the guest_state_exit_irqoff() function, which mirrors the potentially inverted logic for the entry path above.

While reviewing this patch, I also noticed a pre-existing issue in the vCPU run loop:

arch/s390/kvm/arm64/arm.c:kvm_arch_vcpu_ioctl_run() {
	...
		smp_store_mb(vcpu->mode, IN_GUEST_MODE);

		if (kvm_request_pending(vcpu)) {
			vcpu->mode = OUTSIDE_GUEST_MODE;
			continue;
		}
	...
}

[Severity: High]
This is a pre-existing issue, but does checking kvm_request_pending() here while host interrupts are fully enabled leave a race window for lost IPIs?

The vCPU run loop sets vcpu->mode = IN_GUEST_MODE and checks kvm_request_pending() while host interrupts are fully enabled. Consequently, if an IPI arrives immediately after the check, the host will consume it right away. The vCPU thread could then proceed to enter the guest without noticing the request, causing the request to be missed until a subsequent unrelated exit.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
author reviewer
#41 lore raw

PATCH v2 15/20 KVM: s390: arm64: Use QAAF init save area

In reply to Steffen Eiden

Use the (cached) result of QAAF fc2 (Generate Initial System-Registers-Save Area) to initialize the Save Area.

---
 arch/s390/kvm/arm64/arm.c   | 1 -
 arch/s390/kvm/arm64/reset.c | 5 +++++
 2 files changed, 5 insertions(+), 1 deletion(-)
arch/s390/kvm/arm64/arm.c +0 -1
index 3a0bba6e0a06..ab6e7d135435 100644
@@ -360,7 +360,6 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
 	int ret;
 
 	sae_block->save_area = virt_to_phys(save_area);
-	save_area->sdo = virt_to_phys(sae_block);
 
 	ret = kvm_vcpu_set_target(vcpu, init);
 	if (ret)
arch/s390/kvm/arm64/reset.c +5 -0
index 74622ba6f081..bc7bcee75779 100644
@@ -10,6 +10,8 @@
 
 #include <clocksource/arm_arch_timer.h>
 
+#include "qaaf.h"
+
 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)
 {
 	return true;
@@ -79,6 +81,9 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 	if (loaded)
 		vcpu_put(vcpu);
 
+	kvm_vcpu_init_save_area(&vcpu->arch.save_area);
+	vcpu->arch.save_area.sdo = virt_to_phys(&vcpu->arch.sae_block);
+
 	kvm_reset_vcpu_core(vcpu);
 	kvm_reset_sys_regs(vcpu);
 
Signature · 1 line
2.53.0
#42 lore raw

Re: [PATCH v2 15/20] KVM: s390: arm64: Use QAAF init save area

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

Use the (cached) result of QAAF fc2 (Generate Initial System-Registers-Save Area) to initialize the Save Area.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
author reviewer
#43 lore raw

PATCH v2 16/20 KVM: s390: arm64: Implement exception injection

In reply to Steffen Eiden

Add exception injection support for s390 KVM arm64 architecture. This implements the core exception entry mechanism and fault injection functions needed for proper guest exception handling.

---
 arch/s390/include/asm/kvm_host_arm64.h |   2 +
 arch/s390/kvm/arm64/Makefile           |   1 +
 arch/s390/kvm/arm64/arm.c              |  15 +---
 arch/s390/kvm/arm64/exception.c        | 105 +++++++++++++++++++++++++
 arch/s390/kvm/arm64/inject_fault.c     |  73 ++++++++++++++++-
 5 files changed, 183 insertions(+), 13 deletions(-)
 create mode 100644 arch/s390/kvm/arm64/exception.c
arch/s390/include/asm/kvm_host_arm64.h +2 -0
index 0bf396ef54c2..16a378b68c8a 100644
@@ -426,4 +426,6 @@ static inline bool has_broken_cntvoff(void)
 	return false;
 }
 
+void kvm_adjust_pc(struct kvm_vcpu *vcpu);
+
 #endif /* ASM_KVM_HOST_ARM64_H */
arch/s390/kvm/arm64/Makefile +1 -0
index e19040cdc27d..cb3bf6da6d63 100644
@@ -9,6 +9,7 @@ ccflags-y += -I $(src) -I $(objtree)/$(obj) -I$(srctree)/arch/s390/kvm/gmap -DKV
 
 kvm-arm64-obj := \
 	arm.o \
+	exception.o \
 	feature.o \
 	guest.o \
 	handle_exit.o \
arch/s390/kvm/arm64/arm.c +4 -11
index ab6e7d135435..89984c6a5b64 100644
@@ -379,19 +379,11 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
 	return -EINVAL;
 }
 
-static void adjust_pc(struct kvm_vcpu *vcpu)
-{
-	if (vcpu_get_flag(vcpu, INCREMENT_PC)) {
-		kvm_skip_instr(vcpu);
-		vcpu_clear_flag(vcpu, INCREMENT_PC);
-	}
-}
-
 static void arm_vcpu_run(struct kvm_vcpu *vcpu)
 {
 	struct kvm_sae_block *sae_block = &vcpu->arch.sae_block;
 
-	adjust_pc(vcpu);
+	kvm_adjust_pc(vcpu);
 
 	local_irq_disable();
 	guest_timing_enter_irqoff();
@@ -496,8 +488,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
 	kvm_sigset_deactivate(vcpu);
 out:
-	if (unlikely(vcpu_get_flag(vcpu, INCREMENT_PC)))
-		adjust_pc(vcpu);
+	if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
+		     vcpu_get_flag(vcpu, INCREMENT_PC)))
+		kvm_adjust_pc(vcpu);
 
 	save_vx_regs(vcpu->arch.ctxt.vregs);
 	kernel_fpu_end(&fpu_save, KERNEL_FPC | KERNEL_VXR);
arch/s390/kvm/arm64/exception.c new file +105 -0

105 lines not on this page · raw message

arch/s390/kvm/arm64/inject_fault.c +71 -2

88 lines not on this page · raw message

Signature · 1 line
2.53.0
#44 lore raw

Re: [PATCH v2 16/20] KVM: s390: arm64: Implement exception injection

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Synthesized ESR_ELx for injected exceptions (Aborts and Unknown exceptions) conditionally sets the…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Synthesized ESR_ELx for injected exceptions (Aborts and Unknown exceptions) conditionally sets the IL bit, violating architectural requirements. --

commit 0f2bf8b10fedf69578d14536aed0e0dc39a725b4
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Implement exception injection

This commit adds core exception entry and fault injection mechanisms for the s390 KVM arm64 architecture, enabling guest exception handling.

Quoted 4 lines
arch/s390/kvm/arm64/inject_fault.c +0 -0
index 425dbeaa421c6..7ecf91f86d901 100644

[ ... ]

Quoted 5 lines
arch/s390/kvm/arm64/inject_fault.c +5 -0
+static void inject_abt64(struct kvm_vcpu *vcpu, bool is_iabt, unsigned long addr)
+{
+	unsigned long cpsr = *vcpu_cpsr(vcpu);
+	bool is_aarch32 = vcpu_mode_is_32bit(vcpu);
+	u64 esr = 0;

arch/s390/kvm/arm64/inject_fault.c line 24

Quoted 10 lines
arch/s390/kvm/arm64/inject_fault.c +10 -0
+
+	if (kvm_vcpu_abt_iss1tw(vcpu))
+		esr |= ESR_ELx_FSC_SEA_TTW(kvm_vcpu_abt_gltl(vcpu));
+	else
+		esr |= ESR_ELx_FSC_EXTABT;
+
+	pend_sync_exception(vcpu);
+
+	if (kvm_vcpu_trap_il_is32bit(vcpu))
+		esr |= ESR_ELx_IL;

[Severity: High]
Does this code conditionally omit the IL bit for injected aborts when the trapped instruction is 16-bit?

The ARM architecture requires the IL bit in ESR_ELx to be unconditionally set to 1 for injected Instruction and Data Aborts, regardless of the trapping instruction's length.

Could this cause the guest kernel to receive an architecturally incorrect ESR_ELx value (IL=0 instead of IL=1) and potentially break guest exception handlers if it traps on a 16-bit instruction?

Quoted 5 lines
arch/s390/kvm/arm64/inject_fault.c +5 -0
+
+	if (is_aarch32 || (cpsr & PSR_MODE_MASK) == PSR_MODE_EL0t)
+		esr |= (ESR_ELx_EC_IABT_LOW << ESR_ELx_EC_SHIFT);
+	else
+		esr |= (ESR_ELx_EC_IABT_CUR << ESR_ELx_EC_SHIFT);

[ ... ]

arch/s390/kvm/arm64/inject_fault.c line 54

Quoted 11 lines

void kvm_inject_undefined(struct kvm_vcpu *vcpu)
{

arch/s390/kvm/arm64/inject_fault.c +8 -1
-	/* Stub until s390 supports arm64 sysregs TODO sysregs*/
+	u64 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT);
+
+	/*
+	 * Build an unknown exception, depending on the instruction
+	 * set.
+	 */
+	if (kvm_vcpu_trap_il_is32bit(vcpu))
+		esr |= ESR_ELx_IL;

[Severity: High]
Similar to the abort injection above, does this code conditionally omit the IL bit for an injected Unknown exception?

The architecture also requires the IL bit to be unconditionally set to 1 for Unknown exceptions.

Quoted 3 lines
arch/s390/kvm/arm64/inject_fault.c +2 -0
+
+	kvm_inject_sync(vcpu, esr);
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
author reviewer
#45 lore raw

PATCH v2 17/20 KVM: s390: arm64: Finalize page fault handling

In reply to Steffen Eiden

Complete the page fault handling implementation by replacing temporary error returns with proper ARM64 exception injection.

---
 arch/s390/include/asm/kvm_host_arm64.h |  1 +
 arch/s390/kvm/arm64/arm.c              |  6 +++
 arch/s390/kvm/arm64/mmu.c              | 68 ++++++++++++++++++--------
 3 files changed, 54 insertions(+), 21 deletions(-)
arch/s390/include/asm/kvm_host_arm64.h +1 -0
index 16a378b68c8a..c0e8fbddc9dc 100644
@@ -427,5 +427,6 @@ static inline bool has_broken_cntvoff(void)
 }
 
 void kvm_adjust_pc(struct kvm_vcpu *vcpu);
+size_t kvm_parange_to_address_sanitized(u32 id_parange);
 
 #endif /* ASM_KVM_HOST_ARM64_H */
arch/s390/kvm/arm64/arm.c +6 -0
index 89984c6a5b64..d2ff69422217 100644
@@ -62,9 +62,15 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 
 static u64 kvm_max_guest_address(void)
 {
+	u64 mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
+	u64 id_pa_addr;
 	u64 max_addr;
 
+	id_pa_addr = kvm_parange_to_address_sanitized(
+		SYS_FIELD_GET(ID_AA64MMFR0_EL1, PARANGE, mmfr0));
+
 	max_addr = min_t(u64, TASK_SIZE_MAX, sclp.hamax);
+	max_addr = min_t(u64, max_addr, id_pa_addr);
 	max_addr = max_t(u64, max_addr, SZ_1G - 1);
 	return ALIGN_DOWN(max_addr + 1, SZ_1G) - 1;
 }
arch/s390/kvm/arm64/mmu.c +47 -21

99 lines not on this page · raw message

Signature · 1 line
2.53.0
#46 lore raw

Re: [PATCH v2 17/20] KVM: s390: arm64: Finalize page fault handling

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Architectural Violation: Injected aborts incorrectly set the ESR_ELx_IL bit conditionally. --

commit b703e320efef81f7ebb0637eda7231ef63b3ca22
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Finalize page fault handling

This commit finalizes page fault handling by replacing temporary error returns with proper ARM64 exception injection. Specifically, it routes PGM_ADDRESSING faults and out-of-bounds IPAs to kvm_inject_size_fault() and kvm_inject_sea().

Quoted 8 lines
arch/s390/kvm/arm64/mmu.c +0 -0
index bf3442d3609b2..b5f1dc3ca0d28 100644
@@ -30,12 +30,14 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, gpa_t fault_ipa,
 	ret = kvm_s390_faultin_gfn(vcpu, NULL, &f);
 	if (ret <= 0)
 		return ret;

arch/s390/kvm/arm64/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, gpa_t fault_ipa, · line 38

Quoted 10 lines
arch/s390/kvm/arm64/mmu.c +4 -4
-	if (ret == PGM_ADDRESSING)
+	if (ret == PGM_ADDRESSING) {
 		/*
-		 * Without the relevant sysregs we cannot do anything for now.
-		 * Go back to userspace with an error. TODO sysreg handling
+		 * There is no page with the requested address. Inject size fault
+		 * which is the closest arm match to PGM-addressing
 		 */
-		return -ENOEXEC;
+		kvm_inject_size_fault(vcpu);

[Severity: High]
This is a pre-existing issue, but does routing faults to kvm_inject_size_fault() expose an architectural violation for 16-bit instructions?

When a guest user-space process executing a 16-bit instruction triggers a fault, this patch routes it to the exception injection logic. That logic currently conditionally sets the IL bit based on instruction length:

arch/s390/kvm/arm64/inject_fault.c:inject_abt64() {
    ...
    if (kvm_vcpu_trap_il_is32bit(vcpu))
        esr |= ESR_ELx_IL;
    ...
}

(The same conditional logic is also present in kvm_inject_undefined()).

For an SError, Instruction Abort, or Data Abort with ISV == 0, the architecture mandates that the IL bit must be 1 regardless of the trapping instruction's length.

Could this cause KVM to synthesize an ESR_ELx with IL=0 instead of the required IL=1 for 16-bit instructions? If the guest OS exception handler receives an architecturally invalid syndrome, could it malfunction or lead to a guest kernel panic?

Quoted 5 lines
arch/s390/kvm/arm64/mmu.c +2 -0
+		return 1;
+	}
 	KVM_BUG_ON(ret, vcpu->kvm);
 	return -EINVAL;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
author reviewer
#47 lore raw

PATCH v2 18/20 KVM: s390: arm64: Implement SVE for arm guests

In reply to Steffen Eiden

Add support for Scalable Vector Extension (SVE) in ARM64 guests running on s390 hardware, providing 128-bit vector operations backed by s390's Vector Extension (VX).

The implementation is limited to 128-bit vectors (VQ=1) matching s390 VX register width. Z registers reuse existing FP/SIMD register space (vregs), while P registers and FFR are stored in the SAE block.

This enables ARM64 guests to use basic SVE instructions while maintaining compatibility with s390 hardware capabilities.

---
 arch/s390/include/asm/kvm_host_arm64.h |   6 +-
 arch/s390/kvm/arm64/arm.c              |   7 +
 arch/s390/kvm/arm64/feature.c          |   9 +-
 arch/s390/kvm/arm64/feature.h          |  10 ++
 arch/s390/kvm/arm64/guest.c            | 204 +++++++++++++++++++++++++
 arch/s390/kvm/arm64/reset.c            |  22 ++-
 6 files changed, 253 insertions(+), 5 deletions(-)
arch/s390/include/asm/kvm_host_arm64.h +3 -3
index c0e8fbddc9dc..73eb09e98848 100644
@@ -31,8 +31,9 @@ bool cpus_have_final_cap(unsigned int num);
 
 #define KVM_HAVE_MMU_RWLOCK
 #define KVM_MAX_VCPUS 1
-#define KVM_S390_ARM64_IMPL_FEATURES \
-	(BIT(KVM_ARM_VCPU_PTRAUTH_ADDRESS) | BIT(KVM_ARM_VCPU_PTRAUTH_GENERIC))
+#define KVM_S390_ARM64_IMPL_FEATURES (		\
+	 BIT(KVM_ARM_VCPU_SVE)			\
+)
 
 #define KVM_HALT_POLL_NS_DEFAULT 50000
 
@@ -132,7 +133,6 @@ struct kvm_vcpu_stat {
 };
 
 #define kvm_has_mte(_kvm) false
-#define vcpu_has_sve(_vcpu) false
 #define vcpu_has_ptrauth(_vcpu) false
 
 struct kvm_arch_memory_slot {
arch/s390/kvm/arm64/arm.c +7 -0
index d2ff69422217..a46c9c62a5f9 100644
@@ -22,6 +22,7 @@
 #include <kvm_mmu.h>
 
 #include "arm.h"
+#include "feature.h"
 #include "handle_exit.h"
 #include "qaaf.h"
 #include "feature.h"
@@ -53,6 +54,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_IOEVENTFD:
 		ret = 1;
 		break;
+	case KVM_CAP_ARM_SVE:
+		ret = system_supports_sve();
+		break;
 	default:
 		ret = 0;
 	}
@@ -258,6 +262,9 @@ static unsigned long system_supported_vcpu_features(void)
 {
 	unsigned long features = KVM_S390_ARM64_IMPL_FEATURES;
 
+	if (!system_supports_sve())
+		clear_bit(KVM_ARM_VCPU_SVE, &features);
+
 	return features;
 }
 
arch/s390/kvm/arm64/feature.c +8 -1
index 39c8ae7e951c..3847cdd2f92b 100644
@@ -56,7 +56,6 @@ int __init kvm_arm_host_sanitize_features(struct qaaf_qmc_block *qaaf_qmc)
 	MASK_RESERVED(qaaf_qmc, ID_AA64MMFR4_EL1);
 
 	MODIFY(qaaf_qmc, ID_AA64PFR0_EL1, SEL2, NI);
-	MODIFY(qaaf_qmc, ID_AA64PFR0_EL1, SVE, NI);
 	MASK_RESERVED(qaaf_qmc, ID_AA64PFR0_EL1);
 	MASK_RESERVED(qaaf_qmc, ID_AA64PFR1_EL1);
 	MASK_RESERVED(qaaf_qmc, ID_AA64PFR2_EL1);
@@ -163,3 +162,11 @@ bool cpus_have_final_cap(unsigned int num)
 		return false;
 	}
 }
+
+bool system_supports_sve(void)
+{
+	return cpu_has_vx() &&
+	       SYS_FIELD_GET(ID_AA64PFR0_EL1, SVE,
+			     read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) ==
+		       ID_AA64PFR0_EL1_SVE_IMP;
+}
arch/s390/kvm/arm64/feature.h +10 -0
index 9c902df1f26c..892cf8bcf3a1 100644
@@ -5,9 +5,13 @@
 
 #include <linux/types.h>
 #include <linux/bitfield.h>
+#include <linux/cpufeature.h>
 
 #include <asm/sae.h>
 
+#include <arm64/sysreg.h>
+#include <arm64/sys_regs.h>
+
 #include "qaaf.h"
 
 int __init kvm_arm_host_sanitize_features(struct qaaf_qmc_block *qaaf_qmc);
@@ -84,4 +88,10 @@ static inline u64 kvm_sae_irptc(void)
 
 #define kvm_vcpu_has_pmu(_v) false
 
+#define vcpu_has_sve(_vcpu)							\
+	(system_supports_sve() &&						\
+	 test_bit(KVM_ARCH_FLAG_GUEST_HAS_SVE, &((_vcpu)->kvm)->arch.flags))
+
+bool system_supports_sve(void);
+
 #endif /* ARCH_S390_KVM_FEATURE_H */
arch/s390/kvm/arm64/guest.c +204 -0

240 lines not on this page · raw message

arch/s390/kvm/arm64/reset.c +21 -1
index bc7bcee75779..754c4d4a70b6 100644
@@ -3,6 +3,8 @@
 #include <linux/kvm_host.h>
 #include <linux/fpu.h>
 
+#include "asm/cpufeature.h"
+
 #include <arm64/kvm_emulate.h>
 #include <arm64/kvm_nested.h>
 #include <arm64/sys_regs.h>
@@ -11,9 +13,13 @@
 #include <clocksource/arm_arch_timer.h>
 
 #include "qaaf.h"
+#include "feature.h"
 
 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)
 {
+	if (vcpu_has_sve(vcpu) && !kvm_arm_vcpu_sve_finalized(vcpu))
+		return false;
+
 	return true;
 }
 
@@ -76,6 +82,10 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 	 */
 	preempt_disable();
 
+	if (!vcpu_get_flag(vcpu, VCPU_SVE_FINALIZED) &&
+	    vcpu_has_feature(vcpu, KVM_ARM_VCPU_SVE))
+		set_bit(KVM_ARCH_FLAG_GUEST_HAS_SVE, &vcpu->kvm->arch.flags);
+
 	/* The reset must run with an unloaded save area */
 	loaded = vcpu_is_loaded(vcpu);
 	if (loaded)
@@ -114,5 +124,15 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 
 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
 {
-	return 0;
+	switch (feature) {
+	case KVM_ARM_VCPU_SVE:
+		if (!cpu_has_vx())
+			return -EINVAL;
+		if (vcpu_get_flag(vcpu, VCPU_SVE_FINALIZED))
+			return -EPERM;
+		vcpu_set_flag(vcpu, VCPU_SVE_FINALIZED);
+		return 0;
+	}
+
+	return -EINVAL;
 }
Signature · 1 line
2.53.0
#48 lore raw

Re: [PATCH v2 18/20] KVM: s390: arm64: Implement SVE for arm guests

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] SVE register accessors copy fewer bytes than the KVM ABI specifies, failing to zero-pad the…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] SVE register accessors copy fewer bytes than the KVM ABI specifies, failing to zero-pad the remainder of the userspace buffer.
- [Medium] SVE ioctls and vCPU finalization bypass feature gating checks.
- [Low] Unrecognized or invalid SVE register IDs result in a -EFAULT return code instead of -ENOENT or -EINVAL.
--

commit 7e278493890194c05cc617a90bf08952cf400bea
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Implement SVE for arm guests

Add support for Scalable Vector Extension (SVE) in ARM64 guests running on s390 hardware, providing 128-bit vector operations backed by s390's Vector Extension (VX).

Quoted 4 lines
arch/s390/kvm/arm64/guest.c +0 -0
index 0474609759360..63fc4a64ab48f 100644

[ ... ]

arch/s390/kvm/arm64/guest.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 109

Quoted 8 lines
arch/s390/kvm/arm64/guest.c +8 -0
+static inline int get_sve_ffr_reg(struct kvm_vcpu *vcpu, unsigned int regnum,
+				  u16 __user *uptr)
+{
+	/* ffr is pregmax + 1 */
+	if (regnum != KVM_ARM64_SVE_NUM_PREGS)
+		return -EINVAL;
+
+	if (put_user(vcpu->arch.sae_block.sve_ffr, uptr))

[Severity: Medium]
The ARM64 KVM ABI defines FFR and PREG registers as 32 bytes (KVM_REG_SIZE_U256), but this only writes 2 bytes via the u16 pointer. Will this leave 30 bytes of the userspace buffer uninitialized, potentially leaking memory contents into the migration stream when QEMU reads it?

arch/s390/kvm/arm64/guest.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 120

Quoted 11 lines
arch/s390/kvm/arm64/guest.c +11 -0
+		return -EFAULT;
+	return 0;
+}
+
+static inline int get_sve_preg(struct kvm_vcpu *vcpu, unsigned int regnum,
+			       u16 __user *uptr)
+{
+	if (regnum < 0 || regnum >= KVM_ARM64_SVE_NUM_PREGS)
+		return -EINVAL;
+
+	if (put_user(vcpu->arch.sae_block.sve_pregs[regnum], uptr))

[Severity: Medium]
Similar to get_sve_ffr_reg(), this only copies 2 bytes for a register size defined as 32 bytes. Should this zero-pad the rest of the buffer according to the ABI?

arch/s390/kvm/arm64/guest.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 133

Quoted 13 lines
arch/s390/kvm/arm64/guest.c +13 -0
+		return -EFAULT;
+	return 0;
+}
+
+static inline int get_sve_zreg(struct kvm_vcpu *vcpu, unsigned int regnum,
+			       __vector128 __user *uptr)
+{
+	if (regnum < 0 || regnum >= KVM_ARM64_SVE_NUM_ZREGS)
+		return -EINVAL;
+
+	/* vreg and svreg overlap and zreg is also just 128 bit so we reuse the vreg space */
+	if (copy_to_user(uptr, &vcpu->arch.ctxt.vregs[regnum],
+			 sizeof(vcpu->arch.ctxt.vregs[regnum])))

[Severity: Medium]
The ZREG size is defined as 256 bytes (KVM_REG_SIZE_U2048) by the ABI, but this only copies 16 bytes. Does this leave the remaining 240 bytes of the userspace buffer untouched, breaking ABI expectations?

Quoted 3 lines
arch/s390/kvm/arm64/guest.c +3 -0
+		return -EFAULT;
+	return 0;
+}

[ ... ]

Quoted 4 lines
arch/s390/kvm/arm64/guest.c +4 -0
+static int get_sve_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
+{
+	void __user *uptr = (void __user *)reg->addr;
+	unsigned int regnum;

arch/s390/kvm/arm64/guest.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 226

Quoted 10 lines
arch/s390/kvm/arm64/guest.c +10 -0
+	int ret = -EFAULT;
+
+	if (reg->id == KVM_REG_ARM64_SVE_VLS)
+		ret = get_sve_vls(vcpu, uptr);
+	else if (sve_ffr_index(reg->id, &regnum) >= 0)
+		ret = get_sve_ffr_reg(vcpu, regnum, uptr);
+	else if (sve_preg_index(reg->id, &regnum) >= 0)
+		ret = get_sve_preg(vcpu, regnum, uptr);
+	else if (sve_zreg_index(reg->id, &regnum) >= 0)
+		ret = get_sve_zreg(vcpu, regnum, uptr);

[Severity: Medium]
Does this function need to enforce the vcpu_has_sve() and kvm_arm_vcpu_sve_finalized() constraints before delegating to the register accessors, as required by the ABI?

[Severity: Low]
If an unrecognized or invalid SVE register ID is passed, the index validators (e.g. sve_zreg_index()) return negative error codes like -ENOENT or -EINVAL, but this function ignores them and falls through to returning the default initialized ret = -EFAULT. Is it intentional to return -EFAULT instead of propagating the specific error codes, which userspace like QEMU might rely on to detect the end of slices?

Quoted 8 lines
arch/s390/kvm/arm64/guest.c +8 -0
+
+	return ret;
+}
+
+static int set_sve_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
+{
+	const void __user *uptr = (const void __user *)reg->addr;
+	int ret = -EFAULT;

arch/s390/kvm/arm64/guest.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 244

Quoted 10 lines
arch/s390/kvm/arm64/guest.c +10 -0
+	unsigned int regnum;
+
+	if (reg->id == KVM_REG_ARM64_SVE_VLS)
+		ret = set_sve_vls(vcpu, uptr);
+	else if (sve_ffr_index(reg->id, &regnum) >= 0)
+		ret = set_sve_ffr_reg(vcpu, regnum, uptr);
+	else if (sve_preg_index(reg->id, &regnum) >= 0)
+		ret = set_sve_preg(vcpu, regnum, uptr);
+	else if (sve_zreg_index(reg->id, &regnum) >= 0)
+		ret = set_sve_zreg(vcpu, regnum, uptr);

[Severity: Medium]
Like get_sve_reg(), should this also check if the vCPU has the SVE feature enabled and finalized before allowing state modifications to vector registers?

Quoted 8 lines
arch/s390/kvm/arm64/guest.c +4 -0
+
+	return ret;
+}
+
arch/s390/kvm/arm64/reset.c +0 -0
index bc7bcee75779e..754c4d4a70b6b 100644

[ ... ]

arch/s390/kvm/arm64/reset.c void kvm_reset_vcpu(struct kvm_vcpu *vcpu) · line 132

Quoted 9 lines

int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature) {

arch/s390/kvm/arm64/reset.c +6 -1
-	return 0;
+	switch (feature) {
+	case KVM_ARM_VCPU_SVE:
+		if (!cpu_has_vx())
+			return -EINVAL;
+		if (vcpu_get_flag(vcpu, VCPU_SVE_FINALIZED))
+			return -EPERM;

[Severity: Medium]
Should this function verify that the KVM_ARM_VCPU_SVE feature is actually enabled for this vCPU (e.g. via vcpu_has_feature()) before allowing finalization? Without this check, userspace could potentially finalize SVE on non-SVE vCPUs.

Quoted 6 lines
arch/s390/kvm/arm64/reset.c +5 -0
+		vcpu_set_flag(vcpu, VCPU_SVE_FINALIZED);
+		return 0;
+	}
+
+	return -EINVAL;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
author reviewer
#49 lore raw

PATCH v2 19/20 KVM: s390: arm64: Promote PTRAUTH capability

In reply to Steffen Eiden

Promote the pointer authentication capability if supported by hardware.

---
 arch/s390/include/asm/kvm_host_arm64.h |  8 ++------
 arch/s390/kvm/arm64/arm.c              |  9 +++++++++
 arch/s390/kvm/arm64/feature.c          | 18 ++++++++++++++++++
 arch/s390/kvm/arm64/feature.h          |  5 +++++
 4 files changed, 34 insertions(+), 6 deletions(-)
arch/s390/include/asm/kvm_host_arm64.h +2 -6
index 73eb09e98848..32facf045704 100644
@@ -32,6 +32,8 @@ bool cpus_have_final_cap(unsigned int num);
 #define KVM_HAVE_MMU_RWLOCK
 #define KVM_MAX_VCPUS 1
 #define KVM_S390_ARM64_IMPL_FEATURES (		\
+	 BIT(KVM_ARM_VCPU_PTRAUTH_ADDRESS) |	\
+	 BIT(KVM_ARM_VCPU_PTRAUTH_GENERIC) |	\
 	 BIT(KVM_ARM_VCPU_SVE)			\
 )
 
@@ -133,7 +135,6 @@ struct kvm_vcpu_stat {
 };
 
 #define kvm_has_mte(_kvm) false
-#define vcpu_has_ptrauth(_vcpu) false
 
 struct kvm_arch_memory_slot {
 };
@@ -170,11 +171,6 @@ struct kvm_vm_stat {
 	struct kvm_vm_stat_generic generic;
 };
 
-static inline bool system_has_full_ptr_auth(void)
-{
-	return true;
-}
-
 #define kvm_vm_is_protected(_kvm) false
 #define vcpu_is_protected(_vcpu) false
 
arch/s390/kvm/arm64/arm.c +9 -0
index a46c9c62a5f9..ebd8045c0900 100644
@@ -57,6 +57,10 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_ARM_SVE:
 		ret = system_supports_sve();
 		break;
+	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
+	case KVM_CAP_ARM_PTRAUTH_GENERIC:
+		ret = kvm_has_full_ptr_auth();
+		break;
 	default:
 		ret = 0;
 	}
@@ -265,6 +269,11 @@ static unsigned long system_supported_vcpu_features(void)
 	if (!system_supports_sve())
 		clear_bit(KVM_ARM_VCPU_SVE, &features);
 
+	if (!kvm_has_full_ptr_auth()) {
+		clear_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features);
+		clear_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features);
+	}
+
 	return features;
 }
 
arch/s390/kvm/arm64/feature.c +18 -0
index 3847cdd2f92b..a2848e4b3e62 100644
@@ -170,3 +170,21 @@ bool system_supports_sve(void)
 			     read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) ==
 		       ID_AA64PFR0_EL1_SVE_IMP;
 }
+
+bool system_has_full_ptr_auth(void)
+{
+	u64 isar1 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
+	u64 isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
+	bool address_auth;
+	bool generic_auth;
+
+	address_auth = SYS_FIELD_GET(ID_AA64ISAR1_EL1, APA, isar1) >= ID_AA64ISAR1_EL1_APA_PAuth ||
+		       SYS_FIELD_GET(ID_AA64ISAR1_EL1, API, isar1) >= ID_AA64ISAR1_EL1_API_PAuth ||
+		       SYS_FIELD_GET(ID_AA64ISAR2_EL1, APA3, isar2) >= ID_AA64ISAR2_EL1_APA3_PAuth;
+
+	generic_auth = SYS_FIELD_GET(ID_AA64ISAR1_EL1, GPA, isar1) >= ID_AA64ISAR1_EL1_GPA_IMP ||
+		       SYS_FIELD_GET(ID_AA64ISAR1_EL1, GPI, isar1) >= ID_AA64ISAR1_EL1_GPI_IMP ||
+		       SYS_FIELD_GET(ID_AA64ISAR2_EL1, GPA3, isar2) >= ID_AA64ISAR2_EL1_GPA3_IMP;
+
+	return address_auth && generic_auth;
+}
arch/s390/kvm/arm64/feature.h +5 -0
index 892cf8bcf3a1..c11ac0daa428 100644
@@ -94,4 +94,9 @@ static inline u64 kvm_sae_irptc(void)
 
 bool system_supports_sve(void);
 
+#define vcpu_has_ptrauth(vcpu)						\
+	 (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_ADDRESS) ||       \
+	  vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_GENERIC))
+bool system_has_full_ptr_auth(void);
+
 #endif /* ARCH_S390_KVM_FEATURE_H */
Signature · 1 line
2.53.0
#50 lore raw

Re: [PATCH v2 19/20] KVM: s390: arm64: Promote PTRAUTH capability

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Missing PTRAUTH traps configuration causes guest crashes. - [Low] Macro redefinition of…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Missing PTRAUTH traps configuration causes guest crashes.
- [Low] Macro redefinition of 'vcpu_has_ptrauth' causes a build failure.
--

commit cda854ae1be029b75a496434857cbf27f60d5cc0
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Promote PTRAUTH capability

Promote the pointer authentication capability if supported by hardware.

arch/s390/include/asm/kvm_host_arm64.h bool cpus_have_final_cap(unsigned int num); · line 38

Quoted 12 lines
arch/s390/include/asm/kvm_host_arm64.h +2 -0
index 73eb09e98848f..32facf0457049 100644
@@ -32,6 +32,8 @@ bool cpus_have_final_cap(unsigned int num);
 #define KVM_HAVE_MMU_RWLOCK
 #define KVM_MAX_VCPUS 1
 #define KVM_S390_ARM64_IMPL_FEATURES (		\
+	 BIT(KVM_ARM_VCPU_PTRAUTH_ADDRESS) |	\
+	 BIT(KVM_ARM_VCPU_PTRAUTH_GENERIC) |	\
 	 BIT(KVM_ARM_VCPU_SVE)			\
 )

[Severity: High]
Does promoting these PTRAUTH capabilities lead to a guest crash?

When a guest executes Pointer Authentication instructions, the hardware checks the Hypervisor Configuration Register (HCR_EL2) bits HCR_API and HCR_APK. Because vcpu_reset_hcr() hardcodes vcpu->arch.hcr_elz and omits these bits:

arch/s390/kvm/arm64/reset.c:vcpu_reset_hcr() {
        vcpu->arch.hcr_elz = HCR_EL2_E2H | HCR_EL2_RW | HCR_EL2_AMO |
                             HCR_EL2_IMO | HCR_EL2_FMO | HCR_EL2_PTW;
        /* traps */
        vcpu->arch.hcr_elz |= HCR_EL2_TSC | HCR_EL2_TID1 | HCR_EL2_TID2 |
                              HCR_EL2_TID3 | HCR_EL2_TID4 | HCR_EL2_TID5 |
                              HCR_EL2_TIDCP | HCR_EL2_TLOR;
}

PTRAUTH instructions will unconditionally trap to the hypervisor with ESR_ELx_EC_PAC. Since arm_exit_handlers[] lacks a handler for ESR_ELx_EC_PAC:

arch/s390/kvm/arm64/handle_exit.c:arm_exit_handlers[] = {
        [0 ... ESR_ELx_EC_MAX]  = kvm_handle_unknown_ec,
        [ESR_ELx_EC_HVC64]      = handle_hvc,
        [ESR_ELx_EC_SYS64]      = kvm_handle_sys_reg,
        [ESR_ELx_EC_IABT_LOW]   = kvm_handle_guest_abort,
        [ESR_ELx_EC_DABT_LOW]   = kvm_handle_guest_abort,
};

It seems the hypervisor will fall back to kvm_handle_unknown_ec(), injecting an unexpected undefined instruction exception back to the guest and crashing it. Should we configure the traps or add an exit handler before enabling this?

[ ... ]

arch/s390/kvm/arm64/feature.h static inline u64 kvm_sae_irptc(void) · line 99

Quoted 11 lines
arch/s390/kvm/arm64/feature.h +3 -0
index 892cf8bcf3a12..c11ac0daa4287 100644
@@ -94,4 +94,9 @@ static inline u64 kvm_sae_irptc(void)
 
 bool system_supports_sve(void);
 
+#define vcpu_has_ptrauth(vcpu)						\
+	 (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_ADDRESS) ||       \
+	  vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_GENERIC))

[Severity: Low]
Can this macro redefinition cause a build failure on s390?

Because the C files including this header (like arm.c and feature.c) also include <linux/kvm_host.h>, they transitively include <arm64/kvm_host.h>. Since CONFIG_ARM64_PTR_AUTH is not set on s390, <arm64/kvm_host.h> defines vcpu_has_ptrauth(vcpu) as false.

Redefining it here without first using #undef vcpu_has_ptrauth might trigger a -Wmacro-redefined warning, which breaks the build if -Werror is used.

Quoted 3 lines
arch/s390/kvm/arm64/feature.h +2 -0
+bool system_has_full_ptr_auth(void);
+
 #endif /* ARCH_S390_KVM_FEATURE_H */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
author reviewer
#51 lore raw

PATCH v2 20/20 s390: Report AEF features to sysfs

In reply to Steffen Eiden

Display various information about the Arm execution Facility (AEF) to userspace. The files are located in /sys/firmware/aef/. Show if the feature is available and usable for the host, which formats are available, and how many arm vCPUS per guest are supported by the machine. Additionally, provide the raw binary output of QAAF fc1 (Query Model Capabilities) in the file qmc and fc2 (Initial System-Registers-Save Area) in the file save_area.

---
 arch/s390/kernel/aef.c | 86 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)
arch/s390/kernel/aef.c +86 -0
index 825e2643a892..2691f1ac63dc 100644
@@ -40,6 +40,66 @@ const struct aef_info *aef_info(void)
 }
 EXPORT_SYMBOL(aef_info);
 
+static struct kset *aef_query_kset;
+static struct kobject *aef_kobj;
+
+#define aef_sysfs_qval_def(_name)					\
+	static ssize_t _name##_show(struct kobject *kobj,		\
+				    struct kobj_attribute *attr,	\
+				    char *buf)				\
+	{								\
+		return sysfs_emit(buf, "%lx\n", info._name);		\
+	}								\
+	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
+
+#define aef_sysfs_qval_attr(_name) &_name##_attr.attr
+
+aef_sysfs_qval_def(arm_guest_supp);
+aef_sysfs_qval_def(sae_avail);
+aef_sysfs_qval_def(ptff_avail);
+aef_sysfs_qval_def(supp_state_desc_formats);
+aef_sysfs_qval_def(supp_save_area_formats);
+aef_sysfs_qval_def(max_num_vcpu);
+
+static ssize_t qmc_read(struct file *filp, struct kobject *kobj,
+			const struct bin_attribute *attr, char *buf, loff_t off,
+			size_t count)
+{
+	return memory_read_from_buffer(buf, count, &off, &qmc, sizeof(qmc));
+}
+
+BIN_ATTR_RO(qmc, sizeof(qmc));
+
+static ssize_t save_area_read(struct file *filp, struct kobject *kobj,
+			      const struct bin_attribute *attr, char *buf,
+			      loff_t off, size_t count)
+{
+	return memory_read_from_buffer(buf, count, &off, &save_area,
+				       sizeof(save_area));
+}
+
+BIN_ATTR_RO(save_area, sizeof(save_area));
+
+static struct attribute *aef_query_attrs[] = {
+	aef_sysfs_qval_attr(sae_avail),
+	aef_sysfs_qval_attr(ptff_avail),
+	aef_sysfs_qval_attr(supp_state_desc_formats),
+	aef_sysfs_qval_attr(supp_save_area_formats),
+	aef_sysfs_qval_attr(max_num_vcpu),
+	NULL,
+};
+
+static const struct bin_attribute *aef_query_bin_attrs[] = {
+	&bin_attr_qmc,
+	&bin_attr_save_area,
+	NULL,
+};
+
+static struct attribute_group aef_query_attr_group = {
+	.attrs = aef_query_attrs,
+	.bin_attrs = aef_query_bin_attrs,
+};
+
 static int __init aef_init_save_area(void)
 {
 	int ret;
@@ -95,7 +155,33 @@ static int __init aef_sysfs_init(void)
 	if (rc)
 		return rc;
 
+	aef_kobj = kobject_create_and_add("aef", firmware_kobj);
+	if (!aef_kobj)
+		return -ENOMEM;
+
+	rc = sysfs_create_file(aef_kobj, aef_sysfs_qval_attr(arm_guest_supp));
+	if (rc)
+		goto out_kobj;
+
+	aef_query_kset = kset_create_and_add("query", NULL, aef_kobj);
+	if (!aef_query_kset)
+		goto out_arm_guest_supp;
+
+	rc = sysfs_create_group(&aef_query_kset->kobj, &aef_query_attr_group);
+	if (rc)
+		goto out_kset;
+
 	return 0;
+
+out_kset:
+	kset_unregister(aef_query_kset);
+out_arm_guest_supp:
+	sysfs_remove_file(aef_kobj, &arm_guest_supp_attr.attr);
+out_kobj:
+	kobject_del(aef_kobj);
+	kobject_put(aef_kobj);
+
+	return rc;
 }
 
 arch_initcall(aef_sysfs_init);
Signature · 1 line
2.53.0
#52 lore raw

Re: [PATCH v2 20/20] s390: Report AEF features to sysfs

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] aef_init_save_area() unconditionally executes the 'qaaf' hardware instruction without checking for hardware support, causing a boot failure or kernel panic on unsupported hosts.
- [Medium] Missing error code assignment when kset_create_and_add() fails, resulting in a false success return.

Pre-existing issues:
- [High] The EX_TABLE entry in the qaaf() inline assembly incorrectly protects the non-faulting lhi instruction instead of the potentially faulting .insn instruction. --

commit 90a00cf465554d5bd4e3bc2a83bb5b6d1a27209d
Author: Steffen Eiden <[email protected]>

s390: Report AEF features to sysfs

Display various information about the Arm execution Facility (AEF) to userspace. The files are located in /sys/firmware/aef/. Show if the feature is available and usable for the host, which formats are available, and how many arm vCPUS per guest are supported by the machine. Additionally, provide the raw binary output of QAAF fc1 (Query Model Capabilities) in the file qmc and fc2 (Initial System-Registers-Save Area) in the file save_area.

Quoted 4 lines
arch/s390/kernel/aef.c +0 -0
index 825e2643a8921..2691f1ac63dca 100644

[ ... ]

Quoted 4 lines
arch/s390/kernel/aef.c +0 -0
@@ -40,6 +40,66 @@ const struct aef_info *aef_info(void)
 }
 EXPORT_SYMBOL(aef_info);
 

[ ... ]

arch/s390/kernel/aef.c

Quoted 10 lines
arch/s390/kernel/aef.c +10 -0
+static int __init aef_init_save_area(void)
+{
+	int ret;
+
+	union qaaf_gr0_gisrsa gr0 = {
+		.fc = QAAF_FC_GISRSA,
+		.saf = SAE_SAVE_AREA_FORMAT_0,
+	};
+
+	ret = qaaf(gr0.val, (union qaaf_block *)&save_area);

[Severity: High]
Does aef_init_save_area() need to check if info.sae_avail is set before executing qaaf()?

If aef_query_info() early returns success when the AEF feature is unsupported, aef_init_save_area() executes the qaaf instruction unconditionally. This will cause a boot failure or kernel panic on unsupported hosts.

[Severity: High]
This is a pre-existing issue, but this call unconditionally triggers a kernel panic on older hardware due to an issue with the qaaf() inline assembly.

arch/s390/include/asm/sae.h:qaaf() {
...

	asm volatile(
		"	lgr	0,%[r0]\n"
		"	.insn	rre,0xb9ad0000,%[r1],0\n"
		"0:	lhi	%[exc],0\n"
		"1:\n"
		EX_TABLE(0b, 1b)

...
}

The EX_TABLE entry incorrectly protects the non-faulting lhi instruction (label 0:) instead of the preceding, potentially faulting .insn instruction. When qaaf faults, the exception table misses it, resulting in an unhandled hardware exception.

Quoted 5 lines
arch/s390/kernel/aef.c +5 -0
+	if (ret)
+		return ret;
+
+	return 0;
+}

[ ... ]

Quoted 5 lines
arch/s390/kernel/aef.c +1 -0
@@ -95,7 +155,33 @@ static int __init aef_sysfs_init(void)
 	if (rc)
 		return rc;
 
+	aef_kobj = kobject_create_and_add("aef", firmware_kobj);

arch/s390/kernel/aef.c static int __init aef_sysfs_init(void) · line 168

Quoted 10 lines
arch/s390/kernel/aef.c +10 -0
+	if (!aef_kobj)
+		return -ENOMEM;
+
+	rc = sysfs_create_file(aef_kobj, aef_sysfs_qval_attr(arm_guest_supp));
+	if (rc)
+		goto out_kobj;
+
+	aef_query_kset = kset_create_and_add("query", NULL, aef_kobj);
+	if (!aef_query_kset)
+		goto out_arm_guest_supp;

[Severity: Medium]
Will this error path in aef_sysfs_init() return a false success?

If kset_create_and_add() fails due to memory exhaustion, the local variable 'rc' remains 0 from the successful sysfs_create_file() call above. The initcall will report success despite failing to create the sysfs structure.

Quoted 6 lines
arch/s390/kernel/aef.c +5 -0
+
+	rc = sysfs_create_group(&aef_query_kset->kobj, &aef_query_attr_group);
+	if (rc)
+		goto out_kset;
+
 	return 0;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20