blob: 5d9a75b0e49749b89a8029852cf995a57fc58bcb [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;
Pierre-Clément Tosi8383c542022-11-01 14:07:29 +000018use zeroize::Zeroize;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000019
Pierre-Clément Tosif0f9b8b2022-10-19 10:12:49 +010020pub const SIZE_4KB: usize = 4 << 10;
Pierre-Clément Tosia1d3ea32022-11-01 15:05:11 +000021pub const SIZE_2MB: usize = 2 << 20;
Pierre-Clément Tosif0f9b8b2022-10-19 10:12:49 +010022
Pierre-Clément Tosi4f4f5eb2022-12-08 14:31:42 +000023pub const GUEST_PAGE_SIZE: usize = SIZE_4KB;
24
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +010025/// Computes the largest multiple of the provided alignment smaller or equal to the address.
26///
27/// Note: the result is undefined if alignment isn't a power of two.
28pub const fn unchecked_align_down(addr: usize, alignment: usize) -> usize {
29 addr & !(alignment - 1)
30}
31
Pierre-Clément Tosi20b60962022-10-17 13:35:27 +010032/// Computes the smallest multiple of the provided alignment larger or equal to the address.
33///
34/// Note: the result is undefined if alignment isn't a power of two and may wrap to 0.
35pub const fn unchecked_align_up(addr: usize, alignment: usize) -> usize {
36 unchecked_align_down(addr + alignment - 1, alignment)
37}
38
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +010039/// Safe wrapper around unchecked_align_up() that validates its assumptions and doesn't wrap.
40pub const fn align_up(addr: usize, alignment: usize) -> Option<usize> {
41 if !alignment.is_power_of_two() {
42 None
43 } else if let Some(s) = addr.checked_add(alignment - 1) {
44 Some(unchecked_align_down(s, alignment))
45 } else {
46 None
47 }
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010048}
49
Pierre-Clément Tosi1cc5eb72023-02-02 11:09:18 +000050/// Performs an integer division rounding up.
51///
52/// Note: Returns None if den isn't a power of two.
53pub const fn ceiling_div(num: usize, den: usize) -> Option<usize> {
54 let Some(r) = align_up(num, den) else {
55 return None;
56 };
57
58 r.checked_div(den)
59}
60
Andrew Walbran41ebe932022-12-14 15:22:30 +000061/// Aligns the given address to the given alignment, if it is a power of two.
62///
63/// Returns `None` if the alignment isn't a power of two.
64pub const fn align_down(addr: usize, alignment: usize) -> Option<usize> {
65 if !alignment.is_power_of_two() {
66 None
67 } else {
68 Some(unchecked_align_down(addr, alignment))
69 }
70}
71
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010072/// Computes the address of the 4KiB page containing a given address.
Pierre-Clément Tosi446136e2022-10-19 10:10:42 +010073pub const fn page_4kb_of(addr: usize) -> usize {
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +010074 unchecked_align_down(addr, SIZE_4KB)
Pierre-Clément Tosida4440a2022-08-22 18:06:32 +010075}
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000076
77#[inline]
78fn min_dcache_line_size() -> usize {
79 const DMINLINE_SHIFT: usize = 16;
80 const DMINLINE_MASK: usize = 0xf;
81 let ctr_el0: usize;
82
83 unsafe { asm!("mrs {x}, ctr_el0", x = out(reg) ctr_el0) }
84
85 // DminLine: log2 of the number of words in the smallest cache line of all the data caches.
86 let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK;
87
88 1 << dminline
89}
90
Pierre-Clément Tosi2ca2e312022-11-29 11:24:52 +000091/// Flush `size` bytes of data cache by virtual address.
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000092#[inline]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000093pub fn flush_region(start: usize, size: usize) {
94 let line_size = min_dcache_line_size();
95 let end = start + size;
96 let start = unchecked_align_down(start, line_size);
97
98 for line in (start..end).step_by(line_size) {
99 // SAFETY - Clearing cache lines shouldn't have Rust-visible side effects.
Pierre-Clément Tosi7d6944f2023-03-30 19:14:11 +0100100 unsafe {
101 asm!(
102 "dc cvau, {x}",
103 x = in(reg) line,
104 options(nomem, nostack, preserves_flags),
105 )
106 }
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000107 }
108}
Pierre-Clément Tosi8383c542022-11-01 14:07:29 +0000109
110#[inline]
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000111/// Flushes the slice to the point of unification.
112pub fn flush(reg: &[u8]) {
113 flush_region(reg.as_ptr() as usize, reg.len())
114}
115
116#[inline]
Pierre-Clément Tosi8383c542022-11-01 14:07:29 +0000117/// Overwrites the slice with zeroes, to the point of unification.
118pub fn flushed_zeroize(reg: &mut [u8]) {
119 reg.zeroize();
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000120 flush(reg)
Pierre-Clément Tosi8383c542022-11-01 14:07:29 +0000121}
Jiyong Parkb87f3302023-03-21 10:03:11 +0900122
Jiyong Park9c63cd12023-03-21 17:53:07 +0900123/// Flatten [[T; N]] into &[T]
124/// TODO: use slice::flatten when it graduates from experimental
125pub fn flatten<T, const N: usize>(original: &[[T; N]]) -> &[T] {
126 // SAFETY: no overflow because original (whose size is len()*N) is already in memory
127 let len = original.len() * N;
128 // SAFETY: [T] has the same layout as [T;N]
129 unsafe { core::slice::from_raw_parts(original.as_ptr().cast(), len) }
130}
131
Jiyong Parkb87f3302023-03-21 10:03:11 +0900132/// Create &CStr out of &str literal
133#[macro_export]
134macro_rules! cstr {
135 ($str:literal) => {{
136 CStr::from_bytes_with_nul(concat!($str, "\0").as_bytes()).unwrap()
137 }};
138}