diff --git a/README.md b/README.md index a2bad2bd..eb14cc07 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,14 @@ This script does the following steps for you: - create a TAB (tock application bundle) - if you have a J-Link compatible board connected: flash this TAB to your board (using tockloader) +## Configuration of Memory Sizes + +The configuration of different memory parameters can be configured using +environment variables: + +- `APP_HEAP_SIZE` sets the size of the app heap and defaults to 1024. +- `APP_STACK_SIZE` sets the size of the app stack and default to 2048. +- `KERNEL_HEAP_SIZE` sets the size of the kernel heap and default to 1024. ## License diff --git a/boards/layout_hail.ld b/boards/layout_hail.ld.handlebars similarity index 93% rename from boards/layout_hail.ld rename to boards/layout_hail.ld.handlebars index f5473eea..37ddb066 100644 --- a/boards/layout_hail.ld +++ b/boards/layout_hail.ld.handlebars @@ -10,7 +10,7 @@ MEMORY { * Any change to STACK_SIZE should be accompanied by a corresponding change to * `elf2tab`'s `--stack` option */ -STACK_SIZE = 2048; +STACK_SIZE = {{stack_size}}; MPU_MIN_ALIGN = 8K; diff --git a/boards/layout_hifive1.ld b/boards/layout_hifive1.ld.handlebars similarity index 95% rename from boards/layout_hifive1.ld rename to boards/layout_hifive1.ld.handlebars index 2e085809..8806126c 100644 --- a/boards/layout_hifive1.ld +++ b/boards/layout_hifive1.ld.handlebars @@ -17,7 +17,7 @@ MEMORY { * Any change to STACK_SIZE should be accompanied by a corresponding change to * `elf2tab`'s `--stack` option */ -STACK_SIZE = 2048; +STACK_SIZE = {{stack_size}}; MPU_MIN_ALIGN = 1K; diff --git a/boards/layout_nrf52.ld b/boards/layout_nrf52.ld.handlebars similarity index 93% rename from boards/layout_nrf52.ld rename to boards/layout_nrf52.ld.handlebars index 419a0bc2..b7c8f07b 100644 --- a/boards/layout_nrf52.ld +++ b/boards/layout_nrf52.ld.handlebars @@ -10,7 +10,7 @@ MEMORY { * Any change to STACK_SIZE should be accompanied by a corresponding change to * `elf2tab`'s `--stack` option */ -STACK_SIZE = 2048; +STACK_SIZE = {{stack_size}}; MPU_MIN_ALIGN = 8K; diff --git a/boards/layout_nrf52840.ld b/boards/layout_nrf52840.ld.handlebars similarity index 93% rename from boards/layout_nrf52840.ld rename to boards/layout_nrf52840.ld.handlebars index d99efa02..721d4ec8 100644 --- a/boards/layout_nrf52840.ld +++ b/boards/layout_nrf52840.ld.handlebars @@ -10,7 +10,7 @@ MEMORY { * Any change to STACK_SIZE should be accompanied by a corresponding change to * `elf2tab`'s `--stack` option */ -STACK_SIZE = 2048; +STACK_SIZE = {{stack_size}}; MPU_MIN_ALIGN = 8K; diff --git a/boards/layout_opentitan.ld b/boards/layout_opentitan.ld.handlebars similarity index 95% rename from boards/layout_opentitan.ld rename to boards/layout_opentitan.ld.handlebars index db48eef0..80b4ecab 100644 --- a/boards/layout_opentitan.ld +++ b/boards/layout_opentitan.ld.handlebars @@ -17,7 +17,7 @@ MEMORY { * Any change to STACK_SIZE should be accompanied by a corresponding change to * `elf2tab`'s `--stack` option */ -STACK_SIZE = 2048; +STACK_SIZE = {{stack_size}}; MPU_MIN_ALIGN = 1K; diff --git a/build.rs b/build.rs index b24d3d2e..3f025de2 100644 --- a/build.rs +++ b/build.rs @@ -1,12 +1,15 @@ use std::env; use std::fs; use std::fs::File; +use std::io::prelude::*; use std::io::BufRead; use std::io::BufReader; use std::path::Path; use std::process; static LAYOUT_FILE_NAME: &str = "layout.ld"; +static APP_STACK_SIZE: &str = "APP_STACK_SIZE"; +static DEFAULT_STACK_SIZE_BYTES: &str = "2048"; fn main() { static PLATFORM_ENV_VAR: &str = "PLATFORM"; @@ -16,15 +19,28 @@ fn main() { println!("cargo:rerun-if-env-changed={}", PLATFORM_ENV_VAR); println!("cargo:rerun-if-env-changed={}", APP_HEAP_SIZE); + println!("cargo:rerun-if-env-changed={}", APP_STACK_SIZE); println!("cargo:rerun-if-env-changed={}", KERNEL_HEAP_SIZE); println!("cargo:rerun-if-changed={}", PLATFORM_FILE_NAME); println!("cargo:rerun-if-changed={}", LAYOUT_FILE_NAME); + let stack_size = read_env_var(APP_STACK_SIZE); + if stack_size.is_none() { + println!( + "cargo:warning=No explicit stack size specified. \ + Using default of 2048 bytes. \ + Set this explicitly using the \ + environment variable {}. + ", + APP_STACK_SIZE + ); + } + let platform_name = read_env_var(PLATFORM_ENV_VAR).or_else(|| read_board_name_from_file(PLATFORM_FILE_NAME)); if let Some(platform_name) = platform_name { println!("cargo:rustc-env={}={}", PLATFORM_ENV_VAR, platform_name); - copy_linker_file(platform_name.trim()); + create_linker_file(platform_name.trim(), stack_size); } else { println!( "cargo:warning=No platform specified. \ @@ -34,13 +50,14 @@ fn main() { set_default_env(APP_HEAP_SIZE, "1024"); set_default_env(KERNEL_HEAP_SIZE, "1024"); + set_default_env(APP_STACK_SIZE, DEFAULT_STACK_SIZE_BYTES); } fn set_default_env(env_var: &str, default: &str) { if let Some(s) = read_env_var(env_var) { println!("cargo:rustc-env={}={}", env_var, s); } else { - // Just use a default of 1024 if nothing is passed in + // Use the provided default value if nothing is passed in println!("cargo:rustc-env={}={}", env_var, default); } } @@ -63,12 +80,30 @@ fn read_board_name_from_file(file_name: &str) -> Option { Some(board_name) } -fn copy_linker_file(platform_name: &str) { - let linker_file_name = format!("boards/layout_{}.ld", platform_name); +fn create_linker_file(platform_name: &str, stack_size: Option) { + let linker_file_name = format!("boards/layout_{}.ld.handlebars", platform_name); + let path = Path::new(&linker_file_name); if !path.exists() { - println!("Cannot find layout file {:?}", path); + println!("Cannot find layout template file {:?}", path); process::exit(1); } - fs::copy(linker_file_name, LAYOUT_FILE_NAME).unwrap(); + + create_linker_file_from_template( + path, + &stack_size.unwrap_or_else(|| DEFAULT_STACK_SIZE_BYTES.to_string()), + ) +} + +fn create_linker_file_from_template(path: &Path, stack_size: &str) { + let mut linker_template = File::open(path).expect("Failed to open file."); + let mut template_content = String::new(); + linker_template + .read_to_string(&mut template_content) + .expect("Could not read linke file to string."); + + let processed_template = template_content.replace("{{stack_size}}", &stack_size); + + let out_path = Path::new(LAYOUT_FILE_NAME); + fs::write(out_path, processed_template).expect("Failed to write linker file."); } diff --git a/tools/flash.sh b/tools/flash.sh index 7f4c4ea7..797dc11e 100755 --- a/tools/flash.sh +++ b/tools/flash.sh @@ -48,7 +48,7 @@ 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 +elf2tab -n "${artifact}" -o "${tab_file_name}" "${elf_file_name}" --stack $APP_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}\""