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