Files
XunilOS/kernel/src/util.rs
csd4ni3l 9aeff321f8 Add ET_EXEC ELF loading and validation, and a non-working ET_REL
implementation, running support, also basic syscalls to test if it
works. add a syscall ABI and a new libxunil ABI for calling back to the
kernel with syscalls, add user mode protection when running ELF, add TSS
Privilege Stack table to fix GPF issues, add invalid opcode handler, fix
rust panics using rectangle_filled instead of fb clear. Also add a hello
world example binary that runs in usermode at startup.
2026-04-01 14:06:20 +02:00

70 lines
1.5 KiB
Rust

use crate::{driver::timer::TIMER, println};
use spin::{Mutex, MutexGuard};
pub struct LinkedNode {
pub size: usize,
pub next: Option<&'static mut LinkedNode>,
}
impl LinkedNode {
pub const fn new(size: usize) -> LinkedNode {
LinkedNode { size, next: None }
}
pub fn start_addr(&self) -> usize {
self as *const Self as usize
}
pub fn end_addr(&self) -> usize {
self.start_addr() + self.size
}
}
pub struct Locked<A> {
inner: Mutex<A>,
}
impl<A> Locked<A> {
pub const fn new(inner: A) -> Self {
Locked {
inner: Mutex::new(inner),
}
}
pub fn lock(&self) -> MutexGuard<A> {
self.inner.lock()
}
}
pub fn test_performance<F: FnOnce()>(function: F) {
let start = TIMER.now();
let ret = function();
println!("took {} ms", (TIMER.now() - start).elapsed());
ret
}
pub fn get_bit(value: u8, position: u8) -> u8 {
(value >> position) & 1
}
#[inline]
pub const fn align_down(addr: u64, align: u64) -> u64 {
assert!(align.is_power_of_two(), "`align` must be a power of two");
addr & !(align - 1)
}
#[inline]
pub const fn align_up(addr: u64, align: u64) -> u64 {
assert!(align.is_power_of_two(), "`align` must be a power of two");
let align_mask = align - 1;
if addr & align_mask == 0 {
addr
} else {
if let Some(aligned) = (addr | align_mask).checked_add(1) {
aligned
} else {
panic!("attempt to add with overflow")
}
}
}