Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp422729pxp; Wed, 16 Mar 2022 08:28:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVIn7n7h3Frvkuv+1VyCI37xl3r8qvwpvF82VuiCRnGf/LV2gUnG42B/pvxOIC6ZMY2/ol X-Received: by 2002:a17:902:e944:b0:14e:dc4f:f099 with SMTP id b4-20020a170902e94400b0014edc4ff099mr446135pll.161.1647444498404; Wed, 16 Mar 2022 08:28:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647444498; cv=none; d=google.com; s=arc-20160816; b=wE0mByg77D2+Hn4TRoaYog54UVU7SWf2mUhjzo1H30t1DJt3y9qxbFl8SamHtjpc7G GdMrgBTKiWpvQmUjbBjc9cOQ2u+Tjgyztegfpa+rNCtFCyM+rDstHLLI0vb5H7EWXLVt txsTCewnD5pyvZpAi1DRL+DjV9+HTa7UzwbFEQMgY+3+WGKzx6Uss3Pw6K16C9wtNee4 dKvMCjiuaOOXIHHdRZpwfHIMnOQmc3pOYcAUTwjbeMOrrihGlVGSrU4Z+WDJSgfpgvzW T9zuP+UwvXsG148ZN9dPp/8HKMkmSwTwm2gdl5FVwACZkSk5nOT+LSaCuSeEbU/jqI25 GC5w== 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=pFe5Kw1Glxbsn2vs28Bv6VmNJYtmPCKN/m1cmFDezJ8=; b=yEyxc69OnhZnyDK19FlgCH93lKrDLYQSSVdRGhm9SVT1r9QyLB93ZBuKQo9BI8s6We U08QtBUI5dAQK8VVCE66Mdqiau1d38zOvynY/halLXWxubuZoJb5SVZS6QtHJmpnsNsp GCXpiAzTndRLGasG9IaWHE8Gfr+54DX0f/OYfgqanRta1ylhVBiGQy/GVX7Pi1O6VWmy 9kxWVbey+M0iwOG67cPUiinOQFGUdqLuY09fGolqEBG22uhs9b9+FWG3WaVwyCSzfALl vqxWG9dQu2Kayatmwak6N8RU9GjrFanc2+XZI1n3urHLUz8m/mkDsndw3lgfSo7rsB4r qkSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=SSb5Nm30; 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 j3-20020a170903028300b00153ae51977esi2651875plr.307.2022.03.16.08.27.58; Wed, 16 Mar 2022 08:28:18 -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=SSb5Nm30; 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 S1352778AbiCPCL5 (ORCPT + 99 others); Tue, 15 Mar 2022 22:11:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352042AbiCPCLZ (ORCPT ); Tue, 15 Mar 2022 22:11:25 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 694465E759 for ; Tue, 15 Mar 2022 19:10:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1647396607; x=1678932607; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/H9cxL3Wn0YCVimtCdpIhLpxVTiPjXcc6SSTk/dzTTQ=; b=SSb5Nm30KvmAdiRTyzox48Hj2hjoNNpph2zmVfDolmriDUcxD/oCD4Ky v8dAKECwKKVUzNd2PwQ6QqutKX4YpXutTmwAupUkkOjLyA9DBPFcqniSP lNbq8Qml+GSgwFuoNAGkcBpN0x4KLYD4zzSXTHLcb6oLmjwYP3T/U7QGF j3u45SIMGLP/Q+VLZawUD41tTZ0hc56GgourA2RJrI5gA5ViZI+scZ7e2 xD1+O0aNdYOs8Pt0uxVZKe++0uLpGWe5DckXlYvROZc39pfAvLY5h5X3D JfQC1n5bDj08nlOs1Pf6CJeJ0ki4wDHSOw/pYIArqlJUbAWAMe1mgTJ4+ A==; X-IronPort-AV: E=McAfee;i="6200,9189,10286"; a="256422498" X-IronPort-AV: E=Sophos;i="5.90,185,1643702400"; d="scan'208";a="256422498" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Mar 2022 19:10:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,185,1643702400"; d="scan'208";a="690414200" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 15 Mar 2022 19:09:57 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id EB16D77A; Wed, 16 Mar 2022 04:10:09 +0200 (EET) 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: [PATCHv6 09/30] x86/tdx: Add MSR support for TDX guests Date: Wed, 16 Mar 2022 05:08:35 +0300 Message-Id: <20220316020856.24435-10-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220316020856.24435-1-kirill.shutemov@linux.intel.com> References: <20220316020856.24435-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_NONE,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 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 --- 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 3795a70463aa..8addc242aa0e 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -135,6 +135,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; @@ -164,6 +202,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.34.1