2019-03-27 14:35:08 +04:00
|
|
|
use rcore_memory::memory_set::handler::{ByFrame, Delay};
|
2019-02-22 19:48:05 +04:00
|
|
|
use rcore_memory::memory_set::MemoryAttr;
|
2019-03-07 20:03:06 +04:00
|
|
|
use rcore_memory::paging::PageTable;
|
|
|
|
use rcore_memory::Page;
|
2019-03-07 18:06:26 +04:00
|
|
|
use rcore_memory::PAGE_SIZE;
|
2019-02-22 19:48:05 +04:00
|
|
|
|
|
|
|
use crate::memory::GlobalFrameAlloc;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2019-03-10 04:39:22 +04:00
|
|
|
pub fn sys_mmap(
|
|
|
|
mut addr: usize,
|
|
|
|
len: usize,
|
|
|
|
prot: usize,
|
|
|
|
flags: usize,
|
2019-03-22 19:58:19 +04:00
|
|
|
fd: usize,
|
2019-03-10 04:39:22 +04:00
|
|
|
offset: usize,
|
|
|
|
) -> SysResult {
|
2019-02-27 20:15:33 +04:00
|
|
|
let prot = MmapProt::from_bits_truncate(prot);
|
|
|
|
let flags = MmapFlags::from_bits_truncate(flags);
|
2019-03-10 04:39:22 +04:00
|
|
|
info!(
|
|
|
|
"mmap: addr={:#x}, size={:#x}, prot={:?}, flags={:?}, fd={}, offset={:#x}",
|
|
|
|
addr, len, prot, flags, fd, offset
|
|
|
|
);
|
2019-02-27 20:15:33 +04:00
|
|
|
|
2019-02-25 20:46:09 +04:00
|
|
|
let mut proc = process();
|
2019-03-07 18:06:26 +04:00
|
|
|
if addr == 0 {
|
|
|
|
// although NULL can be a valid address
|
|
|
|
// but in C, NULL is regarded as allocation failure
|
|
|
|
// so just skip it
|
|
|
|
addr = PAGE_SIZE;
|
|
|
|
}
|
2019-03-08 10:22:00 +04:00
|
|
|
|
|
|
|
if flags.contains(MmapFlags::FIXED) {
|
|
|
|
// we have to map it to addr, so remove the old mapping first
|
2019-03-22 19:45:57 +04:00
|
|
|
proc.vm.pop_with_split(addr, addr + len);
|
2019-03-08 10:22:00 +04:00
|
|
|
} else {
|
2019-03-22 19:45:57 +04:00
|
|
|
addr = proc.vm.find_free_area(addr, len);
|
2019-03-08 10:22:00 +04:00
|
|
|
}
|
2019-02-25 20:46:09 +04:00
|
|
|
|
2019-02-22 19:48:05 +04:00
|
|
|
if flags.contains(MmapFlags::ANONYMOUS) {
|
|
|
|
if flags.contains(MmapFlags::SHARED) {
|
2019-02-28 06:59:52 +04:00
|
|
|
return Err(SysError::EINVAL);
|
2019-02-22 19:48:05 +04:00
|
|
|
}
|
2019-03-22 19:45:57 +04:00
|
|
|
proc.vm.push(
|
2019-03-10 04:39:22 +04:00
|
|
|
addr,
|
|
|
|
addr + len,
|
|
|
|
prot.to_attr(),
|
|
|
|
Delay::new(GlobalFrameAlloc),
|
2019-03-24 12:25:32 +04:00
|
|
|
"mmap_anon",
|
2019-03-10 04:39:22 +04:00
|
|
|
);
|
2019-03-08 15:04:39 +04:00
|
|
|
return Ok(addr);
|
2019-03-22 19:58:19 +04:00
|
|
|
} else {
|
|
|
|
// only check
|
|
|
|
let _ = proc.get_file(fd)?;
|
|
|
|
|
|
|
|
// TODO: delay mmap file
|
|
|
|
proc.vm.push(
|
|
|
|
addr,
|
|
|
|
addr + len,
|
|
|
|
prot.to_attr(),
|
|
|
|
ByFrame::new(GlobalFrameAlloc),
|
2019-03-24 12:25:32 +04:00
|
|
|
"mmap_file",
|
2019-03-22 19:58:19 +04:00
|
|
|
);
|
|
|
|
let data = unsafe { slice::from_raw_parts_mut(addr as *mut u8, len) };
|
|
|
|
let file = proc.get_file(fd)?;
|
|
|
|
let read_len = file.read_at(offset, data)?;
|
|
|
|
if read_len != data.len() {
|
2019-03-23 19:50:30 +04:00
|
|
|
// use count() to consume the iterator
|
|
|
|
data[read_len..].iter_mut().map(|x| *x = 0).count();
|
2019-03-22 19:58:19 +04:00
|
|
|
}
|
|
|
|
return Ok(addr);
|
2019-02-22 19:48:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 20:03:06 +04:00
|
|
|
pub fn sys_mprotect(addr: usize, len: usize, prot: usize) -> SysResult {
|
|
|
|
let prot = MmapProt::from_bits_truncate(prot);
|
2019-03-10 04:39:22 +04:00
|
|
|
info!(
|
|
|
|
"mprotect: addr={:#x}, size={:#x}, prot={:?}",
|
|
|
|
addr, len, prot
|
|
|
|
);
|
2019-03-07 20:03:06 +04:00
|
|
|
|
|
|
|
let mut proc = process();
|
2019-03-08 06:10:13 +04:00
|
|
|
let attr = prot.to_attr();
|
2019-03-07 20:03:06 +04:00
|
|
|
|
2019-03-08 18:37:05 +04:00
|
|
|
// FIXME: properly set the attribute of the area
|
|
|
|
// now some mut ptr check is fault
|
2019-03-27 14:35:08 +04:00
|
|
|
let memory_area = proc
|
|
|
|
.vm
|
|
|
|
.iter()
|
|
|
|
.find(|area| area.is_overlap_with(addr, addr + len));
|
2019-03-08 18:37:05 +04:00
|
|
|
if memory_area.is_none() {
|
|
|
|
return Err(SysError::ENOMEM);
|
2019-03-07 20:03:06 +04:00
|
|
|
}
|
2019-03-22 19:45:57 +04:00
|
|
|
proc.vm.edit(|pt| {
|
2019-03-08 18:37:05 +04:00
|
|
|
for page in Page::range_of(addr, addr + len) {
|
2019-03-10 04:39:22 +04:00
|
|
|
let entry = pt
|
|
|
|
.get_entry(page.start_address())
|
|
|
|
.expect("failed to get entry");
|
2019-03-08 18:37:05 +04:00
|
|
|
attr.apply(entry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(0)
|
2019-03-07 20:03:06 +04:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:48:05 +04:00
|
|
|
pub fn sys_munmap(addr: usize, len: usize) -> SysResult {
|
2019-03-02 16:15:55 +04:00
|
|
|
info!("munmap addr={:#x}, size={:#x}", addr, len);
|
|
|
|
let mut proc = process();
|
2019-03-22 19:45:57 +04:00
|
|
|
proc.vm.pop_with_split(addr, addr + len);
|
2019-03-02 16:15:55 +04:00
|
|
|
Ok(0)
|
2019-02-22 19:48:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
pub struct MmapProt: usize {
|
|
|
|
/// Data cannot be accessed
|
|
|
|
const NONE = 0;
|
|
|
|
/// Data can be read
|
|
|
|
const READ = 1 << 0;
|
|
|
|
/// Data can be written
|
|
|
|
const WRITE = 1 << 1;
|
|
|
|
/// Data can be executed
|
|
|
|
const EXEC = 1 << 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
pub struct MmapFlags: usize {
|
|
|
|
/// Changes are shared.
|
|
|
|
const SHARED = 1 << 0;
|
|
|
|
/// Changes are private.
|
|
|
|
const PRIVATE = 1 << 1;
|
2019-03-08 10:22:00 +04:00
|
|
|
/// Place the mapping at the exact address
|
|
|
|
const FIXED = 1 << 4;
|
2019-02-22 19:48:05 +04:00
|
|
|
/// The mapping is not backed by any file. (non-POSIX)
|
|
|
|
const ANONYMOUS = 1 << 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 06:10:13 +04:00
|
|
|
impl MmapProt {
|
|
|
|
fn to_attr(self) -> MemoryAttr {
|
|
|
|
let mut attr = MemoryAttr::default().user();
|
2019-03-10 04:39:22 +04:00
|
|
|
if self.contains(MmapProt::EXEC) {
|
|
|
|
attr = attr.execute();
|
|
|
|
}
|
2019-03-09 11:55:00 +04:00
|
|
|
// FIXME: see sys_mprotect
|
2019-03-10 04:39:22 +04:00
|
|
|
// if !self.contains(MmapProt::WRITE) { attr = attr.readonly(); }
|
2019-03-08 06:10:13 +04:00
|
|
|
attr
|
|
|
|
}
|
|
|
|
}
|