Skip to contentSkip to conversation

KVM: s390: vsie: fix instruction retry for nested guests in ESA mode

PATCH Superseded Yvan Janssens 3 participants 6 messages Nov 25

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover KVM: s390: vsie: fix instruction retry for nested guests in ESA mode

Since v7.1 a nested hypervisor can run its guests in ESA mode (the ESA/390-compatibility mode of the machine) when user space enables KVM_CAP_S390_VSIE_ESAMODE. One piece of vsie still treats the PSW of such a guest as a z/Architecture PSW: the rewind that vsie uses to have an instruction repeated. For a state description in ESA/390 format the instruction is skipped instead, and a stray value is left in the half of the PSW field that such a state description does not use.

What a nested guest sees: the first STORE FACILITY LIST EXTENDED after every entry into SIE does nothing (operand and condition code untouched), because vsie intercepts it to shadow the facility list. A program that tests facility bits gets zeros.

Patch 1 is the fix, patch 2 a selftest that reproduces the problem with a small guest 2 that issues SIE itself (modelled on the vsie selftest that is queued in kvm-s390 next). The series is based on v7.4-rc4-260-g4dce97c6c265.

Why it matters beyond one instruction: backward compatibility is a defining property of this platform, and the ESA/390-compatibility mode is the architecture's mechanism for running older programs on machines that only start in z/Architecture mode. Any hypervisor that runs as a KVM guest and gives its own guests that mode depends on vsie handling such state descriptions exactly like the machine.

Testing (z14)
- On an LPAR running v7.3.6 with patch 1 (the host owns SIE) the selftest of patch 2 passes all three checks: the unused doubleword stays 0, the PSW ends behind the last instruction, and the first STFLE stores the designated list.
- Without patch 1 (v7.3.6, test kernel running as a KVM guest above a level that passed ESA mode state descriptions on): the unused PSW doubleword comes back as 0x7ffffff8 and neither STFLE stores anything. In such a nested setup above a host without the fix, checks 1 and 3 still fail with patch 1 in the test kernel, because the defect acts once per level.
- On a host whose lower level refuses ESA mode state descriptions the test skips (validity 0x0001 seen by guest 2).
- KVM selftests (12 s390 tests and 5 generic ones) and the s390x kvm-unit-tests (37 pass, 4 fail, 21 skip) give the same result for every test with and without the series (v7.3.6, nested setup); the 4 failures (sclp-1g, sclp-3g, css, edat) are the same with the unmodified module.
- Built with W=1 and sparse without warnings; checkpatch --strict is clean apart from the MAINTAINERS note for the new file.
- Not tested: MOVE PAGE partial execution and the addressing-exception path for a guest in ESA mode. MOVE PAGE is not available in the mode without the ESA/XC facilities, so the first should not occur.

Assistance: the series was prepared with an AI coding assistant (see the Assisted-by tags); the defect was found while tracing why a facility test failed in a nested guest, the analysis, fix and test were written with the assistant and reviewed by the submitter.

Yvan Janssens (2):
KVM: s390: vsie: Rewind the PSW of ESA mode guests in the right format
KVM: s390: selftests: Add a test for nested guests in ESA mode

 arch/s390/kvm/s390/vsie.c                     |  23 ++-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/s390/vsie_esa_test.c        | 180 ++++++++++++++++++
 3 files changed, 202 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/s390/vsie_esa_test.c

base-commit: 4dce97c6c265544342e716e62fd24bbb232590c5

Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 KVM: s390: vsie: Rewind the PSW of ESA mode guests in the right format

A state description without the z/Architecture flag holds the eight-byte ESA/390 PSW in the first doubleword of its PSW field: the addressing mode is bit 32 and the instruction address is in bits 33-63 of that doubleword (Principles of Operation, "Program-Status Word" as it applies in the ESA/390-compatibility mode). The second doubleword is not part of the PSW.

Since nested guests can be run in ESA mode, vsie still rewinds the PSW of their shadow state description as if it were a z/Architecture PSW. It subtracts the instruction length from the second doubleword, which leaves the instruction address alone and stores a wrapped address there (0x7ffffffc for one four-byte instruction). The value is copied back into the state description of guest 2 when the shadow is released.

The rewind is how vsie repeats an instruction: STORE FACILITY LIST EXTENDED after the facility list that guest 2 designates has been shadowed (handle_stfle()), MOVE PAGE after a fault was resolved (vsie_handle_mvpg()), and the instruction that is reported with an addressing exception when guest 3 touches memory guest 2 does not have. For a guest in ESA mode the instruction is skipped instead. The first STORE FACILITY LIST EXTENDED after every entry into SIE returns with its operand and the condition code untouched, so a program in the nested guest that looks at the facility list sees zeros.

Rewind the instruction address where the format of the state description has it.

The selftest in the next patch reproduces the problem: on 7.3.6 the second doubleword of the PSW field comes back as 0x7ffffff8 after two skipped instructions and the nested guest never stores a facility list.

Fixes b0ad874d9852 ("KVM: s390: vsie: Allow non-zarch guests")
Assisted-by Claude:claude-fable-5-2
---
 arch/s390/kvm/s390/vsie.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
