2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2000 Peter Wemm
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/kernel.h>
35 #include <sys/mutex.h>
37 #include <sys/rwlock.h>
38 #include <sys/sysctl.h>
42 #include <vm/vm_param.h>
43 #include <vm/vm_object.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_pageout.h>
46 #include <vm/vm_pager.h>
48 /* list of phys pager objects */
49 static struct pagerlst phys_pager_object_list;
50 /* protect access to phys_pager_object_list */
51 static struct mtx phys_pager_mtx;
53 static int default_phys_pager_getpages(vm_object_t object, vm_page_t *m,
54 int count, int *rbehind, int *rahead);
55 static int default_phys_pager_populate(vm_object_t object, vm_pindex_t pidx,
56 int fault_type, vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last);
57 static boolean_t default_phys_pager_haspage(vm_object_t object,
58 vm_pindex_t pindex, int *before, int *after);
59 const struct phys_pager_ops default_phys_pg_ops = {
60 .phys_pg_getpages = default_phys_pager_getpages,
61 .phys_pg_populate = default_phys_pager_populate,
62 .phys_pg_haspage = default_phys_pager_haspage,
71 TAILQ_INIT(&phys_pager_object_list);
72 mtx_init(&phys_pager_mtx, "phys_pager list", NULL, MTX_DEF);
76 phys_pager_allocate(void *handle, const struct phys_pager_ops *ops, void *data,
77 vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff, struct ucred *cred)
79 vm_object_t object, object1;
84 * Offset should be page aligned.
89 pindex = OFF_TO_IDX(foff + PAGE_MASK + size);
93 mtx_lock(&phys_pager_mtx);
95 * Look up pager, creating as necessary.
98 object = vm_pager_object_lookup(&phys_pager_object_list, handle);
101 * Allocate object and associate it with the pager.
103 mtx_unlock(&phys_pager_mtx);
104 object1 = vm_object_allocate(OBJT_PHYS, pindex);
105 mtx_lock(&phys_pager_mtx);
106 object = vm_pager_object_lookup(&phys_pager_object_list,
108 if (object != NULL) {
110 * We raced with other thread while
113 if (pindex > object->size)
114 object->size = pindex;
119 object->handle = handle;
120 object->un_pager.phys.ops = ops;
121 object->un_pager.phys.data_ptr = data;
122 if (ops->phys_pg_populate != NULL)
123 vm_object_set_flag(object, OBJ_POPULATE);
124 TAILQ_INSERT_TAIL(&phys_pager_object_list,
125 object, pager_object_list);
128 if (pindex > object->size)
129 object->size = pindex;
131 mtx_unlock(&phys_pager_mtx);
132 vm_object_deallocate(object1);
134 object = vm_object_allocate(OBJT_PHYS, pindex);
135 object->un_pager.phys.ops = ops;
136 object->un_pager.phys.data_ptr = data;
137 if (ops->phys_pg_populate != NULL)
138 vm_object_set_flag(object, OBJ_POPULATE);
140 if (init && ops->phys_pg_ctor != NULL)
141 ops->phys_pg_ctor(object, prot, foff, cred);
147 phys_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
148 vm_ooffset_t foff, struct ucred *ucred)
150 return (phys_pager_allocate(handle, &default_phys_pg_ops, NULL,
151 size, prot, foff, ucred));
155 phys_pager_dealloc(vm_object_t object)
158 if (object->handle != NULL) {
159 VM_OBJECT_WUNLOCK(object);
160 mtx_lock(&phys_pager_mtx);
161 TAILQ_REMOVE(&phys_pager_object_list, object, pager_object_list);
162 mtx_unlock(&phys_pager_mtx);
163 VM_OBJECT_WLOCK(object);
165 object->type = OBJT_DEAD;
166 if (object->un_pager.phys.ops->phys_pg_dtor != NULL)
167 object->un_pager.phys.ops->phys_pg_dtor(object);
168 object->handle = NULL;
172 * Fill as many pages as vm_fault has allocated for us.
175 default_phys_pager_getpages(vm_object_t object, vm_page_t *m, int count,
176 int *rbehind, int *rahead)
180 for (i = 0; i < count; i++) {
181 if (vm_page_none_valid(m[i])) {
182 if ((m[i]->flags & PG_ZERO) == 0)
183 pmap_zero_page(m[i]);
186 KASSERT(vm_page_all_valid(m[i]),
187 ("phys_pager_getpages: partially valid page %p", m[i]));
188 KASSERT(m[i]->dirty == 0,
189 ("phys_pager_getpages: dirty page %p", m[i]));
195 return (VM_PAGER_OK);
199 phys_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
202 return (object->un_pager.phys.ops->phys_pg_getpages(object, m,
203 count, rbehind, rahead));
207 * Implement a pretty aggressive clustered getpages strategy. Hint that
208 * everything in an entire 4MB window should be prefaulted at once.
210 * 4MB (1024 slots per page table page) is convenient for x86,
211 * but may not be for other arches.
214 #define PHYSCLUSTER 1024
216 static int phys_pager_cluster = PHYSCLUSTER;
217 SYSCTL_INT(_vm, OID_AUTO, phys_pager_cluster, CTLFLAG_RWTUN,
218 &phys_pager_cluster, 0,
219 "prefault window size for phys pager");
222 * Max hint to vm_page_alloc() about the further allocation needs
223 * inside the phys_pager_populate() loop. The number of bits used to
224 * implement VM_ALLOC_COUNT() determines the hard limit on this value.
225 * That limit is currently 65535.
230 default_phys_pager_populate(vm_object_t object, vm_pindex_t pidx,
231 int fault_type __unused, vm_prot_t max_prot __unused, vm_pindex_t *first,
235 vm_pindex_t base, end, i;
238 base = rounddown(pidx, phys_pager_cluster);
239 end = base + phys_pager_cluster - 1;
240 if (end >= object->size)
241 end = object->size - 1;
249 for (i = base; i <= end; i++) {
250 ahead = MIN(end - i, PHYSALLOC);
251 m = vm_page_grab(object, i,
252 VM_ALLOC_NORMAL | VM_ALLOC_COUNT(ahead));
253 if (!vm_page_all_valid(m))
254 vm_page_zero_invalid(m, TRUE);
255 KASSERT(m->dirty == 0,
256 ("phys_pager_populate: dirty page %p", m));
258 return (VM_PAGER_OK);
262 phys_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
263 vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
265 return (object->un_pager.phys.ops->phys_pg_populate(object, pidx,
266 fault_type, max_prot, first, last));
270 phys_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
274 panic("phys_pager_putpage called");
278 default_phys_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
281 vm_pindex_t base, end;
283 base = rounddown(pindex, phys_pager_cluster);
284 end = base + phys_pager_cluster - 1;
286 *before = pindex - base;
288 *after = end - pindex;
293 phys_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
296 return (object->un_pager.phys.ops->phys_pg_haspage(object, pindex,
300 const struct pagerops physpagerops = {
301 .pgo_kvme_type = KVME_TYPE_PHYS,
302 .pgo_init = phys_pager_init,
303 .pgo_alloc = phys_pager_alloc,
304 .pgo_dealloc = phys_pager_dealloc,
305 .pgo_getpages = phys_pager_getpages,
306 .pgo_putpages = phys_pager_putpages,
307 .pgo_haspage = phys_pager_haspage,
308 .pgo_populate = phys_pager_populate,