2

I read some manuals about page directories and page tables and still very confused about these concepts. For example here I found that making only a single level paging is wasting memory. So in x86 architecture we use 2-level paging.

cr3 --> PageDirectory --> PageTable --> Page

So consider the following linear address.

01001010101 1010101101 10101110101010 
 page tbl     page      offset

Using page tbl bits we find the page directory entry physical address. PD = cr3 + L * (page tbl) which contains page table physical address PT. Now we are looking for our page P = PT + L * page. So in single level we had one page table 4MiB. Now we have 1024 page tables 4KiB = 4MiB in total. Don't see memory economy.

What did I miss?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • 4MB of contiguous physical memory per process is actually quite a lot. My Linux desktop has 151 user-space processes running right now (still counting multi-threaded processes as 1). (I excluded kernel processes because they don't have their own page tables). That would be 604MiB spent on page tables alone. – Peter Cordes Nov 15 '17 at 16:14
  • @PeterCordes Yes, kind of... But anyway if we have all 1024 page tables allocated we will have the same 4MiB of memory spent on page table. The thing is not all of these 1024 page tables are allocated, correct? – St.Antario Nov 15 '17 at 17:29
  • Yes, exactly. My comment only makes sense along with @Fuz's answer: the memory maps of most processes are very sparse, especially in a 64-bit system. – Peter Cordes Nov 15 '17 at 17:57

1 Answers1

5

Normally, the page table is far from full. With a flat page table, if only the first few and the last few entries are needed, still the entire table needs to be allocated. With page directories, all but the first and last page table can be omitted, thus saving a lot of memory. The gains seem to be small in today's age of vast RAM, but it was really a lot back when computers might only have 4 MiB of RAM in total.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • Sounds reasonable. Can I ask you some clarification. As specified here [here](http://wiki.osdev.org/Paging) We use only 20 bits in page directory. I that the reason it's 4KiB aligned? In order to represent the full range of available addresses? – St.Antario Nov 15 '17 at 15:22
  • @St.Antario Exactly. Only the high 20 bits of the page table's address are used, the low 12 bits are used for bookkeeping. – fuz Nov 15 '17 at 15:28