1 /* $NetBSD: pmap.h,v 1.59 2001/12/04 00:05:05 darrenr Exp $ */ 2 3 /* 4 * Copyright (c) 1996 5 * The President and Fellows of Harvard College. All rights reserved. 6 * Copyright (c) 1992, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * This software was developed by the Computer Systems Engineering group 10 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 11 * contributed to Berkeley. 12 * 13 * All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Aaron Brown and 16 * Harvard University. 17 * This product includes software developed by the University of 18 * California, Lawrence Berkeley Laboratory. 19 * 20 * @InsertRedistribution@ 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by Aaron Brown and 24 * Harvard University. 25 * This product includes software developed by the University of 26 * California, Berkeley and its contributors. 27 * 4. Neither the name of the University nor the names of its contributors 28 * may be used to endorse or promote products derived from this software 29 * without specific prior written permission. 30 * 31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41 * SUCH DAMAGE. 42 * 43 * @(#)pmap.h 8.1 (Berkeley) 6/11/93 44 */ 45 46 #ifndef _SPARC_PMAP_H_ 47 #define _SPARC_PMAP_H_ 48 49 #if defined(_KERNEL_OPT) 50 #include "opt_sparc_arch.h" 51 #endif 52 53 #include <machine/pte.h> 54 55 /* 56 * Pmap structure. 57 * 58 * The pmap structure really comes in two variants, one---a single 59 * instance---for kernel virtual memory and the other---up to nproc 60 * instances---for user virtual memory. Unfortunately, we have to mash 61 * both into the same structure. Fortunately, they are almost the same. 62 * 63 * The kernel begins at 0xf8000000 and runs to 0xffffffff (although 64 * some of this is not actually used). Kernel space, including DVMA 65 * space (for now?), is mapped identically into all user contexts. 66 * There is no point in duplicating this mapping in each user process 67 * so they do not appear in the user structures. 68 * 69 * User space begins at 0x00000000 and runs through 0x1fffffff, 70 * then has a `hole', then resumes at 0xe0000000 and runs until it 71 * hits the kernel space at 0xf8000000. This can be mapped 72 * contiguously by ignorning the top two bits and pretending the 73 * space goes from 0 to 37ffffff. Typically the lower range is 74 * used for text+data and the upper for stack, but the code here 75 * makes no such distinction. 76 * 77 * Since each virtual segment covers 256 kbytes, the user space 78 * requires 3584 segments, while the kernel (including DVMA) requires 79 * only 512 segments. 80 * 81 * 82 ** FOR THE SUN4/SUN4C 83 * 84 * The segment map entry for virtual segment vseg is offset in 85 * pmap->pm_rsegmap by 0 if pmap is not the kernel pmap, or by 86 * NUSEG if it is. We keep a pointer called pmap->pm_segmap 87 * pre-offset by this value. pmap->pm_segmap thus contains the 88 * values to be loaded into the user portion of the hardware segment 89 * map so as to reach the proper PMEGs within the MMU. The kernel 90 * mappings are `set early' and are always valid in every context 91 * (every change is always propagated immediately). 92 * 93 * The PMEGs within the MMU are loaded `on demand'; when a PMEG is 94 * taken away from context `c', the pmap for context c has its 95 * corresponding pm_segmap[vseg] entry marked invalid (the MMU segment 96 * map entry is also made invalid at the same time). Thus 97 * pm_segmap[vseg] is the `invalid pmeg' number (127 or 511) whenever 98 * the corresponding PTEs are not actually in the MMU. On the other 99 * hand, pm_pte[vseg] is NULL only if no pages in that virtual segment 100 * are in core; otherwise it points to a copy of the 32 or 64 PTEs that 101 * must be loaded in the MMU in order to reach those pages. 102 * pm_npte[vseg] counts the number of valid pages in each vseg. 103 * 104 * XXX performance: faster to count valid bits? 105 * 106 * The kernel pmap cannot malloc() PTEs since malloc() will sometimes 107 * allocate a new virtual segment. Since kernel mappings are never 108 * `stolen' out of the MMU, we just keep all its PTEs there, and have 109 * no software copies. Its mmu entries are nonetheless kept on lists 110 * so that the code that fiddles with mmu lists has something to fiddle. 111 * 112 ** FOR THE SUN4M 113 * 114 * On this architecture, the virtual-to-physical translation (page) tables 115 * are *not* stored within the MMU as they are in the earlier Sun architect- 116 * ures; instead, they are maintained entirely within physical memory (there 117 * is a TLB cache to prevent the high performance hit from keeping all page 118 * tables in core). Thus there is no need to dynamically allocate PMEGs or 119 * SMEGs; only contexts must be shared. 120 * 121 * We maintain two parallel sets of tables: one is the actual MMU-edible 122 * hierarchy of page tables in allocated kernel memory; these tables refer 123 * to each other by physical address pointers in SRMMU format (thus they 124 * are not very useful to the kernel's management routines). The other set 125 * of tables is similar to those used for the Sun4/100's 3-level MMU; it 126 * is a hierarchy of regmap and segmap structures which contain kernel virtual 127 * pointers to each other. These must (unfortunately) be kept in sync. 128 * 129 */ 130 #define NKREG ((int)((-(unsigned)KERNBASE) / NBPRG)) /* i.e., 8 */ 131 #define NUREG (256 - NKREG) /* i.e., 248 */ 132 133 TAILQ_HEAD(mmuhd,mmuentry); 134 135 /* 136 * data appearing in both user and kernel pmaps 137 * 138 * note: if we want the same binaries to work on the 4/4c and 4m, we have to 139 * include the fields for both to make sure that the struct kproc 140 * is the same size. 141 */ 142 struct pmap { 143 union ctxinfo *pm_ctx; /* current context, if any */ 144 int pm_ctxnum; /* current context's number */ 145 struct simplelock pm_lock; /* spinlock */ 146 int pm_refcount; /* just what it says */ 147 148 struct mmuhd pm_reglist; /* MMU regions on this pmap (4/4c) */ 149 struct mmuhd pm_seglist; /* MMU segments on this pmap (4/4c) */ 150 151 struct regmap *pm_regmap; 152 153 int **pm_reg_ptps; /* SRMMU-edible region tables for 4m */ 154 int *pm_reg_ptps_pa;/* _Physical_ address of pm_reg_ptps */ 155 156 int pm_gap_start; /* Starting with this vreg there's */ 157 int pm_gap_end; /* no valid mapping until here */ 158 159 struct pmap_statistics pm_stats; /* pmap statistics */ 160 }; 161 162 struct regmap { 163 struct segmap *rg_segmap; /* point to NSGPRG PMEGs */ 164 int *rg_seg_ptps; /* SRMMU-edible segment tables (NULL 165 * indicates invalid region (4m) */ 166 smeg_t rg_smeg; /* the MMU region number (4c) */ 167 u_char rg_nsegmap; /* number of valid PMEGS */ 168 }; 169 170 struct segmap { 171 int *sg_pte; /* points to NPTESG PTEs */ 172 pmeg_t sg_pmeg; /* the MMU segment number (4c) */ 173 u_char sg_npte; /* number of valid PTEs per seg */ 174 }; 175 176 typedef struct pmap *pmap_t; 177 178 #if 0 179 struct kvm_cpustate { 180 int kvm_npmemarr; 181 struct memarr kvm_pmemarr[MA_SIZE]; 182 int kvm_seginval; /* [4,4c] */ 183 struct segmap kvm_segmap_store[NKREG*NSEGRG]; /* [4,4c] */ 184 }/*not yet used*/; 185 #endif 186 187 #ifdef _KERNEL 188 189 #define PMAP_NULL ((pmap_t)0) 190 191 extern struct pmap kernel_pmap_store; 192 193 /* 194 * Bounds on managed physical addresses. Used by (MD) users 195 * of uvm_pglistalloc() to provide search hints. 196 */ 197 extern paddr_t vm_first_phys, vm_last_phys; 198 extern psize_t vm_num_phys; 199 200 /* 201 * Since PTEs also contain type bits, we have to have some way 202 * to tell pmap_enter `this is an IO page' or `this is not to 203 * be cached'. Since physical addresses are always aligned, we 204 * can do this with the low order bits. 205 * 206 * The ordering below is important: PMAP_PGTYPE << PG_TNC must give 207 * exactly the PG_NC and PG_TYPE bits. 208 */ 209 #define PMAP_OBIO 1 /* tells pmap_enter to use PG_OBIO */ 210 #define PMAP_VME16 2 /* etc */ 211 #define PMAP_VME32 3 /* etc */ 212 #define PMAP_NC 4 /* tells pmap_enter to set PG_NC */ 213 #define PMAP_TNC_4 7 /* mask to get PG_TYPE & PG_NC */ 214 215 #define PMAP_T2PTE_4(x) (((x) & PMAP_TNC_4) << PG_TNC_SHIFT) 216 #define PMAP_IOENC_4(io) (io) 217 218 /* 219 * On a SRMMU machine, the iospace is encoded in bits [3-6] of the 220 * physical address passed to pmap_enter(). 221 */ 222 #define PMAP_TYPE_SRMMU 0x78 /* mask to get 4m page type */ 223 #define PMAP_PTESHFT_SRMMU 25 /* right shift to put type in pte */ 224 #define PMAP_SHFT_SRMMU 3 /* left shift to extract iospace */ 225 #define PMAP_TNC_SRMMU 127 /* mask to get PG_TYPE & PG_NC */ 226 227 /*#define PMAP_IOC 0x00800000 -* IO cacheable, NOT shifted */ 228 229 #define PMAP_T2PTE_SRMMU(x) (((x) & PMAP_TYPE_SRMMU) << PMAP_PTESHFT_SRMMU) 230 #define PMAP_IOENC_SRMMU(io) ((io) << PMAP_SHFT_SRMMU) 231 232 /* Encode IO space for pmap_enter() */ 233 #define PMAP_IOENC(io) (CPU_ISSUN4M ? PMAP_IOENC_SRMMU(io) : PMAP_IOENC_4(io)) 234 235 int pmap_dumpsize __P((void)); 236 int pmap_dumpmmu __P((int (*)__P((dev_t, daddr_t, caddr_t, size_t)), 237 daddr_t)); 238 239 #define pmap_kernel() (&kernel_pmap_store) 240 #define pmap_resident_count(pmap) pmap_count_ptes(pmap) 241 242 #define PMAP_PREFER(fo, ap) pmap_prefer((fo), (ap)) 243 244 #define PMAP_EXCLUDE_DECLS /* tells MI pmap.h *not* to include decls */ 245 246 /* FUNCTION DECLARATIONS FOR COMMON PMAP MODULE */ 247 248 struct proc; 249 void pmap_activate __P((struct proc *)); 250 void pmap_deactivate __P((struct proc *)); 251 void pmap_bootstrap __P((int nmmu, int nctx, int nregion)); 252 int pmap_count_ptes __P((struct pmap *)); 253 void pmap_prefer __P((vaddr_t, vaddr_t *)); 254 int pmap_pa_exists __P((paddr_t)); 255 void pmap_unwire __P((pmap_t, vaddr_t)); 256 void pmap_collect __P((pmap_t)); 257 void pmap_copy __P((pmap_t, pmap_t, vaddr_t, vsize_t, vaddr_t)); 258 pmap_t pmap_create __P((void)); 259 void pmap_destroy __P((pmap_t)); 260 void pmap_init __P((void)); 261 vaddr_t pmap_map __P((vaddr_t, paddr_t, paddr_t, int)); 262 paddr_t pmap_phys_address __P((int)); 263 void pmap_reference __P((pmap_t)); 264 void pmap_remove __P((pmap_t, vaddr_t, vaddr_t)); 265 #define pmap_update(pmap) /* nothing (yet) */ 266 void pmap_virtual_space __P((vaddr_t *, vaddr_t *)); 267 void pmap_redzone __P((void)); 268 void kvm_uncache __P((caddr_t, int)); 269 struct user; 270 int mmu_pagein __P((struct pmap *pm, vaddr_t, int)); 271 void pmap_writetext __P((unsigned char *, int)); 272 void pmap_globalize_boot_cpuinfo __P((struct cpu_info *)); 273 274 275 /* SUN4/SUN4C SPECIFIC DECLARATIONS */ 276 277 #if defined(SUN4) || defined(SUN4C) 278 boolean_t pmap_clear_modify4_4c __P((struct vm_page *)); 279 boolean_t pmap_clear_reference4_4c __P((struct vm_page *)); 280 void pmap_copy_page4_4c __P((paddr_t, paddr_t)); 281 int pmap_enter4_4c __P((pmap_t, vaddr_t, paddr_t, vm_prot_t, 282 int)); 283 boolean_t pmap_extract4_4c __P((pmap_t, vaddr_t, paddr_t *)); 284 boolean_t pmap_is_modified4_4c __P((struct vm_page *)); 285 boolean_t pmap_is_referenced4_4c __P((struct vm_page *)); 286 void pmap_kenter_pa4_4c __P((vaddr_t, paddr_t, vm_prot_t)); 287 void pmap_kremove4_4c __P((vaddr_t, vsize_t)); 288 void pmap_page_protect4_4c __P((struct vm_page *, vm_prot_t)); 289 void pmap_protect4_4c __P((pmap_t, vaddr_t, vaddr_t, vm_prot_t)); 290 void pmap_zero_page4_4c __P((paddr_t)); 291 void pmap_changeprot4_4c __P((pmap_t, vaddr_t, vm_prot_t, int)); 292 293 #endif 294 295 /* SIMILAR DECLARATIONS FOR SUN4M MODULE */ 296 297 #if defined(SUN4M) 298 boolean_t pmap_clear_modify4m __P((struct vm_page *)); 299 boolean_t pmap_clear_reference4m __P((struct vm_page *)); 300 void pmap_copy_page4m __P((paddr_t, paddr_t)); 301 void pmap_copy_page_viking_mxcc(paddr_t, paddr_t); 302 void pmap_copy_page_hypersparc(paddr_t, paddr_t); 303 int pmap_enter4m __P((pmap_t, vaddr_t, paddr_t, vm_prot_t, 304 int)); 305 boolean_t pmap_extract4m __P((pmap_t, vaddr_t, paddr_t *)); 306 boolean_t pmap_is_modified4m __P((struct vm_page *)); 307 boolean_t pmap_is_referenced4m __P((struct vm_page *)); 308 void pmap_kenter_pa4m __P((vaddr_t, paddr_t, vm_prot_t)); 309 void pmap_kremove4m __P((vaddr_t, vsize_t)); 310 void pmap_page_protect4m __P((struct vm_page *, vm_prot_t)); 311 void pmap_protect4m __P((pmap_t, vaddr_t, vaddr_t, vm_prot_t)); 312 void pmap_zero_page4m __P((paddr_t)); 313 void pmap_zero_page_viking_mxcc(paddr_t); 314 void pmap_zero_page_hypersparc(paddr_t); 315 void pmap_changeprot4m __P((pmap_t, vaddr_t, vm_prot_t, int)); 316 317 #endif /* defined SUN4M */ 318 319 #if !defined(SUN4M) && (defined(SUN4) || defined(SUN4C)) 320 321 #define pmap_clear_modify pmap_clear_modify4_4c 322 #define pmap_clear_reference pmap_clear_reference4_4c 323 #define pmap_enter pmap_enter4_4c 324 #define pmap_extract pmap_extract4_4c 325 #define pmap_is_modified pmap_is_modified4_4c 326 #define pmap_is_referenced pmap_is_referenced4_4c 327 #define pmap_kenter_pa pmap_kenter_pa4_4c 328 #define pmap_kremove pmap_kremove4_4c 329 #define pmap_page_protect pmap_page_protect4_4c 330 #define pmap_protect pmap_protect4_4c 331 #define pmap_changeprot pmap_changeprot4_4c 332 333 #elif defined(SUN4M) && !(defined(SUN4) || defined(SUN4C)) 334 335 #define pmap_clear_modify pmap_clear_modify4m 336 #define pmap_clear_reference pmap_clear_reference4m 337 #define pmap_enter pmap_enter4m 338 #define pmap_extract pmap_extract4m 339 #define pmap_is_modified pmap_is_modified4m 340 #define pmap_is_referenced pmap_is_referenced4m 341 #define pmap_kenter_pa pmap_kenter_pa4m 342 #define pmap_kremove pmap_kremove4m 343 #define pmap_page_protect pmap_page_protect4m 344 #define pmap_protect pmap_protect4m 345 #define pmap_changeprot pmap_changeprot4m 346 347 #else /* must use function pointers */ 348 349 extern boolean_t(*pmap_clear_modify_p) __P((struct vm_page *)); 350 extern boolean_t(*pmap_clear_reference_p) __P((struct vm_page *)); 351 extern int (*pmap_enter_p) __P((pmap_t, vaddr_t, paddr_t, vm_prot_t, 352 int)); 353 extern boolean_t (*pmap_extract_p) __P((pmap_t, vaddr_t, paddr_t *)); 354 extern boolean_t(*pmap_is_modified_p) __P((struct vm_page *)); 355 extern boolean_t(*pmap_is_referenced_p) __P((struct vm_page *)); 356 extern void (*pmap_kenter_pa_p) __P((vaddr_t, paddr_t, vm_prot_t)); 357 extern void (*pmap_kremove_p) __P((vaddr_t, vsize_t)); 358 extern void (*pmap_page_protect_p) __P((struct vm_page *, vm_prot_t)); 359 extern void (*pmap_protect_p) __P((pmap_t, vaddr_t, vaddr_t, vm_prot_t)); 360 extern void (*pmap_changeprot_p) __P((pmap_t, vaddr_t, vm_prot_t, int)); 361 362 #define pmap_clear_modify (*pmap_clear_modify_p) 363 #define pmap_clear_reference (*pmap_clear_reference_p) 364 #define pmap_enter (*pmap_enter_p) 365 #define pmap_extract (*pmap_extract_p) 366 #define pmap_is_modified (*pmap_is_modified_p) 367 #define pmap_is_referenced (*pmap_is_referenced_p) 368 #define pmap_kenter_pa (*pmap_kenter_pa_p) 369 #define pmap_kremove (*pmap_kremove_p) 370 #define pmap_page_protect (*pmap_page_protect_p) 371 #define pmap_protect (*pmap_protect_p) 372 #define pmap_changeprot (*pmap_changeprot_p) 373 374 #endif 375 376 /* pmap_{zero,copy}_page() may be assisted by specialized hardware */ 377 #define pmap_zero_page (*cpuinfo.zero_page) 378 #define pmap_copy_page (*cpuinfo.copy_page) 379 380 #if defined(SUN4M) 381 /* 382 * Macros which implement SRMMU TLB flushing/invalidation 383 */ 384 #define tlb_flush_page_real(va) \ 385 sta(((vaddr_t)(va) & ~0xfff) | ASI_SRMMUFP_L3, ASI_SRMMUFP, 0) 386 387 #define tlb_flush_segment_real(vr, vs) \ 388 sta(((vr)<<RGSHIFT) | ((vs)<<SGSHIFT) | ASI_SRMMUFP_L2, ASI_SRMMUFP,0) 389 390 #define tlb_flush_region_real(vr) \ 391 sta(((vr) << RGSHIFT) | ASI_SRMMUFP_L1, ASI_SRMMUFP, 0) 392 393 #define tlb_flush_context_real() sta(ASI_SRMMUFP_L0, ASI_SRMMUFP, 0) 394 #define tlb_flush_all_real() sta(ASI_SRMMUFP_LN, ASI_SRMMUFP, 0) 395 396 #endif /* SUN4M */ 397 398 #endif /* _KERNEL */ 399 400 #endif /* _SPARC_PMAP_H_ */ 401