xref: /freebsd/sys/vm/vm_kern.c (revision a839bdc8af1604869ecb09a48a320a146579861c)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
17df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
18df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
19df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
363c4dd356SDavid Greenman  *	from: @(#)vm_kern.c	8.3 (Berkeley) 1/12/94
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *
39df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40df8bae1dSRodney W. Grimes  * All rights reserved.
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
45df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
46df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
47df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
48df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57df8bae1dSRodney W. Grimes  *  School of Computer Science
58df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
59df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
62df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
633c4dd356SDavid Greenman  *
64a839bdc8SDmitrij Tejblum  * $Id: vm_kern.c,v 1.54 1999/03/16 07:39:07 alc Exp $
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes /*
68df8bae1dSRodney W. Grimes  *	Kernel memory management.
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #include <sys/param.h>
72df8bae1dSRodney W. Grimes #include <sys/systm.h>
73f23b4c91SGarrett Wollman #include <sys/proc.h>
74a1f6d91cSDavid Greenman #include <sys/malloc.h>
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes #include <vm/vm.h>
77efeaf95aSDavid Greenman #include <vm/vm_param.h>
78efeaf95aSDavid Greenman #include <vm/vm_prot.h>
79996c772fSJohn Dyson #include <sys/lock.h>
80efeaf95aSDavid Greenman #include <vm/pmap.h>
81efeaf95aSDavid Greenman #include <vm/vm_map.h>
82efeaf95aSDavid Greenman #include <vm/vm_object.h>
83df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
84df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
859b4288a3SBruce Evans #include <vm/vm_extern.h>
86df8bae1dSRodney W. Grimes 
875b0a7408SJohn Dyson vm_map_t kernel_map=0;
885b0a7408SJohn Dyson vm_map_t kmem_map=0;
895b0a7408SJohn Dyson vm_map_t exec_map=0;
905b0a7408SJohn Dyson vm_map_t clean_map=0;
915b0a7408SJohn Dyson vm_map_t u_map=0;
925b0a7408SJohn Dyson vm_map_t buffer_map=0;
935b0a7408SJohn Dyson vm_map_t mb_map=0;
945b0a7408SJohn Dyson int mb_map_full=0;
955b0a7408SJohn Dyson vm_map_t io_map=0;
965b0a7408SJohn Dyson vm_map_t phys_map=0;
97f23b4c91SGarrett Wollman 
98df8bae1dSRodney W. Grimes /*
99df8bae1dSRodney W. Grimes  *	kmem_alloc_pageable:
100df8bae1dSRodney W. Grimes  *
101df8bae1dSRodney W. Grimes  *	Allocate pageable memory to the kernel's address map.
102f81b8592SDavid Greenman  *	"map" must be kernel_map or a submap of kernel_map.
103df8bae1dSRodney W. Grimes  */
104df8bae1dSRodney W. Grimes 
1050d94caffSDavid Greenman vm_offset_t
1060d94caffSDavid Greenman kmem_alloc_pageable(map, size)
107df8bae1dSRodney W. Grimes 	vm_map_t map;
108df8bae1dSRodney W. Grimes 	register vm_size_t size;
109df8bae1dSRodney W. Grimes {
110df8bae1dSRodney W. Grimes 	vm_offset_t addr;
111df8bae1dSRodney W. Grimes 	register int result;
112df8bae1dSRodney W. Grimes 
113df8bae1dSRodney W. Grimes 	size = round_page(size);
114df8bae1dSRodney W. Grimes 	addr = vm_map_min(map);
115df8bae1dSRodney W. Grimes 	result = vm_map_find(map, NULL, (vm_offset_t) 0,
116bd7e5f99SJohn Dyson 	    &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
117df8bae1dSRodney W. Grimes 	if (result != KERN_SUCCESS) {
118df8bae1dSRodney W. Grimes 		return (0);
119df8bae1dSRodney W. Grimes 	}
120df8bae1dSRodney W. Grimes 	return (addr);
121df8bae1dSRodney W. Grimes }
122df8bae1dSRodney W. Grimes 
123df8bae1dSRodney W. Grimes /*
124a839bdc8SDmitrij Tejblum  *	kmem_alloc_nofault:
125a839bdc8SDmitrij Tejblum  *
126a839bdc8SDmitrij Tejblum  *	Same as kmem_alloc_pageable, except that it create a nofault entry.
127a839bdc8SDmitrij Tejblum  */
128a839bdc8SDmitrij Tejblum 
129a839bdc8SDmitrij Tejblum vm_offset_t
130a839bdc8SDmitrij Tejblum kmem_alloc_nofault(map, size)
131a839bdc8SDmitrij Tejblum 	vm_map_t map;
132a839bdc8SDmitrij Tejblum 	register vm_size_t size;
133a839bdc8SDmitrij Tejblum {
134a839bdc8SDmitrij Tejblum 	vm_offset_t addr;
135a839bdc8SDmitrij Tejblum 	register int result;
136a839bdc8SDmitrij Tejblum 
137a839bdc8SDmitrij Tejblum 	size = round_page(size);
138a839bdc8SDmitrij Tejblum 	addr = vm_map_min(map);
139a839bdc8SDmitrij Tejblum 	result = vm_map_find(map, NULL, (vm_offset_t) 0,
140a839bdc8SDmitrij Tejblum 	    &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
141a839bdc8SDmitrij Tejblum 	if (result != KERN_SUCCESS) {
142a839bdc8SDmitrij Tejblum 		return (0);
143a839bdc8SDmitrij Tejblum 	}
144a839bdc8SDmitrij Tejblum 	return (addr);
145a839bdc8SDmitrij Tejblum }
146a839bdc8SDmitrij Tejblum 
147a839bdc8SDmitrij Tejblum /*
148df8bae1dSRodney W. Grimes  *	Allocate wired-down memory in the kernel's address map
149df8bae1dSRodney W. Grimes  *	or a submap.
150df8bae1dSRodney W. Grimes  */
1510d94caffSDavid Greenman vm_offset_t
1520d94caffSDavid Greenman kmem_alloc(map, size)
153df8bae1dSRodney W. Grimes 	register vm_map_t map;
154df8bae1dSRodney W. Grimes 	register vm_size_t size;
155df8bae1dSRodney W. Grimes {
156df8bae1dSRodney W. Grimes 	vm_offset_t addr;
157df8bae1dSRodney W. Grimes 	register vm_offset_t offset;
158df8bae1dSRodney W. Grimes 	vm_offset_t i;
159df8bae1dSRodney W. Grimes 
160df8bae1dSRodney W. Grimes 	size = round_page(size);
161df8bae1dSRodney W. Grimes 
162df8bae1dSRodney W. Grimes 	/*
1630d94caffSDavid Greenman 	 * Use the kernel object for wired-down kernel pages. Assume that no
1640d94caffSDavid Greenman 	 * region of the kernel object is referenced more than once.
165df8bae1dSRodney W. Grimes 	 */
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes 	/*
1680d94caffSDavid Greenman 	 * Locate sufficient space in the map.  This will give us the final
1690d94caffSDavid Greenman 	 * virtual address for the new memory, and thus will tell us the
1700d94caffSDavid Greenman 	 * offset within the kernel map.
171df8bae1dSRodney W. Grimes 	 */
172df8bae1dSRodney W. Grimes 	vm_map_lock(map);
173e47ed70bSJohn Dyson 	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
174df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
175df8bae1dSRodney W. Grimes 		return (0);
176df8bae1dSRodney W. Grimes 	}
177df8bae1dSRodney W. Grimes 	offset = addr - VM_MIN_KERNEL_ADDRESS;
178df8bae1dSRodney W. Grimes 	vm_object_reference(kernel_object);
179bd7e5f99SJohn Dyson 	vm_map_insert(map, kernel_object, offset, addr, addr + size,
180bd7e5f99SJohn Dyson 		VM_PROT_ALL, VM_PROT_ALL, 0);
181df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes 	/*
1840d94caffSDavid Greenman 	 * Guarantee that there are pages already in this object before
1850d94caffSDavid Greenman 	 * calling vm_map_pageable.  This is to prevent the following
1860d94caffSDavid Greenman 	 * scenario:
187df8bae1dSRodney W. Grimes 	 *
1880d94caffSDavid Greenman 	 * 1) Threads have swapped out, so that there is a pager for the
1890d94caffSDavid Greenman 	 * kernel_object. 2) The kmsg zone is empty, and so we are
1900d94caffSDavid Greenman 	 * kmem_allocing a new page for it. 3) vm_map_pageable calls vm_fault;
1910d94caffSDavid Greenman 	 * there is no page, but there is a pager, so we call
1920d94caffSDavid Greenman 	 * pager_data_request.  But the kmsg zone is empty, so we must
1930d94caffSDavid Greenman 	 * kmem_alloc. 4) goto 1 5) Even if the kmsg zone is not empty: when
1940d94caffSDavid Greenman 	 * we get the data back from the pager, it will be (very stale)
1950d94caffSDavid Greenman 	 * non-zero data.  kmem_alloc is defined to return zero-filled memory.
196df8bae1dSRodney W. Grimes 	 *
1970d94caffSDavid Greenman 	 * We're intentionally not activating the pages we allocate to prevent a
1980d94caffSDavid Greenman 	 * race with page-out.  vm_map_pageable will wire the pages.
199df8bae1dSRodney W. Grimes 	 */
200df8bae1dSRodney W. Grimes 
201df8bae1dSRodney W. Grimes 	for (i = 0; i < size; i += PAGE_SIZE) {
202df8bae1dSRodney W. Grimes 		vm_page_t mem;
203df8bae1dSRodney W. Grimes 
20495461b45SJohn Dyson 		mem = vm_page_grab(kernel_object, OFF_TO_IDX(offset + i),
20595461b45SJohn Dyson 				VM_ALLOC_ZERO | VM_ALLOC_RETRY);
206f70f05f2SJohn Dyson 		if ((mem->flags & PG_ZERO) == 0)
207df8bae1dSRodney W. Grimes 			vm_page_zero_fill(mem);
2087fb0c17eSDavid Greenman 		mem->valid = VM_PAGE_BITS_ALL;
2091c7c3c6aSMatthew Dillon 		vm_page_flag_clear(mem, PG_ZERO);
2101c7c3c6aSMatthew Dillon 		vm_page_wakeup(mem);
211df8bae1dSRodney W. Grimes 	}
212df8bae1dSRodney W. Grimes 
213df8bae1dSRodney W. Grimes 	/*
214df8bae1dSRodney W. Grimes 	 * And finally, mark the data as non-pageable.
215df8bae1dSRodney W. Grimes 	 */
216df8bae1dSRodney W. Grimes 
217df8bae1dSRodney W. Grimes 	(void) vm_map_pageable(map, (vm_offset_t) addr, addr + size, FALSE);
218df8bae1dSRodney W. Grimes 
219df8bae1dSRodney W. Grimes 	return (addr);
220df8bae1dSRodney W. Grimes }
221df8bae1dSRodney W. Grimes 
222df8bae1dSRodney W. Grimes /*
223df8bae1dSRodney W. Grimes  *	kmem_free:
224df8bae1dSRodney W. Grimes  *
225df8bae1dSRodney W. Grimes  *	Release a region of kernel virtual memory allocated
226df8bae1dSRodney W. Grimes  *	with kmem_alloc, and return the physical pages
227df8bae1dSRodney W. Grimes  *	associated with that region.
2281c7c3c6aSMatthew Dillon  *
2291c7c3c6aSMatthew Dillon  *	This routine may not block on kernel maps.
230df8bae1dSRodney W. Grimes  */
2310d94caffSDavid Greenman void
2320d94caffSDavid Greenman kmem_free(map, addr, size)
233df8bae1dSRodney W. Grimes 	vm_map_t map;
234df8bae1dSRodney W. Grimes 	register vm_offset_t addr;
235df8bae1dSRodney W. Grimes 	vm_size_t size;
236df8bae1dSRodney W. Grimes {
237df8bae1dSRodney W. Grimes 	(void) vm_map_remove(map, trunc_page(addr), round_page(addr + size));
238df8bae1dSRodney W. Grimes }
239df8bae1dSRodney W. Grimes 
240df8bae1dSRodney W. Grimes /*
241df8bae1dSRodney W. Grimes  *	kmem_suballoc:
242df8bae1dSRodney W. Grimes  *
243df8bae1dSRodney W. Grimes  *	Allocates a map to manage a subrange
244df8bae1dSRodney W. Grimes  *	of the kernel virtual address space.
245df8bae1dSRodney W. Grimes  *
246df8bae1dSRodney W. Grimes  *	Arguments are as follows:
247df8bae1dSRodney W. Grimes  *
248df8bae1dSRodney W. Grimes  *	parent		Map to take range from
249df8bae1dSRodney W. Grimes  *	size		Size of range to find
250df8bae1dSRodney W. Grimes  *	min, max	Returned endpoints of map
251df8bae1dSRodney W. Grimes  *	pageable	Can the region be paged
252df8bae1dSRodney W. Grimes  */
2530d94caffSDavid Greenman vm_map_t
2542d8acc0fSJohn Dyson kmem_suballoc(parent, min, max, size)
255df8bae1dSRodney W. Grimes 	register vm_map_t parent;
256df8bae1dSRodney W. Grimes 	vm_offset_t *min, *max;
257df8bae1dSRodney W. Grimes 	register vm_size_t size;
258df8bae1dSRodney W. Grimes {
259df8bae1dSRodney W. Grimes 	register int ret;
260df8bae1dSRodney W. Grimes 	vm_map_t result;
261df8bae1dSRodney W. Grimes 
262df8bae1dSRodney W. Grimes 	size = round_page(size);
263df8bae1dSRodney W. Grimes 
264df8bae1dSRodney W. Grimes 	*min = (vm_offset_t) vm_map_min(parent);
265df8bae1dSRodney W. Grimes 	ret = vm_map_find(parent, NULL, (vm_offset_t) 0,
266bd7e5f99SJohn Dyson 	    min, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
267df8bae1dSRodney W. Grimes 	if (ret != KERN_SUCCESS) {
268df8bae1dSRodney W. Grimes 		printf("kmem_suballoc: bad status return of %d.\n", ret);
269df8bae1dSRodney W. Grimes 		panic("kmem_suballoc");
270df8bae1dSRodney W. Grimes 	}
271df8bae1dSRodney W. Grimes 	*max = *min + size;
272df8bae1dSRodney W. Grimes 	pmap_reference(vm_map_pmap(parent));
2732d8acc0fSJohn Dyson 	result = vm_map_create(vm_map_pmap(parent), *min, *max);
274df8bae1dSRodney W. Grimes 	if (result == NULL)
275df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: cannot create submap");
276df8bae1dSRodney W. Grimes 	if ((ret = vm_map_submap(parent, *min, *max, result)) != KERN_SUCCESS)
277df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: unable to change range to submap");
278df8bae1dSRodney W. Grimes 	return (result);
279df8bae1dSRodney W. Grimes }
280df8bae1dSRodney W. Grimes 
281df8bae1dSRodney W. Grimes /*
2821c7c3c6aSMatthew Dillon  *	kmem_malloc:
2831c7c3c6aSMatthew Dillon  *
284df8bae1dSRodney W. Grimes  * 	Allocate wired-down memory in the kernel's address map for the higher
285df8bae1dSRodney W. Grimes  * 	level kernel memory allocator (kern/kern_malloc.c).  We cannot use
286df8bae1dSRodney W. Grimes  * 	kmem_alloc() because we may need to allocate memory at interrupt
287df8bae1dSRodney W. Grimes  * 	level where we cannot block (canwait == FALSE).
288df8bae1dSRodney W. Grimes  *
289df8bae1dSRodney W. Grimes  * 	This routine has its own private kernel submap (kmem_map) and object
290df8bae1dSRodney W. Grimes  * 	(kmem_object).  This, combined with the fact that only malloc uses
291df8bae1dSRodney W. Grimes  * 	this routine, ensures that we will never block in map or object waits.
292df8bae1dSRodney W. Grimes  *
293df8bae1dSRodney W. Grimes  * 	Note that this still only works in a uni-processor environment and
294df8bae1dSRodney W. Grimes  * 	when called at splhigh().
295df8bae1dSRodney W. Grimes  *
296df8bae1dSRodney W. Grimes  * 	We don't worry about expanding the map (adding entries) since entries
297df8bae1dSRodney W. Grimes  * 	for wired maps are statically allocated.
2981c7c3c6aSMatthew Dillon  *
2991c7c3c6aSMatthew Dillon  *	NOTE:  This routine is not supposed to block if M_NOWAIT is set, but
3001c7c3c6aSMatthew Dillon  *	I have not verified that it actually does not block.
301df8bae1dSRodney W. Grimes  */
302df8bae1dSRodney W. Grimes vm_offset_t
3031c7c3c6aSMatthew Dillon kmem_malloc(map, size, flags)
304df8bae1dSRodney W. Grimes 	register vm_map_t map;
305df8bae1dSRodney W. Grimes 	register vm_size_t size;
3061c7c3c6aSMatthew Dillon 	int flags;
307df8bae1dSRodney W. Grimes {
308df8bae1dSRodney W. Grimes 	register vm_offset_t offset, i;
309df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
310df8bae1dSRodney W. Grimes 	vm_offset_t addr;
311df8bae1dSRodney W. Grimes 	vm_page_t m;
312df8bae1dSRodney W. Grimes 
313649c409dSDavid Greenman 	if (map != kmem_map && map != mb_map)
314649c409dSDavid Greenman 		panic("kmem_malloc: map != {kmem,mb}_map");
315df8bae1dSRodney W. Grimes 
316df8bae1dSRodney W. Grimes 	size = round_page(size);
317df8bae1dSRodney W. Grimes 	addr = vm_map_min(map);
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes 	/*
3200d94caffSDavid Greenman 	 * Locate sufficient space in the map.  This will give us the final
3210d94caffSDavid Greenman 	 * virtual address for the new memory, and thus will tell us the
3220d94caffSDavid Greenman 	 * offset within the kernel map.
323df8bae1dSRodney W. Grimes 	 */
324df8bae1dSRodney W. Grimes 	vm_map_lock(map);
325e47ed70bSJohn Dyson 	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
326df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
3275eb7d0cdSDavid Greenman 		if (map == mb_map) {
3285eb7d0cdSDavid Greenman 			mb_map_full = TRUE;
3295994d893SDavid Greenman 			printf("Out of mbuf clusters - adjust NMBCLUSTERS or increase maxusers!\n");
3305eb7d0cdSDavid Greenman 			return (0);
3315eb7d0cdSDavid Greenman 		}
3321c7c3c6aSMatthew Dillon 		if ((flags & M_NOWAIT) == 0)
333e47ed70bSJohn Dyson 			panic("kmem_malloc(%d): kmem_map too small: %d total allocated",
334e47ed70bSJohn Dyson 				size, map->size);
335df8bae1dSRodney W. Grimes 		return (0);
336df8bae1dSRodney W. Grimes 	}
3370891ef4cSJohn Dyson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
338df8bae1dSRodney W. Grimes 	vm_object_reference(kmem_object);
339bd7e5f99SJohn Dyson 	vm_map_insert(map, kmem_object, offset, addr, addr + size,
340bd7e5f99SJohn Dyson 		VM_PROT_ALL, VM_PROT_ALL, 0);
341df8bae1dSRodney W. Grimes 
342df8bae1dSRodney W. Grimes 	for (i = 0; i < size; i += PAGE_SIZE) {
3431c7c3c6aSMatthew Dillon 		/*
3441c7c3c6aSMatthew Dillon 		 * Note: if M_NOWAIT specified alone, allocate from
3451c7c3c6aSMatthew Dillon 		 * interrupt-safe queues only (just the free list).  If
3461c7c3c6aSMatthew Dillon 		 * M_ASLEEP or M_USE_RESERVE is also specified, we can also
3471c7c3c6aSMatthew Dillon 		 * allocate from the cache.  Neither of the latter two
3481c7c3c6aSMatthew Dillon 		 * flags may be specified from an interrupt since interrupts
3491c7c3c6aSMatthew Dillon 		 * are not allowed to mess with the cache queue.
3501c7c3c6aSMatthew Dillon 		 */
351b18bfc3dSJohn Dyson retry:
352a316d390SJohn Dyson 		m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i),
3531c7c3c6aSMatthew Dillon 		    ((flags & (M_NOWAIT|M_ASLEEP|M_USE_RESERVE)) == M_NOWAIT) ?
3541c7c3c6aSMatthew Dillon 			VM_ALLOC_INTERRUPT :
3551c7c3c6aSMatthew Dillon 			VM_ALLOC_SYSTEM);
356df8bae1dSRodney W. Grimes 
357df8bae1dSRodney W. Grimes 		/*
3580d94caffSDavid Greenman 		 * Ran out of space, free everything up and return. Don't need
3590d94caffSDavid Greenman 		 * to lock page queues here as we know that the pages we got
3600d94caffSDavid Greenman 		 * aren't on any queues.
361df8bae1dSRodney W. Grimes 		 */
362df8bae1dSRodney W. Grimes 		if (m == NULL) {
3631c7c3c6aSMatthew Dillon 			if ((flags & M_NOWAIT) == 0) {
364c7003c69SAlan Cox 				vm_map_unlock(map);
365b18bfc3dSJohn Dyson 				VM_WAIT;
366c7003c69SAlan Cox 				vm_map_lock(map);
367b18bfc3dSJohn Dyson 				goto retry;
368b18bfc3dSJohn Dyson 			}
369df8bae1dSRodney W. Grimes 			vm_map_delete(map, addr, addr + size);
370df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
3711c7c3c6aSMatthew Dillon 			if (flags & M_ASLEEP) {
3721c7c3c6aSMatthew Dillon 				VM_AWAIT;
3731c7c3c6aSMatthew Dillon 			}
374df8bae1dSRodney W. Grimes 			return (0);
375df8bae1dSRodney W. Grimes 		}
376e69763a3SDoug Rabson 		vm_page_flag_clear(m, PG_ZERO);
3777fb0c17eSDavid Greenman 		m->valid = VM_PAGE_BITS_ALL;
378df8bae1dSRodney W. Grimes 	}
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 	/*
3810d94caffSDavid Greenman 	 * Mark map entry as non-pageable. Assert: vm_map_insert() will never
3820d94caffSDavid Greenman 	 * be able to extend the previous entry so there will be a new entry
3830d94caffSDavid Greenman 	 * exactly corresponding to this address range and it will have
3840d94caffSDavid Greenman 	 * wired_count == 0.
385df8bae1dSRodney W. Grimes 	 */
386df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, addr, &entry) ||
387df8bae1dSRodney W. Grimes 	    entry->start != addr || entry->end != addr + size ||
388c7003c69SAlan Cox 	    entry->wired_count != 0)
389df8bae1dSRodney W. Grimes 		panic("kmem_malloc: entry not found or misaligned");
390c7003c69SAlan Cox 	entry->wired_count = 1;
391df8bae1dSRodney W. Grimes 
392b7b2aac2SJohn Dyson 	vm_map_simplify_entry(map, entry);
393b7b2aac2SJohn Dyson 
394df8bae1dSRodney W. Grimes 	/*
3950d94caffSDavid Greenman 	 * Loop thru pages, entering them in the pmap. (We cannot add them to
3960d94caffSDavid Greenman 	 * the wired count without wrapping the vm_page_queue_lock in
3970d94caffSDavid Greenman 	 * splimp...)
398df8bae1dSRodney W. Grimes 	 */
399df8bae1dSRodney W. Grimes 	for (i = 0; i < size; i += PAGE_SIZE) {
400a316d390SJohn Dyson 		m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i));
401bd7e5f99SJohn Dyson 		vm_page_wire(m);
402e69763a3SDoug Rabson 		vm_page_wakeup(m);
4031c7c3c6aSMatthew Dillon 		/*
4041c7c3c6aSMatthew Dillon 		 * Because this is kernel_pmap, this call will not block.
4051c7c3c6aSMatthew Dillon 		 */
406b18bfc3dSJohn Dyson 		pmap_enter(kernel_pmap, addr + i, VM_PAGE_TO_PHYS(m),
407b18bfc3dSJohn Dyson 			VM_PROT_ALL, 1);
408e69763a3SDoug Rabson 		vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE | PG_REFERENCED);
409df8bae1dSRodney W. Grimes 	}
410df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 	return (addr);
413df8bae1dSRodney W. Grimes }
414df8bae1dSRodney W. Grimes 
415df8bae1dSRodney W. Grimes /*
4161c7c3c6aSMatthew Dillon  *	kmem_alloc_wait:
417df8bae1dSRodney W. Grimes  *
418df8bae1dSRodney W. Grimes  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
419df8bae1dSRodney W. Grimes  *	has no room, the caller sleeps waiting for more memory in the submap.
420df8bae1dSRodney W. Grimes  *
4211c7c3c6aSMatthew Dillon  *	This routine may block.
422df8bae1dSRodney W. Grimes  */
4231c7c3c6aSMatthew Dillon 
4240d94caffSDavid Greenman vm_offset_t
4250d94caffSDavid Greenman kmem_alloc_wait(map, size)
426df8bae1dSRodney W. Grimes 	vm_map_t map;
427df8bae1dSRodney W. Grimes 	vm_size_t size;
428df8bae1dSRodney W. Grimes {
429df8bae1dSRodney W. Grimes 	vm_offset_t addr;
430df8bae1dSRodney W. Grimes 
431df8bae1dSRodney W. Grimes 	size = round_page(size);
432df8bae1dSRodney W. Grimes 
433df8bae1dSRodney W. Grimes 	for (;;) {
434df8bae1dSRodney W. Grimes 		/*
4350d94caffSDavid Greenman 		 * To make this work for more than one map, use the map's lock
4360d94caffSDavid Greenman 		 * to lock out sleepers/wakers.
437df8bae1dSRodney W. Grimes 		 */
438df8bae1dSRodney W. Grimes 		vm_map_lock(map);
439e47ed70bSJohn Dyson 		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
440df8bae1dSRodney W. Grimes 			break;
441df8bae1dSRodney W. Grimes 		/* no space now; see if we can ever get space */
442df8bae1dSRodney W. Grimes 		if (vm_map_max(map) - vm_map_min(map) < size) {
443df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
444df8bae1dSRodney W. Grimes 			return (0);
445df8bae1dSRodney W. Grimes 		}
446df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
44724a1cce3SDavid Greenman 		tsleep(map, PVM, "kmaw", 0);
448df8bae1dSRodney W. Grimes 	}
449bd7e5f99SJohn Dyson 	vm_map_insert(map, NULL, (vm_offset_t) 0, addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
450df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
451df8bae1dSRodney W. Grimes 	return (addr);
452df8bae1dSRodney W. Grimes }
453df8bae1dSRodney W. Grimes 
454df8bae1dSRodney W. Grimes /*
4551c7c3c6aSMatthew Dillon  *	kmem_free_wakeup:
456df8bae1dSRodney W. Grimes  *
45724a1cce3SDavid Greenman  *	Returns memory to a submap of the kernel, and wakes up any processes
458df8bae1dSRodney W. Grimes  *	waiting for memory in that map.
459df8bae1dSRodney W. Grimes  */
4600d94caffSDavid Greenman void
4610d94caffSDavid Greenman kmem_free_wakeup(map, addr, size)
462df8bae1dSRodney W. Grimes 	vm_map_t map;
463df8bae1dSRodney W. Grimes 	vm_offset_t addr;
464df8bae1dSRodney W. Grimes 	vm_size_t size;
465df8bae1dSRodney W. Grimes {
466df8bae1dSRodney W. Grimes 	vm_map_lock(map);
467df8bae1dSRodney W. Grimes 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
46824a1cce3SDavid Greenman 	wakeup(map);
469df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
470df8bae1dSRodney W. Grimes }
471df8bae1dSRodney W. Grimes 
472df8bae1dSRodney W. Grimes /*
4731c7c3c6aSMatthew Dillon  * 	kmem_init:
4741c7c3c6aSMatthew Dillon  *
4751c7c3c6aSMatthew Dillon  *	Create the kernel map; insert a mapping covering kernel text,
4761c7c3c6aSMatthew Dillon  *	data, bss, and all space allocated thus far (`boostrap' data).  The
4771c7c3c6aSMatthew Dillon  *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
4781c7c3c6aSMatthew Dillon  *	`start' as allocated, and the range between `start' and `end' as free.
479df8bae1dSRodney W. Grimes  */
4801c7c3c6aSMatthew Dillon 
4810d94caffSDavid Greenman void
4820d94caffSDavid Greenman kmem_init(start, end)
483df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
484df8bae1dSRodney W. Grimes {
485df8bae1dSRodney W. Grimes 	register vm_map_t m;
486df8bae1dSRodney W. Grimes 
4872d8acc0fSJohn Dyson 	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
488df8bae1dSRodney W. Grimes 	vm_map_lock(m);
489df8bae1dSRodney W. Grimes 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
490df8bae1dSRodney W. Grimes 	kernel_map = m;
4913075778bSJohn Dyson 	kernel_map->system_map = 1;
492df8bae1dSRodney W. Grimes 	(void) vm_map_insert(m, NULL, (vm_offset_t) 0,
493bd7e5f99SJohn Dyson 	    VM_MIN_KERNEL_ADDRESS, start, VM_PROT_ALL, VM_PROT_ALL, 0);
494df8bae1dSRodney W. Grimes 	/* ... and ending with the completion of the above `insert' */
495df8bae1dSRodney W. Grimes 	vm_map_unlock(m);
496df8bae1dSRodney W. Grimes }
4971c7c3c6aSMatthew Dillon 
498