I'm trying to better understand paging

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
vhg119
Member
Member
Posts: 71
Joined: Fri Aug 24, 2007 5:56 pm
Location: CA, USA

I'm trying to better understand paging

Post by vhg119 »

Hi everyone,

I've read a ton of docs, including the Intel Manual on the 80386. I'm trying to wrap my head around paging and virtual memory. I have a few questions that I hope someone might be able to help me clear up:

Do the PDBR, GDTR, and IDTR hold physical addresses?

If the PDBR holds physical addresses, does this mean that every page dir is always in physical memory?

If they don't hold physical addresses, how does the PDBR get translated?

Are page tables automatically 4KB too, just like the page directories? (stupid question but I just need reassurance)

Do page dirs entries hold the physical address of a page table? If so, would that mean that all page tables must always be in physical memory?

My mind is stuck in the recursive loop when I try to understand this.

Also, could someone list or point me to something that lists how the memory is used? IE. Video buffer occupies 0xB8000 to 0xBFFFF .. etc

One last thing...

I've read an article that had an example of setting up the GDT for paging.
The author created 5 GDT descriptors- Null, Ring0 Code, Ring0 Data, Ring3 Code, Ring3 Data.
The author mentioned that all the code descriptors had a base of 0x0 and a 4GB limit.
Am I missing something here?
Shouldnt the Ring0 Code descriptor have a base of 2GB and a limit of 2GB? And the Ring3 Code descriptor have a base of 0x0 and a limit of 2GB?
Otherwise, how would a Ring3 code call a callgate?

Regards,

Vince
vhg119
Member
Member
Posts: 71
Joined: Fri Aug 24, 2007 5:56 pm
Location: CA, USA

Post by vhg119 »