arch/s390/kvm/s390/vsie.c +21 -2
index 3ce133f20fa2..58afe913bff4 100644
@@ -948,6 +948,25 @@ static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
 	vsie_page->scb_s.icptcode = 0;
 }
 
+/*
+ * Rewind the PSW of a shadow scb. An ESA mode guest has the eight-byte
+ * ESA/390 PSW in the first doubleword of the PSW field: the addressing mode
+ * in bit 32 and the instruction address in bits 33-63. The second doubleword
+ * is not part of its PSW.
+ */
+static void vsie_rewind_psw(struct kvm_s390_sie_block *scb, int ilen)
+{
+	u64 addr_mask;
+
+	if (atomic_read(&scb->cpuflags) & CPUSTAT_ZARCH) {
+		scb->gpsw.addr = __rewind_psw(scb->gpsw, ilen);
+		return;
+	}
+	addr_mask = scb->gpsw.mask & PSW32_ADDR_AMODE ? PSW32_ADDR_INSN : 0x00ffffffUL;
+	scb->gpsw.mask = (scb->gpsw.mask & ~PSW32_ADDR_INSN) |
+			 ((scb->gpsw.mask - ilen) & addr_mask);
+}
+
 /* rewind the psw and clear the vsie icpt, so we can retry execution */
 static void retry_vsie_icpt(struct vsie_page *vsie_page)
 {
@@ -960,7 +979,7 @@ static void retry_vsie_icpt(struct vsie_page *vsie_page)
 		if (!ilen)
 			ilen = 4;
 	}
-	scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
+	vsie_rewind_psw(scb_s, ilen);
 	clear_vsie_icpt(vsie_page);
 }
 
@@ -1413,7 +1432,7 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 		scb_s->icptcode = ICPT_PROGI;
 		scb_s->iprcc = PGM_ADDRESSING;
 		scb_s->pgmilc = 4;
-		scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
+		vsie_rewind_psw(scb_s, 4);
 		rc = 1;
 	}
 	return rc;
Signature · 1 line
2.47.3
maintainer
#2 lore raw

Re: [PATCH 1/2] KVM: s390: vsie: Rewind the PSW of ESA mode guests in the right format

Quoted 24 lines · Yvan Janssens

A state description without the z/Architecture flag holds the eight-byte ESA/390 PSW in the first doubleword of its PSW field: the addressing mode is bit 32 and the instruction address is in bits 33-63 of that doubleword (Principles of Operation, "Program-Status Word" as it applies in the ESA/390-compatibility mode). The second doubleword is not part of the PSW.

Since nested guests can be run in ESA mode, vsie still rewinds the PSW of their shadow state description as if it were a z/Architecture PSW. It subtracts the instruction length from the second doubleword, which leaves the instruction address alone and stores a wrapped address there (0x7ffffffc for one four-byte instruction). The value is copied back into the state description of guest 2 when the shadow is released.

The rewind is how vsie repeats an instruction: STORE FACILITY LIST EXTENDED after the facility list that guest 2 designates has been shadowed (handle_stfle()), MOVE PAGE after a fault was resolved (vsie_handle_mvpg()), and the instruction that is reported with an addressing exception when guest 3 touches memory guest 2 does not have. For a guest in ESA mode the instruction is skipped instead. The first STORE FACILITY LIST EXTENDED after every entry into SIE returns with its operand and the condition code untouched, so a program in the nested guest that looks at the facility list sees zeros.

Quoted 5 lines

Rewind the instruction address where the format of the state description has it.

The selftest in the next patch reproduces the problem: on 7.3.6 the second doubleword of the PSW field comes back as 0x7ffffff8 after two

I'm wondering if we should only copy the a DWORD back to the guest for the PSW in ESA mode and not the long PSW. On the other hand I'd like to have as little ESA accommodations in this code as possible. Sigh.

Quoted 1 line

skipped instructions and the nested guest never stores a facility list.

While I do appreciate documentation of what a patch changes and why it changes it, I don't like lengthy explanations of basic concepts.
Please take the time to write this yourself if it's not 100% trivial. This helps me to decide if you actually understood the changes that you're proposing. Especially when you change obscure stuff like this :)

I'd suggest something like:

Basic ESA mode support for vsie was introduced with b0ad874d9852. Such guests use the 8 byte short PSW instead of the 16 byte zArch PSW. This means that the mask and the address have to share the same DWORD.

Unfortunately the vsie code does not consider this when rewinding the PSW address in the shadow scb and manipulates the (unused) second DWORD instead of the first. This patch adds a vsie specific rewind function to address that.

Since vsie only provides basic ESA support I'm not adding a short PSW and we make do with writing mask and address into the same PSW mask field.

Code seems fine.

Quoted 14 lines
Fixes b0ad874d9852 ("KVM: s390: vsie: Allow non-zarch guests")
Assisted-by Claude:claude-fable-5-2
---
  arch/s390/kvm/s390/vsie.c | 23 +++++++++++++++++++++--
  1 file changed, 21 insertions(+), 2 deletions(-)
