pmap.c revision 108157
1/* 2 * Copyright (c) 1991 Regents of the University of California. 3 * All rights reserved. 4 * Copyright (c) 1994 John S. Dyson 5 * All rights reserved. 6 * Copyright (c) 1994 David Greenman 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * the Systems Programming Group of the University of Utah Computer 11 * Science Department and William Jolitz of UUNET Technologies Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91 42 * $FreeBSD: head/sys/sparc64/sparc64/pmap.c 108157 2002-12-21 10:44:30Z jake $ 43 */ 44 45/* 46 * Manages physical address maps. 47 * 48 * In addition to hardware address maps, this module is called upon to 49 * provide software-use-only maps which may or may not be stored in the 50 * same form as hardware maps. These pseudo-maps are used to store 51 * intermediate results from copy operations to and from address spaces. 52 * 53 * Since the information managed by this module is also stored by the 54 * logical address mapping module, this module may throw away valid virtual 55 * to physical mappings at almost any time. However, invalidations of 56 * mappings must be done as requested. 57 * 58 * In order to cope with hardware architectures which make virtual to 59 * physical map invalidates expensive, this module may delay invalidate 60 * reduced protection operations until such time as they are actually 61 * necessary. This module is given full information as to which processors 62 * are currently using which maps, and to when physical maps must be made 63 * correct. 64 */ 65 66#include "opt_msgbuf.h" 67#include "opt_pmap.h" 68 69#include <sys/param.h> 70#include <sys/kernel.h> 71#include <sys/ktr.h> 72#include <sys/lock.h> 73#include <sys/msgbuf.h> 74#include <sys/mutex.h> 75#include <sys/proc.h> 76#include <sys/smp.h> 77#include <sys/sysctl.h> 78#include <sys/systm.h> 79#include <sys/vmmeter.h> 80 81#include <dev/ofw/openfirm.h> 82 83#include <vm/vm.h> 84#include <vm/vm_param.h> 85#include <vm/vm_kern.h> 86#include <vm/vm_page.h> 87#include <vm/vm_map.h> 88#include <vm/vm_object.h> 89#include <vm/vm_extern.h> 90#include <vm/vm_pageout.h> 91#include <vm/vm_pager.h> 92#include <vm/uma.h> 93 94#include <machine/cache.h> 95#include <machine/frame.h> 96#include <machine/instr.h> 97#include <machine/md_var.h> 98#include <machine/metadata.h> 99#include <machine/ofw_mem.h> 100#include <machine/smp.h> 101#include <machine/tlb.h> 102#include <machine/tte.h> 103#include <machine/tsb.h> 104 105#define PMAP_DEBUG 106 107#ifndef PMAP_SHPGPERPROC 108#define PMAP_SHPGPERPROC 200 109#endif 110 111/* 112 * Virtual and physical address of message buffer. 113 */ 114struct msgbuf *msgbufp; 115vm_offset_t msgbuf_phys; 116 117/* 118 * Physical addresses of first and last available physical page. 119 */ 120vm_offset_t avail_start; 121vm_offset_t avail_end; 122 123int pmap_pagedaemon_waken; 124 125/* 126 * Map of physical memory reagions. 127 */ 128vm_offset_t phys_avail[128]; 129static struct ofw_mem_region mra[128]; 130struct ofw_mem_region sparc64_memreg[128]; 131int sparc64_nmemreg; 132static struct ofw_map translations[128]; 133static int translations_size; 134 135/* 136 * First and last available kernel virtual addresses. 137 */ 138vm_offset_t virtual_avail; 139vm_offset_t virtual_end; 140vm_offset_t kernel_vm_end; 141 142vm_offset_t vm_max_kernel_address; 143 144static vm_offset_t crashdumpmap; 145 146/* 147 * Kernel pmap. 148 */ 149struct pmap kernel_pmap_store; 150 151/* 152 * Allocate physical memory for use in pmap_bootstrap. 153 */ 154static vm_offset_t pmap_bootstrap_alloc(vm_size_t size); 155 156static vm_offset_t pmap_map_direct(vm_page_t m); 157 158extern int tl1_immu_miss_patch_1[]; 159extern int tl1_immu_miss_patch_2[]; 160extern int tl1_dmmu_miss_patch_1[]; 161extern int tl1_dmmu_miss_patch_2[]; 162extern int tl1_dmmu_prot_patch_1[]; 163extern int tl1_dmmu_prot_patch_2[]; 164 165/* 166 * If user pmap is processed with pmap_remove and with pmap_remove and the 167 * resident count drops to 0, there are no more pages to remove, so we 168 * need not continue. 169 */ 170#define PMAP_REMOVE_DONE(pm) \ 171 ((pm) != kernel_pmap && (pm)->pm_stats.resident_count == 0) 172 173/* 174 * The threshold (in bytes) above which tsb_foreach() is used in pmap_remove() 175 * and pmap_protect() instead of trying each virtual address. 176 */ 177#define PMAP_TSB_THRESH ((TSB_SIZE / 2) * PAGE_SIZE) 178 179#ifdef PMAP_STATS 180static long pmap_enter_nupdate; 181static long pmap_enter_nreplace; 182static long pmap_enter_nnew; 183static long pmap_ncache_enter; 184static long pmap_ncache_enter_c; 185static long pmap_ncache_enter_cc; 186static long pmap_ncache_enter_nc; 187static long pmap_ncache_remove; 188static long pmap_ncache_remove_c; 189static long pmap_ncache_remove_cc; 190static long pmap_ncache_remove_nc; 191static long pmap_niflush; 192 193SYSCTL_NODE(_debug, OID_AUTO, pmap_stats, CTLFLAG_RD, 0, "Statistics"); 194SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_enter_nupdate, CTLFLAG_RD, 195 &pmap_enter_nupdate, 0, "Number of pmap_enter() updates"); 196SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_enter_nreplace, CTLFLAG_RD, 197 &pmap_enter_nreplace, 0, "Number of pmap_enter() replacements"); 198SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_enter_nnew, CTLFLAG_RD, 199 &pmap_enter_nnew, 0, "Number of pmap_enter() additions"); 200SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_ncache_enter, CTLFLAG_RD, 201 &pmap_ncache_enter, 0, "Number of pmap_cache_enter() calls"); 202SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_ncache_enter_c, CTLFLAG_RD, 203 &pmap_ncache_enter_c, 0, "Number of pmap_cache_enter() cacheable"); 204SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_ncache_enter_cc, CTLFLAG_RD, 205 &pmap_ncache_enter_cc, 0, "Number of pmap_cache_enter() change color"); 206SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_ncache_enter_nc, CTLFLAG_RD, 207 &pmap_ncache_enter_nc, 0, "Number of pmap_cache_enter() noncacheable"); 208SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_ncache_remove, CTLFLAG_RD, 209 &pmap_ncache_remove, 0, "Number of pmap_cache_remove() calls"); 210SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_ncache_remove_c, CTLFLAG_RD, 211 &pmap_ncache_remove_c, 0, "Number of pmap_cache_remove() cacheable"); 212SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_ncache_remove_cc, CTLFLAG_RD, 213 &pmap_ncache_remove_cc, 0, "Number of pmap_cache_remove() change color"); 214SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_ncache_remove_nc, CTLFLAG_RD, 215 &pmap_ncache_remove_nc, 0, "Number of pmap_cache_remove() noncacheable"); 216SYSCTL_LONG(_debug_pmap_stats, OID_AUTO, pmap_niflush, CTLFLAG_RD, 217 &pmap_niflush, 0, "Number of pmap I$ flushes"); 218 219#define PMAP_STATS_INC(var) atomic_add_long(&var, 1) 220#else 221#define PMAP_STATS_INC(var) 222#endif 223 224/* 225 * Quick sort callout for comparing memory regions. 226 */ 227static int mr_cmp(const void *a, const void *b); 228static int om_cmp(const void *a, const void *b); 229static int 230mr_cmp(const void *a, const void *b) 231{ 232 const struct ofw_mem_region *mra; 233 const struct ofw_mem_region *mrb; 234 235 mra = a; 236 mrb = b; 237 if (mra->mr_start < mrb->mr_start) 238 return (-1); 239 else if (mra->mr_start > mrb->mr_start) 240 return (1); 241 else 242 return (0); 243} 244static int 245om_cmp(const void *a, const void *b) 246{ 247 const struct ofw_map *oma; 248 const struct ofw_map *omb; 249 250 oma = a; 251 omb = b; 252 if (oma->om_start < omb->om_start) 253 return (-1); 254 else if (oma->om_start > omb->om_start) 255 return (1); 256 else 257 return (0); 258} 259 260/* 261 * Bootstrap the system enough to run with virtual memory. 262 */ 263void 264pmap_bootstrap(vm_offset_t ekva) 265{ 266 struct pmap *pm; 267 struct tte *tp; 268 vm_offset_t off; 269 vm_offset_t pa; 270 vm_offset_t va; 271 vm_size_t physsz; 272 vm_size_t virtsz; 273 ihandle_t pmem; 274 ihandle_t vmem; 275 int sz; 276 int i; 277 int j; 278 279 /* 280 * Find out what physical memory is available from the prom and 281 * initialize the phys_avail array. This must be done before 282 * pmap_bootstrap_alloc is called. 283 */ 284 if ((pmem = OF_finddevice("/memory")) == -1) 285 panic("pmap_bootstrap: finddevice /memory"); 286 if ((sz = OF_getproplen(pmem, "available")) == -1) 287 panic("pmap_bootstrap: getproplen /memory/available"); 288 if (sizeof(phys_avail) < sz) 289 panic("pmap_bootstrap: phys_avail too small"); 290 if (sizeof(mra) < sz) 291 panic("pmap_bootstrap: mra too small"); 292 bzero(mra, sz); 293 if (OF_getprop(pmem, "available", mra, sz) == -1) 294 panic("pmap_bootstrap: getprop /memory/available"); 295 sz /= sizeof(*mra); 296 CTR0(KTR_PMAP, "pmap_bootstrap: physical memory"); 297 qsort(mra, sz, sizeof (*mra), mr_cmp); 298 physsz = 0; 299 for (i = 0, j = 0; i < sz; i++, j += 2) { 300 CTR2(KTR_PMAP, "start=%#lx size=%#lx", mra[i].mr_start, 301 mra[i].mr_size); 302 phys_avail[j] = mra[i].mr_start; 303 phys_avail[j + 1] = mra[i].mr_start + mra[i].mr_size; 304 physsz += mra[i].mr_size; 305 } 306 physmem = btoc(physsz); 307 308 virtsz = roundup(physsz, PAGE_SIZE_4M << (PAGE_SHIFT - TTE_SHIFT)); 309 vm_max_kernel_address = VM_MIN_KERNEL_ADDRESS + virtsz; 310 tsb_kernel_size = virtsz >> (PAGE_SHIFT - TTE_SHIFT); 311 tsb_kernel_mask = (tsb_kernel_size >> TTE_SHIFT) - 1; 312 313 /* 314 * Get the available physical memory ranges from /memory/reg. These 315 * are only used for kernel dumps, but it may not be wise to do prom 316 * calls in that situation. 317 */ 318 if ((sz = OF_getproplen(pmem, "reg")) == -1) 319 panic("pmap_bootstrap: getproplen /memory/reg"); 320 if (sizeof(sparc64_memreg) < sz) 321 panic("pmap_bootstrap: sparc64_memreg too small"); 322 if (OF_getprop(pmem, "reg", sparc64_memreg, sz) == -1) 323 panic("pmap_bootstrap: getprop /memory/reg"); 324 sparc64_nmemreg = sz / sizeof(*sparc64_memreg); 325 326 /* 327 * Set the start and end of kva. The kernel is loaded at the first 328 * available 4 meg super page, so round up to the end of the page. 329 */ 330 virtual_avail = roundup2(ekva, PAGE_SIZE_4M); 331 virtual_end = vm_max_kernel_address; 332 kernel_vm_end = vm_max_kernel_address; 333 334 /* 335 * Allocate the kernel tsb. 336 */ 337 pa = pmap_bootstrap_alloc(tsb_kernel_size); 338 if (pa & PAGE_MASK_4M) 339 panic("pmap_bootstrap: tsb unaligned\n"); 340 tsb_kernel_phys = pa; 341 tsb_kernel = (struct tte *)virtual_avail; 342 virtual_avail += tsb_kernel_size; 343 344 /* 345 * Patch the virtual address and the tsb mask into the trap table. 346 */ 347 348#define SETHI(rd, imm22) \ 349 (EIF_OP(IOP_FORM2) | EIF_F2_RD(rd) | EIF_F2_OP2(INS0_SETHI) | \ 350 EIF_IMM((imm22) >> 10, 22)) 351#define OR_R_I_R(rd, imm13, rs1) \ 352 (EIF_OP(IOP_MISC) | EIF_F3_RD(rd) | EIF_F3_OP3(INS2_OR) | \ 353 EIF_F3_RS1(rs1) | EIF_F3_I(1) | EIF_IMM(imm13, 13)) 354 355#define PATCH(addr) do { \ 356 if (addr[0] != SETHI(IF_F2_RD(addr[0]), 0x0) || \ 357 addr[1] != OR_R_I_R(IF_F3_RD(addr[1]), 0x0, IF_F3_RS1(addr[1])) || \ 358 addr[2] != SETHI(IF_F2_RD(addr[2]), 0x0)) \ 359 panic("pmap_boostrap: patched instructions have changed"); \ 360 addr[0] |= EIF_IMM((tsb_kernel_mask) >> 10, 22); \ 361 addr[1] |= EIF_IMM(tsb_kernel_mask, 10); \ 362 addr[2] |= EIF_IMM(((vm_offset_t)tsb_kernel) >> 10, 22); \ 363 flush(addr); \ 364 flush(addr + 1); \ 365 flush(addr + 2); \ 366} while (0) 367 368 PATCH(tl1_immu_miss_patch_1); 369 PATCH(tl1_immu_miss_patch_2); 370 PATCH(tl1_dmmu_miss_patch_1); 371 PATCH(tl1_dmmu_miss_patch_2); 372 PATCH(tl1_dmmu_prot_patch_1); 373 PATCH(tl1_dmmu_prot_patch_2); 374 375 /* 376 * Lock it in the tlb. 377 */ 378 pmap_map_tsb(); 379 bzero(tsb_kernel, tsb_kernel_size); 380 381 /* 382 * Enter fake 8k pages for the 4MB kernel pages, so that 383 * pmap_kextract() will work for them. 384 */ 385 for (i = 0; i < kernel_tlb_slots; i++) { 386 pa = kernel_tlbs[i].te_pa; 387 va = kernel_tlbs[i].te_va; 388 for (off = 0; off < PAGE_SIZE_4M; off += PAGE_SIZE) { 389 tp = tsb_kvtotte(va + off); 390 tp->tte_vpn = TV_VPN(va + off, TS_8K); 391 tp->tte_data = TD_V | TD_8K | TD_PA(pa + off) | 392 TD_REF | TD_SW | TD_CP | TD_CV | TD_P | TD_W; 393 } 394 } 395 396 /* 397 * Allocate a kernel stack with guard page for thread0 and map it into 398 * the kernel tsb. 399 */ 400 pa = pmap_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE); 401 kstack0_phys = pa; 402 kstack0 = virtual_avail + (KSTACK_GUARD_PAGES * PAGE_SIZE); 403 virtual_avail += (KSTACK_PAGES + KSTACK_GUARD_PAGES) * PAGE_SIZE; 404 for (i = 0; i < KSTACK_PAGES; i++) { 405 pa = kstack0_phys + i * PAGE_SIZE; 406 va = kstack0 + i * PAGE_SIZE; 407 tp = tsb_kvtotte(va); 408 tp->tte_vpn = TV_VPN(va, TS_8K); 409 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_REF | TD_SW | 410 TD_CP | TD_CV | TD_P | TD_W; 411 } 412 413 /* 414 * Allocate the message buffer. 415 */ 416 msgbuf_phys = pmap_bootstrap_alloc(MSGBUF_SIZE); 417 418 /* 419 * Add the prom mappings to the kernel tsb. 420 */ 421 if ((vmem = OF_finddevice("/virtual-memory")) == -1) 422 panic("pmap_bootstrap: finddevice /virtual-memory"); 423 if ((sz = OF_getproplen(vmem, "translations")) == -1) 424 panic("pmap_bootstrap: getproplen translations"); 425 if (sizeof(translations) < sz) 426 panic("pmap_bootstrap: translations too small"); 427 bzero(translations, sz); 428 if (OF_getprop(vmem, "translations", translations, sz) == -1) 429 panic("pmap_bootstrap: getprop /virtual-memory/translations"); 430 sz /= sizeof(*translations); 431 translations_size = sz; 432 CTR0(KTR_PMAP, "pmap_bootstrap: translations"); 433 qsort(translations, sz, sizeof (*translations), om_cmp); 434 for (i = 0; i < sz; i++) { 435 CTR3(KTR_PMAP, 436 "translation: start=%#lx size=%#lx tte=%#lx", 437 translations[i].om_start, translations[i].om_size, 438 translations[i].om_tte); 439 if (translations[i].om_start < VM_MIN_PROM_ADDRESS || 440 translations[i].om_start > VM_MAX_PROM_ADDRESS) 441 continue; 442 for (off = 0; off < translations[i].om_size; 443 off += PAGE_SIZE) { 444 va = translations[i].om_start + off; 445 tp = tsb_kvtotte(va); 446 tp->tte_vpn = TV_VPN(va, TS_8K); 447 tp->tte_data = 448 ((translations[i].om_tte & 449 ~(TD_SOFT_MASK << TD_SOFT_SHIFT)) | TD_EXEC) + 450 off; 451 } 452 } 453 454 /* 455 * Calculate the first and last available physical addresses. 456 */ 457 avail_start = phys_avail[0]; 458 for (i = 0; phys_avail[i + 2] != 0; i += 2) 459 ; 460 avail_end = phys_avail[i + 1]; 461 Maxmem = sparc64_btop(avail_end); 462 463 /* 464 * Allocate virtual address space for the message buffer. 465 */ 466 msgbufp = (struct msgbuf *)virtual_avail; 467 virtual_avail += round_page(MSGBUF_SIZE); 468 469 /* 470 * Allocate virtual address space to map pages during a kernel dump. 471 */ 472 crashdumpmap = virtual_avail; 473 virtual_avail += MAXDUMPPGS * PAGE_SIZE; 474 475 /* 476 * Initialize the kernel pmap (which is statically allocated). 477 */ 478 pm = kernel_pmap; 479 for (i = 0; i < MAXCPU; i++) 480 pm->pm_context[i] = TLB_CTX_KERNEL; 481 pm->pm_active = ~0; 482 483 /* XXX flush all non-locked tlb entries */ 484} 485 486void 487pmap_map_tsb(void) 488{ 489 vm_offset_t va; 490 vm_offset_t pa; 491 u_long data; 492 u_long s; 493 int i; 494 495 s = intr_disable(); 496 497 /* 498 * Map the 4mb tsb pages. 499 */ 500 for (i = 0; i < tsb_kernel_size; i += PAGE_SIZE_4M) { 501 va = (vm_offset_t)tsb_kernel + i; 502 pa = tsb_kernel_phys + i; 503 /* XXX - cheetah */ 504 data = TD_V | TD_4M | TD_PA(pa) | TD_L | TD_CP | TD_CV | 505 TD_P | TD_W; 506 stxa(AA_DMMU_TAR, ASI_DMMU, TLB_TAR_VA(va) | 507 TLB_TAR_CTX(TLB_CTX_KERNEL)); 508 stxa_sync(0, ASI_DTLB_DATA_IN_REG, data); 509 } 510 511 /* 512 * Set the secondary context to be the kernel context (needed for 513 * fp block operations in the kernel and the cache code). 514 */ 515 stxa(AA_DMMU_SCXR, ASI_DMMU, TLB_CTX_KERNEL); 516 membar(Sync); 517 518 intr_restore(s); 519} 520 521/* 522 * Allocate a physical page of memory directly from the phys_avail map. 523 * Can only be called from pmap_bootstrap before avail start and end are 524 * calculated. 525 */ 526static vm_offset_t 527pmap_bootstrap_alloc(vm_size_t size) 528{ 529 vm_offset_t pa; 530 int i; 531 532 size = round_page(size); 533 for (i = 0; phys_avail[i + 1] != 0; i += 2) { 534 if (phys_avail[i + 1] - phys_avail[i] < size) 535 continue; 536 pa = phys_avail[i]; 537 phys_avail[i] += size; 538 return (pa); 539 } 540 panic("pmap_bootstrap_alloc"); 541} 542 543void 544pmap_context_rollover(void) 545{ 546 u_long data; 547 u_long tag; 548 int i; 549 550 mtx_assert(&sched_lock, MA_OWNED); 551 CTR0(KTR_PMAP, "pmap_context_rollover"); 552 for (i = 0; i < tlb_dtlb_entries; i++) { 553 /* XXX - cheetah */ 554 data = ldxa(TLB_DAR_SLOT(i), ASI_DTLB_DATA_ACCESS_REG); 555 tag = ldxa(TLB_DAR_SLOT(i), ASI_DTLB_TAG_READ_REG); 556 if ((data & TD_V) != 0 && (data & TD_L) == 0 && 557 TLB_TAR_CTX(tag) != TLB_CTX_KERNEL) 558 stxa_sync(TLB_DAR_SLOT(i), ASI_DTLB_DATA_ACCESS_REG, 0); 559 data = ldxa(TLB_DAR_SLOT(i), ASI_ITLB_DATA_ACCESS_REG); 560 tag = ldxa(TLB_DAR_SLOT(i), ASI_ITLB_TAG_READ_REG); 561 if ((data & TD_V) != 0 && (data & TD_L) == 0 && 562 TLB_TAR_CTX(tag) != TLB_CTX_KERNEL) 563 stxa_sync(TLB_DAR_SLOT(i), ASI_ITLB_DATA_ACCESS_REG, 0); 564 } 565 PCPU_SET(tlb_ctx, PCPU_GET(tlb_ctx_min)); 566} 567 568static __inline u_int 569pmap_context_alloc(void) 570{ 571 u_int context; 572 573 mtx_assert(&sched_lock, MA_OWNED); 574 context = PCPU_GET(tlb_ctx); 575 if (context + 1 == PCPU_GET(tlb_ctx_max)) 576 pmap_context_rollover(); 577 else 578 PCPU_SET(tlb_ctx, context + 1); 579 return (context); 580} 581 582/* 583 * Initialize the pmap module. 584 */ 585void 586pmap_init(vm_offset_t phys_start, vm_offset_t phys_end) 587{ 588 vm_offset_t addr; 589 vm_size_t size; 590 int result; 591 int i; 592 593 for (i = 0; i < vm_page_array_size; i++) { 594 vm_page_t m; 595 596 m = &vm_page_array[i]; 597 STAILQ_INIT(&m->md.tte_list); 598 m->md.flags = 0; 599 m->md.color = 0; 600 } 601 602 for (i = 0; i < translations_size; i++) { 603 addr = translations[i].om_start; 604 size = translations[i].om_size; 605 if (addr < VM_MIN_PROM_ADDRESS || addr > VM_MAX_PROM_ADDRESS) 606 continue; 607 result = vm_map_find(kernel_map, NULL, 0, &addr, size, TRUE, 608 VM_PROT_ALL, VM_PROT_ALL, 0); 609 if (result != KERN_SUCCESS || addr != translations[i].om_start) 610 panic("pmap_init: vm_map_find"); 611 } 612} 613 614/* 615 * Initialize the address space (zone) for the pv_entries. Set a 616 * high water mark so that the system can recover from excessive 617 * numbers of pv entries. 618 */ 619void 620pmap_init2(void) 621{ 622} 623 624/* 625 * Extract the physical page address associated with the given 626 * map/virtual_address pair. 627 */ 628vm_offset_t 629pmap_extract(pmap_t pm, vm_offset_t va) 630{ 631 struct tte *tp; 632 633 if (pm == kernel_pmap) 634 return (pmap_kextract(va)); 635 tp = tsb_tte_lookup(pm, va); 636 if (tp == NULL) 637 return (0); 638 else 639 return (TTE_GET_PA(tp) | (va & TTE_GET_PAGE_MASK(tp))); 640} 641 642/* 643 * Extract the physical page address associated with the given kernel virtual 644 * address. 645 */ 646vm_offset_t 647pmap_kextract(vm_offset_t va) 648{ 649 struct tte *tp; 650 651 tp = tsb_kvtotte(va); 652 if ((tp->tte_data & TD_V) == 0) 653 return (0); 654 return (TTE_GET_PA(tp) | (va & TTE_GET_PAGE_MASK(tp))); 655} 656 657int 658pmap_cache_enter(vm_page_t m, vm_offset_t va) 659{ 660 struct tte *tp; 661 int color; 662 663 PMAP_STATS_INC(pmap_ncache_enter); 664 665 /* 666 * Find the color for this virtual address and note the added mapping. 667 */ 668 color = DCACHE_COLOR(va); 669 m->md.colors[color]++; 670 671 /* 672 * If all existing mappings have the same color, the mapping is 673 * cacheable. 674 */ 675 if (m->md.color == color) { 676 KASSERT(m->md.colors[DCACHE_OTHER_COLOR(color)] == 0, 677 ("pmap_cache_enter: cacheable, mappings of other color")); 678 PMAP_STATS_INC(pmap_ncache_enter_c); 679 return (1); 680 } 681 682 /* 683 * If there are no mappings of the other color, and the page still has 684 * the wrong color, this must be a new mapping. Change the color to 685 * match the new mapping, which is cacheable. We must flush the page 686 * from the cache now. 687 */ 688 if (m->md.colors[DCACHE_OTHER_COLOR(color)] == 0) { 689 KASSERT(m->md.colors[color] == 1, 690 ("pmap_cache_enter: changing color, not new mapping")); 691 dcache_page_inval(VM_PAGE_TO_PHYS(m)); 692 m->md.color = color; 693 PMAP_STATS_INC(pmap_ncache_enter_cc); 694 return (1); 695 } 696 697 PMAP_STATS_INC(pmap_ncache_enter_nc); 698 699 /* 700 * If the mapping is already non-cacheable, just return. 701 */ 702 if (m->md.color == -1) 703 return (0); 704 705 /* 706 * Mark all mappings as uncacheable, flush any lines with the other 707 * color out of the dcache, and set the color to none (-1). 708 */ 709 STAILQ_FOREACH(tp, &m->md.tte_list, tte_link) { 710 tp->tte_data &= ~TD_CV; 711 tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp)); 712 } 713 dcache_page_inval(VM_PAGE_TO_PHYS(m)); 714 m->md.color = -1; 715 return (0); 716} 717 718void 719pmap_cache_remove(vm_page_t m, vm_offset_t va) 720{ 721 struct tte *tp; 722 int color; 723 724 CTR3(KTR_PMAP, "pmap_cache_remove: m=%p va=%#lx c=%d", m, va, 725 m->md.colors[DCACHE_COLOR(va)]); 726 KASSERT(m->md.colors[DCACHE_COLOR(va)] > 0, 727 ("pmap_cache_remove: no mappings %d <= 0", 728 m->md.colors[DCACHE_COLOR(va)])); 729 PMAP_STATS_INC(pmap_ncache_remove); 730 731 /* 732 * Find the color for this virtual address and note the removal of 733 * the mapping. 734 */ 735 color = DCACHE_COLOR(va); 736 m->md.colors[color]--; 737 738 /* 739 * If the page is cacheable, just return and keep the same color, even 740 * if there are no longer any mappings. 741 */ 742 if (m->md.color != -1) { 743 PMAP_STATS_INC(pmap_ncache_remove_c); 744 return; 745 } 746 747 KASSERT(m->md.colors[DCACHE_OTHER_COLOR(color)] != 0, 748 ("pmap_cache_remove: uncacheable, no mappings of other color")); 749 750 /* 751 * If the page is not cacheable (color is -1), and the number of 752 * mappings for this color is not zero, just return. There are 753 * mappings of the other color still, so remain non-cacheable. 754 */ 755 if (m->md.colors[color] != 0) { 756 PMAP_STATS_INC(pmap_ncache_remove_nc); 757 return; 758 } 759 760 PMAP_STATS_INC(pmap_ncache_remove_cc); 761 762 /* 763 * The number of mappings for this color is now zero. Recache the 764 * other colored mappings, and change the page color to the other 765 * color. There should be no lines in the data cache for this page, 766 * so flushing should not be needed. 767 */ 768 STAILQ_FOREACH(tp, &m->md.tte_list, tte_link) { 769 tp->tte_data |= TD_CV; 770 tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp)); 771 } 772 m->md.color = DCACHE_OTHER_COLOR(color); 773} 774 775/* 776 * Map a wired page into kernel virtual address space. 777 */ 778void 779pmap_kenter(vm_offset_t va, vm_offset_t pa) 780{ 781 vm_offset_t ova; 782 struct tte *tp; 783 vm_page_t om; 784 vm_page_t m; 785 u_long data; 786 787 tp = tsb_kvtotte(va); 788 m = PHYS_TO_VM_PAGE(pa); 789 CTR4(KTR_PMAP, "pmap_kenter: va=%#lx pa=%#lx tp=%p data=%#lx", 790 va, pa, tp, tp->tte_data); 791 if ((tp->tte_data & TD_V) != 0) { 792 om = PHYS_TO_VM_PAGE(TTE_GET_PA(tp)); 793 ova = TTE_GET_VA(tp); 794 STAILQ_REMOVE(&om->md.tte_list, tp, tte, tte_link); 795 pmap_cache_remove(om, ova); 796 if (va != ova) 797 tlb_page_demap(kernel_pmap, ova); 798 } 799 data = TD_V | TD_8K | TD_PA(pa) | TD_REF | TD_SW | TD_CP | TD_P | TD_W; 800 if (pmap_cache_enter(m, va) != 0) 801 data |= TD_CV; 802 tp->tte_vpn = TV_VPN(va, TS_8K); 803 tp->tte_data = data; 804 STAILQ_INSERT_TAIL(&m->md.tte_list, tp, tte_link); 805 tp->tte_pmap = kernel_pmap; 806} 807 808/* 809 * Map a wired page into kernel virtual address space. This additionally 810 * takes a flag argument wich is or'ed to the TTE data. This is used by 811 * bus_space_map(). 812 * NOTE: if the mapping is non-cacheable, it's the caller's responsibility 813 * to flush entries that might still be in the cache, if applicable. 814 */ 815void 816pmap_kenter_flags(vm_offset_t va, vm_offset_t pa, u_long flags) 817{ 818 struct tte *tp; 819 820 tp = tsb_kvtotte(va); 821 CTR4(KTR_PMAP, "pmap_kenter_flags: va=%#lx pa=%#lx tp=%p data=%#lx", 822 va, pa, tp, tp->tte_data); 823 tp->tte_vpn = TV_VPN(va, TS_8K); 824 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_REF | TD_P | flags; 825} 826 827/* 828 * Make a temporary mapping for a physical address. This is only intended 829 * to be used for panic dumps. Caching issues can be ignored completely here, 830 * because pages mapped this way are only read. 831 */ 832void * 833pmap_kenter_temporary(vm_offset_t pa, int i) 834{ 835 struct tte *tp; 836 vm_offset_t va; 837 838 va = crashdumpmap + i * PAGE_SIZE; 839 tlb_page_demap(kernel_pmap, va); 840 tp = tsb_kvtotte(va); 841 tp->tte_vpn = TV_VPN(va, TS_8K); 842 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_REF | TD_CP | TD_CV | TD_P; 843 return ((void *)crashdumpmap); 844} 845 846/* 847 * Remove a wired page from kernel virtual address space. 848 */ 849void 850pmap_kremove(vm_offset_t va) 851{ 852 struct tte *tp; 853 vm_page_t m; 854 855 tp = tsb_kvtotte(va); 856 CTR3(KTR_PMAP, "pmap_kremove: va=%#lx tp=%p data=%#lx", va, tp, 857 tp->tte_data); 858 m = PHYS_TO_VM_PAGE(TTE_GET_PA(tp)); 859 STAILQ_REMOVE(&m->md.tte_list, tp, tte, tte_link); 860 pmap_cache_remove(m, va); 861 TTE_ZERO(tp); 862} 863 864/* 865 * Inverse of pmap_kenter_flags, used by bus_space_unmap(). 866 */ 867void 868pmap_kremove_flags(vm_offset_t va) 869{ 870 struct tte *tp; 871 872 tp = tsb_kvtotte(va); 873 CTR3(KTR_PMAP, "pmap_kremove: va=%#lx tp=%p data=%#lx", va, tp, 874 tp->tte_data); 875 TTE_ZERO(tp); 876} 877 878/* 879 * Map a range of physical addresses into kernel virtual address space. 880 * 881 * The value passed in *virt is a suggested virtual address for the mapping. 882 * Architectures which can support a direct-mapped physical to virtual region 883 * can return the appropriate address within that region, leaving '*virt' 884 * unchanged. We cannot and therefore do not; *virt is updated with the 885 * first usable address after the mapped region. 886 */ 887vm_offset_t 888pmap_map(vm_offset_t *virt, vm_offset_t pa_start, vm_offset_t pa_end, int prot) 889{ 890 struct tte *tp; 891 vm_offset_t sva; 892 vm_offset_t va; 893 vm_offset_t pa; 894 895 pa = pa_start; 896 sva = *virt; 897 va = sva; 898 for (; pa < pa_end; pa += PAGE_SIZE, va += PAGE_SIZE) { 899 tp = tsb_kvtotte(va); 900 tp->tte_vpn = TV_VPN(va, TS_8K); 901 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_REF | TD_SW | 902 TD_CP | TD_CV | TD_P | TD_W; 903 } 904 tlb_range_demap(kernel_pmap, sva, sva + (pa_end - pa_start) - 1); 905 *virt = va; 906 return (sva); 907} 908 909static vm_offset_t 910pmap_map_direct(vm_page_t m) 911{ 912 vm_offset_t pa; 913 vm_offset_t va; 914 915 pa = VM_PAGE_TO_PHYS(m); 916 if (m->md.color == -1) { 917 KASSERT(m->md.colors[0] != 0 && m->md.colors[1] != 0, 918 ("pmap_map_direct: non-cacheable, only 1 color")); 919 va = TLB_DIRECT_MASK | pa | TLB_DIRECT_UNCACHEABLE; 920 } else { 921 KASSERT(m->md.colors[DCACHE_OTHER_COLOR(m->md.color)] == 0, 922 ("pmap_map_direct: cacheable, mappings of other color")); 923 va = TLB_DIRECT_MASK | pa | 924 (m->md.color << TLB_DIRECT_COLOR_SHIFT); 925 } 926 return (va << TLB_DIRECT_SHIFT); 927} 928 929/* 930 * Map a list of wired pages into kernel virtual address space. This is 931 * intended for temporary mappings which do not need page modification or 932 * references recorded. Existing mappings in the region are overwritten. 933 */ 934void 935pmap_qenter(vm_offset_t sva, vm_page_t *m, int count) 936{ 937 vm_offset_t va; 938 939 va = sva; 940 while (count-- > 0) { 941 pmap_kenter(va, VM_PAGE_TO_PHYS(*m)); 942 va += PAGE_SIZE; 943 m++; 944 } 945 tlb_range_demap(kernel_pmap, sva, va); 946} 947 948/* 949 * Remove page mappings from kernel virtual address space. Intended for 950 * temporary mappings entered by pmap_qenter. 951 */ 952void 953pmap_qremove(vm_offset_t sva, int count) 954{ 955 vm_offset_t va; 956 957 va = sva; 958 while (count-- > 0) { 959 pmap_kremove(va); 960 va += PAGE_SIZE; 961 } 962 tlb_range_demap(kernel_pmap, sva, va); 963} 964 965#ifndef KSTACK_MAX_PAGES 966#define KSTACK_MAX_PAGES 32 967#endif 968 969/* 970 * Create the kernel stack and pcb for a new thread. 971 * This routine directly affects the fork perf for a process and 972 * create performance for a thread. 973 */ 974void 975pmap_new_thread(struct thread *td, int pages) 976{ 977 vm_page_t ma[KSTACK_MAX_PAGES]; 978 vm_object_t ksobj; 979 vm_offset_t ks; 980 vm_page_t m; 981 u_int i; 982 983 /* Bounds check */ 984 if (pages <= 1) 985 pages = KSTACK_PAGES; 986 else if (pages > KSTACK_MAX_PAGES) 987 pages = KSTACK_MAX_PAGES; 988 989 /* 990 * Allocate object for the kstack, 991 */ 992 ksobj = vm_object_allocate(OBJT_DEFAULT, pages); 993 td->td_kstack_obj = ksobj; 994 995 /* 996 * Get a kernel virtual address for the kstack for this thread. 997 */ 998 ks = kmem_alloc_nofault(kernel_map, 999 (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 1000 if (ks == 0) 1001 panic("pmap_new_thread: kstack allocation failed"); 1002 if (KSTACK_GUARD_PAGES != 0) { 1003 tlb_page_demap(kernel_pmap, ks); 1004 ks += KSTACK_GUARD_PAGES * PAGE_SIZE; 1005 } 1006 td->td_kstack = ks; 1007 1008 /* 1009 * Knowing the number of pages allocated is useful when you 1010 * want to deallocate them. 1011 */ 1012 td->td_kstack_pages = pages; 1013 1014 for (i = 0; i < pages; i++) { 1015 /* 1016 * Get a kernel stack page. 1017 */ 1018 m = vm_page_grab(ksobj, i, 1019 VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED); 1020 ma[i] = m; 1021 1022 vm_page_wakeup(m); 1023 vm_page_flag_clear(m, PG_ZERO); 1024 m->valid = VM_PAGE_BITS_ALL; 1025 } 1026 1027 /* 1028 * Enter the page into the kernel address space. 1029 */ 1030 pmap_qenter(ks, ma, pages); 1031} 1032 1033/* 1034 * Dispose the kernel stack for a thread that has exited. 1035 * This routine directly impacts the exit perf of a process and thread. 1036 */ 1037void 1038pmap_dispose_thread(struct thread *td) 1039{ 1040 vm_object_t ksobj; 1041 vm_offset_t ks; 1042 vm_page_t m; 1043 int i; 1044 int pages; 1045 1046 pages = td->td_kstack_pages; 1047 ksobj = td->td_kstack_obj; 1048 ks = td->td_kstack; 1049 for (i = 0; i < pages ; i++) { 1050 m = vm_page_lookup(ksobj, i); 1051 if (m == NULL) 1052 panic("pmap_dispose_thread: kstack already missing?"); 1053 vm_page_lock_queues(); 1054 vm_page_busy(m); 1055 vm_page_unwire(m, 0); 1056 vm_page_free(m); 1057 vm_page_unlock_queues(); 1058 } 1059 pmap_qremove(ks, pages); 1060 kmem_free(kernel_map, ks - (KSTACK_GUARD_PAGES * PAGE_SIZE), 1061 (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE); 1062 vm_object_deallocate(ksobj); 1063} 1064 1065/* 1066 * Set up a variable sized alternate kstack. 1067 */ 1068void 1069pmap_new_altkstack(struct thread *td, int pages) 1070{ 1071 /* shuffle the original stack */ 1072 td->td_altkstack_obj = td->td_kstack_obj; 1073 td->td_altkstack = td->td_kstack; 1074 td->td_altkstack_pages = td->td_kstack_pages; 1075 1076 pmap_new_thread(td, pages); 1077} 1078 1079void 1080pmap_dispose_altkstack(struct thread *td) 1081{ 1082 pmap_dispose_thread(td); 1083 1084 /* restore the original kstack */ 1085 td->td_kstack = td->td_altkstack; 1086 td->td_kstack_obj = td->td_altkstack_obj; 1087 td->td_kstack_pages = td->td_altkstack_pages; 1088 td->td_altkstack = 0; 1089 td->td_altkstack_obj = NULL; 1090 td->td_altkstack_pages = 0; 1091} 1092 1093/* 1094 * Allow the kernel stack for a thread to be prejudicially paged out. 1095 */ 1096void 1097pmap_swapout_thread(struct thread *td) 1098{ 1099 vm_object_t ksobj; 1100 vm_offset_t ks; 1101 vm_page_t m; 1102 int i; 1103 int pages; 1104 1105 pages = td->td_kstack_pages; 1106 ksobj = td->td_kstack_obj; 1107 ks = (vm_offset_t)td->td_kstack; 1108 for (i = 0; i < pages; i++) { 1109 m = vm_page_lookup(ksobj, i); 1110 if (m == NULL) 1111 panic("pmap_swapout_thread: kstack already missing?"); 1112 vm_page_lock_queues(); 1113 vm_page_dirty(m); 1114 vm_page_unwire(m, 0); 1115 vm_page_unlock_queues(); 1116 } 1117 pmap_qremove(ks, pages); 1118} 1119 1120/* 1121 * Bring the kernel stack for a specified thread back in. 1122 */ 1123void 1124pmap_swapin_thread(struct thread *td) 1125{ 1126 vm_page_t ma[KSTACK_MAX_PAGES]; 1127 vm_object_t ksobj; 1128 vm_offset_t ks; 1129 vm_page_t m; 1130 int rv; 1131 int i; 1132 int pages; 1133 1134 pages = td->td_kstack_pages; 1135 ksobj = td->td_kstack_obj; 1136 ks = td->td_kstack; 1137 for (i = 0; i < pages; i++) { 1138 m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 1139 if (m->valid != VM_PAGE_BITS_ALL) { 1140 rv = vm_pager_get_pages(ksobj, &m, 1, 0); 1141 if (rv != VM_PAGER_OK) 1142 panic("pmap_swapin_thread: cannot get kstack"); 1143 m = vm_page_lookup(ksobj, i); 1144 m->valid = VM_PAGE_BITS_ALL; 1145 } 1146 ma[i] = m; 1147 vm_page_lock_queues(); 1148 vm_page_wire(m); 1149 vm_page_wakeup(m); 1150 vm_page_unlock_queues(); 1151 } 1152 pmap_qenter(ks, ma, pages); 1153} 1154 1155/* 1156 * Initialize the pmap associated with process 0. 1157 */ 1158void 1159pmap_pinit0(pmap_t pm) 1160{ 1161 int i; 1162 1163 for (i = 0; i < MAXCPU; i++) 1164 pm->pm_context[i] = 0; 1165 pm->pm_active = 0; 1166 pm->pm_tsb = NULL; 1167 pm->pm_tsb_obj = NULL; 1168 bzero(&pm->pm_stats, sizeof(pm->pm_stats)); 1169} 1170 1171/* 1172 * Initialize a preallocated and zeroed pmap structure, uch as one in a 1173 * vmspace structure. 1174 */ 1175void 1176pmap_pinit(pmap_t pm) 1177{ 1178 vm_page_t ma[TSB_PAGES]; 1179 vm_page_t m; 1180 int i; 1181 1182 /* 1183 * Allocate kva space for the tsb. 1184 */ 1185 if (pm->pm_tsb == NULL) { 1186 pm->pm_tsb = (struct tte *)kmem_alloc_pageable(kernel_map, 1187 TSB_BSIZE); 1188 } 1189 1190 /* 1191 * Allocate an object for it. 1192 */ 1193 if (pm->pm_tsb_obj == NULL) 1194 pm->pm_tsb_obj = vm_object_allocate(OBJT_DEFAULT, TSB_PAGES); 1195 1196 for (i = 0; i < TSB_PAGES; i++) { 1197 m = vm_page_grab(pm->pm_tsb_obj, i, 1198 VM_ALLOC_RETRY | VM_ALLOC_ZERO); 1199 if ((m->flags & PG_ZERO) == 0) 1200 pmap_zero_page(m); 1201 1202 m->wire_count++; 1203 cnt.v_wire_count++; 1204 1205 vm_page_flag_clear(m, PG_BUSY); 1206 m->valid = VM_PAGE_BITS_ALL; 1207 1208 ma[i] = m; 1209 } 1210 pmap_qenter((vm_offset_t)pm->pm_tsb, ma, TSB_PAGES); 1211 1212 for (i = 0; i < MAXCPU; i++) 1213 pm->pm_context[i] = -1; 1214 pm->pm_active = 0; 1215 bzero(&pm->pm_stats, sizeof(pm->pm_stats)); 1216} 1217 1218void 1219pmap_pinit2(pmap_t pmap) 1220{ 1221 /* XXX: Remove this stub when no longer called */ 1222} 1223 1224/* 1225 * Release any resources held by the given physical map. 1226 * Called when a pmap initialized by pmap_pinit is being released. 1227 * Should only be called if the map contains no valid mappings. 1228 */ 1229void 1230pmap_release(pmap_t pm) 1231{ 1232 vm_object_t obj; 1233 vm_page_t m; 1234 1235 CTR2(KTR_PMAP, "pmap_release: ctx=%#x tsb=%p", 1236 pm->pm_context[PCPU_GET(cpuid)], pm->pm_tsb); 1237 obj = pm->pm_tsb_obj; 1238 KASSERT(obj->ref_count == 1, ("pmap_release: tsbobj ref count != 1")); 1239 KASSERT(pmap_resident_count(pm) == 0, 1240 ("pmap_release: resident pages %ld != 0", 1241 pmap_resident_count(pm))); 1242 while (!TAILQ_EMPTY(&obj->memq)) { 1243 m = TAILQ_FIRST(&obj->memq); 1244 vm_page_lock_queues(); 1245 if (vm_page_sleep_if_busy(m, FALSE, "pmaprl")) 1246 continue; 1247 vm_page_busy(m); 1248 KASSERT(m->hold_count == 0, 1249 ("pmap_release: freeing held tsb page")); 1250 m->wire_count--; 1251 cnt.v_wire_count--; 1252 vm_page_free_zero(m); 1253 vm_page_unlock_queues(); 1254 } 1255 pmap_qremove((vm_offset_t)pm->pm_tsb, TSB_PAGES); 1256} 1257 1258/* 1259 * Grow the number of kernel page table entries. Unneeded. 1260 */ 1261void 1262pmap_growkernel(vm_offset_t addr) 1263{ 1264 1265 panic("pmap_growkernel: can't grow kernel"); 1266} 1267 1268int 1269pmap_remove_tte(struct pmap *pm, struct pmap *pm2, struct tte *tp, 1270 vm_offset_t va) 1271{ 1272 vm_page_t m; 1273 1274 m = PHYS_TO_VM_PAGE(TTE_GET_PA(tp)); 1275 STAILQ_REMOVE(&m->md.tte_list, tp, tte, tte_link); 1276 if ((tp->tte_data & TD_WIRED) != 0) 1277 pm->pm_stats.wired_count--; 1278 if ((tp->tte_data & TD_PV) != 0) { 1279 if ((tp->tte_data & TD_W) != 0 && 1280 pmap_track_modified(pm, va)) 1281 vm_page_dirty(m); 1282 if ((tp->tte_data & TD_REF) != 0) 1283 vm_page_flag_set(m, PG_REFERENCED); 1284 if (STAILQ_EMPTY(&m->md.tte_list)) 1285 vm_page_flag_clear(m, PG_WRITEABLE); 1286 pm->pm_stats.resident_count--; 1287 } 1288 pmap_cache_remove(m, va); 1289 TTE_ZERO(tp); 1290 if (PMAP_REMOVE_DONE(pm)) 1291 return (0); 1292 return (1); 1293} 1294 1295/* 1296 * Remove the given range of addresses from the specified map. 1297 */ 1298void 1299pmap_remove(pmap_t pm, vm_offset_t start, vm_offset_t end) 1300{ 1301 struct tte *tp; 1302 vm_offset_t va; 1303 1304 CTR3(KTR_PMAP, "pmap_remove: ctx=%#lx start=%#lx end=%#lx", 1305 pm->pm_context[PCPU_GET(cpuid)], start, end); 1306 if (PMAP_REMOVE_DONE(pm)) 1307 return; 1308 if (end - start > PMAP_TSB_THRESH) { 1309 tsb_foreach(pm, NULL, start, end, pmap_remove_tte); 1310 tlb_context_demap(pm); 1311 } else { 1312 for (va = start; va < end; va += PAGE_SIZE) { 1313 if ((tp = tsb_tte_lookup(pm, va)) != NULL) { 1314 if (!pmap_remove_tte(pm, NULL, tp, va)) 1315 break; 1316 } 1317 } 1318 tlb_range_demap(pm, start, end - 1); 1319 } 1320} 1321 1322void 1323pmap_remove_all(vm_page_t m) 1324{ 1325 struct pmap *pm; 1326 struct tte *tpn; 1327 struct tte *tp; 1328 vm_offset_t va; 1329 1330 KASSERT((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0, 1331 ("pv_remove_all: illegal for unmanaged page %#lx", 1332 VM_PAGE_TO_PHYS(m))); 1333 for (tp = STAILQ_FIRST(&m->md.tte_list); tp != NULL; tp = tpn) { 1334 tpn = STAILQ_NEXT(tp, tte_link); 1335 if ((tp->tte_data & TD_PV) == 0) 1336 continue; 1337 pm = TTE_GET_PMAP(tp); 1338 va = TTE_GET_VA(tp); 1339 if ((tp->tte_data & TD_WIRED) != 0) 1340 pm->pm_stats.wired_count--; 1341 if ((tp->tte_data & TD_REF) != 0) 1342 vm_page_flag_set(m, PG_REFERENCED); 1343 if ((tp->tte_data & TD_W) != 0 && 1344 pmap_track_modified(pm, va)) 1345 vm_page_dirty(m); 1346 tp->tte_data &= ~TD_V; 1347 tlb_page_demap(pm, va); 1348 STAILQ_REMOVE(&m->md.tte_list, tp, tte, tte_link); 1349 pm->pm_stats.resident_count--; 1350 pmap_cache_remove(m, va); 1351 TTE_ZERO(tp); 1352 } 1353 vm_page_flag_clear(m, PG_WRITEABLE); 1354} 1355 1356int 1357pmap_protect_tte(struct pmap *pm, struct pmap *pm2, struct tte *tp, 1358 vm_offset_t va) 1359{ 1360 vm_page_t m; 1361 1362 if ((tp->tte_data & TD_PV) != 0) { 1363 m = PHYS_TO_VM_PAGE(TTE_GET_PA(tp)); 1364 if ((tp->tte_data & TD_REF) != 0) { 1365 vm_page_flag_set(m, PG_REFERENCED); 1366 tp->tte_data &= ~TD_REF; 1367 } 1368 if ((tp->tte_data & TD_W) != 0 && 1369 pmap_track_modified(pm, va)) { 1370 vm_page_dirty(m); 1371 } 1372 } 1373 tp->tte_data &= ~(TD_W | TD_SW); 1374 return (0); 1375} 1376 1377/* 1378 * Set the physical protection on the specified range of this map as requested. 1379 */ 1380void 1381pmap_protect(pmap_t pm, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot) 1382{ 1383 vm_offset_t va; 1384 struct tte *tp; 1385 1386 CTR4(KTR_PMAP, "pmap_protect: ctx=%#lx sva=%#lx eva=%#lx prot=%#lx", 1387 pm->pm_context[PCPU_GET(cpuid)], sva, eva, prot); 1388 1389 if ((prot & VM_PROT_READ) == VM_PROT_NONE) { 1390 pmap_remove(pm, sva, eva); 1391 return; 1392 } 1393 1394 if (prot & VM_PROT_WRITE) 1395 return; 1396 1397 if (eva - sva > PMAP_TSB_THRESH) { 1398 tsb_foreach(pm, NULL, sva, eva, pmap_protect_tte); 1399 tlb_context_demap(pm); 1400 } else { 1401 for (va = sva; va < eva; va += PAGE_SIZE) { 1402 if ((tp = tsb_tte_lookup(pm, va)) != NULL) 1403 pmap_protect_tte(pm, NULL, tp, va); 1404 } 1405 tlb_range_demap(pm, sva, eva - 1); 1406 } 1407} 1408 1409/* 1410 * Map the given physical page at the specified virtual address in the 1411 * target pmap with the protection requested. If specified the page 1412 * will be wired down. 1413 */ 1414void 1415pmap_enter(pmap_t pm, vm_offset_t va, vm_page_t m, vm_prot_t prot, 1416 boolean_t wired) 1417{ 1418 struct tte *tp; 1419 vm_offset_t pa; 1420 u_long data; 1421 1422 pa = VM_PAGE_TO_PHYS(m); 1423 CTR6(KTR_PMAP, 1424 "pmap_enter: ctx=%p m=%p va=%#lx pa=%#lx prot=%#x wired=%d", 1425 pm->pm_context[PCPU_GET(cpuid)], m, va, pa, prot, wired); 1426 1427 /* 1428 * If there is an existing mapping, and the physical address has not 1429 * changed, must be protection or wiring change. 1430 */ 1431 if ((tp = tsb_tte_lookup(pm, va)) != NULL && TTE_GET_PA(tp) == pa) { 1432 CTR0(KTR_PMAP, "pmap_enter: update"); 1433 PMAP_STATS_INC(pmap_enter_nupdate); 1434 1435 /* 1436 * Wiring change, just update stats. 1437 */ 1438 if (wired) { 1439 if ((tp->tte_data & TD_WIRED) == 0) { 1440 tp->tte_data |= TD_WIRED; 1441 pm->pm_stats.wired_count++; 1442 } 1443 } else { 1444 if ((tp->tte_data & TD_WIRED) != 0) { 1445 tp->tte_data &= ~TD_WIRED; 1446 pm->pm_stats.wired_count--; 1447 } 1448 } 1449 1450 /* 1451 * Save the old bits and clear the ones we're interested in. 1452 */ 1453 data = tp->tte_data; 1454 tp->tte_data &= ~(TD_EXEC | TD_SW | TD_W); 1455 1456 /* 1457 * If we're turning off write permissions, sense modify status. 1458 */ 1459 if ((prot & VM_PROT_WRITE) != 0) { 1460 tp->tte_data |= TD_SW; 1461 if (wired) { 1462 tp->tte_data |= TD_W; 1463 } 1464 } else if ((data & TD_W) != 0 && 1465 pmap_track_modified(pm, va)) { 1466 vm_page_dirty(m); 1467 } 1468 1469 /* 1470 * If we're turning on execute permissions, flush the icache. 1471 */ 1472 if ((prot & VM_PROT_EXECUTE) != 0) { 1473 if ((data & TD_EXEC) == 0) { 1474 PMAP_STATS_INC(pmap_niflush); 1475 icache_page_inval(pa); 1476 } 1477 tp->tte_data |= TD_EXEC; 1478 } 1479 1480 /* 1481 * Delete the old mapping. 1482 */ 1483 tlb_page_demap(pm, TTE_GET_VA(tp)); 1484 } else { 1485 /* 1486 * If there is an existing mapping, but its for a different 1487 * phsyical address, delete the old mapping. 1488 */ 1489 if (tp != NULL) { 1490 CTR0(KTR_PMAP, "pmap_enter: replace"); 1491 PMAP_STATS_INC(pmap_enter_nreplace); 1492 vm_page_lock_queues(); 1493 pmap_remove_tte(pm, NULL, tp, va); 1494 vm_page_unlock_queues(); 1495 tlb_page_demap(pm, va); 1496 } else { 1497 CTR0(KTR_PMAP, "pmap_enter: new"); 1498 PMAP_STATS_INC(pmap_enter_nnew); 1499 } 1500 1501 /* 1502 * Now set up the data and install the new mapping. 1503 */ 1504 data = TD_V | TD_8K | TD_PA(pa) | TD_CP; 1505 if (pm == kernel_pmap) 1506 data |= TD_P; 1507 if (prot & VM_PROT_WRITE) 1508 data |= TD_SW; 1509 if (prot & VM_PROT_EXECUTE) { 1510 data |= TD_EXEC; 1511 PMAP_STATS_INC(pmap_niflush); 1512 icache_page_inval(pa); 1513 } 1514 1515 /* 1516 * If its wired update stats. We also don't need reference or 1517 * modify tracking for wired mappings, so set the bits now. 1518 */ 1519 if (wired) { 1520 pm->pm_stats.wired_count++; 1521 data |= TD_REF | TD_WIRED; 1522 if ((prot & VM_PROT_WRITE) != 0) 1523 data |= TD_W; 1524 } 1525 1526 tsb_tte_enter(pm, m, va, TS_8K, data); 1527 } 1528} 1529 1530void 1531pmap_object_init_pt(pmap_t pm, vm_offset_t addr, vm_object_t object, 1532 vm_pindex_t pindex, vm_size_t size, int limit) 1533{ 1534 /* XXX */ 1535} 1536 1537void 1538pmap_prefault(pmap_t pm, vm_offset_t va, vm_map_entry_t entry) 1539{ 1540 /* XXX */ 1541} 1542 1543/* 1544 * Change the wiring attribute for a map/virtual-address pair. 1545 * The mapping must already exist in the pmap. 1546 */ 1547void 1548pmap_change_wiring(pmap_t pm, vm_offset_t va, boolean_t wired) 1549{ 1550 struct tte *tp; 1551 1552 if ((tp = tsb_tte_lookup(pm, va)) != NULL) { 1553 if (wired) { 1554 if ((tp->tte_data & TD_WIRED) == 0) 1555 pm->pm_stats.wired_count++; 1556 tp->tte_data |= TD_WIRED; 1557 } else { 1558 if ((tp->tte_data & TD_WIRED) != 0) 1559 pm->pm_stats.wired_count--; 1560 tp->tte_data &= ~TD_WIRED; 1561 } 1562 } 1563} 1564 1565static int 1566pmap_copy_tte(pmap_t src_pmap, pmap_t dst_pmap, struct tte *tp, vm_offset_t va) 1567{ 1568 vm_page_t m; 1569 u_long data; 1570 1571 if (tsb_tte_lookup(dst_pmap, va) == NULL) { 1572 data = tp->tte_data & 1573 ~(TD_PV | TD_REF | TD_SW | TD_CV | TD_W); 1574 m = PHYS_TO_VM_PAGE(TTE_GET_PA(tp)); 1575 tsb_tte_enter(dst_pmap, m, va, TS_8K, data); 1576 } 1577 return (1); 1578} 1579 1580void 1581pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, 1582 vm_size_t len, vm_offset_t src_addr) 1583{ 1584 struct tte *tp; 1585 vm_offset_t va; 1586 1587 if (dst_addr != src_addr) 1588 return; 1589 if (len > PMAP_TSB_THRESH) { 1590 tsb_foreach(src_pmap, dst_pmap, src_addr, src_addr + len, 1591 pmap_copy_tte); 1592 tlb_context_demap(dst_pmap); 1593 } else { 1594 for (va = src_addr; va < src_addr + len; va += PAGE_SIZE) { 1595 if ((tp = tsb_tte_lookup(src_pmap, va)) != NULL) 1596 pmap_copy_tte(src_pmap, dst_pmap, tp, va); 1597 } 1598 tlb_range_demap(dst_pmap, src_addr, src_addr + len - 1); 1599 } 1600} 1601 1602/* 1603 * Zero a page of physical memory by temporarily mapping it into the tlb. 1604 */ 1605void 1606pmap_zero_page(vm_page_t m) 1607{ 1608 vm_offset_t va; 1609 1610 va = pmap_map_direct(m); 1611 CTR2(KTR_PMAP, "pmap_zero_page: pa=%#lx va=%#lx", 1612 VM_PAGE_TO_PHYS(m), va); 1613 bzero((void *)va, PAGE_SIZE); 1614} 1615 1616void 1617pmap_zero_page_area(vm_page_t m, int off, int size) 1618{ 1619 vm_offset_t va; 1620 1621 KASSERT(off + size <= PAGE_SIZE, ("pmap_zero_page_area: bad off/size")); 1622 va = pmap_map_direct(m); 1623 CTR4(KTR_PMAP, "pmap_zero_page_area: pa=%#lx va=%#lx off=%#x size=%#x", 1624 VM_PAGE_TO_PHYS(m), va, off, size); 1625 bzero((void *)(va + off), size); 1626} 1627 1628void 1629pmap_zero_page_idle(vm_page_t m) 1630{ 1631 vm_offset_t va; 1632 1633 va = pmap_map_direct(m); 1634 CTR2(KTR_PMAP, "pmap_zero_page_idle: pa=%#lx va=%#lx", 1635 VM_PAGE_TO_PHYS(m), va); 1636 bzero((void *)va, PAGE_SIZE); 1637} 1638 1639/* 1640 * Copy a page of physical memory by temporarily mapping it into the tlb. 1641 */ 1642void 1643pmap_copy_page(vm_page_t msrc, vm_page_t mdst) 1644{ 1645 vm_offset_t dst; 1646 vm_offset_t src; 1647 1648 src = pmap_map_direct(msrc); 1649 dst = pmap_map_direct(mdst); 1650 CTR4(KTR_PMAP, "pmap_zero_page: src=%#lx va=%#lx dst=%#lx va=%#lx", 1651 VM_PAGE_TO_PHYS(msrc), src, VM_PAGE_TO_PHYS(mdst), dst); 1652 bcopy((void *)src, (void *)dst, PAGE_SIZE); 1653} 1654 1655/* 1656 * Returns true if the pmap's pv is one of the first 1657 * 16 pvs linked to from this page. This count may 1658 * be changed upwards or downwards in the future; it 1659 * is only necessary that true be returned for a small 1660 * subset of pmaps for proper page aging. 1661 */ 1662boolean_t 1663pmap_page_exists_quick(pmap_t pm, vm_page_t m) 1664{ 1665 struct tte *tp; 1666 int loops; 1667 1668 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0) 1669 return (FALSE); 1670 loops = 0; 1671 STAILQ_FOREACH(tp, &m->md.tte_list, tte_link) { 1672 if ((tp->tte_data & TD_PV) == 0) 1673 continue; 1674 if (TTE_GET_PMAP(tp) == pm) 1675 return (TRUE); 1676 if (++loops >= 16) 1677 break; 1678 } 1679 return (FALSE); 1680} 1681 1682/* 1683 * Remove all pages from specified address space, this aids process exit 1684 * speeds. This is much faster than pmap_remove n the case of running down 1685 * an entire address space. Only works for the current pmap. 1686 */ 1687void 1688pmap_remove_pages(pmap_t pm, vm_offset_t sva, vm_offset_t eva) 1689{ 1690} 1691 1692/* 1693 * Lower the permission for all mappings to a given page. 1694 */ 1695void 1696pmap_page_protect(vm_page_t m, vm_prot_t prot) 1697{ 1698 1699 if ((prot & VM_PROT_WRITE) == 0) { 1700 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) 1701 pmap_clear_write(m); 1702 else 1703 pmap_remove_all(m); 1704 } 1705} 1706 1707vm_offset_t 1708pmap_phys_address(int ppn) 1709{ 1710 1711 return (sparc64_ptob(ppn)); 1712} 1713 1714/* 1715 * pmap_ts_referenced: 1716 * 1717 * Return a count of reference bits for a page, clearing those bits. 1718 * It is not necessary for every reference bit to be cleared, but it 1719 * is necessary that 0 only be returned when there are truly no 1720 * reference bits set. 1721 * 1722 * XXX: The exact number of bits to check and clear is a matter that 1723 * should be tested and standardized at some point in the future for 1724 * optimal aging of shared pages. 1725 */ 1726 1727int 1728pmap_ts_referenced(vm_page_t m) 1729{ 1730 struct tte *tpf; 1731 struct tte *tpn; 1732 struct tte *tp; 1733 int count; 1734 1735 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0) 1736 return (0); 1737 count = 0; 1738 if ((tp = STAILQ_FIRST(&m->md.tte_list)) != NULL) { 1739 tpf = tp; 1740 do { 1741 tpn = STAILQ_NEXT(tp, tte_link); 1742 STAILQ_REMOVE(&m->md.tte_list, tp, tte, tte_link); 1743 STAILQ_INSERT_TAIL(&m->md.tte_list, tp, tte_link); 1744 if ((tp->tte_data & TD_PV) == 0 || 1745 !pmap_track_modified(TTE_GET_PMAP(tp), 1746 TTE_GET_VA(tp))) 1747 continue; 1748 if ((tp->tte_data & TD_REF) != 0) { 1749 tp->tte_data &= ~TD_REF; 1750 if (++count > 4) 1751 break; 1752 } 1753 } while ((tp = tpn) != NULL && tp != tpf); 1754 } 1755 return (count); 1756} 1757 1758boolean_t 1759pmap_is_modified(vm_page_t m) 1760{ 1761 struct tte *tp; 1762 1763 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0) 1764 return FALSE; 1765 STAILQ_FOREACH(tp, &m->md.tte_list, tte_link) { 1766 if ((tp->tte_data & TD_PV) == 0 || 1767 !pmap_track_modified(TTE_GET_PMAP(tp), TTE_GET_VA(tp))) 1768 continue; 1769 if ((tp->tte_data & TD_W) != 0) 1770 return (TRUE); 1771 } 1772 return (FALSE); 1773} 1774 1775void 1776pmap_clear_modify(vm_page_t m) 1777{ 1778 struct tte *tp; 1779 1780 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0) 1781 return; 1782 STAILQ_FOREACH(tp, &m->md.tte_list, tte_link) { 1783 if ((tp->tte_data & TD_PV) == 0) 1784 continue; 1785 if ((tp->tte_data & TD_W) != 0) { 1786 tp->tte_data &= ~TD_W; 1787 tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp)); 1788 } 1789 } 1790} 1791 1792void 1793pmap_clear_reference(vm_page_t m) 1794{ 1795 struct tte *tp; 1796 1797 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0) 1798 return; 1799 STAILQ_FOREACH(tp, &m->md.tte_list, tte_link) { 1800 if ((tp->tte_data & TD_PV) == 0) 1801 continue; 1802 if ((tp->tte_data & TD_REF) != 0) { 1803 tp->tte_data &= ~TD_REF; 1804 tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp)); 1805 } 1806 } 1807} 1808 1809void 1810pmap_clear_write(vm_page_t m) 1811{ 1812 struct tte *tp; 1813 1814 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 || 1815 (m->flags & PG_WRITEABLE) == 0) 1816 return; 1817 STAILQ_FOREACH(tp, &m->md.tte_list, tte_link) { 1818 if ((tp->tte_data & TD_PV) == 0) 1819 continue; 1820 if ((tp->tte_data & (TD_SW | TD_W)) != 0) { 1821 if ((tp->tte_data & TD_W) != 0 && 1822 pmap_track_modified(TTE_GET_PMAP(tp), 1823 TTE_GET_VA(tp))) 1824 vm_page_dirty(m); 1825 tp->tte_data &= ~(TD_SW | TD_W); 1826 tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp)); 1827 } 1828 } 1829 vm_page_flag_clear(m, PG_WRITEABLE); 1830} 1831 1832int 1833pmap_mincore(pmap_t pm, vm_offset_t addr) 1834{ 1835 /* TODO; */ 1836 return (0); 1837} 1838 1839/* 1840 * Activate a user pmap. The pmap must be activated before its address space 1841 * can be accessed in any way. 1842 */ 1843void 1844pmap_activate(struct thread *td) 1845{ 1846 struct vmspace *vm; 1847 vm_offset_t tsb; 1848 u_long context; 1849 pmap_t pm; 1850 1851 vm = td->td_proc->p_vmspace; 1852 pm = &vm->vm_pmap; 1853 tsb = (vm_offset_t)pm->pm_tsb; 1854 1855 KASSERT(pm->pm_active == 0, ("pmap_activate: pmap already active?")); 1856 KASSERT(pm->pm_context[PCPU_GET(cpuid)] != 0, 1857 ("pmap_activate: activating nucleus context?")); 1858 1859 mtx_lock_spin(&sched_lock); 1860 stxa(AA_DMMU_TSB, ASI_DMMU, tsb); 1861 stxa(AA_IMMU_TSB, ASI_IMMU, tsb); 1862 membar(Sync); 1863 context = pmap_context_alloc(); 1864 pm->pm_context[PCPU_GET(cpuid)] = context; 1865 pm->pm_active |= PCPU_GET(cpumask); 1866 PCPU_SET(vmspace, vm); 1867 stxa(AA_DMMU_PCXR, ASI_DMMU, context); 1868 membar(Sync); 1869 mtx_unlock_spin(&sched_lock); 1870} 1871 1872vm_offset_t 1873pmap_addr_hint(vm_object_t object, vm_offset_t va, vm_size_t size) 1874{ 1875 1876 return (va); 1877} 1878