All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
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" <kirill.shutemov@linux.intel.com>
Subject: [PATCH 10/26] x86/tdx: Support TDX guest port I/O at decompression time
Date: Tue, 14 Dec 2021 18:02:48 +0300	[thread overview]
Message-ID: <20211214150304.62613-11-kirill.shutemov@linux.intel.com> (raw)
In-Reply-To: <20211214150304.62613-1-kirill.shutemov@linux.intel.com>

From: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>

Port IO triggers a #VE exception in TDX guests.  During normal runtime,
the kernel will handle those exceptions for any port IO.

But for the early code in the decompressor, #VE cannot be used because
the IDT needed for handling the exception is not set up.

Replace IN/OUT instructions with TDX IO hypercalls by defining helper
macros __in/__out and by re-defining them in the decompressor code.
Also, since TDX IO hypercall requires an IO size parameter, allow
__in/__out macros to accept size as an input parameter.

Reviewed-by: Andi Kleen <ak@linux.intel.com>
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
---
 arch/x86/boot/compressed/Makefile |  1 +
 arch/x86/boot/compressed/misc.h   |  8 ++++--
 arch/x86/boot/compressed/tdcall.S |  3 +++
 arch/x86/boot/compressed/tdx.h    | 44 +++++++++++++++++++++++++++++++
 arch/x86/include/asm/io.h         | 16 ++++++++---
 5 files changed, 66 insertions(+), 6 deletions(-)
 create mode 100644 arch/x86/boot/compressed/tdcall.S

diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
index 22a2a6cc2ab4..1bfe30ebadbe 100644
--- a/arch/x86/boot/compressed/Makefile
+++ b/arch/x86/boot/compressed/Makefile
@@ -99,6 +99,7 @@ endif
 
 vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o
 vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o
+vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdcall.o
 
 vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o
 efi-obj-$(CONFIG_EFI_STUB) = $(objtree)/drivers/firmware/efi/libstub/lib.a
diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h
index 0d8e275a9d96..6502adf71a2f 100644
--- a/arch/x86/boot/compressed/misc.h
+++ b/arch/x86/boot/compressed/misc.h
@@ -19,6 +19,12 @@
 /* cpu_feature_enabled() cannot be used this early */
 #define USE_EARLY_PGTABLE_L5
 
+/*
+ * Redefine __in/__out macros via tdx.h before including
+ * linux/io.h.
+ */
+#include "tdx.h"
+
 #include <linux/linkage.h>
 #include <linux/screen_info.h>
 #include <linux/elf.h>
@@ -28,8 +34,6 @@
 #include <asm/bootparam.h>
 #include <asm/desc_defs.h>
 
-#include "tdx.h"
-
 #define BOOT_CTYPE_H
 #include <linux/acpi.h>
 
diff --git a/arch/x86/boot/compressed/tdcall.S b/arch/x86/boot/compressed/tdcall.S
new file mode 100644
index 000000000000..aafadc136c88
--- /dev/null
+++ b/arch/x86/boot/compressed/tdcall.S
@@ -0,0 +1,3 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include "../../kernel/tdcall.S"
diff --git a/arch/x86/boot/compressed/tdx.h b/arch/x86/boot/compressed/tdx.h
index 18970c09512e..5d39608a2af4 100644
--- a/arch/x86/boot/compressed/tdx.h
+++ b/arch/x86/boot/compressed/tdx.h
@@ -6,8 +6,52 @@
 #include <linux/types.h>
 
 #ifdef CONFIG_INTEL_TDX_GUEST
+
+#include <vdso/limits.h>
+#include <uapi/asm/vmx.h>
+#include <asm/tdx.h>
+
 void early_tdx_detect(void);
 bool early_is_tdx_guest(void);
