Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp4051294pxv; Tue, 13 Jul 2021 09:36:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJweTZ4GWoF21xjV9SX5CGj++WfhscecXmr60aNNQyB1pf1xocr4G037mVFczxwtxGXmS6GG X-Received: by 2002:a92:d848:: with SMTP id h8mr3627999ilq.282.1626194209419; Tue, 13 Jul 2021 09:36:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626194209; cv=none; d=google.com; s=arc-20160816; b=kkvfkTfcyw5FzSs1eBluNE3FbVFO+JweVXJFD+MdJ+lCrDwvhbQKZ4DwPFctjVU4lD wZlH3vqq+W9bU2ST8ODCrQiPJNvwU1d20xXS4EU14pQ/70SPhC7V14+OdA/B3mPQYacv wiXM1vVRoli/cx1+svFAJA8EceMGsrO0HzY6sAb1s2qbhxtSJgj2pTmPzkuH9SBo/bdF vo7DHDNIBth6+vILYqKOunDnx5CRA47RbDuAbf1w8HzZgKkhxXVbykEngQBkdMPYmOru VBMH8oXS1amqV5wXCZC7mBMErtdlXDqBQXw5fgMmEE6uQ4cEgsqNbGMy3Kfp6zzaL6De wHqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=kqLCR8V7aHa3dOA8rm3sPsU8TE7Ylyd+IJ7q57+xdpQ=; b=M0fqoZ3NKN2KkOKmaDSRjHRQKL3J+zt7ceEonyJWZQ0NzrTfRW6/KsG46kqMMURAGF 3UvuDJ1hObzRa9b/2lEt+bXnpgRa1YPFp5x3Sdyz5LKfbNy1EHVA3yjyCr8k0DHcVqYs MgUADXvn+bzxwa0xIwnRbmxX3PEwdnn7RDD4TL5xq9oj8Ic59m20GfBHuIXzhOZCnF8W M+Ri3QH+10ClJ5JN8iH35/Jo0iy1d1Y9fAVjoHRm7/V0TA0WZG0+iXqHsFJDuRbcHDM/ F6DXrtS2cnTrXHIFlNKLc2l7KB7CkiyMjOvclaMX8Cn9xD4exVKT//BIZKCx9ezSuAO5 OadA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=L0uvO4Nv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u26si10149815jam.87.2021.07.13.09.36.37; Tue, 13 Jul 2021 09:36:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=L0uvO4Nv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235573AbhGMQhw (ORCPT + 99 others); Tue, 13 Jul 2021 12:37:52 -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 S234850AbhGMQhk (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 EE528C061225 for ; Tue, 13 Jul 2021 09:34:25 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id v184-20020a257ac10000b02904f84a5c5297so27713660ybc.16 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=IPlpRVWBUmAcIlHZcp60xTiC6KH4bd3b8mOzmKvQ63BpbAB6qgIBmQYer+pIinY7G1 SWsAdqkKUoLuT/7ojDSlpKTxM6wdQGf+E1avLzXWO1Xxz953pU/4sadn4PlUxfe8StmB /oEXUq22gEddrZAAmtlpsW4RGlAJeCoV+0DKZ27cjYUCLjMcQRXfSo13XMSiorTxOW4P foUUKAk11yUbtz549A4DORXnXtTCEzybr0nLF9VG7eylWewr95vos9f0tHjvaal2NDsl fE/Ln5ZgSVrdq0UZYB4enhqams5W0WojTYcWMDuWEZFohS2A9e7hCkn5F37E21x4tHBZ az7w== X-Gm-Message-State: AOAM533n+pdikvzYoUp1wg6IgzqM+F4Gn9/ZUA/cln86b7Nd2oryWDMx uX1qOdeiu7PW3FeHkwknsQemuNM1lXA= 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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); } -- 2.32.0.93.g670b81a890-goog