Skip to contentSkip to conversation

KVM: arm64 on s390 System Register Handling

PATCH In review Steffen Eiden 5 participants 52 messages Nov 6 – Nov 24

Subsystems (6)
KVM arm64 s390 KVM arm64 +2 more subsystems
lore
Share & tools
lore

Steffen Eiden

author reviewer
#0 lore raw

v3 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 system register & exception handling.

This is the second series on the way to arm64 guests on s390. The first series laid the KVM foundation (basic module, vCPU run, simple page faults) and is included in v7.4. This series builds the system register layer on top and closes the biggest of the remaining gaps towards a functional guest support.

This series requires 5 fixup patches not yet merged in order to build clean.

[PATCH 0/3] KVM: s390: arm64: Fixes for arm64 KVM support [0] [PATCH v2] s390: Guard emitting assembler macros for insn-common-asm.h [1] [PATCH] KVM: s390: arm64: Tolerate unused functions in extracted source [2]

The series applies without those patches on 7.4-rc1 but the build will fail or trigger warnings w/o those patches.

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.

A few code pieces are moved inside arm headers/code to reduce the amount of shared sections. This adds a bit of churn in the diff but reduces the shared marker __churn__ at the end.

The shared regions deliberately contain more code and more headers than s390 currently needs. Larger contiguous regions mean less churn whenever the surrounding arm64 code changes. Some of this code will be used by later series anyways. Some of the shared code will probably never be reused by s390 at all; I trust the compiler to remove it. The reduced churn outweighs this drawback. Since unused function checking is disabled for shared code [3], sharing more than required does not introduce warnings once everything is merged.

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.

Emulation:

Guest accesses to ID registers are trapped and served from a sanitized copy of the QAAF query results, using the ID register sanitisation and finalisation infrastructure shared with arm64. Userspace sees and controls guest ID registers with the usual semantics; after the first vCPU run the values are final. All other system registers live in the SAE save area and are accessed by the host via EASR/SASR, either on guest traps or through the one-reg/two-reg UAPI. The save area itself is initialized from the QAAF-provided initial save area.

Exception handling:

Unsupported or invalid guest actions now raise proper arm64 exceptions instead of failing exits. Page faults beyond the guest's address limits are injected as address size faults or synchronous external aborts, mirroring arm64's kvm_handle_guest_abort(); injected faults re-enter the guest with the pending exception instead of exiting to userspace.

Features and userspace:

SVE is offered to guests at 128-bit vector length, backed by s390's Vector Extension, and pointer authentication is passed through where hardware support exists. The AEF capabilities are reported under /sys/firmware/aef/, including the raw QAAF query results; the interface is documented in Documentation/ABI.

For merging we will need a feature branch.

A branch with patches [0,1,2] + this series is available at kernel.org [3].

Steffen

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

---
Changes in v3:
- rebase into 7.4-rc2 (including the first batch of arm on s390 patches)
- guard style changed from #ifdef ARM64_S390_COMMON to #if ARM64_S390_COMMON == 1
- Improve and clarify commit messages
arm64:
- generate OSLSR definitions
- consolidate more shared sections by:
- moving some code in beforehand
- deliberately adding more code/header than required.
due to [3] there will not be any unused warnings when merged.
- reduce sharing churn

s390
- streamline sasr/easr instruction generation and use
- aef_info() aggregate removed; new minimal aef_usable()/aef_gisrsa() API
- qaaf enum numbering reorganized, on reserved field was incorrectly identified as a register.
- aef.c no longer aggregates or gates on CONFIG_KVM_S390_ARM64.
- spectre.c stubs replaced with real queries of sanitized ID regs.
- Many new sys_reg_descs entries
- QAAF init save area: save area now memcpy'd from cached aef_gisrsa() instead of kvm_vcpu_init_save_area()
- improved pagefault handling
- reworked SVE helpers
- fixed various smaller bugs
- Link to v2: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- rework the entire sharing code concept to use marker instead of moving code.

---
Steffen Eiden (23):
KVM: arm64: Refactor idreg caching into dedicated structure
KVM: arm64: Extract number of sys_reg_desc into a constant
arm64: sysreg: Convert OSLSR to automatic register generation
arm64: Prepare headers for sysreg sharing with s390
arm64: Share system register related headers and functions with s390
KVM: s390: arm64: Prepare for sharing arm64 SVE and ftr-reg code
KVM: arm64: Prepare sys_regs.c for sharing with s390
KVM: arm64: Share system register code with s390
s390: tools: Share arm64/kvm headers
s390: Add range check asm macro
s390: Read/write ARM system registers
s390: Query arm guest time
KVM: s390: arm64: Query Available Arm features
KVM: s390: arm64: Verify machine feature set before loading
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

 Documentation/ABI/testing/sysfs-firmware-aef |  70 +++
 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            |  64 ++-
 arch/arm64/include/asm/spectre.h             |  13 +-
 arch/arm64/include/asm/sysreg.h              |  12 +-
 arch/arm64/kvm/arm.c                         |   3 +
 arch/arm64/kvm/config.c                      |  10 +-
 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                    | 109 ++--
 arch/arm64/kvm/sys_regs.h                    |   1 +
 arch/arm64/tools/sysreg                      |  14 +
 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                  |  12 +
 arch/s390/include/asm/insn-common-asm.h      |  28 +
 arch/s390/include/asm/kvm_host_arm64.h       | 237 +++++++-
 arch/s390/include/asm/kvm_host_arm64_types.h | 115 ++++
 arch/s390/include/asm/sae-asm.h              |  45 ++
 arch/s390/include/asm/sae.h                  |  93 +++
 arch/s390/include/asm/timex.h                |  53 ++
 arch/s390/kernel/Makefile                    |   2 +-
 arch/s390/kernel/aef.c                       | 207 +++++++
 arch/s390/kernel/dis.c                       |   1 +
 arch/s390/kvm/arm64/Makefile                 |   7 +-
 arch/s390/kvm/arm64/Makefile.gen             |   3 +-
 arch/s390/kvm/arm64/arm.c                    | 101 +++-
 arch/s390/kvm/arm64/exception.c              | 109 ++++
 arch/s390/kvm/arm64/feature.c                | 188 ++++++
 arch/s390/kvm/arm64/feature.h                |  97 ++++
 arch/s390/kvm/arm64/guest.c                  | 268 ++++++++-
 arch/s390/kvm/arm64/handle_exit.c            |   1 +
 arch/s390/kvm/arm64/inject_fault.c           |  65 ++-
 arch/s390/kvm/arm64/mmu.c                    |  74 ++-
 arch/s390/kvm/arm64/qaaf.c                   |  85 +++
 arch/s390/kvm/arm64/qaaf.h                   |  12 +
 arch/s390/kvm/arm64/reset.c                  |  71 +++
 arch/s390/kvm/arm64/spectre.c                |  51 ++
 arch/s390/kvm/arm64/sys_regs.c               | 826 +++++++++++++++++++++++++++
 arch/s390/kvm/arm64/trace.h                  |  50 ++
 arch/s390/tools/Makefile.arm64h              |  11 +-
 arch/s390/tools/opcodes.txt                  |   3 +
 include/kvm/arm_arch_timer.h                 |   3 +
 47 files changed, 3030 insertions(+), 149 deletions(-)

base-commit: bc71431221f5ba8bb33918399552636e059c2c45
change-id: 20261106-sysreg-d6adf0cd34d4
prerequisite-message-id: [email protected]
prerequisite-patch-id: 93d5fe93939c707a0d0c57a847270c706a9bae29
prerequisite-patch-id: af10f106ab88b2632aa77e739bf8f64e8f3b5499
prerequisite-patch-id: cf8d158bdbb43de4ae2d524efe9005f9e42ead1f
prerequisite-message-id: [email protected]
prerequisite-patch-id: ed28ef94c9056cb81bb738f1b54745d012cb613f
prerequisite-message-id: [email protected]
prerequisite-patch-id: a1648acb9dedea7550a5bfcce937a21016d569a8

author reviewer
#1 lore raw

PATCH v3 1/23 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. The new struct is placed in a shared region. Update all users to use the new structure.

No functional changes.

---
 arch/arm64/include/asm/kvm_host.h  | 50 ++++++++++++++++++++------------------
 arch/arm64/kvm/config.c            | 10 ++++----
 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, 39 insertions(+), 34 deletions(-)
arch/arm64/include/asm/kvm_host.h +27 -23
index 97332ffcb130..7f051dad79c7 100644
@@ -105,6 +105,25 @@ struct vcpu_reset_state {
 	bool		reset;
 };
 
+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;
+};
+
 #endif /* ARM64_S390_COMMON == 1 */
 
 #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
@@ -478,22 +497,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;
@@ -1634,19 +1638,19 @@ void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
 bool kvm_pkvm_vcpu_is_powered_off(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;
@@ -1654,7 +1658,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 +5 -5
index adaf26ab93b8..49bbe68c0b9e 100644
@@ -1467,11 +1467,11 @@ static bool idreg_apply_deps(struct kvm *kvm, const struct reg_feat_map_desc *r)
 			resx.res0 |= reg_feat_map_bits(&r->bit_feat_map[i]);
 	}
 
-	val = kvm->arch.id_regs[r->idreg_idx];
-	kvm->arch.id_regs[r->idreg_idx] &= ~resx.res0;
-	kvm->arch.id_regs[r->idreg_idx] |= resx.res1;
+	val = kvm->arch.id_regs.ftr_reg[r->idreg_idx];
+	kvm->arch.id_regs.ftr_reg[r->idreg_idx] &= ~resx.res0;
+	kvm->arch.id_regs.ftr_reg[r->idreg_idx] |= resx.res1;
 
-	return val != kvm->arch.id_regs[r->idreg_idx];
+	return val != kvm->arch.id_regs.ftr_reg[r->idreg_idx];
 }
 
 void kvm_idregs_enforce_dependencies(struct kvm *kvm)
@@ -1488,7 +1488,7 @@ void kvm_idregs_enforce_dependencies(struct kvm *kvm)
 
 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 fc0475f0877d..41ee83290697 100644
@@ -399,7 +399,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;
 
 	/*
 	 * pKVM runs only GICv3. Anything else becomes 0, the model of a VM
@@ -419,7 +419,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;
 
 		goto out;
 	}
@@ -574,7 +574,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 32e82653ebf7..10b144e50feb 100644
@@ -296,7 +296,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;
 }
@@ -607,7 +607,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 850eba167aad..0f0b0495b710 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.56.0
#2 lore raw

Re: [PATCH v3 01/23] KVM: arm64: Refactor idreg caching into dedicated structure

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

Move VM-wide ID register emulation fields from struct kvm_arch into a new struct kvm_vm_id_regs. The new struct is placed in a shared region. Update all users to use the new structure.

No functional changes.

Sashiko has reviewed this patch and found no issues.

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

Re: [PATCH v3 01/23] KVM: arm64: Refactor idreg caching into dedicated structure

In reply to Steffen Eiden
Quoted 30 lines · Steffen Eiden

Move VM-wide ID register emulation fields from struct kvm_arch into a new struct kvm_vm_id_regs. The new struct is placed in a shared region. Update all users to use the new structure.

No functional changes.

---
 arch/arm64/include/asm/kvm_host.h  | 50 ++++++++++++++++++++------------------
 arch/arm64/kvm/config.c            | 10 ++++----
 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, 39 insertions(+), 34 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -0
index 97332ffcb130..7f051dad79c7 100644
@@ -105,6 +105,25 @@ struct vcpu_reset_state {
 	bool		reset;
 };
 
+struct kvm_vm_id_regs {
+	/*

arch/arm64/include/asm/kvm_host.h struct vcpu_reset_state { · line 119

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +10 -0
+	 * 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];

I'm OK with changing the name of the array, but then you need to be consistent and change the name of the indexing macros throughout the code. This should be a preliminary change.

Quoted 90 lines
arch/arm64/include/asm/kvm_host.h +15 -23
+
+	u64 midr_el1;
+	u64 revidr_el1;
+	u64 aidr_el1;
+	u64 ctr_el0;
+};
+
 #endif /* ARM64_S390_COMMON == 1 */
 
 #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
@@ -478,22 +497,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;
@@ -1634,19 +1638,19 @@ void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
 bool kvm_pkvm_vcpu_is_powered_off(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;
@@ -1654,7 +1658,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 +4 -4
index adaf26ab93b8..49bbe68c0b9e 100644
@@ -1467,11 +1467,11 @@ static bool idreg_apply_deps(struct kvm *kvm, const struct reg_feat_map_desc *r)
 			resx.res0 |= reg_feat_map_bits(&r->bit_feat_map[i]);
 	}
 
-	val = kvm->arch.id_regs[r->idreg_idx];
-	kvm->arch.id_regs[r->idreg_idx] &= ~resx.res0;
-	kvm->arch.id_regs[r->idreg_idx] |= resx.res1;
+	val = kvm->arch.id_regs.ftr_reg[r->idreg_idx];
+	kvm->arch.id_regs.ftr_reg[r->idreg_idx] &= ~resx.res0;
+	kvm->arch.id_regs.ftr_reg[r->idreg_idx] |= resx.res1;
 
-	return val != kvm->arch.id_regs[r->idreg_idx];
+	return val != kvm->arch.id_regs.ftr_reg[r->idreg_idx];
 }
 
 void kvm_idregs_enforce_dependencies(struct kvm *kvm)

arch/arm64/kvm/config.c void kvm_idregs_enforce_dependencies(struct kvm *kvm) · line 1494

Quoted 8 lines
arch/arm64/kvm/config.c +1 -1
@@ -1488,7 +1488,7 @@ void kvm_idregs_enforce_dependencies(struct kvm *kvm)
 
 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) {

All this stuff should really be cleaned up by introducing the correct level of abstraction. I have a local untested hack that does this:

arch/arm64/include/asm/kvm_host.h +14 -3
index ed00637f1d7a8..65d34e2da17d2 100644
@@ -1639,11 +1639,16 @@ void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu);
 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu);
 bool kvm_pkvm_vcpu_is_powered_off(struct kvm_vcpu *vcpu);
 
+static inline u64 *__vm_ftr_reg_by_idx(struct kvm_vm_id_regs *id_regs, u32 idx)
+{
+	return &id_regs->ftr_reg[idx];
+}
+
 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 &id_regs->ftr_reg[IDREG_IDX(reg)];
