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

Lib lpc55 rng seed #1820

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/lpc55xpresso/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ task-slots = ["gpio_driver", "syscon_driver"]
[tasks.rng_driver]
name = "drv-lpc55-rng"
priority = 3
max-sizes = {flash = 16384, ram = 4096}
uses = ["rng", "pmc"]
start = true
stacksize = 2200
stacksize = 4200
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think this'd make it the highest stack usage in the system, above control-plane-agent. Do we have a sense for what caused this? (Asking because I've been winding up playing stack size whack-a-mole lately.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having read more of the code, I can answer this question at least partially: the Server type used in the RNG server is large, and it's being created on the stack and passed around.

It's large primarily due to the Sha3 embedded within it, which I recently noticed in an unrelated case was about 2 kiB.

The easiest way to fix this would be to allocate a static Sha3 instance using ClaimOnceCell (from lib/static-cell) and then pass it into the server constructor to take ownership of (via a &'static mut).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to use mutable-statics because Sha3 doesn't have a const constructor (per discussion in chat): b43295c

task-slots = ["syscon_driver"]
extern-regions = ["dice_certs", "dice_rng"]

[tasks.pong]
name = "task-pong"
Expand Down
9 changes: 9 additions & 0 deletions app/oxide-rot-1/app-dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ stacksize = 12304
start = true
extern-regions = ["dice_alias", "dice_certs"]

[tasks.rng_driver]
name = "drv-lpc55-rng"
priority = 6
uses = ["rng", "pmc"]
start = true
stacksize = 4200
task-slots = ["syscon_driver"]
extern-regions = ["dice_certs", "dice_rng"]

[signing.certs]
signing-certs = ["../../support/fake_certs/fake_certificate.der.crt"]
root-certs = ["../../support/fake_certs/fake_certificate.der.crt"]
Expand Down
4 changes: 2 additions & 2 deletions app/rot-carrier/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ pins = [
[tasks.rng_driver]
name = "drv-lpc55-rng"
priority = 5
max-sizes = {flash = 16384, ram = 4096}
uses = ["rng", "pmc"]
start = true
stacksize = 2200
stacksize = 4200
task-slots = ["syscon_driver"]
extern-regions = ["dice_certs", "dice_rng"]

[tasks.sprot]
name = "drv-lpc55-sprot-server"
Expand Down
16 changes: 16 additions & 0 deletions chips/lpc55/memory.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,19 @@ size = 0x800
read = true
write = true
execute = false

[[dice_rng]]
name = "a"
address =0x40101a00
size = 0x100
read = true
write = true
execute = false

[[dice_rng]]
name = "b"
address =0x40101a00
size = 0x100
read = true
write = true
execute = false
18 changes: 16 additions & 2 deletions drv/lpc55-rng/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,35 @@ edition = "2021"

[dependencies]
cfg-if = { workspace = true }
hubpack.workspace = true
idol-runtime = { workspace = true }
lpc55-pac = { workspace = true }
num-traits = { workspace = true }
rand_chacha = { workspace = true }
rand_core = { workspace = true }
serde.workspace = true
sha3.workspace = true
zerocopy = { workspace = true }
zeroize.workspace = true

drv-lpc55-syscon-api = { path = "../lpc55-syscon-api" }
drv-rng-api = { path = "../rng-api" }
lib-dice.path = "../../lib/dice"
lib-lpc55-rng.path = "../../lib/lpc55-rng"
ringbuf.path = "../../lib/ringbuf"
stage0-handoff = { path = "../../lib/stage0-handoff", optional = true }
userlib = { path = "../../sys/userlib", features = ["panic-messages"] }

[build-dependencies]
idol = { workspace = true }
anyhow.workspace = true
build-util.path = "../../build/util"
cfg-if.workspace = true
idol.workspace = true
indexmap = { workspace = true, optional = true }
serde.workspace = true

[features]
default = ["dice-seed"]
dice-seed = ["indexmap", "stage0-handoff"]
no-ipc-counters = ["idol/no-counters"]

# This section is here to discourage RLS/rust-analyzer from doing test builds,
Expand Down
72 changes: 66 additions & 6 deletions drv/lpc55-rng/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,75 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
use anyhow::{anyhow, Result};
use idol::{server::ServerStyle, CounterSettings};

cfg_if::cfg_if! {
if #[cfg(feature = "dice-seed")] {
mod config {
include!("src/config.rs");
}
use anyhow::Context;
use config::DataRegion;
use indexmap::IndexMap;
use std::{fs::File, io::Write};

const CFG_SRC: &str = "rng-config.rs";
}
}

#[cfg(feature = "dice-seed")]
fn extern_region_to_cfg<W: Write>(
out: &mut W,
data_regions: &IndexMap<String, DataRegion>,
name: &str,
) -> Result<()> {
let region = data_regions
.get(name)
.ok_or_else(|| anyhow::anyhow!("dice_certs data region not found"))?;

Ok(writeln!(
out,
r##"pub const {}: DataRegion = DataRegion {{
address: {:#x},
size: {:#x},
}};"##,
name.to_uppercase(),
region.address,
region.size
)?)
}

#[cfg(feature = "dice-seed")]
fn extern_regions_to_cfg(path: &str) -> Result<()> {
let out_dir = build_util::out_dir();
let dest_path = out_dir.join(path);
let mut out =
File::create(dest_path).context(format!("creating {}", path))?;

let data_regions = build_util::task_extern_regions::<DataRegion>()?;
if data_regions.is_empty() {
return Err(anyhow!("no data regions found"));
}

writeln!(out, "use crate::config::DataRegion;\n\n")?;

extern_region_to_cfg(&mut out, &data_regions, "dice_certs")?;
extern_region_to_cfg(&mut out, &data_regions, "dice_rng")
}

fn main() -> Result<()> {
idol::Generator::new()
.with_counters(
idol::CounterSettings::default().with_server_counters(false),
)
.with_counters(CounterSettings::default().with_server_counters(false))
.build_server_support(
"../../idl/rng.idol",
"server_stub.rs",
idol::server::ServerStyle::InOrder,
)?;
ServerStyle::InOrder,
)
.map_err(|e| anyhow!(e))?;

#[cfg(feature = "dice-seed")]
extern_regions_to_cfg(CFG_SRC)?;

Ok(())
}
11 changes: 11 additions & 0 deletions drv/lpc55-rng/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#[derive(serde::Deserialize, Default, Debug)]
#[serde(rename_all = "kebab-case")]
#[cfg(feature = "dice-seed")]
pub struct DataRegion {
pub address: usize,
pub size: usize,
}
Loading
Loading