Skip to contentSkip to conversation

KVM: PPC: MMIO fixes

PATCH Superseded Fabiano Rosas 3 participants 17 messages Jan 7, 2022 – Jan 11, 2022

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover KVM: PPC: MMIO fixes

This v3 addresses review comments:

Merge patches 3 and 7, now patch 6, which returns EMULATE_FAIL and now also alters the error message.

Remove the now unnecessary 'advance' variable from emulate_loadstore in patch 4.

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

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

Fabiano Rosas (6):
KVM: PPC: Book3S HV: Stop returning internal values to userspace
KVM: PPC: Fix vmx/vsx mixup in mmio emulation
KVM: PPC: Don't use pr_emerg when mmio emulation fails
KVM: PPC: mmio: Queue interrupt at kvmppc_emulate_mmio
KVM: PPC: mmio: Return to guest after emulation failure
KVM: PPC: mmio: Reject instructions that access more than mmio.data
size

 arch/powerpc/kvm/emulate_loadstore.c |  8 +-------
 arch/powerpc/kvm/powerpc.c           | 24 +++++++++++++++++-------
 2 files changed, 18 insertions(+), 14 deletions(-)
Signature · 1 line
2.33.1
#1 lore raw

PATCH v3 1/6 KVM: PPC: Book3S HV: Stop returning internal values to userspace

Our kvm_arch_vcpu_ioctl_run currently returns the RESUME_HOST values to userspace, against the API of the KVM_RUN ioctl which returns 0 on success.

---
This was noticed while enabling the kvm selftests for powerpc. There's an assert at the _vcpu_run function when we return a value different from the expected.

---
 arch/powerpc/kvm/powerpc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/powerpc/kvm/powerpc.c +8 -0
index a72920f4f221..1e130bb087c4 100644
@@ -1849,6 +1849,14 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 #ifdef CONFIG_ALTIVEC
 out:
 #endif
+
+	/*
+	 * We're already returning to userspace, don't pass the
+	 * RESUME_HOST flags along.
+	 */
+	if (r > 0)
+		r = 0;
+
 	vcpu_put(vcpu);
 	return r;
 }
Signature · 1 line
2.33.1
#2 lore raw

PATCH v3 2/6 KVM: PPC: Fix vmx/vsx mixup in mmio emulation

In reply to Fabiano Rosas

The MMIO emulation code for vector instructions is duplicated between VSX and VMX. When emulating VMX we should check the VMX copy size instead of the VSX one.

Fixes acc9eb9305fe ("KVM: PPC: Reimplement LOAD_VMX/STORE_VMX instruction ...")
---
 arch/powerpc/kvm/powerpc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/kvm/powerpc.c +2 -2
