All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Colin Xu <colin.xu@intel.com>, Yu Ning <yu.ning@intel.com>,
	Chuanxiao Dong <chuanxiao.dong@intel.com>
Subject: [Qemu-devel] [PULL 02/25] hax: Honor CPUState::halted
Date: Fri, 21 Jun 2019 13:29:57 +0200	[thread overview]
Message-ID: <1561116620-22245-3-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1561116620-22245-1-git-send-email-pbonzini@redhat.com>

From: Colin Xu <colin.xu@intel.com>

QEMU tracks whether a vcpu is halted using CPUState::halted. E.g.,
after initialization or reset, halted is 0 for the BSP (vcpu 0)
and 1 for the APs (vcpu 1, 2, ...). A halted vcpu should not be
handed to the hypervisor to run (e.g. hax_vcpu_run()).

Under HAXM, Android Emulator sometimes boots into a "vcpu shutdown
request" error while executing in SeaBIOS, with the HAXM driver
logging a guest triple fault in vcpu 1, 2, ... at RIP 0x3. That is
ultimately because the HAX accelerator asks HAXM to run those APs
when they are still in the halted state.

Normally, the vcpu thread for an AP will start by looping in
qemu_wait_io_event(), until the BSP kicks it via a pair of IPIs
(INIT followed by SIPI). But because the HAX accelerator does not
honor cpu->halted, it allows the AP vcpu thread to proceed to
hax_vcpu_run() as soon as it receives any kick, even if the kick
does not come from the BSP. It turns out that emulator has a
worker thread which periodically kicks every vcpu thread (possibly
to collect CPU usage data), and if one of these kicks comes before
those by the BSP, the AP will start execution from the wrong RIP,
resulting in the aforementioned SMP boot failure.

The solution is inspired by the KVM accelerator (credit to
Chuanxiao Dong <chuanxiao.dong@intel.com> for the pointer):

1. Get rid of questionable logic that unconditionally resets
   cpu->halted before hax_vcpu_run(). Instead, only reset it at the
   right moments (there are only a few "unhalt" events).
2. Add a check for cpu->halted before hax_vcpu_run().

Note that although the non-Unrestricted Guest (!ug_platform) code
path also forcibly resets cpu->halted, it is left untouched,
because only the UG code path supports SMP guests.

The patch is first merged to android emulator with Change-Id:
I9c5752cc737fd305d7eace1768ea12a07309d716

Cc: Yu Ning <yu.ning@intel.com>
Cc: Chuanxiao Dong <chuanxiao.dong@intel.com>
Signed-off-by: Colin Xu <colin.xu@intel.com>
Message-Id: <20190610021939.13669-1-colin.xu@intel.com>
---
 cpus.c                |  1 -
 target/i386/hax-all.c | 36 ++++++++++++++++++++++++++++++++++--
 2 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/cpus.c b/cpus.c
index dde3b7b..1af51b7 100644
--- a/cpus.c
+++ b/cpus.c
@@ -1594,7 +1594,6 @@ static void *qemu_hax_cpu_thread_fn(void *arg)
 
     cpu->thread_id = qemu_get_thread_id();
     cpu->created = true;
-    cpu->halted = 0;
     current_cpu = cpu;
 
     hax_init_vcpu(cpu);
diff --git a/target/i386/hax-all.c b/target/i386/hax-all.c
index 64fd51a..9e7b779 100644
--- a/target/i386/hax-all.c
+++ b/target/i386/hax-all.c
@@ -471,13 +471,35 @@ static int hax_vcpu_hax_exec(CPUArchState *env)
         return 0;
     }
 
-    cpu->halted = 0;
-
     if (cpu->interrupt_request & CPU_INTERRUPT_POLL) {
         cpu->interrupt_request &= ~CPU_INTERRUPT_POLL;
         apic_poll_irq(x86_cpu->apic_state);
     }
 
