blob: a6f0dd5c42630725928e3a1e6dfc911fee63df1f [file] [log] [blame]
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +01001// 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 Tosia0934c12022-11-25 20:54:11 +000017use core::arch::asm;
Srivatsa Vaddagiric25d68e2023-04-19 22:56:33 -070018use core::ops::Range;
Pierre-Clément Tosi8383c542022-11-01 14:07:29 +000019use zeroize::Zeroize;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000020
Pierre-Clément Tosif0f9b8b2022-10-19 10:12:49 +010021pub const SIZE_4KB: usize = 4 << 10;
Pierre-Clément Tosia1d3ea32022-11-01 15:05:11 +000022pub const SIZE_2MB: usize = 2 << 20;
Pierre-Clément Tosi164a6f52023-04-18 19:29:11 +010023pub const SIZE_4MB: usize = 4 << 20;
Pierre-Clément Tosif0f9b8b2022-10-19 10:12:49 +010024
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000025pub const GUEST_PAGE_SIZE: usize = SIZE_4KB;
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010026pub const PVMFW_PAGE_SIZE: usize = SIZE_4KB;
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000027
Jakob Vukalovicc9afb512023-03-30 16:04:32 +000028/// Read a value from a system register.
29#[macro_export]
30macro_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.
Pierre-Clément Tosi2aedaae2023-04-14 15:01:47 +010046///
47/// # Safety
48///
49/// Callers must ensure that side effects of updating the system register are properly handled.
Jakob Vukalovicc9afb512023-03-30 16:04:32 +000050#[macro_export]
51macro_rules! write_sysreg {
52 ($sysreg:literal, $val:expr) => {{
53 let value: usize = $val;
Pierre-Clément Tosi2aedaae2023-04-14 15:01:47 +010054 core::arch::asm!(
55 concat!("msr ", $sysreg, ", {}"),
56 in(reg) value,
57 options(nomem, nostack, preserves_flags),
58 )
Jakob Vukalovicc9afb512023-03-30 16:04:32 +000059 }};
60}
61
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +010062/// 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.
65pub const fn unchecked_align_down(addr: usize, alignment: usize) -> usize {
66 addr & !(alignment - 1)
67}
68
Pierre-Clément Tosi20b60962022-10-17 13:35:27 +010069/// 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.
72pub const fn unchecked_align_up(addr: usize, alignment: usize) -> usize {
73 unchecked_align_down(addr + alignment - 1, alignment)
74}
75
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +010076/// Safe wrapper around unchecked_align_up() that validates its assumptions and doesn't wrap.
77pub 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 }
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010085}
86
Pierre-Clément Tosi1cc5eb72023-02-02 11:09:18 +000087/// Performs an integer division rounding up.
88///
89/// Note: Returns None if den isn't a power of two.
90pub 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
Andrew Walbran41ebe932022-12-14 15:22:30 +000098/// 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.
101pub const fn align_down(addr: usize, alignment: usize) -> Option<usize> {
102 if !alignment.is_power_of_two() {
103 None
104 } else {
105 Some(unchecked_align_down(addr, alignment))
106 }
107}
108
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +0100109/// Computes the address of the 4KiB page containing a given address.
Pierre-Clément Tosi446136e2022-10-19 10:10:42 +0100110pub const fn page_4kb_of(addr: usize) -> usize {
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +0100111 unchecked_align_down(addr, SIZE_4KB)
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +0100112}
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000113
114#[inline]
Pierre-Clément Tosi97f52492023-04-04 15:52:17 +0100115/// Read the number of words in the smallest cache line of all the data caches and unified caches.
116pub fn min_dcache_line_size() -> usize {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000117 const DMINLINE_SHIFT: usize = 16;
118 const DMINLINE_MASK: usize = 0xf;
Jakob Vukalovicc9afb512023-03-30 16:04:32 +0000119 let ctr_el0 = read_sysreg!("ctr_el0");
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000120
121 // DminLine: log2 of the number of words in the smallest cache line of all the data caches.
122 let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK;
123
124 1 << dminline
125}
126
Pierre-Clément Tosi2ca2e312022-11-29 11:24:52 +0000127/// Flush `size` bytes of data cache by virtual address.
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000128#[inline]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000129pub fn flush_region(start: usize, size: usize) {
130 let line_size = min_dcache_line_size();
131 let end = start + size;
132 let start = unchecked_align_down(start, line_size);
133
134 for line in (start..end).step_by(line_size) {
135 // SAFETY - Clearing cache lines shouldn't have Rust-visible side effects.
Pierre-Clément Tosi7d6944f2023-03-30 19:14:11 +0100136 unsafe {
137 asm!(
138 "dc cvau, {x}",
139 x = in(reg) line,
140 options(nomem, nostack, preserves_flags),
141 )
142 }
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000143 }
144}
Pierre-Clément Tosi8383c542022-11-01 14:07:29 +0000145
146#[inline]
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000147/// Flushes the slice to the point of unification.
148pub fn flush(reg: &[u8]) {
149 flush_region(reg.as_ptr() as usize, reg.len())
150}
151
152#[inline]
Pierre-Clément Tosi8383c542022-11-01 14:07:29 +0000153/// Overwrites the slice with zeroes, to the point of unification.
154pub fn flushed_zeroize(reg: &mut [u8]) {
155 reg.zeroize();
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000156 flush(reg)
Pierre-Clément Tosi8383c542022-11-01 14:07:29 +0000157}
Jiyong Parkb87f3302023-03-21 10:03:11 +0900158
Jiyong Park9c63cd12023-03-21 17:53:07 +0900159/// Flatten [[T; N]] into &[T]
160/// TODO: use slice::flatten when it graduates from experimental
161pub fn flatten<T, const N: usize>(original: &[[T; N]]) -> &[T] {
162 // SAFETY: no overflow because original (whose size is len()*N) is already in memory
163 let len = original.len() * N;
164 // SAFETY: [T] has the same layout as [T;N]
165 unsafe { core::slice::from_raw_parts(original.as_ptr().cast(), len) }
166}
167
Srivatsa Vaddagiric25d68e2023-04-19 22:56:33 -0700168/// Trait to check containment of one range within another.
169pub(crate) trait RangeExt {
170 /// Returns true if `self` is contained within the `other` range.
171 fn is_within(&self, other: &Self) -> bool;
172}
173
174impl<T: PartialOrd> RangeExt for Range<T> {
175 fn is_within(&self, other: &Self) -> bool {
176 self.start >= other.start && self.end <= other.end
177 }
178}
179
Jiyong Parkb87f3302023-03-21 10:03:11 +0900180/// Create &CStr out of &str literal
181#[macro_export]
182macro_rules! cstr {
183 ($str:literal) => {{
184 CStr::from_bytes_with_nul(concat!($str, "\0").as_bytes()).unwrap()
185 }};
186}