Ok... So I RTFM for the 21st time and I found some answers to my questions.
(INTEL 80386 PROGRAMMER'S REFERENCE MANUAL 1986)

The GDTR holds a linear address... therefore is subject to translation by the paging mechanism. (page 94).

The PDBR holds a physical address. (page 99)
- So does this mean that if there were 8000 tasks, each with their own Page Directory, we would need all 8000 Page Directories stored in physical memory at all time?
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

The GDT, IDT, TSS, and LDT all use an linear address (virtual address). They must be mapped into virtual memory for the processor to read them. If the GDT pointer is set to 0x3000. Then you enable paging and nothing is mapped at 0x3000 then the processor will fault, although before paging was enabled it worked fine. The same applies to the IDT, LDT, and TSS.

The page directory and tables all use physical addresses.

The relation of the virtual address to the physical address is implied by:

dir_index = vaddress >> 22;
table_index = vaddress << 10 >> 22;

So it reads the table from dir[dir_index], then it reads the entry in that table from the_table[table_index]. What ever is the physical address stored there is what the vaddress maps to.
So does this mean that if there were 8000 tasks, each with their own Page Directory, we would need all 8000 Page Directories stored in physical memory at all time?
Yes.

However, everything is stored in physical memory somewhere.. virtual memory is not a whole new storage area. A simple example of translation might be:


unsigned long map[0x1000] = {1, 3, 9, 10, 33, 55, 1000, 1022, .... ect };
translated_address | (address & 0xFFF) = map[address >> 12];


We can translate page addresses from 0x0 to 0x1000000. This is very similar to how virtual memory does it's translation, or I should say the address the processor sends towards the memory circuitry is translated.

A major difference is the amount of space it would take to use a really big array like that for 4GB.

unsigned long map[0xffffffff >> 12];

That takes 4MB. This is almost exactly how much memory would be taken using a page directory and tables to map 4GB of memory. The reasoning takes over when you realize that not ever, if hardly in normal circumstances, would a process/application ever need 4GB of accessible memory (therefore also meaning mapped/accessible memory). So only the tables needed are allocated and entered into the directory. This makes mapping only 4MB of memory take 8192 bytes instead of always using one big large array of contiguous memory which would take 4MB always and every time.

Also having the tables linked into the directory, instead of using one large array, enables you to share tables. This becomes very useful when splitting the address space between kernel and user land. Since the kernel's tables can simply be linked into every page directory for every process. Instead of creating a seperate set of tables for kernel land for every process.
vhg119
Member
Member
Posts: 71
Joined: Fri Aug 24, 2007 5:56 pm
Location: CA, USA

Post by vhg119 »

Thanks for the reply.

I read that a Page Table is itself a page. I also noticed that a PDE for a Page Table has a 'present' bit.

If the address held in a PTE is a physical address and a Page Table is always stored in physical memory (not swapped out to HD), what's the use of a 'present' bit?

Vince
jnc100
Member
Member
Posts: 777
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

vhg119 wrote:If the address held in a PTE is a physical address and a Page Table is always stored in physical memory (not swapped out to HD), what's the use of a 'present' bit?
It allows you to mark an entire 4MB area of virtual memory as not present by setting a single bit, rather than having to create every single possible page table and mark all the entries in that as not present. Also, it is entirely possible to swap a page table out to disk. Typically, you would swap page tables for a rarely used background process when memory is really low, mark it as not present and use the other bits to specify where in the swap file the page table has been swapped to, much like you would do with a 'normal' page (i.e. one that isn't a page table). This is entirely doable as long as you zero all new pages on allocation, then any pde or pte that is marked as not present, but has some other bits set, exists in the swap file, rather than having not been allocated in the first place.

Regards,
John
User avatar
JAAman
Member
Member
Posts: 880
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

(INTEL 80386 PROGRAMMER'S REFERENCE MANUAL 1986)
i seriously suggest you get an updated manual, as the newer manuals have changed some information and corrected inaccuracies and ambiguities (there is a link to the latest copies in my signature, and on that page, there is a link which explains how you can get a hard-copy -- highly recommended, its much easier to read a book than an online document, be sure to get all 5 -- they are shipped free of charge, practically anywhere in the world)
I've read an article that had an example of setting up the GDT for paging.
The author created 5 GDT descriptors- Null, Ring0 Code, Ring0 Data, Ring3 Code, Ring3 Data.
The author mentioned that all the code descriptors had a base of 0x0 and a 4GB limit.
this is what is called 'flat mode' (as described in 3A:3.2.1, but modified to include protection rings), and is used by almost all OSs (there are a few exceptions) this system is simple because you dont need to worry about segment address translation (it is essentially 'disabled') -- though do note, that you will need at least 1 more entry for at least 1 TSS per CPU (if you use any ring3 code, you must have a TSS loaded)
Am I missing something here?
Shouldnt the Ring0 Code descriptor have a base of 2GB and a limit of 2GB? And the Ring3 Code descriptor have a base of 0x0 and a limit of 2GB?
you dont need to separate the code segments, because all protection is handled by paging under this system, so setting all the segment bases to 0, and limits to 4GB, you make the segmentation system invisible, and all protection is handled by the more flexible paging system (it is possible to use both, but quickly becomes more complicated and provides little (or no) benefit -- this has no adverse affect on call gates (if you choose to use them, most dont)
- So does this mean that if there were 8000 tasks, each with their own Page Directory, we would need all 8000 Page Directories stored in physical memory at all time?
no, only the currently executing page directory needs to be currently in physical memory (and not-currently in use page tables can be swapped out, or just not yet created)
vhg119
Member
Member
Posts: 71
Joined: Fri Aug 24, 2007 5:56 pm
Location: CA, USA

Post by vhg119 »

Thanks guys. Those are great answers.
no, only the currently executing page directory needs to be currently in physical memory (and not-currently in use page tables can be swapped out, or just not yet created)
I suppose that I should implement the process scheduler to swap in the current executing page directory every time it switches to a new task.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

My opinion would be to not worry too much over having thousands of processes at your early stage of development. Then again other people very well may have a different opinion to varying degrees. :P
vhg119
Member
Member
Posts: 71
Joined: Fri Aug 24, 2007 5:56 pm
Location: CA, USA

Post by vhg119 »

Thanks guys. I think I've found answers to every question I listed in this thread.

I've also ordered the updated manual from Intel. I was told that the 1986 one was an easier read, but I'm tired of all the errors in it.
User avatar
Candy
Member
Member
Posts: 3883
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

Kevin McGuire wrote:My opinion would be to not worry too much over having thousands of processes at your early stage of development. Then again other people very well may have a different opinion to varying degrees. :P
I would say, worry as soon as you get things that affect your current code. That would be, if you are limiting each process to having its own code space, worry about the upper limit. If you think the upper limit is sufficient, continue. If it isn't, rethink.
User avatar
JAAman
Member
Member
Posts: 880
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

vhg119 wrote:Thanks guys. I think I've found answers to every question I listed in this thread.

I've also ordered the updated manual from Intel. I was told that the 1986 one was an easier read, but I'm tired of all the errors in it.
well, its a lot smaller, but lacks the details of the newer chips (much of the added content is new features or new recommendations for use of existing features)
Post Reply