KVM: PPC: MMIO fixes
Subsystems (2)
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
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
@@ -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 v3 2/6 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 v3 3/6 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
Nicholas Piggin
reviewerRe: [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.Signed-off-by Fabiano Rosas <[email protected]>---
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@@ -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
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.Signed-off-by Fabiano Rosas <[email protected]>---
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.
PATCH v3 4/6 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. 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
@@ -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
@@ -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 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.
Signed-off-by Fabiano Rosas <[email protected]>
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@@ -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@@ -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 ;
Re: [PATCH v3 4/6] KVM: PPC: mmio: Queue interrupt at kvmppc_emulate_mmio
In reply to Fabiano RosasExcerpts 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
Signed-off-by Fabiano Rosas <[email protected]>--- 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@@ -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@@ -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
PATCH v3 5/6 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
Nicholas Piggin
reviewerRe: [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.Signed-off-by Fabiano Rosas <[email protected]>Reviewed-by Alexey Kardashevskiy <[email protected]>--- arch/powerpc/kvm/powerpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/powerpc/kvm/powerpc.c +0
-0
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
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.Signed-off-by Fabiano Rosas <[email protected]>Reviewed-by Alexey Kardashevskiy <[email protected]>--- arch/powerpc/kvm/powerpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/powerpc/kvm/powerpc.c +0
-0arch/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_infojust 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.
Nicholas Piggin
reviewerRe: [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.Signed-off-by Fabiano Rosas <[email protected]>Reviewed-by Alexey Kardashevskiy <[email protected]>--- arch/powerpc/kvm/powerpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/powerpc/kvm/powerpc.c +0
-0arch/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_infojust 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.
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.Signed-off-by Fabiano Rosas <[email protected]>Reviewed-by Alexey Kardashevskiy <[email protected]>--- arch/powerpc/kvm/powerpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/powerpc/kvm/powerpc.c +0
-0arch/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_infojust 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.
PATCH v3 6/6 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.
(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
@@ -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
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_loadand 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
Signed-off-by Fabiano Rosas <[email protected]>Reviewed-by Alexey Kardashevskiy <[email protected]>--- arch/powerpc/kvm/powerpc.c | 6 ++++-- 1 file changed, 4 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,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
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_loadand 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
Reviewed-by Nicholas Piggin <[email protected]>Quoted 13 lines
Signed-off-by Fabiano Rosas <[email protected]>Reviewed-by Alexey Kardashevskiy <[email protected]>--- arch/powerpc/kvm/powerpc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)arch/powerpc/kvm/powerpc.c +0
-0@@ -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
-1if (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.