Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pmm: properly detect first available memory region #281

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions mm/pmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#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 @@ -246,7 +244,27 @@ static inline unsigned int find_max_avail_order(size_t size) {
return PAGE_ORDER_4K;
}

static size_t process_memory_range(unsigned index) {
static unsigned find_first_avail_region(void) {
addr_range_t range;

for (unsigned int i = 0; i < regions_num; i++) {
if (get_avail_memory_range(i, &range) < 0)
continue;

if (_paddr(range.start) < _paddr(MB(1)))
continue;

if (_paddr(range.end) - _paddr(range.start) < MB(EARLY_VIRT_MEM))
continue;

return i;
}

panic("PMM: Cannot obtain first available physical memory address range\n");
UNREACHABLE();
}

static size_t process_memory_range(unsigned index, unsigned first_avail_region) {
paddr_t start, end, cur;
unsigned int max_order;
addr_range_t range;
Expand All @@ -267,7 +285,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 <= PMM_FIRST_AVAIL_REGION)
if (index <= 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 @@ -320,11 +338,11 @@ void reclaim_frame(mfn_t mfn, unsigned int order) {
add_frame(mfn, order);
}

static inline void check_early_frames(void) {
static inline void check_early_frames(unsigned first_avail_region) {
unsigned early_frames_cnt;
addr_range_t range;

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

early_frames_cnt =
Expand All @@ -336,6 +354,8 @@ static inline void check_early_frames(void) {
}

void init_pmm(void) {
unsigned first_region_index;

printk("Initialize Physical Memory Manager\n");

BUILD_BUG_ON(sizeof(frames_array_t) > PAGE_SIZE);
Expand All @@ -348,13 +368,15 @@ void init_pmm(void) {
list_init(&busy_frames[order]);
}

first_region_index = find_first_avail_region();

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

display_frames_count();

check_early_frames();
check_early_frames(first_region_index);

if (opt_debug)
display_frames();
Expand Down