1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * The Mach Operating System project at Carnegie-Mellon University.
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * from: @(#)vm_kern.c 8.3 (Berkeley) 1/12/94
33 *
34 *
35 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36 * All rights reserved.
37 *
38 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39 *
40 * Permission to use, copy, modify and distribute this software and
41 * its documentation is hereby granted, provided that both the copyright
42 * notice and this permission notice appear in all copies of the
43 * software, derivative works or modified versions, and any portions
44 * thereof, and that both notices appear in supporting documentation.
45 *
46 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49 *
50 * Carnegie Mellon requests users of this software to return to
51 *
52 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53 * School of Computer Science
54 * Carnegie Mellon University
55 * Pittsburgh PA 15213-3890
56 *
57 * any improvements or extensions that they make and grant Carnegie the
58 * rights to redistribute these changes.
59 */
60
61 /*
62 * Kernel memory management.
63 */
64
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h> /* for ticks and hz */
71 #include <sys/eventhandler.h>
72 #include <sys/lock.h>
73 #include <sys/proc.h>
74 #include <sys/malloc.h>
75 #include <sys/rwlock.h>
76 #include <sys/sysctl.h>
77 #include <sys/vmem.h>
78
79 #include <vm/vm.h>
80 #include <vm/vm_param.h>
81 #include <vm/vm_kern.h>
82 #include <vm/pmap.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_pageout.h>
87 #include <vm/vm_extern.h>
88 #include <vm/uma.h>
89
90 vm_map_t kernel_map;
91 vm_map_t exec_map;
92 vm_map_t pipe_map;
93
94 const void *zero_region;
95 CTASSERT((ZERO_REGION_SIZE & PAGE_MASK) == 0);
96
97 /* NB: Used by kernel debuggers. */
98 const u_long vm_maxuser_address = VM_MAXUSER_ADDRESS;
99
100 SYSCTL_ULONG(_vm, OID_AUTO, min_kernel_address, CTLFLAG_RD,
101 SYSCTL_NULL_ULONG_PTR, VM_MIN_KERNEL_ADDRESS, "Min kernel address");
102
103 SYSCTL_ULONG(_vm, OID_AUTO, max_kernel_address, CTLFLAG_RD,
104 #if defined(__arm__) || defined(__sparc64__)
105 &vm_max_kernel_address, 0,
106 #else
107 SYSCTL_NULL_ULONG_PTR, VM_MAX_KERNEL_ADDRESS,
108 #endif
109 "Max kernel address");
110
111 /*
112 * kva_alloc:
113 *
114 * Allocate a virtual address range with no underlying object and
115 * no initial mapping to physical memory. Any mapping from this
116 * range to physical memory must be explicitly created prior to
117 * its use, typically with pmap_qenter(). Any attempt to create
118 * a mapping on demand through vm_fault() will result in a panic.
119 */
120 vm_offset_t
kva_alloc(size)121 kva_alloc(size)
122 vm_size_t size;
123 {
124 vm_offset_t addr;
125
126 size = round_page(size);
127 if (vmem_alloc(kernel_arena, size, M_BESTFIT | M_NOWAIT, &addr))
128 return (0);
129
130 return (addr);
131 }
132
133 /*
134 * kva_free:
135 *
136 * Release a region of kernel virtual memory allocated
137 * with kva_alloc, and return the physical pages
138 * associated with that region.
139 *
140 * This routine may not block on kernel maps.
141 */
142 void
kva_free(addr,size)143 kva_free(addr, size)
144 vm_offset_t addr;
145 vm_size_t size;
146 {
147
148 size = round_page(size);
149 vmem_free(kernel_arena, addr, size);
150 }
151
152 /*
153 * Allocates a region from the kernel address map and physical pages
154 * within the specified address range to the kernel object. Creates a
155 * wired mapping from this region to these pages, and returns the
156 * region's starting virtual address. The allocated pages are not
157 * necessarily physically contiguous. If M_ZERO is specified through the
158 * given flags, then the pages are zeroed before they are mapped.
159 */
160 vm_offset_t
kmem_alloc_attr(vmem_t * vmem,vm_size_t size,int flags,vm_paddr_t low,vm_paddr_t high,vm_memattr_t memattr)161 kmem_alloc_attr(vmem_t *vmem, vm_size_t size, int flags, vm_paddr_t low,
162 vm_paddr_t high, vm_memattr_t memattr)
163 {
164 vm_object_t object = vmem == kmem_arena ? kmem_object : kernel_object;
165 vm_offset_t addr, i;
166 vm_ooffset_t offset;
167 vm_page_t m;
168 int pflags, level;
169
170 size = round_page(size);
171 if (vmem_alloc(vmem, size, M_BESTFIT | flags, &addr))
172 return (0);
173 offset = addr - VM_MIN_KERNEL_ADDRESS;
174 pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
175 VM_OBJECT_WLOCK(object);
176 for (i = 0; i < size; i += PAGE_SIZE) {
177 level = 0;
178 retry:
179 m = vm_page_alloc_contig(object, OFF_TO_IDX(offset + i),
180 pflags, 1, low, high, PAGE_SIZE, 0, memattr);
181 if (m == NULL) {
182 VM_OBJECT_WUNLOCK(object);
183 if (level < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
184 vm_pageout_reclaim_contig(1, low, high,
185 PAGE_SIZE, 0, level);
186 VM_OBJECT_WLOCK(object);
187 level++;
188 goto retry;
189 }
190 kmem_unback(object, addr, i);
191 vmem_free(vmem, addr, size);
192 return (0);
193 }
194 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
195 pmap_zero_page(m);
196 m->valid = VM_PAGE_BITS_ALL;
197 pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL,
198 VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
199 }
200 VM_OBJECT_WUNLOCK(object);
201 return (addr);
202 }
203
204 /*
205 * Allocates a region from the kernel address map and physically
206 * contiguous pages within the specified address range to the kernel
207 * object. Creates a wired mapping from this region to these pages, and
208 * returns the region's starting virtual address. If M_ZERO is specified
209 * through the given flags, then the pages are zeroed before they are
210 * mapped.
211 */
212 vm_offset_t
kmem_alloc_contig(struct vmem * vmem,vm_size_t size,int flags,vm_paddr_t low,vm_paddr_t high,u_long alignment,vm_paddr_t boundary,vm_memattr_t memattr)213 kmem_alloc_contig(struct vmem *vmem, vm_size_t size, int flags, vm_paddr_t low,
214 vm_paddr_t high, u_long alignment, vm_paddr_t boundary,
215 vm_memattr_t memattr)
216 {
217 vm_object_t object = vmem == kmem_arena ? kmem_object : kernel_object;
218 vm_offset_t addr, tmp;
219 vm_ooffset_t offset;
220 vm_page_t end_m, m;
221 int pflags, level;
222 u_long npages;
223
224 size = round_page(size);
225 npages = atop(size);
226 if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
227 return (0);
228 offset = addr - VM_MIN_KERNEL_ADDRESS;
229 pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
230 VM_OBJECT_WLOCK(object);
231 level = 0;
232 retry:
233 m = vm_page_alloc_contig(object, OFF_TO_IDX(offset), pflags,
234 npages, low, high, alignment, boundary, memattr);
235 if (m == NULL) {
236 VM_OBJECT_WUNLOCK(object);
237 if (level < ((flags & M_NOWAIT) != 0 ? 1 : 3)) {
238 vm_pageout_reclaim_contig(atop(size), low, high,
239 alignment, boundary, level);
240 VM_OBJECT_WLOCK(object);
241 level++;
242 goto retry;
243 }
244 vmem_free(vmem, addr, size);
245 return (0);
246 }
247 end_m = m + npages;
248 tmp = addr;
249 for (; m < end_m; m++) {
250 if ((flags & M_ZERO) && (m->flags & PG_ZERO) == 0)
251 pmap_zero_page(m);
252 m->valid = VM_PAGE_BITS_ALL;
253 pmap_enter(kernel_pmap, tmp, m, VM_PROT_ALL,
254 VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
255 tmp += PAGE_SIZE;
256 }
257 VM_OBJECT_WUNLOCK(object);
258 return (addr);
259 }
260
261 /*
262 * kmem_suballoc:
263 *
264 * Allocates a map to manage a subrange
265 * of the kernel virtual address space.
266 *
267 * Arguments are as follows:
268 *
269 * parent Map to take range from
270 * min, max Returned endpoints of map
271 * size Size of range to find
272 * superpage_align Request that min is superpage aligned
273 */
274 vm_map_t
kmem_suballoc(vm_map_t parent,vm_offset_t * min,vm_offset_t * max,vm_size_t size,boolean_t superpage_align)275 kmem_suballoc(vm_map_t parent, vm_offset_t *min, vm_offset_t *max,
276 vm_size_t size, boolean_t superpage_align)
277 {
278 int ret;
279 vm_map_t result;
280
281 size = round_page(size);
282
283 *min = vm_map_min(parent);
284 ret = vm_map_find(parent, NULL, 0, min, size, 0, superpage_align ?
285 VMFS_SUPER_SPACE : VMFS_ANY_SPACE, VM_PROT_ALL, VM_PROT_ALL,
286 MAP_ACC_NO_CHARGE);
287 if (ret != KERN_SUCCESS)
288 panic("kmem_suballoc: bad status return of %d", ret);
289 *max = *min + size;
290 result = vm_map_create(vm_map_pmap(parent), *min, *max);
291 if (result == NULL)
292 panic("kmem_suballoc: cannot create submap");
293 if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
294 panic("kmem_suballoc: unable to change range to submap");
295 return (result);
296 }
297
298 /*
299 * kmem_malloc:
300 *
301 * Allocate wired-down pages in the kernel's address space.
302 */
303 vm_offset_t
kmem_malloc(struct vmem * vmem,vm_size_t size,int flags)304 kmem_malloc(struct vmem *vmem, vm_size_t size, int flags)
305 {
306 vm_offset_t addr;
307 int rv;
308
309 size = round_page(size);
310 if (vmem_alloc(vmem, size, flags | M_BESTFIT, &addr))
311 return (0);
312
313 rv = kmem_back((vmem == kmem_arena) ? kmem_object : kernel_object,
314 addr, size, flags);
315 if (rv != KERN_SUCCESS) {
316 vmem_free(vmem, addr, size);
317 return (0);
318 }
319 return (addr);
320 }
321
322 /*
323 * kmem_back:
324 *
325 * Allocate physical pages for the specified virtual address range.
326 */
327 int
kmem_back(vm_object_t object,vm_offset_t addr,vm_size_t size,int flags)328 kmem_back(vm_object_t object, vm_offset_t addr, vm_size_t size, int flags)
329 {
330 vm_offset_t offset, i;
331 vm_page_t m;
332 int pflags;
333
334 KASSERT(object == kmem_object || object == kernel_object,
335 ("kmem_back: only supports kernel objects."));
336
337 offset = addr - VM_MIN_KERNEL_ADDRESS;
338 pflags = malloc2vm_flags(flags) | VM_ALLOC_NOBUSY | VM_ALLOC_WIRED;
339
340 VM_OBJECT_WLOCK(object);
341 for (i = 0; i < size; i += PAGE_SIZE) {
342 retry:
343 m = vm_page_alloc(object, OFF_TO_IDX(offset + i), pflags);
344
345 /*
346 * Ran out of space, free everything up and return. Don't need
347 * to lock page queues here as we know that the pages we got
348 * aren't on any queues.
349 */
350 if (m == NULL) {
351 VM_OBJECT_WUNLOCK(object);
352 if ((flags & M_NOWAIT) == 0) {
353 VM_WAIT;
354 VM_OBJECT_WLOCK(object);
355 goto retry;
356 }
357 kmem_unback(object, addr, i);
358 return (KERN_NO_SPACE);
359 }
360 if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
361 pmap_zero_page(m);
362 KASSERT((m->oflags & VPO_UNMANAGED) != 0,
363 ("kmem_malloc: page %p is managed", m));
364 m->valid = VM_PAGE_BITS_ALL;
365 pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL,
366 VM_PROT_ALL | PMAP_ENTER_WIRED, 0);
367 }
368 VM_OBJECT_WUNLOCK(object);
369
370 return (KERN_SUCCESS);
371 }
372
373 /*
374 * kmem_unback:
375 *
376 * Unmap and free the physical pages underlying the specified virtual
377 * address range.
378 *
379 * A physical page must exist within the specified object at each index
380 * that is being unmapped.
381 */
382 void
kmem_unback(vm_object_t object,vm_offset_t addr,vm_size_t size)383 kmem_unback(vm_object_t object, vm_offset_t addr, vm_size_t size)
384 {
385 vm_page_t m;
386 vm_offset_t i, offset;
387
388 KASSERT(object == kmem_object || object == kernel_object,
389 ("kmem_unback: only supports kernel objects."));
390
391 pmap_remove(kernel_pmap, addr, addr + size);
392 offset = addr - VM_MIN_KERNEL_ADDRESS;
393 VM_OBJECT_WLOCK(object);
394 for (i = 0; i < size; i += PAGE_SIZE) {
395 m = vm_page_lookup(object, OFF_TO_IDX(offset + i));
396 vm_page_unwire(m, PQ_NONE);
397 vm_page_free(m);
398 }
399 VM_OBJECT_WUNLOCK(object);
400 }
401
402 /*
403 * kmem_free:
404 *
405 * Free memory allocated with kmem_malloc. The size must match the
406 * original allocation.
407 */
408 void
kmem_free(struct vmem * vmem,vm_offset_t addr,vm_size_t size)409 kmem_free(struct vmem *vmem, vm_offset_t addr, vm_size_t size)
410 {
411
412 size = round_page(size);
413 kmem_unback((vmem == kmem_arena) ? kmem_object : kernel_object,
414 addr, size);
415 vmem_free(vmem, addr, size);
416 }
417
418 /*
419 * kmap_alloc_wait:
420 *
421 * Allocates pageable memory from a sub-map of the kernel. If the submap
422 * has no room, the caller sleeps waiting for more memory in the submap.
423 *
424 * This routine may block.
425 */
426 vm_offset_t
kmap_alloc_wait(map,size)427 kmap_alloc_wait(map, size)
428 vm_map_t map;
429 vm_size_t size;
430 {
431 vm_offset_t addr;
432
433 size = round_page(size);
434 if (!swap_reserve(size))
435 return (0);
436
437 for (;;) {
438 /*
439 * To make this work for more than one map, use the map's lock
440 * to lock out sleepers/wakers.
441 */
442 vm_map_lock(map);
443 if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
444 break;
445 /* no space now; see if we can ever get space */
446 if (vm_map_max(map) - vm_map_min(map) < size) {
447 vm_map_unlock(map);
448 swap_release(size);
449 return (0);
450 }
451 map->needs_wakeup = TRUE;
452 vm_map_unlock_and_wait(map, 0);
453 }
454 vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL,
455 VM_PROT_ALL, MAP_ACC_CHARGED);
456 vm_map_unlock(map);
457 return (addr);
458 }
459
460 /*
461 * kmap_free_wakeup:
462 *
463 * Returns memory to a submap of the kernel, and wakes up any processes
464 * waiting for memory in that map.
465 */
466 void
kmap_free_wakeup(map,addr,size)467 kmap_free_wakeup(map, addr, size)
468 vm_map_t map;
469 vm_offset_t addr;
470 vm_size_t size;
471 {
472
473 vm_map_lock(map);
474 (void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
475 if (map->needs_wakeup) {
476 map->needs_wakeup = FALSE;
477 vm_map_wakeup(map);
478 }
479 vm_map_unlock(map);
480 }
481
482 void
kmem_init_zero_region(void)483 kmem_init_zero_region(void)
484 {
485 vm_offset_t addr, i;
486 vm_page_t m;
487
488 /*
489 * Map a single physical page of zeros to a larger virtual range.
490 * This requires less looping in places that want large amounts of
491 * zeros, while not using much more physical resources.
492 */
493 addr = kva_alloc(ZERO_REGION_SIZE);
494 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
495 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
496 if ((m->flags & PG_ZERO) == 0)
497 pmap_zero_page(m);
498 for (i = 0; i < ZERO_REGION_SIZE; i += PAGE_SIZE)
499 pmap_qenter(addr + i, &m, 1);
500 pmap_protect(kernel_pmap, addr, addr + ZERO_REGION_SIZE, VM_PROT_READ);
501
502 zero_region = (const void *)addr;
503 }
504
505 /*
506 * kmem_init:
507 *
508 * Create the kernel map; insert a mapping covering kernel text,
509 * data, bss, and all space allocated thus far (`boostrap' data). The
510 * new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
511 * `start' as allocated, and the range between `start' and `end' as free.
512 */
513 void
kmem_init(start,end)514 kmem_init(start, end)
515 vm_offset_t start, end;
516 {
517 vm_map_t m;
518
519 m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
520 m->system_map = 1;
521 vm_map_lock(m);
522 /* N.B.: cannot use kgdb to debug, starting with this assignment ... */
523 kernel_map = m;
524 (void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
525 #ifdef __amd64__
526 KERNBASE,
527 #else
528 VM_MIN_KERNEL_ADDRESS,
529 #endif
530 start, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
531 /* ... and ending with the completion of the above `insert' */
532 vm_map_unlock(m);
533 }
534
535 #ifdef DIAGNOSTIC
536 /*
537 * Allow userspace to directly trigger the VM drain routine for testing
538 * purposes.
539 */
540 static int
debug_vm_lowmem(SYSCTL_HANDLER_ARGS)541 debug_vm_lowmem(SYSCTL_HANDLER_ARGS)
542 {
543 int error, i;
544
545 i = 0;
546 error = sysctl_handle_int(oidp, &i, 0, req);
547 if (error)
548 return (error);
549 if (i)
550 EVENTHANDLER_INVOKE(vm_lowmem, 0);
551 return (0);
552 }
553
554 SYSCTL_PROC(_debug, OID_AUTO, vm_lowmem, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
555 debug_vm_lowmem, "I", "set to trigger vm_lowmem event");
556 #endif
557