1 /* $NetBSD: pmap_68k.c,v 1.117 2026/11/25 15:28:25 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2025, 2026 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 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 * Pmap module for the Motorola 68851 / 68030 / 68040 / 68060 MMUs. 34 * (...and HP 68851-like MMU.) 35 * 36 * This implementation supports both 2-level and 3-level page table 37 * layouts. The 3-level is mandated by 68040 / 68060, and the 2-level 38 * is mandated by the HP MMU. The 68851 and 68030 can do either, and 39 * for now, the 2-level arrangement is retained for those MMUs, although 40 * eventually we will switch them to the 3-level configuration. 41 * 42 * To support both configurations, page tables are abstracted away from 43 * the page table pages that contain them. The interface pmap operations 44 * operate on "leaf" (page) tables, and only when one of those tables needs 45 * to be allocated or freed, do the differences between the two configurations 46 * need to be dealt with. All of the tables are kept in a red-black tree 47 * that's indexed by their "segment" number (where "segment" is defined as 48 * "the amount of space mapped by a single leaf table"). This avoids having 49 * to burn large amounts of kernel address space to access tables which are 50 * expected to be sparsely-populated. 51 * 52 * In order to reduce the number of tree lookups, the most recently used 53 * leaf table is cached, and the interface contract is such that bulk 54 * operations are allowed to access subsequent PTEs within a given table 55 * (segment) without having to perform another PTE lookup. 56 * 57 * This illustrates the initial table layout for a simple program 58 * (/usr/bin/yes) using the standard m68k address space layout (based 59 * on the historical 4.3BSD-on-hp300 layout, which was itself based on 60 * HP-UX in order to facilitate HP-UX binary compatibility back when 61 * that was considered to be important). This example uses a 4K page 62 * size. 63 * 64 * TEXTADDR is $0000.2000 (not always strictly true, but close enough) 65 * USRSTACK is $FFF0.0000 (grows down, first used page VA is $FFEF.F000) 66 * 67 * (TEXTADDR is $0000.2000 because the linker uses 8K page size for 68 * broader compatibility and keeps the 0-page unmapped so that NULL 69 * pointer dereferences blow up.) 70 * 71 * This is to say: the text / data / heap of this program are in the 72 * bottom 1MB of the address space, and the stack is in the second-from- 73 * the-top 1MB of the address space. 74 * 75 * In the 2-level layout, the level-1 table is 4KB in size, and has 1024 76 * entries. Those 1024 entries together represent the 4GB user address 77 * space, and each entry thus maps a 4MB "segment" by itself pointing to 78 * a level-2 table which themselves are 4KB in size and have 1024 entries 79 * (4MB / 1024 -> 4KB, which is the page size ... convenient!) So, when 80 * our very simple program is loaded, we have a table structure that looks 81 * like this: 82 * 83 * (4KB) 84 * +----------------------+ 85 * | Level-1 | 86 * |0 1023| 87 * +----------------------+ 88 * | | 89 * | | 90 * +---------+ +---------+ 91 * | | 92 * v v 93 * (4KB) (4KB) 94 * +----------------------+ +----------------------+ 95 * | Level-2 | | Level-2 | 96 * | 2 4 | | 767 | 97 * +----------------------+ +----------------------+ 98 * | | | 99 * | +-+ | 100 * v v v 101 * TEXT DATA/bss/heap stack 102 * 103 * As you can see, this requires 3 tables (1 level-1 and 2 level-2). Each 104 * table consumes a full 4KB page, so mapping this address space requires 105 * 3 total pages. 106 * 107 * In the 3-level layout, the level-1 and level-2 tables each contain 128 108 * entries, making them 512 bytes in size. When using 4KB pages, the level-3 109 * tables contain 64 entries, making them 256 bytes in size. 110 * 111 * So, assuming the same address space layout, the 3-level structure looks 112 * like this: 113 * 114 * (512B) 115 * +--------------+ 116 * | Level-1 | 117 * |0 127| 118 * +--------------+ 119 * | | 120 * +---+ +---+ 121 * v v 122 * (512B) (512B) 123 * +--------------+ +--------------+ 124 * | Level-2 | | Level-2 | 125 * |0 | | 123 | 126 * +--------------+ +--------------+ 127 * | | 128 * +---------+ +-----+ 129 * v v 130 * (256B) (256B) 131 * +------------+ +------------+ 132 * | Level-3 | | Level-3 | 133 * | 2 4 | | 63| 134 * +------------+ +------------+ 135 * | | | 136 * | +-+ | 137 * v v v 138 * TEXT DATA/bss/heap stack 139 * 140 * The table allocator has two pools of memory for tables in the 3-level 141 * configuration: one for "segment" tables (always 512 bytes) and one for 142 * "page" or "leaf" tables (256 bytes in size for 4K pages). Pages are 143 * allocated to the pools one at a time, and then the tables are allocated 144 * from the pages. Because of this, we only need two pages, 33% less (!), 145 * than the 2-level configuration to map the same address space. 146 * 147 * There is a cost, however: each access that misses the Address Translation 148 * Cache costs one extra memory cycle in the 3-level configuration. 149 * 150 * LOCKING IN THIS PMAP MODULE: 151 * 152 * This pmap module supports fine-grained locking for multiprocessor 153 * systems. 154 * 155 * There are two data structures that require access serialization: the 156 * pmap structures and the PV lists contained within the vm_page structures 157 * ("pvh"). 158 * 159 * ** pmap locking ** 160 * The general idea is that the pmap is locked for the duration of a 161 * pmap operation. For the most part, pmap operations are non-blocking. 162 * and calls to memory allocators to free resources are deferred to 163 * pmap_update() calls which happen outside the critical path. 164 * 165 * There is an exception, however: pmap_enter() frequently needs to 166 * allocate resources to perform its work -- page tables and PV entries. 167 * The code around these allocations is structured in such a way that if 168 * the allocation may block, the pmap lock is dropped, the allocation 169 * performed, and then the operation is restarted from the beginning. 170 * 171 * ** PV list locking ** 172 * PV lists are locked when mappings are entered or removed via the 173 * usual map-oriented entry points (pmap_enter(), pmap_remove(), etc.), 174 * also via the page-orgiented entry points (pmap_page_protect(), 175 * pmap_clear_reference(), etc.). In the former case, the locks are 176 * acquired in the pmap -> pvh order. In the latter case, the locks 177 * are acquired in the pvh -> pmap order. 178 * 179 * In order to avoid a deadlock, we set forth that the canonical ordering 180 * is pmap -> pvh (i.e. virtual -> physical). This is the vast majority 181 * of pmap interface calls. 182 * 183 * In oder to handle this situation, we have the following rules: 184 * 185 * ==> To /modify/ a PTE's attributes, you must hold /either/ the pmap lock 186 * or the pvh lock, and an atomic update to that PTE must be performed 187 * (this is generally handled with pte_update()). 188 * 189 * ==> To /invalidate/ a PTE and remove a mapping (including changing the 190 * PA mapped by the PTE), you must hold /both/ the pmap lock /and/ the 191 * pvh lock. 192 * 193 * ==> The canonocal locking order is pmap -> pvh. pvh -> pmap must be 194 * done using try-locking with a recovery path. The place where 195 * this matters is pmap_page_protect(), which might have to deal 196 * with PROT_NONE, in which case it needs to acquire a pmap's lock 197 * while holding the pvh lock for that page in order to remove all 198 * mappings of the page. 199 * 200 * Notably, pmap_testbit() and pmap_changebit() (used to implement 201 * pmap_is_{modified,referenced}() and pmap_clear_{modify,reference}() 202 * respectively) do not have to lock any pmaps at all. 203 * 204 * Also note: the kernel pmap's lock is not acquired in pmap_kenter_pa() 205 * or pmap_kremove(). Those functions can be called from interrupt 206 * context, and their mappings are explcitiy not PV-tracked per the API 207 * contract. Since the kernel pmap's PTEs are always made available 208 * ahead of time and never freed, there is no need to serialize any 209 * access within those functions. 210 * 211 * Now, as you might expect, there aren't exactly a lot of MP-capable 212 * m68k systems just floating around out there. And so, this code has 213 * been structured such that, when built without the MULTIPROCESSOR 214 * option, no memory or cpu-time costs are paid -- all of the calls and 215 * initializations are compiled out and what remains is perfectly safe 216 * to run on a uni-processor system. The blocking-for-allocation handling 217 * remains, however, since it's evert bit as relevant on a UP system as 218 * an MP system. 219 */ 220 221 /* 222 * Current status: 223 * - Very stable multi-user on virt68k (qemu 68040; does not accurately 224 * model cache or ATC, but suitable for exercising large memory configs). 225 * 226 * - Very stable multi-user running memory pressure stress tests on 227 * small-memory configs in virt68k (qemu 68040 **with fixed MMU 228 * emulation**) and mac68k (10MB 68040, 68030). Very stable on 229 * diskless next68k (32MB 68040). 230 * 231 * - Ports that have been adapted: cesfic (default), hp300 (default), 232 * luna68k (default), mac68k (default), mvme68k (default), news68k (default), 233 * next68k (default), virt68k (default), x68k. 234 * 235 * XXX TODO XXX 236 * 237 * - Adapt amiga (hard), atari (hard). 238 * - Test on 68020 + 68851. It turns out, this is a pretty rare 239 * combination to find out in the wild. No problems are anticipated, 240 * however, since the 68030's MMU is essentially identical for our 241 * purposes. 242 * - Test on real HP MMU. 243 * - More rigorous testing in various emulators (Nono, UAE?). 244 * - Convert '851 / '030 to 3-level (maybe?). 245 * - Tune ATC batch thresholds for each MMU type. 246 * - Add some more instrumentation. 247 * - Eventually disable instrumentation by default. 248 * - ... 249 * - PROFIT! 250 * 251 * Oh, yah, and there's some work going on here to support multiprocessor 252 * machines. It's not ready yet, so don't get too excited. 253 */ 254 255 #include "opt_ddb.h" 256 #include "opt_kgdb.h" 257 #include "opt_m68k_arch.h" 258 #include "opt_multiprocessor.h" 259 260 #include <sys/cdefs.h> 261 __KERNEL_RCSID(0, "$NetBSD: pmap_68k.c,v 1.117 2026/11/25 15:28:25 thorpej Exp $"); 262 263 #include <sys/param.h> 264 #include <sys/systm.h> 265 #include <sys/evcnt.h> 266 #include <sys/proc.h> 267 #include <sys/pool.h> 268 #include <sys/cpu.h> 269 #include <sys/atomic.h> 270 #include <sys/kmem.h> 271 272 #include <machine/pcb.h> 273 274 #include <uvm/uvm.h> 275 #include <uvm/uvm_physseg.h> 276 277 #include <m68k/cacheops.h> 278 #include <m68k/mmu.h> 279 280 #if !defined(M68K_MMU_MOTOROLA) && !defined(M68K_MMU_HP) 281 #error Hit the road, Jack... 282 #endif 283 284 /**************************** MMU CONFIGURATION ******************************/ 285 286 #include "opt_m68k_arch.h" 287 288 #if defined(M68K_MMU_68030) 289 #include <m68k/mmu_30.h> /* for cpu_kcore_hdr_t */ 290 #endif 291 292 #define ATC_MAXVA 8 /* max ATC batch */ 293 294 /* 295 * We consider 3 different MMU classes: 296 * - 68851 (includes 68030) 297 * - 68040 (includes 68060) 298 * - HP MMU for 68020 (68851-like, 2-level 4K only, external VAC) 299 */ 300 301 #define MMU_CLASS_68851 0 302 #define MMU_CLASS_68040 1 303 #define MMU_CLASS_HP 3 304 305 static int pmap_mmuclass __read_mostly; 306 307 #if defined(M68K_MMU_68851) || defined(M68K_MMU_68030) 308 #define MMU_CONFIG_68851_CLASS 1 309 #else 310 #define MMU_CONFIG_68851_CLASS 0 311 #endif 312 313 #if defined(M68K_MMU_68040) || defined(M68K_MMU_68060) 314 #define MMU_CONFIG_68040_CLASS 1 315 #else 316 #define MMU_CONFIG_68040_CLASS 0 317 #endif 318 319 #if defined(M68K_MMU_HP) 320 #define MMU_CONFIG_HP_CLASS 1 321 #else 322 #define MMU_CONFIG_HP_CLASS 0 323 #endif 324 325 #define MMU_CONFIG_NCLASSES (MMU_CONFIG_68851_CLASS + \ 326 MMU_CONFIG_68040_CLASS + \ 327 MMU_CONFIG_HP_CLASS) 328 329 #if MMU_CONFIG_NCLASSES == 1 330 331 #if MMU_CONFIG_68851_CLASS 332 #define MMU_IS_68851_CLASS 1 333 #elif MMU_CONFIG_68040_CLASS 334 #define MMU_IS_68040_CLASS 1 335 #elif MMU_CONFIG_HP_CLASS 336 #define MMU_IS_HP_CLASS 1 337 #else 338 #error Single MMU config predicate error. 339 #endif 340 341 #else /* MMU_CONFIG_NCLASSES != 1 */ 342 343 #if MMU_CONFIG_68851_CLASS 344 #define MMU_IS_68851_CLASS (pmap_mmuclass == MMU_CLASS_68851) 345 #endif 346 347 #if MMU_CONFIG_68040_CLASS 348 #define MMU_IS_68040_CLASS (pmap_mmuclass == MMU_CLASS_68040) 349 #endif 350 351 #if MMU_CONFIG_HP_CLASS 352 #define MMU_IS_HP_CLASS (pmap_mmuclass == MMU_CLASS_HP) 353 #endif 354 355 #endif /* MMU_CONFIG_NCLASSES == 1 */ 356 357 #ifndef MMU_IS_68851_CLASS 358 #define MMU_IS_68851_CLASS 0 359 #endif 360 361 #ifndef MMU_IS_68040_CLASS 362 #define MMU_IS_68040_CLASS 0 363 #endif 364 365 #ifndef MMU_IS_HP_CLASS 366 #define MMU_IS_HP_CLASS 0 367 #endif 368 369 /* 370 * 68040 must always use 3-level. Eventually, we will switch the '851 371 * type over to 3-level as well, for for now, it gets 2-level. The 372 * HP MMU is stuck there for all eternity. 373 */ 374 #define MMU_USE_3L (MMU_IS_68040_CLASS) 375 #define MMU_USE_2L (!MMU_USE_3L) 376 377 /***************************** INSTRUMENTATION *******************************/ 378 379 #define PMAP_EVENT_COUNTERS 380 381 /* vvv these are protected by the growkernel lock vvv */ 382 383 static struct evcnt pmap_nkptpages_initial_ev = 384 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap nkptpages", "initial"); 385 static struct evcnt pmap_nkptpages_current_ev = 386 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap nkptpages", "current"); 387 EVCNT_ATTACH_STATIC(pmap_nkptpages_initial_ev); 388 EVCNT_ATTACH_STATIC(pmap_nkptpages_current_ev); 389 390 static struct evcnt pmap_nkstpages_initial_ev = 391 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap nkstpages", "initial"); 392 static struct evcnt pmap_nkstpages_current_ev = 393 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap nkstpages", "current"); 394 EVCNT_ATTACH_STATIC(pmap_nkstpages_initial_ev); 395 EVCNT_ATTACH_STATIC(pmap_nkstpages_current_ev); 396 397 static struct evcnt pmap_maxkva_ev = 398 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap", "maxkva"); 399 EVCNT_ATTACH_STATIC(pmap_maxkva_ev); 400 static struct evcnt pmap_kvalimit_ev = 401 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap", "kvalimit"); 402 EVCNT_ATTACH_STATIC(pmap_kvalimit_ev); 403 404 /* ^^^ these are protected by the growkernel lock ^^^ */ 405 406 static struct evcnt pmap_nptpages_current_ev = 407 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap nptpages", "current"); 408 static struct evcnt pmap_nptpages_hiwat_ev = 409 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap nptpages", "hiwat"); 410 EVCNT_ATTACH_STATIC(pmap_nptpages_current_ev); 411 EVCNT_ATTACH_STATIC(pmap_nptpages_hiwat_ev); 412 413 #ifdef PMAP_EVENT_COUNTERS 414 static struct evcnt pmap_ptpage_alloc_ev = 415 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap ptpage", "alloc"); 416 EVCNT_ATTACH_STATIC(pmap_ptpage_alloc_ev); 417 static struct evcnt pmap_ptpage_free_ev = 418 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap ptpage", "free"); 419 EVCNT_ATTACH_STATIC(pmap_ptpage_free_ev); 420 421 static struct evcnt pmap_pv_alloc_wait_ev = 422 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap pv_alloc", "wait"); 423 EVCNT_ATTACH_STATIC(pmap_pv_alloc_wait_ev); 424 425 static struct evcnt pmap_pv_alloc_nowait_ev = 426 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap pv_alloc", "nowait"); 427 EVCNT_ATTACH_STATIC(pmap_pv_alloc_nowait_ev); 428 429 static struct evcnt pmap_pv_enter_called_ev = 430 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap pv_enter", "called"); 431 EVCNT_ATTACH_STATIC(pmap_pv_enter_called_ev); 432 433 static struct evcnt pmap_pv_enter_usr_ci_ev = 434 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap pv_enter", "usr_ci"); 435 EVCNT_ATTACH_STATIC(pmap_pv_enter_usr_ci_ev); 436 437 #if MMU_CONFIG_HP_CLASS 438 static struct evcnt pmap_pv_enter_vac_ci_ev = 439 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap pv_enter", "vac_ci"); 440 EVCNT_ATTACH_STATIC(pmap_pv_enter_vac_ci_ev); 441 #endif 442 443 static struct evcnt pmap_pv_enter_ci_multi_ev = 444 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap pv_enter", "ci_multi"); 445 EVCNT_ATTACH_STATIC(pmap_pv_enter_ci_multi_ev); 446 447 static struct evcnt pmap_pv_remove_called_ev = 448 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap pv_remove", "called"); 449 EVCNT_ATTACH_STATIC(pmap_pv_remove_called_ev); 450 451 static struct evcnt pmap_pv_remove_ci_ev = 452 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap pv_remove", "ci"); 453 EVCNT_ATTACH_STATIC(pmap_pv_remove_ci_ev); 454 455 static struct evcnt pmap_pt_cache_hit_ev = 456 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap pt_cache", "hit"); 457 EVCNT_ATTACH_STATIC(pmap_pt_cache_hit_ev); 458 459 static struct evcnt pmap_pt_cache_miss_ev = 460 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap pt_cache", "miss"); 461 EVCNT_ATTACH_STATIC(pmap_pt_cache_miss_ev); 462 463 static struct evcnt pmap_enter_nowait_ev = 464 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap enter", "nowait"); 465 EVCNT_ATTACH_STATIC(pmap_enter_nowait_ev); 466 467 static struct evcnt pmap_enter_yeswait_ev = 468 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap enter", "yeswait"); 469 EVCNT_ATTACH_STATIC(pmap_enter_yeswait_ev); 470 471 static struct evcnt pmap_enter_pte_alloc_fail_ev = 472 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap enter", "pte alloc failed"); 473 EVCNT_ATTACH_STATIC(pmap_enter_pte_alloc_fail_ev); 474 475 static struct evcnt pmap_enter_pv_alloc_fail_ev = 476 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap enter", "pv alloc failed"); 477 EVCNT_ATTACH_STATIC(pmap_enter_pv_alloc_fail_ev); 478 479 static struct evcnt pmap_enter_valid_ev = 480 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap enter", "valid"); 481 EVCNT_ATTACH_STATIC(pmap_enter_valid_ev); 482 483 static struct evcnt pmap_enter_wire_change_ev = 484 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap enter", "wire change"); 485 EVCNT_ATTACH_STATIC(pmap_enter_wire_change_ev); 486 487 static struct evcnt pmap_enter_prot_change_ev = 488 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap enter", "prot change"); 489 EVCNT_ATTACH_STATIC(pmap_enter_prot_change_ev); 490 491 static struct evcnt pmap_enter_pa_change_ev = 492 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap enter", "pa change"); 493 EVCNT_ATTACH_STATIC(pmap_enter_pa_change_ev); 494 495 static struct evcnt pmap_atc_ic_ev = 496 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "ic all"); 497 EVCNT_ATTACH_STATIC(pmap_atc_ic_ev); 498 static struct evcnt pmap_atc_dc_ev = 499 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "dc all"); 500 EVCNT_ATTACH_STATIC(pmap_atc_dc_ev); 501 static struct evcnt pmap_atc_bc_ev = 502 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "bc all"); 503 EVCNT_ATTACH_STATIC(pmap_atc_bc_ev); 504 505 #if MMU_CONFIG_HP_CLASS 506 static struct evcnt pmap_atc_vac_all_ev = 507 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "vac all"); 508 EVCNT_ATTACH_STATIC(pmap_atc_vac_all_ev); 509 static struct evcnt pmap_atc_vac_super_ev = 510 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "vac super"); 511 EVCNT_ATTACH_STATIC(pmap_atc_vac_super_ev); 512 static struct evcnt pmap_atc_vac_user_ev = 513 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "vac user"); 514 EVCNT_ATTACH_STATIC(pmap_atc_vac_user_ev); 515 #endif 516 517 static struct evcnt pmap_atc_bc60inv_all_ev = 518 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "bc60 all"); 519 EVCNT_ATTACH_STATIC(pmap_atc_bc60inv_all_ev); 520 static struct evcnt pmap_atc_bc60inv_user_ev = 521 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "bc60 user"); 522 EVCNT_ATTACH_STATIC(pmap_atc_bc60inv_user_ev); 523 524 static struct evcnt pmap_atc_ic_pg_ev = 525 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "ic pg"); 526 EVCNT_ATTACH_STATIC(pmap_atc_ic_pg_ev); 527 static struct evcnt pmap_atc_dc_pg_ev = 528 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "dc pg"); 529 EVCNT_ATTACH_STATIC(pmap_atc_dc_pg_ev); 530 static struct evcnt pmap_atc_bc_pg_ev = 531 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "bc pg"); 532 EVCNT_ATTACH_STATIC(pmap_atc_bc_pg_ev); 533 534 static struct evcnt pmap_atc_all_user_ev = 535 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "all user"); 536 EVCNT_ATTACH_STATIC(pmap_atc_all_user_ev); 537 static struct evcnt pmap_atc_all_super_ev = 538 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "all super"); 539 EVCNT_ATTACH_STATIC(pmap_atc_all_super_ev); 540 static struct evcnt pmap_atc_all_ev = 541 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "all"); 542 EVCNT_ATTACH_STATIC(pmap_atc_all_ev); 543 544 static struct evcnt pmap_atc_k_ev = 545 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "k"); 546 EVCNT_ATTACH_STATIC(pmap_atc_k_ev); 547 static struct evcnt pmap_atc_enter_ev = 548 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "enter"); 549 EVCNT_ATTACH_STATIC(pmap_atc_enter_ev); 550 static struct evcnt pmap_atc_remove_ev = 551 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "remove"); 552 EVCNT_ATTACH_STATIC(pmap_atc_remove_ev); 553 static struct evcnt pmap_atc_prot_ev = 554 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "prot"); 555 EVCNT_ATTACH_STATIC(pmap_atc_prot_ev); 556 static struct evcnt pmap_atc_cache_ev = 557 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "cache"); 558 EVCNT_ATTACH_STATIC(pmap_atc_cache_ev); 559 static struct evcnt pmap_atc_activate_ev = 560 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "activate"); 561 EVCNT_ATTACH_STATIC(pmap_atc_activate_ev); 562 563 static struct evcnt pmap_atc_req_all_ev = 564 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "req all"); 565 EVCNT_ATTACH_STATIC(pmap_atc_req_all_ev); 566 static struct evcnt pmap_atc_req_saturate_ev = 567 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "req saturate"); 568 EVCNT_ATTACH_STATIC(pmap_atc_req_saturate_ev); 569 570 static struct evcnt pmap_atc_one_user_ev = 571 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "one user"); 572 EVCNT_ATTACH_STATIC(pmap_atc_one_user_ev); 573 static struct evcnt pmap_atc_one_super_ev = 574 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "one super"); 575 EVCNT_ATTACH_STATIC(pmap_atc_one_super_ev); 576 static struct evcnt pmap_atc_one_skipped_ev = 577 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "one skipped"); 578 EVCNT_ATTACH_STATIC(pmap_atc_one_skipped_ev); 579 static struct evcnt pmap_atc_dfc_switch_ev = 580 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "dfc switch"); 581 EVCNT_ATTACH_STATIC(pmap_atc_dfc_switch_ev); 582 583 static struct evcnt pmap_atc_pv_ev = 584 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "pv"); 585 EVCNT_ATTACH_STATIC(pmap_atc_pv_ev); 586 static struct evcnt pmap_atc_pv_skipped_ev = 587 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "pv skipped"); 588 EVCNT_ATTACH_STATIC(pmap_atc_pv_skipped_ev); 589 static struct evcnt pmap_atc_pv_cpushp_ev = 590 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "pv cpushp"); 591 EVCNT_ATTACH_STATIC(pmap_atc_pv_cpushp_ev); 592 static struct evcnt pmap_atc_pv_cinvp_ev = 593 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "pv cinvp"); 594 EVCNT_ATTACH_STATIC(pmap_atc_pv_cinvp_ev); 595 596 __CTASSERT(ATC_MAXVA == 8); 597 static struct evcnt pmap_atc_0_ev = 598 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "batch 0"); 599 EVCNT_ATTACH_STATIC(pmap_atc_0_ev); 600 static struct evcnt pmap_atc_1_ev = 601 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "batch 1"); 602 EVCNT_ATTACH_STATIC(pmap_atc_1_ev); 603 static struct evcnt pmap_atc_2_ev = 604 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "batch 2"); 605 EVCNT_ATTACH_STATIC(pmap_atc_2_ev); 606 static struct evcnt pmap_atc_3_ev = 607 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "batch 3"); 608 EVCNT_ATTACH_STATIC(pmap_atc_3_ev); 609 static struct evcnt pmap_atc_4_ev = 610 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "batch 4"); 611 EVCNT_ATTACH_STATIC(pmap_atc_4_ev); 612 static struct evcnt pmap_atc_5_ev = 613 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "batch 5"); 614 EVCNT_ATTACH_STATIC(pmap_atc_5_ev); 615 static struct evcnt pmap_atc_6_ev = 616 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "batch 6"); 617 EVCNT_ATTACH_STATIC(pmap_atc_6_ev); 618 static struct evcnt pmap_atc_7_ev = 619 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "batch 7"); 620 EVCNT_ATTACH_STATIC(pmap_atc_7_ev); 621 static struct evcnt pmap_atc_8_ev = 622 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap atc", "batch 8"); 623 EVCNT_ATTACH_STATIC(pmap_atc_8_ev); 624 625 static struct evcnt pmap_lev1map_set_same_ev = 626 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap lev1map", "set same"); 627 EVCNT_ATTACH_STATIC(pmap_lev1map_set_same_ev); 628 static struct evcnt pmap_lev1map_act_hit_ev = 629 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap lev1map", "act hit"); 630 EVCNT_ATTACH_STATIC(pmap_lev1map_act_hit_ev); 631 static struct evcnt pmap_lev1map_act_miss_ev = 632 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap lev1map", "act miss"); 633 EVCNT_ATTACH_STATIC(pmap_lev1map_act_miss_ev); 634 static struct evcnt pmap_lev1map_fault_null_ev = 635 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap lev1map", "fault null"); 636 EVCNT_ATTACH_STATIC(pmap_lev1map_fault_null_ev); 637 #endif 638 639 #if defined(MULTIPROCESSOR) 640 #define pmap_evcnt_impl(e) atomic_inc_32(&pmap_ ## e ## _ev.ev_count32) 641 #define pmap_evcnt_dec_impl(e) atomic_dec_32(&pmap_ ## e ## _ev.ev_count32) 642 #else 643 #define pmap_evcnt_impl(e) __asm __volatile("addql %1,%0" \ 644 : "+m" (pmap_ ## e ## _ev.ev_count32) \ 645 : "i" (1)) 646 #define pmap_evcnt_dec_impl(e) __asm __volatile("subql %1,%0" \ 647 : "+m" (pmap_ ## e ## _ev.ev_count32) \ 648 : "i" (1)) 649 #endif 650 #define pmap_evcnt_set_impl(e, v) \ 651 atomic_store_relaxed(&pmap_ ## e ## _ev.ev_count32, (v)) 652 653 #ifdef PMAP_EVENT_COUNTERS 654 #define pmap_evcnt(e) pmap_evcnt_impl(e) 655 #define pmap_evcnt_dec(e) pmap_evcnt_dec_impl(e) 656 #define pmap_evcnt_set(e, v) pmap_evcnt_set_impl((e), (v)) 657 #else 658 #define pmap_evcnt(e) __nothing 659 #define pmap_evcnt_dec(e) __nothing 660 #define pmap_evcnt_set(e, v) __nothing 661 #endif 662 663 #if MMU_CONFIG_HP_CLASS 664 static void pmap_atc_process_hp(void); 665 #endif 666 #if MMU_CONFIG_68851_CLASS 667 static void pmap_atc_process_51(void); 668 #endif 669 #if MMU_CONFIG_68040_CLASS 670 static void pmap_atc_process_40(void); 671 #endif 672 673 static void (*pmap_load_urp_func)(paddr_t) __read_mostly; 674 static void (*pmap_atc_process_local_func)(void) __read_mostly; 675 676 static void 677 pmap_mmuclass_init(void) 678 { 679 switch (mmutype) { 680 #if MMU_CONFIG_68040_CLASS 681 case MMU_68040: 682 case MMU_68060: 683 pmap_mmuclass = MMU_CLASS_68040; 684 pmap_atc_process_local_func = pmap_atc_process_40; 685 /* 686 * XXX This is messy because 68060 frequently gets 687 * XXX initialize to MMU_68040. Should be cleaned 688 * XXX up once the Hibler pmap is obsoleted. 689 */ 690 #if defined(M68040) 691 if (cputype == CPU_68040) { 692 pmap_load_urp_func = mmu_load_urp40; 693 } 694 #endif 695 #if defined(M68060) 696 if (cputype == CPU_68060) { 697 pmap_load_urp_func = mmu_load_urp60; 698 } 699 #endif 700 break; 701 #endif 702 #if MMU_CONFIG_68851_CLASS 703 case MMU_68851: 704 case MMU_68030: 705 pmap_mmuclass = MMU_CLASS_68851; 706 pmap_atc_process_local_func = pmap_atc_process_51; 707 protorp[0] = MMU51_CRP_BITS; 708 pmap_load_urp_func = mmu_load_urp51; 709 break; 710 #endif 711 #if MMU_CONFIG_HP_CLASS 712 case MMU_HP: 713 pmap_mmuclass = MMU_CLASS_HP; 714 pmap_atc_process_local_func = pmap_atc_process_hp; 715 pmap_load_urp_func = mmu_load_urp20hp; 716 break; 717 #endif 718 default: 719 panic("%s: mmutype=%d not configured?", __func__, mmutype); 720 } 721 722 if (pmap_atc_process_local_func == NULL) { 723 panic("%s: No pmap_atc_process_*() for cputype=%d mmutype=%d", 724 __func__, cputype, mmutype); 725 } 726 727 if (pmap_load_urp_func == NULL) { 728 panic("%s: No mmu_load_*() for cputype=%d mmutype=%d", 729 __func__, cputype, mmutype); 730 } 731 } 732 733 #if MMU_CONFIG_HP_CLASS 734 static vaddr_t pmap_aliasmask __read_mostly; 735 #endif 736 737 /* 738 * pmap_init_vac: 739 * 740 * Set up virtually-addressed cache information. Only relevant 741 * for the HP MMU. 742 */ 743 void 744 pmap_init_vac(size_t vacsize) 745 { 746 #if MMU_CONFIG_HP_CLASS 747 KASSERT(pmap_aliasmask == 0); 748 KASSERT(powerof2(vacsize)); 749 pmap_aliasmask = vacsize - 1; 750 #endif 751 } 752 753 /*************************** FORWARD DECLARATIONS ****************************/ 754 755 #if defined(MULTIPROCESSOR) 756 757 struct pmap_message { 758 uint32_t pmsg_cpus; /* target CPUs */ 759 u_int pmsg_type; /* message type */ 760 union { 761 pmap_t pmsg_pmap; /* target pmap */ 762 }; 763 }; 764 765 #define PMSG_T_UNLOAD 0 /* unload this user pmap */ 766 767 #define PMSG_T_user_last PMSG_T_UNLOAD 768 769 static void pmap_message_user_acquire(struct pmap_message *); 770 static void pmap_message_user_send(struct pmap_message *); 771 static void pmap_message_user_wait(struct pmap_message *); 772 773 #endif /* MULTIPROCESSOR */ 774 775 /***************************** PHYS <-> VM PAGE ******************************/ 776 777 static bool pmap_initialized_p __read_mostly; 778 779 static inline struct vm_page * 780 pmap_pa_to_pg(paddr_t pa) 781 { 782 return pmap_initialized_p ? PHYS_TO_VM_PAGE(pa) : NULL; 783 } 784 785 static pt_entry_t pmap_changebit(struct vm_page *, pt_entry_t, pt_entry_t); 786 787 /*************************** RESOURCE MANAGEMENT *****************************/ 788 789 static struct pmap kernel_pmap_store; 790 struct pmap * const kernel_pmap_ptr = &kernel_pmap_store; 791 792 /* 793 * Physical address of kernel level 1 table. This name is compatible 794 * with the Hibler pmap's name. 795 */ 796 paddr_t Sysseg_pa __read_mostly; 797 798 /* 799 * Avoid a memory load when doing comparisons against pmap_kernel() 800 * within this compilation unit. 801 */ 802 #undef pmap_kernel 803 #define pmap_kernel() (&kernel_pmap_store) 804 805 /* 806 * Number of tables per page table page: 807 * 0 - number of leaf page tables per page 808 * 1 - number of segment tables per page 809 */ 810 static unsigned int pmap_ptpage_table_counts[2] __read_mostly; 811 812 __CTASSERT(LA40_L1_COUNT == LA40_L2_COUNT); 813 814 static void 815 pmap_ptpage_init(void) 816 { 817 if (MMU_USE_3L) { 818 pmap_ptpage_table_counts[0] = PAGE_SIZE / TBL40_L3_SIZE; 819 pmap_ptpage_table_counts[1] = PAGE_SIZE / TBL40_L2_SIZE; 820 } else { 821 pmap_ptpage_table_counts[0] = 1; 822 pmap_ptpage_table_counts[1] = 1; 823 } 824 } 825 826 static struct vm_page * 827 pmap_page_alloc(bool nowait) 828 { 829 uint64_t ticket; 830 struct vm_page *pg; 831 const int flags = nowait ? UVM_PGA_USERESERVE : 0; 832 833 while (ticket = uvm_wait_prepare(), 834 (pg = uvm_pagealloc(NULL, 0, NULL, flags)) == NULL) { 835 if (nowait) { 836 return NULL; 837 } 838 uvm_wait("pmappg", ticket); 839 } 840 pg->flags &= ~PG_BUSY; /* never busy */ 841 842 return pg; 843 } 844 845 static vaddr_t 846 pmap_vpage_alloc(bool nowait, int flags, struct vm_page **pg_out) 847 { 848 struct vm_page *pg; 849 const int uvm_f_nowait = nowait ? UVM_KMF_NOWAIT : 0; 850 vaddr_t pgva; 851 852 /* Allocate a VA for the page. */ 853 pgva = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, 854 UVM_KMF_VAONLY | uvm_f_nowait); 855 if (__predict_false(pgva == 0)) { 856 return 0; 857 } 858 859 /* Get a page. */ 860 pg = pmap_page_alloc(nowait); 861 if (__predict_false(pg == NULL)) { 862 uvm_km_free(kernel_map, pgva, PAGE_SIZE, UVM_KMF_VAONLY); 863 return 0; 864 } 865 866 /* Map the page according to the flags and zero it out. */ 867 pmap_kenter_pa(pgva, VM_PAGE_TO_PHYS(pg), 868 UVM_PROT_READ | UVM_PROT_WRITE, flags); 869 zeropage((void *)pgva); 870 871 if (__predict_true(pg_out != NULL)) { 872 *pg_out = pg; 873 } 874 875 return pgva; 876 } 877 878 static struct pmap_ptpage * 879 pmap_ptpage_alloc(pmap_t pmap, bool segtab, bool nowait) 880 { 881 const unsigned int tabcnt = pmap_ptpage_table_counts[segtab]; 882 const size_t size = sizeof(struct pmap_ptpage) + 883 (sizeof(struct pmap_table) * tabcnt); 884 const size_t tabsize = PAGE_SIZE / tabcnt; 885 struct vm_page *pg; 886 struct pmap_ptpage *ptp; 887 struct pmap_table *pt; 888 vaddr_t ptpva; 889 890 /* First check to see if we have one in the garbage bin. */ 891 ptp = TAILQ_FIRST(&pmap->pm_garbage.g_ptpages); 892 if (__predict_false(ptp != NULL)) { 893 ptpva = m68k_ptob(ptp->ptp_vpagenum); 894 pg = ptp->ptp_pg; 895 goto gotit; 896 } 897 898 KASSERT(PMAP_LOCK_OWNED(pmap)); 899 PMAP_UNLOCK(pmap); 900 901 ptp = kmem_zalloc(size, nowait ? KM_NOSLEEP : KM_SLEEP); 902 if (__predict_false(ptp == NULL)) { 903 goto fail; 904 } 905 906 /* 907 * Allocate a mapped page for the PT page. PT pages 908 * are always mapped cache-inhibited. 909 */ 910 ptpva = pmap_vpage_alloc(nowait, PMAP_NOCACHE, &pg); 911 if (ptpva == 0) { 912 kmem_free(ptp, size); 913 goto fail; 914 } 915 916 PMAP_LOCK(pmap); 917 918 /* 919 * All resources for the PT page have been allocated. 920 * Now initialize it and the individual table descriptors. 921 */ 922 gotit: 923 LIST_INIT(&ptp->ptp_freelist); 924 ptp->ptp_pg = pg; 925 ptp->ptp_vpagenum = m68k_btop(ptpva); 926 ptp->ptp_freecnt = tabcnt; 927 ptp->ptp_segtab = segtab; 928 929 for (unsigned int i = 0; i < tabcnt; ptpva += tabsize, i++) { 930 pt = &ptp->ptp_tables[i]; 931 pt->pt_ptpage = ptp; 932 pt->pt_entries = (pt_entry_t *)ptpva; 933 LIST_INSERT_HEAD(&ptp->ptp_freelist, pt, pt_freelist); 934 } 935 936 pmap_evcnt(ptpage_alloc); 937 pmap_evcnt_impl(nptpages_current); 938 if (pmap_nptpages_current_ev.ev_count32 > 939 pmap_nptpages_hiwat_ev.ev_count32) { 940 pmap_evcnt_set_impl(nptpages_hiwat, 941 pmap_nptpages_current_ev.ev_count32); 942 } 943 944 return ptp; 945 fail: 946 PMAP_LOCK(pmap); 947 return NULL; 948 } 949 950 static void 951 pmap_ptpage_free(struct pmap_ptpage *ptp) 952 { 953 const unsigned int tabcnt = pmap_ptpage_table_counts[ptp->ptp_segtab]; 954 const size_t size = sizeof(struct pmap_ptpage) + 955 (sizeof(struct pmap_table) * tabcnt); 956 957 uvm_km_free(kernel_map, m68k_ptob(ptp->ptp_vpagenum), PAGE_SIZE, 958 UVM_KMF_WIRED); 959 kmem_free(ptp, size); 960 961 pmap_evcnt(ptpage_free); 962 pmap_evcnt_dec(nptpages_current); 963 } 964 965 static struct pool pmap_pool; 966 static struct pool pmap_pv_pool; 967 968 #define PMAP_PV_LOWAT 16 969 970 static void 971 pmap_alloc_init(void) 972 { 973 pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 974 PVH_ATTR_MASK + 1, /* align */ 975 0, /* ioff */ 976 0, /* flags */ 977 "pmappv", /* wchan */ 978 &pool_allocator_meta, /* palloc */ 979 IPL_VM); /* ipl */ 980 981 /* 982 * Set a low water mark on the pv_entry pool, so that we are 983 * more likely to have these around even in extreme memory 984 * starvation. 985 */ 986 pool_setlowat(&pmap_pv_pool, PMAP_PV_LOWAT); 987 988 pool_init(&pmap_pool, sizeof(struct pmap), 989 #ifdef PMAP_ENABLE_LOCKS 990 CACHELINE_SIZE, /* cacheline-aligned if using locks */ 991 #else 992 0, /* align */ 993 #endif 994 0, /* ioff */ 995 0, /* flags */ 996 "pmappl", /* wchan */ 997 &pool_allocator_kmem, /* palloc */ 998 IPL_NONE); /* ipl */ 999 } 1000 1001 static inline pmap_t 1002 pmap_alloc(void) 1003 { 1004 pmap_t pmap = pool_get(&pmap_pool, PR_WAITOK); 1005 memset(pmap, 0, sizeof(*pmap)); 1006 return pmap; 1007 } 1008 1009 static inline void 1010 pmap_free(pmap_t pmap) 1011 { 1012 pool_put(&pmap_pool, pmap); 1013 } 1014 1015 static struct pv_entry * 1016 pmap_pv_alloc(pmap_t pmap, bool nowait) 1017 { 1018 struct pv_entry *pv; 1019 1020 #ifdef PMAP_EVENT_COUNTERS 1021 if (nowait) { 1022 pmap_evcnt(pv_alloc_nowait); 1023 } else { 1024 pmap_evcnt(pv_alloc_wait); 1025 } 1026 #endif 1027 1028 /* Check for one in the garbage bin. */ 1029 pv = LIST_FIRST(&pmap->pm_garbage.g_pvlist); 1030 if (__predict_false(pv != NULL)) { 1031 return pv; 1032 } 1033 1034 KASSERT(PMAP_LOCK_OWNED(pmap)); 1035 1036 if (! nowait) { 1037 PMAP_UNLOCK(pmap); 1038 } 1039 pv = pool_get(&pmap_pv_pool, nowait ? PR_NOWAIT : 0); 1040 if (__predict_true(pv != NULL)) { 1041 KASSERT((((uintptr_t)pv) & PVH_ATTR_MASK) == 0); 1042 } 1043 if (! nowait) { 1044 PMAP_LOCK(pmap); 1045 } 1046 return pv; 1047 } 1048 1049 static void 1050 pmap_pv_free(struct pv_entry *pv) 1051 { 1052 pool_put(&pmap_pv_pool, pv); 1053 } 1054 1055 /* 1056 * Whenever we need to free resources back to the system, we want to 1057 * do it in a batch with any locks released. So, we have this around 1058 * to collect the garbage, as needed. 1059 */ 1060 static inline void 1061 pmap_garbage_init(struct pmap_garbage *g) 1062 { 1063 TAILQ_INIT(&g->g_ptpages); 1064 LIST_INIT(&g->g_pvlist); 1065 } 1066 1067 static inline void 1068 pmap_garbage_transfer(struct pmap_garbage *to, struct pmap_garbage *from) 1069 { 1070 TAILQ_INIT(&to->g_ptpages); 1071 TAILQ_CONCAT(&to->g_ptpages, &from->g_ptpages, ptp_list); 1072 LIST_MOVE(&from->g_pvlist, &to->g_pvlist, pv_pmlist); 1073 } 1074 1075 static void 1076 pmap_garbage_collect(struct pmap_garbage *g) 1077 { 1078 struct pmap_ptpage *ptp; 1079 struct pv_entry *pv; 1080 1081 while ((ptp = TAILQ_FIRST(&g->g_ptpages)) != NULL) { 1082 TAILQ_REMOVE(&g->g_ptpages, ptp, ptp_list); 1083 /* 1084 * Can't assert ptp_freecnt here; it won't match up 1085 * in the pmap_remove_all() case. 1086 * 1087 * KASSERT(ptp->ptp_freecnt == 1088 * pmap_ptpage_table_counts[ptp->ptp_segtab]); 1089 */ 1090 pmap_ptpage_free(ptp); 1091 } 1092 1093 while ((pv = LIST_FIRST(&g->g_pvlist)) != NULL) { 1094 LIST_REMOVE(pv, pv_pmlist); 1095 pmap_pv_free(pv); 1096 } 1097 } 1098 1099 /*************************** PER-CPU PMAP DATA *******************************/ 1100 1101 #if defined(MULTIPROCESSOR) 1102 static inline uint32_t 1103 pmap_cpu_others(pmap_t pmap) 1104 { 1105 return atomic_load_relaxed(&pmap->pm_cpus) & ~(1U << cpu_number()); 1106 } 1107 1108 static inline void 1109 pmap_cpu_self_set(pmap_t pmap) 1110 { 1111 /* 1112 * We use an atomic operation to synchronize against IPIs, 1113 * and require the mutex to be owned to synchronize with 1114 * anyone wanted to send ATC invalidation messages. 1115 */ 1116 KASSERT(PMAP_LOCK_OWNED(pmap)); 1117 atomic_or_32(&pmap->pm_cpus, 1U << cpu_number()); 1118 } 1119 1120 static inline void 1121 pmap_cpu_self_clear(pmap_t pmap) 1122 { 1123 /* 1124 * Only atomic operations here; the worst thing that can 1125 * happen is we take an IPI and don't need to do any work. 1126 */ 1127 atomic_and_32(&pmap->pm_cpus, ~(1U << cpu_number())); 1128 } 1129 #else 1130 #define pmap_cpu_others(pm) 0 1131 #define pmap_cpu_self_set(pm) __nothing 1132 #define pmap_cpu_self_clear(pm) __nothing 1133 #endif /* MULTIPROCESSOR */ 1134 1135 /* 1136 * We keep some per-cpu information immediately following the cpu_info 1137 * structure for this CPU. 1138 */ 1139 1140 #define ATC_ALLVA ((uint16_t)-1) 1141 struct pmap_atc { 1142 uint16_t atc_count; 1143 uint16_t atc_flags; 1144 struct { 1145 pmap_t atc_pmap; 1146 vaddr_t atc_addrflags; 1147 paddr_t atc_pa; /* for 68040/68060 cache flush */ 1148 } atc_data[ATC_MAXVA]; 1149 }; 1150 1151 #define ATC_F_ALL __BIT(0) /* hit it all */ 1152 #define ATC_F_USER __BIT(1) /* user VAs involved */ 1153 #define ATC_F_SUPER __BIT(2) /* supervisor VAs involved */ 1154 #define ATC_F_IC __BIT(3) /* also flush the I cache */ 1155 #define ATC_F_DC __BIT(4) /* also flush the D cache */ 1156 #define ATC_F_BC (ATC_F_IC|ATC_F_DC) 1157 #define ATC_F_PV __BIT(5) /* result of a P->V operation */ 1158 #define ATC_F_K __BIT(6) /* result of pmap_k*() */ 1159 #define ATC_F_ENTER __BIT(7) /* result of an "enter" */ 1160 #define ATC_F_REMOVE __BIT(8) /* result of a "remove" */ 1161 #define ATC_F_PROT __BIT(9) /* result of a protection op */ 1162 #define ATC_F_CACHE __BIT(10) /* result of a cache op */ 1163 1164 #define ATC_DAT_VA(a, i) ((a)->atc_data[i].atc_addrflags & ~PAGE_MASK) 1165 1166 struct pmap_percpu { 1167 pmap_t pc_pmap; 1168 struct pmap_atc pc_atc; /* ATC invalidation info */ 1169 paddr_t pc_lev1pa; /* value currently loaded into %urp */ 1170 }; 1171 1172 /* 1173 * m68k_machdep.c CTASSERT's that sizeof(struct cpu_info) is <= 2KB. 1174 * We'll do one better and assert that sizeof(struct cpu_info) PLUS 1175 * sizeof(struct pmap_percpu) is <= 2KB. Take that, slackers! 1176 */ 1177 __CTASSERT(sizeof(struct cpu_info) + sizeof(struct pmap_percpu) <= 2048); 1178 1179 static inline struct pmap_percpu * 1180 pmap_percpu(void) 1181 { 1182 vaddr_t va = (vaddr_t)&cpuinfo; 1183 1184 va += sizeof(struct cpu_info); 1185 return (struct pmap_percpu *)va; 1186 } 1187 1188 /******************** ADDRESS TRANSLATION CACHE HANDLING ********************/ 1189 1190 #if defined(M68020) || defined(M68030) 1191 static inline void 1192 ic20_inval(void) 1193 { 1194 __asm __volatile("movc %0,%%cacr" 1195 : 1196 : "d" (IC_CLEAR)); 1197 } 1198 #endif 1199 1200 #if MMU_CONFIG_HP_CLASS 1201 extern vaddr_t MMUbase; 1202 1203 static inline void 1204 hp_vac_inv_all(void) 1205 { 1206 uint32_t *mmureg = (uint32_t *)(MMUbase + MMUCMD); 1207 1208 __asm __volatile("andl %0,%2;" 1209 "orl %1,%2" 1210 : 1211 : "i" (~MMU_CEN), "i" (MMU_CEN), "m" (*mmureg)); 1212 } 1213 1214 static inline void 1215 hp_vac_inv_super(void) 1216 { 1217 uint32_t *mmureg = (uint32_t *)(MMUbase + MMUSSTP); 1218 int tmp; 1219 1220 __asm __volatile("movl %1,%0;" 1221 "movl %0,%1" 1222 : "=d" (tmp) 1223 : "m" (*mmureg)); 1224 } 1225 1226 static inline void 1227 hp_vac_inv_user(void) 1228 { 1229 uint32_t *mmureg = (uint32_t *)(MMUbase + MMUUSTP); 1230 int tmp; 1231 1232 __asm __volatile("movl %1,%0;" 1233 "movl %0,%1" 1234 : "=d" (tmp) 1235 : "m" (*mmureg)); 1236 } 1237 1238 static inline void 1239 hp_tbia(void) 1240 { 1241 uint32_t *mmureg = (uint32_t *)(MMUbase + MMUTBINVAL); 1242 int tmp; 1243 1244 __asm __volatile("movl %1,%0" 1245 : "=d" (tmp) 1246 : "m" (*mmureg)); 1247 } 1248 1249 static inline void 1250 hp_tbias(void) 1251 { 1252 uint32_t *mmureg = (uint32_t *)(MMUbase + MMUTBINVAL); 1253 1254 __asm __volatile("movl %0,%1" 1255 : 1256 : "i" (0x8000), "m" (*mmureg)); 1257 } 1258 1259 static inline void 1260 hp_tbiau(void) 1261 { 1262 uint32_t *mmureg = (uint32_t *)(MMUbase + MMUTBINVAL); 1263 1264 __asm __volatile("movl %0,%1" 1265 : 1266 : "i" (0), "m" (*mmureg)); 1267 } 1268 1269 static inline void 1270 hp_tbis(vaddr_t va) 1271 { 1272 __asm __volatile("movsl %0,(%1)" 1273 : 1274 : "d" (0), "a" (va)); 1275 } 1276 1277 static void 1278 pmap_atc_process_hp(void) 1279 { 1280 const struct pmap_percpu *pc = pmap_percpu(); 1281 const struct pmap_atc *atc = &pc->pc_atc; 1282 1283 if (atc->atc_flags & ATC_F_IC) { 1284 pmap_evcnt(atc_ic); 1285 ic20_inval(); 1286 } 1287 1288 if (atc->atc_flags & ATC_F_DC) { 1289 switch (atc->atc_flags & (ATC_F_SUPER|ATC_F_USER)) { 1290 case ATC_F_SUPER: 1291 pmap_evcnt(atc_vac_super); 1292 hp_vac_inv_super(); 1293 /* this also invalidates the TLB */ 1294 return; 1295 case ATC_F_USER: 1296 pmap_evcnt(atc_vac_user); 1297 hp_vac_inv_user(); 1298 /* this also invalidates the TLB */ 1299 return; 1300 default: 1301 pmap_evcnt(atc_vac_all); 1302 hp_vac_inv_all(); 1303 if (atc->atc_flags & ATC_F_ALL) { 1304 pmap_evcnt(atc_all); 1305 hp_tbia(); 1306 return; 1307 } 1308 break; 1309 } 1310 } 1311 1312 switch (atc->atc_flags & (ATC_F_ALL|ATC_F_SUPER|ATC_F_USER)) { 1313 case ATC_F_ALL|ATC_F_SUPER|ATC_F_USER: 1314 pmap_evcnt(atc_all); 1315 hp_tbia(); 1316 return; 1317 case ATC_F_ALL|ATC_F_SUPER: 1318 pmap_evcnt(atc_all_super); 1319 hp_tbias(); 1320 return; 1321 case ATC_F_ALL|ATC_F_USER: 1322 pmap_evcnt(atc_all_user); 1323 hp_tbiau(); 1324 return; 1325 } 1326 1327 setdfc(FC_PURGE); 1328 for (int i = 0; i < atc->atc_count; i++) { 1329 if (atc->atc_data[i].atc_pmap == pmap_kernel()) { 1330 pmap_evcnt(atc_one_super); 1331 } else if (atc->atc_data[i].atc_pmap == pc->pc_pmap) { 1332 pmap_evcnt(atc_one_user); 1333 } else { 1334 pmap_evcnt(atc_one_skipped); 1335 continue; 1336 } 1337 vaddr_t va = ATC_DAT_VA(atc, i); 1338 hp_tbis(va); 1339 } 1340 setdfc(FC_USERD); 1341 } 1342 #endif /* MMU_CONFIG_HP_CLASS */ 1343 1344 #if MMU_CONFIG_68851_CLASS 1345 static inline void 1346 dc30_inval(void) 1347 { 1348 __asm __volatile("movc %0,%%cacr" 1349 : 1350 : "d" (DC_CLEAR)); 1351 } 1352 1353 static inline void 1354 bc30_inval(void) 1355 { 1356 __asm __volatile("movc %0,%%cacr" 1357 : 1358 : "d" (CACHE_CLR)); 1359 } 1360 1361 static inline void 1362 pflusha51(void) 1363 { 1364 __asm __volatile("pflusha"); 1365 } 1366 1367 static inline void 1368 pflush_all_user51(void) 1369 { 1370 __asm __volatile("pflush %0,#4" 1371 : 1372 : "i" (FC_USERD)); 1373 } 1374 1375 static inline void 1376 pflush_all_super51(void) 1377 { 1378 __asm __volatile("pflush %0,#4" 1379 : 1380 : "i" (FC_SUPERD)); 1381 } 1382 1383 static inline void 1384 pflush_one_user51(vaddr_t va) 1385 { 1386 __asm __volatile("pflush %0,#4,(%1)" 1387 : 1388 : "i" (FC_USERD), "a" (va)); 1389 } 1390 1391 static inline void 1392 pflush_one_super51(vaddr_t va) 1393 { 1394 __asm __volatile("pflush %0,#4,(%1)" 1395 : 1396 : "i" (FC_SUPERD), "a" (va)); 1397 } 1398 1399 static void 1400 pmap_atc_process_51(void) 1401 { 1402 const struct pmap_percpu *pc = pmap_percpu(); 1403 const struct pmap_atc *atc = &pc->pc_atc; 1404 1405 if (atc->atc_flags & ATC_F_ALL) { 1406 switch (atc->atc_flags & (ATC_F_USER|ATC_F_SUPER)) { 1407 case ATC_F_USER: 1408 pmap_evcnt(atc_all_user); 1409 pflush_all_user51(); 1410 break; 1411 case ATC_F_SUPER: 1412 pmap_evcnt(atc_all_super); 1413 pflush_all_super51(); 1414 break; 1415 default: 1416 pmap_evcnt(atc_all); 1417 pflusha51(); 1418 break; 1419 } 1420 goto done; 1421 } 1422 1423 for (int i = 0; i < atc->atc_count; i++) { 1424 vaddr_t va = ATC_DAT_VA(atc, i); 1425 if (atc->atc_data[i].atc_pmap == pmap_kernel()) { 1426 pmap_evcnt(atc_one_super); 1427 pflush_one_super51(va); 1428 } else if (atc->atc_data[i].atc_pmap == pc->pc_pmap) { 1429 pmap_evcnt(atc_one_user); 1430 pflush_one_user51(va); 1431 } else { 1432 pmap_evcnt(atc_one_skipped); 1433 } 1434 } 1435 done: 1436 switch (atc->atc_flags & (ATC_F_IC | ATC_F_DC)) { 1437 case ATC_F_IC | ATC_F_DC: 1438 #ifdef PMAP_EVENT_COUNTERS 1439 if (cputype == CPU_68020) { 1440 pmap_evcnt(atc_ic); 1441 } else { 1442 pmap_evcnt(atc_bc); 1443 } 1444 #endif 1445 bc30_inval(); /* ok to do on 68020, too */ 1446 break; 1447 case ATC_F_IC: 1448 pmap_evcnt(atc_ic); 1449 ic20_inval(); 1450 break; 1451 #ifdef M68030 1452 case ATC_F_DC: 1453 pmap_evcnt(atc_dc); 1454 dc30_inval(); 1455 break; 1456 #endif 1457 default: 1458 break; 1459 } 1460 } 1461 #endif /* MMU_CONFIG_68851_CLASS */ 1462 1463 #if MMU_CONFIG_68040_CLASS 1464 static inline void 1465 cpusha_bc40(void) 1466 { 1467 __asm __volatile(".chip 68040; cpusha %bc; .chip 68030"); 1468 } 1469 1470 static inline void 1471 cpushp_bc40(paddr_t pa) 1472 { 1473 __asm __volatile(".chip 68040; cpushp %%bc,(%0); .chip 68030" 1474 : 1475 : "a" (pa)); 1476 } 1477 1478 static inline void 1479 cinva_ic40(void) 1480 { 1481 __asm __volatile(".chip 68040; cinva %ic; .chip 68030"); 1482 } 1483 1484 static inline void 1485 cinvp_ic40(paddr_t pa) 1486 { 1487 __asm __volatile(".chip 68040; cinvp %%ic,(%0); .chip 68030" 1488 : 1489 : "a" (pa)); 1490 } 1491 1492 static inline void 1493 cpusha_dc40(void) 1494 { 1495 __asm __volatile(".chip 68040; cpusha %dc; .chip 68030"); 1496 } 1497 1498 static inline void 1499 cpushp_dc40(paddr_t pa) 1500 { 1501 __asm __volatile(".chip 68040; cpushp %%dc,(%0); .chip 68030" 1502 : 1503 : "a" (pa)); 1504 } 1505 1506 static inline void 1507 pflusha40(void) 1508 { 1509 __asm __volatile(".chip 68040; pflusha; .chip 68030"); 1510 } 1511 1512 static inline void 1513 pflush40(vaddr_t va) 1514 { 1515 /* We assume %dfc is already set up for us. */ 1516 __asm __volatile(".chip 68040; pflush (%0); .chip 68030" 1517 : 1518 : "a" (va)); 1519 } 1520 1521 #if defined(M68060) 1522 static inline void 1523 bc60_inval(int which) 1524 { 1525 int tmp; 1526 1527 __asm __volatile(" movc %%cacr,%0;" 1528 " orl %1,%0;" 1529 " movc %0,%%cacr" 1530 : "=d" (tmp) 1531 : "d" (which)); 1532 } 1533 #endif /* M68060 */ 1534 1535 static void 1536 pmap_atc_process_40(void) 1537 { 1538 const struct pmap_percpu *pc = pmap_percpu(); 1539 const struct pmap_atc *atc = &pc->pc_atc; 1540 1541 if (atc->atc_flags & ATC_F_ALL) { 1542 switch (atc->atc_flags & (ATC_F_IC|ATC_F_DC)) { 1543 case ATC_F_IC|ATC_F_DC: 1544 pmap_evcnt(atc_bc); 1545 cpusha_bc40(); 1546 break; 1547 case ATC_F_IC: 1548 pmap_evcnt(atc_ic); 1549 cinva_ic40(); 1550 break; 1551 case ATC_F_DC: 1552 pmap_evcnt(atc_dc); 1553 cpusha_dc40(); 1554 break; 1555 default: 1556 break; 1557 } 1558 pmap_evcnt(atc_all); 1559 pflusha40(); 1560 } else { 1561 int i, fc, prev_fc = FC_USERD; /* USERD is steady state */ 1562 for (i = 0; i < atc->atc_count; i++) { 1563 switch (atc->atc_data[i].atc_addrflags & 1564 (ATC_F_IC|ATC_F_DC)) { 1565 case ATC_F_IC|ATC_F_DC: 1566 pmap_evcnt(atc_bc_pg); 1567 cpushp_bc40(atc->atc_data[i].atc_pa); 1568 break; 1569 case ATC_F_IC: 1570 pmap_evcnt(atc_ic_pg); 1571 cinvp_ic40(atc->atc_data[i].atc_pa); 1572 break; 1573 case ATC_F_DC: 1574 pmap_evcnt(atc_dc_pg); 1575 cpushp_dc40(atc->atc_data[i].atc_pa); 1576 break; 1577 default: 1578 break; 1579 } 1580 if (atc->atc_data[i].atc_pmap == pmap_kernel()) { 1581 pmap_evcnt(atc_one_super); 1582 fc = FC_SUPERD; 1583 } else if (atc->atc_data[i].atc_pmap == pc->pc_pmap) { 1584 pmap_evcnt(atc_one_user); 1585 fc = FC_USERD; 1586 } else { 1587 pmap_evcnt(atc_one_skipped); 1588 continue; 1589 } 1590 if (fc != prev_fc) { 1591 pmap_evcnt(atc_dfc_switch); 1592 setdfc(fc); 1593 prev_fc = fc; 1594 } 1595 pflush40(ATC_DAT_VA(atc, i)); 1596 } 1597 if (prev_fc != FC_USERD) { 1598 pmap_evcnt(atc_dfc_switch); 1599 setdfc(FC_USERD); 1600 } 1601 } 1602 #if defined(M68060) 1603 if (cputype == M68060) { 1604 int which; 1605 1606 if (atc->atc_flags & ATC_F_SUPER) { 1607 pmap_evcnt(atc_bc60inv_all); 1608 which = IC60_CABC; 1609 } else { 1610 pmap_evcnt(atc_bc60inv_user); 1611 which = IC60_CUBC; 1612 } 1613 bc60_inval(which); 1614 } 1615 #endif 1616 } 1617 #endif /* MMU_CONFIG_68040_CLASS */ 1618 1619 static void 1620 pmap_atc_invalidate(pmap_t pmap, vaddr_t va, paddr_t pa, uint16_t flags) 1621 { 1622 struct pmap_atc *atc = &pmap_percpu()->pc_atc; 1623 int i; 1624 1625 #ifdef PMAP_EVENT_COUNTERS 1626 if (__predict_false(flags & ATC_F_PV)) { 1627 pmap_evcnt(atc_pv); 1628 } 1629 if (flags & ATC_F_K) { 1630 pmap_evcnt(atc_k); 1631 } 1632 if (flags & ATC_F_ENTER) { 1633 pmap_evcnt(atc_enter); 1634 } 1635 if (flags & ATC_F_REMOVE) { 1636 pmap_evcnt(atc_remove); 1637 } 1638 if (flags & ATC_F_PROT) { 1639 pmap_evcnt(atc_prot); 1640 } 1641 #endif 1642 1643 if (__predict_true(pmap != NULL)) { 1644 flags |= (pmap == pmap_kernel() ? ATC_F_SUPER : ATC_F_USER); 1645 } 1646 1647 if ((i = atc->atc_count) == ATC_MAXVA || (flags & ATC_F_ALL) != 0) { 1648 #ifdef PMAP_EVENT_COUNTERS 1649 if ((atc->atc_flags & ATC_F_ALL) == 0) { 1650 if (atc->atc_count == ATC_MAXVA) { 1651 pmap_evcnt(atc_req_saturate); 1652 } else if (atc->atc_count != ATC_ALLVA) { 1653 pmap_evcnt(atc_req_all); 1654 } 1655 } 1656 #endif 1657 if (flags & ATC_F_ALL) { 1658 atc->atc_count = ATC_ALLVA; 1659 } 1660 atc->atc_flags |= flags | ATC_F_ALL; 1661 return; 1662 } 1663 1664 atc->atc_count++; 1665 atc->atc_flags |= flags; 1666 atc->atc_data[i].atc_pmap = pmap; 1667 atc->atc_data[i].atc_addrflags = va | flags; 1668 atc->atc_data[i].atc_pa = pa; 1669 } 1670 1671 static inline void 1672 pmap_atc_invalidate_all(uint16_t flags) 1673 { 1674 KASSERT((flags & (ATC_F_USER | ATC_F_SUPER)) != 0); 1675 pmap_atc_invalidate(NULL, 0, 0, flags | ATC_F_ALL); 1676 } 1677 1678 static inline void 1679 pmap_atc_invalidate_pv(struct pv_entry *pv, paddr_t pa, uint16_t atc_flags) 1680 { 1681 /* 1682 * The ATC is flushed on each context switch, so if this is not 1683 * the currently loaded pmap, we don't have to do any work. 1684 * 1685 * Considerations: 1686 * - 68030's D-cache is flushed on context switch. 1687 * 1688 * - HP MMU VAC is also flushed on context switch. 1689 * 1690 * - 68040 and 68060 D-cache is physically addressed 1691 * and might need to be hit here. 1692 */ 1693 if (__predict_true(pv->pv_pmap != pmap_kernel() && 1694 pv->pv_pmap != pmap_percpu()->pc_pmap)) { 1695 #if MMU_CONFIG_68040_CLASS 1696 /* push the page, if needed. */ 1697 if (MMU_IS_68040_CLASS) { 1698 if (atc_flags & ATC_F_DC) { 1699 pmap_evcnt(atc_pv_cpushp); 1700 cpushp_dc40(pa); 1701 } 1702 if (atc_flags & ATC_F_IC) { 1703 pmap_evcnt(atc_pv_cinvp); 1704 cinvp_ic40(pa); 1705 } 1706 } 1707 #endif 1708 pmap_evcnt(atc_pv_skipped); 1709 return; 1710 } 1711 pmap_atc_invalidate(pv->pv_pmap, PV_VA(pv), pa, atc_flags | ATC_F_PV); 1712 } 1713 1714 static inline void 1715 pmap_atc_process(void) 1716 { 1717 struct pmap_atc *atc = &pmap_percpu()->pc_atc; 1718 1719 #ifdef PMAP_EVENT_COUNTERS 1720 switch (atc->atc_count) { 1721 case 0: pmap_evcnt(atc_0); break; 1722 case 1: pmap_evcnt(atc_1); break; 1723 case 2: pmap_evcnt(atc_2); break; 1724 case 3: pmap_evcnt(atc_3); break; 1725 case 4: pmap_evcnt(atc_4); break; 1726 case 5: pmap_evcnt(atc_5); break; 1727 case 6: pmap_evcnt(atc_6); break; 1728 case 7: pmap_evcnt(atc_7); break; 1729 case 8: pmap_evcnt(atc_8); break; 1730 default: break; 1731 } 1732 #endif 1733 1734 if (atc->atc_count) { 1735 (*pmap_atc_process_local_func)(); 1736 atc->atc_count = 0; 1737 } 1738 atc->atc_flags = 0; 1739 } 1740 1741 #if 0 1742 static bool 1743 pmap_atc_check(void) 1744 { 1745 struct pmap_atc *atc = &pmap_percpu()->pc_atc; 1746 1747 if (__predict_true(atc->atc_count == 0)) { 1748 return true; 1749 } 1750 1751 printf("%s: atc_count=%u atc_flags=0x%x\n", __func__, 1752 atc->atc_count, atc->atc_flags); 1753 for (int i = 0; i < atc->atc_count; i++) { 1754 printf("\t%d: pmap=%p%s\n", i, atc->atc_data[i].atc_pmap, 1755 atc->atc_data[i].atc_pmap == pmap_kernel() ? " (k)" : ""); 1756 printf("\t va=0x%lx\n", ATC_DAT_VA(atc, i)); 1757 printf("\t flags=0x%lx\n", 1758 atc->atc_data[i].atc_addrflags & PAGE_MASK); 1759 printf("\t pa=0x%lx\n", atc->atc_data[i].atc_pa); 1760 } 1761 return false; 1762 } 1763 #else 1764 #define pmap_atc_check() true 1765 #endif 1766 1767 /************************ PTE MANIPULATION HELPERS ***************************/ 1768 1769 /* Assert assumptions made in <machine/pmap.h>. */ 1770 __CTASSERT(DT51_PAGE == PTE40_RESIDENT); 1771 __CTASSERT(PTE51_WP == PTE40_W); 1772 __CTASSERT(PTE51_U == PTE40_U); 1773 __CTASSERT(PTE51_M == PTE40_M); 1774 __CTASSERT(PTE51_CI == PTE40_CM_NC_SER); 1775 __CTASSERT(PMBM_F_U0 == PTE40_U0); 1776 __CTASSERT(PMBM_F_U1 == PTE40_U1); 1777 1778 static pt_entry_t pmap_pte_proto[UVM_PROT_ALL + 1] __read_mostly; 1779 static pt_entry_t pmap_pte_proto_ci[UVM_PROT_ALL + 1] __read_mostly; 1780 static pt_entry_t pmap_pte_proto_cwt[UVM_PROT_ALL + 1] __read_mostly; 1781 static pt_entry_t pmap_pte_proto_um[UVM_PROT_ALL + 1] __read_mostly; 1782 static pt_entry_t pmap_ste_proto __read_mostly; 1783 1784 static inline paddr_t 1785 pte_pa(pt_entry_t pte) 1786 { 1787 return pte & PTE40_PGA; 1788 } 1789 1790 /* 1791 * These predicate inlines compile down into BFEXTU, so are quite fast. 1792 */ 1793 1794 static inline bool 1795 pte_valid_p(pt_entry_t pte) 1796 { 1797 return !!(pte & PTE_VALID); 1798 } 1799 1800 static inline bool 1801 pte_wired_p(pt_entry_t pte) 1802 { 1803 return !!(pte & PTE_WIRED); 1804 } 1805 1806 static inline bool 1807 pte_managed_p(pt_entry_t pte) 1808 { 1809 return !!(pte & PTE_PVLIST); 1810 } 1811 1812 static inline bool 1813 pte_ci_p(pt_entry_t pte) 1814 { 1815 /* 1816 * Happily, PTE51_CI is bit 6, which is set for both of the 1817 * cache-inhibited modes on 68040, so we can just check for 1818 * that. 1819 */ 1820 return !!(pte & PTE51_CI); 1821 } 1822 1823 #define PTE_PROT_CHANGE_BITS (PTE_WP | PTE_CMASK) 1824 1825 static inline pt_entry_t 1826 pte_change_prot(pt_entry_t opte, vm_prot_t prot) 1827 { 1828 pt_entry_t *pte_proto = pte_ci_p(opte) ? pmap_pte_proto_ci 1829 : pmap_pte_proto; 1830 1831 return (opte & ~PTE_PROT_CHANGE_BITS) | pte_proto[prot]; 1832 } 1833 1834 static inline pt_entry_t 1835 pte_load(volatile pt_entry_t *ptep) 1836 { 1837 return *ptep; 1838 } 1839 1840 static inline void 1841 pte_store(volatile pt_entry_t *ptep, pt_entry_t npte) 1842 { 1843 *ptep = npte; 1844 } 1845 1846 /* 1847 * Don't inline the CAS.L instruction; some systems have non-working 1848 * READ-MODIFY-WRITE cycle logic. This will ensure that we'll use 1849 * restartable atomic sequence, if required. 1850 * 1851 * AND.L and OR.L don't use the RMC signal, so they aren't subject 1852 * to the same constraints. 1853 */ 1854 static inline pt_entry_t 1855 pte_update(volatile pt_entry_t *ptep, pt_entry_t opte, pt_entry_t npte) 1856 { 1857 /* 1858 * Use compare-and-swap to update the PTE. This ensures there's 1859 * no possibility of losing any hardware-maintained bits when 1860 * updating the PTE. 1861 */ 1862 #if !defined(__HAVE_M68K_BROKEN_RMC) 1863 __asm __volatile("casl %1,%2,%0" 1864 : "+m" (*ptep), "+d" (opte) 1865 : "d" (npte) 1866 : "cc", "memory"); 1867 return opte; 1868 #else 1869 return atomic_cas_uint(ptep, opte, npte); 1870 #endif 1871 } 1872 1873 #if MMU_CONFIG_HP_CLASS 1874 /* 1875 * These are only used for HP MMU VAC shenanigans. There is no need 1876 * for these to be truly atomic, and systems with an HP MMU can't do 1877 * truly atomic operations anyway. 1878 */ 1879 static inline void 1880 pte_set(volatile pt_entry_t *ptep, pt_entry_t bits) 1881 { 1882 *ptep |= bits; 1883 } 1884 1885 static inline void 1886 pte_mask(volatile pt_entry_t *ptep, pt_entry_t mask) 1887 { 1888 *ptep &= mask; 1889 } 1890 #endif /* MMU_CONFIG_HP_CLASS */ 1891 1892 static inline pt_entry_t 1893 pte_set_ci(pt_entry_t pte) 1894 { 1895 return (pte & ~PTE_CMASK) | (MMU_IS_68040_CLASS ? PTE40_CM_NC_SER 1896 : PTE51_CI); 1897 } 1898 1899 static inline pt_entry_t 1900 pte_clr_ci(pt_entry_t pte) 1901 { 1902 pte &= ~PTE_CMASK; 1903 if (MMU_IS_68040_CLASS) { 1904 pte |= (pte & PTE_WP) ? PTE40_CM_WT 1905 : PTE40_CM_CB; 1906 } 1907 return pte; 1908 } 1909 1910 static void 1911 pmap_pte_proto_init(void) 1912 { 1913 pt_entry_t c_bits, rw_c_bits, ci_bits, prot_bits, um_bits; 1914 int prot; 1915 1916 /* 1917 * N.B. PTE40_CM_WT == 0, so "write-though" caching has the same 1918 * PTE bits on all variants of the MMU. 1919 */ 1920 1921 if (MMU_IS_68040_CLASS) { 1922 rw_c_bits = PTE40_CM_CB; 1923 ci_bits = PTE40_CM_NC_SER; 1924 } else { 1925 rw_c_bits = 0; 1926 ci_bits = PTE51_CI; 1927 } 1928 1929 for (prot = 1; prot <= UVM_PROT_ALL; prot++) { 1930 prot_bits = um_bits = 0; 1931 if (prot & UVM_PROT_WRITE) { 1932 um_bits = PTE_U | PTE_M; 1933 } else if (prot & (UVM_PROT_READ|UVM_PROT_EXEC)) { 1934 prot_bits = PTE_WP; 1935 um_bits = PTE_U; 1936 } 1937 c_bits = (prot & UVM_PROT_WRITE) ? rw_c_bits : 0; 1938 pmap_pte_proto[prot] = PTE_VALID | prot_bits | c_bits; 1939 pmap_pte_proto_ci[prot] = PTE_VALID | prot_bits | ci_bits; 1940 pmap_pte_proto_cwt[prot] = PTE_VALID | prot_bits; 1941 pmap_pte_proto_um[prot] = um_bits; 1942 } 1943 1944 /* 1945 * from hp300/DOC/HPMMU.notes: 1946 * 1947 * Segment table entries: 1948 * 1949 * bits 31-12: Physical page frame number of PT page 1950 * bits 11-4: Reserved at zero (can software use them?) 1951 * bit 3: Reserved at one 1952 * bits 1-0: Valid bits (hardware uses bit 1) 1953 * 1954 * This is all roughly compatible with 68851 and 68040: 1955 * 1956 * bit 3: DTE51_U / UTE40_U (used) 1957 * bits 1-0: DT51_SHORT / UTE40_RESIDENT 1958 * 1959 * The Hibler pmap set "SG_U" in the 68040 case, but not in 1960 * any others (??), which seems at odds with HPMMU.notes, but 1961 * whatever. It does not seem to cause any harm to set the 1962 * "used" bit in all cases, so that's what we'll do. If it 1963 * does prove to be problematic, we can make adjustments. 1964 */ 1965 pmap_ste_proto = DTE51_U | DT51_SHORT; 1966 } 1967 1968 static inline pt_entry_t 1969 pmap_make_pte(paddr_t pa, vm_prot_t prot, u_int flags) 1970 { 1971 pt_entry_t *pte_proto; 1972 1973 switch (flags & PMAP_CACHE_MASK) { 1974 case PMAP_NOCACHE: 1975 case PMAP_WRITE_COMBINE: 1976 case PMAP_NOCACHE_OVR: 1977 pte_proto = pmap_pte_proto_ci; 1978 break; 1979 1980 case PMAP_WRITE_THROUGH: 1981 pte_proto = pmap_pte_proto_cwt; 1982 break; 1983 1984 default: 1985 pte_proto = pmap_pte_proto; 1986 break; 1987 } 1988 1989 prot &= UVM_PROT_ALL; 1990 KASSERT(prot != 0); 1991 1992 /* 1993 * N.B. We seed the U and M bits in the PTE based on the access 1994 * that was used to produce the page fault because the MMU will 1995 * skip the write-back of a PTE upon access if the copy in the 1996 * ATC already has the bit set. 1997 */ 1998 pt_entry_t npte = pa | pte_proto[prot] | 1999 pmap_pte_proto_um[flags & UVM_PROT_ALL]; 2000 2001 if (flags & PMAP_WIRED) { 2002 npte |= PTE_WIRED; 2003 } 2004 2005 return npte; 2006 } 2007 2008 /************************** PAGE TABLE MANAGEMENT ****************************/ 2009 2010 /* 2011 * Kernel page table management works differently from user page table 2012 * management. An initial set of kernel PTs are allocated during early 2013 * bootstrap (enough to map the virtual addresses set up at that time, 2014 * plus a little extra to give the kernel some breathing room while 2015 * UVM gets initialized -- see pmap_bootstrap1()). If more PTs are 2016 * needed in order to expand the kernel address space, pmap_growkernel() 2017 * is called to allocate some more. We always allocate kernel PTs in 2018 * chunks of one page, allocating more inner segment tables as needed 2019 * to link them into the MMU tree (3-level), or just poking them in 2020 * directly to the level-1 table (2-level). 2021 * 2022 * The kernel PTs are mapped into a single linear array to make that 2023 * makes it possible to simply index by virtual page number to find 2024 * the PTE that maps that virtual address. 2025 */ 2026 #define PTPAGEVASZ ((PAGE_SIZE / sizeof(pt_entry_t)) * PAGE_SIZE) 2027 #define PTPAGEVAOFS (PTPAGEVASZ - 1) 2028 2029 #define pmap_round_ptpage(va) (((va) + PTPAGEVAOFS) & ~PTPAGEVAOFS) 2030 #define pmap_trunc_ptpage(va) ((va) & ~PTPAGEVAOFS) 2031 2032 /* 2033 * kernel_virtual_start marks the first kernel virtual address that 2034 * is handed off to UVM to manage. kernel_virtual_end marks the end 2035 * of the kernel address space that is currently mappable with the 2036 * number of pages allocated to kernel PTs. 2037 * 2038 * kernel_virtual_start is fixed once pmap_bootstrap1() completes. 2039 * kernel_virtual_end can be extended by calling pmap_growkernel(). 2040 * 2041 * kernel_virtual_max represents the absolute maximum. It starts at 2042 * KERNEL_MAX_ADDRESS, but may get clamped by fixed mappings that 2043 * start beyond the end of kernel virtual address space. 2044 * 2045 * kernel_virtual_max is exported to the rest of the kernel via 2046 * pmap_virtual_space() and VM_MAX_KERNEL_ADDRESS. 2047 */ 2048 #define KERNEL_MAX_ADDRESS ((vaddr_t)0 - PAGE_SIZE) 2049 static vaddr_t kernel_virtual_start __read_mostly; 2050 static vaddr_t kernel_virtual_end __read_mostly; 2051 vaddr_t kernel_virtual_max __read_mostly = KERNEL_MAX_ADDRESS; 2052 2053 /* 2054 * kernel_stnext_pa and kernel_stnext_endpa together implement a 2055 * simple allocator for inner segment tables used in the 3-level 2056 * configuration. When the initial level-1 table is allocated 2057 * the remained of that page is set in kernel_stnext_pa, and 2058 * kernel_stnext_endpa is set to the next page boundary. When 2059 * a segment table is needed, kernel_stnext_pa is the address 2060 * of the next free table and is advanced by the L2 table size 2061 * (512 bytes). If that allocation attempt finds that kernel_stnext_pa 2062 * is equal to kernel_stnext_endpa, a new page is allocated and 2063 * kernel_stnext_pa and kernel_stnext_endpa updated to reflect 2064 * the newly-allocated page before the table is taken from it. 2065 */ 2066 static paddr_t kernel_stnext_pa __read_mostly; 2067 static paddr_t kernel_stnext_endpa __read_mostly; 2068 2069 /* 2070 * Null segment table that every pmap gets as its initial level 1 2071 * map. This is a single page allocated in pmap_bootstrap1(), and 2072 * we zero it out in pmap_init(). 2073 */ 2074 static paddr_t null_segtab_pa __read_mostly; 2075 2076 /* 2077 * pmap_load_urp: 2078 * 2079 * Load the user root table into the MMU. 2080 */ 2081 static inline void 2082 pmap_load_urp(paddr_t urp) 2083 { 2084 pmap_percpu()->pc_lev1pa = urp; 2085 (*pmap_load_urp_func)(urp); 2086 } 2087 2088 static inline void 2089 pmap_set_lev1map(pmap_t pmap, struct pmap_table *pt, paddr_t pa) 2090 { 2091 2092 KASSERT(PMAP_LOCK_OWNED(pmap)); 2093 2094 /* 2095 * Make sure this is a reasonable transition. There is a 2096 * situation where pmap points to null_segtab_pa and we're 2097 * asked to set null_segtab_pa. This can happen if the 2098 * very first thing that a process does is take a signal 2099 * that causes process exit: 2100 * 2101 * child_return -> userret -> postsig -> sigexit -> 2102 * uvm_proc_exit -> uvmspace_free -> pmap_remove_all 2103 * 2104 * Otherwise, only to/from null_segtab_pa are allowed. 2105 */ 2106 if (__predict_false(pmap->pm_lev1pa == pa)) { 2107 KASSERT(pa == null_segtab_pa); 2108 pmap_evcnt(lev1map_set_same); 2109 return; 2110 } 2111 KASSERT((pmap->pm_lev1pa == null_segtab_pa && pa != null_segtab_pa) 2112 || (pmap->pm_lev1pa != null_segtab_pa && pa == null_segtab_pa)); 2113 2114 KASSERT((pa == null_segtab_pa && pt == NULL) 2115 || (pa != null_segtab_pa && pt != NULL)); 2116 2117 pmap->pm_lev1map = pt; 2118 pmap->pm_lev1pa = pa; 2119 2120 #if defined(MULTIPROCESSOR) 2121 /* 2122 * We only send an UNLOAD message to remote CPUs when we're 2123 * transitioning to null_segtab; pmap_activate() or pmap_fault() 2124 * will handle the transition from null_segtab when necessary. 2125 */ 2126 uint32_t others = (pa == null_segtab_pa) ? pmap_cpu_others(pmap) : 0; 2127 struct pmap_message pmsg = { 2128 .pmsg_cpus = others, 2129 .pmsg_type = PMSG_T_UNLOAD, 2130 .pmsg_pmap = pmap, 2131 }; 2132 2133 if (others) { 2134 pmap_message_user_acquire(&pmsg); 2135 pmap_message_user_send(&pmsg); 2136 /* Do our work in parallel with others. */ 2137 } 2138 #endif 2139 2140 if (pmap == pmap_percpu()->pc_pmap) { 2141 /* 2142 * The MMU-specific load-urp functions take care of 2143 * any necessary ATC / cache invalidations. 2144 */ 2145 pmap_load_urp(pmap->pm_lev1pa); 2146 } 2147 2148 #if defined(MULTIPROCESSOR) 2149 if (others) { 2150 /* wait for others to be finished */ 2151 pmap_message_user_wait(&pmsg); 2152 } 2153 #endif 2154 } 2155 2156 /* 2157 * Table accessors. 2158 */ 2159 static inline unsigned int 2160 pmap_pagenum(vaddr_t va) 2161 { 2162 return ((va) >> PAGE_SHIFT); 2163 } 2164 2165 static inline unsigned int 2166 pmap_segnum(vaddr_t va) 2167 { 2168 return MMU_USE_3L ? ((va) >> SEGSHIFT3L) : ((va) >> SEGSHIFT2L); 2169 } 2170 2171 static inline unsigned int 2172 pmap_st1_index(vaddr_t va) 2173 { 2174 return MMU_USE_3L ? LA40_RI(va) : LA2L_RI(va); 2175 } 2176 2177 static inline unsigned int 2178 pmap_st_index(vaddr_t va) 2179 { 2180 return MMU_USE_3L ? LA40_PI(va) : LA2L_RI(va); 2181 } 2182 2183 static inline unsigned int 2184 pmap_pt_index(vaddr_t va) 2185 { 2186 return MMU_USE_3L ? LA40_PGI(va) : LA2L_PGI(va); 2187 } 2188 2189 static inline vaddr_t 2190 pmap_trunc_seg(vaddr_t va) 2191 { 2192 return MMU_USE_3L ? pmap_trunc_seg_3L(va) : pmap_trunc_seg_2L(va); 2193 } 2194 2195 static inline vaddr_t 2196 pmap_trunc_seg1(vaddr_t va) 2197 { 2198 KASSERT(MMU_USE_3L); 2199 return pmap_trunc_seg1_3L(va); 2200 } 2201 2202 static inline vaddr_t 2203 pmap_round_seg(vaddr_t va) 2204 { 2205 return MMU_USE_3L ? pmap_round_seg_3L(va) : pmap_round_seg_2L(va); 2206 } 2207 2208 static inline vaddr_t 2209 pmap_next_seg(vaddr_t va) 2210 { 2211 return pmap_round_seg(va + PAGE_SIZE); 2212 } 2213 2214 static paddr_t 2215 pmap_table_pa(const struct pmap_table * const pt) 2216 { 2217 const struct pmap_ptpage * const ptp = pt->pt_ptpage; 2218 const vaddr_t ptpva = m68k_ptob(ptp->ptp_vpagenum); 2219 const vaddr_t ptva = (vaddr_t)pt->pt_entries; 2220 2221 return VM_PAGE_TO_PHYS(ptp->ptp_pg) + (ptva - ptpva); 2222 } 2223 2224 static inline unsigned int 2225 pmap_table_make_key(unsigned int segnum, bool segtab) 2226 { 2227 KASSERT((segnum & 0x80000000) == 0); 2228 return (segnum << 1) | (unsigned int)segtab; 2229 } 2230 2231 static int 2232 pmap_table_rb_compare_key(void *v __unused, const void *n, const void *k) 2233 { 2234 const struct pmap_table * const pt1 = n; 2235 const unsigned int k1 = pt1->pt_key; 2236 const unsigned int k2 = *(const unsigned int *)k; 2237 2238 return (int)(k1 - k2); 2239 } 2240 2241 static int 2242 pmap_table_rb_compare_nodes(void *v, const void *n1, const void *n2) 2243 { 2244 const struct pmap_table * const pt2 = n2; 2245 2246 return pmap_table_rb_compare_key(v, n1, &pt2->pt_key); 2247 } 2248 2249 static const rb_tree_ops_t pmap_table_rb_ops = { 2250 .rbto_compare_nodes = pmap_table_rb_compare_nodes, 2251 .rbto_compare_key = pmap_table_rb_compare_key, 2252 .rbto_node_offset = offsetof(struct pmap_table, pt_node), 2253 }; 2254 2255 static struct pmap_table * 2256 pmap_table_alloc(pmap_t pmap, bool segtab, bool nowait) 2257 { 2258 struct pmap_ptpage_list *pmlist = &pmap->pm_ptpages[segtab]; 2259 struct pmap_ptpage *ptp, *newptp = NULL; 2260 struct pmap_table *pt; 2261 2262 try_again: 2263 if ((ptp = TAILQ_FIRST(pmlist)) == NULL || ptp->ptp_freecnt == 0) { 2264 /* 2265 * No PT pages with free tables (empty PT pages are moved 2266 * to the tail of the list). Allocate a new PT page and 2267 * try again. If someone else successfully allocates one 2268 * while we're sleeping, then we'll use it and free what 2269 * we allocated back to the system. 2270 */ 2271 KASSERT(ptp == NULL || LIST_FIRST(&ptp->ptp_freelist) == NULL); 2272 if (newptp == NULL) { 2273 /* 2274 * N.B. pmap_ptpage_alloc() will temporarily exit 2275 * the critical section. 2276 */ 2277 newptp = pmap_ptpage_alloc(pmap, segtab, nowait); 2278 if (newptp == NULL) { 2279 /* 2280 * If we didn't wait, then no one would 2281 * have allocated one behind our back. 2282 */ 2283 KASSERT(nowait); 2284 return NULL; 2285 } 2286 goto try_again; 2287 } 2288 ptp = newptp; 2289 TAILQ_INSERT_HEAD(pmlist, newptp, ptp_list); 2290 } 2291 if (__predict_false(newptp != NULL && ptp != newptp)) { 2292 /* Not using newly-allocated PT page; free it back. */ 2293 TAILQ_INSERT_TAIL(&pmap->pm_garbage.g_ptpages, newptp, 2294 ptp_list); 2295 } 2296 pt = LIST_FIRST(&ptp->ptp_freelist); 2297 KASSERT(pt != NULL); 2298 LIST_REMOVE(pt, pt_freelist); 2299 ptp->ptp_freecnt--; 2300 if (ptp->ptp_freecnt == 0 && 2301 TAILQ_NEXT(ptp, ptp_list) != NULL) { 2302 TAILQ_REMOVE(pmlist, ptp, ptp_list); 2303 TAILQ_INSERT_TAIL(pmlist, ptp, ptp_list); 2304 } 2305 KASSERT(pt->pt_st == NULL); 2306 pt->pt_holdcnt = 1; 2307 2308 return pt; 2309 } 2310 2311 static void 2312 pmap_table_free(pmap_t pmap, struct pmap_table *pt, struct pmap_garbage *g) 2313 { 2314 struct pmap_ptpage *ptp = pt->pt_ptpage; 2315 struct pmap_ptpage_list *pmlist = &pmap->pm_ptpages[ptp->ptp_segtab]; 2316 2317 KASSERT(pt->pt_st == NULL); 2318 2319 LIST_INSERT_HEAD(&ptp->ptp_freelist, pt, pt_freelist); 2320 KASSERT(ptp->ptp_freecnt < pmap_ptpage_table_counts[ptp->ptp_segtab]); 2321 ptp->ptp_freecnt++; 2322 2323 /* 2324 * If the PT page no longer has any active tables, then 2325 * remove it from the pmap and queue it up to be given 2326 * back to the system. 2327 */ 2328 if (ptp->ptp_freecnt == pmap_ptpage_table_counts[ptp->ptp_segtab]) { 2329 TAILQ_REMOVE(pmlist, ptp, ptp_list); 2330 TAILQ_INSERT_TAIL(&g->g_ptpages, ptp, ptp_list); 2331 } 2332 /* 2333 * If the PT page now has exactly one free table, then 2334 * put it at the head of its list so that it is allocated 2335 * from first the next time a table is needed. 2336 */ 2337 else if (ptp->ptp_freecnt == 1) { 2338 TAILQ_REMOVE(pmlist, ptp, ptp_list); 2339 TAILQ_INSERT_HEAD(pmlist, ptp, ptp_list); 2340 } 2341 /* 2342 * Push this PT page down the list if it has more free tables 2343 * than the ones that come after. The goal is to keep PT pages 2344 * with the fewest free tables at the head of the list so that 2345 * they're allocated from first. This is an effort to keep 2346 * fragmentation at bay so as to increase the likelihood that 2347 * we can free PT pages back to the system. 2348 */ 2349 else { 2350 struct pmap_ptpage *next_ptp; 2351 for (next_ptp = TAILQ_NEXT(ptp, ptp_list); 2352 next_ptp != NULL; 2353 next_ptp = TAILQ_NEXT(next_ptp, ptp_list)) { 2354 if (next_ptp->ptp_freecnt < ptp->ptp_freecnt) { 2355 break; 2356 } 2357 } 2358 if (next_ptp != NULL && 2359 next_ptp != TAILQ_NEXT(ptp, ptp_list) && 2360 next_ptp->ptp_freecnt != 0) { 2361 TAILQ_REMOVE(pmlist, ptp, ptp_list); 2362 TAILQ_INSERT_AFTER(pmlist, next_ptp, ptp, ptp_list); 2363 } 2364 } 2365 } 2366 2367 /* 2368 * pmap_table_retain: 2369 * 2370 * Take a retain count on the specified table. Retain counts 2371 * are used to ensure the table remains stable while working 2372 * on it, and each mapping placed into the table also gets 2373 * a retain count. 2374 */ 2375 static inline void 2376 pmap_table_retain(struct pmap_table *pt) 2377 { 2378 if (__predict_true(pt != NULL)) { 2379 pt->pt_holdcnt++; 2380 KASSERT(pt->pt_holdcnt != 0); 2381 } 2382 } 2383 2384 /* 2385 * pmap_table_release: 2386 * 2387 * Release a previously-taken retain count on the specified 2388 * table. If the retain count drops to zero, the table is 2389 * unlinked from the lookup tree and the MMU tree and freed. 2390 */ 2391 static __noinline void 2392 pmap_table_release_slow(pmap_t pmap, struct pmap_table *pt, 2393 struct pmap_garbage *g) 2394 { 2395 KASSERT(pt != NULL); 2396 KASSERT(pt->pt_holdcnt != 0); 2397 pt->pt_holdcnt--; 2398 if (__predict_false(pt->pt_holdcnt != 0)) { 2399 return; 2400 } 2401 2402 /* 2403 * If the caller doesn't expect the count to go to zero, 2404 * they won't have bothered with a completion context. 2405 * Going to zero is unexpected in this case, so blow up 2406 * if it happens. 2407 */ 2408 KASSERT(g != NULL); 2409 if (__predict_true(pt == pmap->pm_pt_cache)) { 2410 pmap->pm_pt_cache = NULL; 2411 } 2412 if (__predict_true(pt->pt_st != NULL)) { 2413 /* 2414 * This table needs to be unlinked from the lookup 2415 * tree and the MMU tree. 2416 */ 2417 pte_store(&pt->pt_st->pt_entries[pt->pt_stidx], 0); 2418 rb_tree_remove_node(&pmap->pm_tables, pt); 2419 pmap_table_release_slow(pmap, pt->pt_st, g); 2420 pt->pt_st = NULL; 2421 } else if (pt == pmap->pm_lev1map) { 2422 pmap_set_lev1map(pmap, NULL, null_segtab_pa); 2423 } 2424 pmap_table_free(pmap, pt, g); 2425 } 2426 2427 static inline void 2428 pmap_table_release(pmap_t pmap, struct pmap_table *pt, 2429 struct pmap_garbage *g) 2430 { 2431 if (__predict_true(pt != NULL)) { 2432 if (__predict_true(pt->pt_holdcnt > 1)) { 2433 pt->pt_holdcnt--; 2434 return; 2435 } 2436 pmap_table_release_slow(pmap, pt, g); 2437 } 2438 } 2439 2440 /* 2441 * pmap_table_lookup: 2442 * 2443 * Lookup the table corresponding to the specified segment. 2444 */ 2445 static struct pmap_table * 2446 pmap_table_lookup(pmap_t pmap, unsigned int segnum, bool segtab) 2447 { 2448 const unsigned int key = pmap_table_make_key(segnum, segtab); 2449 struct pmap_table *pt; 2450 2451 if ((pt = pmap->pm_pt_cache) == NULL || pt->pt_key != key) { 2452 pmap_evcnt(pt_cache_miss); 2453 pt = rb_tree_find_node(&pmap->pm_tables, &key); 2454 if (__predict_true(!segtab)) { 2455 pmap->pm_pt_cache = pt; 2456 } 2457 } else { 2458 pmap_evcnt(pt_cache_hit); 2459 } 2460 if (pt != NULL) { 2461 pmap_table_retain(pt); 2462 } 2463 return pt; 2464 } 2465 2466 /* 2467 * pmap_table_insert: 2468 * 2469 * Allocate and insert a table into the tree at the specified 2470 * location. 2471 */ 2472 static struct pmap_table * 2473 pmap_table_insert(pmap_t pmap, struct pmap_table *t1, unsigned int stidx, 2474 unsigned int segnum, bool segtab, bool nowait) 2475 { 2476 struct pmap_table *t2, *ret_t; 2477 2478 t2 = pmap_table_lookup(pmap, segnum, segtab); 2479 if (t2 != NULL) { 2480 /* 2481 * Table at this level already exists, and looking 2482 * it up gave us a retain count, so we no longer need 2483 * the retain count on the upper level table (it is 2484 * retained-by-proxy by the table we just found). 2485 * We pass NULL for the completion context because 2486 * we don't expect the upper level table's retain count 2487 * to drop to zero, and we want things to blow up 2488 * loudly if it does! 2489 */ 2490 pmap_table_release(pmap, t1, NULL); 2491 return t2; 2492 } 2493 2494 /* 2495 * Allocate the new table. 2496 * 2497 * N.B. pmap_table_alloc() will temporarily exit the critical section. 2498 */ 2499 t2 = pmap_table_alloc(pmap, segtab, nowait); 2500 if (__predict_false(t2 == NULL)) { 2501 pmap_table_release(pmap, t1, &pmap->pm_garbage); 2502 return NULL; 2503 } 2504 t2->pt_key = pmap_table_make_key(segnum, segtab); 2505 2506 /* 2507 * Now that we have the new table, we need to insert it into the 2508 * table lookup tree. If we blocked while allocating, it's possible 2509 * someone raced with us and inserted one behind our back, so we need 2510 * to check for that. 2511 */ 2512 ret_t = rb_tree_insert_node(&pmap->pm_tables, t2); 2513 if (__predict_false(ret_t != t2)) { 2514 /* 2515 * Someone beat us to the punch. If this happens, 2516 * then we also need to drop the retain count on 2517 * t1 because the table we just found already has 2518 * a retain count on it. 2519 */ 2520 pmap_table_retain(ret_t); 2521 pmap_table_release(pmap, t2, &pmap->pm_garbage); 2522 pmap_table_release(pmap, t1, NULL); 2523 return ret_t; 2524 } 2525 2526 /* 2527 * Table has been successfully inserted into the lookup 2528 * tree, now link it into the MMU's tree. The new table 2529 * takes ownership of the retain count that was taken on 2530 * the upper level table while working. 2531 */ 2532 t2->pt_st = t1; 2533 t2->pt_stidx = (unsigned short)stidx; 2534 pte_store(&t1->pt_entries[stidx], pmap_ste_proto | pmap_table_pa(t2)); 2535 2536 return t2; 2537 } 2538 2539 /************************* INTERPROCESSOR MESSAGING **************************/ 2540 2541 #if defined(MULTIPROCESSOR) 2542 2543 /* 2544 * User pmap messages can only be sent from thread context, and the 2545 * target pmap lock must be held. 2546 */ 2547 static struct pmap_message * volatile pmap_user_msgp __cacheline_aligned; 2548 2549 static void 2550 pmap_message_user_acquire(struct pmap_message *pmsg) 2551 { 2552 KASSERT(pmsg->pmsg_cpus != 0); 2553 KASSERT(pmsg->pmsg_type <= PMSG_T_user_last); 2554 KASSERT(pmsg->pmsg_pmap != NULL && pmsg->pmsg_pmap != pmap_kernel()); 2555 2556 for (;;) { 2557 void *actual = atomic_cas_ptr(&pmap_user_msgp, NULL, pmsg); 2558 if (actual == NULL) { 2559 return; 2560 } 2561 /* Spin waiting for it to clear. */ 2562 while (atomic_load_relaxed(&pmap_user_msgp) != NULL) { 2563 __nothing; 2564 } 2565 } 2566 } 2567 2568 static void 2569 pmap_message_user_send(struct pmap_message *pmsg) 2570 { 2571 /* XXX */ 2572 } 2573 2574 static void 2575 pmap_message_user_wait(struct pmap_message *pmsg) 2576 { 2577 /* Spin waiting for targets to finish. */ 2578 while (atomic_load_relaxed(&pmsg->pmsg_cpus) != 0) { 2579 __nothing; 2580 } 2581 2582 atomic_store_relaxed(&pmap_user_msgp, NULL); 2583 } 2584 2585 void 2586 pmap_message_user_process(void) 2587 { 2588 struct pmap_percpu *pc = pmap_percpu(); 2589 struct pmap_message *pmsg = atomic_load_relaxed(&pmap_user_msgp); 2590 2591 switch (pmsg->pmsg_type) { 2592 case PMSG_T_UNLOAD: 2593 if (pmsg->pmsg_pmap == pc->pc_pmap) { 2594 pmap_cpu_self_clear(pmsg->pmsg_pmap); 2595 pc->pc_pmap = NULL; 2596 pmap_load_urp(null_segtab_pa); 2597 } 2598 break; 2599 2600 default: 2601 panic("%s: unknown type=%u\n", __func__, pmsg->pmsg_type); 2602 } 2603 } 2604 2605 #endif /* MULTIPROCESSOR */ 2606 2607 /*************************** PTE LOOKUP FUNCTIONS ****************************/ 2608 2609 static pt_entry_t *kernel_ptes __read_mostly; 2610 2611 /* 2612 * pmap_kernel_pte: 2613 * 2614 * Get the PTE that maps the specified kernel virtual address. 2615 * 2616 * Take note: the caller *may assume* they they can linearly 2617 * access adjacent PTEs up until the address indicated by 2618 * virtual_end! That means, "pte++" is totally fine until you 2619 * get to the current limit of the kernel virtual address space! 2620 */ 2621 static pt_entry_t * 2622 pmap_kernel_pte(vaddr_t va) 2623 { 2624 /* 2625 * The kernel PTEs are mapped as a linear array, whose entries 2626 * represent the entire possible 4GB supervisor address space. 2627 * 2628 * Kernel PT pages are pre-allocated and mapped into this linear 2629 * space (via pmap_growkernel(), as needed) and never freed back. 2630 * So, as long as the VA is below virtual_end, we know that a PTE 2631 * exists to back it. 2632 * 2633 * We don't assert that the VA < virtual_end, however; there may 2634 * be special cases where we need to get a PTE that has been 2635 * statically-allocated out beyond where virtual space is allowed 2636 * to grow. We'll find out soon enough if a PT page doesn't back 2637 * it, because a fault will occur when the PTE is accessed. 2638 */ 2639 KASSERT(va >= VM_MIN_KERNEL_ADDRESS); 2640 return &kernel_ptes[m68k_btop(va - VM_MIN_KERNEL_ADDRESS)]; 2641 } 2642 2643 /* 2644 * pmap_pte_lookup: 2645 * 2646 * Lookup the PTE for the given address, returning a retained 2647 * reference to the table containing the PTE. 2648 * 2649 * Take note: the caller *may assume* they they can linearly 2650 * access adjacent PTEs that map addresses within the same 2651 * segment! That means, "pte++" is totally fine until you 2652 * get to the next segment boundary! 2653 */ 2654 static pt_entry_t * 2655 pmap_pte_lookup(pmap_t pmap, vaddr_t va, struct pmap_table **out_pt) 2656 { 2657 if (pmap == pmap_kernel()) { 2658 *out_pt = NULL; 2659 return pmap_kernel_pte(va); 2660 } 2661 2662 const unsigned int segnum = pmap_segnum(va); 2663 2664 struct pmap_table *pt = pmap_table_lookup(pmap, segnum, false); 2665 if (__predict_true(pt != NULL)) { 2666 *out_pt = pt; /* already retained */ 2667 return &pt->pt_entries[pmap_pt_index(va)]; 2668 } 2669 2670 *out_pt = NULL; 2671 return NULL; 2672 } 2673 2674 /* 2675 * pmap_pte_alloc: 2676 * 2677 * Like pmap_pte_lookup(), but allocates tables as necessary. 2678 * 2679 * We enter in a critical section, but may drop that along 2680 * the way and re-validate our own assumptions. Callers 2681 * (pmap_enter(), basically), should be aware of this. 2682 */ 2683 static pt_entry_t * 2684 pmap_pte_alloc(pmap_t pmap, vaddr_t va, struct pmap_table **out_pt, bool nowait) 2685 { 2686 struct pmap_table *st, *pt; 2687 pt_entry_t *ptep; 2688 2689 KASSERT(PMAP_LOCK_OWNED(pmap)); 2690 2691 ptep = pmap_pte_lookup(pmap, va, out_pt); 2692 if (__predict_true(ptep != NULL)) { 2693 return ptep; 2694 } 2695 2696 /* 2697 * First get a reference on the top-level segment table and 2698 * retain it so that it's stable while we work. 2699 */ 2700 if (__predict_true((st = pmap->pm_lev1map) != NULL)) { 2701 pmap_table_retain(st); 2702 } else { 2703 /* 2704 * Oh look! Baby pmap's first mapping! Allocate 2705 * a segment table. 2706 * 2707 * N.B. pmap_table_alloc() will temporarily exit the 2708 * critical section. 2709 */ 2710 st = pmap_table_alloc(pmap, true/*segtab*/, nowait); 2711 if (__predict_false(st == NULL)) { 2712 return NULL; 2713 } 2714 2715 /* Re-validate that we still need the segment table. */ 2716 if (__predict_false(pmap->pm_lev1map != NULL)) { 2717 /* Raced and lost. */ 2718 pmap_table_release(pmap, st, &pmap->pm_garbage); 2719 st = pmap->pm_lev1map; 2720 pmap_table_retain(st); 2721 } else { 2722 /* New table is returned to us retained. */ 2723 pmap_set_lev1map(pmap, st, pmap_table_pa(st)); 2724 } 2725 } 2726 2727 /* 2728 * Now we know that st points to a valid segment table with a 2729 * retain count that lets us safely reference it. 2730 */ 2731 2732 if (MMU_USE_3L) { 2733 /* Get the inner segment table for this virtual address. */ 2734 struct pmap_table * const st1 = st; 2735 st = pmap_table_insert(pmap, st1, pmap_st1_index(va), 2736 pmap_st1_index(va), true/*segtab*/, nowait); 2737 if (__predict_false(st == NULL)) { 2738 pmap_table_release(pmap, st1, &pmap->pm_garbage); 2739 return NULL; 2740 } 2741 } 2742 2743 /* We can now allocate and insert the leaf page table. */ 2744 pt = pmap_table_insert(pmap, st, pmap_st_index(va), pmap_segnum(va), 2745 false/*segtab*/, nowait); 2746 if (__predict_false(pt == NULL)) { 2747 pmap_table_release(pmap, st, &pmap->pm_garbage); 2748 return NULL; 2749 } 2750 2751 *out_pt = pt; 2752 return &pt->pt_entries[pmap_pt_index(va)]; 2753 } 2754 2755 /************************** P->V ENTRY MANAGEMENT ****************************/ 2756 2757 static inline pt_entry_t * 2758 pmap_pv_pte(struct pv_entry * const pv) 2759 { 2760 const vaddr_t va = PV_VA(pv); 2761 2762 if (__predict_true(pv->pv_pmap != pmap_kernel())) { 2763 KASSERT(pv->pv_pt != NULL); 2764 return &pv->pv_pt->pt_entries[pmap_pt_index(va)]; 2765 } 2766 return pmap_kernel_pte(va); 2767 } 2768 2769 #define MATCHING_PMAP(p1, p2) \ 2770 ((p1) == (p2) || \ 2771 (p1) == pmap_kernel() || (p2) == pmap_kernel()) 2772 2773 #define CONFLICTING_ALIAS(va1, va2) \ 2774 (((va1) & pmap_aliasmask) != ((va2) & pmap_aliasmask)) 2775 2776 /* 2777 * pmap_pv_enter: 2778 * 2779 * Add a physical->virtual entry to the pv table. Caller must provide 2780 * the storage for the new PV entry. 2781 * 2782 * We are responsible for storing the new PTE into the destination 2783 * table. We are also guaranteed that no mapping exists there, but 2784 * the MMU has a negative cache in the ATC (see 68030UM Figure 9-8. 2785 * Address Translation General Flowchart, ATC hit, B==1 case, as well 2786 * as 68040UM Figure 3-21. ATC Entry and Tag Fields, R bit and the 2787 * associated descriptive text), so we still have to handle ATC entry 2788 * invalidation. 2789 */ 2790 static void 2791 pmap_pv_enter(pmap_t pmap, struct vm_page *pg, vaddr_t va, vm_prot_t prot, 2792 struct pmap_table *pt, pt_entry_t npte, struct pv_entry *newpv) 2793 { 2794 const bool usr_ci = pte_ci_p(npte); 2795 const paddr_t pa = VM_PAGE_TO_PHYS(pg); 2796 uint16_t atc_flags = 0; 2797 struct pv_entry *pv; 2798 pt_entry_t opte; 2799 2800 pmap_evcnt(pv_enter_called); 2801 2802 KASSERT(newpv != NULL); 2803 KASSERT(PMAP_LOCK_OWNED(pmap)); 2804 2805 npte |= PTE_PVLIST; 2806 2807 newpv->pv_pmap = pmap; 2808 newpv->pv_vf = va; 2809 newpv->pv_pt = pt; 2810 2811 if (__predict_false(usr_ci)) { 2812 newpv->pv_vf |= PV_F_CI_USR; 2813 } 2814 2815 /* 2816 * If this is an EXEC mapping, then we have to ensure that 2817 * the I$ doesn't load stale data. 2818 * 2819 * XXX Should have a soft-PTE bit for this. 2820 */ 2821 if (prot & UVM_PROT_EXEC) { 2822 #if MMU_CONFIG_68040_CLASS 2823 if (MMU_IS_68040_CLASS) { 2824 atc_flags |= ATC_F_BC; 2825 } 2826 #endif 2827 } 2828 2829 pt_entry_t *ptep = pmap_pv_pte(newpv); 2830 2831 PVH_LOCK(pg); 2832 2833 #ifdef DEBUG 2834 /* 2835 * Make sure the entry doesn't already exist. 2836 */ 2837 for (pv = VM_MDPAGE_PVS(pg); pv != NULL; pv = pv->pv_next) { 2838 if (pmap == pv->pv_pmap && va == PV_VA(pv)) { 2839 PVH_UNLOCK(pg); 2840 panic("%s: pmap=%p va=0x%08lx already in PV table", 2841 __func__, pmap, va); 2842 /* NOTREACHED */ 2843 } 2844 } 2845 #endif 2846 2847 newpv->pv_next = VM_MDPAGE_PVS(pg); 2848 VM_MDPAGE_SETPVP(VM_MDPAGE_HEAD_PVP(pg), newpv); 2849 LIST_INSERT_HEAD(&pmap->pm_pvlist, newpv, pv_pmlist); 2850 VM_MDPAGE_ADD_UM(pg, npte); 2851 2852 #if MMU_CONFIG_HP_CLASS 2853 if (MMU_IS_HP_CLASS) { 2854 /* Go handle the HP MMU's VAC. */ 2855 goto hp_mmu_vac_shenanigans; 2856 } 2857 #endif 2858 2859 /* 2860 * If the page is marked as being cache-inhibited, it means 2861 * there is at least one user-requested CI mapping already 2862 * (and that all of the extant mappings are thus CI). 2863 * 2864 * In this case, we need to make sure that the one we're 2865 * establishing now is CI as well. 2866 */ 2867 if (__predict_false(VM_MDPAGE_CI_P(pg))) { 2868 npte = pte_set_ci(npte); 2869 pte_store(ptep, npte); 2870 goto out; 2871 } 2872 2873 /* Set the PTE for the new mapping. */ 2874 pte_store(ptep, npte); 2875 2876 /* 2877 * If this is a user-requested CI mapping, we need to make 2878 * sure the page is purged from the cache and mark any other 2879 * mappings of this page CI as well. 2880 */ 2881 if (__predict_false(usr_ci)) { 2882 uint16_t atc_c_flags = ATC_F_BC; 2883 2884 VM_MDPAGE_SET_CI(pg); 2885 pmap_evcnt(pv_enter_usr_ci); 2886 2887 /* 2888 * There shouldn't be very many of these; CI mappings 2889 * of managed pages are typically only for coherent DMA 2890 * purposes, and multiple mappings of the same page are 2891 * extremely uncommon in that scenario. 2892 */ 2893 for (pv = newpv->pv_next; pv != NULL; pv = pv->pv_next) { 2894 pt_entry_t actual; 2895 pmap_evcnt(pv_enter_ci_multi); 2896 ptep = pmap_pv_pte(pv); 2897 for (opte = pte_load(ptep);; opte = actual) { 2898 npte = pte_set_ci(opte); 2899 actual = pte_update(ptep, opte, npte); 2900 if (__predict_true(actual == opte)) { 2901 pmap_atc_invalidate_pv(pv, pa, 2902 atc_flags | atc_c_flags | 2903 ATC_F_CACHE); 2904 atc_c_flags = 0; 2905 break; 2906 } 2907 } 2908 } 2909 } 2910 out: 2911 PVH_UNLOCK(pg); 2912 pmap_atc_invalidate(pmap, va, pa, atc_flags | ATC_F_ENTER); 2913 return; 2914 2915 #if MMU_CONFIG_HP_CLASS 2916 hp_mmu_vac_shenanigans: 2917 /* 2918 * We have ourselves a VAC, so in addition to checking for 2919 * user-requested-CI mappings, we have to check for cache 2920 * aliases and cache-inhibit all mappings for a page that 2921 * have a cache alias conflict. 2922 * 2923 * - All mappings of a given page within the same pmap must 2924 * not collide. (The VAC is flushed when switching pmaps 2925 * by virtue of a new segment table pointer being loaded 2926 * into the user segment table register.) 2927 * 2928 * - The Hibler pmap check to see that the kernel doesn't have 2929 * conflicting mappings with any user pmap. We'll do the same, 2930 * which seems reasonable on the surface if you think about it 2931 * for a couple of minutes. 2932 * 2933 * - The Hibler pmap also just punts and cache-inhibits all 2934 * mappings once it becomes > 2, but we do NOT do that because 2935 * it will severely penalize shared libraries. 2936 * 2937 * N.B. The method used here will not universally render all 2938 * mappings for a given page uncached; only address spaces with 2939 * conflicts are penalized. 2940 * 2941 * XXX This probably only matters if one of the mappings is 2942 * XXX writable, as this is the only situation where data 2943 * XXX inconsistency could arise. There is probably room 2944 * XXX for further optimization if someone with one of these 2945 * XXX machines cares to take it up. 2946 * XXX 2947 * XXX ALSO... Hibler's pmap checked for incompatible aliases 2948 * XXX between user and kernel... but doesn't the cache have 2949 * XXX a USER/SUPER discriminator on the tag? You can selectively 2950 * XXX invalidate that way! 2951 */ 2952 2953 /* Set the PTE for the new mapping. */ 2954 pte_store(ptep, npte); 2955 pmap_atc_invalidate(pmap, va, pa, atc_flags | ATC_F_ENTER); 2956 2957 /* 2958 * Invalidate the ATC entry **after** storing the PTE so that 2959 * there is no window where another MMU table walk finds the 2960 * stale invalid entry. 2961 */ 2962 2963 vaddr_t pv_flags = newpv->pv_vf & PV_F_CI_USR; 2964 if (usr_ci) { 2965 pmap_evcnt(pv_enter_usr_ci); 2966 } 2967 2968 for (pv = newpv->pv_next; pv != NULL; pv = pv->pv_next) { 2969 if (MATCHING_PMAP(pmap, pv->pv_pmap) && 2970 CONFLICTING_ALIAS(va, PV_VA(pv))) { 2971 pmap_evcnt(pv_enter_vac_ci); 2972 pv_flags |= PV_F_CI_VAC; 2973 break; 2974 } 2975 } 2976 2977 if (__predict_true(pv_flags == 0)) { 2978 /* No new inhibitions! */ 2979 PVH_UNLOCK(pg); 2980 return; 2981 } 2982 2983 VM_MDPAGE_SET_CI(pg); 2984 for (pv = VM_MDPAGE_PVS(pg); pv != NULL; pv = pv->pv_next) { 2985 if (MATCHING_PMAP(pmap, pv->pv_pmap)) { 2986 pmap_evcnt(pv_enter_ci_multi); 2987 pv->pv_vf |= pv_flags; 2988 pte_set(pmap_pv_pte(pv), PTE51_CI); 2989 pmap_atc_invalidate_pv(pv, pa, ATC_F_DC | ATC_F_CACHE); 2990 } 2991 } 2992 PVH_UNLOCK(pg); 2993 #endif /* MMU_CONFIG_HP_CLASS */ 2994 } 2995 2996 /* 2997 * pmap_pv_remove: 2998 * 2999 * Remove a physical->virtual entry from the pv table. 3000 */ 3001 static void 3002 pmap_pv_remove(pmap_t pmap, struct vm_page *pg, vaddr_t va, pt_entry_t opte, 3003 struct pmap_garbage *g) 3004 { 3005 struct pv_entry **pvp, *pv; 3006 pt_entry_t *ptep, npte; 3007 paddr_t pa = VM_PAGE_TO_PHYS(pg); 3008 3009 pmap_evcnt(pv_remove_called); 3010 3011 KASSERT(PVH_LOCK_OWNED(pg)); 3012 KASSERT(PMAP_LOCK_OWNED(pmap)); 3013 3014 /* Update cached U/M bits from mapping that's going away. */ 3015 VM_MDPAGE_ADD_UM(pg, opte); 3016 3017 for (pvp = VM_MDPAGE_HEAD_PVP(pg), pv = VM_MDPAGE_PVS(pg); 3018 pv != NULL; 3019 pvp = &pv->pv_next, pv = *pvp) { 3020 if (pmap == pv->pv_pmap && va == PV_VA(pv)) { 3021 break; 3022 } 3023 } 3024 3025 KASSERT(pv != NULL); 3026 VM_MDPAGE_SETPVP(pvp, pv->pv_next); 3027 LIST_REMOVE(pv, pv_pmlist); 3028 3029 KASSERT(g != NULL); 3030 LIST_INSERT_HEAD(&g->g_pvlist, pv, pv_pmlist); 3031 3032 #if MMU_CONFIG_HP_CLASS 3033 if (MMU_IS_HP_CLASS) { 3034 /* Go handle the HP MMU's VAC. */ 3035 goto hp_mmu_vac_shenanigans; 3036 } 3037 #endif 3038 3039 /* 3040 * If the page is marked as being cache-inhibited, then it 3041 * means there was at least one user-requested CI mapping 3042 * for the page. In that case, we need to scan the P->V 3043 * list to see if any remain, and if not, clear the CI 3044 * status for the page. 3045 * 3046 * N.B. This requires traversing the list twice: once to 3047 * check if any of the mappings are user-requested-CI, 3048 * and one again to fix them up. But, we're making a 3049 * classical space-vs-time trade-off here: Assuming that 3050 * this is a rare situation, it's better to pay the cpu 3051 * cost on the rare edge transitions rather than always pay 3052 * the memory cost of having a counter to track something 3053 * that almost never happens (and, when it does, the list 3054 * will be very short). 3055 */ 3056 if (__predict_false(VM_MDPAGE_CI_P(pg))) { 3057 pmap_evcnt(pv_remove_ci); 3058 for (pv = VM_MDPAGE_PVS(pg); pv != NULL; pv = pv->pv_next) { 3059 if (pv->pv_vf & PV_F_CI_USR) { 3060 /* 3061 * There is still at least one user-requested 3062 * CI mapping, so we can't change the page's CI 3063 * status. 3064 */ 3065 return; 3066 } 3067 } 3068 KASSERT(pv == NULL); 3069 VM_MDPAGE_CLR_CI(pg); 3070 for (pv = VM_MDPAGE_PVS(pg); pv != NULL; pv = pv->pv_next) { 3071 pt_entry_t actual; 3072 ptep = pmap_pv_pte(pv); 3073 for (opte = pte_load(ptep);; opte = actual) { 3074 opte = pte_load(ptep); 3075 npte = pte_clr_ci(opte); 3076 actual = pte_update(ptep, opte, npte); 3077 if (__predict_true(actual == opte)) { 3078 pmap_atc_invalidate_pv(pv, pa, 3079 ATC_F_REMOVE | ATC_F_CACHE); 3080 break; 3081 } 3082 } 3083 } 3084 } 3085 return; 3086 3087 #if MMU_CONFIG_HP_CLASS 3088 hp_mmu_vac_shenanigans: 3089 /* 3090 * If we have a VAC and the page was cache-inhibited due to 3091 * a cache alias conflict, we can re-enable the cache if there 3092 * is just one such mapping left. 3093 */ 3094 if (__predict_false(VM_MDPAGE_CI_P(pg))) { 3095 vaddr_t all_ci_flags = PV_F_CI_USR; 3096 3097 pmap_evcnt(pv_remove_ci); 3098 3099 for (pv = VM_MDPAGE_PVS(pg); pv != NULL; pv = pv->pv_next) { 3100 if (! MATCHING_PMAP(pmap, pv->pv_pmap)) { 3101 continue; 3102 } 3103 if (pv->pv_vf & all_ci_flags) { 3104 /* 3105 * There is at least one CI_USR mapping 3106 * or more than one CI_VAC mapping, so 3107 * the CI status of the page remains 3108 * unchanged. 3109 */ 3110 return; 3111 } 3112 all_ci_flags |= pv->pv_vf & PV_F_CI_VAC; 3113 } 3114 KASSERT(pv == NULL); 3115 /* 3116 * We now know we can remove CI from the page mappings 3117 * in the matching address space. If no CI mappings 3118 * remain, then we can clear the CI indicator on the 3119 * page. 3120 */ 3121 all_ci_flags = 0; 3122 for (pv = VM_MDPAGE_PVS(pg); pv != NULL; pv = pv->pv_next) { 3123 if (! MATCHING_PMAP(pmap, pv->pv_pmap)) { 3124 all_ci_flags |= pv->pv_vf; 3125 continue; 3126 } 3127 pte_mask(pmap_pv_pte(pv), ~((uint32_t)PTE51_CI)); 3128 pmap_atc_invalidate_pv(pv, pa, 3129 ATC_F_REMOVE | ATC_F_CACHE); 3130 } 3131 all_ci_flags &= PV_F_CI_USR | PV_F_CI_VAC; 3132 if (__predict_true(all_ci_flags == 0)) { 3133 VM_MDPAGE_CLR_CI(pg); 3134 } 3135 } 3136 #endif /* MMU_CONFIG_HP_CLASS */ 3137 } 3138 3139 #undef CONFLICTING_ALIAS 3140 #undef MATCHING_PMAP 3141 3142 /***************** PMAP INTERFACE (AND ADJACENT) FUNCTIONS *******************/ 3143 3144 static inline void 3145 pmap_stat_update_impl(volatile long *valp, int val) 3146 { 3147 #if defined(MULTIPROCESSOR) 3148 atomic_add_long(valp, val); 3149 #else 3150 /* Atomic vis a vis interrupts. */ 3151 __asm __volatile("addl %1,%0" 3152 : "+m" (*valp) 3153 : "d" (val)); 3154 #endif 3155 } 3156 3157 #define pmap_stat_update(pm, stat, delta) \ 3158 pmap_stat_update_impl(&(pm)->pm_stats.stat, (delta)) 3159 3160 static inline void 3161 pmap_stat_set_impl(volatile long *valp, int val) 3162 { 3163 atomic_store_relaxed(valp, val); 3164 } 3165 3166 #define pmap_stat_set(pm, stat, val) \ 3167 pmap_stat_set_impl(&(pm)->pm_stats.stat, (val)) 3168 3169 /* 3170 * pmap_pinit: 3171 * 3172 * Common bits of pmap structure initialization shared between 3173 * the kernel pmap and user pmaps. 3174 */ 3175 static void 3176 pmap_pinit(pmap_t pmap, paddr_t lev1pa) 3177 { 3178 PMAP_MUTEX_INIT(&pmap->pm_mutex, MUTEX_DEFAULT, IPL_NONE); 3179 pmap->pm_lev1pa = lev1pa; 3180 rb_tree_init(&pmap->pm_tables, &pmap_table_rb_ops); 3181 TAILQ_INIT(&pmap->pm_ptpages[0]); 3182 TAILQ_INIT(&pmap->pm_ptpages[1]); 3183 LIST_INIT(&pmap->pm_pvlist); 3184 3185 pmap_garbage_init(&pmap->pm_garbage); 3186 3187 pmap->pm_refcnt = 1; 3188 } 3189 3190 /* 3191 * pmap_virtual_space: [ INTERFACE ] 3192 * 3193 * Define the initial bounds of the kernel virtual address space. 3194 * 3195 * In this implementation, the start address we return marks the 3196 * end of the statically allocated special kernel virtual addresses 3197 * set up in pmap_bootstrap1(). We return kernel_virtual_max as 3198 * the end because we can grow the kernel address space using 3199 * pmap_growkernel(). 3200 */ 3201 void 3202 pmap_virtual_space(vaddr_t *vstartp, vaddr_t *vendp) 3203 { 3204 *vstartp = kernel_virtual_start; 3205 *vendp = kernel_virtual_max; 3206 } 3207 3208 /* 3209 * pmap_init: [ INTERFACE ] 3210 * 3211 * Initialize the pmap module. Called by vm_init(), to initialize any 3212 * structures that the pmap system needs to map virtual memory. 3213 */ 3214 void 3215 pmap_init(void) 3216 { 3217 /* Initialize the pmap / pv_entry allocators. */ 3218 pmap_alloc_init(); 3219 3220 /* Initialize the PT page allocator. */ 3221 pmap_ptpage_init(); 3222 3223 /* Now it's safe to do P->V entry recording! */ 3224 pmap_initialized_p = true; 3225 } 3226 3227 /* 3228 * pmap_create: [ INTERFACE ] 3229 * 3230 * Create and return a physical map. 3231 */ 3232 pmap_t 3233 pmap_create(void) 3234 { 3235 pmap_t pmap; 3236 3237 /* 3238 * We reference the null segment table and and have a NULL 3239 * lev1map pointer until the first mapping is entered. 3240 */ 3241 pmap = pmap_alloc(); 3242 pmap_pinit(pmap, null_segtab_pa); 3243 3244 return pmap; 3245 } 3246 3247 /* 3248 * pmap_destroy: [ INTERFACE ] 3249 * 3250 * Drop the reference count on the specified pmap, releasing 3251 * all resources if the reference count drops to zero. 3252 */ 3253 void 3254 pmap_destroy(pmap_t pmap) 3255 { 3256 uint32_t newval; 3257 3258 KASSERT(pmap->pm_refcnt > 0); 3259 #if defined(MULTIPROCESSOR) 3260 newval = atomic_dec_32_nv(&pmap->pm_refcnt); 3261 #else 3262 newval = --pmap->pm_refcnt; 3263 #endif 3264 if (newval) { 3265 return; 3266 } 3267 3268 /* 3269 * uvmspace_free() calls pmap_remove_all() but does not 3270 * call pmap_update() aftwards. Do that pmap_update() 3271 * now. 3272 */ 3273 pmap_update(pmap); 3274 3275 /* We assume all mappings have been removed. */ 3276 KASSERT(pmap->pm_lev1map == NULL); 3277 KASSERT(pmap->pm_lev1pa == null_segtab_pa); 3278 3279 /* ...and all the garbage has been collected. */ 3280 KASSERT(TAILQ_EMPTY(&pmap->pm_garbage.g_ptpages)); 3281 KASSERT(LIST_EMPTY(&pmap->pm_garbage.g_pvlist)); 3282 3283 pmap_free(pmap); 3284 } 3285 3286 /* 3287 * pmap_reference: [ INTERFACE ] 3288 * 3289 * Add a reference to the specified pmap. 3290 */ 3291 void 3292 pmap_reference(pmap_t pmap) 3293 { 3294 #if defined(MULTIPROCESSOR) 3295 atomic_inc_32(&pmap->pm_refcnt); 3296 #else 3297 pmap->pm_refcnt++; 3298 #endif 3299 KASSERT(pmap->pm_refcnt > 0); 3300 } 3301 3302 /* 3303 * pmap_remove_mapping: 3304 * 3305 * Invalidate a single page denoted by pmap/va. 3306 * 3307 * If (ptep != NULL), it is the already computed PTE for the mapping. 3308 * 3309 * If the caller wishes to prevent the page table from being freed, 3310 * they should perform an extra retain. 3311 */ 3312 static void 3313 pmap_remove_mapping(pmap_t pmap, vaddr_t va, pt_entry_t *ptep, 3314 struct pmap_table *pt, struct vm_page *pg, 3315 uint16_t atc_flags, struct pmap_garbage *g) 3316 { 3317 KASSERT(pt != NULL || pmap == pmap_kernel()); 3318 KASSERT(ptep != NULL); 3319 const paddr_t opte = pte_load(ptep); 3320 KASSERT(pte_valid_p(opte)); 3321 const paddr_t pa = pte_pa(opte); 3322 KASSERT(pg == NULL || pa == VM_PAGE_TO_PHYS(pg)); 3323 3324 KASSERT(pg == NULL || PVH_LOCK_OWNED(pg)); 3325 KASSERT(PMAP_LOCK_OWNED(pmap)); 3326 3327 /* Update statistics. */ 3328 if (pte_wired_p(opte)) { 3329 pmap_stat_update(pmap, wired_count, -1); 3330 } 3331 pmap_stat_update(pmap, resident_count, -1); 3332 3333 /* 3334 * 68020/68030 I-cache and 68030 D-cache are virtually-addressed, 3335 * so removing a mapping for a page requires invalidating the 3336 * cache. 3337 * 3338 * We can skip this if the mapping was cache-inhibited. 3339 * 3340 * N.B. This also covers the external VAC on HP MMU systems. 3341 * 3342 * XXX For I-cache, should check EXEC'ness of the page. 3343 */ 3344 if (cputype < CPU_68040 && !pte_ci_p(opte)) { 3345 atc_flags |= ATC_F_DC | ATC_F_IC; 3346 } 3347 3348 if (__predict_true(pg != NULL)) { 3349 KASSERT(pte_managed_p(opte)); 3350 pmap_pv_remove(pmap, pg, va, opte, g); 3351 } else { 3352 KASSERT(! pte_managed_p(opte)); 3353 } 3354 3355 /* 3356 * Zap the PTE and drop the retain count that the mapping 3357 * had on the table. 3358 */ 3359 pte_store(ptep, 0); 3360 pmap_table_release(pmap, pt, g); 3361 3362 /* 3363 * Now that the ATC can't be reloaded from the PTE, invalidate 3364 * the ATC entry. 3365 */ 3366 pmap_atc_invalidate(pmap, va, pa, atc_flags | ATC_F_REMOVE); 3367 } 3368 3369 /* 3370 * pmap_remove: [ INTERFACE ] 3371 * 3372 * Remove the given range of addresses from the specified map. 3373 * 3374 * It is assumed that the start and end are properly rounded 3375 * to the page size. 3376 * 3377 * N.B. Callers of pmap_remove_internal() are expected to 3378 * provide an initialized completion context, which we 3379 * will finalize. 3380 */ 3381 void 3382 pmap_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva) 3383 { 3384 pt_entry_t opte, *ptep; 3385 struct pmap_table *pt; 3386 struct vm_page *pg; 3387 vaddr_t nextseg; 3388 3389 /* 3390 * If this is the kernel pmap, we can use a faster method 3391 * for accessing the PTEs (since the PT pages are always 3392 * resident). 3393 * 3394 * Note that this routine should NEVER be called from an 3395 * interrupt context; pmap_kremove() is used for that. 3396 */ 3397 if (pmap == pmap_kernel()) { 3398 struct pmap_garbage kg; 3399 3400 /* 3401 * We can't safely defer freeing resources for the 3402 * kernel pmap because pmap_update() can be called 3403 * in an interrupt context, so we have to use our 3404 * own garbage can. 3405 */ 3406 pmap_garbage_init(&kg); 3407 3408 PMAP_LOCK(pmap); 3409 for (ptep = pmap_kernel_pte(sva); sva < eva; 3410 ptep++, sva += PAGE_SIZE) { 3411 opte = pte_load(ptep); 3412 if (pte_valid_p(opte)) { 3413 pg = pmap_pa_to_pg(pte_pa(opte)); 3414 if (pg != NULL) { 3415 PVH_LOCK(pg); 3416 } 3417 pmap_remove_mapping(pmap, sva, 3418 ptep, NULL/*pt*/, pg, 0, &kg); 3419 if (pg != NULL) { 3420 PVH_UNLOCK(pg); 3421 } 3422 } 3423 } 3424 PMAP_UNLOCK(pmap); 3425 3426 /* kernel PT pages are never freed. */ 3427 KASSERT(TAILQ_EMPTY(&kg.g_ptpages)); 3428 3429 /* ...but we might have freed PV entries. */ 3430 pmap_garbage_collect(&kg); 3431 3432 return; 3433 } 3434 3435 PMAP_LOCK(pmap); 3436 while (sva < eva) { 3437 nextseg = pmap_next_seg(sva); 3438 if (nextseg == 0 || nextseg > eva) { 3439 nextseg = eva; 3440 } 3441 3442 ptep = pmap_pte_lookup(pmap, sva, &pt); 3443 if (ptep == NULL) { 3444 /* 3445 * No table for this address, meaning nothing 3446 * within this segment; advance to the next 3447 * one. 3448 */ 3449 sva = nextseg; 3450 continue; 3451 } 3452 3453 for (; sva < nextseg; ptep++, sva += PAGE_SIZE) { 3454 opte = pte_load(ptep); 3455 if (! pte_valid_p(opte)) { 3456 continue; 3457 } 3458 pg = pmap_pa_to_pg(pte_pa(opte)); 3459 if (pg != NULL) { 3460 PVH_LOCK(pg); 3461 } 3462 pmap_remove_mapping(pmap, sva, 3463 ptep, pt, pg, 0, &pmap->pm_garbage); 3464 if (pg != NULL) { 3465 PVH_UNLOCK(pg); 3466 } 3467 } 3468 pmap_table_release(pmap, pt, &pmap->pm_garbage); 3469 } 3470 PMAP_UNLOCK(pmap); 3471 } 3472 3473 /* 3474 * pmap_remove_all: [ INTERFACE ] 3475 * 3476 * Remove all mappings from a pmap in bulk. This is only called 3477 * when it's known that the address space is no longer visible to 3478 * any user process (e.g. during exit or exec). 3479 */ 3480 bool 3481 pmap_remove_all(pmap_t pmap) 3482 { 3483 struct pv_entry *pv; 3484 struct vm_page *pg; 3485 pt_entry_t opte; 3486 3487 KASSERT(pmap != pmap_kernel()); 3488 3489 /* 3490 * This process is pretty simple: 3491 * 3492 * ==> (1) Set the segment table pointer to the NULL segment table. 3493 * 3494 * ==> (2) Copy the PT page list to a tempory list and re-init. 3495 * 3496 * ==> (3) Walk the PV entry list and remove each entry. 3497 * 3498 * ==> (4) Zero the wired and resident count. 3499 * 3500 * Once we've done that, we just need to free everything 3501 * back to the system. 3502 */ 3503 3504 PMAP_LOCK(pmap); 3505 3506 /* Step 1. */ 3507 pmap_set_lev1map(pmap, NULL, null_segtab_pa); 3508 3509 /* Step 2. */ 3510 pmap->pm_pt_cache = NULL; 3511 TAILQ_CONCAT(&pmap->pm_garbage.g_ptpages, &pmap->pm_ptpages[0], 3512 ptp_list); 3513 TAILQ_CONCAT(&pmap->pm_garbage.g_ptpages, &pmap->pm_ptpages[1], 3514 ptp_list); 3515 memset(&pmap->pm_tables, 0, sizeof(pmap->pm_tables)); 3516 rb_tree_init(&pmap->pm_tables, &pmap_table_rb_ops); 3517 KASSERT(RB_TREE_MIN(&pmap->pm_tables) == NULL); 3518 3519 /* Step 3. */ 3520 while ((pv = LIST_FIRST(&pmap->pm_pvlist)) != NULL) { 3521 KASSERT(pv->pv_pmap == pmap); 3522 opte = pte_load(pmap_pv_pte(pv)); 3523 pg = pmap_pa_to_pg(pte_pa(opte)); 3524 PVH_LOCK(pg); 3525 pmap_pv_remove(pmap, pg, PV_VA(pv), opte, &pmap->pm_garbage); 3526 PVH_UNLOCK(pg); 3527 } 3528 3529 /* Step 4. */ 3530 pmap_stat_set(pmap, wired_count, 0); 3531 pmap_stat_set(pmap, resident_count, 0); 3532 3533 PMAP_UNLOCK(pmap); 3534 3535 return true; 3536 } 3537 3538 /* 3539 * pmap_page_protect: [ INTERFACE ] 3540 * 3541 * Lower the permission for all mappings to a given page to 3542 * the permissions specified. 3543 */ 3544 void 3545 pmap_page_protect(struct vm_page *pg, vm_prot_t prot) 3546 { 3547 struct pmap_garbage g; 3548 struct pv_entry *pv; 3549 pmap_t pmap; 3550 3551 if (prot & UVM_PROT_WRITE) { 3552 /* No protection to revoke. */ 3553 return; 3554 } 3555 3556 if (prot & UVM_PROT_READ) { 3557 /* Making page copy-on-write. */ 3558 pmap_changebit(pg, PTE_WP, ~0U); 3559 return; 3560 } 3561 3562 /* Removing all mappings for a page. */ 3563 pmap_garbage_init(&g); 3564 3565 restart: 3566 PVH_LOCK(pg); 3567 while ((pv = VM_MDPAGE_PVS(pg)) != NULL) { 3568 pmap = pv->pv_pmap; 3569 if (! PMAP_TRYLOCK(pmap)) { 3570 /* 3571 * Deadlock avoidance. Take an extra reference 3572 * on the pmap so that it doesn't disappear on 3573 * us, then drop the pvh lock, acquire (and 3574 * release) the pmap lock, then go around again. 3575 */ 3576 pmap_reference(pmap); 3577 PVH_UNLOCK(pg); 3578 PMAP_LOCK(pmap); 3579 PMAP_UNLOCK(pmap); 3580 pmap_destroy(pmap); 3581 goto restart; 3582 } 3583 pmap_remove_mapping(pmap, PV_VA(pv), pmap_pv_pte(pv), 3584 pv->pv_pt, pg, ATC_F_PV, &g); 3585 PMAP_UNLOCK(pmap); 3586 } 3587 PVH_UNLOCK(pg); 3588 3589 /* Page-oriented operations must collect the garbage immediately. */ 3590 pmap_atc_process(); 3591 pmap_garbage_collect(&g); 3592 } 3593 3594 /* 3595 * pmap_protect: [ INTERFACE ] 3596 * 3597 * Set the physical protection on the specified range of this map 3598 * as requested. 3599 */ 3600 void 3601 pmap_protect(pmap_t pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot) 3602 { 3603 pt_entry_t *ptep, opte, npte; 3604 struct pmap_table *pt; 3605 vaddr_t nextseg; 3606 #if MMU_CONFIG_68040_CLASS 3607 bool removing_write; 3608 #endif 3609 uint16_t atc_flags; 3610 3611 if ((prot & UVM_PROT_READ) == 0) { 3612 pmap_remove(pmap, sva, eva); 3613 return; 3614 } 3615 3616 PMAP_LOCK(pmap); 3617 3618 #if MMU_CONFIG_68040_CLASS 3619 removing_write = (prot & UVM_PROT_WRITE) == 0; 3620 #endif 3621 while (sva < eva) { 3622 nextseg = pmap_next_seg(sva); 3623 if (nextseg == 0 || nextseg > eva) { 3624 nextseg = eva; 3625 } 3626 3627 ptep = pmap_pte_lookup(pmap, sva, &pt); 3628 if (ptep == NULL) { 3629 /* 3630 * No table for this address, meaning nothing 3631 * within this segment; advance to the next 3632 * one. 3633 */ 3634 sva = nextseg; 3635 continue; 3636 } 3637 3638 /* 3639 * Change protection on mapping if it is valid and doesn't 3640 * already have the correct protection. 3641 */ 3642 for (; sva < nextseg; ptep++, sva += PAGE_SIZE) { 3643 pt_entry_t actual; 3644 opte = pte_load(ptep); 3645 paddr_t pa = pte_pa(opte); 3646 try_again: 3647 if (! pte_valid_p(opte)) { 3648 continue; 3649 } 3650 npte = pte_change_prot(opte, prot); 3651 if (npte == opte) { 3652 continue; 3653 } 3654 atc_flags = ATC_F_PROT; 3655 #if MMU_CONFIG_68040_CLASS 3656 if (MMU_IS_68040_CLASS && removing_write) { 3657 /* 3658 * Clear caches if making RO (see section 3659 * "7.3 Cache Coherency" in the manual). 3660 */ 3661 atc_flags |= ATC_F_DC; 3662 } 3663 #endif 3664 actual = pte_update(ptep, opte, npte); 3665 if (__predict_false(actual != opte)) { 3666 /* Lost race updating PTE; try again. */ 3667 opte = actual; 3668 goto try_again; 3669 } 3670 pmap_atc_invalidate(pmap, sva, pa, atc_flags); 3671 } 3672 pmap_table_release(pmap, pt, NULL); 3673 } 3674 3675 PMAP_UNLOCK(pmap); 3676 } 3677 3678 /* 3679 * pmap_enter: [ INTERFACE ] 3680 * 3681 * Insert the given physical address (pa) at the specified 3682 * virtual address (va) in the target physical map with the 3683 * protection requested. 3684 * 3685 * If specified, the page will be wired down, meaning that 3686 * related pte can not be reclaimed. 3687 * 3688 * Note: This is the only routine which MAY NOT lazy-evaluate 3689 * or lose information. That is, this routine must actually 3690 * insert this page into the given map NOW. 3691 */ 3692 int 3693 pmap_enter(pmap_t pmap, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags) 3694 { 3695 struct pmap_table *pt; 3696 pt_entry_t *ptep, npte, opte; 3697 struct pv_entry *newpv; 3698 int error = 0; 3699 const bool nowait = !!(flags & PMAP_CANFAIL); 3700 3701 /* cpu-local mappings never allowed via this path. */ 3702 KASSERT((flags & PMAP_CPU_LOCAL) == 0); 3703 3704 struct vm_page * const pg = pmap_pa_to_pg(pa); 3705 if (__predict_false(pg == NULL)) { 3706 /* 3707 * PA is not part of managed memory. Make the mapping 3708 * cache-inhibited on the assumption that it's a device 3709 * unless there cache mode has been explcitly called out. 3710 */ 3711 if (__predict_true((flags & PMAP_CACHE_MASK) == 0)) { 3712 flags |= PMAP_NOCACHE; 3713 } 3714 } 3715 3716 PMAP_LOCK(pmap); 3717 3718 if (nowait) { 3719 pmap_evcnt(enter_nowait); 3720 } else { 3721 pmap_evcnt(enter_yeswait); 3722 } 3723 3724 /* Get the destination table. */ 3725 ptep = pmap_pte_alloc(pmap, va, &pt, nowait); 3726 if (__predict_false(ptep == NULL)) { 3727 pmap_evcnt(enter_pte_alloc_fail); 3728 error = ENOMEM; 3729 goto out; 3730 } 3731 3732 /* Compute the new PTE. */ 3733 npte = pmap_make_pte(pa, prot, flags); 3734 3735 /* Fetch old PTE. */ 3736 opte = pte_load(ptep); 3737 3738 /* 3739 * Check to see if there is a valid mapping at this address. 3740 * It might simply be a wiring or protection change. 3741 */ 3742 if (pte_valid_p(opte)) { 3743 pmap_evcnt(enter_valid); 3744 restart: 3745 if (pte_pa(opte) == pa) { 3746 /* 3747 * Just a protection or wiring change. 3748 * 3749 * Since the old PTE is handy, go ahead and update 3750 * the cached U/M attributes now. Normally we would 3751 * do this in pmap_remove_mapping(), but we're not 3752 * taking that path in this case. We also add in 3753 * any U/M attributes hinted by the access type 3754 * that brought us to pmap_enter() in the first 3755 * place (a write-fault on a writable page mapped 3756 * read-only during a page-out, for example). 3757 * 3758 * Also ensure that the PV list status of the mapping 3759 * is consistent. 3760 */ 3761 if (__predict_true(pg != NULL)) { 3762 VM_MDPAGE_ADD_UM(pg, opte | npte); 3763 KASSERT(pte_managed_p(opte)); 3764 npte |= PTE_PVLIST; 3765 } 3766 3767 /* Preserve cache-inhibited status. */ 3768 if (__predict_false(pte_ci_p(opte))) { 3769 npte = 3770 (npte & ~PTE_CMASK) | (opte & PTE_CMASK); 3771 } 3772 3773 /* Set the new PTE. */ 3774 pte_store(ptep, npte); 3775 3776 const pt_entry_t diff = opte ^ npte; 3777 3778 #ifdef PMAP_EVENT_COUNTERS 3779 if (diff & PTE_WIRED) { 3780 pmap_evcnt(enter_wire_change); 3781 } 3782 if (diff & PTE_WP) { 3783 pmap_evcnt(enter_prot_change); 3784 } 3785 #endif 3786 3787 if (pte_wired_p(diff)) { 3788 pmap_stat_update(pmap, wired_count, 3789 pte_wired_p(npte) ? 1 : -1); 3790 } 3791 if (diff) { 3792 /* 3793 * Protection or caching status is changing; 3794 * flush the page from the cache. 3795 */ 3796 uint16_t atc_flags = MMU_IS_68040_CLASS 3797 ? ATC_F_BC : 0; 3798 3799 /* 3800 * If the new mapping is CI and the old 3801 * one is not, then flush D-cache on 3802 * CPUs where the cache is virtually- 3803 * addressed. 3804 */ 3805 if (cputype < CPU_68040 && 3806 pte_ci_p(diff) && pte_ci_p(npte)) { 3807 atc_flags |= ATC_F_DC; 3808 } 3809 /* XXX prot upgrade vs ??? */ 3810 pmap_atc_invalidate(pmap, va, pa, 3811 atc_flags | ATC_F_PROT); 3812 } 3813 3814 /* All done! */ 3815 goto out_release; 3816 } 3817 3818 /* 3819 * The mapping has completely changed. Need to remove 3820 * the old one first. 3821 * 3822 * This drops the retain count on the PT owned by the 3823 * previous mapping, but the newly-entered mapping will 3824 * inherit the retain count taken when we looked up the 3825 * PTE. 3826 * 3827 * XXX Can we elide the ATC flush here? We're going to 3828 * XXX hit the ATC after setting the new PTE anyway. 3829 */ 3830 pmap_evcnt(enter_pa_change); 3831 if (pg != NULL) { 3832 PVH_LOCK(pg); 3833 } 3834 pmap_remove_mapping(pmap, va, ptep, pt, pg, 0, 3835 &pmap->pm_garbage); 3836 if (pg != NULL) { 3837 PVH_UNLOCK(pg); 3838 } 3839 } 3840 3841 /* 3842 * By the time we get here, we should be assured that the 3843 * PTE at ptep is invalid. 3844 */ 3845 KASSERT(! pte_valid_p(pte_load(ptep))); 3846 3847 /* Update pmap stats now. */ 3848 pmap_stat_update(pmap, resident_count, 1); 3849 if (__predict_false(pte_wired_p(npte))) { 3850 pmap_stat_update(pmap, wired_count, 1); 3851 } 3852 3853 if (__predict_true(pg != NULL)) { 3854 /* 3855 * Managed pages also go on the PV list, so we are 3856 * going to need a PV entry. 3857 * 3858 * Because this is an extremely common case, we are 3859 * first going to attempt allocation while still in 3860 * the critical section. If that fails and waiting 3861 * is allowed, we'll leave the critical section and 3862 * try a blocking allocation. 3863 * 3864 * N.B. pmap_pv_alloc() will check for a free entry 3865 * in the garbage bin and do the critical section 3866 * exit/re-entry for us. 3867 */ 3868 newpv = pmap_pv_alloc(pmap, true/*nowait flag*/); 3869 if (__predict_false(newpv == NULL)) { 3870 if (nowait) { 3871 pmap_evcnt(enter_pv_alloc_fail); 3872 error = ENOMEM; 3873 goto out_release; 3874 } 3875 newpv = pmap_pv_alloc(pmap, false/*nowait flag*/); 3876 KASSERT(newpv != NULL); 3877 /* 3878 * Because we may have blocked while allocating 3879 * the PV entry, we have to re-validate our 3880 * environment, as another thread could have 3881 * inserted a mapping here behind our back. 3882 */ 3883 opte = pte_load(ptep); 3884 if (__predict_false(pte_valid_p(opte))) { 3885 pmap_stat_update(pmap, 3886 resident_count, -1); 3887 if (pte_wired_p(npte)) { 3888 pmap_stat_update(pmap, 3889 wired_count, -1); 3890 } 3891 LIST_INSERT_HEAD(&pmap->pm_garbage.g_pvlist, 3892 newpv, pv_pmlist); 3893 goto restart; 3894 } 3895 } 3896 3897 /* 3898 * Enter the mapping into the PV list. pmap_pv_enter() 3899 * will also set the PTE in the table. 3900 * 3901 * The new mapping takes ownership of the PT 3902 * retain count we took while looking up the PTE. 3903 */ 3904 pmap_pv_enter(pmap, pg, va, prot, pt, npte, newpv); 3905 goto out; 3906 } 3907 3908 /* 3909 * Not a managed mapping, so set the new PTE. As with managed 3910 * mappings, the new mapping takes ownership of the PT retain 3911 * count we took while looking up the PTE. 3912 */ 3913 pte_store(ptep, npte); 3914 3915 /* 3916 * See comments in pmap_pv_enter() for why we have to hit 3917 * the ATC here. 3918 */ 3919 pmap_atc_invalidate(pmap, va, pa, ATC_F_ENTER); 3920 goto out; 3921 3922 out_release: 3923 pmap_table_release(pmap, pt, &pmap->pm_garbage); 3924 out: 3925 if (__predict_false(error != 0)) { 3926 /* Don't rely on a pmap_update() call after a failure. */ 3927 struct pmap_garbage g; 3928 pmap_garbage_transfer(&g, &pmap->pm_garbage); 3929 PMAP_UNLOCK(pmap); 3930 pmap_atc_process(); 3931 pmap_garbage_collect(&g); 3932 return error; 3933 } 3934 PMAP_UNLOCK(pmap); 3935 return error; 3936 } 3937 3938 /* 3939 * pmap_kenter_pa: [ INTERFACE ] 3940 * 3941 * Enter a va -> pa mapping into the kernel pmap without any 3942 * physical->virtual tracking. 3943 */ 3944 void 3945 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags) 3946 { 3947 pmap_t const pmap = pmap_kernel(); 3948 int atc_flags = ATC_F_K | ATC_F_ENTER; 3949 3950 KASSERT(va >= VM_MIN_KERNEL_ADDRESS); 3951 3952 pt_entry_t * const ptep = pmap_kernel_pte(va); 3953 3954 /* Build the new PTE. */ 3955 const pt_entry_t npte = pmap_make_pte(pa, prot, flags | PMAP_WIRED); 3956 3957 /* 3958 * If this is an EXEC mapping, then we have to ensure that 3959 * the I$ doesn't load stale data. 3960 */ 3961 if (__predict_false(prot & UVM_PROT_EXEC)) { 3962 #if MMU_CONFIG_68040_CLASS 3963 if (MMU_IS_68040_CLASS) { 3964 atc_flags |= ATC_F_BC; 3965 } 3966 #endif 3967 } 3968 3969 /* There must not be a valid PTE here. */ 3970 KASSERT(! pte_valid_p(pte_load(ptep))); 3971 3972 /* Set the new PTE. */ 3973 pte_store(ptep, npte); 3974 3975 pmap_stat_update(pmap, resident_count, 1); 3976 pmap_stat_update(pmap, wired_count, 1); 3977 3978 /* 3979 * See comments in pmap_pv_enter() as for why we hit the ATC here. 3980 * This *should* be unnecessary because this is a wired kernel 3981 * mapping and no demand-page-ins should have happened at this 3982 * VA, but we're erring on the side of caution for now. 3983 */ 3984 pmap_atc_invalidate(pmap_kernel(), va, pa, atc_flags); 3985 3986 /* if PMAP_CPU_LOCAL, we can avoid sending a TLB shootdown */ 3987 } 3988 3989 /* 3990 * pmap_kremove: [ INTERFACE ] 3991 * 3992 * Remove a mapping entered with pmap_kenter_pa() starting at va, 3993 * for size bytes (assumed to be page rounded). 3994 */ 3995 void 3996 pmap_kremove(vaddr_t va, vsize_t size) 3997 { 3998 pt_entry_t *ptep, opte; 3999 pmap_t const pmap = pmap_kernel(); 4000 int count = 0; 4001 uint16_t atc_flags; 4002 4003 KASSERT(va >= VM_MIN_KERNEL_ADDRESS); 4004 4005 for (ptep = pmap_kernel_pte(va); size != 0; 4006 ptep++, size -= PAGE_SIZE, va += PAGE_SIZE) { 4007 opte = pte_load(ptep); 4008 if (pte_valid_p(opte)) { 4009 KASSERT(! pte_managed_p(opte)); 4010 KASSERT(pte_wired_p(opte)); 4011 4012 atc_flags = ATC_F_K | ATC_F_REMOVE; 4013 4014 /* See pmap_remove_mapping(). */ 4015 /* XXX We skip IC here. On purpose. */ 4016 if (cputype < CPU_68040 && !pte_ci_p(opte)) { 4017 atc_flags |= ATC_F_DC; 4018 } 4019 4020 /* Zap the mapping. */ 4021 pte_store(ptep, 0); 4022 pmap_atc_invalidate(pmap_kernel(), va, 0, atc_flags); 4023 count++; 4024 } 4025 } 4026 4027 /* Update stats. */ 4028 if (__predict_true(count != 0)) { 4029 pmap_stat_update(pmap, resident_count, -count); 4030 pmap_stat_update(pmap, wired_count, -count); 4031 } 4032 } 4033 4034 /* 4035 * pmap_unwire: [ INTERFACE ] 4036 * 4037 * Clear the wired attribute for a map/virtual-address pair. 4038 * 4039 * The mapping must already exist in the pmap. 4040 */ 4041 void 4042 pmap_unwire(pmap_t pmap, vaddr_t va) 4043 { 4044 struct pmap_table *pt; 4045 pt_entry_t actual, opte, npte, *ptep; 4046 4047 PMAP_LOCK(pmap); 4048 4049 ptep = pmap_pte_lookup(pmap, va, &pt); 4050 KASSERT(ptep != NULL); 4051 4052 for (opte = pte_load(ptep);; opte = actual) { 4053 KASSERT(pte_valid_p(opte)); 4054 4055 /* 4056 * If the wiring actually changed (always?), clear the wire 4057 * bit and update the wire count. Note that the wiring is 4058 * not a hardware characteristic so there is no need to 4059 * invalidate the ATC. 4060 */ 4061 if (! pte_wired_p(opte)) { 4062 break; 4063 } 4064 npte = opte & ~PTE_WIRED; 4065 actual = pte_update(ptep, opte, npte); 4066 if (__predict_true(actual == opte)) { 4067 pmap_stat_update(pmap, wired_count, -1); 4068 break; 4069 } 4070 } 4071 4072 pmap_table_release(pmap, pt, NULL); 4073 4074 PMAP_UNLOCK(pmap); 4075 } 4076 4077 /* 4078 * pmap_extract: [ INTERFACE ] 4079 * 4080 * Extract the physical address associated with the given 4081 * pmap/virtual address pair. 4082 * 4083 * pmap_extract_info: 4084 * 4085 * Like pmap_extract(), but also returns information 4086 * about the mapping (wired, cache-inhibited, etc.) 4087 */ 4088 static bool 4089 pmap_extract_pte(pt_entry_t *ptep, vaddr_t va, paddr_t *pap, int *flagsp) 4090 { 4091 pt_entry_t pte; 4092 4093 pte = pte_load(ptep); 4094 if (__predict_true(pte_valid_p(pte))) { 4095 if (__predict_true(pap != NULL)) { 4096 *pap = pte_pa(pte) | (va & PAGE_MASK); 4097 } 4098 if (__predict_false(flagsp != NULL)) { 4099 *flagsp = 4100 (pte_wired_p(pte) ? PMAP_WIRED : 0) | 4101 (pte_ci_p(pte) ? PMAP_NOCACHE : 0); 4102 } 4103 return true; 4104 } 4105 return false; 4106 } 4107 4108 bool 4109 pmap_extract_info(pmap_t pmap, vaddr_t va, paddr_t *pap, int *flagsp) 4110 { 4111 struct pmap_table *pt; 4112 pt_entry_t *ptep; 4113 bool rv = false; 4114 4115 if (__predict_true(pmap == pmap_kernel())) { 4116 if (__predict_true(va < kernel_virtual_end)) { 4117 ptep = pmap_kernel_pte(va); 4118 rv = pmap_extract_pte(ptep, va, pap, flagsp); 4119 } 4120 return rv; 4121 } 4122 4123 PMAP_LOCK(pmap); 4124 ptep = pmap_pte_lookup(pmap, va, &pt); 4125 if (__predict_true(ptep != NULL)) { 4126 rv = pmap_extract_pte(ptep, va, pap, flagsp); 4127 pmap_table_release(pmap, pt, NULL); 4128 } 4129 PMAP_UNLOCK(pmap); 4130 4131 return rv; 4132 } 4133 4134 bool 4135 pmap_extract(pmap_t pmap, vaddr_t va, paddr_t *pap) 4136 { 4137 return pmap_extract_info(pmap, va, pap, NULL); 4138 } 4139 4140 /* 4141 * vtophys: 4142 * 4143 * Dumber version of pmap_extract(pmap_kernel(), ...) 4144 */ 4145 paddr_t 4146 vtophys(vaddr_t va) 4147 { 4148 paddr_t pa; 4149 bool rv __diagused; 4150 4151 rv = pmap_extract_info(pmap_kernel(), va, &pa, NULL); 4152 KASSERT(rv); 4153 return rv ? pa : -1; 4154 } 4155 4156 /* 4157 * kvtop: 4158 * 4159 * Sigh. 4160 */ 4161 int 4162 kvtop(void *v) 4163 { 4164 return (int)vtophys((vaddr_t)v); 4165 } 4166 4167 /* 4168 * pmap_copy: [ INTERFACE ] 4169 * 4170 * Copy the mapping range specified by src_addr/len 4171 * from the source map to the range dst_addr/len 4172 * in the destination map. 4173 * 4174 * This routine is only advisory and need not do anything. 4175 */ 4176 /* call deleted in <machine/pmap.h> */ 4177 4178 /* 4179 * pmap_update: [ INTERFACE ] 4180 * 4181 * Require that all active physical maps contain no 4182 * incorrect entries NOW, by processing any deferred 4183 * pmap operations. 4184 */ 4185 void 4186 pmap_update(pmap_t pmap) 4187 { 4188 struct pmap_garbage g; 4189 4190 /* Process outstanding ATC invalidations. */ 4191 pmap_atc_process(); 4192 4193 /* 4194 * The kernel pmap is special and doesn't free page tables 4195 * or defer freeing of PV entries, because pmap_update() can 4196 * be called in an interrupt context. 4197 */ 4198 if (pmap == pmap_kernel()) { 4199 KASSERT(TAILQ_EMPTY(&pmap->pm_garbage.g_ptpages)); 4200 KASSERT(LIST_EMPTY(&pmap->pm_garbage.g_pvlist)); 4201 return; 4202 } 4203 4204 KASSERT(cpuinfo.ci_idepth == 0); 4205 PMAP_LOCK(pmap); 4206 pmap_garbage_transfer(&g, &pmap->pm_garbage); 4207 PMAP_UNLOCK(pmap); 4208 4209 pmap_garbage_collect(&g); 4210 } 4211 4212 /* 4213 * pmap_activate: [ INTERFACE ] 4214 * 4215 * Activate the pmap used by the specified process. This includes 4216 * reloading the MMU context of the current process, and marking 4217 * the pmap in use by the processor. 4218 */ 4219 void 4220 pmap_activate(struct lwp *l) 4221 { 4222 pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap; 4223 struct pmap_percpu *pc = pmap_percpu(); 4224 4225 KASSERT(l == curlwp); 4226 if (__predict_false(pc->pc_atc.atc_count != 0)) { 4227 pmap_evcnt(atc_activate); 4228 pmap_atc_process(); 4229 } 4230 4231 /* 4232 * Because the kernel has a separate root pointer, we don't 4233 * need to activate the kernel pmap. 4234 */ 4235 if (pmap != pmap_kernel()) { 4236 #if defined(MULTIPROCESSOR) 4237 int s = splhigh(); /* ensure pc_pmap is stable against IPIs */ 4238 pmap_t prev_pmap = pc->pc_pmap; 4239 if (prev_pmap != NULL && prev_pmap != pmap) { 4240 pc->pc_pmap = NULL; 4241 pmap_cpu_self_clear(prev_pmap); 4242 } 4243 _splx(s); /* skip softint processing here */ 4244 #endif 4245 PMAP_LOCK(pmap); 4246 pmap_cpu_self_set(pmap); 4247 if (pc->pc_lev1pa == pmap->pm_lev1pa) { 4248 pmap_evcnt(lev1map_act_hit); 4249 } else { 4250 pmap_evcnt(lev1map_act_miss); 4251 pmap_load_urp(pmap->pm_lev1pa); 4252 } 4253 PMAP_UNLOCK(pmap); 4254 pc->pc_pmap = pmap; 4255 } 4256 } 4257 4258 /* 4259 * pmap_deactivate: [ INTERFACE ] 4260 * 4261 * Mark that the pmap used by the specified process is no longer 4262 * in use by the processor. 4263 */ 4264 void 4265 pmap_deactivate(struct lwp *l) 4266 { 4267 /* 4268 * There's no real work for us to do here. We continue to live 4269 * on this lev1map until the next pass through pmap_activate() 4270 * on this CPU. All pmap_deactivate() calls are paired with a 4271 * nearly-immediate pmap_activate(). 4272 */ 4273 } 4274 4275 /* 4276 * pmap_zero_page: [ INTERFACE ] 4277 * 4278 * Zero the specified VM page by mapping the page into the kernel 4279 * and using memset() (or equivalent) to clear its contents. 4280 */ 4281 void 4282 pmap_zero_page(paddr_t pa) 4283 { 4284 const int flags = MMU_IS_HP_CLASS ? PMAP_NOCACHE|PMAP_WIRED 4285 : PMAP_WIRED; 4286 vaddr_t const dstva = cpuinfo.ci_tmpva_dst; 4287 pt_entry_t * const dst_ptep = pmap_kernel_pte(dstva); 4288 4289 /* Build the new PTE. */ 4290 const pt_entry_t dst_pte = 4291 pmap_make_pte(pa, UVM_PROT_READ | UVM_PROT_WRITE, flags); 4292 4293 /* Set the new PTE. */ 4294 KASSERT(! pte_valid_p(pte_load(dst_ptep))); 4295 pte_store(dst_ptep, dst_pte); 4296 /* XXX Possibly being over-cautious here; see pmap_kenter_pa(). */ 4297 TBIS(dstva); 4298 4299 /* Zero the page. */ 4300 zeropage((void *)dstva); 4301 4302 /* Invalidate the PTEs. */ 4303 pte_store(dst_ptep, 0); 4304 TBIS(dstva); 4305 } 4306 4307 /* 4308 * pmap_copy_page: [ INTERFACE ] 4309 * 4310 * Copy the specified VM page by mapping the page(s) into the kernel 4311 * and using memcpy() (or equivalent). 4312 */ 4313 void 4314 pmap_copy_page(paddr_t src, paddr_t dst) 4315 { 4316 const int flags = MMU_IS_HP_CLASS ? PMAP_NOCACHE|PMAP_WIRED 4317 : PMAP_WIRED; 4318 vaddr_t const srcva = cpuinfo.ci_tmpva_src; 4319 vaddr_t const dstva = cpuinfo.ci_tmpva_dst; 4320 pt_entry_t * const src_ptep = pmap_kernel_pte(srcva); 4321 pt_entry_t * const dst_ptep = pmap_kernel_pte(dstva); 4322 4323 /* Build the new PTEs. */ 4324 const pt_entry_t src_pte = 4325 pmap_make_pte(src, UVM_PROT_READ, flags); 4326 const pt_entry_t dst_pte = 4327 pmap_make_pte(dst, UVM_PROT_READ | UVM_PROT_WRITE, flags); 4328 4329 /* Set the new PTEs. */ 4330 KASSERT(! pte_valid_p(pte_load(src_ptep))); 4331 pte_store(src_ptep, src_pte); 4332 /* XXX Possibly being over-cautious here; see pmap_kenter_pa(). */ 4333 TBIS(srcva); 4334 4335 KASSERT(! pte_valid_p(pte_load(dst_ptep))); 4336 pte_store(dst_ptep, dst_pte); 4337 /* XXX Possibly being over-cautious here; see pmap_kenter_pa(). */ 4338 TBIS(dstva); 4339 4340 /* Copy the page. */ 4341 copypage((void *)srcva, (void *)dstva); 4342 4343 /* Invalidate the PTEs. */ 4344 pte_store(src_ptep, 0); 4345 TBIS(srcva); 4346 4347 pte_store(dst_ptep, 0); 4348 TBIS(dstva); 4349 } 4350 4351 /* 4352 * pmap_testbit: 4353 * 4354 * Test the modified / referenced bits of a physical page. 4355 */ 4356 static bool 4357 pmap_testbit(struct vm_page *pg, pt_entry_t bit) 4358 { 4359 struct pv_entry *pv; 4360 pt_entry_t combined_pte = 0; 4361 4362 PVH_LOCK(pg); 4363 4364 combined_pte |= VM_MDPAGE_UM(pg); 4365 4366 for (pv = VM_MDPAGE_PVS(pg); 4367 (combined_pte & bit) == 0 && pv != NULL; 4368 pv = pv->pv_next) { 4369 combined_pte |= pte_load(pmap_pv_pte(pv)); 4370 } 4371 4372 VM_MDPAGE_ADD_UM(pg, combined_pte); 4373 4374 PVH_UNLOCK(pg); 4375 4376 return (combined_pte & bit) != 0; 4377 } 4378 4379 /* 4380 * pmap_is_referenced: [ INTERFACE ] 4381 * 4382 * Return whether or not the specified physical page has been referenced 4383 * by any physical maps. 4384 */ 4385 bool 4386 pmap_is_referenced(struct vm_page *pg) 4387 { 4388 return pmap_testbit(pg, PTE_U); 4389 } 4390 4391 /* 4392 * pmap_is_modified: [ INTERFACE ] 4393 * 4394 * Return whether or not the specified physical page has been modified 4395 * by any physical maps. 4396 */ 4397 bool 4398 pmap_is_modified(struct vm_page *pg) 4399 { 4400 return pmap_testbit(pg, PTE_M); 4401 } 4402 4403 /* 4404 * pmap_changebit: 4405 * 4406 * Test-and-change various bits (including mod/ref bits). 4407 * Returns the accumulated previously-set PTE bits. 4408 */ 4409 static pt_entry_t 4410 pmap_changebit(struct vm_page *pg, pt_entry_t set, pt_entry_t mask) 4411 { 4412 struct pv_entry *pv; 4413 pt_entry_t *ptep, combined_pte = 0, opte, npte; 4414 paddr_t pa = VM_PAGE_TO_PHYS(pg); 4415 4416 PVH_LOCK(pg); 4417 4418 /* 4419 * Since we need to report if the page was mod/ref'd before 4420 * we cleared the bit, we need to seed ourself with the current 4421 * state in the vm_page in the event there are no mappings 4422 * left to enumerate. 4423 */ 4424 combined_pte |= VM_MDPAGE_UM(pg); 4425 4426 /* 4427 * Since we're running over every mapping for the page anyway, 4428 * we might as well synchronize any attribute bits that we're 4429 * not clearing. 4430 */ 4431 for (pv = VM_MDPAGE_PVS(pg); pv != NULL; pv = pv->pv_next) { 4432 pt_entry_t actual; 4433 ptep = pmap_pv_pte(pv); 4434 for (opte = pte_load(ptep);; opte = actual) { 4435 npte = (opte | set) & mask; 4436 combined_pte |= opte; 4437 4438 if (opte == npte) { 4439 break; 4440 } 4441 4442 actual = pte_update(ptep, opte, npte); 4443 if (__predict_true(actual == opte)) { 4444 /* 4445 * We must flush the ATC even if it's not a 4446 * "critical" bit, because the MMU will only 4447 * write-back a PTE when the U or M bits 4448 * transition from 0 to 1. If we clear them 4449 * in our tracking structure and in the tables 4450 * in memory but not the ATC, then a sticky 4451 * ATC entry might cause a future U or M 4452 * event to be missed. 4453 * 4454 * Cache push is only necessary if 4455 * the critical page attribute (W, CM) 4456 * were changed. If we're just clearing 4457 * U or M, no push is necessary. 4458 */ 4459 pmap_atc_invalidate_pv(pv, pa, 4460 ((opte ^ npte) & PTE_CRIT_BITS) 4461 ? ATC_F_BC : 0); 4462 break; 4463 } 4464 /* Lost race, try again. */ 4465 } 4466 } 4467 4468 /* 4469 * Update any attributes we looked at, clear the ones we're clearing. 4470 */ 4471 VM_MDPAGE_SET_UM(pg, (combined_pte | set) & mask); 4472 4473 PVH_UNLOCK(pg); 4474 4475 pmap_atc_process(); 4476 4477 return combined_pte; 4478 } 4479 4480 /* 4481 * pmap_clear_modify: [ INTERFACE ] 4482 * 4483 * Clear the modify bits on the specified physical page. 4484 */ 4485 bool 4486 pmap_clear_modify(struct vm_page *pg) 4487 { 4488 return (pmap_changebit(pg, 0, (pt_entry_t)~PTE_M) & PTE_M) != 0; 4489 } 4490 4491 /* 4492 * pmap_clear_reference: [ INTERFACE ] 4493 * 4494 * Clear the reference bit on the specified physical page. 4495 */ 4496 bool 4497 pmap_clear_reference(struct vm_page *pg) 4498 { 4499 return (pmap_changebit(pg, 0, (pt_entry_t)~PTE_U) & PTE_U) != 0; 4500 } 4501 4502 /* 4503 * pmap_phys_address: [ INTERFACE ] 4504 * 4505 * Return the physical address corresponding to the specified 4506 * cookie. Used by the device pager to decode a device driver's 4507 * mmap entry point return value. 4508 */ 4509 paddr_t 4510 pmap_phys_address(paddr_t cookie) 4511 { 4512 return m68k_ptob(cookie); 4513 } 4514 4515 /* 4516 * The kernel_lev1map is a little tricky. This one is used only by 4517 * the primary CPU. Each secondary CPU has its own lev1map and its 4518 * own lev2 and lev3 table that map its cpu_info at a fixed address 4519 * (CPUINFO_VA). These are all allocated in a single page that also 4520 * holds that CPU's cpu_info. It looks like this: 4521 * 4522 * (high address) 4523 * +----------------------------------------+ 4524 * | | 4525 * | | 4526 * | CPUn lev1map (512 bytes) | 4527 * | | 4528 * | | 4529 * +----------------------------------------+ 4530 * | | 4531 * | | 4532 * | CPUn CPUINFO_VA lev2 table (512 bytes) | 4533 * | | 4534 * | | 4535 * +----------------------------------------+ 4536 * | | 4537 * | CPUn CPUINFO_VA lev3 table (256 bytes) | 4538 * | | 4539 * +----------------------------------------+ 4540 * / / 4541 * \ \ 4542 * / / 4543 * \ (unused space ??? bytes) \ 4544 * / / 4545 * \ \ 4546 * / / 4547 * +----------------------------------------+ 4548 * | | 4549 * | | 4550 * | | 4551 * | CPUn cpu_info (<= 2048 bytes) | 4552 * | | 4553 * | | 4554 * | | 4555 * +----------------------------------------+ 4556 * (low address) 4557 * 4558 * Each secondary CPU gets its own kernel lev1map to stash in its %srp, 4559 * but it is initialized with the contents of the main kernel_lev1map 4560 * except for the L1 slot that maps CPUINFO_VA. That particular slot 4561 * points to that CPU's CPUINFO_VA lev2 table, which in turn has a 4562 * slot that points to that CPU's CPUINFO_VA lev3 table, which has a 4563 * slot that maps this particular page at CPUINFO_VA. 4564 * 4565 * AND BECAUSE YOU ASKED! YES! If we happen to be running on a 4566 * MP system that has a 68020+68851 or a 68030, then the MMU will 4567 * be configured to use the same 3-level table structure as the 4568 * 68040/68060, and thus we can use this same layout. 4569 */ 4570 static pt_entry_t *kernel_lev1map __read_mostly; 4571 4572 #if defined(PMAP_ENABLE_LOCKS) 4573 static kmutex_t pmap_growkernel_lock __cacheline_aligned; 4574 4575 /* 4576 * pmap_percpu_alloc: 4577 * 4578 * Allocate a per-cpu page. 4579 */ 4580 struct cpu_info * 4581 pmap_percpu_alloc(void) 4582 { 4583 /* 4584 * We're supposed to map PTs cache-inhibited, but we do not want 4585 * the cpu_info to be cache-inhibited, so what do we do? 4586 * 4587 * Well, these PTs aren't going to be accessed very frequently 4588 * at all. Once at setup, and maybe again when pmap_growkernel() 4589 * is called. So we will map the page as write-through. 4590 */ 4591 vaddr_t va = pmap_vpage_alloc(true/*nowait*/, PMAP_WRITE_THROUGH, NULL); 4592 if (__predict_false(va == 0)) { 4593 panic("%s: unable to allocate per-cpu page", __func__); 4594 } 4595 4596 return (struct cpu_info *)va; 4597 } 4598 4599 /* 4600 * pmap_percpu_init: 4601 * 4602 * Initialize a per-cpu page. Must be called only after the 4603 * cpu_info associated with this per-cpu page has been linked 4604 * into cpu_info_list. 4605 */ 4606 void 4607 pmap_percpu_init(struct cpu_info *ci) 4608 { 4609 vaddr_t va = (vaddr_t)ci, lev1, lev2, lev3; 4610 paddr_t pgpa, lev1pa, lev2pa, lev3pa; 4611 4612 KASSERT(! CPU_IS_PRIMARY(ci)); 4613 4614 if (! pmap_extract(pmap_kernel(), va, &pgpa)) { 4615 panic("%s: can't get PA for per-cpu VA=0x%lx", 4616 __func__, va); 4617 } 4618 4619 /* Compute the addresses of the L1, L2, and L3 tables. */ 4620 lev1 = (va + PAGE_SIZE) - TBL40_L1_SIZE; 4621 lev1pa = (pgpa + PAGE_SIZE) - TBL40_L1_SIZE; 4622 4623 lev2 = lev1 - TBL40_L2_SIZE; 4624 lev2pa = lev1pa - TBL40_L2_SIZE; 4625 4626 lev3 = lev2 - TBL40_L3_SIZE; 4627 lev3pa = lev2pa - TBL40_L3_SIZE; 4628 4629 mutex_enter(&pmap_growkernel_lock); 4630 4631 /* Copy the base kernel_lev1map into the lev1 map. */ 4632 memcpy((void *)lev1, kernel_lev1map, TBL40_L1_SIZE); 4633 4634 /* Enter the lev2 table into the lev1 map. */ 4635 ((pt_entry_t *)lev1)[LA40_RI(CPUINFO_VA)] = pmap_ste_proto | lev2pa; 4636 4637 /* Enter the lev3 table into the lev2 table. */ 4638 ((pt_entry_t *)lev2)[LA40_PI(CPUINFO_VA)] = pmap_ste_proto | lev3pa; 4639 4640 /* Enter the page into the lev3 table. */ 4641 ((pt_entry_t *)lev3)[LA40_PGI(CPUINFO_VA)] = 4642 pmap_make_pte(pgpa, VM_PROT_READ | VM_PROT_WRITE, 4643 PMAP_WRITE_THROUGH); 4644 4645 mutex_exit(&pmap_growkernel_lock); 4646 4647 /* Allocate per-cpu temp VAs. */ 4648 va = uvm_km_alloc(kernel_map, PAGE_SIZE * 3, 0, UVM_KMF_VAONLY); 4649 if (__predict_false(va == 0)) { 4650 panic("%s: unable to allocate per-cpu VAs", __func__); 4651 } 4652 4653 ci->ci_tmpva_vmmap = va; 4654 va += PAGE_SIZE; 4655 ci->ci_tmpva_src = va; 4656 va += PAGE_SIZE; 4657 ci->ci_tmpva_dst = va; 4658 va += PAGE_SIZE; 4659 4660 /* Fin. */ 4661 } 4662 #endif /* MULTIPROCESSOR */ 4663 4664 /* 4665 * pmap_percpu_start: 4666 * 4667 * Tasks to do for the pmap when a CPU starts. 4668 */ 4669 void 4670 pmap_percpu_start(void) 4671 { 4672 /* Load the null user segment table. */ 4673 pmap_load_urp(null_segtab_pa); 4674 4675 #if defined(MULTIPROCESSOR) 4676 /* 4677 * We mark the kernel pmap as always active on every CPU 4678 * in order to avoid having to special-case it later. 4679 */ 4680 PMAP_LOCK(pmap_kernel()); 4681 pmap_cpu_self_set(pmap_kernel()); 4682 PMAP_UNLOCK(pmap_kernel()); 4683 #endif 4684 } 4685 4686 /* 4687 * pmap_growkernel_alloc_page: 4688 * 4689 * Helper for pmap_growkernel(). 4690 */ 4691 static paddr_t 4692 pmap_growkernel_alloc_page(void) 4693 { 4694 /* 4695 * XXX Needs more work if we're going to do this during 4696 * XXX early bootstrap. 4697 */ 4698 if (! uvm.page_init_done) { 4699 panic("%s: called before UVM initialized", __func__); 4700 } 4701 4702 struct vm_page *pg = pmap_page_alloc(true/*nowait*/); 4703 if (pg == NULL) { 4704 panic("%s: out of memory", __func__); 4705 } 4706 4707 paddr_t pa = VM_PAGE_TO_PHYS(pg); 4708 pmap_zero_page(pa); 4709 #if MMU_CONFIG_68040_CLASS 4710 if (MMU_IS_68040_CLASS) { 4711 DCFP(pa); 4712 } 4713 #endif 4714 return pa; 4715 } 4716 4717 /* 4718 * pmap_growkernel_insert_lev1map: 4719 * 4720 * Helper for pmap_growkernel_link_kptpage(). 4721 */ 4722 static void 4723 pmap_growkernel_insert_lev1map(vaddr_t va, pt_entry_t lev1ste) 4724 { 4725 unsigned int idx = LA40_RI(va); 4726 4727 kernel_lev1map[idx] = lev1ste; 4728 4729 #if defined(MULTIPROCESSOR) 4730 CPU_INFO_ITERATOR cii; 4731 struct cpu_info *ci; 4732 vaddr_t pgva, lev1; 4733 4734 for (CPU_INFO_FOREACH(cii, ci)) { 4735 if (CPU_IS_PRIMARY(ci)) { 4736 continue; 4737 } 4738 pgva = (vaddr_t)ci; 4739 lev1 = (pgva + PAGE_SIZE) - TBL40_L1_SIZE; 4740 ((pt_entry_t *)lev1)[idx] = lev1ste; 4741 } 4742 #endif /* MULTIPROCESSOR */ 4743 } 4744 4745 /* 4746 * pmap_growkernel_link_kptpage: 4747 * 4748 * Helper for pmap_growkernel(). 4749 */ 4750 static void 4751 pmap_growkernel_link_kptpage(vaddr_t va, paddr_t ptp_pa) 4752 { 4753 /* 4754 * This is trivial for the 2-level MMU configuration. 4755 */ 4756 if (MMU_USE_2L) { 4757 KASSERT((kernel_lev1map[LA2L_RI(va)] & DT51_SHORT) == 0); 4758 kernel_lev1map[LA2L_RI(va)] = pmap_ste_proto | ptp_pa; 4759 return; 4760 } 4761 4762 /* 4763 * N.B. pmap_zero_page() is used in this process, which 4764 * uses cpuinfo.ci_tmpva_dst. cpuinfo.ci_tmpva_src is available 4765 * for our use, however, so that's what we used to temporarily 4766 * map inner segment table pages. 4767 */ 4768 const vaddr_t stpg_va = cpuinfo.ci_tmpva_src; 4769 pt_entry_t *stpg_pte = pmap_kernel_pte(stpg_va); 4770 4771 paddr_t stpa, stpg_pa, stpgoff; 4772 paddr_t pa = ptp_pa, end_pa = ptp_pa + PAGE_SIZE; 4773 pt_entry_t *stes; 4774 4775 for (; pa < end_pa; va += NBSEG3L, pa += TBL40_L3_SIZE) { 4776 if ((kernel_lev1map[LA40_RI(va)] & UTE40_RESIDENT) == 0) { 4777 /* Level-2 table for this segment needed. */ 4778 if (kernel_stnext_pa == kernel_stnext_endpa) { 4779 /* 4780 * No more slots left in the last page 4781 * we allocated for segment tables. Grab 4782 * another one. 4783 */ 4784 kernel_stnext_pa = pmap_growkernel_alloc_page(); 4785 kernel_stnext_endpa = 4786 kernel_stnext_pa + PAGE_SIZE; 4787 pmap_evcnt_impl(nkstpages_current); 4788 } 4789 pmap_growkernel_insert_lev1map(va, 4790 pmap_ste_proto | kernel_stnext_pa); 4791 kernel_stnext_pa += TBL40_L2_SIZE; 4792 } 4793 stpa = kernel_lev1map[LA40_RI(va)] & UTE40_PTA; 4794 stpg_pa = m68k_trunc_page(stpa); 4795 4796 pte_store(stpg_pte, 4797 pmap_make_pte(stpg_pa, UVM_PROT_READ | UVM_PROT_WRITE, 4798 PMAP_WIRED | PMAP_NOCACHE)); 4799 TBIS(stpg_va); 4800 4801 stpgoff = stpa - stpg_pa; 4802 stes = (pt_entry_t *)(stpg_va + stpgoff); 4803 stes[LA40_PI(va)] = pmap_ste_proto | pa; 4804 } 4805 pte_store(stpg_pte, 0); 4806 TBIS(stpg_va); 4807 } 4808 4809 /* 4810 * pmap_growkernel: [ INTERFACE ] 4811 * 4812 * Grow the kernel address space. This is a hint from the 4813 * upper layer to pre-allocate more kernel PT pages. 4814 */ 4815 vaddr_t 4816 pmap_growkernel(vaddr_t maxkvaddr) 4817 { 4818 PMAP_MUTEX_ENTER(&pmap_growkernel_lock); 4819 4820 KASSERT((kernel_virtual_end & PTPAGEVAOFS) == 0); 4821 4822 vaddr_t new_maxkva = pmap_round_ptpage(maxkvaddr); 4823 if (new_maxkva < kernel_virtual_end) { 4824 /* 4825 * Great news! We already have what we need to map 4826 * the requested max address. This happens one during 4827 * early bootstrap before UVM's notion of "maxkvaddr" 4828 * has been initialized. 4829 */ 4830 new_maxkva = kernel_virtual_end; 4831 goto done; 4832 } 4833 4834 if (new_maxkva > kernel_virtual_max) { 4835 panic("%s: out of kernel VA space (req=0x%08lx limit=0x%08lx)", 4836 __func__, maxkvaddr, kernel_virtual_max); 4837 } 4838 4839 /* 4840 * Allocate PT pages and link them into the MMU tree as we 4841 * go. 4842 */ 4843 vaddr_t va, ptp_pa, pte_va; 4844 for (va = kernel_virtual_end; va < new_maxkva; va += PTPAGEVASZ) { 4845 /* Allocate page and link it into the MMU tree. */ 4846 ptp_pa = pmap_growkernel_alloc_page(); 4847 pmap_growkernel_link_kptpage(va, ptp_pa); 4848 pmap_evcnt_impl(nkptpages_current); 4849 4850 /* Map the PT page into the kernel PTE array. */ 4851 pte_va = (vaddr_t)pmap_kernel_pte(va); 4852 pmap_kenter_pa(pte_va, ptp_pa, UVM_PROT_READ | UVM_PROT_WRITE, 4853 PMAP_WIRED | PMAP_NOCACHE); 4854 /* We invalidate_all below. */ 4855 } 4856 kernel_virtual_end = new_maxkva; 4857 done: 4858 pmap_evcnt_set_impl(maxkva, new_maxkva); 4859 pmap_evcnt_set_impl(kvalimit, kernel_virtual_max); 4860 4861 PMAP_MUTEX_EXIT(&pmap_growkernel_lock); 4862 4863 /* Also catches new lev1map entries on secondary CPUs. */ 4864 pmap_atc_invalidate_all(ATC_F_SUPER | ATC_F_DC); 4865 pmap_atc_process(); 4866 4867 return new_maxkva; 4868 } 4869 4870 /* 4871 * pmap_prefer: [ INTERFACE ] 4872 * 4873 * Attempt to arrange for pages at a given VM object offset 4874 * to occupy the same virtually-addressed cache footprint 4875 * in order to avoid cache aliases. 4876 */ 4877 #if MMU_CONFIG_HP_CLASS 4878 static struct evcnt pmap_prefer_nochange_ev = 4879 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "nochange"); 4880 static struct evcnt pmap_prefer_change_ev = 4881 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "change"); 4882 4883 EVCNT_ATTACH_STATIC(pmap_prefer_change_ev); 4884 EVCNT_ATTACH_STATIC(pmap_prefer_nochange_ev); 4885 #endif 4886 void 4887 pmap_prefer(vaddr_t hint, vaddr_t *vap, int td) 4888 { 4889 #if MMU_CONFIG_HP_CLASS 4890 if (MMU_IS_HP_CLASS) { 4891 vaddr_t va = *vap; 4892 ptrdiff_t diff = (hint - va) & pmap_aliasmask; 4893 4894 if (diff == 0) { 4895 pmap_evcnt(prefer_nochange); 4896 } else { 4897 pmap_evcnt(prefer_change); 4898 if (__predict_false(td)) { 4899 va -= pmap_aliasmask + 1; 4900 } 4901 *vap = va + diff; 4902 } 4903 } 4904 #endif 4905 } 4906 4907 /* 4908 * pmap_procwr: [ INTERFACE ] 4909 * 4910 * Perform any cache synchronization required after writing 4911 * to a process's address space. 4912 */ 4913 void 4914 pmap_procwr(struct proc *p, vaddr_t va, size_t len) 4915 { 4916 /* 4917 * This is just a wrapper around the "cachectl" machdep 4918 * system call. 4919 * 4920 * XXX This is kind of gross, to be honest. 4921 */ 4922 (void)cachectl1(0x80000004, va, len, p); 4923 } 4924 4925 /* 4926 * pmap_write_text_enter: 4927 * 4928 * Temporarily map a page of kernel text read-write for the 4929 * kernel debugger. 4930 */ 4931 bool 4932 pmap_write_text_enter(vaddr_t pgva, struct pmap_write_text_context *ctx) 4933 { 4934 pt_entry_t *pte = pmap_kernel_pte(pgva); 4935 4936 pt_entry_t opte = pte_load(pte); 4937 if (! pte_valid_p(opte)) { 4938 return false; 4939 } 4940 4941 /* 4942 * N.B. we use the 68851 PTE bit names here, but in 4943 * the case of the kernel text, it all works out vis 4944 * a vis the 68040 PTE bits. 4945 * 4946 * Note the mapping is cache-inhibited to save hair. 4947 */ 4948 pt_entry_t npte = (opte & ~PTE51_WP) | PTE51_CI; 4949 pte_store(pte, npte); 4950 TBIS(pgva); 4951 4952 ctx->pgva = pgva; 4953 ctx->ptep = pte; 4954 ctx->opte = opte; 4955 4956 return true; 4957 } 4958 4959 /* 4960 * pmap_write_text_exit: 4961 * 4962 * Undo the effects of pmap_write_text_enter(). 4963 */ 4964 void 4965 pmap_write_text_exit(struct pmap_write_text_context *ctx) 4966 { 4967 pte_store(ctx->ptep, ctx->opte); 4968 TBIS(ctx->pgva); 4969 ICIA(); 4970 } 4971 4972 static paddr_t kernel_reloc_offset __read_mostly; 4973 static vaddr_t kernel_reloc_end __read_mostly; 4974 4975 /* 4976 * pmap_init_kcore_hdr: 4977 * 4978 * Initialize the m68k kernel crash dump header with information 4979 * necessary to perform KVA -> phys translations. 4980 * 4981 * Returns a pointer to the crash dump RAM segment entries for 4982 * machine-specific code to initialize. 4983 */ 4984 phys_ram_seg_t * 4985 pmap_init_kcore_hdr(cpu_kcore_hdr_t *h) 4986 { 4987 struct gen68k_kcore_hdr *m = &h->un._gen68k; 4988 4989 memset(h, 0, sizeof(*h)); 4990 4991 /* 4992 * Initialize the `dispatcher' portion of the header. 4993 */ 4994 strcpy(h->name, "gen68k"); 4995 h->page_size = PAGE_SIZE; 4996 h->kernbase = VM_MIN_KERNEL_ADDRESS; 4997 4998 /* Fixed relocation information. */ 4999 m->reloc = kernel_reloc_offset; 5000 m->relocend = kernel_reloc_end; 5001 5002 /* 5003 * Fill in information about our MMU configuration. 5004 * 5005 * We essentially pretend to be a 68851 as far as table- 5006 * walks are concerned. 5007 * 5008 * We provide the kernel's MMU_* constant so that the TT 5009 * registers can be interpreted correctly. 5010 */ 5011 m->mmutype = mmutype; 5012 m->tcr = MMU_USE_3L ? MMU51_3L_TCR_BITS : MMU51_TCR_BITS; 5013 m->srp[0] = MMU51_SRP_BITS; 5014 m->srp[1] = Sysseg_pa; 5015 5016 #if MMU_CONFIG_68040_CLASS 5017 if (MMU_IS_68040_CLASS) { 5018 m->itt0 = mmu_tt40[MMU_TTREG_ITT0]; 5019 m->itt1 = mmu_tt40[MMU_TTREG_ITT1]; 5020 m->tt0 = mmu_tt40[MMU_TTREG_DTT0]; 5021 m->tt1 = mmu_tt40[MMU_TTREG_DTT1]; 5022 } 5023 #endif 5024 #if defined(M68K_MMU_68030) 5025 if (mmutype == MMU_68030) { 5026 m->tt0 = mmu_tt30[MMU_TTREG_TT0]; 5027 m->tt1 = mmu_tt30[MMU_TTREG_TT1]; 5028 } 5029 #endif 5030 5031 return m->ram_segs; 5032 } 5033 5034 /* 5035 * pmap_fault: 5036 * 5037 * Pmap hook for trap()'s page fault handler. We intercept this 5038 * to take care of any lazy operations that can be resolved at page 5039 * fault time. 5040 */ 5041 int 5042 pmap_fault(struct vm_map *map, vaddr_t va, vm_prot_t prot) 5043 { 5044 struct pmap_percpu * const pc = pmap_percpu(); 5045 pmap_t pmap = vm_map_pmap(map); 5046 5047 if (pmap != pmap_kernel()) { 5048 /* 5049 * If the pmap gained a lev1map but this CPU is still 5050 * on the null_segtab, then load the new lev1map and 5051 * try again. 5052 */ 5053 if (pc->pc_lev1pa != pmap->pm_lev1pa) { 5054 KASSERT(pc->pc_lev1pa == null_segtab_pa); 5055 pmap_evcnt(lev1map_fault_null); 5056 pmap_load_urp(pmap->pm_lev1pa); 5057 return 0; 5058 } 5059 } 5060 5061 return uvm_fault(map, va, prot); 5062 } 5063 5064 /***************************** PMAP BOOTSTRAP ********************************/ 5065 5066 /* 5067 * pmap_pa_has_static_mapping: 5068 * 5069 * Returns true if the specified PA (and length) has a static 5070 * mapping, and what that mapping is and its properties. 5071 */ 5072 bool 5073 pmap_pa_has_static_mapping(paddr_t pa, size_t len, vm_prot_t prot, 5074 vaddr_t *vap, int *flagsp) 5075 { 5076 #if defined(M68030) || defined(M68040) || defined(M68060) 5077 /* First, check for Transparent Translation. */ 5078 if (cputype >= CPU_68030 && 5079 mmu_range_is_tt(pa, len, prot, flagsp)) { 5080 *vap = pa; 5081 return true; 5082 } 5083 #endif 5084 5085 const struct pmap_bootmap *pmbm = machine_bootmap; 5086 paddr_t lastpg = m68k_btop(pa + (len - 1)); 5087 paddr_t firstpg = m68k_btop(pa); 5088 paddr_t tfirst, tlast; 5089 vaddr_t tblva; 5090 bool need_write = !!(prot & UVM_PROT_WRITE); 5091 5092 for (; pmbm->pmbm_vaddr != (vaddr_t)-1; pmbm++) { 5093 if (pmbm->pmbm_flags & (PMBM_F_VAONLY|PMBM_F_KEEPOUT)) { 5094 continue; 5095 } 5096 if (pmbm->pmbm_flags & PMBM_F_FIXEDVA) { 5097 tblva = pmbm->pmbm_vaddr; 5098 } else { 5099 tblva = *pmbm->pmbm_vaddr_ptr; 5100 } 5101 tfirst = m68k_btop(pmbm->pmbm_paddr); 5102 tlast = m68k_btop(pmbm->pmbm_paddr + (pmbm->pmbm_size - 1)); 5103 5104 if (firstpg >= tfirst && lastpg <= tlast) { 5105 if (need_write && 5106 (pmbm->pmbm_flags & PMBM_F_RO) != 0) { 5107 return false; 5108 } 5109 *vap = tblva + (pa - pmbm->pmbm_paddr); 5110 if (pmbm->pmbm_flags & PMBM_F_CI) { 5111 *flagsp = PMAP_NOCACHE; 5112 } else { 5113 *flagsp = 0; 5114 } 5115 return true; 5116 } 5117 } 5118 5119 return false; 5120 } 5121 5122 /* 5123 * pmap_va_is_static_mapping: 5124 * 5125 * Returns true if the specified VA (and length) is a static 5126 * mapping. 5127 */ 5128 bool 5129 pmap_va_is_static_mapping(vaddr_t va, size_t len) 5130 { 5131 #if defined(M68030) || defined(M68040) || defined(M68060) 5132 int map_flags; 5133 5134 /* First, check for Transparent Translation. */ 5135 if (cputype >= CPU_68030 && 5136 (mmu_range_is_tt(va, len, UVM_PROT_READ, &map_flags) || 5137 mmu_range_is_tt(va, len, UVM_PROT_WRITE, &map_flags) || 5138 mmu_range_is_tt(va, len, UVM_PROT_EXEC, &map_flags))) { 5139 return true; 5140 } 5141 #endif 5142 5143 const struct pmap_bootmap *pmbm = machine_bootmap; 5144 vaddr_t lastpg = m68k_btop(va + (len - 1)); 5145 vaddr_t firstpg = m68k_btop(va); 5146 vaddr_t tblva, tfirst, tlast; 5147 for (; pmbm->pmbm_vaddr != (vaddr_t)-1; pmbm++) { 5148 if (pmbm->pmbm_flags & (PMBM_F_VAONLY|PMBM_F_KEEPOUT)) { 5149 continue; 5150 } 5151 if (pmbm->pmbm_flags & PMBM_F_FIXEDVA) { 5152 tblva = pmbm->pmbm_vaddr; 5153 } else { 5154 tblva = *pmbm->pmbm_vaddr_ptr; 5155 } 5156 tfirst = m68k_btop(tblva); 5157 tlast = m68k_btop(tblva + (pmbm->pmbm_size - 1)); 5158 5159 if (firstpg >= tfirst && lastpg <= tlast) { 5160 return true; 5161 } 5162 } 5163 5164 return false; 5165 } 5166 5167 /* 5168 * The kernel virtual address space layout that this implementation is tuned 5169 * for assumes that KVA space begins at $0000.0000, that the static kernel 5170 * image (text/data/bss, etc.) resides at or near the bottom of this space, 5171 * and that all additional KVA that's mapped by PTEs grows upwards from there. 5172 * 5173 * Regions mapped by Transparent Translation registers (68030 and up) 5174 * are assumed to lie beyond where the KVA space is expected to grow. When 5175 * we encounter these regions in the machine_bootmap[] (represented by a 5176 * KEEPOUT entry), we clamp the maximum KVA to prevent its growth into that 5177 * region. The TT mechanism is not terribly precise, and only supports 5178 * VA==PA mappings, so it's only really suitable for device regions that 5179 * are in the upper reaches of the physical address space (at or beyond 1GB 5180 * or so). 5181 * 5182 * This implementation certainly could be adjusted to work with other address 5183 * space layouts, but the assumption asserted here is a bit baked-in. 5184 */ 5185 __CTASSERT(VM_MIN_KERNEL_ADDRESS == 0); 5186 5187 /* 5188 * The virtual kernel PTE array covers the entire 4GB kernel supervisor 5189 * address space, but is sparsely populated. The amount of VA space required 5190 * for this linear array is: 5191 * 5192 * (4GB / PAGE_SIZE) * sizeof(pt_entry_t) 5193 * -or- 5194 * 4KB: 4MB (1024 pages) 5195 * 8KB: 2MB (512 pages) 5196 * 5197 * To avoid doing 64-bit math, we calculate it like so: 5198 * 5199 * ((0xffffffff >> PAGE_SHIFT) + 1) * sizeof(pt_entry_t) 5200 * 5201 * The traditional name for this virtual array is "Sysmap". 5202 */ 5203 #define SYSMAP_VA_SIZE (((0xffffffffU >> PAGE_SHIFT) + 1) * sizeof(pt_entry_t)) 5204 5205 /* 5206 * In the Hibler/Utah pmap, the kernel PTE array was placed right near 5207 * the very top of the kernel virtual address space. This was because 5208 * of the hp300's unique physical memory arrangement: the last page of 5209 * memory is always located at PA $FFFF.F000 and the physical address 5210 * of the beginning of RAM varied based on the RAM size. This meant that 5211 * VA $FFFF.F000 is a convenient place to map the RAM VA==PA, making 5212 * transition between "MMU off" and "MMU on" (and vice versa) easier. 5213 * Since VA $FFFF.F000 was already going to be mapped, it made sense to 5214 * put something else along side of it in order to minimize waste in 5215 * PT pages. 5216 * 5217 * As noted above, this implementation is tuned for a growing-from-0 5218 * virtual space layout. However, we have a special case for this 5219 * particular requirement: if a platform defines SYSMAP_VA, then we 5220 * will assume it is as a high address, place the kernel PTE array at 5221 * that KVA, and ensure sufficient page tables to map from that VA until 5222 * the very end of the 4GB supervisor address space. These tables will 5223 * be allocated before the machine_bootmap[] is processed to map physical 5224 * addresses, thus allowing the machine_bootmap[] use it to map physical 5225 * addresses into one of these high virtual addresses if necessary. The 5226 * beginning of this region will also serve to clamp the maximum kernel 5227 * virtual address, in the same way as a KEEPOUT region in machine_bootmap[]. 5228 * 5229 * For reference, the traditional hp300 definition is: 5230 * 5231 * #define SYSMAP_VA ((vaddr_t)(0-PAGE_SIZE*NPTEPG*2)) 5232 * 5233 * ...and because the hp300 always used a 4KB page size (restriction 5234 * of HP MMU), this is: 0 - 4096*1024*2 5235 * -> 0 - 8388608 (8MB) 5236 * -> $FF80.0000 5237 * 5238 * Unfortunately (for the hp300), this means 2 PT pages for the top of 5239 * the address space (in the 2-level case), but that's unavoidable anyway 5240 * because of the last page being a separate mapping and the kernel PTE 5241 * array needs 4MB of space on its own. 5242 */ 5243 5244 static vaddr_t lwp0uarea __read_mostly; 5245 paddr_t lwp0upa __read_mostly = (paddr_t)-1; 5246 5247 /* XXX Doesn't belong here. */ 5248 paddr_t avail_start __read_mostly;/* PA of first available page */ 5249 paddr_t avail_end __read_mostly; /* PA of last available page */ 5250 5251 extern char * kernel_text; 5252 extern char * etext; 5253 extern void * msgbufaddr; 5254 5255 /* 5256 * pmap_bootstrap1: 5257 * 5258 * Phase 1 of bootstrapping virtual memory. This is called before 5259 * the MMU is enabled to set up the initial kernel MMU tables and 5260 * allocate other important data structures. 5261 * 5262 * Because the MMU has not yet been turned on, and we don't know if 5263 * we're running VA==PA, we have to manually relocate all global 5264 * symbol references. 5265 * 5266 * Arguments: nextpa Physical address immediately 5267 * following the kernel / symbols / 5268 * etc. This will be page-rounded 5269 * before use. 5270 * 5271 * reloff VA<->PA relocation offset 5272 * 5273 * Returns: nextpa Updated value after all of the 5274 * allocations performed. 5275 */ 5276 paddr_t __attribute__((no_instrument_function)) 5277 pmap_bootstrap1(paddr_t nextpa, paddr_t reloff) 5278 { 5279 paddr_t stnext_endpa, stnext_pa; 5280 paddr_t pa, kernimg_endpa, kern_lev1pa; 5281 vaddr_t va, nextva, kern_lev1va; 5282 pt_entry_t *pte, *epte; 5283 const struct pmap_bootmap *pmbm; 5284 int entry_count = 0; 5285 5286 #ifdef SYSMAP_VA 5287 #define VA_RANGE_DEFAULT 0 5288 #define VA_RANGE_KPTES 1 5289 #define INIT_NRANGES 2 5290 #else 5291 #define VA_RANGE_DEFAULT 0 5292 #define VA_RANGE_KPTES 0 5293 #define INIT_NRANGES 1 5294 #endif 5295 #define MAX_RANGES 8 5296 5297 struct va_range { 5298 vaddr_t start_va; 5299 vaddr_t end_va; 5300 paddr_t start_ptp; 5301 paddr_t end_ptp; 5302 } va_ranges[MAX_RANGES], *var; 5303 int nranges = INIT_NRANGES, r; 5304 5305 #define VA_IN_RANGE(va, var) \ 5306 ((va) >= (var)->start_va && \ 5307 ((va) < (var)->end_va || (var)->end_va == 0)) 5308 5309 #define VA_PTE_BASE(va, var) \ 5310 (&((pt_entry_t *) \ 5311 PMAP_BOOTSTRAP_RELOC_PA((var)->start_ptp))[ \ 5312 m68k_btop((va) - (var)->start_va)]) 5313 5314 #define RELOC(v, t) *((t *)PMAP_BOOTSTRAP_RELOC_GLOB(&(v))) 5315 5316 /* Record the relocation offset for kernel crash dumps. */ 5317 RELOC(kernel_reloc_offset, paddr_t) = reloff; 5318 5319 /* 5320 * First determination we have to make is our configuration: 5321 * Are we using a 2-level or 3-level table? For the purposes 5322 * of bootstrapping the kernel, it's "68040-class" and "other", 5323 * the former getting the 3-level table. 5324 */ 5325 const bool is_68040_class = RELOC(mmutype, int) == MMU_68040 || 5326 RELOC(mmutype, int) == MMU_68060; 5327 const bool use_3l = is_68040_class; 5328 5329 /* 5330 * Based on MMU class, figure out what the constant values of 5331 * segment / page table entries look like. 5332 * 5333 * See pmap_pte_proto_init(). 5334 */ 5335 pt_entry_t proto_ro_pte; /* read-only */ 5336 pt_entry_t proto_rw_pte; /* read-write */ 5337 pt_entry_t proto_rw_cwt_pte; /* read-write, cache-write-through */ 5338 pt_entry_t proto_rw_ci_pte; /* read-write, cache-inhibited */ 5339 pt_entry_t proto_ste; 5340 5341 /* 5342 * N.B. PTE40_CM_WT == 0, so "write-though" caching has the same 5343 * PTE bits on all variants of the MMU. 5344 */ 5345 proto_ro_pte = PTE_VALID|PTE_WIRED|PTE_WP; 5346 proto_rw_cwt_pte = PTE_VALID|PTE_WIRED; 5347 5348 if (is_68040_class) { 5349 proto_rw_pte = PTE_VALID|PTE_WIRED|PTE40_CM_CB; 5350 proto_rw_ci_pte = PTE_VALID|PTE_WIRED|PTE40_CM_NC_SER; 5351 } else { 5352 proto_rw_pte = PTE_VALID|PTE_WIRED; 5353 proto_rw_ci_pte = PTE_VALID|PTE_WIRED|PTE51_CI; 5354 } 5355 proto_ste = DTE51_U | DT51_SHORT; 5356 5357 /* 5358 * Allocate some important fixed virtual (and physical) addresses. 5359 * We use the sum total of this initial mapped kernel space to 5360 * determine how many initial kernel PT pages to allocate. The 5361 * things that consume physical space will come first, and the 5362 * virtual-space-{only,mostly} things come at the end. 5363 * 5364 * lwp0upa lwp0 u-area USPACE (p) 5365 * lwp0uarea (v) 5366 * 5367 * Sysseg_pa kernel lev1map PAGE_SIZE (p) 5368 * kernel_lev1map PAGE_SIZE (v, ci) 5369 * 5370 * ^^^^ end of simple relocation region ^^^^ 5371 * 5372 * null_segtab_pa null segtab PAGE_SIZE (p) 5373 * 5374 * tmpmap_srcva temp map, src PAGE_SIZE (v) 5375 * tmpmap_dstva temp map, dst PAGE_SIZE (v) 5376 * 5377 * vmmap ya tmp map PAGE_SIZE (v) 5378 * 5379 * msgbufaddr kernel msg buf round_page(MSGBUFSIZE) (v) 5380 * 5381 * kernel_ptes kernel PTEs SYSMAP_VA_SIZE (v, ci) 5382 * (see comments above) 5383 * 5384 * When we allocate the kernel lev1map, for the 2-level 5385 * configuration, there is no inner segment tables to allocate, 5386 * the leaf PT pages get poked directly into the level-1 table. 5387 * 5388 * In the 3-level configuration, to map all of the leaf tables, 5389 * inner segment table pages are allocated as necessary. We 5390 * first take those tables from the page containing the level-1 5391 * table, and allocate additional pages as necessary. 5392 */ 5393 5394 nextpa = m68k_round_page(nextpa); 5395 nextva = PMAP_BOOTSTRAP_PA_TO_VA(nextpa); 5396 5397 /* 5398 * nextpa now represents the end of the loaded kernel image. 5399 * This includes the .data + .bss segments, the debugger symbols, 5400 * and any other ancillary data loaded after the kernel. 5401 * 5402 * N.B. This represents the start of our dynamic memory allocation, 5403 * which will be referenced below when we zero the memory we've 5404 * allocated. 5405 */ 5406 kernimg_endpa = nextpa; 5407 5408 /* 5409 * lwp0 u-area. If the caller hasn't already told us where 5410 * to put it (heh), then allocate it here. 5411 * 5412 * In either case, we finish setting it up in pmap_bootstrap2(). 5413 */ 5414 if (RELOC(lwp0upa, paddr_t) == (paddr_t)-1) { 5415 RELOC(lwp0upa, paddr_t) = nextpa; 5416 nextpa += USPACE; 5417 } 5418 RELOC(lwp0uarea, vaddr_t) = nextva; 5419 nextva += USPACE; 5420 5421 size_t nstpages = 0; 5422 5423 /* kernel level-1 map */ 5424 RELOC(Sysseg_pa, paddr_t) = kern_lev1pa = nextpa; 5425 nextpa += PAGE_SIZE; 5426 RELOC(kernel_lev1map, vaddr_t) = kern_lev1va = nextva; 5427 nextva += PAGE_SIZE; 5428 nstpages++; 5429 5430 /* This is the end of the simple relocation region. */ 5431 RELOC(kernel_reloc_end, vaddr_t) = nextva; 5432 5433 /* 5434 * For 3-level configs, we now have space to allocate 5435 * inner segment tables. 5436 */ 5437 stnext_pa = kern_lev1pa + TBL40_L1_SIZE; 5438 stnext_endpa = m68k_round_page(stnext_pa); 5439 5440 /* null segment table */ 5441 #ifdef NULL_SEGTAB_PA 5442 RELOC(null_segtab_pa, paddr_t) = (paddr_t)NULL_SEGTAB_PA; 5443 #else 5444 RELOC(null_segtab_pa, paddr_t) = nextpa; 5445 nextpa += PAGE_SIZE; 5446 #endif 5447 5448 /* pmap temporary map addresses */ 5449 RELOC(cpu_info_primary.ci_tmpva_src, vaddr_t) = nextva; 5450 nextva += PAGE_SIZE; 5451 RELOC(cpu_info_primary.ci_tmpva_dst, vaddr_t) = nextva; 5452 nextva += PAGE_SIZE; 5453 5454 /* vmmap temporary map address */ 5455 RELOC(cpu_info_primary.ci_tmpva_vmmap, vaddr_t) = nextva; 5456 nextva += PAGE_SIZE; 5457 5458 /* kernel message buffer */ 5459 RELOC(msgbufaddr, vaddr_t) = nextva; 5460 nextva += m68k_round_page(MSGBUFSIZE); 5461 5462 /* Kernel PTE array. */ 5463 #ifdef SYSMAP_VA 5464 if ((vaddr_t)SYSMAP_VA < RELOC(kernel_virtual_max, vaddr_t)) { 5465 RELOC(kernel_virtual_max, vaddr_t) = (vaddr_t)SYSMAP_VA; 5466 } 5467 RELOC(kernel_ptes, vaddr_t) = (vaddr_t)SYSMAP_VA; 5468 va_ranges[VA_RANGE_KPTES].start_va = (vaddr_t)SYSMAP_VA; 5469 va_ranges[VA_RANGE_KPTES].end_va = 0; /* end of the address space */ 5470 #else 5471 RELOC(kernel_ptes, vaddr_t) = nextva; 5472 nextva += SYSMAP_VA_SIZE; 5473 #endif /* SYSMAP_VA */ 5474 5475 /* 5476 * Look through the machine_bootmap looking for any non-fixed 5477 * VAs we need to allocate. These will go into the default 5478 * VA range. 5479 */ 5480 pmbm = (const struct pmap_bootmap *) 5481 PMAP_BOOTSTRAP_RELOC_GLOB(machine_bootmap); 5482 for (; pmbm->pmbm_vaddr != (vaddr_t)-1; pmbm++) { 5483 if (pmbm->pmbm_size == 0) { 5484 continue; 5485 } 5486 if (pmbm->pmbm_flags & (PMBM_F_FIXEDVA | PMBM_F_KEEPOUT)) { 5487 va = m68k_trunc_page(pmbm->pmbm_vaddr); 5488 if (va < RELOC(kernel_virtual_max, vaddr_t)) { 5489 RELOC(kernel_virtual_max, vaddr_t) = va; 5490 } 5491 } else { 5492 *(vaddr_t *) 5493 PMAP_BOOTSTRAP_RELOC_GLOB(pmbm->pmbm_vaddr_ptr) = 5494 nextva; 5495 nextva += m68k_round_page(pmbm->pmbm_size); 5496 } 5497 } 5498 5499 /* UVM-managed kernel virtual starts here. */ 5500 RELOC(kernel_virtual_start, vaddr_t) = nextva; 5501 5502 /* 5503 * Allocate enough PT pages to map all of physical memory. 5504 * This should be sufficient to prevent pmap_growkernel() 5505 * from having to do any work before the VM system is set 5506 * up. 5507 * 5508 * XXX mac68k also relies on being able to map the last page 5509 * XXX of RAM VA==PA for the mmu-switchoff dance. Unlike hp300, 5510 * XXX this is not at a fixed location. However, RAM generally 5511 * XXX starts at $0000.0000 on Macs, so this calculation should 5512 * XXX be sufficient to ensure there is a PTE available for this 5513 * XXX purpose. 5514 * XXX TODO: Provide a way for cpu_startup() on mac68k to assert 5515 * XXX this (export kernel_virtual_end?). 5516 */ 5517 nextva += RELOC(physmem, psize_t) << PAGE_SHIFT; 5518 nextva = pmap_round_ptpage(nextva); 5519 if (nextva > RELOC(kernel_virtual_max, vaddr_t) || 5520 nextva < RELOC(kernel_virtual_start, vaddr_t)) { 5521 /* clamp it. */ 5522 nextva = RELOC(kernel_virtual_max, vaddr_t); 5523 } 5524 5525 /* 5526 * This marks the end of UVM-managed kernel virtual space, 5527 * until such time as pmap_growkernel() is called to expand 5528 * it. 5529 */ 5530 va_ranges[VA_RANGE_DEFAULT].start_va = 5531 pmap_trunc_ptpage(VM_MIN_KERNEL_ADDRESS); 5532 va_ranges[VA_RANGE_DEFAULT].end_va = 5533 pmap_round_ptpage(nextva); 5534 RELOC(kernel_virtual_end, vaddr_t) = nextva; 5535 5536 /* 5537 * Now that we know the end of the default range, look through 5538 * the machine_bootmap to see if there are any fixed VA ranges 5539 * we need to allocate static PTs for. 5540 */ 5541 pmbm = (const struct pmap_bootmap *) 5542 PMAP_BOOTSTRAP_RELOC_GLOB(machine_bootmap); 5543 for (; pmbm->pmbm_vaddr != (vaddr_t)-1; pmbm++) { 5544 if (pmbm->pmbm_size == 0) { 5545 continue; 5546 } 5547 if ((pmbm->pmbm_flags & (PMBM_F_FIXEDVA | PMBM_F_KEEPOUT)) != 5548 PMBM_F_FIXEDVA) { 5549 continue; 5550 } 5551 /* 5552 * Found one; see if there's a range already allocated 5553 * for it. 5554 */ 5555 for (r = 0; r < nranges; r++) { 5556 var = &va_ranges[r]; 5557 if (VA_IN_RANGE(pmbm->pmbm_vaddr, var)) { 5558 break; 5559 } 5560 } 5561 if (r < nranges) { 5562 /* Range of PTs already exists. */ 5563 continue; 5564 } 5565 /* 5566 * We need to allocate a range. If we're already at 5567 * the max, I guess we lose. 5568 */ 5569 if (r == MAX_RANGES) { 5570 break; 5571 } 5572 r = nranges++; 5573 va_ranges[r].start_va = pmap_trunc_ptpage(pmbm->pmbm_vaddr); 5574 va_ranges[r].end_va = 5575 pmap_round_ptpage(pmbm->pmbm_vaddr + pmbm->pmbm_size); 5576 } 5577 5578 /* 5579 * Now, compute the number of PT pages required to map the 5580 * required VA ranges and allocate them. 5581 */ 5582 size_t nptpages, total_ptpages = 0; 5583 for (r = 0; r < nranges; r++) { 5584 var = &va_ranges[r]; 5585 nptpages = (var->end_va - var->start_va) / PTPAGEVASZ; 5586 var->start_ptp = nextpa; 5587 nextpa += nptpages * PAGE_SIZE; 5588 var->end_ptp = nextpa; 5589 total_ptpages += nptpages; 5590 } 5591 5592 #ifdef PMAP_MACHINE_CHECK_BOOTSTRAP_ALLOCATIONS 5593 /* 5594 * Right here, the old mac68k Utah pmap_bootstrap1() has a 5595 * check to see if the kernel + bootstrap allocations fit 5596 * within one of the memory segments mapped by the loader. 5597 * This is a hook to accommodate that requirement. 5598 */ 5599 void (*alloc_checkfn)(paddr_t, paddr_t) = (void *) 5600 PMAP_BOOTSTRAP_RELOC_GLOB(pmap_machine_check_bootstrap_allocations); 5601 (*alloc_checkfn)(nextpa, reloff); 5602 #endif 5603 5604 /* 5605 * The bulk of the dynamic memory allocation is done (there 5606 * may be more below if we have to allocate more inner segment 5607 * table pages, but we'll burn that bridge when we come to it). 5608 * 5609 * Zero out all of these freshly-allocated pages. 5610 */ 5611 pte = (pt_entry_t *)PMAP_BOOTSTRAP_RELOC_PA(kernimg_endpa); 5612 epte = (pt_entry_t *)PMAP_BOOTSTRAP_RELOC_PA(nextpa); 5613 while (pte < epte) { 5614 *pte++ = 0; 5615 } 5616 5617 /* 5618 * Ok, let's get to mapping stuff! Almost everything is in 5619 * the default VA range. 5620 */ 5621 var = &va_ranges[VA_RANGE_DEFAULT]; 5622 5623 /* 5624 * Kernel text - read-only. 5625 * 5626 * ...that is, unless, a platform as some quirky requirement 5627 * (hello mac68k!). This hook lets a platform specify an 5628 * alternate proto PTE for the kernel text (in the mac68k case, 5629 * it will be read/write write-through-cacheable). Once the 5630 * kernel is up and running on its own mappings, machine-specific 5631 * code can perform any fixups as necessary. 5632 */ 5633 #ifndef PMAP_BOOTSTRAP_TEXT_PROTO_PTE 5634 #define PMAP_BOOTSTRAP_TEXT_PROTO_PTE proto_ro_pte 5635 #endif 5636 pa = PMAP_BOOTSTRAP_VA_TO_PA(m68k_trunc_page(&kernel_text)); 5637 pte = VA_PTE_BASE(&kernel_text, var); 5638 epte = VA_PTE_BASE(&etext, var); 5639 while (pte < epte) { 5640 *pte++ = PMAP_BOOTSTRAP_TEXT_PROTO_PTE | pa; 5641 pa += PAGE_SIZE; 5642 entry_count++; 5643 } 5644 5645 /* Remainder of kernel image - read-write. */ 5646 epte = VA_PTE_BASE(PMAP_BOOTSTRAP_PA_TO_VA(kernimg_endpa), var); 5647 while (pte < epte) { 5648 *pte++ = proto_rw_pte | pa; 5649 pa += PAGE_SIZE; 5650 entry_count++; 5651 } 5652 5653 /* lwp0 u-area - read-write. */ 5654 pa = RELOC(lwp0upa, paddr_t); 5655 pte = VA_PTE_BASE(RELOC(lwp0uarea, vaddr_t), var); 5656 epte = VA_PTE_BASE(RELOC(lwp0uarea, vaddr_t) + USPACE, var); 5657 while (pte < epte) { 5658 *pte++ = proto_rw_pte | pa; 5659 pa += PAGE_SIZE; 5660 entry_count++; 5661 } 5662 5663 /* Kernel lev1map - read-write, cache-inhibited. */ 5664 pte = VA_PTE_BASE(kern_lev1va, var); 5665 *pte = proto_rw_ci_pte | kern_lev1pa; 5666 entry_count++; 5667 5668 /* 5669 * Kernel leaf PT pages - read-write, cache-inhibited. 5670 * 5671 * These will be in a different VA range if the machine 5672 * defines SYSMAP_VA. 5673 */ 5674 va = RELOC(kernel_ptes, vaddr_t); 5675 pt_entry_t *kptes = (pt_entry_t *)va; 5676 struct va_range *kpte_var = &va_ranges[VA_RANGE_KPTES]; 5677 5678 for (r = 0; r < nranges; r++) { 5679 var = &va_ranges[r]; 5680 va = (vaddr_t)(&kptes[m68k_btop(var->start_va)]); 5681 pte = VA_PTE_BASE(va, kpte_var); 5682 for (pa = var->start_ptp; pa < var->end_ptp; pa += PAGE_SIZE) { 5683 *pte++ = proto_rw_ci_pte | pa; 5684 entry_count++; 5685 } 5686 } 5687 5688 /* 5689 * Now perform any machine-specific mappings at VAs 5690 * allocated earlier. 5691 */ 5692 pmbm = (const struct pmap_bootmap *) 5693 PMAP_BOOTSTRAP_RELOC_GLOB(machine_bootmap); 5694 for (; pmbm->pmbm_vaddr != (vaddr_t)-1; pmbm++) { 5695 pt_entry_t proto; 5696 5697 if (pmbm->pmbm_size == 0 || 5698 (pmbm->pmbm_flags & (PMBM_F_VAONLY | PMBM_F_KEEPOUT))) { 5699 continue; 5700 } 5701 if (pmbm->pmbm_flags & PMBM_F_FIXEDVA) { 5702 va = pmbm->pmbm_vaddr; 5703 } else { 5704 va = *(vaddr_t *) 5705 PMAP_BOOTSTRAP_RELOC_GLOB(pmbm->pmbm_vaddr_ptr); 5706 } 5707 for (r = 0; r < nranges; r++) { 5708 var = &va_ranges[r]; 5709 if (VA_IN_RANGE(va, var)) { 5710 break; 5711 } 5712 } 5713 pa = pmbm->pmbm_paddr; 5714 pte = VA_PTE_BASE(va, var); 5715 switch (pmbm->pmbm_flags & (PMBM_F_CI|PMBM_F_CWT|PMBM_F_RO)) { 5716 case PMBM_F_CI|PMBM_F_RO: 5717 proto = proto_rw_ci_pte | PTE_WP; 5718 break; 5719 case PMBM_F_CWT: 5720 proto = proto_rw_cwt_pte; 5721 break; 5722 case PMBM_F_CI: 5723 proto = proto_rw_ci_pte; 5724 break; 5725 case PMBM_F_RO: 5726 proto = proto_ro_pte; 5727 break; 5728 default: 5729 proto = proto_rw_pte; 5730 break; 5731 } 5732 /* Add in any special PTE bits that might be required. */ 5733 proto |= (pmbm->pmbm_flags & PMBM_F_PTEBITS); 5734 for (vsize_t size = m68k_round_page(pmbm->pmbm_size); 5735 size != 0; 5736 va += PAGE_SIZE, pa += PAGE_SIZE, size -= PAGE_SIZE) { 5737 *pte++ = proto | pa; 5738 entry_count++; 5739 } 5740 } 5741 5742 /* 5743 * Now that all of the individual VAs are mapped in the leaf 5744 * tables, it's time to link those tables into the segment 5745 * table. 5746 * 5747 * For the 2-level case, this is trivial. For the 3-level 5748 * case, we will have to allocate inner segment tables. 5749 */ 5750 for (r = 0; r < nranges; r++) { 5751 var = &va_ranges[r]; 5752 if (use_3l) { 5753 pt_entry_t *stes, *stes1 = (pt_entry_t *) 5754 PMAP_BOOTSTRAP_RELOC_PA(kern_lev1pa); 5755 for (va = var->start_va, pa = var->start_ptp; 5756 pa < var->end_ptp; 5757 va += NBSEG3L, pa += TBL40_L3_SIZE) { 5758 unsigned int ri = LA40_RI(va); 5759 if ((stes1[ri] & UTE40_RESIDENT) == 0) { 5760 /* 5761 * Level-2 table for this segment 5762 * needed. 5763 */ 5764 if (stnext_pa == stnext_endpa) { 5765 /* 5766 * No more slots left in the 5767 * last page we allocated for 5768 * segment tables. Grab 5769 * another one. 5770 */ 5771 stnext_pa = nextpa; 5772 nextpa += PAGE_SIZE; 5773 stnext_endpa = nextpa; 5774 nstpages++; 5775 #ifdef PMAP_MACHINE_CHECK_BOOTSTRAP_ALLOCATIONS 5776 (*alloc_checkfn)(nextpa, 5777 reloff); 5778 #endif 5779 /* 5780 * Zero out the new inner 5781 * segment table page. 5782 */ 5783 pte = (pt_entry_t *) 5784 PMAP_BOOTSTRAP_RELOC_PA( 5785 stnext_pa); 5786 epte = (pt_entry_t *) 5787 PMAP_BOOTSTRAP_RELOC_PA( 5788 stnext_endpa); 5789 while (pte < epte) { 5790 *pte++ = 0; 5791 } 5792 } 5793 stes1[ri] = proto_ste | stnext_pa; 5794 stnext_pa += TBL40_L2_SIZE; 5795 } 5796 stes = (pt_entry_t *) 5797 PMAP_BOOTSTRAP_RELOC_PA( 5798 stes1[ri] & UTE40_PTA); 5799 stes[LA40_PI(va)] = proto_ste | pa; 5800 } 5801 } else { 5802 pt_entry_t *stes = (pt_entry_t *) 5803 PMAP_BOOTSTRAP_RELOC_PA(kern_lev1pa); 5804 for (va = var->start_va, pa = var->start_ptp; 5805 pa < var->end_ptp; 5806 va += NBSEG2L, pa += PAGE_SIZE) { 5807 stes[LA2L_RI(va)] = proto_ste | pa; 5808 } 5809 } 5810 } 5811 5812 /* Instrumentation. */ 5813 RELOC(pmap_nkptpages_initial_ev.ev_count32, uint32_t) = 5814 RELOC(pmap_nkptpages_current_ev.ev_count32, uint32_t) = total_ptpages; 5815 RELOC(pmap_nkstpages_initial_ev.ev_count32, uint32_t) = 5816 RELOC(pmap_nkstpages_current_ev.ev_count32, uint32_t) = nstpages; 5817 5818 /* 5819 * Record the number of wired mappings we created above 5820 * in the kernel pmap stats. 5821 */ 5822 RELOC(kernel_pmap_store.pm_stats.resident_count, long) = entry_count; 5823 RELOC(kernel_pmap_store.pm_stats.wired_count, long) = entry_count; 5824 5825 /* 5826 * Stash any left-over segment table space for use by 5827 * pmap_growkernel() later. 5828 */ 5829 RELOC(kernel_stnext_pa, paddr_t) = stnext_pa; 5830 RELOC(kernel_stnext_endpa, paddr_t) = stnext_endpa; 5831 5832 return nextpa; 5833 } 5834 5835 /* 5836 * PAGE_SIZE should always evaluate to a compile-time constant in this 5837 * context. 5838 */ 5839 __CTASSERT(PAGE_SIZE != 0); 5840 5841 /* 5842 * This serves to provide assurance that other bits of m68k code can 5843 * assume that USPACE is 8KB in size. 5844 */ 5845 __CTASSERT(USPACE == 8192); 5846 5847 /* 5848 * pmap_bootstrap2: 5849 * 5850 * Phase 2 of bootstrapping virtual memory. This is called after 5851 * the MMU has been enabled to finish setting up run-time-computed 5852 * global pmap data and the lwp0 u-area. 5853 * 5854 * Returns the new kernel %sp value for lwp0. 5855 */ 5856 void * 5857 pmap_bootstrap2(void) 5858 { 5859 /* Setup the MMU class; needed before anything else. */ 5860 pmap_mmuclass_init(); 5861 5862 /* Early low-level UVM initialization. */ 5863 uvmexp.pagesize = PAGE_SIZE; 5864 uvm_md_init(); 5865 5866 /* Initialize prototype PTEs; needed before anything else is mapped. */ 5867 pmap_pte_proto_init(); 5868 5869 /* Initialize the kernel pmap. */ 5870 pmap_pinit(pmap_kernel(), Sysseg_pa); 5871 5872 /* perform per-cpu pmap setup for the boot cpu */ 5873 pmap_percpu_start(); 5874 5875 /* Initialize lwp0 u-area. */ 5876 memset((void *)lwp0uarea, 0, USPACE); 5877 uvm_lwp_setuarea(&lwp0, lwp0uarea); 5878 5879 /* Create a fake exception frame so that cpu_lwp_fork() can copy it. */ 5880 struct trapframe *tf = (struct trapframe *)(lwp0uarea + USPACE) - 1; 5881 tf->tf_sr = PSL_USER; 5882 lwp0.l_md.md_regs = (int *)tf; 5883 5884 /* 5885 * Initialize the source/destination control registers for 5886 * movs. 5887 */ 5888 setsfc(FC_USERD); 5889 setdfc(FC_USERD); 5890 5891 PMAP_MUTEX_INIT(&pmap_growkernel_lock, MUTEX_DEFAULT, IPL_NONE); 5892 5893 return tf; 5894 } 5895 5896 /***************************** PMAP DEBUGGING ********************************/ 5897 5898 #ifdef PMAP_DEBUG 5899 5900 void pmap_test_mod_ref(void); 5901 5902 void 5903 pmap_test_mod_ref(void) 5904 { 5905 struct vm_page *pg = pmap_page_alloc(false/*nowait*/); 5906 paddr_t pa = VM_PAGE_TO_PHYS(pg); 5907 vaddr_t va = (vaddr_t)vmmap; 5908 volatile int *loc = (volatile int *)va; 5909 int val; 5910 bool mod, ref; 5911 bool exp_mod, exp_ref; 5912 5913 /* Initialize page and mod/ref state to pristine. */ 5914 pmap_zero_page(pa); 5915 pmap_clear_modify(pg); 5916 pmap_clear_reference(pg); 5917 5918 mod = pmap_is_modified(pg); 5919 ref = pmap_is_referenced(pg); 5920 exp_mod = false; 5921 exp_ref = false; 5922 printf("%s: validating pristine page: mod=%d(%d) ref=%d(%d) (%s)\n", 5923 __func__, 5924 mod, exp_mod, ref, exp_ref, 5925 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 5926 5927 /* Enter non-seeded R/W mapping. */ 5928 pmap_enter(pmap_kernel(), va, pa, UVM_PROT_ALL, 0); 5929 pmap_update(pmap_kernel()); 5930 5931 mod = pmap_is_modified(pg); 5932 ref = pmap_is_referenced(pg); 5933 exp_mod = false; 5934 exp_ref = false; 5935 printf("%s: enter(ALL, 0): mod=%d(%d) ref=%d(%d) (%s)\n", 5936 __func__, 5937 mod, exp_mod, ref, exp_ref, 5938 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 5939 5940 /* reference page */ 5941 val = *loc; 5942 5943 mod = pmap_is_modified(pg); 5944 ref = pmap_is_referenced(pg); 5945 exp_mod = false; 5946 exp_ref = true; 5947 printf("%s: ref val=%d: mod=%d(%d) ref=%d(%d) (%s)\n", 5948 __func__, 5949 val, 5950 mod, exp_mod, ref, exp_ref, 5951 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 5952 5953 /* validate clear behavior. */ 5954 exp_mod = mod; 5955 exp_ref = ref; 5956 mod = pmap_clear_modify(pg); 5957 ref = pmap_clear_reference(pg); 5958 printf("%s: checking clear 1: mod=%d(%d) ref=%d(%d) (%s)\n", 5959 __func__, 5960 mod, exp_mod, ref, exp_ref, 5961 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 5962 5963 mod = pmap_is_modified(pg); 5964 ref = pmap_is_referenced(pg); 5965 exp_mod = false; 5966 exp_ref = false; 5967 printf("%s: checking clear 2: mod=%d(%d) ref=%d(%d) (%s)\n", 5968 __func__, 5969 mod, exp_mod, ref, exp_ref, 5970 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 5971 5972 /* modify page */ 5973 *loc = 0xff; 5974 5975 mod = pmap_is_modified(pg); 5976 ref = pmap_is_referenced(pg); 5977 exp_mod = true; 5978 exp_ref = true; 5979 printf("%s: mod 1: mod=%d(%d) ref=%d(%d) (%s)\n", 5980 __func__, 5981 mod, exp_mod, ref, exp_ref, 5982 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 5983 5984 /* write-protect page */ 5985 pmap_page_protect(pg, UVM_PROT_READ); 5986 5987 mod = pmap_clear_modify(pg); 5988 ref = pmap_clear_reference(pg); 5989 exp_mod = true; 5990 exp_ref = true; 5991 printf("%s: page_protect(READ) mod 2: mod=%d(%d) ref=%d(%d) (%s)\n", 5992 __func__, 5993 mod, exp_mod, ref, exp_ref, 5994 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 5995 5996 mod = pmap_is_modified(pg); 5997 ref = pmap_is_referenced(pg); 5998 exp_mod = false; 5999 exp_ref = false; 6000 printf("%s: checking clear 3: mod=%d(%d) ref=%d(%d) (%s)\n", 6001 __func__, 6002 mod, exp_mod, ref, exp_ref, 6003 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 6004 6005 /* modify page again */ 6006 pmap_enter(pmap_kernel(), va, pa, UVM_PROT_ALL, 0); 6007 pmap_update(pmap_kernel()); 6008 *loc = 0xaa; 6009 6010 mod = pmap_is_modified(pg); 6011 ref = pmap_is_referenced(pg); 6012 exp_mod = true; 6013 exp_ref = true; 6014 printf("%s: mod 3: mod=%d(%d) ref=%d(%d) (%s)\n", 6015 __func__, 6016 mod, exp_mod, ref, exp_ref, 6017 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 6018 6019 /* remove all mappings of page */ 6020 pmap_page_protect(pg, UVM_PROT_NONE); 6021 6022 mod = pmap_clear_modify(pg); 6023 ref = pmap_clear_reference(pg); 6024 exp_mod = true; 6025 exp_ref = true; 6026 printf("%s: page_protect(NONE) mod 4: mod=%d(%d) ref=%d(%d) (%s)\n", 6027 __func__, 6028 mod, exp_mod, ref, exp_ref, 6029 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 6030 6031 /* modify page again */ 6032 pmap_enter(pmap_kernel(), va, pa, UVM_PROT_ALL, 0); 6033 pmap_update(pmap_kernel()); 6034 *loc = 0xaa; 6035 6036 mod = pmap_is_modified(pg); 6037 ref = pmap_is_referenced(pg); 6038 exp_mod = true; 6039 exp_ref = true; 6040 printf("%s: mod 5: mod=%d(%d) ref=%d(%d) (%s)\n", 6041 __func__, 6042 mod, exp_mod, ref, exp_ref, 6043 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 6044 6045 /* read-protect VA */ 6046 pmap_protect(pmap_kernel(), va, va+PAGE_SIZE, UVM_PROT_READ); 6047 pmap_update(pmap_kernel()); 6048 6049 mod = pmap_is_modified(pg); 6050 ref = pmap_is_referenced(pg); 6051 exp_mod = true; 6052 exp_ref = true; 6053 printf("%s: va prot(READ) mod 6: mod=%d(%d) ref=%d(%d) (%s)\n", 6054 __func__, 6055 mod, exp_mod, ref, exp_ref, 6056 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 6057 6058 /* none-protect VA */ 6059 pmap_protect(pmap_kernel(), va, va+PAGE_SIZE, UVM_PROT_NONE); 6060 pmap_update(pmap_kernel()); 6061 6062 mod = pmap_is_modified(pg); 6063 ref = pmap_is_referenced(pg); 6064 exp_mod = true; 6065 exp_ref = true; 6066 printf("%s: va prot(NONE) mod 7: mod=%d(%d) ref=%d(%d) (%s)\n", 6067 __func__, 6068 mod, exp_mod, ref, exp_ref, 6069 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 6070 6071 /* Reset the page. */ 6072 pmap_clear_modify(pg); 6073 pmap_clear_reference(pg); 6074 6075 mod = pmap_is_modified(pg); 6076 ref = pmap_is_referenced(pg); 6077 exp_mod = false; 6078 exp_ref = false; 6079 printf("%s: validating pristine page: mod=%d(%d) ref=%d(%d) (%s)\n", 6080 __func__, 6081 mod, exp_mod, ref, exp_ref, 6082 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 6083 6084 printf("%s: verifying mapping does not exist: (%s)\n", 6085 __func__, 6086 pmap_extract(pmap_kernel(), va, NULL) ? "FAIL" : "OK"); 6087 6088 /* Enter R-seeded R/W mapping. */ 6089 pmap_enter(pmap_kernel(), va, pa, UVM_PROT_READ|UVM_PROT_WRITE, 6090 UVM_PROT_READ); 6091 pmap_update(pmap_kernel()); 6092 6093 mod = pmap_is_modified(pg); 6094 ref = pmap_is_referenced(pg); 6095 exp_mod = false; 6096 exp_ref = true; 6097 printf("%s: enter(R|W, R): mod=%d(%d) ref=%d(%d) (%s)\n", 6098 __func__, 6099 mod, exp_mod, ref, exp_ref, 6100 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 6101 6102 /* Enter W-seeded R/W mapping. */ 6103 pmap_enter(pmap_kernel(), va, pa, UVM_PROT_READ|UVM_PROT_WRITE, 6104 UVM_PROT_WRITE); 6105 pmap_update(pmap_kernel()); 6106 6107 mod = pmap_is_modified(pg); 6108 ref = pmap_is_referenced(pg); 6109 exp_mod = true; 6110 exp_ref = true; 6111 printf("%s: enter(R|W, W): mod=%d(%d) ref=%d(%d) (%s)\n", 6112 __func__, 6113 mod, exp_mod, ref, exp_ref, 6114 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 6115 6116 pmap_clear_modify(pg); 6117 pmap_clear_reference(pg); 6118 6119 mod = pmap_is_modified(pg); 6120 ref = pmap_is_referenced(pg); 6121 exp_mod = false; 6122 exp_ref = false; 6123 printf("%s: validating pristine page: mod=%d(%d) ref=%d(%d) (%s)\n", 6124 __func__, 6125 mod, exp_mod, ref, exp_ref, 6126 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 6127 6128 /* 6129 * Now we're going to reference the page before modifying it 6130 * without any intervening clear. The MMU should update the 6131 * PTE once for the ref, and a second time for the mod, *even 6132 * if the ATC entry is still valid*. 6133 * 6134 * We are going to fetch the PTEs directly to validate the 6135 * MMU's behavior. 6136 */ 6137 pt_entry_t *ptep = pmap_kernel_pte(va); 6138 pt_entry_t ref_pte, mod_pte; 6139 int s = splhigh(); 6140 val = *loc; 6141 ref_pte = pte_load(ptep); 6142 *loc = 0; 6143 mod_pte = pte_load(ptep); 6144 splx(s); 6145 6146 mod = (mod_pte & PTE_M) != 0; 6147 ref = (ref_pte & PTE_U) != 0; 6148 exp_mod = true; 6149 exp_ref = true; 6150 printf("%s: validating MMU behavior: mod=%d(%d) ref=%d(%d) (%s)\n", 6151 __func__, 6152 mod, exp_mod, ref, exp_ref, 6153 mod == exp_mod && ref == exp_ref ? "OK" : "FAIL"); 6154 6155 /* all done. */ 6156 pmap_remove(pmap_kernel(), va, va+PAGE_SIZE); 6157 pmap_update(pmap_kernel()); 6158 6159 printf("%s: done\n", __func__); 6160 } 6161 6162 #endif /* PMAP_DEBUG */ 6163