Build Your Own OS #8

OSDev wiki

Page Frame Allocation

First, we need to know how much memory is accessible on the PC the OS is running on. The least demanding approach to do this is to peruse it from the multiboot structure passed to us by GRUB. GRUB gathers the data we need about the memory — what is saved, I/O planned, read-just and so on We should likewise ensure that we don’t stamp the piece of memory utilized by the portion as free (since GRUB doesn’t check this memory as saved). One approach to know how much memory the part utilizes is to send out names toward the start and the finish of the bit paired from the linker script:

ENTRY(loader)           /* the name of the entry symbol */    . = 0xC0100000          /* the code should be relocated to 3 GB + 1 MB */    /* these labels get exported to the code files */
kernel_virtual_start = .;
kernel_physical_start = . - 0xC0000000;
/* align at 4 KB and load at 1 MB */
.text ALIGN (0x1000) : AT(ADDR(.text)-0xC0000000)
*(.text) /* all text sections from all files */
/* align at 4 KB and load at 1 MB + . */
.rodata ALIGN (0x1000) : AT(ADDR(.rodata)-0xC0000000)
*(.rodata*) /* all read-only data sections from all files */
/* align at 4 KB and load at 1 MB + . */
.data ALIGN (0x1000) : AT(ADDR(.data)-0xC0000000)
*(.data) /* all data sections from all files */
/* align at 4 KB and load at 1 MB + . */
.bss ALIGN (0x1000) : AT(ADDR(.bss)-0xC0000000)
*(COMMON) /* all COMMON sections from all files */
*(.bss) /* all bss sections from all files */
kernel_virtual_end = .;
kernel_physical_end = . - 0xC0000000;
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
void kernel_virtual_start(void);    /* ... */    unsigned int vaddr = (unsigned int) &kernel_virtual_start;

Managing Available Memory

How would we realize which page outlines are being used? The page outline allocator needs to monitor which are free and which aren’t. There are a few different ways to do this: bitmaps, connected records, trees, the Buddy System (utilized by Linux), and so on For more data about the various calculations see the article on OSDev.

How Might We Access a Page Frame?

The page outline allocator returns the actual beginning location of the page outline. This page outline isn’t planned in — no page table focuses to this page outline. How might we peruse and compose information to the casing?

(768 << 22) | (1023 << 12) | 0x000 = 0xC03FF000

A Kernel Heap

So far we’ve simply had the option to work with fixed-size information, or straightforwardly with crude memory. Since we have a page outline allocator we can carry out mallocand be freeto use it in the bit.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store