Skip to content

Commit

Permalink
Merge pull request #831 from stlankes/talc
Browse files Browse the repository at this point in the history
fix initialization of the memory subsystem
  • Loading branch information
stlankes authored Aug 9, 2023
2 parents 226a473 + 43dca19 commit 1dc6c02
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
5 changes: 4 additions & 1 deletion src/arch/x86_64/mm/physicalmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ fn detect_from_multiboot_info() -> Result<(), ()> {
start_address.as_usize(),
(m.base_address() + m.length()) as usize,
);
let _ = TOTAL_MEMORY.fetch_add((m.base_address() + m.length()) as usize, Ordering::SeqCst);
let _ = TOTAL_MEMORY.fetch_add(
(m.base_address() + m.length() - start_address.as_u64()) as usize,
Ordering::SeqCst,
);
PHYSICAL_FREE_LIST.lock().list.push_back(entry);
}

Expand Down
2 changes: 2 additions & 0 deletions src/mm/freelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use core::cmp::Ordering;

use align_address::Align;

#[derive(Debug)]
pub struct FreeListEntry {
pub start: usize,
pub end: usize,
Expand All @@ -15,6 +16,7 @@ impl FreeListEntry {
}
}

#[derive(Debug)]
pub struct FreeList {
pub list: LinkedList<FreeListEntry>,
}
Expand Down
50 changes: 17 additions & 33 deletions src/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,50 +140,34 @@ pub(crate) fn init() {

let virt_size: usize =
(available_memory - stack_reserve).align_down(LargePageSize::SIZE as usize);

let virt_addr = if has_1gib_pages && virt_size > HugePageSize::SIZE as usize {
arch::mm::virtualmem::allocate_aligned(
virt_size.align_up(HugePageSize::SIZE as usize),
HugePageSize::SIZE as usize,
)
.unwrap()
} else {
arch::mm::virtualmem::allocate_aligned(virt_size, LargePageSize::SIZE as usize).unwrap()
};
let virt_addr =
arch::mm::virtualmem::allocate_aligned(virt_size, LargePageSize::SIZE as usize)
.unwrap();
heap_start_addr = virt_addr;

info!(
"Heap: size {} MB, start address {:p}",
virt_size >> 20,
virt_addr
);

// try to map a huge page
let mut counter = if has_1gib_pages && virt_size > HugePageSize::SIZE as usize {
paging::map_heap::<HugePageSize>(virt_addr, 1);
HugePageSize::SIZE as usize
#[cfg(target_arch = "x86_64")]
if has_1gib_pages && virt_size > HugePageSize::SIZE as usize {
// Mount large pages to the next huge page boundary
map_addr = virt_addr.align_up_to_huge_page();
map_size = virt_size - (map_addr - virt_addr).as_usize();
let npages = (map_addr - virt_addr).as_usize() / LargePageSize::SIZE as usize;
paging::map_heap::<LargePageSize>(virt_addr, npages);
} else {
0
};

if counter == 0 && has_2mib_pages {
// fall back to large pages
paging::map_heap::<LargePageSize>(virt_addr, 1);
counter = LargePageSize::SIZE as usize;
map_addr = virt_addr;
map_size = virt_size;
}

if counter == 0 {
// fall back to normal pages, but map at least the size of a large page
paging::map_heap::<BasePageSize>(
virt_addr,
LargePageSize::SIZE as usize / BasePageSize::SIZE as usize,
);
counter = LargePageSize::SIZE as usize;
#[cfg(not(target_arch = "x86_64"))]
{
map_addr = virt_addr;
map_size = virt_size;
}

heap_start_addr = virt_addr;

map_addr = virt_addr + counter;
map_size = virt_size - counter;
}

if has_1gib_pages
Expand Down

0 comments on commit 1dc6c02

Please sign in to comment.