lib.rs 3.0 KB
Newer Older
Y
Yifan Wu 已提交
1
#![no_std]
2
#![feature(asm)]
Y
Yifan Wu 已提交
3 4
#![feature(linkage)]
#![feature(panic_info_message)]
5
#![feature(alloc_error_handler)]
Y
Yifan Wu 已提交
6 7 8 9 10 11

#[macro_use]
pub mod console;
mod syscall;
mod lang_items;

Y
Yifan Wu 已提交
12
extern crate alloc;
Y
Yifan Wu 已提交
13 14
#[macro_use]
extern crate bitflags;
Y
Yifan Wu 已提交
15

16 17
use syscall::*;
use buddy_system_allocator::LockedHeap;
Y
Yifan Wu 已提交
18
use alloc::vec::Vec;
19

20
const USER_HEAP_SIZE: usize = 32768;
21 22 23 24 25 26 27 28 29 30 31

static mut HEAP_SPACE: [u8; USER_HEAP_SIZE] = [0; USER_HEAP_SIZE];

#[global_allocator]
static HEAP: LockedHeap = LockedHeap::empty();

#[alloc_error_handler]
pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
    panic!("Heap allocation error, layout = {:?}", layout);
}

Y
Yifan Wu 已提交
32 33
#[no_mangle]
#[link_section = ".text.entry"]
Y
Yifan Wu 已提交
34
pub extern "C" fn _start(argc: usize, argv: usize) -> ! {
35 36 37 38
    unsafe {
        HEAP.lock()
            .init(HEAP_SPACE.as_ptr() as usize, USER_HEAP_SIZE);
    }
Y
Yifan Wu 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    let mut v: Vec<&'static str> = Vec::new();
    for i in 0..argc {
        let str_start = unsafe {
            ((argv + i * core::mem::size_of::<usize>()) as *const usize).read_volatile()
        };
        let len = (0usize..).find(|i| unsafe {
            ((str_start + *i) as *const u8).read_volatile() == 0
        }).unwrap();
        v.push(
            core::str::from_utf8(unsafe {
                core::slice::from_raw_parts(str_start as *const u8, len)
            }).unwrap()
        );
    }
    exit(main(argc, v.as_slice()));
Y
Yifan Wu 已提交
54 55 56 57
}

#[linkage = "weak"]
#[no_mangle]
Y
Yifan Wu 已提交
58
fn main(_argc: usize, _argv: &[&str]) -> i32 {
Y
Yifan Wu 已提交
59 60 61
    panic!("Cannot find main!");
}

Y
Yifan Wu 已提交
62 63 64 65 66 67 68 69 70 71
bitflags! {
    pub struct OpenFlags: u32 {
        const RDONLY = 0;
        const WRONLY = 1 << 0;
        const RDWR = 1 << 1;
        const CREATE = 1 << 9;
        const TRUNC = 1 << 10;
    }
}

72
pub fn dup(fd: usize) -> isize { sys_dup(fd) }
Y
Yifan Wu 已提交
73
pub fn open(path: &str, flags: OpenFlags) -> isize { sys_open(path, flags.bits) }
Y
Yifan Wu 已提交
74 75
pub fn close(fd: usize) -> isize { sys_close(fd) }
pub fn pipe(pipe_fd: &mut [usize]) -> isize { sys_pipe(pipe_fd) }
76 77
pub fn read(fd: usize, buf: &mut [u8]) -> isize { sys_read(fd, buf) }
pub fn write(fd: usize, buf: &[u8]) -> isize { sys_write(fd, buf) }
Y
Yifan Wu 已提交
78
pub fn exit(exit_code: i32) -> ! { sys_exit(exit_code); }
79 80 81 82
pub fn yield_() -> isize { sys_yield() }
pub fn get_time() -> isize { sys_get_time() }
pub fn getpid() -> isize { sys_getpid() }
pub fn fork() -> isize { sys_fork() }
Y
Yifan Wu 已提交
83
pub fn exec(path: &str, args: &[*const u8]) -> isize { sys_exec(path, args) }
84 85 86
pub fn wait(exit_code: &mut i32) -> isize {
    loop {
        match sys_waitpid(-1, exit_code as *mut _) {
87 88
            -2 => { yield_(); }
            // -1 or a real pid
89 90 91 92
            exit_pid => return exit_pid,
        }
    }
}
93

94 95 96
pub fn waitpid(pid: usize, exit_code: &mut i32) -> isize {
    loop {
        match sys_waitpid(pid as isize, exit_code as *mut _) {
97 98
            -2 => { yield_(); }
            // -1 or a real pid
99 100 101
            exit_pid => return exit_pid,
        }
    }
102 103 104 105 106 107
}
pub fn sleep(period_ms: usize) {
    let start = sys_get_time();
    while sys_get_time() < start + period_ms as isize {
        sys_yield();
    }
108
}