index 1e130bb087c4..92e552ab5a77 100644
@@ -1507,7 +1507,7 @@ int kvmppc_handle_vmx_load(struct kvm_vcpu *vcpu,
 {
 	enum emulation_result emulated = EMULATE_DONE;
 
-	if (vcpu->arch.mmio_vsx_copy_nums > 2)
+	if (vcpu->arch.mmio_vmx_copy_nums > 2)
 		return EMULATE_FAIL;
 
 	while (vcpu->arch.mmio_vmx_copy_nums) {
@@ -1604,7 +1604,7 @@ int kvmppc_handle_vmx_store(struct kvm_vcpu *vcpu,
 	unsigned int index = rs & KVM_MMIO_REG_MASK;
 	enum emulation_result emulated = EMULATE_DONE;
 
-	if (vcpu->arch.mmio_vsx_copy_nums > 2)
+	if (vcpu->arch.mmio_vmx_copy_nums > 2)
 		return EMULATE_FAIL;
 
 	vcpu->arch.io_gpr = rs;
Signature · 1 line
2.33.1
#3 lore raw

PATCH v3 3/6 KVM: PPC: Don't use pr_emerg when mmio emulation fails

In reply to Fabiano Rosas

If MMIO emulation fails we deliver a Program interrupt to the guest. This is a normal event for the host, so use pr_info.

---
 arch/powerpc/kvm/powerpc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/powerpc.c +1 -1
index 92e552ab5a77..4d7d0d080232 100644
@@ -308,7 +308,7 @@ int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
 
 		kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
 		/* XXX Deliver Program interrupt to guest. */
-		pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
+		pr_info("%s: emulation failed (%08x)\n", __func__, last_inst);
 		r = RESUME_HOST;
 		break;
 	}
Signature · 1 line
2.33.1
#4 lore raw

Re: [PATCH v3 3/6] KVM: PPC: Don't use pr_emerg when mmio emulation fails

Excerpts from Fabiano Rosas's message of January 8, 2022 7:00 am:

Quoted 5 lines

If MMIO emulation fails we deliver a Program interrupt to the guest. This is a normal event for the host, so use pr_info.

---

Should it be rate limited to prevent guest spamming host log? In the case of informational messages it's always good if it gives the administrator some idea of what to do with it. If it's normal for the host does it even need a message? If yes, then identifying which guest and adding something like "(this might becaused by a bug in guest driver)" would set the poor admin's mind at rest.

Thanks,
Nick

Quoted 20 lines
 arch/powerpc/kvm/powerpc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/powerpc.c +1 -1
index 92e552ab5a77..4d7d0d080232 100644
@@ -308,7 +308,7 @@ int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
 
 		kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
 		/* XXX Deliver Program interrupt to guest. */
-		pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
+		pr_info("%s: emulation failed (%08x)\n", __func__, last_inst);
 		r = RESUME_HOST;
 		break;
 	}
Signature · 1 line
2.33.1
#5 lore raw

Re: [PATCH v3 3/6] KVM: PPC: Don't use pr_emerg when mmio emulation fails

Quoted 7 lines · Nicholas Piggin

Excerpts from Fabiano Rosas's message of January 8, 2022 7:00 am:

Quoted 5 lines

If MMIO emulation fails we deliver a Program interrupt to the guest. This is a normal event for the host, so use pr_info.

---

Quoted 6 lines

Should it be rate limited to prevent guest spamming host log? In the case of informational messages it's always good if it gives the administrator some idea of what to do with it. If it's normal for the host does it even need a message? If yes, then identifying which guest and adding something like "(this might becaused by a bug in guest driver)" would set the poor admin's mind at rest.

I'll drop this message then and improve the other one that is emitted earlier at the emulation code.

#6 lore raw

PATCH v3 4/6 KVM: PPC: mmio: Queue interrupt at kvmppc_emulate_mmio

In reply to Fabiano Rosas

If MMIO emulation fails, we queue a Program interrupt to the guest. Move that line up into kvmppc_emulate_mmio, which is where we set RESUME_GUEST/HOST. This allows the removal of the 'advance' variable.

No functional change, just separation of responsibilities.

---
 arch/powerpc/kvm/emulate_loadstore.c | 8 +-------
 arch/powerpc/kvm/powerpc.c           | 2 +-
 2 files changed, 2 insertions(+), 8 deletions(-)
arch/powerpc/kvm/emulate_loadstore.c +1 -7
index 48272a9b9c30..4dec920fe4c9 100644
@@ -73,7 +73,6 @@ int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu)
 {
 	u32 inst;
 	enum emulation_result emulated = EMULATE_FAIL;
-	int advance = 1;
 	struct instruction_op op;
 
 	/* this default type might be overwritten by subcategories */
@@ -355,15 +354,10 @@ int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu)
 		}
 	}
 
-	if (emulated = EMULATE_FAIL) {
-		advance = 0;
-		kvmppc_core_queue_program(vcpu, 0);
-	}
-
 	trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
 
 	/* Advance past emulated instruction. */
-	if (advance)
+	if (emulated != EMULATE_FAIL)
 		kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
 
 	return emulated;
arch/powerpc/kvm/powerpc.c +1 -1
index 4d7d0d080232..6daeea4a7de1 100644
@@ -307,7 +307,7 @@ int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
 		u32 last_inst;
 
 		kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
