cacheflush_sys/
lib.rs

1use libc;
2
3extern "C" {
4    fn clear_cache(start: *const u8, end: *const usize);
5}
6
7pub unsafe fn flush(start_addr: *const u8, len: usize) -> std::io::Result<()> {
8    unsafe { *libc::__errno_location() = 0 };
9    let end = start_addr.wrapping_add(len);
10    clear_cache(start_addr, end as *const usize);
11    let e = std::io::Error::last_os_error();
12    match e.raw_os_error() {
13        Some(errno) if errno != 0 => Err(e),
14        Some(_) => Ok(()),
15        None => Ok(()),
16    }
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22
23    #[test]
24    fn it_works_always_on_x86_64() {
25        unsafe {
26            flush(0xffff00 as *const u8, 4096).unwrap();
27        }
28    }
29}