+		return __vm_ftr_reg_by_idx(id_regs, IDREG_IDX(reg));
 	case SYS_CTR_EL0:
 		return &id_regs->ctr_el0;
 	case SYS_MIDR_EL1:
@@ -1658,8 +1663,14 @@ static inline u64 *__vm_id_reg(struct kvm_vm_id_regs *id_regs, u32 reg)
 	}
 }
 
-#define kvm_read_vm_id_reg(kvm, reg)					\
-	({ u64 __val = *__vm_id_reg(&(kvm)->arch.id_regs, reg); __val; })
+#define kvm_get_vm_ftr_reg_by_idx(kvm, idx)					\
+	({ u64 *__valp = __vm_ftr_reg_by_idx(&(kvm)->arch.id_regs, idx); __valp; })
+
+#define kvm_get_vm_id_reg(kvm, reg)					\
+	({ u64 *__valp = __vm_id_reg(&(kvm)->arch.id_regs, reg); __valp; })
+
+#define kvm_read_vm_id_reg(kvm, reg)				\
+	({ u64 __val = *kvm_get_vm_id_reg(kvm, reg); __val; })
 
 void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);
 
arch/arm64/kvm/config.c +7 -6
index 399693203e934..37eae14c3924a 100644
@@ -1565,7 +1565,7 @@ static const struct reg_feat_map_desc *idreg_feat_maps[] = {
 static bool idreg_apply_deps(struct kvm *kvm, const struct reg_feat_map_desc *r)
 {
 	struct resx resx = {};
-	u64 val;
+	u64 val, *valp;
 
 	for (int i = 0; i < r->bit_feat_map_sz; i++) {
 		if ((r->bit_feat_map[i].flags & CALL_FUNC) &&
@@ -1573,11 +1573,12 @@ static bool idreg_apply_deps(struct kvm *kvm, const struct reg_feat_map_desc *r)
 			resx.res0 |= reg_feat_map_bits(&r->bit_feat_map[i]);
 	}
 
-	val = kvm->arch.id_regs.ftr_reg[r->idreg_idx];
-	kvm->arch.id_regs.ftr_reg[r->idreg_idx] &= ~resx.res0;
-	kvm->arch.id_regs.ftr_reg[r->idreg_idx] |= resx.res1;
+	valp = kvm_get_vm_ftr_reg_by_idx(kvm, r->idreg_idx);
+	val = *valp;
+	*valp &= ~resx.res0;
+	*valp |= resx.res1;
 
-	return val != kvm->arch.id_regs.ftr_reg[r->idreg_idx];
+	return val != *valp;
 }
 
 void kvm_idregs_enforce_dependencies(struct kvm *kvm)
@@ -1594,7 +1595,7 @@ void kvm_idregs_enforce_dependencies(struct kvm *kvm)
 
 static bool idreg_feat_match(struct kvm *kvm, const struct reg_bits_to_feat_map *map)
 {
-	u64 regval = kvm->arch.id_regs.ftr_reg[map->regidx];
+	u64 regval = *kvm_get_vm_ftr_reg_by_idx(kvm, map->regidx);
 	u64 regfld = (regval >> map->shift) & GENMASK(map->width - 1, 0);
 
 	if (map->sign) {

arch/arm64/kvm/hyp/nvhe/pkvm.c static void pkvm_init_features_from_host(struct pkvm_hyp_vm *hyp_vm, const struc · line 402

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
index fc0475f0877d..41ee83290697 100644
@@ -399,7 +399,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;

This stuff is just terrible code, and needs to be cleaned up. It should read something like:

arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
index ef936dee20ad3..ad100df6a8b0d 100644
@@ -413,7 +413,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.id_regs.ctr_el0 = host_kvm->arch.id_regs.ctr_el0;
+	*kvm_get_vm_id_reg(kvm, SYS_CTR_EL0) = kvm_read_vm_id_reg((struct kvm *)host_kvm, SYS_CTR_EL0);
 
 	/*
 	 * pKVM runs only GICv3. Anything else becomes 0, the model of a VM

Overall, I'd like to see a first patch introducing helpers that the code uses everywhere possible, then another patch that introduces the structure, the renaming, and changes the accessors without touching the rest of the code.

Thanks,

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

Re: [PATCH v3 01/23] KVM: arm64: Refactor idreg caching into dedicated structure

Quoted 3 lines · Marc Zyngier
Quoted 1 line · Steffen Eiden

...

Quoted 5 lines

Overall, I'd like to see a first patch introducing helpers that the code uses everywhere possible, then another patch that introduces the structure, the renaming, and changes the accessors without touching the rest of the code.

Sounds good. I'll implement that.

Thank you!

Steffen

author reviewer
#5 lore raw

PATCH v3 2/23 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. s390 needs code from the shared code to define the sys_reg_descs. But also the shared code needs the definition of the sys_reg_array and we cannot forward declare arrays and use their size before it is properly defined. This patch solves this chicken or egg problem. For the time being s390 cannot process the full arm64 defined sys_reg_descs array.

No functional changes.

---
 arch/arm64/kvm/sys_regs.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
arch/arm64/kvm/sys_regs.c +14 -12
index 0f0b0495b710..2b28b929f9f3 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;
@@ -5957,7 +5959,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);
@@ -5980,7 +5982,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.56.0
#6 lore raw

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

Sashiko has reviewed this patch and found no issues.
Quoted 12 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. s390 needs code from the shared code to define the sys_reg_descs. But also the shared code needs the definition of the sys_reg_array and we cannot forward declare arrays and use their size before it is properly defined. This patch solves this chicken or egg problem. For the time being s390 cannot process the full arm64 defined sys_reg_descs array.

Quoted 5 lines

No functional changes.

Sashiko has reviewed this patch and found no issues.

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

PATCH v3 3/23 arm64: sysreg: Convert OSLSR to automatic register generation

In reply to Steffen Eiden

Convert OSLSR to automatic register generation as per DDI0601. As OSLSR_OLSM is a composite field using bit 0 and bit 3 the combined values are still defined manually. While at it rename OSLSR_EL1_OSLM_IMPLEMENTED to OSLSR_EL1_OSLM_IMP to be consistent with the rest of the system register values.

No functional changes.

---
 arch/arm64/include/asm/sysreg.h | 12 +++++++-----
 arch/arm64/kvm/sys_regs.c       |  2 +-
 arch/arm64/tools/sysreg         | 14 ++++++++++++++
 3 files changed, 22 insertions(+), 6 deletions(-)
arch/arm64/include/asm/sysreg.h +7 -5
index e3b60ee03023..2ec9ba9cd896 100644
@@ -186,11 +186,13 @@
 #define SYS_DBGWCRn_EL1(n)		sys_reg(2, 0, 0, n, 7)
 #define SYS_MDRAR_EL1			sys_reg(2, 0, 1, 0, 0)
 
-#define SYS_OSLSR_EL1			sys_reg(2, 0, 1, 1, 4)
-#define OSLSR_EL1_OSLM_MASK		(BIT(3) | BIT(0))
-#define OSLSR_EL1_OSLM_NI		0
-#define OSLSR_EL1_OSLM_IMPLEMENTED	BIT(3)
-#define OSLSR_EL1_OSLK			BIT(1)
+#define OSLSR_EL1_OSLM_MASK (OSLSR_EL1_OSLM0_MASK | OSLSR_EL1_OSLM1_MASK)
+#define OSLSR_EL1_OSLM_NI						\
+	((OSLSR_EL1_OSLM1_NI << OSLSR_EL1_OSLM1_SHIFT) |		\
+	(OSLSR_EL1_OSLM0_VAL_0 << OSLSR_EL1_OSLM0_SHIFT))
+#define OSLSR_EL1_OSLM_IMP						\
+	((OSLSR_EL1_OSLM1_IMP << OSLSR_EL1_OSLM1_SHIFT) |		\
+	(OSLSR_EL1_OSLM0_VAL_0 << OSLSR_EL1_OSLM0_SHIFT))
 
 #define SYS_OSDLR_EL1			sys_reg(2, 0, 1, 3, 4)
 #define SYS_DBGPRCR_EL1			sys_reg(2, 0, 1, 4, 4)
arch/arm64/kvm/sys_regs.c +1 -1
index 2b28b929f9f3..8c91de6a4097 100644
@@ -3309,7 +3309,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_MDRAR_EL1), trap_raz_wi },
 	{ SYS_DESC(SYS_OSLAR_EL1), trap_oslar_el1 },
 	{ SYS_DESC(SYS_OSLSR_EL1), trap_oslsr_el1, reset_val, OSLSR_EL1,
-		OSLSR_EL1_OSLM_IMPLEMENTED, .set_user = set_oslsr_el1, },
+		OSLSR_EL1_OSLM_IMP, .set_user = set_oslsr_el1, },
 	{ SYS_DESC(SYS_OSDLR_EL1), trap_raz_wi },
 	{ SYS_DESC(SYS_DBGPRCR_EL1), trap_raz_wi },
 	{ SYS_DESC(SYS_DBGCLAIMSET_EL1), trap_raz_wi },
arch/arm64/tools/sysreg +14 -0
index 997c62ab72e6..5d38f115bac1 100644
@@ -122,6 +122,20 @@ Res0	63:1
 Field	0	OSLK
 EndSysreg
 
+Sysreg	OSLSR_EL1	2	0	1	1	4
+Res0	63:4
+UnsignedEnum	3	OSLM1
+	 0b0	NI
+	 0b1	IMP
+EndEnum
+UnsignedEnum	2	nTT
+EndEnum
+Field	1	OSLK
+UnsignedEnum	0	OSLM0
+	 0b0	VAL_0
+EndEnum
+EndSysreg
+
 Sysreg	SPMACCESSR_EL1	2	0	9	13	3
 UnsignedEnum	63:62	P31
 	0b00	TRAP_RW
Signature · 1 line
2.56.0
#8 lore raw

Re: [PATCH v3 03/23] arm64: sysreg: Convert OSLSR to automatic register generation

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] arm64: sysreg: OSLSR_EL1_OSLK redefinition in tools/ sysreg.h breaks perf --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] arm64: sysreg: OSLSR_EL1_OSLK redefinition in tools/ sysreg.h breaks perf
--

commit 33e9aa857b4dfa390c27f99a1a44127d7465fe7b
Author: Steffen Eiden <[email protected]>

arm64: sysreg: Convert OSLSR to automatic register generation

This commit converts OSLSR_EL1 to automatic system register generation in arch/arm64/tools/sysreg and renames OSLSR_EL1_OSLM_IMPLEMENTED to OSLSR_EL1_OSLM_IMP.

Quoted 10 lines
arch/arm64/include/asm/sysreg.h +0 -2
index e3b60ee030230..2ec9ba9cd896f 100644
@@ -186,11 +186,13 @@
 #define SYS_DBGWCRn_EL1(n)		sys_reg(2, 0, 0, n, 7)
 #define SYS_MDRAR_EL1			sys_reg(2, 0, 1, 0, 0)
 
-#define SYS_OSLSR_EL1			sys_reg(2, 0, 1, 1, 4)
-#define OSLSR_EL1_OSLM_MASK		(BIT(3) | BIT(0))

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

Quoted 10 lines
arch/arm64/include/asm/sysreg.h +7 -3
-#define OSLSR_EL1_OSLM_NI		0
-#define OSLSR_EL1_OSLM_IMPLEMENTED	BIT(3)
-#define OSLSR_EL1_OSLK			BIT(1)
+#define OSLSR_EL1_OSLM_MASK (OSLSR_EL1_OSLM0_MASK | OSLSR_EL1_OSLM1_MASK)
+#define OSLSR_EL1_OSLM_NI						\
+	((OSLSR_EL1_OSLM1_NI << OSLSR_EL1_OSLM1_SHIFT) |		\
+	(OSLSR_EL1_OSLM0_VAL_0 << OSLSR_EL1_OSLM0_SHIFT))
+#define OSLSR_EL1_OSLM_IMP						\
+	((OSLSR_EL1_OSLM1_IMP << OSLSR_EL1_OSLM1_SHIFT) |		\
+	(OSLSR_EL1_OSLM0_VAL_0 << OSLSR_EL1_OSLM0_SHIFT))

[Severity: Medium]
Should tools/arch/arm64/include/asm/sysreg.h receive the same update?

The tools build generates its own asm/sysreg-defs.h from arch/arm64/tools/sysreg, which now defines OSLSR_EL1_OSLK as GENMASK(1, 1). Because tools/arch/arm64/include/asm/sysreg.h includes asm/sysreg-defs.h and still defines OSLSR_EL1_OSLK as BIT(1), building tools/perf fails under -Werror with a macro redefinition error.

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

PATCH v3 4/23 arm64: Prepare headers for sysreg sharing with s390

In reply to Steffen Eiden

Move the get-spectre-state functions to a convenient location. Guard GICv5 headers away from s390 for arm_arch_timer.h.

No functional changes.

---
 arch/arm64/include/asm/spectre.h | 10 +++++-----
 include/kvm/arm_arch_timer.h     |  3 +++
 2 files changed, 8 insertions(+), 5 deletions(-)
arch/arm64/include/asm/spectre.h +5 -5
index 296ae3420bfd..2ecc9eddbebb 100644
@@ -26,6 +26,11 @@ enum mitigation_state {
 	SPECTRE_VULNERABLE,
 };
 
+enum mitigation_state arm64_get_spectre_v2_state(void);
+enum mitigation_state arm64_get_spectre_v4_state(void);
+enum mitigation_state arm64_get_meltdown_state(void);
+enum mitigation_state arm64_get_spectre_bhb_state(void);
+
 struct pt_regs;
 struct task_struct;
 
@@ -81,21 +86,16 @@ static __always_inline void arm64_apply_bp_hardening(void)
 		d->fn();
 }
 
-enum mitigation_state arm64_get_spectre_v2_state(void);
 bool has_spectre_v2(const struct arm64_cpu_capabilities *cap, int scope);
 void spectre_v2_enable_mitigation(const struct arm64_cpu_capabilities *__unused);
 
 bool has_spectre_v3a(const struct arm64_cpu_capabilities *cap, int scope);
 void spectre_v3a_enable_mitigation(const struct arm64_cpu_capabilities *__unused);
 
-enum mitigation_state arm64_get_spectre_v4_state(void);
 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);
 
-enum mitigation_state arm64_get_meltdown_state(void);
-
-enum mitigation_state arm64_get_spectre_bhb_state(void);
 bool is_spectre_bhb_affected(const struct arm64_cpu_capabilities *entry, int scope);
 extern bool __nospectre_bhb;
 u8 get_spectre_bhb_loop_value(void);
include/kvm/arm_arch_timer.h +3 -0
index 80f96ea59f1a..dcffad87b88f 100644
@@ -10,7 +10,10 @@
 #include <linux/clocksource.h>
 #include <linux/hrtimer.h>
 
+/* no GICv5 for s390 */
+#ifndef __s390__
 #include <linux/irqchip/arm-gic-v5.h>
+#endif /* !__s390__ */
 
 enum kvm_arch_timers {
 	TIMER_PTIMER,
Signature · 1 line
2.56.0
#10 lore raw

Re: [PATCH v3 04/23] arm64: Prepare headers for sysreg sharing with s390

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

Move the get-spectre-state functions to a convenient location. Guard GICv5 headers away from s390 for arm_arch_timer.h.

No functional changes.

Sashiko has reviewed this patch and found no issues.

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

PATCH v3 5/23 arm64: Share system register related headers and functions 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. The new shared headers cover cover CPU identification, Exception emulation, ID register state, Mitigation state, and the kvm system register management infrastructure. Also add definitions useful for later feature additions to reduce further churn.

With these headers s390 is able to implement system register handling for its arm64 guests.

---
 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    | 14 +++++++-------
 arch/arm64/include/asm/spectre.h     |  3 +++
 arch/arm64/kvm/sys_regs.h            |  1 +
 6 files changed, 21 insertions(+), 8 deletions(-)
arch/arm64/include/asm/cache.h +3 -0
index 10a7ffadee3d..ee3078c68768 100644
@@ -8,6 +8,7 @@
 #define L1_CACHE_SHIFT		(6)
 #define L1_CACHE_BYTES		(1 << L1_CACHE_SHIFT)
 
+#if ARM64_S390_COMMON == 1
 #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 == 1 */
+
 /*
  * 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 6002d1d5f55f..a8225467cb09 100644
@@ -5,6 +5,7 @@
 #ifndef __ASM_CPUTYPE_H
 #define __ASM_CPUTYPE_H
 
+#if ARM64_S390_COMMON == 1
 #define INVALID_HWID		ULONG_MAX
 
 #define MPIDR_UP_BITMASK	(0x1 << 30)
@@ -261,6 +262,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 == 1 */
+
 #ifndef __ASSEMBLER__
 
 #include <asm/sysreg.h>
arch/arm64/include/asm/kvm_emulate.h +4 -1
index 334211b8b2ee..4ad190d4559b 100644
@@ -23,6 +23,7 @@
 #include <asm/cputype.h>
 #include <asm/virt.h>
 
+#if ARM64_S390_COMMON == 1
 #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 == 1 */
+
 #define kvm_exception_type_names		\
 	{ except_type_sync,	"SYNC"   },	\
 	{ except_type_irq,	"IRQ"    },	\
@@ -507,13 +510,13 @@ static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu)
 	return esr_abt_is_sea(kvm_vcpu_get_esr(vcpu));
 }
 
+#if ARM64_S390_COMMON == 1
 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);
 }
 
-#if ARM64_S390_COMMON == 1
 static inline bool esr_abt_is_write_fault(unsigned long esr)
 {
 	if (esr_abt_is_s1ptw(esr)) {
arch/arm64/include/asm/kvm_host.h +7 -7
index 7f051dad79c7..01de095cf0fb 100644
@@ -1332,9 +1332,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;
 };
@@ -1371,11 +1368,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;
@@ -1386,6 +1381,11 @@ int __init populate_nv_trap_config(void);
 void kvm_calculate_traps(struct kvm_vcpu *vcpu);
 
 #if ARM64_S390_COMMON == 1
+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);
@@ -1623,8 +1623,6 @@ static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature)
 
 #define kvm_vcpu_initialized(v) vcpu_get_flag(v, VCPU_INITIALIZED)
 
-#endif /* ARM64_S390_COMMON == 1 */
-
 int kvm_trng_call(struct kvm_vcpu *vcpu);
 #ifdef CONFIG_KVM
 extern phys_addr_t hyp_mem_base;
@@ -1711,6 +1709,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 == 1 */
+
 /* Check for a given level of PAuth support */
 #define kvm_has_pauth(k, l)						\
 	({								\
arch/arm64/include/asm/spectre.h +3 -0
index 2ecc9eddbebb..4c6c07c9b515 100644
@@ -19,6 +19,7 @@
 #include <asm/cpufeature.h>
 #include <asm/virt.h>
 
+#if ARM64_S390_COMMON == 1
 /* Watch out, ordering is important here. */
 enum mitigation_state {
 	SPECTRE_UNAFFECTED,
@@ -31,6 +32,8 @@ enum mitigation_state arm64_get_spectre_v4_state(void);
 enum mitigation_state arm64_get_meltdown_state(void);
 enum mitigation_state arm64_get_spectre_bhb_state(void);
 
+#endif /* ARM64_S390_COMMON == 1 */
+
 struct pt_regs;
 struct task_struct;
 
arch/arm64/kvm/sys_regs.h +1 -0
index a3cccad2766f..7cb7ad208e05 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.56.0
#12 lore raw

Re: [PATCH v3 05/23] arm64: Share system register related headers and functions with s390

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] arm64: cache.h: -Wundef build failure in vdso32 on ARM64_S390_COMMON --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] arm64: cache.h: -Wundef build failure in vdso32 on ARM64_S390_COMMON
--

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

arm64: Share system register related headers and functions with s390

This commit marks arm64 system register, CPU identification, exception emulation, and mitigation definitions as shared with s390.

Quoted 13 lines
arch/arm64/include/asm/cache.h +1 -0
index 10a7ffadee3da..ee3078c687687 100644
@@ -8,6 +8,7 @@
 #define L1_CACHE_SHIFT		(6)
 #define L1_CACHE_BYTES		(1 << L1_CACHE_SHIFT)
 
+#if ARM64_S390_COMMON == 1
 #define CLIDR_LOUU_SHIFT	27
 #define CLIDR_LOC_SHIFT		24
 #define CLIDR_LOUIS_SHIFT	21

arch/arm64/include/asm/cache.h line 29

Quoted 4 lines
arch/arm64/include/asm/cache.h +1 -0
@@ -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 == 1 */

[Severity: High]
Does evaluating ARM64_S390_COMMON outside the !defined(BUILD_VDSO) guard break the 32-bit compat vDSO build with -Wundef?

arch/arm64/kernel/vdso32/Makefile sets VDSO_CPPFLAGS from scratch without inheriting KBUILD_CPPFLAGS (where -DARM64_S390_COMMON=1 is defined) while enabling -Wundef in VDSO_CFLAGS. When arch/arm64/kernel/vdso32/vgettimeofday.c includes asm/cache.h via vdso/datapage.h -> vdso/cache.h, ARM64_S390_COMMON is undefined, causing a -Wundef warning and a build regression when CONFIG_COMPAT_VDSO=y and CONFIG_WERROR=y.

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

PATCH v3 6/23 KVM: s390: arm64: Prepare for sharing arm64 SVE and ftr-reg code

In reply to Steffen Eiden

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

No functional changes.

---
 arch/s390/kvm/arm64/arm.c   | 2 ++
 arch/s390/kvm/arm64/guest.c | 6 ++++++
 2 files changed, 8 insertions(+)
arch/s390/kvm/arm64/arm.c +2 -0
index bd9d85235f43..29d3add0bbda 100644
@@ -8,6 +8,7 @@
 #include <linux/kvm_host.h>
 #include <linux/cleanup.h>
 #include <linux/fpu.h>
+#include <linux/bitfield.h>
 
 #include <asm/access-regs.h>
 
@@ -24,6 +25,7 @@
 #include "trace.h"
 
 static unsigned long system_supported_vcpu_features(struct kvm_vcpu *vcpu);
+#define read_sanitised_ftr_reg(_id) 0
 
 #define __INCL_GEN_ARM_FILE
 #include "generated/arm.inc"
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.56.0
#14 lore raw

Re: [PATCH v3 06/23] KVM: s390: arm64: Prepare for sharing arm64 SVE and ftr-reg code

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

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

No functional changes.

Sashiko has reviewed this patch and found no issues.

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

PATCH v3 7/23 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 reset_{vm,vcpu}_ftr_id_regs just before the UAPI definitions. Also, move kvm_handle_cp14_load_store to other cp functions. This small refactoring allows to have less shared sections.

No functional changes.

---
 arch/arm64/kvm/sys_regs.c | 52 +++++++++++++++++++++++++----------------------
 1 file changed, 28 insertions(+), 24 deletions(-)
arch/arm64/kvm/sys_regs.c +28 -24
index 8c91de6a4097..e71c1b169b49 100644
@@ -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)
@@ -4888,6 +4882,12 @@ static void perform_access(struct kvm_vcpu *vcpu,
 		kvm_incr_pc(vcpu);
 }
 
+int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu)
+{
+	kvm_inject_undefined(vcpu);
+	return 1;
+}
+
 /*
  * emulate_cp --  tries to match a sys_reg access in a handling table, and
  *                call the corresponding trap handler.
@@ -5340,25 +5340,9 @@ void kvm_sys_regs_create_debugfs(struct kvm *kvm)
 			    &sr_resx_fops);
 }
 
-static void reset_vm_ftr_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *reg)
-{
-	u32 id = reg_to_encoding(reg);
-	struct kvm *kvm = vcpu->kvm;
-
-	if (test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags))
-		return;
-
-	kvm_set_vm_id_reg(kvm, id, reg->reset(vcpu, reg));
-}
-
+static void reset_vm_ftr_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *reg);
 static void reset_vcpu_ftr_id_reg(struct kvm_vcpu *vcpu,
-				  const struct sys_reg_desc *reg)
-{
-	if (kvm_vcpu_initialized(vcpu))
-		return;
-
-	reg->reset(vcpu, reg);
-}
+				  const struct sys_reg_desc *reg);
 
 /**
  * kvm_reset_sys_regs - sets system registers to reset value
@@ -5432,6 +5416,26 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+static void reset_vm_ftr_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *reg)
+{
+	u32 id = reg_to_encoding(reg);
+	struct kvm *kvm = vcpu->kvm;
+
+	if (test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags))
+		return;
+
+	kvm_set_vm_id_reg(kvm, id, reg->reset(vcpu, reg));
+}
+
+static void reset_vcpu_ftr_id_reg(struct kvm_vcpu *vcpu,
+				  const struct sys_reg_desc *reg)
+{
+	if (kvm_vcpu_initialized(vcpu))
+		return;
+
+	reg->reset(vcpu, reg);
+}
+
 /******************************************************************************
  * Userspace API
  *****************************************************************************/
Signature · 1 line
2.56.0
#16 lore raw

Re: [PATCH v3 07/23] KVM: arm64: Prepare sys_regs.c for sharing with s390

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

Prepare sharing of sys_regs.c with s390 by moving reset_{vm,vcpu}_ftr_id_regs just before the UAPI definitions. Also, move kvm_handle_cp14_load_store to other cp functions. This small refactoring allows to have less shared sections.

No functional changes.

Sashiko has reviewed this patch and found no issues.

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

PATCH v3 8/23 KVM: arm64: Share system register code with s390

In reply to Steffen Eiden

Mark functions that s390 can use to implement system registers for arm on s390 guests as shared functions. The new shared functionality covers system register infrastructure for KVM, Pointer auth checks and shareable parts of the SVE register API. In order to reduce the number of shared sections more code than s390 requires is shared. This is no problem as for the shared code unused function checking is disabled.

---
 arch/arm64/kvm/arm.c      |  3 +++
 arch/arm64/kvm/guest.c    | 11 +++++++----
 arch/arm64/kvm/sys_regs.c | 27 +++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 4 deletions(-)
arch/arm64/kvm/arm.c +3 -0
index 157bf4b81202..d83df5ff68f4 100644
@@ -366,6 +366,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 	kvm_arm_teardown_hypercalls(kvm);
 }
 
+#if ARM64_S390_COMMON == 1
 static bool kvm_has_full_ptr_auth(void)
 {
 	bool apa, gpa, api, gpi, apa3, gpa3;
@@ -400,6 +401,8 @@ static bool kvm_has_full_ptr_auth(void)
 		(apa + api + apa3) == 1);
 }
 
+#endif /* ARM64_S390_COMMON == 1 */
+
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 {
 	int r;
arch/arm64/kvm/guest.c +7 -4
index 878ea4500c5c..26ec4751e17e 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 == 1 */
-
 #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 == 1 */
+
 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;
 }
 
+#if ARM64_S390_COMMON == 1
 #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 == 1 */
+
 /*
  * 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 == 1 */
-
 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 == 1 */
+
 /**
  * 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 +27 -0
index e71c1b169b49..fa1ed13daeb9 100644
@@ -45,6 +45,7 @@
  * 64bit interface.
  */
 
+#if ARM64_S390_COMMON == 1
 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 == 1 */
+
 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);
 }
 
+#if ARM64_S390_COMMON == 1
 /* 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 == 1 */
+
 /*
  * 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;
 }
 
+#if ARM64_S390_COMMON == 1
 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 == 1 */
+
 static int set_oslsr_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
 			 u64 val)
 {
@@ -1012,6 +1021,7 @@ static u64 reset_actlr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	return actlr;
 }
 
+#if ARM64_S390_COMMON == 1
 static u64 reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	u64 mpidr = kvm_calculate_mpidr(vcpu);
@@ -1020,6 +1030,8 @@ static u64 reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	return mpidr;
 }
 
