Skip to content
Snippets Groups Projects
Verified Commit c0a96aa7 authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Reduce memory used by x86_64 page tables

parent 0874fabe
No related branches found
No related tags found
No related merge requests found
...@@ -23,13 +23,10 @@ pub unsafe fn paging_create< ...@@ -23,13 +23,10 @@ pub unsafe fn paging_create<
D: Disk, D: Disk,
M: Iterator<Item=OsMemoryEntry>, M: Iterator<Item=OsMemoryEntry>,
V: Iterator<Item=OsVideoMode> V: Iterator<Item=OsVideoMode>
>(os: &mut dyn Os<D, M, V>, kernel_phys: usize) -> Option<usize> { >(os: &mut dyn Os<D, M, V>, kernel_phys: usize, kernel_size: usize) -> Option<usize> {
// Create PML4 // Create PML4
let pml4 = paging_allocate(os)?; let pml4 = paging_allocate(os)?;
// Recursive mapping for compatibility
pml4[511] = pml4.as_ptr() as u64 | 1 << 1 | 1;
{ {
// Create PDP for identity mapping // Create PDP for identity mapping
let pdp = paging_allocate(os)?; let pdp = paging_allocate(os)?;
...@@ -38,20 +35,15 @@ pub unsafe fn paging_create< ...@@ -38,20 +35,15 @@ pub unsafe fn paging_create<
pml4[0] = pdp.as_ptr() as u64 | 1 << 1 | 1; pml4[0] = pdp.as_ptr() as u64 | 1 << 1 | 1;
pml4[256] = pdp.as_ptr() as u64 | 1 << 1 | 1; pml4[256] = pdp.as_ptr() as u64 | 1 << 1 | 1;
// Identity map 8 GiB pages // Identity map 8 GiB using 2 MiB pages
for pdp_i in 0..8 { for pdp_i in 0..8 {
let pd = paging_allocate(os)?; let pd = paging_allocate(os)?;
pdp[pdp_i] = pd.as_ptr() as u64 | 1 << 1 | 1; pdp[pdp_i] = pd.as_ptr() as u64 | 1 << 1 | 1;
for pd_i in 0..pd.len() { for pd_i in 0..pd.len() {
let pt = paging_allocate(os)?; let addr =
pd[pd_i] = pt.as_ptr() as u64 | 1 << 1 | 1; pdp_i as u64 * 0x4000_0000 +
for pt_i in 0..pt.len() { pd_i as u64 * 0x20_0000;
let addr = pd[pd_i] = addr | 1 << 7 | 1 << 1 | 1;
pdp_i as u64 * 0x4000_0000 +
pd_i as u64 * 0x20_0000 +
pt_i as u64 * 0x1000;
pt[pt_i] = addr | 1 << 1 | 1;
}
} }
} }
} }
...@@ -63,23 +55,30 @@ pub unsafe fn paging_create< ...@@ -63,23 +55,30 @@ pub unsafe fn paging_create<
// Link second to last PML4 entry to PDP // Link second to last PML4 entry to PDP
pml4[510] = pdp.as_ptr() as u64 | 1 << 1 | 1; pml4[510] = pdp.as_ptr() as u64 | 1 << 1 | 1;
// Map 1 GiB at kernel offset // Map kernel_size at kernel offset
for pdp_i in 0..1 { let mut kernel_mapped = 0;
let mut pdp_i = 0;
while kernel_mapped < kernel_size && pdp_i < pdp.len() {
let pd = paging_allocate(os)?; let pd = paging_allocate(os)?;
pdp[pdp_i] = pd.as_ptr() as u64 | 1 << 1 | 1; pdp[pdp_i] = pd.as_ptr() as u64 | 1 << 1 | 1;
for pd_i in 0..pd.len() { pdp_i += 1;
let mut pd_i = 0;
while kernel_mapped < kernel_size && pd_i < pd.len(){
let pt = paging_allocate(os)?; let pt = paging_allocate(os)?;
pd[pd_i] = pt.as_ptr() as u64 | 1 << 1 | 1; pd[pd_i] = pt.as_ptr() as u64 | 1 << 1 | 1;
for pt_i in 0..pt.len() { pd_i += 1;
let addr =
pdp_i as u64 * 0x4000_0000 + let mut pt_i = 0;
pd_i as u64 * 0x20_0000 + while kernel_mapped < kernel_size && pt_i < pt.len() {
pt_i as u64 * 0x1000 + let addr = (kernel_phys + kernel_mapped) as u64;
kernel_phys as u64;
pt[pt_i] = addr | 1 << 1 | 1; pt[pt_i] = addr | 1 << 1 | 1;
pt_i += 1;
kernel_mapped += 4096;
} }
} }
} }
assert!(kernel_mapped >= kernel_size);
} }
Some(pml4.as_ptr() as usize) Some(pml4.as_ptr() as usize)
......
...@@ -291,7 +291,7 @@ fn main< ...@@ -291,7 +291,7 @@ fn main<
kernel kernel
}; };
let page_phys = unsafe { paging_create(os, kernel.as_ptr() as usize) } let page_phys = unsafe { paging_create(os, kernel.as_ptr() as usize, kernel.len()) }
.expect("Failed to set up paging"); .expect("Failed to set up paging");
//TODO: properly reserve page table allocations so kernel does not re-use them //TODO: properly reserve page table allocations so kernel does not re-use them
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment