Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bugs of SYS_execve that uses SYS_mmap not correctly, and add exam…
Browse files Browse the repository at this point in the history
…ple app for ELF loader.
thesayol committed May 7, 2024
1 parent 9f8e68d commit e35b067
Showing 72 changed files with 881 additions and 3,619 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

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

10 changes: 1 addition & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@ members = [
"crates/axfs_vfs",
"crates/axio",
"crates/capability",
"crates/crate_interface",
"crates/driver_9p",
"crates/driver_block",
"crates/driver_common",
@@ -22,11 +21,8 @@ members = [
"crates/driver_virtio",
"crates/dtb",
"crates/flatten_objects",
"crates/handler_table",
"crates/kernel_guard",
"crates/lazy_init",
"crates/linked_list",
"crates/memory_addr",
"crates/page_table",
"crates/page_table_entry",
"crates/percpu",
@@ -37,7 +33,7 @@ members = [
"crates/spinlock",
"crates/timer_list",
"crates/tuple_for_each",
"crates/line_discipline",
"crates/tty",

"modules/axalloc",
"modules/axlog",
@@ -72,7 +68,3 @@ lto = true
[profile.reldebug]
inherits = "release"
debug = true


[patch.crates-io]
crate_interface = { path = "crates/crate_interface" }
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ RuxOS was inspired by [Unikraft](https://github.com/unikraft/unikraft) and [Arce
* [x] SMP scheduling with single run queue
* [x] File system
* [x] Compatible with Linux apps
* [x] Dynamically loading apps
* [ ] Interrupt driven device I/O
* [ ] Async I/O

@@ -63,6 +64,9 @@ The currently supported applications (Rust), as well as their dependent modules
| [iperf](apps/c/iperf/) | alloc, paging, net, fs, blkfs, select, fp_simd | A network performance test tool |
| [redis](apps/c/redis/) | alloc, paging, fp_simd, irq, multitask, fs, blkfs, net, pipe, epoll, poll, virtio-9p, rtc | A Redis server on Ruxos |
| [sqlite3](apps/c/sqlite3/) | alloc, paging, fs, fp_simd, blkfs | A simple test for Sqlite3 API |
| [cpp](apps/c/cpp/) | alloc, paging, irq, multitask, fs, random-hw | C++ benchmark |
| [dl](apps/c/dl/) | paging, alloc, irq, musl, multitask, fs, pipe, poll, rtc, signal, virtio-9p | An example for dynamically loading apps |


## Build & Run

4 changes: 2 additions & 2 deletions api/ruxfeat/Cargo.toml
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ 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"]
@@ -71,7 +71,7 @@ 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"]
tty=["ruxhal/tty", "ruxruntime/tty", "alloc", "irq"]

[dependencies]
ruxruntime = { path = "../../modules/ruxruntime" }
8 changes: 5 additions & 3 deletions api/ruxos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ authors = [
"Shiping Yuan <robert_yuan@pku.edu.com>",
]
description = "POSIX-compatible APIs for Ruxos modules"
license = "GPL-3.0-or-later OR Apache-2.0"
license = "Mulan PSL v2"
repository = "https://github.com/syswonder/ruxos/tree/main/api/ruxos_posix_api"

[features]
@@ -52,18 +52,20 @@ axnet = { path = "../../modules/axnet", optional = true }
# Other crates
axio = { path = "../../crates/axio" }
axerrno = { path = "../../crates/axerrno" }
memory_addr = { path = "../../crates/memory_addr" }
memory_addr = "0.1.0"
static_assertions = "1.1.0"
spin = { version = "0.9" }
spinlock = { path = "../../crates/spinlock" }
lazy_static = { version = "1.4", features = ["spin_no_std"] }
flatten_objects = { path = "../../crates/flatten_objects" }
page_table = { path = "../../crates/page_table" }
crate_interface = { path = "../../crates/crate_interface" }
crate_interface = "0.1.1"

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" }
15 changes: 14 additions & 1 deletion api/ruxos_posix_api/src/imp/execve/mod.rs
Original file line number Diff line number Diff line change
@@ -37,7 +37,11 @@ pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! {

// non 8B info
stack.push(&[0u8; 32], 16);
<<<<<<< HEAD
let rand = rand();
=======
let rand = unsafe { [sys_random(), sys_random()] };
>>>>>>> 5d0b4af (fix bugs of SYS_execve that uses SYS_mmap not correctly, and add example app for ELF loader.)
let p_rand = stack.push(&rand, 16);

// auxv
@@ -88,7 +92,6 @@ pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! {
// handle envs and args
let mut env_vec = vec![];
let mut arg_vec = vec![];
let mut argc = 0;

let mut envp = envp as *const usize;
unsafe {
@@ -104,7 +107,10 @@ pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! {
while *argv != 0 {
arg_vec.push(*argv);
argv = argv.add(1);
<<<<<<< HEAD
argc += 1;
=======
>>>>>>> 5d0b4af (fix bugs of SYS_execve that uses SYS_mmap not correctly, and add example app for ELF loader.)
}
arg_vec.push(0);
}
@@ -113,7 +119,11 @@ pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! {
stack.push(&auxv, 16);
stack.push(&env_vec, 8);
stack.push(&arg_vec, 8);
<<<<<<< HEAD
let sp = stack.push(&[argc], 8);
=======
let sp = stack.push(&[arg_vec.len() - 1], 8); // argc
>>>>>>> 5d0b4af (fix bugs of SYS_execve that uses SYS_mmap not correctly, and add example app for ELF loader.)

// try run
debug!(
@@ -148,11 +158,14 @@ fn set_sp_and_jmp(sp: usize, entry: usize) -> ! {
unreachable!("sys_execve: unknown arch, sp 0x{sp:x}, entry 0x{entry:x}");
}

<<<<<<< HEAD
/// for AT_RANDOM
fn rand() -> [i64; 2] {
unsafe { [sys_random(), sys_random()] }
}

=======
>>>>>>> 5d0b4af (fix bugs of SYS_execve that uses SYS_mmap not correctly, and add example app for ELF loader.)
fn platform() -> usize {
#[cfg(target_arch = "aarch64")]
const PLATFORM_STRING: &[u8] = b"aarch64\0";
20 changes: 20 additions & 0 deletions api/ruxos_posix_api/src/imp/execve/stack.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use alloc::{vec, vec::Vec};

<<<<<<< HEAD
use ruxconfig::TASK_STACK_SIZE;

const STACK_SIZE: usize = TASK_STACK_SIZE;
=======
const STACK_SIZE: usize = ruxconfig::TASK_STACK_SIZE;
>>>>>>> 5d0b4af (fix bugs of SYS_execve that uses SYS_mmap not correctly, and add example app for ELF loader.)

#[derive(Debug)]
pub struct Stack {
@@ -20,6 +24,7 @@ impl Stack {
top: STACK_SIZE,
}
}
<<<<<<< HEAD

/// panic if overflow
fn panic_if_of(&self) {
@@ -44,6 +49,21 @@ impl Stack {
self.top = self.align(align);

self.panic_if_of();
=======

/// addr of top of stack
pub fn sp(&self) -> usize {
self.data.as_ptr() as usize + self.top
}

/// push data to stack and return the addr of sp
pub fn push<T>(&mut self, data: &[T], align: usize) -> usize {
// move sp to right place
self.top -= core::mem::size_of_val(data);
self.top = memory_addr::align_down(self.top, align);

assert!(self.top <= self.data.len(), "sys_execve: stack overflow.");
>>>>>>> 5d0b4af (fix bugs of SYS_execve that uses SYS_mmap not correctly, and add example app for ELF loader.)

// write data into stack
let sp = self.sp() as *mut T;
2 changes: 1 addition & 1 deletion api/ruxos_posix_api/src/imp/ioctl.rs
Original file line number Diff line number Diff line change
@@ -102,7 +102,7 @@ pub fn sys_ioctl(fd: c_int, request: usize, data: usize) -> c_int {
TIOCGPGRP => {
warn!("stdout TIOCGPGRP, pretend to be have a tty process group.");
unsafe {
*(data as *mut u32) = sys_getpgid(0)as _;
*(data as *mut u32) = sys_getpgid(0) as _;
}
Ok(0)
}
74 changes: 32 additions & 42 deletions api/ruxos_posix_api/src/imp/stdio.rs
Original file line number Diff line number Diff line change
@@ -19,60 +19,50 @@ 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())
}
// 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 names = ruxhal::get_all_device_names();

// read the first tty device
if let Some(name) = names.get(0) {
return Ok(ruxhal::tty_read(buf, name));
};

Ok(0)

// 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> {
let mut a = alloc::vec![];
for u in buf {
a.push(*u as char);
}
let names = ruxhal::get_all_device_names();
let mut len = 0;
if let Some(name) = names.get(0) {
len = ruxhal::tty_write(buf, name);
}
Ok(len)
// console_write_bytes(buf)
Ok(ruxhal::tty_write(buf, get_stdio_tty_name()))
}

fn flush(&mut self) -> AxResult {
1 change: 0 additions & 1 deletion api/ruxos_posix_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
#![cfg_attr(all(not(test), not(doc)), no_std)]
#![feature(ip_in_core)]
#![feature(result_option_inspect)]
#![feature(doc_cfg)]
#![feature(doc_auto_cfg)]
#![allow(clippy::missing_safety_doc)]
Loading

0 comments on commit e35b067

Please sign in to comment.