lab 03
This commit is contained in:
parent
5ab1ca507c
commit
8b2f8bf2a3
9 changed files with 140 additions and 23 deletions
|
@ -5,6 +5,7 @@ pub const KERNEL_STACK_SIZE: usize = 4096 * 2;
|
|||
pub const MAX_APP_NUM: usize = 4;
|
||||
pub const APP_BASE_ADDRESS: usize = 0x80400000;
|
||||
pub const APP_SIZE_LIMIT: usize = 0x20000;
|
||||
pub const MAX_SYSCALL_NUM: usize = 500;
|
||||
|
||||
/*
|
||||
#[cfg(feature = "board_k210")]
|
||||
|
|
|
@ -14,6 +14,7 @@ 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;
|
||||
|
@ -21,13 +22,17 @@ mod process;
|
|||
use fs::*;
|
||||
use process::*;
|
||||
|
||||
use crate::task::TASK_MANAGER;
|
||||
|
||||
/// handle syscall exception with `syscall_id` and other arguments
|
||||
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
|
||||
TASK_MANAGER.mark_syscall(syscall_id);
|
||||
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(),
|
||||
SYSCALL_TASK_INFO => sys_task_info(args[0] as *mut TaskInfo),
|
||||
_ => panic!("Unsupported syscall_id: {}", syscall_id),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,20 @@
|
|||
//! Process management syscalls
|
||||
use crate::task::{exit_current_and_run_next, suspend_current_and_run_next};
|
||||
use crate::timer::get_time_ms;
|
||||
use log::trace;
|
||||
|
||||
use crate::task::{exit_current_and_run_next, suspend_current_and_run_next, TaskStatus, TASK_MANAGER};
|
||||
use crate::timer::{get_time, get_time_ms};
|
||||
use crate::config::MAX_SYSCALL_NUM;
|
||||
|
||||
/// Task information
|
||||
#[allow(dead_code)]
|
||||
pub struct TaskInfo {
|
||||
/// Task status in it's life cycle
|
||||
status: TaskStatus,
|
||||
/// The numbers of syscall called by task
|
||||
syscall_times: [u32; MAX_SYSCALL_NUM],
|
||||
/// Total running time of task
|
||||
time: usize,
|
||||
}
|
||||
|
||||
/// task exits and submit an exit code
|
||||
pub fn sys_exit(exit_code: i32) -> ! {
|
||||
|
@ -19,3 +33,14 @@ pub fn sys_yield() -> isize {
|
|||
pub fn sys_get_time() -> isize {
|
||||
get_time_ms() as isize
|
||||
}
|
||||
|
||||
/// YOUR JOB: Finish sys_task_info to pass testcases
|
||||
pub fn sys_task_info(ti: *mut TaskInfo) -> isize {
|
||||
unsafe {
|
||||
(*ti).status = TASK_MANAGER.get_current_status();
|
||||
(*ti).time = get_time();
|
||||
(*ti).syscall_times = TASK_MANAGER.get_current_syscall_times()
|
||||
}
|
||||
trace!("kernel: sys_task_info");
|
||||
0
|
||||
}
|
||||
|
|
|
@ -15,13 +15,13 @@ mod switch;
|
|||
#[allow(clippy::module_inception)]
|
||||
mod task;
|
||||
|
||||
use crate::config::MAX_APP_NUM;
|
||||
use crate::config::{MAX_APP_NUM, MAX_SYSCALL_NUM};
|
||||
use crate::loader::{get_num_app, init_app_cx};
|
||||
use crate::sbi::shutdown;
|
||||
use crate::sync::UPSafeCell;
|
||||
use lazy_static::*;
|
||||
use switch::__switch;
|
||||
use task::{TaskControlBlock, TaskStatus};
|
||||
pub use task::{TaskControlBlock, TaskStatus};
|
||||
|
||||
pub use context::TaskContext;
|
||||
|
||||
|
@ -56,6 +56,7 @@ lazy_static! {
|
|||
let mut tasks = [TaskControlBlock {
|
||||
task_cx: TaskContext::zero_init(),
|
||||
task_status: TaskStatus::UnInit,
|
||||
syscall_times: [0; MAX_SYSCALL_NUM]
|
||||
}; MAX_APP_NUM];
|
||||
for (i, task) in tasks.iter_mut().enumerate() {
|
||||
task.task_cx = TaskContext::goto_restore(init_app_cx(i));
|
||||
|
@ -138,6 +139,36 @@ impl TaskManager {
|
|||
shutdown(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// get current task id
|
||||
pub fn get_current_id(&self) -> usize {
|
||||
let inner = self.inner.exclusive_access();
|
||||
inner.current_task
|
||||
}
|
||||
|
||||
/// get current task status
|
||||
pub fn get_current_status(&self) -> TaskStatus {
|
||||
let inner = self.inner.exclusive_access();
|
||||
let current = inner.current_task;
|
||||
inner.tasks[current].task_status
|
||||
}
|
||||
|
||||
/// get current task syscall times
|
||||
pub fn get_current_syscall_times(&self) -> [u32; MAX_SYSCALL_NUM] {
|
||||
let inner = self.inner.exclusive_access();
|
||||
let current = inner.current_task;
|
||||
return inner.tasks[current].syscall_times.clone();
|
||||
}
|
||||
|
||||
/// record syscall
|
||||
pub fn mark_syscall(&self, syscall_id: usize) {
|
||||
if syscall_id >= MAX_SYSCALL_NUM {
|
||||
return;
|
||||
}
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
let current = inner.current_task;
|
||||
inner.tasks[current].syscall_times[syscall_id] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// run first task
|
||||
|
|
|
@ -1,17 +1,29 @@
|
|||
//! Types related to task management
|
||||
|
||||
use crate::config::MAX_SYSCALL_NUM;
|
||||
|
||||
use super::TaskContext;
|
||||
|
||||
/// The task control block (TCB) of a task.
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct TaskControlBlock {
|
||||
/// The task status in it's lifecycle
|
||||
pub task_status: TaskStatus,
|
||||
/// The task context
|
||||
pub task_cx: TaskContext,
|
||||
/// The numbers of syscall called by task
|
||||
pub syscall_times: [u32; MAX_SYSCALL_NUM],
|
||||
}
|
||||
|
||||
/// The status of a task
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
pub enum TaskStatus {
|
||||
/// uninitialized
|
||||
UnInit,
|
||||
/// ready to run
|
||||
Ready,
|
||||
/// running
|
||||
Running,
|
||||
/// exited
|
||||
Exited,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue