mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-20 09:03:28 +00:00
kernel: mem: fix memory zeroing
Fix memory zeroing when requesting a physical pointer. Previously, only the first 25% of a page was zeroed due to a bug when casting between *mut u8 and *mut usize. This now uses `write_volatile` to ensure the memory clear isn't optimized out, and uses `usize` pointers everywhere. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
f2e9d2b76c
commit
006a50d206
2 changed files with 20 additions and 4 deletions
|
|
@ -451,7 +451,11 @@ impl MemoryManager {
|
||||||
let virt = virt as *mut usize;
|
let virt = virt as *mut usize;
|
||||||
|
|
||||||
// Zero-out the page
|
// Zero-out the page
|
||||||
unsafe { virt.write_bytes(0, PAGE_SIZE / core::mem::size_of::<usize>()) };
|
let range_start = virt;
|
||||||
|
let range_end = range_start.wrapping_add(PAGE_SIZE / core::mem::size_of::<usize>());
|
||||||
|
unsafe {
|
||||||
|
crate::mem::bzero(range_start, range_end);
|
||||||
|
};
|
||||||
if is_user {
|
if is_user {
|
||||||
crate::arch::mem::hand_page_to_user(virt as _)?;
|
crate::arch::mem::hand_page_to_user(virt as _)?;
|
||||||
}
|
}
|
||||||
|
|
@ -870,3 +874,14 @@ impl MemoryManager {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub unsafe fn bzero<T>(mut start: *mut T, end: *mut T)
|
||||||
|
where
|
||||||
|
T: Copy,
|
||||||
|
{
|
||||||
|
while start < end {
|
||||||
|
// NOTE(volatile) to prevent this from being transformed into `memclr`
|
||||||
|
core::ptr::write_volatile(start, core::mem::zeroed());
|
||||||
|
start = start.offset(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -833,10 +833,11 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR
|
||||||
|
|
||||||
if !phys_ptr.is_null() {
|
if !phys_ptr.is_null() {
|
||||||
if mm.is_main_memory(phys_ptr) {
|
if mm.is_main_memory(phys_ptr) {
|
||||||
|
let range_start = range.as_mut_ptr() as *mut usize;
|
||||||
|
let range_end =
|
||||||
|
range_start.wrapping_add(range.len() / core::mem::size_of::<usize>());
|
||||||
unsafe {
|
unsafe {
|
||||||
range
|
crate::mem::bzero(range_start, range_end);
|
||||||
.as_mut_ptr()
|
|
||||||
.write_bytes(0, range.len() / core::mem::size_of::<usize>())
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
for offset in (range.as_ptr() as usize..(range.as_ptr() as usize + range.len()))
|
for offset in (range.as_ptr() as usize..(range.as_ptr() as usize + range.len()))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue