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 core::cell::OnceCell;
9
10use k23_cpu_local::cpu_local;
11use k23_spin::OnceLock;
12use kasync::executor::Executor;
13use kasync::time::{Instant, Timer};
14use loader_api::BootInfo;
15
16use crate::arch;
17use crate::device_tree::DeviceTree;
18
19static GLOBAL: OnceLock<Global> = OnceLock::new();
20
21cpu_local! {
22 static CPU_LOCAL: OnceCell<CpuLocal> = OnceCell::new();
23}
24
25#[derive(Debug)]
26pub struct Global {
27 pub executor: Executor,
28 pub timer: Timer,
29 pub device_tree: DeviceTree,
30 pub boot_info: &'static BootInfo,
31 pub time_origin: Instant,
32 pub arch: arch::state::Global,
33}
34
35#[derive(Debug)]
36pub struct CpuLocal {
37 pub id: usize,
38 pub arch: arch::state::CpuLocal,
39}
40
41pub fn try_init_global<F>(f: F) -> crate::Result<&'static Global>
42where
43 F: FnOnce() -> crate::Result<Global>,
44{
45 GLOBAL.get_or_try_init(f)
46}
47
48pub fn init_cpu_local(state: CpuLocal) {
49 CPU_LOCAL
50 .set(state)
51 .expect("CPU local state already initialized");
52}
53
54pub fn global() -> &'static Global {
55 GLOBAL.get().expect("Global state not initialized")
56}
57
58pub fn try_global() -> Option<&'static Global> {
59 GLOBAL.get()
60}
61
62pub fn cpu_local() -> &'static CpuLocal {
63 CPU_LOCAL.get().expect("Cpu local state not initialized")
64}
65
66pub fn try_cpu_local() -> Option<&'static CpuLocal> {
67 CPU_LOCAL.get()
68}