Skip to contentSkip to conversation

x86/boot: Reduce assembly code

PATCH Superseded Frediano Ziglio 2 participants 15 messages Sep 10, 2024 – Sep 24, 2024

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover x86/boot: Reduce assembly code

This series came from part of the work of removing duplications between boot code and rewriting part of code from assembly to C. First 2 patches rework BIOS/PVH paths to reuse some code. Third patch rewrites EFI code in pure C.

Frediano Ziglio (3):
x86/boot: Initialise BSS as soon as possible
x86/boot: Refactor BIOS/PVH start
x86/boot: Rewrite EFI start part in C

 xen/arch/x86/boot/head.S      | 288 +++++++++++-----------------------
 xen/arch/x86/efi/Makefile     |   1 +
 xen/arch/x86/efi/parse-mbi2.c |  54 +++++++
 xen/arch/x86/efi/stub.c       |   3 +-
 4 files changed, 147 insertions(+), 199 deletions(-)
 create mode 100644 xen/arch/x86/efi/parse-mbi2.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 x86/boot: Initialise BSS as soon as possible

Allows to call C code earlier.

---
 xen/arch/x86/boot/head.S | 86 ++++++++++++++++++++--------------------
 1 file changed, 44 insertions(+), 42 deletions(-)
xen/arch/x86/boot/head.S +44 -42
index 12bbb97f33..de118d72f2 100644
@@ -231,6 +231,27 @@ __efi64_mb2_start:
         /* VGA is not available on EFI platforms. */
         movl   $0,vga_text_buffer(%rip)
 
+        /*
+         * Align the stack as UEFI spec requires. Keep it aligned
+         * before efi_multiboot2() call by pushing/popping even
+         * numbers of items on it.
+         */
+        and     $~15,%rsp
+
+        /*
+         * Initialize BSS (no nasty surprises!).
+         * It must be done earlier than in BIOS case
+         * because efi_multiboot2() touches it.
+         */
+        mov     %eax, %edx
+        lea     __bss_start(%rip), %edi
+        lea     __bss_end(%rip), %ecx
+        sub     %edi, %ecx
+        shr     $3, %ecx
+        xor     %eax, %eax
+        rep stosq
+        mov     %edx, %eax
+
         /* Check for Multiboot2 bootloader. */
         cmp     $MULTIBOOT2_BOOTLOADER_MAGIC,%eax
         je      .Lefi_multiboot2_proto
@@ -321,34 +342,12 @@ __efi64_mb2_start:
         lea     .Lmb2_no_ih(%rip),%r15
         jz      x86_32_switch
 
-        /*
-         * Align the stack as UEFI spec requires. Keep it aligned
-         * before efi_multiboot2() call by pushing/popping even
-         * numbers of items on it.
-         */
-        and     $~15,%rsp
-
         /* Save Multiboot2 magic on the stack. */
         push    %rax
 
         /* Save EFI ImageHandle on the stack. */
         push    %rdi
 
-        /*
-         * Initialize BSS (no nasty surprises!).
-         * It must be done earlier than in BIOS case
-         * because efi_multiboot2() touches it.
-         */
-        lea     __bss_start(%rip),%edi
-        lea     __bss_end(%rip),%ecx
-        sub     %edi,%ecx
-        shr     $3,%ecx
-        xor     %eax,%eax
-        rep stosq
-
-        /* Keep the stack aligned. Do not pop a single item off it. */
-        mov     (%rsp),%rdi
-
         /*
          * efi_multiboot2() is called according to System V AMD64 ABI:
          *   - IN:  %rdi - EFI ImageHandle, %rsi - EFI SystemTable,
@@ -434,6 +433,8 @@ __pvh_start:
         /* Set up stack. */
         lea     STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp
 
+        call    initialise_bss
+
         mov     %ebx, sym_esi(pvh_start_info_pa)
 
         /* Force xen console.  Will revert to user choice in init code. */
@@ -459,6 +460,25 @@ __pvh_start:
 
 #endif /* CONFIG_PVH_GUEST */
 
+initialise_bss:
+        /*
+         * Initialise the BSS.
+         *
+         * !!! WARNING - also zeroes the current stack !!!
+         */
+        pop     %ebp
+        mov     %eax, %edx
+
+        lea     sym_esi(__bss_start), %edi
+        lea     sym_esi(__bss_end), %ecx
+        sub     %edi, %ecx
+        xor     %eax, %eax
+        shr     $2, %ecx
+        rep stosl
+
+        mov     %edx, %eax
+        jmp     *%ebp
+
 __start:
         cld
         cli
@@ -489,6 +509,8 @@ __start:
         /* Set up stack. */
         lea     STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp
 
+        call    initialise_bss
+
         /* Bootloaders may set multiboot{1,2}.mem_lower to a nonzero value. */
         xor     %edx,%edx
 
@@ -645,26 +667,6 @@ trampoline_setup:
          * reserved for trampoline code and data.
          */
 
-        /*
-         * Do not zero BSS on EFI platform here.
-         * It was initialized earlier.
-         */
-        cmpb    $0, sym_esi(efi_platform)
-        jnz     1f
-
-        /*
-         * Initialise the BSS.
-         *
-         * !!! WARNING - also zeroes the current stack !!!
-         */
-        lea     sym_esi(__bss_start), %edi
-        lea     sym_esi(__bss_end), %ecx
-        sub     %edi,%ecx
-        xor     %eax,%eax
-        shr     $2,%ecx
-        rep stosl
-
-1:
         /* Interrogate CPU extended features via CPUID. */
         mov     $1, %eax
         cpuid
Signature · 1 line
2.34.1
#9 lore raw

PATCH 3/3 x86/boot: Rewrite EFI start part in C

In reply to Frediano Ziglio

No need to have it coded in assembly.

---
 xen/arch/x86/boot/head.S      | 131 ++++++----------------------------
 xen/arch/x86/efi/Makefile     |   1 +
 xen/arch/x86/efi/parse-mbi2.c |  54 ++++++++++++++
 xen/arch/x86/efi/stub.c       |   3 +-
 4 files changed, 80 insertions(+), 109 deletions(-)
 create mode 100644 xen/arch/x86/efi/parse-mbi2.c
xen/arch/x86/boot/head.S +23 -108
index a91413184c..4b05b564a0 100644
@@ -122,8 +122,6 @@ multiboot2_header:
 .Lbad_cpu_msg: .asciz "ERR: Not a 64-bit CPU!"
 .Lbad_ldr_msg: .asciz "ERR: Not a Multiboot bootloader!"
 .Lbad_ldr_nbs: .asciz "ERR: Bootloader shutdown EFI x64 boot services!"
-.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!"
 .Lno_nx_msg:   .asciz "ERR: Not an NX-capable CPU!"
@@ -162,17 +160,6 @@ early_error: /* Here to improve the disassembly. */
         mov     $sym_offs(.Lno_nx_msg), %ecx
         jmp     .Lget_vtb
 #endif
-.Lmb2_no_st:
-        /*
-         * Here we are on EFI platform. vga_text_buffer was zapped earlier
-         * because there is pretty good chance that VGA is unavailable.
-         */
-        mov     $sym_offs(.Lbad_ldr_nst), %ecx
-        jmp     .Lget_vtb
-.Lmb2_no_ih:
-        /* Ditto. */
-        mov     $sym_offs(.Lbad_ldr_nih), %ecx
-        jmp     .Lget_vtb
 .Lmb2_no_bs:
         /*
          * Ditto. Additionally, here there is a chance that Xen was started
@@ -190,6 +177,10 @@ early_error: /* Here to improve the disassembly. */
         mov     $sym_offs(.Lbad_efi_msg), %ecx
         xor     %edi,%edi                       # No VGA text buffer
         jmp     .Lprint_err
+.Ldirect_error:
+        mov     sym_esi(vga_text_buffer), %edi
+        mov     %eax, %esi
+        jmp     1f
 .Lget_vtb:
         mov     sym_esi(vga_text_buffer), %edi
 .Lprint_err:
@@ -255,34 +246,29 @@ __efi64_mb2_start:
         rep stosq
         mov     %edx, %eax
 
-        /* Check for Multiboot2 bootloader. */
-        cmp     $MULTIBOOT2_BOOTLOADER_MAGIC,%eax
-        je      .Lefi_multiboot2_proto
-
-        /* Jump to .Lnot_multiboot after switching CPU to x86_32 mode. */
-        lea     .Lnot_multiboot(%rip), %r15
-        jmp     x86_32_switch
+        /* Save Multiboot2 magic on the stack. */
+        push    %rax
 
-.Lefi_multiboot2_proto:
-        /* Zero EFI SystemTable, EFI ImageHandle addresses and cmdline. */
-        xor     %esi,%esi
-        xor     %edi,%edi
-        xor     %edx,%edx
+        /* Save Multiboot2 pointer on the stack. */
+        push    %rbx
 
