Skip to content

Commit

Permalink
Merge branch 'patch-add-file-mapping' into patch-fix-mmap
Browse files Browse the repository at this point in the history
  • Loading branch information
MemoryShore committed May 28, 2024
2 parents 33e9f0b + d1d0aca commit 8eb687c
Show file tree
Hide file tree
Showing 20 changed files with 396 additions and 117 deletions.
6 changes: 3 additions & 3 deletions kernel/src/arch/riscv64/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
page_frame::{FrameAllocator, PageFrameCount, PageFrameUsage, PhysPageFrame},
},
kernel_mapper::KernelMapper,
page::{PageEntry, PageFlags, PAGE_1G_SHIFT},
page::{EntryFlags, PageEntry, PAGE_1G_SHIFT},
ucontext::UserMapper,
MemoryManagementArch, PageTableKind, PhysAddr, VirtAddr,
},
Expand Down Expand Up @@ -270,8 +270,8 @@ impl VirtAddr {
}

/// 获取内核地址默认的页面标志
pub unsafe fn kernel_page_flags<A: MemoryManagementArch>(_virt: VirtAddr) -> PageFlags<A> {
PageFlags::from_data(RiscV64MMArch::ENTRY_FLAG_DEFAULT_PAGE)
pub unsafe fn kernel_page_flags<A: MemoryManagementArch>(_virt: VirtAddr) -> EntryFlags<A> {
EntryFlags::from_data(RiscV64MMArch::ENTRY_FLAG_DEFAULT_PAGE)
.set_user(false)
.set_execute(true)
}
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/arch/x86_64/kvm/vmx/ept.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::arch::mm::LockedFrameAllocator;
use crate::arch::mm::PageMapper;
use crate::arch::MMArch;
use crate::mm::page::PageFlags;
use crate::mm::page::EntryFlags;
use crate::mm::{PageTableKind, PhysAddr, VirtAddr};
use crate::smp::core::smp_get_processor_id;
use crate::smp::cpu::AtomicProcessorId;
Expand Down Expand Up @@ -92,7 +92,7 @@ impl EptMapper {
&mut self,
gpa: u64,
hpa: u64,
flags: PageFlags<MMArch>,
flags: EntryFlags<MMArch>,
) -> Result<(), SystemError> {
if self.readonly {
return Err(SystemError::EAGAIN_OR_EWOULDBLOCK);
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/arch/x86_64/kvm/vmx/mmu.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
arch::kvm::vmx::ept::EptMapper,
libs::mutex::Mutex,
mm::{page::PageFlags, syscall::ProtFlags},
mm::{page::EntryFlags, syscall::ProtFlags},
virt::kvm::host_mem::{__gfn_to_pfn, kvm_vcpu_gfn_to_memslot, PAGE_MASK, PAGE_SHIFT},
};
use bitfield_struct::bitfield;
Expand Down Expand Up @@ -218,7 +218,7 @@ pub fn __direct_map(
}
// 把gpa映射到hpa
let mut ept_mapper = EptMapper::lock();
let page_flags = PageFlags::from_prot_flags(ProtFlags::from_bits_truncate(0x7_u64), false);
let page_flags = EntryFlags::from_prot_flags(ProtFlags::from_bits_truncate(0x7_u64), false);
unsafe {
assert!(ept_mapper.walk(gpa, pfn << PAGE_SHIFT, page_flags).is_ok());
}
Expand Down
10 changes: 5 additions & 5 deletions kernel/src/arch/x86_64/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
};

use crate::mm::kernel_mapper::KernelMapper;
use crate::mm::page::{PageEntry, PageFlags, PAGE_1G_SHIFT};
use crate::mm::page::{EntryFlags, PageEntry, PAGE_1G_SHIFT};
use crate::mm::{MemoryManagementArch, PageTableKind, PhysAddr, VirtAddr};

use system_error::SystemError;
Expand Down Expand Up @@ -650,17 +650,17 @@ impl FrameAllocator for LockedFrameAllocator {
}

/// 获取内核地址默认的页面标志
pub unsafe fn kernel_page_flags<A: MemoryManagementArch>(virt: VirtAddr) -> PageFlags<A> {
pub unsafe fn kernel_page_flags<A: MemoryManagementArch>(virt: VirtAddr) -> EntryFlags<A> {
let info: X86_64MMBootstrapInfo = BOOTSTRAP_MM_INFO.unwrap();

if virt.data() >= info.kernel_code_start && virt.data() < info.kernel_code_end {
// Remap kernel code execute
return PageFlags::new().set_execute(true).set_write(true);
return EntryFlags::new().set_execute(true).set_write(true);
} else if virt.data() >= info.kernel_data_end && virt.data() < info.kernel_rodata_end {
// Remap kernel rodata read only
return PageFlags::new().set_execute(true);
return EntryFlags::new().set_execute(true);
} else {
return PageFlags::new().set_write(true).set_execute(true);
return EntryFlags::new().set_write(true).set_execute(true);
}
}

Expand Down
4 changes: 2 additions & 2 deletions kernel/src/driver/net/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::arch::mm::kernel_page_flags;
use crate::arch::MMArch;

use crate::mm::kernel_mapper::KernelMapper;
use crate::mm::page::{page_manager_lock_irqsave, PageFlags};
use crate::mm::page::{page_manager_lock_irqsave, EntryFlags};
use crate::mm::{
allocator::page_frame::{
allocate_page_frames, deallocate_page_frames, PageFrameCount, PhysPageFrame,
Expand All @@ -25,7 +25,7 @@ pub fn dma_alloc(pages: usize) -> (usize, NonNull<u8>) {
// 清空这块区域,防止出现脏数据
core::ptr::write_bytes(virt.data() as *mut u8, 0, count.data() * MMArch::PAGE_SIZE);

let dma_flags: PageFlags<MMArch> = PageFlags::mmio_flags();
let dma_flags: EntryFlags<MMArch> = EntryFlags::mmio_flags();

let mut kernel_mapper = KernelMapper::lock();
let kernel_mapper = kernel_mapper.as_mut().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/driver/video/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
spinlock::SpinLock,
},
mm::{
allocator::page_frame::PageFrameCount, kernel_mapper::KernelMapper, page::PageFlags,
allocator::page_frame::PageFrameCount, kernel_mapper::KernelMapper, page::EntryFlags,
MemoryManagementArch,
},
time::timer::{Timer, TimerFunction},
Expand Down Expand Up @@ -95,7 +95,7 @@ impl VideoRefreshManager {
let count = PageFrameCount::new(
page_align_up(frame_buffer_info_guard.buf_size()) / MMArch::PAGE_SIZE,
);
let page_flags: PageFlags<MMArch> = PageFlags::new().set_execute(true).set_write(true);
let page_flags: EntryFlags<MMArch> = EntryFlags::new().set_execute(true).set_write(true);

let mut kernel_mapper = KernelMapper::lock();
let mut kernel_mapper = kernel_mapper.as_mut();
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/driver/virtio/virtio_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::arch::mm::kernel_page_flags;
use crate::arch::MMArch;

use crate::mm::kernel_mapper::KernelMapper;
use crate::mm::page::{page_manager_lock_irqsave, PageFlags};
use crate::mm::page::{page_manager_lock_irqsave, EntryFlags};
use crate::mm::{
allocator::page_frame::{
allocate_page_frames, deallocate_page_frames, PageFrameCount, PhysPageFrame,
Expand Down Expand Up @@ -32,7 +32,7 @@ unsafe impl Hal for HalImpl {
// 清空这块区域,防止出现脏数据
core::ptr::write_bytes(virt.data() as *mut u8, 0, count.data() * MMArch::PAGE_SIZE);

let dma_flags: PageFlags<MMArch> = PageFlags::mmio_flags();
let dma_flags: EntryFlags<MMArch> = EntryFlags::mmio_flags();

let mut kernel_mapper = KernelMapper::lock();
let kernel_mapper = kernel_mapper.as_mut().unwrap();
Expand Down
41 changes: 41 additions & 0 deletions kernel/src/filesystem/vfs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use alloc::{
sync::{Arc, Weak},
vec::Vec,
};
use hashbrown::HashMap;
use log::error;
use system_error::SystemError;

Expand All @@ -16,6 +17,7 @@ use crate::{
filesystem::procfs::ProcfsFilePrivateData,
ipc::pipe::{LockedPipeInode, PipeFsPrivateData},
libs::{rwlock::RwLock, spinlock::SpinLock},
mm::page::Page,
net::{
event_poll::{EPollItem, EPollPrivateData, EventPoll},
socket::SocketInode,
Expand Down Expand Up @@ -118,6 +120,45 @@ impl FileMode {
return self.bits() & FileMode::O_ACCMODE.bits();
}
}

#[allow(dead_code)]
pub struct PageCache {
inode_ref: Weak<dyn IndexNode>,
map: HashMap<usize, Arc<Page>>,
}

impl PageCache {
pub fn new(inode_ref: Weak<dyn IndexNode>) -> PageCache {
Self {
inode_ref,
map: HashMap::new(),
}
}

pub fn add_page(&mut self, offset: usize, page: Arc<Page>) {
self.map.insert(offset, page);
}

pub fn get_page(&self, offset: usize) -> Option<Arc<Page>> {
self.map.get(&offset).cloned()
}

// pub fn get_pages(&self, start_pgoff: usize, end_pgoff: usize) -> Vec<Arc<Page>> {
// let mut vec = Vec::new();
// for pgoff in start_pgoff..=end_pgoff {
// if let Some(page) = self.map.get(&pgoff) {
// vec.push(page.clone());
// }
// }
// vec
// }
}

pub trait PageCacheOperations: IndexNode {
fn write_page(&self, page: Page);
fn read_ahead(&self);
}

/// @brief 抽象文件结构体
#[derive(Debug)]
pub struct File {
Expand Down
11 changes: 10 additions & 1 deletion kernel/src/filesystem/vfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ use crate::{
time::PosixTimeSpec,
};

use self::{core::generate_inode_id, file::FileMode, syscall::ModeType, utils::DName};
use self::{
core::generate_inode_id,
file::{FileMode, PageCache},
syscall::ModeType,
utils::DName,
};
pub use self::{core::ROOT_INODE, file::FilePrivateData, mount::MountFS};

/// vfs容许的最大的路径名称长度
Expand Down Expand Up @@ -548,6 +553,10 @@ pub trait IndexNode: Any + Sync + Send + Debug + CastFromSync {
fn parent(&self) -> Result<Arc<dyn IndexNode>, SystemError> {
return self.find("..");
}

fn page_cache(&self) -> Option<PageCache> {
None
}
}

impl DowncastArc for dyn IndexNode {
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/ipc/shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl ShmManager {
let mut page_manager_guard = page_manager_lock_irqsave();
let mut cur_phys = PhysPageFrame::new(phys_page.0);
for _ in 0..page_count.data() {
let mut page = Page::new(true);
let mut page = Page::new(true, cur_phys);
page.set_shm_id(shm_id);
let paddr = cur_phys.phys_address();
page_manager_guard.insert(paddr, page);
Expand Down
8 changes: 4 additions & 4 deletions kernel/src/ipc/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
libs::spinlock::SpinLock,
mm::{
allocator::page_frame::{PageFrameCount, PhysPageFrame, VirtPageFrame},
page::{page_manager_lock_irqsave, PageFlags, PageFlushAll},
page::{page_manager_lock_irqsave, EntryFlags, PageFlushAll},
syscall::ProtFlags,
ucontext::{AddressSpace, VMA},
VirtAddr, VmFlags,
Expand Down Expand Up @@ -324,8 +324,8 @@ impl Syscall {
.ok_or(SystemError::EINVAL)?;
let vm_flags = VmFlags::from(shmflg);
let destination = VirtPageFrame::new(region.start());
let page_flags: PageFlags<MMArch> =
PageFlags::from_prot_flags(ProtFlags::from(vm_flags), true);
let page_flags: EntryFlags<MMArch> =
EntryFlags::from_prot_flags(ProtFlags::from(vm_flags), true);
let flusher: PageFlushAll<MMArch> = PageFlushAll::new();

// 将共享内存映射到对应虚拟区域
Expand Down Expand Up @@ -358,7 +358,7 @@ impl Syscall {
// 验证用户虚拟内存区域是否有效
let _ = UserBufferReader::new(vaddr.data() as *const u8, size, true)?;

// 必须在取消映射前获取到PageFlags
// 必须在取消映射前获取到EntryFlags
let page_flags = address_write_guard
.user_mapper
.utable
Expand Down
4 changes: 2 additions & 2 deletions kernel/src/mm/c_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{

use super::{
allocator::page_frame::PageFrameCount, kernel_mapper::KernelMapper, mmio_buddy::mmio_pool,
no_init::pseudo_map_phys, page::PageFlags, MemoryManagementArch, PhysAddr, VirtAddr,
no_init::pseudo_map_phys, page::EntryFlags, MemoryManagementArch, PhysAddr, VirtAddr,
};

lazy_static! {
Expand All @@ -40,7 +40,7 @@ pub unsafe extern "C" fn rs_map_phys(vaddr: usize, paddr: usize, size: usize, fl
let count = PageFrameCount::new(page_align_up(size) / MMArch::PAGE_SIZE);
// debug!("rs_map_phys: vaddr: {vaddr:?}, paddr: {paddr:?}, count: {count:?}, flags: {flags:?}");

let mut page_flags: PageFlags<MMArch> = PageFlags::new().set_execute(true).set_write(true);
let mut page_flags: EntryFlags<MMArch> = EntryFlags::new().set_execute(true).set_write(true);
if flags & PAGE_U_S as usize != 0 {
page_flags = page_flags.set_user(true);
}
Expand Down
Loading

0 comments on commit 8eb687c

Please sign in to comment.