Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3527227pxb; Mon, 24 Jan 2022 11:26:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJzNpwwG2AYpG2RlfUi747k5dQYXgInRbX/fsrdMxRwoIZ69LZHDmrCxXM/sYfvTrcJoQzIH X-Received: by 2002:a65:4cc2:: with SMTP id n2mr12792934pgt.535.1643052398225; Mon, 24 Jan 2022 11:26:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643052398; cv=none; d=google.com; s=arc-20160816; b=lG74JY6j0WdOk5/sgW99CZNZm5Tp0kLVvUj8UnWL+zinC5jFR+o4+BQHR+m6Ov0Es3 Jm5GSyBxZqIs1cD/Y+lKKRSZpx5pbCsWIP9ATBW2P7C6zO/fCYykTo5domKD4LX47Ntq LwKemKJMR9LkhRjp9MS5Im9ba+9dpa3SzEpGVLWyhQHwL8dda32f3qgp8Y6U9UjtkBx3 UD1CBiB28dePDtXCCm1UUcGML/PbpGmc6Z+AabslmUQ3A/pgEAXxU8IkSZf1PdQDml8y ky/WAAtGh/86AlXN+69FkQc4eV/0hOqjJ6XicFaDzaEU8qfny8fuVnYAQy6KZlrgRzB6 W0+A== 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=5PGSS6OY2RGGU/jzYgWglEsxKpHo4KqXo4InlEjLgTw=; b=o5+52q8Qd3zGwb24OvDvO5bJu5qq60Cu58sGnZNRrOTJgMiO8nxcLzAdMg11vAJa/i Yc9tGqJdONQ5ViJbGzjFZ6Qu2mU7+uITa6wrkixfK/MzZr+UHUn4A8sZ86YrhewSJCHb AdxnoQg+qH0Yi24eAlNzEh9504IS/wNtcl7utm3IwaJCfYR4Qbzs9sSHwuguXPpaIQ7P URp68t1bfYPJvkIXPXIabl0X173s7UWBBtepKJm0IqxjZxl90Jv5B2s1Un+TA8ZOc+3O i9VnUwKBz0x81tEURf2ZbLp9MrcW5dp0GrmYX/sOPD+jG9GLV6s+DXHF44SJgZU94NQV 7Lhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cNStg1N0; 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 k8si16231327pfu.23.2022.01.24.11.26.22; Mon, 24 Jan 2022 11:26:38 -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=cNStg1N0; 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 S240192AbiAXPDO (ORCPT + 99 others); Mon, 24 Jan 2022 10:03:14 -0500 Received: from mga14.intel.com ([192.55.52.115]:43409 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239188AbiAXPC2 (ORCPT ); Mon, 24 Jan 2022 10:02:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643036548; x=1674572548; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qcUqbzAY6M/ii9A7rG49fqPPHN0dWo5mN7mZmaHffu4=; b=cNStg1N01fqC1hP0HtlVF87eTL36pkMt8awZxzX1it4zCJvbLwAXUKj8 8WOAodIIGdlnyeYYHV5hFaLnouaqgZ8ez/TEI9sQJjzZV2qwcY9On0omO riPw8+i747WFm/aQXMQOIMGeWaR3VY9gR/Xo/1QfHBRAJs8gQcD3UCgjB CGxC70CNLZ4hnoiRkXWWB903xoPBBvYqQa//23eUuSih5qjm6WSrk/+JN n6jsGaSBP3iah3/logAVH0+KqqJ0zXivGdE1tE+svXt1gArLux14bBA/L NtnrSC+CqjL+mWA4Huack4CJN+zg4D3K06NJjZYgwHm+h3XSi8PWUmr6+ Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10236"; a="246280269" X-IronPort-AV: E=Sophos;i="5.88,311,1635231600"; d="scan'208";a="246280269" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jan 2022 07:02:19 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,311,1635231600"; d="scan'208";a="532102238" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga007.fm.intel.com with ESMTP; 24 Jan 2022 07:02:13 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id BC3DC56A; 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 09/29] x86/tdx: Detect TDX at early kernel decompression time Date: Mon, 24 Jan 2022 18:01:55 +0300 Message-Id: <20220124150215.36893-10-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 The early decompression code does port I/O for its console output. But, handling the decompression-time port I/O demands a different approach from normal runtime because the IDT required to support #VE based port I/O emulation is not yet set up. Paravirtualizing I/O calls during the decompression step is acceptable because the decompression code size is small enough and hence patching it will not bloat the image size a lot. To support port I/O in decompression code, TDX must be detected before the decompression code might do port I/O. Add support to detect for TDX guest support before console_init() in the extract_kernel(). Detecting it above the console_init() is early enough for patching port I/O. Add an early_is_tdx_guest() interface to get the cached TDX guest status in the decompression code. The actual port I/O paravirtualization will come later in the series. Reviewed-by: Tony Luck Signed-off-by: Kuppuswamy Sathyanarayanan Signed-off-by: Kirill A. Shutemov --- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/misc.c | 8 ++++++++ arch/x86/boot/compressed/misc.h | 2 ++ arch/x86/boot/compressed/tdx.c | 29 +++++++++++++++++++++++++++++ arch/x86/boot/compressed/tdx.h | 16 ++++++++++++++++ arch/x86/boot/cpuflags.c | 3 +-- arch/x86/boot/cpuflags.h | 1 + arch/x86/include/asm/shared/tdx.h | 7 +++++++ arch/x86/include/asm/tdx.h | 4 +--- 9 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 arch/x86/boot/compressed/tdx.c create mode 100644 arch/x86/boot/compressed/tdx.h create mode 100644 arch/x86/include/asm/shared/tdx.h diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index 6115274fe10f..732f6b21ecbd 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -101,6 +101,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/misc.c b/arch/x86/boot/compressed/misc.c index a4339cb2d247..d8373d766672 100644 --- a/arch/x86/boot/compressed/misc.c +++ b/arch/x86/boot/compressed/misc.c @@ -370,6 +370,14 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, lines = boot_params->screen_info.orig_video_lines; cols = boot_params->screen_info.orig_video_cols; + /* + * Detect TDX guest environment. + * + * It has to be done before console_init() in order to use + * paravirtualized port I/O oprations if needed. + */ + early_tdx_detect(); + console_init(); /* diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 16ed360b6692..0d8e275a9d96 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -28,6 +28,8 @@ #include #include +#include "tdx.h" + #define BOOT_CTYPE_H #include diff --git a/arch/x86/boot/compressed/tdx.c b/arch/x86/boot/compressed/tdx.c new file mode 100644 index 000000000000..6853376fe69a --- /dev/null +++ b/arch/x86/boot/compressed/tdx.c @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * tdx.c - Early boot code for TDX + */ + +#include "../cpuflags.h" +#include "../string.h" + +#include + +static bool tdx_guest_detected; + +bool early_is_tdx_guest(void) +{ + return tdx_guest_detected; +} + +void early_tdx_detect(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; + + /* Cache TDX guest feature status */ + tdx_guest_detected = true; +} diff --git a/arch/x86/boot/compressed/tdx.h b/arch/x86/boot/compressed/tdx.h new file mode 100644 index 000000000000..18970c09512e --- /dev/null +++ b/arch/x86/boot/compressed/tdx.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2021 Intel Corporation */ +#ifndef BOOT_COMPRESSED_TDX_H +#define BOOT_COMPRESSED_TDX_H + +#include + +#ifdef CONFIG_INTEL_TDX_GUEST +void early_tdx_detect(void); +bool early_is_tdx_guest(void); +#else +static inline void early_tdx_detect(void) { }; +static inline bool early_is_tdx_guest(void) { return false; } +#endif + +#endif diff --git a/arch/x86/boot/cpuflags.c b/arch/x86/boot/cpuflags.c index a0b75f73dc63..a83d67ec627d 100644 --- a/arch/x86/boot/cpuflags.c +++ b/arch/x86/boot/cpuflags.c @@ -71,8 +71,7 @@ int has_eflag(unsigned long mask) # define EBX_REG "=b" #endif -static inline void cpuid_count(u32 id, u32 count, - u32 *a, u32 *b, u32 *c, u32 *d) +void cpuid_count(u32 id, u32 count, u32 *a, u32 *b, u32 *c, u32 *d) { asm volatile(".ifnc %%ebx,%3 ; movl %%ebx,%3 ; .endif \n\t" "cpuid \n\t" diff --git a/arch/x86/boot/cpuflags.h b/arch/x86/boot/cpuflags.h index 2e20814d3ce3..475b8fde90f7 100644 --- a/arch/x86/boot/cpuflags.h +++ b/arch/x86/boot/cpuflags.h @@ -17,5 +17,6 @@ extern u32 cpu_vendor[3]; int has_eflag(unsigned long mask); void get_cpuflags(void); +void cpuid_count(u32 id, u32 count, u32 *a, u32 *b, u32 *c, u32 *d); #endif diff --git a/arch/x86/include/asm/shared/tdx.h b/arch/x86/include/asm/shared/tdx.h new file mode 100644 index 000000000000..12bede46d048 --- /dev/null +++ b/arch/x86/include/asm/shared/tdx.h @@ -0,0 +1,7 @@ +#ifndef _ASM_X86_SHARED_TDX_H +#define _ASM_X86_SHARED_TDX_H + +#define TDX_CPUID_LEAF_ID 0x21 +#define TDX_IDENT "IntelTDX " + +#endif diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index 9b4714a45bb9..53f7dd0fbe58 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -5,9 +5,7 @@ #include #include - -#define TDX_CPUID_LEAF_ID 0x21 -#define TDX_IDENT "IntelTDX " +#include #define TDX_HYPERCALL_STANDARD 0 -- 2.34.1