1 /*- 2 * Copyright (c) 2000 Peter Wemm 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/linker_set.h> 32 #include <sys/conf.h> 33 #include <sys/kernel.h> 34 #include <sys/lock.h> 35 #include <sys/proc.h> 36 #include <sys/mutex.h> 37 #include <sys/mman.h> 38 #include <sys/sysctl.h> 39 40 #include <vm/vm.h> 41 #include <vm/vm_object.h> 42 #include <vm/vm_page.h> 43 #include <vm/vm_pager.h> 44 45 /* list of phys pager objects */ 46 static struct pagerlst phys_pager_object_list; 47 /* protect access to phys_pager_object_list */ 48 static struct mtx phys_pager_mtx; 49 50 static void 51 phys_pager_init(void) 52 { 53 54 TAILQ_INIT(&phys_pager_object_list); 55 mtx_init(&phys_pager_mtx, "phys_pager list", NULL, MTX_DEF); 56 } 57 58 /* 59 * MPSAFE 60 */ 61 static vm_object_t 62 phys_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 63 vm_ooffset_t foff) 64 { 65 vm_object_t object, object1; 66 vm_pindex_t pindex; 67 68 /* 69 * Offset should be page aligned. 70 */ 71 if (foff & PAGE_MASK) 72 return (NULL); 73 74 pindex = OFF_TO_IDX(foff + PAGE_MASK + size); 75 76 if (handle != NULL) { 77 mtx_lock(&phys_pager_mtx); 78 /* 79 * Look up pager, creating as necessary. 80 */ 81 object1 = NULL; 82 object = vm_pager_object_lookup(&phys_pager_object_list, handle); 83 if (object == NULL) { 84 /* 85 * Allocate object and associate it with the pager. 86 */ 87 mtx_unlock(&phys_pager_mtx); 88 object1 = vm_object_allocate(OBJT_PHYS, pindex); 89 mtx_lock(&phys_pager_mtx); 90 object = vm_pager_object_lookup(&phys_pager_object_list, 91 handle); 92 if (object != NULL) { 93 /* 94 * We raced with other thread while 95 * allocating object. 96 */ 97 if (pindex > object->size) 98 object->size = pindex; 99 } else { 100 object = object1; 101 object1 = NULL; 102 object->handle = handle; 103 TAILQ_INSERT_TAIL(&phys_pager_object_list, object, 104 pager_object_list); 105 } 106 } else { 107 if (pindex > object->size) 108 object->size = pindex; 109 } 110 mtx_unlock(&phys_pager_mtx); 111 vm_object_deallocate(object1); 112 } else { 113 object = vm_object_allocate(OBJT_PHYS, pindex); 114 } 115 116 return (object); 117 } 118 119 /* 120 * MPSAFE 121 */ 122 static void 123 phys_pager_dealloc(vm_object_t object) 124 { 125 126 if (object->handle != NULL) { 127 VM_OBJECT_UNLOCK(object); 128 mtx_lock(&phys_pager_mtx); 129 TAILQ_REMOVE(&phys_pager_object_list, object, pager_object_list); 130 mtx_unlock(&phys_pager_mtx); 131 VM_OBJECT_LOCK(object); 132 } 133 } 134 135 /* 136 * Fill as many pages as vm_fault has allocated for us. 137 */ 138 static int 139 phys_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage) 140 { 141 int i; 142 143 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 144 for (i = 0; i < count; i++) { 145 if (m[i]->valid == 0) { 146 if ((m[i]->flags & PG_ZERO) == 0) 147 pmap_zero_page(m[i]); 148 m[i]->valid = VM_PAGE_BITS_ALL; 149 } 150 KASSERT(m[i]->valid == VM_PAGE_BITS_ALL, 151 ("phys_pager_getpages: partially valid page %p", m[i])); 152 m[i]->dirty = 0; 153 /* The requested page must remain busy, the others not. */ 154 if (reqpage != i) { 155 m[i]->oflags &= ~VPO_BUSY; 156 m[i]->busy = 0; 157 } 158 } 159 return (VM_PAGER_OK); 160 } 161 162 static void 163 phys_pager_putpages(vm_object_t object, vm_page_t *m, int count, boolean_t sync, 164 int *rtvals) 165 { 166 167 panic("phys_pager_putpage called"); 168 } 169 170 /* 171 * Implement a pretty aggressive clustered getpages strategy. Hint that 172 * everything in an entire 4MB window should be prefaulted at once. 173 * 174 * XXX 4MB (1024 slots per page table page) is convenient for x86, 175 * but may not be for other arches. 176 */ 177 #ifndef PHYSCLUSTER 178 #define PHYSCLUSTER 1024 179 #endif 180 static boolean_t 181 phys_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 182 int *after) 183 { 184 vm_pindex_t base, end; 185 186 base = pindex & (~(PHYSCLUSTER - 1)); 187 end = base + (PHYSCLUSTER - 1); 188 if (before != NULL) 189 *before = pindex - base; 190 if (after != NULL) 191 *after = end - pindex; 192 return (TRUE); 193 } 194 195 struct pagerops physpagerops = { 196 .pgo_init = phys_pager_init, 197 .pgo_alloc = phys_pager_alloc, 198 .pgo_dealloc = phys_pager_dealloc, 199 .pgo_getpages = phys_pager_getpages, 200 .pgo_putpages = phys_pager_putpages, 201 .pgo_haspage = phys_pager_haspage, 202 }; 203