-		/* XXX Deliver Program interrupt to guest. */
+		kvmppc_core_queue_program(vcpu, 0);
 		pr_info("%s: emulation failed (%08x)\n", __func__, last_inst);
 		r = RESUME_HOST;
 		break;
Signature · 1 line
2.33.1
Reviewed-by on v3 4/6
#7 lore raw

Re: [PATCH v3 4/6] KVM: PPC: mmio: Queue interrupt at kvmppc_emulate_mmio

Quoted 8 lines · Fabiano Rosas

If MMIO emulation fails, we queue a Program interrupt to the guest. Move that line up into kvmppc_emulate_mmio, which is where we set RESUME_GUEST/HOST. This allows the removal of the 'advance' variable.

No functional change, just separation of responsibilities.

Quoted 47 lines
---
  arch/powerpc/kvm/emulate_loadstore.c | 8 +-------
  arch/powerpc/kvm/powerpc.c           | 2 +-
  2 files changed, 2 insertions(+), 8 deletions(-)
arch/powerpc/kvm/emulate_loadstore.c +1 -7
index 48272a9b9c30..4dec920fe4c9 100644
@@ -73,7 +73,6 @@ int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu)
  {
  	u32 inst;
  	enum emulation_result emulated = EMULATE_FAIL;
-	int advance = 1;
  	struct instruction_op op;
 
  	/* this default type might be overwritten by subcategories */
@@ -355,15 +354,10 @@ int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu)
  		}
  	}
 
-	if (emulated = EMULATE_FAIL) {
-		advance = 0;
-		kvmppc_core_queue_program(vcpu, 0);
-	}
-
  	trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
 
  	/* Advance past emulated instruction. */
-	if (advance)
+	if (emulated != EMULATE_FAIL)
  		kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
 
  	return emulated;
arch/powerpc/kvm/powerpc.c +1 -1
index 4d7d0d080232..6daeea4a7de1 100644
@@ -307,7 +307,7 @@ int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
  		u32 last_inst;
 
  		kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
-		/* XXX Deliver Program interrupt to guest. */
+		kvmppc_core_queue_program(vcpu, 0);
  		pr_info("%s: emulation failed (%08x)\n", __func__, last_inst);
  		r = RESUME_HOST;
  		break;
reviewer Reviewed-by on v3 4/6
#8 lore raw

Re: [PATCH v3 4/6] KVM: PPC: mmio: Queue interrupt at kvmppc_emulate_mmio

In reply to Fabiano Rosas

Excerpts from Fabiano Rosas's message of January 8, 2022 7:00 am:

Quoted 6 lines

If MMIO emulation fails, we queue a Program interrupt to the guest. Move that line up into kvmppc_emulate_mmio, which is where we set RESUME_GUEST/HOST. This allows the removal of the 'advance' variable.

No functional change, just separation of responsibilities.

Looks cleaner.

Quoted 53 lines
---
 arch/powerpc/kvm/emulate_loadstore.c | 8 +-------
 arch/powerpc/kvm/powerpc.c           | 2 +-
 2 files changed, 2 insertions(+), 8 deletions(-)
arch/powerpc/kvm/emulate_loadstore.c +1 -7
index 48272a9b9c30..4dec920fe4c9 100644
@@ -73,7 +73,6 @@ int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu)
 {
 	u32 inst;
 	enum emulation_result emulated = EMULATE_FAIL;
-	int advance = 1;
 	struct instruction_op op;
 
 	/* this default type might be overwritten by subcategories */
@@ -355,15 +354,10 @@ int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu)
 		}
 	}
 
-	if (emulated == EMULATE_FAIL) {
-		advance = 0;
-		kvmppc_core_queue_program(vcpu, 0);
-	}
-
 	trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
 
 	/* Advance past emulated instruction. */
-	if (advance)
+	if (emulated != EMULATE_FAIL)
 		kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
 
 	return emulated;
arch/powerpc/kvm/powerpc.c +1 -1
index 4d7d0d080232..6daeea4a7de1 100644
@@ -307,7 +307,7 @@ int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
 		u32 last_inst;
 
 		kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
