Skip to contentSkip to conversation

Move some boot code from assembly to C

PATCH In review Frediano Ziglio 2 participants 18 messages Nov 22, 2024 – Dec 10, 2024

lore
Share & tools
lore
#0 lore raw

v2 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.

Changes since v1:
- rebased (with conflicts).

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 v2 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

Re: [PATCH v2 1/4] Use an include/boot directory to override headers for boot code

Quoted 6 lines · Frediano Ziglio

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.

I'm afraid that in isolation it's not clear what is intended. Boot code is all located in a single directory. Can't we use local headers there, using #include "...", instead of ...

xen/arch/x86/boot/Makefile CFLAGS_x86_32 := $(subst -m64,-m32 -march=i686,$(XEN_TREEWIDE_CFLAGS)) · line 21

Quoted 8 lines
xen/arch/x86/boot/Makefile +1 -1
@@ -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__

... introducing a arch-wide subdir, which non-boot code could easily (ab)use?

Jan

#3 lore raw

Re: [PATCH v2 1/4] Use an include/boot directory to override headers for boot code

Quoted 9 lines · Jan Beulich
Quoted 6 lines · Frediano Ziglio

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.

Quoted 4 lines

I'm afraid that in isolation it's not clear what is intended. Boot code is all located in a single directory. Can't we use local headers there, using #include "...", instead of ...

That approach was refused. Some definitions are in the headers (like CPU features for instance) but duplicating the definitions was rejected as a solution.
So the idea is to reuse such definitions. But, as stated in the comment, the "x86" includes are not for x86, but most of them are just for x64.This for historic reasons. But most of the code is x64 only so changing headers to be x86 compatible would complicate them for a minimal gain.

Quoted 11 lines

xen/arch/x86/boot/Makefile CFLAGS_x86_32 := $(subst -m64,-m32 -march=i686,$(XEN_TREEWIDE_CFLAGS)) · line 21

Quoted 8 lines
xen/arch/x86/boot/Makefile +1 -1
@@ -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__

... introducing a arch-wide subdir, which non-boot code could easily (ab)use?

You would have to explicitly add the "boot" into the path, it does not seem "easy" to me, but if you really want to do it you can do with these or any other headers, like simply "#include "../arch/arm/..." in x64 code. Same easiness.

Quoted 1 line

Jan

Frediano

#4 lore raw

Re: [PATCH v2 1/4] Use an include/boot directory to override headers for boot code

Quoted 10 lines · Frediano Ziglio
Quoted 9 lines · Jan Beulich
Quoted 6 lines · Frediano Ziglio

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.

Quoted 6 lines
Quoted 4 lines

I'm afraid that in isolation it's not clear what is intended. Boot code is all located in a single directory. Can't we use local headers there, using #include "...", instead of ...

That approach was refused.

Can you provide a reference please?

Quoted 8 lines

Some definitions are in the headers (like
CPU features for instance) but duplicating the definitions was rejected as a solution.
So the idea is to reuse such definitions. But, as stated in the comment, the "x86" includes are not for x86, but most of them are just for x64.This for historic reasons. But most of the code is x64 only so changing headers to be x86 compatible would complicate them for a minimal gain.

Yet the amount of what wants sharing ought to be low. It might even help to clarify what it is that is meant to become shared.

Quoted 11 lines
Quoted 10 lines

xen/arch/x86/boot/Makefile CFLAGS_x86_32 := $(subst -m64,-m32 -march=i686,$(XEN_TREEWIDE_CFLAGS)) · line 21

Quoted 8 lines
xen/arch/x86/boot/Makefile +1 -1
@@ -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__

... introducing a arch-wide subdir, which non-boot code could easily (ab)use?

Quoted 1 line

You would have to explicitly add the "boot" into the path,

No different from "hvm" or "guest", just to give two examples.

Quoted 4 lines

it does not
seem "easy" to me, but if you really want to do it you can do with these or any other headers, like simply "#include "../arch/arm/..." in x64 code. Same easiness.

Well, no, the ".." in #include directives certainly stands out.

Jan

#5 lore raw

PATCH v2 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
#6 lore raw

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

Quoted 3 lines · Frediano Ziglio

This allows to include other headers and avoid duplicated declarations.

Again it's left unclear what the purpose / goal is.

xen/arch/x86/include/boot/public/xen.h line 10

