Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1795914pxy; Fri, 23 Apr 2021 17:56:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUwK7TteD4wr0rXzMsB15wwRBymRJVlnS2hqxC9XGGvtsc1fXi86325e20B6dqYJfS+qMN X-Received: by 2002:aa7:db95:: with SMTP id u21mr7529167edt.152.1619225766363; Fri, 23 Apr 2021 17:56:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619225766; cv=none; d=google.com; s=arc-20160816; b=ZEYtRluu+uDsb3CUTK8MBF8qtcE9q0i71gp1sPPR8nSzC0kEjRCPwhzhTpgeWhvD2g mcoSJZltrJobJzFM2JNR8E78drTzSUyOCnmsJ5tXVcf3gvLwQ3du+sdQyWv1hujWfUwJ iS1BjG+Fg6l+rV4epJp11YZ3weFVhagy6Mivj6HVz8v58Ts7aeJ1bbnebaCSjuJGvuIZ i+O+lF6tkZsJOeIPJVvHRV4BisF9+GZQZ9l9Oid8yI/Qqtkr4WxIgPynTnJvYNKDgdlD VUppUqifmlrTqVLbIzYGORSHtIOW80bzMENfg10X2Cb651b3chC8Xx+KuYWSlZnPWL9L gH+A== 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=xQjgXxDowxam8JHFp2lMQSy0uU5l4G90MChbJDwG3P4=; b=wtgtahSGxewLLhqpDI/WprLv1CigR03LiJ7/cFzDn2ITzwkC6wuIN86+bgU5mP2xdf oC3YUDtRe0u8p6DQzaXdW1afJHc/mYgJxSTiHPJyKWTSkSpjH+H7o0PKAgI8aV87XRmy RRWd9S4E/ciAhK60ulDzfPy+6QeEpk05i1dJYFN5y8R/1DYQYWPtgjEOPCkdi+zerxrC KFsNpwTiFLGXYMZtsN3/2zwvHZpjn0cUTCTnZn1SWgvGv70/9GtP8BJab06jHVWOV3LN wbgbaJLNGoQCyHHQnt/bcgxeY30wQ5l2JDt1pI3C/5Zfr7lxkDNX4i7+g7qYPSGGnwjf d6AQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=vrkGY+oT; 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 g2si474781ejm.158.2021.04.23.17.55.43; Fri, 23 Apr 2021 17:56:06 -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=vrkGY+oT; 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 S244553AbhDXAxV (ORCPT + 99 others); Fri, 23 Apr 2021 20:53:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244501AbhDXAvr (ORCPT ); Fri, 23 Apr 2021 20:51:47 -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 5BBEBC061344 for ; Fri, 23 Apr 2021 17:47:58 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id d89-20020a25a3620000b02904dc8d0450c6so26112062ybi.2 for ; Fri, 23 Apr 2021 17:47: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=xQjgXxDowxam8JHFp2lMQSy0uU5l4G90MChbJDwG3P4=; b=vrkGY+oTI2Wmke2vymXqHiinOuYnSi6pt0qxIsFDWY4rSh3zLHKkzCF+4c/pNPTL1+ 56kMamCA5mbaPHuJ7yLSXgWMYUOOTWaFOBB6wLJJskT2qn0POVeJ3ueX7QT4vc5hwKle Lkk7ORtMHJ+w5qAavedqunXHaoueL6+Nitu4GDM4WJBYeNlZts309QUfSIRiYTwL+1Ak qQLtz0UDF3IzaxdMHoAacttefRIPqjwPck33g12HYKSoLLDsjh5dVm0VvaoXTM7dzPaj rizbGFen1vqHJvflNG2YRnIg+14a3fSBOUghG7UE+k2VqhbQFOcdItXJhxBMnax/+TST Rzfw== 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=xQjgXxDowxam8JHFp2lMQSy0uU5l4G90MChbJDwG3P4=; b=am/Gh5UyJD1PnVqd47g8JhYSoBZPyY8jUrCBkaWGqjCcDckAosWYn+ebsPfdebiIR0 OARvqekL3k/t8qwIWoA3aPQKJiAjeJrAsn9FchVE8PXJjCK+rVnoNHD5mXm1ZCVI5wvV wLNCQCyWJKclRWPO/NurmZYAXZBvGzTEOuKCNWCRFr8cDH6iGASrebHsCzt8vcTSgPKH QsJpz6RbsVrRFbmuH6AudTnJRZujMUU1ALv6YIC7UsOLettrV9ORsscrsaH5Ge4OnbGu 1WD9FMXQ8Uz4fe2NclmkwwY/WOWeyrjMhBdEvO7WvFA/5Wbr4jMIkkh8SQOPlbhKuMNP oUbw== X-Gm-Message-State: AOAM532Ct5YYn1dO9HeDxEaNBJPBPf3vib/EG6OnTJrpRkpeM3F/n9DA jWES9WWymlTrK75hH86K2Mw14TH6OsY= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:f:10:ad52:3246:e190:f070]) (user=seanjc job=sendgmr) by 2002:a25:7315:: with SMTP id o21mr6501271ybc.319.1619225277610; Fri, 23 Apr 2021 17:47:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 23 Apr 2021 17:46:28 -0700 In-Reply-To: <20210424004645.3950558-1-seanjc@google.com> Message-Id: <20210424004645.3950558-27-seanjc@google.com> Mime-Version: 1.0 References: <20210424004645.3950558-1-seanjc@google.com> X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog Subject: [PATCH 26/43] 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 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 596c8f9766ac..5f30181fd240 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -3111,9 +3111,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; @@ -3129,11 +3131,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 @@ -3174,17 +3181,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.31.1.498.g6c1eba8ee3d-goog