Skip to content

Commit

Permalink
Merge branch 'mp4dev' of github.com:ken4647/ruxos into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ken4647 committed Dec 25, 2024
2 parents 43f3ac8 + 519c0b1 commit 59adfc1
Show file tree
Hide file tree
Showing 97 changed files with 2,686 additions and 1,112 deletions.
40 changes: 35 additions & 5 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 @@ -42,6 +42,7 @@ members = [
"modules/ruxnet",
"modules/axsync",
"modules/rux9p",
"modules/ruxmm",
"modules/ruxconfig",
"modules/ruxdisplay",
"modules/ruxdriver",
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ V ?=
# App options
A ?= apps/c/helloworld
APP ?= $(A)
FEATURES ?=
FEATURES ?= multitask paging fs
APP_FEATURES ?=

# QEMU options
Expand Down
3 changes: 2 additions & 1 deletion api/ruxfeat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ 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"]
paging = ["alloc", "ruxhal/paging", "ruxtask/paging", "ruxruntime/paging", "ruxmm/paging"]
tls = ["alloc", "ruxhal/tls", "ruxruntime/tls", "ruxtask?/tls"]

# Multi-threading and scheduler
Expand Down Expand Up @@ -96,6 +96,7 @@ tty = ["ruxhal/tty", "ruxruntime/tty", "alloc", "irq"]
[dependencies]
ruxruntime = { path = "../../modules/ruxruntime" }
ruxhal = { path = "../../modules/ruxhal" }
ruxmm = { path = "../../modules/ruxmm" }
axlog = { path = "../../modules/axlog" }
axalloc = { path = "../../modules/axalloc", optional = true }
ruxdriver = { path = "../../modules/ruxdriver", optional = true }
Expand Down
5 changes: 3 additions & 2 deletions api/ruxos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ default = []

smp = ["ruxfeat/smp"]
alloc = ["dep:axalloc", "ruxfeat/alloc"]
paging = ["alloc", "ruxfeat/paging"]
paging = ["alloc", "ruxfeat/paging", "ruxmm"]
multitask = ["ruxfeat/multitask", "ruxtask/multitask", "dep:ruxfutex"]
fd = ["alloc"]
fs = ["dep:ruxfs", "ruxfeat/fs", "fd"]
net = ["dep:ruxnet", "ruxfeat/net", "fd"]
signal = ["ruxruntime/signal", "ruxhal/signal"]
signal = ["ruxruntime/signal", "ruxhal/signal", "ruxtask/signal"]
pipe = ["fd"]
select = ["fd"]
epoll = ["fd"]
Expand All @@ -44,6 +44,7 @@ axlog = { path = "../../modules/axlog" }
ruxhal = { path = "../../modules/ruxhal" }
axsync = { path = "../../modules/axsync" }
ruxfdtable = { path = "../../modules/ruxfdtable" }
ruxmm = { path = "../../modules/ruxmm", optional = true }
ruxfutex = { path = "../../modules/ruxfutex", optional = true }
axalloc = { path = "../../modules/axalloc", optional = true }
ruxtask = { path = "../../modules/ruxtask", optional = true }
Expand Down
1 change: 1 addition & 0 deletions api/ruxos_posix_api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ typedef struct {{
"kstat",
"stack_t",
"ino_t",
"rusage",
"dirent",
];
let allow_vars = [
Expand Down
16 changes: 16 additions & 0 deletions api/ruxos_posix_api/src/imp/execve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod stack;

use alloc::vec;
use core::ffi::c_char;
use ruxtask::current;

use crate::{
config,
Expand All @@ -14,9 +15,14 @@ use crate::{

/// int execve(const char *pathname, char *const argv[], char *const envp[] );
pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! {
debug!(
"execve: pathname {:?}, argv {:?}, envp {:?}",
pathname, argv, envp
);
use auxv::*;

let path = char_ptr_to_str(pathname).unwrap();
debug!("sys_execve: path is {}", path);
let prog = load_elf::ElfProg::new(path);

// get entry
Expand All @@ -33,6 +39,7 @@ pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! {
};

// create stack
// memory broken, use stack alloc to store args and envs
let mut stack = stack::Stack::new();

// non 8B info
Expand Down Expand Up @@ -119,6 +126,15 @@ pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! {
prog.entry
);

// TODO: may lead to memory leaky, release stack after the change of stack
current().set_stack_top(stack.stack_top() - stack.stack_size(), stack.stack_size());
warn!(
"sys_execve: current_id_name {:?}, stack top 0x{:x}, size 0x{:x}",
current().id_name(),
current().stack_top(),
stack.stack_size()
);

set_sp_and_jmp(sp, entry);
}

Expand Down
31 changes: 27 additions & 4 deletions api/ruxos_posix_api/src/imp/execve/stack.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use alloc::{vec, vec::Vec};
use alloc::vec::Vec;
use ruxtask::task::TaskStack;

const STACK_SIZE: usize = ruxconfig::TASK_STACK_SIZE;

#[derive(Debug)]
pub struct Stack {
/// task stack
task_stack: TaskStack,
/// stack
data: Vec<u8>,
/// index of top byte of stack
Expand All @@ -13,9 +16,15 @@ pub struct Stack {
impl Stack {
/// alloc a stack
pub fn new() -> Self {
Self {
data: vec![0u8; STACK_SIZE],
top: STACK_SIZE,
let task_stack = TaskStack::alloc(STACK_SIZE);
unsafe {
let start = task_stack.top().as_mut_ptr().sub(STACK_SIZE);

Self {
task_stack,
data: Vec::from_raw_parts(start, STACK_SIZE, STACK_SIZE),
top: STACK_SIZE,
}
}
}

Expand All @@ -24,6 +33,14 @@ impl Stack {
self.data.as_ptr() as usize + self.top
}

pub fn stack_size(&self) -> usize {
self.data.len()
}

pub fn stack_top(&self) -> usize {
self.task_stack.top().into()
}

/// 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
Expand All @@ -41,3 +58,9 @@ impl Stack {
sp as usize
}
}

impl Drop for Stack {
fn drop(&mut self) {
error!("execve's stack dropped. {:#?}", self);
}
}
62 changes: 19 additions & 43 deletions api/ruxos_posix_api/src/imp/fd_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
* See the Mulan PSL v2 for more details.
*/

use alloc::sync::Arc;
use core::ffi::c_int;

use axerrno::{LinuxError, LinuxResult};
use ruxfdtable::{FileLike, RuxStat, RuxTimeSpec, FD_TABLE, RUX_FILE_LIMIT};
use ruxfdtable::{RuxStat, RuxTimeSpec};
use ruxtask::current;
pub use ruxtask::fs::{add_file_like, close_file_like, get_file_like, RUX_FILE_LIMIT};

use super::stdio::{stdin, stdout};
use crate::ctypes;

impl From<ctypes::timespec> for RuxTimeSpec {
Expand Down Expand Up @@ -124,39 +124,6 @@ impl From<RuxStat> for ctypes::stat {
}
}

lazy_static::lazy_static! {
static ref MUST_EXEC: usize = {
FD_TABLE.write().add_at(0, Arc::new(stdin()) as _).unwrap(); // stdin
FD_TABLE.write().add_at(1, Arc::new(stdout()) as _).unwrap(); // stdout
FD_TABLE.write().add_at(2, Arc::new(stdout()) as _).unwrap(); // stderr
0
};
}

pub fn get_file_like(fd: c_int) -> LinuxResult<Arc<dyn FileLike>> {
let _exec = *MUST_EXEC;
FD_TABLE
.read()
.get(fd as usize)
.cloned()
.ok_or(LinuxError::EBADF)
}

pub fn add_file_like(f: Arc<dyn FileLike>) -> LinuxResult<c_int> {
let _exec = *MUST_EXEC;
Ok(FD_TABLE.write().add(f).ok_or(LinuxError::EMFILE)? as c_int)
}

pub fn close_file_like(fd: c_int) -> LinuxResult {
let _exec = *MUST_EXEC;
let f = FD_TABLE
.write()
.remove(fd as usize)
.ok_or(LinuxError::EBADF)?;
drop(f);
Ok(())
}

/// Close a file by `fd`.
pub fn sys_close(fd: c_int) -> c_int {
debug!("sys_close <= {}", fd);
Expand All @@ -169,7 +136,7 @@ pub fn sys_close(fd: c_int) -> c_int {
fn dup_fd(old_fd: c_int) -> LinuxResult<c_int> {
let f = get_file_like(old_fd)?;
let new_fd = add_file_like(f)?;
Ok(new_fd)
Ok(new_fd as _)
}

/// Duplicate a file descriptor.
Expand All @@ -195,11 +162,13 @@ pub fn sys_dup2(old_fd: c_int, new_fd: c_int) -> c_int {
if new_fd as usize >= RUX_FILE_LIMIT {
return Err(LinuxError::EBADF);
}
close_file_like(new_fd)?;
close_file_like(new_fd as _)?;

let f = get_file_like(old_fd)?;
FD_TABLE
.write()
let f = get_file_like(old_fd as _)?;
let binding_task = current();
let mut binding_fs = binding_task.fs.lock();
let fd_table = &mut binding_fs.as_mut().unwrap().fd_table;
fd_table
.add_at(new_fd as usize, f)
.ok_or(LinuxError::EMFILE)?;

Expand Down Expand Up @@ -238,6 +207,11 @@ pub fn sys_fcntl(fd: c_int, cmd: c_int, arg: usize) -> c_int {
syscall_body!(sys_fcntl, {
match cmd as u32 {
ctypes::F_DUPFD => dup_fd(fd),
ctypes::F_GETFD => {
// Return (as the function result) the file descriptor flags; the arg is ignored.
// temporary unsupport CLOEXEC flag
Ok(0)
}
ctypes::F_DUPFD_CLOEXEC => {
// TODO: Change fd flags
dup_fd(fd)
Expand Down Expand Up @@ -267,8 +241,10 @@ pub fn sys_fcntl(fd: c_int, cmd: c_int, arg: usize) -> c_int {
if arg == 0 || arg == 1 || arg == 2 {
return Ok(0);
}
FD_TABLE
.write()
let binding_task = current();
let mut binding_fs = binding_task.fs.lock();
let fd_table = &mut binding_fs.as_mut().unwrap().fd_table;
fd_table
.add_at(arg, get_file_like(fd)?)
.ok_or(LinuxError::EMFILE)?;
let _ = close_file_like(fd);
Expand Down
Loading

0 comments on commit 59adfc1

Please sign in to comment.