-		/* XXX Deliver Program interrupt to guest. */
+		kvmppc_core_queue_program(vcpu, 0);
 		pr_info("%s: emulation failed (%08x)\n", __func__, last_inst);
 		r = RESUME_HOST;
 		break;
Signature · 1 line
2.33.1
#9 lore raw

PATCH v3 5/6 KVM: PPC: mmio: Return to guest after emulation failure

In reply to Fabiano Rosas

If MMIO emulation fails we don't want to crash the whole guest by returning to userspace.

The original commit bbf45ba57eae ("KVM: ppc: PowerPC 440 KVM implementation") added a todo:

/* XXX Deliver Program interrupt to guest. */

and later the commit d69614a295ae ("KVM: PPC: Separate loadstore emulation from priv emulation") added the Program interrupt injection but in another file, so I'm assuming it was missed that this block needed to be altered.

---
 arch/powerpc/kvm/powerpc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/powerpc.c +1 -1
index 6daeea4a7de1..56b0faab7a5f 100644
@@ -309,7 +309,7 @@ int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
 		kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
 		kvmppc_core_queue_program(vcpu, 0);
 		pr_info("%s: emulation failed (%08x)\n", __func__, last_inst);
-		r = RESUME_HOST;
+		r = RESUME_GUEST;
 		break;
 	}
 	default:
Signature · 1 line
2.33.1
#10 lore raw

Re: [PATCH v3 5/6] KVM: PPC: mmio: Return to guest after emulation failure

Excerpts from Fabiano Rosas's message of January 8, 2022 7:00 am:

Quoted 24 lines

If MMIO emulation fails we don't want to crash the whole guest by returning to userspace.

The original commit bbf45ba57eae ("KVM: ppc: PowerPC 440 KVM implementation") added a todo:

/* XXX Deliver Program interrupt to guest. */

and later the commit d69614a295ae ("KVM: PPC: Separate loadstore emulation from priv emulation") added the Program interrupt injection but in another file, so I'm assuming it was missed that this block needed to be altered.

---
 arch/powerpc/kvm/powerpc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/powerpc.c +0 -0
index 6daeea4a7de1..56b0faab7a5f 100644

arch/powerpc/kvm/powerpc.c int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu) · line 312

Quoted 5 lines
arch/powerpc/kvm/powerpc.c +1 -1
@@ -309,7 +309,7 @@ int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
 		kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
 		kvmppc_core_queue_program(vcpu, 0);
 		pr_info("%s: emulation failed (%08x)\n", __func__, last_inst);
-		r = RESUME_HOST;
+		r = RESUME_GUEST;

So at this point can the pr_info just go away?

I wonder if this shouldn't be a DSI rather than a program check. DSI with DSISR[37] looks a bit more expected. Not that Linux probably does much with it but at least it would give a SIGBUS rather than SIGILL.

Thanks,
Nick

#11 lore raw

Re: [PATCH v3 5/6] KVM: PPC: mmio: Return to guest after emulation failure

Quoted 33 lines · Nicholas Piggin

Excerpts from Fabiano Rosas's message of January 8, 2022 7:00 am:

Quoted 24 lines

If MMIO emulation fails we don't want to crash the whole guest by returning to userspace.

The original commit bbf45ba57eae ("KVM: ppc: PowerPC 440 KVM implementation") added a todo:

   /* XXX Deliver Program interrupt to guest. */

and later the commit d69614a295ae ("KVM: PPC: Separate loadstore emulation from priv emulation") added the Program interrupt injection but in another file, so I'm assuming it was missed that this block needed to be altered.

---
  arch/powerpc/kvm/powerpc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/powerpc.c +0 -0
index 6daeea4a7de1..56b0faab7a5f 100644

arch/powerpc/kvm/powerpc.c int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu) · line 312

Quoted 5 lines
arch/powerpc/kvm/powerpc.c +1 -1
@@ -309,7 +309,7 @@ int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
  		kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
  		kvmppc_core_queue_program(vcpu, 0);
  		pr_info("%s: emulation failed (%08x)\n", __func__, last_inst);
-		r = RESUME_HOST;
+		r = RESUME_GUEST;

