⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

veridian_kernel/
lib.rs

1//! VeridianOS Kernel Library
2//!
3//! This library provides the core functionality for the VeridianOS kernel
4//! and exports necessary items for testing.
5
6#![no_std]
7#![cfg_attr(all(test, target_os = "none"), no_main)]
8#![feature(custom_test_frameworks)]
9#![feature(abi_x86_interrupt)]
10#![cfg_attr(target_os = "none", feature(alloc_error_handler))]
11// naked_functions is stable since Rust 1.88.0, no feature flag needed
12// Custom test runner only for bare-metal; host target uses standard #[test] harness.
13#![cfg_attr(target_os = "none", test_runner(crate::test_runner))]
14#![cfg_attr(target_os = "none", reexport_test_harness_main = "test_main")]
15
16#[cfg(feature = "alloc")]
17extern crate alloc;
18
19// On bare-metal targets use the custom kernel heap allocators.
20// On host (x86_64-unknown-linux-gnu) for coverage/testing, delegate to the
21// system allocator so that test code using Vec/String/alloc compiles and runs.
22#[cfg(all(target_arch = "x86_64", target_os = "none"))]
23use linked_list_allocator::LockedHeap;
24
25#[cfg(any(target_arch = "riscv64", target_arch = "aarch64"))]
26mod simple_alloc_unsafe;
27#[cfg(any(target_arch = "riscv64", target_arch = "aarch64"))]
28use simple_alloc_unsafe::{LockedUnsafeBumpAllocator, UnsafeBumpAllocator};
29
30#[cfg(all(target_arch = "x86_64", target_os = "none"))]
31#[global_allocator]
32static ALLOCATOR: LockedHeap = LockedHeap::empty();
33
34#[cfg(any(target_arch = "riscv64", target_arch = "aarch64"))]
35#[global_allocator]
36pub static ALLOCATOR: UnsafeBumpAllocator = UnsafeBumpAllocator::new();
37
38#[cfg(any(target_arch = "riscv64", target_arch = "aarch64"))]
39pub static LOCKED_ALLOCATOR: LockedUnsafeBumpAllocator = LockedUnsafeBumpAllocator::empty();
40
41// Host target: use the system allocator so unit tests can allocate normally.
42#[cfg(not(target_os = "none"))]
43extern crate std;
44#[cfg(not(target_os = "none"))]
45#[global_allocator]
46static SYSTEM_ALLOCATOR: std::alloc::System = std::alloc::System;
47
48/// Get a reference to the global allocator
49#[cfg(all(target_arch = "x86_64", target_os = "none"))]
50pub fn get_allocator() -> &'static LockedHeap {
51    &ALLOCATOR
52}
53
54/// Get a reference to the global allocator for RISC-V/AArch64
55#[cfg(any(target_arch = "riscv64", target_arch = "aarch64"))]
56pub fn get_allocator() -> &'static LockedUnsafeBumpAllocator {
57    &LOCKED_ALLOCATOR
58}
59
60#[macro_use]
61pub mod print;
62pub mod print_capture;
63
64mod intrinsics;
65
66pub mod arch;
67pub mod audio;
68pub mod bootstrap;
69mod cap;
70pub mod crypto;
71pub mod desktop;
72pub mod drivers;
73pub mod elf;
74pub mod error;
75pub mod fs;
76pub mod graphics;
77pub mod ipc;
78pub mod irq;
79pub mod log_service;
80pub mod media;
81pub mod mm;
82pub mod net;
83pub mod perf;
84pub mod phase2_validation;
85pub mod pkg;
86pub mod power;
87pub mod process;
88pub mod raii;
89pub mod sched;
90pub mod security;
91pub mod serial;
92pub mod services;
93pub mod stdlib;
94pub mod sync;
95mod syscall;
96pub mod sysfs;
97pub mod test_tasks;
98pub mod thread_api;
99pub mod timer;
100pub mod userland;
101pub mod userspace;
102pub mod utils;
103pub mod video;
104pub mod virt;
105
106#[allow(dead_code)]
107#[cfg(feature = "alloc")]
108pub mod browser;
109#[allow(dead_code)]
110#[cfg(feature = "alloc")]
111pub mod debug;
112#[allow(dead_code)]
113#[cfg(feature = "alloc")]
114pub mod devtools;
115#[allow(dead_code)]
116#[cfg(feature = "alloc")]
117pub mod verification;
118
119#[cfg(test)]
120mod test_config;
121mod test_framework;
122
123#[cfg(test)]
124mod raii_tests;
125
126#[cfg(test)]
127mod integration_tests;
128
129pub mod bench;
130
131// Re-export for tests and benchmarks
132// Re-export memory management for tests
133pub use mm::{FrameNumber, MemoryRegion, FRAME_SIZE};
134// Re-export scheduler items for tests
135pub use sched::{Priority, SchedClass, Task};
136#[cfg(test)]
137pub use test_framework::test_runner;
138pub use test_framework::{
139    cycles_to_ns, exit_qemu, read_timestamp, test_panic_handler, BenchmarkRunner, QemuExitCode,
140    Testable,
141};
142
143#[cfg(all(test, target_os = "none"))]
144#[no_mangle]
145pub extern "C" fn _start() -> ! {
146    test_main();
147    loop {
148        core::hint::spin_loop();
149    }
150}
151
152#[cfg(all(test, target_os = "none"))]
153#[panic_handler]
154fn panic(info: &core::panic::PanicInfo) -> ! {
155    test_framework::test_panic_handler(info)
156}
157
158/// Heap allocation error handler.
159///
160/// Panic is intentional: heap allocation failure in a no_std kernel is
161/// unrecoverable. The alloc_error_handler ABI requires `-> !`.
162#[cfg(target_os = "none")]
163#[alloc_error_handler]
164fn alloc_error_handler(layout: core::alloc::Layout) -> ! {
165    panic!("Allocation error: {:?}", layout);
166}