Scenario: a process acquired a new block of memory, then try to access part of this block:
1 2 3 |
|
What happens in malloc():
- stdlib will try to handle it internally, if can’t satisfy this request, then,
- Call sbrk() to let kernel enlarge current process’s heap space (by adjust process’s VMAs).
What happens next when try to access the memory:
- CPU’s MMU use this virtual address to look up in current CPU’s TLB, not found.
- Then MMU switch to look up this address in process’s Page Table, try to do the virtual-to-physical address translation.
- Step 2 will fail, a Page Fault exception happens.
- In Page Fault exception handler, check if current process has write permission to this address, that’s done by check process’s VMA list.
- Allocate a physical page, update process’s Page Table for this page.
- After the exception handler returned, and the process get scheduled to execute again, it will retry the instruction that caused the Page Fault, this time will get correct physical address pointed to the new page. Update TLB entry for this new map.
- Select proper line in Cache for this physical address, write the new value to Cache, then hardware will write back this new value from Cache to real memory at proper time.