Build Your Own OS #7

Virtual Memory

How Much Memory is There?

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 Can We Access a Page Frame?

(768 << 22) | (1023 << 12) | 0x000 = 0xC03FF000
global enablePaging                                               enablePaging:                       ; load page directory (eax has the address of the page directory)                           
mov eax, [esp+4]
mov cr3, eax
; enable 4MBpage
; mov ebx, cr4 ; read current cr4
; or ebx, 0x00000010 ; set PSE - enable 4MB page
; mov cr4, ebx ; update cr4
; enable paging
mov ebx, cr0 ; read current cr0
or ebx, 0x80000000 ; set PG . set pages as read-only for both userspace and supervisor, replace 0x80000000 above with 0x80010000, which also sets the WP bit.
mov cr0, ebx ; update cr0
ret ; now paging is enabled

A Kernel Heap

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Composition in Flutter & Dart [Functional Programming — Part 3]

How to add quickly and easily multiple SSH keys to Desktop (Ubuntu, Window, …)

RSpec Isn’t Given, it’s Earned

Automatically delete read database notifications in Laravel

Core FQL concepts part 3: Data aggregation

It’s time for some quality control for WordPress plug-ins — a proposal

Image of a meter showing quality levels

⚡Azure Deployment -> Microsoft Teams

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
Niroshan Pushparaj

Niroshan Pushparaj

More from Medium

Fake BTS in Action.

Press Releases Communication System

Day 1 — Introduction to Web 3.0

Blog Post #8: Final Project