kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: respect singlestep when emulating instruction
@ 2020-05-18 21:36 Felipe Franciosi
  2020-05-18 22:38 ` Paolo Bonzini
  0 siblings, 1 reply; 3+ messages in thread
From: Felipe Franciosi @ 2020-05-18 21:36 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: kvm, stable, Felipe Franciosi

When userspace configures KVM_GUESTDBG_SINGLESTEP, KVM will manage the
presence of X86_EFLAGS_TF via kvm_set/get_rflags on vcpus. The actual
rflag bit is therefore hidden from callers.

That includes init_emulate_ctxt() which uses the value returned from
kvm_get_flags() to set ctxt->tf. As a result, x86_emulate_instruction()
will skip a single step, leaving singlestep_rip stale and not returning
to userspace.

This resolves the issue by observing the vcpu guest_debug configuration
alongside ctxt->tf in x86_emulate_instruction(), performing the single
step if set.

Signed-off-by: Felipe Franciosi <felipe@nutanix.com>
---
 arch/x86/kvm/x86.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c17e6eb9ad43..78463f01c606 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6919,7 +6919,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
 		if (!ctxt->have_exception ||
 		    exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
 			kvm_rip_write(vcpu, ctxt->eip);
-			if (r && ctxt->tf)
+			if ((r && ctxt->tf) || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))
 				r = kvm_vcpu_do_singlestep(vcpu);
 			if (kvm_x86_ops.update_emulated_instruction)
 				kvm_x86_ops.update_emulated_instruction(vcpu);
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] KVM: x86: respect singlestep when emulating instruction
  2020-05-18 21:36 [PATCH] KVM: x86: respect singlestep when emulating instruction Felipe Franciosi
@ 2020-05-18 22:38 ` Paolo Bonzini
  2020-05-19  7:32   ` Felipe Franciosi
  0 siblings, 1 reply; 3+ messages in thread
From: Paolo Bonzini @ 2020-05-18 22:38 UTC (permalink / raw)
  To: Felipe Franciosi; +Cc: kvm, stable

On 18/05/20 23:36, Felipe Franciosi wrote:
>  		    exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
>  			kvm_rip_write(vcpu, ctxt->eip);
> -			if (r && ctxt->tf)
> +			if ((r && ctxt->tf) || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))
>  				r = kvm_vcpu_do_singlestep(vcpu);

Almost:

	if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))

This is because if r == 0 you have to exit to userspace with KVM_EXIT_MMIO
and KVM_EXIT_IO before completing execution of the instruction.  Once
this is done, you'll get here again and you'll be able to go through
kvm_vcpu_do_singlestep.

Thanks,

Paolo


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] KVM: x86: respect singlestep when emulating instruction
  2020-05-18 22:38 ` Paolo Bonzini
@ 2020-05-19  7:32   ` Felipe Franciosi
  0 siblings, 0 replies; 3+ messages in thread
From: Felipe Franciosi @ 2020-05-19  7:32 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: kvm, stable



> On May 18, 2020, at 11:38 PM, Paolo Bonzini <pbonzini@redhat.com> wrote:
> 
> On 18/05/20 23:36, Felipe Franciosi wrote:
>> 		    exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
>> 			kvm_rip_write(vcpu, ctxt->eip);
>> -			if (r && ctxt->tf)
>> +			if ((r && ctxt->tf) || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))
>> 				r = kvm_vcpu_do_singlestep(vcpu);
> 
> Almost:
> 
> 	if (r && (ctxt->tf || (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP))
> 
> This is because if r == 0 you have to exit to userspace with KVM_EXIT_MMIO
> and KVM_EXIT_IO before completing execution of the instruction.  Once
> this is done, you'll get here again and you'll be able to go through
> kvm_vcpu_do_singlestep.

AHH yeah. I tested *only* with SINGLESTEP enabled and I didn't do any
MMIO/PIO, so I didn't even realise it. Thanks. Sending v2.

F.

> 
> Thanks,
> 
> Paolo
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-05-19  8:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-18 21:36 [PATCH] KVM: x86: respect singlestep when emulating instruction Felipe Franciosi
2020-05-18 22:38 ` Paolo Bonzini
2020-05-19  7:32   ` Felipe Franciosi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).