Unstub lma2vma code

This commit is contained in:
TuxSH 2018-02-27 13:26:51 +01:00
parent b0cb819b06
commit 24b15dad40
2 changed files with 65 additions and 44 deletions

View file

@ -42,6 +42,8 @@ SECTIONS
.warm_crt0 :
{
. = ALIGN(64);
__warmboot_crt0_lma__ = LOADADDR(.warm_crt0);
__warmboot_crt0_start__ = ABSOLUTE(.);
KEEP (*(.warm_crt0.text*))
KEEP (build/warmboot_init.o(.text*))
*(.warm_crt0.rodata*)
@ -52,26 +54,46 @@ SECTIONS
*(.warm_crt0.bss*)
build/warmboot_init.o(.bss*)
. = ALIGN(64);
__warmboot_crt0_end__ = ABSOLUTE(.);
} >warmboot_crt0 AT>fake
.pk2ldr :
{
. = ALIGN(4096);
__pk2ldr_lma__ = LOADADDR(.pk2ldr);
__pk2ldr_start__ = ABSOLUTE(.);
KEEP (build/package2.o(.text*))
build/package2.o(.rodata*)
build/package2.o(.data*)
. = ALIGN(8);
build/package2.o(.bss*)
. = ALIGN(16);
__pk2ldr_end__ = ABSOLUTE(.);
} >pk2ldr AT>fake
.vectors :
{
. = ALIGN(2048);
__vectors_lma__ = LOADADDR(.vectors);
__vectors_start__ = ABSOLUTE(.);
KEEP (*(.vectors*))
. = ALIGN(16);
__vectors_end__ = ABSOLUTE(.);
} >evt AT>fake
.text :
{
. = ALIGN(256);
__main_lma__ = LOADADDR(.text);
__main_start__ = ABSOLUTE(.);
*(.text.unlikely .text.*_unlikely .text.unlikely.*)
*(.text.exit .text.exit.*)
*(.text.startup .text.startup.*)
*(.text.hot .text.hot.*)
*(.text .stub .text.* .gnu.linkonce.t.*)
. = ALIGN(8);
} >main AT>fake
.init :
{
KEEP( *(.init) )
@ -85,15 +107,6 @@ SECTIONS
. = ALIGN(8);
} >main AT>fake
.text :
{
*(.text.unlikely .text.*_unlikely .text.unlikely.*)
*(.text.exit .text.exit.*)
*(.text.startup .text.startup.*)
*(.text.hot .text.hot.*)
*(.text .stub .text.* .gnu.linkonce.t.*)
. = ALIGN(8);
} >main AT>fake
.fini :
{
@ -108,8 +121,8 @@ SECTIONS
. = ALIGN(8);
} >main AT>fake
.got : { __got_start__ = .; *(.got) *(.igot) } >main AT>fake
.got.plt : { *(.got.plt) *(.igot.plt) __got_end__ = .;} >main AT>fake
.got : { __got_start__ = ABSOLUTE(.); *(.got) *(.igot) } >main AT>fake
.got.plt : { *(.got.plt) *(.igot.plt) __got_end__ = ABSOLUTE(.);} >main AT>fake
.preinit_array :
{
@ -167,12 +180,12 @@ SECTIONS
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__ = .;
__bss_start__ = ABSOLUTE(.);
*(.dynbss)
*(.bss .bss.* .gnu.linkonce.b.*)
*(COMMON)
. = ALIGN(8);
__bss_end__ = .;
__bss_end__ = ABSOLUTE(.);
} >main AT>fake
@ -192,7 +205,7 @@ SECTIONS
.gnu.version_r : { *(.gnu.version_r) } >main AT>fake
.dynsym : { *(.dynsym) } >main AT>fake
.dynstr : { *(.dynstr) } >main AT>fake
.rela.dyn : { *(.rela.*) } >main AT>fake
.rela.dyn : { *(.rela.*); __main_end__ = ABSOLUTE(.);} >main AT>fake
__end__ = ABSOLUTE(.) ;

View file

@ -2,17 +2,19 @@
#include "mmu.h"
#include "memory_map.h"
/*
extern void (*__preinit_array_start[])(void);
extern void (*__preinit_array_end[])(void);
extern void (*__init_array_start[])(void);
extern void (*__init_array_end[])(void);
extern void (*const __preinit_array_start[])(void);
extern void (*const __preinit_array_end[])(void);
extern void ( *const __init_array_start[])(void);
extern void (*const __init_array_end[])(void);
extern void _init(void);
extern uint8_t __warmboot_crt0_start__[], __warmboot_crt0_end__[], __warmboot_crt0_lma__[];
extern uint8_t __main_start__[], __main_end__[], __main_lma__[];
extern uint8_t __pk2ldr_start__[], __pk2ldr_end__[], __pk2ldr_lma__[];
extern uint8_t __vectors_start__[], __vectors_end__[], __vectors_lma__[];*/
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 __vectors_start__[], __vectors_end__[], __vectors_lma__[];
extern void flush_dcache_all_tzram_pa(void);
extern void invalidate_icache_all_tzram_pa(void);
@ -102,45 +104,51 @@ static void configure_ttbls(void) {
tzram_map_all_segments(mmu_l3_tbl);
}
#if 0
static void copy_lma_to_vma(unsigned int segment_id, void *lma, size_t size, bool vma_is_pa) {
uintptr_t vma = vma_is_pa ? TZRAM_GET_SEGMENT_PA(segment_id) : TZRAM_GET_SEGMENT_ADDRESS(segment_id);
uintptr_t vma_offset = (uintptr_t)lma & 0xFFF;
__attribute__((noinline)) static void copy_lma_to_vma(const void *vma, const void *lma, size_t size) {
uint64_t *p_vma = (uint64_t *)vma;
uint64_t *p_lma = (uint64_t *)lma;
const uint64_t *p_lma = (const uint64_t *)((size_t)lma + __start_cold);
for (size_t i = 0; i < size / 8; i++) {
p_vma[vma_offset / 8 + i] = p_lma[i];
p_vma[i] = p_lma[i];
}
}
static void __libc_init_array(void) {
for (size_t i = 0; i < __preinit_array_end - __preinit_array_start; i++)
__preinit_array_start[i]();
_init(); /* FIXME: do we have this gcc-provided symbol if we build with -nostartfiles? */
for (size_t i = 0; i < __init_array_end - __init_array_start; i++)
__init_array_start[i]();
}
#endif
uintptr_t get_coldboot_crt0_stack_address(void) {
return TZRAM_GET_SEGMENT_PA(TZRAM_SEGMENT_ID_CORE3_STACK) + 0x800;
}
__attribute__((target("cmodel=large"), noinline)) static void copy_warmboot_crt0(void) {
copy_lma_to_vma(__warmboot_crt0_start__, __warmboot_crt0_lma__, __warmboot_crt0_end__ - __warmboot_crt0_start__);
}
__attribute__((target("cmodel=large"), noinline)) static void copy_other_sections(void) {
copy_lma_to_vma(__main_start__, __main_lma__, __main_end__ - __main_start__);
copy_lma_to_vma(__pk2ldr_start__, __pk2ldr_lma__, __pk2ldr_end__ - __pk2ldr_start__);
copy_lma_to_vma(__vectors_start__, __vectors_lma__, __vectors_end__ - __vectors_start__);
}
__attribute__((target("cmodel=large"), noinline)) static void __libc_init_array(void) {
/* On the stock secmon the init array lives in crt0 rodata, but whatever... */
for (size_t i = 0; i < __preinit_array_end - __preinit_array_start; i++)
__preinit_array_start[i]();
//_init(); /* FIXME: do we have this gcc-provided symbol if we build with -nostartfiles? */
for (size_t i = 0; i < __init_array_end - __init_array_start; i++)
__init_array_start[i]();
}
void coldboot_init(void) {
/* TODO: Set NX BOOTLOADER clock time field */
/*copy_lma_to_vma(TZRAM_SEGMENT_ID_WARMBOOT_CRT0_AND_MAIN, __warmboot_crt0_lma__, __warmboot_crt0_end__ - __warmboot_crt0_start__, true);*/
copy_warmboot_crt0();
/* TODO: set some mmio regs, etc. */
/* TODO: initialize DMA controllers */
configure_ttbls();
/*copy_lma_to_vma(TZRAM_SEGMENT_ID_WARMBOOT_CRT0_AND_MAIN, __main_lma__, __main_end__ - __main_start__, false);
copy_lma_to_vma(TZRAM_SEGMENT_ID_PK2LDR, __pk2ldr_lma__, __pk2ldr_end__ - __pk2ldr_start__, false);
copy_lma_to_vma(TZRAM_SEGEMENT_ID_SECMON_EVT, __vectors_lma__, __vectors_end__ - __vectors_start__, false);*/
copy_other_sections();
/* TODO: set the MMU regs & tlbi & enable MMU */
flush_dcache_all_tzram_pa();
invalidate_icache_all_tzram_pa();
/* At this point we can access the mapped segments */
/* TODO: zero-initialize the cpu context */
/* Nintendo clears the (emtpy) pk2ldr's BSS section, but we embed it 0-filled in the binary */
/*__libc_init_array(); construct global objects */
__libc_init_array(); /* construct global objects */
}