xref: /dragonfly/sys/vm/vm_kern.c (revision 23265324)
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: src/sys/vm/vm_kern.c,v 1.61.2.2 2002/03/12 18:25:26 tegge Exp $
65  * $DragonFly: src/sys/vm/vm_kern.c,v 1.28 2007/01/02 04:52:31 dillon Exp $
66  */
67 
68 /*
69  *	Kernel memory management.
70  */
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/proc.h>
75 #include <sys/malloc.h>
76 #include <sys/kernel.h>
77 #include <sys/sysctl.h>
78 
79 #include <vm/vm.h>
80 #include <vm/vm_param.h>
81 #include <sys/lock.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_kern.h>
88 #include <vm/vm_extern.h>
89 
90 struct vm_map kernel_map;
91 struct vm_map exec_map;
92 struct vm_map clean_map;
93 struct vm_map buffer_map;
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 vm_offset_t
102 kmem_alloc_pageable(vm_map_t map, vm_size_t size)
103 {
104 	vm_offset_t addr;
105 	int result;
106 
107 	size = round_page(size);
108 	addr = vm_map_min(map);
109 	result = vm_map_find(map, NULL, (vm_offset_t) 0,
110 			     &addr, size,
111 			     TRUE,
112 			     VM_MAPTYPE_NORMAL,
113 			     VM_PROT_ALL, VM_PROT_ALL,
114 			     0);
115 	if (result != KERN_SUCCESS) {
116 		return (0);
117 	}
118 	return (addr);
119 }
120 
121 /*
122  *	kmem_alloc_nofault:
123  *
124  *	Same as kmem_alloc_pageable, except that it create a nofault entry.
125  */
126 vm_offset_t
127 kmem_alloc_nofault(vm_map_t map, vm_size_t size)
128 {
129 	vm_offset_t addr;
130 	int result;
131 
132 	size = round_page(size);
133 	addr = vm_map_min(map);
134 	result = vm_map_find(map, NULL, (vm_offset_t) 0,
135 			     &addr, size,
136 			     TRUE,
137 			     VM_MAPTYPE_NORMAL,
138 			     VM_PROT_ALL, VM_PROT_ALL,
139 			     MAP_NOFAULT);
140 	if (result != KERN_SUCCESS) {
141 		return (0);
142 	}
143 	return (addr);
144 }
145 
146 /*
147  *	Allocate wired-down memory in the kernel's address map
148  *	or a submap.
149  */
150 vm_offset_t
151 kmem_alloc3(vm_map_t map, vm_size_t size, int kmflags)
152 {
153 	vm_offset_t addr;
154 	vm_offset_t i;
155 	int count;
156 
157 	size = round_page(size);
158 
159 	if (kmflags & KM_KRESERVE)
160 		count = vm_map_entry_kreserve(MAP_RESERVE_COUNT);
161 	else
162 		count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
163 
164 	/*
165 	 * Use the kernel object for wired-down kernel pages. Assume that no
166 	 * region of the kernel object is referenced more than once.
167 	 *
168 	 * Locate sufficient space in the map.  This will give us the final
169 	 * virtual address for the new memory, and thus will tell us the
170 	 * offset within the kernel map.
171 	 */
172 	vm_map_lock(map);
173 	if (vm_map_findspace(map, vm_map_min(map), size, 1, &addr)) {
174 		vm_map_unlock(map);
175 		if (kmflags & KM_KRESERVE)
176 			vm_map_entry_krelease(count);
177 		else
178 			vm_map_entry_release(count);
179 		return (0);
180 	}
181 	vm_object_reference(&kernel_object);
182 	vm_map_insert(map, &count,
183 		      &kernel_object, addr, addr, addr + size,
184 		      VM_MAPTYPE_NORMAL,
185 		      VM_PROT_ALL, VM_PROT_ALL,
186 		      0);
187 	vm_map_unlock(map);
188 	if (kmflags & KM_KRESERVE)
189 		vm_map_entry_krelease(count);
190 	else
191 		vm_map_entry_release(count);
192 
193 	/*
194 	 * Guarantee that there are pages already in this object before
195 	 * calling vm_map_wire.  This is to prevent the following
196 	 * scenario:
197 	 *
198 	 * 1) Threads have swapped out, so that there is a pager for the
199 	 * kernel_object. 2) The kmsg zone is empty, and so we are
200 	 * kmem_allocing a new page for it. 3) vm_map_wire calls vm_fault;
201 	 * there is no page, but there is a pager, so we call
202 	 * pager_data_request.  But the kmsg zone is empty, so we must
203 	 * kmem_alloc. 4) goto 1 5) Even if the kmsg zone is not empty: when
204 	 * we get the data back from the pager, it will be (very stale)
205 	 * non-zero data.  kmem_alloc is defined to return zero-filled memory.
206 	 *
207 	 * We're intentionally not activating the pages we allocate to prevent a
208 	 * race with page-out.  vm_map_wire will wire the pages.
209 	 */
210 
211 	for (i = 0; i < size; i += PAGE_SIZE) {
212 		vm_page_t mem;
213 
214 		mem = vm_page_grab(&kernel_object, OFF_TO_IDX(addr + i),
215 			    VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
216 		if ((mem->flags & PG_ZERO) == 0)
217 			vm_page_zero_fill(mem);
218 		mem->valid = VM_PAGE_BITS_ALL;
219 		vm_page_flag_clear(mem, PG_ZERO);
220 		vm_page_wakeup(mem);
221 	}
222 
223 	/*
224 	 * And finally, mark the data as non-pageable.
225 	 */
226 
227 	vm_map_wire(map, (vm_offset_t) addr, addr + size, kmflags);
228 
229 	return (addr);
230 }
231 
232 /*
233  *	kmem_free:
234  *
235  *	Release a region of kernel virtual memory allocated
236  *	with kmem_alloc, and return the physical pages
237  *	associated with that region.
238  *
239  *	This routine may not block on kernel maps.
240  */
241 void
242 kmem_free(vm_map_t map, vm_offset_t addr, vm_size_t size)
243 {
244 	vm_map_remove(map, trunc_page(addr), round_page(addr + size));
245 }
246 
247 /*
248  *	kmem_suballoc:
249  *
250  *	Used to break a system map into smaller maps, usually to reduce
251  *	contention and to provide large KVA spaces for subsystems like the
252  *	buffer cache.
253  *
254  *	parent		Map to take range from
255  *	result
256  *	size		Size of range to find
257  *	min, max	Returned endpoints of map
258  *	pageable	Can the region be paged
259  */
260 void
261 kmem_suballoc(vm_map_t parent, vm_map_t result,
262 	      vm_offset_t *min, vm_offset_t *max, vm_size_t size)
263 {
264 	int ret;
265 
266 	size = round_page(size);
267 
268 	*min = (vm_offset_t) vm_map_min(parent);
269 	ret = vm_map_find(parent, NULL, (vm_offset_t) 0,
270 			  min, size,
271 			  TRUE,
272 			  VM_MAPTYPE_UNSPECIFIED,
273 			  VM_PROT_ALL, VM_PROT_ALL,
274 			  0);
275 	if (ret != KERN_SUCCESS) {
276 		kprintf("kmem_suballoc: bad status return of %d.\n", ret);
277 		panic("kmem_suballoc");
278 	}
279 	*max = *min + size;
280 	pmap_reference(vm_map_pmap(parent));
281 	vm_map_init(result, *min, *max, vm_map_pmap(parent));
282 	if ((ret = vm_map_submap(parent, *min, *max, result)) != KERN_SUCCESS)
283 		panic("kmem_suballoc: unable to change range to submap");
284 }
285 
286 /*
287  *	kmem_alloc_wait:
288  *
289  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
290  *	has no room, the caller sleeps waiting for more memory in the submap.
291  *
292  *	This routine may block.
293  */
294 
295 vm_offset_t
296 kmem_alloc_wait(vm_map_t map, vm_size_t size)
297 {
298 	vm_offset_t addr;
299 	int count;
300 
301 	size = round_page(size);
302 
303 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
304 
305 	for (;;) {
306 		/*
307 		 * To make this work for more than one map, use the map's lock
308 		 * to lock out sleepers/wakers.
309 		 */
310 		vm_map_lock(map);
311 		if (vm_map_findspace(map, vm_map_min(map), size, 1, &addr) == 0)
312 			break;
313 		/* no space now; see if we can ever get space */
314 		if (vm_map_max(map) - vm_map_min(map) < size) {
315 			vm_map_entry_release(count);
316 			vm_map_unlock(map);
317 			return (0);
318 		}
319 		vm_map_unlock(map);
320 		tsleep(map, 0, "kmaw", 0);
321 	}
322 	vm_map_insert(map, &count,
323 		      NULL, (vm_offset_t) 0,
324 		      addr, addr + size,
325 		      VM_MAPTYPE_NORMAL,
326 		      VM_PROT_ALL, VM_PROT_ALL,
327 		      0);
328 	vm_map_unlock(map);
329 	vm_map_entry_release(count);
330 	return (addr);
331 }
332 
333 /*
334  *	kmem_free_wakeup:
335  *
336  *	Returns memory to a submap of the kernel, and wakes up any processes
337  *	waiting for memory in that map.
338  */
339 void
340 kmem_free_wakeup(vm_map_t map, vm_offset_t addr, vm_size_t size)
341 {
342 	int count;
343 
344 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
345 	vm_map_lock(map);
346 	vm_map_delete(map, trunc_page(addr), round_page(addr + size), &count);
347 	wakeup(map);
348 	vm_map_unlock(map);
349 	vm_map_entry_release(count);
350 }
351 
352 /*
353  * 	kmem_init:
354  *
355  *	Create the kernel_map and insert mappings to cover areas already
356  *	allocated or reserved thus far.  That is, the area (KvaStart,start)
357  *	and (end,KvaEnd) must be marked as allocated.
358  *
359  *	We could use a min_offset of 0 instead of KvaStart, but since the
360  *	min_offset is not used for any calculations other then a bounds check
361  *	it does not effect readability.  KvaStart is more appropriate.
362  *
363  *	Depend on the zalloc bootstrap cache to get our vm_map_entry_t.
364  */
365 void
366 kmem_init(vm_offset_t start, vm_offset_t end)
367 {
368 	vm_map_t m;
369 	int count;
370 
371 	m = vm_map_create(&kernel_map, &kernel_pmap, KvaStart, KvaEnd);
372 	vm_map_lock(m);
373 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
374 	m->system_map = 1;
375 	count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
376 	if (KvaStart != start) {
377 		vm_map_insert(m, &count, NULL, (vm_offset_t) 0,
378 			      KvaStart, start,
379 			      VM_MAPTYPE_NORMAL,
380 			      VM_PROT_ALL, VM_PROT_ALL,
381 			      0);
382 	}
383 	if (KvaEnd != end) {
384 		vm_map_insert(m, &count, NULL, (vm_offset_t) 0,
385 			      end, KvaEnd,
386 			      VM_MAPTYPE_NORMAL,
387 			      VM_PROT_ALL, VM_PROT_ALL,
388 			      0);
389 	}
390 	/* ... and ending with the completion of the above `insert' */
391 	vm_map_unlock(m);
392 	vm_map_entry_release(count);
393 }
394 
395 static int
396 kvm_size(SYSCTL_HANDLER_ARGS)
397 {
398 	unsigned long ksize = KvaSize;
399 
400 	return sysctl_handle_long(oidp, &ksize, 0, req);
401 }
402 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
403     0, 0, kvm_size, "IU", "Size of KVM");
404 
405 static int
406 kvm_free(SYSCTL_HANDLER_ARGS)
407 {
408 	unsigned long kfree = virtual_end - kernel_vm_end;
409 
410 	return sysctl_handle_long(oidp, &kfree, 0, req);
411 }
412 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
413     0, 0, kvm_free, "IU", "Amount of KVM free");
414 
415