YmeOS — Let’s Build our own OS !!!😉

#7-virtual memory paging

  1. Segmentation is a virtual process that creates variable-sized address spaces in computer storage for related data, called segments.

Why Paging?

Paging is the most common technique used in x86 to enable virtual memory. Virtual memory through paging means that each process will get the impression that the available memory range is 0x00000000 - 0xFFFFFFFF even though the actual size of the memory might be much less.

Paging in x86

Paging in x86 consists of a page directory (PDT) that can contain references to 1024 page tables (PT), each of which can point to 1024 sections of physical memory called page frames (PF). Each page frame is 4096 byte large. In a virtual (linear) address, the highest 10 bits specifies the offset of a page directory entry (PDE) in the current PDT, the next 10 bits the offset of a page table entry (PTE) within the page table pointed to by that PDE. The lowest 12 bits in the address is the offset within the page frame to be addressed.

Enabling Paging

Create a file called paging_enable.s and include following assembly code to it.

Paging and the Kernel

Noe let’s see how this paging effects the heart of OS, the Kernel.

Higher-half Linker Script

For this we have to modify the link.ld to implement this. The following code can be used:

Entering the Higher Half

When GRUB jumps to the kernel code, there will be no paging table. Therefore, all references to 0xC0100000 + x won’t be mapped to the correct physical address, and will therefore cause a general protection exception or the computer will just crash.

  • Add identity mapping for the first 4 MB of the virtual address space.
  • Add an entry for 0xC0100000 that maps to 0x0010000
; assembly code executing at around 0x00100000
; enable paging for both actual location of kernel
; and its higher-half virtual location lea ebx, [higher_half] ; load the address of the label in ebx
jmp ebx ; jump to the label higher_half:
; code here executes in the higher half kernel
; eip is larger than 0xC0000000
; can continue kernel initialisation, calling C code, etc.

Undergraduate | Software Engineering ( BSc Hons ) | www.linkedin.com/in/thushara-samaraweera-121b251a4