Skip to content

Commit

Permalink
feat: testcase on oskernel
Browse files Browse the repository at this point in the history
  • Loading branch information
Fediory committed Mar 29, 2024
1 parent d30ad90 commit 0ef9435
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ os/new_cmd

# kernel build
os/target/
kernel.bin

# user programs build
user/riscv64/
Expand Down
2 changes: 2 additions & 0 deletions os/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ ifeq ($(BOARD), 2k1000)
@echo $(CURDIR)
@./fat $(MINITERM_START_CMD) $(LA_2K1000_PORT_FREQ)
else ifeq ($(BOARD), laqemu)
@cp ./target/loongarch64-unknown-linux-gnu/debug/os.bin ../os.bin
@mv ../os.bin ../kernel.bin
@./la_fat $(QEMU_2K1000)
endif

Expand Down
1 change: 1 addition & 0 deletions os/src/arch/la64/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub const MEMORY_SIZE: usize = 0x1000_0000;
pub const USER_STACK_SIZE: usize = PAGE_SIZE * 40;
pub const USER_HEAP_SIZE: usize = PAGE_SIZE * 20;
pub const SYSTEM_TASK_LIMIT: usize = 128;
pub const DEFAULT_FD_LIMIT: usize = 128;
pub const SYSTEM_FD_LIMIT: usize = 256;
pub const PAGE_SIZE: usize = 0x1000;
pub const PAGE_SIZE_BITS: usize = PAGE_SIZE.trailing_zeros() as usize;
Expand Down
1 change: 1 addition & 0 deletions os/src/arch/la64/syscall_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub const SYSCALL_WAIT4: usize = 260; // wait is implemented as wait4(pid, statu
pub const SYSCALL_PRLIMIT: usize = 261;
pub const SYSCALL_RENAMEAT2: usize = 276;
pub const SYSCALL_MEMBARRIER: usize = 283;
pub const SYSCALL_STATX: usize = 291;
pub const SYSCALL_FACCESSAT2: usize = 439;
// Not standard POSIX sys_call
pub const SYSCALL_LS: usize = 500;
Expand Down
4 changes: 2 additions & 2 deletions os/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use self::fat32::{BlockDevice, DiskInodeType};

