-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crates/sel4-newlib: Switch to more static approach
Signed-off-by: Nick Spinale <[email protected]>
- Loading branch information
Showing
15 changed files
with
173 additions
and
222 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,6 @@ mk { | |
features = [ | ||
"nosys" | ||
"all-symbols" | ||
"sel4-panicking-env" | ||
]; | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,6 @@ mk { | |
features = [ | ||
"nosys" | ||
"all-symbols" | ||
"sel4-panicking-env" | ||
]; | ||
}; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ mk rec { | |
features = [ | ||
"nosys" | ||
"all-symbols" | ||
"sel4-panicking-env" | ||
]; | ||
}; | ||
getrandom = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}; | ||
} |
Oops, something went wrong.