From patchwork Mon Jun 26 14:12:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Kai" X-Patchwork-Id: 13292974 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 5A7B6EB64D7 for ; Mon, 26 Jun 2023 14:14:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB31D8D0008; Mon, 26 Jun 2023 10:14:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E63078D0001; Mon, 26 Jun 2023 10:14:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D2B7C8D0008; Mon, 26 Jun 2023 10:14:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C5B568D0001 for ; Mon, 26 Jun 2023 10:14:09 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1AA4440757 for ; Mon, 26 Jun 2023 14:14:09 +0000 (UTC) X-FDA: 80945093418.22.498F9DE Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by imf09.hostedemail.com (Postfix) with ESMTP id BD3B014000C for ; Mon, 26 Jun 2023 14:14:06 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SYpXkxy3; spf=pass (imf09.hostedemail.com: domain of kai.huang@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=kai.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687788847; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6PCGgVO7q7I0moesqS1gO84H3m2tPfOGSckDbfdCqfY=; b=N7lDuhn8n6PiEp+2xBWK1JeUyl6c34PvxLa+kiQaJdNqK9ir8HF3cwoyd3CMIHcO/1LSMJ +WUxMax/RwECW4BB3Y3vTjJ7A1bf1XwcpypD1FHDhM1mlIfLZrAz5NCRNgt2uT6q/MbL5k R/B0G7pLXZ/oLZkZe29CTFf6y4P670I= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687788847; a=rsa-sha256; cv=none; b=jFLD6VLnt2UdkU03om9RMQRYCeen4PRpY88jetxBkgUBocIR/GBF9GRbD69+2Fj9sGvS0C Z0ApWLY8SB+KCWn5TORiWopKfCNqToGxO8tE9BeOZl0Ospw/3Sy9Im2GYPLO0/bcR76cmQ ewqIiev/6UTuzTToceRbMHAAjsJqKxY= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=SYpXkxy3; spf=pass (imf09.hostedemail.com: domain of kai.huang@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=kai.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1687788846; x=1719324846; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=t6DeTcVVz69NU4jP/RLKnV8J3tDyrmh8ZA/L9s72880=; b=SYpXkxy3zRO0hX8Q9yGFg3NW9UuR8ZXeBWf6jx0DxK+zOcUT61y6V2CN jw6KVAvd+8RNSDyQ1UC7eADu3HzTbCnEpFRbkwbPJPMj32twPcY6drKDi fmW7R+lyqARqD3rg57ikVV6NgrEAFqu5xXuVNmczErDRg5Z/oljt/DAmF oXnWUBmN13gA5t+zgOnpsXBcZ6nZvfEcWoJJdDfeFFWL5KasFpcLG4oi2 JAAlerYflSiBwqLG0u2G2ozRqHDbi037O3ttz0cQxigW86td74/5MRABI 4V6VaQI2NtKKgMMK+EgDCQ/gpoLeIe8+txIcEn+h80fErIGsYJb+CWjVr w==; X-IronPort-AV: E=McAfee;i="6600,9927,10753"; a="346033637" X-IronPort-AV: E=Sophos;i="6.01,159,1684825200"; d="scan'208";a="346033637" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jun 2023 07:14:06 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10753"; a="890292288" X-IronPort-AV: E=Sophos;i="6.01,159,1684825200"; d="scan'208";a="890292288" Received: from smithau-mobl1.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.213.179.223]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jun 2023 07:13:59 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: linux-mm@kvack.org, x86@kernel.org, dave.hansen@intel.com, kirill.shutemov@linux.intel.com, tony.luck@intel.com, peterz@infradead.org, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, david@redhat.com, dan.j.williams@intel.com, rafael.j.wysocki@intel.com, ashok.raj@intel.com, reinette.chatre@intel.com, len.brown@intel.com, ak@linux.intel.com, isaku.yamahata@intel.com, ying.huang@intel.com, chao.gao@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, nik.borisov@suse.com, bagasdotme@gmail.com, sagis@google.com, imammedo@redhat.com, kai.huang@intel.com Subject: [PATCH v12 05/22] x86/virt/tdx: Add SEAMCALL infrastructure Date: Tue, 27 Jun 2023 02:12:35 +1200 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: BD3B014000C X-Rspam-User: X-Stat-Signature: g5rejogg4rwds64e4c3a8jkzj6i7mcek X-Rspamd-Server: rspam03 X-HE-Tag: 1687788846-507267 X-HE-Meta: U2FsdGVkX19MjI2FYEj8tC8JQ57r9cIDb2kuVYxepv9L0ak1dMYppVB74Qx0AOOK9yZHTVEIp5F9Kz2WzlSK836leWb+vHBvdn+uA20nvjzm9ZKb3O09b1LZmB4B/Nv2z/e0410L+t8d45TL10f0vG9ORtbpnbH6DUghQhOOoxnQNvMNotTL7MCk13I2H3EUXPOgIEoMcteY2ZdlTO9+WSjOP6s6TNL0Q6Si3VQyqsMSySJsVx2447kf1bUQ+ygXBXRIN5E5gSEOg2QfVOd84zIA5PuXSvsbVeWh8tJBaD7IKjQ/O/4LuclgBvNYYAPqvpzwREGOw76BBNCbPKmxfzP/yr+bVsPuTDKsEvHPQ15om9aAMNbLvpED9LnTASFLMKTtWY3dyiuzqMxayXGqFh4MipczZWK2OZC5LFmlyEMcGhF3FB7ZqF3iO4g+4qI75stL2GXuMRNqmTTU3JpFaGtDDLhUMM78OqwwfDOmmBvADCRGD57pcxI6hOWzjbl0J6TRlZRdizt7ts1tKrBBLqWR0KLfVIjnL/AY5SnIHqwIvWnKs3FXOCqzgaxPVWreA4PE0voVvEvO2nIC3sDN/SRAwApU1vDE9mAywARsjd/rLY+TkxGdW07NX99+lyf2bPVKHou0x472E+WeilPmuEUVH5X88vOc1/WDz2w9iH9DvcqthVNOvVBub5dnnabPSUKWbaxjeorw3QnJ1t0CFaFaorsfTI8G79VzJhDM3lvHYLxjv9C/cuX9lxT4h3wrVmG1rHDhrheG0x95xlgU3mlJNckao1TTH9J5rvFnXngnAkv4GLPdaP+O6zHp4K5k9k8v6hyZwo1MiPRrKijk3M2EsPFf3HOM1nUP+2b9l0etk0042A4q1AmCPAkkZVJnHOUfDh9AtheR6u8ZGmutZUPRmZChpQomRneDV2qAFtbfEex/X4PYNTE8nFyLuegl4GNKzTsb8/y7dFT7PZs q4nXswWR o7rKa7Fnou1ERKWpE41lZfMDNRAHQ+7IRIKxHHmxprwgiVMRHt4NaxwufvddXUZgEn4MoVC4fuPtEVUwVcqmn9YropYOGeo3fOYfB8wZgBRfwXkfVOc9V/HsyIFWEklJXH0utkWoT3NFaibvAg64x0rECiRD/k41kRdVRfqp/eT+bkvHwL/ldFyh2FvAv20CFX9k4C0h8slNHtj4= 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: TDX introduces a new CPU mode: Secure Arbitration Mode (SEAM). This mode runs only the TDX module itself or other code to load the TDX module. The host kernel communicates with SEAM software via a new SEAMCALL instruction. This is conceptually similar to a guest->host hypercall, except it is made from the host to SEAM software instead. The TDX module establishes a new SEAMCALL ABI which allows the host to initialize the module and to manage VMs. Add infrastructure to make SEAMCALLs. The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure. Also add a wrapper function of SEAMCALL to convert SEAMCALL error code to the kernel error code, and print out SEAMCALL error code to help the user to understand what went wrong. Signed-off-by: Kai Huang Reviewed-by: Isaku Yamahata --- v11 -> v12: - Moved _ASM_EXT_TABLE() for #UD/#GP to a later patch for better patch review, and removed related part from changelog. - Minor code changes in seamcall() (David) - Added Isaku's tag v10 -> v11: - No update v9 -> v10: - Make the TDX_SEAMCALL_{GP|UD} error codes unconditional but doesn't define them when INTEL_TDX_HOST is enabled. (Dave) - Slightly improved changelog to explain why add assembly code to handle #UD and #GP. v8 -> v9: - Changed patch title (Dave). - Enhanced seamcall() to include the cpu id to the error message when SEAMCALL fails. v7 -> v8: - Improved changelog (Dave): - Trim down some sentences (Dave). - Removed __seamcall() and seamcall() function name and changed accordingly (Dave). - Improved the sentence explaining why to handle #GP (Dave). - Added code to print out error message in seamcall(), following the idea that tdx_enable() to return universal error and print out error message to make clear what's going wrong (Dave). Also mention this in changelog. v6 -> v7: - No change. v5 -> v6: - Added code to handle #UD and #GP (Dave). - Moved the seamcall() wrapper function to this patch, and used a temporary __always_unused to avoid compile warning (Dave). - v3 -> v5 (no feedback on v4): - Explicitly tell TDX_SEAMCALL_VMFAILINVALID is returned if the SEAMCALL itself fails. - Improve the changelog. --- arch/x86/virt/vmx/tdx/Makefile | 2 +- arch/x86/virt/vmx/tdx/seamcall.S | 52 ++++++++++++++++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 42 ++++++++++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.h | 10 ++++++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 arch/x86/virt/vmx/tdx/seamcall.S create mode 100644 arch/x86/virt/vmx/tdx/tdx.h diff --git a/arch/x86/virt/vmx/tdx/Makefile b/arch/x86/virt/vmx/tdx/Makefile index 93ca8b73e1f1..38d534f2c113 100644 --- a/arch/x86/virt/vmx/tdx/Makefile +++ b/arch/x86/virt/vmx/tdx/Makefile @@ -1,2 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y += tdx.o +obj-y += tdx.o seamcall.o diff --git a/arch/x86/virt/vmx/tdx/seamcall.S b/arch/x86/virt/vmx/tdx/seamcall.S new file mode 100644 index 000000000000..f81be6b9c133 --- /dev/null +++ b/arch/x86/virt/vmx/tdx/seamcall.S @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#include +#include + +#include "tdxcall.S" + +/* + * __seamcall() - Host-side interface functions to SEAM software module + * (the P-SEAMLDR or the TDX module). + * + * Transform function call register arguments into the SEAMCALL register + * ABI. Return TDX_SEAMCALL_VMFAILINVALID if the SEAMCALL itself fails, + * or the completion status of the SEAMCALL leaf function. Additional + * output operands are saved in @out (if it is provided by the caller). + * + *------------------------------------------------------------------------- + * SEAMCALL ABI: + *------------------------------------------------------------------------- + * Input Registers: + * + * RAX - SEAMCALL Leaf number. + * RCX,RDX,R8-R9 - SEAMCALL Leaf specific input registers. + * + * Output Registers: + * + * RAX - SEAMCALL completion status code. + * RCX,RDX,R8-R11 - SEAMCALL Leaf specific output registers. + * + *------------------------------------------------------------------------- + * + * __seamcall() function ABI: + * + * @fn (RDI) - SEAMCALL Leaf number, moved to RAX + * @rcx (RSI) - Input parameter 1, moved to RCX + * @rdx (RDX) - Input parameter 2, moved to RDX + * @r8 (RCX) - Input parameter 3, moved to R8 + * @r9 (R8) - Input parameter 4, moved to R9 + * + * @out (R9) - struct tdx_module_output pointer + * stored temporarily in R12 (not + * used by the P-SEAMLDR or the TDX + * module). It can be NULL. + * + * Return (via RAX) the completion status of the SEAMCALL, or + * TDX_SEAMCALL_VMFAILINVALID. + */ +SYM_FUNC_START(__seamcall) + FRAME_BEGIN + TDX_MODULE_CALL host=1 + FRAME_END + RET +SYM_FUNC_END(__seamcall) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 908590e85749..f8233cba5931 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -12,14 +12,56 @@ #include #include #include +#include #include #include #include +#include "tdx.h" static u32 tdx_global_keyid __ro_after_init; static u32 tdx_guest_keyid_start __ro_after_init; static u32 tdx_nr_guest_keyids __ro_after_init; +/* + * Wrapper of __seamcall() to convert SEAMCALL leaf function error code + * to kernel error code. @seamcall_ret and @out contain the SEAMCALL + * leaf function return code and the additional output respectively if + * not NULL. + */ +static int __always_unused seamcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, + u64 *seamcall_ret, + struct tdx_module_output *out) +{ + u64 sret; + int cpu; + + /* Need a stable CPU id for printing error message */ + cpu = get_cpu(); + sret = __seamcall(fn, rcx, rdx, r8, r9, out); + put_cpu(); + + /* Save SEAMCALL return code if the caller wants it */ + if (seamcall_ret) + *seamcall_ret = sret; + + switch (sret) { + case 0: + /* SEAMCALL was successful */ + return 0; + case TDX_SEAMCALL_VMFAILINVALID: + pr_err_once("module is not loaded.\n"); + return -ENODEV; + default: + pr_err_once("SEAMCALL failed: CPU %d: leaf %llu, error 0x%llx.\n", + cpu, fn, sret); + if (out) + pr_err_once("additional output: rcx 0x%llx, rdx 0x%llx, r8 0x%llx, r9 0x%llx, r10 0x%llx, r11 0x%llx.\n", + out->rcx, out->rdx, out->r8, + out->r9, out->r10, out->r11); + return -EIO; + } +} + static int __init record_keyid_partitioning(u32 *tdx_keyid_start, u32 *nr_tdx_keyids) { diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h new file mode 100644 index 000000000000..48ad1a1ba737 --- /dev/null +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _X86_VIRT_TDX_H +#define _X86_VIRT_TDX_H + +#include + +struct tdx_module_output; +u64 __seamcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, + struct tdx_module_output *out); +#endif