1 /* $NetBSD: pmap.h,v 1.75 2008/03/11 05:34:02 matt Exp $ */ 2 3 /* 4 * Copyright (c) 1991 Regents of the University of California. 5 * All rights reserved. 6 * 7 * Changed for the VAX port. /IC 8 * 9 * This code is derived from software contributed to Berkeley by 10 * the Systems Programming Group of the University of Utah Computer 11 * Science Department. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)pmap.h 7.6 (Berkeley) 5/10/91 38 */ 39 40 /* 41 * Copyright (c) 1987 Carnegie-Mellon University 42 * 43 * Changed for the VAX port. /IC 44 * 45 * This code is derived from software contributed to Berkeley by 46 * the Systems Programming Group of the University of Utah Computer 47 * Science Department. 48 * 49 * Redistribution and use in source and binary forms, with or without 50 * modification, are permitted provided that the following conditions 51 * are met: 52 * 1. Redistributions of source code must retain the above copyright 53 * notice, this list of conditions and the following disclaimer. 54 * 2. Redistributions in binary form must reproduce the above copyright 55 * notice, this list of conditions and the following disclaimer in the 56 * documentation and/or other materials provided with the distribution. 57 * 3. All advertising materials mentioning features or use of this software 58 * must display the following acknowledgement: 59 * This product includes software developed by the University of 60 * California, Berkeley and its contributors. 61 * 4. Neither the name of the University nor the names of its contributors 62 * may be used to endorse or promote products derived from this software 63 * without specific prior written permission. 64 * 65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 75 * SUCH DAMAGE. 76 * 77 * @(#)pmap.h 7.6 (Berkeley) 5/10/91 78 */ 79 80 81 #ifndef PMAP_H 82 #define PMAP_H 83 84 #include <sys/simplelock.h> 85 86 #include <machine/pte.h> 87 #include <machine/mtpr.h> 88 #include <machine/pcb.h> 89 90 /* 91 * Some constants to make life easier. 92 */ 93 #define LTOHPS (PGSHIFT - VAX_PGSHIFT) 94 #define LTOHPN (1 << LTOHPS) 95 96 /* 97 * Pmap structure 98 * pm_stack holds lowest allocated memory for the process stack. 99 */ 100 101 typedef struct pmap { 102 struct pte *pm_p1ap; /* Base of alloced p1 pte space */ 103 int pm_count; /* reference count */ 104 struct pcb *pm_pcbs; /* PCBs using this pmap */ 105 struct pte *pm_p0br; /* page 0 base register */ 106 long pm_p0lr; /* page 0 length register */ 107 struct pte *pm_p1br; /* page 1 base register */ 108 long pm_p1lr; /* page 1 length register */ 109 struct simplelock pm_lock; /* Lock entry in MP environment */ 110 struct pmap_statistics pm_stats; /* Some statistics */ 111 } *pmap_t; 112 113 /* 114 * For each struct vm_page, there is a list of all currently valid virtual 115 * mappings of that page. An entry is a pv_entry_t, the list is pv_table. 116 */ 117 118 struct pv_entry { 119 struct pv_entry *pv_next; /* next pv_entry */ 120 vaddr_t pv_vaddr; /* address for this physical page */ 121 struct pmap *pv_pmap; /* pmap this entry belongs to */ 122 int pv_attr; /* write/modified bits */ 123 }; 124 125 extern struct pv_entry *pv_table; 126 127 /* Mapping macros used when allocating SPT */ 128 #define MAPVIRT(ptr, count) \ 129 ptr = virtual_avail; \ 130 virtual_avail += (count) * VAX_NBPG; 131 132 #define MAPPHYS(ptr, count, perm) \ 133 ptr = avail_start + KERNBASE; \ 134 avail_start += (count) * VAX_NBPG; 135 136 #ifdef _KERNEL 137 138 extern struct pmap kernel_pmap_store; 139 140 #define pmap_kernel() (&kernel_pmap_store) 141 142 #endif /* _KERNEL */ 143 144 145 /* 146 * Real nice (fast) routines to get the virtual address of a physical page 147 * (and vice versa). 148 */ 149 #define PMAP_VTOPHYS(va) ((va) & ~KERNBASE) 150 #define PMAP_MAP_POOLPAGE(pa) ((pa) | KERNBASE) 151 #define PMAP_UNMAP_POOLPAGE(va) ((va) & ~KERNBASE) 152 153 #define PMAP_STEAL_MEMORY 154 155 /* 156 * This is the by far most used pmap routine. Make it inline. 157 */ 158 __inline static bool 159 pmap_extract(pmap_t pmap, vaddr_t va, paddr_t *pap) 160 { 161 int *pte, sva; 162 163 if (va & KERNBASE) { 164 paddr_t pa; 165 166 pa = kvtophys(va); /* Is 0 if not mapped */ 167 if (pap) 168 *pap = pa; 169 if (pa) 170 return (true); 171 return (false); 172 } 173 174 sva = PG_PFNUM(va); 175 if (va < 0x40000000) { 176 if (sva >= (pmap->pm_p0lr & ~AST_MASK)) 177 goto fail; 178 pte = (int *)pmap->pm_p0br; 179 } else { 180 if (sva < pmap->pm_p1lr) 181 goto fail; 182 pte = (int *)pmap->pm_p1br; 183 } 184 /* 185 * Since the PTE tables are sparsely allocated, make sure the page 186 * table page actually exists before deferencing the pte itself. 187 */ 188 if (kvtopte(&pte[sva])->pg_v && (pte[sva] & PG_FRAME)) { 189 if (pap) 190 *pap = (pte[sva] & PG_FRAME) << VAX_PGSHIFT; 191 return (true); 192 } 193 fail: 194 if (pap) 195 *pap = 0; 196 return (false); 197 } 198 199 bool pmap_clear_modify_long(struct pv_entry *); 200 bool pmap_clear_reference_long(struct pv_entry *); 201 bool pmap_is_modified_long(struct pv_entry *); 202 void pmap_page_protect_long(struct pv_entry *, vm_prot_t); 203 void pmap_protect_long(pmap_t, vaddr_t, vaddr_t, vm_prot_t); 204 205 __inline static bool 206 pmap_is_referenced(struct vm_page *pg) 207 { 208 struct pv_entry *pv = pv_table + (VM_PAGE_TO_PHYS(pg) >> PGSHIFT); 209 bool rv = (pv->pv_attr & PG_V) != 0; 210 211 return rv; 212 } 213 214 __inline static bool 215 pmap_clear_reference(struct vm_page *pg) 216 { 217 struct pv_entry *pv = pv_table + (VM_PAGE_TO_PHYS(pg) >> PGSHIFT); 218 bool rv = (pv->pv_attr & PG_V) != 0; 219 220 pv->pv_attr &= ~PG_V; 221 if (pv->pv_pmap != NULL || pv->pv_next != NULL) 222 rv |= pmap_clear_reference_long(pv); 223 return rv; 224 } 225 226 __inline static bool 227 pmap_clear_modify(struct vm_page *pg) 228 { 229 struct pv_entry *pv = pv_table + (VM_PAGE_TO_PHYS(pg) >> PGSHIFT); 230 bool rv = (pv->pv_attr & PG_M) != 0; 231 232 pv->pv_attr &= ~PG_M; 233 if (pv->pv_pmap != NULL || pv->pv_next != NULL) 234 rv |= pmap_clear_modify_long(pv); 235 return rv; 236 } 237 238 __inline static bool 239 pmap_is_modified(struct vm_page *pg) 240 { 241 struct pv_entry *pv = pv_table + (VM_PAGE_TO_PHYS(pg) >> PGSHIFT); 242 if (pv->pv_attr & PG_M) 243 return 1; 244 else 245 return pmap_is_modified_long(pv); 246 } 247 248 __inline static void 249 pmap_page_protect(struct vm_page *pg, vm_prot_t prot) 250 { 251 struct pv_entry *pv = pv_table + (VM_PAGE_TO_PHYS(pg) >> PGSHIFT); 252 253 if (pv->pv_pmap != NULL || pv->pv_next != NULL) 254 pmap_page_protect_long(pv, prot); 255 } 256 257 __inline static void 258 pmap_protect(pmap_t pmap, vaddr_t start, vaddr_t end, vm_prot_t prot) 259 { 260 if (pmap->pm_p0lr != 0 || pmap->pm_p1lr != 0x200000 || 261 (start & KERNBASE) != 0) 262 pmap_protect_long(pmap, start, end, prot); 263 } 264 265 static __inline void 266 pmap_remove_all(struct pmap *pmap) 267 { 268 /* Nothing. */ 269 } 270 271 /* Routines that are best to define as macros */ 272 #define pmap_phys_address(phys) ((u_int)(phys) << PGSHIFT) 273 #define pmap_copy(a,b,c,d,e) /* Dont do anything */ 274 #define pmap_update(pmap) /* nothing (yet) */ 275 #define pmap_collect(pmap) /* No need so far */ 276 #define pmap_remove(pmap, start, slut) pmap_protect(pmap, start, slut, 0) 277 #define pmap_resident_count(pmap) ((pmap)->pm_stats.resident_count) 278 #define pmap_wired_count(pmap) ((pmap)->pm_stats.wired_count) 279 #define pmap_reference(pmap) (pmap)->pm_count++ 280 281 /* These can be done as efficient inline macros */ 282 #define pmap_copy_page(src, dst) \ 283 __asm("addl3 $0x80000000,%0,%%r0;" \ 284 "addl3 $0x80000000,%1,%%r1;" \ 285 "movc3 $4096,(%%r0),(%%r1)" \ 286 :: "r"(src), "r"(dst) \ 287 : "r0","r1","r2","r3","r4","r5"); 288 289 #define pmap_zero_page(phys) \ 290 __asm("addl3 $0x80000000,%0,%%r0;" \ 291 "movc5 $0,(%%r0),$0,$4096,(%%r0)" \ 292 :: "r"(phys) \ 293 : "r0","r1","r2","r3","r4","r5"); 294 295 /* Prototypes */ 296 void pmap_bootstrap(void); 297 vaddr_t pmap_map(vaddr_t, vaddr_t, vaddr_t, int); 298 299 #endif /* PMAP_H */ 300