From patchwork Wed Apr 24 22:20:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 10915823 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5AA261708 for ; Wed, 24 Apr 2019 22:24:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4B1A228BE1 for ; Wed, 24 Apr 2019 22:24:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3DB4B28BE8; Wed, 24 Apr 2019 22:24:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9F82D28BE1 for ; Wed, 24 Apr 2019 22:24:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729435AbfDXWXh (ORCPT ); Wed, 24 Apr 2019 18:23:37 -0400 Received: from mga12.intel.com ([192.55.52.136]:32374 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726184AbfDXWXh (ORCPT ); Wed, 24 Apr 2019 18:23:37 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Apr 2019 15:20:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,391,1549958400"; d="scan'208";a="145460537" Received: from sjchrist-coffee.jf.intel.com ([10.54.74.181]) by orsmga003.jf.intel.com with ESMTP; 24 Apr 2019 15:20:33 -0700 From: Sean Christopherson To: Paolo Bonzini , =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= Cc: kvm@vger.kernel.org, Nadav Amit Subject: [kvm-unit-tests PATCH] x86: APIC: Add test for pending NMIs while NMIs are blocked Date: Wed, 24 Apr 2019 15:20:32 -0700 Message-Id: <20190424222032.26437-1-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Though explicit documentation is difficult to unearth, x86 guarantees that exactly one NMI will be pended when NMIs are blocked. The SDM essentially calls this out in its section on handling NMIs in SMM: NMI interrupts are blocked upon entry to the SMI handler. If an NMI request occurs during the SMI handler, it is latched and serviced after the processor exits SMM. Only one NMI request will be latched during the SMI handler. Add a test to send multiple NMIs from within an NMI handler to verify that KVM correctly pends exactly *one* NMI when NMIs are blocked. Cc: Nadav Amit Signed-off-by: Sean Christopherson --- x86/apic.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/x86/apic.c b/x86/apic.c index 51744cf..9b78288 100644 --- a/x86/apic.c +++ b/x86/apic.c @@ -403,6 +403,34 @@ static void test_multiple_nmi(void) report("multiple nmi", ok); } +static void pending_nmi_handler(isr_regs_t *regs) +{ + int i; + + if (++nmi_received == 1) { + for (i = 0; i < 10; ++i) + apic_icr_write(APIC_DEST_PHYSICAL | APIC_DM_NMI, 0); + } +} + +static void test_pending_nmi(void) +{ + int i; + + handle_irq(2, pending_nmi_handler); + for (i = 0; i < 100000; ++i) { + nmi_received = 0; + + apic_icr_write(APIC_DEST_PHYSICAL | APIC_DM_NMI, 0); + while (nmi_received < 2) + pause(); + + if (nmi_received != 2) + break; + } + report("pending nmi", nmi_received == 2); +} + static volatile int lvtt_counter = 0; static void lvtt_handler(isr_regs_t *regs) @@ -615,6 +643,7 @@ int main(void) test_sti_nmi(); test_multiple_nmi(); + test_pending_nmi(); test_apic_timer_one_shot(); test_apic_change_mode();