72 lines
No EOL
1.3 KiB
Rust
72 lines
No EOL
1.3 KiB
Rust
#![no_std]
|
|
#![feature(linkage)]
|
|
#![feature(panic_info_message)]
|
|
|
|
#[macro_use]
|
|
pub mod console;
|
|
mod lang_items;
|
|
mod syscall;
|
|
|
|
#[no_mangle]
|
|
#[link_section = ".text.entry"]
|
|
pub extern "C" fn _start() -> ! {
|
|
exit(main());
|
|
panic!("unreachable after sys_exit!");
|
|
}
|
|
|
|
#[linkage = "weak"]
|
|
#[no_mangle]
|
|
fn main() -> i32 {
|
|
panic!("Cannot find main!");
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Debug, Default)]
|
|
pub struct TimeVal {
|
|
pub sec: usize,
|
|
pub usec: usize,
|
|
}
|
|
|
|
impl TimeVal {
|
|
pub fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
}
|
|
|
|
use syscall::*;
|
|
|
|
pub fn write(fd: usize, buf: &[u8]) -> isize {
|
|
sys_write(fd, buf)
|
|
}
|
|
pub fn exit(exit_code: i32) -> isize {
|
|
sys_exit(exit_code)
|
|
}
|
|
pub fn yield_() -> isize {
|
|
sys_yield()
|
|
}
|
|
pub fn get_time() -> isize {
|
|
let time = TimeVal::new();
|
|
match sys_get_time(&time, 0) {
|
|
0 => ((time.sec & 0xffff) * 1000 + time.usec / 1000) as isize,
|
|
_ => -1,
|
|
}
|
|
}
|
|
|
|
pub fn sbrk(size: i32) -> isize {
|
|
sys_sbrk(size)
|
|
}
|
|
|
|
pub fn munmap(start: usize, len: usize) -> isize {
|
|
sys_munmap(start, len)
|
|
}
|
|
|
|
pub fn mmap(start: usize, len: usize, prot: usize) -> isize {
|
|
sys_mmap(start, len, prot)
|
|
}
|
|
|
|
pub fn sleep(period_ms: usize) {
|
|
let start = get_time();
|
|
while get_time() < start + period_ms as isize {
|
|
sys_yield();
|
|
}
|
|
} |