Quoted 13 lines
xen/arch/x86/include/boot/public/xen.h new file +10 -0
@@ -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

What is this to guard against? We're in the Xen tree, building Xen.

xen/arch/x86/include/boot/public/xen.h line 20

Quoted 9 lines
xen/arch/x86/include/boot/public/xen.h new file +9 -0
+#include <xen/types.h>
+
+#ifdef __i386__
+
+# define __XEN_TOOLS__ 1
+# undef __XEN__
+# include <public/arch-x86/xen.h>
+# define __XEN__ 1
+# undef __XEN_TOOLS__

Why would __XEN__ need un-defining and __XEN_TOOLS__ (seemingly wrongly) need defining? (As an aside, I don't think the expansion of either macro really matters. IOW I don#t see the need for the two 1-s.)

Jan

#7 lore raw

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

Quoted 8 lines · Jan Beulich
Quoted 3 lines · Frediano Ziglio

This allows to include other headers and avoid duplicated declarations.

Again it's left unclear what the purpose / goal is.

Reduce duplication avoiding duplicate declarations. The alternative would be to duplicate them, which was proposed already and refused as duplication was not good.

Quoted 7 lines
Quoted 7 lines
xen/arch/x86/include/boot/public/xen.h new file +4 -0
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/* This header allows the inclusion of public xen.h */
+
Quoted 9 lines

xen/arch/x86/include/boot/public/xen.h line 10

Quoted 6 lines
xen/arch/x86/include/boot/public/xen.h new file +6 -0
+#ifndef BOOT__PUBLIC__XEN_H
+#define BOOT__PUBLIC__XEN_H
+
+#if !defined(__XEN__) || defined(__XEN_TOOLS__) || __XEN__ != 1
+#error Unexpected defines
+#endif

What is this to guard against? We're in the Xen tree, building Xen.

In include/public/arch-x86/xen.h file there are these declarations:

#if defined(__i386__)

# ifdef __XEN__
__DeFiNe__ __DECL_REG_LO8(which) uint32_t e ## which ## x
__DeFiNe__ __DECL_REG_LO16(name) union { uint32_t e ## name; }
# endif
#include "xen-x86_32.h"
# ifdef __XEN__
__UnDeF__ __DECL_REG_LO8
__UnDeF__ __DECL_REG_LO16
__DeFiNe__ __DECL_REG_LO8(which) e ## which ## x
__DeFiNe__ __DECL_REG_LO16(name) e ## name
# endif
#elif defined(__x86_64__)
#include "xen-x86_64.h"
#endif

This header allows us to include that part without compiler errors due to __DeFiNe__ and __UnDeF__ not being C code.

Quoted 14 lines

xen/arch/x86/include/boot/public/xen.h line 20

Quoted 9 lines
xen/arch/x86/include/boot/public/xen.h new file +9 -0
+#include <xen/types.h>
+
+#ifdef __i386__
+
+# define __XEN_TOOLS__ 1
+# undef __XEN__
+# include <public/arch-x86/xen.h>
+# define __XEN__ 1
+# undef __XEN_TOOLS__

Why would __XEN__ need un-defining and __XEN_TOOLS__ (seemingly wrongly) need defining? (As an aside, I don't think the expansion of either macro really matters. IOW I don#t see the need for the two 1-s.)

Quoted 1 line

Jan

The "1"-s are the default definitions, simply I define them temporarily and then restore them.

Frediano

#8 lore raw

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

Quoted 5 lines · Frediano Ziglio
Quoted 4 lines · Jan Beulich
Quoted 2 lines · Frediano Ziglio

This allows to include other headers and avoid duplicated declarations.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Again it's left unclear what the purpose / goal is.

Reduce duplication avoiding duplicate declarations. The alternative would be to duplicate them, which was proposed already and refused as duplication was not good.

Which declarations specifically?

Quoted 35 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/include/boot/public/xen.h new file +4 -0
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/* This header allows the inclusion of public xen.h */
+
Quoted 9 lines

xen/arch/x86/include/boot/public/xen.h line 10

Quoted 6 lines
xen/arch/x86/include/boot/public/xen.h new file +6 -0
+#ifndef BOOT__PUBLIC__XEN_H
+#define BOOT__PUBLIC__XEN_H
+
+#if !defined(__XEN__) || defined(__XEN_TOOLS__) || __XEN__ != 1
+#error Unexpected defines
+#endif

What is this to guard against? We're in the Xen tree, building Xen.

In include/public/arch-x86/xen.h file there are these declarations:

#if defined(__i386__)

# ifdef __XEN__
__DeFiNe__ __DECL_REG_LO8(which) uint32_t e ## which ## x
__DeFiNe__ __DECL_REG_LO16(name) union { uint32_t e ## name; }
# endif
#include "xen-x86_32.h"
# ifdef __XEN__
__UnDeF__ __DECL_REG_LO8
__UnDeF__ __DECL_REG_LO16
__DeFiNe__ __DECL_REG_LO8(which) e ## which ## x
__DeFiNe__ __DECL_REG_LO16(name) e ## name
# endif
#elif defined(__x86_64__)
#include "xen-x86_64.h"
#endif
Quoted 2 lines

This header allows us to include that part without compiler errors due to __DeFiNe__ and __UnDeF__ not being C code.

And why exactly can't 32-bit code simply include the compat variant of the public header, which is being generated by processing those non-C constructs?

Jan

#9 lore raw

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

Quoted 53 lines · Jan Beulich
Quoted 5 lines · Frediano Ziglio
Quoted 4 lines · Jan Beulich
Quoted 2 lines · Frediano Ziglio

This allows to include other headers and avoid duplicated declarations.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Again it's left unclear what the purpose / goal is.

Reduce duplication avoiding duplicate declarations. The alternative would be to duplicate them, which was proposed already and refused as duplication was not good.

Which declarations specifically?

Quoted 35 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/include/boot/public/xen.h new file +4 -0
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/* This header allows the inclusion of public xen.h */
+
Quoted 9 lines

xen/arch/x86/include/boot/public/xen.h line 10

Quoted 6 lines
xen/arch/x86/include/boot/public/xen.h new file +6 -0
+#ifndef BOOT__PUBLIC__XEN_H
+#define BOOT__PUBLIC__XEN_H
+
+#if !defined(__XEN__) || defined(__XEN_TOOLS__) || __XEN__ != 1
+#error Unexpected defines
+#endif

What is this to guard against? We're in the Xen tree, building Xen.

In include/public/arch-x86/xen.h file there are these declarations:

#if defined(__i386__)

# ifdef __XEN__
__DeFiNe__ __DECL_REG_LO8(which) uint32_t e ## which ## x
__DeFiNe__ __DECL_REG_LO16(name) union { uint32_t e ## name; }
# endif
#include "xen-x86_32.h"
# ifdef __XEN__
__UnDeF__ __DECL_REG_LO8
__UnDeF__ __DECL_REG_LO16
__DeFiNe__ __DECL_REG_LO8(which) e ## which ## x
__DeFiNe__ __DECL_REG_LO16(name) e ## name
# endif
#elif defined(__x86_64__)
#include "xen-x86_64.h"
#endif
Quoted 7 lines
Quoted 2 lines

This header allows us to include that part without compiler errors due to __DeFiNe__ and __UnDeF__ not being C code.

And why exactly can't 32-bit code simply include the compat variant of the public header, which is being generated by processing those non-C constructs?

I suppose I could solve that specific issue in that way. Where are they generated?

Quoted 1 line

Jan

Frediano

#10 lore raw

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

Quoted 42 lines · Frediano Ziglio
Quoted 41 lines · Jan Beulich
Quoted 39 lines · Frediano Ziglio
Quoted 17 lines · Jan Beulich

xen/arch/x86/include/boot/public/xen.h line 10

Quoted 13 lines · Frediano Ziglio
xen/arch/x86/include/boot/public/xen.h new file +10 -0
@@ -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

What is this to guard against? We're in the Xen tree, building Xen.

In include/public/arch-x86/xen.h file there are these declarations:

#if defined(__i386__)

# ifdef __XEN__
__DeFiNe__ __DECL_REG_LO8(which) uint32_t e ## which ## x
__DeFiNe__ __DECL_REG_LO16(name) union { uint32_t e ## name; }
# endif
#include "xen-x86_32.h"
# ifdef __XEN__
__UnDeF__ __DECL_REG_LO8
__UnDeF__ __DECL_REG_LO16
__DeFiNe__ __DECL_REG_LO8(which) e ## which ## x
__DeFiNe__ __DECL_REG_LO16(name) e ## name
# endif
#elif defined(__x86_64__)
#include "xen-x86_64.h"
#endif

This header allows us to include that part without compiler errors due to __DeFiNe__ and __UnDeF__ not being C code.

Quoted 6 lines
Quoted 3 lines

And why exactly can't 32-bit code simply include the compat variant of the public header, which is being generated by processing those non-C constructs?

I suppose I could solve that specific issue in that way. Where are they generated?

include/Makefile generates them into include/compat/.

Jan

#11 lore raw

PATCH v2 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 1b3bd16fe5..510b3cfe6c 100644
@@ -517,16 +517,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:
-
         /* Interrogate CPU extended features via CPUID. */
         mov     $1, %eax
         cpuid
xen/arch/x86/boot/reloc.c +22 -6
index 7a375ad41c..8f757813ee 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,27 +349,41 @@ 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)
 {
     memctx ctx = { trampoline_phys + TRAMPOLINE_HEAP_END };
 
+    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
#12 lore raw

Re: [PATCH v2 3/4] x86/boot: Move some settings to C

Quoted 9 lines · 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(-)

From the diffstat alone - is this really a win?

Quoted 39 lines
xen/arch/x86/boot/head.S +0 -10
@@ -517,16 +517,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:
-
         /* Interrogate CPU extended features via CPUID. */
         mov     $1, %eax
         cpuid
xen/arch/x86/boot/reloc.c +3 -1
@@ -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"

xen/arch/x86/boot/reloc.c static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, memctx *ctx) · line 357

Quoted 10 lines
xen/arch/x86/boot/reloc.c +3 -1
@@ -347,27 +349,41 @@ 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)
 {
     memctx ctx = { trampoline_phys + TRAMPOLINE_HEAP_END };
 
+    void *res;
+

Nit: Please avoid blank lines between declarations unless the set of locals is huge, or some really need to stand out.

Quoted 15 lines
xen/arch/x86/boot/reloc.c +4 -3
     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);

xen/arch/x86/boot/reloc.c static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, memctx *ctx) · line 378

Quoted 10 lines
xen/arch/x86/boot/reloc.c +5 -1
+        {
+            res = pvh_info_reloc(in, &ctx);
+            break;
+        }
         /* Fallthrough */
 
     default:
         /* Nothing we can do */
-        return NULL;
+        res = NULL;

Simply keep returning here? No need to write the NULL when the variables start out zeroed?

xen/arch/x86/boot/reloc.c static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, memctx *ctx) · line 386

Quoted 8 lines
xen/arch/x86/boot/reloc.c +7 -0
     }
+
+#ifdef CONFIG_PVH_GUEST
+    if ( pvh_boot )
+        pvh_start_info_pa = (unsigned long)res;
+#endif
+
+    multiboot_ptr = (unsigned long)res;

In the assembly original this is an "else" to the if().

Jan

#13 lore raw

Re: [PATCH v2 3/4] x86/boot: Move some settings to C

Quoted 12 lines · Jan Beulich
Quoted 9 lines · 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(-)
Quoted 2 lines

From the diffstat alone - is this really a win?

Yes, C can be longer then assembly, consider calling a function, assembly:

foo:

call foo

C:

void foo(int x); // declaration (maybe in a separate header)

   void foo(int x) {
       ...
   }

   foo(123);

yes, much longer. Actually we could avoid the declaration, but usually we explicitly force the compiler to complains about a missing declaration. The reason is that usually programmers prefer the compiler to avoid crashes and check for the passed parameters. This requires more code but pay back the time not having to debug crashes.

If you look more at wide range (so, not only at this patch) the code for a bit increases adding new files and symbols but after a while the code starts to reduce (once added headers and preparation).

Quoted 47 lines
Quoted 47 lines
xen/arch/x86/boot/head.S +0 -10
@@ -517,16 +517,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:
-
         /* Interrogate CPU extended features via CPUID. */
         mov     $1, %eax
         cpuid
xen/arch/x86/boot/reloc.c +4 -2
@@ -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,27 +349,41 @@ 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)
 {
     memctx ctx = { trampoline_phys + TRAMPOLINE_HEAP_END };
 
Quoted 6 lines

xen/arch/x86/boot/reloc.c static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, memctx *ctx) · line 357

