Skip to content

Commit

Permalink
pmm: refactor add_frame()
Browse files Browse the repository at this point in the history
The add_frame() does not update the input address parameter and uses
mfn_t instead of pointer to paddr_t.

Split add_frame() into add_early_frame() (used only for first region
of physical memory) and add_frame(), thereby drop 'initial' paramater.

Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Aug 13, 2021
1 parent 8bd037d commit 54a4658
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions mm/pmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,30 @@ void display_frames_count(void) {
}
}

static void add_frame(paddr_t *pa, unsigned int order, bool initial) {
frame_t *free_frame = &early_frames[free_frame_idx++];
static inline frame_t *new_frame(mfn_t mfn, unsigned int order) {
frame_t *frame = &early_frames[free_frame_idx++];

if (free_frame_idx > ARRAY_SIZE(early_frames))
panic("Not enough initial frames for PMM allocation!\n");

free_frame->order = order;
free_frame->mfn = paddr_to_mfn(*pa);
free_frame->flags.free = true;
frame->order = order;
frame->mfn = mfn;
frame->flags.free = true;

*pa += (PAGE_SIZE << order);

if (initial)
list_add(&free_frame->list, &free_frames[order]);
else
list_add_tail(&free_frame->list, &free_frames[order]);
frames_count[order]++;
return frame;
}

static inline void add_early_frame(mfn_t mfn, unsigned int order) {
frame_t *frame = new_frame(mfn, order);

list_add(&frame->list, &free_frames[order]);
}

void reclaim_frame(mfn_t mfn, unsigned int order) {
paddr_t pa = mfn_to_paddr(mfn);
static inline void add_frame(mfn_t mfn, unsigned int order) {
frame_t *frame = new_frame(mfn, order);

add_frame(&pa, order, false);
list_add_tail(&frame->list, &free_frames[order]);
}

static size_t process_memory_range(unsigned index) {
Expand All @@ -91,24 +92,37 @@ static size_t process_memory_range(unsigned index) {
*/

/* Add initial 4K frames and align to 2M. */
while (cur % PAGE_SIZE_2M && cur + PAGE_SIZE <= end)
add_frame(&cur, PAGE_ORDER_4K, true);
while (cur % PAGE_SIZE_2M && cur + PAGE_SIZE <= end) {
if (index <= 1)
add_early_frame(paddr_to_mfn(cur), PAGE_ORDER_4K);
else
add_frame(paddr_to_mfn(cur), PAGE_ORDER_4K);
cur += (PAGE_SIZE << PAGE_ORDER_4K);
}

/* Add initial 2M frames and align to 1G. */
while (cur % PAGE_SIZE_1G && cur + PAGE_SIZE_2M <= end)
add_frame(&cur, PAGE_ORDER_2M, false);
while (cur % PAGE_SIZE_1G && cur + PAGE_SIZE_2M <= end) {
add_frame(paddr_to_mfn(cur), PAGE_ORDER_2M);
cur += (PAGE_SIZE << PAGE_ORDER_2M);
}

/* Add all remaining 1G frames. */
while (cur + PAGE_SIZE_1G <= end)
add_frame(&cur, PAGE_ORDER_1G, false);
while (cur + PAGE_SIZE_1G <= end) {
add_frame(paddr_to_mfn(cur), PAGE_ORDER_1G);
cur += (PAGE_SIZE << PAGE_ORDER_1G);
}

/* Add all remaining 2M frames. */
while (cur + PAGE_SIZE_2M <= end)
add_frame(&cur, PAGE_ORDER_2M, false);
while (cur + PAGE_SIZE_2M <= end) {
add_frame(paddr_to_mfn(cur), PAGE_ORDER_2M);
cur += (PAGE_SIZE << PAGE_ORDER_2M);
}

/* Add all remaining 4K frames. */
while (cur < end)
add_frame(&cur, PAGE_ORDER_4K, false);
while (cur < end) {
add_frame(paddr_to_mfn(cur), PAGE_ORDER_4K);
cur += (PAGE_SIZE << PAGE_ORDER_4K);
}

if (cur != end) {
panic(
Expand All @@ -132,6 +146,8 @@ static inline void display_frames(void) {
}
}

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

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

Expand Down

0 comments on commit 54a4658

Please sign in to comment.