demand 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
beyondsociety

demand paging

Post by beyondsociety »

If I wanted to use the whole 4GB of address space. I would need to map 4MB of memory for the page directory/tables.

4kb/page * 1024 pages = 4096kb = 4MB of address space to map.

Now in reality, I only want to use one page directory and one table as temporary to setup paging. So I would only need to allocate 8092 bytes (8KB), 4096 bytes for size of page directory and 4096 bytes for size of page table.

4kb/page * 1 pages = 4096 bytes = 4kb for page directory
4096 bytes (4kb) for 1 page table.

So for example:

Code: Select all

unsigned long address = 0x100000; // starting address
unsigned int i;

/* map the first 4MB (1MB - 5MB) of memory to access 4GB of memory */
for(i = 0; i < 1024; i++;)
{
          page_table[i] = address | 3;
          address += 0x1000; // 0x1000 = 4kb
}
Would be:

Code: Select all

unsigned long address = 0x100000; // starting address
unsigned int i;

/* Map 8KB of memory, enough for one page directory and page table */
for(i = 0; i < 1; i++)
{
         page_table[i] = address | 3;
         address += 0x1000;  // 0x1000 = 4kb
}
Do have the right idea?
proxy

Re:demand paging

Post by proxy »

a neat trick i leaned about setting up paging is if you map an entry in the page directory to itself (pretending the page dir is a page table) it will give you access to the page directory and all mapped pages at fixed locations.

personally, i like to set the last entry of the page directory to map the page directory...so the very last page of virtual memory is the virtual address of page directory.

and mapped pages are accessible starting at: 0xffc00000. at the cost of 4 megs of virtual memory you have a world of convinience :)

proxy
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:demand paging

Post by Pype.Clicker »

i'm not 100% certain i got BeyondSociety's point correctly, but indeed, as Proxy said, recursive mapping of the page table leads to very convenient and yet efficient scheme.

You can naturally access PG_DIR_ENTRY(vaddr) or PG_TBL_ENTRY(vaddr) at any moment, while having a single page that map one of the tables may require an important amount of TLB flushes just for page maintainance. It would also make it harder to copy/compare entries from distinct entries ...

Btw, the term "demand paging" usually signifies "allocating frames for pages as invalid accesses are made from the user process" ...
beyondsociety

Re:demand paging

Post by beyondsociety »

Btw, the term "demand paging" usually signifies "allocating frames for pages as invalid accesses are made from the user process" ...
Im was thinking along the lines of partial mapping where I only need to map enough memory for the necessary page directory and page table for setting up paging.

I guess I need to read the intel manuals again. Also a design of it on paper might help me to explain it better to you. Goes off to read his manual. ;D
srg

Re:demand paging

Post by srg »

This trick of mapping the page directory into it's self, I still don't understand what the benefites of this are.

What are the real benefits of this idea, I just doesn't make sense to me at the moment.

srg
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:demand paging

Post by Candy »

srg wrote: This trick of mapping the page directory into it's self, I still don't understand what the benefites of this are.

What are the real benefits of this idea, I just doesn't make sense to me at the moment.

srg
you save 4k, you get a very simple way of addressing the page tables, you can use way simpler page mapping algorithms, because the mapping is so simple you also save pages (since you auto-map the page tables, you don't have to spend extra page tables on mapping the page tables). On a 4-level system (amd64) the savings are of course higher.

Anyway, since I'm kind of depressed and don't write in a nice tone (sorry bout that) when I'm like that, I'm not going to post here for some days/weeks/months/something until it's passed. Cya.
srg

Re:demand paging

Post by srg »

Get Well soon Candy

hmm Well what I don't understand is how does it make mapping and addressing simpler?

thanks
srg
proxy

Re:demand paging

Post by proxy »

because it provides a fixed virtual memory locationfor each mapped page. for example.

if page_dir[1023] = page_dir then

the vaddress of the page_dir is 0xfffff000

the vaddress of page_dir = 0xffc00000 + (i * page_size);

once you map the page table, you can almost immidiiately access it to map the pages in it via that virtual address which is trivial to calculate.

proxy
beyondsociety

Re:demand paging

Post by beyondsociety »

Ive been toying with paging for quite a while and I have noticed something wrong with my design.

My paging design:
I allocate one page directory at 0x124000 and map the page directory onto itself and then mark all entries to not present except for one page table. I then allocate the one page table at 0x125000 and map 2MB of memory (512 entries in the page table) and set the rest to not present.
beyondsociety

Re:demand paging

Post by beyondsociety »

[glow=red,2,300]Update:[/glow]In my original design I was using two variables: one for the index into the page directory and one for the mapping. Thus when my code ran, it would page fault because the variable for the page directory index was at a different location than the other variable for the mapping.

So instead of mapping two seperate areas, I just used one variable for both solving my problem.
Post Reply