uvm_km.c revision 1.128 1 1.128 matt /* $NetBSD: uvm_km.c,v 1.128 2012/07/09 11:19:34 matt Exp $ */
2 1.1 mrg
3 1.47 chs /*
4 1.1 mrg * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.47 chs * Copyright (c) 1991, 1993, The Regents of the University of California.
6 1.1 mrg *
7 1.1 mrg * All rights reserved.
8 1.1 mrg *
9 1.1 mrg * This code is derived from software contributed to Berkeley by
10 1.1 mrg * The Mach Operating System project at Carnegie-Mellon University.
11 1.1 mrg *
12 1.1 mrg * Redistribution and use in source and binary forms, with or without
13 1.1 mrg * modification, are permitted provided that the following conditions
14 1.1 mrg * are met:
15 1.1 mrg * 1. Redistributions of source code must retain the above copyright
16 1.1 mrg * notice, this list of conditions and the following disclaimer.
17 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 mrg * notice, this list of conditions and the following disclaimer in the
19 1.1 mrg * documentation and/or other materials provided with the distribution.
20 1.108 chuck * 3. Neither the name of the University nor the names of its contributors
21 1.1 mrg * may be used to endorse or promote products derived from this software
22 1.1 mrg * without specific prior written permission.
23 1.1 mrg *
24 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 mrg * SUCH DAMAGE.
35 1.1 mrg *
36 1.1 mrg * @(#)vm_kern.c 8.3 (Berkeley) 1/12/94
37 1.4 mrg * from: Id: uvm_km.c,v 1.1.2.14 1998/02/06 05:19:27 chs Exp
38 1.1 mrg *
39 1.1 mrg *
40 1.1 mrg * Copyright (c) 1987, 1990 Carnegie-Mellon University.
41 1.1 mrg * All rights reserved.
42 1.47 chs *
43 1.1 mrg * Permission to use, copy, modify and distribute this software and
44 1.1 mrg * its documentation is hereby granted, provided that both the copyright
45 1.1 mrg * notice and this permission notice appear in all copies of the
46 1.1 mrg * software, derivative works or modified versions, and any portions
47 1.1 mrg * thereof, and that both notices appear in supporting documentation.
48 1.47 chs *
49 1.47 chs * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
50 1.47 chs * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
51 1.1 mrg * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
52 1.47 chs *
53 1.1 mrg * Carnegie Mellon requests users of this software to return to
54 1.1 mrg *
55 1.1 mrg * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
56 1.1 mrg * School of Computer Science
57 1.1 mrg * Carnegie Mellon University
58 1.1 mrg * Pittsburgh PA 15213-3890
59 1.1 mrg *
60 1.1 mrg * any improvements or extensions that they make and grant Carnegie the
61 1.1 mrg * rights to redistribute these changes.
62 1.1 mrg */
63 1.6 mrg
64 1.1 mrg /*
65 1.1 mrg * uvm_km.c: handle kernel memory allocation and management
66 1.1 mrg */
67 1.1 mrg
68 1.7 chuck /*
69 1.7 chuck * overview of kernel memory management:
70 1.7 chuck *
71 1.7 chuck * the kernel virtual address space is mapped by "kernel_map." kernel_map
72 1.62 thorpej * starts at VM_MIN_KERNEL_ADDRESS and goes to VM_MAX_KERNEL_ADDRESS.
73 1.62 thorpej * note that VM_MIN_KERNEL_ADDRESS is equal to vm_map_min(kernel_map).
74 1.7 chuck *
75 1.47 chs * the kernel_map has several "submaps." submaps can only appear in
76 1.7 chuck * the kernel_map (user processes can't use them). submaps "take over"
77 1.7 chuck * the management of a sub-range of the kernel's address space. submaps
78 1.7 chuck * are typically allocated at boot time and are never released. kernel
79 1.47 chs * virtual address space that is mapped by a submap is locked by the
80 1.7 chuck * submap's lock -- not the kernel_map's lock.
81 1.7 chuck *
82 1.7 chuck * thus, the useful feature of submaps is that they allow us to break
83 1.7 chuck * up the locking and protection of the kernel address space into smaller
84 1.7 chuck * chunks.
85 1.7 chuck *
86 1.126 para * the vm system has several standard kernel submaps/arenas, including:
87 1.126 para * kmem_arena => used for kmem/pool (memoryallocators(9))
88 1.7 chuck * pager_map => used to map "buf" structures into kernel space
89 1.7 chuck * exec_map => used during exec to handle exec args
90 1.7 chuck * etc...
91 1.7 chuck *
92 1.127 rmind * The kmem_arena is a "special submap", as it lives in a fixed map entry
93 1.127 rmind * within the kernel_map and is controlled by vmem(9).
94 1.126 para *
95 1.7 chuck * the kernel allocates its private memory out of special uvm_objects whose
96 1.7 chuck * reference count is set to UVM_OBJ_KERN (thus indicating that the objects
97 1.7 chuck * are "special" and never die). all kernel objects should be thought of
98 1.47 chs * as large, fixed-sized, sparsely populated uvm_objects. each kernel
99 1.62 thorpej * object is equal to the size of kernel virtual address space (i.e. the
100 1.62 thorpej * value "VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS").
101 1.7 chuck *
102 1.101 pooka * note that just because a kernel object spans the entire kernel virtual
103 1.7 chuck * address space doesn't mean that it has to be mapped into the entire space.
104 1.47 chs * large chunks of a kernel object's space go unused either because
105 1.47 chs * that area of kernel VM is unmapped, or there is some other type of
106 1.7 chuck * object mapped into that range (e.g. a vnode). for submap's kernel
107 1.7 chuck * objects, the only part of the object that can ever be populated is the
108 1.7 chuck * offsets that are managed by the submap.
109 1.7 chuck *
110 1.7 chuck * note that the "offset" in a kernel object is always the kernel virtual
111 1.62 thorpej * address minus the VM_MIN_KERNEL_ADDRESS (aka vm_map_min(kernel_map)).
112 1.7 chuck * example:
113 1.62 thorpej * suppose VM_MIN_KERNEL_ADDRESS is 0xf8000000 and the kernel does a
114 1.7 chuck * uvm_km_alloc(kernel_map, PAGE_SIZE) [allocate 1 wired down page in the
115 1.7 chuck * kernel map]. if uvm_km_alloc returns virtual address 0xf8235000,
116 1.7 chuck * then that means that the page at offset 0x235000 in kernel_object is
117 1.47 chs * mapped at 0xf8235000.
118 1.7 chuck *
119 1.7 chuck * kernel object have one other special property: when the kernel virtual
120 1.7 chuck * memory mapping them is unmapped, the backing memory in the object is
121 1.7 chuck * freed right away. this is done with the uvm_km_pgremove() function.
122 1.7 chuck * this has to be done because there is no backing store for kernel pages
123 1.7 chuck * and no need to save them after they are no longer referenced.
124 1.126 para *
125 1.127 rmind * Generic arenas:
126 1.126 para *
127 1.127 rmind * kmem_arena:
128 1.127 rmind * Main arena controlling the kernel KVA used by other arenas.
129 1.127 rmind *
130 1.127 rmind * kmem_va_arena:
131 1.127 rmind * Implements quantum caching in order to speedup allocations and
132 1.127 rmind * reduce fragmentation. The pool(9), unless created with a custom
133 1.127 rmind * meta-data allocator, and kmem(9) subsystems use this arena.
134 1.127 rmind *
135 1.127 rmind * Arenas for meta-data allocations are used by vmem(9) and pool(9).
136 1.127 rmind * These arenas cannot use quantum cache. However, kmem_va_meta_arena
137 1.127 rmind * compensates this by importing larger chunks from kmem_arena.
138 1.127 rmind *
139 1.127 rmind * kmem_va_meta_arena:
140 1.127 rmind * Space for meta-data.
141 1.127 rmind *
142 1.127 rmind * kmem_meta_arena:
143 1.127 rmind * Imports from kmem_va_meta_arena. Allocations from this arena are
144 1.127 rmind * backed with the pages.
145 1.127 rmind *
146 1.127 rmind * Arena stacking:
147 1.127 rmind *
148 1.127 rmind * kmem_arena
149 1.127 rmind * kmem_va_arena
150 1.127 rmind * kmem_va_meta_arena
151 1.127 rmind * kmem_meta_arena
152 1.7 chuck */
153 1.55 lukem
154 1.55 lukem #include <sys/cdefs.h>
155 1.128 matt __KERNEL_RCSID(0, "$NetBSD: uvm_km.c,v 1.128 2012/07/09 11:19:34 matt Exp $");
156 1.55 lukem
157 1.55 lukem #include "opt_uvmhist.h"
158 1.7 chuck
159 1.117 para #include "opt_kmempages.h"
160 1.117 para
161 1.117 para #ifndef NKMEMPAGES
162 1.117 para #define NKMEMPAGES 0
163 1.117 para #endif
164 1.117 para
165 1.117 para /*
166 1.117 para * Defaults for lower and upper-bounds for the kmem_arena page count.
167 1.117 para * Can be overridden by kernel config options.
168 1.117 para */
169 1.117 para #ifndef NKMEMPAGES_MIN
170 1.117 para #define NKMEMPAGES_MIN NKMEMPAGES_MIN_DEFAULT
171 1.117 para #endif
172 1.117 para
173 1.117 para #ifndef NKMEMPAGES_MAX
174 1.117 para #define NKMEMPAGES_MAX NKMEMPAGES_MAX_DEFAULT
175 1.117 para #endif
176 1.117 para
177 1.117 para
178 1.1 mrg #include <sys/param.h>
179 1.1 mrg #include <sys/systm.h>
180 1.1 mrg #include <sys/proc.h>
181 1.72 yamt #include <sys/pool.h>
182 1.112 para #include <sys/vmem.h>
183 1.112 para #include <sys/kmem.h>
184 1.1 mrg
185 1.1 mrg #include <uvm/uvm.h>
186 1.1 mrg
187 1.1 mrg /*
188 1.1 mrg * global data structures
189 1.1 mrg */
190 1.1 mrg
191 1.49 chs struct vm_map *kernel_map = NULL;
192 1.1 mrg
193 1.1 mrg /*
194 1.1 mrg * local data structues
195 1.1 mrg */
196 1.1 mrg
197 1.112 para static struct vm_map kernel_map_store;
198 1.112 para static struct vm_map_entry kernel_image_mapent_store;
199 1.112 para static struct vm_map_entry kernel_kmem_mapent_store;
200 1.1 mrg
201 1.117 para int nkmempages = 0;
202 1.112 para vaddr_t kmembase;
203 1.112 para vsize_t kmemsize;
204 1.72 yamt
205 1.112 para vmem_t *kmem_arena;
206 1.112 para vmem_t *kmem_va_arena;
207 1.72 yamt
208 1.72 yamt /*
209 1.117 para * kmeminit_nkmempages: calculate the size of kmem_arena.
210 1.117 para */
211 1.117 para void
212 1.117 para kmeminit_nkmempages(void)
213 1.117 para {
214 1.117 para int npages;
215 1.117 para
216 1.117 para if (nkmempages != 0) {
217 1.117 para /*
218 1.117 para * It's already been set (by us being here before)
219 1.117 para * bail out now;
220 1.117 para */
221 1.117 para return;
222 1.117 para }
223 1.117 para
224 1.119 para #if defined(PMAP_MAP_POOLPAGE)
225 1.119 para npages = (physmem / 4);
226 1.119 para #else
227 1.119 para npages = (physmem / 3) * 2;
228 1.119 para #endif /* defined(PMAP_MAP_POOLPAGE) */
229 1.117 para
230 1.119 para #ifndef NKMEMPAGES_MAX_UNLIMITED
231 1.117 para if (npages > NKMEMPAGES_MAX)
232 1.117 para npages = NKMEMPAGES_MAX;
233 1.119 para #endif
234 1.117 para
235 1.117 para if (npages < NKMEMPAGES_MIN)
236 1.117 para npages = NKMEMPAGES_MIN;
237 1.117 para
238 1.117 para nkmempages = npages;
239 1.117 para }
240 1.117 para
241 1.117 para /*
242 1.112 para * uvm_km_bootstrap: init kernel maps and objects to reflect reality (i.e.
243 1.1 mrg * KVM already allocated for text, data, bss, and static data structures).
244 1.1 mrg *
245 1.62 thorpej * => KVM is defined by VM_MIN_KERNEL_ADDRESS/VM_MAX_KERNEL_ADDRESS.
246 1.82 christos * we assume that [vmin -> start] has already been allocated and that
247 1.62 thorpej * "end" is the end.
248 1.1 mrg */
249 1.1 mrg
250 1.8 mrg void
251 1.112 para uvm_km_bootstrap(vaddr_t start, vaddr_t end)
252 1.1 mrg {
253 1.119 para bool kmem_arena_small;
254 1.62 thorpej vaddr_t base = VM_MIN_KERNEL_ADDRESS;
255 1.118 matt struct uvm_map_args args;
256 1.118 matt int error;
257 1.118 matt
258 1.118 matt UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
259 1.118 matt UVMHIST_LOG(maphist, "start=%"PRIxVADDR" end=%#"PRIxVADDR,
260 1.118 matt start, end, 0,0);
261 1.27 thorpej
262 1.117 para kmeminit_nkmempages();
263 1.119 para kmemsize = (vsize_t)nkmempages * PAGE_SIZE;
264 1.119 para kmem_arena_small = kmemsize < 64 * 1024 * 1024;
265 1.112 para
266 1.118 matt UVMHIST_LOG(maphist, "kmemsize=%#"PRIxVSIZE, kmemsize, 0,0,0);
267 1.118 matt
268 1.27 thorpej /*
269 1.27 thorpej * next, init kernel memory objects.
270 1.8 mrg */
271 1.1 mrg
272 1.8 mrg /* kernel_object: for pageable anonymous kernel memory */
273 1.95 ad uvm_kernel_object = uao_create(VM_MAX_KERNEL_ADDRESS -
274 1.112 para VM_MIN_KERNEL_ADDRESS, UAO_FLAG_KERNOBJ);
275 1.1 mrg
276 1.24 thorpej /*
277 1.56 thorpej * init the map and reserve any space that might already
278 1.56 thorpej * have been allocated kernel space before installing.
279 1.8 mrg */
280 1.1 mrg
281 1.112 para uvm_map_setup(&kernel_map_store, base, end, VM_MAP_PAGEABLE);
282 1.112 para kernel_map_store.pmap = pmap_kernel();
283 1.70 yamt if (start != base) {
284 1.112 para error = uvm_map_prepare(&kernel_map_store,
285 1.71 yamt base, start - base,
286 1.70 yamt NULL, UVM_UNKNOWN_OFFSET, 0,
287 1.62 thorpej UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
288 1.70 yamt UVM_ADV_RANDOM, UVM_FLAG_FIXED), &args);
289 1.70 yamt if (!error) {
290 1.112 para kernel_image_mapent_store.flags =
291 1.112 para UVM_MAP_KERNEL | UVM_MAP_STATIC | UVM_MAP_NOMERGE;
292 1.112 para error = uvm_map_enter(&kernel_map_store, &args,
293 1.112 para &kernel_image_mapent_store);
294 1.70 yamt }
295 1.70 yamt
296 1.70 yamt if (error)
297 1.70 yamt panic(
298 1.112 para "uvm_km_bootstrap: could not reserve space for kernel");
299 1.112 para
300 1.112 para kmembase = args.uma_start + args.uma_size;
301 1.114 matt } else {
302 1.114 matt kmembase = base;
303 1.70 yamt }
304 1.47 chs
305 1.118 matt error = uvm_map_prepare(&kernel_map_store,
306 1.118 matt kmembase, kmemsize,
307 1.118 matt NULL, UVM_UNKNOWN_OFFSET, 0,
308 1.118 matt UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
309 1.118 matt UVM_ADV_RANDOM, UVM_FLAG_FIXED), &args);
310 1.118 matt if (!error) {
311 1.118 matt kernel_kmem_mapent_store.flags =
312 1.118 matt UVM_MAP_KERNEL | UVM_MAP_STATIC | UVM_MAP_NOMERGE;
313 1.118 matt error = uvm_map_enter(&kernel_map_store, &args,
314 1.118 matt &kernel_kmem_mapent_store);
315 1.118 matt }
316 1.118 matt
317 1.118 matt if (error)
318 1.118 matt panic("uvm_km_bootstrap: could not reserve kernel kmem");
319 1.118 matt
320 1.8 mrg /*
321 1.8 mrg * install!
322 1.8 mrg */
323 1.8 mrg
324 1.112 para kernel_map = &kernel_map_store;
325 1.112 para
326 1.112 para pool_subsystem_init();
327 1.112 para vmem_bootstrap();
328 1.112 para
329 1.112 para kmem_arena = vmem_create("kmem", kmembase, kmemsize, PAGE_SIZE,
330 1.112 para NULL, NULL, NULL,
331 1.112 para 0, VM_NOSLEEP | VM_BOOTSTRAP, IPL_VM);
332 1.112 para
333 1.112 para vmem_init(kmem_arena);
334 1.112 para
335 1.118 matt UVMHIST_LOG(maphist, "kmem vmem created (base=%#"PRIxVADDR
336 1.118 matt ", size=%#"PRIxVSIZE, kmembase, kmemsize, 0,0);
337 1.118 matt
338 1.112 para kmem_va_arena = vmem_create("kva", 0, 0, PAGE_SIZE,
339 1.112 para vmem_alloc, vmem_free, kmem_arena,
340 1.120 para (kmem_arena_small ? 4 : 8) * PAGE_SIZE,
341 1.119 para VM_NOSLEEP | VM_BOOTSTRAP, IPL_VM);
342 1.118 matt
343 1.118 matt UVMHIST_LOG(maphist, "<- done", 0,0,0,0);
344 1.112 para }
345 1.112 para
346 1.112 para /*
347 1.112 para * uvm_km_init: init the kernel maps virtual memory caches
348 1.112 para * and start the pool/kmem allocator.
349 1.112 para */
350 1.112 para void
351 1.112 para uvm_km_init(void)
352 1.112 para {
353 1.112 para
354 1.112 para kmem_init();
355 1.112 para
356 1.112 para kmeminit(); // killme
357 1.1 mrg }
358 1.1 mrg
359 1.1 mrg /*
360 1.1 mrg * uvm_km_suballoc: allocate a submap in the kernel map. once a submap
361 1.1 mrg * is allocated all references to that area of VM must go through it. this
362 1.1 mrg * allows the locking of VAs in kernel_map to be broken up into regions.
363 1.1 mrg *
364 1.82 christos * => if `fixed' is true, *vmin specifies where the region described
365 1.112 para * pager_map => used to map "buf" structures into kernel space
366 1.5 thorpej * by the submap must start
367 1.1 mrg * => if submap is non NULL we use that as the submap, otherwise we
368 1.1 mrg * alloc a new map
369 1.1 mrg */
370 1.78 yamt
371 1.8 mrg struct vm_map *
372 1.83 thorpej uvm_km_suballoc(struct vm_map *map, vaddr_t *vmin /* IN/OUT */,
373 1.93 thorpej vaddr_t *vmax /* OUT */, vsize_t size, int flags, bool fixed,
374 1.112 para struct vm_map *submap)
375 1.8 mrg {
376 1.8 mrg int mapflags = UVM_FLAG_NOMERGE | (fixed ? UVM_FLAG_FIXED : 0);
377 1.118 matt UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
378 1.1 mrg
379 1.71 yamt KASSERT(vm_map_pmap(map) == pmap_kernel());
380 1.71 yamt
381 1.8 mrg size = round_page(size); /* round up to pagesize */
382 1.1 mrg
383 1.8 mrg /*
384 1.8 mrg * first allocate a blank spot in the parent map
385 1.8 mrg */
386 1.8 mrg
387 1.82 christos if (uvm_map(map, vmin, size, NULL, UVM_UNKNOWN_OFFSET, 0,
388 1.8 mrg UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
389 1.43 chs UVM_ADV_RANDOM, mapflags)) != 0) {
390 1.118 matt panic("%s: unable to allocate space in parent map", __func__);
391 1.8 mrg }
392 1.8 mrg
393 1.8 mrg /*
394 1.82 christos * set VM bounds (vmin is filled in by uvm_map)
395 1.8 mrg */
396 1.1 mrg
397 1.82 christos *vmax = *vmin + size;
398 1.5 thorpej
399 1.8 mrg /*
400 1.8 mrg * add references to pmap and create or init the submap
401 1.8 mrg */
402 1.1 mrg
403 1.8 mrg pmap_reference(vm_map_pmap(map));
404 1.8 mrg if (submap == NULL) {
405 1.112 para submap = kmem_alloc(sizeof(*submap), KM_SLEEP);
406 1.8 mrg if (submap == NULL)
407 1.8 mrg panic("uvm_km_suballoc: unable to create submap");
408 1.8 mrg }
409 1.112 para uvm_map_setup(submap, *vmin, *vmax, flags);
410 1.112 para submap->pmap = vm_map_pmap(map);
411 1.1 mrg
412 1.8 mrg /*
413 1.8 mrg * now let uvm_map_submap plug in it...
414 1.8 mrg */
415 1.1 mrg
416 1.112 para if (uvm_map_submap(map, *vmin, *vmax, submap) != 0)
417 1.8 mrg panic("uvm_km_suballoc: submap allocation failed");
418 1.1 mrg
419 1.112 para return(submap);
420 1.1 mrg }
421 1.1 mrg
422 1.1 mrg /*
423 1.110 yamt * uvm_km_pgremove: remove pages from a kernel uvm_object and KVA.
424 1.1 mrg */
425 1.1 mrg
426 1.8 mrg void
427 1.83 thorpej uvm_km_pgremove(vaddr_t startva, vaddr_t endva)
428 1.1 mrg {
429 1.95 ad struct uvm_object * const uobj = uvm_kernel_object;
430 1.78 yamt const voff_t start = startva - vm_map_min(kernel_map);
431 1.78 yamt const voff_t end = endva - vm_map_min(kernel_map);
432 1.53 chs struct vm_page *pg;
433 1.52 chs voff_t curoff, nextoff;
434 1.53 chs int swpgonlydelta = 0;
435 1.118 matt UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
436 1.1 mrg
437 1.78 yamt KASSERT(VM_MIN_KERNEL_ADDRESS <= startva);
438 1.78 yamt KASSERT(startva < endva);
439 1.86 yamt KASSERT(endva <= VM_MAX_KERNEL_ADDRESS);
440 1.78 yamt
441 1.109 rmind mutex_enter(uobj->vmobjlock);
442 1.110 yamt pmap_remove(pmap_kernel(), startva, endva);
443 1.52 chs for (curoff = start; curoff < end; curoff = nextoff) {
444 1.52 chs nextoff = curoff + PAGE_SIZE;
445 1.52 chs pg = uvm_pagelookup(uobj, curoff);
446 1.53 chs if (pg != NULL && pg->flags & PG_BUSY) {
447 1.52 chs pg->flags |= PG_WANTED;
448 1.109 rmind UVM_UNLOCK_AND_WAIT(pg, uobj->vmobjlock, 0,
449 1.52 chs "km_pgrm", 0);
450 1.109 rmind mutex_enter(uobj->vmobjlock);
451 1.52 chs nextoff = curoff;
452 1.8 mrg continue;
453 1.52 chs }
454 1.8 mrg
455 1.52 chs /*
456 1.52 chs * free the swap slot, then the page.
457 1.52 chs */
458 1.8 mrg
459 1.53 chs if (pg == NULL &&
460 1.64 pk uao_find_swslot(uobj, curoff >> PAGE_SHIFT) > 0) {
461 1.53 chs swpgonlydelta++;
462 1.53 chs }
463 1.52 chs uao_dropswap(uobj, curoff >> PAGE_SHIFT);
464 1.53 chs if (pg != NULL) {
465 1.97 ad mutex_enter(&uvm_pageqlock);
466 1.53 chs uvm_pagefree(pg);
467 1.97 ad mutex_exit(&uvm_pageqlock);
468 1.53 chs }
469 1.8 mrg }
470 1.109 rmind mutex_exit(uobj->vmobjlock);
471 1.8 mrg
472 1.54 chs if (swpgonlydelta > 0) {
473 1.95 ad mutex_enter(&uvm_swap_data_lock);
474 1.54 chs KASSERT(uvmexp.swpgonly >= swpgonlydelta);
475 1.54 chs uvmexp.swpgonly -= swpgonlydelta;
476 1.95 ad mutex_exit(&uvm_swap_data_lock);
477 1.54 chs }
478 1.24 thorpej }
479 1.24 thorpej
480 1.24 thorpej
481 1.24 thorpej /*
482 1.78 yamt * uvm_km_pgremove_intrsafe: like uvm_km_pgremove(), but for non object backed
483 1.78 yamt * regions.
484 1.24 thorpej *
485 1.24 thorpej * => when you unmap a part of anonymous kernel memory you want to toss
486 1.52 chs * the pages right away. (this is called from uvm_unmap_...).
487 1.24 thorpej * => none of the pages will ever be busy, and none of them will ever
488 1.52 chs * be on the active or inactive queues (because they have no object).
489 1.24 thorpej */
490 1.24 thorpej
491 1.24 thorpej void
492 1.102 ad uvm_km_pgremove_intrsafe(struct vm_map *map, vaddr_t start, vaddr_t end)
493 1.24 thorpej {
494 1.122 bouyer #define __PGRM_BATCH 16
495 1.52 chs struct vm_page *pg;
496 1.122 bouyer paddr_t pa[__PGRM_BATCH];
497 1.122 bouyer int npgrm, i;
498 1.122 bouyer vaddr_t va, batch_vastart;
499 1.122 bouyer
500 1.118 matt UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
501 1.24 thorpej
502 1.102 ad KASSERT(VM_MAP_IS_KERNEL(map));
503 1.128 matt KASSERTMSG(vm_map_min(map) <= start,
504 1.128 matt "vm_map_min(map) [%#"PRIxVADDR"] <= start [%#"PRIxVADDR"]"
505 1.128 matt " (size=%#"PRIxVSIZE")",
506 1.128 matt vm_map_min(map), start, end - start);
507 1.78 yamt KASSERT(start < end);
508 1.102 ad KASSERT(end <= vm_map_max(map));
509 1.78 yamt
510 1.122 bouyer for (va = start; va < end;) {
511 1.122 bouyer batch_vastart = va;
512 1.122 bouyer /* create a batch of at most __PGRM_BATCH pages to free */
513 1.122 bouyer for (i = 0;
514 1.122 bouyer i < __PGRM_BATCH && va < end;
515 1.122 bouyer va += PAGE_SIZE) {
516 1.122 bouyer if (!pmap_extract(pmap_kernel(), va, &pa[i])) {
517 1.122 bouyer continue;
518 1.122 bouyer }
519 1.122 bouyer i++;
520 1.122 bouyer }
521 1.122 bouyer npgrm = i;
522 1.122 bouyer /* now remove the mappings */
523 1.124 bouyer pmap_kremove(batch_vastart, va - batch_vastart);
524 1.122 bouyer /* and free the pages */
525 1.122 bouyer for (i = 0; i < npgrm; i++) {
526 1.122 bouyer pg = PHYS_TO_VM_PAGE(pa[i]);
527 1.122 bouyer KASSERT(pg);
528 1.122 bouyer KASSERT(pg->uobject == NULL && pg->uanon == NULL);
529 1.122 bouyer KASSERT((pg->flags & PG_BUSY) == 0);
530 1.122 bouyer uvm_pagefree(pg);
531 1.40 chs }
532 1.24 thorpej }
533 1.122 bouyer #undef __PGRM_BATCH
534 1.1 mrg }
535 1.1 mrg
536 1.78 yamt #if defined(DEBUG)
537 1.78 yamt void
538 1.102 ad uvm_km_check_empty(struct vm_map *map, vaddr_t start, vaddr_t end)
539 1.78 yamt {
540 1.102 ad struct vm_page *pg;
541 1.78 yamt vaddr_t va;
542 1.78 yamt paddr_t pa;
543 1.118 matt UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
544 1.78 yamt
545 1.102 ad KDASSERT(VM_MAP_IS_KERNEL(map));
546 1.102 ad KDASSERT(vm_map_min(map) <= start);
547 1.78 yamt KDASSERT(start < end);
548 1.102 ad KDASSERT(end <= vm_map_max(map));
549 1.78 yamt
550 1.78 yamt for (va = start; va < end; va += PAGE_SIZE) {
551 1.78 yamt if (pmap_extract(pmap_kernel(), va, &pa)) {
552 1.81 simonb panic("uvm_km_check_empty: va %p has pa 0x%llx",
553 1.81 simonb (void *)va, (long long)pa);
554 1.78 yamt }
555 1.121 rmind mutex_enter(uvm_kernel_object->vmobjlock);
556 1.121 rmind pg = uvm_pagelookup(uvm_kernel_object,
557 1.121 rmind va - vm_map_min(kernel_map));
558 1.121 rmind mutex_exit(uvm_kernel_object->vmobjlock);
559 1.121 rmind if (pg) {
560 1.121 rmind panic("uvm_km_check_empty: "
561 1.121 rmind "has page hashed at %p", (const void *)va);
562 1.78 yamt }
563 1.78 yamt }
564 1.78 yamt }
565 1.78 yamt #endif /* defined(DEBUG) */
566 1.1 mrg
567 1.1 mrg /*
568 1.78 yamt * uvm_km_alloc: allocate an area of kernel memory.
569 1.1 mrg *
570 1.78 yamt * => NOTE: we can return 0 even if we can wait if there is not enough
571 1.1 mrg * free VM space in the map... caller should be prepared to handle
572 1.1 mrg * this case.
573 1.1 mrg * => we return KVA of memory allocated
574 1.1 mrg */
575 1.1 mrg
576 1.14 eeh vaddr_t
577 1.83 thorpej uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
578 1.1 mrg {
579 1.14 eeh vaddr_t kva, loopva;
580 1.14 eeh vaddr_t offset;
581 1.44 thorpej vsize_t loopsize;
582 1.8 mrg struct vm_page *pg;
583 1.78 yamt struct uvm_object *obj;
584 1.78 yamt int pgaflags;
585 1.89 drochner vm_prot_t prot;
586 1.78 yamt UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
587 1.1 mrg
588 1.40 chs KASSERT(vm_map_pmap(map) == pmap_kernel());
589 1.78 yamt KASSERT((flags & UVM_KMF_TYPEMASK) == UVM_KMF_WIRED ||
590 1.78 yamt (flags & UVM_KMF_TYPEMASK) == UVM_KMF_PAGEABLE ||
591 1.78 yamt (flags & UVM_KMF_TYPEMASK) == UVM_KMF_VAONLY);
592 1.111 matt KASSERT((flags & UVM_KMF_VAONLY) != 0 || (flags & UVM_KMF_COLORMATCH) == 0);
593 1.111 matt KASSERT((flags & UVM_KMF_COLORMATCH) == 0 || (flags & UVM_KMF_VAONLY) != 0);
594 1.1 mrg
595 1.8 mrg /*
596 1.8 mrg * setup for call
597 1.8 mrg */
598 1.8 mrg
599 1.78 yamt kva = vm_map_min(map); /* hint */
600 1.8 mrg size = round_page(size);
601 1.95 ad obj = (flags & UVM_KMF_PAGEABLE) ? uvm_kernel_object : NULL;
602 1.78 yamt UVMHIST_LOG(maphist," (map=0x%x, obj=0x%x, size=0x%x, flags=%d)",
603 1.78 yamt map, obj, size, flags);
604 1.1 mrg
605 1.8 mrg /*
606 1.8 mrg * allocate some virtual space
607 1.8 mrg */
608 1.8 mrg
609 1.78 yamt if (__predict_false(uvm_map(map, &kva, size, obj, UVM_UNKNOWN_OFFSET,
610 1.78 yamt align, UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
611 1.78 yamt UVM_ADV_RANDOM,
612 1.111 matt (flags & (UVM_KMF_TRYLOCK | UVM_KMF_NOWAIT | UVM_KMF_WAITVA
613 1.112 para | UVM_KMF_COLORMATCH)))) != 0)) {
614 1.8 mrg UVMHIST_LOG(maphist, "<- done (no VM)",0,0,0,0);
615 1.8 mrg return(0);
616 1.8 mrg }
617 1.8 mrg
618 1.8 mrg /*
619 1.8 mrg * if all we wanted was VA, return now
620 1.8 mrg */
621 1.8 mrg
622 1.78 yamt if (flags & (UVM_KMF_VAONLY | UVM_KMF_PAGEABLE)) {
623 1.8 mrg UVMHIST_LOG(maphist,"<- done valloc (kva=0x%x)", kva,0,0,0);
624 1.8 mrg return(kva);
625 1.8 mrg }
626 1.40 chs
627 1.8 mrg /*
628 1.8 mrg * recover object offset from virtual address
629 1.8 mrg */
630 1.8 mrg
631 1.8 mrg offset = kva - vm_map_min(kernel_map);
632 1.8 mrg UVMHIST_LOG(maphist, " kva=0x%x, offset=0x%x", kva, offset,0,0);
633 1.8 mrg
634 1.8 mrg /*
635 1.8 mrg * now allocate and map in the memory... note that we are the only ones
636 1.8 mrg * whom should ever get a handle on this area of VM.
637 1.8 mrg */
638 1.8 mrg
639 1.8 mrg loopva = kva;
640 1.44 thorpej loopsize = size;
641 1.78 yamt
642 1.107 matt pgaflags = UVM_FLAG_COLORMATCH;
643 1.103 ad if (flags & UVM_KMF_NOWAIT)
644 1.103 ad pgaflags |= UVM_PGA_USERESERVE;
645 1.78 yamt if (flags & UVM_KMF_ZERO)
646 1.78 yamt pgaflags |= UVM_PGA_ZERO;
647 1.89 drochner prot = VM_PROT_READ | VM_PROT_WRITE;
648 1.89 drochner if (flags & UVM_KMF_EXEC)
649 1.89 drochner prot |= VM_PROT_EXECUTE;
650 1.44 thorpej while (loopsize) {
651 1.114 matt KASSERTMSG(!pmap_extract(pmap_kernel(), loopva, NULL),
652 1.114 matt "loopva=%#"PRIxVADDR, loopva);
653 1.78 yamt
654 1.107 matt pg = uvm_pagealloc_strat(NULL, offset, NULL, pgaflags,
655 1.107 matt #ifdef UVM_KM_VMFREELIST
656 1.107 matt UVM_PGA_STRAT_ONLY, UVM_KM_VMFREELIST
657 1.107 matt #else
658 1.107 matt UVM_PGA_STRAT_NORMAL, 0
659 1.107 matt #endif
660 1.107 matt );
661 1.47 chs
662 1.8 mrg /*
663 1.8 mrg * out of memory?
664 1.8 mrg */
665 1.8 mrg
666 1.35 thorpej if (__predict_false(pg == NULL)) {
667 1.58 chs if ((flags & UVM_KMF_NOWAIT) ||
668 1.80 yamt ((flags & UVM_KMF_CANFAIL) && !uvm_reclaimable())) {
669 1.8 mrg /* free everything! */
670 1.78 yamt uvm_km_free(map, kva, size,
671 1.78 yamt flags & UVM_KMF_TYPEMASK);
672 1.58 chs return (0);
673 1.8 mrg } else {
674 1.8 mrg uvm_wait("km_getwait2"); /* sleep here */
675 1.8 mrg continue;
676 1.8 mrg }
677 1.8 mrg }
678 1.47 chs
679 1.78 yamt pg->flags &= ~PG_BUSY; /* new page */
680 1.78 yamt UVM_PAGE_OWN(pg, NULL);
681 1.78 yamt
682 1.8 mrg /*
683 1.52 chs * map it in
684 1.8 mrg */
685 1.40 chs
686 1.104 cegger pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
687 1.106 cegger prot, PMAP_KMPAGE);
688 1.8 mrg loopva += PAGE_SIZE;
689 1.8 mrg offset += PAGE_SIZE;
690 1.44 thorpej loopsize -= PAGE_SIZE;
691 1.8 mrg }
692 1.69 junyoung
693 1.112 para pmap_update(pmap_kernel());
694 1.69 junyoung
695 1.8 mrg UVMHIST_LOG(maphist,"<- done (kva=0x%x)", kva,0,0,0);
696 1.8 mrg return(kva);
697 1.1 mrg }
698 1.1 mrg
699 1.1 mrg /*
700 1.1 mrg * uvm_km_free: free an area of kernel memory
701 1.1 mrg */
702 1.1 mrg
703 1.8 mrg void
704 1.83 thorpej uvm_km_free(struct vm_map *map, vaddr_t addr, vsize_t size, uvm_flag_t flags)
705 1.8 mrg {
706 1.118 matt UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
707 1.1 mrg
708 1.78 yamt KASSERT((flags & UVM_KMF_TYPEMASK) == UVM_KMF_WIRED ||
709 1.78 yamt (flags & UVM_KMF_TYPEMASK) == UVM_KMF_PAGEABLE ||
710 1.78 yamt (flags & UVM_KMF_TYPEMASK) == UVM_KMF_VAONLY);
711 1.78 yamt KASSERT((addr & PAGE_MASK) == 0);
712 1.40 chs KASSERT(vm_map_pmap(map) == pmap_kernel());
713 1.1 mrg
714 1.8 mrg size = round_page(size);
715 1.1 mrg
716 1.78 yamt if (flags & UVM_KMF_PAGEABLE) {
717 1.78 yamt uvm_km_pgremove(addr, addr + size);
718 1.78 yamt } else if (flags & UVM_KMF_WIRED) {
719 1.109 rmind /*
720 1.109 rmind * Note: uvm_km_pgremove_intrsafe() extracts mapping, thus
721 1.109 rmind * remove it after. See comment below about KVA visibility.
722 1.109 rmind */
723 1.102 ad uvm_km_pgremove_intrsafe(map, addr, addr + size);
724 1.8 mrg }
725 1.99 yamt
726 1.99 yamt /*
727 1.109 rmind * Note: uvm_unmap_remove() calls pmap_update() for us, before
728 1.109 rmind * KVA becomes globally available.
729 1.99 yamt */
730 1.8 mrg
731 1.112 para uvm_unmap1(map, addr, addr + size, UVM_FLAG_VAONLY);
732 1.66 pk }
733 1.66 pk
734 1.10 thorpej /* Sanity; must specify both or none. */
735 1.10 thorpej #if (defined(PMAP_MAP_POOLPAGE) || defined(PMAP_UNMAP_POOLPAGE)) && \
736 1.10 thorpej (!defined(PMAP_MAP_POOLPAGE) || !defined(PMAP_UNMAP_POOLPAGE))
737 1.10 thorpej #error Must specify MAP and UNMAP together.
738 1.10 thorpej #endif
739 1.10 thorpej
740 1.112 para int
741 1.112 para uvm_km_kmem_alloc(vmem_t *vm, vmem_size_t size, vm_flag_t flags,
742 1.112 para vmem_addr_t *addr)
743 1.72 yamt {
744 1.72 yamt struct vm_page *pg;
745 1.112 para vmem_addr_t va;
746 1.112 para int rc;
747 1.112 para vaddr_t loopva;
748 1.112 para vsize_t loopsize;
749 1.72 yamt
750 1.112 para size = round_page(size);
751 1.72 yamt
752 1.112 para #if defined(PMAP_MAP_POOLPAGE)
753 1.112 para if (size == PAGE_SIZE) {
754 1.72 yamt again:
755 1.112 para #ifdef PMAP_ALLOC_POOLPAGE
756 1.112 para pg = PMAP_ALLOC_POOLPAGE((flags & VM_SLEEP) ?
757 1.112 para 0 : UVM_PGA_USERESERVE);
758 1.112 para #else
759 1.112 para pg = uvm_pagealloc(NULL, 0, NULL,
760 1.112 para (flags & VM_SLEEP) ? 0 : UVM_PGA_USERESERVE);
761 1.112 para #endif /* PMAP_ALLOC_POOLPAGE */
762 1.112 para if (__predict_false(pg == NULL)) {
763 1.112 para if (flags & VM_SLEEP) {
764 1.112 para uvm_wait("plpg");
765 1.112 para goto again;
766 1.112 para }
767 1.123 rmind return ENOMEM;
768 1.112 para }
769 1.112 para va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
770 1.112 para if (__predict_false(va == 0)) {
771 1.112 para uvm_pagefree(pg);
772 1.112 para return ENOMEM;
773 1.72 yamt }
774 1.112 para *addr = va;
775 1.112 para return 0;
776 1.72 yamt }
777 1.112 para #endif /* PMAP_MAP_POOLPAGE */
778 1.112 para
779 1.112 para rc = vmem_alloc(vm, size, flags, &va);
780 1.112 para if (rc != 0)
781 1.112 para return rc;
782 1.72 yamt
783 1.112 para loopva = va;
784 1.112 para loopsize = size;
785 1.72 yamt
786 1.112 para while (loopsize) {
787 1.128 matt #ifdef DIAGNOSTIC
788 1.128 matt paddr_t pa;
789 1.128 matt #endif
790 1.128 matt KASSERTMSG(!pmap_extract(pmap_kernel(), loopva, &pa),
791 1.128 matt "loopva=%#"PRIxVADDR" loopsize=%#"PRIxVSIZE
792 1.128 matt " pa=%#"PRIxPADDR" vmem=%p",
793 1.128 matt loopva, loopsize, pa, vm);
794 1.114 matt
795 1.114 matt pg = uvm_pagealloc(NULL, loopva, NULL,
796 1.115 matt UVM_FLAG_COLORMATCH
797 1.114 matt | ((flags & VM_SLEEP) ? 0 : UVM_PGA_USERESERVE));
798 1.112 para if (__predict_false(pg == NULL)) {
799 1.112 para if (flags & VM_SLEEP) {
800 1.112 para uvm_wait("plpg");
801 1.112 para continue;
802 1.112 para } else {
803 1.112 para uvm_km_pgremove_intrsafe(kernel_map, va,
804 1.112 para va + size);
805 1.125 yamt vmem_free(vm, va, size);
806 1.112 para return ENOMEM;
807 1.112 para }
808 1.112 para }
809 1.123 rmind
810 1.112 para pg->flags &= ~PG_BUSY; /* new page */
811 1.112 para UVM_PAGE_OWN(pg, NULL);
812 1.112 para pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
813 1.112 para VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE);
814 1.107 matt
815 1.112 para loopva += PAGE_SIZE;
816 1.112 para loopsize -= PAGE_SIZE;
817 1.15 thorpej }
818 1.112 para pmap_update(pmap_kernel());
819 1.112 para
820 1.112 para *addr = va;
821 1.16 thorpej
822 1.112 para return 0;
823 1.10 thorpej }
824 1.10 thorpej
825 1.10 thorpej void
826 1.112 para uvm_km_kmem_free(vmem_t *vm, vmem_addr_t addr, size_t size)
827 1.72 yamt {
828 1.112 para
829 1.112 para size = round_page(size);
830 1.72 yamt #if defined(PMAP_UNMAP_POOLPAGE)
831 1.112 para if (size == PAGE_SIZE) {
832 1.112 para paddr_t pa;
833 1.72 yamt
834 1.112 para pa = PMAP_UNMAP_POOLPAGE(addr);
835 1.112 para uvm_pagefree(PHYS_TO_VM_PAGE(pa));
836 1.72 yamt return;
837 1.72 yamt }
838 1.112 para #endif /* PMAP_UNMAP_POOLPAGE */
839 1.112 para uvm_km_pgremove_intrsafe(kernel_map, addr, addr + size);
840 1.112 para pmap_update(pmap_kernel());
841 1.72 yamt
842 1.112 para vmem_free(vm, addr, size);
843 1.72 yamt }
844 1.72 yamt
845 1.112 para bool
846 1.112 para uvm_km_va_starved_p(void)
847 1.10 thorpej {
848 1.112 para vmem_size_t total;
849 1.112 para vmem_size_t free;
850 1.112 para
851 1.112 para total = vmem_size(kmem_arena, VMEM_ALLOC|VMEM_FREE);
852 1.112 para free = vmem_size(kmem_arena, VMEM_FREE);
853 1.10 thorpej
854 1.112 para return (free < (total / 10));
855 1.1 mrg }
856