Skip to contentSkip to conversation

Reuse 32 bit C code more safely

PATCH Superseded Frediano Ziglio 3 participants 17 messages Oct 5, 2024 – Oct 7, 2024

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover Reuse 32 bit C code more safely

First version as no RFC.

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 1/3, which is the largest patch.
Patch 2/3 reuses code to relocate the trampoline between 32 and 64 bit. Patch 3/3 moves 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 "staging" branch, currently commit 6b20755c24ccd69d2482b45dfb9193940cdc346e.

Frediano Ziglio (3):
x86/boot: create a C bundle for 32 bit boot code and use it x86/boot: Reuse code to relocate trampoline
x86/boot: Use boot_vid_info and trampoline_phys variables directly
from C code

 .gitignore                                    |   5 +-
 xen/arch/x86/boot/Makefile                    |  38 +++-
 .../x86/boot/{build32.lds => build32.lds.S}   |  51 ++++-
 xen/arch/x86/boot/cmdline.c                   |  12 --
 xen/arch/x86/boot/head.S                      |  49 +----
 xen/arch/x86/boot/reloc-trampoline.c          |  30 +++
 xen/arch/x86/boot/reloc.c                     |  33 +--
 xen/arch/x86/efi/efi-boot.h                   |  15 +-
 xen/tools/combine_two_binaries                | 195 ++++++++++++++++++
 9 files changed, 314 insertions(+), 114 deletions(-)
 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 1/3 x86/boot: create a C bundle for 32 bit boot code and use it

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.

---
 .gitignore                                    |   5 +-
 xen/arch/x86/boot/Makefile                    |  36 +++-
 .../x86/boot/{build32.lds => build32.lds.S}   |  45 +++-
 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                | 195 ++++++++++++++++++
 7 files changed, 263 insertions(+), 56 deletions(-)
 rename xen/arch/x86/boot/{build32.lds => build32.lds.S} (63%)
 create mode 100755 xen/tools/combine_two_binaries
.gitignore +2 -3
index d88be80b28..d538c6adb1 100644
@@ -247,11 +247,10 @@ xen/.config
 xen/.config.old
 xen/.xen.elf32
 xen/System.map
-xen/arch/x86/boot/mkelf32
+xen/arch/x86/boot/build32.*.lds
 xen/arch/x86/boot/cmdline.S
+xen/arch/x86/boot/mkelf32
 xen/arch/x86/boot/reloc.S
-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/Makefile +27 -9
index ff0f965876..da87179fef 100644
@@ -1,4 +1,4 @@
-obj-bin-y += head.o
+obj-bin-y += head.o cbundle.o
 
 head-bin-objs := cmdline.o reloc.o
 
@@ -9,7 +9,6 @@ targets   += $(head-bin-objs)
 head-bin-objs := $(addprefix $(obj)/,$(head-bin-objs))
 
 $(obj)/head.o: AFLAGS-y += -Wa$(comma)-I$(obj)
-$(obj)/head.o: $(head-bin-objs:.o=.bin)
 
 CFLAGS_x86_32 := $(subst -m64,-m32 -march=i686,$(XEN_TREEWIDE_CFLAGS))
 $(call cc-options-add,CFLAGS_x86_32,CC,$(EMBEDDED_EXTRA_CFLAGS))
@@ -24,10 +23,29 @@ $(head-bin-objs): XEN_CFLAGS := $(CFLAGS_x86_32) -fpic
 LDFLAGS_DIRECT-$(call ld-option,--warn-rwx-segments) := --no-warn-rwx-segments
 LDFLAGS_DIRECT += $(LDFLAGS_DIRECT-y)
 
-%.bin: %.lnk
-	$(OBJCOPY) -j .text -O binary $< $@
-
-%.lnk: %.o $(src)/build32.lds
-	$(LD) $(subst x86_64,i386,$(LDFLAGS_DIRECT)) -N -T $(filter %.lds,$^) -o $@ $<
-
-clean-files := *.lnk *.bin
+$(obj)/build32.final.lds: AFLAGS-y += -DFINAL
+$(obj)/build32.other.lds $(obj)/build32.final.lds: $(src)/build32.lds.S
+	$(call if_changed_dep,cpp_lds_S)
+
+$(obj)/%.x64.o: $(src)/%.c FORCE
+	$(call if_changed_rule,cc_o_c)
+
+$(obj)/cbundle.o: $(head-bin-objs) $(obj)/build32.other.lds $(obj)/build32.final.lds
+## link all object files together
+	$(LD) $(subst x86_64,i386,$(LDFLAGS_DIRECT)) -r -o $(obj)/cbundle.tmp.o $(head-bin-objs)
+## link twice with 2 different layouts
+	$(LD) $(subst x86_64,i386,$(LDFLAGS_DIRECT)) --orphan-handling=error -N -T $(obj)/build32.other.lds -o [email protected] $(obj)/cbundle.tmp.o
+	$(LD) $(subst x86_64,i386,$(LDFLAGS_DIRECT)) --orphan-handling=error -N -T $(obj)/build32.final.lds -Map $(obj)/cbundle.map -o [email protected] $(obj)/cbundle.tmp.o
+## extract binaries from them
+	$(OBJCOPY) -j .text -O binary [email protected] [email protected]
+	$(OBJCOPY) -j .text -O binary [email protected] [email protected]
+## generate final assembly file combining and checking above binaries
+	$(PYTHON) $(srctree)/tools/combine_two_binaries \
+		--script $(obj)/build32.final.lds \
+		--bin1 [email protected] --bin2 [email protected] \
+		--map $(obj)/cbundle.map --exports cmdline_parse_early,reloc \
+		--section-header '.section .init.text, "ax", @progbits' \
+		--output $(obj)/cbundle.s
+	$(CC) -c $(obj)/cbundle.s -o [email protected]
+	rm -f $(obj)/cbundle.tmp.o [email protected] [email protected] [email protected] [email protected] $(obj)/cbundle.map $(obj)/cbundle.s $@
+	mv [email protected] $@
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +39 -6

