Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2639174pxb; Tue, 24 Aug 2021 04:11:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXpUrRkMiwU5kbhOMDhNPeUXI+DujhI4s39o4ju7ZOLC4f0Ve1XsvTEg/ZPw0q2Pez+00n X-Received: by 2002:aa7:de92:: with SMTP id j18mr42224656edv.141.1629803486308; Tue, 24 Aug 2021 04:11:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629803486; cv=none; d=google.com; s=arc-20160816; b=ykHpmnfU/LgS6ShjR/blqEUUsxDRA5Pue6y0AZe3UzCH3rhpUM25jY/7duHdhjcwfv J0dQqIXTfHA0jPxiVODX6Xo8RVK+JLUZxoQ570pxZHlGXqL6MnwDYwQG8skAoufYUW7i 0i+/X/wqne+V+o5bbKg6hm5WVy3saRS8elwrY8COVa8YcTJTcCtdDL9E7em87iYnLIpG Pnvqo3dF+ooQZml4+G49DiQPsNyKtyNOscq4LKnPKn4+wPMXJgd6FLwDYUqLYRcMQDaR qBRulO5BxGch1uzg5baxuHTfxM32Iivvz+TFl/GJUnhwL7vw7hxMhjPoo4pPOnLSyzAS Cn4w== 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; bh=tR/85sls5k73tFMPzp1rBtieGPQtrXy6qFbduMQr42w=; b=idncP+4AIcKNRGU1NMP7TCfOUifXQuRG1KkSv//8QcE3/ej09qcZwJrx+dBWhrvQUs i2rB1S9XMe5xHDSGQIBngKC6k+Qa2Rlh2aNux95yDGQJA2w4lj+iUFn0M6PVW8I05Jkf +4L4Zc7bY9RYMCk6iN04C45noSqnwzNS4WgZQldtfb9X/X6Ef0MZ14k0QRF4SvBecetv ZI6ree8csOlkIHoR0QpQnXoKOLNR8PTKJVP3FIKFbwLDuXL36ZWjNMJ2w4wvi6JS6/Pb YEdzTdanZt/KDzz7PFEi17UZkKvlcCX+FOdVWAK5VWNcMSMm2xYHQCWp/9y2m/IYiKyp B89w== 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 hs11si11045398ejc.85.2021.08.24.04.11.02; Tue, 24 Aug 2021 04:11:26 -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 S236820AbhHXLJa (ORCPT + 99 others); Tue, 24 Aug 2021 07:09:30 -0400 Received: from mga18.intel.com ([134.134.136.126]:3711 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236759AbhHXLJR (ORCPT ); Tue, 24 Aug 2021 07:09:17 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10085"; a="204423855" X-IronPort-AV: E=Sophos;i="5.84,347,1620716400"; d="scan'208";a="204423855" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2021 04:08:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.84,347,1620716400"; d="scan'208";a="493501681" Received: from lxy-dell.sh.intel.com ([10.239.159.31]) by fmsmga008.fm.intel.com with ESMTP; 24 Aug 2021 04:08:30 -0700 From: Xiaoyao Li To: Paolo Bonzini Cc: Xiaoyao Li , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/5] KVM: VMX: Disallow PT MSRs accessing if PT is not exposed to guest Date: Tue, 24 Aug 2021 19:07:42 +0800 Message-Id: <20210824110743.531127-5-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210824110743.531127-1-xiaoyao.li@intel.com> References: <20210824110743.531127-1-xiaoyao.li@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Per SDM, it triggers #GP for all the accessing of PT MSRs, if X86_FEATURE_INTEL_PT is not available. Signed-off-by: Xiaoyao Li --- arch/x86/kvm/vmx/vmx.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 4a70a6d2f442..1bbc4d84c623 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1010,9 +1010,16 @@ static unsigned long segment_base(u16 selector) static inline bool pt_can_write_msr(struct vcpu_vmx *vmx) { return vmx_pt_mode_is_host_guest() && + guest_cpuid_has(&vmx->vcpu, X86_FEATURE_INTEL_PT) && !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN); } +static inline bool pt_can_read_msr(struct kvm_vcpu *vcpu) +{ + return vmx_pt_mode_is_host_guest() && + guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT); +} + static inline bool pt_output_base_valid(struct kvm_vcpu *vcpu, u64 base) { /* The base must be 128-byte aligned and a legal physical address. */ @@ -1849,24 +1856,24 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) &msr_info->data); break; case MSR_IA32_RTIT_CTL: - if (!vmx_pt_mode_is_host_guest()) + if (!pt_can_read_msr(vcpu)) return 1; msr_info->data = vmx->pt_desc.guest.ctl; break; case MSR_IA32_RTIT_STATUS: - if (!vmx_pt_mode_is_host_guest()) + if (!pt_can_read_msr(vcpu)) return 1; msr_info->data = vmx->pt_desc.guest.status; break; case MSR_IA32_RTIT_CR3_MATCH: - if (!vmx_pt_mode_is_host_guest() || + if (!pt_can_read_msr(vcpu) || !intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering)) return 1; msr_info->data = vmx->pt_desc.guest.cr3_match; break; case MSR_IA32_RTIT_OUTPUT_BASE: - if (!vmx_pt_mode_is_host_guest() || + if (!pt_can_read_msr(vcpu) || (!intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output) && !intel_pt_validate_cap(vmx->pt_desc.caps, @@ -1875,7 +1882,7 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data = vmx->pt_desc.guest.output_base; break; case MSR_IA32_RTIT_OUTPUT_MASK: - if (!vmx_pt_mode_is_host_guest() || + if (!pt_can_read_msr(vcpu) || (!intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output) && !intel_pt_validate_cap(vmx->pt_desc.caps, @@ -1885,7 +1892,7 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) break; case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: index = msr_info->index - MSR_IA32_RTIT_ADDR0_A; - if (!vmx_pt_mode_is_host_guest() || + if (!pt_can_read_msr(vcpu) || (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_num_address_ranges))) return 1; @@ -2154,6 +2161,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) return vmx_set_vmx_msr(vcpu, msr_index, data); case MSR_IA32_RTIT_CTL: if (!vmx_pt_mode_is_host_guest() || + !guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT) || vmx_rtit_ctl_check(vcpu, data) || vmx->nested.vmxon) return 1; -- 2.27.0