Skip to contentSkip to conversation

Reuse 32 bit C code more safely

PATCH Superseded Frediano Ziglio 2 participants 12 messages Oct 7, 2024 – Oct 10, 2024

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover Reuse 32 bit C code more safely

This series attempt to:
- use more C code, that is replace some assembly code with C;
- avoid some code duplication between C and assembly;
- prevent some issues having relocations in C code.

The idea is extending the current C to binary code conversion done for 32 bit C code called from head.S making sure relocations are safe and allowing external symbols usage from C code.

Note that, as an addition, scripts generating code check for no data to allow code and data separation.

More details of the implementation are in commit message 3/6, which is the largest patch.
Patch 4/6 reuses code to relocate the trampoline between 32 and 64 bit. Patches 5/6 and 6/6 move some code from assembly to C.

Other RFC commits were excluded, I didn't manage to entangle the issues sharing headers between 32 and 64 bits.
Since RFC code was more tested, also with CI and some incompatibility were fixed. On that it's weird that we need Python 3.8 for Qemu but we still use Python 2 for Xen. Shouldn't we bump requirement to Python 3 even for Xen?

Code boot successfully using:
- BIOS boot;
- EFI boot with Grub2 and ELF file;
- direct EFI boot without Grub.

Code is currently based on "master" branch, currently commit 6b20755c24ccd69d2482b45dfb9193940cdc346e.

Changes since v1:
- 2 preliminary commits for adjust .gitignore;
- last commit split (one variable at a time);
- lot of style and names changes;
- first commit, now 3/6 had some build changes, more details on commit message.

Frediano Ziglio (6):
.gitignore: Remove not generated files
x86/boot: Rationalise .gitignore
x86/boot: create a C bundle for 32 bit boot code and use it x86/boot: Reuse code to relocate trampoline x86/boot: Use trampoline_phys variable directly from C code x86/boot: Use boot_vid_info variable directly from C code

 .gitignore                                    |   5 -
 xen/arch/x86/boot/.gitignore                  |   4 +
 xen/arch/x86/boot/Makefile                    |  62 ++++--
 .../x86/boot/{build32.lds => build32.lds.S}   |  49 ++++-
 xen/arch/x86/boot/cmdline.c                   |  12 --
 xen/arch/x86/boot/head.S                      |  49 +----
 xen/arch/x86/boot/reloc-trampoline.c          |  36 ++++
 xen/arch/x86/boot/reloc.c                     |  33 +--
 xen/arch/x86/efi/efi-boot.h                   |  15 +-
 xen/tools/combine_two_binaries                | 198 ++++++++++++++++++
 10 files changed, 344 insertions(+), 119 deletions(-)
 create mode 100644 xen/arch/x86/boot/.gitignore
 rename xen/arch/x86/boot/{build32.lds => build32.lds.S} (57%)
 create mode 100644 xen/arch/x86/boot/reloc-trampoline.c
 create mode 100755 xen/tools/combine_two_binaries
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/6 .gitignore: Remove not generated files

Both reloc.S and cmdline.S are not generated since commit d58a509e01c4833515a9a8340706d1a23bef168b ("build,x86: remove the need for build32.mk").

---
 .gitignore | 2 --
 1 file changed, 2 deletions(-)
.gitignore +0 -2
index d88be80b28..5a75b204f7 100644
@@ -248,8 +248,6 @@ xen/.config.old
 xen/.xen.elf32
 xen/System.map
 xen/arch/x86/boot/mkelf32
