1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
@@ -2,7 +2,7 @@ //! controls all the frames in the operating system. use super::{PhysAddr, PhysPageNum}; -use crate::config::MEMORY_END; +use crate::config::{MEMORY_END, PAGE_SIZE}; use crate::sync::UPSafeCell; use alloc::vec::Vec; use core::fmt::{self, Debug, Formatter}; @@ -117,6 +117,13 @@ pub fn frame_dealloc(ppn: PhysPageNum) { FRAME_ALLOCATOR.exclusive_access().dealloc(ppn); } +/// Check if the remaining physical memory is sufficient +pub fn is_mem_sufficient(_len: usize) -> bool { + let fa = FRAME_ALLOCATOR.exclusive_access(); + let page_cnt = fa.end - fa.current + fa.recycled.len(); + (_len + PAGE_SIZE - 1) / PAGE_SIZE <= page_cnt +} + #[allow(unused)] /// a simple test for frame allocator pub fn frame_allocator_test() {
@@ -63,6 +63,19 @@ impl MemorySet { None, ); } + /// Remove framed area + pub fn remove_framed_area(&mut self, range: VPNRange) { + let mut idx = 0; + for area in self.areas.iter_mut() { + if area.vpn_range.get_start().0 == range.get_start().0 + && area.vpn_range.get_end().0 == range.get_end().0 { + area.unmap(&mut self.page_table); + break; + } + idx += 1; + } + self.areas.remove(idx); + } fn push(&mut self, mut map_area: MapArea, data: Option<&[u8]>) { map_area.map(&mut self.page_table); if let Some(data) = data {
@@ -12,9 +12,9 @@ mod heap_allocator; mod memory_set; mod page_table; -pub use address::{PhysAddr, PhysPageNum, VirtAddr, VirtPageNum}; -use address::{StepByOne, VPNRange}; -pub use frame_allocator::{frame_alloc, FrameTracker}; +pub use address::{PhysAddr, PhysPageNum, VirtAddr, VirtPageNum, VPNRange}; +use address::StepByOne; +pub use frame_allocator::{frame_alloc, FrameTracker, is_mem_sufficient}; pub use memory_set::remap_test; pub use memory_set::{kernel_stack_position, MapPermission, MemorySet, KERNEL_SPACE}; pub use page_table::{translated_byte_buffer, PageTableEntry};
@@ -30,8 +30,11 @@ mod process; use fs::*; use process::*; + +use crate::task::inc_syscall_times; /// handle syscall exception with `syscall_id` and other arguments pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize { + inc_syscall_times(syscall_id); match syscall_id { SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]), SYSCALL_EXIT => sys_exit(args[0] as i32),
@@ -1,9 +1,11 @@ //! Process management syscalls + +use core::mem::size_of; + use crate::{ - config::MAX_SYSCALL_NUM, - task::{ - change_program_brk, exit_current_and_run_next, suspend_current_and_run_next, TaskStatus, - }, + config::MAX_SYSCALL_NUM, mm::translated_byte_buffer, task::{ + change_program_brk, current_user_token, exit_current_and_run_next, get_scheduled_timespan, get_syscall_times, suspend_current_and_run_next, task_mmap, task_munmap, TaskStatus + }, timer::get_time_us }; #[repr(C)] @@ -43,7 +45,21 @@ pub fn sys_yield() -> isize { /// HINT: What if [`TimeVal`] is splitted by two pages ? pub fn sys_get_time(_ts: *mut TimeVal, _tz: usize) -> isize { trace!("kernel: sys_get_time"); - -1 + let _us = get_time_us(); + let time_val = TimeVal { + sec: _us / 1_000_000, + usec: _us % 1_000_000, + }; + let buffers = translated_byte_buffer( + current_user_token(), _ts as *const u8, size_of::<TimeVal>()); + let mut time_val_ptr = &time_val as *const _ as *const u8; + for buffer in buffers { + unsafe { + time_val_ptr.copy_to(buffer.as_mut_ptr(), buffer.len()); + time_val_ptr = time_val_ptr.add(buffer.len()); + } + } + 0 } /// YOUR JOB: Finish sys_task_info to pass testcases @@ -51,19 +67,33 @@ pub fn sys_get_time(_ts: *mut TimeVal, _tz: usize) -> isize { /// HINT: What if [`TaskInfo`] is splitted by two pages ? pub fn sys_task_info(_ti: *mut TaskInfo) -> isize { trace!("kernel: sys_task_info NOT IMPLEMENTED YET!"); - -1 + let task_info = TaskInfo { + status: TaskStatus::Running, + syscall_times: get_syscall_times(), + time: get_scheduled_timespan(), + }; + let buffers = translated_byte_buffer( + current_user_token(), _ti as *const u8, size_of::<TaskInfo>()); + let mut task_info_ptr = &task_info as *const _ as *const u8; + for buffer in buffers { + unsafe { + task_info_ptr.copy_to(buffer.as_mut_ptr(), buffer.len()); + task_info_ptr = task_info_ptr.add(buffer.len()); + } + } + 0 } // YOUR JOB: Implement mmap. pub fn sys_mmap(_start: usize, _len: usize, _port: usize) -> isize { - trace!("kernel: sys_mmap NOT IMPLEMENTED YET!"); - -1 + // trace!("kernel: sys_mmap NOT IMPLEMENTED YET!"); + task_mmap(_start, _len, _port) } // YOUR JOB: Implement munmap. pub fn sys_munmap(_start: usize, _len: usize) -> isize { - trace!("kernel: sys_munmap NOT IMPLEMENTED YET!"); - -1 + // trace!("kernel: sys_munmap NOT IMPLEMENTED YET!"); + task_munmap(_start, _len) } /// change data segment size pub fn sys_sbrk(size: i32) -> isize {
@@ -14,8 +14,11 @@ mod switch; #[allow(clippy::module_inception)] mod task; +use crate::config::MAX_SYSCALL_NUM; use crate::loader::{get_app_data, get_num_app}; +use crate::mm::{is_mem_sufficient, MapPermission, VPNRange, VirtAddr, VirtPageNum}; use crate::sync::UPSafeCell; +use crate::timer::get_time_ms; use crate::trap::TrapContext; use alloc::vec::Vec; use lazy_static::*; @@ -140,6 +143,11 @@ impl TaskManager { let mut inner = self.inner.exclusive_access(); let current = inner.current_task; inner.tasks[next].task_status = TaskStatus::Running; + // if the task is being called for the first time, + if inner.tasks[next].time == 0usize { + // then set it to the current time + inner.tasks[next].time = get_time_ms(); + } inner.current_task = next; let current_task_cx_ptr = &mut inner.tasks[current].task_cx as *mut TaskContext; let next_task_cx_ptr = &inner.tasks[next].task_cx as *const TaskContext; @@ -153,6 +161,80 @@ impl TaskManager { panic!("All applications completed!"); } } + + /// Get task's syscall times + pub fn get_syscall_times(&self) -> [u32; MAX_SYSCALL_NUM] { + let inner = self.inner.exclusive_access(); + let current = inner.current_task; + inner.tasks[current].syscall_times.clone() + } + + /// Get task's time span between current time and first scheduled time + pub fn get_scheduled_timespan(&self) -> usize { + let inner = self.inner.exclusive_access(); + let current = inner.current_task; + get_time_ms() - inner.tasks[current].time + } + + /// Increase syscall times + pub fn inc_syscall_times(&self, syscall_id: usize) -> bool { + if syscall_id >= MAX_SYSCALL_NUM { + return false; + } + let mut inner = self.inner.exclusive_access(); + let current = inner.current_task; + inner.tasks[current].syscall_times[syscall_id] += 1; + true + } + + /// Map some pages to memory_set + fn mmap(&self, start_va: VirtAddr, end_va: VirtAddr, port: usize) -> isize { + let mut inner = self.inner.exclusive_access(); + let cur = inner.current_task; + let vpn_range = VPNRange::new( + VirtPageNum::from(start_va), + end_va.ceil() + ); + + // 3. Check if trying to map mapped page + for vpn in vpn_range { + if let Some(pte) = inner.tasks[cur].memory_set.translate(vpn) { + if pte.is_valid() { + return -1; + } + } + } + + // After checking all errors may occur, + // push a new map_area to current memory_set + let perm = MapPermission::from_bits((port as u8) << 1).unwrap() | MapPermission::U; + inner.tasks[cur].memory_set.insert_framed_area(start_va, end_va, perm); + 0 + } + + /// Unmap some pages in memory set + fn munmap(&self, start_va: VirtAddr, end_va: VirtAddr) -> isize { + let mut inner = self.inner.exclusive_access(); + let cur = inner.current_task; + let vpn_range = VPNRange::new( + VirtPageNum::from(start_va), + end_va.ceil(), + ); + + // 1. Check if trying to unmap unmapped page + for vpn in vpn_range { + if let Some(pte) = inner.tasks[cur].memory_set.translate(vpn) { + if !pte.is_valid() { + return -1 + } + } + } + + // After checking all errors may occur, + // remove some pages from current memory_set + inner.tasks[cur].memory_set.remove_framed_area(vpn_range); + 0 + } } /// Run the first task in task list. @@ -202,3 +284,46 @@ pub fn current_trap_cx() -> &'static mut TrapContext { pub fn change_program_brk(size: i32) -> Option<usize> { TASK_MANAGER.change_current_program_brk(size) } + +/// Get current task's syscall times +pub fn get_syscall_times() -> [u32; MAX_SYSCALL_NUM] { + TASK_MANAGER.get_syscall_times() +} + +/// Get current task's time span between current time and first scheduled time +pub fn get_scheduled_timespan() -> usize { + TASK_MANAGER.get_scheduled_timespan() +} + +/// Increase current task's syscall times +pub fn inc_syscall_times(syscall_id: usize) -> bool { + TASK_MANAGER.inc_syscall_times(syscall_id) +} + +/// Map some pages to current task's memory_set +pub fn task_mmap(start: usize, len: usize, port: usize) -> isize { + let start_va = VirtAddr::from(start); + // 1. illegal start virtual address or port + if !start_va.aligned() || port & !0x7 != 0 || port & 0x7 == 0 { + return -1; + } + + // 2. Check is there sufficient physical memory + if !is_mem_sufficient(len) { + return -1; + } + + let end_va = VirtAddr::from(start + len); + TASK_MANAGER.mmap(start_va, end_va, port) +} + +/// Unmap some pages in current task's memory set +pub fn task_munmap(start: usize, len: usize) -> isize { + let start_va = VirtAddr::from(start); + if !start_va.aligned() { + return -1; + } + + let end_va = VirtAddr::from(start + len); + TASK_MANAGER.munmap(start_va, end_va) +} \ No newline at end of file
@@ -1,6 +1,6 @@ //! Types related to task management use super::TaskContext; -use crate::config::TRAP_CONTEXT_BASE; +use crate::config::{MAX_SYSCALL_NUM, TRAP_CONTEXT_BASE}; use crate::mm::{ kernel_stack_position, MapPermission, MemorySet, PhysPageNum, VirtAddr, KERNEL_SPACE, }; @@ -28,6 +28,12 @@ pub struct TaskControlBlock { /// Program break pub program_brk: usize, + + /// The syscall times + pub syscall_times: [u32; MAX_SYSCALL_NUM], + + /// The first time the task was scheduled + pub time: usize, } impl TaskControlBlock { @@ -63,6 +69,8 @@ impl TaskControlBlock { base_size: user_sp, heap_bottom: user_sp, program_brk: user_sp, + syscall_times: [0u32; MAX_SYSCALL_NUM], + time: 0usize, }; // prepare TrapContext in user space let trap_cx = task_control_block.get_trap_cx();
|