Refactor create_table_entry
All checks were successful
Build / build (push) Successful in 2m44s

This commit is contained in:
Mathieu Strypsteen 2024-12-27 11:45:58 +01:00
parent f930bb2fd0
commit bb7c9b6bfa

View file

@ -94,9 +94,12 @@ impl AddressSpace {
let directory_i = virt_page / 512 % 512; let directory_i = virt_page / 512 % 512;
let pdpt_i = virt_page / 512 / 512 % 512; let pdpt_i = virt_page / 512 / 512 % 512;
let pml4_i = virt_page / 512 / 512 / 512 % 512; let pml4_i = virt_page / 512 / 512 / 512 % 512;
let pdpt = get_table_entry(self.pml4.get_mut().unwrap(), pml4_i); self.create_table_entry(pml4_i as i32, -1, -1, true, true);
let directory = get_table_entry(pdpt, pdpt_i); self.create_table_entry(pml4_i as i32, pdpt_i as i32, -1, true, true);
let table = get_table_entry(directory, directory_i); self.create_table_entry(pml4_i as i32, pdpt_i as i32, directory_i as i32, true, true);
let table = self.pml4.get_mut().unwrap().entries_virt[pml4_i].as_mut().unwrap().entries_virt[pdpt_i].as_mut().unwrap().entries_virt[directory_i]
.as_mut()
.unwrap();
let should_invalidate = table.entries_phys[table_i].present() == 1; let should_invalidate = table.entries_phys[table_i].present() == 1;
if !only_flags { if !only_flags {
table.entries_phys[table_i].set_address(phys / 0x1000); table.entries_phys[table_i].set_address(phys / 0x1000);
@ -173,6 +176,30 @@ impl AddressSpace {
load_cr3(virt_to_phys(pml4_address)); load_cr3(virt_to_phys(pml4_address));
} }
} }
fn create_table_entry(&mut self, pml4_i: i32, pdpt_i: i32, directory_i: i32, user: bool, exec: bool) {
let mut table = self.pml4.get_mut().unwrap();
let mut i = pml4_i as usize;
if pdpt_i != -1 {
table = table.entries_virt[i].as_mut().unwrap();
i = pdpt_i as usize;
}
if directory_i != -1 {
table = table.entries_virt[i].as_mut().unwrap();
i = directory_i as usize;
}
if table.entries_virt[i].is_none() {
let new_table = Box::new(PageTable {
entries_phys: [PageEntry(0); 512],
entries_virt: [const { None }; 512],
});
table.entries_phys[i].set_address(virt_to_phys(new_table.as_ref() as *const PageTable as u64) / 0x1000);
table.entries_virt[i] = Some(new_table);
table.entries_phys[i].set_write(1);
table.entries_phys[i].set_user(user as u64);
table.entries_phys[i].set_execute_disable(!exec as u64);
table.entries_phys[i].set_present(1);
}
}
} }
fn get_free_frame() -> u64 { fn get_free_frame() -> u64 {
@ -200,20 +227,6 @@ pub fn virt_to_phys(virt: u64) -> u64 {
let heap_map = HEAP_PHYS_MAPPING.lock(); let heap_map = HEAP_PHYS_MAPPING.lock();
heap_map[(virt as usize - KERNEL_HEAP_START as usize) / 0x1000] + virt % 0x1000 heap_map[(virt as usize - KERNEL_HEAP_START as usize) / 0x1000] + virt % 0x1000
} }
fn get_table_entry(table: &mut PageTable, i: usize) -> &mut PageTable {
if table.entries_virt[i].is_none() {
let new_table = Box::new(PageTable {
entries_phys: [PageEntry(0); 512],
entries_virt: [const { None }; 512],
});
table.entries_phys[i].set_address(virt_to_phys(new_table.as_ref() as *const PageTable as u64) / 0x1000);
table.entries_virt[i] = Some(new_table);
table.entries_phys[i].set_write(1);
table.entries_phys[i].set_user(1);
table.entries_phys[i].set_present(1);
}
table.entries_virt[i].as_mut().unwrap()
}
pub fn setup_paging(loader_struct: &LoaderStruct, phys_start: u64, heap_start: u64) { pub fn setup_paging(loader_struct: &LoaderStruct, phys_start: u64, heap_start: u64) {
HEAP_PHYS_START.store(heap_start, Ordering::SeqCst); HEAP_PHYS_START.store(heap_start, Ordering::SeqCst);
let mut memory_size = 0; let mut memory_size = 0;
@ -264,12 +277,11 @@ pub fn setup_paging(loader_struct: &LoaderStruct, phys_start: u64, heap_start: u
entries_virt: [const { None }; 512], entries_virt: [const { None }; 512],
})) }))
.unwrap_or_else(|_| panic!()); .unwrap_or_else(|_| panic!());
let pml4 = address_space.pml4.get_mut().unwrap();
for i in 256..512 { for i in 256..512 {
get_table_entry(pml4, i);
pml4.entries_phys[i].set_user(0);
if i != 511 { if i != 511 {
pml4.entries_phys[i].set_execute_disable(1); address_space.create_table_entry(i as i32, -1, -1, false, false);
} else {
address_space.create_table_entry(i as i32, -1, -1, false, true);
} }
} }
for i in text_start..text_end { for i in text_start..text_end {