1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-18 16:32:05 +00:00

thermosphere: rework linkscrips, use discardable sections, better sp pivot on crash

This commit is contained in:
TuxSH 2019-08-02 00:47:48 +02:00
parent e6c5eb3928
commit e0339049b3
9 changed files with 92 additions and 56 deletions

View file

@ -24,12 +24,19 @@ export PLATFORM := qemu
PLATFORM_SOURCES := src/platform/qemu PLATFORM_SOURCES := src/platform/qemu
PLATFORM_DEFINES := -DPLATFORM_QEMU PLATFORM_DEFINES := -DPLATFORM_QEMU
else ifeq ($(PLATFORM), tegra-t210-arm-tf)
export PLATFORM := tegra-t210-arm-tf
PLATFORM_SOURCES := src/platform/tegra
PLATFORM_DEFINES := -DPLATFORM_TEGRA -DPLATFORM_TEGRA_T210_ARM_TF
else else
export PLATFORM := tegra export PLATFORM := tegra
PLATFORM_SOURCES := src/platform/tegra PLATFORM_SOURCES := src/platform/tegra
PLATFORM_DEFINES := -DPLATFORM_TEGRA PLATFORM_DEFINES := -DPLATFORM_TEGRA -D DPLATFORM_TEGRA_T210_NINTENDO
endif endif

View file

