diff --git a/build.rs b/build.rs index cd7da7c..3165444 100644 --- a/build.rs +++ b/build.rs @@ -2,7 +2,7 @@ fn main() { // Tell ld to use linker script. - println!("cargo::rustc-link-arg=-Tsrc/script.ld"); + println!("cargo::rustc-link-arg=-Tsrc/script.lds"); // Don't do any magic linker stuff. println!("cargo::rustc-link-arg=--omagic"); } diff --git a/src/entry.rs b/src/entry.rs index adfa75a..50c4ee0 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,6 +1,7 @@ use crate::abort; unsafe extern "C" fn _enter() { + let id = riscv::register::mhartid::read(); write_tp(&id); // TODO: set up stack for all harts diff --git a/src/main.rs b/src/main.rs index 38d3890..3a80ade 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,7 +40,7 @@ unsafe extern "C" fn _enter() -> ! { "csrw mideleg, t5", // delegate all machine interrupts // set the stack pointer - "la sp, _init_stack_top", + "la sp, _stack_end", // Make sure machine mode is set, and enable coarse interrupts "li t0, (0b11 << 11) | (1 << 7) | (1 << 3)", diff --git a/src/script.ld b/src/script.lds similarity index 68% rename from src/script.ld rename to src/script.lds index 6513164..68b1556 100644 --- a/src/script.ld +++ b/src/script.lds @@ -17,18 +17,25 @@ SECTIONS { . = ORIGIN(ram); # start at 0x8000_0000 (DRAM) .text : { # put code first + PROVIDE(_text_start = .); *(.text.init) # start with anything in the .text.init section *(.text .text.*) # then put anything else in .text + PROVIDE(_text_end = .); } >ram AT>ram :text # put this section into the text segment PROVIDE(_global_pointer = .); # this is magic, google "linker relaxation" .rodata : { # next, read-only data + PROVIDE(_rodata_start = .); *(.rodata .rodata.*) + PROVIDE(_rodata_end = .); } >ram AT>ram :text # goes into the text segment as well (since instructions are generally read-only) .data : { # and the data section + . = ALIGN(4096); + PROVIDE(_data_start = .); *(.sdata .sdata.*) *(.data .data.*) + PROVIDE(_data_end = .); } >ram AT>ram :data # this will go into the data segment .bss :{ # finally, the BSS @@ -37,10 +44,15 @@ SECTIONS { PROVIDE(_bss_end = .); # ... and one at the end } >ram AT>ram :bss # and this goes into the bss segment - . = ALIGN(16); # our stack needs to be 16-byte aligned, per the C calling convention - PROVIDE(_init_stack_top = . + 0x1000); # reserve 0x1000 bytes (4 kB) for the initialisation stack + PROVIDE(_memory_start = ORIGIN(ram)); - PROVIDE(_kernel_heap_bottom = _init_stack_top); # allocate heap to remaining physical memory + . = ALIGN(16); # stack is 16-byte aligned, per the C calling convention + PROVIDE(_stack_start = .); + PROVIDE(_stack_end = _stack_start + 0x80000); # reserve 0x80000 bytes for the stack + + PROVIDE(_memory_end = ORIGIN(ram) + LENGTH(ram)); + + PROVIDE(_kernel_heap_bottom = _stack_end); # allocate heap to remaining physical memory PROVIDE(_kernel_heap_top = ORIGIN(ram) + LENGTH(ram)); # top of heap is end of ram PROVIDE(_kernel_heap_size = _kernel_heap_top - _kernel_heap_bottom); # capture size of heap } \ No newline at end of file