Skip to contentSkip to conversation

Move some boot code from assembly to C

PATCH Superseded Frediano Ziglio 1 participant 5 messages Nov 14, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Move some boot code from assembly to C

As a continuation of this series start sorting out the issue of headers not compatible with 32 bit.
Instead of having to change headers which are almost only used for 64 bit allows to override headers or move reusable definitions to new shared headers.
This results in less changes.

Frediano Ziglio (4):
Use an include/boot directory to override headers for boot code x86/boot: Use header to allows inclusion of public xen.h header x86/boot: Move some settings to C
x86/boot: Use external symbols from cmdline_parse_early

 xen/arch/x86/boot/Makefile                |  2 +-
 xen/arch/x86/boot/build32.lds.S           |  4 ++++
 xen/arch/x86/boot/cmdline.c               | 14 ++++++++++--
 xen/arch/x86/boot/head.S                  | 19 +--------------
 xen/arch/x86/boot/reloc.c                 | 28 ++++++++++++++++++-----
 xen/arch/x86/boot/trampoline.S            |  2 +-
 xen/arch/x86/include/asm/guest/pvh-boot.h |  1 +
 xen/arch/x86/include/asm/setup.h          |  2 ++
 xen/arch/x86/include/boot/public/xen.h    | 28 +++++++++++++++++++++++
 xen/arch/x86/include/boot/xen/cpumask.h   |  1 +
 xen/arch/x86/include/boot/xen/string.h    | 10 ++++++++
 11 files changed, 83 insertions(+), 28 deletions(-)
 create mode 100644 xen/arch/x86/include/boot/public/xen.h
 create mode 100644 xen/arch/x86/include/boot/xen/cpumask.h
 create mode 100644 xen/arch/x86/include/boot/xen/string.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 Use an include/boot directory to override headers for boot code

Not all headers can be used by 32 bit boot code.
Allows to override some headers, we don't want to mess up with main headers as most of the code is only 64 bit so the easy stuff should be done for 64 bit declarations.
Boot headers should be 64 bit compatibles to avoid having multiple declarations.

---
 xen/arch/x86/boot/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/boot/Makefile +1 -1
index d457876659..13d4583173 100644
@@ -18,7 +18,7 @@ CFLAGS_x86_32 := $(subst -m64,-m32 -march=i686,$(XEN_TREEWIDE_CFLAGS))
 $(call cc-options-add,CFLAGS_x86_32,CC,$(EMBEDDED_EXTRA_CFLAGS))
 CFLAGS_x86_32 += -Werror -fno-builtin -g0 -msoft-float -mregparm=3
 CFLAGS_x86_32 += -nostdinc -include $(filter %/include/xen/config.h,$(XEN_CFLAGS))
-CFLAGS_x86_32 += $(filter -I% -O%,$(XEN_CFLAGS)) -D__XEN__
+CFLAGS_x86_32 += -I$(srctree)/arch/x86/include/boot $(filter -I% -O%,$(XEN_CFLAGS)) -D__XEN__
 
 # override for 32bit binaries
 $(obj32): CFLAGS_stack_boundary :=
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/4 x86/boot: Use header to allows inclusion of public xen.h header

In reply to Frediano Ziglio

This allows to include other headers and avoid duplicated declarations.

---
 xen/arch/x86/include/boot/public/xen.h | 28 ++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100644 xen/arch/x86/include/boot/public/xen.h
xen/arch/x86/include/boot/public/xen.h new file +28 -0
new file mode 100644
index 0000000000..399b86b5e5
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/* This header allows the inclusion of public xen.h */
+
+#ifndef BOOT__PUBLIC__XEN_H
+#define BOOT__PUBLIC__XEN_H
+
+#if !defined(__XEN__) || defined(__XEN_TOOLS__) || __XEN__ != 1
+#error Unexpected defines
+#endif
+
+#include <xen/types.h>
+
+#ifdef __i386__
+
+# define __XEN_TOOLS__ 1
+# undef __XEN__
+# include <public/arch-x86/xen.h>
+# define __XEN__ 1
+# undef __XEN_TOOLS__
+
+#else
+
+# include <public/arch-x86/xen.h>
+
+#endif
+
+#endif /* BOOT__PUBLIC__XEN_H */
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/4 x86/boot: Move some settings to C

In reply to Frediano Ziglio

Initialise multiboot_ptr and pvh_start_info_pa from C code.