-xen/arch/x86/boot/cmdline.S
-xen/arch/x86/boot/reloc.S
 xen/arch/x86/boot/*.bin
 xen/arch/x86/boot/*.lnk
 xen/arch/x86/efi.lds
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/6 x86/boot: Rationalise .gitignore

In reply to Frediano Ziglio

Strip all related content out of the root .gitignore, and provide a more local .gitignore's with up-to-date patterns.

---
 .gitignore                   | 3 ---
 xen/arch/x86/boot/.gitignore | 3 +++
 2 files changed, 3 insertions(+), 3 deletions(-)
 create mode 100644 xen/arch/x86/boot/.gitignore
.gitignore +0 -3
index 5a75b204f7..dae97ebfd7 100644
@@ -247,9 +247,6 @@ xen/.config
 xen/.config.old
 xen/.xen.elf32
 xen/System.map
-xen/arch/x86/boot/mkelf32
-xen/arch/x86/boot/*.bin
-xen/arch/x86/boot/*.lnk
 xen/arch/x86/efi.lds
 xen/arch/x86/efi/check.efi
 xen/arch/x86/efi/mkreloc
xen/arch/x86/boot/.gitignore new file +3 -0
new file mode 100644
index 0000000000..a379db7988
@@ -0,0 +1,3 @@
+/mkelf32
+/*.bin
+/*.lnk
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 3/6 x86/boot: create a C bundle for 32 bit boot code and use it

In reply to Frediano Ziglio

The current method to include 32 bit C boot code is:
- compile each function we want to use into a separate object file;
- each function is compiled with -fpic option;
- convert these object files to binary files. This operation removes GOP which we don't want in the executable;
- a small assembly part in each file add the entry point;
- code can't have external references, all possible variables are passed by value or pointer;
- include these binary files in head.S.

There are currently some limitations:
- code is compiled separately, it's not possible to share a function (like memcpy) between different functions to use;
- although code is compiled with -fpic there's no certainty there are no relocations, specifically data ones. This can lead into hard to find bugs;
- it's hard to add a simple function;
- having to pass external variables makes hard to do multiple things otherwise functions would require a lot of parameters so code would have to be split into multiple functions which is not easy;
- we generate a single text section containing data and code, not a problem at the moment but if we want to add W^X protection it's not helpful.

Current change extends the current process:
- all object files are linked together before getting converted making possible to share code between the function we want to call;
- a single object file is generated with all functions to use and exported symbols to easily call;
- variables to use are declared in linker script and easily used inside C code. Declaring them manually could be annoying but makes also easier to check them. Using external pointers can be still an issue if they are not fixed. If an external symbol is not declared this gives a link error;
- linker script put data (bss and data) into a separate section and check that that section is empty making sure code is W^X compatible;

Some details of the implementation:
- C code is compiled with -fpic flags (as before);
- object files from C code are linked together;
- the single bundled object file is linked with 2 slightly different script files to generate 2 bundled object files;
- the 2 bundled object files are converted to binary removing the need for global offset tables;
- a Python script is used to generate assembly source from the 2 binaries;
- the single assembly file is compiled to generate final bundled object file;
- to detect possible unwanted relocation in data/code code is generated with different addresses. This is enforced starting .text section at different positions and adding a fixed "gap" at the beginning. This makes sure code and data is position independent;
- to detect used symbols in data/code symbols are placed in .text section at different offsets (based on the line in the linker script). This is needed as potentially a reference to a symbol is converted to a reference to the containing section so multiple symbols could be converted to reference to same symbol (section name) and we need to distinguish them;
- to avoid relocations
- --orphan-handling=error option to linker is used to make sure we account for all possible sections from C code;

Current limitations:
- the main one is the lack of support for 64 bit code. It would make sure that even the code used for 64 bit (at the moment EFI code) is code and data position independent. We cannot assume that code that came from code compiled for 32 bit and compiled for 64 bit is code and data position independent, different compiler options lead to different code/data.

---
Changes since v1:
- separate lines adding files in Makefile;
- remove unneeded "#undef ENTRY" in build32.lds.S;
- print some information from combine_two_binaries only passing --verbose;
- detect --orphan-handling=error option dynamically;
- define and use a LD32;
- more intermediate targets to build more in parallel;
- use obj32 in Makefile to keep list of 32 bit object files;
- 32 bit object files are now named XXX.32.o;
- rename "cbundle" to "built_in_32".

---
 xen/arch/x86/boot/.gitignore                  |   5 +-
 xen/arch/x86/boot/Makefile                    |  58 +++--
 .../x86/boot/{build32.lds => build32.lds.S}   |  43 +++-
 xen/arch/x86/boot/cmdline.c                   |  12 --
 xen/arch/x86/boot/head.S                      |  12 --
 xen/arch/x86/boot/reloc.c                     |  14 --
 xen/tools/combine_two_binaries                | 198 ++++++++++++++++++
 7 files changed, 283 insertions(+), 59 deletions(-)
 rename xen/arch/x86/boot/{build32.lds => build32.lds.S} (63%)
 create mode 100755 xen/tools/combine_two_binaries
xen/arch/x86/boot/.gitignore +3 -2
index a379db7988..ebad650e5c 100644
@@ -1,3 +1,4 @@
 /mkelf32
-/*.bin
-/*.lnk
+/build32.*.lds
+/built_in_32.*.bin
+/built_in_32.*.map
xen/arch/x86/boot/Makefile +45 -13

77 lines not on this page · raw message

xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +37 -6

62 lines not on this page · raw message

xen/arch/x86/boot/cmdline.c +0 -12
index fc9241ede9..196c580e91 100644
@@ -18,18 +18,6 @@
  * Linux kernel source (linux/lib/string.c).
  */
 
