Skip to contentSkip to conversation

x86: init improvements

PATCH In review Roger Pau Monne 2 participants 5 messages May 2, 2023 – May 3, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover x86: init improvements

Hello,

The following series contain two minor improvements for early boot: the first one is an alignment check when building the initial page tables, the second is a consistency fix for the GDT used by the BSP for the trampoline code.

Both are a result of some debugging work done on a system with broken firmware that resulted in Xen text not being loaded at a 2Mb aligned address. This resulted in corrupted page tables that would manifest as the ljmp from compatibility mode in trampoline_protmode_entry causing a triple fault due to the GDT being located in the Xen text section, and the page table entry for that address being corrupt because Xen was not loaded a 2Mb boundary.

The aim of the series (specially the first patch) is not to allow booting on such broken firmware, but to print an error message instead of causing a triple fault.

Thanks, Roger.

Roger Pau Monne (2):
x86/head: check base address alignment x86/trampoline: load the GDT located in the trampoline page

 xen/arch/x86/boot/head.S       | 14 ++++++++++++++
 xen/arch/x86/boot/trampoline.S |  3 +++
 2 files changed, 17 insertions(+)
Signature · 1 line
2.40.0
#1 lore raw

PATCH v2 1/2 x86/head: check base address alignment

Ensure that the base address is 2M aligned, or else the page table entries created would be corrupt as reserved bits on the PDE end up set.

We have encountered a broken firmware where grub2 would end up loading Xen at a non 2M aligned region when using the multiboot2 protocol, and that caused a very difficult to debug triple fault.

If the alignment is not as required by the page tables print an error message and stop the boot. Also add a build time check that the calculation of symbol offsets don't break alignment of passed addresses.

The check could be performed earlier, but so far the alignment is required by the page tables, and hence feels more natural that the check lives near to the piece of code that requires it.

Note that when booted as an EFI application from the PE entry point the alignment check is already performed by efi_arch_load_addr_check(), and hence there's no need to add another check at the point where page tables get built in efi_arch_memory_setup().

---
Changes since v1:
- Use test instead of and instruction.
- Add a build time check for sym_offs correctness.
- Reword part of the commit message.

---
 xen/arch/x86/boot/head.S | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
xen/arch/x86/boot/head.S +14 -0
index 0fb7dd3029f2..b9c9447df9df 100644
@@ -1,3 +1,4 @@
+#include <xen/lib.h>
 #include <xen/multiboot.h>
 #include <xen/multiboot2.h>
 #include <public/xen.h>
@@ -121,6 +122,7 @@ multiboot2_header:
 .Lbad_ldr_nst: .asciz "ERR: EFI SystemTable is not provided by bootloader!"
 .Lbad_ldr_nih: .asciz "ERR: EFI ImageHandle is not provided by bootloader!"
 .Lbad_efi_msg: .asciz "ERR: EFI IA-32 platforms are not supported!"
+.Lbag_alg_msg: .asciz "ERR: Xen must be loaded at a 2Mb boundary!"
 
         .section .init.data, "aw", @progbits
         .align 4
@@ -146,6 +148,9 @@ bad_cpu:
 not_multiboot:
         add     $sym_offs(.Lbad_ldr_msg),%esi   # Error message
         jmp     .Lget_vtb
+not_aligned:
+        add     $sym_offs(.Lbag_alg_msg),%esi   # Error message
+        jmp     .Lget_vtb
 .Lmb2_no_st:
         /*
          * Here we are on EFI platform. vga_text_buffer was zapped earlier
@@ -670,6 +675,15 @@ trampoline_setup:
         cmp     %edi, %eax
         jb      1b
 
+        .if !IS_ALIGNED(sym_offs(0), 1 << L2_PAGETABLE_SHIFT)
+        .error "Symbol offset calculation breaks alignment"
+        .endif
+
+        /* Check that the image base is aligned. */
+        lea     sym_esi(_start), %eax
+        test    $(1 << L2_PAGETABLE_SHIFT) - 1, %eax
+        jnz     not_aligned
+
         /* Map Xen into the higher mappings using 2M superpages. */
         lea     _PAGE_PSE + PAGE_HYPERVISOR_RWX + sym_esi(_start), %eax
         mov     $sym_offs(_start),   %ecx   /* %eax = PTE to write ^      */
Signature · 1 line
2.40.0
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] x86/head: check base address alignment

Quoted 17 lines · Roger Pau Monne

Ensure that the base address is 2M aligned, or else the page table entries created would be corrupt as reserved bits on the PDE end up set.

We have encountered a broken firmware where grub2 would end up loading Xen at a non 2M aligned region when using the multiboot2 protocol, and that caused a very difficult to debug triple fault.

