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