Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support configuration of memory size #321

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Minor

- Support configuring the memory size with `WASEFIRE_MEMORY_PAGE_COUNT`
- Support `platform::update::is_supported()`
- Support `platform::update`
- Support `platform` and `platform::reboot()`
Expand Down
20 changes: 18 additions & 2 deletions crates/scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,10 @@ impl<B: Board> Scheduler<B> {

#[cfg(feature = "wasm")]
fn load(&mut self, wasm: &'static [u8]) {
const MEMORY_SIZE: usize = memory_size();
#[repr(align(16))]
struct Memory([u8; 0x10000]);
static mut MEMORY: Memory = Memory([0; 0x10000]);
struct Memory([u8; MEMORY_SIZE]);
static mut MEMORY: Memory = Memory([0; MEMORY_SIZE]);
#[cfg(not(feature = "unsafe-skip-validation"))]
let module = Module::new(wasm).unwrap();
// SAFETY: The module is valid by the feature invariant.
Expand Down Expand Up @@ -421,3 +422,18 @@ impl From<()> for Trap {
Trap
}
}

#[cfg(feature = "wasm")]
const fn memory_size() -> usize {
let page = match option_env!("WASEFIRE_MEMORY_PAGE_COUNT") {
Some(x) => {
let x = x.as_bytes();
assert!(x.len() == 1, "not a single digit");
let x = x[0];
assert!(x.is_ascii_digit(), "not a single digit");
x as usize
}
None => 1,
};
page * 0x10000
}
13 changes: 12 additions & 1 deletion crates/xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,16 @@ struct RunnerOptions {
#[clap(long)]
measure_bloat: bool,

/// Show the (top N) stack sizes of the firmware
/// Show the (top N) stack sizes of the firmware.
#[clap(long)]
stack_sizes: Option<Option<usize>>,

/// Allocates <MEMORY_PAGE_COUNT> pages for the WASM module.
///
/// Supported values are numbers between 0 and 9 inclusive, i.e. single digit. The default when
/// missing is 1 page.
#[clap(long)]
memory_page_count: Option<usize>,
}

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -533,6 +540,10 @@ impl RunnerOptions {
if !features.is_empty() {
cargo.arg(format!("--features={}", features.join(",")));
}
if let Some(n) = self.memory_page_count {
ensure!((0 ..= 9).contains(&n), "--memory-page-count supports single digit only");
cargo.env("WASEFIRE_MEMORY_PAGE_COUNT", format!("{n}"));
}
cargo.env("RUSTFLAGS", rustflags.join(" "));
cargo.current_dir(format!("crates/runner-{}", self.name));
fs::touch("target/wasefire/applet.wasm")?;
Expand Down