149 lines
3.7 KiB
Rust
149 lines
3.7 KiB
Rust
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
|
|
|
|
#[path = "unsupported.rs"]
|
|
mod unsupported;
|
|
pub use self::unsupported::{STDIN_BUF_SIZE, Stderr, Stdin, is_ebadf, panic_output};
|
|
|
|
pub struct Stdout;
|
|
// pub struct Stdin;
|
|
|
|
impl Stdout {
|
|
pub const fn new() -> Self {
|
|
Self
|
|
}
|
|
}
|
|
// impl Stdin {
|
|
// pub const fn new() -> Self {
|
|
// Self
|
|
// }
|
|
// }
|
|
|
|
impl io::Write for Stdout {
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
unsafe { Ok(write(1, buf) as usize) }
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
// todo!()
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
// impl io::Read for Stdin {
|
|
// fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
// Ok(0)
|
|
// // unsafe { Ok(read(0, buf) as usize) }
|
|
// }
|
|
// }
|
|
|
|
#[repr(u64)]
|
|
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,
|
|
WriteTemp = 999,
|
|
Unimplemented = 1 << 31,
|
|
}
|
|
|
|
impl From<u64> for SysCall {
|
|
fn from(value: u64) -> Self {
|
|
match value {
|
|
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,
|
|
999 => SysCall::WriteTemp,
|
|
_ => SysCall::Unimplemented,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
unsafe fn _syscall(
|
|
syscall: SysCall,
|
|
mut a1: u64,
|
|
mut a2: u64,
|
|
mut a3: u64,
|
|
mut a4: u64,
|
|
mut a5: u64,
|
|
mut a6: u64,
|
|
mut a7: u64,
|
|
) -> (u64, u64, u64, u64, u64, u64, u64, u64) {
|
|
let mut a0 = syscall as u64;
|
|
unsafe {
|
|
core::arch::asm!(
|
|
"ecall",
|
|
inlateout("a0") a0,
|
|
inlateout("a1") a1,
|
|
inlateout("a2") a2,
|
|
inlateout("a3") a3,
|
|
inlateout("a4") a4,
|
|
inlateout("a5") a5,
|
|
inlateout("a6") a6,
|
|
inlateout("a7") a7,
|
|
clobber_abi("system")
|
|
);
|
|
}
|
|
(a0, a1, a2, a3, a4, a5, a6, a7)
|
|
}
|
|
|
|
macro_rules! syscall {
|
|
($syscall:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr, $a6:expr, $a7:expr) => {
|
|
_syscall($syscall, $a1, $a2, $a3, $a4, $a5, $a6, $a7)
|
|
};
|
|
($syscall:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr, $a6:expr) => {
|
|
syscall!($syscall, $a1, $a2, $a3, $a4, $a5, $a6, 0)
|
|
};
|
|
($syscall:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr) => {
|
|
syscall!($syscall, $a1, $a2, $a3, $a4, $a5, 0)
|
|
};
|
|
($syscall:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr) => {
|
|
syscall!($syscall, $a1, $a2, $a3, $a4, 0)
|
|
};
|
|
($syscall:expr, $a1:expr, $a2:expr, $a3:expr) => {
|
|
syscall!($syscall, $a1, $a2, $a3, 0)
|
|
};
|
|
($syscall:expr, $a1:expr, $a2:expr) => {
|
|
syscall!($syscall, $a1, $a2, 0)
|
|
};
|
|
($syscall:expr, $a1:expr) => {
|
|
syscall!($syscall, $a1, 0)
|
|
};
|
|
($syscall:expr) => {
|
|
syscall!($syscall, 0)
|
|
};
|
|
}
|
|
pub fn write(file_descriptor: u64, buf: &[u8]) -> u64 {
|
|
unsafe {
|
|
let ptr = buf.as_ptr();
|
|
let size = buf.len();
|
|
let (len, ..) = syscall!(SysCall::Write, file_descriptor, ptr as u64, size as u64);
|
|
len
|
|
}
|
|
}
|
|
pub fn read(file_descriptor: u64, buf: &mut [u8]) -> u64 {
|
|
unsafe {
|
|
let ptr = buf.as_ptr();
|
|
let size = buf.len();
|
|
let (len, ..) = syscall!(SysCall::Read, file_descriptor, ptr as u64, size as u64);
|
|
len
|
|
}
|
|
}
|