Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp911648pxp; Wed, 16 Mar 2022 21:03:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx0HzVbQo8hVRgn3sgLMwqjs0Xcb1gFPJzXok7sgQ5hK3BMMhkSaraLv3ehBDv6ghglHEp1 X-Received: by 2002:a17:902:aa0b:b0:151:e196:c6e4 with SMTP id be11-20020a170902aa0b00b00151e196c6e4mr2852585plb.162.1647489794585; Wed, 16 Mar 2022 21:03:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647489794; cv=none; d=google.com; s=arc-20160816; b=cY7o7sWulRS75QA5yC/TbuXzKALwOAOT4sMNiEZYHEr92c76pVoSsDvGaXioIQzz+n V9Wjazap4aJ/+H08omjzCLCP1+pkpPeMzg7U7hBWDhmwEcOJLf00tmrCQFcuJ7Z4F0d6 HhPzCCva/ermESQIr/I8Bymf0S5S9fIovgqOqYQ56qmkLT26L4z+YooI9EHVibGJOO31 trDfqeK6pJ7jwstGyMdvsuAxsC03SQ6arvLvUNtmpa2CNMC40HBLVUtaMzAj0RrI6L8f M9BqMjfVHLmg+5OZi82N8JgZz41Q69bTcbfXPT17bBhY9rS238RTnmwehyGzEuTg//Ja nNnQ== 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=JC2J8quxGe9Xrp0zEjARxr3qbjA5ySQ4fXhQJ0RMsVQ=; b=XjHI0ofkAp+WOY4xGpTxVAlhp3YyHznZNhN2ndb87ueWLl5gg4K1IG0ZYaPdWZiA23 K8mkmMOb8lnf39nUsbD3tyyGYhHQ2YsQH7vXaAkqXR8mLgJvLrBB1b75ffxMWzz+Xni5 Jz5Bem83LPcEatYXALA5ySe+BC17qXTpbqPXGzTEoV1pEs9UXKOSeBaKaocq9A6JgJov NBZvly1aEZlKg9hKZu2UF0ATGLilkYvquLQlG015ZHMeH7F4Vs6qAsE3exPUyJwPrd4a 1Q8D+C4e8Oq4RGRvpcQeC4GIuRJ1inF3Tfo1H0XSBX5WFfN1OuoIFPWwoos6sIEBo6V9 XlLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="g4pS+/Sh"; 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 j7-20020a170902690700b00153872697dcsi3324858plk.542.2022.03.16.21.03.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 21:03:14 -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="g4pS+/Sh"; 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 8CEC35676E; Wed, 16 Mar 2022 20:45:29 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353084AbiCPCNO (ORCPT + 99 others); Tue, 15 Mar 2022 22:13:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353082AbiCPCL5 (ORCPT ); Tue, 15 Mar 2022 22:11:57 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D7C35E77F for ; Tue, 15 Mar 2022 19:10:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1647396623; x=1678932623; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7vy8Es+waSYbbTPTYGpEvEtFa6NrMN5IUtBBXlV1bbA=; b=g4pS+/ShFeUoERyB25lE2MvX5POU2IWSRD3VQ8cGRAobbooktc19tESm VovltISK71vyea3ZaQC/xE+3zABEnNkfpCu/SQiE2u5XnU4cbYuF2z93V tRUV5xMA1ipFxXvdRU1oKwCd5c9jzWIphyXIXVlQLfRRLE32mV1uM0hB4 AxiZ7kzOi4QQRnx5XkGkRxb29Pg6OfKlZWV0H5iqLFkkRzMXLGj3QwIgc 7EivnIDOJPZRV8aVMiQs5grzKKD4OiZOCE155LIVXEW/nDuIJQjxPU+sA 9Eiaw+DjfDYUVCc524paTKSB3XTVzykFsWJapYmVK4/fy/bOGE9HGQY1s Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10286"; a="319688295" X-IronPort-AV: E=Sophos;i="5.90,185,1643702400"; d="scan'208";a="319688295" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Mar 2022 19:10:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,185,1643702400"; d="scan'208";a="613475528" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga004.fm.intel.com with ESMTP; 15 Mar 2022 19:10:04 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 62E6A8DF; Wed, 16 Mar 2022 04:10:10 +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 17/30] x86/tdx: Port I/O: add runtime hypercalls Date: Wed, 16 Mar 2022 05:08:43 +0300 Message-Id: <20220316020856.24435-18-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=-3.4 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=unavailable 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 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. == Userspace Implications == The ioperm() facility allows userspace access to I/O instructions like inb/outb. Among other things, this allows writing userspace device drivers. This series has no special handling for ioperm(). Users will be able to successfully request I/O permissions but will induce a #VE on their first I/O instruction which leads SIGSEGV. If this is undesirable users can enable kernel lockdown feature with 'lockdown=integrity' kernel command line option. It makes ioperm() fail. More robust handling of this situation (denying ioperm() in all TDX guests) will be addressed in follow-on work. Signed-off-by: Kuppuswamy Sathyanarayanan Reviewed-by: Andi Kleen Reviewed-by: Dan Williams Signed-off-by: Kirill A. Shutemov Reviewed-by: Dave Hansen --- arch/x86/coco/core.c | 7 +++- arch/x86/coco/tdx/tdx.c | 79 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/arch/x86/coco/core.c b/arch/x86/coco/core.c index 3f3008783e05..df08edc94f9b 100644 --- a/arch/x86/coco/core.c +++ b/arch/x86/coco/core.c @@ -18,7 +18,12 @@ static u64 cc_mask __ro_after_init; static bool intel_cc_platform_has(enum cc_attr attr) { - return false; + switch (attr) { + case CC_ATTR_GUEST_UNROLL_STRING_IO: + return true; + default: + return false; + } } /* diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 68da658f563f..3caa5f551d90 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -19,6 +19,16 @@ #define EPT_READ 0 #define EPT_WRITE 1 +/* Port I/O direction */ +#define PORT_READ 0 +#define PORT_WRITE 1 + +/* See Exit Qualification for I/O Instructions in VMX documentation */ +#define VE_IS_IO_IN(e) ((e) & BIT(3)) +#define VE_GET_IO_SIZE(e) (((e) & GENMASK(2, 0)) + 1) +#define VE_GET_PORT_NUM(e) ((e) >> 16) +#define VE_IS_IO_STRING(e) ((e) & BIT(4)) + /* * Wrapper for standard use of __tdx_hypercall with no output aside from * return code. @@ -334,6 +344,73 @@ static bool handle_mmio(struct pt_regs *regs, struct ve_info *ve) return true; } +static bool handle_in(struct pt_regs *regs, int size, int port) +{ + struct tdx_hypercall_args args = { + .r10 = TDX_HYPERCALL_STANDARD, + .r11 = hcall_func(EXIT_REASON_IO_INSTRUCTION), + .r12 = size, + .r13 = PORT_READ, + .r14 = port, + }; + bool success; + u64 mask = GENMASK(BITS_PER_BYTE * size, 0); + + /* + * Emulate the I/O read via hypercall. More info about ABI can be found + * in TDX Guest-Host-Communication Interface (GHCI) section titled + * "TDG.VP.VMCALL". + */ + success = !__tdx_hypercall(&args, TDX_HCALL_HAS_OUTPUT); + + /* Update part of the register affected by the emulated instruction */ + regs->ax &= ~mask; + if (success) + regs->ax |= args.r11 & mask; + + return success; +} + +static bool handle_out(struct pt_regs *regs, int size, int port) +{ + u64 mask = GENMASK(BITS_PER_BYTE * size, 0); + + /* + * Emulate the I/O 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(hcall_func(EXIT_REASON_IO_INSTRUCTION), size, + PORT_WRITE, port, regs->ax & mask); +} + +/* + * 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 handle_io(struct pt_regs *regs, u32 exit_qual) +{ + bool in; + int size, port; + + 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); + + + if (in) + return handle_in(regs, size, port); + else + return handle_out(regs, size, port); +} + void tdx_get_ve_info(struct ve_info *ve) { struct tdx_module_output out; @@ -383,6 +460,8 @@ static bool virt_exception_kernel(struct pt_regs *regs, struct ve_info *ve) return handle_cpuid(regs); case EXIT_REASON_EPT_VIOLATION: return handle_mmio(regs, ve); + case EXIT_REASON_IO_INSTRUCTION: + return handle_io(regs, ve->exit_qual); default: pr_warn("Unexpected #VE: %lld\n", ve->exit_reason); return false; -- 2.34.1