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; |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 18 | use zeroize::Zeroize; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 19 | |
Pierre-Clément Tosi | f0f9b8b | 2022-10-19 10:12:49 +0100 | [diff] [blame] | 20 | pub const SIZE_4KB: usize = 4 << 10; |
Pierre-Clément Tosi | a1d3ea3 | 2022-11-01 15:05:11 +0000 | [diff] [blame] | 21 | pub const SIZE_2MB: usize = 2 << 20; |
Pierre-Clément Tosi | f0f9b8b | 2022-10-19 10:12:49 +0100 | [diff] [blame] | 22 | |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 23 | pub const GUEST_PAGE_SIZE: usize = SIZE_4KB; |
| 24 | |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 25 | /// Read a value from a system register. |
| 26 | #[macro_export] |
| 27 | macro_rules! read_sysreg { |
| 28 | ($sysreg:literal) => {{ |
| 29 | let mut r: usize; |
| 30 | // Safe because it reads a system register and does not affect Rust. |
| 31 | unsafe { |
| 32 | core::arch::asm!( |
| 33 | concat!("mrs {}, ", $sysreg), |
| 34 | out(reg) r, |
| 35 | options(nomem, nostack, preserves_flags), |
| 36 | ) |
| 37 | } |
| 38 | r |
| 39 | }}; |
| 40 | } |
| 41 | |
| 42 | /// Write a value to a system register. |
| 43 | #[macro_export] |
| 44 | macro_rules! write_sysreg { |
| 45 | ($sysreg:literal, $val:expr) => {{ |
| 46 | let value: usize = $val; |
| 47 | // Safe because it writes a system register and does not affect Rust. |
| 48 | unsafe { |
| 49 | core::arch::asm!( |
| 50 | concat!("msr ", $sysreg, ", {}"), |
| 51 | in(reg) value, |
| 52 | options(nomem, nostack, preserves_flags), |
| 53 | ) |
| 54 | } |
| 55 | }}; |
| 56 | } |
| 57 | |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 58 | /// Computes the largest multiple of the provided alignment smaller or equal to the address. |
| 59 | /// |
| 60 | /// Note: the result is undefined if alignment isn't a power of two. |
| 61 | pub const fn unchecked_align_down(addr: usize, alignment: usize) -> usize { |
| 62 | addr & !(alignment - 1) |
| 63 | } |
| 64 | |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 65 | /// Computes the smallest multiple of the provided alignment larger or equal to the address. |
| 66 | /// |
| 67 | /// Note: the result is undefined if alignment isn't a power of two and may wrap to 0. |
| 68 | pub const fn unchecked_align_up(addr: usize, alignment: usize) -> usize { |
| 69 | unchecked_align_down(addr + alignment - 1, alignment) |
| 70 | } |
| 71 | |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 72 | /// Safe wrapper around unchecked_align_up() that validates its assumptions and doesn't wrap. |
| 73 | pub const fn align_up(addr: usize, alignment: usize) -> Option<usize> { |
| 74 | if !alignment.is_power_of_two() { |
| 75 | None |
| 76 | } else if let Some(s) = addr.checked_add(alignment - 1) { |
| 77 | Some(unchecked_align_down(s, alignment)) |
| 78 | } else { |
| 79 | None |
| 80 | } |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Pierre-Clément Tosi | 1cc5eb7 | 2023-02-02 11:09:18 +0000 | [diff] [blame] | 83 | /// Performs an integer division rounding up. |
| 84 | /// |
| 85 | /// Note: Returns None if den isn't a power of two. |
| 86 | pub const fn ceiling_div(num: usize, den: usize) -> Option<usize> { |
| 87 | let Some(r) = align_up(num, den) else { |
| 88 | return None; |
| 89 | }; |
| 90 | |
| 91 | r.checked_div(den) |
| 92 | } |
| 93 | |
Andrew Walbran | 41ebe93 | 2022-12-14 15:22:30 +0000 | [diff] [blame] | 94 | /// Aligns the given address to the given alignment, if it is a power of two. |
| 95 | /// |
| 96 | /// Returns `None` if the alignment isn't a power of two. |
| 97 | pub const fn align_down(addr: usize, alignment: usize) -> Option<usize> { |
| 98 | if !alignment.is_power_of_two() { |
| 99 | None |
| 100 | } else { |
| 101 | Some(unchecked_align_down(addr, alignment)) |
| 102 | } |
| 103 | } |
| 104 | |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 105 | /// 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] | 106 | pub const fn page_4kb_of(addr: usize) -> usize { |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 107 | unchecked_align_down(addr, SIZE_4KB) |
Pierre-Clément Tosi | da4440a | 2022-08-22 18:06:32 +0100 | [diff] [blame] | 108 | } |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 109 | |
| 110 | #[inline] |
| 111 | fn min_dcache_line_size() -> usize { |
| 112 | const DMINLINE_SHIFT: usize = 16; |
| 113 | const DMINLINE_MASK: usize = 0xf; |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 114 | let ctr_el0 = read_sysreg!("ctr_el0"); |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 115 | |
| 116 | // DminLine: log2 of the number of words in the smallest cache line of all the data caches. |
| 117 | let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK; |
| 118 | |
| 119 | 1 << dminline |
| 120 | } |
| 121 | |
Pierre-Clément Tosi | 2ca2e31 | 2022-11-29 11:24:52 +0000 | [diff] [blame] | 122 | /// Flush `size` bytes of data cache by virtual address. |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 123 | #[inline] |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 124 | pub fn flush_region(start: usize, size: usize) { |
| 125 | let line_size = min_dcache_line_size(); |
| 126 | let end = start + size; |
| 127 | let start = unchecked_align_down(start, line_size); |
| 128 | |
| 129 | for line in (start..end).step_by(line_size) { |
| 130 | // 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] | 131 | unsafe { |
| 132 | asm!( |
| 133 | "dc cvau, {x}", |
| 134 | x = in(reg) line, |
| 135 | options(nomem, nostack, preserves_flags), |
| 136 | ) |
| 137 | } |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 140 | |
| 141 | #[inline] |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 142 | /// Flushes the slice to the point of unification. |
| 143 | pub fn flush(reg: &[u8]) { |
| 144 | flush_region(reg.as_ptr() as usize, reg.len()) |
| 145 | } |
| 146 | |
| 147 | #[inline] |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 148 | /// Overwrites the slice with zeroes, to the point of unification. |
| 149 | pub fn flushed_zeroize(reg: &mut [u8]) { |
| 150 | reg.zeroize(); |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 151 | flush(reg) |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 152 | } |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 153 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame] | 154 | /// Flatten [[T; N]] into &[T] |
| 155 | /// TODO: use slice::flatten when it graduates from experimental |
| 156 | pub fn flatten<T, const N: usize>(original: &[[T; N]]) -> &[T] { |
| 157 | // SAFETY: no overflow because original (whose size is len()*N) is already in memory |
| 158 | let len = original.len() * N; |
| 159 | // SAFETY: [T] has the same layout as [T;N] |
| 160 | unsafe { core::slice::from_raw_parts(original.as_ptr().cast(), len) } |
| 161 | } |
| 162 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 163 | /// Create &CStr out of &str literal |
| 164 | #[macro_export] |
| 165 | macro_rules! cstr { |
| 166 | ($str:literal) => {{ |
| 167 | CStr::from_bytes_with_nul(concat!($str, "\0").as_bytes()).unwrap() |
| 168 | }}; |
| 169 | } |