-/*
- * This entry point is entered from xen/arch/x86/boot/head.S with:
- *   - %eax      = &cmdline,
- *   - %edx      = &early_boot_opts.
- */
-asm (
-    "    .text                         \n"
-    "    .globl _start                 \n"
-    "_start:                           \n"
-    "    jmp  cmdline_parse_early      \n"
-    );
-
 #include <xen/compiler.h>
 #include <xen/kconfig.h>
 #include <xen/macros.h>
xen/arch/x86/boot/head.S +0 -12
index c4de1dfab5..e0776e3896 100644
@@ -759,18 +759,6 @@ trampoline_setup:
         /* Jump into the relocated trampoline. */
         lret
 
-        /*
-         * cmdline and reloc are written in C, and linked to be 32bit PIC with
-         * entrypoints at 0 and using the fastcall convention.
-         */
-FUNC_LOCAL(cmdline_parse_early)
-        .incbin "cmdline.bin"
-END(cmdline_parse_early)
-
-FUNC_LOCAL(reloc)
-        .incbin "reloc.bin"
-END(reloc)
-
 ENTRY(trampoline_start)
 #include "trampoline.S"
 ENTRY(trampoline_end)
xen/arch/x86/boot/reloc.c +0 -14
index 8c58affcd9..94b078d7b1 100644
@@ -12,20 +12,6 @@
  *    Daniel Kiper <[email protected]>
  */
 
-/*
- * This entry point is entered from xen/arch/x86/boot/head.S with:
- *   - %eax       = MAGIC,
- *   - %edx       = INFORMATION_ADDRESS,
- *   - %ecx       = TOPMOST_LOW_MEMORY_STACK_ADDRESS.
- *   - 0x04(%esp) = BOOT_VIDEO_INFO_ADDRESS.
- */
-asm (
-    "    .text                         \n"
-    "    .globl _start                 \n"
-    "_start:                           \n"
-    "    jmp  reloc                    \n"
-    );
-
 #include <xen/compiler.h>
 #include <xen/macros.h>
 #include <xen/types.h>
xen/tools/combine_two_binaries new file +198 -0

198 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

PATCH v2 4/6 x86/boot: Reuse code to relocate trampoline

In reply to Frediano Ziglio
---
 xen/arch/x86/boot/Makefile           | 12 ++++++----
 xen/arch/x86/boot/build32.lds.S      |  5 ++++
 xen/arch/x86/boot/head.S             | 23 +-----------------
 xen/arch/x86/boot/reloc-trampoline.c | 36 ++++++++++++++++++++++++++++
 xen/arch/x86/efi/efi-boot.h          | 15 ++----------
 5 files changed, 52 insertions(+), 39 deletions(-)
 create mode 100644 xen/arch/x86/boot/reloc-trampoline.c
xen/arch/x86/boot/Makefile +8 -4
index 4cf0d7e140..24b4b0cb17 100644
@@ -1,14 +1,18 @@
 obj32 := cmdline.o
 obj32 += reloc.o
+obj32 += reloc-trampoline.o
+
+obj64 := reloc-trampoline.o
 
 obj-bin-y += head.o
 obj-bin-y += built_in_32.o
+obj-bin-y += $(obj64)
 
 obj32 := $(patsubst %.o,%.32.o,$(obj32))
 
-nocov-y   += $(obj32)
-noubsan-y += $(obj32)
-targets   += $(obj32)
+nocov-y   += $(obj32) $(obj64)
+noubsan-y += $(obj32) $(obj64)
+targets   += $(obj32) $(obj64)
 
 obj32 := $(addprefix $(obj)/,$(obj32))
 
