Skip to content

Commit

Permalink
crates/sel4-newlib: Switch to more static approach
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <[email protected]>
  • Loading branch information
nspin committed Jan 6, 2024
1 parent 673540c commit 56e0380
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 222 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion crates/examples/microkit/http-server/pds/server/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ mk {
features = [
"nosys"
"all-symbols"
"sel4-panicking-env"
];
};

Expand Down
5 changes: 1 addition & 4 deletions crates/examples/microkit/http-server/pds/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sel4-externally-shared = { path = "../../../../../sel4-externally-shared", featu
sel4-immediate-sync-once-cell = { path = "../../../../../sel4-immediate-sync-once-cell" }
sel4-logging = { path = "../../../../../sel4-logging" }
sel4-microkit-message = { path = "../../../../../sel4-microkit/message" }
sel4-newlib = { path = "../../../../../sel4-newlib", features = ["nosys", "all-symbols"] }
sel4-shared-ring-buffer = { path = "../../../../../sel4-shared-ring-buffer" }
sel4-shared-ring-buffer-block-io = { path = "../../../../../sel4-shared-ring-buffer/block-io" }
sel4-shared-ring-buffer-smoltcp = { path = "../../../../../sel4-shared-ring-buffer/smoltcp" }
Expand All @@ -53,10 +54,6 @@ path = "../../../../../sel4-microkit"
default-features = false
features = ["alloc"]

[dependencies.sel4-newlib]
path = "../../../../../sel4-newlib"
features = ["nosys", "all-symbols", "sel4-panicking-env"]

[dependencies.sel4-shared-ring-buffer-block-io-types]
path = "../../../../../sel4-shared-ring-buffer/block-io/types"

Expand Down
17 changes: 1 addition & 16 deletions crates/examples/microkit/http-server/pds/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ static LOGGER: Logger = LoggerBuilder::const_default()
fn init() -> impl Handler {
LOGGER.set().unwrap();

setup_newlib();

let timer_client = TimerClient::new(channels::TIMER_DRIVER);
let net_client = NetClient::new(channels::NET_DRIVER);
let block_client = BlockClient::new(channels::BLOCK_DRIVER);
Expand Down Expand Up @@ -184,17 +182,4 @@ fn init() -> impl Handler {
)
}

fn setup_newlib() {
use sel4_newlib::*;

set_static_heap_for_sbrk({
static HEAP: StaticHeap<{ 1024 * 1024 }> = StaticHeap::new();
&HEAP
});

set_implementations(Implementations {
_sbrk: Some(sbrk_with_static_heap),
_write: Some(write_with_debug_put_char),
..Default::default()
})
}
sel4_newlib::declare_sbrk_with_static_heap!(1024 * 1024);
1 change: 0 additions & 1 deletion crates/private/tests/root-task/mbedtls/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ mk {
features = [
"nosys"
"all-symbols"
"sel4-panicking-env"
];
};
};
Expand Down
5 changes: 1 addition & 4 deletions crates/private/tests/root-task/mbedtls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ license = "BSD-2-Clause"
log = "0.4.17"
sel4 = { path = "../../../../sel4" }
sel4-logging = { path = "../../../../sel4-logging" }
sel4-newlib = { path = "../../../../sel4-newlib", features = ["nosys", "all-symbols"] }
sel4-root-task = { path = "../../../../sel4-root-task" }

[dependencies.mbedtls]
Expand All @@ -37,7 +38,3 @@ default-features = false
git = "https://github.com/coliasgroup/rust-mbedtls"
tag = "keep/30d001b63baea36135b2590c4fd05e95"
default-features = false

[dependencies.sel4-newlib]
path = "../../../../sel4-newlib"
features = ["nosys", "all-symbols", "sel4-panicking-env"]
29 changes: 14 additions & 15 deletions crates/private/tests/root-task/mbedtls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use core::ffi::{c_char, c_int, CStr};

use sel4_logging::{LevelFilter, Logger, LoggerBuilder};
use sel4_newlib as _;
use sel4_root_task::{debug_print, debug_println, root_task};

const LOG_LEVEL: LevelFilter = LevelFilter::Debug;
Expand All @@ -31,21 +30,9 @@ fn main(_: &sel4::BootInfo) -> ! {
unreachable!()
}

