From patchwork Tue Oct 25 12:42:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 13019165 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F77AECDFA1 for ; Tue, 25 Oct 2022 12:42:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232041AbiJYMmq (ORCPT ); Tue, 25 Oct 2022 08:42:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232151AbiJYMmg (ORCPT ); Tue, 25 Oct 2022 08:42:36 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A84C2189C3F for ; Tue, 25 Oct 2022 05:42:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666701753; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=YKqk8JFJPFyZT4uxuIdJDMrDXX3QslvogL56oEpKGVE=; b=RbktoLaAc31iTH8BLWmWEFcnApgnO2bogzaVyD04rvsXObL76g+TtTpM5cPT6vqF+esE+Z cv2F9zQQoQ84sfpmrZddfHDO5kLG1vCXXmCCk5Q7Go8ut+rRbziwf89LgqBlsATKbCsBKA 6Dh4a7tPuitaDb8e0G8rb9mmMmWY7QI= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-341-73h3obR2O_m7aheX040YGA-1; Tue, 25 Oct 2022 08:42:30 -0400 X-MC-Unique: 73h3obR2O_m7aheX040YGA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E26DC3C0D84F; Tue, 25 Oct 2022 12:42:27 +0000 (UTC) Received: from amdlaptop.tlv.redhat.com (dhcp-4-238.tlv.redhat.com [10.35.4.238]) by smtp.corp.redhat.com (Postfix) with ESMTP id 428352024CCA; Tue, 25 Oct 2022 12:42:24 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: Paolo Bonzini , Yang Zhong , linux-kselftest@vger.kernel.org, Kees Cook , Borislav Petkov , Guang Zeng , Wanpeng Li , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Maxim Levitsky , Joerg Roedel , linux-kernel@vger.kernel.org, Wei Wang , Jim Mattson , Dave Hansen , Sean Christopherson , Vitaly Kuznetsov , x86@kernel.org, Shuah Khan Subject: [PATCH v4 00/23] SMM emulation and interrupt shadow fixes Date: Tue, 25 Oct 2022 15:42:00 +0300 Message-Id: <20221025124223.227577-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This patch series is a result of long debug work to find out why sometimes guests with win11 secure boot were failing during boot. During writing a unit test I found another bug, turns out that on rsm emulation, if the rsm instruction was done in real or 32 bit mode, KVM would truncate the restored RIP to 32 bit. I also refactored the way we write SMRAM so it is easier now to understand what is going on. The main bug in this series which I fixed is that we allowed #SMI to happen during the STI interrupt shadow, and we did nothing to both reset it on #SMI handler entry and restore it on RSM. V4: - rebased on top of patch series from Paolo which allows smm support to be disabled by Kconfig option. - addressed review feedback. I included these patches in the series for reference. Best regards, Maxim Levitsky Maxim Levitsky (15): bug: introduce ASSERT_STRUCT_OFFSET KVM: x86: emulator: em_sysexit should update ctxt->mode KVM: x86: emulator: introduce emulator_recalc_and_set_mode KVM: x86: emulator: update the emulation mode after rsm KVM: x86: emulator: update the emulation mode after CR0 write KVM: x86: smm: number of GPRs in the SMRAM image depends on the image format KVM: x86: smm: check for failures on smm entry KVM: x86: smm: add structs for KVM's smram layout KVM: x86: smm: use smram structs in the common code KVM: x86: smm: use smram struct for 32 bit smram load/restore KVM: x86: smm: use smram struct for 64 bit smram load/restore KVM: svm: drop explicit return value of kvm_vcpu_map KVM: x86: SVM: use smram structs KVM: x86: SVM: don't save SVM state to SMRAM when VM is not long mode capable KVM: x86: smm: preserve interrupt shadow in SMRAM Paolo Bonzini (8): KVM: x86: start moving SMM-related functions to new files KVM: x86: move SMM entry to a new file KVM: x86: move SMM exit to a new file KVM: x86: do not go through ctxt->ops when emulating rsm KVM: allow compiling out SMM support KVM: x86: compile out vendor-specific code if SMM is disabled KVM: x86: remove SMRAM address space if SMM is not supported KVM: x86: do not define KVM_REQ_SMI if SMM disabled arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 29 +- arch/x86/kvm/Kconfig | 11 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/emulate.c | 458 +++---------- arch/x86/kvm/kvm_cache_regs.h | 5 - arch/x86/kvm/kvm_emulate.h | 47 +- arch/x86/kvm/lapic.c | 14 +- arch/x86/kvm/lapic.h | 7 +- arch/x86/kvm/mmu/mmu.c | 1 + arch/x86/kvm/smm.c | 637 ++++++++++++++++++ arch/x86/kvm/smm.h | 160 +++++ arch/x86/kvm/svm/nested.c | 3 + arch/x86/kvm/svm/svm.c | 43 +- arch/x86/kvm/vmx/nested.c | 1 + arch/x86/kvm/vmx/vmcs12.h | 5 +- arch/x86/kvm/vmx/vmx.c | 11 +- arch/x86/kvm/x86.c | 353 +--------- include/linux/build_bug.h | 9 + tools/testing/selftests/kvm/x86_64/smm_test.c | 2 + 20 files changed, 1031 insertions(+), 768 deletions(-) create mode 100644 arch/x86/kvm/smm.c create mode 100644 arch/x86/kvm/smm.h