From patchwork Fri Jan 28 20:59:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 12729102 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07FADC4332F for ; Fri, 28 Jan 2022 20:59:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B354D6B0100; Fri, 28 Jan 2022 15:59:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B0C826B0106; Fri, 28 Jan 2022 15:59:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 95D816B0108; Fri, 28 Jan 2022 15:59:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay027.a.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id 85F796B0100 for ; Fri, 28 Jan 2022 15:59:16 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 508B720769 for ; Fri, 28 Jan 2022 20:59:16 +0000 (UTC) X-FDA: 79080911112.03.E6B3E18 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by imf27.hostedemail.com (Postfix) with ESMTP id D397A40002 for ; Fri, 28 Jan 2022 20:59:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643403554; x=1674939554; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=x5vneaFsQl42X/y/ifPS+cErbpw7siRo7HVvjOaNdMc=; b=OKyGLNGzdaOfLfHhOSqZJ3FJofEh5hmSH+drQyuyj+X3MTHpKmpY/isb mjio+x7pmVlMrsG/4+Vn0D6jPM0yYTGIRJcowj87yRD2zI/y+slAgmIq0 mfDkvJi4UeDc963VnpTdSsK7whOQE2UXBPrvz64zh63Y4Jvn11RI3lAEL qmjC7lYCdJe2IWRqQalfJuy8J2wux3ACnGUin9WEUcQ3S1tX65QbGVUyM gk4XMFyI+UVpajTm899iSEKpjxJ5qeuUi3+C3VLePA94CCQXQ/ZpgZe4t fmcTYfQnnvuMHouWj/SxUgO+k07hQ1eRZ18sfm6Qokg8vw81Y2t7NunTU w==; X-IronPort-AV: E=McAfee;i="6200,9189,10241"; a="310509214" X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="310509214" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jan 2022 12:59:10 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="675199537" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 28 Jan 2022 12:59:03 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id DF800488; Fri, 28 Jan 2022 22:59:09 +0200 (EET) From: "Kirill A. Shutemov" To: Borislav Petkov , Andy Lutomirski , Sean Christopherson , Andrew Morton , Joerg Roedel , Ard Biesheuvel Cc: Andi Kleen , Kuppuswamy Sathyanarayanan , David Rientjes , Vlastimil Babka , Tom Lendacky , Thomas Gleixner , Peter Zijlstra , Paolo Bonzini , Ingo Molnar , Varad Gautam , Dario Faggioli , Dave Hansen , Brijesh Singh , Mike Rapoport , David Hildenbrand , x86@kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv3 7/7] x86/tdx: Unaccepted memory support Date: Fri, 28 Jan 2022 23:59:06 +0300 Message-Id: <20220128205906.27503-8-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220128205906.27503-1-kirill.shutemov@linux.intel.com> References: <20220128205906.27503-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: D397A40002 X-Stat-Signature: tq3n38fh51csbdky5xsboupazbqn7xb6 Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=OKyGLNGz; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf27.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.100) smtp.mailfrom=kirill.shutemov@linux.intel.com X-Rspam-User: nil X-HE-Tag: 1643403554-439166 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: All preparation is complete. Hookup TDX-specific code to accept memory. There are two tdx_accept_memory() implementations: one in main kernel and one in the decompressor. The implementation in core kernel uses tdx_hcall_gpa_intent(). The helper is not available in the decompressor, self-contained implementation added there instead. Signed-off-by: Kirill A. Shutemov --- arch/x86/Kconfig | 1 + arch/x86/boot/compressed/tdx.c | 27 ++++++++++++++++++++ arch/x86/boot/compressed/unaccepted_memory.c | 10 +++++++- arch/x86/include/asm/shared/tdx.h | 21 +++++++++++++++ arch/x86/include/asm/tdx.h | 19 -------------- arch/x86/kernel/tdx.c | 6 +++++ arch/x86/mm/unaccepted_memory.c | 6 ++++- 7 files changed, 69 insertions(+), 21 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 9f4fdd408698..b4ba8cc3e9c0 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -886,6 +886,7 @@ config INTEL_TDX_GUEST select ARCH_HAS_CC_PLATFORM select X86_MCE select X86_MEM_ENCRYPT + select UNACCEPTED_MEMORY help Support running as a guest under Intel TDX. Without this support, the guest kernel can not boot or run under TDX. diff --git a/arch/x86/boot/compressed/tdx.c b/arch/x86/boot/compressed/tdx.c index f2e1449c74cd..eac80d172e36 100644 --- a/arch/x86/boot/compressed/tdx.c +++ b/arch/x86/boot/compressed/tdx.c @@ -6,11 +6,13 @@ #include "../cpuflags.h" #include "../string.h" #include "../io.h" +#include "error.h" #include #include #include +#include static bool tdx_guest_detected; @@ -86,3 +88,28 @@ void early_tdx_detect(void) pio_ops.outw = tdx_outw; pio_ops.outl = tdx_outl; } + +#define TDACCEPTPAGE 6 +#define TDVMCALL_MAP_GPA 0x10001 + +void tdx_accept_memory(phys_addr_t start, phys_addr_t end) +{ + struct tdx_hypercall_output outl = {0}; + int i; + + if (__tdx_hypercall(TDX_HYPERCALL_STANDARD, TDVMCALL_MAP_GPA, + start, end, 0, 0, &outl)) { + error("Cannot accept memory: MapGPA failed\n"); + } + + /* + * For shared->private conversion, accept the page using TDACCEPTPAGE + * TDX module call. + */ + for (i = 0; i < (end - start) / PAGE_SIZE; i++) { + if (__tdx_module_call(TDACCEPTPAGE, start + i * PAGE_SIZE, + 0, 0, 0, NULL)) { + error("Cannot accept memory: page accept failed\n"); + } + } +} diff --git a/arch/x86/boot/compressed/unaccepted_memory.c b/arch/x86/boot/compressed/unaccepted_memory.c index d0de7e88dade..a2df2cae5c1b 100644 --- a/arch/x86/boot/compressed/unaccepted_memory.c +++ b/arch/x86/boot/compressed/unaccepted_memory.c @@ -1,12 +1,17 @@ // SPDX-License-Identifier: GPL-2.0-only +#include #include "error.h" #include "misc.h" +#include "tdx.h" static inline void __accept_memory(phys_addr_t start, phys_addr_t end) { /* Platform-specific memory-acceptance call goes here */ - error("Cannot accept memory"); + if (early_is_tdx_guest()) + tdx_accept_memory(start, end); + else + error("Cannot accept memory"); } void mark_unaccepted(struct boot_params *params, u64 start, u64 end) @@ -18,6 +23,9 @@ void mark_unaccepted(struct boot_params *params, u64 start, u64 end) * *marked* as unaccepted. */ + /* __accept_memory() needs to know if kernel runs in TDX environment */ + early_tdx_detect(); + /* * Accept small regions that might not be able to be represented * in the bitmap: diff --git a/arch/x86/include/asm/shared/tdx.h b/arch/x86/include/asm/shared/tdx.h index 4a0218bedc75..b17e7d68e0d3 100644 --- a/arch/x86/include/asm/shared/tdx.h +++ b/arch/x86/include/asm/shared/tdx.h @@ -3,6 +3,21 @@ #include +/* + * Used in __tdx_module_call() to gather the output registers' + * values of the TDCALL instruction when requesting services from + * the TDX module. This is a software only structure and not part + * of the TDX module/VMM ABI + */ +struct tdx_module_output { + u64 rcx; + u64 rdx; + u64 r8; + u64 r9; + u64 r10; + u64 r11; +}; + /* * Used in __tdx_hypercall() to gather the output registers' values * of the TDCALL instruction when requesting services from the VMM. @@ -23,8 +38,14 @@ struct tdx_hypercall_output { #define TDX_CPUID_LEAF_ID 0x21 #define TDX_IDENT "IntelTDX " +/* Used to communicate with the TDX module */ +u64 __tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, + struct tdx_module_output *out); + /* Used to request services from the VMM */ u64 __tdx_hypercall(u64 type, u64 fn, u64 r12, u64 r13, u64 r14, u64 r15, struct tdx_hypercall_output *out); +extern void tdx_accept_memory(phys_addr_t start, phys_addr_t end); + #endif diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index f6a5fb4bf72c..cf0f7f008e6c 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -7,21 +7,6 @@ #include #include -/* - * Used in __tdx_module_call() to gather the output registers' - * values of the TDCALL instruction when requesting services from - * the TDX module. This is a software only structure and not part - * of the TDX module/VMM ABI - */ -struct tdx_module_output { - u64 rcx; - u64 rdx; - u64 r8; - u64 r9; - u64 r10; - u64 r11; -}; - /* * Used by the #VE exception handler to gather the #VE exception * info from the TDX module. This is a software only structure @@ -43,10 +28,6 @@ struct ve_info { void __init tdx_early_init(void); bool is_tdx_guest(void); -/* Used to communicate with the TDX module */ -u64 __tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, - struct tdx_module_output *out); - bool tdx_get_ve_info(struct ve_info *ve); bool tdx_handle_virt_exception(struct pt_regs *regs, struct ve_info *ve); diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c index 35ef57d778bb..a9bce0a54e1c 100644 --- a/arch/x86/kernel/tdx.c +++ b/arch/x86/kernel/tdx.c @@ -176,6 +176,12 @@ int tdx_hcall_request_gpa_type(phys_addr_t start, phys_addr_t end, bool enc) return 0; } +void tdx_accept_memory(phys_addr_t start, phys_addr_t end) +{ + if (tdx_hcall_request_gpa_type(start, end, true)) + panic("Accepting memory failed\n"); +} + static u64 __cpuidle _tdx_halt(const bool irq_disabled, const bool do_sti) { /* diff --git a/arch/x86/mm/unaccepted_memory.c b/arch/x86/mm/unaccepted_memory.c index adcac22dfe75..2c4ef49a0c9b 100644 --- a/arch/x86/mm/unaccepted_memory.c +++ b/arch/x86/mm/unaccepted_memory.c @@ -5,6 +5,7 @@ #include #include +#include #include static DEFINE_SPINLOCK(unaccepted_memory_lock); @@ -22,7 +23,10 @@ static void __accept_memory(phys_addr_t start, phys_addr_t end) for_each_set_bitrange_from(rs, re, unaccepted_memory, DIV_ROUND_UP(end, PMD_SIZE)) { /* Platform-specific memory-acceptance call goes here */ - panic("Cannot accept memory"); + if (cc_platform_has(CC_ATTR_GUEST_TDX)) + tdx_accept_memory(rs * PMD_SIZE, re * PMD_SIZE); + else + panic("Cannot accept memory"); bitmap_clear(unaccepted_memory, rs, re - rs); } }