Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 1 | // Copyright 2023, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Wrappers of assembly calls. |
| 16 | |
Michał Mazurek | f08509a | 2025-01-24 11:39:24 +0000 | [diff] [blame^] | 17 | pub mod layout; |
Bartłomiej Grzesik | 86f108d | 2024-11-29 15:48:00 +0100 | [diff] [blame] | 18 | pub mod platform; |
| 19 | |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 20 | /// Reads a value from a system register. |
| 21 | #[macro_export] |
| 22 | macro_rules! read_sysreg { |
| 23 | ($sysreg:literal) => {{ |
| 24 | let mut r: usize; |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 25 | #[allow(unused_unsafe)] // In case the macro is used within an unsafe block. |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 26 | // SAFETY: Reading a system register does not affect memory. |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 27 | unsafe { |
| 28 | core::arch::asm!( |
| 29 | concat!("mrs {}, ", $sysreg), |
| 30 | out(reg) r, |
| 31 | options(nomem, nostack, preserves_flags), |
| 32 | ) |
| 33 | } |
| 34 | r |
| 35 | }}; |
| 36 | } |
| 37 | |
| 38 | /// Writes a value to a system register. |
| 39 | /// |
| 40 | /// # Safety |
| 41 | /// |
| 42 | /// Callers must ensure that side effects of updating the system register are properly handled. |
| 43 | #[macro_export] |
| 44 | macro_rules! write_sysreg { |
| 45 | ($sysreg:literal, $val:expr) => {{ |
| 46 | let value: usize = $val; |
| 47 | core::arch::asm!( |
| 48 | concat!("msr ", $sysreg, ", {}"), |
| 49 | in(reg) value, |
| 50 | options(nomem, nostack, preserves_flags), |
| 51 | ) |
| 52 | }}; |
| 53 | } |
| 54 | |
| 55 | /// Executes an instruction synchronization barrier. |
| 56 | #[macro_export] |
| 57 | macro_rules! isb { |
| 58 | () => {{ |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 59 | #[allow(unused_unsafe)] // In case the macro is used within an unsafe block. |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 60 | // SAFETY: memory barriers do not affect Rust's memory model. |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 61 | unsafe { |
| 62 | core::arch::asm!("isb", options(nomem, nostack, preserves_flags)); |
| 63 | } |
| 64 | }}; |
| 65 | } |
| 66 | |
| 67 | /// Executes a data synchronization barrier. |
| 68 | #[macro_export] |
| 69 | macro_rules! dsb { |
| 70 | ($option:literal) => {{ |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 71 | #[allow(unused_unsafe)] // In case the macro is used within an unsafe block. |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 72 | // SAFETY: memory barriers do not affect Rust's memory model. |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 73 | unsafe { |
| 74 | core::arch::asm!(concat!("dsb ", $option), options(nomem, nostack, preserves_flags)); |
| 75 | } |
| 76 | }}; |
| 77 | } |
| 78 | |
Pierre-Clément Tosi | 8ab7c37 | 2024-10-30 20:46:04 +0000 | [diff] [blame] | 79 | /// Executes a data cache operation. |
| 80 | #[macro_export] |
| 81 | macro_rules! dc { |
| 82 | ($option:literal, $addr:expr) => {{ |
| 83 | let addr: usize = $addr; |
| 84 | #[allow(unused_unsafe)] // In case the macro is used within an unsafe block. |
| 85 | // SAFETY: Clearing cache lines shouldn't have Rust-visible side effects. |
| 86 | unsafe { |
| 87 | core::arch::asm!( |
| 88 | concat!("dc ", $option, ", {x}"), |
| 89 | x = in(reg) addr, |
| 90 | options(nomem, nostack, preserves_flags), |
| 91 | ); |
| 92 | } |
| 93 | }}; |
| 94 | } |
| 95 | |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 96 | /// Invalidates cached leaf PTE entries by virtual address. |
| 97 | #[macro_export] |
| 98 | macro_rules! tlbi { |
| 99 | ($option:literal, $asid:expr, $addr:expr) => {{ |
| 100 | let asid: usize = $asid; |
| 101 | let addr: usize = $addr; |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 102 | #[allow(unused_unsafe)] // In case the macro is used within an unsafe block. |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 103 | // SAFETY: Invalidating the TLB doesn't affect Rust. When the address matches a |
| 104 | // block entry larger than the page size, all translations for the block are invalidated. |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 105 | unsafe { |
| 106 | core::arch::asm!( |
| 107 | concat!("tlbi ", $option, ", {x}"), |
| 108 | x = in(reg) (asid << 48) | (addr >> 12), |
| 109 | options(nomem, nostack, preserves_flags) |
| 110 | ); |
| 111 | } |
| 112 | }}; |
| 113 | } |
Pierre-Clément Tosi | 4ec3a93 | 2024-10-08 18:10:25 +0100 | [diff] [blame] | 114 | |
Pierre-Clément Tosi | 043dfb7 | 2024-10-30 21:17:10 +0000 | [diff] [blame] | 115 | /// STRB intrinsics. |
Pierre-Clément Tosi | 4ec3a93 | 2024-10-08 18:10:25 +0100 | [diff] [blame] | 116 | /// |
| 117 | /// See https://github.com/rust-lang/rust/issues/131894 |
| 118 | /// |
| 119 | /// # Safety |
| 120 | /// |
| 121 | /// `dst` must be valid for writes. |
Pierre-Clément Tosi | 043dfb7 | 2024-10-30 21:17:10 +0000 | [diff] [blame] | 122 | #[inline] |
| 123 | pub unsafe fn strb(dst: *mut u8, src: u8) { |
Pierre-Clément Tosi | 4ec3a93 | 2024-10-08 18:10:25 +0100 | [diff] [blame] | 124 | // SAFETY: strb only modifies *dst, which must be valid for writes. |
| 125 | unsafe { |
| 126 | core::arch::asm!( |
| 127 | "strb {value:w}, [{ptr}]", |
| 128 | value = in(reg) src, |
| 129 | ptr = in(reg) dst, |
| 130 | options(preserves_flags), |
| 131 | ); |
| 132 | } |
| 133 | } |
Pierre-Clément Tosi | 8ab7c37 | 2024-10-30 20:46:04 +0000 | [diff] [blame] | 134 | |
| 135 | /// Reads the number of words in the smallest cache line of all the data caches and unified caches. |
| 136 | #[inline] |
| 137 | pub fn min_dcache_line_size() -> usize { |
| 138 | const DMINLINE_SHIFT: usize = 16; |
| 139 | const DMINLINE_MASK: usize = 0xf; |
| 140 | let ctr_el0 = read_sysreg!("ctr_el0"); |
| 141 | |
| 142 | // DminLine: log2 of the number of words in the smallest cache line of all the data caches. |
| 143 | let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK; |
| 144 | |
| 145 | 1 << dminline |
| 146 | } |