-        /* Skip Multiboot2 information fixed part. */
-        lea     (MB2_fixed_sizeof+MULTIBOOT2_TAG_ALIGN-1)(%rbx),%ecx
-        and     $~(MULTIBOOT2_TAG_ALIGN-1),%ecx
+        /*
+         * efi_parse_mbi2() is called according to System V AMD64 ABI:
+         *   - IN:  %edi - Multiboot2 magic, %rsi - Multiboot2 pointer.
+         *   - OUT: %eax - error string.
+         */
+        mov     %eax, %edi
+        mov     %ebx, %esi
+        call    efi_parse_mbi2
+        test    %rax, %rax
+        lea     .Ldirect_error(%rip), %r15
+        jnz     x86_32_switch
 
-.Lefi_mb2_tsize:
-        /* Check Multiboot2 information total size. */
-        mov     %ecx,%r8d
-        sub     %ebx,%r8d
-        cmp     %r8d,MB2_fixed_total_size(%rbx)
-        jbe     .Lrun_bs
+        /* Restore Multiboot2 pointer. */
+        pop     %rbx
 
-        /* Are EFI boot services available? */
-        cmpl    $MULTIBOOT2_TAG_TYPE_EFI_BS,MB2_tag_type(%rcx)
-        jne     .Lefi_mb2_st
+        /* Restore Multiboot2 magic. */
+        pop     %rax
 
         /* We are on EFI platform and EFI boot services are available. */
         incb    efi_platform(%rip)
@@ -292,77 +278,6 @@ __efi64_mb2_start:
          * be run on EFI platforms.
          */
         incb    skip_realmode(%rip)
-        jmp     .Lefi_mb2_next_tag
-
-.Lefi_mb2_st:
-        /* Get EFI SystemTable address from Multiboot2 information. */
-        cmpl    $MULTIBOOT2_TAG_TYPE_EFI64,MB2_tag_type(%rcx)
-        cmove   MB2_efi64_st(%rcx),%rsi
-        je      .Lefi_mb2_next_tag
-
-        /* Get EFI ImageHandle address from Multiboot2 information. */
-        cmpl    $MULTIBOOT2_TAG_TYPE_EFI64_IH,MB2_tag_type(%rcx)
-        cmove   MB2_efi64_ih(%rcx),%rdi
-        je      .Lefi_mb2_next_tag
-
-        /* Get command line from Multiboot2 information. */
-        cmpl    $MULTIBOOT2_TAG_TYPE_CMDLINE, MB2_tag_type(%rcx)
-        jne     .Lno_cmdline
-        lea     MB2_tag_string(%rcx), %rdx
-        jmp     .Lefi_mb2_next_tag
-.Lno_cmdline:
-
-        /* Is it the end of Multiboot2 information? */
-        cmpl    $MULTIBOOT2_TAG_TYPE_END,MB2_tag_type(%rcx)
-        je      .Lrun_bs
-
-.Lefi_mb2_next_tag:
-        /* Go to next Multiboot2 information tag. */
-        add     MB2_tag_size(%rcx),%ecx
-        add     $(MULTIBOOT2_TAG_ALIGN-1),%ecx
-        and     $~(MULTIBOOT2_TAG_ALIGN-1),%ecx
-        jmp     .Lefi_mb2_tsize
-
-.Lrun_bs:
-        /* Are EFI boot services available? */
-        cmpb    $0,efi_platform(%rip)
-
-        /* Jump to .Lmb2_no_bs after switching CPU to x86_32 mode. */
-        lea     .Lmb2_no_bs(%rip),%r15
-        jz      x86_32_switch
-
-        /* Is EFI SystemTable address provided by boot loader? */
-        test    %rsi,%rsi
-
-        /* Jump to .Lmb2_no_st after switching CPU to x86_32 mode. */
-        lea     .Lmb2_no_st(%rip),%r15
-        jz      x86_32_switch
-
-        /* Is EFI ImageHandle address provided by boot loader? */
-        test    %rdi,%rdi
-
-        /* Jump to .Lmb2_no_ih after switching CPU to x86_32 mode. */
-        lea     .Lmb2_no_ih(%rip),%r15
-        jz      x86_32_switch
-
-        /* Save Multiboot2 magic on the stack. */
-        push    %rax
-
-        /* Save EFI ImageHandle on the stack. */
-        push    %rdi
-
-        /*
-         * efi_multiboot2() is called according to System V AMD64 ABI:
-         *   - IN:  %rdi - EFI ImageHandle, %rsi - EFI SystemTable,
-         *          %rdx - MB2 cmdline
-         */
-        call    efi_multiboot2
-
-        /* Just pop an item from the stack. */
-        pop     %rax
-
-        /* Restore Multiboot2 magic. */
-        pop     %rax
 
         /* Jump to trampoline_setup after switching CPU to x86_32 mode. */
         lea     trampoline_setup(%rip),%r15
xen/arch/x86/efi/Makefile +1 -0
index 24dfecfad1..0bbb29dc0e 100644
@@ -13,6 +13,7 @@ $(addprefix $(obj)/,$(EFIOBJ-y)): CFLAGS_stack_boundary := $(cflags-stack-bounda
 
 obj-y := common-stub.o stub.o
 obj-$(XEN_BUILD_EFI) := $(filter-out %.init.o,$(EFIOBJ-y))
+obj-y += parse-mbi2.o
 obj-bin-$(XEN_BUILD_EFI) := $(filter %.init.o,$(EFIOBJ-y))
 extra-$(XEN_BUILD_EFI) += buildid.o relocs-dummy.o
 nocov-$(XEN_BUILD_EFI) += stub.o
xen/arch/x86/efi/parse-mbi2.c new file +54 -0

54 lines not on this page · raw message

xen/arch/x86/efi/stub.c +2 -1
index 2cd5c8d4dc..27d40964d5 100644
@@ -17,7 +17,8 @@
  */
 
 void __init noreturn efi_multiboot2(EFI_HANDLE ImageHandle,
-                                    EFI_SYSTEM_TABLE *SystemTable)
+                                    EFI_SYSTEM_TABLE *SystemTable,
+                                    const char *cmdline)
 {
     static const CHAR16 __initconst err[] =
         L"Xen does not have EFI code build in!\r\nSystem halted!\r\n";
Signature · 1 line
2.34.1
#5 lore raw

PATCH 2/3 x86/boot: Refactor BIOS/PVH start

In reply to Frediano Ziglio

The 2 code paths were sharing quite some common code, reuse it instead of having duplications.
Use %ebp register to store boot type before running common code.

---
 xen/arch/x86/boot/head.S | 113 +++++++++++++++------------------------
 1 file changed, 44 insertions(+), 69 deletions(-)
xen/arch/x86/boot/head.S +44 -69
index de118d72f2..a91413184c 100644
@@ -25,6 +25,9 @@
 #define MB2_HT(name)      (MULTIBOOT2_HEADER_TAG_##name)
 #define MB2_TT(name)      (MULTIBOOT2_TAG_TYPE_##name)
 
+#define BOOT_TYPE_BIOS 1
+#define BOOT_TYPE_PVH 2
+
         .macro mb2ht_args arg:req, args:vararg
         .long \arg
         .ifnb \args
@@ -409,13 +412,28 @@ cs32_switch:
 ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .long sym_offs(__pvh_start))
 
 __pvh_start:
-        cld
         cli
+        mov     $BOOT_TYPE_PVH, %ebp
+        jmp     common_bios_pvh
+#endif /* CONFIG_PVH_GUEST */
+
+__start:
+        cli
+        mov     $BOOT_TYPE_BIOS, %ebp
+
+common_bios_pvh:
+        cld
 
         /*
-         * We need one call (i.e. push) to determine the load address.  See
-         * __start for a discussion on how to do this safely using the PVH
-         * info structure.
+         * Multiboot (both 1 and 2) and PVH specify the stack pointer as
+         * undefined.  This is unhelpful for relocatable images, where one
+         * call (i.e. push) is required to calculate the image's load address.
+         *
+         * Durig BIOS boot, there is one area of memory we know about with
+         * reasonable confidence that it isn't overlapped by Xen, and that's
+         * the Multiboot info structure in %ebx.  Use it as a temporary stack.
+         *
+         * During PVH boot use info structure in %ebx.
          */
 
         /* Preserve the field we're about to clobber. */
@@ -433,14 +451,9 @@ __pvh_start:
         /* Set up stack. */
         lea     STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp
 
-        call    initialise_bss
-
-        mov     %ebx, sym_esi(pvh_start_info_pa)
-
-        /* Force xen console.  Will revert to user choice in init code. */
-        movb    $-1, sym_esi(opt_console_xen)
-
-        /* Prepare gdt and segments */
+        /*
+         * Initialize GDTR and basic data segments.
+         */
         add     %esi, sym_esi(gdt_boot_base)
         lgdt    sym_esi(gdt_boot_descr)
 
