From patchwork Tue Sep 12 15:31:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 138435 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp676907vqx; Tue, 12 Sep 2023 14:00:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHjYAnicjS3XUuR1Od5nAqywJ5/h48izCCm+c6L1novfwNd9REq4xt8eM5vbX3BLZOVGjge X-Received: by 2002:a05:6a20:914f:b0:140:253b:b658 with SMTP id x15-20020a056a20914f00b00140253bb658mr543773pzc.57.1694552447399; Tue, 12 Sep 2023 14:00:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694552447; cv=none; d=google.com; s=arc-20160816; b=l4rcTfix/+gPG5Jh60uMO6mWXs7DOIdav4e7R6z9RWRyNMO7M/42imuTXj8a1QgvHd ixQH4MWy8+WwCdKs5yzSmTwnJREtuxSbyh+Avc9KTlrr1B5iCNxUTLmHvTPmGsie5zid h++FYar65Jq3umjxwjon8him7uNlZVvE+6majyMkbLhEzL6iugpVfiwuWPdxOq2Fasno oEvNsOV5AmZMCB/J43YpnFqpeay48kQ84KyLZiRuhkDxL/TXB/038TQAjhObEx13UnJ9 dWSU0U0CtoOP4Kg1LZw+ckNVI8JqQWzQEnZZlIY/w85Ezi/h0oT5WcYWQvh62/FXlaAS BN/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=PP5wRTRD08bVm357yqI9pRN4yJatwNf9xjVkvOAQadI=; fh=8KRjjvFji43CdJHlmuUKtvsc6N1jyfmLpTgAhBZDKXY=; b=Q9cjp1n09uJNqo/ap2DysqMidUnJpawW+8xyqmwDBxcaQgbOxLm5USt5/DmoU5o1/u KFL4Ha5M9gz90nR3CaUXU1R0Z/fix4+KM8PrjEZP8TGLtQ/yqgzEzMoyNvCz14Exe/Ku yk+Yysc/uLup5sIwd1rMQJcKP9n50+mT+0CfmEnYoq5Nl39orLk23GUvLsJ0PZWHtXKv Yh9rvI8/83FZ9gwMreWQWgcgiGd6jtO7b6OF8EyPwFC34mzt92xYud5QI/VwBeNG7lVL XtJKPA+ODKTrKfsXmfopV6SAyM6hKdn47zVRGMh5Y5jpzQt2/NXtlxC+Lw0A5m6ZE+mH v1iQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=0HEWXDPK; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id s17-20020a639251000000b005658d4c82f8si8361451pgn.877.2023.09.12.14.00.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 14:00:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=0HEWXDPK; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 43D5C819D722; Tue, 12 Sep 2023 08:32:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236369AbjILPcI (ORCPT + 37 others); Tue, 12 Sep 2023 11:32:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236248AbjILPcG (ORCPT ); Tue, 12 Sep 2023 11:32:06 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42FBF10E2; Tue, 12 Sep 2023 08:32:02 -0700 (PDT) Date: Tue, 12 Sep 2023 15:31:59 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694532720; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=PP5wRTRD08bVm357yqI9pRN4yJatwNf9xjVkvOAQadI=; b=0HEWXDPKtBkmaXtNCwDODf09lmg8Oek8U3TOVAIW7wKbmbUTv9HeOhqhugal5Y1wgZTZGf 6H4O58IiBFakW35o16I+sJI0gNWSRvcgs9p6b4xvixtHQKKvpaXime57PxJscYKrnp6y19 k9YHZGh+uQ3VvZwdI3Dn73LX7suGY45aP4SooLcwpD+PjOv7lzDXaaQwnbiAOXJmUxwIFn UxTmOQIEUcYfqSZl80qm+Ens6TYGqLkfv2EBhsf6zmFCSgvNsM9Ia2mk7WmVFz4uEYikxw U7Qel5g9t2A/jMI2GfXtI5VeHP4+pRyIz+GsX2/WmL948byeC8dA+JeK0I7lfQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694532720; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=PP5wRTRD08bVm357yqI9pRN4yJatwNf9xjVkvOAQadI=; b=VKuDCuYIf4KvkG0aPgbEp4mXjRpdaGDKWVyAfwcrnJ7vf0CTUrd2/1CMKFJAVNdouBRyO3 G6nvpuX/lL9DAVBQ== From: "tip-bot2 for Kai Huang" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/tdx] x86/virt/tdx: Make TDX_MODULE_CALL handle SEAMCALL #UD and #GP Cc: Peter Zijlstra , Kai Huang , Dave Hansen , "Kirill A. Shutemov" , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <169453272013.27769.12828428554321636529.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 12 Sep 2023 08:32:17 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776867026839731496 X-GMAIL-MSGID: 1776867026839731496 The following commit has been merged into the x86/tdx branch of tip: Commit-ID: d6cb0f01644c5cd2bc51b88df933e07849cfaa76 Gitweb: https://git.kernel.org/tip/d6cb0f01644c5cd2bc51b88df933e07849cfaa76 Author: Kai Huang AuthorDate: Tue, 15 Aug 2023 23:02:05 +12:00 Committer: Dave Hansen CommitterDate: Mon, 11 Sep 2023 16:35:04 -07:00 x86/virt/tdx: Make TDX_MODULE_CALL handle SEAMCALL #UD and #GP SEAMCALL instruction causes #UD if the CPU isn't in VMX operation. Currently the TDX_MODULE_CALL assembly doesn't handle #UD, thus making SEAMCALL when VMX is disabled would cause Oops. Unfortunately, there are legal cases that SEAMCALL can be made when VMX is disabled. For instance, VMX can be disabled due to emergency reboot while there are still TDX guests running. Extend the TDX_MODULE_CALL assembly to return an error code for #UD to handle this case gracefully, e.g., KVM can then quietly eat all SEAMCALL errors caused by emergency reboot. SEAMCALL instruction also causes #GP when TDX isn't enabled by the BIOS. Use _ASM_EXTABLE_FAULT() to catch both exceptions with the trap number recorded, and define two new error codes by XORing the trap number to the TDX_SW_ERROR. This opportunistically handles #GP too while using the same simple assembly 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 code rather than a less understandable Oops. This is basically based on Peter's code. Suggested-by: Peter Zijlstra Signed-off-by: Kai Huang Signed-off-by: Dave Hansen Reviewed-by: Kirill A. Shutemov Acked-by: Peter Zijlstra (Intel) Link: https://lore.kernel.org/all/de975832a367f476aab2d0eb0d9de66019a16b54.1692096753.git.kai.huang%40intel.com --- 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 a69bb7d..adcbe3f 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 3f0b83a..016a2a1 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 @@ -191,11 +193,28 @@ .if \host .Lseamcall_vmfailinvalid\@: mov $TDX_SEAMCALL_VMFAILINVALID, %rax + jmp .Lseamcall_fail\@ + +.Lseamcall_trap\@: + /* + * SEAMCALL caused #GP or #UD. By reaching here RAX 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