Skip to content

Commit

Permalink
Lab pgtbl: vmprint
Browse files Browse the repository at this point in the history
  • Loading branch information
foyoodo committed Mar 16, 2021
1 parent 773d70b commit 5d14570
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions kernel/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ uint64 walkaddr(pagetable_t, uint64);
int copyout(pagetable_t, uint64, char *, uint64);
int copyin(pagetable_t, char *, uint64, uint64);
int copyinstr(pagetable_t, char *, uint64, uint64);
void vmprint(pagetable_t);

// plic.c
void plicinit(void);
Expand Down
2 changes: 2 additions & 0 deletions kernel/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ exec(char *path, char **argv)
p->trapframe->sp = sp; // initial stack pointer
proc_freepagetable(oldpagetable, oldsz);

if (p->pid == 1) vmprint(p->pagetable);

return argc; // this ends up in a0, the first argument to main(argc, argv)

bad:
Expand Down
26 changes: 26 additions & 0 deletions kernel/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,29 @@ copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max)
return -1;
}
}

void
vmprintwalk(pagetable_t pagetable, int deep)
{
// there are 2^9 = 512 PTEs in a page table.
for (int i = 0; i < 512; i++) {
pte_t pte = pagetable[i];
if (pte & PTE_V) {
for (int i = 0; i <= deep; ++i) printf(".. ");
printf("%d: pte %p pa %p\n", i, pte, PTE2PA(pte));

if ((pte & (PTE_R | PTE_W | PTE_X)) == 0) {
// this PTE points to a lower-level page table.
uint64 child = PTE2PA(pte);
vmprintwalk((pagetable_t)child, deep + 1);
}
}
}
}

void
vmprint(pagetable_t pagetable)
{
printf("page table %p\n", pagetable);
vmprintwalk(pagetable, 0);
}

0 comments on commit 5d14570

Please sign in to comment.