@@ -55,7 +59,7 @@ $(obj)/built_in_32.o: $(obj)/built_in_32.other.bin $(obj)/built_in_32.final.bin
 		--script $(obj)/build32.final.lds \
 		--bin1 $(obj)/built_in_32.other.bin --bin2 $(obj)/built_in_32.final.bin \
 		--map $(obj)/built_in_32.final.map \
-		--exports cmdline_parse_early,reloc \
+		--exports cmdline_parse_early,reloc,reloc_trampoline32 \
 		--section-header '.section .init.text, "ax", @progbits' \
 		--output $(obj)/built_in_32.s
 	$(CC) -c $(obj)/built_in_32.s -o [email protected]
xen/arch/x86/boot/build32.lds.S +5 -0
index 72a4c5c614..9d09e3adbd 100644
@@ -41,6 +41,11 @@ SECTIONS
          * Potentially they should be all variables. */
         DECLARE_IMPORT(__base_relocs_start);
         DECLARE_IMPORT(__base_relocs_end);
+        DECLARE_IMPORT(__trampoline_rel_start);
+        DECLARE_IMPORT(__trampoline_rel_stop);
+        DECLARE_IMPORT(__trampoline_seg_start);
+        DECLARE_IMPORT(__trampoline_seg_stop);
+        DECLARE_IMPORT(trampoline_phys);
         . = . + GAP;
         *(.text)
         *(.text.*)
xen/arch/x86/boot/head.S +1 -22
index e0776e3896..ade2c5c43d 100644
@@ -706,28 +706,7 @@ trampoline_setup:
         mov     %edx, sym_offs(l1_bootmap)(%esi, %ecx, 8)
 
         /* Apply relocations to bootstrap trampoline. */
-        mov     sym_esi(trampoline_phys), %edx
-        lea     sym_esi(__trampoline_rel_start), %edi
-        lea     sym_esi(__trampoline_rel_stop), %ecx
-1:
-        mov     (%edi), %eax
-        add     %edx, (%edi, %eax)
-        add     $4,%edi
-
-        cmp     %ecx, %edi
-        jb      1b
-
-        /* Patch in the trampoline segment. */
-        shr     $4,%edx
-        lea     sym_esi(__trampoline_seg_start), %edi
-        lea     sym_esi(__trampoline_seg_stop), %ecx
-1:
-        mov     (%edi), %eax
-        mov     %dx, (%edi, %eax)
-        add     $4,%edi
-
-        cmp     %ecx, %edi
-        jb      1b
+        call    reloc_trampoline32
 
         /* Do not parse command line on EFI platform here. */
         cmpb    $0, sym_esi(efi_platform)
xen/arch/x86/boot/reloc-trampoline.c new file +36 -0
new file mode 100644
index 0000000000..c899e99b08
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/compiler.h>
+#include <xen/stdint.h>
+#include <asm/trampoline.h>
+
+extern const int32_t __trampoline_rel_start[], __trampoline_rel_stop[];
+extern const int32_t __trampoline_seg_start[], __trampoline_seg_stop[];
+
+#if defined(__i386__)
+void reloc_trampoline32(void)
+#elif defined (__x86_64__)
+void reloc_trampoline64(void)
+#else
+#error Unknow architecture
+#endif
+{
+    unsigned long phys = trampoline_phys;
+    const int32_t *trampoline_ptr;
+
+    /*
+     * Apply relocations to trampoline.
+     *
+     * This modifies the trampoline in place within Xen, so that it will
+     * operate correctly when copied into place.
+     */
+    for ( trampoline_ptr = __trampoline_rel_start;
+          trampoline_ptr < __trampoline_rel_stop;
+          ++trampoline_ptr )
+        *(uint32_t *)(*trampoline_ptr + (long)trampoline_ptr) += phys;
+
+    for ( trampoline_ptr = __trampoline_seg_start;
+          trampoline_ptr < __trampoline_seg_stop;
+          ++trampoline_ptr )
+        *(uint16_t *)(*trampoline_ptr + (long)trampoline_ptr) = phys >> 4;
+}
xen/arch/x86/efi/efi-boot.h +2 -13
index 94f3443364..1acceec471 100644
@@ -103,27 +103,16 @@ static void __init efi_arch_relocate_image(unsigned long delta)
     }
 }
 
