1
votes

I know that a page table is responsible for translating virtual addresses to physical addresses by using a map like structure, and a page is some kind of fixed size block in virtual memory. But how are these two related? are each pages (virtual) being translated to physical addresses (physical pages) using the page table? Why do we need to organize the virtual memory into fixed pages anyway? I tried googling but cannot find some explicit answers. Any inputs are appreciated.

1
If you are interested on the subject you will quickly find that google searches and even SO questions are insufficient tools to deep into the matter. I strongly recommend getting a good book and learn from there. Modern Operating Systems by Tanenbaum is probably the most revered in the field.bolov

1 Answers

1
votes

Lets say you have a system without virtual memory and a program issues an instructions such as MOV REG,0, its effectivelly accessing the physical address 0. Then you realise that since programs can read and write on each other's memory area, this implementation can cause issues. So we introduce virtual memory

What is page table

We want to maintain a mapping, programs will still generate addresses in similar way (which we now call virtual addresses) but before sending the memory access instructions to memory we will feed them to our MMU unit, which will provide us with a physical address, we then send this address to memory.

What information we need for mapping

In simple approach we store two entries in table, virtual address and physical address. lets say we use a 32 bit system, so every page table entry in this simple approach will cost us 64 bits or 8 bytes. So 8 bytes every entry and there are 2^32 addresses, our page table is going to take lot of memory.

How to reduce the size of page table

Since we don't want to waste (2^32)*8 bytes on page table, we decide we don't want to add entry for every address, instead we will divide our physical memory into frames of size 4 KB. This means we have (2^32)/(2^12) = 2^20 frames, in this setting we only need first 20 bits of an address to identify the page itself, remaining 12 bits are offset bits. So all the entries starting with 00000000000000000000 are on page 1, where 00000000000000000000000000000000 is the first address and 00000000000000000000111111111111 is the last of this page. so now we store our mapping in following way

enter image description here

lets say now we receive MOV REG,0 instuction, so we check page table, since address 0 is between 0 and 4K, it lies on first page and its mapped to frame 2. so we replace the first 20 bits which give us page base address with the base address of frame 2, and we have our physical address, which we pass to memory.

Still there are lots of details that can't really be clarified in SO post, I suggest you pick a good book such as Modern Operating Systems or Operating Systems: Three Easy Pieces