Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp941058rwb; Wed, 26 Jul 2023 05:29:08 -0700 (PDT) X-Google-Smtp-Source: APBJJlFQnrixBufIVd0pyNmIUasHL+5fPk9obHBesHpIgrmGut7T98Jv0z+SvtaqQCzpd3AYpEhQ X-Received: by 2002:a17:906:104e:b0:98e:2b01:ab97 with SMTP id j14-20020a170906104e00b0098e2b01ab97mr1464771ejj.68.1690374548024; Wed, 26 Jul 2023 05:29:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690374548; cv=none; d=google.com; s=arc-20160816; b=oOC7UFUhlDlcU/RDd0VWRffrLxxRKlxeHKh/RaV6NLRHHza6WQ8g4E1EBhfL10Z4mU ABMK6WpNHP0sUFc4pVk9OmumFPwlCK3U5JYMN/+AAGbO9mQ7ru4+HGhV8oBUggCCLq4e KdxSeiKV3OCpq5NnSBAyh7Wn37FWXiHK71rr5Q2JdHFRqUPJ1wGSozMqN8C3n5/c6Jc8 8gv7INNdIav4hiFnPpxSdwIQvCjO8k8mTK8F1SeOZ3JtRdPWghLweg2IuMbRSzrhYkGO y0lqRSOlTbzXKXjcrpVIt9AKVtLTk3iMmuoEmQsYaDJ/y58NnS98B4ycFlyxlRdL6t7z arqA== 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=8H7vQUODKjHYE/gIn5gIHhTL4HgKb4WucTi+V1dcbtY=; fh=2mS+cL9HFyFsmDWXYa/oYA+er1w4QmXcvRQHpch1hp4=; b=rAbu9kmzyzoRtdpwNN5hWzKjM/4ajyaX0vkClEn1CQzxvvME9A55Zk/tsIjJc3rG4t uoxjgOzfVGiRkKGHWwwRUM09wEdVk4VpHzYFtgF2PL6BtH+O7wwD+vFwXO9zNTNMkmSr XsvcskkJTEIl0/c44lJ5TP1JOyJM01ZPZz4965D0E9g++VaVOw0WQMHc/OLIBOZ6lvdM 5Xw94XN+lpGvIVmXXiPQtC2L1/r3ZpSXbPFSp0X83dnRrCjUdZgGworaaTi6rSYifX4l f/fEPDxzUI8FIqz3Mq9+iGpASxG41+jMst8nuzFyFUmniNFCGti07E5FWfur9ZSUVJJt +dBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=bqmDYNbb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cb8-20020a170906a44800b0097885fd66f4si10073391ejb.162.2023.07.26.05.28.43; Wed, 26 Jul 2023 05:29:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=bqmDYNbb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S233808AbjGZL1a (ORCPT + 99 others); Wed, 26 Jul 2023 07:27:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233633AbjGZL1L (ORCPT ); Wed, 26 Jul 2023 07:27:11 -0400 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2184F2D61 for ; Wed, 26 Jul 2023 04:26:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690370803; x=1721906803; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PCqk/V8S9JuDbXKPeJIkYLpPXwKGYNM/R3Cxhj2/vcE=; b=bqmDYNbbmWA50IgNuv3M0xvtsYNJZoZwV6H2fwAzRDX0yriLXd1pxtbW b/xOwoizopnlJxLM7yEL9jt7Xva5lhBH1NCv9e9xrHrjhko1lKguebLLt 87u3mae+Vn4OKnvfixk5qL+mlsTVNYccAjNEI8WfBwRLzoRinanjtG1wi owJadqOhzk0nTrATRlE9xfBXHyQeNFWeZmON5eg5DA4kWFk9keeQtpENf Q19hJx+v2YnjViL1qlW7QnAvPFSfGQAPaYZiIwteiQDm8lTxEy+wzeUdj Db0m9KNiPNn/EzPnMJN+Jjeys3CUv23hBcB8dNYDTnWQR7TaCHrhLZguf A==; X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="370664229" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="370664229" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jul 2023 04:26:26 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="791811223" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="791811223" Received: from bterry-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.209.177.246]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jul 2023 04:26:23 -0700 From: Kai Huang To: peterz@infradead.org, kirill.shutemov@linux.intel.com, linux-kernel@vger.kernel.org Cc: dave.hansen@intel.com, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, x86@kernel.org, seanjc@google.com, pbonzini@redhat.com, isaku.yamahata@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, n.borisov.lkml@gmail.com, kai.huang@intel.com Subject: [PATCH v3 11/12] x86/virt/tdx: Allow SEAMCALL to handle #UD and #GP Date: Wed, 26 Jul 2023 23:25:13 +1200 Message-ID: <659b10910c206cb4a8de314fcf7cd58814279aa6.1690369495.git.kai.huang@intel.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org TDX memory has integrity and confidentiality protections. Violations of this integrity protection are supposed to only affect TDX operations and are never supposed to affect the host kernel itself. In other words, the host kernel should never, itself, see machine checks induced by the TDX integrity hardware. Alas, the first few generations of TDX hardware have an erratum. A partial write to a TDX private memory cacheline will silently "poison" the line. Subsequent reads will consume the poison and generate a machine check. According to the TDX hardware spec, neither of these things should have happened. On the platform with this erratum, it would be nice if the #MC handler could print additional information to show the #MC was TDX private memory error due to possible kernel bug. To do that, the machine check handler needs to use SEAMCALL to query page type of the error memory from the TDX module, because there's no existing infrastructure to track TDX private pages. SEAMCALL instruction causes #UD if CPU isn't in VMX operation. In #MC handler, it is legal that CPU isn't in VMX operation when making this SEAMCALL. Extend the TDX_MODULE_CALL macro to handle #UD so the SEAMCALL can return error code instead of Oops in the #MC handler. Opportunistically handles #GP too since they share the same code. A bonus is when kernel mistakenly calls SEAMCALL when CPU isn't in VMX operation, or when TDX isn't enabled by the BIOS, or when the BIOS is buggy, the kernel can get a nicer error message rather than a less understandable Oops. This is basically based on Peter's code. Cc: Kirill A. Shutemov Cc: Dave Hansen Cc: Peter Zijlstra Suggested-by: Peter Zijlstra Signed-off-by: Kai Huang --- v2 -> v3: - Added more material to the changelog to explain the TDX erratum (hoping it can be merged together with this series). - Changed to use %rdi to hold $TDX_SW_ERROR instead of %r12 as the latter is callee-saved. v1 -> v2: - Skip saving output registers when SEAMCALL #UD/#GP --- arch/x86/include/asm/tdx.h | 4 ++++ arch/x86/virt/vmx/tdx/tdxcall.S | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index a69bb7d3061b..adcbe3f1de30 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -8,6 +8,7 @@ #include #include +#include #include /* @@ -20,6 +21,9 @@ #define TDX_SW_ERROR (TDX_ERROR | GENMASK_ULL(47, 40)) #define TDX_SEAMCALL_VMFAILINVALID (TDX_SW_ERROR | _UL(0xFFFF0000)) +#define TDX_SEAMCALL_GP (TDX_SW_ERROR | X86_TRAP_GP) +#define TDX_SEAMCALL_UD (TDX_SW_ERROR | X86_TRAP_UD) + #ifndef __ASSEMBLY__ /* diff --git a/arch/x86/virt/vmx/tdx/tdxcall.S b/arch/x86/virt/vmx/tdx/tdxcall.S index 3ed6d8b8d2a9..ccb7ff08078a 100644 --- a/arch/x86/virt/vmx/tdx/tdxcall.S +++ b/arch/x86/virt/vmx/tdx/tdxcall.S @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0 */ #include #include +#include #include /* @@ -85,6 +86,7 @@ .endif /* \saved */ .if \host +.Lseamcall\@: seamcall /* * SEAMCALL instruction is essentially a VMExit from VMX root @@ -192,11 +194,28 @@ .if \host .Lseamcall_vmfailinvalid\@: mov $TDX_SEAMCALL_VMFAILINVALID, %rax + jmp .Lseamcall_fail\@ + +.Lseamcall_trap\@: + /* + * SEAMCALL caused #GP or #UD. By reaching here %eax contains + * the trap number. Convert the trap number to the TDX error + * code by setting TDX_SW_ERROR to the high 32-bits of %rax. + * + * Note cannot OR TDX_SW_ERROR directly to %rax as OR instruction + * only accepts 32-bit immediate at most. + */ + movq $TDX_SW_ERROR, %rdi + orq %rdi, %rax + +.Lseamcall_fail\@: .if \ret && \saved /* pop the unused structure pointer back to %rsi */ popq %rsi .endif jmp .Lout\@ + + _ASM_EXTABLE_FAULT(.Lseamcall\@, .Lseamcall_trap\@) .endif /* \host */ .endm -- 2.41.0