2018-04-27 19:10:39 +04:00
|
|
|
use super::*;
|
|
|
|
use memory::Stack;
|
2018-04-28 06:40:31 +04:00
|
|
|
use xmas_elf::ElfFile;
|
|
|
|
use core::slice;
|
2018-04-27 19:10:39 +04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Process {
|
|
|
|
pub(in process) pid: Pid,
|
|
|
|
name: &'static str,
|
|
|
|
kstack: Stack,
|
|
|
|
// page_table: Box<PageTable>,
|
|
|
|
pub(in process) status: Status,
|
|
|
|
pub(in process) rsp: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Pid = usize;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Status {
|
|
|
|
Ready, Running, Sleeping(usize), Exited
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Process {
|
|
|
|
/// Make a new kernel thread
|
|
|
|
pub fn new(name: &'static str, entry: extern fn(), mc: &mut MemoryController) -> Self {
|
|
|
|
let kstack = mc.alloc_stack(7).unwrap();
|
|
|
|
let rsp = unsafe{ (kstack.top() as *mut TrapFrame).offset(-1) } as usize;
|
|
|
|
|
|
|
|
let tf = unsafe{ &mut *(rsp as *mut TrapFrame) };
|
|
|
|
*tf = TrapFrame::new_kernel_thread(entry, kstack.top());
|
|
|
|
|
|
|
|
Process {
|
|
|
|
pid: 0,
|
|
|
|
name,
|
|
|
|
kstack,
|
|
|
|
status: Status::Ready,
|
|
|
|
rsp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Make the first kernel thread `initproc`
|
|
|
|
/// Should be called only once
|
|
|
|
pub fn new_init(mc: &mut MemoryController) -> Self {
|
|
|
|
assert_has_not_been_called!();
|
|
|
|
Process {
|
|
|
|
pid: 0,
|
|
|
|
name: "init",
|
|
|
|
kstack: mc.kernel_stack.take().unwrap(),
|
|
|
|
status: Status::Running,
|
|
|
|
rsp: 0, // will be set at first schedule
|
|
|
|
}
|
|
|
|
}
|
2018-04-28 06:40:31 +04:00
|
|
|
|
2018-05-11 20:22:00 +04:00
|
|
|
pub fn new_user(begin: usize, end: usize, mc: &mut MemoryController) -> Self {
|
2018-04-28 06:40:31 +04:00
|
|
|
let slice = unsafe{ slice::from_raw_parts(begin as *const u8, end - begin) };
|
|
|
|
let elf = ElfFile::new(slice).expect("failed to read elf");
|
|
|
|
for program_header in elf.program_iter() {
|
|
|
|
println!("{:?}", program_header);
|
|
|
|
}
|
2018-05-11 20:22:00 +04:00
|
|
|
for section in elf.section_iter() {
|
|
|
|
println!("{:?}", section);
|
|
|
|
}
|
2018-04-28 06:40:31 +04:00
|
|
|
unimplemented!();
|
|
|
|
}
|
2018-05-11 20:22:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
use memory::{MemorySet, MemoryArea};
|
|
|
|
|
|
|
|
fn new_memory_set_from_elf(elf: ElfFile, mc: &mut MemoryController) -> MemorySet {
|
|
|
|
use xmas_elf::program::ProgramHeader;
|
|
|
|
|
|
|
|
let mut set = MemorySet::new(mc);
|
|
|
|
for ph in elf.program_iter() {
|
|
|
|
match ph {
|
|
|
|
ProgramHeader::Ph32(ph) => unimplemented!(),
|
|
|
|
ProgramHeader::Ph64(ph) => {
|
|
|
|
set.push(MemoryArea {
|
|
|
|
start_addr: ph.virtual_addr as usize,
|
|
|
|
end_addr: (ph.virtual_addr + ph.mem_size) as usize,
|
|
|
|
flags: ph.flags.0, // TODO: handle it
|
|
|
|
name: "",
|
|
|
|
mapped: false,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set
|
2018-04-27 19:10:39 +04:00
|
|
|
}
|