---
 xen/arch/x86/boot/build32.lds.S           |  3 +++
 xen/arch/x86/boot/head.S                  | 10 --------
 xen/arch/x86/boot/reloc.c                 | 28 ++++++++++++++++++-----
 xen/arch/x86/include/asm/guest/pvh-boot.h |  1 +
 4 files changed, 26 insertions(+), 16 deletions(-)
xen/arch/x86/boot/build32.lds.S +3 -0
index 1e59732edd..1726c17c88 100644
@@ -51,6 +51,9 @@ SECTIONS
         DECLARE_IMPORT(__trampoline_seg_stop);
         DECLARE_IMPORT(trampoline_phys);
         DECLARE_IMPORT(boot_vid_info);
+        DECLARE_IMPORT(multiboot_ptr);
+        DECLARE_IMPORT(pvh_boot);
+        DECLARE_IMPORT(pvh_start_info_pa);
         . = . + GAP;
         *(.text)
         *(.text.*)
xen/arch/x86/boot/head.S +0 -10
index dcda91cfda..f86910294f 100644
@@ -515,16 +515,6 @@ trampoline_setup:
         /*      reloc(magic/eax, info/edx) using fastcall. */
         call    reloc
 
-#ifdef CONFIG_PVH_GUEST
-        cmpb    $0, sym_esi(pvh_boot)
-        je      1f
-        mov     %eax, sym_esi(pvh_start_info_pa)
-        jmp     2f
-#endif
-1:
-        mov     %eax, sym_esi(multiboot_ptr)
-2:
-
         /*
          * Now trampoline_phys points to the following structure (lowest address
          * is at the bottom):
xen/arch/x86/boot/reloc.c +22 -6
index e50e161b27..a03ef71a4c 100644
@@ -17,13 +17,15 @@
 #include <xen/types.h>
 
 #include <xen/kconfig.h>
-#include <xen/multiboot.h>
 #include <xen/multiboot2.h>
 #include <xen/page-size.h>
+#include <xen/bug.h>
 
 #include <asm/trampoline.h>
+#include <asm/setup.h>
 
 #include <public/arch-x86/hvm/start_info.h>
+#include <asm/guest/pvh-boot.h>
 
 #ifdef CONFIG_VIDEO
 # include "video.h"
@@ -347,28 +349,42 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, memctx *ctx)
 }
 
 /* SAF-1-safe */
-void *reloc(uint32_t magic, uint32_t in)
+void reloc(uint32_t magic, uint32_t in)
 {
     /* Get bottom-most low-memory stack address. */
     memctx ctx = { trampoline_phys + TRAMPOLINE_SPACE };
 
+    void *res;
+
     switch ( magic )
     {
     case MULTIBOOT_BOOTLOADER_MAGIC:
-        return mbi_reloc(in, &ctx);
+        res = mbi_reloc(in, &ctx);
+        break;
 
     case MULTIBOOT2_BOOTLOADER_MAGIC:
-        return mbi2_reloc(in, &ctx);
+        res = mbi2_reloc(in, &ctx);
+        break;
 
     case XEN_HVM_START_MAGIC_VALUE:
         if ( IS_ENABLED(CONFIG_PVH_GUEST) )
-            return pvh_info_reloc(in, &ctx);
+        {
+            res = pvh_info_reloc(in, &ctx);
+            break;
+        }
         /* Fallthrough */
 
     default:
         /* Nothing we can do */
-        return NULL;
+        res = NULL;
     }
+
+#ifdef CONFIG_PVH_GUEST
+    if ( pvh_boot )
+        pvh_start_info_pa = (unsigned long)res;
+#endif
+
+    multiboot_ptr = (unsigned long)res;
 }
 
 /*
xen/arch/x86/include/asm/guest/pvh-boot.h +1 -0
index 247ba6899e..b5ad2b11a4 100644
@@ -13,6 +13,7 @@
 #ifdef CONFIG_PVH_GUEST
 
 extern bool pvh_boot;
+extern uint32_t pvh_start_info_pa;
 
 void pvh_init(multiboot_info_t **mbi, module_t **mod);
 void pvh_print_info(void);
Signature · 1 line
2.34.1
#4 lore raw

PATCH 4/4 x86/boot: Use external symbols from cmdline_parse_early

In reply to Frediano Ziglio

Move some assembly code to C.

---
 xen/arch/x86/boot/build32.lds.S         |  1 +
 xen/arch/x86/boot/cmdline.c             | 14 ++++++++++++--
 xen/arch/x86/boot/head.S                |  9 +--------
 xen/arch/x86/boot/trampoline.S          |  2 +-
 xen/arch/x86/include/asm/setup.h        |  2 ++
 xen/arch/x86/include/boot/xen/cpumask.h |  1 +
 xen/arch/x86/include/boot/xen/string.h  | 10 ++++++++++
 7 files changed, 28 insertions(+), 11 deletions(-)
 create mode 100644 xen/arch/x86/include/boot/xen/cpumask.h
 create mode 100644 xen/arch/x86/include/boot/xen/string.h
xen/arch/x86/boot/build32.lds.S +1 -0
index 1726c17c88..652f951e52 100644
@@ -54,6 +54,7 @@ SECTIONS
         DECLARE_IMPORT(multiboot_ptr);
         DECLARE_IMPORT(pvh_boot);
         DECLARE_IMPORT(pvh_start_info_pa);
+        DECLARE_IMPORT(early_boot_opts);
         . = . + GAP;
         *(.text)
         *(.text.*)
xen/arch/x86/boot/cmdline.c +12 -2
index 196c580e91..7a8a3ed97f 100644
@@ -22,6 +22,9 @@
 #include <xen/kconfig.h>
 #include <xen/macros.h>
 #include <xen/types.h>
+#include <xen/multiboot.h>
+
+#include <asm/setup.h>
 
 #include "video.h"
 
@@ -39,6 +42,8 @@ typedef struct __packed {
 #endif
 } early_boot_opts_t;
 
+extern early_boot_opts_t early_boot_opts;
+
 /* Avoid pulling in all of ctypes.h for this. */
 #define tolower(c)	((c) | 0x20)
 
