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 | 626347c | 2024-11-29 13:08:27 +0100 | [diff] [blame] | 17 | pub mod dbm; |
Michał Mazurek | 747c08d | 2024-11-29 13:26:22 +0100 | [diff] [blame] | 18 | pub mod exceptions; |
Michał Mazurek | 3a9ecbf | 2024-11-29 13:09:12 +0100 | [diff] [blame] | 19 | pub mod hvc; |
Michał Mazurek | f08509a | 2025-01-24 11:39:24 +0000 | [diff] [blame] | 20 | pub mod layout; |
Michał Mazurek | 6cd065f | 2024-11-29 13:07:11 +0100 | [diff] [blame] | 21 | pub mod linker; |
Michał Mazurek | 5b29469 | 2024-11-29 13:05:46 +0100 | [diff] [blame] | 22 | pub mod page_table; |
Bartłomiej Grzesik | 86f108d | 2024-11-29 15:48:00 +0100 | [diff] [blame] | 23 | pub mod platform; |
Bartłomiej Grzesik | ee0a5c6 | 2025-02-04 09:06:17 +0000 | [diff] [blame] | 24 | pub mod rand; |
Bartłomiej Grzesik | 3d7fccf | 2025-01-30 15:10:01 +0000 | [diff] [blame] | 25 | pub mod uart; |
Bartłomiej Grzesik | 86f108d | 2024-11-29 15:48:00 +0100 | [diff] [blame] | 26 | |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 27 | /// Reads a value from a system register. |
| 28 | #[macro_export] |
| 29 | macro_rules! read_sysreg { |
| 30 | ($sysreg:literal) => {{ |
| 31 | let mut r: usize; |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 32 | #[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] | 33 | // SAFETY: Reading a system register does not affect memory. |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 34 | unsafe { |
| 35 | core::arch::asm!( |
| 36 | concat!("mrs {}, ", $sysreg), |
| 37 | out(reg) r, |
| 38 | options(nomem, nostack, preserves_flags), |
| 39 | ) |
| 40 | } |
| 41 | r |
| 42 | }}; |
| 43 | } |
| 44 | |
| 45 | /// Writes a value to a system register. |
| 46 | /// |
| 47 | /// # Safety |
| 48 | /// |
| 49 | /// Callers must ensure that side effects of updating the system register are properly handled. |
| 50 | #[macro_export] |
| 51 | macro_rules! write_sysreg { |
| 52 | ($sysreg:literal, $val:expr) => {{ |
| 53 | let value: usize = $val; |
| 54 | core::arch::asm!( |
| 55 | concat!("msr ", $sysreg, ", {}"), |
| 56 | in(reg) value, |
| 57 | options(nomem, nostack, preserves_flags), |
| 58 | ) |
| 59 | }}; |
| 60 | } |
| 61 | |
| 62 | /// Executes an instruction synchronization barrier. |
| 63 | #[macro_export] |
| 64 | macro_rules! isb { |
| 65 | () => {{ |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 66 | #[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] | 67 | // SAFETY: memory barriers do not affect Rust's memory model. |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 68 | unsafe { |
| 69 | core::arch::asm!("isb", options(nomem, nostack, preserves_flags)); |
| 70 | } |
| 71 | }}; |
| 72 | } |
| 73 | |
| 74 | /// Executes a data synchronization barrier. |
| 75 | #[macro_export] |
| 76 | macro_rules! dsb { |
| 77 | ($option:literal) => {{ |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 78 | #[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] | 79 | // SAFETY: memory barriers do not affect Rust's memory model. |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 80 | unsafe { |
| 81 | core::arch::asm!(concat!("dsb ", $option), options(nomem, nostack, preserves_flags)); |
| 82 | } |
| 83 | }}; |
| 84 | } |
| 85 | |
Pierre-Clément Tosi | 8ab7c37 | 2024-10-30 20:46:04 +0000 | [diff] [blame] | 86 | /// Executes a data cache operation. |
| 87 | #[macro_export] |
| 88 | macro_rules! dc { |
| 89 | ($option:literal, $addr:expr) => {{ |
| 90 | let addr: usize = $addr; |
| 91 | #[allow(unused_unsafe)] // In case the macro is used within an unsafe block. |
| 92 | // SAFETY: Clearing cache lines shouldn't have Rust-visible side effects. |
| 93 | unsafe { |
| 94 | core::arch::asm!( |
| 95 | concat!("dc ", $option, ", {x}"), |
| 96 | x = in(reg) addr, |
| 97 | options(nomem, nostack, preserves_flags), |
| 98 | ); |
| 99 | } |
| 100 | }}; |
| 101 | } |
| 102 | |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 103 | /// Invalidates cached leaf PTE entries by virtual address. |
| 104 | #[macro_export] |
| 105 | macro_rules! tlbi { |
| 106 | ($option:literal, $asid:expr, $addr:expr) => {{ |
| 107 | let asid: usize = $asid; |
| 108 | let addr: usize = $addr; |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 109 | #[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] | 110 | // SAFETY: Invalidating the TLB doesn't affect Rust. When the address matches a |
| 111 | // 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] | 112 | unsafe { |
| 113 | core::arch::asm!( |
| 114 | concat!("tlbi ", $option, ", {x}"), |
| 115 | x = in(reg) (asid << 48) | (addr >> 12), |
| 116 | options(nomem, nostack, preserves_flags) |
| 117 | ); |
| 118 | } |
| 119 | }}; |
| 120 | } |
Pierre-Clément Tosi | 4ec3a93 | 2024-10-08 18:10:25 +0100 | [diff] [blame] | 121 | |
Pierre-Clément Tosi | 043dfb7 | 2024-10-30 21:17:10 +0000 | [diff] [blame] | 122 | /// STRB intrinsics. |
Pierre-Clément Tosi | 4ec3a93 | 2024-10-08 18:10:25 +0100 | [diff] [blame] | 123 | /// |
| 124 | /// See https://github.com/rust-lang/rust/issues/131894 |
| 125 | /// |
| 126 | /// # Safety |
| 127 | /// |
| 128 | /// `dst` must be valid for writes. |
Pierre-Clément Tosi | 043dfb7 | 2024-10-30 21:17:10 +0000 | [diff] [blame] | 129 | #[inline] |
| 130 | pub unsafe fn strb(dst: *mut u8, src: u8) { |
Pierre-Clément Tosi | 4ec3a93 | 2024-10-08 18:10:25 +0100 | [diff] [blame] | 131 | // SAFETY: strb only modifies *dst, which must be valid for writes. |
| 132 | unsafe { |
| 133 | core::arch::asm!( |
| 134 | "strb {value:w}, [{ptr}]", |
| 135 | value = in(reg) src, |
| 136 | ptr = in(reg) dst, |
| 137 | options(preserves_flags), |
| 138 | ); |
| 139 | } |
| 140 | } |
Pierre-Clément Tosi | 8ab7c37 | 2024-10-30 20:46:04 +0000 | [diff] [blame] | 141 | |
| 142 | /// Reads the number of words in the smallest cache line of all the data caches and unified caches. |
| 143 | #[inline] |
| 144 | pub fn min_dcache_line_size() -> usize { |
| 145 | const DMINLINE_SHIFT: usize = 16; |
| 146 | const DMINLINE_MASK: usize = 0xf; |
| 147 | let ctr_el0 = read_sysreg!("ctr_el0"); |
| 148 | |
| 149 | // DminLine: log2 of the number of words in the smallest cache line of all the data caches. |
| 150 | let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK; |
| 151 | |
| 152 | 1 << dminline |
| 153 | } |