Quoted 2 lines
xen/arch/x86/boot/reloc.c +2 -0
+    void *res;
+

Nit: Please avoid blank lines between declarations unless the set of locals is huge, or some really need to stand out.

Noted.

Quoted 21 lines
Quoted 21 lines
xen/arch/x86/boot/reloc.c +8 -3
     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 */
 
Quoted 8 lines

xen/arch/x86/boot/reloc.c static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, memctx *ctx) · line 378

Quoted 4 lines
xen/arch/x86/boot/reloc.c +1 -1
     default:
         /* Nothing we can do */
-        return NULL;
+        res = NULL;

Simply keep returning here? No need to write the NULL when the variables start out zeroed?

Yes, considering pvh_start_info_pa and multiboot_ptr should be already NULL it makes sense

Quoted 11 lines

xen/arch/x86/boot/reloc.c static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, memctx *ctx) · line 386

Quoted 8 lines
xen/arch/x86/boot/reloc.c +7 -0
     }
+
+#ifdef CONFIG_PVH_GUEST
+    if ( pvh_boot )
+        pvh_start_info_pa = (unsigned long)res;
+#endif
+
+    multiboot_ptr = (unsigned long)res;

In the assembly original this is an "else" to the if().

I suppose the return change above would solve also this.

Quoted 1 line

