diff --git a/Cargo.lock b/Cargo.lock index ded1c2e91..80456966b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2315,6 +2315,7 @@ dependencies = [ "sel4-pl011-driver", "sel4-platform-info", "sel4-rustfmt-helper", + "sel4-stack", "serde", "smccc", "spin 0.9.8", @@ -2534,6 +2535,7 @@ name = "sel4-reset" version = "0.1.0" dependencies = [ "cfg-if", + "sel4-stack", ] [[package]] @@ -2589,6 +2591,7 @@ dependencies = [ "sel4-elf-header", "sel4-initialize-tls", "sel4-panicking-env", + "sel4-stack", "unwinding", ] @@ -2732,6 +2735,10 @@ dependencies = [ "tock-registers", ] +[[package]] +name = "sel4-stack" +version = "0.1.0" + [[package]] name = "sel4-sync" version = "0.1.0" @@ -2943,6 +2950,7 @@ dependencies = [ "sel4-elf-header", "sel4-initialize-tls", "sel4-root-task", + "sel4-stack", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ba449efd6..ad2161dc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -135,6 +135,7 @@ members = [ "crates/sel4-shared-ring-buffer/block-io/types", "crates/sel4-shared-ring-buffer/bookkeeping", "crates/sel4-shared-ring-buffer/smoltcp", + "crates/sel4-stack", "crates/sel4-sync", "crates/sel4-test-harness", "crates/sel4/bitfield-ops", diff --git a/crates/examples/root-task/spawn-thread/Cargo.nix b/crates/examples/root-task/spawn-thread/Cargo.nix index 7f6fc9406..394991b1a 100644 --- a/crates/examples/root-task/spawn-thread/Cargo.nix +++ b/crates/examples/root-task/spawn-thread/Cargo.nix @@ -14,6 +14,7 @@ mk { sel4 sel4-root-task sel4-elf-header + sel4-stack sel4-initialize-tls ; }; diff --git a/crates/examples/root-task/spawn-thread/Cargo.toml b/crates/examples/root-task/spawn-thread/Cargo.toml index 95100b49a..c252b2263 100644 --- a/crates/examples/root-task/spawn-thread/Cargo.toml +++ b/crates/examples/root-task/spawn-thread/Cargo.toml @@ -22,3 +22,4 @@ sel4 = { path = "../../../sel4" } sel4-elf-header = { path = "../../../sel4-elf-header" } sel4-initialize-tls = { path = "../../../sel4-initialize-tls" } sel4-root-task = { path = "../../../sel4-root-task" } +sel4-stack = { path = "../../../sel4-stack" } diff --git a/crates/examples/root-task/spawn-thread/src/main.rs b/crates/examples/root-task/spawn-thread/src/main.rs index 19bb21049..b157de24e 100644 --- a/crates/examples/root-task/spawn-thread/src/main.rs +++ b/crates/examples/root-task/spawn-thread/src/main.rs @@ -24,6 +24,7 @@ use sel4_initialize_tls::{TlsImage, TlsReservationLayout, UncheckedTlsImage}; use sel4_root_task::{ abort, panicking::catch_unwind, root_task, set_global_allocator_mutex_notification, Never, }; +use sel4_stack::Stack; static SECONDARY_THREAD_STACK: Stack<4096> = Stack::new(); @@ -133,7 +134,9 @@ fn find_largest_kernel_untyped(bootinfo: &sel4::BootInfo) -> sel4::cap::Untyped fn create_user_context(f: SecondaryThreadFn) -> sel4::UserContext { let mut ctx = sel4::UserContext::default(); - *ctx.sp_mut() = SECONDARY_THREAD_STACK.top().try_into().unwrap(); + *ctx.sp_mut() = (SECONDARY_THREAD_STACK.top().ptr() as usize) + .try_into() + .unwrap(); *ctx.pc_mut() = (secondary_thread_entrypoint as usize).try_into().unwrap(); *ctx.c_param_mut(0) = f.into_arg(); @@ -253,23 +256,6 @@ fn get_tls_image() -> TlsImage { // // // -#[repr(C, align(16))] -struct Stack(UnsafeCell<[u8; N]>); - -unsafe impl Sync for Stack {} - -impl Stack { - const fn new() -> Self { - Self(UnsafeCell::new([0; N])) - } - - fn top(&self) -> usize { - (self.0.get() as usize) + N - } -} - -// // // - #[repr(C, align(4096))] struct IpcBufferFrame(UnsafeCell<[u8; GRANULE_SIZE]>); diff --git a/crates/sel4-kernel-loader/Cargo.nix b/crates/sel4-kernel-loader/Cargo.nix index be5cbb167..be78360c7 100644 --- a/crates/sel4-kernel-loader/Cargo.nix +++ b/crates/sel4-kernel-loader/Cargo.nix @@ -20,6 +20,7 @@ mk { sel4-config sel4-kernel-loader-embed-page-tables-runtime sel4-immutable-cell + sel4-stack ; sel4-kernel-loader-payload-types = localCrates.sel4-kernel-loader-payload-types // { features = [ "serde" ]; }; }; diff --git a/crates/sel4-kernel-loader/Cargo.toml b/crates/sel4-kernel-loader/Cargo.toml index 91bcc31d1..2203f9496 100644 --- a/crates/sel4-kernel-loader/Cargo.toml +++ b/crates/sel4-kernel-loader/Cargo.toml @@ -28,6 +28,7 @@ sel4-kernel-loader-embed-page-tables-runtime = { path = "embed-page-tables/runti sel4-kernel-loader-payload-types = { path = "payload-types", features = ["serde"] } sel4-logging = { path = "../sel4-logging" } sel4-platform-info = { path = "../sel4-platform-info" } +sel4-stack = { path = "../sel4-stack" } spin = { version = "0.9.4", features = ["lock_api"] } [build-dependencies] diff --git a/crates/sel4-kernel-loader/src/this_image.rs b/crates/sel4-kernel-loader/src/this_image.rs index 32a27b00d..c7b0d48ed 100644 --- a/crates/sel4-kernel-loader/src/this_image.rs +++ b/crates/sel4-kernel-loader/src/this_image.rs @@ -55,26 +55,7 @@ pub(crate) mod stacks { use core::cell::UnsafeCell; use sel4_config::sel4_cfg_usize; - - #[repr(C, align(16))] - struct Stack(UnsafeCell<[u8; N]>); - - unsafe impl Sync for Stack {} - - impl Stack { - pub const fn new() -> Self { - Self(UnsafeCell::new([0; N])) - } - - pub const fn top(&self) -> StackTop { - StackTop(self.0.get().cast::().wrapping_add(N)) - } - } - - #[repr(transparent)] - pub struct StackTop(#[allow(dead_code)] *mut u8); - - unsafe impl Sync for StackTop {} + use sel4_stack::{Stack, StackTop}; const PRIMARY_STACK_SIZE: usize = 4096 * 8; // TODO this is excessive diff --git a/crates/sel4-reset/Cargo.nix b/crates/sel4-reset/Cargo.nix index b72744993..6c91b430f 100644 --- a/crates/sel4-reset/Cargo.nix +++ b/crates/sel4-reset/Cargo.nix @@ -4,11 +4,12 @@ # SPDX-License-Identifier: BSD-2-Clause # -{ mk, versions }: +{ mk, versions, localCrates }: mk { package.name = "sel4-reset"; dependencies = { inherit (versions) cfg-if; + inherit (localCrates) sel4-stack; }; } diff --git a/crates/sel4-reset/Cargo.toml b/crates/sel4-reset/Cargo.toml index 33da63163..4e3edbc85 100644 --- a/crates/sel4-reset/Cargo.toml +++ b/crates/sel4-reset/Cargo.toml @@ -18,3 +18,4 @@ license = "BSD-2-Clause" [dependencies] cfg-if = "1.0.0" +sel4-stack = { path = "../sel4-stack" } diff --git a/crates/sel4-reset/src/lib.rs b/crates/sel4-reset/src/lib.rs index 463b5b515..376aed3ce 100644 --- a/crates/sel4-reset/src/lib.rs +++ b/crates/sel4-reset/src/lib.rs @@ -7,12 +7,13 @@ #![no_std] use core::arch::global_asm; -use core::cell::UnsafeCell; use core::ptr; use core::slice; use cfg_if::cfg_if; +use sel4_stack::{Stack, StackTop}; + // // // #[repr(C)] @@ -43,42 +44,6 @@ impl<'a> Regions<'a> { // // // -#[repr(C)] -#[cfg_attr( - any( - target_arch = "aarch64", - target_arch = "riscv32", - target_arch = "riscv64", - target_arch = "x86_64", - ), - repr(align(16)) -)] -#[cfg_attr(target_arch = "arm", repr(align(4)))] -pub struct Stack(UnsafeCell<[u8; N]>); - -unsafe impl Sync for Stack {} - -impl Stack { - pub const fn new() -> Self { - Self(UnsafeCell::new([0; N])) - } - - pub const fn top(&self) -> StackTop { - StackTop(self.0.get().cast::().wrapping_add(N)) - } -} - -impl Default for Stack { - fn default() -> Self { - Self::new() - } -} - -#[repr(transparent)] -pub struct StackTop(#[allow(dead_code)] *mut u8); - -unsafe impl Sync for StackTop {} - const STACK_SIZE: usize = 4096; #[link_section = ".persistent"] diff --git a/crates/sel4-runtime-common/Cargo.nix b/crates/sel4-runtime-common/Cargo.nix index c9636d70a..d24744d96 100644 --- a/crates/sel4-runtime-common/Cargo.nix +++ b/crates/sel4-runtime-common/Cargo.nix @@ -10,7 +10,7 @@ mk { package.name = "sel4-runtime-common"; dependencies = { inherit (versions) cfg-if; - inherit (localCrates) sel4-panicking-env sel4-elf-header; + inherit (localCrates) sel4-panicking-env sel4-elf-header sel4-stack; sel4 = localCrates.sel4 // { default-features = false; optional = true; }; sel4-initialize-tls = localCrates.sel4-initialize-tls // { features = [ "on-stack" ]; optional = true; }; }; diff --git a/crates/sel4-runtime-common/Cargo.toml b/crates/sel4-runtime-common/Cargo.toml index a94de4448..fc8a81b16 100644 --- a/crates/sel4-runtime-common/Cargo.toml +++ b/crates/sel4-runtime-common/Cargo.toml @@ -26,6 +26,7 @@ sel4 = { path = "../sel4", default-features = false, optional = true } sel4-elf-header = { path = "../sel4-elf-header" } sel4-initialize-tls = { path = "../sel4-initialize-tls", features = ["on-stack"], optional = true } sel4-panicking-env = { path = "../sel4-panicking/env" } +sel4-stack = { path = "../sel4-stack" } [target."cfg(not(target_arch = \"arm\"))".dependencies.unwinding] version = "0.1.6" diff --git a/crates/sel4-runtime-common/src/start.rs b/crates/sel4-runtime-common/src/start.rs index 0b4a65565..f821ba50a 100644 --- a/crates/sel4-runtime-common/src/start.rs +++ b/crates/sel4-runtime-common/src/start.rs @@ -10,42 +10,6 @@ use core::arch::global_asm; use core::cell::UnsafeCell; -#[repr(C)] -#[cfg_attr( - any( - target_arch = "aarch64", - target_arch = "riscv32", - target_arch = "riscv64", - target_arch = "x86_64", - ), - repr(align(16)) -)] -#[cfg_attr(target_arch = "arm", repr(align(4)))] -pub struct Stack(UnsafeCell<[u8; N]>); - -unsafe impl Sync for Stack {} - -impl Stack { - pub const fn new() -> Self { - Self(UnsafeCell::new([0; N])) - } - - pub const fn top(&self) -> StackTop { - StackTop(self.0.get().cast::().wrapping_add(N)) - } -} - -impl Default for Stack { - fn default() -> Self { - Self::new() - } -} - -#[repr(transparent)] -pub struct StackTop(#[allow(dead_code)] *mut u8); - -unsafe impl Sync for StackTop {} - #[macro_export] macro_rules! declare_stack { ($size:expr) => { @@ -158,5 +122,5 @@ cfg_if::cfg_if! { } pub mod _private { - pub use super::{Stack, StackTop}; + pub use sel4_stack::{Stack, StackTop}; } diff --git a/crates/sel4-stack/Cargo.nix b/crates/sel4-stack/Cargo.nix new file mode 100644 index 000000000..6c7cbbec8 --- /dev/null +++ b/crates/sel4-stack/Cargo.nix @@ -0,0 +1,11 @@ +# +# Copyright 2024, Colias Group, LLC +# +# SPDX-License-Identifier: BSD-2-Clause +# + +{ mk }: + +mk { + package.name = "sel4-stack"; +} diff --git a/crates/sel4-stack/Cargo.toml b/crates/sel4-stack/Cargo.toml new file mode 100644 index 000000000..1a546b880 --- /dev/null +++ b/crates/sel4-stack/Cargo.toml @@ -0,0 +1,17 @@ +# +# Copyright 2023, Colias Group, LLC +# +# SPDX-License-Identifier: BSD-2-Clause +# +# +# This file is generated from './Cargo.nix'. You can edit this file directly +# if you are not using this project's Cargo manifest management tools. +# See 'hacking/cargo-manifest-management/README.md' for more information. +# + +[package] +name = "sel4-stack" +version = "0.1.0" +authors = ["Nick Spinale "] +edition = "2021" +license = "BSD-2-Clause" diff --git a/crates/sel4-stack/src/lib.rs b/crates/sel4-stack/src/lib.rs new file mode 100644 index 000000000..b1fa36bc8 --- /dev/null +++ b/crates/sel4-stack/src/lib.rs @@ -0,0 +1,51 @@ +// +// Copyright 2023, Colias Group, LLC +// +// SPDX-License-Identifier: BSD-2-Clause +// + +#![no_std] + +use core::cell::UnsafeCell; + +#[repr(C)] +#[cfg_attr( + any( + target_arch = "aarch64", + target_arch = "riscv32", + target_arch = "riscv64", + target_arch = "x86_64", + ), + repr(align(16)) +)] +#[cfg_attr(target_arch = "arm", repr(align(4)))] +pub struct Stack(UnsafeCell<[u8; N]>); + +unsafe impl Sync for Stack {} + +impl Stack { + pub const fn new() -> Self { + Self(UnsafeCell::new([0; N])) + } + + pub const fn top(&self) -> StackTop { + StackTop(self.0.get().cast::().wrapping_add(N)) + } +} + +impl Default for Stack { + fn default() -> Self { + Self::new() + } +} + +#[repr(transparent)] +pub struct StackTop(#[allow(dead_code)] *mut u8); + +impl StackTop { + pub fn ptr(&self) -> *mut u8 { + self.0 + } +} + +unsafe impl Sync for StackTop {}