Skip to content

Commit

Permalink
crates/sel4-stack: Introduce
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <[email protected]>
  • Loading branch information
nspin committed Jul 9, 2024
1 parent f73e43f commit e8d0465
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 114 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions crates/examples/root-task/spawn-thread/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mk {
sel4
sel4-root-task
sel4-elf-header
sel4-stack
sel4-initialize-tls
;
};
Expand Down
1 change: 1 addition & 0 deletions crates/examples/root-task/spawn-thread/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
22 changes: 4 additions & 18 deletions crates/examples/root-task/spawn-thread/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -253,23 +256,6 @@ fn get_tls_image() -> TlsImage {

// // //

#[repr(C, align(16))]
struct Stack<const N: usize>(UnsafeCell<[u8; N]>);

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

impl<const N: usize> Stack<N> {
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]>);

Expand Down
1 change: 1 addition & 0 deletions crates/sel4-kernel-loader/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]; };
};
Expand Down
1 change: 1 addition & 0 deletions crates/sel4-kernel-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
21 changes: 1 addition & 20 deletions crates/sel4-kernel-loader/src/this_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,7 @@ pub(crate) mod stacks {
use core::cell::UnsafeCell;

use sel4_config::sel4_cfg_usize;

#[repr(C, align(16))]
struct Stack<const N: usize>(UnsafeCell<[u8; N]>);

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

impl<const N: usize> Stack<N> {
pub const fn new() -> Self {
Self(UnsafeCell::new([0; N]))
}

pub const fn top(&self) -> StackTop {
StackTop(self.0.get().cast::<u8>().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

Expand Down
3 changes: 2 additions & 1 deletion crates/sel4-reset/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
1 change: 1 addition & 0 deletions crates/sel4-reset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ license = "BSD-2-Clause"

[dependencies]
cfg-if = "1.0.0"
sel4-stack = { path = "../sel4-stack" }
39 changes: 2 additions & 37 deletions crates/sel4-reset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<const N: usize>(UnsafeCell<[u8; N]>);

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

impl<const N: usize> Stack<N> {
pub const fn new() -> Self {
Self(UnsafeCell::new([0; N]))
}

pub const fn top(&self) -> StackTop {
StackTop(self.0.get().cast::<u8>().wrapping_add(N))
}
}

impl<const N: usize> Default for Stack<N> {
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"]
Expand Down
2 changes: 1 addition & 1 deletion crates/sel4-runtime-common/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
};
Expand Down
1 change: 1 addition & 0 deletions crates/sel4-runtime-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 1 addition & 37 deletions crates/sel4-runtime-common/src/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<const N: usize>(UnsafeCell<[u8; N]>);

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

impl<const N: usize> Stack<N> {
pub const fn new() -> Self {
Self(UnsafeCell::new([0; N]))
}

pub const fn top(&self) -> StackTop {
StackTop(self.0.get().cast::<u8>().wrapping_add(N))
}
}

impl<const N: usize> Default for Stack<N> {
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) => {
Expand Down Expand Up @@ -158,5 +122,5 @@ cfg_if::cfg_if! {
}

pub mod _private {
pub use super::{Stack, StackTop};
pub use sel4_stack::{Stack, StackTop};
}
11 changes: 11 additions & 0 deletions crates/sel4-stack/Cargo.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Copyright 2024, Colias Group, LLC
#
# SPDX-License-Identifier: BSD-2-Clause
#

{ mk }:

mk {
package.name = "sel4-stack";
}
17 changes: 17 additions & 0 deletions crates/sel4-stack/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
edition = "2021"
license = "BSD-2-Clause"
51 changes: 51 additions & 0 deletions crates/sel4-stack/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<const N: usize>(UnsafeCell<[u8; N]>);

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

impl<const N: usize> Stack<N> {
pub const fn new() -> Self {
Self(UnsafeCell::new([0; N]))
}

pub const fn top(&self) -> StackTop {
StackTop(self.0.get().cast::<u8>().wrapping_add(N))
}
}

impl<const N: usize> Default for Stack<N> {
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 {}

0 comments on commit e8d0465

Please sign in to comment.