vm.c revision 1.14 1 /* $NetBSD: vm.c,v 1.14 2007/08/26 23:51:08 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 static int ubc_winvalid;
67 static struct uvm_object *ubc_uobj;
68 static off_t ubc_offset;
69 static int ubc_flags;
70
71 struct uvm_pagerops uvm_vnodeops;
72 struct uvm_pagerops aobj_pager;
73 struct uvmexp uvmexp;
74 struct uvm uvm;
75
76 struct vmspace rump_vmspace;
77 struct vm_map rump_vmmap;
78
79 /*
80 * vm pages
81 */
82
83 struct vm_page *
84 rumpvm_makepage(struct uvm_object *uobj, voff_t off)
85 {
86 struct vm_page *pg;
87
88 pg = rumpuser_malloc(sizeof(struct vm_page), 0);
89 memset(pg, 0, sizeof(struct vm_page));
90 TAILQ_INSERT_TAIL(&uobj->memq, pg, listq);
91 pg->offset = off;
92 pg->uobject = uobj;
93
94 pg->uanon = (void *)rumpuser_malloc(PAGE_SIZE, 0);
95 memset((void *)pg->uanon, 0, PAGE_SIZE);
96 pg->flags = PG_CLEAN;
97
98 return pg;
99 }
100
101 void
102 rumpvm_freepage(struct vm_page *pg)
103 {
104 struct uvm_object *uobj = pg->uobject;
105
106 TAILQ_REMOVE(&uobj->memq, pg, listq);
107 rumpuser_free((void *)pg->uanon);
108 rumpuser_free(pg);
109 }
110
111 /*
112 * vnode pager
113 */
114
115 static int
116 vn_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
117 int *npages, int centeridx, vm_prot_t access_type,
118 int advice, int flags)
119 {
120 struct vnode *vp = (struct vnode *)uobj;
121
122 return VOP_GETPAGES(vp, off, pgs, npages, centeridx, access_type,
123 advice, flags);
124 }
125
126 static int
127 vn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
128 {
129 struct vnode *vp = (struct vnode *)uobj;
130
131 return VOP_PUTPAGES(vp, offlo, offhi, flags);
132 }
133
134 /*
135 * Anon object stuff
136 */
137
138 static int
139 ao_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
140 int *npages, int centeridx, vm_prot_t access_type,
141 int advice, int flags)
142 {
143 struct vm_page *pg;
144 int i;
145
146 if (centeridx)
147 panic("%s: centeridx != 0 not supported", __func__);
148
149 /* loop over pages */
150 off = trunc_page(off);
151 for (i = 0; i < *npages; i++) {
152 pg = uvm_pagelookup(uobj, off + (i << PAGE_SHIFT));
153 if (pg) {
154 pgs[i] = pg;
155 } else {
156 pg = rumpvm_makepage(uobj, off + (i << PAGE_SHIFT));
157 pgs[i] = pg;
158 }
159 }
160
161 return 0;
162
163 }
164
165 static int
166 ao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
167 {
168 struct vm_page *pg;
169
170 /* we only free all pages for now */
171 if ((flags & PGO_FREE) == 0 || (flags & PGO_ALLPAGES) == 0)
172 return 0;
173
174 while ((pg = TAILQ_FIRST(&uobj->memq)) != NULL)
175 rumpvm_freepage(pg);
176
177 return 0;
178 }
179
180 struct uvm_object *
181 uao_create(vsize_t size, int flags)
182 {
183 struct uvm_object *uobj;
184
185 uobj = rumpuser_malloc(sizeof(struct uvm_object), 0);
186 memset(uobj, 0, sizeof(struct uvm_object));
187 uobj->pgops = &aobj_pager;
188 TAILQ_INIT(&uobj->memq);
189
190 return uobj;
191 }
192
193 void
194 uao_detach(struct uvm_object *uobj)
195 {
196
197 ao_put(uobj, 0, 0, PGO_ALLPAGES | PGO_FREE);
198 rumpuser_free(uobj);
199 }
200
201 /*
202 * UBC
203 */
204
205 int
206 rump_ubc_magic_uiomove(size_t n, struct uio *uio)
207 {
208 int npages = len2npages(uio->uio_offset, n);
209 struct vm_page *pgs[npages];
210 int i, rv;
211
212 if (ubc_winvalid == 0)
213 panic("%s: ubc window not allocated", __func__);
214
215 memset(pgs, 0, sizeof(pgs));
216 rv = ubc_uobj->pgops->pgo_get(ubc_uobj, ubc_offset,
217 pgs, &npages, 0, 0, 0, 0);
218 if (rv)
219 return rv;
220
221 for (i = 0; i < npages; i++) {
222 size_t xfersize;
223 off_t pageoff;
224
225 pageoff = uio->uio_offset & PAGE_MASK;
226 xfersize = MIN(MIN(n, PAGE_SIZE), PAGE_SIZE-pageoff);
227 uiomove((uint8_t *)pgs[i]->uanon + pageoff, xfersize, uio);
228 if (uio->uio_rw == UIO_WRITE)
229 pgs[i]->flags &= ~PG_CLEAN;
230 ubc_offset += xfersize;
231 n -= xfersize;
232 }
233
234 return 0;
235 }
236
237 void *
238 ubc_alloc(struct uvm_object *uobj, voff_t offset, vsize_t *lenp, int advice,
239 int flags)
240 {
241 vsize_t reallen;
242
243 /* XXX: only one window, but that's ok for now */
244 if (ubc_winvalid == 1)
245 panic("%s: ubc window already allocated", __func__);
246
247 printf("UBC_ALLOC offset 0x%x\n", (int)offset);
248 ubc_uobj = uobj;
249 ubc_offset = offset;
250 reallen = round_page(*lenp);
251 ubc_flags = flags;
252
253 ubc_winvalid = 1;
254
255 return RUMP_UBC_MAGIC_WINDOW;
256 }
257
258 void
259 ubc_release(void *va, int flags)
260 {
261
262 ubc_winvalid = 0;
263 }
264
265 int
266 ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
267 int advice, int flags)
268 {
269 void *win;
270 vsize_t len;
271
272 while (todo > 0) {
273 len = todo;
274
275 win = ubc_alloc(uobj, uio->uio_offset, &len, 0, flags);
276 rump_ubc_magic_uiomove(len, uio);
277 ubc_release(win, 0);
278
279 todo -= len;
280 }
281 return 0;
282 }
283
284
285 /*
286 * Misc routines
287 */
288
289 void
290 rumpvm_init()
291 {
292
293 uvm_vnodeops.pgo_get = vn_get;
294 uvm_vnodeops.pgo_put = vn_put;
295 aobj_pager.pgo_get = ao_get;
296 aobj_pager.pgo_put = ao_put;
297
298 uvmexp.free = 1024*1024; /* XXX */
299 uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
300 }
301
302 void
303 uvm_pageactivate(struct vm_page *pg)
304 {
305
306 /* nada */
307 }
308
309 void
310 uvm_page_unbusy(struct vm_page **pgs, int npgs)
311 {
312
313 /* nada */
314 }
315
316 void
317 uvm_pagewire(struct vm_page *pg)
318 {
319
320 /* nada */
321 }
322
323 void
324 uvm_pageunwire(struct vm_page *pg)
325 {
326
327 /* nada */
328 }
329
330 vaddr_t
331 uvm_pagermapin(struct vm_page **pps, int npages, int flags)
332 {
333
334 panic("%s: unimplemented", __func__);
335 }
336
337 struct vm_page *
338 uvm_pagelookup(struct uvm_object *uobj, voff_t off)
339 {
340 struct vm_page *pg;
341
342 TAILQ_FOREACH(pg, &uobj->memq, listq)
343 if (pg->offset == off)
344 return pg;
345
346 return NULL;
347 }
348
349 struct vm_page *
350 uvm_pageratop(vaddr_t va)
351 {
352
353 panic("%s: unimplemented", __func__);
354 }
355
356 void
357 uvm_estimatepageable(int *active, int *inactive)
358 {
359
360 *active = 0;
361 *inactive = 0;
362 panic("%s: unimplemented", __func__);
363 }
364
365 void
366 uvm_aio_biodone1(struct buf *bp)
367 {
368
369 panic("%s: unimplemented", __func__);
370 }
371
372 void
373 uvm_aio_biodone(struct buf *bp)
374 {
375
376 panic("%s: unimplemented", __func__);
377 }
378
379 void
380 uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
381 {
382
383 vp->v_size = vp->v_writesize = newsize;
384 }
385
386 void
387 uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
388 {
389
390 vp->v_writesize = newsize;
391 }
392
393 void
394 uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
395 {
396 int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
397 struct vm_page *pgs[maxpages];
398 struct uvm_object *uobj = &vp->v_uobj;
399 int rv, npages, i;
400
401 while (len) {
402 npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
403 memset(pgs, 0, npages * sizeof(struct vm_page *));
404 rv = uobj->pgops->pgo_get(uobj, off, pgs, &npages, 0, 0, 0, 0);
405 assert(npages > 0);
406
407 for (i = 0; i < npages; i++) {
408 uint8_t *start;
409 size_t chunkoff, chunklen;
410
411 chunkoff = off & PAGE_MASK;
412 chunklen = MIN(PAGE_SIZE - chunkoff, len);
413 start = (uint8_t *)pgs[i]->uanon + chunkoff;
414
415 memset(start, 0, chunklen);
416 pgs[i]->flags &= PG_CLEAN;
417
418 off += chunklen;
419 len -= chunklen;
420 }
421 }
422
423 return;
424 }
425
426 struct uvm_ractx *
427 uvm_ra_allocctx()
428 {
429
430 return NULL;
431 }
432
433 void
434 uvm_ra_freectx(struct uvm_ractx *ra)
435 {
436
437 return;
438 }
439
440 bool
441 uvn_clean_p(struct uvm_object *uobj)
442 {
443 struct vnode *vp = (void *)uobj;
444
445 return (vp->v_flag & VONWORKLST) == 0;
446 }
447
448 /*
449 * Kmem
450 */
451
452 void *
453 kmem_alloc(size_t size, km_flag_t kmflag)
454 {
455
456 return rumpuser_malloc(size, kmflag == KM_NOSLEEP);
457 }
458
459 void *
460 kmem_zalloc(size_t size, km_flag_t kmflag)
461 {
462 void *rv;
463
464 rv = kmem_alloc(size, kmflag);
465 if (rv)
466 memset(rv, 0, size);
467
468 return rv;
469 }
470
471 void
472 kmem_free(void *p, size_t size)
473 {
474
475 rumpuser_free(p);
476 }
477
478 /*
479 * UVM km
480 */
481
482 vaddr_t
483 uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
484 {
485 void *rv;
486
487 rv = rumpuser_malloc(size, flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT));
488 if (rv && flags & UVM_KMF_ZERO)
489 memset(rv, 0, size);
490
491 return (vaddr_t)rv;
492 }
493
494 void
495 uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
496 {
497
498 rumpuser_free((void *)vaddr);
499 }
500
501 struct vm_map *
502 uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
503 vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
504 {
505
506 return (struct vm_map *)417416;
507 }
508