Skip to contentSkip to conversation

efi/x86: Avoid legacy decompressor during EFI boot

PATCH Superseded Ard Biesheuvel 5 participants 26 messages Apr 24, 2023 – May 3, 2023

Subsystems (2)

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Ard Biesheuvel

author maintainer
#0 lore raw

cover efi/x86: Avoid legacy decompressor during EFI boot

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Cc Evgeniy Baskov <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Alexey Khoroshilov <[email protected]>
Cc Peter Jones <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Dave Young <[email protected]>
Cc Mario Limonciello <[email protected]>
Cc Kees Cook <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Linus Torvalds <[email protected]>

[0] https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/

Ard Biesheuvel (6):
x86: decompressor: Move global symbol references to C code
x86: decompressor: Factor out kernel decompression and relocation
x86: efistub: Obtain ACPI RSDP address while running in the stub
x86: efistub: Perform 4/5 level paging switch from the stub
x86: efistub: Prefer EFI memory attributes protocol over DXE services
x86: efistub: Avoid legacy decompressor when doing EFI boot

 arch/x86/boot/compressed/efi_mixed.S           |  55 ---
 arch/x86/boot/compressed/head_32.S             |  24 --
 arch/x86/boot/compressed/head_64.S             |  39 +--
 arch/x86/boot/compressed/misc.c                |  44 ++-
 arch/x86/include/asm/efi.h                     |   2 +
 drivers/firmware/efi/libstub/efi-stub-helper.c |   4 +
 drivers/firmware/efi/libstub/x86-stub.c        | 360 +++++++++++++-------
 7 files changed, 279 insertions(+), 249 deletions(-)
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

PATCH 1/6 x86: decompressor: Move global symbol references to C code

We no longer have to be cautious when referring to global variables in the position independent decompressor code, now that we use PIE codegen and assert in the linker script that no GOT entries exist (which would require adjustment for the actual runtime load address of the decompressor binary).

This means we can simply refer to global variables directly, instead of passing them into C routines from asm code. Let's do so for the code that will be called directly from the EFI stub after a subsequent patch, and avoid the need to pass these quantities directly.

---
 arch/x86/boot/compressed/head_32.S |  8 --------
 arch/x86/boot/compressed/head_64.S |  8 +-------
 arch/x86/boot/compressed/misc.c    | 17 ++++++++++-------
 3 files changed, 11 insertions(+), 22 deletions(-)
arch/x86/boot/compressed/head_32.S +0 -8
index 987ae727cf9f0d04..3ecc1bbe971e1d43 100644
@@ -179,13 +179,7 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
  */
 	/* push arguments for extract_kernel: */
 
-	pushl	output_len@GOTOFF(%ebx)	/* decompressed length, end of relocs */
 	pushl	%ebp			/* output address */
-	pushl	input_len@GOTOFF(%ebx)	/* input_len */
-	leal	input_data@GOTOFF(%ebx), %eax
-	pushl	%eax			/* input_data */
-	leal	boot_heap@GOTOFF(%ebx), %eax
-	pushl	%eax			/* heap area */
 	pushl	%esi			/* real mode pointer */
 	call	extract_kernel		/* returns kernel entry point in %eax */
 	addl	$24, %esp
@@ -213,8 +207,6 @@ SYM_DATA_END_LABEL(gdt, SYM_L_LOCAL, gdt_end)
  */
 	.bss
 	.balign 4
-boot_heap:
-	.fill BOOT_HEAP_SIZE, 1, 0
 boot_stack:
 	.fill BOOT_STACK_SIZE, 1, 0
 boot_stack_end:
arch/x86/boot/compressed/head_64.S +1 -7
index 03c4328a88cbd5d0..5ab014be179c1103 100644
@@ -564,11 +564,7 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
  */
 	pushq	%rsi			/* Save the real mode argument */
 	movq	%rsi, %rdi		/* real mode address */
-	leaq	boot_heap(%rip), %rsi	/* malloc area for uncompression */
-	leaq	input_data(%rip), %rdx  /* input_data */
-	movl	input_len(%rip), %ecx	/* input_len */
-	movq	%rbp, %r8		/* output target address */
-	movl	output_len(%rip), %r9d	/* decompressed length, end of relocs */
+	movq	%rbp, %rsi		/* output target address */
 	call	extract_kernel		/* returns kernel entry point in %rax */
 	popq	%rsi
 
@@ -726,8 +722,6 @@ SYM_DATA_END_LABEL(boot_idt, SYM_L_GLOBAL, boot_idt_end)
  */
 	.bss
 	.balign 4
-SYM_DATA_LOCAL(boot_heap,	.fill BOOT_HEAP_SIZE, 1, 0)
-
 SYM_DATA_START_LOCAL(boot_stack)
 	.fill BOOT_STACK_SIZE, 1, 0
 	.balign 16
arch/x86/boot/compressed/misc.c +10 -7
index 014ff222bf4b35c2..3b79667412ceb388 100644
@@ -330,6 +330,11 @@ static size_t parse_elf(void *output)
 	return ehdr.e_entry - LOAD_PHYSICAL_ADDR;
 }
 
+static u8 boot_heap[BOOT_HEAP_SIZE] __aligned(4);
+
+extern unsigned char input_data[];
+extern unsigned int input_len, output_len;
+
 /*
  * The compressed kernel image (ZO), has been moved so that its position
  * is against the end of the buffer used to hold the uncompressed kernel
@@ -347,14 +352,12 @@ static size_t parse_elf(void *output)
  *             |-------uncompressed kernel image---------|
  *
  */
-asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
-				  unsigned char *input_data,
-				  unsigned long input_len,
-				  unsigned char *output,
-				  unsigned long output_len)
+asmlinkage __visible void *extract_kernel(void *rmode,
+				  unsigned char *output)
 {
 	const unsigned long kernel_total_size = VO__end - VO__text;
 	unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
+	memptr heap = (memptr)boot_heap;
 	unsigned long needed_size;
 	size_t entry_offset;
 
@@ -412,7 +415,7 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
 	 * entries. This ensures the full mapped area is usable RAM
 	 * and doesn't include any reserved areas.
 	 */
-	needed_size = max(output_len, kernel_total_size);
+	needed_size = max((unsigned long)output_len, kernel_total_size);
 #ifdef CONFIG_X86_64
 	needed_size = ALIGN(needed_size, MIN_KERNEL_ALIGN);
 #endif
@@ -443,7 +446,7 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,
 #ifdef CONFIG_X86_64
 	if (heap > 0x3fffffffffffUL)
 		error("Destination address too large");
-	if (virt_addr + max(output_len, kernel_total_size) > KERNEL_IMAGE_SIZE)
+	if (virt_addr + needed_size > KERNEL_IMAGE_SIZE)
 		error("Destination virtual address is beyond the kernel mapping area");
 #else
 	if (heap > ((-__PAGE_OFFSET-(128<<20)-1) & 0x7fffffff))
Signature · 1 line
2.39.2
author maintainer
#2 lore raw

PATCH 2/6 x86: decompressor: Factor out kernel decompression and relocation

In reply to Ard Biesheuvel

Factor out the decompressor sequence that invokes the decompressor, parses the ELF and applies the relocations so that we will be able to call it directly from the EFI stub.

---
 arch/x86/boot/compressed/misc.c | 27 ++++++++++++++++----
 1 file changed, 22 insertions(+), 5 deletions(-)
arch/x86/boot/compressed/misc.c +22 -5
index 3b79667412ceb388..0fa5df49ce14f196 100644
@@ -330,11 +330,32 @@ static size_t parse_elf(void *output)
 	return ehdr.e_entry - LOAD_PHYSICAL_ADDR;
 }
 
+const unsigned long kernel_total_size = VO__end - VO__text;
+
 static u8 boot_heap[BOOT_HEAP_SIZE] __aligned(4);
 
 extern unsigned char input_data[];
 extern unsigned int input_len, output_len;
 
