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