Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6DE70C433FE for ; Tue, 14 Dec 2021 15:04:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235515AbhLNPEs (ORCPT ); Tue, 14 Dec 2021 10:04:48 -0500 Received: from mga18.intel.com ([134.134.136.126]:28579 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235202AbhLNPDZ (ORCPT ); Tue, 14 Dec 2021 10:03:25 -0500 X-IronPort-AV: E=McAfee;i="6200,9189,10197"; a="225852708" X-IronPort-AV: E=Sophos;i="5.88,205,1635231600"; d="scan'208";a="225852708" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Dec 2021 07:03:15 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,205,1635231600"; d="scan'208";a="754837187" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga005.fm.intel.com with ESMTP; 14 Dec 2021 07:03:09 -0800 Received: by black.fi.intel.com (Postfix, from userid 1000) id A34A9A11; Tue, 14 Dec 2021 17:03: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, x86@kernel.org, linux-kernel@vger.kernel.org, "Kirill A . Shutemov" Subject: [PATCH 11/26] x86/tdx: Add port I/O emulation Date: Tue, 14 Dec 2021 18:02:49 +0300 Message-Id: <20211214150304.62613-12-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211214150304.62613-1-kirill.shutemov@linux.intel.com> References: <20211214150304.62613-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kuppuswamy Sathyanarayanan TDX hypervisors cannot emulate instructions directly. This includes port I/O which is normally emulated in the hypervisor. All port I/O instructions inside TDX trigger the #VE exception in the guest and would be normally emulated there. Use a hypercall to emulate port I/O. Extend the tdx_handle_virt_exception() and add support to handle the #VE due to port I/O instructions. String I/O operations are not supported in TDX. Unroll them by declaring CC_ATTR_GUEST_UNROLL_STRING_IO confidential computing attribute. Signed-off-by: Kuppuswamy Sathyanarayanan Reviewed-by: Andi Kleen Reviewed-by: Dan Williams Signed-off-by: Kirill A. Shutemov --- arch/x86/kernel/cc_platform.c | 3 +++ arch/x86/kernel/tdx.c | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/arch/x86/kernel/cc_platform.c b/arch/x86/kernel/cc_platform.c index e291e071aa63..890452a85dae 100644 --- a/arch/x86/kernel/cc_platform.c +++ b/arch/x86/kernel/cc_platform.c @@ -16,6 +16,9 @@ static bool intel_cc_platform_has(enum cc_attr attr) { + if (attr == CC_ATTR_GUEST_UNROLL_STRING_IO) + return true; + return false; } diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c index b04802b4b69e..00bf02bc9838 100644 --- a/arch/x86/kernel/tdx.c +++ b/arch/x86/kernel/tdx.c @@ -13,6 +13,12 @@ /* TDX Module Call Leaf IDs */ #define TDX_GET_VEINFO 3 +/* See Exit Qualification for I/O Instructions in VMX documentation */ +#define VE_IS_IO_IN(exit_qual) (((exit_qual) & 8) ? 1 : 0) +#define VE_GET_IO_SIZE(exit_qual) (((exit_qual) & 7) + 1) +#define VE_GET_PORT_NUM(exit_qual) ((exit_qual) >> 16) +#define VE_IS_IO_STRING(exit_qual) ((exit_qual) & 16 ? 1 : 0) + static bool tdx_guest_detected __ro_after_init; /* @@ -259,6 +265,45 @@ static int tdx_handle_mmio(struct pt_regs *regs, struct ve_info *ve) return insn.length; } +/* + * Emulate I/O using hypercall. + * + * Assumes the IO instruction was using ax, which is enforced + * by the standard io.h macros. + * + * Return True on success or False on failure. + */ +static bool tdx_handle_io(struct pt_regs *regs, u32 exit_qual) +{ + struct tdx_hypercall_output out; + int size, port, ret; + u64 mask; + bool in; + + if (VE_IS_IO_STRING(exit_qual)) + return false; + + in = VE_IS_IO_IN(exit_qual); + size = VE_GET_IO_SIZE(exit_qual); + port = VE_GET_PORT_NUM(exit_qual); + mask = GENMASK(BITS_PER_BYTE * size, 0); + + /* + * Emulate the I/O read/write via hypercall. More info about + * ABI can be found in TDX Guest-Host-Communication Interface + * (GHCI) sec titled "TDG.VP.VMCALL". + */ + ret = _tdx_hypercall(EXIT_REASON_IO_INSTRUCTION, size, !in, port, + in ? 0 : regs->ax, &out); + if (!in) + return !ret; + + regs->ax &= ~mask; + regs->ax |= ret ? UINT_MAX : out.r11 & mask; + + return !ret; +} + bool tdx_get_ve_info(struct ve_info *ve) { struct tdx_module_output out; @@ -338,6 +383,9 @@ static bool tdx_virt_exception_kernel(struct pt_regs *regs, struct ve_info *ve) if (!ret) pr_warn_once("MMIO failed\n"); break; + case EXIT_REASON_IO_INSTRUCTION: + ret = tdx_handle_io(regs, ve->exit_qual); + break; default: pr_warn("Unexpected #VE: %lld\n", ve->exit_reason); break; -- 2.32.0