+
+static inline unsigned int tdx_io_in(int size, int port)
+{
+	struct tdx_hypercall_output out;
+
+	__tdx_hypercall(TDX_HYPERCALL_STANDARD, EXIT_REASON_IO_INSTRUCTION,
+			size, 0, port, 0, &out);
+
+	return out.r10 ? UINT_MAX : out.r11;
+}
+
+static inline void tdx_io_out(int size, int port, u64 value)
+{
+	struct tdx_hypercall_output out;
+
+	__tdx_hypercall(TDX_HYPERCALL_STANDARD, EXIT_REASON_IO_INSTRUCTION,
+			size, 1, port, value, &out);
+}
+
+#define __out(bwl, bw, sz)						\
+do {									\
+	if (early_is_tdx_guest()) {					\
+		tdx_io_out(sz, port, value);				\
+	} else {							\
+		asm volatile("out" #bwl " %" #bw "0, %w1" : :		\
+				"a"(value), "Nd"(port));		\
+	}								\
+} while (0)
+
+#define __in(bwl, bw, sz)						\
+do {									\
+	if (early_is_tdx_guest()) {					\
+		value = tdx_io_in(sz, port);				\
+	} else {							\
+		asm volatile("in" #bwl " %w1, %" #bw "0" :		\
+				"=a"(value) : "Nd"(port));		\
+	}								\
+} while (0)
+
 #else
 static inline void early_tdx_detect(void) { };
 static inline bool early_is_tdx_guest(void) { return false; }
diff --git a/arch/x86/include/asm/io.h b/arch/x86/include/asm/io.h
index f6d91ecb8026..f5bb8972b4b2 100644
--- a/arch/x86/include/asm/io.h
+++ b/arch/x86/include/asm/io.h
@@ -257,18 +257,26 @@ static inline void slow_down_io(void)
 
 #endif
 
