1 /* $NetBSD: pmap.c,v 1.32 2005/02/25 07:09:58 simonb Exp $ */ 2 3 /* 4 * Copyright 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 40 * Copyright (C) 1995, 1996 TooLs GmbH. 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by TooLs GmbH. 54 * 4. The name of TooLs GmbH may not be used to endorse or promote products 55 * derived from this software without specific prior written permission. 56 * 57 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 60 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 61 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 62 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 63 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 64 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 65 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 66 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.32 2005/02/25 07:09:58 simonb Exp $"); 71 72 #include <sys/param.h> 73 #include <sys/malloc.h> 74 #include <sys/proc.h> 75 #include <sys/user.h> 76 #include <sys/queue.h> 77 #include <sys/systm.h> 78 #include <sys/pool.h> 79 #include <sys/device.h> 80 81 #include <uvm/uvm.h> 82 83 #include <machine/cpu.h> 84 #include <machine/pcb.h> 85 #include <machine/powerpc.h> 86 87 #include <powerpc/spr.h> 88 #include <machine/tlb.h> 89 90 /* 91 * kernmap is an array of PTEs large enough to map in 92 * 4GB. At 16KB/page it is 256K entries or 2MB. 93 */ 94 #define KERNMAP_SIZE ((0xffffffffU/PAGE_SIZE)+1) 95 caddr_t kernmap; 96 97 #define MINCTX 2 98 #define NUMCTX 256 99 volatile struct pmap *ctxbusy[NUMCTX]; 100 101 #define TLBF_USED 0x1 102 #define TLBF_REF 0x2 103 #define TLBF_LOCKED 0x4 104 #define TLB_LOCKED(i) (tlb_info[(i)].ti_flags & TLBF_LOCKED) 105 typedef struct tlb_info_s { 106 char ti_flags; 107 char ti_ctx; /* TLB_PID assiciated with the entry */ 108 u_int ti_va; 109 } tlb_info_t; 110 111 volatile tlb_info_t tlb_info[NTLB]; 112 /* We'll use a modified FIFO replacement policy cause it's cheap */ 113 volatile int tlbnext = TLB_NRESERVED; 114 115 u_long dtlb_miss_count = 0; 116 u_long itlb_miss_count = 0; 117 u_long ktlb_miss_count = 0; 118 u_long utlb_miss_count = 0; 119 120 /* Event counters */ 121 struct evcnt tlbmiss_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, 122 NULL, "cpu", "tlbmiss"); 123 struct evcnt tlbhit_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, 124 NULL, "cpu", "tlbhit"); 125 struct evcnt tlbflush_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, 126 NULL, "cpu", "tlbflush"); 127 struct evcnt tlbenter_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, 128 NULL, "cpu", "tlbenter"); 129 130 struct pmap kernel_pmap_; 131 132 int physmem; 133 static int npgs; 134 static u_int nextavail; 135 #ifndef MSGBUFADDR 136 extern paddr_t msgbuf_paddr; 137 #endif 138 139 static struct mem_region *mem, *avail; 140 141 /* 142 * This is a cache of referenced/modified bits. 143 * Bits herein are shifted by ATTRSHFT. 144 */ 145 static char *pmap_attrib; 146 147 #define PV_WIRED 0x1 148 #define PV_WIRE(pv) ((pv)->pv_va |= PV_WIRED) 149 #define PV_UNWIRE(pv) ((pv)->pv_va &= ~PV_WIRED) 150 #define PV_ISWIRED(pv) ((pv)->pv_va & PV_WIRED) 151 #define PV_CMPVA(va,pv) (!(((pv)->pv_va ^ (va)) & (~PV_WIRED))) 152 153 struct pv_entry { 154 struct pv_entry *pv_next; /* Linked list of mappings */ 155 vaddr_t pv_va; /* virtual address of mapping */ 156 struct pmap *pv_pm; 157 }; 158 159 struct pv_entry *pv_table; 160 static struct pool pv_pool; 161 162 static int pmap_initialized; 163 164 static int ctx_flush(int); 165 166 inline struct pv_entry *pa_to_pv(paddr_t); 167 static inline char *pa_to_attr(paddr_t); 168 169 static inline volatile u_int *pte_find(struct pmap *, vaddr_t); 170 static inline int pte_enter(struct pmap *, vaddr_t, u_int); 171 172 static inline int pmap_enter_pv(struct pmap *, vaddr_t, paddr_t, boolean_t); 173 static void pmap_remove_pv(struct pmap *, vaddr_t, paddr_t); 174 175 176 inline struct pv_entry * 177 pa_to_pv(paddr_t pa) 178 { 179 int bank, pg; 180 181 bank = vm_physseg_find(atop(pa), &pg); 182 if (bank == -1) 183 return NULL; 184 return &vm_physmem[bank].pmseg.pvent[pg]; 185 } 186 187 static inline char * 188 pa_to_attr(paddr_t pa) 189 { 190 int bank, pg; 191 192 bank = vm_physseg_find(atop(pa), &pg); 193 if (bank == -1) 194 return NULL; 195 return &vm_physmem[bank].pmseg.attrs[pg]; 196 } 197 198 /* 199 * Insert PTE into page table. 200 */ 201 int 202 pte_enter(struct pmap *pm, vaddr_t va, u_int pte) 203 { 204 int seg = STIDX(va); 205 int ptn = PTIDX(va); 206 u_int oldpte; 207 208 if (!pm->pm_ptbl[seg]) { 209 /* Don't allocate a page to clear a non-existent mapping. */ 210 if (!pte) 211 return (0); 212 /* Allocate a page XXXX this will sleep! */ 213 pm->pm_ptbl[seg] = 214 (uint *)uvm_km_zalloc(kernel_map, PAGE_SIZE); 215 } 216 oldpte = pm->pm_ptbl[seg][ptn]; 217 pm->pm_ptbl[seg][ptn] = pte; 218 219 /* Flush entry. */ 220 ppc4xx_tlb_flush(va, pm->pm_ctx); 221 if (oldpte != pte) { 222 if (pte == 0) 223 pm->pm_stats.resident_count--; 224 else 225 pm->pm_stats.resident_count++; 226 } 227 return (1); 228 } 229 230 /* 231 * Get a pointer to a PTE in a page table. 232 */ 233 volatile u_int * 234 pte_find(struct pmap *pm, vaddr_t va) 235 { 236 int seg = STIDX(va); 237 int ptn = PTIDX(va); 238 239 if (pm->pm_ptbl[seg]) 240 return (&pm->pm_ptbl[seg][ptn]); 241 242 return (NULL); 243 } 244 245 /* 246 * This is called during initppc, before the system is really initialized. 247 */ 248 void 249 pmap_bootstrap(u_int kernelstart, u_int kernelend) 250 { 251 struct mem_region *mp, *mp1; 252 int cnt, i; 253 u_int s, e, sz; 254 255 /* 256 * Allocate the kernel page table at the end of 257 * kernel space so it's in the locked TTE. 258 */ 259 kernmap = (caddr_t)kernelend; 260 261 /* 262 * Initialize kernel page table. 263 */ 264 for (i = 0; i < STSZ; i++) { 265 pmap_kernel()->pm_ptbl[i] = 0; 266 } 267 ctxbusy[0] = ctxbusy[1] = pmap_kernel(); 268 269 /* 270 * Announce page-size to the VM-system 271 */ 272 uvmexp.pagesize = NBPG; 273 uvm_setpagesize(); 274 275 /* 276 * Get memory. 277 */ 278 mem_regions(&mem, &avail); 279 for (mp = mem; mp->size; mp++) { 280 physmem += btoc(mp->size); 281 printf("+%lx,",mp->size); 282 } 283 printf("\n"); 284 ppc4xx_tlb_init(); 285 /* 286 * Count the number of available entries. 287 */ 288 for (cnt = 0, mp = avail; mp->size; mp++) 289 cnt++; 290 291 /* 292 * Page align all regions. 293 * Non-page aligned memory isn't very interesting to us. 294 * Also, sort the entries for ascending addresses. 295 */ 296 kernelstart &= ~PGOFSET; 297 kernelend = (kernelend + PGOFSET) & ~PGOFSET; 298 for (mp = avail; mp->size; mp++) { 299 s = mp->start; 300 e = mp->start + mp->size; 301 printf("%08x-%08x -> ",s,e); 302 /* 303 * Check whether this region holds all of the kernel. 304 */ 305 if (s < kernelstart && e > kernelend) { 306 avail[cnt].start = kernelend; 307 avail[cnt++].size = e - kernelend; 308 e = kernelstart; 309 } 310 /* 311 * Look whether this regions starts within the kernel. 312 */ 313 if (s >= kernelstart && s < kernelend) { 314 if (e <= kernelend) 315 goto empty; 316 s = kernelend; 317 } 318 /* 319 * Now look whether this region ends within the kernel. 320 */ 321 if (e > kernelstart && e <= kernelend) { 322 if (s >= kernelstart) 323 goto empty; 324 e = kernelstart; 325 } 326 /* 327 * Now page align the start and size of the region. 328 */ 329 s = round_page(s); 330 e = trunc_page(e); 331 if (e < s) 332 e = s; 333 sz = e - s; 334 printf("%08x-%08x = %x\n",s,e,sz); 335 /* 336 * Check whether some memory is left here. 337 */ 338 if (sz == 0) { 339 empty: 340 memmove(mp, mp + 1, 341 (cnt - (mp - avail)) * sizeof *mp); 342 cnt--; 343 mp--; 344 continue; 345 } 346 /* 347 * Do an insertion sort. 348 */ 349 npgs += btoc(sz); 350 for (mp1 = avail; mp1 < mp; mp1++) 351 if (s < mp1->start) 352 break; 353 if (mp1 < mp) { 354 memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1); 355 mp1->start = s; 356 mp1->size = sz; 357 } else { 358 mp->start = s; 359 mp->size = sz; 360 } 361 } 362 363 /* 364 * We cannot do pmap_steal_memory here, 365 * since we don't run with translation enabled yet. 366 */ 367 #ifndef MSGBUFADDR 368 /* 369 * allow for msgbuf 370 */ 371 sz = round_page(MSGBUFSIZE); 372 mp = NULL; 373 for (mp1 = avail; mp1->size; mp1++) 374 if (mp1->size >= sz) 375 mp = mp1; 376 if (mp == NULL) 377 panic("not enough memory?"); 378 379 npgs -= btoc(sz); 380 msgbuf_paddr = mp->start + mp->size - sz; 381 mp->size -= sz; 382 if (mp->size <= 0) 383 memmove(mp, mp + 1, (cnt - (mp - avail)) * sizeof *mp); 384 #endif 385 386 for (mp = avail; mp->size; mp++) 387 uvm_page_physload(atop(mp->start), atop(mp->start + mp->size), 388 atop(mp->start), atop(mp->start + mp->size), 389 VM_FREELIST_DEFAULT); 390 391 /* 392 * Initialize kernel pmap and hardware. 393 */ 394 /* Setup TLB pid allocator so it knows we alreadu using PID 1 */ 395 pmap_kernel()->pm_ctx = KERNEL_PID; 396 nextavail = avail->start; 397 398 399 evcnt_attach_static(&tlbmiss_ev); 400 evcnt_attach_static(&tlbhit_ev); 401 evcnt_attach_static(&tlbflush_ev); 402 evcnt_attach_static(&tlbenter_ev); 403 } 404 405 /* 406 * Restrict given range to physical memory 407 * 408 * (Used by /dev/mem) 409 */ 410 void 411 pmap_real_memory(paddr_t *start, psize_t *size) 412 { 413 struct mem_region *mp; 414 415 for (mp = mem; mp->size; mp++) { 416 if (*start + *size > mp->start && 417 *start < mp->start + mp->size) { 418 if (*start < mp->start) { 419 *size -= mp->start - *start; 420 *start = mp->start; 421 } 422 if (*start + *size > mp->start + mp->size) 423 *size = mp->start + mp->size - *start; 424 return; 425 } 426 } 427 *size = 0; 428 } 429 430 /* 431 * Initialize anything else for pmap handling. 432 * Called during vm_init(). 433 */ 434 void 435 pmap_init(void) 436 { 437 struct pv_entry *pv; 438 vsize_t sz; 439 vaddr_t addr; 440 int i, s; 441 int bank; 442 char *attr; 443 444 sz = (vsize_t)((sizeof(struct pv_entry) + 1) * npgs); 445 sz = round_page(sz); 446 addr = uvm_km_zalloc(kernel_map, sz); 447 s = splvm(); 448 pv = pv_table = (struct pv_entry *)addr; 449 for (i = npgs; --i >= 0;) 450 pv++->pv_pm = NULL; 451 pmap_attrib = (char *)pv; 452 memset(pv, 0, npgs); 453 454 pv = pv_table; 455 attr = pmap_attrib; 456 for (bank = 0; bank < vm_nphysseg; bank++) { 457 sz = vm_physmem[bank].end - vm_physmem[bank].start; 458 vm_physmem[bank].pmseg.pvent = pv; 459 vm_physmem[bank].pmseg.attrs = attr; 460 pv += sz; 461 attr += sz; 462 } 463 464 pmap_initialized = 1; 465 splx(s); 466 467 /* Setup a pool for additional pvlist structures */ 468 pool_init(&pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pv_entry", NULL); 469 } 470 471 /* 472 * How much virtual space is available to the kernel? 473 */ 474 void 475 pmap_virtual_space(vaddr_t *start, vaddr_t *end) 476 { 477 478 #if 0 479 /* 480 * Reserve one segment for kernel virtual memory 481 */ 482 *start = (vaddr_t)(KERNEL_SR << ADDR_SR_SHFT); 483 *end = *start + SEGMENT_LENGTH; 484 #else 485 *start = (vaddr_t) VM_MIN_KERNEL_ADDRESS; 486 *end = (vaddr_t) VM_MAX_KERNEL_ADDRESS; 487 #endif 488 } 489 490 #ifdef PMAP_GROWKERNEL 491 /* 492 * Preallocate kernel page tables to a specified VA. 493 * This simply loops through the first TTE for each 494 * page table from the beginning of the kernel pmap, 495 * reads the entry, and if the result is 496 * zero (either invalid entry or no page table) it stores 497 * a zero there, populating page tables in the process. 498 * This is not the most efficient technique but i don't 499 * expect it to be called that often. 500 */ 501 extern struct vm_page *vm_page_alloc1 __P((void)); 502 extern void vm_page_free1 __P((struct vm_page *)); 503 504 vaddr_t kbreak = VM_MIN_KERNEL_ADDRESS; 505 506 vaddr_t 507 pmap_growkernel(vaddr_t maxkvaddr) 508 { 509 int s; 510 int seg; 511 paddr_t pg; 512 struct pmap *pm = pmap_kernel(); 513 514 s = splvm(); 515 516 /* Align with the start of a page table */ 517 for (kbreak &= ~(PTMAP-1); kbreak < maxkvaddr; 518 kbreak += PTMAP) { 519 seg = STIDX(kbreak); 520 521 if (pte_find(pm, kbreak)) 522 continue; 523 524 if (uvm.page_init_done) { 525 pg = (paddr_t)VM_PAGE_TO_PHYS(vm_page_alloc1()); 526 } else { 527 if (!uvm_page_physget(&pg)) 528 panic("pmap_growkernel: no memory"); 529 } 530 if (!pg) 531 panic("pmap_growkernel: no pages"); 532 pmap_zero_page((paddr_t)pg); 533 534 /* XXX This is based on all phymem being addressable */ 535 pm->pm_ptbl[seg] = (u_int *)pg; 536 } 537 splx(s); 538 return (kbreak); 539 } 540 541 /* 542 * vm_page_alloc1: 543 * 544 * Allocate and return a memory cell with no associated object. 545 */ 546 struct vm_page * 547 vm_page_alloc1(void) 548 { 549 struct vm_page *pg; 550 551 pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE); 552 if (pg) { 553 pg->wire_count = 1; /* no mappings yet */ 554 pg->flags &= ~PG_BUSY; /* never busy */ 555 } 556 return pg; 557 } 558 559 /* 560 * vm_page_free1: 561 * 562 * Returns the given page to the free list, 563 * disassociating it with any VM object. 564 * 565 * Object and page must be locked prior to entry. 566 */ 567 void 568 vm_page_free1(struct vm_page *mem) 569 { 570 #ifdef DIAGNOSTIC 571 if (mem->flags != (PG_CLEAN|PG_FAKE)) { 572 printf("Freeing invalid page %p\n", mem); 573 printf("pa = %llx\n", (unsigned long long)VM_PAGE_TO_PHYS(mem)); 574 #ifdef DDB 575 Debugger(); 576 #endif 577 return; 578 } 579 #endif 580 mem->flags |= PG_BUSY; 581 mem->wire_count = 0; 582 uvm_pagefree(mem); 583 } 584 #endif 585 586 /* 587 * Create and return a physical map. 588 */ 589 struct pmap * 590 pmap_create(void) 591 { 592 struct pmap *pm; 593 594 pm = malloc(sizeof *pm, M_VMPMAP, M_WAITOK); 595 memset(pm, 0, sizeof *pm); 596 pm->pm_refs = 1; 597 return pm; 598 } 599 600 /* 601 * Add a reference to the given pmap. 602 */ 603 void 604 pmap_reference(struct pmap *pm) 605 { 606 607 pm->pm_refs++; 608 } 609 610 /* 611 * Retire the given pmap from service. 612 * Should only be called if the map contains no valid mappings. 613 */ 614 void 615 pmap_destroy(struct pmap *pm) 616 { 617 int i; 618 619 if (--pm->pm_refs > 0) { 620 return; 621 } 622 KASSERT(pm->pm_stats.resident_count == 0); 623 KASSERT(pm->pm_stats.wired_count == 0); 624 for (i = 0; i < STSZ; i++) 625 if (pm->pm_ptbl[i]) { 626 uvm_km_free(kernel_map, (vaddr_t)pm->pm_ptbl[i], 627 PAGE_SIZE); 628 pm->pm_ptbl[i] = NULL; 629 } 630 if (pm->pm_ctx) 631 ctx_free(pm); 632 free(pm, M_VMPMAP); 633 } 634 635 /* 636 * Copy the range specified by src_addr/len 637 * from the source map to the range dst_addr/len 638 * in the destination map. 639 * 640 * This routine is only advisory and need not do anything. 641 */ 642 void 643 pmap_copy(struct pmap *dst_pmap, struct pmap *src_pmap, vaddr_t dst_addr, 644 vsize_t len, vaddr_t src_addr) 645 { 646 } 647 648 /* 649 * Require that all active physical maps contain no 650 * incorrect entries NOW. 651 */ 652 void 653 pmap_update(struct pmap *pmap) 654 { 655 } 656 657 /* 658 * Garbage collects the physical map system for 659 * pages which are no longer used. 660 * Success need not be guaranteed -- that is, there 661 * may well be pages which are not referenced, but 662 * others may be collected. 663 * Called by the pageout daemon when pages are scarce. 664 */ 665 void 666 pmap_collect(struct pmap *pm) 667 { 668 } 669 670 /* 671 * Fill the given physical page with zeroes. 672 */ 673 void 674 pmap_zero_page(paddr_t pa) 675 { 676 677 #ifdef PPC_4XX_NOCACHE 678 memset((caddr_t)pa, 0, PAGE_SIZE); 679 #else 680 int i; 681 682 for (i = PAGE_SIZE/CACHELINESIZE; i > 0; i--) { 683 __asm __volatile ("dcbz 0,%0" :: "r"(pa)); 684 pa += CACHELINESIZE; 685 } 686 #endif 687 } 688 689 /* 690 * Copy the given physical source page to its destination. 691 */ 692 void 693 pmap_copy_page(paddr_t src, paddr_t dst) 694 { 695 696 memcpy((caddr_t)dst, (caddr_t)src, PAGE_SIZE); 697 dcache_flush_page(dst); 698 } 699 700 /* 701 * This returns whether this is the first mapping of a page. 702 */ 703 static inline int 704 pmap_enter_pv(struct pmap *pm, vaddr_t va, paddr_t pa, boolean_t wired) 705 { 706 struct pv_entry *pv, *npv = NULL; 707 int s; 708 709 if (!pmap_initialized) 710 return 0; 711 712 s = splvm(); 713 pv = pa_to_pv(pa); 714 if (!pv->pv_pm) { 715 /* 716 * No entries yet, use header as the first entry. 717 */ 718 pv->pv_va = va; 719 pv->pv_pm = pm; 720 pv->pv_next = NULL; 721 } else { 722 /* 723 * There is at least one other VA mapping this page. 724 * Place this entry after the header. 725 */ 726 npv = pool_get(&pv_pool, PR_WAITOK); 727 npv->pv_va = va; 728 npv->pv_pm = pm; 729 npv->pv_next = pv->pv_next; 730 pv->pv_next = npv; 731 } 732 if (wired) { 733 PV_WIRE(pv); 734 } 735 splx(s); 736 return (1); 737 } 738 739 static void 740 pmap_remove_pv(struct pmap *pm, vaddr_t va, paddr_t pa) 741 { 742 struct pv_entry *pv, *npv; 743 744 /* 745 * Remove from the PV table. 746 */ 747 pv = pa_to_pv(pa); 748 if (!pv) 749 return; 750 751 /* 752 * If it is the first entry on the list, it is actually 753 * in the header and we must copy the following entry up 754 * to the header. Otherwise we must search the list for 755 * the entry. In either case we free the now unused entry. 756 */ 757 if (pm == pv->pv_pm && PV_CMPVA(va, pv)) { 758 if (PV_ISWIRED(pv)) { 759 pm->pm_stats.wired_count--; 760 } 761 if ((npv = pv->pv_next)) { 762 *pv = *npv; 763 pool_put(&pv_pool, npv); 764 } else 765 pv->pv_pm = NULL; 766 } else { 767 for (; (npv = pv->pv_next) != NULL; pv = npv) 768 if (pm == npv->pv_pm && PV_CMPVA(va, npv)) 769 break; 770 if (npv) { 771 pv->pv_next = npv->pv_next; 772 if (PV_ISWIRED(npv)) { 773 pm->pm_stats.wired_count--; 774 } 775 pool_put(&pv_pool, npv); 776 } 777 } 778 } 779 780 /* 781 * Insert physical page at pa into the given pmap at virtual address va. 782 */ 783 int 784 pmap_enter(struct pmap *pm, vaddr_t va, paddr_t pa, vm_prot_t prot, int flags) 785 { 786 int s; 787 u_int tte; 788 int managed; 789 790 /* 791 * Have to remove any existing mapping first. 792 */ 793 pmap_remove(pm, va, va + PAGE_SIZE); 794 795 if (flags & PMAP_WIRED) 796 flags |= prot; 797 798 managed = 0; 799 if (vm_physseg_find(atop(pa), NULL) != -1) 800 managed = 1; 801 802 /* 803 * Generate TTE. 804 */ 805 tte = TTE_PA(pa); 806 /* XXXX -- need to support multiple page sizes. */ 807 tte |= TTE_SZ_16K; 808 #ifdef DIAGNOSTIC 809 if ((flags & (PME_NOCACHE | PME_WRITETHROUG)) == 810 (PME_NOCACHE | PME_WRITETHROUG)) 811 panic("pmap_enter: uncached & writethrough"); 812 #endif 813 if (flags & PME_NOCACHE) 814 /* Must be I/O mapping */ 815 tte |= TTE_I | TTE_G; 816 #ifdef PPC_4XX_NOCACHE 817 tte |= TTE_I; 818 #else 819 else if (flags & PME_WRITETHROUG) 820 /* Uncached and writethrough are not compatible */ 821 tte |= TTE_W; 822 #endif 823 if (pm == pmap_kernel()) 824 tte |= TTE_ZONE(ZONE_PRIV); 825 else 826 tte |= TTE_ZONE(ZONE_USER); 827 828 if (flags & VM_PROT_WRITE) 829 tte |= TTE_WR; 830 831 if (flags & VM_PROT_EXECUTE) 832 tte |= TTE_EX; 833 834 /* 835 * Now record mapping for later back-translation. 836 */ 837 if (pmap_initialized && managed) { 838 char *attr; 839 840 if (!pmap_enter_pv(pm, va, pa, flags & PMAP_WIRED)) { 841 /* Could not enter pv on a managed page */ 842 return 1; 843 } 844 845 /* Now set attributes. */ 846 attr = pa_to_attr(pa); 847 #ifdef DIAGNOSTIC 848 if (!attr) 849 panic("managed but no attr"); 850 #endif 851 if (flags & VM_PROT_ALL) 852 *attr |= PMAP_ATTR_REF; 853 if (flags & VM_PROT_WRITE) 854 *attr |= PMAP_ATTR_CHG; 855 } 856 857 s = splvm(); 858 859 /* Insert page into page table. */ 860 pte_enter(pm, va, tte); 861 862 /* If this is a real fault, enter it in the tlb */ 863 if (tte && ((flags & PMAP_WIRED) == 0)) { 864 ppc4xx_tlb_enter(pm->pm_ctx, va, tte); 865 } 866 splx(s); 867 868 /* Flush the real memory from the instruction cache. */ 869 if ((prot & VM_PROT_EXECUTE) && (tte & TTE_I) == 0) 870 __syncicache((void *)pa, PAGE_SIZE); 871 872 if (flags & PMAP_WIRED) 873 pm->pm_stats.wired_count++; 874 875 return 0; 876 } 877 878 void 879 pmap_unwire(struct pmap *pm, vaddr_t va) 880 { 881 struct pv_entry *pv, *npv; 882 paddr_t pa; 883 int s; 884 885 if (!pmap_extract(pm, va, &pa)) { 886 return; 887 } 888 889 pv = pa_to_pv(pa); 890 if (!pv) 891 return; 892 893 /* 894 * If it is the first entry on the list, it is actually 895 * in the header and we must copy the following entry up 896 * to the header. Otherwise we must search the list for 897 * the entry. In either case we free the now unused entry. 898 */ 899 s = splvm(); 900 for (npv = pv; (npv = pv->pv_next) != NULL; pv = npv) { 901 if (pm == npv->pv_pm && PV_CMPVA(va, npv)) { 902 if (PV_ISWIRED(npv)) { 903 PV_UNWIRE(npv); 904 pm->pm_stats.wired_count--; 905 } 906 break; 907 } 908 } 909 splx(s); 910 } 911 912 void 913 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot) 914 { 915 int s; 916 u_int tte; 917 struct pmap *pm = pmap_kernel(); 918 919 /* 920 * Have to remove any existing mapping first. 921 */ 922 923 /* 924 * Generate TTE. 925 * 926 * XXXX 927 * 928 * Since the kernel does not handle execution privileges properly, 929 * we will handle read and execute permissions together. 930 */ 931 tte = 0; 932 if (prot & VM_PROT_ALL) { 933 934 tte = TTE_PA(pa) | TTE_EX | TTE_ZONE(ZONE_PRIV); 935 /* XXXX -- need to support multiple page sizes. */ 936 tte |= TTE_SZ_16K; 937 #ifdef DIAGNOSTIC 938 if ((prot & (PME_NOCACHE | PME_WRITETHROUG)) == 939 (PME_NOCACHE | PME_WRITETHROUG)) 940 panic("pmap_kenter_pa: uncached & writethrough"); 941 #endif 942 if (prot & PME_NOCACHE) 943 /* Must be I/O mapping */ 944 tte |= TTE_I | TTE_G; 945 #ifdef PPC_4XX_NOCACHE 946 tte |= TTE_I; 947 #else 948 else if (prot & PME_WRITETHROUG) 949 /* Uncached and writethrough are not compatible */ 950 tte |= TTE_W; 951 #endif 952 if (prot & VM_PROT_WRITE) 953 tte |= TTE_WR; 954 } 955 956 s = splvm(); 957 958 /* Insert page into page table. */ 959 pte_enter(pm, va, tte); 960 splx(s); 961 } 962 963 void 964 pmap_kremove(vaddr_t va, vsize_t len) 965 { 966 967 while (len > 0) { 968 pte_enter(pmap_kernel(), va, 0); 969 va += PAGE_SIZE; 970 len -= PAGE_SIZE; 971 } 972 } 973 974 /* 975 * Remove the given range of mapping entries. 976 */ 977 void 978 pmap_remove(struct pmap *pm, vaddr_t va, vaddr_t endva) 979 { 980 int s; 981 paddr_t pa; 982 volatile u_int *ptp; 983 984 s = splvm(); 985 while (va < endva) { 986 987 if ((ptp = pte_find(pm, va)) && (pa = *ptp)) { 988 pa = TTE_PA(pa); 989 pmap_remove_pv(pm, va, pa); 990 *ptp = 0; 991 ppc4xx_tlb_flush(va, pm->pm_ctx); 992 pm->pm_stats.resident_count--; 993 } 994 va += PAGE_SIZE; 995 } 996 997 splx(s); 998 } 999 1000 /* 1001 * Get the physical page address for the given pmap/virtual address. 1002 */ 1003 boolean_t 1004 pmap_extract(struct pmap *pm, vaddr_t va, paddr_t *pap) 1005 { 1006 int seg = STIDX(va); 1007 int ptn = PTIDX(va); 1008 u_int pa = 0; 1009 int s; 1010 1011 s = splvm(); 1012 if (pm->pm_ptbl[seg] && (pa = pm->pm_ptbl[seg][ptn])) { 1013 *pap = TTE_PA(pa) | (va & PGOFSET); 1014 } 1015 splx(s); 1016 return (pa != 0); 1017 } 1018 1019 /* 1020 * Lower the protection on the specified range of this pmap. 1021 * 1022 * There are only two cases: either the protection is going to 0, 1023 * or it is going to read-only. 1024 */ 1025 void 1026 pmap_protect(struct pmap *pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot) 1027 { 1028 volatile u_int *ptp; 1029 int s, bic; 1030 1031 if ((prot & VM_PROT_READ) == 0) { 1032 pmap_remove(pm, sva, eva); 1033 return; 1034 } 1035 bic = 0; 1036 if ((prot & VM_PROT_WRITE) == 0) { 1037 bic |= TTE_WR; 1038 } 1039 if ((prot & VM_PROT_EXECUTE) == 0) { 1040 bic |= TTE_EX; 1041 } 1042 if (bic == 0) { 1043 return; 1044 } 1045 s = splvm(); 1046 while (sva < eva) { 1047 if ((ptp = pte_find(pm, sva)) != NULL) { 1048 *ptp &= ~bic; 1049 ppc4xx_tlb_flush(sva, pm->pm_ctx); 1050 } 1051 sva += PAGE_SIZE; 1052 } 1053 splx(s); 1054 } 1055 1056 boolean_t 1057 pmap_check_attr(struct vm_page *pg, u_int mask, int clear) 1058 { 1059 paddr_t pa; 1060 char *attr; 1061 int s, rv; 1062 1063 /* 1064 * First modify bits in cache. 1065 */ 1066 pa = VM_PAGE_TO_PHYS(pg); 1067 attr = pa_to_attr(pa); 1068 if (attr == NULL) 1069 return FALSE; 1070 1071 s = splvm(); 1072 rv = ((*attr & mask) != 0); 1073 if (clear) { 1074 *attr &= ~mask; 1075 pmap_page_protect(pg, mask == PMAP_ATTR_CHG ? VM_PROT_READ : 0); 1076 } 1077 splx(s); 1078 return rv; 1079 } 1080 1081 1082 /* 1083 * Lower the protection on the specified physical page. 1084 * 1085 * There are only two cases: either the protection is going to 0, 1086 * or it is going to read-only. 1087 */ 1088 void 1089 pmap_page_protect(struct vm_page *pg, vm_prot_t prot) 1090 { 1091 paddr_t pa = VM_PAGE_TO_PHYS(pg); 1092 vaddr_t va; 1093 struct pv_entry *pvh, *pv, *npv; 1094 struct pmap *pm; 1095 1096 pvh = pa_to_pv(pa); 1097 if (pvh == NULL) 1098 return; 1099 1100 /* Handle extra pvs which may be deleted in the operation */ 1101 for (pv = pvh->pv_next; pv; pv = npv) { 1102 npv = pv->pv_next; 1103 1104 pm = pv->pv_pm; 1105 va = pv->pv_va; 1106 pmap_protect(pm, va, va + PAGE_SIZE, prot); 1107 } 1108 /* Now check the head pv */ 1109 if (pvh->pv_pm) { 1110 pv = pvh; 1111 pm = pv->pv_pm; 1112 va = pv->pv_va; 1113 pmap_protect(pm, va, va + PAGE_SIZE, prot); 1114 } 1115 } 1116 1117 /* 1118 * Activate the address space for the specified process. If the process 1119 * is the current process, load the new MMU context. 1120 */ 1121 void 1122 pmap_activate(struct lwp *l) 1123 { 1124 #if 0 1125 struct pcb *pcb = &l->l_proc->p_addr->u_pcb; 1126 pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap; 1127 1128 /* 1129 * XXX Normally performed in cpu_fork(). 1130 */ 1131 printf("pmap_activate(%p), pmap=%p\n",l,pmap); 1132 pcb->pcb_pm = pmap; 1133 #endif 1134 } 1135 1136 /* 1137 * Deactivate the specified process's address space. 1138 */ 1139 void 1140 pmap_deactivate(struct lwp *l) 1141 { 1142 } 1143 1144 /* 1145 * Synchronize caches corresponding to [addr, addr+len) in p. 1146 */ 1147 void 1148 pmap_procwr(struct proc *p, vaddr_t va, size_t len) 1149 { 1150 struct pmap *pm = p->p_vmspace->vm_map.pmap; 1151 int msr, ctx, opid, step; 1152 1153 step = CACHELINESIZE; 1154 1155 /* 1156 * Need to turn off IMMU and switch to user context. 1157 * (icbi uses DMMU). 1158 */ 1159 if (!(ctx = pm->pm_ctx)) { 1160 /* No context -- assign it one */ 1161 ctx_alloc(pm); 1162 ctx = pm->pm_ctx; 1163 } 1164 __asm __volatile("mfmsr %0;" 1165 "li %1, %7;" 1166 "andc %1,%0,%1;" 1167 "mtmsr %1;" 1168 "sync;isync;" 1169 "mfpid %1;" 1170 "mtpid %2;" 1171 "sync; isync;" 1172 "1:" 1173 "dcbf 0,%3;" 1174 "icbi 0,%3;" 1175 "add %3,%3,%5;" 1176 "addc. %4,%4,%6;" 1177 "bge 1b;" 1178 "mtpid %1;" 1179 "mtmsr %0;" 1180 "sync; isync" 1181 : "=&r" (msr), "=&r" (opid) 1182 : "r" (ctx), "r" (va), "r" (len), "r" (step), "r" (-step), 1183 "K" (PSL_IR | PSL_DR)); 1184 } 1185 1186 1187 /* This has to be done in real mode !!! */ 1188 void 1189 ppc4xx_tlb_flush(vaddr_t va, int pid) 1190 { 1191 u_long i, found; 1192 u_long msr; 1193 1194 /* If there's no context then it can't be mapped. */ 1195 if (!pid) 1196 return; 1197 1198 asm("mfpid %1;" /* Save PID */ 1199 "mfmsr %2;" /* Save MSR */ 1200 "li %0,0;" /* Now clear MSR */ 1201 "mtmsr %0;" 1202 "mtpid %4;" /* Set PID */ 1203 "sync;" 1204 "tlbsx. %0,0,%3;" /* Search TLB */ 1205 "sync;" 1206 "mtpid %1;" /* Restore PID */ 1207 "mtmsr %2;" /* Restore MSR */ 1208 "sync;isync;" 1209 "li %1,1;" 1210 "beq 1f;" 1211 "li %1,0;" 1212 "1:" 1213 : "=&r" (i), "=&r" (found), "=&r" (msr) 1214 : "r" (va), "r" (pid)); 1215 if (found && !TLB_LOCKED(i)) { 1216 1217 /* Now flush translation */ 1218 asm volatile( 1219 "tlbwe %0,%1,0;" 1220 "sync;isync;" 1221 : : "r" (0), "r" (i)); 1222 1223 tlb_info[i].ti_ctx = 0; 1224 tlb_info[i].ti_flags = 0; 1225 tlbnext = i; 1226 /* Successful flushes */ 1227 tlbflush_ev.ev_count++; 1228 } 1229 } 1230 1231 void 1232 ppc4xx_tlb_flush_all(void) 1233 { 1234 u_long i; 1235 1236 for (i = 0; i < NTLB; i++) 1237 if (!TLB_LOCKED(i)) { 1238 asm volatile( 1239 "tlbwe %0,%1,0;" 1240 "sync;isync;" 1241 : : "r" (0), "r" (i)); 1242 tlb_info[i].ti_ctx = 0; 1243 tlb_info[i].ti_flags = 0; 1244 } 1245 1246 asm volatile("sync;isync"); 1247 } 1248 1249 /* Find a TLB entry to evict. */ 1250 static int 1251 ppc4xx_tlb_find_victim(void) 1252 { 1253 int flags; 1254 1255 for (;;) { 1256 if (++tlbnext >= NTLB) 1257 tlbnext = TLB_NRESERVED; 1258 flags = tlb_info[tlbnext].ti_flags; 1259 if (!(flags & TLBF_USED) || 1260 (flags & (TLBF_LOCKED | TLBF_REF)) == 0) { 1261 u_long va, stack = (u_long)&va; 1262 1263 if (!((tlb_info[tlbnext].ti_va ^ stack) & (~PGOFSET)) && 1264 (tlb_info[tlbnext].ti_ctx == KERNEL_PID) && 1265 (flags & TLBF_USED)) { 1266 /* Kernel stack page */ 1267 flags |= TLBF_USED; 1268 tlb_info[tlbnext].ti_flags = flags; 1269 } else { 1270 /* Found it! */ 1271 return (tlbnext); 1272 } 1273 } else { 1274 tlb_info[tlbnext].ti_flags = (flags & ~TLBF_REF); 1275 } 1276 } 1277 } 1278 1279 void 1280 ppc4xx_tlb_enter(int ctx, vaddr_t va, u_int pte) 1281 { 1282 u_long th, tl, idx; 1283 tlbpid_t pid; 1284 u_short msr; 1285 paddr_t pa; 1286 int s, sz; 1287 1288 tlbenter_ev.ev_count++; 1289 1290 sz = (pte & TTE_SZ_MASK) >> TTE_SZ_SHIFT; 1291 pa = (pte & TTE_RPN_MASK(sz)); 1292 th = (va & TLB_EPN_MASK) | (sz << TLB_SIZE_SHFT) | TLB_VALID; 1293 tl = (pte & ~TLB_RPN_MASK) | pa; 1294 tl |= ppc4xx_tlbflags(va, pa); 1295 1296 s = splhigh(); 1297 idx = ppc4xx_tlb_find_victim(); 1298 1299 #ifdef DIAGNOSTIC 1300 if ((idx < TLB_NRESERVED) || (idx >= NTLB)) { 1301 panic("ppc4xx_tlb_enter: replacing entry %ld", idx); 1302 } 1303 #endif 1304 1305 tlb_info[idx].ti_va = (va & TLB_EPN_MASK); 1306 tlb_info[idx].ti_ctx = ctx; 1307 tlb_info[idx].ti_flags = TLBF_USED | TLBF_REF; 1308 1309 asm volatile( 1310 "mfmsr %0;" /* Save MSR */ 1311 "li %1,0;" 1312 "tlbwe %1,%3,0;" /* Invalidate old entry. */ 1313 "mtmsr %1;" /* Clear MSR */ 1314 "mfpid %1;" /* Save old PID */ 1315 "mtpid %2;" /* Load translation ctx */ 1316 "sync; isync;" 1317 #ifdef DEBUG 1318 "andi. %3,%3,63;" 1319 "tweqi %3,0;" /* XXXXX DEBUG trap on index 0 */ 1320 #endif 1321 "tlbwe %4,%3,1; tlbwe %5,%3,0;" /* Set TLB */ 1322 "sync; isync;" 1323 "mtpid %1; mtmsr %0;" /* Restore PID and MSR */ 1324 "sync; isync;" 1325 : "=&r" (msr), "=&r" (pid) 1326 : "r" (ctx), "r" (idx), "r" (tl), "r" (th)); 1327 splx(s); 1328 } 1329 1330 void 1331 ppc4xx_tlb_unpin(int i) 1332 { 1333 1334 if (i == -1) 1335 for (i = 0; i < TLB_NRESERVED; i++) 1336 tlb_info[i].ti_flags &= ~TLBF_LOCKED; 1337 else 1338 tlb_info[i].ti_flags &= ~TLBF_LOCKED; 1339 } 1340 1341 void 1342 ppc4xx_tlb_init(void) 1343 { 1344 int i; 1345 1346 /* Mark reserved TLB entries */ 1347 for (i = 0; i < TLB_NRESERVED; i++) { 1348 tlb_info[i].ti_flags = TLBF_LOCKED | TLBF_USED; 1349 tlb_info[i].ti_ctx = KERNEL_PID; 1350 } 1351 1352 /* Setup security zones */ 1353 /* Z0 - accessible by kernel only if TLB entry permissions allow 1354 * Z1,Z2 - access is controlled by TLB entry permissions 1355 * Z3 - full access regardless of TLB entry permissions 1356 */ 1357 1358 asm volatile( 1359 "mtspr %0,%1;" 1360 "sync;" 1361 :: "K"(SPR_ZPR), "r" (0x1b000000)); 1362 } 1363 1364 1365 /* 1366 * We should pass the ctx in from trap code. 1367 */ 1368 int 1369 pmap_tlbmiss(vaddr_t va, int ctx) 1370 { 1371 volatile u_int *pte; 1372 u_long tte; 1373 1374 tlbmiss_ev.ev_count++; 1375 1376 /* 1377 * XXXX We will reserve 0-0x80000000 for va==pa mappings. 1378 */ 1379 if (ctx != KERNEL_PID || (va & 0x80000000)) { 1380 pte = pte_find((struct pmap *)ctxbusy[ctx], va); 1381 if (pte == NULL) { 1382 /* Map unmanaged addresses directly for kernel access */ 1383 return 1; 1384 } 1385 tte = *pte; 1386 if (tte == 0) { 1387 return 1; 1388 } 1389 } else { 1390 /* Create a 16MB writable mapping. */ 1391 #ifdef PPC_4XX_NOCACHE 1392 tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_I | TTE_WR; 1393 #else 1394 tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_WR; 1395 #endif 1396 } 1397 tlbhit_ev.ev_count++; 1398 ppc4xx_tlb_enter(ctx, va, tte); 1399 1400 return 0; 1401 } 1402 1403 /* 1404 * Flush all the entries matching a context from the TLB. 1405 */ 1406 static int 1407 ctx_flush(int cnum) 1408 { 1409 int i; 1410 1411 /* We gotta steal this context */ 1412 for (i = TLB_NRESERVED; i < NTLB; i++) { 1413 if (tlb_info[i].ti_ctx == cnum) { 1414 /* Can't steal ctx if it has a locked entry. */ 1415 if (TLB_LOCKED(i)) { 1416 #ifdef DIAGNOSTIC 1417 printf("ctx_flush: can't invalidate " 1418 "locked mapping %d " 1419 "for context %d\n", i, cnum); 1420 #ifdef DDB 1421 Debugger(); 1422 #endif 1423 #endif 1424 return (1); 1425 } 1426 #ifdef DIAGNOSTIC 1427 if (i < TLB_NRESERVED) 1428 panic("TLB entry %d not locked", i); 1429 #endif 1430 /* Invalidate particular TLB entry regardless of locked status */ 1431 asm volatile("tlbwe %0,%1,0" : :"r"(0),"r"(i)); 1432 tlb_info[i].ti_flags = 0; 1433 } 1434 } 1435 return (0); 1436 } 1437 1438 /* 1439 * Allocate a context. If necessary, steal one from someone else. 1440 * 1441 * The new context is flushed from the TLB before returning. 1442 */ 1443 int 1444 ctx_alloc(struct pmap *pm) 1445 { 1446 int s, cnum; 1447 static int next = MINCTX; 1448 1449 if (pm == pmap_kernel()) { 1450 #ifdef DIAGNOSTIC 1451 printf("ctx_alloc: kernel pmap!\n"); 1452 #endif 1453 return (0); 1454 } 1455 s = splvm(); 1456 1457 /* Find a likely context. */ 1458 cnum = next; 1459 do { 1460 if ((++cnum) > NUMCTX) 1461 cnum = MINCTX; 1462 } while (ctxbusy[cnum] != NULL && cnum != next); 1463 1464 /* Now clean it out */ 1465 oops: 1466 if (cnum < MINCTX) 1467 cnum = MINCTX; /* Never steal ctx 0 or 1 */ 1468 if (ctx_flush(cnum)) { 1469 /* oops -- something's wired. */ 1470 if ((++cnum) > NUMCTX) 1471 cnum = MINCTX; 1472 goto oops; 1473 } 1474 1475 if (ctxbusy[cnum]) { 1476 #ifdef DEBUG 1477 /* We should identify this pmap and clear it */ 1478 printf("Warning: stealing context %d\n", cnum); 1479 #endif 1480 ctxbusy[cnum]->pm_ctx = 0; 1481 } 1482 ctxbusy[cnum] = pm; 1483 next = cnum; 1484 splx(s); 1485 pm->pm_ctx = cnum; 1486 1487 return cnum; 1488 } 1489 1490 /* 1491 * Give away a context. 1492 */ 1493 void 1494 ctx_free(struct pmap *pm) 1495 { 1496 int oldctx; 1497 1498 oldctx = pm->pm_ctx; 1499 1500 if (oldctx == 0) 1501 panic("ctx_free: freeing kernel context"); 1502 #ifdef DIAGNOSTIC 1503 if (ctxbusy[oldctx] == 0) 1504 printf("ctx_free: freeing free context %d\n", oldctx); 1505 if (ctxbusy[oldctx] != pm) { 1506 printf("ctx_free: freeing someone esle's context\n " 1507 "ctxbusy[%d] = %p, pm->pm_ctx = %p\n", 1508 oldctx, (void *)(u_long)ctxbusy[oldctx], pm); 1509 #ifdef DDB 1510 Debugger(); 1511 #endif 1512 } 1513 #endif 1514 /* We should verify it has not been stolen and reallocated... */ 1515 ctxbusy[oldctx] = NULL; 1516 ctx_flush(oldctx); 1517 } 1518 1519 1520 #ifdef DEBUG 1521 /* 1522 * Test ref/modify handling. 1523 */ 1524 void pmap_testout __P((void)); 1525 void 1526 pmap_testout() 1527 { 1528 vaddr_t va; 1529 volatile int *loc; 1530 int val = 0; 1531 paddr_t pa; 1532 struct vm_page *pg; 1533 int ref, mod; 1534 1535 /* Allocate a page */ 1536 va = (vaddr_t)uvm_km_zalloc(kernel_map, PAGE_SIZE); 1537 loc = (int*)va; 1538 1539 pmap_extract(pmap_kernel(), va, &pa); 1540 pg = PHYS_TO_VM_PAGE(pa); 1541 pmap_unwire(pmap_kernel(), va); 1542 1543 pmap_remove(pmap_kernel(), va, va+1); 1544 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0); 1545 pmap_update(pmap_kernel()); 1546 1547 /* Now clear reference and modify */ 1548 ref = pmap_clear_reference(pg); 1549 mod = pmap_clear_modify(pg); 1550 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1551 (void *)(u_long)va, (long)pa, 1552 ref, mod); 1553 1554 /* Check it's properly cleared */ 1555 ref = pmap_is_referenced(pg); 1556 mod = pmap_is_modified(pg); 1557 printf("Checking cleared page: ref %d, mod %d\n", 1558 ref, mod); 1559 1560 /* Reference page */ 1561 val = *loc; 1562 1563 ref = pmap_is_referenced(pg); 1564 mod = pmap_is_modified(pg); 1565 printf("Referenced page: ref %d, mod %d val %x\n", 1566 ref, mod, val); 1567 1568 /* Now clear reference and modify */ 1569 ref = pmap_clear_reference(pg); 1570 mod = pmap_clear_modify(pg); 1571 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1572 (void *)(u_long)va, (long)pa, 1573 ref, mod); 1574 1575 /* Modify page */ 1576 *loc = 1; 1577 1578 ref = pmap_is_referenced(pg); 1579 mod = pmap_is_modified(pg); 1580 printf("Modified page: ref %d, mod %d\n", 1581 ref, mod); 1582 1583 /* Now clear reference and modify */ 1584 ref = pmap_clear_reference(pg); 1585 mod = pmap_clear_modify(pg); 1586 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1587 (void *)(u_long)va, (long)pa, 1588 ref, mod); 1589 1590 /* Check it's properly cleared */ 1591 ref = pmap_is_referenced(pg); 1592 mod = pmap_is_modified(pg); 1593 printf("Checking cleared page: ref %d, mod %d\n", 1594 ref, mod); 1595 1596 /* Modify page */ 1597 *loc = 1; 1598 1599 ref = pmap_is_referenced(pg); 1600 mod = pmap_is_modified(pg); 1601 printf("Modified page: ref %d, mod %d\n", 1602 ref, mod); 1603 1604 /* Check pmap_protect() */ 1605 pmap_protect(pmap_kernel(), va, va+1, VM_PROT_READ); 1606 pmap_update(pmap_kernel()); 1607 ref = pmap_is_referenced(pg); 1608 mod = pmap_is_modified(pg); 1609 printf("pmap_protect(VM_PROT_READ): ref %d, mod %d\n", 1610 ref, mod); 1611 1612 /* Now clear reference and modify */ 1613 ref = pmap_clear_reference(pg); 1614 mod = pmap_clear_modify(pg); 1615 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1616 (void *)(u_long)va, (long)pa, 1617 ref, mod); 1618 1619 /* Reference page */ 1620 val = *loc; 1621 1622 ref = pmap_is_referenced(pg); 1623 mod = pmap_is_modified(pg); 1624 printf("Referenced page: ref %d, mod %d val %x\n", 1625 ref, mod, val); 1626 1627 /* Now clear reference and modify */ 1628 ref = pmap_clear_reference(pg); 1629 mod = pmap_clear_modify(pg); 1630 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1631 (void *)(u_long)va, (long)pa, 1632 ref, mod); 1633 1634 /* Modify page */ 1635 #if 0 1636 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0); 1637 pmap_update(pmap_kernel()); 1638 #endif 1639 *loc = 1; 1640 1641 ref = pmap_is_referenced(pg); 1642 mod = pmap_is_modified(pg); 1643 printf("Modified page: ref %d, mod %d\n", 1644 ref, mod); 1645 1646 /* Check pmap_protect() */ 1647 pmap_protect(pmap_kernel(), va, va+1, VM_PROT_NONE); 1648 pmap_update(pmap_kernel()); 1649 ref = pmap_is_referenced(pg); 1650 mod = pmap_is_modified(pg); 1651 printf("pmap_protect(): ref %d, mod %d\n", 1652 ref, mod); 1653 1654 /* Now clear reference and modify */ 1655 ref = pmap_clear_reference(pg); 1656 mod = pmap_clear_modify(pg); 1657 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1658 (void *)(u_long)va, (long)pa, 1659 ref, mod); 1660 1661 /* Reference page */ 1662 val = *loc; 1663 1664 ref = pmap_is_referenced(pg); 1665 mod = pmap_is_modified(pg); 1666 printf("Referenced page: ref %d, mod %d val %x\n", 1667 ref, mod, val); 1668 1669 /* Now clear reference and modify */ 1670 ref = pmap_clear_reference(pg); 1671 mod = pmap_clear_modify(pg); 1672 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1673 (void *)(u_long)va, (long)pa, 1674 ref, mod); 1675 1676 /* Modify page */ 1677 #if 0 1678 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0); 1679 pmap_update(pmap_kernel()); 1680 #endif 1681 *loc = 1; 1682 1683 ref = pmap_is_referenced(pg); 1684 mod = pmap_is_modified(pg); 1685 printf("Modified page: ref %d, mod %d\n", 1686 ref, mod); 1687 1688 /* Check pmap_pag_protect() */ 1689 pmap_page_protect(pg, VM_PROT_READ); 1690 ref = pmap_is_referenced(pg); 1691 mod = pmap_is_modified(pg); 1692 printf("pmap_page_protect(VM_PROT_READ): ref %d, mod %d\n", 1693 ref, mod); 1694 1695 /* Now clear reference and modify */ 1696 ref = pmap_clear_reference(pg); 1697 mod = pmap_clear_modify(pg); 1698 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1699 (void *)(u_long)va, (long)pa, 1700 ref, mod); 1701 1702 /* Reference page */ 1703 val = *loc; 1704 1705 ref = pmap_is_referenced(pg); 1706 mod = pmap_is_modified(pg); 1707 printf("Referenced page: ref %d, mod %d val %x\n", 1708 ref, mod, val); 1709 1710 /* Now clear reference and modify */ 1711 ref = pmap_clear_reference(pg); 1712 mod = pmap_clear_modify(pg); 1713 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1714 (void *)(u_long)va, (long)pa, 1715 ref, mod); 1716 1717 /* Modify page */ 1718 #if 0 1719 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0); 1720 pmap_update(pmap_kernel()); 1721 #endif 1722 *loc = 1; 1723 1724 ref = pmap_is_referenced(pg); 1725 mod = pmap_is_modified(pg); 1726 printf("Modified page: ref %d, mod %d\n", 1727 ref, mod); 1728 1729 /* Check pmap_pag_protect() */ 1730 pmap_page_protect(pg, VM_PROT_NONE); 1731 ref = pmap_is_referenced(pg); 1732 mod = pmap_is_modified(pg); 1733 printf("pmap_page_protect(): ref %d, mod %d\n", 1734 ref, mod); 1735 1736 /* Now clear reference and modify */ 1737 ref = pmap_clear_reference(pg); 1738 mod = pmap_clear_modify(pg); 1739 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1740 (void *)(u_long)va, (long)pa, 1741 ref, mod); 1742 1743 1744 /* Reference page */ 1745 val = *loc; 1746 1747 ref = pmap_is_referenced(pg); 1748 mod = pmap_is_modified(pg); 1749 printf("Referenced page: ref %d, mod %d val %x\n", 1750 ref, mod, val); 1751 1752 /* Now clear reference and modify */ 1753 ref = pmap_clear_reference(pg); 1754 mod = pmap_clear_modify(pg); 1755 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1756 (void *)(u_long)va, (long)pa, 1757 ref, mod); 1758 1759 /* Modify page */ 1760 #if 0 1761 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0); 1762 pmap_update(pmap_kernel()); 1763 #endif 1764 *loc = 1; 1765 1766 ref = pmap_is_referenced(pg); 1767 mod = pmap_is_modified(pg); 1768 printf("Modified page: ref %d, mod %d\n", 1769 ref, mod); 1770 1771 /* Unmap page */ 1772 pmap_remove(pmap_kernel(), va, va+1); 1773 pmap_update(pmap_kernel()); 1774 ref = pmap_is_referenced(pg); 1775 mod = pmap_is_modified(pg); 1776 printf("Unmapped page: ref %d, mod %d\n", ref, mod); 1777 1778 /* Now clear reference and modify */ 1779 ref = pmap_clear_reference(pg); 1780 mod = pmap_clear_modify(pg); 1781 printf("Clearing page va %p pa %lx: ref %d, mod %d\n", 1782 (void *)(u_long)va, (long)pa, ref, mod); 1783 1784 /* Check it's properly cleared */ 1785 ref = pmap_is_referenced(pg); 1786 mod = pmap_is_modified(pg); 1787 printf("Checking cleared page: ref %d, mod %d\n", 1788 ref, mod); 1789 1790 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 1791 VM_PROT_ALL|PMAP_WIRED); 1792 uvm_km_free(kernel_map, (vaddr_t)va, PAGE_SIZE); 1793 } 1794 #endif 1795