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,4 +1,5 @@
#![feature(iterator_try_collect, iter_order_by)]
#![allow(unused_features)]
#![cfg_attr(any(not(feature = "std"), target_arch = "riscv64"), no_std)]
use core::cell::RefCell;

View File

@@ -7,6 +7,6 @@ edition = "2024"
proc-macro = true
[dependencies]
image = "0.25"
image = { version = "0.25", default-features = false, features = ["png"] }
syn = { version = "2", features = ["full"] }
zyn = "0.5"

View File

@@ -1,12 +0,0 @@
[package]
name = "os-std-macros"
version = "0.1.0"
edition = "2024"
[lib]
proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full"] }

View File

@@ -1 +0,0 @@

View File

@@ -1,9 +0,0 @@
[package]
name = "os-std"
version = "0.1.0"
edition = "2024"
[dependencies]
os-std-macros = { path = "../os-std-macros" }
shared = { path = "../shared", features = ["user"] }
io = { path = "../io", features = ["alloc"] }

View File

@@ -1 +0,0 @@
pub use io::SeekFrom;

View File

@@ -1,62 +0,0 @@
#![no_std]
extern crate alloc;
pub mod io;
pub mod prelude;
pub use shared::fs;
pub use shared::syscall;
#[macro_export]
macro_rules! custom_std_setup {
() => {
use $crate::prelude::*;
extern crate alloc;
struct GlobalAllocator;
#[global_allocator]
static GLOBAL_ALLOCATOR: GlobalAllocator = GlobalAllocator;
unsafe impl core::alloc::GlobalAlloc for GlobalAllocator {
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
$crate::syscall::alloc(layout)
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: core::alloc::Layout) {
$crate::syscall::dealloc(ptr, layout)
}
}
#[panic_handler]
fn panic(_panic_info: &core::panic::PanicInfo) -> ! {
// TODO print
loop {}
}
#[unsafe(no_mangle)]
pub extern "C" fn _start() {
main()
}
};
}
#[macro_export]
macro_rules! print {
($($args:expr),*) => {
$crate::syscall::write_string_temp(&format!($($args),*))
};
}
#[macro_export]
macro_rules! println {
() => {
$crate::print!("");
// $crate::print!("\n\r");
};
($($args:expr),*) => {
$crate::print!($($args),*);
// $crate::println!();
};
}

View File

@@ -1,5 +0,0 @@
pub use crate::print;
pub use crate::println;
pub use alloc::format;
pub use alloc::string::String;
pub use alloc::vec;

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);
}
}