64 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 +195 -0

195 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/3] x86/boot: create a C bundle for 32 bit boot code and use it

Quoted 5 lines · Frediano Ziglio
.gitignore +0 -0
index d88be80b28..d538c6adb1 100644

.gitignore xen/.config · line 254

Quoted 10 lines
.gitignore +2 -3
@@ -247,11 +247,10 @@ xen/.config
 xen/.config.old
 xen/.xen.elf32
 xen/System.map
-xen/arch/x86/boot/mkelf32
+xen/arch/x86/boot/build32.*.lds
 xen/arch/x86/boot/cmdline.S
+xen/arch/x86/boot/mkelf32
 xen/arch/x86/boot/reloc.S
-xen/arch/x86/boot/*.bin
-xen/arch/x86/boot/*.lnk

cmdline.S and reloc.S are stale too.  They're from an even older form of this bodge (c/s 1ab7c128d9d1).

However, we're trying to move away from one global .gitignore file. Would you mind deleting all of these entries, and making a new xen/arch/x86/boot/.gitignore with just the ones that you need.  (c/s 0a15b7695bd98 is an example of cleaning up the tools/ocaml side of things).

Feel free to make it a prep patch if you want.  This one is complicated enough.

xen/arch/x86/boot/Makefile line 1

Quoted 7 lines
xen/arch/x86/boot/Makefile +1 -1
index ff0f965876..da87179fef 100644
@@ -1,4 +1,4 @@
-obj-bin-y += head.o
+obj-bin-y += head.o cbundle.o

+obj-bin-y += cbundle.o
 obj-bin-y += head.o

please.  Putting things on a single line is already getting unwieldy by patch 2.

I have to admit that I'm still not convinced of "cbundle" as a name, but I can't think of anything better either.

Quoted 25 lines
xen/arch/x86/boot/Makefile +3 -8
 head-bin-objs := cmdline.o reloc.o
 
@@ -9,7 +9,6 @@ targets   += $(head-bin-objs)
 head-bin-objs := $(addprefix $(obj)/,$(head-bin-objs))
 
 $(obj)/head.o: AFLAGS-y += -Wa$(comma)-I$(obj)
-$(obj)/head.o: $(head-bin-objs:.o=.bin)
 
 CFLAGS_x86_32 := $(subst -m64,-m32 -march=i686,$(XEN_TREEWIDE_CFLAGS))
 $(call cc-options-add,CFLAGS_x86_32,CC,$(EMBEDDED_EXTRA_CFLAGS))
@@ -24,10 +23,29 @@ $(head-bin-objs): XEN_CFLAGS := $(CFLAGS_x86_32) -fpic
 LDFLAGS_DIRECT-$(call ld-option,--warn-rwx-segments) := --no-warn-rwx-segments
 LDFLAGS_DIRECT += $(LDFLAGS_DIRECT-y)
 
-%.bin: %.lnk
-	$(OBJCOPY) -j .text -O binary $< $@
-
-%.lnk: %.o $(src)/build32.lds
-	$(LD) $(subst x86_64,i386,$(LDFLAGS_DIRECT)) -N -T $(filter %.lds,$^) -o $@ $<
-
-clean-files := *.lnk *.bin
+$(obj)/build32.final.lds: AFLAGS-y += -DFINAL
+$(obj)/build32.other.lds $(obj)/build32.final.lds: $(src)/build32.lds.S
+	$(call if_changed_dep,cpp_lds_S)

xen/arch/x86/boot/Makefile $(head-bin-objs): XEN_CFLAGS := $(CFLAGS_x86_32) -fpic · line 38

Quoted 10 lines
xen/arch/x86/boot/Makefile +10 -0
+
+$(obj)/%.x64.o: $(src)/%.c FORCE
+	$(call if_changed_rule,cc_o_c)
+
+$(obj)/cbundle.o: $(head-bin-objs) $(obj)/build32.other.lds $(obj)/build32.final.lds
+## link all object files together
+	$(LD) $(subst x86_64,i386,$(LDFLAGS_DIRECT)) -r -o $(obj)/cbundle.tmp.o $(head-bin-objs)
+## link twice with 2 different layouts
+	$(LD) $(subst x86_64,i386,$(LDFLAGS_DIRECT)) --orphan-handling=error -N -T $(obj)/build32.other.lds -o [email protected] $(obj)/cbundle.tmp.o
+	$(LD) $(subst x86_64,i386,$(LDFLAGS_DIRECT)) --orphan-handling=error -N -T $(obj)/build32.final.lds -Map $(obj)/cbundle.map -o [email protected] $(obj)/cbundle.tmp.o

Looking at this, I think we want an $(LD32) variable.  Also, --orphan-handling isn't supported by all supported linkers, so you'll have to make it conditional.  See how we handle =warn in the general case.

As a tangent, I know we have this form in the main Xen link, but I have the same complaint there.  This is a serial operation that could be less serial.

AFAICT, you link everything together into cbundle.tmp, then do two (parallelisable) relink+obcopy, then run the python script.  Would it be possible to split it into 4(?) rules.

Quoted 21 lines
xen/arch/x86/boot/Makefile +13 -0
+## extract binaries from them
+	$(OBJCOPY) -j .text -O binary [email protected] [email protected]
+	$(OBJCOPY) -j .text -O binary [email protected] [email protected]
+## generate final assembly file combining and checking above binaries
+	$(PYTHON) $(srctree)/tools/combine_two_binaries \
+		--script $(obj)/build32.final.lds \
+		--bin1 [email protected] --bin2 [email protected] \
+		--map $(obj)/cbundle.map --exports cmdline_parse_early,reloc \
+		--section-header '.section .init.text, "ax", @progbits' \
+		--output $(obj)/cbundle.s
+	$(CC) -c $(obj)/cbundle.s -o [email protected]
+	rm -f $(obj)/cbundle.tmp.o [email protected] [email protected] [email protected] [email protected] $(obj)/cbundle.map $(obj)/cbundle.s $@
+	mv [email protected] $@
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +0 -0
similarity index 63%
rename from xen/arch/x86/boot/build32.lds
rename to xen/arch/x86/boot/build32.lds.S
index 56edaa727b..fe422e3d25 100644

xen/arch/x86/boot/build32.lds.S line 18

Quoted 5 lines
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +1 -1
@@ -15,22 +15,54 @@
  * with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-ENTRY(_start)
+#undef ENTRY

The need for this undef should have gone away with c/s b102c9f1ce29.

Quoted 20 lines
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +15 -2
+
+#ifdef FINAL
+# define GAP 0
+# define MULT 0
+# define TEXT_START
+#else
+# define GAP 0x010200
+# define MULT 1
+# define TEXT_START 0x408020
+#endif
+# define DECLARE_IMPORT(name) name = . + (__LINE__ * MULT)
+
+ENTRY(dummy_start)
 
 SECTIONS
 {
-  /* Merge code and data into one section. */
-  .text : {
+  /* Merge code and read-only data into one section. */
+  .text TEXT_START : {

xen/arch/x86/boot/build32.lds.S line 46

Quoted 10 lines
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +10 -0
+        /* Silence linker warning, we are not going to use it */
+        dummy_start = .;
+
+        /* Declare below any symbol name needed.
+         * Each symbol should be on its own line.
+         * It looks like a tedious work but we make sure the things we use.
+         * Potentially they should be all variables. */
+        DECLARE_IMPORT(__base_relocs_start);
+        DECLARE_IMPORT(__base_relocs_end);
+        . = . + GAP;

I have to admit that I've got lost here trying to follow what's going on.

The linker script is emitting symbols based on line number that are either adjacent or all the same (depending on MULT), and these names are also parsed by the python script ?

I also note that running this patch:

$ make -s -j8 build
 __  __            _  _    ____   ___                     _        _    
_      
 \ \/ /___ _ __   | || |  |___ \ / _ \    _   _ _ __  ___| |_ __ _| |__
| | ___
  \  // _ \ '_ \  | || |_   __) | | | |__| | | | '_ \/ __| __/ _` | '_
\| |/ _ \
  /  \  __/ | | | |__   _| / __/| |_| |__| |_| | | | \__ \ || (_| | |_)
| |  __/
 /_/\_\___|_| |_|    |_|(_)_____|\___/    \__,_|_|
|_|___/\__\__,_|_.__/|_|\___|
                                                                              
 
Position 0x70c 50 trampoline_phys
Position 0x87e 51 boot_vid_info
Position 0xade 51 boot_vid_info
Position 0xb92 51 boot_vid_info
Position 0xbae 50 trampoline_phys
Position 0xbb4 46 __trampoline_rel_start
Position 0xbba 47 __trampoline_rel_stop
Position 0xbc6 47 __trampoline_rel_stop
Position 0xbe2 48 __trampoline_seg_start
Position 0xbe8 49 __trampoline_seg_stop
Position 0xbf8 49 __trampoline_seg_stop
$

which looks like debugging leaking out to stderr.

~Andrew

#3 lore raw

Re: [PATCH 1/3] x86/boot: create a C bundle for 32 bit boot code and use it

Quoted 11 lines · Andrew Cooper

xen/arch/x86/boot/Makefile line 1

Quoted 7 lines · Frediano Ziglio
xen/arch/x86/boot/Makefile +1 -1
index ff0f965876..da87179fef 100644
@@ -1,4 +1,4 @@
-obj-bin-y += head.o
+obj-bin-y += head.o cbundle.o

+obj-bin-y += cbundle.o
 obj-bin-y += head.o

Quoted 5 lines

please.  Putting things on a single line is already getting unwieldy by patch 2.

I have to admit that I'm still not convinced of "cbundle" as a name, but I can't think of anything better either.

Based on the other thread, and in combination with existing Kbuild nomenclature, this probably wants to be built_in_32.o

~Andrew

#4 lore raw

PATCH 2/3 x86/boot: Reuse code to relocate trampoline

In reply to Frediano Ziglio
---
 xen/arch/x86/boot/Makefile           |  6 +++---
 xen/arch/x86/boot/build32.lds.S      |  5 +++++
 xen/arch/x86/boot/head.S             | 23 +--------------------
 xen/arch/x86/boot/reloc-trampoline.c | 30 ++++++++++++++++++++++++++++
 xen/arch/x86/efi/efi-boot.h          | 15 ++------------
 5 files changed, 41 insertions(+), 38 deletions(-)
 create mode 100644 xen/arch/x86/boot/reloc-trampoline.c
xen/arch/x86/boot/Makefile +3 -3
index da87179fef..c16c4a8595 100644
@@ -1,6 +1,6 @@
-obj-bin-y += head.o cbundle.o
+obj-bin-y += head.o cbundle.o reloc-trampoline.x64.o
 
-head-bin-objs := cmdline.o reloc.o
+head-bin-objs := cmdline.o reloc.o reloc-trampoline.o
 
 nocov-y   += $(head-bin-objs)
 noubsan-y += $(head-bin-objs)
@@ -43,7 +43,7 @@ $(obj)/cbundle.o: $(head-bin-objs) $(obj)/build32.other.lds $(obj)/build32.final
 	$(PYTHON) $(srctree)/tools/combine_two_binaries \
 		--script $(obj)/build32.final.lds \
 		--bin1 [email protected] --bin2 [email protected] \
-		--map $(obj)/cbundle.map --exports cmdline_parse_early,reloc \
+		--map $(obj)/cbundle.map --exports cmdline_parse_early,reloc,reloc_trampoline32 \
 		--section-header '.section .init.text, "ax", @progbits' \
 		--output $(obj)/cbundle.s
 	$(CC) -c $(obj)/cbundle.s -o [email protected]
xen/arch/x86/boot/build32.lds.S +5 -0
index fe422e3d25..2d10a75fb1 100644
@@ -43,6 +43,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 +30 -0
new file mode 100644
index 0000000000..9509dfa28a
@@ -0,0 +1,30 @@
+/* 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. */
+    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 2/3] x86/boot: Reuse code to relocate trampoline

Quoted 15 lines · Frediano Ziglio
---
 xen/arch/x86/boot/Makefile           |  6 +++---
 xen/arch/x86/boot/build32.lds.S      |  5 +++++
 xen/arch/x86/boot/head.S             | 23 +--------------------
 xen/arch/x86/boot/reloc-trampoline.c | 30 ++++++++++++++++++++++++++++
 xen/arch/x86/efi/efi-boot.h          | 15 ++------------
 5 files changed, 41 insertions(+), 38 deletions(-)
 create mode 100644 xen/arch/x86/boot/reloc-trampoline.c
xen/arch/x86/boot/Makefile +0 -0
index da87179fef..c16c4a8595 100644

xen/arch/x86/boot/Makefile line 1

Quoted 2 lines
xen/arch/x86/boot/Makefile +1 -1
@@ -1,6 +1,6 @@
-obj-bin-y += head.o cbundle.o
+obj-bin-y += head.o cbundle.o reloc-trampoline.x64.o

Ah.  I think the $(obj)/%.x64.o rule you had in the previous patch wants introducing here.

That said, x64 is the one name for 64bit that we reliably don't use. Also...

xen/arch/x86/boot/Makefile line 3

Quoted 2 lines
xen/arch/x86/boot/Makefile +1 -1
-head-bin-objs := cmdline.o reloc.o
+head-bin-objs := cmdline.o reloc.o reloc-trampoline.o

... head-bin-objs isn't really correct now seeing as they're not binaries in head.S.  Also ...

xen/arch/x86/boot/Makefile line 6

Quoted 2 lines
xen/arch/x86/boot/Makefile +0 -0
 nocov-y   += $(head-bin-objs)
 noubsan-y += $(head-bin-objs)

The no$(foo)'s needs extending to the 64bit objects too.  They're also used early enough to explode.

In Xen, 64bit objects are the norm, and it's 32bit ones which are the exception, so how about we special case *.i386.o instead.  Then

obj32 := cmdline.i386.o
obj32 += reloc.i386.o
obj32 += reloc-trampoline.i386.o

and apply no$(foo) over everything.

Quoted 76 lines
xen/arch/x86/boot/Makefile +1 -1
@@ -43,7 +43,7 @@ $(obj)/cbundle.o: $(head-bin-objs) $(obj)/build32.other.lds $(obj)/build32.final
 	$(PYTHON) $(srctree)/tools/combine_two_binaries \
 		--script $(obj)/build32.final.lds \
 		--bin1 [email protected] --bin2 [email protected] \
-		--map $(obj)/cbundle.map --exports cmdline_parse_early,reloc \
+		--map $(obj)/cbundle.map --exports cmdline_parse_early,reloc,reloc_trampoline32 \
 		--section-header '.section .init.text, "ax", @progbits' \
 		--output $(obj)/cbundle.s
 	$(CC) -c $(obj)/cbundle.s -o [email protected]
xen/arch/x86/boot/build32.lds.S +5 -0
index fe422e3d25..2d10a75fb1 100644
@@ -43,6 +43,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 +11 -0
new file mode 100644
index 0000000000..9509dfa28a
@@ -0,0 +1,30 @@
+/* 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)

xen/arch/x86/boot/reloc-trampoline.c line 21

Quoted 10 lines
xen/arch/x86/boot/reloc-trampoline.c new file +10 -0
+#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. */

Please take the opportunity to expand on this.

/*
 * Apply relocations to trampoline.
 *
 * This modifies the trampoline in place within Xen, so that it will
 * operate correctly when copied into place.
 */

This property is entirely non-obvious, and what is is hurting us trying to make R^X work.

But - it also makes it clean how to fix the problem (later, after you've unified the relocation paths with this patch).

xen/arch/x86/boot/reloc-trampoline.c line 25

Quoted 4 lines
xen/arch/x86/boot/reloc-trampoline.c new file +4 -0
+    for ( trampoline_ptr = __trampoline_rel_start;
+          trampoline_ptr < __trampoline_rel_stop;
+          ++trampoline_ptr )
+        *(uint32_t *)(*trampoline_ptr + (long)trampoline_ptr) += phys;

Newline here please.

xen/arch/x86/boot/reloc-trampoline.c line 30

Quoted 5 lines
xen/arch/x86/boot/reloc-trampoline.c new file +5 -0
+    for ( trampoline_ptr = __trampoline_seg_start;
+          trampoline_ptr < __trampoline_seg_stop;
+          ++trampoline_ptr )
+        *(uint16_t *)(*trampoline_ptr + (long)trampoline_ptr) = phys >> 4;
+}

I was looking at the code generation.  32bit looks fine, but 64bit is outragous.

It turns out I have ubsan enabled in this build, and the code generation is much better with nocov-y extended to cover reloc-trampoline.x64.o

~Andrew

#6 lore raw

Re: [PATCH 2/3] x86/boot: Reuse code to relocate trampoline

Quoted 25 lines · Andrew Cooper

xen/arch/x86/boot/Makefile line 1

Quoted 5 lines · Frediano Ziglio
xen/arch/x86/boot/Makefile +1 -1
@@ -1,6 +1,6 @@
-obj-bin-y += head.o cbundle.o
+obj-bin-y += head.o cbundle.o reloc-trampoline.x64.o

Ah.  I think the $(obj)/%.x64.o rule you had in the previous patch wants introducing here.

That said, x64 is the one name for 64bit that we reliably don't use. Also...

xen/arch/x86/boot/Makefile line 3

Quoted 2 lines
xen/arch/x86/boot/Makefile +1 -1
-head-bin-objs := cmdline.o reloc.o
+head-bin-objs := cmdline.o reloc.o reloc-trampoline.o

... head-bin-objs isn't really correct now seeing as they're not binaries in head.S.  Also ...

xen/arch/x86/boot/Makefile line 6

Quoted 2 lines
xen/arch/x86/boot/Makefile +0 -0
 nocov-y   += $(head-bin-objs)
 noubsan-y += $(head-bin-objs)

The no$(foo)'s needs extending to the 64bit objects too.  They're also used early enough to explode.

Quoted 6 lines

In Xen, 64bit objects are the norm, and it's 32bit ones which are the exception, so how about we special case *.i386.o instead.  Then

obj32 := cmdline.i386.o
obj32 += reloc.i386.o
obj32 += reloc-trampoline.i386.o

I'd like to advocate for ix86 or i686. i386 gives a wrong impression imo.

Jan

#7 lore raw

Re: [PATCH 2/3] x86/boot: Reuse code to relocate trampoline

Quoted 30 lines · Jan Beulich
Quoted 28 lines · Andrew Cooper

xen/arch/x86/boot/Makefile line 1

Quoted 5 lines · Frediano Ziglio
xen/arch/x86/boot/Makefile +1 -1
@@ -1,6 +1,6 @@
-obj-bin-y += head.o cbundle.o
+obj-bin-y += head.o cbundle.o reloc-trampoline.x64.o

Ah. I think the $(obj)/%.x64.o rule you had in the previous patch wants introducing here.

That said, x64 is the one name for 64bit that we reliably don't use. Also...

xen/arch/x86/boot/Makefile line 3

Quoted 2 lines
xen/arch/x86/boot/Makefile +1 -1
-head-bin-objs := cmdline.o reloc.o
+head-bin-objs := cmdline.o reloc.o reloc-trampoline.o

... head-bin-objs isn't really correct now seeing as they're not binaries in head.S. Also ...

xen/arch/x86/boot/Makefile line 6

Quoted 2 lines
xen/arch/x86/boot/Makefile +0 -0
 nocov-y   += $(head-bin-objs)
 noubsan-y += $(head-bin-objs)

The no$(foo)'s needs extending to the 64bit objects too. They're also used early enough to explode.

In Xen, 64bit objects are the norm, and it's 32bit ones which are the exception, so how about we special case *.i386.o instead. Then

Quoted 7 lines
Quoted 3 lines

obj32 := cmdline.i386.o
obj32 += reloc.i386.o
obj32 += reloc-trampoline.i386.o

I'd like to advocate for ix86 or i686. i386 gives a wrong impression imo.

Jan

Why not simply x86 ? We already use it.

Frediano

#8 lore raw

Re: [PATCH 2/3] x86/boot: Reuse code to relocate trampoline

Quoted 36 lines · Frediano Ziglio
Quoted 34 lines · Jan Beulich
Quoted 31 lines · Andrew Cooper

xen/arch/x86/boot/Makefile line 1

Quoted 5 lines · Frediano Ziglio
xen/arch/x86/boot/Makefile +1 -1
@@ -1,6 +1,6 @@
-obj-bin-y += head.o cbundle.o
+obj-bin-y += head.o cbundle.o reloc-trampoline.x64.o

Ah. I think the $(obj)/%.x64.o rule you had in the previous patch wants introducing here.

That said, x64 is the one name for 64bit that we reliably don't use. Also...

xen/arch/x86/boot/Makefile line 3

Quoted 2 lines
xen/arch/x86/boot/Makefile +1 -1
-head-bin-objs := cmdline.o reloc.o
+head-bin-objs := cmdline.o reloc.o reloc-trampoline.o

... head-bin-objs isn't really correct now seeing as they're not binaries in head.S. Also ...

xen/arch/x86/boot/Makefile line 6

Quoted 2 lines
xen/arch/x86/boot/Makefile +0 -0
 nocov-y   += $(head-bin-objs)
 noubsan-y += $(head-bin-objs)

The no$(foo)'s needs extending to the 64bit objects too. They're also used early enough to explode.

In Xen, 64bit objects are the norm, and it's 32bit ones which are the exception, so how about we special case *.i386.o instead. Then

obj32 := cmdline.i386.o
obj32 += reloc.i386.o
obj32 += reloc-trampoline.i386.o

Quoted 6 lines
Quoted 3 lines

I'd like to advocate for ix86 or i686. i386 gives a wrong impression imo.

Jan

Why not simply x86 ? We already use it.

Looking at current files, we also use (to distinguish more clearly 32 and 64 bit) x86_32.

Frediano

#9 lore raw

Re: [PATCH 2/3] x86/boot: Reuse code to relocate trampoline

Quoted 38 lines · Frediano Ziglio
Quoted 36 lines · Frediano Ziglio
Quoted 34 lines · Jan Beulich
Quoted 31 lines · Andrew Cooper

xen/arch/x86/boot/Makefile line 1

Quoted 5 lines · Frediano Ziglio
xen/arch/x86/boot/Makefile +1 -1
@@ -1,6 +1,6 @@
-obj-bin-y += head.o cbundle.o
+obj-bin-y += head.o cbundle.o reloc-trampoline.x64.o

Ah. I think the $(obj)/%.x64.o rule you had in the previous patch wants introducing here.

That said, x64 is the one name for 64bit that we reliably don't use. Also...

xen/arch/x86/boot/Makefile line 3

Quoted 2 lines
xen/arch/x86/boot/Makefile +1 -1
-head-bin-objs := cmdline.o reloc.o
+head-bin-objs := cmdline.o reloc.o reloc-trampoline.o

... head-bin-objs isn't really correct now seeing as they're not binaries in head.S. Also ...

xen/arch/x86/boot/Makefile line 6

Quoted 2 lines
xen/arch/x86/boot/Makefile +0 -0
 nocov-y   += $(head-bin-objs)
 noubsan-y += $(head-bin-objs)

The no$(foo)'s needs extending to the 64bit objects too. They're also used early enough to explode.

In Xen, 64bit objects are the norm, and it's 32bit ones which are the exception, so how about we special case *.i386.o instead. Then

obj32 := cmdline.i386.o
obj32 += reloc.i386.o
obj32 += reloc-trampoline.i386.o

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I'd like to advocate for ix86 or i686. i386 gives a wrong impression imo.

Why not simply x86 ? We already use it.

Looking at current files, we also use (to distinguish more clearly 32 and 64 bit) x86_32.

Either would be fine with me; as to x86 I took it that Andrew wanted to express the 32-bit-ness, which x86 alone doesn't unambiguously do.

Jan

#10 lore raw

Re: [PATCH 2/3] x86/boot: Reuse code to relocate trampoline

Quoted 33 lines · Jan Beulich
Quoted 32 lines · Frediano Ziglio
Quoted 30 lines · Frediano Ziglio
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Andrew Cooper

xen/arch/x86/boot/Makefile line 1

Quoted 5 lines · Frediano Ziglio
xen/arch/x86/boot/Makefile +1 -1
@@ -1,6 +1,6 @@
-obj-bin-y += head.o cbundle.o
+obj-bin-y += head.o cbundle.o reloc-trampoline.x64.o

Ah. I think the $(obj)/%.x64.o rule you had in the previous patch wants introducing here.

That said, x64 is the one name for 64bit that we reliably don't use. Also...

xen/arch/x86/boot/Makefile line 3

Quoted 2 lines
xen/arch/x86/boot/Makefile +1 -1
-head-bin-objs := cmdline.o reloc.o
+head-bin-objs := cmdline.o reloc.o reloc-trampoline.o

... head-bin-objs isn't really correct now seeing as they're not binaries in head.S. Also ...

xen/arch/x86/boot/Makefile line 6

Quoted 2 lines
xen/arch/x86/boot/Makefile +0 -0
 nocov-y   += $(head-bin-objs)
 noubsan-y += $(head-bin-objs)

The no$(foo)'s needs extending to the 64bit objects too. They're also used early enough to explode.

In Xen, 64bit objects are the norm, and it's 32bit ones which are the exception, so how about we special case *.i386.o instead. Then

obj32 := cmdline.i386.o
obj32 += reloc.i386.o
obj32 += reloc-trampoline.i386.o

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

I'd like to advocate for ix86 or i686. i386 gives a wrong impression imo.

Why not simply x86 ? We already use it.

Looking at current files, we also use (to distinguish more clearly 32 and 64 bit) x86_32.

Either would be fine with me; as to x86 I took it that Andrew wanted to express the 32-bit-ness, which x86 alone doesn't unambiguously do.

On further thought, why not just foo.32.o ?

That should be clear enough.

~Andrew

#11 lore raw

Re: [PATCH 2/3] x86/boot: Reuse code to relocate trampoline

Quoted 40 lines · Andrew Cooper
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Frediano Ziglio
Quoted 24 lines · Frediano Ziglio
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Andrew Cooper

xen/arch/x86/boot/Makefile line 1

Quoted 5 lines · Frediano Ziglio
xen/arch/x86/boot/Makefile +1 -1
@@ -1,6 +1,6 @@
-obj-bin-y += head.o cbundle.o
+obj-bin-y += head.o cbundle.o reloc-trampoline.x64.o

Ah. I think the $(obj)/%.x64.o rule you had in the previous patch wants introducing here.

That said, x64 is the one name for 64bit that we reliably don't use. Also...

xen/arch/x86/boot/Makefile line 2

Quoted 2 lines
xen/arch/x86/boot/Makefile +1 -1
-head-bin-objs := cmdline.o reloc.o
+head-bin-objs := cmdline.o reloc.o reloc-trampoline.o

... head-bin-objs isn't really correct now seeing as they're not binaries in head.S. Also ...

xen/arch/x86/boot/Makefile line 4

Quoted 2 lines
xen/arch/x86/boot/Makefile +0 -0
 nocov-y   += $(head-bin-objs)
 noubsan-y += $(head-bin-objs)

The no$(foo)'s needs extending to the 64bit objects too. They're also used early enough to explode.

Quoted 9 lines
Quoted 7 lines
Quoted 6 lines

In Xen, 64bit objects are the norm, and it's 32bit ones which are the exception, so how about we special case *.i386.o instead. Then

obj32 := cmdline.i386.o
obj32 += reloc.i386.o
obj32 += reloc-trampoline.i386.o

I'd like to advocate for ix86 or i686. i386 gives a wrong impression imo.

Why not simply x86 ? We already use it.

Looking at current files, we also use (to distinguish more clearly 32 and 64 bit) x86_32.

Quoted 8 lines
Quoted 2 lines

Either would be fine with me; as to x86 I took it that Andrew wanted to express the 32-bit-ness, which x86 alone doesn't unambiguously do.

On further thought, why not just foo.32.o ?

That should be clear enough.

~Andrew

At this point, it starts to be more of a personal preference. I slightly prefer x86_32 looking at file names and Makefile's macros. Pick one.

Frediano

#12 lore raw

Re: [PATCH 2/3] x86/boot: Reuse code to relocate trampoline

Quoted 44 lines · Frediano Ziglio
Quoted 43 lines · Andrew Cooper
Quoted 40 lines · Jan Beulich
Quoted 29 lines · Frediano Ziglio
Quoted 27 lines · Frediano Ziglio
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Andrew Cooper

xen/arch/x86/boot/Makefile line 1

Quoted 5 lines · Frediano Ziglio
xen/arch/x86/boot/Makefile +1 -1
@@ -1,6 +1,6 @@
-obj-bin-y += head.o cbundle.o
+obj-bin-y += head.o cbundle.o reloc-trampoline.x64.o

Ah. I think the $(obj)/%.x64.o rule you had in the previous patch wants introducing here.

That said, x64 is the one name for 64bit that we reliably don't use. Also...

xen/arch/x86/boot/Makefile line 2

Quoted 2 lines
xen/arch/x86/boot/Makefile +1 -1
-head-bin-objs := cmdline.o reloc.o
+head-bin-objs := cmdline.o reloc.o reloc-trampoline.o

... head-bin-objs isn't really correct now seeing as they're not binaries in head.S. Also ...

xen/arch/x86/boot/Makefile line 4

Quoted 2 lines
xen/arch/x86/boot/Makefile +0 -0
 nocov-y   += $(head-bin-objs)
 noubsan-y += $(head-bin-objs)

The no$(foo)'s needs extending to the 64bit objects too. They're also used early enough to explode.

In Xen, 64bit objects are the norm, and it's 32bit ones which are the exception, so how about we special case *.i386.o instead. Then

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

obj32 := cmdline.i386.o
obj32 += reloc.i386.o
obj32 += reloc-trampoline.i386.o

I'd like to advocate for ix86 or i686. i386 gives a wrong impression imo.

Why not simply x86 ? We already use it.

Looking at current files, we also use (to distinguish more clearly 32 and 64 bit) x86_32.

Either would be fine with me; as to x86 I took it that Andrew wanted to express the 32-bit-ness, which x86 alone doesn't unambiguously do.

Quoted 7 lines
Quoted 3 lines

On further thought, why not just foo.32.o ?

That should be clear enough.

At this point, it starts to be more of a personal preference. I slightly prefer x86_32 looking at file names and Makefile's macros. Pick one.

I like Andrew's most recent suggestion, fwiw.

Jan

#13 lore raw

PATCH 3/3 x86/boot: Use boot_vid_info and trampoline_phys variables 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        | 14 +-------------
 xen/arch/x86/boot/reloc.c       | 19 ++++++++++---------
 3 files changed, 12 insertions(+), 22 deletions(-)
xen/arch/x86/boot/build32.lds.S +1 -0
index 2d10a75fb1..6598a24e04 100644
@@ -48,6 +48,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 -13
index ade2c5c43d..dcda91cfda 100644
@@ -510,22 +510,10 @@ 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
-
-#ifdef CONFIG_VIDEO
-        lea     sym_esi(boot_vid_info), %edx
-#else
-        xor     %edx, %edx
-#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) using fastcall. */
         call    reloc
-        add     $4, %esp
 
 #ifdef CONFIG_PVH_GUEST
         cmpb    $0, sym_esi(pvh_boot)
xen/arch/x86/boot/reloc.c +10 -9
index 94b078d7b1..8527fa8d01 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>
 
@@ -176,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;
@@ -185,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;
@@ -290,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);
 
