From: alex.bennee@linaro.org (Alex Bennée)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 3/3] arm64: kvm: Fix single step for guest skipped instructions
Date: Tue, 03 Oct 2017 17:30:11 +0100 [thread overview]
Message-ID: <873770yz1o.fsf@linaro.org> (raw)
In-Reply-To: <4d9fc0a2-bcf9-ca26-8646-037c2dcc6545@arm.com>
Julien Thierry <julien.thierry@arm.com> writes:
> On 03/10/17 15:57, Alex Benn?e wrote:
>>
>> Julien Thierry <julien.thierry@arm.com> writes:
>>
>>> On 31/08/17 15:01, Christoffer Dall wrote:
<snip>
>>>>>>>
>>>>>>> Does that sound like what you had in mind? Or does it seem better than
>>>>>>> the
>>>>>>> current patch?
>>>>>>>
>>>>>> I was thinking to change the skip_instruction function to return an
>>>>>> int, and then call kvm_handle_debug_ss() from skip_instruction, which
>>>>>> would update the kvm_run structure and exit here and then.
>>>>>>
>>>>>
>>>>> Setting up the debug exception from within kvm_skip_instruction seem to
>>>>> change a bit too much its semantic from arm to arm64. I would find this
>>>>> easily confusing.
>>>>>
>>>>>> However, I'm now thinking that this doesn't really work either,
>>>>>> because we could have to emulate a trapped MMIO instruction in user
>>>>>> space, and then it's not clear how to exit with a debug exception at
>>>>>> the same time.
>>
>> A debug exception at guest exit point is (IIRC) just having the
>> appropriate status in the run->exit_reason (KVM_EXIT_DEBUG). If you need
>> to exit for MMIO emulation (i.e. the instruction has not run yet) you
>> shouldn't do that. Exit, emulate and return. We could handle the ioctl
>> to clear SS in userspace but I guess that gets just as messy.
>>
>>>>>>
>>>>>> So perhaps we should stick with your original approach.
>>>>>>
>>>>>
>>>>> I had not realized that was possible. This makes things more complicated for
>>>>> avoiding a back and forth with the guest for trapped exceptions. Out of
>>>>> luck, having the debug flag does look like single stepping would work as
>>>>> expected for userland MMIOs.
<snip>
This is my currently untested but otherwise simpler solution:
>From 46ea80d7dc9b98661fcd51c41090f8ad74a6690f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alex=20Benn=C3=A9e?= <alex.bennee@linaro.org>
Date: Tue, 3 Oct 2017 17:17:15 +0100
Subject: [PATCH] KVM: arm64: handle single-stepping trapped instructions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
If we are using guest debug to single-step the guest we need to ensure
we exit after emulating the instruction. This only affects
instructions emulated by the kernel. If we exit to userspace anyway we
leave it to userspace to work out what to do.
Signed-off-by: Alex Benn?e <alex.bennee@linaro.org>
---
arch/arm64/kvm/handle_exit.c | 51 ++++++++++++++++++++++++++++++++------------
1 file changed, 37 insertions(+), 14 deletions(-)
diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
index 7debb74843a0..b197ffb10e96 100644
--- a/arch/arm64/kvm/handle_exit.c
+++ b/arch/arm64/kvm/handle_exit.c
@@ -178,6 +178,42 @@ static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
return arm_exit_handlers[hsr_ec];
}
+/*
+ * When handling traps we need to ensure exit the guest if we
+ * successfully emulated the instruction while single-stepping. If we
+ * have to exit anyway for userspace emulation then it's up to
+ * userspace to handle the "while SSing case".
+ */
+
+static int handle_trap_exceptions(struct kvm_vcpu *vcpu, struct kvm_run *run)
+{
+ int handled;
+
+ /*
+ * See ARM ARM B1.14.1: "Hyp traps on instructions
+ * that fail their condition code check"
+ */
+ if (!kvm_condition_valid(vcpu)) {
+ kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
+ handled = 1;
+ } else {
+ exit_handle_fn exit_handler;
+
+ exit_handler = kvm_get_exit_handler(vcpu);
+ handled = exit_handler(vcpu, run);
+ }
+
+ if (handled && (vcpu->debug_flags & KVM_GUESTDBG_SINGLESTEP)) {
+ u32 hsr = kvm_vcpu_get_hsr(vcpu);
+
+ handled = 0;
+ run->exit_reason = KVM_EXIT_DEBUG;
+ run->debug.arch.hsr = hsr;
+ }
+
+ return handled;
+}
+
/*
* Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
* proper exit to userspace.
@@ -185,8 +221,6 @@ static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
int exception_index)
{
- exit_handle_fn exit_handler;
-
if (ARM_SERROR_PENDING(exception_index)) {
u8 hsr_ec = ESR_ELx_EC(kvm_vcpu_get_hsr(vcpu));
@@ -214,18 +248,7 @@ int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
kvm_inject_vabt(vcpu);
return 1;
case ARM_EXCEPTION_TRAP:
- /*
- * See ARM ARM B1.14.1: "Hyp traps on instructions
- * that fail their condition code check"
- */
- if (!kvm_condition_valid(vcpu)) {
- kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
- return 1;
- }
-
- exit_handler = kvm_get_exit_handler(vcpu);
-
- return exit_handler(vcpu, run);
+ return handle_trap_exceptions(vcpu, run);
case ARM_EXCEPTION_HYP_GONE:
/*
* EL2 has been reset to the hyp-stub. This happens when a guest
--
2.14.1
next prev parent reply other threads:[~2017-10-03 16:30 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-30 9:01 [PATCH 0/3] Fix single step for traps Julien Thierry
2017-08-30 9:01 ` [PATCH 1/3] arm64: Use existing defines for mdscr Julien Thierry
2017-08-30 9:01 ` [PATCH 2/3] arm64: Fix single stepping in kernel traps Julien Thierry
2017-08-30 9:01 ` [PATCH 3/3] arm64: kvm: Fix single step for guest skipped instructions Julien Thierry
2017-08-30 9:19 ` Marc Zyngier
2017-08-30 9:40 ` Julien Thierry
2017-08-30 18:53 ` Christoffer Dall
2017-08-31 8:45 ` Julien Thierry
2017-08-31 8:54 ` Christoffer Dall
2017-08-31 9:37 ` Julien Thierry
2017-08-31 10:53 ` Christoffer Dall
2017-08-31 12:56 ` Julien Thierry
2017-08-31 13:28 ` Christoffer Dall
2017-08-31 13:57 ` Julien Thierry
2017-08-31 14:01 ` Christoffer Dall
2017-09-29 12:38 ` Julien Thierry
2017-10-03 14:57 ` Alex Bennée
2017-10-03 15:07 ` Julien Thierry
2017-10-03 15:48 ` Alex Bennée
2017-10-03 16:17 ` Julien Thierry
2017-10-03 16:30 ` Alex Bennée [this message]
2017-10-03 17:08 ` Julien Thierry
2017-10-03 17:26 ` Alex Bennée
2017-10-04 8:07 ` Julien Thierry
2017-10-04 10:08 ` Alex Bennée
2017-10-04 10:28 ` Paolo Bonzini
2017-10-04 10:50 ` Alex Bennée
2017-10-04 14:19 ` Paolo Bonzini
2017-10-04 10:42 ` Julien Thierry
2017-10-04 15:42 ` Alex Bennée
2017-10-04 16:10 ` Julien Thierry
2017-10-04 18:23 ` Alex Bennée
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=873770yz1o.fsf@linaro.org \
--to=alex.bennee@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).