2018-06-03 19:05:43 +04:00
|
|
|
use alloc::String;
|
2018-05-22 19:48:39 +04:00
|
|
|
use arch::interrupt::*;
|
2018-06-03 19:05:43 +04:00
|
|
|
use memory::{MemoryArea, MemoryAttr, MemorySet};
|
|
|
|
use xmas_elf::{ElfFile, header::HeaderPt2, program::{Flags, ProgramHeader}};
|
2018-04-27 19:10:39 +04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Process {
|
|
|
|
pub(in process) pid: Pid,
|
2018-05-19 12:32:18 +04:00
|
|
|
pub(in process) parent: Pid,
|
2018-05-18 07:49:27 +04:00
|
|
|
pub(in process) name: String,
|
2018-06-02 21:10:20 +04:00
|
|
|
pub(in process) memory_set: MemorySet,
|
2018-04-27 19:10:39 +04:00
|
|
|
pub(in process) status: Status,
|
2018-06-02 16:49:09 +04:00
|
|
|
pub(in process) context: Context,
|
2018-04-27 19:10:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type Pid = usize;
|
2018-05-19 12:32:18 +04:00
|
|
|
pub type ErrorCode = usize;
|
2018-04-27 19:10:39 +04:00
|
|
|
|
2018-05-23 07:20:36 +04:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2018-04-27 19:10:39 +04:00
|
|
|
pub enum Status {
|
2018-05-19 12:32:18 +04:00
|
|
|
Ready,
|
|
|
|
Running,
|
2018-05-20 11:37:48 +04:00
|
|
|
Waiting(Pid),
|
|
|
|
Sleeping,
|
2018-05-19 12:32:18 +04:00
|
|
|
Exited(ErrorCode),
|
2018-04-27 19:10:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Process {
|
|
|
|
/// Make a new kernel thread
|
2018-06-02 21:10:20 +04:00
|
|
|
pub fn new(name: &str, entry: extern fn(usize) -> !, arg: usize) -> Self {
|
2018-07-10 20:53:40 +04:00
|
|
|
let ms = MemorySet::new();
|
2018-07-12 12:42:21 +04:00
|
|
|
let context = unsafe { Context::new_kernel_thread(entry, arg, ms.kstack_top(), ms.token()) };
|
2018-04-27 19:10:39 +04:00
|
|
|
|
|
|
|
Process {
|
|
|
|
pid: 0,
|
2018-05-19 12:32:18 +04:00
|
|
|
parent: 0,
|
2018-05-18 07:49:27 +04:00
|
|
|
name: String::from(name),
|
2018-06-02 21:10:20 +04:00
|
|
|
memory_set: ms,
|
2018-04-27 19:10:39 +04:00
|
|
|
status: Status::Ready,
|
2018-06-02 16:49:09 +04:00
|
|
|
context,
|
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
|
2018-07-12 08:13:39 +04:00
|
|
|
pub fn new_init() -> Self {
|
2018-04-27 19:10:39 +04:00
|
|
|
assert_has_not_been_called!();
|
|
|
|
Process {
|
|
|
|
pid: 0,
|
2018-05-19 12:32:18 +04:00
|
|
|
parent: 0,
|
2018-05-18 07:49:27 +04:00
|
|
|
name: String::from("init"),
|
2018-07-12 08:13:39 +04:00
|
|
|
memory_set: MemorySet::new(),
|
2018-04-27 19:10:39 +04:00
|
|
|
status: Status::Running,
|
2018-06-02 16:49:09 +04:00
|
|
|
context: unsafe { Context::null() }, // will be set at first schedule
|
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-06-02 21:10:20 +04:00
|
|
|
pub fn new_user(data: &[u8]) -> Self {
|
2018-05-12 21:57:49 +04:00
|
|
|
// Parse elf
|
2018-05-18 07:49:27 +04:00
|
|
|
let elf = ElfFile::new(data).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
|
2018-07-12 14:56:29 +04:00
|
|
|
use consts::{USER_STACK_OFFSET, USER_STACK_SIZE, USER32_STACK_OFFSET};
|
2018-05-19 12:32:18 +04:00
|
|
|
let (user_stack_buttom, user_stack_top) = match is32 {
|
2018-07-12 14:56:29 +04:00
|
|
|
true => (USER32_STACK_OFFSET, USER32_STACK_OFFSET + USER_STACK_SIZE),
|
2018-05-17 17:06:13 +04:00
|
|
|
false => (USER_STACK_OFFSET, USER_STACK_OFFSET + USER_STACK_SIZE),
|
|
|
|
};
|
2018-05-12 21:57:49 +04:00
|
|
|
|
|
|
|
// Make page table
|
2018-07-10 20:53:40 +04:00
|
|
|
let mut memory_set = memory_set_from(&elf);
|
2018-06-02 21:10:20 +04:00
|
|
|
memory_set.push(MemoryArea::new(user_stack_buttom, user_stack_top, MemoryAttr::default().user(), "user_stack"));
|
2018-05-19 14:42:08 +04:00
|
|
|
trace!("{:#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
|
2018-07-10 20:53:40 +04:00
|
|
|
unsafe {
|
|
|
|
memory_set.with(|| {
|
|
|
|
for ph in elf.program_iter() {
|
|
|
|
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),
|
|
|
|
};
|
|
|
|
use core::slice;
|
|
|
|
let target = unsafe { slice::from_raw_parts_mut(virt_addr as *mut u8, file_size) };
|
|
|
|
target.copy_from_slice(&data[offset..offset + file_size]);
|
2018-05-17 17:06:13 +04:00
|
|
|
}
|
2018-07-10 20:53:40 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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-07-12 12:42:21 +04:00
|
|
|
let context = unsafe {
|
|
|
|
Context::new_user_thread(
|
|
|
|
entry_addr, user_stack_top - 8, memory_set.kstack_top(), is32, memory_set.token())
|
|
|
|
};
|
2018-05-12 18:57:30 +04:00
|
|
|
|
|
|
|
Process {
|
|
|
|
pid: 0,
|
2018-05-19 12:32:18 +04:00
|
|
|
parent: 0,
|
2018-05-18 07:49:27 +04:00
|
|
|
name: String::new(),
|
2018-06-02 21:10:20 +04:00
|
|
|
memory_set,
|
2018-05-12 18:57:30 +04:00
|
|
|
status: Status::Ready,
|
2018-06-02 16:49:09 +04:00
|
|
|
context,
|
2018-05-12 18:57:30 +04:00
|
|
|
}
|
2018-04-28 06:40:31 +04:00
|
|
|
}
|
2018-05-13 11:06:44 +04:00
|
|
|
|
|
|
|
/// Fork
|
2018-06-02 21:10:20 +04:00
|
|
|
pub fn fork(&self, tf: &TrapFrame) -> Self {
|
2018-05-13 17:13:57 +04:00
|
|
|
// Clone memory set, make a new page table
|
2018-07-10 20:53:40 +04:00
|
|
|
let memory_set = self.memory_set.clone();
|
2018-05-13 17:13:57 +04:00
|
|
|
|
|
|
|
// 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
|
2018-07-10 20:53:40 +04:00
|
|
|
unsafe {
|
|
|
|
memory_set.with(|| {
|
|
|
|
for (area, data) in memory_set.iter().zip(datas.iter()) {
|
|
|
|
unsafe { area.as_slice_mut() }.copy_from_slice(data.as_slice())
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-05-13 17:13:57 +04:00
|
|
|
|
2018-07-12 12:42:21 +04:00
|
|
|
// Push context at kstack top
|
|
|
|
let context = unsafe { Context::new_fork(tf, memory_set.kstack_top(), memory_set.token()) };
|
2018-05-13 17:13:57 +04:00
|
|
|
|
|
|
|
Process {
|
|
|
|
pid: 0,
|
2018-05-19 12:32:18 +04:00
|
|
|
parent: self.pid,
|
2018-05-18 07:49:27 +04:00
|
|
|
name: self.name.clone() + "_fork",
|
2018-06-02 21:10:20 +04:00
|
|
|
memory_set,
|
2018-05-13 17:13:57 +04:00
|
|
|
status: Status::Ready,
|
2018-06-02 16:49:09 +04:00
|
|
|
context,
|
2018-05-13 17:13:57 +04:00
|
|
|
}
|
2018-05-13 11:06:44 +04:00
|
|
|
}
|
2018-05-19 12:32:18 +04:00
|
|
|
|
|
|
|
pub fn exit_code(&self) -> Option<ErrorCode> {
|
|
|
|
match self.status {
|
|
|
|
Status::Exited(code) => Some(code),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2018-05-11 20:22:00 +04:00
|
|
|
}
|
|
|
|
|
2018-07-10 20:53:40 +04:00
|
|
|
fn memory_set_from<'a>(elf: &'a ElfFile<'a>) -> MemorySet {
|
|
|
|
let mut set = MemorySet::new();
|
|
|
|
for ph in elf.program_iter() {
|
|
|
|
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),
|
|
|
|
};
|
|
|
|
set.push(MemoryArea::new(virt_addr, virt_addr + mem_size, memory_attr_from(flags), ""));
|
2018-05-11 20:22:00 +04:00
|
|
|
}
|
2018-07-10 20:53:40 +04:00
|
|
|
set
|
2018-05-12 18:57:30 +04:00
|
|
|
}
|
|
|
|
|
2018-07-10 20:53:40 +04:00
|
|
|
fn memory_attr_from(elf_flags: Flags) -> MemoryAttr {
|
|
|
|
let mut flags = MemoryAttr::default().user();
|
|
|
|
// TODO: handle readonly
|
|
|
|
if elf_flags.is_execute() { flags = flags.execute(); }
|
|
|
|
flags
|
2018-04-27 19:10:39 +04:00
|
|
|
}
|