-extern const int32_t __trampoline_rel_start[], __trampoline_rel_stop[];
-extern const int32_t __trampoline_seg_start[], __trampoline_seg_stop[];
+void reloc_trampoline64(void);
 
 static void __init relocate_trampoline(unsigned long phys)
 {
-    const int32_t *trampoline_ptr;
-
     trampoline_phys = phys;
 
     if ( !efi_enabled(EFI_LOADER) )
         return;
 
-    /* Apply relocations to trampoline. */
-    for ( trampoline_ptr = __trampoline_rel_start;
-          trampoline_ptr < __trampoline_rel_stop;
-          ++trampoline_ptr )
-        *(u32 *)(*trampoline_ptr + (long)trampoline_ptr) += phys;
-    for ( trampoline_ptr = __trampoline_seg_start;
-          trampoline_ptr < __trampoline_seg_stop;
-          ++trampoline_ptr )
-        *(u16 *)(*trampoline_ptr + (long)trampoline_ptr) = phys >> 4;
+    reloc_trampoline64();
 }
 
 static void __init place_string(u32 *addr, const char *s)
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v2 4/6] x86/boot: Reuse code to relocate trampoline

Quoted 1 line · Frediano Ziglio

This patch looks fine now (give or take some knock-on effects from patch 3).

However it probably does need a little more in the way of commit message.  You're moving the EFI implementation, building it for both 32 and 64bit, and replacing the assembly version.

~Andrew

#6 lore raw

PATCH v2 5/6 x86/boot: Use trampoline_phys variable directly from C code

In reply to Frediano Ziglio

No more need to pass from assembly code.

---
Changes since v1:
- split the 2 variable changes into 2 commits.

---
 xen/arch/x86/boot/head.S  | 13 ++++---------
 xen/arch/x86/boot/reloc.c |  9 ++++++---
 2 files changed, 10 insertions(+), 12 deletions(-)
xen/arch/x86/boot/head.S +4 -9
index ade2c5c43d..32b658fa2b 100644
@@ -510,22 +510,17 @@ trampoline_setup:
         mov     %esi, sym_esi(xen_phys_start)
         mov     %esi, sym_esi(trampoline_xen_phys_start)
 
-        /* Get bottom-most low-memory stack address. */
-        mov     sym_esi(trampoline_phys), %ecx
-        add     $TRAMPOLINE_SPACE,%ecx
-
+        /* Boot video info to be filled from MB2. */
 #ifdef CONFIG_VIDEO
-        lea     sym_esi(boot_vid_info), %edx
+        lea     sym_esi(boot_vid_info), %ecx
 #else
-        xor     %edx, %edx
+        xor     %ecx, %ecx
 #endif
 
         /* Save Multiboot / PVH info struct (after relocation) for later use. */
-        push    %edx                /* Boot video info to be filled from MB2. */
         mov     %ebx, %edx          /* Multiboot / PVH information address. */
-        /*      reloc(magic/eax, info/edx, trampoline/ecx, video/stk) using fastcall. */
+        /*      reloc(magic/eax, info/edx, video/stk) using fastcall. */
         call    reloc
-        add     $4, %esp
 
 #ifdef CONFIG_PVH_GUEST
         cmpb    $0, sym_esi(pvh_boot)
xen/arch/x86/boot/reloc.c +6 -3
index 94b078d7b1..4cca61adec 100644
@@ -19,6 +19,9 @@
 #include <xen/kconfig.h>
 #include <xen/multiboot.h>
 #include <xen/multiboot2.h>
+#include <xen/page-size.h>
+
+#include <asm/trampoline.h>
 
 #include <public/arch-x86/hvm/start_info.h>
 
@@ -346,10 +349,10 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx
 }
 
 /* SAF-1-safe */
