From patchwork Tue Oct 9 18:08:48 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andreas_F=C3=A4rber?= X-Patchwork-Id: 1571031 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 6046EDFFF1 for ; Tue, 9 Oct 2012 18:09:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756740Ab2JISJK (ORCPT ); Tue, 9 Oct 2012 14:09:10 -0400 Received: from cantor2.suse.de ([195.135.220.15]:56356 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756718Ab2JISJG (ORCPT ); Tue, 9 Oct 2012 14:09:06 -0400 Received: from relay2.suse.de (unknown [195.135.220.254]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx2.suse.de (Postfix) with ESMTP id 07D00A3DE0; Tue, 9 Oct 2012 20:09:04 +0200 (CEST) From: =?UTF-8?q?Andreas=20F=C3=A4rber?= To: kvm@vger.kernel.org Cc: Marcelo Tosatti , Lai Jiangshan , Avi Kivity , =?UTF-8?q?Andreas=20F=C3=A4rber?= Subject: [PATCH stable-0.15 1/6] qemu-kvm: fix improper nmi emulation Date: Tue, 9 Oct 2012 20:08:48 +0200 Message-Id: <1349806133-1916-2-git-send-email-afaerber@suse.de> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1349806133-1916-1-git-send-email-afaerber@suse.de> References: <1349806133-1916-1-git-send-email-afaerber@suse.de> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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. fix the race found by Jan. Signed-off-by: Lai Jiangshan Reported-by: Kenji Kaneshige Signed-off-by: Avi Kivity (cherry picked from commit 67feec6ed854b3618b37ccf050b90192cbb96e0f) Signed-off-by: Andreas Färber --- hw/apic.c | 33 +++++++++++++++++++++++++++++++++ hw/apic.h | 1 + monitor.c | 6 +++++- 3 Dateien geändert, 39 Zeilen hinzugefügt(+), 1 Zeile entfernt(-) diff --git a/hw/apic.c b/hw/apic.c index a45b57f..243900d 100644 --- a/hw/apic.c +++ b/hw/apic.c @@ -204,6 +204,39 @@ void apic_deliver_pic_intr(DeviceState *d, int level) } } +static inline uint32_t kapic_reg(struct kvm_lapic_state *kapic, int reg_id); + +static void kvm_irqchip_deliver_nmi(void *p) +{ + APICState *s = p; + struct kvm_lapic_state klapic; + uint32_t lvt; + + kvm_get_lapic(s->cpu_env, &klapic); + lvt = kapic_reg(&klapic, 0x32 + APIC_LVT_LINT1); + + if (lvt & APIC_LVT_MASKED) { + return; + } + + if (((lvt >> 8) & 7) != APIC_DM_NMI) { + return; + } + + kvm_vcpu_ioctl(s->cpu_env, KVM_NMI); +} + +void apic_deliver_nmi(DeviceState *d) +{ + APICState *s = DO_UPCAST(APICState, busdev.qdev, d); + + if (kvm_irqchip_in_kernel()) { + run_on_cpu(s->cpu_env, kvm_irqchip_deliver_nmi, s); + } else { + apic_local_deliver(s, APIC_LVT_LINT1); + } +} + #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 7680929..6af0673 100644 --- a/monitor.c +++ b/monitor.c @@ -2604,7 +2604,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;