Orbital (#16)
* Port previous ethernet scheme * Add ipd * Fix initfs rebuilds, use QEMU user networking addresses in ipd * Add tcp/udp, netutils, dns, and network config * Add fsync to network driver * Add dns, router, subnet by default * Fix e1000 driver. Make ethernet and IP non-blocking to avoid deadlocks * Add orbital server, WIP * Add futex * Add orbutils and orbital * Update libstd, orbutils, and orbital Move ANSI key encoding to vesad * Add orbital assets * Update orbital * Update to add login manager * Add blocking primitives, block for most things except waitpid, update orbital * Wait in waitpid and IRQ, improvements for other waits * Fevent in root scheme * WIP: Switch to using fevent * Reorganize * Event based e1000d driver * Superuser-only access to some network schemes, display, and disk * Superuser root and irq schemes * Fix orbital
This commit is contained in:
parent
372d44f88c
commit
224c43f761
92 changed files with 3415 additions and 473 deletions
110
kernel/syscall/futex.rs
Normal file
110
kernel/syscall/futex.rs
Normal file
|
@ -0,0 +1,110 @@
|
|||
use alloc::arc::Arc;
|
||||
use collections::VecDeque;
|
||||
use core::intrinsics;
|
||||
use spin::{Once, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
use context::{self, Context};
|
||||
use syscall::error::{Error, Result, ESRCH, EAGAIN, EINVAL};
|
||||
use syscall::flag::{FUTEX_WAIT, FUTEX_WAKE, FUTEX_REQUEUE};
|
||||
use syscall::validate::validate_slice_mut;
|
||||
|
||||
type FutexList = VecDeque<(usize, Arc<RwLock<Context>>)>;
|
||||
|
||||
/// Fast userspace mutex list
|
||||
static FUTEXES: Once<RwLock<FutexList>> = Once::new();
|
||||
|
||||
/// Initialize futexes, called if needed
|
||||
fn init_futexes() -> RwLock<FutexList> {
|
||||
RwLock::new(VecDeque::new())
|
||||
}
|
||||
|
||||
/// Get the global futexes list, const
|
||||
pub fn futexes() -> RwLockReadGuard<'static, FutexList> {
|
||||
FUTEXES.call_once(init_futexes).read()
|
||||
}
|
||||
|
||||
/// Get the global futexes list, mutable
|
||||
pub fn futexes_mut() -> RwLockWriteGuard<'static, FutexList> {
|
||||
FUTEXES.call_once(init_futexes).write()
|
||||
}
|
||||
|
||||
pub fn futex(addr: &mut i32, op: usize, val: i32, val2: usize, addr2: *mut i32) -> Result<usize> {
|
||||
match op {
|
||||
FUTEX_WAIT => {
|
||||
{
|
||||
let mut futexes = futexes_mut();
|
||||
|
||||
let context_lock = {
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
context_lock.clone()
|
||||
};
|
||||
|
||||
if unsafe { intrinsics::atomic_load(addr) != val } {
|
||||
return Err(Error::new(EAGAIN));
|
||||
}
|
||||
|
||||
context_lock.write().block();
|
||||
|
||||
futexes.push_back((addr as *mut i32 as usize, context_lock));
|
||||
}
|
||||
|
||||
unsafe { context::switch(); }
|
||||
|
||||
Ok(0)
|
||||
},
|
||||
FUTEX_WAKE => {
|
||||
let mut woken = 0;
|
||||
|
||||
{
|
||||
let mut futexes = futexes_mut();
|
||||
|
||||
let mut i = 0;
|
||||
while i < futexes.len() && (woken as i32) < val {
|
||||
if futexes[i].0 == addr as *mut i32 as usize {
|
||||
if let Some(futex) = futexes.swap_remove_back(i) {
|
||||
futex.1.write().unblock();
|
||||
woken += 1;
|
||||
}
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(woken)
|
||||
},
|
||||
FUTEX_REQUEUE => {
|
||||
let addr2_safe = validate_slice_mut(addr2, 1).map(|addr2_safe| &mut addr2_safe[0])?;
|
||||
|
||||
let mut woken = 0;
|
||||
let mut requeued = 0;
|
||||
|
||||
{
|
||||
let mut futexes = futexes_mut();
|
||||
|
||||
let mut i = 0;
|
||||
while i < futexes.len() && (woken as i32) < val {
|
||||
if futexes[i].0 == addr as *mut i32 as usize {
|
||||
if let Some(futex) = futexes.swap_remove_back(i) {
|
||||
futex.1.write().unblock();
|
||||
woken += 1;
|
||||
}
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
while i < futexes.len() && requeued < val2 {
|
||||
if futexes[i].0 == addr as *mut i32 as usize {
|
||||
futexes[i].0 = addr2_safe as *mut i32 as usize;
|
||||
requeued += 1;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(woken)
|
||||
},
|
||||
_ => Err(Error::new(EINVAL))
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ extern crate syscall;
|
|||
pub use self::syscall::{data, error, flag, number, scheme};
|
||||
|
||||
pub use self::fs::*;
|
||||
pub use self::futex::futex;
|
||||
pub use self::process::*;
|
||||
pub use self::time::*;
|
||||
pub use self::validate::*;
|
||||
|
@ -16,6 +17,9 @@ use self::number::*;
|
|||
/// Filesystem syscalls
|
||||
pub mod fs;
|
||||
|
||||
/// Fast userspace mutex
|
||||
pub mod futex;
|
||||
|
||||
/// Process syscalls
|
||||
pub mod process;
|
||||
|
||||
|
@ -28,7 +32,7 @@ pub mod validate;
|
|||
#[no_mangle]
|
||||
pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, stack: usize) -> usize {
|
||||
#[inline(always)]
|
||||
fn inner(a: usize, b: usize, c: usize, d: usize, e: usize, _f: usize, stack: usize) -> Result<usize> {
|
||||
fn inner(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, stack: usize) -> Result<usize> {
|
||||
match a & SYS_CLASS {
|
||||
SYS_CLASS_FILE => match a & SYS_ARG {
|
||||
SYS_ARG_SLICE => file_op_slice(a, b, validate_slice(c as *const u8, d)?),
|
||||
|
@ -66,6 +70,7 @@ pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize
|
|||
SYS_SETUID => setuid(b as u32),
|
||||
SYS_SETGID => setgid(b as u32),
|
||||
SYS_CLOCK_GETTIME => clock_gettime(b, validate_slice_mut(c as *mut TimeSpec, 1).map(|time| &mut time[0])?),
|
||||
SYS_FUTEX => futex(validate_slice_mut(b as *mut i32, 1).map(|uaddr| &mut uaddr[0])?, c, d as i32, e, f as *mut i32),
|
||||
SYS_PIPE2 => pipe2(validate_slice_mut(b as *mut usize, 2)?, c),
|
||||
SYS_PHYSALLOC => physalloc(b),
|
||||
SYS_PHYSFREE => physfree(b, c),
|
||||
|
@ -78,10 +83,10 @@ pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize
|
|||
}
|
||||
|
||||
let result = inner(a, b, c, d, e, f, stack);
|
||||
|
||||
/*
|
||||
if let Err(ref err) = result {
|
||||
println!("{}, {}, {}, {}: {}", a & 0xFFFF, b, c, d, err);
|
||||
println!("{}, {}, {}, {}: {}", a, b, c, d, err);
|
||||
}
|
||||
|
||||
*/
|
||||
Error::mux(result)
|
||||
}
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
use alloc::arc::Arc;
|
||||
use alloc::boxed::Box;
|
||||
use collections::{BTreeMap, Vec};
|
||||
use core::mem;
|
||||
use core::{mem, str};
|
||||
use core::ops::DerefMut;
|
||||
use core::str;
|
||||
use spin::Mutex;
|
||||
|
||||
use arch;
|
||||
|
@ -247,7 +246,7 @@ pub fn clone(flags: usize, stack_base: usize) -> Result<usize> {
|
|||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let mut context = context_lock.write();
|
||||
context.status = context::Status::Blocked;
|
||||
context.block();
|
||||
vfork = true;
|
||||
} else {
|
||||
vfork = false;
|
||||
|
@ -416,14 +415,13 @@ pub fn exit(status: usize) -> ! {
|
|||
|
||||
let vfork = context.vfork;
|
||||
context.vfork = false;
|
||||
context.waitpid.notify();
|
||||
(vfork, context.ppid)
|
||||
};
|
||||
if vfork {
|
||||
if let Some(context_lock) = contexts.get(ppid) {
|
||||
let mut context = context_lock.write();
|
||||
if context.status == context::Status::Blocked {
|
||||
context.status = context::Status::Runnable;
|
||||
} else {
|
||||
if ! context.unblock() {
|
||||
println!("{} not blocked for exit vfork unblock", ppid);
|
||||
}
|
||||
} else {
|
||||
|
@ -622,9 +620,7 @@ pub fn exec(path: &[u8], arg_ptrs: &[[usize; 2]]) -> Result<usize> {
|
|||
if vfork {
|
||||
if let Some(context_lock) = contexts.get(ppid) {
|
||||
let mut context = context_lock.write();
|
||||
if context.status == context::Status::Blocked {
|
||||
context.status = context::Status::Runnable;
|
||||
} else {
|
||||
if ! context.unblock() {
|
||||
println!("{} not blocked for exec vfork unblock", ppid);
|
||||
}
|
||||
} else {
|
||||
|
@ -808,7 +804,7 @@ pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result<usize> {
|
|||
loop {
|
||||
{
|
||||
let mut exited = false;
|
||||
|
||||
let waitpid;
|
||||
{
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.get(pid).ok_or(Error::new(ESRCH))?;
|
||||
|
@ -820,6 +816,7 @@ pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result<usize> {
|
|||
}
|
||||
exited = true;
|
||||
}
|
||||
waitpid = context.waitpid.clone();
|
||||
}
|
||||
|
||||
if exited {
|
||||
|
@ -827,6 +824,8 @@ pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result<usize> {
|
|||
return contexts.remove(pid).ok_or(Error::new(ESRCH)).and(Ok(pid));
|
||||
} else if flags & WNOHANG == WNOHANG {
|
||||
return Ok(0);
|
||||
} else {
|
||||
waitpid.wait();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,17 +26,20 @@ pub fn nanosleep(req: &TimeSpec, rem_opt: Option<&mut TimeSpec>) -> Result<usize
|
|||
let start = arch::time::monotonic();
|
||||
let sum = start.1 + req.tv_nsec as u64;
|
||||
let end = (start.0 + req.tv_sec as u64 + sum / 1000000000, sum % 1000000000);
|
||||
|
||||
loop {
|
||||
unsafe { context::switch(); }
|
||||
|
||||
let current = arch::time::monotonic();
|
||||
if current.0 > end.0 || (current.0 == end.0 && current.1 >= end.1) {
|
||||
break;
|
||||
}
|
||||
{
|
||||
let contexts = context::contexts();
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let mut context = context_lock.write();
|
||||
|
||||
context.wake = Some(end);
|
||||
context.block();
|
||||
}
|
||||
|
||||
unsafe { context::switch(); }
|
||||
|
||||
if let Some(mut rem) = rem_opt {
|
||||
//TODO let current = arch::time::monotonic();
|
||||
rem.tv_sec = 0;
|
||||
rem.tv_nsec = 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue