Paging: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
(Slight Correction)
(Page Directory)
Line 6: Line 6:


==MMU==
==MMU==
Paging is achieved through the use of the [[MMU]]. The MMU is a unit that wonderfully transforms virtual addresses into physical addresses based on the current paging table.
Paging is achieved through the use of the [[MMU]]. The MMU is a unit that transforms virtual addresses into physical addresses based on the current page table.This section focuses on the x86 MMU.


===Relaxed Technical Overview===
===Page Directory===
The topmost paging structure is the page directory. It is essentially an array of page directory entries that take the following form.
Think of your RAM. It is probably several hundred megabytes of continuous non-volatile memory. Now, imagine that it is actually 4 gigabytes. This is what the MMU is paid to do with a little help from the kernel.


'''Note: With 5mb pages, bits 21 through 12 are Reserved!'''
In a 32bit Intel x86 CPU, there are 2 types of tables. There are the:
i* Page Directory
* Page Tables


[[Image:Page dir.png|frame|A Page Table Entry]]
Knowing this, there are some simple rules, if you will:
* There is only one page directory in use at any one time.
* Each structure consumes 4kb of space.
* Each entry in each structure is 4bytes in size.


The page table address field represents the physical address of the page table that managers the four megabytes at that point. Please note that it is very important that this address be 4kb aligned. This is needed, due to the fact that the last bits of the dword are overwritten by access bits and such.


The next valid flag, S, or 'Page Size', stores the page size for that specific entry. If the bit is set, then pages are 4mb in size. Otherwise, they are 4kb.
If you will recall, we know that the MMU is made to map x number of megabytes to 4 gigabytes. Considering this, we can now figure out how physical memory is mapped.


A, or 'Accessed' is used to discover whether a page has been read or written to. If it has, then the bit is set, otherwise, it is not. Note that, this bit will not be cleared by the CPU, so that burden falls on the OS. (ie. if it needs this bit at all.)
*page directory size = 4096 bytes
*page entry = 4 bytes
*number of directory entries = 4096/4 = 1024
*number of table entries = directory entries = 1024
*mapped RAM per directory entry (aka. a table) = 4gb/1024 = 4mb
*mapped RAM per table entry (aka. a page) = 4mb/1024 = 4kb.


D, is the 'Cache Disable' bit. If set, the page will not be cached. Otherwise, it will be.
There you have it. Each 'page' is then 4kb.


W, the controls 'Write-Through' abilities of the page. If the bit is set, write-through caching is enabled. If not, then write-back is enabled instead.


U, the user\supervisor bit, controls access to the page based on privilege level. If the bit is set, then the page may be accessed by all; if the bit is not set, however, only the supervisor can access it.
The structure of the above in computer speak is quite simple. Each entry is the address of it's child. If it's a directory entry, the entry consists of the table's address. Moreover, if it's a table's entry we're talking about, then it's the the address of the mapped physical memory.


R, the read and write permissions flag, either makes the page only readable, that is, when it is not set, or makes the page both readable and writable, that is, being set.
A short note, however, just to ruin your simplicity. Each entry described above also contains several flags. As this is a relaxed overview, I'll only list the three major ones here:
*Bits 31-12: 4kb aligned address of entry
*Bit 2: User\Supervisor
*Bit 1: Read\Write
*Bit 0: Presence Flag - While set, the MMU will assume that this page is currently in memory.


P, or 'Presence', determines if the page is actually in physical memory at the moment. (eg. if a page only exists on the hard drive, it is not in physical memory.) If a page is called, but not present, a page fault will occur, and the OS should handle it. (See below.)
Have you made the connection? Each page is 4kb, and each entry's address must be 4kb aligned! It's just clicking, isn't it?

===Page Table===
Todo


====Example====
====Example====

Revision as of 20:00, 15 November 2007

This page is a work in progress.
This page may thus be incomplete. Its content may be changed in the near future.
This page is a stub.
You can help the wiki by accurately adding more contents to it.

Overview

Paging is a memory scheme that breaks up memory in groups of pages that are constantly swapped between hard disk and computer. This allows for one to appear as though they have more memory than they actually do.

MMU

Paging is achieved through the use of the MMU. The MMU is a unit that transforms virtual addresses into physical addresses based on the current page table.This section focuses on the x86 MMU.

Page Directory

The topmost paging structure is the page directory. It is essentially an array of page directory entries that take the following form.

Note: With 5mb pages, bits 21 through 12 are Reserved!

A Page Table Entry

The page table address field represents the physical address of the page table that managers the four megabytes at that point. Please note that it is very important that this address be 4kb aligned. This is needed, due to the fact that the last bits of the dword are overwritten by access bits and such.

The next valid flag, S, or 'Page Size', stores the page size for that specific entry. If the bit is set, then pages are 4mb in size. Otherwise, they are 4kb.

A, or 'Accessed' is used to discover whether a page has been read or written to. If it has, then the bit is set, otherwise, it is not. Note that, this bit will not be cleared by the CPU, so that burden falls on the OS. (ie. if it needs this bit at all.)

D, is the 'Cache Disable' bit. If set, the page will not be cached. Otherwise, it will be.

W, the controls 'Write-Through' abilities of the page. If the bit is set, write-through caching is enabled. If not, then write-back is enabled instead.

U, the user\supervisor bit, controls access to the page based on privilege level. If the bit is set, then the page may be accessed by all; if the bit is not set, however, only the supervisor can access it.

R, the read and write permissions flag, either makes the page only readable, that is, when it is not set, or makes the page both readable and writable, that is, being set.

P, or 'Presence', determines if the page is actually in physical memory at the moment. (eg. if a page only exists on the hard drive, it is not in physical memory.) If a page is called, but not present, a page fault will occur, and the OS should handle it. (See below.)

Page Table

Todo

Example

Say I loaded my kernel to 0x100000. However, I want it mapped to 0xc0000000. After loading my kernel, I initiate paging, and set up the appropriate tables. (See Higher Half Kernel) After Identity Paging the first megabyte, I start to create my second table (ie. at entry #768 in my directory.) to map 0x100000 to 0xc0000000. My code could be like:

mov eax, 0x0
mov ebx, 0x100000
.fill_table:
     mov ecx, ebx
     or ecx, 3
     mov [table_768+eax*4], ecx
     add ebx, 4096
     inc eax
     cmp eax, 1024
     je .end
     jmp .fill_table
.end:	

Enabling

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 bit of CR0.

mov eax, [page_directory]
mov cr3, eax

mov eax, cr0
or eax, 0x80000000
mov cr0, eax

Usage

Todo

Page Faults

A page fault is an exception caused when a process is seeking to access an area of virtual memory that is not mapped to any physical memory.

Handling

Todo

See Also

Articles

External Links

Paging Tutorial