Skip to contentSkip to conversation

Mini-OS: x86: zero out .bss segment at boot

PATCH In review Juergen Gross 4 participants 14 messages Feb 7, 2024 x86

lore
Share & tools
lore
#0 lore raw

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
index b27bbed7..3dddf4ad 100644
@@ -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
index 79d9e05b..335eb4ff 100644
@@ -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
index 4fc364ff..e02e080b 100644
@@ -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
index a1b975dc..6b398cef 100644
@@ -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
#1 lore raw

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_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
index b27bbed7..3dddf4ad 100644
@@ -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
index 79d9e05b..335eb4ff 100644
@@ -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
index 4fc364ff..e02e080b 100644
@@ -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
index a1b975dc..6b398cef 100644
@@ -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
#2 lore raw
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

#3 lore raw
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

#4 lore raw
In reply to Jürgen Groß
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

#5 lore raw
In reply to Jürgen Groß

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

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

#7 lore raw

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

#8 lore raw
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

#9 lore raw

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

#10 lore raw
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 call handle_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

#11 lore raw
In reply to Juergen Gross

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

#12 lore raw
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

#13 lore raw
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