use self::{cache::PageCache, directory_tree::DirectoryTreeNode, file_trait::File};
use crate::{
config::SYSTEM_FD_LIMIT,
config::{SYSTEM_FD_LIMIT,DEFAULT_FD_LIMIT},
mm::{Frame, UserBuffer},
syscall::errno::*,
};
Expand Down Expand Up @@ -265,7 +265,7 @@ pub struct FdTable {

#[allow(unused)]
impl FdTable {
pub const DEFAULT_FD_LIMIT: usize = 64;
pub const DEFAULT_FD_LIMIT: usize = DEFAULT_FD_LIMIT;
pub const SYSTEM_FD_LIMIT: usize = SYSTEM_FD_LIMIT;
pub fn new(inner: Vec<Option<FileDescriptor>>) -> Self {
Self {
Expand Down
75 changes: 67 additions & 8 deletions os/src/syscall/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ pub fn sys_close(fd: usize) -> isize {
/// # Warning
/// Only O_CLOEXEC is supported now
pub fn sys_pipe2(pipefd: usize, flags: u32) -> isize {
// println!("enter pipe2");

// judging flags
const VALID_FLAGS: OpenFlags = OpenFlags::from_bits_truncate(
0o2000000 /* O_CLOEXEC */ | 0o40000 /* O_DIRECT */ | 0o4000, /* O_NONBLOCK */
);
Expand All @@ -408,11 +411,12 @@ pub fn sys_pipe2(pipefd: usize, flags: u32) -> isize {
return EINVAL;
}
};

let task = current_task().unwrap();
let mut fd_table = task.files.lock();
let (pipe_read, pipe_write) = make_pipe();
let read_fd = match fd_table.insert(FileDescriptor::new(
flags.contains(OpenFlags::O_CLOEXEC),
flags.contains(OpenFlags::O_CLOEXEC) || flags.contains(OpenFlags::O_RDONLY),
false,
pipe_read,
)) {
Expand All @@ -425,25 +429,29 @@ pub fn sys_pipe2(pipefd: usize, flags: u32) -> isize {
pipe_write,
)) {
Ok(fd) => fd,
Err(errno) => return errno,
Err(errno) => {
println!("error pipe");
return errno
},
};

info!(
"[sys_pipe2] read_fd: {}, write_fd: {}, flags: {:?}",
read_fd, write_fd, flags
);

let token = task.get_user_token();
if copy_to_user_array(
token,
[read_fd as u32, write_fd as u32].as_ptr(),
pipefd as *mut u32,
2,
4,
)
.is_err()
{
log::error!("[sys_pipe2] Failed to copy to {:?}", pipefd);
return EFAULT;
};
info!(
"[sys_pipe2] read_fd: {}, write_fd: {}, flags: {:?}",
read_fd, write_fd, flags
);
SUCCESS
}

Expand Down Expand Up @@ -481,6 +489,7 @@ pub fn sys_getdents64(fd: usize, dirp: *mut u8, count: usize) -> isize {
}

pub fn sys_dup(oldfd: usize) -> isize {
// println!("enter dup");
let task = current_task().unwrap();
let mut fd_table = task.files.lock();
let old_file_descriptor = match fd_table.get_ref(oldfd) {
Expand Down Expand Up @@ -526,11 +535,19 @@ pub fn sys_dup3(oldfd: usize, newfd: usize, flags: u32) -> isize {
Ok(file_descriptor) => file_descriptor.clone(),
Err(errno) => return errno,
};
file_descriptor.set_cloexec(is_cloexec);
// println!("{}",is_cloexec);

if is_cloexec {
file_descriptor.set_cloexec(is_cloexec);
}

match fd_table.insert_at(file_descriptor, newfd) {
Ok(fd) => fd as isize,
Err(errno) => errno,
}



}

// This syscall is not complete at all, only /read proc/self/exe
Expand Down Expand Up @@ -645,6 +662,48 @@ pub fn sys_fstat(fd: usize, statbuf: *mut u8) -> isize {
SUCCESS
}

pub fn sys_statx(dirfd: usize, path: *const u8, buf: *mut u8, flags: u32,) -> isize {
let token = current_user_token();
let path = match translated_str(token, path) {
Ok(path) => path,
Err(errno) => return errno,
};


let flags = match OpenFlags::from_bits(flags) {
Some(flags) => flags,
None => {
warn!("[sys_statx] unknown flags");
return EINVAL;
}
};

let task = current_task().unwrap();
let file_descriptor = match dirfd {
AT_FDCWD => task.fs.lock().working_inode.as_ref().clone(),
fd => {
let fd_table = task.files.lock();
match fd_table.get_ref(fd) {
Ok(file_descriptor) => file_descriptor.clone(),
Err(errno) => return errno,
}
}
};

match file_descriptor.open(&path, flags, false) {
Ok(file_descriptor) => {
if copy_to_user(token, &file_descriptor.get_stat(), buf as *mut Stat).is_err() {
log::error!("[sys_fstatat] Failed to copy to {:?}", buf);
return EFAULT;
};
SUCCESS
}
Err(errno) => errno,
}
}



#[repr(C)]
#[derive(Clone, Copy)]
pub struct Statfs {
Expand Down
7 changes: 7 additions & 0 deletions os/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub fn syscall_name(id: usize) -> &'static str {
SYSCALL_READLINKAT => "readlinkat",
SYSCALL_FSTATAT => "fstatat",
SYSCALL_FSTAT => "fstat",
SYSCALL_STATX => "statx",
SYSCALL_STATFS => "statfs",
SYSCALL_FTRUNCATE => "ftruncate",
SYSCALL_FSYNC => "fsync",
Expand Down Expand Up @@ -197,6 +198,12 @@ pub fn syscall(syscall_id: usize, args: [usize; 6]) -> isize {
args[3] as u32,
),
SYSCALL_FSTAT => sys_fstat(args[0], args[1] as *mut u8),
SYSCALL_STATX => sys_statx(
args[0],
args[1] as *const u8,
args[2] as *mut u8,
args[3] as u32,
),
SYSCALL_FTRUNCATE => sys_ftruncate(args[0], args[1] as isize),
SYSCALL_FSYNC => sys_fsync(args[0]),
SYSCALL_UTIMENSAT => sys_utimensat(
Expand Down

0 comments on commit 0ef9435

Please sign in to comment.