-void *reloc(uint32_t magic, uint32_t in, uint32_t trampoline,
-            uint32_t video_info)
+void *reloc(uint32_t magic, uint32_t in, uint32_t video_info)
 {
-    memctx ctx = { trampoline };
+    /* Get bottom-most low-memory stack address. */
+    memctx ctx = { (uint32_t)((long)trampoline_phys + TRAMPOLINE_SPACE) };
 
     switch ( magic )
     {
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v2 5/6] x86/boot: Use trampoline_phys variable directly from C code

Quoted 29 lines · Frediano Ziglio

No more need to pass from assembly code.

---
Changes since v1:
- split the 2 variable changes into 2 commits.

---
 xen/arch/x86/boot/head.S  | 13 ++++---------
 xen/arch/x86/boot/reloc.c |  9 ++++++---
 2 files changed, 10 insertions(+), 12 deletions(-)
xen/arch/x86/boot/head.S +2 -6
index ade2c5c43d..32b658fa2b 100644
@@ -510,22 +510,17 @@ trampoline_setup:
         mov     %esi, sym_esi(xen_phys_start)
         mov     %esi, sym_esi(trampoline_xen_phys_start)
 
-        /* Get bottom-most low-memory stack address. */
-        mov     sym_esi(trampoline_phys), %ecx
-        add     $TRAMPOLINE_SPACE,%ecx
-
+        /* Boot video info to be filled from MB2. */
 #ifdef CONFIG_VIDEO
-        lea     sym_esi(boot_vid_info), %edx
+        lea     sym_esi(boot_vid_info), %ecx
 #else
-        xor     %edx, %edx

xen/arch/x86/boot/head.S trampoline_setup: · line 528

Quoted 10 lines
xen/arch/x86/boot/head.S +2 -3
+        xor     %ecx, %ecx
 #endif
 
         /* Save Multiboot / PVH info struct (after relocation) for later use. */
-        push    %edx                /* Boot video info to be filled from MB2. */
         mov     %ebx, %edx          /* Multiboot / PVH information address. */
-        /*      reloc(magic/eax, info/edx, trampoline/ecx, video/stk) using fastcall. */
+        /*      reloc(magic/eax, info/edx, video/stk) using fastcall. */
         call    reloc
-        add     $4, %esp

Sorry - I was expecting you split the patches the other way around.

If you drop video first, then trampoline second, there's no churn changing video from being on the stack to being in ecx.

As it stands the "video/stk" needs to become "video/ecx".

Quoted 18 lines
xen/arch/x86/boot/head.S +0 -0
 #ifdef CONFIG_PVH_GUEST
         cmpb    $0, sym_esi(pvh_boot)
xen/arch/x86/boot/reloc.c +3 -0
index 94b078d7b1..4cca61adec 100644
@@ -19,6 +19,9 @@
 #include <xen/kconfig.h>
 #include <xen/multiboot.h>
 #include <xen/multiboot2.h>
+#include <xen/page-size.h>
+
+#include <asm/trampoline.h>
 
 #include <public/arch-x86/hvm/start_info.h>
 

xen/arch/x86/boot/reloc.c static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx · line 355

Quoted 10 lines
xen/arch/x86/boot/reloc.c +3 -3
@@ -346,10 +349,10 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx
 }
 
 /* SAF-1-safe */
