Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3525265pxb; Mon, 24 Jan 2022 11:24:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJyaBO90lR+3MncpwGc7qkBfLPDh3WHWUmvypC69QNyuCQsjVABMDSpXWDma0v2+DytoJaOt X-Received: by 2002:a17:90b:4ac5:: with SMTP id mh5mr3324550pjb.197.1643052242323; Mon, 24 Jan 2022 11:24:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643052242; cv=none; d=google.com; s=arc-20160816; b=SBbonFBkG24XdxJ39X/CeJuFt+tr2IMmF5WhnSLxemEtQlH32imwd5fTeW4JZLZs7t outE4ndJ8qmgUIcugR9DG9Bxv48tsAt0dnhZ52V4RzaXSYLj77ymhtfb2jBG8hOKtMHY nQMRuvZvW8vgER2ZIRHX414F05hP2vY6mlLFSra/ReBzOTMSuj0Qv2yJDqqxVxvy5VbH HxhsvOlho5C23y5HdbvjiVvVUwwCXdJrq9MvEUt+MCm/nFPNcQoPe3tvYiDk84jwB8dh BV2oHXhuubmsNOknTEn6IWLdJW5RX7szt09sa8p+0QjL5hWH2q8EeDiNYXm2Y6VU0r1g n6dA== 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=08DaS03TEMdJbWvf34xC1Pd1WphlLrfN8lo/WG+VOsA=; b=rYz9Qy98eCotw9RPn/IIDqqKjOtEXxGoLNyoyeTSVFs2ZZ5suymIuLBct3JC3x7Aoe Y+YwXRxUpM1DmOr/EzStYdhN4cYlfbYOxyxPYIZinwAuhJHiNvZZHFFrQudrX4f4WZxv NUiY/Mbgt9THvUqVLbUc0ZfOD4SZenIrC5PGzt/OB7awpLnO8WlsBdP4XptLa/TNldWr LBBkZe0Dciu9EYtBLVbhXCLPJ911PvPyZUUjgaEE6YiGDmB5+Eh69aKle3f8+J75zg1v qzkDRAjt9KRB+ghKCXtJlMAWjvuK8F3jG/NPvHImYH/UxXvi1sh4qv0Qf3rp1Wpnhi3l I3iA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="i1/ziA95"; 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=pass (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 d3si14003082pgh.861.2022.01.24.11.23.45; Mon, 24 Jan 2022 11:24:02 -0800 (PST) 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; dkim=pass header.i=@intel.com header.s=Intel header.b="i1/ziA95"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238993AbiAXPCQ (ORCPT + 99 others); Mon, 24 Jan 2022 10:02:16 -0500 Received: from mga01.intel.com ([192.55.52.88]:64635 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234477AbiAXPCO (ORCPT ); Mon, 24 Jan 2022 10:02:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643036534; x=1674572534; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/A3yuQETcYGHh0zJl4ypTfWdoT0Qd+Amo3JnNUQZ+5U=; b=i1/ziA95PgPylILearWcq3NZW4/v3U0noCpvLeVrOTYBfRS7tPID7Npq gVOtCB6GiZafl4SdUZGPXyA6T4Xo6r2Ng88RKJ5EKHN/LmPd1YfWaZAz9 JRoWoSGw/TusfvnNZ5yrmY4zP63jg7ShnTtHliJYNxKRfi9npDLcsdrcE R9wxmi2wv8+DGId2KDfxM7NCQi/hZvGV0Rksawwa8MNEU15JWhFf7tOAj 1erZOsd3uHLwBEnABkGDO49jS6WQm/qRB7vu06rLgl1Vdy7fIBeQBOBVv ZU90PyFNNDDz/cnT3Bc67OOqrsi1lvEy5yJzT2RB4WPmfKzWCQuIivfNa g==; X-IronPort-AV: E=McAfee;i="6200,9189,10236"; a="270498500" X-IronPort-AV: E=Sophos;i="5.88,311,1635231600"; d="scan'208";a="270498500" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jan 2022 07:02:12 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,311,1635231600"; d="scan'208";a="673649998" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 24 Jan 2022 07:02:06 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id 4E5A115C; Mon, 24 Jan 2022 17:02:19 +0200 (EET) From: "Kirill A. Shutemov" To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@intel.com, luto@kernel.org, peterz@infradead.org Cc: sathyanarayanan.kuppuswamy@linux.intel.com, aarcange@redhat.com, ak@linux.intel.com, dan.j.williams@intel.com, david@redhat.com, hpa@zytor.com, jgross@suse.com, jmattson@google.com, joro@8bytes.org, jpoimboe@redhat.com, knsathya@kernel.org, pbonzini@redhat.com, sdeep@vmware.com, seanjc@google.com, tony.luck@intel.com, vkuznets@redhat.com, wanpengli@tencent.com, x86@kernel.org, linux-kernel@vger.kernel.org, "Kirill A . Shutemov" Subject: [PATCHv2 01/29] x86/tdx: Detect running as a TDX guest in early boot Date: Mon, 24 Jan 2022 18:01:47 +0300 Message-Id: <20220124150215.36893-2-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124150215.36893-1-kirill.shutemov@linux.intel.com> References: <20220124150215.36893-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kuppuswamy Sathyanarayanan cc_platform_has() API is used in the kernel to enable confidential computing features. Since TDX guest is a confidential computing platform, it also needs to use this API. In preparation of extending cc_platform_has() API to support TDX guest, use CPUID instruction to detect support for TDX guests in the early boot code (via tdx_early_init()). Since copy_bootdata() is the first user of cc_platform_has() API, detect the TDX guest status before it. Since cc_plaform_has() API will be used frequently across the boot code, instead of repeatedly detecting the TDX guest status using the CPUID instruction, detect once and cache the result. Add a function (is_tdx_guest()) to read the cached TDX guest status in CC APIs. Define a synthetic feature flag (X86_FEATURE_TDX_GUEST) and set this bit in a valid TDX guest platform. This feature bit will be used to do TDX-specific handling in some areas of the ARCH code where a function call to check for TDX guest status is not cost-effective (for example, TDX hypercall support). Signed-off-by: Kuppuswamy Sathyanarayanan Reviewed-by: Andi Kleen Reviewed-by: Tony Luck Signed-off-by: Kirill A. Shutemov --- arch/x86/Kconfig | 12 +++++++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +++++- arch/x86/include/asm/tdx.h | 23 ++++++++++++++++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/head64.c | 4 +++ arch/x86/kernel/tdx.c | 31 ++++++++++++++++++++++++ 7 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/tdx.h create mode 100644 arch/x86/kernel/tdx.c diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 6fddb63271d9..09e6744af3f8 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -880,6 +880,18 @@ config ACRN_GUEST IOT with small footprint and real-time features. More details can be found in https://projectacrn.org/. +config INTEL_TDX_GUEST + bool "Intel TDX (Trust Domain Extensions) - Guest Support" + depends on X86_64 && CPU_SUP_INTEL + depends on X86_X2APIC + help + Support running as a guest under Intel TDX. Without this support, + the guest kernel can not boot or run under TDX. + TDX includes memory encryption and integrity capabilities + which protect the confidentiality and integrity of guest + memory contents and CPU state. TDX guests are protected from + potential attacks from the VMM. + endif #HYPERVISOR_GUEST source "arch/x86/Kconfig.cpu" diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 6db4e2932b3d..defed3bd543b 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -238,6 +238,7 @@ #define X86_FEATURE_VMW_VMMCALL ( 8*32+19) /* "" VMware prefers VMMCALL hypercall instruction */ #define X86_FEATURE_PVUNLOCK ( 8*32+20) /* "" PV unlock function */ #define X86_FEATURE_VCPUPREEMPT ( 8*32+21) /* "" PV vcpu_is_preempted function */ +#define X86_FEATURE_TDX_GUEST ( 8*32+22) /* Intel Trust Domain Extensions Guest */ /* Intel-defined CPU features, CPUID level 0x00000007:0 (EBX), word 9 */ #define X86_FEATURE_FSGSBASE ( 9*32+ 0) /* RDFSBASE, WRFSBASE, RDGSBASE, WRGSBASE instructions*/ diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index 8f28fafa98b3..f556086e6093 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -65,6 +65,12 @@ # define DISABLE_SGX (1 << (X86_FEATURE_SGX & 31)) #endif +#ifdef CONFIG_INTEL_TDX_GUEST +# define DISABLE_TDX_GUEST 0 +#else +# define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31)) +#endif + /* * Make sure to add features to the correct mask */ @@ -76,7 +82,7 @@ #define DISABLED_MASK5 0 #define DISABLED_MASK6 0 #define DISABLED_MASK7 (DISABLE_PTI) -#define DISABLED_MASK8 0 +#define DISABLED_MASK8 (DISABLE_TDX_GUEST) #define DISABLED_MASK9 (DISABLE_SMAP|DISABLE_SGX) #define DISABLED_MASK10 0 #define DISABLED_MASK11 0 diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h new file mode 100644 index 000000000000..e375a950a033 --- /dev/null +++ b/arch/x86/include/asm/tdx.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2021-2022 Intel Corporation */ +#ifndef _ASM_X86_TDX_H +#define _ASM_X86_TDX_H + +#include + +#define TDX_CPUID_LEAF_ID 0x21 +#define TDX_IDENT "IntelTDX " + +#ifdef CONFIG_INTEL_TDX_GUEST + +void __init tdx_early_init(void); +bool is_tdx_guest(void); + +#else + +static inline void tdx_early_init(void) { }; +static inline bool is_tdx_guest(void) { return false; } + +#endif /* CONFIG_INTEL_TDX_GUEST */ + +#endif /* _ASM_X86_TDX_H */ diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 6aef9ee28a39..211d9fcdd729 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -130,6 +130,7 @@ obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o +obj-$(CONFIG_INTEL_TDX_GUEST) += tdx.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index de563db9cdcd..1cb6346ec3d1 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -40,6 +40,7 @@ #include #include #include +#include /* * Manage page tables very early on. @@ -516,6 +517,9 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data) copy_bootdata(__va(real_mode_data)); + /* Needed before cc_platform_has() can be used for TDX */ + tdx_early_init(); + /* * Load microcode early on BSP. */ diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c new file mode 100644 index 000000000000..1ef6979a6434 --- /dev/null +++ b/arch/x86/kernel/tdx.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2021-2022 Intel Corporation */ + +#undef pr_fmt +#define pr_fmt(fmt) "tdx: " fmt + +#include +#include + +static bool tdx_guest_detected __ro_after_init; + +bool is_tdx_guest(void) +{ + return tdx_guest_detected; +} + +void __init tdx_early_init(void) +{ + u32 eax, sig[3]; + + cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2], &sig[1]); + + if (memcmp(TDX_IDENT, sig, 12)) + return; + + tdx_guest_detected = true; + + setup_force_cpu_cap(X86_FEATURE_TDX_GUEST); + + pr_info("Guest detected\n"); +} -- 2.34.1