1 /* $NetBSD: pmap.c,v 1.79 2003/04/01 15:28:41 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jeremy Cooper. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * XXX These comments aren't quite accurate. Need to change. 41 * The sun3x uses the MC68851 Memory Management Unit, which is built 42 * into the CPU. The 68851 maps virtual to physical addresses using 43 * a multi-level table lookup, which is stored in the very memory that 44 * it maps. The number of levels of lookup is configurable from one 45 * to four. In this implementation, we use three, named 'A' through 'C'. 46 * 47 * The MMU translates virtual addresses into physical addresses by 48 * traversing these tables in a proccess called a 'table walk'. The most 49 * significant 7 bits of the Virtual Address ('VA') being translated are 50 * used as an index into the level A table, whose base in physical memory 51 * is stored in a special MMU register, the 'CPU Root Pointer' or CRP. The 52 * address found at that index in the A table is used as the base 53 * address for the next table, the B table. The next six bits of the VA are 54 * used as an index into the B table, which in turn gives the base address 55 * of the third and final C table. 56 * 57 * The next six bits of the VA are used as an index into the C table to 58 * locate a Page Table Entry (PTE). The PTE is a physical address in memory 59 * to which the remaining 13 bits of the VA are added, producing the 60 * mapped physical address. 61 * 62 * To map the entire memory space in this manner would require 2114296 bytes 63 * of page tables per process - quite expensive. Instead we will 64 * allocate a fixed but considerably smaller space for the page tables at 65 * the time the VM system is initialized. When the pmap code is asked by 66 * the kernel to map a VA to a PA, it allocates tables as needed from this 67 * pool. When there are no more tables in the pool, tables are stolen 68 * from the oldest mapped entries in the tree. This is only possible 69 * because all memory mappings are stored in the kernel memory map 70 * structures, independent of the pmap structures. A VA which references 71 * one of these invalidated maps will cause a page fault. The kernel 72 * will determine that the page fault was caused by a task using a valid 73 * VA, but for some reason (which does not concern it), that address was 74 * not mapped. It will ask the pmap code to re-map the entry and then 75 * it will resume executing the faulting task. 76 * 77 * In this manner the most efficient use of the page table space is 78 * achieved. Tasks which do not execute often will have their tables 79 * stolen and reused by tasks which execute more frequently. The best 80 * size for the page table pool will probably be determined by 81 * experimentation. 82 * 83 * You read all of the comments so far. Good for you. 84 * Now go play! 85 */ 86 87 /*** A Note About the 68851 Address Translation Cache 88 * The MC68851 has a 64 entry cache, called the Address Translation Cache 89 * or 'ATC'. This cache stores the most recently used page descriptors 90 * accessed by the MMU when it does translations. Using a marker called a 91 * 'task alias' the MMU can store the descriptors from 8 different table 92 * spaces concurrently. The task alias is associated with the base 93 * address of the level A table of that address space. When an address 94 * space is currently active (the CRP currently points to its A table) 95 * the only cached descriptors that will be obeyed are ones which have a 96 * matching task alias of the current space associated with them. 97 * 98 * Since the cache is always consulted before any table lookups are done, 99 * it is important that it accurately reflect the state of the MMU tables. 100 * Whenever a change has been made to a table that has been loaded into 101 * the MMU, the code must be sure to flush any cached entries that are 102 * affected by the change. These instances are documented in the code at 103 * various points. 104 */ 105 /*** A Note About the Note About the 68851 Address Translation Cache 106 * 4 months into this code I discovered that the sun3x does not have 107 * a MC68851 chip. Instead, it has a version of this MMU that is part of the 108 * the 68030 CPU. 109 * All though it behaves very similarly to the 68851, it only has 1 task 110 * alias and a 22 entry cache. So sadly (or happily), the first paragraph 111 * of the previous note does not apply to the sun3x pmap. 112 */ 113 114 #include "opt_ddb.h" 115 116 #include <sys/param.h> 117 #include <sys/systm.h> 118 #include <sys/proc.h> 119 #include <sys/malloc.h> 120 #include <sys/pool.h> 121 #include <sys/user.h> 122 #include <sys/queue.h> 123 #include <sys/kcore.h> 124 125 #include <uvm/uvm.h> 126 127 #include <machine/cpu.h> 128 #include <machine/kcore.h> 129 #include <machine/mon.h> 130 #include <machine/pmap.h> 131 #include <machine/pte.h> 132 #include <machine/vmparam.h> 133 #include <m68k/cacheops.h> 134 135 #include <sun3/sun3/cache.h> 136 #include <sun3/sun3/machdep.h> 137 138 #include "pmap_pvt.h" 139 140 /* XXX - What headers declare these? */ 141 extern struct pcb *curpcb; 142 extern int physmem; 143 144 /* Defined in locore.s */ 145 extern char kernel_text[]; 146 147 /* Defined by the linker */ 148 extern char etext[], edata[], end[]; 149 extern char *esym; /* DDB */ 150 151 /*************************** DEBUGGING DEFINITIONS *********************** 152 * Macros, preprocessor defines and variables used in debugging can make * 153 * code hard to read. Anything used exclusively for debugging purposes * 154 * is defined here to avoid having such mess scattered around the file. * 155 *************************************************************************/ 156 #ifdef PMAP_DEBUG 157 /* 158 * To aid the debugging process, macros should be expanded into smaller steps 159 * that accomplish the same goal, yet provide convenient places for placing 160 * breakpoints. When this code is compiled with PMAP_DEBUG mode defined, the 161 * 'INLINE' keyword is defined to an empty string. This way, any function 162 * defined to be a 'static INLINE' will become 'outlined' and compiled as 163 * a separate function, which is much easier to debug. 164 */ 165 #define INLINE /* nothing */ 166 167 /* 168 * It is sometimes convenient to watch the activity of a particular table 169 * in the system. The following variables are used for that purpose. 170 */ 171 a_tmgr_t *pmap_watch_atbl = 0; 172 b_tmgr_t *pmap_watch_btbl = 0; 173 c_tmgr_t *pmap_watch_ctbl = 0; 174 175 int pmap_debug = 0; 176 #define DPRINT(args) if (pmap_debug) printf args 177 178 #else /********** Stuff below is defined if NOT debugging **************/ 179 180 #define INLINE inline 181 #define DPRINT(args) /* nada */ 182 183 #endif /* PMAP_DEBUG */ 184 /*********************** END OF DEBUGGING DEFINITIONS ********************/ 185 186 /*** Management Structure - Memory Layout 187 * For every MMU table in the sun3x pmap system there must be a way to 188 * manage it; we must know which process is using it, what other tables 189 * depend on it, and whether or not it contains any locked pages. This 190 * is solved by the creation of 'table management' or 'tmgr' 191 * structures. One for each MMU table in the system. 192 * 193 * MAP OF MEMORY USED BY THE PMAP SYSTEM 194 * 195 * towards lower memory 196 * kernAbase -> +-------------------------------------------------------+ 197 * | Kernel MMU A level table | 198 * kernBbase -> +-------------------------------------------------------+ 199 * | Kernel MMU B level tables | 200 * kernCbase -> +-------------------------------------------------------+ 201 * | | 202 * | Kernel MMU C level tables | 203 * | | 204 * mmuCbase -> +-------------------------------------------------------+ 205 * | User MMU C level tables | 206 * mmuAbase -> +-------------------------------------------------------+ 207 * | | 208 * | User MMU A level tables | 209 * | | 210 * mmuBbase -> +-------------------------------------------------------+ 211 * | User MMU B level tables | 212 * tmgrAbase -> +-------------------------------------------------------+ 213 * | TMGR A level table structures | 214 * tmgrBbase -> +-------------------------------------------------------+ 215 * | TMGR B level table structures | 216 * tmgrCbase -> +-------------------------------------------------------+ 217 * | TMGR C level table structures | 218 * pvbase -> +-------------------------------------------------------+ 219 * | Physical to Virtual mapping table (list heads) | 220 * pvebase -> +-------------------------------------------------------+ 221 * | Physical to Virtual mapping table (list elements) | 222 * | | 223 * +-------------------------------------------------------+ 224 * towards higher memory 225 * 226 * For every A table in the MMU A area, there will be a corresponding 227 * a_tmgr structure in the TMGR A area. The same will be true for 228 * the B and C tables. This arrangement will make it easy to find the 229 * controling tmgr structure for any table in the system by use of 230 * (relatively) simple macros. 231 */ 232 233 /* 234 * Global variables for storing the base addresses for the areas 235 * labeled above. 236 */ 237 static vaddr_t kernAphys; 238 static mmu_long_dte_t *kernAbase; 239 static mmu_short_dte_t *kernBbase; 240 static mmu_short_pte_t *kernCbase; 241 static mmu_short_pte_t *mmuCbase; 242 static mmu_short_dte_t *mmuBbase; 243 static mmu_long_dte_t *mmuAbase; 244 static a_tmgr_t *Atmgrbase; 245 static b_tmgr_t *Btmgrbase; 246 static c_tmgr_t *Ctmgrbase; 247 static pv_t *pvbase; 248 static pv_elem_t *pvebase; 249 struct pmap kernel_pmap; 250 251 /* 252 * This holds the CRP currently loaded into the MMU. 253 */ 254 struct mmu_rootptr kernel_crp; 255 256 /* 257 * Just all around global variables. 258 */ 259 static TAILQ_HEAD(a_pool_head_struct, a_tmgr_struct) a_pool; 260 static TAILQ_HEAD(b_pool_head_struct, b_tmgr_struct) b_pool; 261 static TAILQ_HEAD(c_pool_head_struct, c_tmgr_struct) c_pool; 262 263 264 /* 265 * Flags used to mark the safety/availability of certain operations or 266 * resources. 267 */ 268 static boolean_t bootstrap_alloc_enabled = FALSE; /*Safe to use pmap_bootstrap_alloc().*/ 269 int tmp_vpages_inuse; /* Temporary virtual pages are in use */ 270 271 /* 272 * XXX: For now, retain the traditional variables that were 273 * used in the old pmap/vm interface (without NONCONTIG). 274 */ 275 /* Kernel virtual address space available: */ 276 vaddr_t virtual_avail, virtual_end; 277 /* Physical address space available: */ 278 paddr_t avail_start, avail_end; 279 280 /* This keep track of the end of the contiguously mapped range. */ 281 vaddr_t virtual_contig_end; 282 283 /* Physical address used by pmap_next_page() */ 284 paddr_t avail_next; 285 286 /* These are used by pmap_copy_page(), etc. */ 287 vaddr_t tmp_vpages[2]; 288 289 /* memory pool for pmap structures */ 290 struct pool pmap_pmap_pool; 291 292 /* 293 * The 3/80 is the only member of the sun3x family that has non-contiguous 294 * physical memory. Memory is divided into 4 banks which are physically 295 * locatable on the system board. Although the size of these banks varies 296 * with the size of memory they contain, their base addresses are 297 * permenently fixed. The following structure, which describes these 298 * banks, is initialized by pmap_bootstrap() after it reads from a similar 299 * structure provided by the ROM Monitor. 300 * 301 * For the other machines in the sun3x architecture which do have contiguous 302 * RAM, this list will have only one entry, which will describe the entire 303 * range of available memory. 304 */ 305 struct pmap_physmem_struct avail_mem[SUN3X_NPHYS_RAM_SEGS]; 306 u_int total_phys_mem; 307 308 /*************************************************************************/ 309 310 /* 311 * XXX - Should "tune" these based on statistics. 312 * 313 * My first guess about the relative numbers of these needed is 314 * based on the fact that a "typical" process will have several 315 * pages mapped at low virtual addresses (text, data, bss), then 316 * some mapped shared libraries, and then some stack pages mapped 317 * near the high end of the VA space. Each process can use only 318 * one A table, and most will use only two B tables (maybe three) 319 * and probably about four C tables. Therefore, the first guess 320 * at the relative numbers of these needed is 1:2:4 -gwr 321 * 322 * The number of C tables needed is closely related to the amount 323 * of physical memory available plus a certain amount attributable 324 * to the use of double mappings. With a few simulation statistics 325 * we can find a reasonably good estimation of this unknown value. 326 * Armed with that and the above ratios, we have a good idea of what 327 * is needed at each level. -j 328 * 329 * Note: It is not physical memory memory size, but the total mapped 330 * virtual space required by the combined working sets of all the 331 * currently _runnable_ processes. (Sleeping ones don't count.) 332 * The amount of physical memory should be irrelevant. -gwr 333 */ 334 #ifdef FIXED_NTABLES 335 #define NUM_A_TABLES 16 336 #define NUM_B_TABLES 32 337 #define NUM_C_TABLES 64 338 #else 339 unsigned int NUM_A_TABLES, NUM_B_TABLES, NUM_C_TABLES; 340 #endif /* FIXED_NTABLES */ 341 342 /* 343 * This determines our total virtual mapping capacity. 344 * Yes, it is a FIXED value so we can pre-allocate. 345 */ 346 #define NUM_USER_PTES (NUM_C_TABLES * MMU_C_TBL_SIZE) 347 348 /* 349 * The size of the Kernel Virtual Address Space (KVAS) 350 * for purposes of MMU table allocation is -KERNBASE 351 * (length from KERNBASE to 0xFFFFffff) 352 */ 353 #define KVAS_SIZE (-KERNBASE) 354 355 /* Numbers of kernel MMU tables to support KVAS_SIZE. */ 356 #define KERN_B_TABLES (KVAS_SIZE >> MMU_TIA_SHIFT) 357 #define KERN_C_TABLES (KVAS_SIZE >> MMU_TIB_SHIFT) 358 #define NUM_KERN_PTES (KVAS_SIZE >> MMU_TIC_SHIFT) 359 360 /*************************** MISCELANEOUS MACROS *************************/ 361 #define pmap_lock(pmap) simple_lock(&pmap->pm_lock) 362 #define pmap_unlock(pmap) simple_unlock(&pmap->pm_lock) 363 #define pmap_add_ref(pmap) ++pmap->pm_refcount 364 #define pmap_del_ref(pmap) --pmap->pm_refcount 365 #define pmap_refcount(pmap) pmap->pm_refcount 366 367 void *pmap_bootstrap_alloc(int); 368 369 static INLINE void *mmu_ptov __P((paddr_t)); 370 static INLINE paddr_t mmu_vtop __P((void *)); 371 372 #if 0 373 static INLINE a_tmgr_t * mmuA2tmgr __P((mmu_long_dte_t *)); 374 #endif /* 0 */ 375 static INLINE b_tmgr_t * mmuB2tmgr __P((mmu_short_dte_t *)); 376 static INLINE c_tmgr_t * mmuC2tmgr __P((mmu_short_pte_t *)); 377 378 static INLINE pv_t *pa2pv __P((paddr_t)); 379 static INLINE int pteidx __P((mmu_short_pte_t *)); 380 static INLINE pmap_t current_pmap __P((void)); 381 382 /* 383 * We can always convert between virtual and physical addresses 384 * for anything in the range [KERNBASE ... avail_start] because 385 * that range is GUARANTEED to be mapped linearly. 386 * We rely heavily upon this feature! 387 */ 388 static INLINE void * 389 mmu_ptov(pa) 390 paddr_t pa; 391 { 392 vaddr_t va; 393 394 va = (pa + KERNBASE); 395 #ifdef PMAP_DEBUG 396 if ((va < KERNBASE) || (va >= virtual_contig_end)) 397 panic("mmu_ptov"); 398 #endif 399 return ((void*)va); 400 } 401 402 static INLINE paddr_t 403 mmu_vtop(vva) 404 void *vva; 405 { 406 vaddr_t va; 407 408 va = (vaddr_t)vva; 409 #ifdef PMAP_DEBUG 410 if ((va < KERNBASE) || (va >= virtual_contig_end)) 411 panic("mmu_vtop"); 412 #endif 413 return (va - KERNBASE); 414 } 415 416 /* 417 * These macros map MMU tables to their corresponding manager structures. 418 * They are needed quite often because many of the pointers in the pmap 419 * system reference MMU tables and not the structures that control them. 420 * There needs to be a way to find one when given the other and these 421 * macros do so by taking advantage of the memory layout described above. 422 * Here's a quick step through the first macro, mmuA2tmgr(): 423 * 424 * 1) find the offset of the given MMU A table from the base of its table 425 * pool (table - mmuAbase). 426 * 2) convert this offset into a table index by dividing it by the 427 * size of one MMU 'A' table. (sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE) 428 * 3) use this index to select the corresponding 'A' table manager 429 * structure from the 'A' table manager pool (Atmgrbase[index]). 430 */ 431 /* This function is not currently used. */ 432 #if 0 433 static INLINE a_tmgr_t * 434 mmuA2tmgr(mmuAtbl) 435 mmu_long_dte_t *mmuAtbl; 436 { 437 int idx; 438 439 /* Which table is this in? */ 440 idx = (mmuAtbl - mmuAbase) / MMU_A_TBL_SIZE; 441 #ifdef PMAP_DEBUG 442 if ((idx < 0) || (idx >= NUM_A_TABLES)) 443 panic("mmuA2tmgr"); 444 #endif 445 return (&Atmgrbase[idx]); 446 } 447 #endif /* 0 */ 448 449 static INLINE b_tmgr_t * 450 mmuB2tmgr(mmuBtbl) 451 mmu_short_dte_t *mmuBtbl; 452 { 453 int idx; 454 455 /* Which table is this in? */ 456 idx = (mmuBtbl - mmuBbase) / MMU_B_TBL_SIZE; 457 #ifdef PMAP_DEBUG 458 if ((idx < 0) || (idx >= NUM_B_TABLES)) 459 panic("mmuB2tmgr"); 460 #endif 461 return (&Btmgrbase[idx]); 462 } 463 464 /* mmuC2tmgr INTERNAL 465 ** 466 * Given a pte known to belong to a C table, return the address of 467 * that table's management structure. 468 */ 469 static INLINE c_tmgr_t * 470 mmuC2tmgr(mmuCtbl) 471 mmu_short_pte_t *mmuCtbl; 472 { 473 int idx; 474 475 /* Which table is this in? */ 476 idx = (mmuCtbl - mmuCbase) / MMU_C_TBL_SIZE; 477 #ifdef PMAP_DEBUG 478 if ((idx < 0) || (idx >= NUM_C_TABLES)) 479 panic("mmuC2tmgr"); 480 #endif 481 return (&Ctmgrbase[idx]); 482 } 483 484 /* This is now a function call below. 485 * #define pa2pv(pa) \ 486 * (&pvbase[(unsigned long)\ 487 * m68k_btop(pa)\ 488 * ]) 489 */ 490 491 /* pa2pv INTERNAL 492 ** 493 * Return the pv_list_head element which manages the given physical 494 * address. 495 */ 496 static INLINE pv_t * 497 pa2pv(pa) 498 paddr_t pa; 499 { 500 struct pmap_physmem_struct *bank; 501 int idx; 502 503 bank = &avail_mem[0]; 504 while (pa >= bank->pmem_end) 505 bank = bank->pmem_next; 506 507 pa -= bank->pmem_start; 508 idx = bank->pmem_pvbase + m68k_btop(pa); 509 #ifdef PMAP_DEBUG 510 if ((idx < 0) || (idx >= physmem)) 511 panic("pa2pv"); 512 #endif 513 return &pvbase[idx]; 514 } 515 516 /* pteidx INTERNAL 517 ** 518 * Return the index of the given PTE within the entire fixed table of 519 * PTEs. 520 */ 521 static INLINE int 522 pteidx(pte) 523 mmu_short_pte_t *pte; 524 { 525 return (pte - kernCbase); 526 } 527 528 /* 529 * This just offers a place to put some debugging checks, 530 * and reduces the number of places "curlwp" appears... 531 */ 532 static INLINE pmap_t 533 current_pmap() 534 { 535 struct vmspace *vm; 536 struct vm_map *map; 537 pmap_t pmap; 538 539 if (curlwp == NULL) 540 pmap = &kernel_pmap; 541 else { 542 vm = curproc->p_vmspace; 543 map = &vm->vm_map; 544 pmap = vm_map_pmap(map); 545 } 546 547 return (pmap); 548 } 549 550 551 /*************************** FUNCTION DEFINITIONS ************************ 552 * These appear here merely for the compiler to enforce type checking on * 553 * all function calls. * 554 *************************************************************************/ 555 556 /** Internal functions 557 ** Most functions used only within this module are defined in 558 ** pmap_pvt.h (why not here if used only here?) 559 **/ 560 static void pmap_page_upload __P((void)); 561 562 /** Interface functions 563 ** - functions required by the Mach VM Pmap interface, with MACHINE_CONTIG 564 ** defined. 565 **/ 566 void pmap_pinit __P((pmap_t)); 567 void pmap_release __P((pmap_t)); 568 569 /********************************** CODE ******************************** 570 * Functions that are called from other parts of the kernel are labeled * 571 * as 'INTERFACE' functions. Functions that are only called from * 572 * within the pmap module are labeled as 'INTERNAL' functions. * 573 * Functions that are internal, but are not (currently) used at all are * 574 * labeled 'INTERNAL_X'. * 575 ************************************************************************/ 576 577 /* pmap_bootstrap INTERNAL 578 ** 579 * Initializes the pmap system. Called at boot time from 580 * locore2.c:_vm_init() 581 * 582 * Reminder: having a pmap_bootstrap_alloc() and also having the VM 583 * system implement pmap_steal_memory() is redundant. 584 * Don't release this code without removing one or the other! 585 */ 586 void 587 pmap_bootstrap(nextva) 588 vaddr_t nextva; 589 { 590 struct physmemory *membank; 591 struct pmap_physmem_struct *pmap_membank; 592 vaddr_t va, eva; 593 paddr_t pa; 594 int b, c, i, j; /* running table counts */ 595 int size, resvmem; 596 597 /* 598 * This function is called by __bootstrap after it has 599 * determined the type of machine and made the appropriate 600 * patches to the ROM vectors (XXX- I don't quite know what I meant 601 * by that.) It allocates and sets up enough of the pmap system 602 * to manage the kernel's address space. 603 */ 604 605 /* 606 * Determine the range of kernel virtual and physical 607 * space available. Note that we ABSOLUTELY DEPEND on 608 * the fact that the first bank of memory (4MB) is 609 * mapped linearly to KERNBASE (which we guaranteed in 610 * the first instructions of locore.s). 611 * That is plenty for our bootstrap work. 612 */ 613 virtual_avail = m68k_round_page(nextva); 614 virtual_contig_end = KERNBASE + 0x400000; /* +4MB */ 615 virtual_end = VM_MAX_KERNEL_ADDRESS; 616 /* Don't need avail_start til later. */ 617 618 /* We may now call pmap_bootstrap_alloc(). */ 619 bootstrap_alloc_enabled = TRUE; 620 621 /* 622 * This is a somewhat unwrapped loop to deal with 623 * copying the PROM's 'phsymem' banks into the pmap's 624 * banks. The following is always assumed: 625 * 1. There is always at least one bank of memory. 626 * 2. There is always a last bank of memory, and its 627 * pmem_next member must be set to NULL. 628 */ 629 membank = romVectorPtr->v_physmemory; 630 pmap_membank = avail_mem; 631 total_phys_mem = 0; 632 633 for (;;) { /* break on !membank */ 634 pmap_membank->pmem_start = membank->address; 635 pmap_membank->pmem_end = membank->address + membank->size; 636 total_phys_mem += membank->size; 637 membank = membank->next; 638 if (!membank) 639 break; 640 /* This silly syntax arises because pmap_membank 641 * is really a pre-allocated array, but it is put into 642 * use as a linked list. 643 */ 644 pmap_membank->pmem_next = pmap_membank + 1; 645 pmap_membank = pmap_membank->pmem_next; 646 } 647 /* This is the last element. */ 648 pmap_membank->pmem_next = NULL; 649 650 /* 651 * Note: total_phys_mem, physmem represent 652 * actual physical memory, including that 653 * reserved for the PROM monitor. 654 */ 655 physmem = btoc(total_phys_mem); 656 657 /* 658 * Avail_end is set to the first byte of physical memory 659 * after the end of the last bank. We use this only to 660 * determine if a physical address is "managed" memory. 661 * This address range should be reduced to prevent the 662 * physical pages needed by the PROM monitor from being used 663 * in the VM system. 664 */ 665 resvmem = total_phys_mem - *(romVectorPtr->memoryAvail); 666 resvmem = m68k_round_page(resvmem); 667 avail_end = pmap_membank->pmem_end - resvmem; 668 669 /* 670 * First allocate enough kernel MMU tables to map all 671 * of kernel virtual space from KERNBASE to 0xFFFFFFFF. 672 * Note: All must be aligned on 256 byte boundaries. 673 * Start with the level-A table (one of those). 674 */ 675 size = sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE; 676 kernAbase = pmap_bootstrap_alloc(size); 677 memset(kernAbase, 0, size); 678 679 /* Now the level-B kernel tables... */ 680 size = sizeof(mmu_short_dte_t) * MMU_B_TBL_SIZE * KERN_B_TABLES; 681 kernBbase = pmap_bootstrap_alloc(size); 682 memset(kernBbase, 0, size); 683 684 /* Now the level-C kernel tables... */ 685 size = sizeof(mmu_short_pte_t) * MMU_C_TBL_SIZE * KERN_C_TABLES; 686 kernCbase = pmap_bootstrap_alloc(size); 687 memset(kernCbase, 0, size); 688 /* 689 * Note: In order for the PV system to work correctly, the kernel 690 * and user-level C tables must be allocated contiguously. 691 * Nothing should be allocated between here and the allocation of 692 * mmuCbase below. XXX: Should do this as one allocation, and 693 * then compute a pointer for mmuCbase instead of this... 694 * 695 * Allocate user MMU tables. 696 * These must be contiguous with the preceding. 697 */ 698 699 #ifndef FIXED_NTABLES 700 /* 701 * The number of user-level C tables that should be allocated is 702 * related to the size of physical memory. In general, there should 703 * be enough tables to map four times the amount of available RAM. 704 * The extra amount is needed because some table space is wasted by 705 * fragmentation. 706 */ 707 NUM_C_TABLES = (total_phys_mem * 4) / (MMU_C_TBL_SIZE * MMU_PAGE_SIZE); 708 NUM_B_TABLES = NUM_C_TABLES / 2; 709 NUM_A_TABLES = NUM_B_TABLES / 2; 710 #endif /* !FIXED_NTABLES */ 711 712 size = sizeof(mmu_short_pte_t) * MMU_C_TBL_SIZE * NUM_C_TABLES; 713 mmuCbase = pmap_bootstrap_alloc(size); 714 715 size = sizeof(mmu_short_dte_t) * MMU_B_TBL_SIZE * NUM_B_TABLES; 716 mmuBbase = pmap_bootstrap_alloc(size); 717 718 size = sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE * NUM_A_TABLES; 719 mmuAbase = pmap_bootstrap_alloc(size); 720 721 /* 722 * Fill in the never-changing part of the kernel tables. 723 * For simplicity, the kernel's mappings will be editable as a 724 * flat array of page table entries at kernCbase. The 725 * higher level 'A' and 'B' tables must be initialized to point 726 * to this lower one. 727 */ 728 b = c = 0; 729 730 /* 731 * Invalidate all mappings below KERNBASE in the A table. 732 * This area has already been zeroed out, but it is good 733 * practice to explicitly show that we are interpreting 734 * it as a list of A table descriptors. 735 */ 736 for (i = 0; i < MMU_TIA(KERNBASE); i++) { 737 kernAbase[i].addr.raw = 0; 738 } 739 740 /* 741 * Set up the kernel A and B tables so that they will reference the 742 * correct spots in the contiguous table of PTEs allocated for the 743 * kernel's virtual memory space. 744 */ 745 for (i = MMU_TIA(KERNBASE); i < MMU_A_TBL_SIZE; i++) { 746 kernAbase[i].attr.raw = 747 MMU_LONG_DTE_LU | MMU_LONG_DTE_SUPV | MMU_DT_SHORT; 748 kernAbase[i].addr.raw = mmu_vtop(&kernBbase[b]); 749 750 for (j=0; j < MMU_B_TBL_SIZE; j++) { 751 kernBbase[b + j].attr.raw = mmu_vtop(&kernCbase[c]) 752 | MMU_DT_SHORT; 753 c += MMU_C_TBL_SIZE; 754 } 755 b += MMU_B_TBL_SIZE; 756 } 757 758 pmap_alloc_usermmu(); /* Allocate user MMU tables. */ 759 pmap_alloc_usertmgr(); /* Allocate user MMU table managers.*/ 760 pmap_alloc_pv(); /* Allocate physical->virtual map. */ 761 762 /* 763 * We are now done with pmap_bootstrap_alloc(). Round up 764 * `virtual_avail' to the nearest page, and set the flag 765 * to prevent use of pmap_bootstrap_alloc() hereafter. 766 */ 767 pmap_bootstrap_aalign(PAGE_SIZE); 768 bootstrap_alloc_enabled = FALSE; 769 770 /* 771 * Now that we are done with pmap_bootstrap_alloc(), we 772 * must save the virtual and physical addresses of the 773 * end of the linearly mapped range, which are stored in 774 * virtual_contig_end and avail_start, respectively. 775 * These variables will never change after this point. 776 */ 777 virtual_contig_end = virtual_avail; 778 avail_start = virtual_avail - KERNBASE; 779 780 /* 781 * `avail_next' is a running pointer used by pmap_next_page() to 782 * keep track of the next available physical page to be handed 783 * to the VM system during its initialization, in which it 784 * asks for physical pages, one at a time. 785 */ 786 avail_next = avail_start; 787 788 /* 789 * Now allocate some virtual addresses, but not the physical pages 790 * behind them. Note that virtual_avail is already page-aligned. 791 * 792 * tmp_vpages[] is an array of two virtual pages used for temporary 793 * kernel mappings in the pmap module to facilitate various physical 794 * address-oritented operations. 795 */ 796 tmp_vpages[0] = virtual_avail; 797 virtual_avail += PAGE_SIZE; 798 tmp_vpages[1] = virtual_avail; 799 virtual_avail += PAGE_SIZE; 800 801 /** Initialize the PV system **/ 802 pmap_init_pv(); 803 804 /* 805 * Fill in the kernel_pmap structure and kernel_crp. 806 */ 807 kernAphys = mmu_vtop(kernAbase); 808 kernel_pmap.pm_a_tmgr = NULL; 809 kernel_pmap.pm_a_phys = kernAphys; 810 kernel_pmap.pm_refcount = 1; /* always in use */ 811 simple_lock_init(&kernel_pmap.pm_lock); 812 813 kernel_crp.rp_attr = MMU_LONG_DTE_LU | MMU_DT_LONG; 814 kernel_crp.rp_addr = kernAphys; 815 816 /* 817 * Now pmap_enter_kernel() may be used safely and will be 818 * the main interface used hereafter to modify the kernel's 819 * virtual address space. Note that since we are still running 820 * under the PROM's address table, none of these table modifications 821 * actually take effect until pmap_takeover_mmu() is called. 822 * 823 * Note: Our tables do NOT have the PROM linear mappings! 824 * Only the mappings created here exist in our tables, so 825 * remember to map anything we expect to use. 826 */ 827 va = (vaddr_t)KERNBASE; 828 pa = 0; 829 830 /* 831 * The first page of the kernel virtual address space is the msgbuf 832 * page. The page attributes (data, non-cached) are set here, while 833 * the address is assigned to this global pointer in cpu_startup(). 834 * It is non-cached, mostly due to paranoia. 835 */ 836 pmap_enter_kernel(va, pa|PMAP_NC, VM_PROT_ALL); 837 va += PAGE_SIZE; pa += PAGE_SIZE; 838 839 /* Next page is used as the temporary stack. */ 840 pmap_enter_kernel(va, pa, VM_PROT_ALL); 841 va += PAGE_SIZE; pa += PAGE_SIZE; 842 843 /* 844 * Map all of the kernel's text segment as read-only and cacheable. 845 * (Cacheable is implied by default). Unfortunately, the last bytes 846 * of kernel text and the first bytes of kernel data will often be 847 * sharing the same page. Therefore, the last page of kernel text 848 * has to be mapped as read/write, to accomodate the data. 849 */ 850 eva = m68k_trunc_page((vaddr_t)etext); 851 for (; va < eva; va += PAGE_SIZE, pa += PAGE_SIZE) 852 pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_EXECUTE); 853 854 /* 855 * Map all of the kernel's data as read/write and cacheable. 856 * This includes: data, BSS, symbols, and everything in the 857 * contiguous memory used by pmap_bootstrap_alloc() 858 */ 859 for (; pa < avail_start; va += PAGE_SIZE, pa += PAGE_SIZE) 860 pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_WRITE); 861 862 /* 863 * At this point we are almost ready to take over the MMU. But first 864 * we must save the PROM's address space in our map, as we call its 865 * routines and make references to its data later in the kernel. 866 */ 867 pmap_bootstrap_copyprom(); 868 pmap_takeover_mmu(); 869 pmap_bootstrap_setprom(); 870 871 /* Notify the VM system of our page size. */ 872 uvmexp.pagesize = PAGE_SIZE; 873 uvm_setpagesize(); 874 875 pmap_page_upload(); 876 } 877 878 879 /* pmap_alloc_usermmu INTERNAL 880 ** 881 * Called from pmap_bootstrap() to allocate MMU tables that will 882 * eventually be used for user mappings. 883 */ 884 void 885 pmap_alloc_usermmu() 886 { 887 /* XXX: Moved into caller. */ 888 } 889 890 /* pmap_alloc_pv INTERNAL 891 ** 892 * Called from pmap_bootstrap() to allocate the physical 893 * to virtual mapping list. Each physical page of memory 894 * in the system has a corresponding element in this list. 895 */ 896 void 897 pmap_alloc_pv() 898 { 899 int i; 900 unsigned int total_mem; 901 902 /* 903 * Allocate a pv_head structure for every page of physical 904 * memory that will be managed by the system. Since memory on 905 * the 3/80 is non-contiguous, we cannot arrive at a total page 906 * count by subtraction of the lowest available address from the 907 * highest, but rather we have to step through each memory 908 * bank and add the number of pages in each to the total. 909 * 910 * At this time we also initialize the offset of each bank's 911 * starting pv_head within the pv_head list so that the physical 912 * memory state routines (pmap_is_referenced(), 913 * pmap_is_modified(), et al.) can quickly find coresponding 914 * pv_heads in spite of the non-contiguity. 915 */ 916 total_mem = 0; 917 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) { 918 avail_mem[i].pmem_pvbase = m68k_btop(total_mem); 919 total_mem += avail_mem[i].pmem_end - 920 avail_mem[i].pmem_start; 921 if (avail_mem[i].pmem_next == NULL) 922 break; 923 } 924 pvbase = (pv_t *) pmap_bootstrap_alloc(sizeof(pv_t) * 925 m68k_btop(total_phys_mem)); 926 } 927 928 /* pmap_alloc_usertmgr INTERNAL 929 ** 930 * Called from pmap_bootstrap() to allocate the structures which 931 * facilitate management of user MMU tables. Each user MMU table 932 * in the system has one such structure associated with it. 933 */ 934 void 935 pmap_alloc_usertmgr() 936 { 937 /* Allocate user MMU table managers */ 938 /* It would be a lot simpler to just make these BSS, but */ 939 /* we may want to change their size at boot time... -j */ 940 Atmgrbase = (a_tmgr_t *) pmap_bootstrap_alloc(sizeof(a_tmgr_t) 941 * NUM_A_TABLES); 942 Btmgrbase = (b_tmgr_t *) pmap_bootstrap_alloc(sizeof(b_tmgr_t) 943 * NUM_B_TABLES); 944 Ctmgrbase = (c_tmgr_t *) pmap_bootstrap_alloc(sizeof(c_tmgr_t) 945 * NUM_C_TABLES); 946 947 /* 948 * Allocate PV list elements for the physical to virtual 949 * mapping system. 950 */ 951 pvebase = (pv_elem_t *) pmap_bootstrap_alloc( 952 sizeof(pv_elem_t) * (NUM_USER_PTES + NUM_KERN_PTES)); 953 } 954 955 /* pmap_bootstrap_copyprom() INTERNAL 956 ** 957 * Copy the PROM mappings into our own tables. Note, we 958 * can use physical addresses until __bootstrap returns. 959 */ 960 void 961 pmap_bootstrap_copyprom() 962 { 963 struct sunromvec *romp; 964 int *mon_ctbl; 965 mmu_short_pte_t *kpte; 966 int i, len; 967 968 romp = romVectorPtr; 969 970 /* 971 * Copy the mappings in SUN3X_MON_KDB_BASE...SUN3X_MONEND 972 * Note: mon_ctbl[0] maps SUN3X_MON_KDB_BASE 973 */ 974 mon_ctbl = *romp->monptaddr; 975 i = m68k_btop(SUN3X_MON_KDB_BASE - KERNBASE); 976 kpte = &kernCbase[i]; 977 len = m68k_btop(SUN3X_MONEND - SUN3X_MON_KDB_BASE); 978 979 for (i = 0; i < len; i++) { 980 kpte[i].attr.raw = mon_ctbl[i]; 981 } 982 983 /* 984 * Copy the mappings at MON_DVMA_BASE (to the end). 985 * Note, in here, mon_ctbl[0] maps MON_DVMA_BASE. 986 * Actually, we only want the last page, which the 987 * PROM has set up for use by the "ie" driver. 988 * (The i82686 needs its SCP there.) 989 * If we copy all the mappings, pmap_enter_kernel 990 * may complain about finding valid PTEs that are 991 * not recorded in our PV lists... 992 */ 993 mon_ctbl = *romp->shadowpteaddr; 994 i = m68k_btop(SUN3X_MON_DVMA_BASE - KERNBASE); 995 kpte = &kernCbase[i]; 996 len = m68k_btop(SUN3X_MON_DVMA_SIZE); 997 for (i = (len-1); i < len; i++) { 998 kpte[i].attr.raw = mon_ctbl[i]; 999 } 1000 } 1001 1002 /* pmap_takeover_mmu INTERNAL 1003 ** 1004 * Called from pmap_bootstrap() after it has copied enough of the 1005 * PROM mappings into the kernel map so that we can use our own 1006 * MMU table. 1007 */ 1008 void 1009 pmap_takeover_mmu() 1010 { 1011 1012 loadcrp(&kernel_crp); 1013 } 1014 1015 /* pmap_bootstrap_setprom() INTERNAL 1016 ** 1017 * Set the PROM mappings so it can see kernel space. 1018 * Note that physical addresses are used here, which 1019 * we can get away with because this runs with the 1020 * low 1GB set for transparent translation. 1021 */ 1022 void 1023 pmap_bootstrap_setprom() 1024 { 1025 mmu_long_dte_t *mon_dte; 1026 extern struct mmu_rootptr mon_crp; 1027 int i; 1028 1029 mon_dte = (mmu_long_dte_t *) mon_crp.rp_addr; 1030 for (i = MMU_TIA(KERNBASE); i < MMU_TIA(KERN_END); i++) { 1031 mon_dte[i].attr.raw = kernAbase[i].attr.raw; 1032 mon_dte[i].addr.raw = kernAbase[i].addr.raw; 1033 } 1034 } 1035 1036 1037 /* pmap_init INTERFACE 1038 ** 1039 * Called at the end of vm_init() to set up the pmap system to go 1040 * into full time operation. All initialization of kernel_pmap 1041 * should be already done by now, so this should just do things 1042 * needed for user-level pmaps to work. 1043 */ 1044 void 1045 pmap_init() 1046 { 1047 /** Initialize the manager pools **/ 1048 TAILQ_INIT(&a_pool); 1049 TAILQ_INIT(&b_pool); 1050 TAILQ_INIT(&c_pool); 1051 1052 /************************************************************** 1053 * Initialize all tmgr structures and MMU tables they manage. * 1054 **************************************************************/ 1055 /** Initialize A tables **/ 1056 pmap_init_a_tables(); 1057 /** Initialize B tables **/ 1058 pmap_init_b_tables(); 1059 /** Initialize C tables **/ 1060 pmap_init_c_tables(); 1061 1062 /** Initialize the pmap pools **/ 1063 pool_init(&pmap_pmap_pool, sizeof(struct pmap), 0, 0, 0, "pmappl", 1064 &pool_allocator_nointr); 1065 } 1066 1067 /* pmap_init_a_tables() INTERNAL 1068 ** 1069 * Initializes all A managers, their MMU A tables, and inserts 1070 * them into the A manager pool for use by the system. 1071 */ 1072 void 1073 pmap_init_a_tables() 1074 { 1075 int i; 1076 a_tmgr_t *a_tbl; 1077 1078 for (i=0; i < NUM_A_TABLES; i++) { 1079 /* Select the next available A manager from the pool */ 1080 a_tbl = &Atmgrbase[i]; 1081 1082 /* 1083 * Clear its parent entry. Set its wired and valid 1084 * entry count to zero. 1085 */ 1086 a_tbl->at_parent = NULL; 1087 a_tbl->at_wcnt = a_tbl->at_ecnt = 0; 1088 1089 /* Assign it the next available MMU A table from the pool */ 1090 a_tbl->at_dtbl = &mmuAbase[i * MMU_A_TBL_SIZE]; 1091 1092 /* 1093 * Initialize the MMU A table with the table in the `proc0', 1094 * or kernel, mapping. This ensures that every process has 1095 * the kernel mapped in the top part of its address space. 1096 */ 1097 memcpy(a_tbl->at_dtbl, kernAbase, MMU_A_TBL_SIZE * 1098 sizeof(mmu_long_dte_t)); 1099 1100 /* 1101 * Finally, insert the manager into the A pool, 1102 * making it ready to be used by the system. 1103 */ 1104 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link); 1105 } 1106 } 1107 1108 /* pmap_init_b_tables() INTERNAL 1109 ** 1110 * Initializes all B table managers, their MMU B tables, and 1111 * inserts them into the B manager pool for use by the system. 1112 */ 1113 void 1114 pmap_init_b_tables() 1115 { 1116 int i,j; 1117 b_tmgr_t *b_tbl; 1118 1119 for (i=0; i < NUM_B_TABLES; i++) { 1120 /* Select the next available B manager from the pool */ 1121 b_tbl = &Btmgrbase[i]; 1122 1123 b_tbl->bt_parent = NULL; /* clear its parent, */ 1124 b_tbl->bt_pidx = 0; /* parent index, */ 1125 b_tbl->bt_wcnt = 0; /* wired entry count, */ 1126 b_tbl->bt_ecnt = 0; /* valid entry count. */ 1127 1128 /* Assign it the next available MMU B table from the pool */ 1129 b_tbl->bt_dtbl = &mmuBbase[i * MMU_B_TBL_SIZE]; 1130 1131 /* Invalidate every descriptor in the table */ 1132 for (j=0; j < MMU_B_TBL_SIZE; j++) 1133 b_tbl->bt_dtbl[j].attr.raw = MMU_DT_INVALID; 1134 1135 /* Insert the manager into the B pool */ 1136 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link); 1137 } 1138 } 1139 1140 /* pmap_init_c_tables() INTERNAL 1141 ** 1142 * Initializes all C table managers, their MMU C tables, and 1143 * inserts them into the C manager pool for use by the system. 1144 */ 1145 void 1146 pmap_init_c_tables() 1147 { 1148 int i,j; 1149 c_tmgr_t *c_tbl; 1150 1151 for (i=0; i < NUM_C_TABLES; i++) { 1152 /* Select the next available C manager from the pool */ 1153 c_tbl = &Ctmgrbase[i]; 1154 1155 c_tbl->ct_parent = NULL; /* clear its parent, */ 1156 c_tbl->ct_pidx = 0; /* parent index, */ 1157 c_tbl->ct_wcnt = 0; /* wired entry count, */ 1158 c_tbl->ct_ecnt = 0; /* valid entry count, */ 1159 c_tbl->ct_pmap = NULL; /* parent pmap, */ 1160 c_tbl->ct_va = 0; /* base of managed range */ 1161 1162 /* Assign it the next available MMU C table from the pool */ 1163 c_tbl->ct_dtbl = &mmuCbase[i * MMU_C_TBL_SIZE]; 1164 1165 for (j=0; j < MMU_C_TBL_SIZE; j++) 1166 c_tbl->ct_dtbl[j].attr.raw = MMU_DT_INVALID; 1167 1168 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link); 1169 } 1170 } 1171 1172 /* pmap_init_pv() INTERNAL 1173 ** 1174 * Initializes the Physical to Virtual mapping system. 1175 */ 1176 void 1177 pmap_init_pv() 1178 { 1179 int i; 1180 1181 /* Initialize every PV head. */ 1182 for (i = 0; i < m68k_btop(total_phys_mem); i++) { 1183 pvbase[i].pv_idx = PVE_EOL; /* Indicate no mappings */ 1184 pvbase[i].pv_flags = 0; /* Zero out page flags */ 1185 } 1186 } 1187 1188 /* get_a_table INTERNAL 1189 ** 1190 * Retrieve and return a level A table for use in a user map. 1191 */ 1192 a_tmgr_t * 1193 get_a_table() 1194 { 1195 a_tmgr_t *tbl; 1196 pmap_t pmap; 1197 1198 /* Get the top A table in the pool */ 1199 tbl = a_pool.tqh_first; 1200 if (tbl == NULL) { 1201 /* 1202 * XXX - Instead of panicing here and in other get_x_table 1203 * functions, we do have the option of sleeping on the head of 1204 * the table pool. Any function which updates the table pool 1205 * would then issue a wakeup() on the head, thus waking up any 1206 * processes waiting for a table. 1207 * 1208 * Actually, the place to sleep would be when some process 1209 * asks for a "wired" mapping that would run us short of 1210 * mapping resources. This design DEPENDS on always having 1211 * some mapping resources in the pool for stealing, so we 1212 * must make sure we NEVER let the pool become empty. -gwr 1213 */ 1214 panic("get_a_table: out of A tables."); 1215 } 1216 1217 TAILQ_REMOVE(&a_pool, tbl, at_link); 1218 /* 1219 * If the table has a non-null parent pointer then it is in use. 1220 * Forcibly abduct it from its parent and clear its entries. 1221 * No re-entrancy worries here. This table would not be in the 1222 * table pool unless it was available for use. 1223 * 1224 * Note that the second argument to free_a_table() is FALSE. This 1225 * indicates that the table should not be relinked into the A table 1226 * pool. That is a job for the function that called us. 1227 */ 1228 if (tbl->at_parent) { 1229 pmap = tbl->at_parent; 1230 free_a_table(tbl, FALSE); 1231 pmap->pm_a_tmgr = NULL; 1232 pmap->pm_a_phys = kernAphys; 1233 } 1234 return tbl; 1235 } 1236 1237 /* get_b_table INTERNAL 1238 ** 1239 * Return a level B table for use. 1240 */ 1241 b_tmgr_t * 1242 get_b_table() 1243 { 1244 b_tmgr_t *tbl; 1245 1246 /* See 'get_a_table' for comments. */ 1247 tbl = b_pool.tqh_first; 1248 if (tbl == NULL) 1249 panic("get_b_table: out of B tables."); 1250 TAILQ_REMOVE(&b_pool, tbl, bt_link); 1251 if (tbl->bt_parent) { 1252 tbl->bt_parent->at_dtbl[tbl->bt_pidx].attr.raw = MMU_DT_INVALID; 1253 tbl->bt_parent->at_ecnt--; 1254 free_b_table(tbl, FALSE); 1255 } 1256 return tbl; 1257 } 1258 1259 /* get_c_table INTERNAL 1260 ** 1261 * Return a level C table for use. 1262 */ 1263 c_tmgr_t * 1264 get_c_table() 1265 { 1266 c_tmgr_t *tbl; 1267 1268 /* See 'get_a_table' for comments */ 1269 tbl = c_pool.tqh_first; 1270 if (tbl == NULL) 1271 panic("get_c_table: out of C tables."); 1272 TAILQ_REMOVE(&c_pool, tbl, ct_link); 1273 if (tbl->ct_parent) { 1274 tbl->ct_parent->bt_dtbl[tbl->ct_pidx].attr.raw = MMU_DT_INVALID; 1275 tbl->ct_parent->bt_ecnt--; 1276 free_c_table(tbl, FALSE); 1277 } 1278 return tbl; 1279 } 1280 1281 /* 1282 * The following 'free_table' and 'steal_table' functions are called to 1283 * detach tables from their current obligations (parents and children) and 1284 * prepare them for reuse in another mapping. 1285 * 1286 * Free_table is used when the calling function will handle the fate 1287 * of the parent table, such as returning it to the free pool when it has 1288 * no valid entries. Functions that do not want to handle this should 1289 * call steal_table, in which the parent table's descriptors and entry 1290 * count are automatically modified when this table is removed. 1291 */ 1292 1293 /* free_a_table INTERNAL 1294 ** 1295 * Unmaps the given A table and all child tables from their current 1296 * mappings. Returns the number of pages that were invalidated. 1297 * If 'relink' is true, the function will return the table to the head 1298 * of the available table pool. 1299 * 1300 * Cache note: The MC68851 will automatically flush all 1301 * descriptors derived from a given A table from its 1302 * Automatic Translation Cache (ATC) if we issue a 1303 * 'PFLUSHR' instruction with the base address of the 1304 * table. This function should do, and does so. 1305 * Note note: We are using an MC68030 - there is no 1306 * PFLUSHR. 1307 */ 1308 int 1309 free_a_table(a_tbl, relink) 1310 a_tmgr_t *a_tbl; 1311 boolean_t relink; 1312 { 1313 int i, removed_cnt; 1314 mmu_long_dte_t *dte; 1315 mmu_short_dte_t *dtbl; 1316 b_tmgr_t *tmgr; 1317 1318 /* 1319 * Flush the ATC cache of all cached descriptors derived 1320 * from this table. 1321 * Sun3x does not use 68851's cached table feature 1322 * flush_atc_crp(mmu_vtop(a_tbl->dte)); 1323 */ 1324 1325 /* 1326 * Remove any pending cache flushes that were designated 1327 * for the pmap this A table belongs to. 1328 * a_tbl->parent->atc_flushq[0] = 0; 1329 * Not implemented in sun3x. 1330 */ 1331 1332 /* 1333 * All A tables in the system should retain a map for the 1334 * kernel. If the table contains any valid descriptors 1335 * (other than those for the kernel area), invalidate them all, 1336 * stopping short of the kernel's entries. 1337 */ 1338 removed_cnt = 0; 1339 if (a_tbl->at_ecnt) { 1340 dte = a_tbl->at_dtbl; 1341 for (i=0; i < MMU_TIA(KERNBASE); i++) { 1342 /* 1343 * If a table entry points to a valid B table, free 1344 * it and its children. 1345 */ 1346 if (MMU_VALID_DT(dte[i])) { 1347 /* 1348 * The following block does several things, 1349 * from innermost expression to the 1350 * outermost: 1351 * 1) It extracts the base (cc 1996) 1352 * address of the B table pointed 1353 * to in the A table entry dte[i]. 1354 * 2) It converts this base address into 1355 * the virtual address it can be 1356 * accessed with. (all MMU tables point 1357 * to physical addresses.) 1358 * 3) It finds the corresponding manager 1359 * structure which manages this MMU table. 1360 * 4) It frees the manager structure. 1361 * (This frees the MMU table and all 1362 * child tables. See 'free_b_table' for 1363 * details.) 1364 */ 1365 dtbl = mmu_ptov(dte[i].addr.raw); 1366 tmgr = mmuB2tmgr(dtbl); 1367 removed_cnt += free_b_table(tmgr, TRUE); 1368 dte[i].attr.raw = MMU_DT_INVALID; 1369 } 1370 } 1371 a_tbl->at_ecnt = 0; 1372 } 1373 if (relink) { 1374 a_tbl->at_parent = NULL; 1375 TAILQ_REMOVE(&a_pool, a_tbl, at_link); 1376 TAILQ_INSERT_HEAD(&a_pool, a_tbl, at_link); 1377 } 1378 return removed_cnt; 1379 } 1380 1381 /* free_b_table INTERNAL 1382 ** 1383 * Unmaps the given B table and all its children from their current 1384 * mappings. Returns the number of pages that were invalidated. 1385 * (For comments, see 'free_a_table()'). 1386 */ 1387 int 1388 free_b_table(b_tbl, relink) 1389 b_tmgr_t *b_tbl; 1390 boolean_t relink; 1391 { 1392 int i, removed_cnt; 1393 mmu_short_dte_t *dte; 1394 mmu_short_pte_t *dtbl; 1395 c_tmgr_t *tmgr; 1396 1397 removed_cnt = 0; 1398 if (b_tbl->bt_ecnt) { 1399 dte = b_tbl->bt_dtbl; 1400 for (i=0; i < MMU_B_TBL_SIZE; i++) { 1401 if (MMU_VALID_DT(dte[i])) { 1402 dtbl = mmu_ptov(MMU_DTE_PA(dte[i])); 1403 tmgr = mmuC2tmgr(dtbl); 1404 removed_cnt += free_c_table(tmgr, TRUE); 1405 dte[i].attr.raw = MMU_DT_INVALID; 1406 } 1407 } 1408 b_tbl->bt_ecnt = 0; 1409 } 1410 1411 if (relink) { 1412 b_tbl->bt_parent = NULL; 1413 TAILQ_REMOVE(&b_pool, b_tbl, bt_link); 1414 TAILQ_INSERT_HEAD(&b_pool, b_tbl, bt_link); 1415 } 1416 return removed_cnt; 1417 } 1418 1419 /* free_c_table INTERNAL 1420 ** 1421 * Unmaps the given C table from use and returns it to the pool for 1422 * re-use. Returns the number of pages that were invalidated. 1423 * 1424 * This function preserves any physical page modification information 1425 * contained in the page descriptors within the C table by calling 1426 * 'pmap_remove_pte().' 1427 */ 1428 int 1429 free_c_table(c_tbl, relink) 1430 c_tmgr_t *c_tbl; 1431 boolean_t relink; 1432 { 1433 int i, removed_cnt; 1434 1435 removed_cnt = 0; 1436 if (c_tbl->ct_ecnt) { 1437 for (i=0; i < MMU_C_TBL_SIZE; i++) { 1438 if (MMU_VALID_DT(c_tbl->ct_dtbl[i])) { 1439 pmap_remove_pte(&c_tbl->ct_dtbl[i]); 1440 removed_cnt++; 1441 } 1442 } 1443 c_tbl->ct_ecnt = 0; 1444 } 1445 1446 if (relink) { 1447 c_tbl->ct_parent = NULL; 1448 TAILQ_REMOVE(&c_pool, c_tbl, ct_link); 1449 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link); 1450 } 1451 return removed_cnt; 1452 } 1453 1454 1455 /* pmap_remove_pte INTERNAL 1456 ** 1457 * Unmap the given pte and preserve any page modification 1458 * information by transfering it to the pv head of the 1459 * physical page it maps to. This function does not update 1460 * any reference counts because it is assumed that the calling 1461 * function will do so. 1462 */ 1463 void 1464 pmap_remove_pte(pte) 1465 mmu_short_pte_t *pte; 1466 { 1467 u_short pv_idx, targ_idx; 1468 paddr_t pa; 1469 pv_t *pv; 1470 1471 pa = MMU_PTE_PA(*pte); 1472 if (is_managed(pa)) { 1473 pv = pa2pv(pa); 1474 targ_idx = pteidx(pte); /* Index of PTE being removed */ 1475 1476 /* 1477 * If the PTE being removed is the first (or only) PTE in 1478 * the list of PTEs currently mapped to this page, remove the 1479 * PTE by changing the index found on the PV head. Otherwise 1480 * a linear search through the list will have to be executed 1481 * in order to find the PVE which points to the PTE being 1482 * removed, so that it may be modified to point to its new 1483 * neighbor. 1484 */ 1485 1486 pv_idx = pv->pv_idx; /* Index of first PTE in PV list */ 1487 if (pv_idx == targ_idx) { 1488 pv->pv_idx = pvebase[targ_idx].pve_next; 1489 } else { 1490 1491 /* 1492 * Find the PV element pointing to the target 1493 * element. Note: may have pv_idx==PVE_EOL 1494 */ 1495 1496 for (;;) { 1497 if (pv_idx == PVE_EOL) { 1498 goto pv_not_found; 1499 } 1500 if (pvebase[pv_idx].pve_next == targ_idx) 1501 break; 1502 pv_idx = pvebase[pv_idx].pve_next; 1503 } 1504 1505 /* 1506 * At this point, pv_idx is the index of the PV 1507 * element just before the target element in the list. 1508 * Unlink the target. 1509 */ 1510 1511 pvebase[pv_idx].pve_next = pvebase[targ_idx].pve_next; 1512 } 1513 1514 /* 1515 * Save the mod/ref bits of the pte by simply 1516 * ORing the entire pte onto the pv_flags member 1517 * of the pv structure. 1518 * There is no need to use a separate bit pattern 1519 * for usage information on the pv head than that 1520 * which is used on the MMU ptes. 1521 */ 1522 1523 pv_not_found: 1524 pv->pv_flags |= (u_short) pte->attr.raw; 1525 } 1526 pte->attr.raw = MMU_DT_INVALID; 1527 } 1528 1529 /* pmap_stroll INTERNAL 1530 ** 1531 * Retrieve the addresses of all table managers involved in the mapping of 1532 * the given virtual address. If the table walk completed successfully, 1533 * return TRUE. If it was only partially successful, return FALSE. 1534 * The table walk performed by this function is important to many other 1535 * functions in this module. 1536 * 1537 * Note: This function ought to be easier to read. 1538 */ 1539 boolean_t 1540 pmap_stroll(pmap, va, a_tbl, b_tbl, c_tbl, pte, a_idx, b_idx, pte_idx) 1541 pmap_t pmap; 1542 vaddr_t va; 1543 a_tmgr_t **a_tbl; 1544 b_tmgr_t **b_tbl; 1545 c_tmgr_t **c_tbl; 1546 mmu_short_pte_t **pte; 1547 int *a_idx, *b_idx, *pte_idx; 1548 { 1549 mmu_long_dte_t *a_dte; /* A: long descriptor table */ 1550 mmu_short_dte_t *b_dte; /* B: short descriptor table */ 1551 1552 if (pmap == pmap_kernel()) 1553 return FALSE; 1554 1555 /* Does the given pmap have its own A table? */ 1556 *a_tbl = pmap->pm_a_tmgr; 1557 if (*a_tbl == NULL) 1558 return FALSE; /* No. Return unknown. */ 1559 /* Does the A table have a valid B table 1560 * under the corresponding table entry? 1561 */ 1562 *a_idx = MMU_TIA(va); 1563 a_dte = &((*a_tbl)->at_dtbl[*a_idx]); 1564 if (!MMU_VALID_DT(*a_dte)) 1565 return FALSE; /* No. Return unknown. */ 1566 /* Yes. Extract B table from the A table. */ 1567 *b_tbl = mmuB2tmgr(mmu_ptov(a_dte->addr.raw)); 1568 /* Does the B table have a valid C table 1569 * under the corresponding table entry? 1570 */ 1571 *b_idx = MMU_TIB(va); 1572 b_dte = &((*b_tbl)->bt_dtbl[*b_idx]); 1573 if (!MMU_VALID_DT(*b_dte)) 1574 return FALSE; /* No. Return unknown. */ 1575 /* Yes. Extract C table from the B table. */ 1576 *c_tbl = mmuC2tmgr(mmu_ptov(MMU_DTE_PA(*b_dte))); 1577 *pte_idx = MMU_TIC(va); 1578 *pte = &((*c_tbl)->ct_dtbl[*pte_idx]); 1579 1580 return TRUE; 1581 } 1582 1583 /* pmap_enter INTERFACE 1584 ** 1585 * Called by the kernel to map a virtual address 1586 * to a physical address in the given process map. 1587 * 1588 * Note: this function should apply an exclusive lock 1589 * on the pmap system for its duration. (it certainly 1590 * would save my hair!!) 1591 * This function ought to be easier to read. 1592 */ 1593 int 1594 pmap_enter(pmap, va, pa, prot, flags) 1595 pmap_t pmap; 1596 vaddr_t va; 1597 paddr_t pa; 1598 vm_prot_t prot; 1599 int flags; 1600 { 1601 boolean_t insert, managed; /* Marks the need for PV insertion.*/ 1602 u_short nidx; /* PV list index */ 1603 int mapflags; /* Flags for the mapping (see NOTE1) */ 1604 u_int a_idx, b_idx, pte_idx; /* table indices */ 1605 a_tmgr_t *a_tbl; /* A: long descriptor table manager */ 1606 b_tmgr_t *b_tbl; /* B: short descriptor table manager */ 1607 c_tmgr_t *c_tbl; /* C: short page table manager */ 1608 mmu_long_dte_t *a_dte; /* A: long descriptor table */ 1609 mmu_short_dte_t *b_dte; /* B: short descriptor table */ 1610 mmu_short_pte_t *c_pte; /* C: short page descriptor table */ 1611 pv_t *pv; /* pv list head */ 1612 boolean_t wired; /* is the mapping to be wired? */ 1613 enum {NONE, NEWA, NEWB, NEWC} llevel; /* used at end */ 1614 1615 if (pmap == pmap_kernel()) { 1616 pmap_enter_kernel(va, pa, prot); 1617 return 0; 1618 } 1619 1620 /* 1621 * Determine if the mapping should be wired. 1622 */ 1623 wired = ((flags & PMAP_WIRED) != 0); 1624 1625 /* 1626 * NOTE1: 1627 * 1628 * On November 13, 1999, someone changed the pmap_enter() API such 1629 * that it now accepts a 'flags' argument. This new argument 1630 * contains bit-flags for the architecture-independent (UVM) system to 1631 * use in signalling certain mapping requirements to the architecture- 1632 * dependent (pmap) system. The argument it replaces, 'wired', is now 1633 * one of the flags within it. 1634 * 1635 * In addition to flags signaled by the architecture-independent 1636 * system, parts of the architecture-dependent section of the sun3x 1637 * kernel pass their own flags in the lower, unused bits of the 1638 * physical address supplied to this function. These flags are 1639 * extracted and stored in the temporary variable 'mapflags'. 1640 * 1641 * Extract sun3x specific flags from the physical address. 1642 */ 1643 mapflags = (pa & ~MMU_PAGE_MASK); 1644 pa &= MMU_PAGE_MASK; 1645 1646 /* 1647 * Determine if the physical address being mapped is on-board RAM. 1648 * Any other area of the address space is likely to belong to a 1649 * device and hence it would be disasterous to cache its contents. 1650 */ 1651 if ((managed = is_managed(pa)) == FALSE) 1652 mapflags |= PMAP_NC; 1653 1654 /* 1655 * For user mappings we walk along the MMU tables of the given 1656 * pmap, reaching a PTE which describes the virtual page being 1657 * mapped or changed. If any level of the walk ends in an invalid 1658 * entry, a table must be allocated and the entry must be updated 1659 * to point to it. 1660 * There is a bit of confusion as to whether this code must be 1661 * re-entrant. For now we will assume it is. To support 1662 * re-entrancy we must unlink tables from the table pool before 1663 * we assume we may use them. Tables are re-linked into the pool 1664 * when we are finished with them at the end of the function. 1665 * But I don't feel like doing that until we have proof that this 1666 * needs to be re-entrant. 1667 * 'llevel' records which tables need to be relinked. 1668 */ 1669 llevel = NONE; 1670 1671 /* 1672 * Step 1 - Retrieve the A table from the pmap. If it has no 1673 * A table, allocate a new one from the available pool. 1674 */ 1675 1676 a_tbl = pmap->pm_a_tmgr; 1677 if (a_tbl == NULL) { 1678 /* 1679 * This pmap does not currently have an A table. Allocate 1680 * a new one. 1681 */ 1682 a_tbl = get_a_table(); 1683 a_tbl->at_parent = pmap; 1684 1685 /* 1686 * Assign this new A table to the pmap, and calculate its 1687 * physical address so that loadcrp() can be used to make 1688 * the table active. 1689 */ 1690 pmap->pm_a_tmgr = a_tbl; 1691 pmap->pm_a_phys = mmu_vtop(a_tbl->at_dtbl); 1692 1693 /* 1694 * If the process receiving a new A table is the current 1695 * process, we are responsible for setting the MMU so that 1696 * it becomes the current address space. This only adds 1697 * new mappings, so no need to flush anything. 1698 */ 1699 if (pmap == current_pmap()) { 1700 kernel_crp.rp_addr = pmap->pm_a_phys; 1701 loadcrp(&kernel_crp); 1702 } 1703 1704 if (!wired) 1705 llevel = NEWA; 1706 } else { 1707 /* 1708 * Use the A table already allocated for this pmap. 1709 * Unlink it from the A table pool if necessary. 1710 */ 1711 if (wired && !a_tbl->at_wcnt) 1712 TAILQ_REMOVE(&a_pool, a_tbl, at_link); 1713 } 1714 1715 /* 1716 * Step 2 - Walk into the B table. If there is no valid B table, 1717 * allocate one. 1718 */ 1719 1720 a_idx = MMU_TIA(va); /* Calculate the TIA of the VA. */ 1721 a_dte = &a_tbl->at_dtbl[a_idx]; /* Retrieve descriptor from table */ 1722 if (MMU_VALID_DT(*a_dte)) { /* Is the descriptor valid? */ 1723 /* The descriptor is valid. Use the B table it points to. */ 1724 /************************************* 1725 * a_idx * 1726 * v * 1727 * a_tbl -> +-+-+-+-+-+-+-+-+-+-+-+- * 1728 * | | | | | | | | | | | | * 1729 * +-+-+-+-+-+-+-+-+-+-+-+- * 1730 * | * 1731 * \- b_tbl -> +-+- * 1732 * | | * 1733 * +-+- * 1734 *************************************/ 1735 b_dte = mmu_ptov(a_dte->addr.raw); 1736 b_tbl = mmuB2tmgr(b_dte); 1737 1738 /* 1739 * If the requested mapping must be wired, but this table 1740 * being used to map it is not, the table must be removed 1741 * from the available pool and its wired entry count 1742 * incremented. 1743 */ 1744 if (wired && !b_tbl->bt_wcnt) { 1745 TAILQ_REMOVE(&b_pool, b_tbl, bt_link); 1746 a_tbl->at_wcnt++; 1747 } 1748 } else { 1749 /* The descriptor is invalid. Allocate a new B table. */ 1750 b_tbl = get_b_table(); 1751 1752 /* Point the parent A table descriptor to this new B table. */ 1753 a_dte->addr.raw = mmu_vtop(b_tbl->bt_dtbl); 1754 a_dte->attr.raw = MMU_LONG_DTE_LU | MMU_DT_SHORT; 1755 a_tbl->at_ecnt++; /* Update parent's valid entry count */ 1756 1757 /* Create the necessary back references to the parent table */ 1758 b_tbl->bt_parent = a_tbl; 1759 b_tbl->bt_pidx = a_idx; 1760 1761 /* 1762 * If this table is to be wired, make sure the parent A table 1763 * wired count is updated to reflect that it has another wired 1764 * entry. 1765 */ 1766 if (wired) 1767 a_tbl->at_wcnt++; 1768 else if (llevel == NONE) 1769 llevel = NEWB; 1770 } 1771 1772 /* 1773 * Step 3 - Walk into the C table, if there is no valid C table, 1774 * allocate one. 1775 */ 1776 1777 b_idx = MMU_TIB(va); /* Calculate the TIB of the VA */ 1778 b_dte = &b_tbl->bt_dtbl[b_idx]; /* Retrieve descriptor from table */ 1779 if (MMU_VALID_DT(*b_dte)) { /* Is the descriptor valid? */ 1780 /* The descriptor is valid. Use the C table it points to. */ 1781 /************************************** 1782 * c_idx * 1783 * | v * 1784 * \- b_tbl -> +-+-+-+-+-+-+-+-+-+-+- * 1785 * | | | | | | | | | | | * 1786 * +-+-+-+-+-+-+-+-+-+-+- * 1787 * | * 1788 * \- c_tbl -> +-+-- * 1789 * | | | * 1790 * +-+-- * 1791 **************************************/ 1792 c_pte = mmu_ptov(MMU_PTE_PA(*b_dte)); 1793 c_tbl = mmuC2tmgr(c_pte); 1794 1795 /* If mapping is wired and table is not */ 1796 if (wired && !c_tbl->ct_wcnt) { 1797 TAILQ_REMOVE(&c_pool, c_tbl, ct_link); 1798 b_tbl->bt_wcnt++; 1799 } 1800 } else { 1801 /* The descriptor is invalid. Allocate a new C table. */ 1802 c_tbl = get_c_table(); 1803 1804 /* Point the parent B table descriptor to this new C table. */ 1805 b_dte->attr.raw = mmu_vtop(c_tbl->ct_dtbl); 1806 b_dte->attr.raw |= MMU_DT_SHORT; 1807 b_tbl->bt_ecnt++; /* Update parent's valid entry count */ 1808 1809 /* Create the necessary back references to the parent table */ 1810 c_tbl->ct_parent = b_tbl; 1811 c_tbl->ct_pidx = b_idx; 1812 /* 1813 * Store the pmap and base virtual managed address for faster 1814 * retrieval in the PV functions. 1815 */ 1816 c_tbl->ct_pmap = pmap; 1817 c_tbl->ct_va = (va & (MMU_TIA_MASK|MMU_TIB_MASK)); 1818 1819 /* 1820 * If this table is to be wired, make sure the parent B table 1821 * wired count is updated to reflect that it has another wired 1822 * entry. 1823 */ 1824 if (wired) 1825 b_tbl->bt_wcnt++; 1826 else if (llevel == NONE) 1827 llevel = NEWC; 1828 } 1829 1830 /* 1831 * Step 4 - Deposit a page descriptor (PTE) into the appropriate 1832 * slot of the C table, describing the PA to which the VA is mapped. 1833 */ 1834 1835 pte_idx = MMU_TIC(va); 1836 c_pte = &c_tbl->ct_dtbl[pte_idx]; 1837 if (MMU_VALID_DT(*c_pte)) { /* Is the entry currently valid? */ 1838 /* 1839 * The PTE is currently valid. This particular call 1840 * is just a synonym for one (or more) of the following 1841 * operations: 1842 * change protection of a page 1843 * change wiring status of a page 1844 * remove the mapping of a page 1845 * 1846 * XXX - Semi critical: This code should unwire the PTE 1847 * and, possibly, associated parent tables if this is a 1848 * change wiring operation. Currently it does not. 1849 * 1850 * This may be ok if pmap_unwire() is the only 1851 * interface used to UNWIRE a page. 1852 */ 1853 1854 /* First check if this is a wiring operation. */ 1855 if (wired && (c_pte->attr.raw & MMU_SHORT_PTE_WIRED)) { 1856 /* 1857 * The PTE is already wired. To prevent it from being 1858 * counted as a new wiring operation, reset the 'wired' 1859 * variable. 1860 */ 1861 wired = FALSE; 1862 } 1863 1864 /* Is the new address the same as the old? */ 1865 if (MMU_PTE_PA(*c_pte) == pa) { 1866 /* 1867 * Yes, mark that it does not need to be reinserted 1868 * into the PV list. 1869 */ 1870 insert = FALSE; 1871 1872 /* 1873 * Clear all but the modified, referenced and wired 1874 * bits on the PTE. 1875 */ 1876 c_pte->attr.raw &= (MMU_SHORT_PTE_M 1877 | MMU_SHORT_PTE_USED | MMU_SHORT_PTE_WIRED); 1878 } else { 1879 /* No, remove the old entry */ 1880 pmap_remove_pte(c_pte); 1881 insert = TRUE; 1882 } 1883 1884 /* 1885 * TLB flush is only necessary if modifying current map. 1886 * However, in pmap_enter(), the pmap almost always IS 1887 * the current pmap, so don't even bother to check. 1888 */ 1889 TBIS(va); 1890 } else { 1891 /* 1892 * The PTE is invalid. Increment the valid entry count in 1893 * the C table manager to reflect the addition of a new entry. 1894 */ 1895 c_tbl->ct_ecnt++; 1896 1897 /* XXX - temporarily make sure the PTE is cleared. */ 1898 c_pte->attr.raw = 0; 1899 1900 /* It will also need to be inserted into the PV list. */ 1901 insert = TRUE; 1902 } 1903 1904 /* 1905 * If page is changing from unwired to wired status, set an unused bit 1906 * within the PTE to indicate that it is wired. Also increment the 1907 * wired entry count in the C table manager. 1908 */ 1909 if (wired) { 1910 c_pte->attr.raw |= MMU_SHORT_PTE_WIRED; 1911 c_tbl->ct_wcnt++; 1912 } 1913 1914 /* 1915 * Map the page, being careful to preserve modify/reference/wired 1916 * bits. At this point it is assumed that the PTE either has no bits 1917 * set, or if there are set bits, they are only modified, reference or 1918 * wired bits. If not, the following statement will cause erratic 1919 * behavior. 1920 */ 1921 #ifdef PMAP_DEBUG 1922 if (c_pte->attr.raw & ~(MMU_SHORT_PTE_M | 1923 MMU_SHORT_PTE_USED | MMU_SHORT_PTE_WIRED)) { 1924 printf("pmap_enter: junk left in PTE at %p\n", c_pte); 1925 Debugger(); 1926 } 1927 #endif 1928 c_pte->attr.raw |= ((u_long) pa | MMU_DT_PAGE); 1929 1930 /* 1931 * If the mapping should be read-only, set the write protect 1932 * bit in the PTE. 1933 */ 1934 if (!(prot & VM_PROT_WRITE)) 1935 c_pte->attr.raw |= MMU_SHORT_PTE_WP; 1936 1937 /* 1938 * If the mapping should be cache inhibited (indicated by the flag 1939 * bits found on the lower order of the physical address.) 1940 * mark the PTE as a cache inhibited page. 1941 */ 1942 if (mapflags & PMAP_NC) 1943 c_pte->attr.raw |= MMU_SHORT_PTE_CI; 1944 1945 /* 1946 * If the physical address being mapped is managed by the PV 1947 * system then link the pte into the list of pages mapped to that 1948 * address. 1949 */ 1950 if (insert && managed) { 1951 pv = pa2pv(pa); 1952 nidx = pteidx(c_pte); 1953 1954 pvebase[nidx].pve_next = pv->pv_idx; 1955 pv->pv_idx = nidx; 1956 } 1957 1958 /* Move any allocated tables back into the active pool. */ 1959 1960 switch (llevel) { 1961 case NEWA: 1962 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link); 1963 /* FALLTHROUGH */ 1964 case NEWB: 1965 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link); 1966 /* FALLTHROUGH */ 1967 case NEWC: 1968 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link); 1969 /* FALLTHROUGH */ 1970 default: 1971 break; 1972 } 1973 1974 return 0; 1975 } 1976 1977 /* pmap_enter_kernel INTERNAL 1978 ** 1979 * Map the given virtual address to the given physical address within the 1980 * kernel address space. This function exists because the kernel map does 1981 * not do dynamic table allocation. It consists of a contiguous array of ptes 1982 * and can be edited directly without the need to walk through any tables. 1983 * 1984 * XXX: "Danger, Will Robinson!" 1985 * Note that the kernel should never take a fault on any page 1986 * between [ KERNBASE .. virtual_avail ] and this is checked in 1987 * trap.c for kernel-mode MMU faults. This means that mappings 1988 * created in that range must be implicily wired. -gwr 1989 */ 1990 void 1991 pmap_enter_kernel(va, pa, prot) 1992 vaddr_t va; 1993 paddr_t pa; 1994 vm_prot_t prot; 1995 { 1996 boolean_t was_valid, insert; 1997 u_short pte_idx; 1998 int flags; 1999 mmu_short_pte_t *pte; 2000 pv_t *pv; 2001 paddr_t old_pa; 2002 2003 flags = (pa & ~MMU_PAGE_MASK); 2004 pa &= MMU_PAGE_MASK; 2005 2006 if (is_managed(pa)) 2007 insert = TRUE; 2008 else 2009 insert = FALSE; 2010 2011 /* 2012 * Calculate the index of the PTE being modified. 2013 */ 2014 pte_idx = (u_long) m68k_btop(va - KERNBASE); 2015 2016 /* This array is traditionally named "Sysmap" */ 2017 pte = &kernCbase[pte_idx]; 2018 2019 if (MMU_VALID_DT(*pte)) { 2020 was_valid = TRUE; 2021 /* 2022 * If the PTE already maps a different 2023 * physical address, umap and pv_unlink. 2024 */ 2025 old_pa = MMU_PTE_PA(*pte); 2026 if (pa != old_pa) 2027 pmap_remove_pte(pte); 2028 else { 2029 /* 2030 * Old PA and new PA are the same. No need to 2031 * relink the mapping within the PV list. 2032 */ 2033 insert = FALSE; 2034 2035 /* 2036 * Save any mod/ref bits on the PTE. 2037 */ 2038 pte->attr.raw &= (MMU_SHORT_PTE_USED|MMU_SHORT_PTE_M); 2039 } 2040 } else { 2041 pte->attr.raw = MMU_DT_INVALID; 2042 was_valid = FALSE; 2043 } 2044 2045 /* 2046 * Map the page. Being careful to preserve modified/referenced bits 2047 * on the PTE. 2048 */ 2049 pte->attr.raw |= (pa | MMU_DT_PAGE); 2050 2051 if (!(prot & VM_PROT_WRITE)) /* If access should be read-only */ 2052 pte->attr.raw |= MMU_SHORT_PTE_WP; 2053 if (flags & PMAP_NC) 2054 pte->attr.raw |= MMU_SHORT_PTE_CI; 2055 if (was_valid) 2056 TBIS(va); 2057 2058 /* 2059 * Insert the PTE into the PV system, if need be. 2060 */ 2061 if (insert) { 2062 pv = pa2pv(pa); 2063 pvebase[pte_idx].pve_next = pv->pv_idx; 2064 pv->pv_idx = pte_idx; 2065 } 2066 } 2067 2068 void 2069 pmap_kenter_pa(va, pa, prot) 2070 vaddr_t va; 2071 paddr_t pa; 2072 vm_prot_t prot; 2073 { 2074 mmu_short_pte_t *pte; 2075 2076 /* This array is traditionally named "Sysmap" */ 2077 pte = &kernCbase[(u_long)m68k_btop(va - KERNBASE)]; 2078 2079 KASSERT(!MMU_VALID_DT(*pte)); 2080 pte->attr.raw = MMU_DT_INVALID | MMU_DT_PAGE | (pa & MMU_PAGE_MASK); 2081 if (!(prot & VM_PROT_WRITE)) 2082 pte->attr.raw |= MMU_SHORT_PTE_WP; 2083 } 2084 2085 void 2086 pmap_kremove(va, len) 2087 vaddr_t va; 2088 vsize_t len; 2089 { 2090 int idx, eidx; 2091 2092 #ifdef PMAP_DEBUG 2093 if ((sva & PGOFSET) || (eva & PGOFSET)) 2094 panic("pmap_kremove: alignment"); 2095 #endif 2096 2097 idx = m68k_btop(va - KERNBASE); 2098 eidx = m68k_btop(va + len - KERNBASE); 2099 2100 while (idx < eidx) { 2101 kernCbase[idx++].attr.raw = MMU_DT_INVALID; 2102 TBIS(va); 2103 va += PAGE_SIZE; 2104 } 2105 } 2106 2107 /* pmap_map INTERNAL 2108 ** 2109 * Map a contiguous range of physical memory into a contiguous range of 2110 * the kernel virtual address space. 2111 * 2112 * Used for device mappings and early mapping of the kernel text/data/bss. 2113 * Returns the first virtual address beyond the end of the range. 2114 */ 2115 vaddr_t 2116 pmap_map(va, pa, endpa, prot) 2117 vaddr_t va; 2118 paddr_t pa; 2119 paddr_t endpa; 2120 int prot; 2121 { 2122 int sz; 2123 2124 sz = endpa - pa; 2125 do { 2126 pmap_enter_kernel(va, pa, prot); 2127 va += PAGE_SIZE; 2128 pa += PAGE_SIZE; 2129 sz -= PAGE_SIZE; 2130 } while (sz > 0); 2131 pmap_update(pmap_kernel()); 2132 return(va); 2133 } 2134 2135 /* pmap_protect INTERFACE 2136 ** 2137 * Apply the given protection to the given virtual address range within 2138 * the given map. 2139 * 2140 * It is ok for the protection applied to be stronger than what is 2141 * specified. We use this to our advantage when the given map has no 2142 * mapping for the virtual address. By skipping a page when this 2143 * is discovered, we are effectively applying a protection of VM_PROT_NONE, 2144 * and therefore do not need to map the page just to apply a protection 2145 * code. Only pmap_enter() needs to create new mappings if they do not exist. 2146 * 2147 * XXX - This function could be speeded up by using pmap_stroll() for inital 2148 * setup, and then manual scrolling in the for() loop. 2149 */ 2150 void 2151 pmap_protect(pmap, startva, endva, prot) 2152 pmap_t pmap; 2153 vaddr_t startva, endva; 2154 vm_prot_t prot; 2155 { 2156 boolean_t iscurpmap; 2157 int a_idx, b_idx, c_idx; 2158 a_tmgr_t *a_tbl; 2159 b_tmgr_t *b_tbl; 2160 c_tmgr_t *c_tbl; 2161 mmu_short_pte_t *pte; 2162 2163 if (pmap == pmap_kernel()) { 2164 pmap_protect_kernel(startva, endva, prot); 2165 return; 2166 } 2167 2168 /* 2169 * In this particular pmap implementation, there are only three 2170 * types of memory protection: 'all' (read/write/execute), 2171 * 'read-only' (read/execute) and 'none' (no mapping.) 2172 * It is not possible for us to treat 'executable' as a separate 2173 * protection type. Therefore, protection requests that seek to 2174 * remove execute permission while retaining read or write, and those 2175 * that make little sense (write-only for example) are ignored. 2176 */ 2177 switch (prot) { 2178 case VM_PROT_NONE: 2179 /* 2180 * A request to apply the protection code of 2181 * 'VM_PROT_NONE' is a synonym for pmap_remove(). 2182 */ 2183 pmap_remove(pmap, startva, endva); 2184 return; 2185 case VM_PROT_EXECUTE: 2186 case VM_PROT_READ: 2187 case VM_PROT_READ|VM_PROT_EXECUTE: 2188 /* continue */ 2189 break; 2190 case VM_PROT_WRITE: 2191 case VM_PROT_WRITE|VM_PROT_READ: 2192 case VM_PROT_WRITE|VM_PROT_EXECUTE: 2193 case VM_PROT_ALL: 2194 /* None of these should happen in a sane system. */ 2195 return; 2196 } 2197 2198 /* 2199 * If the pmap has no A table, it has no mappings and therefore 2200 * there is nothing to protect. 2201 */ 2202 if ((a_tbl = pmap->pm_a_tmgr) == NULL) 2203 return; 2204 2205 a_idx = MMU_TIA(startva); 2206 b_idx = MMU_TIB(startva); 2207 c_idx = MMU_TIC(startva); 2208 b_tbl = (b_tmgr_t *) c_tbl = NULL; 2209 2210 iscurpmap = (pmap == current_pmap()); 2211 while (startva < endva) { 2212 if (b_tbl || MMU_VALID_DT(a_tbl->at_dtbl[a_idx])) { 2213 if (b_tbl == NULL) { 2214 b_tbl = (b_tmgr_t *) a_tbl->at_dtbl[a_idx].addr.raw; 2215 b_tbl = mmu_ptov((vaddr_t)b_tbl); 2216 b_tbl = mmuB2tmgr((mmu_short_dte_t *)b_tbl); 2217 } 2218 if (c_tbl || MMU_VALID_DT(b_tbl->bt_dtbl[b_idx])) { 2219 if (c_tbl == NULL) { 2220 c_tbl = (c_tmgr_t *) MMU_DTE_PA(b_tbl->bt_dtbl[b_idx]); 2221 c_tbl = mmu_ptov((vaddr_t)c_tbl); 2222 c_tbl = mmuC2tmgr((mmu_short_pte_t *)c_tbl); 2223 } 2224 if (MMU_VALID_DT(c_tbl->ct_dtbl[c_idx])) { 2225 pte = &c_tbl->ct_dtbl[c_idx]; 2226 /* make the mapping read-only */ 2227 pte->attr.raw |= MMU_SHORT_PTE_WP; 2228 /* 2229 * If we just modified the current address space, 2230 * flush any translations for the modified page from 2231 * the translation cache and any data from it in the 2232 * data cache. 2233 */ 2234 if (iscurpmap) 2235 TBIS(startva); 2236 } 2237 startva += PAGE_SIZE; 2238 2239 if (++c_idx >= MMU_C_TBL_SIZE) { /* exceeded C table? */ 2240 c_tbl = NULL; 2241 c_idx = 0; 2242 if (++b_idx >= MMU_B_TBL_SIZE) { /* exceeded B table? */ 2243 b_tbl = NULL; 2244 b_idx = 0; 2245 } 2246 } 2247 } else { /* C table wasn't valid */ 2248 c_tbl = NULL; 2249 c_idx = 0; 2250 startva += MMU_TIB_RANGE; 2251 if (++b_idx >= MMU_B_TBL_SIZE) { /* exceeded B table? */ 2252 b_tbl = NULL; 2253 b_idx = 0; 2254 } 2255 } /* C table */ 2256 } else { /* B table wasn't valid */ 2257 b_tbl = NULL; 2258 b_idx = 0; 2259 startva += MMU_TIA_RANGE; 2260 a_idx++; 2261 } /* B table */ 2262 } 2263 } 2264 2265 /* pmap_protect_kernel INTERNAL 2266 ** 2267 * Apply the given protection code to a kernel address range. 2268 */ 2269 void 2270 pmap_protect_kernel(startva, endva, prot) 2271 vaddr_t startva, endva; 2272 vm_prot_t prot; 2273 { 2274 vaddr_t va; 2275 mmu_short_pte_t *pte; 2276 2277 pte = &kernCbase[(unsigned long) m68k_btop(startva - KERNBASE)]; 2278 for (va = startva; va < endva; va += PAGE_SIZE, pte++) { 2279 if (MMU_VALID_DT(*pte)) { 2280 switch (prot) { 2281 case VM_PROT_ALL: 2282 break; 2283 case VM_PROT_EXECUTE: 2284 case VM_PROT_READ: 2285 case VM_PROT_READ|VM_PROT_EXECUTE: 2286 pte->attr.raw |= MMU_SHORT_PTE_WP; 2287 break; 2288 case VM_PROT_NONE: 2289 /* this is an alias for 'pmap_remove_kernel' */ 2290 pmap_remove_pte(pte); 2291 break; 2292 default: 2293 break; 2294 } 2295 /* 2296 * since this is the kernel, immediately flush any cached 2297 * descriptors for this address. 2298 */ 2299 TBIS(va); 2300 } 2301 } 2302 } 2303 2304 /* pmap_unwire INTERFACE 2305 ** 2306 * Clear the wired attribute of the specified page. 2307 * 2308 * This function is called from vm_fault.c to unwire 2309 * a mapping. 2310 */ 2311 void 2312 pmap_unwire(pmap, va) 2313 pmap_t pmap; 2314 vaddr_t va; 2315 { 2316 int a_idx, b_idx, c_idx; 2317 a_tmgr_t *a_tbl; 2318 b_tmgr_t *b_tbl; 2319 c_tmgr_t *c_tbl; 2320 mmu_short_pte_t *pte; 2321 2322 /* Kernel mappings always remain wired. */ 2323 if (pmap == pmap_kernel()) 2324 return; 2325 2326 /* 2327 * Walk through the tables. If the walk terminates without 2328 * a valid PTE then the address wasn't wired in the first place. 2329 * Return immediately. 2330 */ 2331 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl, &pte, &a_idx, 2332 &b_idx, &c_idx) == FALSE) 2333 return; 2334 2335 2336 /* Is the PTE wired? If not, return. */ 2337 if (!(pte->attr.raw & MMU_SHORT_PTE_WIRED)) 2338 return; 2339 2340 /* Remove the wiring bit. */ 2341 pte->attr.raw &= ~(MMU_SHORT_PTE_WIRED); 2342 2343 /* 2344 * Decrement the wired entry count in the C table. 2345 * If it reaches zero the following things happen: 2346 * 1. The table no longer has any wired entries and is considered 2347 * unwired. 2348 * 2. It is placed on the available queue. 2349 * 3. The parent table's wired entry count is decremented. 2350 * 4. If it reaches zero, this process repeats at step 1 and 2351 * stops at after reaching the A table. 2352 */ 2353 if (--c_tbl->ct_wcnt == 0) { 2354 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link); 2355 if (--b_tbl->bt_wcnt == 0) { 2356 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link); 2357 if (--a_tbl->at_wcnt == 0) { 2358 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link); 2359 } 2360 } 2361 } 2362 } 2363 2364 /* pmap_copy INTERFACE 2365 ** 2366 * Copy the mappings of a range of addresses in one pmap, into 2367 * the destination address of another. 2368 * 2369 * This routine is advisory. Should we one day decide that MMU tables 2370 * may be shared by more than one pmap, this function should be used to 2371 * link them together. Until that day however, we do nothing. 2372 */ 2373 void 2374 pmap_copy(pmap_a, pmap_b, dst, len, src) 2375 pmap_t pmap_a, pmap_b; 2376 vaddr_t dst; 2377 vsize_t len; 2378 vaddr_t src; 2379 { 2380 /* not implemented. */ 2381 } 2382 2383 /* pmap_copy_page INTERFACE 2384 ** 2385 * Copy the contents of one physical page into another. 2386 * 2387 * This function makes use of two virtual pages allocated in pmap_bootstrap() 2388 * to map the two specified physical pages into the kernel address space. 2389 * 2390 * Note: We could use the transparent translation registers to make the 2391 * mappings. If we do so, be sure to disable interrupts before using them. 2392 */ 2393 void 2394 pmap_copy_page(srcpa, dstpa) 2395 paddr_t srcpa, dstpa; 2396 { 2397 vaddr_t srcva, dstva; 2398 int s; 2399 2400 srcva = tmp_vpages[0]; 2401 dstva = tmp_vpages[1]; 2402 2403 s = splvm(); 2404 #ifdef DIAGNOSTIC 2405 if (tmp_vpages_inuse++) 2406 panic("pmap_copy_page: temporary vpages are in use."); 2407 #endif 2408 2409 /* Map pages as non-cacheable to avoid cache polution? */ 2410 pmap_kenter_pa(srcva, srcpa, VM_PROT_READ); 2411 pmap_kenter_pa(dstva, dstpa, VM_PROT_READ|VM_PROT_WRITE); 2412 2413 /* Hand-optimized version of bcopy(src, dst, PAGE_SIZE) */ 2414 copypage((char *) srcva, (char *) dstva); 2415 2416 pmap_kremove(srcva, PAGE_SIZE); 2417 pmap_kremove(dstva, PAGE_SIZE); 2418 2419 #ifdef DIAGNOSTIC 2420 --tmp_vpages_inuse; 2421 #endif 2422 splx(s); 2423 } 2424 2425 /* pmap_zero_page INTERFACE 2426 ** 2427 * Zero the contents of the specified physical page. 2428 * 2429 * Uses one of the virtual pages allocated in pmap_boostrap() 2430 * to map the specified page into the kernel address space. 2431 */ 2432 void 2433 pmap_zero_page(dstpa) 2434 paddr_t dstpa; 2435 { 2436 vaddr_t dstva; 2437 int s; 2438 2439 dstva = tmp_vpages[1]; 2440 s = splvm(); 2441 #ifdef DIAGNOSTIC 2442 if (tmp_vpages_inuse++) 2443 panic("pmap_zero_page: temporary vpages are in use."); 2444 #endif 2445 2446 /* The comments in pmap_copy_page() above apply here also. */ 2447 pmap_kenter_pa(dstva, dstpa, VM_PROT_READ|VM_PROT_WRITE); 2448 2449 /* Hand-optimized version of bzero(ptr, PAGE_SIZE) */ 2450 zeropage((char *) dstva); 2451 2452 pmap_kremove(dstva, PAGE_SIZE); 2453 #ifdef DIAGNOSTIC 2454 --tmp_vpages_inuse; 2455 #endif 2456 splx(s); 2457 } 2458 2459 /* pmap_collect INTERFACE 2460 ** 2461 * Called from the VM system when we are about to swap out 2462 * the process using this pmap. This should give up any 2463 * resources held here, including all its MMU tables. 2464 */ 2465 void 2466 pmap_collect(pmap) 2467 pmap_t pmap; 2468 { 2469 /* XXX - todo... */ 2470 } 2471 2472 /* pmap_create INTERFACE 2473 ** 2474 * Create and return a pmap structure. 2475 */ 2476 pmap_t 2477 pmap_create() 2478 { 2479 pmap_t pmap; 2480 2481 pmap = pool_get(&pmap_pmap_pool, PR_WAITOK); 2482 pmap_pinit(pmap); 2483 return pmap; 2484 } 2485 2486 /* pmap_pinit INTERNAL 2487 ** 2488 * Initialize a pmap structure. 2489 */ 2490 void 2491 pmap_pinit(pmap) 2492 pmap_t pmap; 2493 { 2494 memset(pmap, 0, sizeof(struct pmap)); 2495 pmap->pm_a_tmgr = NULL; 2496 pmap->pm_a_phys = kernAphys; 2497 pmap->pm_refcount = 1; 2498 simple_lock_init(&pmap->pm_lock); 2499 } 2500 2501 /* pmap_release INTERFACE 2502 ** 2503 * Release any resources held by the given pmap. 2504 * 2505 * This is the reverse analog to pmap_pinit. It does not 2506 * necessarily mean for the pmap structure to be deallocated, 2507 * as in pmap_destroy. 2508 */ 2509 void 2510 pmap_release(pmap) 2511 pmap_t pmap; 2512 { 2513 /* 2514 * As long as the pmap contains no mappings, 2515 * which always should be the case whenever 2516 * this function is called, there really should 2517 * be nothing to do. 2518 */ 2519 #ifdef PMAP_DEBUG 2520 if (pmap == pmap_kernel()) 2521 panic("pmap_release: kernel pmap"); 2522 #endif 2523 /* 2524 * XXX - If this pmap has an A table, give it back. 2525 * The pmap SHOULD be empty by now, and pmap_remove 2526 * should have already given back the A table... 2527 * However, I see: pmap->pm_a_tmgr->at_ecnt == 1 2528 * at this point, which means some mapping was not 2529 * removed when it should have been. -gwr 2530 */ 2531 if (pmap->pm_a_tmgr != NULL) { 2532 /* First make sure we are not using it! */ 2533 if (kernel_crp.rp_addr == pmap->pm_a_phys) { 2534 kernel_crp.rp_addr = kernAphys; 2535 loadcrp(&kernel_crp); 2536 } 2537 #ifdef PMAP_DEBUG /* XXX - todo! */ 2538 /* XXX - Now complain... */ 2539 printf("pmap_release: still have table\n"); 2540 Debugger(); 2541 #endif 2542 free_a_table(pmap->pm_a_tmgr, TRUE); 2543 pmap->pm_a_tmgr = NULL; 2544 pmap->pm_a_phys = kernAphys; 2545 } 2546 } 2547 2548 /* pmap_reference INTERFACE 2549 ** 2550 * Increment the reference count of a pmap. 2551 */ 2552 void 2553 pmap_reference(pmap) 2554 pmap_t pmap; 2555 { 2556 pmap_lock(pmap); 2557 pmap_add_ref(pmap); 2558 pmap_unlock(pmap); 2559 } 2560 2561 /* pmap_dereference INTERNAL 2562 ** 2563 * Decrease the reference count on the given pmap 2564 * by one and return the current count. 2565 */ 2566 int 2567 pmap_dereference(pmap) 2568 pmap_t pmap; 2569 { 2570 int rtn; 2571 2572 pmap_lock(pmap); 2573 rtn = pmap_del_ref(pmap); 2574 pmap_unlock(pmap); 2575 2576 return rtn; 2577 } 2578 2579 /* pmap_destroy INTERFACE 2580 ** 2581 * Decrement a pmap's reference count and delete 2582 * the pmap if it becomes zero. Will be called 2583 * only after all mappings have been removed. 2584 */ 2585 void 2586 pmap_destroy(pmap) 2587 pmap_t pmap; 2588 { 2589 if (pmap_dereference(pmap) == 0) { 2590 pmap_release(pmap); 2591 pool_put(&pmap_pmap_pool, pmap); 2592 } 2593 } 2594 2595 /* pmap_is_referenced INTERFACE 2596 ** 2597 * Determine if the given physical page has been 2598 * referenced (read from [or written to.]) 2599 */ 2600 boolean_t 2601 pmap_is_referenced(pg) 2602 struct vm_page *pg; 2603 { 2604 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2605 pv_t *pv; 2606 int idx; 2607 2608 /* 2609 * Check the flags on the pv head. If they are set, 2610 * return immediately. Otherwise a search must be done. 2611 */ 2612 2613 pv = pa2pv(pa); 2614 if (pv->pv_flags & PV_FLAGS_USED) 2615 return TRUE; 2616 2617 /* 2618 * Search through all pv elements pointing 2619 * to this page and query their reference bits 2620 */ 2621 2622 for (idx = pv->pv_idx; idx != PVE_EOL; idx = pvebase[idx].pve_next) { 2623 if (MMU_PTE_USED(kernCbase[idx])) { 2624 return TRUE; 2625 } 2626 } 2627 return FALSE; 2628 } 2629 2630 /* pmap_is_modified INTERFACE 2631 ** 2632 * Determine if the given physical page has been 2633 * modified (written to.) 2634 */ 2635 boolean_t 2636 pmap_is_modified(pg) 2637 struct vm_page *pg; 2638 { 2639 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2640 pv_t *pv; 2641 int idx; 2642 2643 /* see comments in pmap_is_referenced() */ 2644 pv = pa2pv(pa); 2645 if (pv->pv_flags & PV_FLAGS_MDFY) 2646 return TRUE; 2647 2648 for (idx = pv->pv_idx; 2649 idx != PVE_EOL; 2650 idx = pvebase[idx].pve_next) { 2651 2652 if (MMU_PTE_MODIFIED(kernCbase[idx])) { 2653 return TRUE; 2654 } 2655 } 2656 2657 return FALSE; 2658 } 2659 2660 /* pmap_page_protect INTERFACE 2661 ** 2662 * Applies the given protection to all mappings to the given 2663 * physical page. 2664 */ 2665 void 2666 pmap_page_protect(pg, prot) 2667 struct vm_page *pg; 2668 vm_prot_t prot; 2669 { 2670 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2671 pv_t *pv; 2672 int idx; 2673 vaddr_t va; 2674 struct mmu_short_pte_struct *pte; 2675 c_tmgr_t *c_tbl; 2676 pmap_t pmap, curpmap; 2677 2678 curpmap = current_pmap(); 2679 pv = pa2pv(pa); 2680 2681 for (idx = pv->pv_idx; idx != PVE_EOL; idx = pvebase[idx].pve_next) { 2682 pte = &kernCbase[idx]; 2683 switch (prot) { 2684 case VM_PROT_ALL: 2685 /* do nothing */ 2686 break; 2687 case VM_PROT_EXECUTE: 2688 case VM_PROT_READ: 2689 case VM_PROT_READ|VM_PROT_EXECUTE: 2690 /* 2691 * Determine the virtual address mapped by 2692 * the PTE and flush ATC entries if necessary. 2693 */ 2694 va = pmap_get_pteinfo(idx, &pmap, &c_tbl); 2695 pte->attr.raw |= MMU_SHORT_PTE_WP; 2696 if (pmap == curpmap || pmap == pmap_kernel()) 2697 TBIS(va); 2698 break; 2699 case VM_PROT_NONE: 2700 /* Save the mod/ref bits. */ 2701 pv->pv_flags |= pte->attr.raw; 2702 /* Invalidate the PTE. */ 2703 pte->attr.raw = MMU_DT_INVALID; 2704 2705 /* 2706 * Update table counts. And flush ATC entries 2707 * if necessary. 2708 */ 2709 va = pmap_get_pteinfo(idx, &pmap, &c_tbl); 2710 2711 /* 2712 * If the PTE belongs to the kernel map, 2713 * be sure to flush the page it maps. 2714 */ 2715 if (pmap == pmap_kernel()) { 2716 TBIS(va); 2717 } else { 2718 /* 2719 * The PTE belongs to a user map. 2720 * update the entry count in the C 2721 * table to which it belongs and flush 2722 * the ATC if the mapping belongs to 2723 * the current pmap. 2724 */ 2725 c_tbl->ct_ecnt--; 2726 if (pmap == curpmap) 2727 TBIS(va); 2728 } 2729 break; 2730 default: 2731 break; 2732 } 2733 } 2734 2735 /* 2736 * If the protection code indicates that all mappings to the page 2737 * be removed, truncate the PV list to zero entries. 2738 */ 2739 if (prot == VM_PROT_NONE) 2740 pv->pv_idx = PVE_EOL; 2741 } 2742 2743 /* pmap_get_pteinfo INTERNAL 2744 ** 2745 * Called internally to find the pmap and virtual address within that 2746 * map to which the pte at the given index maps. Also includes the PTE's C 2747 * table manager. 2748 * 2749 * Returns the pmap in the argument provided, and the virtual address 2750 * by return value. 2751 */ 2752 vaddr_t 2753 pmap_get_pteinfo(idx, pmap, tbl) 2754 u_int idx; 2755 pmap_t *pmap; 2756 c_tmgr_t **tbl; 2757 { 2758 vaddr_t va = 0; 2759 2760 /* 2761 * Determine if the PTE is a kernel PTE or a user PTE. 2762 */ 2763 if (idx >= NUM_KERN_PTES) { 2764 /* 2765 * The PTE belongs to a user mapping. 2766 */ 2767 /* XXX: Would like an inline for this to validate idx... */ 2768 *tbl = &Ctmgrbase[(idx - NUM_KERN_PTES) / MMU_C_TBL_SIZE]; 2769 2770 *pmap = (*tbl)->ct_pmap; 2771 /* 2772 * To find the va to which the PTE maps, we first take 2773 * the table's base virtual address mapping which is stored 2774 * in ct_va. We then increment this address by a page for 2775 * every slot skipped until we reach the PTE. 2776 */ 2777 va = (*tbl)->ct_va; 2778 va += m68k_ptob(idx % MMU_C_TBL_SIZE); 2779 } else { 2780 /* 2781 * The PTE belongs to the kernel map. 2782 */ 2783 *pmap = pmap_kernel(); 2784 2785 va = m68k_ptob(idx); 2786 va += KERNBASE; 2787 } 2788 2789 return va; 2790 } 2791 2792 /* pmap_clear_modify INTERFACE 2793 ** 2794 * Clear the modification bit on the page at the specified 2795 * physical address. 2796 * 2797 */ 2798 boolean_t 2799 pmap_clear_modify(pg) 2800 struct vm_page *pg; 2801 { 2802 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2803 boolean_t rv; 2804 2805 rv = pmap_is_modified(pg); 2806 pmap_clear_pv(pa, PV_FLAGS_MDFY); 2807 return rv; 2808 } 2809 2810 /* pmap_clear_reference INTERFACE 2811 ** 2812 * Clear the referenced bit on the page at the specified 2813 * physical address. 2814 */ 2815 boolean_t 2816 pmap_clear_reference(pg) 2817 struct vm_page *pg; 2818 { 2819 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2820 boolean_t rv; 2821 2822 rv = pmap_is_referenced(pg); 2823 pmap_clear_pv(pa, PV_FLAGS_USED); 2824 return rv; 2825 } 2826 2827 /* pmap_clear_pv INTERNAL 2828 ** 2829 * Clears the specified flag from the specified physical address. 2830 * (Used by pmap_clear_modify() and pmap_clear_reference().) 2831 * 2832 * Flag is one of: 2833 * PV_FLAGS_MDFY - Page modified bit. 2834 * PV_FLAGS_USED - Page used (referenced) bit. 2835 * 2836 * This routine must not only clear the flag on the pv list 2837 * head. It must also clear the bit on every pte in the pv 2838 * list associated with the address. 2839 */ 2840 void 2841 pmap_clear_pv(pa, flag) 2842 paddr_t pa; 2843 int flag; 2844 { 2845 pv_t *pv; 2846 int idx; 2847 vaddr_t va; 2848 pmap_t pmap; 2849 mmu_short_pte_t *pte; 2850 c_tmgr_t *c_tbl; 2851 2852 pv = pa2pv(pa); 2853 pv->pv_flags &= ~(flag); 2854 for (idx = pv->pv_idx; idx != PVE_EOL; idx = pvebase[idx].pve_next) { 2855 pte = &kernCbase[idx]; 2856 pte->attr.raw &= ~(flag); 2857 2858 /* 2859 * The MC68030 MMU will not set the modified or 2860 * referenced bits on any MMU tables for which it has 2861 * a cached descriptor with its modify bit set. To insure 2862 * that it will modify these bits on the PTE during the next 2863 * time it is written to or read from, we must flush it from 2864 * the ATC. 2865 * 2866 * Ordinarily it is only necessary to flush the descriptor 2867 * if it is used in the current address space. But since I 2868 * am not sure that there will always be a notion of 2869 * 'the current address space' when this function is called, 2870 * I will skip the test and always flush the address. It 2871 * does no harm. 2872 */ 2873 2874 va = pmap_get_pteinfo(idx, &pmap, &c_tbl); 2875 TBIS(va); 2876 } 2877 } 2878 2879 /* pmap_extract INTERFACE 2880 ** 2881 * Return the physical address mapped by the virtual address 2882 * in the specified pmap. 2883 * 2884 * Note: this function should also apply an exclusive lock 2885 * on the pmap system during its duration. 2886 */ 2887 boolean_t 2888 pmap_extract(pmap, va, pap) 2889 pmap_t pmap; 2890 vaddr_t va; 2891 paddr_t *pap; 2892 { 2893 int a_idx, b_idx, pte_idx; 2894 a_tmgr_t *a_tbl; 2895 b_tmgr_t *b_tbl; 2896 c_tmgr_t *c_tbl; 2897 mmu_short_pte_t *c_pte; 2898 2899 if (pmap == pmap_kernel()) 2900 return pmap_extract_kernel(va, pap); 2901 2902 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl, 2903 &c_pte, &a_idx, &b_idx, &pte_idx) == FALSE) 2904 return FALSE; 2905 2906 if (!MMU_VALID_DT(*c_pte)) 2907 return FALSE; 2908 2909 if (pap != NULL) 2910 *pap = MMU_PTE_PA(*c_pte); 2911 return (TRUE); 2912 } 2913 2914 /* pmap_extract_kernel INTERNAL 2915 ** 2916 * Extract a translation from the kernel address space. 2917 */ 2918 boolean_t 2919 pmap_extract_kernel(va, pap) 2920 vaddr_t va; 2921 paddr_t *pap; 2922 { 2923 mmu_short_pte_t *pte; 2924 2925 pte = &kernCbase[(u_int) m68k_btop(va - KERNBASE)]; 2926 if (!MMU_VALID_DT(*pte)) 2927 return (FALSE); 2928 if (pap != NULL) 2929 *pap = MMU_PTE_PA(*pte); 2930 return (TRUE); 2931 } 2932 2933 /* pmap_remove_kernel INTERNAL 2934 ** 2935 * Remove the mapping of a range of virtual addresses from the kernel map. 2936 * The arguments are already page-aligned. 2937 */ 2938 void 2939 pmap_remove_kernel(sva, eva) 2940 vaddr_t sva; 2941 vaddr_t eva; 2942 { 2943 int idx, eidx; 2944 2945 #ifdef PMAP_DEBUG 2946 if ((sva & PGOFSET) || (eva & PGOFSET)) 2947 panic("pmap_remove_kernel: alignment"); 2948 #endif 2949 2950 idx = m68k_btop(sva - KERNBASE); 2951 eidx = m68k_btop(eva - KERNBASE); 2952 2953 while (idx < eidx) { 2954 pmap_remove_pte(&kernCbase[idx++]); 2955 TBIS(sva); 2956 sva += PAGE_SIZE; 2957 } 2958 } 2959 2960 /* pmap_remove INTERFACE 2961 ** 2962 * Remove the mapping of a range of virtual addresses from the given pmap. 2963 * 2964 * If the range contains any wired entries, this function will probably create 2965 * disaster. 2966 */ 2967 void 2968 pmap_remove(pmap, start, end) 2969 pmap_t pmap; 2970 vaddr_t start; 2971 vaddr_t end; 2972 { 2973 2974 if (pmap == pmap_kernel()) { 2975 pmap_remove_kernel(start, end); 2976 return; 2977 } 2978 2979 /* 2980 * If the pmap doesn't have an A table of its own, it has no mappings 2981 * that can be removed. 2982 */ 2983 if (pmap->pm_a_tmgr == NULL) 2984 return; 2985 2986 /* 2987 * Remove the specified range from the pmap. If the function 2988 * returns true, the operation removed all the valid mappings 2989 * in the pmap and freed its A table. If this happened to the 2990 * currently loaded pmap, the MMU root pointer must be reloaded 2991 * with the default 'kernel' map. 2992 */ 2993 if (pmap_remove_a(pmap->pm_a_tmgr, start, end)) { 2994 if (kernel_crp.rp_addr == pmap->pm_a_phys) { 2995 kernel_crp.rp_addr = kernAphys; 2996 loadcrp(&kernel_crp); 2997 /* will do TLB flush below */ 2998 } 2999 pmap->pm_a_tmgr = NULL; 3000 pmap->pm_a_phys = kernAphys; 3001 } 3002 3003 /* 3004 * If we just modified the current address space, 3005 * make sure to flush the MMU cache. 3006 * 3007 * XXX - this could be an unecessarily large flush. 3008 * XXX - Could decide, based on the size of the VA range 3009 * to be removed, whether to flush "by pages" or "all". 3010 */ 3011 if (pmap == current_pmap()) 3012 TBIAU(); 3013 } 3014 3015 /* pmap_remove_a INTERNAL 3016 ** 3017 * This is function number one in a set of three that removes a range 3018 * of memory in the most efficient manner by removing the highest possible 3019 * tables from the memory space. This particular function attempts to remove 3020 * as many B tables as it can, delegating the remaining fragmented ranges to 3021 * pmap_remove_b(). 3022 * 3023 * If the removal operation results in an empty A table, the function returns 3024 * TRUE. 3025 * 3026 * It's ugly but will do for now. 3027 */ 3028 boolean_t 3029 pmap_remove_a(a_tbl, start, end) 3030 a_tmgr_t *a_tbl; 3031 vaddr_t start; 3032 vaddr_t end; 3033 { 3034 boolean_t empty; 3035 int idx; 3036 vaddr_t nstart, nend; 3037 b_tmgr_t *b_tbl; 3038 mmu_long_dte_t *a_dte; 3039 mmu_short_dte_t *b_dte; 3040 3041 /* 3042 * The following code works with what I call a 'granularity 3043 * reduction algorithim'. A range of addresses will always have 3044 * the following properties, which are classified according to 3045 * how the range relates to the size of the current granularity 3046 * - an A table entry: 3047 * 3048 * 1 2 3 4 3049 * -+---+---+---+---+---+---+---+- 3050 * -+---+---+---+---+---+---+---+- 3051 * 3052 * A range will always start on a granularity boundary, illustrated 3053 * by '+' signs in the table above, or it will start at some point 3054 * inbetween a granularity boundary, as illustrated by point 1. 3055 * The first step in removing a range of addresses is to remove the 3056 * range between 1 and 2, the nearest granularity boundary. This 3057 * job is handled by the section of code governed by the 3058 * 'if (start < nstart)' statement. 3059 * 3060 * A range will always encompass zero or more intergral granules, 3061 * illustrated by points 2 and 3. Integral granules are easy to 3062 * remove. The removal of these granules is the second step, and 3063 * is handled by the code block 'if (nstart < nend)'. 3064 * 3065 * Lastly, a range will always end on a granularity boundary, 3066 * ill. by point 3, or it will fall just beyond one, ill. by point 3067 * 4. The last step involves removing this range and is handled by 3068 * the code block 'if (nend < end)'. 3069 */ 3070 nstart = MMU_ROUND_UP_A(start); 3071 nend = MMU_ROUND_A(end); 3072 3073 if (start < nstart) { 3074 /* 3075 * This block is executed if the range starts between 3076 * a granularity boundary. 3077 * 3078 * First find the DTE which is responsible for mapping 3079 * the start of the range. 3080 */ 3081 idx = MMU_TIA(start); 3082 a_dte = &a_tbl->at_dtbl[idx]; 3083 3084 /* 3085 * If the DTE is valid then delegate the removal of the sub 3086 * range to pmap_remove_b(), which can remove addresses at 3087 * a finer granularity. 3088 */ 3089 if (MMU_VALID_DT(*a_dte)) { 3090 b_dte = mmu_ptov(a_dte->addr.raw); 3091 b_tbl = mmuB2tmgr(b_dte); 3092 3093 /* 3094 * The sub range to be removed starts at the start 3095 * of the full range we were asked to remove, and ends 3096 * at the greater of: 3097 * 1. The end of the full range, -or- 3098 * 2. The end of the full range, rounded down to the 3099 * nearest granularity boundary. 3100 */ 3101 if (end < nstart) 3102 empty = pmap_remove_b(b_tbl, start, end); 3103 else 3104 empty = pmap_remove_b(b_tbl, start, nstart); 3105 3106 /* 3107 * If the removal resulted in an empty B table, 3108 * invalidate the DTE that points to it and decrement 3109 * the valid entry count of the A table. 3110 */ 3111 if (empty) { 3112 a_dte->attr.raw = MMU_DT_INVALID; 3113 a_tbl->at_ecnt--; 3114 } 3115 } 3116 /* 3117 * If the DTE is invalid, the address range is already non- 3118 * existent and can simply be skipped. 3119 */ 3120 } 3121 if (nstart < nend) { 3122 /* 3123 * This block is executed if the range spans a whole number 3124 * multiple of granules (A table entries.) 3125 * 3126 * First find the DTE which is responsible for mapping 3127 * the start of the first granule involved. 3128 */ 3129 idx = MMU_TIA(nstart); 3130 a_dte = &a_tbl->at_dtbl[idx]; 3131 3132 /* 3133 * Remove entire sub-granules (B tables) one at a time, 3134 * until reaching the end of the range. 3135 */ 3136 for (; nstart < nend; a_dte++, nstart += MMU_TIA_RANGE) 3137 if (MMU_VALID_DT(*a_dte)) { 3138 /* 3139 * Find the B table manager for the 3140 * entry and free it. 3141 */ 3142 b_dte = mmu_ptov(a_dte->addr.raw); 3143 b_tbl = mmuB2tmgr(b_dte); 3144 free_b_table(b_tbl, TRUE); 3145 3146 /* 3147 * Invalidate the DTE that points to the 3148 * B table and decrement the valid entry 3149 * count of the A table. 3150 */ 3151 a_dte->attr.raw = MMU_DT_INVALID; 3152 a_tbl->at_ecnt--; 3153 } 3154 } 3155 if (nend < end) { 3156 /* 3157 * This block is executed if the range ends beyond a 3158 * granularity boundary. 3159 * 3160 * First find the DTE which is responsible for mapping 3161 * the start of the nearest (rounded down) granularity 3162 * boundary. 3163 */ 3164 idx = MMU_TIA(nend); 3165 a_dte = &a_tbl->at_dtbl[idx]; 3166 3167 /* 3168 * If the DTE is valid then delegate the removal of the sub 3169 * range to pmap_remove_b(), which can remove addresses at 3170 * a finer granularity. 3171 */ 3172 if (MMU_VALID_DT(*a_dte)) { 3173 /* 3174 * Find the B table manager for the entry 3175 * and hand it to pmap_remove_b() along with 3176 * the sub range. 3177 */ 3178 b_dte = mmu_ptov(a_dte->addr.raw); 3179 b_tbl = mmuB2tmgr(b_dte); 3180 3181 empty = pmap_remove_b(b_tbl, nend, end); 3182 3183 /* 3184 * If the removal resulted in an empty B table, 3185 * invalidate the DTE that points to it and decrement 3186 * the valid entry count of the A table. 3187 */ 3188 if (empty) { 3189 a_dte->attr.raw = MMU_DT_INVALID; 3190 a_tbl->at_ecnt--; 3191 } 3192 } 3193 } 3194 3195 /* 3196 * If there are no more entries in the A table, release it 3197 * back to the available pool and return TRUE. 3198 */ 3199 if (a_tbl->at_ecnt == 0) { 3200 a_tbl->at_parent = NULL; 3201 TAILQ_REMOVE(&a_pool, a_tbl, at_link); 3202 TAILQ_INSERT_HEAD(&a_pool, a_tbl, at_link); 3203 empty = TRUE; 3204 } else { 3205 empty = FALSE; 3206 } 3207 3208 return empty; 3209 } 3210 3211 /* pmap_remove_b INTERNAL 3212 ** 3213 * Remove a range of addresses from an address space, trying to remove entire 3214 * C tables if possible. 3215 * 3216 * If the operation results in an empty B table, the function returns TRUE. 3217 */ 3218 boolean_t 3219 pmap_remove_b(b_tbl, start, end) 3220 b_tmgr_t *b_tbl; 3221 vaddr_t start; 3222 vaddr_t end; 3223 { 3224 boolean_t empty; 3225 int idx; 3226 vaddr_t nstart, nend, rstart; 3227 c_tmgr_t *c_tbl; 3228 mmu_short_dte_t *b_dte; 3229 mmu_short_pte_t *c_dte; 3230 3231 3232 nstart = MMU_ROUND_UP_B(start); 3233 nend = MMU_ROUND_B(end); 3234 3235 if (start < nstart) { 3236 idx = MMU_TIB(start); 3237 b_dte = &b_tbl->bt_dtbl[idx]; 3238 if (MMU_VALID_DT(*b_dte)) { 3239 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte)); 3240 c_tbl = mmuC2tmgr(c_dte); 3241 if (end < nstart) 3242 empty = pmap_remove_c(c_tbl, start, end); 3243 else 3244 empty = pmap_remove_c(c_tbl, start, nstart); 3245 if (empty) { 3246 b_dte->attr.raw = MMU_DT_INVALID; 3247 b_tbl->bt_ecnt--; 3248 } 3249 } 3250 } 3251 if (nstart < nend) { 3252 idx = MMU_TIB(nstart); 3253 b_dte = &b_tbl->bt_dtbl[idx]; 3254 rstart = nstart; 3255 while (rstart < nend) { 3256 if (MMU_VALID_DT(*b_dte)) { 3257 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte)); 3258 c_tbl = mmuC2tmgr(c_dte); 3259 free_c_table(c_tbl, TRUE); 3260 b_dte->attr.raw = MMU_DT_INVALID; 3261 b_tbl->bt_ecnt--; 3262 } 3263 b_dte++; 3264 rstart += MMU_TIB_RANGE; 3265 } 3266 } 3267 if (nend < end) { 3268 idx = MMU_TIB(nend); 3269 b_dte = &b_tbl->bt_dtbl[idx]; 3270 if (MMU_VALID_DT(*b_dte)) { 3271 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte)); 3272 c_tbl = mmuC2tmgr(c_dte); 3273 empty = pmap_remove_c(c_tbl, nend, end); 3274 if (empty) { 3275 b_dte->attr.raw = MMU_DT_INVALID; 3276 b_tbl->bt_ecnt--; 3277 } 3278 } 3279 } 3280 3281 if (b_tbl->bt_ecnt == 0) { 3282 b_tbl->bt_parent = NULL; 3283 TAILQ_REMOVE(&b_pool, b_tbl, bt_link); 3284 TAILQ_INSERT_HEAD(&b_pool, b_tbl, bt_link); 3285 empty = TRUE; 3286 } else { 3287 empty = FALSE; 3288 } 3289 3290 return empty; 3291 } 3292 3293 /* pmap_remove_c INTERNAL 3294 ** 3295 * Remove a range of addresses from the given C table. 3296 */ 3297 boolean_t 3298 pmap_remove_c(c_tbl, start, end) 3299 c_tmgr_t *c_tbl; 3300 vaddr_t start; 3301 vaddr_t end; 3302 { 3303 boolean_t empty; 3304 int idx; 3305 mmu_short_pte_t *c_pte; 3306 3307 idx = MMU_TIC(start); 3308 c_pte = &c_tbl->ct_dtbl[idx]; 3309 for (;start < end; start += MMU_PAGE_SIZE, c_pte++) { 3310 if (MMU_VALID_DT(*c_pte)) { 3311 pmap_remove_pte(c_pte); 3312 c_tbl->ct_ecnt--; 3313 } 3314 } 3315 3316 if (c_tbl->ct_ecnt == 0) { 3317 c_tbl->ct_parent = NULL; 3318 TAILQ_REMOVE(&c_pool, c_tbl, ct_link); 3319 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link); 3320 empty = TRUE; 3321 } else { 3322 empty = FALSE; 3323 } 3324 3325 return empty; 3326 } 3327 3328 /* is_managed INTERNAL 3329 ** 3330 * Determine if the given physical address is managed by the PV system. 3331 * Note that this logic assumes that no one will ask for the status of 3332 * addresses which lie in-between the memory banks on the 3/80. If they 3333 * do so, it will falsely report that it is managed. 3334 * 3335 * Note: A "managed" address is one that was reported to the VM system as 3336 * a "usable page" during system startup. As such, the VM system expects the 3337 * pmap module to keep an accurate track of the useage of those pages. 3338 * Any page not given to the VM system at startup does not exist (as far as 3339 * the VM system is concerned) and is therefore "unmanaged." Examples are 3340 * those pages which belong to the ROM monitor and the memory allocated before 3341 * the VM system was started. 3342 */ 3343 boolean_t 3344 is_managed(pa) 3345 paddr_t pa; 3346 { 3347 if (pa >= avail_start && pa < avail_end) 3348 return TRUE; 3349 else 3350 return FALSE; 3351 } 3352 3353 /* pmap_bootstrap_alloc INTERNAL 3354 ** 3355 * Used internally for memory allocation at startup when malloc is not 3356 * available. This code will fail once it crosses the first memory 3357 * bank boundary on the 3/80. Hopefully by then however, the VM system 3358 * will be in charge of allocation. 3359 */ 3360 void * 3361 pmap_bootstrap_alloc(size) 3362 int size; 3363 { 3364 void *rtn; 3365 3366 #ifdef PMAP_DEBUG 3367 if (bootstrap_alloc_enabled == FALSE) { 3368 mon_printf("pmap_bootstrap_alloc: disabled\n"); 3369 sunmon_abort(); 3370 } 3371 #endif 3372 3373 rtn = (void *) virtual_avail; 3374 virtual_avail += size; 3375 3376 #ifdef PMAP_DEBUG 3377 if (virtual_avail > virtual_contig_end) { 3378 mon_printf("pmap_bootstrap_alloc: out of mem\n"); 3379 sunmon_abort(); 3380 } 3381 #endif 3382 3383 return rtn; 3384 } 3385 3386 /* pmap_bootstap_aalign INTERNAL 3387 ** 3388 * Used to insure that the next call to pmap_bootstrap_alloc() will 3389 * return a chunk of memory aligned to the specified size. 3390 * 3391 * Note: This function will only support alignment sizes that are powers 3392 * of two. 3393 */ 3394 void 3395 pmap_bootstrap_aalign(size) 3396 int size; 3397 { 3398 int off; 3399 3400 off = virtual_avail & (size - 1); 3401 if (off) { 3402 (void) pmap_bootstrap_alloc(size - off); 3403 } 3404 } 3405 3406 /* pmap_pa_exists 3407 ** 3408 * Used by the /dev/mem driver to see if a given PA is memory 3409 * that can be mapped. (The PA is not in a hole.) 3410 */ 3411 int 3412 pmap_pa_exists(pa) 3413 paddr_t pa; 3414 { 3415 int i; 3416 3417 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) { 3418 if ((pa >= avail_mem[i].pmem_start) && 3419 (pa < avail_mem[i].pmem_end)) 3420 return (1); 3421 if (avail_mem[i].pmem_next == NULL) 3422 break; 3423 } 3424 return (0); 3425 } 3426 3427 /* Called only from locore.s and pmap.c */ 3428 void _pmap_switch __P((pmap_t pmap)); 3429 3430 /* 3431 * _pmap_switch INTERNAL 3432 * 3433 * This is called by locore.s:cpu_switch() when it is 3434 * switching to a new process. Load new translations. 3435 * Note: done in-line by locore.s unless PMAP_DEBUG 3436 * 3437 * Note that we do NOT allocate a context here, but 3438 * share the "kernel only" context until we really 3439 * need our own context for user-space mappings in 3440 * pmap_enter_user(). [ s/context/mmu A table/ ] 3441 */ 3442 void 3443 _pmap_switch(pmap) 3444 pmap_t pmap; 3445 { 3446 u_long rootpa; 3447 3448 /* 3449 * Only do reload/flush if we have to. 3450 * Note that if the old and new process 3451 * were BOTH using the "null" context, 3452 * then this will NOT flush the TLB. 3453 */ 3454 rootpa = pmap->pm_a_phys; 3455 if (kernel_crp.rp_addr != rootpa) { 3456 DPRINT(("pmap_activate(%p)\n", pmap)); 3457 kernel_crp.rp_addr = rootpa; 3458 loadcrp(&kernel_crp); 3459 TBIAU(); 3460 } 3461 } 3462 3463 /* 3464 * Exported version of pmap_activate(). This is called from the 3465 * machine-independent VM code when a process is given a new pmap. 3466 * If (p == curlwp) do like cpu_switch would do; otherwise just 3467 * take this as notification that the process has a new pmap. 3468 */ 3469 void 3470 pmap_activate(l) 3471 struct lwp *l; 3472 { 3473 if (l->l_proc == curproc) { 3474 _pmap_switch(l->l_proc->p_vmspace->vm_map.pmap); 3475 } 3476 } 3477 3478 /* 3479 * pmap_deactivate INTERFACE 3480 ** 3481 * This is called to deactivate the specified process's address space. 3482 */ 3483 void 3484 pmap_deactivate(l) 3485 struct lwp *l; 3486 { 3487 /* Nothing to do. */ 3488 } 3489 3490 /* 3491 * Fill in the sun3x-specific part of the kernel core header 3492 * for dumpsys(). (See machdep.c for the rest.) 3493 */ 3494 void 3495 pmap_kcore_hdr(sh) 3496 struct sun3x_kcore_hdr *sh; 3497 { 3498 u_long spa, len; 3499 int i; 3500 3501 sh->pg_frame = MMU_SHORT_PTE_BASEADDR; 3502 sh->pg_valid = MMU_DT_PAGE; 3503 sh->contig_end = virtual_contig_end; 3504 sh->kernCbase = (u_long)kernCbase; 3505 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) { 3506 spa = avail_mem[i].pmem_start; 3507 spa = m68k_trunc_page(spa); 3508 len = avail_mem[i].pmem_end - spa; 3509 len = m68k_round_page(len); 3510 sh->ram_segs[i].start = spa; 3511 sh->ram_segs[i].size = len; 3512 } 3513 } 3514 3515 3516 /* pmap_virtual_space INTERFACE 3517 ** 3518 * Return the current available range of virtual addresses in the 3519 * arguuments provided. Only really called once. 3520 */ 3521 void 3522 pmap_virtual_space(vstart, vend) 3523 vaddr_t *vstart, *vend; 3524 { 3525 *vstart = virtual_avail; 3526 *vend = virtual_end; 3527 } 3528 3529 /* 3530 * Provide memory to the VM system. 3531 * 3532 * Assume avail_start is always in the 3533 * first segment as pmap_bootstrap does. 3534 */ 3535 static void 3536 pmap_page_upload() 3537 { 3538 paddr_t a, b; /* memory range */ 3539 int i; 3540 3541 /* Supply the memory in segments. */ 3542 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) { 3543 a = atop(avail_mem[i].pmem_start); 3544 b = atop(avail_mem[i].pmem_end); 3545 if (i == 0) 3546 a = atop(avail_start); 3547 if (avail_mem[i].pmem_end > avail_end) 3548 b = atop(avail_end); 3549 3550 uvm_page_physload(a, b, a, b, VM_FREELIST_DEFAULT); 3551 3552 if (avail_mem[i].pmem_next == NULL) 3553 break; 3554 } 3555 } 3556 3557 /* pmap_count INTERFACE 3558 ** 3559 * Return the number of resident (valid) pages in the given pmap. 3560 * 3561 * Note: If this function is handed the kernel map, it will report 3562 * that it has no mappings. Hopefully the VM system won't ask for kernel 3563 * map statistics. 3564 */ 3565 segsz_t 3566 pmap_count(pmap, type) 3567 pmap_t pmap; 3568 int type; 3569 { 3570 u_int count; 3571 int a_idx, b_idx; 3572 a_tmgr_t *a_tbl; 3573 b_tmgr_t *b_tbl; 3574 c_tmgr_t *c_tbl; 3575 3576 /* 3577 * If the pmap does not have its own A table manager, it has no 3578 * valid entires. 3579 */ 3580 if (pmap->pm_a_tmgr == NULL) 3581 return 0; 3582 3583 a_tbl = pmap->pm_a_tmgr; 3584 3585 count = 0; 3586 for (a_idx = 0; a_idx < MMU_TIA(KERNBASE); a_idx++) { 3587 if (MMU_VALID_DT(a_tbl->at_dtbl[a_idx])) { 3588 b_tbl = mmuB2tmgr(mmu_ptov(a_tbl->at_dtbl[a_idx].addr.raw)); 3589 for (b_idx = 0; b_idx < MMU_B_TBL_SIZE; b_idx++) { 3590 if (MMU_VALID_DT(b_tbl->bt_dtbl[b_idx])) { 3591 c_tbl = mmuC2tmgr( 3592 mmu_ptov(MMU_DTE_PA(b_tbl->bt_dtbl[b_idx]))); 3593 if (type == 0) 3594 /* 3595 * A resident entry count has been requested. 3596 */ 3597 count += c_tbl->ct_ecnt; 3598 else 3599 /* 3600 * A wired entry count has been requested. 3601 */ 3602 count += c_tbl->ct_wcnt; 3603 } 3604 } 3605 } 3606 } 3607 3608 return count; 3609 } 3610 3611 /************************ SUN3 COMPATIBILITY ROUTINES ******************** 3612 * The following routines are only used by DDB for tricky kernel text * 3613 * text operations in db_memrw.c. They are provided for sun3 * 3614 * compatibility. * 3615 *************************************************************************/ 3616 /* get_pte INTERNAL 3617 ** 3618 * Return the page descriptor the describes the kernel mapping 3619 * of the given virtual address. 3620 */ 3621 extern u_long ptest_addr __P((u_long)); /* XXX: locore.s */ 3622 u_int 3623 get_pte(va) 3624 vaddr_t va; 3625 { 3626 u_long pte_pa; 3627 mmu_short_pte_t *pte; 3628 3629 /* Get the physical address of the PTE */ 3630 pte_pa = ptest_addr(va & ~PGOFSET); 3631 3632 /* Convert to a virtual address... */ 3633 pte = (mmu_short_pte_t *) (KERNBASE + pte_pa); 3634 3635 /* Make sure it is in our level-C tables... */ 3636 if ((pte < kernCbase) || 3637 (pte >= &mmuCbase[NUM_USER_PTES])) 3638 return 0; 3639 3640 /* ... and just return its contents. */ 3641 return (pte->attr.raw); 3642 } 3643 3644 3645 /* set_pte INTERNAL 3646 ** 3647 * Set the page descriptor that describes the kernel mapping 3648 * of the given virtual address. 3649 */ 3650 void 3651 set_pte(va, pte) 3652 vaddr_t va; 3653 u_int pte; 3654 { 3655 u_long idx; 3656 3657 if (va < KERNBASE) 3658 return; 3659 3660 idx = (unsigned long) m68k_btop(va - KERNBASE); 3661 kernCbase[idx].attr.raw = pte; 3662 TBIS(va); 3663 } 3664 3665 /* 3666 * Routine: pmap_procwr 3667 * 3668 * Function: 3669 * Synchronize caches corresponding to [addr, addr+len) in p. 3670 */ 3671 void 3672 pmap_procwr(p, va, len) 3673 struct proc *p; 3674 vaddr_t va; 3675 size_t len; 3676 { 3677 (void)cachectl1(0x80000004, va, len, p); 3678 } 3679 3680 3681 #ifdef PMAP_DEBUG 3682 /************************** DEBUGGING ROUTINES ************************** 3683 * The following routines are meant to be an aid to debugging the pmap * 3684 * system. They are callable from the DDB command line and should be * 3685 * prepared to be handed unstable or incomplete states of the system. * 3686 ************************************************************************/ 3687 3688 /* pv_list 3689 ** 3690 * List all pages found on the pv list for the given physical page. 3691 * To avoid endless loops, the listing will stop at the end of the list 3692 * or after 'n' entries - whichever comes first. 3693 */ 3694 void 3695 pv_list(pa, n) 3696 paddr_t pa; 3697 int n; 3698 { 3699 int idx; 3700 vaddr_t va; 3701 pv_t *pv; 3702 c_tmgr_t *c_tbl; 3703 pmap_t pmap; 3704 3705 pv = pa2pv(pa); 3706 idx = pv->pv_idx; 3707 for (; idx != PVE_EOL && n > 0; idx = pvebase[idx].pve_next, n--) { 3708 va = pmap_get_pteinfo(idx, &pmap, &c_tbl); 3709 printf("idx %d, pmap 0x%x, va 0x%x, c_tbl %x\n", 3710 idx, (u_int) pmap, (u_int) va, (u_int) c_tbl); 3711 } 3712 } 3713 #endif /* PMAP_DEBUG */ 3714 3715 #ifdef NOT_YET 3716 /* and maybe not ever */ 3717 /************************** LOW-LEVEL ROUTINES ************************** 3718 * These routines will eventually be re-written into assembly and placed* 3719 * in locore.s. They are here now as stubs so that the pmap module can * 3720 * be linked as a standalone user program for testing. * 3721 ************************************************************************/ 3722 /* flush_atc_crp INTERNAL 3723 ** 3724 * Flush all page descriptors derived from the given CPU Root Pointer 3725 * (CRP), or 'A' table as it is known here, from the 68851's automatic 3726 * cache. 3727 */ 3728 void 3729 flush_atc_crp(a_tbl) 3730 { 3731 mmu_long_rp_t rp; 3732 3733 /* Create a temporary root table pointer that points to the 3734 * given A table. 3735 */ 3736 rp.attr.raw = ~MMU_LONG_RP_LU; 3737 rp.addr.raw = (unsigned int) a_tbl; 3738 3739 mmu_pflushr(&rp); 3740 /* mmu_pflushr: 3741 * movel sp(4)@,a0 3742 * pflushr a0@ 3743 * rts 3744 */ 3745 } 3746 #endif /* NOT_YET */ 3747