So at this point can the pr_info just go away?

Quoted 4 lines

I wonder if this shouldn't be a DSI rather than a program check. DSI with DSISR[37] looks a bit more expected. Not that Linux probably does much with it but at least it would give a SIGBUS rather than SIGILL.

It does not like it is more expected to me, it is not about wrong memory attributes, it is the instruction itself which cannot execute.

DSISR[37]:
Set to 1 if the access is due to a lq, stq, lwat, ldat, lbarx, lharx, lwarx, ldarx, lqarx, stwat,
stdat, stbcx., sthcx., stwcx., stdcx., or stqcx. instruction that addresses storage that is Write
Through Required or Caching Inhibited; or if the access is due to a copy or paste. instruction
that addresses storage that is Caching Inhibited; or if the access is due to a lwat, ldat, stwat, or
stdat instruction that addresses storage that is Guarded; otherwise set to 0.

#12 lore raw

Re: [PATCH v3 5/6] KVM: PPC: mmio: Return to guest after emulation failure

Excerpts from Alexey Kardashevskiy's message of January 11, 2022 9:51 am:

Quoted 36 lines · Nicholas Piggin
Quoted 33 lines

Excerpts from Fabiano Rosas's message of January 8, 2022 7:00 am:

Quoted 24 lines

If MMIO emulation fails we don't want to crash the whole guest by returning to userspace.

The original commit bbf45ba57eae ("KVM: ppc: PowerPC 440 KVM implementation") added a todo:

   /* XXX Deliver Program interrupt to guest. */

and later the commit d69614a295ae ("KVM: PPC: Separate loadstore emulation from priv emulation") added the Program interrupt injection but in another file, so I'm assuming it was missed that this block needed to be altered.

---
  arch/powerpc/kvm/powerpc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/powerpc.c +0 -0
index 6daeea4a7de1..56b0faab7a5f 100644

arch/powerpc/kvm/powerpc.c int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu) · line 312

Quoted 5 lines
arch/powerpc/kvm/powerpc.c +1 -1
@@ -309,7 +309,7 @@ int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
  		kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
  		kvmppc_core_queue_program(vcpu, 0);
  		pr_info("%s: emulation failed (%08x)\n", __func__, last_inst);
-		r = RESUME_HOST;
+		r = RESUME_GUEST;

So at this point can the pr_info just go away?

Quoted 7 lines
Quoted 4 lines

I wonder if this shouldn't be a DSI rather than a program check. DSI with DSISR[37] looks a bit more expected. Not that Linux probably does much with it but at least it would give a SIGBUS rather than SIGILL.

It does not like it is more expected to me, it is not about wrong memory attributes, it is the instruction itself which cannot execute.

It's not an illegal instruction though, it can't execute because of the nature of the data / address it is operating on. That says d-side to me.

DSISR[37] isn't perfect but if you squint it's not terrible. It's about certain instructions that have restrictions operating on other than normal cacheable mappings.

Thanks,
Nick

Quoted 13 lines

DSISR[37]:
Set to 1 if the access is due to a lq, stq, lwat, ldat, lbarx, lharx, lwarx, ldarx, lqarx, stwat,
stdat, stbcx., sthcx., stwcx., stdcx., or stqcx. instruction that addresses storage that is Write
Through Required or Caching Inhibited; or if the access is due to a copy or paste. instruction
that addresses storage that is Caching Inhibited; or if the access is due to a lwat, ldat, stwat, or
stdat instruction that addresses storage that is Guarded; otherwise set to 0.

#13 lore raw

Re: [PATCH v3 5/6] KVM: PPC: mmio: Return to guest after emulation failure

Quoted 45 lines · Nicholas Piggin

Excerpts from Alexey Kardashevskiy's message of January 11, 2022 9:51 am:

Quoted 36 lines · Nicholas Piggin
Quoted 33 lines

Excerpts from Fabiano Rosas's message of January 8, 2022 7:00 am:

Quoted 24 lines

If MMIO emulation fails we don't want to crash the whole guest by returning to userspace.