@@ -449,67 +462,44 @@ __pvh_start:
         mov     %ecx, %es
         mov     %ecx, %ss
 
-        /* Skip bootloader setup and bios setup, go straight to trampoline */
-        movb    $1, sym_esi(pvh_boot)
-        movb    $1, sym_esi(skip_realmode)
-
-        /* Set trampoline_phys to use mfn 1 to avoid having a mapping at VA 0 */
-        movw    $0x1000, sym_esi(trampoline_phys)
-        mov     (%ebx), %eax /* mov $XEN_HVM_START_MAGIC_VALUE, %eax */
-        jmp     trampoline_setup
-
-#endif /* CONFIG_PVH_GUEST */
+        /* Load null descriptor to unused segment registers. */
+        xor     %ecx, %ecx
+        mov     %ecx, %fs
+        mov     %ecx, %gs
 
-initialise_bss:
         /*
          * Initialise the BSS.
          *
          * !!! WARNING - also zeroes the current stack !!!
          */
-        pop     %ebp
         mov     %eax, %edx
-
         lea     sym_esi(__bss_start), %edi
         lea     sym_esi(__bss_end), %ecx
         sub     %edi, %ecx
         xor     %eax, %eax
         shr     $2, %ecx
         rep stosl
-
         mov     %edx, %eax
-        jmp     *%ebp
-
-__start:
-        cld
-        cli
 
-        /*
-         * Multiboot (both 1 and 2) specify the stack pointer as undefined
-         * when entering in BIOS circumstances.  This is unhelpful for
-         * relocatable images, where one call (i.e. push) is required to
-         * calculate the image's load address.
-         *
-         * This early in boot, there is one area of memory we know about with
-         * reasonable confidence that it isn't overlapped by Xen, and that's
-         * the Multiboot info structure in %ebx.  Use it as a temporary stack.
-         */
-
-        /* Preserve the field we're about to clobber. */
-        mov     (%ebx), %edx
-        lea     4(%ebx), %esp
+#ifdef CONFIG_PVH_GUEST
+        cmp     $BOOT_TYPE_PVH, %ebp
+        jne     1f
 
-        /* Calculate the load base address. */
-        call    1f
-1:      pop     %esi
-        sub     $sym_offs(1b), %esi
+        mov     %ebx, sym_esi(pvh_start_info_pa)
 
-        /* Restore the clobbered field. */
-        mov     %edx, (%ebx)
+        /* Force xen console.  Will revert to user choice in init code. */
+        movb    $-1, sym_esi(opt_console_xen)
 
-        /* Set up stack. */
-        lea     STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp
+        /* Skip bootloader setup and bios setup, go straight to trampoline */
+        movb    $1, sym_esi(pvh_boot)
+        movb    $1, sym_esi(skip_realmode)
 
-        call    initialise_bss
+        /* Set trampoline_phys to use mfn 1 to avoid having a mapping at VA 0 */
+        movw    $0x1000, sym_esi(trampoline_phys)
+        mov     (%ebx), %eax /* mov $XEN_HVM_START_MAGIC_VALUE, %eax */
+        jmp     trampoline_setup
+1:
+#endif /* CONFIG_PVH_GUEST */
 
         /* Bootloaders may set multiboot{1,2}.mem_lower to a nonzero value. */
         xor     %edx,%edx
@@ -568,22 +558,7 @@ __start:
 trampoline_bios_setup:
         /*
          * Called on legacy BIOS platforms only.
-         *
-         * Initialize GDTR and basic data segments.
          */
-        add     %esi,sym_esi(gdt_boot_base)
-        lgdt    sym_esi(gdt_boot_descr)
-
-        mov     $BOOT_DS,%ecx
-        mov     %ecx,%ds
-        mov     %ecx,%es
-        mov     %ecx,%ss
-        /* %esp is initialized later. */
-
-        /* Load null descriptor to unused segment registers. */
-        xor     %ecx,%ecx
-        mov     %ecx,%fs
-        mov     %ecx,%gs
 
         /* Set up trampoline segment 64k below EBDA */
         movzwl  0x40e,%ecx          /* EBDA segment */
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH 2/3] x86/boot: Refactor BIOS/PVH start

xen/arch/x86/boot/head.S line 29

Quoted 8 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +2 -0
@@ -25,6 +25,9 @@
 #define MB2_HT(name)      (MULTIBOOT2_HEADER_TAG_##name)
 #define MB2_TT(name)      (MULTIBOOT2_TAG_TYPE_##name)
 
+#define BOOT_TYPE_BIOS 1
+#define BOOT_TYPE_PVH 2

Did you consider using 0 and 1, to be able to use XOR on the BIOS path and TEST for checking?

Quoted 5 lines
xen/arch/x86/boot/head.S +0 -1
@@ -409,13 +412,28 @@ cs32_switch:
 ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .long sym_offs(__pvh_start))
 
 __pvh_start:
-        cld

xen/arch/x86/boot/head.S cs32_switch: · line 424

Quoted 10 lines
xen/arch/x86/boot/head.S +9 -0
         cli
+        mov     $BOOT_TYPE_PVH, %ebp
+        jmp     common_bios_pvh
+#endif /* CONFIG_PVH_GUEST */
+
+__start:
+        cli
+        mov     $BOOT_TYPE_BIOS, %ebp
+
+common_bios_pvh:

I think labels like this one don't need to show up in the symbol table, and hence would better start with .L.

xen/arch/x86/boot/head.S cs32_switch: · line 425

Quoted 1 line
xen/arch/x86/boot/head.S +1 -0
+        cld

So you fold the STDs but not the STIs, despite that not even having been first on the PVH path. This decision wants explaining in the description, even if just briefly.

xen/arch/x86/boot/head.S __pvh_start: · line 436

Quoted 5 lines
xen/arch/x86/boot/head.S +0 -1
@@ -433,14 +451,9 @@ __pvh_start:
         /* Set up stack. */
         lea     STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp
 
-        call    initialise_bss

I'm little puzzled: The previous patch moved it "as early as possible" just for it to be moved to a later point again here?

xen/arch/x86/boot/head.S __pvh_start: · line 458

Quoted 11 lines
xen/arch/x86/boot/head.S +3 -6
-        mov     %ebx, sym_esi(pvh_start_info_pa)
-
-        /* Force xen console.  Will revert to user choice in init code. */
-        movb    $-1, sym_esi(opt_console_xen)
-
-        /* Prepare gdt and segments */
+        /*
+         * Initialize GDTR and basic data segments.
+         */
         add     %esi, sym_esi(gdt_boot_base)
         lgdt    sym_esi(gdt_boot_descr)

No real need to change the comment? In any even it wants to remain a single-line one.

Quoted 8 lines
xen/arch/x86/boot/head.S +0 -4
@@ -449,67 +462,44 @@ __pvh_start:
         mov     %ecx, %es
         mov     %ecx, %ss
 
-        /* Skip bootloader setup and bios setup, go straight to trampoline */
-        movb    $1, sym_esi(pvh_boot)
-        movb    $1, sym_esi(skip_realmode)
-

xen/arch/x86/boot/head.S __pvh_start: · line 468

Quoted 10 lines
xen/arch/x86/boot/head.S +4 -6
-        /* Set trampoline_phys to use mfn 1 to avoid having a mapping at VA 0 */
-        movw    $0x1000, sym_esi(trampoline_phys)
-        mov     (%ebx), %eax /* mov $XEN_HVM_START_MAGIC_VALUE, %eax */
-        jmp     trampoline_setup
-
-#endif /* CONFIG_PVH_GUEST */
+        /* Load null descriptor to unused segment registers. */
+        xor     %ecx, %ecx
+        mov     %ecx, %fs
+        mov     %ecx, %gs

The comment said "descriptor", yes, but it's a null selector that we load here. Perhaps worth adjusting as the comment is moved.

Quoted 51 lines
xen/arch/x86/boot/head.S +6 -29
-initialise_bss:
         /*
          * Initialise the BSS.
          *
          * !!! WARNING - also zeroes the current stack !!!
          */
-        pop     %ebp
         mov     %eax, %edx
-
         lea     sym_esi(__bss_start), %edi
         lea     sym_esi(__bss_end), %ecx
         sub     %edi, %ecx
         xor     %eax, %eax
         shr     $2, %ecx
         rep stosl
-
         mov     %edx, %eax
-        jmp     *%ebp
-
-__start:
-        cld
-        cli
 
-        /*
-         * Multiboot (both 1 and 2) specify the stack pointer as undefined
-         * when entering in BIOS circumstances.  This is unhelpful for
-         * relocatable images, where one call (i.e. push) is required to
-         * calculate the image's load address.
-         *
-         * This early in boot, there is one area of memory we know about with
-         * reasonable confidence that it isn't overlapped by Xen, and that's
-         * the Multiboot info structure in %ebx.  Use it as a temporary stack.
-         */
-
-        /* Preserve the field we're about to clobber. */
-        mov     (%ebx), %edx
-        lea     4(%ebx), %esp
+#ifdef CONFIG_PVH_GUEST
+        cmp     $BOOT_TYPE_PVH, %ebp
+        jne     1f
 
