From mboxrd@z Thu Jan 1 00:00:00 1970 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 A4E091BDAA7; Fri, 16 Aug 2024 13:45:04 +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=1723815904; cv=none; b=TfxArKuFyUWYt7cFCIu3oY8ekDfGpM6G0PoiEOc/BIPKvDhPUTdBu+OP08KYZtwkFEwDqjG2KPJdQ9f8IukKMrQyjV26x3xB73oLpwWIYlk4f9Xs2oYgyMW3IBI8r14FIVaWzB05+TJO99t5wBJOaz2Q+Pt87cSu2WkSMnFJD3M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723815904; c=relaxed/simple; bh=hvzqcn28E3scaeuRjxnr8MAC0MN2MjYYCtSOK+Ib1v8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J7ikNMXbOdfu54SVm8wcLiOmPUHMlCJrc9rzIFdkIsxOnT33FgSb/iilWE0W7uRHhyGOIo57DDDoAb0lecif3SslE1jp2VKnXXi1VSagB6r6/fl9dX3A6v66ldNBLwOAIf0crSo58eQRr4bm5QG2IJJrAQ8ePWQr6WRwB8pcKK8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TNyuC3l2; 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="TNyuC3l2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 597EAC4AF09; Fri, 16 Aug 2024 13:45:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723815904; bh=hvzqcn28E3scaeuRjxnr8MAC0MN2MjYYCtSOK+Ib1v8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TNyuC3l2wggg08ER39I7Ih7rSNrN4swhgxoL6SQIBJ2EKR79Si3SAyQXvEmPiqTtp TXY9+66YpZcw20hUahSdhswKPjkCy5XJhWcld1TD2dQyyuiNnKnjs1GPp29TCL9Kg2 UgyysDM0R9CDaouZtM6o5oMmY+qdKJGAcOuCAYkn5ZRqP+MS7dC0OCNaf/FqnwtiVN +MEXuh+P9NrKSLRC4f1f+o79zMcfP6M0fH378PEx+NY27zJuC43swqc6Qb+gRR8Zym uKfyMGvdWB3SoOMRI3Q4Y2GlmqYx0FdbTlCUiW0YJVZXPKdzOqJ+twbww8lfk7INrb QDeKZLK7CkWFw== 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 v3 03/10] x86/tdx: Allow MMIO from userspace Date: Fri, 16 Aug 2024 15:43:53 +0200 Message-ID: <2a79d86c268d934644b8e4a5a8c59b4699fa0015.1723807851.git.legion@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. So far, no additional checks have been made. Signed-off-by: Alexey Gladkov (Intel) --- arch/x86/coco/tdx/tdx.c | 43 +++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 86c22fec97fb..254d5293d25a 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -484,6 +484,32 @@ static int valid_vaddr(struct ve_info *ve, enum insn_mmio_type mmio, int size, return 0; } +static int decode_insn_struct(struct insn *insn, struct pt_regs *regs) +{ + char buffer[MAX_INSN_SIZE]; + + if (user_mode(regs)) { + int nr_copied = insn_fetch_from_user(regs, buffer); + + if (nr_copied <= 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) { @@ -564,21 +590,14 @@ static int handle_mmio_read(struct insn *insn, enum insn_mmio_type mmio, int siz 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 = {}; unsigned long vaddr; int size, ret; - /* 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 = decode_insn_struct(&insn, regs); + if (ret) + return ret; mmio = insn_decode_mmio(&insn, &size); if (WARN_ON_ONCE(mmio == INSN_MMIO_DECODE_FAILED)) @@ -774,6 +793,10 @@ static int virt_exception_user(struct pt_regs *regs, struct 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; -- 2.45.2