Difference Between Virtual Page and Page Frame

Difference between virtual page and page frame?

Page frame is a physical property of the main memory. Whereas, virtual page is... virtual.

frame 0  frame 1  frame 2  frame 3  frame 4
----------------------------------------------
| | | | | |
| | | | | |
---------------------------------------------- Main Memory

What is the difference between a page and a page table?

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

Sample Image

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

How paging in Linux distinguishes pages from page frames?

A process address space is organized into logical pages. A logical page may be mapped to a physical page frame.

Does it mean that the when a swapped page is loaded back in the Physical memory, its virtual address remains the same but the Physical address changes?

It means a lot more than that. However, yes, a logical page may be mapped to different physical page frames over time.

Do virtual pages reside only in storage? And what is virtual memory without storage?

That diagram is highly confusing because it is showing two concepts at once: Logical memory translation and virtual memory—AND it is splitting the two concepts apart, rather than conflating them as was the norm in the old days.

What will the page table store, they're no pages essentially?

The page table defines the logical address space. It identifies which pages are in the address space. Such pages may or may not exist in physical memory.

In the absence of the swap area how virtual addresses are translated?

Entirely using the page table. If the system uses paging, the operating system has to implement a second level of translation to locate where a specific page is located in secondary storage.

What is virtual memory if there's no swap area and only ram? A typical virtual memory is disk + ram. What if there there's no disk in that expression?

Then you have logical memory translation without virtual memory.

Page frame number vs. Page table entry

physical_address = PFN * page_size + offset

is correct.

The page frames are contiugous page_size-aligned and page_size big chunks of memory, which fill the whole virtually addressable memory.

The page table entries are contained in virtual addresses. In turn, the entries contain page frame numbers, so that the nth entry selects the mth page frame. You can view it like that:

virtual -> physical
PTE -> PFN

After all you can say that page table entries point to page frames.

is number of frame = number of pages(linux)?

Physical pages are called page frames (you can call them frames). The term page is reserved for pages in virtual memory.

The virtual memory is divided into chunks of equal size by the kernel. Physical memory is also divided into pages (page frames) of the same size.

For example if we have 4GB of RAM, on 32 bits architecture, this means : 1048576 page frames of size 4KB

Let's continue,

for each page frame (physical page) the kernel maintain a structure struct page. This structure is defined in linux/mm_types.h(https://github.com/torvalds/linux/blob/master/include/linux/mm_types.h), this structure contain a member named mapping (struct address_space *mapping) which specifies the address space in which a page frame is located. There also a member named index which represent the offset inside this mapping.

All struct pages are kept in global mem_map array this array is used by the kernel to know all the associations between virtual and physical memory.

Finally, to convert a virtual address to a physical one the kernel use the macro virt_to_page() defined in asm-i386/page.h which point to pfn_to_page(https://github.com/torvalds/linux/blob/master/include/asm-generic/memory_model.h).

Before an example, let's see the layout of an address in 32 bits architecture

| 10 bits - Directory | 10 bits - Page table | 12 bits - Offset |

Let's see an example of translating memory virtual address to physical one:

http://img11.imageshack.us/img11/9426/pagingexample.png

Hope this help.

Regards.



Related Topics



Leave a reply



Submit