Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp4184355pxb; Mon, 4 Oct 2021 19:54:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBVrqYwx8hc/pYa/UuiMJxKHLxB6k0/2v0Wp6c6EraCpMtYbjstj6CQ2EWYXX1/WFAuRSq X-Received: by 2002:a17:906:3fc1:: with SMTP id k1mr22122879ejj.44.1633402457540; Mon, 04 Oct 2021 19:54:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633402457; cv=none; d=google.com; s=arc-20160816; b=zyB6ONAXvNjfjCVlsEcLC0CvEIGojAsdBmwrYT5xiQ4hPvgGs+9rbff1itE6sdiy7P c5hyBH+FLA1aG21YN80xv4o2nVYw4twHNH9RbcqW87VfYeQyR84TNNeMIVjebYO4t/Q1 rjvUrb4IOoDj12b/SaOM2jMcGBWCqqzx01OAvUkQVgM7VekDmMzU+SdFDMeogZN/skL4 V8vhmLFFes07CjBEMkyqWjthE1wORoTVeQHFy1aXcdGlDG11TqZkvf8klwKuyjJrquFv voBIeboeM3JHNJd3esQ+HbyfhWkohxGP0iW73n+cOXWcb8dzd7YlQfFNvD3qZZq0nMoN 7hSg== 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=9Du5CqFhVk5fD18m2MCB5PDuOhGxl/NKggaDJicEtO8=; b=yQT+KPkK8udHHC5l7PIbujB2WDOtSrt9YAZDWdpE7NzxIgGQt63ldPncffcnLB1OMC r5wsrcjX51IwFnsN/qKha2vMxhYRPILPovck7330LWJEBYcooWamFcKTv+SrGNLD7PuK xiHlh0NR8H6kpjlMon/hssmpwZ2kg1mrnlU2F2+Yd6iKLqQp6lyAY79b4zlkO6xp/lDv hMpCrvudHEnZUe0k8G4gxBOLgsUdOmYf1ndc+zP+RD9lPufeSJMSC1ikKPu+ADfuIgOb CEKwIz9LDN6q4d5/vjox+pAYV9Zka3pQuxLQ2xQghnfZCVnitzVCLmbGsb1kugZGfm+4 udIw== 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 c11si20773696ejm.510.2021.10.04.19.53.54; Mon, 04 Oct 2021 19:54:17 -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 S231508AbhJECyU (ORCPT + 99 others); Mon, 4 Oct 2021 22:54:20 -0400 Received: from mga01.intel.com ([192.55.52.88]:43302 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231273AbhJECyQ (ORCPT ); Mon, 4 Oct 2021 22:54:16 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10127"; a="248894628" X-IronPort-AV: E=Sophos;i="5.85,347,1624345200"; d="scan'208";a="248894628" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Oct 2021 19:52:26 -0700 X-IronPort-AV: E=Sophos;i="5.85,347,1624345200"; d="scan'208";a="483409096" Received: from asaini1-mobl1.amr.corp.intel.com (HELO skuppusw-desk1.amr.corp.intel.com) ([10.251.138.96]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Oct 2021 19:52:25 -0700 From: Kuppuswamy Sathyanarayanan To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, Paolo Bonzini , David Hildenbrand , Andrea Arcangeli , Josh Poimboeuf , Juergen Gross , Deep Shah , VMware Inc , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Cc: Peter H Anvin , Dave Hansen , Tony Luck , Dan Williams , Andi Kleen , Kirill Shutemov , Sean Christopherson , Kuppuswamy Sathyanarayanan , linux-kernel@vger.kernel.org Subject: [PATCH v8 03/11] x86/cpufeatures: Add TDX Guest CPU feature Date: Mon, 4 Oct 2021 19:51:57 -0700 Message-Id: <20211005025205.1784480-4-sathyanarayanan.kuppuswamy@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211005025205.1784480-1-sathyanarayanan.kuppuswamy@linux.intel.com> References: <20211005025205.1784480-1-sathyanarayanan.kuppuswamy@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add CPU feature detection for Trusted Domain Extensions support. TDX feature adds capabilities to keep guest register state and memory isolated from hypervisor. For TDX guest platforms, executing CPUID(eax=0x21, ecx=0) will return following values in EAX, EBX, ECX and EDX. EAX: Maximum sub-leaf number: 0 EBX/EDX/ECX: Vendor string: EBX = "Inte" EDX = "lTDX" ECX = " " So when above condition is true, set X86_FEATURE_TDX_GUEST feature cap bit. Signed-off-by: Kuppuswamy Sathyanarayanan --- Changes since v7: * Add comments for the order of tdx_early_init() call. * Similar to AMD (sme_me_mask) added a global variable is_tdx_guest for Intel TDX related checks. * Changed pr_fmt from "x86/tdx: " to "tdx:" arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/tdx.h | 22 ++++++++++++++++ arch/x86/kernel/Makefile | 2 ++ arch/x86/kernel/head64.c | 8 ++++++ arch/x86/kernel/tdx.c | 40 ++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 arch/x86/include/asm/tdx.h create mode 100644 arch/x86/kernel/tdx.c diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index d0ce5cfd3ac1..84997abeb401 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) /* Trusted 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/tdx.h b/arch/x86/include/asm/tdx.h new file mode 100644 index 000000000000..d552c7248bc7 --- /dev/null +++ b/arch/x86/include/asm/tdx.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2020 Intel Corporation */ +#ifndef _ASM_X86_TDX_H +#define _ASM_X86_TDX_H + +#include + +#define TDX_CPUID_LEAF_ID 0x21 + +#ifdef CONFIG_INTEL_TDX_GUEST + +extern u64 is_tdx_guest; +void __init tdx_early_init(void); + +#else + +#define is_tdx_guest 0ULL +static inline void tdx_early_init(void) { }; + +#endif /* CONFIG_INTEL_TDX_GUEST */ + +#endif /* _ASM_X86_TDX_H */ diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index f91403a78594..159fccfece65 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -29,6 +29,7 @@ KASAN_SANITIZE_dumpstack_$(BITS).o := n KASAN_SANITIZE_stacktrace.o := n KASAN_SANITIZE_paravirt.o := n KASAN_SANITIZE_sev.o := n +KASAN_SANITIZE_tdx.o := n # With some compiler versions the generated code results in boot hangs, caused # by several compilation units. To be safe, disable all instrumentation. @@ -127,6 +128,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 f98c76a1d16c..97ce0d037387 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. @@ -495,6 +496,13 @@ asmlinkage __visible void __init x86_64_start_kernel(char * real_mode_data) copy_bootdata(__va(real_mode_data)); + /* + * tdx_early_init() has dependency on command line parameters. + * So the order of calling it should be after copy_bootdata() + * (in which command line parameter is initialized). + */ + 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..ad3ff5925153 --- /dev/null +++ b/arch/x86/kernel/tdx.c @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2020 Intel Corporation */ + +#undef pr_fmt +#define pr_fmt(fmt) "tdx: " fmt + +#include + +/* + * Allocate it in the data region to avoid zeroing it during + * BSS initialization. It is mainly used in cc_platform_has() + * call during early boot call. + */ +u64 __section(".data") is_tdx_guest = 0; + +static void __init is_tdx_guest_init(void) +{ + u32 eax, sig[3]; + + if (cpuid_eax(0) < TDX_CPUID_LEAF_ID) { + is_tdx_guest = 0; + return; + } + + cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2], &sig[1]); + + is_tdx_guest = !memcmp("IntelTDX ", sig, 12); +} + +void __init tdx_early_init(void) +{ + is_tdx_guest_init(); + + if (!is_tdx_guest) + return; + + setup_force_cpu_cap(X86_FEATURE_TDX_GUEST); + + pr_info("Guest initialized\n"); +} -- 2.25.1