Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp3844267pxy; Mon, 26 Apr 2021 11:04:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxThX5YtLRq/v5eEyQzRRXZ+GYDDuN+NRzr2foPyulakVlaOuyr8a4NrT39ml5ucN+IfIUG X-Received: by 2002:aa7:c746:: with SMTP id c6mr22370642eds.169.1619460277524; Mon, 26 Apr 2021 11:04:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619460277; cv=none; d=google.com; s=arc-20160816; b=L+NWnRlLoFLNaMidYubIKuSunIYSXv7UoDIItCPTHRzViKHnSTeBQGCG5EoIHAQcfv LwHP05nfHhOcP3o1z3lk5QC55ExNS5yvMrteg3eMbeE9FNDUp22sbGvpZnMQ4ukwLyZB 2RjwBnPSHwkJTE52/97tcHWbVfK+asbX75GNh8QTouuNdLIvz1sEiNhRJGsnJiskAKG0 DOq45r71oDOrgIye3IcdTkCSXphmUkaLZxtakv1zhYqyL/XBJHrXY660CzRNTt1mnHiq PRIgLoMqY8k8Jw1TrPdEsChjX/tUZy9gmaKper/i6o/L1RsbTLt99zkoiLaklZjNuY3J en0w== 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=XWyofL2tsR0NtB2hjloqCPB6dC/dM0d31U3vzI6yIew=; b=H6PdPed18JxI5UDZaGNehfKVqzP4O3yFi/P5rPn7eomLt2oJTPz4nlF3prtaeQRXW8 F73ItQhI/KnnwgBOGPYPAGriQ7LBRRmEBIU8Q3MJ09F6cP1kxz0JEoPAvwDIkx3jsII6 f4Tjc0dab8mwJbryT4zmdGggILlk5w7LJattPBpVvgvir/MNULUMFF2j9iYFz9WWNAD7 WUg35Fn79HL5VnNCrxYWd36YCBX8MMVUx4YABPcfRyXTOn6FknSf6cmqi/DdO0riCA/0 et7D8KijSysJF2Svsug14Ono3+s84xPU2Rp1M1gUCx2K3X3P7m+rdhWgUWh8En7SswYs go8w== 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 f14si461702edv.139.2021.04.26.11.04.12; Mon, 26 Apr 2021 11:04:37 -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 S234385AbhDZSDh (ORCPT + 99 others); Mon, 26 Apr 2021 14:03:37 -0400 Received: from mga14.intel.com ([192.55.52.115]:31731 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234231AbhDZSDX (ORCPT ); Mon, 26 Apr 2021 14:03:23 -0400 IronPort-SDR: FA+kx4EARuUNJ0gFI+noQRS8N2aYLPf9kFyr5KVKhQhQYDhl3ZVL7UwHhEpXy6qCpXY0+sgKNW 4tfxv+l35vUw== X-IronPort-AV: E=McAfee;i="6200,9189,9966"; a="195934020" X-IronPort-AV: E=Sophos;i="5.82,252,1613462400"; d="scan'208";a="195934020" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Apr 2021 11:02:42 -0700 IronPort-SDR: Io6PjEDlYejx2FeDv2IovhT29X9k1BVlQxOVFkAQ3JLQ9toqBPTbHM29Mw7G1yHKysoYIFAfkM JMcV0bl3Votw== X-IronPort-AV: E=Sophos;i="5.82,252,1613462400"; d="scan'208";a="447353341" Received: from ssumanpx-mobl.amr.corp.intel.com (HELO skuppusw-mobl5.amr.corp.intel.com) ([10.254.34.197]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Apr 2021 11:02:41 -0700 From: Kuppuswamy Sathyanarayanan To: Peter Zijlstra , Andy Lutomirski , Dave Hansen , Dan Williams , Tony Luck Cc: Andi Kleen , Kirill Shutemov , Kuppuswamy Sathyanarayanan , Raj Ashok , Sean Christopherson , linux-kernel@vger.kernel.org, Kuppuswamy Sathyanarayanan , Sean Christopherson Subject: [RFC v2 04/32] x86/x86: Add is_tdx_guest() interface Date: Mon, 26 Apr 2021 11:01:31 -0700 Message-Id: <72a11cdb0a3e5086cfc11a49ba3e20d05dc3a119.1619458733.git.sathyanarayanan.kuppuswamy@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add helper function to detect TDX feature support. It will be used to protect TDX specific code. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Reviewed-by: Andi Kleen Reviewed-by: Tony Luck Signed-off-by: Kuppuswamy Sathyanarayanan --- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/tdx.c | 32 +++++++++++++++++++++++++++++++ arch/x86/include/asm/tdx.h | 8 ++++++++ arch/x86/kernel/tdx.c | 6 ++++++ 4 files changed, 47 insertions(+) create mode 100644 arch/x86/boot/compressed/tdx.c diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index e0bc3988c3fa..a2554621cefe 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -96,6 +96,7 @@ ifdef CONFIG_X86_64 endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o +vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o efi-obj-$(CONFIG_EFI_STUB) = $(objtree)/drivers/firmware/efi/libstub/lib.a diff --git a/arch/x86/boot/compressed/tdx.c b/arch/x86/boot/compressed/tdx.c new file mode 100644 index 000000000000..0a87c1775b67 --- /dev/null +++ b/arch/x86/boot/compressed/tdx.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * tdx.c - Early boot code for TDX + */ + +#include + +static int __ro_after_init tdx_guest = -1; + +static inline bool native_cpuid_has_tdx_guest(void) +{ + u32 eax = TDX_CPUID_LEAF_ID, signature[3] = {0}; + + if (native_cpuid_eax(0) < TDX_CPUID_LEAF_ID) + return false; + + native_cpuid(&eax, &signature[0], &signature[1], &signature[2]); + + if (memcmp("IntelTDX ", signature, 12)) + return false; + + return true; +} + +bool is_tdx_guest(void) +{ + if (tdx_guest < 0) + tdx_guest = native_cpuid_has_tdx_guest(); + + return !!tdx_guest; +} + diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index 679500e807f3..69af72d08d3d 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -9,10 +9,18 @@ #include +/* Common API to check TDX support in decompression and common kernel code. */ +bool is_tdx_guest(void); + void __init tdx_early_init(void); #else // !CONFIG_INTEL_TDX_GUEST +static inline bool is_tdx_guest(void) +{ + return false; +} + static inline void tdx_early_init(void) { }; #endif /* CONFIG_INTEL_TDX_GUEST */ diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c index f927e36769d5..6a7193fead08 100644 --- a/arch/x86/kernel/tdx.c +++ b/arch/x86/kernel/tdx.c @@ -19,6 +19,12 @@ static inline bool cpuid_has_tdx_guest(void) return true; } +bool is_tdx_guest(void) +{ + return static_cpu_has(X86_FEATURE_TDX_GUEST); +} +EXPORT_SYMBOL_GPL(is_tdx_guest); + void __init tdx_early_init(void) { if (!cpuid_has_tdx_guest()) -- 2.25.1