@ -1,6 +1,11 @@
OUTPUT_ARCH(aarch64) OUTPUT_ARCH(aarch64)
ENTRY(_start) ENTRY(_start)
PHDRS
{
main PT_LOAD;
}
SECTIONS SECTIONS
{ {
PROVIDE(__start__ = ORIGIN(main)); PROVIDE(__start__ = ORIGIN(main));
@ -19,37 +24,37 @@ SECTIONS
__vectors_start__ = ABSOLUTE(.); __vectors_start__ = ABSOLUTE(.);
KEEP(*(.vectors*)); KEEP(*(.vectors*));
. = ALIGN(8); . = ALIGN(8);
} >main } >main :main
.init : .init :
{ {
KEEP( *(.init) ) KEEP( *(.init) )
. = ALIGN(8); . = ALIGN(8);
} >main } >main :main
.plt : .plt :
{ {
*(.plt) *(.plt)
*(.iplt) *(.iplt)
. = ALIGN(8); . = ALIGN(8);
} >main } >main :main
.fini : .fini :
{ {
KEEP( *(.fini) ) KEEP( *(.fini) )
. = ALIGN(8); . = ALIGN(8);
} >main } >main :main
.rodata : .rodata :
{ {
*(.rodata .rodata.* .gnu.linkonce.r.*) *(.rodata .rodata.* .gnu.linkonce.r.*)
SORT(CONSTRUCTORS) SORT(CONSTRUCTORS)
. = ALIGN(8); . = ALIGN(8);
} >main } >main :main
.got : { __got_start__ = ABSOLUTE(.); *(.got) *(.igot) } >main .got : { __got_start__ = ABSOLUTE(.); *(.got) *(.igot) } >main :main
.got.plt : { *(.got.plt) *(.igot.plt) __got_end__ = ABSOLUTE(.);} >main .got.plt : { *(.got.plt) *(.igot.plt) __got_end__ = ABSOLUTE(.);} >main :main
.preinit_array : .preinit_array :
{ {
@ -58,7 +63,7 @@ SECTIONS
KEEP (*(.preinit_array)) KEEP (*(.preinit_array))
PROVIDE (__preinit_array_end = ABSOLUTE(.)); PROVIDE (__preinit_array_end = ABSOLUTE(.));
. = ALIGN(8); . = ALIGN(8);
} >main } >main :main
.init_array : .init_array :
{ {
@ -66,7 +71,7 @@ SECTIONS
KEEP (*(SORT(.init_array.*))) KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array)) KEEP (*(.init_array))
PROVIDE (__init_array_end = ABSOLUTE(.)); PROVIDE (__init_array_end = ABSOLUTE(.));
} >main } >main :main
.fini_array : .fini_array :
{ {
@ -76,7 +81,7 @@ SECTIONS
KEEP (*(SORT(.fini_array.*))) KEEP (*(SORT(.fini_array.*)))
PROVIDE (__fini_array_end = ABSOLUTE(.)); PROVIDE (__fini_array_end = ABSOLUTE(.));
. = ALIGN(8); . = ALIGN(8);
} >main } >main :main
.ctors : .ctors :
{ {
@ -86,7 +91,7 @@ SECTIONS
KEEP (*(SORT(.ctors.*))) KEEP (*(SORT(.ctors.*)))
KEEP (*(.ctors)) KEEP (*(.ctors))
. = ALIGN(8); . = ALIGN(8);
} >main } >main :main
.dtors ALIGN(8) : .dtors ALIGN(8) :
{ {
@ -96,35 +101,35 @@ SECTIONS
KEEP (*(SORT(.dtors.*))) KEEP (*(SORT(.dtors.*)))
KEEP (*(.dtors)) KEEP (*(.dtors))
. = ALIGN(8); . = ALIGN(8);
} >main } >main :main
.data ALIGN(8) : .data ALIGN(8) :
{ {
*(.data .data.* .gnu.linkonce.d.*) *(.data .data.* .gnu.linkonce.d.*)
CONSTRUCTORS CONSTRUCTORS
. = ALIGN(8); . = ALIGN(8);
} >main } >main :main
.eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } >main .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } >main :main
.eh_frame : { KEEP (*(.eh_frame)) *(.eh_frame.*) } >main .eh_frame : { KEEP (*(.eh_frame)) *(.eh_frame.*) } >main :main
.gcc_except_table : { *(.gcc_except_table .gcc_except_table.*) } >main .gcc_except_table : { *(.gcc_except_table .gcc_except_table.*) } >main :main
.gnu_extab : { *(.gnu_extab*) } >main .gnu_extab : { *(.gnu_extab*) } >main :main
.exception_ranges : { *(.exception_ranges .exception_ranges*) } >main .exception_ranges : { *(.exception_ranges .exception_ranges*) } >main :main
.dynamic : { *(.dynamic) } >main .dynamic : { *(.dynamic) } >main :main
.interp : { *(.interp) } >main .interp : { *(.interp) } >main :main
.note.gnu.build-id : { *(.note.gnu.build-id) } >main .note.gnu.build-id : { *(.note.gnu.build-id) } >main :main
.hash : { *(.hash) } >main .hash : { *(.hash) } >main :main
.gnu.hash : { *(.gnu.hash) } >main .gnu.hash : { *(.gnu.hash) } >main :main
.gnu.version : { *(.gnu.version) } >main .gnu.version : { *(.gnu.version) } >main :main
.gnu.version_d : { *(.gnu.version_d) } >main .gnu.version_d : { *(.gnu.version_d) } >main :main
.gnu.version_r : { *(.gnu.version_r) } >main .gnu.version_r : { *(.gnu.version_r) } >main :main
.dynsym : { *(.dynsym) } >main .dynsym : { *(.dynsym) } >main :main
.dynstr : { *(.dynstr) } >main .dynstr : { *(.dynstr) } >main :main
.rela.dyn : { *(.rela.*); __main_end__ = ABSOLUTE(.);} >main .rela.dyn : { *(.rela.*); __main_end__ = ABSOLUTE(.);} >main :main
.bss : .bss (NOLOAD) :
{ {
. = ALIGN(8); . = ALIGN(8);
__bss_start__ = ABSOLUTE(.); __bss_start__ = ABSOLUTE(.);
@ -133,13 +138,22 @@ SECTIONS
*(COMMON) *(COMMON)
. = ALIGN(8); . = ALIGN(8);
__end__ = ABSOLUTE(.); __end__ = ABSOLUTE(.);
} >main } >main :NONE
. = ALIGN(0x1000); . = ALIGN(0x1000);
__end__ = ABSOLUTE(.) ; __end__ = ABSOLUTE(.) ;
__stacks_top__ = ABSOLUTE(. + 0x1000); .temp (NOLOAD) :
__excep_stacks_top__ = ABSOLUTE(. + 0x2000); /* Note: potentially overwrites warmboot firmware. */ {
. = ALIGN(0x1000);
__stacks_top__ = ABSOLUTE(. + 0x2000);
__crash_stacks_top__ = ABSOLUTE(. + 0x3000);
. += 0x3000;
*(.temp.*)
. = ALIGN(0x1000);
} >temp :NONE
. = ALIGN(8); . = ALIGN(8);

View file

