Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Cargo.lock

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

11 changes: 11 additions & 0 deletions beskar-core/src/drivers/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ impl KeyCode {
Self::Numpad7 => '7',
Self::Numpad8 => '8',
Self::Numpad9 => '9',
Self::Comma => ',',
Self::Dot => '.',
Self::Minus => '-',
Self::Equal => '=',
Self::Semicolon => ';',
Self::Apostrophe => '\'',
Self::Slash => '/',
Self::Backslash => '\\',
Self::Tilde => '~',
Self::LeftBracket => '[',
Self::RightBracket => ']',
_ => '\0',
};

Expand Down
13 changes: 10 additions & 3 deletions beskar-core/src/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,29 @@ pub enum Syscall {
/// The second argument is the alignment of the memory.
/// The third argument is the protection flags of the memory.
MemoryMap = 5,
/// MemoryUnmap syscall.
///
/// Frees previously allocated memory.
///
/// The first argument is the pointer to the memory region.
/// The second argument is the size of the memory region.
MemoryUnmap = 6,
/// MemoryProtect syscall.
///
/// Changes the protection of a memory region.
///
/// The first argument is the pointer to the memory region.
/// The second argument is the size of the memory region.
/// The third argument is the new protection flags.
MemoryProtect = 6,
MemoryProtect = 7,
/// Put the thread to sleep for a given amount of time.
///
/// The first argument is the time to sleep in milliseconds.
Sleep = 7,
Sleep = 8,
/// Put the thread to sleep until a given event is signalled.
///
/// The first argument is the sleep handle to wait on.
WaitOnEvent = 8,
WaitOnEvent = 9,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
Expand Down
8 changes: 4 additions & 4 deletions beskar-core/src/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Pixel {

#[must_use]
#[inline]
fn new_rgb(components: PixelComponents) -> Self {
pub fn new_rgb(components: PixelComponents) -> Self {
Self(
((u32::from(components.blue)) << 16)
| ((u32::from(components.green)) << 8)
Expand All @@ -165,7 +165,7 @@ impl Pixel {

#[must_use]
#[inline]
fn new_bgr(components: PixelComponents) -> Self {
pub fn new_bgr(components: PixelComponents) -> Self {
Self(
((u32::from(components.red)) << 16)
| ((u32::from(components.green)) << 8)
Expand All @@ -185,7 +185,7 @@ impl Pixel {

#[must_use]
#[inline]
fn components_bgr(self) -> PixelComponents {
pub fn components_bgr(self) -> PixelComponents {
let red = u8::try_from((self.0 >> 16) & 0xFF).unwrap();
let green = u8::try_from((self.0 >> 8) & 0xFF).unwrap();
let blue = u8::try_from(self.0 & 0xFF).unwrap();
Expand All @@ -194,7 +194,7 @@ impl Pixel {

#[must_use]
#[inline]
fn components_rgb(self) -> PixelComponents {
pub fn components_rgb(self) -> PixelComponents {
let blue = u8::try_from((self.0 >> 16) & 0xFF).unwrap();
let green = u8::try_from((self.0 >> 8) & 0xFF).unwrap();
let red = u8::try_from(self.0 & 0xFF).unwrap();
Expand Down
32 changes: 32 additions & 0 deletions beskar-hal/src/x86_64/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,37 @@ where
}
}

#[inline]
/// Initialize the FPU
pub unsafe fn fpu_init() {
unsafe {
core::arch::asm!("fninit", options(nomem, nostack, preserves_flags));
}
}

#[inline]
/// Save the FPU state
pub unsafe fn fpu_save(dst: &mut super::structures::SseSave) {
unsafe {
core::arch::asm!(
"fxsave [{}]",
in(reg) dst,
options(nostack, preserves_flags)
);
}
}

#[inline]
/// Restore the FPU state
pub unsafe fn fpu_restore(src: &super::structures::SseSave) {
unsafe {
core::arch::asm!(
"fxrstor [{}]",
in(reg) src,
options(readonly, nostack, preserves_flags)
);
}
}

/// This value can be used to fill the stack when debugging stack overflows.
pub const STACK_DEBUG_INSTR: u8 = 0xCC;
11 changes: 11 additions & 0 deletions beskar-hal/src/x86_64/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use beskar_core::arch::{PhysAddr, VirtAddr, paging::Frame};
pub struct Cr0;

impl Cr0 {
pub const MONITOR_COPROCESSOR: u64 = 1 << 1;
pub const EMULATE_COPROCESSOR: u64 = 1 << 2;
pub const TASK_SWITCHED: u64 = 1 << 3;
pub const WRITE_PROTECT: u64 = 1 << 16;
pub const CACHE_DISABLE: u64 = 1 << 30;
Expand All @@ -28,6 +30,14 @@ impl Cr0 {
}
}

#[inline]
/// Clears the TS (Task Switched) flag in CR0.
pub unsafe fn clear_ts() {
unsafe {
core::arch::asm!("clts", options(nomem, nostack, preserves_flags));
}
}

#[inline]
/// # Safety
///
Expand Down Expand Up @@ -99,6 +109,7 @@ impl Cr4 {
pub const TSD: u64 = 1 << 2;
pub const PAE: u64 = 1 << 5;
pub const OSFXSR: u64 = 1 << 9;
pub const OSXMMEXCPT: u64 = 1 << 10;
pub const SMXE: u64 = 1 << 14;
pub const FSGSBASE: u64 = 1 << 16;
pub const PCIDE: u64 = 1 << 17;
Expand Down
26 changes: 26 additions & 0 deletions beskar-hal/src/x86_64/structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,32 @@ impl TaskStateSegment {
}
}

#[derive(Debug, Clone)]
#[repr(C, align(16))]
/// Saved SSE state (512 bytes aligned to 16 bytes)
pub struct SseSave {
pub data: [u8; 160],
pub xmm: [core::arch::x86_64::__m128; 16],
_res: [core::arch::x86_64::__m128; 3],
pub available: [core::arch::x86_64::__m128; 3],
}
beskar_core::static_assert!(size_of::<SseSave>() == 512);

impl Default for SseSave {
fn default() -> Self {
Self::new()
}
}

impl SseSave {
#[must_use]
#[inline]
pub const fn new() -> Self {
// Safety: All-zeroed state is a valid initial state for SSE save area.
unsafe { core::mem::zeroed() }
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
14 changes: 8 additions & 6 deletions beskar-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ pub fn __init() {
#[inline]
/// In debug builds, triggers a breakpoint interrupt (`int3`).
pub fn debug_break() {
#[cfg(debug_assertions)]
unsafe {
core::arch::asm!("int3", options(nomem, nostack, preserves_flags));
if cfg!(debug_assertions) {
unsafe {
core::arch::asm!("int3", options(nomem, nostack, preserves_flags));
}
}
}

Expand All @@ -95,8 +96,9 @@ pub fn debug_break() {
///
/// The provided value `x` is placed in the `RAX` register before triggering the interrupt.
pub fn debug_break_value(x: u64) {
#[cfg(debug_assertions)]
unsafe {
core::arch::asm!("int3", in("rax") x, options(nomem, nostack, preserves_flags));
if cfg!(debug_assertions) {
unsafe {
core::arch::asm!("int3", in("rax") x, options(nomem, nostack, preserves_flags));
}
}
}
13 changes: 13 additions & 0 deletions beskar-lib/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ pub fn mmap(
NonNull::new(ptr).ok_or_else(|| MemoryError::new(MemoryErrorKind::OutOfMemory))
}

/// Unmap memory from the address space
///
/// Returns true if the operation was successful, false otherwise.
///
/// # Safety
///
/// The pointer and size must be valid and correspond to a previously mapped region
/// that will no longer be used after this call.
pub unsafe fn munmap(ptr: *mut u8, size: u64) -> bool {
let res = crate::sys::sc_munmap(ptr, size);
res.is_success()
}

/// Change the protection of a memory region
///
/// Returns true if the operation was successful, false otherwise.
Expand Down
6 changes: 6 additions & 0 deletions beskar-lib/src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ pub fn sc_mmap(size: u64, alignment: u64, flags: u64) -> *mut u8 {
res as _
}

#[inline]
pub fn sc_munmap(ptr: *mut u8, size: u64) -> SyscallExitCode {
let res = syscalls::syscall_2(Syscall::MemoryUnmap, ptr as u64, size);
SyscallExitCode::try_from(res).unwrap()
}

#[inline]
pub fn sc_mprotect(ptr: *mut u8, size: u64, flags: u64) -> SyscallExitCode {
let res = syscalls::syscall_3(Syscall::MemoryProtect, ptr as u64, size, flags);
Expand Down
1 change: 1 addition & 0 deletions kernel/foundry/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ edition = "2024"

[dependencies]
beskar-core = { workspace = true }
hashbrown = { version = "0.16.1" }
hyperdrive = { workspace = true }
thiserror = { workspace = true }
4 changes: 2 additions & 2 deletions kernel/foundry/storage/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ pub trait FileSystem {
fn read_dir(&mut self, path: Path) -> FileResult<Vec<PathBuf>>;
}

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PathBuf(String);

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Path<'a>(&'a str);

impl PathBuf {
Expand Down
Loading