From patchwork Tue Jul 13 16:32:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374521 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04FC7C11F66 for ; Tue, 13 Jul 2021 16:33:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DE4F8611AC for ; Tue, 13 Jul 2021 16:33:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231931AbhGMQg2 (ORCPT ); Tue, 13 Jul 2021 12:36:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231802AbhGMQg1 (ORCPT ); Tue, 13 Jul 2021 12:36:27 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58004C0613E9 for ; Tue, 13 Jul 2021 09:33:37 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id bm22-20020a05620a1996b02903b87dad5a1dso4009873qkb.23 for ; Tue, 13 Jul 2021 09:33:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc:content-transfer-encoding; bh=yeyM8miFXVfhYCiRacpfh4x5/Y1VXoOxcgLbMzodMTs=; b=gmkIzyulalQgm0iUkw5EyMI4acmPrftCi/JxE0avsh261tlyjOsTc8y3eCFLK4U2xj 4kMtA7UeZ8G5aJwgY7KEbGInJGWrQqEEns3qJzuxuW1KoLj+KPqVhRXa0Mt3TrLYfPOw bPgGWsWO3VwZM0GLigsA92jIZERVIPFwD20MS1hpLdwc/3nHnwpHDXzLMwM+fIw8XtM4 vSwAXaSy0sE8JwcIfU4EsYGfBiRWfzfaM/qXn3KuCFDZIuneUXZkhevPebN5elEwjjry xtDF80ue9AK2pfDYCLUCGxX5PzK2swNv30wmbASC+CHfCAkc+LG0Cngbqzk6ruE/8OgZ IZIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc :content-transfer-encoding; bh=yeyM8miFXVfhYCiRacpfh4x5/Y1VXoOxcgLbMzodMTs=; b=WrBWDQVUDodjH0f6XIm7XcoeOsQYQ8AKU/chiBCUw3+QCafS4z+4zzA31MS6ci/StX pqjakSYEhBDYzzb5Q3XZ+7HBaGmI9Rfu0k5PGmsQiAWWFQoi/babX6l7RV0VfJ7sZJsE uDQ4hAoywBxZ3eQHVx4ISklbyXfd3waEF7mAk5Qx2f2F+Y0n1D/RwDi7/ZICnZkGl9Cp Ml0fGebXdWqFBMu2CWf6aHc4LygcvrKWv1tZAuAaDXiN8KIaVEgn9qI+AGqNZFlj/xnc 3ReajlFIO0K3JEdVEBMLB++js8636bFq5aMufGj8/qL5iOrXv3ski5EA9pufBuSGtDy6 PdiQ== X-Gm-Message-State: AOAM530PAjZIyZpWeHvxZm7qrwY4eEvLJZIy974JR1FWVNH9DuXNL4vo PIHXrcGb0zRSy4loet+f+WIVsA3TO3U= X-Google-Smtp-Source: ABdhPJzNv3IrIvUUMO8p+vTbbzvg8/gZPxZ+5gcJjFCq3w/wqMdzu0+2wJRWpZC/wQLsNMZ8ef8ajgs/xvE= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a0c:b44b:: with SMTP id e11mr5841955qvf.38.1626194016387; Tue, 13 Jul 2021 09:33:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:39 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-2-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 01/46] KVM: x86: Flush the guest's TLB on INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Flush the guest's TLB on INIT, as required by Intel's SDM. Although AMD's APM states that the TLBs are unchanged by INIT, it's not clear that that's correct as the APM also states that the TLB is flush on "External initialization of the processor." Regardless, relying on the guest to be paranoid is unnecessarily risky, while an unnecessary flush is benign from a functional perspective and likely has no measurable impact on guest performance. Note, as of the April 2021 version of Intels' SDM, it also contradicts itself with respect to TLB flushing. The overview of INIT explicitly calls out the TLBs as being invalidated, while a table later in the same section says they are unchanged. 9.1 INITIALIZATION OVERVIEW: The major difference is that during an INIT, the internal caches, MSRs, MTRRs, and x87 FPU state are left unchanged (although, the TLBs and BTB are invalidated as with a hardware reset) Table 9-1: Register Power up Reset INIT Data and Code Cache, TLBs: Invalid[6] Invalid[6] Unchanged Given Core2's erratum[*] about global TLB entries not being flush on INIT, it's safe to assume that the table is simply wrong. AZ28. INIT Does Not Clear Global Entries in the TLB Problem: INIT may not flush a TLB entry when: • The processor is in protected mode with paging enabled and the page global enable flag is set (PGE bit of CR4 register) • G bit for the page table entry is set • TLB entry is present in TLB when INIT occurs • Software may encounter unexpected page fault or incorrect address translation due to a TLB entry erroneously left in TLB after INIT. Workaround: Write to CR3, CR4 (setting bits PSE, PGE or PAE) or CR0 (setting bits PG or PE) registers before writing to memory early in BIOS code to clear all the global entries from TLB. Status: For the steppings affected, see the Summary Tables of Changes. [*] https://www.intel.com/content/dam/support/us/en/documents/processors/mobile/celeron/sb/320121.pdf Fixes: 6aa8b732ca01 ("[PATCH] kvm: userspace interface") Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 8166ad113fb2..4ffc4ca7d7b0 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10867,6 +10867,18 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) */ if (old_cr0 & X86_CR0_PG) kvm_mmu_reset_context(vcpu); + + /* + * Intel's SDM states that all TLB entries are flushed on INIT. AMD's + * APM states the TLBs are untouched by INIT, but it also states that + * the TLBs are flushed on "External initialization of the processor." + * Flush the guest TLB regardless of vendor, there is no meaningful + * benefit in relying on the guest to flush the TLB immediately after + * INIT. A spurious TLB flush is benign and likely negligible from a + * performance perspective. + */ + if (init_event) + kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); } void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) From patchwork Tue Jul 13 16:32:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374523 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71E49C07E96 for ; Tue, 13 Jul 2021 16:33:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 59DD2611AC for ; Tue, 13 Jul 2021 16:33:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233067AbhGMQgc (ORCPT ); Tue, 13 Jul 2021 12:36:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232852AbhGMQga (ORCPT ); Tue, 13 Jul 2021 12:36:30 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E7BAC0613DD for ; Tue, 13 Jul 2021 09:33:39 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id j186-20020a25d2c30000b029055ed6ffbea6so20159660ybg.14 for ; Tue, 13 Jul 2021 09:33:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=CGUCznRdiPWAmsCMQFonxOBex98YQEwxWBKOh5eQt7M=; b=tJqwt8bZlc8oPW/4EXV9GTQdpI1HLnBQftLNu4By8/T3ToR8ArcbB9DvXcDFFNlyEw 8WGUYedXdNMVnobO4pNadTycYVgvAqx+pU9fBKQwrveviacCgiUe18n1XQTtJECgVnSD nEmhRfmToc4gQ2EiDKpiUBqatrjl0o4v41mw6KOFeh08WRlBzWHqupDweD875Nm3LzCa SHBnz5mw89ezDDv0lxIOD1gXHU1dathcQwWezPxEEq5rSx9TWP18aTDg5gV02+V7DOde hHUG0LVjlHoKSJZ0QcOAIvzh0z4EjlVlPWvYe3LvuMPBKNJZGEmTikF22RI9jVnxvkDq CIDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=CGUCznRdiPWAmsCMQFonxOBex98YQEwxWBKOh5eQt7M=; b=PmycVH6jqaXpXXfSxqJSuwVAxL5tQwIK7v0lfWyHZy1GxZ180cfmv+nA2LneD62v7y P1Qhpy7f54208RRceWpvw3biYhQBJLLTO0c3SGwDGhwgVPSVkzM81g/e4WWqtWhurq2e xB5/ypEV60mqIcQ9ZP5tJMEfDXrIkfWSd68Hi2ybl0939wpLOKAaRuUds4ptW/eHk4gj Gkxp55lngxuzW+rfY9WuTgpCpq/nZXl4WtUUoptDzMwsPRIu1smg74MQVhEk4OBZekOq ZRzaJS5DXpHlly7lPX9IQ/J7MCSC0IMjmTwNRDpCWfDx4hWTqsSd5gGngnBd6cb6yWtQ BOCQ== X-Gm-Message-State: AOAM531k3eLKvFC2NKdJV8L3/jqLkji7LAptq5Vp/Bfx9x7l0w+7dx2v d2yfBVUJA1wOOuz7uBTwz0reJY+nzOE= X-Google-Smtp-Source: ABdhPJwCVfYAbbbuA0so14IArA0SM/M/g2EbwalCLw55NEUbXBxsGXZnNsPeOTln8DYQHm8nyn9QAMQ9+Lg= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:ab41:: with SMTP id u59mr7079483ybi.218.1626194018438; Tue, 13 Jul 2021 09:33:38 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:40 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-3-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 02/46] KVM: nVMX: Set LDTR to its architecturally defined value on nested VM-Exit From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Set L1's LDTR on VM-Exit per the Intel SDM: The host-state area does not contain a selector field for LDTR. LDTR is established as follows on all VM exits: the selector is cleared to 0000H, the segment is marked unusable and is otherwise undefined (although the base address is always canonical). This is likely a benign bug since the LDTR is unusable, as it means the L1 VMM is conditioned to reload its LDTR in order to function properly on bare metal. Fixes: 4704d0befb07 ("KVM: nVMX: Exiting from L2 to L1") Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 1a52134b0c42..7f8184f432b4 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4298,6 +4298,10 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, }; vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); + memset(&seg, 0, sizeof(seg)); + seg.unusable = 1; + vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR); + kvm_set_dr(vcpu, 7, 0x400); vmcs_write64(GUEST_IA32_DEBUGCTL, 0); From patchwork Tue Jul 13 16:32:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374527 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4889C11F67 for ; Tue, 13 Jul 2021 16:33:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AEF4B611AC for ; Tue, 13 Jul 2021 16:33:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233182AbhGMQge (ORCPT ); Tue, 13 Jul 2021 12:36:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232911AbhGMQgc (ORCPT ); Tue, 13 Jul 2021 12:36:32 -0400 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F176C061786 for ; Tue, 13 Jul 2021 09:33:41 -0700 (PDT) Received: by mail-qt1-x84a.google.com with SMTP id d11-20020ac851cb0000b02902536d2bea0fso7922494qtn.19 for ; Tue, 13 Jul 2021 09:33:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=mmLJtuxuRzAHPf02CnAQZUwvZOlodw4FBAs7yXac4yw=; b=ackNT3Xf2TczdqhxE47QzxoI8NtDGT1V5X8X1lHIXIgZPFBOiSDIuRAdwH9W955FJR ZIEG6OvRQjTHnrJWBqAujpUp7JbA5vwpo3wkFslmC0pJE8AMp56UEiBNMOrCA5bTwdTg 5EHVEyXFS86ovbnJWweyorLcrMIcWJZWlLR3aBuh/tJi7K2LcHi3pnXUz0OW4aaZBtym enEme6MgjIe6hv2yBvojOXwfBmkNIjbYlKQA+xru3klsjLN54FATOWxqm/jaYs2imICI bpfi9349CJR7aIpe/xjo+UTaMDvYX8rxcVeIm9FPyTXD2dJaIqxmwv2aTpa1gcjRWp6m S9cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=mmLJtuxuRzAHPf02CnAQZUwvZOlodw4FBAs7yXac4yw=; b=HcHX+D/V0LFcM08A7182uJ2SWAQNxSMKLvEDEMq9SKo8UdWfHCohKyCzJYS8hsxd7A ADtAXbTYbN3kKbQbKTKwvqPQaqYwwBhrS39ctdCkLJVxG+1bT9vqZnVDU7d2tWbZEvKX X2KN0J6TAE31HRB5D4kexaNomUp+7NY2dVFv0GfpDK7jAPRMVfSOyL4ovuw0z0FYX43w gCLUR9sCcLuHdWqeqwl+oAAkHMFlb0z5AHql9Z9ONEnR23f2jgFMbcG1JTbpT7SZB1cz wyPpzpEz7U+zvWeMBisBslgcgXQnF/ohcGbSZRMY99oLSjOO8DlGu69E2Uz+BJiANUUZ wlUw== X-Gm-Message-State: AOAM531F+x08XCSu8qcqel+TuFlqWmACDIVAbeqEABveYT0E8HfEbjA4 MDwK0cyF4vRc5SQj2WfQD4GGeuASn70= X-Google-Smtp-Source: ABdhPJyyd6AjY4T8NXHb5A8b1AAJZuH1WT8RrMrrbuRr/MnPYhCDMdm2EiTY7UF5eWNcJ+wTouQaor6XU18= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a05:6214:ca1:: with SMTP id s1mr5765426qvs.43.1626194020169; Tue, 13 Jul 2021 09:33:40 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:41 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-4-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 03/46] KVM: SVM: Zero out GDTR.base and IDTR.base on INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Explicitly set GDTR.base and IDTR.base to zero when intializing the VMCB. Functionally this only affects INIT, as the bases are implicitly set to zero on RESET by virtue of the VMCB being zero allocated. Per AMD's APM, GDTR.base and IDTR.base are zeroed after RESET and INIT. Fixes: 04d2cc7780d4 ("KVM: Move main vcpu loop into subarch independent code") Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 616b9679ddcc..2150642e1bef 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1254,7 +1254,9 @@ static void init_vmcb(struct kvm_vcpu *vcpu) SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; save->cs.limit = 0xffff; + save->gdtr.base = 0; save->gdtr.limit = 0xffff; + save->idtr.base = 0; save->idtr.limit = 0xffff; init_sys_seg(&save->ldtr, SEG_TYPE_LDT); From patchwork Tue Jul 13 16:32:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374525 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C75BFC07E96 for ; Tue, 13 Jul 2021 16:33:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B15236128E for ; Tue, 13 Jul 2021 16:33:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233294AbhGMQgf (ORCPT ); Tue, 13 Jul 2021 12:36:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232250AbhGMQgd (ORCPT ); Tue, 13 Jul 2021 12:36:33 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBA76C0613DD for ; Tue, 13 Jul 2021 09:33:42 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id g3-20020a256b030000b0290551bbd99700so27767466ybc.6 for ; Tue, 13 Jul 2021 09:33:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=qVAaYnz6pX9NpBAvuefpA8nWzJech9ezcBMh3QJZR+s=; b=OR7dSXpwFlC+X0W7OqUvl4kIpeBJF+qjuDAJSMu2iAijKG2W0OPADSYrJ2Wlhv48mZ fgMSwugGchIv/CKKT4x2ilhtNc+qnQcrk8hQjzYDiR7aefplm7QDZGvv2zo6cVjGOjkH LJFpRk2h0pTzxk4r4bO5Hx8r+ZbiIfNVl8V4MmUGHxgwRHLeiFFNp0AWc0xE5YRzqf8Z 7MBdicd6KCU7NsaXNX9b173HANfizgOfHUWoNINcFEf7t5MwiKgUIAfFjddfkZKfGsZZ S2+xwdBV4MhtbNLKwVLAB74Mx6z2AoDjZr7nxIspW7L3xOtOosiEdu3KZx0hn8gDmtUs HB4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=qVAaYnz6pX9NpBAvuefpA8nWzJech9ezcBMh3QJZR+s=; b=QYqouLJ1jwO11ud9EXK+RoOgQPuNPND3s+gxTnrTFGkeeA9nhqxWq2h1L71WvUqV+P eX4TGwyBovZJBYpFVzrK6GcSSiSp/5iJVTIR6sf5g34AIdXNkVPtDJ9PbWPkaFl/KJag UX/E7eZJ+jHIKiqEXZywtlBK7RiNLuorWbBq3+uGQAk7QlhpaBrsP7wlzlY3tz2RpPPV Xv9sAZPgTtRfNd7f8C1LCowGxCkhLja3S747+g2ksASBeKRHIVqBLHGNwDO+BN1FXcJk Xe2wMc/SR6qAoxrKQyJHRojbRSN4qDJOMfQ8DggyMv1E+xDUAF0mPbO5eOOHC2Z6h8C8 ev8Q== X-Gm-Message-State: AOAM5335+4i8MPaDBL0BlmghJNYF1qMjlzu0j72MjO37/nkIgKPzKLFn wPCvtHRIH3GbYBoE8gl3Zjjgc5JbMDY= X-Google-Smtp-Source: ABdhPJyiyPuUnBaRxZJxtDyjf+Sb+7Fe/yKqvjWGeg/m1vbZgaG2C17q3843vEj6qP4u2s+znFWM0FGOyAg= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:888b:: with SMTP id d11mr7526792ybl.385.1626194022140; Tue, 13 Jul 2021 09:33:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:42 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-5-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 04/46] KVM: VMX: Set EDX at INIT with CPUID.0x1, Family-Model-Stepping From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Set EDX at RESET/INIT based on the userspace-defined CPUID model when possible, i.e. when CPUID.0x1.EAX is defind by userspace. At RESET/INIT, all CPUs that support CPUID set EDX to the FMS enumerated in CPUID.0x1.EAX. If no CPUID match is found, fall back to KVM's default of 0x600 (Family '6'), which is the least awful approximation of KVM's virtual CPU model. Fixes: 6aa8b732ca01 ("[PATCH] kvm: userspace interface") Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 927a552393b9..825197f21700 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4394,6 +4394,7 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) { struct vcpu_vmx *vmx = to_vmx(vcpu); struct msr_data apic_base_msr; + u32 eax, dummy; u64 cr0; vmx->rmode.vm86_active = 0; @@ -4401,7 +4402,11 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vmx->msr_ia32_umwait_control = 0; - vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); + eax = 1; + if (!kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true)) + eax = get_rdx_init_val(); + kvm_rdx_write(vcpu, eax); + vmx->hv_deadline_tsc = -1; kvm_set_cr8(vcpu, 0); From patchwork Tue Jul 13 16:32:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374529 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 856EBC07E96 for ; Tue, 13 Jul 2021 16:33:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6E6136128E for ; Tue, 13 Jul 2021 16:33:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233371AbhGMQgh (ORCPT ); Tue, 13 Jul 2021 12:36:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233268AbhGMQgf (ORCPT ); Tue, 13 Jul 2021 12:36:35 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD990C0613EE for ; Tue, 13 Jul 2021 09:33:44 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id x84-20020a2531570000b029055d47682463so26320035ybx.5 for ; Tue, 13 Jul 2021 09:33:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=AGAjMyxP8Kz+nybtWUECIkFt/HGEOoMESc1mkCwzAiw=; b=UCQx6DBF7jZuuIBk/LPOaIgpig7BPufPBP1HccKEpPWjOVUUq2+vOZCpyk45K55qfp IkuPgFkVYU4NmkVbDXfrYp/H5AaJ7Z+DcRzUrLSCLZfOnxLnPUj08XVdXKqz8sAuYuuf Q8/+nsvzS7H9XvNpXs5ViM+41tnfVgbpU7l+P7tno+mb1Uo26SRvha9xzM2KvWSW5L8C ndiUikKd7/v8Flub24h2PE485cDvfS5vRnYIzDM16YMR/fFrJlaGPWFStEhUGkvp1HUh xAPbEHG9+4CH0XASSTBS4FdMeBpMtV/mPuAaWrm4G/rUytfIQOlUEle9k2NlpI/ojg0A U70g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=AGAjMyxP8Kz+nybtWUECIkFt/HGEOoMESc1mkCwzAiw=; b=XbY0YBffQoJQ7s7iLc9UWAi0idvMEVMOZ3LL3ZPl3rxyDfKZU4kxCWOiDDY9Ln/jFR bkIaSkaqUXQIucZUDG4CKkF7Cyhle3uaXqrSOTdM6GKFcblQUqdjJT9QFbM+2bbj9QTp RK84aucnI5Q1fFMEeTVeaCIfKD++Wwc7CIxSNpOddvazk3vuKyUvsSieWD0sBFsH1V/6 OanY3viH1ENhAja4chLpTzhCaqOShw2/E0HKI/2gHsjNXe8jzgVbFswv8PjsRmQZwsL8 vXvSQa1OfR3r5Do5l92wxc5W17WcZKL8bMfJrPfznDyFS5E4we+JsRGsNWdtPoHtYnju D3LA== X-Gm-Message-State: AOAM532TQSURsg7bWyl8x4QS5MprHt8CddDGWlhNkfKi/v5vOkyd4IoS e8i/OzIOPudlgrCzyMV2+lI3YmwoRPM= X-Google-Smtp-Source: ABdhPJyfcF3/EzRv4tHswNTrHoKijaTLQxcVLSWE/9QuBQ3MsAO9+Y4R2BDk6XtS3whc0LgFUVxHhUHC310= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:b55:: with SMTP id 82mr6710404ybl.501.1626194024052; Tue, 13 Jul 2021 09:33:44 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:43 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-6-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 05/46] KVM: SVM: Require exact CPUID.0x1 match when stuffing EDX at INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Do not allow an inexact CPUID "match" when querying the guest's CPUID.0x1 to stuff EDX during INIT. In the common case, where the guest CPU model is an AMD variant, allowing an inexact match is a nop since KVM doesn't emulate Intel's goofy "out-of-range" logic for AMD and Hygon. If the vCPU model happens to be an Intel variant, an inexact match is possible if and only if the max CPUID leaf is precisely '0'. Aside from the fact that there's probably no CPU in existence with a single CPUID leaf, if the max CPUID leaf is '0', that means that CPUID.0.EAX is '0', and thus an inexact match for CPUID.0x1.EAX will also yield '0'. So, with lots of twisty logic, no functional change intended. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 2150642e1bef..12e49dc16efe 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1359,7 +1359,7 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) } init_vmcb(vcpu); - kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, false); + kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true); kvm_rdx_write(vcpu, eax); if (kvm_vcpu_apicv_active(vcpu) && !init_event) From patchwork Tue Jul 13 16:32:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374531 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C6177C07E95 for ; Tue, 13 Jul 2021 16:33:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B18E76120A for ; Tue, 13 Jul 2021 16:33:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233530AbhGMQgm (ORCPT ); Tue, 13 Jul 2021 12:36:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233558AbhGMQgi (ORCPT ); Tue, 13 Jul 2021 12:36:38 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E794DC061788 for ; Tue, 13 Jul 2021 09:33:46 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id h7-20020a5b0a870000b029054c59edf217so27712991ybq.3 for ; Tue, 13 Jul 2021 09:33:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=TUihYYZX01zOKRYdFMaN5OLTZeUf4zUielfX3s9wDnE=; b=Dhw50de7+3iQD4SG+Jz/qLL69yJiabJyBAj5H5B7E3+UMuR2CyUW8NfEstP/fOuRRc mEf9TpUF8eo/vMnIZJA61FPtKnpR+g9c1bnT6FCBvW03iGvbh9ZAve+TVdOOoekYWcc3 u/GRtrv7FQvpaPNjH0LK6SsgJ/Cd7BLOyAHTCGvHNVDnxFvIr+QZmwfZrXsXyjBM0Vvs berGIXOUWIrmoXTgV3U3Mb++Zrzn8H/k/ZRWI1g7ZciER+Q13T0wQx3cX5RkiMV990g6 6c8Mi33kyQaz5AFnhpmrBAJdxk/3SLLpL5IAdfWzk0mWjqpENbWekjHuseVx3H0Lul5z oQBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=TUihYYZX01zOKRYdFMaN5OLTZeUf4zUielfX3s9wDnE=; b=Hp2Fc12iv7/eM5+6iqeJX+XRnLVxEzuSuJv2FlJkwEfubDswodU2b2ObsI92PAfkTu sqhTqQYkhHTm8Y5rKcENwu7QF+4uW0cUdmhBjZFxbjIkW0F0/F1t2APXTxIyu2fNaR+9 1w5lgHa6NeKGgyeSp5kpVLBmuE3NMlNeWDOZlJqWXejMWHf8mcDstt/RZvakIEDngWH/ oNx94MiVDgbqeJUzDRR33nh1ini7ZXsidnLOQYCf5cVDCkaWpdiugB7dGV2jKDcIh9nS G4EuDNn2IiIgRFzdP36qbdJydo2hKIZgXKqIjrIYWhdq3peGi4YQmKv8kOPEG7Ht21sa +LbA== X-Gm-Message-State: AOAM532kdqexH2ZNUVIrkFBtWrsC8EB2e+gLBvnbIvFvyT1m1yrQHE1c 44s7Lgfl42gB5onBDh/HekkLAyphCfs= X-Google-Smtp-Source: ABdhPJweObVAAyzT25jnRiOd8aMiD8irO4yT89qYPN61ugodwuG6JmENd4I3SPiS4n83/LNgs+BVfFp2o/s= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:8208:: with SMTP id q8mr7275091ybk.378.1626194026059; Tue, 13 Jul 2021 09:33:46 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:44 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-7-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 06/46] KVM: SVM: Fall back to KVM's hardcoded value for EDX at RESET/INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org At vCPU RESET/INIT (mostly RESET), stuff EDX with KVM's hardcoded, default Family-Model-Stepping ID of 0x600 if CPUID.0x1 isn't defined. At RESET, the CPUID lookup is guaranteed to "miss" because KVM emulates RESET before exposing the vCPU to userspace, i.e. userspace can't possibly have done set the vCPU's CPUID model, and thus KVM will always write '0'. At INIT, using 0x600 is less bad than using '0'. While initializing EDX to '0' is _extremely_ unlikely to be noticed by the guest, let alone break the guest, and can be overridden by userspace for the RESET case, using 0x600 is preferable as it will allow consolidating the relevant VMX and SVM RESET/INIT logic in the future. And, digging through old specs suggests that neither Intel nor AMD have ever shipped a CPU that initialized EDX to '0' at RESET. Regarding 0x600 as KVM's default Family, it is a sane default and in many ways the most appropriate. Prior to the 386 implementations, DX was undefined at RESET. With the 386, 486, 586/P5, and 686/P6/Athlon, both Intel and AMD set EDX to 3, 4, 5, and 6 respectively. AMD switched to using '15' as its primary Family with the introduction of AMD64, but Intel has continued using '6' for the last few decades. So, '6' is a valid Family for both Intel and AMD CPUs, is compatible with both 32-bit and 64-bit CPUs (albeit not a perfect fit for 64-bit AMD), and of the common Families (3 - 6), is the best fit with respect to KVM's virtual CPU model. E.g. prior to the P6, Intel CPUs did not have a STI window. Modern operating systems, Linux included, rely on the STI window, e.g. for "safe halt", and KVM unconditionally assumes the virtual CPU has an STI window. Thus enumerating a Family ID of 3, 4, or 5 would be provably wrong. Opportunistically remove a stale comment. Fixes: 66f7b72e1171 ("KVM: x86: Make register state after reset conform to specification") Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 12e49dc16efe..7da214660c64 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1277,7 +1277,6 @@ static void init_vmcb(struct kvm_vcpu *vcpu) kvm_mmu_reset_context(vcpu); save->cr4 = X86_CR4_PAE; - /* rdx = ?? */ if (npt_enabled) { /* Setup VMCB for Nested Paging */ @@ -1359,7 +1358,15 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) } init_vmcb(vcpu); - kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true); + /* + * Fall back to KVM's default Family/Model/Stepping if no CPUID match + * is found. Note, it's impossible to get a match at RESET since KVM + * emulates RESET before exposing the vCPU to userspace, i.e. it's + * impossible for kvm_cpuid() to find a valid entry on RESET. But, go + * through the motions in case that's ever remedied, and to be pedantic. + */ + if (!kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true)) + eax = get_rdx_init_val(); kvm_rdx_write(vcpu, eax); if (kvm_vcpu_apicv_active(vcpu) && !init_event) From patchwork Tue Jul 13 16:32:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374533 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 95451C07E95 for ; Tue, 13 Jul 2021 16:33:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7EE126128B for ; Tue, 13 Jul 2021 16:33:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233829AbhGMQgp (ORCPT ); Tue, 13 Jul 2021 12:36:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233797AbhGMQgl (ORCPT ); Tue, 13 Jul 2021 12:36:41 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7FD8C0613DD for ; Tue, 13 Jul 2021 09:33:48 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id w16-20020a05620a4450b02903b88832b7cfso3103610qkp.5 for ; Tue, 13 Jul 2021 09:33:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=aiokP7gXEi/+lm4t4Nc0wlY5UTXEFoQdvF62MxbIn5k=; b=hY+meXs2gYW7Dto1FpL154fzd3T6mmPiVvvxWryypZ0ivIdoK3bMWkMbY25DhEfegn WnFele0LERdxBEro8TKORykXPOpJq+YCFT4B7+YJQ/CVpub97MzMr6p6xVrljB6ieD7Q vjjNswgIS4i5uHMOSW3uNyyfv1YkHcK56jJWb85X/a5YBPGBvWsMgMxfOqQt3chTxGPX gPX1qDtoqrG8/+k7qmQh6UoTyfXeZq5mIORWe1zs6ZGmMBzD8g9v5O7kCDjOKsyJB/vo +L8PL4EOU208ctZ+zIRwmUGppTPjjMkmcVulIuXJINLJGKGPrCVm19fq94ltXl1WKjHi etCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=aiokP7gXEi/+lm4t4Nc0wlY5UTXEFoQdvF62MxbIn5k=; b=q7++S+NcFKOYXhc941GJVtuqMsiekz2euZlaaICsXIF5Zyi9lAahzKEBjwbwPmFyPh fzWKikfHxEwpdc7xV7i297lTomVBaL8c7FB5kfy11zbmtlJiOvDCk8jNz7M/pJRQAhFu 2LdvXk9BSjN/I9m951OUcav95BnltnzjDpLqzM3xtVuRLH6g9XCYHccbkby94XpGDKPD lSyvS4WXXgV0ep86MG1HhozmzrOb/K3p2J7x2LuINaVIw3bHPjHiUTz8qli849fbazZu hFKd+sieOAoWe6B4GxjC085RQQajJgaBcIowPlVOUJxWvxSgMrXvaXnnLIYkhUha6KRz n5qQ== X-Gm-Message-State: AOAM530MU+LCm8aJQmyIhJZikVS2cAYuseMKj3qUsqhEWK+U3Ou6kKVW hHGlnjBUeVgzN/C6zEFAkTo9zhOFd94= X-Google-Smtp-Source: ABdhPJxyYzNmCbK+8PVWv/wDB2e4DroqBp9Dv2IeQbdBVHp7UYLkLgMXU7AWD+mJQV5ENWWNA3cpTjmCbT0= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a05:6214:16ca:: with SMTP id d10mr5659095qvz.59.1626194027964; Tue, 13 Jul 2021 09:33:47 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:45 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-8-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 07/46] KVM: VMX: Remove explicit MMU reset in enter_rmode() From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Drop an explicit MMU reset when entering emulated real mode now that the vCPU INIT/RESET path correctly handles conditional MMU resets, e.g. if INIT arrives while the vCPU is in 64-bit mode. Note, while there are multiple other direct calls to vmx_set_cr0(), i.e. paths that change CR0 without invoking kvm_post_set_cr0(), only the INIT emulation can reach enter_rmode(). CLTS emulation only toggles CR.TS, VM-Exit (and late VM-Fail) emulation cannot architecturally transition to Real Mode, and VM-Enter to Real Mode is possible if and only if Unrestricted Guest is enabled (exposed to L1). This effectively reverts commit 8668a3c468ed ("KVM: VMX: Reset mmu context when entering real mode") Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 825197f21700..0f5e97a904e5 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2852,8 +2852,6 @@ static void enter_rmode(struct kvm_vcpu *vcpu) fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); - - kvm_mmu_reset_context(vcpu); } int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) From patchwork Tue Jul 13 16:32:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374535 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 576D3C11F66 for ; Tue, 13 Jul 2021 16:33:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3F7C5611C0 for ; Tue, 13 Jul 2021 16:33:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234036AbhGMQgq (ORCPT ); Tue, 13 Jul 2021 12:36:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233858AbhGMQgl (ORCPT ); Tue, 13 Jul 2021 12:36:41 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8084C0613EE for ; Tue, 13 Jul 2021 09:33:50 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id o12-20020a5b050c0000b02904f4a117bd74so27727775ybp.17 for ; Tue, 13 Jul 2021 09:33:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=Yi6bDLPGCrbqS09Vb1Q1YxAsIO5UfWIqGpJKeQgB6SM=; b=IVnOLNaL2eWedum/MsETC2COPdFjr41LCUU0dWy0VQ24GHK+90gGUlHlDQeoU8HW1d mmZ1yZqe9nXwNxMCwWOpj3gCIcVAat5HY7uNR5tiFwITBNaq+DXV9yOhggr1MdNKUb/g QrqZ1ZNJHkRub0g1yBYxgv51Da5cgu4KZ4YESMWLgP8GrcT3ARqrjmmwA0JbSFfCbLFM 026R2zcJndSlaBgezEKyQObEjkqDmPNqUxBbPJAwDunR+PPzo8UceXVLjBwkpicIxDvF 1J5/mCRFFkGEHBcITYmPmDxZEtL2Ov1hnEOlpQd5OBFvhuVAP7y35LmQTsxHT74r0uUB +61w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=Yi6bDLPGCrbqS09Vb1Q1YxAsIO5UfWIqGpJKeQgB6SM=; b=WC12WViW9pJ/5Y974Pb90CMFvzdCFi/2zwxym5e22Lll+IjR4xfzkVSUd8FqLmjJTy z1Siuhd9xAa9MLemllNdzaB5q9xXAdMFeEKyCZgg+exyeHhvAK3Jhv/ZvmRPLea7TCoc RijPxVa232vGX8f3kNyyFeHieCuMC7MLkqZdICOMubJRx34Hu5d4lnDgcGskoT2OxxAs WeSmZBcCrY61S0zumis0et1EXR0KPLTU9Riw829GH4W/taDlc+OC5NL7gPRsYgUCDt1j 5VxT0e0ljmqlmdfVM/DfLDUPscYfmcHzXoDlF/CSaVUP48OrIjfECpSs1JQi03Ay8aCL foKQ== X-Gm-Message-State: AOAM532PlGR7op4g7UClGxce007WGyGZvwZiyc4u4yRyvTtNmjfu59sO QTIIaFI6hJmUCTl+/JYlDnXmm+LdoOI= X-Google-Smtp-Source: ABdhPJwBGiKmMkpMUzwxlAmBPHwwMkjSyv361z6WDe1EupMy+LnGBNRjp8Wby34ZiMJVsA4w5V0A1rK4PJs= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:550:: with SMTP id 77mr7152673ybf.452.1626194030009; Tue, 13 Jul 2021 09:33:50 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:46 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-9-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 08/46] KVM: SVM: Drop explicit MMU reset at RESET/INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Drop an explicit MMU reset in SVM's vCPU RESET/INIT flow now that the common x86 path correctly handles conditional MMU resets, e.g. if INIT arrives while the vCPU is in 64-bit mode. This reverts commit ebae871a509d ("kvm: svm: reset mmu on VCPU reset"). Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 7da214660c64..44248548be7d 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1274,7 +1274,6 @@ static void init_vmcb(struct kvm_vcpu *vcpu) * It also updates the guest-visible cr0 value. */ svm_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); - kvm_mmu_reset_context(vcpu); save->cr4 = X86_CR4_PAE; From patchwork Tue Jul 13 16:32:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374537 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7CE3C07E95 for ; Tue, 13 Jul 2021 16:33:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 924F36120A for ; Tue, 13 Jul 2021 16:33:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234169AbhGMQgr (ORCPT ); Tue, 13 Jul 2021 12:36:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233470AbhGMQgn (ORCPT ); Tue, 13 Jul 2021 12:36:43 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9045FC0613EF for ; Tue, 13 Jul 2021 09:33:52 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id r190-20020a375dc70000b02903acea04c19fso17416255qkb.8 for ; Tue, 13 Jul 2021 09:33:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=EX4D+eojcaFEJw7uYtuJ66OnM1pg9jtwgYRDkvzAZPk=; b=v5skcrT3HKZ7xQ2i5ono4qCe9BI8iCx1TyHGuIg8UDLy05+b783LKDIH4rcFkL6xzJ 3KtS0KgaiO5t4nVn7u39gDjZ58gPjSGiDmJKw+lcQDMDGTFKJ8ZVn4wl9fXEKfVBk/Cd Btv1eAVs1kbUS19V1NvXCCfHgbc4WAkmBfEtmVmJn4Lv2kQAE/IzKR52ksbjENONv8A4 GyELtmDz3eZM7sB3agX/9IPiFDiOROnfGFGmWLP60HjKV2qv6xJ1KwANcfan2eyF9WiL ge9TpONOShlUc+p/zl5A8mOogVdIGFRtdprH9m/URPDQWdnD7lI3NJUIzsBz0QuYSPrN w47A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=EX4D+eojcaFEJw7uYtuJ66OnM1pg9jtwgYRDkvzAZPk=; b=Liw70FWvFqlJqlyI4W4FfML7CQKW7BqtJvtv84ySOEPfkUGNEkNNfWwbjfULu6QPPF 24z8LjEJFeE4dtN6Y+f+r1XS3udynjyHVuVxHbL43vxqKHIbLDuhev7bS/5OW6+QiGh6 oxsV77iSGrloXYSj2QmJLcxlNIsgV7X0Ah+tsPei8xUPIjbdBMWCIXwmH/ni/KZyoXGU 9cqJdk01rkWdKrgdOgoKYpBYQVRP6wxBC6AyObSAJsJzlSdlGJ+bggbTeTwKzCheLaGt h+IbmpBEJpR+YMzBu3zC5coroCT/O+5jQG1r7yap1Flc7rOyiVT2jeIQRaBC9hoXXHuZ Morw== X-Gm-Message-State: AOAM53223DGxRomGMRWI9dOS9t0M1roxslgFAk1+NdGofc/Bpu3xMarH mvcRHXpSZWwavEzgEYx2ZKwSiMsePxI= X-Google-Smtp-Source: ABdhPJx8f/UWJyCjWFmOihdukFJMFY7Cz4y+bbrOY9mCxlOBAzT5ppdpIqlNGyDPoqZF0KDpBM6E3EcgXNs= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a0c:d68f:: with SMTP id k15mr5823187qvi.14.1626194031709; Tue, 13 Jul 2021 09:33:51 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:47 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-10-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 09/46] KVM: SVM: Drop a redundant init_vmcb() from svm_create_vcpu() From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Drop an extra init_vmcb() from svm_create_vcpu(), svm_vcpu_reset() is guaranteed to call init_vmcb() and there are no consumers of the VMCB data between ->vcpu_create() and ->vcpu_reset(). Keep the call to svm_switch_vmcb() as sev_es_create_vcpu() touches the current VMCB, but hoist it up a few lines to associate the switch with the allocation of vmcb01. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 44248548be7d..cef9520fe77f 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1431,15 +1431,13 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) svm->vmcb01.ptr = page_address(vmcb01_page); svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT); + svm_switch_vmcb(svm, &svm->vmcb01); if (vmsa_page) svm->vmsa = page_address(vmsa_page); svm->guest_state_loaded = false; - svm_switch_vmcb(svm, &svm->vmcb01); - init_vmcb(vcpu); - svm_init_osvw(vcpu); vcpu->arch.microcode_version = 0x01000065; From patchwork Tue Jul 13 16:32:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374539 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 346A5C11F66 for ; Tue, 13 Jul 2021 16:34:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 20FA8611C0 for ; Tue, 13 Jul 2021 16:34:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233732AbhGMQgt (ORCPT ); Tue, 13 Jul 2021 12:36:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233676AbhGMQgo (ORCPT ); Tue, 13 Jul 2021 12:36:44 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EFD5C0613DD for ; Tue, 13 Jul 2021 09:33:54 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id c5-20020a0562141465b02902e2f9404330so8017743qvy.9 for ; Tue, 13 Jul 2021 09:33:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=LCuijp/LgWy7Zf7nKMsAJ/SqN4uwlKk3jFpZ0B2dGyo=; b=chy0oHxym/ue2ksnIVnqz8+fw9GXKUgLXtGVmUl+sFwwgboS/wOTM2nzRO9ESgMBtL hclJywDEgQ+8V5ox2uhi+cqCLKkD4DWHuroZCF0UCKNGOLGY3CVxTkF14aicTr67ytWv aoIhtkRkyyisUMhMDCs5kWQs1n8Qh1FDRiF5ehw2NJTajag0Ym25ERziA6Dvfb+NeEjE jnDVlf8tJp82W2lHaqw8EJe8iH59tzh9jhdlYmicGFuoAkbKNUSlyNbQ68Bukqvy2Udi oNwJDB1LYERljoSAHrUv4SIxbFuTBjpxH5lDnKiGJo2YKG9OZPWRrrlhY5GFgH3Gql0C 1juQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=LCuijp/LgWy7Zf7nKMsAJ/SqN4uwlKk3jFpZ0B2dGyo=; b=CInVI+PTBrJNP2F5aAUEMPBg6y94KebEtRC/p5Cf7/YZdp3QHpDqooxJCDAsmgcs1i tXETKhTlfMzT1LKNnnqWSeW3e5yP9ndui0ml5Y1orXNQ2ilohlzTk8MqS7EWQUXdBbAu suzDoB8L7zofCTVyHofB6VPbQ5H3eFxZnGTaSHBtbxrTkqzpefo9dpraLplnxCEHiLeI 1EjXhXRyrril2o6VNi4dXhZRbUxXYXI5y0rAipjJMwPbjf0jSuQLDmJO0TFMvp9AR8wh +xSRvEjIabVZeqqf/jBGlbtuEr1YLnJZF9Zgi66B9yaF+2KBjWJEGSA9LCDkGujlkEdJ Fx4g== X-Gm-Message-State: AOAM530Kp30PJ3c+E82OgoW0jsRCbhGb2aCKyM9I9oy/csMES8YE8Gdm ph9rM9L/t/0+tRcQJXptNic38E8i2Ho= X-Google-Smtp-Source: ABdhPJyExWoaSPN4yVn66GFYgoo1Y+P3Z9I0FwdoNB0tbDz/4uSJ9d/HWwh3kjVX22cXxNrew0K/s7Ox8EE= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a05:6214:14e2:: with SMTP id k2mr5662267qvw.21.1626194033616; Tue, 13 Jul 2021 09:33:53 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:48 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-11-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 10/46] KVM: VMX: Move init_vmcs() invocation to vmx_vcpu_reset() From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Initialize constant VMCS state in vcpu_vcpu_reset() instead of in vmx_vcpu_create(), which allows for the removal of the open coded "vCPU load" sequence since ->vcpu_reset() is invoked while the vCPU is properly loaded (which is the entire point of vCPU reset...). Deferring initialization is effectively a nop as it's impossible to safely access the VMCS between the current call site and its new home, as both the vCPU and the pCPU are put immediately after init_vmcs(), i.e. the VMCS isn't guaranteed to be loaded. Note, task preemption is not a problem as vmx_sched_in() _can't_ touch the VMCS as ->sched_in() is invoked before the vCPU, and thus VMCS, is reloaded. I.e. the preemption path also can't consume VMCS state. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 0f5e97a904e5..26c0e776827c 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4293,10 +4293,6 @@ static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) #define VMX_XSS_EXIT_BITMAP 0 -/* - * Noting that the initialization of Guest-state Area of VMCS is in - * vmx_vcpu_reset(). - */ static void init_vmcs(struct vcpu_vmx *vmx) { if (nested) @@ -4395,6 +4391,9 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) u32 eax, dummy; u64 cr0; + if (!init_event) + init_vmcs(vmx); + vmx->rmode.vm86_active = 0; vmx->spec_ctrl = 0; @@ -6782,7 +6781,7 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu) { struct vmx_uret_msr *tsx_ctrl; struct vcpu_vmx *vmx; - int i, cpu, err; + int i, err; BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0); vmx = to_vmx(vcpu); @@ -6844,12 +6843,7 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu) vmx->msr_bitmap_mode = 0; vmx->loaded_vmcs = &vmx->vmcs01; - cpu = get_cpu(); - vmx_vcpu_load(vcpu, cpu); - vcpu->cpu = cpu; - init_vmcs(vmx); - vmx_vcpu_put(vcpu); - put_cpu(); + if (cpu_need_virtualize_apic_accesses(vcpu)) { err = alloc_apic_access_page(vcpu->kvm); if (err) From patchwork Tue Jul 13 16:32:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374541 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5376C07E95 for ; Tue, 13 Jul 2021 16:34:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CD2B861369 for ; Tue, 13 Jul 2021 16:34:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234097AbhGMQgz (ORCPT ); Tue, 13 Jul 2021 12:36:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234061AbhGMQgq (ORCPT ); Tue, 13 Jul 2021 12:36:46 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B6E3C06178C for ; Tue, 13 Jul 2021 09:33:56 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id x84-20020a2531570000b029055d47682463so26320861ybx.5 for ; Tue, 13 Jul 2021 09:33:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=2p0iOguwtrAqfZk6csdNwTfhtvIQqe6ewaUZHjQW6QQ=; b=bwqJvkBxbRL0/g4Xzd9Ql1plZAvQNrdXHppJu6oYhco63ZmuP/tVS8P+zbXYdG+ZGc gvw4h+9xWoxBp7Ni+vvzkMwTXdP2uW98D7eyuH9AOUG8tN2CSZnhEbvrvAuNV+T7jR0n PbRRy+RcA+q5hJJ5GEKfy6Hpo6oV0P35P49kmXnx7A9Tbn9b0VHHjCRQJFQB96C9DkR9 FwVAqAxZejhBCiIYw67z9X1NLxGm0y8/uXQfYZr95I8YbCv8/Utet/LN+oXxYvtrD/Fb KpNOmI2OBjI7fXZqKkzTI52M118bhAV6FYfVnVU13byy7qYBGMQkF6JBtCYqlybSquwz phJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=2p0iOguwtrAqfZk6csdNwTfhtvIQqe6ewaUZHjQW6QQ=; b=ImVm7zmNqCNJDaL66xli4OwPkIHE3l9QeHslK/Cg1TBwzFh/+O1uXImzmlx9rMcUdP 1FtugRkXjvzz8w/XyzqfU0Q4wW6kcoDaAxaltzVhk5PslpgFvgEcm7j1T4elFD+8oPFJ yriyONP3mNrOROqrlg03YUtstrFPXZMC3ijwMGN/Toj9tglZUC76RehBCegt6S5kt1fK ITSlmF0kw6f7EKlO54HmlsEYRb9Ch+x/B5Jq8rfV7ycUbnRX89XT1geyMdXkOD7oL+nC FTpplY58DQgAbkmJ4Xna+zrLTp32yzygtT5/PQ4JoayBcH+lHuUERqB7EbJSc4dlh4fi r8cA== X-Gm-Message-State: AOAM533pLFpVn5bkAldj/Pyox9/MEm4QzFwM6nvGA3KCwOixGX8n0vPj G4juieg0NfFpFdpgfrhVwMwAonsMwWI= X-Google-Smtp-Source: ABdhPJzNTBWmA3tPCuNn4jseNgyZpSNd1p78BX6a4vgfI+qwfUwp5NwdI/d8xzHu1U1caO3EMzuVHOinO0I= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:f827:: with SMTP id u39mr6778858ybd.100.1626194035426; Tue, 13 Jul 2021 09:33:55 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:49 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-12-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 11/46] KVM: x86: WARN if the APIC map is dirty without an in-kernel local APIC From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org WARN if KVM ends up in a state where it thinks its APIC map needs to be recalculated, but KVM is not emulating the local APIC. This is mostly to document KVM's "rules" in order to provide clarity in future cleanups. Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index ba5a27879f1d..add4dd1e3528 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -192,6 +192,9 @@ void kvm_recalculate_apic_map(struct kvm *kvm) if (atomic_read_acquire(&kvm->arch.apic_map_dirty) == CLEAN) return; + WARN_ONCE(!irqchip_in_kernel(kvm), + "Dirty APIC map without an in-kernel local APIC"); + mutex_lock(&kvm->arch.apic_map_lock); /* * Read kvm->arch.apic_map_dirty before kvm->arch.apic_map From patchwork Tue Jul 13 16:32:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374543 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75B14C07E95 for ; Tue, 13 Jul 2021 16:34:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6010D61288 for ; Tue, 13 Jul 2021 16:34:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234179AbhGMQg7 (ORCPT ); Tue, 13 Jul 2021 12:36:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234335AbhGMQgs (ORCPT ); Tue, 13 Jul 2021 12:36:48 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07344C06139F for ; Tue, 13 Jul 2021 09:33:57 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id x84-20020a2531570000b029055d47682463so26320959ybx.5 for ; Tue, 13 Jul 2021 09:33:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=xu9lt7ml4nTWe9cpEWKdb0fo86g2MaNZbmfwbHMDB9k=; b=fvcTCZaP7A7KM7+eVBkJ3b6bqoSUEtZ9Q4kaNDtWYRCWlGiQW1TfaVXqXxNra1YNAk 3m1x2PMNDOodqjQEvqFbGqj6rmIJe5ICcVTgxeDlgTorX+fCJvENw3/5Lh2uOQAkO/6i DxK2XwqqlRjL1sx+zC2vlVjq4RYlbIHTXCgE4Wdafy4UgSRpCofV86xw0AZH1IPVGMzf Hz3EewbfVTtRvY+H0ae9dtfNm0bpZgtAmCvwdrbG4a+OyRh5PG5qBNdw6fmvBQv1kVkT ESWi9enkzktooOyXyBqI7YYaM3n022F4bsMGGfF02JlavaWBk9/qjpUbUnss58H1NBXS M1Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=xu9lt7ml4nTWe9cpEWKdb0fo86g2MaNZbmfwbHMDB9k=; b=DJNbx7o2eRTpybREwvqq+ElQkVfrW8UmkIw5NpHZZ2DMGQy7+GiAtH0DYWIu5bj+dU A6QcfI95tUBwkL/xut7zz2ypRTIMIHHQFuKg5rEmmbdlQ5P/FMob05su/exvP2raYDJL uuQWzBSnz1QJ55t90v7hSKaArEY+a3YcJ2VA2dXPvmkYYpeoNqNQKmHUq48Yv6F2TNEo OCrZuyyF6GqaS0JxmQ9PSUZKlgGdQ8sBGnufR6rkITH7lrcpnQIMzxMINcSyzeeRFN7c MYVp1aquhQZ8poy/6ZLwAxZUOZsdXO9QxHWS4wCUg995d0OO6pcB3S+vxt3dbvAB3das Da3A== X-Gm-Message-State: AOAM531f+6a0B7qz0S0ZlslJXySK7k5t5uLLNZHemNW5kAfhLxBvO3oH sGees0wJqs1V+GUnqdIsWw9KH7AzuSs= X-Google-Smtp-Source: ABdhPJyUk7ZBK00HqxMu16yC3TPRFg54XBhrFFpZfREhXqq8Th4XViilePl3UFmAj6aEj+O6jCUueIaSxSo= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:bcd2:: with SMTP id l18mr6659895ybm.66.1626194037119; Tue, 13 Jul 2021 09:33:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:50 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-13-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 12/46] KVM: x86: Remove defunct BSP "update" in local APIC reset From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Remove a BSP APIC update in kvm_lapic_reset() that is a glorified and confusing nop. When the code was originally added, kvm_vcpu_is_bsp() queried kvm->arch.bsp_vcpu, i.e. the intent was to set the BSP bit in the BSP vCPU's APIC. But, stuffing the BSP bit at INIT was wrong since the guest can change its BSP(s); this was fixed by commit 58d269d8cccc ("KVM: x86: BSP in MSR_IA32_APICBASE is writable"). In other words, kvm_vcpu_is_bsp() is now purely a reflection of vcpu->arch.apic_base.MSR_IA32_APICBASE_BSP, thus the update will always set the current value and kvm_lapic_set_base() is effectively a nop if the new and old values match. The RESET case, which does need to stuff the BSP for the reset vCPU, is handled by vendor code (though this will soon be moved to common code). No functional change intended. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index add4dd1e3528..a24ce8fe93e5 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2367,9 +2367,7 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event) apic->highest_isr_cache = -1; update_divide_count(apic); atomic_set(&apic->lapic_timer.pending, 0); - if (kvm_vcpu_is_bsp(vcpu)) - kvm_lapic_set_base(vcpu, - vcpu->arch.apic_base | MSR_IA32_APICBASE_BSP); + vcpu->arch.pv_eoi.msr_val = 0; apic_update_ppr(apic); if (vcpu->arch.apicv_active) { From patchwork Tue Jul 13 16:32:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374545 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60881C11F66 for ; Tue, 13 Jul 2021 16:34:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4BD21611C0 for ; Tue, 13 Jul 2021 16:34:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234394AbhGMQhF (ORCPT ); Tue, 13 Jul 2021 12:37:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234467AbhGMQgw (ORCPT ); Tue, 13 Jul 2021 12:36:52 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8925C0613A9 for ; Tue, 13 Jul 2021 09:33:59 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 132-20020a25158a0000b029055791ebe1e6so27978970ybv.20 for ; Tue, 13 Jul 2021 09:33:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=CzDDt8Xveb+AiWMJHUA0uSHyGS/Vcfc0iWePgVgIMys=; b=t/atGzTCApK+WNx7wLzAjU9pLgb4MFOh9aA6d7dCtEFsCMWJxKXU1eUEIbWIQUlpJo t+GkqFwH31xp4VkQZZDtPm2SFWkIGwda8iaXl20eWtg+mQqecMO2J5NHpFfpbU8ye7xW k8HJEnbn/ILcp7nTdho08R1WmRHOkG1GJt6/o2suwvxDuEXFznyhpEDCNDHIN6+7cN5K er0Qt0MDzUOiVsthGvPQVsdzIF4mSdBl/UyLFvIVOBdncApEgqfzyxj/S6hBwUm+aP2E CrLITG0nS33ReX4zpN93eCjBWjLWtuu7uiP5lmxJhcYO4SNKsnVNlP9Gmo985EkY13nb mboQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=CzDDt8Xveb+AiWMJHUA0uSHyGS/Vcfc0iWePgVgIMys=; b=ABexVmjuad3uVd2c5tJONnKfA4vow8hP3yZWsS7XAEYTe7w6i0wueCLRrR7iqxYTyI d5l7L1Fo2FoDHt6bJdDqI4077EcuK9U4YqQ38eqNzhiZNBbTnZH/6TLQ1xbozHkNKxs6 zaBHw7YD4wgLR/A5x4vQX+wIe9IO3r6uZJ1Kf2jYjv9EoerqC5wCe5w6PLPLGMKoCmfc 97sHq4d24rv+zp+SXaRt9bnB+FQajXrbwcbF2USA2Dy3nZ4Wsc7oR5XgVYr8QDMa7fQf G7Arpu82aZECjmVnR1hbV0CCF2t606HqHLCLKOajoH9ygOxaZA8M4MVzLIve/uT/tDfz e2rA== X-Gm-Message-State: AOAM531dwaSBTBi6hII4u4M0g54oRjfw30wcoTkZl7/ona8Q7tzv0e9t KhuU1diFTc6+uKcGyAEH/36w2E55nDY= X-Google-Smtp-Source: ABdhPJx78YkFT+MLfyvhOYclBp02FKti+D9w4pPn2xvCBQRgtVqiigGbSW4kQVcEh3McY0+sMGlIfLONp8g= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:8482:: with SMTP id v2mr6838057ybk.214.1626194039015; Tue, 13 Jul 2021 09:33:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:51 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-14-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 13/46] KVM: x86: Migrate the PIT only if vcpu0 is migrated, not any BSP From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Make vcpu0 the arbitrary owner of the PIT, as was intended when PIT migration was added by commit 2f5997140f22 ("KVM: migrate PIT timer"). The PIT was unintentionally turned into being owned by the BSP by commit c5af89b68abb ("KVM: Introduce kvm_vcpu_is_bsp() function."), and was then unintentionally converted to a shared ownership model when kvm_vcpu_is_bsp() was modified to check the APIC base MSR instead of hardcoding vcpu0 as the BSP. Functionally, this just means the PIT's hrtimer is migrated less often. The real motivation is to remove the usage of kvm_vcpu_is_bsp(), so that more legacy/broken crud can be removed in a future patch. Fixes: 58d269d8cccc ("KVM: x86: BSP in MSR_IA32_APICBASE is writable") Signed-off-by: Sean Christopherson --- arch/x86/kvm/i8254.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c index a6e218c6140d..5a69cce4d72d 100644 --- a/arch/x86/kvm/i8254.c +++ b/arch/x86/kvm/i8254.c @@ -220,7 +220,8 @@ void __kvm_migrate_pit_timer(struct kvm_vcpu *vcpu) struct kvm_pit *pit = vcpu->kvm->arch.vpit; struct hrtimer *timer; - if (!kvm_vcpu_is_bsp(vcpu) || !pit) + /* Somewhat arbitrarily make vcpu0 the owner of the PIT. */ + if (vcpu->vcpu_id || !pit) return; timer = &pit->pit_state.timer; From patchwork Tue Jul 13 16:32:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374547 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2B3EC07E96 for ; Tue, 13 Jul 2021 16:34:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 898AB61288 for ; Tue, 13 Jul 2021 16:34:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234103AbhGMQhI (ORCPT ); Tue, 13 Jul 2021 12:37:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234564AbhGMQgy (ORCPT ); Tue, 13 Jul 2021 12:36:54 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CCF7C061786 for ; Tue, 13 Jul 2021 09:34:01 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id x84-20020a2531570000b029055d47682463so26321223ybx.5 for ; Tue, 13 Jul 2021 09:34:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=+Nszryi/FBfiKvCS21S457SpsXLG0MQv2DcCak+yOfg=; b=dYSEg7/diIS9Cfak6cnpxdhGL0FSJKRng8fOxUiZH4b+JZquqrZU6RwapYCQ2b/WwQ Ql5679h0r51uwoi2GFoLh1gyv0COTeR6sm67Yz9DozbVn3azGAxAYEOxmnBnbkAlsiJO BTFeNCyI2qs1Q/LZmHbj88ZQGfmFPUrVTzC0ZhKXGvcgZf5nofq7AyInPrhRDsnRALtr ClnjS9VvZfRGJIAojOgna+w37eQubM9cior+bQJqIV3OCAvEXwSMnG3uG99rB5zzIG/7 FPCVd2Cb2512mgJdZElaqqNbsMd5/zILNMk/j/IlxbRmn9pc04pf7Bzc0IGUf5QxiCym T/pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=+Nszryi/FBfiKvCS21S457SpsXLG0MQv2DcCak+yOfg=; b=ba/GBeGOjna1G9zFQa2OMCM6M2cUy3QyEiaGeo0TIoVALqC9xbayLQWQGoDBz5nq9u RAB5cgwoZsgbh71FdpnjzjxS034Zt4OvuQEuZNPJP/1iG2j7bCE95U7vPIe4+XKp+IkF YK3fJOVeBIhLloM5oD558eJhvT+XzZyJ/+GY3SQhpY4aUqUlniy4ngp7bwsszel938Ca Z5TiCasYCNYwn32Xozclg7nSxHaqXIVdE9VPA6rh5AeXfnzBZNOew7HRxT+04QeMB/aO jpt01bWSUTNsGe2IBwLMyOrku90kzp9fy4K+g5HMdoLKGjG1fZ5vagDsQrUHcTKsCZFQ xOug== X-Gm-Message-State: AOAM530t4lHDLOTpeXewMf07Tn13wKUs8RLDw78zNb01rxjVGAr8PM9g rlHfYJDNkV27fgcnAYkZTYOTiGvo+kE= X-Google-Smtp-Source: ABdhPJxffu2ahKXuQIo8OH4YIeYZkZ120VWwITlZ1im5RmpLKGGaQQboqVBU5fYWDnF0FGkKEQowlrh+Pds= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:6d43:: with SMTP id i64mr6948596ybc.252.1626194040788; Tue, 13 Jul 2021 09:34:00 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:52 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-15-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 14/46] KVM: x86: Don't force set BSP bit when local APIC is managed by userspace From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Don't set the BSP bit in vcpu->arch.apic_base when the local APIC is managed by userspace. Forcing all vCPUs to be BSPs is non-sensical, and was dead code when it was added by commit 97222cc83163 ("KVM: Emulate local APIC in kernel"). At the time, kvm_lapic_set_base() was invoked if and only if the local APIC was in-kernel (and it couldn't be called before the vCPU created its APIC). kvm_lapic_set_base() eventually gained generic usage, but the latent bug escaped notice because the only true consumer would be the guest itself in the form of an explicit RDMSRs on APs. Out of Linux, SeaBIOS, and EDK2/OVMF, only OVMF consumes the BSP bit from the APIC_BASE MSR. For the vast majority of usage in OVMF, BSP confusion would be benign. OVMF's BSP election upon SMI rendezvous might be broken, but practically no one runs KVM with an out-of-kernel local APIC, let alone does so while utilizing SMIs with OVMF. Fixes: 97222cc83163 ("KVM: Emulate local APIC in kernel") Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index a24ce8fe93e5..acb201d16b5e 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2268,9 +2268,6 @@ void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value) u64 old_value = vcpu->arch.apic_base; struct kvm_lapic *apic = vcpu->arch.apic; - if (!apic) - value |= MSR_IA32_APICBASE_BSP; - vcpu->arch.apic_base = value; if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) From patchwork Tue Jul 13 16:32:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374551 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E63FEC07E96 for ; Tue, 13 Jul 2021 16:34:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CFEE761288 for ; Tue, 13 Jul 2021 16:34:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234918AbhGMQhL (ORCPT ); Tue, 13 Jul 2021 12:37:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234693AbhGMQg6 (ORCPT ); Tue, 13 Jul 2021 12:36:58 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61BFCC0613AB for ; Tue, 13 Jul 2021 09:34:03 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id z64-20020a257e430000b0290550b1931c8dso27317018ybc.4 for ; Tue, 13 Jul 2021 09:34:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=BciuiwGJwso95cAjqjPQ+Wf7zIf6IuqrurDTOWbsptw=; b=K15tC7HDPTNvLb3YFMilynTbQJZ6O0e2OQH/a9z/1hQKLTNoG4NeQ1JhiahFrS7kqQ cBKoQgJwCMjP48NwpdespWi4BNVtl4/sj2FokKy/Ep3QkGdmMRT7m1ypoPBjdKPPqZdc 2vhzrKqdFd4eTeWm7AItGiRY2rbZc8OC5Umv0R1jqhKwLK7eamLdU7iIE6mdeoK8giH6 L8J8kU0ZUJMyVMRvCNYPIgmywqZELvjwtMv3fIt+8PCbentMMgdAE2ZA5sApIleNszOR HsCSv9K6CaDTY6mYi9faILfSIlI+iEmAvOChhkP8Hns3uvzYENdZagiGPI4hggmTS/ly bKkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=BciuiwGJwso95cAjqjPQ+Wf7zIf6IuqrurDTOWbsptw=; b=g5S2JlCbkl87nZnIBv3c/rt10y7pdwgdzqlEH7Sqh20W7yrIuKpIDGszfEAtdH/2fW HGYKnZQu/LrkLJ9+9DfB4jkgqVLwhGUgNHRfSnheGqrxavgzJ/CqbCd+sTdjPy8NFKWZ GBsHEceegeVssNHsF/FuzCmEG9VBO44Y6KITb5n5Z8dKGKW5VtoNMONEgVsiaseaPjH4 uBW4j8SkfIQyeNyRDStS2VpF8HTg9h0OU1cixoxGxwQEW0yVbDnLNcln232NHEVW4afj yJKw/HGHn5kmVRXsOOUg7vzLctnG0FbplPBW8euTuw1hmrAyFLL3+51G4c3bqiDtyWVS e9KQ== X-Gm-Message-State: AOAM5319Hj3jwd8U/8PEU30P18d3NEw5SSdOAXiSOHiEHSxdx8MPx8H+ A75guoopuSV2TxhQrKIkLtAF1BnpYjU= X-Google-Smtp-Source: ABdhPJyldDfff3SwBK+LS3MY+Ihjl7Hs/4K5Kegxr1rFTVMBEybaCSca4HJNGryM5LLVfjVZ49Kw78j4q5A= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:6c6:: with SMTP id 189mr6740215ybg.33.1626194042605; Tue, 13 Jul 2021 09:34:02 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:53 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-16-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 15/46] KVM: x86: Set BSP bit in reset BSP vCPU's APIC base by default From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Set the BSP bit appropriately during local APIC "reset" instead of relying on vendor code to clean up at a later point. This is a step towards consolidating the local APIC, VMX, and SVM xAPIC initialization code. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index acb201d16b5e..0fb282b64c8f 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2321,6 +2321,7 @@ EXPORT_SYMBOL_GPL(kvm_apic_update_apicv); void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event) { struct kvm_lapic *apic = vcpu->arch.apic; + u64 msr_val; int i; if (!apic) @@ -2330,8 +2331,10 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event) hrtimer_cancel(&apic->lapic_timer.timer); if (!init_event) { - kvm_lapic_set_base(vcpu, APIC_DEFAULT_PHYS_BASE | - MSR_IA32_APICBASE_ENABLE); + msr_val = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE; + if (kvm_vcpu_is_reset_bsp(vcpu)) + msr_val |= MSR_IA32_APICBASE_BSP; + kvm_lapic_set_base(vcpu, msr_val); kvm_apic_set_xapic_id(apic, vcpu->vcpu_id); } kvm_apic_set_version(apic->vcpu); From patchwork Tue Jul 13 16:32:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374549 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3B1E8C07E95 for ; Tue, 13 Jul 2021 16:34:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 23E476120A for ; Tue, 13 Jul 2021 16:34:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234691AbhGMQhK (ORCPT ); Tue, 13 Jul 2021 12:37:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234721AbhGMQg6 (ORCPT ); Tue, 13 Jul 2021 12:36:58 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 437C9C0613AD for ; Tue, 13 Jul 2021 09:34:05 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id c18-20020a05622a0592b0290251fb198592so14080919qtb.1 for ; Tue, 13 Jul 2021 09:34:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=BViIrq7/naaabix9eGtnsasIB9sZItVFvJ7J2DJEvDU=; b=ppznS5ugBlkpj+tQLECKl+QN/zTYkSxBGDVy/qaWcGfKQF3pHSPIMK9qk9f2GQikti Rlq/803lQYl1woPs1eam7z0RNaTdBIJRVT9hPb6z7rUWro/rEV92gg0yn1HWGr2lIwrO Z0Q/sd26v8m8ezlWllo3x2DDPmZcedwzF0qTeJYTYa1Zf2w7iGG9yKlsSKq2MgXCmD+U IN8m4KRzXSK+Jjhp6rmY/eEKkIbsldEgkKlY48hiyzDM6c6XNgtQoj7WMW8dNQhu0cvb nFZNN1wTe7qEBFHQdL+wOacaOqeE/vzt/mzweMcOUIAKVhuSUsKPdu2bbmQH9DlGWbKC oNog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=BViIrq7/naaabix9eGtnsasIB9sZItVFvJ7J2DJEvDU=; b=pRo57DZEpSYUPnx3WCdeyPcEwIvHoSOTaDhLy8ARKdT+rSls+NjuwaCYg4Kt6Uwj95 Or5jTtTCK5VWAZm6Uum9R869MdUAx+jReaMuq+cx/v9HuqmIP5VOtbxI1YBXSEXky+92 Wo9fksSpKwl7gLbc6Jv6NpSi4pGHM+ALRiv7YVTt8eMGPl6/guitpWASGA1+mhYlHM0S iX3ijpes1uP4it8amQz+kMYI/EXjV4kei2R0dOg/yM9khC+70EaZGNVnA10TSH15+vw6 JklcuHJ8Q0hgjBZr+OIpeDfqRnNaSbvhfFnu1g9ZvMUsW1cPhQNOhabkZGZhnc7/6Rhv OW6g== X-Gm-Message-State: AOAM5327tv7lxc+DGaxc03cDsTI7r8U+rKcHv9cH8Y5wm2gmBrehzuiI WkdoZKVV6A4n0L9QZb//SIxqxE+a4+A= X-Google-Smtp-Source: ABdhPJxpNGefM+IAg3Y2fzT15N+qSYQC/SIzrDhkYXcNhqrVOgJxPBZGyV3jq0fM6xMJTqJVEYsCmDfmR5s= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a0c:f802:: with SMTP id r2mr5856501qvn.24.1626194044333; Tue, 13 Jul 2021 09:34:04 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:54 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-17-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 16/46] KVM: VMX: Stuff vcpu->arch.apic_base directly at vCPU RESET From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Write vcpu->arch.apic_base directly instead of bouncing through kvm_set_apic_base(). This is a glorified nop, and is a step towards cleaning up the mess that is local APIC creation. When using an in-kernel APIC, kvm_create_lapic() explicitly sets vcpu->arch.apic_base to MSR_IA32_APICBASE_ENABLE to avoid its own kvm_lapic_set_base() call in kvm_lapic_reset() from triggering state changes. That call during RESET exists purely to set apic->base_address to the default base value. As a result, by the time VMX gets control, the only missing piece is the BSP bit being set for the reset BSP. For a userspace APIC, there are no side effects to process (for the APIC). In both cases, the call to kvm_update_cpuid_runtime() is a nop because the vCPU hasn't yet been exposed to userspace, i.e. there can't be any CPUID entries. No functional change intended. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 26c0e776827c..e6cc389ec697 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4387,7 +4387,6 @@ static void init_vmcs(struct vcpu_vmx *vmx) static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) { struct vcpu_vmx *vmx = to_vmx(vcpu); - struct msr_data apic_base_msr; u32 eax, dummy; u64 cr0; @@ -4408,12 +4407,10 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) kvm_set_cr8(vcpu, 0); if (!init_event) { - apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | - MSR_IA32_APICBASE_ENABLE; + vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE | + MSR_IA32_APICBASE_ENABLE; if (kvm_vcpu_is_reset_bsp(vcpu)) - apic_base_msr.data |= MSR_IA32_APICBASE_BSP; - apic_base_msr.host_initiated = true; - kvm_set_apic_base(vcpu, &apic_base_msr); + vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP; } vmx_segment_cache_clear(vmx); From patchwork Tue Jul 13 16:32:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374553 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97E2DC07E95 for ; Tue, 13 Jul 2021 16:34:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 81C00611C0 for ; Tue, 13 Jul 2021 16:34:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235025AbhGMQhM (ORCPT ); Tue, 13 Jul 2021 12:37:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233899AbhGMQhC (ORCPT ); Tue, 13 Jul 2021 12:37:02 -0400 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EEA2AC0613B3 for ; Tue, 13 Jul 2021 09:34:06 -0700 (PDT) Received: by mail-qv1-xf49.google.com with SMTP id c22-20020a0ca9d60000b02902e60d75210eso6777021qvb.19 for ; Tue, 13 Jul 2021 09:34:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=7YozZGvMSf/jCSN+Qg+5Dz6Wwzwe/H91pLQ2KVU283o=; b=BcNiINlWTlRB8Pp+iMlEXO+KNmSi8xP6HR5i/GC4buznyoCiy/utgiSwMW3ALjnRRn nIpEH1jUwc+YZ3Yn7PK4UP9oqeU4IW5K/XzzJbfbh6C4bgPmmoMMJkyrpH1ALo1XYynt TsXmleEcKm0hBx1rFkAQmUKil57oy5fY7pn8NQo1wt9t66LJ82I4yGg9xJDTSeW5eZEA e3j6G1/I9kN/huX6tphK/VyqRlLFn5ejiUgW8+sYFssO7Ma9iQROYOYnEA96gQ1QVZyJ U6LH3vNXjiUxmU1AFo1P5pgjw9AnLO4O6iYNFMmf1UhFmTH8D1aEBBk511dVsjh3qTY4 U5Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=7YozZGvMSf/jCSN+Qg+5Dz6Wwzwe/H91pLQ2KVU283o=; b=FNnP5Kb7PZHdVV/Jzx4fGatHRxgds6J6Q33qjR+5IzfGeRZRnJXGbhcYj7wbqlvlnL jEtJZRmJtgwwiY6l0bn4uBJKJ1MpSum5VQA9+ax0ygJIBZfcYfU0ZkIYiemTs/ZIlQpT iMgDPWMIN8oXvC/demwk5+rQZ4PxwBIVVxxtNppE/FAnRRoAucqxAR9VAz777owQKuJp XIy+CLKOSZw79EZYN7h/0C8UO/SFqJuNHKSGvCmXHKs1/8bsybX6CmE0vP5Pq4zKOkzw iuo1zwTRMRxU4q1E+j14Cdz+OeHbRsLTrOpCV1C4x89kBNdBcifaophDcibyOaq1Dnhm Goug== X-Gm-Message-State: AOAM532wjp2y389YPBTJ+LdgKt+A0SJKftAL0C8zEtuW5TaRQzx0EXhn tvBlPS/0HMJC3VHZSnuOgFJXhCyFCxM= X-Google-Smtp-Source: ABdhPJyWcZKsgK+9l2bQEZZgjzvQP/ukF58TgRZwOcnOZ5AVCa0Ry7R87N04oKPrwcnp5PCb7GBdAf3MB0s= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a05:6214:ca1:: with SMTP id s1mr5767467qvs.43.1626194046121; Tue, 13 Jul 2021 09:34:06 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:55 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-18-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 17/46] KVM: x86: Open code necessary bits of kvm_lapic_set_base() at vCPU RESET From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Stuff vcpu->arch.apic_base and apic->base_address directly during APIC reset, as opposed to bouncing through kvm_set_apic_base() while fudging the ENABLE bit during creation to avoid the other, unwanted side effects. This is a step towards consolidating the APIC RESET logic across x86, VMX, and SVM. Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 0fb282b64c8f..295a9d02a9a5 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2321,7 +2321,6 @@ EXPORT_SYMBOL_GPL(kvm_apic_update_apicv); void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event) { struct kvm_lapic *apic = vcpu->arch.apic; - u64 msr_val; int i; if (!apic) @@ -2331,10 +2330,13 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event) hrtimer_cancel(&apic->lapic_timer.timer); if (!init_event) { - msr_val = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE; + vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE | + MSR_IA32_APICBASE_ENABLE; if (kvm_vcpu_is_reset_bsp(vcpu)) - msr_val |= MSR_IA32_APICBASE_BSP; - kvm_lapic_set_base(vcpu, msr_val); + vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP; + + apic->base_address = APIC_DEFAULT_PHYS_BASE; + kvm_apic_set_xapic_id(apic, vcpu->vcpu_id); } kvm_apic_set_version(apic->vcpu); @@ -2477,11 +2479,6 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) lapic_timer_advance_dynamic = false; } - /* - * APIC is created enabled. This will prevent kvm_lapic_set_base from - * thinking that APIC state has changed. - */ - vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE; static_branch_inc(&apic_sw_disabled.key); /* sw disabled at reset */ kvm_iodevice_init(&apic->dev, &apic_mmio_ops); From patchwork Tue Jul 13 16:32:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374555 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 92494C07E95 for ; Tue, 13 Jul 2021 16:34:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7DF0460FF2 for ; Tue, 13 Jul 2021 16:34:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234796AbhGMQhT (ORCPT ); Tue, 13 Jul 2021 12:37:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234577AbhGMQhI (ORCPT ); Tue, 13 Jul 2021 12:37:08 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C64BAC0613F0 for ; Tue, 13 Jul 2021 09:34:08 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id e13-20020a37e50d0000b02903ad5730c883so13160580qkg.22 for ; Tue, 13 Jul 2021 09:34:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=W278OFdK/P/bNFpq0Bb1OSbV6D8FalgDucUvoRZi8RY=; b=gCgBVGbHo73p6nEJ4i0JrquqLSNvkwfArAZEZi51w/FOuHl22eFS3N9ImBvFd/wizJ 4MSic2vK7XjZNoMyVNMLEEGLAPwg0YecQAEEYhC3SFIC3ut0A0+sT3W31Uu//Ycaongz gPOeB9d7tgg50J7Axm5OpgxtjQYFPM+W2RSrKYZNbJ/JHJkkgz4HLEnvcX/YztiuiwOI blOCCnayoP5Wq9QLoQAY+O9Ktl/tC/3NFZw4WWXV86NlhbtJeixsIhrC/J5r2QuqVSDC rXAkBSkDf4K3x9TWnIUE1u9Lv5u526aBkM8iLsv8dhPfT73SaZ37h3THsBUsAteGAUi3 xX7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=W278OFdK/P/bNFpq0Bb1OSbV6D8FalgDucUvoRZi8RY=; b=oA36aV2LqGoH9G+lxWmQKcBhEO3QRmEc54f82dXAB5jcjMQdvaA6o4Zdq9oIUVOe0j 5iFy1xyHhWtsg2MxPpNuTTUyD++SCI9V74CHSoa9YDgk4bJv9AuuTIFgmCbKg3ZJCMWK ucb0vPaQcAK4S3BuK4T/zXLT9ZIYmqz5XnwNQJqH1tj6SxJIM7hNE0/imkMCZ3L546Ls UcPU7I7TeDT84GayhzJ3i6oLV8aMAHSqGXwsv1GF2xFGX6hbXmFV+z3esjDV9R6TzrFb 0zkKubZ8m9EXRxAoHdNmESCyLy12UGkU4YdUYTI7zHxxWDS/hX7Zub/R+5G8ibGms685 2Uvw== X-Gm-Message-State: AOAM530a6l4Cp+6J1+ieL7AtNgwAzaQh2YFdc2m6bxFcg3IwBqJoFoRC JChoqR1PFxyFM/robay56UK1rYhRaM8= X-Google-Smtp-Source: ABdhPJzQxKY8FMJLMHKrBNfxNicKU/xgg+E8+T+bUXp6E5XFpnGiGnGsZEMWXFBEbVq4NfVdCVOAhO/+0wg= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a05:6214:dce:: with SMTP id 14mr5690015qvt.40.1626194047885; Tue, 13 Jul 2021 09:34:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:56 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-19-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 18/46] KVM: x86: Consolidate APIC base RESET initialization code From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Consolidate the APIC base RESET logic, which is currently spread out across both x86 and vendor code. For an in-kernel APIC, the vendor code is redundant. But for a userspace APIC, KVM relies on the vendor code to initialize vcpu->arch.apic_base. Hoist the vcpu->arch.apic_base initialization above the !apic check so that it applies to both flavors of APIC emulation, and delete the vendor code. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 14 ++++++++------ arch/x86/kvm/svm/svm.c | 6 ------ arch/x86/kvm/vmx/vmx.c | 7 ------- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 295a9d02a9a5..76fb00921203 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2323,18 +2323,20 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event) struct kvm_lapic *apic = vcpu->arch.apic; int i; - if (!apic) - return; - - /* Stop the timer in case it's a reset to an active apic */ - hrtimer_cancel(&apic->lapic_timer.timer); - if (!init_event) { vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE; if (kvm_vcpu_is_reset_bsp(vcpu)) vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP; + } + if (!apic) + return; + + /* Stop the timer in case it's a reset to an active apic */ + hrtimer_cancel(&apic->lapic_timer.timer); + + if (!init_event) { apic->base_address = APIC_DEFAULT_PHYS_BASE; kvm_apic_set_xapic_id(apic, vcpu->vcpu_id); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index cef9520fe77f..f7486b1645de 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1349,12 +1349,6 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) svm->spec_ctrl = 0; svm->virt_spec_ctrl = 0; - if (!init_event) { - vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE | - MSR_IA32_APICBASE_ENABLE; - if (kvm_vcpu_is_reset_bsp(vcpu)) - vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP; - } init_vmcb(vcpu); /* diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index e6cc389ec697..ff82c05b948b 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4406,13 +4406,6 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vmx->hv_deadline_tsc = -1; kvm_set_cr8(vcpu, 0); - if (!init_event) { - vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE | - MSR_IA32_APICBASE_ENABLE; - if (kvm_vcpu_is_reset_bsp(vcpu)) - vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP; - } - vmx_segment_cache_clear(vmx); seg_setup(VCPU_SREG_CS); From patchwork Tue Jul 13 16:32:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374557 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43B56C07E95 for ; Tue, 13 Jul 2021 16:34:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 31128613B0 for ; Tue, 13 Jul 2021 16:34:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235125AbhGMQhW (ORCPT ); Tue, 13 Jul 2021 12:37:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234654AbhGMQhJ (ORCPT ); Tue, 13 Jul 2021 12:37:09 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDEF6C0617A7 for ; Tue, 13 Jul 2021 09:34:10 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id x15-20020a25ce0f0000b029055bb0981111so27835773ybe.7 for ; Tue, 13 Jul 2021 09:34:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=ZGE0Ud/qVCZnk8Jm0n4NdfNCnKEFqwV9kF+jzCOxReM=; b=eJE/vsjdoIjKg0l/fefgluUoqMn1tp8SQbilZDd1BIAdea2s9uik2rUcU1yJtYlmaU nUVojmJULs7MYLUu7Kuq0hULFwA5xjoU+9R2N1LRwBdYUlECLFOIFApwpOsC6q+0n6+L /MOSbc0J72hfumxHjNz8NyQPb+s1lSgOY3KQSmUUZkxn6jTLNsp8hxAlsz5c11N0mcuc IqG6+6+R5+hmeyfZ44Q1cu9vXCgA6HGao//Zhb5yOwVFbQ8M6RqETRRSuwN210UvRRTg g3+Ne7Qjj4SkShboKxugnosuwXj40arzmFzyT1Ysjv2Gn0xw3C1A2aDCXvelTmflOe1N /HBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=ZGE0Ud/qVCZnk8Jm0n4NdfNCnKEFqwV9kF+jzCOxReM=; b=ZHak6nnyG6/mVEzPJXonrAc2tl51nafX43ti+wOI59+3VRGofbtK5FQB0gjwJ7JgUG vU4gnqe7jve/+okH89cpu5cF3trMud6OzkX/4uSM7q2tVyMT0iN+hqB28lwa3hmHMoTg xQV8bQSt6EDpY6rpfv5hHPHHh5O4uIROCVC1qqh+OaXod+znKgOM3hEB7W2+JeziAu01 n3UxI6j/G48JPdsI2DDC63d1Tx7W0U5JzlVOlEB1m4Bkqx2vCxhVqUwrHcz/NZ4qzSrP BL45SFBboOcnZDQ7bq8hNP5qk0AUchAPlwX/C4VCClnhY5SFCk2xLcSr3R4dURG5N52V diMg== X-Gm-Message-State: AOAM533byO/VoGbjDHeVozNX5xczLIv9EpX5sksE1Ui7BS7KY+H450Eq mSxXwxee658XFxGRQIErgxKhGbTUbHI= X-Google-Smtp-Source: ABdhPJwxjBtS/4+vWcO0fWKUNC/RdbMGpPpcjqp9zN0/Nc25SyYzwzdI0AiObIIA5+t7C6z3KURNPGbhH50= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:b790:: with SMTP id n16mr7108901ybh.274.1626194049930; Tue, 13 Jul 2021 09:34:09 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:57 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-20-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 19/46] KVM: x86: Move EDX initialization at vCPU RESET to common code From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Move the EDX initialization at vCPU RESET, which is now identical between VMX and SVM, into common code. No functional change intended. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 5 ----- arch/x86/kvm/svm/svm.c | 13 ------------- arch/x86/kvm/vmx/vmx.c | 6 ------ arch/x86/kvm/x86.c | 13 +++++++++++++ 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 974cbfb1eefe..0ec988778db1 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1772,11 +1772,6 @@ static inline unsigned long read_msr(unsigned long msr) } #endif -static inline u32 get_rdx_init_val(void) -{ - return 0x600; /* P6 family */ -} - static inline void kvm_inject_gp(struct kvm_vcpu *vcpu, u32 error_code) { kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index f7486b1645de..268580713938 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1343,25 +1343,12 @@ static void init_vmcb(struct kvm_vcpu *vcpu) static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) { struct vcpu_svm *svm = to_svm(vcpu); - u32 dummy; - u32 eax = 1; svm->spec_ctrl = 0; svm->virt_spec_ctrl = 0; init_vmcb(vcpu); - /* - * Fall back to KVM's default Family/Model/Stepping if no CPUID match - * is found. Note, it's impossible to get a match at RESET since KVM - * emulates RESET before exposing the vCPU to userspace, i.e. it's - * impossible for kvm_cpuid() to find a valid entry on RESET. But, go - * through the motions in case that's ever remedied, and to be pedantic. - */ - if (!kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true)) - eax = get_rdx_init_val(); - kvm_rdx_write(vcpu, eax); - if (kvm_vcpu_apicv_active(vcpu) && !init_event) avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE); } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index ff82c05b948b..f506b94539ab 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4387,7 +4387,6 @@ static void init_vmcs(struct vcpu_vmx *vmx) static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) { struct vcpu_vmx *vmx = to_vmx(vcpu); - u32 eax, dummy; u64 cr0; if (!init_event) @@ -4398,11 +4397,6 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vmx->msr_ia32_umwait_control = 0; - eax = 1; - if (!kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true)) - eax = get_rdx_init_val(); - kvm_rdx_write(vcpu, eax); - vmx->hv_deadline_tsc = -1; kvm_set_cr8(vcpu, 0); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4ffc4ca7d7b0..fd9026437fdd 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10787,6 +10787,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) { unsigned long old_cr0 = kvm_read_cr0(vcpu); + u32 eax, dummy; kvm_lapic_reset(vcpu, init_event); @@ -10853,6 +10854,18 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->arch.regs_avail = ~0; vcpu->arch.regs_dirty = ~0; + /* + * Fall back to KVM's default Family/Model/Stepping of 0x600 (P6/Athlon) + * if no CPUID match is found. Note, it's impossible to get a match at + * RESET since KVM emulates RESET before exposing the vCPU to userspace, + * i.e. it'simpossible for kvm_cpuid() to find a valid entry on RESET. + * But, go through the motions in case that's ever remedied. + */ + eax = 1; + if (!kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, true)) + eax = 0x600; + kvm_rdx_write(vcpu, eax); + vcpu->arch.ia32_xss = 0; static_call(kvm_x86_vcpu_reset)(vcpu, init_event); From patchwork Tue Jul 13 16:32:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374559 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07DD0C11F66 for ; Tue, 13 Jul 2021 16:34:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E9BC4613B0 for ; Tue, 13 Jul 2021 16:34:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232813AbhGMQhZ (ORCPT ); Tue, 13 Jul 2021 12:37:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234305AbhGMQhK (ORCPT ); Tue, 13 Jul 2021 12:37:10 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3D55C0613A0 for ; Tue, 13 Jul 2021 09:34:12 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id c3-20020a37b3030000b02903ad0001a2e8so17413866qkf.3 for ; Tue, 13 Jul 2021 09:34:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=VLNuKyQvTwa0Rm4aLkj5S4FZy5CA1rkSDtpmTBtHVoc=; b=oyR1MaMm7QYmjSg9mz0Rnv0KDk7T4VF7Pt6iLuMNec+Tfcf0IzGyVlEyBqbSQEeMSG 2MGXJFQ8F2EUCdTEqyaLc+rw0gcDJLIOCwp7z9/ZrM/Gbg3V+GRrXOV76VpTnGOsgMJA 2JTX5Qnv/hDtrnydyoMMrjvdxDgJQjzV4pesC2rNnm7dXyVj6xId94m9ZTCHKUeF5Rp/ lhkxyPPRy3e4NhlSyG35GE5cR5oiT4gKg+E8pCxVUNWHR/9Ua4jjD4HRqRgnlY+wRxgU 2gsJb0pCB9IMDhynM+wJjeOdGklqzO69wysuwhhAB+g7BeyzLicvq54M3YR+fp+l/I9P f/kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=VLNuKyQvTwa0Rm4aLkj5S4FZy5CA1rkSDtpmTBtHVoc=; b=YhaMvsC3eV4ANum8MhhW4X9SU86YR8uumww2O0zDdSY2JYnqDAFelVdfXzq1AxY/z3 pgPNXp93ap7Av9JalAgnLdq4dfLejb4vo3i1f4INZhRnfi3x1TinRWgQDItqx5hM/WP6 32X00Es3TDrnRRfESailjZIUwzxmoxXvrrUKqW7QtDQxGD2OEJo+gXfOk8un8pF17h1z jkioGImfPoeoSrvZRASn2cseh1TTU9PLp4OmBuCzp90N8lvX7wF6/l7cU5UyA1viuir9 gB/Kx3apL6XjkmR1POz/8yACuGFqueC3S1ArnxI+kmDtRHVRXvnKFPZFhqzs9OZy2lup jL2w== X-Gm-Message-State: AOAM531UUakQwlwYeDK1rvI3g9kA5J6XweYtWVekV1rEfLhO9iGNZ362 meUZ8w/hs0LpZ2QwiTWTyIaJTCWEQug= X-Google-Smtp-Source: ABdhPJw73ip1ApUCgVsMmAoAoiyMSd7pyjPaFYmLRPs47MCJPzGXMnA+hVNCaPFYY9W2qPMwzI2uzs3Z/9U= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:ad4:57ac:: with SMTP id g12mr5843456qvx.32.1626194051878; Tue, 13 Jul 2021 09:34:11 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:58 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-21-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 20/46] KVM: SVM: Don't bother writing vmcb->save.rip at vCPU RESET/INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Drop unnecessary initialization of vmcb->save.rip during vCPU RESET/INIT, as svm_vcpu_run() unconditionally propagates VCPU_REGS_RIP to save.rip. No true functional change intended. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 268580713938..0101646e42e0 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1266,8 +1266,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu) svm_set_efer(vcpu, 0); save->dr6 = 0xffff0ff0; kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); - save->rip = 0x0000fff0; - vcpu->arch.regs[VCPU_REGS_RIP] = save->rip; + vcpu->arch.regs[VCPU_REGS_RIP] = 0x0000fff0; /* * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0. From patchwork Tue Jul 13 16:32:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374561 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A720C11F67 for ; Tue, 13 Jul 2021 16:34:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2DBE960FEE for ; Tue, 13 Jul 2021 16:34:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235259AbhGMQh0 (ORCPT ); Tue, 13 Jul 2021 12:37:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234728AbhGMQhL (ORCPT ); Tue, 13 Jul 2021 12:37:11 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BC09C0613A9 for ; Tue, 13 Jul 2021 09:34:14 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id t144-20020a3746960000b02903ad9c5e94baso17330786qka.16 for ; Tue, 13 Jul 2021 09:34:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=vZPov19t8seutNZvZmmF4+o9jQkrFF2KtkeJS46Aq18=; b=VopMNO7HNEXA8hUjPdJmij4fABMuFmfzYYgItGc0441FQi4C11HDt5fH9s88dy6jaK lf2Ks3MiNV/S8wXOU85/kDoApjMfT9uLCwAvyGvMoFpAKZ6mk4iLcp4WYs8PK51S0hpE VQhlrRGpvcXODsgUOONai0ZNyBlekD0bDh6ZjgGYjEVGHgNnDCA5oPvVjzX+E/HqooXx sgiDsSkVVgMYkfs/z/CxT1tn3X+rKDFbL6RgOVkH97VE3WtZHis00v8RpEllvHe/LBg4 N5qJveL998nOtOCGNtZUuKUBlZqVHfkeXncK7+8PUih743LvR3eADLKkfLREijOz6ayR P6kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=vZPov19t8seutNZvZmmF4+o9jQkrFF2KtkeJS46Aq18=; b=AgcBnlnMY/HRQJLlkm0wdAPFXWjEltO9hLB8mvQV2HhtLDyANOM3zr5TshiYiAqIPa I7kQewA5P5e9J0k/PRzTlPRtbRegsAOy0LatdAnS7xogtcyKQ5kzrXv/SiKZyqzq8KR4 m6WP3fuOtKccV8ry8v+0ESK86btS2c7R+Xd1+2TJ6G31oScG1ETTYHQQoUDw9/1kHiHI xahnv6Sv1o7vTrZA/iOGEnYofmWuo4RNUSRCThtPU9+ghAPJLlZdKf9MMmTIispQAzUb 4yqhefPCs3WeTUzChUTCbJUFRGmnKmqZ7UpNDQZKJQ7U4LF0mt3fCctti/rx/SRyAlVS VHFg== X-Gm-Message-State: AOAM532ufoXHcrHghFfLK1nYOVrPJ5oZ3ax+v0VR1/X77L5WmZCaEttx 82JB9u7s0xEsM2TvYqn0jW8WnoTVA7Y= X-Google-Smtp-Source: ABdhPJxTBvC2/uhRrFcuw3Dvu3wtq6/KTPvhO8/hFY8KAScEIlI7/pKb9QNOKEf+W8kvdnqsw1bCV2USwJA= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a0c:ff48:: with SMTP id y8mr5643746qvt.29.1626194053767; Tue, 13 Jul 2021 09:34:13 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:32:59 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-22-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 21/46] KVM: VMX: Invert handling of CR0.WP for EPT without unrestricted guest From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Opt-in to forcing CR0.WP=1 for shadow paging, and stop lying about WP being "always on" for unrestricted guest. In addition to making KVM a wee bit more honest, this paves the way for additional cleanup. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index f506b94539ab..02aec75ec6f6 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -136,8 +136,7 @@ module_param(allow_smaller_maxphyaddr, bool, S_IRUGO); #define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD) #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE #define KVM_VM_CR0_ALWAYS_ON \ - (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \ - X86_CR0_WP | X86_CR0_PG | X86_CR0_PE) + (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) @@ -2995,9 +2994,7 @@ void ept_save_pdptrs(struct kvm_vcpu *vcpu) kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); } -static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, - unsigned long cr0, - struct kvm_vcpu *vcpu) +static void ept_update_paging_mode_cr0(unsigned long cr0, struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); @@ -3016,9 +3013,6 @@ static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, vcpu->arch.cr0 = cr0; vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); } - - if (!(cr0 & X86_CR0_WP)) - *hw_cr0 &= ~X86_CR0_WP; } void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) @@ -3031,6 +3025,8 @@ void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; else { hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; + if (!enable_ept) + hw_cr0 |= X86_CR0_WP; if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) enter_pmode(vcpu); @@ -3049,7 +3045,7 @@ void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) #endif if (enable_ept && !is_unrestricted_guest(vcpu)) - ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); + ept_update_paging_mode_cr0(cr0, vcpu); vmcs_writel(CR0_READ_SHADOW, cr0); vmcs_writel(GUEST_CR0, hw_cr0); From patchwork Tue Jul 13 16:33:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374563 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1774BC07E95 for ; Tue, 13 Jul 2021 16:34:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0236D60FEE for ; Tue, 13 Jul 2021 16:34:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235322AbhGMQh3 (ORCPT ); Tue, 13 Jul 2021 12:37:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234944AbhGMQhL (ORCPT ); Tue, 13 Jul 2021 12:37:11 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56F77C0613B7 for ; Tue, 13 Jul 2021 09:34:16 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id o12-20020a5b050c0000b02904f4a117bd74so27729545ybp.17 for ; Tue, 13 Jul 2021 09:34:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=SpdLdjXPNNhl9wZEIGsluiDA75HnwRkDLJ0hyfNw524=; b=t4kjJYieQpw4LJJALxZZ4yV76kGvq8sra8kEjcnQ/7a5lTzX7A6+wiG8ekpz7CQX4a Pe8S7iB37i6ptXkE42k3LXAaLtO59dWKm6lWAgzAzJB9Cwpm+MF5RAVjdSF2xMGyCurH +W1b6lpnT6MyT431QmjSyogeY4Ezs/g+qK3dauIgzmvURYHnLgZarQrl6yx3DSNggXLS QcUq9U0GDoourspKZfcRadoHXEinNe53UcM0w7SIfjoXA38Zmvv0DYsnr3obTK1vwDPQ IIgTUoWgr0/h2/iqv+qcKbqvInG1NJ5XTRFDd1SHKRbh+UPSQu4KgYhJ+hb0JbNQPAE9 Rnng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=SpdLdjXPNNhl9wZEIGsluiDA75HnwRkDLJ0hyfNw524=; b=PDKgWJfOaqmWhKFQb2wDGeCqAd/B/b8PmkhA/yINsrfF2wr1NGZdU7QWLX6BqlwCwO DNrrNpySpxiMkjDhRhWsLDI5FD95kdyO6/v1ESAPhnqS9moB3+8I4EkJt+q9cpty8lta ef44tjop5hktFoUDeiJQREQxYImJNiAmGnam6DEIBrwRt2Is07LaVOwCV0UnK58C7IDT jtH8LBL0wkd9gQbObXxCWMqpWAzvE+MMWeO/Nm+oZj0jPQ75B+F3rWj2Kxc1sHE4LUKq jeYjJHLc9sz4l54b8I4rGp5Jb+Cl6qYW0Y+77E4VN36jknmXtVJz5hwZPGXBTgMb437J ujqA== X-Gm-Message-State: AOAM533R+yjRO0YQLm7oHQLHwbbKLkks4u+OIDBMpZFCQG93r4b386rj Gybh45s5MvdtsDc/M7f+wNBvsghYegw= X-Google-Smtp-Source: ABdhPJz0YhX1hkBr0YJh3SNPRM0IKCsv7wp5ZfOtSE+e0NjzuKVwEpYI/kv5sNqlISvMSKALs+bCp5wCQg4= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:ae25:: with SMTP id a37mr7393055ybj.253.1626194055577; Tue, 13 Jul 2021 09:34:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:00 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-23-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 22/46] KVM: VMX: Remove direct write to vcpu->arch.cr0 during vCPU RESET/INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Remove a bogus write to vcpu->arch.cr0 that immediately precedes vmx_set_cr0() during vCPU RESET/INIT. For RESET, this is a nop since the "old" CR0 value is meaningless. But for INIT, if the vCPU is coming from paging enabled mode, crushing vcpu->arch.cr0 will cause the various is_paging() checks in vmx_set_cr0() to get false negatives. For the exit_lmode() case, the false negative is benign as vmx_set_efer() is called immediately after vmx_set_cr0(). For EPT without unrestricted guest, the false negative will cause KVM to unnecessarily run with CR3 load/store exiting. But again, this is benign, albeit sub-optimal. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 02aec75ec6f6..ed631564c651 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4383,7 +4383,6 @@ static void init_vmcs(struct vcpu_vmx *vmx) static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) { struct vcpu_vmx *vmx = to_vmx(vcpu); - u64 cr0; if (!init_event) init_vmcs(vmx); @@ -4454,9 +4453,7 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); - cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; - vmx->vcpu.arch.cr0 = cr0; - vmx_set_cr0(vcpu, cr0); /* enter rmode */ + vmx_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); vmx_set_cr4(vcpu, 0); vmx_set_efer(vcpu, 0); From patchwork Tue Jul 13 16:33:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374565 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ABFEEC07E95 for ; Tue, 13 Jul 2021 16:34:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 95EEF60FF2 for ; Tue, 13 Jul 2021 16:34:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234788AbhGMQhj (ORCPT ); Tue, 13 Jul 2021 12:37:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234850AbhGMQhV (ORCPT ); Tue, 13 Jul 2021 12:37:21 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6600DC061788 for ; Tue, 13 Jul 2021 09:34:18 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id x15-20020a25ce0f0000b029055bb0981111so27836293ybe.7 for ; Tue, 13 Jul 2021 09:34:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=ALNwA0nn2jLAQkKVjta8iUWZIOtO+j8qUemSoasWZMM=; b=tsKrHbB6gm1pLlzWOE5P/IRXR75BgxSuXuWVMouLiiTJ1WzDTscsVtINWPa43tZ5BD jAAZb2xZjUDp1pu1gd5X9ZzGy2QB0llRnWqPUu41NVnV+Utj6vmWcIOVkRCRTAm6Wgob 7Edon0eyQ6UzWs9KN+QKVBD/X2uXu/wxcW9dIwZMvEtwN6QupwXnc3Ayt2w9DBwYyEU0 YB2pUdfAdaYmM9gsELj58ApUQ8TesWVWFAl+rTB6IbvgLkaGOv3tqAPhC5zJyWBWnwD5 TQ9Z4s3JZd8NetNeY2i+uP4XWNIHL6TptPfh9BdSZDVJZWYcf5+2ZPJH4m6f3l1iuACd u1ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=ALNwA0nn2jLAQkKVjta8iUWZIOtO+j8qUemSoasWZMM=; b=CmhRy8IzT+ro2pQsA3I/arJ0cV7ITdw6UAYaj39KqgjGSYdJzp5rJJDnW0hVzPlWoJ 5TP4XNxT3PJcF8s4T4/wEDHTvzXHZlGIF5egTsLn5vNPqAQ7kvIhMegom+lVvKyt5BY4 HSN8yyXuGacRU+hnerSYBIKmUGOCngIKoPCV5gvnOVYhIAC7CN0Cdn5fcHcIiIoNpREC fEO2w7ZNNnlPRSztoIIncvMeJYlXtsnOiZJjGB7UXARWaItxMoomaXmjcGpaF2H6cE2l v9Xgqws1KYdXT1kL3fO7dWWSCfwM9XI6HBH7MZ6USa0/o31Nb1/haMsIkcEVUcKwHkOy TqaA== X-Gm-Message-State: AOAM532c+aR9lcY730gpBLAG+9QNHIspSoRUEIHGNqXG8oBPfmU7meCU t2LdonmCpnTNrZMjtvp6c3LMCmmjA24= X-Google-Smtp-Source: ABdhPJyIhV0xW5ndqPkBZyaoFrVwHHdjADouAB+hvpbP8ojYPVZtH7kxt44HS/FDUwcOTc564glOCn43Ku8= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:df06:: with SMTP id w6mr7173312ybg.361.1626194057638; Tue, 13 Jul 2021 09:34:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:01 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-24-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 23/46] KVM: VMX: Fold ept_update_paging_mode_cr0() back into vmx_set_cr0() From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Move the CR0/CR3/CR4 shenanigans for EPT without unrestricted guest back into vmx_set_cr0(). This will allow a future patch to eliminate the rather gross stuffing of vcpu->arch.cr0 in the paging transition cases by snapshotting the old CR0. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index ed631564c651..db70fe463aa1 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2994,27 +2994,6 @@ void ept_save_pdptrs(struct kvm_vcpu *vcpu) kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); } -static void ept_update_paging_mode_cr0(unsigned long cr0, struct kvm_vcpu *vcpu) -{ - struct vcpu_vmx *vmx = to_vmx(vcpu); - - if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3)) - vmx_cache_reg(vcpu, VCPU_EXREG_CR3); - if (!(cr0 & X86_CR0_PG)) { - /* From paging/starting to nonpaging */ - exec_controls_setbit(vmx, CPU_BASED_CR3_LOAD_EXITING | - CPU_BASED_CR3_STORE_EXITING); - vcpu->arch.cr0 = cr0; - vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); - } else if (!is_paging(vcpu)) { - /* From nonpaging to paging */ - exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING | - CPU_BASED_CR3_STORE_EXITING); - vcpu->arch.cr0 = cr0; - vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); - } -} - void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) { struct vcpu_vmx *vmx = to_vmx(vcpu); @@ -3044,8 +3023,23 @@ void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) } #endif - if (enable_ept && !is_unrestricted_guest(vcpu)) - ept_update_paging_mode_cr0(cr0, vcpu); + if (enable_ept && !is_unrestricted_guest(vcpu)) { + if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3)) + vmx_cache_reg(vcpu, VCPU_EXREG_CR3); + if (!(cr0 & X86_CR0_PG)) { + /* From paging/starting to nonpaging */ + exec_controls_setbit(vmx, CPU_BASED_CR3_LOAD_EXITING | + CPU_BASED_CR3_STORE_EXITING); + vcpu->arch.cr0 = cr0; + vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); + } else if (!is_paging(vcpu)) { + /* From nonpaging to paging */ + exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING | + CPU_BASED_CR3_STORE_EXITING); + vcpu->arch.cr0 = cr0; + vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); + } + } vmcs_writel(CR0_READ_SHADOW, cr0); vmcs_writel(GUEST_CR0, hw_cr0); From patchwork Tue Jul 13 16:33:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374567 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7362CC11F67 for ; Tue, 13 Jul 2021 16:34:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5DC1160FF3 for ; Tue, 13 Jul 2021 16:34:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235475AbhGMQhl (ORCPT ); Tue, 13 Jul 2021 12:37:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234911AbhGMQhY (ORCPT ); Tue, 13 Jul 2021 12:37:24 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15ACBC0613E9 for ; Tue, 13 Jul 2021 09:34:20 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id g3-20020a256b030000b0290551bbd99700so27770161ybc.6 for ; Tue, 13 Jul 2021 09:34:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=xdAkcygaPEcpp4CNM8O/R9k6BDNdm0ATuqQsXeEt38E=; b=BxOhaKklb4xqr+QX8WDrqI3aFBnGaDnXY21Deh5ubB3ZAKGYD37cbdfvfm36hH93kR lQDSbXnaqpxUuUwXhZXMbzXgDOEOhHdcAAFtwn8I3qw9bNAYyvbz69+Ba7NW+v2ja7tl 7EHvZBIwvviPZWxRdpdUwWh6VjSlBL0yudtDitGZJOkVNBOzwzMNThiydO54XUsAV88I jTqwamhMr90yjJkWgYh9q8RuUGrONnxkLjB1Mw6wYc6WjRrDx2IwWAcWwOgkdCKk42Yd OVLrzGRm3pp7LPzExfemC9WHeslyX9ujzoKgNdAWJogTE2juRQ+Zb1sjhFkdh+vfvB2E YUjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=xdAkcygaPEcpp4CNM8O/R9k6BDNdm0ATuqQsXeEt38E=; b=URoPbJbsgm7buYEilmy5FIG7xcSbv0n9f6j9w6/CD17+1H7piJUAIkiHH8zmDgL1SJ 0vsSrqnZmhLLE7/n6cRBWOIRUNY2Ga3tJkN594KRhd9fm5HYnviXnbC8KqG8w0KBYpTd KZoQVmpPtdOHneCWBx+CXwKorwtHxnf0yjQ1sGWdpxTvlT8ipylZNYT3D+KE6BFbH7WX VEyZ3IA5Vr6WYI7iGnb8lNsmw2jRGz9pgg2DnaT6YKMVHkdPHxsHE9aZ6gQH+sTbGS1x 1ZIWR1KuLJUfcEw+v0z4/pn9iEbwCAGg082idWeZ6ULPSOE/vOt0sM+68R7mQq8JkBW/ y/vQ== X-Gm-Message-State: AOAM5306nPIXV6unOfKHwOQzfkvqoqV8Otxs8TOdF+aPZ1tVga5Ll7nd 6KWNm8BsaDgq0BYw9YMdF5cJnammrAA= X-Google-Smtp-Source: ABdhPJyWC6nAKsLCUaj4djllb/OFERwatUMnjg5LpWOKBrpAkn05V353L2nG5W05C3HlAoSn9h9MIXjbBEU= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:d310:: with SMTP id e16mr7596128ybf.63.1626194059316; Tue, 13 Jul 2021 09:34:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:02 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-25-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 24/46] KVM: nVMX: Do not clear CR3 load/store exiting bits if L1 wants 'em From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Keep CR3 load/store exiting enable as needed when running L2 in order to honor L1's desires. This fixes a largely theoretical bug where L1 could intercept CR3 but not CR0.PG and end up not getting the desired CR3 exits when L2 enables paging. In other words, the existing !is_paging() check inadvertantly handles the normal case for L2 where vmx_set_cr0() is called during VM-Enter, which is guaranteed to run with paging enabled, and thus will never clear the bits. Removing the !is_paging() check will also allow future consolidation and cleanup of the related code. From a performance perspective, this is all a nop, as the VMCS controls shadow will optimize away the VMWRITE when the controls are in the desired state. Add a comment explaining why CR3 is intercepted, with a big disclaimer about not querying the old CR3. Because vmx_set_cr0() is used for flows that are not directly tied to MOV CR3, e.g. vCPU RESET/INIT and nested VM-Enter, it's possible that is_paging() is not synchronized with CR3 load/store exiting. This is actually guaranteed in the current code, as KVM starts with CR3 interception disabled. Obviously that can be fixed, but there's no good reason to play whack-a-mole, and it tends to end poorly, e.g. descriptor table exiting for UMIP emulation attempted to be precise in the past and ended up botching the interception toggling. Fixes: fe3ef05c7572 ("KVM: nVMX: Prepare vmcs02 from vmcs01 and vmcs12") Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 46 +++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index db70fe463aa1..58c6d7b98624 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2994,10 +2994,14 @@ void ept_save_pdptrs(struct kvm_vcpu *vcpu) kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); } +#define CR3_EXITING_BITS (CPU_BASED_CR3_LOAD_EXITING | \ + CPU_BASED_CR3_STORE_EXITING) + void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) { struct vcpu_vmx *vmx = to_vmx(vcpu); unsigned long hw_cr0; + u32 tmp; hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF); if (is_unrestricted_guest(vcpu)) @@ -3024,18 +3028,42 @@ void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) #endif if (enable_ept && !is_unrestricted_guest(vcpu)) { + /* + * Ensure KVM has an up-to-date snapshot of the guest's CR3. If + * the below code _enables_ CR3 exiting, vmx_cache_reg() will + * (correctly) stop reading vmcs.GUEST_CR3 because it thinks + * KVM's CR3 is installed. + */ if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3)) vmx_cache_reg(vcpu, VCPU_EXREG_CR3); + + /* + * When running with EPT but not unrestricted guest, KVM must + * intercept CR3 accesses when paging is _disabled_. This is + * necessary because restricted guests can't actually run with + * paging disabled, and so KVM stuffs its own CR3 in order to + * run the guest when identity mapped page tables. + * + * Do _NOT_ check the old CR0.PG, e.g. to optimize away the + * update, it may be stale with respect to CR3 interception, + * e.g. after nested VM-Enter. + * + * Lastly, honor L1's desires, i.e. intercept CR3 loads and/or + * stores to forward them to L1, even if KVM does not need to + * intercept them to preserve its identity mapped page tables. + */ if (!(cr0 & X86_CR0_PG)) { - /* From paging/starting to nonpaging */ - exec_controls_setbit(vmx, CPU_BASED_CR3_LOAD_EXITING | - CPU_BASED_CR3_STORE_EXITING); - vcpu->arch.cr0 = cr0; - vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); - } else if (!is_paging(vcpu)) { - /* From nonpaging to paging */ - exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING | - CPU_BASED_CR3_STORE_EXITING); + exec_controls_setbit(vmx, CR3_EXITING_BITS); + } else if (!is_guest_mode(vcpu)) { + exec_controls_clearbit(vmx, CR3_EXITING_BITS); + } else { + tmp = exec_controls_get(vmx); + tmp &= ~CR3_EXITING_BITS; + tmp |= get_vmcs12(vcpu)->cpu_based_vm_exec_control & CR3_EXITING_BITS; + exec_controls_set(vmx, tmp); + } + + if (!is_paging(vcpu) != !(cr0 & X86_CR0_PG)) { vcpu->arch.cr0 = cr0; vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); } From patchwork Tue Jul 13 16:33:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374569 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14A61C07E95 for ; Tue, 13 Jul 2021 16:34:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 00A3360FEE for ; Tue, 13 Jul 2021 16:34:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235399AbhGMQhm (ORCPT ); Tue, 13 Jul 2021 12:37:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235293AbhGMQh2 (ORCPT ); Tue, 13 Jul 2021 12:37:28 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18FA2C0613AC for ; Tue, 13 Jul 2021 09:34:22 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id h1-20020a255f410000b02905585436b530so27738425ybm.21 for ; Tue, 13 Jul 2021 09:34:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=CgrSZBstNZwFy9Uo6jdcpFw84xBpoqNrT77cRxMhd+E=; b=dIYjzPGsJ6TLn2wVcO+Ux1e9QGqStPI+HZp8nyXUUAtU47hnu0NRDZeyBPGX0xFdOO CXTsHUJY33F6lOorYUZ0Llq6fkPEaeM4tReHGBoya2vw8YVLMng/27ngwKQy82UJdyvq 41l7qU7Vk53muwMcq7TtV5Zh1c4lwHBklW7C9ce/5AfBsbN+9oAHO5/C60HoBLd8np7H K/rngvmcWlQFtWxii1lpF4dxVsiOTrn6nxvHagBhdOuPiTMMRZs7CM3/jFZazKs2aWkV G0kawCHRm+J0IE3KlcNr1SEAFRDRvWLkCRIksCBe0P6T4ZNiuZ0wsS2VYZwSWvUzLt0z qa6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=CgrSZBstNZwFy9Uo6jdcpFw84xBpoqNrT77cRxMhd+E=; b=Zl6fY8wg2w+rRlWNsnrBpr0FSF9BrC2G37/bBYSDK+AOxb1FO4RZpsFjHqpSDGybmh Fjodya8lr8g+xU4LhiJ5le9LOWCwU2xWMeX83xkbERkbH1afSGWE8yO4EMUUcMzS0z4a 5SbwO4BDNl5jzsH0TNRTzMMKhJ09wM/VJ1T45VnfJP+IAHIRhT6L8UGG5CLt54Xm6wa0 U54y1FERwTSCS6uz9vs2PvMe5OiYlslXPa5EZdY3pJ3kqJ/PHVWI2l0mBAIGI0MZX5eJ oHKlDmotNi6qvwYiXEIvMGG5Ln3IOopcaB5QV0PWMsoeWWD1Qd79aW+M7iiDzAFu1R/P 16RA== X-Gm-Message-State: AOAM533NnCcjm982DWm/gSSqk4f2WcC5XUTQCEDXkai0k2pqSImj8tgC DK7azB2f6eHRex/oO8caKEQcNomvySk= X-Google-Smtp-Source: ABdhPJyAx6+D/2SdrYQx5dm7fHrEFUIbnOPmhfqtgcFzKyqmQ1LUZK792ZNmUj9molVy3SElH+EwUtvhOuY= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:258:: with SMTP id 85mr7222066ybc.109.1626194061338; Tue, 13 Jul 2021 09:34:21 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:03 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-26-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 25/46] KVM: VMX: Pull GUEST_CR3 from the VMCS iff CR3 load exiting is disabled From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Tweak the logic for grabbing vmcs.GUEST_CR3 in vmx_cache_reg() to look directly at the execution controls, as opposed to effectively inferring the controls based on vCPUs. Inferring the controls isn't wrong, but it creates a very subtle dependency between the caching logic, the state of vcpu->arch.cr0 (via is_paging()), and the behavior of vmx_set_cr0(). Using the execution controls doesn't completely eliminate the dependency in vmx_set_cr0(), e.g. neglecting to cache CR3 before enabling interception would still break the guest, but it does reduce the code dependency and mostly eliminate the logical dependency (that CR3 loads are intercepted in certain scenarios). Eliminating the subtle read of vcpu->arch.cr0 will also allow for additional cleanup in vmx_set_cr0(). Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 58c6d7b98624..d632c0a16f12 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2262,8 +2262,11 @@ static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & guest_owned_bits; break; case VCPU_EXREG_CR3: - if (is_unrestricted_guest(vcpu) || - (enable_ept && is_paging(vcpu))) + /* + * When intercepting CR3 loads, e.g. for shadowing paging, KVM's + * CR3 is loaded into hardware, not the guest's CR3. + */ + if (!(exec_controls_get(to_vmx(vcpu)) & CPU_BASED_CR3_LOAD_EXITING)) vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); break; case VCPU_EXREG_CR4: From patchwork Tue Jul 13 16:33:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374571 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D4B3C07E96 for ; Tue, 13 Jul 2021 16:35:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 574DF61376 for ; Tue, 13 Jul 2021 16:35:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235570AbhGMQhw (ORCPT ); Tue, 13 Jul 2021 12:37:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235075AbhGMQhj (ORCPT ); Tue, 13 Jul 2021 12:37:39 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D82DAC0613B6 for ; Tue, 13 Jul 2021 09:34:23 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id q10-20020a056902150ab02905592911c932so27952341ybu.15 for ; Tue, 13 Jul 2021 09:34:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=Uj2yMP906hWJGI2naVvWw65tlAlIQnpXZj8DT6KTFYY=; b=BxKkSE4nzeAN82oc/h3WNbMOSRY3kLdlaT0lHNjn7J4r+J+n8aJkIo3ROQbdwAp/yH 0ZINHZQZNXvGy2cM95myujd5l78dY2Tp1gB8s9W351mOgogMtBCeIUtqk1kBT5iXq0qD R9NRqcVKVpWgTxsU2jc9PhQejQngk8QL5FmFxA8zChtI0o/mFvRWRjqIhaUJq3/zmDvw K2jjCA7L3U+jh1vFCyx9Da+qHBAOKUAil5rAkUGPsAmCMaJnJP2BQe+sanJ0HRjV+nLj PMzoupGosc0XCIFqqcYrkAD0LHta7Xj18h701jf9TC+e+1whDLbXelZVJOLif7Zqym5i +H6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=Uj2yMP906hWJGI2naVvWw65tlAlIQnpXZj8DT6KTFYY=; b=cuKphMpZKSUCSTPCUc45tyrLDn2khnhfqWu4jL+utX3+bxa5iyd/92GagoMbczPd5U tCcRHjkRRUC2t37JU3v6LrdMX/SBBbD/05R20xF35HxJ5BsqpBK59taQZAvDkkWOHgXL JMtyfwXgOk6az/3gqJcCD579NPm0q68fWpYTaxbO03he9ez+yWlbmeUl/4J+/9tq4mt2 evuc0YM04yvzQugYTTrUpQCdnny2muotJmyUayJwyS91Chgsy3ozyY+xc8IjN227wUGC WBAoYcJ4pU/70Iwc5C41jXzTyDCuu+F0//gF81Lw2neq0vOESJffKAsH5RvsV20/xrbM jNIg== X-Gm-Message-State: AOAM533YGYtkd5ymlHClrb3lWSThlfOtXpmbAe2ywkL89R55eCjYd/aa mdU4sPVfEo4/nbiGNn6i1Tg5dsfX8iM= X-Google-Smtp-Source: ABdhPJyBu4WEipwbM+p875QlkcqwEMGe7LqGyGF0Q+kW0j9wqkKUWt9cLevGQ/uZA/fwiAZ3QM+IQGUEZ0Y= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a5b:303:: with SMTP id j3mr6439721ybp.433.1626194063094; Tue, 13 Jul 2021 09:34:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:04 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-27-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 26/46] KVM: x86/mmu: Skip the permission_fault() check on MMIO if CR0.PG=0 From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Skip the MMU permission_fault() check if paging is disabled when verifying the cached MMIO GVA is usable. The check is unnecessary and can theoretically get a false positive since the MMU doesn't zero out "permissions" or "pkru_mask" when guest paging is disabled. The obvious alternative is to zero out all the bitmasks when configuring nonpaging MMUs, but that's unnecessary work and doesn't align with the MMU's general approach of doing as little as possible for flows that are supposed to be unreachable. This is nearly a nop as the false positive is nothing more than an insignificant performance blip, and more or less limited to string MMIO when L1 is running with paging disabled. KVM doesn't cache MMIO if L2 is active with nested TDP since the "GVA" is really an L2 GPA. If L2 is active without nested TDP, then paging can't be disabled as neither VMX nor SVM allows entering the guest without paging of some form. Jumping back to L1 with paging disabled, in that case direct_map is true and so KVM will use CR2 as a GPA; the only time it doesn't is if the fault from the emulator doesn't match or emulator_can_use_gpa(), and that fails only on string MMIO and other instructions with multiple memory operands. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index fd9026437fdd..6a11ec5d38ac 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6557,9 +6557,9 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, * there is no pkey in EPT page table for L1 guest or EPT * shadow page table for L2 guest. */ - if (vcpu_match_mmio_gva(vcpu, gva) - && !permission_fault(vcpu, vcpu->arch.walk_mmu, - vcpu->arch.mmio_access, 0, access)) { + if (vcpu_match_mmio_gva(vcpu, gva) && (!is_paging(vcpu) || + !permission_fault(vcpu, vcpu->arch.walk_mmu, + vcpu->arch.mmio_access, 0, access))) { *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | (gva & (PAGE_SIZE - 1)); trace_vcpu_match_mmio(gva, *gpa, write, false); From patchwork Tue Jul 13 16:33:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374573 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51241C07E95 for ; Tue, 13 Jul 2021 16:35:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3A9B361003 for ; Tue, 13 Jul 2021 16:35:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235413AbhGMQhy (ORCPT ); Tue, 13 Jul 2021 12:37:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235114AbhGMQhk (ORCPT ); Tue, 13 Jul 2021 12:37:40 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0924C0611C4 for ; Tue, 13 Jul 2021 09:34:25 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id o11-20020a056902110bb029055b266be219so27630092ybu.13 for ; Tue, 13 Jul 2021 09:34:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=kqLCR8V7aHa3dOA8rm3sPsU8TE7Ylyd+IJ7q57+xdpQ=; b=L0uvO4Nv9mAWXOcCpamXUEqDt0dP2pLlpducZY/pkFLDNBb6CisQgY7KLoL4NAcPOt RQ3XWNG9KLTBSp+/jdBa2P2c1hCV+9Au3q5140kAxu1K2Rf3LyTPI68qZvgOWJljZZhq ICuDR47ZPk0YAsRREQ6ml0daxIhdHSGWt347Fm5yM1H/OYZP5G6GYrVYoodnbEolG/FG 78tilrrB2SzFuOiAKU+pZsbwv/bEhk2d47GdogRw2w9gfFX1btQ8clLivylD4I/68S5q RM1WZv4axMrGE2U1gDQIZ03n8cZbE4Gd4QHuPEgw0bya3aldE4qeF1vtZ5pqZa6hJcrJ PZXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=kqLCR8V7aHa3dOA8rm3sPsU8TE7Ylyd+IJ7q57+xdpQ=; b=l7TJF+K6NUr1gwfvxw9oN0czETLcP/bbBW7FIRCJzb90K5R6iKInW4fd9NBkdLpZRY sD9DfLtUnfzqTEWQS7kjupe1JyeTaur3PlhVczuWeEn1CFG5tvYor2DdgMxhQYm7VlkQ E9GzVxdFpEQJsx0GpQRbCew67z6COm9unc5wp6067yshIIQHssrJ3eBndD2HmZJEBtcI OMtFWtIRP/qd2PH42B6nYn92KNe3Xx/YJo+TXiXd3lAG6Sj7V2URqd7s0RkhHQQcDtoY qqkrpJYX93rIBQc0fapn/F0lCtooIMktSO20/DtfHgPHrdZCAA5uYGzUhh3+y/y9Pf3K IVPw== X-Gm-Message-State: AOAM531ux8BJ3yNF0g1hhpVRz5dsioQQr4vZuWO/ruNOIu8BQ4A99a0F ibbp5EasDfCrlX08teH5FGpfnx95zTA= X-Google-Smtp-Source: ABdhPJyNNaY8GZpqQWIROfW9H0Rs0iusOrbZZt+VBtotb51SY27UM28YNDro4AqvL8BXrBUDivtbq+/vp5w= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:be8a:: with SMTP id i10mr6997224ybk.176.1626194065180; Tue, 13 Jul 2021 09:34:25 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:05 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-28-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 27/46] KVM: VMX: Process CR0.PG side effects after setting CR0 assets From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Move the long mode and EPT w/o unrestricted guest side effect processing down in vmx_set_cr0() so that the EPT && !URG case doesn't have to stuff vcpu->arch.cr0 early. This also fixes an oddity where CR0 might not be marked available, i.e. the early vcpu->arch.cr0 write would appear to be in danger of being overwritten, though that can't actually happen in the current code since CR0.TS is the only guest-owned bit, and CR0.TS is not read by vmx_set_cr4(). Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index d632c0a16f12..45b123bb5aaa 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -3003,9 +3003,11 @@ void ept_save_pdptrs(struct kvm_vcpu *vcpu) void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) { struct vcpu_vmx *vmx = to_vmx(vcpu); - unsigned long hw_cr0; + unsigned long hw_cr0, old_cr0_pg; u32 tmp; + old_cr0_pg = kvm_read_cr0_bits(vcpu, X86_CR0_PG); + hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF); if (is_unrestricted_guest(vcpu)) hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; @@ -3021,11 +3023,16 @@ void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) enter_rmode(vcpu); } + vmcs_writel(CR0_READ_SHADOW, cr0); + vmcs_writel(GUEST_CR0, hw_cr0); + vcpu->arch.cr0 = cr0; + kvm_register_mark_available(vcpu, VCPU_EXREG_CR0); + #ifdef CONFIG_X86_64 if (vcpu->arch.efer & EFER_LME) { - if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) + if (!old_cr0_pg && (cr0 & X86_CR0_PG)) enter_lmode(vcpu); - if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) + else if (old_cr0_pg && !(cr0 & X86_CR0_PG)) exit_lmode(vcpu); } #endif @@ -3066,17 +3073,11 @@ void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) exec_controls_set(vmx, tmp); } - if (!is_paging(vcpu) != !(cr0 & X86_CR0_PG)) { - vcpu->arch.cr0 = cr0; + /* Note, vmx_set_cr4() consumes the new vcpu->arch.cr0. */ + if ((old_cr0_pg ^ cr0) & X86_CR0_PG) vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); - } } - vmcs_writel(CR0_READ_SHADOW, cr0); - vmcs_writel(GUEST_CR0, hw_cr0); - vcpu->arch.cr0 = cr0; - kvm_register_mark_available(vcpu, VCPU_EXREG_CR0); - /* depends on vcpu->arch.cr0 to be set to a new value */ vmx->emulation_required = emulation_required(vcpu); } From patchwork Tue Jul 13 16:33:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374575 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4F77CC11F66 for ; Tue, 13 Jul 2021 16:35:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3AB6960FF3 for ; Tue, 13 Jul 2021 16:35:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235709AbhGMQh5 (ORCPT ); Tue, 13 Jul 2021 12:37:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234911AbhGMQhl (ORCPT ); Tue, 13 Jul 2021 12:37:41 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DD1DC06119D for ; Tue, 13 Jul 2021 09:34:28 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id c13-20020a25880d0000b029055492c8987bso27621700ybl.19 for ; Tue, 13 Jul 2021 09:34:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=cW8BIVTxIgKroIq7kMrutoUZpTyYwV90yWvpiIBilPc=; b=kjC+N6ZAjKJ76JOx9gw1poeBnbs7f2TUs96FtDfxukpM4jgMbWSbGEY4ji86iiS3da KjaDmuQnpnv9YMt0GJJ5mLcyddJ7cyucks0rzrSCc97QJHriPtUTLgcgZQhXR/wREnsB Hi8yzV5oxqD0ZATDXTpeX9dFpPq/SOKOg8i+1OyvFYVtItvfEhM7UWBJVwYyd4IHtA3C xrFrNdRpDpeBZT95BiCTp8u2oyOjjhaGXDOgpVf2RS/8KwKEahNN5ih+bY0W8Ufey0J6 hrYhTvbqsehiS8KNt5AnCLByJtyZ3T99WFm7TuCPhZl6eakojNa4eikhhB847boP3n5s gI/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=cW8BIVTxIgKroIq7kMrutoUZpTyYwV90yWvpiIBilPc=; b=MlAcgpP5o0L1Mm48VB/lWgFDpJCf9OTs30ELJZiO6wFNqupd+tZeY9XYwbOPm3W/PZ gbaB0AW7TdKEPM5WGHbSnGsQKAOx/wkQzN+Ua4s28IaDZG8R9jGLWfvElMYHPM/MUoMb dfVOc/T7VW+nD5Y1S0if00xR7aUNXrtV84y32Iad+8uq5nQt84PKoMCPVe6JxDvtYGIl egBOCBZHdkH+ps6cl2U9ndSwVM86+fLqGYpR6F4E8oJMdqGpeX9hvY8TBinAgLBVSOWl a9O8GuFa80gS/7LCGagAsQRG0Aq7Org95zciJed0psjTVxW1Z8cQzHD2xPR0QhCGgcTC 5ImA== X-Gm-Message-State: AOAM530TdO2cciC6LerJxV6uJGGsPiHJhDhkMCeFgoUyo0vSPHdS2PyH gmTCfNpZNVgYv+Knqr5Ag4CQYDa9JDs= X-Google-Smtp-Source: ABdhPJxxEY5h40hwjxnBrqRbvxMDcqYn0Pq1pp/svYskxe9JHvsyD6AN1JmqX+UdGZbHRnRL801DTneK6PU= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:1e57:: with SMTP id e84mr7107672ybe.308.1626194067266; Tue, 13 Jul 2021 09:34:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:06 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-29-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 28/46] KVM: VMX: Skip emulation required checks during pmode/rmode transitions From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Don't refresh "emulation required" when stuffing segments during transitions to/from real mode when running without unrestricted guest. The checks are unnecessary as vmx_set_cr0() unconditionally rechecks "emulation required". They also happen to be broken, as enter_pmode() and enter_rmode() run with a stale vcpu->arch.cr0. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 45b123bb5aaa..7ab493708b06 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2719,6 +2719,8 @@ static __init int alloc_kvm_area(void) return 0; } +static void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg); + static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, struct kvm_segment *save) { @@ -2735,7 +2737,7 @@ static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, save->dpl = save->selector & SEGMENT_RPL_MASK; save->s = 1; } - vmx_set_segment(vcpu, save, seg); + __vmx_set_segment(vcpu, save, seg); } static void enter_pmode(struct kvm_vcpu *vcpu) @@ -2756,7 +2758,7 @@ static void enter_pmode(struct kvm_vcpu *vcpu) vmx->rmode.vm86_active = 0; - vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); + __vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); flags = vmcs_readl(GUEST_RFLAGS); flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; @@ -3291,7 +3293,7 @@ static u32 vmx_segment_access_rights(struct kvm_segment *var) return ar; } -void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) +static void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) { struct vcpu_vmx *vmx = to_vmx(vcpu); const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; @@ -3304,7 +3306,7 @@ void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) vmcs_write16(sf->selector, var->selector); else if (var->s) fix_rmode_seg(seg, &vmx->rmode.segs[seg]); - goto out; + return; } vmcs_writel(sf->base, var->base); @@ -3326,9 +3328,13 @@ void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) var->type |= 0x1; /* Accessed */ vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); +} -out: - vmx->emulation_required = emulation_required(vcpu); +void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) +{ + __vmx_set_segment(vcpu, var, seg); + + to_vmx(vcpu)->emulation_required = emulation_required(vcpu); } static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) From patchwork Tue Jul 13 16:33:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374577 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0AB6C07E95 for ; Tue, 13 Jul 2021 16:35:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D60F860FF4 for ; Tue, 13 Jul 2021 16:35:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235397AbhGMQiH (ORCPT ); Tue, 13 Jul 2021 12:38:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235671AbhGMQhz (ORCPT ); Tue, 13 Jul 2021 12:37:55 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FE88C0617A6 for ; Tue, 13 Jul 2021 09:34:30 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id p63-20020a25d8420000b029055bc6fd5e5bso27638049ybg.9 for ; Tue, 13 Jul 2021 09:34:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=TAL/n8RnNwdD8AwDKd/laGABPuxqZ6wUJPdjHXeK3wU=; b=Tr7xd27FqJinXV9I+T++c9rT4QfnImlcAvns5MU0AaTRwComalb5fSUqdheriwG/7N NU0zoFwh7c2Aak7fekov/B3SRExinrkiNAINVwwZ//nqu4kWDT/nCxV1WYXPFAEiBfcO pOTfu7td7Rqwl88FOQK2db0Ul+VpOY3LMKpQNAhgjIggJvpv+uFkrhaMY3pSh0kBl+dH sYKkTSAFNm1ThFueKxi8XVz/4JyF3v2QptuyhYhFvPH1zx/OaMK3araEOz+zI0LCn9OK weA3k8XskRDIylCsLInLhe2sTI9Qfsd/JBZnzJCHzLZGqMVvYYOJzgI9RGE/15AJUBfo AY9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=TAL/n8RnNwdD8AwDKd/laGABPuxqZ6wUJPdjHXeK3wU=; b=mTpI/YSBXX+kStNYVj8IES3919pnKF+2eY194eRYAHUTUfRuwraXX54wzz7OW7k3YJ J8K+83Z1XwyYpfGmUnrgIzkU0eKreHodBOQvppu0qePkErwXg9jqxTVykZUzbLJJzSsZ XW4i4+2WjlrfcxiwthZr8JcPek6fUV6KSfC+INalHKGhOTvhc2JpBqwcyIsrpbbFEGcL U3EBcmOIznnmsb/miJXJG7mv+uPMnNhz2hNIu/VAy37DgvSR12Yez7flUlHKZMW3x11p fN2U3Fyp0pvj0BLEZFmcqS/MO5Dah/uVKL1MkFAOJVf4zKV8wSTcPg90gdleUmWrTBO4 FKZA== X-Gm-Message-State: AOAM533x04pxq9cFUSd9EQ+cgMr7Sh22aCFnxumEW1tRCrTeKI3bQjHG dOJHfSj4SdnsCsqaOLRjkVxbC3y6oOA= X-Google-Smtp-Source: ABdhPJyCKJLetM0jt9gxVmJZdJLGtEJ5Zh7Ru2a2KvBCilNSVSWuC4qgRlH2IPTLc4+pXas/nnyBw8B+1w4= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:7c7:: with SMTP id 190mr7065632ybh.194.1626194069449; Tue, 13 Jul 2021 09:34:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:07 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-30-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 29/46] KVM: nVMX: Don't evaluate "emulation required" on nested VM-Exit From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Use the "internal" variants of setting segment registers when stuffing state on nested VM-Exit in order to skip the "emulation required" updates. VM-Exit must always go to protected mode, and all segments are mostly hardcoded (to valid values) on VM-Exit. The bits of the segments that aren't hardcoded are explicitly checked during VM-Enter, e.g. the selector RPLs must all be zero. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 16 ++++++++-------- arch/x86/kvm/vmx/vmx.c | 6 ++---- arch/x86/kvm/vmx/vmx.h | 2 +- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 7f8184f432b4..a77cfc8bcf11 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4267,7 +4267,7 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, seg.l = 1; else seg.db = 1; - vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); + __vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); seg = (struct kvm_segment) { .base = 0, .limit = 0xFFFFFFFF, @@ -4278,17 +4278,17 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, .g = 1 }; seg.selector = vmcs12->host_ds_selector; - vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); + __vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); seg.selector = vmcs12->host_es_selector; - vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); + __vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); seg.selector = vmcs12->host_ss_selector; - vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); + __vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); seg.selector = vmcs12->host_fs_selector; seg.base = vmcs12->host_fs_base; - vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); + __vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); seg.selector = vmcs12->host_gs_selector; seg.base = vmcs12->host_gs_base; - vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); + __vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); seg = (struct kvm_segment) { .base = vmcs12->host_tr_base, .limit = 0x67, @@ -4296,11 +4296,11 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, .type = 11, .present = 1 }; - vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); + __vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); memset(&seg, 0, sizeof(seg)); seg.unusable = 1; - vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR); + __vmx_set_segment(vcpu, &seg, VCPU_SREG_LDTR); kvm_set_dr(vcpu, 7, 0x400); vmcs_write64(GUEST_IA32_DEBUGCTL, 0); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 7ab493708b06..a1e5706fd27b 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2719,8 +2719,6 @@ static __init int alloc_kvm_area(void) return 0; } -static void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg); - static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, struct kvm_segment *save) { @@ -3293,7 +3291,7 @@ static u32 vmx_segment_access_rights(struct kvm_segment *var) return ar; } -static void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) +void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) { struct vcpu_vmx *vmx = to_vmx(vcpu); const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; @@ -3330,7 +3328,7 @@ static void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, in vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); } -void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) +static void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) { __vmx_set_segment(vcpu, var, seg); diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 3979a947933a..b584e41bed44 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -373,7 +373,7 @@ void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); void set_cr4_guest_host_mask(struct vcpu_vmx *vmx); void ept_save_pdptrs(struct kvm_vcpu *vcpu); void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg); -void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg); +void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg); u64 construct_eptp(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level); bool vmx_guest_inject_ac(struct kvm_vcpu *vcpu); From patchwork Tue Jul 13 16:33:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374579 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05BB6C07E96 for ; Tue, 13 Jul 2021 16:35:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DD6B960FF3 for ; Tue, 13 Jul 2021 16:35:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235186AbhGMQiN (ORCPT ); Tue, 13 Jul 2021 12:38:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235765AbhGMQiD (ORCPT ); Tue, 13 Jul 2021 12:38:03 -0400 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11548C0604DE for ; Tue, 13 Jul 2021 09:34:32 -0700 (PDT) Received: by mail-qv1-xf49.google.com with SMTP id z1-20020a0cfec10000b02902dbb4e0a8f2so12205015qvs.6 for ; Tue, 13 Jul 2021 09:34:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=g4KLbj+QdWVACsUllh3wnqDquVHXBzm/Vor2zYtx/bc=; b=VkYmfhAjybA7UVH++QHLbtB2zXv3ANgGvddIZcUFdBilVfldY6hi4Yx0m0vawC/nV7 ExC2v2DD8RWEYCg/4MQX/PJ9fueketZaoP3CTkmKMh7uk3c1IulHKU2W1eFQwXDJRp4F 7H3QxLOpX1tTrIk2iMxYOEosylHJC33I3V6EiydySNGCd5/ZpLOUVU/vKTv3JrifL3KS M8zRn+lpVIROZGnURtdwRA+/8rIL7AjHyxB15fd1rI7g5ZdvYYyULK5plKFU/EJIY2uK pFU4eEGy/vDJEMKSwQ0tqJgnFmZu96rDNu4Sq7wh7Q3Kz8A5Nk6Can7lL8+ODLlTN8OF JCOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=g4KLbj+QdWVACsUllh3wnqDquVHXBzm/Vor2zYtx/bc=; b=G0RG1yuZUOQnirQkolvm3MPdl0sZujCxsezfHAV6UuMc8Fqf/1F0wVRwR3ZtW89FI+ asCH4mN2TsgcDbuGiLX3nv/XiwwalOaKqP3/ZjbuQc5iTgWJE7MzwQuKze3hq6LawKc1 +y2Slm0zZ+kS5eUyNIp8b6doUcJB7ZZUnfXvDm3y+Mly5F6dOTOBvvOhGp/b1AwJF7LO G1kuzY2gVJWyBJN+gjj6feZfcCn7GvvE6U3m6Ri7EJ20o+Y1KP5kzglG3QlS6kmJxSuE wFb4TQ7CwinHvWYVZEDHB9cCf6fcLaTevn3UXUrnqX+/0u7hSkxqcj3a78GtVkHEkY2O btsg== X-Gm-Message-State: AOAM532jzFhtDSZz2yDkqYNFZ+fDnXhr/KcZxaGUBKFa3bssZBlefS9Z ZDyS2nBAwMml9b+qqeqirfB0hv5a8Oc= X-Google-Smtp-Source: ABdhPJzfAdfd5DYONg/CglEPN79CjRHf972Neu6xnVDPfEjPMPpTHH12YI8YjGso85YNoBNdm286wXZC1NQ= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a05:6214:4102:: with SMTP id kc2mr821831qvb.44.1626194071222; Tue, 13 Jul 2021 09:34:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:08 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-31-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 30/46] KVM: SVM: Tweak order of cr0/cr4/efer writes at RESET/INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Hoist svm_set_cr0() up in the sequence of register initialization during vCPU RESET/INIT, purely to match VMX so that a future patch can move the sequences to common x86. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 0101646e42e0..875d68c4cb9b 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1262,18 +1262,13 @@ static void init_vmcb(struct kvm_vcpu *vcpu) init_sys_seg(&save->ldtr, SEG_TYPE_LDT); init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); + svm_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); svm_set_cr4(vcpu, 0); svm_set_efer(vcpu, 0); save->dr6 = 0xffff0ff0; kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); vcpu->arch.regs[VCPU_REGS_RIP] = 0x0000fff0; - /* - * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0. - * It also updates the guest-visible cr0 value. - */ - svm_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); - save->cr4 = X86_CR4_PAE; if (npt_enabled) { From patchwork Tue Jul 13 16:33:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374583 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E3118C07E96 for ; Tue, 13 Jul 2021 16:35:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C98EA6100A for ; Tue, 13 Jul 2021 16:35:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234452AbhGMQiT (ORCPT ); Tue, 13 Jul 2021 12:38:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235172AbhGMQiD (ORCPT ); Tue, 13 Jul 2021 12:38:03 -0400 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA595C0617AB for ; Tue, 13 Jul 2021 09:34:33 -0700 (PDT) Received: by mail-qt1-x84a.google.com with SMTP id 12-20020ac8570c0000b02902520e309f5dso12870069qtw.8 for ; Tue, 13 Jul 2021 09:34:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=O8F2nWEtJIYygcgMXcnof9oNbtAG6cyy1DOkdSdwMwg=; b=lU/BnI4u37nMxKBf4RWQS1VmHCe7EUt2MinPqtPnyxVcBwX/6V64VvvmeOcembhiO6 Fprqn/CnOqJMKcsOPPzucpZLv1vGA1N9tcnpLtaiHfDYfqNCgC/zob/VQjdxKRpyQnof QBBb1uS5huu4/kLbkHYr/5X+FgA9vJTv8rcGXNLigLmmuXXM4AOdXbQtEfDjM6t0Z+Wy TdKLoA5l5QH6h7TTEQLkgDvNbBQ3XRmxsmjUFyEHcR/2ORuq1waGYySNCSIpAOIdlP9u 4rRsLSxw5MS4UPJR8mYRLGLusoXXR9fs5C6dgZCdKlGAAcu0EXIEORb7T9ZPFN3Pj0O0 m0Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=O8F2nWEtJIYygcgMXcnof9oNbtAG6cyy1DOkdSdwMwg=; b=YsfEVCP4X5xJJfhwFKIKzZKRWbDTreK5rrNl8qvYDBeFah3egBiga/rmuAnxXPNqnr KrLglUp+gzN9I9rRpYBrP662X/YlTAHKPJk/tls1iktDNiASVEiKtUJKgKN3HGgBEw7X w0XWC15wwHXrZr0FLxrR62Sa/0qUQTV9Vbs/DUwh7tU2tD5YB8MkoOS8NEeb0FDH0rfa Uf+xgqWf2LhnNE7IU77jNBoa+lQmVF+Kw/g09DN/tVMQMtTmtNyzz4IffptuSjo9Hp8V F34XEuKs8gPLkBZ6zs6m+Aj9BogWWXqZ9lQvSE//onZBmL1TZCyMCIe87/4iG0t6NPtf 8DrA== X-Gm-Message-State: AOAM530TmdJyPfF/LH257a/FyvygrN2k7cTh2We/ivdG+ULNaDgT2QGP RTDzoHXL29pYz4eRwxT46P8Ggji+0S0= X-Google-Smtp-Source: ABdhPJyPd/xo5zM+iDP1oWprDa1FMBnkUeM41nu9gskrU1zIA0fuH2wN+X9Rl3NkeEyMGrhXmO9eCog+ck0= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a05:6214:1141:: with SMTP id b1mr5849322qvt.2.1626194073090; Tue, 13 Jul 2021 09:34:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:09 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-32-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 31/46] KVM: SVM: Drop redundant writes to vmcb->save.cr4 at RESET/INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Drop direct writes to vmcb->save.cr4 during vCPU RESET/INIT, as the values being written are fully redundant with respect to svm_set_cr4(vcpu, 0) a few lines earlier. Note, svm_set_cr4() also correctly forces X86_CR4_PAE when NPT is disabled. No functional change intended. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 875d68c4cb9b..6eff7f1a4672 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1269,8 +1269,6 @@ static void init_vmcb(struct kvm_vcpu *vcpu) kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); vcpu->arch.regs[VCPU_REGS_RIP] = 0x0000fff0; - save->cr4 = X86_CR4_PAE; - if (npt_enabled) { /* Setup VMCB for Nested Paging */ control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE; @@ -1280,7 +1278,6 @@ static void init_vmcb(struct kvm_vcpu *vcpu) svm_clr_intercept(svm, INTERCEPT_CR3_WRITE); save->g_pat = vcpu->arch.pat; save->cr3 = 0; - save->cr4 = 0; } svm->current_vmcb->asid_generation = 0; svm->asid = 0; From patchwork Tue Jul 13 16:33:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374581 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB7DBC11F66 for ; Tue, 13 Jul 2021 16:35:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B24C560FF3 for ; Tue, 13 Jul 2021 16:35:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234172AbhGMQiO (ORCPT ); Tue, 13 Jul 2021 12:38:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234364AbhGMQiE (ORCPT ); Tue, 13 Jul 2021 12:38:04 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7D34C05BD0B for ; Tue, 13 Jul 2021 09:34:35 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id gf3-20020a0562142503b02902b1b968a608so17722659qvb.16 for ; Tue, 13 Jul 2021 09:34:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=+E9aSBRBRnkdWbZXjoXlsgbyRNBB/RIBNAs/dlJauCk=; b=NSZq/SEFDbfPCXS71NJBAFGp8Ig+zn4LbogUdovO0aC2VBPEqD/YgmNxIzbsQxJRU9 JqVDzETYLL2xbaTjHMcVEUKGtUE21NrFz9r4FqlCODoda+38sDeHSySrDElfOYHpAL+U 0lcH2CitPsirGgl1hYsFeLMnxRcAugs3O28lyTLI0J1mXzg+fk2w0cGcmkId9rDJ7XjO uWRWQSmIljDT9iQVrv5Y4sNGok5ZlU9w+JdFyEJj1v/zlx/AEt5DzSzMUjyo9M5WiayP NL6pgXe8BIbT8bf2mkEQTqNrlugEBobTil/y6lCsuythfDFpU8QOhm4FqrWPsQyaK19S YHzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=+E9aSBRBRnkdWbZXjoXlsgbyRNBB/RIBNAs/dlJauCk=; b=g22HhecNWVuvLNWr9FDeh+GwSC3zQpYyFUP0UrRUWEQHOj6ei5/t5Ia37rZIqqu9UN rsSncQ6V7iE5D+CQWRcx3M590KSOM3kUMyLedTgG+E9rgJG8NRRQmDQCCcyYwoYQArp1 N0eOq+sMCxIMe2/dIBpIKIF2cvveOlbelENVbSdxANT2UqLGAa4zYdglItbYBMQpK2Kd YQupK4ni4OMbYVnDwQ4b3PVSTbglUVE+L+i4HVC7WrBRL6b7pMD9DsZl9vmshum1dlkB 3XfM+hD57QfFl8KxelIg7/9fCFijuo9hN67vWS+7P5f4smqXMTIUzqXa+/GJqA4U2lhg abyg== X-Gm-Message-State: AOAM531UgAotOalNr9dAQCpN4MTFWco+raPggaoowJXbaQg/BHmIVg13 CLpr3wlUvyd5EogOjBlB8wSa/ksaIDs= X-Google-Smtp-Source: ABdhPJwHQZR9ZfdF+ROsFu+h74c/v2u8IVsDn5+mNncgUeyZxKK2nE85oqgLKZnLs5svw3k7Ct0j7yrpyNY= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:ad4:568a:: with SMTP id bc10mr5808858qvb.20.1626194074843; Tue, 13 Jul 2021 09:34:34 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:10 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-33-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 32/46] KVM: SVM: Stuff save->dr6 at during VMSA sync, not at RESET/INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Move code to stuff vmcb->save.dr6 to its architectural init value from svm_vcpu_reset() into sev_es_sync_vmsa(). Except for protected guests, a.k.a. SEV-ES guests, vmcb->save.dr6 is set during VM-Enter, i.e. the extra write is unnecessary. For SEV-ES, stuffing save->dr6 handles a theoretical case where the VMSA could be encrypted before the first KVM_RUN. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/sev.c | 1 + arch/x86/kvm/svm/svm.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 8d36f0c73071..e34ee60fc9d7 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -583,6 +583,7 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->xcr0 = svm->vcpu.arch.xcr0; save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; + save->dr6 = svm->vcpu.arch.dr6; /* * SEV-ES will use a VMSA that is pointed to by the VMCB, not diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 6eff7f1a4672..251b230b2fef 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1265,7 +1265,6 @@ static void init_vmcb(struct kvm_vcpu *vcpu) svm_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); svm_set_cr4(vcpu, 0); svm_set_efer(vcpu, 0); - save->dr6 = 0xffff0ff0; kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); vcpu->arch.regs[VCPU_REGS_RIP] = 0x0000fff0; From patchwork Tue Jul 13 16:33:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374585 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DE8E9C07E95 for ; Tue, 13 Jul 2021 16:35:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C608460FF4 for ; Tue, 13 Jul 2021 16:35:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235658AbhGMQiZ (ORCPT ); Tue, 13 Jul 2021 12:38:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235684AbhGMQiN (ORCPT ); Tue, 13 Jul 2021 12:38:13 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC810C05BD16 for ; Tue, 13 Jul 2021 09:34:37 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id v184-20020a257ac10000b02904f84a5c5297so27714517ybc.16 for ; Tue, 13 Jul 2021 09:34:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=DVgV3Z0gTUR2hqJGMpL7GHNs1vys1N7z/dx+tpFDkkY=; b=MV+hTug3uDCJg7x2mewre9wftE2qM/EJg+HNfWN3QWW8wkxZA8FqkQlKHiKzWzT6QW DusnjIkp+Z0QCAYPSFqUcx4ZthkukkquAkmjRfW0cmKYss2Hefi+oVF997m/wBO9BKFJ dg73PY4slMYmpTDILMBRDmNJlDZlGN4r/L3RQ2Np8azj+Yscl/d2WcA22KS3Z1WZ6nQM lYYqNBHYIC0MAVfFgb3VtinXu5nkc/ot8UQb1W99LwKwLCNJRosPDihZmGBnNFrL2+lP 7GxcQy5V4dfT37shpjn48rRBI1lJwFtXVsL73zaEdBCIgj/7x3wzZnxoVFtIXr1r+b1p BXXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=DVgV3Z0gTUR2hqJGMpL7GHNs1vys1N7z/dx+tpFDkkY=; b=gH7HfE7ZInVanaD9Jo9vHN8xWnN7Cd3zt3gk4PgnO5GZdKl0z9/Ck3De37QC0mmQf5 yptZXCoZhy1v7yE5yIuVj4BUoptZFEV653VCC6H7n8v5ToFIX2GqxySTd337CPj4fDbh CRxu3uBJtt42io8ZxMlCuj885w8gsgHv5Hl/QQ7aAI/aN50SKYHXj4CE2Htbmn5TouEr rF3XfVniFZGoJtyIYyhDsLQ6Y7kTnAXzAjrrpdbJsWI/M1hVonfavl/tFe6rWp8m0QMy +V8S6hMJP1RKC51V/9flqVJymGE2hSS3NJn1r64/Ss0GXZuzJxw38Sq95wPbT3iBp/lN t+qA== X-Gm-Message-State: AOAM531OGRDzabOkGwNzmiinrPgG/L4nCJBSGmesMTRDRlNNH3tSf6e2 nAq+2y39Sj8VaDqyRQ+A4ZIhbh6OZMM= X-Google-Smtp-Source: ABdhPJzo3ORPeHMNrW4B0mGf0M1Vko//pVeo1rshL1l6wodETc6qDNpBe0/HK36lH5icnSNe6kohGUvu570= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:c503:: with SMTP id v3mr6721332ybe.192.1626194076960; Tue, 13 Jul 2021 09:34:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:11 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-34-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 33/46] KVM: VMX: Skip pointless MSR bitmap update when setting EFER From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Split setup_msrs() into vmx_setup_uret_msrs() and an open coded refresh of the MSR bitmap, and skip the latter when refreshing the user return MSRs during an EFER load. Only the x2APIC MSRs are dynamically exposed and hidden, and those are not affected by a change in EFER. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index a1e5706fd27b..d7a4db15a169 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1647,11 +1647,12 @@ static void vmx_setup_uret_msr(struct vcpu_vmx *vmx, unsigned int msr, } /* - * Set up the vmcs to automatically save and restore system - * msrs. Don't touch the 64-bit msrs if the guest is in legacy - * mode, as fiddling with msrs is very expensive. + * Configuring user return MSRs to automatically save, load, and restore MSRs + * that need to be shoved into hardware when running the guest. Note, omitting + * an MSR here does _NOT_ mean it's not emulated, only that it will not be + * loaded into hardware when running the guest. */ -static void setup_msrs(struct vcpu_vmx *vmx) +static void vmx_setup_uret_msrs(struct vcpu_vmx *vmx) { #ifdef CONFIG_X86_64 bool load_syscall_msrs; @@ -1681,9 +1682,6 @@ static void setup_msrs(struct vcpu_vmx *vmx) */ vmx_setup_uret_msr(vmx, MSR_IA32_TSX_CTRL, boot_cpu_has(X86_FEATURE_RTM)); - if (cpu_has_vmx_msr_bitmap()) - vmx_update_msr_bitmap(&vmx->vcpu); - /* * The set of MSRs to load may have changed, reload MSRs before the * next VM-Enter. @@ -2874,7 +2872,7 @@ int vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) msr->data = efer & ~EFER_LME; } - setup_msrs(vmx); + vmx_setup_uret_msrs(vmx); return 0; } @@ -4469,7 +4467,10 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) if (kvm_mpx_supported()) vmcs_write64(GUEST_BNDCFGS, 0); - setup_msrs(vmx); + vmx_setup_uret_msrs(vmx); + + if (cpu_has_vmx_msr_bitmap()) + vmx_update_msr_bitmap(&vmx->vcpu); vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ From patchwork Tue Jul 13 16:33:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374587 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04CA8C07E96 for ; Tue, 13 Jul 2021 16:35:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E240E60FEE for ; Tue, 13 Jul 2021 16:35:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235184AbhGMQi0 (ORCPT ); Tue, 13 Jul 2021 12:38:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234677AbhGMQiN (ORCPT ); Tue, 13 Jul 2021 12:38:13 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEB36C0613AA for ; Tue, 13 Jul 2021 09:34:39 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id p63-20020a25d8420000b029055bc6fd5e5bso27638673ybg.9 for ; Tue, 13 Jul 2021 09:34:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=skFR+CsoOPtmRzhI0uP937m12Z4yOYLQ+FONgUtSivg=; b=VUNyEnJAv35fM9G5xqTp30Qf15uHEjgktioN/MXNnOgqOjgJyiLtOthrNo4CVzFWOq 7FR+Pn31WLI7alKgLgS/XhMvOfGAxaHe4IpK9ljxYyt3+SKc1idY430RXnjcELjuIXVD sbBtVq8I51qkeIQANBerUbKhsae8izm7tPGNzxndhYKXxC8DobNsKY5xbUiwWfW4OLEa pmlxZoO9XW0evRHpDwEDCPodNs+K2b9LyydThD520BX4vFv3xRVwDVj5lLAlkcQCfCBe D5rpSDfu6aWIfZXmj0y2PWCz6Ni50PEaTJUukiQh+IPNpu+Wn33QsKFOPTQuWPp+yua6 tvKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=skFR+CsoOPtmRzhI0uP937m12Z4yOYLQ+FONgUtSivg=; b=UiYc2esTMOPU8JG1l2G9DcDLOqwIzo47qYYANAz1gTbPUuxzyeFGcMStVnqh8Qoegd 2j+hK258zYpN8dBdv+qhp+qzZG5k6OB47DUhCGGLccDSllPiZKGwjhsSEuxExYBuGItj MvOs/uXcZnYfGu1UCKLtp/jpCoPlZAtXCmk+LcEZX7+F1A+7Ee62qMsYI+ZlDpq/3tye 3xMaHKBJVzVSw7cED7nGE3olQBSmjGth+xFr+fy2y8Cpd7Gw/YGNoj7idGHhcjURqrGy +WSbi1jiOySLJzgnV/mfmIzXQbW7h41xjQt8iDVzD24dERarsVDbKZfUxPuyvKxDo0IM 0IyQ== X-Gm-Message-State: AOAM531r9zCOQpPs3WtAYRVFDEzBNx1bXFqHGyRW5UvlWZovMkJVy2Qt XyLjzpaU5LUe+xFphVrCXSpwIucONu0= X-Google-Smtp-Source: ABdhPJwlFJn49OqshcuieT77PrsL//3oCmHnYyTpJeaMJp1u9tGGcgfRO0Sx7zmlRsDLItNxd9fVZcFPeOc= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:2e49:: with SMTP id b9mr7359056ybn.250.1626194078968; Tue, 13 Jul 2021 09:34:38 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:12 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-35-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 34/46] KVM: VMX: Refresh list of user return MSRs after setting guest CPUID From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org After a CPUID update, refresh the list of user return MSRs that are loaded into hardware when running the vCPU. This is necessary to handle the oddball case where userspace exposes X86_FEATURE_RDTSCP to the guest after the vCPU is running. Fixes: 0023ef39dc35 ("kvm: vmx: Set IA32_TSC_AUX for legacy mode guests") Fixes: 4e47c7a6d714 ("KVM: VMX: Add instruction rdtscp support for guest") Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index d7a4db15a169..3045daa3ec30 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7159,6 +7159,8 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */ vcpu->arch.xsaves_enabled = false; + vmx_setup_uret_msrs(vmx); + if (cpu_has_secondary_exec_ctrls()) { vmx_compute_secondary_exec_control(vmx); vmcs_set_secondary_exec_control(vmx); From patchwork Tue Jul 13 16:33:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374589 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 15212C07E95 for ; Tue, 13 Jul 2021 16:35:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 00D1660FF3 for ; Tue, 13 Jul 2021 16:35:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234431AbhGMQid (ORCPT ); Tue, 13 Jul 2021 12:38:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230376AbhGMQiO (ORCPT ); Tue, 13 Jul 2021 12:38:14 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5856C0613B8 for ; Tue, 13 Jul 2021 09:34:41 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id x15-20020a25ce0f0000b029055bb0981111so27838099ybe.7 for ; Tue, 13 Jul 2021 09:34:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=vIWc55IJyEVoNC9ov84IeWMlOnEp9DjAfbK1wsM0Yas=; b=EHc24xcXcaRLUjqC/SA/M1jyyqgnl7dGPNghNeAJo0E8DqPiCbfriPxhy3OM70O3wi b81ZekWmOvgC2OLvhY4OmkiUxUqGhMlM4Z7mrJoknSobg/b/DU5ks4sxiwwRaq7cN3Gs 6WS2YVOAU0Z32iJccqc2e/9cTiahvi6lpKHnkEoPIy/j0Y+8gRXRYMnGIBosd946LE3S sfgYQIalCI+kyt04wGn1isr9y/6HMebU/Sjs/nrDtLPHAdDsygLgR5yipsoOYKoSt8Js rHQhIp9br2v/hoB6vYN46MnTpECYYWqYHaXxkAyayCvMU+TrvSGU3ObdRW0HZy+dvILY NKaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=vIWc55IJyEVoNC9ov84IeWMlOnEp9DjAfbK1wsM0Yas=; b=UxTJJmm2snBvgx5qUjn3kpjbOR+pMVDKvPdFZ80Wbg1D8zu2J8zbqd9w0Th0zaRcLZ KNIEXwK/rmWLJCI9YYypnzoBbIs1suF94C7LwROYnymTGgBXJ7ZFnL0vbpqTzwr92Lq0 /nnews72v6gML3bxOh//nZGU/T+b0oNPV3aXzL1vREwM63EN3YvgzyQ24Ww9/zqGmy0F hW19jS/fvePpr6HlpVg2iW9Lr0nctD996GQXYUIBB4rWJsVzqoZZJmuvtziokbB3e7FF fbjT2+6v1u3FPAUBQ7L4ALoMxmAnieaOWSO1VasrVj9AJaQdXjTiDChi/wTn2VtJnAk9 hljQ== X-Gm-Message-State: AOAM533JbPlMEOkm0rV45qA5KPiCZif9ubEH6E1QqwYipzl4QFI74OcZ KNFqaDa5CkrjD5mYASS6S5AlFFtdFSQ= X-Google-Smtp-Source: ABdhPJxqtfpMYERsHaOr+5vEs9x2n03DLrMc9aG60KPKlR/Yj/1lPr/5wO9Qd4ZHkQ90BdZUtkmmGF4rChc= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:d97:: with SMTP id 145mr7293842ybn.276.1626194081024; Tue, 13 Jul 2021 09:34:41 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:13 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-36-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 35/46] KVM: VMX: Don't _explicitly_ reconfigure user return MSRs on vCPU INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org When emulating vCPU INIT, do not unconditionally refresh the list of user return MSRs that need to be loaded into hardware when running the guest. Unconditionally refreshing the list is confusing, as the vast majority of MSRs are not modified on INIT. The real motivation is to handle the case where an INIT during long mode obviates the need to load the SYSCALL MSRs, and that is handled as needed by vmx_set_efer(). Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 3045daa3ec30..555235d6c17e 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4406,6 +4406,8 @@ static void init_vmcs(struct vcpu_vmx *vmx) vmx->pt_desc.guest.output_mask = 0x7F; vmcs_write64(GUEST_IA32_RTIT_CTL, 0); } + + vmx_setup_uret_msrs(vmx); } static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) @@ -4467,8 +4469,6 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) if (kvm_mpx_supported()) vmcs_write64(GUEST_BNDCFGS, 0); - vmx_setup_uret_msrs(vmx); - if (cpu_has_vmx_msr_bitmap()) vmx_update_msr_bitmap(&vmx->vcpu); From patchwork Tue Jul 13 16:33:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374591 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E29DDC07E95 for ; Tue, 13 Jul 2021 16:35:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CB37160FF4 for ; Tue, 13 Jul 2021 16:35:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235544AbhGMQig (ORCPT ); Tue, 13 Jul 2021 12:38:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235546AbhGMQiZ (ORCPT ); Tue, 13 Jul 2021 12:38:25 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D2A8C05BD26 for ; Tue, 13 Jul 2021 09:34:44 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id v184-20020a257ac10000b02904f84a5c5297so27715593ybc.16 for ; Tue, 13 Jul 2021 09:34:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=9wqr9l9rn3Fb1zwRgcq5bBwnS7s/+k7U1KQtXn6qui4=; b=nHyxyZ6RG/ToM4hIEqA3tiRylNSegnO7fPS5sd0Ve+8Qw880sTF7kgOUiU9kNgQ29G wUevBqSs4gBv5jWgJHhBNmyh4Jz3mWRMqMm1Ahsi5Qnxm1K3ktrIAr+dLUnZqf4Q9Bcl abrHmLYx+HE0RSb/IF5nhllPhH2vQWWxWgwQ6ySsajYq6COWqsenGs8KK4IcGKngjKWi 0NB8m1B4JxYNBieaOiskZvdxGIx37GjtMinJNliqxcaj8tpJv1qdAJHNvs1Eq3pjM5kc 9aVQTDvJDJkfPmG6LvljWeXAafy9URMevKsJCaSTkHSoEiuBEn6MwpPetpILrrLJ5KDF j41g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=9wqr9l9rn3Fb1zwRgcq5bBwnS7s/+k7U1KQtXn6qui4=; b=Vl/fW6gzFcEXArpl4jgFlZ64B85goHS12iqb4eemefnyRVQYeoyVQ1VnhHTC8Jm2cm EF9ZNutNImbmNXRbtnK3pOL7wRpP26rt7K55CTS2nd7U1sZ2uycPmD9+MjbGDT1bvutA KP4HeI+2U2YwlUZiPQOAcUGyCyadD2kp9mM1NWzMXFH1b11lqdjpn4JGH2GqXjpo7d86 pFfxqaBHjMfpEjKMal8ORTSdaaetcBIJU70SDlAHCV7v2AbKYC68QqeMhoIi/D+Wogmx +zioCnMmnS2TgXtD5AhwaIzYGwLBv25lXQ3fcFvrSlPrbWrpFKcnDi00ujoGTM8mkpEY r4NA== X-Gm-Message-State: AOAM531njrxkywe8UsBP6mpt9Rf202iVTJm369f66bnHjLa2mVTyn8Ep 1uTziAOaQwYuQxCl5HGNy/QzpbUUdeA= X-Google-Smtp-Source: ABdhPJwHQ3cIvB9bZHdex56kIWqYaWWDxiTGgWCm1Wsv9meh0YHUlKRQSZ7WuApnem728OAC+fdJysOnh+8= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:abcc:: with SMTP id v70mr6892084ybi.216.1626194083200; Tue, 13 Jul 2021 09:34:43 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:14 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-37-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 36/46] KVM: x86: Move setting of sregs during vCPU RESET/INIT to common x86 From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Move the setting of CR0, CR4, EFER, RFLAGS, and RIP from vendor code to common x86. VMX and SVM now have near-identical sequences, the only difference being that VMX updates the exception bitmap. Updating the bitmap on SVM is unnecessary, but benign. Unfortunately it can't be left behind in VMX due to the need to update exception intercepts after the control registers are set. Reviewed-by: Reiji Watanabe Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 6 ------ arch/x86/kvm/vmx/vmx.c | 9 --------- arch/x86/kvm/x86.c | 8 ++++++++ 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 251b230b2fef..ea4bea428078 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1262,12 +1262,6 @@ static void init_vmcb(struct kvm_vcpu *vcpu) init_sys_seg(&save->ldtr, SEG_TYPE_LDT); init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); - svm_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); - svm_set_cr4(vcpu, 0); - svm_set_efer(vcpu, 0); - kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); - vcpu->arch.regs[VCPU_REGS_RIP] = 0x0000fff0; - if (npt_enabled) { /* Setup VMCB for Nested Paging */ control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 555235d6c17e..ef92ec40d3d9 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4454,9 +4454,6 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vmcs_write64(GUEST_IA32_DEBUGCTL, 0); } - kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); - kvm_rip_write(vcpu, 0xfff0); - vmcs_writel(GUEST_GDTR_BASE, 0); vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); @@ -4484,12 +4481,6 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); - vmx_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); - vmx_set_cr4(vcpu, 0); - vmx_set_efer(vcpu, 0); - - vmx_update_exception_bitmap(vcpu); - vpid_sync_context(vmx->vpid); if (init_event) vmx_clear_hlt(vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6a11ec5d38ac..3aa952edd5f4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10870,6 +10870,14 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) static_call(kvm_x86_vcpu_reset)(vcpu, init_event); + kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); + kvm_rip_write(vcpu, 0xfff0); + + static_call(kvm_x86_set_cr0)(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); + static_call(kvm_x86_set_cr4)(vcpu, 0); + static_call(kvm_x86_set_efer)(vcpu, 0); + static_call(kvm_x86_update_exception_bitmap)(vcpu); + /* * Reset the MMU context if paging was enabled prior to INIT (which is * implied if CR0.PG=1 as CR0 will be '0' prior to RESET). Unlike the From patchwork Tue Jul 13 16:33:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374593 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E34ACC11F66 for ; Tue, 13 Jul 2021 16:35:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C3E2460FF4 for ; Tue, 13 Jul 2021 16:35:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235797AbhGMQih (ORCPT ); Tue, 13 Jul 2021 12:38:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235620AbhGMQiZ (ORCPT ); Tue, 13 Jul 2021 12:38:25 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77C2CC0613BA for ; Tue, 13 Jul 2021 09:34:46 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id p63-20020a25d8420000b029055bc6fd5e5bso27640224ybg.9 for ; Tue, 13 Jul 2021 09:34:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=7TYutvwiNQ0wwwZfy6Csacs/gOXz4YtFws186782Baw=; b=lDyzSVB8js2o047UWN71BM8/WfEwkxLDHVzimxp2ThMT2lJuStsKlT+8jU2eCOZsds QMg5A/eVoPNOz8SnG7Q2cVoIqjg1ogQ+J1uOiMvosgh36iDIbPvrD/bnc2m0hcDB8rXW dSqJGy9m0qz1snNWlrUIDNrzkz1lQF3FjDbYGJVVnh/YWgPr2d0Lzj/nvpbHBoWJd1U5 LaCNIzT4bg8aU41wtyJ1KJhtpwuVH+47X4IGWDmmAX61avOIr1HPfrEv1RupNhbX22Dv Hra7g6Fa9nSbPEn8at8DuI6trY91NfsboK1n0d/Z9N4Id/cjLBga1SEXXnJ+UVmBB7Y5 j8KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=7TYutvwiNQ0wwwZfy6Csacs/gOXz4YtFws186782Baw=; b=K8HZWlD0quzoJq1S0ZK4mQFvS8MiWCsvI77lNlO7ik63irSmaeavphpEmkMTXdSZAx n5DDY2bwphZdeuJqcS0I+N9Q08Iysq+2aEy18esAA9YXnmW5WqL996pMP7dOgh7nFWj3 OU2AoniAKhwvRv7kK3vZ1inP2e9kzl/OBpY4JvbxJhb8rPFeSfdoiG6+GNHUpjDiRAfz Oem2vi5e/yVGx+bgJNMWgzb+ArGRDq5d2JF4V21xw213i+beaYbT78h14ft2uRa01uTN 1HPPX3XX/TjDBBS63kiJwePxirM1W3NIWNGdpOvUoszAOA7Je1K4W70OIY9ztn28rvje 464g== X-Gm-Message-State: AOAM530+J5iu2kCgs28peFsrK2CCHUM7hms6OOeIxSowmPKUQvpMRW2g VI1IVAXhSOVXnHiIa/R0de7mo2zaHiE= X-Google-Smtp-Source: ABdhPJz9BrOtOdA4tipcZJ7RCVOjdwjB2CaLZaXT0dUnSI2USqQTlYZpY3sNb/D4smdOz4Iwe7qjAKTP/zk= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:38c7:: with SMTP id f190mr7076549yba.5.1626194085652; Tue, 13 Jul 2021 09:34:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:15 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-38-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 37/46] KVM: VMX: Remove obsolete MSR bitmap refresh at vCPU RESET/INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Remove an unnecessary MSR bitmap refresh during vCPU RESET/INIT. In both cases, the MSR bitmap already has the desired values and state. At RESET, the vCPU is guaranteed to be running with x2APIC disabled, the x2APIC MSRs are guaranteed to be intercepted due to the MSR bitmap being initialized to all ones by alloc_loaded_vmcs(), and vmx->msr_bitmap_mode is guaranteed to be zero, i.e. reflecting x2APIC disabled. At INIT, the APIC_BASE MSR is not modified, thus there can't be any change in x2APIC state. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index ef92ec40d3d9..7e99535a4cbb 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4466,9 +4466,6 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) if (kvm_mpx_supported()) vmcs_write64(GUEST_BNDCFGS, 0); - if (cpu_has_vmx_msr_bitmap()) - vmx_update_msr_bitmap(&vmx->vcpu); - vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ if (cpu_has_vmx_tpr_shadow() && !init_event) { From patchwork Tue Jul 13 16:33:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374595 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF30AC07E95 for ; Tue, 13 Jul 2021 16:35:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DBA7060FF4 for ; Tue, 13 Jul 2021 16:35:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234577AbhGMQii (ORCPT ); Tue, 13 Jul 2021 12:38:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235716AbhGMQi0 (ORCPT ); Tue, 13 Jul 2021 12:38:26 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8EB92C061788 for ; Tue, 13 Jul 2021 09:34:48 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id v184-20020a257ac10000b02904f84a5c5297so27717166ybc.16 for ; Tue, 13 Jul 2021 09:34:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=WzjACxS3vJSqCJVMr5JKsyBcfyvMVtL3FJuhC43+sT4=; b=pSSOz/N1wBCqRCB9767Mr2YSJDrKkYTfm5X9v0AonKgBoIiGxz1CeUEyFH5Zk8aXZl 2rTjJl90nXqe2QhywLVjRUzZU5KHq4fzPmvbkUsbOzl7GIxVsNd2HIvPlnjvWoQlTEe8 E2pURX7CKvGnQgIV7NFRGJMcEglu6rRWMdAx/CYQR7lv9DItpUg2pP54rgU3ilk2okiv Cyiy7cv8z1OKEMrI8DZnAwNAHv/XoAErHdtKCl6xl4JtTMQvqepGPlwLTo2czym+pavh DaJzB3A3B4SuwRpaWyxIlNfuK5g0CEDEWpHJ2TRHd0XEKUXacBOXUlmIIxOmd5JgYcr7 tU0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=WzjACxS3vJSqCJVMr5JKsyBcfyvMVtL3FJuhC43+sT4=; b=BOiUHryssDmwQztHKtD0GJxb/MeDa50pDHQ2d8HjZ75fhAwN0e/OFdX+zvxJXUFU1X BUh+bypMEUxdk6RjBoxtVOkMm8ostBwgcmeTuXEWvUJfd3SqHnYwbmABR5g7lxjyGuVk v99fTaZljO2kbhmwG3MjC3aler9BsTyrB25yqfRutcvBPJzaOVL+BNeqLgHIGCcxnQVu O/Bqn7Z1fSbNoVEfvU8Jy+FMtPym4iPymQGP8dZ3t8LnS8d2hnMX5rDosz29yqKdzHmL 3Aoq7NssIBb9YgbwuEXAwJCPNPX2U4CmwKprXn9DrKeP2vlgrFajGK/M6jtyPfCAcEFe P0Gw== X-Gm-Message-State: AOAM5329KpG5YemPvd1u0J1iXKZUQuP0ojvK6bVn98IGKvf+itH3ze7z Vtak7KEgSnYzsMB255TgH6/aS1VoDl0= X-Google-Smtp-Source: ABdhPJye3BexlkfrOe53+nGwvSDofxLGrCXkzmJ1TZUWM1skavM0IHGrLjAAIkgdLaNtPaZR+KShBwMKQ/w= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:c78f:: with SMTP id w137mr7097023ybe.381.1626194087757; Tue, 13 Jul 2021 09:34:47 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:16 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-39-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 38/46] KVM: nVMX: Remove obsolete MSR bitmap refresh at nested transitions From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Drop unnecessary MSR bitmap updates during nested transitions, as L1's APIC_BASE MSR is not modified by the standard VM-Enter/VM-Exit flows, and L2's MSR bitmap is managed separately. In the unlikely event that L1 is pathological and loads APIC_BASE via the VM-Exit load list, KVM will handle updating the bitmap in its normal WRMSR flows. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 6 ------ arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/vmx/vmx.h | 1 - 3 files changed, 1 insertion(+), 8 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index a77cfc8bcf11..0d0dd6580cfd 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4305,9 +4305,6 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, kvm_set_dr(vcpu, 7, 0x400); vmcs_write64(GUEST_IA32_DEBUGCTL, 0); - if (cpu_has_vmx_msr_bitmap()) - vmx_update_msr_bitmap(vcpu); - if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, vmcs12->vm_exit_msr_load_count)) nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); @@ -4386,9 +4383,6 @@ static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) kvm_mmu_reset_context(vcpu); - if (cpu_has_vmx_msr_bitmap()) - vmx_update_msr_bitmap(vcpu); - /* * This nasty bit of open coding is a compromise between blindly * loading L1's MSRs using the exit load lists (incorrect emulation diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 7e99535a4cbb..f605b43d28e1 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -3865,7 +3865,7 @@ static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu, u8 mode) } } -void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) +static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); u8 mode = vmx_msr_bitmap_mode(vcpu); diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index b584e41bed44..1b3dd5ddf235 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -378,7 +378,6 @@ u64 construct_eptp(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level); bool vmx_guest_inject_ac(struct kvm_vcpu *vcpu); void vmx_update_exception_bitmap(struct kvm_vcpu *vcpu); -void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); bool vmx_nmi_blocked(struct kvm_vcpu *vcpu); bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu); bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); From patchwork Tue Jul 13 16:33:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374603 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B876FC11F69 for ; Tue, 13 Jul 2021 16:35:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A70C960FEE for ; Tue, 13 Jul 2021 16:35:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235870AbhGMQio (ORCPT ); Tue, 13 Jul 2021 12:38:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234909AbhGMQia (ORCPT ); Tue, 13 Jul 2021 12:38:30 -0400 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8652CC06178A for ; Tue, 13 Jul 2021 09:34:50 -0700 (PDT) Received: by mail-qk1-x74a.google.com with SMTP id bi3-20020a05620a3183b02903b55bbe1ef9so17391658qkb.13 for ; Tue, 13 Jul 2021 09:34:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=/kobFDO1v5kxfmrecOu9TTYWpLXIpef9EiaYGzHDjPQ=; b=gPT/NLx/hKtf2TiWbAi/D/oofKe74s5iRoN0Y2bdIZ3lwZnppiDA0osrA7VY3/M91x AhQQK1JCZnN0jw3nhBgB7FiJZVFYCT9JnQ4BXAWhGEpVpUf3fMekheoovaZi78dj0FKw xh8UYd0NbDDf4N90irX4TGpk1MKh5fLo/yV/dBf1y/rCO+1lptagV7Ay7Hagh6Mzv7bO m2pa5xmKIXIdKs3V+x0KsQEks25FClM+X0oNmuNI/hV3qliSxyVSwvXFe6E2HR748cpg MQhKQOZPlr+Sq4p4tbQ8C9MUasRSNpLD540b8+KhTjCnfCYOWymQt2ZX4AVNBkr6vAev avnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=/kobFDO1v5kxfmrecOu9TTYWpLXIpef9EiaYGzHDjPQ=; b=mmLH/mdWDfQpXlvHoJYM2aMYC9rFZb/tWF/HvnPk1dia4ne2WoxsKz0rKBBUsCwWq9 OwZZG53NC0wdA21ocHHmnDCA8IhigCUYb1MD+ApdR0/Ui8H27WxQQRwlqcJuENeTXM01 JeCctj/CP8vKC1yRKEwvOGr8W2HdDoZuTxIO6o9nmKYq4RdJQkfgnMRPqzQPQTYnbuCg J+wqSB+6aqLxpTvT5fzLg1cg1B07cUUBXWF8/CSUfetvl1zbfgTEooq84eaXn7xmRaHT oynkLICRJDXmQuAF1AFiOR0/PtiH1gzqAVBYb5OjbPYRkTLwBL/ckrdNkzbu+7yRCWqD xXIA== X-Gm-Message-State: AOAM5335/8r0VZ84uv/1Uje5D3k8X61NGjoTjC5zL05nzWVQAL2PpLT7 VE+bUXwZC0uE7KnUrjKwNN8AG32iUsk= X-Google-Smtp-Source: ABdhPJwZDspm/v4ErGDdok/nXXI1lMusRSIDGXRWs4DBdv8SnhzOc4vqEiq0Bb16kx2wAN8QYhG6fV2JCiM= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a0c:9e6a:: with SMTP id z42mr5749421qve.37.1626194089669; Tue, 13 Jul 2021 09:34:49 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:17 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-40-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 39/46] KVM: VMX: Don't redo x2APIC MSR bitmaps when userspace filter is changed From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Drop an explicit call to update the x2APIC MSRs when the userspace MSR filter is modified. The x2APIC MSRs are deliberately exempt from userspace filtering. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index f605b43d28e1..d5a174ff20f9 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -3936,7 +3936,6 @@ static void vmx_msr_filter_changed(struct kvm_vcpu *vcpu) } pt_update_intercept_for_msr(vcpu); - vmx_update_msr_bitmap_x2apic(vcpu, vmx_msr_bitmap_mode(vcpu)); } static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, From patchwork Tue Jul 13 16:33:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374597 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4F094C07E96 for ; Tue, 13 Jul 2021 16:35:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 38D0560FEE for ; Tue, 13 Jul 2021 16:35:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235340AbhGMQil (ORCPT ); Tue, 13 Jul 2021 12:38:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234290AbhGMQid (ORCPT ); Tue, 13 Jul 2021 12:38:33 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94BECC061340 for ; Tue, 13 Jul 2021 09:34:52 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id k32-20020a25b2a00000b0290557cf3415f8so28014796ybj.1 for ; Tue, 13 Jul 2021 09:34:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=5yZvKb3rht26ruASUkfQQVLesQhgsV9e0Ri724OAElc=; b=qI6F9hUh/CIx4LeAR6ngfn6eVtHvGqm10D3gtey8+kdr/skmewXwWVwQypgbry8aat H4jh6mB2yjq1zF/qWDWutJnIJe1IkGQ7YgGi57lmF0ThEgAIHCU+jX6Ehc5p61pJU9Js e/g+1WPVMy/Yi0b93O99Uh9KBMTuxVG1iw6qrskti3nuECQvYuzYPaE18bz+hXnixVt+ bFuRxdI9Ev9fD7hV/Ky+Ob8N6gI5qdhShSTYIsj3V5OJaRnBXvRvB66h2qX3u+7hnhc+ gWpcCwuIVKA3BalSVBMcSh+Jw2hu03YyuiM8aeylpByUHy1sEXRmL/snVSafAN9B5Nmd 2lyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=5yZvKb3rht26ruASUkfQQVLesQhgsV9e0Ri724OAElc=; b=V2VuU70ZEXRcB+uRIhG2VBQsPBTFU1oZ7jlEBaRrs1SJRFmWzbDpqA0ZFYg5yqr0RN hftABWSZd6xNrEKalhdqeE9+AnitjXO/2exR/zTOu3Oel5Ip9MnqT04a2libYa6cElr/ hk9FXlGQW9O9pYP3n0HYmtojxUrGjG4jlJdHKBr/PWHmwD7kC9OoCFnXC2rXtjFaojBS 1TAZXblruFKlgCDVdZBRcuX0bULC+6bl3Xwk+pAhm9zVL3GwD2wlMEsppkukugZDhHJR 0wi+DOvSwqojDccoDAPncd+BzZNrF5cJ9QTVdNwJmeRtWsoIQoRfiLXaT75c/8W1vOUr FIxw== X-Gm-Message-State: AOAM531uw3yrqWNVcNDMDCCgHwzXep16uR+Ht81V8d6Gy4j76LhCZrV9 4G9QxRISOJvXHLG66o1MqkGumKvRC3c= X-Google-Smtp-Source: ABdhPJxViBAl8YwtovSKDcDwK19KFAhg1ewICGm5+/+qj/Chcc16EfHW9/j+T1IxW1o2ZrX6X0U2NUbFKGU= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:8b12:: with SMTP id i18mr6611742ybl.162.1626194091791; Tue, 13 Jul 2021 09:34:51 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:18 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-41-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 40/46] KVM: VMX: Remove unnecessary initialization of msr_bitmap_mode From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Don't bother initializing msr_bitmap_mode to 0, all of struct vcpu_vmx is zero initialized. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index d5a174ff20f9..bc09a2f7cb5f 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6835,7 +6835,6 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu) vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R); vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R); } - vmx->msr_bitmap_mode = 0; vmx->loaded_vmcs = &vmx->vmcs01; From patchwork Tue Jul 13 16:33:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374599 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D50EFC11F67 for ; Tue, 13 Jul 2021 16:35:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C2B4360FF4 for ; Tue, 13 Jul 2021 16:35:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235571AbhGMQin (ORCPT ); Tue, 13 Jul 2021 12:38:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235052AbhGMQie (ORCPT ); Tue, 13 Jul 2021 12:38:34 -0400 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A878C0612E9 for ; Tue, 13 Jul 2021 09:34:54 -0700 (PDT) Received: by mail-qv1-xf49.google.com with SMTP id l4-20020a0ce0840000b02902cec39ab618so17785519qvk.5 for ; Tue, 13 Jul 2021 09:34:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=+rqcgnm7H23kDtzI+NHHhjYq+hDDRi5POz7LV9x1q80=; b=EUJbXsjQJoA5ET64LXIKu5IwjyTv2OWjBqPQfKNl2ofUWNM0hNyDZH5Ne3c9P89XsG 6oSPkScJ1YGYbjScYTuHIfTGFWtPqSyAADXT662JDTvVUnz6RyTkTDWuYvgE/G4sZ7t2 FWXnX0XyxBe4Qfb+XxY0AfGU8pOpLbIZho0UE48yoGm4MNJijRm3yuEudWA5VGc4HFZh OxElfscJXYpk7c+b5m044mL6z041YBXvHwhE4hGHHEM+uwaX5geQF5G1QhTXn3QPQCKt mZllRrXhxZZ0qCds7XN9/LtRHF4xjMRfFzH5Wzdwdb/h5KmM03LV+VUADPwvuSM+5b2b j1xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=+rqcgnm7H23kDtzI+NHHhjYq+hDDRi5POz7LV9x1q80=; b=hdpU3bfeEagloqIDghUuTOOoXw5+oCAWNw+t5HGkl5M179wW9vRY5QKYzacpYqsJjp XiTL4pFOcRHrVLoH5JStXBcChtHHj66X9d8G4ND93mX4Ty2ioAFFMynIOJDgoLcv+Pi3 w5Q8rvmYsVjnZaPPimT6ovfeVi6MZq8A3TMsKTSXdJkNex4mIqDV5j8+LwTr+Cd8hyOF Lkn1IxpBPP0EQ31i2cQlaOFu3OXEvCwMSX1VukW5Sq4y317MC5IYTb4+3H96Y2kSl4YY xjKlz2Ko5Gj6n4T65kp3GpnHtq9mPOpTfy5bB1ziLpIu7HikKYyDhJIv1p5GFN3tbTmM Ztpg== X-Gm-Message-State: AOAM530pOuSXIRTtF1nuvZGgo7Bbp9Rx9h5ontMsTfIlQrt6uTckEmO9 DXtVmK+C3NHCIDrOe8S8Tciby5wmXHI= X-Google-Smtp-Source: ABdhPJyX1XTDODijjpMeMU63KSmlqP/qOCjPFipPO8vYTD0ueg0PSQRQ+Wdp0FIJdWendLKxStrwihyhN/o= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a05:6214:1021:: with SMTP id k1mr5843936qvr.4.1626194093657; Tue, 13 Jul 2021 09:34:53 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:19 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-42-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 41/46] KVM: VMX: Smush x2APIC MSR bitmap adjustments into single function From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Consolidate all of the dynamic MSR bitmap adjustments into vmx_update_msr_bitmap_x2apic(), and rename the mode tracker to reflect that it is x2APIC specific. If KVM gains more cases of dynamic MSR pass-through, odds are very good that those new cases will be better off with their own logic, e.g. see Intel PT MSRs and MSR_IA32_SPEC_CTRL. Attempting to handle all updates in a common helper did more harm than good, as KVM ended up collecting a large number of useless "updates". Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 55 ++++++++++++++++-------------------------- arch/x86/kvm/vmx/vmx.h | 2 +- 2 files changed, 22 insertions(+), 35 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index bc09a2f7cb5f..cdde1dfaa574 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -3812,21 +3812,6 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type) vmx_set_msr_bitmap_write(msr_bitmap, msr); } -static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) -{ - u8 mode = 0; - - if (cpu_has_secondary_exec_ctrls() && - (secondary_exec_controls_get(to_vmx(vcpu)) & - SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { - mode |= MSR_BITMAP_MODE_X2APIC; - if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) - mode |= MSR_BITMAP_MODE_X2APIC_APICV; - } - - return mode; -} - static void vmx_reset_x2apic_msrs(struct kvm_vcpu *vcpu, u8 mode) { unsigned long *msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; @@ -3844,11 +3829,29 @@ static void vmx_reset_x2apic_msrs(struct kvm_vcpu *vcpu, u8 mode) } } -static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu, u8 mode) +static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu) { + struct vcpu_vmx *vmx = to_vmx(vcpu); + u8 mode; + if (!cpu_has_vmx_msr_bitmap()) return; + if (cpu_has_secondary_exec_ctrls() && + (secondary_exec_controls_get(vmx) & + SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { + mode = MSR_BITMAP_MODE_X2APIC; + if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) + mode |= MSR_BITMAP_MODE_X2APIC_APICV; + } else { + mode = 0; + } + + if (!(mode ^ vmx->x2apic_msr_bitmap_mode)) + return; + + vmx->x2apic_msr_bitmap_mode = mode; + vmx_reset_x2apic_msrs(vcpu, mode); /* @@ -3865,21 +3868,6 @@ static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu, u8 mode) } } -static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) -{ - struct vcpu_vmx *vmx = to_vmx(vcpu); - u8 mode = vmx_msr_bitmap_mode(vcpu); - u8 changed = mode ^ vmx->msr_bitmap_mode; - - if (!changed) - return; - - if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) - vmx_update_msr_bitmap_x2apic(vcpu, mode); - - vmx->msr_bitmap_mode = mode; -} - void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); @@ -4139,8 +4127,7 @@ static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); } - if (cpu_has_vmx_msr_bitmap()) - vmx_update_msr_bitmap(vcpu); + vmx_update_msr_bitmap_x2apic(vcpu); } u32 vmx_exec_control(struct vcpu_vmx *vmx) @@ -6186,7 +6173,7 @@ void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) } secondary_exec_controls_set(vmx, sec_exec_control); - vmx_update_msr_bitmap(vcpu); + vmx_update_msr_bitmap_x2apic(vcpu); } static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 1b3dd5ddf235..e370091d57c6 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -229,7 +229,7 @@ struct nested_vmx { struct vcpu_vmx { struct kvm_vcpu vcpu; u8 fail; - u8 msr_bitmap_mode; + u8 x2apic_msr_bitmap_mode; /* * If true, host state has been stored in vmx->loaded_vmcs for From patchwork Tue Jul 13 16:33:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374601 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32F16C11F6A for ; Tue, 13 Jul 2021 16:35:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1E6C4600CD for ; Tue, 13 Jul 2021 16:35:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229666AbhGMQip (ORCPT ); Tue, 13 Jul 2021 12:38:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235538AbhGMQig (ORCPT ); Tue, 13 Jul 2021 12:38:36 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3219CC05BD2D for ; Tue, 13 Jul 2021 09:34:56 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id c17-20020ac87dd10000b0290250fd339409so14043386qte.6 for ; Tue, 13 Jul 2021 09:34:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=ruF9NeysJG8YlLwDSa8NGDxOHgaZRuCfs6bt3ZWAhqU=; b=q/QtnyQ9oosp7aP3q8zCPx1rbONx4piEuNDTe5YQXzXRg7B3QsfVOcDOUrjXpAfZ80 rUMkNbVNonq5w6AWR16/QUr487h5VRHwruFBiRrptKAaDx/pT3JeWaWN4clj6AKxK7xC r022arb8lhLRWVLSlgIt/w7FQmIElKHrLHkQratxTBJTdeCvMEmwt6ZV+m/3BikSZ5wf lxWIf/tGWw2e8h/bCuvjojg6z0dfF9T7gqY0C2T7Ub5Ayp0nNsseS5hyH7MzbMDDrh4S 1gB7by08qF06UvRUJgS6IXMbGLQbKO4qZCtrmWWFO8R4th4LMvFeOSVW1O6cAGvE8VlR nOjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=ruF9NeysJG8YlLwDSa8NGDxOHgaZRuCfs6bt3ZWAhqU=; b=K69sBwYxPrX+uzTi+9pKA+a9JCfUX8kjQZmFIDCZ0/uTIYFljE+Ehae4Rjj2W3rAjP YFfXLPWuSQBsSOFM0tIL+gKIbYXW9ZKeMfiT/QXUfaVUGX4rKIGGne+bodlVWHcpn21y aWQxdZ3mjJHv42UCXMG8Q4mUROq6VpyQdU1nZ6iOH5xk4Kz9D6ls9I6kAqEjjowKSbuS yzTQVj7PDot9eP+r56BTG/M5VnV0Wzk2EdxrjC8I6YSFENh3vYLrtXarSpcQAAGeJGvL 984BsZaoxRt6FNPpol0HsqQGQIf4vzvBWRBS9SkONIP9oJbC3NcxB6lYY00WulbZbQwU Gs2A== X-Gm-Message-State: AOAM533+VKzM/UB7/U4TA75gKe5es+Q+oFCCutoloaFIDANt9spnogDQ RunW75yasd8/h6K6mBSQhHRt5Lt1fYM= X-Google-Smtp-Source: ABdhPJzVU9yW2GAGD/nTjgmJlL07BIkZt8qPLIe2eCv6L8Db/53PtthwwLzzlQYczyhtkjPQksbVWmB+MNI= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a05:6214:4b2:: with SMTP id w18mr5659848qvz.5.1626194095368; Tue, 13 Jul 2021 09:34:55 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:20 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-43-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 42/46] KVM: VMX: Remove redundant write to set vCPU as active at RESET/INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Drop a call to vmx_clear_hlt() during vCPU INIT, the guest's activity state is unconditionally set to "active" a few lines earlier in vmx_vcpu_reset(). No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index cdde1dfaa574..4acfb2f450e6 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4465,8 +4465,6 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); vpid_sync_context(vmx->vpid); - if (init_event) - vmx_clear_hlt(vcpu); } static void vmx_enable_irq_window(struct kvm_vcpu *vcpu) From patchwork Tue Jul 13 16:33:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374605 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B030C07E96 for ; Tue, 13 Jul 2021 16:35:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EB581600CD for ; Tue, 13 Jul 2021 16:35:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236088AbhGMQir (ORCPT ); Tue, 13 Jul 2021 12:38:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235829AbhGMQii (ORCPT ); Tue, 13 Jul 2021 12:38:38 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3805DC05BD32 for ; Tue, 13 Jul 2021 09:34:58 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id l16-20020a25cc100000b0290558245b7eabso27775815ybf.10 for ; Tue, 13 Jul 2021 09:34:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=kkwdoJIGvsW7E6rhoByJg5nAs3eFczhVd+MvdJJpI18=; b=qGHaSx/UlBq3fT7bOWw8v4WtpLzwISDRglaEYJFcQBgOupLdDvDCtvgvBQ5s3lmxSg Qnk6Tu/bMf2Ll24BFQNUi1QyXQyXTWeu/+GXQAOsUuZZt33XkS054wtvx/Nogp8zmLdH ZGFzFG0XyDq4qWw0pIZB9JcQwqO1FcbyVMdd7dv3M6rnorldK9rsMLTSW2F+yJ6YZ8h5 km13PAJmaDBqmQn44DUtXDth1qM69k2l+o8Roz5hG4FDeBYbpGYxX61LZ18985TeeYMn 2PczuNTLwAuC1inZ0rmuHUezMYE+P3Kuk30agrd5tyTGS+LDCXR4/JW7N35JG/izUPJq iM2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=kkwdoJIGvsW7E6rhoByJg5nAs3eFczhVd+MvdJJpI18=; b=Cfjdpnp+cQ6jrOhw92oiQzZGDvE+qNrHF788nrPEWqA6wMuIM+DSKS206Bnf/TaJqZ ImjkyNo+mRExqsKVDBR0nB1h0kaKJSWPWqm2ST6z89B2TsqTD3ffGsEihVtPzvPsw32x BbtAIAbFNvubE6i9seAlHsHdq9M8w7nco/XvZD3eQhCx2WFAg7G328eeC1ePV5KiRN3L fUL6vxY8sVq6ASYl5ibS5g8dFvnCzSMauuPyIHTu8dbiFpr+nw8NyA5AVPZFp6vMsODT vNsRKb8kshmpLvBYoMOdbcvJ0wVFS5EZiXUxQ/0oLT4VjvX+frWKs6d9ZR0ZQyTb7Tiy 4DAA== X-Gm-Message-State: AOAM532Ytyp8bTENc4kI5vrZvtwIp5mi2YlqzNlVT7TOVldOx93IxWRD /k2qpUyLJ++c0+gf1RO+VdysQVilqcQ= X-Google-Smtp-Source: ABdhPJxrYd/echyvro/44ZRTVuasMTDq61J+3yCP8qTNghRGTn2OkROQt5Le6/N6bkpMboEQMwfaEed2LNU= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:9bc4:: with SMTP id w4mr6530083ybo.168.1626194097439; Tue, 13 Jul 2021 09:34:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:21 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-44-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 43/46] KVM: VMX: Move RESET-only VMWRITE sequences to init_vmcs() From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Move VMWRITE sequences in vmx_vcpu_reset() guarded by !init_event into init_vmcs() to make it more obvious that they're, uh, initializing the VMCS. No meaningful functional change intended (though the order of VMWRITEs and whatnot is different). Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 4acfb2f450e6..97fa2aa676bd 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4393,6 +4393,19 @@ static void init_vmcs(struct vcpu_vmx *vmx) vmcs_write64(GUEST_IA32_RTIT_CTL, 0); } + vmcs_write32(GUEST_SYSENTER_CS, 0); + vmcs_writel(GUEST_SYSENTER_ESP, 0); + vmcs_writel(GUEST_SYSENTER_EIP, 0); + vmcs_write64(GUEST_IA32_DEBUGCTL, 0); + + if (cpu_has_vmx_tpr_shadow()) { + vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); + if (cpu_need_tpr_shadow(&vmx->vcpu)) + vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, + __pa(vmx->vcpu.arch.apic->regs)); + vmcs_write32(TPR_THRESHOLD, 0); + } + vmx_setup_uret_msrs(vmx); } @@ -4433,13 +4446,6 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); - if (!init_event) { - vmcs_write32(GUEST_SYSENTER_CS, 0); - vmcs_writel(GUEST_SYSENTER_ESP, 0); - vmcs_writel(GUEST_SYSENTER_EIP, 0); - vmcs_write64(GUEST_IA32_DEBUGCTL, 0); - } - vmcs_writel(GUEST_GDTR_BASE, 0); vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); @@ -4454,14 +4460,6 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ - if (cpu_has_vmx_tpr_shadow() && !init_event) { - vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); - if (cpu_need_tpr_shadow(vcpu)) - vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, - __pa(vcpu->arch.apic->regs)); - vmcs_write32(TPR_THRESHOLD, 0); - } - kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); vpid_sync_context(vmx->vpid); From patchwork Tue Jul 13 16:33:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374607 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D872C11F66 for ; Tue, 13 Jul 2021 16:36:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2646160FF3 for ; Tue, 13 Jul 2021 16:36:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235826AbhGMQiu (ORCPT ); Tue, 13 Jul 2021 12:38:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231931AbhGMQik (ORCPT ); Tue, 13 Jul 2021 12:38:40 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 352D1C0612FC for ; Tue, 13 Jul 2021 09:35:00 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id h7-20020a5b0a870000b029054c59edf217so27720902ybq.3 for ; Tue, 13 Jul 2021 09:35:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=wiSZeaOJz6E5eaSw6IcwZw34ds4nUby2LO6OctxjAfc=; b=JlLgNaatfTImrlLKDteCXIQCOvCb7D/zd/Eu4BP8cEmHg7r7VjKchWZkJwwyg08wAe aCHxM1JMig6P15Ukgg4qRI7XmC0/FhSLV8og8HvomKXEpPzVYK7x7byEbdHBk0cjwrMy BhSwQF2jP7TF/4PczCGUhGmF+ICPxtxma149TzwxP8LwjQ/5/UpPeSZIYCBExWDGAKxH gg7yF4UJOwYieLGaWNNPWQzqOqraVbPqBexeoeQlBg3WqgQ/pMjMIxmc3veB8GR7Twx/ +SOXWsMXyaRaLIQRiEZWiF+lTDWhzNx4F0M9LHtA8itikEIR1y1klXAV8e4S2pYVZ9cQ Uc+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=wiSZeaOJz6E5eaSw6IcwZw34ds4nUby2LO6OctxjAfc=; b=Fi+7S1SQKLixRNndoipXi/JYKyy7xMso3Kucz7WJAjZJte3yWlxqp+nLzrKNKp6njM 6UK+BL8fbmgZaNb+v5CNlnN+cgpzRZEnlLxpfecbTJcZ5OvRTGXKDDYPs9z3S0LfQJBy J6+xZ/m/4bTBSGApnFNZaP0ix1sCwGwptONBg7ra7dhGoPl3qDZNLghFbYEc1eIl2Xs4 pS5L5mA96wsKt1C1iPriso/tDUe+my7D+jHogHO/ZaSRz2rf1dBhiqs3rA6ad53ebRD8 0QXgkzxhlhmihaPzZBIHcIDMiZ2Bpe5vn0MqHdw3R/CSG9cx3cwXfA0KHjcru1TxFRxs SegA== X-Gm-Message-State: AOAM530FHKm6yi0sov6YiYQzavFG7WYuYdIPWO25nak38Kf4PPo+y+TU xrE1jAMy3Q9Kv5slsXfpKOaMnKoZLzU= X-Google-Smtp-Source: ABdhPJyBvicK0PC23syQ0zjJevsQ+gDXEZvCcjh5Ouah2do0HAGsJSUY+5MMhTO6SaMMg+0yinBYAWXikpA= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:afcd:: with SMTP id d13mr6691400ybj.504.1626194099437; Tue, 13 Jul 2021 09:34:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:22 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-45-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 44/46] KVM: SVM: Emulate #INIT in response to triple fault shutdown From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Emulate a full #INIT instead of simply initializing the VMCB if the guest hits a shutdown. Initializing the VMCB but not other vCPU state, much of which is mirrored by the VMCB, results in incoherent and broken vCPU state. Ideally, KVM would not automatically init anything on shutdown, and instead put the vCPU into e.g. KVM_MP_STATE_UNINITIALIZED and force userspace to explicitly INIT or RESET the vCPU. Even better would be to add KVM_MP_STATE_SHUTDOWN, since technically NMI can break shutdown (and SMI on Intel CPUs). But, that ship has sailed, and emulating #INIT is the next best thing as that has at least some connection with reality since there exist bare metal platforms that automatically INIT the CPU if it hits shutdown. Fixes: 46fe4ddd9dbb ("[PATCH] KVM: SVM: Propagate cpu shutdown events to userspace") Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 10 +++++++--- arch/x86/kvm/x86.c | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index ea4bea428078..285587a7fe80 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2058,11 +2058,15 @@ static int shutdown_interception(struct kvm_vcpu *vcpu) return -EINVAL; /* - * VMCB is undefined after a SHUTDOWN intercept - * so reinitialize it. + * VMCB is undefined after a SHUTDOWN intercept. INIT the vCPU to put + * the VMCB in a known good state. Unfortuately, KVM doesn't have + * KVM_MP_STATE_SHUTDOWN and can't add it without potentially breaking + * userspace. At a platform view, INIT is acceptable behavior as + * there exist bare metal platforms that automatically INIT the CPU + * in response to shutdown. */ clear_page(svm->vmcb); - init_vmcb(vcpu); + kvm_vcpu_reset(vcpu, true); kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; return 0; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3aa952edd5f4..f35dd8192c32 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10901,6 +10901,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) if (init_event) kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); } +EXPORT_SYMBOL_GPL(kvm_vcpu_reset); void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) { From patchwork Tue Jul 13 16:33:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374609 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 701DFC11F66 for ; Tue, 13 Jul 2021 16:36:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 58F2C60FF4 for ; Tue, 13 Jul 2021 16:36:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236039AbhGMQi4 (ORCPT ); Tue, 13 Jul 2021 12:38:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236037AbhGMQip (ORCPT ); Tue, 13 Jul 2021 12:38:45 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C7D8C05BD36 for ; Tue, 13 Jul 2021 09:35:02 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id h7-20020a5b0a870000b029054c59edf217so27721035ybq.3 for ; Tue, 13 Jul 2021 09:35:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=UT/52VUVOqVgs3pMVDFi63r7I/kst4+a2VnG271Mlf0=; b=Le2y0L9ajtK20eY1aYFmEMeU71b7R7G6iQZ4oxeoBAmPHNOoz8E3EMTZ23HbdSGHDO jzImdMVK1EXYwAnxlbaMGscMlX/RIO4GX4UqguOxBxAPu/uFYM30cPn2W/nb9ZUHGkCH nyLjtkAMAbEkcPSHk2wduvqFt3t1IalxUU2JnJSm0lEhKFWNLvD0aJbxpHV8n263yboj HToOwsuAE270xHVPtJFw6xiFRIAtkNLZkyK4QDbS9WdmncoUsvf8IG06OrVfRTc44cms LNE2gSfPd4mcPTpMLexF8dRc8pLy7Mz4a2K3yxaGbJcjYDJ4U317EktfEzEb4zMcgfT7 cDcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=UT/52VUVOqVgs3pMVDFi63r7I/kst4+a2VnG271Mlf0=; b=F8oSefbJSLv/LmJ809AyG2jao+6Sm3T3P6jhubIXjO9OER5E97F1PjV65padKKn0TQ J3/4eStT73jOHOpGrRRi0dmbdASlbtv8xvCgwqZ3rOxDNy/ghL9oLvf28HXR6bDuZL7E 5Zdp8yeTBAQB6utnEIBFBwkfXD+ajaLYCSKa1yWtJk19jfLyh5d/rFJ+ADPrtdFua3b9 Rya8TetwHfzhEH0w8z10vixeswbQOO+fXblHjgP3IrV2RLiwkwPl9iBcN6dZL/6woI2C dYNBaTAwFeS62MS6pe8f5kf6wQY/8lk2WYhlBd2bEXkUFRZVG6mwx4I5laWsNwZ5odBu L7ew== X-Gm-Message-State: AOAM5330KREaIuHbSRFmnCLAHxf66u2F4nHpZ695pyYpy6DZY6oevf0O r4664sGDEDevG5bF/1GKyOpDmzKJxr0= X-Google-Smtp-Source: ABdhPJxCevqBpFkZ2lfVrBSpKHEF9/P7qKhBWQULS515DzR83SlveXF4ojqHFpSeq1upJEmA7eXDn7GkQRQ= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a5b:c0a:: with SMTP id f10mr6638912ybq.9.1626194101487; Tue, 13 Jul 2021 09:35:01 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:23 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-46-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 45/46] KVM: SVM: Drop redundant clearing of vcpu->arch.hflags at INIT/RESET From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Drop redundant clears of vcpu->arch.hflags in init_vmcb() now that init_vmcb() is invoked only through kvm_vcpu_reset(), which always clears hflags. And of course, the second clearing in init_vmcb() was always redundant. Suggested-by: Reiji Watanabe Signed-off-by: Sean Christopherson Reviewed-by: Reiji Watanabe --- arch/x86/kvm/svm/svm.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 285587a7fe80..46d341f57e26 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1177,8 +1177,6 @@ static void init_vmcb(struct kvm_vcpu *vcpu) struct vmcb_control_area *control = &svm->vmcb->control; struct vmcb_save_area *save = &svm->vmcb->save; - vcpu->arch.hflags = 0; - svm_set_intercept(svm, INTERCEPT_CR0_READ); svm_set_intercept(svm, INTERCEPT_CR3_READ); svm_set_intercept(svm, INTERCEPT_CR4_READ); @@ -1277,7 +1275,6 @@ static void init_vmcb(struct kvm_vcpu *vcpu) svm->nested.vmcb12_gpa = INVALID_GPA; svm->nested.last_vmcb12_gpa = INVALID_GPA; - vcpu->arch.hflags = 0; if (!kvm_pause_in_guest(vcpu->kvm)) { control->pause_filter_count = pause_filter_count; From patchwork Tue Jul 13 16:33:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12374611 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9CE2CC07E95 for ; Tue, 13 Jul 2021 16:36:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7DDE560FF3 for ; Tue, 13 Jul 2021 16:36:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235744AbhGMQjN (ORCPT ); Tue, 13 Jul 2021 12:39:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236068AbhGMQiq (ORCPT ); Tue, 13 Jul 2021 12:38:46 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3DD26C05BD3D for ; Tue, 13 Jul 2021 09:35:04 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id l16-20020a25cc100000b0290558245b7eabso27776273ybf.10 for ; Tue, 13 Jul 2021 09:35:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=2rxcC4vIuxY5YCnXRcrldky6aRxCM98WRgaPiN4Y0rs=; b=eyv4YFSBvRhlJkYZ4guHD6EoCspFxgLX9Taq/OJ/Zs1/OeCzPSXj58SmF1tJfheNPQ FaPADp2ER+YkXFwezeosrFHo5tmarn8ns12RB/5m/73NFJWy5Ww98gIkk84AzOl1O1wE rTHUBkUPUmDZtEEPm2x1r+kq62dMjUKtd1cbZIWNMZwsXZqzS1wa3P3fLRnmKv3f1oY4 edCTgdIBfuTKTtUSC2MiWXVOWpWGtQxMhBQOkd2BdN+laE4PzbcnubXP2vU+ZZInHx/g gskZ//sHZVEEkZN1yQ4/+vcL2LhkQKVTBruvwCId633vdzOgO3AoVDUvDgwbXBvv2gSM L+sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=2rxcC4vIuxY5YCnXRcrldky6aRxCM98WRgaPiN4Y0rs=; b=hEkWUt3clXL9lMCYAV+ICqpPJWJ0pEqRAgRY7nhde1j8DVOSb4gN4yXJH0/6TfOrBf STw6dnD8HofqVulgSptDFpnnBaeUqQ+H3uwA62GCGwSAGM6dIrosEkEEnU30dZWKUjH8 GejS9y29GEApcVx5mIwU0xDFltw4OPjVe45fvYQCfay/cm9BEsx51SywM9SarGQM74wF YWIxtS/f1azhCxD3xJbnlGEcstBcCEQZmLGCe1s0FCNX3m7EPI0E8I69zKkE043Qk2Ho YlPKdNGtfEhFoJPpPdS5Sn7yPFyWM1M+LFTQCq/KdmqFXkCZTq5S4Qn2J71FB3tHoB15 Gbwg== X-Gm-Message-State: AOAM533ItIJk8Wj1fDfsS+2zjrGGsP2ZOtfoRwoHFuc6urREmfid+XZH 5WXPPg6Oj2hqL4o36X7Qj0xa2wDjYsg= X-Google-Smtp-Source: ABdhPJym3PBNjITgxPhnZwVrT14qQ1Y9+05uaLxuS6+kIvCIhq8Vjgfa7U6VT4khAaNQwcvT17uHumvZviM= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a25:ac8f:: with SMTP id x15mr7405404ybi.36.1626194103477; Tue, 13 Jul 2021 09:35:03 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:24 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-47-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 46/46] KVM: x86: Preserve guest's CR0.CD/NW on INIT From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Preserve CR0.CD and CR0.NW on INIT instead of forcing them to '1', as defined by both Intel's SDM and AMD's APM. Note, current versions of Intel's SDM are very poorly written with respect to INIT behavior. Table 9-1. "IA-32 and Intel 64 Processor States Following Power-up, Reset, or INIT" quite clearly lists power-up, RESET, _and_ INIT as setting CR0=60000010H, i.e. CD/NW=1. But the SDM then attempts to qualify CD/NW behavior in a footnote: 2. The CD and NW flags are unchanged, bit 4 is set to 1, all other bits are cleared. Presumably that footnote is only meant for INIT, as the RESET case and especially the power-up case are rather non-sensical. Another footnote all but confirms that: 6. Internal caches are invalid after power-up and RESET, but left unchanged with an INIT. Bare metal testing shows that CD/NW are indeed preserved on INIT (someone else can hack their BIOS to check RESET and power-up :-D). Reported-by: Reiji Watanabe Signed-off-by: Sean Christopherson Reviewed-by: Reiji Watanabe --- arch/x86/kvm/x86.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f35dd8192c32..3f0226259496 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10787,6 +10787,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) { unsigned long old_cr0 = kvm_read_cr0(vcpu); + unsigned long new_cr0; u32 eax, dummy; kvm_lapic_reset(vcpu, init_event); @@ -10873,7 +10874,18 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); kvm_rip_write(vcpu, 0xfff0); - static_call(kvm_x86_set_cr0)(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); + /* + * CR0.CD/NW are set on RESET, preserved on INIT. Note, some versions + * of Intel's SDM list CD/NW as being set on INIT, but they contradict + * (or qualify) that with a footnote stating that CD/NW are preserved. + */ + new_cr0 = X86_CR0_ET; + if (init_event) + new_cr0 |= (old_cr0 & (X86_CR0_NW | X86_CR0_CD)); + else + new_cr0 |= X86_CR0_NW | X86_CR0_CD; + + static_call(kvm_x86_set_cr0)(vcpu, new_cr0); static_call(kvm_x86_set_cr4)(vcpu, 0); static_call(kvm_x86_set_efer)(vcpu, 0); static_call(kvm_x86_update_exception_bitmap)(vcpu);