Skip to content

Commit

Permalink
PA3 : lazy allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
i5046821854 committed Mar 5, 2023
1 parent 75b30c1 commit 8a7df5b
Show file tree
Hide file tree
Showing 2 changed files with 416 additions and 2 deletions.
23 changes: 21 additions & 2 deletions trap.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ extern uint vectors[]; // in vectors.S: array of 256 entry pointers
struct spinlock tickslock;
uint ticks;

int mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm);


void
tvinit(void)
{
Expand All @@ -36,6 +39,7 @@ idtinit(void)
void
trap(struct trapframe *tf)
{
char * mem;
if(tf->trapno == T_SYSCALL){
if(myproc()->killed)
exit();
Expand Down Expand Up @@ -77,9 +81,24 @@ trap(struct trapframe *tf)
cpuid(), tf->cs, tf->eip);
lapiceoi();
break;

case T_PGFLT:
mem = kalloc();
uint addr = PGROUNDDOWN(rcr2());
if(mem == 0){
cprintf("out of memory during alloc\n");
break;
}
memset(mem, 0, PGSIZE);
if(mappages(myproc()->pgdir, (char*) addr, PGSIZE, V2P(mem), PTE_W | PTE_U) < 0)
{
cprintf("out of memory during alloc\n");
kfree(mem);
break;
}
break;
//PAGEBREAK: 13
default:

default:
if(myproc() == 0 || (tf->cs&3) == 0){
// In kernel, it must be our mistake.
cprintf("unexpected trap %d from cpu %d eip %x (cr2=0x%x)\n",
Expand Down
Loading

0 comments on commit 8a7df5b

Please sign in to comment.