在 user task exit 時,需要將在 user space 使用的 page 以及對應的 page entry 做回收,這件事是由即將 exit 的 user task 自己處理。kernel space 所用到的 page (e.g. kernel stack) 會由 task reaper 回收。
1
2
3
4
5
6
7
8
9
| void do_exit(int status) {
current_task->state = ZOMBIE;
current_task->exit_status = status;
// reclaim all allocated pages
page_reclaim(current_task->mm.pgd);
schedule();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| void page_free(void* page_virt_addr) {
uint64_t pfn = phy_to_pfn(virtual_to_physical((uint64_t)page_virt_addr));
page[pfn].used = AVAL;
remain_page++;
}
void page_reclaim_pte(uint64_t pte_phy) {
uint64_t* pte = (uint64_t*)(pte_phy | KERNEL_VIRT_BASE);
for (int i = 0; i < 512; i++) {
if (pte[i]) {
void* page = (void*)((pte[i] & ~0xFFF) | KERNEL_VIRT_BASE);
page_free(page);
}
}
page_free(pte);
}
void page_reclaim_pmd(uint64_t pmd_phy) {
uint64_t* pmd = (uint64_t*)(pmd_phy | KERNEL_VIRT_BASE);
for (int i = 0; i < 512; i++) {
if (pmd[i]) {
page_reclaim_pte(pmd[i] & ~0xFFF);
}
}
page_free(pmd);
}
void page_reclaim_pud(uint64_t pud_phy) {
uint64_t* pud = (uint64_t*)(pud_phy | KERNEL_VIRT_BASE);
for (int i = 0; i < 512; i++) {
if (pud[i]) {
page_reclaim_pmd(pud[i] & ~0xFFF);
}
}
page_free(pud);
}
void page_reclaim(uint64_t pgd_phy) {
uint64_t* pgd = (uint64_t*)(pgd_phy | KERNEL_VIRT_BASE);
for (int i = 0; i < 512; i++) {
if (pgd[i]) {
page_reclaim_pud(pgd[i] & ~0xFFF);
}
}
page_free(pgd);
}
|