Better virtual file system, keyboard through MMIO&VirtIO

This commit is contained in:
2026-03-05 14:41:28 +01:00
parent 041e544330
commit 9b6aec28f5
37 changed files with 1191 additions and 355 deletions

View File

@@ -5,6 +5,7 @@ edition = "2024"
[dependencies]
bffs = { path = "../bffs" }
io = { path = "../io" }
[features]
kernel = []

View File

@@ -1,9 +1,16 @@
#[derive(Debug)]
pub struct File {
fd: u64,
}
impl File {
/// # Safety
/// The file descriptor must be valid
pub unsafe fn new(fd: u64) -> Self {
Self { fd }
}
pub fn as_fd(&self) -> u64 {
self.fd
}
}

View File

@@ -1,10 +1,15 @@
use core::{alloc::Layout, time::Duration};
use bffs::path::Path;
use io::SeekFrom;
use crate::fs::File;
#[repr(u64)]
pub enum SysCall {
Write = 1,
Open = 2,
Seek = 8,
Alloc = 40,
Dealloc = 41,
Exit = 60,
@@ -17,7 +22,9 @@ pub enum SysCall {
impl From<u64> for SysCall {
fn from(value: u64) -> Self {
match value {
1 => SysCall::Write,
2 => SysCall::Open,
8 => SysCall::Seek,
40 => SysCall::Alloc,
41 => SysCall::Dealloc,
60 => SysCall::Exit,
@@ -128,12 +135,29 @@ pub fn dealloc(ptr: *mut u8, layout: core::alloc::Layout) {
syscall!(SysCall::Dealloc, ptr as u64, size as u64, align as u64);
}
}
pub fn open<P: AsRef<Path>>(path: P) -> u64 {
pub fn open<P: AsRef<Path>>(path: P) -> File {
unsafe {
let path_str = path.as_ref().as_str();
let ptr = path_str.as_ptr();
let size = path_str.len();
let (fd, ..) = syscall!(SysCall::Open, ptr as u64, size as u64);
fd
File::new(fd)
}
}
pub fn write(file: &mut File, buf: &[u8]) {
unsafe {
let ptr = buf.as_ptr();
let size = buf.len();
syscall!(SysCall::Write, file.as_fd(), ptr as u64, size as u64);
}
}
pub fn seek(file: &mut File, seek: SeekFrom) {
unsafe {
let (discriminant, value) = match seek {
SeekFrom::Start(v) => (0, v),
SeekFrom::End(v) => (1, v as u64),
SeekFrom::Current(v) => (2, v as u64),
};
syscall!(SysCall::Seek, file.as_fd(), discriminant, value);
}
}