[wip] add to-kernel-space/from-kernel-space address conversion

This commit is contained in:
Berkus Decker 2021-02-26 00:16:42 +02:00
parent de91603059
commit e215f9d62c
4 changed files with 27 additions and 6 deletions

View File

@ -10,3 +10,8 @@ mod virt_addr;
pub use asid::*;
pub use phys_addr::*;
pub use virt_addr::*;
// @todo Check largest VA supported, calculate physical_memory_offset
// @todo Keep in mind amount of physical memory present, the following
// @todo will only work for 1Gb board:
pub const PHYSICAL_MEMORY_OFFSET: u64 = 0xffff_8000_0000_0000; // Last 1GiB of VA space

View File

@ -4,7 +4,10 @@
*/
use {
crate::mm::{align_down, align_up},
crate::{
memory::VirtAddr,
mm::{align_down, align_up},
},
bit_field::BitField,
core::{
convert::{From, Into},
@ -97,6 +100,13 @@ impl PhysAddr {
{
self.aligned_down(align) == self
}
/// Convert physical memory address into a kernel virtual address.
pub fn user_to_kernel(&self) -> VirtAddr {
use super::PHYSICAL_MEMORY_OFFSET;
assert!(self.0 < !PHYSICAL_MEMORY_OFFSET); // Can't have phys address over 1GiB then
VirtAddr::new(self.0 + PHYSICAL_MEMORY_OFFSET)
}
}
impl fmt::Debug for PhysAddr {

View File

@ -4,7 +4,10 @@
*/
use {
crate::mm::{align_down, align_up},
crate::{
memory::PhysAddr,
mm::{align_down, align_up},
},
bit_field::BitField,
core::{
convert::{From, Into, TryInto},
@ -158,6 +161,13 @@ impl VirtAddr {
pub fn l0_index(&self) -> u9 {
u9::new(((self.0 >> 12 >> 9 >> 9 >> 9) & 0o777).try_into().unwrap())
}
/// Convert kernel-space virtual address into a physical memory address.
pub fn kernel_to_user(&self) -> PhysAddr {
use super::PHYSICAL_MEMORY_OFFSET;
assert!(self.0 > PHYSICAL_MEMORY_OFFSET);
PhysAddr::new(self.0 - PHYSICAL_MEMORY_OFFSET)
}
}
impl fmt::Debug for VirtAddr {

View File

@ -1,7 +1,3 @@
// Check largest VA supported, calculate physical_memory_offset
//
const PHYSICAL_MEMORY_OFFSET: u64 = 0xffff_8000_0000_0000; // Last 1GiB of VA space
// AArch64:
// Table D4-8-2021: check supported granule sizes, select alloc policy based on results.
// TTBR_ELx is the pdbr for specific page tables