refactor: better ProcessIo impl

This commit is contained in:
2021-11-02 17:26:51 +02:00
parent 85b25c3954
commit abfa6d28eb
6 changed files with 66 additions and 17 deletions
+1
View File
@@ -106,6 +106,7 @@ impl<const N: usize> CharRing<N> {
};
if byte == b'\n' || byte == b'\r' {
dev.write(true, b"\r\n").ok();
break;
}
+2 -2
View File
@@ -95,9 +95,9 @@ pub unsafe fn enter(initrd: Option<(usize, usize)>) -> ! {
// Open stdout
{
let mut io = proc.io.lock();
let node = io.ioctx.as_ref().unwrap().find(None, "/dev/uart0", true).unwrap();
let node = io.ioctx().find(None, "/dev/uart0", true).unwrap();
// TODO fd cloning?
io.files.push(node.open().unwrap());
io.place_file(node.open().unwrap()).unwrap();
}
Process::execve(|space| elf::load_elf(space, &mut file), 0).unwrap();
+41 -7
View File
@@ -6,7 +6,7 @@ use crate::mem::{
};
use crate::proc::{PROCESSES, SCHED};
use crate::sync::IrqSafeSpinLock;
use alloc::{vec::Vec, rc::Rc};
use alloc::{rc::Rc, collections::BTreeMap};
use core::cell::UnsafeCell;
use core::fmt;
use core::sync::atomic::{AtomicU32, Ordering};
@@ -53,9 +53,11 @@ struct ProcessInner {
///
pub struct ProcessIo {
///
pub ioctx: Option<Ioctx>,
ioctx: Option<Ioctx>,
///
pub files: Vec<File>,
files: BTreeMap<usize, File>,
///
file_bitmap: u64,
}
/// Structure describing an operating system process
@@ -134,6 +136,41 @@ impl fmt::Display for Pid {
}
}
impl ProcessIo {
///
pub fn file(&mut self, idx: usize) -> Result<&mut File, Errno> {
self.files.get_mut(&idx).ok_or(Errno::InvalidFile)
}
///
pub fn ioctx(&mut self) -> &mut Ioctx {
self.ioctx.as_mut().unwrap()
}
///
pub fn place_file(&mut self, file: File) -> Result<usize, Errno> {
for bit in 0..64 {
if self.file_bitmap & (1 << bit) == 0 {
if self.files.insert(bit, file).is_some() {
panic!("Open file bitmap is broken");
}
self.file_bitmap |= 1 << bit;
return Ok(bit);
}
}
Err(Errno::TooManyDescriptors)
}
///
pub fn new() -> Self {
Self {
files: BTreeMap::new(),
file_bitmap: 0,
ioctx: None
}
}
}
impl Process {
const USTACK_VIRT_TOP: usize = 0x100000000;
const USTACK_PAGES: usize = 4;
@@ -223,10 +260,7 @@ impl Process {
let id = Pid::new_kernel();
let res = Rc::new(Self {
ctx: UnsafeCell::new(Context::kernel(entry as usize, arg)),
io: IrqSafeSpinLock::new(ProcessIo {
ioctx: None,
files: Vec::new(),
}),
io: IrqSafeSpinLock::new(ProcessIo::new()),
inner: IrqSafeSpinLock::new(ProcessInner {
id,
exit: None,
+4 -6
View File
@@ -115,25 +115,23 @@ pub unsafe fn syscall(num: usize, args: &[usize]) -> Result<usize, Errno> {
let proc = Process::current();
let mut io = proc.io.lock();
let node = io.ioctx.as_ref().unwrap().find(None, path, true)?;
let node = io.ioctx().find(None, path, true)?;
// TODO check access
io.files.push(node.open()?);
Ok(io.files.len() - 1)
io.place_file(node.open()?)
}
abi::SYS_READ => {
let proc = Process::current();
let mut io = proc.io.lock();
let buf = validate_user_ptr(args[1], args[2])?;
io.files[args[0]].read(buf)
io.file(args[0])?.read(buf)
}
abi::SYS_WRITE => {
let proc = Process::current();
let mut io = proc.io.lock();
let buf = validate_user_ptr(args[1], args[2])?;
io.files[args[0]].write(buf)
io.file(args[0])?.write(buf)
}
// Extra system calls