diff --git a/boards/layout_apollo3.ld b/boards/layout_apollo3.ld index 95fde335..8713c278 100644 --- a/boards/layout_apollo3.ld +++ b/boards/layout_apollo3.ld @@ -6,12 +6,6 @@ MEMORY { SRAM (rwx) : ORIGIN = 0x10002000, LENGTH = 0x2000 } -/* - * Any change to STACK_SIZE should be accompanied by a corresponding change to - * `elf2tab`'s `--stack` option - */ -STACK_SIZE = 2048; - MPU_MIN_ALIGN = 8K; INCLUDE layout_generic.ld diff --git a/boards/layout_hail.ld b/boards/layout_hail.ld index f5473eea..179a8831 100644 --- a/boards/layout_hail.ld +++ b/boards/layout_hail.ld @@ -6,12 +6,6 @@ MEMORY { SRAM (rwx) : ORIGIN = 0x20004000, LENGTH = 62K } -/* - * Any change to STACK_SIZE should be accompanied by a corresponding change to - * `elf2tab`'s `--stack` option - */ -STACK_SIZE = 2048; - MPU_MIN_ALIGN = 8K; INCLUDE layout_generic.ld diff --git a/boards/layout_hifive1.ld b/boards/layout_hifive1.ld index 2e085809..20294ce7 100644 --- a/boards/layout_hifive1.ld +++ b/boards/layout_hifive1.ld @@ -13,12 +13,6 @@ MEMORY { SRAM (rwx) : ORIGIN = 0x80002400, LENGTH = 0x1C00 } -/* - * Any change to STACK_SIZE should be accompanied by a corresponding change to - * `elf2tab`'s `--stack` option - */ -STACK_SIZE = 2048; - MPU_MIN_ALIGN = 1K; INCLUDE layout_generic.ld diff --git a/boards/layout_imxrt1050.ld b/boards/layout_imxrt1050.ld index 56ecf11f..3e7f904f 100644 --- a/boards/layout_imxrt1050.ld +++ b/boards/layout_imxrt1050.ld @@ -6,12 +6,6 @@ MEMORY { SRAM (rwx) : ORIGIN = 0x20004000, LENGTH = 112K } -/* - * Any change to STACK_SIZE should be accompanied by a corresponding change to - * `elf2tab`'s `--stack` option - */ -STACK_SIZE = 2048; - MPU_MIN_ALIGN = 8K; INCLUDE layout_generic.ld diff --git a/boards/layout_msp432.ld b/boards/layout_msp432.ld index 2262a155..2c9d2a4c 100644 --- a/boards/layout_msp432.ld +++ b/boards/layout_msp432.ld @@ -4,12 +4,6 @@ MEMORY { SRAM (rwx) : ORIGIN = 0x20004000, LENGTH = 0x2000 } -/* - * Any change to STACK_SIZE should be accompanied by a corresponding change to - * `elf2tab`'s `--stack` option - */ -STACK_SIZE = 2048; - MPU_MIN_ALIGN = 8K; INCLUDE layout_generic.ld diff --git a/boards/layout_nrf52.ld b/boards/layout_nrf52.ld index 40cdb10e..942e86b3 100644 --- a/boards/layout_nrf52.ld +++ b/boards/layout_nrf52.ld @@ -6,12 +6,6 @@ MEMORY { SRAM (rwx) : ORIGIN = 0x20004000, LENGTH = 62K } -/* - * Any change to STACK_SIZE should be accompanied by a corresponding change to - * `elf2tab`'s `--stack` option - */ -STACK_SIZE = 2048; - MPU_MIN_ALIGN = 8K; INCLUDE layout_generic.ld diff --git a/boards/layout_nrf52840.ld b/boards/layout_nrf52840.ld index d99efa02..31bf3468 100644 --- a/boards/layout_nrf52840.ld +++ b/boards/layout_nrf52840.ld @@ -6,12 +6,6 @@ MEMORY { SRAM (rwx) : ORIGIN = 0x20004000, LENGTH = 62K } -/* - * Any change to STACK_SIZE should be accompanied by a corresponding change to - * `elf2tab`'s `--stack` option - */ -STACK_SIZE = 2048; - MPU_MIN_ALIGN = 8K; INCLUDE layout_generic.ld diff --git a/boards/layout_nucleo_f429zi.ld b/boards/layout_nucleo_f429zi.ld index 8261e9cd..3e407b42 100644 --- a/boards/layout_nucleo_f429zi.ld +++ b/boards/layout_nucleo_f429zi.ld @@ -6,12 +6,6 @@ MEMORY { SRAM (rwx) : ORIGIN = 0x20004000, LENGTH = 112K } -/* - * Any change to STACK_SIZE should be accompanied by a corresponding change to - * `elf2tab`'s `--stack` option - */ -STACK_SIZE = 2048; - MPU_MIN_ALIGN = 8K; INCLUDE layout_generic.ld diff --git a/boards/layout_nucleo_f446re.ld b/boards/layout_nucleo_f446re.ld index 6536b308..83e698b1 100644 --- a/boards/layout_nucleo_f446re.ld +++ b/boards/layout_nucleo_f446re.ld @@ -6,12 +6,6 @@ MEMORY { SRAM (rwx) : ORIGIN = 0x20004000, LENGTH = 176K } -/* - * Any change to STACK_SIZE should be accompanied by a corresponding change to - * `elf2tab`'s `--stack` option - */ -STACK_SIZE = 2048; - MPU_MIN_ALIGN = 8K; INCLUDE layout_generic.ld diff --git a/boards/layout_opentitan.ld b/boards/layout_opentitan.ld index bf15b4db..ada781ce 100644 --- a/boards/layout_opentitan.ld +++ b/boards/layout_opentitan.ld @@ -13,12 +13,6 @@ MEMORY { SRAM (rwx) : ORIGIN = 0x10004000, LENGTH = 512K } -/* - * Any change to STACK_SIZE should be accompanied by a corresponding change to - * `elf2tab`'s `--stack` option - */ -STACK_SIZE = 2048; - MPU_MIN_ALIGN = 1K; INCLUDE layout_generic.ld diff --git a/core/examples/empty_main.rs b/core/examples/empty_main.rs index 1179b434..b7d91698 100644 --- a/core/examples/empty_main.rs +++ b/core/examples/empty_main.rs @@ -4,6 +4,11 @@ #![no_std] +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x400] = [0; 0x400]; + // If you don't *use* anything from libtock_core directly, cargo will not link // it into the executable. However, we still need the runtime and lang items. // Therefore a libtock_core app that doesn't directly mention anything in diff --git a/examples-features/alloc_error.rs b/examples-features/alloc_error.rs index 8b20f84a..8c982cbb 100644 --- a/examples-features/alloc_error.rs +++ b/examples-features/alloc_error.rs @@ -11,6 +11,11 @@ use libtock::println; use libtock::result::TockResult; use libtock::syscalls; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] fn main() -> TockResult<()> { let mut vec = Vec::new(); diff --git a/examples-features/ble_scanning.rs b/examples-features/ble_scanning.rs index c6a68736..5bbe5969 100644 --- a/examples-features/ble_scanning.rs +++ b/examples-features/ble_scanning.rs @@ -5,6 +5,11 @@ use libtock::result::TockResult; use libtock::simple_ble; use serde::Deserialize; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[derive(Deserialize)] struct LedCommand { pub nr: u8, diff --git a/examples-features/ctap.rs b/examples-features/ctap.rs index 3a66efff..138bc54c 100644 --- a/examples-features/ctap.rs +++ b/examples-features/ctap.rs @@ -4,6 +4,11 @@ #![no_std] #![feature(alloc_error_handler)] +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x5000] = [0; 0x5000]; + extern crate alloc; use core::alloc::Layout; diff --git a/examples-features/libtock_test.rs b/examples-features/libtock_test.rs index a938f467..99c49318 100644 --- a/examples-features/libtock_test.rs +++ b/examples-features/libtock_test.rs @@ -22,6 +22,11 @@ use libtock::result::TockResult; use libtock::timer::DriverContext; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/examples-features/panic.rs b/examples-features/panic.rs index 8af98bf3..543a395b 100644 --- a/examples-features/panic.rs +++ b/examples-features/panic.rs @@ -7,6 +7,11 @@ use libtock::println; use libtock::result::TockResult; use libtock::syscalls; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] async fn main() -> TockResult<()> { panic!("Bye world!"); diff --git a/examples-features/simple_ble.rs b/examples-features/simple_ble.rs index 8dde3095..378ba723 100644 --- a/examples-features/simple_ble.rs +++ b/examples-features/simple_ble.rs @@ -7,6 +7,11 @@ use libtock::simple_ble::BleAdvertisingDriver; use libtock::timer::Duration; use serde::Serialize; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[derive(Serialize)] struct LedCommand { pub nr: u8, diff --git a/examples/adc.rs b/examples/adc.rs index 28b15349..b4279701 100644 --- a/examples/adc.rs +++ b/examples/adc.rs @@ -4,6 +4,11 @@ use libtock::println; use libtock::result::TockResult; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/examples/adc_buffer.rs b/examples/adc_buffer.rs index 1337cf44..9d04d110 100644 --- a/examples/adc_buffer.rs +++ b/examples/adc_buffer.rs @@ -5,6 +5,11 @@ use libtock::println; use libtock::result::TockResult; use libtock::syscalls; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] /// Reads a 128 byte sample into a buffer and prints the first value to the console. async fn main() -> TockResult<()> { diff --git a/examples/blink.rs b/examples/blink.rs index fd3b1e5b..2891c52f 100644 --- a/examples/blink.rs +++ b/examples/blink.rs @@ -3,6 +3,11 @@ use libtock::result::TockResult; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x400] = [0; 0x400]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/examples/blink_random.rs b/examples/blink_random.rs index 93a263d4..44c6bbe0 100644 --- a/examples/blink_random.rs +++ b/examples/blink_random.rs @@ -4,6 +4,11 @@ use libtock::leds::LedsDriver; use libtock::result::TockResult; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x400] = [0; 0x400]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/examples/button_leds.rs b/examples/button_leds.rs index 4a48f2f6..a8035a7c 100644 --- a/examples/button_leds.rs +++ b/examples/button_leds.rs @@ -4,6 +4,11 @@ use futures::future; use libtock::buttons::ButtonState; use libtock::result::TockResult; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/examples/button_read.rs b/examples/button_read.rs index e888185d..c1512f04 100644 --- a/examples/button_read.rs +++ b/examples/button_read.rs @@ -4,6 +4,11 @@ use libtock::println; use libtock::result::TockResult; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/examples/button_subscribe.rs b/examples/button_subscribe.rs index 8b2f17ed..d2efea04 100644 --- a/examples/button_subscribe.rs +++ b/examples/button_subscribe.rs @@ -6,6 +6,11 @@ use libtock::println; use libtock::result::TockResult; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/examples/ctap.rs b/examples/ctap.rs index d545d0c5..34c0943f 100644 --- a/examples/ctap.rs +++ b/examples/ctap.rs @@ -6,6 +6,11 @@ use libtock::result::TockResult; use libtock::syscalls; use libtock::{print, println}; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/examples/gpio.rs b/examples/gpio.rs index 9241e744..96c0b9cd 100644 --- a/examples/gpio.rs +++ b/examples/gpio.rs @@ -3,6 +3,11 @@ use libtock::result::TockResult; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + // Example works on P0.03 #[libtock::main] async fn main() -> TockResult<()> { diff --git a/examples/gpio_read.rs b/examples/gpio_read.rs index 8fc216b7..1cf432ee 100644 --- a/examples/gpio_read.rs +++ b/examples/gpio_read.rs @@ -5,6 +5,11 @@ use libtock::println; use libtock::result::TockResult; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + // example works on p0.03 #[libtock::main] async fn main() -> TockResult<()> { diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 49ada785..5ca45ce7 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -7,6 +7,11 @@ use libtock::println; use libtock::result::TockResult; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x400] = [0; 0x400]; + #[libtock::main] async fn main() -> TockResult<()> { let drivers = libtock::retrieve_drivers()?; diff --git a/examples/hmac.rs b/examples/hmac.rs index fe956bbc..8d1f9d3a 100644 --- a/examples/hmac.rs +++ b/examples/hmac.rs @@ -5,6 +5,11 @@ use libtock::println; use libtock::result::TockResult; use libtock::syscalls; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/examples/sensors.rs b/examples/sensors.rs index 5196cd19..1e70e04d 100644 --- a/examples/sensors.rs +++ b/examples/sensors.rs @@ -5,6 +5,11 @@ use libtock::result::TockResult; use libtock::sensors::Sensor; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/examples/seven_segment.rs b/examples/seven_segment.rs index 88b0700d..2363ea44 100644 --- a/examples/seven_segment.rs +++ b/examples/seven_segment.rs @@ -4,6 +4,11 @@ use libtock::electronics::ShiftRegister; use libtock::result::TockResult; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + fn number_to_bits(n: u8) -> [bool; 8] { match n { 1 => [false, false, false, true, false, true, false, false], diff --git a/examples/temperature.rs b/examples/temperature.rs index 6298cb43..fd347e7b 100644 --- a/examples/temperature.rs +++ b/examples/temperature.rs @@ -4,6 +4,11 @@ use libtock::println; use libtock::result::TockResult; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/examples/timer.rs b/examples/timer.rs index 2277e6c2..fffdf066 100644 --- a/examples/timer.rs +++ b/examples/timer.rs @@ -8,6 +8,11 @@ use libtock::result::TockResult; use libtock::timer::DriverContext; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + const DELAY_MS: usize = 500; #[libtock::main] diff --git a/examples/timer_parallel.rs b/examples/timer_parallel.rs index 66ddac2a..c058ec82 100644 --- a/examples/timer_parallel.rs +++ b/examples/timer_parallel.rs @@ -6,6 +6,11 @@ use libtock::result::TockResult; use libtock::timer::Duration; use libtock::timer::ParallelSleepDriver; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + async fn blink( timer_driver: &ParallelSleepDriver<'_>, duration: Duration, diff --git a/examples/timer_subscribe.rs b/examples/timer_subscribe.rs index 5a7ad4ba..a7b832e9 100644 --- a/examples/timer_subscribe.rs +++ b/examples/timer_subscribe.rs @@ -5,6 +5,11 @@ use libtock::println; use libtock::result::TockResult; use libtock::timer::Duration; +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x800] = [0; 0x800]; + #[libtock::main] async fn main() -> TockResult<()> { let mut drivers = libtock::retrieve_drivers()?; diff --git a/layout_generic.ld b/layout_generic.ld index 3c5065cc..5386576e 100644 --- a/layout_generic.ld +++ b/layout_generic.ld @@ -14,7 +14,6 @@ * FLASH (rx) : ORIGIN = 0x10030, LENGTH = 0x0FFD0 * SRAM (RWX) : ORIGIN = 0x20000, LENGTH = 0x10000 * } - * STACK_SIZE = 2048; * MPU_MIN_ALIGN = 8K; * INCLUDE ../libtock-rs/layout.ld */ @@ -64,7 +63,7 @@ SECTIONS { * .rel.data section */ LONG(LOADADDR(.endflash) - _beginning); /* The size of the stack requested by this application */ - LONG(STACK_SIZE); + LONG(_stack_top_aligned - _sstack); /* Pad the header out to a multiple of 32 bytes so there is not a gap * between the header and subsequent .data section. It's unclear why, * but LLD is aligning sections to a multiple of 32 bytes. */ @@ -96,12 +95,11 @@ SECTIONS { * TBF header if needed. */ _sram_origin = .; - - . = . + STACK_SIZE; - - _stack_top_unaligned = .; + _sstack = .; + KEEP(*(.stack_buffer)) + _stack_top_unaligned = .; . = ALIGN(8); - _stack_top_aligned = .; + _stack_top_aligned = .; } > SRAM /* Data section, static initialized variables @@ -165,4 +163,4 @@ SECTIONS { } ASSERT((_stack_top_aligned - _stack_top_unaligned) == 0, " -STACK_SIZE must be 8 byte multiple") +the stack size must be 8 byte multiple") diff --git a/tock b/tock index c5b7a4f2..9f408649 160000 --- a/tock +++ b/tock @@ -1 +1 @@ -Subproject commit c5b7a4f2c89a8c067f0f5786788f4037b32329fd +Subproject commit 9f408649ce25f46f0dd49dae7f1a0a9849cae2af diff --git a/tools/flash.sh b/tools/flash.sh index 2ac28679..48bf4866 100755 --- a/tools/flash.sh +++ b/tools/flash.sh @@ -68,7 +68,9 @@ tab_file_name="${libtock_target_path}.tab" mkdir -p "${libtock_target_path}" cp "$1" "${elf_file_name}" -elf2tab -n "${artifact}" -o "${tab_file_name}" "${elf_file_name}" --stack 2048 --app-heap $APP_HEAP_SIZE --kernel-heap $KERNEL_HEAP_SIZE --protected-region-size=64 +STACK_SIZE=$(nm --print-size --size-sort --radix=d "${elf_file_name}" | grep STACK_MEMORY | cut -d " " -f 2) + +elf2tab -n "${artifact}" -o "${tab_file_name}" "${elf_file_name}" --stack ${STACK_SIZE} --app-heap $APP_HEAP_SIZE --kernel-heap $KERNEL_HEAP_SIZE --protected-region-size=64 if [ $tockload == "n" ]; then echo "Skipping flashing for platform \"${PLATFORM}\""