+unsigned long decompress_kernel(unsigned char *outbuf, unsigned long virt_addr,
+				void (*error)(char *x))
+{
+	unsigned long entry;
+
+	if (!free_mem_ptr) {
+		free_mem_ptr     = (unsigned long)boot_heap;
+		free_mem_end_ptr = (unsigned long)boot_heap + sizeof(boot_heap);
+	}
+
+	__decompress(input_data, input_len, NULL, NULL, outbuf, output_len,
+		     NULL, error);
+
+	entry = parse_elf(outbuf);
+	handle_relocations(outbuf, output_len, virt_addr);
+
+	return entry;
+}
+
 /*
  * The compressed kernel image (ZO), has been moved so that its position
  * is against the end of the buffer used to hold the uncompressed kernel
@@ -355,7 +376,6 @@ extern unsigned int input_len, output_len;
 asmlinkage __visible void *extract_kernel(void *rmode,
 				  unsigned char *output)
 {
-	const unsigned long kernel_total_size = VO__end - VO__text;
 	unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
 	memptr heap = (memptr)boot_heap;
 	unsigned long needed_size;
@@ -458,10 +478,7 @@ asmlinkage __visible void *extract_kernel(void *rmode,
 #endif
 
 	debug_putstr("\nDecompressing Linux... ");
-	__decompress(input_data, input_len, NULL, NULL, output, output_len,
-			NULL, error);
-	entry_offset = parse_elf(output);
-	handle_relocations(output, output_len, virt_addr);
+	entry_offset = decompress_kernel(output, virt_addr, error);
 
 	debug_putstr("done.\nBooting the kernel (entry_offset: 0x");
 	debug_puthex(entry_offset);
Signature · 1 line
2.39.2
author maintainer
#3 lore raw

PATCH 3/6 x86: efistub: Obtain ACPI RSDP address while running in the stub

In reply to Ard Biesheuvel

One of the actions performed by the decompressor is populating the RSDP address field in the boot_params struct, and when doing EFI boot, EFI configuration tables are the preferred source for this information.

In preparation for removing the decompressor code from the EFI stub boot path, set this field from the EFI stub code.

---
 drivers/firmware/efi/libstub/x86-stub.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/firmware/efi/libstub/x86-stub.c +5 -0
index a0bfd31358ba97b1..e136c94037dda8d3 100644
@@ -787,6 +787,11 @@ asmlinkage unsigned long efi_main(efi_handle_t handle,
 		efi_dxe_table = NULL;
 	}
 
+	if (!boot_params->acpi_rsdp_addr)
+		boot_params->acpi_rsdp_addr = (unsigned long)
+				(get_efi_config_table(ACPI_20_TABLE_GUID) ?:
+				 get_efi_config_table(ACPI_TABLE_GUID));
+
 	/*
 	 * If the kernel isn't already loaded at a suitable address,
 	 * relocate it.
Signature · 1 line
2.39.2
author maintainer
#4 lore raw

PATCH 4/6 x86: efistub: Perform 4/5 level paging switch from the stub

In reply to Ard Biesheuvel

In preparation for updating the EFI stub boot flow to avoid the bare metal decompressor code altogether, implement the support code for switching between 4 and 5 levels of paging before jumping to the kernel proper.

---
 drivers/firmware/efi/libstub/efi-stub-helper.c |   4 +
 drivers/firmware/efi/libstub/x86-stub.c        | 145 ++++++++++++++++++++
 2 files changed, 149 insertions(+)
drivers/firmware/efi/libstub/efi-stub-helper.c +4 -0
index 1e0203d74691ffcc..fc5f3b4c45e91401 100644
@@ -16,6 +16,8 @@
 
 #include "efistub.h"
 
+extern bool efi_no5lvl;
+
 bool efi_nochunk;
 bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
 bool efi_novamap;
@@ -73,6 +75,8 @@ efi_status_t efi_parse_options(char const *cmdline)
 			efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
 		} else if (!strcmp(param, "noinitrd")) {
 			efi_noinitrd = true;
+		} else if (IS_ENABLED(CONFIG_X86_64) && !strcmp(param, "no5lvl")) {
+			efi_no5lvl = true;
 		} else if (!strcmp(param, "efi") && val) {
 			efi_nochunk = parse_option_str(val, "nochunk");
 			efi_novamap |= parse_option_str(val, "novamap");
drivers/firmware/efi/libstub/x86-stub.c +145 -0

163 lines not on this page · raw message

Signature · 1 line
2.39.2
#5 lore raw

Re: [PATCH 4/6] x86: efistub: Perform 4/5 level paging switch from the stub

Quoted 4 lines · Ard Biesheuvel

In preparation for updating the EFI stub boot flow to avoid the bare metal decompressor code altogether, implement the support code for switching between 4 and 5 levels of paging before jumping to the kernel proper.

I must admit it is neat. I like it a lot.

Any chance we can share the code with the traditional decompressor? There's not much that EFI specific here. It should be possible to isolate it from the rest, no?

Quoted 16 lines
drivers/firmware/efi/libstub/x86-stub.c +8 -0
@@ -792,6 +925,14 @@ asmlinkage unsigned long efi_main(efi_handle_t handle,
 				(get_efi_config_table(ACPI_20_TABLE_GUID) ?:
 				 get_efi_config_table(ACPI_TABLE_GUID));
 
+#ifdef CONFIG_X86_64
+	status = efi_setup_5level_paging();
+	if (status != EFI_SUCCESS) {
+		efi_err("efi_setup_5level_paging() failed!\n");
+		goto fail;
+	}
+#endif
+
 	/*
 	 * If the kernel isn't already loaded at a suitable address,
 	 * relocate it.

drivers/firmware/efi/libstub/x86-stub.c asmlinkage unsigned long efi_main(efi_handle_t handle, · line 1060

Quoted 10 lines
drivers/firmware/efi/libstub/x86-stub.c +4 -0
@@ -910,6 +1051,10 @@ asmlinkage unsigned long efi_main(efi_handle_t handle,
 		goto fail;
 	}
 
+#ifdef CONFIG_X86_64
+	efi_5level_switch();
+#endif
+
 	return bzimage_addr;
 fail:
 	efi_err("efi_main() failed!\n");

Maybe use IS_ENABLED() + dummy efi_setup_5level_paging()/efi_5level_switch() instead of #ifdefs?

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
author maintainer
#6 lore raw

Re: [PATCH 4/6] x86: efistub: Perform 4/5 level paging switch from the stub

Quoted 9 lines · Kirill A . Shutemov
Quoted 4 lines · Ard Biesheuvel

In preparation for updating the EFI stub boot flow to avoid the bare metal decompressor code altogether, implement the support code for switching between 4 and 5 levels of paging before jumping to the kernel proper.

I must admit it is neat. I like it a lot.

Thanks!

Quoted 4 lines

Any chance we can share the code with the traditional decompressor? There's not much that EFI specific here. It should be possible to isolate it from the rest, no?

I agree. The EFI boot code should still avoid the bare metal trampoline allocation/deallocation, but the actual payload could be the same - it's just an indirect call with the GDT and page table pointers as arguments.

Quoted 20 lines
Quoted 20 lines
drivers/firmware/efi/libstub/x86-stub.c +8 -0
@@ -792,6 +925,14 @@ asmlinkage unsigned long efi_main(efi_handle_t handle,
                              (get_efi_config_table(ACPI_20_TABLE_GUID) ?:
                               get_efi_config_table(ACPI_TABLE_GUID));
 
+#ifdef CONFIG_X86_64
+     status = efi_setup_5level_paging();
+     if (status != EFI_SUCCESS) {
+             efi_err("efi_setup_5level_paging() failed!\n");
+             goto fail;
+     }
+#endif
+
      /*
       * If the kernel isn't already loaded at a suitable address,
       * relocate it.
@@ -910,6 +1051,10 @@ asmlinkage unsigned long efi_main(efi_handle_t handle,
              goto fail;
      }
 
Quoted 11 lines

drivers/firmware/efi/libstub/x86-stub.c asmlinkage unsigned long efi_main(efi_handle_t handle, · line 1060

Quoted 7 lines
drivers/firmware/efi/libstub/x86-stub.c +4 -0
+#ifdef CONFIG_X86_64
+     efi_5level_switch();
+#endif
+
      return bzimage_addr;
 fail:
      efi_err("efi_main() failed!\n");

Maybe use IS_ENABLED() + dummy efi_setup_5level_paging()/efi_5level_switch() instead of #ifdefs?

These are functions returning void so I can just move the #ifdef into the function implementation. Wo do need #ifdefs at some level, as i386 does not provide a definition for __KERNEL32_CS

author maintainer
#7 lore raw

PATCH 5/6 x86: efistub: Prefer EFI memory attributes protocol over DXE services

In reply to Ard Biesheuvel

Currently, we rely on DXE services in some cases to clear non-execute restrictions from page allocations that need to be executable. This is dodgy, because DXE services are not specified by UEFI but by PI, and they are not intended for consumption by OS loaders. However, no alternative existed at the time.

Now, there is a new UEFI protocol that should be used instead, so if it exists, prefer it over the DXE services calls.

---
 drivers/firmware/efi/libstub/x86-stub.c | 28 ++++++++++++++------
 1 file changed, 20 insertions(+), 8 deletions(-)
drivers/firmware/efi/libstub/x86-stub.c +20 -8
index 7b8717cbb96a1246..ea4024a6a04e507f 100644
@@ -25,6 +25,7 @@ const efi_system_table_t *efi_system_table;
 const efi_dxe_services_table_t *efi_dxe_table;
 u32 image_offset __section(".data");
 static efi_loaded_image_t *image = NULL;
+static efi_memory_attribute_protocol_t *memattr;
 
 static efi_status_t
 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
@@ -221,12 +222,18 @@ adjust_memory_range_protection(unsigned long start, unsigned long size)
 	unsigned long rounded_start, rounded_end;
 	unsigned long unprotect_start, unprotect_size;
 
-	if (efi_dxe_table == NULL)
-		return;
-
 	rounded_start = rounddown(start, EFI_PAGE_SIZE);
 	rounded_end = roundup(start + size, EFI_PAGE_SIZE);
 
+	if (memattr != NULL) {
+		efi_call_proto(memattr, clear_memory_attributes, rounded_start,
+			       rounded_end - rounded_start, EFI_MEMORY_XP);
+		return;
+	}
+
+	if (efi_dxe_table == NULL)
+		return;
+
 	/*
 	 * Don't modify memory region attributes, they are
 	 * already suitable, to lower the possibility to
@@ -913,13 +920,18 @@ asmlinkage unsigned long efi_main(efi_handle_t handle,
 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
 		efi_exit(handle, EFI_INVALID_PARAMETER);
 
-	efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
-	if (efi_dxe_table &&
-	    efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
-		efi_warn("Ignoring DXE services table: invalid signature\n");
-		efi_dxe_table = NULL;
+	if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {
+		efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
+		if (efi_dxe_table &&
+		    efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
+			efi_warn("Ignoring DXE services table: invalid signature\n");
+			efi_dxe_table = NULL;
+		}
 	}
 
+	/* grab the memory attributes protocol if it exists */
+	efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
+
 	if (!boot_params->acpi_rsdp_addr)
 		boot_params->acpi_rsdp_addr = (unsigned long)
 				(get_efi_config_table(ACPI_20_TABLE_GUID) ?:
Signature · 1 line
2.39.2
author maintainer
#8 lore raw

PATCH 6/6 x86: efistub: Avoid legacy decompressor when doing EFI boot

In reply to Ard Biesheuvel

The bare metal decompressor code was never really intended to run in a hosted environment such as the EFI boot services, and does a few things that are problematic in the context of EFI boot now that the logo requirements are getting tighter.

In particular, the decompressor moves itself around in memory, and assumes that arbitrary chunks of low memory can be temporarily emptied, populated with trampoline code and restored again afterwards, and this is difficult to support in a context where memory is not permitted to be mapped writable and executable at the same time or, at the very least, is mapped non-executable by default, and needs special treatment for this restriction to be lifted.

Since EFI already maps all of memory 1:1, we don't need to create new page tables or handle page faults when decompressing the kernel. That means there is also no need to set up special exception handlers for SEV. Generally, there is little need to do anything that the decompressor does beyond
- perform the 4/5 level paging switch, if needed,
- decompress the kernel
- relocate the kernel

So let's do all of this from the EFI stub code, and avoid the bare metal decompressor altogether.

---
 arch/x86/boot/compressed/efi_mixed.S    |  55 ------
 arch/x86/boot/compressed/head_32.S      |  16 --
 arch/x86/boot/compressed/head_64.S      |  31 ----
 arch/x86/include/asm/efi.h              |   2 +
 drivers/firmware/efi/libstub/x86-stub.c | 188 ++++++++------------
 5 files changed, 75 insertions(+), 217 deletions(-)
arch/x86/boot/compressed/efi_mixed.S +0 -55
index 4ca70bf93dc0bdcd..075dd6410f9a4d60 100644
@@ -245,10 +245,6 @@ SYM_FUNC_START(efi32_entry)
 	jmp	startup_32
 SYM_FUNC_END(efi32_entry)
 
-#define ST32_boottime		60 // offsetof(efi_system_table_32_t, boottime)
-#define BS32_handle_protocol	88 // offsetof(efi_boot_services_32_t, handle_protocol)
-#define LI32_image_base		32 // offsetof(efi_loaded_image_32_t, image_base)
-
 /*
  * efi_status_t efi32_pe_entry(efi_handle_t image_handle,
  *			       efi_system_table_32_t *sys_table)
@@ -256,8 +252,6 @@ SYM_FUNC_END(efi32_entry)
 SYM_FUNC_START(efi32_pe_entry)
 	pushl	%ebp
 	movl	%esp, %ebp
-	pushl	%eax				// dummy push to allocate loaded_image
-
 	pushl	%ebx				// save callee-save registers
 	pushl	%edi
 
@@ -266,48 +260,8 @@ SYM_FUNC_START(efi32_pe_entry)
 	movl	$0x80000003, %eax		// EFI_UNSUPPORTED
 	jnz	2f
 
-	call	1f
-1:	pop	%ebx
-
-	/* Get the loaded image protocol pointer from the image handle */
-	leal	-4(%ebp), %eax
-	pushl	%eax				// &loaded_image
-	leal	(loaded_image_proto - 1b)(%ebx), %eax
-	pushl	%eax				// pass the GUID address
-	pushl	8(%ebp)				// pass the image handle
-
-	/*
-	 * Note the alignment of the stack frame.
-	 *   sys_table
-	 *   handle             <-- 16-byte aligned on entry by ABI
-	 *   return address
-	 *   frame pointer
-	 *   loaded_image       <-- local variable
-	 *   saved %ebx		<-- 16-byte aligned here
-	 *   saved %edi
-	 *   &loaded_image
-	 *   &loaded_image_proto
-	 *   handle             <-- 16-byte aligned for call to handle_protocol
-	 */
-
-	movl	12(%ebp), %eax			// sys_table
-	movl	ST32_boottime(%eax), %eax	// sys_table->boottime
-	call	*BS32_handle_protocol(%eax)	// sys_table->boottime->handle_protocol
-	addl	$12, %esp			// restore argument space
-	testl	%eax, %eax
-	jnz	2f
-
 	movl	8(%ebp), %ecx			// image_handle
 	movl	12(%ebp), %edx			// sys_table
-	movl	-4(%ebp), %esi			// loaded_image
-	movl	LI32_image_base(%esi), %esi	// loaded_image->image_base
-	leal	(startup_32 - 1b)(%ebx), %ebp	// runtime address of startup_32
-	/*
-	 * We need to set the image_offset variable here since startup_32() will
-	 * use it before we get to the 64-bit efi_pe_entry() in C code.
-	 */
-	subl	%esi, %ebp			// calculate image_offset
-	movl	%ebp, (image_offset - 1b)(%ebx)	// save image_offset
 	xorl	%esi, %esi
 	jmp	efi32_entry			// pass %ecx, %edx, %esi
 						// no other registers remain live
@@ -318,15 +272,6 @@ SYM_FUNC_START(efi32_pe_entry)
 	RET
 SYM_FUNC_END(efi32_pe_entry)
 
-	.section ".rodata"
-	/* EFI loaded image protocol GUID */
-	.balign 4
-SYM_DATA_START_LOCAL(loaded_image_proto)
-	.long	0x5b1b31a1
-	.word	0x9562, 0x11d2
-	.byte	0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b
-SYM_DATA_END(loaded_image_proto)
-
 	.data
 	.balign	8
 SYM_DATA_START_LOCAL(efi32_boot_gdt)
arch/x86/boot/compressed/head_32.S +0 -16
index 3ecc1bbe971e1d43..a578fe178f1cfee7 100644
@@ -84,19 +84,6 @@ SYM_FUNC_START(startup_32)
 
 #ifdef CONFIG_RELOCATABLE
 	leal	startup_32@GOTOFF(%edx), %ebx
-
-#ifdef CONFIG_EFI_STUB
-/*
- * If we were loaded via the EFI LoadImage service, startup_32() will be at an
- * offset to the start of the space allocated for the image. efi_pe_entry() will
- * set up image_offset to tell us where the image actually starts, so that we
- * can use the full available buffer.
- *	image_offset = startup_32 - image_base
- * Otherwise image_offset will be zero and has no effect on the calculations.
- */
-	subl    image_offset@GOTOFF(%edx), %ebx
-#endif
-
 	movl	BP_kernel_alignment(%esi), %eax
 	decl	%eax
 	addl    %eax, %ebx
@@ -153,10 +140,7 @@ SYM_FUNC_END(startup_32)
 #ifdef CONFIG_EFI_STUB
 SYM_FUNC_START(efi32_stub_entry)
 	add	$0x4, %esp
-	movl	8(%esp), %esi	/* save boot_params pointer */
 	call	efi_main
-	/* efi_main returns the possibly relocated address of startup_32 */
-	jmp	*%eax
 SYM_FUNC_END(efi32_stub_entry)
 SYM_FUNC_ALIAS(efi_stub_entry, efi32_stub_entry)
 #endif
arch/x86/boot/compressed/head_64.S +0 -31
index 5ab014be179c1103..eb8ead7bce9fc577 100644
@@ -146,19 +146,6 @@ SYM_FUNC_START(startup_32)
 
 #ifdef CONFIG_RELOCATABLE
 	movl	%ebp, %ebx
-
-#ifdef CONFIG_EFI_STUB
-/*
- * If we were loaded via the EFI LoadImage service, startup_32 will be at an
- * offset to the start of the space allocated for the image. efi_pe_entry will
- * set up image_offset to tell us where the image actually starts, so that we
- * can use the full available buffer.
- *	image_offset = startup_32 - image_base
- * Otherwise image_offset will be zero and has no effect on the calculations.
- */
-	subl    rva(image_offset)(%ebp), %ebx
-#endif
-
 	movl	BP_kernel_alignment(%esi), %eax
 	decl	%eax
 	addl	%eax, %ebx
@@ -346,20 +333,6 @@ SYM_CODE_START(startup_64)
 	/* Start with the delta to where the kernel will run at. */
 #ifdef CONFIG_RELOCATABLE
 	leaq	startup_32(%rip) /* - $startup_32 */, %rbp
-
-#ifdef CONFIG_EFI_STUB
-/*
- * If we were loaded via the EFI LoadImage service, startup_32 will be at an
- * offset to the start of the space allocated for the image. efi_pe_entry will
- * set up image_offset to tell us where the image actually starts, so that we
- * can use the full available buffer.
- *	image_offset = startup_32 - image_base
- * Otherwise image_offset will be zero and has no effect on the calculations.
- */
-	movl    image_offset(%rip), %eax
-	subq	%rax, %rbp
-#endif
-
 	movl	BP_kernel_alignment(%rsi), %eax
 	decl	%eax
 	addq	%rax, %rbp
@@ -529,11 +502,7 @@ SYM_CODE_END(startup_64)
 #endif
 SYM_FUNC_START(efi64_stub_entry)
 	and	$~0xf, %rsp			/* realign the stack */
-	movq	%rdx, %rbx			/* save boot_params pointer */
 	call	efi_main
-	movq	%rbx,%rsi
-	leaq	rva(startup_64)(%rax), %rax
-	jmp	*%rax
 SYM_FUNC_END(efi64_stub_entry)
 SYM_FUNC_ALIAS(efi_stub_entry, efi64_stub_entry)
 #endif
arch/x86/include/asm/efi.h +2 -0
index 419280d263d2e3f2..36b848e7d088a59b 100644
@@ -216,6 +216,8 @@ efi_status_t efi_set_virtual_address_map(unsigned long memory_map_size,
 
 #ifdef CONFIG_EFI_MIXED
 
+#define EFI_ALLOC_LIMIT		(efi_is_64bit() ? ULONG_MAX : U32_MAX)
+
 #define ARCH_HAS_EFISTUB_WRAPPERS
 
 static inline bool efi_is_64bit(void)
drivers/firmware/efi/libstub/x86-stub.c +73 -115

254 lines not on this page · raw message

Signature · 1 line
2.39.2
maintainer
#9 lore raw
In reply to Ard Biesheuvel
Quoted 8 lines · Ard Biesheuvel
 arch/x86/boot/compressed/efi_mixed.S           |  55 ---
 arch/x86/boot/compressed/head_32.S             |  24 --
 arch/x86/boot/compressed/head_64.S             |  39 +--
 arch/x86/boot/compressed/misc.c                |  44 ++-
 arch/x86/include/asm/efi.h                     |   2 +
 drivers/firmware/efi/libstub/efi-stub-helper.c |   4 +
 drivers/firmware/efi/libstub/x86-stub.c        | 360 +++++++++++++-------
 7 files changed, 279 insertions(+), 249 deletions(-)

Upon a quick scan, I can't argue with that diffstat and would prefer a lot more if we did this instead of Evgeny's pile which touches a lot of nasty and hard to debug code which gets executed on *everything*.

So if people agree with that approach, I'd gladly give it a more detailed look.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#10 lore raw
Quoted 11 lines · Borislav Petkov
Quoted 8 lines · Ard Biesheuvel
 arch/x86/boot/compressed/efi_mixed.S           |  55 ---
 arch/x86/boot/compressed/head_32.S             |  24 --
 arch/x86/boot/compressed/head_64.S             |  39 +--
 arch/x86/boot/compressed/misc.c                |  44 ++-
 arch/x86/include/asm/efi.h                     |   2 +
 drivers/firmware/efi/libstub/efi-stub-helper.c |   4 +
 drivers/firmware/efi/libstub/x86-stub.c        | 360 +++++++++++++-------
 7 files changed, 279 insertions(+), 249 deletions(-)
Quoted 7 lines

Upon a quick scan, I can't argue with that diffstat and would prefer a lot more if we did this instead of Evgeny's pile which touches a lot of nasty and hard to debug code which gets executed on *everything*.

So if people agree with that approach, I'd gladly give it a more detailed look.

I think the general approach is better, but to be honest, I may have missed a thing or two, so it would be good if people more familiar with the history could chime in.

#11 lore raw
In reply to Ard Biesheuvel
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

My series also implements W^X for both UEFI and non-UEFI boot paths, but I agree that we can just consider non-UEFI code legacy and it would be better to avoid touching it and encourage everyone to use UEFI code path on x86 instead. If PE format will also get fixed with either my patches or some others, I do like your approach more than mine, as it removes a lot of old cruft but does not break things (as far as I see). Seems like a perfect compromise between [1] and my approach.

I've briefly tested the patches and looked through them and they look good to me. Two things I've noticed:
* there's one TSC-related TODO;
* probably we want to clear .bss in efi32_stub_entry and
efi64_stub_entry
for UEFI handover protocol, since it's unfortunately still present
and
.bss will contain garbage.
I'll probably do some more testing on the weekend and let you know if I find something.

Please tell me if/when you are going to merge these or similar, and I will clean up and rebase PE-related patches on top of these.

I'd also like to send W^X patches for EFISTUB (omitting the non-UEFI boot path) as a follow up after the PE file header will get fixed. They will be considerably smaller with this approach and will not touch legacy code.

Thanks,
Evgeniy Baskov

Quoted 38 lines
Cc Evgeniy Baskov <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Alexey Khoroshilov <[email protected]>
Cc Peter Jones <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Dave Young <[email protected]>
Cc Mario Limonciello <[email protected]>
Cc Kees Cook <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Linus Torvalds <[email protected]>

[0] https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/

Ard Biesheuvel (6):
x86: decompressor: Move global symbol references to C code
x86: decompressor: Factor out kernel decompression and relocation
x86: efistub: Obtain ACPI RSDP address while running in the stub
x86: efistub: Perform 4/5 level paging switch from the stub
x86: efistub: Prefer EFI memory attributes protocol over DXE services
x86: efistub: Avoid legacy decompressor when doing EFI boot

 arch/x86/boot/compressed/efi_mixed.S           |  55 ---
 arch/x86/boot/compressed/head_32.S             |  24 --
 arch/x86/boot/compressed/head_64.S             |  39 +--
 arch/x86/boot/compressed/misc.c                |  44 ++-
 arch/x86/include/asm/efi.h                     |   2 +
 drivers/firmware/efi/libstub/efi-stub-helper.c |   4 +
 drivers/firmware/efi/libstub/x86-stub.c        | 360 
+++++++++++++-------
 7 files changed, 279 insertions(+), 249 deletions(-)
author maintainer
#12 lore raw
Quoted 43 lines · Evgeniy Baskov
Quoted 32 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more
amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is
supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the
first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called
from the EFI stub. Then, it reimplements the EFI boot flow to decompress
the kernel and boot it directly, without relying on the trampoline code,
page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Quoted 11 lines

My series also implements W^X for both UEFI and non-UEFI boot paths, but I
agree that we can just consider non-UEFI code legacy and it would be better
to avoid touching it and encourage everyone to use UEFI code path on x86 instead. If PE format will also get fixed with either my patches or some others, I do like your approach more than mine, as it removes a lot of old
cruft but does not break things (as far as I see). Seems like a perfect compromise between [1] and my approach.

Thanks, I'm glad we agree.

Quoted 9 lines

I've briefly tested the patches and looked through them and they look good
to me. Two things I've noticed:
* there's one TSC-related TODO;
* probably we want to clear .bss in efi32_stub_entry and
efi64_stub_entry
for UEFI handover protocol, since it's unfortunately still present
and
.bss will contain garbage.

I'll look into that.

Quoted 3 lines

I'll probably do some more testing on the weekend and let you know if I find something.

Yes, please.

Quoted 10 lines

Please tell me if/when you are going to merge these or similar, and I will
clean up and rebase PE-related patches on top of these.

I'd also like to send W^X patches for EFISTUB (omitting the non-UEFI boot
path) as a follow up after the PE file header will get fixed. They will be
considerably smaller with this approach and will not touch legacy code.

#13 lore raw
In reply to Ard Biesheuvel
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

I tried booting an SEV and an SEV-ES guest using this and both failed to boot:

EFI stub: WARNING: Decompression failed: Out of memory while allocating z_stream

I'll have to take a closer look as to why, but it might be a couple of days before I can get to it.

Thanks,
Tom

Quoted 38 lines
Cc Evgeniy Baskov <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Alexey Khoroshilov <[email protected]>
Cc Peter Jones <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Dave Young <[email protected]>
Cc Mario Limonciello <[email protected]>
Cc Kees Cook <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Linus Torvalds <[email protected]>

[0] https://lore.kernel.org/all/[email protected]/
[1] https://lore.kernel.org/all/[email protected]/

Ard Biesheuvel (6):
x86: decompressor: Move global symbol references to C code
x86: decompressor: Factor out kernel decompression and relocation
x86: efistub: Obtain ACPI RSDP address while running in the stub
x86: efistub: Perform 4/5 level paging switch from the stub
x86: efistub: Prefer EFI memory attributes protocol over DXE services
x86: efistub: Avoid legacy decompressor when doing EFI boot

  arch/x86/boot/compressed/efi_mixed.S           |  55 ---
  arch/x86/boot/compressed/head_32.S             |  24 --
  arch/x86/boot/compressed/head_64.S             |  39 +--
  arch/x86/boot/compressed/misc.c                |  44 ++-
  arch/x86/include/asm/efi.h                     |   2 +
  drivers/firmware/efi/libstub/efi-stub-helper.c |   4 +
  drivers/firmware/efi/libstub/x86-stub.c        | 360 +++++++++++++-------
  7 files changed, 279 insertions(+), 249 deletions(-)
author maintainer
#14 lore raw
Quoted 37 lines · Tom Lendacky
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Quoted 8 lines

I tried booting an SEV and an SEV-ES guest using this and both failed to boot:

EFI stub: WARNING: Decompression failed: Out of memory while allocating z_stream

I'll have to take a closer look as to why, but it might be a couple of days before I can get to it.

Thanks Tom.

The internal malloc() seems to be failing, which is often caused by BSS clearing problems. Could you elaborate a little bit on the boot environment you are using here?

#15 lore raw
Quoted 47 lines · Ard Biesheuvel
Quoted 37 lines · Tom Lendacky
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Quoted 8 lines

I tried booting an SEV and an SEV-ES guest using this and both failed to boot:

EFI stub: WARNING: Decompression failed: Out of memory while allocating z_stream

I'll have to take a closer look as to why, but it might be a couple of days before I can get to it.

Quoted 5 lines

Thanks Tom.

The internal malloc() seems to be failing, which is often caused by BSS clearing problems. Could you elaborate a little bit on the boot environment you are using here?

I'm using Qemu v7.2.1 as my VMM, Linux 6.3 with your series applied for my host/hypervisor and guest kernel and the current OVMF tree built using OvmfPkgX64.dsc.

I was originally using the current merge window Linux, but moved to the release version just to . With the release version SEV and SEV-ES still fail to boot, but SEV actually #GPs now. And some of the register contents look like encrypted data:

ConvertPages: range 1000000 - 4FA1FFF covers multiple entries
!!!! X64 Exception Type - 0D(#GP - General Protection) CPU Apic ID - 00000000 !!!! ExceptionData - 0000000000000000

RIP  - 00000000597E71C1, CS  - 0000000000000038, RFLAGS - 0000000000210206
RAX  - 1FBA02A45943B920, RCX - 0000000000AF7009, RDX - A9DAE761B64A1F1B
RBX  - 1FBA02A45943B8C0, RSP - 000000007FD97320, RBP - 0000000002000000
RSI  - 0000000001000000, RDI - 1FBA02A45943DE68
R8   - 0000000003EF3C94, R9  - 0000000000000000, R10 - 000000007D7C6018
R11  - 0000000000000000, R12 - 0000000001000000, R13 - 00000000597EDD98
R14  - 0000000001000000, R15 - 000000007E0A5198
DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
GS   - 0000000000000030, SS  - 0000000000000030
CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FA01000
CR4  - 0000000000000668, CR8 - 0000000000000000
DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400

GDTR - 000000007F7DC000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F34C018 0000000000000FFF, TR - 0000000000000000
FXSAVE_STATE - 000000007FD96F80
!!!! Find image based on IP(0x597E71C1) /root/kernels/ovmf-build-X64/Build/OvmfX64/DEBUG_GCC5/X64/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe/DEBUG/Variable RuntimeDxe.dll (ImageBase=0000000000D4792C, EntryPoint=0000000000D50CC3) !!!!

So, yes, probably an area of memory that was zeroes when mapped unencrypted, but wasn't cleared after changing the mapping to encrypted.

Thanks,
Tom

author maintainer
#16 lore raw
Quoted 86 lines · Tom Lendacky
Quoted 47 lines · Ard Biesheuvel
Quoted 37 lines · Tom Lendacky
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Quoted 8 lines

I tried booting an SEV and an SEV-ES guest using this and both failed to boot:

EFI stub: WARNING: Decompression failed: Out of memory while allocating z_stream

I'll have to take a closer look as to why, but it might be a couple of days before I can get to it.

Quoted 5 lines

Thanks Tom.

The internal malloc() seems to be failing, which is often caused by BSS clearing problems. Could you elaborate a little bit on the boot environment you are using here?

I'm using Qemu v7.2.1 as my VMM, Linux 6.3 with your series applied for my host/hypervisor and guest kernel and the current OVMF tree built using OvmfPkgX64.dsc.

I was originally using the current merge window Linux, but moved to the release version just to . With the release version SEV and SEV-ES still fail to boot, but SEV actually #GPs now. And some of the register contents look like encrypted data:

ConvertPages: range 1000000 - 4FA1FFF covers multiple entries
!!!! X64 Exception Type - 0D(#GP - General Protection) CPU Apic ID - 00000000 !!!! ExceptionData - 0000000000000000

RIP  - 00000000597E71C1, CS  - 0000000000000038, RFLAGS - 0000000000210206
RAX  - 1FBA02A45943B920, RCX - 0000000000AF7009, RDX - A9DAE761B64A1F1B
RBX  - 1FBA02A45943B8C0, RSP - 000000007FD97320, RBP - 0000000002000000
RSI  - 0000000001000000, RDI - 1FBA02A45943DE68
R8   - 0000000003EF3C94, R9  - 0000000000000000, R10 - 000000007D7C6018
R11  - 0000000000000000, R12 - 0000000001000000, R13 - 00000000597EDD98
R14  - 0000000001000000, R15 - 000000007E0A5198
DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
GS   - 0000000000000030, SS  - 0000000000000030
CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FA01000
CR4  - 0000000000000668, CR8 - 0000000000000000
DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400

GDTR - 000000007F7DC000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F34C018 0000000000000FFF, TR - 0000000000000000
FXSAVE_STATE - 000000007FD96F80
!!!! Find image based on IP(0x597E71C1) /root/kernels/ovmf-build-X64/Build/OvmfX64/DEBUG_GCC5/X64/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe/DEBUG/Variable RuntimeDxe.dll (ImageBase=0000000000D4792C, EntryPoint=0000000000D50CC3) !!!!

Quoted 4 lines

So, yes, probably an area of memory that was zeroes when mapped unencrypted, but wasn't cleared after changing the mapping to encrypted.

Thanks.

It seems I was a bit naive and underestimated the amount of SEV related processing that goes on in the decompressor after the EFI stub has handed over. I will have to take some time and go through this, and decide whether there is a way we can share this code with the EFI stub without introducing yet another permutation that requires testing and maintenance.

Any suggestions on how to test this stuff is appreciated - does QEMU emulate any of this? Does consumer-level AMD hardware implement the pieces I'd need to run a SEV host with SNP support etc?

#17 lore raw
Quoted 101 lines · Ard Biesheuvel
Quoted 86 lines · Tom Lendacky
Quoted 47 lines · Ard Biesheuvel
Quoted 37 lines · Tom Lendacky
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Quoted 8 lines

I tried booting an SEV and an SEV-ES guest using this and both failed to boot:

EFI stub: WARNING: Decompression failed: Out of memory while allocating z_stream

I'll have to take a closer look as to why, but it might be a couple of days before I can get to it.

Quoted 5 lines

Thanks Tom.

The internal malloc() seems to be failing, which is often caused by BSS clearing problems. Could you elaborate a little bit on the boot environment you are using here?

I'm using Qemu v7.2.1 as my VMM, Linux 6.3 with your series applied for my host/hypervisor and guest kernel and the current OVMF tree built using OvmfPkgX64.dsc.

I was originally using the current merge window Linux, but moved to the release version just to . With the release version SEV and SEV-ES still fail to boot, but SEV actually #GPs now. And some of the register contents look like encrypted data:

ConvertPages: range 1000000 - 4FA1FFF covers multiple entries
!!!! X64 Exception Type - 0D(#GP - General Protection) CPU Apic ID - 00000000 !!!! ExceptionData - 0000000000000000

RIP  - 00000000597E71C1, CS  - 0000000000000038, RFLAGS - 0000000000210206
RAX  - 1FBA02A45943B920, RCX - 0000000000AF7009, RDX - A9DAE761B64A1F1B
RBX  - 1FBA02A45943B8C0, RSP - 000000007FD97320, RBP - 0000000002000000
RSI  - 0000000001000000, RDI - 1FBA02A45943DE68
R8   - 0000000003EF3C94, R9  - 0000000000000000, R10 - 000000007D7C6018
R11  - 0000000000000000, R12 - 0000000001000000, R13 - 00000000597EDD98
R14  - 0000000001000000, R15 - 000000007E0A5198
DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
GS   - 0000000000000030, SS  - 0000000000000030
CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FA01000
CR4  - 0000000000000668, CR8 - 0000000000000000
DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400

GDTR - 000000007F7DC000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F34C018 0000000000000FFF, TR - 0000000000000000
FXSAVE_STATE - 000000007FD96F80
!!!! Find image based on IP(0x597E71C1) /root/kernels/ovmf-build-X64/Build/OvmfX64/DEBUG_GCC5/X64/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe/DEBUG/Variable RuntimeDxe.dll (ImageBase=0000000000D4792C, EntryPoint=0000000000D50CC3) !!!!

Quoted 4 lines

So, yes, probably an area of memory that was zeroes when mapped unencrypted, but wasn't cleared after changing the mapping to encrypted.

Thanks.

It seems I was a bit naive and underestimated the amount of SEV related processing that goes on in the decompressor after the EFI stub has handed over. I will have to take some time and go through this, and decide whether there is a way we can share this code with the EFI stub without introducing yet another permutation that requires testing and maintenance.

Quoted 3 lines

Any suggestions on how to test this stuff is appreciated - does QEMU emulate any of this? Does consumer-level AMD hardware implement the pieces I'd need to run a SEV host with SNP support etc?

Unfortunately Qemu doesn't emulate any of this and SEV support, specifically the SEV firmware, isn't available on consumer-level parts, so you would need an EPYC part to do SEV.

Thanks,
Tom

#18 lore raw
In reply to Tom Lendacky
Quoted 85 lines · Tom Lendacky
Quoted 47 lines · Ard Biesheuvel
Quoted 37 lines · Tom Lendacky
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Quoted 8 lines

I tried booting an SEV and an SEV-ES guest using this and both failed to boot:

EFI stub: WARNING: Decompression failed: Out of memory while allocating z_stream

I'll have to take a closer look as to why, but it might be a couple of days before I can get to it.

Quoted 5 lines

Thanks Tom.

The internal malloc() seems to be failing, which is often caused by BSS clearing problems. Could you elaborate a little bit on the boot environment you are using here?

I'm using Qemu v7.2.1 as my VMM, Linux 6.3 with your series applied for my host/hypervisor and guest kernel and the current OVMF tree built using OvmfPkgX64.dsc.

I was originally using the current merge window Linux, but moved to the release version just to . With the release version SEV and SEV-ES still fail to boot, but SEV actually #GPs now. And some of the register contents look like encrypted data:

ConvertPages: range 1000000 - 4FA1FFF covers multiple entries
!!!! X64 Exception Type - 0D(#GP - General Protection)  CPU Apic ID - 00000000 !!!!
ExceptionData - 0000000000000000
RIP  - 00000000597E71C1, CS  - 0000000000000038, RFLAGS - 0000000000210206
RAX  - 1FBA02A45943B920, RCX - 0000000000AF7009, RDX - A9DAE761B64A1F1B
RBX  - 1FBA02A45943B8C0, RSP - 000000007FD97320, RBP - 0000000002000000
RSI  - 0000000001000000, RDI - 1FBA02A45943DE68
R8   - 0000000003EF3C94, R9  - 0000000000000000, R10 - 000000007D7C6018
R11  - 0000000000000000, R12 - 0000000001000000, R13 - 00000000597EDD98
R14  - 0000000001000000, R15 - 000000007E0A5198
DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
GS   - 0000000000000030, SS  - 0000000000000030
CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FA01000
CR4  - 0000000000000668, CR8 - 0000000000000000
DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400
GDTR - 000000007F7DC000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F34C018 0000000000000FFF,   TR - 0000000000000000
FXSAVE_STATE - 000000007FD96F80
!!!! Find image based on IP(0x597E71C1) /root/kernels/ovmf-build-X64/Build/OvmfX64/DEBUG_GCC5/X64/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe/DEBUG/Variable RuntimeDxe.dll (ImageBase=0000000000D4792C, EntryPoint=0000000000D50CC3) !!!!

Quoted 3 lines

So, yes, probably an area of memory that was zeroes when mapped unencrypted, but wasn't cleared after changing the mapping to encrypted.

Yes, looks like a bss clearing issue. If I add __section(".data") to free_mem_ptr and free_mem_end_ptr in arch/x86/boot/compressed/misc.c and to malloc_ptr and malloc_cnt in include/linux/decompress/mm.h, then I can boot an SEV guest.

However, an SEV-ES guest is triple faulting. This looks to be because we're still on the EFI CS of 0x38 after switching GDTs in arch/x86/kernel/head_64.S by calling startup_64_setup_env(). Before switching to the kernel CS, we take a #VC (from CPUID calls in sme_enable) and things blow up on the iretq. Moving the block headed by the comment "Now switch to __KERNEL_CS so IRET works reliably" to just after calling startup_64_setup_env() fixes it and an SEV-ES guest can boot.

This worked before because I believe we switched off the EFI CS as part of the kernel decompressor support and so this bug wasn't exposed. But this needs to be fixed regardless of this series.

Thanks,
Tom

Quoted 5 lines

Thanks,
Tom

author maintainer
#19 lore raw
Quoted 108 lines · Tom Lendacky
Quoted 89 lines · Tom Lendacky
Quoted 48 lines · Ard Biesheuvel
Quoted 37 lines · Tom Lendacky
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Quoted 9 lines

I tried booting an SEV and an SEV-ES guest using this and both failed to boot:

EFI stub: WARNING: Decompression failed: Out of memory while allocating z_stream

I'll have to take a closer look as to why, but it might be a couple of days before I can get to it.

Quoted 5 lines

Thanks Tom.

The internal malloc() seems to be failing, which is often caused by BSS clearing problems. Could you elaborate a little bit on the boot environment you are using here?

I'm using Qemu v7.2.1 as my VMM, Linux 6.3 with your series applied for my host/hypervisor and guest kernel and the current OVMF tree built using OvmfPkgX64.dsc.

I was originally using the current merge window Linux, but moved to the release version just to . With the release version SEV and SEV-ES still fail to
boot, but SEV actually #GPs now. And some of the register contents look like encrypted data:

ConvertPages: range 1000000 - 4FA1FFF covers multiple entries !!!! X64 Exception Type - 0D(#GP - General Protection) CPU Apic ID - 00000000 !!!!
ExceptionData - 0000000000000000

RIP  - 00000000597E71C1, CS  - 0000000000000038, RFLAGS - 0000000000210206
RAX  - 1FBA02A45943B920, RCX - 0000000000AF7009, RDX - A9DAE761B64A1F1B
RBX  - 1FBA02A45943B8C0, RSP - 000000007FD97320, RBP - 0000000002000000
RSI  - 0000000001000000, RDI - 1FBA02A45943DE68
R8   - 0000000003EF3C94, R9  - 0000000000000000, R10 - 000000007D7C6018
R11  - 0000000000000000, R12 - 0000000001000000, R13 - 00000000597EDD98
R14  - 0000000001000000, R15 - 000000007E0A5198
DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
GS   - 0000000000000030, SS  - 0000000000000030
CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FA01000
CR4  - 0000000000000668, CR8 - 0000000000000000
DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400

GDTR - 000000007F7DC000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F34C018 0000000000000FFF, TR - 0000000000000000
FXSAVE_STATE - 000000007FD96F80
!!!! Find image based on IP(0x597E71C1) /root/kernels/ovmf-build-X64/Build/OvmfX64/DEBUG_GCC5/X64/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe/DEBUG/Variable RuntimeDxe.dll (ImageBase=0000000000D4792C, EntryPoint=0000000000D50CC3) !!!!

Quoted 3 lines

So, yes, probably an area of memory that was zeroes when mapped unencrypted, but wasn't cleared after changing the mapping to encrypted.

Yes, looks like a bss clearing issue. If I add __section(".data") to free_mem_ptr and free_mem_end_ptr in arch/x86/boot/compressed/misc.c and to malloc_ptr and malloc_cnt in include/linux/decompress/mm.h, then I can boot an SEV guest.

However, an SEV-ES guest is triple faulting. This looks to be because we're still on the EFI CS of 0x38 after switching GDTs in arch/x86/kernel/head_64.S by calling startup_64_setup_env(). Before switching to the kernel CS, we take a #VC (from CPUID calls in sme_enable) and things blow up on the iretq. Moving the block headed by the comment "Now switch to __KERNEL_CS so IRET works reliably" to just after calling startup_64_setup_env() fixes it and an SEV-ES guest can boot.

Quoted 4 lines

This worked before because I believe we switched off the EFI CS as part of the kernel decompressor support and so this bug wasn't exposed. But this needs to be fixed regardless of this series.

Very interesting. I was under the assumption that everything that goes on in sev_enable() in the decompressor would be rather indispensable to boot in SEV mode (which I only spotted today) so I am quite surprised that things just appear to work. (There is some 32-bit SEV code in the decompressor as well that obviously never gets called when booting in long mode, but I hadn't quite grasped how much other SEV code there actually is)

maintainer
#20 lore raw
Quoted 3 lines · Ard Biesheuvel

(There is some 32-bit SEV code in the decompressor as well that obviously never gets called when booting in long mode, but I hadn't quite grasped how much other SEV code there actually is)

If you mean the thing in startup_32 which does get_sev_encryption_bit() etc, that is going away.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#21 lore raw
Quoted 9 lines · Borislav Petkov
Quoted 3 lines · Ard Biesheuvel

(There is some 32-bit SEV code in the decompressor as well that obviously never gets called when booting in long mode, but I hadn't quite grasped how much other SEV code there actually is)

If you mean the thing in startup_32 which does get_sev_encryption_bit() etc, that is going away.

OK, good to know.

#22 lore raw
In reply to Ard Biesheuvel
Quoted 114 lines · Ard Biesheuvel
Quoted 108 lines · Tom Lendacky
Quoted 89 lines · Tom Lendacky
Quoted 48 lines · Ard Biesheuvel
Quoted 37 lines · Tom Lendacky
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Quoted 9 lines

I tried booting an SEV and an SEV-ES guest using this and both failed to boot:

EFI stub: WARNING: Decompression failed: Out of memory while allocating z_stream

I'll have to take a closer look as to why, but it might be a couple of days before I can get to it.

Quoted 5 lines

Thanks Tom.

The internal malloc() seems to be failing, which is often caused by BSS clearing problems. Could you elaborate a little bit on the boot environment you are using here?

I'm using Qemu v7.2.1 as my VMM, Linux 6.3 with your series applied for my host/hypervisor and guest kernel and the current OVMF tree built using OvmfPkgX64.dsc.

I was originally using the current merge window Linux, but moved to the release version just to . With the release version SEV and SEV-ES still fail to
boot, but SEV actually #GPs now. And some of the register contents look like encrypted data:

ConvertPages: range 1000000 - 4FA1FFF covers multiple entries !!!! X64 Exception Type - 0D(#GP - General Protection) CPU Apic ID - 00000000 !!!!
ExceptionData - 0000000000000000

RIP  - 00000000597E71C1, CS  - 0000000000000038, RFLAGS - 0000000000210206
RAX  - 1FBA02A45943B920, RCX - 0000000000AF7009, RDX - A9DAE761B64A1F1B
RBX  - 1FBA02A45943B8C0, RSP - 000000007FD97320, RBP - 0000000002000000
RSI  - 0000000001000000, RDI - 1FBA02A45943DE68
R8   - 0000000003EF3C94, R9  - 0000000000000000, R10 - 000000007D7C6018
R11  - 0000000000000000, R12 - 0000000001000000, R13 - 00000000597EDD98
R14  - 0000000001000000, R15 - 000000007E0A5198
DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
GS   - 0000000000000030, SS  - 0000000000000030
CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FA01000
CR4  - 0000000000000668, CR8 - 0000000000000000
DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400

GDTR - 000000007F7DC000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F34C018 0000000000000FFF, TR - 0000000000000000
FXSAVE_STATE - 000000007FD96F80
!!!! Find image based on IP(0x597E71C1) /root/kernels/ovmf-build-X64/Build/OvmfX64/DEBUG_GCC5/X64/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe/DEBUG/Variable RuntimeDxe.dll (ImageBase=0000000000D4792C, EntryPoint=0000000000D50CC3) !!!!

Quoted 3 lines

So, yes, probably an area of memory that was zeroes when mapped unencrypted, but wasn't cleared after changing the mapping to encrypted.

Yes, looks like a bss clearing issue. If I add __section(".data") to free_mem_ptr and free_mem_end_ptr in arch/x86/boot/compressed/misc.c and to malloc_ptr and malloc_cnt in include/linux/decompress/mm.h, then I can boot an SEV guest.

However, an SEV-ES guest is triple faulting. This looks to be because we're still on the EFI CS of 0x38 after switching GDTs in arch/x86/kernel/head_64.S by calling startup_64_setup_env(). Before switching to the kernel CS, we take a #VC (from CPUID calls in sme_enable) and things blow up on the iretq. Moving the block headed by the comment "Now switch to __KERNEL_CS so IRET works reliably" to just after calling startup_64_setup_env() fixes it and an SEV-ES guest can boot.

Quoted 4 lines

This worked before because I believe we switched off the EFI CS as part of the kernel decompressor support and so this bug wasn't exposed. But this needs to be fixed regardless of this series.

Quoted 7 lines

Very interesting. I was under the assumption that everything that goes on in sev_enable() in the decompressor would be rather indispensable to boot in SEV mode (which I only spotted today) so I am quite surprised that things just appear to work. (There is some 32-bit SEV code in the decompressor as well that obviously never gets called when booting in long mode, but I hadn't quite grasped how much other SEV code there actually is)

The sev_enable() function for SEV and SEV-ES is more for ensuring a proper environment (ensuring the proper CPUID bits are present for the guest, checking the GHCB protocol level, properly preparing pagetables, etc.). Since we're still in EFI and using the EFI #VC handler and pagetables, we don't require some of that stuff, but some of the it would need to be performed at some point (I wasn't trying to be a malicious hypervisor).

I haven't tested SEV-SNP yet because the hypervisor support is not upstream, yet, and I haven't applied your series to an SNP hypervisor tree, yet. There is a lot of support in sev_enable() for SNP that may likely cause a problem compared to SEV and SEV-ES.

SEV-SNP has a lot more checking and validation going on and it ensures it gets the confidential computing blob from EFI into the boot parameters. I'm not sure when I'll be able to test SNP, since I'm off next week and trying to wrap up a bunch of stuff before I leave.

Thanks,
Tom

author maintainer
#23 lore raw
Quoted 124 lines · Tom Lendacky
Quoted 114 lines · Ard Biesheuvel
Quoted 108 lines · Tom Lendacky
Quoted 89 lines · Tom Lendacky
Quoted 48 lines · Ard Biesheuvel
Quoted 37 lines · Tom Lendacky
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Quoted 9 lines

I tried booting an SEV and an SEV-ES guest using this and both failed to boot:

EFI stub: WARNING: Decompression failed: Out of memory while allocating z_stream

I'll have to take a closer look as to why, but it might be a couple of days before I can get to it.

Quoted 5 lines

Thanks Tom.

The internal malloc() seems to be failing, which is often caused by BSS clearing problems. Could you elaborate a little bit on the boot environment you are using here?

I'm using Qemu v7.2.1 as my VMM, Linux 6.3 with your series applied for my host/hypervisor and guest kernel and the current OVMF tree built using OvmfPkgX64.dsc.

I was originally using the current merge window Linux, but moved to the release version just to . With the release version SEV and SEV-ES still fail to
boot, but SEV actually #GPs now. And some of the register contents look like encrypted data:

ConvertPages: range 1000000 - 4FA1FFF covers multiple entries !!!! X64 Exception Type - 0D(#GP - General Protection) CPU Apic ID - 00000000 !!!!
ExceptionData - 0000000000000000

RIP  - 00000000597E71C1, CS  - 0000000000000038, RFLAGS - 0000000000210206
RAX  - 1FBA02A45943B920, RCX - 0000000000AF7009, RDX - A9DAE761B64A1F1B
RBX  - 1FBA02A45943B8C0, RSP - 000000007FD97320, RBP - 0000000002000000
RSI  - 0000000001000000, RDI - 1FBA02A45943DE68
R8   - 0000000003EF3C94, R9  - 0000000000000000, R10 - 000000007D7C6018
R11  - 0000000000000000, R12 - 0000000001000000, R13 - 00000000597EDD98
R14  - 0000000001000000, R15 - 000000007E0A5198
DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
GS   - 0000000000000030, SS  - 0000000000000030
CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FA01000
CR4  - 0000000000000668, CR8 - 0000000000000000
DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400

GDTR - 000000007F7DC000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F34C018 0000000000000FFF, TR - 0000000000000000
FXSAVE_STATE - 000000007FD96F80
!!!! Find image based on IP(0x597E71C1) /root/kernels/ovmf-build-X64/Build/OvmfX64/DEBUG_GCC5/X64/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe/DEBUG/Variable RuntimeDxe.dll (ImageBase=0000000000D4792C, EntryPoint=0000000000D50CC3) !!!!

Quoted 3 lines

So, yes, probably an area of memory that was zeroes when mapped unencrypted, but wasn't cleared after changing the mapping to encrypted.

Yes, looks like a bss clearing issue. If I add __section(".data") to free_mem_ptr and free_mem_end_ptr in arch/x86/boot/compressed/misc.c and to malloc_ptr and malloc_cnt in include/linux/decompress/mm.h, then I can boot an SEV guest.

However, an SEV-ES guest is triple faulting. This looks to be because we're still on the EFI CS of 0x38 after switching GDTs in arch/x86/kernel/head_64.S by calling startup_64_setup_env(). Before switching to the kernel CS, we take a #VC (from CPUID calls in sme_enable) and things blow up on the iretq. Moving the block headed by the comment "Now switch to __KERNEL_CS so IRET works reliably" to just after calling startup_64_setup_env() fixes it and an SEV-ES guest can boot.

Quoted 4 lines

This worked before because I believe we switched off the EFI CS as part of the kernel decompressor support and so this bug wasn't exposed. But this needs to be fixed regardless of this series.

Quoted 7 lines

Very interesting. I was under the assumption that everything that goes on in sev_enable() in the decompressor would be rather indispensable to boot in SEV mode (which I only spotted today) so I am quite surprised that things just appear to work. (There is some 32-bit SEV code in the decompressor as well that obviously never gets called when booting in long mode, but I hadn't quite grasped how much other SEV code there actually is)

Quoted 7 lines

The sev_enable() function for SEV and SEV-ES is more for ensuring a proper environment (ensuring the proper CPUID bits are present for the guest, checking the GHCB protocol level, properly preparing pagetables, etc.). Since we're still in EFI and using the EFI #VC handler and pagetables, we don't require some of that stuff, but some of the it would need to be performed at some point (I wasn't trying to be a malicious hypervisor).

OK, good to know.

Quoted 10 lines

I haven't tested SEV-SNP yet because the hypervisor support is not upstream, yet, and I haven't applied your series to an SNP hypervisor tree, yet. There is a lot of support in sev_enable() for SNP that may likely cause a problem compared to SEV and SEV-ES.

SEV-SNP has a lot more checking and validation going on and it ensures it gets the confidential computing blob from EFI into the boot parameters. I'm not sure when I'll be able to test SNP, since I'm off next week and trying to wrap up a bunch of stuff before I leave.

Would it make sense, given the apparent direct dependency on EFI, to move that stuff into the EFI stub instead of doing it from the decompressor?

Thanks for giving this a spin - I can wait for more testing until you get back, and hopefully, we'll have converged a bit more by that time and there will be a new revision of the series available for testing.

BTW any clue whether your boot path is relying on the EFI handover protocol? I.e., QemuStartLegacyImage() in OVMF? Evgeny mentioned that BSS needs clearing in that case, and I was wondering if that is the case you are hitting.
(If you have a DEBUG OVMF boot log you could share, I can go through it myself)

#24 lore raw
Quoted 146 lines · Ard Biesheuvel
Quoted 124 lines · Tom Lendacky
Quoted 114 lines · Ard Biesheuvel
Quoted 108 lines · Tom Lendacky
Quoted 89 lines · Tom Lendacky
Quoted 48 lines · Ard Biesheuvel
Quoted 37 lines · Tom Lendacky
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Quoted 9 lines

I tried booting an SEV and an SEV-ES guest using this and both failed to boot:

EFI stub: WARNING: Decompression failed: Out of memory while allocating z_stream

I'll have to take a closer look as to why, but it might be a couple of days before I can get to it.

Quoted 5 lines

Thanks Tom.

The internal malloc() seems to be failing, which is often caused by BSS clearing problems. Could you elaborate a little bit on the boot environment you are using here?

I'm using Qemu v7.2.1 as my VMM, Linux 6.3 with your series applied for my host/hypervisor and guest kernel and the current OVMF tree built using OvmfPkgX64.dsc.

I was originally using the current merge window Linux, but moved to the release version just to . With the release version SEV and SEV-ES still fail to
boot, but SEV actually #GPs now. And some of the register contents look like encrypted data:

ConvertPages: range 1000000 - 4FA1FFF covers multiple entries !!!! X64 Exception Type - 0D(#GP - General Protection) CPU Apic ID - 00000000 !!!!
ExceptionData - 0000000000000000

RIP  - 00000000597E71C1, CS  - 0000000000000038, RFLAGS - 0000000000210206
RAX  - 1FBA02A45943B920, RCX - 0000000000AF7009, RDX - A9DAE761B64A1F1B
RBX  - 1FBA02A45943B8C0, RSP - 000000007FD97320, RBP - 0000000002000000
RSI  - 0000000001000000, RDI - 1FBA02A45943DE68
R8   - 0000000003EF3C94, R9  - 0000000000000000, R10 - 000000007D7C6018
R11  - 0000000000000000, R12 - 0000000001000000, R13 - 00000000597EDD98
R14  - 0000000001000000, R15 - 000000007E0A5198
DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
GS   - 0000000000000030, SS  - 0000000000000030
CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FA01000
CR4  - 0000000000000668, CR8 - 0000000000000000
DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400

GDTR - 000000007F7DC000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F34C018 0000000000000FFF, TR - 0000000000000000
FXSAVE_STATE - 000000007FD96F80
!!!! Find image based on IP(0x597E71C1) /root/kernels/ovmf-build-X64/Build/OvmfX64/DEBUG_GCC5/X64/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe/DEBUG/Variable RuntimeDxe.dll (ImageBase=0000000000D4792C, EntryPoint=0000000000D50CC3) !!!!

Quoted 3 lines

So, yes, probably an area of memory that was zeroes when mapped unencrypted, but wasn't cleared after changing the mapping to encrypted.

Yes, looks like a bss clearing issue. If I add __section(".data") to free_mem_ptr and free_mem_end_ptr in arch/x86/boot/compressed/misc.c and to malloc_ptr and malloc_cnt in include/linux/decompress/mm.h, then I can boot an SEV guest.

However, an SEV-ES guest is triple faulting. This looks to be because we're still on the EFI CS of 0x38 after switching GDTs in arch/x86/kernel/head_64.S by calling startup_64_setup_env(). Before switching to the kernel CS, we take a #VC (from CPUID calls in sme_enable) and things blow up on the iretq. Moving the block headed by the comment "Now switch to __KERNEL_CS so IRET works reliably" to just after calling startup_64_setup_env() fixes it and an SEV-ES guest can boot.

Quoted 4 lines

This worked before because I believe we switched off the EFI CS as part of the kernel decompressor support and so this bug wasn't exposed. But this needs to be fixed regardless of this series.

Quoted 7 lines

Very interesting. I was under the assumption that everything that goes on in sev_enable() in the decompressor would be rather indispensable to boot in SEV mode (which I only spotted today) so I am quite surprised that things just appear to work. (There is some 32-bit SEV code in the decompressor as well that obviously never gets called when booting in long mode, but I hadn't quite grasped how much other SEV code there actually is)

Quoted 7 lines

The sev_enable() function for SEV and SEV-ES is more for ensuring a proper environment (ensuring the proper CPUID bits are present for the guest, checking the GHCB protocol level, properly preparing pagetables, etc.). Since we're still in EFI and using the EFI #VC handler and pagetables, we don't require some of that stuff, but some of the it would need to be performed at some point (I wasn't trying to be a malicious hypervisor).

OK, good to know.

Quoted 10 lines

I haven't tested SEV-SNP yet because the hypervisor support is not upstream, yet, and I haven't applied your series to an SNP hypervisor tree, yet. There is a lot of support in sev_enable() for SNP that may likely cause a problem compared to SEV and SEV-ES.

SEV-SNP has a lot more checking and validation going on and it ensures it gets the confidential computing blob from EFI into the boot parameters. I'm not sure when I'll be able to test SNP, since I'm off next week and trying to wrap up a bunch of stuff before I leave.

Quoted 3 lines

Would it make sense, given the apparent direct dependency on EFI, to move that stuff into the EFI stub instead of doing it from the decompressor?

We tried to make the support for SNP not be reliant on EFI. If EFI is present, then get the confidential computing blob from it. Otherwise, the cc blob can be supplied as part of the setup data in the Linux boot protocol (see find_cc_block() in arch/x86/boot/compressed/sev.c).

So I guess some of it could be moved, but I'm not sure how much.

Quoted 7 lines

Thanks for giving this a spin - I can wait for more testing until you get back, and hopefully, we'll have converged a bit more by that time and there will be a new revision of the series available for testing.

BTW any clue whether your boot path is relying on the EFI handover protocol? I.e., QemuStartLegacyImage() in OVMF? Evgeny mentioned that

I don't believe it is. It's using the Grub EFI bootloader to load and boot Linux.

Quoted 3 lines

BSS needs clearing in that case, and I was wondering if that is the case you are hitting.
(If you have a DEBUG OVMF boot log you could share, I can go through it myself)

Sure, I'll send you a copy in a separate email.

Thanks,
Tom

author maintainer
#25 lore raw
Quoted 152 lines · Tom Lendacky
Quoted 146 lines · Ard Biesheuvel
Quoted 124 lines · Tom Lendacky
Quoted 114 lines · Ard Biesheuvel
Quoted 108 lines · Tom Lendacky
Quoted 89 lines · Tom Lendacky
Quoted 48 lines · Ard Biesheuvel
Quoted 37 lines · Tom Lendacky
Quoted 26 lines · Ard Biesheuvel

This series is conceptually a combination of Evgeny's series [0] and mine [1], both of which attempt to make the early decompressor code more amenable to executing in the EFI environment with stricter handling of memory permissions.

My series [1] implemented zboot for x86, by getting rid of the entire x86 decompressor, and replacing it with existing EFI code that does the same but in a generic way. The downside of this is that only EFI boot is supported, making it unviable for distros, which need to support BIOS boot and hybrid EFI boot modes that omit the EFI stub.

Evgeny's series [0] adapted the entire decompressor code flow to allow it to execute in the EFI context as well as the bare metal context, and this involves changes to the 1:1 mapping code and the page fault handlers etc, none of which are really needed when doing EFI boot in the first place.

So this series attempts to occupy the middle ground here: it makes minimal changes to the existing decompressor so some of it can be called from the EFI stub. Then, it reimplements the EFI boot flow to decompress the kernel and boot it directly, without relying on the trampoline code, page table code or page fault handling code. This allows us to get rid of quite a bit of unsavory EFI stub code, and replace it with two clear invocations of the EFI firmware APIs to clear NX restrictions from allocations that have been populated with executable code.

Quoted 8 lines

The only code that is being reused is the decompression library itself, along with the minimal ELF parsing that is required to copy the ELF segments in place, and the relocation processing that fixes up absolute symbol references to refer to the correct virtual addresses.

Note that some of Evgeny's changes to clean up the PE/COFF header generation will still be needed, but I've omitted those here for brevity.

Quoted 9 lines

I tried booting an SEV and an SEV-ES guest using this and both failed to boot:

EFI stub: WARNING: Decompression failed: Out of memory while allocating z_stream

I'll have to take a closer look as to why, but it might be a couple of days before I can get to it.

Quoted 5 lines

Thanks Tom.

The internal malloc() seems to be failing, which is often caused by BSS clearing problems. Could you elaborate a little bit on the boot environment you are using here?

I'm using Qemu v7.2.1 as my VMM, Linux 6.3 with your series applied for my host/hypervisor and guest kernel and the current OVMF tree built using OvmfPkgX64.dsc.

I was originally using the current merge window Linux, but moved to the release version just to . With the release version SEV and SEV-ES still fail to
boot, but SEV actually #GPs now. And some of the register contents look like encrypted data:

ConvertPages: range 1000000 - 4FA1FFF covers multiple entries !!!! X64 Exception Type - 0D(#GP - General Protection) CPU Apic ID - 00000000 !!!!
ExceptionData - 0000000000000000

RIP  - 00000000597E71C1, CS  - 0000000000000038, RFLAGS - 0000000000210206
RAX  - 1FBA02A45943B920, RCX - 0000000000AF7009, RDX - A9DAE761B64A1F1B
RBX  - 1FBA02A45943B8C0, RSP - 000000007FD97320, RBP - 0000000002000000
RSI  - 0000000001000000, RDI - 1FBA02A45943DE68
R8   - 0000000003EF3C94, R9  - 0000000000000000, R10 - 000000007D7C6018
R11  - 0000000000000000, R12 - 0000000001000000, R13 - 00000000597EDD98
R14  - 0000000001000000, R15 - 000000007E0A5198
DS   - 0000000000000030, ES  - 0000000000000030, FS  - 0000000000000030
GS   - 0000000000000030, SS  - 0000000000000030
CR0  - 0000000080010033, CR2 - 0000000000000000, CR3 - 000000007FA01000
CR4  - 0000000000000668, CR8 - 0000000000000000
DR0  - 0000000000000000, DR1 - 0000000000000000, DR2 - 0000000000000000
DR3  - 0000000000000000, DR6 - 00000000FFFF0FF0, DR7 - 0000000000000400

GDTR - 000000007F7DC000 0000000000000047, LDTR - 0000000000000000
IDTR - 000000007F34C018 0000000000000FFF, TR - 0000000000000000
FXSAVE_STATE - 000000007FD96F80
!!!! Find image based on IP(0x597E71C1) /root/kernels/ovmf-build-X64/Build/OvmfX64/DEBUG_GCC5/X64/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe/DEBUG/Variable RuntimeDxe.dll (ImageBase=0000000000D4792C, EntryPoint=0000000000D50CC3) !!!!

Quoted 3 lines

So, yes, probably an area of memory that was zeroes when mapped unencrypted, but wasn't cleared after changing the mapping to encrypted.

Yes, looks like a bss clearing issue. If I add __section(".data") to free_mem_ptr and free_mem_end_ptr in arch/x86/boot/compressed/misc.c and to malloc_ptr and malloc_cnt in include/linux/decompress/mm.h, then I can boot an SEV guest.

However, an SEV-ES guest is triple faulting. This looks to be because we're still on the EFI CS of 0x38 after switching GDTs in arch/x86/kernel/head_64.S by calling startup_64_setup_env(). Before switching to the kernel CS, we take a #VC (from CPUID calls in sme_enable) and things blow up on the iretq. Moving the block headed by the comment "Now switch to __KERNEL_CS so IRET works reliably" to just after calling startup_64_setup_env() fixes it and an SEV-ES guest can boot.

Quoted 4 lines

This worked before because I believe we switched off the EFI CS as part of the kernel decompressor support and so this bug wasn't exposed. But this needs to be fixed regardless of this series.

Quoted 7 lines

Very interesting. I was under the assumption that everything that goes on in sev_enable() in the decompressor would be rather indispensable to boot in SEV mode (which I only spotted today) so I am quite surprised that things just appear to work. (There is some 32-bit SEV code in the decompressor as well that obviously never gets called when booting in long mode, but I hadn't quite grasped how much other SEV code there actually is)

Quoted 7 lines

The sev_enable() function for SEV and SEV-ES is more for ensuring a proper environment (ensuring the proper CPUID bits are present for the guest, checking the GHCB protocol level, properly preparing pagetables, etc.). Since we're still in EFI and using the EFI #VC handler and pagetables, we don't require some of that stuff, but some of the it would need to be performed at some point (I wasn't trying to be a malicious hypervisor).

OK, good to know.

Quoted 10 lines

I haven't tested SEV-SNP yet because the hypervisor support is not upstream, yet, and I haven't applied your series to an SNP hypervisor tree, yet. There is a lot of support in sev_enable() for SNP that may likely cause a problem compared to SEV and SEV-ES.

SEV-SNP has a lot more checking and validation going on and it ensures it gets the confidential computing blob from EFI into the boot parameters. I'm not sure when I'll be able to test SNP, since I'm off next week and trying to wrap up a bunch of stuff before I leave.

Quoted 3 lines

Would it make sense, given the apparent direct dependency on EFI, to move that stuff into the EFI stub instead of doing it from the decompressor?

Quoted 7 lines

We tried to make the support for SNP not be reliant on EFI. If EFI is present, then get the confidential computing blob from it. Otherwise, the cc blob can be supplied as part of the setup data in the Linux boot protocol (see find_cc_block() in arch/x86/boot/compressed/sev.c).

So I guess some of it could be moved, but I'm not sure how much.

Fair enough.

Quoted 11 lines
Quoted 7 lines

Thanks for giving this a spin - I can wait for more testing until you get back, and hopefully, we'll have converged a bit more by that time and there will be a new revision of the series available for testing.

BTW any clue whether your boot path is relying on the EFI handover protocol? I.e., QemuStartLegacyImage() in OVMF? Evgeny mentioned that

I don't believe it is. It's using the Grub EFI bootloader to load and boot Linux.

Looking at the log you just sent (thanks), it appears you are using distro shim+grub, which implement their own rough approximation of PE file loading, and typically use the EFI handover protocol. This is good news, because we already know how broken shim and GRUB are, and I was concerned that there might be another issue to track down here.

I'll implement Evgeny's suggestion to clear BSS explicitly in the EFI handover protocol entrypoints for the next revision of the series.

Thanks,