WIP: Kevent

This commit is contained in:
Jeremy Soller 2016-09-23 15:47:53 -06:00
parent 94ef9dd14a
commit 616dfbc055
16 changed files with 131 additions and 13 deletions

View file

@ -42,6 +42,11 @@ impl Scheme for DisplayScheme {
Ok(id) Ok(id)
} }
fn fevent(&self, _id: usize, flags: usize) -> Result<usize> {
println!("fevent {:X}", flags);
Ok(0)
}
fn fsync(&self, _id: usize) -> Result<usize> { fn fsync(&self, _id: usize) -> Result<usize> {
Ok(0) Ok(0)
} }

View file

@ -1,5 +1,5 @@
initfs:bin/vesad initfs:bin/vesad
initfs:bin/ps2d initfs:bin/ps2d
#initfs:bin/pcid #initfs:bin/pcid
#initfs:bin/example initfs:bin/example
initfs:bin/login display: initfs:bin/ion #initfs:bin/login display: initfs:bin/ion

View file

@ -8,7 +8,7 @@ use super::{contexts, Context, Status, CONTEXT_ID};
/// # Safety /// # Safety
/// ///
/// Do not call this while holding locks! /// Do not call this while holding locks!
pub unsafe fn switch() { pub unsafe fn switch() -> bool {
use core::ops::DerefMut; use core::ops::DerefMut;
// Set the global lock to avoid the unsafe operations below from causing issues // Set the global lock to avoid the unsafe operations below from causing issues
@ -48,10 +48,9 @@ pub unsafe fn switch() {
} }
if to_ptr as usize == 0 { if to_ptr as usize == 0 {
// TODO: Sleep, wait for interrupt
// Unset global lock if no context found // Unset global lock if no context found
arch::context::CONTEXT_SWITCH_LOCK.store(false, Ordering::SeqCst); arch::context::CONTEXT_SWITCH_LOCK.store(false, Ordering::SeqCst);
return; return false;
} }
//println!("Switch {} to {}", (&*from_ptr).id, (&*to_ptr).id); //println!("Switch {} to {}", (&*from_ptr).id, (&*to_ptr).id);
@ -64,4 +63,6 @@ pub unsafe fn switch() {
CONTEXT_ID.store((&mut *to_ptr).id, Ordering::SeqCst); CONTEXT_ID.store((&mut *to_ptr).id, Ordering::SeqCst);
(&mut *from_ptr).arch.switch_to(&mut (&mut *to_ptr).arch); (&mut *from_ptr).arch.switch_to(&mut (&mut *to_ptr).arch);
true
} }

View file

@ -162,8 +162,11 @@ pub extern fn kmain() {
loop { loop {
unsafe { unsafe {
interrupt::disable(); interrupt::disable();
context::switch(); if context::switch() {
interrupt::enable_and_nop(); interrupt::enable_and_nop();
} else {
interrupt::enable_and_halt();
}
} }
} }
} }

View file

@ -48,7 +48,7 @@ impl Scheme for DebugScheme {
if i > 0 { if i > 0 {
return Ok(i); return Ok(i);
} else { } else {
unsafe { context::switch(); } unsafe { context::switch(); } //TODO: Block
} }
} }
} }

33
kernel/scheme/event.rs Normal file
View file

