All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] x86/realmode: use a dummy trampoline for Xen PV guests
@ 2022-11-22 16:38 Juergen Gross
  2022-11-22 16:38 ` [PATCH v2 1/3] x86/realmode: test real_mode_header outside of real_mode_size_needed() Juergen Gross
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Juergen Gross @ 2022-11-22 16:38 UTC (permalink / raw)
  To: linux-kernel, x86, linux-efi, platform-driver-x86
  Cc: Juergen Gross, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin, Ard Biesheuvel, Darren Hart,
	Andy Shevchenko, Boris Ostrovsky, xen-devel

A Xen PV guests can't run in realmode, so the realmode trampoline can
omitted.

Changes in V2:
- complete new approach

Juergen Gross (3):
  x86/realmode: test real_mode_header outside of real_mode_size_needed()
  x86/realmode: add trampoline reference structure
  x86/xen: add a dummy trampoline for Xen PV guests

 arch/x86/include/asm/realmode.h | 15 +++++++++++----
 arch/x86/platform/efi/quirks.c  |  3 ++-
 arch/x86/realmode/init.c        | 25 ++++++++++++++++++++-----
 arch/x86/xen/enlighten_pv.c     | 17 +++++++++++++++++
 4 files changed, 50 insertions(+), 10 deletions(-)

-- 
2.35.3


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/3] x86/realmode: test real_mode_header outside of real_mode_size_needed()
  2022-11-22 16:38 [PATCH v2 0/3] x86/realmode: use a dummy trampoline for Xen PV guests Juergen Gross
@ 2022-11-22 16:38 ` Juergen Gross
  2022-11-22 16:38 ` [PATCH v2 2/3] x86/realmode: add trampoline reference structure Juergen Gross
  2022-11-22 16:38 ` [PATCH v2 3/3] x86/xen: add a dummy trampoline for Xen PV guests Juergen Gross
  2 siblings, 0 replies; 5+ messages in thread
From: Juergen Gross @ 2022-11-22 16:38 UTC (permalink / raw)
  To: linux-kernel, x86, linux-efi, platform-driver-x86
  Cc: Juergen Gross, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin, Ard Biesheuvel, Darren Hart,
	Andy Shevchenko

Move the test for the realmode trampoline memory having been allocated
already to the callers of real_mode_size_needed(). This allows to use
that function in setup_real_mode() and set_real_mode_permissions(),
too.

While at it change the size calculation to use PAGE_ALIGN() instead of
open coding it.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 arch/x86/include/asm/realmode.h | 5 +----
 arch/x86/platform/efi/quirks.c  | 3 ++-
 arch/x86/realmode/init.c        | 6 +++---
 3 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/arch/x86/include/asm/realmode.h b/arch/x86/include/asm/realmode.h
index fd6f6e5b755a..1eb3d4232e81 100644
--- a/arch/x86/include/asm/realmode.h
+++ b/arch/x86/include/asm/realmode.h
@@ -78,10 +78,7 @@ extern unsigned char secondary_startup_64_no_verify[];
 
 static inline size_t real_mode_size_needed(void)
 {
-	if (real_mode_header)
-		return 0;	/* already allocated. */
-
-	return ALIGN(real_mode_blob_end - real_mode_blob, PAGE_SIZE);
+	return PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
 }
 
 static inline void set_real_mode_mem(phys_addr_t mem)
diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c
index b0b848d6933a..7c18ca720eee 100644
--- a/arch/x86/platform/efi/quirks.c
+++ b/arch/x86/platform/efi/quirks.c
@@ -445,7 +445,8 @@ void __init efi_free_boot_services(void)
 		 * panicking early.)
 		 */
 		rm_size = real_mode_size_needed();