Jan

Frediano

#14 lore raw

Re: [PATCH v2 3/4] x86/boot: Move some settings to C

Quoted 47 lines · Frediano Ziglio
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Frediano Ziglio

switch ( magic )
{
case MULTIBOOT_BOOTLOADER_MAGIC:

xen/arch/x86/boot/reloc.c +8 -3
-        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 */
Quoted 8 lines

xen/arch/x86/boot/reloc.c static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, memctx *ctx) · line 378

Quoted 4 lines
     default:
         /* Nothing we can do */
xen/arch/x86/boot/reloc.c +1 -1
-        return NULL;
+        res = NULL;

Simply keep returning here? No need to write the NULL when the variables start out zeroed?

Yes, considering pvh_start_info_pa and multiboot_ptr should be already NULL it makes sense

Quoted 11 lines

xen/arch/x86/boot/reloc.c static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, memctx *ctx) · line 386

Quoted 8 lines
     }
xen/arch/x86/boot/reloc.c +7 -0
+
+#ifdef CONFIG_PVH_GUEST
+    if ( pvh_boot )
+        pvh_start_info_pa = (unsigned long)res;
+#endif
+
+    multiboot_ptr = (unsigned long)res;

In the assembly original this is an "else" to the if().

Quoted 1 line

I suppose the return change above would solve also this.

