|
|
|
#![no_std]
|
|
|
|
#![feature(llvm_asm)]
|
|
|
|
#![feature(linkage)]
|
|
|
|
#![feature(panic_info_message)]
|
|
|
|
#![feature(alloc_error_handler)]
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
pub mod console;
|
|
|
|
mod syscall;
|
|
|
|
mod lang_items;
|
|
|
|
|
|
|
|
use syscall::*;
|
|
|
|
use buddy_system_allocator::LockedHeap;
|
|
|
|
|
|
|
|
const USER_HEAP_SIZE: usize = 16384;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
#[link_section = ".text.entry"]
|
|
|
|
pub extern "C" fn _start() -> ! {
|
|
|
|
unsafe {
|
|
|
|
HEAP.lock()
|
|
|
|
.init(HEAP_SPACE.as_ptr() as usize, USER_HEAP_SIZE);
|
|
|
|
}
|
|
|
|
exit(main());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[linkage = "weak"]
|
|
|
|
#[no_mangle]
|
|
|
|
fn main() -> i32 {
|
|
|
|
panic!("Cannot find main!");
|
|
|
|
}
|
|
|
|
|
|
|
|
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) }
|
|
|
|
pub fn exit(xstate: i32) -> ! { sys_exit(xstate); }
|
|
|
|
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() }
|
|
|
|
pub fn exec(path: &str) -> isize { sys_exec(path) }
|
|
|
|
pub fn wait(xstate: &mut i32) -> isize { sys_waitpid(-1, xstate as *mut _) }
|
|
|
|
pub fn waitpid(pid: usize, xstate: &mut i32) -> isize {
|
|
|
|
sys_waitpid(pid as isize, xstate as *mut _)
|
|
|
|
}
|