1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-27 10:13:28 +04:00
rCore/kernel/src/process/structs.rs

450 lines
15 KiB
Rust
Raw Normal View History

2019-03-12 09:38:20 +04:00
use alloc::{boxed::Box, collections::BTreeMap, string::String, sync::Arc, vec::Vec, sync::Weak};
use core::fmt;
2018-12-13 22:37:51 +04:00
use log::*;
2019-03-10 11:23:15 +04:00
use spin::{Mutex, RwLock};
2018-12-17 19:54:13 +04:00
use xmas_elf::{ElfFile, header, program::{Flags, Type}};
use smoltcp::socket::SocketHandle;
use smoltcp::wire::IpEndpoint;
use rcore_memory::PAGE_SIZE;
2019-03-10 11:23:15 +04:00
use rcore_thread::Tid;
2019-01-25 18:58:49 +04:00
use crate::arch::interrupt::{Context, TrapFrame};
2018-12-17 19:54:13 +04:00
use crate::memory::{ByFrame, GlobalFrameAlloc, KernelStack, MemoryAttr, MemorySet};
use crate::fs::{FileHandle, OpenOptions};
use crate::sync::Condvar;
use crate::drivers::NET_DRIVERS;
use super::abi::{self, ProcInitInfo};
2018-11-15 19:41:22 +04:00
// TODO: avoid pub
2019-01-25 18:58:49 +04:00
pub struct Thread {
pub context: Context,
pub kstack: KernelStack,
/// Kernel performs futex wake when thread exits.
/// Ref: [http://man7.org/linux/man-pages/man2/set_tid_address.2.html]
pub clear_child_tid: usize,
2019-01-25 18:58:49 +04:00
pub proc: Arc<Mutex<Process>>,
}
#[derive(Clone, Debug)]
pub struct TcpSocketState {
pub local_endpoint: Option<IpEndpoint>, // save local endpoint for bind()
pub is_listening: bool,
}
2019-03-12 09:38:20 +04:00
#[derive(Clone, Debug)]
pub struct UdpSocketState {
pub remote_endpoint: Option<IpEndpoint>, // remember remote endpoint for connect(0)
}
#[derive(Clone, Debug)]
pub enum SocketType {
Raw,
Tcp(TcpSocketState),
2019-03-12 09:38:20 +04:00
Udp(UdpSocketState),
Icmp
}
2019-03-09 20:48:04 +04:00
#[derive(Debug)]
pub struct SocketWrapper {
pub handle: SocketHandle,
pub socket_type: SocketType,
}
#[derive(Clone)]
pub enum FileLike {
File(FileHandle),
Socket(SocketWrapper)
}
impl fmt::Debug for FileLike {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
FileLike::File(_) => write!(f, "File"),
FileLike::Socket(wrapper) => {
match wrapper.socket_type {
SocketType::Raw => write!(f, "RawSocket"),
SocketType::Tcp(_) => write!(f, "TcpSocket"),
2019-03-12 09:38:20 +04:00
SocketType::Udp(_) => write!(f, "UdpSocket"),
SocketType::Icmp => write!(f, "IcmpSocket"),
}
},
}
}
}
2019-03-10 11:23:15 +04:00
/// Pid type
/// For strong type separation
2019-03-10 21:06:44 +04:00
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
2019-03-10 11:23:15 +04:00
pub struct Pid(Option<usize>);
impl Pid {
pub fn uninitialized() -> Self {
Pid(None)
}
/// Return if it was uninitialized before this call
/// When returning true, it usually means this is the first thread
pub fn set_if_uninitialized(&mut self, tid: Tid) -> bool {
if self.0 == None {
self.0 = Some(tid as usize);
true
} else {
false
}
}
pub fn get(&self) -> usize {
self.0.unwrap()
}
/// Return whether this pid represents the init process
pub fn is_init(&self) -> bool {
self.0 == Some(0)
}
2019-03-10 11:23:15 +04:00
}
impl fmt::Display for Pid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
Some(pid) => write!(f, "{}", pid),
None => write!(f, "None"),
}
}
}
2018-12-13 22:37:51 +04:00
pub struct Process {
2019-03-10 21:06:44 +04:00
// resources
2018-11-15 19:41:22 +04:00
pub memory_set: MemorySet,
pub files: BTreeMap<usize, FileLike>,
pub cwd: String,
2019-03-10 21:06:44 +04:00
futexes: BTreeMap<usize, Arc<Condvar>>,
// relationship
2019-03-10 11:23:15 +04:00
pub pid: Pid, // i.e. tgid, usually the tid of first thread
2019-03-10 21:06:44 +04:00
pub parent: Option<Arc<Mutex<Process>>>,
pub children: Vec<Weak<Mutex<Process>>>,
2019-03-10 11:23:15 +04:00
pub threads: Vec<Tid>, // threads in the same process
2019-03-10 21:06:44 +04:00
// for waiting child
pub child_exit: Arc<Condvar>, // notified when the a child process is going to terminate
pub child_exit_code: BTreeMap<usize, usize>, // child process store its exit code here
}
2019-03-10 11:23:15 +04:00
/// Records the mapping between pid and Process struct.
lazy_static! {
pub static ref PROCESSES: RwLock<BTreeMap<usize, Weak<Mutex<Process>>>> = RwLock::new(BTreeMap::new());
}
2019-01-25 18:58:49 +04:00
/// Let `rcore_thread` can switch between our `Thread`
impl rcore_thread::Context for Thread {
unsafe fn switch_to(&mut self, target: &mut rcore_thread::Context) {
2018-11-03 17:45:03 +04:00
use core::mem::transmute;
2019-01-25 18:58:49 +04:00
let (target, _): (&mut Thread, *const ()) = transmute(target);
self.context.switch(&mut target.context);
2018-07-17 15:06:30 +04:00
}
2019-03-10 11:23:15 +04:00
fn set_tid(&mut self, tid: Tid) {
// set pid=tid if unspecified
let mut proc = self.proc.lock();
if proc.pid.set_if_uninitialized(tid) {
// first thread in the process
// link to its ppid
2019-03-10 21:06:44 +04:00
if let Some(parent) = &proc.parent {
let mut parent = parent.lock();
parent.children.push(Arc::downgrade(&self.proc));
2019-03-10 11:23:15 +04:00
}
}
// add it to threads
proc.threads.push(tid);
PROCESSES.write().insert(proc.pid.get(), Arc::downgrade(&self.proc));
}
2018-07-16 21:56:28 +04:00
}
2019-01-25 18:58:49 +04:00
impl Thread {
/// Make a struct for the init thread
2019-03-10 21:06:44 +04:00
/// TODO: remove this, we only need `Context::null()`
2019-01-25 18:58:49 +04:00
pub unsafe fn new_init() -> Box<Thread> {
Box::new(Thread {
context: Context::null(),
2018-11-03 17:45:03 +04:00
kstack: KernelStack::new(),
clear_child_tid: 0,
2019-01-25 18:58:49 +04:00
proc: Arc::new(Mutex::new(Process {
memory_set: MemorySet::new(),
files: BTreeMap::default(),
2019-03-02 16:25:30 +04:00
cwd: String::from("/"),
futexes: BTreeMap::default(),
2019-03-10 11:23:15 +04:00
pid: Pid::uninitialized(),
2019-03-10 21:06:44 +04:00
parent: None,
children: Vec::new(),
2019-03-10 11:23:15 +04:00
threads: Vec::new(),
2019-03-10 21:06:44 +04:00
child_exit: Arc::new(Condvar::new()),
child_exit_code: BTreeMap::new(),
2019-01-25 18:58:49 +04:00
})),
2018-11-03 17:45:03 +04:00
})
}
2019-01-25 18:58:49 +04:00
/// Make a new kernel thread starting from `entry` with `arg`
pub fn new_kernel(entry: extern fn(usize) -> !, arg: usize) -> Box<Thread> {
2018-11-03 17:45:03 +04:00
let memory_set = MemorySet::new();
let kstack = KernelStack::new();
2019-01-25 18:58:49 +04:00
Box::new(Thread {
context: unsafe { Context::new_kernel_thread(entry, arg, kstack.top(), memory_set.token()) },
2018-11-03 17:45:03 +04:00
kstack,
clear_child_tid: 0,
2019-03-10 21:06:44 +04:00
// TODO: kernel thread should not have a process
2019-01-25 18:58:49 +04:00
proc: Arc::new(Mutex::new(Process {
memory_set,
files: BTreeMap::default(),
2019-03-02 16:25:30 +04:00
cwd: String::from("/"),
futexes: BTreeMap::default(),
2019-03-10 11:23:15 +04:00
pid: Pid::uninitialized(),
2019-03-10 21:06:44 +04:00
parent: None,
children: Vec::new(),
2019-03-10 11:23:15 +04:00
threads: Vec::new(),
2019-03-10 21:06:44 +04:00
child_exit: Arc::new(Condvar::new()),
child_exit_code: BTreeMap::new()
2019-01-25 18:58:49 +04:00
})),
2018-11-03 17:45:03 +04:00
})
}
2019-01-25 18:58:49 +04:00
/// Make a new user process from ELF `data`
pub fn new_user<'a, Iter>(data: &[u8], args: Iter) -> Box<Thread>
2018-11-15 19:41:22 +04:00
where Iter: Iterator<Item=&'a str>
{
// Parse elf
let elf = ElfFile::new(data).expect("failed to read elf");
2018-05-17 17:06:13 +04:00
let is32 = match elf.header.pt2 {
2018-07-14 07:56:55 +04:00
header::HeaderPt2::Header32(_) => true,
header::HeaderPt2::Header64(_) => false,
2018-05-17 17:06:13 +04:00
};
2018-11-26 21:23:07 +04:00
match elf.header.pt2.type_().as_type() {
header::Type::Executable => {
// #[cfg(feature = "no_mmu")]
// panic!("ELF is not shared object");
},
header::Type::SharedObject => {},
_ => panic!("ELF is not executable or shared object"),
}
// Make page table
let (mut memory_set, entry_addr) = memory_set_from(&elf);
2018-11-26 21:23:07 +04:00
// User stack
use crate::consts::{USER_STACK_OFFSET, USER_STACK_SIZE, USER32_STACK_OFFSET};
#[cfg(not(feature = "no_mmu"))]
let mut ustack_top = {
let (ustack_buttom, ustack_top) = match is32 {
true => (USER32_STACK_OFFSET, USER32_STACK_OFFSET + USER_STACK_SIZE),
false => (USER_STACK_OFFSET, USER_STACK_OFFSET + USER_STACK_SIZE),
};
memory_set.push(ustack_buttom, ustack_top, MemoryAttr::default().user(), ByFrame::new(GlobalFrameAlloc), "user_stack");
2018-11-26 21:23:07 +04:00
ustack_top
};
#[cfg(feature = "no_mmu")]
let mut ustack_top = memory_set.push(USER_STACK_SIZE).as_ptr() as usize + USER_STACK_SIZE;
2018-05-17 17:06:13 +04:00
let init_info = ProcInitInfo {
args: args.map(|s| String::from(s)).collect(),
envs: BTreeMap::new(),
auxv: {
let mut map = BTreeMap::new();
if let Some(phdr) = elf.program_iter()
.find(|ph| ph.get_type() == Ok(Type::Phdr)) {
// if phdr exists in program header, use it
map.insert(abi::AT_PHDR, phdr.virtual_addr() as usize);
} else if let Some(elf_addr) = elf.program_iter().find(|ph| ph.get_type() == Ok(Type::Load) && ph.offset() == 0) {
// otherwise, check if elf is loaded from the beginning, then phdr can be inferred.
map.insert(abi::AT_PHDR, elf_addr.virtual_addr() as usize + elf.header.pt2.ph_offset() as usize);
} else {
warn!("new_user: no phdr found, tls might not work");
}
map.insert(abi::AT_PHENT, elf.header.pt2.ph_entry_size() as usize);
map.insert(abi::AT_PHNUM, elf.header.pt2.ph_count() as usize);
map.insert(abi::AT_PAGESZ, PAGE_SIZE);
map
},
};
2018-07-10 20:53:40 +04:00
unsafe {
memory_set.with(|| { ustack_top = init_info.push_at(ustack_top) });
2018-07-10 20:53:40 +04:00
}
2018-11-26 21:23:07 +04:00
trace!("{:#x?}", memory_set);
2018-11-03 17:45:03 +04:00
let kstack = KernelStack::new();
2018-11-05 15:31:45 +04:00
let mut files = BTreeMap::new();
files.insert(0, FileLike::File(FileHandle::new(crate::fs::STDIN.clone(), OpenOptions { read: true, write: false, append: false })));
files.insert(1, FileLike::File(FileHandle::new(crate::fs::STDOUT.clone(), OpenOptions { read: false, write: true, append: false })));
files.insert(2, FileLike::File(FileHandle::new(crate::fs::STDOUT.clone(), OpenOptions { read: false, write: true, append: false })));
2019-01-25 18:58:49 +04:00
Box::new(Thread {
context: unsafe {
Context::new_user_thread(
entry_addr, ustack_top, kstack.top(), is32, memory_set.token())
2018-07-16 20:23:02 +04:00
},
2018-11-03 17:45:03 +04:00
kstack,
clear_child_tid: 0,
2019-01-25 18:58:49 +04:00
proc: Arc::new(Mutex::new(Process {
memory_set,
files,
2019-03-02 16:25:30 +04:00
cwd: String::from("/"),
futexes: BTreeMap::default(),
2019-03-10 11:23:15 +04:00
pid: Pid::uninitialized(),
2019-03-10 21:06:44 +04:00
parent: None,
children: Vec::new(),
2019-03-10 11:23:15 +04:00
threads: Vec::new(),
2019-03-10 21:06:44 +04:00
child_exit: Arc::new(Condvar::new()),
child_exit_code: BTreeMap::new()
2019-01-25 18:58:49 +04:00
})),
})
}
2018-05-13 11:06:44 +04:00
2019-01-25 18:58:49 +04:00
/// Fork a new process from current one
pub fn fork(&self, tf: &TrapFrame) -> Box<Thread> {
// Clone memory set, make a new page table
2019-01-25 18:58:49 +04:00
let memory_set = self.proc.lock().memory_set.clone();
let files = self.proc.lock().files.clone();
let cwd = self.proc.lock().cwd.clone();
2019-03-10 21:06:44 +04:00
let parent = Some(self.proc.clone());
debug!("fork: finish clone MemorySet");
2018-11-26 21:23:07 +04:00
// MMU: copy data to the new space
// NoMMU: coping data has been done in `memory_set.clone()`
#[cfg(not(feature = "no_mmu"))]
for area in memory_set.iter() {
let data = Vec::<u8>::from(unsafe { area.as_slice() });
unsafe { memory_set.with(|| {
area.as_slice_mut().copy_from_slice(data.as_slice())
}) }
2018-07-10 20:53:40 +04:00
}
debug!("fork: temporary copy data!");
2018-11-03 17:45:03 +04:00
let kstack = KernelStack::new();
let iface = &*(NET_DRIVERS.read()[0]);
let mut sockets = iface.sockets();
for (_fd, file) in files.iter() {
if let FileLike::Socket(wrapper) = file {
sockets.retain(wrapper.handle);
}
}
2019-01-25 18:58:49 +04:00
Box::new(Thread {
2019-03-08 20:47:02 +04:00
context: unsafe { Context::new_fork(tf, kstack.top(), memory_set.token()) },
2018-11-03 17:45:03 +04:00
kstack,
clear_child_tid: 0,
2019-01-25 18:58:49 +04:00
proc: Arc::new(Mutex::new(Process {
memory_set,
files,
cwd,
futexes: BTreeMap::default(),
2019-03-10 11:23:15 +04:00
pid: Pid::uninitialized(),
2019-03-10 21:06:44 +04:00
parent,
children: Vec::new(),
2019-03-10 11:23:15 +04:00
threads: Vec::new(),
2019-03-10 21:06:44 +04:00
child_exit: Arc::new(Condvar::new()),
child_exit_code: BTreeMap::new()
2019-01-25 18:58:49 +04:00
})),
})
2018-10-30 08:45:09 +04:00
}
2019-03-08 18:37:05 +04:00
/// Create a new thread in the same process.
pub fn clone(&self, tf: &TrapFrame, stack_top: usize, tls: usize, clear_child_tid: usize) -> Box<Thread> {
2019-03-08 18:37:05 +04:00
let kstack = KernelStack::new();
let token = self.proc.lock().memory_set.token();
Box::new(Thread {
2019-03-08 20:47:02 +04:00
context: unsafe { Context::new_clone(tf, stack_top, kstack.top(), token, tls) },
2019-03-08 18:37:05 +04:00
kstack,
clear_child_tid,
2019-03-08 18:37:05 +04:00
proc: self.proc.clone(),
})
}
2018-10-30 08:45:09 +04:00
}
impl Process {
pub fn get_free_fd(&self) -> usize {
(0..).find(|i| !self.files.contains_key(i)).unwrap()
}
pub fn get_futex(&mut self, uaddr: usize) -> Arc<Condvar> {
if !self.futexes.contains_key(&uaddr) {
self.futexes.insert(uaddr, Arc::new(Condvar::new()));
}
self.futexes.get(&uaddr).unwrap().clone()
}
}
2018-11-27 07:26:37 +04:00
/// Generate a MemorySet according to the ELF file.
/// Also return the real entry point address.
fn memory_set_from(elf: &ElfFile<'_>) -> (MemorySet, usize) {
debug!("creating MemorySet from ELF");
2018-11-26 21:23:07 +04:00
let mut ms = MemorySet::new();
let entry = elf.header.pt2.entry_point() as usize;
// [NoMMU] Get total memory size and alloc space
let va_begin = elf.program_iter()
.filter(|ph| ph.get_type() == Ok(Type::Load))
.map(|ph| ph.virtual_addr()).min().unwrap() as usize;
let va_end = elf.program_iter()
.filter(|ph| ph.get_type() == Ok(Type::Load))
.map(|ph| ph.virtual_addr() + ph.mem_size()).max().unwrap() as usize;
let va_size = va_end - va_begin;
#[cfg(feature = "no_mmu")]
let target = ms.push(va_size);
#[cfg(feature = "no_mmu")]
{ entry = entry - va_begin + target.as_ptr() as usize; }
#[cfg(feature = "board_k210")]
{ entry += 0x40000000; }
2018-07-10 20:53:40 +04:00
for ph in elf.program_iter() {
if ph.get_type() != Ok(Type::Load) {
2018-07-14 07:56:55 +04:00
continue;
}
let virt_addr = ph.virtual_addr() as usize;
2018-11-26 21:23:07 +04:00
let offset = ph.offset() as usize;
let file_size = ph.file_size() as usize;
let mem_size = ph.mem_size() as usize;
#[cfg(target_arch = "aarch64")]
assert_eq!((virt_addr >> 48), 0xffff, "Segment Fault");
2018-11-26 21:23:07 +04:00
// Get target slice
#[cfg(feature = "no_mmu")]
let target = &mut target[virt_addr - va_begin..virt_addr - va_begin + mem_size];
#[cfg(feature = "no_mmu")]
debug!("area @ {:?}, size = {:#x}", target.as_ptr(), mem_size);
2018-11-26 21:23:07 +04:00
#[cfg(not(feature = "no_mmu"))]
let target = {
ms.push(virt_addr, virt_addr + mem_size, ph.flags().to_attr(), ByFrame::new(GlobalFrameAlloc), "");
2018-11-26 21:23:07 +04:00
unsafe { ::core::slice::from_raw_parts_mut(virt_addr as *mut u8, mem_size) }
2018-07-10 20:53:40 +04:00
};
2018-11-26 21:23:07 +04:00
// Copy data
unsafe {
ms.with(|| {
if file_size != 0 {
target[..file_size].copy_from_slice(&elf.input[offset..offset + file_size]);
}
target[file_size..].iter_mut().for_each(|x| *x = 0);
});
}
}
(ms, entry)
}
trait ToMemoryAttr {
fn to_attr(&self) -> MemoryAttr;
}
impl ToMemoryAttr for Flags {
fn to_attr(&self) -> MemoryAttr {
let mut flags = MemoryAttr::default().user();
// FIXME: handle readonly
if self.is_execute() { flags = flags.execute(); }
flags
}
2018-10-30 08:45:09 +04:00
}