lab04: sys_get_time
Some checks failed
Build Rust Doc And Run tests / build-doc (push) Has been cancelled
Build Rust Doc And Run tests / run-tests (push) Has been cancelled

This commit is contained in:
Tateisi 2025-08-03 17:36:14 +08:00
parent 58fa67414e
commit f830ce95d6
2 changed files with 22 additions and 10 deletions

View file

@ -1,8 +1,8 @@
//! Process management syscalls //! Process management syscalls
use crate::config::PAGE_SIZE; use crate::config::PAGE_SIZE;
use crate::mm::{MapPermission, VPNRange, VirtAddr}; use crate::mm::{translated_byte_buffer, MapPermission, VPNRange, VirtAddr};
use crate::task::{change_program_brk, create_new_map_area, exit_current_and_run_next, get_current_task_page_table_entry, suspend_current_and_run_next, unmap_virtual_page}; use crate::task::{change_program_brk, create_new_map_area, current_user_token, exit_current_and_run_next, get_current_task_page_table_entry, suspend_current_and_run_next, unmap_virtual_page};
use crate::timer::get_time_us; use crate::timer::get_time_us;
/// task exits and submit an exit code /// task exits and submit an exit code
@ -28,14 +28,27 @@ pub struct TimeVal {
/// get current time /// get current time
pub fn sys_get_time(ts: *mut TimeVal, _tz: usize) -> isize { pub fn sys_get_time(ts: *mut TimeVal, _tz: usize) -> isize {
let us = get_time_us(); let us = get_time_us();
unsafe { let dst_vec = translated_byte_buffer(
*ts = TimeVal { current_user_token(),
ts as *const u8, core::mem::size_of::<TimeVal>()
);
let ref time_val = TimeVal {
sec: us / 1_000_000, sec: us / 1_000_000,
usec: us % 1_000_000, usec: us % 1_000_000,
}; };
let src_ptr = time_val as *const TimeVal;
for (idx, dst) in dst_vec.into_iter().enumerate() {
let unit_len = dst.len();
unsafe {
dst.copy_from_slice(
core::slice::from_raw_parts(
src_ptr.wrapping_byte_add(idx * unit_len) as *const u8,
unit_len
)
);
}
} }
0 0
-1
} }
/// change data segment size /// change data segment size

View file

@ -6,7 +6,6 @@ const SYSCALL_WRITE: usize = 64;
const SYSCALL_EXIT: usize = 93; const SYSCALL_EXIT: usize = 93;
const SYSCALL_YIELD: usize = 124; const SYSCALL_YIELD: usize = 124;
const SYSCALL_GET_TIME: usize = 169; const SYSCALL_GET_TIME: usize = 169;
const SYSCALL_GETTIMEOFDAY: usize = 169;
const SYSCALL_SBRK: usize = 214; const SYSCALL_SBRK: usize = 214;
const SYSCALL_MMAP: usize = 222; const SYSCALL_MMAP: usize = 222;
const SYSCALL_MUNMAP: usize = 215; const SYSCALL_MUNMAP: usize = 215;
@ -38,7 +37,7 @@ pub fn sys_yield() -> isize {
} }
pub fn sys_get_time(time: &TimeVal, tz: usize) -> isize { pub fn sys_get_time(time: &TimeVal, tz: usize) -> isize {
syscall(SYSCALL_GETTIMEOFDAY, [time as *const _ as usize, tz, 0]) syscall(SYSCALL_GET_TIME, [time as *const _ as usize, tz, 0])
} }
pub fn sys_sbrk(size: i32) -> isize { pub fn sys_sbrk(size: i32) -> isize {