uvm_aobj.c revision 1.18 1 /* $NetBSD: uvm_aobj.c,v 1.18 1999/03/26 17:34:15 chs Exp $ */
2
3 /*
4 * Copyright (c) 1998 Chuck Silvers, Charles D. Cranor and
5 * Washington University.
6 * All rights reserved.
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor and
19 * Washington University.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * from: Id: uvm_aobj.c,v 1.1.2.5 1998/02/06 05:14:38 chs Exp
35 */
36 /*
37 * uvm_aobj.c: anonymous memory uvm_object pager
38 *
39 * author: Chuck Silvers <chuq (at) chuq.com>
40 * started: Jan-1998
41 *
42 * - design mostly from Chuck Cranor
43 */
44
45
46
47 #include "opt_uvmhist.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/proc.h>
52 #include <sys/malloc.h>
53 #include <sys/pool.h>
54
55 #include <vm/vm.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_kern.h>
58
59 #include <uvm/uvm.h>
60
61 /*
62 * an aobj manages anonymous-memory backed uvm_objects. in addition
63 * to keeping the list of resident pages, it also keeps a list of
64 * allocated swap blocks. depending on the size of the aobj this list
65 * of allocated swap blocks is either stored in an array (small objects)
66 * or in a hash table (large objects).
67 */
68
69 /*
70 * local structures
71 */
72
73 /*
74 * for hash tables, we break the address space of the aobj into blocks
75 * of UAO_SWHASH_CLUSTER_SIZE pages. we require the cluster size to
76 * be a power of two.
77 */
78
79 #define UAO_SWHASH_CLUSTER_SHIFT 4
80 #define UAO_SWHASH_CLUSTER_SIZE (1 << UAO_SWHASH_CLUSTER_SHIFT)
81
82 /* get the "tag" for this page index */
83 #define UAO_SWHASH_ELT_TAG(PAGEIDX) \
84 ((PAGEIDX) >> UAO_SWHASH_CLUSTER_SHIFT)
85
86 /* given an ELT and a page index, find the swap slot */
87 #define UAO_SWHASH_ELT_PAGESLOT(ELT, PAGEIDX) \
88 ((ELT)->slots[(PAGEIDX) & (UAO_SWHASH_CLUSTER_SIZE - 1)])
89
90 /* given an ELT, return its pageidx base */
91 #define UAO_SWHASH_ELT_PAGEIDX_BASE(ELT) \
92 ((ELT)->tag << UAO_SWHASH_CLUSTER_SHIFT)
93
94 /*
95 * the swhash hash function
96 */
97 #define UAO_SWHASH_HASH(AOBJ, PAGEIDX) \
98 (&(AOBJ)->u_swhash[(((PAGEIDX) >> UAO_SWHASH_CLUSTER_SHIFT) \
99 & (AOBJ)->u_swhashmask)])
100
101 /*
102 * the swhash threshhold determines if we will use an array or a
103 * hash table to store the list of allocated swap blocks.
104 */
105
106 #define UAO_SWHASH_THRESHOLD (UAO_SWHASH_CLUSTER_SIZE * 4)
107 #define UAO_USES_SWHASH(AOBJ) \
108 ((AOBJ)->u_pages > UAO_SWHASH_THRESHOLD) /* use hash? */
109
110 /*
111 * the number of buckets in a swhash, with an upper bound
112 */
113 #define UAO_SWHASH_MAXBUCKETS 256
114 #define UAO_SWHASH_BUCKETS(AOBJ) \
115 (min((AOBJ)->u_pages >> UAO_SWHASH_CLUSTER_SHIFT, \
116 UAO_SWHASH_MAXBUCKETS))
117
118
119 /*
120 * uao_swhash_elt: when a hash table is being used, this structure defines
121 * the format of an entry in the bucket list.
122 */
123
124 struct uao_swhash_elt {
125 LIST_ENTRY(uao_swhash_elt) list; /* the hash list */
126 vaddr_t tag; /* our 'tag' */
127 int count; /* our number of active slots */
128 int slots[UAO_SWHASH_CLUSTER_SIZE]; /* the slots */
129 };
130
131 /*
132 * uao_swhash: the swap hash table structure
133 */
134
135 LIST_HEAD(uao_swhash, uao_swhash_elt);
136
137 /*
138 * uao_swhash_elt_pool: pool of uao_swhash_elt structures
139 */
140
141 struct pool uao_swhash_elt_pool;
142
143 /*
144 * uvm_aobj: the actual anon-backed uvm_object
145 *
146 * => the uvm_object is at the top of the structure, this allows
147 * (struct uvm_device *) == (struct uvm_object *)
148 * => only one of u_swslots and u_swhash is used in any given aobj
149 */
150
151 struct uvm_aobj {
152 struct uvm_object u_obj; /* has: lock, pgops, memq, #pages, #refs */
153 int u_pages; /* number of pages in entire object */
154 int u_flags; /* the flags (see uvm_aobj.h) */
155 int *u_swslots; /* array of offset->swapslot mappings */
156 /*
157 * hashtable of offset->swapslot mappings
158 * (u_swhash is an array of bucket heads)
159 */
160 struct uao_swhash *u_swhash;
161 u_long u_swhashmask; /* mask for hashtable */
162 LIST_ENTRY(uvm_aobj) u_list; /* global list of aobjs */
163 };
164
165 /*
166 * uvm_aobj_pool: pool of uvm_aobj structures
167 */
168
169 struct pool uvm_aobj_pool;
170
171 /*
172 * local functions
173 */
174
175 static void uao_init __P((void));
176 static struct uao_swhash_elt *uao_find_swhash_elt __P((struct uvm_aobj *,
177 int, boolean_t));
178 static int uao_find_swslot __P((struct uvm_aobj *,
179 int));
180 static boolean_t uao_flush __P((struct uvm_object *,
181 vaddr_t, vaddr_t,
182 int));
183 static void uao_free __P((struct uvm_aobj *));
184 static int uao_get __P((struct uvm_object *, vaddr_t,
185 vm_page_t *, int *, int,
186 vm_prot_t, int, int));
187 static boolean_t uao_releasepg __P((struct vm_page *,
188 struct vm_page **));
189
190
191
192 /*
193 * aobj_pager
194 *
195 * note that some functions (e.g. put) are handled elsewhere
196 */
197
198 struct uvm_pagerops aobj_pager = {
199 uao_init, /* init */
200 uao_reference, /* reference */
201 uao_detach, /* detach */
202 NULL, /* fault */
203 uao_flush, /* flush */
204 uao_get, /* get */
205 NULL, /* asyncget */
206 NULL, /* put (done by pagedaemon) */
207 NULL, /* cluster */
208 NULL, /* mk_pcluster */
209 uvm_shareprot, /* shareprot */
210 NULL, /* aiodone */
211 uao_releasepg /* releasepg */
212 };
213
214 /*
215 * uao_list: global list of active aobjs, locked by uao_list_lock
216 */
217
218 static LIST_HEAD(aobjlist, uvm_aobj) uao_list;
219 static simple_lock_data_t uao_list_lock;
220
221
222 /*
223 * functions
224 */
225
226 /*
227 * hash table/array related functions
228 */
229
230 /*
231 * uao_find_swhash_elt: find (or create) a hash table entry for a page
232 * offset.
233 *
234 * => the object should be locked by the caller
235 */
236
237 static struct uao_swhash_elt *
238 uao_find_swhash_elt(aobj, pageidx, create)
239 struct uvm_aobj *aobj;
240 int pageidx;
241 boolean_t create;
242 {
243 struct uao_swhash *swhash;
244 struct uao_swhash_elt *elt;
245 int page_tag;
246
247 swhash = UAO_SWHASH_HASH(aobj, pageidx); /* first hash to get bucket */
248 page_tag = UAO_SWHASH_ELT_TAG(pageidx); /* tag to search for */
249
250 /*
251 * now search the bucket for the requested tag
252 */
253 for (elt = swhash->lh_first; elt != NULL; elt = elt->list.le_next) {
254 if (elt->tag == page_tag)
255 return(elt);
256 }
257
258 /* fail now if we are not allowed to create a new entry in the bucket */
259 if (!create)
260 return NULL;
261
262
263 /*
264 * allocate a new entry for the bucket and init/insert it in
265 */
266 elt = pool_get(&uao_swhash_elt_pool, PR_WAITOK);
267 LIST_INSERT_HEAD(swhash, elt, list);
268 elt->tag = page_tag;
269 elt->count = 0;
270 memset(elt->slots, 0, sizeof(elt->slots));
271
272 return(elt);
273 }
274
275 /*
276 * uao_find_swslot: find the swap slot number for an aobj/pageidx
277 *
278 * => object must be locked by caller
279 */
280 __inline static int
281 uao_find_swslot(aobj, pageidx)
282 struct uvm_aobj *aobj;
283 int pageidx;
284 {
285
286 /*
287 * if noswap flag is set, then we never return a slot
288 */
289
290 if (aobj->u_flags & UAO_FLAG_NOSWAP)
291 return(0);
292
293 /*
294 * if hashing, look in hash table.
295 */
296
297 if (UAO_USES_SWHASH(aobj)) {
298 struct uao_swhash_elt *elt =
299 uao_find_swhash_elt(aobj, pageidx, FALSE);
300
301 if (elt)
302 return(UAO_SWHASH_ELT_PAGESLOT(elt, pageidx));
303 else
304 return(NULL);
305 }
306
307 /*
308 * otherwise, look in the array
309 */
310 return(aobj->u_swslots[pageidx]);
311 }
312
313 /*
314 * uao_set_swslot: set the swap slot for a page in an aobj.
315 *
316 * => setting a slot to zero frees the slot
317 * => object must be locked by caller
318 */
319 int
320 uao_set_swslot(uobj, pageidx, slot)
321 struct uvm_object *uobj;
322 int pageidx, slot;
323 {
324 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
325 int oldslot;
326 UVMHIST_FUNC("uao_set_swslot"); UVMHIST_CALLED(pdhist);
327 UVMHIST_LOG(pdhist, "aobj %p pageidx %d slot %d",
328 aobj, pageidx, slot, 0);
329
330 /*
331 * if noswap flag is set, then we can't set a slot
332 */
333
334 if (aobj->u_flags & UAO_FLAG_NOSWAP) {
335
336 if (slot == 0)
337 return(0); /* a clear is ok */
338
339 /* but a set is not */
340 printf("uao_set_swslot: uobj = %p\n", uobj);
341 panic("uao_set_swslot: attempt to set a slot on a NOSWAP object");
342 }
343
344 /*
345 * are we using a hash table? if so, add it in the hash.
346 */
347
348 if (UAO_USES_SWHASH(aobj)) {
349 /*
350 * Avoid allocating an entry just to free it again if
351 * the page had not swap slot in the first place, and
352 * we are freeing.
353 */
354 struct uao_swhash_elt *elt =
355 uao_find_swhash_elt(aobj, pageidx, slot ? TRUE : FALSE);
356 if (elt == NULL) {
357 #ifdef DIAGNOSTIC
358 if (slot)
359 panic("uao_set_swslot: didn't create elt");
360 #endif
361 return (0);
362 }
363
364 oldslot = UAO_SWHASH_ELT_PAGESLOT(elt, pageidx);
365 UAO_SWHASH_ELT_PAGESLOT(elt, pageidx) = slot;
366
367 /*
368 * now adjust the elt's reference counter and free it if we've
369 * dropped it to zero.
370 */
371
372 /* an allocation? */
373 if (slot) {
374 if (oldslot == 0)
375 elt->count++;
376 } else { /* freeing slot ... */
377 if (oldslot) /* to be safe */
378 elt->count--;
379
380 if (elt->count == 0) {
381 LIST_REMOVE(elt, list);
382 pool_put(&uao_swhash_elt_pool, elt);
383 }
384 }
385
386 } else {
387 /* we are using an array */
388 oldslot = aobj->u_swslots[pageidx];
389 aobj->u_swslots[pageidx] = slot;
390 }
391 return (oldslot);
392 }
393
394 /*
395 * end of hash/array functions
396 */
397
398 /*
399 * uao_free: free all resources held by an aobj, and then free the aobj
400 *
401 * => the aobj should be dead
402 */
403 static void
404 uao_free(aobj)
405 struct uvm_aobj *aobj;
406 {
407
408 if (UAO_USES_SWHASH(aobj)) {
409 int i, hashbuckets = aobj->u_swhashmask + 1;
410
411 /*
412 * free the swslots from each hash bucket,
413 * then the hash bucket, and finally the hash table itself.
414 */
415 for (i = 0; i < hashbuckets; i++) {
416 struct uao_swhash_elt *elt, *next;
417
418 for (elt = aobj->u_swhash[i].lh_first; elt != NULL;
419 elt = next) {
420 int j;
421
422 for (j = 0; j < UAO_SWHASH_CLUSTER_SIZE; j++)
423 {
424 int slot = elt->slots[j];
425
426 if (slot) {
427 uvm_swap_free(slot, 1);
428
429 /*
430 * this page is no longer
431 * only in swap.
432 */
433 simple_lock(&uvm.swap_data_lock);
434 uvmexp.swpgonly--;
435 simple_unlock(&uvm.swap_data_lock);
436 }
437 }
438
439 next = elt->list.le_next;
440 pool_put(&uao_swhash_elt_pool, elt);
441 }
442 }
443 FREE(aobj->u_swhash, M_UVMAOBJ);
444 } else {
445 int i;
446
447 /*
448 * free the array
449 */
450
451 for (i = 0; i < aobj->u_pages; i++)
452 {
453 int slot = aobj->u_swslots[i];
454
455 if (slot) {
456 uvm_swap_free(slot, 1);
457
458 /* this page is no longer only in swap. */
459 simple_lock(&uvm.swap_data_lock);
460 uvmexp.swpgonly--;
461 simple_unlock(&uvm.swap_data_lock);
462 }
463 }
464 FREE(aobj->u_swslots, M_UVMAOBJ);
465 }
466
467 /*
468 * finally free the aobj itself
469 */
470 pool_put(&uvm_aobj_pool, aobj);
471 }
472
473 /*
474 * pager functions
475 */
476
477 /*
478 * uao_create: create an aobj of the given size and return its uvm_object.
479 *
480 * => for normal use, flags are always zero
481 * => for the kernel object, the flags are:
482 * UAO_FLAG_KERNOBJ - allocate the kernel object (can only happen once)
483 * UAO_FLAG_KERNSWAP - enable swapping of kernel object (" ")
484 */
485 struct uvm_object *
486 uao_create(size, flags)
487 vsize_t size;
488 int flags;
489 {
490 static struct uvm_aobj kernel_object_store; /* home of kernel_object */
491 static int kobj_alloced = 0; /* not allocated yet */
492 int pages = round_page(size) >> PAGE_SHIFT;
493 struct uvm_aobj *aobj;
494
495 /*
496 * malloc a new aobj unless we are asked for the kernel object
497 */
498 if (flags & UAO_FLAG_KERNOBJ) { /* want kernel object? */
499 if (kobj_alloced)
500 panic("uao_create: kernel object already allocated");
501
502 /*
503 * XXXTHORPEJ: Need to call this now, so the pool gets
504 * initialized!
505 */
506 uao_init();
507
508 aobj = &kernel_object_store;
509 aobj->u_pages = pages;
510 aobj->u_flags = UAO_FLAG_NOSWAP; /* no swap to start */
511 /* we are special, we never die */
512 aobj->u_obj.uo_refs = UVM_OBJ_KERN;
513 kobj_alloced = UAO_FLAG_KERNOBJ;
514 } else if (flags & UAO_FLAG_KERNSWAP) {
515 aobj = &kernel_object_store;
516 if (kobj_alloced != UAO_FLAG_KERNOBJ)
517 panic("uao_create: asked to enable swap on kernel object");
518 kobj_alloced = UAO_FLAG_KERNSWAP;
519 } else { /* normal object */
520 aobj = pool_get(&uvm_aobj_pool, PR_WAITOK);
521 aobj->u_pages = pages;
522 aobj->u_flags = 0; /* normal object */
523 aobj->u_obj.uo_refs = 1; /* start with 1 reference */
524 }
525
526 /*
527 * allocate hash/array if necessary
528 *
529 * note: in the KERNSWAP case no need to worry about locking since
530 * we are still booting we should be the only thread around.
531 */
532 if (flags == 0 || (flags & UAO_FLAG_KERNSWAP) != 0) {
533 int mflags = (flags & UAO_FLAG_KERNSWAP) != 0 ?
534 M_NOWAIT : M_WAITOK;
535
536 /* allocate hash table or array depending on object size */
537 if (UAO_USES_SWHASH(aobj)) {
538 aobj->u_swhash = hashinit(UAO_SWHASH_BUCKETS(aobj),
539 M_UVMAOBJ, mflags, &aobj->u_swhashmask);
540 if (aobj->u_swhash == NULL)
541 panic("uao_create: hashinit swhash failed");
542 } else {
543 MALLOC(aobj->u_swslots, int *, pages * sizeof(int),
544 M_UVMAOBJ, mflags);
545 if (aobj->u_swslots == NULL)
546 panic("uao_create: malloc swslots failed");
547 memset(aobj->u_swslots, 0, pages * sizeof(int));
548 }
549
550 if (flags) {
551 aobj->u_flags &= ~UAO_FLAG_NOSWAP; /* clear noswap */
552 return(&aobj->u_obj);
553 /* done! */
554 }
555 }
556
557 /*
558 * init aobj fields
559 */
560 simple_lock_init(&aobj->u_obj.vmobjlock);
561 aobj->u_obj.pgops = &aobj_pager;
562 TAILQ_INIT(&aobj->u_obj.memq);
563 aobj->u_obj.uo_npages = 0;
564
565 /*
566 * now that aobj is ready, add it to the global list
567 * XXXCHS: uao_init hasn't been called'd in the KERNOBJ case,
568 * do we really need the kernel object on this list anyway?
569 */
570 simple_lock(&uao_list_lock);
571 LIST_INSERT_HEAD(&uao_list, aobj, u_list);
572 simple_unlock(&uao_list_lock);
573
574 /*
575 * done!
576 */
577 return(&aobj->u_obj);
578 }
579
580
581
582 /*
583 * uao_init: set up aobj pager subsystem
584 *
585 * => called at boot time from uvm_pager_init()
586 */
587 static void
588 uao_init()
589 {
590 static int uao_initialized;
591
592 if (uao_initialized)
593 return;
594 uao_initialized = TRUE;
595
596 LIST_INIT(&uao_list);
597 simple_lock_init(&uao_list_lock);
598
599 /*
600 * NOTE: Pages fror this pool must not come from a pageable
601 * kernel map!
602 */
603 pool_init(&uao_swhash_elt_pool, sizeof(struct uao_swhash_elt),
604 0, 0, 0, "uaoeltpl", 0, NULL, NULL, M_UVMAOBJ);
605
606 pool_init(&uvm_aobj_pool, sizeof(struct uvm_aobj), 0, 0, 0,
607 "aobjpl", 0,
608 pool_page_alloc_nointr, pool_page_free_nointr, M_UVMAOBJ);
609 }
610
611 /*
612 * uao_reference: add a ref to an aobj
613 *
614 * => aobj must be unlocked (we will lock it)
615 */
616 void
617 uao_reference(uobj)
618 struct uvm_object *uobj;
619 {
620 UVMHIST_FUNC("uao_reference"); UVMHIST_CALLED(maphist);
621
622 /*
623 * kernel_object already has plenty of references, leave it alone.
624 */
625
626 if (uobj->uo_refs == UVM_OBJ_KERN)
627 return;
628
629 simple_lock(&uobj->vmobjlock);
630 uobj->uo_refs++; /* bump! */
631 UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
632 uobj, uobj->uo_refs,0,0);
633 simple_unlock(&uobj->vmobjlock);
634 }
635
636 /*
637 * uao_detach: drop a reference to an aobj
638 *
639 * => aobj must be unlocked, we will lock it
640 */
641 void
642 uao_detach(uobj)
643 struct uvm_object *uobj;
644 {
645 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
646 struct vm_page *pg;
647 boolean_t busybody;
648 UVMHIST_FUNC("uao_detach"); UVMHIST_CALLED(maphist);
649
650 /*
651 * detaching from kernel_object is a noop.
652 */
653 if (uobj->uo_refs == UVM_OBJ_KERN)
654 return;
655
656 simple_lock(&uobj->vmobjlock);
657
658 UVMHIST_LOG(maphist," (uobj=0x%x) ref=%d", uobj,uobj->uo_refs,0,0);
659 uobj->uo_refs--; /* drop ref! */
660 if (uobj->uo_refs) { /* still more refs? */
661 simple_unlock(&uobj->vmobjlock);
662 UVMHIST_LOG(maphist, "<- done (rc>0)", 0,0,0,0);
663 return;
664 }
665
666 /*
667 * remove the aobj from the global list.
668 */
669 simple_lock(&uao_list_lock);
670 LIST_REMOVE(aobj, u_list);
671 simple_unlock(&uao_list_lock);
672
673 /*
674 * free all the pages that aren't PG_BUSY, mark for release any that are.
675 */
676
677 busybody = FALSE;
678 for (pg = uobj->memq.tqh_first ; pg != NULL ; pg = pg->listq.tqe_next) {
679
680 if (pg->flags & PG_BUSY) {
681 pg->flags |= PG_RELEASED;
682 busybody = TRUE;
683 continue;
684 }
685
686 /* zap the mappings, free the swap slot, free the page */
687 pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE);
688 uao_dropswap(&aobj->u_obj, pg->offset >> PAGE_SHIFT);
689 uvm_lock_pageq();
690 uvm_pagefree(pg);
691 uvm_unlock_pageq();
692 }
693
694 /*
695 * if we found any busy pages, we're done for now.
696 * mark the aobj for death, releasepg will finish up for us.
697 */
698 if (busybody) {
699 aobj->u_flags |= UAO_FLAG_KILLME;
700 simple_unlock(&aobj->u_obj.vmobjlock);
701 return;
702 }
703
704 /*
705 * finally, free the rest.
706 */
707 uao_free(aobj);
708 }
709
710 /*
711 * uao_flush: uh, yea, sure it's flushed. really!
712 */
713 boolean_t
714 uao_flush(uobj, start, end, flags)
715 struct uvm_object *uobj;
716 vaddr_t start, end;
717 int flags;
718 {
719
720 /*
721 * anonymous memory doesn't "flush"
722 */
723 /*
724 * XXX
725 * deal with PGO_DEACTIVATE (for madvise(MADV_SEQUENTIAL))
726 * and PGO_FREE (for msync(MSINVALIDATE))
727 */
728 return TRUE;
729 }
730
731 /*
732 * uao_get: fetch me a page
733 *
734 * we have three cases:
735 * 1: page is resident -> just return the page.
736 * 2: page is zero-fill -> allocate a new page and zero it.
737 * 3: page is swapped out -> fetch the page from swap.
738 *
739 * cases 1 and 2 can be handled with PGO_LOCKED, case 3 cannot.
740 * so, if the "center" page hits case 3 (or any page, with PGO_ALLPAGES),
741 * then we will need to return VM_PAGER_UNLOCK.
742 *
743 * => prefer map unlocked (not required)
744 * => object must be locked! we will _unlock_ it before starting any I/O.
745 * => flags: PGO_ALLPAGES: get all of the pages
746 * PGO_LOCKED: fault data structures are locked
747 * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
748 * => NOTE: caller must check for released pages!!
749 */
750 static int
751 uao_get(uobj, offset, pps, npagesp, centeridx, access_type, advice, flags)
752 struct uvm_object *uobj;
753 vaddr_t offset;
754 struct vm_page **pps;
755 int *npagesp;
756 int centeridx, advice, flags;
757 vm_prot_t access_type;
758 {
759 struct uvm_aobj *aobj = (struct uvm_aobj *)uobj;
760 vaddr_t current_offset;
761 vm_page_t ptmp;
762 int lcv, gotpages, maxpages, swslot, rv;
763 boolean_t done;
764 UVMHIST_FUNC("uao_get"); UVMHIST_CALLED(pdhist);
765
766 UVMHIST_LOG(pdhist, "aobj=%p offset=%d, flags=%d", aobj, offset, flags,0);
767
768 /*
769 * get number of pages
770 */
771
772 maxpages = *npagesp;
773
774 /*
775 * step 1: handled the case where fault data structures are locked.
776 */
777
778 if (flags & PGO_LOCKED) {
779
780 /*
781 * step 1a: get pages that are already resident. only do
782 * this if the data structures are locked (i.e. the first
783 * time through).
784 */
785
786 done = TRUE; /* be optimistic */
787 gotpages = 0; /* # of pages we got so far */
788
789 for (lcv = 0, current_offset = offset ; lcv < maxpages ;
790 lcv++, current_offset += PAGE_SIZE) {
791 /* do we care about this page? if not, skip it */
792 if (pps[lcv] == PGO_DONTCARE)
793 continue;
794
795 ptmp = uvm_pagelookup(uobj, current_offset);
796
797 /*
798 * if page is new, attempt to allocate the page, then
799 * zero-fill it.
800 */
801 if (ptmp == NULL && uao_find_swslot(aobj,
802 current_offset >> PAGE_SHIFT) == 0) {
803 ptmp = uvm_pagealloc(uobj, current_offset,
804 NULL);
805 if (ptmp) {
806 /* new page */
807 ptmp->flags &= ~(PG_BUSY|PG_FAKE);
808 ptmp->pqflags |= PQ_AOBJ;
809 UVM_PAGE_OWN(ptmp, NULL);
810 uvm_pagezero(ptmp);
811 }
812 }
813
814 /*
815 * to be useful must get a non-busy, non-released page
816 */
817 if (ptmp == NULL ||
818 (ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
819 if (lcv == centeridx ||
820 (flags & PGO_ALLPAGES) != 0)
821 /* need to do a wait or I/O! */
822 done = FALSE;
823 continue;
824 }
825
826 /*
827 * useful page: busy/lock it and plug it in our
828 * result array
829 */
830 /* caller must un-busy this page */
831 ptmp->flags |= PG_BUSY;
832 UVM_PAGE_OWN(ptmp, "uao_get1");
833 pps[lcv] = ptmp;
834 gotpages++;
835
836 } /* "for" lcv loop */
837
838 /*
839 * step 1b: now we've either done everything needed or we
840 * to unlock and do some waiting or I/O.
841 */
842
843 UVMHIST_LOG(pdhist, "<- done (done=%d)", done, 0,0,0);
844
845 *npagesp = gotpages;
846 if (done)
847 /* bingo! */
848 return(VM_PAGER_OK);
849 else
850 /* EEK! Need to unlock and I/O */
851 return(VM_PAGER_UNLOCK);
852 }
853
854 /*
855 * step 2: get non-resident or busy pages.
856 * object is locked. data structures are unlocked.
857 */
858
859 for (lcv = 0, current_offset = offset ; lcv < maxpages ;
860 lcv++, current_offset += PAGE_SIZE) {
861 /*
862 * - skip over pages we've already gotten or don't want
863 * - skip over pages we don't _have_ to get
864 */
865 if (pps[lcv] != NULL ||
866 (lcv != centeridx && (flags & PGO_ALLPAGES) == 0))
867 continue;
868
869 /*
870 * we have yet to locate the current page (pps[lcv]). we
871 * first look for a page that is already at the current offset.
872 * if we find a page, we check to see if it is busy or
873 * released. if that is the case, then we sleep on the page
874 * until it is no longer busy or released and repeat the lookup.
875 * if the page we found is neither busy nor released, then we
876 * busy it (so we own it) and plug it into pps[lcv]. this
877 * 'break's the following while loop and indicates we are
878 * ready to move on to the next page in the "lcv" loop above.
879 *
880 * if we exit the while loop with pps[lcv] still set to NULL,
881 * then it means that we allocated a new busy/fake/clean page
882 * ptmp in the object and we need to do I/O to fill in the data.
883 */
884
885 /* top of "pps" while loop */
886 while (pps[lcv] == NULL) {
887 /* look for a resident page */
888 ptmp = uvm_pagelookup(uobj, current_offset);
889
890 /* not resident? allocate one now (if we can) */
891 if (ptmp == NULL) {
892
893 ptmp = uvm_pagealloc(uobj, current_offset,
894 NULL); /* alloc */
895
896 /* out of RAM? */
897 if (ptmp == NULL) {
898 simple_unlock(&uobj->vmobjlock);
899 UVMHIST_LOG(pdhist,
900 "sleeping, ptmp == NULL\n",0,0,0,0);
901 uvm_wait("uao_getpage");
902 simple_lock(&uobj->vmobjlock);
903 /* goto top of pps while loop */
904 continue;
905 }
906
907 /*
908 * safe with PQ's unlocked: because we just
909 * alloc'd the page
910 */
911 ptmp->pqflags |= PQ_AOBJ;
912
913 /*
914 * got new page ready for I/O. break pps while
915 * loop. pps[lcv] is still NULL.
916 */
917 break;
918 }
919
920 /* page is there, see if we need to wait on it */
921 if ((ptmp->flags & (PG_BUSY|PG_RELEASED)) != 0) {
922 ptmp->flags |= PG_WANTED;
923 UVMHIST_LOG(pdhist,
924 "sleeping, ptmp->flags 0x%x\n",
925 ptmp->flags,0,0,0);
926 UVM_UNLOCK_AND_WAIT(ptmp, &uobj->vmobjlock, 0,
927 "uao_get", 0);
928 simple_lock(&uobj->vmobjlock);
929 continue; /* goto top of pps while loop */
930 }
931
932 /*
933 * if we get here then the page has become resident and
934 * unbusy between steps 1 and 2. we busy it now (so we
935 * own it) and set pps[lcv] (so that we exit the while
936 * loop).
937 */
938 /* we own it, caller must un-busy */
939 ptmp->flags |= PG_BUSY;
940 UVM_PAGE_OWN(ptmp, "uao_get2");
941 pps[lcv] = ptmp;
942 }
943
944 /*
945 * if we own the valid page at the correct offset, pps[lcv] will
946 * point to it. nothing more to do except go to the next page.
947 */
948 if (pps[lcv])
949 continue; /* next lcv */
950
951 /*
952 * we have a "fake/busy/clean" page that we just allocated.
953 * do the needed "i/o", either reading from swap or zeroing.
954 */
955 swslot = uao_find_swslot(aobj, current_offset >> PAGE_SHIFT);
956
957 /*
958 * just zero the page if there's nothing in swap.
959 */
960 if (swslot == 0)
961 {
962 /*
963 * page hasn't existed before, just zero it.
964 */
965 uvm_pagezero(ptmp);
966 }
967 else
968 {
969 UVMHIST_LOG(pdhist, "pagein from swslot %d",
970 swslot, 0,0,0);
971
972 /*
973 * page in the swapped-out page.
974 * unlock object for i/o, relock when done.
975 */
976 simple_unlock(&uobj->vmobjlock);
977 rv = uvm_swap_get(ptmp, swslot, PGO_SYNCIO);
978 simple_lock(&uobj->vmobjlock);
979
980 /*
981 * I/O done. check for errors.
982 */
983 if (rv != VM_PAGER_OK)
984 {
985 UVMHIST_LOG(pdhist, "<- done (error=%d)",
986 rv,0,0,0);
987 if (ptmp->flags & PG_WANTED)
988 /* object lock still held */
989 thread_wakeup(ptmp);
990 ptmp->flags &= ~(PG_WANTED|PG_BUSY);
991 UVM_PAGE_OWN(ptmp, NULL);
992 uvm_lock_pageq();
993 uvm_pagefree(ptmp);
994 uvm_unlock_pageq();
995 simple_unlock(&uobj->vmobjlock);
996 return (rv);
997 }
998 }
999
1000 /*
1001 * we got the page! clear the fake flag (indicates valid
1002 * data now in page) and plug into our result array. note
1003 * that page is still busy.
1004 *
1005 * it is the callers job to:
1006 * => check if the page is released
1007 * => unbusy the page
1008 * => activate the page
1009 */
1010
1011 ptmp->flags &= ~PG_FAKE; /* data is valid ... */
1012 pmap_clear_modify(PMAP_PGARG(ptmp)); /* ... and clean */
1013 pps[lcv] = ptmp;
1014
1015 } /* lcv loop */
1016
1017 /*
1018 * finally, unlock object and return.
1019 */
1020
1021 simple_unlock(&uobj->vmobjlock);
1022 UVMHIST_LOG(pdhist, "<- done (OK)",0,0,0,0);
1023 return(VM_PAGER_OK);
1024 }
1025
1026 /*
1027 * uao_releasepg: handle released page in an aobj
1028 *
1029 * => "pg" is a PG_BUSY [caller owns it], PG_RELEASED page that we need
1030 * to dispose of.
1031 * => caller must handle PG_WANTED case
1032 * => called with page's object locked, pageq's unlocked
1033 * => returns TRUE if page's object is still alive, FALSE if we
1034 * killed the page's object. if we return TRUE, then we
1035 * return with the object locked.
1036 * => if (nextpgp != NULL) => we return pageq.tqe_next here, and return
1037 * with the page queues locked [for pagedaemon]
1038 * => if (nextpgp == NULL) => we return with page queues unlocked [normal case]
1039 * => we kill the aobj if it is not referenced and we are suppose to
1040 * kill it ("KILLME").
1041 */
1042 static boolean_t uao_releasepg(pg, nextpgp)
1043 struct vm_page *pg;
1044 struct vm_page **nextpgp; /* OUT */
1045 {
1046 struct uvm_aobj *aobj = (struct uvm_aobj *) pg->uobject;
1047
1048 #ifdef DIAGNOSTIC
1049 if ((pg->flags & PG_RELEASED) == 0)
1050 panic("uao_releasepg: page not released!");
1051 #endif
1052
1053 /*
1054 * dispose of the page [caller handles PG_WANTED] and swap slot.
1055 */
1056 pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE);
1057 uao_dropswap(&aobj->u_obj, pg->offset >> PAGE_SHIFT);
1058 uvm_lock_pageq();
1059 if (nextpgp)
1060 *nextpgp = pg->pageq.tqe_next; /* next page for daemon */
1061 uvm_pagefree(pg);
1062 if (!nextpgp)
1063 uvm_unlock_pageq(); /* keep locked for daemon */
1064
1065 /*
1066 * if we're not killing the object, we're done.
1067 */
1068 if ((aobj->u_flags & UAO_FLAG_KILLME) == 0)
1069 return TRUE;
1070
1071 #ifdef DIAGNOSTIC
1072 if (aobj->u_obj.uo_refs)
1073 panic("uvm_km_releasepg: kill flag set on referenced object!");
1074 #endif
1075
1076 /*
1077 * if there are still pages in the object, we're done for now.
1078 */
1079 if (aobj->u_obj.uo_npages != 0)
1080 return TRUE;
1081
1082 #ifdef DIAGNOSTIC
1083 if (aobj->u_obj.memq.tqh_first)
1084 panic("uvn_releasepg: pages in object with npages == 0");
1085 #endif
1086
1087 /*
1088 * finally, free the rest.
1089 */
1090 uao_free(aobj);
1091
1092 return FALSE;
1093 }
1094
1095 /*
1096 * uao_dropswap: release any swap resources from this aobj page.
1097 *
1098 * => aobj must be locked or have a reference count of 0.
1099 */
1100
1101 void
1102 uao_dropswap(uobj, pageidx)
1103 struct uvm_object *uobj;
1104 int pageidx;
1105 {
1106 int slot;
1107
1108 slot = uao_set_swslot(uobj, pageidx, 0);
1109 if (slot) {
1110 uvm_swap_free(slot, 1);
1111 }
1112 }
1113