From patchwork Fri Oct 14 06:36:12 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lai Jiangshan X-Patchwork-Id: 119714 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 648FAB6F8E for ; Fri, 14 Oct 2011 17:35:10 +1100 (EST) Received: from localhost ([::1]:50307 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1REbMH-0000j6-PH for incoming@patchwork.ozlabs.org; Fri, 14 Oct 2011 02:35:05 -0400 Received: from eggs.gnu.org ([140.186.70.92]:38469) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1REbMC-0000iX-8E for qemu-devel@nongnu.org; Fri, 14 Oct 2011 02:35:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1REbMB-0001eJ-0H for qemu-devel@nongnu.org; Fri, 14 Oct 2011 02:35:00 -0400 Received: from [222.73.24.84] (port=61566 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1REbM8-0001dy-Kg for qemu-devel@nongnu.org; Fri, 14 Oct 2011 02:34:58 -0400 Received: from tang.cn.fujitsu.com (tang.cn.fujitsu.com [10.167.250.3]) by song.cn.fujitsu.com (Postfix) with ESMTP id 07BDB1700BD; Fri, 14 Oct 2011 14:34:50 +0800 (CST) Received: from mailserver.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id p9E6YlXa011908; Fri, 14 Oct 2011 14:34:47 +0800 Received: from lai.fc14.fnst ([10.167.225.146]) by mailserver.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.1FP4) with ESMTP id 2011101414330483-439718 ; Fri, 14 Oct 2011 14:33:04 +0800 Message-ID: <4E97D85C.7070107@cn.fujitsu.com> Date: Fri, 14 Oct 2011 14:36:12 +0800 From: Lai Jiangshan User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.9) Gecko/20100921 Fedora/3.1.4-1.fc14 Thunderbird/3.1.4 MIME-Version: 1.0 To: Jan Kiszka References: <20110913093835.GB4265@localhost.localdomain> <20110914093441.e2bb305c.kamezawa.hiroyu@jp.fujitsu.com> <4E705BC3.5000508@cn.fujitsu.com> <20110915164704.9cacd407.kamezawa.hiroyu@jp.fujitsu.com> <4E71B28F.7030201@cn.fujitsu.com> <4E72F3BA.2000603@jp.fujitsu.com> <4E73200A.7040908@jp.fujitsu.com> <4E76C6AA.9080403@cn.fujitsu.com> <4E7B04DC.1030407@cn.fujitsu.com> <4E7B4B8F.507@siemens.com> <4E7C51E4.2000503@cn.fujitsu.com> <4E7F3585.40108@redhat.com> <4E7F635E.6080009@web.de> <4E8035F9.9080908@redhat.com> <4E928B54.1070707@cn.fujitsu.com> <4E92958E.9000509@web.de> <4E9476E2.1070804@cn.fujitsu.com> <4E948842.4030406@web.de> <4E978827.6070008@cn.fujitsu.com> <4E97CE42.9020102@web.de> In-Reply-To: <4E97CE42.9020102@web.de> X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.1FP4|July 25, 2010) at 2011-10-14 14:33:04, Serialize by Router on mailserver/fnst(Release 8.5.1FP4|July 25, 2010) at 2011-10-14 14:33:06, Serialize complete at 2011-10-14 14:33:06 X-detected-operating-system: by eggs.gnu.org: FreeBSD 6.x (1) X-Received-From: 222.73.24.84 Cc: "kvm@vger.kernel.org" , Avi Kivity , "qemu-devel@nongnu.org" , KAMEZAWA Hiroyuki , Kenji Kaneshige Subject: [Qemu-devel] [PATCH 1/1 V4] qemu-kvm: fix improper nmi emulation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org On 10/14/2011 01:53 PM, Jan Kiszka wrote: > On 2011-10-14 02:53, Lai Jiangshan wrote: >> >>> >>> As explained in some other mail, we could then emulate the missing >>> kernel feature by reading out the current in-kernel APIC state, testing >>> if LINT1 is unmasked, and then delivering the NMI directly. >>> >> >> Only the thread of the VCPU can safely get the in-kernel LAPIC states, >> so this approach will cause some troubles. > > run_on_cpu() can help. > > Jan > Ah, I forgot it, Thanks. From: Lai Jiangshan Currently, NMI interrupt is blindly sent to all the vCPUs when NMI button event happens. This doesn't properly emulate real hardware on which NMI button event triggers LINT1. Because of this, NMI is sent to the processor even when LINT1 is maskied in LVT. For example, this causes the problem that kdump initiated by NMI sometimes doesn't work on KVM, because kdump assumes NMI is masked on CPUs other than CPU0. With this patch, inject-nmi request is handled as follows. - When in-kernel irqchip is disabled, deliver LINT1 instead of NMI interrupt. - When in-kernel irqchip is enabled, get the in-kernel LAPIC states and test the APIC_LVT_MASKED, if LINT1 is unmasked, and then delivering the NMI directly. (Suggested by Jan Kiszka) Changed from old version: re-implement it by the Jan's suggestion. Signed-off-by: Lai Jiangshan Reported-by: Kenji Kaneshige --- hw/apic.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/apic.h | 1 + monitor.c | 6 +++++- 3 files changed, 54 insertions(+), 1 deletions(-) diff --git a/hw/apic.c b/hw/apic.c index 69d6ac5..9a40129 100644 --- a/hw/apic.c +++ b/hw/apic.c @@ -205,6 +205,54 @@ void apic_deliver_pic_intr(DeviceState *d, int level) } } +#ifdef KVM_CAP_IRQCHIP +static inline uint32_t kapic_reg(struct kvm_lapic_state *kapic, int reg_id); + +struct kvm_get_remote_lapic_params { + CPUState *env; + struct kvm_lapic_state klapic; +}; + +static void kvm_get_remote_lapic(void *p) +{ + struct kvm_get_remote_lapic_params *params = p; + + kvm_get_lapic(params->env, ¶ms->klapic); +} + +void apic_deliver_nmi(DeviceState *d) +{ + APICState *s = DO_UPCAST(APICState, busdev.qdev, d); + + if (kvm_irqchip_in_kernel()) { + struct kvm_get_remote_lapic_params p = {.env = s->cpu_env,}; + uint32_t lvt; + + run_on_cpu(s->cpu_env, kvm_get_remote_lapic, &p); + lvt = kapic_reg(&p.klapic, 0x32 + APIC_LVT_LINT1); + + if (lvt & APIC_LVT_MASKED) { + return; + } + + if (((lvt >> 8) & 7) != APIC_DM_NMI) { + return; + } + + cpu_interrupt(s->cpu_env, CPU_INTERRUPT_NMI); + } else { + apic_local_deliver(s, APIC_LVT_LINT1); + } +} +#else +void apic_deliver_nmi(DeviceState *d) +{ + APICState *s = DO_UPCAST(APICState, busdev.qdev, d); + + apic_local_deliver(s, APIC_LVT_LINT1); +} +#endif + #define foreach_apic(apic, deliver_bitmask, code) \ {\ int __i, __j, __mask;\ diff --git a/hw/apic.h b/hw/apic.h index c857d52..3a4be0a 100644 --- a/hw/apic.h +++ b/hw/apic.h @@ -10,6 +10,7 @@ void apic_deliver_irq(uint8_t dest, uint8_t dest_mode, uint8_t trigger_mode); int apic_accept_pic_intr(DeviceState *s); void apic_deliver_pic_intr(DeviceState *s, int level); +void apic_deliver_nmi(DeviceState *d); int apic_get_interrupt(DeviceState *s); void apic_reset_irq_delivered(void); int apic_get_irq_delivered(void); diff --git a/monitor.c b/monitor.c index cb485bf..0b81f17 100644 --- a/monitor.c +++ b/monitor.c @@ -2616,7 +2616,11 @@ static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data) CPUState *env; for (env = first_cpu; env != NULL; env = env->next_cpu) { - cpu_interrupt(env, CPU_INTERRUPT_NMI); + if (!env->apic_state) { + cpu_interrupt(env, CPU_INTERRUPT_NMI); + } else { + apic_deliver_nmi(env->apic_state); + } } return 0;