2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
37 #include <sys/mutex.h>
39 #include <sys/rwlock.h>
40 #include <sys/sysctl.h>
44 #include <vm/vm_param.h>
45 #include <vm/vm_object.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_pageout.h>
48 #include <vm/vm_pager.h>
50 /* list of phys pager objects */
51 static struct pagerlst phys_pager_object_list;
52 /* protect access to phys_pager_object_list */
53 static struct mtx phys_pager_mtx;
55 static int default_phys_pager_getpages(vm_object_t object, vm_page_t *m,
56 int count, int *rbehind, int *rahead);
57 static int default_phys_pager_populate(vm_object_t object, vm_pindex_t pidx,
58 int fault_type, vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last);
59 static boolean_t default_phys_pager_haspage(vm_object_t object,
60 vm_pindex_t pindex, int *before, int *after);
61 const struct phys_pager_ops default_phys_pg_ops = {
62 .phys_pg_getpages = default_phys_pager_getpages,
63 .phys_pg_populate = default_phys_pager_populate,
64 .phys_pg_haspage = default_phys_pager_haspage,
73 TAILQ_INIT(&phys_pager_object_list);
74 mtx_init(&phys_pager_mtx, "phys_pager list", NULL, MTX_DEF);
78 phys_pager_allocate(void *handle, const struct phys_pager_ops *ops, void *data,
79 vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff, struct ucred *cred)
81 vm_object_t object, object1;
86 * Offset should be page aligned.
91 pindex = OFF_TO_IDX(foff + PAGE_MASK + size);
95 mtx_lock(&phys_pager_mtx);
97 * Look up pager, creating as necessary.
100 object = vm_pager_object_lookup(&phys_pager_object_list, handle);
101 if (object == NULL) {
103 * Allocate object and associate it with the pager.
105 mtx_unlock(&phys_pager_mtx);
106 object1 = vm_object_allocate(OBJT_PHYS, pindex);
107 mtx_lock(&phys_pager_mtx);
108 object = vm_pager_object_lookup(&phys_pager_object_list,
110 if (object != NULL) {
112 * We raced with other thread while
115 if (pindex > object->size)
116 object->size = pindex;
121 object->handle = handle;
122 object->un_pager.phys.ops = ops;
123 object->un_pager.phys.data_ptr = data;
124 if (ops->phys_pg_populate != NULL)
125 vm_object_set_flag(object, OBJ_POPULATE);
126 TAILQ_INSERT_TAIL(&phys_pager_object_list,
127 object, pager_object_list);
130 if (pindex > object->size)
131 object->size = pindex;
133 mtx_unlock(&phys_pager_mtx);
134 vm_object_deallocate(object1);
136 object = vm_object_allocate(OBJT_PHYS, pindex);
137 object->un_pager.phys.ops = ops;
138 object->un_pager.phys.data_ptr = data;
139 if (ops->phys_pg_populate != NULL)
140 vm_object_set_flag(object, OBJ_POPULATE);
142 if (init && ops->phys_pg_ctor != NULL)
143 ops->phys_pg_ctor(object, prot, foff, cred);
149 phys_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
150 vm_ooffset_t foff, struct ucred *ucred)
152 return (phys_pager_allocate(handle, &default_phys_pg_ops, NULL,
153 size, prot, foff, ucred));
157 phys_pager_dealloc(vm_object_t object)
160 if (object->handle != NULL) {
161 VM_OBJECT_WUNLOCK(object);
162 mtx_lock(&phys_pager_mtx);
163 TAILQ_REMOVE(&phys_pager_object_list, object, pager_object_list);
164 mtx_unlock(&phys_pager_mtx);
165 VM_OBJECT_WLOCK(object);
167 object->type = OBJT_DEAD;
168 if (object->un_pager.phys.ops->phys_pg_dtor != NULL)
169 object->un_pager.phys.ops->phys_pg_dtor(object);
170 object->handle = NULL;
174 * Fill as many pages as vm_fault has allocated for us.
177 default_phys_pager_getpages(vm_object_t object, vm_page_t *m, int count,
178 int *rbehind, int *rahead)
182 for (i = 0; i < count; i++) {
183 if (vm_page_none_valid(m[i])) {
184 if ((m[i]->flags & PG_ZERO) == 0)
185 pmap_zero_page(m[i]);
188 KASSERT(vm_page_all_valid(m[i]),
189 ("phys_pager_getpages: partially valid page %p", m[i]));
190 KASSERT(m[i]->dirty == 0,
191 ("phys_pager_getpages: dirty page %p", m[i]));
197 return (VM_PAGER_OK);
201 phys_pager_getpages(vm_object_t object, vm_page_t *m, int count, int *rbehind,
204 return (object->un_pager.phys.ops->phys_pg_getpages(object, m,
205 count, rbehind, rahead));
209 * Implement a pretty aggressive clustered getpages strategy. Hint that
210 * everything in an entire 4MB window should be prefaulted at once.
212 * 4MB (1024 slots per page table page) is convenient for x86,
213 * but may not be for other arches.
216 #define PHYSCLUSTER 1024
218 static int phys_pager_cluster = PHYSCLUSTER;
219 SYSCTL_INT(_vm, OID_AUTO, phys_pager_cluster, CTLFLAG_RWTUN,
220 &phys_pager_cluster, 0,
221 "prefault window size for phys pager");
224 * Max hint to vm_page_alloc() about the further allocation needs
225 * inside the phys_pager_populate() loop. The number of bits used to
226 * implement VM_ALLOC_COUNT() determines the hard limit on this value.
227 * That limit is currently 65535.
232 default_phys_pager_populate(vm_object_t object, vm_pindex_t pidx,
233 int fault_type __unused, vm_prot_t max_prot __unused, vm_pindex_t *first,
237 vm_pindex_t base, end, i;
240 base = rounddown(pidx, phys_pager_cluster);
241 end = base + phys_pager_cluster - 1;
242 if (end >= object->size)
243 end = object->size - 1;
251 for (i = base; i <= end; i++) {
252 ahead = MIN(end - i, PHYSALLOC);
253 m = vm_page_grab(object, i,
254 VM_ALLOC_NORMAL | VM_ALLOC_COUNT(ahead));
255 if (!vm_page_all_valid(m))
256 vm_page_zero_invalid(m, TRUE);
257 KASSERT(m->dirty == 0,
258 ("phys_pager_populate: dirty page %p", m));
260 return (VM_PAGER_OK);
264 phys_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
265 vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
267 return (object->un_pager.phys.ops->phys_pg_populate(object, pidx,
268 fault_type, max_prot, first, last));
272 phys_pager_putpages(vm_object_t object, vm_page_t *m, int count, boolean_t sync,
276 panic("phys_pager_putpage called");
280 default_phys_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
283 vm_pindex_t base, end;
285 base = rounddown(pindex, phys_pager_cluster);
286 end = base + phys_pager_cluster - 1;
288 *before = pindex - base;
290 *after = end - pindex;
295 phys_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
298 return (object->un_pager.phys.ops->phys_pg_haspage(object, pindex,
302 const struct pagerops physpagerops = {
303 .pgo_kvme_type = KVME_TYPE_PHYS,
304 .pgo_init = phys_pager_init,
305 .pgo_alloc = phys_pager_alloc,
306 .pgo_dealloc = phys_pager_dealloc,
307 .pgo_getpages = phys_pager_getpages,
308 .pgo_putpages = phys_pager_putpages,
309 .pgo_haspage = phys_pager_haspage,
310 .pgo_populate = phys_pager_populate,