From patchwork Fri Nov 25 12:58:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaxi Chen X-Patchwork-Id: 25996 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3995138wrr; Fri, 25 Nov 2022 04:59:45 -0800 (PST) X-Google-Smtp-Source: AA0mqf5d83s2sdxv3ehd8LjT+/WgC0ghM7YjCJXPK8MrurNc6r5hpaVeikCvYcnF/fOlpoCAAbai X-Received: by 2002:a63:ef55:0:b0:45f:efc9:5935 with SMTP id c21-20020a63ef55000000b0045fefc95935mr33811604pgk.28.1669381184722; Fri, 25 Nov 2022 04:59:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669381184; cv=none; d=google.com; s=arc-20160816; b=a+ZGagYm4yHbjzl43ijHlKpPKfj/KPgpjF7eZSpkwYNcUSW/zmWiu/K/lnbdJnIeBx ie1aKafvpolEMEqpjoSGZO7a4rmSSuT1H/b/9+wWsjFqNqDZCxT0T8yT66FL7bO349qr k7q3xlYvVmuFgRfLvH0xanXYfmtROKzJbE9J17O2FcChbmx0yE0XyQ0qKDy3c9RdklHo u6uoqYWoawylQPLDxYjkgjUGiO/4vIBDrFY0J1s/CbYdIvEfA3NtlBZ1pRKK0dDXMxyA oLq8FoIVMHD4/hK/sNMmfv8j0J8+xxY2vboK6zqxbSwbcQoYIa3xxPD12uHjdN/kG8sb qXeA== 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 :dkim-signature; bh=7Z4hoP7GdakKMOBc3WVFKqiTYuXZswsZS9MuWNsieLY=; b=vxugJ9I7UMnwwh8GX2FHJHVtjQm4jOvr3V9p1FoQ7tbUV6Dnz8MkpNlz7+p4YVaXXn v/dlsr1nIEbxVptx+7rlZZSKVLG++cG3IiU/WoWDD6GTTVQqczTBUT53SHXqSWUu4NYz DL+7TEsFL9H2+yCkNKMypSysK6Hw7mb4HRMgKzpFZDx1yjUxd5RryEqs9eT8DINprzUJ CqmEP/d4OSBXKLgScQ6awgDGLS++VDz0/njqLXt1GX26tAfSfZ0KBP3Qst0TFRz4aMGF M+FdoNo+txax0V6jgyrsLqBoLPnHo6sp/mWPZC4vl1gBp6WfgFOhqtGTyAhdApFo61w9 IdzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fT0Ox0Jy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 74-20020a63044d000000b00476a48051edsi3895497pge.476.2022.11.25.04.59.31; Fri, 25 Nov 2022 04:59:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fT0Ox0Jy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229734AbiKYM6v (ORCPT + 99 others); Fri, 25 Nov 2022 07:58:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229652AbiKYM6s (ORCPT ); Fri, 25 Nov 2022 07:58:48 -0500 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C75522870A; Fri, 25 Nov 2022 04:58:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1669381127; x=1700917127; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gVUECM94yIRd057tWclrKJlWO+Krnw3qbsLeXiY67U4=; b=fT0Ox0Jyw2gCVYCnv8Cq/WO2HrKeK2yu9P1GZXcaXGGvXyLUBa2d3uY4 8pTs7muKOCnXoSuOo8jyZBa5NGToV0PEsTJSQLxOWJI10G1p1XzFSNcjL ISzUnG72076CzBcmUkNlEPPBEQjhbP2zJC46q6W3Rycn6jHwFdi+DRkcp OVGfK14rkQUNK/OE2EXOKXhxBd29Cc4DpOMAcJhw8UQp79HiGvKQKXKGg SfIfqApcC9LfucsEX1fL/4UF7eBoVWZBLaNyciFAQ9n4nJPQ00bH9Bhno 0tVmxOUflLXMQZFb1zSNNWqBnnUse0VcwE+Q9sS8r6O8VEQu2GIf6oW/e Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10541"; a="294180562" X-IronPort-AV: E=Sophos;i="5.96,193,1665471600"; d="scan'208";a="294180562" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Nov 2022 04:58:47 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10541"; a="706061243" X-IronPort-AV: E=Sophos;i="5.96,193,1665471600"; d="scan'208";a="706061243" Received: from jiaxichen-precision-3650-tower.sh.intel.com ([10.239.159.75]) by fmsmga008.fm.intel.com with ESMTP; 25 Nov 2022 04:58:42 -0800 From: Jiaxi Chen To: kvm@vger.kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, ndesaulniers@google.com, alexandre.belloni@bootlin.com, peterz@infradead.org, jpoimboe@kernel.org, chang.seok.bae@intel.com, pawan.kumar.gupta@linux.intel.com, babu.moger@amd.com, jmattson@google.com, sandipan.das@amd.com, tony.luck@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, fenghua.yu@intel.com, keescook@chromium.org, nathan@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 2/8] KVM: x86: Update KVM-only leaf handling to allow for 100% KVM-only leafs Date: Fri, 25 Nov 2022 20:58:39 +0800 Message-Id: <20221125125845.1182922-3-jiaxi.chen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221125125845.1182922-1-jiaxi.chen@linux.intel.com> References: <20221125125845.1182922-1-jiaxi.chen@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750473044944428429?= X-GMAIL-MSGID: =?utf-8?q?1750473044944428429?= From: Sean Christopherson Rename kvm_cpu_cap_init_scattered() to kvm_cpu_cap_init_kvm_defined() in anticipation of adding KVM-only CPUID leafs that aren't recognized by the kernel and thus not scattered, i.e. for leafs that are 100% KVM-defined. Adjust/add comments to kvm_only_cpuid_leafs and KVM_X86_FEATURE to document how to create new kvm_only_cpuid_leafs entries for scattered features as well as features that are entirely unknown to the kernel. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/cpuid.c | 8 ++++---- arch/x86/kvm/reverse_cpuid.h | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 65b103874139..a3adac48766b 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -549,9 +549,9 @@ static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf) } static __always_inline -void kvm_cpu_cap_init_scattered(enum kvm_only_cpuid_leafs leaf, u32 mask) +void kvm_cpu_cap_init_kvm_defined(enum kvm_only_cpuid_leafs leaf, u32 mask) { - /* Use kvm_cpu_cap_mask for non-scattered leafs. */ + /* Use kvm_cpu_cap_mask for leafs that aren't KVM-only. */ BUILD_BUG_ON(leaf < NCAPINTS); kvm_cpu_caps[leaf] = mask; @@ -561,7 +561,7 @@ void kvm_cpu_cap_init_scattered(enum kvm_only_cpuid_leafs leaf, u32 mask) static __always_inline void kvm_cpu_cap_mask(enum cpuid_leafs leaf, u32 mask) { - /* Use kvm_cpu_cap_init_scattered for scattered leafs. */ + /* Use kvm_cpu_cap_init_kvm_defined for KVM-only leafs. */ BUILD_BUG_ON(leaf >= NCAPINTS); kvm_cpu_caps[leaf] &= mask; @@ -670,7 +670,7 @@ void kvm_set_cpu_caps(void) F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | f_xfd ); - kvm_cpu_cap_init_scattered(CPUID_12_EAX, + kvm_cpu_cap_init_kvm_defined(CPUID_12_EAX, SF(SGX1) | SF(SGX2) ); diff --git a/arch/x86/kvm/reverse_cpuid.h b/arch/x86/kvm/reverse_cpuid.h index a19d473d0184..443a6b3e66c0 100644 --- a/arch/x86/kvm/reverse_cpuid.h +++ b/arch/x86/kvm/reverse_cpuid.h @@ -7,9 +7,9 @@ #include /* - * Hardware-defined CPUID leafs that are scattered in the kernel, but need to - * be directly used by KVM. Note, these word values conflict with the kernel's - * "bug" caps, but KVM doesn't use those. + * Hardware-defined CPUID leafs that are either scattered by the kernel or are + * unknown to the kernel, but need to be directly used by KVM. Note, these + * word values conflict with the kernel's "bug" caps, but KVM doesn't use those. */ enum kvm_only_cpuid_leafs { CPUID_12_EAX = NCAPINTS, @@ -18,6 +18,18 @@ enum kvm_only_cpuid_leafs { NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS, }; +/* + * Define a KVM-only feature flag. + * + * For features that are scattered by cpufeatures.h, __feature_translate() also + * needs to be updated to translate the kernel-defined feature into the + * KVM-defined feature. + * + * For features that are 100% KVM-only, i.e. not defined by cpufeatures.h, + * forego the intermediate KVM_X86_FEATURE and directly define X86_FEATURE_* so + * that X86_FEATURE_* can be used in KVM. No __feature_translate() handling is + * needed in this case. + */ #define KVM_X86_FEATURE(w, f) ((w)*32 + (f)) /* Intel-defined SGX sub-features, CPUID level 0x12 (EAX). */