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