From 0767c556c8169d387862e05e2b5e5e801341094d Mon Sep 17 00:00:00 2001 From: yufeng <321353225@qq.com> Date: Tue, 18 Apr 2023 18:20:35 +0800 Subject: [PATCH] skip step1 --- arch/Cargo.toml | 2 +- arch/src/riscv64/timer.rs | 4 ++++ crates/backtrace/Cargo.toml | 2 +- kernel/Cargo.toml | 2 +- kernel/src/syscall/consts.rs | 9 ++++++++ kernel/src/syscall/fd.rs | 36 +++++++++++++++++++++++++++++- kernel/src/syscall/mm.rs | 3 ++- kernel/src/syscall/mod.rs | 15 ++++++++----- kernel/src/syscall/time.rs | 12 ++++++++++ kernel/src/tasks/initproc.rs | 8 ++++--- kernel/src/tasks/mod.rs | 7 +++++- modules/devfs/Cargo.toml | 2 +- modules/devices/Cargo.toml | 2 +- modules/executor/Cargo.toml | 2 +- modules/executor/src/lib.rs | 13 +++++++++++ modules/executor/src/task.rs | 8 +++++-- modules/frame_allocator/Cargo.toml | 2 +- modules/fs/Cargo.toml | 2 +- modules/fs/src/fatfs_shim.rs | 14 +++++++----- modules/hal/Cargo.toml | 2 +- modules/kalloc/Cargo.toml | 2 +- modules/logging/Cargo.toml | 2 +- modules/logging/src/lib.rs | 4 ++-- modules/ramfs/Cargo.toml | 2 +- 24 files changed, 124 insertions(+), 33 deletions(-) diff --git a/arch/Cargo.toml b/arch/Cargo.toml index ef7c35d..7059bee 100644 --- a/arch/Cargo.toml +++ b/arch/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" [dependencies] riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } -log = "0.4.17" +log = "0.4" bitflags = "2.0.2" diff --git a/arch/src/riscv64/timer.rs b/arch/src/riscv64/timer.rs index 6c59bdf..79db3ec 100644 --- a/arch/src/riscv64/timer.rs +++ b/arch/src/riscv64/timer.rs @@ -12,6 +12,10 @@ pub fn get_time_ms() -> usize { time::read() / (CLOCK_FREQ / MSEC_PER_SEC) } +pub fn get_time() -> usize { + time::read() +} + // è®¾ç½®ä¸‹ä¸€æ¬¡æ—¶é’Ÿä¸æ–è§¦å‘æ—¶é—´ pub fn set_next_timeout() { // 调用sbi设置定时器 diff --git a/crates/backtrace/Cargo.toml b/crates/backtrace/Cargo.toml index a22edc8..f719ed5 100644 --- a/crates/backtrace/Cargo.toml +++ b/crates/backtrace/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -log = "0.4.17" +log = "0.4" riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 77fdf69..2e8c9e8 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -10,7 +10,7 @@ frame_allocator = { path = "../modules/frame_allocator" } logging = { path = "../modules/logging" } panic_handler = { path = "../modules/panic_handler" } kalloc = { path = "../modules/kalloc" } -log = "0.4.17" +log = "0.4" devices = { path = "../modules/devices" } hal = { path = "../modules/hal" } arch = { path = "../arch" } diff --git a/kernel/src/syscall/consts.rs b/kernel/src/syscall/consts.rs index d695a93..0af64ff 100644 --- a/kernel/src/syscall/consts.rs +++ b/kernel/src/syscall/consts.rs @@ -265,3 +265,12 @@ bitflags! { const MAP_FILE = 0; } } + +#[repr(C)] +pub struct Dirent { + pub ino: u64, // ç´¢å¼•ç»“ç‚¹å· + pub off: i64, // 到下一个direntçš„åç§» + pub reclen: u16, // 当å‰dirent的长度 + pub ftype: u8, // 文件类型 + pub name: [u8; 0], // 文件å +} diff --git a/kernel/src/syscall/fd.rs b/kernel/src/syscall/fd.rs index d4ac5c4..f098333 100644 --- a/kernel/src/syscall/fd.rs +++ b/kernel/src/syscall/fd.rs @@ -1,3 +1,5 @@ +use core::mem::size_of; + use executor::current_task; use fs::{ mount::{open, rebuild_path, umount}, @@ -8,7 +10,7 @@ use log::debug; use crate::syscall::{ c2rust_buffer, c2rust_ref, - consts::{from_vfs, AT_CWD}, + consts::{from_vfs, AT_CWD, Dirent}, }; use super::{c2rust_str, consts::LinuxError}; @@ -222,3 +224,35 @@ pub async fn sys_umount2(special: usize, flags: usize) -> Result<usize, LinuxErr Ok(0) } + +pub async fn sys_getdents64(fd: usize, buf_ptr: usize, len: usize) -> Result<usize, LinuxError> { + debug!( + "sys_getdents64 @ fd: {}, buf_ptr: {:#X}, len: {}", + fd, buf_ptr, len + ); + + let file = current_task() + .as_user_task() + .unwrap() + .inner_map(|x| x.fd_table.get(fd)) + .unwrap(); + + let mut ptr = buf_ptr; + for i in file.read_dir().map_err(from_vfs)? { + let file_bytes = i.filename.as_bytes(); + let current_len = size_of::<Dirent>() + file_bytes.len() + 1; + + if len - (ptr - buf_ptr) < current_len { + break; + } + let dirent = c2rust_ref(ptr as *mut Dirent); + dirent.ino = 0; + dirent.off = current_len as i64; + dirent.reclen = current_len as u16; + let buffer = c2rust_buffer(dirent.name.as_mut_ptr(), file_bytes.len() + 1); + buffer[..file_bytes.len()].copy_from_slice(file_bytes); + buffer[file_bytes.len()] = b'\0'; + ptr = ptr + current_len; + } + Ok(ptr - buf_ptr) +} diff --git a/kernel/src/syscall/mm.rs b/kernel/src/syscall/mm.rs index 86733ed..8d20c82 100644 --- a/kernel/src/syscall/mm.rs +++ b/kernel/src/syscall/mm.rs @@ -69,7 +69,8 @@ pub async fn sys_munmap(start: usize, len: usize) -> Result<usize, LinuxError> { let current_task = current_task().as_user_task().unwrap(); current_task.inner_map(|mut x| { - x.memset.drain_filter(|x| (start..start + len).contains(&x.vpn.to_addr())); + x.memset + .drain_filter(|x| (start..start + len).contains(&x.vpn.to_addr())); }); Ok(0) } diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index d5be8a8..d1d8bed 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -13,13 +13,14 @@ use log::warn; use self::{ consts::{ LinuxError, SYS_BRK, SYS_CHDIR, SYS_CLONE, SYS_CLOSE, SYS_DUP, SYS_DUP3, SYS_EXECVE, - SYS_EXIT, SYS_FSTAT, SYS_GETCWD, SYS_GETPID, SYS_GETPPID, SYS_GETTIMEOFDAY, SYS_MKDIRAT, - SYS_MMAP, SYS_MOUNT, SYS_NANOSLEEP, SYS_OPENAT, SYS_PIPE2, SYS_READ, SYS_SCHED_YIELD, - SYS_UMOUNT2, SYS_UNAME, SYS_UNLINKAT, SYS_WAIT4, SYS_WRITE, SYS_MUNMAP, + SYS_EXIT, SYS_FSTAT, SYS_GETCWD, SYS_GETDENTS, SYS_GETPID, SYS_GETPPID, SYS_GETTIMEOFDAY, + SYS_MKDIRAT, SYS_MMAP, SYS_MOUNT, SYS_MUNMAP, SYS_NANOSLEEP, SYS_OPENAT, SYS_PIPE2, + SYS_READ, SYS_SCHED_YIELD, SYS_TIMES, SYS_UMOUNT2, SYS_UNAME, SYS_UNLINKAT, SYS_WAIT4, + SYS_WRITE, }, fd::{ - sys_close, sys_dup, sys_dup3, sys_fstat, sys_mkdir_at, sys_mount, sys_openat, sys_pipe2, - sys_read, sys_umount2, sys_unlinkat, sys_write, + sys_close, sys_dup, sys_dup3, sys_fstat, sys_getdents64, sys_mkdir_at, sys_mount, + sys_openat, sys_pipe2, sys_read, sys_umount2, sys_unlinkat, sys_write, }, mm::{sys_brk, sys_mmap, sys_munmap}, sys::sys_uname, @@ -27,7 +28,7 @@ use self::{ sys_chdir, sys_clone, sys_execve, sys_exit, sys_getcwd, sys_getpid, sys_getppid, sys_sched_yield, sys_wait4, }, - time::{sys_gettimeofday, sys_nanosleep}, + time::{sys_gettimeofday, sys_nanosleep, sys_times}, }; pub async fn syscall(call_type: usize, args: [usize; 7]) -> Result<usize, LinuxError> { @@ -87,6 +88,8 @@ pub async fn syscall(call_type: usize, args: [usize; 7]) -> Result<usize, LinuxE .await } SYS_MUNMAP => sys_munmap(args[0] as _, args[1] as _).await, + SYS_TIMES => sys_times(args[0] as _).await, + SYS_GETDENTS => sys_getdents64(args[0] as _, args[1] as _, args[2] as _).await, _ => { warn!("unsupported syscall"); Err(LinuxError::EPERM) diff --git a/kernel/src/syscall/time.rs b/kernel/src/syscall/time.rs index 25efa19..f3ccea8 100644 --- a/kernel/src/syscall/time.rs +++ b/kernel/src/syscall/time.rs @@ -4,7 +4,9 @@ use core::{ task::{Context, Poll}, }; +use arch::get_time; use devices::RTC_DEVICES; +use executor::{current_task, TMS}; use fs::TimeSepc; use log::debug; @@ -45,6 +47,16 @@ pub async fn sys_nanosleep(req_ptr: usize, rem_ptr: usize) -> Result<usize, Linu Ok(0) } +pub async fn sys_times(tms_ptr: usize) -> Result<usize, LinuxError> { + debug!("sys_times @ tms: {:#x}", tms_ptr); + let tms = c2rust_ref(tms_ptr as *mut TMS); + current_task() + .as_user_task() + .unwrap() + .inner_map(|x| *tms = x.tms); + Ok(get_time()) +} + pub struct WaitNsec(usize); impl Future for WaitNsec { diff --git a/kernel/src/tasks/initproc.rs b/kernel/src/tasks/initproc.rs index 2a8e7a4..7db23a0 100644 --- a/kernel/src/tasks/initproc.rs +++ b/kernel/src/tasks/initproc.rs @@ -63,18 +63,20 @@ async fn run_all() -> bool { "gettimeofday", "mkdir_", "mmap", - "mount /dev/sda /mount", + "mount /dev/sda ./mnt", "munmap", "open", + "times", "openat", "pipe", "read", "sleep", - "umount /dev/sda /mount", + "umount /dev/sda ./mnt", "uname", "unlink", "wait", "waitpid", + "getdents", "write", "yield", ]; @@ -129,8 +131,8 @@ pub async fn command(cmd: &str) -> bool { } pub async fn initproc() { + // command("run_all").await; let mut buffer = Vec::new(); - // let mut buffer = [0u8; 30]; let mut new_line = true; loop { if new_line { diff --git a/kernel/src/tasks/mod.rs b/kernel/src/tasks/mod.rs index 3bf67b9..5f1d8c1 100644 --- a/kernel/src/tasks/mod.rs +++ b/kernel/src/tasks/mod.rs @@ -5,7 +5,7 @@ use core::{ }; use alloc::{boxed::Box, sync::Arc, vec::Vec}; -use arch::{get_time_ms, trap_pre_handle, user_restore, ContextOps, VirtPage}; +use arch::{get_time, get_time_ms, trap_pre_handle, user_restore, ContextOps, VirtPage}; use executor::{current_task, thread, AsyncTask, Executor, KernelTask, MemType, UserTask}; use log::debug; @@ -47,9 +47,13 @@ pub async fn user_entry_inner() { debug!("program exit with code: {}", exit_code); break; } + let ustart = 0; unsafe { user_restore(cx_ref); } + task.inner_map(|mut inner| inner.tms.utime += (get_time() - ustart) as u64); + + let sstart = 0; let trap_type = trap_pre_handle(cx_ref); match trap_type { arch::TrapType::Breakpoint => {} @@ -105,6 +109,7 @@ pub async fn user_entry_inner() { } } } + task.inner_map(|mut inner| inner.tms.stime += (get_time() - sstart) as u64); // yield_now().await; } } diff --git a/modules/devfs/Cargo.toml b/modules/devfs/Cargo.toml index 88e2707..bbbb788 100644 --- a/modules/devfs/Cargo.toml +++ b/modules/devfs/Cargo.toml @@ -10,5 +10,5 @@ vfscore = { path = "../../crates/vfscore" } logging = { path = "../logging" } devices = { path = "../devices" } arch = { path = "../../arch" } -log = "0.4.17" +log = "0.4" sync = { path = "../sync" } diff --git a/modules/devices/Cargo.toml b/modules/devices/Cargo.toml index b5ed56f..c1986c9 100644 --- a/modules/devices/Cargo.toml +++ b/modules/devices/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] arch = { path = "../../arch" } fdt = "0.1.5" -log = "0.4.16" +log = "0.4" sync = { path = "../sync" } virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers", rev = "f1d1cbb"} kheader = { path = "../kheader" } diff --git a/modules/executor/Cargo.toml b/modules/executor/Cargo.toml index 6b11b07..bd8c86d 100644 --- a/modules/executor/Cargo.toml +++ b/modules/executor/Cargo.toml @@ -11,6 +11,6 @@ logging = { path = "../logging" } arch = { path = "../../arch"} sync = { path = "../sync" } frame_allocator = { path = "../frame_allocator" } -log = "0.4.17" +log = "0.4" fs = { path = "../fs" } devfs = { path = "../devfs" } diff --git a/modules/executor/src/lib.rs b/modules/executor/src/lib.rs index b74aa5d..ff8fcaa 100644 --- a/modules/executor/src/lib.rs +++ b/modules/executor/src/lib.rs @@ -12,3 +12,16 @@ pub mod thread; pub use executor::*; pub use ops::*; pub use task::*; + +// tms_utime记录的是进程执行用户代ç 的时间. +// tms_stimeè®°å½•çš„æ˜¯è¿›ç¨‹æ‰§è¡Œå†…æ ¸ä»£ç 的时间. +// tms_cutime记录的是å进程执行用户代ç 的时间. +// tms_ustime记录的是åè¿›ç¨‹æ‰§è¡Œå†…æ ¸ä»£ç 的时间. +#[allow(dead_code)] +#[derive(Default, Clone, Copy)] +pub struct TMS { + pub utime: u64, + pub stime: u64, + pub cutime: u64, + pub cstime: u64, +} diff --git a/modules/executor/src/task.rs b/modules/executor/src/task.rs index af917ac..6c8607c 100644 --- a/modules/executor/src/task.rs +++ b/modules/executor/src/task.rs @@ -11,7 +11,7 @@ use fs::{File, SeekFrom}; use log::debug; use sync::{Mutex, MutexGuard}; -use crate::{task_id_alloc, thread, AsyncTask, TaskId, FUTURE_LIST}; +use crate::{task_id_alloc, thread, AsyncTask, TaskId, FUTURE_LIST, TMS}; #[allow(dead_code)] pub struct KernelTask { @@ -145,6 +145,7 @@ pub struct TaskInner { pub heap: usize, pub entry: usize, pub children: Vec<Arc<UserTask>>, + pub tms: TMS, } #[allow(dead_code)] @@ -191,6 +192,7 @@ impl UserTask { heap: 0, children: Vec::new(), entry: 0, + tms: Default::default(), }; Arc::new(Self { @@ -296,15 +298,17 @@ impl UserTask { } pub fn sbrk(&self, incre: isize) -> usize { - let mut inner = self.inner.lock(); + let inner = self.inner.lock(); let curr_page = inner.heap / PAGE_SIZE; let after_page = (inner.heap as isize + incre) as usize / PAGE_SIZE; + drop(inner); // need alloc frame page if after_page > curr_page { for i in curr_page..after_page { self.frame_alloc(VirtPage::new(i + 1), MemType::CodeSection); } } + let mut inner = self.inner.lock(); inner.heap = (inner.heap as isize + incre) as usize; inner.heap } diff --git a/modules/frame_allocator/Cargo.toml b/modules/frame_allocator/Cargo.toml index ba3f0ee..8e4a2a6 100644 --- a/modules/frame_allocator/Cargo.toml +++ b/modules/frame_allocator/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] bit_field = "0.10.1" -log = "0.4.17" +log = "0.4" arch = { path = "../../arch" } # devices = { path = "../devices" } kheader = { path = "../kheader" } diff --git a/modules/fs/Cargo.toml b/modules/fs/Cargo.toml index 6105de8..b2af1e2 100644 --- a/modules/fs/Cargo.toml +++ b/modules/fs/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -log = "0.4.17" +log = "0.4" vfscore = { path = "../../crates/vfscore" } sync = { path = "../sync" } devices = { path = "../devices" } diff --git a/modules/fs/src/fatfs_shim.rs b/modules/fs/src/fatfs_shim.rs index 678f0e6..369f0cd 100644 --- a/modules/fs/src/fatfs_shim.rs +++ b/modules/fs/src/fatfs_shim.rs @@ -165,16 +165,20 @@ impl INodeInterface for FatFile { fn seek(&self, seek: vfscore::SeekFrom) -> VfsResult<usize> { let mut inner = self.inner.lock(); - inner.inner + inner + .inner .seek(match seek { vfscore::SeekFrom::SET(offset) => SeekFrom::Start(offset as u64), vfscore::SeekFrom::CURRENT(offset) => SeekFrom::Current(offset as i64), vfscore::SeekFrom::END(offset) => SeekFrom::End(offset as i64), }) - .map_or_else(|e| Err(as_vfs_err(e)), |x| { - inner.offset = x as usize; - Ok(x as usize) - }) + .map_or_else( + |e| Err(as_vfs_err(e)), + |x| { + inner.offset = x as usize; + Ok(x as usize) + }, + ) } } diff --git a/modules/hal/Cargo.toml b/modules/hal/Cargo.toml index 60b9c95..156946d 100644 --- a/modules/hal/Cargo.toml +++ b/modules/hal/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" [dependencies] arch = { path = "../../arch" } -log = "0.4.16" +log = "0.4" frame_allocator = { path = "../frame_allocator" } diff --git a/modules/kalloc/Cargo.toml b/modules/kalloc/Cargo.toml index 7540ff0..9d8749e 100644 --- a/modules/kalloc/Cargo.toml +++ b/modules/kalloc/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" [dependencies] buddy_system_allocator = "0.8" -log = "0.4.17" +log = "0.4" diff --git a/modules/logging/Cargo.toml b/modules/logging/Cargo.toml index ce4d8a7..c19e168 100644 --- a/modules/logging/Cargo.toml +++ b/modules/logging/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -log = "0.4.16" +log = "0.4" arch = { path = "../../arch" } diff --git a/modules/logging/src/lib.rs b/modules/logging/src/lib.rs index 30c02d0..9754bd2 100644 --- a/modules/logging/src/lib.rs +++ b/modules/logging/src/lib.rs @@ -17,7 +17,7 @@ impl Log for Logger { } print_in_color( - format_args!("[{}] {}\n", record.level(), record.args()), + format_args!("[{}] {}", record.level(), record.args()), level_to_color_code(record.level()), ); } @@ -62,7 +62,7 @@ macro_rules! println { macro_rules! with_color { ($args: ident, $color_code: ident) => {{ - format_args!("\u{1B}[{}m{}\u{1B}[0m", $color_code as u8, $args) + format_args!("\u{1B}[{}m{}\u{1B}[0m\n", $color_code as u8, $args) }}; } diff --git a/modules/ramfs/Cargo.toml b/modules/ramfs/Cargo.toml index 0955dca..e4b1ed7 100644 --- a/modules/ramfs/Cargo.toml +++ b/modules/ramfs/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" [dependencies] vfscore = { path = "../../crates/vfscore" } -log = "0.4.16" +log = "0.4" sync = { path = "../sync" } -- GitLab