-		if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
+		if (!real_mode_header && rm_size &&
+		    (start + rm_size) < (1<<20) && size >= rm_size) {
 			set_real_mode_mem(start);
 			start += rm_size;
 			size -= rm_size;
diff --git a/arch/x86/realmode/init.c b/arch/x86/realmode/init.c
index 41d7669a97ad..37a3658efaa0 100644
--- a/arch/x86/realmode/init.c
+++ b/arch/x86/realmode/init.c
@@ -48,7 +48,7 @@ void __init reserve_real_mode(void)
 	phys_addr_t mem;
 	size_t size = real_mode_size_needed();
 
-	if (!size)
+	if (real_mode_header || !size)
 		return;
 
 	WARN_ON(slab_is_available());
@@ -94,7 +94,7 @@ static void __init setup_real_mode(void)
 	unsigned char *base;
 	unsigned long phys_base;
 	struct trampoline_header *trampoline_header;
-	size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
+	size_t size = real_mode_size_needed();
 #ifdef CONFIG_X86_64
 	u64 *trampoline_pgd;
 	u64 efer;
@@ -182,7 +182,7 @@ static void __init setup_real_mode(void)
 static void __init set_real_mode_permissions(void)
 {
 	unsigned char *base = (unsigned char *) real_mode_header;
-	size_t size = PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
+	size_t size = real_mode_size_needed();
 
 	size_t ro_size =
 		PAGE_ALIGN(real_mode_header->ro_end) -
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/3] x86/realmode: add trampoline reference structure
  2022-11-22 16:38 [PATCH v2 0/3] x86/realmode: use a dummy trampoline for Xen PV guests Juergen Gross
  2022-11-22 16:38 ` [PATCH v2 1/3] x86/realmode: test real_mode_header outside of real_mode_size_needed() Juergen Gross
@ 2022-11-22 16:38 ` Juergen Gross
  2022-11-22 16:38 ` [PATCH v2 3/3] x86/xen: add a dummy trampoline for Xen PV guests Juergen Gross
  2 siblings, 0 replies; 5+ messages in thread
From: Juergen Gross @ 2022-11-22 16:38 UTC (permalink / raw)
  To: linux-kernel, x86
  Cc: Juergen Gross, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin

Add a new struct trampoline_ref containing the relevant pointers for
handling the realmode trampoline. Use a central pointer for accessing
this new structure in preparation of adding support of replacing it
with a dummy trampoline.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 arch/x86/include/asm/realmode.h | 12 +++++++++++-
 arch/x86/realmode/init.c        | 13 +++++++++++--
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/arch/x86/include/asm/realmode.h b/arch/x86/include/asm/realmode.h
index 1eb3d4232e81..778d6cb1db6c 100644
--- a/arch/x86/include/asm/realmode.h
+++ b/arch/x86/include/asm/realmode.h
@@ -14,6 +14,13 @@
 #include <linux/types.h>
 #include <asm/io.h>
 
+/* Pointers for installing the realmode trampoline. */
+struct trampoline_ref {
+	unsigned char *blob;
+	unsigned char *blob_end;
+	unsigned char *relocs;
+};
+
 /* This must match data at realmode/rm/header.S */
 struct real_mode_header {
 	u32	text_start;
@@ -56,6 +63,8 @@ struct trampoline_header {
 };
 
 extern struct real_mode_header *real_mode_header;
+extern struct trampoline_ref *real_mode_trampoline __initdata;
+
 extern unsigned char real_mode_blob_end[];
 
 extern unsigned long initial_code;
@@ -78,7 +87,8 @@ extern unsigned char secondary_startup_64_no_verify[];
 
 static inline size_t real_mode_size_needed(void)
 {
-	return PAGE_ALIGN(real_mode_blob_end - real_mode_blob);
+	return PAGE_ALIGN(real_mode_trampoline->blob_end -
+			  real_mode_trampoline->blob);
 }
 
 static inline void set_real_mode_mem(phys_addr_t mem)
diff --git a/arch/x86/realmode/init.c b/arch/x86/realmode/init.c
index 37a3658efaa0..5a670a9ed2f7 100644
--- a/arch/x86/realmode/init.c
+++ b/arch/x86/realmode/init.c
@@ -12,6 +12,15 @@
 #include <asm/sev.h>
 
 struct real_mode_header *real_mode_header;
+
+static __initdata struct trampoline_ref realmode_trampoline = {
+	.blob = real_mode_blob,
+	.blob_end = real_mode_blob_end,
+	.relocs = real_mode_relocs,
+};
+
+struct trampoline_ref *real_mode_trampoline __initdata = &realmode_trampoline;
+
 u32 *trampoline_cr4_features;
 
 /* Hold the pgd entry used on booting additional CPUs */
@@ -111,12 +120,12 @@ static void __init setup_real_mode(void)
 	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
 		set_memory_decrypted((unsigned long)base, size >> PAGE_SHIFT);
 
-	memcpy(base, real_mode_blob, size);
+	memcpy(base, real_mode_trampoline->blob, size);
 
 	phys_base = __pa(base);
 	real_mode_seg = phys_base >> 4;
 
-	rel = (u32 *) real_mode_relocs;
+	rel = (u32 *) real_mode_trampoline->relocs;
 
 	/* 16-bit segment relocations. */
 	count = *rel++;
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 3/3] x86/xen: add a dummy trampoline for Xen PV guests
  2022-11-22 16:38 [PATCH v2 0/3] x86/realmode: use a dummy trampoline for Xen PV guests Juergen Gross
  2022-11-22 16:38 ` [PATCH v2 1/3] x86/realmode: test real_mode_header outside of real_mode_size_needed() Juergen Gross
  2022-11-22 16:38 ` [PATCH v2 2/3] x86/realmode: add trampoline reference structure Juergen Gross
@ 2022-11-22 16:38 ` Juergen Gross
  2022-11-23  9:37   ` Jan Beulich
  2 siblings, 1 reply; 5+ messages in thread
From: Juergen Gross @ 2022-11-22 16:38 UTC (permalink / raw)
  To: linux-kernel, x86
  Cc: Juergen Gross, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin, Boris Ostrovsky, xen-devel

When running as a Xen PV guest there is no need for setting up the
realmode trampoline, as realmode isn't supported in this environment.

Trying to setup the trampoline has been proven to be problematic in
some cases, especially when trying to debug early boot problems with
Xen requiring to keep the EFI boot-services memory mapped (some
firmware variants seem to claim basically all memory below 1M for boot
services).

Setup a dummy trampoline in order to make init_real_mode() happy.
In order to avoid too tight coupling between the Xen PV specific code
and the trampoline handling, modify the trampoline handling to do
nothing if the detected trampoline size is 0.

Fixes: 084ee1c641a0 ("x86, realmode: Relocator for realmode code")
Signed-off-by: Juergen Gross <jgross@suse.com>
---
 arch/x86/realmode/init.c    |  6 ++++++
 arch/x86/xen/enlighten_pv.c | 17 +++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/arch/x86/realmode/init.c b/arch/x86/realmode/init.c
index 5a670a9ed2f7..538328ce5ff5 100644
--- a/arch/x86/realmode/init.c
+++ b/arch/x86/realmode/init.c
@@ -110,6 +110,9 @@ static void __init setup_real_mode(void)
 	int i;
 #endif
 
+	if (!size)
+		return;
+
 	base = (unsigned char *)real_mode_header;
 
 	/*
@@ -204,6 +207,9 @@ static void __init set_real_mode_permissions(void)
 	unsigned long text_start =
 		(unsigned long) __va(real_mode_header->text_start);
 
+	if (!size)
+		return;
+
 	set_memory_nx((unsigned long) base, size >> PAGE_SHIFT);
 	set_memory_ro((unsigned long) base, ro_size >> PAGE_SHIFT);
 	set_memory_x((unsigned long) text_start, text_size >> PAGE_SHIFT);
diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
index 038da45f057a..a6194a6a6806 100644
--- a/arch/x86/xen/enlighten_pv.c
+++ b/arch/x86/xen/enlighten_pv.c
@@ -71,6 +71,7 @@
 #include <asm/mwait.h>
 #include <asm/pci_x86.h>
 #include <asm/cpu.h>
+#include <asm/realmode.h>
 #ifdef CONFIG_X86_IOPL_IOPERM
 #include <asm/io_bitmap.h>
 #endif
@@ -137,12 +138,28 @@ static void __init xen_pv_init_platform(void)
 	xen_init_time_ops();
 }
 
+static struct real_mode_header xen_rm_header;
+
+static __initdata struct trampoline_ref xen_dummy_trampoline = {
+	.blob = (unsigned char *)&xen_rm_header,
+	.blob_end = (unsigned char *)&xen_rm_header,
+};
+
+/* Setup dummy trampoline of size 0 with no relocations. */
+static void __init xen_setup_trampoline(void)
+{
+	real_mode_header = &xen_rm_header;
+	real_mode_trampoline = &xen_dummy_trampoline;
+}
+
 static void __init xen_pv_guest_late_init(void)
 {
 #ifndef CONFIG_SMP
 	/* Setup shared vcpu info for non-smp configurations */
 	xen_setup_vcpu_info_placement();
 #endif
+
+	xen_setup_trampoline();
 }
 
 static __read_mostly unsigned int cpuid_leaf5_ecx_val;
-- 
2.35.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 3/3] x86/xen: add a dummy trampoline for Xen PV guests
  2022-11-22 16:38 ` [PATCH v2 3/3] x86/xen: add a dummy trampoline for Xen PV guests Juergen Gross
@ 2022-11-23  9:37   ` Jan Beulich
  0 siblings, 0 replies; 5+ messages in thread
From: Jan Beulich @ 2022-11-23  9:37 UTC (permalink / raw)
  To: Juergen Gross
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H. Peter Anvin, Boris Ostrovsky, xen-devel, linux-kernel, x86

On 22.11.2022 17:38, Juergen Gross wrote:
> @@ -137,12 +138,28 @@ static void __init xen_pv_init_platform(void)
>  	xen_init_time_ops();
>  }
>  
> +static struct real_mode_header xen_rm_header;
> +
> +static __initdata struct trampoline_ref xen_dummy_trampoline = {
> +	.blob = (unsigned char *)&xen_rm_header,
> +	.blob_end = (unsigned char *)&xen_rm_header,

With both pointing to the start of the struct, doesn't it suffice
for the above to be xen_rm_header[0]? At which point there'd be no
reason to wonder whether that struct could live in .init.data ...

Jan

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-11-23  9:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-22 16:38 [PATCH v2 0/3] x86/realmode: use a dummy trampoline for Xen PV guests Juergen Gross
2022-11-22 16:38 ` [PATCH v2 1/3] x86/realmode: test real_mode_header outside of real_mode_size_needed() Juergen Gross
2022-11-22 16:38 ` [PATCH v2 2/3] x86/realmode: add trampoline reference structure Juergen Gross
2022-11-22 16:38 ` [PATCH v2 3/3] x86/xen: add a dummy trampoline for Xen PV guests Juergen Gross
2022-11-23  9:37   ` Jan Beulich

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.