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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
@@ -171,6 +171,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); +uint64 lazyalloc(struct proc * p, uint64 va); // plic.c void plicinit(void);
@@ -43,12 +43,15 @@ sys_sbrk(void) { int addr; int n; + struct proc *p = myproc(); if(argint(0, &n) < 0) return -1; - addr = myproc()->sz; - if(growproc(n) < 0) - return -1; + addr = p->sz; // old sz + p->sz += n; + if (n < 0) { + uvmdealloc(p->pagetable, addr, p->sz); + } return addr; }
@@ -67,6 +67,11 @@ usertrap(void) syscall(); } else if((which_dev = devintr()) != 0){ // ok + } else if((r_scause() == 13) || (r_scause() == 15)){ // page fault + // lazy allocation + if (lazyalloc(myproc(), r_stval()) <= 0) { + p->killed = 1; + } } else { printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid); printf(" sepc=%p stval=%p\n", r_sepc(), r_stval());
@@ -5,6 +5,8 @@ #include "riscv.h" #include "defs.h" #include "fs.h" +#include "spinlock.h" +#include "proc.h" /* * the kernel's page table. @@ -102,12 +104,19 @@ walkaddr(pagetable_t pagetable, uint64 va) pte = walk(pagetable, va, 0); if(pte == 0) - return 0; + goto lzac; if((*pte & PTE_V) == 0) - return 0; + goto lzac; if((*pte & PTE_U) == 0) - return 0; + goto lzac; pa = PTE2PA(*pte); + + if (0) { +lzac: + if ((pa = lazyalloc(myproc(), va)) <= 0) + pa = 0; + } + return pa; } @@ -157,7 +166,8 @@ mappages(pagetable_t pagetable, uint64 va, uint64 size, uint64 pa, int perm) if((pte = walk(pagetable, a, 1)) == 0) return -1; if(*pte & PTE_V) - panic("remap"); + // panic("remap"); + ; *pte = PA2PTE(pa) | perm | PTE_V; if(a == last) break; @@ -180,10 +190,14 @@ uvmunmap(pagetable_t pagetable, uint64 va, uint64 npages, int do_free) panic("uvmunmap: not aligned"); for(a = va; a < va + npages*PGSIZE; a += PGSIZE){ - if((pte = walk(pagetable, a, 0)) == 0) - panic("uvmunmap: walk"); - if((*pte & PTE_V) == 0) - panic("uvmunmap: not mapped"); + if((pte = walk(pagetable, a, 0)) == 0) { + // panic("uvmunmap: walk"); + continue; + } + if((*pte & PTE_V) == 0) { + // panic("uvmunmap: not mapped"); + continue; + } if(PTE_FLAGS(*pte) == PTE_V) panic("uvmunmap: not a leaf"); if(do_free){ @@ -314,10 +328,14 @@ uvmcopy(pagetable_t old, pagetable_t new, uint64 sz) char *mem; for(i = 0; i < sz; i += PGSIZE){ - if((pte = walk(old, i, 0)) == 0) - panic("uvmcopy: pte should exist"); - if((*pte & PTE_V) == 0) - panic("uvmcopy: page not present"); + if((pte = walk(old, i, 0)) == 0){ + // panic("uvmcopy: pte should exist"); + continue; + } + if((*pte & PTE_V) == 0){ + // panic("uvmcopy: page not present"); + continue; + } pa = PTE2PA(*pte); flags = PTE_FLAGS(*pte); if((mem = kalloc()) == 0) @@ -440,3 +458,40 @@ copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max) return -1; } } + + +// lazy allocation memory va for proc p: handle page-fault. +// return allocated memory (pa), 0 for failed +uint64 lazyalloc(struct proc * p, uint64 va){ +#define lazyalloc_debug 0 +#define lazyalloc_warn(info) { \ + printf("lazyalloc(): %s", info); \ + printf(" scause %p pid=%d\n", r_scause(), p->pid); \ + printf(" sepc=%p stval=%p\n", r_sepc(), r_stval()); \ +} + if(va >= p->sz || va < PGROUNDUP(p->trapframe->sp)){ + #if lazyalloc_debug + lazyalloc_warn("vm addr higher then any allocated with sbrk\n"); + #endif + return 0; + } + char * mem; + uint64 a = PGROUNDDOWN(va); + mem = kalloc(); + if(mem == 0){ + #if lazyalloc_debug + lazyalloc_warn("kalloc() == 0\n"); + #endif + return 0; + } + memset(mem, 0, PGSIZE); + if(mappages(p->pagetable, a, PGSIZE, (uint64)mem, PTE_W|PTE_X|PTE_R|PTE_U) != 0){ + #if lazyalloc_debug + lazyalloc_warn("mappages() != 0\n"); + #endif + kfree(mem); + return 0; + } + + return (uint64)mem; +}
|