Mini-OS: x86: zero out .bss segment at boot
PATCH 1/1 Mini-OS: x86: zero out .bss segment at boot
The .bss segment should be zeroed at very early boot.
While adding the extern declaration of __bss_start for x86, make it together with the other linker table defined section boundaries common for all architectures.
--- arch/x86/setup.c | 2 ++ include/arm/arch_mm.h | 1 - include/mm.h | 2 ++ include/x86/arch_mm.h | 1 - 4 files changed, 4 insertions(+), 2 deletions(-)
arch/x86/setup.c +2 -0
@@ -184,6 +184,8 @@ arch_init(void *par) {static char hello[] ="Bootstrapping...\n" ; +memset (&__bss_start,0 , &_end - &__bss_start); +hpc_init (); (void )HYPERVISOR_console_io (CONSOLEIO_write,strlen (hello), hello);
include/arm/arch_mm.h +0 -1
@@ -3,7 +3,6 @@typedef uint64_t paddr_t ; -extern char _text, _etext, _erodata, _edata, _end, __bss_start;extern int _boot_stack[];extern int _boot_stack_end[];extern uint32_t physical_address_offset;/* Add this to a virtual address to get the physical address (wraps at 4GB) */
include/mm.h +2 -0
@@ -46,6 +46,8 @@#define round_pgdown (_p) ((_p) & PAGE_MASK)#define round_pgup (_p) (((_p) + (PAGE_SIZE - 1)) & PAGE_MASK) +extern char _text, _etext, _erodata, _edata, _end, __bss_start; +extern unsigned long nr_free_pages;extern unsigned long *mm_alloc_bitmap;
include/x86/arch_mm.h +0 -1
@@ -225,7 +225,6 @@ extern unsigned long *phys_to_machine_mapping;#else extern pgentry_t page_table_base[];#endif -extern char _text, _etext, _erodata, _edata, _end;extern unsigned long mfn_zero;static __inline__maddr_t phys_to_machine (paddr_t phys) {
Signature · 1 line
2.35.3
Juergen Gross, le mer. 07 févr. 2024 11:31:38 +0100, a ecrit:
Quoted 1 line
The .bss segment should be zeroed at very early boot.
Is that not done by the elf loader of Xen?
Quoted 64 lines
While adding the extern declaration of
__bss_startfor x86, make it together with the other linker table defined section boundaries common for all architectures.Signed-off-by Juergen Gross <[email protected]>--- arch/x86/setup.c | 2 ++ include/arm/arch_mm.h | 1 - include/mm.h | 2 ++ include/x86/arch_mm.h | 1 - 4 files changed, 4 insertions(+), 2 deletions(-)arch/x86/setup.c +2
-0@@ -184,6 +184,8 @@ arch_init(void *par) {static char hello[] ="Bootstrapping...\n" ; +memset (&__bss_start,0 , &_end - &__bss_start); +hpc_init (); (void )HYPERVISOR_console_io (CONSOLEIO_write,strlen (hello), hello);include/arm/arch_mm.h +0
-1@@ -3,7 +3,6 @@typedef uint64_t paddr_t ; -extern char _text, _etext, _erodata, _edata, _end, __bss_start;extern int _boot_stack[];extern int _boot_stack_end[];extern uint32_t physical_address_offset;/* Add this to a virtual address to get the physical address (wraps at 4GB) */ include/mm.h +2
-0@@ -46,6 +46,8 @@#define round_pgdown (_p) ((_p) & PAGE_MASK)#define round_pgup (_p) (((_p) + (PAGE_SIZE - 1)) & PAGE_MASK) +extern char _text, _etext, _erodata, _edata, _end, __bss_start; +extern unsigned long nr_free_pages;extern unsigned long *mm_alloc_bitmap;include/x86/arch_mm.h +0
-1@@ -225,7 +225,6 @@ extern unsigned long *phys_to_machine_mapping;#else extern pgentry_t page_table_base[];#endif -extern char _text, _etext, _erodata, _edata, _end;extern unsigned long mfn_zero;static __inline__maddr_t phys_to_machine (paddr_t phys) {Signature · 1 line
2.35.3
Quoted 4 lines · Samuel Thibault
Juergen Gross, le mer. 07 févr. 2024 11:31:38 +0100, a ecrit:
Quoted 1 line
The .bss segment should be zeroed at very early boot.
Is that not done by the elf loader of Xen?
It might be done by Xen tools today, but I'm quite sure it is not part of the ABI. The hypervisor doesn't clear .bss when loading a domain (e.g. dom0).
I stumbled over it while implementing kexec in Mini-OS. For that I need it for sure.
Juergen
Quoted 9 lines · Jürgen Groß
Quoted 4 lines · Samuel Thibault
Juergen Gross, le mer. 07 févr. 2024 11:31:38 +0100, a ecrit:
Quoted 1 line
The .bss segment should be zeroed at very early boot.
Is that not done by the elf loader of Xen?
It might be done by Xen tools today, but I'm quite sure it is not part of the ABI. The hypervisor doesn't clear .bss when loading a domain (e.g. dom0).
libelf's elf_memcpy() looks to say otherwise.
Quoted 2 lines
I stumbled over it while implementing kexec in Mini-OS. For that I need it for sure.
Hmm, yes, if the protocol there doesn't guarantee zeroing. But then you probably want to give this as the reason for the change?
Jan
Quoted 9 lines · Jürgen Groß
Quoted 4 lines · Samuel Thibault
Juergen Gross, le mer. 07 févr. 2024 11:31:38 +0100, a ecrit:
Quoted 1 line
The .bss segment should be zeroed at very early boot.
Is that not done by the elf loader of Xen?
It might be done by Xen tools today, but I'm quite sure it is not part of the ABI. The hypervisor doesn't clear .bss when loading a domain (e.g. dom0).
It is a requirement of loading an ELF file.
If we're not doing it for dom0, that's a bug.
Quoted 2 lines
I stumbled over it while implementing kexec in Mini-OS. For that I need it for sure.
A kexec kernel does need to do this, unless the kexec-loader is going to guarantee to do it, and I don't believe it does.
~Andrew
Jürgen Groß, le mer. 07 févr. 2024 11:42:20 +0100, a ecrit:
Quoted 1 line
while implementing kexec in Mini-OS.
Oh, nice :D
Quoted 1 line
For that I need it for sure.
It needs to be done by kexec itself then.
Samuel
Quoted 8 lines · Samuel Thibault
Jürgen Groß, le mer. 07 févr. 2024 11:42:20 +0100, a ecrit:
Quoted 1 line
while implementing kexec in Mini-OS.
Oh, nice :D
Quoted 1 line
For that I need it for sure.
It needs to be done by kexec itself then.
That's another option, yes.
The question is whether we want to support to be kexec-ed from other systems, too. Then I'd rather do it on both sides.
Juergen
Jürgen Groß, le mer. 07 févr. 2024 12:16:44 +0100, a ecrit:
Quoted 6 lines · Samuel Thibault
Quoted 5 lines
Jürgen Groß, le mer. 07 févr. 2024 11:42:20 +0100, a ecrit:
Quoted 1 line
while implementing kexec in Mini-OS.
Oh, nice :D
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
For that I need it for sure.
It needs to be done by kexec itself then.
That's another option, yes.
The question is whether we want to support to be kexec-ed from other systems, too.
But aren't other systems' kexec supports supposed to do the memset?
They really should.
Samuel
Quoted 11 lines · Samuel Thibault
Jürgen Groß, le mer. 07 févr. 2024 12:16:44 +0100, a ecrit:
Quoted 10 lines · Samuel Thibault
Quoted 8 lines
Jürgen Groß, le mer. 07 févr. 2024 11:42:20 +0100, a ecrit:
Quoted 1 line
while implementing kexec in Mini-OS.
Oh, nice :D
Quoted 1 line
For that I need it for sure.
It needs to be done by kexec itself then.
Quoted 8 lines
Quoted 4 lines
That's another option, yes.
The question is whether we want to support to be kexec-ed from other systems, too.
But aren't other systems' kexec supports supposed to do the memset?
They really should.
I guess there is a reason why the Linux kernel does clear its .bss section in early boot. Maybe it is due to how the boot process works (the ELF file is encapsulated in vmlinuz), but following your reasoning they should have cleared their .bss while unpacking the ELF contents, not while booting the contents.
I'm not sure they do the .bss clearing in kexec either, as the kernel is clearing it anyway.
Juergen
Jürgen Groß, le mer. 07 févr. 2024 12:43:03 +0100, a ecrit:
Quoted 17 lines · Samuel Thibault
Quoted 16 lines
Jürgen Groß, le mer. 07 févr. 2024 12:16:44 +0100, a ecrit:
Quoted 14 lines · Samuel Thibault
Quoted 8 lines
Jürgen Groß, le mer. 07 févr. 2024 11:42:20 +0100, a ecrit:
Quoted 1 line
while implementing kexec in Mini-OS.
Oh, nice :D
Quoted 1 line
For that I need it for sure.
It needs to be done by kexec itself then.
That's another option, yes.
The question is whether we want to support to be kexec-ed from other systems, too.
Quoted 7 lines
Quoted 3 lines
But aren't other systems' kexec supports supposed to do the memset?
They really should.
I guess there is a reason why the Linux kernel does clear its .bss section in early boot. Maybe it is due to how the boot process works (the ELF file is encapsulated in vmlinuz),
Yes, the unpack prevents grub etc. from doing it.
Quoted 2 lines
but following your reasoning they should have cleared their .bss while unpacking the ELF contents, not while booting the contents.
AIUI the decompressor itself doesn't actually know about ELF. decompress_kernel() does call handle_relocations(), but it should indeed clear bss itself and not leave it out to assembly indeed.
Quoted 1 line
I'm not sure they do the .bss clearing in kexec either,
AIUI they do, see kimage_load_normal_segment() which clears the page before possibly loading some file piece into it.
Really, this is part of what "loading an ELF" means.
Samuel
Quoted 35 lines · Samuel Thibault
Jürgen Groß, le mer. 07 févr. 2024 12:43:03 +0100, a ecrit:
Quoted 17 lines · Samuel Thibault
Quoted 16 lines
Jürgen Groß, le mer. 07 févr. 2024 12:16:44 +0100, a ecrit:
Quoted 14 lines · Samuel Thibault
Quoted 8 lines
Jürgen Groß, le mer. 07 févr. 2024 11:42:20 +0100, a ecrit:
Quoted 1 line
while implementing kexec in Mini-OS.
Oh, nice :D
Quoted 1 line
For that I need it for sure.
It needs to be done by kexec itself then.
That's another option, yes.
The question is whether we want to support to be kexec-ed from other systems, too.
Quoted 7 lines
Quoted 3 lines
But aren't other systems' kexec supports supposed to do the memset?
They really should.
I guess there is a reason why the Linux kernel does clear its .bss section in early boot. Maybe it is due to how the boot process works (the ELF file is encapsulated in vmlinuz),
Yes, the unpack prevents grub etc. from doing it.
Quoted 2 lines
but following your reasoning they should have cleared their .bss while unpacking the ELF contents, not while booting the contents.
AIUI the decompressor itself doesn't actually know about ELF.
decompress_kernel()does callhandle_relocations(), but it should indeed clear bss itself and not leave it out to assembly indeed.
Quoted 6 lines
Quoted 1 line
I'm not sure they do the .bss clearing in kexec either,
AIUI they do, see
kimage_load_normal_segment()which clears the page before possibly loading some file piece into it.Really, this is part of what "loading an ELF" means.
Okay, I'll do the clearing only in the kexec code then.
Juergen
arch/x86/setup.c arch_init(void *par) · line 187
Quoted 7 lines · Juergen Gross
arch/x86/setup.c +1
-0@@ -184,6 +184,8 @@ arch_init(void *par) {static char hello[] ="Bootstrapping...\n" ; +memset (&__bss_start,0 , &_end - &__bss_start);
Doesn't / shouldn't the loader guarantee this? I ask in particular because doing this here implies earlier assembly code may not write to any variable in .bss (nothing like that looks to exist right now, but who knows what may be added at some point).
include/mm.h line 49
Quoted 7 lines
include/mm.h +1
-0@@ -46,6 +46,8 @@#define round_pgdown (_p) ((_p) & PAGE_MASK)#define round_pgup (_p) (((_p) + (PAGE_SIZE - 1)) & PAGE_MASK) +extern char _text, _etext, _erodata, _edata, _end, __bss_start;
Maybe use the more conventional array form? Thus also eliminating the need for & at the use sites?
Jan
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Juergen Gross
arch/x86/setup.c +0
-0@@ -184,6 +184,8 @@ arch_init(void *par) {static char hello[] ="Bootstrapping...\n" ;
Quoted 6 lines
arch/x86/setup.c arch_init(void *par) · line 187
Quoted 1 line
arch/x86/setup.c +1
-0+memset (&__bss_start,0 , &_end - &__bss_start);Doesn't / shouldn't the loader guarantee this? I ask in particular because doing this here implies earlier assembly code may not write to any variable in .bss (nothing like that looks to exist right now, but who knows what may be added at some point).
Oh, am I misremembering that the hypervisor doesn't do the clearing?
I do remember a bug when dom0 Linux crashed due to a not zeroed .bss, so I added the zeroing in the paravirt startup code of the kernel.
But maybe this was due to a bug in the linker script of the kernel.
Quoted 11 lines
include/mm.h line 49
Quoted 8 lines
include/mm.h +1
-0@@ -46,6 +46,8 @@#define round_pgdown (_p) ((_p) & PAGE_MASK)#define round_pgup (_p) (((_p) + (PAGE_SIZE - 1)) & PAGE_MASK) +extern char _text, _etext, _erodata, _edata, _end, __bss_start;Maybe use the more conventional array form? Thus also eliminating the need for & at the use sites?
This was basically just a movement of code to another file.
But I can make this change, of course.
Juergen
Quoted 15 lines · Juergen Gross
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Juergen Gross
arch/x86/setup.c +0
-0@@ -184,6 +184,8 @@ arch_init(void *par) {static char hello[] ="Bootstrapping...\n" ;Quoted 6 lines
arch/x86/setup.c arch_init(void *par) · line 187
Quoted 1 line
arch/x86/setup.c +1
-0+memset (&__bss_start,0 , &_end - &__bss_start);Doesn't / shouldn't the loader guarantee this? I ask in particular because doing this here implies earlier assembly code may not write to any variable in .bss (nothing like that looks to exist right now, but who knows what may be added at some point).
Quoted 6 lines
Oh, am I misremembering that the hypervisor doesn't do the clearing?
I do remember a bug when dom0 Linux crashed due to a not zeroed .bss, so I added the zeroing in the paravirt startup code of the kernel.
But maybe this was due to a bug in the linker script of the kernel.
That's what I recall.
Jan