Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process Page Tables

I'm interested in gaining a greater understanding of the virtual memory and page mechanism, specifically for Windows x86 systems. From what I have gathered from various online resources (including other questions posted on SO),

1) The individual page tables for each process are located within the kernel address space of that same process.

2) There is only a single page table per process, containing the mapping of virtual pages onto physical pages (or frames).

3) The physical address corresponding to a given virtual address is calculated by the memory management unit (MMU) essentially by using the first 20 bits of the provided virtual address as the index of the page table, using that index to retrieve the beginning address of the physical frame and then applying some offset to that address according to the remaining 12 bits of the virtual address.

Are these three statements correct? Or am I misinterpreting the information?

like image 514
Jimmy Avatar asked Jul 08 '11 17:07

Jimmy


People also ask

What is process page table?

Page Tables are built for each process address space. The Page Table maps logical virtual addresses for a process to physical memory locations. The location for a set of Page Tables for a process is passed to the processor hardware during a context switch.

Where are process page tables stored?

The page table of the process is held in the kernel space. The kernel may have several page tables in RAM, but only one is the active page table. In x86 CPUs, it's the page table pointed by register CR3.

How many page tables are in a system?

Short answer. The most commonly used number of page tables on x86-64 system is 4. This is assuming a 64-bit OS using what Intel calls IA-32e paging mode and 4 KB pages. Fewer page tables are needed for some of the other modes (as described in the Intel documentation).

What does a page table contains?

The page table stores all the Frame numbers corresponding to the page numbers of the page table. In other words, the page table maps the page number to its actual location (frame number) in the memory.


1 Answers

So, first lets clarify some things:

  1. In the case of the x86 architecture, it is not the operating system that determines the paging policy, it is the CPU (more specifically it's MMU). How the operating system views the paging system is independent of the the way it is implemented. As a commenter rightly pointed out, there is an OS specific component to paging models. This is subordinate to the hardware's way of doing things.
  2. 32 bit and 64 bit x86 processors have different paging schemes so you can't really talk about the x86 paging model without also specifying the word size of the processor.

What follows is a massively condensed version of the 32 bit x86 paging model, using the simplest version of it. There are many additional tweaks that are possible and I know that various OS's make use of them. I'm not going into those because I'm not really familiar with the internals of most OS's and because you really shouldn't go into that until you have a grasp on the simpler stuff. If you want the to know all of the wonderful quirks of the x86 paging model, you can go to the Intel docs: Intel System Programming Guide

In the simplest paging model, the memory space is divided into 4KB blocks called pages. A contiguous chunk of 1024 of these is mapped to a page table (which is also 4KB in size). For a further level of indirection, All 1024 page tables are mapped to a 4KB page directory and the base of this directory sits in a special register %cr3 in the processor. This two level structure is in place because most memory spaces in the OS are sparse which means that most of it is unused. You don't want to keep a bunch of page tables around for memory that isn't touched.

When you get a memory address, the most significant 10 bits index into the page directory, which gives you the base of the page table. The next 10 bits index into that page table to give you the base of the physical page (also called the physical frame). Finally, the last 12 bits index into the frame. The MMU does all of this for you, assuming you've set %cr3 to the correct value.

64 bit systems have a 4 level paging system because their memory spaces are much more sparse. Also, it is possible to page sizes that are not 4KB.

To actually get to your questions:

  1. All of this paging information (tables, directories etc) sits in kernel memory. Note that kernel memory is one big chuck and there is no concept of having kernel memory for a single process.
  2. There is only one page directory per process. This is because the page directory defines a memory space and each process has exactly one memory space.
  3. The last paragraph above gives you the way an address is chopped up.

Edit: Clean up and minor modifications.

like image 114
Abhay Buch Avatar answered Oct 08 '22 05:10

Abhay Buch