Skip to contentSkip to conversation

KVM: s390: Fix lpsw/e breaking event handling

PATCH In review Janosch Frank 4 participants 11 messages Mar 23 – Apr 15

Subsystems (4)
lore
Share & tools
lore

Janosch Frank

author maintainer
#0 lore raw

cover KVM: s390: Fix lpsw/e breaking event handling

LPSW and LPSWE need to set the gbea on completion but currently don't. Time to fix this up. LPSWEY was designed to not set the bear.

This is only ever problematic when a machine check is pending and the guest hasn't opened the PSW machine check mask. In all other cases SIE handles the BEAR. So impact is very low.

---
v2:
- BEAR needs to be current psw address - ilen since code 04 is suppressing
- Added simple test
---
Janosch Frank (2):
KVM: s390: Fix lpsw/e breaking event handling
KVM: s390: selftests: Add load psw bear test

 arch/s390/kvm/priv.c                     |   8 +-
 tools/testing/selftests/kvm/Makefile.kvm |   1 +
 tools/testing/selftests/kvm/s390/bear.c  | 184 +++++++++++++++++++++++
 3 files changed, 191 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/s390/bear.c
Signature · 1 line
2.51.0
author maintainer
#1 lore raw

PATCH 1/2 KVM: s390: Fix lpsw/e breaking event handling

LPSW and LPSWE need to set the gbea on completion but currently don't. Time to fix this up.

LPSWEY was designed to not set the bear.

Fixes 48a3e950f4cee ("KVM: s390: Add support for machine checks.")
---
 arch/s390/kvm/priv.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/s390/kvm/priv.c +6 -2
index a3250ad83a8e..cc0553da14cb 100644
@@ -714,12 +714,13 @@ int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
 {
 	psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
 	psw32_t new_psw;
-	u64 addr;
+	u64 addr, iaddr;
 	int rc;
 	u8 ar;
 
 	vcpu->stat.instruction_lpsw++;
 
+	iaddr = gpsw->addr - kvm_s390_get_ilen(vcpu);
 	if (gpsw->mask & PSW_MASK_PSTATE)
 		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
 
@@ -737,18 +738,20 @@ int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
 	gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
 	if (!is_valid_psw(gpsw))
 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+	vcpu->arch.sie_block->gbea = iaddr;
 	return 0;
 }
 
 static int handle_lpswe(struct kvm_vcpu *vcpu)
 {
 	psw_t new_psw;
-	u64 addr;
+	u64 addr, iaddr;
 	int rc;
 	u8 ar;
 
 	vcpu->stat.instruction_lpswe++;
 
+	iaddr = vcpu->arch.sie_block->gpsw.addr - kvm_s390_get_ilen(vcpu);
 	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
 		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
 
@@ -761,6 +764,7 @@ static int handle_lpswe(struct kvm_vcpu *vcpu)
 	vcpu->arch.sie_block->gpsw = new_psw;
 	if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+	vcpu->arch.sie_block->gbea = iaddr;
 	return 0;
 }
 
Signature · 1 line
2.51.0
maintainer Reviewed-by on v1 1/2
#2 lore raw

Am 23.03.26 um 16:35 schrieb Janosch Frank:

Quoted 8 lines

LPSW and LPSWE need to set the gbea on completion but currently don't. Time to fix this up.

LPSWEY was designed to not set the bear.

Fixes 48a3e950f4cee ("KVM: s390: Add support for machine checks.")
maintainer Reviewed-by on v1 1/2
#3 lore raw
In reply to Janosch Frank
Quoted 8 lines · On Mon, 23 Mar 2026 15:35:22 +0000 Janosch Frank <[email protected]> wrote:

LPSW and LPSWE need to set the gbea on completion but currently don't. Time to fix this up.

LPSWEY was designed to not set the bear.

Fixes 48a3e950f4cee ("KVM: s390: Add support for machine checks.")
Quoted 53 lines
---
 arch/s390/kvm/priv.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/s390/kvm/priv.c +6 -2
