Home | History | Annotate | Line # | Download | only in uvm
uvm_amap.h revision 1.33.38.2
      1  1.33.38.2  ad /*	$NetBSD: uvm_amap.h,v 1.33.38.2 2007/07/21 19:21:54 ad Exp $	*/
      2  1.33.38.2  ad 
      3  1.33.38.2  ad /*
      4  1.33.38.2  ad  *
      5  1.33.38.2  ad  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      6  1.33.38.2  ad  * All rights reserved.
      7  1.33.38.2  ad  *
      8  1.33.38.2  ad  * Redistribution and use in source and binary forms, with or without
      9  1.33.38.2  ad  * modification, are permitted provided that the following conditions
     10  1.33.38.2  ad  * are met:
     11  1.33.38.2  ad  * 1. Redistributions of source code must retain the above copyright
     12  1.33.38.2  ad  *    notice, this list of conditions and the following disclaimer.
     13  1.33.38.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.33.38.2  ad  *    notice, this list of conditions and the following disclaimer in the
     15  1.33.38.2  ad  *    documentation and/or other materials provided with the distribution.
     16  1.33.38.2  ad  * 3. All advertising materials mentioning features or use of this software
     17  1.33.38.2  ad  *    must display the following acknowledgement:
     18  1.33.38.2  ad  *      This product includes software developed by Charles D. Cranor and
     19  1.33.38.2  ad  *      Washington University.
     20  1.33.38.2  ad  * 4. The name of the author may not be used to endorse or promote products
     21  1.33.38.2  ad  *    derived from this software without specific prior written permission.
     22  1.33.38.2  ad  *
     23  1.33.38.2  ad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  1.33.38.2  ad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  1.33.38.2  ad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.33.38.2  ad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  1.33.38.2  ad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  1.33.38.2  ad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  1.33.38.2  ad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  1.33.38.2  ad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  1.33.38.2  ad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  1.33.38.2  ad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.33.38.2  ad  */
     34  1.33.38.2  ad 
     35  1.33.38.2  ad #ifndef _UVM_UVM_AMAP_H_
     36  1.33.38.2  ad #define _UVM_UVM_AMAP_H_
     37  1.33.38.2  ad 
     38  1.33.38.2  ad /*
     39  1.33.38.2  ad  * uvm_amap.h: general amap interface and amap implementation-specific info
     40  1.33.38.2  ad  */
     41  1.33.38.2  ad 
     42  1.33.38.2  ad /*
     43  1.33.38.2  ad  * an amap structure contains pointers to a set of anons that are
     44  1.33.38.2  ad  * mapped together in virtual memory (an anon is a single page of
     45  1.33.38.2  ad  * anonymous virtual memory -- see uvm_anon.h).  in uvm we hide the
     46  1.33.38.2  ad  * details of the implementation of amaps behind a general amap
     47  1.33.38.2  ad  * interface.  this allows us to change the amap implementation
     48  1.33.38.2  ad  * without having to touch the rest of the code.  this file is divided
     49  1.33.38.2  ad  * into two parts: the definition of the uvm amap interface and the
     50  1.33.38.2  ad  * amap implementation-specific definitions.
     51  1.33.38.2  ad  */
     52  1.33.38.2  ad 
     53  1.33.38.2  ad #ifdef _KERNEL
     54  1.33.38.2  ad 
     55  1.33.38.2  ad /*
     56  1.33.38.2  ad  * part 1: amap interface
     57  1.33.38.2  ad  */
     58  1.33.38.2  ad 
     59  1.33.38.2  ad void	uvm_amap_init(void);
     60  1.33.38.2  ad 
     61  1.33.38.2  ad /*
     62  1.33.38.2  ad  * forward definition of vm_amap structure.  only amap
     63  1.33.38.2  ad  * implementation-specific code should directly access the fields of
     64  1.33.38.2  ad  * this structure.
     65  1.33.38.2  ad  */
     66  1.33.38.2  ad 
     67  1.33.38.2  ad struct vm_amap;
     68  1.33.38.2  ad 
     69  1.33.38.2  ad 
     70  1.33.38.2  ad /*
     71  1.33.38.2  ad  * prototypes for the amap interface
     72  1.33.38.2  ad  */
     73  1.33.38.2  ad 
     74  1.33.38.2  ad void		amap_add 	/* add an anon to an amap */
     75  1.33.38.2  ad 			(struct vm_aref *, vaddr_t,
     76  1.33.38.2  ad 			 struct vm_anon *, bool);
     77  1.33.38.2  ad struct vm_amap	*amap_alloc	/* allocate a new amap */
     78  1.33.38.2  ad 			(vaddr_t, vaddr_t, int);
     79  1.33.38.2  ad void		amap_copy	/* clear amap needs-copy flag */
     80  1.33.38.2  ad 			(struct vm_map *, struct vm_map_entry *, int,
     81  1.33.38.2  ad 			 vaddr_t, vaddr_t);
     82  1.33.38.2  ad void		amap_cow_now	/* resolve all COW faults now */
     83  1.33.38.2  ad 			(struct vm_map *, struct vm_map_entry *);
     84  1.33.38.2  ad int		amap_extend	/* make amap larger */
     85  1.33.38.2  ad 			(struct vm_map_entry *, vsize_t, int);
     86  1.33.38.2  ad int		amap_flags	/* get amap's flags */
     87  1.33.38.2  ad 			(struct vm_amap *);
     88  1.33.38.2  ad void		amap_free	/* free amap */
     89  1.33.38.2  ad 			(struct vm_amap *);
     90  1.33.38.2  ad void		amap_lock	/* lock amap */
     91  1.33.38.2  ad 			(struct vm_amap *);
     92  1.33.38.2  ad struct vm_anon	*amap_lookup	/* lookup an anon @ offset in amap */
     93  1.33.38.2  ad 			(struct vm_aref *, vaddr_t);
     94  1.33.38.2  ad void		amap_lookups	/* lookup multiple anons */
     95  1.33.38.2  ad 			(struct vm_aref *, vaddr_t,
     96  1.33.38.2  ad 			 struct vm_anon **, int);
     97  1.33.38.2  ad void		amap_ref	/* add a reference to an amap */
     98  1.33.38.2  ad 			(struct vm_amap *, vaddr_t, vsize_t, int);
     99  1.33.38.2  ad int		amap_refs	/* get number of references of amap */
    100  1.33.38.2  ad 			(struct vm_amap *);
    101  1.33.38.2  ad void		amap_share_protect /* protect pages in a shared amap */
    102  1.33.38.2  ad 			(struct vm_map_entry *, vm_prot_t);
    103  1.33.38.2  ad void		amap_splitref	/* split reference to amap into two */
    104  1.33.38.2  ad 			(struct vm_aref *, struct vm_aref *, vaddr_t);
    105  1.33.38.2  ad void		amap_unadd	/* remove an anon from an amap */
    106  1.33.38.2  ad 			(struct vm_aref *, vaddr_t);
    107  1.33.38.2  ad void		amap_unlock	/* unlock amap */
    108  1.33.38.2  ad 			(struct vm_amap *);
    109  1.33.38.2  ad void		amap_unref	/* drop reference to an amap */
    110  1.33.38.2  ad 			(struct vm_amap *, vaddr_t, vsize_t, bool);
    111  1.33.38.2  ad void		amap_wipeout	/* remove all anons from amap */
    112  1.33.38.2  ad 			(struct vm_amap *);
    113  1.33.38.2  ad bool		amap_swap_off
    114  1.33.38.2  ad 			(int, int);
    115  1.33.38.2  ad 
    116  1.33.38.2  ad /*
    117  1.33.38.2  ad  * amap flag values
    118  1.33.38.2  ad  */
    119  1.33.38.2  ad 
    120  1.33.38.2  ad #define AMAP_SHARED	0x1	/* amap is shared */
    121  1.33.38.2  ad #define AMAP_REFALL	0x2	/* amap_ref: reference entire amap */
    122  1.33.38.2  ad #define AMAP_SWAPOFF	0x4	/* amap_swap_off() is in progress */
    123  1.33.38.2  ad 
    124  1.33.38.2  ad /*
    125  1.33.38.2  ad  * amap_copy flags
    126  1.33.38.2  ad  */
    127  1.33.38.2  ad 
    128  1.33.38.2  ad #define	AMAP_COPY_NOWAIT	0x02	/* not allowed to sleep */
    129  1.33.38.2  ad #define	AMAP_COPY_NOCHUNK	0x04	/* not allowed to chunk */
    130  1.33.38.2  ad #define	AMAP_COPY_NOMERGE	0x08	/* not allowed to merge */
    131  1.33.38.2  ad 
    132  1.33.38.2  ad /*
    133  1.33.38.2  ad  * amap_extend flags
    134  1.33.38.2  ad  */
    135  1.33.38.2  ad #define AMAP_EXTEND_BACKWARDS	0x00	/* add "size" to start of map */
    136  1.33.38.2  ad #define AMAP_EXTEND_FORWARDS	0x01	/* add "size" to end of map */
    137  1.33.38.2  ad #define AMAP_EXTEND_NOWAIT	0x02	/* not allowed to sleep */
    138  1.33.38.2  ad 
    139  1.33.38.2  ad #endif /* _KERNEL */
    140  1.33.38.2  ad 
    141  1.33.38.2  ad /**********************************************************************/
    142  1.33.38.2  ad 
    143  1.33.38.2  ad /*
    144  1.33.38.2  ad  * part 2: amap implementation-specific info
    145  1.33.38.2  ad  */
    146  1.33.38.2  ad 
    147  1.33.38.2  ad /*
    148  1.33.38.2  ad  * we currently provide an array-based amap implementation.  in this
    149  1.33.38.2  ad  * implementation we provide the option of tracking split references
    150  1.33.38.2  ad  * so that we don't lose track of references during partial unmaps
    151  1.33.38.2  ad  * ... this is enabled with the "UVM_AMAP_PPREF" define.
    152  1.33.38.2  ad  */
    153  1.33.38.2  ad 
    154  1.33.38.2  ad #define UVM_AMAP_PPREF		/* track partial references */
    155  1.33.38.2  ad 
    156  1.33.38.2  ad /*
    157  1.33.38.2  ad  * here is the definition of the vm_amap structure for this implementation.
    158  1.33.38.2  ad  */
    159  1.33.38.2  ad 
    160  1.33.38.2  ad struct vm_amap {
    161  1.33.38.2  ad 	kmutex_t am_l;		/* lock [locks all vm_amap fields] */
    162  1.33.38.2  ad 	int am_ref;		/* reference count */
    163  1.33.38.2  ad 	int am_flags;		/* flags */
    164  1.33.38.2  ad 	int am_maxslot;		/* max # of slots allocated */
    165  1.33.38.2  ad 	int am_nslot;		/* # of slots currently in map ( <= maxslot) */
    166  1.33.38.2  ad 	int am_nused;		/* # of slots currently in use */
    167  1.33.38.2  ad 	int *am_slots;		/* contig array of active slots */
    168  1.33.38.2  ad 	int *am_bckptr;		/* back pointer array to am_slots */
    169  1.33.38.2  ad 	struct vm_anon **am_anon; /* array of anonymous pages */
    170  1.33.38.2  ad #ifdef UVM_AMAP_PPREF
    171  1.33.38.2  ad 	int *am_ppref;		/* per page reference count (if !NULL) */
    172  1.33.38.2  ad #endif
    173  1.33.38.2  ad 	LIST_ENTRY(vm_amap) am_list;
    174  1.33.38.2  ad };
    175  1.33.38.2  ad 
    176  1.33.38.2  ad /*
    177  1.33.38.2  ad  * note that am_slots, am_bckptr, and am_anon are arrays.   this allows
    178  1.33.38.2  ad  * fast lookup of pages based on their virual address at the expense of
    179  1.33.38.2  ad  * some extra memory.   in the future we should be smarter about memory
    180  1.33.38.2  ad  * usage and fall back to a non-array based implementation on systems
    181  1.33.38.2  ad  * that are short of memory (XXXCDC).
    182  1.33.38.2  ad  *
    183  1.33.38.2  ad  * the entries in the array are called slots... for example an amap that
    184  1.33.38.2  ad  * covers four pages of virtual memory is said to have four slots.   here
    185  1.33.38.2  ad  * is an example of the array usage for a four slot amap.   note that only
    186  1.33.38.2  ad  * slots one and three have anons assigned to them.  "D/C" means that we
    187  1.33.38.2  ad  * "don't care" about the value.
    188  1.33.38.2  ad  *
    189  1.33.38.2  ad  *            0     1      2     3
    190  1.33.38.2  ad  * am_anon:   NULL, anon0, NULL, anon1		(actual pointers to anons)
    191  1.33.38.2  ad  * am_bckptr: D/C,  1,     D/C,  0		(points to am_slots entry)
    192  1.33.38.2  ad  *
    193  1.33.38.2  ad  * am_slots:  3, 1, D/C, D/C    		(says slots 3 and 1 are in use)
    194  1.33.38.2  ad  *
    195  1.33.38.2  ad  * note that am_bckptr is D/C if the slot in am_anon is set to NULL.
    196  1.33.38.2  ad  * to find the entry in am_slots for an anon, look at am_bckptr[slot],
    197  1.33.38.2  ad  * thus the entry for slot 3 in am_slots[] is at am_slots[am_bckptr[3]].
    198  1.33.38.2  ad  * in general, if am_anon[X] is non-NULL, then the following must be
    199  1.33.38.2  ad  * true: am_slots[am_bckptr[X]] == X
    200  1.33.38.2  ad  *
    201  1.33.38.2  ad  * note that am_slots is always contig-packed.
    202  1.33.38.2  ad  */
    203  1.33.38.2  ad 
    204  1.33.38.2  ad /*
    205  1.33.38.2  ad  * defines for handling of large sparce amaps:
    206  1.33.38.2  ad  *
    207  1.33.38.2  ad  * one of the problems of array-based amaps is that if you allocate a
    208  1.33.38.2  ad  * large sparcely-used area of virtual memory you end up allocating
    209  1.33.38.2  ad  * large arrays that, for the most part, don't get used.  this is a
    210  1.33.38.2  ad  * problem for BSD in that the kernel likes to make these types of
    211  1.33.38.2  ad  * allocations to "reserve" memory for possible future use.
    212  1.33.38.2  ad  *
    213  1.33.38.2  ad  * for example, the kernel allocates (reserves) a large chunk of user
    214  1.33.38.2  ad  * VM for possible stack growth.  most of the time only a page or two
    215  1.33.38.2  ad  * of this VM is actually used.  since the stack is anonymous memory
    216  1.33.38.2  ad  * it makes sense for it to live in an amap, but if we allocated an
    217  1.33.38.2  ad  * amap for the entire stack range we could end up wasting a large
    218  1.33.38.2  ad  * amount of malloc'd KVM.
    219  1.33.38.2  ad  *
    220  1.33.38.2  ad  * for example, on the i386 at boot time we allocate two amaps for the stack
    221  1.33.38.2  ad  * of /sbin/init:
    222  1.33.38.2  ad  *  1. a 7680 slot amap at protection 0 (reserve space for stack)
    223  1.33.38.2  ad  *  2. a 512 slot amap at protection 7 (top of stack)
    224  1.33.38.2  ad  *
    225  1.33.38.2  ad  * most of the array allocated for the amaps for this is never used.
    226  1.33.38.2  ad  * the amap interface provides a way for us to avoid this problem by
    227  1.33.38.2  ad  * allowing amap_copy() to break larger amaps up into smaller sized
    228  1.33.38.2  ad  * chunks (controlled by the "canchunk" option).   we use this feature
    229  1.33.38.2  ad  * to reduce our memory usage with the BSD stack management.  if we
    230  1.33.38.2  ad  * are asked to create an amap with more than UVM_AMAP_LARGE slots in it,
    231  1.33.38.2  ad  * we attempt to break it up into a UVM_AMAP_CHUNK sized amap if the
    232  1.33.38.2  ad  * "canchunk" flag is set.
    233  1.33.38.2  ad  *
    234  1.33.38.2  ad  * so, in the i386 example, the 7680 slot area is never referenced so
    235  1.33.38.2  ad  * nothing gets allocated (amap_copy is never called because the protection
    236  1.33.38.2  ad  * is zero).   the 512 slot area for the top of the stack is referenced.
    237  1.33.38.2  ad  * the chunking code breaks it up into 16 slot chunks (hopefully a single
    238  1.33.38.2  ad  * 16 slot chunk is enough to handle the whole stack).
    239  1.33.38.2  ad  */
    240  1.33.38.2  ad 
    241  1.33.38.2  ad #define UVM_AMAP_LARGE	256	/* # of slots in "large" amap */
    242  1.33.38.2  ad #define UVM_AMAP_CHUNK	16	/* # of slots to chunk large amaps in */
    243  1.33.38.2  ad 
    244  1.33.38.2  ad #ifdef _KERNEL
    245  1.33.38.2  ad 
    246  1.33.38.2  ad /*
    247  1.33.38.2  ad  * macros
    248  1.33.38.2  ad  */
    249  1.33.38.2  ad 
    250  1.33.38.2  ad /* AMAP_B2SLOT: convert byte offset to slot */
    251  1.33.38.2  ad #define AMAP_B2SLOT(S,B) {						\
    252  1.33.38.2  ad 	KASSERT(((B) & (PAGE_SIZE - 1)) == 0);				\
    253  1.33.38.2  ad 	(S) = (B) >> PAGE_SHIFT;					\
    254  1.33.38.2  ad }
    255  1.33.38.2  ad 
    256  1.33.38.2  ad /*
    257  1.33.38.2  ad  * lock/unlock/refs/flags macros
    258  1.33.38.2  ad  */
    259  1.33.38.2  ad 
    260  1.33.38.2  ad #define amap_flags(AMAP)	((AMAP)->am_flags)
    261  1.33.38.2  ad #define amap_lock(AMAP)		mutex_enter(&(AMAP)->am_l)
    262  1.33.38.2  ad #define amap_lock_try(AMAP)	mutex_tryenter(&(AMAP)->am_l)
    263  1.33.38.2  ad #define amap_refs(AMAP)		((AMAP)->am_ref)
    264  1.33.38.2  ad #define amap_unlock(AMAP)	mutex_exit(&(AMAP)->am_l)
    265  1.33.38.2  ad 
    266  1.33.38.2  ad /*
    267  1.33.38.2  ad  * if we enable PPREF, then we have a couple of extra functions that
    268  1.33.38.2  ad  * we need to prototype here...
    269  1.33.38.2  ad  */
    270  1.33.38.2  ad 
    271  1.33.38.2  ad #ifdef UVM_AMAP_PPREF
    272  1.33.38.2  ad 
    273  1.33.38.2  ad #define PPREF_NONE ((int *) -1)	/* not using ppref */
    274  1.33.38.2  ad 
    275  1.33.38.2  ad void		amap_pp_adjref		/* adjust references */
    276  1.33.38.2  ad 			(struct vm_amap *, int, vsize_t, int);
    277  1.33.38.2  ad void		amap_pp_establish	/* establish ppref */
    278  1.33.38.2  ad 			(struct vm_amap *, vaddr_t);
    279  1.33.38.2  ad void		amap_wiperange		/* wipe part of an amap */
    280  1.33.38.2  ad 			(struct vm_amap *, int, int);
    281  1.33.38.2  ad #endif	/* UVM_AMAP_PPREF */
    282  1.33.38.2  ad 
    283  1.33.38.2  ad #endif /* _KERNEL */
    284  1.33.38.2  ad 
    285  1.33.38.2  ad #endif /* _UVM_UVM_AMAP_H_ */
    286