Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 1 | // Copyright 2022, 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 | //! Miscellaneous helper functions. |
| 16 | |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 17 | use core::arch::asm; |
Srivatsa Vaddagiri | c25d68e | 2023-04-19 22:56:33 -0700 | [diff] [blame] | 18 | use core::ops::Range; |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame^] | 19 | use vmbase::read_sysreg; |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 20 | use zeroize::Zeroize; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 21 | |
Pierre-Clément Tosi | f0f9b8b | 2022-10-19 10:12:49 +0100 | [diff] [blame] | 22 | pub const SIZE_4KB: usize = 4 << 10; |
Pierre-Clément Tosi | a1d3ea3 | 2022-11-01 15:05:11 +0000 | [diff] [blame] | 23 | pub const SIZE_2MB: usize = 2 << 20; |
Pierre-Clément Tosi | 164a6f5 | 2023-04-18 19:29:11 +0100 | [diff] [blame] | 24 | pub const SIZE_4MB: usize = 4 << 20; |
Pierre-Clément Tosi | f0f9b8b | 2022-10-19 10:12:49 +0100 | [diff] [blame] | 25 | |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 26 | pub const GUEST_PAGE_SIZE: usize = SIZE_4KB; |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 27 | pub const PVMFW_PAGE_SIZE: usize = SIZE_4KB; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 28 | |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 29 | /// Computes the largest multiple of the provided alignment smaller or equal to the address. |
| 30 | /// |
| 31 | /// Note: the result is undefined if alignment isn't a power of two. |
| 32 | pub const fn unchecked_align_down(addr: usize, alignment: usize) -> usize { |
| 33 | addr & !(alignment - 1) |
| 34 | } |
| 35 | |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 36 | /// Computes the smallest multiple of the provided alignment larger or equal to the address. |
| 37 | /// |
| 38 | /// Note: the result is undefined if alignment isn't a power of two and may wrap to 0. |
| 39 | pub const fn unchecked_align_up(addr: usize, alignment: usize) -> usize { |
| 40 | unchecked_align_down(addr + alignment - 1, alignment) |
| 41 | } |
| 42 | |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 43 | /// Safe wrapper around unchecked_align_up() that validates its assumptions and doesn't wrap. |
| 44 | pub const fn align_up(addr: usize, alignment: usize) -> Option<usize> { |
| 45 | if !alignment.is_power_of_two() { |
| 46 | None |
| 47 | } else if let Some(s) = addr.checked_add(alignment - 1) { |
| 48 | Some(unchecked_align_down(s, alignment)) |
| 49 | } else { |
| 50 | None |
| 51 | } |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 54 | /// Performs an integer division rounding up. |
| 55 | /// |
| 56 | /// Note: Returns None if den isn't a power of two. |
| 57 | pub const fn ceiling_div(num: usize, den: usize) -> Option<usize> { |
| 58 | let Some(r) = align_up(num, den) else { |
| 59 | return None; |
| 60 | }; |
| 61 | |
| 62 | r.checked_div(den) |
| 63 | } |
| 64 | |
Andrew Walbran | 41ebe93 | 2022-12-14 15:22:30 +0000 | [diff] [blame] | 65 | /// Aligns the given address to the given alignment, if it is a power of two. |
| 66 | /// |
| 67 | /// Returns `None` if the alignment isn't a power of two. |
Pierre-Clément Tosi | f19c0e6 | 2023-05-02 13:56:58 +0000 | [diff] [blame] | 68 | #[allow(dead_code)] // Currently unused but might be needed again. |
Andrew Walbran | 41ebe93 | 2022-12-14 15:22:30 +0000 | [diff] [blame] | 69 | pub const fn align_down(addr: usize, alignment: usize) -> Option<usize> { |
| 70 | if !alignment.is_power_of_two() { |
| 71 | None |
| 72 | } else { |
| 73 | Some(unchecked_align_down(addr, alignment)) |
| 74 | } |
| 75 | } |
| 76 | |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 77 | /// Computes the address of the 4KiB page containing a given address. |
Pierre-Clément Tosi | 446136e | 2022-10-19 10:10:42 +0100 | [diff] [blame] | 78 | pub const fn page_4kb_of(addr: usize) -> usize { |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 79 | unchecked_align_down(addr, SIZE_4KB) |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 80 | } |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 81 | |
| 82 | #[inline] |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 83 | /// Read the number of words in the smallest cache line of all the data caches and unified caches. |
| 84 | pub fn min_dcache_line_size() -> usize { |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 85 | const DMINLINE_SHIFT: usize = 16; |
| 86 | const DMINLINE_MASK: usize = 0xf; |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 87 | let ctr_el0 = read_sysreg!("ctr_el0"); |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 88 | |
| 89 | // DminLine: log2 of the number of words in the smallest cache line of all the data caches. |
| 90 | let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK; |
| 91 | |
| 92 | 1 << dminline |
| 93 | } |
| 94 | |
Pierre-Clément Tosi | 2ca2e31 | 2022-11-29 11:24:52 +0000 | [diff] [blame] | 95 | /// Flush `size` bytes of data cache by virtual address. |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 96 | #[inline] |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 97 | pub fn flush_region(start: usize, size: usize) { |
| 98 | let line_size = min_dcache_line_size(); |
| 99 | let end = start + size; |
| 100 | let start = unchecked_align_down(start, line_size); |
| 101 | |
| 102 | for line in (start..end).step_by(line_size) { |
| 103 | // SAFETY - Clearing cache lines shouldn't have Rust-visible side effects. |
Pierre-Clément Tosi | 7d6944f | 2023-03-30 19:14:11 +0100 | [diff] [blame] | 104 | unsafe { |
| 105 | asm!( |
| 106 | "dc cvau, {x}", |
| 107 | x = in(reg) line, |
| 108 | options(nomem, nostack, preserves_flags), |
| 109 | ) |
| 110 | } |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 113 | |
| 114 | #[inline] |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 115 | /// Flushes the slice to the point of unification. |
| 116 | pub fn flush(reg: &[u8]) { |
| 117 | flush_region(reg.as_ptr() as usize, reg.len()) |
| 118 | } |
| 119 | |
| 120 | #[inline] |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 121 | /// Overwrites the slice with zeroes, to the point of unification. |
| 122 | pub fn flushed_zeroize(reg: &mut [u8]) { |
| 123 | reg.zeroize(); |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 124 | flush(reg) |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 125 | } |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 126 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 127 | /// Flatten [[T; N]] into &[T] |
| 128 | /// TODO: use slice::flatten when it graduates from experimental |
| 129 | pub fn flatten<T, const N: usize>(original: &[[T; N]]) -> &[T] { |
| 130 | // SAFETY: no overflow because original (whose size is len()*N) is already in memory |
| 131 | let len = original.len() * N; |
| 132 | // SAFETY: [T] has the same layout as [T;N] |
| 133 | unsafe { core::slice::from_raw_parts(original.as_ptr().cast(), len) } |
| 134 | } |
| 135 | |
Srivatsa Vaddagiri | c25d68e | 2023-04-19 22:56:33 -0700 | [diff] [blame] | 136 | /// Trait to check containment of one range within another. |
| 137 | pub(crate) trait RangeExt { |
| 138 | /// Returns true if `self` is contained within the `other` range. |
| 139 | fn is_within(&self, other: &Self) -> bool; |
| 140 | } |
| 141 | |
| 142 | impl<T: PartialOrd> RangeExt for Range<T> { |
| 143 | fn is_within(&self, other: &Self) -> bool { |
| 144 | self.start >= other.start && self.end <= other.end |
| 145 | } |
| 146 | } |
| 147 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 148 | /// Create &CStr out of &str literal |
| 149 | #[macro_export] |
| 150 | macro_rules! cstr { |
| 151 | ($str:literal) => {{ |
Pierre-Clément Tosi | 7c5df04 | 2023-05-12 12:06:44 +0000 | [diff] [blame] | 152 | core::ffi::CStr::from_bytes_with_nul(concat!($str, "\0").as_bytes()).unwrap() |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 153 | }}; |
| 154 | } |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 155 | |
Jakob Vukalovic | 4c1edbe | 2023-04-17 19:10:57 +0100 | [diff] [blame] | 156 | /// Returns `true` if hardware dirty state management is available. |
| 157 | pub fn dbm_available() -> bool { |
| 158 | if !cfg!(feature = "cpu_feat_hafdbs") { |
| 159 | return false; |
| 160 | } |
| 161 | // Hardware dirty bit management available flag (ID_AA64MMFR1_EL1.HAFDBS[1]) |
| 162 | const DBM_AVAILABLE: usize = 1 << 1; |
| 163 | read_sysreg!("id_aa64mmfr1_el1") & DBM_AVAILABLE != 0 |
| 164 | } |