xref: /freebsd/sys/vm/vm_kern.c (revision 99e8005137088aafb1350e23b113d69b01b0820f)
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	from: @(#)vm_kern.c	8.3 (Berkeley) 1/12/94
37  *
38  *
39  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40  * All rights reserved.
41  *
42  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43  *
44  * Permission to use, copy, modify and distribute this software and
45  * its documentation is hereby granted, provided that both the copyright
46  * notice and this permission notice appear in all copies of the
47  * software, derivative works or modified versions, and any portions
48  * thereof, and that both notices appear in supporting documentation.
49  *
50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53  *
54  * Carnegie Mellon requests users of this software to return to
55  *
56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57  *  School of Computer Science
58  *  Carnegie Mellon University
59  *  Pittsburgh PA 15213-3890
60  *
61  * any improvements or extensions that they make and grant Carnegie the
62  * rights to redistribute these changes.
63  *
64  * $FreeBSD$
65  */
66 
67 /*
68  *	Kernel memory management.
69  */
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/lock.h>
74 #include <sys/mutex.h>
75 #include <sys/proc.h>
76 #include <sys/malloc.h>
77 
78 #include <vm/vm.h>
79 #include <vm/vm_param.h>
80 #include <vm/pmap.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_pageout.h>
85 #include <vm/vm_extern.h>
86 
87 vm_map_t kernel_map=0;
88 vm_map_t kmem_map=0;
89 vm_map_t exec_map=0;
90 vm_map_t clean_map=0;
91 vm_map_t buffer_map=0;
92 vm_map_t mb_map=0;
93 int mb_map_full=0;
94 
95 /*
96  *	kmem_alloc_pageable:
97  *
98  *	Allocate pageable memory to the kernel's address map.
99  *	"map" must be kernel_map or a submap of kernel_map.
100  */
101 
102 vm_offset_t
103 kmem_alloc_pageable(map, size)
104 	vm_map_t map;
105 	vm_size_t size;
106 {
107 	vm_offset_t addr;
108 	int result;
109 	int hadvmlock;
110 
111 	hadvmlock = mtx_owned(&vm_mtx);
112 	if (!hadvmlock)
113 		mtx_lock(&vm_mtx);
114 	size = round_page(size);
115 	addr = vm_map_min(map);
116 	result = vm_map_find(map, NULL, (vm_offset_t) 0,
117 	    &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
118 	if (!hadvmlock)
119 		mtx_unlock(&vm_mtx);
120 	if (result != KERN_SUCCESS) {
121 		return (0);
122 	}
123 	return (addr);
124 }
125 
126 /*
127  *	kmem_alloc_nofault:
128  *
129  *	Same as kmem_alloc_pageable, except that it create a nofault entry.
130  */
131 
132 vm_offset_t
133 kmem_alloc_nofault(map, size)
134 	vm_map_t map;
135 	vm_size_t size;
136 {
137 	vm_offset_t addr;
138 	int result;
139 
140 	int hadvmlock;
141 
142 	hadvmlock = mtx_owned(&vm_mtx);
143 	if (!hadvmlock)
144 		mtx_lock(&vm_mtx);
145 	size = round_page(size);
146 	addr = vm_map_min(map);
147 	result = vm_map_find(map, NULL, (vm_offset_t) 0,
148 	    &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
149 	if (!hadvmlock)
150 		mtx_unlock(&vm_mtx);
151 	if (result != KERN_SUCCESS) {
152 		return (0);
153 	}
154 	return (addr);
155 }
156 
157 /*
158  *	Allocate wired-down memory in the kernel's address map
159  *	or a submap.
160  */
161 vm_offset_t
162 kmem_alloc(map, size)
163 	vm_map_t map;
164 	vm_size_t size;
165 {
166 	vm_offset_t addr;
167 	vm_offset_t offset;
168 	vm_offset_t i;
169 	int hadvmlock;
170 
171 	hadvmlock = mtx_owned(&vm_mtx);
172 	if (!hadvmlock)
173 		mtx_lock(&vm_mtx);
174 	size = round_page(size);
175 
176 	/*
177 	 * Use the kernel object for wired-down kernel pages. Assume that no
178 	 * region of the kernel object is referenced more than once.
179 	 */
180 
181 	/*
182 	 * Locate sufficient space in the map.  This will give us the final
183 	 * virtual address for the new memory, and thus will tell us the
184 	 * offset within the kernel map.
185 	 */
186 	vm_map_lock(map);
187 	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
188 		vm_map_unlock(map);
189 		if (!hadvmlock)
190 			mtx_unlock(&vm_mtx);
191 		return (0);
192 	}
193 	offset = addr - VM_MIN_KERNEL_ADDRESS;
194 	vm_object_reference(kernel_object);
195 	vm_map_insert(map, kernel_object, offset, addr, addr + size,
196 		VM_PROT_ALL, VM_PROT_ALL, 0);
197 	vm_map_unlock(map);
198 
199 	/*
200 	 * Guarantee that there are pages already in this object before
201 	 * calling vm_map_pageable.  This is to prevent the following
202 	 * scenario:
203 	 *
204 	 * 1) Threads have swapped out, so that there is a pager for the
205 	 * kernel_object. 2) The kmsg zone is empty, and so we are
206 	 * kmem_allocing a new page for it. 3) vm_map_pageable calls vm_fault;
207 	 * there is no page, but there is a pager, so we call
208 	 * pager_data_request.  But the kmsg zone is empty, so we must
209 	 * kmem_alloc. 4) goto 1 5) Even if the kmsg zone is not empty: when
210 	 * we get the data back from the pager, it will be (very stale)
211 	 * non-zero data.  kmem_alloc is defined to return zero-filled memory.
212 	 *
213 	 * We're intentionally not activating the pages we allocate to prevent a
214 	 * race with page-out.  vm_map_pageable will wire the pages.
215 	 */
216 
217 	for (i = 0; i < size; i += PAGE_SIZE) {
218 		vm_page_t mem;
219 
220 		mem = vm_page_grab(kernel_object, OFF_TO_IDX(offset + i),
221 				VM_ALLOC_ZERO | VM_ALLOC_RETRY);
222 		if ((mem->flags & PG_ZERO) == 0)
223 			vm_page_zero_fill(mem);
224 		mem->valid = VM_PAGE_BITS_ALL;
225 		vm_page_flag_clear(mem, PG_ZERO);
226 		vm_page_wakeup(mem);
227 	}
228 
229 	/*
230 	 * And finally, mark the data as non-pageable.
231 	 */
232 
233 	(void) vm_map_pageable(map, (vm_offset_t) addr, addr + size, FALSE);
234 
235 	if (!hadvmlock)
236 		mtx_unlock(&vm_mtx);
237 	return (addr);
238 }
239 
240 /*
241  *	kmem_free:
242  *
243  *	Release a region of kernel virtual memory allocated
244  *	with kmem_alloc, and return the physical pages
245  *	associated with that region.
246  *
247  *	This routine may not block on kernel maps.
248  */
249 void
250 kmem_free(map, addr, size)
251 	vm_map_t map;
252 	vm_offset_t addr;
253 	vm_size_t size;
254 {
255 	int hadvmlock;
256 
257 	hadvmlock = mtx_owned(&vm_mtx);
258 	if (!hadvmlock)
259 		mtx_lock(&vm_mtx);
260 
261 	(void) vm_map_remove(map, trunc_page(addr), round_page(addr + size));
262 
263 	if (!hadvmlock)
264 		mtx_unlock(&vm_mtx);
265 }
266 
267 /*
268  *	kmem_suballoc:
269  *
270  *	Allocates a map to manage a subrange
271  *	of the kernel virtual address space.
272  *
273  *	Arguments are as follows:
274  *
275  *	parent		Map to take range from
276  *	min, max	Returned endpoints of map
277  *	size		Size of range to find
278  */
279 vm_map_t
280 kmem_suballoc(parent, min, max, size)
281 	vm_map_t parent;
282 	vm_offset_t *min, *max;
283 	vm_size_t size;
284 {
285 	int ret;
286 	vm_map_t result;
287 	int hadvmlock;
288 
289 	hadvmlock = mtx_owned(&vm_mtx);
290 	if (!hadvmlock)
291 		mtx_lock(&vm_mtx);
292 
293 	size = round_page(size);
294 
295 	*min = (vm_offset_t) vm_map_min(parent);
296 	ret = vm_map_find(parent, NULL, (vm_offset_t) 0,
297 	    min, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
298 	if (ret != KERN_SUCCESS) {
299 		printf("kmem_suballoc: bad status return of %d.\n", ret);
300 		panic("kmem_suballoc");
301 	}
302 	*max = *min + size;
303 	pmap_reference(vm_map_pmap(parent));
304 	result = vm_map_create(vm_map_pmap(parent), *min, *max);
305 	if (result == NULL)
306 		panic("kmem_suballoc: cannot create submap");
307 	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
308 		panic("kmem_suballoc: unable to change range to submap");
309 	if (!hadvmlock)
310 		mtx_unlock(&vm_mtx);
311 	return (result);
312 }
313 
314 /*
315  *	kmem_malloc:
316  *
317  * 	Allocate wired-down memory in the kernel's address map for the higher
318  * 	level kernel memory allocator (kern/kern_malloc.c).  We cannot use
319  * 	kmem_alloc() because we may need to allocate memory at interrupt
320  * 	level where we cannot block (canwait == FALSE).
321  *
322  * 	This routine has its own private kernel submap (kmem_map) and object
323  * 	(kmem_object).  This, combined with the fact that only malloc uses
324  * 	this routine, ensures that we will never block in map or object waits.
325  *
326  * 	Note that this still only works in a uni-processor environment and
327  * 	when called at splhigh().
328  *
329  * 	We don't worry about expanding the map (adding entries) since entries
330  * 	for wired maps are statically allocated.
331  *
332  *	NOTE:  This routine is not supposed to block if M_NOWAIT is set, but
333  *	I have not verified that it actually does not block.
334  */
335 vm_offset_t
336 kmem_malloc(map, size, flags)
337 	vm_map_t map;
338 	vm_size_t size;
339 	int flags;
340 {
341 	vm_offset_t offset, i;
342 	vm_map_entry_t entry;
343 	vm_offset_t addr;
344 	vm_page_t m;
345 	int hadvmlock;
346 
347 	if (map != kmem_map && map != mb_map)
348 		panic("kmem_malloc: map != {kmem,mb}_map");
349 
350 	hadvmlock = mtx_owned(&vm_mtx);
351 	if (!hadvmlock)
352 		mtx_lock(&vm_mtx);
353 
354 	size = round_page(size);
355 	addr = vm_map_min(map);
356 
357 	/*
358 	 * Locate sufficient space in the map.  This will give us the final
359 	 * virtual address for the new memory, and thus will tell us the
360 	 * offset within the kernel map.
361 	 */
362 	vm_map_lock(map);
363 	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
364 		vm_map_unlock(map);
365 		if (map == mb_map) {
366 			mb_map_full = TRUE;
367 			printf("Out of mbuf clusters - adjust NMBCLUSTERS or increase maxusers!\n");
368 			goto bad;
369 		}
370 		if ((flags & M_NOWAIT) == 0)
371 			panic("kmem_malloc(%ld): kmem_map too small: %ld total allocated",
372 				(long)size, (long)map->size);
373 		goto bad;
374 	}
375 	offset = addr - VM_MIN_KERNEL_ADDRESS;
376 	vm_object_reference(kmem_object);
377 	vm_map_insert(map, kmem_object, offset, addr, addr + size,
378 		VM_PROT_ALL, VM_PROT_ALL, 0);
379 
380 	for (i = 0; i < size; i += PAGE_SIZE) {
381 		/*
382 		 * Note: if M_NOWAIT specified alone, allocate from
383 		 * interrupt-safe queues only (just the free list).  If
384 		 * M_ASLEEP or M_USE_RESERVE is also specified, we can also
385 		 * allocate from the cache.  Neither of the latter two
386 		 * flags may be specified from an interrupt since interrupts
387 		 * are not allowed to mess with the cache queue.
388 		 */
389 retry:
390 		m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i),
391 		    ((flags & (M_NOWAIT|M_ASLEEP|M_USE_RESERVE)) == M_NOWAIT) ?
392 			VM_ALLOC_INTERRUPT :
393 			VM_ALLOC_SYSTEM);
394 
395 		/*
396 		 * Ran out of space, free everything up and return. Don't need
397 		 * to lock page queues here as we know that the pages we got
398 		 * aren't on any queues.
399 		 */
400 		if (m == NULL) {
401 			if ((flags & M_NOWAIT) == 0) {
402 				vm_map_unlock(map);
403 				VM_WAIT;
404 				vm_map_lock(map);
405 				goto retry;
406 			}
407 			vm_map_delete(map, addr, addr + size);
408 			vm_map_unlock(map);
409 			if (flags & M_ASLEEP) {
410 				VM_AWAIT;
411 			}
412 			goto bad;
413 		}
414 		vm_page_flag_clear(m, PG_ZERO);
415 		m->valid = VM_PAGE_BITS_ALL;
416 	}
417 
418 	/*
419 	 * Mark map entry as non-pageable. Assert: vm_map_insert() will never
420 	 * be able to extend the previous entry so there will be a new entry
421 	 * exactly corresponding to this address range and it will have
422 	 * wired_count == 0.
423 	 */
424 	if (!vm_map_lookup_entry(map, addr, &entry) ||
425 	    entry->start != addr || entry->end != addr + size ||
426 	    entry->wired_count != 0)
427 		panic("kmem_malloc: entry not found or misaligned");
428 	entry->wired_count = 1;
429 
430 	vm_map_simplify_entry(map, entry);
431 
432 	/*
433 	 * Loop thru pages, entering them in the pmap. (We cannot add them to
434 	 * the wired count without wrapping the vm_page_queue_lock in
435 	 * splimp...)
436 	 */
437 	for (i = 0; i < size; i += PAGE_SIZE) {
438 		m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i));
439 		vm_page_wire(m);
440 		vm_page_wakeup(m);
441 		/*
442 		 * Because this is kernel_pmap, this call will not block.
443 		 */
444 		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 1);
445 		vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE | PG_REFERENCED);
446 	}
447 	vm_map_unlock(map);
448 
449 	if (!hadvmlock)
450 		mtx_unlock(&vm_mtx);
451 	return (addr);
452 
453 bad:
454 	if (!hadvmlock)
455 		mtx_unlock(&vm_mtx);
456 	return (0);
457 }
458 
459 /*
460  *	kmem_alloc_wait:
461  *
462  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
463  *	has no room, the caller sleeps waiting for more memory in the submap.
464  *
465  *	This routine may block.
466  */
467 
468 vm_offset_t
469 kmem_alloc_wait(map, size)
470 	vm_map_t map;
471 	vm_size_t size;
472 {
473 	vm_offset_t addr;
474 	int hadvmlock;
475 
476 	hadvmlock = mtx_owned(&vm_mtx);
477 	if (!hadvmlock)
478 		mtx_lock(&vm_mtx);
479 
480 	size = round_page(size);
481 
482 	for (;;) {
483 		/*
484 		 * To make this work for more than one map, use the map's lock
485 		 * to lock out sleepers/wakers.
486 		 */
487 		vm_map_lock(map);
488 		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
489 			break;
490 		/* no space now; see if we can ever get space */
491 		if (vm_map_max(map) - vm_map_min(map) < size) {
492 			vm_map_unlock(map);
493 			if (!hadvmlock)
494 				mtx_unlock(&vm_mtx);
495 			return (0);
496 		}
497 		vm_map_unlock(map);
498 		msleep(map, &vm_mtx, PVM, "kmaw", 0);
499 	}
500 	vm_map_insert(map, NULL, (vm_offset_t) 0, addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
501 	vm_map_unlock(map);
502 	if (!hadvmlock)
503 		mtx_unlock(&vm_mtx);
504 	return (addr);
505 }
506 
507 /*
508  *	kmem_free_wakeup:
509  *
510  *	Returns memory to a submap of the kernel, and wakes up any processes
511  *	waiting for memory in that map.
512  */
513 void
514 kmem_free_wakeup(map, addr, size)
515 	vm_map_t map;
516 	vm_offset_t addr;
517 	vm_size_t size;
518 {
519 	int hadvmlock;
520 
521 	hadvmlock = mtx_owned(&vm_mtx);
522 	if (!hadvmlock)
523 		mtx_lock(&vm_mtx);
524 	vm_map_lock(map);
525 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
526 	wakeup(map);
527 	vm_map_unlock(map);
528 	if (!hadvmlock)
529 		mtx_unlock(&vm_mtx);
530 }
531 
532 /*
533  * 	kmem_init:
534  *
535  *	Create the kernel map; insert a mapping covering kernel text,
536  *	data, bss, and all space allocated thus far (`boostrap' data).  The
537  *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
538  *	`start' as allocated, and the range between `start' and `end' as free.
539  */
540 
541 void
542 kmem_init(start, end)
543 	vm_offset_t start, end;
544 {
545 	vm_map_t m;
546 
547 	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
548 	vm_map_lock(m);
549 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
550 	kernel_map = m;
551 	kernel_map->system_map = 1;
552 	(void) vm_map_insert(m, NULL, (vm_offset_t) 0,
553 	    VM_MIN_KERNEL_ADDRESS, start, VM_PROT_ALL, VM_PROT_ALL, 0);
554 	/* ... and ending with the completion of the above `insert' */
555 	vm_map_unlock(m);
556 }
557