Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit 5c5fcfe

Browse files
authored
fix[addr_space]: ensure data is copied to kernel address space before convert VA to PA (#52)
Underlying driver assumes a linear mapping between virtual address and physical address when converting them, which is only present in kernel address space. So under circumstance related to physical address, we should copy the buffer to kernel space. - `sys_write` will finally call `axfs::dev::Disk::write_one` in dev.rs, which will use the buffer directly. - read from stdin will finally call `console_read_bytes`, which will also use the buffer directly.
1 parent b55bc93 commit 5c5fcfe

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

modules/axfs/src/dev.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ impl Disk {
7070
pub fn write_one(&mut self, buf: &[u8]) -> DevResult<usize> {
7171
let write_size = if self.offset == 0 && buf.len() >= BLOCK_SIZE {
7272
// whole block
73-
self.dev.write_block(self.block_id, &buf[0..BLOCK_SIZE])?;
73+
// copy data to kernel address space
74+
// Because underlying driver assumes a linear mapping between virtual address and
75+
// physical address when converting them, which is only present in kernel address space.
76+
let data = buf[0..BLOCK_SIZE].to_vec();
77+
self.dev.write_block(self.block_id, &data)?;
7478
self.block_id += 1;
7579
BLOCK_SIZE
7680
} else {

modules/axhal/src/mem.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ pub struct MemRegion {
5454
/// `paddr = vaddr - PHYS_VIRT_OFFSET`.
5555
#[inline]
5656
pub const fn virt_to_phys(vaddr: VirtAddr) -> PhysAddr {
57+
assert!(
58+
vaddr.as_usize() >= PHYS_VIRT_OFFSET,
59+
"Converted address is invalid, check if the virtual address is in kernel space"
60+
);
5761
pa!(vaddr.as_usize() - PHYS_VIRT_OFFSET)
5862
}
5963

0 commit comments

Comments
 (0)