1 /* $NetBSD: pmap_motorola.c,v 1.114 2026/11/14 02:43:48 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1991, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * This code is derived from software contributed to Berkeley by 37 * the Systems Programming Group of the University of Utah Computer 38 * Science Department. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. Neither the name of the University nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 * 64 * @(#)pmap.c 8.6 (Berkeley) 5/27/94 65 */ 66 67 /* 68 * Motorola m68k-family physical map management code. 69 * 70 * Supports: 71 * 68020 with 68851 MMU 72 * 68020 with HP MMU 73 * 68030 with on-chip MMU 74 * 68040 with on-chip MMU 75 * 68060 with on-chip MMU 76 * 77 * Notes: 78 * Don't even pay lip service to multiprocessor support. 79 * 80 * We assume TLB entries don't have process tags (except for the 81 * supervisor/user distinction) so we only invalidate TLB entries 82 * when changing mappings for the current (or kernel) pmap. This is 83 * technically not true for the 68851 but we flush the TLB on every 84 * context switch, so it effectively winds up that way. 85 * 86 * Bitwise and/or operations are significantly faster than bitfield 87 * references so we use them when accessing STE/PTEs in the pmap_pte_* 88 * macros. Note also that the two are not always equivalent; e.g.: 89 * (*pte & PG_PROT) [4] != pte->pg_prot [1] 90 * and a couple of routines that deal with protection and wiring take 91 * some shortcuts that assume the and/or definitions. 92 */ 93 94 /* 95 * Manages physical address maps. 96 * 97 * In addition to hardware address maps, this 98 * module is called upon to provide software-use-only 99 * maps which may or may not be stored in the same 100 * form as hardware maps. These pseudo-maps are 101 * used to store intermediate results from copy 102 * operations to and from address spaces. 103 * 104 * Since the information managed by this module is 105 * also stored by the logical address mapping module, 106 * this module may throw away valid virtual-to-physical 107 * mappings at almost any time. However, invalidations 108 * of virtual-to-physical mappings must be done as 109 * requested. 110 * 111 * In order to cope with hardware architectures which 112 * make virtual-to-physical map invalidates expensive, 113 * this module may delay invalidate or reduced protection 114 * operations until such time as they are actually 115 * necessary. This module is given full information as 116 * to which processors are currently using which maps, 117 * and to when physical maps must be made correct. 118 */ 119 120 #include "opt_ddb.h" 121 #include "opt_kgdb.h" 122 #include "opt_m68k_arch.h" 123 124 #include <sys/cdefs.h> 125 __KERNEL_RCSID(0, "$NetBSD: pmap_motorola.c,v 1.114 2026/11/14 02:43:48 thorpej Exp $"); 126 127 #include <sys/param.h> 128 #include <sys/systm.h> 129 #include <sys/proc.h> 130 #include <sys/pool.h> 131 #include <sys/cpu.h> 132 #ifndef __HAVE_M68K_BROKEN_RMC 133 #include <sys/atomic.h> 134 #endif 135 136 #include <machine/pcb.h> 137 138 #include <uvm/uvm.h> 139 #include <uvm/uvm_physseg.h> 140 141 #include <m68k/cacheops.h> 142 #include <m68k/mmu.h> 143 144 #if !defined(M68K_MMU_MOTOROLA) && !defined(M68K_MMU_HP) 145 #error Hit the road, Jack... 146 #endif 147 148 #ifdef DEBUG 149 #define PDB_FOLLOW 0x0001 150 #define PDB_INIT 0x0002 151 #define PDB_ENTER 0x0004 152 #define PDB_REMOVE 0x0008 153 #define PDB_CREATE 0x0010 154 #define PDB_PTPAGE 0x0020 155 #define PDB_CACHE 0x0040 156 #define PDB_BITS 0x0080 157 #define PDB_COLLECT 0x0100 158 #define PDB_PROTECT 0x0200 159 #define PDB_SEGTAB 0x0400 160 #define PDB_MULTIMAP 0x0800 161 #define PDB_PARANOIA 0x2000 162 #define PDB_WIRING 0x4000 163 #define PDB_PVDUMP 0x8000 164 165 int debugmap = 0; 166 int pmapdebug = PDB_PARANOIA; 167 168 #define PMAP_DPRINTF(l, x) if (pmapdebug & (l)) printf x 169 #else /* ! DEBUG */ 170 #define PMAP_DPRINTF(l, x) /* nothing */ 171 #endif /* DEBUG */ 172 173 /* 174 * Get STEs and PTEs for user/kernel address space 175 */ 176 #if defined(M68040) || defined(M68060) 177 #define pmap_ste1(m, v) \ 178 (&((m)->pm_stab[(vaddr_t)(v) >> SG4_SHIFT1])) 179 /* XXX assumes physically contiguous ST pages (if more than one) */ 180 #define pmap_ste2(m, v) \ 181 (&((m)->pm_stab[(st_entry_t *)(*(u_int *)pmap_ste1(m, v) & SG4_ADDR1) \ 182 - (m)->pm_stpa + (((v) & SG4_MASK2) >> SG4_SHIFT2)])) 183 #if defined(M68020) || defined(M68030) 184 #define pmap_ste(m, v) \ 185 (&((m)->pm_stab[(vaddr_t)(v) \ 186 >> (mmutype == MMU_68040 ? SG4_SHIFT1 : SG_ISHIFT)])) 187 #define pmap_ste_v(m, v) \ 188 (mmutype == MMU_68040 \ 189 ? ((*pmap_ste1(m, v) & SG_V) && \ 190 (*pmap_ste2(m, v) & SG_V)) \ 191 : (*pmap_ste(m, v) & SG_V)) 192 #else 193 #define pmap_ste(m, v) \ 194 (&((m)->pm_stab[(vaddr_t)(v) >> SG4_SHIFT1])) 195 #define pmap_ste_v(m, v) \ 196 ((*pmap_ste1(m, v) & SG_V) && (*pmap_ste2(m, v) & SG_V)) 197 #endif 198 #else 199 #define pmap_ste(m, v) (&((m)->pm_stab[(vaddr_t)(v) >> SG_ISHIFT])) 200 #define pmap_ste_v(m, v) (*pmap_ste(m, v) & SG_V) 201 #endif 202 203 #define pmap_pte(m, v) (&((m)->pm_ptab[(vaddr_t)(v) >> PG_SHIFT])) 204 #define pmap_pte_pa(pte) (*(pte) & PG_FRAME) 205 #define pmap_pte_w(pte) (*(pte) & PG_W) 206 #define pmap_pte_ci(pte) (*(pte) & PG_CI) 207 #define pmap_pte_m(pte) (*(pte) & PG_M) 208 #define pmap_pte_u(pte) (*(pte) & PG_U) 209 #define pmap_pte_prot(pte) (*(pte) & PG_PROT) 210 #define pmap_pte_v(pte) (*(pte) & PG_V) 211 212 #define pmap_pte_set_w(pte, v) \ 213 if (v) *(pte) |= PG_W; else *(pte) &= ~PG_W 214 #define pmap_pte_set_prot(pte, v) \ 215 if (v) *(pte) |= PG_PROT; else *(pte) &= ~PG_PROT 216 #define pmap_pte_w_chg(pte, nw) ((nw) ^ pmap_pte_w(pte)) 217 #define pmap_pte_prot_chg(pte, np) ((np) ^ pmap_pte_prot(pte)) 218 219 /* 220 * Given a map and a machine independent protection code, 221 * convert to an m68k protection code. 222 */ 223 #define pte_prot(m, p) (protection_codes[p]) 224 static u_int protection_codes[8]; 225 226 /* 227 * Kernel page table page management. 228 */ 229 struct kpt_page { 230 struct kpt_page *kpt_next; /* link on either used or free list */ 231 vaddr_t kpt_va; /* always valid kernel VA */ 232 paddr_t kpt_pa; /* PA of this page (for speed) */ 233 }; 234 struct kpt_page *kpt_free_list, *kpt_used_list; 235 struct kpt_page *kpt_pages; 236 237 /* 238 * Kernel segment/page table and page table map. 239 * The page table map gives us a level of indirection we need to dynamically 240 * expand the page table. It is essentially a copy of the segment table 241 * with PTEs instead of STEs. All are initialized in locore at boot time. 242 * Sysmap will initially contain VM_KERNEL_PT_PAGES pages of PTEs. 243 * Segtabzero is an empty segment table which all processes share til they 244 * reference something. 245 */ 246 paddr_t Sysseg_pa; 247 st_entry_t *Sysseg; 248 pt_entry_t *Sysmap, *Sysptmap; 249 st_entry_t *Segtabzero, *Segtabzeropa; 250 251 /* # of kernel PT pages (initial only, can grow dynamically) */ 252 #ifndef VM_KERNEL_PT_PAGES 253 #define VM_KERNEL_PT_PAGES ((vsize_t)2) 254 #endif 255 vsize_t Sysptsize = VM_KERNEL_PT_PAGES; 256 257 static struct pmap kernel_pmap_store; 258 struct pmap *const kernel_pmap_ptr = &kernel_pmap_store; 259 struct vm_map *st_map, *pt_map; 260 struct vm_map st_map_store, pt_map_store; 261 262 vaddr_t lwp0uarea; /* lwp0 u-area VA, initialized in bootstrap */ 263 264 paddr_t avail_start; /* PA of first available physical page */ 265 paddr_t avail_end; /* PA of last available physical page */ 266 vaddr_t virtual_avail; /* VA of first avail page (after kernel bss)*/ 267 vaddr_t virtual_end; /* VA of last avail page (end of kernel AS) */ 268 int page_cnt; /* number of pages managed by VM system */ 269 270 bool pmap_initialized = false; /* Has pmap_init completed? */ 271 272 vaddr_t m68k_uptbase = M68K_PTBASE; 273 274 /* Sysmap begins where the managed kernel virtual space ends. */ 275 vaddr_t kernel_virtual_max = SYSMAP_VA; 276 277 struct pv_header { 278 struct pv_entry pvh_first; /* first PV entry */ 279 uint32_t pvh_attrs; /* attributes: 280 bits 0-7: PTE bits 281 bits 8-15: flags */ 282 }; 283 284 #define PVH_CI 0x10 /* all entries are cache-inhibited */ 285 #define PVH_PTPAGE 0x20 /* entry maps a page table page */ 286 287 struct pv_header *pv_table; 288 TAILQ_HEAD(pv_page_list, pv_page) pv_page_freelist; 289 int pv_nfree; 290 291 #ifdef M68K_EC_VAC 292 static u_int pmap_aliasmask; /* separation at which VA aliasing ok */ 293 #endif 294 #if defined(M68040) || defined(M68060) 295 u_int protostfree; /* prototype (default) free ST map */ 296 #endif 297 298 pt_entry_t *caddr1_pte; /* PTE for CADDR1 */ 299 pt_entry_t *caddr2_pte; /* PTE for CADDR2 */ 300 301 struct pool pmap_pmap_pool; /* memory pool for pmap structures */ 302 struct pool pmap_pv_pool; /* memory pool for pv entries */ 303 304 #define pmap_alloc_pv() pool_get(&pmap_pv_pool, PR_NOWAIT) 305 #define pmap_free_pv(pv) pool_put(&pmap_pv_pool, (pv)) 306 307 #define PAGE_IS_MANAGED(pa) (pmap_initialized && uvm_pageismanaged(pa)) 308 309 static inline struct pv_header * 310 pa_to_pvh(paddr_t pa) 311 { 312 uvm_physseg_t bank = 0; /* XXX gcc4 -Wuninitialized */ 313 psize_t pg = 0; 314 315 bank = uvm_physseg_find(atop((pa)), &pg); 316 return &uvm_physseg_get_pmseg(bank)->pvheader[pg]; 317 } 318 319 /* 320 * Internal routines 321 */ 322 void pmap_remove_mapping(pmap_t, vaddr_t, pt_entry_t *, int, 323 struct pv_entry **); 324 bool pmap_testbit(paddr_t, int); 325 bool pmap_changebit(paddr_t, pt_entry_t, pt_entry_t); 326 int pmap_enter_ptpage(pmap_t, vaddr_t, bool); 327 void pmap_ptpage_addref(vaddr_t); 328 int pmap_ptpage_delref(vaddr_t); 329 void pmap_pinit(pmap_t); 330 void pmap_release(pmap_t); 331 332 #ifdef DEBUG 333 void pmap_pvdump(paddr_t); 334 void pmap_check_wiring(const char *, vaddr_t); 335 #endif 336 337 /* pmap_remove_mapping flags */ 338 #define PRM_TFLUSH 0x01 339 #define PRM_CFLUSH 0x02 340 #define PRM_KEEPPTPAGE 0x04 341 342 #define active_pmap(pm) \ 343 ((pm) == pmap_kernel() || (pm) == curproc->p_vmspace->vm_map.pmap) 344 345 #define active_user_pmap(pm) \ 346 (curproc && \ 347 (pm) != pmap_kernel() && (pm) == curproc->p_vmspace->vm_map.pmap) 348 349 static void (*pmap_load_urp_func)(paddr_t); 350 351 /* 352 * pmap_load_urp: 353 * 354 * Load the user root table into the MMU. 355 */ 356 static inline void 357 pmap_load_urp(paddr_t urp) 358 { 359 (*pmap_load_urp_func)(urp); 360 } 361 362 #ifdef M68K_EC_VAC 363 /* 364 * pmap_init_vac: 365 * 366 * Set up virtually-addressed cache information. Only relevant 367 * for the HP MMU. 368 */ 369 void 370 pmap_init_vac(size_t vacsize) 371 { 372 KASSERT(pmap_aliasmask == 0); 373 KASSERT(powerof2(vacsize)); 374 pmap_aliasmask = vacsize - 1; 375 } 376 #endif /* M68K_EC_VAC */ 377 378 /* 379 * PAGE_SIZE should always evaluate to a compile-time constant in this 380 * context. 381 */ 382 __CTASSERT(PAGE_SIZE != 0); 383 384 /* 385 * pmap_bootstrap2: [ INTERFACE ] 386 * 387 * Phase 2 of pmap bootstrap. (Phase 1 is system-specific.) 388 * 389 * Initialize lwp0 uarea after MMU is turned on, using lwp0uarea 390 * variable saved during pmap_bootstrap(). 391 * 392 * Returns the new kernel %sp value for lwp0. 393 */ 394 void * 395 pmap_bootstrap2(void) 396 { 397 398 uvmexp.pagesize = PAGE_SIZE; 399 uvm_md_init(); 400 401 /* 402 * Initialize protection array. 403 * XXX: Could this have port specific values? Can't this be static? 404 */ 405 protection_codes[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_NONE] = 0; 406 protection_codes[VM_PROT_READ|VM_PROT_NONE|VM_PROT_NONE] = PG_RO; 407 protection_codes[VM_PROT_READ|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO; 408 protection_codes[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO; 409 protection_codes[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW; 410 protection_codes[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW; 411 protection_codes[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW; 412 protection_codes[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW; 413 414 /* 415 * Initialize pmap_kernel(). 416 */ 417 pmap_kernel()->pm_stpa = (st_entry_t *)Sysseg_pa; 418 pmap_kernel()->pm_stab = Sysseg; 419 pmap_kernel()->pm_ptab = Sysmap; 420 #if defined(M68040) || defined(M68060) 421 if (mmutype == MMU_68040) 422 pmap_kernel()->pm_stfree = protostfree; 423 #endif 424 pmap_kernel()->pm_count = 1; 425 426 /* 427 * Initialize lwp0 uarea, and curlwp. 428 */ 429 memset((void *)lwp0uarea, 0, USPACE); 430 uvm_lwp_setuarea(&lwp0, lwp0uarea); 431 432 /* Create a fake exception frame so that cpu_lwp_fork() can copy it. */ 433 struct trapframe *tf = (struct trapframe *)(lwp0uarea + USPACE) - 1; 434 tf->tf_sr = PSL_USER; 435 lwp0.l_md.md_regs = (int *)tf; 436 437 /* 438 * Initialize the source/destination control registers for 439 * movs. 440 */ 441 setsfc(FC_USERD); 442 setdfc(FC_USERD); 443 444 return tf; 445 } 446 447 /* 448 * pmap_virtual_space: [ INTERFACE ] 449 * 450 * Report the range of available kernel virtual address 451 * space to the VM system during bootstrap. 452 * 453 * This is only an interface function if we do not use 454 * pmap_steal_memory()! 455 * 456 * Note: no locking is necessary in this function. 457 */ 458 void 459 pmap_virtual_space(vaddr_t *vstartp, vaddr_t *vendp) 460 { 461 462 *vstartp = virtual_avail; 463 *vendp = virtual_end; 464 } 465 466 /* 467 * pmap_init: [ INTERFACE ] 468 * 469 * Initialize the pmap module. Called by vm_init(), to initialize any 470 * structures that the pmap system needs to map virtual memory. 471 * 472 * Note: no locking is necessary in this function. 473 */ 474 void 475 pmap_init(void) 476 { 477 vaddr_t addr, addr2; 478 vsize_t s; 479 struct pv_header *pvh; 480 int rv; 481 int npages; 482 uvm_physseg_t bank; 483 484 PMAP_DPRINTF(PDB_FOLLOW, ("pmap_init()\n")); 485 486 /* 487 * Before we do anything else, initialize the PTE pointers 488 * used by pmap_zero_page() and pmap_copy_page(). 489 */ 490 caddr1_pte = pmap_pte(pmap_kernel(), CADDR1); 491 caddr2_pte = pmap_pte(pmap_kernel(), CADDR2); 492 493 PMAP_DPRINTF(PDB_INIT, 494 ("pmap_init: Sysseg %p, Sysmap %p, Sysptmap %p\n", 495 Sysseg, Sysmap, Sysptmap)); 496 PMAP_DPRINTF(PDB_INIT, 497 (" pstart %lx, pend %lx, vstart %lx, vend %lx\n", 498 avail_start, avail_end, virtual_avail, virtual_end)); 499 500 /* 501 * Allocate memory for random pmap data structures. Includes the 502 * initial segment table, pv_head_table and pmap_attributes. 503 */ 504 for (page_cnt = 0, bank = uvm_physseg_get_first(); 505 uvm_physseg_valid_p(bank); 506 bank = uvm_physseg_get_next(bank)) 507 page_cnt += uvm_physseg_get_end(bank) - uvm_physseg_get_start(bank); 508 s = M68K_STSIZE; /* Segtabzero */ 509 s += page_cnt * sizeof(struct pv_header); /* pv table */ 510 s = round_page(s); 511 addr = uvm_km_alloc(kernel_map, s, 0, UVM_KMF_WIRED | UVM_KMF_ZERO); 512 if (addr == 0) 513 panic("pmap_init: can't allocate data structures"); 514 515 Segtabzero = (st_entry_t *)addr; 516 (void)pmap_extract(pmap_kernel(), addr, 517 (paddr_t *)(void *)&Segtabzeropa); 518 addr += M68K_STSIZE; 519 520 pv_table = (struct pv_header *) addr; 521 addr += page_cnt * sizeof(struct pv_header); 522 523 PMAP_DPRINTF(PDB_INIT, ("pmap_init: %lx bytes: page_cnt %x s0 %p(%p) " 524 "tbl %p\n", 525 s, page_cnt, Segtabzero, Segtabzeropa, 526 pv_table)); 527 528 /* 529 * Now that the pv and attribute tables have been allocated, 530 * assign them to the memory segments. 531 */ 532 pvh = pv_table; 533 for (bank = uvm_physseg_get_first(); 534 uvm_physseg_valid_p(bank); 535 bank = uvm_physseg_get_next(bank)) { 536 npages = uvm_physseg_get_end(bank) - uvm_physseg_get_start(bank); 537 uvm_physseg_get_pmseg(bank)->pvheader = pvh; 538 pvh += npages; 539 } 540 541 /* 542 * Allocate physical memory for kernel PT pages and their management. 543 * We need 1 PT page per possible task plus some slop. 544 */ 545 npages = uimin(atop(M68K_MAX_KPTSIZE), maxproc+16); 546 s = ptoa(npages) + round_page(npages * sizeof(struct kpt_page)); 547 548 /* 549 * Verify that space will be allocated in region for which 550 * we already have kernel PT pages. 551 */ 552 addr = 0; 553 rv = uvm_map(kernel_map, &addr, s, NULL, UVM_UNKNOWN_OFFSET, 0, 554 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE, 555 UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)); 556 if (rv != 0 || (addr + s) >= (vaddr_t)Sysmap) 557 panic("pmap_init: kernel PT too small"); 558 uvm_unmap(kernel_map, addr, addr + s); 559 560 /* 561 * Now allocate the space and link the pages together to 562 * form the KPT free list. 563 */ 564 addr = uvm_km_alloc(kernel_map, s, 0, UVM_KMF_WIRED | UVM_KMF_ZERO); 565 if (addr == 0) 566 panic("pmap_init: cannot allocate KPT free list"); 567 s = ptoa(npages); 568 addr2 = addr + s; 569 kpt_pages = &((struct kpt_page *)addr2)[npages]; 570 kpt_free_list = NULL; 571 do { 572 addr2 -= PAGE_SIZE; 573 (--kpt_pages)->kpt_next = kpt_free_list; 574 kpt_free_list = kpt_pages; 575 kpt_pages->kpt_va = addr2; 576 (void) pmap_extract(pmap_kernel(), addr2, 577 (paddr_t *)&kpt_pages->kpt_pa); 578 } while (addr != addr2); 579 580 PMAP_DPRINTF(PDB_INIT, ("pmap_init: KPT: %ld pages from %lx to %lx\n", 581 atop(s), addr, addr + s)); 582 583 /* 584 * Allocate the segment table map and the page table map. 585 */ 586 s = maxproc * M68K_STSIZE; 587 st_map = uvm_km_suballoc(kernel_map, &addr, &addr2, s, 0, false, 588 &st_map_store); 589 590 addr = m68k_uptbase; 591 if ((M68K_PTMAXSIZE / M68K_MAX_PTSIZE) < maxproc) { 592 s = M68K_PTMAXSIZE; 593 /* 594 * XXX We don't want to hang when we run out of 595 * page tables, so we lower maxproc so that fork() 596 * will fail instead. Note that root could still raise 597 * this value via sysctl(3). 598 */ 599 maxproc = (M68K_PTMAXSIZE / M68K_MAX_PTSIZE); 600 } else 601 s = (maxproc * M68K_MAX_PTSIZE); 602 pt_map = uvm_km_suballoc(kernel_map, &addr, &addr2, s, 0, 603 true, &pt_map_store); 604 605 #if defined(M68040) || defined(M68060) 606 if (mmutype == MMU_68040) { 607 protostfree = ~l2tobm(0); 608 for (rv = MAXUL2SIZE; rv < sizeof(protostfree)*NBBY; rv++) 609 protostfree &= ~l2tobm(rv); 610 } 611 #endif 612 613 /* 614 * Initialize the pmap pools. 615 */ 616 pool_init(&pmap_pmap_pool, sizeof(struct pmap), 0, 0, 0, "pmappl", 617 &pool_allocator_nointr, IPL_NONE); 618 619 /* 620 * Initialize the pv_entry pools. 621 */ 622 pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvpl", 623 &pool_allocator_meta, IPL_NONE); 624 625 /* 626 * Now that this is done, mark the pages shared with the 627 * hardware page table search as non-CCB (actually, as CI). 628 * 629 * XXX Hm. Given that this is in the kernel map, can't we just 630 * use the va's? 631 */ 632 #ifdef M68060 633 #if defined(M68020) || defined(M68030) || defined(M68040) 634 if (cputype == CPU_68060) 635 #endif 636 { 637 struct kpt_page *kptp = kpt_free_list; 638 paddr_t paddr; 639 640 while (kptp) { 641 pmap_changebit(kptp->kpt_pa, PG_CI, 642 (pt_entry_t)~PG_CCB); 643 kptp = kptp->kpt_next; 644 } 645 646 paddr = (paddr_t)Segtabzeropa; 647 while (paddr < (paddr_t)Segtabzeropa + M68K_STSIZE) { 648 pmap_changebit(paddr, PG_CI, 649 (pt_entry_t)~PG_CCB); 650 paddr += PAGE_SIZE; 651 } 652 653 DCIS(); 654 } 655 #endif 656 657 /* 658 * Set up the routine that loads the MMU root table pointer. 659 */ 660 switch (cputype) { 661 #if defined(M68020) 662 case CPU_68020: 663 #ifdef M68K_MMU_MOTOROLA 664 if (mmutype == MMU_68851) { 665 protorp[0] = MMU51_CRP_BITS; 666 pmap_load_urp_func = mmu_load_urp51; 667 } 668 #endif 669 #ifdef M68K_MMU_HP 670 if (mmutype == MMU_HP) { 671 pmap_load_urp_func = mmu_load_urp20hp; 672 } 673 #endif 674 break; 675 #endif /* M68020 */ 676 #if defined(M68030) 677 case CPU_68030: 678 protorp[0] = MMU51_CRP_BITS; 679 pmap_load_urp_func = mmu_load_urp51; 680 break; 681 #endif /* M68030 */ 682 #if defined(M68040) 683 case CPU_68040: 684 pmap_load_urp_func = mmu_load_urp40; 685 break; 686 #endif /* M68040 */ 687 #if defined(M68060) 688 case CPU_68060: 689 pmap_load_urp_func = mmu_load_urp60; 690 break; 691 #endif /* M68060 */ 692 default: 693 break; 694 } 695 if (pmap_load_urp_func == NULL) { 696 panic("pmap_init: No mmu_load_*() for cpu=%d mmu=%d", 697 cputype, mmutype); 698 } 699 700 /* 701 * Now it is safe to enable pv_table recording. 702 */ 703 pmap_initialized = true; 704 } 705 706 /* 707 * pmap_create: [ INTERFACE ] 708 * 709 * Create and return a physical map. 710 * 711 * Note: no locking is necessary in this function. 712 */ 713 pmap_t 714 pmap_create(void) 715 { 716 struct pmap *pmap; 717 718 PMAP_DPRINTF(PDB_FOLLOW|PDB_CREATE, 719 ("pmap_create()\n")); 720 721 pmap = pool_get(&pmap_pmap_pool, PR_WAITOK); 722 memset(pmap, 0, sizeof(*pmap)); 723 pmap_pinit(pmap); 724 return pmap; 725 } 726 727 /* 728 * pmap_pinit: 729 * 730 * Initialize a preallocated and zeroed pmap structure. 731 * 732 * Note: THIS FUNCTION SHOULD BE MOVED INTO pmap_create()! 733 */ 734 void 735 pmap_pinit(struct pmap *pmap) 736 { 737 738 PMAP_DPRINTF(PDB_FOLLOW|PDB_CREATE, 739 ("pmap_pinit(%p)\n", pmap)); 740 741 /* 742 * No need to allocate page table space yet but we do need a 743 * valid segment table. Initially, we point everyone at the 744 * "null" segment table. On the first pmap_enter, a real 745 * segment table will be allocated. 746 */ 747 pmap->pm_stab = Segtabzero; 748 pmap->pm_stpa = Segtabzeropa; 749 #if defined(M68040) || defined(M68060) 750 #if defined(M68020) || defined(M68030) 751 if (mmutype == MMU_68040) 752 #endif 753 pmap->pm_stfree = protostfree; 754 #endif 755 pmap->pm_count = 1; 756 } 757 758 /* 759 * pmap_destroy: [ INTERFACE ] 760 * 761 * Drop the reference count on the specified pmap, releasing 762 * all resources if the reference count drops to zero. 763 */ 764 void 765 pmap_destroy(pmap_t pmap) 766 { 767 int count; 768 769 PMAP_DPRINTF(PDB_FOLLOW, ("pmap_destroy(%p)\n", pmap)); 770 771 #ifdef __HAVE_M68K_BROKEN_RMC 772 count = --pmap->pm_count; 773 #else 774 count = atomic_dec_uint_nv(&pmap->pm_count); 775 #endif 776 if (count == 0) { 777 pmap_release(pmap); 778 pool_put(&pmap_pmap_pool, pmap); 779 } 780 } 781 782 /* 783 * pmap_release: 784 * 785 * Release the resources held by a pmap. 786 * 787 * Note: THIS FUNCTION SHOULD BE MOVED INTO pmap_destroy(). 788 */ 789 void 790 pmap_release(pmap_t pmap) 791 { 792 793 PMAP_DPRINTF(PDB_FOLLOW, ("pmap_release(%p)\n", pmap)); 794 795 #ifdef notdef /* DIAGNOSTIC */ 796 /* count would be 0 from pmap_destroy... */ 797 if (pmap->pm_count != 1) 798 panic("pmap_release count"); 799 #endif 800 801 if (pmap->pm_ptab) { 802 pmap_remove(pmap_kernel(), (vaddr_t)pmap->pm_ptab, 803 (vaddr_t)pmap->pm_ptab + M68K_MAX_PTSIZE); 804 uvm_km_pgremove((vaddr_t)pmap->pm_ptab, 805 (vaddr_t)pmap->pm_ptab + M68K_MAX_PTSIZE); 806 uvm_km_free(pt_map, (vaddr_t)pmap->pm_ptab, 807 M68K_MAX_PTSIZE, UVM_KMF_VAONLY); 808 } 809 KASSERT(pmap->pm_stab == Segtabzero); 810 } 811 812 /* 813 * pmap_reference: [ INTERFACE ] 814 * 815 * Add a reference to the specified pmap. 816 */ 817 void 818 pmap_reference(pmap_t pmap) 819 { 820 PMAP_DPRINTF(PDB_FOLLOW, ("pmap_reference(%p)\n", pmap)); 821 822 #ifdef __HAVE_M68K_BROKEN_RMC 823 pmap->pm_count++; 824 #else 825 atomic_inc_uint(&pmap->pm_count); 826 #endif 827 } 828 829 /* 830 * pmap_activate: [ INTERFACE ] 831 * 832 * Activate the pmap used by the specified process. This includes 833 * reloading the MMU context if the current process, and marking 834 * the pmap in use by the processor. 835 * 836 * Note: we may only use spin locks here, since we are called 837 * by a critical section in cpu_switch()! 838 */ 839 void 840 pmap_activate(struct lwp *l) 841 { 842 pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap; 843 844 PMAP_DPRINTF(PDB_FOLLOW|PDB_SEGTAB, 845 ("pmap_activate(%p)\n", l)); 846 847 KASSERT(l == curlwp); 848 849 /* 850 * Because the kernel has a separate root pointer, we don't 851 * need to activate the kernel pmap. 852 */ 853 if (pmap != pmap_kernel()) { 854 pmap_load_urp((paddr_t)pmap->pm_stpa); 855 } 856 } 857 858 /* 859 * pmap_deactivate: [ INTERFACE ] 860 * 861 * Mark that the pmap used by the specified process is no longer 862 * in use by the processor. 863 * 864 * The comment above pmap_activate() wrt. locking applies here, 865 * as well. 866 */ 867 void 868 pmap_deactivate(struct lwp *l) 869 { 870 871 /* No action necessary in this pmap implementation. */ 872 } 873 874 /* 875 * pmap_remove: [ INTERFACE ] 876 * 877 * Remove the given range of addresses from the specified map. 878 * 879 * It is assumed that the start and end are properly 880 * rounded to the page size. 881 */ 882 void 883 pmap_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva) 884 { 885 vaddr_t nssva; 886 pt_entry_t *pte; 887 int flags; 888 #ifdef M68K_EC_VAC 889 bool firstpage = true, needcflush = false; 890 #endif 891 892 PMAP_DPRINTF(PDB_FOLLOW|PDB_REMOVE|PDB_PROTECT, 893 ("pmap_remove(%p, %lx, %lx)\n", pmap, sva, eva)); 894 895 flags = active_pmap(pmap) ? PRM_TFLUSH : 0; 896 while (sva < eva) { 897 nssva = m68k_trunc_seg(sva) + NBSEG; 898 if (nssva == 0 || nssva > eva) 899 nssva = eva; 900 901 /* 902 * Invalidate every valid mapping within this segment. 903 */ 904 905 pte = pmap_pte(pmap, sva); 906 while (sva < nssva) { 907 908 /* 909 * If this segment is unallocated, 910 * skip to the next segment boundary. 911 */ 912 913 if (!pmap_ste_v(pmap, sva)) { 914 sva = nssva; 915 break; 916 } 917 918 if (pmap_pte_v(pte)) { 919 #ifdef M68K_EC_VAC 920 if (pmap_aliasmask) { 921 922 /* 923 * Purge kernel side of VAC to ensure 924 * we get the correct state of any 925 * hardware maintained bits. 926 */ 927 928 if (firstpage) { 929 DCIS(); 930 } 931 932 /* 933 * Remember if we may need to 934 * flush the VAC due to a non-CI 935 * mapping. 936 */ 937 938 if (!needcflush && !pmap_pte_ci(pte)) 939 needcflush = true; 940 941 } 942 firstpage = false; 943 #endif 944 pmap_remove_mapping(pmap, sva, pte, flags, NULL); 945 } 946 pte++; 947 sva += PAGE_SIZE; 948 } 949 } 950 951 #ifdef M68K_EC_VAC 952 953 /* 954 * Didn't do anything, no need for cache flushes 955 */ 956 957 if (firstpage) 958 return; 959 960 /* 961 * In a couple of cases, we don't need to worry about flushing 962 * the VAC: 963 * 1. if this is a kernel mapping, 964 * we have already done it 965 * 2. if it is a user mapping not for the current process, 966 * it won't be there 967 */ 968 969 if (pmap_aliasmask && !active_user_pmap(pmap)) 970 needcflush = false; 971 if (needcflush) { 972 if (pmap == pmap_kernel()) { 973 DCIS(); 974 } else { 975 DCIU(); 976 } 977 } 978 #endif 979 } 980 981 /* 982 * pmap_page_protect: [ INTERFACE ] 983 * 984 * Lower the permission for all mappings to a given page to 985 * the permissions specified. 986 */ 987 void 988 pmap_page_protect(struct vm_page *pg, vm_prot_t prot) 989 { 990 paddr_t pa = VM_PAGE_TO_PHYS(pg); 991 struct pv_header *pvh; 992 struct pv_entry *pv; 993 pt_entry_t *pte; 994 int s; 995 996 #ifdef DEBUG 997 if ((pmapdebug & (PDB_FOLLOW|PDB_PROTECT)) || 998 (prot == VM_PROT_NONE && (pmapdebug & PDB_REMOVE))) 999 printf("pmap_page_protect(%p, %x)\n", pg, prot); 1000 #endif 1001 1002 switch (prot) { 1003 case VM_PROT_READ|VM_PROT_WRITE: 1004 case VM_PROT_ALL: 1005 return; 1006 1007 /* copy_on_write */ 1008 case VM_PROT_READ: 1009 case VM_PROT_READ|VM_PROT_EXECUTE: 1010 pmap_changebit(pa, PG_RO, ~0); 1011 return; 1012 1013 /* remove_all */ 1014 default: 1015 break; 1016 } 1017 1018 pvh = pa_to_pvh(pa); 1019 pv = &pvh->pvh_first; 1020 s = splvm(); 1021 while (pv->pv_pmap != NULL) { 1022 1023 pte = pmap_pte(pv->pv_pmap, pv->pv_va); 1024 #ifdef DEBUG 1025 if (!pmap_ste_v(pv->pv_pmap, pv->pv_va) || 1026 pmap_pte_pa(pte) != pa) 1027 panic("pmap_page_protect: bad mapping"); 1028 #endif 1029 pmap_remove_mapping(pv->pv_pmap, pv->pv_va, 1030 pte, PRM_TFLUSH|PRM_CFLUSH, NULL); 1031 } 1032 splx(s); 1033 } 1034 1035 /* 1036 * pmap_protect: [ INTERFACE ] 1037 * 1038 * Set the physical protection on the specified range of this map 1039 * as requested. 1040 */ 1041 void 1042 pmap_protect(pmap_t pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot) 1043 { 1044 vaddr_t nssva; 1045 pt_entry_t *pte; 1046 bool firstpage __unused, needtflush; 1047 int isro; 1048 1049 PMAP_DPRINTF(PDB_FOLLOW|PDB_PROTECT, 1050 ("pmap_protect(%p, %lx, %lx, %x)\n", 1051 pmap, sva, eva, prot)); 1052 1053 #ifdef PMAPSTATS 1054 protect_stats.calls++; 1055 #endif 1056 if ((prot & VM_PROT_READ) == VM_PROT_NONE) { 1057 pmap_remove(pmap, sva, eva); 1058 return; 1059 } 1060 isro = pte_prot(pmap, prot); 1061 needtflush = active_pmap(pmap); 1062 firstpage = true; 1063 while (sva < eva) { 1064 nssva = m68k_trunc_seg(sva) + NBSEG; 1065 if (nssva == 0 || nssva > eva) 1066 nssva = eva; 1067 1068 /* 1069 * If VA belongs to an unallocated segment, 1070 * skip to the next segment boundary. 1071 */ 1072 1073 if (!pmap_ste_v(pmap, sva)) { 1074 sva = nssva; 1075 continue; 1076 } 1077 1078 /* 1079 * Change protection on mapping if it is valid and doesn't 1080 * already have the correct protection. 1081 */ 1082 1083 pte = pmap_pte(pmap, sva); 1084 while (sva < nssva) { 1085 if (pmap_pte_v(pte) && pmap_pte_prot_chg(pte, isro)) { 1086 #ifdef M68K_EC_VAC 1087 1088 /* 1089 * Purge kernel side of VAC to ensure we 1090 * get the correct state of any hardware 1091 * maintained bits. 1092 * 1093 * XXX do we need to clear the VAC in 1094 * general to reflect the new protection? 1095 */ 1096 1097 if (firstpage && pmap_aliasmask) 1098 DCIS(); 1099 #endif 1100 1101 #if defined(M68040) || defined(M68060) 1102 1103 /* 1104 * Clear caches if making RO (see section 1105 * "7.3 Cache Coherency" in the manual). 1106 */ 1107 1108 #if defined(M68020) || defined(M68030) 1109 if (isro && mmutype == MMU_68040) 1110 #else 1111 if (isro) 1112 #endif 1113 { 1114 paddr_t pa = pmap_pte_pa(pte); 1115 1116 DCFP(pa); 1117 ICPP(pa); 1118 } 1119 #endif 1120 pmap_pte_set_prot(pte, isro); 1121 if (needtflush) 1122 TBIS(sva); 1123 firstpage = false; 1124 } 1125 pte++; 1126 sva += PAGE_SIZE; 1127 } 1128 } 1129 } 1130 1131 /* 1132 * pmap_enter: [ INTERFACE ] 1133 * 1134 * Insert the given physical page (pa) at 1135 * the specified virtual address (va) in the 1136 * target physical map with the protection requested. 1137 * 1138 * If specified, the page will be wired down, meaning 1139 * that the related pte cannot be reclaimed. 1140 * 1141 * Note: This is the only routine which MAY NOT lazy-evaluate 1142 * or lose information. Thatis, this routine must actually 1143 * insert this page into the given map NOW. 1144 */ 1145 int 1146 pmap_enter(pmap_t pmap, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags) 1147 { 1148 pt_entry_t *pte; 1149 struct pv_entry *opv = NULL; 1150 int npte; 1151 paddr_t opa; 1152 bool cacheable = true; 1153 bool checkpv = true; 1154 bool wired = (flags & PMAP_WIRED) != 0; 1155 bool can_fail = (flags & PMAP_CANFAIL) != 0; 1156 1157 PMAP_DPRINTF(PDB_FOLLOW|PDB_ENTER, 1158 ("pmap_enter(%p, %lx, %lx, %x, %x)\n", 1159 pmap, va, pa, prot, wired)); 1160 1161 #ifdef DIAGNOSTIC 1162 /* 1163 * pmap_enter() should never be used for CADDR1 and CADDR2. 1164 */ 1165 if (pmap == pmap_kernel() && 1166 (va == (vaddr_t)CADDR1 || va == (vaddr_t)CADDR2)) 1167 panic("pmap_enter: used for CADDR1 or CADDR2"); 1168 #endif 1169 1170 /* 1171 * For user mapping, allocate kernel VM resources if necessary. 1172 */ 1173 if (pmap->pm_ptab == NULL) { 1174 pmap->pm_ptab = (pt_entry_t *) 1175 uvm_km_alloc(pt_map, M68K_MAX_PTSIZE, 0, 1176 UVM_KMF_VAONLY | 1177 (can_fail ? UVM_KMF_NOWAIT : UVM_KMF_WAITVA)); 1178 if (pmap->pm_ptab == NULL) 1179 return ENOMEM; 1180 } 1181 1182 /* 1183 * Segment table entry not valid, we need a new PT page 1184 */ 1185 if (!pmap_ste_v(pmap, va)) { 1186 int err = pmap_enter_ptpage(pmap, va, can_fail); 1187 if (err) 1188 return err; 1189 } 1190 1191 pa = m68k_trunc_page(pa); 1192 pte = pmap_pte(pmap, va); 1193 opa = pmap_pte_pa(pte); 1194 1195 PMAP_DPRINTF(PDB_ENTER, ("enter: pte %p, *pte %x\n", pte, *pte)); 1196 1197 /* 1198 * Mapping has not changed, must be protection or wiring change. 1199 */ 1200 if (opa == pa) { 1201 /* 1202 * Wiring change, just update stats. 1203 * We don't worry about wiring PT pages as they remain 1204 * resident as long as there are valid mappings in them. 1205 * Hence, if a user page is wired, the PT page will be also. 1206 */ 1207 if (pmap_pte_w_chg(pte, wired ? PG_W : 0)) { 1208 PMAP_DPRINTF(PDB_ENTER, 1209 ("enter: wiring change -> %x\n", wired)); 1210 if (wired) 1211 pmap->pm_stats.wired_count++; 1212 else 1213 pmap->pm_stats.wired_count--; 1214 } 1215 /* 1216 * Retain cache inhibition status 1217 */ 1218 checkpv = false; 1219 if (pmap_pte_ci(pte)) 1220 cacheable = false; 1221 goto validate; 1222 } 1223 1224 /* 1225 * Mapping has changed, invalidate old range and fall through to 1226 * handle validating new mapping. 1227 */ 1228 if (opa) { 1229 PMAP_DPRINTF(PDB_ENTER, 1230 ("enter: removing old mapping %lx\n", va)); 1231 pmap_remove_mapping(pmap, va, pte, 1232 PRM_TFLUSH|PRM_CFLUSH|PRM_KEEPPTPAGE, &opv); 1233 } 1234 1235 /* 1236 * If this is a new user mapping, increment the wiring count 1237 * on this PT page. PT pages are wired down as long as there 1238 * is a valid mapping in the page. 1239 */ 1240 if (pmap != pmap_kernel()) 1241 pmap_ptpage_addref(trunc_page((vaddr_t)pte)); 1242 1243 /* 1244 * Enter on the PV list if part of our managed memory 1245 * Note that we raise IPL while manipulating pv_table 1246 * since pmap_enter can be called at interrupt time. 1247 */ 1248 if (PAGE_IS_MANAGED(pa)) { 1249 struct pv_header *pvh; 1250 struct pv_entry *pv, *npv; 1251 int s; 1252 1253 pvh = pa_to_pvh(pa); 1254 pv = &pvh->pvh_first; 1255 s = splvm(); 1256 1257 PMAP_DPRINTF(PDB_ENTER, 1258 ("enter: pv at %p: %lx/%p/%p\n", 1259 pv, pv->pv_va, pv->pv_pmap, pv->pv_next)); 1260 /* 1261 * No entries yet, use header as the first entry 1262 */ 1263 if (pv->pv_pmap == NULL) { 1264 pv->pv_va = va; 1265 pv->pv_pmap = pmap; 1266 pv->pv_next = NULL; 1267 pv->pv_ptste = NULL; 1268 pv->pv_ptpmap = NULL; 1269 pvh->pvh_attrs = 0; 1270 } 1271 /* 1272 * There is at least one other VA mapping this page. 1273 * Place this entry after the header. 1274 */ 1275 else { 1276 #ifdef DEBUG 1277 for (npv = pv; npv; npv = npv->pv_next) 1278 if (pmap == npv->pv_pmap && va == npv->pv_va) 1279 panic("pmap_enter: already in pv_tab"); 1280 #endif 1281 if (opv != NULL) { 1282 npv = opv; 1283 opv = NULL; 1284 } else { 1285 npv = pmap_alloc_pv(); 1286 } 1287 KASSERT(npv != NULL); 1288 npv->pv_va = va; 1289 npv->pv_pmap = pmap; 1290 npv->pv_next = pv->pv_next; 1291 npv->pv_ptste = NULL; 1292 npv->pv_ptpmap = NULL; 1293 pv->pv_next = npv; 1294 1295 #ifdef M68K_EC_VAC 1296 1297 /* 1298 * Since there is another logical mapping for the 1299 * same page we may need to cache-inhibit the 1300 * descriptors on those CPUs with external VACs. 1301 * We don't need to CI if: 1302 * 1303 * - No two mappings belong to the same user pmaps. 1304 * Since the cache is flushed on context switches 1305 * there is no problem between user processes. 1306 * 1307 * - Mappings within a single pmap are a certain 1308 * magic distance apart. VAs at these appropriate 1309 * boundaries map to the same cache entries or 1310 * otherwise don't conflict. 1311 * 1312 * To keep it simple, we only check for these special 1313 * cases if there are only two mappings, otherwise we 1314 * punt and always CI. 1315 * 1316 * Note that there are no aliasing problems with the 1317 * on-chip data-cache when the WA bit is set. 1318 */ 1319 1320 if (pmap_aliasmask) { 1321 if (pvh->pvh_attrs & PVH_CI) { 1322 PMAP_DPRINTF(PDB_CACHE, 1323 ("enter: pa %lx already CI'ed\n", 1324 pa)); 1325 checkpv = cacheable = false; 1326 } else if (npv->pv_next || 1327 ((pmap == pv->pv_pmap || 1328 pmap == pmap_kernel() || 1329 pv->pv_pmap == pmap_kernel()) && 1330 ((pv->pv_va & pmap_aliasmask) != 1331 (va & pmap_aliasmask)))) { 1332 PMAP_DPRINTF(PDB_CACHE, 1333 ("enter: pa %lx CI'ing all\n", 1334 pa)); 1335 cacheable = false; 1336 pvh->pvh_attrs |= PVH_CI; 1337 } 1338 } 1339 #endif 1340 } 1341 1342 /* 1343 * Speed pmap_is_referenced() or pmap_is_modified() based 1344 * on the hint provided in access_type. 1345 */ 1346 #ifdef DIAGNOSTIC 1347 if ((flags & VM_PROT_ALL) & ~prot) 1348 panic("pmap_enter: access_type exceeds prot"); 1349 #endif 1350 if (flags & VM_PROT_WRITE) 1351 pvh->pvh_attrs |= (PG_U|PG_M); 1352 else if (flags & VM_PROT_ALL) 1353 pvh->pvh_attrs |= PG_U; 1354 1355 splx(s); 1356 } 1357 /* 1358 * Assumption: if it is not part of our managed memory 1359 * then it must be device memory which may be volatile. 1360 */ 1361 else if (pmap_initialized) { 1362 checkpv = cacheable = false; 1363 } 1364 1365 /* 1366 * Increment counters 1367 */ 1368 pmap->pm_stats.resident_count++; 1369 if (wired) 1370 pmap->pm_stats.wired_count++; 1371 1372 validate: 1373 #ifdef M68K_EC_VAC 1374 /* 1375 * Purge kernel side of VAC to ensure we get correct state 1376 * of HW bits so we don't clobber them. 1377 */ 1378 if (pmap_aliasmask) 1379 DCIS(); 1380 #endif 1381 1382 /* 1383 * Build the new PTE. 1384 */ 1385 1386 npte = pa | pte_prot(pmap, prot) | (*pte & (PG_M|PG_U)) | PG_V; 1387 if (wired) 1388 npte |= PG_W; 1389 if (!checkpv && !cacheable) 1390 #if defined(M68040) || defined(M68060) 1391 #if defined(M68020) || defined(M68030) 1392 npte |= (mmutype == MMU_68040 ? PG_CIN : PG_CI); 1393 #else 1394 npte |= PG_CIN; 1395 #endif 1396 #else 1397 npte |= PG_CI; 1398 #endif 1399 #if defined(M68040) || defined(M68060) 1400 #if defined(M68020) || defined(M68030) 1401 else if (mmutype == MMU_68040 && (npte & (PG_PROT|PG_CI)) == PG_RW) 1402 #else 1403 else if ((npte & (PG_PROT|PG_CI)) == PG_RW) 1404 #endif 1405 npte |= PG_CCB; 1406 #endif 1407 1408 PMAP_DPRINTF(PDB_ENTER, ("enter: new pte value %x\n", npte)); 1409 1410 /* 1411 * Remember if this was a wiring-only change. 1412 * If so, we need not flush the TLB and caches. 1413 */ 1414 1415 wired = ((*pte ^ npte) == PG_W); 1416 #if defined(M68040) || defined(M68060) 1417 #if defined(M68020) || defined(M68030) 1418 if (mmutype == MMU_68040 && !wired) 1419 #else 1420 if (!wired) 1421 #endif 1422 { 1423 DCFP(pa); 1424 ICPP(pa); 1425 } 1426 #endif 1427 *pte = npte; 1428 if (!wired && active_pmap(pmap)) 1429 TBIS(va); 1430 #ifdef M68K_EC_VAC 1431 /* 1432 * The following is executed if we are entering a second 1433 * (or greater) mapping for a physical page and the mappings 1434 * may create an aliasing problem. In this case we must 1435 * cache inhibit the descriptors involved and flush any 1436 * external VAC. 1437 */ 1438 if (checkpv && !cacheable) { 1439 pmap_changebit(pa, PG_CI, ~0); 1440 DCIA(); 1441 #ifdef DEBUG 1442 if ((pmapdebug & (PDB_CACHE|PDB_PVDUMP)) == 1443 (PDB_CACHE|PDB_PVDUMP)) 1444 pmap_pvdump(pa); 1445 #endif 1446 } 1447 #endif 1448 #ifdef DEBUG 1449 if ((pmapdebug & PDB_WIRING) && pmap != pmap_kernel()) 1450 pmap_check_wiring("enter", trunc_page((vaddr_t)pte)); 1451 #endif 1452 1453 if (opv != NULL) 1454 pmap_free_pv(opv); 1455 1456 return 0; 1457 } 1458 1459 void 1460 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags) 1461 { 1462 pmap_t pmap = pmap_kernel(); 1463 pt_entry_t *pte; 1464 int s, npte; 1465 1466 PMAP_DPRINTF(PDB_FOLLOW|PDB_ENTER, 1467 ("pmap_kenter_pa(%lx, %lx, %x)\n", va, pa, prot)); 1468 1469 /* 1470 * Segment table entry not valid, we need a new PT page 1471 */ 1472 1473 if (!pmap_ste_v(pmap, va)) { 1474 s = splvm(); 1475 pmap_enter_ptpage(pmap, va, false); 1476 splx(s); 1477 } 1478 1479 pa = m68k_trunc_page(pa); 1480 pte = pmap_pte(pmap, va); 1481 1482 PMAP_DPRINTF(PDB_ENTER, ("enter: pte %p, *pte %x\n", pte, *pte)); 1483 KASSERT(!pmap_pte_v(pte)); 1484 1485 /* 1486 * Increment counters 1487 */ 1488 1489 pmap->pm_stats.resident_count++; 1490 pmap->pm_stats.wired_count++; 1491 1492 /* 1493 * Build the new PTE. 1494 */ 1495 1496 npte = pa | pte_prot(pmap, prot) | PG_V | PG_W; 1497 if (flags & PMAP_NOCACHE) { 1498 npte |= PG_CI; 1499 } 1500 #if defined(M68040) || defined(M68060) 1501 #if defined(M68020) || defined(M68030) 1502 if (mmutype == MMU_68040 && (npte & (PG_PROT|PG_CI)) == PG_RW) 1503 #else 1504 if ((npte & (PG_PROT|PG_CI)) == PG_RW) 1505 #endif 1506 npte |= PG_CCB; 1507 1508 if (mmutype == MMU_68040) { 1509 DCFP(pa); 1510 ICPP(pa); 1511 } 1512 #endif 1513 1514 *pte = npte; 1515 TBIS(va); 1516 } 1517 1518 void 1519 pmap_kremove(vaddr_t va, vsize_t size) 1520 { 1521 pmap_t pmap = pmap_kernel(); 1522 pt_entry_t *pte; 1523 vaddr_t nssva; 1524 vaddr_t eva = va + size; 1525 #ifdef M68K_EC_VAC 1526 bool firstpage, needcflush; 1527 #endif 1528 1529 PMAP_DPRINTF(PDB_FOLLOW|PDB_REMOVE|PDB_PROTECT, 1530 ("pmap_kremove(%lx, %lx)\n", va, size)); 1531 1532 #ifdef M68K_EC_VAC 1533 firstpage = true; 1534 needcflush = false; 1535 #endif 1536 while (va < eva) { 1537 nssva = m68k_trunc_seg(va) + NBSEG; 1538 if (nssva == 0 || nssva > eva) 1539 nssva = eva; 1540 1541 /* 1542 * If VA belongs to an unallocated segment, 1543 * skip to the next segment boundary. 1544 */ 1545 1546 if (!pmap_ste_v(pmap, va)) { 1547 va = nssva; 1548 continue; 1549 } 1550 1551 /* 1552 * Invalidate every valid mapping within this segment. 1553 */ 1554 1555 pte = pmap_pte(pmap, va); 1556 while (va < nssva) { 1557 if (!pmap_pte_v(pte)) { 1558 pte++; 1559 va += PAGE_SIZE; 1560 continue; 1561 } 1562 #ifdef M68K_EC_VAC 1563 if (pmap_aliasmask) { 1564 1565 /* 1566 * Purge kernel side of VAC to ensure 1567 * we get the correct state of any 1568 * hardware maintained bits. 1569 */ 1570 1571 if (firstpage) { 1572 DCIS(); 1573 firstpage = false; 1574 } 1575 1576 /* 1577 * Remember if we may need to 1578 * flush the VAC. 1579 */ 1580 1581 needcflush = true; 1582 } 1583 #endif 1584 pmap->pm_stats.wired_count--; 1585 pmap->pm_stats.resident_count--; 1586 *pte = PG_NV; 1587 TBIS(va); 1588 pte++; 1589 va += PAGE_SIZE; 1590 } 1591 } 1592 1593 #ifdef M68K_EC_VAC 1594 1595 /* 1596 * In a couple of cases, we don't need to worry about flushing 1597 * the VAC: 1598 * 1. if this is a kernel mapping, 1599 * we have already done it 1600 * 2. if it is a user mapping not for the current process, 1601 * it won't be there 1602 */ 1603 1604 if (pmap_aliasmask && !active_user_pmap(pmap)) 1605 needcflush = false; 1606 if (needcflush) { 1607 if (pmap == pmap_kernel()) { 1608 DCIS(); 1609 } else { 1610 DCIU(); 1611 } 1612 } 1613 #endif 1614 } 1615 1616 /* 1617 * pmap_unwire: [ INTERFACE ] 1618 * 1619 * Clear the wired attribute for a map/virtual-address pair. 1620 * 1621 * The mapping must already exist in the pmap. 1622 */ 1623 void 1624 pmap_unwire(pmap_t pmap, vaddr_t va) 1625 { 1626 pt_entry_t *pte; 1627 1628 PMAP_DPRINTF(PDB_FOLLOW, 1629 ("pmap_unwire(%p, %lx)\n", pmap, va)); 1630 1631 pte = pmap_pte(pmap, va); 1632 1633 /* 1634 * If wiring actually changed (always?) clear the wire bit and 1635 * update the wire count. Note that wiring is not a hardware 1636 * characteristic so there is no need to invalidate the TLB. 1637 */ 1638 1639 if (pmap_pte_w_chg(pte, 0)) { 1640 pmap_pte_set_w(pte, false); 1641 pmap->pm_stats.wired_count--; 1642 } 1643 } 1644 1645 /* 1646 * pmap_extract: [ INTERFACE ] 1647 * 1648 * Extract the physical address associated with the given 1649 * pmap/virtual address pair. 1650 */ 1651 bool 1652 pmap_extract(pmap_t pmap, vaddr_t va, paddr_t *pap) 1653 { 1654 paddr_t pa; 1655 u_int pte; 1656 1657 PMAP_DPRINTF(PDB_FOLLOW, 1658 ("pmap_extract(%p, %lx) -> ", pmap, va)); 1659 1660 if (pmap_ste_v(pmap, va)) { 1661 pte = *(u_int *)pmap_pte(pmap, va); 1662 if (pte) { 1663 pa = (pte & PG_FRAME) | (va & ~PG_FRAME); 1664 if (pap != NULL) 1665 *pap = pa; 1666 #ifdef DEBUG 1667 if (pmapdebug & PDB_FOLLOW) 1668 printf("%lx\n", pa); 1669 #endif 1670 return true; 1671 } 1672 } 1673 #ifdef DEBUG 1674 if (pmapdebug & PDB_FOLLOW) 1675 printf("failed\n"); 1676 #endif 1677 return false; 1678 } 1679 1680 /* 1681 * vtophys: [ INTERFACE-ish ] 1682 * 1683 * Kernel virtual to physical. Use with caution. 1684 */ 1685 paddr_t 1686 vtophys(vaddr_t va) 1687 { 1688 paddr_t pa; 1689 1690 if (pmap_extract(pmap_kernel(), va, &pa)) 1691 return pa; 1692 KASSERT(0); 1693 return (paddr_t) -1; 1694 } 1695 1696 /* 1697 * pmap_copy: [ INTERFACE ] 1698 * 1699 * Copy the mapping range specified by src_addr/len 1700 * from the source map to the range dst_addr/len 1701 * in the destination map. 1702 * 1703 * This routine is only advisory and need not do anything. 1704 */ 1705 void 1706 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vaddr_t dst_addr, vsize_t len, 1707 vaddr_t src_addr) 1708 { 1709 1710 PMAP_DPRINTF(PDB_FOLLOW, 1711 ("pmap_copy(%p, %p, %lx, %lx, %lx)\n", 1712 dst_pmap, src_pmap, dst_addr, len, src_addr)); 1713 } 1714 1715 /* 1716 * pmap_collect1(): 1717 * 1718 * Garbage-collect KPT pages. Helper for the above (bogus) 1719 * pmap_collect(). 1720 * 1721 * Note: THIS SHOULD GO AWAY, AND BE REPLACED WITH A BETTER 1722 * WAY OF HANDLING PT PAGES! 1723 */ 1724 static inline void 1725 pmap_collect1(pmap_t pmap, paddr_t startpa, paddr_t endpa) 1726 { 1727 paddr_t pa; 1728 struct pv_header *pvh; 1729 struct pv_entry *pv; 1730 pt_entry_t *pte; 1731 paddr_t kpa; 1732 #ifdef DEBUG 1733 st_entry_t *ste; 1734 int opmapdebug = 0; 1735 #endif 1736 1737 for (pa = startpa; pa < endpa; pa += PAGE_SIZE) { 1738 struct kpt_page *kpt, **pkpt; 1739 1740 /* 1741 * Locate physical pages which are being used as kernel 1742 * page table pages. 1743 */ 1744 1745 pvh = pa_to_pvh(pa); 1746 pv = &pvh->pvh_first; 1747 if (pv->pv_pmap != pmap_kernel() || 1748 !(pvh->pvh_attrs & PVH_PTPAGE)) 1749 continue; 1750 do { 1751 if (pv->pv_ptste && pv->pv_ptpmap == pmap_kernel()) 1752 break; 1753 } while ((pv = pv->pv_next)); 1754 if (pv == NULL) 1755 continue; 1756 #ifdef DEBUG 1757 if (pv->pv_va < (vaddr_t)Sysmap || 1758 pv->pv_va >= (vaddr_t)Sysmap + M68K_MAX_PTSIZE) { 1759 printf("collect: kernel PT VA out of range\n"); 1760 pmap_pvdump(pa); 1761 continue; 1762 } 1763 #endif 1764 pte = (pt_entry_t *)(pv->pv_va + PAGE_SIZE); 1765 while (--pte >= (pt_entry_t *)pv->pv_va && *pte == PG_NV) 1766 ; 1767 if (pte >= (pt_entry_t *)pv->pv_va) 1768 continue; 1769 1770 #ifdef DEBUG 1771 if (pmapdebug & (PDB_PTPAGE|PDB_COLLECT)) { 1772 printf("collect: freeing KPT page at %lx (ste %x@%p)\n", 1773 pv->pv_va, *pv->pv_ptste, pv->pv_ptste); 1774 opmapdebug = pmapdebug; 1775 pmapdebug |= PDB_PTPAGE; 1776 } 1777 1778 ste = pv->pv_ptste; 1779 #endif 1780 /* 1781 * If all entries were invalid we can remove the page. 1782 * We call pmap_remove_entry to take care of invalidating 1783 * ST and Sysptmap entries. 1784 */ 1785 1786 if (!pmap_extract(pmap, pv->pv_va, &kpa)) { 1787 printf("collect: freeing KPT page at %lx (ste %x@%p)\n", 1788 pv->pv_va, *pv->pv_ptste, pv->pv_ptste); 1789 panic("pmap_collect: mapping not found"); 1790 } 1791 pmap_remove_mapping(pmap, pv->pv_va, NULL, 1792 PRM_TFLUSH|PRM_CFLUSH, NULL); 1793 1794 /* 1795 * Use the physical address to locate the original 1796 * (kmem_alloc assigned) address for the page and put 1797 * that page back on the free list. 1798 */ 1799 1800 for (pkpt = &kpt_used_list, kpt = *pkpt; 1801 kpt != NULL; 1802 pkpt = &kpt->kpt_next, kpt = *pkpt) 1803 if (kpt->kpt_pa == kpa) 1804 break; 1805 #ifdef DEBUG 1806 if (kpt == NULL) 1807 panic("pmap_collect: lost a KPT page"); 1808 if (pmapdebug & (PDB_PTPAGE|PDB_COLLECT)) 1809 printf("collect: %lx (%lx) to free list\n", 1810 kpt->kpt_va, kpa); 1811 #endif 1812 *pkpt = kpt->kpt_next; 1813 kpt->kpt_next = kpt_free_list; 1814 kpt_free_list = kpt; 1815 #ifdef DEBUG 1816 if (pmapdebug & (PDB_PTPAGE|PDB_COLLECT)) 1817 pmapdebug = opmapdebug; 1818 1819 if (*ste != SG_NV) 1820 printf("collect: kernel STE at %p still valid (%x)\n", 1821 ste, *ste); 1822 ste = &Sysptmap[ste - pmap_ste(pmap_kernel(), 0)]; 1823 if (*ste != SG_NV) 1824 printf("collect: kernel PTmap at %p still valid (%x)\n", 1825 ste, *ste); 1826 #endif 1827 } 1828 } 1829 1830 /* 1831 * pmap_collect: 1832 * 1833 * Helper for pmap_enter_ptpage(). 1834 * 1835 * Garbage collects the physical map system for pages which are no 1836 * longer used. Success need not be guaranteed -- that is, there 1837 * may well be pages which are not referenced, but others may be 1838 * collected. 1839 */ 1840 static void 1841 pmap_collect(void) 1842 { 1843 int s; 1844 uvm_physseg_t bank; 1845 1846 /* 1847 * XXX This is very bogus. We should handle kernel PT 1848 * XXX pages much differently. 1849 */ 1850 1851 s = splvm(); 1852 for (bank = uvm_physseg_get_first(); 1853 uvm_physseg_valid_p(bank); 1854 bank = uvm_physseg_get_next(bank)) { 1855 pmap_collect1(pmap_kernel(), ptoa(uvm_physseg_get_start(bank)), 1856 ptoa(uvm_physseg_get_end(bank))); 1857 } 1858 splx(s); 1859 } 1860 1861 /* 1862 * pmap_zero_page: [ INTERFACE ] 1863 * 1864 * Zero the specified (machine independent) page by mapping the page 1865 * into virtual memory and using memset to clear its contents, one 1866 * machine dependent page at a time. 1867 * 1868 * Note: WE DO NOT CURRENTLY LOCK THE TEMPORARY ADDRESSES! 1869 * (Actually, we go to splvm(), and since we don't 1870 * support multiple processors, this is sufficient.) 1871 */ 1872 void 1873 pmap_zero_page(paddr_t phys) 1874 { 1875 int npte; 1876 1877 PMAP_DPRINTF(PDB_FOLLOW, ("pmap_zero_page(%lx)\n", phys)); 1878 1879 npte = phys | PG_V; 1880 #ifdef M68K_EC_VAC 1881 if (pmap_aliasmask) { 1882 1883 /* 1884 * Cache-inhibit the mapping on VAC machines, as we would 1885 * be wasting the cache load. 1886 */ 1887 1888 npte |= PG_CI; 1889 } 1890 #endif 1891 1892 #if defined(M68040) || defined(M68060) 1893 #if defined(M68020) || defined(M68030) 1894 if (mmutype == MMU_68040) 1895 #endif 1896 { 1897 /* 1898 * Set copyback caching on the page; this is required 1899 * for cache consistency (since regular mappings are 1900 * copyback as well). 1901 */ 1902 1903 npte |= PG_CCB; 1904 } 1905 #endif 1906 1907 *caddr1_pte = npte; 1908 TBIS((vaddr_t)CADDR1); 1909 1910 zeropage(CADDR1); 1911 1912 #ifdef DEBUG 1913 *caddr1_pte = PG_NV; 1914 TBIS((vaddr_t)CADDR1); 1915 #endif 1916 } 1917 1918 /* 1919 * pmap_copy_page: [ INTERFACE ] 1920 * 1921 * Copy the specified (machine independent) page by mapping the page 1922 * into virtual memory and using memcpy to copy the page, one machine 1923 * dependent page at a time. 1924 * 1925 * Note: WE DO NOT CURRENTLY LOCK THE TEMPORARY ADDRESSES! 1926 * (Actually, we go to splvm(), and since we don't 1927 * support multiple processors, this is sufficient.) 1928 */ 1929 void 1930 pmap_copy_page(paddr_t src, paddr_t dst) 1931 { 1932 pt_entry_t npte1, npte2; 1933 1934 PMAP_DPRINTF(PDB_FOLLOW, ("pmap_copy_page(%lx, %lx)\n", src, dst)); 1935 1936 npte1 = src | PG_RO | PG_V; 1937 npte2 = dst | PG_V; 1938 #ifdef M68K_EC_VAC 1939 if (pmap_aliasmask) { 1940 1941 /* 1942 * Cache-inhibit the mapping on VAC machines, as we would 1943 * be wasting the cache load. 1944 */ 1945 1946 npte1 |= PG_CI; 1947 npte2 |= PG_CI; 1948 } 1949 #endif 1950 1951 #if defined(M68040) || defined(M68060) 1952 #if defined(M68020) || defined(M68030) 1953 if (mmutype == MMU_68040) 1954 #endif 1955 { 1956 /* 1957 * Set copyback caching on the pages; this is required 1958 * for cache consistency (since regular mappings are 1959 * copyback as well). 1960 */ 1961 1962 npte1 |= PG_CCB; 1963 npte2 |= PG_CCB; 1964 } 1965 #endif 1966 1967 *caddr1_pte = npte1; 1968 TBIS((vaddr_t)CADDR1); 1969 1970 *caddr2_pte = npte2; 1971 TBIS((vaddr_t)CADDR2); 1972 1973 copypage(CADDR1, CADDR2); 1974 1975 #ifdef DEBUG 1976 *caddr1_pte = PG_NV; 1977 TBIS((vaddr_t)CADDR1); 1978 1979 *caddr2_pte = PG_NV; 1980 TBIS((vaddr_t)CADDR2); 1981 #endif 1982 } 1983 1984 /* 1985 * pmap_clear_modify: [ INTERFACE ] 1986 * 1987 * Clear the modify bits on the specified physical page. 1988 */ 1989 bool 1990 pmap_clear_modify(struct vm_page *pg) 1991 { 1992 paddr_t pa = VM_PAGE_TO_PHYS(pg); 1993 1994 PMAP_DPRINTF(PDB_FOLLOW, ("pmap_clear_modify(%p)\n", pg)); 1995 1996 return pmap_changebit(pa, 0, (pt_entry_t)~PG_M); 1997 } 1998 1999 /* 2000 * pmap_clear_reference: [ INTERFACE ] 2001 * 2002 * Clear the reference bit on the specified physical page. 2003 */ 2004 bool 2005 pmap_clear_reference(struct vm_page *pg) 2006 { 2007 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2008 2009 PMAP_DPRINTF(PDB_FOLLOW, ("pmap_clear_reference(%p)\n", pg)); 2010 2011 return pmap_changebit(pa, 0, (pt_entry_t)~PG_U); 2012 } 2013 2014 /* 2015 * pmap_is_referenced: [ INTERFACE ] 2016 * 2017 * Return whether or not the specified physical page is referenced 2018 * by any physical maps. 2019 */ 2020 bool 2021 pmap_is_referenced(struct vm_page *pg) 2022 { 2023 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2024 2025 return pmap_testbit(pa, PG_U); 2026 } 2027 2028 /* 2029 * pmap_is_modified: [ INTERFACE ] 2030 * 2031 * Return whether or not the specified physical page is modified 2032 * by any physical maps. 2033 */ 2034 bool 2035 pmap_is_modified(struct vm_page *pg) 2036 { 2037 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2038 2039 return pmap_testbit(pa, PG_M); 2040 } 2041 2042 /* 2043 * pmap_phys_address: [ INTERFACE ] 2044 * 2045 * Return the physical address corresponding to the specified 2046 * cookie. Used by the device pager to decode a device driver's 2047 * mmap entry point return value. 2048 * 2049 * Note: no locking is necessary in this function. 2050 */ 2051 paddr_t 2052 pmap_phys_address(paddr_t ppn) 2053 { 2054 return m68k_ptob(ppn); 2055 } 2056 2057 #ifdef M68K_EC_VAC 2058 /* 2059 * pmap_prefer: [ INTERFACE ] 2060 * 2061 * Find the first virtual address >= *vap that does not 2062 * cause a virtually-addressed cache alias problem. 2063 */ 2064 void 2065 pmap_prefer(vaddr_t foff, vaddr_t *vap) 2066 { 2067 vaddr_t va; 2068 vsize_t d; 2069 2070 #ifdef M68K_MMU_MOTOROLA 2071 if (pmap_aliasmask) 2072 #endif 2073 { 2074 va = *vap; 2075 d = foff - va; 2076 d &= pmap_aliasmask; 2077 *vap = va + d; 2078 } 2079 } 2080 #endif /* M68K_EC_VAC */ 2081 2082 /* 2083 * Miscellaneous support routines follow 2084 */ 2085 2086 /* 2087 * pmap_remove_mapping: 2088 * 2089 * Invalidate a single page denoted by pmap/va. 2090 * 2091 * If (pte != NULL), it is the already computed PTE for the page. 2092 * 2093 * If (flags & PRM_TFLUSH), we must invalidate any TLB information. 2094 * 2095 * If (flags & PRM_CFLUSH), we must flush/invalidate any cache 2096 * information. 2097 * 2098 * If (flags & PRM_KEEPPTPAGE), we don't free the page table page 2099 * if the reference drops to zero. 2100 */ 2101 /* static */ 2102 void 2103 pmap_remove_mapping(pmap_t pmap, vaddr_t va, pt_entry_t *pte, int flags, 2104 struct pv_entry **opvp) 2105 { 2106 paddr_t pa; 2107 struct pv_header *pvh; 2108 struct pv_entry *pv, *npv, *opv = NULL; 2109 struct pmap *ptpmap; 2110 st_entry_t *ste; 2111 int s, bits; 2112 #ifdef DEBUG 2113 pt_entry_t opte; 2114 #endif 2115 2116 PMAP_DPRINTF(PDB_FOLLOW|PDB_REMOVE|PDB_PROTECT, 2117 ("pmap_remove_mapping(%p, %lx, %p, %x, %p)\n", 2118 pmap, va, pte, flags, opvp)); 2119 2120 /* 2121 * PTE not provided, compute it from pmap and va. 2122 */ 2123 2124 if (pte == NULL) { 2125 pte = pmap_pte(pmap, va); 2126 if (*pte == PG_NV) 2127 return; 2128 } 2129 2130 #ifdef M68K_EC_VAC 2131 if (pmap_aliasmask && (flags & PRM_CFLUSH)) { 2132 2133 /* 2134 * Purge kernel side of VAC to ensure we get the correct 2135 * state of any hardware maintained bits. 2136 */ 2137 2138 DCIS(); 2139 2140 /* 2141 * If this is a non-CI user mapping for the current process, 2142 * flush the VAC. Note that the kernel side was flushed 2143 * above so we don't worry about non-CI kernel mappings. 2144 */ 2145 2146 if (active_user_pmap(pmap) && !pmap_pte_ci(pte)) { 2147 DCIU(); 2148 } 2149 } 2150 #endif 2151 2152 pa = pmap_pte_pa(pte); 2153 #ifdef DEBUG 2154 opte = *pte; 2155 #endif 2156 2157 /* 2158 * Update statistics 2159 */ 2160 2161 if (pmap_pte_w(pte)) 2162 pmap->pm_stats.wired_count--; 2163 pmap->pm_stats.resident_count--; 2164 2165 #if defined(M68040) || defined(M68060) 2166 #if defined(M68020) || defined(M68030) 2167 if (mmutype == MMU_68040) 2168 #endif 2169 if ((flags & PRM_CFLUSH)) { 2170 DCFP(pa); 2171 ICPP(pa); 2172 } 2173 #endif 2174 2175 /* 2176 * Invalidate the PTE after saving the reference modify info. 2177 */ 2178 2179 PMAP_DPRINTF(PDB_REMOVE, ("remove: invalidating pte at %p\n", pte)); 2180 bits = *pte & (PG_U|PG_M); 2181 *pte = PG_NV; 2182 if ((flags & PRM_TFLUSH) && active_pmap(pmap)) 2183 TBIS(va); 2184 2185 /* 2186 * For user mappings decrement the wiring count on 2187 * the PT page. 2188 */ 2189 2190 if (pmap != pmap_kernel()) { 2191 vaddr_t ptpva = trunc_page((vaddr_t)pte); 2192 int refs = pmap_ptpage_delref(ptpva); 2193 #ifdef DEBUG 2194 if (pmapdebug & PDB_WIRING) 2195 pmap_check_wiring("remove", ptpva); 2196 #endif 2197 2198 /* 2199 * If reference count drops to 0, and we're not instructed 2200 * to keep it around, free the PT page. 2201 */ 2202 2203 if (refs == 0 && (flags & PRM_KEEPPTPAGE) == 0) { 2204 #ifdef DIAGNOSTIC 2205 struct pv_header *ptppvh; 2206 struct pv_entry *ptppv; 2207 #endif 2208 paddr_t ptppa; 2209 2210 ptppa = pmap_pte_pa(pmap_pte(pmap_kernel(), ptpva)); 2211 #ifdef DIAGNOSTIC 2212 if (PAGE_IS_MANAGED(ptppa) == 0) 2213 panic("pmap_remove_mapping: unmanaged PT page"); 2214 ptppvh = pa_to_pvh(ptppa); 2215 ptppv = &ptppvh->pvh_first; 2216 if (ptppv->pv_ptste == NULL) 2217 panic("pmap_remove_mapping: ptste == NULL"); 2218 if (ptppv->pv_pmap != pmap_kernel() || 2219 ptppv->pv_va != ptpva || 2220 ptppv->pv_next != NULL) 2221 panic("pmap_remove_mapping: " 2222 "bad PT page pmap %p, va 0x%lx, next %p", 2223 ptppv->pv_pmap, ptppv->pv_va, 2224 ptppv->pv_next); 2225 #endif 2226 pmap_remove_mapping(pmap_kernel(), ptpva, 2227 NULL, PRM_TFLUSH|PRM_CFLUSH, NULL); 2228 rw_enter(uvm_kernel_object->vmobjlock, RW_WRITER); 2229 uvm_pagefree(PHYS_TO_VM_PAGE(ptppa)); 2230 rw_exit(uvm_kernel_object->vmobjlock); 2231 PMAP_DPRINTF(PDB_REMOVE|PDB_PTPAGE, 2232 ("remove: PT page 0x%lx (0x%lx) freed\n", 2233 ptpva, ptppa)); 2234 } 2235 } 2236 2237 /* 2238 * If this isn't a managed page, we are all done. 2239 */ 2240 2241 if (PAGE_IS_MANAGED(pa) == 0) 2242 return; 2243 2244 /* 2245 * Otherwise remove it from the PV table 2246 * (raise IPL since we may be called at interrupt time). 2247 */ 2248 2249 pvh = pa_to_pvh(pa); 2250 pv = &pvh->pvh_first; 2251 ste = NULL; 2252 s = splvm(); 2253 2254 /* 2255 * If it is the first entry on the list, it is actually 2256 * in the header and we must copy the following entry up 2257 * to the header. Otherwise we must search the list for 2258 * the entry. In either case we free the now unused entry. 2259 */ 2260 2261 if (pmap == pv->pv_pmap && va == pv->pv_va) { 2262 ste = pv->pv_ptste; 2263 ptpmap = pv->pv_ptpmap; 2264 npv = pv->pv_next; 2265 if (npv) { 2266 *pv = *npv; 2267 opv = npv; 2268 } else 2269 pv->pv_pmap = NULL; 2270 } else { 2271 for (npv = pv->pv_next; npv; npv = npv->pv_next) { 2272 if (pmap == npv->pv_pmap && va == npv->pv_va) 2273 break; 2274 pv = npv; 2275 } 2276 #ifdef DEBUG 2277 if (npv == NULL) 2278 panic("pmap_remove: PA not in pv_tab"); 2279 #endif 2280 ste = npv->pv_ptste; 2281 ptpmap = npv->pv_ptpmap; 2282 pv->pv_next = npv->pv_next; 2283 opv = npv; 2284 pvh = pa_to_pvh(pa); 2285 pv = &pvh->pvh_first; 2286 } 2287 2288 #ifdef M68K_EC_VAC 2289 2290 /* 2291 * If only one mapping left we no longer need to cache inhibit 2292 */ 2293 2294 if (pmap_aliasmask && 2295 pv->pv_pmap && pv->pv_next == NULL && (pvh->pvh_attrs & PVH_CI)) { 2296 PMAP_DPRINTF(PDB_CACHE, 2297 ("remove: clearing CI for pa %lx\n", pa)); 2298 pvh->pvh_attrs &= ~PVH_CI; 2299 pmap_changebit(pa, 0, (pt_entry_t)~PG_CI); 2300 #ifdef DEBUG 2301 if ((pmapdebug & (PDB_CACHE|PDB_PVDUMP)) == 2302 (PDB_CACHE|PDB_PVDUMP)) 2303 pmap_pvdump(pa); 2304 #endif 2305 } 2306 #endif 2307 2308 /* 2309 * If this was a PT page we must also remove the 2310 * mapping from the associated segment table. 2311 */ 2312 2313 if (ste) { 2314 PMAP_DPRINTF(PDB_REMOVE|PDB_PTPAGE, 2315 ("remove: ste was %x@%p pte was %x@%p\n", 2316 *ste, ste, opte, pmap_pte(pmap, va))); 2317 #if defined(M68040) || defined(M68060) 2318 #if defined(M68020) || defined(M68030) 2319 if (mmutype == MMU_68040) 2320 #endif 2321 { 2322 st_entry_t *este = &ste[NPTEPG/SG4_LEV3SIZE]; 2323 2324 while (ste < este) 2325 *ste++ = SG_NV; 2326 #ifdef DEBUG 2327 ste -= NPTEPG/SG4_LEV3SIZE; 2328 #endif 2329 } 2330 #if defined(M68020) || defined(M68030) 2331 else 2332 #endif 2333 #endif 2334 #if defined(M68020) || defined(M68030) 2335 *ste = SG_NV; 2336 #endif 2337 2338 /* 2339 * If it was a user PT page, we decrement the 2340 * reference count on the segment table as well, 2341 * freeing it if it is now empty. 2342 */ 2343 2344 if (ptpmap != pmap_kernel()) { 2345 PMAP_DPRINTF(PDB_REMOVE|PDB_SEGTAB, 2346 ("remove: stab %p, refcnt %d\n", 2347 ptpmap->pm_stab, ptpmap->pm_sref - 1)); 2348 #ifdef DEBUG 2349 if ((pmapdebug & PDB_PARANOIA) && 2350 ptpmap->pm_stab != 2351 (st_entry_t *)trunc_page((vaddr_t)ste)) 2352 panic("remove: bogus ste"); 2353 #endif 2354 if (--(ptpmap->pm_sref) == 0) { 2355 PMAP_DPRINTF(PDB_REMOVE|PDB_SEGTAB, 2356 ("remove: free stab %p\n", 2357 ptpmap->pm_stab)); 2358 uvm_km_free(st_map, (vaddr_t)ptpmap->pm_stab, 2359 M68K_STSIZE, UVM_KMF_WIRED); 2360 ptpmap->pm_stab = Segtabzero; 2361 ptpmap->pm_stpa = Segtabzeropa; 2362 #if defined(M68040) || defined(M68060) 2363 #if defined(M68020) || defined(M68030) 2364 if (mmutype == MMU_68040) 2365 #endif 2366 ptpmap->pm_stfree = protostfree; 2367 #endif 2368 /* 2369 * Segment table has changed; reload the 2370 * MMU if it's the active user pmap. 2371 */ 2372 if (active_user_pmap(ptpmap)) { 2373 pmap_load_urp((paddr_t)ptpmap->pm_stpa); 2374 } 2375 } 2376 } 2377 pvh->pvh_attrs &= ~PVH_PTPAGE; 2378 ptpmap->pm_ptpages--; 2379 } 2380 2381 /* 2382 * Update saved attributes for managed page 2383 */ 2384 2385 pvh->pvh_attrs |= bits; 2386 splx(s); 2387 2388 if (opvp != NULL) 2389 *opvp = opv; 2390 else if (opv != NULL) 2391 pmap_free_pv(opv); 2392 } 2393 2394 /* 2395 * pmap_testbit: 2396 * 2397 * Test the modified/referenced bits of a physical page. 2398 */ 2399 /* static */ 2400 bool 2401 pmap_testbit(paddr_t pa, int bit) 2402 { 2403 struct pv_header *pvh; 2404 struct pv_entry *pv; 2405 pt_entry_t *pte; 2406 int s; 2407 2408 pvh = pa_to_pvh(pa); 2409 pv = &pvh->pvh_first; 2410 s = splvm(); 2411 2412 /* 2413 * Check saved info first 2414 */ 2415 2416 if (pvh->pvh_attrs & bit) { 2417 splx(s); 2418 return true; 2419 } 2420 2421 #ifdef M68K_EC_VAC 2422 2423 /* 2424 * Flush VAC to get correct state of any hardware maintained bits. 2425 */ 2426 2427 if (pmap_aliasmask && (bit & (PG_U|PG_M))) 2428 DCIS(); 2429 #endif 2430 2431 /* 2432 * Not found. Check current mappings, returning immediately if 2433 * found. Cache a hit to speed future lookups. 2434 */ 2435 2436 if (pv->pv_pmap != NULL) { 2437 for (; pv; pv = pv->pv_next) { 2438 pte = pmap_pte(pv->pv_pmap, pv->pv_va); 2439 if (*pte & bit) { 2440 pvh->pvh_attrs |= bit; 2441 splx(s); 2442 return true; 2443 } 2444 } 2445 } 2446 splx(s); 2447 return false; 2448 } 2449 2450 /* 2451 * pmap_changebit: 2452 * 2453 * Change the modified/referenced bits, or other PTE bits, 2454 * for a physical page. 2455 */ 2456 /* static */ 2457 bool 2458 pmap_changebit(paddr_t pa, pt_entry_t set, pt_entry_t mask) 2459 { 2460 struct pv_header *pvh; 2461 struct pv_entry *pv; 2462 pt_entry_t *pte, npte; 2463 vaddr_t va; 2464 int s; 2465 #if defined(M68K_EC_VAC) || defined(M68040) || defined(M68060) 2466 bool firstpage = true; 2467 #endif 2468 bool r; 2469 2470 PMAP_DPRINTF(PDB_BITS, 2471 ("pmap_changebit(%lx, %x, %x)\n", pa, set, mask)); 2472 2473 pvh = pa_to_pvh(pa); 2474 pv = &pvh->pvh_first; 2475 s = splvm(); 2476 2477 /* 2478 * Clear saved attributes (modify, reference) 2479 */ 2480 2481 r = (pvh->pvh_attrs & ~mask) != 0; 2482 pvh->pvh_attrs &= mask; 2483 2484 /* 2485 * Loop over all current mappings setting/clearing as appropriate 2486 * If setting RO do we need to clear the VAC? 2487 */ 2488 2489 if (pv->pv_pmap != NULL) { 2490 #ifdef DEBUG 2491 int toflush = 0; 2492 #endif 2493 for (; pv; pv = pv->pv_next) { 2494 #ifdef DEBUG 2495 toflush |= (pv->pv_pmap == pmap_kernel()) ? 2 : 1; 2496 #endif 2497 va = pv->pv_va; 2498 pte = pmap_pte(pv->pv_pmap, va); 2499 #ifdef M68K_EC_VAC 2500 2501 /* 2502 * Flush VAC to ensure we get correct state of HW bits 2503 * so we don't clobber them. 2504 */ 2505 2506 if (firstpage && pmap_aliasmask) { 2507 firstpage = false; 2508 DCIS(); 2509 } 2510 #endif 2511 npte = (*pte | set) & mask; 2512 if (*pte != npte) { 2513 r = true; 2514 #if defined(M68040) || defined(M68060) 2515 /* 2516 * If we are changing caching status or 2517 * protection make sure the caches are 2518 * flushed (but only once). 2519 */ 2520 if (firstpage && 2521 #if defined(M68020) || defined(M68030) 2522 (mmutype == MMU_68040) && 2523 #endif 2524 ((set == PG_RO) || 2525 (set & PG_CMASK) || 2526 (mask & PG_CMASK) == 0)) { 2527 firstpage = false; 2528 DCFP(pa); 2529 ICPP(pa); 2530 } 2531 #endif 2532 *pte = npte; 2533 if (active_pmap(pv->pv_pmap)) 2534 TBIS(va); 2535 } 2536 } 2537 } 2538 splx(s); 2539 return r; 2540 } 2541 2542 /* 2543 * pmap_enter_ptpage: 2544 * 2545 * Allocate and map a PT page for the specified pmap/va pair. 2546 */ 2547 /* static */ 2548 int 2549 pmap_enter_ptpage(pmap_t pmap, vaddr_t va, bool can_fail) 2550 { 2551 paddr_t ptpa; 2552 uint64_t ticket; 2553 struct vm_page *pg; 2554 struct pv_header *pvh; 2555 struct pv_entry *pv; 2556 st_entry_t *ste; 2557 int s; 2558 2559 PMAP_DPRINTF(PDB_FOLLOW|PDB_ENTER|PDB_PTPAGE, 2560 ("pmap_enter_ptpage: pmap %p, va %lx\n", pmap, va)); 2561 2562 /* 2563 * Allocate a segment table if necessary. Note that it is allocated 2564 * from a private map and not pt_map. This keeps user page tables 2565 * aligned on segment boundaries in the kernel address space. 2566 * The segment table is wired down. It will be freed whenever the 2567 * reference count drops to zero. 2568 */ 2569 if (pmap->pm_stab == Segtabzero) { 2570 pmap->pm_stab = (st_entry_t *) 2571 uvm_km_alloc(st_map, M68K_STSIZE, 0, 2572 UVM_KMF_WIRED | UVM_KMF_ZERO | 2573 (can_fail ? UVM_KMF_NOWAIT : 0)); 2574 if (pmap->pm_stab == NULL) { 2575 pmap->pm_stab = Segtabzero; 2576 return ENOMEM; 2577 } 2578 (void) pmap_extract(pmap_kernel(), (vaddr_t)pmap->pm_stab, 2579 (paddr_t *)&pmap->pm_stpa); 2580 #if defined(M68040) || defined(M68060) 2581 #if defined(M68020) || defined(M68030) 2582 if (mmutype == MMU_68040) 2583 #endif 2584 { 2585 pt_entry_t *pte; 2586 2587 pte = pmap_pte(pmap_kernel(), pmap->pm_stab); 2588 *pte = (*pte & ~PG_CMASK) | PG_CI; 2589 pmap->pm_stfree = protostfree; 2590 } 2591 #endif 2592 /* 2593 * Segment table has changed; reload the 2594 * MMU if it's the active user pmap. 2595 */ 2596 if (active_user_pmap(pmap)) { 2597 pmap_load_urp((paddr_t)pmap->pm_stpa); 2598 } 2599 2600 PMAP_DPRINTF(PDB_ENTER|PDB_PTPAGE|PDB_SEGTAB, 2601 ("enter: pmap %p stab %p(%p)\n", 2602 pmap, pmap->pm_stab, pmap->pm_stpa)); 2603 } 2604 2605 ste = pmap_ste(pmap, va); 2606 #if defined(M68040) || defined(M68060) 2607 /* 2608 * Allocate level 2 descriptor block if necessary 2609 */ 2610 #if defined(M68020) || defined(M68030) 2611 if (mmutype == MMU_68040) 2612 #endif 2613 { 2614 if (*ste == SG_NV) { 2615 int ix; 2616 void *addr; 2617 2618 ix = bmtol2(pmap->pm_stfree); 2619 if (ix == -1) 2620 panic("enter: out of address space"); /* XXX */ 2621 pmap->pm_stfree &= ~l2tobm(ix); 2622 addr = (void *)&pmap->pm_stab[ix*SG4_LEV2SIZE]; 2623 memset(addr, 0, SG4_LEV2SIZE*sizeof(st_entry_t)); 2624 addr = (void *)&pmap->pm_stpa[ix*SG4_LEV2SIZE]; 2625 *ste = (u_int)addr | SG_RW | SG_U | SG_V; 2626 2627 PMAP_DPRINTF(PDB_ENTER|PDB_PTPAGE|PDB_SEGTAB, 2628 ("enter: alloc ste2 %d(%p)\n", ix, addr)); 2629 } 2630 ste = pmap_ste2(pmap, va); 2631 /* 2632 * Since a level 2 descriptor maps a block of SG4_LEV3SIZE 2633 * level 3 descriptors, we need a chunk of NPTEPG/SG4_LEV3SIZE 2634 * (16) such descriptors (PAGE_SIZE/SG4_LEV3SIZE bytes) to map a 2635 * PT page--the unit of allocation. We set `ste' to point 2636 * to the first entry of that chunk which is validated in its 2637 * entirety below. 2638 */ 2639 ste = (st_entry_t *)((int)ste & ~(PAGE_SIZE/SG4_LEV3SIZE-1)); 2640 2641 PMAP_DPRINTF(PDB_ENTER|PDB_PTPAGE|PDB_SEGTAB, 2642 ("enter: ste2 %p (%p)\n", pmap_ste2(pmap, va), ste)); 2643 } 2644 #endif 2645 va = trunc_page((vaddr_t)pmap_pte(pmap, va)); 2646 2647 /* 2648 * In the kernel we allocate a page from the kernel PT page 2649 * free list and map it into the kernel page table map (via 2650 * pmap_enter). 2651 */ 2652 if (pmap == pmap_kernel()) { 2653 struct kpt_page *kpt; 2654 2655 s = splvm(); 2656 if ((kpt = kpt_free_list) == NULL) { 2657 /* 2658 * No PT pages available. 2659 * Try once to free up unused ones. 2660 */ 2661 PMAP_DPRINTF(PDB_COLLECT, 2662 ("enter: no KPT pages, collecting...\n")); 2663 pmap_collect(); 2664 if ((kpt = kpt_free_list) == NULL) 2665 panic("pmap_enter_ptpage: can't get KPT page"); 2666 } 2667 kpt_free_list = kpt->kpt_next; 2668 kpt->kpt_next = kpt_used_list; 2669 kpt_used_list = kpt; 2670 ptpa = kpt->kpt_pa; 2671 memset((void *)kpt->kpt_va, 0, PAGE_SIZE); 2672 pmap_enter(pmap, va, ptpa, VM_PROT_READ | VM_PROT_WRITE, 2673 VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED); 2674 pmap_update(pmap); 2675 #ifdef DEBUG 2676 if (pmapdebug & (PDB_ENTER|PDB_PTPAGE)) { 2677 int ix = pmap_ste(pmap, va) - pmap_ste(pmap, 0); 2678 2679 printf("enter: add &Sysptmap[%d]: %x (KPT page %lx)\n", 2680 ix, Sysptmap[ix], kpt->kpt_va); 2681 } 2682 #endif 2683 splx(s); 2684 } else { 2685 /* 2686 * For user processes we just allocate a page from the 2687 * VM system. Note that we set the page "wired" count to 1, 2688 * which is what we use to check if the page can be freed. 2689 * See pmap_remove_mapping(). 2690 * 2691 * Count the segment table reference first so that we won't 2692 * lose the segment table when low on memory. 2693 */ 2694 2695 pmap->pm_sref++; 2696 PMAP_DPRINTF(PDB_ENTER|PDB_PTPAGE, 2697 ("enter: about to alloc UPT pg at %lx\n", va)); 2698 rw_enter(uvm_kernel_object->vmobjlock, RW_WRITER); 2699 while (ticket = uvm_wait_prepare(), 2700 (pg = uvm_pagealloc(uvm_kernel_object, 2701 va - vm_map_min(kernel_map), 2702 NULL, UVM_PGA_ZERO)) == NULL) { 2703 rw_exit(uvm_kernel_object->vmobjlock); 2704 if (can_fail) { 2705 pmap->pm_sref--; 2706 return ENOMEM; 2707 } 2708 uvm_wait("ptpage", ticket); 2709 rw_enter(uvm_kernel_object->vmobjlock, RW_WRITER); 2710 } 2711 rw_exit(uvm_kernel_object->vmobjlock); 2712 pg->flags &= ~(PG_BUSY|PG_FAKE); 2713 UVM_PAGE_OWN(pg, NULL); 2714 ptpa = VM_PAGE_TO_PHYS(pg); 2715 pmap_enter(pmap_kernel(), va, ptpa, 2716 VM_PROT_READ | VM_PROT_WRITE, 2717 VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED); 2718 pmap_update(pmap_kernel()); 2719 } 2720 #if defined(M68040) || defined(M68060) 2721 /* 2722 * Turn off copyback caching of page table pages, 2723 * could get ugly otherwise. 2724 */ 2725 #if defined(M68020) || defined(M68030) 2726 if (mmutype == MMU_68040) 2727 #endif 2728 { 2729 #ifdef DEBUG 2730 pt_entry_t *pte = pmap_pte(pmap_kernel(), va); 2731 if ((pmapdebug & PDB_PARANOIA) && (*pte & PG_CCB) == 0) 2732 printf("%s PT no CCB: kva=%lx ptpa=%lx pte@%p=%x\n", 2733 pmap == pmap_kernel() ? "Kernel" : "User", 2734 va, ptpa, pte, *pte); 2735 #endif 2736 if (pmap_changebit(ptpa, PG_CI, (pt_entry_t)~PG_CCB)) 2737 DCIS(); 2738 } 2739 #endif 2740 /* 2741 * Locate the PV entry in the kernel for this PT page and 2742 * record the STE address. This is so that we can invalidate 2743 * the STE when we remove the mapping for the page. 2744 */ 2745 pvh = pa_to_pvh(ptpa); 2746 s = splvm(); 2747 if (pvh) { 2748 pv = &pvh->pvh_first; 2749 pvh->pvh_attrs |= PVH_PTPAGE; 2750 do { 2751 if (pv->pv_pmap == pmap_kernel() && pv->pv_va == va) 2752 break; 2753 } while ((pv = pv->pv_next)); 2754 } else { 2755 pv = NULL; 2756 } 2757 #ifdef DEBUG 2758 if (pv == NULL) 2759 panic("pmap_enter_ptpage: PT page not entered"); 2760 #endif 2761 pv->pv_ptste = ste; 2762 pv->pv_ptpmap = pmap; 2763 2764 PMAP_DPRINTF(PDB_ENTER|PDB_PTPAGE, 2765 ("enter: new PT page at PA %lx, ste at %p\n", ptpa, ste)); 2766 2767 /* 2768 * Map the new PT page into the segment table. 2769 * Also increment the reference count on the segment table if this 2770 * was a user page table page. Note that we don't use vm_map_pageable 2771 * to keep the count like we do for PT pages, this is mostly because 2772 * it would be difficult to identify ST pages in pmap_pageable to 2773 * release them. We also avoid the overhead of vm_map_pageable. 2774 */ 2775 #if defined(M68040) || defined(M68060) 2776 #if defined(M68020) || defined(M68030) 2777 if (mmutype == MMU_68040) 2778 #endif 2779 { 2780 st_entry_t *este; 2781 2782 for (este = &ste[NPTEPG/SG4_LEV3SIZE]; ste < este; ste++) { 2783 *ste = ptpa | SG_U | SG_RW | SG_V; 2784 ptpa += SG4_LEV3SIZE * sizeof(st_entry_t); 2785 } 2786 } 2787 #if defined(M68020) || defined(M68030) 2788 else 2789 *ste = (ptpa & SG_FRAME) | SG_RW | SG_V; 2790 #endif 2791 #else 2792 *ste = (ptpa & SG_FRAME) | SG_RW | SG_V; 2793 #endif 2794 if (pmap != pmap_kernel()) { 2795 PMAP_DPRINTF(PDB_ENTER|PDB_PTPAGE|PDB_SEGTAB, 2796 ("enter: stab %p refcnt %d\n", 2797 pmap->pm_stab, pmap->pm_sref)); 2798 } 2799 /* 2800 * Flush stale TLB info. 2801 */ 2802 if (pmap == pmap_kernel()) 2803 TBIAS(); 2804 else 2805 TBIAU(); 2806 pmap->pm_ptpages++; 2807 splx(s); 2808 2809 return 0; 2810 } 2811 2812 /* 2813 * pmap_ptpage_addref: 2814 * 2815 * Add a reference to the specified PT page. 2816 */ 2817 void 2818 pmap_ptpage_addref(vaddr_t ptpva) 2819 { 2820 struct vm_page *pg; 2821 2822 rw_enter(uvm_kernel_object->vmobjlock, RW_WRITER); 2823 pg = uvm_pagelookup(uvm_kernel_object, ptpva - vm_map_min(kernel_map)); 2824 pg->wire_count++; 2825 PMAP_DPRINTF(PDB_ENTER|PDB_PTPAGE|PDB_SEGTAB, 2826 ("ptpage addref: pg %p now %d\n", 2827 pg, pg->wire_count)); 2828 rw_exit(uvm_kernel_object->vmobjlock); 2829 } 2830 2831 /* 2832 * pmap_ptpage_delref: 2833 * 2834 * Delete a reference to the specified PT page. 2835 */ 2836 int 2837 pmap_ptpage_delref(vaddr_t ptpva) 2838 { 2839 struct vm_page *pg; 2840 int rv; 2841 2842 rw_enter(uvm_kernel_object->vmobjlock, RW_WRITER); 2843 pg = uvm_pagelookup(uvm_kernel_object, ptpva - vm_map_min(kernel_map)); 2844 rv = --pg->wire_count; 2845 PMAP_DPRINTF(PDB_ENTER|PDB_PTPAGE|PDB_SEGTAB, 2846 ("ptpage delref: pg %p now %d\n", 2847 pg, pg->wire_count)); 2848 rw_exit(uvm_kernel_object->vmobjlock); 2849 return rv; 2850 } 2851 2852 /* 2853 * Routine: pmap_procwr 2854 * 2855 * Function: 2856 * Synchronize caches corresponding to [addr, addr + len) in p. 2857 */ 2858 void 2859 pmap_procwr(struct proc *p, vaddr_t va, size_t len) 2860 { 2861 2862 (void)cachectl1(0x80000004, va, len, p); 2863 } 2864 2865 /* 2866 * Routine: pmap_write_text_enter 2867 * 2868 * Function: 2869 * Temporarily map a page of kernel text read-write for the 2870 * kernel debugger. 2871 */ 2872 bool 2873 pmap_write_text_enter(vaddr_t pgva, struct pmap_write_text_context *ctx) 2874 { 2875 volatile pt_entry_t *pte; 2876 pt_entry_t oldpte, tmppte; 2877 2878 #ifdef M68K_MMU_HP 2879 /* 2880 * Flush the supervisor side of the VAC to 2881 * prevent a cache hit on the old, read-only PTE. 2882 */ 2883 if (ectype == EC_VIRT) 2884 DCIS(); 2885 #endif 2886 2887 /* 2888 * N.B. we use the 68851 PTE bit names here, but in 2889 * the case of the kernel text, it all works out vis 2890 * a vis the 68040 PTE bits. 2891 * 2892 * Note the mapping is cache-inhibited to save hair. 2893 */ 2894 pte = kvtopte(pgva); 2895 oldpte = *pte; 2896 if ((oldpte & DT51_PAGE) == 0) { 2897 return false; 2898 } 2899 2900 tmppte = (oldpte & ~PTE51_WP) | PTE51_CI; 2901 *pte = tmppte; 2902 TBIS(pgva); 2903 2904 ctx->pgva = pgva; 2905 ctx->ptep = pte; 2906 ctx->opte = oldpte; 2907 2908 return true; 2909 } 2910 2911 /* 2912 * Routine: pmap_write_text_exit 2913 * 2914 * Function: 2915 * Undo the effects of pmap_write_text_enter(). 2916 */ 2917 void 2918 pmap_write_text_exit(struct pmap_write_text_context *ctx) 2919 { 2920 *ctx->ptep = ctx->opte; 2921 TBIS(ctx->pgva); 2922 } 2923 2924 void 2925 _pmap_set_page_cacheable(pmap_t pmap, vaddr_t va) 2926 { 2927 2928 if (!pmap_ste_v(pmap, va)) 2929 return; 2930 2931 #if defined(M68040) || defined(M68060) 2932 #if defined(M68020) || defined(M68030) 2933 if (mmutype == MMU_68040) { 2934 #endif 2935 if (pmap_changebit(pmap_pte_pa(pmap_pte(pmap, va)), PG_CCB, 2936 (pt_entry_t)~PG_CI)) 2937 DCIS(); 2938 2939 #if defined(M68020) || defined(M68030) 2940 } else 2941 pmap_changebit(pmap_pte_pa(pmap_pte(pmap, va)), 0, 2942 (pt_entry_t)~PG_CI); 2943 #endif 2944 #else 2945 pmap_changebit(pmap_pte_pa(pmap_pte(pmap, va)), 0, 2946 (pt_entry_t)~PG_CI); 2947 #endif 2948 } 2949 2950 void 2951 _pmap_set_page_cacheinhibit(pmap_t pmap, vaddr_t va) 2952 { 2953 2954 if (!pmap_ste_v(pmap, va)) 2955 return; 2956 2957 #if defined(M68040) || defined(M68060) 2958 #if defined(M68020) || defined(M68030) 2959 if (mmutype == MMU_68040) { 2960 #endif 2961 if (pmap_changebit(pmap_pte_pa(pmap_pte(pmap, va)), PG_CI, 2962 (pt_entry_t)~PG_CCB)) 2963 DCIS(); 2964 #if defined(M68020) || defined(M68030) 2965 } else 2966 pmap_changebit(pmap_pte_pa(pmap_pte(pmap, va)), PG_CI, ~0); 2967 #endif 2968 #else 2969 pmap_changebit(pmap_pte_pa(pmap_pte(pmap, va)), PG_CI, ~0); 2970 #endif 2971 } 2972 2973 int 2974 _pmap_page_is_cacheable(pmap_t pmap, vaddr_t va) 2975 { 2976 2977 if (!pmap_ste_v(pmap, va)) 2978 return 0; 2979 2980 return (pmap_pte_ci(pmap_pte(pmap, va)) == 0) ? 1 : 0; 2981 } 2982 2983 vaddr_t kernel_reloc_offset; 2984 2985 /* 2986 * pmap_init_kcore_hdr: 2987 * 2988 * Initialize the m68k kernel crash dump header with information 2989 * necessary to perform KVA -> phys translations. 2990 * 2991 * Returns a pointer to the crash dump RAM segment entries for 2992 * machine-specific code to initialize. 2993 */ 2994 phys_ram_seg_t * 2995 pmap_init_kcore_hdr(cpu_kcore_hdr_t *h) 2996 { 2997 struct m68k_kcore_hdr *m = &h->un._m68k; 2998 extern char end[]; 2999 3000 memset(h, 0, sizeof(*h)); 3001 3002 /* 3003 * Initialize the `dispatcher' portion of the header. 3004 */ 3005 strcpy(h->name, machine); 3006 h->page_size = PAGE_SIZE; 3007 h->kernbase = KERNBASE; 3008 3009 /* 3010 * Fill in information about our MMU configuration. 3011 */ 3012 m->mmutype = mmutype; 3013 m->sg_v = SG_V; 3014 m->sg_frame = SG_FRAME; 3015 m->sg_ishift = SG_ISHIFT; 3016 m->sg_pmask = SG_PMASK; 3017 m->sg40_shift1 = SG4_SHIFT1; 3018 m->sg40_mask2 = SG4_MASK2; 3019 m->sg40_shift2 = SG4_SHIFT2; 3020 m->sg40_mask3 = SG4_MASK3; 3021 m->sg40_shift3 = SG4_SHIFT3; 3022 m->sg40_addr1 = SG4_ADDR1; 3023 m->sg40_addr2 = SG4_ADDR2; 3024 m->pg_v = PG_V; 3025 m->pg_frame = PG_FRAME; 3026 3027 /* 3028 * Initialize pointer to kernel segment table. 3029 */ 3030 m->sysseg_pa = Sysseg_pa; 3031 3032 /* 3033 * Initialize relocation value such that: 3034 * 3035 * pa = (va - KERNBASE) + reloc 3036 */ 3037 m->reloc = kernel_reloc_offset; 3038 3039 /* 3040 * Define the end of the relocatable range. 3041 */ 3042 m->relocend = (uint32_t)end; 3043 3044 return m->ram_segs; 3045 } 3046 3047 #ifdef DEBUG 3048 /* 3049 * pmap_pvdump: 3050 * 3051 * Dump the contents of the PV list for the specified physical page. 3052 */ 3053 void 3054 pmap_pvdump(paddr_t pa) 3055 { 3056 struct pv_header *pvh; 3057 struct pv_entry *pv; 3058 3059 printf("pa %lx", pa); 3060 pvh = pa_to_pvh(pa); 3061 for (pv = &pvh->pvh_first; pv; pv = pv->pv_next) 3062 printf(" -> pmap %p, va %lx, ptste %p, ptpmap %p", 3063 pv->pv_pmap, pv->pv_va, pv->pv_ptste, pv->pv_ptpmap); 3064 printf("\n"); 3065 } 3066 3067 /* 3068 * pmap_check_wiring: 3069 * 3070 * Count the number of valid mappings in the specified PT page, 3071 * and ensure that it is consistent with the number of wirings 3072 * to that page that the VM system has. 3073 */ 3074 void 3075 pmap_check_wiring(const char *str, vaddr_t va) 3076 { 3077 pt_entry_t *pte; 3078 paddr_t pa; 3079 struct vm_page *pg; 3080 int count; 3081 3082 if (!pmap_ste_v(pmap_kernel(), va) || 3083 !pmap_pte_v(pmap_pte(pmap_kernel(), va))) 3084 return; 3085 3086 pa = pmap_pte_pa(pmap_pte(pmap_kernel(), va)); 3087 pg = PHYS_TO_VM_PAGE(pa); 3088 if (pg->wire_count > PAGE_SIZE / sizeof(pt_entry_t)) { 3089 panic("*%s*: 0x%lx: wire count %d", str, va, pg->wire_count); 3090 } 3091 3092 count = 0; 3093 for (pte = (pt_entry_t *)va; pte < (pt_entry_t *)(va + PAGE_SIZE); 3094 pte++) 3095 if (*pte) 3096 count++; 3097 if (pg->wire_count != count) 3098 panic("*%s*: 0x%lx: w%d/a%d", 3099 str, va, pg->wire_count, count); 3100 } 3101 #endif /* DEBUG */ 3102 3103 /* 3104 * XXX XXX XXX These are legacy remants and should go away XXX XXX XXX 3105 * (Cribbed from vm_machdep.c because they're tied to this pmap impl.) 3106 */ 3107 3108 /* 3109 * Map `size' bytes of physical memory starting at `paddr' into 3110 * kernel VA space at `vaddr'. Read/write and cache-inhibit status 3111 * are specified by `prot'. 3112 */ 3113 void 3114 physaccess(void *vaddr, void *paddr, int size, int prot) 3115 { 3116 pt_entry_t *pte; 3117 u_int page; 3118 3119 pte = kvtopte(vaddr); 3120 page = (u_int)paddr & PG_FRAME; 3121 for (size = btoc(size); size; size--) { 3122 *pte++ = PG_V | prot | page; 3123 page += PAGE_SIZE; 3124 } 3125 TBIAS(); 3126 } 3127 3128 void 3129 physunaccess(void *vaddr, int size) 3130 { 3131 pt_entry_t *pte; 3132 3133 pte = kvtopte(vaddr); 3134 for (size = btoc(size); size; size--) 3135 *pte++ = PG_NV; 3136 TBIAS(); 3137 } 3138 3139 /* 3140 * Convert kernel VA to physical address 3141 */ 3142 int 3143 kvtop(void *addr) 3144 { 3145 return (int)vtophys((vaddr_t)addr); 3146 } 3147 3148 /* 3149 * pmap_pa_has_static_mapping: 3150 * 3151 * Returns true if the specified PA (and length) has a static 3152 * mapping, and what that mapping is and its properties. 3153 */ 3154 bool 3155 pmap_pa_has_static_mapping(paddr_t pa, size_t len, vm_prot_t prot, 3156 vaddr_t *vap, int *flagsp) 3157 { 3158 #if defined(M68030) || defined(M68040) || defined(M68060) 3159 /* First, check for Transparent Translation. */ 3160 if (cputype >= CPU_68030 && 3161 mmu_range_is_tt(pa, len, prot, flagsp)) { 3162 *vap = pa; 3163 return true; 3164 } 3165 #endif 3166 3167 const struct pmap_bootmap *pmbm = machine_bootmap; 3168 paddr_t lastpg = m68k_btop(pa + (len - 1)); 3169 paddr_t firstpg = m68k_btop(pa); 3170 paddr_t tfirst, tlast; 3171 vaddr_t tblva; 3172 bool need_write = !!(prot & UVM_PROT_WRITE); 3173 3174 for (; pmbm->pmbm_vaddr != (vaddr_t)-1; pmbm++) { 3175 if (pmbm->pmbm_flags & (PMBM_F_VAONLY|PMBM_F_KEEPOUT)) { 3176 continue; 3177 } 3178 if (pmbm->pmbm_flags & PMBM_F_FIXEDVA) { 3179 tblva = pmbm->pmbm_vaddr; 3180 } else { 3181 tblva = *pmbm->pmbm_vaddr_ptr; 3182 } 3183 tfirst = m68k_btop(pmbm->pmbm_paddr); 3184 tlast = m68k_btop(pmbm->pmbm_paddr + (pmbm->pmbm_size - 1)); 3185 3186 if (firstpg >= tfirst && lastpg <= tlast) { 3187 if (need_write && 3188 (pmbm->pmbm_flags & PMBM_F_RO) != 0) { 3189 return false; 3190 } 3191 *vap = tblva + (pa - pmbm->pmbm_paddr); 3192 if (pmbm->pmbm_flags & PMBM_F_CI) { 3193 *flagsp = PMAP_NOCACHE; 3194 } else { 3195 *flagsp = 0; 3196 } 3197 return true; 3198 } 3199 } 3200 3201 return false; 3202 } 3203 3204 /* 3205 * pmap_va_is_static_mapping: 3206 * 3207 * Returns true if the specified VA (and length) is a static 3208 * mapping. 3209 */ 3210 bool 3211 pmap_va_is_static_mapping(vaddr_t va, size_t len) 3212 { 3213 #if defined(M68030) || defined(M68040) || defined(M68060) 3214 int map_flags; 3215 3216 /* First, check for Transparent Translation. */ 3217 if (cputype >= CPU_68030 && 3218 (mmu_range_is_tt(va, len, UVM_PROT_READ, &map_flags) || 3219 mmu_range_is_tt(va, len, UVM_PROT_WRITE, &map_flags) || 3220 mmu_range_is_tt(va, len, UVM_PROT_EXEC, &map_flags))) { 3221 return true; 3222 } 3223 #endif 3224 3225 const struct pmap_bootmap *pmbm = machine_bootmap; 3226 vaddr_t lastpg = m68k_btop(va + (len - 1)); 3227 vaddr_t firstpg = m68k_btop(va); 3228 vaddr_t tblva, tfirst, tlast; 3229 for (; pmbm->pmbm_vaddr != (vaddr_t)-1; pmbm++) { 3230 if (pmbm->pmbm_flags & (PMBM_F_VAONLY|PMBM_F_KEEPOUT)) { 3231 continue; 3232 } 3233 if (pmbm->pmbm_flags & PMBM_F_FIXEDVA) { 3234 tblva = pmbm->pmbm_vaddr; 3235 } else { 3236 tblva = *pmbm->pmbm_vaddr_ptr; 3237 } 3238 tfirst = m68k_btop(tblva); 3239 tlast = m68k_btop(tblva + (pmbm->pmbm_size - 1)); 3240 3241 if (firstpg >= tfirst && lastpg <= tlast) { 3242 return true; 3243 } 3244 } 3245 3246 return false; 3247 } 3248