Page Tables

Kenton Lam

This is from a CSSE2310 lecture on 29/08/2019. Page tables are complicated (not really).

Multi-level example

Given a multi-level page table struct, how much memory is needed for the page table if
512MiB of contiguous memory is in use?

Assume:

First, we compute the number of pages. This is equal to the number of entries we need to store because one page has exactly one page table entry.
number of entries=number of pages=memory in usepage size=512 MiB/4 KiB=229/212=217
\begin{aligned}
\text{number of entries} = \text{number of pages} &= \frac {\text{memory in use}} {\text{page size}} \
&= 512\text{ MiB} / 4 \text{ KiB} \
&= 2^{29} / 2^{12} = 2^{17}
\end{aligned}

However, the page table itself is stored in pages. We compute how many page table entries can be stored per page.
entries in one page=size of pagesize of entry=212/22=210
\begin{aligned}
\text{entries in one page} &= \frac {\text{size of page}} {\text{size of entry}} \
&= 2^{12} / 2^{2} = 2^{10}
\end{aligned}

Now, we work out how many pages are used to store the page table.
pages in page table=number of entriesentries per page=217/210=27
\begin{aligned}
\text{pages in page table} &= \frac{\text{number of entries}} {\text{entries per page}} \
&= 2^{17} / 2^{10} = 2^7
\end{aligned}

At this point, this is the number of pages in the second level of the page table. We need one extra page for the top level table. Therefore, the final answer is
(27+1)×4 KiB=516 KiB.
(2^{7} + 1) \times 4 \text{ KiB} = 516 \text{ KiB}.

Another case

As above, but how much memory is used if only 1 MiB is used (contiguous)?

Again, we compute the number of pages.
number of entries=number of pages=1 MiB/4 KiB=220/212=28
\begin{aligned}
\text{number of entries} = \text{number of pages} &= 1\text{ MiB} / 4 \text{ KiB} \
&= 2^{20} / 2^{12} = 2^{8}
\end{aligned}

We compute how many page table entries can be stored per page.
entries in one page=size of pagesize of entry=212/22=210
\begin{aligned}
\text{entries in one page} &= \frac {\text{size of page}} {\text{size of entry}} \
&= 2^{12} / 2^{2} = 2^{10}
\end{aligned}

Now, we work out how many pages are used to store the page table.
pages in page table=number of entriesentries per page=28/210=1
\begin{aligned}
\text{pages in page table} &= \frac{\text{number of entries}} {\text{entries per page}} \
&=\left\lceil 2^{8} / 2^{10}\right\rceil = 1
\end{aligned}

Note that above, we rounded up because we can't use part of a page. This is the number in the second level of the page table. We need one extra page for the top level table. Therefore, the final answer is
page table size=(1+1)×4 KiB=8 KiB.
\text{page table size} = (1 + 1) \times 4 \text{ KiB} = 8 \text{ KiB}.

Exercise

Consider a system with 36-bit physical addresses and 32-bit virtual addresses. Pages are 8 KiB each and page table entries are 4 bytes each.

Remark: Single-level page tables will always use a fixed amount of memory. Multi-level tables will adapt to the amount of memory being used.

Single-level

In a single-level page table, the page table must be big enough to hold an entry for every virtual address. Thus,
number of entries=possible virtual addressespage size=232/213=219
\begin{aligned}
\text{number of entries} &= \frac {\text{possible virtual addresses}} {\text{page size}} \
&= 2^{32} / 2^{13} = 2^{19}
\end{aligned}

As in the previous case, how many entries can we fit into one page?
entries per page=page sizeentry size=213/22=211
\begin{aligned}
\text{entries per page} &= \frac{\text{page size}} {\text{entry size}} \
&= 2^{13} / 2^2 = 2^{11}
\end{aligned}

Next, we work out how many pages the page table is
number of pages in page table=number of entriesentries per page=219/211=28
\begin{aligned}
\text{number of pages in page table} &= \frac{\text{number of entries}} {\text{entries per page}} \
&=2^{19} / 2^{11} = 2^8
\end{aligned}

Thus,
page table size=28×23=211=2048 KiB.
\begin{aligned}
\text{page table size} = 2^8 \times 2^3 = 2^{11} = 2048 \text{ KiB}.
\end{aligned}

Multi-level

Note that 2102^{10} bytes is one KiB. Because GiB is 3 orders of magnitude larger than KiB, 1 GiB is 2302^{30} bytes. Now we're going to skip a few steps. Some numbers are reused from calculations above.
number of pages=number of entries=230/213=217pages in table=217/211=26size of table=(26+1)×23 KiB=520 KiB
\begin{aligned}
\text{number of pages} &= \text{number of entries} = 2^{30} / 2^{13} = 2^{17} \
\text{pages in table} &= 2^{17} / 2^{11} = 2^6 \
\text{size of table} &= (2^6+1) \times 2^{3} \text{ KiB} = 520 \text{ KiB}
\end{aligned}