cleanup memset command a bit more

- we don't need a general memset call -- just a zero page call
- use native rust semantics to embody the call. some alignment checking is
done on the arguments on the way in and out that seem worth it
- the unrolled loop doesn't pay off because we're at the limit of
write bandwidth on the CPU at this point. we would need write-back
caches to gain more performance through unrolling.
This commit is contained in:
bunnie 2023-01-17 23:05:20 +08:00
parent a2f7c19eac
commit 3417046e7b

View file

@ -30,40 +30,12 @@ extern "C" {
fn flush_mmu(); fn flush_mmu();
} }
const CACHE_LINE_SIZE_IN_U32: usize = 8; unsafe fn zeropage(s: *mut u32) {
/// `memset` loop-unrolled to the width of a cache line. let page = core::slice::from_raw_parts_mut(
/// s,
/// `n` is the number of cache-line sized elements to set; by PAGE_SIZE / core::mem::size_of::<u32>()
/// specifying `n` this way, we eliminate the possibility of );
/// specifying and unaligned memset, at the expense of a more page.fill(0);
/// obtuse API. However, this call is only used in this module,
/// so each invocation can be manually checked for correctness.
///
/// Thus, in order to zero out a page, this is called with an `n`
/// that is equal to 4096 (bytes in a page), divided by 4 (bytes
/// per u32), divided by 8 (number of u32 per cache line)
unsafe fn memset(s: *mut u32, c: i32, n: usize) -> *mut u32 {
// unroll loop to the width of a cache line
let mut i = 0;
while i < n * CACHE_LINE_SIZE_IN_U32 {
*s.add(i) = c as u32;
i += 1;
*s.add(i) = c as u32;
i += 1;
*s.add(i) = c as u32;
i += 1;
*s.add(i) = c as u32;
i += 1;
*s.add(i) = c as u32;
i += 1;
*s.add(i) = c as u32;
i += 1;
*s.add(i) = c as u32;
i += 1;
*s.add(i) = c as u32;
i += 1;
}
s
} }
bitflags! { bitflags! {
@ -378,8 +350,7 @@ impl MemoryMapping {
// Zero-out the new page // Zero-out the new page
let page_addr = l0pt_virt as *mut usize; let page_addr = l0pt_virt as *mut usize;
unsafe { memset(page_addr as *mut u32, 0, unsafe { zeropage(page_addr as *mut u32)
PAGE_SIZE / (core::mem::size_of::<u32>() * CACHE_LINE_SIZE_IN_U32))
}; };
} }
@ -649,9 +620,7 @@ pub fn map_page_inner(
)?; )?;
// Zero-out the new page // Zero-out the new page
unsafe { memset(l0_pt as *mut u32, 0, unsafe { zeropage(l0_pt as *mut u32) };
PAGE_SIZE / (core::mem::size_of::<u32>() * CACHE_LINE_SIZE_IN_U32)
) };
} }
// Ensure the entry hasn't already been mapped. // Ensure the entry hasn't already been mapped.
@ -953,9 +922,7 @@ pub fn ensure_page_exists_inner(address: usize) -> Result<usize, xous_kernel::Er
flush_mmu(); flush_mmu();
// Zero-out the page // Zero-out the page
memset(virt as *mut u32, 0, zeropage(virt as *mut u32);
PAGE_SIZE / (core::mem::size_of::<u32>() * CACHE_LINE_SIZE_IN_U32)
);
// Move the page into userspace // Move the page into userspace
*entry = (ppn1 << 20) *entry = (ppn1 << 20)