Alice Wang | f47b234 | 2023-06-02 11:51:57 +0000 | [diff] [blame] | 1 | // Copyright 2023, 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 | //! Utility functions for memory management. |
| 16 | |
Alice Wang | 3fa9b80 | 2023-06-06 07:52:31 +0000 | [diff] [blame] | 17 | use crate::read_sysreg; |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame] | 18 | use crate::util::unchecked_align_down; |
Alice Wang | 3fa9b80 | 2023-06-06 07:52:31 +0000 | [diff] [blame] | 19 | use core::arch::asm; |
Alice Wang | f47b234 | 2023-06-02 11:51:57 +0000 | [diff] [blame] | 20 | use core::ptr::NonNull; |
Alice Wang | 3fa9b80 | 2023-06-06 07:52:31 +0000 | [diff] [blame] | 21 | use zeroize::Zeroize; |
Alice Wang | f47b234 | 2023-06-02 11:51:57 +0000 | [diff] [blame] | 22 | |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame] | 23 | /// The size of a 4KB memory in bytes. |
| 24 | pub const SIZE_4KB: usize = 4 << 10; |
Pierre-Clément Tosi | c332fae | 2023-06-22 11:37:12 +0000 | [diff] [blame] | 25 | /// The size of a 64KB memory in bytes. |
| 26 | pub const SIZE_64KB: usize = 64 << 10; |
Pierre-Clément Tosi | f3681e8 | 2023-06-22 11:38:22 +0000 | [diff] [blame] | 27 | /// The size of a 128KB memory in bytes. |
| 28 | pub const SIZE_128KB: usize = 128 << 10; |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame] | 29 | /// The size of a 2MB memory in bytes. |
| 30 | pub const SIZE_2MB: usize = 2 << 20; |
| 31 | /// The size of a 4MB memory in bytes. |
| 32 | pub const SIZE_4MB: usize = 4 << 20; |
| 33 | |
Alice Wang | 4b3cc11 | 2023-06-06 12:22:53 +0000 | [diff] [blame] | 34 | /// The page size in bytes assumed by vmbase - 4 KiB. |
| 35 | pub const PAGE_SIZE: usize = SIZE_4KB; |
| 36 | |
Alice Wang | 3fa9b80 | 2023-06-06 07:52:31 +0000 | [diff] [blame] | 37 | /// Reads the number of words in the smallest cache line of all the data caches and unified caches. |
| 38 | #[inline] |
| 39 | pub fn min_dcache_line_size() -> usize { |
| 40 | const DMINLINE_SHIFT: usize = 16; |
| 41 | const DMINLINE_MASK: usize = 0xf; |
| 42 | let ctr_el0 = read_sysreg!("ctr_el0"); |
| 43 | |
| 44 | // DminLine: log2 of the number of words in the smallest cache line of all the data caches. |
| 45 | let dminline = (ctr_el0 >> DMINLINE_SHIFT) & DMINLINE_MASK; |
| 46 | |
| 47 | 1 << dminline |
| 48 | } |
| 49 | |
| 50 | /// Flush `size` bytes of data cache by virtual address. |
| 51 | #[inline] |
| 52 | pub(super) fn flush_region(start: usize, size: usize) { |
| 53 | let line_size = min_dcache_line_size(); |
| 54 | let end = start + size; |
| 55 | let start = unchecked_align_down(start, line_size); |
| 56 | |
| 57 | for line in (start..end).step_by(line_size) { |
Andrew Walbran | c06e734 | 2023-07-05 14:00:51 +0000 | [diff] [blame] | 58 | // SAFETY: Clearing cache lines shouldn't have Rust-visible side effects. |
Alice Wang | 3fa9b80 | 2023-06-06 07:52:31 +0000 | [diff] [blame] | 59 | unsafe { |
| 60 | asm!( |
| 61 | "dc cvau, {x}", |
| 62 | x = in(reg) line, |
| 63 | options(nomem, nostack, preserves_flags), |
| 64 | ) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// Flushes the slice to the point of unification. |
| 70 | #[inline] |
| 71 | pub fn flush(reg: &[u8]) { |
| 72 | flush_region(reg.as_ptr() as usize, reg.len()) |
| 73 | } |
| 74 | |
| 75 | /// Overwrites the slice with zeroes, to the point of unification. |
| 76 | #[inline] |
| 77 | pub fn flushed_zeroize(reg: &mut [u8]) { |
| 78 | reg.zeroize(); |
| 79 | flush(reg) |
| 80 | } |
| 81 | |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame] | 82 | /// Computes the address of the 4KiB page containing a given address. |
| 83 | pub const fn page_4kb_of(addr: usize) -> usize { |
| 84 | unchecked_align_down(addr, SIZE_4KB) |
| 85 | } |
| 86 | |
Alice Wang | f47b234 | 2023-06-02 11:51:57 +0000 | [diff] [blame] | 87 | /// Returns the intermediate physical address corresponding to the given virtual address. |
| 88 | /// |
| 89 | /// As we use identity mapping for everything, this is just a cast, but it's useful to use it to be |
| 90 | /// explicit about where we are converting from virtual to physical address. |
| 91 | pub fn virt_to_phys(vaddr: NonNull<u8>) -> usize { |
| 92 | vaddr.as_ptr() as _ |
| 93 | } |
| 94 | |
| 95 | /// Returns a pointer for the virtual address corresponding to the given non-zero intermediate |
| 96 | /// physical address. |
| 97 | /// |
| 98 | /// Panics if `paddr` is 0. |
| 99 | pub fn phys_to_virt(paddr: usize) -> NonNull<u8> { |
| 100 | NonNull::new(paddr as _).unwrap() |
| 101 | } |