If the alignment is not as required by the page tables print an error message and stop the boot. Also add a build time check that the calculation of symbol offsets don't break alignment of passed addresses.

The check could be performed earlier, but so far the alignment is required by the page tables, and hence feels more natural that the check lives near to the piece of code that requires it.

Quoted 7 lines

Note that when booted as an EFI application from the PE entry point the alignment check is already performed by efi_arch_load_addr_check(), and hence there's no need to add another check at the point where page tables get built in efi_arch_memory_setup().

Would you mind if, while committing, ...

xen/arch/x86/boot/head.S bad_cpu: · line 151

Quoted 5 lines
xen/arch/x86/boot/head.S +1 -0
@@ -146,6 +148,9 @@ bad_cpu:
 not_multiboot:
         add     $sym_offs(.Lbad_ldr_msg),%esi   # Error message
         jmp     .Lget_vtb
+not_aligned:

... a .L prefix was added to this label, bringing it out of sync with the earlier one, but in line with e.g. ...

xen/arch/x86/boot/head.S bad_cpu: · line 154

Quoted 3 lines
xen/arch/x86/boot/head.S +2 -0
+        add     $sym_offs(.Lbag_alg_msg),%esi   # Error message
+        jmp     .Lget_vtb
 .Lmb2_no_st:

... this one? I don't think the label is particularly useful to have in the symbol table (nor are not_multiboot and likely a few others).

Jan

#3 lore raw

Re: [PATCH v2 1/2] x86/head: check base address alignment

Quoted 39 lines · Jan Beulich
Quoted 17 lines · Roger Pau Monne

Ensure that the base address is 2M aligned, or else the page table entries created would be corrupt as reserved bits on the PDE end up set.

We have encountered a broken firmware where grub2 would end up loading Xen at a non 2M aligned region when using the multiboot2 protocol, and that caused a very difficult to debug triple fault.

If the alignment is not as required by the page tables print an error message and stop the boot. Also add a build time check that the calculation of symbol offsets don't break alignment of passed addresses.

The check could be performed earlier, but so far the alignment is required by the page tables, and hence feels more natural that the check lives near to the piece of code that requires it.

Quoted 7 lines

Note that when booted as an EFI application from the PE entry point the alignment check is already performed by efi_arch_load_addr_check(), and hence there's no need to add another check at the point where page tables get built in efi_arch_memory_setup().

Would you mind if, while committing, ...

xen/arch/x86/boot/head.S bad_cpu: · line 151

Quoted 5 lines
xen/arch/x86/boot/head.S +1 -0
@@ -146,6 +148,9 @@ bad_cpu:
 not_multiboot:
         add     $sym_offs(.Lbad_ldr_msg),%esi   # Error message
         jmp     .Lget_vtb
+not_aligned:

... a .L prefix was added to this label, bringing it out of sync with the earlier one, but in line with e.g. ...

Quoted 6 lines

xen/arch/x86/boot/head.S bad_cpu: · line 154

Quoted 3 lines
xen/arch/x86/boot/head.S +2 -0
+        add     $sym_offs(.Lbag_alg_msg),%esi   # Error message
+        jmp     .Lget_vtb
 .Lmb2_no_st:

... this one? I don't think the label is particularly useful to have in the symbol table (nor are not_multiboot and likely a few others).

Hm, right, yes, I don't think having those on the symbol table is helpful, please adjust.

Thanks, Roger.

#4 lore raw

PATCH v2 2/2 x86/trampoline: load the GDT located in the trampoline page

In reply to Roger Pau Monne

When booting the BSP the portion of the code executed from the trampoline page will be using the GDT located in the hypervisor .text.head section rather than the GDT located in the relocated trampoline page.

If skip_realmode is not set the GDT located in the trampoline page will be loaded after having executed the BIOS call, otherwise the GDT from .text.head will be used for all the protected mode trampoline code execution.

Note that both gdt_boot_descr and gdt_48 contain the same entries, but the former is located inside the hypervisor .text section, while the later lives in the relocated trampoline page.

This is not harmful as-is, as both GDTs contain the same entries, but for consistency with the APs switch the BSP trampoline code to also use the GDT on the relocated trampoline page.

---
Changes since v1:
- Reword comment.

---
 xen/arch/x86/boot/trampoline.S | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/x86/boot/trampoline.S +3 -0
index cdecf949b410..c6005fa33d1f 100644
@@ -164,6 +164,9 @@ GLOBAL(trampoline_cpu_started)
 
         .code32
 trampoline_boot_cpu_entry:
+        /* Switch to relocated trampoline GDT. */
+        lgdt    bootsym_rel(gdt_48, 4)
+
         cmpb    $0,bootsym_rel(skip_realmode,5)
         jnz     .Lskip_realmode
 
Signature · 1 line
2.40.0