Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp299583pxb; Tue, 12 Apr 2022 02:11:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw8RQiE6v9gxn6imJLr+5+hDknEccodHVoUGJujJq+S6PHaYQ+mTGCnkC/VK/d0gitVggl9 X-Received: by 2002:a17:902:aa07:b0:158:6d3e:7c54 with SMTP id be7-20020a170902aa0700b001586d3e7c54mr2369418plb.91.1649754672077; Tue, 12 Apr 2022 02:11:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649754672; cv=none; d=google.com; s=arc-20160816; b=1IWaEE9mWYlqfxxFIPScPC4hexKMN9DGrteme5sldoUyFgmsGMch6tKosdOPfW4QNg T/JvYq4Ln29VaJxIlFXshjWGYYkf2scq8+0dOkwONYP2ozczUvlEr29uGWY87SIvQhV8 q0uLFYhyg5feO5Fg6I39ZKXTKNQjFu7+i16MaitsBWybNeuZEjMUZTxvVFV3e1b8jclH UynnrU7x53B2hMYqeen8pAPZPMlA7GgsYW58Cj3sDHUfRRxWdtkQtLtOYZjLS9vZusVQ wuVhp7LrfBb5YaXP3WZ4DgLJXqYDGpmwDBLnG0Hcxs/nZ3LLPlX5Rz5s2jd0hFHNgNIa Rw3Q== 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:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=O7j/V4cosJnhlCYRww6/sf2/D/QNhSVeVKjySnicq3A=; b=xxuyA7ApuK77d8wTO+AkmpkqDCCxs+deUYMrwEDgPpa6rz5Z/2A2eLBtwqY+k3bJNW YMfLsGGL+oOMrqucYMNecvCcqssG7rjniDnEZxU7bjdiOQ3f/uGpTsmaJtfI3aUklsGV KZsNsWsd9qe8wv5oX0MLT/zYkbBVXFqqGxKrBPKV5bZgHUYIc+n+mbfdcD6+36ITPJPf X17VpYXnlnXHS9l8829NvBQ9Stg/e8nwAaARy0U2Fwk3AgYGnBy8XtLITXif9oSVtxBf p7PUyksW77Y2bvxF9xf3iQDVoLy07irJZ+zHDQcPAtW8cSB6yB/giXZhJ0GUIJivg8+j B7Iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="v/9shhr0"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 19-20020a170902ee5300b001560317fb7csi10003491plo.110.2022.04.12.02.10.58; Tue, 12 Apr 2022 02:11:12 -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=@linutronix.de header.s=2020 header.b="v/9shhr0"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240270AbiDIBbA (ORCPT + 99 others); Fri, 8 Apr 2022 21:31:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240374AbiDIBaZ (ORCPT ); Fri, 8 Apr 2022 21:30:25 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE6531107F1; Fri, 8 Apr 2022 18:27:38 -0700 (PDT) Date: Sat, 09 Apr 2022 01:27:36 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1649467657; 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: in-reply-to:in-reply-to:references:references; bh=O7j/V4cosJnhlCYRww6/sf2/D/QNhSVeVKjySnicq3A=; b=v/9shhr09nnR6g2Q1Pdipz0rAVO7zsAaFJpIjgaQiyf1/lljE5mhvenvIz8iQrUKk2b6B3 600svDpr87R7MkO81HUCP4A2VF4n3UGh3/HWDZG6RfVV7iBl/DVtWGgbRbxa142X3vm3zE TJ78jP0VJuCzpLTcVV0RiaC5imF/Xk7xMXwq52QNjy+DWum7Zmn88Zl73MueP/vB8NOL83 YsSjVhPFmz4u2aQOpG2JxiAf1QYiEuhXUXmbdPaEa1UsJ56BG5/g9CXBvUWmHEcWi7wGjU 1R2mnlMyzOSLbUFLtR3mPRsXxtRociBoRNzl84ydz+dVvyrh8ETeIJQxXVtyzw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1649467657; 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: in-reply-to:in-reply-to:references:references; bh=O7j/V4cosJnhlCYRww6/sf2/D/QNhSVeVKjySnicq3A=; b=+kKTzcy7iTTXtc9y3eOia74tjdsrW8Jlk139uIKZmwrqk1TaWVDC+UwsnaSUF7h9de+fKd VtFmG1GWfzALeqDA== From: "tip-bot2 for Kirill A. Shutemov" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/tdx] x86/tdx: Add MSR support for TDX guests Cc: Kuppuswamy Sathyanarayanan , "Kirill A. Shutemov" , Dave Hansen , Andi Kleen , Tony Luck , Thomas Gleixner , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20220405232939.73860-10-kirill.shutemov@linux.intel.com> References: <20220405232939.73860-10-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Message-ID: <164946765646.4207.5045926072837872321.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 The following commit has been merged into the x86/tdx branch of tip: Commit-ID: ae87f609cd52825fa7fa36f02b29e4357fd29eaa Gitweb: https://git.kernel.org/tip/ae87f609cd52825fa7fa36f02b29e4357fd29eaa Author: Kirill A. Shutemov AuthorDate: Wed, 06 Apr 2022 02:29:18 +03:00 Committer: Dave Hansen CommitterDate: Thu, 07 Apr 2022 08:27:51 -07:00 x86/tdx: Add MSR support for TDX guests Use hypercall to emulate MSR read/write for the TDX platform. There are two viable approaches for doing MSRs in a TD guest: 1. Execute the RDMSR/WRMSR instructions like most VMs and bare metal do. Some will succeed, others will cause a #VE. All of those that cause a #VE will be handled with a TDCALL. 2. Use paravirt infrastructure. The paravirt hook has to keep a list of which MSRs would cause a #VE and use a TDCALL. All other MSRs execute RDMSR/WRMSR instructions directly. The second option can be ruled out because the list of MSRs was challenging to maintain. That leaves option #1 as the only viable solution for the minimal TDX support. Kernel relies on the exception fixup machinery to handle MSR access errors. #VE handler uses the same exception fixup code as #GP. It covers MSR accesses along with other types of fixups. For performance-critical MSR writes (like TSC_DEADLINE), future patches will replace the WRMSR/#VE sequence with the direct TDCALL. RDMSR and WRMSR specification details can be found in Guest-Host-Communication Interface (GHCI) for Intel Trust Domain Extensions (Intel TDX) specification, sec titled "TDG.VP. VMCALL" and "TDG.VP.VMCALL". Co-developed-by: Kuppuswamy Sathyanarayanan Signed-off-by: Kuppuswamy Sathyanarayanan Signed-off-by: Kirill A. Shutemov Signed-off-by: Dave Hansen Reviewed-by: Andi Kleen Reviewed-by: Tony Luck Reviewed-by: Dave Hansen Reviewed-by: Thomas Gleixner Link: https://lkml.kernel.org/r/20220405232939.73860-10-kirill.shutemov@linux.intel.com --- arch/x86/coco/tdx/tdx.c | 42 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index ed73025..00ff0a8 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -142,6 +142,44 @@ void __cpuidle tdx_safe_halt(void) WARN_ONCE(1, "HLT instruction emulation failed\n"); } +static bool read_msr(struct pt_regs *regs) +{ + struct tdx_hypercall_args args = { + .r10 = TDX_HYPERCALL_STANDARD, + .r11 = hcall_func(EXIT_REASON_MSR_READ), + .r12 = regs->cx, + }; + + /* + * Emulate the MSR read via hypercall. More info about ABI + * can be found in TDX Guest-Host-Communication Interface + * (GHCI), section titled "TDG.VP.VMCALL". + */ + if (__tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT)) + return false; + + regs->ax = lower_32_bits(args.r11); + regs->dx = upper_32_bits(args.r11); + return true; +} + +static bool write_msr(struct pt_regs *regs) +{ + struct tdx_hypercall_args args = { + .r10 = TDX_HYPERCALL_STANDARD, + .r11 = hcall_func(EXIT_REASON_MSR_WRITE), + .r12 = regs->cx, + .r13 = (u64)regs->dx << 32 | regs->ax, + }; + + /* + * Emulate the MSR write via hypercall. More info about ABI + * can be found in TDX Guest-Host-Communication Interface + * (GHCI) section titled "TDG.VP.VMCALL". + */ + return !__tdx_hypercall(&args, 0); +} + void tdx_get_ve_info(struct ve_info *ve) { struct tdx_module_output out; @@ -178,6 +216,10 @@ static bool virt_exception_kernel(struct pt_regs *regs, struct ve_info *ve) switch (ve->exit_reason) { case EXIT_REASON_HLT: return handle_halt(); + case EXIT_REASON_MSR_READ: + return read_msr(regs); + case EXIT_REASON_MSR_WRITE: + return write_msr(regs); default: pr_warn("Unexpected #VE: %lld\n", ve->exit_reason); return false;