From nobody Sat Nov 30 02:54:16 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 12C4F4F20E; Fri, 13 Sep 2024 17:06:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726247187; cv=none; b=qXsaQmkkw4N4xAdWL+vzThU2/nyeAenEgIT2u50LEj1wCVtsipvz8adS1P3xz2JmL4huAKho6s9YiU1HqZ8Gv3Fid9stt1sRsgFNpzisb5aTuYEh8xRHVN9OhWD7n7X2jH8d9E/dF5ulMQHSX3w+MjHrSckeiX2gh412bU97x4A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726247187; c=relaxed/simple; bh=F/xRqCRoXDUPxpDyWTV8ycqqc2DnTbaoD2OG8e3q4Uc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Go9PEUVpVZ8T7aJY/O/beOafQxDv40vQXgln4lLTd+pg7l/Wg34FksoBue7qmiydknL9QcPt3xXdncSLD3LEai8ApaLLFpw+0C5cgDeIZMLT4We5R3cE50fCOpkYHRrldPcHWJhVkDVcQ7kSqIp1nqQEjCwdlQHtqxPeOI8v6Y4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dKy/Juz6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dKy/Juz6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21932C4CECC; Fri, 13 Sep 2024 17:06:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726247186; bh=F/xRqCRoXDUPxpDyWTV8ycqqc2DnTbaoD2OG8e3q4Uc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dKy/Juz6AA6ZcbF02HZoEw2DS3GFRLrbcnEiAVyMzvgP6jfi4u3eN1CGCNBY3Yqri tqAhk7fBWqZL7ClPDoXiwAmKfYyNxgkqUWb1vUhJsZFWHvMV9jqY7IP+oiwM9mpSZg X20vJH+aumvXOLlSGUSCfg88cri1ub+xpfFYMOf3s0sngoLx6CC7t82SFy3h6LlyJs ZIxDMh6TeMK0mu6iqclSauhtUmDQ17cXtgoMTVqXwNkitXKPSwO+TOC+H96pbzLAfe eG818ne/iMgmog0UCHJFiYk3SDzEVqwKAd/86dY4xhPgtC9N42FgwC8NWuWIJwn11n MOK9FHuAXbtSg== From: Alexey Gladkov To: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev Cc: "Alexey Gladkov (Intel)" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , "Kirill A. Shutemov" , Andrew Morton , Yuan Yao , Geert Uytterhoeven , Yuntao Wang , Kai Huang , Baoquan He , Oleg Nesterov , cho@microsoft.com, decui@microsoft.com, John.Starks@microsoft.com Subject: [PATCH v7 4/6] x86/tdx: Allow MMIO from userspace Date: Fri, 13 Sep 2024 19:05:59 +0200 Message-ID: <5a94e08959fc9360eddd30a5743f16165353282b.1726237595.git.legion@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alexey Gladkov (Intel)" The MMIO emulation is only allowed for kernel space code. It is carried out through a special API, which uses only certain instructions. This does not allow userspace to work with virtual devices. Allow userspace to use the same instructions as kernel space to access MMIO. Additional checks have been added previously. Reviewed-by: Thomas Gleixner Reviewed-by: Kirill A. Shutemov Signed-off-by: Alexey Gladkov (Intel) --- arch/x86/coco/tdx/tdx.c | 45 +++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 30651a5af180..dffc343e64d7 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -488,6 +488,32 @@ static int valid_vaddr(struct ve_info *ve, enum insn_m= mio_type mmio, int size, return 0; } =20 +static int decode_insn_struct(struct insn *insn, struct pt_regs *regs) +{ + char buffer[MAX_INSN_SIZE]; + + if (user_mode(regs)) { + int nr_copied =3D insn_fetch_from_user(regs, buffer); + + if (nr_copied <=3D 0) + return -EFAULT; + + if (!insn_decode_from_regs(insn, regs, buffer, nr_copied)) + return -EINVAL; + } else { + if (copy_from_kernel_nofault(buffer, (void *)regs->ip, MAX_INSN_SIZE)) + return -EFAULT; + + if (insn_decode(insn, buffer, MAX_INSN_SIZE, INSN_MODE_64)) + return -EINVAL; + } + + if (!insn->immediate.got) + return -EINVAL; + + return 0; +} + static int handle_mmio_write(struct insn *insn, enum insn_mmio_type mmio, = int size, struct pt_regs *regs, struct ve_info *ve) { @@ -568,27 +594,20 @@ static int handle_mmio_read(struct insn *insn, enum i= nsn_mmio_type mmio, int siz =20 static int handle_mmio(struct pt_regs *regs, struct ve_info *ve) { - char buffer[MAX_INSN_SIZE]; enum insn_mmio_type mmio; struct insn insn =3D {}; unsigned long vaddr; int size, ret; =20 - /* Only in-kernel MMIO is supported */ - if (WARN_ON_ONCE(user_mode(regs))) - return -EFAULT; - - if (copy_from_kernel_nofault(buffer, (void *)regs->ip, MAX_INSN_SIZE)) - return -EFAULT; - - if (insn_decode(&insn, buffer, MAX_INSN_SIZE, INSN_MODE_64)) - return -EINVAL; + ret =3D decode_insn_struct(&insn, regs); + if (ret) + return ret; =20 mmio =3D insn_decode_mmio(&insn, &size); if (WARN_ON_ONCE(mmio =3D=3D INSN_MMIO_DECODE_FAILED)) return -EINVAL; =20 - if (!fault_in_kernel_space(ve->gla)) { + if (!user_mode(regs) && !fault_in_kernel_space(ve->gla)) { WARN_ONCE(1, "Access to userspace address is not supported"); return -EINVAL; } @@ -783,6 +802,10 @@ static int virt_exception_user(struct pt_regs *regs, s= truct ve_info *ve) switch (ve->exit_reason) { case EXIT_REASON_CPUID: return handle_cpuid(regs, ve); + case EXIT_REASON_EPT_VIOLATION: + if (is_private_gpa(ve->gpa)) + panic("Unexpected EPT-violation on private memory."); + return handle_mmio(regs, ve); default: pr_warn("Unexpected #VE: %lld\n", ve->exit_reason); return -EIO; --=20 2.46.0