Skip to content

Commit

Permalink
add some syscalls and add stdin (#131)
Browse files Browse the repository at this point in the history
* add sys_getrandom and sys_nanosleep

* add stdin

* fix sys_nanosleep

* update rcore-fs and add sys_poll

* add sys_prlimit64

* fix color problem of ls

* fix a problem of compiling zCore

* add gdbinit for make debug

* fix typo of flag

* Update linux-syscall/src/file/poll.rs

Co-authored-by: Runji Wang <[email protected]>

* Update linux-syscall/src/file/poll.rs

Co-authored-by: Runji Wang <[email protected]>

* remove comment

* remove pub at inner

* fix a problem of pointer

* fix arg of nanosleep

* Update linux-syscall/src/misc.rs

Co-authored-by: Runji Wang <[email protected]>

* remove pub

* fix a problem of null ptr in sys_prlimit

Co-authored-by: Runji Wang <[email protected]>
  • Loading branch information
yukiiiteru and wangrunji0408 authored Aug 11, 2020
1 parent 01c671d commit 2c6893e
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 92 deletions.
2 changes: 1 addition & 1 deletion linux-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ linux-syscall = { path = "../linux-syscall" }
linux-object = { path = "../linux-object" }
zircon-object = { path = "../zircon-object" }
kernel-hal = { path = "../kernel-hal" }
rcore-fs-hostfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "e17b27b", optional = true }
rcore-fs-hostfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47", optional = true }
env_logger = { version = "0.7", optional = true }
kernel-hal-unix = { path = "../kernel-hal-unix", optional = true }
async-std = { version = "1.5", features = ["attributes"], optional = true }
Expand Down
12 changes: 7 additions & 5 deletions linux-object/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ description = "Linux kernel objects"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = "0.1.36"
log = "0.4"
spin = "0.5"
xmas-elf = "0.7"
Expand All @@ -16,8 +17,9 @@ hashbrown = "0.7"
zircon-object = { path = "../zircon-object", features = ["elf"] }
kernel-hal = { path = "../kernel-hal" }
downcast-rs = { git = "https://github.com/rcore-os/downcast-rs", rev = "a632ce1", default-features = false }
rcore-fs = { git = "https://github.com/rcore-os/rcore-fs", rev = "e17b27b" }
rcore-fs-sfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "e17b27b" }
rcore-fs-ramfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "e17b27b" }
rcore-fs-mountfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "e17b27b" }
rcore-fs-devfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "e17b27b" }
lazy_static = { version = "1.4", features = ["spin_no_std"] }
rcore-fs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" }
rcore-fs-sfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" }
rcore-fs-ramfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" }
rcore-fs-mountfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" }
rcore-fs-devfs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" }
1 change: 1 addition & 0 deletions linux-object/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ impl From<FsError> for LxError {
FsError::Again => LxError::EAGAIN,
FsError::SymLoop => LxError::ELOOP,
FsError::Busy => LxError::EBUSY,
FsError::Interrupted => LxError::EINTR,
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion linux-object/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#![allow(dead_code)]

use alloc::{string::String, sync::Arc};
use alloc::{boxed::Box, string::String, sync::Arc};

use super::FileLike;
use crate::error::{LxError, LxResult};
use async_trait::async_trait;
use rcore_fs::vfs::{FsError, INode, Metadata, PollStatus};
use spin::Mutex;
use zircon_object::object::*;
Expand Down Expand Up @@ -154,6 +155,10 @@ impl File {
Ok(status)
}

pub async fn async_poll(&self) -> LxResult<PollStatus> {
Ok(self.inode.async_poll().await?)
}

pub fn io_control(&self, cmd: u32, arg: usize) -> LxResult<usize> {
self.inode.io_control(cmd, arg)?;
Ok(0)
Expand All @@ -172,6 +177,7 @@ impl File {
}
}

#[async_trait]
impl FileLike for File {
fn read(&self, buf: &mut [u8]) -> LxResult<usize> {
self.read(buf)
Expand All @@ -193,6 +199,10 @@ impl FileLike for File {
self.poll()
}

async fn async_poll(&self) -> LxResult<PollStatus> {
self.async_poll().await
}

fn ioctl(&self, request: usize, arg1: usize, _arg2: usize, _arg3: usize) -> LxResult<usize> {
self.io_control(request as u32, arg1)
}
Expand Down
5 changes: 4 additions & 1 deletion linux-object/src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::{sync::Arc, vec::Vec};
use alloc::{boxed::Box, sync::Arc, vec::Vec};

use rcore_fs::vfs::*;
use rcore_fs_devfs::{special::*, DevFS};
Expand All @@ -16,6 +16,7 @@ pub use rcore_fs::vfs;

use crate::error::*;
use crate::process::LinuxProcess;
use async_trait::async_trait;
use core::convert::TryFrom;
use downcast_rs::impl_downcast;
use zircon_object::object::*;
Expand All @@ -29,6 +30,7 @@ mod pseudo;
mod random;
mod stdio;

#[async_trait]
/// Generic file interface
///
/// - Normal file, Directory
Expand All @@ -40,6 +42,7 @@ pub trait FileLike: KernelObject {
fn read_at(&self, offset: u64, buf: &mut [u8]) -> LxResult<usize>;
fn write_at(&self, offset: u64, buf: &[u8]) -> LxResult<usize>;
fn poll(&self) -> LxResult<PollStatus>;
async fn async_poll(&self) -> LxResult<PollStatus>;
fn ioctl(&self, request: usize, arg1: usize, arg2: usize, arg3: usize) -> LxResult<usize>;
fn fcntl(&self, cmd: usize, arg: usize) -> LxResult<usize>;
}
Expand Down
150 changes: 82 additions & 68 deletions linux-object/src/fs/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,92 @@
#![allow(unsafe_code)]

use super::ioctl::*;
use alloc::collections::VecDeque;
use alloc::sync::Arc;
use core::any::Any;
use lazy_static::lazy_static;
use rcore_fs::vfs::*;
//use spin::Mutex;
use spin::Mutex;

//#[derive(Default)]
//pub struct Stdin {
// buf: Mutex<VecDeque<char>>,
// pub pushed: Condvar,
//}
//
//impl Stdin {
// pub fn push(&self, c: char) {
// self.buf.lock().push_back(c);
// self.pushed.notify_one();
// }
// pub fn pop(&self) -> char {
// loop {
// let mut buf_lock = self.buf.lock();
// match buf_lock.pop_front() {
// Some(c) => return c,
// None => {
// self.pushed.wait(buf_lock);
// }
// }
// }
// }
// pub fn can_read(&self) -> bool {
// return self.buf.lock().len() > 0;
// }
//}
lazy_static! {
pub static ref STDIN: Arc<Stdin> = Default::default();
pub static ref STDOUT: Arc<Stdout> = Default::default();
}

#[derive(Default)]
pub struct Stdin {
buf: Mutex<VecDeque<char>>,
// TODO: add signal
}

impl Stdin {
pub fn push(&self, c: char) {
self.buf.lock().push_back(c);
// self.pushed.notify_one();
}
pub fn pop(&self) -> char {
loop {
let mut buf_lock = self.buf.lock();
if let Some(c) = buf_lock.pop_front() {
return c;
} else {
// self.pushed.wait(buf_lock);
}
}
}
pub fn can_read(&self) -> bool {
self.buf.lock().len() > 0
}
}

#[derive(Default)]
pub struct Stdout;

//impl INode for Stdin {
// fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> {
// if self.can_read() {
// buf[0] = self.pop() as u8;
// Ok(1)
// } else {
// Err(FsError::Again)
// }
// }
// fn write_at(&self, _offset: usize, _buf: &[u8]) -> Result<usize> {
// unimplemented!()
// }
// fn poll(&self) -> Result<PollStatus> {
// Ok(PollStatus {
// read: self.can_read(),
// write: false,
// error: false,
// })
// }
// fn io_control(&self, cmd: u32, data: usize) -> Result<()> {
// match cmd as usize {
// TCGETS | TIOCGWINSZ | TIOCSPGRP => {
// // pretend to be tty
// Ok(())
// }
// TIOCGPGRP => {
// // pretend to be have a tty process group
// // TODO: verify pointer
// unsafe { *(data as *mut u32) = 0 };
// Ok(())
// }
// _ => Err(FsError::NotSupported),
// }
// }
// fn as_any_ref(&self) -> &dyn Any {
// self
// }
//}
impl INode for Stdin {
fn read_at(&self, _offset: usize, buf: &mut [u8]) -> Result<usize> {
if self.can_read() {
buf[0] = self.pop() as u8;
Ok(1)
} else {
let mut buffer = [0; 255];
let len = kernel_hal::serial_read(&mut buffer);
for c in &buffer[..len] {
self.push((*c).into());
}
Err(FsError::Again)
}
}
fn write_at(&self, _offset: usize, _buf: &[u8]) -> Result<usize> {
unimplemented!()
}
fn poll(&self) -> Result<PollStatus> {
Ok(PollStatus {
read: self.can_read(),
write: false,
error: false,
})
}
/*
fn io_control(&self, cmd: u32, data: usize) -> Result<usize> {
match cmd as usize {
TCGETS | TIOCGWINSZ | TIOCSPGRP => {
// pretend to be tty
Ok(0)
}
TIOCGPGRP => {
// pretend to be have a tty process group
// TODO: verify pointer
unsafe { *(data as *mut u32) = 0 };
Ok(0)
}
_ => Err(FsError::NotSupported),
}
}
*/
fn as_any_ref(&self) -> &dyn Any {
self
}
}

impl INode for Stdout {
fn read_at(&self, _offset: usize, _buf: &mut [u8]) -> Result<usize> {
Expand All @@ -92,17 +106,17 @@ impl INode for Stdout {
error: false,
})
}
fn io_control(&self, cmd: u32, data: usize) -> Result<()> {
fn io_control(&self, cmd: u32, data: usize) -> Result<usize> {
match cmd as usize {
TCGETS | TIOCGWINSZ | TIOCSPGRP => {
// pretend to be tty
Ok(())
Ok(0)
}
TIOCGPGRP => {
// pretend to be have a tty process group
// TODO: verify pointer
unsafe { *(data as *mut u32) = 0 };
Ok(())
Ok(0)
}
_ => Err(FsError::NotSupported),
}
Expand Down
4 changes: 2 additions & 2 deletions linux-object/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl LinuxProcess {
/// Create a new process.
pub fn new(rootfs: Arc<dyn FileSystem>) -> Self {
let stdin = File::new(
Arc::new(Stdout), // FIXME: stdin
STDIN.clone(), // FIXME: stdin
OpenOptions {
read: true,
write: false,
Expand All @@ -160,7 +160,7 @@ impl LinuxProcess {
String::from("/dev/stdin"),
) as Arc<dyn FileLike>;
let stdout = File::new(
Arc::new(Stdout), // TODO: open from '/dev/stdout'
STDOUT.clone(), // TODO: open from '/dev/stdout'
OpenOptions {
read: false,
write: true,
Expand Down
2 changes: 1 addition & 1 deletion linux-syscall/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ numeric-enum-macro = "0.2"
zircon-object = { path = "../zircon-object" }
linux-object = { path = "../linux-object" }
kernel-hal = { path = "../kernel-hal" }
rcore-fs = { git = "https://github.com/rcore-os/rcore-fs", rev = "e17b27b" }
rcore-fs = { git = "https://github.com/rcore-os/rcore-fs", rev = "517af47" }
lazy_static = { version = "1.4", features = ["spin_no_std"] }

Loading

0 comments on commit 2c6893e

Please sign in to comment.