@ -1,5 +1,6 @@
MEMORY MEMORY
{ {
NULL : ORIGIN = 0, LENGTH = 0x1000 NULL : ORIGIN = 0, LENGTH = 0x1000
main : ORIGIN = 0x60000000, LENGTH = 128M /* QEMU's memory map changes dynamically? */ main : ORIGIN = 0x60000000, LENGTH = 64M /* QEMU's memory map changes dynamically? */
} temp : ORIGIN = 0x64000000, LENGTH = 64M
}

View file

@ -19,6 +19,8 @@
// start.s // start.s
extern uintptr_t g_initialKernelEntrypoint; extern uintptr_t g_initialKernelEntrypoint;
extern u8 __stacks_top__[], __crash_stacks_top__[];
// Prevents it from being put in BSS // Prevents it from being put in BSS
CoreCtx g_coreCtxs[4] = { CoreCtx g_coreCtxs[4] = {
{ .coreId = 0 }, { .coreId = 0 },
@ -29,10 +31,12 @@ CoreCtx g_coreCtxs[4] = {
void coreCtxInit(u32 coreId, bool isColdbootCore, u64 argument) void coreCtxInit(u32 coreId, bool isColdbootCore, u64 argument)
{ {
size_t crashStackSize = (__crash_stacks_top__ - __stacks_top__) / 4;
currentCoreCtx = &g_coreCtxs[coreId]; currentCoreCtx = &g_coreCtxs[coreId];
currentCoreCtx->isColdbootCore = isColdbootCore; currentCoreCtx->isColdbootCore = isColdbootCore;
currentCoreCtx->kernelArgument = argument; currentCoreCtx->kernelArgument = argument;
if (isColdbootCore) { currentCoreCtx->crashStack = __crash_stacks_top__ - crashStackSize * coreId;
if (isColdbootCore && currentCoreCtx->kernelEntrypoint == 0) {
currentCoreCtx->kernelEntrypoint = g_initialKernelEntrypoint; currentCoreCtx->kernelEntrypoint = g_initialKernelEntrypoint;
} }
} }

View file

@ -18,10 +18,12 @@
#include "utils.h" #include "utils.h"
typedef struct CoreCtx { typedef struct CoreCtx {
u64 kernelArgument; u64 kernelArgument; // @0x00
uintptr_t kernelEntrypoint; uintptr_t kernelEntrypoint; // @0x08
u32 coreId; // @0x10 u8 *crashStack; // @0x10
bool isColdbootCore; // @0x14 u64 scratch; // @0x18
u32 coreId; // @0x20
bool isColdbootCore; // @0x24
} CoreCtx; } CoreCtx;
extern CoreCtx g_coreCtxs[4]; extern CoreCtx g_coreCtxs[4];

View file

@ -63,17 +63,14 @@
.endm .endm
.macro pivot_stack_for_crash .macro pivot_stack_for_crash
// Note: reset x18 assumed uncorrupted // Note: x18 assumed uncorrupted
// Note: replace sp_el0 with crashing sp // Note: replace sp_el0 with crashing sp
mrs x18, esr_el2 str x16, [x18, #0x18] // currentCoreCtx->scratch = x16
mov x18, sp mov x16, sp
msr sp_el0, x18 msr sp_el0, x16
bic x18, x18, #0xFF ldr x16, [x18, #0x10] // currentCoreCtx->crashStack
bic x18, x18, #0x300 mov sp, x16
add x18, x18, #0x400 ldr x16, [x18, #0x18]
mov sp, x18
ldp x18, xzr, [sp, #-0x10]
add sp, sp, #0x1000
.endm .endm
/* Actual Vectors for Thermosphere. */ /* Actual Vectors for Thermosphere. */

View file

@ -0,0 +1,8 @@
MEMORY
{
NULL : ORIGIN = 0, LENGTH = 0x1000
main : ORIGIN = 0x80000000, LENGTH = 0xD000
/* This area is overwritten by the secure monitor when preparing for deep sleep. */
temp : ORIGIN = 0xFF800000, LENGTH = 0x400000
}

View file

@ -0,0 +1,8 @@
MEMORY
{
NULL : ORIGIN = 0, LENGTH = 0x1000
main : ORIGIN = 0x80000000, LENGTH = 0xD000
/* This area is overwritten by the secure monitor when preparing for deep sleep. */
temp : ORIGIN = 0x8000F000, LENGTH = 0x11000
}

View file

@ -1,5 +0,0 @@
MEMORY
{
NULL : ORIGIN = 0, LENGTH = 0x1000
main : ORIGIN = 0x80000000, LENGTH = 0xD000 - 0x2000 /* 0x2000 for stacks. */
}