Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp952831pxb; Wed, 6 Apr 2022 05:11:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBlmGnzhUn8M7GOEaO9FZpFkhxDiRAK6au9eqBdUz/sjgQK361nD0Hb1pqjZuHK8mbr2du X-Received: by 2002:a17:902:e88e:b0:154:7562:176d with SMTP id w14-20020a170902e88e00b001547562176dmr8030734plg.13.1649247099830; Wed, 06 Apr 2022 05:11:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649247099; cv=none; d=google.com; s=arc-20160816; b=Vx6ul4dfy08HcM/nl1Ul7lnFO/1F3L7vmCXHHsnNituBV45XgTUO2CX0NjDU/tiH5K DDndk3qjbcXxwtyM4iKSdzc/pkhJLPgfYGEMcVGv7FIIy8qqbX+VMXPl/HAuIrIK+1zF akA8vNG2vOCPW31YbUIJZwX9EXX4QM1r0gIDl1l5XwegwONt8eEtohZNjchjI+EJy8bm kvEAOwDfai7qG8VrIlXDjHWuRJFQ+8mJ7a3zfJNfElrRh7IEYK3MjgUS00QZeOa2g/hV MoOJ26V+ck1qLCmk6nBXw+SO3isY7bxUHRRq00mbAX/vtwrKhgZtSL36Nth+m0rLmiP1 fj6w== 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=Upb712skcvAb5efabC7Es8iLLzdfCCCXfw4t2bojiIU=; b=nNsjo/E1j28HlFa5lYIe8CvPxFvaGNNO/nFB5p3O8BiC10EljAE2HJCB5xj8/mL7Of mxZiUbK6yDw6T1XbTfMwnNY1CDSTi7Y9SkPPfKUEj0CqCTb5teYOaC94FhrryvNunFfo IsydSQjudCqhPgy5z8glq6IHLzKi9rN7YXQHu7j506xrKftdMHkWOOKYpvzS1SafGyf0 hK59NIGK5ZbG+NE1LluCmTyCRwLA0AjcQ7hOejHB2CzqVrW38oI+XWlmnPCkg701035T m84S7syqMhW9P1jWcymfFW0iinlZ3Q4YuF5i9YXTK3+Uoos0DL5bvtux62Wd1cbloL5U xmpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Ku9Bw49/"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id j13-20020aa78d0d000000b004fa3a8e00c0si14059240pfe.375.2022.04.06.05.11.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 05:11:39 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Ku9Bw49/"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F3D696A4322; Wed, 6 Apr 2022 03:48:41 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2359604AbiDFDQy (ORCPT + 99 others); Tue, 5 Apr 2022 23:16:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1384958AbiDFCOb (ORCPT ); Tue, 5 Apr 2022 22:14:31 -0400 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 135F71B2566 for ; Tue, 5 Apr 2022 16:35:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1649201717; x=1680737717; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5fa58IG6IkoYlTr3b12lt1ElQz5ZUgXZHAh5KeLMD4U=; b=Ku9Bw49/mVml5GAHuo574bTiG8Sngs0EgR50rTbDTkWGEbpqPvw8CRBn avqXA1fjSGoWzhlSTR3WPCexY6QEDdTQLieG7NizO45xFlMVgbbTEjETl pm5G3ld+Hvyt93ac5I7QHkVKz/oMVXJx7/jHrxw3k9I0SoBOjbkKm38BO ZfC0ar3q2ffr71fqeqqKkNBH929a7oNHPLzDmXAS9vhxb2Myph41bxm3t R6UXOrEcXijfi00ttYczLo3vbfaaRy30PE16DHGvae2/5ed3PCJXG08wT ZHjx0hea/QSe4oZLql37kO09XKQwo1790T2LWoep314jdpxBdciaOXls6 Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10308"; a="321586049" X-IronPort-AV: E=Sophos;i="5.90,238,1643702400"; d="scan'208";a="321586049" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Apr 2022 16:34:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,238,1643702400"; d="scan'208";a="652106922" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga002.fm.intel.com with ESMTP; 05 Apr 2022 16:34:41 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id DFF83655; Wed, 6 Apr 2022 02:29:45 +0300 (EEST) From: "Kirill A. Shutemov" To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@intel.com, luto@kernel.org, peterz@infradead.org Cc: sathyanarayanan.kuppuswamy@linux.intel.com, aarcange@redhat.com, ak@linux.intel.com, dan.j.williams@intel.com, david@redhat.com, hpa@zytor.com, jgross@suse.com, jmattson@google.com, joro@8bytes.org, jpoimboe@redhat.com, knsathya@kernel.org, pbonzini@redhat.com, sdeep@vmware.com, seanjc@google.com, tony.luck@intel.com, vkuznets@redhat.com, wanpengli@tencent.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, x86@kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , Dave Hansen Subject: [PATCHv8 09/30] x86/tdx: Add MSR support for TDX guests Date: Wed, 6 Apr 2022 02:29:18 +0300 Message-Id: <20220405232939.73860-10-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405232939.73860-1-kirill.shutemov@linux.intel.com> References: <20220405232939.73860-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 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 Reviewed-by: Andi Kleen Reviewed-by: Tony Luck Signed-off-by: Kirill A. Shutemov Reviewed-by: Dave Hansen Reviewed-by: Thomas Gleixner --- 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 ed7302581cc7..00ff0a830970 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; -- 2.35.1