1/* $OpenBSD: uvm_amap.h,v 1.36 2025/05/25 01:52:00 gnezdo 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
59struct vm_amap;
60
61/*
62 * prototypes for the amap interface
63 */
64
65 /* ensure amap can store anon */
66void amap_populate(struct vm_aref *, vaddr_t);
67 /* add an anon to an amap */
68int amap_add(struct vm_aref *, vaddr_t, struct vm_anon *,
69 boolean_t);
70 /* allocate a new amap */
71struct vm_amap *amap_alloc(vaddr_t, int, int);
72 /* clear amap needs-copy flag */
73void amap_copy(vm_map_t, vm_map_entry_t, int, boolean_t, vaddr_t,
74 vaddr_t);
75 /* resolve all COW faults now */
76void amap_cow_now(vm_map_t, vm_map_entry_t);
77 /* free amap */
78void amap_free(struct vm_amap *);
79 /* init amap module (at boot time) */
80void amap_init(void);
81 /* lookup an anon @ offset in amap */
82struct vm_anon *amap_lookup(struct vm_aref *, vaddr_t);
83 /* lookup multiple anons */
84void amap_lookups(struct vm_aref *, vaddr_t, struct vm_anon **, int);
85 /* add a reference to an amap */
86void amap_ref(struct vm_amap *, vaddr_t, vsize_t, int);
87 /* split reference to amap into two */
88void amap_splitref(struct vm_aref *, struct vm_aref *, vaddr_t);
89 /* remove an anon from an amap */
90void amap_unadd(struct vm_aref *, vaddr_t);
91 /* drop reference to an amap */
92void amap_unref(struct vm_amap *, vaddr_t, vsize_t, int);
93 /* remove all anons from amap */
94void amap_wipeout(struct vm_amap *);
95boolean_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 track split references so that we don't lose track of
116 * references during partial unmaps.
117 */
118
119/*
120 * here is the definition of the vm_amap structure and helper structures for
121 * this implementation.
122 */
123
124struct vm_amap_chunk {
125 TAILQ_ENTRY(vm_amap_chunk) ac_list;
126 int ac_baseslot;
127 uint16_t ac_usedmap;
128 uint16_t ac_nslot;
129 struct vm_anon *ac_anon[];
130};
131
132struct vm_amap {
133 struct rwlock *am_lock; /* lock for all vm_amap flags */
134 int am_ref; /* reference count */
135 int am_flags; /* flags */
136 int am_nslot; /* # of slots currently in map */
137 int am_nused; /* # of slots currently in use */
138 int *am_ppref; /* per page reference count (if !NULL) */
139 LIST_ENTRY(vm_amap) am_list;
140
141 union {
142 struct {
143 struct vm_amap_chunk **amn_buckets;
144 TAILQ_HEAD(, vm_amap_chunk) amn_chunks;
145 int amn_nbuckets; /* # of buckets */
146 int amn_ncused; /* # of chunkers currently in use */
147 int amn_hashshift; /* shift count to hash slot to bucket */
148 } ami_normal;
149
150 /*
151 * MUST be last element in vm_amap because it contains a
152 * variably sized array element.
153 */
154 struct vm_amap_chunk ami_small;
155 } am_impl;
156
157#define am_buckets am_impl.ami_normal.amn_buckets
158#define am_chunks am_impl.ami_normal.amn_chunks
159#define am_nbuckets am_impl.ami_normal.amn_nbuckets
160#define am_ncused am_impl.ami_normal.amn_ncused
161#define am_hashshift am_impl.ami_normal.amn_hashshift
162
163#define am_small am_impl.ami_small
164};
165
166/*
167 * The entries in an amap are called slots. For example an amap that
168 * covers four pages is said to have four slots.
169 *
170 * The slots of an amap are clustered into chunks of UVM_AMAP_CHUNK
171 * slots each. The data structure of a chunk is vm_amap_chunk.
172 * Every chunk contains an array of pointers to vm_anon, and a bitmap
173 * is used to represent which of the slots are in use.
174 *
175 * Small amaps of up to UVM_AMAP_CHUNK slots have the chunk directly
176 * embedded in the amap structure.
177 *
178 * amaps with more slots are normal amaps and organize chunks in a hash
179 * table. The hash table is organized as an array of buckets.
180 * All chunks of the amap are additionally stored in a linked list.
181 * Chunks that belong to the same hash bucket are stored in the list
182 * consecutively. When all slots in a chunk are unused, the chunk is freed.
183 *
184 * For large amaps, the bucket array can grow large. See the description
185 * below how large bucket arrays are avoided.
186 */
187
188/*
189 * defines for handling of large sparse amaps:
190 *
191 * one of the problems of array-based amaps is that if you allocate a
192 * large sparsely-used area of virtual memory you end up allocating
193 * large arrays that, for the most part, don't get used. this is a
194 * problem for BSD in that the kernel likes to make these types of
195 * allocations to "reserve" memory for possible future use.
196 *
197 * for example, the kernel allocates (reserves) a large chunk of user
198 * VM for possible stack growth. most of the time only a page or two
199 * of this VM is actually used. since the stack is anonymous memory
200 * it makes sense for it to live in an amap, but if we allocated an
201 * amap for the entire stack range we could end up wasting a large
202 * amount of malloc'd KVM.
203 *
204 * for example, on the i386 at boot time we allocate two amaps for the stack
205 * of /sbin/init:
206 * 1. a 7680 slot amap at protection PROT_NONE (reserve space for stack)
207 * 2. a 512 slot amap at protection PROT_READ|PROT_WRITE (top of stack)
208 *
209 * most of the array allocated for the amaps for this is never used.
210 * the amap interface provides a way for us to avoid this problem by
211 * allowing amap_copy() to break larger amaps up into smaller sized
212 * chunks (controlled by the "canchunk" option). we use this feature
213 * to reduce our memory usage with the BSD stack management. if we
214 * are asked to create an amap with more than UVM_AMAP_LARGE slots in it,
215 * we attempt to break it up into a UVM_AMAP_CHUNK sized amap if the
216 * "canchunk" flag is set.
217 *
218 * so, in the i386 example, the 7680 slot area is never referenced so
219 * nothing gets allocated (amap_copy is never called because the protection
220 * is zero). the 512 slot area for the top of the stack is referenced.
221 * the chunking code breaks it up into 16 slot chunks (hopefully a single
222 * 16 slot chunk is enough to handle the whole stack).
223 */
224
225#define UVM_AMAP_LARGE 256 /* # of slots in "large" amap */
226#define UVM_AMAP_CHUNK 16 /* # of slots to chunk large amaps in */
227
228#define UVM_AMAP_SMALL(amap) ((amap)->am_nslot <= UVM_AMAP_CHUNK)
229#define UVM_AMAP_SLOTIDX(slot) ((slot) % UVM_AMAP_CHUNK)
230#define UVM_AMAP_BUCKET(amap, slot) \
231 (((slot) / UVM_AMAP_CHUNK) >> (amap)->am_hashshift)
232
233#ifdef _KERNEL
234
235/*
236 * macros
237 */
238
239/* AMAP_B2SLOT: convert byte offset to slot */
240#define AMAP_B2SLOT(S,B) { \
241 KASSERT(((B) & (PAGE_SIZE - 1)) == 0); \
242 (S) = (B) >> PAGE_SHIFT; \
243}
244
245#define AMAP_CHUNK_FOREACH(chunk, amap) \
246 for (chunk = (UVM_AMAP_SMALL(amap) ? \
247 &(amap)->am_small : TAILQ_FIRST(&(amap)->am_chunks)); \
248 (chunk) != NULL; (chunk) = TAILQ_NEXT(chunk, ac_list))
249
250#define AMAP_BASE_SLOT(slot) \
251 (((slot) / UVM_AMAP_CHUNK) * UVM_AMAP_CHUNK)
252
253/*
254 * flags macros
255 */
256
257#define amap_flags(AMAP) ((AMAP)->am_flags)
258#define amap_refs(AMAP) ((AMAP)->am_ref)
259
260#define amap_lock(AMAP, RWLT) rw_enter((AMAP)->am_lock, (RWLT))
261#define amap_unlock(AMAP) rw_exit((AMAP)->am_lock)
262
263#endif /* _KERNEL */
264
265#endif /* _UVM_UVM_AMAP_H_ */