-        /* Calculate the load base address. */
-        call    1f
-1:      pop     %esi
-        sub     $sym_offs(1b), %esi
+        mov     %ebx, sym_esi(pvh_start_info_pa)
 
-        /* Restore the clobbered field. */
-        mov     %edx, (%ebx)
+        /* Force xen console.  Will revert to user choice in init code. */
+        movb    $-1, sym_esi(opt_console_xen)

xen/arch/x86/boot/head.S __pvh_start: · line 498

Quoted 10 lines
xen/arch/x86/boot/head.S +5 -3
 
-        /* Set up stack. */
-        lea     STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp
+        /* Skip bootloader setup and bios setup, go straight to trampoline */
+        movb    $1, sym_esi(pvh_boot)
+        movb    $1, sym_esi(skip_realmode)
 
-        call    initialise_bss
+        /* Set trampoline_phys to use mfn 1 to avoid having a mapping at VA 0 */
+        movw    $0x1000, sym_esi(trampoline_phys)

I realize you merely move this, yet I question the use of MOVW here. You use 32-bit operations e.g. to set %ebp; perhaps the same should be done here, even if that means a 1 byte code size increase. In any even this would preferably use PAGE_SIZE imo.

Jan

#2 lore raw

Re: [PATCH 1/3] x86/boot: Initialise BSS as soon as possible

In reply to Frediano Ziglio

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 239

Quoted 12 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +6 -0
@@ -231,6 +231,27 @@ __efi64_mb2_start:
         /* VGA is not available on EFI platforms. */
         movl   $0,vga_text_buffer(%rip)
 
+        /*
+         * Align the stack as UEFI spec requires. Keep it aligned
+         * before efi_multiboot2() call by pushing/popping even
+         * numbers of items on it.
+         */
+        and     $~15,%rsp

You don't use the stack below, so it's not clear if/why this needs moving. If it does, please add the missing blank after the comma (like you nicely do everywhere else).

Apart from this there's the question on the precise placement of the calls - see respective comment on patch 2 (which I needed to look at first to have an opinion here).

Jan

#10 lore raw

Re: [PATCH 3/3] x86/boot: Rewrite EFI start part in C

In reply to Frediano Ziglio
Quoted 1 line · Frediano Ziglio

No need to have it coded in assembly.

As to the title: It's the EFI/MB2 case you re-write. That wants reflecting there, as the "normal" EFI start part is all C already anyway. I also think you mean "partly"?

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 247

Quoted 3 lines
xen/arch/x86/boot/head.S +0 -0
@@ -255,34 +246,29 @@ __efi64_mb2_start:
         rep stosq
         mov     %edx, %eax

This can be dropped then, by making ...

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 250

Quoted 9 lines
xen/arch/x86/boot/head.S +2 -7
-        /* Check for Multiboot2 bootloader. */
-        cmp     $MULTIBOOT2_BOOTLOADER_MAGIC,%eax
-        je      .Lefi_multiboot2_proto
-
-        /* Jump to .Lnot_multiboot after switching CPU to x86_32 mode. */
-        lea     .Lnot_multiboot(%rip), %r15
-        jmp     x86_32_switch
+        /* Save Multiboot2 magic on the stack. */
+        push    %rax

... this use %rdx.

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 253

Quoted 7 lines
xen/arch/x86/boot/head.S +2 -5
-.Lefi_multiboot2_proto:
-        /* Zero EFI SystemTable, EFI ImageHandle addresses and cmdline. */
-        xor     %esi,%esi
-        xor     %edi,%edi
-        xor     %edx,%edx
+        /* Save Multiboot2 pointer on the stack. */
+        push    %rbx

%rbx doesn't need preserving around a C function call (which will do so itself if necessary). I understand a 2nd PUSH may be necessary anyway, to keep the stack aligned, yet that then would need commenting differently. Plus as long as we call our own functions only, we don't require such alignment.

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 258

Quoted 7 lines
xen/arch/x86/boot/head.S +4 -3
-        /* Skip Multiboot2 information fixed part. */
-        lea     (MB2_fixed_sizeof+MULTIBOOT2_TAG_ALIGN-1)(%rbx),%ecx
-        and     $~(MULTIBOOT2_TAG_ALIGN-1),%ecx
+        /*
+         * efi_parse_mbi2() is called according to System V AMD64 ABI:
+         *   - IN:  %edi - Multiboot2 magic, %rsi - Multiboot2 pointer.
+         *   - OUT: %eax - error string.

Nit: %rax according to the code below.

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 261

Quoted 3 lines
xen/arch/x86/boot/head.S +3 -0
+         */
+        mov     %eax, %edi
+        mov     %ebx, %esi

This latter one would better be a 64-bit MOV, for it being a pointer?

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 265

Quoted 4 lines
xen/arch/x86/boot/head.S +4 -0
+        call    efi_parse_mbi2
+        test    %rax, %rax
+        lea     .Ldirect_error(%rip), %r15
+        jnz     x86_32_switch

As requested by Andrew in a related context: LEA first please to follow the pattern allowing macro fusion, even if here it is less because of performance concerns but more to avoid giving a bad example.

