Skip to content

Commit 4784783

Browse files
committed
Separate vm debug code, tidy some memory fns
Signed-off-by: Graham MacDonald <[email protected]>
1 parent b24c58b commit 4784783

File tree

4 files changed

+281
-263
lines changed

4 files changed

+281
-263
lines changed

aarch64/src/kmem.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ unsafe extern "C" {
1818
}
1919

2020
fn base_addr() -> usize {
21-
0xffff_8000_0000_0000
21+
KZERO
2222
}
2323

2424
fn eboottext_addr() -> usize {
@@ -93,25 +93,23 @@ pub fn total_kernel_range() -> PhysRange {
9393
PhysRange(from_virt_to_physaddr(base_addr())..from_virt_to_physaddr(end_addr()))
9494
}
9595

96-
// TODO Meh, this is only valid if it's been mapped as an offset - should probably remove
97-
pub const fn physaddr_as_virt(pa: PhysAddr) -> usize {
98-
(pa.addr() as usize).wrapping_add(KZERO)
96+
/// Transform the physical address to a virtual address, under the assumption that
97+
/// the virtual address is the physical address offset from KZERO.
98+
pub const fn physaddr_as_ptr_mut_offset_from_kzero<T>(pa: PhysAddr) -> *mut T {
99+
(pa.addr() as usize).wrapping_add(KZERO) as *mut T
99100
}
100101

101-
// TODO remove?
102-
pub const fn physaddr_as_ptr_mut<T>(pa: PhysAddr) -> *mut T {
103-
physaddr_as_virt(pa) as *mut T
104-
}
105-
106-
// TODO remove?
107-
pub const fn from_virt_to_physaddr(va: usize) -> PhysAddr {
102+
/// Given a virtual address, return the physical address. Makes a massive assumption
103+
/// that the code is mapped offset to KZERO, so should be used with extreme care.
104+
pub fn from_virt_to_physaddr(va: usize) -> PhysAddr {
105+
debug_assert!(va >= KZERO, "from_virt_to_physaddr: va {} must be >= KZERO ({})", va, KZERO);
108106
PhysAddr::new((va - KZERO) as u64)
109107
}
110108

111109
/// Given an address, return the physical address. Makes a massive assumption
112110
/// that the code is mapped offset to KZERO, so should be used with extreme care.
113111
pub fn from_ptr_to_physaddr_offset_from_kzero<T>(a: *const T) -> PhysAddr {
114-
PhysAddr::new((a.addr() - KZERO) as u64)
112+
from_virt_to_physaddr(a.addr())
115113
}
116114

117115
pub fn early_pages_range() -> PhysRange {

aarch64/src/main.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod trap;
1919
mod uartmini;
2020
mod uartpl011;
2121
mod vm;
22+
mod vmdebug;
2223

2324
extern crate alloc;
2425

@@ -139,8 +140,8 @@ pub extern "C" fn main9(dtb_va: usize) {
139140

140141
print_memory_info();
141142

142-
vm::print_recursive_tables(RootPageTableType::Kernel);
143-
vm::print_recursive_tables(RootPageTableType::User);
143+
vmdebug::print_recursive_tables(RootPageTableType::Kernel);
144+
vmdebug::print_recursive_tables(RootPageTableType::User);
144145

145146
{
146147
let page_table = unsafe { &mut *ptr::addr_of_mut!(KERNEL_PAGETABLE) };
@@ -163,8 +164,8 @@ pub extern "C" fn main9(dtb_va: usize) {
163164
}
164165
}
165166

166-
vm::print_recursive_tables(RootPageTableType::Kernel);
167-
vm::print_recursive_tables(RootPageTableType::User);
167+
vmdebug::print_recursive_tables(RootPageTableType::Kernel);
168+
vmdebug::print_recursive_tables(RootPageTableType::User);
168169

169170
println!("Now try user space");
170171

@@ -189,8 +190,8 @@ pub extern "C" fn main9(dtb_va: usize) {
189190
}
190191
}
191192

192-
vm::print_recursive_tables(RootPageTableType::Kernel);
193-
vm::print_recursive_tables(RootPageTableType::User);
193+
vmdebug::print_recursive_tables(RootPageTableType::Kernel);
194+
vmdebug::print_recursive_tables(RootPageTableType::User);
194195

195196
let _b = Box::new("ddododo");
196197

0 commit comments

Comments
 (0)