Add the rust std as a custom sysroot

This commit is contained in:
2026-03-17 18:29:00 +01:00
parent 404a681254
commit 46cdc3714e
50 changed files with 1158 additions and 320 deletions

View File

@@ -1,3 +1,7 @@
use io::{IoBase, Read, Write};
use crate::syscall;
#[derive(Debug)]
pub struct File {
fd: u64,
@@ -6,7 +10,7 @@ pub struct File {
impl File {
/// # Safety
/// The file descriptor must be valid
pub unsafe fn new(fd: u64) -> Self {
pub unsafe fn from_raw_fd(fd: u64) -> Self {
Self { fd }
}
@@ -14,3 +18,23 @@ impl File {
self.fd
}
}
impl IoBase for File {
type Error = ();
}
impl Read for File {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
Ok(syscall::read(self.as_fd(), buf) as usize)
}
}
impl Write for File {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
Ok(syscall::write(self.as_fd(), buf) as usize)
}
fn flush(&mut self) -> Result<(), Self::Error> {
todo!()
}
}

View File

@@ -10,9 +10,12 @@ pub enum SysCall {
Read = 0,
Write = 1,
Open = 2,
Close = 3,
Seek = 8,
Alloc = 40,
Dealloc = 41,
Spawn = 58,
ExecVE = 59,
Exit = 60,
NanoSleep = 101,
WriteIntTemp = 998,
@@ -26,9 +29,12 @@ impl From<u64> for SysCall {
0 => SysCall::Read,
1 => SysCall::Write,
2 => SysCall::Open,
3 => SysCall::Close,
8 => SysCall::Seek,
40 => SysCall::Alloc,
41 => SysCall::Dealloc,
58 => SysCall::Spawn,
59 => SysCall::ExecVE,
60 => SysCall::Exit,
101 => SysCall::NanoSleep,
998 => SysCall::WriteIntTemp,
@@ -143,30 +149,53 @@ pub fn open<P: AsRef<Path>>(path: P) -> File {
let ptr = path_str.as_ptr();
let size = path_str.len();
let (fd, ..) = syscall!(SysCall::Open, ptr as u64, size as u64);
File::new(fd)
File::from_raw_fd(fd)
}
}
pub fn write(file: &mut File, buf: &[u8]) {
pub fn close(file_descriptor: u64) {
unsafe {
syscall!(SysCall::Close, file_descriptor);
}
}
pub fn write(file_descriptor: u64, buf: &[u8]) -> u64 {
unsafe {
let ptr = buf.as_ptr();
let size = buf.len();
syscall!(SysCall::Write, file.as_fd(), ptr as u64, size as u64);
let (len, ..) = syscall!(SysCall::Write, file_descriptor, ptr as u64, size as u64);
len
}
}
pub fn read(file: &mut File, buf: &mut [u8]) {
pub fn read(file_descriptor: u64, buf: &mut [u8]) -> u64 {
unsafe {
let ptr = buf.as_ptr();
let size = buf.len();
syscall!(SysCall::Read, file.as_fd(), ptr as u64, size as u64);
let (len, ..) = syscall!(SysCall::Read, file_descriptor, ptr as u64, size as u64);
len
}
}
pub fn seek(file: &mut File, seek: SeekFrom) {
pub fn seek(file_descriptor: u64, 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);
syscall!(SysCall::Seek, file_descriptor, discriminant, value);
}
}
pub fn spawn<P: AsRef<Path>>(path: P) {
unsafe {
let path_str = path.as_ref().as_str();
let ptr = path_str.as_ptr();
let size = path_str.len();
syscall!(SysCall::Spawn, ptr as u64, size as u64);
}
}
pub fn execve<P: AsRef<Path>>(path: P) {
unsafe {
let path_str = path.as_ref().as_str();
let ptr = path_str.as_ptr();
let size = path_str.len();
syscall!(SysCall::ExecVE, ptr as u64, size as u64);
}
}