Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2639216pxb; Tue, 24 Aug 2021 04:11:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyKAEwgA0yr7dBqEqw5rvemGN1+ZtGneXg60BrU1gt4RmI6g5D3SXR0Zc3S3pZ6N3maCuDj X-Received: by 2002:aa7:dd12:: with SMTP id i18mr1098366edv.368.1629803490836; Tue, 24 Aug 2021 04:11:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629803490; cv=none; d=google.com; s=arc-20160816; b=wBGxbRvqIsgQD3+RDosSdZwILdtK46P1w4WNmQ6GlY3EK/JqfSuVwbCVaoWnUkE4Zz Qu3ojPCQfMP1Q0rBwR9FM6Efzu2UwK6k7/JAR6IPyRWFTGol+XXk8VMARwoCg//KeWmr QwGjCjHNgmi83jI2XiC82x3GyiLXMBfTUtzHugjbcgOWlvOGIjetUob5Iz9NdmiK/d4F CFZNYveC1a4eG0yFaDYGgvE9VNbGJS+XKei/5dyKvfL0JImvnS5KBKR5WN/Gx3HSvBgF kTlHr224DUwSjc8hXzO9ov+fbCx18go6rpucz+MAgf+OArxqD8amFyzCfMPPUli7zfQ+ L1lA== 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=fqQe5VceP4zwsEnfie8Mo5sRg1eQAHRfutSSo75V63o=; b=YbaoOWJzBRq29dKycirLz13qaWIb1UdJcpOxMAXhPNDgUM/Jrkx6+ag1aDzoIZ6ntF rBdUrItXj1sYU78LSCIGM0N8ZbOIypMIoP6P3+r5ZXRHD1Nfav/oMJCxCzJwRiIWWrxf Bv6JlwiQ1elCjbmOexuEUTtvCEDJS3iOQ39mAg6epuCRADWdpS1Ny9767xt1ou+n9fKW dBuz1dJEae+Xc229YJf9fC9MTK0q+5mGW2s8H8GbpAjjR7AwMcFtdDbupEkDkN4hEHU2 Hh57c+1TQmlYsb6hU9sWllfJWSq4ZR0Q7tJBXjZkEVfhqzHZsv+stV1igbCmZqiAoXQ+ ypTw== 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 q19si18762403edv.394.2021.08.24.04.11.07; Tue, 24 Aug 2021 04:11:30 -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 S236805AbhHXLJd (ORCPT + 99 others); Tue, 24 Aug 2021 07:09:33 -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 S236795AbhHXLJT (ORCPT ); Tue, 24 Aug 2021 07:09:19 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10085"; a="204423861" X-IronPort-AV: E=Sophos;i="5.84,347,1620716400"; d="scan'208";a="204423861" 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:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.84,347,1620716400"; d="scan'208";a="493501693" Received: from lxy-dell.sh.intel.com ([10.239.159.31]) by fmsmga008.fm.intel.com with ESMTP; 24 Aug 2021 04:08:32 -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 5/5] KVM: VMX: Check Intel PT related CPUID leaves Date: Tue, 24 Aug 2021 19:07:43 +0800 Message-Id: <20210824110743.531127-6-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 CPUID 0XD leaves reports the capabilities of Intel PT and decides which bits are valid to be set in MSR_IA32_RTIT_CTL. KVM needs to check the guest CPUID values set by userspace doesn't enable any bit which is not supported by bare metal. Otherwise, it allows guest to set corresponding bit in MSR_IA32_RTIT_CTL and it will trigger vm-entry failure if unsupported bit is exposed to guest and set by guest. Signed-off-by: Xiaoyao Li --- There is bit 31 of CPUID(0xD, 0).ECX that doesn't restrict any bit in MSR_IA32_RTIT_CTL. If guest has different value than host, it won't cause any vm-entry failure, but guest will parse the PT packet with wrong format. I also check it to be same as host to ensure the virtualization correctness. --- arch/x86/kvm/cpuid.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 739be5da3bca..0c8e06a24156 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -76,6 +76,7 @@ static inline struct kvm_cpuid_entry2 *cpuid_entry2_find( static int kvm_check_cpuid(struct kvm_cpuid_entry2 *entries, int nent) { struct kvm_cpuid_entry2 *best; + u32 eax, ebx, ecx, edx; /* * The existing code assumes virtual address is 48-bit or 57-bit in the @@ -89,6 +90,30 @@ static int kvm_check_cpuid(struct kvm_cpuid_entry2 *entries, int nent) return -EINVAL; } + /* + * CPUID 0xD leaves tell Intel PT capabilities, which decides + * pt_desc.ctl_bitmask in later update_intel_pt_cfg(). + * + * pt_desc.ctl_bitmask decides the legal value for guest + * MSR_IA32_RTIT_CTL. KVM cannot support PT capabilities beyond native, + * otherwise it will trigger vm-entry failure if guest sets native + * unsupported bits in MSR_IA32_RTIT_CTL. + */ + best = cpuid_entry2_find(entries, nent, 0xD, 0); + if (best) { + cpuid_count(0xD, 0, &eax, &ebx, &ecx, &edx); + if (best->ebx & ~ebx || best->ecx & ~ecx) + return -EINVAL; + } + best = cpuid_entry2_find(entries, nent, 0xD, 1); + if (best) { + cpuid_count(0xD, 0, &eax, &ebx, &ecx, &edx); + if (((best->eax & 0x7) > (eax & 0x7)) || + ((best->eax & ~eax) >> 16) || + (best->ebx & ~ebx)) + return -EINVAL; + } + return 0; } -- 2.27.0