1 /* $OpenBSD: uvm_amap.h,v 1.29 2016/05/26 13:37:26 stefan Exp $ */ 2 /* $NetBSD: uvm_amap.h,v 1.14 2001/02/18 21:19:08 chs Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef _UVM_UVM_AMAP_H_ 30 #define _UVM_UVM_AMAP_H_ 31 32 /* 33 * uvm_amap.h: general amap interface and amap implementation-specific info 34 */ 35 36 /* 37 * an amap structure contains pointers to a set of anons that are 38 * mapped together in virtual memory (an anon is a single page of 39 * anonymous virtual memory -- see uvm_anon.h). in uvm we hide the 40 * details of the implementation of amaps behind a general amap 41 * interface. this allows us to change the amap implementation 42 * without having to touch the rest of the code. this file is divided 43 * into two parts: the definition of the uvm amap interface and the 44 * amap implementation-specific definitions. 45 */ 46 47 #ifdef _KERNEL 48 49 /* 50 * part 1: amap interface 51 */ 52 53 /* 54 * forward definition of vm_amap structure. only amap 55 * implementation-specific code should directly access the fields of 56 * this structure. 57 */ 58 59 struct vm_amap; 60 61 /* 62 * prototypes for the amap interface 63 */ 64 65 /* ensure amap can store anon */ 66 void amap_populate(struct vm_aref *, vaddr_t); 67 /* add an anon to an amap */ 68 int amap_add(struct vm_aref *, vaddr_t, struct vm_anon *, 69 boolean_t); 70 /* allocate a new amap */ 71 struct vm_amap *amap_alloc(vaddr_t, int, int); 72 /* clear amap needs-copy flag */ 73 void amap_copy(vm_map_t, vm_map_entry_t, int, boolean_t, vaddr_t, 74 vaddr_t); 75 /* resolve all COW faults now */ 76 void amap_cow_now(vm_map_t, vm_map_entry_t); 77 /* free amap */ 78 void amap_free(struct vm_amap *); 79 /* init amap module (at boot time) */ 80 void amap_init(void); 81 /* lookup an anon @ offset in amap */ 82 struct vm_anon *amap_lookup(struct vm_aref *, vaddr_t); 83 /* lookup multiple anons */ 84 void amap_lookups(struct vm_aref *, vaddr_t, struct vm_anon **, int); 85 /* add a reference to an amap */ 86 void amap_ref(struct vm_amap *, vaddr_t, vsize_t, int); 87 /* split reference to amap into two */ 88 void amap_splitref(struct vm_aref *, struct vm_aref *, vaddr_t); 89 /* remove an anon from an amap */ 90 void amap_unadd(struct vm_aref *, vaddr_t); 91 /* drop reference to an amap */ 92 void amap_unref(struct vm_amap *, vaddr_t, vsize_t, int); 93 /* remove all anons from amap */ 94 void amap_wipeout(struct vm_amap *); 95 boolean_t amap_swap_off(int, int); 96 97 /* 98 * amap flag values 99 */ 100 101 #define AMAP_SHARED 0x1 /* amap is shared */ 102 #define AMAP_REFALL 0x2 /* amap_ref: reference entire amap */ 103 #define AMAP_SWAPOFF 0x4 /* amap_swap_off() is in progress */ 104 105 #endif /* _KERNEL */ 106 107 /**********************************************************************/ 108 109 /* 110 * part 2: amap implementation-specific info 111 */ 112 113 /* 114 * we currently provide an array-based amap implementation. in this 115 * implementation we provide the option of tracking split references 116 * so that we don't lose track of references during partial unmaps 117 * ... this is enabled with the "UVM_AMAP_PPREF" define. 118 */ 119 120 #define UVM_AMAP_PPREF /* track partial references */ 121 122 /* 123 * here is the definition of the vm_amap structure and helper structures for 124 * this implementation. 125 */ 126 127 struct vm_amap_chunk { 128 TAILQ_ENTRY(vm_amap_chunk) ac_list; 129 int ac_baseslot; 130 uint16_t ac_usedmap; 131 uint16_t ac_nslot; 132 struct vm_anon *ac_anon[]; 133 }; 134 135 struct vm_amap { 136 int am_ref; /* reference count */ 137 int am_flags; /* flags */ 138 int am_nslot; /* # of slots currently in map */ 139 int am_nused; /* # of slots currently in use */ 140 #ifdef UVM_AMAP_PPREF 141 int *am_ppref; /* per page reference count (if !NULL) */ 142 #endif 143 LIST_ENTRY(vm_amap) am_list; 144 145 union { 146 struct { 147 struct vm_amap_chunk **amn_buckets; 148 TAILQ_HEAD(, vm_amap_chunk) amn_chunks; 149 int amn_ncused; /* # of chunkers currently in use */ 150 int amn_hashshift; /* shift count to hash slot to bucket */ 151 } ami_normal; 152 153 /* 154 * MUST be last element in vm_amap because it contains a 155 * variably sized array element. 156 */ 157 struct vm_amap_chunk ami_small; 158 } am_impl; 159 160 #define am_buckets am_impl.ami_normal.amn_buckets 161 #define am_chunks am_impl.ami_normal.amn_chunks 162 #define am_ncused am_impl.ami_normal.amn_ncused 163 #define am_hashshift am_impl.ami_normal.amn_hashshift 164 165 #define am_small am_impl.ami_small 166 }; 167 168 /* 169 * The entries in an amap are called slots. For example an amap that 170 * covers four pages is said to have four slots. 171 * 172 * The slots of an amap are clustered into chunks of UVM_AMAP_CHUNK 173 * slots each. The data structure of a chunk is vm_amap_chunk. 174 * Every chunk contains an array of pointers to vm_anon, and a bitmap 175 * is used to represent which of the slots are in use. 176 * 177 * Small amaps of up to UVM_AMAP_CHUNK slots have the chunk directly 178 * embedded in the amap structure. 179 * 180 * amaps with more slots are normal amaps and organize chunks in a hash 181 * table. The hash table is organized as an array of buckets. 182 * All chunks of the amap are additionally stored in a linked list. 183 * Chunks that belong to the same hash bucket are stored in the list 184 * consecutively. When all slots in a chunk are unused, the chunk is freed. 185 * 186 * For large amaps, the bucket array can grow large. See the description 187 * below how large bucket arrays are avoided. 188 */ 189 190 /* 191 * defines for handling of large sparce amaps: 192 * 193 * one of the problems of array-based amaps is that if you allocate a 194 * large sparcely-used area of virtual memory you end up allocating 195 * large arrays that, for the most part, don't get used. this is a 196 * problem for BSD in that the kernel likes to make these types of 197 * allocations to "reserve" memory for possible future use. 198 * 199 * for example, the kernel allocates (reserves) a large chunk of user 200 * VM for possible stack growth. most of the time only a page or two 201 * of this VM is actually used. since the stack is anonymous memory 202 * it makes sense for it to live in an amap, but if we allocated an 203 * amap for the entire stack range we could end up wasting a large 204 * amount of malloc'd KVM. 205 * 206 * for example, on the i386 at boot time we allocate two amaps for the stack 207 * of /sbin/init: 208 * 1. a 7680 slot amap at protection 0 (reserve space for stack) 209 * 2. a 512 slot amap at protection 7 (top of stack) 210 * 211 * most of the array allocated for the amaps for this is never used. 212 * the amap interface provides a way for us to avoid this problem by 213 * allowing amap_copy() to break larger amaps up into smaller sized 214 * chunks (controlled by the "canchunk" option). we use this feature 215 * to reduce our memory usage with the BSD stack management. if we 216 * are asked to create an amap with more than UVM_AMAP_LARGE slots in it, 217 * we attempt to break it up into a UVM_AMAP_CHUNK sized amap if the 218 * "canchunk" flag is set. 219 * 220 * so, in the i386 example, the 7680 slot area is never referenced so 221 * nothing gets allocated (amap_copy is never called because the protection 222 * is zero). the 512 slot area for the top of the stack is referenced. 223 * the chunking code breaks it up into 16 slot chunks (hopefully a single 224 * 16 slot chunk is enough to handle the whole stack). 225 */ 226 227 #define UVM_AMAP_LARGE 256 /* # of slots in "large" amap */ 228 #define UVM_AMAP_CHUNK 16 /* # of slots to chunk large amaps in */ 229 230 #define UVM_AMAP_SMALL(amap) ((amap)->am_nslot <= UVM_AMAP_CHUNK) 231 #define UVM_AMAP_SLOTIDX(slot) ((slot) % UVM_AMAP_CHUNK) 232 #define UVM_AMAP_BUCKET(amap, slot) \ 233 (((slot) / UVM_AMAP_CHUNK) >> (amap)->am_hashshift) 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 #define AMAP_CHUNK_FOREACH(chunk, amap) \ 248 for (chunk = (UVM_AMAP_SMALL(amap) ? \ 249 &(amap)->am_small : TAILQ_FIRST(&(amap)->am_chunks)); \ 250 (chunk) != NULL; (chunk) = TAILQ_NEXT(chunk, ac_list)) 251 252 #define AMAP_BASE_SLOT(slot) \ 253 (((slot) / UVM_AMAP_CHUNK) * UVM_AMAP_CHUNK) 254 255 /* 256 * flags macros 257 */ 258 259 #define amap_flags(AMAP) ((AMAP)->am_flags) 260 #define amap_refs(AMAP) ((AMAP)->am_ref) 261 262 /* 263 * if we enable PPREF, then we have a couple of extra functions that 264 * we need to prototype here... 265 */ 266 267 #ifdef UVM_AMAP_PPREF 268 269 #define PPREF_NONE ((int *) -1) /* not using ppref */ 270 271 /* adjust references */ 272 void amap_pp_adjref(struct vm_amap *, int, vsize_t, int); 273 /* establish ppref */ 274 void amap_pp_establish(struct vm_amap *); 275 /* wipe part of an amap */ 276 void amap_wiperange(struct vm_amap *, int, int); 277 #endif /* UVM_AMAP_PPREF */ 278 279 #endif /* _KERNEL */ 280 281 #endif /* _UVM_UVM_AMAP_H_ */ 282