xen/arch/x86/efi/Makefile $(addprefix $(obj)/,$(EFIOBJ-y)): CFLAGS_stack_boundary := $(cflags-stack-bounda · line 16

Quoted 7 lines
xen/arch/x86/efi/Makefile +1 -0
@@ -13,6 +13,7 @@ $(addprefix $(obj)/,$(EFIOBJ-y)): CFLAGS_stack_boundary := $(cflags-stack-bounda
 
 obj-y := common-stub.o stub.o
 obj-$(XEN_BUILD_EFI) := $(filter-out %.init.o,$(EFIOBJ-y))
+obj-y += parse-mbi2.o

obj-bin-y I suppose, for it all being __init / __initdata, and hence the string literals in particular also wanting to move to .init.rodata.

Quoted 5 lines
xen/arch/x86/efi/parse-mbi2.c new file +2 -0
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+

xen/arch/x86/efi/parse-mbi2.c line 12

Quoted 10 lines
xen/arch/x86/efi/parse-mbi2.c new file +10 -0
+#include <xen/efi.h>
+#include <xen/init.h>
+#include <xen/multiboot2.h>
+#include <asm/asm_defns.h>
+#include <asm/efibind.h>
+#include <efi/efidef.h>
+#include <efi/eficapsule.h>
+#include <efi/eficon.h>
+#include <efi/efidevp.h>
+#include <efi/efiapi.h>

I don't think all of these are needed? Please limit #include-s to just what is actually used.

xen/arch/x86/efi/parse-mbi2.c line 16

Quoted 3 lines
xen/arch/x86/efi/parse-mbi2.c new file +3 -0
+void asmlinkage __init efi_multiboot2(EFI_HANDLE ImageHandle,
+                                      EFI_SYSTEM_TABLE *SystemTable,
+                                      const char *cmdline);

This i now solely called from C code and hence shouldn't be asmlinkage.

xen/arch/x86/efi/parse-mbi2.c line 18

Quoted 1 line
xen/arch/x86/efi/parse-mbi2.c new file +1 -0
+const char *__init efi_parse_mbi2(uint32_t magic, const multiboot2_fixed_t *mbi)

Whereas this, called from assembly code and not having / needing a declaration, should be.

xen/arch/x86/efi/parse-mbi2.c line 27

Quoted 9 lines
xen/arch/x86/efi/parse-mbi2.c new file +9 -0
+{
+    const multiboot2_tag_t *tag;
+    EFI_HANDLE ImageHandle = NULL;
+    EFI_SYSTEM_TABLE *SystemTable = NULL;
+    const char *cmdline = NULL;
+    bool have_bs = false;
+
+    if ( magic != MULTIBOOT2_BOOTLOADER_MAGIC )
+        return "ERR: Not a Multiboot bootloader!";

Assembly code merely re-used a message. Now that it separate, please make it say "Multiboot2".

xen/arch/x86/efi/parse-mbi2.c line 30

Quoted 2 lines
xen/arch/x86/efi/parse-mbi2.c new file +2 -0
+    /* Skip Multiboot2 information fixed part. */
+    for ( tag = _p(ROUNDUP((unsigned long)(mbi + 1), MULTIBOOT2_TAG_ALIGN));

The comment is placed as if it applied to the entire loop. It wants to move inside the for() to make clear it only applies to the loop setup.

xen/arch/x86/efi/parse-mbi2.c line 32

Quoted 2 lines
xen/arch/x86/efi/parse-mbi2.c new file +2 -0
+          (void *)tag - (void *)mbi < mbi->total_size && tag->type != MULTIBOOT2_TAG_TYPE_END;
+          tag = _p(ROUNDUP((unsigned long)((void *)tag + tag->size), MULTIBOOT2_TAG_ALIGN)) )

Now that this is done in C, I think the checking wants to be more thorough, to no only make sure the start of a sub-struct is within the specified size, but all of it (se we won't even access past ->total_size).

Further looks like there's a line length issue here.

Also please don't cast away const-ness from pointers.

Quoted 9 lines
xen/arch/x86/efi/parse-mbi2.c new file +9 -0
+    {
+        if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI_BS )
+            have_bs = true;
+        else if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI64 )
+            SystemTable = _p(((const multiboot2_tag_efi64_t *)tag)->pointer);
+        else if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI64_IH )
+            ImageHandle = _p(((const multiboot2_tag_efi64_ih_t *)tag)->pointer);
+        else if ( tag->type == MULTIBOOT2_TAG_TYPE_CMDLINE )
+            cmdline = ((const multiboot2_tag_string_t *)tag)->string;

xen/arch/x86/efi/parse-mbi2.c line 51

Quoted 10 lines
xen/arch/x86/efi/parse-mbi2.c new file +10 -0
+    }
+
+    if ( !have_bs )
+        return "ERR: Bootloader shutdown EFI x64 boot services!";
+    if ( !SystemTable )
+        return "ERR: EFI SystemTable is not provided by bootloader!";
+    if ( !ImageHandle )
+        return "ERR: EFI ImageHandle is not provided by bootloader!";
+
+    efi_multiboot2(ImageHandle, SystemTable, cmdline);

This being invoked from here now makes me wonder about the (new) function's name and whether a separate new function is actually
needed: Can't the new code then be integrated right into efi_multiboot2(), thus eliminating the question on the naming of the function?

xen/arch/x86/efi/stub.c line 24

Quoted 12 lines
xen/arch/x86/efi/stub.c +2 -1
@@ -17,7 +17,8 @@
  */
 
 void __init noreturn efi_multiboot2(EFI_HANDLE ImageHandle,
-                                    EFI_SYSTEM_TABLE *SystemTable)
+                                    EFI_SYSTEM_TABLE *SystemTable,
+                                    const char *cmdline)
 {
     static const CHAR16 __initconst err[] =
         L"Xen does not have EFI code build in!\r\nSystem halted!\r\n";

This, if not a separate change, wants mentioning in the description. It's a related observation that this wasn't properly updated, but nothing that necessarily needs doing here. Question is whether the declaration of the function wouldn't better go into a header now in the first place.

Jan

#3 lore raw

Re: [PATCH 1/3] x86/boot: Initialise BSS as soon as possible

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich
Quoted 6 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +0 -0
@@ -231,6 +231,27 @@ __efi64_mb2_start:
         /* VGA is not available on EFI platforms. */
         movl   $0,vga_text_buffer(%rip)
 
Quoted 11 lines

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 239

Quoted 6 lines
xen/arch/x86/boot/head.S +6 -0
+        /*
+         * Align the stack as UEFI spec requires. Keep it aligned
+         * before efi_multiboot2() call by pushing/popping even
+         * numbers of items on it.
+         */
+        and     $~15,%rsp

You don't use the stack below, so it's not clear if/why this needs moving. If it does, please add the missing blank after the comma (like you nicely do everywhere else).

Fixed the blank. The reason is more clear if you look at the last commit in the series, at least for the EFI part. For the BIOS/PVH part is less clear, but the rationale is the same. The commit came from a larger series where BIOS/PVH is mainly written in C so there is more clear.

Quoted 4 lines

Apart from this there's the question on the precise placement of the calls - see respective comment on patch 2 (which I needed to look at first to have an opinion here).

I think you removed too much context, not clear what code you are referring to, last hunk above is an "and" instruction.

Quoted 1 line

Jan

Frediano

#7 lore raw

Re: [PATCH 2/3] x86/boot: Refactor BIOS/PVH start

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich
Quoted 6 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +0 -0
@@ -25,6 +25,9 @@
 #define MB2_HT(name)      (MULTIBOOT2_HEADER_TAG_##name)
 #define MB2_TT(name)      (MULTIBOOT2_TAG_TYPE_##name)
 
Quoted 6 lines

xen/arch/x86/boot/head.S line 29

Quoted 2 lines
xen/arch/x86/boot/head.S +2 -0
+#define BOOT_TYPE_BIOS 1
+#define BOOT_TYPE_PVH 2

Did you consider using 0 and 1, to be able to use XOR on the BIOS path and TEST for checking?

Not clear what you are trying to achieve. Fewer bytes using the XOR? I think the TEST in this case is only reducing readability, it's an enumeration.
If you are concerns about code size I would use an 8 bit register (I would say DL) and use EBP register to temporary save EAX, 8 bit registers have usually tiny instructions, MOV has same size as XOR you mentioned not loosing any readability or forcing to change values.

Quoted 19 lines
Quoted 5 lines
xen/arch/x86/boot/head.S +0 -1
@@ -409,13 +412,28 @@ cs32_switch:
 ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .long sym_offs(__pvh_start))
 
 __pvh_start:
-        cld

xen/arch/x86/boot/head.S cs32_switch: · line 424

Quoted 10 lines
xen/arch/x86/boot/head.S +9 -0
         cli
+        mov     $BOOT_TYPE_PVH, %ebp
+        jmp     common_bios_pvh
+#endif /* CONFIG_PVH_GUEST */
+
+__start:
+        cli
+        mov     $BOOT_TYPE_BIOS, %ebp
+
+common_bios_pvh:

I think labels like this one don't need to show up in the symbol table, and hence would better start with .L.

Done

Quoted 6 lines

xen/arch/x86/boot/head.S cs32_switch: · line 425

Quoted 1 line
xen/arch/x86/boot/head.S +1 -0
+        cld

So you fold the STDs but not the STIs, despite that not even having been first on the PVH path. This decision wants explaining in the description, even if just briefly.

Just in case, I disable interrupts ASAP. Not that this should change much the result.
Would you prefer to fold it too?
By "description" do you mean having an additional comment in the code or something in the commit message?

Quoted 9 lines

xen/arch/x86/boot/head.S __pvh_start: · line 436

Quoted 5 lines
xen/arch/x86/boot/head.S +0 -1
@@ -433,14 +451,9 @@ __pvh_start:
         /* Set up stack. */
         lea     STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp
 
-        call    initialise_bss

I'm little puzzled: The previous patch moved it "as early as possible" just for it to be moved to a later point again here?

The rationale is being able to use C, for that you need a stack, correct segments and BSS.
Are you suggesting any change?

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/boot/head.S +0 -6
-        mov     %ebx, sym_esi(pvh_start_info_pa)
-
-        /* Force xen console.  Will revert to user choice in init code. */
-        movb    $-1, sym_esi(opt_console_xen)
-
-        /* Prepare gdt and segments */
Quoted 9 lines

xen/arch/x86/boot/head.S __pvh_start: · line 458

Quoted 5 lines
xen/arch/x86/boot/head.S +3 -0
+        /*
+         * Initialize GDTR and basic data segments.
+         */
         add     %esi, sym_esi(gdt_boot_base)
         lgdt    sym_esi(gdt_boot_descr)

No real need to change the comment? In any even it wants to remain a single-line one.

It's not changed, it's the exact comment from the second copy of this code (the BIOS path).
I think this comment is more clear than the first, I'll change to be one line.

Quoted 13 lines
Quoted 13 lines
xen/arch/x86/boot/head.S +0 -9
@@ -449,67 +462,44 @@ __pvh_start:
         mov     %ecx, %es
         mov     %ecx, %ss
 
-        /* Skip bootloader setup and bios setup, go straight to trampoline */
-        movb    $1, sym_esi(pvh_boot)
-        movb    $1, sym_esi(skip_realmode)
-
-        /* Set trampoline_phys to use mfn 1 to avoid having a mapping at VA 0 */
-        movw    $0x1000, sym_esi(trampoline_phys)
-        mov     (%ebx), %eax /* mov $XEN_HVM_START_MAGIC_VALUE, %eax */
-        jmp     trampoline_setup
-
Quoted 9 lines

xen/arch/x86/boot/head.S __pvh_start: · line 468

Quoted 5 lines
xen/arch/x86/boot/head.S +4 -1
-#endif /* CONFIG_PVH_GUEST */
+        /* Load null descriptor to unused segment registers. */
+        xor     %ecx, %ecx
+        mov     %ecx, %fs
+        mov     %ecx, %gs

The comment said "descriptor", yes, but it's a null selector that we load here. Perhaps worth adjusting as the comment is moved.

I'll do.

Quoted 58 lines
Quoted 58 lines
xen/arch/x86/boot/head.S +9 -31
-initialise_bss:
         /*
          * Initialise the BSS.
          *
          * !!! WARNING - also zeroes the current stack !!!
          */
-        pop     %ebp
         mov     %eax, %edx
