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