index a3250ad83a8e..cc0553da14cb 100644
@@ -714,12 +714,13 @@ int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
 {
 	psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
 	psw32_t new_psw;
-	u64 addr;
+	u64 addr, iaddr;
 	int rc;
 	u8 ar;
 
 	vcpu->stat.instruction_lpsw++;
 
+	iaddr = gpsw->addr - kvm_s390_get_ilen(vcpu);
 	if (gpsw->mask & PSW_MASK_PSTATE)
 		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
 
@@ -737,18 +738,20 @@ int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
 	gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
 	if (!is_valid_psw(gpsw))
 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+	vcpu->arch.sie_block->gbea = iaddr;
 	return 0;
 }
 
 static int handle_lpswe(struct kvm_vcpu *vcpu)
 {
 	psw_t new_psw;
-	u64 addr;
+	u64 addr, iaddr;
 	int rc;
 	u8 ar;
 
 	vcpu->stat.instruction_lpswe++;
 
+	iaddr = vcpu->arch.sie_block->gpsw.addr - kvm_s390_get_ilen(vcpu);
 	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
 		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
 
@@ -761,6 +764,7 @@ static int handle_lpswe(struct kvm_vcpu *vcpu)
 	vcpu->arch.sie_block->gpsw = new_psw;
 	if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+	vcpu->arch.sie_block->gbea = iaddr;
 	return 0;
 }
 
maintainer
#4 lore raw
In reply to Janosch Frank
Quoted 32 lines · Janosch Frank

LPSW and LPSWE need to set the gbea on completion but currently don't. Time to fix this up.

LPSWEY was designed to not set the bear.

Fixes 48a3e950f4cee ("KVM: s390: Add support for machine checks.")
---
 arch/s390/kvm/priv.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/s390/kvm/priv.c +2 -1
index a3250ad83a8e..cc0553da14cb 100644
@@ -714,12 +714,13 @@ int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
 {
 	psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
 	psw32_t new_psw;
-	u64 addr;
+	u64 addr, iaddr;
 	int rc;
 	u8 ar;
 
 	vcpu->stat.instruction_lpsw++;
 
+	iaddr = gpsw->addr - kvm_s390_get_ilen(vcpu);
 	if (gpsw->mask & PSW_MASK_PSTATE)
 		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
 

arch/s390/kvm/priv.c int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu) · line 742

Quoted 5 lines
arch/s390/kvm/priv.c +1 -0
@@ -737,18 +738,20 @@ int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
 	gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
 	if (!is_valid_psw(gpsw))
 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+	vcpu->arch.sie_block->gbea = iaddr;
 	return 0;

This doesn't look correct to me: even if there is an early specification exception the PSW is still replaced, and hence the breaking event address needs to be updated. Read: the gbea assignment should be moved before the is_valid_psw() check.

arch/s390/kvm/priv.c int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu) · line 749

Quoted 6 lines
arch/s390/kvm/priv.c +1 -1
 static int handle_lpswe(struct kvm_vcpu *vcpu)
 {
 	psw_t new_psw;
-	u64 addr;
+	u64 addr, iaddr;
 	int rc;

This breaks reverse x-mas style. :)

arch/s390/kvm/priv.c static int handle_lpswe(struct kvm_vcpu *vcpu) · line 768

Quoted 6 lines
arch/s390/kvm/priv.c +1 -0
@@ -761,6 +764,7 @@ static int handle_lpswe(struct kvm_vcpu *vcpu)
 	vcpu->arch.sie_block->gpsw = new_psw;
 	if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+	vcpu->arch.sie_block->gbea = iaddr;
 	return 0;

Like above, I believe this is not correct.

author maintainer
#5 lore raw
Quoted 32 lines · Heiko Carstens
Quoted 31 lines · Janosch Frank

LPSW and LPSWE need to set the gbea on completion but currently don't. Time to fix this up.

LPSWEY was designed to not set the bear.

Fixes 48a3e950f4cee ("KVM: s390: Add support for machine checks.")
---
  arch/s390/kvm/priv.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
arch/s390/kvm/priv.c +2 -1
index a3250ad83a8e..cc0553da14cb 100644
@@ -714,12 +714,13 @@ int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
  {
  	psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
  	psw32_t new_psw;
-	u64 addr;
+	u64 addr, iaddr;
  	int rc;
  	u8 ar;
 
  	vcpu->stat.instruction_lpsw++;
 
+	iaddr = gpsw->addr - kvm_s390_get_ilen(vcpu);
  	if (gpsw->mask & PSW_MASK_PSTATE)
  		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
 
Quoted 11 lines

arch/s390/kvm/priv.c int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu) · line 742

Quoted 6 lines
arch/s390/kvm/priv.c +1 -0
@@ -737,18 +738,20 @@ int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
  	gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
  	if (!is_valid_psw(gpsw))
  		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+	vcpu->arch.sie_block->gbea = iaddr;
  	return 0;

This doesn't look correct to me: even if there is an early specification exception the PSW is still replaced, and hence the breaking event address needs to be updated. Read: the gbea assignment should be moved before the is_valid_psw() check.

