blob: c97ed994d160562329c18d9fd90972358ed36408 [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 Tosiad1fc752023-05-31 16:56:56 +000020use crate::mmu::{PageTable, MMIO_LAZY_MAP_FLAG};
21use aarch64_paging::idmap::IdMap;
Jakob Vukalovicb99905d2023-04-20 15:46:02 +010022use aarch64_paging::paging::{Attributes, Descriptor, MemoryRegion as VaRange};
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +000023use aarch64_paging::MapError;
Andrew Walbran848decf2022-12-15 14:39:38 +000024use alloc::alloc::alloc_zeroed;
25use alloc::alloc::dealloc;
26use alloc::alloc::handle_alloc_error;
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -070027use alloc::boxed::Box;
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +000028use alloc::vec::Vec;
Andrew Walbran87933f32023-05-09 15:29:06 +000029use buddy_system_allocator::{FrameAllocator, LockedFrameAllocator};
Andrew Walbran848decf2022-12-15 14:39:38 +000030use core::alloc::Layout;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000031use core::cmp::max;
32use core::cmp::min;
33use core::fmt;
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +010034use core::iter::once;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000035use core::num::NonZeroUsize;
36use core::ops::Range;
Andrew Walbran848decf2022-12-15 14:39:38 +000037use core::ptr::NonNull;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000038use core::result;
Alice Wang90e6f162023-04-17 13:49:45 +000039use hyp::get_hypervisor;
Pierre-Clément Tosi90238c52023-04-27 17:59:10 +000040use log::trace;
Jakob Vukalovic4c1edbe2023-04-17 19:10:57 +010041use log::{debug, error};
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -070042use once_cell::race::OnceBox;
Jakob Vukalovic85a00d72023-04-20 09:51:10 +010043use spin::mutex::SpinMutex;
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +000044use tinyvec::ArrayVec;
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +000045use vmbase::{dsb, isb, layout, memory::set_dbm_enabled, tlbi};
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000046
Jiyong Park0ee65392023-03-27 20:52:45 +090047/// Base of the system's contiguous "main" memory.
48pub const BASE_ADDR: usize = 0x8000_0000;
49/// First address that can't be translated by a level 1 TTBR0_EL1.
50pub const MAX_ADDR: usize = 1 << 40;
51
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +000052const PT_ROOT_LEVEL: usize = 1;
53const PT_ASID: usize = 1;
54
Andrew Walbran0d8b54d2022-12-08 16:32:33 +000055pub type MemoryRange = Range<usize>;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000056
Jakob Vukalovic85a00d72023-04-20 09:51:10 +010057pub static MEMORY: SpinMutex<Option<MemoryTracker>> = SpinMutex::new(None);
58unsafe impl Send for MemoryTracker {}
59
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +010060#[derive(Clone, Copy, Debug, Default, PartialEq)]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000061enum MemoryType {
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +000062 #[default]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000063 ReadOnly,
64 ReadWrite,
65}
66
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +000067#[derive(Clone, Debug, Default)]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000068struct MemoryRegion {
69 range: MemoryRange,
70 mem_type: MemoryType,
71}
72
73impl MemoryRegion {
74 /// True if the instance overlaps with the passed range.
75 pub fn overlaps(&self, range: &MemoryRange) -> bool {
Andrew Walbran19690632022-12-07 16:41:30 +000076 overlaps(&self.range, range)
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000077 }
78
79 /// True if the instance is fully contained within the passed range.
80 pub fn is_within(&self, range: &MemoryRange) -> bool {
Srivatsa Vaddagiric25d68e2023-04-19 22:56:33 -070081 self.as_ref().is_within(range)
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000082 }
83}
84
85impl AsRef<MemoryRange> for MemoryRegion {
86 fn as_ref(&self) -> &MemoryRange {
87 &self.range
88 }
89}
90
Andrew Walbran19690632022-12-07 16:41:30 +000091/// Returns true if one range overlaps with the other at all.
92fn overlaps<T: Copy + Ord>(a: &Range<T>, b: &Range<T>) -> bool {
93 max(a.start, b.start) < min(a.end, b.end)
94}
95
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000096/// Tracks non-overlapping slices of main memory.
97pub struct MemoryTracker {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000098 total: MemoryRange,
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +000099 page_table: PageTable,
Andrew Walbran19690632022-12-07 16:41:30 +0000100 regions: ArrayVec<[MemoryRegion; MemoryTracker::CAPACITY]>,
101 mmio_regions: ArrayVec<[MemoryRange; MemoryTracker::MMIO_CAPACITY]>,
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000102}
103
104/// Errors for MemoryTracker operations.
105#[derive(Debug, Clone)]
106pub enum MemoryTrackerError {
107 /// Tried to modify the memory base address.
108 DifferentBaseAddress,
109 /// Tried to shrink to a larger memory size.
110 SizeTooLarge,
111 /// Tracked regions would not fit in memory size.
112 SizeTooSmall,
113 /// Reached limit number of tracked regions.
114 Full,
115 /// Region is out of the tracked memory address space.
116 OutOfRange,
117 /// New region overlaps with tracked regions.
118 Overlaps,
119 /// Region couldn't be mapped.
120 FailedToMap,
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100121 /// Region couldn't be unmapped.
122 FailedToUnmap,
Alice Wang90e6f162023-04-17 13:49:45 +0000123 /// Error from the interaction with the hypervisor.
124 Hypervisor(hyp::Error),
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000125 /// Failure to set `SHARED_MEMORY`.
126 SharedMemorySetFailure,
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700127 /// Failure to set `SHARED_POOL`.
128 SharedPoolSetFailure,
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100129 /// Invalid page table entry.
130 InvalidPte,
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100131 /// Failed to flush memory region.
132 FlushRegionFailed,
133 /// Failed to set PTE dirty state.
134 SetPteDirtyFailed,
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000135}
136
137impl fmt::Display for MemoryTrackerError {
138 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139 match self {
140 Self::DifferentBaseAddress => write!(f, "Received different base address"),
141 Self::SizeTooLarge => write!(f, "Tried to shrink to a larger memory size"),
142 Self::SizeTooSmall => write!(f, "Tracked regions would not fit in memory size"),
143 Self::Full => write!(f, "Reached limit number of tracked regions"),
144 Self::OutOfRange => write!(f, "Region is out of the tracked memory address space"),
145 Self::Overlaps => write!(f, "New region overlaps with tracked regions"),
146 Self::FailedToMap => write!(f, "Failed to map the new region"),
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100147 Self::FailedToUnmap => write!(f, "Failed to unmap the new region"),
Alice Wang90e6f162023-04-17 13:49:45 +0000148 Self::Hypervisor(e) => e.fmt(f),
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000149 Self::SharedMemorySetFailure => write!(f, "Failed to set SHARED_MEMORY"),
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700150 Self::SharedPoolSetFailure => write!(f, "Failed to set SHARED_POOL"),
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100151 Self::InvalidPte => write!(f, "Page table entry is not valid"),
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100152 Self::FlushRegionFailed => write!(f, "Failed to flush memory region"),
153 Self::SetPteDirtyFailed => write!(f, "Failed to set PTE dirty state"),
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000154 }
155 }
156}
157
Alice Wang90e6f162023-04-17 13:49:45 +0000158impl From<hyp::Error> for MemoryTrackerError {
159 fn from(e: hyp::Error) -> Self {
160 Self::Hypervisor(e)
Andrew Walbran19690632022-12-07 16:41:30 +0000161 }
162}
163
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000164type Result<T> = result::Result<T, MemoryTrackerError>;
165
Andrew Walbran87933f32023-05-09 15:29:06 +0000166static SHARED_POOL: OnceBox<LockedFrameAllocator<32>> = OnceBox::new();
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000167static SHARED_MEMORY: SpinMutex<Option<MemorySharer>> = SpinMutex::new(None);
168
169/// Allocates memory on the heap and shares it with the host.
170///
171/// Unshares all pages when dropped.
172pub struct MemorySharer {
173 granule: usize,
174 shared_regions: Vec<(usize, Layout)>,
175}
176
177impl MemorySharer {
178 const INIT_CAP: usize = 10;
179
180 pub fn new(granule: usize) -> Self {
181 assert!(granule.is_power_of_two());
182 Self { granule, shared_regions: Vec::with_capacity(Self::INIT_CAP) }
183 }
184
185 /// Get from the global allocator a granule-aligned region that suits `hint` and share it.
Andrew Walbran87933f32023-05-09 15:29:06 +0000186 pub fn refill(&mut self, pool: &mut FrameAllocator<32>, hint: Layout) {
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000187 let layout = hint.align_to(self.granule).unwrap().pad_to_align();
188 assert_ne!(layout.size(), 0);
189 // SAFETY - layout has non-zero size.
190 let Some(shared) = NonNull::new(unsafe { alloc_zeroed(layout) }) else {
191 handle_alloc_error(layout);
192 };
193
194 let base = shared.as_ptr() as usize;
195 let end = base.checked_add(layout.size()).unwrap();
196 trace!("Sharing memory region {:#x?}", base..end);
197 for vaddr in (base..end).step_by(self.granule) {
198 let vaddr = NonNull::new(vaddr as *mut _).unwrap();
199 get_hypervisor().mem_share(virt_to_phys(vaddr).try_into().unwrap()).unwrap();
200 }
201 self.shared_regions.push((base, layout));
202
Andrew Walbran87933f32023-05-09 15:29:06 +0000203 pool.add_frame(base, end);
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000204 }
205}
206
207impl Drop for MemorySharer {
208 fn drop(&mut self) {
209 while let Some((base, layout)) = self.shared_regions.pop() {
210 let end = base.checked_add(layout.size()).unwrap();
211 trace!("Unsharing memory region {:#x?}", base..end);
212 for vaddr in (base..end).step_by(self.granule) {
213 let vaddr = NonNull::new(vaddr as *mut _).unwrap();
214 get_hypervisor().mem_unshare(virt_to_phys(vaddr).try_into().unwrap()).unwrap();
215 }
216
217 // SAFETY - The region was obtained from alloc_zeroed() with the recorded layout.
218 unsafe { dealloc(base as *mut _, layout) };
219 }
220 }
221}
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700222
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000223impl MemoryTracker {
224 const CAPACITY: usize = 5;
Andrew Walbran19690632022-12-07 16:41:30 +0000225 const MMIO_CAPACITY: usize = 5;
Pierre-Clément Tosi164a6f52023-04-18 19:29:11 +0100226 const PVMFW_RANGE: MemoryRange = (BASE_ADDR - SIZE_4MB)..BASE_ADDR;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000227
228 /// Create a new instance from an active page table, covering the maximum RAM size.
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +0000229 pub fn new(mut page_table: PageTable) -> Self {
Jakob Vukalovic4c1edbe2023-04-17 19:10:57 +0100230 // Activate dirty state management first, otherwise we may get permission faults immediately
231 // after activating the new page table. This has no effect before the new page table is
232 // activated because none of the entries in the initial idmap have the DBM flag.
Alice Wang4dd20932023-05-26 13:47:16 +0000233 set_dbm_enabled(true);
Jakob Vukalovic4c1edbe2023-04-17 19:10:57 +0100234
235 debug!("Activating dynamic page table...");
236 // SAFETY - page_table duplicates the static mappings for everything that the Rust code is
237 // aware of so activating it shouldn't have any visible effect.
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +0000238 unsafe { page_table.activate() }
Jakob Vukalovic4c1edbe2023-04-17 19:10:57 +0100239 debug!("... Success!");
240
Andrew Walbran19690632022-12-07 16:41:30 +0000241 Self {
Jiyong Park0ee65392023-03-27 20:52:45 +0900242 total: BASE_ADDR..MAX_ADDR,
Andrew Walbran19690632022-12-07 16:41:30 +0000243 page_table,
244 regions: ArrayVec::new(),
245 mmio_regions: ArrayVec::new(),
246 }
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000247 }
248
249 /// Resize the total RAM size.
250 ///
251 /// This function fails if it contains regions that are not included within the new size.
252 pub fn shrink(&mut self, range: &MemoryRange) -> Result<()> {
253 if range.start != self.total.start {
254 return Err(MemoryTrackerError::DifferentBaseAddress);
255 }
256 if self.total.end < range.end {
257 return Err(MemoryTrackerError::SizeTooLarge);
258 }
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +0000259 if !self.regions.iter().all(|r| r.is_within(range)) {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000260 return Err(MemoryTrackerError::SizeTooSmall);
261 }
262
263 self.total = range.clone();
264 Ok(())
265 }
266
267 /// Allocate the address range for a const slice; returns None if failed.
268 pub fn alloc_range(&mut self, range: &MemoryRange) -> Result<MemoryRange> {
Andrew Walbranda65ab12022-12-07 15:10:13 +0000269 let region = MemoryRegion { range: range.clone(), mem_type: MemoryType::ReadOnly };
270 self.check(&region)?;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000271 self.page_table.map_rodata(range).map_err(|e| {
272 error!("Error during range allocation: {e}");
273 MemoryTrackerError::FailedToMap
274 })?;
Andrew Walbranda65ab12022-12-07 15:10:13 +0000275 self.add(region)
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000276 }
277
278 /// Allocate the address range for a mutable slice; returns None if failed.
279 pub fn alloc_range_mut(&mut self, range: &MemoryRange) -> Result<MemoryRange> {
Andrew Walbranda65ab12022-12-07 15:10:13 +0000280 let region = MemoryRegion { range: range.clone(), mem_type: MemoryType::ReadWrite };
281 self.check(&region)?;
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +0000282 self.page_table.map_data_dbm(range).map_err(|e| {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000283 error!("Error during mutable range allocation: {e}");
284 MemoryTrackerError::FailedToMap
285 })?;
Andrew Walbranda65ab12022-12-07 15:10:13 +0000286 self.add(region)
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000287 }
288
289 /// Allocate the address range for a const slice; returns None if failed.
290 pub fn alloc(&mut self, base: usize, size: NonZeroUsize) -> Result<MemoryRange> {
291 self.alloc_range(&(base..(base + size.get())))
292 }
293
294 /// Allocate the address range for a mutable slice; returns None if failed.
295 pub fn alloc_mut(&mut self, base: usize, size: NonZeroUsize) -> Result<MemoryRange> {
296 self.alloc_range_mut(&(base..(base + size.get())))
297 }
298
Andrew Walbran19690632022-12-07 16:41:30 +0000299 /// Checks that the given range of addresses is within the MMIO region, and then maps it
300 /// appropriately.
301 pub fn map_mmio_range(&mut self, range: MemoryRange) -> Result<()> {
302 // MMIO space is below the main memory region.
Pierre-Clément Tosi164a6f52023-04-18 19:29:11 +0100303 if range.end > self.total.start || overlaps(&Self::PVMFW_RANGE, &range) {
Andrew Walbran19690632022-12-07 16:41:30 +0000304 return Err(MemoryTrackerError::OutOfRange);
305 }
306 if self.mmio_regions.iter().any(|r| overlaps(r, &range)) {
307 return Err(MemoryTrackerError::Overlaps);
308 }
309 if self.mmio_regions.len() == self.mmio_regions.capacity() {
310 return Err(MemoryTrackerError::Full);
311 }
312
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100313 self.page_table.map_device_lazy(&range).map_err(|e| {
Andrew Walbran19690632022-12-07 16:41:30 +0000314 error!("Error during MMIO device mapping: {e}");
315 MemoryTrackerError::FailedToMap
316 })?;
317
Andrew Walbran19690632022-12-07 16:41:30 +0000318 if self.mmio_regions.try_push(range).is_some() {
319 return Err(MemoryTrackerError::Full);
320 }
321
322 Ok(())
323 }
324
Andrew Walbranda65ab12022-12-07 15:10:13 +0000325 /// Checks that the given region is within the range of the `MemoryTracker` and doesn't overlap
326 /// with any other previously allocated regions, and that the regions ArrayVec has capacity to
327 /// add it.
328 fn check(&self, region: &MemoryRegion) -> Result<()> {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000329 if !region.is_within(&self.total) {
330 return Err(MemoryTrackerError::OutOfRange);
331 }
Andrew Walbranda65ab12022-12-07 15:10:13 +0000332 if self.regions.iter().any(|r| r.overlaps(&region.range)) {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000333 return Err(MemoryTrackerError::Overlaps);
334 }
Andrew Walbranda65ab12022-12-07 15:10:13 +0000335 if self.regions.len() == self.regions.capacity() {
336 return Err(MemoryTrackerError::Full);
337 }
338 Ok(())
339 }
340
341 fn add(&mut self, region: MemoryRegion) -> Result<MemoryRange> {
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +0000342 if self.regions.try_push(region).is_some() {
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000343 return Err(MemoryTrackerError::Full);
344 }
345
Pierre-Clément Tosi328dfb62022-11-25 18:20:42 +0000346 Ok(self.regions.last().unwrap().as_ref().clone())
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000347 }
Andrew Walbran19690632022-12-07 16:41:30 +0000348
349 /// Unmaps all tracked MMIO regions from the MMIO guard.
350 ///
351 /// Note that they are not unmapped from the page table.
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100352 pub fn mmio_unmap_all(&mut self) -> Result<()> {
353 for range in &self.mmio_regions {
354 self.page_table
355 .modify_range(range, &mmio_guard_unmap_page)
356 .map_err(|_| MemoryTrackerError::FailedToUnmap)?;
Andrew Walbran19690632022-12-07 16:41:30 +0000357 }
Andrew Walbran19690632022-12-07 16:41:30 +0000358 Ok(())
359 }
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700360
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000361 /// Initialize the shared heap to dynamically share memory from the global allocator.
362 pub fn init_dynamic_shared_pool(&mut self) -> Result<()> {
363 let granule = get_hypervisor().memory_protection_granule()?;
364 let previous = SHARED_MEMORY.lock().replace(MemorySharer::new(granule));
365 if previous.is_some() {
366 return Err(MemoryTrackerError::SharedMemorySetFailure);
367 }
368
369 SHARED_POOL
Andrew Walbran87933f32023-05-09 15:29:06 +0000370 .set(Box::new(LockedFrameAllocator::new()))
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000371 .map_err(|_| MemoryTrackerError::SharedPoolSetFailure)?;
372
373 Ok(())
374 }
375
376 /// Initialize the shared heap from a static region of memory.
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700377 ///
378 /// Some hypervisors such as Gunyah do not support a MemShare API for guest
379 /// to share its memory with host. Instead they allow host to designate part
380 /// of guest memory as "shared" ahead of guest starting its execution. The
381 /// shared memory region is indicated in swiotlb node. On such platforms use
382 /// a separate heap to allocate buffers that can be shared with host.
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000383 pub fn init_static_shared_pool(&mut self, range: Range<usize>) -> Result<()> {
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700384 let size = NonZeroUsize::new(range.len()).unwrap();
385 let range = self.alloc_mut(range.start, size)?;
Andrew Walbran87933f32023-05-09 15:29:06 +0000386 let shared_pool = LockedFrameAllocator::<32>::new();
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700387
Andrew Walbran87933f32023-05-09 15:29:06 +0000388 shared_pool.lock().insert(range);
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700389
390 SHARED_POOL
391 .set(Box::new(shared_pool))
392 .map_err(|_| MemoryTrackerError::SharedPoolSetFailure)?;
393
394 Ok(())
395 }
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000396
397 /// Unshares any memory that may have been shared.
398 pub fn unshare_all_memory(&mut self) {
399 drop(SHARED_MEMORY.lock().take());
400 }
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100401
402 /// Handles translation fault for blocks flagged for lazy MMIO mapping by enabling the page
403 /// table entry and MMIO guard mapping the block. Breaks apart a block entry if required.
404 pub fn handle_mmio_fault(&mut self, addr: usize) -> Result<()> {
405 let page_range = page_4kb_of(addr)..page_4kb_of(addr) + PVMFW_PAGE_SIZE;
406 self.page_table
407 .modify_range(&page_range, &verify_lazy_mapped_block)
408 .map_err(|_| MemoryTrackerError::InvalidPte)?;
409 get_hypervisor().mmio_guard_map(page_range.start)?;
410 // Maps a single device page, breaking up block mappings if necessary.
411 self.page_table.map_device(&page_range).map_err(|_| MemoryTrackerError::FailedToMap)
412 }
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100413
414 /// Flush all memory regions marked as writable-dirty.
415 fn flush_dirty_pages(&mut self) -> Result<()> {
416 // Collect memory ranges for which dirty state is tracked.
417 let writable_regions =
418 self.regions.iter().filter(|r| r.mem_type == MemoryType::ReadWrite).map(|r| &r.range);
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +0000419 let payload_range = appended_payload_range();
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100420 // Execute a barrier instruction to ensure all hardware updates to the page table have been
421 // observed before reading PTE flags to determine dirty state.
422 dsb!("ish");
423 // Now flush writable-dirty pages in those regions.
424 for range in writable_regions.chain(once(&payload_range)) {
425 self.page_table
426 .modify_range(range, &flush_dirty_range)
427 .map_err(|_| MemoryTrackerError::FlushRegionFailed)?;
428 }
429 Ok(())
430 }
431
432 /// Handles permission fault for read-only blocks by setting writable-dirty state.
433 /// In general, this should be called from the exception handler when hardware dirty
434 /// state management is disabled or unavailable.
435 pub fn handle_permission_fault(&mut self, addr: usize) -> Result<()> {
436 self.page_table
437 .modify_range(&(addr..addr + 1), &mark_dirty_block)
438 .map_err(|_| MemoryTrackerError::SetPteDirtyFailed)
439 }
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000440}
441
442impl Drop for MemoryTracker {
443 fn drop(&mut self) {
Alice Wang4dd20932023-05-26 13:47:16 +0000444 set_dbm_enabled(false);
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100445 self.flush_dirty_pages().unwrap();
Jakob Vukalovic4c1edbe2023-04-17 19:10:57 +0100446 self.unshare_all_memory();
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000447 }
448}
Andrew Walbran19690632022-12-07 16:41:30 +0000449
Andrew Walbran2b0c7fb2023-05-09 12:16:20 +0000450/// Allocates a memory range of at least the given size and alignment that is shared with the host.
451/// Returns a pointer to the buffer.
Pierre-Clément Tosi2d5bc582023-05-03 11:23:11 +0000452pub fn alloc_shared(layout: Layout) -> hyp::Result<NonNull<u8>> {
453 assert_ne!(layout.size(), 0);
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000454 let Some(buffer) = try_shared_alloc(layout) else {
Andrew Walbran848decf2022-12-15 14:39:38 +0000455 handle_alloc_error(layout);
456 };
457
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000458 trace!("Allocated shared buffer at {buffer:?} with {layout:?}");
Andrew Walbran848decf2022-12-15 14:39:38 +0000459 Ok(buffer)
460}
461
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000462fn try_shared_alloc(layout: Layout) -> Option<NonNull<u8>> {
463 let mut shared_pool = SHARED_POOL.get().unwrap().lock();
464
Andrew Walbran87933f32023-05-09 15:29:06 +0000465 if let Some(buffer) = shared_pool.alloc_aligned(layout) {
466 Some(NonNull::new(buffer as _).unwrap())
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000467 } else if let Some(shared_memory) = SHARED_MEMORY.lock().as_mut() {
468 shared_memory.refill(&mut shared_pool, layout);
Andrew Walbran87933f32023-05-09 15:29:06 +0000469 shared_pool.alloc_aligned(layout).map(|buffer| NonNull::new(buffer as _).unwrap())
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000470 } else {
471 None
472 }
473}
474
Andrew Walbran848decf2022-12-15 14:39:38 +0000475/// Unshares and deallocates a memory range which was previously allocated by `alloc_shared`.
476///
Andrew Walbran2b0c7fb2023-05-09 12:16:20 +0000477/// The layout passed in must be the same layout passed to the original `alloc_shared` call.
Andrew Walbran848decf2022-12-15 14:39:38 +0000478///
479/// # Safety
480///
Andrew Walbran2b0c7fb2023-05-09 12:16:20 +0000481/// The memory must have been allocated by `alloc_shared` with the same layout, and not yet
Andrew Walbran848decf2022-12-15 14:39:38 +0000482/// deallocated.
Pierre-Clément Tosi2d5bc582023-05-03 11:23:11 +0000483pub unsafe fn dealloc_shared(vaddr: NonNull<u8>, layout: Layout) -> hyp::Result<()> {
Andrew Walbran87933f32023-05-09 15:29:06 +0000484 SHARED_POOL.get().unwrap().lock().dealloc_aligned(vaddr.as_ptr() as usize, layout);
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -0700485
Pierre-Clément Tosif19c0e62023-05-02 13:56:58 +0000486 trace!("Deallocated shared buffer at {vaddr:?} with {layout:?}");
Andrew Walbran848decf2022-12-15 14:39:38 +0000487 Ok(())
488}
489
Andrew Walbran848decf2022-12-15 14:39:38 +0000490/// Returns the intermediate physical address corresponding to the given virtual address.
491///
Andrew Walbran272bd7a2023-01-24 14:02:36 +0000492/// As we use identity mapping for everything, this is just a cast, but it's useful to use it to be
493/// explicit about where we are converting from virtual to physical address.
494pub fn virt_to_phys(vaddr: NonNull<u8>) -> usize {
495 vaddr.as_ptr() as _
496}
497
498/// Returns a pointer for the virtual address corresponding to the given non-zero intermediate
499/// physical address.
500///
501/// Panics if `paddr` is 0.
502pub fn phys_to_virt(paddr: usize) -> NonNull<u8> {
503 NonNull::new(paddr as _).unwrap()
Andrew Walbran848decf2022-12-15 14:39:38 +0000504}
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100505
506/// Checks whether a PTE at given level is a page or block descriptor.
507#[inline]
508fn is_leaf_pte(flags: &Attributes, level: usize) -> bool {
509 const LEAF_PTE_LEVEL: usize = 3;
510 if flags.contains(Attributes::TABLE_OR_PAGE) {
511 level == LEAF_PTE_LEVEL
512 } else {
513 level < LEAF_PTE_LEVEL
514 }
515}
516
517/// Checks whether block flags indicate it should be MMIO guard mapped.
518fn verify_lazy_mapped_block(
519 _range: &VaRange,
520 desc: &mut Descriptor,
521 level: usize,
522) -> result::Result<(), ()> {
523 let flags = desc.flags().expect("Unsupported PTE flags set");
524 if !is_leaf_pte(&flags, level) {
525 return Ok(()); // Skip table PTEs as they aren't tagged with MMIO_LAZY_MAP_FLAG.
526 }
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +0000527 if flags.contains(MMIO_LAZY_MAP_FLAG) && !flags.contains(Attributes::VALID) {
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100528 Ok(())
529 } else {
530 Err(())
531 }
532}
533
534/// MMIO guard unmaps page
535fn mmio_guard_unmap_page(
536 va_range: &VaRange,
537 desc: &mut Descriptor,
538 level: usize,
539) -> result::Result<(), ()> {
540 let flags = desc.flags().expect("Unsupported PTE flags set");
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100541 if !is_leaf_pte(&flags, level) {
542 return Ok(());
543 }
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100544 // This function will be called on an address range that corresponds to a device. Only if a
545 // page has been accessed (written to or read from), will it contain the VALID flag and be MMIO
546 // guard mapped. Therefore, we can skip unmapping invalid pages, they were never MMIO guard
547 // mapped anyway.
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100548 if flags.contains(Attributes::VALID) {
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100549 assert!(
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +0000550 flags.contains(MMIO_LAZY_MAP_FLAG),
Jakob Vukalovicb99905d2023-04-20 15:46:02 +0100551 "Attempting MMIO guard unmap for non-device pages"
552 );
553 assert_eq!(
554 va_range.len(),
555 PVMFW_PAGE_SIZE,
556 "Failed to break down block mapping before MMIO guard mapping"
557 );
558 let page_base = va_range.start().0;
559 assert_eq!(page_base % PVMFW_PAGE_SIZE, 0);
560 // Since mmio_guard_map takes IPAs, if pvmfw moves non-ID address mapping, page_base
561 // should be converted to IPA. However, since 0x0 is a valid MMIO address, we don't use
562 // virt_to_phys here, and just pass page_base instead.
563 get_hypervisor().mmio_guard_unmap(page_base).map_err(|e| {
564 error!("Error MMIO guard unmapping: {e}");
565 })?;
566 }
567 Ok(())
568}
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100569
570/// Flushes a memory range the descriptor refers to, if the descriptor is in writable-dirty state.
571fn flush_dirty_range(
572 va_range: &VaRange,
573 desc: &mut Descriptor,
574 level: usize,
575) -> result::Result<(), ()> {
576 // Only flush ranges corresponding to dirty leaf PTEs.
577 let flags = desc.flags().ok_or(())?;
578 if !is_leaf_pte(&flags, level) {
579 return Ok(());
580 }
581 if !flags.contains(Attributes::READ_ONLY) {
582 helpers::flush_region(va_range.start().0, va_range.len());
583 }
584 Ok(())
585}
586
587/// Clears read-only flag on a PTE, making it writable-dirty. Used when dirty state is managed
588/// in software to handle permission faults on read-only descriptors.
589fn mark_dirty_block(
590 va_range: &VaRange,
591 desc: &mut Descriptor,
592 level: usize,
593) -> result::Result<(), ()> {
594 let flags = desc.flags().ok_or(())?;
595 if !is_leaf_pte(&flags, level) {
596 return Ok(());
597 }
598 if flags.contains(Attributes::DBM) {
599 assert!(flags.contains(Attributes::READ_ONLY), "unexpected PTE writable state");
600 desc.modify_flags(Attributes::empty(), Attributes::READ_ONLY);
601 // Updating the read-only bit of a PTE requires TLB invalidation.
602 // A TLB maintenance instruction is only guaranteed to be complete after a DSB instruction.
603 // An ISB instruction is required to ensure the effects of completed TLB maintenance
604 // instructions are visible to instructions fetched afterwards.
605 // See ARM ARM E2.3.10, and G5.9.
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +0000606 tlbi!("vale1", PT_ASID, va_range.start().0);
Jakob Vukalovic44b1ce32023-04-17 19:10:10 +0100607 dsb!("ish");
608 isb!();
609 Ok(())
610 } else {
611 Err(())
612 }
613}
Pierre-Clément Tosiad1fc752023-05-31 16:56:56 +0000614
615/// Returns memory range reserved for the appended payload.
616pub fn appended_payload_range() -> Range<usize> {
617 let start = helpers::align_up(layout::binary_end(), helpers::SIZE_4KB).unwrap();
618 // pvmfw is contained in a 2MiB region so the payload can't be larger than the 2MiB alignment.
619 let end = helpers::align_up(start, helpers::SIZE_2MB).unwrap();
620 start..end
621}
622
623/// Region allocated for the stack.
624pub fn stack_range() -> Range<usize> {
625 const STACK_PAGES: usize = 8;
626
627 layout::stack_range(STACK_PAGES * PVMFW_PAGE_SIZE)
628}
629
630pub fn init_page_table() -> result::Result<PageTable, MapError> {
631 let mut page_table: PageTable = IdMap::new(PT_ASID, PT_ROOT_LEVEL).into();
632
633 // Stack and scratch ranges are explicitly zeroed and flushed before jumping to payload,
634 // so dirty state management can be omitted.
635 page_table.map_data(&layout::scratch_range())?;
636 page_table.map_data(&stack_range())?;
637 page_table.map_code(&layout::text_range())?;
638 page_table.map_rodata(&layout::rodata_range())?;
639 page_table.map_data_dbm(&appended_payload_range())?;
640
641 Ok(page_table)
642}