x86/boot: Reduce assembly code
v4 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.
Changes since v1, more details in specific commits:
- style updates;
- comments and descriptions improvements;
- other improvements.
Changes since v2:
- rebased on master, resolved conflicts;
- add comment on trampoline section.
Changes since v3:
- changed new function name;
- declare efi_multiboot2 in a separate header;
- distinguish entry point from using magic number;
- other minor changes (see commens in commits).
Frediano Ziglio (4):
x86/boot: Initialise BSS sooner
x86/boot: Refactor BIOS/PVH start
x86/boot: Rewrite EFI/MBI2 code partly in C
x86/boot: Improve MBI2 structure check
xen/arch/x86/boot/head.S | 287 ++++++++++----------------------- xen/arch/x86/efi/Makefile | 1 + xen/arch/x86/efi/efi-boot.h | 6 +- xen/arch/x86/efi/parse-mbi2.c | 59 +++++++ xen/arch/x86/efi/stub.c | 3 +- xen/arch/x86/include/asm/efi.h | 18 +++ 6 files changed, 170 insertions(+), 204 deletions(-) create mode 100644 xen/arch/x86/efi/parse-mbi2.c create mode 100644 xen/arch/x86/include/asm/efi.h
Signature · 1 line
2.34.1
PATCH v4 1/4 x86/boot: Initialise BSS sooner
Allows to call C code earlier.
In order to safely call C code we need to setup stack, selectors and BSS.
---
Changes since v1:
- improve commit message;
- improve some comments;
- fix some code style (spacing);
- set trampoline_phys as 32 bit value;
- use PAGE_SIZE mnemonic instead of 0x1000;
- use local label.
Changes since v3:
- improved comment;
- fixed indentation.
--- xen/arch/x86/boot/head.S | 77 ++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 38 deletions(-)
xen/arch/x86/boot/head.S +39 -38
@@ -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 .Linitialise_bss + mov %ebx, sym_esi(pvh_start_info_pa) /* Force xen console. Will revert to user choice in init code. */
@@ -459,6 +460,20 @@ __pvh_start: #endif /* CONFIG_PVH_GUEST */ +.Linitialise_bss: + /* Initialise the BSS. Preserve %eax (BOOTLOADER_MAGIC). */ + mov %eax, %ebp + + lea sym_esi(__bss_start), %edi + lea sym_esi(__bss_end), %ecx + sub %edi, %ecx + xor %eax, %eax + shr $2, %ecx + rep stosl + + mov %ebp, %eax + ret + __start: cld cli
@@ -489,6 +504,8 @@ __start: /* Set up stack. */ lea STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp + call .Linitialise_bss + /* Bootloaders may set multiboot{1,2}.mem_lower to a nonzero value. */ xor %edx,%edx
@@ -645,22 +662,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. */ - 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
PATCH v4 2/4 x86/boot: Refactor BIOS/PVH start
In reply to Frediano ZiglioThe 2 code paths were sharing quite some common code, reuse it instead of having duplications.
Use %dl register to store boot type before running common code. Using a 8 bit register reduces code size.
---
Changes since v1:
- use %dl instead of %ebp to reduce code size;
- fold cli instruction;
- update some comments.
Changes since v3:
- dropped %dl and constant, distinguish entry by magic.
--- xen/arch/x86/boot/head.S | 108 +++++++++++++++------------------------ 1 file changed, 40 insertions(+), 68 deletions(-)
xen/arch/x86/boot/head.S +40 -68
@@ -25,6 +25,8 @@ #define MB2_HT(name) (MULTIBOOT2_HEADER_TAG_##name) #define MB2_TT(name) (MULTIBOOT2_TAG_TYPE_##name) +#define XEN_HVM_START_MAGIC_VALUE 0x336ec578 + .macro mb2ht_args arg:req, args:vararg .long \arg .ifnb \args
@@ -409,13 +411,27 @@ cs32_switch: ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .long sym_offs(__pvh_start)) __pvh_start: - cld + mov (%ebx), %eax /* mov $XEN_HVM_START_MAGIC_VALUE, %eax */ + /* + * Fall through into BIOS code. + * We will use %eax to distinguish we came from PHV entry point. + */ +#endif /* CONFIG_PVH_GUEST */ + +__start: cli + 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 +449,7 @@ __pvh_start: /* Set up stack. */ lea STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp - call .Linitialise_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,62 +458,40 @@ __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) + /* Load null selector to unused segment registers. */ + xor %ecx, %ecx + mov %ecx, %fs + mov %ecx, %gs - /* 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 */ - -.Linitialise_bss: /* Initialise the BSS. Preserve %eax (BOOTLOADER_MAGIC). */ mov %eax, %ebp - lea sym_esi(__bss_start), %edi lea sym_esi(__bss_end), %ecx sub %edi, %ecx xor %eax, %eax shr $2, %ecx rep stosl - mov %ebp, %eax - ret - -__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 $XEN_HVM_START_MAGIC_VALUE, %eax + 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 .Linitialise_bss + /* Set trampoline_phys to use mfn 1 to avoid having a mapping at VA 0 */ + movl $PAGE_SIZE, 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
@@ -563,22 +550,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
PATCH v4 4/4 x86/boot: Improve MBI2 structure check
In reply to Frediano ZiglioTag structure should contain at least the tag header. Entire tag structure must be contained inside MBI2 data.
--- xen/arch/x86/efi/parse-mbi2.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
xen/arch/x86/efi/parse-mbi2.c +4 -1
@@ -13,6 +13,7 @@ efi_multiboot2_prelude(uint32_t magic, const multiboot2_fixed_t *mbi)EFI_HANDLE ImageHandle =NULL ;EFI_SYSTEM_TABLE *SystemTable =NULL ;const char *cmdline =NULL ; +const void *const mbi_end = (const void *)mbi + mbi->total_size;bool have_bs = false;if ( magic !=MULTIBOOT2_BOOTLOADER_MAGIC )
@@ -21,7 +22,9 @@ efi_multiboot2_prelude(uint32_t magic, const multiboot2_fixed_t *mbi)/* Skip Multiboot2 information fixed part. */ tag =_p (ROUNDUP ((unsigned long )(mbi +1 ),MULTIBOOT2_TAG_ALIGN )); -for ( ; (const void *)tag - (const void *)mbi < mbi->total_size +for ( ; (const void *)(tag +1 ) <= mbi_end + && tag->size >=sizeof (*tag) + && (const void *)tag + tag->size <= mbi_end && tag->type !=MULTIBOOT2_TAG_TYPE_END ; tag =_p (ROUNDUP ((unsigned long )((const void *)tag + tag->size),MULTIBOOT2_TAG_ALIGN )) )
Signature · 1 line
2.34.1
PATCH v4 3/4 x86/boot: Rewrite EFI/MBI2 code partly in C
In reply to Frediano ZiglioNo need to have it coded in assembly.
---
Changes since v1:
- update some comments;
- explain why %ebx is saved before calling efi_parse_mbi2;
- move lea before test instruction;
- removed asmlinkage from efi_multiboot2 and add to efi_parse_mbi2;
- fix line length;
- update an error message specifying "Multiboot2" instead of "Multiboot";
- use obj-bin-X instead of obj-X in Makefile;
- avoid restoring %eax (MBI magic).
Changes since v3:
- rename new function to efi_multiboot2_prelude;
- declare efi_multiboot2 in a separate header.
--- xen/arch/x86/boot/head.S | 142 +++++++-------------------------- xen/arch/x86/efi/Makefile | 1 + xen/arch/x86/efi/efi-boot.h | 6 +- xen/arch/x86/efi/parse-mbi2.c | 56 +++++++++++++ xen/arch/x86/efi/stub.c | 3 +- xen/arch/x86/include/asm/efi.h | 18 +++++ 6 files changed, 108 insertions(+), 118 deletions(-) create mode 100644 xen/arch/x86/efi/parse-mbi2.c create mode 100644 xen/arch/x86/include/asm/efi.h
xen/arch/x86/boot/head.S +28 -114
@@ -121,8 +121,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!"
@@ -161,17 +159,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
@@ -189,6 +176,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:
@@ -235,7 +226,7 @@ __efi64_mb2_start: /* * Align the stack as UEFI spec requires. Keep it aligned - * before efi_multiboot2() call by pushing/popping even + * before efi_multiboot2_prelude() call by pushing/popping even * numbers of items on it. */ and $~15, %rsp
@@ -243,7 +234,7 @@ __efi64_mb2_start: /* * Initialize BSS (no nasty surprises!). * It must be done earlier than in BIOS case - * because efi_multiboot2() touches it. + * because efi_multiboot2_prelude() touches it. */ mov %eax, %edx lea __bss_start(%rip), %edi
@@ -252,36 +243,30 @@ __efi64_mb2_start: shr $3, %ecx xor %eax, %eax 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 - -.Lefi_multiboot2_proto: - /* Zero EFI SystemTable, EFI ImageHandle addresses and cmdline. */ - xor %esi,%esi - xor %edi,%edi - xor %edx,%edx - /* Skip Multiboot2 information fixed part. */ - lea (MB2_fixed_sizeof+MULTIBOOT2_TAG_ALIGN-1)(%rbx),%ecx - and $~(MULTIBOOT2_TAG_ALIGN-1),%ecx - -.Lefi_mb2_tsize: - /* Check Multiboot2 information total size. */ - mov %ecx,%r8d - sub %ebx,%r8d - cmp %r8d,MB2_fixed_total_size(%rbx) - jbe .Lrun_bs + /* + * Spill MB2 magic. + * Spill the pointer too, to keep the stack aligned. + */ + push %rdx + push %rbx - /* Are EFI boot services available? */ - cmpl $MULTIBOOT2_TAG_TYPE_EFI_BS,MB2_tag_type(%rcx) - jne .Lefi_mb2_st + /* + * efi_multiboot2_prelude() is called according to System V AMD64 ABI: + * - IN: %edi - Multiboot2 magic, + * %rsi - Multiboot2 pointer. + * - OUT: %rax - error string. + */ + mov %edx, %edi + mov %rbx, %rsi + call efi_multiboot2_prelude + lea .Ldirect_error(%rip), %r15 + test %rax, %rax + jnz x86_32_switch + + /* Restore Multiboot2 pointer and magic. */ + pop %rbx + pop %rax /* We are on EFI platform and EFI boot services are available. */ incb efi_platform(%rip)
@@ -291,77 +276,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
@@ -14,5 +14,6 @@ $(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-bin-$(XEN_BUILD_EFI) := $(filter %.init.o,$(EFIOBJ-y)) +obj-bin-y += parse-mbi2.o extra-$(XEN_BUILD_EFI) += buildid.o relocs-dummy.o nocov-$(XEN_BUILD_EFI) += stub.o
xen/arch/x86/efi/efi-boot.h +3 -3
@@ -816,9 +816,9 @@ static const char *__init get_option(const char *cmd, const char *opt)return o; } -void asmlinkage __init efi_multiboot2(EFI_HANDLE ImageHandle, -EFI_SYSTEM_TABLE *SystemTable, -const char *cmdline) +void __init efi_multiboot2 (EFI_HANDLE ImageHandle, +EFI_SYSTEM_TABLE *SystemTable, +const char *cmdline) {EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;EFI_HANDLE gop_handle;
xen/arch/x86/efi/parse-mbi2.c new file +56 -0
56 lines not on this page · raw message
xen/arch/x86/efi/stub.c +2 -1
@@ -17,7 +17,8 @@*/ void __init noreturnefi_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" ;
xen/arch/x86/include/asm/efi.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef X86_ASM_EFI_H +#define X86_ASM_EFI_H + +#include <xen/types.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 efi_multiboot2 (EFI_HANDLE ImageHandle, +EFI_SYSTEM_TABLE *SystemTable, +const char *cmdline); + +#endif /* X86_ASM_EFI_H */
Signature · 1 line
2.34.1
Re: [PATCH v4 2/4] x86/boot: Refactor BIOS/PVH start
In reply to Frediano ZiglioQuoted 4 lines · Frediano Ziglio
The 2 code paths were sharing quite some common code, reuse it instead of having duplications.
Use %dl register to store boot type before running common code. Using a 8 bit register reduces code size.
These final two lines are stale and can be dropped.
xen/arch/x86/boot/head.S cs32_switch: · line 417
Quoted 13 lines
xen/arch/x86/boot/head.S +4
-1@@ -409,13 +411,27 @@ cs32_switch: ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .long sym_offs(__pvh_start)) __pvh_start: - cld + mov (%ebx), %eax /* mov $XEN_HVM_START_MAGIC_VALUE, %eax */ + /* + * Fall through into BIOS code. + * We will use %eax to distinguish we came from PHV entry point.
PVH.
It occurs to me that we could actually have:
mov $XEN_HVM_START_MAGIC_VALUE, %eax
Given the cross-check against 0(%ebx) later, that's marginally more robust.
xen/arch/x86/boot/head.S __pvh_start: · line 464
Quoted 11 lines
xen/arch/x86/boot/head.S +4
-3@@ -449,62 +458,40 @@ __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) + /* Load null selector to unused segment registers. */ + xor %ecx, %ecx + mov %ecx, %fs + mov %ecx, %gs
Honestly, the more I look at this, the more bizarre it is.
We should just set up %fs/gs like we do %ds/es, which in this case is simply to drop the comment and the xor.
Quoted 10 lines
xen/arch/x86/boot/head.S +0
-8- /* 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 */ - -.Linitialise_bss: /* Initialise the BSS. Preserve %eax (BOOTLOADER_MAGIC). */
xen/arch/x86/boot/head.S __pvh_start: · line 474
Quoted 10 lines
xen/arch/x86/boot/head.S +0
-2mov %eax, %ebp - lea sym_esi(__bss_start), %edi lea sym_esi(__bss_end), %ecx sub %edi, %ecx xor %eax, %eax shr $2, %ecx rep stosl - mov %ebp, %eax
Are these two dropped lines intentional?
I did ask on the previous version; it's weird to introduce the code with them present, then delete them in the subsequent patch.
Quoted 35 lines
xen/arch/x86/boot/head.S +6
-25- ret - -__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 $XEN_HVM_START_MAGIC_VALUE, %eax + 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 491
Quoted 10 lines
xen/arch/x86/boot/head.S +6
-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 .Linitialise_bss + /* Set trampoline_phys to use mfn 1 to avoid having a mapping at VA 0 */ + movl $PAGE_SIZE, sym_esi(trampoline_phys) + mov (%ebx), %eax /* mov $XEN_HVM_START_MAGIC_VALUE, %eax */
This isn't needed any more. It was previously reloading %eax after REP STOSL, but that's sorted elsehow now.
Overall, this bit of the diff is still hard to read, but it's the best we're going to get I think. The end result is good.
I'm happy to adjust all of these on commit.
~Andrew
Re: [PATCH v4 3/4] x86/boot: Rewrite EFI/MBI2 code partly in C
In reply to Frediano Ziglioxen/arch/x86/boot/head.S __efi64_mb2_start: · line 278
Quoted 13 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +0
-0@@ -252,36 +243,30 @@ __efi64_mb2_start: /* We are on EFI platform and EFI boot services are available. */ incb efi_platform(%rip)@@ -291,77 +276,6 @@ __efi64_mb2_start: * be run on EFI platforms. */ incb skip_realmode(%rip)
Well, these are two unfounded assumptions about the compile-time defaults of certain variables.
Lets fix it afterwards, to save interfering with this series.
xen/arch/x86/efi/parse-mbi2.c line 1
Quoted 7 lines
xen/arch/x86/efi/parse-mbi2.c new file +1
-0@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */
GPL-2.0-only. The unsuffixed form is deprecated now.
Quoted 14 lines
xen/arch/x86/efi/parse-mbi2.c new file +14
-0+ +#include <xen/efi.h> +#include <xen/init.h> +#include <xen/multiboot2.h> +#include <asm/asm_defns.h> +#include <asm/efi.h> + +const char * asmlinkage __init +efi_multiboot2_prelude (uint32_t magic,const multiboot2_fixed_t *mbi) +{ +const multiboot2_tag_t *tag; +EFI_HANDLE ImageHandle =NULL ; +EFI_SYSTEM_TABLE *SystemTable =NULL ; +const char *cmdline =NULL ;
xen/arch/x86/efi/parse-mbi2.c line 25
Quoted 10 lines
xen/arch/x86/efi/parse-mbi2.c new file +10
-0+bool have_bs = false; + +if ( magic !=MULTIBOOT2_BOOTLOADER_MAGIC ) +return "ERR: Not a Multiboot2 bootloader!" ; + +/* Skip Multiboot2 information fixed part. */ + tag =_p (ROUNDUP ((unsigned long )(mbi +1 ),MULTIBOOT2_TAG_ALIGN )); + + for ( ; (const void *)tag - (const void *)mbi < mbi->total_size + && tag->type != MULTIBOOT2_TAG_TYPE_END;
&& on previous line.
But, this can move into the switch statement and reduce the for() expression somewhat.
xen/arch/x86/efi/parse-mbi2.c line 27
Quoted 2 lines
xen/arch/x86/efi/parse-mbi2.c new file +2
-0+ tag =_p (ROUNDUP ((unsigned long )((const void *)tag + tag->size), + MULTIBOOT2_TAG_ALIGN)) )
No need to cast through (const void *) I don't think. It's byte-granular when unsigned long too. This should do:
_p(ROUNDUP(((unsigned long)tag + tag->size), MULTIBOOT2_TAG_ALIGN))
xen/arch/x86/efi/parse-mbi2.c line 33
Quoted 6 lines
xen/arch/x86/efi/parse-mbi2.c new file +6
-0+ { +switch ( tag->type ) + { +case MULTIBOOT2_TAG_TYPE_EFI_BS : + have_bs = true; +break ;
Newlines after break's please.
xen/arch/x86/efi/parse-mbi2.c line 42
Quoted 9 lines
xen/arch/x86/efi/parse-mbi2.c new file +9
-0+case MULTIBOOT2_TAG_TYPE_EFI64 : + SystemTable =_p (((const multiboot2_tag_efi64_t *)tag)->pointer); +break ; +case MULTIBOOT2_TAG_TYPE_EFI64_IH : + ImageHandle =_p (((const multiboot2_tag_efi64_ih_t *)tag)->pointer); +break ; +case MULTIBOOT2_TAG_TYPE_CMDLINE : + cmdline = ((const multiboot2_tag_string_t *)tag)->string; +break ;
default:
/* This comment is here because MISRA thinks you can't read the code without it. */
break;
Probably not that wording, but it reflects what I think of this particular rule.
I can fix all on commit, but this needs an EFI ack.
~Andrew
Re: [PATCH v4 2/4] x86/boot: Refactor BIOS/PVH start
In reply to Andrew CooperQuoted 15 lines · Andrew Cooper
xen/arch/x86/boot/head.S __pvh_start: · line 464
Quoted 11 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +4
-3@@ -449,62 +458,40 @@ __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) + /* Load null selector to unused segment registers. */ + xor %ecx, %ecx + mov %ecx, %fs + mov %ecx, %gsHonestly, the more I look at this, the more bizarre it is.
Quoted 2 lines
We should just set up %fs/gs like we do %ds/es, which in this case is simply to drop the comment and the xor.
What's bizarre here? As long as we don't use %fs/%gs, it doesn't matter much what we set them to. So yes, they may be set to what %ds etc are set to, but they may as well be marked unusable. Documentation-wise that's cleaner imo, as down the road - when a need to use one arises - it then won't require auditing of all code to figure where the register(s) is(are) actually used (just to find: nowhere). Even if a comment to this effect was left here, I for one wouldn't trust it in a couple of years time, but rather fear it went stale.
Jan
Re: [PATCH v4 3/4] x86/boot: Rewrite EFI/MBI2 code partly in C
In reply to Andrew CooperQuoted 21 lines · Andrew Cooper
Quoted 7 lines · Frediano Ziglio
xen/arch/x86/efi/parse-mbi2.c new file +7
-0+const char * asmlinkage __init +efi_multiboot2_prelude (uint32_t magic,const multiboot2_fixed_t *mbi) +{ +const multiboot2_tag_t *tag; +EFI_HANDLE ImageHandle =NULL ; +EFI_SYSTEM_TABLE *SystemTable =NULL ; +const char *cmdline =NULL ;xen/arch/x86/efi/parse-mbi2.c line 25
Quoted 10 lines
xen/arch/x86/efi/parse-mbi2.c new file +10
-0+bool have_bs = false; + +if ( magic !=MULTIBOOT2_BOOTLOADER_MAGIC ) +return "ERR: Not a Multiboot2 bootloader!" ; + +/* Skip Multiboot2 information fixed part. */ + tag =_p (ROUNDUP ((unsigned long )(mbi +1 ),MULTIBOOT2_TAG_ALIGN )); + + for ( ; (const void *)tag - (const void *)mbi < mbi->total_size + && tag->type != MULTIBOOT2_TAG_TYPE_END;&& on previous line.
Quoted 2 lines
But, this can move into the switch statement and reduce the
for()expression somewhat.
While it can in principle, it would require further adjustments to the loop body, which I'm uncertain would be desirable. I notice Frediano also didn't make any change in v5 for this particular comment. Frediano:
As indicated before, sending a new version without addressing all comments isn't nice. If you don't agree with a comment and hence don't make a change requested, you will want to "address" the comment verbally.
Jan
Re: [PATCH v4 3/4] x86/boot: Rewrite EFI/MBI2 code partly in C
Quoted 26 lines · Jan Beulich
Quoted 21 lines · Andrew Cooper
Quoted 7 lines · Frediano Ziglio
xen/arch/x86/efi/parse-mbi2.c new file +7
-0+const char * asmlinkage __init +efi_multiboot2_prelude (uint32_t magic,const multiboot2_fixed_t *mbi) +{ +const multiboot2_tag_t *tag; +EFI_HANDLE ImageHandle =NULL ; +EFI_SYSTEM_TABLE *SystemTable =NULL ; +const char *cmdline =NULL ;xen/arch/x86/efi/parse-mbi2.c line 25
Quoted 10 lines
xen/arch/x86/efi/parse-mbi2.c new file +10
-0+bool have_bs = false; + +if ( magic !=MULTIBOOT2_BOOTLOADER_MAGIC ) +return "ERR: Not a Multiboot2 bootloader!" ; + +/* Skip Multiboot2 information fixed part. */ + tag =_p (ROUNDUP ((unsigned long )(mbi +1 ),MULTIBOOT2_TAG_ALIGN )); + + for ( ; (const void *)tag - (const void *)mbi < mbi->total_size + && tag->type != MULTIBOOT2_TAG_TYPE_END;&& on previous line.
Quoted 2 lines
But, this can move into the switch statement and reduce the
for()expression somewhat.
Quoted 8 lines
While it can in principle, it would require further adjustments to the loop body, which I'm uncertain would be desirable. I notice Frediano also didn't make any change in v5 for this particular comment. Frediano:
As indicated before, sending a new version without addressing all comments isn't nice. If you don't agree with a comment and hence don't make a change requested, you will want to "address" the comment verbally.Jan
I was going to reply, then I forgot. Yes, I'm trying to reply and address all comments. In this specific instance, for some reason while I was addressing the others, I forgot this.
Frediano
Re: [PATCH v4 3/4] x86/boot: Rewrite EFI/MBI2 code partly in C
In reply to Andrew CooperQuoted 58 lines · Andrew Cooper
xen/arch/x86/boot/head.S __efi64_mb2_start: · line 278
Quoted 13 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +0
-0@@ -252,36 +243,30 @@ __efi64_mb2_start: /* We are on EFI platform and EFI boot services are available. */ incb efi_platform(%rip)@@ -291,77 +276,6 @@ __efi64_mb2_start: * be run on EFI platforms. */ incb skip_realmode(%rip)Well, these are two unfounded assumptions about the compile-time defaults of certain variables.
Lets fix it afterwards, to save interfering with this series.
xen/arch/x86/efi/parse-mbi2.c line 1
Quoted 7 lines
xen/arch/x86/efi/parse-mbi2.c new file +1
-0@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ GPL-2.0-only. The unsuffixed form is deprecated now.
Quoted 14 lines
xen/arch/x86/efi/parse-mbi2.c new file +14
-0+ +#include <xen/efi.h> +#include <xen/init.h> +#include <xen/multiboot2.h> +#include <asm/asm_defns.h> +#include <asm/efi.h> + +const char * asmlinkage __init +efi_multiboot2_prelude (uint32_t magic,const multiboot2_fixed_t *mbi) +{ +const multiboot2_tag_t *tag; +EFI_HANDLE ImageHandle =NULL ; +EFI_SYSTEM_TABLE *SystemTable =NULL ; +const char *cmdline =NULL ;xen/arch/x86/efi/parse-mbi2.c line 25
Quoted 10 lines
xen/arch/x86/efi/parse-mbi2.c new file +10
-0+bool have_bs = false; + +if ( magic !=MULTIBOOT2_BOOTLOADER_MAGIC ) +return "ERR: Not a Multiboot2 bootloader!" ; + +/* Skip Multiboot2 information fixed part. */ + tag =_p (ROUNDUP ((unsigned long )(mbi +1 ),MULTIBOOT2_TAG_ALIGN )); + + for ( ; (const void *)tag - (const void *)mbi < mbi->total_size + && tag->type != MULTIBOOT2_TAG_TYPE_END;&& on previous line.
Quoted 3 lines
But, this can move into the switch statement and reduce the
for()expression somewhat.
I forgot to reply this, I even though about what to write. There are multiple reasons:
- having now a switch, it would require a goto/label or an additional variable to exit the loop, unless I change all other case to continue and have 2 breaks, either cases not much improving in my opinion;
- that specific part of the for loop is checking for termination and that condition is doing exactly this;
- there are multiple instances of this kind of loop, and I was thinking of adding a macro to simplify and reuse that loop so that form is more suitable to do it.
... omissis ...
Frediano
Re: [PATCH v4 3/4] x86/boot: Rewrite EFI/MBI2 code partly in C
Quoted 55 lines · Frediano Ziglio
Quoted 54 lines · Andrew Cooper
xen/arch/x86/boot/head.S __efi64_mb2_start: · line 278
Quoted 13 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +0
-0@@ -252,36 +243,30 @@ __efi64_mb2_start: /* We are on EFI platform and EFI boot services are available. */ incb efi_platform(%rip)@@ -291,77 +276,6 @@ __efi64_mb2_start: * be run on EFI platforms. */ incb skip_realmode(%rip)Well, these are two unfounded assumptions about the compile-time defaults of certain variables.
Lets fix it afterwards, to save interfering with this series.
xen/arch/x86/efi/parse-mbi2.c line 1
Quoted 7 lines
xen/arch/x86/efi/parse-mbi2.c new file +1
-0@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ GPL-2.0-only. The unsuffixed form is deprecated now.
Quoted 14 lines
xen/arch/x86/efi/parse-mbi2.c new file +14
-0+ +#include <xen/efi.h> +#include <xen/init.h> +#include <xen/multiboot2.h> +#include <asm/asm_defns.h> +#include <asm/efi.h> + +const char * asmlinkage __init +efi_multiboot2_prelude (uint32_t magic,const multiboot2_fixed_t *mbi) +{ +const multiboot2_tag_t *tag; +EFI_HANDLE ImageHandle =NULL ; +EFI_SYSTEM_TABLE *SystemTable =NULL ; +const char *cmdline =NULL ;xen/arch/x86/efi/parse-mbi2.c line 25
Quoted 10 lines
xen/arch/x86/efi/parse-mbi2.c new file +10
-0+bool have_bs = false; + +if ( magic !=MULTIBOOT2_BOOTLOADER_MAGIC ) +return "ERR: Not a Multiboot2 bootloader!" ; + +/* Skip Multiboot2 information fixed part. */ + tag =_p (ROUNDUP ((unsigned long )(mbi +1 ),MULTIBOOT2_TAG_ALIGN )); + + for ( ; (const void *)tag - (const void *)mbi < mbi->total_size + && tag->type != MULTIBOOT2_TAG_TYPE_END;&& on previous line.
Quoted 7 lines
Quoted 3 lines
But, this can move into the switch statement and reduce the
for()expression somewhat.I forgot to reply this, I even though about what to write. There are multiple reasons:
- having now a switch, it would require a goto/label or an additional variable to exit the loop,
Oh, of course. Sorry, my mistake. Leave it as is.
~Andrew
Re: [PATCH v4 2/4] x86/boot: Refactor BIOS/PVH start
In reply to Jan BeulichQuoted 29 lines · Jan Beulich
Quoted 15 lines · Andrew Cooper
xen/arch/x86/boot/head.S __pvh_start: · line 464
Quoted 11 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +4
-3@@ -449,62 +458,40 @@ __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) + /* Load null selector to unused segment registers. */ + xor %ecx, %ecx + mov %ecx, %fs + mov %ecx, %gsHonestly, the more I look at this, the more bizarre it is.
Quoted 2 lines
We should just set up %fs/gs like we do %ds/es, which in this case is simply to drop the comment and the xor.
What's bizarre here? As long as we don't use %fs/%gs, it doesn't matter much what we set them to. So yes, they may be set to what %ds etc are set to, but they may as well be marked unusable. Documentation-wise that's cleaner imo, as down the road - when a need to use one arises - it then won't require auditing of all code to figure where the register(s) is(are) actually used (just to find: nowhere). Even if a comment to this effect was left here, I for one wouldn't trust it in a couple of years time, but rather fear it went stale.
Quoted 1 line
Jan
Hi,
are you against this change and asking to roll it back?
Frediano
Re: [PATCH v4 2/4] x86/boot: Refactor BIOS/PVH start
Quoted 30 lines · Frediano Ziglio
Quoted 20 lines · Jan Beulich
Quoted 15 lines · Andrew Cooper
xen/arch/x86/boot/head.S __pvh_start: · line 464
Quoted 11 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +4
-3@@ -449,62 +458,40 @@ __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) + /* Load null selector to unused segment registers. */ + xor %ecx, %ecx + mov %ecx, %fs + mov %ecx, %gsHonestly, the more I look at this, the more bizarre it is.
Quoted 2 lines
We should just set up %fs/gs like we do %ds/es, which in this case is simply to drop the comment and the xor.
Quoted 8 lines
What's bizarre here? As long as we don't use %fs/%gs, it doesn't matter much what we set them to. So yes, they may be set to what %ds etc are set to, but they may as well be marked unusable. Documentation-wise that's cleaner imo, as down the road - when a need to use one arises - it then won't require auditing of all code to figure where the register(s) is(are) actually used (just to find: nowhere). Even if a comment to this effect was left here, I for one wouldn't trust it in a couple of years time, but rather fear it went stale.
Quoted 2 lines
Hi,
are you against this change and asking to roll it back?
Well, first of all I'm hoping for a reply by Andrew. Maybe I'm overlooking something. As it stands right now I indeed think we'd be better off keeping nul selectors in %fs and %gs.
Jan