From 3b7885078732d77d72b29564ed8273405ecec1f8 Mon Sep 17 00:00:00 2001 From: OrangeQi-CQ <2082448790@qq.com> Date: Tue, 27 May 2025 22:55:22 +0800 Subject: [PATCH 01/11] Satisfy page cache --- modules/axfs/src/fops.rs | 20 ++++++++- modules/axmm/src/aspace.rs | 71 +++++++++++++++++++++++++++++++- modules/axmm/src/backend/file.rs | 8 ++++ modules/axmm/src/backend/mod.rs | 13 +++++- 4 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 modules/axmm/src/backend/file.rs diff --git a/modules/axfs/src/fops.rs b/modules/axfs/src/fops.rs index 9cd1827a5e..e87ca97199 100644 --- a/modules/axfs/src/fops.rs +++ b/modules/axfs/src/fops.rs @@ -24,6 +24,7 @@ pub type FilePerm = axfs_vfs::VfsNodePerm; pub struct File { node: WithCap, is_append: bool, + is_direct: bool, offset: u64, } @@ -46,6 +47,7 @@ pub struct OpenOptions { create: bool, create_new: bool, directory: bool, + direct: bool, // system-specific _custom_flags: i32, _mode: u32, @@ -64,6 +66,7 @@ impl OpenOptions { create: false, create_new: false, directory: false, + direct: false, // system-specific _custom_flags: 0, _mode: 0o666, @@ -101,6 +104,11 @@ impl OpenOptions { pub fn directory(&mut self, directory: bool) { self.directory = directory; } + /// Sets the option to use page cache + pub fn direct(&mut self, direct: bool) { + self.direct = direct; + } + /// check whether contains directory. pub fn has_directory(&self) -> bool { self.directory @@ -152,7 +160,6 @@ impl File { } fn _open_at(dir: Option<&VfsNodeRef>, path: &str, opts: &OpenOptions) -> AxResult { - debug!("open file: {} {:?}", path, opts); if !opts.is_valid() { return ax_err!(InvalidInput); } @@ -192,6 +199,7 @@ impl File { Ok(Self { node: WithCap::new(node, access_cap), is_append: opts.append, + is_direct: opts.direct, offset: 0, }) } @@ -279,6 +287,16 @@ impl File { pub fn get_attr(&self) -> AxResult { self.access_node(Cap::empty())?.get_attr() } + + /// Gets the file offset + pub fn get_offset(&self) -> u64 { + self.offset + } + + /// Check whether direct or not + pub fn is_direct(&self) -> bool { + self.is_direct + } } impl Directory { diff --git a/modules/axmm/src/aspace.rs b/modules/axmm/src/aspace.rs index f9691ec10f..879e7394af 100644 --- a/modules/axmm/src/aspace.rs +++ b/modules/axmm/src/aspace.rs @@ -2,7 +2,7 @@ use core::fmt; use axerrno::{AxError, AxResult, ax_err}; use axhal::mem::phys_to_virt; -use axhal::paging::{MappingFlags, PageTable, PagingError}; +use axhal::paging::{MappingFlags, PageTable, PagingError, PageSize}; use memory_addr::{ MemoryAddr, PAGE_SIZE_4K, PageIter4K, PhysAddr, VirtAddr, VirtAddrRange, is_aligned_4k, }; @@ -161,6 +161,58 @@ impl AddrSpace { Ok(()) } + pub fn map_shm( + &mut self, + start: VirtAddr, + size: usize, + flags: MappingFlags, + populate: bool, + ) -> AxResult { + panic!("Unimplement"); + } + + /// Add a new file mapping + pub fn map_file( + &mut self, + start: VirtAddr, + size: usize, + flags: MappingFlags, + fd: i32, + offset: usize, + shared: bool, + populate: bool, + ) -> AxResult { + self.validate_region(start, size)?; + // warn!("map file flags: {}", flags.contains(MappingFlags::WRITE)); + let area = MemoryArea::new(start, size, flags, Backend::new_file(fd, offset, shared, populate)); + self.areas + .map(area, &mut self.pt, false) + .map_err(mapping_err_to_ax_err)?; + Ok(()) + } + + /// Forcely set the page table + pub fn force_map_page( + &mut self, + vaddr: VirtAddr, + paddr: PhysAddr, + access_flags: MappingFlags + ) -> bool { + match self.areas.find(vaddr) { + Some(area) => { + if area.flags().contains(access_flags) { + self.pt.map(vaddr, paddr, PageSize::Size4K, area.flags()) + .map(|_| true) + .unwrap_or_else(|_| panic!("FORCE MAP PAGE FAILED(PAGE TABLE FAILEDA): {:#x} => {:#x}!", vaddr, paddr)); + } else { + panic!("FORCE MAP PAGE FAILED(ACCESS MOD): {:#x} => {:#x}!", vaddr, paddr); + } + }, + _ => panic!("FORCE MAP PAGE FAILED(NO AREA): {:#x} => {:#x}!", vaddr, paddr), + }; + true + } + /// Populates the area with physical frames, returning false if the area /// contains unmapped area. pub fn populate_area(&mut self, mut start: VirtAddr, size: usize) -> AxResult { @@ -360,10 +412,25 @@ impl AddrSpace { false } + /// Returns (fd, offset, shared, popoulate, virtaddr_start) + pub fn get_file_metadata(&mut self, vaddr: VirtAddr) -> Option<(i32, usize, bool, bool, VirtAddr)> { + if !self.va_range.contains(vaddr) { + return None; + } + if let Some(area) = self.areas.find(vaddr) { + match area.backend() { + Backend::File { fd, offset, shared, populate } + => return Some((*fd, *offset, *shared, *populate, area.start())), + _ => return None, + } + } + None + } + /// Handles a page fault at the given address. /// /// `access_flags` indicates the access type that caused the page fault. - /// + /// /// Returns `true` if the page fault is handled successfully (not a real /// fault). pub fn handle_page_fault(&mut self, vaddr: VirtAddr, access_flags: MappingFlags) -> bool { diff --git a/modules/axmm/src/backend/file.rs b/modules/axmm/src/backend/file.rs new file mode 100644 index 0000000000..bdef60b03d --- /dev/null +++ b/modules/axmm/src/backend/file.rs @@ -0,0 +1,8 @@ +use super::Backend; + +impl Backend { + /// Creates a new allocation mapping backend. + pub const fn new_file(fd: i32, offset: usize, populate: bool, shared: bool) -> Self { + Self::File { fd, offset, shared, populate} + } +} diff --git a/modules/axmm/src/backend/mod.rs b/modules/axmm/src/backend/mod.rs index be58b3e59d..c1a6e35bb7 100644 --- a/modules/axmm/src/backend/mod.rs +++ b/modules/axmm/src/backend/mod.rs @@ -6,6 +6,7 @@ use memory_set::MappingBackend; mod alloc; mod linear; +mod file; /// A unified enum type for different memory mapping backends. /// @@ -36,6 +37,13 @@ pub enum Backend { /// Whether to populate the physical frames when creating the mapping. populate: bool, }, + /// File mapping backend. + File { + fd: i32, + offset: usize, + shared: bool, + populate: bool, + }, } impl MappingBackend for Backend { @@ -46,6 +54,7 @@ impl MappingBackend for Backend { match *self { Self::Linear { pa_va_offset } => Self::map_linear(start, size, flags, pt, pa_va_offset), Self::Alloc { populate } => Self::map_alloc(start, size, flags, pt, populate), + Self::File { .. } => true, } } @@ -53,6 +62,7 @@ impl MappingBackend for Backend { match *self { Self::Linear { pa_va_offset } => Self::unmap_linear(start, size, pt, pa_va_offset), Self::Alloc { populate } => Self::unmap_alloc(start, size, pt, populate), + Self::File { .. } => true, } } @@ -81,7 +91,8 @@ impl Backend { Self::Linear { .. } => false, // Linear mappings should not trigger page faults. Self::Alloc { populate } => { Self::handle_page_fault_alloc(vaddr, orig_flags, page_table, populate) - } + }, + Self::File { .. } => false, } } } From a6661b0e15e247c6e9fb6c2c28e18ae6ae9471f3 Mon Sep 17 00:00:00 2001 From: OrangeQi-CQ <2082448790@qq.com> Date: Thu, 5 Jun 2025 14:55:52 +0800 Subject: [PATCH 02/11] satisfy page cache --- Cargo.lock | 4 +- Cargo.toml | 4 +- modules/axfs/Cargo.toml | 2 +- modules/axhal/src/mem.rs | 4 ++ modules/axmm/src/aspace.rs | 81 ++++++++++++++------------------ modules/axmm/src/backend/file.rs | 8 ---- modules/axmm/src/backend/mod.rs | 11 ----- 7 files changed, 44 insertions(+), 70 deletions(-) delete mode 100644 modules/axmm/src/backend/file.rs diff --git a/Cargo.lock b/Cargo.lock index b17ebade57..cc53104769 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1321,7 +1321,7 @@ checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" [[package]] name = "page_table_entry" version = "0.5.3" -source = "git+https://github.com/Mivik/page_table_multiarch.git?rev=19ededd#19ededdb806ab3b22efb4880661790524fa421a5" +source = "git+https://github.com/OrangeQi-CQ/page_table_multiarch#b292a473e2a88d6af8daeaafc7ecbf0312b33e88" dependencies = [ "aarch64-cpu 10.0.0", "bitflags 2.8.0", @@ -1332,7 +1332,7 @@ dependencies = [ [[package]] name = "page_table_multiarch" version = "0.5.3" -source = "git+https://github.com/Mivik/page_table_multiarch.git?rev=19ededd#19ededdb806ab3b22efb4880661790524fa421a5" +source = "git+https://github.com/OrangeQi-CQ/page_table_multiarch#b292a473e2a88d6af8daeaafc7ecbf0312b33e88" dependencies = [ "log", "memory_addr", diff --git a/Cargo.toml b/Cargo.toml index 7214779f1a..84aaf800eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,5 +69,5 @@ axdma = { path = "modules/axdma" } lto = true [patch.crates-io] -page_table_multiarch = { git = "https://github.com/Mivik/page_table_multiarch.git", rev = "19ededd" } -page_table_entry = { git = "https://github.com/Mivik/page_table_multiarch.git", rev = "19ededd" } +page_table_multiarch = { git = "https://github.com/OrangeQi-CQ/page_table_multiarch" } +page_table_entry = { git = "https://github.com/OrangeQi-CQ/page_table_multiarch" } diff --git a/modules/axfs/Cargo.toml b/modules/axfs/Cargo.toml index 36219c8bcd..3000dcee91 100644 --- a/modules/axfs/Cargo.toml +++ b/modules/axfs/Cargo.toml @@ -19,7 +19,7 @@ fatfs = ["dep:fatfs"] myfs = ["dep:crate_interface"] use-ramdisk = [] -default = ["devfs", "ramfs", "fatfs", "procfs", "sysfs"] +default = ["devfs", "ramfs", "lwext4_rs", "procfs", "sysfs"] [dependencies] log = "=0.4.21" diff --git a/modules/axhal/src/mem.rs b/modules/axhal/src/mem.rs index e4e15747d3..23de880d45 100644 --- a/modules/axhal/src/mem.rs +++ b/modules/axhal/src/mem.rs @@ -54,6 +54,10 @@ pub struct MemRegion { /// `paddr = vaddr - PHYS_VIRT_OFFSET`. #[inline] pub const fn virt_to_phys(vaddr: VirtAddr) -> PhysAddr { + assert!( + vaddr.as_usize() >= PHYS_VIRT_OFFSET, + "Converted address is invalid, check if the virtual address is in kernel space" + ); pa!(vaddr.as_usize() - PHYS_VIRT_OFFSET) } diff --git a/modules/axmm/src/aspace.rs b/modules/axmm/src/aspace.rs index 879e7394af..41cdd73e46 100644 --- a/modules/axmm/src/aspace.rs +++ b/modules/axmm/src/aspace.rs @@ -1,4 +1,4 @@ -use core::fmt; +use core::{error, fmt}; use axerrno::{AxError, AxResult, ax_err}; use axhal::mem::phys_to_virt; @@ -7,6 +7,7 @@ use memory_addr::{ MemoryAddr, PAGE_SIZE_4K, PageIter4K, PhysAddr, VirtAddr, VirtAddrRange, is_aligned_4k, }; use memory_set::{MemoryArea, MemorySet}; +use axhal::arch::flush_tlb; use crate::backend::Backend; use crate::mapping_err_to_ax_err; @@ -39,6 +40,26 @@ impl AddrSpace { &self.pt } + pub fn check_page_dirty(&mut self, vaddr: VirtAddr) -> bool { + // 必须要刷新 tlb,否则会导致标志位不同步! + flush_tlb(Some(vaddr)); + self.pt.is_dirty(vaddr).unwrap() + } + + pub fn set_page_dirty(&mut self, vaddr: VirtAddr, dirty: bool) { + self.pt.set_dirty(vaddr, dirty).unwrap(); + } + + pub fn check_page_access(&mut self, vaddr: VirtAddr) -> bool { + // 必须要刷新 tlb,否则会导致标志位不同步! + flush_tlb(Some(vaddr)); + self.pt.is_accessed(vaddr).unwrap() + } + + pub fn set_page_access(&mut self, vaddr: VirtAddr, access: bool) { + self.pt.set_accessed(vaddr, access).unwrap(); + } + /// Returns the root physical address of the inner page table. pub const fn page_table_root(&self) -> PhysAddr { self.pt.root_paddr() @@ -161,36 +182,6 @@ impl AddrSpace { Ok(()) } - pub fn map_shm( - &mut self, - start: VirtAddr, - size: usize, - flags: MappingFlags, - populate: bool, - ) -> AxResult { - panic!("Unimplement"); - } - - /// Add a new file mapping - pub fn map_file( - &mut self, - start: VirtAddr, - size: usize, - flags: MappingFlags, - fd: i32, - offset: usize, - shared: bool, - populate: bool, - ) -> AxResult { - self.validate_region(start, size)?; - // warn!("map file flags: {}", flags.contains(MappingFlags::WRITE)); - let area = MemoryArea::new(start, size, flags, Backend::new_file(fd, offset, shared, populate)); - self.areas - .map(area, &mut self.pt, false) - .map_err(mapping_err_to_ax_err)?; - Ok(()) - } - /// Forcely set the page table pub fn force_map_page( &mut self, @@ -213,6 +204,19 @@ impl AddrSpace { true } + + pub fn force_unmap_page(&mut self, vaddr: VirtAddr) { + match self.areas.find(vaddr) { + Some(_) => { + self.pt.unmap(vaddr) + .map(|_| true) + .unwrap_or_else(|_| panic!("FORCE FORCE PAGE FAILED(PAGE TABLE FAILEDA): {:#x}!", vaddr)); + }, + _ => panic!("FORCE UNMAP PAGE FAILED(NO AREA): {:#x}!", vaddr), + }; + } + + /// Populates the area with physical frames, returning false if the area /// contains unmapped area. pub fn populate_area(&mut self, mut start: VirtAddr, size: usize) -> AxResult { @@ -412,21 +416,6 @@ impl AddrSpace { false } - /// Returns (fd, offset, shared, popoulate, virtaddr_start) - pub fn get_file_metadata(&mut self, vaddr: VirtAddr) -> Option<(i32, usize, bool, bool, VirtAddr)> { - if !self.va_range.contains(vaddr) { - return None; - } - if let Some(area) = self.areas.find(vaddr) { - match area.backend() { - Backend::File { fd, offset, shared, populate } - => return Some((*fd, *offset, *shared, *populate, area.start())), - _ => return None, - } - } - None - } - /// Handles a page fault at the given address. /// /// `access_flags` indicates the access type that caused the page fault. diff --git a/modules/axmm/src/backend/file.rs b/modules/axmm/src/backend/file.rs deleted file mode 100644 index bdef60b03d..0000000000 --- a/modules/axmm/src/backend/file.rs +++ /dev/null @@ -1,8 +0,0 @@ -use super::Backend; - -impl Backend { - /// Creates a new allocation mapping backend. - pub const fn new_file(fd: i32, offset: usize, populate: bool, shared: bool) -> Self { - Self::File { fd, offset, shared, populate} - } -} diff --git a/modules/axmm/src/backend/mod.rs b/modules/axmm/src/backend/mod.rs index c1a6e35bb7..4420e75129 100644 --- a/modules/axmm/src/backend/mod.rs +++ b/modules/axmm/src/backend/mod.rs @@ -6,7 +6,6 @@ use memory_set::MappingBackend; mod alloc; mod linear; -mod file; /// A unified enum type for different memory mapping backends. /// @@ -37,13 +36,6 @@ pub enum Backend { /// Whether to populate the physical frames when creating the mapping. populate: bool, }, - /// File mapping backend. - File { - fd: i32, - offset: usize, - shared: bool, - populate: bool, - }, } impl MappingBackend for Backend { @@ -54,7 +46,6 @@ impl MappingBackend for Backend { match *self { Self::Linear { pa_va_offset } => Self::map_linear(start, size, flags, pt, pa_va_offset), Self::Alloc { populate } => Self::map_alloc(start, size, flags, pt, populate), - Self::File { .. } => true, } } @@ -62,7 +53,6 @@ impl MappingBackend for Backend { match *self { Self::Linear { pa_va_offset } => Self::unmap_linear(start, size, pt, pa_va_offset), Self::Alloc { populate } => Self::unmap_alloc(start, size, pt, populate), - Self::File { .. } => true, } } @@ -92,7 +82,6 @@ impl Backend { Self::Alloc { populate } => { Self::handle_page_fault_alloc(vaddr, orig_flags, page_table, populate) }, - Self::File { .. } => false, } } } From 841fb0af871cf2474e78788f8709732c9d40e7d4 Mon Sep 17 00:00:00 2001 From: OrangeQi-CQ <2082448790@qq.com> Date: Sun, 8 Jun 2025 19:24:49 +0800 Subject: [PATCH 03/11] debug --- modules/axfs/Cargo.toml | 2 +- modules/axhal/src/arch/x86_64/trap.rs | 1 + modules/axmm/src/aspace.rs | 37 ++++++++++++++++++++++----- modules/axmm/src/backend/alloc.rs | 3 +++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/modules/axfs/Cargo.toml b/modules/axfs/Cargo.toml index 3000dcee91..36219c8bcd 100644 --- a/modules/axfs/Cargo.toml +++ b/modules/axfs/Cargo.toml @@ -19,7 +19,7 @@ fatfs = ["dep:fatfs"] myfs = ["dep:crate_interface"] use-ramdisk = [] -default = ["devfs", "ramfs", "lwext4_rs", "procfs", "sysfs"] +default = ["devfs", "ramfs", "fatfs", "procfs", "sysfs"] [dependencies] log = "=0.4.21" diff --git a/modules/axhal/src/arch/x86_64/trap.rs b/modules/axhal/src/arch/x86_64/trap.rs index 55c42e44b4..5f11404a7a 100644 --- a/modules/axhal/src/arch/x86_64/trap.rs +++ b/modules/axhal/src/arch/x86_64/trap.rs @@ -36,6 +36,7 @@ fn x86_trap_handler(tf: &mut TrapFrame) { if !matches!(tf.vector as u8, IRQ_VECTOR_START..=IRQ_VECTOR_END) { unmask_irqs(tf); } + error!("{:#x?}", tf); match tf.vector as u8 { PAGE_FAULT_VECTOR => handle_page_fault(tf), BREAKPOINT_VECTOR => debug!("#BP @ {:#x} ", tf.rip), diff --git a/modules/axmm/src/aspace.rs b/modules/axmm/src/aspace.rs index 41cdd73e46..f21dcfbc9a 100644 --- a/modules/axmm/src/aspace.rs +++ b/modules/axmm/src/aspace.rs @@ -9,6 +9,7 @@ use memory_addr::{ use memory_set::{MemoryArea, MemorySet}; use axhal::arch::flush_tlb; +use axconfig::plat::PHYS_VIRT_OFFSET; use crate::backend::Backend; use crate::mapping_err_to_ax_err; @@ -189,23 +190,45 @@ impl AddrSpace { paddr: PhysAddr, access_flags: MappingFlags ) -> bool { + match self.pt.query(VirtAddr::from(0x2000)) { + Ok((oldpaddr, _, _)) => assert!(oldpaddr != PhysAddr::from_usize(0xd1851028c48000)), + _ => (), + }; + match self.areas.find(vaddr) { Some(area) => { - if area.flags().contains(access_flags) { - self.pt.map(vaddr, paddr, PageSize::Size4K, area.flags()) - .map(|_| true) - .unwrap_or_else(|_| panic!("FORCE MAP PAGE FAILED(PAGE TABLE FAILEDA): {:#x} => {:#x}!", vaddr, paddr)); - } else { + if !area.flags().contains(access_flags) { panic!("FORCE MAP PAGE FAILED(ACCESS MOD): {:#x} => {:#x}!", vaddr, paddr); + return false; + } + match self.pt.map(vaddr, paddr, PageSize::Size4K, area.flags()) { + Ok(_) => { + match self.pt.query(VirtAddr::from(0x2000)) { + Ok((oldpaddr, _, _)) => assert!(oldpaddr != PhysAddr::from_usize(0xd1851028c48000)), + _ => (), + }; + return true; + }, + Err(e) => { + panic!("FORCE MAP PAGE FAILED(PAGE TABLE FAILED {:?}): {:#x} => {:#x}", e, vaddr, paddr); + return false; + } } }, - _ => panic!("FORCE MAP PAGE FAILED(NO AREA): {:#x} => {:#x}!", vaddr, paddr), + _ => { + panic!("FORCE MAP PAGE FAILED(NO AREA): {:#x} => {:#x}!", vaddr, paddr); + return false; + }, }; - true } pub fn force_unmap_page(&mut self, vaddr: VirtAddr) { + // panic!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaassssssssssssssssssssssssssssssssssss"); + // assert!( + // vaddr.as_usize() < PHYS_VIRT_OFFSET, + // "Force unmap addr is invalid, check if the virtual address is in kernel space" + // ); match self.areas.find(vaddr) { Some(_) => { self.pt.unmap(vaddr) diff --git a/modules/axmm/src/backend/alloc.rs b/modules/axmm/src/backend/alloc.rs index d5ec5787e6..a896188a04 100644 --- a/modules/axmm/src/backend/alloc.rs +++ b/modules/axmm/src/backend/alloc.rs @@ -43,6 +43,7 @@ impl Backend { // allocate all possible physical frames for populated mapping. for addr in PageIter4K::new(start, start + size).unwrap() { if let Some(frame) = alloc_frame(true) { + assert!(frame != PhysAddr::from(0xd1851028c48000)); if let Ok(tlb) = pt.map(addr, frame, PageSize::Size4K, flags) { tlb.ignore(); // TLB flush on map is unnecessary, as there are no outdated mappings. } else { @@ -72,7 +73,9 @@ impl Backend { } tlb.flush(); dealloc_frame(frame); + error!("success unmap {:#x}", addr); } else { + error!("failed unmap {:#x}", addr); // Deallocation is needn't if the page is not mapped. } } From e4dfb2c25bc4ce32fa89146c7a1e19a2b7913261 Mon Sep 17 00:00:00 2001 From: OrangeQi-CQ <2082448790@qq.com> Date: Wed, 11 Jun 2025 12:28:00 +0800 Subject: [PATCH 04/11] Satisfy page cache --- modules/axfs/src/fops.rs | 31 +++++++++++++++---------------- modules/axmm/src/aspace.rs | 19 ++----------------- modules/axmm/src/backend/alloc.rs | 4 ++-- 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/modules/axfs/src/fops.rs b/modules/axfs/src/fops.rs index e87ca97199..6a4a3d8354 100644 --- a/modules/axfs/src/fops.rs +++ b/modules/axfs/src/fops.rs @@ -24,7 +24,6 @@ pub type FilePerm = axfs_vfs::VfsNodePerm; pub struct File { node: WithCap, is_append: bool, - is_direct: bool, offset: u64, } @@ -114,6 +113,10 @@ impl OpenOptions { self.directory } + pub fn has_direct(&self) -> bool { + self.direct + } + /// Sets the create flags. pub fn set_create(mut self, create: bool, create_new: bool) -> Self { self.create = create; @@ -133,6 +136,11 @@ impl OpenOptions { self } + pub fn set_direct(mut self, direct: bool) -> Self { + self.direct = direct; + self + } + const fn is_valid(&self) -> bool { if !self.read && !self.write && !self.append && !self.directory { return false; @@ -163,7 +171,7 @@ impl File { if !opts.is_valid() { return ax_err!(InvalidInput); } - + let node_option = crate::root::lookup(dir, path); let node = if opts.create || opts.create_new { match node_option { @@ -175,14 +183,16 @@ impl File { node } // not exists, create new - Err(VfsError::NotFound) => crate::root::create_file(dir, path)?, + Err(VfsError::NotFound) => { + crate::root::create_file(dir, path)? + }, Err(e) => return Err(e), } } else { // just open the existing node_option? }; - + let attr = node.get_attr()?; if attr.is_dir() { return ax_err!(IsADirectory); @@ -191,7 +201,7 @@ impl File { if !perm_to_cap(attr.perm()).contains(access_cap) { return ax_err!(PermissionDenied); } - + node.open()?; if opts.truncate { node.truncate(0)?; @@ -199,7 +209,6 @@ impl File { Ok(Self { node: WithCap::new(node, access_cap), is_append: opts.append, - is_direct: opts.direct, offset: 0, }) } @@ -287,16 +296,6 @@ impl File { pub fn get_attr(&self) -> AxResult { self.access_node(Cap::empty())?.get_attr() } - - /// Gets the file offset - pub fn get_offset(&self) -> u64 { - self.offset - } - - /// Check whether direct or not - pub fn is_direct(&self) -> bool { - self.is_direct - } } impl Directory { diff --git a/modules/axmm/src/aspace.rs b/modules/axmm/src/aspace.rs index f21dcfbc9a..432677af73 100644 --- a/modules/axmm/src/aspace.rs +++ b/modules/axmm/src/aspace.rs @@ -1,5 +1,4 @@ -use core::{error, fmt}; - +use core::fmt; use axerrno::{AxError, AxResult, ax_err}; use axhal::mem::phys_to_virt; use axhal::paging::{MappingFlags, PageTable, PagingError, PageSize}; @@ -8,8 +7,6 @@ use memory_addr::{ }; use memory_set::{MemoryArea, MemorySet}; use axhal::arch::flush_tlb; - -use axconfig::plat::PHYS_VIRT_OFFSET; use crate::backend::Backend; use crate::mapping_err_to_ax_err; @@ -190,41 +187,29 @@ impl AddrSpace { paddr: PhysAddr, access_flags: MappingFlags ) -> bool { - match self.pt.query(VirtAddr::from(0x2000)) { - Ok((oldpaddr, _, _)) => assert!(oldpaddr != PhysAddr::from_usize(0xd1851028c48000)), - _ => (), - }; - match self.areas.find(vaddr) { Some(area) => { if !area.flags().contains(access_flags) { panic!("FORCE MAP PAGE FAILED(ACCESS MOD): {:#x} => {:#x}!", vaddr, paddr); - return false; } match self.pt.map(vaddr, paddr, PageSize::Size4K, area.flags()) { Ok(_) => { - match self.pt.query(VirtAddr::from(0x2000)) { - Ok((oldpaddr, _, _)) => assert!(oldpaddr != PhysAddr::from_usize(0xd1851028c48000)), - _ => (), - }; return true; }, Err(e) => { panic!("FORCE MAP PAGE FAILED(PAGE TABLE FAILED {:?}): {:#x} => {:#x}", e, vaddr, paddr); - return false; } } }, _ => { panic!("FORCE MAP PAGE FAILED(NO AREA): {:#x} => {:#x}!", vaddr, paddr); - return false; }, }; } pub fn force_unmap_page(&mut self, vaddr: VirtAddr) { - // panic!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaassssssssssssssssssssssssssssssssssss"); + // panic!(""); // assert!( // vaddr.as_usize() < PHYS_VIRT_OFFSET, // "Force unmap addr is invalid, check if the virtual address is in kernel space" diff --git a/modules/axmm/src/backend/alloc.rs b/modules/axmm/src/backend/alloc.rs index a896188a04..cb9c0d59ca 100644 --- a/modules/axmm/src/backend/alloc.rs +++ b/modules/axmm/src/backend/alloc.rs @@ -73,9 +73,9 @@ impl Backend { } tlb.flush(); dealloc_frame(frame); - error!("success unmap {:#x}", addr); + // error!("success unmap {:#x}", addr); } else { - error!("failed unmap {:#x}", addr); + // error!("failed unmap {:#x}", addr); // Deallocation is needn't if the page is not mapped. } } From 45fe967305f2af09f5d52062abefbbf5715ead51 Mon Sep 17 00:00:00 2001 From: OrangeQi-CQ <2082448790@qq.com> Date: Wed, 11 Jun 2025 12:43:39 +0800 Subject: [PATCH 05/11] Satisfy page cache --- Cargo.lock | 4 ++-- modules/axhal/src/arch/x86_64/trap.rs | 1 - modules/axmm/src/backend/alloc.rs | 3 --- modules/axmm/src/backend/mod.rs | 2 +- 4 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ba14785f3..b72985d45c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -757,9 +757,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.41" +version = "0.4.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d" +checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c" dependencies = [ "android-tzdata", "iana-time-zone", diff --git a/modules/axhal/src/arch/x86_64/trap.rs b/modules/axhal/src/arch/x86_64/trap.rs index 5f11404a7a..55c42e44b4 100644 --- a/modules/axhal/src/arch/x86_64/trap.rs +++ b/modules/axhal/src/arch/x86_64/trap.rs @@ -36,7 +36,6 @@ fn x86_trap_handler(tf: &mut TrapFrame) { if !matches!(tf.vector as u8, IRQ_VECTOR_START..=IRQ_VECTOR_END) { unmask_irqs(tf); } - error!("{:#x?}", tf); match tf.vector as u8 { PAGE_FAULT_VECTOR => handle_page_fault(tf), BREAKPOINT_VECTOR => debug!("#BP @ {:#x} ", tf.rip), diff --git a/modules/axmm/src/backend/alloc.rs b/modules/axmm/src/backend/alloc.rs index cb9c0d59ca..d5ec5787e6 100644 --- a/modules/axmm/src/backend/alloc.rs +++ b/modules/axmm/src/backend/alloc.rs @@ -43,7 +43,6 @@ impl Backend { // allocate all possible physical frames for populated mapping. for addr in PageIter4K::new(start, start + size).unwrap() { if let Some(frame) = alloc_frame(true) { - assert!(frame != PhysAddr::from(0xd1851028c48000)); if let Ok(tlb) = pt.map(addr, frame, PageSize::Size4K, flags) { tlb.ignore(); // TLB flush on map is unnecessary, as there are no outdated mappings. } else { @@ -73,9 +72,7 @@ impl Backend { } tlb.flush(); dealloc_frame(frame); - // error!("success unmap {:#x}", addr); } else { - // error!("failed unmap {:#x}", addr); // Deallocation is needn't if the page is not mapped. } } diff --git a/modules/axmm/src/backend/mod.rs b/modules/axmm/src/backend/mod.rs index 4420e75129..be58b3e59d 100644 --- a/modules/axmm/src/backend/mod.rs +++ b/modules/axmm/src/backend/mod.rs @@ -81,7 +81,7 @@ impl Backend { Self::Linear { .. } => false, // Linear mappings should not trigger page faults. Self::Alloc { populate } => { Self::handle_page_fault_alloc(vaddr, orig_flags, page_table, populate) - }, + } } } } From aef3d945cbcf2ac2fb35fc46151801b2dc8b3000 Mon Sep 17 00:00:00 2001 From: OrangeQi-CQ <2082448790@qq.com> Date: Wed, 11 Jun 2025 14:15:11 +0800 Subject: [PATCH 06/11] Satisfy page cache --- Cargo.lock | 4 ++-- modules/axfs/src/fops.rs | 12 ++++++------ modules/axmm/src/aspace.rs | 5 +++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b72985d45c..6ba14785f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -757,9 +757,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.40" +version = "0.4.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c" +checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d" dependencies = [ "android-tzdata", "iana-time-zone", diff --git a/modules/axfs/src/fops.rs b/modules/axfs/src/fops.rs index 6a4a3d8354..b69f0ca6a2 100644 --- a/modules/axfs/src/fops.rs +++ b/modules/axfs/src/fops.rs @@ -168,10 +168,11 @@ impl File { } fn _open_at(dir: Option<&VfsNodeRef>, path: &str, opts: &OpenOptions) -> AxResult { + debug!("open file: {} {:?}", path, opts); if !opts.is_valid() { return ax_err!(InvalidInput); } - + let node_option = crate::root::lookup(dir, path); let node = if opts.create || opts.create_new { match node_option { @@ -183,16 +184,14 @@ impl File { node } // not exists, create new - Err(VfsError::NotFound) => { - crate::root::create_file(dir, path)? - }, + Err(VfsError::NotFound) => crate::root::create_file(dir, path)?, Err(e) => return Err(e), } } else { // just open the existing node_option? }; - + let attr = node.get_attr()?; if attr.is_dir() { return ax_err!(IsADirectory); @@ -201,7 +200,7 @@ impl File { if !perm_to_cap(attr.perm()).contains(access_cap) { return ax_err!(PermissionDenied); } - + node.open()?; if opts.truncate { node.truncate(0)?; @@ -213,6 +212,7 @@ impl File { }) } + /// Opens a file at the path relative to the current directory. Returns a /// [`File`] object. pub fn open(path: &str, opts: &OpenOptions) -> AxResult { diff --git a/modules/axmm/src/aspace.rs b/modules/axmm/src/aspace.rs index 432677af73..555ebb6b12 100644 --- a/modules/axmm/src/aspace.rs +++ b/modules/axmm/src/aspace.rs @@ -1,4 +1,5 @@ use core::fmt; + use axerrno::{AxError, AxResult, ax_err}; use axhal::mem::phys_to_virt; use axhal::paging::{MappingFlags, PageTable, PagingError, PageSize}; @@ -202,7 +203,7 @@ impl AddrSpace { } }, _ => { - panic!("FORCE MAP PAGE FAILED(NO AREA): {:#x} => {:#x}!", vaddr, paddr); + panic!("FORCE MAP PAGE FAILED(NOT FOUND AREA): {:#x} => {:#x}!", vaddr, paddr); }, }; } @@ -220,7 +221,7 @@ impl AddrSpace { .map(|_| true) .unwrap_or_else(|_| panic!("FORCE FORCE PAGE FAILED(PAGE TABLE FAILEDA): {:#x}!", vaddr)); }, - _ => panic!("FORCE UNMAP PAGE FAILED(NO AREA): {:#x}!", vaddr), + _ => panic!("FORCE UNMAP PAGE FAILED(NOT FOUND AREA): {:#x}!", vaddr), }; } From 354271e39b30160f2b22f1dc2af657838ca4a7be Mon Sep 17 00:00:00 2001 From: OrangeQi-CQ <2082448790@qq.com> Date: Wed, 11 Jun 2025 14:33:40 +0800 Subject: [PATCH 07/11] Satisfy page cache --- modules/axfs/src/fops.rs | 1 - modules/axmm/src/aspace.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/modules/axfs/src/fops.rs b/modules/axfs/src/fops.rs index b69f0ca6a2..cff40a8925 100644 --- a/modules/axfs/src/fops.rs +++ b/modules/axfs/src/fops.rs @@ -212,7 +212,6 @@ impl File { }) } - /// Opens a file at the path relative to the current directory. Returns a /// [`File`] object. pub fn open(path: &str, opts: &OpenOptions) -> AxResult { diff --git a/modules/axmm/src/aspace.rs b/modules/axmm/src/aspace.rs index 555ebb6b12..b268cb3638 100644 --- a/modules/axmm/src/aspace.rs +++ b/modules/axmm/src/aspace.rs @@ -428,7 +428,6 @@ impl AddrSpace { /// Handles a page fault at the given address. /// /// `access_flags` indicates the access type that caused the page fault. - /// /// Returns `true` if the page fault is handled successfully (not a real /// fault). pub fn handle_page_fault(&mut self, vaddr: VirtAddr, access_flags: MappingFlags) -> bool { From e88890999de64f5022e65533c162735973da4730 Mon Sep 17 00:00:00 2001 From: OrangeQi-CQ <2082448790@qq.com> Date: Wed, 11 Jun 2025 14:36:27 +0800 Subject: [PATCH 08/11] Satisfy page cache --- modules/axmm/src/aspace.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/modules/axmm/src/aspace.rs b/modules/axmm/src/aspace.rs index b268cb3638..7645310ab7 100644 --- a/modules/axmm/src/aspace.rs +++ b/modules/axmm/src/aspace.rs @@ -210,11 +210,6 @@ impl AddrSpace { pub fn force_unmap_page(&mut self, vaddr: VirtAddr) { - // panic!(""); - // assert!( - // vaddr.as_usize() < PHYS_VIRT_OFFSET, - // "Force unmap addr is invalid, check if the virtual address is in kernel space" - // ); match self.areas.find(vaddr) { Some(_) => { self.pt.unmap(vaddr) From 5e92e321fecba8754251b38bde3ada8703081469 Mon Sep 17 00:00:00 2001 From: OrangeQi-CQ <2082448790@qq.com> Date: Wed, 11 Jun 2025 14:43:38 +0800 Subject: [PATCH 09/11] Satisfy page cache --- modules/axmm/src/aspace.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/axmm/src/aspace.rs b/modules/axmm/src/aspace.rs index 7645310ab7..c583097782 100644 --- a/modules/axmm/src/aspace.rs +++ b/modules/axmm/src/aspace.rs @@ -423,6 +423,7 @@ impl AddrSpace { /// Handles a page fault at the given address. /// /// `access_flags` indicates the access type that caused the page fault. + /// /// Returns `true` if the page fault is handled successfully (not a real /// fault). pub fn handle_page_fault(&mut self, vaddr: VirtAddr, access_flags: MappingFlags) -> bool { From ad8397ee07fa46ae04ff5effa4fc981b7011a240 Mon Sep 17 00:00:00 2001 From: OrangeQi-CQ <2082448790@qq.com> Date: Wed, 11 Jun 2025 14:44:22 +0800 Subject: [PATCH 10/11] Satisfy page cache. --- modules/axmm/src/aspace.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/axmm/src/aspace.rs b/modules/axmm/src/aspace.rs index c583097782..1f87cbb71c 100644 --- a/modules/axmm/src/aspace.rs +++ b/modules/axmm/src/aspace.rs @@ -423,7 +423,7 @@ impl AddrSpace { /// Handles a page fault at the given address. /// /// `access_flags` indicates the access type that caused the page fault. - /// + /// /// Returns `true` if the page fault is handled successfully (not a real /// fault). pub fn handle_page_fault(&mut self, vaddr: VirtAddr, access_flags: MappingFlags) -> bool { From 511c2b62254f3ea2253cb51bd4d1430a838a0a4b Mon Sep 17 00:00:00 2001 From: OrangeQi-CQ <2082448790@qq.com> Date: Wed, 11 Jun 2025 16:36:07 +0800 Subject: [PATCH 11/11] Satisfy page cache. --- Cargo.lock | 4 ++-- Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ba14785f3..6e51ce15ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1288,7 +1288,7 @@ checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" [[package]] name = "page_table_entry" version = "0.5.3" -source = "git+https://github.com/OrangeQi-CQ/page_table_multiarch#b292a473e2a88d6af8daeaafc7ecbf0312b33e88" +source = "git+https://github.com/OrangeQi-CQ/page_table_multiarch?rev=8bbd18a#8bbd18a15770c74734d2b2f69b69243ee53d6739" dependencies = [ "aarch64-cpu 10.0.0", "bitflags 2.9.1", @@ -1299,7 +1299,7 @@ dependencies = [ [[package]] name = "page_table_multiarch" version = "0.5.3" -source = "git+https://github.com/OrangeQi-CQ/page_table_multiarch#b292a473e2a88d6af8daeaafc7ecbf0312b33e88" +source = "git+https://github.com/OrangeQi-CQ/page_table_multiarch?rev=8bbd18a#8bbd18a15770c74734d2b2f69b69243ee53d6739" dependencies = [ "log", "memory_addr", diff --git a/Cargo.toml b/Cargo.toml index 84aaf800eb..667ea51df5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,5 +69,5 @@ axdma = { path = "modules/axdma" } lto = true [patch.crates-io] -page_table_multiarch = { git = "https://github.com/OrangeQi-CQ/page_table_multiarch" } -page_table_entry = { git = "https://github.com/OrangeQi-CQ/page_table_multiarch" } +page_table_multiarch = { git = "https://github.com/OrangeQi-CQ/page_table_multiarch", rev = "8bbd18a" } +page_table_entry = { git = "https://github.com/OrangeQi-CQ/page_table_multiarch", rev = "8bbd18a" }