xref: /original-bsd/sys/vm/vm_kern.c (revision 333da485)
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  * %sccs.include.redist.c%
9  *
10  *	@(#)vm_kern.c	8.3 (Berkeley) 01/12/94
11  *
12  *
13  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
14  * All rights reserved.
15  *
16  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
17  *
18  * Permission to use, copy, modify and distribute this software and
19  * its documentation is hereby granted, provided that both the copyright
20  * notice and this permission notice appear in all copies of the
21  * software, derivative works or modified versions, and any portions
22  * thereof, and that both notices appear in supporting documentation.
23  *
24  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
25  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
26  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
27  *
28  * Carnegie Mellon requests users of this software to return to
29  *
30  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
31  *  School of Computer Science
32  *  Carnegie Mellon University
33  *  Pittsburgh PA 15213-3890
34  *
35  * any improvements or extensions that they make and grant Carnegie the
36  * rights to redistribute these changes.
37  */
38 
39 /*
40  *	Kernel memory management.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 
46 #include <vm/vm.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_pageout.h>
49 #include <vm/vm_kern.h>
50 
51 /*
52  *	kmem_alloc_pageable:
53  *
54  *	Allocate pageable memory to the kernel's address map.
55  *	map must be "kernel_map" below.
56  */
57 
58 vm_offset_t kmem_alloc_pageable(map, size)
59 	vm_map_t		map;
60 	register vm_size_t	size;
61 {
62 	vm_offset_t		addr;
63 	register int		result;
64 
65 #if	0
66 	if (map != kernel_map)
67 		panic("kmem_alloc_pageable: not called with kernel_map");
68 #endif
69 
70 	size = round_page(size);
71 
72 	addr = vm_map_min(map);
73 	result = vm_map_find(map, NULL, (vm_offset_t) 0,
74 				&addr, size, TRUE);
75 	if (result != KERN_SUCCESS) {
76 		return(0);
77 	}
78 
79 	return(addr);
80 }
81 
82 /*
83  *	Allocate wired-down memory in the kernel's address map
84  *	or a submap.
85  */
86 vm_offset_t kmem_alloc(map, size)
87 	register vm_map_t	map;
88 	register vm_size_t	size;
89 {
90 	vm_offset_t		addr;
91 	register vm_offset_t	offset;
92 	extern vm_object_t	kernel_object;
93 	vm_offset_t		i;
94 
95 	size = round_page(size);
96 
97 	/*
98 	 *	Use the kernel object for wired-down kernel pages.
99 	 *	Assume that no region of the kernel object is
100 	 *	referenced more than once.
101 	 */
102 
103 	/*
104 	 * Locate sufficient space in the map.  This will give us the
105 	 * final virtual address for the new memory, and thus will tell
106 	 * us the offset within the kernel map.
107 	 */
108 	vm_map_lock(map);
109 	if (vm_map_findspace(map, 0, size, &addr)) {
110 		vm_map_unlock(map);
111 		return (0);
112 	}
113 	offset = addr - VM_MIN_KERNEL_ADDRESS;
114 	vm_object_reference(kernel_object);
115 	vm_map_insert(map, kernel_object, offset, addr, addr + size);
116 	vm_map_unlock(map);
117 
118 	/*
119 	 *	Guarantee that there are pages already in this object
120 	 *	before calling vm_map_pageable.  This is to prevent the
121 	 *	following scenario:
122 	 *
123 	 *		1) Threads have swapped out, so that there is a
124 	 *		   pager for the kernel_object.
125 	 *		2) The kmsg zone is empty, and so we are kmem_allocing
126 	 *		   a new page for it.
127 	 *		3) vm_map_pageable calls vm_fault; there is no page,
128 	 *		   but there is a pager, so we call
129 	 *		   pager_data_request.  But the kmsg zone is empty,
130 	 *		   so we must kmem_alloc.
131 	 *		4) goto 1
132 	 *		5) Even if the kmsg zone is not empty: when we get
133 	 *		   the data back from the pager, it will be (very
134 	 *		   stale) non-zero data.  kmem_alloc is defined to
135 	 *		   return zero-filled memory.
136 	 *
137 	 *	We're intentionally not activating the pages we allocate
138 	 *	to prevent a race with page-out.  vm_map_pageable will wire
139 	 *	the pages.
140 	 */
141 
142 	vm_object_lock(kernel_object);
143 	for (i = 0 ; i < size; i+= PAGE_SIZE) {
144 		vm_page_t	mem;
145 
146 		while ((mem = vm_page_alloc(kernel_object, offset+i)) == NULL) {
147 			vm_object_unlock(kernel_object);
148 			VM_WAIT;
149 			vm_object_lock(kernel_object);
150 		}
151 		vm_page_zero_fill(mem);
152 		mem->flags &= ~PG_BUSY;
153 	}
154 	vm_object_unlock(kernel_object);
155 
156 	/*
157 	 *	And finally, mark the data as non-pageable.
158 	 */
159 
160 	(void) vm_map_pageable(map, (vm_offset_t) addr, addr + size, FALSE);
161 
162 	/*
163 	 *	Try to coalesce the map
164 	 */
165 
166 	vm_map_simplify(map, addr);
167 
168 	return(addr);
169 }
170 
171 /*
172  *	kmem_free:
173  *
174  *	Release a region of kernel virtual memory allocated
175  *	with kmem_alloc, and return the physical pages
176  *	associated with that region.
177  */
178 void kmem_free(map, addr, size)
179 	vm_map_t		map;
180 	register vm_offset_t	addr;
181 	vm_size_t		size;
182 {
183 	(void) vm_map_remove(map, trunc_page(addr), round_page(addr + size));
184 }
185 
186 /*
187  *	kmem_suballoc:
188  *
189  *	Allocates a map to manage a subrange
190  *	of the kernel virtual address space.
191  *
192  *	Arguments are as follows:
193  *
194  *	parent		Map to take range from
195  *	size		Size of range to find
196  *	min, max	Returned endpoints of map
197  *	pageable	Can the region be paged
198  */
199 vm_map_t kmem_suballoc(parent, min, max, size, pageable)
200 	register vm_map_t	parent;
201 	vm_offset_t		*min, *max;
202 	register vm_size_t	size;
203 	boolean_t		pageable;
204 {
205 	register int	ret;
206 	vm_map_t	result;
207 
208 	size = round_page(size);
209 
210 	*min = (vm_offset_t) vm_map_min(parent);
211 	ret = vm_map_find(parent, NULL, (vm_offset_t) 0,
212 				min, size, TRUE);
213 	if (ret != KERN_SUCCESS) {
214 		printf("kmem_suballoc: bad status return of %d.\n", ret);
215 		panic("kmem_suballoc");
216 	}
217 	*max = *min + size;
218 	pmap_reference(vm_map_pmap(parent));
219 	result = vm_map_create(vm_map_pmap(parent), *min, *max, pageable);
220 	if (result == NULL)
221 		panic("kmem_suballoc: cannot create submap");
222 	if ((ret = vm_map_submap(parent, *min, *max, result)) != KERN_SUCCESS)
223 		panic("kmem_suballoc: unable to change range to submap");
224 	return(result);
225 }
226 
227 /*
228  * Allocate wired-down memory in the kernel's address map for the higher
229  * level kernel memory allocator (kern/kern_malloc.c).  We cannot use
230  * kmem_alloc() because we may need to allocate memory at interrupt
231  * level where we cannot block (canwait == FALSE).
232  *
233  * This routine has its own private kernel submap (kmem_map) and object
234  * (kmem_object).  This, combined with the fact that only malloc uses
235  * this routine, ensures that we will never block in map or object waits.
236  *
237  * Note that this still only works in a uni-processor environment and
238  * when called at splhigh().
239  *
240  * We don't worry about expanding the map (adding entries) since entries
241  * for wired maps are statically allocated.
242  */
243 vm_offset_t
244 kmem_malloc(map, size, canwait)
245 	register vm_map_t	map;
246 	register vm_size_t	size;
247 	boolean_t		canwait;
248 {
249 	register vm_offset_t	offset, i;
250 	vm_map_entry_t		entry;
251 	vm_offset_t		addr;
252 	vm_page_t		m;
253 	extern vm_object_t	kmem_object;
254 
255 	if (map != kmem_map && map != mb_map)
256 		panic("kern_malloc_alloc: map != {kmem,mb}_map");
257 
258 	size = round_page(size);
259 	addr = vm_map_min(map);
260 
261 	/*
262 	 * Locate sufficient space in the map.  This will give us the
263 	 * final virtual address for the new memory, and thus will tell
264 	 * us the offset within the kernel map.
265 	 */
266 	vm_map_lock(map);
267 	if (vm_map_findspace(map, 0, size, &addr)) {
268 		vm_map_unlock(map);
269 		if (canwait)		/* XXX  should wait */
270 			panic("kmem_malloc: %s too small",
271 			    map == kmem_map ? "kmem_map" : "mb_map");
272 		return (0);
273 	}
274 	offset = addr - vm_map_min(kmem_map);
275 	vm_object_reference(kmem_object);
276 	vm_map_insert(map, kmem_object, offset, addr, addr + size);
277 
278 	/*
279 	 * If we can wait, just mark the range as wired
280 	 * (will fault pages as necessary).
281 	 */
282 	if (canwait) {
283 		vm_map_unlock(map);
284 		(void) vm_map_pageable(map, (vm_offset_t) addr, addr + size,
285 				       FALSE);
286 		vm_map_simplify(map, addr);
287 		return(addr);
288 	}
289 
290 	/*
291 	 * If we cannot wait then we must allocate all memory up front,
292 	 * pulling it off the active queue to prevent pageout.
293 	 */
294 	vm_object_lock(kmem_object);
295 	for (i = 0; i < size; i += PAGE_SIZE) {
296 		m = vm_page_alloc(kmem_object, offset + i);
297 
298 		/*
299 		 * Ran out of space, free everything up and return.
300 		 * Don't need to lock page queues here as we know
301 		 * that the pages we got aren't on any queues.
302 		 */
303 		if (m == NULL) {
304 			while (i != 0) {
305 				i -= PAGE_SIZE;
306 				m = vm_page_lookup(kmem_object, offset + i);
307 				vm_page_free(m);
308 			}
309 			vm_object_unlock(kmem_object);
310 			vm_map_delete(map, addr, addr + size);
311 			vm_map_unlock(map);
312 			return(0);
313 		}
314 #if 0
315 		vm_page_zero_fill(m);
316 #endif
317 		m->flags &= ~PG_BUSY;
318 	}
319 	vm_object_unlock(kmem_object);
320 
321 	/*
322 	 * Mark map entry as non-pageable.
323 	 * Assert: vm_map_insert() will never be able to extend the previous
324 	 * entry so there will be a new entry exactly corresponding to this
325 	 * address range and it will have wired_count == 0.
326 	 */
327 	if (!vm_map_lookup_entry(map, addr, &entry) ||
328 	    entry->start != addr || entry->end != addr + size ||
329 	    entry->wired_count)
330 		panic("kmem_malloc: entry not found or misaligned");
331 	entry->wired_count++;
332 
333 	/*
334 	 * Loop thru pages, entering them in the pmap.
335 	 * (We cannot add them to the wired count without
336 	 * wrapping the vm_page_queue_lock in splimp...)
337 	 */
338 	for (i = 0; i < size; i += PAGE_SIZE) {
339 		vm_object_lock(kmem_object);
340 		m = vm_page_lookup(kmem_object, offset + i);
341 		vm_object_unlock(kmem_object);
342 		pmap_enter(map->pmap, addr + i, VM_PAGE_TO_PHYS(m),
343 			   VM_PROT_DEFAULT, TRUE);
344 	}
345 	vm_map_unlock(map);
346 
347 	vm_map_simplify(map, addr);
348 	return(addr);
349 }
350 
351 /*
352  *	kmem_alloc_wait
353  *
354  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
355  *	has no room, the caller sleeps waiting for more memory in the submap.
356  *
357  */
358 vm_offset_t kmem_alloc_wait(map, size)
359 	vm_map_t	map;
360 	vm_size_t	size;
361 {
362 	vm_offset_t	addr;
363 
364 	size = round_page(size);
365 
366 	for (;;) {
367 		/*
368 		 * To make this work for more than one map,
369 		 * use the map's lock to lock out sleepers/wakers.
370 		 */
371 		vm_map_lock(map);
372 		if (vm_map_findspace(map, 0, size, &addr) == 0)
373 			break;
374 		/* no space now; see if we can ever get space */
375 		if (vm_map_max(map) - vm_map_min(map) < size) {
376 			vm_map_unlock(map);
377 			return (0);
378 		}
379 		assert_wait((int)map, TRUE);
380 		vm_map_unlock(map);
381 		thread_block();
382 	}
383 	vm_map_insert(map, NULL, (vm_offset_t)0, addr, addr + size);
384 	vm_map_unlock(map);
385 	return (addr);
386 }
387 
388 /*
389  *	kmem_free_wakeup
390  *
391  *	Returns memory to a submap of the kernel, and wakes up any threads
392  *	waiting for memory in that map.
393  */
394 void	kmem_free_wakeup(map, addr, size)
395 	vm_map_t	map;
396 	vm_offset_t	addr;
397 	vm_size_t	size;
398 {
399 	vm_map_lock(map);
400 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
401 	thread_wakeup((int)map);
402 	vm_map_unlock(map);
403 }
404 
405 /*
406  * Create the kernel map; insert a mapping covering kernel text, data, bss,
407  * and all space allocated thus far (`boostrap' data).  The new map will thus
408  * map the range between VM_MIN_KERNEL_ADDRESS and `start' as allocated, and
409  * the range between `start' and `end' as free.
410  */
411 void kmem_init(start, end)
412 	vm_offset_t start, end;
413 {
414 	register vm_map_t m;
415 
416 	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end, FALSE);
417 	vm_map_lock(m);
418 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
419 	kernel_map = m;
420 	(void) vm_map_insert(m, NULL, (vm_offset_t)0,
421 	    VM_MIN_KERNEL_ADDRESS, start);
422 	/* ... and ending with the completion of the above `insert' */
423 	vm_map_unlock(m);
424 }
425