+#endif /* ARM64_S390_COMMON == 1 */
+
 static unsigned int hidden_visibility(const struct kvm_vcpu *vcpu,
 				      const struct sys_reg_desc *r)
 {
@@ -1589,6 +1601,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
 #define AMU_AMEVCNTR1_EL0(n) { SYS_DESC(SYS_AMEVCNTR1_EL0(n)), undef_access }
 #define AMU_AMEVTYPER1_EL0(n) { SYS_DESC(SYS_AMEVTYPER1_EL0(n)), undef_access }
 
+#if ARM64_S390_COMMON == 1
 static unsigned int ptrauth_visibility(const struct kvm_vcpu *vcpu,
 			const struct sys_reg_desc *rd)
 {
@@ -1609,6 +1622,8 @@ static unsigned int ptrauth_visibility(const struct kvm_vcpu *vcpu,
 	__PTRAUTH_KEY(k ## KEYLO_EL1),					\
 	__PTRAUTH_KEY(k ## KEYHI_EL1)
 
+#endif /* ARM64_S390_COMMON == 1 */
+
 static bool access_arch_timer(struct kvm_vcpu *vcpu,
 			      struct sys_reg_params *p,
 			      const struct sys_reg_desc *r)
@@ -1906,6 +1921,7 @@ static int arm64_check_features(struct kvm_vcpu *vcpu,
 	return 0;
 }
 
+#if ARM64_S390_COMMON == 1
 static u8 pmuver_to_perfmon(u8 pmuver)
 {
 	switch (pmuver) {
@@ -2840,6 +2856,8 @@ static bool bad_redir_trap(struct kvm_vcpu *vcpu,
 	.val = 0,				\
 }
 
+#endif /* ARM64_S390_COMMON == 1 */
+
 static bool access_sp_el1(struct kvm_vcpu *vcpu,
 			  struct sys_reg_params *p,
 			  const struct sys_reg_desc *r)
@@ -3212,6 +3230,7 @@ static void init_imp_id_regs(void)
 	boot_cpu_aidr_val = read_sysreg(aidr_el1);
 }
 
+#if ARM64_S390_COMMON == 1
 static u64 reset_imp_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 {
 	switch (reg_to_encoding(r)) {
@@ -3269,6 +3288,8 @@ static int set_imp_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
 	.val = mask,					\
 	}
 
+#endif /* ARM64_S390_COMMON == 1 */
+
 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 +4854,7 @@ static const struct sys_reg_desc cp15_64_regs[] = {
 	{ SYS_DESC(SYS_AARCH32_CNTVCTSS),     access_arch_timer },
 };
 
+#if ARM64_S390_COMMON == 1
 static bool check_sysreg_table(const struct sys_reg_desc *table, unsigned int n,
 			       bool reset_check)
 {
@@ -4882,6 +4904,8 @@ static void perform_access(struct kvm_vcpu *vcpu,
 		kvm_incr_pc(vcpu);
 }
 
+#endif /* ARM64_S390_COMMON == 1 */
+
 int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu)
 {
 	kvm_inject_undefined(vcpu);
@@ -5416,6 +5440,7 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+#if ARM64_S390_COMMON == 1
 static void reset_vm_ftr_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *reg)
 {
 	u32 id = reg_to_encoding(reg);
@@ -5757,6 +5782,8 @@ int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
 	return write_demux_regids(uindices);
 }
 
+#endif /* ARM64_S390_COMMON == 1 */
+
 #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.56.0
#18 lore raw

Re: [PATCH v3 08/23] KVM: arm64: Share system register code with s390

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: arm64: piecemeal sys_regs.c sharing and mixed PAuth/SVE changes --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: arm64: piecemeal sys_regs.c sharing and mixed PAuth/SVE changes
--

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

KVM: arm64: Share system register code with s390

This commit wraps selected pointer authentication, SVE, and system register helpers in arch/arm64/kvm/ with ARM64_S390_COMMON guards so they can be compiled into s390 KVM.

Quoted 36 lines
arch/arm64/kvm/arm.c +3 -0
index 157bf4b812020..d83df5ff68f4a 100644
@@ -366,6 +366,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 	kvm_arm_teardown_hypercalls(kvm);
 }
 
+#if ARM64_S390_COMMON == 1
 static bool kvm_has_full_ptr_auth(void)
 {
 	bool apa, gpa, api, gpi, apa3, gpa3;
@@ -400,6 +401,8 @@ static bool kvm_has_full_ptr_auth(void)
 		(apa + api + apa3) == 1);
 }
 
+#endif /* ARM64_S390_COMMON == 1 */
+
 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 {
 	int r;
arch/arm64/kvm/guest.c +1 -2
index 878ea4500c5c5..26ec4751e17e2 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 == 1 */
-
 #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 == 1 */

[ ... ]

Quoted 4 lines
arch/arm64/kvm/sys_regs.c +0 -0
index e71c1b169b491..fa1ed13daeb92 100644

[ ... ]

Quoted 9 lines
arch/arm64/kvm/sys_regs.c +2 -0
@@ -3269,6 +3288,8 @@ static int set_imp_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
 	.val = mask,					\
 	}
 
+#endif /* ARM64_S390_COMMON == 1 */
+
 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);

[ ... ]

arch/arm64/kvm/sys_regs.c int kvm_handle_sys_reg(struct kvm_vcpu *vcpu) · line 5446

Quoted 8 lines
arch/arm64/kvm/sys_regs.c +1 -0
@@ -5416,6 +5440,7 @@ int kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
+#if ARM64_S390_COMMON == 1
 static void reset_vm_ftr_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *reg)
 {
 	u32 id = reg_to_encoding(reg);

[Severity: Medium]
As reported by Marc Zyngier on v2 [1], why does this patch cherry-pick isolated helper blocks from arch/arm64/kvm/sys_regs.c instead of sharing the architectural system register code and sys_reg_descs[] table as a whole?

Leaving sys_reg_descs[], kvm_reset_sys_regs(), kvm_handle_sys_reg(), and arm64_check_features() unshared causes commit "KVM: s390: arm64: Implement arm sysreg managing infrastructure" to duplicate the architectural system register table and core handling logic in arch/s390/kvm/arm64/sys_regs.c, which risks the two implementations diverging over time. Abstracting the underlying vCPU state accessors would allow reusing sys_regs.c and its descriptor tables directly.

In addition, this commit bundles pointer authentication capability sharing in arch/arm64/kvm/arm.c and SVE helper sharing in arch/arm64/kvm/guest.c into the same patch as the system register changes, rather than splitting them to match the corresponding s390 feature patches.

[1] https://lore.kernel.org/r/[email protected]

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

PATCH v3 9/23 s390: tools: Share 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 f3f8e9d9bdd8..88dcce26179c 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 (#if ARM64_S390_COMMON == 1) only this part will be copied.
 # If the file does not contain any markers the whole file will be copied.
@@ -36,7 +37,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.56.0
#20 lore raw

Re: [PATCH v3 09/23] s390: tools: Share arm64/kvm headers

Sashiko has reviewed this patch and found no issues.
Quoted 7 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.

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

PATCH v3 10/23 s390: Add range check asm macro

In reply to Steffen Eiden

The range check macro turns cryptic assembler errors into an easy to understand error if e.g. someone mistypes an input value in an asm macro.

Add an asm macro that verifies that a given input value is in a specific range. Add macros which check for ranges often used when creating instructions. While at it, add a range check for the register numbers in the GR_NUM and VX_NUM macro.

---
 arch/s390/include/asm/insn-common-asm.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
arch/s390/include/asm/insn-common-asm.h +28 -0
index fd9b3cacb7c5..f3f10f2c92bb 100644
@@ -9,6 +9,32 @@
 
 #ifdef __ASSEMBLER__
 
+.macro	RANGECHECK	min max val
+	.if (\val < \min) || (\val > \max)
+	.error "operand out of range (\val is not between \min and \max)"
+	.endif
+.endm
+
+.macro	ASSERT_U16	val
+	RANGECHECK 0, 65535, \val
+.endm
+
+.macro	ASSERT_S16	val
+	RANGECHECK -32768, 32767, \val
+.endm
+
+.macro	ASSERT_U8	val
+	RANGECHECK 0, 255, \val
+.endm
+
+.macro	ASSERT_U4	val
+	RANGECHECK 0, 15, \val
+.endm
+
+.macro	ASSERT_U5	val
+	RANGECHECK 0, 31, \val
+.endm
+
 /*
  * GR_NUM - Retrieve general-purpose register number
  *
@@ -24,6 +50,7 @@
 	.endr
 	.if \opd == 255
 		\opd = \gr
+		ASSERT_U4	\opd
 	.endif
 .endm
 
@@ -46,6 +73,7 @@
 	.endr
 	.if \opd == 255
 		\opd = \vxr
+		ASSERT_U5	\opd
 	.endif
 .endm
 
Signature · 1 line
2.56.0
#22 lore raw

Re: [PATCH v3 10/23] s390: Add range check asm macro

Sashiko has reviewed this patch and found no issues.
Quoted 4 lines

The range check macro turns cryptic assembler errors into an easy to understand error if e.g. someone mistypes an input value in an asm macro.

Quoted 8 lines

Add an asm macro that verifies that a given input value is in a specific range. Add macros which check for ranges often used when creating instructions. While at it, add a range check for the register numbers in the GR_NUM and VX_NUM macro.

Sashiko has reviewed this patch and found no issues.

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

PATCH v3 11/23 s390: Read/write ARM system registers

In reply to Steffen Eiden

Introduce Extract Arm System Register and Set 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 | 45 +++++++++++++++++++++++++++
 arch/s390/include/asm/sae.h     | 67 +++++++++++++++++++++++++++++++++++++++++
 arch/s390/kernel/dis.c          |  1 +
 arch/s390/tools/opcodes.txt     |  2 ++
 4 files changed, 115 insertions(+)
arch/s390/include/asm/sae-asm.h new file +45 -0
new file mode 100644
index 000000000000..a1abb07b5a77
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_S390_SAE_ASM_H
+#define __ASM_S390_SAE_ASM_H
+
+#ifndef __ASM_S390_SAE_H
+#error only <asm/sae.h> can be included directly
+#endif
+
+#include <asm/insn-common-asm.h>
+
+#ifdef __ASSEMBLER__
+
+/*
+ * INSN_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	INSN_RIE_H	opc, gr1, gr3, imm2, m4
+	GR_NUM	r1, \gr1
+	GR_NUM	r3, \gr3
+	ASSERT_U16	\opc
+	ASSERT_U16	\imm2
+	ASSERT_U4	\m4
+	.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
+	INSN_RIE_H 0xed99, \r1, \r3, \i2, \m4
+.endm
+
+.macro	EASR	r1, r3, i2, m4
+	INSN_RIE_H 0xed9b, \r1, \r3, \i2, \m4
+.endm
+
+#endif /* __ASSEMBLER__ */
+#endif /* __ASM_S390_SAE_ASM_H */
arch/s390/include/asm/sae.h +67 -0

79 lines not on this page · raw message

arch/s390/kernel/dis.c +1 -0
index d2d299ba8b86..065264667db3 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_R0RUU]    = { 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..22fb9c500a56 100644
@@ -1255,6 +1255,8 @@ ed64	ley	RXY_FRRD
 ed65	ldy	RXY_FRRD
 ed66	stey	RXY_FRRD
 ed67	stdy	RXY_FRRD
+ed99	sasr	RIE_R0RUU
+ed9b	easr	RIE_R0RUU
 eda8	czdt	RSL_LRDFU
 eda9	czxt	RSL_LRDFU
 edaa	cdzt	RSL_LRDFU
Signature · 1 line
2.56.0
#24 lore raw

Re: [PATCH v3 11/23] s390: Read/write ARM system registers

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

Introduce Extract Arm System Register and Set 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.

Sashiko has reviewed this patch and found no issues.

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

PATCH v3 12/23 s390: 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 | 53 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
arch/s390/include/asm/timex.h +53 -0
index 23421b6eb225..8ee69fa3c3a1 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 */
 #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,44 @@ 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
+ *
+ * Converts the Arm guest physical time into the Arm guest time offset.
+ *
+ * The caller must ensure that this PTFF function is available,
+ * before calling this function. The condition code is discarded.
+ *
+ * Return: Arm guest time offset
+ */
+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
+ *
+ * Converts the Arm guest time offset into the Arm guest physical time.
+ *
+ * The caller must ensure that this PTFF function is available,
+ * before calling this function. The condition code is discarded.
+ *
+ * Return: Arm guest physical time
+ */
+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.56.0
#26 lore raw

Re: [PATCH v3 12/23] s390: Query arm guest time

Sashiko has reviewed this patch and found no issues.
Quoted 8 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.

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

PATCH v3 13/23 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                  |  12 +++
 arch/s390/include/asm/kvm_host_arm64_types.h | 115 +++++++++++++++++++++++++++
 arch/s390/include/asm/sae.h                  |  26 ++++++
 arch/s390/kernel/Makefile                    |   2 +-
 arch/s390/kernel/aef.c                       |  94 ++++++++++++++++++++++
 arch/s390/kvm/arm64/Makefile                 |   1 +
 arch/s390/kvm/arm64/arm.c                    |   6 +-
 arch/s390/kvm/arm64/qaaf.c                   |  83 +++++++++++++++++++
 arch/s390/kvm/arm64/qaaf.h                   |  12 +++
 arch/s390/tools/opcodes.txt                  |   1 +
 10 files changed, 350 insertions(+), 2 deletions(-)
arch/s390/include/asm/aef.h new file +12 -0
new file mode 100644
index 000000000000..06519cc575be
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _ASM_S390_AEF_H
+#define _ASM_S390_AEF_H
+
+#include <asm/kvm_host_arm64_types.h>
+
+const struct kvm_sae_save_area *aef_gisrsa(void);
+const struct qaaf_qmc_block *aef_qmc(void);
+bool aef_usable(void);
+
+#endif /* _ASM_S390_AEF_H */
arch/s390/include/asm/kvm_host_arm64_types.h +115 -0

119 lines not on this page · raw message

arch/s390/include/asm/sae.h +26 -0
index cac4fbf58e88..d1b9af770108 100644
@@ -12,6 +12,7 @@
 asmlinkage void sae(phys_addr_t sae_block_phys);
 
 #include <linux/io.h>
+#include <asm/extable.h>
 #include <asm/kvm_host_arm64_types.h>
 #include <asm/insn-common-asm-emit.h>
 
@@ -112,5 +113,30 @@ static __always_inline void lasrm(struct kvm_sae_save_area *save_area)
 	);
 }
 
+/**
+ * qaaf() - Query Available Arm Features
+ * @gr0: QAAF function code
+ * @qaaf_block: Pointer to the page for the output
+ *
+ * Perform QAAF. The result is written to qaaf_block.
+ */
+static __always_inline int qaaf(u64 gr0, union qaaf_block *qaaf_block)
+{
+	int exception = 1;
+
+	asm_inline volatile(
+		"	lgr	0,%[r0]\n"
+		"0:	.insn	rre,0xb9ad0000,%[r1],0\n"
+		"	lhi	%[exc],0\n"
+		"1:\n"
+		EX_TABLE_INSN(0b, 1b)
+		: "=m" (*qaaf_block), [exc] "+d" (exception)
+		: [r1] "a" (qaaf_block), [r0] "d" (gr0)
+		: "0"
+	);
+
+	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 +94 -0

94 lines not on this page · raw message

arch/s390/kvm/arm64/Makefile +1 -0
index b48dc65b7aaa..e9587c6d6de9 100644
@@ -16,6 +16,7 @@ kvm-arm64-local := \
 	inject_fault.o \
 	mmio.o \
 	mmu.o \
+	qaaf.o \
 	reset.o
 
 kvm-arm64-obj := $(kvm-arm64-local)
arch/s390/kvm/arm64/arm.c +5 -1
index 29d3add0bbda..62c2b45599b9 100644
@@ -11,6 +11,7 @@
 #include <linux/bitfield.h>
 
 #include <asm/access-regs.h>
+#include <asm/aef.h>
 
 #include <arm64/kvm_emulate.h>
 #include <arm64/sysreg.h>
@@ -20,6 +21,7 @@
 
 #include "arm.h"
 #include "handle_exit.h"
+#include "qaaf.h"
 
 #define CREATE_TRACE_POINTS
 #include "trace.h"
@@ -741,9 +743,11 @@ 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 (!sclp.has_aef || !aef_usable())
 		return -ENODEV;
 
+	kvm_init_qaaf();
+
 	return kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
 }
 
arch/s390/kvm/arm64/qaaf.c new file +83 -0

83 lines not on this page · raw message

arch/s390/kvm/arm64/qaaf.h new file +12 -0
new file mode 100644
index 000000000000..f1d50a9654b1
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef ARCH_S390_KVM_ARM64_QAAF_H
+#define ARCH_S390_KVM_ARM64_QAAF_H
+
+#include <asm/aef.h>
+
+__init void kvm_init_qaaf(void);
+struct qaaf_qmc_block *kvm_qaaf_qmc(void);
+u64 kvm_qaaf_read_ftr_reg(u32 id);
+
+#endif
arch/s390/tools/opcodes.txt +1 -0
index 22fb9c500a56..bb4ba4038cb0 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.56.0
#28 lore raw

Re: [PATCH v3 13/23] KVM: s390: arm64: Query Available Arm features

Sashiko has reviewed this patch and found no issues.
Quoted 7 lines

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.

Quoted 5 lines

Function code 2 provides a initialised save area.

Sashiko has reviewed this patch and found no issues.

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

PATCH v3 14/23 KVM: s390: arm64: Verify machine feature set before loading

In reply to Steffen Eiden

Arm guests on s390 need more than the Arm Execution Facility alone. In addition, the PTFF time conversion functions are required. In addition the guest mapping relies on EDAT2 for large pages.

Verify that these features are present before loading the kvm_arm64 module. Every machine having the aef facility present should also support those. Print an error message if this is not the case.

---
 arch/s390/kvm/arm64/arm.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
arch/s390/kvm/arm64/arm.c +20 -1
index 62c2b45599b9..3af0b47b8377 100644
@@ -741,9 +741,28 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 	return -ENOIOCTLCMD;
 }
 
+static bool __init machine_supports_arm64_kvm(void)
+{
+	if (!sclp.has_aef || !aef_usable()) {
+		pr_info("ARM Execution Facility not available\n");
+		return false;
+	}
+
+	if (!ptff_query(PTFF_QAGTO) || !ptff_query(PTFF_QAGPT)) {
+		pr_err("No ARM time conversion functions available (unexpected machine feature set)\n");
+		return false;
+	}
+	if (!cpu_has_edat2()) {
+		pr_err("No EDAT2 facility available (unexpected machine feature set)\n");
+		return false;
+	}
+
+	return true;
+}
+
 static int __init kvm_s390_arm64_init(void)
 {
-	if (!sclp.has_aef || !aef_usable())
+	if (!machine_supports_arm64_kvm())
 		return -ENODEV;
 
 	kvm_init_qaaf();
Signature · 1 line
2.56.0
#30 lore raw

Re: [PATCH v3 14/23] KVM: s390: arm64: Verify machine feature set before loading

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

Arm guests on s390 need more than the Arm Execution Facility alone. In addition, the PTFF time conversion functions are required. In addition the guest mapping relies on EDAT2 for large pages.

Verify that these features are present before loading the kvm_arm64 module. Every machine having the aef facility present should also support those. Print an error message if this is not the case.

Sashiko has reviewed this patch and found no issues.

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

PATCH v3 15/23 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              |   2 +-
 arch/s390/kvm/arm64/feature.c          | 163 +++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/feature.h          |  86 +++++++++++++++++
 arch/s390/kvm/arm64/qaaf.c             |   2 +
 6 files changed, 266 insertions(+), 1 deletion(-)
arch/s390/include/asm/kvm_host_arm64.h +13 -0
index 3e69724f17c9..9afe37f6696c 100644
@@ -15,6 +15,19 @@ enum vcpu_sysreg;
 
 #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_HAVE_VCPU_RUN_PID_CHANGE
arch/s390/kvm/arm64/Makefile +1 -0
index e9587c6d6de9..e75b38071f8a 100644
@@ -11,6 +11,7 @@ ccflags-y += -I $(src) -I $(objtree)/$(obj) -I$(srctree)/arch/$(SRCARCH)/kvm/gma
 
 kvm-arm64-local := \
 	arm.o \
+	feature.o \
 	guest.o \
 	handle_exit.o \
 	inject_fault.o \
arch/s390/kvm/arm64/arm.c +1 -1
index 3af0b47b8377..ba6235629c7d 100644
@@ -20,6 +20,7 @@
 #include <kvm_mmu.h>
 
 #include "arm.h"
+#include "feature.h"
 #include "handle_exit.h"
 #include "qaaf.h"
 
@@ -27,7 +28,6 @@
 #include "trace.h"
 
 static unsigned long system_supported_vcpu_features(struct kvm_vcpu *vcpu);
-#define read_sanitised_ftr_reg(_id) 0
 
 #define __INCL_GEN_ARM_FILE
 #include "generated/arm.inc"
arch/s390/kvm/arm64/feature.c new file +163 -0

163 lines not on this page · raw message

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

86 lines not on this page · raw message

arch/s390/kvm/arm64/qaaf.c +2 -0
index f6b1750aed68..36487025013c 100644
@@ -3,6 +3,7 @@
 #include <linux/kvm_host.h>
 #include <asm/aef.h>
 #include "qaaf.h"
+#include "feature.h"
 
 static struct qaaf_qmc_block qmc_sanitized;
 
@@ -17,6 +18,7 @@ struct qaaf_qmc_block *kvm_qaaf_qmc(void)
 void __init kvm_init_qaaf(void)
 {
 	qmc_sanitized = *aef_qmc();
+	kvm_arm_host_sanitize_features(&qmc_sanitized);
 }
 
 #define _qaaf_reg_case(id) case SYS_##id: return qmc_sanitized.regs[QAAF_REG_##id]
Signature · 1 line
2.56.0
#32 lore raw

Re: [PATCH v3 15/23] KVM: s390: arm64: Implement feature sanitisation

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] KVM: s390: wrong ARM64_HAS_HCR_NV1 check in cpus_have_final_cap() - [Low] KVM: s390: missing…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] KVM: s390: wrong ARM64_HAS_HCR_NV1 check in cpus_have_final_cap()
- [Low] KVM: s390: missing ID_AFR0_EL1 masking in kvm_arm_host_sanitize_features()
- [Low] KVM: s390: malformed kernel-doc comment for read_sanitised_ftr_reg()
--

commit 1c037e7c3587ba94101456185c381c6fbc670b9c
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Implement feature sanitisation

This commit implements feature detection and ID register sanitization for arm64 KVM guests on s390 using capabilities queried via QAAF.

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

[ ... ]

Quoted 2 lines
arch/s390/kvm/arm64/feature.c new file +2 -0
+void __init kvm_arm_host_sanitize_features(struct qaaf_qmc_block *qaaf_qmc)
+{

[ ... ]

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

Quoted 8 lines
arch/s390/kvm/arm64/feature.c new file +8 -0
+	MASK_RESERVED(qaaf_qmc, ID_PFR0_EL1);
+	MASK_RESERVED(qaaf_qmc, ID_PFR1_EL1);
+	MASK_RESERVED(qaaf_qmc, ID_PFR2_EL1);
+
+	MASK_RESERVED(qaaf_qmc, ID_DFR0_EL1);
+	MASK_RESERVED(qaaf_qmc, ID_DFR1_EL1);
+
+	MASK_RESERVED(qaaf_qmc, ID_MMFR0_EL1);

[Severity: Low]
Should kvm_arm_host_sanitize_features() also mask reserved bits for ID_AFR0_EL1? kvm_qaaf_read_ftr_reg() exposes qmc_sanitized.regs[QAAF_REG_ID_AFR0_EL1], and without MASK_RESERVED(qaaf_qmc, ID_AFR0_EL1) its reserved bits [63:16] remain unmasked when queried via read_sanitised_ftr_reg().

[ ... ]

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 141

Quoted 4 lines
arch/s390/kvm/arm64/feature.c new file +4 -0
+	case ARM64_HAS_HCR_NV1:
+		reg_val = read_sanitised_ftr_reg(SYS_ID_AA64MMFR4_EL1);
+		return SYS_FIELD_GET(ID_AA64MMFR4_EL1, E2H0, reg_val) ==
+		       ID_AA64MMFR4_EL1_E2H0_IMP;

[Severity: Medium]
Does the ARM64_HAS_HCR_NV1 case check for FEAT_E2H0 instead of HCR_EL2.NV1 support?

In the Arm architecture, ID_AA64MMFR4_EL1.E2H0 is a signed field where 0 (ID_AA64MMFR4_EL1_E2H0_IMP) means HCR_EL2.E2H can be programmed to 0, 0xf (ID_AA64MMFR4_EL1_E2H0_NI) means E2H0 is not implemented but HCR_EL2.NV1 is supported if nested virtualization is present, and 0xe (ID_AA64MMFR4_EL1_E2H0_NI_NV1) means neither is implemented. On arm64, has_nv1() checks for nested virtualization support and that E2H0 is not NI_NV1:

arch/arm64/kernel/cpufeature.c:has_nv1() {
	return (__system_matches_cap(ARM64_HAS_NESTED_VIRT) &&
		!(has_cpuid_feature(entry, scope) ||
		  is_midr_in_range_list(nv1_ni_list)));
}

Checking E2H0 == ID_AA64MMFR4_EL1_E2H0_IMP causes cpus_have_final_cap() to return true whenever E2H0 is 0 even though nested virtualization is not supported on s390, and false when E2H0 is 0xf.

[ ... ]

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

[ ... ]

Quoted 5 lines
arch/s390/kvm/arm64/feature.h new file +5 -0
+/**
+ * read_sanitised_ftr_reg() - Get the value of a sys register
+ *
+ * @id - ARM sysreg id
+ *

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

Quoted 10 lines
arch/s390/kvm/arm64/feature.h new file +10 -0
+ * Get the value of a sys register describing the host configuration.
+ * The returned value indicates support by the machine (HW/FW)
+ * after some sanitisation.
+ *
+ * Example:
+ *	read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)
+ *
+ * Return: Sanitised HW value of the specified register
+ */
+static inline u64 read_sanitised_ftr_reg(u32 id)

[Severity: Low]
This isn't a bug, but the kernel-doc comment uses "@id -" instead of "@id:", which causes scripts/kernel-doc to warn that the parameter is not described in read_sanitised_ftr_reg().

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

PATCH v3 16/23 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 | 205 ++++++++-
 arch/s390/kvm/arm64/Makefile           |   4 +-
 arch/s390/kvm/arm64/Makefile.gen       |   3 +-
 arch/s390/kvm/arm64/spectre.c          |  51 +++
 arch/s390/kvm/arm64/sys_regs.c         | 815 +++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/trace.h            |  50 ++
 arch/s390/tools/Makefile.arm64h        |   4 +
 10 files changed, 1161 insertions(+), 3 deletions(-)
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 +204 -1

255 lines not on this page · raw message

arch/s390/kvm/arm64/Makefile +3 -1
index e75b38071f8a..24ac6f6f6278 100644
@@ -18,7 +18,9 @@ kvm-arm64-local := \
 	mmio.o \
 	mmu.o \
 	qaaf.o \
-	reset.o
+	reset.o \
+	spectre.o \
+	sys_regs.o
 
 kvm-arm64-obj := $(kvm-arm64-local)
 kvm-arm64-obj += $(patsubst %.o,%-arm64.o,$(kvm-y))
arch/s390/kvm/arm64/Makefile.gen +2 -1
index 9c33b068fab5..6c62ac412d57 100644
@@ -7,7 +7,8 @@
 ARM64_CFILES := \
 	arm.c \
 	guest.c \
-	mmio.c
+	mmio.c \
+	sys_regs.c
 
 quiet_cmd_extract_inc = GEN     $@
       cmd_extract_inc = mkdir -p $(dir $@); \
arch/s390/kvm/arm64/spectre.c new file +51 -0

51 lines not on this page · raw message

arch/s390/kvm/arm64/sys_regs.c new file +815 -0

815 lines not on this page · raw message

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

62 lines not on this page · raw message

arch/s390/tools/Makefile.arm64h +4 -0
index 88dcce26179c..9761b0bfcfbd 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.56.0
#34 lore raw

Re: [PATCH v3 16/23] KVM: s390: arm64: Implement arm sysreg managing infrastructure

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] KVM: s390: arm64: preemptible smp_processor_id() in _vcpu_read/write_sys_reg() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] KVM: s390: arm64: preemptible smp_processor_id() in _vcpu_read/write_sys_reg()
--

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

