Skip to content

Commit

Permalink
[syscall] Split syscalls into multiple files according to the syscall…
Browse files Browse the repository at this point in the history
… categories
  • Loading branch information
Azure-stars committed Sep 20, 2024
1 parent 6cb97af commit b306a18
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 239 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ axmm = { git = "https://github.com/arceos-org/arceos.git" }
axtask = { git = "https://github.com/arceos-org/arceos.git" }
axsync = { git = "https://github.com/arceos-org/arceos.git" }
axruntime = { git = "https://github.com/arceos-org/arceos.git", features = ["multitask"] }

[target.'cfg(target_arch = "x86_64")'.dependencies]
x86 = "0.52"
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern crate axstd;

mod loader;
mod mm;
mod syscall;
mod syscall_imp;
mod task;
use alloc::sync::Arc;

Expand Down
33 changes: 20 additions & 13 deletions src/mm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use alloc::string::ToString;
use axerrno::AxResult;
use memory_addr::{MemoryAddr, PageIter4K, VirtAddr, PAGE_SIZE_4K};

use axhal::mem::phys_to_virt;
use axhal::paging::MappingFlags;
use axhal::trap::{register_trap_handler, PAGE_FAULT};
use axhal::{
mem::phys_to_virt,
paging::MappingFlags,
trap::{register_trap_handler, PAGE_FAULT},
};

use axmm::AddrSpace;
use axtask::TaskExtRef;

Expand Down Expand Up @@ -71,33 +74,37 @@ pub fn load_user_app(app_name: &str) -> AxResult<(VirtAddr, VirtAddr, AddrSpace)
// TDOO: flush the I-cache
}

let ustack_base = uspace.end();
// The user stack is divided into two parts:
// `ustack_bottom` -> `ustack top`: It is the stack space that users actually read and write.
// `ustack_top` -> `ustack end`: It is the space that contains the arguments, environment variables and auxv passed to the app.
// When the app starts running, the stack pointer points to `ustack_top`.
let ustack_end = uspace.end();
let ustack_size = crate::USER_STACK_SIZE;
let ustack_top = ustack_base - ustack_size;
let ustack_bottom = ustack_end - ustack_size;
debug!(
"Mapping user stack: {:#x?} -> {:#x?}",
ustack_top, ustack_base
ustack_bottom, ustack_end
);
// FIXME: Add more arguments and environment variables
let (stack_data, ustack_bottom) = elf_parser::get_app_stack_region(
let (stack_data, ustack_top) = elf_parser::get_app_stack_region(
&[app_name.to_string()],
&[],
&elf_info.auxv,
ustack_top,
ustack_bottom,
ustack_size,
);
uspace.map_alloc(
ustack_top,
ustack_bottom,
ustack_size,
MappingFlags::READ | MappingFlags::WRITE | MappingFlags::USER,
true,
)?;
{
// Copy the stack data to the user stack
let ustack_bottom_align = VirtAddr::from_usize(ustack_bottom).align_down_4k();
let stack_data_offset = ustack_bottom_align - ustack_top;
let ustack_top_align = VirtAddr::from_usize(ustack_top).align_down_4k();
let stack_data_offset = ustack_top_align - ustack_bottom;
// Only copy data which contains args, envs and auxv.
for (idx, vaddr) in PageIter4K::new(ustack_bottom_align, ustack_base)
for (idx, vaddr) in PageIter4K::new(ustack_top_align, ustack_end)
.expect("Failed to create page iterator")
.enumerate()
{
Expand All @@ -116,7 +123,7 @@ pub fn load_user_app(app_name: &str) -> AxResult<(VirtAddr, VirtAddr, AddrSpace)
}
}

Ok((elf_info.entry, VirtAddr::from(ustack_bottom), uspace))
Ok((elf_info.entry, VirtAddr::from(ustack_top), uspace))
}

#[register_trap_handler(PAGE_FAULT)]
Expand Down
225 changes: 0 additions & 225 deletions src/syscall.rs

This file was deleted.

18 changes: 18 additions & 0 deletions src/syscall_imp/fs/ctl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use core::ffi::c_void;

use crate::syscall_body;

/// The ioctl() system call manipulates the underlying device parameters
/// of special files.
///
/// # Arguments
/// * `fd` - The file descriptor
/// * `op` - The request code. It is of type unsigned long in glibc and BSD,
/// and of type int in musl and other UNIX systems.
/// * `argp` - The argument to the request. It is a pointer to a memory location
pub(crate) fn sys_ioctl(_fd: i32, _op: usize, _argp: *mut c_void) -> i32 {
syscall_body!(sys_ioctl, {
warn!("Unimplemented syscall: SYS_IOCTL");
Ok(0)
})
}
14 changes: 14 additions & 0 deletions src/syscall_imp/fs/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use core::ffi::c_void;

use arceos_posix_api as api;

pub(crate) fn sys_read(fd: i32, buf: *mut c_void, count: usize) -> isize {
api::sys_read(fd, buf, count)
}
pub(crate) fn sys_write(fd: i32, buf: *const c_void, count: usize) -> isize {
api::sys_write(fd, buf, count)
}

pub(crate) fn sys_writev(fd: i32, iov: *const api::ctypes::iovec, iocnt: i32) -> isize {
unsafe { api::sys_writev(fd, iov, iocnt) }
}
5 changes: 5 additions & 0 deletions src/syscall_imp/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod ctl;
mod io;

pub(crate) use ctl::*;
pub(crate) use io::*;
Loading

0 comments on commit b306a18

Please sign in to comment.