Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp631529pxk; Wed, 23 Sep 2020 11:47:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy4H9dDax6qMH+V/eaP2BnNa/5f3LmADXg058/FLM5fO/ksNR0MDLtHf/BC+aTaXaX0PDg3 X-Received: by 2002:aa7:d805:: with SMTP id v5mr735941edq.29.1600886856088; Wed, 23 Sep 2020 11:47:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600886856; cv=none; d=google.com; s=arc-20160816; b=CO1kZg1aMpYWK67PTokcUZmisgLz71dkRRTWgKEg8e5LWt15RxID0swCKKko9akR1E bVtOT68zkeAxzdrEK1UtwH4JVK4h8LjHO9gJXrktYxlk74XvJmcWbzFN3vhnWLDvkUZN PZuKuHgPEk1b6K9Li+/+C0YnT1+UhcNwpw9AVh0rClMUToJ27rcjyUBCbtRNT9E3W4Qj Myng4Cw+Cy/S0ymyWZHidf72kKYL7Vper9SW80qlYop47vayoLelVZSxa7wsaHY5/JaJ sMbTz9FnHYGC8e7r1WTHQFZ6Q3vEIlgY0I7kQQAABzOk1aM4XxBD6j+v/Wp1kfcrrb3S AnDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=cKfsn+1H/CFGNQ5JjmMdtTXPRqajMC1mUvAHvWphDm8=; b=XK0R8dZ69y8togDJKnoImFEvhb70ypOIlwxVSbvcsInxdcf/KovMGapj1yQXGAvlvA U45ewk1+RZyg+cnTJWtP5gQfOglacVBp+RJLXJnjFr+hGWYyF0PJhsNRW+lp2yinZjAR fzmjGfmli9eybjKnO/02G/t02bzScEHWHKXX63Lgvw0iurY+Y4DGW8ugyeZUJh6SXAna iMyOo6eRoNjP3Zl5ho/Yl/HJIAoPq9rRy4Aysg9NyYrKxgvwTX8/9tNGkKOTaquBhenD DPOQxsjMPHL9oVKXlUih6Lqm5crI0tcpwCF2nnx5r8DynJb12CRQZoUX+JEu0+DG63am dcew== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mb24si497736ejb.583.2020.09.23.11.47.12; Wed, 23 Sep 2020 11:47:36 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726896AbgIWSoz (ORCPT + 99 others); Wed, 23 Sep 2020 14:44:55 -0400 Received: from mga07.intel.com ([134.134.136.100]:14508 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726572AbgIWSoz (ORCPT ); Wed, 23 Sep 2020 14:44:55 -0400 IronPort-SDR: 1F7CkZkGOyF8NgCYve3Ca3B9ZBVcic9I5Dd8XC7vG035bEyI9ZdlbzjxsHVmGzOKoi2OGdG18G yayQ+VLGfYSg== X-IronPort-AV: E=McAfee;i="6000,8403,9753"; a="225124476" X-IronPort-AV: E=Sophos;i="5.77,293,1596524400"; d="scan'208";a="225124476" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Sep 2020 11:44:54 -0700 IronPort-SDR: kD4EuJhIxwZ98QtVceAE/7zQkm55kLcJtHz1/7pjNAWRZPuVoZwIpeuMm99Gh+f/y2dIvaj/QN B0F0czfVTbVQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.77,293,1596524400"; d="scan'208";a="347457655" Received: from sjchrist-coffee.jf.intel.com ([10.54.74.160]) by FMSMGA003.fm.intel.com with ESMTP; 23 Sep 2020 11:44:54 -0700 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, Dan Cross , Peter Shier Subject: [PATCH v2 3/7] KVM: nVMX: Explicitly check for valid guest state for !unrestricted guest Date: Wed, 23 Sep 2020 11:44:48 -0700 Message-Id: <20200923184452.980-4-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200923184452.980-1-sean.j.christopherson@intel.com> References: <20200923184452.980-1-sean.j.christopherson@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Call guest_state_valid() directly instead of querying emulation_required when checking if L1 is attempting VM-Enter with invalid guest state. If emulate_invalid_guest_state is false, KVM will fixup segment regs to avoid emulation and will never set emulation_required, i.e. KVM will incorrectly miss the associated consistency checks because the nested path stuffs segments directly into vmcs02. Opportunsitically add Consistency Check tracing to make future debug suck a little less. Fixes: 2bb8cafea80bf ("KVM: vVMX: signal failure for nested VMEntry if emulation_required") Fixes: 3184a995f782c ("KVM: nVMX: fix vmentry failure code when L2 state would require emulation") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/vmx.c | 8 ++------ arch/x86/kvm/vmx/vmx.h | 9 +++++++++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 171e34286908..a50714a86dde 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -2573,7 +2573,7 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, * which means L1 attempted VMEntry to L2 with invalid state. * Fail the VMEntry. */ - if (vmx->emulation_required) { + if (CC(!vmx_guest_state_valid(vcpu))) { *entry_failure_code = ENTRY_FAIL_DEFAULT; return -EINVAL; } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 6f9a0c6d5dc5..e8480dbef881 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -341,7 +341,6 @@ static const struct kernel_param_ops vmentry_l1d_flush_ops = { }; module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644); -static bool guest_state_valid(struct kvm_vcpu *vcpu); static u32 vmx_segment_access_rights(struct kvm_segment *var); static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr, int type); @@ -1415,7 +1414,7 @@ static void vmx_vcpu_put(struct kvm_vcpu *vcpu) static bool emulation_required(struct kvm_vcpu *vcpu) { - return emulate_invalid_guest_state && !guest_state_valid(vcpu); + return emulate_invalid_guest_state && !vmx_guest_state_valid(vcpu); } unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) @@ -3471,11 +3470,8 @@ static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) * not. * We assume that registers are always usable */ -static bool guest_state_valid(struct kvm_vcpu *vcpu) +bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu) { - if (enable_unrestricted_guest) - return true; - /* real mode guest state checks */ if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index d7ec66db5eb8..e147f180350f 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -343,6 +343,15 @@ 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); u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa, int root_level); + +bool __vmx_guest_state_valid(struct kvm_vcpu *vcpu); +static inline bool vmx_guest_state_valid(struct kvm_vcpu *vcpu) +{ + if (enable_unrestricted_guest) + return true; + + return __vmx_guest_state_valid(vcpu); +} void update_exception_bitmap(struct kvm_vcpu *vcpu); void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); bool vmx_nmi_blocked(struct kvm_vcpu *vcpu); -- 2.28.0