KVM: s390: arm64: Implement arm sysreg managing infrastructure

This commit implements the infrastructure for the s390 host to read, write, and trap guest arm64 system registers using EASR/SASR instructions and shared arm64 sysreg tables.

Quoted 4 lines
arch/s390/include/asm/kvm_host_arm64.h +0 -0
index 9afe37f6696c8..35b6d8972359c 100644

[ ... ]

Quoted 39 lines
arch/s390/include/asm/kvm_host_arm64.h +35 -0
@@ -219,4 +245,181 @@ 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) \
+	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(v, r, op, val)				\
+	do {								\
+		u64 __v = vcpu_read_sys_reg(v, r);			\
+		__v op (val);						\
+		vcpu_write_sys_reg(v, __v, r);				\
+	} while (0)
+
+/**
+ * _vcpu_read_sys_reg() - read a guest sysreg with easr
+ * @C: vcpu to read from; if loaded, may only be called from its own thread
+ * @R: sysreg id; must be readable by easr; must be compile time constant
+ *
+ *   if SYSREGS_ON_CPU: proceed with flags = 0
+ *   otherwise:         proceed with flags = EASR_FLAG_SA
+ *
+ */
+#define _vcpu_read_sys_reg(C, R) \
+	({	BUILD_BUG_ON(!__builtin_constant_p((R))); \
+		KVM_BUG_ON(vcpu_is_loaded(C) && smp_processor_id() != (C)->cpu, (C)->kvm); \
+		(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
+ *
+ * @C: vcpu to write to; if loaded, may only be called from its own thread
+ * @V: value to write
+ * @R: sysreg id; must be writable by sasr; must be compile time constant

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

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

[Severity: High]
Can _vcpu_read_sys_reg() and _vcpu_write_sys_reg() call smp_processor_id() with preemption enabled?

vcpu_load() sets vcpu->cpu and then calls put_cpu(), and kvm_arch_vcpu_ioctl_run() re-enables local interrupts before calling handle_exit(). When exit handlers such as kvm_handle_sys_reg() or kvm_handle_guest_abort() read or write save-area registers, vcpu_is_loaded() is true in preemptible context.

On CONFIG_DEBUG_PREEMPT=y kernels, smp_processor_id() triggers a check_preemption_disabled() BUG splat. On any preemptible kernel, if the vCPU thread is migrated between reading (C)->cpu and evaluating smp_processor_id(), the preempt notifier updates (C)->cpu to the new CPU and KVM_BUG_ON() fires falsely, killing the VM.

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

Re: [PATCH v3 16/23] KVM: s390: arm64: Implement arm sysreg managing infrastructure

In reply to Steffen Eiden

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

Quoted 12 lines · Steffen Eiden
arch/s390/include/asm/kvm_host_arm64.h +12 -0
+/**
+ * _vcpu_read_sys_reg() - read a guest sysreg with easr
+ * @C: vcpu to read from; if loaded, may only be called from its own thread
+ * @R: sysreg id; must be readable by easr; must be compile time constant
+ *
+ *   if SYSREGS_ON_CPU: proceed with flags = 0
+ *   otherwise:         proceed with flags = EASR_FLAG_SA
+ *
+ */
+#define _vcpu_read_sys_reg(C, R) \
+	({	BUILD_BUG_ON(!__builtin_constant_p((R))); \
+		KVM_BUG_ON(vcpu_is_loaded(C) && smp_processor_id() != (C)->cpu, (C)->kvm); \

ah, this slipped through. the condition checked here was supposed to be:

    vcpu_is_loaded(C) && kvm_get_running_vcpu() == (C)

same for _vcpu_write_sys_reg below.

Quoted 23 lines
arch/s390/include/asm/kvm_host_arm64.h +21 -0
+		(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
+ *
+ * @C: vcpu to write to; if loaded, may only be called from its own thread
+ * @V: value to write
+ * @R: sysreg id; must be writable by sasr; must be compile time constant
+
+ *   if SYSREGS_ON_CPU: proceed with flags = 0
+ *   otherwise:         proceed with flags = SASR_FLAG_INITIALIZED
+ */
+#define _vcpu_write_sys_reg(C, V, R) \
+	({	BUILD_BUG_ON(!__builtin_constant_p((R))); \
+		KVM_BUG_ON(vcpu_is_loaded(C) && smp_processor_id() != (C)->cpu, (C)->kvm); \
+		(vcpu_is_loaded(C)) \
+			? __vcpu_write_sr((C), (V), (R), 0) \
+			: __vcpu_write_sr((C), (V), (R), SASR_FLAG_INITIALIZED); })
+
Signature · 1 line
2.56.0
author reviewer
#36 lore raw

PATCH v3 17/23 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              | 29 +++++++++++++++++++++++
 arch/s390/kvm/arm64/guest.c            | 12 ++++++----
 arch/s390/kvm/arm64/handle_exit.c      |  1 +
 arch/s390/kvm/arm64/reset.c            | 42 ++++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/sys_regs.c         |  1 +
 6 files changed, 84 insertions(+), 4 deletions(-)
arch/s390/include/asm/kvm_host_arm64.h +3 -0
index 35b6d8972359..44deae423ace 100644
@@ -157,6 +157,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 +29 -0
index ba6235629c7d..62fd71af8968 100644
@@ -15,6 +15,7 @@
 
 #include <arm64/kvm_emulate.h>
 #include <arm64/sysreg.h>
+#include <arm64/sys_regs.h>
 
 #include <gmap.h>
 #include <kvm_mmu.h>
@@ -50,6 +51,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 		break;
 	case KVM_CAP_IOEVENTFD:
 	case KVM_CAP_PRE_FAULT_MEMORY:
+	case KVM_CAP_ONE_REG:
 		ret = 1;
 		break;
 	default:
@@ -136,6 +138,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;
 
@@ -193,6 +197,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	sae_block->mso = 0L;
 	sae_block->msl = vcpu->kvm->arch.mem_limit - 1;
 	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",
@@ -207,6 +212,7 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
 
 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
 {
+	kfree(vcpu->arch.ccsidr);
 	kvm_s390_free_mmu_cache(vcpu->arch.mc);
 
 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
@@ -218,6 +224,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 	vcpu->cpu = cpu;
 
 	lasrm(&vcpu->arch.save_area);
+
+	vcpu->arch.sae_block.gpto = vcpu->kvm->arch.epd;
 }
 
 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
@@ -261,6 +269,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;
@@ -271,6 +280,10 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
 	if (likely(READ_ONCE(vcpu->pid)))
 		return 0;
 
+	ret = kvm_vcpu_finalize_sys_regs(vcpu);
+	if (ret)
+		return ret;
+
 	scoped_guard(mutex, &kvm->arch.config_lock)
 		set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
 
@@ -477,9 +490,17 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 		trace_kvm_entry(*vcpu_pc(vcpu));
 
 		adjust_pc(vcpu);
+		vcpu_write_host_sys_reg(vcpu, vcpu->arch.hcr_el2, SYS_HCR_EL2);
+		vcpu_write_host_sys_reg(vcpu, vcpu->arch.hcrx_el2, 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);
 
 		arm_vcpu_enter_exit(virt_to_phys(&vcpu->arch.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);
+
 		vcpu->mode = OUTSIDE_GUEST_MODE;
 		vcpu->stat.exits++;
 
@@ -762,11 +783,19 @@ static bool __init machine_supports_arm64_kvm(void)
 
 static int __init kvm_s390_arm64_init(void)
 {
+	int ret;
+
 	if (!machine_supports_arm64_kvm())
 		return -ENODEV;
 
 	kvm_init_qaaf();
 
+	ret = kvm_sys_reg_table_init();
+	if (ret) {
+		kvm_info("Error initializing system register tables\n");
+		return ret;
+	}
+
 	return kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
 }
 
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 e0c7773c2165..1653163ed6d6 100644
@@ -62,6 +62,7 @@ static int kvm_handle_unknown_ec(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

arch/s390/kvm/arm64/sys_regs.c +1 -0
index b1d4335779c7..417a05ce9015 100644
@@ -441,6 +441,7 @@ static int arch_timer_set_user(struct kvm_vcpu *vcpu,
 	case SYS_CNTPCT_EL0:
 		gpto = ptff_qagto(val);
 		vcpu->arch.sae_block.gpto = gpto;
+		vcpu->kvm->arch.epd = gpto;
 		return 0;
 	}
 
Signature · 1 line
2.56.0
#37 lore raw

Re: [PATCH v3 17/23] KVM: s390: arm64: Integrate sysreg into the host

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] KVM: s390: arm64: hcrx_el2 not updated after finalizing ID regs - [Medium] KVM: s390: arm64…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] KVM: s390: arm64: hcrx_el2 not updated after finalizing ID regs
- [Medium] KVM: s390: arm64: preemptible smp_processor_id() in kvm_handle_sys_reg()
- [Medium] KVM: s390: arm64: missing reset of ctxt.elr_el1, spsr_el1, and sp_el1
--

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

KVM: s390: arm64: Integrate sysreg into the host

This commit integrates the arm64 system register management infrastructure into the s390 host by initializing and resetting system registers on vCPU creation and reset, finalizing them on the first vCPU run, handling ESR_ELx_EC_SYS64 exits, and exposing them via KVM_CAP_ONE_REG.

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +0 -0
index ba6235629c7d9..62fd71af8968c 100644

[ ... ]

arch/s390/kvm/arm64/arm.c int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) · line 289

Quoted 11 lines
arch/s390/kvm/arm64/arm.c +4 -0
@@ -271,6 +280,10 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
 	if (likely(READ_ONCE(vcpu->pid)))
 		return 0;
 
+	ret = kvm_vcpu_finalize_sys_regs(vcpu);
+	if (ret)
+		return ret;
+
 	scoped_guard(mutex, &kvm->arch.config_lock)
 		set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
 

[Severity: Medium]
Should kvm_arch_vcpu_run_pid_change() recompute vcpu->arch.hcrx_el2 (for example via vcpu_reset_hcrx()) after finalizing ID registers?

vcpu_reset_hcrx() sets HCRX_EL2_MSCEn from ID_AA64ISAR2_EL1 during kvm_reset_vcpu() at KVM_ARM_VCPU_INIT time. Because KVM_SET_ONE_REG is only permitted after KVM_ARM_VCPU_INIT, if userspace clears FEAT_MOPS in SYS_ID_AA64ISAR2_EL1 before the first KVM_RUN, vcpu->arch.hcrx_el2 retains the stale HCRX_EL2_MSCEn bit and kvm_arch_vcpu_ioctl_run() leaves MOPS instructions enabled in hardware for the guest.

[ ... ]

arch/s390/kvm/arm64/handle_exit.c static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu) · line 68

Quoted 12 lines
arch/s390/kvm/arm64/handle_exit.c +1 -0
index e0c7773c2165d..1653163ed6d65 100644
@@ -62,6 +62,7 @@ static int kvm_handle_unknown_ec(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,
 };

[Severity: Medium]
Does calling kvm_handle_sys_reg() from handle_exit() invoke _vcpu_read_sys_reg() and _vcpu_write_sys_reg() in preemptible context while the vCPU is loaded?

In kvm_arch_vcpu_ioctl_run(), handle_exit() runs after local_irq_enable() with preemption enabled and vcpu_is_loaded(vcpu) true. When a guest traps on a save-area system register such as OSLSR_EL1 or SCXTNUM_EL0/EL1, kvm_handle_sys_reg() -> perform_access() calls vcpu_read_sys_reg() or vcpu_write_sys_reg():

arch/s390/include/asm/kvm_host_arm64.h:_vcpu_read_sys_reg()

	KVM_BUG_ON(vcpu_is_loaded(C) && smp_processor_id() != (C)->cpu, (C)->kvm);

Under CONFIG_DEBUG_PREEMPT=y this triggers a "using smp_processor_id() in preemptible code" splat, and if the vCPU thread is preempted and migrated between smp_processor_id() and reading (C)->cpu, KVM_BUG_ON() fires and terminates the VM.

Quoted 4 lines
arch/s390/kvm/arm64/reset.c +0 -0
index e4cdce1dbf9d2..0339901f5fd08 100644

[ ... ]

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

Quoted 12 lines
arch/s390/kvm/arm64/reset.c +5 -0
@@ -43,9 +80,14 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 		vcpu_put(vcpu);
 
 	kvm_reset_vcpu_core(vcpu);
+	kvm_reset_sys_regs(vcpu);
 
 	/* Reset special registers */
 	vcpu_reset_hcr(vcpu);
+	vcpu_reset_hcrx(vcpu);
+	vcpu_reset_cptr(vcpu);
+	vcpu_reset_cnthctl(vcpu);
+	vcpu_reset_icc(vcpu);

[Severity: Medium]
Does kvm_reset_vcpu() leave vcpu->arch.ctxt.elr_el1, vcpu->arch.ctxt.spsr_el1, and vcpu->arch.sae_block.sp_el1 unreset when a vCPU is reset after running?

Neither kvm_reset_vcpu_core() nor kvm_reset_sys_regs() resets those fields because sys_reg_descs[] has no entries for ELR_EL1, SPSR_EL1, or SP_EL1. On the next guest entry after a vCPU reset, kvm_arch_vcpu_ioctl_run() writes the stale ctxt.elr_el1 and ctxt.spsr_el1 values back into SYS_ELR_EL1 and SYS_SPSR_EL1 in the hardware save area.

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

PATCH v3 18/23 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 62fd71af8968..4de0f17052cb 100644
@@ -371,7 +371,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 0339901f5fd0..dc00bf398b15 100644
@@ -3,6 +3,8 @@
 #include <linux/kvm_host.h>
 #include <linux/fpu.h>
 
+#include <asm/aef.h>
+
 #include <arm64/kvm_emulate.h>
 #include <arm64/kvm_nested.h>
 #include <arm64/sys_regs.h>
@@ -79,6 +81,9 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 	if (loaded)
 		vcpu_put(vcpu);
 
+	memcpy(&vcpu->arch.save_area, aef_gisrsa(), sizeof(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.56.0
#39 lore raw

Re: [PATCH v3 18/23] KVM: s390: arm64: Use QAAF init save area

Sashiko has reviewed this patch and found no issues.
Quoted 6 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.

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

PATCH v3 19/23 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        | 109 +++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/inject_fault.c     |  65 +++++++++++++++++++-
 5 files changed, 179 insertions(+), 13 deletions(-)
arch/s390/include/asm/kvm_host_arm64.h +2 -0
index 44deae423ace..79549434a1d6 100644
@@ -425,4 +425,6 @@ static inline bool vgic_host_has_gicv5(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 24ac6f6f6278..b0f2da6ac482 100644
@@ -11,6 +11,7 @@ ccflags-y += -I $(src) -I $(objtree)/$(obj) -I$(srctree)/arch/$(SRCARCH)/kvm/gma
 
 kvm-arm64-local := \
 	arm.o \
+	exception.o \
 	feature.o \
 	guest.o \
 	handle_exit.o \
arch/s390/kvm/arm64/arm.c +4 -11
index 4de0f17052cb..91e9f4a75ce7 100644
@@ -390,14 +390,6 @@ 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 noinstr void arm_vcpu_enter_exit(phys_addr_t sae_block)
 {
 	guest_timing_enter_irqoff();
@@ -488,7 +480,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
 		trace_kvm_entry(*vcpu_pc(vcpu));
 
-		adjust_pc(vcpu);
+		kvm_adjust_pc(vcpu);
 		vcpu_write_host_sys_reg(vcpu, vcpu->arch.hcr_el2, SYS_HCR_EL2);
 		vcpu_write_host_sys_reg(vcpu, vcpu->arch.hcrx_el2, SYS_HCRX_EL2);
 		vcpu_write_host_sys_reg(vcpu, vcpu->arch.mpidr, SYS_VMPIDR_EL2);
@@ -511,8 +503,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 +109 -0

109 lines not on this page · raw message

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

80 lines not on this page · raw message

Signature · 1 line
2.56.0
#41 lore raw

Re: [PATCH v3 19/23] KVM: s390: arm64: Implement exception injection

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] KVM: s390: arm64: preemptible smp_processor_id() and migration race in sysreg access - [Medium] KVM…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] KVM: s390: arm64: preemptible smp_processor_id() and migration race in sysreg access
- [Medium] KVM: s390: arm64: unsigned gltl overflow in inject_abt64() --

commit 9004b39a5f6b970883241ce75cb94bfa9bdd2fe1
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Implement exception injection

This commit adds guest exception entry and fault injection support for arm64 guests running under KVM on s390.

Quoted 4 lines
arch/s390/kvm/arm64/arm.c +0 -0
index 4de0f17052cb8..91e9f4a75ce73 100644

[ ... ]

arch/s390/kvm/arm64/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) · line 508

Quoted 9 lines
arch/s390/kvm/arm64/arm.c +3 -2
@@ -511,8 +503,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);

[Severity: High]
Does accessing save-area registers with preemption enabled here and during handle_exit() trigger a debug_smp_processor_id() splat and race with host CPU migration?

At the out label (before vcpu_put()) and inside handle_exit(), the vCPU is loaded (vcpu_is_loaded(vcpu) is true) while IRQs and preemption are enabled. When an exception is injected (for example via kvm_inject_undefined() -> kvm_inject_sync() or inject_abt64()) or delivered here via kvm_adjust_pc() -> kvm_inject_exception() -> enter_exception64(), vcpu_read_sys_reg() and vcpu_write_sys_reg() access VBAR_EL1, SCTLR_EL1, FAR_EL1, and ESR_EL1 through _vcpu_read_sys_reg() and _vcpu_write_sys_reg():

arch/s390/include/asm/kvm_host_arm64.h:_vcpu_read_sys_reg()

	KVM_BUG_ON(vcpu_is_loaded(C) && smp_processor_id() != (C)->cpu, (C)->kvm);

Calling smp_processor_id() in preemptible context triggers a BUG splat when CONFIG_DEBUG_PREEMPT is enabled, and if the vCPU task is preempted and migrated between reading smp_processor_id() and reading vcpu->cpu, KVM_BUG_ON() fires and terminates the VM:

CPU0 CPU1
handle_exit() / kvm_adjust_pc()
vcpu_read_sys_reg(vcpu, VBAR_EL1)

    smp_processor_id() -> 0
  <preempted and migrated>
                                  kvm_sched_in()
                                    kvm_arch_vcpu_load(vcpu, 1)
                                      vcpu->cpu = 1
                                  vcpu->cpu -> 1 != 0
                                  KVM_BUG_ON()  <-- kills VM
Quoted 13 lines
arch/s390/kvm/arm64/inject_fault.c +5 -0
index 425dbeaa421c6..33f40c5188c98 100644
@@ -2,6 +2,38 @@
 
 #include <arm64/kvm_emulate.h>
 
+static void pend_sync_exception(struct kvm_vcpu *vcpu)
+{
+	kvm_pend_exception(vcpu, EXCEPT_AA64_EL1_SYNC);
+}
+

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

Quoted 10 lines
arch/s390/kvm/arm64/inject_fault.c +10 -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;
+
+	if (kvm_vcpu_abt_iss1tw(vcpu))
+		esr |= ESR_ELx_FSC_SEA_TTW(kvm_vcpu_abt_gltl(vcpu));
+	else
+		esr |= ESR_ELx_FSC_EXTABT;

[Severity: Medium]
Can ESR_ELx_FSC_SEA_TTW(kvm_vcpu_abt_gltl(vcpu)) overflow the 6-bit FSC field into bit 8 of ESR_EL1 on a level -1 translation table walk?

vcpu->arch.sae_block.hai.gltl is declared as u8, so kvm_vcpu_abt_gltl() zero-extends a two's-complement -1 (0xff) to 255 instead of -1. ESR_ELx_FSC_SEA_TTW(255) then evaluates to 0x113 (0x14 + 255), which sets bit 8 (ESR_ELx_CM for data aborts or a RES0 bit for instruction aborts) in esr, and kvm_inject_size_fault() only masks off GENMASK_ULL(5, 0) afterwards.

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

PATCH v3 20/23 KVM: s390: arm64: Finalize page fault handling

In reply to Steffen Eiden

Replace the temporary -ENOEXEC returns of the arm64-on-s390 page fault handling with exception injection into the guest, mirroring arm64's kvm_handle_guest_abort().

Translation faults beyond the physical address range are injected as an address size fault, while faults falling between the VM's IPA size and that range are injected as a synchronous external abort. Instruction aborts and stage-1 walk faults on host addresses that back no guest page are injected as synchronous external aborts as well, and a PGM-addressing from the fault-in path is injected as an address size fault, the closest arm64 equivalent. Injected faults return 1 so the vCPU re-enters the guest with the pending exception instead of exiting to userspace.

---
 arch/s390/include/asm/kvm_host_arm64.h |  1 +
 arch/s390/kvm/arm64/arm.c              |  6 +++
 arch/s390/kvm/arm64/mmu.c              | 74 +++++++++++++++++++++++-----------
 arch/s390/kvm/arm64/sys_regs.c         |  4 ++
 4 files changed, 62 insertions(+), 23 deletions(-)
arch/s390/include/asm/kvm_host_arm64.h +1 -0
index 79549434a1d6..20f51cf9ce37 100644
@@ -426,5 +426,6 @@ static inline bool vgic_host_has_gicv5(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 91e9f4a75ce7..b5a0f8c1e9a4 100644
@@ -63,9 +63,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 +51 -23

115 lines not on this page · raw message

arch/s390/kvm/arm64/sys_regs.c +4 -0
index 417a05ce9015..951a02d89fc5 100644
@@ -317,6 +317,10 @@ static int arm64_check_features(struct kvm_vcpu *vcpu,
 		if (SYS_FIELD_GET(ID_AA64MMFR0_EL1, PARANGE, val) >
 		    SYS_FIELD_GET(ID_AA64MMFR0_EL1, PARANGE, host_val))
 			return -E2BIG;
+		if (kvm_parange_to_address_sanitized(
+			    SYS_FIELD_GET(ID_AA64MMFR0_EL1, PARANGE, val)) <
+		    vcpu->kvm->arch.guest_phys_size)
+			return -E2BIG;
 		break;
 	case SYS_CTR_EL0:
 		/* forbid upgrading from VIPT to PIPT */
Signature · 1 line
2.56.0
#43 lore raw

Re: [PATCH v3 20/23] KVM: s390: arm64: Finalize page fault handling

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: s390: arm64: preemptible smp_processor_id() in fault injection --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: s390: arm64: preemptible smp_processor_id() in fault injection
--

commit 2432a7ede0d5feb054800e41b623b3e845ffe9c3
Author: Steffen Eiden <[email protected]>

KVM: s390: arm64: Finalize page fault handling

This commit replaces temporary -ENOEXEC returns in the arm64-on-s390 page fault handler with address size fault and synchronous external abort injection into the guest, and bounds the guest address limit and ID_AA64MMFR0_EL1.PARANGE against the supported physical address range.

Quoted 4 lines
arch/s390/kvm/arm64/mmu.c +0 -0
index 31e0d036896f1..7a6f6ba326c13 100644

[ ... ]

Quoted 19 lines
arch/s390/kvm/arm64/mmu.c +6 -4
@@ -32,12 +33,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;
-	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);
+		return 1;
+	}
 	KVM_BUG_ON(ret, vcpu->kvm);
 	return -EINVAL;
 }

[ ... ]

Quoted 15 lines
arch/s390/kvm/arm64/mmu.c +4 -7
@@ -98,16 +130,17 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 	}
 
 	if (translation) {
-		/*
-		 * For both cases:
-		 * Without the relevant sysregs we cannot do anything for now.
-		 * Go back to userspace with an error. TODO sysreg handling
-		 */
-		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit()))
-			return -ENOEXEC;
+		/* Beyond the PA range programmed for the guest */
+		if (fault_ipa >= kvm_get_pa_address(vcpu->kvm)) {
+			kvm_inject_size_fault(vcpu);
+			return 1;

arch/s390/kvm/arm64/mmu.c int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) · line 144

Quoted 10 lines
arch/s390/kvm/arm64/mmu.c +6 -2
+		}
 