arch/s390/kvm/s390/vsie.c +0 -0
index 3ce133f20fa2..58afe913bff4 100644

arch/s390/kvm/s390/vsie.c static inline void clear_vsie_icpt(struct vsie_page *vsie_page) · line 956

Quoted 9 lines
arch/s390/kvm/s390/vsie.c +6 -0
@@ -948,6 +948,25 @@ static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
  	vsie_page->scb_s.icptcode = 0;
  }
 
+/*
+ * Rewind the PSW of a shadow scb. An ESA mode guest has the eight-byte
+ * ESA/390 PSW in the first doubleword of the PSW field: the addressing mode
+ * in bit 32 and the instruction address in bits 33-63. The second doubleword
+ * is not part of its PSW.
+ */

Please remove this comment it suffers from the same problem that the commit message has.

arch/s390/kvm/s390/vsie.c static inline void clear_vsie_icpt(struct vsie_page *vsie_page) · line 964

Quoted 8 lines
arch/s390/kvm/s390/vsie.c +8 -0
+static void vsie_rewind_psw(struct kvm_s390_sie_block *scb, int ilen)
+{
+	u64 addr_mask;
+
+	if (atomic_read(&scb->cpuflags) & CPUSTAT_ZARCH) {
+		scb->gpsw.addr = __rewind_psw(scb->gpsw, ilen);
+		return;
+	}

Please add a short comment that this is for the esa short psw and that's why everything is written to the mask.

Quoted 26 lines
arch/s390/kvm/s390/vsie.c +7 -2
+	addr_mask = scb->gpsw.mask & PSW32_ADDR_AMODE ? PSW32_ADDR_INSN : 0x00ffffffUL;
+	scb->gpsw.mask = (scb->gpsw.mask & ~PSW32_ADDR_INSN) |
+			 ((scb->gpsw.mask - ilen) & addr_mask);
+}
+
  /* rewind the psw and clear the vsie icpt, so we can retry execution */
  static void retry_vsie_icpt(struct vsie_page *vsie_page)
  {
@@ -960,7 +979,7 @@ static void retry_vsie_icpt(struct vsie_page *vsie_page)
  		if (!ilen)
  			ilen = 4;
  	}
-	scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
+	vsie_rewind_psw(scb_s, ilen);
  	clear_vsie_icpt(vsie_page);
  }
 
@@ -1413,7 +1432,7 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  		scb_s->icptcode = ICPT_PROGI;
  		scb_s->iprcc = PGM_ADDRESSING;
  		scb_s->pgmilc = 4;
-		scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
+		vsie_rewind_psw(scb_s, 4);
  		rc = 1;
  	}
  	return rc;
#3 lore raw

Re: [PATCH 1/2] KVM: s390: vsie: Rewind the PSW of ESA mode guests in the right format

Quoted 3 lines · Janosch Frank

I'm wondering if we should only copy the a DWORD back to the guest for the PSW in ESA mode and not the long PSW. On the other hand I'd like to have as little ESA accommodations in this code as possible. Sigh.

With the fix, vsie no longer writes to the second doubleword, so the full copy-back just returns what guest 2 put there. I'd rather not add another ESA special case for that, so I left it as is.

Thanks for the review; I've posted a v2 that addresses the commit message and the comments - it should be on the list.

The new cover letter also contains the rationale for this work, and this might be slightly OT to discuss here. Feel free to reach out directly to discuss it off-list to reduce noise.

/y

#4 lore raw

PATCH 2/2 KVM: s390: selftests: Add a test for nested guests in ESA mode

In reply to Yvan Janssens

Run a nested guest from a state description without the z/Architecture flag, with KVM_CAP_S390_VSIE_ESAMODE enabled, and let it execute STORE FACILITY LIST EXTENDED twice. vsie intercepts the first one of every SIE entry to shadow the facility list guest 2 designates and then has the instruction repeated.

The test checks that the second doubleword of the PSW field, which is not part of an ESA/390 PSW, comes back unchanged, that the PSW points behind the last instruction, and that the first instruction stored the designated list. What the second instruction stored is printed for diagnosis.

The test is skipped without the capability, without interpretive execution for the guest (SIEF2) and without facility 7 in the guest's model.

Assisted-by Claude:claude-fable-5-2
---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/s390/vsie_esa_test.c        | 180 ++++++++++++++++++
 2 files changed, 181 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/s390/vsie_esa_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 283c59d7649c..aef64ec2719c 100644
@@ -227,6 +227,7 @@ TEST_GEN_PROGS_s390 += s390/shared_zeropage_test
 TEST_GEN_PROGS_s390 += s390/ucontrol_test
 TEST_GEN_PROGS_s390 += s390/user_operexec
 TEST_GEN_PROGS_s390 += s390/keyop
+TEST_GEN_PROGS_s390 += s390/vsie_esa_test
 TEST_GEN_PROGS_s390 += rseq_test
 TEST_GEN_PROGS_s390 += s390/irq_routing
 TEST_GEN_PROGS_s390 += mmu_stress_test
tools/testing/selftests/kvm/s390/vsie_esa_test.c new file +180 -0

180 lines not on this page · raw message

Signature · 1 line
2.47.3