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 | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame^] | 19 | use vmbase::memory::SIZE_4KB; |
Alice Wang | 81399f5 | 2023-05-26 14:23:43 +0000 | [diff] [blame] | 20 | use vmbase::read_sysreg; |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame^] | 21 | use vmbase::util::unchecked_align_down; |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 22 | use zeroize::Zeroize; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 23 | |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 24 | pub const GUEST_PAGE_SIZE: usize = SIZE_4KB; |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 25 | pub const PVMFW_PAGE_SIZE: usize = SIZE_4KB; |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 26 | |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 27 | #[inline] |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 28 | /// Read the number of words in the smallest cache line of all the data caches and unified caches. |
| 29 | pub fn min_dcache_line_size() -> usize { |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 30 | const DMINLINE_SHIFT: usize = 16; |
| 31 | const DMINLINE_MASK: usize = 0xf; |
Jakob Vukalovic | c9afb51 | 2023-03-30 16:04:32 +0000 | [diff] [blame] | 32 | let ctr_el0 = read_sysreg!("ctr_el0"); |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 33 | |
| 34 | // DminLine: log2 of the number of words in the smallest cache line of all the data caches. |
| 35 | let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK; |
| 36 | |
| 37 | 1 << dminline |
| 38 | } |
| 39 | |
Pierre-Clément Tosi | 2ca2e31 | 2022-11-29 11:24:52 +0000 | [diff] [blame] | 40 | /// Flush `size` bytes of data cache by virtual address. |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 41 | #[inline] |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 42 | pub fn flush_region(start: usize, size: usize) { |
| 43 | let line_size = min_dcache_line_size(); |
| 44 | let end = start + size; |
| 45 | let start = unchecked_align_down(start, line_size); |
| 46 | |
| 47 | for line in (start..end).step_by(line_size) { |
| 48 | // 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] | 49 | unsafe { |
| 50 | asm!( |
| 51 | "dc cvau, {x}", |
| 52 | x = in(reg) line, |
| 53 | options(nomem, nostack, preserves_flags), |
| 54 | ) |
| 55 | } |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 58 | |
| 59 | #[inline] |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 60 | /// Flushes the slice to the point of unification. |
| 61 | pub fn flush(reg: &[u8]) { |
| 62 | flush_region(reg.as_ptr() as usize, reg.len()) |
| 63 | } |
| 64 | |
| 65 | #[inline] |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 66 | /// Overwrites the slice with zeroes, to the point of unification. |
| 67 | pub fn flushed_zeroize(reg: &mut [u8]) { |
| 68 | reg.zeroize(); |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 69 | flush(reg) |
Pierre-Clément Tosi | 8383c54 | 2022-11-01 14:07:29 +0000 | [diff] [blame] | 70 | } |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 71 | |
Srivatsa Vaddagiri | c25d68e | 2023-04-19 22:56:33 -0700 | [diff] [blame] | 72 | /// Trait to check containment of one range within another. |
| 73 | pub(crate) trait RangeExt { |
| 74 | /// Returns true if `self` is contained within the `other` range. |
| 75 | fn is_within(&self, other: &Self) -> bool; |
| 76 | } |
| 77 | |
| 78 | impl<T: PartialOrd> RangeExt for Range<T> { |
| 79 | fn is_within(&self, other: &Self) -> bool { |
| 80 | self.start >= other.start && self.end <= other.end |
| 81 | } |
| 82 | } |
| 83 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 84 | /// Create &CStr out of &str literal |
| 85 | #[macro_export] |
| 86 | macro_rules! cstr { |
| 87 | ($str:literal) => {{ |
Pierre-Clément Tosi | 7c5df04 | 2023-05-12 12:06:44 +0000 | [diff] [blame] | 88 | 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] | 89 | }}; |
| 90 | } |