xref: /freebsd/sys/vm/vm_kern.c (revision 8ce2d00a044cef91b79cbc72ec106f79186bf34a)
160727d8bSWarner Losh /*-
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  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_kern.c	8.3 (Berkeley) 1/12/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Kernel memory management.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
7060363fb9SLuigi Rizzo #include <sys/kernel.h>		/* for ticks and hz */
710f2c2ce0SPawel Jakub Dawidek #include <sys/eventhandler.h>
72fb919e4dSMark Murray #include <sys/lock.h>
73fb919e4dSMark Murray #include <sys/mutex.h>
74f23b4c91SGarrett Wollman #include <sys/proc.h>
75a1f6d91cSDavid Greenman #include <sys/malloc.h>
76df8bae1dSRodney W. Grimes 
77df8bae1dSRodney W. Grimes #include <vm/vm.h>
78efeaf95aSDavid Greenman #include <vm/vm_param.h>
79efeaf95aSDavid Greenman #include <vm/pmap.h>
80efeaf95aSDavid Greenman #include <vm/vm_map.h>
81efeaf95aSDavid Greenman #include <vm/vm_object.h>
82df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
83df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
849b4288a3SBruce Evans #include <vm/vm_extern.h>
850f2c2ce0SPawel Jakub Dawidek #include <vm/uma.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;
90cebde069SMike Silbersack vm_map_t pipe_map;
915b0a7408SJohn Dyson vm_map_t buffer_map=0;
92f23b4c91SGarrett Wollman 
93df8bae1dSRodney W. Grimes /*
94a839bdc8SDmitrij Tejblum  *	kmem_alloc_nofault:
95a839bdc8SDmitrij Tejblum  *
96b77c2bcdSAlan Cox  *	Allocate a virtual address range with no underlying object and
97b77c2bcdSAlan Cox  *	no initial mapping to physical memory.  Any mapping from this
98b77c2bcdSAlan Cox  *	range to physical memory must be explicitly created prior to
99b77c2bcdSAlan Cox  *	its use, typically with pmap_qenter().  Any attempt to create
100b77c2bcdSAlan Cox  *	a mapping on demand through vm_fault() will result in a panic.
101a839bdc8SDmitrij Tejblum  */
102a839bdc8SDmitrij Tejblum vm_offset_t
103a839bdc8SDmitrij Tejblum kmem_alloc_nofault(map, size)
104a839bdc8SDmitrij Tejblum 	vm_map_t map;
105030f2369SAlfred Perlstein 	vm_size_t size;
106a839bdc8SDmitrij Tejblum {
107a839bdc8SDmitrij Tejblum 	vm_offset_t addr;
108030f2369SAlfred Perlstein 	int result;
109a839bdc8SDmitrij Tejblum 
110a839bdc8SDmitrij Tejblum 	size = round_page(size);
111a839bdc8SDmitrij Tejblum 	addr = vm_map_min(map);
112848d1419SAlan Cox 	result = vm_map_find(map, NULL, 0,
113a839bdc8SDmitrij Tejblum 	    &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
114a839bdc8SDmitrij Tejblum 	if (result != KERN_SUCCESS) {
115a839bdc8SDmitrij Tejblum 		return (0);
116a839bdc8SDmitrij Tejblum 	}
117a839bdc8SDmitrij Tejblum 	return (addr);
118a839bdc8SDmitrij Tejblum }
119a839bdc8SDmitrij Tejblum 
120a839bdc8SDmitrij Tejblum /*
121df8bae1dSRodney W. Grimes  *	Allocate wired-down memory in the kernel's address map
122df8bae1dSRodney W. Grimes  *	or a submap.
123df8bae1dSRodney W. Grimes  */
1240d94caffSDavid Greenman vm_offset_t
1250d94caffSDavid Greenman kmem_alloc(map, size)
126030f2369SAlfred Perlstein 	vm_map_t map;
127030f2369SAlfred Perlstein 	vm_size_t size;
128df8bae1dSRodney W. Grimes {
129df8bae1dSRodney W. Grimes 	vm_offset_t addr;
130030f2369SAlfred Perlstein 	vm_offset_t offset;
131df8bae1dSRodney W. Grimes 	vm_offset_t i;
132df8bae1dSRodney W. Grimes 
133df8bae1dSRodney W. Grimes 	size = round_page(size);
134df8bae1dSRodney W. Grimes 
135df8bae1dSRodney W. Grimes 	/*
1360d94caffSDavid Greenman 	 * Use the kernel object for wired-down kernel pages. Assume that no
1370d94caffSDavid Greenman 	 * region of the kernel object is referenced more than once.
138df8bae1dSRodney W. Grimes 	 */
139df8bae1dSRodney W. Grimes 
140df8bae1dSRodney W. Grimes 	/*
1410d94caffSDavid Greenman 	 * Locate sufficient space in the map.  This will give us the final
1420d94caffSDavid Greenman 	 * virtual address for the new memory, and thus will tell us the
1430d94caffSDavid Greenman 	 * offset within the kernel map.
144df8bae1dSRodney W. Grimes 	 */
145df8bae1dSRodney W. Grimes 	vm_map_lock(map);
146e47ed70bSJohn Dyson 	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
147df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
148df8bae1dSRodney W. Grimes 		return (0);
149df8bae1dSRodney W. Grimes 	}
150df8bae1dSRodney W. Grimes 	offset = addr - VM_MIN_KERNEL_ADDRESS;
151df8bae1dSRodney W. Grimes 	vm_object_reference(kernel_object);
152bd7e5f99SJohn Dyson 	vm_map_insert(map, kernel_object, offset, addr, addr + size,
153bd7e5f99SJohn Dyson 		VM_PROT_ALL, VM_PROT_ALL, 0);
154df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes 	/*
1570d94caffSDavid Greenman 	 * Guarantee that there are pages already in this object before
1588f101a2fSJonathan Mini 	 * calling vm_map_wire.  This is to prevent the following
1590d94caffSDavid Greenman 	 * scenario:
160df8bae1dSRodney W. Grimes 	 *
1610d94caffSDavid Greenman 	 * 1) Threads have swapped out, so that there is a pager for the
1620d94caffSDavid Greenman 	 * kernel_object. 2) The kmsg zone is empty, and so we are
1638f101a2fSJonathan Mini 	 * kmem_allocing a new page for it. 3) vm_map_wire calls vm_fault;
1640d94caffSDavid Greenman 	 * there is no page, but there is a pager, so we call
1650d94caffSDavid Greenman 	 * pager_data_request.  But the kmsg zone is empty, so we must
1660d94caffSDavid Greenman 	 * kmem_alloc. 4) goto 1 5) Even if the kmsg zone is not empty: when
1670d94caffSDavid Greenman 	 * we get the data back from the pager, it will be (very stale)
1680d94caffSDavid Greenman 	 * non-zero data.  kmem_alloc is defined to return zero-filled memory.
169df8bae1dSRodney W. Grimes 	 *
1700d94caffSDavid Greenman 	 * We're intentionally not activating the pages we allocate to prevent a
1718f101a2fSJonathan Mini 	 * race with page-out.  vm_map_wire will wire the pages.
172df8bae1dSRodney W. Grimes 	 */
17349c06616SAlan Cox 	VM_OBJECT_LOCK(kernel_object);
174df8bae1dSRodney W. Grimes 	for (i = 0; i < size; i += PAGE_SIZE) {
175df8bae1dSRodney W. Grimes 		vm_page_t mem;
176df8bae1dSRodney W. Grimes 
17795461b45SJohn Dyson 		mem = vm_page_grab(kernel_object, OFF_TO_IDX(offset + i),
178ddf4bb37SAlan Cox 		    VM_ALLOC_NOBUSY | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1797fb0c17eSDavid Greenman 		mem->valid = VM_PAGE_BITS_ALL;
1809f5c801bSAlan Cox 		KASSERT((mem->flags & PG_UNMANAGED) != 0,
1819f5c801bSAlan Cox 		    ("kmem_alloc: page %p is managed", mem));
182df8bae1dSRodney W. Grimes 	}
18349c06616SAlan Cox 	VM_OBJECT_UNLOCK(kernel_object);
184df8bae1dSRodney W. Grimes 
185df8bae1dSRodney W. Grimes 	/*
186df8bae1dSRodney W. Grimes 	 * And finally, mark the data as non-pageable.
187df8bae1dSRodney W. Grimes 	 */
188abd498aaSBruce M Simpson 	(void) vm_map_wire(map, addr, addr + size,
189abd498aaSBruce M Simpson 	    VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
190df8bae1dSRodney W. Grimes 
191df8bae1dSRodney W. Grimes 	return (addr);
192df8bae1dSRodney W. Grimes }
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes /*
195df8bae1dSRodney W. Grimes  *	kmem_free:
196df8bae1dSRodney W. Grimes  *
197df8bae1dSRodney W. Grimes  *	Release a region of kernel virtual memory allocated
198df8bae1dSRodney W. Grimes  *	with kmem_alloc, and return the physical pages
199df8bae1dSRodney W. Grimes  *	associated with that region.
2001c7c3c6aSMatthew Dillon  *
2011c7c3c6aSMatthew Dillon  *	This routine may not block on kernel maps.
202df8bae1dSRodney W. Grimes  */
2030d94caffSDavid Greenman void
2040d94caffSDavid Greenman kmem_free(map, addr, size)
205df8bae1dSRodney W. Grimes 	vm_map_t map;
206030f2369SAlfred Perlstein 	vm_offset_t addr;
207df8bae1dSRodney W. Grimes 	vm_size_t size;
208df8bae1dSRodney W. Grimes {
20923955314SAlfred Perlstein 
210df8bae1dSRodney W. Grimes 	(void) vm_map_remove(map, trunc_page(addr), round_page(addr + size));
211df8bae1dSRodney W. Grimes }
212df8bae1dSRodney W. Grimes 
213df8bae1dSRodney W. Grimes /*
214df8bae1dSRodney W. Grimes  *	kmem_suballoc:
215df8bae1dSRodney W. Grimes  *
216df8bae1dSRodney W. Grimes  *	Allocates a map to manage a subrange
217df8bae1dSRodney W. Grimes  *	of the kernel virtual address space.
218df8bae1dSRodney W. Grimes  *
219df8bae1dSRodney W. Grimes  *	Arguments are as follows:
220df8bae1dSRodney W. Grimes  *
221df8bae1dSRodney W. Grimes  *	parent		Map to take range from
222df8bae1dSRodney W. Grimes  *	min, max	Returned endpoints of map
223030f2369SAlfred Perlstein  *	size		Size of range to find
224df8bae1dSRodney W. Grimes  */
2250d94caffSDavid Greenman vm_map_t
2262d8acc0fSJohn Dyson kmem_suballoc(parent, min, max, size)
2276e4f51d1SAlfred Perlstein 	vm_map_t parent;
228df8bae1dSRodney W. Grimes 	vm_offset_t *min, *max;
2296e4f51d1SAlfred Perlstein 	vm_size_t size;
230df8bae1dSRodney W. Grimes {
2316e4f51d1SAlfred Perlstein 	int ret;
232df8bae1dSRodney W. Grimes 	vm_map_t result;
23323955314SAlfred Perlstein 
234df8bae1dSRodney W. Grimes 	size = round_page(size);
235df8bae1dSRodney W. Grimes 
236df8bae1dSRodney W. Grimes 	*min = (vm_offset_t) vm_map_min(parent);
237df8bae1dSRodney W. Grimes 	ret = vm_map_find(parent, NULL, (vm_offset_t) 0,
238bd7e5f99SJohn Dyson 	    min, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
239df8bae1dSRodney W. Grimes 	if (ret != KERN_SUCCESS) {
240df8bae1dSRodney W. Grimes 		printf("kmem_suballoc: bad status return of %d.\n", ret);
241df8bae1dSRodney W. Grimes 		panic("kmem_suballoc");
242df8bae1dSRodney W. Grimes 	}
243df8bae1dSRodney W. Grimes 	*max = *min + size;
2442d8acc0fSJohn Dyson 	result = vm_map_create(vm_map_pmap(parent), *min, *max);
245df8bae1dSRodney W. Grimes 	if (result == NULL)
246df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: cannot create submap");
2476e4f51d1SAlfred Perlstein 	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
248df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: unable to change range to submap");
249df8bae1dSRodney W. Grimes 	return (result);
250df8bae1dSRodney W. Grimes }
251df8bae1dSRodney W. Grimes 
252df8bae1dSRodney W. Grimes /*
2531c7c3c6aSMatthew Dillon  *	kmem_malloc:
2541c7c3c6aSMatthew Dillon  *
255df8bae1dSRodney W. Grimes  * 	Allocate wired-down memory in the kernel's address map for the higher
256df8bae1dSRodney W. Grimes  * 	level kernel memory allocator (kern/kern_malloc.c).  We cannot use
257df8bae1dSRodney W. Grimes  * 	kmem_alloc() because we may need to allocate memory at interrupt
258df8bae1dSRodney W. Grimes  * 	level where we cannot block (canwait == FALSE).
259df8bae1dSRodney W. Grimes  *
260df8bae1dSRodney W. Grimes  * 	This routine has its own private kernel submap (kmem_map) and object
261df8bae1dSRodney W. Grimes  * 	(kmem_object).  This, combined with the fact that only malloc uses
262df8bae1dSRodney W. Grimes  * 	this routine, ensures that we will never block in map or object waits.
263df8bae1dSRodney W. Grimes  *
264df8bae1dSRodney W. Grimes  * 	Note that this still only works in a uni-processor environment and
265df8bae1dSRodney W. Grimes  * 	when called at splhigh().
266df8bae1dSRodney W. Grimes  *
267df8bae1dSRodney W. Grimes  * 	We don't worry about expanding the map (adding entries) since entries
268df8bae1dSRodney W. Grimes  * 	for wired maps are statically allocated.
2691c7c3c6aSMatthew Dillon  *
2701c7c3c6aSMatthew Dillon  *	NOTE:  This routine is not supposed to block if M_NOWAIT is set, but
2711c7c3c6aSMatthew Dillon  *	I have not verified that it actually does not block.
27208442f8aSBosko Milekic  *
27308442f8aSBosko Milekic  *	`map' is ONLY allowed to be kmem_map or one of the mbuf submaps to
27408442f8aSBosko Milekic  *	which we never free.
275df8bae1dSRodney W. Grimes  */
276df8bae1dSRodney W. Grimes vm_offset_t
2771c7c3c6aSMatthew Dillon kmem_malloc(map, size, flags)
278030f2369SAlfred Perlstein 	vm_map_t map;
279030f2369SAlfred Perlstein 	vm_size_t size;
2801c7c3c6aSMatthew Dillon 	int flags;
281df8bae1dSRodney W. Grimes {
282030f2369SAlfred Perlstein 	vm_offset_t offset, i;
283df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
284df8bae1dSRodney W. Grimes 	vm_offset_t addr;
285df8bae1dSRodney W. Grimes 	vm_page_t m;
2861e081f88SJeff Roberson 	int pflags;
287df8bae1dSRodney W. Grimes 
288df8bae1dSRodney W. Grimes 	size = round_page(size);
289df8bae1dSRodney W. Grimes 	addr = vm_map_min(map);
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes 	/*
2920d94caffSDavid Greenman 	 * Locate sufficient space in the map.  This will give us the final
2930d94caffSDavid Greenman 	 * virtual address for the new memory, and thus will tell us the
2940d94caffSDavid Greenman 	 * offset within the kernel map.
295df8bae1dSRodney W. Grimes 	 */
296df8bae1dSRodney W. Grimes 	vm_map_lock(map);
297e47ed70bSJohn Dyson 	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
298df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
2990f2c2ce0SPawel Jakub Dawidek 		if ((flags & M_NOWAIT) == 0) {
3000f2c2ce0SPawel Jakub Dawidek 			EVENTHANDLER_INVOKE(vm_lowmem, 0);
3010f2c2ce0SPawel Jakub Dawidek 			uma_reclaim();
3020f2c2ce0SPawel Jakub Dawidek 			vm_map_lock(map);
3030f2c2ce0SPawel Jakub Dawidek 			if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
3040f2c2ce0SPawel Jakub Dawidek 				vm_map_unlock(map);
3053efc015bSPeter Wemm 				panic("kmem_malloc(%ld): kmem_map too small: %ld total allocated",
3063efc015bSPeter Wemm 					(long)size, (long)map->size);
3070f2c2ce0SPawel Jakub Dawidek 			}
3080f2c2ce0SPawel Jakub Dawidek 		} else {
309f31c239dSAlan Cox 			return (0);
310df8bae1dSRodney W. Grimes 		}
3110f2c2ce0SPawel Jakub Dawidek 	}
3120891ef4cSJohn Dyson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
313df8bae1dSRodney W. Grimes 	vm_object_reference(kmem_object);
314bd7e5f99SJohn Dyson 	vm_map_insert(map, kmem_object, offset, addr, addr + size,
315bd7e5f99SJohn Dyson 		VM_PROT_ALL, VM_PROT_ALL, 0);
316df8bae1dSRodney W. Grimes 
3171c7c3c6aSMatthew Dillon 	/*
3181c7c3c6aSMatthew Dillon 	 * Note: if M_NOWAIT specified alone, allocate from
3191c7c3c6aSMatthew Dillon 	 * interrupt-safe queues only (just the free list).  If
32002cd7c3cSJohn Baldwin 	 * M_USE_RESERVE is also specified, we can also
3211c7c3c6aSMatthew Dillon 	 * allocate from the cache.  Neither of the latter two
3221c7c3c6aSMatthew Dillon 	 * flags may be specified from an interrupt since interrupts
3231c7c3c6aSMatthew Dillon 	 * are not allowed to mess with the cache queue.
3241c7c3c6aSMatthew Dillon 	 */
3251e081f88SJeff Roberson 
32695f24639SJeff Roberson 	if ((flags & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT)
327a623fedeSAlan Cox 		pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED;
32895f24639SJeff Roberson 	else
329a623fedeSAlan Cox 		pflags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED;
33095f24639SJeff Roberson 
33195f24639SJeff Roberson 	if (flags & M_ZERO)
33295f24639SJeff Roberson 		pflags |= VM_ALLOC_ZERO;
33395f24639SJeff Roberson 
334acbff226SAlan Cox 	VM_OBJECT_LOCK(kmem_object);
3351e081f88SJeff Roberson 	for (i = 0; i < size; i += PAGE_SIZE) {
3361e081f88SJeff Roberson retry:
33795f24639SJeff Roberson 		m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i), pflags);
338df8bae1dSRodney W. Grimes 
339df8bae1dSRodney W. Grimes 		/*
3400d94caffSDavid Greenman 		 * Ran out of space, free everything up and return. Don't need
3410d94caffSDavid Greenman 		 * to lock page queues here as we know that the pages we got
3420d94caffSDavid Greenman 		 * aren't on any queues.
343df8bae1dSRodney W. Grimes 		 */
344df8bae1dSRodney W. Grimes 		if (m == NULL) {
3451c7c3c6aSMatthew Dillon 			if ((flags & M_NOWAIT) == 0) {
346acbff226SAlan Cox 				VM_OBJECT_UNLOCK(kmem_object);
347c7003c69SAlan Cox 				vm_map_unlock(map);
348b18bfc3dSJohn Dyson 				VM_WAIT;
349c7003c69SAlan Cox 				vm_map_lock(map);
350acbff226SAlan Cox 				VM_OBJECT_LOCK(kmem_object);
351b18bfc3dSJohn Dyson 				goto retry;
352b18bfc3dSJohn Dyson 			}
353ff91d780STor Egge 			/*
354ff91d780STor Egge 			 * Free the pages before removing the map entry.
355ff91d780STor Egge 			 * They are already marked busy.  Calling
356ff91d780STor Egge 			 * vm_map_delete before the pages has been freed or
357ff91d780STor Egge 			 * unbusied will cause a deadlock.
358ff91d780STor Egge 			 */
359ff91d780STor Egge 			while (i != 0) {
360ff91d780STor Egge 				i -= PAGE_SIZE;
361ff91d780STor Egge 				m = vm_page_lookup(kmem_object,
362ff91d780STor Egge 						   OFF_TO_IDX(offset + i));
36357123de6SAlan Cox 				vm_page_lock_queues();
364a623fedeSAlan Cox 				vm_page_unwire(m, 0);
365ff91d780STor Egge 				vm_page_free(m);
36657123de6SAlan Cox 				vm_page_unlock_queues();
367ff91d780STor Egge 			}
368acbff226SAlan Cox 			VM_OBJECT_UNLOCK(kmem_object);
369df8bae1dSRodney W. Grimes 			vm_map_delete(map, addr, addr + size);
370df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
371f31c239dSAlan Cox 			return (0);
372df8bae1dSRodney W. Grimes 		}
3731e081f88SJeff Roberson 		if (flags & M_ZERO && (m->flags & PG_ZERO) == 0)
374fff6062aSAlan Cox 			pmap_zero_page(m);
37549c06616SAlan Cox 		m->valid = VM_PAGE_BITS_ALL;
3769f5c801bSAlan Cox 		KASSERT((m->flags & PG_UNMANAGED) != 0,
3779f5c801bSAlan Cox 		    ("kmem_malloc: page %p is managed", m));
378df8bae1dSRodney W. Grimes 	}
379acbff226SAlan Cox 	VM_OBJECT_UNLOCK(kmem_object);
380df8bae1dSRodney W. Grimes 
381df8bae1dSRodney W. Grimes 	/*
3820d94caffSDavid Greenman 	 * Mark map entry as non-pageable. Assert: vm_map_insert() will never
3830d94caffSDavid Greenman 	 * be able to extend the previous entry so there will be a new entry
3840d94caffSDavid Greenman 	 * exactly corresponding to this address range and it will have
3850d94caffSDavid Greenman 	 * wired_count == 0.
386df8bae1dSRodney W. Grimes 	 */
387df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, addr, &entry) ||
388df8bae1dSRodney W. Grimes 	    entry->start != addr || entry->end != addr + size ||
389c7003c69SAlan Cox 	    entry->wired_count != 0)
390df8bae1dSRodney W. Grimes 		panic("kmem_malloc: entry not found or misaligned");
391c7003c69SAlan Cox 	entry->wired_count = 1;
392df8bae1dSRodney W. Grimes 
393ff5dcf25SAlan Cox 	/*
394ff5dcf25SAlan Cox 	 * At this point, the kmem_object must be unlocked because
395ff5dcf25SAlan Cox 	 * vm_map_simplify_entry() calls vm_object_deallocate(), which
396ff5dcf25SAlan Cox 	 * locks the kmem_object.
397ff5dcf25SAlan Cox 	 */
398b7b2aac2SJohn Dyson 	vm_map_simplify_entry(map, entry);
399b7b2aac2SJohn Dyson 
400df8bae1dSRodney W. Grimes 	/*
4010f3b612aSAlan Cox 	 * Loop thru pages, entering them in the pmap.
402df8bae1dSRodney W. Grimes 	 */
403acbff226SAlan Cox 	VM_OBJECT_LOCK(kmem_object);
404ff5dcf25SAlan Cox 	for (i = 0; i < size; i += PAGE_SIZE) {
405a316d390SJohn Dyson 		m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i));
4061c7c3c6aSMatthew Dillon 		/*
4071c7c3c6aSMatthew Dillon 		 * Because this is kernel_pmap, this call will not block.
4081c7c3c6aSMatthew Dillon 		 */
4090385347cSPeter Wemm 		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 1);
41066bdd5d6SAlan Cox 		vm_page_wakeup(m);
411df8bae1dSRodney W. Grimes 	}
412ff5dcf25SAlan Cox 	VM_OBJECT_UNLOCK(kmem_object);
413df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
414df8bae1dSRodney W. Grimes 
415df8bae1dSRodney W. Grimes 	return (addr);
416df8bae1dSRodney W. Grimes }
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes /*
4191c7c3c6aSMatthew Dillon  *	kmem_alloc_wait:
420df8bae1dSRodney W. Grimes  *
421df8bae1dSRodney W. Grimes  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
422df8bae1dSRodney W. Grimes  *	has no room, the caller sleeps waiting for more memory in the submap.
423df8bae1dSRodney W. Grimes  *
4241c7c3c6aSMatthew Dillon  *	This routine may block.
425df8bae1dSRodney W. Grimes  */
4260d94caffSDavid Greenman vm_offset_t
4270d94caffSDavid Greenman kmem_alloc_wait(map, size)
428df8bae1dSRodney W. Grimes 	vm_map_t map;
429df8bae1dSRodney W. Grimes 	vm_size_t size;
430df8bae1dSRodney W. Grimes {
431df8bae1dSRodney W. Grimes 	vm_offset_t addr;
43223955314SAlfred Perlstein 
433df8bae1dSRodney W. Grimes 	size = round_page(size);
434df8bae1dSRodney W. Grimes 
435df8bae1dSRodney W. Grimes 	for (;;) {
436df8bae1dSRodney W. Grimes 		/*
4370d94caffSDavid Greenman 		 * To make this work for more than one map, use the map's lock
4380d94caffSDavid Greenman 		 * to lock out sleepers/wakers.
439df8bae1dSRodney W. Grimes 		 */
440df8bae1dSRodney W. Grimes 		vm_map_lock(map);
441e47ed70bSJohn Dyson 		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
442df8bae1dSRodney W. Grimes 			break;
443df8bae1dSRodney W. Grimes 		/* no space now; see if we can ever get space */
444df8bae1dSRodney W. Grimes 		if (vm_map_max(map) - vm_map_min(map) < size) {
445df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
446df8bae1dSRodney W. Grimes 			return (0);
447df8bae1dSRodney W. Grimes 		}
4489688f931SAlan Cox 		map->needs_wakeup = TRUE;
4498ce2d00aSPawel Jakub Dawidek 		vm_map_unlock_and_wait(map, 0);
450df8bae1dSRodney W. Grimes 	}
4519688f931SAlan Cox 	vm_map_insert(map, NULL, 0, addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
452df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
453df8bae1dSRodney W. Grimes 	return (addr);
454df8bae1dSRodney W. Grimes }
455df8bae1dSRodney W. Grimes 
456df8bae1dSRodney W. Grimes /*
4571c7c3c6aSMatthew Dillon  *	kmem_free_wakeup:
458df8bae1dSRodney W. Grimes  *
45924a1cce3SDavid Greenman  *	Returns memory to a submap of the kernel, and wakes up any processes
460df8bae1dSRodney W. Grimes  *	waiting for memory in that map.
461df8bae1dSRodney W. Grimes  */
4620d94caffSDavid Greenman void
4630d94caffSDavid Greenman kmem_free_wakeup(map, addr, size)
464df8bae1dSRodney W. Grimes 	vm_map_t map;
465df8bae1dSRodney W. Grimes 	vm_offset_t addr;
466df8bae1dSRodney W. Grimes 	vm_size_t size;
467df8bae1dSRodney W. Grimes {
46823955314SAlfred Perlstein 
469df8bae1dSRodney W. Grimes 	vm_map_lock(map);
470df8bae1dSRodney W. Grimes 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
4719688f931SAlan Cox 	if (map->needs_wakeup) {
4729688f931SAlan Cox 		map->needs_wakeup = FALSE;
4739688f931SAlan Cox 		vm_map_wakeup(map);
4749688f931SAlan Cox 	}
475df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
476df8bae1dSRodney W. Grimes }
477df8bae1dSRodney W. Grimes 
478df8bae1dSRodney W. Grimes /*
4791c7c3c6aSMatthew Dillon  * 	kmem_init:
4801c7c3c6aSMatthew Dillon  *
4811c7c3c6aSMatthew Dillon  *	Create the kernel map; insert a mapping covering kernel text,
4821c7c3c6aSMatthew Dillon  *	data, bss, and all space allocated thus far (`boostrap' data).  The
4831c7c3c6aSMatthew Dillon  *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
4841c7c3c6aSMatthew Dillon  *	`start' as allocated, and the range between `start' and `end' as free.
485df8bae1dSRodney W. Grimes  */
4860d94caffSDavid Greenman void
4870d94caffSDavid Greenman kmem_init(start, end)
488df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
489df8bae1dSRodney W. Grimes {
490030f2369SAlfred Perlstein 	vm_map_t m;
491df8bae1dSRodney W. Grimes 
4922d8acc0fSJohn Dyson 	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
493c9267356SAlan Cox 	m->system_map = 1;
494df8bae1dSRodney W. Grimes 	vm_map_lock(m);
495df8bae1dSRodney W. Grimes 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
496df8bae1dSRodney W. Grimes 	kernel_map = m;
497c9267356SAlan Cox 	(void) vm_map_insert(m, NULL, (vm_ooffset_t) 0,
498e6eaadbaSAlan Cox 	    VM_MIN_KERNEL_ADDRESS, start, VM_PROT_ALL, VM_PROT_ALL,
499e6eaadbaSAlan Cox 	    MAP_NOFAULT);
500df8bae1dSRodney W. Grimes 	/* ... and ending with the completion of the above `insert' */
501df8bae1dSRodney W. Grimes 	vm_map_unlock(m);
502df8bae1dSRodney W. Grimes }
503