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
17 use core::arch::asm;
18 use core::ops::Range;
19 use zeroize::Zeroize;
20
21 pub const SIZE_4KB: usize = 4 << 10;
22 pub const SIZE_2MB: usize = 2 << 20;
23 pub const SIZE_4MB: usize = 4 << 20;
24
25 pub const GUEST_PAGE_SIZE: usize = SIZE_4KB;
26 pub const PVMFW_PAGE_SIZE: usize = SIZE_4KB;
27
28 /// Read a value from a system register.
29 #[macro_export]
30 macro_rules! read_sysreg {
31 ($sysreg:literal) => {{
32 let mut r: usize;
33 // Safe because it reads a system register and does not affect Rust.
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 /// Write 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 /// Computes the largest multiple of the provided alignment smaller or equal to the address.
63 ///
64 /// Note: the result is undefined if alignment isn't a power of two.
unchecked_align_down(addr: usize, alignment: usize) -> usize65 pub const fn unchecked_align_down(addr: usize, alignment: usize) -> usize {
66 addr & !(alignment - 1)
67 }
68
69 /// Computes the smallest multiple of the provided alignment larger or equal to the address.
70 ///
71 /// Note: the result is undefined if alignment isn't a power of two and may wrap to 0.
unchecked_align_up(addr: usize, alignment: usize) -> usize72 pub const fn unchecked_align_up(addr: usize, alignment: usize) -> usize {
73 unchecked_align_down(addr + alignment - 1, alignment)
74 }
75
76 /// Safe wrapper around unchecked_align_up() that validates its assumptions and doesn't wrap.
align_up(addr: usize, alignment: usize) -> Option<usize>77 pub const fn align_up(addr: usize, alignment: usize) -> Option<usize> {
78 if !alignment.is_power_of_two() {
79 None
80 } else if let Some(s) = addr.checked_add(alignment - 1) {
81 Some(unchecked_align_down(s, alignment))
82 } else {
83 None
84 }
85 }
86
87 /// Performs an integer division rounding up.
88 ///
89 /// Note: Returns None if den isn't a power of two.
ceiling_div(num: usize, den: usize) -> Option<usize>90 pub const fn ceiling_div(num: usize, den: usize) -> Option<usize> {
91 let Some(r) = align_up(num, den) else {
92 return None;
93 };
94
95 r.checked_div(den)
96 }
97
98 /// Aligns the given address to the given alignment, if it is a power of two.
99 ///
100 /// Returns `None` if the alignment isn't a power of two.
101 #[allow(dead_code)] // Currently unused but might be needed again.
align_down(addr: usize, alignment: usize) -> Option<usize>102 pub const fn align_down(addr: usize, alignment: usize) -> Option<usize> {
103 if !alignment.is_power_of_two() {
104 None
105 } else {
106 Some(unchecked_align_down(addr, alignment))
107 }
108 }
109
110 /// Computes the address of the 4KiB page containing a given address.
page_4kb_of(addr: usize) -> usize111 pub const fn page_4kb_of(addr: usize) -> usize {
112 unchecked_align_down(addr, SIZE_4KB)
113 }
114
115 #[inline]
116 /// Read the number of words in the smallest cache line of all the data caches and unified caches.
min_dcache_line_size() -> usize117 pub fn min_dcache_line_size() -> usize {
118 const DMINLINE_SHIFT: usize = 16;
119 const DMINLINE_MASK: usize = 0xf;
120 let ctr_el0 = read_sysreg!("ctr_el0");
121
122 // DminLine: log2 of the number of words in the smallest cache line of all the data caches.
123 let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK;
124
125 1 << dminline
126 }
127
128 /// Flush `size` bytes of data cache by virtual address.
129 #[inline]
flush_region(start: usize, size: usize)130 pub fn flush_region(start: usize, size: usize) {
131 let line_size = min_dcache_line_size();
132 let end = start + size;
133 let start = unchecked_align_down(start, line_size);
134
135 for line in (start..end).step_by(line_size) {
136 // SAFETY - Clearing cache lines shouldn't have Rust-visible side effects.
137 unsafe {
138 asm!(
139 "dc cvau, {x}",
140 x = in(reg) line,
141 options(nomem, nostack, preserves_flags),
142 )
143 }
144 }
145 }
146
147 #[inline]
148 /// Flushes the slice to the point of unification.
flush(reg: &[u8])149 pub fn flush(reg: &[u8]) {
150 flush_region(reg.as_ptr() as usize, reg.len())
151 }
152
153 #[inline]
154 /// Overwrites the slice with zeroes, to the point of unification.
flushed_zeroize(reg: &mut [u8])155 pub fn flushed_zeroize(reg: &mut [u8]) {
156 reg.zeroize();
157 flush(reg)
158 }
159
160 /// Flatten [[T; N]] into &[T]
161 /// TODO: use slice::flatten when it graduates from experimental
flatten<T, const N: usize>(original: &[[T; N]]) -> &[T]162 pub fn flatten<T, const N: usize>(original: &[[T; N]]) -> &[T] {
163 // SAFETY: no overflow because original (whose size is len()*N) is already in memory
164 let len = original.len() * N;
165 // SAFETY: [T] has the same layout as [T;N]
166 unsafe { core::slice::from_raw_parts(original.as_ptr().cast(), len) }
167 }
168
169 /// Trait to check containment of one range within another.
170 pub(crate) trait RangeExt {
171 /// Returns true if `self` is contained within the `other` range.
is_within(&self, other: &Self) -> bool172 fn is_within(&self, other: &Self) -> bool;
173 }
174
175 impl<T: PartialOrd> RangeExt for Range<T> {
is_within(&self, other: &Self) -> bool176 fn is_within(&self, other: &Self) -> bool {
177 self.start >= other.start && self.end <= other.end
178 }
179 }
180
181 /// Create &CStr out of &str literal
182 #[macro_export]
183 macro_rules! cstr {
184 ($str:literal) => {{
185 CStr::from_bytes_with_nul(concat!($str, "\0").as_bytes()).unwrap()
186 }};
187 }
188