Reuse 32 bit C code more safely
cover Reuse 32 bit C code more safely
This RFC 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 2/5, which is the largest patch.
Patch 1/5 is to prepare code and avoid data.
Patch 3/5 is an example of code reuse between 32 and 64 bit. Patch 4/5 is also another example of code reuse but is more hacky and dirty due to not being possible include use some headers.
Code boot successfully using:
- BIOS boot;
- EFI boot with Grub2 and ELF file;
- direct EFI boot without Grub.
Suggestions/opinions are welcome.
Code is currently based on "staging" branch, currently commit 6471badeeec92db1cb8155066551f7509cd82efd.
Frediano Ziglio (5):
Avoid usage of global in reloc.c
x86/boot: create a C bundle for 32 bit boot code and use it Reuse code to relocate trampoline
Remove duplication preparing pages
setup mapping for trampoline in setup_pagesXX
.gitignore | 3 +-
xen/arch/x86/boot/Makefile | 30 ++-
.../x86/boot/{build32.lds => build32.lds.S} | 60 +++++-
xen/arch/x86/boot/cmdline.c | 7 -
xen/arch/x86/boot/head.S | 89 +--------
xen/arch/x86/boot/reloc-trampoline.c | 28 +++
xen/arch/x86/boot/reloc-trampoline64.c | 1 +
xen/arch/x86/boot/reloc.c | 69 ++++---
xen/arch/x86/boot/setup-pages.c | 116 ++++++++++++
xen/arch/x86/boot/setup-pages64.c | 1 +
xen/arch/x86/boot/x86_64.S | 2 +-
xen/arch/x86/efi/efi-boot.h | 67 +------
xen/arch/x86/include/asm/page.h | 3 +-
xen/tools/make_output | 177 ++++++++++++++++++
14 files changed, 446 insertions(+), 207 deletions(-)
rename xen/arch/x86/boot/{build32.lds => build32.lds.S} (52%)
create mode 100644 xen/arch/x86/boot/reloc-trampoline.c
create mode 120000 xen/arch/x86/boot/reloc-trampoline64.c
create mode 100644 xen/arch/x86/boot/setup-pages.c
create mode 120000 xen/arch/x86/boot/setup-pages64.c
create mode 100755 xen/tools/make_outputSignature · 1 line
2.46.0
RFC 1/5 Avoid usage of global in reloc.c
All code and dat from this file will go into a text section which we want to not be writeable.
--- xen/arch/x86/boot/reloc.c | 62 ++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 30 deletions(-)
xen/arch/x86/boot/reloc.c +32 -30
@@ -72,11 +72,13 @@ struct vesa_mode_info {#define get_mb2_data (tag, type, member) (((const multiboot2_tag_##type##_t *)(tag))->member)#define get_mb2_string (tag, type, member) ((uint32_t)get_mb2_data(tag, type, member)) -static uint32_t alloc; +typedef struct memctx { +uint32_t alloc; +}memctx ; -static uint32_t alloc_mem (uint32_t bytes) +static uint32_t alloc_mem (uint32_t bytes,memctx *ctx) { -return alloc -=ROUNDUP (bytes,16 ); +return ctx->alloc -=ROUNDUP (bytes,16 ); }static void zero_mem (uint32_t s,uint32_t bytes)
@@ -85,11 +87,11 @@ static void zero_mem(uint32_t s, uint32_t bytes) *(char *)s++ =0 ; } -static uint32_t copy_mem (uint32_t src,uint32_t bytes) +static uint32_t copy_mem (uint32_t src,uint32_t bytes,memctx *ctx) {uint32_t dst, dst_ret; - dst =alloc_mem (bytes); + dst =alloc_mem (bytes, ctx); dst_ret = dst;while ( bytes-- )
@@ -98,7 +100,7 @@ static uint32_t copy_mem(uint32_t src, uint32_t bytes)return dst_ret; } -static uint32_t copy_string (uint32_t src) +static uint32_t copy_string (uint32_t src,memctx *ctx) {uint32_t p;
@@ -108,17 +110,17 @@ static uint32_t copy_string(uint32_t src)for ( p = src; *(char *)p !='\0' ; p++ )continue ; -return copy_mem (src, p - src +1 ); +return copy_mem (src, p - src +1 , ctx); } -static struct hvm_start_info *pvh_info_reloc (uint32_t in) +static struct hvm_start_info *pvh_info_reloc (uint32_t in,memctx *ctx) {struct hvm_start_info *out; - out =_p (copy_mem (in,sizeof (*out))); + out =_p (copy_mem (in,sizeof (*out), ctx));if ( out->cmdline_paddr ) - out->cmdline_paddr =copy_string (out->cmdline_paddr); + out->cmdline_paddr =copy_string (out->cmdline_paddr, ctx);if ( out->nr_modules ) {
@@ -127,51 +129,51 @@ static struct hvm_start_info *pvh_info_reloc(uint32_t in) out->modlist_paddr =copy_mem (out->modlist_paddr, - out->nr_modules *sizeof (struct hvm_modlist_entry )); + out->nr_modules *sizeof (struct hvm_modlist_entry ), ctx); mods =_p (out->modlist_paddr);for ( i =0 ; i < out->nr_modules; i++ ) {if ( mods[i].cmdline_paddr ) - mods[i].cmdline_paddr =copy_string (mods[i].cmdline_paddr); + mods[i].cmdline_paddr =copy_string (mods[i].cmdline_paddr, ctx); } }return out; } -static multiboot_info_t *mbi_reloc (uint32_t mbi_in) +static multiboot_info_t *mbi_reloc (uint32_t mbi_in,memctx *ctx) {int i;multiboot_info_t *mbi_out; - mbi_out =_p (copy_mem (mbi_in,sizeof (*mbi_out))); + mbi_out =_p (copy_mem (mbi_in,sizeof (*mbi_out), ctx));if ( mbi_out->flags &MBI_CMDLINE ) - mbi_out->cmdline =copy_string (mbi_out->cmdline); + mbi_out->cmdline =copy_string (mbi_out->cmdline, ctx);if ( mbi_out->flags &MBI_MODULES ) {module_t *mods; mbi_out->mods_addr =copy_mem (mbi_out->mods_addr, - mbi_out->mods_count *sizeof (module_t)); + mbi_out->mods_count *sizeof (module_t), ctx); mods =_p (mbi_out->mods_addr);for ( i =0 ; i < mbi_out->mods_count; i++ ) {if ( mods[i].string ) - mods[i].string =copy_string (mods[i].string); + mods[i].string =copy_string (mods[i].string, ctx); } }if ( mbi_out->flags &MBI_MEMMAP ) - mbi_out->mmap_addr =copy_mem (mbi_out->mmap_addr, mbi_out->mmap_length); + mbi_out->mmap_addr =copy_mem (mbi_out->mmap_addr, mbi_out->mmap_length, ctx);if ( mbi_out->flags &MBI_LOADERNAME ) - mbi_out->boot_loader_name =copy_string (mbi_out->boot_loader_name); + mbi_out->boot_loader_name =copy_string (mbi_out->boot_loader_name, ctx);/* Mask features we don't understand or don't relocate. */ mbi_out->flags &= (MBI_MEMLIMITS |
@@ -183,7 +185,7 @@ static multiboot_info_t *mbi_reloc(uint32_t mbi_in)return mbi_out; } -static multiboot_info_t *mbi2_reloc (uint32_t mbi_in,uint32_t video_out) +static multiboot_info_t *mbi2_reloc (uint32_t mbi_in,uint32_t video_out,memctx *ctx) {const multiboot2_fixed_t *mbi_fix =_p (mbi_in);const multiboot2_memory_map_t *mmap_src;
@@ -197,7 +199,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out)uint32_t ptr;unsigned int i, mod_idx =0 ; - ptr =alloc_mem (sizeof (*mbi_out)); + ptr =alloc_mem (sizeof (*mbi_out), ctx); mbi_out =_p (ptr);zero_mem (ptr,sizeof (*mbi_out));
@@ -222,7 +224,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out)* __start_xen() may put Xen image placement into it. */ mbi_out->mods_addr =alloc_mem ((mbi_out->mods_count +1 ) * -sizeof (*mbi_out_mods)); +sizeof (*mbi_out_mods), ctx); mbi_out_mods =_p (mbi_out->mods_addr); }
@@ -238,13 +240,13 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out)case MULTIBOOT2_TAG_TYPE_BOOT_LOADER_NAME : mbi_out->flags |=MBI_LOADERNAME ; ptr =get_mb2_string (tag, string, string); - mbi_out->boot_loader_name =copy_string (ptr); + mbi_out->boot_loader_name =copy_string (ptr, ctx);break ;case MULTIBOOT2_TAG_TYPE_CMDLINE : mbi_out->flags |=MBI_CMDLINE ; ptr =get_mb2_string (tag, string, string); - mbi_out->cmdline =copy_string (ptr); + mbi_out->cmdline =copy_string (ptr, ctx);break ;case MULTIBOOT2_TAG_TYPE_BASIC_MEMINFO :
@@ -263,7 +265,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out) mbi_out->mmap_length /=get_mb2_data (tag, mmap, entry_size); mbi_out->mmap_length *=sizeof (*mmap_dst); - mbi_out->mmap_addr =alloc_mem (mbi_out->mmap_length); + mbi_out->mmap_addr =alloc_mem (mbi_out->mmap_length, ctx); mmap_src =get_mb2_data (tag, mmap, entries); mmap_dst =_p (mbi_out->mmap_addr);
@@ -290,7 +292,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out) mbi_out_mods[mod_idx].mod_start =get_mb2_data (tag, module, mod_start); mbi_out_mods[mod_idx].mod_end =get_mb2_data (tag, module, mod_end); ptr =get_mb2_string (tag, module, cmdline); - mbi_out_mods[mod_idx].string =copy_string (ptr); + mbi_out_mods[mod_idx].string =copy_string (ptr, ctx); mbi_out_mods[mod_idx].reserved =0 ; ++mod_idx;break ;
@@ -356,19 +358,19 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out)void *reloc (uint32_t magic,uint32_t in,uint32_t trampoline,uint32_t video_info) { - alloc = trampoline; +memctx ctx = { trampoline };switch ( magic ) {case MULTIBOOT_BOOTLOADER_MAGIC : -return mbi_reloc (in); +return mbi_reloc (in, &ctx);case MULTIBOOT2_BOOTLOADER_MAGIC : -return mbi2_reloc (in, video_info); +return mbi2_reloc (in, video_info, &ctx);case XEN_HVM_START_MAGIC_VALUE :if (IS_ENABLED (CONFIG_PVH_GUEST ) ) -return pvh_info_reloc (in); +return pvh_info_reloc (in, &ctx);/* Fallthrough */ default :
Signature · 1 line
2.46.0
Re: [RFC 1/5] Avoid usage of global in reloc.c
Quoted 2 lines · Frediano Ziglio
All code and dat from this file will go into a text section which we want to not be writeable.
Getting data out of the instruction cache is a good thing, but writeability isn't relevant here. This logic only executes in 32bit unpaged mode.
Quoted 11 lines
Signed-off-by Frediano Ziglio <[email protected]>--- xen/arch/x86/boot/reloc.c | 62 ++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 30 deletions(-)xen/arch/x86/boot/reloc.c +0
-0
xen/arch/x86/boot/reloc.c struct vesa_mode_info { · line 77
Quoted 7 lines
xen/arch/x86/boot/reloc.c +3
-1@@ -72,11 +72,13 @@ struct vesa_mode_info {#define get_mb2_data (tag, type, member) (((const multiboot2_tag_##type##_t *)(tag))->member)#define get_mb2_string (tag, type, member) ((uint32_t)get_mb2_data(tag, type, member)) -static uint32_t alloc; +typedef struct memctx { +uint32_t alloc; +}memctx ;
Can we take the opportunity to rename alloc to ptr, and have a comment explaining how this works
/* Simple bump allocator. It starts from the base of the trampoline and allocates downwards. */
Happy to fix up on commit.
~Andrew
RFC 2/5 x86/boot: create a C bundle for 32 bit boot code and use it
In reply to Frediano ZiglioThe 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 | 3 +-
xen/arch/x86/boot/Makefile | 28 ++-
.../x86/boot/{build32.lds => build32.lds.S} | 46 ++++-
xen/arch/x86/boot/cmdline.c | 7 -
xen/arch/x86/boot/head.S | 12 --
xen/arch/x86/boot/reloc.c | 7 -
xen/tools/make_output | 177 ++++++++++++++++++
7 files changed, 242 insertions(+), 38 deletions(-)
rename xen/arch/x86/boot/{build32.lds => build32.lds.S} (62%)
create mode 100755 xen/tools/make_output.gitignore +1 -2
@@ -251,8 +251,7 @@ 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/boot/build32.*.lds xen/arch/x86/efi.lds xen/arch/x86/efi/check.efi xen/arch/x86/efi/mkreloc
xen/arch/x86/boot/Makefile +24 -4
@@ -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,31 @@ $(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) +$(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) + %.bin: %.lnk $(OBJCOPY) -j .text -O binary $< $@ -%.lnk: %.o $(src)/build32.lds - $(LD) $(subst x86_64,i386,$(LDFLAGS_DIRECT)) -N -T $(filter %.lds,$^) -o $@ $< +$(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/make_output \ + --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] $@ clean-files := *.lnk *.bin
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +40 -6
71 lines not on this page · raw message
xen/arch/x86/boot/cmdline.c +0 -7
@@ -23,13 +23,6 @@* - %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
@@ -870,18 +870,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. - */ - ALIGN -cmdline_parse_early: - .incbin "cmdline.bin" - - ALIGN -reloc: - .incbin "reloc.bin" - ENTRY(trampoline_start) #include "trampoline.S" ENTRY(trampoline_end)
xen/arch/x86/boot/reloc.c +0 -7
@@ -19,13 +19,6 @@* - %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/make_output new file +177 -0
177 lines not on this page · raw message
Signature · 1 line
2.46.0
Re: [RFC 2/5] x86/boot: create a C bundle for 32 bit boot code and use it
.gitignore xen/System.map · line 254
Quoted 9 lines · Frediano Ziglio
.gitignore +1
-2@@ -251,8 +251,7 @@ 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/boot/build32.*.lds
Please retain (largely) alphabetic sorting.
Quoted 64 lines
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +38
-6@@ -15,22 +15,54 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -ENTRY(_start) +#undef ENTRY + +#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 : { + /* 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; *(.text) *(.text.*) - *(.data) - *(.data.*) *(.rodata) *(.rodata.*) + } + + /* Writeable data sections. Check empty. + * We collapse all into code section and we don't want it to be writeable. */ + .data : { + *(.data) + *(.data.*) *(.bss) *(.bss.*) } - + /DISCARD/ : { + *(.comment) + *(.comment.*) + *(.note.*) + } /* Dynamic linkage sections. Collected simply so we can check they're empty. */ .got : { *(.got)
xen/arch/x86/boot/build32.lds.S SECTIONS · line 84
Quoted 4 lines
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +1
-0@@ -49,6 +81,7 @@ SECTIONS } .rel : { *(.rel.*) + *(.data.rel.*)
This looks like you're mixing up .data.rel (a data section, where some items may require relocation) and .rel.data (the relocation section for .data). If you want all .data.* empty, this section should be empty, too. (I also don't think this would ever take effect, due to the earlier *(.data.*).)
Jan
Re: [RFC 2/5] x86/boot: create a C bundle for 32 bit boot code and use it
Quoted 14 lines · Jan Beulich
.gitignore xen/System.map · line 254
Quoted 9 lines · Frediano Ziglio
.gitignore +1
-2@@ -251,8 +251,7 @@ 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/boot/build32.*.ldsPlease retain (largely) alphabetic sorting.
Changed locally.
Quoted 64 lines
Quoted 24 lines
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +14
-1@@ -15,22 +15,54 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -ENTRY(_start) +#undef ENTRY + +#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 {xen/arch/x86/boot/build32.lds.S line 42
Quoted 10 lines
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +8
-2- /* Merge code and data into one section. */ - .text : { + /* Merge code and read-only data into one section. */ + .text TEXT_START : { + /* 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 weuse.
Quoted 4 lines
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +4
-0+ * Potentially they should be all variables. */ + DECLARE_IMPORT(__base_relocs_start); + DECLARE_IMPORT(__base_relocs_end); + . = . + GAP;xen/arch/x86/boot/build32.lds.S
Quoted 10 lines
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +4
-2*(.text) *(.text.*) - *(.data) - *(.data.*) *(.rodata) *(.rodata.*) + } + + /* Writeable data sections. Check empty. + * We collapse all into code section and we don't want it to bewriteable. */
xen/arch/x86/boot/build32.lds.S
Quoted 13 lines
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +8
-1+ .data : { + *(.data) + *(.data.*) *(.bss) *(.bss.*) } - + /DISCARD/ : { + *(.comment) + *(.comment.*) + *(.note.*) + } /* Dynamic linkage sections. Collected simply so we can checkthey're empty. */
Quoted 17 lines
xen/arch/x86/boot/build32.lds.S SECTIONS · line 84
Quoted 7 lines
xen/arch/x86/boot/build32.lds.S renamed from xen/arch/x86/boot/build32.lds +1
-0.got : { *(.got)@@ -49,6 +81,7 @@ SECTIONS } .rel : { *(.rel.*) + *(.data.rel.*)This looks like you're mixing up .data.rel (a data section, where some items
may require relocation) and .rel.data (the relocation section for .data). If
you want all .data.* empty, this section should be empty, too. (I also don't
think this would ever take effect, due to the earlier *(.data.*).)
Removed this last hunk, yes, it's useless.
Quoted 2 lines
Jan
Thanks,
Frediano
RFC 3/5 Reuse code to relocate trampoline
In reply to Frediano Ziglio--- xen/arch/x86/boot/Makefile | 6 +++--- xen/arch/x86/boot/build32.lds.S | 4 ++++ xen/arch/x86/boot/head.S | 24 ++-------------------- xen/arch/x86/boot/reloc-trampoline.c | 28 ++++++++++++++++++++++++++ xen/arch/x86/boot/reloc-trampoline64.c | 1 + xen/arch/x86/efi/efi-boot.h | 15 ++------------ 6 files changed, 40 insertions(+), 38 deletions(-) create mode 100644 xen/arch/x86/boot/reloc-trampoline.c create mode 120000 xen/arch/x86/boot/reloc-trampoline64.c
xen/arch/x86/boot/Makefile +3 -3
@@ -1,6 +1,6 @@ -obj-bin-y += head.o cbundle.o +obj-bin-y += head.o cbundle.o reloc-trampoline64.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/make_output \ --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 +4 -0
@@ -43,6 +43,10 @@ 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); . = . + GAP; *(.text) *(.text.*)
xen/arch/x86/boot/head.S +2 -22
@@ -817,28 +817,8 @@ 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 + mov sym_esi(trampoline_phys), %eax + 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 +28 -0
@@ -0,0 +1,28 @@ + +#include <xen/stdint.h> + +#pragma GCC visibility push(hidden) +extern const int32_t __trampoline_rel_start[], __trampoline_rel_stop[]; +extern const int32_t __trampoline_seg_start[], __trampoline_seg_stop[]; +#pragma GCC visibility pop + +#if defined(__i386__) +void reloc_trampoline32(unsigned long phys) +#elif defined (__x86_64__) +void reloc_trampoline64(unsigned long phys) +#else +#error Unknow architecture +#endif +{ +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/boot/reloc-trampoline64.c new file +1 -0
@@ -0,0 +1 @@ +reloc-trampoline.c
xen/arch/x86/efi/efi-boot.h +2 -13
@@ -101,27 +101,16 @@ static void __init efi_arch_relocate_image(unsigned long delta) } } -extern const s32 __trampoline_rel_start[], __trampoline_rel_stop[]; -extern const s32 __trampoline_seg_start[], __trampoline_seg_stop[]; +void reloc_trampoline64 (unsigned long phys);static void __init relocate_trampoline (unsigned long phys) { -const s32 *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 (phys); }static void __init place_string (u32 *addr,const char *s)
Signature · 1 line
2.46.0
RFC 4/5 Remove duplication preparing pages
In reply to Frediano ZiglioPretty hacky, headers are not that compatible 32/64 bits
--- xen/arch/x86/boot/Makefile | 6 +- xen/arch/x86/boot/build32.lds.S | 8 +++ xen/arch/x86/boot/head.S | 43 +----------- xen/arch/x86/boot/setup-pages.c | 105 ++++++++++++++++++++++++++++++ xen/arch/x86/boot/setup-pages64.c | 1 + xen/arch/x86/efi/efi-boot.h | 52 +-------------- 6 files changed, 121 insertions(+), 94 deletions(-) create mode 100644 xen/arch/x86/boot/setup-pages.c create mode 120000 xen/arch/x86/boot/setup-pages64.c
xen/arch/x86/boot/Makefile +3 -3
@@ -1,6 +1,6 @@ -obj-bin-y += head.o cbundle.o reloc-trampoline64.o +obj-bin-y += head.o cbundle.o reloc-trampoline64.o setup-pages64.o -head-bin-objs := cmdline.o reloc.o reloc-trampoline.o +head-bin-objs := cmdline.o reloc.o reloc-trampoline.o setup-pages.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/make_output \ --script $(obj)/build32.final.lds \ --bin1 [email protected] --bin2 [email protected] \ - --map $(obj)/cbundle.map --exports cmdline_parse_early,reloc,reloc_trampoline32 \ + --map $(obj)/cbundle.map --exports cmdline_parse_early,reloc,reloc_trampoline32,setup_pages32 \ --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 +8 -0
@@ -47,6 +47,14 @@ SECTIONS DECLARE_IMPORT(__trampoline_rel_stop); DECLARE_IMPORT(__trampoline_seg_start); DECLARE_IMPORT(__trampoline_seg_stop); + DECLARE_IMPORT(l2_xenmap); + DECLARE_IMPORT(l2_directmap); + DECLARE_IMPORT(l2_bootmap); + DECLARE_IMPORT(l3_bootmap); + DECLARE_IMPORT(_start); + DECLARE_IMPORT(_end); + DECLARE_IMPORT(xen_phys_start); + //DECLARE_IMPORT(); . = . + GAP; *(.text) *(.text.*)
xen/arch/x86/boot/head.S +1 -42
49 lines not on this page · raw message
xen/arch/x86/boot/setup-pages.c new file +105 -0
105 lines not on this page · raw message
xen/arch/x86/boot/setup-pages64.c new file +1 -0
@@ -0,0 +1 @@ +setup-pages.c
xen/arch/x86/efi/efi-boot.h +3 -49
66 lines not on this page · raw message
Signature · 1 line
2.46.0
RFC 5/5 setup mapping for trampoline in setup_pagesXX
In reply to Frediano ZiglioReduce assembly code, make boot page mappings more similar between multiple paths (direct EFI and not).
--- xen/arch/x86/boot/build32.lds.S | 2 ++ xen/arch/x86/boot/head.S | 10 ---------- xen/arch/x86/boot/setup-pages.c | 25 ++++++++++++++++++------- xen/arch/x86/boot/x86_64.S | 2 +- xen/arch/x86/include/asm/page.h | 3 ++- 5 files changed, 23 insertions(+), 19 deletions(-)
xen/arch/x86/boot/build32.lds.S +2 -0
@@ -49,11 +49,13 @@ SECTIONS DECLARE_IMPORT(__trampoline_seg_stop); DECLARE_IMPORT(l2_xenmap); DECLARE_IMPORT(l2_directmap); + DECLARE_IMPORT(l1_bootmap); DECLARE_IMPORT(l2_bootmap); DECLARE_IMPORT(l3_bootmap); DECLARE_IMPORT(_start); DECLARE_IMPORT(_end); DECLARE_IMPORT(xen_phys_start); + DECLARE_IMPORT(trampoline_phys); //DECLARE_IMPORT(); . = . + GAP; *(.text)
xen/arch/x86/boot/head.S +0 -10
@@ -765,16 +765,6 @@ trampoline_setup: call setup_pages32 - /* Map l1_bootmap[] into l2_bootmap[0]. */ - lea __PAGE_HYPERVISOR + sym_esi(l1_bootmap), %eax - mov %eax, sym_esi(l2_bootmap) - - /* Map the permanent trampoline page into l1_bootmap[]. */ - mov sym_esi(trampoline_phys), %ecx - lea __PAGE_HYPERVISOR_RX(%ecx), %edx /* %edx = PTE to write */ - shr $PAGE_SHIFT, %ecx /* %ecx = Slot to write */ - mov %edx, sym_offs(l1_bootmap)(%esi, %ecx, 8) - /* Apply relocations to bootstrap trampoline. */ mov sym_esi(trampoline_phys), %eax call reloc_trampoline32
xen/arch/x86/boot/setup-pages.c +18 -7
@@ -9,8 +9,10 @@#pragma GCC visibility push(hidden)extern char _start[], _end[]; -extern uint64_t l2_xenmap[512 ], l3_bootmap[512 ], l2_directmap[512 ], l2_bootmap[512 ]; +extern uint64_t l2_xenmap[512 ], l2_directmap[512 ], + l3_bootmap[512 ], l2_bootmap[512 ], l1_bootmap[512 ];extern unsigned long xen_phys_start; +extern unsigned long trampoline_phys;#pragma GCC visibility pop#define _PAGE_PRESENT 0x001
@@ -19,12 +21,11 @@ extern unsigned long xen_phys_start;#define _PAGE_DIRTY 0x040#define _PAGE_PSE 0x080#define _PAGE_GLOBAL 0x100 +#define _PAGE_NX (1LLU << 63)#define PAGE_HYPERVISOR PAGE_HYPERVISOR_RW#define PAGE_HYPERVISOR_RW (__PAGE_HYPERVISOR_RW | _PAGE_GLOBAL)#define __PAGE_HYPERVISOR_RW (__PAGE_HYPERVISOR_RO | _PAGE_DIRTY | _PAGE_RW) -// TODO -#define _PAGE_NX 0#define __PAGE_HYPERVISOR_RO (_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_NX)#define PAGE_HYPERVISOR_RWX (__PAGE_HYPERVISOR | _PAGE_GLOBAL)#define __PAGE_HYPERVISOR (__PAGE_HYPERVISOR_RX | _PAGE_DIRTY | _PAGE_RW)
@@ -33,14 +34,16 @@ extern unsigned long xen_phys_start;#define L2_PAGETABLE_SHIFT 21#define L2_PAGETABLE_ENTRIES 512#define PAGE_SIZE 4096 +#define PAGE_SHIFT 12#define l2_table_offset (a) (((a) >> L2_PAGETABLE_SHIFT) & (L2_PAGETABLE_ENTRIES - 1)) -#define l2e_from_paddr (a,f) ((a) | put_pte_flags(f)) -#define l3e_from_paddr (a,f) ((a) | put_pte_flags(f)) +#define l1e_from_paddr (a, flags) ((a) | put_pte_flags(flags)) +#define l2e_from_paddr (a, flags) ((a) | put_pte_flags(flags)) +#define l3e_from_paddr (a, flags) ((a) | put_pte_flags(flags))#define l2e_add_flags (x, flags) (x |= put_pte_flags(flags))typedef uint64_t l2_pgentry_t ; -static inline int64_t put_pte_flags (unsigned int x) +static inline uint64_t put_pte_flags (uint64_t x) { -return (((int64_t )x & ~0xfff ) <<40 ) | (x &0xfff ); +return x; }void __attribute__((__stdcall__))setup_pages32 (void )
@@ -102,4 +105,12 @@ void setup_pages64(void) l2_directmap[i] = pte; }#undef l2_4G_offset + +/* Map l1_bootmap[] into l2_bootmap[0]. */ + l2_bootmap[0 ] =l2e_from_paddr ((unsigned long )l1_bootmap, + __PAGE_HYPERVISOR); + +/* Map the permanent trampoline page into l1_bootmap[]. */ + l1_bootmap[(unsigned long )trampoline_phys >>PAGE_SHIFT ] = +l1e_from_paddr ((unsigned long )trampoline_phys, __PAGE_HYPERVISOR_RX); }
xen/arch/x86/boot/x86_64.S +1 -1
@@ -195,7 +195,7 @@ GLOBAL(__page_tables_end) .section .init.data, "aw", @progbits .align PAGE_SIZE, 0 -l1_bootmap: +GLOBAL(l1_bootmap) .fill L1_PAGETABLE_ENTRIES, 8, 0 .size l1_bootmap, . - l1_bootmap
xen/arch/x86/include/asm/page.h +2 -1
@@ -286,7 +286,8 @@ extern l2_pgentry_t l2_xenmap[L2_PAGETABLE_ENTRIES], l2_bootmap[4 *L2_PAGETABLE_ENTRIES ];extern l3_pgentry_t l3_bootmap[L3_PAGETABLE_ENTRIES ];extern l2_pgentry_t l2_directmap[4 *L2_PAGETABLE_ENTRIES ]; -extern l1_pgentry_t l1_fixmap[L1_PAGETABLE_ENTRIES ]; +extern l1_pgentry_t l1_fixmap[L1_PAGETABLE_ENTRIES ], + l1_bootmap[L1_PAGETABLE_ENTRIES ];void paging_init (void );void efi_update_l4_pgtable (unsigned int l4idx,l4_pgentry_t l4e);#endif /* !defined(__ASSEMBLY__) */
Signature · 1 line
2.46.0
Quoted 24 lines · Frediano Ziglio
This RFC 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.Smaking 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 2/5, which is the largest patch.
Patch 1/5 is to prepare code and avoid data.
Patch 3/5 is an example of code reuse between 32 and 64 bit. Patch 4/5 is also another example of code reuse but is more hacky and dirty due to not being possible include use some headers.Code boot successfully using:
- BIOS boot;
- EFI boot with Grub2 and ELF file;
- direct EFI boot without Grub.
Quoted 4 lines
Suggestions/opinions are welcome.
Code is currently based on "staging" branch, currently commit
6471badeeec92db1cb8155066551f7509cd82efd.
I fully support taking logic out of asm and writing it in C, as well as taking steps to dedup the EFI and non-EFI paths. A couple of observations before diving into the details.
The visibility pragmas mean that you've lost the -include xen/config.h from the $(CC) invocation. We use this to get some Xen-wide settings everywhere, which includes handling visibility.
The symlinks for dual builds are going to cause problems for tarball archives. Instead you can encode this with make rules. e.g.
obj-y += foo32.o foo64.o
%32.o: %.c
$(CC) -m32 ...
%64.o: %.c
$(CC) -m64 ...
will build two different .o's from the same .c. This is how XTF builds different tests from the same source.
I'm on the fence with the ifdefary and bit suffixes. I don't think we need the error case because x86_128 isn't coming along any time soon.
For completeness, there's a trick used by the shadow code (see SHADOW_INTERNAL_NAME()) which adds a suffix without local ifdefary. It's nicer to read, but breaks grep/cscope/etc. I'm torn as to which is the lesser evil.
~Andrew
Quoted 53 lines · Andrew Cooper
Quoted 24 lines · Frediano Ziglio
This RFC 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.Smaking 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 2/5, which is the largest patch.
Patch 1/5 is to prepare code and avoid data.
Patch 3/5 is an example of code reuse between 32 and 64 bit. Patch 4/5 is also another example of code reuse but is more hacky and dirty due to not being possible include use some headers.Code boot successfully using:
- BIOS boot;
- EFI boot with Grub2 and ELF file;
- direct EFI boot without Grub.Quoted 4 lines
Suggestions/opinions are welcome.
Code is currently based on "staging" branch, currently commit
6471badeeec92db1cb8155066551f7509cd82efd.I fully support taking logic out of asm and writing it in C, as well as taking steps to dedup the EFI and non-EFI paths. A couple of observations before diving into the details.
The visibility pragmas mean that you've lost the
-include xen/config.hfrom the $(CC) invocation. We use this to get some Xen-wide settings everywhere, which includes handling visibility.The symlinks for dual builds are going to cause problems for tarball archives. Instead you can encode this with make rules. e.g.
obj-y += foo32.o foo64.o
%32.o: %.c
$(CC) -m32 ...%64.o: %.c
$(CC) -m64 ...will build two different .o's from the same .c. This is how XTF builds different tests from the same source.
Quoted 7 lines
I'm on the fence with the ifdefary and bit suffixes. I don't think we need the error case because
x86_128isn't coming along any time soon.For completeness, there's a trick used by the shadow code (see
SHADOW_INTERNAL_NAME()) which adds a suffix without local ifdefary. It's nicer to read, but breaks grep/cscope/etc. I'm torn as to which is the lesser evil.
While generally I prefer that approach shadow code takes, I think the #ifdef-ary is acceptably bounded here. What I'm more worried by are the fair number of #define-s for the 32-bit case of setting up page tables.
Jan