-void *reloc(uint32_t magic, uint32_t in, uint32_t trampoline,
-            uint32_t video_info)
+void *reloc(uint32_t magic, uint32_t in, uint32_t video_info)
 {
-    memctx ctx = { trampoline };
+    /* Get bottom-most low-memory stack address. */
+    memctx ctx = { (uint32_t)((long)trampoline_phys + TRAMPOLINE_SPACE) };

You don't need any casts here.  The result can't exceed 1<<20.

Again, I agree this is a correct transform of the code, but the comment you've moved is incorrect AFAICT, and conflicts with

typedef struct memctx {
    /*
     * Simple bump allocator.
     *
     * It starts from the base of the trampoline and allocates downwards. */
    uint32_t ptr;
} memctx;

which is probably my fault, but needs resolving.

What does the trampoline layout actually look like?  Can we see about correcting the comments?

~Andrew

#8 lore raw

Re: [PATCH v2 5/6] x86/boot: Use trampoline_phys variable directly from C code

Quoted 34 lines · Andrew Cooper
Quoted 32 lines · Frediano Ziglio

No more need to pass from assembly code.

---
Changes since v1:
- split the 2 variable changes into 2 commits.

---
 xen/arch/x86/boot/head.S  | 13 ++++---------
 xen/arch/x86/boot/reloc.c |  9 ++++++---
 2 files changed, 10 insertions(+), 12 deletions(-)
xen/arch/x86/boot/head.S +3 -6
index ade2c5c43d..32b658fa2b 100644
@@ -510,22 +510,17 @@ trampoline_setup:
         mov     %esi, sym_esi(xen_phys_start)
         mov     %esi, sym_esi(trampoline_xen_phys_start)
 
-        /* Get bottom-most low-memory stack address. */
-        mov     sym_esi(trampoline_phys), %ecx
-        add     $TRAMPOLINE_SPACE,%ecx
-
+        /* Boot video info to be filled from MB2. */
 #ifdef CONFIG_VIDEO
-        lea     sym_esi(boot_vid_info), %edx
+        lea     sym_esi(boot_vid_info), %ecx
 #else
-        xor     %edx, %edx
+        xor     %ecx, %ecx
 #endif
 
Quoted 10 lines

xen/arch/x86/boot/head.S trampoline_setup: · line 528

Quoted 7 lines
xen/arch/x86/boot/head.S +1 -3
         /* Save Multiboot / PVH info struct (after relocation) for later use. */
-        push    %edx                /* Boot video info to be filled from MB2. */
         mov     %ebx, %edx          /* Multiboot / PVH information address. */
-        /*      reloc(magic/eax, info/edx, trampoline/ecx, video/stk) using fastcall. */
+        /*      reloc(magic/eax, info/edx, video/stk) using fastcall. */
         call    reloc
-        add     $4, %esp

Sorry - I was expecting you split the patches the other way around.

I'll do it again.

Quoted 5 lines

If you drop video first, then trampoline second, there's no churn changing video from being on the stack to being in ecx.

As it stands the "video/stk" needs to become "video/ecx".

I'll take care.

Quoted 20 lines
Quoted 20 lines
xen/arch/x86/boot/head.S +0 -0
 #ifdef CONFIG_PVH_GUEST
         cmpb    $0, sym_esi(pvh_boot)
xen/arch/x86/boot/reloc.c +3 -0
index 94b078d7b1..4cca61adec 100644
@@ -19,6 +19,9 @@
 #include <xen/kconfig.h>
 #include <xen/multiboot.h>
 #include <xen/multiboot2.h>
+#include <xen/page-size.h>
+
+#include <asm/trampoline.h>
 
 #include <public/arch-x86/hvm/start_info.h>
 
@@ -346,10 +349,10 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx
 }
 
Quoted 11 lines

xen/arch/x86/boot/reloc.c static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx · line 355

Quoted 8 lines
xen/arch/x86/boot/reloc.c +3 -3
 /* SAF-1-safe */
-void *reloc(uint32_t magic, uint32_t in, uint32_t trampoline,
-            uint32_t video_info)
+void *reloc(uint32_t magic, uint32_t in, uint32_t video_info)
 {
-    memctx ctx = { trampoline };
+    /* Get bottom-most low-memory stack address. */
+    memctx ctx = { (uint32_t)((long)trampoline_phys + TRAMPOLINE_SPACE) };

You don't need any casts here. The result can't exceed 1<<20.

I'll do

Quoted 13 lines

Again, I agree this is a correct transform of the code, but the comment you've moved is incorrect AFAICT, and conflicts with

typedef struct memctx {
    /*
     * Simple bump allocator.
     *
     * It starts from the base of the trampoline and allocates downwards.
     */
    uint32_t ptr;
} memctx;
Quoted 5 lines

which is probably my fault, but needs resolving.

What does the trampoline layout actually look like? Can we see about correcting the comments?

Can you suggest what I should write?

Quoted 1 line

~Andrew

Frediano

#9 lore raw

PATCH v2 6/6 x86/boot: Use boot_vid_info variable directly from C code

In reply to Frediano Ziglio

No more need to pass from assembly code.

---
 xen/arch/x86/boot/build32.lds.S |  1 +
 xen/arch/x86/boot/head.S        |  9 +--------
 xen/arch/x86/boot/reloc.c       | 12 +++++-------
 3 files changed, 7 insertions(+), 15 deletions(-)
xen/arch/x86/boot/build32.lds.S +1 -0
index 9d09e3adbd..26ef609523 100644
@@ -46,6 +46,7 @@ SECTIONS
         DECLARE_IMPORT(__trampoline_seg_start);
         DECLARE_IMPORT(__trampoline_seg_stop);
         DECLARE_IMPORT(trampoline_phys);
+        DECLARE_IMPORT(boot_vid_info);
         . = . + GAP;
         *(.text)
         *(.text.*)
xen/arch/x86/boot/head.S +1 -8
index 32b658fa2b..dcda91cfda 100644
@@ -510,16 +510,9 @@ trampoline_setup:
         mov     %esi, sym_esi(xen_phys_start)
         mov     %esi, sym_esi(trampoline_xen_phys_start)
 
-        /* Boot video info to be filled from MB2. */
-#ifdef CONFIG_VIDEO
-        lea     sym_esi(boot_vid_info), %ecx
-#else
-        xor     %ecx, %ecx
-#endif
-
         /* Save Multiboot / PVH info struct (after relocation) for later use. */
         mov     %ebx, %edx          /* Multiboot / PVH information address. */
-        /*      reloc(magic/eax, info/edx, video/stk) using fastcall. */
+        /*      reloc(magic/eax, info/edx) using fastcall. */
         call    reloc
 
 #ifdef CONFIG_PVH_GUEST
xen/arch/x86/boot/reloc.c +5 -7
index 4cca61adec..8527fa8d01 100644
@@ -179,7 +179,7 @@ static multiboot_info_t *mbi_reloc(uint32_t mbi_in, memctx *ctx)
     return mbi_out;
 }
 
