KVM: PPC: MMIO fixes
Subsystems (2)
v2 cover KVM: PPC: MMIO fixes
The change from v1 is that I have altered the MMIO size check to fail the emulation in case the size exceeds 8 bytes. That brought some cleanups and another fix along with it, we were returning to userspace in case of failure instead of the guest.
This has now become an MMIO series, but since the first commit has been reviewed already, I'm leaving it here.
v1:
https://lore.kernel.org/r/[email protected]
Fabiano Rosas (7):
KVM: PPC: Book3S HV: Stop returning internal values to userspace
KVM: PPC: Fix vmx/vsx mixup in mmio emulation
KVM: PPC: Fix mmio length message
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 | 4 +--- arch/powerpc/kvm/powerpc.c | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-)
Signature · 1 line
2.33.1
PATCH v2 1/7 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
@@ -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
PATCH v2 2/7 KVM: PPC: Fix vmx/vsx mixup in mmio emulation
In reply to Fabiano RosasThe 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.
--- arch/powerpc/kvm/powerpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/kvm/powerpc.c +2 -2
@@ -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
PATCH v2 3/7 KVM: PPC: Fix mmio length message
In reply to Fabiano RosasWe check against 'bytes' but print 'run->mmio.len' which at that point has an old value.
e.g. 16-byte load:
before:__kvmppc_handle_load: bad MMIO length: 8
now:__kvmppc_handle_load: bad MMIO length: 16
--- arch/powerpc/kvm/powerpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/kvm/powerpc.c +2 -2
@@ -1246,7 +1246,7 @@ 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); } run->mmio.phys_addr = vcpu->arch.paddr_accessed;
@@ -1335,7 +1335,7 @@ 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); } run->mmio.phys_addr = vcpu->arch.paddr_accessed;
Signature · 1 line
2.33.1
Re: [PATCH v2 3/7] KVM: PPC: Fix mmio length message
Quoted 21 lines · Fabiano Rosas
We check against 'bytes' but print '
run->mmio.len' which at that point has an old value.e.g. 16-byte load:
before:
__kvmppc_handle_load: bad MMIO length: 8now:
__kvmppc_handle_load: bad MMIO length: 16Signed-off-by Fabiano Rosas <[email protected]>--- arch/powerpc/kvm/powerpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/powerpc/kvm/powerpc.c +0
-0
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,7 @@ 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;" here and below as there is really no point in trashing kvm_run::mmio (not much harm too but still) and this code does not handle more than 8 bytes anyway.
Quoted 12 lines
arch/powerpc/kvm/powerpc.c +1
-1} run->mmio.phys_addr = vcpu->arch.paddr_accessed;@@ -1335,7 +1335,7 @@ 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); } run->mmio.phys_addr = vcpu->arch.paddr_accessed;
Signature · 1 line
Alexey
PATCH v2 4/7 KVM: PPC: Don't use pr_emerg when mmio emulation fails
In reply to Fabiano RosasIf 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
@@ -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
PATCH v2 5/7 KVM: PPC: mmio: Queue interrupt at kvmppc_emulate_mmio
In reply to Fabiano RosasIf 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.
No functional change, just separation of responsibilities.
--- arch/powerpc/kvm/emulate_loadstore.c | 4 +--- arch/powerpc/kvm/powerpc.c | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-)
arch/powerpc/kvm/emulate_loadstore.c +1 -3
@@ -355,10 +355,8 @@ int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu) } } -if (emulated =EMULATE_FAIL ) { +if (emulated =EMULATE_FAIL ) advance =0 ; -kvmppc_core_queue_program (vcpu,0 ); - }trace_kvm_ppc_instr (inst,kvmppc_get_pc (vcpu), emulated);
arch/powerpc/kvm/powerpc.c +1 -1
@@ -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
Re: [PATCH v2 5/7] KVM: PPC: mmio: Queue interrupt at kvmppc_emulate_mmio
Quoted 17 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.No functional change, just separation of responsibilities.
Signed-off-by Fabiano Rosas <[email protected]>--- arch/powerpc/kvm/emulate_loadstore.c | 4 +--- arch/powerpc/kvm/powerpc.c | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-)arch/powerpc/kvm/emulate_loadstore.c +0
-0
arch/powerpc/kvm/emulate_loadstore.c int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu) · line 359
Quoted 6 lines
arch/powerpc/kvm/emulate_loadstore.c +1
-1@@ -355,10 +355,8 @@ int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu) } } -if (emulated =EMULATE_FAIL ) { +if (emulated =EMULATE_FAIL ) advance =0 ;
You can now drop @advance by moving the "if" few lines down.
Quoted 18 lines
arch/powerpc/kvm/emulate_loadstore.c +0
-2-kvmppc_core_queue_program (vcpu,0 ); - }trace_kvm_ppc_instr (inst,kvmppc_get_pc (vcpu), emulated);arch/powerpc/kvm/powerpc.c +1
-1@@ -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
Alexey
PATCH v2 6/7 KVM: PPC: mmio: Return to guest after emulation failure
In reply to Fabiano RosasIf 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
@@ -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
Re: [PATCH v2 6/7] KVM: PPC: mmio: Return to guest after emulation failure
Quoted 6 lines · 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:
Quoted 8 lines
/* 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.Signed-off-by Fabiano Rosas <[email protected]>
Looks right.
but this means if I want to keep debugging those kvm selftests in comfort, I'll have to have some exception handlers in the vm as otherwise the failing $pc is lost after this change :)
Quoted 17 lines
--- arch/powerpc/kvm/powerpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)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 ;break ; }default :
Signature · 1 line
Alexey
Re: [PATCH v2 6/7] KVM: PPC: mmio: Return to guest after emulation failure
Quoted 17 lines · Alexey Kardashevskiy
Quoted 6 lines · 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:Quoted 8 lines
/* 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.Signed-off-by Fabiano Rosas <[email protected]>
Quoted 6 lines
Looks right.
Reviewed-by Alexey Kardashevskiy <[email protected]>but this means if I want to keep debugging those kvm selftests in comfort, I'll have to have some exception handlers in the vm as otherwise the failing $pc is lost after this change :)
Yes! But that will be a problem for any test. These kinds of issues is why I wanted a trial period before sending the test infrastructure upstream. Maybe we don't need exception handlers, but just a way to force the test to crash if it tries to fetch from 0x700.
PATCH v2 7/7 KVM: PPC: mmio: Reject instructions that access more than mmio.data size
In reply to Fabiano RosasThe 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.
--- arch/powerpc/kvm/powerpc.c | 2 ++ 1 file changed, 2 insertions(+)
arch/powerpc/kvm/powerpc.c +2 -0
@@ -1247,6 +1247,7 @@ 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__, bytes); +return EMULATE_FAIL ; } run->mmio.phys_addr = vcpu->arch.paddr_accessed;
@@ -1336,6 +1337,7 @@ int kvmppc_handle_store(struct kvm_vcpu *vcpu,if (bytes >sizeof (run->mmio.data)) {printk (KERN_ERR "%s: bad MMIO length: %d\n" , __func__, bytes); +return EMULATE_FAIL ; } run->mmio.phys_addr = vcpu->arch.paddr_accessed;
Signature · 1 line
2.33.1
Re: [PATCH v2 7/7] KVM: PPC: mmio: Reject instructions that access more than mmio.data size
Quoted 13 lines · 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_loadand even then we allow userspace to make a partial copy of the data, which ends up overwriting some fields of the mmio structure.
Quoted 7 lines
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.
Ah thereee it is :-)
I'd merge it into 3/7.
anyway,
Quoted 26 lines
Signed-off-by Fabiano Rosas <[email protected]>--- arch/powerpc/kvm/powerpc.c | 2 ++ 1 file changed, 2 insertions(+)arch/powerpc/kvm/powerpc.c +2
-0@@ -1247,6 +1247,7 @@ 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__, bytes); +return EMULATE_FAIL ; } run->mmio.phys_addr = vcpu->arch.paddr_accessed;@@ -1336,6 +1337,7 @@ int kvmppc_handle_store(struct kvm_vcpu *vcpu,if (bytes >sizeof (run->mmio.data)) {printk (KERN_ERR "%s: bad MMIO length: %d\n" , __func__, bytes); +return EMULATE_FAIL ; } run->mmio.phys_addr = vcpu->arch.paddr_accessed;
Signature · 1 line
Alexey