Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support tty #103

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ members = [
"crates/spinlock",
"crates/timer_list",
"crates/tuple_for_each",
"crates/tty",

"modules/axalloc",
"modules/axlog",
Expand Down
27 changes: 22 additions & 5 deletions api/ruxfeat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,26 @@ irq = ["ruxhal/irq", "ruxruntime/irq", "ruxtask?/irq"]
rtc = ["ruxhal/rtc", "ruxruntime/rtc"]

# Memory
alloc = ["axalloc", "ruxruntime/alloc", "ruxfs/alloc"]
alloc = ["axalloc", "ruxruntime/alloc", "ruxfs/alloc", "ruxhal/alloc"]
alloc-tlsf = ["axalloc/tlsf"]
alloc-slab = ["axalloc/slab"]
alloc-buddy = ["axalloc/buddy"]
paging = ["alloc", "ruxhal/paging", "ruxruntime/paging"]
tls = ["alloc", "ruxhal/tls", "ruxruntime/tls", "ruxtask?/tls"]

# Multi-threading and scheduler
multitask = ["alloc", "ruxtask/multitask", "axsync/multitask", "ruxruntime/multitask"]
multitask = [
"alloc",
"ruxtask/multitask",
"axsync/multitask",
"ruxruntime/multitask",
]
sched_fifo = ["ruxtask/sched_fifo"]
sched_rr = ["ruxtask/sched_rr", "irq"]
sched_cfs = ["ruxtask/sched_cfs", "irq"]

# File system
fs = ["alloc", "dep:ruxfs", "ruxruntime/fs"]
fs = ["alloc", "dep:ruxfs", "ruxruntime/fs"]
blkfs = ["ruxdriver/virtio-blk", "ruxruntime/blkfs"]
myfs = ["ruxfs?/myfs"]
9pfs = []
Expand All @@ -50,10 +55,20 @@ myfs = ["ruxfs?/myfs"]
net = ["alloc", "ruxdriver/virtio-net", "dep:axnet", "ruxruntime/net"]

# Display
display = ["alloc", "ruxdriver/virtio-gpu", "dep:ruxdisplay", "ruxruntime/display"]
display = [
"alloc",
"ruxdriver/virtio-gpu",
"dep:ruxdisplay",
"ruxruntime/display",
]

# 9P
virtio-9p = ["9pfs", "ruxdriver/virtio-9p", "rux9p/virtio-9p", "ruxruntime/virtio-9p"]
virtio-9p = [
"9pfs",
"ruxdriver/virtio-9p",
"rux9p/virtio-9p",
"ruxruntime/virtio-9p",
]
net-9p = ["9pfs", "net", "rux9p/net-9p", "ruxruntime/net-9p"]

# Device drivers
Expand All @@ -71,6 +86,8 @@ log-level-info = ["axlog/log-level-info"]
log-level-debug = ["axlog/log-level-debug"]
log-level-trace = ["axlog/log-level-trace"]

tty = ["ruxhal/tty", "ruxruntime/tty", "alloc", "irq"]

[dependencies]
ruxruntime = { path = "../../modules/ruxruntime" }
ruxhal = { path = "../../modules/ruxhal" }
Expand Down
2 changes: 2 additions & 0 deletions api/ruxos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,7 @@ cfg-if = "1.0"
elf = { version = "0.7", default-features = false }
bitflags = "2.2"

lazy_init = { path = "../../crates/lazy_init" }

[build-dependencies]
bindgen = { version = "0.66" }
1 change: 1 addition & 0 deletions api/ruxos_posix_api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ typedef struct {{
"PROT_.+",
"MS_.+",
"MREMAP_.+",
"GRND_.*",
];

#[derive(Debug)]
Expand Down
2 changes: 2 additions & 0 deletions api/ruxos_posix_api/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@
#include <sys/uio.h>
#include <unistd.h>
#include <dirent.h>

#include <sys/random.h>
7 changes: 5 additions & 2 deletions api/ruxos_posix_api/src/imp/getrandom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ pub unsafe extern "C" fn sys_getrandom(buf: *mut c_void, buflen: size_t, flags:
if buf.is_null() {
return Err(LinuxError::EFAULT);
}
if flags != 0 {
return Err(LinuxError::EINVAL);

match flags as _ {
crate::ctypes::GRND_NONBLOCK => {}
crate::ctypes::GRND_RANDOM => {}
_ => return Err(LinuxError::EINVAL),
}
// fill the buffer 8 bytes at a time first, then fill the remaining bytes
let buflen_mod = buflen % (core::mem::size_of::<i64>() / core::mem::size_of::<u8>());
Expand Down
10 changes: 7 additions & 3 deletions api/ruxos_posix_api/src/imp/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* See the Mulan PSL v2 for more details.
*/

use crate::imp::fd_ops::get_file_like;
use crate::{imp::fd_ops::get_file_like, sys_getpgid};
use axerrno::LinuxError;
use core::ffi::c_int;

Expand Down Expand Up @@ -46,14 +46,18 @@ pub fn sys_ioctl(fd: c_int, request: usize, data: usize) -> c_int {
}
Ok(0)
}
TCGETS | TIOCSPGRP => {
TCGETS => {
debug!("sys_ioctl: tty TCGETS");
Ok(0)
}
TIOCSPGRP => {
warn!("stdout pretend to be tty");
Ok(0)
}
TIOCGPGRP => {
warn!("stdout TIOCGPGRP, pretend to be have a tty process group.");
unsafe {
*(data as *mut u32) = 0;
*(data as *mut u32) = sys_getpgid(0) as _;
}
Ok(0)
}
Expand Down
44 changes: 20 additions & 24 deletions api/ruxos_posix_api/src/imp/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,37 @@ use {
core::sync::atomic::{AtomicBool, Ordering},
};

fn console_read_bytes() -> Option<u8> {
let ret = ruxhal::console::getchar().map(|c| if c == b'\r' { b'\n' } else { c });
if let Some(c) = ret {
let _ = console_write_bytes(&[c]);
}
ret
}

fn console_write_bytes(buf: &[u8]) -> AxResult<usize> {
ruxhal::console::write_bytes(buf);
Ok(buf.len())
}

struct StdinRaw;
struct StdoutRaw;

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
static STDIO_TTY_NAME: lazy_init::LazyInit<alloc::string::String> = lazy_init::LazyInit::new();
#[cfg(not(feature = "alloc"))]
static STDIO_TTY_NAME: &str = "dummy";

fn get_stdio_tty_name() -> &'static str {
#[cfg(feature = "alloc")]
{
if !STDIO_TTY_NAME.is_init() {
let name = ruxhal::get_all_device_names().first().unwrap().clone();
STDIO_TTY_NAME.init_by(name);
}
}
&STDIO_TTY_NAME
}

impl Read for StdinRaw {
// Non-blocking read, returns number of bytes read.
fn read(&mut self, buf: &mut [u8]) -> AxResult<usize> {
let mut read_len = 0;
while read_len < buf.len() {
if let Some(c) = console_read_bytes() {
buf[read_len] = c;
read_len += 1;
} else {
break;
}
}
Ok(read_len)
Ok(ruxhal::tty_read(buf, get_stdio_tty_name()))
}
}

impl Write for StdoutRaw {
fn write(&mut self, buf: &[u8]) -> AxResult<usize> {
console_write_bytes(buf)
Ok(ruxhal::tty_write(buf, get_stdio_tty_name()))
}

fn flush(&mut self) -> AxResult {
Expand Down
10 changes: 10 additions & 0 deletions apps/c/busybox/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ruxgo_bld
compile_commands.json
.cache
/rootfs/bin/*
/rootfs/lib/*
/rootfs/dev
/rootfs/etc
/rootfs/proc
/rootfs/sys
/rootfs/tmp
24 changes: 24 additions & 0 deletions apps/c/busybox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# busybox

## Quick Start

1. Compile `busybox` or get its ELF binary (using Musl), then copy to `rootfs/bin`.

2. Copy the Musl dyanmic linker to `rootfs/lib`.

3. modify `axbuild.mk`, like:

```makefile
app-objs=main.o

ARGS = /bin/busybox,ls
ENVS =
V9P_PATH=${APP}/rootfs
```

4. Run

```sh
# in the RuxOS main directory.
make run ARCH=aarch64 A=apps/c/busybox V9P=y MUSL=y
```
5 changes: 5 additions & 0 deletions apps/c/busybox/axbuild.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
app-objs=main.o

ARGS = /bin/busybox,sh
ENVS =
V9P_PATH=${APP}/rootfs
34 changes: 34 additions & 0 deletions apps/c/busybox/config_linux.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[build]
compiler = "gcc"
app = "./busybox"

[os]
name = "ruxos"
services = [
"alloc",
"paging",
"musl",
"multitask",
"fs",
"pipe",
"poll",
"rtc",
"virtio-9p",
"irq",
"signal",
]
ulib = "ruxmusl"
develop = "y"

[os.platform]
name = "aarch64-qemu-virt"
mode = "release"
log = "debug"


[os.platform.qemu]
memory = "2g"
v9p = "y"
v9p_path = "./rootfs"
args = "/bin/busybox,sh"
qemu_log = "y"
11 changes: 11 additions & 0 deletions apps/c/busybox/features.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
paging
alloc
irq
musl
multitask
fs
pipe
poll
rtc
signal
virtio-9p
7 changes: 7 additions & 0 deletions apps/c/busybox/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
#include <unistd.h>

int main(int argc, char** argv, char**envp) {
execv(argv[0], argv);
return 0;
}
1 change: 1 addition & 0 deletions apps/c/dl/axbuild.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ app-objs=main.o
ARGS = /bin/hello
ENVS =
V9P_PATH=${APP}/rootfs

# make run ARCH=aarch64 A=apps/c/dl V9P=y MUSL=y LOG=debug
9 changes: 9 additions & 0 deletions crates/tty/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "tty"
version = "0.0.1"
edition = "2021"

[dependencies]
spinlock = { path = "../spinlock" }
lazy_init = { path = "../lazy_init" }
log = "0.4"
Loading
Loading