diff --git a/modules/axfs/src/dev.rs b/modules/axfs/src/dev.rs index f7832db623..17871f42fe 100644 --- a/modules/axfs/src/dev.rs +++ b/modules/axfs/src/dev.rs @@ -70,7 +70,11 @@ impl Disk { pub fn write_one(&mut self, buf: &[u8]) -> DevResult { let write_size = if self.offset == 0 && buf.len() >= BLOCK_SIZE { // whole block - self.dev.write_block(self.block_id, &buf[0..BLOCK_SIZE])?; + // copy data to kernel address space + // Because underlying driver assumes a linear mapping between virtual address and + // physical address when converting them, which is only present in kernel address space. + let data = buf[0..BLOCK_SIZE].to_vec(); + self.dev.write_block(self.block_id, &data)?; self.block_id += 1; BLOCK_SIZE } else { 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) }