Skip to content

Commit

Permalink
Merge pull request #264 from Freax13/fix-large-phys-mem-mapping-2
Browse files Browse the repository at this point in the history
allow allocating more than one level 4 entry (0.9)
  • Loading branch information
phil-opp authored Sep 25, 2022
2 parents 3ed1e84 + 2cbf551 commit 866984b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
14 changes: 9 additions & 5 deletions src/level4_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ impl UsedLevel4Entries {
used
}

pub fn get_free_entry(&mut self) -> PageTableIndex {
let (idx, entry) = self
pub fn get_free_entries(&mut self, num: u64) -> PageTableIndex {
let idx = self
.entry_state
.iter_mut()
.windows(num as usize)
.enumerate()
.find(|(_, &mut entry)| entry == false)
.find(|(_, entries)| entries.iter().all(|&entry| entry == false))
.map(|(idx, _)| idx)
.expect("no usable level 4 entries found");

*entry = true;
for i in 0..num as usize {
self.entry_state[idx + i] = true;
}

PageTableIndex::new(idx.try_into().unwrap())
}
}
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ fn bootloader_main(
enable_nxe_bit();

// Create a recursive page table entry
let recursive_index = PageTableIndex::new(level4_entries.get_free_entry().try_into().unwrap());
let recursive_index =
PageTableIndex::new(level4_entries.get_free_entries(1).try_into().unwrap());
let mut entry = PageTableEntry::new();
entry.set_addr(
p4_physical,
Expand Down Expand Up @@ -243,7 +244,7 @@ fn bootloader_main(
let page: Page = match BOOT_INFO_ADDRESS {
Some(addr) => Page::containing_address(VirtAddr::new(addr)),
None => Page::from_page_table_indices(
level4_entries.get_free_entry(),
level4_entries.get_free_entries(1),
PageTableIndex::new(0),
PageTableIndex::new(0),
PageTableIndex::new(0),
Expand Down Expand Up @@ -286,11 +287,10 @@ fn bootloader_main(

let physical_memory_offset = if cfg!(feature = "map_physical_memory") {
let physical_memory_offset = PHYSICAL_MEMORY_OFFSET.unwrap_or_else(|| {
// If offset not manually provided, find a free p4 entry and map memory here.
// One level 4 entry spans 2^48/512 bytes (over 500gib) so this should suffice.
assert!(max_phys_addr < (1 << 48) / 512);
const LEVEL_4_SIZE: u64 = 4096 * 512 * 512 * 512;
let level_4_entries = (max_phys_addr + (LEVEL_4_SIZE - 1)) / LEVEL_4_SIZE;
Page::from_page_table_indices_1gib(
level4_entries.get_free_entry(),
level4_entries.get_free_entries(level_4_entries),
PageTableIndex::new(0),
)
.start_address()
Expand Down

0 comments on commit 866984b

Please sign in to comment.