xref: /freebsd/sys/vm/vm_kern.c (revision 2395531439bb140427dff4dfd6d67856f907c15e)
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
175929bcfaSPhilippe Charnier  *    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  *
64c3aac50fSPeter Wemm  * $FreeBSD$
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>
73fb919e4dSMark Murray #include <sys/lock.h>
74fb919e4dSMark Murray #include <sys/mutex.h>
75f23b4c91SGarrett Wollman #include <sys/proc.h>
76a1f6d91cSDavid Greenman #include <sys/malloc.h>
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes #include <vm/vm.h>
79efeaf95aSDavid Greenman #include <vm/vm_param.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 buffer_map=0;
925b0a7408SJohn Dyson vm_map_t mb_map=0;
935b0a7408SJohn Dyson int mb_map_full=0;
94f23b4c91SGarrett Wollman 
95df8bae1dSRodney W. Grimes /*
96df8bae1dSRodney W. Grimes  *	kmem_alloc_pageable:
97df8bae1dSRodney W. Grimes  *
98df8bae1dSRodney W. Grimes  *	Allocate pageable memory to the kernel's address map.
99f81b8592SDavid Greenman  *	"map" must be kernel_map or a submap of kernel_map.
100df8bae1dSRodney W. Grimes  */
101df8bae1dSRodney W. Grimes 
1020d94caffSDavid Greenman vm_offset_t
1030d94caffSDavid Greenman kmem_alloc_pageable(map, size)
104df8bae1dSRodney W. Grimes 	vm_map_t map;
105030f2369SAlfred Perlstein 	vm_size_t size;
106df8bae1dSRodney W. Grimes {
107df8bae1dSRodney W. Grimes 	vm_offset_t addr;
108030f2369SAlfred Perlstein 	int result;
10923955314SAlfred Perlstein 	int hadvmlock;
110df8bae1dSRodney W. Grimes 
11123955314SAlfred Perlstein 	hadvmlock = mtx_owned(&vm_mtx);
11223955314SAlfred Perlstein 	if (!hadvmlock)
11323955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
114df8bae1dSRodney W. Grimes 	size = round_page(size);
115df8bae1dSRodney W. Grimes 	addr = vm_map_min(map);
116df8bae1dSRodney W. Grimes 	result = vm_map_find(map, NULL, (vm_offset_t) 0,
117bd7e5f99SJohn Dyson 	    &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
11823955314SAlfred Perlstein 	if (!hadvmlock)
11923955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
120df8bae1dSRodney W. Grimes 	if (result != KERN_SUCCESS) {
121df8bae1dSRodney W. Grimes 		return (0);
122df8bae1dSRodney W. Grimes 	}
123df8bae1dSRodney W. Grimes 	return (addr);
124df8bae1dSRodney W. Grimes }
125df8bae1dSRodney W. Grimes 
126df8bae1dSRodney W. Grimes /*
127a839bdc8SDmitrij Tejblum  *	kmem_alloc_nofault:
128a839bdc8SDmitrij Tejblum  *
129a839bdc8SDmitrij Tejblum  *	Same as kmem_alloc_pageable, except that it create a nofault entry.
130a839bdc8SDmitrij Tejblum  */
131a839bdc8SDmitrij Tejblum 
132a839bdc8SDmitrij Tejblum vm_offset_t
133a839bdc8SDmitrij Tejblum kmem_alloc_nofault(map, size)
134a839bdc8SDmitrij Tejblum 	vm_map_t map;
135030f2369SAlfred Perlstein 	vm_size_t size;
136a839bdc8SDmitrij Tejblum {
137a839bdc8SDmitrij Tejblum 	vm_offset_t addr;
138030f2369SAlfred Perlstein 	int result;
139a839bdc8SDmitrij Tejblum 
14023955314SAlfred Perlstein 	int hadvmlock;
14123955314SAlfred Perlstein 
14223955314SAlfred Perlstein 	hadvmlock = mtx_owned(&vm_mtx);
14323955314SAlfred Perlstein 	if (!hadvmlock)
14423955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
145a839bdc8SDmitrij Tejblum 	size = round_page(size);
146a839bdc8SDmitrij Tejblum 	addr = vm_map_min(map);
147a839bdc8SDmitrij Tejblum 	result = vm_map_find(map, NULL, (vm_offset_t) 0,
148a839bdc8SDmitrij Tejblum 	    &addr, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
14923955314SAlfred Perlstein 	if (!hadvmlock)
15023955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
151a839bdc8SDmitrij Tejblum 	if (result != KERN_SUCCESS) {
152a839bdc8SDmitrij Tejblum 		return (0);
153a839bdc8SDmitrij Tejblum 	}
154a839bdc8SDmitrij Tejblum 	return (addr);
155a839bdc8SDmitrij Tejblum }
156a839bdc8SDmitrij Tejblum 
157a839bdc8SDmitrij Tejblum /*
158df8bae1dSRodney W. Grimes  *	Allocate wired-down memory in the kernel's address map
159df8bae1dSRodney W. Grimes  *	or a submap.
160df8bae1dSRodney W. Grimes  */
1610d94caffSDavid Greenman vm_offset_t
1620d94caffSDavid Greenman kmem_alloc(map, size)
163030f2369SAlfred Perlstein 	vm_map_t map;
164030f2369SAlfred Perlstein 	vm_size_t size;
165df8bae1dSRodney W. Grimes {
166df8bae1dSRodney W. Grimes 	vm_offset_t addr;
167030f2369SAlfred Perlstein 	vm_offset_t offset;
168df8bae1dSRodney W. Grimes 	vm_offset_t i;
16923955314SAlfred Perlstein 	int hadvmlock;
170df8bae1dSRodney W. Grimes 
17123955314SAlfred Perlstein 	hadvmlock = mtx_owned(&vm_mtx);
17223955314SAlfred Perlstein 	if (!hadvmlock)
17323955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
174df8bae1dSRodney W. Grimes 	size = round_page(size);
175df8bae1dSRodney W. Grimes 
176df8bae1dSRodney W. Grimes 	/*
1770d94caffSDavid Greenman 	 * Use the kernel object for wired-down kernel pages. Assume that no
1780d94caffSDavid Greenman 	 * region of the kernel object is referenced more than once.
179df8bae1dSRodney W. Grimes 	 */
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes 	/*
1820d94caffSDavid Greenman 	 * Locate sufficient space in the map.  This will give us the final
1830d94caffSDavid Greenman 	 * virtual address for the new memory, and thus will tell us the
1840d94caffSDavid Greenman 	 * offset within the kernel map.
185df8bae1dSRodney W. Grimes 	 */
186df8bae1dSRodney W. Grimes 	vm_map_lock(map);
187e47ed70bSJohn Dyson 	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
188df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
18923955314SAlfred Perlstein 		if (!hadvmlock)
19023955314SAlfred Perlstein 			mtx_unlock(&vm_mtx);
191df8bae1dSRodney W. Grimes 		return (0);
192df8bae1dSRodney W. Grimes 	}
193df8bae1dSRodney W. Grimes 	offset = addr - VM_MIN_KERNEL_ADDRESS;
194df8bae1dSRodney W. Grimes 	vm_object_reference(kernel_object);
195bd7e5f99SJohn Dyson 	vm_map_insert(map, kernel_object, offset, addr, addr + size,
196bd7e5f99SJohn Dyson 		VM_PROT_ALL, VM_PROT_ALL, 0);
197df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
198df8bae1dSRodney W. Grimes 
199df8bae1dSRodney W. Grimes 	/*
2000d94caffSDavid Greenman 	 * Guarantee that there are pages already in this object before
2010d94caffSDavid Greenman 	 * calling vm_map_pageable.  This is to prevent the following
2020d94caffSDavid Greenman 	 * scenario:
203df8bae1dSRodney W. Grimes 	 *
2040d94caffSDavid Greenman 	 * 1) Threads have swapped out, so that there is a pager for the
2050d94caffSDavid Greenman 	 * kernel_object. 2) The kmsg zone is empty, and so we are
2060d94caffSDavid Greenman 	 * kmem_allocing a new page for it. 3) vm_map_pageable calls vm_fault;
2070d94caffSDavid Greenman 	 * there is no page, but there is a pager, so we call
2080d94caffSDavid Greenman 	 * pager_data_request.  But the kmsg zone is empty, so we must
2090d94caffSDavid Greenman 	 * kmem_alloc. 4) goto 1 5) Even if the kmsg zone is not empty: when
2100d94caffSDavid Greenman 	 * we get the data back from the pager, it will be (very stale)
2110d94caffSDavid Greenman 	 * non-zero data.  kmem_alloc is defined to return zero-filled memory.
212df8bae1dSRodney W. Grimes 	 *
2130d94caffSDavid Greenman 	 * We're intentionally not activating the pages we allocate to prevent a
2140d94caffSDavid Greenman 	 * race with page-out.  vm_map_pageable will wire the pages.
215df8bae1dSRodney W. Grimes 	 */
216df8bae1dSRodney W. Grimes 
217df8bae1dSRodney W. Grimes 	for (i = 0; i < size; i += PAGE_SIZE) {
218df8bae1dSRodney W. Grimes 		vm_page_t mem;
219df8bae1dSRodney W. Grimes 
22095461b45SJohn Dyson 		mem = vm_page_grab(kernel_object, OFF_TO_IDX(offset + i),
22195461b45SJohn Dyson 				VM_ALLOC_ZERO | VM_ALLOC_RETRY);
222f70f05f2SJohn Dyson 		if ((mem->flags & PG_ZERO) == 0)
223df8bae1dSRodney W. Grimes 			vm_page_zero_fill(mem);
2247fb0c17eSDavid Greenman 		mem->valid = VM_PAGE_BITS_ALL;
2251c7c3c6aSMatthew Dillon 		vm_page_flag_clear(mem, PG_ZERO);
2261c7c3c6aSMatthew Dillon 		vm_page_wakeup(mem);
227df8bae1dSRodney W. Grimes 	}
228df8bae1dSRodney W. Grimes 
229df8bae1dSRodney W. Grimes 	/*
230df8bae1dSRodney W. Grimes 	 * And finally, mark the data as non-pageable.
231df8bae1dSRodney W. Grimes 	 */
232df8bae1dSRodney W. Grimes 
233df8bae1dSRodney W. Grimes 	(void) vm_map_pageable(map, (vm_offset_t) addr, addr + size, FALSE);
234df8bae1dSRodney W. Grimes 
23523955314SAlfred Perlstein 	if (!hadvmlock)
23623955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
237df8bae1dSRodney W. Grimes 	return (addr);
238df8bae1dSRodney W. Grimes }
239df8bae1dSRodney W. Grimes 
240df8bae1dSRodney W. Grimes /*
241df8bae1dSRodney W. Grimes  *	kmem_free:
242df8bae1dSRodney W. Grimes  *
243df8bae1dSRodney W. Grimes  *	Release a region of kernel virtual memory allocated
244df8bae1dSRodney W. Grimes  *	with kmem_alloc, and return the physical pages
245df8bae1dSRodney W. Grimes  *	associated with that region.
2461c7c3c6aSMatthew Dillon  *
2471c7c3c6aSMatthew Dillon  *	This routine may not block on kernel maps.
248df8bae1dSRodney W. Grimes  */
2490d94caffSDavid Greenman void
2500d94caffSDavid Greenman kmem_free(map, addr, size)
251df8bae1dSRodney W. Grimes 	vm_map_t map;
252030f2369SAlfred Perlstein 	vm_offset_t addr;
253df8bae1dSRodney W. Grimes 	vm_size_t size;
254df8bae1dSRodney W. Grimes {
25523955314SAlfred Perlstein 	int hadvmlock;
256e2181d41SJohn Baldwin 
25723955314SAlfred Perlstein 	hadvmlock = mtx_owned(&vm_mtx);
25823955314SAlfred Perlstein 	if (!hadvmlock)
25923955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
26023955314SAlfred Perlstein 
261df8bae1dSRodney W. Grimes 	(void) vm_map_remove(map, trunc_page(addr), round_page(addr + size));
26223955314SAlfred Perlstein 
26323955314SAlfred Perlstein 	if (!hadvmlock)
26423955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
265df8bae1dSRodney W. Grimes }
266df8bae1dSRodney W. Grimes 
267df8bae1dSRodney W. Grimes /*
268df8bae1dSRodney W. Grimes  *	kmem_suballoc:
269df8bae1dSRodney W. Grimes  *
270df8bae1dSRodney W. Grimes  *	Allocates a map to manage a subrange
271df8bae1dSRodney W. Grimes  *	of the kernel virtual address space.
272df8bae1dSRodney W. Grimes  *
273df8bae1dSRodney W. Grimes  *	Arguments are as follows:
274df8bae1dSRodney W. Grimes  *
275df8bae1dSRodney W. Grimes  *	parent		Map to take range from
276df8bae1dSRodney W. Grimes  *	min, max	Returned endpoints of map
277030f2369SAlfred Perlstein  *	size		Size of range to find
278df8bae1dSRodney W. Grimes  */
2790d94caffSDavid Greenman vm_map_t
2802d8acc0fSJohn Dyson kmem_suballoc(parent, min, max, size)
2816e4f51d1SAlfred Perlstein 	vm_map_t parent;
282df8bae1dSRodney W. Grimes 	vm_offset_t *min, *max;
2836e4f51d1SAlfred Perlstein 	vm_size_t size;
284df8bae1dSRodney W. Grimes {
2856e4f51d1SAlfred Perlstein 	int ret;
286df8bae1dSRodney W. Grimes 	vm_map_t result;
28723955314SAlfred Perlstein 	int hadvmlock;
28823955314SAlfred Perlstein 
28923955314SAlfred Perlstein 	hadvmlock = mtx_owned(&vm_mtx);
29023955314SAlfred Perlstein 	if (!hadvmlock)
29123955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
292df8bae1dSRodney W. Grimes 
293df8bae1dSRodney W. Grimes 	size = round_page(size);
294df8bae1dSRodney W. Grimes 
295df8bae1dSRodney W. Grimes 	*min = (vm_offset_t) vm_map_min(parent);
296df8bae1dSRodney W. Grimes 	ret = vm_map_find(parent, NULL, (vm_offset_t) 0,
297bd7e5f99SJohn Dyson 	    min, size, TRUE, VM_PROT_ALL, VM_PROT_ALL, 0);
298df8bae1dSRodney W. Grimes 	if (ret != KERN_SUCCESS) {
299df8bae1dSRodney W. Grimes 		printf("kmem_suballoc: bad status return of %d.\n", ret);
300df8bae1dSRodney W. Grimes 		panic("kmem_suballoc");
301df8bae1dSRodney W. Grimes 	}
302df8bae1dSRodney W. Grimes 	*max = *min + size;
303df8bae1dSRodney W. Grimes 	pmap_reference(vm_map_pmap(parent));
3042d8acc0fSJohn Dyson 	result = vm_map_create(vm_map_pmap(parent), *min, *max);
305df8bae1dSRodney W. Grimes 	if (result == NULL)
306df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: cannot create submap");
3076e4f51d1SAlfred Perlstein 	if (vm_map_submap(parent, *min, *max, result) != KERN_SUCCESS)
308df8bae1dSRodney W. Grimes 		panic("kmem_suballoc: unable to change range to submap");
30923955314SAlfred Perlstein 	if (!hadvmlock)
31023955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
311df8bae1dSRodney W. Grimes 	return (result);
312df8bae1dSRodney W. Grimes }
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes /*
3151c7c3c6aSMatthew Dillon  *	kmem_malloc:
3161c7c3c6aSMatthew Dillon  *
317df8bae1dSRodney W. Grimes  * 	Allocate wired-down memory in the kernel's address map for the higher
318df8bae1dSRodney W. Grimes  * 	level kernel memory allocator (kern/kern_malloc.c).  We cannot use
319df8bae1dSRodney W. Grimes  * 	kmem_alloc() because we may need to allocate memory at interrupt
320df8bae1dSRodney W. Grimes  * 	level where we cannot block (canwait == FALSE).
321df8bae1dSRodney W. Grimes  *
322df8bae1dSRodney W. Grimes  * 	This routine has its own private kernel submap (kmem_map) and object
323df8bae1dSRodney W. Grimes  * 	(kmem_object).  This, combined with the fact that only malloc uses
324df8bae1dSRodney W. Grimes  * 	this routine, ensures that we will never block in map or object waits.
325df8bae1dSRodney W. Grimes  *
326df8bae1dSRodney W. Grimes  * 	Note that this still only works in a uni-processor environment and
327df8bae1dSRodney W. Grimes  * 	when called at splhigh().
328df8bae1dSRodney W. Grimes  *
329df8bae1dSRodney W. Grimes  * 	We don't worry about expanding the map (adding entries) since entries
330df8bae1dSRodney W. Grimes  * 	for wired maps are statically allocated.
3311c7c3c6aSMatthew Dillon  *
3321c7c3c6aSMatthew Dillon  *	NOTE:  This routine is not supposed to block if M_NOWAIT is set, but
3331c7c3c6aSMatthew Dillon  *	I have not verified that it actually does not block.
334df8bae1dSRodney W. Grimes  */
335df8bae1dSRodney W. Grimes vm_offset_t
3361c7c3c6aSMatthew Dillon kmem_malloc(map, size, flags)
337030f2369SAlfred Perlstein 	vm_map_t map;
338030f2369SAlfred Perlstein 	vm_size_t size;
3391c7c3c6aSMatthew Dillon 	int flags;
340df8bae1dSRodney W. Grimes {
341030f2369SAlfred Perlstein 	vm_offset_t offset, i;
342df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
343df8bae1dSRodney W. Grimes 	vm_offset_t addr;
344df8bae1dSRodney W. Grimes 	vm_page_t m;
34523955314SAlfred Perlstein 	int hadvmlock;
346df8bae1dSRodney W. Grimes 
347649c409dSDavid Greenman 	if (map != kmem_map && map != mb_map)
348649c409dSDavid Greenman 		panic("kmem_malloc: map != {kmem,mb}_map");
349df8bae1dSRodney W. Grimes 
35023955314SAlfred Perlstein 	hadvmlock = mtx_owned(&vm_mtx);
35123955314SAlfred Perlstein 	if (!hadvmlock)
35223955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
35323955314SAlfred Perlstein 
354df8bae1dSRodney W. Grimes 	size = round_page(size);
355df8bae1dSRodney W. Grimes 	addr = vm_map_min(map);
356df8bae1dSRodney W. Grimes 
357df8bae1dSRodney W. Grimes 	/*
3580d94caffSDavid Greenman 	 * Locate sufficient space in the map.  This will give us the final
3590d94caffSDavid Greenman 	 * virtual address for the new memory, and thus will tell us the
3600d94caffSDavid Greenman 	 * offset within the kernel map.
361df8bae1dSRodney W. Grimes 	 */
362df8bae1dSRodney W. Grimes 	vm_map_lock(map);
363e47ed70bSJohn Dyson 	if (vm_map_findspace(map, vm_map_min(map), size, &addr)) {
364df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
3655eb7d0cdSDavid Greenman 		if (map == mb_map) {
3665eb7d0cdSDavid Greenman 			mb_map_full = TRUE;
3675994d893SDavid Greenman 			printf("Out of mbuf clusters - adjust NMBCLUSTERS or increase maxusers!\n");
36823955314SAlfred Perlstein 			goto bad;
3695eb7d0cdSDavid Greenman 		}
3701c7c3c6aSMatthew Dillon 		if ((flags & M_NOWAIT) == 0)
3713efc015bSPeter Wemm 			panic("kmem_malloc(%ld): kmem_map too small: %ld total allocated",
3723efc015bSPeter Wemm 				(long)size, (long)map->size);
37323955314SAlfred Perlstein 		goto bad;
374df8bae1dSRodney W. Grimes 	}
3750891ef4cSJohn Dyson 	offset = addr - VM_MIN_KERNEL_ADDRESS;
376df8bae1dSRodney W. Grimes 	vm_object_reference(kmem_object);
377bd7e5f99SJohn Dyson 	vm_map_insert(map, kmem_object, offset, addr, addr + size,
378bd7e5f99SJohn Dyson 		VM_PROT_ALL, VM_PROT_ALL, 0);
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 	for (i = 0; i < size; i += PAGE_SIZE) {
3811c7c3c6aSMatthew Dillon 		/*
3821c7c3c6aSMatthew Dillon 		 * Note: if M_NOWAIT specified alone, allocate from
3831c7c3c6aSMatthew Dillon 		 * interrupt-safe queues only (just the free list).  If
3841c7c3c6aSMatthew Dillon 		 * M_ASLEEP or M_USE_RESERVE is also specified, we can also
3851c7c3c6aSMatthew Dillon 		 * allocate from the cache.  Neither of the latter two
3861c7c3c6aSMatthew Dillon 		 * flags may be specified from an interrupt since interrupts
3871c7c3c6aSMatthew Dillon 		 * are not allowed to mess with the cache queue.
3881c7c3c6aSMatthew Dillon 		 */
389b18bfc3dSJohn Dyson retry:
390a316d390SJohn Dyson 		m = vm_page_alloc(kmem_object, OFF_TO_IDX(offset + i),
3911c7c3c6aSMatthew Dillon 		    ((flags & (M_NOWAIT|M_ASLEEP|M_USE_RESERVE)) == M_NOWAIT) ?
3921c7c3c6aSMatthew Dillon 			VM_ALLOC_INTERRUPT :
3931c7c3c6aSMatthew Dillon 			VM_ALLOC_SYSTEM);
394df8bae1dSRodney W. Grimes 
395df8bae1dSRodney W. Grimes 		/*
3960d94caffSDavid Greenman 		 * Ran out of space, free everything up and return. Don't need
3970d94caffSDavid Greenman 		 * to lock page queues here as we know that the pages we got
3980d94caffSDavid Greenman 		 * aren't on any queues.
399df8bae1dSRodney W. Grimes 		 */
400df8bae1dSRodney W. Grimes 		if (m == NULL) {
4011c7c3c6aSMatthew Dillon 			if ((flags & M_NOWAIT) == 0) {
402c7003c69SAlan Cox 				vm_map_unlock(map);
403b18bfc3dSJohn Dyson 				VM_WAIT;
404c7003c69SAlan Cox 				vm_map_lock(map);
405b18bfc3dSJohn Dyson 				goto retry;
406b18bfc3dSJohn Dyson 			}
407df8bae1dSRodney W. Grimes 			vm_map_delete(map, addr, addr + size);
408df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
4091c7c3c6aSMatthew Dillon 			if (flags & M_ASLEEP) {
4101c7c3c6aSMatthew Dillon 				VM_AWAIT;
4111c7c3c6aSMatthew Dillon 			}
41223955314SAlfred Perlstein 			goto bad;
413df8bae1dSRodney W. Grimes 		}
414e69763a3SDoug Rabson 		vm_page_flag_clear(m, PG_ZERO);
4157fb0c17eSDavid Greenman 		m->valid = VM_PAGE_BITS_ALL;
416df8bae1dSRodney W. Grimes 	}
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes 	/*
4190d94caffSDavid Greenman 	 * Mark map entry as non-pageable. Assert: vm_map_insert() will never
4200d94caffSDavid Greenman 	 * be able to extend the previous entry so there will be a new entry
4210d94caffSDavid Greenman 	 * exactly corresponding to this address range and it will have
4220d94caffSDavid Greenman 	 * wired_count == 0.
423df8bae1dSRodney W. Grimes 	 */
424df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, addr, &entry) ||
425df8bae1dSRodney W. Grimes 	    entry->start != addr || entry->end != addr + size ||
426c7003c69SAlan Cox 	    entry->wired_count != 0)
427df8bae1dSRodney W. Grimes 		panic("kmem_malloc: entry not found or misaligned");
428c7003c69SAlan Cox 	entry->wired_count = 1;
429df8bae1dSRodney W. Grimes 
430b7b2aac2SJohn Dyson 	vm_map_simplify_entry(map, entry);
431b7b2aac2SJohn Dyson 
432df8bae1dSRodney W. Grimes 	/*
4330d94caffSDavid Greenman 	 * Loop thru pages, entering them in the pmap. (We cannot add them to
4340d94caffSDavid Greenman 	 * the wired count without wrapping the vm_page_queue_lock in
4350d94caffSDavid Greenman 	 * splimp...)
436df8bae1dSRodney W. Grimes 	 */
437df8bae1dSRodney W. Grimes 	for (i = 0; i < size; i += PAGE_SIZE) {
438a316d390SJohn Dyson 		m = vm_page_lookup(kmem_object, OFF_TO_IDX(offset + i));
439bd7e5f99SJohn Dyson 		vm_page_wire(m);
440e69763a3SDoug Rabson 		vm_page_wakeup(m);
4411c7c3c6aSMatthew Dillon 		/*
4421c7c3c6aSMatthew Dillon 		 * Because this is kernel_pmap, this call will not block.
4431c7c3c6aSMatthew Dillon 		 */
4440385347cSPeter Wemm 		pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 1);
445e69763a3SDoug Rabson 		vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE | PG_REFERENCED);
446df8bae1dSRodney W. Grimes 	}
447df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
448df8bae1dSRodney W. Grimes 
44923955314SAlfred Perlstein 	if (!hadvmlock)
45023955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
451df8bae1dSRodney W. Grimes 	return (addr);
45223955314SAlfred Perlstein 
45323955314SAlfred Perlstein bad:
45423955314SAlfred Perlstein 	if (!hadvmlock)
45523955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
45623955314SAlfred Perlstein 	return (0);
457df8bae1dSRodney W. Grimes }
458df8bae1dSRodney W. Grimes 
459df8bae1dSRodney W. Grimes /*
4601c7c3c6aSMatthew Dillon  *	kmem_alloc_wait:
461df8bae1dSRodney W. Grimes  *
462df8bae1dSRodney W. Grimes  *	Allocates pageable memory from a sub-map of the kernel.  If the submap
463df8bae1dSRodney W. Grimes  *	has no room, the caller sleeps waiting for more memory in the submap.
464df8bae1dSRodney W. Grimes  *
4651c7c3c6aSMatthew Dillon  *	This routine may block.
466df8bae1dSRodney W. Grimes  */
4671c7c3c6aSMatthew Dillon 
4680d94caffSDavid Greenman vm_offset_t
4690d94caffSDavid Greenman kmem_alloc_wait(map, size)
470df8bae1dSRodney W. Grimes 	vm_map_t map;
471df8bae1dSRodney W. Grimes 	vm_size_t size;
472df8bae1dSRodney W. Grimes {
473df8bae1dSRodney W. Grimes 	vm_offset_t addr;
47423955314SAlfred Perlstein 	int hadvmlock;
47523955314SAlfred Perlstein 
47623955314SAlfred Perlstein 	hadvmlock = mtx_owned(&vm_mtx);
47723955314SAlfred Perlstein 	if (!hadvmlock)
47823955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
479df8bae1dSRodney W. Grimes 
480df8bae1dSRodney W. Grimes 	size = round_page(size);
481df8bae1dSRodney W. Grimes 
482df8bae1dSRodney W. Grimes 	for (;;) {
483df8bae1dSRodney W. Grimes 		/*
4840d94caffSDavid Greenman 		 * To make this work for more than one map, use the map's lock
4850d94caffSDavid Greenman 		 * to lock out sleepers/wakers.
486df8bae1dSRodney W. Grimes 		 */
487df8bae1dSRodney W. Grimes 		vm_map_lock(map);
488e47ed70bSJohn Dyson 		if (vm_map_findspace(map, vm_map_min(map), size, &addr) == 0)
489df8bae1dSRodney W. Grimes 			break;
490df8bae1dSRodney W. Grimes 		/* no space now; see if we can ever get space */
491df8bae1dSRodney W. Grimes 		if (vm_map_max(map) - vm_map_min(map) < size) {
492df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
49323955314SAlfred Perlstein 			if (!hadvmlock)
49423955314SAlfred Perlstein 				mtx_unlock(&vm_mtx);
495df8bae1dSRodney W. Grimes 			return (0);
496df8bae1dSRodney W. Grimes 		}
497df8bae1dSRodney W. Grimes 		vm_map_unlock(map);
49823955314SAlfred Perlstein 		msleep(map, &vm_mtx, PVM, "kmaw", 0);
499df8bae1dSRodney W. Grimes 	}
500bd7e5f99SJohn Dyson 	vm_map_insert(map, NULL, (vm_offset_t) 0, addr, addr + size, VM_PROT_ALL, VM_PROT_ALL, 0);
501df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
50223955314SAlfred Perlstein 	if (!hadvmlock)
50323955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
504df8bae1dSRodney W. Grimes 	return (addr);
505df8bae1dSRodney W. Grimes }
506df8bae1dSRodney W. Grimes 
507df8bae1dSRodney W. Grimes /*
5081c7c3c6aSMatthew Dillon  *	kmem_free_wakeup:
509df8bae1dSRodney W. Grimes  *
51024a1cce3SDavid Greenman  *	Returns memory to a submap of the kernel, and wakes up any processes
511df8bae1dSRodney W. Grimes  *	waiting for memory in that map.
512df8bae1dSRodney W. Grimes  */
5130d94caffSDavid Greenman void
5140d94caffSDavid Greenman kmem_free_wakeup(map, addr, size)
515df8bae1dSRodney W. Grimes 	vm_map_t map;
516df8bae1dSRodney W. Grimes 	vm_offset_t addr;
517df8bae1dSRodney W. Grimes 	vm_size_t size;
518df8bae1dSRodney W. Grimes {
51923955314SAlfred Perlstein 	int hadvmlock;
52023955314SAlfred Perlstein 
52123955314SAlfred Perlstein 	hadvmlock = mtx_owned(&vm_mtx);
52223955314SAlfred Perlstein 	if (!hadvmlock)
52323955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
524df8bae1dSRodney W. Grimes 	vm_map_lock(map);
525df8bae1dSRodney W. Grimes 	(void) vm_map_delete(map, trunc_page(addr), round_page(addr + size));
52624a1cce3SDavid Greenman 	wakeup(map);
527df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
52823955314SAlfred Perlstein 	if (!hadvmlock)
52923955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
530df8bae1dSRodney W. Grimes }
531df8bae1dSRodney W. Grimes 
532df8bae1dSRodney W. Grimes /*
5331c7c3c6aSMatthew Dillon  * 	kmem_init:
5341c7c3c6aSMatthew Dillon  *
5351c7c3c6aSMatthew Dillon  *	Create the kernel map; insert a mapping covering kernel text,
5361c7c3c6aSMatthew Dillon  *	data, bss, and all space allocated thus far (`boostrap' data).  The
5371c7c3c6aSMatthew Dillon  *	new map will thus map the range between VM_MIN_KERNEL_ADDRESS and
5381c7c3c6aSMatthew Dillon  *	`start' as allocated, and the range between `start' and `end' as free.
539df8bae1dSRodney W. Grimes  */
5401c7c3c6aSMatthew Dillon 
5410d94caffSDavid Greenman void
5420d94caffSDavid Greenman kmem_init(start, end)
543df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
544df8bae1dSRodney W. Grimes {
545030f2369SAlfred Perlstein 	vm_map_t m;
546df8bae1dSRodney W. Grimes 
5472d8acc0fSJohn Dyson 	m = vm_map_create(kernel_pmap, VM_MIN_KERNEL_ADDRESS, end);
548df8bae1dSRodney W. Grimes 	vm_map_lock(m);
549df8bae1dSRodney W. Grimes 	/* N.B.: cannot use kgdb to debug, starting with this assignment ... */
550df8bae1dSRodney W. Grimes 	kernel_map = m;
5513075778bSJohn Dyson 	kernel_map->system_map = 1;
552df8bae1dSRodney W. Grimes 	(void) vm_map_insert(m, NULL, (vm_offset_t) 0,
553bd7e5f99SJohn Dyson 	    VM_MIN_KERNEL_ADDRESS, start, VM_PROT_ALL, VM_PROT_ALL, 0);
554df8bae1dSRodney W. Grimes 	/* ... and ending with the completion of the above `insert' */
555df8bae1dSRodney W. Grimes 	vm_map_unlock(m);
556df8bae1dSRodney W. Grimes }
557