+    /* After a vcpu is halted (either because it is an AP and has just been
+     * reset, or because it has executed the HLT instruction), it will not be
+     * run (hax_vcpu_run()) until it is unhalted. The next few if blocks check
+     * for events that may change the halted state of this vcpu:
+     *  a) Maskable interrupt, when RFLAGS.IF is 1;
+     *     Note: env->eflags may not reflect the current RFLAGS state, because
+     *           it is not updated after each hax_vcpu_run(). We cannot afford
+     *           to fail to recognize any unhalt-by-maskable-interrupt event
+     *           (in which case the vcpu will halt forever), and yet we cannot
+     *           afford the overhead of hax_vcpu_sync_state(). The current
+     *           solution is to err on the side of caution and have the HLT
+     *           handler (see case HAX_EXIT_HLT below) unconditionally set the
+     *           IF_MASK bit in env->eflags, which, in effect, disables the
+     *           RFLAGS.IF check.
+     *  b) NMI;
+     *  c) INIT signal;
+     *  d) SIPI signal.
+     */
+    if (((cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
+         (env->eflags & IF_MASK)) ||
+        (cpu->interrupt_request & CPU_INTERRUPT_NMI)) {
+        cpu->halted = 0;
+    }
+
     if (cpu->interrupt_request & CPU_INTERRUPT_INIT) {
         DPRINTF("\nhax_vcpu_hax_exec: handling INIT for %d\n",
                 cpu->cpu_index);
@@ -493,6 +515,16 @@ static int hax_vcpu_hax_exec(CPUArchState *env)
         hax_vcpu_sync_state(env, 1);
     }
 
+    if (cpu->halted) {
+        /* If this vcpu is halted, we must not ask HAXM to run it. Instead, we
+         * break out of hax_smp_cpu_exec() as if this vcpu had executed HLT.
+         * That way, this vcpu thread will be trapped in qemu_wait_io_event(),
+         * until the vcpu is unhalted.
+         */
+        cpu->exception_index = EXCP_HLT;
+        return 0;
+    }
+
     do {
         int hax_ret;
 
-- 
1.8.3.1




  parent reply	other threads:[~2019-06-21 11:38 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-21 11:29 [Qemu-devel] [PULL v2 00/25] Misc (mostly x86) patches for 2019-06-21 Paolo Bonzini
2019-06-21 11:29 ` [Qemu-devel] [PULL 01/25] kvm-all: Add/update fprintf's for kvm_*_ioeventfd_del Paolo Bonzini
2019-06-21 11:29 ` Paolo Bonzini [this message]
2019-06-21 11:29 ` [Qemu-devel] [PULL 03/25] i386/kvm: convert hyperv enlightenments properties from bools to bits Paolo Bonzini
2019-06-24 11:04   ` Alex Bennée
2019-06-24 12:26     ` Vitaly Kuznetsov
2019-06-24 12:43       ` Alex Bennée
2019-06-21 11:29 ` [Qemu-devel] [PULL 04/25] i386/kvm: add support for KVM_GET_SUPPORTED_HV_CPUID Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 05/25] i386/kvm: move Hyper-V CPUID filling to hyperv_handle_properties() Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 06/25] i386/kvm: document existing Hyper-V enlightenments Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 07/25] i386/kvm: implement 'hv-passthrough' mode Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 08/25] i386/kvm: hv-stimer requires hv-time and hv-synic Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 09/25] i386/kvm: hv-tlbflush/ipi require hv-vpindex Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 10/25] i386/kvm: hv-evmcs requires hv-vapic Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 11/25] i386/kvm: add support for Direct Mode for Hyper-V synthetic timers Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 12/25] target/i386: define a new MSR based feature word - FEAT_CORE_CAPABILITY Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 13/25] target/i386: kvm: Delete VMX migration blocker on vCPU init failure Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 14/25] KVM: Introduce kvm_arch_destroy_vcpu() Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 15/25] target/i386: kvm: Use symbolic constant for #DB/#BP exception constants Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 16/25] target/i386: kvm: Re-inject #DB to guest with updated DR6 Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 17/25] target/i386: kvm: Block migration for vCPUs exposed with nested virtualization Paolo Bonzini
2019-07-08 18:21   ` Jan Kiszka
2019-07-08 22:26     ` Liran Alon
2019-06-21 11:30 ` [Qemu-devel] [PULL 18/25] linux-headers: sync with latest KVM headers from Linux 5.2 Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 19/25] vmstate: Add support for kernel integer types Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 20/25] target/i386: kvm: Add support for save and restore nested state Paolo Bonzini
2019-06-21 12:29   ` Liran Alon
2019-06-21 12:45     ` Paolo Bonzini
2019-06-21 12:48       ` Liran Alon
2019-06-21 14:55         ` Paolo Bonzini
2019-06-21 15:00           ` Liran Alon
2019-06-21 15:39             ` Paolo Bonzini
2019-06-21 15:44               ` Liran Alon
2019-06-21 16:01                 ` Liran Alon
2019-06-21 11:30 ` [Qemu-devel] [PULL 21/25] target/i386: kvm: Add support for KVM_CAP_EXCEPTION_PAYLOAD Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 22/25] target/i386: kvm: Add nested migration blocker only when kernel lacks required capabilities Paolo Bonzini
2019-06-21 12:39   ` Liran Alon
2019-06-21 15:02     ` Paolo Bonzini
2019-06-21 15:07       ` Liran Alon
2019-06-21 17:27         ` Paolo Bonzini
2019-06-21 17:54           ` Liran Alon
2019-07-08 18:31   ` Jan Kiszka
2019-07-08 18:51     ` Jan Kiszka
2019-07-10 14:40     ` Paolo Bonzini
2019-07-10 16:08       ` Jan Kiszka
2019-07-10 16:34         ` Paolo Bonzini
2019-07-10 17:15           ` Jan Kiszka
2019-07-10 17:18             ` Jan Kiszka
2019-06-21 11:30 ` [Qemu-devel] [PULL 23/25] sd: Fix out-of-bounds assertions Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 24/25] util/main-loop: Fix incorrect assertion Paolo Bonzini
2019-06-21 11:30 ` [Qemu-devel] [PULL 25/25] hw: Nuke hw_compat_4_0_1 and pc_compat_4_0_1 Paolo Bonzini
2019-06-21 13:53 ` [Qemu-devel] [PULL v2 00/25] Misc (mostly x86) patches for 2019-06-21 no-reply
2019-06-21 14:40 ` Peter Maydell
  -- strict thread matches above, loose matches on Subject: below --
2019-06-21  1:42 [Qemu-devel] [PULL " Paolo Bonzini
2019-06-21  1:42 ` [Qemu-devel] [PULL 02/25] hax: Honor CPUState::halted Paolo Bonzini

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=1561116620-22245-3-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=chuanxiao.dong@intel.com \
    --cc=colin.xu@intel.com \
    --cc=qemu-devel@nongnu.org \
    --cc=yu.ning@intel.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.