# Part 3: How Page Table is used

## How Page Table is used?

### Terms:

* *Physical memory allocation*: Use buddy allocation algorithm to alloc/free memory.
* *Kernel has own page table.*
* Each *process also has its own page table.*
* Hardware MMU translates VM to PM, and each CPU processor has a *satp register* to save root page-table page.

### Questions:&#x20;

How do we make sure process page tables and kernel’s page table not pointing to the same physical memory address? Are they all use satp to find address?

### Solution:

![](/files/-M4bvJKCjfEuJKOmNOf7)

* Each process, and kernel all have its own Page Table.
* Switching between user process or to/from kernel needs to set satp register to the address of the root page-table page.
* By using MMU, translate the virtual memory to physical memory address. (See page tables Part 1 for details)
* Note: satp register stores one root page table only.&#x20;

{% hint style="info" %}
**User space to kernel space needs switching process page table to kernel page table.** <br>

**Context switch between process save/restore registers. It is handled by the kernel, so the page table is switched among processes.**

**Example: Timer interrupt on P1. Page table is switched from P1’s to kernel’s. Then the CPU scheduler finds another P2 to run. P2 page table is reset when returning from kernel space to P2 user space.**
{% endhint %}

### **Ask yourself**&#x20;

1\. How MMU work&#x20;

2\. Kernel page table, direct mapping&#x20;

3\. How to turn on page? By storing address of root page-table page to satp register&#x20;

4\. Create process kernel stack, used for saving context&#x20;

5\. Process has own page table&#x20;

6\. Buddy allocator is alloc/free physical address&#x20;

7\. How buddy allocator works?&#x20;

8\. All allocation/free physical memory in os is done by buddy allocator.&#x20;

9\. How to print page table? 3 nested level.&#x20;

10\. How to do lazy page allocation in user space?


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiayingp.gitbook.io/build_a_os/virtual-memory/page-table/part-3-how-page-table-is-used.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
