Unverified Commit ae3308cb authored by Jonathan Schwender's avatar Jonathan Schwender
Browse files

aarch64: Use Linker script to set Stack location

Parts of this commit (especially link.ld) are more or less
directly taken from https://github.com/rust-embedded/rust-raspberrypi-OS-tutorials/blob/master/16_virtual_mem_part4_higher_half_kernel/src/bsp/raspberrypi/link.ld


(MIT License).
I'm not sure if we want to (or already) do virtual addressing in the loader, so
I'm sticking to the physical part for now.

Anyway this seems to fix the problem I had, where the kernel_start and kernel_end
_addresses_ seemingly changed at runtime!
Signed-off-by: default avatarJonathan Schwender <schwenderjonathan@gmail.com>
parent e9165c2e
......@@ -11,12 +11,8 @@ extern "C" {
fn loader_main();
}
const BOOT_STACK_SIZE: usize = 4096;
const BOOT_CORE_ID: u64 = 0; // ID of CPU for booting on SMP systems - this might be board specific in the future
#[link_section = ".data"]
static STACK: [u8; BOOT_STACK_SIZE] = [0; BOOT_STACK_SIZE];
/*
* Memory types available.
*/
......@@ -57,10 +53,6 @@ global_asm!(include_str!("entry.s"));
#[inline(never)]
#[no_mangle]
pub unsafe fn _start_rust() -> ! {
// Pointer to stack base
llvm_asm!("mov sp, $0"
:: "r"(&STACK[BOOT_STACK_SIZE - 0x10] as *const u8 as usize)
:: "volatile");
pre_init()
}
......
......@@ -2,6 +2,19 @@
.equ _core_id_mask, 0b11 //Assume 4 core raspi3
// Load the address of a symbol into a register, PC-relative.
//
// The symbol must lie within +/- 4 GiB of the Program Counter.
//
// # Resources
//
// - https://sourceware.org/binutils/docs-2.36/as/AArch64_002dRelocations.html
.macro ADR_REL register, symbol
adrp \register, \symbol
add \register, \register, #:lo12:\symbol
.endm
.section .text._start
_start:
......@@ -13,7 +26,12 @@ _start:
b.ne 1f
// If execution reaches here, it is the boot core. Now, prepare the jump to Rust code.
// This loads the physical address of the Stack end. For details see
// https://github.com/rust-embedded/rust-raspberrypi-OS-tutorials/blob/master/16_virtual_mem_part4_higher_half_kernel/src/bsp/raspberrypi/link.ld
ADR_REL x4, __boot_core_stack_end_exclusive
mov sp, x4
// Jump to Rust code.
b _start_rust
......
/* Parts of this linker script are directly taken from Andre Richters Project:
* https://github.com/rust-embedded/rust-raspberrypi-OS-tutorials/blob/master/16_virtual_mem_part4_higher_half_kernel/src/bsp/raspberrypi/link.ld
*/
OUTPUT_FORMAT("elf64-littleaarch64")
OUTPUT_ARCH("aarch64")
ENTRY(_start)
......@@ -5,27 +9,56 @@ ENTRY(_start)
/* start address of the RAM, below belongs to the flash */
phys = 0x00080000;
PHDRS
{
segment_rx PT_LOAD FLAGS(5); /* 5 == RX */
segment_rw PT_LOAD FLAGS(6); /* 6 == RW */
}
SECTIONS
{
. = phys;
kernel_start = .;
.text ALIGN(4096) : AT(ADDR(.text)) {
*(.text)
*(.text.*)
}
.rodata ALIGN(4096) : AT(ADDR(.rodata)) {
*(.rodata)
*(.rodata.*)
}
.data ALIGN(4096) : AT(ADDR(.data)) {
*(.data)
*(.data.*)
}
.bss ALIGN(4096) : AT(ADDR(.bss)) {
__rx_start = .;
.text ALIGN(4096) : AT(phys) {
KEEP(*(.text._start))
*(.text._start_arguments) /* Constants (or statics in Rust speak) read by _start(). */
*(.text._start_rust) /* The Rust entry point */
*(.text*) /* Everything else */
} :segment_rx
.rodata : ALIGN(8) { *(.rodata*) } :segment_rx
.got : ALIGN(8) { *(.got) } :segment_rx /* Global offset table Todo - do we use this?*/
. = ALIGN(64K); /* Align to page boundary */
__rx_end_exclusive = .;
__rw_start = .;
.data : { *(.data*) } :segment_rw
.bss : ALIGN(8) {
bss_start = .;
*(.bss)
*(.bss.*)
}
bss_end = .;
*(.bss*);
. = ALIGN(8);
. += 8;
bss_end = .;
} :NONE
. = ALIGN(64K); /* Align to page boundary */
__rw_end_exclusive = .;
/***********************************************************************************************
* Guard Page between boot core stack and data
***********************************************************************************************/
__boot_core_stack_guard_page_start = .;
. += 64K;
__boot_core_stack_guard_page_end_exclusive = .;
/***********************************************************************************************
* Boot Core Stack
***********************************************************************************************/
__boot_core_stack_start = .; /* ^ */
/* | stack */
. += 512K; /* | growth */
/* | direction */
__boot_core_stack_end_exclusive = .; /* | */
kernel_end = .;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment