|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | +// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | +// option. This file may not be copied, modified, or distributed |
| 7 | +// except according to those terms. |
| 8 | + |
| 9 | +//! Architecture-specific code. |
| 10 | +
|
| 11 | +use core::arch::asm; |
| 12 | + |
| 13 | +/// Data Synchronization Barrier. |
| 14 | +pub fn dsb() { |
| 15 | + // SAFETY: Data Synchronization Barrier is always safe. |
| 16 | + unsafe { |
| 17 | + asm!("dsb sy", options(nostack, preserves_flags)); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/// Data Memory Barrier. |
| 22 | +pub fn dmb() { |
| 23 | + // SAFETY: Data Memory Barrier is always safe. |
| 24 | + unsafe { |
| 25 | + asm!("dmb sy", options(nostack, preserves_flags)); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// Instruction Synchronization Barrier. |
| 30 | +pub fn isb() { |
| 31 | + // SAFETY: Instruction Synchronization Barrier is always safe. |
| 32 | + unsafe { |
| 33 | + asm!("isb", options(nostack, preserves_flags)); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// Invalidate all instruction caches. |
| 38 | +pub fn ic_iallu() { |
| 39 | + // SAFETY: `ic iallu` is always safe. |
| 40 | + unsafe { |
| 41 | + asm!("ic iallu", options(nostack, preserves_flags)); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Invalidate all TLB entries for EL2. |
| 46 | +pub fn tlbi_alle2is() { |
| 47 | + // SAFETY: `tlbi alle2is` is always safe. |
| 48 | + unsafe { |
| 49 | + asm!("tlbi alle2is", options(nostack, preserves_flags)); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +macro_rules! sys_reg { |
| 54 | + ($name:ident, {$($const_name:ident: $const_val:expr),*}) => { |
| 55 | + pub mod $name { |
| 56 | + use core::arch::asm; |
| 57 | + $(pub const $const_name: u64 = $const_val;)* |
| 58 | + |
| 59 | + #[doc = concat!("Read the `", stringify!($name), "` system register.")] |
| 60 | + /// |
| 61 | + /// # Safety |
| 62 | + /// |
| 63 | + /// This function emits a raw `MRS` instruction. The caller must guarantee that: |
| 64 | + /// |
| 65 | + /// * The register is readable at the current Exception Level. |
| 66 | + /// * Reading the register does not destructively alter hardware state (e.g., |
| 67 | + /// acknowledging an interrupt by reading `ICC_IAR1_EL1`). |
| 68 | + #[allow(unused)] |
| 69 | + pub unsafe fn read() -> u64 { |
| 70 | + let val: u64; |
| 71 | + // SAFETY: The caller must ensure that the register is safely readable. |
| 72 | + unsafe { |
| 73 | + asm!(concat!("mrs {}, ", stringify!($name)), out(reg) val, options(nostack, preserves_flags)); |
| 74 | + } |
| 75 | + val |
| 76 | + } |
| 77 | + |
| 78 | + #[doc = concat!("Write the `", stringify!($name), "` system register.")] |
| 79 | + /// |
| 80 | + /// # Safety |
| 81 | + /// |
| 82 | + /// This function allows fundamental changes to the CPU state. To avoid Undefined |
| 83 | + /// Behavior, the caller must guarantee: |
| 84 | + /// |
| 85 | + /// * The register is writable at the current Exception Level. |
| 86 | + /// * The write must not invalidate the stack, the heap, or any active Rust references |
| 87 | + /// (e.g., by disabling the MMU). |
| 88 | + /// * This function emits a raw `MSR`. The caller is responsible for issuing context |
| 89 | + /// synchronization (e.g., `ISB`) or memory barriers (`DSB`) if required. |
| 90 | + #[allow(unused)] |
| 91 | + pub unsafe fn write(val: u64) { |
| 92 | + // SAFETY: The caller must ensure that the register is safely writeable. |
| 93 | + unsafe { |
| 94 | + asm!(concat!("msr ", stringify!($name), ", {}"), in(reg) val, options(nostack, preserves_flags)); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + }; |
| 99 | + ($name:ident) => { |
| 100 | + sys_reg!($name, {}); |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 104 | +sys_reg!(sctlr_el2, { |
| 105 | + M: 1 << 0, |
| 106 | + C: 1 << 2, |
| 107 | + I: 1 << 12 |
| 108 | +}); |
| 109 | +sys_reg!(clidr_el1); |
| 110 | +sys_reg!(csselr_el1); |
| 111 | +sys_reg!(ccsidr_el1); |
| 112 | +sys_reg!(hcr_el2); |
| 113 | +sys_reg!(cntvoff_el2); |
| 114 | +sys_reg!(cnthctl_el2); |
| 115 | +sys_reg!(spsr_el2); |
| 116 | +sys_reg!(elr_el2); |
| 117 | +sys_reg!(sp_el1); |
| 118 | + |
| 119 | +pub(super) fn disable_mmu_and_caches() { |
| 120 | + invalidate_dcache(); |
| 121 | + |
| 122 | + // Disable MMU and caches |
| 123 | + let mut sctlr: u64; |
| 124 | + // SAFETY: We are reading a non-destructive register at our current Exception Level. |
| 125 | + unsafe { |
| 126 | + sctlr = sctlr_el2::read(); |
| 127 | + } |
| 128 | + sctlr &= !sctlr_el2::M; // MMU Enable |
| 129 | + sctlr &= !sctlr_el2::C; // Data Cache Enable |
| 130 | + sctlr &= !sctlr_el2::I; // Instruction Cache Enable |
| 131 | + // SAFETY: We assume we have an identity mapped pagetables for the currently running |
| 132 | + // code, so disabling MMU is safe. |
| 133 | + unsafe { |
| 134 | + sctlr_el2::write(sctlr); |
| 135 | + } |
| 136 | + dsb(); |
| 137 | + isb(); |
| 138 | + |
| 139 | + // Invalidate I-cache |
| 140 | + ic_iallu(); |
| 141 | + tlbi_alle2is(); |
| 142 | + |
| 143 | + // Final synchronization |
| 144 | + dsb(); |
| 145 | + isb(); |
| 146 | +} |
| 147 | + |
| 148 | +/// Invalidate D-cache by set/way to the point of coherency. |
| 149 | +pub fn invalidate_dcache() { |
| 150 | + dmb(); |
| 151 | + |
| 152 | + // Cache Level ID Register |
| 153 | + let clidr: u64; |
| 154 | + // SAFETY: We are reading a non-destructive register at a higher Exception Level. |
| 155 | + unsafe { |
| 156 | + clidr = clidr_el1::read(); |
| 157 | + } |
| 158 | + |
| 159 | + // Level of Coherence (LoC) - Bits [26:24] |
| 160 | + let loc = (clidr >> 24) & 0x7; |
| 161 | + |
| 162 | + for level in 0..loc { |
| 163 | + let cache_type = (clidr >> (level * 3)) & 0x7; |
| 164 | + |
| 165 | + // Cache Types: 0=None, 1=Instruction, 2=Data, 3=Split, 4=Unified |
| 166 | + // We don't care about No cache or Instruction cache |
| 167 | + if cache_type < 2 { |
| 168 | + continue; |
| 169 | + } |
| 170 | + |
| 171 | + // Select the Cache Level in CSSELR (Cache Size Selection Register) |
| 172 | + // SAFETY: Writing to `csselr_el1` is always safe, assuming the cache level exists. |
| 173 | + unsafe { |
| 174 | + csselr_el1::write(level << 1); |
| 175 | + } |
| 176 | + |
| 177 | + // Barrier to ensure CSSELR write finishes before reading CCSIDR |
| 178 | + isb(); |
| 179 | + |
| 180 | + // Cache Size ID Register (CCSIDR) |
| 181 | + let ccsidr: u64; |
| 182 | + // SAFETY: We are reading a non-destructive register at a higher Exception Level. |
| 183 | + unsafe { |
| 184 | + ccsidr = ccsidr_el1::read(); |
| 185 | + } |
| 186 | + |
| 187 | + let line_power = (ccsidr & 0x7) + 4; |
| 188 | + let ways = (ccsidr >> 3) & 0x3FF; |
| 189 | + let sets = (ccsidr >> 13) & 0x7FFF; |
| 190 | + |
| 191 | + let way_shift = (ways as u32).leading_zeros(); |
| 192 | + |
| 193 | + for set in 0..=sets { |
| 194 | + for way in 0..=ways { |
| 195 | + let dc_val = (way << way_shift) | (set << line_power) | (level << 1); |
| 196 | + |
| 197 | + // SAFETY: `dc cisw` is always safe, assuming the cache line exists. |
| 198 | + unsafe { |
| 199 | + asm!("dc cisw, {0}", in(reg) dc_val); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + dsb(); |
| 206 | + isb(); |
| 207 | +} |
0 commit comments