Let’s develop our own operating system (OS ) — Part 8

Page Frame Allocation

Radhushani Samundeeshwaran
2 min readSep 10, 2021

Welcome back readers!

So far, we’ve talked about a lot of steps to get to our main aim of “developing our own operating system” and I hope you guys are following along till blog: part 7. I’m delighted to note that we’ve reached the eighth part of our article series, and we’re on our way to achieving our major aim.

Today I am going to discuss about page frame allocation.

The page frame allocator’s duty is to inform the operating system about which parts of memory are available for use. Exporting labels at the beginning and end of the kernel binary from the linker script is one way to figure out how much memory the kernel uses. So we can modify linker script as shown as bellow:

These labels can be read directly from assembly code and pushed into the stack so that C code can access them:

extern kernel_virtual_start
extern kernel_virtual_end
extern kernel_physical_start
extern kernel_physical_end

; ...

push kernel_physical_end
push kernel_physical_start
push kernel_virtual_end
push kernel_virtual_start

call kmain

Also, by using C language we can declare the labels as functions:

void kernel_virtual_start(void);

/* ... */

unsigned int vaddr = (unsigned int) &kernel_virtual_start;

Managing Available Memory

The page frame allocator’s role is to keep track of which are available and which aren’t. We have many methods to achieve this, such as bitmaps, linked lists, trees, the Buddy System (used by Linux) and so on.

How Can We Access a Page Frame?

The page frame allocator delivers the page frame’s physical start address. There is no page table that points to this page frame since it is not mapped in. We must map the page frame into virtual memory by modifying the kernel’s PDT and/or PT. Then we can’t map the page frame into memory since we’d have to create a new page table, which would take up a full page frame, and we’d have to map this page frame’s page frame to write to it.

We’ve only been able to operate with fixed-size data or raw memory up until now. We can implement malloc and free to use in the kernel now that we have a page frame allocator.

Reference: The little book about OS development/Erik Helin, Adam Renberg

I hope you now have a better understanding of page frame allocation. I’ll be back with another article soon.

--

--