2018-04-27 19:10:39 +04:00
|
|
|
use super::*;
|
2018-05-13 17:13:57 +04:00
|
|
|
use memory::{self, Stack, InactivePageTable};
|
2018-05-12 21:57:49 +04:00
|
|
|
use xmas_elf::{ElfFile, program::{Flags, ProgramHeader}, header::HeaderPt2};
|
2018-04-28 06:40:31 +04:00
|
|
|
use core::slice;
|
2018-05-12 07:45:31 +04:00
|
|
|
use alloc::rc::Rc;
|
2018-05-12 21:57:49 +04:00
|
|
|
use rlibc::memcpy;
|
2018-04-27 19:10:39 +04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Process {
|
|
|
|
pub(in process) pid: Pid,
|
|
|
|
name: &'static str,
|
|
|
|
kstack: Stack,
|
2018-05-12 20:47:16 +04:00
|
|
|
pub(in process) memory_set: Option<MemorySet>,
|
|
|
|
pub(in process) page_table: Option<InactivePageTable>,
|
2018-04-27 19:10:39 +04:00
|
|
|
pub(in process) status: Status,
|
|
|
|
pub(in process) rsp: usize,
|
2018-05-12 18:57:30 +04:00
|
|
|
pub(in process) is_user: bool,
|
2018-04-27 19:10:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2018-05-12 18:57:30 +04:00
|
|
|
let tf = TrapFrame::new_kernel_thread(entry, kstack.top());
|
|
|
|
let rsp = kstack.push_at_top(tf);
|
2018-04-27 19:10:39 +04:00
|
|
|
|
|
|
|
Process {
|
|
|
|
pid: 0,
|
|
|
|
name,
|
|
|
|
kstack,
|
2018-05-12 20:47:16 +04:00
|
|
|
memory_set: None,
|
|
|
|
page_table: None,
|
2018-04-27 19:10:39 +04:00
|
|
|
status: Status::Ready,
|
|
|
|
rsp,
|
2018-05-12 18:57:30 +04:00
|
|
|
is_user: false,
|
2018-04-27 19:10:39 +04:00
|
|
|
}
|
|
|
|
}
|
2018-05-13 11:06:44 +04:00
|
|
|
|
2018-04-27 19:10:39 +04:00
|
|
|
/// 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(),
|
2018-05-12 20:47:16 +04:00
|
|
|
memory_set: None,
|
|
|
|
page_table: None,
|
2018-04-27 19:10:39 +04:00
|
|
|
status: Status::Running,
|
|
|
|
rsp: 0, // will be set at first schedule
|
2018-05-12 18:57:30 +04:00
|
|
|
is_user: false,
|
2018-04-27 19:10:39 +04:00
|
|
|
}
|
|
|
|
}
|
2018-04-28 06:40:31 +04:00
|
|
|
|
2018-05-13 11:06:44 +04:00
|
|
|
/// Make a new user thread
|
|
|
|
/// The program elf data is placed at [begin, end)
|
2018-05-17 17:06:13 +04:00
|
|
|
/// uCore x86 32bit program is planned to be supported.
|
2018-05-11 20:22:00 +04:00
|
|
|
pub fn new_user(begin: usize, end: usize, mc: &mut MemoryController) -> Self {
|
2018-05-12 21:57:49 +04:00
|
|
|
// Parse elf
|
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");
|
2018-05-17 17:06:13 +04:00
|
|
|
let is32 = match elf.header.pt2 {
|
|
|
|
HeaderPt2::Header32(_) => true,
|
|
|
|
HeaderPt2::Header64(_) => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
// User stack
|
|
|
|
use consts::{USER_STACK_OFFSET, USER_STACK_SIZE, USER_TCB_OFFSET};
|
|
|
|
let (user_stack_buttom, mut user_stack_top) = match is32 {
|
|
|
|
true => (USER_TCB_OFFSET, USER_TCB_OFFSET + USER_STACK_SIZE),
|
|
|
|
false => (USER_STACK_OFFSET, USER_STACK_OFFSET + USER_STACK_SIZE),
|
|
|
|
};
|
2018-05-12 21:57:49 +04:00
|
|
|
|
|
|
|
// Make page table
|
|
|
|
let mut memory_set = MemorySet::from(&elf);
|
2018-05-17 17:06:13 +04:00
|
|
|
memory_set.push(MemoryArea::new(user_stack_buttom, user_stack_top,
|
2018-05-12 22:30:26 +04:00
|
|
|
EntryFlags::WRITABLE | EntryFlags::NO_EXECUTE | EntryFlags::USER_ACCESSIBLE, "user_stack"));
|
2018-05-13 17:13:57 +04:00
|
|
|
let page_table = mc.make_page_table(&memory_set);
|
2018-05-12 20:47:16 +04:00
|
|
|
debug!("{:#x?}", memory_set);
|
2018-05-12 18:57:30 +04:00
|
|
|
|
2018-05-17 17:06:13 +04:00
|
|
|
let entry_addr = match elf.header.pt2 {
|
|
|
|
HeaderPt2::Header32(header) => header.entry_point as usize,
|
|
|
|
HeaderPt2::Header64(header) => header.entry_point as usize,
|
|
|
|
};
|
|
|
|
|
2018-05-12 21:57:49 +04:00
|
|
|
// Temporary switch to it, in order to copy data
|
|
|
|
let page_table = mc.with(page_table, || {
|
|
|
|
for ph in elf.program_iter() {
|
2018-05-17 17:06:13 +04:00
|
|
|
let (virt_addr, offset, file_size) = match ph {
|
|
|
|
ProgramHeader::Ph32(ph) => (ph.virtual_addr as usize, ph.offset as usize, ph.file_size as usize),
|
|
|
|
ProgramHeader::Ph64(ph) => (ph.virtual_addr as usize, ph.offset as usize, ph.file_size as usize),
|
2018-05-12 21:57:49 +04:00
|
|
|
};
|
2018-05-17 17:06:13 +04:00
|
|
|
unsafe { memcpy(virt_addr as *mut u8, (begin + offset) as *mut u8, file_size) };
|
|
|
|
}
|
|
|
|
if is32 {
|
|
|
|
unsafe {
|
|
|
|
// TODO: full argc & argv
|
|
|
|
*(user_stack_top as *mut u32).offset(-1) = 0; // argv
|
|
|
|
*(user_stack_top as *mut u32).offset(-2) = 0; // argc
|
|
|
|
user_stack_top -= 8;
|
|
|
|
}
|
2018-05-12 21:57:49 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-12 18:57:30 +04:00
|
|
|
|
2018-05-12 21:57:49 +04:00
|
|
|
// Allocate kernel stack and push trap frame
|
2018-05-12 18:57:30 +04:00
|
|
|
let kstack = mc.alloc_stack(7).unwrap();
|
2018-05-17 17:06:13 +04:00
|
|
|
let tf = TrapFrame::new_user_thread(entry_addr, user_stack_top, is32);
|
2018-05-12 18:57:30 +04:00
|
|
|
let rsp = kstack.push_at_top(tf);
|
2018-05-17 17:06:13 +04:00
|
|
|
debug!("rsp = {:#x}", rsp);
|
2018-05-12 18:57:30 +04:00
|
|
|
|
|
|
|
Process {
|
|
|
|
pid: 0,
|
|
|
|
name: "user",
|
|
|
|
kstack,
|
2018-05-12 20:47:16 +04:00
|
|
|
memory_set: Some(memory_set),
|
|
|
|
page_table: Some(page_table),
|
2018-05-12 18:57:30 +04:00
|
|
|
status: Status::Ready,
|
|
|
|
rsp,
|
|
|
|
is_user: true,
|
|
|
|
}
|
2018-04-28 06:40:31 +04:00
|
|
|
}
|
2018-05-13 11:06:44 +04:00
|
|
|
|
|
|
|
/// Fork
|
2018-05-13 17:13:57 +04:00
|
|
|
pub fn fork(&self, tf: &TrapFrame, mc: &mut MemoryController) -> Self {
|
2018-05-13 11:06:44 +04:00
|
|
|
assert!(self.is_user);
|
2018-05-13 17:13:57 +04:00
|
|
|
|
|
|
|
// Clone memory set, make a new page table
|
|
|
|
let memory_set = self.memory_set.as_ref().unwrap().clone();
|
|
|
|
let page_table = mc.make_page_table(&memory_set);
|
|
|
|
|
|
|
|
// Copy data to temp space
|
|
|
|
use alloc::Vec;
|
|
|
|
let datas: Vec<Vec<u8>> = memory_set.iter().map(|area| {
|
|
|
|
Vec::from(unsafe { area.as_slice() })
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
// Temporary switch to it, in order to copy data
|
|
|
|
let page_table = mc.with(page_table, || {
|
|
|
|
for (area, data) in memory_set.iter().zip(datas.iter()) {
|
|
|
|
unsafe { area.as_slice_mut() }.copy_from_slice(data.as_slice())
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Allocate kernel stack and push trap frame
|
|
|
|
let kstack = mc.alloc_stack(7).unwrap();
|
|
|
|
let mut tf = tf.clone();
|
|
|
|
tf.scratch.rax = 1;
|
|
|
|
let rsp = kstack.push_at_top(tf);
|
|
|
|
|
|
|
|
Process {
|
|
|
|
pid: 0,
|
|
|
|
name: "fork",
|
|
|
|
kstack,
|
|
|
|
memory_set: Some(memory_set),
|
|
|
|
page_table: Some(page_table),
|
|
|
|
status: Status::Ready,
|
|
|
|
rsp,
|
|
|
|
is_user: true,
|
|
|
|
}
|
2018-05-13 11:06:44 +04:00
|
|
|
}
|
2018-05-11 20:22:00 +04:00
|
|
|
}
|
|
|
|
|
2018-05-12 18:57:30 +04:00
|
|
|
use memory::{MemorySet, MemoryArea, PhysAddr, FromToVirtualAddress, EntryFlags};
|
2018-05-11 20:22:00 +04:00
|
|
|
|
2018-05-12 21:57:49 +04:00
|
|
|
impl<'a> From<&'a ElfFile<'a>> for MemorySet {
|
|
|
|
fn from(elf: &'a ElfFile<'a>) -> Self {
|
2018-05-12 07:45:31 +04:00
|
|
|
let mut set = MemorySet::new();
|
|
|
|
for ph in elf.program_iter() {
|
2018-05-17 17:06:13 +04:00
|
|
|
let (virt_addr, mem_size, flags) = match ph {
|
|
|
|
ProgramHeader::Ph32(ph) => (ph.virtual_addr as usize, ph.mem_size as usize, ph.flags),
|
|
|
|
ProgramHeader::Ph64(ph) => (ph.virtual_addr as usize, ph.mem_size as usize, ph.flags),
|
2018-05-12 18:57:30 +04:00
|
|
|
};
|
2018-05-17 17:06:13 +04:00
|
|
|
set.push(MemoryArea::new(virt_addr, virt_addr + mem_size, EntryFlags::from(flags), ""));
|
2018-05-11 20:22:00 +04:00
|
|
|
}
|
2018-05-12 07:45:31 +04:00
|
|
|
set
|
2018-05-11 20:22:00 +04:00
|
|
|
}
|
2018-05-12 18:57:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Flags> for EntryFlags {
|
|
|
|
fn from(elf_flags: Flags) -> Self {
|
2018-05-12 20:47:16 +04:00
|
|
|
let mut flags = EntryFlags::PRESENT | EntryFlags::USER_ACCESSIBLE;
|
2018-05-17 17:06:13 +04:00
|
|
|
// if elf_flags.is_write() {
|
|
|
|
// TODO: handle readonly
|
|
|
|
if true {
|
2018-05-12 18:57:30 +04:00
|
|
|
flags = flags | EntryFlags::WRITABLE;
|
|
|
|
}
|
|
|
|
if !elf_flags.is_execute() {
|
|
|
|
flags = flags | EntryFlags::NO_EXECUTE;
|
|
|
|
}
|
|
|
|
flags
|
|
|
|
}
|
2018-04-27 19:10:39 +04:00
|
|
|
}
|