vm.c revision 1.21 1 /* $NetBSD: vm.c,v 1.21 2007/11/04 19:52:14 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by Google Summer of Code.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * Virtual memory emulation routines. Contents:
32 * + UBC
33 * + anon objects & pager
34 * + vnode objects & pager
35 * + misc support routines
36 * + kmem
37 */
38
39 /*
40 * XXX: we abuse pg->uanon for the virtual address of the storage
41 * for each page. phys_addr would fit the job description better,
42 * except that it will create unnecessary lossage on some platforms
43 * due to not being a pointer type.
44 */
45
46 #include <sys/param.h>
47 #include <sys/null.h>
48 #include <sys/vnode.h>
49 #include <sys/buf.h>
50 #include <sys/kmem.h>
51
52 #include <uvm/uvm.h>
53 #include <uvm/uvm_prot.h>
54 #include <uvm/uvm_readahead.h>
55
56 #include <machine/pmap.h>
57
58 #include "rump_private.h"
59 #include "rumpuser.h"
60
61 /* dumdidumdum */
62 #define len2npages(off, len) \
63 (((((len) + PAGE_MASK) & ~(PAGE_MASK)) >> PAGE_SHIFT) \
64 + (((off & PAGE_MASK) + (len & PAGE_MASK)) > PAGE_SIZE))
65
66 struct uvm_pagerops uvm_vnodeops;
67 struct uvm_pagerops aobj_pager;
68 struct uvmexp uvmexp;
69 struct uvm uvm;
70
71 struct vmspace rump_vmspace;
72 struct vm_map rump_vmmap;
73
74 /*
75 * vm pages
76 */
77
78 struct vm_page *
79 rumpvm_makepage(struct uvm_object *uobj, voff_t off)
80 {
81 struct vm_page *pg;
82
83 pg = rumpuser_malloc(sizeof(struct vm_page), 0);
84 memset(pg, 0, sizeof(struct vm_page));
85 pg->offset = off;
86 pg->uobject = uobj;
87
88 pg->uanon = (void *)rumpuser_malloc(PAGE_SIZE, 0);
89 memset((void *)pg->uanon, 0, PAGE_SIZE);
90 pg->flags = PG_CLEAN;
91
92 simple_lock(&uobj->vmobjlock);
93 TAILQ_INSERT_TAIL(&uobj->memq, pg, listq);
94 simple_unlock(&uobj->vmobjlock);
95
96 return pg;
97 }
98
99 /*
100 * Release a page.
101 *
102 * Called with the vm object locked. Returns with the lock released.
103 */
104 void
105 rumpvm_freepage(struct vm_page *pg)
106 {
107 struct uvm_object *uobj = pg->uobject;
108
109 TAILQ_REMOVE(&uobj->memq, pg, listq);
110 simple_unlock(&uobj->vmobjlock);
111
112 rumpuser_free((void *)pg->uanon);
113 rumpuser_free(pg);
114 }
115
116 struct rumpva {
117 vaddr_t addr;
118 struct vm_page *pg;
119
120 LIST_ENTRY(rumpva) entries;
121 };
122 static LIST_HEAD(, rumpva) rvahead = LIST_HEAD_INITIALIZER(rvahead);
123 static kmutex_t rvamtx;
124
125 void
126 rumpvm_enterva(vaddr_t addr, struct vm_page *pg)
127 {
128 struct rumpva *rva;
129
130 rva = rumpuser_malloc(sizeof(struct rumpva), 0);
131 rva->addr = addr;
132 rva->pg = pg;
133 mutex_enter(&rvamtx);
134 LIST_INSERT_HEAD(&rvahead, rva, entries);
135 mutex_exit(&rvamtx);
136 }
137
138 void
139 rumpvm_flushva()
140 {
141 struct rumpva *rva;
142
143 mutex_enter(&rvamtx);
144 while ((rva = LIST_FIRST(&rvahead)) != NULL) {
145 LIST_REMOVE(rva, entries);
146 rumpuser_free(rva);
147 }
148 mutex_exit(&rvamtx);
149 }
150
151 /*
152 * vnode pager
153 */
154
155 static int
156 vn_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
157 int *npages, int centeridx, vm_prot_t access_type,
158 int advice, int flags)
159 {
160 struct vnode *vp = (struct vnode *)uobj;
161
162 return VOP_GETPAGES(vp, off, pgs, npages, centeridx, access_type,
163 advice, flags);
164 }
165
166 static int
167 vn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
168 {
169 struct vnode *vp = (struct vnode *)uobj;
170
171 return VOP_PUTPAGES(vp, offlo, offhi, flags);
172 }
173
174 /*
175 * Anon object stuff
176 */
177
178 static int
179 ao_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
180 int *npages, int centeridx, vm_prot_t access_type,
181 int advice, int flags)
182 {
183 struct vm_page *pg;
184 int i;
185
186 if (centeridx)
187 panic("%s: centeridx != 0 not supported", __func__);
188
189 /* loop over pages */
190 off = trunc_page(off);
191 for (i = 0; i < *npages; i++) {
192 pg = uvm_pagelookup(uobj, off + (i << PAGE_SHIFT));
193 if (pg) {
194 pgs[i] = pg;
195 } else {
196 pg = rumpvm_makepage(uobj, off + (i << PAGE_SHIFT));
197 pgs[i] = pg;
198 }
199 }
200
201 return 0;
202
203 }
204
205 static int
206 ao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
207 {
208 struct vm_page *pg;
209
210 /* we only free all pages for now */
211 if ((flags & PGO_FREE) == 0 || (flags & PGO_ALLPAGES) == 0)
212 return 0;
213
214 simple_lock(&uobj->vmobjlock);
215 while ((pg = TAILQ_FIRST(&uobj->memq)) != NULL)
216 rumpvm_freepage(pg);
217 simple_unlock(&uobj->vmobjlock);
218
219 return 0;
220 }
221
222 struct uvm_object *
223 uao_create(vsize_t size, int flags)
224 {
225 struct uvm_object *uobj;
226
227 uobj = rumpuser_malloc(sizeof(struct uvm_object), 0);
228 memset(uobj, 0, sizeof(struct uvm_object));
229 uobj->pgops = &aobj_pager;
230 TAILQ_INIT(&uobj->memq);
231
232 return uobj;
233 }
234
235 void
236 uao_detach(struct uvm_object *uobj)
237 {
238
239 ao_put(uobj, 0, 0, PGO_ALLPAGES | PGO_FREE);
240 rumpuser_free(uobj);
241 }
242
243 /*
244 * UBC
245 */
246
247 struct ubc_window {
248 struct uvm_object *uwin_obj;
249 voff_t uwin_off;
250 uint8_t *uwin_mem;
251 size_t uwin_mapsize;
252
253 LIST_ENTRY(ubc_window) uwin_entries;
254 };
255
256 static LIST_HEAD(, ubc_window) uwinlst = LIST_HEAD_INITIALIZER(uwinlst);
257 static kmutex_t uwinmtx;
258
259 int
260 rump_ubc_magic_uiomove(void *va, size_t n, struct uio *uio, int *rvp,
261 struct ubc_window *uwinp)
262 {
263 struct vm_page **pgs;
264 int npages = len2npages(uio->uio_offset, n);
265 int i, rv;
266
267 if (uwinp == NULL) {
268 mutex_enter(&uwinmtx);
269 LIST_FOREACH(uwinp, &uwinlst, uwin_entries)
270 if ((uint8_t *)va >= uwinp->uwin_mem
271 && (uint8_t *)va
272 < (uwinp->uwin_mem + uwinp->uwin_mapsize))
273 break;
274 mutex_exit(&uwinmtx);
275 if (uwinp == NULL) {
276 KASSERT(rvp != NULL);
277 return 0;
278 }
279 }
280
281 pgs = rumpuser_malloc(npages * sizeof(pgs), 0);
282 memset(pgs, 0, sizeof(pgs));
283 rv = uwinp->uwin_obj->pgops->pgo_get(uwinp->uwin_obj,
284 uwinp->uwin_off + ((uint8_t *)va - uwinp->uwin_mem),
285 pgs, &npages, 0, 0, 0, 0);
286 if (rv)
287 goto out;
288
289 for (i = 0; i < npages; i++) {
290 size_t xfersize;
291 off_t pageoff;
292
293 pageoff = uio->uio_offset & PAGE_MASK;
294 xfersize = MIN(MIN(n, PAGE_SIZE), PAGE_SIZE-pageoff);
295 uiomove((uint8_t *)pgs[i]->uanon + pageoff, xfersize, uio);
296 if (uio->uio_rw == UIO_WRITE)
297 pgs[i]->flags &= ~PG_CLEAN;
298 n -= xfersize;
299 }
300
301 out:
302 rumpuser_free(pgs);
303 if (rvp)
304 *rvp = rv;
305 return 1;
306 }
307
308 static struct ubc_window *
309 uwin_alloc(struct uvm_object *uobj, voff_t off, vsize_t len)
310 {
311 struct ubc_window *uwinp; /* pronounced: you wimp! */
312
313 uwinp = kmem_alloc(sizeof(struct ubc_window), KM_SLEEP);
314 uwinp->uwin_obj = uobj;
315 uwinp->uwin_off = off;
316 uwinp->uwin_mapsize = len;
317 uwinp->uwin_mem = kmem_alloc(len, KM_SLEEP);
318
319 return uwinp;
320 }
321
322 static void
323 uwin_free(struct ubc_window *uwinp)
324 {
325
326 kmem_free(uwinp->uwin_mem, uwinp->uwin_mapsize);
327 kmem_free(uwinp, sizeof(struct ubc_window));
328 }
329
330 void *
331 ubc_alloc(struct uvm_object *uobj, voff_t offset, vsize_t *lenp, int advice,
332 int flags)
333 {
334 struct ubc_window *uwinp;
335
336 uwinp = uwin_alloc(uobj, offset, *lenp);
337 mutex_enter(&uwinmtx);
338 LIST_INSERT_HEAD(&uwinlst, uwinp, uwin_entries);
339 mutex_exit(&uwinmtx);
340
341 DPRINTF(("UBC_ALLOC offset 0x%llx, uwin %p, mem %p\n",
342 (unsigned long long)offset, uwinp, uwinp->uwin_mem));
343
344 return uwinp->uwin_mem;
345 }
346
347 void
348 ubc_release(void *va, int flags)
349 {
350 struct ubc_window *uwinp;
351
352 mutex_enter(&uwinmtx);
353 LIST_FOREACH(uwinp, &uwinlst, uwin_entries)
354 if ((uint8_t *)va >= uwinp->uwin_mem
355 && (uint8_t *)va < (uwinp->uwin_mem + uwinp->uwin_mapsize))
356 break;
357 mutex_exit(&uwinmtx);
358 if (uwinp == NULL)
359 panic("%s: releasing invalid window at %p", __func__, va);
360
361 LIST_REMOVE(uwinp, uwin_entries);
362 uwin_free(uwinp);
363 }
364
365 int
366 ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
367 int advice, int flags)
368 {
369 struct ubc_window *uwinp;
370 vsize_t len;
371
372 while (todo > 0) {
373 len = todo;
374
375 uwinp = uwin_alloc(uobj, uio->uio_offset, len);
376 rump_ubc_magic_uiomove(uwinp->uwin_mem, len, uio, NULL, uwinp);
377 uwin_free(uwinp);
378
379 todo -= len;
380 }
381 return 0;
382 }
383
384
385 /*
386 * Misc routines
387 */
388
389 void
390 rumpvm_init()
391 {
392
393 uvm_vnodeops.pgo_get = vn_get;
394 uvm_vnodeops.pgo_put = vn_put;
395 aobj_pager.pgo_get = ao_get;
396 aobj_pager.pgo_put = ao_put;
397
398 uvmexp.free = 1024*1024; /* XXX */
399 uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
400
401 mutex_init(&rvamtx, MUTEX_DEFAULT, 0);
402 mutex_init(&uwinmtx, MUTEX_DEFAULT, 0);
403 }
404
405 void
406 uvm_pageactivate(struct vm_page *pg)
407 {
408
409 /* nada */
410 }
411
412 void
413 uvm_page_unbusy(struct vm_page **pgs, int npgs)
414 {
415
416 /* nada */
417 }
418
419 void
420 uvm_pagewire(struct vm_page *pg)
421 {
422
423 /* nada */
424 }
425
426 void
427 uvm_pageunwire(struct vm_page *pg)
428 {
429
430 /* nada */
431 }
432
433 vaddr_t
434 uvm_pagermapin(struct vm_page **pps, int npages, int flags)
435 {
436
437 panic("%s: unimplemented", __func__);
438 }
439
440 struct vm_page *
441 uvm_pagelookup(struct uvm_object *uobj, voff_t off)
442 {
443 struct vm_page *pg;
444
445 simple_lock(&uobj->vmobjlock);
446 TAILQ_FOREACH(pg, &uobj->memq, listq) {
447 if (pg->offset == off) {
448 simple_unlock(&uobj->vmobjlock);
449 return pg;
450 }
451 }
452 simple_unlock(&uobj->vmobjlock);
453
454 return NULL;
455 }
456
457 struct vm_page *
458 uvm_pageratop(vaddr_t va)
459 {
460 struct rumpva *rva;
461
462 mutex_enter(&rvamtx);
463 LIST_FOREACH(rva, &rvahead, entries)
464 if (rva->addr == va)
465 break;
466 mutex_exit(&rvamtx);
467
468 if (rva == NULL)
469 panic("%s: va %llu", __func__, (unsigned long long)va);
470
471 return rva->pg;
472 }
473
474 void
475 uvm_estimatepageable(int *active, int *inactive)
476 {
477
478 /* XXX: guessing game */
479 *active = 1024;
480 *inactive = 1024;
481 }
482
483 void
484 uvm_aio_biodone1(struct buf *bp)
485 {
486
487 panic("%s: unimplemented", __func__);
488 }
489
490 void
491 uvm_aio_biodone(struct buf *bp)
492 {
493
494 uvm_aio_aiodone(bp);
495 }
496
497 void
498 uvm_aio_aiodone(struct buf *bp)
499 {
500
501 if ((bp->b_flags & (B_READ | B_NOCACHE)) == 0 && bioopsp)
502 bioopsp->io_pageiodone(bp);
503 }
504
505 void
506 uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
507 {
508
509 vp->v_size = vp->v_writesize = newsize;
510 }
511
512 void
513 uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
514 {
515
516 vp->v_writesize = newsize;
517 }
518
519 void
520 uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
521 {
522 struct uvm_object *uobj = &vp->v_uobj;
523 struct vm_page **pgs;
524 int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
525 int rv, npages, i;
526
527 pgs = rumpuser_malloc(maxpages * sizeof(pgs), 0);
528 while (len) {
529 npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
530 memset(pgs, 0, npages * sizeof(struct vm_page *));
531 rv = uobj->pgops->pgo_get(uobj, off, pgs, &npages, 0, 0, 0, 0);
532 assert(npages > 0);
533
534 for (i = 0; i < npages; i++) {
535 uint8_t *start;
536 size_t chunkoff, chunklen;
537
538 chunkoff = off & PAGE_MASK;
539 chunklen = MIN(PAGE_SIZE - chunkoff, len);
540 start = (uint8_t *)pgs[i]->uanon + chunkoff;
541
542 memset(start, 0, chunklen);
543 pgs[i]->flags &= PG_CLEAN;
544
545 off += chunklen;
546 len -= chunklen;
547 }
548 }
549 rumpuser_free(pgs);
550
551 return;
552 }
553
554 struct uvm_ractx *
555 uvm_ra_allocctx()
556 {
557
558 return NULL;
559 }
560
561 void
562 uvm_ra_freectx(struct uvm_ractx *ra)
563 {
564
565 return;
566 }
567
568 bool
569 uvn_clean_p(struct uvm_object *uobj)
570 {
571 struct vnode *vp = (void *)uobj;
572
573 return (vp->v_iflag & VI_ONWORKLST) == 0;
574 }
575
576 /*
577 * Kmem
578 */
579
580 void *
581 kmem_alloc(size_t size, km_flag_t kmflag)
582 {
583
584 return rumpuser_malloc(size, kmflag == KM_NOSLEEP);
585 }
586
587 void *
588 kmem_zalloc(size_t size, km_flag_t kmflag)
589 {
590 void *rv;
591
592 rv = kmem_alloc(size, kmflag);
593 if (rv)
594 memset(rv, 0, size);
595
596 return rv;
597 }
598
599 void
600 kmem_free(void *p, size_t size)
601 {
602
603 rumpuser_free(p);
604 }
605
606 /*
607 * UVM km
608 */
609
610 vaddr_t
611 uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
612 {
613 void *rv;
614
615 rv = rumpuser_malloc(size, flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT));
616 if (rv && flags & UVM_KMF_ZERO)
617 memset(rv, 0, size);
618
619 return (vaddr_t)rv;
620 }
621
622 void
623 uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
624 {
625
626 rumpuser_free((void *)vaddr);
627 }
628
629 struct vm_map *
630 uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
631 vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
632 {
633
634 return (struct vm_map *)417416;
635 }
636