The original commit bbf45ba57eae ("KVM: ppc: PowerPC 440 KVM implementation") added a todo:

   /* XXX Deliver Program interrupt to guest. */

and later the commit d69614a295ae ("KVM: PPC: Separate loadstore emulation from priv emulation") added the Program interrupt injection but in another file, so I'm assuming it was missed that this block needed to be altered.

---
  arch/powerpc/kvm/powerpc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kvm/powerpc.c +0 -0
index 6daeea4a7de1..56b0faab7a5f 100644

arch/powerpc/kvm/powerpc.c int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu) · line 312

Quoted 5 lines
arch/powerpc/kvm/powerpc.c +1 -1
@@ -309,7 +309,7 @@ int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
  		kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
  		kvmppc_core_queue_program(vcpu, 0);
  		pr_info("%s: emulation failed (%08x)\n", __func__, last_inst);
-		r = RESUME_HOST;
+		r = RESUME_GUEST;

So at this point can the pr_info just go away?

Quoted 7 lines
Quoted 4 lines

I wonder if this shouldn't be a DSI rather than a program check. DSI with DSISR[37] looks a bit more expected. Not that Linux probably does much with it but at least it would give a SIGBUS rather than SIGILL.

It does not like it is more expected to me, it is not about wrong memory attributes, it is the instruction itself which cannot execute.

Quoted 6 lines

It's not an illegal instruction though, it can't execute because of the nature of the data / address it is operating on. That says d-side to me.

DSISR[37] isn't perfect but if you squint it's not terrible. It's about certain instructions that have restrictions operating on other than normal cacheable mappings.

I think I agree with Nick on this one. At least the DSISR gives _some_ information while the Program is maybe too generic. I would probably be staring at the opcode wondering what is wrong with it.

#14 lore raw

PATCH v3 6/6 KVM: PPC: mmio: Reject instructions that access more than mmio.data size

In reply to Fabiano Rosas

The MMIO interface between the kernel and userspace uses a structure that supports a maximum of 8-bytes of data. Instructions that access more than that need to be emulated in parts.

We currently don't have generic support for splitting the emulation in parts and each set of instructions needs to be explicitly included.

There's already an error message being printed when a load or store exceeds the mmio.data buffer but we don't fail the emulation until later at kvmppc_complete_mmio_load and even then we allow userspace to make a partial copy of the data, which ends up overwriting some fields of the mmio structure.

This patch makes the emulation fail earlier at kvmppc_handle_load|store, which will send a Program interrupt to the guest. This is better than allowing the guest to proceed with partial data.

Note that this was caught in a somewhat artificial scenario using quadword instructions (lq/stq), there's no account of an actual guest in the wild running instructions that are not properly emulated.

(While here, fix the error message to check against 'bytes' and not 'run->mmio.len' which at this point has an old value.)

---
 arch/powerpc/kvm/powerpc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/powerpc/kvm/powerpc.c +4 -2
index 56b0faab7a5f..a1643ca988e0 100644
@@ -1246,7 +1246,8 @@ static int __kvmppc_handle_load(struct kvm_vcpu *vcpu,
 
 	if (bytes > sizeof(run->mmio.data)) {
 		printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
-		       run->mmio.len);
+		       bytes);
+		return EMULATE_FAIL;
 	}
 
 	run->mmio.phys_addr = vcpu->arch.paddr_accessed;
@@ -1335,7 +1336,8 @@ int kvmppc_handle_store(struct kvm_vcpu *vcpu,
 
 	if (bytes > sizeof(run->mmio.data)) {
 		printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
-		       run->mmio.len);
+		       bytes);
+		return EMULATE_FAIL;
 	}
 
 	run->mmio.phys_addr = vcpu->arch.paddr_accessed;
Signature · 1 line
2.33.1
reviewer Reviewed-by on v3 6/6
#15 lore raw

Re: [PATCH v3 6/6] KVM: PPC: mmio: Reject instructions that access more than mmio.data size

Excerpts from Fabiano Rosas's message of January 8, 2022 7:00 am:

Quoted 17 lines

