From 0a8983119338d7cd917b7827cda92aded027ee22 Mon Sep 17 00:00:00 2001 From: TuxSH Date: Wed, 28 Feb 2018 22:29:17 +0100 Subject: [PATCH] Clear bss at runtime like Nintendo actually does --- exosphere/linker.ld | 31 +++++++++++++++++++------------ exosphere/src/coldboot_init.c | 18 +++++++++++++----- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/exosphere/linker.ld b/exosphere/linker.ld index 9d8123482..d22864ee7 100644 --- a/exosphere/linker.ld +++ b/exosphere/linker.ld @@ -68,8 +68,14 @@ SECTIONS build/package2.o(.rodata*) build/package2.o(.data*) . = ALIGN(8); + } >pk2ldr AT>fake + + .pk2ldr.bss : + { + . = ALIGN(8); + __pk2ldr_bss_start__ = ABSOLUTE(.); build/package2.o(.bss*) - . = ALIGN(16); + . = ALIGN(8); __pk2ldr_end__ = ABSOLUTE(.); } >pk2ldr AT>fake @@ -79,7 +85,7 @@ SECTIONS __vectors_lma__ = LOADADDR(.vectors); __vectors_start__ = ABSOLUTE(.); KEEP (*(.vectors*)) - . = ALIGN(16); + . = ALIGN(8); __vectors_end__ = ABSOLUTE(.); } >evt AT>fake @@ -178,16 +184,6 @@ SECTIONS *(.data .data.* .gnu.linkonce.d.*) CONSTRUCTORS . = ALIGN(8); - /* - We're too lazy to clear BSS so we integrate it into .data. - Nintendo does the same for this main segment's BSS. - */ - __bss_start__ = ABSOLUTE(.); - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - . = ALIGN(8); - __bss_end__ = ABSOLUTE(.); } >main AT>fake @@ -209,6 +205,17 @@ SECTIONS .dynstr : { *(.dynstr) } >main AT>fake .rela.dyn : { *(.rela.*); __main_end__ = ABSOLUTE(.);} >main AT>fake + .bss : + { + . = ALIGN(8); + __main_bss_start__ = ABSOLUTE(.); + *(.dynbss) + *(.bss .bss.* .gnu.linkonce.b.*) + *(COMMON) + . = ALIGN(8); + __main_end__ = ABSOLUTE(.); + } >main AT>fake + __end__ = ABSOLUTE(.) ; . = ALIGN(0x1000); diff --git a/exosphere/src/coldboot_init.c b/exosphere/src/coldboot_init.c index 86984f6ec..f7c7d431e 100644 --- a/exosphere/src/coldboot_init.c +++ b/exosphere/src/coldboot_init.c @@ -1,3 +1,4 @@ +#include #include "utils.h" #include "mmu.h" #include "memory_map.h" @@ -5,8 +6,8 @@ extern const uint8_t __start_cold[]; extern const uint8_t __warmboot_crt0_start__[], __warmboot_crt0_end__[], __warmboot_crt0_lma__[]; -extern const uint8_t __main_start__[], __main_end__[], __main_lma__[]; -extern const uint8_t __pk2ldr_start__[], __pk2ldr_end__[], __pk2ldr_lma__[]; +extern const uint8_t __main_start__[], __main_bss_start__[], __main_end__[], __main_lma__[]; +extern const uint8_t __pk2ldr_start__[], __pk2ldr_bss_start__[], __pk2ldr_end__[], __pk2ldr_lma__[]; extern const uint8_t __vectors_start__[], __vectors_end__[], __vectors_lma__[]; /* warmboot_init.c */ @@ -120,10 +121,17 @@ __attribute__((target("cmodel=large"), noinline)) static void copy_other_section copy_lma_to_vma(__vectors_start__, __vectors_lma__, __vectors_end__ - __vectors_start__); } +__attribute__((target("cmodel=large"), noinline)) static void clear_bss(void) { + memset((void *)__pk2ldr_bss_start__, 0, __pk2ldr_end__ - __pk2ldr_bss_start__); + memset((void *)__main_bss_start__, 0, __main_end__ - __main_bss_start__); +} + void coldboot_init(void) { /* TODO: Set NX BOOTLOADER clock time field */ + copy_warmboot_crt0(); /* At this point, we can (and will) access functions located in .warm_crt0 */ + /* TODO: initialize DMA controllers, etc. */ configure_ttbls(); copy_other_sections(); @@ -131,7 +139,7 @@ void coldboot_init(void) { flush_dcache_all_tzram_pa(); invalidate_icache_all_inner_shareable_tzram_pa(); - /* At this point we can access all the mapped segments */ - /* TODO: zero-initialize the cpu context */ - /* Nintendo clears the (emtpy) pk2ldr's BSS section here , but we embed it 0-filled in the binary */ + /* At this point we can access all the mapped segments (all other functions, data...) normally */ + + clear_bss(); }