add sbrk and a usertest
This commit is contained in:
parent
aaa42cfeb2
commit
f45e14bfeb
8 changed files with 153 additions and 1 deletions
|
@ -204,6 +204,16 @@ impl MemorySet {
|
|||
),
|
||||
None,
|
||||
);
|
||||
// used in sbrk
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
user_stack_top.into(),
|
||||
user_stack_top.into(),
|
||||
MapType::Framed,
|
||||
MapPermission::R | MapPermission::W | MapPermission::U,
|
||||
),
|
||||
None,
|
||||
);
|
||||
// map TrapContext
|
||||
memory_set.push(
|
||||
MapArea::new(
|
||||
|
@ -230,6 +240,32 @@ impl MemorySet {
|
|||
pub fn translate(&self, vpn: VirtPageNum) -> Option<PageTableEntry> {
|
||||
self.page_table.translate(vpn)
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn shrink_to(&mut self, start: VirtAddr, end: VirtAddr, new_end: VirtAddr) -> bool {
|
||||
if let Some(area) = self
|
||||
.areas
|
||||
.iter_mut()
|
||||
.find(|area| area.vpn_range.get_start() == start.floor()
|
||||
&& area.vpn_range.get_end() == end.ceil()) {
|
||||
area.shrink_to(&mut self.page_table, new_end.ceil());
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn append_to(&mut self, start: VirtAddr, end: VirtAddr, new_end: VirtAddr) -> bool {
|
||||
if let Some(area) = self
|
||||
.areas
|
||||
.iter_mut()
|
||||
.find(|area| area.vpn_range.get_start() == start.floor()
|
||||
&& area.vpn_range.get_end() == end.ceil()) {
|
||||
area.append_to(&mut self.page_table, new_end.ceil());
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// map area structure, controls a contiguous piece of virtual memory
|
||||
|
@ -289,6 +325,20 @@ impl MapArea {
|
|||
self.unmap_one(page_table, vpn);
|
||||
}
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn shrink_to(&mut self, page_table: &mut PageTable, new_end: VirtPageNum) {
|
||||
for vpn in VPNRange::new(new_end, self.vpn_range.get_end()) {
|
||||
self.unmap_one(page_table, vpn)
|
||||
}
|
||||
self.vpn_range = VPNRange::new(self.vpn_range.get_start(), new_end);
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn append_to(&mut self, page_table: &mut PageTable, new_end: VirtPageNum) {
|
||||
for vpn in VPNRange::new(self.vpn_range.get_end(), new_end) {
|
||||
self.map_one(page_table, vpn)
|
||||
}
|
||||
self.vpn_range = VPNRange::new(self.vpn_range.get_start(), new_end);
|
||||
}
|
||||
/// data: start-aligned but maybe with shorter length
|
||||
/// assume that all frames were cleared before
|
||||
pub fn copy_data(&mut self, page_table: &mut PageTable, data: &[u8]) {
|
||||
|
|
|
@ -14,6 +14,7 @@ const SYSCALL_WRITE: usize = 64;
|
|||
const SYSCALL_EXIT: usize = 93;
|
||||
const SYSCALL_YIELD: usize = 124;
|
||||
const SYSCALL_GET_TIME: usize = 169;
|
||||
const SYSCALL_SBRK: usize = 214;
|
||||
|
||||
mod fs;
|
||||
mod process;
|
||||
|
@ -28,6 +29,7 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
|
|||
SYSCALL_EXIT => sys_exit(args[0] as i32),
|
||||
SYSCALL_YIELD => sys_yield(),
|
||||
SYSCALL_GET_TIME => sys_get_time(),
|
||||
SYSCALL_SBRK => sys_sbrk(args[0] as i32),
|
||||
_ => panic!("Unsupported syscall_id: {}", syscall_id),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! Process management syscalls
|
||||
|
||||
use crate::task::{exit_current_and_run_next, suspend_current_and_run_next};
|
||||
use crate::task::{exit_current_and_run_next, suspend_current_and_run_next, change_program_brk};
|
||||
use crate::timer::get_time_ms;
|
||||
|
||||
/// task exits and submit an exit code
|
||||
|
@ -20,3 +20,12 @@ pub fn sys_yield() -> isize {
|
|||
pub fn sys_get_time() -> isize {
|
||||
get_time_ms() as isize
|
||||
}
|
||||
|
||||
/// change data segment size
|
||||
pub fn sys_sbrk(size: i32) -> isize {
|
||||
if let Some(old_brk) = change_program_brk(size) {
|
||||
old_brk as isize
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
}
|
|
@ -126,6 +126,13 @@ impl TaskManager {
|
|||
inner.tasks[inner.current_task].get_trap_cx()
|
||||
}
|
||||
|
||||
/// Change the current 'Running' task's program break
|
||||
pub fn change_current_program_brk(&self, size: i32) -> Option<usize> {
|
||||
let mut inner = self.inner.exclusive_access();
|
||||
let cur = inner.current_task;
|
||||
inner.tasks[cur].change_program_brk(size)
|
||||
}
|
||||
|
||||
/// Switch current `Running` task to the task we have found,
|
||||
/// or there is no `Ready` task and we can exit with all applications completed
|
||||
fn run_next_task(&self) {
|
||||
|
@ -192,3 +199,8 @@ pub fn current_user_token() -> usize {
|
|||
pub fn current_trap_cx() -> &'static mut TrapContext {
|
||||
TASK_MANAGER.get_current_trap_cx()
|
||||
}
|
||||
|
||||
/// Change the current 'Running' task's program break
|
||||
pub fn change_program_brk(size: i32) -> Option<usize> {
|
||||
TASK_MANAGER.change_current_program_brk(size)
|
||||
}
|
||||
|
|
|
@ -11,6 +11,8 @@ pub struct TaskControlBlock {
|
|||
pub memory_set: MemorySet,
|
||||
pub trap_cx_ppn: PhysPageNum,
|
||||
pub base_size: usize,
|
||||
pub heap_bottom: usize,
|
||||
pub program_brk: usize,
|
||||
}
|
||||
|
||||
impl TaskControlBlock {
|
||||
|
@ -41,6 +43,8 @@ impl TaskControlBlock {
|
|||
memory_set,
|
||||
trap_cx_ppn,
|
||||
base_size: user_sp,
|
||||
heap_bottom: user_sp,
|
||||
program_brk: user_sp,
|
||||
};
|
||||
// prepare TrapContext in user space
|
||||
let trap_cx = task_control_block.get_trap_cx();
|
||||
|
@ -53,6 +57,25 @@ impl TaskControlBlock {
|
|||
);
|
||||
task_control_block
|
||||
}
|
||||
/// change the location of the program break. return None if failed.
|
||||
pub fn change_program_brk(&mut self, size: i32) -> Option<usize> {
|
||||
let old_break = self.program_brk;
|
||||
let new_brk = self.program_brk as isize + size as isize;
|
||||
if new_brk < self.heap_bottom as isize {
|
||||
return None
|
||||
}
|
||||
let result = if size < 0 {
|
||||
self.memory_set.shrink_to(VirtAddr(self.heap_bottom), VirtAddr(self.program_brk), VirtAddr(new_brk as usize))
|
||||
} else {
|
||||
self.memory_set.append_to(VirtAddr(self.heap_bottom), VirtAddr(self.program_brk), VirtAddr(new_brk as usize))
|
||||
};
|
||||
if result {
|
||||
self.program_brk = new_brk as usize;
|
||||
Some(old_break)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue