Remove env scheme and env field on context, fix all warnings

This commit is contained in:
Jeremy Soller
2018-08-11 11:34:32 -06:00
parent fe90664e33
commit 30e68f917e
14 changed files with 29 additions and 326 deletions

View File

@@ -126,7 +126,7 @@ pub unsafe fn init(cpu_id: usize, kernel_start: usize, kernel_end: usize, stack_
let page = Page::containing_address(VirtualAddress::new(frame.start_address().get() + ::KERNEL_OFFSET));
let result = mapper.map_to(page, frame, EntryFlags::PRESENT | EntryFlags::GLOBAL | EntryFlags::NO_EXECUTE | EntryFlags::WRITABLE);
// The flush can be ignored as this is not the active table. See later active_table.switch
unsafe { result.ignore(); }
/* unsafe */ { result.ignore(); }
}
}
@@ -168,7 +168,7 @@ pub unsafe fn init(cpu_id: usize, kernel_start: usize, kernel_end: usize, stack_
let page = Page::containing_address(VirtualAddress::new(virt_addr));
let result = mapper.map_to(page, frame, flags);
// The flush can be ignored as this is not the active table. See later active_table.switch
unsafe { result.ignore(); }
/* unsafe */ { result.ignore(); }
}
}

View File

@@ -1,6 +1,6 @@
use alloc::arc::Arc;
use alloc::boxed::Box;
use alloc::{BTreeMap, Vec, VecDeque};
use alloc::{Vec, VecDeque};
use core::cmp::Ordering;
use core::mem;
use spin::Mutex;
@@ -150,8 +150,6 @@ pub struct Context {
pub name: Arc<Mutex<Box<[u8]>>>,
/// The current working directory
pub cwd: Arc<Mutex<Vec<u8>>>,
/// The process environment
pub env: Arc<Mutex<BTreeMap<Box<[u8]>, Arc<Mutex<Vec<u8>>>>>>,
/// The open files in the scheme
pub files: Arc<Mutex<Vec<Option<FileDescriptor>>>>,
/// Singal actions
@@ -191,7 +189,6 @@ impl Context {
grants: Arc::new(Mutex::new(Vec::new())),
name: Arc::new(Mutex::new(Vec::new().into_boxed_slice())),
cwd: Arc::new(Mutex::new(Vec::new())),
env: Arc::new(Mutex::new(BTreeMap::new())),
files: Arc::new(Mutex::new(Vec::new())),
actions: Arc::new(Mutex::new(vec![(
SigAction {

View File

@@ -1,7 +1,7 @@
use alloc::arc::Arc;
use alloc::boxed::Box;
use alloc::BTreeMap;
use core::alloc::{Alloc, GlobalAlloc, Layout};
use core::alloc::{GlobalAlloc, Layout};
use core::mem;
use core::sync::atomic::Ordering;
use paging;

View File

@@ -1,8 +1,8 @@
//! # Context management
//!
//!
//! For resources on contexts, please consult [wikipedia](https://en.wikipedia.org/wiki/Context_switch) and [osdev](https://wiki.osdev.org/Context_Switching)
use alloc::boxed::Box;
use core::alloc::{Alloc, GlobalAlloc, Layout};
use core::alloc::{GlobalAlloc, Layout};
use core::sync::atomic::Ordering;
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};

View File

@@ -61,7 +61,6 @@ impl<'a> Elf<'a> {
if let Some(symtab) = symtab_opt {
Some(ElfSymbols {
data: self.data,
header: self.header,
symtab: symtab,
i: 0
})
@@ -128,7 +127,6 @@ impl<'a> Iterator for ElfSegments<'a> {
pub struct ElfSymbols<'a> {
data: &'a [u8],
header: &'a header::Header,
symtab: &'a section_header::SectionHeader,
i: usize
}

View File

@@ -24,10 +24,6 @@ impl EventQueue {
}
}
pub fn dup(&self, other: &EventQueue) {
panic!("EventQeuue::dup");
}
pub fn read(&self, events: &mut [Event]) -> Result<usize> {
Ok(self.queue.receive_into(events, true))
}

View File

@@ -22,7 +22,6 @@
#![feature(const_max_value)]
#![feature(const_size_of)]
#![feature(core_intrinsics)]
#![feature(global_allocator)]
#![feature(integer_atomics)]
#![feature(lang_items)]
#![feature(naked_functions)]
@@ -46,9 +45,8 @@ extern crate spin;
#[cfg(feature = "slab")]
extern crate slab_allocator;
use alloc::arc::Arc;
use alloc::vec::Vec;
use core::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use spin::Mutex;
use scheme::{FileHandle, SchemeNamespace};
@@ -134,9 +132,13 @@ pub fn cpu_count() -> usize {
CPU_COUNT.load(Ordering::Relaxed)
}
static mut INIT_ENV: &[u8] = &[];
/// Initialize userspace by running the initfs:bin/init process
/// This function will also set the CWD to initfs:bin and open debug: as stdio
pub extern fn userspace_init() {
let env = unsafe { INIT_ENV };
assert_eq!(syscall::chdir(b"initfs:"), Ok(0));
assert_eq!(syscall::open(b"debug:", syscall::flag::O_RDONLY).map(FileHandle::into), Ok(0));
@@ -144,13 +146,22 @@ pub extern fn userspace_init() {
assert_eq!(syscall::open(b"debug:", syscall::flag::O_WRONLY).map(FileHandle::into), Ok(2));
let fd = syscall::open(b"/bin/init", syscall::flag::O_RDONLY).expect("failed to open init");
syscall::fexec(fd, &[], &[]).expect("failed to execute init");
let mut env_ptrs = Vec::new();
for line in env.split(|b| *b == b'\n') {
env_ptrs.push([
line.as_ptr() as usize,
line.len(),
]);
}
syscall::fexec(fd, &[], &env_ptrs).expect("failed to execute init");
panic!("init returned");
}
/// This is the kernel entry point for the primary CPU. The arch crate is responsible for calling this
pub fn kmain(cpus: usize, env: &[u8]) -> ! {
pub fn kmain(cpus: usize, env: &'static [u8]) -> ! {
CPU_ID.store(0, Ordering::SeqCst);
CPU_COUNT.store(cpus, Ordering::SeqCst);
@@ -161,25 +172,13 @@ pub fn kmain(cpus: usize, env: &[u8]) -> ! {
println!("BSP: {:?} {}", pid, cpus);
println!("Env: {:?}", ::core::str::from_utf8(env));
unsafe { INIT_ENV = env };
match context::contexts_mut().spawn(userspace_init) {
Ok(context_lock) => {
let mut context = context_lock.write();
context.rns = SchemeNamespace::from(1);
context.ens = SchemeNamespace::from(1);
context.status = context::Status::Runnable;
let mut context_env = context.env.lock();
for line in env.split(|b| *b == b'\n') {
let mut parts = line.splitn(2, |b| *b == b'=');
if let Some(name) = parts.next() {
if let Some(data) = parts.next() {
context_env.insert(
name.to_vec().into_boxed_slice(),
Arc::new(Mutex::new(data.to_vec()))
);
}
}
}
},
Err(err) => {
panic!("failed to spawn userspace_init: {:?}", err);

View File

@@ -1,237 +0,0 @@
use alloc::arc::Arc;
use alloc::{BTreeMap, Vec};
use core::{cmp, str};
use core::sync::atomic::{AtomicUsize, Ordering};
use spin::{Mutex, RwLock};
use context;
use syscall::data::Stat;
use syscall::error::*;
use syscall::flag::{MODE_FILE, SEEK_SET, SEEK_CUR, SEEK_END, O_CREAT};
use syscall::scheme::Scheme;
#[derive(Clone)]
struct Handle {
data: Arc<Mutex<Vec<u8>>>,
mode: u16,
seek: usize
}
pub struct EnvScheme {
next_id: AtomicUsize,
handles: RwLock<BTreeMap<usize, Handle>>
}
impl EnvScheme {
pub fn new() -> EnvScheme {
EnvScheme {
next_id: AtomicUsize::new(0),
handles: RwLock::new(BTreeMap::new())
}
}
}
impl Scheme for EnvScheme {
fn open(&self, path: &[u8], flags: usize, _uid: u32, _gid: u32) -> Result<usize> {
let path = str::from_utf8(path).or(Err(Error::new(ENOENT)))?.trim_matches('/');
let env_lock = {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
context.env.clone()
};
if path.is_empty() {
let mut list = Vec::new();
{
let env = env_lock.lock();
for entry in env.iter() {
if ! list.is_empty() {
list.push(b'\n');
}
list.extend_from_slice(&entry.0);
list.push(b'=');
list.extend_from_slice(&entry.1.lock());
}
}
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
self.handles.write().insert(id, Handle {
data: Arc::new(Mutex::new(list)),
mode: MODE_FILE,
seek: 0
});
Ok(id)
} else {
let data = {
let mut env = env_lock.lock();
if env.contains_key(path.as_bytes()) {
env[path.as_bytes()].clone()
} else if flags & O_CREAT == O_CREAT {
let name = path.as_bytes().to_vec().into_boxed_slice();
let data = Arc::new(Mutex::new(Vec::new()));
env.insert(name, data.clone());
data
} else {
return Err(Error::new(ENOENT));
}
};
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
self.handles.write().insert(id, Handle {
data: data,
mode: MODE_FILE,
seek: 0
});
Ok(id)
}
}
fn dup(&self, id: usize, buf: &[u8]) -> Result<usize> {
if ! buf.is_empty() {
return Err(Error::new(EINVAL));
}
let new_handle = {
let handles = self.handles.read();
let handle = handles.get(&id).ok_or(Error::new(EBADF))?;
handle.clone()
};
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
self.handles.write().insert(id, new_handle);
Ok(id)
}
fn read(&self, id: usize, buffer: &mut [u8]) -> Result<usize> {
let mut handles = self.handles.write();
let handle = handles.get_mut(&id).ok_or(Error::new(EBADF))?;
let data = handle.data.lock();
let mut i = 0;
while i < buffer.len() && handle.seek < data.len() {
buffer[i] = data[handle.seek];
i += 1;
handle.seek += 1;
}
Ok(i)
}
fn write(&self, id: usize, buffer: &[u8]) -> Result<usize> {
let mut handles = self.handles.write();
let handle = handles.get_mut(&id).ok_or(Error::new(EBADF))?;
let mut data = handle.data.lock();
let mut i = 0;
while i < buffer.len() && handle.seek < data.len() {
data[handle.seek] = buffer[i];
i += 1;
handle.seek += 1;
}
while i < buffer.len() {
data.push(buffer[i]);
i += 1;
handle.seek += 1;
}
Ok(i)
}
fn seek(&self, id: usize, pos: usize, whence: usize) -> Result<usize> {
let mut handles = self.handles.write();
let handle = handles.get_mut(&id).ok_or(Error::new(EBADF))?;
let len = handle.data.lock().len();
handle.seek = match whence {
SEEK_SET => cmp::min(len, pos),
SEEK_CUR => cmp::max(0, cmp::min(len as isize, handle.seek as isize + pos as isize)) as usize,
SEEK_END => cmp::max(0, cmp::min(len as isize, len as isize + pos as isize)) as usize,
_ => return Err(Error::new(EINVAL))
};
Ok(handle.seek)
}
fn fcntl(&self, id: usize, _cmd: usize, _arg: usize) -> Result<usize> {
let handles = self.handles.read();
let _handle = handles.get(&id).ok_or(Error::new(EBADF))?;
Ok(0)
}
fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
let handles = self.handles.read();
let _handle = handles.get(&id).ok_or(Error::new(EBADF))?;
let mut i = 0;
//TODO: Get env name
let scheme_path = b"env:";
while i < buf.len() && i < scheme_path.len() {
buf[i] = scheme_path[i];
i += 1;
}
Ok(i)
}
fn fstat(&self, id: usize, stat: &mut Stat) -> Result<usize> {
let handles = self.handles.read();
let handle = handles.get(&id).ok_or(Error::new(EBADF))?;
stat.st_mode = handle.mode;
stat.st_size = handle.data.lock().len() as u64;
Ok(0)
}
fn fsync(&self, id: usize) -> Result<usize> {
let handles = self.handles.read();
let _handle = handles.get(&id).ok_or(Error::new(EBADF))?;
Ok(0)
}
fn ftruncate(&self, id: usize, len: usize) -> Result<usize> {
let handles = self.handles.read();
let handle = handles.get(&id).ok_or(Error::new(EBADF))?;
let mut data = handle.data.lock();
if len < data.len() {
data.truncate(len)
} else {
while len > data.len() {
data.push(0);
}
}
Ok(0)
}
fn close(&self, id: usize) -> Result<usize> {
self.handles.write().remove(&id).ok_or(Error::new(EBADF)).and(Ok(0))
}
fn unlink(&self, path: &[u8], _uid: u32, _gid: u32) -> Result<usize> {
let env_lock = {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
context.env.clone()
};
let mut env = env_lock.lock();
if let Some(_) = env.remove(path) {
Ok(0)
} else {
Err(Error::new(ENOENT))
}
}
}

View File

@@ -16,27 +16,6 @@ impl Scheme for EventScheme {
Ok(id.into())
}
fn dup(&self, id: usize, buf: &[u8]) -> Result<usize> {
let id = EventQueueId::from(id);
if ! buf.is_empty() {
return Err(Error::new(EINVAL));
}
let old_queue = {
let handles = queues();
let handle = handles.get(&id).ok_or(Error::new(EBADF))?;
handle.clone()
};
let new_id = next_queue_id();
let new_queue = Arc::new(EventQueue::new(new_id));
queues_mut().insert(new_id, new_queue.clone());
new_queue.dup(&old_queue);
Ok(new_id.into())
}
fn read(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
let id = EventQueueId::from(id);

View File

@@ -17,7 +17,6 @@ use syscall::scheme::Scheme;
use self::debug::DebugScheme;
use self::event::EventScheme;
use self::env::EnvScheme;
use self::initfs::InitFsScheme;
use self::irq::IrqScheme;
use self::memory::MemoryScheme;
@@ -32,9 +31,6 @@ pub mod debug;
/// `event:` - allows reading of `Event`s which are registered using `fevent`
pub mod event;
/// `env:` - access and modify environmental variables
pub mod env;
/// `initfs:` - a readonly filesystem used for initializing the system
pub mod initfs;
@@ -119,7 +115,6 @@ impl SchemeList {
self.insert(ns, Box::new(*b""), |scheme_id| Arc::new(Box::new(RootScheme::new(ns, scheme_id)))).unwrap();
self.insert(ns, Box::new(*b"event"), |_| Arc::new(Box::new(EventScheme))).unwrap();
self.insert(ns, Box::new(*b"env"), |_| Arc::new(Box::new(EnvScheme::new()))).unwrap();
self.insert(ns, Box::new(*b"memory"), |_| Arc::new(Box::new(MemoryScheme))).unwrap();
self.insert(ns, Box::new(*b"sys"), |_| Arc::new(Box::new(SysScheme::new()))).unwrap();
self.insert(ns, Box::new(*b"time"), |scheme_id| Arc::new(Box::new(TimeScheme::new(scheme_id)))).unwrap();

View File

@@ -129,11 +129,6 @@ pub fn format_call(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize) -
c,
d
),
SYS_FEVENT => format!(
"fevent({}, {:#X})",
b,
c
),
SYS_FMAP => format!(
"fmap({}, {:#X}, {})",
b,

View File

@@ -371,11 +371,6 @@ pub fn fcntl(fd: FileHandle, cmd: usize, arg: usize) -> Result<usize> {
}
}
/// Register events for file
pub fn fevent(fd: FileHandle, flags: usize) -> Result<usize> {
Err(Error::new(ENOSYS))
}
pub fn frename(fd: FileHandle, path: &[u8]) -> Result<usize> {
let file = {
let contexts = context::contexts();

View File

@@ -63,7 +63,6 @@ pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, bp: u
SYS_DUP => dup(fd, validate_slice(c as *const u8, d)?).map(FileHandle::into),
SYS_DUP2 => dup2(fd, FileHandle::from(c), validate_slice(d as *const u8, e)?).map(FileHandle::into),
SYS_FCNTL => fcntl(fd, c, d),
SYS_FEVENT => fevent(fd, c),
SYS_FEXEC => fexec(fd, validate_slice(c as *const [usize; 2], d)?, validate_slice(e as *const [usize; 2], f)?),
SYS_FRENAME => frename(fd, validate_slice(c as *const u8, d)?),
SYS_FUNMAP => funmap(b),

View File

@@ -1,7 +1,7 @@
use alloc::arc::Arc;
use alloc::boxed::Box;
use alloc::{BTreeMap, Vec};
use core::alloc::{Alloc, GlobalAlloc, Layout};
use alloc::vec::Vec;
use core::alloc::{GlobalAlloc, Layout};
use core::{intrinsics, mem};
use core::ops::DerefMut;
use spin::Mutex;
@@ -86,7 +86,6 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<ContextId> {
let grants;
let name;
let cwd;
let env;
let files;
let actions;
@@ -265,16 +264,6 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<ContextId> {
cwd = Arc::new(Mutex::new(context.cwd.lock().clone()));
}
if flags & CLONE_VM == CLONE_VM {
env = Arc::clone(&context.env);
} else {
let mut new_env = BTreeMap::new();
for item in context.env.lock().iter() {
new_env.insert(item.0.clone(), Arc::new(Mutex::new(item.1.lock().clone())));
}
env = Arc::new(Mutex::new(new_env));
}
if flags & CLONE_FILES == CLONE_FILES {
files = Arc::clone(&context.files);
} else {
@@ -482,8 +471,6 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<ContextId> {
context.cwd = cwd;
context.env = env;
context.files = files;
context.actions = actions;
@@ -748,7 +735,7 @@ fn exec_noreturn(
(vfork, context.ppid, files)
};
for (fd, file_option) in files.lock().iter_mut().enumerate() {
for (_fd, file_option) in files.lock().iter_mut().enumerate() {
let mut cloexec = false;
if let Some(ref file) = *file_option {
if file.cloexec {
@@ -778,7 +765,7 @@ fn exec_noreturn(
unsafe { usermode(entry, sp, 0); }
}
pub fn fexec(mut fd: FileHandle, arg_ptrs: &[[usize; 2]], var_ptrs: &[[usize; 2]]) -> Result<usize> {
pub fn fexec(fd: FileHandle, arg_ptrs: &[[usize; 2]], var_ptrs: &[[usize; 2]]) -> Result<usize> {
let mut args = Vec::new();
for arg_ptr in arg_ptrs {
let arg = validate_slice(arg_ptr[0] as *const u8, arg_ptr[1])?;
@@ -944,7 +931,7 @@ pub fn exit(status: usize) -> ! {
};
// Files must be closed while context is valid so that messages can be passed
for (fd, file_option) in close_files.drain(..).enumerate() {
for (_fd, file_option) in close_files.drain(..).enumerate() {
if let Some(file) = file_option {
let _ = file.close();
}
@@ -1063,7 +1050,7 @@ pub fn kill(pid: ContextId, sig: usize) -> Result<usize> {
(context.ruid, context.euid, context.pgid)
};
if sig >= 0 && sig < 0x7F {
if sig < 0x7F {
let mut found = 0;
let mut sent = 0;