@@ -335,10 +340,15 @@ static void vga_parse(const char *cmdline, early_boot_opts_t *ebo)
 #endif
 
 /* SAF-1-safe */
-void cmdline_parse_early(const char *cmdline, early_boot_opts_t *ebo)
+void cmdline_parse_early(void)
 {
-    if ( !cmdline )
+    early_boot_opts_t *ebo = &early_boot_opts;
+    struct multiboot_info *mbi = (void *)multiboot_ptr;
+    const char *cmdline;
+
+    if ( !(mbi->flags & MBI_CMDLINE) || !mbi->cmdline )
         return;
+    cmdline = (void *)mbi->cmdline;
 
     ebo->skip_realmode = skip_realmode(cmdline);
     ebo->opt_edd = edd_parse(cmdline);
xen/arch/x86/boot/head.S +1 -8
index f86910294f..b79d4884df 100644
@@ -690,14 +690,7 @@ trampoline_setup:
         cmpb    $0, sym_esi(efi_platform)
         jnz     1f
 
-        /* Bail if there is no command line to parse. */
-        mov     sym_esi(multiboot_ptr), %ebx
-        testl   $MBI_CMDLINE,MB_flags(%ebx)
-        jz      1f
-
-        lea     sym_esi(early_boot_opts), %edx
-        mov     MB_cmdline(%ebx), %eax
-        /*      cmdline_parse_early(cmdline/eax, opts/edx) using fastcall. */
+        /*      cmdline_parse_early using fastcall. */
         call    cmdline_parse_early
 
 1:
xen/arch/x86/boot/trampoline.S +1 -1
index b8ab0ffdcb..f2fa7ca07d 100644
@@ -258,7 +258,7 @@ trampoline_boot_cpu_entry:
 
         .align  2
 /* Keep in sync with cmdline.c:early_boot_opts_t type! */
-early_boot_opts:
+GLOBAL(early_boot_opts)
 skip_realmode:
         .byte   0
 opt_edd:
xen/arch/x86/include/asm/setup.h +2 -0
index 25c15ef914..a5ca007722 100644
@@ -16,6 +16,8 @@ extern uint64_t boot_tsc_stamp;
 extern void *stack_start;
 extern unsigned int multiboot_ptr;
 
+struct domain;
+
 void early_cpu_init(bool verbose);
 void early_time_init(void);
 
xen/arch/x86/include/boot/xen/cpumask.h new file +1 -0
new file mode 100644
index 0000000000..046b862b78
@@ -0,0 +1 @@
+/* Empty. */
xen/arch/x86/include/boot/xen/string.h new file +10 -0
new file mode 100644
index 0000000000..6556d3b4af
@@ -0,0 +1,10 @@
+#ifndef BOOT__XEN__STRING_H
+#define BOOT__XEN__STRING_H
+
+#include <xen/types.h>	/* for size_t */
+
+void *memset(void *s, int c, size_t n);
+void *memcpy(void *dest, const void *src, size_t n);
+void *memmove(void *dest, const void *src, size_t n);
+
+#endif /* BOOT__XEN__STRING_H */
Signature · 1 line
2.34.1