The MMIO interface between the kernel and userspace uses a structure that supports a maximum of 8-bytes of data. Instructions that access more than that need to be emulated in parts.

We currently don't have generic support for splitting the emulation in parts and each set of instructions needs to be explicitly included.

There's already an error message being printed when a load or store exceeds the mmio.data buffer but we don't fail the emulation until later at kvmppc_complete_mmio_load and even then we allow userspace to make a partial copy of the data, which ends up overwriting some fields of the mmio structure.

This patch makes the emulation fail earlier at kvmppc_handle_load|store, which will send a Program interrupt to the guest. This is better than allowing the guest to proceed with partial data.

Quoted 6 lines

Note that this was caught in a somewhat artificial scenario using quadword instructions (lq/stq), there's no account of an actual guest in the wild running instructions that are not properly emulated.

(While here, fix the error message to check against 'bytes' and not 'run->mmio.len' which at this point has an old value.)

This looks good to me

Quoted 12 lines
---
 arch/powerpc/kvm/powerpc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/powerpc/kvm/powerpc.c +0 -0
index 56b0faab7a5f..a1643ca988e0 100644

arch/powerpc/kvm/powerpc.c static int __kvmppc_handle_load(struct kvm_vcpu *vcpu, · line 1249

Quoted 5 lines
arch/powerpc/kvm/powerpc.c +1 -1
@@ -1246,7 +1246,8 @@ static int __kvmppc_handle_load(struct kvm_vcpu *vcpu,
 
 	if (bytes > sizeof(run->mmio.data)) {
 		printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
-		       run->mmio.len);
+		       bytes);

I wonder though this should probably be ratelimited, informational (or at least warning because it's a host message), and perhaps a bit more explanatory that it's a guest problem (or at least lack of host support for particular guest MMIO sizes).

Thanks,
Nick

#16 lore raw

Re: [PATCH v3 6/6] KVM: PPC: mmio: Reject instructions that access more than mmio.data size

Quoted 42 lines · Nicholas Piggin

Excerpts from Fabiano Rosas's message of January 8, 2022 7:00 am:

Quoted 17 lines

The MMIO interface between the kernel and userspace uses a structure that supports a maximum of 8-bytes of data. Instructions that access more than that need to be emulated in parts.

We currently don't have generic support for splitting the emulation in parts and each set of instructions needs to be explicitly included.

There's already an error message being printed when a load or store exceeds the mmio.data buffer but we don't fail the emulation until later at kvmppc_complete_mmio_load and even then we allow userspace to make a partial copy of the data, which ends up overwriting some fields of the mmio structure.

This patch makes the emulation fail earlier at kvmppc_handle_load|store, which will send a Program interrupt to the guest. This is better than allowing the guest to proceed with partial data.

Quoted 6 lines

Note that this was caught in a somewhat artificial scenario using quadword instructions (lq/stq), there's no account of an actual guest in the wild running instructions that are not properly emulated.

(While here, fix the error message to check against 'bytes' and not 'run->mmio.len' which at this point has an old value.)

This looks good to me

Quoted 13 lines
---
 arch/powerpc/kvm/powerpc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/powerpc/kvm/powerpc.c +0 -0
index 56b0faab7a5f..a1643ca988e0 100644
@@ -1246,7 +1246,8 @@ static int __kvmppc_handle_load(struct kvm_vcpu *vcpu,
 
Quoted 9 lines

arch/powerpc/kvm/powerpc.c static int __kvmppc_handle_load(struct kvm_vcpu *vcpu, · line 1249

Quoted 4 lines
arch/powerpc/kvm/powerpc.c +1 -1
 	if (bytes > sizeof(run->mmio.data)) {
 		printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
-		       run->mmio.len);
+		       bytes);

I wonder though this should probably be ratelimited, informational (or at least warning because it's a host message), and perhaps a bit more explanatory that it's a guest problem (or at least lack of host support for particular guest MMIO sizes).

Yes, I'll ratelimit it an try to make it clear that this is something that happened inside the guest but it lacks support in KVM. Then hopefully people will tell to us if they ever need that support.