From 3417046e7b541382d99d7eb34ff8d2bff877e308 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 17 Jan 2023 23:05:20 +0800 Subject: [PATCH] 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. --- kernel/src/arch/riscv/mem.rs | 51 +++++++----------------------------- 1 file changed, 9 insertions(+), 42 deletions(-) diff --git a/kernel/src/arch/riscv/mem.rs b/kernel/src/arch/riscv/mem.rs index d3898aff0..282ec00f8 100644 --- a/kernel/src/arch/riscv/mem.rs +++ b/kernel/src/arch/riscv/mem.rs @@ -30,40 +30,12 @@ extern "C" { fn flush_mmu(); } -const CACHE_LINE_SIZE_IN_U32: usize = 8; -/// `memset` loop-unrolled to the width of a cache line. -/// -/// `n` is the number of cache-line sized elements to set; by -/// specifying `n` this way, we eliminate the possibility of -/// specifying and unaligned memset, at the expense of a more -/// 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 +unsafe fn zeropage(s: *mut u32) { + let page = core::slice::from_raw_parts_mut( + s, + PAGE_SIZE / core::mem::size_of::() + ); + page.fill(0); } bitflags! { @@ -378,8 +350,7 @@ impl MemoryMapping { // Zero-out the new page let page_addr = l0pt_virt as *mut usize; - unsafe { memset(page_addr as *mut u32, 0, - PAGE_SIZE / (core::mem::size_of::() * CACHE_LINE_SIZE_IN_U32)) + unsafe { zeropage(page_addr as *mut u32) }; } @@ -649,9 +620,7 @@ pub fn map_page_inner( )?; // Zero-out the new page - unsafe { memset(l0_pt as *mut u32, 0, - PAGE_SIZE / (core::mem::size_of::() * CACHE_LINE_SIZE_IN_U32) - ) }; + unsafe { zeropage(l0_pt as *mut u32) }; } // Ensure the entry hasn't already been mapped. @@ -953,9 +922,7 @@ pub fn ensure_page_exists_inner(address: usize) -> Result() * CACHE_LINE_SIZE_IN_U32) - ); + zeropage(virt as *mut u32); // Move the page into userspace *entry = (ppn1 << 20)