-static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx *ctx)
+static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, memctx *ctx)
 {
     const multiboot2_fixed_t *mbi_fix = _p(mbi_in);
     const multiboot2_memory_map_t *mmap_src;
@@ -188,7 +188,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx
     memory_map_t *mmap_dst;
     multiboot_info_t *mbi_out;
 #ifdef CONFIG_VIDEO
-    struct boot_video_info *video = NULL;
+    struct boot_video_info *video = &boot_vid_info;
 #endif
     uint32_t ptr;
     unsigned int i, mod_idx = 0;
@@ -293,12 +293,11 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx
 
 #ifdef CONFIG_VIDEO
         case MULTIBOOT2_TAG_TYPE_VBE:
-            if ( video_out )
+            if ( video )
             {
                 const struct vesa_ctrl_info *ci;
                 const struct vesa_mode_info *mi;
 
-                video = _p(video_out);
                 ci = (const void *)get_mb2_data(tag, vbe, vbe_control_info);
                 mi = (const void *)get_mb2_data(tag, vbe, vbe_mode_info);
 
@@ -324,7 +323,6 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx
             if ( (get_mb2_data(tag, framebuffer, framebuffer_type) !=
                   MULTIBOOT2_FRAMEBUFFER_TYPE_RGB) )
             {
-                video_out = 0;
                 video = NULL;
             }
             break;
@@ -349,7 +347,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, memctx
 }
 
 /* SAF-1-safe */
-void *reloc(uint32_t magic, uint32_t in, uint32_t video_info)
+void *reloc(uint32_t magic, uint32_t in)
 {
     /* Get bottom-most low-memory stack address. */
     memctx ctx = { (uint32_t)((long)trampoline_phys + TRAMPOLINE_SPACE) };
@@ -360,7 +358,7 @@ void *reloc(uint32_t magic, uint32_t in, uint32_t video_info)
         return mbi_reloc(in, &ctx);
 
     case MULTIBOOT2_BOOTLOADER_MAGIC:
-        return mbi2_reloc(in, video_info, &ctx);
+        return mbi2_reloc(in, &ctx);
 
     case XEN_HVM_START_MAGIC_VALUE:
         if ( IS_ENABLED(CONFIG_PVH_GUEST) )
Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH v2 6/6] x86/boot: Use boot_vid_info variable directly from C code

Quoted 3 lines · Frediano Ziglio

No more need to pass from assembly code.

This patch is fine now.

As noted in patch 5, there will be less net churn if the order was swapped.  (As it stands, there's at least one correction in this patch as a knock-on effect.)

~Andrew

#11 lore raw
In reply to Frediano Ziglio
Quoted 3 lines · Frediano Ziglio

Frediano Ziglio (6):
.gitignore: Remove not generated files
x86/boot: Rationalise .gitignore

These two are trivial so I've taken them.

~Andrew