You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
7.5 KiB
Rust
234 lines
7.5 KiB
Rust
4 years ago
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
|
//
|
||
3 years ago
|
// Copyright (c) 2020-2022 Andre Richter <andre.o.richter@gmail.com>
|
||
4 years ago
|
|
||
|
//! A record of mapped pages.
|
||
|
|
||
|
use super::{
|
||
3 years ago
|
AccessPermissions, Address, AttributeFields, MMIODescriptor, MemAttributes, MemoryRegion,
|
||
|
Physical, Virtual,
|
||
4 years ago
|
};
|
||
3 years ago
|
use crate::{bsp, info, synchronization, synchronization::InitStateLock, warn};
|
||
4 years ago
|
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
// Private Definitions
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
|
||
|
/// Type describing a virtual memory mapping.
|
||
|
#[allow(missing_docs)]
|
||
|
#[derive(Copy, Clone)]
|
||
|
struct MappingRecordEntry {
|
||
|
pub users: [Option<&'static str>; 5],
|
||
3 years ago
|
pub phys_start_addr: Address<Physical>,
|
||
4 years ago
|
pub virt_start_addr: Address<Virtual>,
|
||
3 years ago
|
pub num_pages: usize,
|
||
4 years ago
|
pub attribute_fields: AttributeFields,
|
||
|
}
|
||
|
|
||
|
struct MappingRecord {
|
||
|
inner: [Option<MappingRecordEntry>; 12],
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
// Global instances
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
|
||
|
static KERNEL_MAPPING_RECORD: InitStateLock<MappingRecord> =
|
||
|
InitStateLock::new(MappingRecord::new());
|
||
|
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
// Private Code
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
|
||
|
impl MappingRecordEntry {
|
||
|
pub fn new(
|
||
|
name: &'static str,
|
||
3 years ago
|
virt_region: &MemoryRegion<Virtual>,
|
||
|
phys_region: &MemoryRegion<Physical>,
|
||
4 years ago
|
attr: &AttributeFields,
|
||
|
) -> Self {
|
||
|
Self {
|
||
|
users: [Some(name), None, None, None, None],
|
||
3 years ago
|
phys_start_addr: phys_region.start_addr(),
|
||
|
virt_start_addr: virt_region.start_addr(),
|
||
|
num_pages: phys_region.num_pages(),
|
||
4 years ago
|
attribute_fields: *attr,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn find_next_free_user(&mut self) -> Result<&mut Option<&'static str>, &'static str> {
|
||
|
if let Some(x) = self.users.iter_mut().find(|x| x.is_none()) {
|
||
|
return Ok(x);
|
||
|
};
|
||
|
|
||
|
Err("Storage for user info exhausted")
|
||
|
}
|
||
|
|
||
|
pub fn add_user(&mut self, user: &'static str) -> Result<(), &'static str> {
|
||
|
let x = self.find_next_free_user()?;
|
||
|
*x = Some(user);
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl MappingRecord {
|
||
|
pub const fn new() -> Self {
|
||
|
Self { inner: [None; 12] }
|
||
|
}
|
||
|
|
||
|
fn find_next_free(&mut self) -> Result<&mut Option<MappingRecordEntry>, &'static str> {
|
||
|
if let Some(x) = self.inner.iter_mut().find(|x| x.is_none()) {
|
||
|
return Ok(x);
|
||
|
}
|
||
|
|
||
|
Err("Storage for mapping info exhausted")
|
||
|
}
|
||
|
|
||
|
fn find_duplicate(
|
||
|
&mut self,
|
||
3 years ago
|
phys_region: &MemoryRegion<Physical>,
|
||
4 years ago
|
) -> Option<&mut MappingRecordEntry> {
|
||
|
self.inner
|
||
|
.iter_mut()
|
||
|
.filter(|x| x.is_some())
|
||
|
.map(|x| x.as_mut().unwrap())
|
||
|
.filter(|x| x.attribute_fields.mem_attributes == MemAttributes::Device)
|
||
3 years ago
|
.find(|x| {
|
||
|
if x.phys_start_addr != phys_region.start_addr() {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if x.num_pages != phys_region.num_pages() {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
true
|
||
|
})
|
||
4 years ago
|
}
|
||
|
|
||
|
pub fn add(
|
||
|
&mut self,
|
||
|
name: &'static str,
|
||
3 years ago
|
virt_region: &MemoryRegion<Virtual>,
|
||
|
phys_region: &MemoryRegion<Physical>,
|
||
4 years ago
|
attr: &AttributeFields,
|
||
|
) -> Result<(), &'static str> {
|
||
|
let x = self.find_next_free()?;
|
||
|
|
||
3 years ago
|
*x = Some(MappingRecordEntry::new(
|
||
|
name,
|
||
|
virt_region,
|
||
|
phys_region,
|
||
|
attr,
|
||
|
));
|
||
4 years ago
|
Ok(())
|
||
|
}
|
||
|
|
||
|
pub fn print(&self) {
|
||
|
const KIB_RSHIFT: u32 = 10; // log2(1024).
|
||
|
const MIB_RSHIFT: u32 = 20; // log2(1024 * 1024).
|
||
|
|
||
4 years ago
|
info!(" -------------------------------------------------------------------------------------------------------------------------------------------");
|
||
4 years ago
|
info!(
|
||
4 years ago
|
" {:^44} {:^30} {:^7} {:^9} {:^35}",
|
||
4 years ago
|
"Virtual", "Physical", "Size", "Attr", "Entity"
|
||
|
);
|
||
4 years ago
|
info!(" -------------------------------------------------------------------------------------------------------------------------------------------");
|
||
4 years ago
|
|
||
3 years ago
|
for i in self.inner.iter().flatten() {
|
||
3 years ago
|
let size = i.num_pages * bsp::memory::mmu::KernelGranule::SIZE;
|
||
4 years ago
|
let virt_start = i.virt_start_addr;
|
||
3 years ago
|
let virt_end_inclusive = virt_start + (size - 1);
|
||
|
let phys_start = i.phys_start_addr;
|
||
|
let phys_end_inclusive = phys_start + (size - 1);
|
||
4 years ago
|
|
||
|
let (size, unit) = if (size >> MIB_RSHIFT) > 0 {
|
||
|
(size >> MIB_RSHIFT, "MiB")
|
||
|
} else if (size >> KIB_RSHIFT) > 0 {
|
||
|
(size >> KIB_RSHIFT, "KiB")
|
||
|
} else {
|
||
|
(size, "Byte")
|
||
|
};
|
||
|
|
||
|
let attr = match i.attribute_fields.mem_attributes {
|
||
|
MemAttributes::CacheableDRAM => "C",
|
||
|
MemAttributes::Device => "Dev",
|
||
|
};
|
||
|
|
||
|
let acc_p = match i.attribute_fields.acc_perms {
|
||
|
AccessPermissions::ReadOnly => "RO",
|
||
|
AccessPermissions::ReadWrite => "RW",
|
||
|
};
|
||
|
|
||
|
let xn = if i.attribute_fields.execute_never {
|
||
|
"XN"
|
||
|
} else {
|
||
|
"X"
|
||
|
};
|
||
|
|
||
|
info!(
|
||
4 years ago
|
" {}..{} --> {}..{} | \
|
||
4 years ago
|
{: >3} {} | {: <3} {} {: <2} | {}",
|
||
|
virt_start,
|
||
|
virt_end_inclusive,
|
||
|
phys_start,
|
||
|
phys_end_inclusive,
|
||
|
size,
|
||
|
unit,
|
||
|
attr,
|
||
|
acc_p,
|
||
|
xn,
|
||
|
i.users[0].unwrap()
|
||
|
);
|
||
|
|
||
|
for k in i.users[1..].iter() {
|
||
|
if let Some(additional_user) = *k {
|
||
|
info!(
|
||
4 years ago
|
" | {}",
|
||
4 years ago
|
additional_user
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
info!(" -------------------------------------------------------------------------------------------------------------------------------------------");
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
// Public Code
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
use synchronization::interface::ReadWriteEx;
|
||
|
|
||
|
/// Add an entry to the mapping info record.
|
||
|
pub fn kernel_add(
|
||
|
name: &'static str,
|
||
3 years ago
|
virt_region: &MemoryRegion<Virtual>,
|
||
|
phys_region: &MemoryRegion<Physical>,
|
||
4 years ago
|
attr: &AttributeFields,
|
||
|
) -> Result<(), &'static str> {
|
||
3 years ago
|
KERNEL_MAPPING_RECORD.write(|mr| mr.add(name, virt_region, phys_region, attr))
|
||
4 years ago
|
}
|
||
|
|
||
|
pub fn kernel_find_and_insert_mmio_duplicate(
|
||
4 years ago
|
mmio_descriptor: &MMIODescriptor,
|
||
4 years ago
|
new_user: &'static str,
|
||
|
) -> Option<Address<Virtual>> {
|
||
3 years ago
|
let phys_region: MemoryRegion<Physical> = (*mmio_descriptor).into();
|
||
4 years ago
|
|
||
4 years ago
|
KERNEL_MAPPING_RECORD.write(|mr| {
|
||
3 years ago
|
let dup = mr.find_duplicate(&phys_region)?;
|
||
4 years ago
|
|
||
|
if let Err(x) = dup.add_user(new_user) {
|
||
|
warn!("{}", x);
|
||
|
}
|
||
|
|
||
|
Some(dup.virt_start_addr)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/// Human-readable print of all recorded kernel mappings.
|
||
|
pub fn kernel_print() {
|
||
4 years ago
|
KERNEL_MAPPING_RECORD.read(|mr| mr.print());
|
||
4 years ago
|
}
|