mirror of
https://github.com/LearningOS/rust-based-os-comp2022.git
synced 2026-11-26 05:43:06 +08:00
add os[1-8]-ref for os refereces, add guide, add README
This commit is contained in:
21
os4-ref/src/syscall/fs.rs
Normal file
21
os4-ref/src/syscall/fs.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
//! File and filesystem-related syscalls
|
||||
|
||||
use crate::mm::translated_byte_buffer;
|
||||
use crate::task::current_user_token;
|
||||
|
||||
const FD_STDOUT: usize = 1;
|
||||
|
||||
pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
|
||||
match fd {
|
||||
FD_STDOUT => {
|
||||
let buffers = translated_byte_buffer(current_user_token(), buf, len);
|
||||
for buffer in buffers {
|
||||
print!("{}", core::str::from_utf8(buffer).unwrap());
|
||||
}
|
||||
len as isize
|
||||
}
|
||||
_ => {
|
||||
panic!("Unsupported fd in sys_write!");
|
||||
}
|
||||
}
|
||||
}
|
||||
42
os4-ref/src/syscall/mod.rs
Normal file
42
os4-ref/src/syscall/mod.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
//! 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_MUNMAP: usize = 215;
|
||||
const SYSCALL_MMAP: usize = 222;
|
||||
const SYSCALL_SET_PRIORITY: usize = 140;
|
||||
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_MMAP => sys_mmap(args[0], args[1], args[2]),
|
||||
SYSCALL_MUNMAP => sys_munmap(args[0], args[1]),
|
||||
SYSCALL_SET_PRIORITY => sys_set_priority(args[0] as isize),
|
||||
SYSCALL_TASK_INFO => sys_task_info(args[0] as *mut TaskInfo),
|
||||
_ => panic!("Unsupported syscall_id: {}", syscall_id),
|
||||
}
|
||||
}
|
||||
62
os4-ref/src/syscall/process.rs
Normal file
62
os4-ref/src/syscall/process.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
//! Process management syscalls
|
||||
|
||||
use crate::config::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,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct TaskInfo {
|
||||
pub status: TaskStatus,
|
||||
pub syscall_times: [u32; MAX_SYSCALL_NUM],
|
||||
pub time: usize,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// YOUR JOB: 引入虚地址后重写 sys_get_time
|
||||
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
|
||||
}
|
||||
|
||||
// CLUE: 从 ch4 开始不再对调度算法进行测试~
|
||||
pub fn sys_set_priority(_prio: isize) -> isize {
|
||||
-1
|
||||
}
|
||||
|
||||
// YOUR JOB: 扩展内核以实现 sys_mmap 和 sys_munmap
|
||||
pub fn sys_mmap(_start: usize, _len: usize, _port: usize) -> isize {
|
||||
-1
|
||||
}
|
||||
|
||||
pub fn sys_munmap(_start: usize, _len: usize) -> isize {
|
||||
-1
|
||||
}
|
||||
|
||||
// YOUR JOB: 引入虚地址后重写 sys_task_info
|
||||
pub fn sys_task_info(ti: *mut TaskInfo) -> isize {
|
||||
-1
|
||||
}
|
||||
Reference in New Issue
Block a user