I'm not convinced of this.

Jan

#15 lore raw

PATCH v2 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 510b3cfe6c..49bacee225 100644
@@ -675,14 +675,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 924bda37c1..204c9bc889 100644
@@ -255,7 +255,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 5c2391a868..c461d5d3a7 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
#16 lore raw

Re: [PATCH v2 4/4] x86/boot: Use external symbols from cmdline_parse_early

Quoted 14 lines · 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

Again the diffstat doesn't really suggest this is a win. As an upside I can see that the argument passing to the function is somewhat ugly when done from assembly, especially when the function needs new parameters added or ones removed / changed. The downside is that now you're switching to dealing with globals, which generally seems less desirable.

xen/arch/x86/include/asm/setup.h extern uint64_t boot_tsc_stamp; · line 23

Quoted 11 lines
xen/arch/x86/include/asm/setup.h +2 -0
@@ -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);
 

While I think I can see why this would be needed, personally I think such forward decls belong either immediately past all #include-s or immediately ahead of where they are first needed.

xen/arch/x86/include/boot/xen/cpumask.h line 1

Quoted 4 lines
xen/arch/x86/include/boot/xen/cpumask.h new file +1 -0
@@ -0,0 +1 @@
+/* Empty. */

Are there perhaps better ways to deal with whatever needs dealing with (which sadly isn't obvious and also isn't mentioned anywhere)? At a guess, asm/numa.h may be where the problem is, yet then setup.h includes that just to get a decl of nodeid_t afaics. As we're meaning to split headers into two or perhaps even three parts anyway (to allow reducing dependency chains), maybe we should do so here and introduce e.g. asm/types/numa.h?

Jan

#17 lore raw
In reply to Frediano Ziglio

ping

Quoted 33 lines · Frediano Ziglio

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.

Changes since v1:
- rebased (with conflicts).

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