-		if (fault_ipa >= vcpu->kvm->arch.guest_phys_size)
-			return -ENOEXEC;
+		/* Falls between the IPA range and the PA range? */
+		if (fault_ipa >= vcpu->kvm->arch.guest_phys_size) {
+			fault_ipa |= FAR_TO_FIPA_OFFSET(kvm_vcpu_get_hfar(vcpu));
+			return kvm_inject_sea(vcpu, is_iabt, fault_ipa);
+		}
 	}

[Severity: Medium]
Can calling kvm_inject_size_fault() and kvm_inject_sea() (as well as kvm_inject_sea_iabt() and kvm_inject_sea_dabt() below) from kvm_handle_guest_abort() invoke smp_processor_id() in preemptible context?

kvm_arch_vcpu_ioctl_run() calls handle_exit() -> kvm_handle_guest_abort() with both interrupts and preemption enabled while the vCPU is loaded (vcpu->cpu != -1). The fault injection helpers access FAR_EL1 and ESR_EL1 through vcpu_read_sys_reg() and vcpu_write_sys_reg(), which expand to _vcpu_read_sys_reg() and _vcpu_write_sys_reg():

arch/s390/include/asm/kvm_host_arm64.h:_vcpu_write_sys_reg() {
	({	BUILD_BUG_ON(!__builtin_constant_p((R))); \
		KVM_BUG_ON(vcpu_is_loaded(C) && smp_processor_id() != (C)->cpu, (C)->kvm); \
		(vcpu_is_loaded(C)) \
			? __vcpu_write_sr((C), (V), (R), 0) \
			: __vcpu_write_sr((C), (V), (R), SASR_FLAG_INITIALIZED); })
}

