Next Generation WASM Microkernel Operating System
1// Copyright 2025 Jonas Kruckenberg
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use crate::arch;
9use crate::device_tree::DeviceTree;
10use core::cell::OnceCell;
11use cpu_local::cpu_local;
12use kasync::executor::Executor;
13use kasync::time::{Instant, Timer};
14use loader_api::BootInfo;
15use spin::OnceLock;
16
17static GLOBAL: OnceLock<Global> = OnceLock::new();
18
19cpu_local! {
20 static CPU_LOCAL: OnceCell<CpuLocal> = OnceCell::new();
21}
22
23#[derive(Debug)]
24pub struct Global {
25 pub executor: Executor,
26 pub timer: Timer,
27 pub device_tree: DeviceTree,
28 pub boot_info: &'static BootInfo,
29 pub time_origin: Instant,
30 pub arch: arch::state::Global,
31}
32
33#[derive(Debug)]
34pub struct CpuLocal {
35 pub id: usize,
36 pub arch: arch::state::CpuLocal,
37}
38
39pub fn try_init_global<F>(f: F) -> crate::Result<&'static Global>
40where
41 F: FnOnce() -> crate::Result<Global>,
42{
43 GLOBAL.get_or_try_init(f)
44}
45
46pub fn init_cpu_local(state: CpuLocal) {
47 CPU_LOCAL
48 .set(state)
49 .expect("CPU local state already initialized");
50}
51
52pub fn global() -> &'static Global {
53 GLOBAL.get().expect("Global state not initialized")
54}
55
56pub fn try_global() -> Option<&'static Global> {
57 GLOBAL.get()
58}
59
60pub fn cpu_local() -> &'static CpuLocal {
61 CPU_LOCAL.get().expect("Cpu local state not initialized")
62}
63
64pub fn try_cpu_local() -> Option<&'static CpuLocal> {
65 CPU_LOCAL.get()
66}