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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
| diff --git a/Makefile b/Makefile index d8509b1..cb5e39b 100644
@@ -175,6 +175,7 @@ UPROGS=\ $U/_grind\ $U/_wc\ $U/_zombie\ + $U/_mmaptest\ diff --git a/kernel/defs.h b/kernel/defs.h index 41098f4..a8de111 100644
@@ -171,6 +171,8 @@ 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); +int mmapalloc(struct proc*, uint64); +int mmapdealloc(struct proc*, uint64, int); // plic.c void plicinit(void); diff --git a/kernel/proc.c b/kernel/proc.c index ba1a9e3..176e629 100644
@@ -5,6 +5,7 @@ #include "spinlock.h" #include "proc.h" #include "defs.h" +#include "fcntl.h" struct cpu cpus[NCPU]; @@ -298,6 +299,20 @@ fork(void) safestrcpy(np->name, p->name, sizeof(p->name)); + // the child has the same mapped regions as the parent + // Don't forget to increment the reference count for a VMA's struct file + for(int i = 0; i < 16; i++){ + if(p->vmas[i].mapped == 1){ + np->vmas[i].addr = p->vmas[i].addr; + np->vmas[i].end = p->vmas[i].end; + np->vmas[i].length = p->vmas[i].length; + np->vmas[i].prot = p->vmas[i].prot; + np->vmas[i].flags = p->vmas[i].flags; + np->vmas[i].offset = p->vmas[i].offset; + np->vmas[i].f = filedup(p->vmas[i].f); + np->vmas[i].mapped = 1; + } + } pid = np->pid; np->state = RUNNABLE; @@ -353,6 +368,12 @@ exit(int status) } } + // unmap the process's mapped regions + for(int i = 0; i < 16; i++){ + if(p->vmas[i].mapped == 1){ + mmapdealloc(myproc(), p->vmas[i].addr, p->vmas[i].end - p->vmas[i].addr); + } + } begin_op(); iput(p->cwd); end_op(); diff --git a/kernel/proc.h b/kernel/proc.h index 9c16ea7..2997ae9 100644
@@ -80,6 +80,20 @@ struct trapframe { /* 280 */ uint64 t6; }; +// Define a structure corresponding to the VMA (virtual memory area) described in Lecture 15, +// recording the address, length, permissions, file, etc. +// for a virtual memory range created by mmap. +struct vma { + uint64 addr; + uint64 end; + int length; + int prot; + int flags; + int offset; + struct file *f; + int mapped; +}; + enum procstate { UNUSED, SLEEPING, RUNNABLE, RUNNING, ZOMBIE }; // Per-process state @@ -103,4 +117,5 @@ struct proc { struct file *ofile[NOFILE]; // Open files struct inode *cwd; // Current directory char name[16]; // Process name (debugging) + struct vma vmas[16]; // declare a fixed-size array of VMAs and allocate from that array as needed }; diff --git a/kernel/syscall.c b/kernel/syscall.c index c1b3670..7320633 100644
@@ -104,6 +104,8 @@ extern uint64 sys_unlink(void); extern uint64 sys_wait(void); extern uint64 sys_write(void); extern uint64 sys_uptime(void); +extern uint64 sys_mmap(void); +extern uint64 sys_munmap(void); static uint64 (*syscalls[])(void) = { [SYS_fork] sys_fork, @@ -127,6 +129,8 @@ static uint64 (*syscalls[])(void) = { [SYS_link] sys_link, [SYS_mkdir] sys_mkdir, [SYS_close] sys_close, +[SYS_mmap] sys_mmap, +[SYS_munmap] sys_munmap, }; void diff --git a/kernel/syscall.h b/kernel/syscall.h index bc5f356..e7b18d6 100644
@@ -20,3 +20,5 @@ #define SYS_link 19 #define SYS_mkdir 20 #define SYS_close 21 +#define SYS_mmap 22 +#define SYS_munmap 23 diff --git a/kernel/sysfile.c b/kernel/sysfile.c index 5dc453b..22d194b 100644
@@ -484,3 +484,66 @@ sys_pipe(void) } return 0; } + +// map files into process address spaces, +// and as part of user-level page fault schemes +uint64 +sys_mmap() +{ + uint64 addr; + int length, prot, flags, fd, offset; + if(argaddr(0, &addr) || argint(1, &length) < 0 || argint(2, &prot) < 0 || + argint(3, &flags) < 0 || argint(4, &fd) < 0 || argint(5, &offset) < 0) + return 0xffffffffffffffff; + + // addr will always be zero, assume offset is zero + if(addr != 0 || offset != 0) + return -1; + + struct proc *p = myproc(); + struct file *f = p->ofile[fd]; + + // if file is read-only, but mmap allow write or shared, then return error + if(!f->writable && (prot & PROT_WRITE) && (flags & MAP_SHARED)) + return 0xffffffffffffffff; + + // find an unused region in the process's address space in which to map the file + int i = 0; + for (; i < 16; i++){ + if(p->vmas[i].mapped == 0) + break; + } + if(i == 16) // no free VMA to use + return 0xffffffffffffffff; + + filedup(f); // increment referent count for file + p->vmas[i].addr = (uint64)p->sz; + p->vmas[i].end = PGROUNDUP(p->sz + length); + p->vmas[i].length = length; + p->vmas[i].prot = prot; + p->vmas[i].flags = flags; + p->vmas[i].offset = offset; + p->vmas[i].f = f; + p->vmas[i].mapped = 1; + + // laze allcation by page fault + p->sz += length; + + return p->vmas[i].addr; +} + +// remove mmap mappings in the indicated address range +uint64 +sys_munmap() +{ + uint64 addr; + int length; + if(argaddr(0, &addr) < 0 || argint(1, &length) < 0) + return -1; + + if(mmapdealloc(myproc(), addr, length) < 0){ + return -1; + } + + return 0; +} diff --git a/kernel/trap.c b/kernel/trap.c index a63249e..b6f56e5 100644
@@ -67,6 +67,11 @@ usertrap(void) syscall(); } else if((which_dev = devintr()) != 0){ // ok + } else if(r_scause() == 0xd || r_scause() == 0xf){ + uint64 va = r_stval(); + if(mmapalloc(p, va) < 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()); diff --git a/kernel/vm.c b/kernel/vm.c index b47f111..6d001d8 100644
@@ -5,6 +5,11 @@ #include "riscv.h" #include "defs.h" #include "fs.h" +#include "spinlock.h" +#include "proc.h" +#include "sleeplock.h" +#include "file.h" +#include "fcntl.h" /* * the kernel's page table. @@ -146,7 +151,7 @@ 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"); + ; *pte = PA2PTE(pa) | perm | PTE_V; if(a == last) break; @@ -170,9 +175,9 @@ uvmunmap(pagetable_t pagetable, uint64 va, uint64 npages, int do_free) for(a = va; a < va + npages*PGSIZE; a += PGSIZE){ 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){ @@ -304,9 +309,9 @@ uvmcopy(pagetable_t old, pagetable_t new, uint64 sz) for(i = 0; i < sz; i += PGSIZE){ 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) @@ -429,3 +434,97 @@ copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max) return -1; } } + +// allocate memory for mmap +int +mmapalloc(struct proc *p, uint64 va) +{ + // check va is vaild + va = PGROUNDDOWN(va); + if(va >= p->sz || va < p->trapframe->sp) + return -1; + + int i = 0; + for(; i < 16; i++){ + if(p->vmas[i].mapped && va >= p->vmas[i].addr && va < p->vmas[i].end) + break; + } + if(i == 16) + return -1; + + char *pa = kalloc(); + if(pa == 0) // no free physical memory + return -1; + memset(pa, 0, PGSIZE); + + // map it into the user address space + // set the permissions correctly on the page + int prot = p->vmas[i].prot; + uint64 flags = PTE_U; + if(prot & PROT_READ) + flags |= PTE_R; + if(prot & PROT_WRITE) + flags |= PTE_W; + if(prot & PROT_EXEC) + flags |= PTE_X; + if(mappages(p->pagetable, va, PGSIZE, (uint64)pa, flags) != 0){ + kfree(pa); + return -1; + } + + // read 4096 bytes of the relevant file into that page + struct inode *ip = p->vmas[i].f->ip; + begin_op(); + ilock(ip); + if(readi(ip, 1, va, va - p->vmas[i].addr, PGSIZE) < 0){ + iunlock(ip); + end_op(); + return -1; + } + iunlock(ip); + end_op(); + return 0; +} + +int +mmapdealloc(struct proc *p, uint64 va, int length) +{ + // find the VMA for the address range + int i = 0; + for(; i < 16; i++){ + if(p->vmas[i].mapped == 1 && va >= p->vmas[i].addr && va + length <= p->vmas[i].end) + break; + } + if(i == 16) + return -1; + + // the file is mapped MAP_SHARED, write the page back to the file + if((p->vmas[i].flags & MAP_SHARED) && p->vmas[i].f->writable){ + struct inode *ip = p->vmas[i].f->ip; + begin_op(); + ilock(ip); + if(writei(ip, 1, va, 0, length) < 0){ + iunlock(ip); + end_op(); + return -1; + } + iunlock(ip); + end_op(); + } + + // unmap the specified pages + uvmunmap(p->pagetable, va, length / PGSIZE, 1); + + // munmap removes all pages of a previous mmap, it should + // decrement the reference count of the corresponding struct file + if(length == p->vmas[i].end - p->vmas[i].addr){ + fileclose(p->vmas[i].f); + p->vmas[i].mapped = 0; + p->sz -= length; + return 0; + } + + p->vmas[i].addr += length; + + return 0; +} diff --git a/user/user.h b/user/user.h index b71ecda..5648874 100644
@@ -23,6 +23,8 @@ int getpid(void); char* sbrk(int); int sleep(int); int uptime(void); +void* mmap(void*, int, int, int, int, int); +int munmap(void*, uint); // ulib.c int stat(const char*, struct stat*); diff --git a/user/usys.pl b/user/usys.pl index 01e426e..d23b9cc 100755
@@ -36,3 +36,5 @@ entry("getpid"); entry("sbrk"); entry("sleep"); entry("uptime"); +entry("mmap"); +entry("munmap");
|