Map Virtual to Physical

Mapping a virtual address to a physical address is relatively easy once the correct formulas are known. There are also some pieces of information that you will need, such as the page size and a page table.

There are four steps to converting a virtual address to a physical address:

  1. Calculate the page (Note: integer division is used for this):
    $$page = \frac{virtual\ address}{page\ size}$$
  2. Calculate the page offset (Note: $\%$ is the modulus operator):
    $$offset = virtual\ address\ \%\ page\ size$$
  3. Find the frame by mapping the page to a frame via the page table
  4. Calculate the physical address:
    $$physical\ address = frame * page\ size + offset$$

It is not always possible to get a physical address from a virtual address. If the page table has a page listed as invalid, then a segmentation fault would occur, instead of accessing the physical address.


Q: Given the following page table, map the (base 10) virtual addresses to physical addresses.
Virtual Addresses: 500, 8200, 102400

(2011 Mid semester exam, Q15)

Relevant information: Page size is 4KB (4096B)

Page Frame
0 200
1 201
2 22
3 invalid
... invalid
25 24
... invalid

For each of these virtual addresses, we need to calculate the physical address using the process outlined above.

Virtual address 500:

$$page = 500 / 4096 = 0$$

$$offset = 500\ \%\ 4096 = 500$$

From our page table, we can see that page 0 maps to frame 200.

$$physical\ address = 200 * 4096 + 500 = 819700$$

Virtual address 8200:

$$page = 8200 / 4096 = 2$$

$$offset = 8200\ \%\ 4096 = 8$$

From our page table, we can see that page 2 maps to frame 22.

$$physical\ address = 22 * 4096 + 8 = 90120$$

Virtual address 102400:

$$page = 102400 / 4096 = 25$$

$$offset = 102400\ \%\ 4096 = 0$$

From our page table, we can see that page 25 maps to frame 24.

$$physical\ address = 24 * 4096 + 0 = 98304$$

From this, we can see that it is possible to get a physical address for every virtual address we were asked to convert. This means that there were no segmentation faults.

It is possible for a page fault to occur, but we have not been given enough information to determine whether this is the case.