When a guest faults on an address above its IPA or PA range, this triggers a debug_smp_processor_id() warning under CONFIG_DEBUG_PREEMPT=y, and if the vCPU thread is preempted and migrated to another CPU between reading vcpu->cpu and smp_processor_id() (where kvm_sched_out() / kvm_sched_in() update vcpu->cpu), KVM_BUG_ON() fires and marks the VM bugged.

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

PATCH v3 21/23 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.

---
 arch/s390/include/asm/kvm_host_arm64.h |   5 +-
 arch/s390/kvm/arm64/arm.c              |   6 +
 arch/s390/kvm/arm64/feature.c          |   9 +-
 arch/s390/kvm/arm64/feature.h          |   6 +
 arch/s390/kvm/arm64/guest.c            | 250 +++++++++++++++++++++++++++++++++
 arch/s390/kvm/arm64/reset.c            |  24 ++++
 6 files changed, 297 insertions(+), 3 deletions(-)
arch/s390/include/asm/kvm_host_arm64.h +3 -2
index 20f51cf9ce37..f096faceb19d 100644
@@ -34,7 +34,9 @@ bool cpus_have_final_cap(unsigned int num);
 #define KVM_MAX_VCPUS 1
 #define __KVM_HAVE_VCPU_RUN_PID_CHANGE
 
-#define KVM_S390_ARM64_IMPL_FEATURES 0
+#define KVM_S390_ARM64_IMPL_FEATURES (		\
+	 BIT(KVM_ARM_VCPU_SVE)			\
+)
 #define KVM_HALT_POLL_NS_DEFAULT 50000
 
 #define KVM_S390_MANAGES_S390_GUEST 0
@@ -130,7 +132,6 @@ struct kvm_vcpu_stat {
 };
 
 #define kvm_has_mte(_kvm) false
-#define vcpu_has_sve(_vcpu) false
 #define vcpu_has_ptrauth(_vcpu) false
 
 #define kvm_has_fpmr(kvm) \
arch/s390/kvm/arm64/arm.c +6 -0
index b5a0f8c1e9a4..8c2a07a96ee3 100644
@@ -54,6 +54,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_ONE_REG:
 		ret = 1;
 		break;
+	case KVM_CAP_ARM_SVE:
+		ret = system_supports_sve();
+		break;
 	default:
 		ret = 0;
 	}
@@ -264,6 +267,9 @@ static unsigned long system_supported_vcpu_features(struct kvm_vcpu *vcpu)
 {
 	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 f8b23ba85f9e..0979e0cadc66 100644
@@ -56,7 +56,6 @@ void __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);
@@ -161,3 +160,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 +6 -0
index 0a59047fda6e..9af6173f6b11 100644
@@ -83,4 +83,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 +250 -0