-
         lea     sym_esi(__bss_start), %edi
         lea     sym_esi(__bss_end), %ecx
         sub     %edi, %ecx
         xor     %eax, %eax
         shr     $2, %ecx
         rep stosl
-
         mov     %edx, %eax
-        jmp     *%ebp
-
-__start:
-        cld
-        cli
 
-        /*
-         * Multiboot (both 1 and 2) specify the stack pointer as undefined
-         * when entering in BIOS circumstances.  This is unhelpful for
-         * relocatable images, where one call (i.e. push) is required to
-         * calculate the image's load address.
-         *
-         * This early in boot, there is one area of memory we know about with
-         * reasonable confidence that it isn't overlapped by Xen, and that's
-         * the Multiboot info structure in %ebx.  Use it as a temporary stack.
-         */
-
-        /* Preserve the field we're about to clobber. */
-        mov     (%ebx), %edx
-        lea     4(%ebx), %esp
+#ifdef CONFIG_PVH_GUEST
+        cmp     $BOOT_TYPE_PVH, %ebp
+        jne     1f
 
-        /* Calculate the load base address. */
-        call    1f
-1:      pop     %esi
-        sub     $sym_offs(1b), %esi
+        mov     %ebx, sym_esi(pvh_start_info_pa)
 
-        /* Restore the clobbered field. */
-        mov     %edx, (%ebx)
+        /* Force xen console.  Will revert to user choice in init code. */
+        movb    $-1, sym_esi(opt_console_xen)
 
-        /* Set up stack. */
-        lea     STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp
+        /* Skip bootloader setup and bios setup, go straight to trampoline */
+        movb    $1, sym_esi(pvh_boot)
+        movb    $1, sym_esi(skip_realmode)
 
Quoted 9 lines

xen/arch/x86/boot/head.S __pvh_start: · line 498

Quoted 3 lines
xen/arch/x86/boot/head.S +2 -1
-        call    initialise_bss
+        /* Set trampoline_phys to use mfn 1 to avoid having a mapping at VA 0 */
+        movw    $0x1000, sym_esi(trampoline_phys)

I realize you merely move this, yet I question the use of MOVW here. You use 32-bit operations e.g. to set %ebp; perhaps the same should be done here, even if that means a 1 byte code size increase. In any even this would preferably use PAGE_SIZE imo.

So movl instead of movw and PAGE_SIZE instead of 0x1000. I'll do.

Quoted 1 line

Jan

Frediano

#4 lore raw

Re: [PATCH 1/3] x86/boot: Initialise BSS as soon as possible

In reply to Frediano Ziglio
Quoted 20 lines · Frediano Ziglio
Quoted 8 lines · Jan Beulich
Quoted 6 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +0 -0
@@ -231,6 +231,27 @@ __efi64_mb2_start:
         /* VGA is not available on EFI platforms. */
         movl   $0,vga_text_buffer(%rip)
 
Quoted 10 lines

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 239

Quoted 6 lines
xen/arch/x86/boot/head.S +6 -0
+        /*
+         * Align the stack as UEFI spec requires. Keep it aligned
+         * before efi_multiboot2() call by pushing/popping even
+         * numbers of items on it.
+         */
+        and     $~15,%rsp

You don't use the stack below, so it's not clear if/why this needs moving. If it does, please add the missing blank after the comma (like you nicely do everywhere else).

Quoted 5 lines

Fixed the blank. The reason is more clear if you look at the last commit in the series, at least for the EFI part. For the BIOS/PVH part is less clear, but the rationale is the same. The commit came from a larger series where BIOS/PVH is mainly written in C so there is more clear.

If there's need to do it _here_, that need wants spelling out in the description.

Quoted 6 lines
Quoted 3 lines

Apart from this there's the question on the precise placement of the calls - see respective comment on patch 2 (which I needed to look at first to have an opinion here).

I think you removed too much context, not clear what code you are referring to, last hunk above is an "and" instruction.

This comment was on the patch as a whole, and I thought it was clear that it would refer to the two calls to the new function. The details of my concern there are, as said, in comments on patch 2.

Jan

#11 lore raw

Re: [PATCH 3/3] x86/boot: Rewrite EFI start part in C

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich
Quoted 1 line · Frediano Ziglio

No need to have it coded in assembly.

As to the title: It's the EFI/MB2 case you re-write. That wants reflecting there, as the "normal" EFI start part is all C already anyway. I also think you mean "partly"?

Updated to "x86/boot: Rewrite EFI/MBI2 code partly in C".

Quoted 16 lines

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 247

Quoted 3 lines
xen/arch/x86/boot/head.S +0 -0
@@ -255,34 +246,29 @@ __efi64_mb2_start:
         rep stosq
         mov     %edx, %eax

This can be dropped then, by making ...

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 250

Quoted 9 lines
xen/arch/x86/boot/head.S +2 -7
-        /* Check for Multiboot2 bootloader. */
-        cmp     $MULTIBOOT2_BOOTLOADER_MAGIC,%eax
-        je      .Lefi_multiboot2_proto
-
-        /* Jump to .Lnot_multiboot after switching CPU to x86_32 mode. */
-        lea     .Lnot_multiboot(%rip), %r15
-        jmp     x86_32_switch
+        /* Save Multiboot2 magic on the stack. */
+        push    %rax
Quoted 2 lines

... this use %rdx.

Done (also below)

Quoted 8 lines

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 253

Quoted 7 lines
xen/arch/x86/boot/head.S +2 -5
-.Lefi_multiboot2_proto:
-        /* Zero EFI SystemTable, EFI ImageHandle addresses and cmdline. */
-        xor     %esi,%esi
-        xor     %edi,%edi
-        xor     %edx,%edx
+        /* Save Multiboot2 pointer on the stack. */
+        push    %rbx
Quoted 6 lines

%rbx doesn't need preserving around a C function call (which will do so itself if necessary). I understand a 2nd PUSH may be necessary anyway, to keep the stack aligned, yet that then would need commenting differently. Plus as long as we call our own functions only, we don't require such alignment.

Extended comment.
16-byte alignment is also in SystemV ABI, I won't remove it in this series.

Quoted 10 lines

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 258

Quoted 7 lines
xen/arch/x86/boot/head.S +4 -3
-        /* Skip Multiboot2 information fixed part. */
-        lea     (MB2_fixed_sizeof+MULTIBOOT2_TAG_ALIGN-1)(%rbx),%ecx
-        and     $~(MULTIBOOT2_TAG_ALIGN-1),%ecx
+        /*
+         * efi_parse_mbi2() is called according to System V AMD64 ABI:
+         *   - IN:  %edi - Multiboot2 magic, %rsi - Multiboot2 pointer.
+         *   - OUT: %eax - error string.

Nit: %rax according to the code below.

Done

Quoted 6 lines

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 261

Quoted 3 lines
xen/arch/x86/boot/head.S +3 -0
+         */
+        mov     %eax, %edi
+        mov     %ebx, %esi

This latter one would better be a 64-bit MOV, for it being a pointer?

Done

Quoted 9 lines

xen/arch/x86/boot/head.S __efi64_mb2_start: · line 265

Quoted 4 lines
xen/arch/x86/boot/head.S +4 -0
+        call    efi_parse_mbi2
+        test    %rax, %rax
+        lea     .Ldirect_error(%rip), %r15
+        jnz     x86_32_switch

As requested by Andrew in a related context: LEA first please to follow the pattern allowing macro fusion, even if here it is less because of performance concerns but more to avoid giving a bad example.

Done

Quoted 11 lines

