1 /* $NetBSD: pmap.c,v 1.131 2026/07/06 16:08:13 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.131 2026/07/06 16:08:13 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 /* XXX - What headers declare these? */ 139 extern struct pcb *curpcb; 140 141 /* Defined in locore.s */ 142 extern char kernel_text[]; 143 144 /* Defined by the linker */ 145 extern char etext[], edata[], end[]; 146 extern char *esym; /* DDB */ 147 148 /*************************** DEBUGGING DEFINITIONS *********************** 149 * Macros, preprocessor defines and variables used in debugging can make * 150 * code hard to read. Anything used exclusively for debugging purposes * 151 * is defined here to avoid having such mess scattered around the file. * 152 *************************************************************************/ 153 #ifdef PMAP_DEBUG 154 /* 155 * To aid the debugging process, macros should be expanded into smaller steps 156 * that accomplish the same goal, yet provide convenient places for placing 157 * breakpoints. When this code is compiled with PMAP_DEBUG mode defined, the 158 * 'INLINE' keyword is defined to an empty string. This way, any function 159 * defined to be a 'static INLINE' will become 'outlined' and compiled as 160 * a separate function, which is much easier to debug. 161 */ 162 #define INLINE /* nothing */ 163 164 /* 165 * It is sometimes convenient to watch the activity of a particular table 166 * in the system. The following variables are used for that purpose. 167 */ 168 a_tmgr_t *pmap_watch_atbl = 0; 169 b_tmgr_t *pmap_watch_btbl = 0; 170 c_tmgr_t *pmap_watch_ctbl = 0; 171 172 int pmap_debug = 0; 173 #define DPRINT(args) if (pmap_debug) printf args 174 175 #else /********** Stuff below is defined if NOT debugging **************/ 176 177 #define INLINE inline 178 #define DPRINT(args) /* nada */ 179 180 #endif /* PMAP_DEBUG */ 181 /*********************** END OF DEBUGGING DEFINITIONS ********************/ 182 183 /*** Management Structure - Memory Layout 184 * For every MMU table in the sun3x pmap system there must be a way to 185 * manage it; we must know which process is using it, what other tables 186 * depend on it, and whether or not it contains any locked pages. This 187 * is solved by the creation of 'table management' or 'tmgr' 188 * structures. One for each MMU table in the system. 189 * 190 * MAP OF MEMORY USED BY THE PMAP SYSTEM 191 * 192 * towards lower memory 193 * kernAbase -> +-------------------------------------------------------+ 194 * | Kernel MMU A level table | 195 * kernBbase -> +-------------------------------------------------------+ 196 * | Kernel MMU B level tables | 197 * kernCbase -> +-------------------------------------------------------+ 198 * | | 199 * | Kernel MMU C level tables | 200 * | | 201 * mmuCbase -> +-------------------------------------------------------+ 202 * | User MMU C level tables | 203 * mmuAbase -> +-------------------------------------------------------+ 204 * | | 205 * | User MMU A level tables | 206 * | | 207 * mmuBbase -> +-------------------------------------------------------+ 208 * | User MMU B level tables | 209 * tmgrAbase -> +-------------------------------------------------------+ 210 * | TMGR A level table structures | 211 * tmgrBbase -> +-------------------------------------------------------+ 212 * | TMGR B level table structures | 213 * tmgrCbase -> +-------------------------------------------------------+ 214 * | TMGR C level table structures | 215 * pvbase -> +-------------------------------------------------------+ 216 * | Physical to Virtual mapping table (list heads) | 217 * pvebase -> +-------------------------------------------------------+ 218 * | Physical to Virtual mapping table (list elements) | 219 * | | 220 * +-------------------------------------------------------+ 221 * towards higher memory 222 * 223 * For every A table in the MMU A area, there will be a corresponding 224 * a_tmgr structure in the TMGR A area. The same will be true for 225 * the B and C tables. This arrangement will make it easy to find the 226 * controlling tmgr structure for any table in the system by use of 227 * (relatively) simple macros. 228 */ 229 230 /* 231 * Global variables for storing the base addresses for the areas 232 * labeled above. 233 */ 234 static vaddr_t kernAphys; 235 static mmu_long_dte_t *kernAbase; 236 static mmu_short_dte_t *kernBbase; 237 static mmu_short_pte_t *kernCbase; 238 static mmu_short_pte_t *mmuCbase; 239 static mmu_short_dte_t *mmuBbase; 240 static mmu_long_dte_t *mmuAbase; 241 static a_tmgr_t *Atmgrbase; 242 static b_tmgr_t *Btmgrbase; 243 static c_tmgr_t *Ctmgrbase; 244 static pv_t *pvbase; 245 static pv_elem_t *pvebase; 246 static struct pmap kernel_pmap; 247 struct pmap *const kernel_pmap_ptr = &kernel_pmap; 248 249 /* 250 * Just all around global variables. 251 */ 252 static TAILQ_HEAD(a_pool_head_struct, a_tmgr_struct) a_pool; 253 static TAILQ_HEAD(b_pool_head_struct, b_tmgr_struct) b_pool; 254 static TAILQ_HEAD(c_pool_head_struct, c_tmgr_struct) c_pool; 255 256 257 /* 258 * Flags used to mark the safety/availability of certain operations or 259 * resources. 260 */ 261 /* Safe to use pmap_bootstrap_alloc(). */ 262 static bool bootstrap_alloc_enabled = false; 263 /* Temporary virtual pages are in use */ 264 int tmp_vpages_inuse; 265 266 /* 267 * XXX: For now, retain the traditional variables that were 268 * used in the old pmap/vm interface (without NONCONTIG). 269 */ 270 /* Kernel virtual address space available: */ 271 vaddr_t virtual_avail, virtual_end; 272 /* Physical address space available: */ 273 paddr_t avail_start, avail_end; 274 275 /* This keep track of the end of the contiguously mapped range. */ 276 vaddr_t virtual_contig_end; 277 278 /* These are used by pmap_copy_page(), etc. */ 279 vaddr_t tmp_vpages[2]; 280 281 /* memory pool for pmap structures */ 282 struct pool pmap_pmap_pool; 283 284 /* 285 * The 3/80 is the only member of the sun3x family that has non-contiguous 286 * physical memory. Memory is divided into 4 banks which are physically 287 * locatable on the system board. Although the size of these banks varies 288 * with the size of memory they contain, their base addresses are 289 * permanently fixed. The following structure, which describes these 290 * banks, is initialized by pmap_bootstrap() after it reads from a similar 291 * structure provided by the ROM Monitor. 292 * 293 * For the other machines in the sun3x architecture which do have contiguous 294 * RAM, this list will have only one entry, which will describe the entire 295 * range of available memory. 296 */ 297 __CTASSERT(SUN3X_NPHYS_RAM_SEGS <= VM_PHYSSEG_MAX); 298 struct pmap_physmem_struct avail_mem[SUN3X_NPHYS_RAM_SEGS]; 299 u_int total_phys_mem; 300 301 /*************************************************************************/ 302 303 /* 304 * XXX - Should "tune" these based on statistics. 305 * 306 * My first guess about the relative numbers of these needed is 307 * based on the fact that a "typical" process will have several 308 * pages mapped at low virtual addresses (text, data, bss), then 309 * some mapped shared libraries, and then some stack pages mapped 310 * near the high end of the VA space. Each process can use only 311 * one A table, and most will use only two B tables (maybe three) 312 * and probably about four C tables. Therefore, the first guess 313 * at the relative numbers of these needed is 1:2:4 -gwr 314 * 315 * The number of C tables needed is closely related to the amount 316 * of physical memory available plus a certain amount attributable 317 * to the use of double mappings. With a few simulation statistics 318 * we can find a reasonably good estimation of this unknown value. 319 * Armed with that and the above ratios, we have a good idea of what 320 * is needed at each level. -j 321 * 322 * Note: It is not physical memory memory size, but the total mapped 323 * virtual space required by the combined working sets of all the 324 * currently _runnable_ processes. (Sleeping ones don't count.) 325 * The amount of physical memory should be irrelevant. -gwr 326 */ 327 #ifdef FIXED_NTABLES 328 #define NUM_A_TABLES 16 329 #define NUM_B_TABLES 32 330 #define NUM_C_TABLES 64 331 #else 332 unsigned int NUM_A_TABLES, NUM_B_TABLES, NUM_C_TABLES; 333 #endif /* FIXED_NTABLES */ 334 335 /* 336 * This determines our total virtual mapping capacity. 337 * Yes, it is a FIXED value so we can pre-allocate. 338 */ 339 #define NUM_USER_PTES (NUM_C_TABLES * MMU_C_TBL_SIZE) 340 341 /* 342 * The size of the Kernel Virtual Address Space (KVAS) 343 * for purposes of MMU table allocation is -KERNBASE 344 * (length from KERNBASE to 0xFFFFffff) 345 */ 346 #define KVAS_SIZE (-KERNBASE3X) 347 348 /* Numbers of kernel MMU tables to support KVAS_SIZE. */ 349 #define KERN_B_TABLES (KVAS_SIZE >> MMU_TIA_SHIFT) 350 #define KERN_C_TABLES (KVAS_SIZE >> MMU_TIB_SHIFT) 351 #define NUM_KERN_PTES (KVAS_SIZE >> MMU_TIC_SHIFT) 352 353 /*************************** MISCELLANEOUS MACROS *************************/ 354 void *pmap_bootstrap_alloc(int); 355 356 static INLINE void *mmu_ptov(paddr_t); 357 static INLINE paddr_t mmu_vtop(void *); 358 359 #if 0 360 static INLINE a_tmgr_t *mmuA2tmgr(mmu_long_dte_t *); 361 #endif /* 0 */ 362 static INLINE b_tmgr_t *mmuB2tmgr(mmu_short_dte_t *); 363 static INLINE c_tmgr_t *mmuC2tmgr(mmu_short_pte_t *); 364 365 static INLINE pv_t *pa2pv(paddr_t); 366 static INLINE int pteidx(mmu_short_pte_t *); 367 static INLINE pmap_t current_pmap(void); 368 369 /* 370 * We can always convert between virtual and physical addresses 371 * for anything in the range [KERNBASE ... avail_start] because 372 * that range is GUARANTEED to be mapped linearly. 373 * We rely heavily upon this feature! 374 */ 375 static INLINE void * 376 mmu_ptov(paddr_t pa) 377 { 378 vaddr_t va; 379 380 va = (pa + KERNBASE3X); 381 #ifdef PMAP_DEBUG 382 if ((va < KERNBASE3X) || (va >= virtual_contig_end)) 383 panic("mmu_ptov"); 384 #endif 385 return (void *)va; 386 } 387 388 static INLINE paddr_t 389 mmu_vtop(void *vva) 390 { 391 vaddr_t va; 392 393 va = (vaddr_t)vva; 394 #ifdef PMAP_DEBUG 395 if ((va < KERNBASE3X) || (va >= virtual_contig_end)) 396 panic("mmu_vtop"); 397 #endif 398 return va - KERNBASE3X; 399 } 400 401 /* 402 * These macros map MMU tables to their corresponding manager structures. 403 * They are needed quite often because many of the pointers in the pmap 404 * system reference MMU tables and not the structures that control them. 405 * There needs to be a way to find one when given the other and these 406 * macros do so by taking advantage of the memory layout described above. 407 * Here's a quick step through the first macro, mmuA2tmgr(): 408 * 409 * 1) find the offset of the given MMU A table from the base of its table 410 * pool (table - mmuAbase). 411 * 2) convert this offset into a table index by dividing it by the 412 * size of one MMU 'A' table. (sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE) 413 * 3) use this index to select the corresponding 'A' table manager 414 * structure from the 'A' table manager pool (Atmgrbase[index]). 415 */ 416 /* This function is not currently used. */ 417 #if 0 418 static INLINE a_tmgr_t * 419 mmuA2tmgr(mmu_long_dte_t *mmuAtbl) 420 { 421 int idx; 422 423 /* Which table is this in? */ 424 idx = (mmuAtbl - mmuAbase) / MMU_A_TBL_SIZE; 425 #ifdef PMAP_DEBUG 426 if ((idx < 0) || (idx >= NUM_A_TABLES)) 427 panic("mmuA2tmgr"); 428 #endif 429 return &Atmgrbase[idx]; 430 } 431 #endif /* 0 */ 432 433 static INLINE b_tmgr_t * 434 mmuB2tmgr(mmu_short_dte_t *mmuBtbl) 435 { 436 int idx; 437 438 /* Which table is this in? */ 439 idx = (mmuBtbl - mmuBbase) / MMU_B_TBL_SIZE; 440 #ifdef PMAP_DEBUG 441 if ((idx < 0) || (idx >= NUM_B_TABLES)) 442 panic("mmuB2tmgr"); 443 #endif 444 return &Btmgrbase[idx]; 445 } 446 447 /* mmuC2tmgr INTERNAL 448 ** 449 * Given a pte known to belong to a C table, return the address of 450 * that table's management structure. 451 */ 452 static INLINE c_tmgr_t * 453 mmuC2tmgr(mmu_short_pte_t *mmuCtbl) 454 { 455 int idx; 456 457 /* Which table is this in? */ 458 idx = (mmuCtbl - mmuCbase) / MMU_C_TBL_SIZE; 459 #ifdef PMAP_DEBUG 460 if ((idx < 0) || (idx >= NUM_C_TABLES)) 461 panic("mmuC2tmgr"); 462 #endif 463 return &Ctmgrbase[idx]; 464 } 465 466 /* This is now a function call below. 467 * #define pa2pv(pa) \ 468 * (&pvbase[(unsigned long)\ 469 * m68k_btop(pa)\ 470 * ]) 471 */ 472 473 /* pa2pv INTERNAL 474 ** 475 * Return the pv_list_head element which manages the given physical 476 * address. 477 */ 478 static INLINE pv_t * 479 pa2pv(paddr_t pa) 480 { 481 struct pmap_physmem_struct *bank; 482 int idx; 483 484 bank = &avail_mem[0]; 485 while (pa >= bank->pmem_seg->ps_end) 486 bank = bank->pmem_next; 487 488 pa -= bank->pmem_seg->ps_start; 489 idx = bank->pmem_pvbase + m68k_btop(pa); 490 #ifdef PMAP_DEBUG 491 if ((idx < 0) || (idx >= physmem)) 492 panic("pa2pv"); 493 #endif 494 return &pvbase[idx]; 495 } 496 497 /* pteidx INTERNAL 498 ** 499 * Return the index of the given PTE within the entire fixed table of 500 * PTEs. 501 */ 502 static INLINE int 503 pteidx(mmu_short_pte_t *pte) 504 { 505 506 return pte - kernCbase; 507 } 508 509 /* 510 * This just offers a place to put some debugging checks, 511 * and reduces the number of places "curlwp" appears... 512 */ 513 static INLINE pmap_t 514 current_pmap(void) 515 { 516 struct vmspace *vm; 517 struct vm_map *map; 518 pmap_t pmap; 519 520 vm = curproc->p_vmspace; 521 map = &vm->vm_map; 522 pmap = vm_map_pmap(map); 523 524 return pmap; 525 } 526 527 528 /*************************** FUNCTION DEFINITIONS ************************ 529 * These appear here merely for the compiler to enforce type checking on * 530 * all function calls. * 531 *************************************************************************/ 532 533 /* 534 * Internal functions 535 */ 536 a_tmgr_t *get_a_table(void); 537 b_tmgr_t *get_b_table(void); 538 c_tmgr_t *get_c_table(void); 539 int free_a_table(a_tmgr_t *, bool); 540 int free_b_table(b_tmgr_t *, bool); 541 int free_c_table(c_tmgr_t *, bool); 542 543 void pmap_bootstrap_aalign(int); 544 void pmap_alloc_usermmu(void); 545 void pmap_alloc_usertmgr(void); 546 void pmap_alloc_pv(void); 547 void pmap_init_a_tables(void); 548 void pmap_init_b_tables(void); 549 void pmap_init_c_tables(void); 550 void pmap_init_pv(void); 551 void pmap_clear_pv(paddr_t, int); 552 static INLINE bool is_managed(paddr_t); 553 554 bool pmap_remove_a(a_tmgr_t *, vaddr_t, vaddr_t); 555 bool pmap_remove_b(b_tmgr_t *, vaddr_t, vaddr_t); 556 bool pmap_remove_c(c_tmgr_t *, vaddr_t, vaddr_t); 557 void pmap_remove_pte(mmu_short_pte_t *); 558 559 void pmap_enter_kernel(vaddr_t, paddr_t, vm_prot_t); 560 static INLINE void pmap_remove_kernel(vaddr_t, vaddr_t); 561 static INLINE void pmap_protect_kernel(vaddr_t, vaddr_t, vm_prot_t); 562 static INLINE bool pmap_extract_kernel(vaddr_t, paddr_t *); 563 vaddr_t pmap_get_pteinfo(u_int, pmap_t *, c_tmgr_t **); 564 static INLINE int pmap_dereference(pmap_t); 565 566 bool pmap_stroll(pmap_t, vaddr_t, a_tmgr_t **, b_tmgr_t **, c_tmgr_t **, 567 mmu_short_pte_t **, int *, int *, int *); 568 void pmap_bootstrap_copyprom(void); 569 void pmap_takeover_mmu(void); 570 void pmap_bootstrap_setprom(void); 571 572 #ifdef PMAP_DEBUG 573 /* Debugging function definitions */ 574 void pv_list(paddr_t, int); 575 #endif /* PMAP_DEBUG */ 576 577 /** Interface functions 578 ** - functions required by the Mach VM Pmap interface, with MACHINE_CONTIG 579 ** defined. 580 ** The new UVM doesn't require them so now INTERNAL. 581 **/ 582 static INLINE void pmap_pinit(pmap_t); 583 static INLINE void pmap_release(pmap_t); 584 585 /********************************** CODE ******************************** 586 * Functions that are called from other parts of the kernel are labeled * 587 * as 'INTERFACE' functions. Functions that are only called from * 588 * within the pmap module are labeled as 'INTERNAL' functions. * 589 * Functions that are internal, but are not (currently) used at all are * 590 * labeled 'INTERNAL_X'. * 591 ************************************************************************/ 592 593 /* pmap_bootstrap INTERNAL 594 ** 595 * Initializes the pmap system. Called at boot time from 596 * locore2.c:_vm_init() 597 * 598 * Reminder: having a pmap_bootstrap_alloc() and also having the VM 599 * system implement pmap_steal_memory() is redundant. 600 * Don't release this code without removing one or the other! 601 */ 602 paddr_t 603 pmap_bootstrap(vaddr_t nextva) 604 { 605 struct physmemory *membank; 606 struct pmap_physmem_struct *pmap_membank; 607 vaddr_t va, eva; 608 paddr_t pa; 609 paddr_t nextpa; 610 int b, c, i, j; /* running table counts */ 611 int size, resvmem; 612 extern void *msgbufaddr; 613 614 /* 615 * This function is called by __bootstrap after it has 616 * determined the type of machine and made the appropriate 617 * patches to the ROM vectors (XXX- I don't quite know what I meant 618 * by that.) It allocates and sets up enough of the pmap system 619 * to manage the kernel's address space. 620 */ 621 622 /* 623 * Determine the range of kernel virtual and physical 624 * space available. Note that we ABSOLUTELY DEPEND on 625 * the fact that the first bank of memory (4MB) is 626 * mapped linearly to KERNBASE (which we guaranteed in 627 * the first instructions of locore.s). 628 * That is plenty for our bootstrap work. 629 */ 630 virtual_avail = m68k_round_page(nextva); 631 virtual_contig_end = KERNBASE3X + 0x400000; /* +4MB */ 632 virtual_end = VM_MAX_KERNEL_ADDRESS; 633 /* Don't need avail_start til later. */ 634 635 /* We may now call pmap_bootstrap_alloc(). */ 636 bootstrap_alloc_enabled = true; 637 638 /* 639 * This is a somewhat unwrapped loop to deal with 640 * copying the PROM's 'phsymem' banks into the pmap's 641 * banks. The following is always assumed: 642 * 1. There is always at least one bank of memory. 643 * 2. There is always a last bank of memory, and its 644 * pmem_next member must be set to NULL. 645 */ 646 membank = romVectorPtr->v_physmemory; 647 total_phys_mem = 0; 648 649 /* Build up the pmap's copy of the membank list. */ 650 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) { 651 pmap_membank = &avail_mem[i]; 652 pmap_membank->pmem_next = &avail_mem[i + 1]; 653 pmap_membank->pmem_seg = &phys_seg_list[i]; 654 } 655 pmap_membank->pmem_next = NULL; 656 pmap_membank = avail_mem; 657 658 for (;;) { /* break on !membank */ 659 pmap_membank->pmem_seg->ps_start = membank->address; 660 pmap_membank->pmem_seg->ps_end = 661 membank->address + membank->size; 662 total_phys_mem += membank->size; 663 membank = membank->next; 664 if (!membank) 665 break; 666 pmap_membank = pmap_membank->pmem_next; 667 } 668 /* Terminate the list early if not all banks are populated. */ 669 pmap_membank->pmem_next = NULL; 670 671 /* 672 * Note: total_phys_mem, physmem represent 673 * actual physical memory, including that 674 * reserved for the PROM monitor. 675 */ 676 physmem = btoc(total_phys_mem); 677 678 /* 679 * Clamp the last bank to account for the PROM's reserved 680 * pages. 681 */ 682 resvmem = total_phys_mem - *(romVectorPtr->memoryAvail); 683 resvmem = m68k_round_page(resvmem); 684 pmap_membank->pmem_seg->ps_end -= resvmem; 685 686 /* 687 * First allocate enough kernel MMU tables to map all 688 * of kernel virtual space from KERNBASE to 0xFFFFFFFF. 689 * Note: All must be aligned on 256 byte boundaries. 690 * Start with the level-A table (one of those). 691 */ 692 size = sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE; 693 kernAbase = pmap_bootstrap_alloc(size); 694 memset(kernAbase, 0, size); 695 696 /* Now the level-B kernel tables... */ 697 size = sizeof(mmu_short_dte_t) * MMU_B_TBL_SIZE * KERN_B_TABLES; 698 kernBbase = pmap_bootstrap_alloc(size); 699 memset(kernBbase, 0, size); 700 701 /* Now the level-C kernel tables... */ 702 size = sizeof(mmu_short_pte_t) * MMU_C_TBL_SIZE * KERN_C_TABLES; 703 kernCbase = pmap_bootstrap_alloc(size); 704 memset(kernCbase, 0, size); 705 /* 706 * Note: In order for the PV system to work correctly, the kernel 707 * and user-level C tables must be allocated contiguously. 708 * Nothing should be allocated between here and the allocation of 709 * mmuCbase below. XXX: Should do this as one allocation, and 710 * then compute a pointer for mmuCbase instead of this... 711 * 712 * Allocate user MMU tables. 713 * These must be contiguous with the preceding. 714 */ 715 716 #ifndef FIXED_NTABLES 717 /* 718 * The number of user-level C tables that should be allocated is 719 * related to the size of physical memory. In general, there should 720 * be enough tables to map four times the amount of available RAM. 721 * The extra amount is needed because some table space is wasted by 722 * fragmentation. 723 */ 724 NUM_C_TABLES = (total_phys_mem * 4) / (MMU_C_TBL_SIZE * MMU_PAGE_SIZE); 725 NUM_B_TABLES = NUM_C_TABLES / 2; 726 NUM_A_TABLES = NUM_B_TABLES / 2; 727 #endif /* !FIXED_NTABLES */ 728 729 size = sizeof(mmu_short_pte_t) * MMU_C_TBL_SIZE * NUM_C_TABLES; 730 mmuCbase = pmap_bootstrap_alloc(size); 731 732 size = sizeof(mmu_short_dte_t) * MMU_B_TBL_SIZE * NUM_B_TABLES; 733 mmuBbase = pmap_bootstrap_alloc(size); 734 735 size = sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE * NUM_A_TABLES; 736 mmuAbase = pmap_bootstrap_alloc(size); 737 738 /* 739 * Fill in the never-changing part of the kernel tables. 740 * For simplicity, the kernel's mappings will be editable as a 741 * flat array of page table entries at kernCbase. The 742 * higher level 'A' and 'B' tables must be initialized to point 743 * to this lower one. 744 */ 745 b = c = 0; 746 747 /* 748 * Invalidate all mappings below KERNBASE in the A table. 749 * This area has already been zeroed out, but it is good 750 * practice to explicitly show that we are interpreting 751 * it as a list of A table descriptors. 752 */ 753 for (i = 0; i < MMU_TIA(KERNBASE3X); i++) { 754 kernAbase[i].addr.raw = 0; 755 } 756 757 /* 758 * Set up the kernel A and B tables so that they will reference the 759 * correct spots in the contiguous table of PTEs allocated for the 760 * kernel's virtual memory space. 761 */ 762 for (i = MMU_TIA(KERNBASE3X); i < MMU_A_TBL_SIZE; i++) { 763 kernAbase[i].attr.raw = 764 MMU_LONG_DTE_LU | MMU_LONG_DTE_SUPV | MMU_DT_SHORT; 765 kernAbase[i].addr.raw = mmu_vtop(&kernBbase[b]); 766 767 for (j = 0; j < MMU_B_TBL_SIZE; j++) { 768 kernBbase[b + j].attr.raw = 769 mmu_vtop(&kernCbase[c]) | MMU_DT_SHORT; 770 c += MMU_C_TBL_SIZE; 771 } 772 b += MMU_B_TBL_SIZE; 773 } 774 775 pmap_alloc_usermmu(); /* Allocate user MMU tables. */ 776 pmap_alloc_usertmgr(); /* Allocate user MMU table managers.*/ 777 pmap_alloc_pv(); /* Allocate physical->virtual map. */ 778 779 /* 780 * We are now done with pmap_bootstrap_alloc(). Round up 781 * `virtual_avail' to the nearest page, and set the flag 782 * to prevent use of pmap_bootstrap_alloc() hereafter. 783 */ 784 pmap_bootstrap_aalign(PAGE_SIZE); 785 bootstrap_alloc_enabled = false; 786 787 /* 788 * Now that we are done with pmap_bootstrap_alloc(), we 789 * must save the virtual and physical addresses of the 790 * end of the linearly mapped range, which are stored in 791 * virtual_contig_end and nextpa, respectively (nextpa is 792 * used to initialize avail_start later). These variables 793 * will never change after this point. 794 */ 795 virtual_contig_end = virtual_avail; 796 nextpa = virtual_avail - KERNBASE3X; 797 798 /* 799 * Now allocate some virtual addresses, but not the physical pages 800 * behind them. Note that virtual_avail is already page-aligned. 801 * 802 * tmp_vpages[] is an array of two virtual pages used for temporary 803 * kernel mappings in the pmap module to facilitate various physical 804 * address-oritented operations. 805 */ 806 tmp_vpages[0] = virtual_avail; 807 virtual_avail += PAGE_SIZE; 808 tmp_vpages[1] = virtual_avail; 809 virtual_avail += PAGE_SIZE; 810 811 /** Initialize the PV system **/ 812 pmap_init_pv(); 813 814 /* 815 * Fill in the kernel_pmap structure and the prototype CRP. 816 */ 817 kernAphys = mmu_vtop(kernAbase); 818 kernel_pmap.pm_a_tmgr = NULL; 819 kernel_pmap.pm_a_phys = kernAphys; 820 kernel_pmap.pm_refcount = 1; /* always in use */ 821 822 /* 823 * protorp[] is initialized in mmu_subr.s. We are not using 824 * the SRP at all; everything uses the CRP in this implementation. 825 */ 826 827 /* 828 * Now pmap_enter_kernel() may be used safely and will be 829 * the main interface used hereafter to modify the kernel's 830 * virtual address space. Note that since we are still running 831 * under the PROM's address table, none of these table modifications 832 * actually take effect until pmap_takeover_mmu() is called. 833 * 834 * Note: Our tables do NOT have the PROM linear mappings! 835 * Only the mappings created here exist in our tables, so 836 * remember to map anything we expect to use. 837 */ 838 va = (vaddr_t)KERNBASE3X; 839 pa = 0; 840 841 /* 842 * The first page of the kernel virtual address space is the msgbuf 843 * page. The page attributes (data, non-cached) are set here, while 844 * the address is assigned to this global pointer in cpu_startup(). 845 * It is non-cached, mostly due to paranoia. 846 */ 847 msgbufaddr = (void *)va; 848 pmap_enter_kernel(va, pa|PMAP_NC, VM_PROT_ALL); 849 va += PAGE_SIZE; 850 pa += PAGE_SIZE; 851 852 /* Next page is used as the temporary stack. */ 853 pmap_enter_kernel(va, pa, VM_PROT_ALL); 854 va += PAGE_SIZE; 855 pa += PAGE_SIZE; 856 857 /* 858 * Map all of the kernel's text segment as read-only and cacheable. 859 * (Cacheable is implied by default). Unfortunately, the last bytes 860 * of kernel text and the first bytes of kernel data will often be 861 * sharing the same page. Therefore, the last page of kernel text 862 * has to be mapped as read/write, to accommodate the data. 863 */ 864 eva = m68k_trunc_page((vaddr_t)etext); 865 for (; va < eva; va += PAGE_SIZE, pa += PAGE_SIZE) 866 pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_EXECUTE); 867 868 /* 869 * Map all of the kernel's data as read/write and cacheable. 870 * This includes: data, BSS, symbols, and everything in the 871 * contiguous memory used by pmap_bootstrap_alloc() 872 */ 873 for (; pa < nextpa; va += PAGE_SIZE, pa += PAGE_SIZE) 874 pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_WRITE); 875 876 /* 877 * At this point we are almost ready to take over the MMU. But first 878 * we must save the PROM's address space in our map, as we call its 879 * routines and make references to its data later in the kernel. 880 */ 881 pmap_bootstrap_copyprom(); 882 pmap_takeover_mmu(); 883 pmap_bootstrap_setprom(); 884 885 /* Notify the VM system of our page size. */ 886 uvmexp.pagesize = PAGE_SIZE; 887 uvm_md_init(); 888 889 return nextpa; 890 } 891 892 893 /* pmap_alloc_usermmu INTERNAL 894 ** 895 * Called from pmap_bootstrap() to allocate MMU tables that will 896 * eventually be used for user mappings. 897 */ 898 void 899 pmap_alloc_usermmu(void) 900 { 901 902 /* XXX: Moved into caller. */ 903 } 904 905 /* pmap_alloc_pv INTERNAL 906 ** 907 * Called from pmap_bootstrap() to allocate the physical 908 * to virtual mapping list. Each physical page of memory 909 * in the system has a corresponding element in this list. 910 */ 911 void 912 pmap_alloc_pv(void) 913 { 914 int i; 915 unsigned int total_mem; 916 917 /* 918 * Allocate a pv_head structure for every page of physical 919 * memory that will be managed by the system. Since memory on 920 * the 3/80 is non-contiguous, we cannot arrive at a total page 921 * count by subtraction of the lowest available address from the 922 * highest, but rather we have to step through each memory 923 * bank and add the number of pages in each to the total. 924 * 925 * At this time we also initialize the offset of each bank's 926 * starting pv_head within the pv_head list so that the physical 927 * memory state routines (pmap_is_referenced(), 928 * pmap_is_modified(), et al.) can quickly find corresponding 929 * pv_heads in spite of the non-contiguity. 930 */ 931 total_mem = 0; 932 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) { 933 avail_mem[i].pmem_pvbase = m68k_btop(total_mem); 934 total_mem += avail_mem[i].pmem_seg->ps_end - 935 avail_mem[i].pmem_seg->ps_start; 936 if (avail_mem[i].pmem_next == NULL) 937 break; 938 } 939 pvbase = (pv_t *)pmap_bootstrap_alloc(sizeof(pv_t) * 940 m68k_btop(total_phys_mem)); 941 } 942 943 /* pmap_alloc_usertmgr INTERNAL 944 ** 945 * Called from pmap_bootstrap() to allocate the structures which 946 * facilitate management of user MMU tables. Each user MMU table 947 * in the system has one such structure associated with it. 948 */ 949 void 950 pmap_alloc_usertmgr(void) 951 { 952 /* Allocate user MMU table managers */ 953 /* It would be a lot simpler to just make these BSS, but */ 954 /* we may want to change their size at boot time... -j */ 955 Atmgrbase = 956 (a_tmgr_t *)pmap_bootstrap_alloc(sizeof(a_tmgr_t) * NUM_A_TABLES); 957 Btmgrbase = 958 (b_tmgr_t *)pmap_bootstrap_alloc(sizeof(b_tmgr_t) * NUM_B_TABLES); 959 Ctmgrbase = 960 (c_tmgr_t *)pmap_bootstrap_alloc(sizeof(c_tmgr_t) * NUM_C_TABLES); 961 962 /* 963 * Allocate PV list elements for the physical to virtual 964 * mapping system. 965 */ 966 pvebase = (pv_elem_t *)pmap_bootstrap_alloc(sizeof(pv_elem_t) * 967 (NUM_USER_PTES + NUM_KERN_PTES)); 968 } 969 970 /* pmap_bootstrap_copyprom() INTERNAL 971 ** 972 * Copy the PROM mappings into our own tables. Note, we 973 * can use physical addresses until __bootstrap returns. 974 */ 975 void 976 pmap_bootstrap_copyprom(void) 977 { 978 struct sunromvec *romp; 979 int *mon_ctbl; 980 mmu_short_pte_t *kpte; 981 int i, len; 982 983 romp = romVectorPtr; 984 985 /* 986 * Copy the mappings in SUN3X_MON_KDB_BASE...SUN3X_MONEND 987 * Note: mon_ctbl[0] maps SUN3X_MON_KDB_BASE 988 */ 989 mon_ctbl = *romp->monptaddr; 990 i = m68k_btop(SUN3X_MON_KDB_BASE - KERNBASE3X); 991 kpte = &kernCbase[i]; 992 len = m68k_btop(SUN3X_MONEND - SUN3X_MON_KDB_BASE); 993 994 for (i = 0; i < len; i++) { 995 kpte[i].attr.raw = mon_ctbl[i]; 996 } 997 998 /* 999 * Copy the mappings at MON_DVMA_BASE (to the end). 1000 * Note, in here, mon_ctbl[0] maps MON_DVMA_BASE. 1001 * Actually, we only want the last page, which the 1002 * PROM has set up for use by the "ie" driver. 1003 * (The i82686 needs its SCP there.) 1004 * If we copy all the mappings, pmap_enter_kernel 1005 * may complain about finding valid PTEs that are 1006 * not recorded in our PV lists... 1007 */ 1008 mon_ctbl = *romp->shadowpteaddr; 1009 i = m68k_btop(SUN3X_MON_DVMA_BASE - KERNBASE3X); 1010 kpte = &kernCbase[i]; 1011 len = m68k_btop(SUN3X_MON_DVMA_SIZE); 1012 for (i = (len - 1); i < len; i++) { 1013 kpte[i].attr.raw = mon_ctbl[i]; 1014 } 1015 } 1016 1017 /* pmap_takeover_mmu INTERNAL 1018 ** 1019 * Called from pmap_bootstrap() after it has copied enough of the 1020 * PROM mappings into the kernel map so that we can use our own 1021 * MMU table. 1022 */ 1023 void 1024 pmap_takeover_mmu(void) 1025 { 1026 mmu_load_urp51(kernAphys); 1027 } 1028 1029 /* pmap_bootstrap_setprom() INTERNAL 1030 ** 1031 * Set the PROM mappings so it can see kernel space. 1032 * Note that physical addresses are used here, which 1033 * we can get away with because this runs with the 1034 * low 1GB set for transparent translation. 1035 */ 1036 void 1037 pmap_bootstrap_setprom(void) 1038 { 1039 mmu_long_dte_t *mon_dte; 1040 extern struct mmu_rootptr mon_crp; 1041 int i; 1042 1043 mon_dte = (mmu_long_dte_t *)mon_crp.rp_addr; 1044 for (i = MMU_TIA(KERNBASE3X); i < MMU_TIA(KERN_END3X); i++) { 1045 mon_dte[i].attr.raw = kernAbase[i].attr.raw; 1046 mon_dte[i].addr.raw = kernAbase[i].addr.raw; 1047 } 1048 } 1049 1050 1051 /* pmap_init INTERFACE 1052 ** 1053 * Called at the end of vm_init() to set up the pmap system to go 1054 * into full time operation. All initialization of kernel_pmap 1055 * should be already done by now, so this should just do things 1056 * needed for user-level pmaps to work. 1057 */ 1058 void 1059 pmap_init(void) 1060 { 1061 1062 /** Initialize the manager pools **/ 1063 TAILQ_INIT(&a_pool); 1064 TAILQ_INIT(&b_pool); 1065 TAILQ_INIT(&c_pool); 1066 1067 /************************************************************** 1068 * Initialize all tmgr structures and MMU tables they manage. * 1069 **************************************************************/ 1070 /** Initialize A tables **/ 1071 pmap_init_a_tables(); 1072 /** Initialize B tables **/ 1073 pmap_init_b_tables(); 1074 /** Initialize C tables **/ 1075 pmap_init_c_tables(); 1076 1077 /** Initialize the pmap pools **/ 1078 pool_init(&pmap_pmap_pool, sizeof(struct pmap), 0, 0, 0, "pmappl", 1079 &pool_allocator_nointr, IPL_NONE); 1080 } 1081 1082 /* pmap_init_a_tables() INTERNAL 1083 ** 1084 * Initializes all A managers, their MMU A tables, and inserts 1085 * them into the A manager pool for use by the system. 1086 */ 1087 void 1088 pmap_init_a_tables(void) 1089 { 1090 int i; 1091 a_tmgr_t *a_tbl; 1092 1093 for (i = 0; i < NUM_A_TABLES; i++) { 1094 /* Select the next available A manager from the pool */ 1095 a_tbl = &Atmgrbase[i]; 1096 1097 /* 1098 * Clear its parent entry. Set its wired and valid 1099 * entry count to zero. 1100 */ 1101 a_tbl->at_parent = NULL; 1102 a_tbl->at_wcnt = a_tbl->at_ecnt = 0; 1103 1104 /* Assign it the next available MMU A table from the pool */ 1105 a_tbl->at_dtbl = &mmuAbase[i * MMU_A_TBL_SIZE]; 1106 1107 /* 1108 * Initialize the MMU A table with the table in the `lwp0', 1109 * or kernel, mapping. This ensures that every process has 1110 * the kernel mapped in the top part of its address space. 1111 */ 1112 memcpy(a_tbl->at_dtbl, kernAbase, 1113 MMU_A_TBL_SIZE * sizeof(mmu_long_dte_t)); 1114 1115 /* 1116 * Finally, insert the manager into the A pool, 1117 * making it ready to be used by the system. 1118 */ 1119 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link); 1120 } 1121 } 1122 1123 /* pmap_init_b_tables() INTERNAL 1124 ** 1125 * Initializes all B table managers, their MMU B tables, and 1126 * inserts them into the B manager pool for use by the system. 1127 */ 1128 void 1129 pmap_init_b_tables(void) 1130 { 1131 int i, j; 1132 b_tmgr_t *b_tbl; 1133 1134 for (i = 0; i < NUM_B_TABLES; i++) { 1135 /* Select the next available B manager from the pool */ 1136 b_tbl = &Btmgrbase[i]; 1137 1138 b_tbl->bt_parent = NULL; /* clear its parent, */ 1139 b_tbl->bt_pidx = 0; /* parent index, */ 1140 b_tbl->bt_wcnt = 0; /* wired entry count, */ 1141 b_tbl->bt_ecnt = 0; /* valid entry count. */ 1142 1143 /* Assign it the next available MMU B table from the pool */ 1144 b_tbl->bt_dtbl = &mmuBbase[i * MMU_B_TBL_SIZE]; 1145 1146 /* Invalidate every descriptor in the table */ 1147 for (j = 0; j < MMU_B_TBL_SIZE; j++) 1148 b_tbl->bt_dtbl[j].attr.raw = MMU_DT_INVALID; 1149 1150 /* Insert the manager into the B pool */ 1151 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link); 1152 } 1153 } 1154 1155 /* pmap_init_c_tables() INTERNAL 1156 ** 1157 * Initializes all C table managers, their MMU C tables, and 1158 * inserts them into the C manager pool for use by the system. 1159 */ 1160 void 1161 pmap_init_c_tables(void) 1162 { 1163 int i, j; 1164 c_tmgr_t *c_tbl; 1165 1166 for (i = 0; i < NUM_C_TABLES; i++) { 1167 /* Select the next available C manager from the pool */ 1168 c_tbl = &Ctmgrbase[i]; 1169 1170 c_tbl->ct_parent = NULL; /* clear its parent, */ 1171 c_tbl->ct_pidx = 0; /* parent index, */ 1172 c_tbl->ct_wcnt = 0; /* wired entry count, */ 1173 c_tbl->ct_ecnt = 0; /* valid entry count, */ 1174 c_tbl->ct_pmap = NULL; /* parent pmap, */ 1175 c_tbl->ct_va = 0; /* base of managed range */ 1176 1177 /* Assign it the next available MMU C table from the pool */ 1178 c_tbl->ct_dtbl = &mmuCbase[i * MMU_C_TBL_SIZE]; 1179 1180 for (j = 0; j < MMU_C_TBL_SIZE; j++) 1181 c_tbl->ct_dtbl[j].attr.raw = MMU_DT_INVALID; 1182 1183 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link); 1184 } 1185 } 1186 1187 /* pmap_init_pv() INTERNAL 1188 ** 1189 * Initializes the Physical to Virtual mapping system. 1190 */ 1191 void 1192 pmap_init_pv(void) 1193 { 1194 int i; 1195 1196 /* Initialize every PV head. */ 1197 for (i = 0; i < m68k_btop(total_phys_mem); i++) { 1198 pvbase[i].pv_idx = PVE_EOL; /* Indicate no mappings */ 1199 pvbase[i].pv_flags = 0; /* Zero out page flags */ 1200 } 1201 } 1202 1203 /* is_managed INTERNAL 1204 ** 1205 * Determine if the given physical address is managed by the PV system. 1206 * Note that this logic assumes that no one will ask for the status of 1207 * addresses which lie in-between the memory banks on the 3/80. If they 1208 * do so, it will falsely report that it is managed. 1209 * 1210 * Note: A "managed" address is one that was reported to the VM system as 1211 * a "usable page" during system startup. As such, the VM system expects the 1212 * pmap module to keep an accurate track of the usage of those pages. 1213 * Any page not given to the VM system at startup does not exist (as far as 1214 * the VM system is concerned) and is therefore "unmanaged." Examples are 1215 * those pages which belong to the ROM monitor and the memory allocated before 1216 * the VM system was started. 1217 */ 1218 static INLINE bool 1219 is_managed(paddr_t pa) 1220 { 1221 if (pa >= avail_start && pa < avail_end) 1222 return true; 1223 else 1224 return false; 1225 } 1226 1227 /* get_a_table INTERNAL 1228 ** 1229 * Retrieve and return a level A table for use in a user map. 1230 */ 1231 a_tmgr_t * 1232 get_a_table(void) 1233 { 1234 a_tmgr_t *tbl; 1235 pmap_t pmap; 1236 1237 /* Get the top A table in the pool */ 1238 tbl = TAILQ_FIRST(&a_pool); 1239 if (tbl == NULL) { 1240 /* 1241 * XXX - Instead of panicking here and in other get_x_table 1242 * functions, we do have the option of sleeping on the head of 1243 * the table pool. Any function which updates the table pool 1244 * would then issue a wakeup() on the head, thus waking up any 1245 * processes waiting for a table. 1246 * 1247 * Actually, the place to sleep would be when some process 1248 * asks for a "wired" mapping that would run us short of 1249 * mapping resources. This design DEPENDS on always having 1250 * some mapping resources in the pool for stealing, so we 1251 * must make sure we NEVER let the pool become empty. -gwr 1252 */ 1253 panic("get_a_table: out of A tables."); 1254 } 1255 1256 TAILQ_REMOVE(&a_pool, tbl, at_link); 1257 /* 1258 * If the table has a non-null parent pointer then it is in use. 1259 * Forcibly abduct it from its parent and clear its entries. 1260 * No re-entrancy worries here. This table would not be in the 1261 * table pool unless it was available for use. 1262 * 1263 * Note that the second argument to free_a_table() is false. This 1264 * indicates that the table should not be relinked into the A table 1265 * pool. That is a job for the function that called us. 1266 */ 1267 if (tbl->at_parent) { 1268 KASSERT(tbl->at_wcnt == 0); 1269 pmap = tbl->at_parent; 1270 free_a_table(tbl, false); 1271 pmap->pm_a_tmgr = NULL; 1272 pmap->pm_a_phys = kernAphys; 1273 } 1274 return tbl; 1275 } 1276 1277 /* get_b_table INTERNAL 1278 ** 1279 * Return a level B table for use. 1280 */ 1281 b_tmgr_t * 1282 get_b_table(void) 1283 { 1284 b_tmgr_t *tbl; 1285 1286 /* See 'get_a_table' for comments. */ 1287 tbl = TAILQ_FIRST(&b_pool); 1288 if (tbl == NULL) 1289 panic("get_b_table: out of B tables."); 1290 TAILQ_REMOVE(&b_pool, tbl, bt_link); 1291 if (tbl->bt_parent) { 1292 KASSERT(tbl->bt_wcnt == 0); 1293 tbl->bt_parent->at_dtbl[tbl->bt_pidx].attr.raw = MMU_DT_INVALID; 1294 tbl->bt_parent->at_ecnt--; 1295 free_b_table(tbl, false); 1296 } 1297 return tbl; 1298 } 1299 1300 /* get_c_table INTERNAL 1301 ** 1302 * Return a level C table for use. 1303 */ 1304 c_tmgr_t * 1305 get_c_table(void) 1306 { 1307 c_tmgr_t *tbl; 1308 1309 /* See 'get_a_table' for comments */ 1310 tbl = TAILQ_FIRST(&c_pool); 1311 if (tbl == NULL) 1312 panic("get_c_table: out of C tables."); 1313 TAILQ_REMOVE(&c_pool, tbl, ct_link); 1314 if (tbl->ct_parent) { 1315 KASSERT(tbl->ct_wcnt == 0); 1316 tbl->ct_parent->bt_dtbl[tbl->ct_pidx].attr.raw = MMU_DT_INVALID; 1317 tbl->ct_parent->bt_ecnt--; 1318 free_c_table(tbl, false); 1319 } 1320 return tbl; 1321 } 1322 1323 /* 1324 * The following 'free_table' and 'steal_table' functions are called to 1325 * detach tables from their current obligations (parents and children) and 1326 * prepare them for reuse in another mapping. 1327 * 1328 * Free_table is used when the calling function will handle the fate 1329 * of the parent table, such as returning it to the free pool when it has 1330 * no valid entries. Functions that do not want to handle this should 1331 * call steal_table, in which the parent table's descriptors and entry 1332 * count are automatically modified when this table is removed. 1333 */ 1334 1335 /* free_a_table INTERNAL 1336 ** 1337 * Unmaps the given A table and all child tables from their current 1338 * mappings. Returns the number of pages that were invalidated. 1339 * If 'relink' is true, the function will return the table to the head 1340 * of the available table pool. 1341 * 1342 * Cache note: The MC68851 will automatically flush all 1343 * descriptors derived from a given A table from its 1344 * Automatic Translation Cache (ATC) if we issue a 1345 * 'PFLUSHR' instruction with the base address of the 1346 * table. This function should do, and does so. 1347 * Note note: We are using an MC68030 - there is no 1348 * PFLUSHR. 1349 */ 1350 int 1351 free_a_table(a_tmgr_t *a_tbl, bool relink) 1352 { 1353 int i, removed_cnt; 1354 mmu_long_dte_t *dte; 1355 mmu_short_dte_t *dtbl; 1356 b_tmgr_t *b_tbl; 1357 uint8_t at_wired, bt_wired; 1358 1359 /* 1360 * Flush the ATC cache of all cached descriptors derived 1361 * from this table. 1362 * Sun3x does not use 68851's cached table feature 1363 * flush_atc_crp(mmu_vtop(a_tbl->dte)); 1364 */ 1365 1366 /* 1367 * Remove any pending cache flushes that were designated 1368 * for the pmap this A table belongs to. 1369 * a_tbl->parent->atc_flushq[0] = 0; 1370 * Not implemented in sun3x. 1371 */ 1372 1373 /* 1374 * All A tables in the system should retain a map for the 1375 * kernel. If the table contains any valid descriptors 1376 * (other than those for the kernel area), invalidate them all, 1377 * stopping short of the kernel's entries. 1378 */ 1379 removed_cnt = 0; 1380 at_wired = a_tbl->at_wcnt; 1381 if (a_tbl->at_ecnt) { 1382 dte = a_tbl->at_dtbl; 1383 for (i = 0; i < MMU_TIA(KERNBASE3X); i++) { 1384 /* 1385 * If a table entry points to a valid B table, free 1386 * it and its children. 1387 */ 1388 if (MMU_VALID_DT(dte[i])) { 1389 /* 1390 * The following block does several things, 1391 * from innermost expression to the 1392 * outermost: 1393 * 1) It extracts the base (cc 1996) 1394 * address of the B table pointed 1395 * to in the A table entry dte[i]. 1396 * 2) It converts this base address into 1397 * the virtual address it can be 1398 * accessed with. (all MMU tables point 1399 * to physical addresses.) 1400 * 3) It finds the corresponding manager 1401 * structure which manages this MMU table. 1402 * 4) It frees the manager structure. 1403 * (This frees the MMU table and all 1404 * child tables. See 'free_b_table' for 1405 * details.) 1406 */ 1407 dtbl = mmu_ptov(dte[i].addr.raw); 1408 b_tbl = mmuB2tmgr(dtbl); 1409 bt_wired = b_tbl->bt_wcnt; 1410 removed_cnt += free_b_table(b_tbl, true); 1411 if (bt_wired) 1412 a_tbl->at_wcnt--; 1413 dte[i].attr.raw = MMU_DT_INVALID; 1414 } 1415 } 1416 a_tbl->at_ecnt = 0; 1417 } 1418 KASSERT(a_tbl->at_wcnt == 0); 1419 1420 if (relink) { 1421 a_tbl->at_parent = NULL; 1422 if (!at_wired) 1423 TAILQ_REMOVE(&a_pool, a_tbl, at_link); 1424 TAILQ_INSERT_HEAD(&a_pool, a_tbl, at_link); 1425 } 1426 return removed_cnt; 1427 } 1428 1429 /* free_b_table INTERNAL 1430 ** 1431 * Unmaps the given B table and all its children from their current 1432 * mappings. Returns the number of pages that were invalidated. 1433 * (For comments, see 'free_a_table()'). 1434 */ 1435 int 1436 free_b_table(b_tmgr_t *b_tbl, bool relink) 1437 { 1438 int i, removed_cnt; 1439 mmu_short_dte_t *dte; 1440 mmu_short_pte_t *dtbl; 1441 c_tmgr_t *c_tbl; 1442 uint8_t bt_wired, ct_wired; 1443 1444 removed_cnt = 0; 1445 bt_wired = b_tbl->bt_wcnt; 1446 if (b_tbl->bt_ecnt) { 1447 dte = b_tbl->bt_dtbl; 1448 for (i = 0; i < MMU_B_TBL_SIZE; i++) { 1449 if (MMU_VALID_DT(dte[i])) { 1450 dtbl = mmu_ptov(MMU_DTE_PA(dte[i])); 1451 c_tbl = mmuC2tmgr(dtbl); 1452 ct_wired = c_tbl->ct_wcnt; 1453 removed_cnt += free_c_table(c_tbl, true); 1454 if (ct_wired) 1455 b_tbl->bt_wcnt--; 1456 dte[i].attr.raw = MMU_DT_INVALID; 1457 } 1458 } 1459 b_tbl->bt_ecnt = 0; 1460 } 1461 KASSERT(b_tbl->bt_wcnt == 0); 1462 1463 if (relink) { 1464 b_tbl->bt_parent = NULL; 1465 if (!bt_wired) 1466 TAILQ_REMOVE(&b_pool, b_tbl, bt_link); 1467 TAILQ_INSERT_HEAD(&b_pool, b_tbl, bt_link); 1468 } 1469 return removed_cnt; 1470 } 1471 1472 /* free_c_table INTERNAL 1473 ** 1474 * Unmaps the given C table from use and returns it to the pool for 1475 * re-use. Returns the number of pages that were invalidated. 1476 * 1477 * This function preserves any physical page modification information 1478 * contained in the page descriptors within the C table by calling 1479 * 'pmap_remove_pte().' 1480 */ 1481 int 1482 free_c_table(c_tmgr_t *c_tbl, bool relink) 1483 { 1484 mmu_short_pte_t *c_pte; 1485 int i, removed_cnt; 1486 uint8_t ct_wired; 1487 1488 removed_cnt = 0; 1489 ct_wired = c_tbl->ct_wcnt; 1490 if (c_tbl->ct_ecnt) { 1491 for (i = 0; i < MMU_C_TBL_SIZE; i++) { 1492 c_pte = &c_tbl->ct_dtbl[i]; 1493 if (MMU_VALID_DT(*c_pte)) { 1494 if (c_pte->attr.raw & MMU_SHORT_PTE_WIRED) 1495 c_tbl->ct_wcnt--; 1496 pmap_remove_pte(c_pte); 1497 removed_cnt++; 1498 } 1499 } 1500 c_tbl->ct_ecnt = 0; 1501 } 1502 KASSERT(c_tbl->ct_wcnt == 0); 1503 1504 if (relink) { 1505 c_tbl->ct_parent = NULL; 1506 if (!ct_wired) 1507 TAILQ_REMOVE(&c_pool, c_tbl, ct_link); 1508 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link); 1509 } 1510 return removed_cnt; 1511 } 1512 1513 1514 /* pmap_remove_pte INTERNAL 1515 ** 1516 * Unmap the given pte and preserve any page modification 1517 * information by transferring it to the pv head of the 1518 * physical page it maps to. This function does not update 1519 * any reference counts because it is assumed that the calling 1520 * function will do so. 1521 */ 1522 void 1523 pmap_remove_pte(mmu_short_pte_t *pte) 1524 { 1525 u_short pv_idx, targ_idx; 1526 paddr_t pa; 1527 pv_t *pv; 1528 1529 pa = MMU_PTE_PA(*pte); 1530 if (is_managed(pa)) { 1531 pv = pa2pv(pa); 1532 targ_idx = pteidx(pte); /* Index of PTE being removed */ 1533 1534 /* 1535 * If the PTE being removed is the first (or only) PTE in 1536 * the list of PTEs currently mapped to this page, remove the 1537 * PTE by changing the index found on the PV head. Otherwise 1538 * a linear search through the list will have to be executed 1539 * in order to find the PVE which points to the PTE being 1540 * removed, so that it may be modified to point to its new 1541 * neighbor. 1542 */ 1543 1544 pv_idx = pv->pv_idx; /* Index of first PTE in PV list */ 1545 if (pv_idx == targ_idx) { 1546 pv->pv_idx = pvebase[targ_idx].pve_next; 1547 } else { 1548 1549 /* 1550 * Find the PV element pointing to the target 1551 * element. Note: may have pv_idx==PVE_EOL 1552 */ 1553 1554 for (;;) { 1555 if (pv_idx == PVE_EOL) { 1556 goto pv_not_found; 1557 } 1558 if (pvebase[pv_idx].pve_next == targ_idx) 1559 break; 1560 pv_idx = pvebase[pv_idx].pve_next; 1561 } 1562 1563 /* 1564 * At this point, pv_idx is the index of the PV 1565 * element just before the target element in the list. 1566 * Unlink the target. 1567 */ 1568 1569 pvebase[pv_idx].pve_next = pvebase[targ_idx].pve_next; 1570 } 1571 1572 /* 1573 * Save the mod/ref bits of the pte by simply 1574 * ORing the entire pte onto the pv_flags member 1575 * of the pv structure. 1576 * There is no need to use a separate bit pattern 1577 * for usage information on the pv head than that 1578 * which is used on the MMU ptes. 1579 */ 1580 1581 pv_not_found: 1582 pv->pv_flags |= (u_short) pte->attr.raw; 1583 } 1584 pte->attr.raw = MMU_DT_INVALID; 1585 } 1586 1587 /* pmap_stroll INTERNAL 1588 ** 1589 * Retrieve the addresses of all table managers involved in the mapping of 1590 * the given virtual address. If the table walk completed successfully, 1591 * return true. If it was only partially successful, return false. 1592 * The table walk performed by this function is important to many other 1593 * functions in this module. 1594 * 1595 * Note: This function ought to be easier to read. 1596 */ 1597 bool 1598 pmap_stroll(pmap_t pmap, vaddr_t va, a_tmgr_t **a_tbl, b_tmgr_t **b_tbl, 1599 c_tmgr_t **c_tbl, mmu_short_pte_t **pte, int *a_idx, int *b_idx, 1600 int *pte_idx) 1601 { 1602 mmu_long_dte_t *a_dte; /* A: long descriptor table */ 1603 mmu_short_dte_t *b_dte; /* B: short descriptor table */ 1604 1605 if (pmap == pmap_kernel()) 1606 return false; 1607 1608 /* Does the given pmap have its own A table? */ 1609 *a_tbl = pmap->pm_a_tmgr; 1610 if (*a_tbl == NULL) 1611 return false; /* No. Return unknown. */ 1612 /* Does the A table have a valid B table 1613 * under the corresponding table entry? 1614 */ 1615 *a_idx = MMU_TIA(va); 1616 a_dte = &((*a_tbl)->at_dtbl[*a_idx]); 1617 if (!MMU_VALID_DT(*a_dte)) 1618 return false; /* No. Return unknown. */ 1619 /* Yes. Extract B table from the A table. */ 1620 *b_tbl = mmuB2tmgr(mmu_ptov(a_dte->addr.raw)); 1621 /* 1622 * Does the B table have a valid C table 1623 * under the corresponding table entry? 1624 */ 1625 *b_idx = MMU_TIB(va); 1626 b_dte = &((*b_tbl)->bt_dtbl[*b_idx]); 1627 if (!MMU_VALID_DT(*b_dte)) 1628 return false; /* No. Return unknown. */ 1629 /* Yes. Extract C table from the B table. */ 1630 *c_tbl = mmuC2tmgr(mmu_ptov(MMU_DTE_PA(*b_dte))); 1631 *pte_idx = MMU_TIC(va); 1632 *pte = &((*c_tbl)->ct_dtbl[*pte_idx]); 1633 1634 return true; 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 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 srcva = tmp_vpages[0]; 2456 dstva = tmp_vpages[1]; 2457 2458 s = splvm(); 2459 #ifdef DIAGNOSTIC 2460 if (tmp_vpages_inuse++) 2461 panic("pmap_copy_page: temporary vpages are in use."); 2462 #endif 2463 2464 /* Map pages as non-cacheable to avoid cache polution? */ 2465 pmap_kenter_pa(srcva, srcpa, VM_PROT_READ, 0); 2466 pmap_kenter_pa(dstva, dstpa, VM_PROT_READ | VM_PROT_WRITE, 0); 2467 2468 /* Hand-optimized version of memcpy(dst, src, PAGE_SIZE) */ 2469 copypage((char *)srcva, (char *)dstva); 2470 2471 pmap_kremove(srcva, PAGE_SIZE); 2472 pmap_kremove(dstva, PAGE_SIZE); 2473 2474 #ifdef DIAGNOSTIC 2475 --tmp_vpages_inuse; 2476 #endif 2477 splx(s); 2478 } 2479 2480 /* pmap_zero_page INTERFACE 2481 ** 2482 * Zero the contents of the specified physical page. 2483 * 2484 * Uses one of the virtual pages allocated in pmap_bootstrap() 2485 * to map the specified page into the kernel address space. 2486 */ 2487 void 2488 pmap_zero_page(paddr_t dstpa) 2489 { 2490 vaddr_t dstva; 2491 int s; 2492 2493 dstva = tmp_vpages[1]; 2494 s = splvm(); 2495 #ifdef DIAGNOSTIC 2496 if (tmp_vpages_inuse++) 2497 panic("pmap_zero_page: temporary vpages are in use."); 2498 #endif 2499 2500 /* The comments in pmap_copy_page() above apply here also. */ 2501 pmap_kenter_pa(dstva, dstpa, VM_PROT_READ | VM_PROT_WRITE, 0); 2502 2503 /* Hand-optimized version of memset(ptr, 0, PAGE_SIZE) */ 2504 zeropage((char *)dstva); 2505 2506 pmap_kremove(dstva, PAGE_SIZE); 2507 #ifdef DIAGNOSTIC 2508 --tmp_vpages_inuse; 2509 #endif 2510 splx(s); 2511 } 2512 2513 /* pmap_pinit INTERNAL 2514 ** 2515 * Initialize a pmap structure. 2516 */ 2517 static INLINE void 2518 pmap_pinit(pmap_t pmap) 2519 { 2520 2521 memset(pmap, 0, sizeof(struct pmap)); 2522 pmap->pm_a_tmgr = NULL; 2523 pmap->pm_a_phys = kernAphys; 2524 pmap->pm_refcount = 1; 2525 } 2526 2527 /* pmap_create INTERFACE 2528 ** 2529 * Create and return a pmap structure. 2530 */ 2531 pmap_t 2532 pmap_create(void) 2533 { 2534 pmap_t pmap; 2535 2536 pmap = pool_get(&pmap_pmap_pool, PR_WAITOK); 2537 pmap_pinit(pmap); 2538 return pmap; 2539 } 2540 2541 /* pmap_release INTERNAL 2542 ** 2543 * Release any resources held by the given pmap. 2544 * 2545 * This is the reverse analog to pmap_pinit. It does not 2546 * necessarily mean for the pmap structure to be deallocated, 2547 * as in pmap_destroy. 2548 */ 2549 static INLINE void 2550 pmap_release(pmap_t pmap) 2551 { 2552 2553 /* 2554 * As long as the pmap contains no mappings, 2555 * which always should be the case whenever 2556 * this function is called, there really should 2557 * be nothing to do. 2558 */ 2559 #ifdef PMAP_DEBUG 2560 if (pmap == pmap_kernel()) 2561 panic("pmap_release: kernel pmap"); 2562 #endif 2563 /* 2564 * XXX - If this pmap has an A table, give it back. 2565 * The pmap SHOULD be empty by now, and pmap_remove 2566 * should have already given back the A table... 2567 * However, I see: pmap->pm_a_tmgr->at_ecnt == 1 2568 * at this point, which means some mapping was not 2569 * removed when it should have been. -gwr 2570 */ 2571 if (pmap->pm_a_tmgr != NULL) { 2572 /* First make sure we are not using it! */ 2573 if (protorp[1] == pmap->pm_a_phys) { 2574 mmu_load_urp51(kernAphys); 2575 } 2576 #ifdef PMAP_DEBUG /* XXX - todo! */ 2577 /* XXX - Now complain... */ 2578 printf("pmap_release: still have table\n"); 2579 Debugger(); 2580 #endif 2581 free_a_table(pmap->pm_a_tmgr, true); 2582 pmap->pm_a_tmgr = NULL; 2583 pmap->pm_a_phys = kernAphys; 2584 } 2585 } 2586 2587 /* pmap_reference INTERFACE 2588 ** 2589 * Increment the reference count of a pmap. 2590 */ 2591 void 2592 pmap_reference(pmap_t pmap) 2593 { 2594 2595 atomic_inc_uint(&pmap->pm_refcount); 2596 } 2597 2598 /* pmap_dereference INTERNAL 2599 ** 2600 * Decrease the reference count on the given pmap 2601 * by one and return the current count. 2602 */ 2603 static INLINE int 2604 pmap_dereference(pmap_t pmap) 2605 { 2606 int rtn; 2607 2608 rtn = atomic_dec_uint_nv(&pmap->pm_refcount); 2609 2610 return rtn; 2611 } 2612 2613 /* pmap_destroy INTERFACE 2614 ** 2615 * Decrement a pmap's reference count and delete 2616 * the pmap if it becomes zero. Will be called 2617 * only after all mappings have been removed. 2618 */ 2619 void 2620 pmap_destroy(pmap_t pmap) 2621 { 2622 2623 if (pmap_dereference(pmap) == 0) { 2624 pmap_release(pmap); 2625 pool_put(&pmap_pmap_pool, pmap); 2626 } 2627 } 2628 2629 /* pmap_is_referenced INTERFACE 2630 ** 2631 * Determine if the given physical page has been 2632 * referenced (read from [or written to.]) 2633 */ 2634 bool 2635 pmap_is_referenced(struct vm_page *pg) 2636 { 2637 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2638 pv_t *pv; 2639 int idx; 2640 2641 /* 2642 * Check the flags on the pv head. If they are set, 2643 * return immediately. Otherwise a search must be done. 2644 */ 2645 2646 pv = pa2pv(pa); 2647 if (pv->pv_flags & PV_FLAGS_USED) 2648 return true; 2649 2650 /* 2651 * Search through all pv elements pointing 2652 * to this page and query their reference bits 2653 */ 2654 2655 for (idx = pv->pv_idx; idx != PVE_EOL; idx = pvebase[idx].pve_next) { 2656 if (MMU_PTE_USED(kernCbase[idx])) { 2657 return true; 2658 } 2659 } 2660 return false; 2661 } 2662 2663 /* pmap_is_modified INTERFACE 2664 ** 2665 * Determine if the given physical page has been 2666 * modified (written to.) 2667 */ 2668 bool 2669 pmap_is_modified(struct vm_page *pg) 2670 { 2671 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2672 pv_t *pv; 2673 int idx; 2674 2675 /* see comments in pmap_is_referenced() */ 2676 pv = pa2pv(pa); 2677 if (pv->pv_flags & PV_FLAGS_MDFY) 2678 return true; 2679 2680 for (idx = pv->pv_idx; 2681 idx != PVE_EOL; 2682 idx = pvebase[idx].pve_next) { 2683 2684 if (MMU_PTE_MODIFIED(kernCbase[idx])) { 2685 return true; 2686 } 2687 } 2688 2689 return false; 2690 } 2691 2692 /* pmap_page_protect INTERFACE 2693 ** 2694 * Applies the given protection to all mappings to the given 2695 * physical page. 2696 */ 2697 void 2698 pmap_page_protect(struct vm_page *pg, vm_prot_t prot) 2699 { 2700 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2701 pv_t *pv; 2702 int idx; 2703 vaddr_t va; 2704 struct mmu_short_pte_struct *pte; 2705 c_tmgr_t *c_tbl; 2706 pmap_t pmap, curpmap; 2707 2708 curpmap = current_pmap(); 2709 pv = pa2pv(pa); 2710 2711 for (idx = pv->pv_idx; idx != PVE_EOL; idx = pvebase[idx].pve_next) { 2712 pte = &kernCbase[idx]; 2713 switch (prot) { 2714 case VM_PROT_ALL: 2715 /* do nothing */ 2716 break; 2717 case VM_PROT_EXECUTE: 2718 case VM_PROT_READ: 2719 case VM_PROT_READ|VM_PROT_EXECUTE: 2720 /* 2721 * Determine the virtual address mapped by 2722 * the PTE and flush ATC entries if necessary. 2723 */ 2724 va = pmap_get_pteinfo(idx, &pmap, &c_tbl); 2725 pte->attr.raw |= MMU_SHORT_PTE_WP; 2726 if (pmap == curpmap || pmap == pmap_kernel()) 2727 TBIS(va); 2728 break; 2729 case VM_PROT_NONE: 2730 /* Save the mod/ref bits. */ 2731 pv->pv_flags |= pte->attr.raw; 2732 /* Invalidate the PTE. */ 2733 pte->attr.raw = MMU_DT_INVALID; 2734 2735 /* 2736 * Update table counts. And flush ATC entries 2737 * if necessary. 2738 */ 2739 va = pmap_get_pteinfo(idx, &pmap, &c_tbl); 2740 2741 /* 2742 * If the PTE belongs to the kernel map, 2743 * be sure to flush the page it maps. 2744 */ 2745 if (pmap == pmap_kernel()) { 2746 TBIS(va); 2747 } else { 2748 /* 2749 * The PTE belongs to a user map. 2750 * update the entry count in the C 2751 * table to which it belongs and flush 2752 * the ATC if the mapping belongs to 2753 * the current pmap. 2754 */ 2755 c_tbl->ct_ecnt--; 2756 if (pmap == curpmap) 2757 TBIS(va); 2758 } 2759 break; 2760 default: 2761 break; 2762 } 2763 } 2764 2765 /* 2766 * If the protection code indicates that all mappings to the page 2767 * be removed, truncate the PV list to zero entries. 2768 */ 2769 if (prot == VM_PROT_NONE) 2770 pv->pv_idx = PVE_EOL; 2771 } 2772 2773 /* pmap_get_pteinfo INTERNAL 2774 ** 2775 * Called internally to find the pmap and virtual address within that 2776 * map to which the pte at the given index maps. Also includes the PTE's C 2777 * table manager. 2778 * 2779 * Returns the pmap in the argument provided, and the virtual address 2780 * by return value. 2781 */ 2782 vaddr_t 2783 pmap_get_pteinfo(u_int idx, pmap_t *pmap, c_tmgr_t **tbl) 2784 { 2785 vaddr_t va = 0; 2786 2787 /* 2788 * Determine if the PTE is a kernel PTE or a user PTE. 2789 */ 2790 if (idx >= NUM_KERN_PTES) { 2791 /* 2792 * The PTE belongs to a user mapping. 2793 */ 2794 /* XXX: Would like an inline for this to validate idx... */ 2795 *tbl = &Ctmgrbase[(idx - NUM_KERN_PTES) / MMU_C_TBL_SIZE]; 2796 2797 *pmap = (*tbl)->ct_pmap; 2798 /* 2799 * To find the va to which the PTE maps, we first take 2800 * the table's base virtual address mapping which is stored 2801 * in ct_va. We then increment this address by a page for 2802 * every slot skipped until we reach the PTE. 2803 */ 2804 va = (*tbl)->ct_va; 2805 va += m68k_ptob(idx % MMU_C_TBL_SIZE); 2806 } else { 2807 /* 2808 * The PTE belongs to the kernel map. 2809 */ 2810 *pmap = pmap_kernel(); 2811 2812 va = m68k_ptob(idx); 2813 va += KERNBASE3X; 2814 } 2815 2816 return va; 2817 } 2818 2819 /* pmap_clear_modify INTERFACE 2820 ** 2821 * Clear the modification bit on the page at the specified 2822 * physical address. 2823 * 2824 */ 2825 bool 2826 pmap_clear_modify(struct vm_page *pg) 2827 { 2828 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2829 bool rv; 2830 2831 rv = pmap_is_modified(pg); 2832 pmap_clear_pv(pa, PV_FLAGS_MDFY); 2833 return rv; 2834 } 2835 2836 /* pmap_clear_reference INTERFACE 2837 ** 2838 * Clear the referenced bit on the page at the specified 2839 * physical address. 2840 */ 2841 bool 2842 pmap_clear_reference(struct vm_page *pg) 2843 { 2844 paddr_t pa = VM_PAGE_TO_PHYS(pg); 2845 bool rv; 2846 2847 rv = pmap_is_referenced(pg); 2848 pmap_clear_pv(pa, PV_FLAGS_USED); 2849 return rv; 2850 } 2851 2852 /* pmap_clear_pv INTERNAL 2853 ** 2854 * Clears the specified flag from the specified physical address. 2855 * (Used by pmap_clear_modify() and pmap_clear_reference().) 2856 * 2857 * Flag is one of: 2858 * PV_FLAGS_MDFY - Page modified bit. 2859 * PV_FLAGS_USED - Page used (referenced) bit. 2860 * 2861 * This routine must not only clear the flag on the pv list 2862 * head. It must also clear the bit on every pte in the pv 2863 * list associated with the address. 2864 */ 2865 void 2866 pmap_clear_pv(paddr_t pa, int flag) 2867 { 2868 pv_t *pv; 2869 int idx; 2870 vaddr_t va; 2871 pmap_t pmap; 2872 mmu_short_pte_t *pte; 2873 c_tmgr_t *c_tbl; 2874 2875 pv = pa2pv(pa); 2876 pv->pv_flags &= ~(flag); 2877 for (idx = pv->pv_idx; idx != PVE_EOL; idx = pvebase[idx].pve_next) { 2878 pte = &kernCbase[idx]; 2879 pte->attr.raw &= ~(flag); 2880 2881 /* 2882 * The MC68030 MMU will not set the modified or 2883 * referenced bits on any MMU tables for which it has 2884 * a cached descriptor with its modify bit set. To insure 2885 * that it will modify these bits on the PTE during the next 2886 * time it is written to or read from, we must flush it from 2887 * the ATC. 2888 * 2889 * Ordinarily it is only necessary to flush the descriptor 2890 * if it is used in the current address space. But since I 2891 * am not sure that there will always be a notion of 2892 * 'the current address space' when this function is called, 2893 * I will skip the test and always flush the address. It 2894 * does no harm. 2895 */ 2896 2897 va = pmap_get_pteinfo(idx, &pmap, &c_tbl); 2898 TBIS(va); 2899 } 2900 } 2901 2902 /* pmap_extract_kernel INTERNAL 2903 ** 2904 * Extract a translation from the kernel address space. 2905 */ 2906 static INLINE bool 2907 pmap_extract_kernel(vaddr_t va, paddr_t *pap) 2908 { 2909 mmu_short_pte_t *pte; 2910 2911 pte = &kernCbase[(u_int)m68k_btop(va - KERNBASE3X)]; 2912 if (!MMU_VALID_DT(*pte)) 2913 return false; 2914 if (pap != NULL) 2915 *pap = MMU_PTE_PA(*pte); 2916 return true; 2917 } 2918 2919 /* pmap_extract INTERFACE 2920 ** 2921 * Return the physical address mapped by the virtual address 2922 * in the specified pmap. 2923 * 2924 * Note: this function should also apply an exclusive lock 2925 * on the pmap system during its duration. 2926 */ 2927 bool 2928 pmap_extract(pmap_t pmap, vaddr_t va, paddr_t *pap) 2929 { 2930 int a_idx, b_idx, pte_idx; 2931 a_tmgr_t *a_tbl; 2932 b_tmgr_t *b_tbl; 2933 c_tmgr_t *c_tbl; 2934 mmu_short_pte_t *c_pte; 2935 2936 if (pmap == pmap_kernel()) 2937 return pmap_extract_kernel(va, pap); 2938 2939 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl, 2940 &c_pte, &a_idx, &b_idx, &pte_idx) == false) 2941 return false; 2942 2943 if (!MMU_VALID_DT(*c_pte)) 2944 return false; 2945 2946 if (pap != NULL) 2947 *pap = MMU_PTE_PA(*c_pte); 2948 return true; 2949 } 2950 2951 /* pmap_phys_address 2952 ** 2953 * Return the physical address corresponding to the specified 2954 * cookie. Used by the device pager to decode a device driver's 2955 * mmap entry point return value. 2956 */ 2957 paddr_t 2958 pmap_phys_address(paddr_t addr) 2959 { 2960 return addr; 2961 } 2962 2963 /* pmap_remove_kernel INTERNAL 2964 ** 2965 * Remove the mapping of a range of virtual addresses from the kernel map. 2966 * The arguments are already page-aligned. 2967 */ 2968 static INLINE void 2969 pmap_remove_kernel(vaddr_t sva, vaddr_t eva) 2970 { 2971 int idx, eidx; 2972 2973 #ifdef PMAP_DEBUG 2974 if ((sva & PGOFSET) || (eva & PGOFSET)) 2975 panic("pmap_remove_kernel: alignment"); 2976 #endif 2977 2978 idx = m68k_btop(sva - KERNBASE3X); 2979 eidx = m68k_btop(eva - KERNBASE3X); 2980 2981 while (idx < eidx) { 2982 pmap_remove_pte(&kernCbase[idx++]); 2983 TBIS(sva); 2984 sva += PAGE_SIZE; 2985 } 2986 } 2987 2988 /* pmap_remove INTERFACE 2989 ** 2990 * Remove the mapping of a range of virtual addresses from the given pmap. 2991 * 2992 */ 2993 void 2994 pmap_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva) 2995 { 2996 2997 if (pmap == pmap_kernel()) { 2998 pmap_remove_kernel(sva, eva); 2999 return; 3000 } 3001 3002 /* 3003 * If the pmap doesn't have an A table of its own, it has no mappings 3004 * that can be removed. 3005 */ 3006 if (pmap->pm_a_tmgr == NULL) 3007 return; 3008 3009 /* 3010 * Remove the specified range from the pmap. If the function 3011 * returns true, the operation removed all the valid mappings 3012 * in the pmap and freed its A table. If this happened to the 3013 * currently loaded pmap, the MMU root pointer must be reloaded 3014 * with the default 'kernel' map. 3015 */ 3016 if (pmap_remove_a(pmap->pm_a_tmgr, sva, eva)) { 3017 if (protorp[1] == pmap->pm_a_phys) { 3018 mmu_load_urp51(kernAphys); 3019 /* will do TLB flush below */ 3020 } 3021 pmap->pm_a_tmgr = NULL; 3022 pmap->pm_a_phys = kernAphys; 3023 } 3024 3025 /* 3026 * If we just modified the current address space, 3027 * make sure to flush the MMU cache. 3028 * 3029 * XXX - this could be an unnecessarily large flush. 3030 * XXX - Could decide, based on the size of the VA range 3031 * to be removed, whether to flush "by pages" or "all". 3032 */ 3033 if (pmap == current_pmap()) 3034 TBIAU(); 3035 } 3036 3037 /* pmap_remove_a INTERNAL 3038 ** 3039 * This is function number one in a set of three that removes a range 3040 * of memory in the most efficient manner by removing the highest possible 3041 * tables from the memory space. This particular function attempts to remove 3042 * as many B tables as it can, delegating the remaining fragmented ranges to 3043 * pmap_remove_b(). 3044 * 3045 * If the removal operation results in an empty A table, the function returns 3046 * true. 3047 * 3048 * It's ugly but will do for now. 3049 */ 3050 bool 3051 pmap_remove_a(a_tmgr_t *a_tbl, vaddr_t sva, vaddr_t eva) 3052 { 3053 bool empty; 3054 int idx; 3055 vaddr_t nstart, nend; 3056 b_tmgr_t *b_tbl; 3057 mmu_long_dte_t *a_dte; 3058 mmu_short_dte_t *b_dte; 3059 uint8_t at_wired, bt_wired; 3060 3061 /* 3062 * The following code works with what I call a 'granularity 3063 * reduction algorithm'. A range of addresses will always have 3064 * the following properties, which are classified according to 3065 * how the range relates to the size of the current granularity 3066 * - an A table entry: 3067 * 3068 * 1 2 3 4 3069 * -+---+---+---+---+---+---+---+- 3070 * -+---+---+---+---+---+---+---+- 3071 * 3072 * A range will always start on a granularity boundary, illustrated 3073 * by '+' signs in the table above, or it will start at some point 3074 * in-between a granularity boundary, as illustrated by point 1. 3075 * The first step in removing a range of addresses is to remove the 3076 * range between 1 and 2, the nearest granularity boundary. This 3077 * job is handled by the section of code governed by the 3078 * 'if (start < nstart)' statement. 3079 * 3080 * A range will always encompass zero or more integral granules, 3081 * illustrated by points 2 and 3. Integral granules are easy to 3082 * remove. The removal of these granules is the second step, and 3083 * is handled by the code block 'if (nstart < nend)'. 3084 * 3085 * Lastly, a range will always end on a granularity boundary, 3086 * ill. by point 3, or it will fall just beyond one, ill. by point 3087 * 4. The last step involves removing this range and is handled by 3088 * the code block 'if (nend < end)'. 3089 */ 3090 nstart = MMU_ROUND_UP_A(sva); 3091 nend = MMU_ROUND_A(eva); 3092 3093 at_wired = a_tbl->at_wcnt; 3094 3095 if (sva < nstart) { 3096 /* 3097 * This block is executed if the range starts between 3098 * a granularity boundary. 3099 * 3100 * First find the DTE which is responsible for mapping 3101 * the start of the range. 3102 */ 3103 idx = MMU_TIA(sva); 3104 a_dte = &a_tbl->at_dtbl[idx]; 3105 3106 /* 3107 * If the DTE is valid then delegate the removal of the sub 3108 * range to pmap_remove_b(), which can remove addresses at 3109 * a finer granularity. 3110 */ 3111 if (MMU_VALID_DT(*a_dte)) { 3112 b_dte = mmu_ptov(a_dte->addr.raw); 3113 b_tbl = mmuB2tmgr(b_dte); 3114 bt_wired = b_tbl->bt_wcnt; 3115 3116 /* 3117 * The sub range to be removed starts at the start 3118 * of the full range we were asked to remove, and ends 3119 * at the greater of: 3120 * 1. The end of the full range, -or- 3121 * 2. The end of the full range, rounded down to the 3122 * nearest granularity boundary. 3123 */ 3124 if (eva < nstart) 3125 empty = pmap_remove_b(b_tbl, sva, eva); 3126 else 3127 empty = pmap_remove_b(b_tbl, sva, nstart); 3128 3129 /* 3130 * If the child table no longer has wired entries, 3131 * decrement wired entry count. 3132 */ 3133 if (bt_wired && b_tbl->bt_wcnt == 0) 3134 a_tbl->at_wcnt--; 3135 3136 /* 3137 * If the removal resulted in an empty B table, 3138 * invalidate the DTE that points to it and decrement 3139 * the valid entry count of the A table. 3140 */ 3141 if (empty) { 3142 a_dte->attr.raw = MMU_DT_INVALID; 3143 a_tbl->at_ecnt--; 3144 } 3145 } 3146 /* 3147 * If the DTE is invalid, the address range is already non- 3148 * existent and can simply be skipped. 3149 */ 3150 } 3151 if (nstart < nend) { 3152 /* 3153 * This block is executed if the range spans a whole number 3154 * multiple of granules (A table entries.) 3155 * 3156 * First find the DTE which is responsible for mapping 3157 * the start of the first granule involved. 3158 */ 3159 idx = MMU_TIA(nstart); 3160 a_dte = &a_tbl->at_dtbl[idx]; 3161 3162 /* 3163 * Remove entire sub-granules (B tables) one at a time, 3164 * until reaching the end of the range. 3165 */ 3166 for (; nstart < nend; a_dte++, nstart += MMU_TIA_RANGE) 3167 if (MMU_VALID_DT(*a_dte)) { 3168 /* 3169 * Find the B table manager for the 3170 * entry and free it. 3171 */ 3172 b_dte = mmu_ptov(a_dte->addr.raw); 3173 b_tbl = mmuB2tmgr(b_dte); 3174 bt_wired = b_tbl->bt_wcnt; 3175 3176 free_b_table(b_tbl, true); 3177 3178 /* 3179 * All child entries has been removed. 3180 * If there were any wired entries in it, 3181 * decrement wired entry count. 3182 */ 3183 if (bt_wired) 3184 a_tbl->at_wcnt--; 3185 3186 /* 3187 * Invalidate the DTE that points to the 3188 * B table and decrement the valid entry 3189 * count of the A table. 3190 */ 3191 a_dte->attr.raw = MMU_DT_INVALID; 3192 a_tbl->at_ecnt--; 3193 } 3194 } 3195 if (nend < eva) { 3196 /* 3197 * This block is executed if the range ends beyond a 3198 * granularity boundary. 3199 * 3200 * First find the DTE which is responsible for mapping 3201 * the start of the nearest (rounded down) granularity 3202 * boundary. 3203 */ 3204 idx = MMU_TIA(nend); 3205 a_dte = &a_tbl->at_dtbl[idx]; 3206 3207 /* 3208 * If the DTE is valid then delegate the removal of the sub 3209 * range to pmap_remove_b(), which can remove addresses at 3210 * a finer granularity. 3211 */ 3212 if (MMU_VALID_DT(*a_dte)) { 3213 /* 3214 * Find the B table manager for the entry 3215 * and hand it to pmap_remove_b() along with 3216 * the sub range. 3217 */ 3218 b_dte = mmu_ptov(a_dte->addr.raw); 3219 b_tbl = mmuB2tmgr(b_dte); 3220 bt_wired = b_tbl->bt_wcnt; 3221 3222 empty = pmap_remove_b(b_tbl, nend, eva); 3223 3224 /* 3225 * If the child table no longer has wired entries, 3226 * decrement wired entry count. 3227 */ 3228 if (bt_wired && b_tbl->bt_wcnt == 0) 3229 a_tbl->at_wcnt--; 3230 /* 3231 * If the removal resulted in an empty B table, 3232 * invalidate the DTE that points to it and decrement 3233 * the valid entry count of the A table. 3234 */ 3235 if (empty) { 3236 a_dte->attr.raw = MMU_DT_INVALID; 3237 a_tbl->at_ecnt--; 3238 } 3239 } 3240 } 3241 3242 /* 3243 * If there are no more entries in the A table, release it 3244 * back to the available pool and return true. 3245 */ 3246 if (a_tbl->at_ecnt == 0) { 3247 KASSERT(a_tbl->at_wcnt == 0); 3248 a_tbl->at_parent = NULL; 3249 if (!at_wired) 3250 TAILQ_REMOVE(&a_pool, a_tbl, at_link); 3251 TAILQ_INSERT_HEAD(&a_pool, a_tbl, at_link); 3252 empty = true; 3253 } else { 3254 /* 3255 * If the table doesn't have wired entries any longer 3256 * but still has unwired entries, put it back into 3257 * the available queue. 3258 */ 3259 if (at_wired && a_tbl->at_wcnt == 0) 3260 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link); 3261 empty = false; 3262 } 3263 3264 return empty; 3265 } 3266 3267 /* pmap_remove_b INTERNAL 3268 ** 3269 * Remove a range of addresses from an address space, trying to remove entire 3270 * C tables if possible. 3271 * 3272 * If the operation results in an empty B table, the function returns true. 3273 */ 3274 bool 3275 pmap_remove_b(b_tmgr_t *b_tbl, vaddr_t sva, vaddr_t eva) 3276 { 3277 bool empty; 3278 int idx; 3279 vaddr_t nstart, nend, rstart; 3280 c_tmgr_t *c_tbl; 3281 mmu_short_dte_t *b_dte; 3282 mmu_short_pte_t *c_dte; 3283 uint8_t bt_wired, ct_wired; 3284 3285 nstart = MMU_ROUND_UP_B(sva); 3286 nend = MMU_ROUND_B(eva); 3287 3288 bt_wired = b_tbl->bt_wcnt; 3289 3290 if (sva < nstart) { 3291 idx = MMU_TIB(sva); 3292 b_dte = &b_tbl->bt_dtbl[idx]; 3293 if (MMU_VALID_DT(*b_dte)) { 3294 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte)); 3295 c_tbl = mmuC2tmgr(c_dte); 3296 ct_wired = c_tbl->ct_wcnt; 3297 3298 if (eva < nstart) 3299 empty = pmap_remove_c(c_tbl, sva, eva); 3300 else 3301 empty = pmap_remove_c(c_tbl, sva, nstart); 3302 3303 /* 3304 * If the child table no longer has wired entries, 3305 * decrement wired entry count. 3306 */ 3307 if (ct_wired && c_tbl->ct_wcnt == 0) 3308 b_tbl->bt_wcnt--; 3309 3310 if (empty) { 3311 b_dte->attr.raw = MMU_DT_INVALID; 3312 b_tbl->bt_ecnt--; 3313 } 3314 } 3315 } 3316 if (nstart < nend) { 3317 idx = MMU_TIB(nstart); 3318 b_dte = &b_tbl->bt_dtbl[idx]; 3319 rstart = nstart; 3320 while (rstart < nend) { 3321 if (MMU_VALID_DT(*b_dte)) { 3322 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte)); 3323 c_tbl = mmuC2tmgr(c_dte); 3324 ct_wired = c_tbl->ct_wcnt; 3325 3326 free_c_table(c_tbl, true); 3327 3328 /* 3329 * All child entries has been removed. 3330 * If there were any wired entries in it, 3331 * decrement wired entry count. 3332 */ 3333 if (ct_wired) 3334 b_tbl->bt_wcnt--; 3335 3336 b_dte->attr.raw = MMU_DT_INVALID; 3337 b_tbl->bt_ecnt--; 3338 } 3339 b_dte++; 3340 rstart += MMU_TIB_RANGE; 3341 } 3342 } 3343 if (nend < eva) { 3344 idx = MMU_TIB(nend); 3345 b_dte = &b_tbl->bt_dtbl[idx]; 3346 if (MMU_VALID_DT(*b_dte)) { 3347 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte)); 3348 c_tbl = mmuC2tmgr(c_dte); 3349 ct_wired = c_tbl->ct_wcnt; 3350 empty = pmap_remove_c(c_tbl, nend, eva); 3351 3352 /* 3353 * If the child table no longer has wired entries, 3354 * decrement wired entry count. 3355 */ 3356 if (ct_wired && c_tbl->ct_wcnt == 0) 3357 b_tbl->bt_wcnt--; 3358 3359 if (empty) { 3360 b_dte->attr.raw = MMU_DT_INVALID; 3361 b_tbl->bt_ecnt--; 3362 } 3363 } 3364 } 3365 3366 if (b_tbl->bt_ecnt == 0) { 3367 KASSERT(b_tbl->bt_wcnt == 0); 3368 b_tbl->bt_parent = NULL; 3369 if (!bt_wired) 3370 TAILQ_REMOVE(&b_pool, b_tbl, bt_link); 3371 TAILQ_INSERT_HEAD(&b_pool, b_tbl, bt_link); 3372 empty = true; 3373 } else { 3374 /* 3375 * If the table doesn't have wired entries any longer 3376 * but still has unwired entries, put it back into 3377 * the available queue. 3378 */ 3379 if (bt_wired && b_tbl->bt_wcnt == 0) 3380 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link); 3381 3382 empty = false; 3383 } 3384 3385 return empty; 3386 } 3387 3388 /* pmap_remove_c INTERNAL 3389 ** 3390 * Remove a range of addresses from the given C table. 3391 */ 3392 bool 3393 pmap_remove_c(c_tmgr_t *c_tbl, vaddr_t sva, vaddr_t eva) 3394 { 3395 bool empty; 3396 int idx; 3397 mmu_short_pte_t *c_pte; 3398 uint8_t ct_wired; 3399 3400 ct_wired = c_tbl->ct_wcnt; 3401 3402 idx = MMU_TIC(sva); 3403 c_pte = &c_tbl->ct_dtbl[idx]; 3404 for (; sva < eva; sva += MMU_PAGE_SIZE, c_pte++) { 3405 if (MMU_VALID_DT(*c_pte)) { 3406 if (c_pte->attr.raw & MMU_SHORT_PTE_WIRED) 3407 c_tbl->ct_wcnt--; 3408 pmap_remove_pte(c_pte); 3409 c_tbl->ct_ecnt--; 3410 } 3411 } 3412 3413 if (c_tbl->ct_ecnt == 0) { 3414 KASSERT(c_tbl->ct_wcnt == 0); 3415 c_tbl->ct_parent = NULL; 3416 if (!ct_wired) 3417 TAILQ_REMOVE(&c_pool, c_tbl, ct_link); 3418 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link); 3419 empty = true; 3420 } else { 3421 /* 3422 * If the table doesn't have wired entries any longer 3423 * but still has unwired entries, put it back into 3424 * the available queue. 3425 */ 3426 if (ct_wired && c_tbl->ct_wcnt == 0) 3427 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link); 3428 empty = false; 3429 } 3430 3431 return empty; 3432 } 3433 3434 /* pmap_bootstrap_alloc INTERNAL 3435 ** 3436 * Used internally for memory allocation at startup when malloc is not 3437 * available. This code will fail once it crosses the first memory 3438 * bank boundary on the 3/80. Hopefully by then however, the VM system 3439 * will be in charge of allocation. 3440 */ 3441 void * 3442 pmap_bootstrap_alloc(int size) 3443 { 3444 void *rtn; 3445 3446 #ifdef PMAP_DEBUG 3447 if (bootstrap_alloc_enabled == false) { 3448 mon_printf("pmap_bootstrap_alloc: disabled\n"); 3449 sunmon_abort(); 3450 } 3451 #endif 3452 3453 rtn = (void *) virtual_avail; 3454 virtual_avail += size; 3455 3456 #ifdef PMAP_DEBUG 3457 if (virtual_avail > virtual_contig_end) { 3458 mon_printf("pmap_bootstrap_alloc: out of mem\n"); 3459 sunmon_abort(); 3460 } 3461 #endif 3462 3463 return rtn; 3464 } 3465 3466 /* pmap_bootstrap_aalign INTERNAL 3467 ** 3468 * Used to insure that the next call to pmap_bootstrap_alloc() will 3469 * return a chunk of memory aligned to the specified size. 3470 * 3471 * Note: This function will only support alignment sizes that are powers 3472 * of two. 3473 */ 3474 void 3475 pmap_bootstrap_aalign(int size) 3476 { 3477 int off; 3478 3479 off = virtual_avail & (size - 1); 3480 if (off) { 3481 (void)pmap_bootstrap_alloc(size - off); 3482 } 3483 } 3484 3485 /* Called only from locore.s and pmap.c */ 3486 void _pmap_switch(pmap_t pmap); 3487 3488 /* 3489 * _pmap_switch INTERNAL 3490 * 3491 * This is called by locore.s:cpu_switch() when it is 3492 * switching to a new process. Load new translations. 3493 * Note: done in-line by locore.s unless PMAP_DEBUG 3494 * 3495 * Note that we do NOT allocate a context here, but 3496 * share the "kernel only" context until we really 3497 * need our own context for user-space mappings in 3498 * pmap_enter_user(). [ s/context/mmu A table/ ] 3499 */ 3500 void 3501 _pmap_switch(pmap_t pmap) 3502 { 3503 u_long rootpa; 3504 3505 /* 3506 * Only do reload/flush if we have to. 3507 * Note that if the old and new process 3508 * were BOTH using the "null" context, 3509 * then this will NOT flush the TLB. 3510 */ 3511 rootpa = pmap->pm_a_phys; 3512 if (protorp[1] != rootpa) { 3513 DPRINT(("pmap_activate(%p)\n", pmap)); 3514 mmu_load_urp51(rootpa); 3515 TBIAU(); 3516 } 3517 } 3518 3519 /* 3520 * Exported version of pmap_activate(). This is called from the 3521 * machine-independent VM code when a process is given a new pmap. 3522 * If (p == curlwp) do like cpu_switch would do; otherwise just 3523 * take this as notification that the process has a new pmap. 3524 */ 3525 void 3526 pmap_activate(struct lwp *l) 3527 { 3528 3529 if (l->l_proc == curproc) { 3530 _pmap_switch(l->l_proc->p_vmspace->vm_map.pmap); 3531 } 3532 } 3533 3534 /* 3535 * pmap_deactivate INTERFACE 3536 ** 3537 * This is called to deactivate the specified process's address space. 3538 */ 3539 void 3540 pmap_deactivate(struct lwp *l) 3541 { 3542 3543 /* Nothing to do. */ 3544 } 3545 3546 /* 3547 * Fill in the sun3x-specific part of the kernel core header 3548 * for dumpsys(). (See machdep.c for the rest.) 3549 */ 3550 void 3551 pmap_kcore_hdr(struct sun3x_kcore_hdr *sh) 3552 { 3553 u_long spa, len; 3554 int i; 3555 3556 sh->pg_frame = MMU_SHORT_PTE_BASEADDR; 3557 sh->pg_valid = MMU_DT_PAGE; 3558 sh->contig_end = virtual_contig_end; 3559 sh->kernCbase = (u_long)kernCbase; 3560 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) { 3561 spa = avail_mem[i].pmem_seg->ps_start; 3562 spa = m68k_trunc_page(spa); 3563 len = avail_mem[i].pmem_seg->ps_end - spa; 3564 len = m68k_round_page(len); 3565 sh->ram_segs[i].start = spa; 3566 sh->ram_segs[i].size = len; 3567 } 3568 } 3569 3570 3571 /* pmap_virtual_space INTERFACE 3572 ** 3573 * Return the current available range of virtual addresses in the 3574 * arguments provided. Only really called once. 3575 */ 3576 void 3577 pmap_virtual_space(vaddr_t *vstart, vaddr_t *vend) 3578 { 3579 3580 *vstart = virtual_avail; 3581 *vend = virtual_end; 3582 } 3583 3584 /* pmap_count INTERFACE 3585 ** 3586 * Return the number of resident (valid) pages in the given pmap. 3587 * 3588 * Note: If this function is handed the kernel map, it will report 3589 * that it has no mappings. Hopefully the VM system won't ask for kernel 3590 * map statistics. 3591 */ 3592 segsz_t 3593 pmap_count(pmap_t pmap, int type) 3594 { 3595 u_int count; 3596 int a_idx, b_idx; 3597 a_tmgr_t *a_tbl; 3598 b_tmgr_t *b_tbl; 3599 c_tmgr_t *c_tbl; 3600 3601 /* 3602 * If the pmap does not have its own A table manager, it has no 3603 * valid entries. 3604 */ 3605 if (pmap->pm_a_tmgr == NULL) 3606 return 0; 3607 3608 a_tbl = pmap->pm_a_tmgr; 3609 3610 count = 0; 3611 for (a_idx = 0; a_idx < MMU_TIA(KERNBASE3X); a_idx++) { 3612 if (MMU_VALID_DT(a_tbl->at_dtbl[a_idx])) { 3613 b_tbl = mmuB2tmgr(mmu_ptov(a_tbl->at_dtbl[a_idx].addr.raw)); 3614 for (b_idx = 0; b_idx < MMU_B_TBL_SIZE; b_idx++) { 3615 if (MMU_VALID_DT(b_tbl->bt_dtbl[b_idx])) { 3616 c_tbl = mmuC2tmgr( 3617 mmu_ptov(MMU_DTE_PA(b_tbl->bt_dtbl[b_idx]))); 3618 if (type == 0) 3619 /* 3620 * A resident entry count has been requested. 3621 */ 3622 count += c_tbl->ct_ecnt; 3623 else 3624 /* 3625 * A wired entry count has been requested. 3626 */ 3627 count += c_tbl->ct_wcnt; 3628 } 3629 } 3630 } 3631 } 3632 3633 return count; 3634 } 3635 3636 /************************ SUN3 COMPATIBILITY ROUTINES ******************** 3637 * The following routines are only used by DDB for tricky kernel text * 3638 * text operations in db_memrw.c. They are provided for sun3 * 3639 * compatibility. * 3640 *************************************************************************/ 3641 /* get_pte INTERNAL 3642 ** 3643 * Return the page descriptor the describes the kernel mapping 3644 * of the given virtual address. 3645 */ 3646 extern u_long ptest_addr(u_long); /* XXX: locore.s */ 3647 u_int 3648 get_pte(vaddr_t va) 3649 { 3650 u_long pte_pa; 3651 mmu_short_pte_t *pte; 3652 3653 /* Get the physical address of the PTE */ 3654 pte_pa = ptest_addr(va & ~PGOFSET); 3655 3656 /* Convert to a virtual address... */ 3657 pte = (mmu_short_pte_t *) (KERNBASE3X + pte_pa); 3658 3659 /* Make sure it is in our level-C tables... */ 3660 if ((pte < kernCbase) || 3661 (pte >= &mmuCbase[NUM_USER_PTES])) 3662 return 0; 3663 3664 /* ... and just return its contents. */ 3665 return (pte->attr.raw); 3666 } 3667 3668 3669 /* set_pte INTERNAL 3670 ** 3671 * Set the page descriptor that describes the kernel mapping 3672 * of the given virtual address. 3673 */ 3674 void 3675 set_pte(vaddr_t va, u_int pte) 3676 { 3677 u_long idx; 3678 3679 if (va < KERNBASE3X) 3680 return; 3681 3682 idx = (unsigned long) m68k_btop(va - KERNBASE3X); 3683 kernCbase[idx].attr.raw = pte; 3684 TBIS(va); 3685 } 3686 3687 /* 3688 * Routine: pmap_procwr 3689 * 3690 * Function: 3691 * Synchronize caches corresponding to [addr, addr+len) in p. 3692 */ 3693 void 3694 pmap_procwr(struct proc *p, vaddr_t va, size_t len) 3695 { 3696 3697 (void)cachectl1(0x80000004, va, len, p); 3698 } 3699 3700 #if defined(DDB) || defined(KGDB) 3701 /* 3702 * Routine: pmap_db_write_text_enter 3703 * 3704 * Function: 3705 * Temporarily map a page of kernel text read-write for the 3706 * kernel debugger. 3707 */ 3708 bool 3709 pmap_db_write_text_enter(vaddr_t pgva, struct pmap_db_write_text_context *ctx) 3710 { 3711 int oldpte, tmppte; 3712 3713 #ifdef HAVECACHE 3714 if (cache_size) { 3715 cache_flush_page(pgva); 3716 } 3717 #endif 3718 3719 oldpte = get_pte(pgva); 3720 if ((oldpte & PG_VALID) == 0) { 3721 return false; 3722 } 3723 3724 /* Make the pte writable and non-cached. */ 3725 tmppte = oldpte; 3726 tmppte &= ~MMU_SHORT_PTE_WP; 3727 tmppte |= MMU_SHORT_PTE_CI; 3728 set_pte(pgva, tmppte); 3729 3730 ctx->pgva = pgva; 3731 ctx->opte = oldpte; 3732 3733 return true; 3734 } 3735 3736 /* 3737 * Routine: pmap_db_write_text_exit 3738 * 3739 * Function: 3740 * Undo the effects of pmap_db_write_text_enter(). 3741 */ 3742 void 3743 pmap_db_write_text_exit(struct pmap_db_write_text_context *ctx) 3744 { 3745 set_pte(ctx->pgva, ctx->opte); 3746 } 3747 #endif /* DDB || KGDB */ 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