+#ifndef __out
+#define __out(bwl, bw, sz)						\
+	asm volatile("out" #bwl " %" #bw "0, %w1" : : "a"(value), "Nd"(port))
+#endif
+
+#ifndef __in
+#define __in(bwl, bw, sz)						\
+	asm volatile("in" #bwl " %w1, %" #bw "0" : "=a"(value) : "Nd"(port))
+#endif
+
 #define BUILDIO(bwl, bw, type)						\
 static inline void out##bwl(unsigned type value, int port)		\
 {									\
-	asm volatile("out" #bwl " %" #bw "0, %w1"			\
-		     : : "a"(value), "Nd"(port));			\
+	__out(bwl, bw, sizeof(type));					\
 }									\
 									\
 static inline unsigned type in##bwl(int port)				\
 {									\
 	unsigned type value;						\
-	asm volatile("in" #bwl " %w1, %" #bw "0"			\
-		     : "=a"(value) : "Nd"(port));			\
+	__in(bwl, bw, sizeof(type));					\
 	return value;							\
 }									\
 									\
-- 
2.32.0


  parent reply	other threads:[~2021-12-14 15:04 UTC|newest]

Thread overview: 106+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-14 15:02 [PATCH 00/26] TDX Guest: TDX core support Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 01/26] x86/tdx: Detect running as a TDX guest in early boot Kirill A. Shutemov
2021-12-14 18:18   ` Borislav Petkov
2021-12-14 20:21     ` Kirill A. Shutemov
2021-12-14 20:58       ` Borislav Petkov
2021-12-14 15:02 ` [PATCH 02/26] x86/tdx: Extend the cc_platform_has() API to support TDX guests Kirill A. Shutemov
2021-12-15 23:19   ` Josh Poimboeuf
2021-12-15 23:35     ` Kirill A. Shutemov
2021-12-15 23:37       ` Josh Poimboeuf
2021-12-16 18:33   ` Borislav Petkov
2021-12-14 15:02 ` [PATCH 03/26] x86/tdx: Add __tdx_module_call() and __tdx_hypercall() helper functions Kirill A. Shutemov
2021-12-21 19:11   ` Borislav Petkov
2021-12-23 16:55     ` Kirill A. Shutemov
2021-12-23 18:53       ` Borislav Petkov
2021-12-24  9:16       ` Paolo Bonzini
2021-12-24 10:34         ` Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 04/26] x86/traps: Add #VE support for TDX guest Kirill A. Shutemov
2021-12-23 19:45   ` Borislav Petkov
2021-12-28 23:31     ` Kirill A. Shutemov
2021-12-29 11:29       ` Borislav Petkov
2021-12-29 17:07         ` Sean Christopherson
2021-12-29 17:35           ` Borislav Petkov
2021-12-29 17:47             ` Sean Christopherson
2021-12-30  8:05         ` Kirill A. Shutemov
2021-12-30 10:53           ` Borislav Petkov
2021-12-30 15:41             ` Kirill A. Shutemov
2021-12-30 18:02               ` Borislav Petkov
2021-12-29 18:42       ` Dave Hansen
2021-12-14 15:02 ` [PATCH 05/26] x86/tdx: Add HLT support for TDX guests (#VE approach) Kirill A. Shutemov
2021-12-28 19:08   ` Borislav Petkov
2021-12-14 15:02 ` [PATCH 06/26] x86/tdx: Add MSR support for TDX guests Kirill A. Shutemov
2021-12-29 11:59   ` Borislav Petkov
2021-12-14 15:02 ` [PATCH 07/26] x86/tdx: Handle CPUID via #VE Kirill A. Shutemov
2021-12-31 17:19   ` Borislav Petkov
2021-12-14 15:02 ` [PATCH 08/26] x86/tdx: Handle in-kernel MMIO Kirill A. Shutemov
2021-12-15 23:31   ` Josh Poimboeuf
2021-12-15 23:37     ` Kirill A. Shutemov
2022-01-06 15:08     ` Kirill A. Shutemov
2022-01-05 10:37   ` Borislav Petkov
2022-01-05 15:43     ` Kirill A. Shutemov
2022-01-07 13:46       ` Borislav Petkov
2022-01-07 17:49         ` Kirill A. Shutemov
2022-01-07 19:04           ` Borislav Petkov
2021-12-14 15:02 ` [PATCH 09/26] x86/tdx: Detect TDX at early kernel decompression time Kirill A. Shutemov
2022-01-07 16:27   ` Borislav Petkov
2021-12-14 15:02 ` Kirill A. Shutemov [this message]
2022-01-13 13:51   ` [PATCH 10/26] x86/tdx: Support TDX guest port I/O at " Borislav Petkov
2022-01-15  1:01     ` Kirill A. Shutemov
2022-01-15 12:16       ` Borislav Petkov
2022-01-17 14:39         ` Kirill A. Shutemov
2022-01-17 18:32           ` Borislav Petkov
2022-01-19 11:53             ` Kirill A. Shutemov
2022-01-19 13:35               ` Borislav Petkov
2022-01-19 15:49                 ` Kirill A. Shutemov
2022-01-19 19:46                   ` Borislav Petkov
2022-01-19 20:08                     ` Kirill A. Shutemov
2022-01-19 20:26                       ` Borislav Petkov
2022-01-20  2:15                         ` [PATCH 1/3] x86: Consolidate port I/O helpers Kirill A. Shutemov
2022-01-20  2:15                           ` [PATCH 2/3] x86/boot: Allow to hook up alternative " Kirill A. Shutemov
2022-01-20 16:38                             ` Kirill A. Shutemov
2022-01-20 21:13                               ` Josh Poimboeuf
2022-01-20 22:19                                 ` Borislav Petkov
2022-01-20  2:15                           ` [PATCH 3/3] x86/boot/compressed: Support TDX guest port I/O at decompression time Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 11/26] x86/tdx: Add port I/O emulation Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 12/26] x86/tdx: Early boot handling of port I/O Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 13/26] x86/boot: Add a trampoline for booting APs via firmware handoff Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 14/26] x86/acpi, x86/boot: Add multiprocessor wake-up support Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 15/26] x86/boot: Avoid #VE during boot for TDX platforms Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 16/26] x86/topology: Disable CPU online/offline control for TDX guests Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 17/26] x86/tdx: Get page shared bit info from the TDX Module Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 18/26] x86/tdx: Exclude shared bit from __PHYSICAL_MASK Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 19/26] x86/tdx: Make pages shared in ioremap() Kirill A. Shutemov
2021-12-22 17:26   ` Tom Lendacky
2021-12-23 17:15     ` Kirill A. Shutemov
2021-12-23 19:45       ` Dave Hansen
2021-12-23 19:53         ` Borislav Petkov
2021-12-23 20:56           ` Kirill A. Shutemov
2021-12-23 21:09             ` Borislav Petkov
2021-12-24 11:03               ` Kirill A. Shutemov
2021-12-27 11:51                 ` Borislav Petkov
2021-12-27 14:14                   ` Kirill A. Shutemov
2021-12-28 18:39                     ` Borislav Petkov
2021-12-28 23:33                       ` Kirill A. Shutemov
2021-12-27 15:07                 ` Tom Lendacky
2022-01-03 14:17                   ` Kirill A. Shutemov
2022-01-03 14:29                     ` Borislav Petkov
2022-01-03 15:15                       ` Kirill A. Shutemov
2022-01-03 16:50                         ` Dave Hansen
2022-01-03 18:10                           ` Kirill A. Shutemov
2022-01-04 19:14                             ` Kirill A. Shutemov
2022-01-04 20:36                               ` Dave Hansen
2022-01-05  0:31                                 ` Kirill A. Shutemov
2022-01-05  0:43                                   ` Dave Hansen
2022-01-05  0:57                                     ` Kirill A. Shutemov
2022-01-05  1:02                                       ` Kirill A. Shutemov
2022-01-05  1:38                                       ` Dave Hansen
2022-01-05  9:46                                         ` Kirill A. Shutemov
2022-01-05 14:16                                     ` Tom Lendacky
2022-01-05 16:02                                       ` Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 20/26] x86/tdx: Add helper to convert memory between shared and private Kirill A. Shutemov
2021-12-14 15:02 ` [PATCH 21/26] x86/mm/cpa: Add support for TDX shared memory Kirill A. Shutemov
2021-12-14 15:03 ` [PATCH 22/26] x86/kvm: Use bounce buffers for TD guest Kirill A. Shutemov
2021-12-14 15:03 ` [PATCH 23/26] x86/tdx: ioapic: Add shared bit for IOAPIC base address Kirill A. Shutemov
2021-12-14 15:03 ` [PATCH 24/26] ACPICA: Avoid cache flush on TDX guest Kirill A. Shutemov
2021-12-14 15:03 ` [PATCH 25/26] x86/tdx: Warn about unexpected WBINVD Kirill A. Shutemov
2021-12-14 15:03 ` [PATCH 26/26] Documentation/x86: Document TDX kernel architecture Kirill A. Shutemov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211214150304.62613-11-kirill.shutemov@linux.intel.com \
    --to=kirill.shutemov@linux.intel.com \
    --cc=aarcange@redhat.com \
    --cc=ak@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=dan.j.williams@intel.com \
    --cc=dave.hansen@intel.com \
    --cc=david@redhat.com \
    --cc=hpa@zytor.com \
    --cc=jgross@suse.com \
    --cc=jmattson@google.com \
    --cc=joro@8bytes.org \
    --cc=jpoimboe@redhat.com \
    --cc=knsathya@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sathyanarayanan.kuppuswamy@linux.intel.com \
    --cc=sdeep@vmware.com \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --cc=tony.luck@intel.com \
    --cc=vkuznets@redhat.com \
    --cc=wanpengli@tencent.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.