fn run_tests() {
{
use sel4_newlib::*;

set_static_heap_for_sbrk({
static HEAP: StaticHeap<{ HEAP_SIZE }> = StaticHeap::new();
&HEAP
});

let mut impls = Implementations::default();
impls._sbrk = Some(sbrk_with_static_heap);
impls._write = Some(write_with_debug_put_char);
set_implementations(impls)
}
sel4_newlib::declare_sbrk_with_static_heap!(HEAP_SIZE);

fn run_tests() {
unsafe {
mbedtls::self_test::enable(rand, Some(log));
}
Expand Down Expand Up @@ -112,3 +99,15 @@ const TESTS: &[(&str, Test)] = tests! {
ecp,
ecjpake,
};

#[allow(non_snake_case)]
mod hack {
#[repr(C, align(16))]
#[derive(Debug)]
pub struct LongDoublePlaceholder(pub [u8; 16]);

#[no_mangle]
extern "C" fn __trunctfdf2(a: LongDoublePlaceholder) -> f64 {
sel4_root_task::abort!("__trunctfdf2({:?})", a)
}
}
1 change: 0 additions & 1 deletion crates/private/tests/root-task/ring-test-harness/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ mk rec {
features = [
"nosys"
"all-symbols"
"sel4-panicking-env"
];
};
getrandom = {
Expand Down
5 changes: 1 addition & 4 deletions crates/private/tests/root-task/ring-test-harness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ license = "BSD-2-Clause"
getrandom = { version = "0.2.10", features = ["custom"] }
rand = { version = "0.8.5", default-features = false, features = ["small_rng"] }
sel4 = { path = "../../../../sel4" }
sel4-newlib = { path = "../../../../sel4-newlib", features = ["nosys", "all-symbols"] }
sel4-root-task = { path = "../../../../sel4-root-task" }
sel4-test-harness = { path = "../../../../sel4-test-harness" }

[dependencies.sel4-newlib]
path = "../../../../sel4-newlib"
features = ["nosys", "all-symbols", "sel4-panicking-env"]
17 changes: 6 additions & 11 deletions crates/sel4-newlib/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,22 @@ mk {
package.name = "sel4-newlib";
features = {
default = [ "detect-libc" ];
detect-libc = [];
detect-libc = [ "cc" ];
nosys = [];
_exit = [];
__trunctfdf2 = [];
_sbrk = [];
_write = [];
errno = [];
all-symbols = [
"_exit"
"_sbrk"
"_write"
"__trunctfdf2"
"errno"
];
};
dependencies = {
inherit (versions) log;
inherit (localCrates)
sel4-immediate-sync-once-cell
;
sel4-panicking-env = localCrates.sel4-panicking-env // { optional = true; };
inherit (localCrates) sel4-panicking-env;
log = { version = versions.log; optional = true; };
};
build-dependencies = {
cc = "1.0.82";
cc = { version = "1.0.82"; optional = true; };
};
}
14 changes: 6 additions & 8 deletions crates/sel4-newlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ edition = "2021"
license = "BSD-2-Clause"

[features]
__trunctfdf2 = []
_exit = []
_sbrk = []
_write = []
all-symbols = ["_exit", "_sbrk", "_write", "__trunctfdf2"]
all-symbols = ["_exit", "_write", "errno"]
default = ["detect-libc"]
detect-libc = []
detect-libc = ["cc"]
errno = []
nosys = []

[dependencies]
log = "0.4.17"
sel4-immediate-sync-once-cell = { path = "../sel4-immediate-sync-once-cell" }
sel4-panicking-env = { path = "../sel4-panicking/env", optional = true }
log = { version = "0.4.17", optional = true }
sel4-panicking-env = { path = "../sel4-panicking/env" }

[build-dependencies]
cc = "1.0.82"
cc = { version = "1.0.82", optional = true }
4 changes: 3 additions & 1 deletion crates/sel4-newlib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ fn main() {
if cfg!(feature = "nosys") {
println!("cargo:rustc-link-lib=static=nosys");
}
if cfg!(feature = "detect-libc") {
#[cfg(feature = "detect-libc")]
{
detect_libc();
}
}

#[cfg(feature = "detect-libc")]
fn detect_libc() {
let tool = cc::Build::new().get_compiler();

Expand Down
29 changes: 29 additions & 0 deletions crates/sel4-newlib/src/errno.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use core::ffi::c_int;

#[cfg(feature = "errno")]
#[no_mangle]
static mut errno: c_int = 0;

#[cfg(not(feature = "errno"))]
extern "C" {
static mut errno: c_int;
}

pub(crate) fn set_errno(err: c_int) {
unsafe {
errno = err;
}
}

pub(crate) mod values {
use super::*;

pub(crate) const ENOENT: c_int = 2;
pub(crate) const ENOMEM: c_int = 12;
}
75 changes: 75 additions & 0 deletions crates/sel4-newlib/src/heap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use super::*;

use core::cell::SyncUnsafeCell;
use core::ffi::{c_int, c_void};
use core::ptr;
use core::sync::atomic::{AtomicIsize, Ordering};

use sel4_panicking_env::abort;

#[repr(align(4096))] // no real reason for this
struct BackingMemory<const N: usize>(SyncUnsafeCell<[u8; N]>);

impl<const N: usize> BackingMemory<N> {
const fn new() -> Self {
Self(SyncUnsafeCell::new([0; N]))
}

const fn bounds(&self) -> *mut [u8] {
ptr::slice_from_raw_parts_mut(self.0.get().cast(), N)
}
}

#[doc(hidden)]
pub struct StaticHeap<const N: usize> {
memory: BackingMemory<N>,
watermark: AtomicIsize,
}

unsafe impl<const N: usize> Sync for StaticHeap<N> {}

impl<const N: usize> StaticHeap<N> {
pub const fn new() -> Self {
Self {
memory: BackingMemory::new(),
watermark: AtomicIsize::new(0),
}
}

// TODO handle overflowing atomic
pub fn sbrk(&self, incr: c_int) -> *mut c_void {
#[cfg(feature = "log")]
{
log::trace!("_sbrk({})", incr);
}
let incr = incr.try_into().unwrap_or_else(|_| abort!());
let old = self.watermark.fetch_add(incr, Ordering::SeqCst);
let new = old + incr;
if new < 0 {
abort!("program break below data segment start")
}
if new > N.try_into().unwrap_or_else(|_| abort!()) {
self.watermark.fetch_sub(incr, Ordering::SeqCst);
errno::set_errno(errno::values::ENOMEM);
return usize::MAX as *mut c_void;
}
unsafe { self.memory.bounds().as_mut_ptr().offset(old).cast() }
}
}

#[macro_export]
macro_rules! declare_sbrk_with_static_heap {
($n:expr) => {
#[no_mangle]
extern "C" fn _sbrk(incr: core::ffi::c_int) -> *mut core::ffi::c_void {
static HEAP: $crate::StaticHeap<{ $n }> = $crate::StaticHeap::new();
HEAP.sbrk(incr)
}
};
}
Loading

0 comments on commit 56e0380

Please sign in to comment.