# Memory Virtualization
###### tags: `virtualization` `VM`
## MMU (memory management unit)
### memory management
* MMU hardware unit
* to help cpu to access memory
* Page table
* os maintains the page table
* the mmu just only references it
* function
* mapping between virtual addresses and physical addresses
* TLB (Translation Lookaside Buffer)
* hardware for page-table cache
* function
* a cache for virtual address translation
## Memory Virtualization
```
+----------------------------+
| |
| +-----------------------+ |
| | guest va | |
| +-----------+-----------+ |
| | |
| | mapping |
| v |
| |
| +-----------------------+ |
| | guest pa | |
| +-----------+-----------+ |
| | |
+----------------------------+
| va: virtual address
| pa: physical address
v mapping
+-----------------------------------------------+
| |
| host virtual memory address |
+--------------------------+--------------------+
|
| mapping
v
+-----------------------------------------------+
| |
| host physical memory address |
| |
+-----------------------------------------------+
```
* Address translation
* Control MMU that accesses translation tables in main memory.
* Memory protection
* Access attribute
* Define attribute and type of memory region to direct how memory operation to be handled.
### Shadow Page Tables
* use software to remapping the memory address
* direct mapping
```
+----------------------------+
| |
| +-----------------------+ |
| | guest va | |
| +-----------+-----------+ |
| | |
| | mapping |
| | |
| | |
| | |
+-------------|--------------+ pa: physical address
| va: virtual address
| (shadow page table on VMM)
v mapping
+-------------+---------------------------------+
| |
| host physical memory address |
| |
+-----------------------------------------------+
```
* can use TLB to tranlation
### Virtualizing MMU
1. When a guest OS is activated, the real PTBR points to the corresponding shadow page table of the guest OS.
2. When the guest OS attempts to modify the PTBR, it will be intercepted by VMM for further emulation.
```
+----------------------------+
| guest +------------------------+
| | |
| | |
| | |
| | |
| modify | |
| PTBR X<-------------- - -X- - - +
| | +
| | | |
+----------------------------+ |
| |
| +------------+
| |
+-----------------------------------------------+
| | | VMM |
| | | |
| | v |
| PTBR V intercept the modify |
| +---+-----+ |
| | shadow | |
| | page | |
| | table | |
| +---------+ |
+-----------------------------------------------+
```
### Construct Shadow Page Table
1. In real or guest OS, the OS maintains its own page table for each process.
2. VMM creates shadow page table for each guest page table
3. Maintenance
* Mark those physical frames used as guest page tables read-only, so that when a guest OS tries to modify its guest page table, an exception would be triggered.

### Page-fault Handling
A page fault caused by guest OS would launch the walking process

1. Walk page tables on guest OS
2. Check the permission on guest
3. Offset shift: GPA(guest physical address) to HVA(host virtual address)
4. Walk page tables on VMM
5. Check the permission on VMM
6. New entry established!
7. Invalidate the TLB entry