@@ -321,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;
@@ -346,10 +347,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)
 {
-    memctx ctx = { trampoline };
+    /* Get bottom-most low-memory stack address. */
+    memctx ctx = { (uint32_t)((long)trampoline_phys + TRAMPOLINE_SPACE) };
 
     switch ( magic )
     {
@@ -357,7 +358,7 @@ void *reloc(uint32_t magic, uint32_t in, uint32_t trampoline,
         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
#14 lore raw

Re: [PATCH 3/3] x86/boot: Use boot_vid_info and trampoline_phys variables directly from C code

Quoted 16 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +0 -8
index ade2c5c43d..dcda91cfda 100644
@@ -510,22 +510,10 @@ 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
-
-#ifdef CONFIG_VIDEO
-        lea     sym_esi(boot_vid_info), %edx
-#else
-        xor     %edx, %edx

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

Quoted 10 lines
xen/arch/x86/boot/head.S +1 -5
-#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) using fastcall. */
         call    reloc
-        add     $4, %esp
 

Please split this patch in two.  Just for testing sanity sake if nothing else.

Now, while I think the patch is a correct transform of the code, ...

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

Quoted 12 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 +1 -1
index 94b078d7b1..8527fa8d01 100644
@@ -185,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;

... doesn't this demonstrate that we're again writing into the trampoline in-Xen, prior to it placing it in low memory?

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

Quoted 11 lines
xen/arch/x86/boot/reloc.c +3 -3
@@ -346,10 +347,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)
 {
-    memctx ctx = { trampoline };
+    /* Get bottom-most low-memory stack address. */
+    memctx ctx = { (uint32_t)((long)trampoline_phys + TRAMPOLINE_SPACE) };

Again, while this is a correct transformation (best as I can tell), wtf?  Doesn't this mean we're bump-allocating downwards into our own stack?

~Andrew

#15 lore raw

Re: [PATCH 3/3] x86/boot: Use boot_vid_info and trampoline_phys variables directly from C code

Quoted 23 lines · Andrew Cooper
Quoted 21 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +0 -11
index ade2c5c43d..dcda91cfda 100644
@@ -510,22 +510,10 @@ 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
-
-#ifdef CONFIG_VIDEO
-        lea     sym_esi(boot_vid_info), %edx
-#else
-        xor     %edx, %edx
-#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. */
Quoted 9 lines

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

Quoted 5 lines
xen/arch/x86/boot/head.S +1 -2
-        /*      reloc(magic/eax, info/edx, trampoline/ecx, video/stk) using fastcall. */
+        /*      reloc(magic/eax, info/edx) using fastcall. */
         call    reloc
-        add     $4, %esp
 

Please split this patch in two. Just for testing sanity sake if nothing else.

Sorry, it's not clear how it should be split. What are the 2 parts ?

Quoted 18 lines

Now, while I think the patch is a correct transform of the code, ...

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

Quoted 12 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 +1 -1
index 94b078d7b1..8527fa8d01 100644
@@ -185,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;

... doesn't this demonstrate that we're again writing into the trampoline in-Xen, prior to it placing it in low memory?

Yes, C is more readable to human beings.
There's nothing to demonstrate as far as I'm concerned. I pointed out different times the assumption you can write into the trampoline to set it up is spread in multiple places. This change just makes it more clear just using a more readable language.

Quoted 15 lines

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

Quoted 11 lines
xen/arch/x86/boot/reloc.c +3 -3
@@ -346,10 +347,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)
 {
-    memctx ctx = { trampoline };
+    /* Get bottom-most low-memory stack address. */
+    memctx ctx = { (uint32_t)((long)trampoline_phys + TRAMPOLINE_SPACE) };

Again, while this is a correct transformation (best as I can tell), wtf? Doesn't this mean we're bump-allocating downwards into our own stack?

Quoted 1 line

~Andrew

Yes, that's how it works, no regressions here.

Frediano

#16 lore raw

Re: [PATCH 3/3] x86/boot: Use boot_vid_info and trampoline_phys variables directly from C code

Quoted 35 lines · Frediano Ziglio
Quoted 23 lines · Andrew Cooper
Quoted 21 lines · Frediano Ziglio
xen/arch/x86/boot/head.S +0 -11
index ade2c5c43d..dcda91cfda 100644
@@ -510,22 +510,10 @@ 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
-
-#ifdef CONFIG_VIDEO
-        lea     sym_esi(boot_vid_info), %edx
-#else
-        xor     %edx, %edx
-#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. */
Quoted 9 lines

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

Quoted 5 lines
xen/arch/x86/boot/head.S +1 -2
-        /*      reloc(magic/eax, info/edx, trampoline/ecx, video/stk) using fastcall. */
+        /*      reloc(magic/eax, info/edx) using fastcall. */
         call    reloc
-        add     $4, %esp
 

Please split this patch in two. Just for testing sanity sake if nothing else.

Quoted 2 lines

Sorry, it's not clear how it should be split. What are the 2 parts ?

Never mind, understood, one variable per commit.

Frediano