Skip to content

Commit a21359b

Browse files
committed
crates/sel4-stack: Introduce
Signed-off-by: Nick Spinale <nick@nickspinale.com>
1 parent 6145def commit a21359b

17 files changed

Lines changed: 105 additions & 114 deletions

File tree

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ members = [
135135
"crates/sel4-shared-ring-buffer/block-io/types",
136136
"crates/sel4-shared-ring-buffer/bookkeeping",
137137
"crates/sel4-shared-ring-buffer/smoltcp",
138+
"crates/sel4-stack",
138139
"crates/sel4-sync",
139140
"crates/sel4-test-harness",
140141
"crates/sel4/bitfield-ops",

crates/examples/root-task/spawn-thread/Cargo.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mk {
1414
sel4
1515
sel4-root-task
1616
sel4-elf-header
17+
sel4-stack
1718
sel4-initialize-tls
1819
;
1920
};

crates/examples/root-task/spawn-thread/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ sel4 = { path = "../../../sel4" }
2222
sel4-elf-header = { path = "../../../sel4-elf-header" }
2323
sel4-initialize-tls = { path = "../../../sel4-initialize-tls" }
2424
sel4-root-task = { path = "../../../sel4-root-task" }
25+
sel4-stack = { path = "../../../sel4-stack" }

crates/examples/root-task/spawn-thread/src/main.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use sel4_initialize_tls::{TlsImage, TlsReservationLayout, UncheckedTlsImage};
2424
use sel4_root_task::{
2525
abort, panicking::catch_unwind, root_task, set_global_allocator_mutex_notification, Never,
2626
};
27+
use sel4_stack::Stack;
2728

2829
static SECONDARY_THREAD_STACK: Stack<4096> = Stack::new();
2930

@@ -133,7 +134,9 @@ fn find_largest_kernel_untyped(bootinfo: &sel4::BootInfo) -> sel4::cap::Untyped
133134
fn create_user_context(f: SecondaryThreadFn) -> sel4::UserContext {
134135
let mut ctx = sel4::UserContext::default();
135136

136-
*ctx.sp_mut() = SECONDARY_THREAD_STACK.top().try_into().unwrap();
137+
*ctx.sp_mut() = (SECONDARY_THREAD_STACK.top().ptr() as usize)
138+
.try_into()
139+
.unwrap();
137140
*ctx.pc_mut() = (secondary_thread_entrypoint as usize).try_into().unwrap();
138141
*ctx.c_param_mut(0) = f.into_arg();
139142

@@ -253,23 +256,6 @@ fn get_tls_image() -> TlsImage {
253256

254257
// // //
255258

256-
#[repr(C, align(16))]
257-
struct Stack<const N: usize>(UnsafeCell<[u8; N]>);
258-
259-
unsafe impl<const N: usize> Sync for Stack<N> {}
260-
261-
impl<const N: usize> Stack<N> {
262-
const fn new() -> Self {
263-
Self(UnsafeCell::new([0; N]))
264-
}
265-
266-
fn top(&self) -> usize {
267-
(self.0.get() as usize) + N
268-
}
269-
}
270-
271-
// // //
272-
273259
#[repr(C, align(4096))]
274260
struct IpcBufferFrame(UnsafeCell<[u8; GRANULE_SIZE]>);
275261

crates/sel4-kernel-loader/Cargo.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mk {
2020
sel4-config
2121
sel4-kernel-loader-embed-page-tables-runtime
2222
sel4-immutable-cell
23+
sel4-stack
2324
;
2425
sel4-kernel-loader-payload-types = localCrates.sel4-kernel-loader-payload-types // { features = [ "serde" ]; };
2526
};

crates/sel4-kernel-loader/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ sel4-kernel-loader-embed-page-tables-runtime = { path = "embed-page-tables/runti
2828
sel4-kernel-loader-payload-types = { path = "payload-types", features = ["serde"] }
2929
sel4-logging = { path = "../sel4-logging" }
3030
sel4-platform-info = { path = "../sel4-platform-info" }
31+
sel4-stack = { path = "../sel4-stack" }
3132
spin = { version = "0.9.4", features = ["lock_api"] }
3233

3334
[build-dependencies]

crates/sel4-kernel-loader/src/this_image.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,7 @@ pub(crate) mod stacks {
5555
use core::cell::UnsafeCell;
5656

5757
use sel4_config::sel4_cfg_usize;
58-
59-
#[repr(C, align(16))]
60-
struct Stack<const N: usize>(UnsafeCell<[u8; N]>);
61-
62-
unsafe impl<const N: usize> Sync for Stack<N> {}
63-
64-
impl<const N: usize> Stack<N> {
65-
pub const fn new() -> Self {
66-
Self(UnsafeCell::new([0; N]))
67-
}
68-
69-
pub const fn top(&self) -> StackTop {
70-
StackTop(self.0.get().cast::<u8>().wrapping_add(N))
71-
}
72-
}
73-
74-
#[repr(transparent)]
75-
pub struct StackTop(#[allow(dead_code)] *mut u8);
76-
77-
unsafe impl Sync for StackTop {}
58+
use sel4_stack::{Stack, StackTop};
7859

7960
const PRIMARY_STACK_SIZE: usize = 4096 * 8; // TODO this is excessive
8061

crates/sel4-reset/Cargo.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
# SPDX-License-Identifier: BSD-2-Clause
55
#
66

7-
{ mk, versions }:
7+
{ mk, versions, localCrates }:
88

99
mk {
1010
package.name = "sel4-reset";
1111
dependencies = {
1212
inherit (versions) cfg-if;
13+
inherit (localCrates) sel4-stack;
1314
};
1415
}

crates/sel4-reset/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ license = "BSD-2-Clause"
1818

1919
[dependencies]
2020
cfg-if = "1.0.0"
21+
sel4-stack = { path = "../sel4-stack" }

0 commit comments

Comments
 (0)