x86/registers/
tlb.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2025.
4
5// This is inspired and adapted for Tock from the [x86](https://github.com/gz/rust-x86) crate.
6
7//! Functions to flush the translation lookaside buffer (TLB).
8
9/// Invalidate the given address in the TLB using the `invlpg` instruction.
10///
11/// # Safety
12/// This function is unsafe as it causes a general protection fault (GP) if the current privilege
13/// level is not 0.
14#[cfg(target_arch = "x86")]
15pub unsafe fn flush(addr: usize) {
16    use core::arch::asm;
17
18    unsafe {
19        asm!("invlpg ({})", in(reg) addr, options(att_syntax, nostack, preserves_flags));
20    }
21}
22
23/// Invalidate the TLB completely by reloading the CR3 register.
24///
25/// # Safety
26/// This function is unsafe as it causes a general protection fault (GP) if the current privilege
27/// level is not 0.
28pub unsafe fn flush_all() {
29    use crate::registers::controlregs;
30    unsafe { controlregs::cr3_write(controlregs::cr3()) }
31}
32
33// For CI only
34
35#[cfg(not(any(doc, target_arch = "x86")))]
36pub unsafe fn flush(_addr: usize) {
37    unimplemented!()
38}