@ -0,0 +1,33 @@
use core::{mem, str};
use arch::interrupt::irq::{ACKS, COUNTS, acknowledge};
use syscall::error::*;
use syscall::scheme::Scheme;
pub struct EventScheme;
impl Scheme for EventScheme {
fn open(&self, _path: &[u8], _flags: usize) -> Result<usize> {
Ok(
}
fn dup(&self, file: usize) -> Result<usize> {
Ok(file)
}
fn read(&self, file: usize, buffer: &mut [u8]) -> Result<usize> {
Ok(0)
}
fn write(&self, file: usize, buffer: &[u8]) -> Result<usize> {
Ok(0)
}
fn fsync(&self, _file: usize) -> Result<usize> {
Ok(0)
}
fn close(&self, _file: usize) -> Result<usize> {
Ok(0)
}
}

View file

@ -52,7 +52,7 @@ impl UserInner {
} }
} }
unsafe { context::switch(); } unsafe { context::switch(); } //TODO: Block
} }
} }
@ -163,7 +163,7 @@ impl UserInner {
if i > 0 { if i > 0 {
return Ok(i * packet_size); return Ok(i * packet_size);
} else { } else {
unsafe { context::switch(); } unsafe { context::switch(); } //TODO: Block
} }
} }
} else { } else {
@ -230,7 +230,12 @@ impl Scheme for UserScheme {
fn seek(&self, file: usize, position: usize, whence: usize) -> Result<usize> { fn seek(&self, file: usize, position: usize, whence: usize) -> Result<usize> {
let inner = self.inner.upgrade().ok_or(Error::new(ENODEV))?; let inner = self.inner.upgrade().ok_or(Error::new(ENODEV))?;
inner.call(SYS_FSYNC, file, position, whence) inner.call(SYS_LSEEK, file, position, whence)
}
fn fevent(&self, file: usize, flags: usize) -> Result<usize> {
let inner = self.inner.upgrade().ok_or(Error::new(ENODEV))?;
inner.call(SYS_FEVENT, file, flags, 0)
} }
fn fstat(&self, file: usize, stat: &mut Stat) -> Result<usize> { fn fstat(&self, file: usize, stat: &mut Stat) -> Result<usize> {

View file

@ -98,6 +98,24 @@ pub fn dup(fd: usize) -> Result<usize> {
scheme.dup(file.number) scheme.dup(file.number)
} }
/// Register events for file
pub fn fevent(fd: usize, flags: usize) -> Result<usize> {
let file = {
let contexts = context::contexts();
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
let context = context_lock.read();
let file = context.get_file(fd).ok_or(Error::new(EBADF))?;
file
};
let scheme = {
let schemes = scheme::schemes();
let scheme = schemes.get(file.scheme).ok_or(Error::new(EBADF))?;
scheme.clone()
};
scheme.fevent(file.number, flags)
}
/// Get the canonical path of the file /// Get the canonical path of the file
pub fn fpath(fd: usize, buf: &mut [u8]) -> Result<usize> { pub fn fpath(fd: usize, buf: &mut [u8]) -> Result<usize> {
let file = { let file = {

View file

@ -45,6 +45,7 @@ pub extern fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize
SYS_CLONE => clone(b, stack), SYS_CLONE => clone(b, stack),
SYS_YIELD => sched_yield(), SYS_YIELD => sched_yield(),
SYS_GETCWD => getcwd(validate_slice_mut(b as *mut u8, c)?), SYS_GETCWD => getcwd(validate_slice_mut(b as *mut u8, c)?),
SYS_FEVENT => fevent(b, c),
SYS_FPATH => fpath(b, validate_slice_mut(c as *mut u8, d)?), SYS_FPATH => fpath(b, validate_slice_mut(c as *mut u8, d)?),
SYS_PHYSMAP => physmap(b, c, d), SYS_PHYSMAP => physmap(b, c, d),
SYS_PHYSUNMAP => physunmap(b), SYS_PHYSUNMAP => physunmap(b),

View file

@ -618,7 +618,6 @@ pub fn sched_yield() -> Result<usize> {
} }
pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result<usize> { pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result<usize> {
//TODO: Implement status_ptr and options
loop { loop {
{ {
let mut exited = false; let mut exited = false;
@ -644,6 +643,6 @@ pub fn waitpid(pid: usize, status_ptr: usize, flags: usize) -> Result<usize> {
} }
} }
unsafe { context::switch(); } unsafe { context::switch(); } //TODO: Block
} }
} }

View file

@ -25,6 +25,21 @@ impl Scheme for ExampleScheme {
} }
fn main(){ fn main(){
{
let events = syscall::open("event:", 0).unwrap();
let a = syscall::open("display:", 0).unwrap();
syscall::fevent(a, syscall::EVENT_READ).unwrap();
loop {
let mut event = syscall::Event::default();
syscall::read(events, &mut event).unwrap();
println!("{:?}", event);
}
let _ = syscall::close(events);
}
thread::spawn(move || { thread::spawn(move || {
let mut socket = File::create(":example").expect("example: failed to create example scheme"); let mut socket = File::create(":example").expect("example: failed to create example scheme");
let scheme = ExampleScheme; let scheme = ExampleScheme;

View file

@ -1,6 +1,30 @@
use core::ops::{Deref, DerefMut}; use core::ops::{Deref, DerefMut};
use core::{mem, slice}; use core::{mem, slice};
#[derive(Copy, Clone, Debug, Default)]
pub struct Event {
pub id: usize,
pub flags: usize,
pub data: usize
}
impl Deref for Event {
type Target = [u8];
fn deref(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self as *const Event as *const u8, mem::size_of::<Event>()) as &[u8]
}
}
}
impl DerefMut for Event {
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self as *mut Event as *mut u8, mem::size_of::<Event>()) as &mut [u8]
}
}
}
#[derive(Copy, Clone, Debug, Default)] #[derive(Copy, Clone, Debug, Default)]
#[repr(packed)] #[repr(packed)]
pub struct Packet { pub struct Packet {

View file

@ -14,6 +14,10 @@ pub const CLONE_SUPERVISE: usize = 0x400000;
pub const CLOCK_REALTIME: usize = 1; pub const CLOCK_REALTIME: usize = 1;
pub const CLOCK_MONOTONIC: usize = 4; pub const CLOCK_MONOTONIC: usize = 4;
pub const EVENT_NONE: usize = 0;
pub const EVENT_READ: usize = 1;
pub const EVENT_WRITE: usize = 2;
pub const FUTEX_WAIT: usize = 0; pub const FUTEX_WAIT: usize = 0;
pub const FUTEX_WAKE: usize = 1; pub const FUTEX_WAKE: usize = 1;
pub const FUTEX_REQUEUE: usize = 2; pub const FUTEX_REQUEUE: usize = 2;

View file

@ -58,6 +58,10 @@ pub fn exit(status: usize) -> Result<usize> {
unsafe { syscall1(SYS_EXIT, status) } unsafe { syscall1(SYS_EXIT, status) }
} }
pub fn fevent(fd: usize, flags: usize) -> Result<usize> {
unsafe { syscall2(SYS_FEVENT, fd, flags) }
}
pub fn fpath(fd: usize, buf: &mut [u8]) -> Result<usize> { pub fn fpath(fd: usize, buf: &mut [u8]) -> Result<usize> {
unsafe { syscall3(SYS_FPATH, fd, buf.as_mut_ptr() as usize, buf.len()) } unsafe { syscall3(SYS_FPATH, fd, buf.as_mut_ptr() as usize, buf.len()) }
} }

View file

@ -6,6 +6,7 @@ pub const SYS_CLOCK_GETTIME: usize = 265;
pub const SYS_DUP: usize = 41; pub const SYS_DUP: usize = 41;
pub const SYS_EXECVE: usize = 11; pub const SYS_EXECVE: usize = 11;
pub const SYS_EXIT: usize = 1; pub const SYS_EXIT: usize = 1;
pub const SYS_FEVENT: usize = 927;
pub const SYS_FPATH: usize = 928; pub const SYS_FPATH: usize = 928;
pub const SYS_FSTAT: usize = 28; pub const SYS_FSTAT: usize = 28;
pub const SYS_FSYNC: usize = 118; pub const SYS_FSYNC: usize = 118;

View file

@ -67,6 +67,11 @@ pub trait Scheme {
Err(Error::new(EBADF)) Err(Error::new(EBADF))
} }
#[allow(unused_variables)]
fn fevent(&self, id: usize, flags: usize) -> Result<usize> {
Err(Error::new(EBADF))
}
#[allow(unused_variables)] #[allow(unused_variables)]
fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> { fn fpath(&self, id: usize, buf: &mut [u8]) -> Result<usize> {
Err(Error::new(EBADF)) Err(Error::new(EBADF))