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 3, 2021
1 parent 5510bff commit f0316fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
10 changes: 10 additions & 0 deletions include/mm/regions.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ static inline uint32_t get_bios_ebda_addr(void) {
return (*(uint16_t *) paddr_to_virt_kern(EBDA_ADDR_ENTRY)) << 4;
}

static inline paddr_t get_region_free_start(void *from) {
paddr_t start = _paddr(from);

/* Find unused beginning of the region */
while (!in_free_region(start))
start += PAGE_SIZE;

return start;
}

#endif /* __ASSEMBLY__ */

#endif /* KTF_REGIONS_H */
30 changes: 21 additions & 9 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 @@ -239,10 +241,7 @@ static size_t process_memory_range(unsigned index) {
if (get_avail_memory_range(index, &range) < 0)
return 0;

/* Find unused beginning of the region */
for (start = _paddr(range.start); !in_free_region(start); start += PAGE_SIZE)
;

start = get_region_free_start(range.start);
cur = start;
end = _paddr(range.end);
size = end - start;
Expand All @@ -254,7 +253,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 +308,21 @@ 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) {
unsigned early_frames_cnt;
addr_range_t range;

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

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

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

Expand All @@ -323,14 +337,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 f0316fc

Please sign in to comment.