blob: 3205a4dbc49d172305e32357fba926ffa168092d [file] [log] [blame]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +00001// 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//! Low-level allocation and tracking of main memory.
16
Andrew Walbran848decf2022-12-15 14:39:38 +000017#![deny(unsafe_op_in_unsafe_fn)]
18
Alice Wang4dd20932023-05-26 13:47:16 +000019use crate::helpers::{self, page_4kb_of, RangeExt, PVMFW_PAGE_SIZE, SIZE_4MB};
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000020use crate::mmu;
Jakob Vukalovicb99905d2023-04-20 15:46:02 +010021use aarch64_paging::paging::{Attributes, Descriptor, MemoryRegion as VaRange};
Andrew Walbran848decf2022-12-15 14:39:38 +000022use alloc::alloc::alloc_zeroed;
23use alloc::alloc::dealloc;
24use alloc::alloc::handle_alloc_error;
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -070025use alloc::boxed::Box;
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +000026use alloc::vec::Vec;
Andrew Walbran87933f32023-05-09 15:29:06 +000027use buddy_system_allocator::{FrameAllocator, LockedFrameAllocator};
Andrew Walbran848decf2022-12-15 14:39:38 +000028use core::alloc::Layout;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000029use core::cmp::max;
30use core::cmp::min;
31use core::fmt;
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +010032use core::iter::once;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000033use core::num::NonZeroUsize;
34use core::ops::Range;
Andrew Walbran848decf2022-12-15 14:39:38 +000035use core::ptr::NonNull;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000036use core::result;
Alice Wang90e6f162023-04-17 13:49:45 +000037use hyp::get_hypervisor;
Pierre-Clément Tosi90238c52023-04-27 17:59:10 +000038use log::trace;
Jakob Vukalovic4c1edbe2023-04-17 19:10:57 +010039use log::{debug, error};
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -070040use once_cell::race::OnceBox;
Jakob Vukalovic85a00d72023-04-20 09:51:10 +010041use spin::mutex::SpinMutex;
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +000042use tinyvec::ArrayVec;
Alice Wang4dd20932023-05-26 13:47:16 +000043use vmbase::{dsb, isb, memory::set_dbm_enabled, tlbi};
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000044
Jiyong Park0ee65392023-03-27 20:52:45 +090045/// Base of the system's contiguous "main" memory.
46pub const BASE_ADDR: usize = 0x8000_0000;
47/// First address that can't be translated by a level 1 TTBR0_EL1.
48pub const MAX_ADDR: usize = 1 << 40;
49
Andrew Walbran0d8b54d2022-12-08 16:32:33 +000050pub type MemoryRange = Range<usize>;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000051
Jakob Vukalovic85a00d72023-04-20 09:51:10 +010052pub static MEMORY: SpinMutex<Option<MemoryTracker>> = SpinMutex::new(None);
53unsafe impl Send for MemoryTracker {}
54
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +010055#[derive(Clone, Copy, Debug, Default, PartialEq)]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000056enum MemoryType {
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +000057 #[default]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000058 ReadOnly,
59 ReadWrite,
60}
61
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +000062#[derive(Clone, Debug, Default)]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000063struct MemoryRegion {
64 range: MemoryRange,
65 mem_type: MemoryType,
66}
67
68impl MemoryRegion {
69 /// True if the instance overlaps with the passed range.
70 pub fn overlaps(&self, range: &MemoryRange) -> bool {
Andrew Walbran19690632022-12-07 16:41:30 +000071 overlaps(&self.range, range)
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000072 }
73
74 /// True if the instance is fully contained within the passed range.
75 pub fn is_within(&self, range: &MemoryRange) -> bool {
Srivatsa Vaddagiric25d68e2023-04-19 22:56:33 -070076 self.as_ref().is_within(range)
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000077 }
78}
79
80impl AsRef<MemoryRange> for MemoryRegion {
81 fn as_ref(&self) -> &MemoryRange {
82 &self.range
83 }
84}
85
Andrew Walbran19690632022-12-07 16:41:30 +000086/// Returns true if one range overlaps with the other at all.
87fn overlaps<T: Copy + Ord>(a: &Range<T>, b: &Range<T>) -> bool {
88 max(a.start, b.start) < min(a.end, b.end)
89}
90
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000091/// Tracks non-overlapping slices of main memory.
92pub struct MemoryTracker {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000093 total: MemoryRange,
94 page_table: mmu::PageTable,
Andrew Walbran19690632022-12-07 16:41:30 +000095 regions: ArrayVec<[MemoryRegion; MemoryTracker::CAPACITY]>,
96 mmio_regions: ArrayVec<[MemoryRange; MemoryTracker::MMIO_CAPACITY]>,
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000097}
98
99/// Errors for MemoryTracker operations.
100#[derive(Debug, Clone)]
101pub enum MemoryTrackerError {
102 /// Tried to modify the memory base address.
103 DifferentBaseAddress,
104 /// Tried to shrink to a larger memory size.
105 SizeTooLarge,
106 /// Tracked regions would not fit in memory size.
107 SizeTooSmall,
108 /// Reached limit number of tracked regions.
109 Full,
110 /// Region is out of the tracked memory address space.
111 OutOfRange,
112 /// New region overlaps with tracked regions.
113 Overlaps,
114 /// Region couldn't be mapped.
115 FailedToMap,
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100116 /// Region couldn't be unmapped.
117 FailedToUnmap,
Alice Wang90e6f162023-04-17 13:49:45 +0000118 /// Error from the interaction with the hypervisor.
119 Hypervisor(hyp::Error),
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000120 /// Failure to set `SHARED_MEMORY`.
121 SharedMemorySetFailure,
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700122 /// Failure to set `SHARED_POOL`.
123 SharedPoolSetFailure,
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100124 /// Invalid page table entry.
125 InvalidPte,
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100126 /// Failed to flush memory region.
127 FlushRegionFailed,
128 /// Failed to set PTE dirty state.
129 SetPteDirtyFailed,
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000130}
131
132impl fmt::Display for MemoryTrackerError {
133 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
134 match self {
135 Self::DifferentBaseAddress => write!(f, "Received different base address"),
136 Self::SizeTooLarge => write!(f, "Tried to shrink to a larger memory size"),
137 Self::SizeTooSmall => write!(f, "Tracked regions would not fit in memory size"),
138 Self::Full => write!(f, "Reached limit number of tracked regions"),
139 Self::OutOfRange => write!(f, "Region is out of the tracked memory address space"),
140 Self::Overlaps => write!(f, "New region overlaps with tracked regions"),
141 Self::FailedToMap => write!(f, "Failed to map the new region"),
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100142 Self::FailedToUnmap => write!(f, "Failed to unmap the new region"),
Alice Wang90e6f162023-04-17 13:49:45 +0000143 Self::Hypervisor(e) => e.fmt(f),
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000144 Self::SharedMemorySetFailure => write!(f, "Failed to set SHARED_MEMORY"),
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700145 Self::SharedPoolSetFailure => write!(f, "Failed to set SHARED_POOL"),
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100146 Self::InvalidPte => write!(f, "Page table entry is not valid"),
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100147 Self::FlushRegionFailed => write!(f, "Failed to flush memory region"),
148 Self::SetPteDirtyFailed => write!(f, "Failed to set PTE dirty state"),
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000149 }
150 }
151}
152
Alice Wang90e6f162023-04-17 13:49:45 +0000153impl From<hyp::Error> for MemoryTrackerError {
154 fn from(e: hyp::Error) -> Self {
155 Self::Hypervisor(e)
Andrew Walbran19690632022-12-07 16:41:30 +0000156 }
157}
158
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000159type Result<T> = result::Result<T, MemoryTrackerError>;
160
Andrew Walbran87933f32023-05-09 15:29:06 +0000161static SHARED_POOL: OnceBox<LockedFrameAllocator<32>> = OnceBox::new();
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000162static SHARED_MEMORY: SpinMutex<Option<MemorySharer>> = SpinMutex::new(None);
163
164/// Allocates memory on the heap and shares it with the host.
165///
166/// Unshares all pages when dropped.
167pub struct MemorySharer {
168 granule: usize,
169 shared_regions: Vec<(usize, Layout)>,
170}
171
172impl MemorySharer {
173 const INIT_CAP: usize = 10;
174
175 pub fn new(granule: usize) -> Self {
176 assert!(granule.is_power_of_two());
177 Self { granule, shared_regions: Vec::with_capacity(Self::INIT_CAP) }
178 }
179
180 /// Get from the global allocator a granule-aligned region that suits `hint` and share it.
Andrew Walbran87933f32023-05-09 15:29:06 +0000181 pub fn refill(&mut self, pool: &mut FrameAllocator<32>, hint: Layout) {
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000182 let layout = hint.align_to(self.granule).unwrap().pad_to_align();
183 assert_ne!(layout.size(), 0);
184 // SAFETY - layout has non-zero size.
185 let Some(shared) = NonNull::new(unsafe { alloc_zeroed(layout) }) else {
186 handle_alloc_error(layout);
187 };
188
189 let base = shared.as_ptr() as usize;
190 let end = base.checked_add(layout.size()).unwrap();
191 trace!("Sharing memory region {:#x?}", base..end);
192 for vaddr in (base..end).step_by(self.granule) {
193 let vaddr = NonNull::new(vaddr as *mut _).unwrap();
194 get_hypervisor().mem_share(virt_to_phys(vaddr).try_into().unwrap()).unwrap();
195 }
196 self.shared_regions.push((base, layout));
197
Andrew Walbran87933f32023-05-09 15:29:06 +0000198 pool.add_frame(base, end);
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000199 }
200}
201
202impl Drop for MemorySharer {
203 fn drop(&mut self) {
204 while let Some((base, layout)) = self.shared_regions.pop() {
205 let end = base.checked_add(layout.size()).unwrap();
206 trace!("Unsharing memory region {:#x?}", base..end);
207 for vaddr in (base..end).step_by(self.granule) {
208 let vaddr = NonNull::new(vaddr as *mut _).unwrap();
209 get_hypervisor().mem_unshare(virt_to_phys(vaddr).try_into().unwrap()).unwrap();
210 }
211
212 // SAFETY - The region was obtained from alloc_zeroed() with the recorded layout.
213 unsafe { dealloc(base as *mut _, layout) };
214 }
215 }
216}
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700217
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000218impl MemoryTracker {
219 const CAPACITY: usize = 5;
Andrew Walbran19690632022-12-07 16:41:30 +0000220 const MMIO_CAPACITY: usize = 5;
Pierre-Clément Tosi164a6f52023-04-18 19:29:11 +0100221 const PVMFW_RANGE: MemoryRange = (BASE_ADDR - SIZE_4MB)..BASE_ADDR;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000222
223 /// Create a new instance from an active page table, covering the maximum RAM size.
Jakob Vukalovic4c1edbe2023-04-17 19:10:57 +0100224 pub fn new(mut page_table: mmu::PageTable) -> Self {
225 // Activate dirty state management first, otherwise we may get permission faults immediately
226 // after activating the new page table. This has no effect before the new page table is
227 // activated because none of the entries in the initial idmap have the DBM flag.
Alice Wang4dd20932023-05-26 13:47:16 +0000228 set_dbm_enabled(true);
Jakob Vukalovic4c1edbe2023-04-17 19:10:57 +0100229
230 debug!("Activating dynamic page table...");
231 // SAFETY - page_table duplicates the static mappings for everything that the Rust code is
232 // aware of so activating it shouldn't have any visible effect.
233 unsafe { page_table.activate() };
234 debug!("... Success!");
235
Andrew Walbran19690632022-12-07 16:41:30 +0000236 Self {
Jiyong Park0ee65392023-03-27 20:52:45 +0900237 total: BASE_ADDR..MAX_ADDR,
Andrew Walbran19690632022-12-07 16:41:30 +0000238 page_table,
239 regions: ArrayVec::new(),
240 mmio_regions: ArrayVec::new(),
241 }
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000242 }
243
244 /// Resize the total RAM size.
245 ///
246 /// This function fails if it contains regions that are not included within the new size.
247 pub fn shrink(&mut self, range: &MemoryRange) -> Result<()> {
248 if range.start != self.total.start {
249 return Err(MemoryTrackerError::DifferentBaseAddress);
250 }
251 if self.total.end < range.end {
252 return Err(MemoryTrackerError::SizeTooLarge);
253 }
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +0000254 if !self.regions.iter().all(|r| r.is_within(range)) {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000255 return Err(MemoryTrackerError::SizeTooSmall);
256 }
257
258 self.total = range.clone();
259 Ok(())
260 }
261
262 /// Allocate the address range for a const slice; returns None if failed.
263 pub fn alloc_range(&mut self, range: &MemoryRange) -> Result<MemoryRange> {
Andrew Walbranda65ab12022-12-07 15:10:13 +0000264 let region = MemoryRegion { range: range.clone(), mem_type: MemoryType::ReadOnly };
265 self.check(&region)?;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000266 self.page_table.map_rodata(range).map_err(|e| {
267 error!("Error during range allocation: {e}");
268 MemoryTrackerError::FailedToMap
269 })?;
Andrew Walbranda65ab12022-12-07 15:10:13 +0000270 self.add(region)
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000271 }
272
273 /// Allocate the address range for a mutable slice; returns None if failed.
274 pub fn alloc_range_mut(&mut self, range: &MemoryRange) -> Result<MemoryRange> {
Andrew Walbranda65ab12022-12-07 15:10:13 +0000275 let region = MemoryRegion { range: range.clone(), mem_type: MemoryType::ReadWrite };
276 self.check(&region)?;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000277 self.page_table.map_data(range).map_err(|e| {
278 error!("Error during mutable range allocation: {e}");
279 MemoryTrackerError::FailedToMap
280 })?;
Andrew Walbranda65ab12022-12-07 15:10:13 +0000281 self.add(region)
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000282 }
283
284 /// Allocate the address range for a const slice; returns None if failed.
285 pub fn alloc(&mut self, base: usize, size: NonZeroUsize) -> Result<MemoryRange> {
286 self.alloc_range(&(base..(base + size.get())))
287 }
288
289 /// Allocate the address range for a mutable slice; returns None if failed.
290 pub fn alloc_mut(&mut self, base: usize, size: NonZeroUsize) -> Result<MemoryRange> {
291 self.alloc_range_mut(&(base..(base + size.get())))
292 }
293
Andrew Walbran19690632022-12-07 16:41:30 +0000294 /// Checks that the given range of addresses is within the MMIO region, and then maps it
295 /// appropriately.
296 pub fn map_mmio_range(&mut self, range: MemoryRange) -> Result<()> {
297 // MMIO space is below the main memory region.
Pierre-Clément Tosi164a6f52023-04-18 19:29:11 +0100298 if range.end > self.total.start || overlaps(&Self::PVMFW_RANGE, &range) {
Andrew Walbran19690632022-12-07 16:41:30 +0000299 return Err(MemoryTrackerError::OutOfRange);
300 }
301 if self.mmio_regions.iter().any(|r| overlaps(r, &range)) {
302 return Err(MemoryTrackerError::Overlaps);
303 }
304 if self.mmio_regions.len() == self.mmio_regions.capacity() {
305 return Err(MemoryTrackerError::Full);
306 }
307
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100308 self.page_table.map_device_lazy(&range).map_err(|e| {
Andrew Walbran19690632022-12-07 16:41:30 +0000309 error!("Error during MMIO device mapping: {e}");
310 MemoryTrackerError::FailedToMap
311 })?;
312
Andrew Walbran19690632022-12-07 16:41:30 +0000313 if self.mmio_regions.try_push(range).is_some() {
314 return Err(MemoryTrackerError::Full);
315 }
316
317 Ok(())
318 }
319
Andrew Walbranda65ab12022-12-07 15:10:13 +0000320 /// Checks that the given region is within the range of the `MemoryTracker` and doesn't overlap
321 /// with any other previously allocated regions, and that the regions ArrayVec has capacity to
322 /// add it.
323 fn check(&self, region: &MemoryRegion) -> Result<()> {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000324 if !region.is_within(&self.total) {
325 return Err(MemoryTrackerError::OutOfRange);
326 }
Andrew Walbranda65ab12022-12-07 15:10:13 +0000327 if self.regions.iter().any(|r| r.overlaps(&region.range)) {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000328 return Err(MemoryTrackerError::Overlaps);
329 }
Andrew Walbranda65ab12022-12-07 15:10:13 +0000330 if self.regions.len() == self.regions.capacity() {
331 return Err(MemoryTrackerError::Full);
332 }
333 Ok(())
334 }
335
336 fn add(&mut self, region: MemoryRegion) -> Result<MemoryRange> {
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +0000337 if self.regions.try_push(region).is_some() {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000338 return Err(MemoryTrackerError::Full);
339 }
340
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +0000341 Ok(self.regions.last().unwrap().as_ref().clone())
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000342 }
Andrew Walbran19690632022-12-07 16:41:30 +0000343
344 /// Unmaps all tracked MMIO regions from the MMIO guard.
345 ///
346 /// Note that they are not unmapped from the page table.
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100347 pub fn mmio_unmap_all(&mut self) -> Result<()> {
348 for range in &self.mmio_regions {
349 self.page_table
350 .modify_range(range, &mmio_guard_unmap_page)
351 .map_err(|_| MemoryTrackerError::FailedToUnmap)?;
Andrew Walbran19690632022-12-07 16:41:30 +0000352 }
Andrew Walbran19690632022-12-07 16:41:30 +0000353 Ok(())
354 }
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700355
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000356 /// Initialize the shared heap to dynamically share memory from the global allocator.
357 pub fn init_dynamic_shared_pool(&mut self) -> Result<()> {
358 let granule = get_hypervisor().memory_protection_granule()?;
359 let previous = SHARED_MEMORY.lock().replace(MemorySharer::new(granule));
360 if previous.is_some() {
361 return Err(MemoryTrackerError::SharedMemorySetFailure);
362 }
363
364 SHARED_POOL
Andrew Walbran87933f32023-05-09 15:29:06 +0000365 .set(Box::new(LockedFrameAllocator::new()))
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000366 .map_err(|_| MemoryTrackerError::SharedPoolSetFailure)?;
367
368 Ok(())
369 }
370
371 /// Initialize the shared heap from a static region of memory.
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700372 ///
373 /// Some hypervisors such as Gunyah do not support a MemShare API for guest
374 /// to share its memory with host. Instead they allow host to designate part
375 /// of guest memory as "shared" ahead of guest starting its execution. The
376 /// shared memory region is indicated in swiotlb node. On such platforms use
377 /// a separate heap to allocate buffers that can be shared with host.
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000378 pub fn init_static_shared_pool(&mut self, range: Range<usize>) -> Result<()> {
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700379 let size = NonZeroUsize::new(range.len()).unwrap();
380 let range = self.alloc_mut(range.start, size)?;
Andrew Walbran87933f32023-05-09 15:29:06 +0000381 let shared_pool = LockedFrameAllocator::<32>::new();
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700382
Andrew Walbran87933f32023-05-09 15:29:06 +0000383 shared_pool.lock().insert(range);
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700384
385 SHARED_POOL
386 .set(Box::new(shared_pool))
387 .map_err(|_| MemoryTrackerError::SharedPoolSetFailure)?;
388
389 Ok(())
390 }
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000391
392 /// Unshares any memory that may have been shared.
393 pub fn unshare_all_memory(&mut self) {
394 drop(SHARED_MEMORY.lock().take());
395 }
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100396
397 /// Handles translation fault for blocks flagged for lazy MMIO mapping by enabling the page
398 /// table entry and MMIO guard mapping the block. Breaks apart a block entry if required.
399 pub fn handle_mmio_fault(&mut self, addr: usize) -> Result<()> {
400 let page_range = page_4kb_of(addr)..page_4kb_of(addr) + PVMFW_PAGE_SIZE;
401 self.page_table
402 .modify_range(&page_range, &verify_lazy_mapped_block)
403 .map_err(|_| MemoryTrackerError::InvalidPte)?;
404 get_hypervisor().mmio_guard_map(page_range.start)?;
405 // Maps a single device page, breaking up block mappings if necessary.
406 self.page_table.map_device(&page_range).map_err(|_| MemoryTrackerError::FailedToMap)
407 }
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100408
409 /// Flush all memory regions marked as writable-dirty.
410 fn flush_dirty_pages(&mut self) -> Result<()> {
411 // Collect memory ranges for which dirty state is tracked.
412 let writable_regions =
413 self.regions.iter().filter(|r| r.mem_type == MemoryType::ReadWrite).map(|r| &r.range);
414 let payload_range = mmu::PageTable::appended_payload_range();
415 // Execute a barrier instruction to ensure all hardware updates to the page table have been
416 // observed before reading PTE flags to determine dirty state.
417 dsb!("ish");
418 // Now flush writable-dirty pages in those regions.
419 for range in writable_regions.chain(once(&payload_range)) {
420 self.page_table
421 .modify_range(range, &flush_dirty_range)
422 .map_err(|_| MemoryTrackerError::FlushRegionFailed)?;
423 }
424 Ok(())
425 }
426
427 /// Handles permission fault for read-only blocks by setting writable-dirty state.
428 /// In general, this should be called from the exception handler when hardware dirty
429 /// state management is disabled or unavailable.
430 pub fn handle_permission_fault(&mut self, addr: usize) -> Result<()> {
431 self.page_table
432 .modify_range(&(addr..addr + 1), &mark_dirty_block)
433 .map_err(|_| MemoryTrackerError::SetPteDirtyFailed)
434 }
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000435}
436
437impl Drop for MemoryTracker {
438 fn drop(&mut self) {
Alice Wang4dd20932023-05-26 13:47:16 +0000439 set_dbm_enabled(false);
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100440 self.flush_dirty_pages().unwrap();
Jakob Vukalovic4c1edbe2023-04-17 19:10:57 +0100441 self.unshare_all_memory();
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000442 }
443}
Andrew Walbran19690632022-12-07 16:41:30 +0000444
Andrew Walbran2b0c7fb2023-05-09 12:16:20 +0000445/// Allocates a memory range of at least the given size and alignment that is shared with the host.
446/// Returns a pointer to the buffer.
Pierre-Clément Tosi2d5bc582023-05-03 11:23:11 +0000447pub fn alloc_shared(layout: Layout) -> hyp::Result<NonNull<u8>> {
448 assert_ne!(layout.size(), 0);
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000449 let Some(buffer) = try_shared_alloc(layout) else {
Andrew Walbran848decf2022-12-15 14:39:38 +0000450 handle_alloc_error(layout);
451 };
452
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000453 trace!("Allocated shared buffer at {buffer:?} with {layout:?}");
Andrew Walbran848decf2022-12-15 14:39:38 +0000454 Ok(buffer)
455}
456
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000457fn try_shared_alloc(layout: Layout) -> Option<NonNull<u8>> {
458 let mut shared_pool = SHARED_POOL.get().unwrap().lock();
459
Andrew Walbran87933f32023-05-09 15:29:06 +0000460 if let Some(buffer) = shared_pool.alloc_aligned(layout) {
461 Some(NonNull::new(buffer as _).unwrap())
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000462 } else if let Some(shared_memory) = SHARED_MEMORY.lock().as_mut() {
463 shared_memory.refill(&mut shared_pool, layout);
Andrew Walbran87933f32023-05-09 15:29:06 +0000464 shared_pool.alloc_aligned(layout).map(|buffer| NonNull::new(buffer as _).unwrap())
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000465 } else {
466 None
467 }
468}
469
Andrew Walbran848decf2022-12-15 14:39:38 +0000470/// Unshares and deallocates a memory range which was previously allocated by `alloc_shared`.
471///
Andrew Walbran2b0c7fb2023-05-09 12:16:20 +0000472/// The layout passed in must be the same layout passed to the original `alloc_shared` call.
Andrew Walbran848decf2022-12-15 14:39:38 +0000473///
474/// # Safety
475///
Andrew Walbran2b0c7fb2023-05-09 12:16:20 +0000476/// The memory must have been allocated by `alloc_shared` with the same layout, and not yet
Andrew Walbran848decf2022-12-15 14:39:38 +0000477/// deallocated.
Pierre-Clément Tosi2d5bc582023-05-03 11:23:11 +0000478pub unsafe fn dealloc_shared(vaddr: NonNull<u8>, layout: Layout) -> hyp::Result<()> {
Andrew Walbran87933f32023-05-09 15:29:06 +0000479 SHARED_POOL.get().unwrap().lock().dealloc_aligned(vaddr.as_ptr() as usize, layout);
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700480
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000481 trace!("Deallocated shared buffer at {vaddr:?} with {layout:?}");
Andrew Walbran848decf2022-12-15 14:39:38 +0000482 Ok(())
483}
484
Andrew Walbran848decf2022-12-15 14:39:38 +0000485/// Returns the intermediate physical address corresponding to the given virtual address.
486///
Andrew Walbran272bd7a2023-01-24 14:02:36 +0000487/// As we use identity mapping for everything, this is just a cast, but it's useful to use it to be
488/// explicit about where we are converting from virtual to physical address.
489pub fn virt_to_phys(vaddr: NonNull<u8>) -> usize {
490 vaddr.as_ptr() as _
491}
492
493/// Returns a pointer for the virtual address corresponding to the given non-zero intermediate
494/// physical address.
495///
496/// Panics if `paddr` is 0.
497pub fn phys_to_virt(paddr: usize) -> NonNull<u8> {
498 NonNull::new(paddr as _).unwrap()
Andrew Walbran848decf2022-12-15 14:39:38 +0000499}
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100500
501/// Checks whether a PTE at given level is a page or block descriptor.
502#[inline]
503fn is_leaf_pte(flags: &Attributes, level: usize) -> bool {
504 const LEAF_PTE_LEVEL: usize = 3;
505 if flags.contains(Attributes::TABLE_OR_PAGE) {
506 level == LEAF_PTE_LEVEL
507 } else {
508 level < LEAF_PTE_LEVEL
509 }
510}
511
512/// Checks whether block flags indicate it should be MMIO guard mapped.
513fn verify_lazy_mapped_block(
514 _range: &VaRange,
515 desc: &mut Descriptor,
516 level: usize,
517) -> result::Result<(), ()> {
518 let flags = desc.flags().expect("Unsupported PTE flags set");
519 if !is_leaf_pte(&flags, level) {
520 return Ok(()); // Skip table PTEs as they aren't tagged with MMIO_LAZY_MAP_FLAG.
521 }
522 if flags.contains(mmu::MMIO_LAZY_MAP_FLAG) && !flags.contains(Attributes::VALID) {
523 Ok(())
524 } else {
525 Err(())
526 }
527}
528
529/// MMIO guard unmaps page
530fn mmio_guard_unmap_page(
531 va_range: &VaRange,
532 desc: &mut Descriptor,
533 level: usize,
534) -> result::Result<(), ()> {
535 let flags = desc.flags().expect("Unsupported PTE flags set");
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100536 if !is_leaf_pte(&flags, level) {
537 return Ok(());
538 }
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100539 // This function will be called on an address range that corresponds to a device. Only if a
540 // page has been accessed (written to or read from), will it contain the VALID flag and be MMIO
541 // guard mapped. Therefore, we can skip unmapping invalid pages, they were never MMIO guard
542 // mapped anyway.
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100543 if flags.contains(Attributes::VALID) {
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100544 assert!(
545 flags.contains(mmu::MMIO_LAZY_MAP_FLAG),
546 "Attempting MMIO guard unmap for non-device pages"
547 );
548 assert_eq!(
549 va_range.len(),
550 PVMFW_PAGE_SIZE,
551 "Failed to break down block mapping before MMIO guard mapping"
552 );
553 let page_base = va_range.start().0;
554 assert_eq!(page_base % PVMFW_PAGE_SIZE, 0);
555 // Since mmio_guard_map takes IPAs, if pvmfw moves non-ID address mapping, page_base
556 // should be converted to IPA. However, since 0x0 is a valid MMIO address, we don't use
557 // virt_to_phys here, and just pass page_base instead.
558 get_hypervisor().mmio_guard_unmap(page_base).map_err(|e| {
559 error!("Error MMIO guard unmapping: {e}");
560 })?;
561 }
562 Ok(())
563}
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100564
565/// Flushes a memory range the descriptor refers to, if the descriptor is in writable-dirty state.
566fn flush_dirty_range(
567 va_range: &VaRange,
568 desc: &mut Descriptor,
569 level: usize,
570) -> result::Result<(), ()> {
571 // Only flush ranges corresponding to dirty leaf PTEs.
572 let flags = desc.flags().ok_or(())?;
573 if !is_leaf_pte(&flags, level) {
574 return Ok(());
575 }
576 if !flags.contains(Attributes::READ_ONLY) {
577 helpers::flush_region(va_range.start().0, va_range.len());
578 }
579 Ok(())
580}
581
582/// Clears read-only flag on a PTE, making it writable-dirty. Used when dirty state is managed
583/// in software to handle permission faults on read-only descriptors.
584fn mark_dirty_block(
585 va_range: &VaRange,
586 desc: &mut Descriptor,
587 level: usize,
588) -> result::Result<(), ()> {
589 let flags = desc.flags().ok_or(())?;
590 if !is_leaf_pte(&flags, level) {
591 return Ok(());
592 }
593 if flags.contains(Attributes::DBM) {
594 assert!(flags.contains(Attributes::READ_ONLY), "unexpected PTE writable state");
595 desc.modify_flags(Attributes::empty(), Attributes::READ_ONLY);
596 // Updating the read-only bit of a PTE requires TLB invalidation.
597 // A TLB maintenance instruction is only guaranteed to be complete after a DSB instruction.
598 // An ISB instruction is required to ensure the effects of completed TLB maintenance
599 // instructions are visible to instructions fetched afterwards.
600 // See ARM ARM E2.3.10, and G5.9.
601 tlbi!("vale1", mmu::PageTable::ASID, va_range.start().0);
602 dsb!("ish");
603 isb!();
604 Ok(())
605 } else {
606 Err(())
607 }
608}