2018-04-27 19:10:39 +04:00
|
|
|
|
use memory::MemoryController;
|
|
|
|
|
use spin::{Once, Mutex};
|
2018-05-13 11:06:44 +04:00
|
|
|
|
use core::slice;
|
2018-04-27 19:10:39 +04:00
|
|
|
|
|
|
|
|
|
use self::process::*;
|
|
|
|
|
use self::processor::*;
|
|
|
|
|
|
|
|
|
|
mod process;
|
|
|
|
|
mod processor;
|
|
|
|
|
|
|
|
|
|
/// 平台相关依赖:struct TrapFrame
|
|
|
|
|
///
|
|
|
|
|
/// ## 必须实现的特性
|
|
|
|
|
///
|
|
|
|
|
/// * Debug: 用于Debug输出
|
|
|
|
|
use arch::interrupt::TrapFrame;
|
|
|
|
|
|
2018-04-28 06:40:31 +04:00
|
|
|
|
// TODO: 使用宏来更优雅地导入符号,现在会有编译错误
|
|
|
|
|
//
|
|
|
|
|
// #![feature(concat_idents)]
|
|
|
|
|
//
|
|
|
|
|
// macro_rules! binary_symbol {
|
|
|
|
|
// ($name: ident) => {
|
|
|
|
|
// extern {
|
|
|
|
|
// fn concat_idents!(_binary_user_, $name, _start)();
|
|
|
|
|
// fn concat_idents!(_binary_user_, $name, _end)();
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// binary_symbol!(forktest);
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "link_user_program")]
|
|
|
|
|
extern {
|
|
|
|
|
fn _binary_user_forktest_start();
|
|
|
|
|
fn _binary_user_forktest_end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-04-27 19:10:39 +04:00
|
|
|
|
pub fn init(mc: &mut MemoryController) {
|
|
|
|
|
PROCESSOR.call_once(|| {Mutex::new({
|
2018-05-13 11:06:44 +04:00
|
|
|
|
let mut processor = Processor::new();
|
2018-04-27 19:10:39 +04:00
|
|
|
|
let initproc = Process::new_init(mc);
|
|
|
|
|
let idleproc = Process::new("idle", idle_thread, mc);
|
2018-04-28 06:40:31 +04:00
|
|
|
|
#[cfg(feature = "link_user_program")]
|
|
|
|
|
let forktest = Process::new_user(_binary_user_forktest_start as usize,
|
2018-05-11 20:22:00 +04:00
|
|
|
|
_binary_user_forktest_end as usize, mc);
|
2018-04-27 19:10:39 +04:00
|
|
|
|
processor.add(initproc);
|
|
|
|
|
processor.add(idleproc);
|
2018-05-12 18:57:30 +04:00
|
|
|
|
processor.add(forktest);
|
2018-04-27 19:10:39 +04:00
|
|
|
|
processor
|
|
|
|
|
})});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PROCESSOR: Once<Mutex<Processor>> = Once::new();
|
|
|
|
|
|
|
|
|
|
/// Called by timer handler in arch
|
|
|
|
|
/// 设置rsp,指向接下来要执行线程的 内核栈顶
|
|
|
|
|
/// 之后中断处理例程会重置rsp,恢复对应线程的上下文
|
|
|
|
|
pub fn schedule(rsp: &mut usize) {
|
|
|
|
|
PROCESSOR.try().unwrap().lock().schedule(rsp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern fn idle_thread() {
|
|
|
|
|
loop {
|
|
|
|
|
println!("idle ...");
|
|
|
|
|
let mut i = 0;
|
|
|
|
|
while i < 1 << 22 {
|
|
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-12 23:41:41 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn fork(tf: &TrapFrame) {
|
|
|
|
|
unimplemented!()
|
2018-04-27 19:10:39 +04:00
|
|
|
|
}
|