From c6c9a26bf9a96064035cba180a721cfc14695756 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 15 Jun 2022 12:35:34 +0800 Subject: [PATCH] kernel: syscall: return heap size for DecreaseHeap Return the heap size and offset when decreasing the heap. Additionally, return the heap size when increasing the heap by 0 bytes. Signed-off-by: Sean Cross --- kernel/src/syscall.rs | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 9afa22620..22292d4fc 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -708,6 +708,16 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR if delta & 0xfff != 0 { return Err(xous_kernel::Error::BadAlignment); } + // Special case for a delta of 0 -- just return the current heap size + if delta == 0 { + let (start, length) = ArchProcess::with_inner_mut(|process_inner| { + (process_inner.mem_heap_base, process_inner.mem_heap_size) + }); + return Ok(xous_kernel::Result::MemoryRange(unsafe { + MemoryRange::new(start, length).unwrap() + })); + } + let start = { ArchProcess::with_inner_mut(|process_inner| { if process_inner.mem_heap_size + delta > process_inner.mem_heap_max { @@ -719,6 +729,8 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR Ok(start as *mut u8) })? }; + + // Mark the new pages as "reserved" MemoryManager::with_mut(|mm| { Ok(xous_kernel::Result::MemoryRange( mm.reserve_range(start, delta, flags)?, @@ -729,22 +741,33 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR if delta & 0xfff != 0 { return Err(xous_kernel::Error::BadAlignment); } - let start = ArchProcess::with_inner_mut(|process_inner| { - if process_inner.mem_heap_size + delta > process_inner.mem_heap_max { + let (start, length, end) = ArchProcess::with_inner_mut(|process_inner| { + // Don't allow decreasing the heap beyond the current allocation + if delta > process_inner.mem_heap_size { return Err(xous_kernel::Error::OutOfMemory); } - let start = process_inner.mem_heap_base + process_inner.mem_heap_size; + let end = process_inner.mem_heap_base + process_inner.mem_heap_size; process_inner.mem_heap_size -= delta; - Ok(start) + Ok(( + process_inner.mem_heap_base, + process_inner.mem_heap_size, + end, + )) })?; + + // Unmap the pages from the heap MemoryManager::with_mut(|mm| { - for page in ((start - delta)..start).step_by(crate::arch::mem::PAGE_SIZE) { + for page in ((end - delta)..end).step_by(crate::arch::mem::PAGE_SIZE) { mm.unmap_page(page as *mut usize) .expect("unable to unmap page"); } }); - Ok(xous_kernel::Result::Ok) + + // Return the new size of the heap + Ok(xous_kernel::Result::MemoryRange(unsafe { + MemoryRange::new(start, length).unwrap() + })) } SysCall::SwitchTo(new_pid, new_context) => SystemServices::with_mut(|ss| { unsafe {