xen/arch/x86/efi/Makefile $(addprefix $(obj)/,$(EFIOBJ-y)): CFLAGS_stack_boundary := $(cflags-stack-bounda · line 16

Quoted 7 lines
xen/arch/x86/efi/Makefile +1 -0
@@ -13,6 +13,7 @@ $(addprefix $(obj)/,$(EFIOBJ-y)): CFLAGS_stack_boundary := $(cflags-stack-bounda
 
 obj-y := common-stub.o stub.o
 obj-$(XEN_BUILD_EFI) := $(filter-out %.init.o,$(EFIOBJ-y))
+obj-y += parse-mbi2.o

obj-bin-y I suppose, for it all being __init / __initdata, and hence the string literals in particular also wanting to move to .init.rodata.

Okay

Quoted 10 lines
Quoted 10 lines
xen/arch/x86/efi/parse-mbi2.c new file +7 -0
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <xen/efi.h>
+#include <xen/init.h>
+#include <xen/multiboot2.h>
+#include <asm/asm_defns.h>
+#include <asm/efibind.h>
Quoted 9 lines

xen/arch/x86/efi/parse-mbi2.c line 12

Quoted 5 lines
xen/arch/x86/efi/parse-mbi2.c new file +5 -0
+#include <efi/efidef.h>
+#include <efi/eficapsule.h>
+#include <efi/eficon.h>
+#include <efi/efidevp.h>
+#include <efi/efiapi.h>

I don't think all of these are needed? Please limit #include-s to just what is actually used.

I had the same idea, but if you comment out any of them code stop compiling.

Quoted 6 lines

xen/arch/x86/efi/parse-mbi2.c line 16

Quoted 3 lines
xen/arch/x86/efi/parse-mbi2.c new file +3 -0
+void asmlinkage __init efi_multiboot2(EFI_HANDLE ImageHandle,
+                                      EFI_SYSTEM_TABLE *SystemTable,
+                                      const char *cmdline);

This i now solely called from C code and hence shouldn't be asmlinkage.

Done

Quoted 5 lines

xen/arch/x86/efi/parse-mbi2.c line 18

Quoted 1 line
xen/arch/x86/efi/parse-mbi2.c new file +1 -0
+const char *__init efi_parse_mbi2(uint32_t magic, const multiboot2_fixed_t *mbi)

Whereas this, called from assembly code and not having / needing a declaration, should be.

Done

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/efi/parse-mbi2.c new file +4 -0
+{
+    const multiboot2_tag_t *tag;
+    EFI_HANDLE ImageHandle = NULL;
+    EFI_SYSTEM_TABLE *SystemTable = NULL;
Quoted 9 lines

xen/arch/x86/efi/parse-mbi2.c line 27

Quoted 5 lines
xen/arch/x86/efi/parse-mbi2.c new file +5 -0
+    const char *cmdline = NULL;
+    bool have_bs = false;
+
+    if ( magic != MULTIBOOT2_BOOTLOADER_MAGIC )
+        return "ERR: Not a Multiboot bootloader!";

Assembly code merely re-used a message. Now that it separate, please make it say "Multiboot2".

Done

Quoted 6 lines

xen/arch/x86/efi/parse-mbi2.c line 30

Quoted 2 lines
xen/arch/x86/efi/parse-mbi2.c new file +2 -0
+    /* Skip Multiboot2 information fixed part. */
+    for ( tag = _p(ROUNDUP((unsigned long)(mbi + 1), MULTIBOOT2_TAG_ALIGN));

The comment is placed as if it applied to the entire loop. It wants to move inside the for() to make clear it only applies to the loop setup.

Separated in a different line.

Quoted 8 lines

xen/arch/x86/efi/parse-mbi2.c line 32

Quoted 2 lines
xen/arch/x86/efi/parse-mbi2.c new file +2 -0
+          (void *)tag - (void *)mbi < mbi->total_size && tag->type != MULTIBOOT2_TAG_TYPE_END;
+          tag = _p(ROUNDUP((unsigned long)((void *)tag + tag->size), MULTIBOOT2_TAG_ALIGN)) )

Now that this is done in C, I think the checking wants to be more thorough, to no only make sure the start of a sub-struct is within the specified size, but all of it (se we won't even access past ->total_size).

I would first just translate the assembly code, then add improvements in a separate commit.

Quoted 2 lines

Further looks like there's a line length issue here.

Fixed

Quoted 2 lines

Also please don't cast away const-ness from pointers.

Done

Quoted 17 lines
Quoted 17 lines
xen/arch/x86/efi/parse-mbi2.c new file +17 -0
+    {
+        if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI_BS )
+            have_bs = true;
+        else if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI64 )
+            SystemTable = _p(((const multiboot2_tag_efi64_t *)tag)->pointer);
+        else if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI64_IH )
+            ImageHandle = _p(((const multiboot2_tag_efi64_ih_t *)tag)->pointer);
+        else if ( tag->type == MULTIBOOT2_TAG_TYPE_CMDLINE )
+            cmdline = ((const multiboot2_tag_string_t *)tag)->string;
+    }
+
+    if ( !have_bs )
+        return "ERR: Bootloader shutdown EFI x64 boot services!";
+    if ( !SystemTable )
+        return "ERR: EFI SystemTable is not provided by bootloader!";
+    if ( !ImageHandle )
+        return "ERR: EFI ImageHandle is not provided by bootloader!";
Quoted 9 lines

xen/arch/x86/efi/parse-mbi2.c line 48

Quoted 2 lines
xen/arch/x86/efi/parse-mbi2.c new file +2 -0
+
+    efi_multiboot2(ImageHandle, SystemTable, cmdline);

This being invoked from here now makes me wonder about the (new) function's name and whether a separate new function is actually
needed: Can't the new code then be integrated right into efi_multiboot2(), thus eliminating the question on the naming of the function?

If you are suggesting putting this parsing code inside efi_multiboot2 in ef-boot.h that would change the behavior, which I would do in a different commit.
Currently, there are 2 different efi_multiboot2 functions, one if ms_abi is supported, the other an empty stubs. However, some checks and tests are done in both cases (ms_abi supported or not). Also, both paths uses SystemTable, so I need to parse MBI2 in any case.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/efi/stub.c +0 -0
@@ -17,7 +17,8 @@
  */
 
Quoted 14 lines

xen/arch/x86/efi/stub.c line 24

Quoted 7 lines
xen/arch/x86/efi/stub.c +2 -1
 void __init noreturn efi_multiboot2(EFI_HANDLE ImageHandle,
-                                    EFI_SYSTEM_TABLE *SystemTable)
+                                    EFI_SYSTEM_TABLE *SystemTable,
+                                    const char *cmdline)
 {
     static const CHAR16 __initconst err[] =
         L"Xen does not have EFI code build in!\r\nSystem halted!\r\n";

This, if not a separate change, wants mentioning in the description. It's a related observation that this wasn't properly updated, but nothing that necessarily needs doing here. Question is whether the declaration of the function wouldn't better go into a header now in the first place.

I had the same though about a header. But currently there's no such header, I mean it should be able to be included by both stub.c and efi-boot.h which are both x86 only code so it should go in xen/arch/x86/ I suppose. Suggestions? Maybe a different solution would be to have a xen/arch/x86/efi/efi-boot-stub.h instead of xen/arch/x86/efi/stub.c ?

Quoted 1 line

Jan

Frediano

#8 lore raw

Re: [PATCH 2/3] x86/boot: Refactor BIOS/PVH start

In reply to Frediano Ziglio
Quoted 16 lines · Frediano Ziglio
Quoted 8 lines · Jan Beulich
Quoted 6 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +0 -0
@@ -25,6 +25,9 @@
 #define MB2_HT(name)      (MULTIBOOT2_HEADER_TAG_##name)
 #define MB2_TT(name)      (MULTIBOOT2_TAG_TYPE_##name)
 
Quoted 6 lines

xen/arch/x86/boot/head.S line 29

Quoted 2 lines
xen/arch/x86/boot/head.S +2 -0
+#define BOOT_TYPE_BIOS 1
+#define BOOT_TYPE_PVH 2

Did you consider using 0 and 1, to be able to use XOR on the BIOS path and TEST for checking?

Quoted 3 lines

Not clear what you are trying to achieve. Fewer bytes using the XOR? I think the TEST in this case is only reducing readability, it's an enumeration.

Except that in practice we don't really need an enum here; a boolean would suffice.

Quoted 4 lines

If you are concerns about code size I would use an 8 bit register (I would say DL) and use EBP register to temporary save EAX, 8 bit registers have usually tiny instructions, MOV has same size as XOR you mentioned not loosing any readability or forcing to change values.

No, it's not code size alone. It's larger code size for no good reason. Using 8-bit ops is an option when you're truly code size constrained, yet that's not the case here. Hence my take is - use the cheapest insns possible.

Quoted 9 lines
Quoted 5 lines

xen/arch/x86/boot/head.S cs32_switch: · line 425

Quoted 1 line
xen/arch/x86/boot/head.S +1 -0
+        cld

So you fold the STDs but not the STIs, despite that not even having been first on the PVH path. This decision wants explaining in the description, even if just briefly.

Just in case, I disable interrupts ASAP. Not that this should change much the result.
Would you prefer to fold it too?

That or explain the difference.

Quoted 2 lines

By "description" do you mean having an additional comment in the code or something in the commit message?

Sorry, I use "description" as a common synonym for "commit message".

Quoted 10 lines
Quoted 9 lines

xen/arch/x86/boot/head.S __pvh_start: · line 436

Quoted 5 lines
xen/arch/x86/boot/head.S +0 -1
@@ -433,14 +451,9 @@ __pvh_start:
         /* Set up stack. */
         lea     STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp
 
-        call    initialise_bss

I'm little puzzled: The previous patch moved it "as early as possible" just for it to be moved to a later point again here?

Quoted 3 lines

The rationale is being able to use C, for that you need a stack, correct segments and BSS.
Are you suggesting any change?

I'm trying to understand the sum effect of both changes, which appear to be moving in opposite direction.

Jan

#12 lore raw

Re: [PATCH 3/3] x86/boot: Rewrite EFI start part in C

In reply to Frediano Ziglio
Quoted 10 lines · Frediano Ziglio
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Frediano Ziglio

-.Lefi_multiboot2_proto:

xen/arch/x86/boot/head.S +2 -4
-        /* Zero EFI SystemTable, EFI ImageHandle addresses and cmdline. */
-        xor     %esi,%esi
-        xor     %edi,%edi
-        xor     %edx,%edx
+        /* Save Multiboot2 pointer on the stack. */
+        push    %rbx
Quoted 8 lines
Quoted 5 lines

%rbx doesn't need preserving around a C function call (which will do so itself if necessary). I understand a 2nd PUSH may be necessary anyway, to keep the stack aligned, yet that then would need commenting differently. Plus as long as we call our own functions only, we don't require such alignment.

Extended comment.
16-byte alignment is also in SystemV ABI, I won't remove it in this series.

Except that we build with -mpreferred-stack-boundary=3, not respecting the ABI in this regard anyway.

Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
xen/arch/x86/efi/parse-mbi2.c new file +17 -0
+    {
+        if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI_BS )
+            have_bs = true;
+        else if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI64 )
+            SystemTable = _p(((const multiboot2_tag_efi64_t *)tag)->pointer);
+        else if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI64_IH )
+            ImageHandle = _p(((const multiboot2_tag_efi64_ih_t *)tag)->pointer);
+        else if ( tag->type == MULTIBOOT2_TAG_TYPE_CMDLINE )
+            cmdline = ((const multiboot2_tag_string_t *)tag)->string;
+    }
+
+    if ( !have_bs )
+        return "ERR: Bootloader shutdown EFI x64 boot services!";
+    if ( !SystemTable )
+        return "ERR: EFI SystemTable is not provided by bootloader!";
+    if ( !ImageHandle )
+        return "ERR: EFI ImageHandle is not provided by bootloader!";
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+    efi_multiboot2(ImageHandle, SystemTable, cmdline);

This being invoked from here now makes me wonder about the (new) function's name and whether a separate new function is actually
needed: Can't the new code then be integrated right into efi_multiboot2(), thus eliminating the question on the naming of the function?

Quoted 7 lines

If you are suggesting putting this parsing code inside efi_multiboot2 in ef-boot.h that would change the behavior, which I would do in a different commit.
Currently, there are 2 different efi_multiboot2 functions, one if ms_abi is supported, the other an empty stubs. However, some checks and tests are done in both cases (ms_abi supported or not). Also, both paths uses SystemTable, so I need to parse MBI2 in any case.

It could be slightly less parsing, but I get your point.

Then, as indicated, the function's name needs to change. The present name simply fails to account for the important-ish fact that efi_multiboot2() is (tail-)called.

Quoted 19 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/efi/stub.c +0 -0
@@ -17,7 +17,8 @@
  */
 
Quoted 13 lines

xen/arch/x86/efi/stub.c line 24

Quoted 7 lines
xen/arch/x86/efi/stub.c +2 -1
 void __init noreturn efi_multiboot2(EFI_HANDLE ImageHandle,
-                                    EFI_SYSTEM_TABLE *SystemTable)
+                                    EFI_SYSTEM_TABLE *SystemTable,
+                                    const char *cmdline)
 {
     static const CHAR16 __initconst err[] =
         L"Xen does not have EFI code build in!\r\nSystem halted!\r\n";

This, if not a separate change, wants mentioning in the description. It's a related observation that this wasn't properly updated, but nothing that necessarily needs doing here. Question is whether the declaration of the function wouldn't better go into a header now in the first place.

Quoted 6 lines

I had the same though about a header. But currently there's no such header, I mean it should be able to be included by both stub.c and efi-boot.h which are both x86 only code so it should go in xen/arch/x86/ I suppose. Suggestions? Maybe a different solution would be to have a xen/arch/x86/efi/efi-boot-stub.h instead of xen/arch/x86/efi/stub.c ?

It's not quite the right place, but maybe (ab)using asm/efibind.h would be slightly better than introducing asm/efi.h just for a single decl?

Jan

#13 lore raw

Re: [PATCH 3/3] x86/boot: Rewrite EFI start part in C

Quoted 1 line · Jan Beulich

... omissis ...

Quoted 27 lines
Quoted 20 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/efi/stub.c +0 -0
@@ -17,7 +17,8 @@
  */
 
Quoted 13 lines

xen/arch/x86/efi/stub.c line 24

Quoted 7 lines
xen/arch/x86/efi/stub.c +2 -1
 void __init noreturn efi_multiboot2(EFI_HANDLE ImageHandle,
-                                    EFI_SYSTEM_TABLE *SystemTable)
+                                    EFI_SYSTEM_TABLE *SystemTable,
+                                    const char *cmdline)
 {
     static const CHAR16 __initconst err[] =
         L"Xen does not have EFI code build in!\r\nSystem halted!\r\n";

This, if not a separate change, wants mentioning in the description. It's a related observation that this wasn't properly updated, but nothing that necessarily needs doing here. Question is whether the declaration of the function wouldn't better go into a header now in the first place.

Quoted 6 lines

I had the same though about a header. But currently there's no such header, I mean it should be able to be included by both stub.c and efi-boot.h which are both x86 only code so it should go in xen/arch/x86/ I suppose. Suggestions? Maybe a different solution would be to have a xen/arch/x86/efi/efi-boot-stub.h instead of xen/arch/x86/efi/stub.c ?

Quoted 4 lines

It's not quite the right place, but maybe (ab)using asm/efibind.h would be slightly better than introducing asm/efi.h just for a single decl?

Jan

Okay, I found the comment on the header to place the declaration.

Kind of works... but the headers are very crazily depending on each other, the header change is

(quoted diff) +0 -0

b/xen/arch/x86/include/asm/efibind.h
index bce02f3707..1fa5522a0d 100644

xen/arch/x86/include/asm/efibind.h +0 -0
@@ -1,2 +1,13 @@

#include <xen/types.h>
+#include <xen/init.h>
#include <asm/x86_64/efibind.h>
+#include <efi/efidef.h>
+#include <efi/eficapsule.h>
+#include <efi/eficon.h>
+#include <efi/efidevp.h>
+#include <efi/efiapi.h>
+
+void __init efi_multiboot2(EFI_HANDLE ImageHandle,
+ EFI_SYSTEM_TABLE *SystemTable,
+ const char *cmdline);
+

How does it sound ?

Frediano

#14 lore raw

Re: [PATCH 3/3] x86/boot: Rewrite EFI start part in C

Quoted 62 lines · Frediano Ziglio
Quoted 1 line · Jan Beulich

... omissis ...

Quoted 27 lines
Quoted 20 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/efi/stub.c +0 -0
@@ -17,7 +17,8 @@
  */
 
Quoted 13 lines

xen/arch/x86/efi/stub.c line 24

Quoted 7 lines
xen/arch/x86/efi/stub.c +2 -1
 void __init noreturn efi_multiboot2(EFI_HANDLE ImageHandle,
-                                    EFI_SYSTEM_TABLE *SystemTable)
+                                    EFI_SYSTEM_TABLE *SystemTable,
+                                    const char *cmdline)
 {
     static const CHAR16 __initconst err[] =
         L"Xen does not have EFI code build in!\r\nSystem halted!\r\n";

This, if not a separate change, wants mentioning in the description. It's a related observation that this wasn't properly updated, but nothing that necessarily needs doing here. Question is whether the declaration of the function wouldn't better go into a header now in the first place.

Quoted 6 lines

I had the same though about a header. But currently there's no such header, I mean it should be able to be included by both stub.c and efi-boot.h which are both x86 only code so it should go in xen/arch/x86/ I suppose. Suggestions? Maybe a different solution would be to have a xen/arch/x86/efi/efi-boot-stub.h instead of xen/arch/x86/efi/stub.c ?

Quoted 4 lines

It's not quite the right place, but maybe (ab)using asm/efibind.h would be slightly better than introducing asm/efi.h just for a single decl?

Jan

Okay, I found the comment on the header to place the declaration.

Kind of works... but the headers are very crazily depending on each other, the header change is

xen/arch/x86/include/asm/efibind.h +11 -0
index bce02f3707..1fa5522a0d 100644
@@ -1,2 +1,13 @@
 #include <xen/types.h>
+#include <xen/init.h>
 #include <asm/x86_64/efibind.h>
+#include <efi/efidef.h>
+#include <efi/eficapsule.h>
+#include <efi/eficon.h>
+#include <efi/efidevp.h>
+#include <efi/efiapi.h>
+
+void __init efi_multiboot2(EFI_HANDLE ImageHandle,
+                           EFI_SYSTEM_TABLE *SystemTable,
+                           const char *cmdline);
+
Quoted 1 line

How does it sound ?

Hmm, no, not good at all. All these #include-s are against the purpose of the header. We'll need asm/efi.h then, I think.

As an aside - you don't need xen/init.h here, as declarations shouldn't have __init attributes.

Jan