Skip to content

Commit

Permalink
lab print a page table finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Nowherechan committed Mar 5, 2023
1 parent 07b142d commit 6ad90d2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions kernel/defs.h
Expand Up @@ -173,6 +173,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, int level); // for pgtbl-lab 2: Print a page table

// plic.c
void plicinit(void);
Expand Down
5 changes: 5 additions & 0 deletions kernel/exec.c
Expand Up @@ -128,6 +128,11 @@ exec(char *path, char **argv)
p->trapframe->sp = sp; // initial stack pointer
proc_freepagetable(oldpagetable, oldsz);

// pgtbl-lab2: Print a page table
if (p->pid == 1) {
vmprint(p->pagetable, 0);
}

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
Expand Up @@ -437,3 +437,29 @@ copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max)
return -1;
}
}

// Created for pgtbl-lab 2: Print a page table
// Take a pagetable_t argument
// Print the contents in the certain format
void
vmprint(pagetable_t pagetable, int level)
{
if (!level)
printf("page table %p\n", pagetable);

// 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) {
uint64 child = PTE2PA(pte);
for (int j = level+1; j > 0; j--) {
printf(" ..");
}
printf("%d: pte %p pa %p\n", i, pte, child);
if ((pte & (PTE_R|PTE_W|PTE_X)) == 0) {
// this PTE points to a lower-level page table.
vmprint((pagetable_t)child, level+1);
}
}
}
}

0 comments on commit 6ad90d2

Please sign in to comment.