286 lines not on this page · raw message

arch/s390/kvm/arm64/reset.c +24 -0
index dc00bf398b15..1ce05a95689f 100644
@@ -2,6 +2,7 @@
 
 #include <linux/kvm_host.h>
 #include <linux/fpu.h>
+#include <linux/cpufeature.h>
 
 #include <asm/aef.h>
 
@@ -12,8 +13,13 @@
 
 #include <clocksource/arm_arch_timer.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,14 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 	 */
 	preempt_disable();
 
+	if (!vcpu_get_flag(vcpu, VCPU_SVE_FINALIZED)) {
+		if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_SVE))
+			set_bit(KVM_ARCH_FLAG_GUEST_HAS_SVE, &vcpu->kvm->arch.flags);
+	} else if (vcpu_has_sve(vcpu)) {
+		memset(vcpu->arch.sae_block.sve_pregs, 0, sizeof(vcpu->arch.sae_block.sve_pregs));
+		vcpu->arch.sae_block.sve_ffr = 0;
+	}
+
 	/* The reset must run with an unloaded save area */
 	loaded = vcpu_is_loaded(vcpu);
 	if (loaded)
@@ -114,5 +128,15 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
 
 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
 {
+	switch (feature) {
+	case KVM_ARM_VCPU_SVE:
+		if (!vcpu_has_sve(vcpu))
+			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.56.0
#45 lore raw

Re: [PATCH v3 21/23] KVM: s390: arm64: Implement SVE for arm guests

Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

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.

Quoted 5 lines

Sashiko has reviewed this patch and found no issues.

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

PATCH v3 22/23 KVM: s390: arm64: Promote PTRAUTH capability

In reply to Steffen Eiden

Promote the pointer authentication capability if supported by hardware and allow userspace to access those.

---
 arch/s390/include/asm/kvm_host_arm64.h | 12 ++++--------
 arch/s390/kvm/arm64/arm.c              | 17 +++++++++++++++++
 arch/s390/kvm/arm64/feature.c          | 18 ++++++++++++++++++
 arch/s390/kvm/arm64/feature.h          |  5 +++++
 arch/s390/kvm/arm64/sys_regs.c         | 10 ++++++++--
 5 files changed, 52 insertions(+), 10 deletions(-)
arch/s390/include/asm/kvm_host_arm64.h +4 -8
index f096faceb19d..5a443bba0018 100644
@@ -34,8 +34,10 @@ bool cpus_have_final_cap(unsigned int num);
 #define KVM_MAX_VCPUS 1
 #define __KVM_HAVE_VCPU_RUN_PID_CHANGE
 
-#define KVM_S390_ARM64_IMPL_FEATURES (		\
-	 BIT(KVM_ARM_VCPU_SVE)			\
+#define KVM_S390_ARM64_IMPL_FEATURES (			\
+	 BIT(KVM_ARM_VCPU_PTRAUTH_ADDRESS)	|	\
+	 BIT(KVM_ARM_VCPU_PTRAUTH_GENERIC)	|	\
+	 BIT(KVM_ARM_VCPU_SVE)				\
 )
 #define KVM_HALT_POLL_NS_DEFAULT 50000
 
@@ -132,7 +134,6 @@ struct kvm_vcpu_stat {
 };
 
 #define kvm_has_mte(_kvm) false
-#define vcpu_has_ptrauth(_vcpu) false
 
 #define kvm_has_fpmr(kvm) \
 	 kvm_has_feat((kvm), ID_AA64PFR2_EL1, FPMR, IMP)
@@ -172,11 +173,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 +17 -0
index 8c2a07a96ee3..ee05d7d9f331 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;
 	}
@@ -227,6 +231,12 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
 }
 
+static void vcpu_set_pauth_traps(struct kvm_vcpu *vcpu)
+{
+	if (vcpu_has_ptrauth(vcpu))
+		vcpu->arch.hcr_el2 |= (HCR_EL2_API | HCR_EL2_APK);
+}
+
 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
 	save_access_regs(&vcpu->arch.host_acrs[0]);
@@ -235,6 +245,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 	lasrm(&vcpu->arch.save_area);
 
 	vcpu->arch.sae_block.gpto = vcpu->kvm->arch.epd;
+
+	vcpu_set_pauth_traps(vcpu);
 }
 
 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
@@ -270,6 +282,11 @@ static unsigned long system_supported_vcpu_features(struct kvm_vcpu *vcpu)
 	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 0979e0cadc66..13a56647d78c 100644
@@ -168,3 +168,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 9af6173f6b11..3ca1ce0139b0 100644
@@ -89,4 +89,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 */
arch/s390/kvm/arm64/sys_regs.c +8 -2
index 951a02d89fc5..8ee17e7fd388 100644
@@ -32,7 +32,6 @@ static u64 __ro_after_init boot_cpu_aidr_val;
 #undef __INCL_GEN_ARM_FILE
 
 DEFINE_STATIC_KEY_FALSE(broken_cntvoff_key);
-
 #define SR_INVALID 0x8badf00d8badf00d
 
 enum sr_loc_attr {
@@ -634,6 +633,14 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_TTBR1_EL1), access_rw, reset_val, TTBR1_EL1, 0 },
 	{ SYS_DESC(SYS_TCR_EL1), access_rw, reset_val, TCR_EL1, 0 },
 
+	/* CRm = 1 */
+	PTRAUTH_KEY(APIA),
+	PTRAUTH_KEY(APIB),
+	/* CRm = 2 */
+	PTRAUTH_KEY(APDA),
+	PTRAUTH_KEY(APDB),
+	/* CRm = 3 */
+	PTRAUTH_KEY(APGA),
 	/* CRn = 5 */
 	{ SYS_DESC(SYS_AFSR0_EL1), access_rw, reset_unknown, AFSR0_EL1 },
 	{ SYS_DESC(SYS_AFSR1_EL1), access_rw, reset_unknown, AFSR1_EL1 },
@@ -642,7 +649,6 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_FAR_EL1), access_rw, reset_unknown, FAR_EL1 },
 	/* CRn = 7 */
 	{ SYS_DESC(SYS_PAR_EL1), access_rw, reset_unknown, PAR_EL1 },
-
 	{ SYS_DESC(SYS_PMMIR_EL1), trap_raz_wi },
 	/* CRn = 10 */
 	{ SYS_DESC(SYS_MAIR_EL1), NULL, reset_unknown, MAIR_EL1 },
Signature · 1 line
2.56.0
#47 lore raw

Re: [PATCH v3 22/23] KVM: s390: arm64: Promote PTRAUTH capability

Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

Promote the pointer authentication capability if supported by hardware and allow userspace to access those.

Sashiko has reviewed this patch and found no issues.

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

PATCH v3 23/23 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.

---
 Documentation/ABI/testing/sysfs-firmware-aef |  70 +++++++++++++++++
 arch/s390/kernel/aef.c                       | 113 +++++++++++++++++++++++++++
 2 files changed, 183 insertions(+)
Documentation/ABI/testing/sysfs-firmware-aef new file +70 -0
new file mode 100644
index 000000000000..6ccd8491a017
@@ -0,0 +1,70 @@
+What:		/sys/firmware/aef/
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Various interfaces to query information about the ARM
+		Execution Facility (AEF).
+
+		The directory is present only on machines that provide
+		the AEF (SCLP facility bit).
+
+What:		/sys/firmware/aef/arm_guest_supp
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, decimal boolean. Reports 1 if all requirements
+		for running arm64 KVM guests (KVM module kvm_arm64) are
+		satisfied by the machine, 0 otherwise. This is equivalent to the
+		logical AND of sae_avail, ptff_avail and edat2_avail below.
+
+What:		/sys/firmware/aef/query/
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Raw machine characteristics as reported by the QAAF
+		instruction.
+
+What:		/sys/firmware/aef/query/sae_avail
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, decimal boolean. Reports 1 if the machine
+		provides the SAE instruction.
+
+What:		/sys/firmware/aef/query/ptff_avail
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, decimal boolean. Reports 1 if the PTFF
+		instruction supports the QAGTO and QAGPT subfunctions.
+
+What:		/sys/firmware/aef/query/edat2_avail
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, decimal boolean. Reports 1 if the EDAT-2
+		facility is available.
+
+What:		/sys/firmware/aef/query/supp_state_desc_formats
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, 0x-prefixed hexadecimal bitmask. Bitmask of
+		the SAE state description formats supported by the machine.
+
+What:		/sys/firmware/aef/query/supp_save_area_formats
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, 0x-prefixed hexadecimal bitmask. Bitmask of
+		the SAE save area formats supported by the machine.
+
+What:		/sys/firmware/aef/query/max_num_vcpu
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, decimal number. Maximum number of guest CPUs
+		per VM.
+
+What:		/sys/firmware/aef/query/qmc
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only binary attribute, 4096 bytes. Contains the
+		unmodified, machine specific QMC response block.
+
+What:		/sys/firmware/aef/query/save_area
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only binary attribute, 4096 bytes. Contains the
+		unmodified, machine specific GISRSA block.
arch/s390/kernel/aef.c +113 -0

140 lines not on this page · raw message

Signature · 1 line
2.56.0
#49 lore raw

Re: [PATCH v3 23/23] s390: Report AEF features to sysfs

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] s390: missing static const on bin_attr_qmc and bin_attr_save_area --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] s390: missing static const on bin_attr_qmc and bin_attr_save_area
--

commit 2934d9947793e4c4621a99293c7dadc5c2b8442b
Author: Steffen Eiden <[email protected]>

s390: Report AEF features to sysfs

This commit exposes Arm Execution Facility (AEF) capabilities, supported formats, vCPU limits, and raw QAAF query blocks to userspace under /sys/firmware/aef/ and documents the new sysfs ABI.

Quoted 4 lines
arch/s390/kernel/aef.c +0 -0
index 31e18953d7b31..96a6d63b26950 100644

[ ... ]

Quoted 1 line
arch/s390/kernel/aef.c +0 -0
@@ -38,6 +47,68 @@ bool aef_usable(void)

[ ... ]

Quoted 8 lines
arch/s390/kernel/aef.c +8 -0
+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));

arch/s390/kernel/aef.c bool aef_usable(void) · line 89

Quoted 10 lines
arch/s390/kernel/aef.c +10 -0
+
+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));

[Severity: Low]
Should BIN_ATTR_RO(qmc, ...) and BIN_ATTR_RO(save_area, ...) be marked static const? BIN_ATTR_RO() expands without storage qualifiers, so omitting static const exports bin_attr_qmc and bin_attr_save_area as mutable global symbols in .data and triggers sparse -Wdecl warnings.

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

Re: [PATCH v3 23/23] s390: Report AEF features to sysfs

In reply to Steffen Eiden
Quoted 23 lines · 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.

---
  Documentation/ABI/testing/sysfs-firmware-aef |  70 +++++++++++++++++
  arch/s390/kernel/aef.c                       | 113 +++++++++++++++++++++++++++
  2 files changed, 183 insertions(+)
Documentation/ABI/testing/sysfs-firmware-aef new file +0 -0
new file mode 100644
index 000000000000..6ccd8491a017

[...]

Quoted 8 lines
Documentation/ABI/testing/sysfs-firmware-aef new file +8 -0
+
+What:		/sys/firmware/aef/query/sae_avail
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, decimal boolean. Reports 1 if the machine
+		provides the SAE instruction.
+
+What:		/sys/firmware/aef/query/ptff_avail

Documentation/ABI/testing/sysfs-firmware-aef line 40

Quoted 10 lines
Documentation/ABI/testing/sysfs-firmware-aef new file +10 -0
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, decimal boolean. Reports 1 if the PTFF
+		instruction supports the QAGTO and QAGPT subfunctions.
+
+What:		/sys/firmware/aef/query/edat2_avail
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, decimal boolean. Reports 1 if the EDAT-2
+		facility is available.

I don't think we need any of the three files above.
sae should be reported as a feature in /proc/cpuinfo, same as sief2.

edat2 is facility 78 in the same file.

PTFF-QAF is unprivileged.

Also, if aef is the requirement for the directory to exist, why would saef not be enabled?

author reviewer
#51 lore raw

Re: [PATCH v3 23/23] s390: Report AEF features to sysfs

Quoted 49 lines · Janosch Frank
Quoted 23 lines · 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.

---
  Documentation/ABI/testing/sysfs-firmware-aef |  70 +++++++++++++++++
  arch/s390/kernel/aef.c                       | 113 +++++++++++++++++++++++++++
  2 files changed, 183 insertions(+)
Documentation/ABI/testing/sysfs-firmware-aef new file +0 -0
new file mode 100644
index 000000000000..6ccd8491a017

[...]

Quoted 8 lines
Documentation/ABI/testing/sysfs-firmware-aef new file +8 -0
+
+What:		/sys/firmware/aef/query/sae_avail
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, decimal boolean. Reports 1 if the machine
+		provides the SAE instruction.
+
+What:		/sys/firmware/aef/query/ptff_avail

Documentation/ABI/testing/sysfs-firmware-aef line 40

Quoted 10 lines
Documentation/ABI/testing/sysfs-firmware-aef new file +10 -0
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, decimal boolean. Reports 1 if the PTFF
+		instruction supports the QAGTO and QAGPT subfunctions.
+
+What:		/sys/firmware/aef/query/edat2_avail
+KernelVersion:	7.5
+Contact:	[email protected]
+Description:	Read-only, decimal boolean. Reports 1 if the EDAT-2
+		facility is available.

I don't think we need any of the three files above.
sae should be reported as a feature in /proc/cpuinfo, same as sief2.

Quoted 3 lines

edat2 is facility 78 in the same file.

PTFF-QAF is unprivileged.

OK, I can remove those. Primarily, I added them for convenience. I'll add those to the Documentation (I'll create one soonish)

Quoted 4 lines

Also, if aef is the requirement for the directory to exist, why would saef not be enabled?

You are right. I'll drop the three files.

I am thinking about always providing the aef directory. And just not calling qaaf in the !aef case. Then userspace does not have to test if the file exists and guess if the file does not exists because the kernel does not support it or the HW. Or is this a non-issue?

Steffen