mirror of
https://github.com/LearningOS/lab0-0-setup-env-run-os1-d0zingcat.git
synced 2026-06-09 07:26:52 +00:00
Initial commit
This commit is contained in:
18
os3-ref/src/syscall/fs.rs
Normal file
18
os3-ref/src/syscall/fs.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
//! File and filesystem-related syscalls
|
||||
|
||||
const FD_STDOUT: usize = 1;
|
||||
|
||||
// YOUR JOB: 修改 sys_write 使之通过测试
|
||||
pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
|
||||
match fd {
|
||||
FD_STDOUT => {
|
||||
let slice = unsafe { core::slice::from_raw_parts(buf, len) };
|
||||
let str = core::str::from_utf8(slice).unwrap();
|
||||
print!("{}", str);
|
||||
len as isize
|
||||
}
|
||||
_ => {
|
||||
panic!("Unsupported fd in sys_write!");
|
||||
}
|
||||
}
|
||||
}
|
||||
36
os3-ref/src/syscall/mod.rs
Normal file
36
os3-ref/src/syscall/mod.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
//! Implementation of syscalls
|
||||
//!
|
||||
//! The single entry point to all system calls, [`syscall()`], is called
|
||||
//! whenever userspace wishes to perform a system call using the `ecall`
|
||||
//! instruction. In this case, the processor raises an 'Environment call from
|
||||
//! U-mode' exception, which is handled as one of the cases in
|
||||
//! [`crate::trap::trap_handler`].
|
||||
//!
|
||||
//! For clarity, each single syscall is implemented as its own function, named
|
||||
//! `sys_` then the name of the syscall. You can find functions like this in
|
||||
//! submodules, and you should also implement syscalls this way.
|
||||
|
||||
const SYSCALL_WRITE: usize = 64;
|
||||
const SYSCALL_EXIT: usize = 93;
|
||||
const SYSCALL_YIELD: usize = 124;
|
||||
const SYSCALL_GET_TIME: usize = 169;
|
||||
const SYSCALL_TASK_INFO: usize = 410;
|
||||
|
||||
mod fs;
|
||||
mod process;
|
||||
|
||||
use fs::*;
|
||||
use process::*;
|
||||
|
||||
/// handle syscall exception with `syscall_id` and other arguments
|
||||
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
|
||||
// LAB1: You may need to update syscall info here.
|
||||
match syscall_id {
|
||||
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
|
||||
SYSCALL_EXIT => sys_exit(args[0] as i32),
|
||||
SYSCALL_YIELD => sys_yield(),
|
||||
SYSCALL_GET_TIME => sys_get_time(args[0] as *mut TimeVal, args[1]),
|
||||
SYSCALL_TASK_INFO => sys_task_info(args[0] as *mut TaskInfo),
|
||||
_ => panic!("Unsupported syscall_id: {}", syscall_id),
|
||||
}
|
||||
}
|
||||
48
os3-ref/src/syscall/process.rs
Normal file
48
os3-ref/src/syscall/process.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
//! Process management syscalls
|
||||
|
||||
use crate::config::{MAX_APP_NUM, MAX_SYSCALL_NUM};
|
||||
use crate::task::{exit_current_and_run_next, suspend_current_and_run_next, TaskStatus};
|
||||
use crate::timer::get_time_us;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct TimeVal {
|
||||
pub sec: usize,
|
||||
pub usec: usize,
|
||||
}
|
||||
|
||||
pub struct TaskInfo {
|
||||
status: TaskStatus,
|
||||
syscall_times: [u32; MAX_SYSCALL_NUM],
|
||||
time: usize,
|
||||
}
|
||||
|
||||
/// task exits and submit an exit code
|
||||
pub fn sys_exit(exit_code: i32) -> ! {
|
||||
info!("[kernel] Application exited with code {}", exit_code);
|
||||
exit_current_and_run_next();
|
||||
panic!("Unreachable in sys_exit!");
|
||||
}
|
||||
|
||||
/// current task gives up resources for other tasks
|
||||
pub fn sys_yield() -> isize {
|
||||
suspend_current_and_run_next();
|
||||
0
|
||||
}
|
||||
|
||||
/// get time with second and microsecond
|
||||
pub fn sys_get_time(ts: *mut TimeVal, _tz: usize) -> isize {
|
||||
let us = get_time_us();
|
||||
unsafe {
|
||||
*ts = TimeVal {
|
||||
sec: us / 1_000_000,
|
||||
usec: us % 1_000_000,
|
||||
};
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
/// YOUR JOB: Finish sys_task_info to pass testcases
|
||||
pub fn sys_task_info(ti: *mut TaskInfo) -> isize {
|
||||
-1
|
||||
}
|
||||
Reference in New Issue
Block a user