Skip to content

Commit

Permalink
fix bugs in mmap and syscall handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ken4647 committed Apr 12, 2024
1 parent 1416cfd commit 095c240
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 97 deletions.
8 changes: 4 additions & 4 deletions api/ruxos_posix_api/src/imp/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ use crate::{ctypes, utils::char_ptr_to_str};
use alloc::vec::Vec;

pub struct File {
inner: Mutex<ruxfs::fops::File>,
pub(crate) inner: Mutex<ruxfs::fops::File>,
}

impl File {
fn new(inner: ruxfs::fops::File) -> Self {
pub(crate) fn new(inner: ruxfs::fops::File) -> Self {
Self {
inner: Mutex::new(inner),
}
}

fn add_to_fd_table(self) -> LinuxResult<c_int> {
pub(crate) fn add_to_fd_table(self) -> LinuxResult<c_int> {
super::fd_ops::add_file_like(Arc::new(self))
}

fn from_fd(fd: c_int) -> LinuxResult<Arc<Self>> {
pub(crate) fn from_fd(fd: c_int) -> LinuxResult<Arc<Self>> {
let f = super::fd_ops::get_file_like(fd)?;
f.into_any()
.downcast::<Self>()
Expand Down
40 changes: 23 additions & 17 deletions api/ruxos_posix_api/src/imp/mmap/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ use super::utils::{
};

#[cfg(feature = "fs")]
use super::utils::release_pages_swaped;
#[cfg(feature = "fs")]
use crate::imp::fs::sys_pwrite64;
use {
super::utils::{release_pages_swaped, write_into},
alloc::sync::Arc,
};

/// Creates a new mapping in the virtual address space of the calling process.
///
Expand Down Expand Up @@ -296,17 +297,10 @@ pub fn sys_msync(start: *mut c_void, len: ctypes::size_t, flags: c_int) -> c_int
if !VirtAddr::from(start).is_aligned(PAGE_SIZE_4K) || len == 0 {
return Err(LinuxError::EINVAL);
}
for (&vaddr, &page_info) in MEM_MAP.lock().range(start..end) {
if let Some((fid, offset, size)) = page_info {
let src = vaddr as *mut c_void;
let ret_size = sys_pwrite64(fid, src, size, offset as i64) as usize;
if size != ret_size {
error!(
"sys_msync: try to pwrite(fid=0x{:x?}, size=0x{:x?}, offset=0x{:x?}) but get ret = 0x{:x?}",
fid, size, offset, ret_size
);
return Err(LinuxError::EFAULT);
}
for (&vaddr, page_info) in MEM_MAP.lock().range(start..end) {
if let Some((file, offset, size)) = page_info {
let src = vaddr as *mut u8;
write_into(file, src, *offset as u64, *size);
}
}
}
Expand Down Expand Up @@ -358,13 +352,25 @@ pub fn sys_mremap(
}
// make sure of consistent_vma is continuous and consistent in both flags and prots.
if let Some(ref mut inner_vma) = consistent_vma {
let end_offset = inner_vma.offset + (inner_vma.end_addr - inner_vma.start_addr);
if inner_vma.end_addr == vma.start_addr
&& inner_vma.flags == vma.flags
&& inner_vma.prot == vma.prot
&& inner_vma.fid == vma.fid
&& (end_offset == vma.offset || inner_vma.fid < 0)
{
#[cfg(feature = "fs")]
if inner_vma.file.is_some() {
if vma.file.is_none() {
return Err(LinuxError::EFAULT);
}
let end_offset =
inner_vma.offset + (inner_vma.end_addr - inner_vma.start_addr);
let vma_file = vma.file.as_ref().unwrap();
let inner_file = inner_vma.file.as_ref().unwrap();
if !Arc::ptr_eq(vma_file, inner_file) || end_offset != vma.offset {
return Err(LinuxError::EFAULT);
}
} else if vma.file.is_some() {
return Err(LinuxError::EFAULT);
}
inner_vma.end_addr = vma.end_addr;
} else {
return Err(LinuxError::EFAULT);
Expand Down
37 changes: 19 additions & 18 deletions api/ruxos_posix_api/src/imp/mmap/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
* See the Mulan PSL v2 for more details.
*/

use crate::ctypes;

#[cfg(not(feature = "fs"))]
use ruxhal::paging::alloc_page_preload;
#[cfg(feature = "fs")]
use {
crate::imp::fs::sys_pread64,
crate::imp::mmap::utils::{preload_page_with_swap, BITMAP_FREE, SWAPED_MAP, SWAP_FID},
use crate::{
ctypes,
imp::mmap::utils::{preload_page_with_swap, read_from, BITMAP_FREE, SWAPED_MAP, SWAP_FILE},
};
#[cfg(not(feature = "fs"))]
use ruxhal::paging::alloc_page_preload;

use crate::imp::mmap::utils::{get_mflags_from_usize, MEM_MAP, VMA_MAP};
use core::{cmp::min, ffi::c_void, ops::Bound};
use core::{cmp::min, ops::Bound};
use memory_addr::PAGE_SIZE_4K;
use page_table::MappingFlags;
use ruxhal::{
Expand Down Expand Up @@ -91,15 +89,15 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl {
preload_page_with_swap(&mut memory_map, &mut swaped_map, &mut off_pool);

// Fill target data to assigned physical addresses, from file or zero according to mapping type
let dst = fake_vaddr.as_mut_ptr() as *mut c_void;
let dst = fake_vaddr.as_mut_ptr();
#[cfg(feature = "fs")]
{
if let Some(off) = swaped_map.remove(&vaddr) {
off_pool.push(off);
sys_pread64(*SWAP_FID, dst, size, off as i64);
} else if vma.fid > 0 && !map_flag.is_empty() {
let off = (vma.offset + (vaddr - vma.start_addr)) as i64;
sys_pread64(vma.fid, dst, size, off);
read_from(&SWAP_FILE, dst, off as u64, size);
} else if let Some(file) = &vma.file {
let off = (vma.offset + (vaddr - vma.start_addr)) as u64;
read_from(file, dst, off, size);
} else {
// Set page to 0 for anonymous mapping
//
Expand All @@ -121,16 +119,22 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl {
}

// Insert the record into `MEM_MAP` with write-back information(`None` if no need to write-back).
#[cfg(feature = "fs")]
if (vma.prot & ctypes::PROT_WRITE != 0)
&& (vma.flags & ctypes::MAP_PRIVATE == 0)
&& (vma.fid > 0)
&& (vma.file.is_some())
{
let map_length = min(PAGE_SIZE_4K, vma.end_addr - vaddr);
let offset = vma.offset + (vaddr - vma.start_addr);
memory_map.insert(vaddr, Some((vma.fid, offset, map_length)));
memory_map.insert(
vaddr,
Some((vma.file.as_ref().unwrap().clone(), offset, map_length)),
);
} else {
memory_map.insert(vaddr, None);
}
#[cfg(not(feature = "fs"))]
memory_map.insert(vaddr, None);

// Do actual mmapping for target vaddr
//
Expand All @@ -140,9 +144,6 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl {
Err(_) => false,
}
} else {
for mapped in vma_map.iter() {
warn!("0x{:x?}", mapped);
}
warn!("vaddr=0x{:x?},cause=0x{:x?}", vaddr, cause);
false
}
Expand Down
Loading

0 comments on commit 095c240

Please sign in to comment.