Implementation of SachOS(Part8-Page Frame Allocation)

Sachithra_Manamperi
3 min readSep 10, 2021

--

This is the 8th in a series of articles on the topic. I’m following the Reference Book “The little book about OS development” by Erik Helin and Adam Renberg.

Here’s the link to part 7.👇

If you Haven’t followed the article series Start reading the first article From here👇

The page frame allocator enables the operating system to divide a system’s physical memory into page frames, which can then be used to allocate memory to programs via a separate paging function.

First, we must determine how much memory is available on the computer that the operating system is running on. The simplest way to do this is to read it from the multiboot structure that GRUB has passed to us.

GRUB gathers the memory information we require, such as what is reserved, I/O mapped, read-only, and so on. We must also ensure that we do not mark the kernel’s memory as free (because GRUB does not mark this memory as reserved). One method for determining how much memory the kernel consumes is to export labels from the linker script at the beginning and end of the kernel binary:

To do so, we must update the linker file link.ld

The page frame allocator must keep track of which frames are and are not available for use. This can be accomplished using bitmaps, linked lists, trees, the Buddy System, and other techniques. Bitmaps are extremely simple to use. Each page frame contains one bit, and one (or more) page frame is reserved for storing the bitmap.

How Do We Get to a Page Frame?

The page frame allocator returns the physical start address of the page frame. Because this page frame is not mapped in, there is no page table that points to it.
What is the most efficient method for reading and writing data to the frame? The page frame must be mapped into virtual memory by modifying the kernel’s PDT and/or PT.
What if all of the available page tables are already taken? Then we won’t be able to map the page frame into memory because we’ll need a new page table that takes up an entire page frame and we’ll have to map the page frame’s page frame to write to it.

Setting aside a piece of the kernel’s higher-half page table for temporarily mapping page frames so that they are accessible is one method. If the kernel is mapped at 0xC0000000 and 4 KB page frames are used, it contains at least one-page table. If we assume or limit ourselves to a kernel with a maximum size of 4 MB minus 4 KB, we can dedicate the last item of this page table to temporary mappings. We can add the page frame we want to utilize as a page table to the paging directory and then delete the temporary mapping once we’ve temporarily mapped it and configured it to map in our initial page frame.

Kernel Heap

Now that we have a page frame allocator, we can implement malloc and free in the kernel. The page frames allocated by the page frame allocator must be mapped to virtual addresses as well. To get started, we’ll need the “kheap.h” header file.

And then we have to create the kheap.c file inside the heap directory

An ideal implementation should return page frames to the page frame allocator when large enough blocks of memory are released.

I believe you have a solid understanding of user mode. This link will take you to my Github repository.

Thank you for reading, and I’ll be back with another series of articles soon.

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Sachithra_Manamperi
Sachithra_Manamperi

Written by Sachithra_Manamperi

🚀 DevOps Engineer | 🎓 Software Engineering Graduate | 🇱🇰 Sri Lankan | 💻 Passionate about Cloud Native tech

No responses yet

Write a response