Right, LPSW(E) completes even when we get the early specs and bear is set when completing LPSW(E).

We might also be missing setting ilc to 0 on early specs. I need to look into that.

author maintainer
#6 lore raw

PATCH 2/2 KVM: s390: selftests: Add load psw bear test

In reply to Janosch Frank

The bear is set for lpsw and lpswe but not for lpswey.

Load PSW and all of its variants are only emulated by KVM if there's a pending machine check. Therefore the tests inject those but never open the masks to receive them.

---
 tools/testing/selftests/kvm/Makefile.kvm |   1 +
 tools/testing/selftests/kvm/s390/bear.c  | 184 +++++++++++++++++++++++
 2 files changed, 185 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/s390/bear.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 6471fa214a9f..9afb6479dbee 100644
@@ -207,6 +207,7 @@ TEST_GEN_PROGS_s390 += s390/user_operexec
 TEST_GEN_PROGS_s390 += s390/keyop
 TEST_GEN_PROGS_s390 += rseq_test
 TEST_GEN_PROGS_s390 += s390/irq_routing
+TEST_GEN_PROGS_s390 += s390/bear
 
 TEST_GEN_PROGS_riscv = $(TEST_GEN_PROGS_COMMON)
 TEST_GEN_PROGS_riscv += riscv/sbi_pmu_test
tools/testing/selftests/kvm/s390/bear.c new file +184 -0

184 lines not on this page · raw message

Signature · 1 line
2.51.0
maintainer Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] KVM: s390: selftests: Add load psw bear test

Am 23.03.26 um 16:35 schrieb Janosch Frank:

Quoted 7 lines

The bear is set for lpsw and lpswe but not for lpswey.

Load PSW and all of its variants are only emulated by KVM if there's a pending machine check. Therefore the tests inject those but never open the masks to receive them.

#8 lore raw

Re: [PATCH 2/2] KVM: s390: selftests: Add load psw bear test

In reply to Janosch Frank

Had a 2nd look.
Just wanted to test that lpswe does indeed leaves the BEAR unchanged and then I realized that lpswey does not work in our guest due to missing cpu model. So either remove the lpswey test for now or fix it.
Something like this on top

tools/testing/selftests/kvm/s390/bear.c +18 -6
@@ -22,7 +22,10 @@ static void guest_lpswey(void)
        u64 bear;
 
        asm volatile (
-               "       larl    %%r3,lpswey_addr\n"
+               "       larl    %%r3,jump_addr\n"
+               "jump_addr:\n"
+               "       j lpswey_addr\n"
+               "       nop\n"
                "lpswey_addr:\n"
                "       .insn   siy,0xeb0000000071,%[psw],0\n"
                "       nop\n"
@@ -107,21 +110,30 @@ static void test_lpswey(void)
        struct kvm_vcpu *vcpu;
        struct kvm_run *run;
        struct kvm_vm *vm;
+       struct kvm_s390_vm_cpu_processor proc;
+       int r;
+
+       vm = vm_create(1);
+       __kvm_device_attr_get(vm->fd, KVM_S390_VM_CPU_MODEL,
+                                 KVM_S390_VM_CPU_PROCESSOR, &proc);
+       proc.fac_list[3] |= 0x4000000000000000UL;
+       r = __kvm_device_attr_set(vm->fd, KVM_S390_VM_CPU_MODEL,
+                                 KVM_S390_VM_CPU_PROCESSOR, &proc);
+       vcpu = vm_vcpu_add(vm, 0, guest_lpswey);
 
-       vm = vm_create_with_one_vcpu(&vcpu, guest_lpswey);
        inject_mcheck(vcpu);
        run = vcpu->run;
        vcpu_run(vcpu);
-       ksft_test_result(run->s.regs.gprs[3] != run->s.regs.gprs[4],
-                        "emulation: lpswey bear does not match\n");
+       ksft_test_result(run->s.regs.gprs[3] == run->s.regs.gprs[4],
+                        "emulation: lpswey bear does match previous\n");
        kvm_vm_free(vm);
 
        vm = vm_create_with_one_vcpu(&vcpu, guest_lpswey);
        run = vcpu->run;
        vcpu_run(vcpu);
        ksft_test_result(run->s.regs.gprs[3] &&
-                        run->s.regs.gprs[3] != run->s.regs.gprs[4],
-                        "interpretation: lpswey bear does not match\n");
+                        run->s.regs.gprs[3] == run->s.regs.gprs[4],
+                        "interpretation: lpswey bear does match previous\n");
        kvm_vm_free(vm);
 }
 
