Implementing Operating System #7

Tharushi Chamalsha
2 min readSep 6, 2021

From the last article, we have learned about the User mode. And this week we gonna learn about virtual memory and paging.

What is virtual memory?

Virtual memory has a very important role in the operating system. It allows us to run more applications on the system than we have enough physical memory to support. Virtual memory is simulated memory that is written to a file on the hard drive. That file is often called a page file or swap file.

You could skip paging entirely and just use segmentation for virtual memory. Each user-mode process would get its own segment, with the base address and limit properly set up. This way no process can see the memory of another process.

Paging

Segmentation translates a logical address into a linear address. Paging translates these linear addresses onto the physical address space and determines access rights and how the memory should be cached.

Why do we use paging?

Paging is a system that allows each process to see a full virtual address space, without actually requiring the full amount of physical memory to be available or present.

In addition to this, paging introduces the benefit of page-level protection. In this system, user processes can only see and modify data which is paged in on their own address space, providing hardware-based isolation. System pages are also protected from user processes.

Paging is achieved through the use of the Memory Management Unit (MMU). The MMU maps memory through a series of tables, two to be exact. They are the paging directory (PD), and the paging table (PT).

Identity Paging : The simplest kind of paging is when we map each virtual address onto the same physical address, called identity paging. This can be done at compile time by creating a page directory where each entry points to its corresponding 4 MB frame.

Enable Paging

Enabling paging is actually very simple. All that is needed is to load CR3 with the address of the page directory and to set the paging (PG) and protection (PE) bits of CR0.

Create paging.s file and paste above code.

Now we have to initialize paging. To do so you have to make a paging.c file and copy following functions.Down forget to make a header file which we can include to the kmain file.

Now you can update kmain.c file with following code.

Don’t forget to update the makefile with o files.

Now you can run the OS using make run command.

--

--