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