author maintainer
#9 lore raw

Re: [PATCH 2/2] KVM: s390: selftests: Add load psw bear test

Quoted 5 lines · Christian Borntraeger

Had a 2nd look.
Just wanted to test that lpswe does indeed leaves the BEAR unchanged and then I realized that lpswey does not work in our guest due to missing cpu model. So either remove the lpswey test for now or fix it.
Something like this on top

Yeah one additional problem is that a PGM during guest execution might easily be missed since there are no checks and guest execution currently actually ends with an operation exception as well.

I'll remove the lpswey test for this series and will try to come up with a fix for the PGM issue for 7.1 or 7.2.

Quoted 52 lines
tools/testing/selftests/kvm/s390/bear.c +18 -6
@@ -22,7 +22,10 @@ static void guest_lpswey(void)
          u64 bear;
 
          asm volatile (
-               "       larl    %%r3,lpswey_addr\n"
+               "       larl    %%r3,jump_addr\n"
+               "jump_addr:\n"
+               "       j lpswey_addr\n"
+               "       nop\n"
                  "lpswey_addr:\n"
                  "       .insn   siy,0xeb0000000071,%[psw],0\n"
                  "       nop\n"
@@ -107,21 +110,30 @@ static void test_lpswey(void)
          struct kvm_vcpu *vcpu;
          struct kvm_run *run;
          struct kvm_vm *vm;
+       struct kvm_s390_vm_cpu_processor proc;
+       int r;
+
+       vm = vm_create(1);
+       __kvm_device_attr_get(vm->fd, KVM_S390_VM_CPU_MODEL,
+                                 KVM_S390_VM_CPU_PROCESSOR, &proc);
+       proc.fac_list[3] |= 0x4000000000000000UL;
+       r = __kvm_device_attr_set(vm->fd, KVM_S390_VM_CPU_MODEL,
+                                 KVM_S390_VM_CPU_PROCESSOR, &proc);
+       vcpu = vm_vcpu_add(vm, 0, guest_lpswey);
 
-       vm = vm_create_with_one_vcpu(&vcpu, guest_lpswey);
          inject_mcheck(vcpu);
          run = vcpu->run;
          vcpu_run(vcpu);
-       ksft_test_result(run->s.regs.gprs[3] != run->s.regs.gprs[4],
-                        "emulation: lpswey bear does not match\n");
+       ksft_test_result(run->s.regs.gprs[3] == run->s.regs.gprs[4],
+                        "emulation: lpswey bear does match previous\n");
          kvm_vm_free(vm);
 
          vm = vm_create_with_one_vcpu(&vcpu, guest_lpswey);
          run = vcpu->run;
          vcpu_run(vcpu);
          ksft_test_result(run->s.regs.gprs[3] &&
-                        run->s.regs.gprs[3] != run->s.regs.gprs[4],
-                        "interpretation: lpswey bear does not match\n");
+                        run->s.regs.gprs[3] == run->s.regs.gprs[4],
+                        "interpretation: lpswey bear does match previous\n");
          kvm_vm_free(vm);
   }
 
author maintainer
#10 lore raw

PATCH v2 1/1 KVM: s390: selftests: Add load psw bear test

In reply to Janosch Frank

The bear is set for lpsw and lpswe, so let's check that also happens when kvm emulates these instructions.

Load PSW and all of its variants are only emulated by KVM if there's a pending machine check. Therefore the tests inject those but never open the masks to receive them.

---
 tools/testing/selftests/kvm/Makefile.kvm |   1 +
 tools/testing/selftests/kvm/s390/bear.c  | 133 +++++++++++++++++++++++
 2 files changed, 134 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/s390/bear.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 6471fa214a9f..9afb6479dbee 100644
@@ -207,6 +207,7 @@ TEST_GEN_PROGS_s390 += s390/user_operexec
 TEST_GEN_PROGS_s390 += s390/keyop
 TEST_GEN_PROGS_s390 += rseq_test
 TEST_GEN_PROGS_s390 += s390/irq_routing
+TEST_GEN_PROGS_s390 += s390/bear
 
 TEST_GEN_PROGS_riscv = $(TEST_GEN_PROGS_COMMON)
 TEST_GEN_PROGS_riscv += riscv/sbi_pmu_test
tools/testing/selftests/kvm/s390/bear.c new file +133 -0

133 lines not on this page · raw message

Signature · 1 line
2.51.0