MIT6.S081 lab3 page tables

lab3主要是帮助复习页表、PTE、物理页之间的关系。

Speed up system calls

When each process is created, map one read-only page at USYSCALL (a VA defined in memlayout.h). At the start of this page, store a struct usyscall (also defined in memlayout.h), and initialize it to store the PID of the current process.

实验指导已经很直白了,在进程创建时映射一块共享页,用来存储进程id。过程:从freelist分配一页空间,然后映射到用户进程指定地址,然后初始化这个页(存入当前进程的pid)。

Print a page table

实际上就是熟悉三级页表的过程。

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
void recursive_vmprint(pagetable_t pagetable, int level){
if(level>=3) return;
char* prefix = "x";
switch (level) {
case 0:
prefix = "..";
break;
case 1:
prefix = ".. ..";
break;
case 2:
prefix = ".. .. ..";
}
for(int i=0; i<512; ++i){
pte_t pte =pagetable[i];
if(pte & PTE_V){
uint64 child = PTE2PA(pte);
printf("%s%d: pte %p pa %p\n", prefix, i, pte, child);
recursive_vmprint((pagetable_t) child, level+1);
}
}
}
void vmprint(pagetable_t pagetable){
// 深度优先遍历,打印页表
printf("page table %p\n", pagetable);
recursive_vmprint(pagetable, 0);
}

从PTE到PA的过程 uint64 child = PTE2PA(pte);,然后将它用作页表入口地址(pagetable_t) child

1
2
typedef uint64 pte_t;
typedef uint64* pagetable_t;

xv6中的指针就是64位无符号整数。

Detecting which pages have been accessed

阅读手册可以知道PTE的第6位是访问标志位,通过观察这个标志就可以知道这个页面是否被访问。

硬件在执行访存指令后就会自动把相应页的PTE_A置为1.

注意:Be sure to clear PTE_A after checking if it is set. Otherwise, it won’t be possible to determine if the page was accessed since the last time pgaccess() was called (i.e., the bit will be set forever).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int pgaccess(pagetable_t pagetable, uint64 addr, int n, uint64 uaddr) {
if (n > 64) {
panic("too much page to check\n");
return -1;
}
uint64 bitmask = 0;
int cur_bitmask = 1;
uint64 va = addr;
pte_t *pte;//由于修改bit位,必须采用指针方式
for (int i = 0; i < n; ++i) {
if ((pte = walk(pagetable, va, 0)) == 0) {
panic("pte should exits\n");
return -1;
}
if (*pte & PTE_A) {
bitmask |= (cur_bitmask << i);
*pte &= ~PTE_A;
}
va += PGSIZE;
}
copyout(pagetable, uaddr, (char *) &bitmask, sizeof(bitmask));
return 0;
}
作者

Desirer

发布于

2024-01-06

更新于

2024-02-02

许可协议