Skip to content

Commit

Permalink
pmm: correctly calculate frames count for early memory
Browse files Browse the repository at this point in the history
Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Sep 2, 2021
1 parent 5510bff commit 4c84127
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions mm/pmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <mm/regions.h>
#include <mm/vmm.h>

#define PMM_FIRST_AVAIL_REGION 1

size_t total_phys_memory;

static list_head_t frames;
Expand Down Expand Up @@ -254,7 +256,7 @@ static size_t process_memory_range(unsigned index) {

/* Add initial 4K frames and align to 2M. */
while ((cur < MB(EARLY_VIRT_MEM) || cur % PAGE_SIZE_2M) && cur + PAGE_SIZE <= end) {
if (index <= 1)
if (index <= PMM_FIRST_AVAIL_REGION)
add_early_frame(paddr_to_mfn(cur), PAGE_ORDER_4K);
else
add_frame(paddr_to_mfn(cur), PAGE_ORDER_4K);
Expand Down Expand Up @@ -309,6 +311,20 @@ static inline void display_frames(void) {

void reclaim_frame(mfn_t mfn, unsigned int order) { add_frame(mfn, order); }

static inline void check_early_frames(void) {
addr_range_t range;
unsigned early_frames;

if (get_avail_memory_range(PMM_FIRST_AVAIL_REGION, &range) < 0)
panic("PMM: Cannot obtain first available physical memory address range\n");

early_frames = (MB(EARLY_VIRT_MEM) - _paddr(range.start)) / PAGE_SIZE;
if (frames_count[PAGE_ORDER_4K] < early_frames) {
panic("Not enough early frames: %u missing\n",
early_frames - frames_count[PAGE_ORDER_4K]);
}
}

void init_pmm(void) {
printk("Initialize Physical Memory Manager\n");

Expand All @@ -323,14 +339,12 @@ void init_pmm(void) {
}

/* Skip low memory range */
for (unsigned int i = 1; i < regions_num; i++)
for (unsigned int i = PMM_FIRST_AVAIL_REGION; i < regions_num; i++)
total_phys_memory += process_memory_range(i);

display_frames_count();

if (frames_count[PAGE_ORDER_4K] < (MB(EARLY_VIRT_MEM) / PAGE_SIZE))
panic("Not enough early frames: %u missing\n",
(MB(EARLY_VIRT_MEM) / PAGE_SIZE) - frames_count[PAGE_ORDER_4K]);
check_early_frames();

if (opt_debug)
display_frames();
Expand Down

0 comments on commit 4c84127

Please sign in to comment.