1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2018 Matthew Macy 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/systm.h> 34 #include <sys/conf.h> 35 #include <sys/bitstring.h> 36 #include <sys/queue.h> 37 #include <sys/cpuset.h> 38 #include <sys/endian.h> 39 #include <sys/kerneldump.h> 40 #include <sys/ktr.h> 41 #include <sys/lock.h> 42 #include <sys/syslog.h> 43 #include <sys/msgbuf.h> 44 #include <sys/malloc.h> 45 #include <sys/mman.h> 46 #include <sys/mutex.h> 47 #include <sys/proc.h> 48 #include <sys/rwlock.h> 49 #include <sys/sched.h> 50 #include <sys/sysctl.h> 51 #include <sys/systm.h> 52 #include <sys/vmem.h> 53 #include <sys/vmmeter.h> 54 #include <sys/smp.h> 55 56 #include <sys/kdb.h> 57 58 #include <dev/ofw/openfirm.h> 59 60 #include <vm/vm.h> 61 #include <vm/pmap.h> 62 #include <vm/vm_param.h> 63 #include <vm/vm_kern.h> 64 #include <vm/vm_page.h> 65 #include <vm/vm_map.h> 66 #include <vm/vm_object.h> 67 #include <vm/vm_extern.h> 68 #include <vm/vm_pageout.h> 69 #include <vm/vm_phys.h> 70 #include <vm/vm_reserv.h> 71 #include <vm/vm_dumpset.h> 72 #include <vm/uma.h> 73 74 #include <machine/_inttypes.h> 75 #include <machine/cpu.h> 76 #include <machine/platform.h> 77 #include <machine/frame.h> 78 #include <machine/md_var.h> 79 #include <machine/psl.h> 80 #include <machine/bat.h> 81 #include <machine/hid.h> 82 #include <machine/pte.h> 83 #include <machine/sr.h> 84 #include <machine/trap.h> 85 #include <machine/mmuvar.h> 86 87 #ifdef INVARIANTS 88 #include <vm/uma_dbg.h> 89 #endif 90 91 #define PPC_BITLSHIFT(bit) (sizeof(long)*NBBY - 1 - (bit)) 92 #define PPC_BIT(bit) (1UL << PPC_BITLSHIFT(bit)) 93 #define PPC_BITLSHIFT_VAL(val, bit) ((val) << PPC_BITLSHIFT(bit)) 94 95 #include "opt_ddb.h" 96 #ifdef DDB 97 static void pmap_pte_walk(pml1_entry_t *l1, vm_offset_t va); 98 #endif 99 100 #define PG_W RPTE_WIRED 101 #define PG_V RPTE_VALID 102 #define PG_MANAGED RPTE_MANAGED 103 #define PG_PROMOTED RPTE_PROMOTED 104 #define PG_M RPTE_C 105 #define PG_A RPTE_R 106 #define PG_X RPTE_EAA_X 107 #define PG_RW RPTE_EAA_W 108 #define PG_PTE_CACHE RPTE_ATTR_MASK 109 110 #define RPTE_SHIFT 9 111 #define NLS_MASK ((1UL<<5)-1) 112 #define RPTE_ENTRIES (1UL<<RPTE_SHIFT) 113 #define RPTE_MASK (RPTE_ENTRIES-1) 114 115 #define NLB_SHIFT 0 116 #define NLB_MASK (((1UL<<52)-1) << 8) 117 118 extern int nkpt; 119 extern caddr_t crashdumpmap; 120 121 #define RIC_FLUSH_TLB 0 122 #define RIC_FLUSH_PWC 1 123 #define RIC_FLUSH_ALL 2 124 125 #define POWER9_TLB_SETS_RADIX 128 /* # sets in POWER9 TLB Radix mode */ 126 127 #define PPC_INST_TLBIE 0x7c000264 128 #define PPC_INST_TLBIEL 0x7c000224 129 #define PPC_INST_SLBIA 0x7c0003e4 130 131 #define ___PPC_RA(a) (((a) & 0x1f) << 16) 132 #define ___PPC_RB(b) (((b) & 0x1f) << 11) 133 #define ___PPC_RS(s) (((s) & 0x1f) << 21) 134 #define ___PPC_RT(t) ___PPC_RS(t) 135 #define ___PPC_R(r) (((r) & 0x1) << 16) 136 #define ___PPC_PRS(prs) (((prs) & 0x1) << 17) 137 #define ___PPC_RIC(ric) (((ric) & 0x3) << 18) 138 139 #define PPC_SLBIA(IH) __XSTRING(.long PPC_INST_SLBIA | \ 140 ((IH & 0x7) << 21)) 141 #define PPC_TLBIE_5(rb,rs,ric,prs,r) \ 142 __XSTRING(.long PPC_INST_TLBIE | \ 143 ___PPC_RB(rb) | ___PPC_RS(rs) | \ 144 ___PPC_RIC(ric) | ___PPC_PRS(prs) | \ 145 ___PPC_R(r)) 146 147 #define PPC_TLBIEL(rb,rs,ric,prs,r) \ 148 __XSTRING(.long PPC_INST_TLBIEL | \ 149 ___PPC_RB(rb) | ___PPC_RS(rs) | \ 150 ___PPC_RIC(ric) | ___PPC_PRS(prs) | \ 151 ___PPC_R(r)) 152 153 #define PPC_INVALIDATE_ERAT PPC_SLBIA(7) 154 155 static __inline void 156 ttusync(void) 157 { 158 __asm __volatile("eieio; tlbsync; ptesync" ::: "memory"); 159 } 160 161 #define TLBIEL_INVAL_SEL_MASK 0xc00 /* invalidation selector */ 162 #define TLBIEL_INVAL_PAGE 0x000 /* invalidate a single page */ 163 #define TLBIEL_INVAL_SET_PID 0x400 /* invalidate a set for the current PID */ 164 #define TLBIEL_INVAL_SET_LPID 0x800 /* invalidate a set for current LPID */ 165 #define TLBIEL_INVAL_SET 0xc00 /* invalidate a set for all LPIDs */ 166 167 #define TLBIE_ACTUAL_PAGE_MASK 0xe0 168 #define TLBIE_ACTUAL_PAGE_4K 0x00 169 #define TLBIE_ACTUAL_PAGE_64K 0xa0 170 #define TLBIE_ACTUAL_PAGE_2M 0x20 171 #define TLBIE_ACTUAL_PAGE_1G 0x40 172 173 #define TLBIE_PRS_PARTITION_SCOPE 0x0 174 #define TLBIE_PRS_PROCESS_SCOPE 0x1 175 176 #define TLBIE_RIC_INVALIDATE_TLB 0x0 /* Invalidate just TLB */ 177 #define TLBIE_RIC_INVALIDATE_PWC 0x1 /* Invalidate just PWC */ 178 #define TLBIE_RIC_INVALIDATE_ALL 0x2 /* Invalidate TLB, PWC, 179 * cached {proc, part}tab entries 180 */ 181 #define TLBIE_RIC_INVALIDATE_SEQ 0x3 /* HPT - only: 182 * Invalidate a range of translations 183 */ 184 185 static __always_inline void 186 radix_tlbie(uint8_t ric, uint8_t prs, uint16_t is, uint32_t pid, uint32_t lpid, 187 vm_offset_t va, uint16_t ap) 188 { 189 uint64_t rb, rs; 190 191 MPASS((va & PAGE_MASK) == 0); 192 193 rs = ((uint64_t)pid << 32) | lpid; 194 rb = va | is | ap; 195 __asm __volatile(PPC_TLBIE_5(%0, %1, %2, %3, 1) : : 196 "r" (rb), "r" (rs), "i" (ric), "i" (prs) : "memory"); 197 } 198 199 static __inline void 200 radix_tlbie_fixup(uint32_t pid, vm_offset_t va, int ap) 201 { 202 203 __asm __volatile("ptesync" ::: "memory"); 204 radix_tlbie(TLBIE_RIC_INVALIDATE_TLB, TLBIE_PRS_PROCESS_SCOPE, 205 TLBIEL_INVAL_PAGE, 0, 0, va, ap); 206 __asm __volatile("ptesync" ::: "memory"); 207 radix_tlbie(TLBIE_RIC_INVALIDATE_TLB, TLBIE_PRS_PROCESS_SCOPE, 208 TLBIEL_INVAL_PAGE, pid, 0, va, ap); 209 } 210 211 static __inline void 212 radix_tlbie_invlpg_user_4k(uint32_t pid, vm_offset_t va) 213 { 214 215 radix_tlbie(TLBIE_RIC_INVALIDATE_TLB, TLBIE_PRS_PROCESS_SCOPE, 216 TLBIEL_INVAL_PAGE, pid, 0, va, TLBIE_ACTUAL_PAGE_4K); 217 radix_tlbie_fixup(pid, va, TLBIE_ACTUAL_PAGE_4K); 218 } 219 220 static __inline void 221 radix_tlbie_invlpg_user_2m(uint32_t pid, vm_offset_t va) 222 { 223 224 radix_tlbie(TLBIE_RIC_INVALIDATE_TLB, TLBIE_PRS_PROCESS_SCOPE, 225 TLBIEL_INVAL_PAGE, pid, 0, va, TLBIE_ACTUAL_PAGE_2M); 226 radix_tlbie_fixup(pid, va, TLBIE_ACTUAL_PAGE_2M); 227 } 228 229 static __inline void 230 radix_tlbie_invlpwc_user(uint32_t pid) 231 { 232 233 radix_tlbie(TLBIE_RIC_INVALIDATE_PWC, TLBIE_PRS_PROCESS_SCOPE, 234 TLBIEL_INVAL_SET_PID, pid, 0, 0, 0); 235 } 236 237 static __inline void 238 radix_tlbie_flush_user(uint32_t pid) 239 { 240 241 radix_tlbie(TLBIE_RIC_INVALIDATE_ALL, TLBIE_PRS_PROCESS_SCOPE, 242 TLBIEL_INVAL_SET_PID, pid, 0, 0, 0); 243 } 244 245 static __inline void 246 radix_tlbie_invlpg_kernel_4k(vm_offset_t va) 247 { 248 249 radix_tlbie(TLBIE_RIC_INVALIDATE_TLB, TLBIE_PRS_PROCESS_SCOPE, 250 TLBIEL_INVAL_PAGE, 0, 0, va, TLBIE_ACTUAL_PAGE_4K); 251 radix_tlbie_fixup(0, va, TLBIE_ACTUAL_PAGE_4K); 252 } 253 254 static __inline void 255 radix_tlbie_invlpg_kernel_2m(vm_offset_t va) 256 { 257 258 radix_tlbie(TLBIE_RIC_INVALIDATE_TLB, TLBIE_PRS_PROCESS_SCOPE, 259 TLBIEL_INVAL_PAGE, 0, 0, va, TLBIE_ACTUAL_PAGE_2M); 260 radix_tlbie_fixup(0, va, TLBIE_ACTUAL_PAGE_2M); 261 } 262 263 /* 1GB pages aren't currently supported. */ 264 static __inline __unused void 265 radix_tlbie_invlpg_kernel_1g(vm_offset_t va) 266 { 267 268 radix_tlbie(TLBIE_RIC_INVALIDATE_TLB, TLBIE_PRS_PROCESS_SCOPE, 269 TLBIEL_INVAL_PAGE, 0, 0, va, TLBIE_ACTUAL_PAGE_1G); 270 radix_tlbie_fixup(0, va, TLBIE_ACTUAL_PAGE_1G); 271 } 272 273 static __inline void 274 radix_tlbie_invlpwc_kernel(void) 275 { 276 277 radix_tlbie(TLBIE_RIC_INVALIDATE_PWC, TLBIE_PRS_PROCESS_SCOPE, 278 TLBIEL_INVAL_SET_LPID, 0, 0, 0, 0); 279 } 280 281 static __inline void 282 radix_tlbie_flush_kernel(void) 283 { 284 285 radix_tlbie(TLBIE_RIC_INVALIDATE_ALL, TLBIE_PRS_PROCESS_SCOPE, 286 TLBIEL_INVAL_SET_LPID, 0, 0, 0, 0); 287 } 288 289 static __inline vm_pindex_t 290 pmap_l3e_pindex(vm_offset_t va) 291 { 292 return ((va & PG_FRAME) >> L3_PAGE_SIZE_SHIFT); 293 } 294 295 static __inline vm_pindex_t 296 pmap_pml3e_index(vm_offset_t va) 297 { 298 299 return ((va >> L3_PAGE_SIZE_SHIFT) & RPTE_MASK); 300 } 301 302 static __inline vm_pindex_t 303 pmap_pml2e_index(vm_offset_t va) 304 { 305 return ((va >> L2_PAGE_SIZE_SHIFT) & RPTE_MASK); 306 } 307 308 static __inline vm_pindex_t 309 pmap_pml1e_index(vm_offset_t va) 310 { 311 return ((va & PG_FRAME) >> L1_PAGE_SIZE_SHIFT); 312 } 313 314 /* Return various clipped indexes for a given VA */ 315 static __inline vm_pindex_t 316 pmap_pte_index(vm_offset_t va) 317 { 318 319 return ((va >> PAGE_SHIFT) & RPTE_MASK); 320 } 321 322 /* Return a pointer to the PT slot that corresponds to a VA */ 323 static __inline pt_entry_t * 324 pmap_l3e_to_pte(pt_entry_t *l3e, vm_offset_t va) 325 { 326 pt_entry_t *pte; 327 vm_paddr_t ptepa; 328 329 ptepa = (be64toh(*l3e) & NLB_MASK); 330 pte = (pt_entry_t *)PHYS_TO_DMAP(ptepa); 331 return (&pte[pmap_pte_index(va)]); 332 } 333 334 /* Return a pointer to the PD slot that corresponds to a VA */ 335 static __inline pt_entry_t * 336 pmap_l2e_to_l3e(pt_entry_t *l2e, vm_offset_t va) 337 { 338 pt_entry_t *l3e; 339 vm_paddr_t l3pa; 340 341 l3pa = (be64toh(*l2e) & NLB_MASK); 342 l3e = (pml3_entry_t *)PHYS_TO_DMAP(l3pa); 343 return (&l3e[pmap_pml3e_index(va)]); 344 } 345 346 /* Return a pointer to the PD slot that corresponds to a VA */ 347 static __inline pt_entry_t * 348 pmap_l1e_to_l2e(pt_entry_t *l1e, vm_offset_t va) 349 { 350 pt_entry_t *l2e; 351 vm_paddr_t l2pa; 352 353 l2pa = (be64toh(*l1e) & NLB_MASK); 354 355 l2e = (pml2_entry_t *)PHYS_TO_DMAP(l2pa); 356 return (&l2e[pmap_pml2e_index(va)]); 357 } 358 359 static __inline pml1_entry_t * 360 pmap_pml1e(pmap_t pmap, vm_offset_t va) 361 { 362 363 return (&pmap->pm_pml1[pmap_pml1e_index(va)]); 364 } 365 366 static pt_entry_t * 367 pmap_pml2e(pmap_t pmap, vm_offset_t va) 368 { 369 pt_entry_t *l1e; 370 371 l1e = pmap_pml1e(pmap, va); 372 if (l1e == NULL || (be64toh(*l1e) & RPTE_VALID) == 0) 373 return (NULL); 374 return (pmap_l1e_to_l2e(l1e, va)); 375 } 376 377 static __inline pt_entry_t * 378 pmap_pml3e(pmap_t pmap, vm_offset_t va) 379 { 380 pt_entry_t *l2e; 381 382 l2e = pmap_pml2e(pmap, va); 383 if (l2e == NULL || (be64toh(*l2e) & RPTE_VALID) == 0) 384 return (NULL); 385 return (pmap_l2e_to_l3e(l2e, va)); 386 } 387 388 static __inline pt_entry_t * 389 pmap_pte(pmap_t pmap, vm_offset_t va) 390 { 391 pt_entry_t *l3e; 392 393 l3e = pmap_pml3e(pmap, va); 394 if (l3e == NULL || (be64toh(*l3e) & RPTE_VALID) == 0) 395 return (NULL); 396 return (pmap_l3e_to_pte(l3e, va)); 397 } 398 399 int nkpt = 64; 400 SYSCTL_INT(_machdep, OID_AUTO, nkpt, CTLFLAG_RD, &nkpt, 0, 401 "Number of kernel page table pages allocated on bootup"); 402 403 vm_paddr_t dmaplimit; 404 405 SYSCTL_DECL(_vm_pmap); 406 407 #ifdef INVARIANTS 408 #define VERBOSE_PMAP 0 409 #define VERBOSE_PROTECT 0 410 static int pmap_logging; 411 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_logging, CTLFLAG_RWTUN, 412 &pmap_logging, 0, "verbose debug logging"); 413 #endif 414 415 static u_int64_t KPTphys; /* phys addr of kernel level 1 */ 416 417 //static vm_paddr_t KERNend; /* phys addr of end of bootstrap data */ 418 419 static vm_offset_t qframe = 0; 420 static struct mtx qframe_mtx; 421 422 void mmu_radix_activate(struct thread *); 423 void mmu_radix_advise(pmap_t, vm_offset_t, vm_offset_t, int); 424 void mmu_radix_align_superpage(vm_object_t, vm_ooffset_t, vm_offset_t *, 425 vm_size_t); 426 void mmu_radix_clear_modify(vm_page_t); 427 void mmu_radix_copy(pmap_t, pmap_t, vm_offset_t, vm_size_t, vm_offset_t); 428 int mmu_radix_decode_kernel_ptr(vm_offset_t, int *, vm_offset_t *); 429 int mmu_radix_enter(pmap_t, vm_offset_t, vm_page_t, vm_prot_t, u_int, int8_t); 430 void mmu_radix_enter_object(pmap_t, vm_offset_t, vm_offset_t, vm_page_t, 431 vm_prot_t); 432 void mmu_radix_enter_quick(pmap_t, vm_offset_t, vm_page_t, vm_prot_t); 433 vm_paddr_t mmu_radix_extract(pmap_t pmap, vm_offset_t va); 434 vm_page_t mmu_radix_extract_and_hold(pmap_t, vm_offset_t, vm_prot_t); 435 void mmu_radix_kenter(vm_offset_t, vm_paddr_t); 436 vm_paddr_t mmu_radix_kextract(vm_offset_t); 437 void mmu_radix_kremove(vm_offset_t); 438 boolean_t mmu_radix_is_modified(vm_page_t); 439 boolean_t mmu_radix_is_prefaultable(pmap_t, vm_offset_t); 440 boolean_t mmu_radix_is_referenced(vm_page_t); 441 void mmu_radix_object_init_pt(pmap_t, vm_offset_t, vm_object_t, 442 vm_pindex_t, vm_size_t); 443 boolean_t mmu_radix_page_exists_quick(pmap_t, vm_page_t); 444 void mmu_radix_page_init(vm_page_t); 445 boolean_t mmu_radix_page_is_mapped(vm_page_t m); 446 void mmu_radix_page_set_memattr(vm_page_t, vm_memattr_t); 447 int mmu_radix_page_wired_mappings(vm_page_t); 448 int mmu_radix_pinit(pmap_t); 449 void mmu_radix_protect(pmap_t, vm_offset_t, vm_offset_t, vm_prot_t); 450 bool mmu_radix_ps_enabled(pmap_t); 451 void mmu_radix_qenter(vm_offset_t, vm_page_t *, int); 452 void mmu_radix_qremove(vm_offset_t, int); 453 vm_offset_t mmu_radix_quick_enter_page(vm_page_t); 454 void mmu_radix_quick_remove_page(vm_offset_t); 455 boolean_t mmu_radix_ts_referenced(vm_page_t); 456 void mmu_radix_release(pmap_t); 457 void mmu_radix_remove(pmap_t, vm_offset_t, vm_offset_t); 458 void mmu_radix_remove_all(vm_page_t); 459 void mmu_radix_remove_pages(pmap_t); 460 void mmu_radix_remove_write(vm_page_t); 461 void mmu_radix_unwire(pmap_t, vm_offset_t, vm_offset_t); 462 void mmu_radix_zero_page(vm_page_t); 463 void mmu_radix_zero_page_area(vm_page_t, int, int); 464 int mmu_radix_change_attr(vm_offset_t, vm_size_t, vm_memattr_t); 465 void mmu_radix_page_array_startup(long pages); 466 467 #include "mmu_oea64.h" 468 469 /* 470 * Kernel MMU interface 471 */ 472 473 static void mmu_radix_bootstrap(vm_offset_t, vm_offset_t); 474 475 static void mmu_radix_copy_page(vm_page_t, vm_page_t); 476 static void mmu_radix_copy_pages(vm_page_t *ma, vm_offset_t a_offset, 477 vm_page_t *mb, vm_offset_t b_offset, int xfersize); 478 static void mmu_radix_growkernel(vm_offset_t); 479 static void mmu_radix_init(void); 480 static int mmu_radix_mincore(pmap_t, vm_offset_t, vm_paddr_t *); 481 static vm_offset_t mmu_radix_map(vm_offset_t *, vm_paddr_t, vm_paddr_t, int); 482 static void mmu_radix_pinit0(pmap_t); 483 484 static void *mmu_radix_mapdev(vm_paddr_t, vm_size_t); 485 static void *mmu_radix_mapdev_attr(vm_paddr_t, vm_size_t, vm_memattr_t); 486 static void mmu_radix_unmapdev(vm_offset_t, vm_size_t); 487 static void mmu_radix_kenter_attr(vm_offset_t, vm_paddr_t, vm_memattr_t ma); 488 static boolean_t mmu_radix_dev_direct_mapped(vm_paddr_t, vm_size_t); 489 static void mmu_radix_dumpsys_map(vm_paddr_t pa, size_t sz, void **va); 490 static void mmu_radix_scan_init(void); 491 static void mmu_radix_cpu_bootstrap(int ap); 492 static void mmu_radix_tlbie_all(void); 493 494 static struct pmap_funcs mmu_radix_methods = { 495 .bootstrap = mmu_radix_bootstrap, 496 .copy_page = mmu_radix_copy_page, 497 .copy_pages = mmu_radix_copy_pages, 498 .cpu_bootstrap = mmu_radix_cpu_bootstrap, 499 .growkernel = mmu_radix_growkernel, 500 .init = mmu_radix_init, 501 .map = mmu_radix_map, 502 .mincore = mmu_radix_mincore, 503 .pinit = mmu_radix_pinit, 504 .pinit0 = mmu_radix_pinit0, 505 506 .mapdev = mmu_radix_mapdev, 507 .mapdev_attr = mmu_radix_mapdev_attr, 508 .unmapdev = mmu_radix_unmapdev, 509 .kenter_attr = mmu_radix_kenter_attr, 510 .dev_direct_mapped = mmu_radix_dev_direct_mapped, 511 .dumpsys_pa_init = mmu_radix_scan_init, 512 .dumpsys_map_chunk = mmu_radix_dumpsys_map, 513 .page_is_mapped = mmu_radix_page_is_mapped, 514 .ps_enabled = mmu_radix_ps_enabled, 515 .object_init_pt = mmu_radix_object_init_pt, 516 .protect = mmu_radix_protect, 517 /* pmap dispatcher interface */ 518 .clear_modify = mmu_radix_clear_modify, 519 .copy = mmu_radix_copy, 520 .enter = mmu_radix_enter, 521 .enter_object = mmu_radix_enter_object, 522 .enter_quick = mmu_radix_enter_quick, 523 .extract = mmu_radix_extract, 524 .extract_and_hold = mmu_radix_extract_and_hold, 525 .is_modified = mmu_radix_is_modified, 526 .is_prefaultable = mmu_radix_is_prefaultable, 527 .is_referenced = mmu_radix_is_referenced, 528 .ts_referenced = mmu_radix_ts_referenced, 529 .page_exists_quick = mmu_radix_page_exists_quick, 530 .page_init = mmu_radix_page_init, 531 .page_wired_mappings = mmu_radix_page_wired_mappings, 532 .qenter = mmu_radix_qenter, 533 .qremove = mmu_radix_qremove, 534 .release = mmu_radix_release, 535 .remove = mmu_radix_remove, 536 .remove_all = mmu_radix_remove_all, 537 .remove_write = mmu_radix_remove_write, 538 .unwire = mmu_radix_unwire, 539 .zero_page = mmu_radix_zero_page, 540 .zero_page_area = mmu_radix_zero_page_area, 541 .activate = mmu_radix_activate, 542 .quick_enter_page = mmu_radix_quick_enter_page, 543 .quick_remove_page = mmu_radix_quick_remove_page, 544 .page_set_memattr = mmu_radix_page_set_memattr, 545 .page_array_startup = mmu_radix_page_array_startup, 546 547 /* Internal interfaces */ 548 .kenter = mmu_radix_kenter, 549 .kextract = mmu_radix_kextract, 550 .kremove = mmu_radix_kremove, 551 .change_attr = mmu_radix_change_attr, 552 .decode_kernel_ptr = mmu_radix_decode_kernel_ptr, 553 554 .tlbie_all = mmu_radix_tlbie_all, 555 }; 556 557 MMU_DEF(mmu_radix, MMU_TYPE_RADIX, mmu_radix_methods); 558 559 static boolean_t pmap_demote_l3e_locked(pmap_t pmap, pml3_entry_t *l3e, vm_offset_t va, 560 struct rwlock **lockp); 561 static boolean_t pmap_demote_l3e(pmap_t pmap, pml3_entry_t *pde, vm_offset_t va); 562 static int pmap_unuse_pt(pmap_t, vm_offset_t, pml3_entry_t, struct spglist *); 563 static int pmap_remove_l3e(pmap_t pmap, pml3_entry_t *pdq, vm_offset_t sva, 564 struct spglist *free, struct rwlock **lockp); 565 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva, 566 pml3_entry_t ptepde, struct spglist *free, struct rwlock **lockp); 567 static vm_page_t pmap_remove_pt_page(pmap_t pmap, vm_offset_t va); 568 static bool pmap_remove_page(pmap_t pmap, vm_offset_t va, pml3_entry_t *pde, 569 struct spglist *free); 570 static bool pmap_remove_ptes(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, 571 pml3_entry_t *l3e, struct spglist *free, struct rwlock **lockp); 572 573 static bool pmap_pv_insert_l3e(pmap_t pmap, vm_offset_t va, pml3_entry_t l3e, 574 u_int flags, struct rwlock **lockp); 575 #if VM_NRESERVLEVEL > 0 576 static void pmap_pv_promote_l3e(pmap_t pmap, vm_offset_t va, vm_paddr_t pa, 577 struct rwlock **lockp); 578 #endif 579 static void pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va); 580 static int pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte); 581 static vm_page_t mmu_radix_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, 582 vm_prot_t prot, vm_page_t mpte, struct rwlock **lockp, bool *invalidate); 583 584 static bool pmap_enter_2mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, 585 vm_prot_t prot, struct rwlock **lockp); 586 static int pmap_enter_l3e(pmap_t pmap, vm_offset_t va, pml3_entry_t newpde, 587 u_int flags, vm_page_t m, struct rwlock **lockp); 588 589 static vm_page_t reclaim_pv_chunk(pmap_t locked_pmap, struct rwlock **lockp); 590 static void free_pv_chunk(struct pv_chunk *pc); 591 static vm_page_t _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex, struct rwlock **lockp); 592 static vm_page_t pmap_allocl3e(pmap_t pmap, vm_offset_t va, 593 struct rwlock **lockp); 594 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, 595 struct rwlock **lockp); 596 static void _pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m, 597 struct spglist *free); 598 static boolean_t pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free); 599 600 static void pmap_invalidate_page(pmap_t pmap, vm_offset_t start); 601 static void pmap_invalidate_all(pmap_t pmap); 602 static int pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode, bool flush); 603 604 /* 605 * Internal flags for pmap_enter()'s helper functions. 606 */ 607 #define PMAP_ENTER_NORECLAIM 0x1000000 /* Don't reclaim PV entries. */ 608 #define PMAP_ENTER_NOREPLACE 0x2000000 /* Don't replace mappings. */ 609 610 #define UNIMPLEMENTED() panic("%s not implemented", __func__) 611 #define UNTESTED() panic("%s not yet tested", __func__) 612 613 /* Number of supported PID bits */ 614 static unsigned int isa3_pid_bits; 615 616 /* PID to start allocating from */ 617 static unsigned int isa3_base_pid; 618 619 #define PROCTAB_SIZE_SHIFT (isa3_pid_bits + 4) 620 #define PROCTAB_ENTRIES (1ul << isa3_pid_bits) 621 622 /* 623 * Map of physical memory regions. 624 */ 625 static struct mem_region *regions, *pregions; 626 static struct numa_mem_region *numa_pregions; 627 static u_int phys_avail_count; 628 static int regions_sz, pregions_sz, numa_pregions_sz; 629 static struct pate *isa3_parttab; 630 static struct prte *isa3_proctab; 631 static vmem_t *asid_arena; 632 633 extern void bs_remap_earlyboot(void); 634 635 #define RADIX_PGD_SIZE_SHIFT 16 636 #define RADIX_PGD_SIZE (1UL << RADIX_PGD_SIZE_SHIFT) 637 638 #define RADIX_PGD_INDEX_SHIFT (RADIX_PGD_SIZE_SHIFT-3) 639 #define NL2EPG (PAGE_SIZE/sizeof(pml2_entry_t)) 640 #define NL3EPG (PAGE_SIZE/sizeof(pml3_entry_t)) 641 642 #define NUPML1E (RADIX_PGD_SIZE/sizeof(uint64_t)) /* number of userland PML1 pages */ 643 #define NUPDPE (NUPML1E * NL2EPG)/* number of userland PDP pages */ 644 #define NUPDE (NUPDPE * NL3EPG) /* number of userland PD entries */ 645 646 /* POWER9 only permits a 64k partition table size. */ 647 #define PARTTAB_SIZE_SHIFT 16 648 #define PARTTAB_SIZE (1UL << PARTTAB_SIZE_SHIFT) 649 650 #define PARTTAB_HR (1UL << 63) /* host uses radix */ 651 #define PARTTAB_GR (1UL << 63) /* guest uses radix must match host */ 652 653 /* TLB flush actions. Used as argument to tlbiel_all() */ 654 enum { 655 TLB_INVAL_SCOPE_LPID = 0, /* invalidate TLBs for current LPID */ 656 TLB_INVAL_SCOPE_GLOBAL = 1, /* invalidate all TLBs */ 657 }; 658 659 #define NPV_LIST_LOCKS MAXCPU 660 static int pmap_initialized; 661 static vm_paddr_t proctab0pa; 662 static vm_paddr_t parttab_phys; 663 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE); 664 665 /* 666 * Data for the pv entry allocation mechanism. 667 * Updates to pv_invl_gen are protected by the pv_list_locks[] 668 * elements, but reads are not. 669 */ 670 static TAILQ_HEAD(pch, pv_chunk) pv_chunks = TAILQ_HEAD_INITIALIZER(pv_chunks); 671 static struct mtx __exclusive_cache_line pv_chunks_mutex; 672 static struct rwlock __exclusive_cache_line pv_list_locks[NPV_LIST_LOCKS]; 673 static struct md_page *pv_table; 674 static struct md_page pv_dummy; 675 676 #ifdef PV_STATS 677 #define PV_STAT(x) do { x ; } while (0) 678 #else 679 #define PV_STAT(x) do { } while (0) 680 #endif 681 682 #define pa_radix_index(pa) ((pa) >> L3_PAGE_SIZE_SHIFT) 683 #define pa_to_pvh(pa) (&pv_table[pa_radix_index(pa)]) 684 685 #define PHYS_TO_PV_LIST_LOCK(pa) \ 686 (&pv_list_locks[pa_radix_index(pa) % NPV_LIST_LOCKS]) 687 688 #define CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa) do { \ 689 struct rwlock **_lockp = (lockp); \ 690 struct rwlock *_new_lock; \ 691 \ 692 _new_lock = PHYS_TO_PV_LIST_LOCK(pa); \ 693 if (_new_lock != *_lockp) { \ 694 if (*_lockp != NULL) \ 695 rw_wunlock(*_lockp); \ 696 *_lockp = _new_lock; \ 697 rw_wlock(*_lockp); \ 698 } \ 699 } while (0) 700 701 #define CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m) \ 702 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, VM_PAGE_TO_PHYS(m)) 703 704 #define RELEASE_PV_LIST_LOCK(lockp) do { \ 705 struct rwlock **_lockp = (lockp); \ 706 \ 707 if (*_lockp != NULL) { \ 708 rw_wunlock(*_lockp); \ 709 *_lockp = NULL; \ 710 } \ 711 } while (0) 712 713 #define VM_PAGE_TO_PV_LIST_LOCK(m) \ 714 PHYS_TO_PV_LIST_LOCK(VM_PAGE_TO_PHYS(m)) 715 716 /* 717 * We support 52 bits, hence: 718 * bits 52 - 31 = 21, 0b10101 719 * RTS encoding details 720 * bits 0 - 3 of rts -> bits 6 - 8 unsigned long 721 * bits 4 - 5 of rts -> bits 62 - 63 of unsigned long 722 */ 723 #define RTS_SIZE ((0x2UL << 61) | (0x5UL << 5)) 724 725 static int powernv_enabled = 1; 726 727 static __always_inline void 728 tlbiel_radix_set_isa300(uint32_t set, uint32_t is, 729 uint32_t pid, uint32_t ric, uint32_t prs) 730 { 731 uint64_t rb; 732 uint64_t rs; 733 734 rb = PPC_BITLSHIFT_VAL(set, 51) | PPC_BITLSHIFT_VAL(is, 53); 735 rs = PPC_BITLSHIFT_VAL((uint64_t)pid, 31); 736 737 __asm __volatile(PPC_TLBIEL(%0, %1, %2, %3, 1) 738 : : "r"(rb), "r"(rs), "i"(ric), "i"(prs) 739 : "memory"); 740 } 741 742 static void 743 tlbiel_flush_isa3(uint32_t num_sets, uint32_t is) 744 { 745 uint32_t set; 746 747 __asm __volatile("ptesync": : :"memory"); 748 749 /* 750 * Flush the first set of the TLB, and the entire Page Walk Cache 751 * and partition table entries. Then flush the remaining sets of the 752 * TLB. 753 */ 754 tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 0); 755 for (set = 1; set < num_sets; set++) 756 tlbiel_radix_set_isa300(set, is, 0, RIC_FLUSH_TLB, 0); 757 758 /* Do the same for process scoped entries. */ 759 tlbiel_radix_set_isa300(0, is, 0, RIC_FLUSH_ALL, 1); 760 for (set = 1; set < num_sets; set++) 761 tlbiel_radix_set_isa300(set, is, 0, RIC_FLUSH_TLB, 1); 762 763 __asm __volatile("ptesync": : :"memory"); 764 } 765 766 static void 767 mmu_radix_tlbiel_flush(int scope) 768 { 769 int is; 770 771 MPASS(scope == TLB_INVAL_SCOPE_LPID || 772 scope == TLB_INVAL_SCOPE_GLOBAL); 773 is = scope + 2; 774 775 tlbiel_flush_isa3(POWER9_TLB_SETS_RADIX, is); 776 __asm __volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory"); 777 } 778 779 static void 780 mmu_radix_tlbie_all() 781 { 782 /* TODO: LPID invalidate */ 783 mmu_radix_tlbiel_flush(TLB_INVAL_SCOPE_GLOBAL); 784 } 785 786 static void 787 mmu_radix_init_amor(void) 788 { 789 /* 790 * In HV mode, we init AMOR (Authority Mask Override Register) so that 791 * the hypervisor and guest can setup IAMR (Instruction Authority Mask 792 * Register), enable key 0 and set it to 1. 793 * 794 * AMOR = 0b1100 .... 0000 (Mask for key 0 is 11) 795 */ 796 mtspr(SPR_AMOR, (3ul << 62)); 797 } 798 799 static void 800 mmu_radix_init_iamr(void) 801 { 802 /* 803 * Radix always uses key0 of the IAMR to determine if an access is 804 * allowed. We set bit 0 (IBM bit 1) of key0, to prevent instruction 805 * fetch. 806 */ 807 mtspr(SPR_IAMR, (1ul << 62)); 808 } 809 810 static void 811 mmu_radix_pid_set(pmap_t pmap) 812 { 813 814 mtspr(SPR_PID, pmap->pm_pid); 815 isync(); 816 } 817 818 /* Quick sort callout for comparing physical addresses. */ 819 static int 820 pa_cmp(const void *a, const void *b) 821 { 822 const vm_paddr_t *pa = a, *pb = b; 823 824 if (*pa < *pb) 825 return (-1); 826 else if (*pa > *pb) 827 return (1); 828 else 829 return (0); 830 } 831 832 #define pte_load_store(ptep, pte) atomic_swap_long(ptep, pte) 833 #define pte_load_clear(ptep) atomic_swap_long(ptep, 0) 834 #define pte_store(ptep, pte) do { \ 835 MPASS((pte) & (RPTE_EAA_R | RPTE_EAA_W | RPTE_EAA_X)); \ 836 *(u_long *)(ptep) = htobe64((u_long)((pte) | PG_V | RPTE_LEAF)); \ 837 } while (0) 838 /* 839 * NB: should only be used for adding directories - not for direct mappings 840 */ 841 #define pde_store(ptep, pa) do { \ 842 *(u_long *)(ptep) = htobe64((u_long)(pa|RPTE_VALID|RPTE_SHIFT)); \ 843 } while (0) 844 845 #define pte_clear(ptep) do { \ 846 *(u_long *)(ptep) = (u_long)(0); \ 847 } while (0) 848 849 #define PMAP_PDE_SUPERPAGE (1 << 8) /* supports 2MB superpages */ 850 851 /* 852 * Promotion to a 2MB (PDE) page mapping requires that the corresponding 4KB 853 * (PTE) page mappings have identical settings for the following fields: 854 */ 855 #define PG_PTE_PROMOTE (PG_X | PG_MANAGED | PG_W | PG_PTE_CACHE | \ 856 PG_M | PG_A | RPTE_EAA_MASK | PG_V) 857 858 static __inline void 859 pmap_resident_count_inc(pmap_t pmap, int count) 860 { 861 862 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 863 pmap->pm_stats.resident_count += count; 864 } 865 866 static __inline void 867 pmap_resident_count_dec(pmap_t pmap, int count) 868 { 869 870 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 871 KASSERT(pmap->pm_stats.resident_count >= count, 872 ("pmap %p resident count underflow %ld %d", pmap, 873 pmap->pm_stats.resident_count, count)); 874 pmap->pm_stats.resident_count -= count; 875 } 876 877 static void 878 pagezero(vm_offset_t va) 879 { 880 va = trunc_page(va); 881 882 bzero((void *)va, PAGE_SIZE); 883 } 884 885 static uint64_t 886 allocpages(int n) 887 { 888 u_int64_t ret; 889 890 ret = moea64_bootstrap_alloc(n * PAGE_SIZE, PAGE_SIZE); 891 for (int i = 0; i < n; i++) 892 pagezero(PHYS_TO_DMAP(ret + i * PAGE_SIZE)); 893 return (ret); 894 } 895 896 static pt_entry_t * 897 kvtopte(vm_offset_t va) 898 { 899 pt_entry_t *l3e; 900 901 l3e = pmap_pml3e(kernel_pmap, va); 902 if ((be64toh(*l3e) & RPTE_VALID) == 0) 903 return (NULL); 904 return (pmap_l3e_to_pte(l3e, va)); 905 } 906 907 void 908 mmu_radix_kenter(vm_offset_t va, vm_paddr_t pa) 909 { 910 pt_entry_t *pte; 911 912 pte = kvtopte(va); 913 MPASS(pte != NULL); 914 *pte = htobe64(pa | RPTE_VALID | RPTE_LEAF | RPTE_EAA_R | \ 915 RPTE_EAA_W | RPTE_EAA_P | PG_M | PG_A); 916 } 917 918 bool 919 mmu_radix_ps_enabled(pmap_t pmap) 920 { 921 return (superpages_enabled && (pmap->pm_flags & PMAP_PDE_SUPERPAGE) != 0); 922 } 923 924 static pt_entry_t * 925 pmap_nofault_pte(pmap_t pmap, vm_offset_t va, int *is_l3e) 926 { 927 pml3_entry_t *l3e; 928 pt_entry_t *pte; 929 930 va &= PG_PS_FRAME; 931 l3e = pmap_pml3e(pmap, va); 932 if (l3e == NULL || (be64toh(*l3e) & PG_V) == 0) 933 return (NULL); 934 935 if (be64toh(*l3e) & RPTE_LEAF) { 936 *is_l3e = 1; 937 return (l3e); 938 } 939 *is_l3e = 0; 940 va &= PG_FRAME; 941 pte = pmap_l3e_to_pte(l3e, va); 942 if (pte == NULL || (be64toh(*pte) & PG_V) == 0) 943 return (NULL); 944 return (pte); 945 } 946 947 int 948 pmap_nofault(pmap_t pmap, vm_offset_t va, vm_prot_t flags) 949 { 950 pt_entry_t *pte; 951 pt_entry_t startpte, origpte, newpte; 952 vm_page_t m; 953 int is_l3e; 954 955 startpte = 0; 956 retry: 957 if ((pte = pmap_nofault_pte(pmap, va, &is_l3e)) == NULL) 958 return (KERN_INVALID_ADDRESS); 959 origpte = newpte = be64toh(*pte); 960 if (startpte == 0) { 961 startpte = origpte; 962 if (((flags & VM_PROT_WRITE) && (startpte & PG_M)) || 963 ((flags & VM_PROT_READ) && (startpte & PG_A))) { 964 pmap_invalidate_all(pmap); 965 #ifdef INVARIANTS 966 if (VERBOSE_PMAP || pmap_logging) 967 printf("%s(%p, %#lx, %#x) (%#lx) -- invalidate all\n", 968 __func__, pmap, va, flags, origpte); 969 #endif 970 return (KERN_FAILURE); 971 } 972 } 973 #ifdef INVARIANTS 974 if (VERBOSE_PMAP || pmap_logging) 975 printf("%s(%p, %#lx, %#x) (%#lx)\n", __func__, pmap, va, 976 flags, origpte); 977 #endif 978 PMAP_LOCK(pmap); 979 if ((pte = pmap_nofault_pte(pmap, va, &is_l3e)) == NULL || 980 be64toh(*pte) != origpte) { 981 PMAP_UNLOCK(pmap); 982 return (KERN_FAILURE); 983 } 984 m = PHYS_TO_VM_PAGE(newpte & PG_FRAME); 985 MPASS(m != NULL); 986 switch (flags) { 987 case VM_PROT_READ: 988 if ((newpte & (RPTE_EAA_R|RPTE_EAA_X)) == 0) 989 goto protfail; 990 newpte |= PG_A; 991 vm_page_aflag_set(m, PGA_REFERENCED); 992 break; 993 case VM_PROT_WRITE: 994 if ((newpte & RPTE_EAA_W) == 0) 995 goto protfail; 996 if (is_l3e) 997 goto protfail; 998 newpte |= PG_M; 999 vm_page_dirty(m); 1000 break; 1001 case VM_PROT_EXECUTE: 1002 if ((newpte & RPTE_EAA_X) == 0) 1003 goto protfail; 1004 newpte |= PG_A; 1005 vm_page_aflag_set(m, PGA_REFERENCED); 1006 break; 1007 } 1008 1009 if (!atomic_cmpset_long(pte, htobe64(origpte), htobe64(newpte))) 1010 goto retry; 1011 ptesync(); 1012 PMAP_UNLOCK(pmap); 1013 if (startpte == newpte) 1014 return (KERN_FAILURE); 1015 return (0); 1016 protfail: 1017 PMAP_UNLOCK(pmap); 1018 return (KERN_PROTECTION_FAILURE); 1019 } 1020 1021 /* 1022 * Returns TRUE if the given page is mapped individually or as part of 1023 * a 2mpage. Otherwise, returns FALSE. 1024 */ 1025 boolean_t 1026 mmu_radix_page_is_mapped(vm_page_t m) 1027 { 1028 struct rwlock *lock; 1029 boolean_t rv; 1030 1031 if ((m->oflags & VPO_UNMANAGED) != 0) 1032 return (FALSE); 1033 lock = VM_PAGE_TO_PV_LIST_LOCK(m); 1034 rw_rlock(lock); 1035 rv = !TAILQ_EMPTY(&m->md.pv_list) || 1036 ((m->flags & PG_FICTITIOUS) == 0 && 1037 !TAILQ_EMPTY(&pa_to_pvh(VM_PAGE_TO_PHYS(m))->pv_list)); 1038 rw_runlock(lock); 1039 return (rv); 1040 } 1041 1042 /* 1043 * Determine the appropriate bits to set in a PTE or PDE for a specified 1044 * caching mode. 1045 */ 1046 static int 1047 pmap_cache_bits(vm_memattr_t ma) 1048 { 1049 if (ma != VM_MEMATTR_DEFAULT) { 1050 switch (ma) { 1051 case VM_MEMATTR_UNCACHEABLE: 1052 return (RPTE_ATTR_GUARDEDIO); 1053 case VM_MEMATTR_CACHEABLE: 1054 return (RPTE_ATTR_MEM); 1055 case VM_MEMATTR_WRITE_BACK: 1056 case VM_MEMATTR_PREFETCHABLE: 1057 case VM_MEMATTR_WRITE_COMBINING: 1058 return (RPTE_ATTR_UNGUARDEDIO); 1059 } 1060 } 1061 return (0); 1062 } 1063 1064 static void 1065 pmap_invalidate_page(pmap_t pmap, vm_offset_t start) 1066 { 1067 ptesync(); 1068 if (pmap == kernel_pmap) 1069 radix_tlbie_invlpg_kernel_4k(start); 1070 else 1071 radix_tlbie_invlpg_user_4k(pmap->pm_pid, start); 1072 ttusync(); 1073 } 1074 1075 static void 1076 pmap_invalidate_page_2m(pmap_t pmap, vm_offset_t start) 1077 { 1078 ptesync(); 1079 if (pmap == kernel_pmap) 1080 radix_tlbie_invlpg_kernel_2m(start); 1081 else 1082 radix_tlbie_invlpg_user_2m(pmap->pm_pid, start); 1083 ttusync(); 1084 } 1085 1086 static void 1087 pmap_invalidate_pwc(pmap_t pmap) 1088 { 1089 ptesync(); 1090 if (pmap == kernel_pmap) 1091 radix_tlbie_invlpwc_kernel(); 1092 else 1093 radix_tlbie_invlpwc_user(pmap->pm_pid); 1094 ttusync(); 1095 } 1096 1097 static void 1098 pmap_invalidate_range(pmap_t pmap, vm_offset_t start, vm_offset_t end) 1099 { 1100 if (((start - end) >> PAGE_SHIFT) > 8) { 1101 pmap_invalidate_all(pmap); 1102 return; 1103 } 1104 ptesync(); 1105 if (pmap == kernel_pmap) { 1106 while (start < end) { 1107 radix_tlbie_invlpg_kernel_4k(start); 1108 start += PAGE_SIZE; 1109 } 1110 } else { 1111 while (start < end) { 1112 radix_tlbie_invlpg_user_4k(pmap->pm_pid, start); 1113 start += PAGE_SIZE; 1114 } 1115 } 1116 ttusync(); 1117 } 1118 1119 static void 1120 pmap_invalidate_all(pmap_t pmap) 1121 { 1122 ptesync(); 1123 if (pmap == kernel_pmap) 1124 radix_tlbie_flush_kernel(); 1125 else 1126 radix_tlbie_flush_user(pmap->pm_pid); 1127 ttusync(); 1128 } 1129 1130 static void 1131 pmap_invalidate_l3e_page(pmap_t pmap, vm_offset_t va, pml3_entry_t l3e) 1132 { 1133 1134 /* 1135 * When the PDE has PG_PROMOTED set, the 2MB page mapping was created 1136 * by a promotion that did not invalidate the 512 4KB page mappings 1137 * that might exist in the TLB. Consequently, at this point, the TLB 1138 * may hold both 4KB and 2MB page mappings for the address range [va, 1139 * va + L3_PAGE_SIZE). Therefore, the entire range must be invalidated here. 1140 * In contrast, when PG_PROMOTED is clear, the TLB will not hold any 1141 * 4KB page mappings for the address range [va, va + L3_PAGE_SIZE), and so a 1142 * single INVLPG suffices to invalidate the 2MB page mapping from the 1143 * TLB. 1144 */ 1145 ptesync(); 1146 if ((l3e & PG_PROMOTED) != 0) 1147 pmap_invalidate_range(pmap, va, va + L3_PAGE_SIZE - 1); 1148 else 1149 pmap_invalidate_page_2m(pmap, va); 1150 1151 pmap_invalidate_pwc(pmap); 1152 } 1153 1154 static __inline struct pv_chunk * 1155 pv_to_chunk(pv_entry_t pv) 1156 { 1157 1158 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK)); 1159 } 1160 1161 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap) 1162 1163 #define PC_FREE0 0xfffffffffffffffful 1164 #define PC_FREE1 0x3ffffffffffffffful 1165 1166 static const uint64_t pc_freemask[_NPCM] = { PC_FREE0, PC_FREE1 }; 1167 1168 /* 1169 * Ensure that the number of spare PV entries in the specified pmap meets or 1170 * exceeds the given count, "needed". 1171 * 1172 * The given PV list lock may be released. 1173 */ 1174 static void 1175 reserve_pv_entries(pmap_t pmap, int needed, struct rwlock **lockp) 1176 { 1177 struct pch new_tail; 1178 struct pv_chunk *pc; 1179 vm_page_t m; 1180 int avail, free; 1181 bool reclaimed; 1182 1183 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 1184 KASSERT(lockp != NULL, ("reserve_pv_entries: lockp is NULL")); 1185 1186 /* 1187 * Newly allocated PV chunks must be stored in a private list until 1188 * the required number of PV chunks have been allocated. Otherwise, 1189 * reclaim_pv_chunk() could recycle one of these chunks. In 1190 * contrast, these chunks must be added to the pmap upon allocation. 1191 */ 1192 TAILQ_INIT(&new_tail); 1193 retry: 1194 avail = 0; 1195 TAILQ_FOREACH(pc, &pmap->pm_pvchunk, pc_list) { 1196 // if ((cpu_feature2 & CPUID2_POPCNT) == 0) 1197 bit_count((bitstr_t *)pc->pc_map, 0, 1198 sizeof(pc->pc_map) * NBBY, &free); 1199 #if 0 1200 free = popcnt_pc_map_pq(pc->pc_map); 1201 #endif 1202 if (free == 0) 1203 break; 1204 avail += free; 1205 if (avail >= needed) 1206 break; 1207 } 1208 for (reclaimed = false; avail < needed; avail += _NPCPV) { 1209 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | 1210 VM_ALLOC_WIRED); 1211 if (m == NULL) { 1212 m = reclaim_pv_chunk(pmap, lockp); 1213 if (m == NULL) 1214 goto retry; 1215 reclaimed = true; 1216 } 1217 PV_STAT(atomic_add_int(&pc_chunk_count, 1)); 1218 PV_STAT(atomic_add_int(&pc_chunk_allocs, 1)); 1219 dump_add_page(m->phys_addr); 1220 pc = (void *)PHYS_TO_DMAP(m->phys_addr); 1221 pc->pc_pmap = pmap; 1222 pc->pc_map[0] = PC_FREE0; 1223 pc->pc_map[1] = PC_FREE1; 1224 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list); 1225 TAILQ_INSERT_TAIL(&new_tail, pc, pc_lru); 1226 PV_STAT(atomic_add_int(&pv_entry_spare, _NPCPV)); 1227 1228 /* 1229 * The reclaim might have freed a chunk from the current pmap. 1230 * If that chunk contained available entries, we need to 1231 * re-count the number of available entries. 1232 */ 1233 if (reclaimed) 1234 goto retry; 1235 } 1236 if (!TAILQ_EMPTY(&new_tail)) { 1237 mtx_lock(&pv_chunks_mutex); 1238 TAILQ_CONCAT(&pv_chunks, &new_tail, pc_lru); 1239 mtx_unlock(&pv_chunks_mutex); 1240 } 1241 } 1242 1243 /* 1244 * First find and then remove the pv entry for the specified pmap and virtual 1245 * address from the specified pv list. Returns the pv entry if found and NULL 1246 * otherwise. This operation can be performed on pv lists for either 4KB or 1247 * 2MB page mappings. 1248 */ 1249 static __inline pv_entry_t 1250 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va) 1251 { 1252 pv_entry_t pv; 1253 1254 TAILQ_FOREACH(pv, &pvh->pv_list, pv_link) { 1255 #ifdef INVARIANTS 1256 if (PV_PMAP(pv) == NULL) { 1257 printf("corrupted pv_chunk/pv %p\n", pv); 1258 printf("pv_chunk: %64D\n", pv_to_chunk(pv), ":"); 1259 } 1260 MPASS(PV_PMAP(pv) != NULL); 1261 MPASS(pv->pv_va != 0); 1262 #endif 1263 if (pmap == PV_PMAP(pv) && va == pv->pv_va) { 1264 TAILQ_REMOVE(&pvh->pv_list, pv, pv_link); 1265 pvh->pv_gen++; 1266 break; 1267 } 1268 } 1269 return (pv); 1270 } 1271 1272 /* 1273 * After demotion from a 2MB page mapping to 512 4KB page mappings, 1274 * destroy the pv entry for the 2MB page mapping and reinstantiate the pv 1275 * entries for each of the 4KB page mappings. 1276 */ 1277 static void 1278 pmap_pv_demote_l3e(pmap_t pmap, vm_offset_t va, vm_paddr_t pa, 1279 struct rwlock **lockp) 1280 { 1281 struct md_page *pvh; 1282 struct pv_chunk *pc; 1283 pv_entry_t pv; 1284 vm_offset_t va_last; 1285 vm_page_t m; 1286 int bit, field; 1287 1288 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 1289 KASSERT((pa & L3_PAGE_MASK) == 0, 1290 ("pmap_pv_demote_pde: pa is not 2mpage aligned")); 1291 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa); 1292 1293 /* 1294 * Transfer the 2mpage's pv entry for this mapping to the first 1295 * page's pv list. Once this transfer begins, the pv list lock 1296 * must not be released until the last pv entry is reinstantiated. 1297 */ 1298 pvh = pa_to_pvh(pa); 1299 va = trunc_2mpage(va); 1300 pv = pmap_pvh_remove(pvh, pmap, va); 1301 KASSERT(pv != NULL, ("pmap_pv_demote_pde: pv not found")); 1302 m = PHYS_TO_VM_PAGE(pa); 1303 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_link); 1304 1305 m->md.pv_gen++; 1306 /* Instantiate the remaining NPTEPG - 1 pv entries. */ 1307 PV_STAT(atomic_add_long(&pv_entry_allocs, NPTEPG - 1)); 1308 va_last = va + L3_PAGE_SIZE - PAGE_SIZE; 1309 for (;;) { 1310 pc = TAILQ_FIRST(&pmap->pm_pvchunk); 1311 KASSERT(pc->pc_map[0] != 0 || pc->pc_map[1] != 0 1312 , ("pmap_pv_demote_pde: missing spare")); 1313 for (field = 0; field < _NPCM; field++) { 1314 while (pc->pc_map[field]) { 1315 bit = cnttzd(pc->pc_map[field]); 1316 pc->pc_map[field] &= ~(1ul << bit); 1317 pv = &pc->pc_pventry[field * 64 + bit]; 1318 va += PAGE_SIZE; 1319 pv->pv_va = va; 1320 m++; 1321 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1322 ("pmap_pv_demote_pde: page %p is not managed", m)); 1323 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_link); 1324 1325 m->md.pv_gen++; 1326 if (va == va_last) 1327 goto out; 1328 } 1329 } 1330 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list); 1331 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list); 1332 } 1333 out: 1334 if (pc->pc_map[0] == 0 && pc->pc_map[1] == 0) { 1335 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list); 1336 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list); 1337 } 1338 PV_STAT(atomic_add_long(&pv_entry_count, NPTEPG - 1)); 1339 PV_STAT(atomic_subtract_int(&pv_entry_spare, NPTEPG - 1)); 1340 } 1341 1342 static void 1343 reclaim_pv_chunk_leave_pmap(pmap_t pmap, pmap_t locked_pmap) 1344 { 1345 1346 if (pmap == NULL) 1347 return; 1348 pmap_invalidate_all(pmap); 1349 if (pmap != locked_pmap) 1350 PMAP_UNLOCK(pmap); 1351 } 1352 1353 /* 1354 * We are in a serious low memory condition. Resort to 1355 * drastic measures to free some pages so we can allocate 1356 * another pv entry chunk. 1357 * 1358 * Returns NULL if PV entries were reclaimed from the specified pmap. 1359 * 1360 * We do not, however, unmap 2mpages because subsequent accesses will 1361 * allocate per-page pv entries until repromotion occurs, thereby 1362 * exacerbating the shortage of free pv entries. 1363 */ 1364 static int active_reclaims = 0; 1365 static vm_page_t 1366 reclaim_pv_chunk(pmap_t locked_pmap, struct rwlock **lockp) 1367 { 1368 struct pv_chunk *pc, *pc_marker, *pc_marker_end; 1369 struct pv_chunk_header pc_marker_b, pc_marker_end_b; 1370 struct md_page *pvh; 1371 pml3_entry_t *l3e; 1372 pmap_t next_pmap, pmap; 1373 pt_entry_t *pte, tpte; 1374 pv_entry_t pv; 1375 vm_offset_t va; 1376 vm_page_t m, m_pc; 1377 struct spglist free; 1378 uint64_t inuse; 1379 int bit, field, freed; 1380 1381 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED); 1382 KASSERT(lockp != NULL, ("reclaim_pv_chunk: lockp is NULL")); 1383 pmap = NULL; 1384 m_pc = NULL; 1385 SLIST_INIT(&free); 1386 bzero(&pc_marker_b, sizeof(pc_marker_b)); 1387 bzero(&pc_marker_end_b, sizeof(pc_marker_end_b)); 1388 pc_marker = (struct pv_chunk *)&pc_marker_b; 1389 pc_marker_end = (struct pv_chunk *)&pc_marker_end_b; 1390 1391 mtx_lock(&pv_chunks_mutex); 1392 active_reclaims++; 1393 TAILQ_INSERT_HEAD(&pv_chunks, pc_marker, pc_lru); 1394 TAILQ_INSERT_TAIL(&pv_chunks, pc_marker_end, pc_lru); 1395 while ((pc = TAILQ_NEXT(pc_marker, pc_lru)) != pc_marker_end && 1396 SLIST_EMPTY(&free)) { 1397 next_pmap = pc->pc_pmap; 1398 if (next_pmap == NULL) { 1399 /* 1400 * The next chunk is a marker. However, it is 1401 * not our marker, so active_reclaims must be 1402 * > 1. Consequently, the next_chunk code 1403 * will not rotate the pv_chunks list. 1404 */ 1405 goto next_chunk; 1406 } 1407 mtx_unlock(&pv_chunks_mutex); 1408 1409 /* 1410 * A pv_chunk can only be removed from the pc_lru list 1411 * when both pc_chunks_mutex is owned and the 1412 * corresponding pmap is locked. 1413 */ 1414 if (pmap != next_pmap) { 1415 reclaim_pv_chunk_leave_pmap(pmap, locked_pmap); 1416 pmap = next_pmap; 1417 /* Avoid deadlock and lock recursion. */ 1418 if (pmap > locked_pmap) { 1419 RELEASE_PV_LIST_LOCK(lockp); 1420 PMAP_LOCK(pmap); 1421 mtx_lock(&pv_chunks_mutex); 1422 continue; 1423 } else if (pmap != locked_pmap) { 1424 if (PMAP_TRYLOCK(pmap)) { 1425 mtx_lock(&pv_chunks_mutex); 1426 continue; 1427 } else { 1428 pmap = NULL; /* pmap is not locked */ 1429 mtx_lock(&pv_chunks_mutex); 1430 pc = TAILQ_NEXT(pc_marker, pc_lru); 1431 if (pc == NULL || 1432 pc->pc_pmap != next_pmap) 1433 continue; 1434 goto next_chunk; 1435 } 1436 } 1437 } 1438 1439 /* 1440 * Destroy every non-wired, 4 KB page mapping in the chunk. 1441 */ 1442 freed = 0; 1443 for (field = 0; field < _NPCM; field++) { 1444 for (inuse = ~pc->pc_map[field] & pc_freemask[field]; 1445 inuse != 0; inuse &= ~(1UL << bit)) { 1446 bit = cnttzd(inuse); 1447 pv = &pc->pc_pventry[field * 64 + bit]; 1448 va = pv->pv_va; 1449 l3e = pmap_pml3e(pmap, va); 1450 if ((be64toh(*l3e) & RPTE_LEAF) != 0) 1451 continue; 1452 pte = pmap_l3e_to_pte(l3e, va); 1453 if ((be64toh(*pte) & PG_W) != 0) 1454 continue; 1455 tpte = be64toh(pte_load_clear(pte)); 1456 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME); 1457 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) 1458 vm_page_dirty(m); 1459 if ((tpte & PG_A) != 0) 1460 vm_page_aflag_set(m, PGA_REFERENCED); 1461 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m); 1462 TAILQ_REMOVE(&m->md.pv_list, pv, pv_link); 1463 1464 m->md.pv_gen++; 1465 if (TAILQ_EMPTY(&m->md.pv_list) && 1466 (m->flags & PG_FICTITIOUS) == 0) { 1467 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m)); 1468 if (TAILQ_EMPTY(&pvh->pv_list)) { 1469 vm_page_aflag_clear(m, 1470 PGA_WRITEABLE); 1471 } 1472 } 1473 pc->pc_map[field] |= 1UL << bit; 1474 pmap_unuse_pt(pmap, va, be64toh(*l3e), &free); 1475 freed++; 1476 } 1477 } 1478 if (freed == 0) { 1479 mtx_lock(&pv_chunks_mutex); 1480 goto next_chunk; 1481 } 1482 /* Every freed mapping is for a 4 KB page. */ 1483 pmap_resident_count_dec(pmap, freed); 1484 PV_STAT(atomic_add_long(&pv_entry_frees, freed)); 1485 PV_STAT(atomic_add_int(&pv_entry_spare, freed)); 1486 PV_STAT(atomic_subtract_long(&pv_entry_count, freed)); 1487 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list); 1488 if (pc->pc_map[0] == PC_FREE0 && pc->pc_map[1] == PC_FREE1) { 1489 PV_STAT(atomic_subtract_int(&pv_entry_spare, _NPCPV)); 1490 PV_STAT(atomic_subtract_int(&pc_chunk_count, 1)); 1491 PV_STAT(atomic_add_int(&pc_chunk_frees, 1)); 1492 /* Entire chunk is free; return it. */ 1493 m_pc = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc)); 1494 dump_drop_page(m_pc->phys_addr); 1495 mtx_lock(&pv_chunks_mutex); 1496 TAILQ_REMOVE(&pv_chunks, pc, pc_lru); 1497 break; 1498 } 1499 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list); 1500 mtx_lock(&pv_chunks_mutex); 1501 /* One freed pv entry in locked_pmap is sufficient. */ 1502 if (pmap == locked_pmap) 1503 break; 1504 next_chunk: 1505 TAILQ_REMOVE(&pv_chunks, pc_marker, pc_lru); 1506 TAILQ_INSERT_AFTER(&pv_chunks, pc, pc_marker, pc_lru); 1507 if (active_reclaims == 1 && pmap != NULL) { 1508 /* 1509 * Rotate the pv chunks list so that we do not 1510 * scan the same pv chunks that could not be 1511 * freed (because they contained a wired 1512 * and/or superpage mapping) on every 1513 * invocation of reclaim_pv_chunk(). 1514 */ 1515 while ((pc = TAILQ_FIRST(&pv_chunks)) != pc_marker) { 1516 MPASS(pc->pc_pmap != NULL); 1517 TAILQ_REMOVE(&pv_chunks, pc, pc_lru); 1518 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru); 1519 } 1520 } 1521 } 1522 TAILQ_REMOVE(&pv_chunks, pc_marker, pc_lru); 1523 TAILQ_REMOVE(&pv_chunks, pc_marker_end, pc_lru); 1524 active_reclaims--; 1525 mtx_unlock(&pv_chunks_mutex); 1526 reclaim_pv_chunk_leave_pmap(pmap, locked_pmap); 1527 if (m_pc == NULL && !SLIST_EMPTY(&free)) { 1528 m_pc = SLIST_FIRST(&free); 1529 SLIST_REMOVE_HEAD(&free, plinks.s.ss); 1530 /* Recycle a freed page table page. */ 1531 m_pc->ref_count = 1; 1532 } 1533 vm_page_free_pages_toq(&free, true); 1534 return (m_pc); 1535 } 1536 1537 /* 1538 * free the pv_entry back to the free list 1539 */ 1540 static void 1541 free_pv_entry(pmap_t pmap, pv_entry_t pv) 1542 { 1543 struct pv_chunk *pc; 1544 int idx, field, bit; 1545 1546 #ifdef VERBOSE_PV 1547 if (pmap != kernel_pmap) 1548 printf("%s(%p, %p)\n", __func__, pmap, pv); 1549 #endif 1550 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 1551 PV_STAT(atomic_add_long(&pv_entry_frees, 1)); 1552 PV_STAT(atomic_add_int(&pv_entry_spare, 1)); 1553 PV_STAT(atomic_subtract_long(&pv_entry_count, 1)); 1554 pc = pv_to_chunk(pv); 1555 idx = pv - &pc->pc_pventry[0]; 1556 field = idx / 64; 1557 bit = idx % 64; 1558 pc->pc_map[field] |= 1ul << bit; 1559 if (pc->pc_map[0] != PC_FREE0 || pc->pc_map[1] != PC_FREE1) { 1560 /* 98% of the time, pc is already at the head of the list. */ 1561 if (__predict_false(pc != TAILQ_FIRST(&pmap->pm_pvchunk))) { 1562 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list); 1563 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list); 1564 } 1565 return; 1566 } 1567 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list); 1568 free_pv_chunk(pc); 1569 } 1570 1571 static void 1572 free_pv_chunk(struct pv_chunk *pc) 1573 { 1574 vm_page_t m; 1575 1576 mtx_lock(&pv_chunks_mutex); 1577 TAILQ_REMOVE(&pv_chunks, pc, pc_lru); 1578 mtx_unlock(&pv_chunks_mutex); 1579 PV_STAT(atomic_subtract_int(&pv_entry_spare, _NPCPV)); 1580 PV_STAT(atomic_subtract_int(&pc_chunk_count, 1)); 1581 PV_STAT(atomic_add_int(&pc_chunk_frees, 1)); 1582 /* entire chunk is free, return it */ 1583 m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc)); 1584 dump_drop_page(m->phys_addr); 1585 vm_page_unwire_noq(m); 1586 vm_page_free(m); 1587 } 1588 1589 /* 1590 * Returns a new PV entry, allocating a new PV chunk from the system when 1591 * needed. If this PV chunk allocation fails and a PV list lock pointer was 1592 * given, a PV chunk is reclaimed from an arbitrary pmap. Otherwise, NULL is 1593 * returned. 1594 * 1595 * The given PV list lock may be released. 1596 */ 1597 static pv_entry_t 1598 get_pv_entry(pmap_t pmap, struct rwlock **lockp) 1599 { 1600 int bit, field; 1601 pv_entry_t pv; 1602 struct pv_chunk *pc; 1603 vm_page_t m; 1604 1605 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 1606 PV_STAT(atomic_add_long(&pv_entry_allocs, 1)); 1607 retry: 1608 pc = TAILQ_FIRST(&pmap->pm_pvchunk); 1609 if (pc != NULL) { 1610 for (field = 0; field < _NPCM; field++) { 1611 if (pc->pc_map[field]) { 1612 bit = cnttzd(pc->pc_map[field]); 1613 break; 1614 } 1615 } 1616 if (field < _NPCM) { 1617 pv = &pc->pc_pventry[field * 64 + bit]; 1618 pc->pc_map[field] &= ~(1ul << bit); 1619 /* If this was the last item, move it to tail */ 1620 if (pc->pc_map[0] == 0 && pc->pc_map[1] == 0) { 1621 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list); 1622 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, 1623 pc_list); 1624 } 1625 PV_STAT(atomic_add_long(&pv_entry_count, 1)); 1626 PV_STAT(atomic_subtract_int(&pv_entry_spare, 1)); 1627 MPASS(PV_PMAP(pv) != NULL); 1628 return (pv); 1629 } 1630 } 1631 /* No free items, allocate another chunk */ 1632 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | 1633 VM_ALLOC_WIRED); 1634 if (m == NULL) { 1635 if (lockp == NULL) { 1636 PV_STAT(pc_chunk_tryfail++); 1637 return (NULL); 1638 } 1639 m = reclaim_pv_chunk(pmap, lockp); 1640 if (m == NULL) 1641 goto retry; 1642 } 1643 PV_STAT(atomic_add_int(&pc_chunk_count, 1)); 1644 PV_STAT(atomic_add_int(&pc_chunk_allocs, 1)); 1645 dump_add_page(m->phys_addr); 1646 pc = (void *)PHYS_TO_DMAP(m->phys_addr); 1647 pc->pc_pmap = pmap; 1648 pc->pc_map[0] = PC_FREE0 & ~1ul; /* preallocated bit 0 */ 1649 pc->pc_map[1] = PC_FREE1; 1650 mtx_lock(&pv_chunks_mutex); 1651 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru); 1652 mtx_unlock(&pv_chunks_mutex); 1653 pv = &pc->pc_pventry[0]; 1654 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list); 1655 PV_STAT(atomic_add_long(&pv_entry_count, 1)); 1656 PV_STAT(atomic_add_int(&pv_entry_spare, _NPCPV - 1)); 1657 MPASS(PV_PMAP(pv) != NULL); 1658 return (pv); 1659 } 1660 1661 #if VM_NRESERVLEVEL > 0 1662 /* 1663 * After promotion from 512 4KB page mappings to a single 2MB page mapping, 1664 * replace the many pv entries for the 4KB page mappings by a single pv entry 1665 * for the 2MB page mapping. 1666 */ 1667 static void 1668 pmap_pv_promote_l3e(pmap_t pmap, vm_offset_t va, vm_paddr_t pa, 1669 struct rwlock **lockp) 1670 { 1671 struct md_page *pvh; 1672 pv_entry_t pv; 1673 vm_offset_t va_last; 1674 vm_page_t m; 1675 1676 KASSERT((pa & L3_PAGE_MASK) == 0, 1677 ("pmap_pv_promote_pde: pa is not 2mpage aligned")); 1678 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa); 1679 1680 /* 1681 * Transfer the first page's pv entry for this mapping to the 2mpage's 1682 * pv list. Aside from avoiding the cost of a call to get_pv_entry(), 1683 * a transfer avoids the possibility that get_pv_entry() calls 1684 * reclaim_pv_chunk() and that reclaim_pv_chunk() removes one of the 1685 * mappings that is being promoted. 1686 */ 1687 m = PHYS_TO_VM_PAGE(pa); 1688 va = trunc_2mpage(va); 1689 pv = pmap_pvh_remove(&m->md, pmap, va); 1690 KASSERT(pv != NULL, ("pmap_pv_promote_pde: pv not found")); 1691 pvh = pa_to_pvh(pa); 1692 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_link); 1693 pvh->pv_gen++; 1694 /* Free the remaining NPTEPG - 1 pv entries. */ 1695 va_last = va + L3_PAGE_SIZE - PAGE_SIZE; 1696 do { 1697 m++; 1698 va += PAGE_SIZE; 1699 pmap_pvh_free(&m->md, pmap, va); 1700 } while (va < va_last); 1701 } 1702 #endif /* VM_NRESERVLEVEL > 0 */ 1703 1704 /* 1705 * First find and then destroy the pv entry for the specified pmap and virtual 1706 * address. This operation can be performed on pv lists for either 4KB or 2MB 1707 * page mappings. 1708 */ 1709 static void 1710 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va) 1711 { 1712 pv_entry_t pv; 1713 1714 pv = pmap_pvh_remove(pvh, pmap, va); 1715 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found")); 1716 free_pv_entry(pmap, pv); 1717 } 1718 1719 /* 1720 * Conditionally create the PV entry for a 4KB page mapping if the required 1721 * memory can be allocated without resorting to reclamation. 1722 */ 1723 static boolean_t 1724 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m, 1725 struct rwlock **lockp) 1726 { 1727 pv_entry_t pv; 1728 1729 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 1730 /* Pass NULL instead of the lock pointer to disable reclamation. */ 1731 if ((pv = get_pv_entry(pmap, NULL)) != NULL) { 1732 pv->pv_va = va; 1733 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m); 1734 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_link); 1735 m->md.pv_gen++; 1736 return (TRUE); 1737 } else 1738 return (FALSE); 1739 } 1740 1741 vm_paddr_t phys_avail_debug[2 * VM_PHYSSEG_MAX]; 1742 #ifdef INVARIANTS 1743 static void 1744 validate_addr(vm_paddr_t addr, vm_size_t size) 1745 { 1746 vm_paddr_t end = addr + size; 1747 bool found = false; 1748 1749 for (int i = 0; i < 2 * phys_avail_count; i += 2) { 1750 if (addr >= phys_avail_debug[i] && 1751 end <= phys_avail_debug[i + 1]) { 1752 found = true; 1753 break; 1754 } 1755 } 1756 KASSERT(found, ("%#lx-%#lx outside of initial phys_avail array", 1757 addr, end)); 1758 } 1759 #else 1760 static void validate_addr(vm_paddr_t addr, vm_size_t size) {} 1761 #endif 1762 #define DMAP_PAGE_BITS (RPTE_VALID | RPTE_LEAF | RPTE_EAA_MASK | PG_M | PG_A) 1763 1764 static vm_paddr_t 1765 alloc_pt_page(void) 1766 { 1767 vm_paddr_t page; 1768 1769 page = allocpages(1); 1770 pagezero(PHYS_TO_DMAP(page)); 1771 return (page); 1772 } 1773 1774 static void 1775 mmu_radix_dmap_range(vm_paddr_t start, vm_paddr_t end) 1776 { 1777 pt_entry_t *pte, pteval; 1778 vm_paddr_t page; 1779 1780 if (bootverbose) 1781 printf("%s %lx -> %lx\n", __func__, start, end); 1782 while (start < end) { 1783 pteval = start | DMAP_PAGE_BITS; 1784 pte = pmap_pml1e(kernel_pmap, PHYS_TO_DMAP(start)); 1785 if ((be64toh(*pte) & RPTE_VALID) == 0) { 1786 page = alloc_pt_page(); 1787 pde_store(pte, page); 1788 } 1789 pte = pmap_l1e_to_l2e(pte, PHYS_TO_DMAP(start)); 1790 if ((start & L2_PAGE_MASK) == 0 && 1791 end - start >= L2_PAGE_SIZE) { 1792 start += L2_PAGE_SIZE; 1793 goto done; 1794 } else if ((be64toh(*pte) & RPTE_VALID) == 0) { 1795 page = alloc_pt_page(); 1796 pde_store(pte, page); 1797 } 1798 1799 pte = pmap_l2e_to_l3e(pte, PHYS_TO_DMAP(start)); 1800 if ((start & L3_PAGE_MASK) == 0 && 1801 end - start >= L3_PAGE_SIZE) { 1802 start += L3_PAGE_SIZE; 1803 goto done; 1804 } else if ((be64toh(*pte) & RPTE_VALID) == 0) { 1805 page = alloc_pt_page(); 1806 pde_store(pte, page); 1807 } 1808 pte = pmap_l3e_to_pte(pte, PHYS_TO_DMAP(start)); 1809 start += PAGE_SIZE; 1810 done: 1811 pte_store(pte, pteval); 1812 } 1813 } 1814 1815 static void 1816 mmu_radix_dmap_populate(vm_size_t hwphyssz) 1817 { 1818 vm_paddr_t start, end; 1819 1820 for (int i = 0; i < pregions_sz; i++) { 1821 start = pregions[i].mr_start; 1822 end = start + pregions[i].mr_size; 1823 if (hwphyssz && start >= hwphyssz) 1824 break; 1825 if (hwphyssz && hwphyssz < end) 1826 end = hwphyssz; 1827 mmu_radix_dmap_range(start, end); 1828 } 1829 } 1830 1831 static void 1832 mmu_radix_setup_pagetables(vm_size_t hwphyssz) 1833 { 1834 vm_paddr_t ptpages, pages; 1835 pt_entry_t *pte; 1836 vm_paddr_t l1phys; 1837 1838 bzero(kernel_pmap, sizeof(struct pmap)); 1839 PMAP_LOCK_INIT(kernel_pmap); 1840 1841 ptpages = allocpages(3); 1842 l1phys = moea64_bootstrap_alloc(RADIX_PGD_SIZE, RADIX_PGD_SIZE); 1843 validate_addr(l1phys, RADIX_PGD_SIZE); 1844 if (bootverbose) 1845 printf("l1phys=%lx\n", l1phys); 1846 MPASS((l1phys & (RADIX_PGD_SIZE-1)) == 0); 1847 for (int i = 0; i < RADIX_PGD_SIZE/PAGE_SIZE; i++) 1848 pagezero(PHYS_TO_DMAP(l1phys + i * PAGE_SIZE)); 1849 kernel_pmap->pm_pml1 = (pml1_entry_t *)PHYS_TO_DMAP(l1phys); 1850 1851 mmu_radix_dmap_populate(hwphyssz); 1852 1853 /* 1854 * Create page tables for first 128MB of KVA 1855 */ 1856 pages = ptpages; 1857 pte = pmap_pml1e(kernel_pmap, VM_MIN_KERNEL_ADDRESS); 1858 *pte = htobe64(pages | RPTE_VALID | RPTE_SHIFT); 1859 pages += PAGE_SIZE; 1860 pte = pmap_l1e_to_l2e(pte, VM_MIN_KERNEL_ADDRESS); 1861 *pte = htobe64(pages | RPTE_VALID | RPTE_SHIFT); 1862 pages += PAGE_SIZE; 1863 pte = pmap_l2e_to_l3e(pte, VM_MIN_KERNEL_ADDRESS); 1864 /* 1865 * the kernel page table pages need to be preserved in 1866 * phys_avail and not overlap with previous allocations 1867 */ 1868 pages = allocpages(nkpt); 1869 if (bootverbose) { 1870 printf("phys_avail after dmap populate and nkpt allocation\n"); 1871 for (int j = 0; j < 2 * phys_avail_count; j+=2) 1872 printf("phys_avail[%d]=%08lx - phys_avail[%d]=%08lx\n", 1873 j, phys_avail[j], j + 1, phys_avail[j + 1]); 1874 } 1875 KPTphys = pages; 1876 for (int i = 0; i < nkpt; i++, pte++, pages += PAGE_SIZE) 1877 *pte = htobe64(pages | RPTE_VALID | RPTE_SHIFT); 1878 kernel_vm_end = VM_MIN_KERNEL_ADDRESS + nkpt * L3_PAGE_SIZE; 1879 if (bootverbose) 1880 printf("kernel_pmap pml1 %p\n", kernel_pmap->pm_pml1); 1881 /* 1882 * Add a physical memory segment (vm_phys_seg) corresponding to the 1883 * preallocated kernel page table pages so that vm_page structures 1884 * representing these pages will be created. The vm_page structures 1885 * are required for promotion of the corresponding kernel virtual 1886 * addresses to superpage mappings. 1887 */ 1888 vm_phys_add_seg(KPTphys, KPTphys + ptoa(nkpt)); 1889 } 1890 1891 static void 1892 mmu_radix_early_bootstrap(vm_offset_t start, vm_offset_t end) 1893 { 1894 vm_paddr_t kpstart, kpend; 1895 vm_size_t physsz, hwphyssz; 1896 //uint64_t l2virt; 1897 int rm_pavail, proctab_size; 1898 int i, j; 1899 1900 kpstart = start & ~DMAP_BASE_ADDRESS; 1901 kpend = end & ~DMAP_BASE_ADDRESS; 1902 1903 /* Get physical memory regions from firmware */ 1904 mem_regions(&pregions, &pregions_sz, ®ions, ®ions_sz); 1905 CTR0(KTR_PMAP, "mmu_radix_early_bootstrap: physical memory"); 1906 1907 if (2 * VM_PHYSSEG_MAX < regions_sz) 1908 panic("mmu_radix_early_bootstrap: phys_avail too small"); 1909 1910 if (bootverbose) 1911 for (int i = 0; i < regions_sz; i++) 1912 printf("regions[%d].mr_start=%lx regions[%d].mr_size=%lx\n", 1913 i, regions[i].mr_start, i, regions[i].mr_size); 1914 /* 1915 * XXX workaround a simulator bug 1916 */ 1917 for (int i = 0; i < regions_sz; i++) 1918 if (regions[i].mr_start & PAGE_MASK) { 1919 regions[i].mr_start += PAGE_MASK; 1920 regions[i].mr_start &= ~PAGE_MASK; 1921 regions[i].mr_size &= ~PAGE_MASK; 1922 } 1923 if (bootverbose) 1924 for (int i = 0; i < pregions_sz; i++) 1925 printf("pregions[%d].mr_start=%lx pregions[%d].mr_size=%lx\n", 1926 i, pregions[i].mr_start, i, pregions[i].mr_size); 1927 1928 phys_avail_count = 0; 1929 physsz = 0; 1930 hwphyssz = 0; 1931 TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz); 1932 for (i = 0, j = 0; i < regions_sz; i++) { 1933 if (bootverbose) 1934 printf("regions[%d].mr_start=%016lx regions[%d].mr_size=%016lx\n", 1935 i, regions[i].mr_start, i, regions[i].mr_size); 1936 1937 if (regions[i].mr_size < PAGE_SIZE) 1938 continue; 1939 1940 if (hwphyssz != 0 && 1941 (physsz + regions[i].mr_size) >= hwphyssz) { 1942 if (physsz < hwphyssz) { 1943 phys_avail[j] = regions[i].mr_start; 1944 phys_avail[j + 1] = regions[i].mr_start + 1945 (hwphyssz - physsz); 1946 physsz = hwphyssz; 1947 phys_avail_count++; 1948 dump_avail[j] = phys_avail[j]; 1949 dump_avail[j + 1] = phys_avail[j + 1]; 1950 } 1951 break; 1952 } 1953 phys_avail[j] = regions[i].mr_start; 1954 phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size; 1955 dump_avail[j] = phys_avail[j]; 1956 dump_avail[j + 1] = phys_avail[j + 1]; 1957 1958 phys_avail_count++; 1959 physsz += regions[i].mr_size; 1960 j += 2; 1961 } 1962 1963 /* Check for overlap with the kernel and exception vectors */ 1964 rm_pavail = 0; 1965 for (j = 0; j < 2 * phys_avail_count; j+=2) { 1966 if (phys_avail[j] < EXC_LAST) 1967 phys_avail[j] += EXC_LAST; 1968 1969 if (phys_avail[j] >= kpstart && 1970 phys_avail[j + 1] <= kpend) { 1971 phys_avail[j] = phys_avail[j + 1] = ~0; 1972 rm_pavail++; 1973 continue; 1974 } 1975 1976 if (kpstart >= phys_avail[j] && 1977 kpstart < phys_avail[j + 1]) { 1978 if (kpend < phys_avail[j + 1]) { 1979 phys_avail[2 * phys_avail_count] = 1980 (kpend & ~PAGE_MASK) + PAGE_SIZE; 1981 phys_avail[2 * phys_avail_count + 1] = 1982 phys_avail[j + 1]; 1983 phys_avail_count++; 1984 } 1985 1986 phys_avail[j + 1] = kpstart & ~PAGE_MASK; 1987 } 1988 1989 if (kpend >= phys_avail[j] && 1990 kpend < phys_avail[j + 1]) { 1991 if (kpstart > phys_avail[j]) { 1992 phys_avail[2 * phys_avail_count] = phys_avail[j]; 1993 phys_avail[2 * phys_avail_count + 1] = 1994 kpstart & ~PAGE_MASK; 1995 phys_avail_count++; 1996 } 1997 1998 phys_avail[j] = (kpend & ~PAGE_MASK) + 1999 PAGE_SIZE; 2000 } 2001 } 2002 qsort(phys_avail, 2 * phys_avail_count, sizeof(phys_avail[0]), pa_cmp); 2003 for (i = 0; i < 2 * phys_avail_count; i++) 2004 phys_avail_debug[i] = phys_avail[i]; 2005 2006 /* Remove physical available regions marked for removal (~0) */ 2007 if (rm_pavail) { 2008 phys_avail_count -= rm_pavail; 2009 for (i = 2 * phys_avail_count; 2010 i < 2*(phys_avail_count + rm_pavail); i+=2) 2011 phys_avail[i] = phys_avail[i + 1] = 0; 2012 } 2013 if (bootverbose) { 2014 printf("phys_avail ranges after filtering:\n"); 2015 for (j = 0; j < 2 * phys_avail_count; j+=2) 2016 printf("phys_avail[%d]=%08lx - phys_avail[%d]=%08lx\n", 2017 j, phys_avail[j], j + 1, phys_avail[j + 1]); 2018 } 2019 physmem = btoc(physsz); 2020 2021 /* XXX assume we're running non-virtualized and 2022 * we don't support BHYVE 2023 */ 2024 if (isa3_pid_bits == 0) 2025 isa3_pid_bits = 20; 2026 parttab_phys = moea64_bootstrap_alloc(PARTTAB_SIZE, PARTTAB_SIZE); 2027 validate_addr(parttab_phys, PARTTAB_SIZE); 2028 for (int i = 0; i < PARTTAB_SIZE/PAGE_SIZE; i++) 2029 pagezero(PHYS_TO_DMAP(parttab_phys + i * PAGE_SIZE)); 2030 2031 proctab_size = 1UL << PROCTAB_SIZE_SHIFT; 2032 proctab0pa = moea64_bootstrap_alloc(proctab_size, proctab_size); 2033 validate_addr(proctab0pa, proctab_size); 2034 for (int i = 0; i < proctab_size/PAGE_SIZE; i++) 2035 pagezero(PHYS_TO_DMAP(proctab0pa + i * PAGE_SIZE)); 2036 2037 mmu_radix_setup_pagetables(hwphyssz); 2038 } 2039 2040 static void 2041 mmu_radix_late_bootstrap(vm_offset_t start, vm_offset_t end) 2042 { 2043 int i; 2044 vm_paddr_t pa; 2045 void *dpcpu; 2046 vm_offset_t va; 2047 2048 /* 2049 * Set up the Open Firmware pmap and add its mappings if not in real 2050 * mode. 2051 */ 2052 if (bootverbose) 2053 printf("%s enter\n", __func__); 2054 2055 /* 2056 * Calculate the last available physical address, and reserve the 2057 * vm_page_array (upper bound). 2058 */ 2059 Maxmem = 0; 2060 for (i = 0; phys_avail[i + 2] != 0; i += 2) 2061 Maxmem = MAX(Maxmem, powerpc_btop(phys_avail[i + 1])); 2062 2063 /* 2064 * Set the start and end of kva. 2065 */ 2066 virtual_avail = VM_MIN_KERNEL_ADDRESS; 2067 virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS; 2068 2069 /* 2070 * Remap any early IO mappings (console framebuffer, etc.) 2071 */ 2072 bs_remap_earlyboot(); 2073 2074 /* 2075 * Allocate a kernel stack with a guard page for thread0 and map it 2076 * into the kernel page map. 2077 */ 2078 pa = allocpages(kstack_pages); 2079 va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE; 2080 virtual_avail = va + kstack_pages * PAGE_SIZE; 2081 CTR2(KTR_PMAP, "moea64_bootstrap: kstack0 at %#x (%#x)", pa, va); 2082 thread0.td_kstack = va; 2083 for (i = 0; i < kstack_pages; i++) { 2084 mmu_radix_kenter(va, pa); 2085 pa += PAGE_SIZE; 2086 va += PAGE_SIZE; 2087 } 2088 thread0.td_kstack_pages = kstack_pages; 2089 2090 /* 2091 * Allocate virtual address space for the message buffer. 2092 */ 2093 pa = msgbuf_phys = allocpages((msgbufsize + PAGE_MASK) >> PAGE_SHIFT); 2094 msgbufp = (struct msgbuf *)PHYS_TO_DMAP(pa); 2095 2096 /* 2097 * Allocate virtual address space for the dynamic percpu area. 2098 */ 2099 pa = allocpages(DPCPU_SIZE >> PAGE_SHIFT); 2100 dpcpu = (void *)PHYS_TO_DMAP(pa); 2101 dpcpu_init(dpcpu, curcpu); 2102 2103 crashdumpmap = (caddr_t)virtual_avail; 2104 virtual_avail += MAXDUMPPGS * PAGE_SIZE; 2105 2106 /* 2107 * Reserve some special page table entries/VA space for temporary 2108 * mapping of pages. 2109 */ 2110 } 2111 2112 static void 2113 mmu_parttab_init(void) 2114 { 2115 uint64_t ptcr; 2116 2117 isa3_parttab = (struct pate *)PHYS_TO_DMAP(parttab_phys); 2118 2119 if (bootverbose) 2120 printf("%s parttab: %p\n", __func__, isa3_parttab); 2121 ptcr = parttab_phys | (PARTTAB_SIZE_SHIFT-12); 2122 if (bootverbose) 2123 printf("setting ptcr %lx\n", ptcr); 2124 mtspr(SPR_PTCR, ptcr); 2125 } 2126 2127 static void 2128 mmu_parttab_update(uint64_t lpid, uint64_t pagetab, uint64_t proctab) 2129 { 2130 uint64_t prev; 2131 2132 if (bootverbose) 2133 printf("%s isa3_parttab %p lpid %lx pagetab %lx proctab %lx\n", __func__, isa3_parttab, 2134 lpid, pagetab, proctab); 2135 prev = be64toh(isa3_parttab[lpid].pagetab); 2136 isa3_parttab[lpid].pagetab = htobe64(pagetab); 2137 isa3_parttab[lpid].proctab = htobe64(proctab); 2138 2139 if (prev & PARTTAB_HR) { 2140 __asm __volatile(PPC_TLBIE_5(%0,%1,2,0,1) : : 2141 "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid)); 2142 __asm __volatile(PPC_TLBIE_5(%0,%1,2,1,1) : : 2143 "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid)); 2144 } else { 2145 __asm __volatile(PPC_TLBIE_5(%0,%1,2,0,0) : : 2146 "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid)); 2147 } 2148 ttusync(); 2149 } 2150 2151 static void 2152 mmu_radix_parttab_init(void) 2153 { 2154 uint64_t pagetab; 2155 2156 mmu_parttab_init(); 2157 pagetab = RTS_SIZE | DMAP_TO_PHYS((vm_offset_t)kernel_pmap->pm_pml1) | \ 2158 RADIX_PGD_INDEX_SHIFT | PARTTAB_HR; 2159 mmu_parttab_update(0, pagetab, 0); 2160 } 2161 2162 static void 2163 mmu_radix_proctab_register(vm_paddr_t proctabpa, uint64_t table_size) 2164 { 2165 uint64_t pagetab, proctab; 2166 2167 pagetab = be64toh(isa3_parttab[0].pagetab); 2168 proctab = proctabpa | table_size | PARTTAB_GR; 2169 mmu_parttab_update(0, pagetab, proctab); 2170 } 2171 2172 static void 2173 mmu_radix_proctab_init(void) 2174 { 2175 2176 isa3_base_pid = 1; 2177 2178 isa3_proctab = (void*)PHYS_TO_DMAP(proctab0pa); 2179 isa3_proctab->proctab0 = 2180 htobe64(RTS_SIZE | DMAP_TO_PHYS((vm_offset_t)kernel_pmap->pm_pml1) | 2181 RADIX_PGD_INDEX_SHIFT); 2182 2183 mmu_radix_proctab_register(proctab0pa, PROCTAB_SIZE_SHIFT - 12); 2184 2185 __asm __volatile("ptesync" : : : "memory"); 2186 __asm __volatile(PPC_TLBIE_5(%0,%1,2,1,1) : : 2187 "r" (TLBIEL_INVAL_SET_LPID), "r" (0)); 2188 __asm __volatile("eieio; tlbsync; ptesync" : : : "memory"); 2189 if (bootverbose) 2190 printf("process table %p and kernel radix PDE: %p\n", 2191 isa3_proctab, kernel_pmap->pm_pml1); 2192 mtmsr(mfmsr() | PSL_DR ); 2193 mtmsr(mfmsr() & ~PSL_DR); 2194 kernel_pmap->pm_pid = isa3_base_pid; 2195 isa3_base_pid++; 2196 } 2197 2198 void 2199 mmu_radix_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, 2200 int advice) 2201 { 2202 struct rwlock *lock; 2203 pml1_entry_t *l1e; 2204 pml2_entry_t *l2e; 2205 pml3_entry_t oldl3e, *l3e; 2206 pt_entry_t *pte; 2207 vm_offset_t va, va_next; 2208 vm_page_t m; 2209 boolean_t anychanged; 2210 2211 if (advice != MADV_DONTNEED && advice != MADV_FREE) 2212 return; 2213 anychanged = FALSE; 2214 PMAP_LOCK(pmap); 2215 for (; sva < eva; sva = va_next) { 2216 l1e = pmap_pml1e(pmap, sva); 2217 if ((be64toh(*l1e) & PG_V) == 0) { 2218 va_next = (sva + L1_PAGE_SIZE) & ~L1_PAGE_MASK; 2219 if (va_next < sva) 2220 va_next = eva; 2221 continue; 2222 } 2223 l2e = pmap_l1e_to_l2e(l1e, sva); 2224 if ((be64toh(*l2e) & PG_V) == 0) { 2225 va_next = (sva + L2_PAGE_SIZE) & ~L2_PAGE_MASK; 2226 if (va_next < sva) 2227 va_next = eva; 2228 continue; 2229 } 2230 va_next = (sva + L3_PAGE_SIZE) & ~L3_PAGE_MASK; 2231 if (va_next < sva) 2232 va_next = eva; 2233 l3e = pmap_l2e_to_l3e(l2e, sva); 2234 oldl3e = be64toh(*l3e); 2235 if ((oldl3e & PG_V) == 0) 2236 continue; 2237 else if ((oldl3e & RPTE_LEAF) != 0) { 2238 if ((oldl3e & PG_MANAGED) == 0) 2239 continue; 2240 lock = NULL; 2241 if (!pmap_demote_l3e_locked(pmap, l3e, sva, &lock)) { 2242 if (lock != NULL) 2243 rw_wunlock(lock); 2244 2245 /* 2246 * The large page mapping was destroyed. 2247 */ 2248 continue; 2249 } 2250 2251 /* 2252 * Unless the page mappings are wired, remove the 2253 * mapping to a single page so that a subsequent 2254 * access may repromote. Since the underlying page 2255 * table page is fully populated, this removal never 2256 * frees a page table page. 2257 */ 2258 if ((oldl3e & PG_W) == 0) { 2259 pte = pmap_l3e_to_pte(l3e, sva); 2260 KASSERT((be64toh(*pte) & PG_V) != 0, 2261 ("pmap_advise: invalid PTE")); 2262 pmap_remove_pte(pmap, pte, sva, be64toh(*l3e), NULL, 2263 &lock); 2264 anychanged = TRUE; 2265 } 2266 if (lock != NULL) 2267 rw_wunlock(lock); 2268 } 2269 if (va_next > eva) 2270 va_next = eva; 2271 va = va_next; 2272 for (pte = pmap_l3e_to_pte(l3e, sva); sva != va_next; 2273 pte++, sva += PAGE_SIZE) { 2274 MPASS(pte == pmap_pte(pmap, sva)); 2275 2276 if ((be64toh(*pte) & (PG_MANAGED | PG_V)) != (PG_MANAGED | PG_V)) 2277 goto maybe_invlrng; 2278 else if ((be64toh(*pte) & (PG_M | PG_RW)) == (PG_M | PG_RW)) { 2279 if (advice == MADV_DONTNEED) { 2280 /* 2281 * Future calls to pmap_is_modified() 2282 * can be avoided by making the page 2283 * dirty now. 2284 */ 2285 m = PHYS_TO_VM_PAGE(be64toh(*pte) & PG_FRAME); 2286 vm_page_dirty(m); 2287 } 2288 atomic_clear_long(pte, htobe64(PG_M | PG_A)); 2289 } else if ((be64toh(*pte) & PG_A) != 0) 2290 atomic_clear_long(pte, htobe64(PG_A)); 2291 else 2292 goto maybe_invlrng; 2293 anychanged = TRUE; 2294 continue; 2295 maybe_invlrng: 2296 if (va != va_next) { 2297 anychanged = true; 2298 va = va_next; 2299 } 2300 } 2301 if (va != va_next) 2302 anychanged = true; 2303 } 2304 if (anychanged) 2305 pmap_invalidate_all(pmap); 2306 PMAP_UNLOCK(pmap); 2307 } 2308 2309 /* 2310 * Routines used in machine-dependent code 2311 */ 2312 static void 2313 mmu_radix_bootstrap(vm_offset_t start, vm_offset_t end) 2314 { 2315 uint64_t lpcr; 2316 2317 if (bootverbose) 2318 printf("%s\n", __func__); 2319 hw_direct_map = 1; 2320 mmu_radix_early_bootstrap(start, end); 2321 if (bootverbose) 2322 printf("early bootstrap complete\n"); 2323 if (powernv_enabled) { 2324 lpcr = mfspr(SPR_LPCR); 2325 mtspr(SPR_LPCR, lpcr | LPCR_UPRT | LPCR_HR); 2326 mmu_radix_parttab_init(); 2327 mmu_radix_init_amor(); 2328 if (bootverbose) 2329 printf("powernv init complete\n"); 2330 } 2331 mmu_radix_init_iamr(); 2332 mmu_radix_proctab_init(); 2333 mmu_radix_pid_set(kernel_pmap); 2334 /* XXX assume CPU_FTR_HVMODE */ 2335 mmu_radix_tlbiel_flush(TLB_INVAL_SCOPE_GLOBAL); 2336 2337 mmu_radix_late_bootstrap(start, end); 2338 numa_mem_regions(&numa_pregions, &numa_pregions_sz); 2339 if (bootverbose) 2340 printf("%s done\n", __func__); 2341 pmap_bootstrapped = 1; 2342 dmaplimit = roundup2(powerpc_ptob(Maxmem), L2_PAGE_SIZE); 2343 PCPU_SET(flags, PCPU_GET(flags) | PC_FLAG_NOSRS); 2344 } 2345 2346 static void 2347 mmu_radix_cpu_bootstrap(int ap) 2348 { 2349 uint64_t lpcr; 2350 uint64_t ptcr; 2351 2352 if (powernv_enabled) { 2353 lpcr = mfspr(SPR_LPCR); 2354 mtspr(SPR_LPCR, lpcr | LPCR_UPRT | LPCR_HR); 2355 2356 ptcr = parttab_phys | (PARTTAB_SIZE_SHIFT-12); 2357 mtspr(SPR_PTCR, ptcr); 2358 mmu_radix_init_amor(); 2359 } 2360 mmu_radix_init_iamr(); 2361 mmu_radix_pid_set(kernel_pmap); 2362 mmu_radix_tlbiel_flush(TLB_INVAL_SCOPE_GLOBAL); 2363 } 2364 2365 static SYSCTL_NODE(_vm_pmap, OID_AUTO, l3e, CTLFLAG_RD, 0, 2366 "2MB page mapping counters"); 2367 2368 static u_long pmap_l3e_demotions; 2369 SYSCTL_ULONG(_vm_pmap_l3e, OID_AUTO, demotions, CTLFLAG_RD, 2370 &pmap_l3e_demotions, 0, "2MB page demotions"); 2371 2372 static u_long pmap_l3e_mappings; 2373 SYSCTL_ULONG(_vm_pmap_l3e, OID_AUTO, mappings, CTLFLAG_RD, 2374 &pmap_l3e_mappings, 0, "2MB page mappings"); 2375 2376 static u_long pmap_l3e_p_failures; 2377 SYSCTL_ULONG(_vm_pmap_l3e, OID_AUTO, p_failures, CTLFLAG_RD, 2378 &pmap_l3e_p_failures, 0, "2MB page promotion failures"); 2379 2380 static u_long pmap_l3e_promotions; 2381 SYSCTL_ULONG(_vm_pmap_l3e, OID_AUTO, promotions, CTLFLAG_RD, 2382 &pmap_l3e_promotions, 0, "2MB page promotions"); 2383 2384 static SYSCTL_NODE(_vm_pmap, OID_AUTO, l2e, CTLFLAG_RD, 0, 2385 "1GB page mapping counters"); 2386 2387 static u_long pmap_l2e_demotions; 2388 SYSCTL_ULONG(_vm_pmap_l2e, OID_AUTO, demotions, CTLFLAG_RD, 2389 &pmap_l2e_demotions, 0, "1GB page demotions"); 2390 2391 void 2392 mmu_radix_clear_modify(vm_page_t m) 2393 { 2394 struct md_page *pvh; 2395 pmap_t pmap; 2396 pv_entry_t next_pv, pv; 2397 pml3_entry_t oldl3e, *l3e; 2398 pt_entry_t oldpte, *pte; 2399 struct rwlock *lock; 2400 vm_offset_t va; 2401 int md_gen, pvh_gen; 2402 2403 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 2404 ("pmap_clear_modify: page %p is not managed", m)); 2405 vm_page_assert_busied(m); 2406 CTR2(KTR_PMAP, "%s(%p)", __func__, m); 2407 2408 /* 2409 * If the page is not PGA_WRITEABLE, then no PTEs can have PG_M set. 2410 * If the object containing the page is locked and the page is not 2411 * exclusive busied, then PGA_WRITEABLE cannot be concurrently set. 2412 */ 2413 if ((m->a.flags & PGA_WRITEABLE) == 0) 2414 return; 2415 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : 2416 pa_to_pvh(VM_PAGE_TO_PHYS(m)); 2417 lock = VM_PAGE_TO_PV_LIST_LOCK(m); 2418 rw_wlock(lock); 2419 restart: 2420 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_link, next_pv) { 2421 pmap = PV_PMAP(pv); 2422 if (!PMAP_TRYLOCK(pmap)) { 2423 pvh_gen = pvh->pv_gen; 2424 rw_wunlock(lock); 2425 PMAP_LOCK(pmap); 2426 rw_wlock(lock); 2427 if (pvh_gen != pvh->pv_gen) { 2428 PMAP_UNLOCK(pmap); 2429 goto restart; 2430 } 2431 } 2432 va = pv->pv_va; 2433 l3e = pmap_pml3e(pmap, va); 2434 oldl3e = be64toh(*l3e); 2435 if ((oldl3e & PG_RW) != 0) { 2436 if (pmap_demote_l3e_locked(pmap, l3e, va, &lock)) { 2437 if ((oldl3e & PG_W) == 0) { 2438 /* 2439 * Write protect the mapping to a 2440 * single page so that a subsequent 2441 * write access may repromote. 2442 */ 2443 va += VM_PAGE_TO_PHYS(m) - (oldl3e & 2444 PG_PS_FRAME); 2445 pte = pmap_l3e_to_pte(l3e, va); 2446 oldpte = be64toh(*pte); 2447 if ((oldpte & PG_V) != 0) { 2448 while (!atomic_cmpset_long(pte, 2449 htobe64(oldpte), 2450 htobe64((oldpte | RPTE_EAA_R) & ~(PG_M | PG_RW)))) 2451 oldpte = be64toh(*pte); 2452 vm_page_dirty(m); 2453 pmap_invalidate_page(pmap, va); 2454 } 2455 } 2456 } 2457 } 2458 PMAP_UNLOCK(pmap); 2459 } 2460 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) { 2461 pmap = PV_PMAP(pv); 2462 if (!PMAP_TRYLOCK(pmap)) { 2463 md_gen = m->md.pv_gen; 2464 pvh_gen = pvh->pv_gen; 2465 rw_wunlock(lock); 2466 PMAP_LOCK(pmap); 2467 rw_wlock(lock); 2468 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) { 2469 PMAP_UNLOCK(pmap); 2470 goto restart; 2471 } 2472 } 2473 l3e = pmap_pml3e(pmap, pv->pv_va); 2474 KASSERT((be64toh(*l3e) & RPTE_LEAF) == 0, ("pmap_clear_modify: found" 2475 " a 2mpage in page %p's pv list", m)); 2476 pte = pmap_l3e_to_pte(l3e, pv->pv_va); 2477 if ((be64toh(*pte) & (PG_M | PG_RW)) == (PG_M | PG_RW)) { 2478 atomic_clear_long(pte, htobe64(PG_M)); 2479 pmap_invalidate_page(pmap, pv->pv_va); 2480 } 2481 PMAP_UNLOCK(pmap); 2482 } 2483 rw_wunlock(lock); 2484 } 2485 2486 void 2487 mmu_radix_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, 2488 vm_size_t len, vm_offset_t src_addr) 2489 { 2490 struct rwlock *lock; 2491 struct spglist free; 2492 vm_offset_t addr; 2493 vm_offset_t end_addr = src_addr + len; 2494 vm_offset_t va_next; 2495 vm_page_t dst_pdpg, dstmpte, srcmpte; 2496 bool invalidate_all; 2497 2498 CTR6(KTR_PMAP, 2499 "%s(dst_pmap=%p, src_pmap=%p, dst_addr=%lx, len=%lu, src_addr=%lx)\n", 2500 __func__, dst_pmap, src_pmap, dst_addr, len, src_addr); 2501 2502 if (dst_addr != src_addr) 2503 return; 2504 lock = NULL; 2505 invalidate_all = false; 2506 if (dst_pmap < src_pmap) { 2507 PMAP_LOCK(dst_pmap); 2508 PMAP_LOCK(src_pmap); 2509 } else { 2510 PMAP_LOCK(src_pmap); 2511 PMAP_LOCK(dst_pmap); 2512 } 2513 2514 for (addr = src_addr; addr < end_addr; addr = va_next) { 2515 pml1_entry_t *l1e; 2516 pml2_entry_t *l2e; 2517 pml3_entry_t srcptepaddr, *l3e; 2518 pt_entry_t *src_pte, *dst_pte; 2519 2520 l1e = pmap_pml1e(src_pmap, addr); 2521 if ((be64toh(*l1e) & PG_V) == 0) { 2522 va_next = (addr + L1_PAGE_SIZE) & ~L1_PAGE_MASK; 2523 if (va_next < addr) 2524 va_next = end_addr; 2525 continue; 2526 } 2527 2528 l2e = pmap_l1e_to_l2e(l1e, addr); 2529 if ((be64toh(*l2e) & PG_V) == 0) { 2530 va_next = (addr + L2_PAGE_SIZE) & ~L2_PAGE_MASK; 2531 if (va_next < addr) 2532 va_next = end_addr; 2533 continue; 2534 } 2535 2536 va_next = (addr + L3_PAGE_SIZE) & ~L3_PAGE_MASK; 2537 if (va_next < addr) 2538 va_next = end_addr; 2539 2540 l3e = pmap_l2e_to_l3e(l2e, addr); 2541 srcptepaddr = be64toh(*l3e); 2542 if (srcptepaddr == 0) 2543 continue; 2544 2545 if (srcptepaddr & RPTE_LEAF) { 2546 if ((addr & L3_PAGE_MASK) != 0 || 2547 addr + L3_PAGE_SIZE > end_addr) 2548 continue; 2549 dst_pdpg = pmap_allocl3e(dst_pmap, addr, NULL); 2550 if (dst_pdpg == NULL) 2551 break; 2552 l3e = (pml3_entry_t *) 2553 PHYS_TO_DMAP(VM_PAGE_TO_PHYS(dst_pdpg)); 2554 l3e = &l3e[pmap_pml3e_index(addr)]; 2555 if (be64toh(*l3e) == 0 && ((srcptepaddr & PG_MANAGED) == 0 || 2556 pmap_pv_insert_l3e(dst_pmap, addr, srcptepaddr, 2557 PMAP_ENTER_NORECLAIM, &lock))) { 2558 *l3e = htobe64(srcptepaddr & ~PG_W); 2559 pmap_resident_count_inc(dst_pmap, 2560 L3_PAGE_SIZE / PAGE_SIZE); 2561 atomic_add_long(&pmap_l3e_mappings, 1); 2562 } else 2563 dst_pdpg->ref_count--; 2564 continue; 2565 } 2566 2567 srcptepaddr &= PG_FRAME; 2568 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr); 2569 KASSERT(srcmpte->ref_count > 0, 2570 ("pmap_copy: source page table page is unused")); 2571 2572 if (va_next > end_addr) 2573 va_next = end_addr; 2574 2575 src_pte = (pt_entry_t *)PHYS_TO_DMAP(srcptepaddr); 2576 src_pte = &src_pte[pmap_pte_index(addr)]; 2577 dstmpte = NULL; 2578 while (addr < va_next) { 2579 pt_entry_t ptetemp; 2580 ptetemp = be64toh(*src_pte); 2581 /* 2582 * we only virtual copy managed pages 2583 */ 2584 if ((ptetemp & PG_MANAGED) != 0) { 2585 if (dstmpte != NULL && 2586 dstmpte->pindex == pmap_l3e_pindex(addr)) 2587 dstmpte->ref_count++; 2588 else if ((dstmpte = pmap_allocpte(dst_pmap, 2589 addr, NULL)) == NULL) 2590 goto out; 2591 dst_pte = (pt_entry_t *) 2592 PHYS_TO_DMAP(VM_PAGE_TO_PHYS(dstmpte)); 2593 dst_pte = &dst_pte[pmap_pte_index(addr)]; 2594 if (be64toh(*dst_pte) == 0 && 2595 pmap_try_insert_pv_entry(dst_pmap, addr, 2596 PHYS_TO_VM_PAGE(ptetemp & PG_FRAME), 2597 &lock)) { 2598 /* 2599 * Clear the wired, modified, and 2600 * accessed (referenced) bits 2601 * during the copy. 2602 */ 2603 *dst_pte = htobe64(ptetemp & ~(PG_W | PG_M | 2604 PG_A)); 2605 pmap_resident_count_inc(dst_pmap, 1); 2606 } else { 2607 SLIST_INIT(&free); 2608 if (pmap_unwire_ptp(dst_pmap, addr, 2609 dstmpte, &free)) { 2610 /* 2611 * Although "addr" is not 2612 * mapped, paging-structure 2613 * caches could nonetheless 2614 * have entries that refer to 2615 * the freed page table pages. 2616 * Invalidate those entries. 2617 */ 2618 invalidate_all = true; 2619 vm_page_free_pages_toq(&free, 2620 true); 2621 } 2622 goto out; 2623 } 2624 if (dstmpte->ref_count >= srcmpte->ref_count) 2625 break; 2626 } 2627 addr += PAGE_SIZE; 2628 if (__predict_false((addr & L3_PAGE_MASK) == 0)) 2629 src_pte = pmap_pte(src_pmap, addr); 2630 else 2631 src_pte++; 2632 } 2633 } 2634 out: 2635 if (invalidate_all) 2636 pmap_invalidate_all(dst_pmap); 2637 if (lock != NULL) 2638 rw_wunlock(lock); 2639 PMAP_UNLOCK(src_pmap); 2640 PMAP_UNLOCK(dst_pmap); 2641 } 2642 2643 static void 2644 mmu_radix_copy_page(vm_page_t msrc, vm_page_t mdst) 2645 { 2646 vm_offset_t src = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(msrc)); 2647 vm_offset_t dst = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mdst)); 2648 2649 CTR3(KTR_PMAP, "%s(%p, %p)", __func__, src, dst); 2650 /* 2651 * XXX slow 2652 */ 2653 bcopy((void *)src, (void *)dst, PAGE_SIZE); 2654 } 2655 2656 static void 2657 mmu_radix_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[], 2658 vm_offset_t b_offset, int xfersize) 2659 { 2660 void *a_cp, *b_cp; 2661 vm_offset_t a_pg_offset, b_pg_offset; 2662 int cnt; 2663 2664 CTR6(KTR_PMAP, "%s(%p, %#x, %p, %#x, %#x)", __func__, ma, 2665 a_offset, mb, b_offset, xfersize); 2666 2667 while (xfersize > 0) { 2668 a_pg_offset = a_offset & PAGE_MASK; 2669 cnt = min(xfersize, PAGE_SIZE - a_pg_offset); 2670 a_cp = (char *)(uintptr_t)PHYS_TO_DMAP( 2671 VM_PAGE_TO_PHYS(ma[a_offset >> PAGE_SHIFT])) + 2672 a_pg_offset; 2673 b_pg_offset = b_offset & PAGE_MASK; 2674 cnt = min(cnt, PAGE_SIZE - b_pg_offset); 2675 b_cp = (char *)(uintptr_t)PHYS_TO_DMAP( 2676 VM_PAGE_TO_PHYS(mb[b_offset >> PAGE_SHIFT])) + 2677 b_pg_offset; 2678 bcopy(a_cp, b_cp, cnt); 2679 a_offset += cnt; 2680 b_offset += cnt; 2681 xfersize -= cnt; 2682 } 2683 } 2684 2685 #if VM_NRESERVLEVEL > 0 2686 /* 2687 * Tries to promote the 512, contiguous 4KB page mappings that are within a 2688 * single page table page (PTP) to a single 2MB page mapping. For promotion 2689 * to occur, two conditions must be met: (1) the 4KB page mappings must map 2690 * aligned, contiguous physical memory and (2) the 4KB page mappings must have 2691 * identical characteristics. 2692 */ 2693 static int 2694 pmap_promote_l3e(pmap_t pmap, pml3_entry_t *pde, vm_offset_t va, 2695 struct rwlock **lockp) 2696 { 2697 pml3_entry_t newpde; 2698 pt_entry_t *firstpte, oldpte, pa, *pte; 2699 vm_page_t mpte; 2700 2701 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 2702 2703 /* 2704 * Examine the first PTE in the specified PTP. Abort if this PTE is 2705 * either invalid, unused, or does not map the first 4KB physical page 2706 * within a 2MB page. 2707 */ 2708 firstpte = (pt_entry_t *)PHYS_TO_DMAP(be64toh(*pde) & PG_FRAME); 2709 setpde: 2710 newpde = *firstpte; 2711 if ((newpde & ((PG_FRAME & L3_PAGE_MASK) | PG_A | PG_V)) != (PG_A | PG_V)) { 2712 CTR2(KTR_PMAP, "pmap_promote_l3e: failure for va %#lx" 2713 " in pmap %p", va, pmap); 2714 goto fail; 2715 } 2716 if ((newpde & (PG_M | PG_RW)) == PG_RW) { 2717 /* 2718 * When PG_M is already clear, PG_RW can be cleared without 2719 * a TLB invalidation. 2720 */ 2721 if (!atomic_cmpset_long(firstpte, htobe64(newpde), htobe64((newpde | RPTE_EAA_R) & ~RPTE_EAA_W))) 2722 goto setpde; 2723 newpde &= ~RPTE_EAA_W; 2724 } 2725 2726 /* 2727 * Examine each of the other PTEs in the specified PTP. Abort if this 2728 * PTE maps an unexpected 4KB physical page or does not have identical 2729 * characteristics to the first PTE. 2730 */ 2731 pa = (newpde & (PG_PS_FRAME | PG_A | PG_V)) + L3_PAGE_SIZE - PAGE_SIZE; 2732 for (pte = firstpte + NPTEPG - 1; pte > firstpte; pte--) { 2733 setpte: 2734 oldpte = be64toh(*pte); 2735 if ((oldpte & (PG_FRAME | PG_A | PG_V)) != pa) { 2736 CTR2(KTR_PMAP, "pmap_promote_l3e: failure for va %#lx" 2737 " in pmap %p", va, pmap); 2738 goto fail; 2739 } 2740 if ((oldpte & (PG_M | PG_RW)) == PG_RW) { 2741 /* 2742 * When PG_M is already clear, PG_RW can be cleared 2743 * without a TLB invalidation. 2744 */ 2745 if (!atomic_cmpset_long(pte, htobe64(oldpte), htobe64((oldpte | RPTE_EAA_R) & ~RPTE_EAA_W))) 2746 goto setpte; 2747 oldpte &= ~RPTE_EAA_W; 2748 CTR2(KTR_PMAP, "pmap_promote_l3e: protect for va %#lx" 2749 " in pmap %p", (oldpte & PG_FRAME & L3_PAGE_MASK) | 2750 (va & ~L3_PAGE_MASK), pmap); 2751 } 2752 if ((oldpte & PG_PTE_PROMOTE) != (newpde & PG_PTE_PROMOTE)) { 2753 CTR2(KTR_PMAP, "pmap_promote_l3e: failure for va %#lx" 2754 " in pmap %p", va, pmap); 2755 goto fail; 2756 } 2757 pa -= PAGE_SIZE; 2758 } 2759 2760 /* 2761 * Save the page table page in its current state until the PDE 2762 * mapping the superpage is demoted by pmap_demote_pde() or 2763 * destroyed by pmap_remove_pde(). 2764 */ 2765 mpte = PHYS_TO_VM_PAGE(be64toh(*pde) & PG_FRAME); 2766 KASSERT(mpte >= vm_page_array && 2767 mpte < &vm_page_array[vm_page_array_size], 2768 ("pmap_promote_l3e: page table page is out of range")); 2769 KASSERT(mpte->pindex == pmap_l3e_pindex(va), 2770 ("pmap_promote_l3e: page table page's pindex is wrong")); 2771 if (pmap_insert_pt_page(pmap, mpte)) { 2772 CTR2(KTR_PMAP, 2773 "pmap_promote_l3e: failure for va %#lx in pmap %p", va, 2774 pmap); 2775 goto fail; 2776 } 2777 2778 /* 2779 * Promote the pv entries. 2780 */ 2781 if ((newpde & PG_MANAGED) != 0) 2782 pmap_pv_promote_l3e(pmap, va, newpde & PG_PS_FRAME, lockp); 2783 2784 pte_store(pde, PG_PROMOTED | newpde); 2785 ptesync(); 2786 atomic_add_long(&pmap_l3e_promotions, 1); 2787 CTR2(KTR_PMAP, "pmap_promote_l3e: success for va %#lx" 2788 " in pmap %p", va, pmap); 2789 return (0); 2790 fail: 2791 atomic_add_long(&pmap_l3e_p_failures, 1); 2792 return (KERN_FAILURE); 2793 } 2794 #endif /* VM_NRESERVLEVEL > 0 */ 2795 2796 int 2797 mmu_radix_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, 2798 vm_prot_t prot, u_int flags, int8_t psind) 2799 { 2800 struct rwlock *lock; 2801 pml3_entry_t *l3e; 2802 pt_entry_t *pte; 2803 pt_entry_t newpte, origpte; 2804 pv_entry_t pv; 2805 vm_paddr_t opa, pa; 2806 vm_page_t mpte, om; 2807 int rv, retrycount; 2808 boolean_t nosleep, invalidate_all, invalidate_page; 2809 2810 va = trunc_page(va); 2811 retrycount = 0; 2812 invalidate_page = invalidate_all = false; 2813 CTR6(KTR_PMAP, "pmap_enter(%p, %#lx, %p, %#x, %#x, %d)", pmap, va, 2814 m, prot, flags, psind); 2815 KASSERT(va <= VM_MAX_KERNEL_ADDRESS, ("pmap_enter: toobig")); 2816 KASSERT((m->oflags & VPO_UNMANAGED) != 0 || !VA_IS_CLEANMAP(va), 2817 ("pmap_enter: managed mapping within the clean submap")); 2818 if ((m->oflags & VPO_UNMANAGED) == 0) 2819 VM_PAGE_OBJECT_BUSY_ASSERT(m); 2820 2821 KASSERT((flags & PMAP_ENTER_RESERVED) == 0, 2822 ("pmap_enter: flags %u has reserved bits set", flags)); 2823 pa = VM_PAGE_TO_PHYS(m); 2824 newpte = (pt_entry_t)(pa | PG_A | PG_V | RPTE_LEAF); 2825 if ((flags & VM_PROT_WRITE) != 0) 2826 newpte |= PG_M; 2827 if ((flags & VM_PROT_READ) != 0) 2828 newpte |= PG_A; 2829 if (prot & VM_PROT_READ) 2830 newpte |= RPTE_EAA_R; 2831 if ((prot & VM_PROT_WRITE) != 0) 2832 newpte |= RPTE_EAA_W; 2833 KASSERT((newpte & (PG_M | PG_RW)) != PG_M, 2834 ("pmap_enter: flags includes VM_PROT_WRITE but prot doesn't")); 2835 2836 if (prot & VM_PROT_EXECUTE) 2837 newpte |= PG_X; 2838 if ((flags & PMAP_ENTER_WIRED) != 0) 2839 newpte |= PG_W; 2840 if (va >= DMAP_MIN_ADDRESS) 2841 newpte |= RPTE_EAA_P; 2842 newpte |= pmap_cache_bits(m->md.mdpg_cache_attrs); 2843 /* 2844 * Set modified bit gratuitously for writeable mappings if 2845 * the page is unmanaged. We do not want to take a fault 2846 * to do the dirty bit accounting for these mappings. 2847 */ 2848 if ((m->oflags & VPO_UNMANAGED) != 0) { 2849 if ((newpte & PG_RW) != 0) 2850 newpte |= PG_M; 2851 } else 2852 newpte |= PG_MANAGED; 2853 2854 lock = NULL; 2855 PMAP_LOCK(pmap); 2856 if (psind == 1) { 2857 /* Assert the required virtual and physical alignment. */ 2858 KASSERT((va & L3_PAGE_MASK) == 0, ("pmap_enter: va unaligned")); 2859 KASSERT(m->psind > 0, ("pmap_enter: m->psind < psind")); 2860 rv = pmap_enter_l3e(pmap, va, newpte | RPTE_LEAF, flags, m, &lock); 2861 goto out; 2862 } 2863 mpte = NULL; 2864 2865 /* 2866 * In the case that a page table page is not 2867 * resident, we are creating it here. 2868 */ 2869 retry: 2870 l3e = pmap_pml3e(pmap, va); 2871 if (l3e != NULL && (be64toh(*l3e) & PG_V) != 0 && ((be64toh(*l3e) & RPTE_LEAF) == 0 || 2872 pmap_demote_l3e_locked(pmap, l3e, va, &lock))) { 2873 pte = pmap_l3e_to_pte(l3e, va); 2874 if (va < VM_MAXUSER_ADDRESS && mpte == NULL) { 2875 mpte = PHYS_TO_VM_PAGE(be64toh(*l3e) & PG_FRAME); 2876 mpte->ref_count++; 2877 } 2878 } else if (va < VM_MAXUSER_ADDRESS) { 2879 /* 2880 * Here if the pte page isn't mapped, or if it has been 2881 * deallocated. 2882 */ 2883 nosleep = (flags & PMAP_ENTER_NOSLEEP) != 0; 2884 mpte = _pmap_allocpte(pmap, pmap_l3e_pindex(va), 2885 nosleep ? NULL : &lock); 2886 if (mpte == NULL && nosleep) { 2887 rv = KERN_RESOURCE_SHORTAGE; 2888 goto out; 2889 } 2890 if (__predict_false(retrycount++ == 6)) 2891 panic("too many retries"); 2892 invalidate_all = true; 2893 goto retry; 2894 } else 2895 panic("pmap_enter: invalid page directory va=%#lx", va); 2896 2897 origpte = be64toh(*pte); 2898 pv = NULL; 2899 2900 /* 2901 * Is the specified virtual address already mapped? 2902 */ 2903 if ((origpte & PG_V) != 0) { 2904 #ifdef INVARIANTS 2905 if (VERBOSE_PMAP || pmap_logging) { 2906 printf("cow fault pmap_enter(%p, %#lx, %p, %#x, %x, %d) --" 2907 " asid=%lu curpid=%d name=%s origpte0x%lx\n", 2908 pmap, va, m, prot, flags, psind, pmap->pm_pid, 2909 curproc->p_pid, curproc->p_comm, origpte); 2910 pmap_pte_walk(pmap->pm_pml1, va); 2911 } 2912 #endif 2913 /* 2914 * Wiring change, just update stats. We don't worry about 2915 * wiring PT pages as they remain resident as long as there 2916 * are valid mappings in them. Hence, if a user page is wired, 2917 * the PT page will be also. 2918 */ 2919 if ((newpte & PG_W) != 0 && (origpte & PG_W) == 0) 2920 pmap->pm_stats.wired_count++; 2921 else if ((newpte & PG_W) == 0 && (origpte & PG_W) != 0) 2922 pmap->pm_stats.wired_count--; 2923 2924 /* 2925 * Remove the extra PT page reference. 2926 */ 2927 if (mpte != NULL) { 2928 mpte->ref_count--; 2929 KASSERT(mpte->ref_count > 0, 2930 ("pmap_enter: missing reference to page table page," 2931 " va: 0x%lx", va)); 2932 } 2933 2934 /* 2935 * Has the physical page changed? 2936 */ 2937 opa = origpte & PG_FRAME; 2938 if (opa == pa) { 2939 /* 2940 * No, might be a protection or wiring change. 2941 */ 2942 if ((origpte & PG_MANAGED) != 0 && 2943 (newpte & PG_RW) != 0) 2944 vm_page_aflag_set(m, PGA_WRITEABLE); 2945 if (((origpte ^ newpte) & ~(PG_M | PG_A)) == 0) { 2946 if ((newpte & (PG_A|PG_M)) != (origpte & (PG_A|PG_M))) { 2947 if (!atomic_cmpset_long(pte, htobe64(origpte), htobe64(newpte))) 2948 goto retry; 2949 if ((newpte & PG_M) != (origpte & PG_M)) 2950 vm_page_dirty(m); 2951 if ((newpte & PG_A) != (origpte & PG_A)) 2952 vm_page_aflag_set(m, PGA_REFERENCED); 2953 ptesync(); 2954 } else 2955 invalidate_all = true; 2956 if (((origpte ^ newpte) & ~(PG_M | PG_A)) == 0) 2957 goto unchanged; 2958 } 2959 goto validate; 2960 } 2961 2962 /* 2963 * The physical page has changed. Temporarily invalidate 2964 * the mapping. This ensures that all threads sharing the 2965 * pmap keep a consistent view of the mapping, which is 2966 * necessary for the correct handling of COW faults. It 2967 * also permits reuse of the old mapping's PV entry, 2968 * avoiding an allocation. 2969 * 2970 * For consistency, handle unmanaged mappings the same way. 2971 */ 2972 origpte = be64toh(pte_load_clear(pte)); 2973 KASSERT((origpte & PG_FRAME) == opa, 2974 ("pmap_enter: unexpected pa update for %#lx", va)); 2975 if ((origpte & PG_MANAGED) != 0) { 2976 om = PHYS_TO_VM_PAGE(opa); 2977 2978 /* 2979 * The pmap lock is sufficient to synchronize with 2980 * concurrent calls to pmap_page_test_mappings() and 2981 * pmap_ts_referenced(). 2982 */ 2983 if ((origpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) 2984 vm_page_dirty(om); 2985 if ((origpte & PG_A) != 0) 2986 vm_page_aflag_set(om, PGA_REFERENCED); 2987 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, opa); 2988 pv = pmap_pvh_remove(&om->md, pmap, va); 2989 if ((newpte & PG_MANAGED) == 0) 2990 free_pv_entry(pmap, pv); 2991 #ifdef INVARIANTS 2992 else if (origpte & PG_MANAGED) { 2993 if (pv == NULL) { 2994 pmap_page_print_mappings(om); 2995 MPASS(pv != NULL); 2996 } 2997 } 2998 #endif 2999 if ((om->a.flags & PGA_WRITEABLE) != 0 && 3000 TAILQ_EMPTY(&om->md.pv_list) && 3001 ((om->flags & PG_FICTITIOUS) != 0 || 3002 TAILQ_EMPTY(&pa_to_pvh(opa)->pv_list))) 3003 vm_page_aflag_clear(om, PGA_WRITEABLE); 3004 } 3005 if ((origpte & PG_A) != 0) 3006 invalidate_page = true; 3007 origpte = 0; 3008 } else { 3009 if (pmap != kernel_pmap) { 3010 #ifdef INVARIANTS 3011 if (VERBOSE_PMAP || pmap_logging) 3012 printf("pmap_enter(%p, %#lx, %p, %#x, %x, %d) -- asid=%lu curpid=%d name=%s\n", 3013 pmap, va, m, prot, flags, psind, 3014 pmap->pm_pid, curproc->p_pid, 3015 curproc->p_comm); 3016 #endif 3017 } 3018 3019 /* 3020 * Increment the counters. 3021 */ 3022 if ((newpte & PG_W) != 0) 3023 pmap->pm_stats.wired_count++; 3024 pmap_resident_count_inc(pmap, 1); 3025 } 3026 3027 /* 3028 * Enter on the PV list if part of our managed memory. 3029 */ 3030 if ((newpte & PG_MANAGED) != 0) { 3031 if (pv == NULL) { 3032 pv = get_pv_entry(pmap, &lock); 3033 pv->pv_va = va; 3034 } 3035 #ifdef VERBOSE_PV 3036 else 3037 printf("reassigning pv: %p to pmap: %p\n", 3038 pv, pmap); 3039 #endif 3040 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, pa); 3041 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_link); 3042 m->md.pv_gen++; 3043 if ((newpte & PG_RW) != 0) 3044 vm_page_aflag_set(m, PGA_WRITEABLE); 3045 } 3046 3047 /* 3048 * Update the PTE. 3049 */ 3050 if ((origpte & PG_V) != 0) { 3051 validate: 3052 origpte = be64toh(pte_load_store(pte, htobe64(newpte))); 3053 KASSERT((origpte & PG_FRAME) == pa, 3054 ("pmap_enter: unexpected pa update for %#lx", va)); 3055 if ((newpte & PG_M) == 0 && (origpte & (PG_M | PG_RW)) == 3056 (PG_M | PG_RW)) { 3057 if ((origpte & PG_MANAGED) != 0) 3058 vm_page_dirty(m); 3059 invalidate_page = true; 3060 3061 /* 3062 * Although the PTE may still have PG_RW set, TLB 3063 * invalidation may nonetheless be required because 3064 * the PTE no longer has PG_M set. 3065 */ 3066 } else if ((origpte & PG_X) != 0 || (newpte & PG_X) == 0) { 3067 /* 3068 * Removing capabilities requires invalidation on POWER 3069 */ 3070 invalidate_page = true; 3071 goto unchanged; 3072 } 3073 if ((origpte & PG_A) != 0) 3074 invalidate_page = true; 3075 } else { 3076 pte_store(pte, newpte); 3077 ptesync(); 3078 } 3079 unchanged: 3080 3081 #if VM_NRESERVLEVEL > 0 3082 /* 3083 * If both the page table page and the reservation are fully 3084 * populated, then attempt promotion. 3085 */ 3086 if ((mpte == NULL || mpte->ref_count == NPTEPG) && 3087 mmu_radix_ps_enabled(pmap) && 3088 (m->flags & PG_FICTITIOUS) == 0 && 3089 vm_reserv_level_iffullpop(m) == 0 && 3090 pmap_promote_l3e(pmap, l3e, va, &lock) == 0) 3091 invalidate_all = true; 3092 #endif 3093 if (invalidate_all) 3094 pmap_invalidate_all(pmap); 3095 else if (invalidate_page) 3096 pmap_invalidate_page(pmap, va); 3097 3098 rv = KERN_SUCCESS; 3099 out: 3100 if (lock != NULL) 3101 rw_wunlock(lock); 3102 PMAP_UNLOCK(pmap); 3103 3104 return (rv); 3105 } 3106 3107 /* 3108 * Tries to create a read- and/or execute-only 2MB page mapping. Returns true 3109 * if successful. Returns false if (1) a page table page cannot be allocated 3110 * without sleeping, (2) a mapping already exists at the specified virtual 3111 * address, or (3) a PV entry cannot be allocated without reclaiming another 3112 * PV entry. 3113 */ 3114 static bool 3115 pmap_enter_2mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot, 3116 struct rwlock **lockp) 3117 { 3118 pml3_entry_t newpde; 3119 3120 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 3121 newpde = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(m->md.mdpg_cache_attrs) | 3122 RPTE_LEAF | PG_V; 3123 if ((m->oflags & VPO_UNMANAGED) == 0) 3124 newpde |= PG_MANAGED; 3125 if (prot & VM_PROT_EXECUTE) 3126 newpde |= PG_X; 3127 if (prot & VM_PROT_READ) 3128 newpde |= RPTE_EAA_R; 3129 if (va >= DMAP_MIN_ADDRESS) 3130 newpde |= RPTE_EAA_P; 3131 return (pmap_enter_l3e(pmap, va, newpde, PMAP_ENTER_NOSLEEP | 3132 PMAP_ENTER_NOREPLACE | PMAP_ENTER_NORECLAIM, NULL, lockp) == 3133 KERN_SUCCESS); 3134 } 3135 3136 /* 3137 * Tries to create the specified 2MB page mapping. Returns KERN_SUCCESS if 3138 * the mapping was created, and either KERN_FAILURE or KERN_RESOURCE_SHORTAGE 3139 * otherwise. Returns KERN_FAILURE if PMAP_ENTER_NOREPLACE was specified and 3140 * a mapping already exists at the specified virtual address. Returns 3141 * KERN_RESOURCE_SHORTAGE if PMAP_ENTER_NOSLEEP was specified and a page table 3142 * page allocation failed. Returns KERN_RESOURCE_SHORTAGE if 3143 * PMAP_ENTER_NORECLAIM was specified and a PV entry allocation failed. 3144 * 3145 * The parameter "m" is only used when creating a managed, writeable mapping. 3146 */ 3147 static int 3148 pmap_enter_l3e(pmap_t pmap, vm_offset_t va, pml3_entry_t newpde, u_int flags, 3149 vm_page_t m, struct rwlock **lockp) 3150 { 3151 struct spglist free; 3152 pml3_entry_t oldl3e, *l3e; 3153 vm_page_t mt, pdpg; 3154 3155 KASSERT((newpde & (PG_M | PG_RW)) != PG_RW, 3156 ("pmap_enter_pde: newpde is missing PG_M")); 3157 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 3158 3159 if ((pdpg = pmap_allocl3e(pmap, va, (flags & PMAP_ENTER_NOSLEEP) != 0 ? 3160 NULL : lockp)) == NULL) { 3161 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx" 3162 " in pmap %p", va, pmap); 3163 return (KERN_RESOURCE_SHORTAGE); 3164 } 3165 l3e = (pml3_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pdpg)); 3166 l3e = &l3e[pmap_pml3e_index(va)]; 3167 oldl3e = be64toh(*l3e); 3168 if ((oldl3e & PG_V) != 0) { 3169 KASSERT(pdpg->ref_count > 1, 3170 ("pmap_enter_pde: pdpg's wire count is too low")); 3171 if ((flags & PMAP_ENTER_NOREPLACE) != 0) { 3172 pdpg->ref_count--; 3173 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx" 3174 " in pmap %p", va, pmap); 3175 return (KERN_FAILURE); 3176 } 3177 /* Break the existing mapping(s). */ 3178 SLIST_INIT(&free); 3179 if ((oldl3e & RPTE_LEAF) != 0) { 3180 /* 3181 * The reference to the PD page that was acquired by 3182 * pmap_allocl3e() ensures that it won't be freed. 3183 * However, if the PDE resulted from a promotion, then 3184 * a reserved PT page could be freed. 3185 */ 3186 (void)pmap_remove_l3e(pmap, l3e, va, &free, lockp); 3187 } else { 3188 if (pmap_remove_ptes(pmap, va, va + L3_PAGE_SIZE, l3e, 3189 &free, lockp)) 3190 pmap_invalidate_all(pmap); 3191 } 3192 vm_page_free_pages_toq(&free, true); 3193 if (va >= VM_MAXUSER_ADDRESS) { 3194 mt = PHYS_TO_VM_PAGE(be64toh(*l3e) & PG_FRAME); 3195 if (pmap_insert_pt_page(pmap, mt)) { 3196 /* 3197 * XXX Currently, this can't happen because 3198 * we do not perform pmap_enter(psind == 1) 3199 * on the kernel pmap. 3200 */ 3201 panic("pmap_enter_pde: trie insert failed"); 3202 } 3203 } else 3204 KASSERT(be64toh(*l3e) == 0, ("pmap_enter_pde: non-zero pde %p", 3205 l3e)); 3206 } 3207 if ((newpde & PG_MANAGED) != 0) { 3208 /* 3209 * Abort this mapping if its PV entry could not be created. 3210 */ 3211 if (!pmap_pv_insert_l3e(pmap, va, newpde, flags, lockp)) { 3212 SLIST_INIT(&free); 3213 if (pmap_unwire_ptp(pmap, va, pdpg, &free)) { 3214 /* 3215 * Although "va" is not mapped, paging- 3216 * structure caches could nonetheless have 3217 * entries that refer to the freed page table 3218 * pages. Invalidate those entries. 3219 */ 3220 pmap_invalidate_page(pmap, va); 3221 vm_page_free_pages_toq(&free, true); 3222 } 3223 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx" 3224 " in pmap %p", va, pmap); 3225 return (KERN_RESOURCE_SHORTAGE); 3226 } 3227 if ((newpde & PG_RW) != 0) { 3228 for (mt = m; mt < &m[L3_PAGE_SIZE / PAGE_SIZE]; mt++) 3229 vm_page_aflag_set(mt, PGA_WRITEABLE); 3230 } 3231 } 3232 3233 /* 3234 * Increment counters. 3235 */ 3236 if ((newpde & PG_W) != 0) 3237 pmap->pm_stats.wired_count += L3_PAGE_SIZE / PAGE_SIZE; 3238 pmap_resident_count_inc(pmap, L3_PAGE_SIZE / PAGE_SIZE); 3239 3240 /* 3241 * Map the superpage. (This is not a promoted mapping; there will not 3242 * be any lingering 4KB page mappings in the TLB.) 3243 */ 3244 pte_store(l3e, newpde); 3245 3246 atomic_add_long(&pmap_l3e_mappings, 1); 3247 CTR2(KTR_PMAP, "pmap_enter_pde: success for va %#lx" 3248 " in pmap %p", va, pmap); 3249 return (KERN_SUCCESS); 3250 } 3251 3252 void 3253 mmu_radix_enter_object(pmap_t pmap, vm_offset_t start, 3254 vm_offset_t end, vm_page_t m_start, vm_prot_t prot) 3255 { 3256 3257 struct rwlock *lock; 3258 vm_offset_t va; 3259 vm_page_t m, mpte; 3260 vm_pindex_t diff, psize; 3261 bool invalidate; 3262 VM_OBJECT_ASSERT_LOCKED(m_start->object); 3263 3264 CTR6(KTR_PMAP, "%s(%p, %#x, %#x, %p, %#x)", __func__, pmap, start, 3265 end, m_start, prot); 3266 3267 invalidate = false; 3268 psize = atop(end - start); 3269 mpte = NULL; 3270 m = m_start; 3271 lock = NULL; 3272 PMAP_LOCK(pmap); 3273 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) { 3274 va = start + ptoa(diff); 3275 if ((va & L3_PAGE_MASK) == 0 && va + L3_PAGE_SIZE <= end && 3276 m->psind == 1 && mmu_radix_ps_enabled(pmap) && 3277 pmap_enter_2mpage(pmap, va, m, prot, &lock)) 3278 m = &m[L3_PAGE_SIZE / PAGE_SIZE - 1]; 3279 else 3280 mpte = mmu_radix_enter_quick_locked(pmap, va, m, prot, 3281 mpte, &lock, &invalidate); 3282 m = TAILQ_NEXT(m, listq); 3283 } 3284 ptesync(); 3285 if (lock != NULL) 3286 rw_wunlock(lock); 3287 if (invalidate) 3288 pmap_invalidate_all(pmap); 3289 PMAP_UNLOCK(pmap); 3290 } 3291 3292 static vm_page_t 3293 mmu_radix_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, 3294 vm_prot_t prot, vm_page_t mpte, struct rwlock **lockp, bool *invalidate) 3295 { 3296 struct spglist free; 3297 pt_entry_t *pte; 3298 vm_paddr_t pa; 3299 3300 KASSERT(!VA_IS_CLEANMAP(va) || 3301 (m->oflags & VPO_UNMANAGED) != 0, 3302 ("mmu_radix_enter_quick_locked: managed mapping within the clean submap")); 3303 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 3304 3305 /* 3306 * In the case that a page table page is not 3307 * resident, we are creating it here. 3308 */ 3309 if (va < VM_MAXUSER_ADDRESS) { 3310 vm_pindex_t ptepindex; 3311 pml3_entry_t *ptepa; 3312 3313 /* 3314 * Calculate pagetable page index 3315 */ 3316 ptepindex = pmap_l3e_pindex(va); 3317 if (mpte && (mpte->pindex == ptepindex)) { 3318 mpte->ref_count++; 3319 } else { 3320 /* 3321 * Get the page directory entry 3322 */ 3323 ptepa = pmap_pml3e(pmap, va); 3324 3325 /* 3326 * If the page table page is mapped, we just increment 3327 * the hold count, and activate it. Otherwise, we 3328 * attempt to allocate a page table page. If this 3329 * attempt fails, we don't retry. Instead, we give up. 3330 */ 3331 if (ptepa && (be64toh(*ptepa) & PG_V) != 0) { 3332 if (be64toh(*ptepa) & RPTE_LEAF) 3333 return (NULL); 3334 mpte = PHYS_TO_VM_PAGE(be64toh(*ptepa) & PG_FRAME); 3335 mpte->ref_count++; 3336 } else { 3337 /* 3338 * Pass NULL instead of the PV list lock 3339 * pointer, because we don't intend to sleep. 3340 */ 3341 mpte = _pmap_allocpte(pmap, ptepindex, NULL); 3342 if (mpte == NULL) 3343 return (mpte); 3344 } 3345 } 3346 pte = (pt_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mpte)); 3347 pte = &pte[pmap_pte_index(va)]; 3348 } else { 3349 mpte = NULL; 3350 pte = pmap_pte(pmap, va); 3351 } 3352 if (be64toh(*pte)) { 3353 if (mpte != NULL) { 3354 mpte->ref_count--; 3355 mpte = NULL; 3356 } 3357 return (mpte); 3358 } 3359 3360 /* 3361 * Enter on the PV list if part of our managed memory. 3362 */ 3363 if ((m->oflags & VPO_UNMANAGED) == 0 && 3364 !pmap_try_insert_pv_entry(pmap, va, m, lockp)) { 3365 if (mpte != NULL) { 3366 SLIST_INIT(&free); 3367 if (pmap_unwire_ptp(pmap, va, mpte, &free)) { 3368 /* 3369 * Although "va" is not mapped, paging- 3370 * structure caches could nonetheless have 3371 * entries that refer to the freed page table 3372 * pages. Invalidate those entries. 3373 */ 3374 *invalidate = true; 3375 vm_page_free_pages_toq(&free, true); 3376 } 3377 mpte = NULL; 3378 } 3379 return (mpte); 3380 } 3381 3382 /* 3383 * Increment counters 3384 */ 3385 pmap_resident_count_inc(pmap, 1); 3386 3387 pa = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(m->md.mdpg_cache_attrs); 3388 if (prot & VM_PROT_EXECUTE) 3389 pa |= PG_X; 3390 else 3391 pa |= RPTE_EAA_R; 3392 if ((m->oflags & VPO_UNMANAGED) == 0) 3393 pa |= PG_MANAGED; 3394 3395 pte_store(pte, pa); 3396 return (mpte); 3397 } 3398 3399 void 3400 mmu_radix_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, 3401 vm_prot_t prot) 3402 { 3403 struct rwlock *lock; 3404 bool invalidate; 3405 3406 lock = NULL; 3407 invalidate = false; 3408 PMAP_LOCK(pmap); 3409 mmu_radix_enter_quick_locked(pmap, va, m, prot, NULL, &lock, 3410 &invalidate); 3411 ptesync(); 3412 if (lock != NULL) 3413 rw_wunlock(lock); 3414 if (invalidate) 3415 pmap_invalidate_all(pmap); 3416 PMAP_UNLOCK(pmap); 3417 } 3418 3419 vm_paddr_t 3420 mmu_radix_extract(pmap_t pmap, vm_offset_t va) 3421 { 3422 pml3_entry_t *l3e; 3423 pt_entry_t *pte; 3424 vm_paddr_t pa; 3425 3426 l3e = pmap_pml3e(pmap, va); 3427 if (__predict_false(l3e == NULL)) 3428 return (0); 3429 if (be64toh(*l3e) & RPTE_LEAF) { 3430 pa = (be64toh(*l3e) & PG_PS_FRAME) | (va & L3_PAGE_MASK); 3431 pa |= (va & L3_PAGE_MASK); 3432 } else { 3433 /* 3434 * Beware of a concurrent promotion that changes the 3435 * PDE at this point! For example, vtopte() must not 3436 * be used to access the PTE because it would use the 3437 * new PDE. It is, however, safe to use the old PDE 3438 * because the page table page is preserved by the 3439 * promotion. 3440 */ 3441 pte = pmap_l3e_to_pte(l3e, va); 3442 if (__predict_false(pte == NULL)) 3443 return (0); 3444 pa = be64toh(*pte); 3445 pa = (pa & PG_FRAME) | (va & PAGE_MASK); 3446 pa |= (va & PAGE_MASK); 3447 } 3448 return (pa); 3449 } 3450 3451 vm_page_t 3452 mmu_radix_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot) 3453 { 3454 pml3_entry_t l3e, *l3ep; 3455 pt_entry_t pte; 3456 vm_paddr_t pa; 3457 vm_page_t m; 3458 3459 pa = 0; 3460 m = NULL; 3461 CTR4(KTR_PMAP, "%s(%p, %#x, %#x)", __func__, pmap, va, prot); 3462 PMAP_LOCK(pmap); 3463 l3ep = pmap_pml3e(pmap, va); 3464 if (l3ep != NULL && (l3e = be64toh(*l3ep))) { 3465 if (l3e & RPTE_LEAF) { 3466 if ((l3e & PG_RW) || (prot & VM_PROT_WRITE) == 0) 3467 m = PHYS_TO_VM_PAGE((l3e & PG_PS_FRAME) | 3468 (va & L3_PAGE_MASK)); 3469 } else { 3470 /* Native endian PTE, do not pass to pmap functions */ 3471 pte = be64toh(*pmap_l3e_to_pte(l3ep, va)); 3472 if ((pte & PG_V) && 3473 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) 3474 m = PHYS_TO_VM_PAGE(pte & PG_FRAME); 3475 } 3476 if (m != NULL && !vm_page_wire_mapped(m)) 3477 m = NULL; 3478 } 3479 PMAP_UNLOCK(pmap); 3480 return (m); 3481 } 3482 3483 static void 3484 mmu_radix_growkernel(vm_offset_t addr) 3485 { 3486 vm_paddr_t paddr; 3487 vm_page_t nkpg; 3488 pml3_entry_t *l3e; 3489 pml2_entry_t *l2e; 3490 3491 CTR2(KTR_PMAP, "%s(%#x)", __func__, addr); 3492 if (VM_MIN_KERNEL_ADDRESS < addr && 3493 addr < (VM_MIN_KERNEL_ADDRESS + nkpt * L3_PAGE_SIZE)) 3494 return; 3495 3496 addr = roundup2(addr, L3_PAGE_SIZE); 3497 if (addr - 1 >= vm_map_max(kernel_map)) 3498 addr = vm_map_max(kernel_map); 3499 while (kernel_vm_end < addr) { 3500 l2e = pmap_pml2e(kernel_pmap, kernel_vm_end); 3501 if ((be64toh(*l2e) & PG_V) == 0) { 3502 /* We need a new PDP entry */ 3503 nkpg = vm_page_alloc(NULL, kernel_vm_end >> L2_PAGE_SIZE_SHIFT, 3504 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | 3505 VM_ALLOC_WIRED | VM_ALLOC_ZERO); 3506 if (nkpg == NULL) 3507 panic("pmap_growkernel: no memory to grow kernel"); 3508 if ((nkpg->flags & PG_ZERO) == 0) 3509 mmu_radix_zero_page(nkpg); 3510 paddr = VM_PAGE_TO_PHYS(nkpg); 3511 pde_store(l2e, paddr); 3512 continue; /* try again */ 3513 } 3514 l3e = pmap_l2e_to_l3e(l2e, kernel_vm_end); 3515 if ((be64toh(*l3e) & PG_V) != 0) { 3516 kernel_vm_end = (kernel_vm_end + L3_PAGE_SIZE) & ~L3_PAGE_MASK; 3517 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) { 3518 kernel_vm_end = vm_map_max(kernel_map); 3519 break; 3520 } 3521 continue; 3522 } 3523 3524 nkpg = vm_page_alloc(NULL, pmap_l3e_pindex(kernel_vm_end), 3525 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | 3526 VM_ALLOC_ZERO); 3527 if (nkpg == NULL) 3528 panic("pmap_growkernel: no memory to grow kernel"); 3529 if ((nkpg->flags & PG_ZERO) == 0) 3530 mmu_radix_zero_page(nkpg); 3531 paddr = VM_PAGE_TO_PHYS(nkpg); 3532 pde_store(l3e, paddr); 3533 3534 kernel_vm_end = (kernel_vm_end + L3_PAGE_SIZE) & ~L3_PAGE_MASK; 3535 if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) { 3536 kernel_vm_end = vm_map_max(kernel_map); 3537 break; 3538 } 3539 } 3540 ptesync(); 3541 } 3542 3543 static MALLOC_DEFINE(M_RADIX_PGD, "radix_pgd", "radix page table root directory"); 3544 static uma_zone_t zone_radix_pgd; 3545 3546 static int 3547 radix_pgd_import(void *arg __unused, void **store, int count, int domain __unused, 3548 int flags) 3549 { 3550 3551 for (int i = 0; i < count; i++) { 3552 vm_page_t m = vm_page_alloc_contig(NULL, 0, 3553 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | 3554 VM_ALLOC_ZERO | VM_ALLOC_WAITOK, RADIX_PGD_SIZE/PAGE_SIZE, 3555 0, (vm_paddr_t)-1, RADIX_PGD_SIZE, L1_PAGE_SIZE, 3556 VM_MEMATTR_DEFAULT); 3557 /* XXX zero on alloc here so we don't have to later */ 3558 store[i] = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)); 3559 } 3560 return (count); 3561 } 3562 3563 static void 3564 radix_pgd_release(void *arg __unused, void **store, int count) 3565 { 3566 vm_page_t m; 3567 struct spglist free; 3568 int page_count; 3569 3570 SLIST_INIT(&free); 3571 page_count = RADIX_PGD_SIZE/PAGE_SIZE; 3572 3573 for (int i = 0; i < count; i++) { 3574 /* 3575 * XXX selectively remove dmap and KVA entries so we don't 3576 * need to bzero 3577 */ 3578 m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)store[i])); 3579 for (int j = page_count-1; j >= 0; j--) { 3580 vm_page_unwire_noq(&m[j]); 3581 SLIST_INSERT_HEAD(&free, &m[j], plinks.s.ss); 3582 } 3583 vm_page_free_pages_toq(&free, false); 3584 } 3585 } 3586 3587 static void 3588 mmu_radix_init() 3589 { 3590 vm_page_t mpte; 3591 vm_size_t s; 3592 int error, i, pv_npg; 3593 3594 /* XXX is this really needed for POWER? */ 3595 /* L1TF, reserve page @0 unconditionally */ 3596 vm_page_blacklist_add(0, bootverbose); 3597 3598 zone_radix_pgd = uma_zcache_create("radix_pgd_cache", 3599 RADIX_PGD_SIZE, NULL, NULL, 3600 #ifdef INVARIANTS 3601 trash_init, trash_fini, 3602 #else 3603 NULL, NULL, 3604 #endif 3605 radix_pgd_import, radix_pgd_release, 3606 NULL, UMA_ZONE_NOBUCKET); 3607 3608 /* 3609 * Initialize the vm page array entries for the kernel pmap's 3610 * page table pages. 3611 */ 3612 PMAP_LOCK(kernel_pmap); 3613 for (i = 0; i < nkpt; i++) { 3614 mpte = PHYS_TO_VM_PAGE(KPTphys + (i << PAGE_SHIFT)); 3615 KASSERT(mpte >= vm_page_array && 3616 mpte < &vm_page_array[vm_page_array_size], 3617 ("pmap_init: page table page is out of range size: %lu", 3618 vm_page_array_size)); 3619 mpte->pindex = pmap_l3e_pindex(VM_MIN_KERNEL_ADDRESS) + i; 3620 mpte->phys_addr = KPTphys + (i << PAGE_SHIFT); 3621 MPASS(PHYS_TO_VM_PAGE(mpte->phys_addr) == mpte); 3622 //pmap_insert_pt_page(kernel_pmap, mpte); 3623 mpte->ref_count = 1; 3624 } 3625 PMAP_UNLOCK(kernel_pmap); 3626 vm_wire_add(nkpt); 3627 3628 CTR1(KTR_PMAP, "%s()", __func__); 3629 TAILQ_INIT(&pv_dummy.pv_list); 3630 3631 /* 3632 * Are large page mappings enabled? 3633 */ 3634 TUNABLE_INT_FETCH("vm.pmap.superpages_enabled", &superpages_enabled); 3635 if (superpages_enabled) { 3636 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0, 3637 ("pmap_init: can't assign to pagesizes[1]")); 3638 pagesizes[1] = L3_PAGE_SIZE; 3639 } 3640 3641 /* 3642 * Initialize the pv chunk list mutex. 3643 */ 3644 mtx_init(&pv_chunks_mutex, "pmap pv chunk list", NULL, MTX_DEF); 3645 3646 /* 3647 * Initialize the pool of pv list locks. 3648 */ 3649 for (i = 0; i < NPV_LIST_LOCKS; i++) 3650 rw_init(&pv_list_locks[i], "pmap pv list"); 3651 3652 /* 3653 * Calculate the size of the pv head table for superpages. 3654 */ 3655 pv_npg = howmany(vm_phys_segs[vm_phys_nsegs - 1].end, L3_PAGE_SIZE); 3656 3657 /* 3658 * Allocate memory for the pv head table for superpages. 3659 */ 3660 s = (vm_size_t)(pv_npg * sizeof(struct md_page)); 3661 s = round_page(s); 3662 pv_table = (struct md_page *)kmem_malloc(s, M_WAITOK | M_ZERO); 3663 for (i = 0; i < pv_npg; i++) 3664 TAILQ_INIT(&pv_table[i].pv_list); 3665 TAILQ_INIT(&pv_dummy.pv_list); 3666 3667 pmap_initialized = 1; 3668 mtx_init(&qframe_mtx, "qfrmlk", NULL, MTX_SPIN); 3669 error = vmem_alloc(kernel_arena, PAGE_SIZE, M_BESTFIT | M_WAITOK, 3670 (vmem_addr_t *)&qframe); 3671 3672 if (error != 0) 3673 panic("qframe allocation failed"); 3674 asid_arena = vmem_create("ASID", isa3_base_pid + 1, (1<<isa3_pid_bits), 3675 1, 1, M_WAITOK); 3676 } 3677 3678 static boolean_t 3679 pmap_page_test_mappings(vm_page_t m, boolean_t accessed, boolean_t modified) 3680 { 3681 struct rwlock *lock; 3682 pv_entry_t pv; 3683 struct md_page *pvh; 3684 pt_entry_t *pte, mask; 3685 pmap_t pmap; 3686 int md_gen, pvh_gen; 3687 boolean_t rv; 3688 3689 rv = FALSE; 3690 lock = VM_PAGE_TO_PV_LIST_LOCK(m); 3691 rw_rlock(lock); 3692 restart: 3693 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) { 3694 pmap = PV_PMAP(pv); 3695 if (!PMAP_TRYLOCK(pmap)) { 3696 md_gen = m->md.pv_gen; 3697 rw_runlock(lock); 3698 PMAP_LOCK(pmap); 3699 rw_rlock(lock); 3700 if (md_gen != m->md.pv_gen) { 3701 PMAP_UNLOCK(pmap); 3702 goto restart; 3703 } 3704 } 3705 pte = pmap_pte(pmap, pv->pv_va); 3706 mask = 0; 3707 if (modified) 3708 mask |= PG_RW | PG_M; 3709 if (accessed) 3710 mask |= PG_V | PG_A; 3711 rv = (be64toh(*pte) & mask) == mask; 3712 PMAP_UNLOCK(pmap); 3713 if (rv) 3714 goto out; 3715 } 3716 if ((m->flags & PG_FICTITIOUS) == 0) { 3717 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m)); 3718 TAILQ_FOREACH(pv, &pvh->pv_list, pv_link) { 3719 pmap = PV_PMAP(pv); 3720 if (!PMAP_TRYLOCK(pmap)) { 3721 md_gen = m->md.pv_gen; 3722 pvh_gen = pvh->pv_gen; 3723 rw_runlock(lock); 3724 PMAP_LOCK(pmap); 3725 rw_rlock(lock); 3726 if (md_gen != m->md.pv_gen || 3727 pvh_gen != pvh->pv_gen) { 3728 PMAP_UNLOCK(pmap); 3729 goto restart; 3730 } 3731 } 3732 pte = pmap_pml3e(pmap, pv->pv_va); 3733 mask = 0; 3734 if (modified) 3735 mask |= PG_RW | PG_M; 3736 if (accessed) 3737 mask |= PG_V | PG_A; 3738 rv = (be64toh(*pte) & mask) == mask; 3739 PMAP_UNLOCK(pmap); 3740 if (rv) 3741 goto out; 3742 } 3743 } 3744 out: 3745 rw_runlock(lock); 3746 return (rv); 3747 } 3748 3749 /* 3750 * pmap_is_modified: 3751 * 3752 * Return whether or not the specified physical page was modified 3753 * in any physical maps. 3754 */ 3755 boolean_t 3756 mmu_radix_is_modified(vm_page_t m) 3757 { 3758 3759 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 3760 ("pmap_is_modified: page %p is not managed", m)); 3761 3762 CTR2(KTR_PMAP, "%s(%p)", __func__, m); 3763 /* 3764 * If the page is not busied then this check is racy. 3765 */ 3766 if (!pmap_page_is_write_mapped(m)) 3767 return (FALSE); 3768 return (pmap_page_test_mappings(m, FALSE, TRUE)); 3769 } 3770 3771 boolean_t 3772 mmu_radix_is_prefaultable(pmap_t pmap, vm_offset_t addr) 3773 { 3774 pml3_entry_t *l3e; 3775 pt_entry_t *pte; 3776 boolean_t rv; 3777 3778 CTR3(KTR_PMAP, "%s(%p, %#x)", __func__, pmap, addr); 3779 rv = FALSE; 3780 PMAP_LOCK(pmap); 3781 l3e = pmap_pml3e(pmap, addr); 3782 if (l3e != NULL && (be64toh(*l3e) & (RPTE_LEAF | PG_V)) == PG_V) { 3783 pte = pmap_l3e_to_pte(l3e, addr); 3784 rv = (be64toh(*pte) & PG_V) == 0; 3785 } 3786 PMAP_UNLOCK(pmap); 3787 return (rv); 3788 } 3789 3790 boolean_t 3791 mmu_radix_is_referenced(vm_page_t m) 3792 { 3793 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 3794 ("pmap_is_referenced: page %p is not managed", m)); 3795 CTR2(KTR_PMAP, "%s(%p)", __func__, m); 3796 return (pmap_page_test_mappings(m, TRUE, FALSE)); 3797 } 3798 3799 /* 3800 * pmap_ts_referenced: 3801 * 3802 * Return a count of reference bits for a page, clearing those bits. 3803 * It is not necessary for every reference bit to be cleared, but it 3804 * is necessary that 0 only be returned when there are truly no 3805 * reference bits set. 3806 * 3807 * As an optimization, update the page's dirty field if a modified bit is 3808 * found while counting reference bits. This opportunistic update can be 3809 * performed at low cost and can eliminate the need for some future calls 3810 * to pmap_is_modified(). However, since this function stops after 3811 * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some 3812 * dirty pages. Those dirty pages will only be detected by a future call 3813 * to pmap_is_modified(). 3814 * 3815 * A DI block is not needed within this function, because 3816 * invalidations are performed before the PV list lock is 3817 * released. 3818 */ 3819 boolean_t 3820 mmu_radix_ts_referenced(vm_page_t m) 3821 { 3822 struct md_page *pvh; 3823 pv_entry_t pv, pvf; 3824 pmap_t pmap; 3825 struct rwlock *lock; 3826 pml3_entry_t oldl3e, *l3e; 3827 pt_entry_t *pte; 3828 vm_paddr_t pa; 3829 int cleared, md_gen, not_cleared, pvh_gen; 3830 struct spglist free; 3831 3832 CTR2(KTR_PMAP, "%s(%p)", __func__, m); 3833 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 3834 ("pmap_ts_referenced: page %p is not managed", m)); 3835 SLIST_INIT(&free); 3836 cleared = 0; 3837 pa = VM_PAGE_TO_PHYS(m); 3838 lock = PHYS_TO_PV_LIST_LOCK(pa); 3839 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : pa_to_pvh(pa); 3840 rw_wlock(lock); 3841 retry: 3842 not_cleared = 0; 3843 if ((pvf = TAILQ_FIRST(&pvh->pv_list)) == NULL) 3844 goto small_mappings; 3845 pv = pvf; 3846 do { 3847 if (pvf == NULL) 3848 pvf = pv; 3849 pmap = PV_PMAP(pv); 3850 if (!PMAP_TRYLOCK(pmap)) { 3851 pvh_gen = pvh->pv_gen; 3852 rw_wunlock(lock); 3853 PMAP_LOCK(pmap); 3854 rw_wlock(lock); 3855 if (pvh_gen != pvh->pv_gen) { 3856 PMAP_UNLOCK(pmap); 3857 goto retry; 3858 } 3859 } 3860 l3e = pmap_pml3e(pmap, pv->pv_va); 3861 oldl3e = be64toh(*l3e); 3862 if ((oldl3e & (PG_M | PG_RW)) == (PG_M | PG_RW)) { 3863 /* 3864 * Although "oldpde" is mapping a 2MB page, because 3865 * this function is called at a 4KB page granularity, 3866 * we only update the 4KB page under test. 3867 */ 3868 vm_page_dirty(m); 3869 } 3870 if ((oldl3e & PG_A) != 0) { 3871 /* 3872 * Since this reference bit is shared by 512 4KB 3873 * pages, it should not be cleared every time it is 3874 * tested. Apply a simple "hash" function on the 3875 * physical page number, the virtual superpage number, 3876 * and the pmap address to select one 4KB page out of 3877 * the 512 on which testing the reference bit will 3878 * result in clearing that reference bit. This 3879 * function is designed to avoid the selection of the 3880 * same 4KB page for every 2MB page mapping. 3881 * 3882 * On demotion, a mapping that hasn't been referenced 3883 * is simply destroyed. To avoid the possibility of a 3884 * subsequent page fault on a demoted wired mapping, 3885 * always leave its reference bit set. Moreover, 3886 * since the superpage is wired, the current state of 3887 * its reference bit won't affect page replacement. 3888 */ 3889 if ((((pa >> PAGE_SHIFT) ^ (pv->pv_va >> L3_PAGE_SIZE_SHIFT) ^ 3890 (uintptr_t)pmap) & (NPTEPG - 1)) == 0 && 3891 (oldl3e & PG_W) == 0) { 3892 atomic_clear_long(l3e, htobe64(PG_A)); 3893 pmap_invalidate_page(pmap, pv->pv_va); 3894 cleared++; 3895 KASSERT(lock == VM_PAGE_TO_PV_LIST_LOCK(m), 3896 ("inconsistent pv lock %p %p for page %p", 3897 lock, VM_PAGE_TO_PV_LIST_LOCK(m), m)); 3898 } else 3899 not_cleared++; 3900 } 3901 PMAP_UNLOCK(pmap); 3902 /* Rotate the PV list if it has more than one entry. */ 3903 if (pv != NULL && TAILQ_NEXT(pv, pv_link) != NULL) { 3904 TAILQ_REMOVE(&pvh->pv_list, pv, pv_link); 3905 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_link); 3906 pvh->pv_gen++; 3907 } 3908 if (cleared + not_cleared >= PMAP_TS_REFERENCED_MAX) 3909 goto out; 3910 } while ((pv = TAILQ_FIRST(&pvh->pv_list)) != pvf); 3911 small_mappings: 3912 if ((pvf = TAILQ_FIRST(&m->md.pv_list)) == NULL) 3913 goto out; 3914 pv = pvf; 3915 do { 3916 if (pvf == NULL) 3917 pvf = pv; 3918 pmap = PV_PMAP(pv); 3919 if (!PMAP_TRYLOCK(pmap)) { 3920 pvh_gen = pvh->pv_gen; 3921 md_gen = m->md.pv_gen; 3922 rw_wunlock(lock); 3923 PMAP_LOCK(pmap); 3924 rw_wlock(lock); 3925 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) { 3926 PMAP_UNLOCK(pmap); 3927 goto retry; 3928 } 3929 } 3930 l3e = pmap_pml3e(pmap, pv->pv_va); 3931 KASSERT((be64toh(*l3e) & RPTE_LEAF) == 0, 3932 ("pmap_ts_referenced: found a 2mpage in page %p's pv list", 3933 m)); 3934 pte = pmap_l3e_to_pte(l3e, pv->pv_va); 3935 if ((be64toh(*pte) & (PG_M | PG_RW)) == (PG_M | PG_RW)) 3936 vm_page_dirty(m); 3937 if ((be64toh(*pte) & PG_A) != 0) { 3938 atomic_clear_long(pte, htobe64(PG_A)); 3939 pmap_invalidate_page(pmap, pv->pv_va); 3940 cleared++; 3941 } 3942 PMAP_UNLOCK(pmap); 3943 /* Rotate the PV list if it has more than one entry. */ 3944 if (pv != NULL && TAILQ_NEXT(pv, pv_link) != NULL) { 3945 TAILQ_REMOVE(&m->md.pv_list, pv, pv_link); 3946 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_link); 3947 m->md.pv_gen++; 3948 } 3949 } while ((pv = TAILQ_FIRST(&m->md.pv_list)) != pvf && cleared + 3950 not_cleared < PMAP_TS_REFERENCED_MAX); 3951 out: 3952 rw_wunlock(lock); 3953 vm_page_free_pages_toq(&free, true); 3954 return (cleared + not_cleared); 3955 } 3956 3957 static vm_offset_t 3958 mmu_radix_map(vm_offset_t *virt __unused, vm_paddr_t start, 3959 vm_paddr_t end, int prot __unused) 3960 { 3961 3962 CTR5(KTR_PMAP, "%s(%p, %#x, %#x, %#x)", __func__, virt, start, end, 3963 prot); 3964 return (PHYS_TO_DMAP(start)); 3965 } 3966 3967 void 3968 mmu_radix_object_init_pt(pmap_t pmap, vm_offset_t addr, 3969 vm_object_t object, vm_pindex_t pindex, vm_size_t size) 3970 { 3971 pml3_entry_t *l3e; 3972 vm_paddr_t pa, ptepa; 3973 vm_page_t p, pdpg; 3974 vm_memattr_t ma; 3975 3976 CTR6(KTR_PMAP, "%s(%p, %#x, %p, %u, %#x)", __func__, pmap, addr, 3977 object, pindex, size); 3978 VM_OBJECT_ASSERT_WLOCKED(object); 3979 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG, 3980 ("pmap_object_init_pt: non-device object")); 3981 /* NB: size can be logically ored with addr here */ 3982 if ((addr & L3_PAGE_MASK) == 0 && (size & L3_PAGE_MASK) == 0) { 3983 if (!mmu_radix_ps_enabled(pmap)) 3984 return; 3985 if (!vm_object_populate(object, pindex, pindex + atop(size))) 3986 return; 3987 p = vm_page_lookup(object, pindex); 3988 KASSERT(p->valid == VM_PAGE_BITS_ALL, 3989 ("pmap_object_init_pt: invalid page %p", p)); 3990 ma = p->md.mdpg_cache_attrs; 3991 3992 /* 3993 * Abort the mapping if the first page is not physically 3994 * aligned to a 2MB page boundary. 3995 */ 3996 ptepa = VM_PAGE_TO_PHYS(p); 3997 if (ptepa & L3_PAGE_MASK) 3998 return; 3999 4000 /* 4001 * Skip the first page. Abort the mapping if the rest of 4002 * the pages are not physically contiguous or have differing 4003 * memory attributes. 4004 */ 4005 p = TAILQ_NEXT(p, listq); 4006 for (pa = ptepa + PAGE_SIZE; pa < ptepa + size; 4007 pa += PAGE_SIZE) { 4008 KASSERT(p->valid == VM_PAGE_BITS_ALL, 4009 ("pmap_object_init_pt: invalid page %p", p)); 4010 if (pa != VM_PAGE_TO_PHYS(p) || 4011 ma != p->md.mdpg_cache_attrs) 4012 return; 4013 p = TAILQ_NEXT(p, listq); 4014 } 4015 4016 PMAP_LOCK(pmap); 4017 for (pa = ptepa | pmap_cache_bits(ma); 4018 pa < ptepa + size; pa += L3_PAGE_SIZE) { 4019 pdpg = pmap_allocl3e(pmap, addr, NULL); 4020 if (pdpg == NULL) { 4021 /* 4022 * The creation of mappings below is only an 4023 * optimization. If a page directory page 4024 * cannot be allocated without blocking, 4025 * continue on to the next mapping rather than 4026 * blocking. 4027 */ 4028 addr += L3_PAGE_SIZE; 4029 continue; 4030 } 4031 l3e = (pml3_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pdpg)); 4032 l3e = &l3e[pmap_pml3e_index(addr)]; 4033 if ((be64toh(*l3e) & PG_V) == 0) { 4034 pa |= PG_M | PG_A | PG_RW; 4035 pte_store(l3e, pa); 4036 pmap_resident_count_inc(pmap, L3_PAGE_SIZE / PAGE_SIZE); 4037 atomic_add_long(&pmap_l3e_mappings, 1); 4038 } else { 4039 /* Continue on if the PDE is already valid. */ 4040 pdpg->ref_count--; 4041 KASSERT(pdpg->ref_count > 0, 4042 ("pmap_object_init_pt: missing reference " 4043 "to page directory page, va: 0x%lx", addr)); 4044 } 4045 addr += L3_PAGE_SIZE; 4046 } 4047 ptesync(); 4048 PMAP_UNLOCK(pmap); 4049 } 4050 } 4051 4052 boolean_t 4053 mmu_radix_page_exists_quick(pmap_t pmap, vm_page_t m) 4054 { 4055 struct md_page *pvh; 4056 struct rwlock *lock; 4057 pv_entry_t pv; 4058 int loops = 0; 4059 boolean_t rv; 4060 4061 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 4062 ("pmap_page_exists_quick: page %p is not managed", m)); 4063 CTR3(KTR_PMAP, "%s(%p, %p)", __func__, pmap, m); 4064 rv = FALSE; 4065 lock = VM_PAGE_TO_PV_LIST_LOCK(m); 4066 rw_rlock(lock); 4067 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) { 4068 if (PV_PMAP(pv) == pmap) { 4069 rv = TRUE; 4070 break; 4071 } 4072 loops++; 4073 if (loops >= 16) 4074 break; 4075 } 4076 if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) { 4077 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m)); 4078 TAILQ_FOREACH(pv, &pvh->pv_list, pv_link) { 4079 if (PV_PMAP(pv) == pmap) { 4080 rv = TRUE; 4081 break; 4082 } 4083 loops++; 4084 if (loops >= 16) 4085 break; 4086 } 4087 } 4088 rw_runlock(lock); 4089 return (rv); 4090 } 4091 4092 void 4093 mmu_radix_page_init(vm_page_t m) 4094 { 4095 4096 CTR2(KTR_PMAP, "%s(%p)", __func__, m); 4097 TAILQ_INIT(&m->md.pv_list); 4098 m->md.mdpg_cache_attrs = VM_MEMATTR_DEFAULT; 4099 } 4100 4101 int 4102 mmu_radix_page_wired_mappings(vm_page_t m) 4103 { 4104 struct rwlock *lock; 4105 struct md_page *pvh; 4106 pmap_t pmap; 4107 pt_entry_t *pte; 4108 pv_entry_t pv; 4109 int count, md_gen, pvh_gen; 4110 4111 if ((m->oflags & VPO_UNMANAGED) != 0) 4112 return (0); 4113 CTR2(KTR_PMAP, "%s(%p)", __func__, m); 4114 lock = VM_PAGE_TO_PV_LIST_LOCK(m); 4115 rw_rlock(lock); 4116 restart: 4117 count = 0; 4118 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) { 4119 pmap = PV_PMAP(pv); 4120 if (!PMAP_TRYLOCK(pmap)) { 4121 md_gen = m->md.pv_gen; 4122 rw_runlock(lock); 4123 PMAP_LOCK(pmap); 4124 rw_rlock(lock); 4125 if (md_gen != m->md.pv_gen) { 4126 PMAP_UNLOCK(pmap); 4127 goto restart; 4128 } 4129 } 4130 pte = pmap_pte(pmap, pv->pv_va); 4131 if ((be64toh(*pte) & PG_W) != 0) 4132 count++; 4133 PMAP_UNLOCK(pmap); 4134 } 4135 if ((m->flags & PG_FICTITIOUS) == 0) { 4136 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m)); 4137 TAILQ_FOREACH(pv, &pvh->pv_list, pv_link) { 4138 pmap = PV_PMAP(pv); 4139 if (!PMAP_TRYLOCK(pmap)) { 4140 md_gen = m->md.pv_gen; 4141 pvh_gen = pvh->pv_gen; 4142 rw_runlock(lock); 4143 PMAP_LOCK(pmap); 4144 rw_rlock(lock); 4145 if (md_gen != m->md.pv_gen || 4146 pvh_gen != pvh->pv_gen) { 4147 PMAP_UNLOCK(pmap); 4148 goto restart; 4149 } 4150 } 4151 pte = pmap_pml3e(pmap, pv->pv_va); 4152 if ((be64toh(*pte) & PG_W) != 0) 4153 count++; 4154 PMAP_UNLOCK(pmap); 4155 } 4156 } 4157 rw_runlock(lock); 4158 return (count); 4159 } 4160 4161 static void 4162 mmu_radix_update_proctab(int pid, pml1_entry_t l1pa) 4163 { 4164 isa3_proctab[pid].proctab0 = htobe64(RTS_SIZE | l1pa | RADIX_PGD_INDEX_SHIFT); 4165 } 4166 4167 int 4168 mmu_radix_pinit(pmap_t pmap) 4169 { 4170 vmem_addr_t pid; 4171 vm_paddr_t l1pa; 4172 4173 CTR2(KTR_PMAP, "%s(%p)", __func__, pmap); 4174 4175 /* 4176 * allocate the page directory page 4177 */ 4178 pmap->pm_pml1 = uma_zalloc(zone_radix_pgd, M_WAITOK); 4179 4180 for (int j = 0; j < RADIX_PGD_SIZE_SHIFT; j++) 4181 pagezero((vm_offset_t)pmap->pm_pml1 + j * PAGE_SIZE); 4182 pmap->pm_radix.rt_root = 0; 4183 TAILQ_INIT(&pmap->pm_pvchunk); 4184 bzero(&pmap->pm_stats, sizeof pmap->pm_stats); 4185 pmap->pm_flags = PMAP_PDE_SUPERPAGE; 4186 vmem_alloc(asid_arena, 1, M_FIRSTFIT|M_WAITOK, &pid); 4187 4188 pmap->pm_pid = pid; 4189 l1pa = DMAP_TO_PHYS((vm_offset_t)pmap->pm_pml1); 4190 mmu_radix_update_proctab(pid, l1pa); 4191 __asm __volatile("ptesync;isync" : : : "memory"); 4192 4193 return (1); 4194 } 4195 4196 /* 4197 * This routine is called if the desired page table page does not exist. 4198 * 4199 * If page table page allocation fails, this routine may sleep before 4200 * returning NULL. It sleeps only if a lock pointer was given. 4201 * 4202 * Note: If a page allocation fails at page table level two or three, 4203 * one or two pages may be held during the wait, only to be released 4204 * afterwards. This conservative approach is easily argued to avoid 4205 * race conditions. 4206 */ 4207 static vm_page_t 4208 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex, struct rwlock **lockp) 4209 { 4210 vm_page_t m, pdppg, pdpg; 4211 4212 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 4213 4214 /* 4215 * Allocate a page table page. 4216 */ 4217 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ | 4218 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) { 4219 if (lockp != NULL) { 4220 RELEASE_PV_LIST_LOCK(lockp); 4221 PMAP_UNLOCK(pmap); 4222 vm_wait(NULL); 4223 PMAP_LOCK(pmap); 4224 } 4225 /* 4226 * Indicate the need to retry. While waiting, the page table 4227 * page may have been allocated. 4228 */ 4229 return (NULL); 4230 } 4231 if ((m->flags & PG_ZERO) == 0) 4232 mmu_radix_zero_page(m); 4233 4234 /* 4235 * Map the pagetable page into the process address space, if 4236 * it isn't already there. 4237 */ 4238 4239 if (ptepindex >= (NUPDE + NUPDPE)) { 4240 pml1_entry_t *l1e; 4241 vm_pindex_t pml1index; 4242 4243 /* Wire up a new PDPE page */ 4244 pml1index = ptepindex - (NUPDE + NUPDPE); 4245 l1e = &pmap->pm_pml1[pml1index]; 4246 pde_store(l1e, VM_PAGE_TO_PHYS(m)); 4247 4248 } else if (ptepindex >= NUPDE) { 4249 vm_pindex_t pml1index; 4250 vm_pindex_t pdpindex; 4251 pml1_entry_t *l1e; 4252 pml2_entry_t *l2e; 4253 4254 /* Wire up a new l2e page */ 4255 pdpindex = ptepindex - NUPDE; 4256 pml1index = pdpindex >> RPTE_SHIFT; 4257 4258 l1e = &pmap->pm_pml1[pml1index]; 4259 if ((be64toh(*l1e) & PG_V) == 0) { 4260 /* Have to allocate a new pdp, recurse */ 4261 if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml1index, 4262 lockp) == NULL) { 4263 vm_page_unwire_noq(m); 4264 vm_page_free_zero(m); 4265 return (NULL); 4266 } 4267 } else { 4268 /* Add reference to l2e page */ 4269 pdppg = PHYS_TO_VM_PAGE(be64toh(*l1e) & PG_FRAME); 4270 pdppg->ref_count++; 4271 } 4272 l2e = (pml2_entry_t *)PHYS_TO_DMAP(be64toh(*l1e) & PG_FRAME); 4273 4274 /* Now find the pdp page */ 4275 l2e = &l2e[pdpindex & RPTE_MASK]; 4276 pde_store(l2e, VM_PAGE_TO_PHYS(m)); 4277 4278 } else { 4279 vm_pindex_t pml1index; 4280 vm_pindex_t pdpindex; 4281 pml1_entry_t *l1e; 4282 pml2_entry_t *l2e; 4283 pml3_entry_t *l3e; 4284 4285 /* Wire up a new PTE page */ 4286 pdpindex = ptepindex >> RPTE_SHIFT; 4287 pml1index = pdpindex >> RPTE_SHIFT; 4288 4289 /* First, find the pdp and check that its valid. */ 4290 l1e = &pmap->pm_pml1[pml1index]; 4291 if ((be64toh(*l1e) & PG_V) == 0) { 4292 /* Have to allocate a new pd, recurse */ 4293 if (_pmap_allocpte(pmap, NUPDE + pdpindex, 4294 lockp) == NULL) { 4295 vm_page_unwire_noq(m); 4296 vm_page_free_zero(m); 4297 return (NULL); 4298 } 4299 l2e = (pml2_entry_t *)PHYS_TO_DMAP(be64toh(*l1e) & PG_FRAME); 4300 l2e = &l2e[pdpindex & RPTE_MASK]; 4301 } else { 4302 l2e = (pml2_entry_t *)PHYS_TO_DMAP(be64toh(*l1e) & PG_FRAME); 4303 l2e = &l2e[pdpindex & RPTE_MASK]; 4304 if ((be64toh(*l2e) & PG_V) == 0) { 4305 /* Have to allocate a new pd, recurse */ 4306 if (_pmap_allocpte(pmap, NUPDE + pdpindex, 4307 lockp) == NULL) { 4308 vm_page_unwire_noq(m); 4309 vm_page_free_zero(m); 4310 return (NULL); 4311 } 4312 } else { 4313 /* Add reference to the pd page */ 4314 pdpg = PHYS_TO_VM_PAGE(be64toh(*l2e) & PG_FRAME); 4315 pdpg->ref_count++; 4316 } 4317 } 4318 l3e = (pml3_entry_t *)PHYS_TO_DMAP(be64toh(*l2e) & PG_FRAME); 4319 4320 /* Now we know where the page directory page is */ 4321 l3e = &l3e[ptepindex & RPTE_MASK]; 4322 pde_store(l3e, VM_PAGE_TO_PHYS(m)); 4323 } 4324 4325 pmap_resident_count_inc(pmap, 1); 4326 return (m); 4327 } 4328 static vm_page_t 4329 pmap_allocl3e(pmap_t pmap, vm_offset_t va, struct rwlock **lockp) 4330 { 4331 vm_pindex_t pdpindex, ptepindex; 4332 pml2_entry_t *pdpe; 4333 vm_page_t pdpg; 4334 4335 retry: 4336 pdpe = pmap_pml2e(pmap, va); 4337 if (pdpe != NULL && (be64toh(*pdpe) & PG_V) != 0) { 4338 /* Add a reference to the pd page. */ 4339 pdpg = PHYS_TO_VM_PAGE(be64toh(*pdpe) & PG_FRAME); 4340 pdpg->ref_count++; 4341 } else { 4342 /* Allocate a pd page. */ 4343 ptepindex = pmap_l3e_pindex(va); 4344 pdpindex = ptepindex >> RPTE_SHIFT; 4345 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex, lockp); 4346 if (pdpg == NULL && lockp != NULL) 4347 goto retry; 4348 } 4349 return (pdpg); 4350 } 4351 4352 static vm_page_t 4353 pmap_allocpte(pmap_t pmap, vm_offset_t va, struct rwlock **lockp) 4354 { 4355 vm_pindex_t ptepindex; 4356 pml3_entry_t *pd; 4357 vm_page_t m; 4358 4359 /* 4360 * Calculate pagetable page index 4361 */ 4362 ptepindex = pmap_l3e_pindex(va); 4363 retry: 4364 /* 4365 * Get the page directory entry 4366 */ 4367 pd = pmap_pml3e(pmap, va); 4368 4369 /* 4370 * This supports switching from a 2MB page to a 4371 * normal 4K page. 4372 */ 4373 if (pd != NULL && (be64toh(*pd) & (RPTE_LEAF | PG_V)) == (RPTE_LEAF | PG_V)) { 4374 if (!pmap_demote_l3e_locked(pmap, pd, va, lockp)) { 4375 /* 4376 * Invalidation of the 2MB page mapping may have caused 4377 * the deallocation of the underlying PD page. 4378 */ 4379 pd = NULL; 4380 } 4381 } 4382 4383 /* 4384 * If the page table page is mapped, we just increment the 4385 * hold count, and activate it. 4386 */ 4387 if (pd != NULL && (be64toh(*pd) & PG_V) != 0) { 4388 m = PHYS_TO_VM_PAGE(be64toh(*pd) & PG_FRAME); 4389 m->ref_count++; 4390 } else { 4391 /* 4392 * Here if the pte page isn't mapped, or if it has been 4393 * deallocated. 4394 */ 4395 m = _pmap_allocpte(pmap, ptepindex, lockp); 4396 if (m == NULL && lockp != NULL) 4397 goto retry; 4398 } 4399 return (m); 4400 } 4401 4402 static void 4403 mmu_radix_pinit0(pmap_t pmap) 4404 { 4405 4406 CTR2(KTR_PMAP, "%s(%p)", __func__, pmap); 4407 PMAP_LOCK_INIT(pmap); 4408 pmap->pm_pml1 = kernel_pmap->pm_pml1; 4409 pmap->pm_pid = kernel_pmap->pm_pid; 4410 4411 pmap->pm_radix.rt_root = 0; 4412 TAILQ_INIT(&pmap->pm_pvchunk); 4413 bzero(&pmap->pm_stats, sizeof pmap->pm_stats); 4414 kernel_pmap->pm_flags = 4415 pmap->pm_flags = PMAP_PDE_SUPERPAGE; 4416 } 4417 /* 4418 * pmap_protect_l3e: do the things to protect a 2mpage in a process 4419 */ 4420 static boolean_t 4421 pmap_protect_l3e(pmap_t pmap, pt_entry_t *l3e, vm_offset_t sva, vm_prot_t prot) 4422 { 4423 pt_entry_t newpde, oldpde; 4424 vm_offset_t eva, va; 4425 vm_page_t m; 4426 boolean_t anychanged; 4427 4428 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 4429 KASSERT((sva & L3_PAGE_MASK) == 0, 4430 ("pmap_protect_l3e: sva is not 2mpage aligned")); 4431 anychanged = FALSE; 4432 retry: 4433 oldpde = newpde = be64toh(*l3e); 4434 if ((oldpde & (PG_MANAGED | PG_M | PG_RW)) == 4435 (PG_MANAGED | PG_M | PG_RW)) { 4436 eva = sva + L3_PAGE_SIZE; 4437 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME); 4438 va < eva; va += PAGE_SIZE, m++) 4439 vm_page_dirty(m); 4440 } 4441 if ((prot & VM_PROT_WRITE) == 0) { 4442 newpde &= ~(PG_RW | PG_M); 4443 newpde |= RPTE_EAA_R; 4444 } 4445 if (prot & VM_PROT_EXECUTE) 4446 newpde |= PG_X; 4447 if (newpde != oldpde) { 4448 /* 4449 * As an optimization to future operations on this PDE, clear 4450 * PG_PROMOTED. The impending invalidation will remove any 4451 * lingering 4KB page mappings from the TLB. 4452 */ 4453 if (!atomic_cmpset_long(l3e, htobe64(oldpde), htobe64(newpde & ~PG_PROMOTED))) 4454 goto retry; 4455 anychanged = TRUE; 4456 } 4457 return (anychanged); 4458 } 4459 4460 void 4461 mmu_radix_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, 4462 vm_prot_t prot) 4463 { 4464 vm_offset_t va_next; 4465 pml1_entry_t *l1e; 4466 pml2_entry_t *l2e; 4467 pml3_entry_t ptpaddr, *l3e; 4468 pt_entry_t *pte; 4469 boolean_t anychanged; 4470 4471 CTR5(KTR_PMAP, "%s(%p, %#x, %#x, %#x)", __func__, pmap, sva, eva, 4472 prot); 4473 4474 KASSERT((prot & ~VM_PROT_ALL) == 0, ("invalid prot %x", prot)); 4475 if (prot == VM_PROT_NONE) { 4476 mmu_radix_remove(pmap, sva, eva); 4477 return; 4478 } 4479 4480 if ((prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) == 4481 (VM_PROT_WRITE|VM_PROT_EXECUTE)) 4482 return; 4483 4484 #ifdef INVARIANTS 4485 if (VERBOSE_PROTECT || pmap_logging) 4486 printf("pmap_protect(%p, %#lx, %#lx, %x) - asid: %lu\n", 4487 pmap, sva, eva, prot, pmap->pm_pid); 4488 #endif 4489 anychanged = FALSE; 4490 4491 PMAP_LOCK(pmap); 4492 for (; sva < eva; sva = va_next) { 4493 l1e = pmap_pml1e(pmap, sva); 4494 if ((be64toh(*l1e) & PG_V) == 0) { 4495 va_next = (sva + L1_PAGE_SIZE) & ~L1_PAGE_MASK; 4496 if (va_next < sva) 4497 va_next = eva; 4498 continue; 4499 } 4500 4501 l2e = pmap_l1e_to_l2e(l1e, sva); 4502 if ((be64toh(*l2e) & PG_V) == 0) { 4503 va_next = (sva + L2_PAGE_SIZE) & ~L2_PAGE_MASK; 4504 if (va_next < sva) 4505 va_next = eva; 4506 continue; 4507 } 4508 4509 va_next = (sva + L3_PAGE_SIZE) & ~L3_PAGE_MASK; 4510 if (va_next < sva) 4511 va_next = eva; 4512 4513 l3e = pmap_l2e_to_l3e(l2e, sva); 4514 ptpaddr = be64toh(*l3e); 4515 4516 /* 4517 * Weed out invalid mappings. 4518 */ 4519 if (ptpaddr == 0) 4520 continue; 4521 4522 /* 4523 * Check for large page. 4524 */ 4525 if ((ptpaddr & RPTE_LEAF) != 0) { 4526 /* 4527 * Are we protecting the entire large page? If not, 4528 * demote the mapping and fall through. 4529 */ 4530 if (sva + L3_PAGE_SIZE == va_next && eva >= va_next) { 4531 if (pmap_protect_l3e(pmap, l3e, sva, prot)) 4532 anychanged = TRUE; 4533 continue; 4534 } else if (!pmap_demote_l3e(pmap, l3e, sva)) { 4535 /* 4536 * The large page mapping was destroyed. 4537 */ 4538 continue; 4539 } 4540 } 4541 4542 if (va_next > eva) 4543 va_next = eva; 4544 4545 for (pte = pmap_l3e_to_pte(l3e, sva); sva != va_next; pte++, 4546 sva += PAGE_SIZE) { 4547 pt_entry_t obits, pbits; 4548 vm_page_t m; 4549 4550 retry: 4551 MPASS(pte == pmap_pte(pmap, sva)); 4552 obits = pbits = be64toh(*pte); 4553 if ((pbits & PG_V) == 0) 4554 continue; 4555 4556 if ((prot & VM_PROT_WRITE) == 0) { 4557 if ((pbits & (PG_MANAGED | PG_M | PG_RW)) == 4558 (PG_MANAGED | PG_M | PG_RW)) { 4559 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME); 4560 vm_page_dirty(m); 4561 } 4562 pbits &= ~(PG_RW | PG_M); 4563 pbits |= RPTE_EAA_R; 4564 } 4565 if (prot & VM_PROT_EXECUTE) 4566 pbits |= PG_X; 4567 4568 if (pbits != obits) { 4569 if (!atomic_cmpset_long(pte, htobe64(obits), htobe64(pbits))) 4570 goto retry; 4571 if (obits & (PG_A|PG_M)) { 4572 anychanged = TRUE; 4573 #ifdef INVARIANTS 4574 if (VERBOSE_PROTECT || pmap_logging) 4575 printf("%#lx %#lx -> %#lx\n", 4576 sva, obits, pbits); 4577 #endif 4578 } 4579 } 4580 } 4581 } 4582 if (anychanged) 4583 pmap_invalidate_all(pmap); 4584 PMAP_UNLOCK(pmap); 4585 } 4586 4587 void 4588 mmu_radix_qenter(vm_offset_t sva, vm_page_t *ma, int count) 4589 { 4590 4591 CTR4(KTR_PMAP, "%s(%#x, %p, %d)", __func__, sva, ma, count); 4592 pt_entry_t oldpte, pa, *pte; 4593 vm_page_t m; 4594 uint64_t cache_bits, attr_bits; 4595 vm_offset_t va; 4596 4597 oldpte = 0; 4598 attr_bits = RPTE_EAA_R | RPTE_EAA_W | RPTE_EAA_P | PG_M | PG_A; 4599 va = sva; 4600 pte = kvtopte(va); 4601 while (va < sva + PAGE_SIZE * count) { 4602 if (__predict_false((va & L3_PAGE_MASK) == 0)) 4603 pte = kvtopte(va); 4604 MPASS(pte == pmap_pte(kernel_pmap, va)); 4605 4606 /* 4607 * XXX there has to be a more efficient way than traversing 4608 * the page table every time - but go for correctness for 4609 * today 4610 */ 4611 4612 m = *ma++; 4613 cache_bits = pmap_cache_bits(m->md.mdpg_cache_attrs); 4614 pa = VM_PAGE_TO_PHYS(m) | cache_bits | attr_bits; 4615 if (be64toh(*pte) != pa) { 4616 oldpte |= be64toh(*pte); 4617 pte_store(pte, pa); 4618 } 4619 va += PAGE_SIZE; 4620 pte++; 4621 } 4622 if (__predict_false((oldpte & RPTE_VALID) != 0)) 4623 pmap_invalidate_range(kernel_pmap, sva, sva + count * 4624 PAGE_SIZE); 4625 else 4626 ptesync(); 4627 } 4628 4629 void 4630 mmu_radix_qremove(vm_offset_t sva, int count) 4631 { 4632 vm_offset_t va; 4633 pt_entry_t *pte; 4634 4635 CTR3(KTR_PMAP, "%s(%#x, %d)", __func__, sva, count); 4636 KASSERT(sva >= VM_MIN_KERNEL_ADDRESS, ("usermode or dmap va %lx", sva)); 4637 4638 va = sva; 4639 pte = kvtopte(va); 4640 while (va < sva + PAGE_SIZE * count) { 4641 if (__predict_false((va & L3_PAGE_MASK) == 0)) 4642 pte = kvtopte(va); 4643 pte_clear(pte); 4644 pte++; 4645 va += PAGE_SIZE; 4646 } 4647 pmap_invalidate_range(kernel_pmap, sva, va); 4648 } 4649 4650 /*************************************************** 4651 * Page table page management routines..... 4652 ***************************************************/ 4653 /* 4654 * Schedule the specified unused page table page to be freed. Specifically, 4655 * add the page to the specified list of pages that will be released to the 4656 * physical memory manager after the TLB has been updated. 4657 */ 4658 static __inline void 4659 pmap_add_delayed_free_list(vm_page_t m, struct spglist *free, 4660 boolean_t set_PG_ZERO) 4661 { 4662 4663 if (set_PG_ZERO) 4664 m->flags |= PG_ZERO; 4665 else 4666 m->flags &= ~PG_ZERO; 4667 SLIST_INSERT_HEAD(free, m, plinks.s.ss); 4668 } 4669 4670 /* 4671 * Inserts the specified page table page into the specified pmap's collection 4672 * of idle page table pages. Each of a pmap's page table pages is responsible 4673 * for mapping a distinct range of virtual addresses. The pmap's collection is 4674 * ordered by this virtual address range. 4675 */ 4676 static __inline int 4677 pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte) 4678 { 4679 4680 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 4681 return (vm_radix_insert(&pmap->pm_radix, mpte)); 4682 } 4683 4684 /* 4685 * Removes the page table page mapping the specified virtual address from the 4686 * specified pmap's collection of idle page table pages, and returns it. 4687 * Otherwise, returns NULL if there is no page table page corresponding to the 4688 * specified virtual address. 4689 */ 4690 static __inline vm_page_t 4691 pmap_remove_pt_page(pmap_t pmap, vm_offset_t va) 4692 { 4693 4694 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 4695 return (vm_radix_remove(&pmap->pm_radix, pmap_l3e_pindex(va))); 4696 } 4697 4698 /* 4699 * Decrements a page table page's wire count, which is used to record the 4700 * number of valid page table entries within the page. If the wire count 4701 * drops to zero, then the page table page is unmapped. Returns TRUE if the 4702 * page table page was unmapped and FALSE otherwise. 4703 */ 4704 static inline boolean_t 4705 pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free) 4706 { 4707 4708 --m->ref_count; 4709 if (m->ref_count == 0) { 4710 _pmap_unwire_ptp(pmap, va, m, free); 4711 return (TRUE); 4712 } else 4713 return (FALSE); 4714 } 4715 4716 static void 4717 _pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free) 4718 { 4719 4720 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 4721 /* 4722 * unmap the page table page 4723 */ 4724 if (m->pindex >= (NUPDE + NUPDPE)) { 4725 /* PDP page */ 4726 pml1_entry_t *pml1; 4727 pml1 = pmap_pml1e(pmap, va); 4728 *pml1 = 0; 4729 } else if (m->pindex >= NUPDE) { 4730 /* PD page */ 4731 pml2_entry_t *l2e; 4732 l2e = pmap_pml2e(pmap, va); 4733 *l2e = 0; 4734 } else { 4735 /* PTE page */ 4736 pml3_entry_t *l3e; 4737 l3e = pmap_pml3e(pmap, va); 4738 *l3e = 0; 4739 } 4740 pmap_resident_count_dec(pmap, 1); 4741 if (m->pindex < NUPDE) { 4742 /* We just released a PT, unhold the matching PD */ 4743 vm_page_t pdpg; 4744 4745 pdpg = PHYS_TO_VM_PAGE(be64toh(*pmap_pml2e(pmap, va)) & PG_FRAME); 4746 pmap_unwire_ptp(pmap, va, pdpg, free); 4747 } 4748 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) { 4749 /* We just released a PD, unhold the matching PDP */ 4750 vm_page_t pdppg; 4751 4752 pdppg = PHYS_TO_VM_PAGE(be64toh(*pmap_pml1e(pmap, va)) & PG_FRAME); 4753 pmap_unwire_ptp(pmap, va, pdppg, free); 4754 } 4755 4756 /* 4757 * Put page on a list so that it is released after 4758 * *ALL* TLB shootdown is done 4759 */ 4760 pmap_add_delayed_free_list(m, free, TRUE); 4761 } 4762 4763 /* 4764 * After removing a page table entry, this routine is used to 4765 * conditionally free the page, and manage the hold/wire counts. 4766 */ 4767 static int 4768 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, pml3_entry_t ptepde, 4769 struct spglist *free) 4770 { 4771 vm_page_t mpte; 4772 4773 if (va >= VM_MAXUSER_ADDRESS) 4774 return (0); 4775 KASSERT(ptepde != 0, ("pmap_unuse_pt: ptepde != 0")); 4776 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME); 4777 return (pmap_unwire_ptp(pmap, va, mpte, free)); 4778 } 4779 4780 void 4781 mmu_radix_release(pmap_t pmap) 4782 { 4783 4784 CTR2(KTR_PMAP, "%s(%p)", __func__, pmap); 4785 KASSERT(pmap->pm_stats.resident_count == 0, 4786 ("pmap_release: pmap resident count %ld != 0", 4787 pmap->pm_stats.resident_count)); 4788 KASSERT(vm_radix_is_empty(&pmap->pm_radix), 4789 ("pmap_release: pmap has reserved page table page(s)")); 4790 4791 pmap_invalidate_all(pmap); 4792 isa3_proctab[pmap->pm_pid].proctab0 = 0; 4793 uma_zfree(zone_radix_pgd, pmap->pm_pml1); 4794 vmem_free(asid_arena, pmap->pm_pid, 1); 4795 } 4796 4797 /* 4798 * Create the PV entry for a 2MB page mapping. Always returns true unless the 4799 * flag PMAP_ENTER_NORECLAIM is specified. If that flag is specified, returns 4800 * false if the PV entry cannot be allocated without resorting to reclamation. 4801 */ 4802 static bool 4803 pmap_pv_insert_l3e(pmap_t pmap, vm_offset_t va, pml3_entry_t pde, u_int flags, 4804 struct rwlock **lockp) 4805 { 4806 struct md_page *pvh; 4807 pv_entry_t pv; 4808 vm_paddr_t pa; 4809 4810 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 4811 /* Pass NULL instead of the lock pointer to disable reclamation. */ 4812 if ((pv = get_pv_entry(pmap, (flags & PMAP_ENTER_NORECLAIM) != 0 ? 4813 NULL : lockp)) == NULL) 4814 return (false); 4815 pv->pv_va = va; 4816 pa = pde & PG_PS_FRAME; 4817 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa); 4818 pvh = pa_to_pvh(pa); 4819 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_link); 4820 pvh->pv_gen++; 4821 return (true); 4822 } 4823 4824 /* 4825 * Fills a page table page with mappings to consecutive physical pages. 4826 */ 4827 static void 4828 pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte) 4829 { 4830 pt_entry_t *pte; 4831 4832 for (pte = firstpte; pte < firstpte + NPTEPG; pte++) { 4833 *pte = htobe64(newpte); 4834 newpte += PAGE_SIZE; 4835 } 4836 } 4837 4838 static boolean_t 4839 pmap_demote_l3e(pmap_t pmap, pml3_entry_t *pde, vm_offset_t va) 4840 { 4841 struct rwlock *lock; 4842 boolean_t rv; 4843 4844 lock = NULL; 4845 rv = pmap_demote_l3e_locked(pmap, pde, va, &lock); 4846 if (lock != NULL) 4847 rw_wunlock(lock); 4848 return (rv); 4849 } 4850 4851 static boolean_t 4852 pmap_demote_l3e_locked(pmap_t pmap, pml3_entry_t *l3e, vm_offset_t va, 4853 struct rwlock **lockp) 4854 { 4855 pml3_entry_t oldpde; 4856 pt_entry_t *firstpte; 4857 vm_paddr_t mptepa; 4858 vm_page_t mpte; 4859 struct spglist free; 4860 vm_offset_t sva; 4861 4862 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 4863 oldpde = be64toh(*l3e); 4864 KASSERT((oldpde & (RPTE_LEAF | PG_V)) == (RPTE_LEAF | PG_V), 4865 ("pmap_demote_l3e: oldpde is missing RPTE_LEAF and/or PG_V %lx", 4866 oldpde)); 4867 if ((oldpde & PG_A) == 0 || (mpte = pmap_remove_pt_page(pmap, va)) == 4868 NULL) { 4869 KASSERT((oldpde & PG_W) == 0, 4870 ("pmap_demote_l3e: page table page for a wired mapping" 4871 " is missing")); 4872 4873 /* 4874 * Invalidate the 2MB page mapping and return "failure" if the 4875 * mapping was never accessed or the allocation of the new 4876 * page table page fails. If the 2MB page mapping belongs to 4877 * the direct map region of the kernel's address space, then 4878 * the page allocation request specifies the highest possible 4879 * priority (VM_ALLOC_INTERRUPT). Otherwise, the priority is 4880 * normal. Page table pages are preallocated for every other 4881 * part of the kernel address space, so the direct map region 4882 * is the only part of the kernel address space that must be 4883 * handled here. 4884 */ 4885 if ((oldpde & PG_A) == 0 || (mpte = vm_page_alloc(NULL, 4886 pmap_l3e_pindex(va), (va >= DMAP_MIN_ADDRESS && va < 4887 DMAP_MAX_ADDRESS ? VM_ALLOC_INTERRUPT : VM_ALLOC_NORMAL) | 4888 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) { 4889 SLIST_INIT(&free); 4890 sva = trunc_2mpage(va); 4891 pmap_remove_l3e(pmap, l3e, sva, &free, lockp); 4892 pmap_invalidate_l3e_page(pmap, sva, oldpde); 4893 vm_page_free_pages_toq(&free, true); 4894 CTR2(KTR_PMAP, "pmap_demote_l3e: failure for va %#lx" 4895 " in pmap %p", va, pmap); 4896 return (FALSE); 4897 } 4898 if (va < VM_MAXUSER_ADDRESS) 4899 pmap_resident_count_inc(pmap, 1); 4900 } 4901 mptepa = VM_PAGE_TO_PHYS(mpte); 4902 firstpte = (pt_entry_t *)PHYS_TO_DMAP(mptepa); 4903 KASSERT((oldpde & PG_A) != 0, 4904 ("pmap_demote_l3e: oldpde is missing PG_A")); 4905 KASSERT((oldpde & (PG_M | PG_RW)) != PG_RW, 4906 ("pmap_demote_l3e: oldpde is missing PG_M")); 4907 4908 /* 4909 * If the page table page is new, initialize it. 4910 */ 4911 if (mpte->ref_count == 1) { 4912 mpte->ref_count = NPTEPG; 4913 pmap_fill_ptp(firstpte, oldpde); 4914 } 4915 4916 KASSERT((be64toh(*firstpte) & PG_FRAME) == (oldpde & PG_FRAME), 4917 ("pmap_demote_l3e: firstpte and newpte map different physical" 4918 " addresses")); 4919 4920 /* 4921 * If the mapping has changed attributes, update the page table 4922 * entries. 4923 */ 4924 if ((be64toh(*firstpte) & PG_PTE_PROMOTE) != (oldpde & PG_PTE_PROMOTE)) 4925 pmap_fill_ptp(firstpte, oldpde); 4926 4927 /* 4928 * The spare PV entries must be reserved prior to demoting the 4929 * mapping, that is, prior to changing the PDE. Otherwise, the state 4930 * of the PDE and the PV lists will be inconsistent, which can result 4931 * in reclaim_pv_chunk() attempting to remove a PV entry from the 4932 * wrong PV list and pmap_pv_demote_l3e() failing to find the expected 4933 * PV entry for the 2MB page mapping that is being demoted. 4934 */ 4935 if ((oldpde & PG_MANAGED) != 0) 4936 reserve_pv_entries(pmap, NPTEPG - 1, lockp); 4937 4938 /* 4939 * Demote the mapping. This pmap is locked. The old PDE has 4940 * PG_A set. If the old PDE has PG_RW set, it also has PG_M 4941 * set. Thus, there is no danger of a race with another 4942 * processor changing the setting of PG_A and/or PG_M between 4943 * the read above and the store below. 4944 */ 4945 pde_store(l3e, mptepa); 4946 ptesync(); 4947 /* 4948 * Demote the PV entry. 4949 */ 4950 if ((oldpde & PG_MANAGED) != 0) 4951 pmap_pv_demote_l3e(pmap, va, oldpde & PG_PS_FRAME, lockp); 4952 4953 atomic_add_long(&pmap_l3e_demotions, 1); 4954 CTR2(KTR_PMAP, "pmap_demote_l3e: success for va %#lx" 4955 " in pmap %p", va, pmap); 4956 return (TRUE); 4957 } 4958 4959 /* 4960 * pmap_remove_kernel_pde: Remove a kernel superpage mapping. 4961 */ 4962 static void 4963 pmap_remove_kernel_l3e(pmap_t pmap, pml3_entry_t *l3e, vm_offset_t va) 4964 { 4965 vm_paddr_t mptepa; 4966 vm_page_t mpte; 4967 4968 KASSERT(pmap == kernel_pmap, ("pmap %p is not kernel_pmap", pmap)); 4969 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 4970 mpte = pmap_remove_pt_page(pmap, va); 4971 if (mpte == NULL) 4972 panic("pmap_remove_kernel_pde: Missing pt page."); 4973 4974 mptepa = VM_PAGE_TO_PHYS(mpte); 4975 4976 /* 4977 * Initialize the page table page. 4978 */ 4979 pagezero(PHYS_TO_DMAP(mptepa)); 4980 4981 /* 4982 * Demote the mapping. 4983 */ 4984 pde_store(l3e, mptepa); 4985 ptesync(); 4986 } 4987 4988 /* 4989 * pmap_remove_l3e: do the things to unmap a superpage in a process 4990 */ 4991 static int 4992 pmap_remove_l3e(pmap_t pmap, pml3_entry_t *pdq, vm_offset_t sva, 4993 struct spglist *free, struct rwlock **lockp) 4994 { 4995 struct md_page *pvh; 4996 pml3_entry_t oldpde; 4997 vm_offset_t eva, va; 4998 vm_page_t m, mpte; 4999 5000 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 5001 KASSERT((sva & L3_PAGE_MASK) == 0, 5002 ("pmap_remove_l3e: sva is not 2mpage aligned")); 5003 oldpde = be64toh(pte_load_clear(pdq)); 5004 if (oldpde & PG_W) 5005 pmap->pm_stats.wired_count -= (L3_PAGE_SIZE / PAGE_SIZE); 5006 pmap_resident_count_dec(pmap, L3_PAGE_SIZE / PAGE_SIZE); 5007 if (oldpde & PG_MANAGED) { 5008 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, oldpde & PG_PS_FRAME); 5009 pvh = pa_to_pvh(oldpde & PG_PS_FRAME); 5010 pmap_pvh_free(pvh, pmap, sva); 5011 eva = sva + L3_PAGE_SIZE; 5012 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME); 5013 va < eva; va += PAGE_SIZE, m++) { 5014 if ((oldpde & (PG_M | PG_RW)) == (PG_M | PG_RW)) 5015 vm_page_dirty(m); 5016 if (oldpde & PG_A) 5017 vm_page_aflag_set(m, PGA_REFERENCED); 5018 if (TAILQ_EMPTY(&m->md.pv_list) && 5019 TAILQ_EMPTY(&pvh->pv_list)) 5020 vm_page_aflag_clear(m, PGA_WRITEABLE); 5021 } 5022 } 5023 if (pmap == kernel_pmap) { 5024 pmap_remove_kernel_l3e(pmap, pdq, sva); 5025 } else { 5026 mpte = pmap_remove_pt_page(pmap, sva); 5027 if (mpte != NULL) { 5028 pmap_resident_count_dec(pmap, 1); 5029 KASSERT(mpte->ref_count == NPTEPG, 5030 ("pmap_remove_l3e: pte page wire count error")); 5031 mpte->ref_count = 0; 5032 pmap_add_delayed_free_list(mpte, free, FALSE); 5033 } 5034 } 5035 return (pmap_unuse_pt(pmap, sva, be64toh(*pmap_pml2e(pmap, sva)), free)); 5036 } 5037 5038 /* 5039 * pmap_remove_pte: do the things to unmap a page in a process 5040 */ 5041 static int 5042 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va, 5043 pml3_entry_t ptepde, struct spglist *free, struct rwlock **lockp) 5044 { 5045 struct md_page *pvh; 5046 pt_entry_t oldpte; 5047 vm_page_t m; 5048 5049 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 5050 oldpte = be64toh(pte_load_clear(ptq)); 5051 if (oldpte & RPTE_WIRED) 5052 pmap->pm_stats.wired_count -= 1; 5053 pmap_resident_count_dec(pmap, 1); 5054 if (oldpte & RPTE_MANAGED) { 5055 m = PHYS_TO_VM_PAGE(oldpte & PG_FRAME); 5056 if ((oldpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) 5057 vm_page_dirty(m); 5058 if (oldpte & PG_A) 5059 vm_page_aflag_set(m, PGA_REFERENCED); 5060 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m); 5061 pmap_pvh_free(&m->md, pmap, va); 5062 if (TAILQ_EMPTY(&m->md.pv_list) && 5063 (m->flags & PG_FICTITIOUS) == 0) { 5064 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m)); 5065 if (TAILQ_EMPTY(&pvh->pv_list)) 5066 vm_page_aflag_clear(m, PGA_WRITEABLE); 5067 } 5068 } 5069 return (pmap_unuse_pt(pmap, va, ptepde, free)); 5070 } 5071 5072 /* 5073 * Remove a single page from a process address space 5074 */ 5075 static bool 5076 pmap_remove_page(pmap_t pmap, vm_offset_t va, pml3_entry_t *l3e, 5077 struct spglist *free) 5078 { 5079 struct rwlock *lock; 5080 pt_entry_t *pte; 5081 bool invalidate_all; 5082 5083 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 5084 if ((be64toh(*l3e) & RPTE_VALID) == 0) { 5085 return (false); 5086 } 5087 pte = pmap_l3e_to_pte(l3e, va); 5088 if ((be64toh(*pte) & RPTE_VALID) == 0) { 5089 return (false); 5090 } 5091 lock = NULL; 5092 5093 invalidate_all = pmap_remove_pte(pmap, pte, va, be64toh(*l3e), free, &lock); 5094 if (lock != NULL) 5095 rw_wunlock(lock); 5096 if (!invalidate_all) 5097 pmap_invalidate_page(pmap, va); 5098 return (invalidate_all); 5099 } 5100 5101 /* 5102 * Removes the specified range of addresses from the page table page. 5103 */ 5104 static bool 5105 pmap_remove_ptes(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, 5106 pml3_entry_t *l3e, struct spglist *free, struct rwlock **lockp) 5107 { 5108 pt_entry_t *pte; 5109 vm_offset_t va; 5110 bool anyvalid; 5111 5112 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 5113 anyvalid = false; 5114 va = eva; 5115 for (pte = pmap_l3e_to_pte(l3e, sva); sva != eva; pte++, 5116 sva += PAGE_SIZE) { 5117 MPASS(pte == pmap_pte(pmap, sva)); 5118 if (*pte == 0) { 5119 if (va != eva) { 5120 anyvalid = true; 5121 va = eva; 5122 } 5123 continue; 5124 } 5125 if (va == eva) 5126 va = sva; 5127 if (pmap_remove_pte(pmap, pte, sva, be64toh(*l3e), free, lockp)) { 5128 anyvalid = true; 5129 sva += PAGE_SIZE; 5130 break; 5131 } 5132 } 5133 if (anyvalid) 5134 pmap_invalidate_all(pmap); 5135 else if (va != eva) 5136 pmap_invalidate_range(pmap, va, sva); 5137 return (anyvalid); 5138 } 5139 5140 void 5141 mmu_radix_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva) 5142 { 5143 struct rwlock *lock; 5144 vm_offset_t va_next; 5145 pml1_entry_t *l1e; 5146 pml2_entry_t *l2e; 5147 pml3_entry_t ptpaddr, *l3e; 5148 struct spglist free; 5149 bool anyvalid; 5150 5151 CTR4(KTR_PMAP, "%s(%p, %#x, %#x)", __func__, pmap, sva, eva); 5152 5153 /* 5154 * Perform an unsynchronized read. This is, however, safe. 5155 */ 5156 if (pmap->pm_stats.resident_count == 0) 5157 return; 5158 5159 anyvalid = false; 5160 SLIST_INIT(&free); 5161 5162 /* XXX something fishy here */ 5163 sva = (sva + PAGE_MASK) & ~PAGE_MASK; 5164 eva = (eva + PAGE_MASK) & ~PAGE_MASK; 5165 5166 PMAP_LOCK(pmap); 5167 5168 /* 5169 * special handling of removing one page. a very 5170 * common operation and easy to short circuit some 5171 * code. 5172 */ 5173 if (sva + PAGE_SIZE == eva) { 5174 l3e = pmap_pml3e(pmap, sva); 5175 if (l3e && (be64toh(*l3e) & RPTE_LEAF) == 0) { 5176 anyvalid = pmap_remove_page(pmap, sva, l3e, &free); 5177 goto out; 5178 } 5179 } 5180 5181 lock = NULL; 5182 for (; sva < eva; sva = va_next) { 5183 if (pmap->pm_stats.resident_count == 0) 5184 break; 5185 l1e = pmap_pml1e(pmap, sva); 5186 if (l1e == NULL || (be64toh(*l1e) & PG_V) == 0) { 5187 va_next = (sva + L1_PAGE_SIZE) & ~L1_PAGE_MASK; 5188 if (va_next < sva) 5189 va_next = eva; 5190 continue; 5191 } 5192 5193 l2e = pmap_l1e_to_l2e(l1e, sva); 5194 if (l2e == NULL || (be64toh(*l2e) & PG_V) == 0) { 5195 va_next = (sva + L2_PAGE_SIZE) & ~L2_PAGE_MASK; 5196 if (va_next < sva) 5197 va_next = eva; 5198 continue; 5199 } 5200 5201 /* 5202 * Calculate index for next page table. 5203 */ 5204 va_next = (sva + L3_PAGE_SIZE) & ~L3_PAGE_MASK; 5205 if (va_next < sva) 5206 va_next = eva; 5207 5208 l3e = pmap_l2e_to_l3e(l2e, sva); 5209 ptpaddr = be64toh(*l3e); 5210 5211 /* 5212 * Weed out invalid mappings. 5213 */ 5214 if (ptpaddr == 0) 5215 continue; 5216 5217 /* 5218 * Check for large page. 5219 */ 5220 if ((ptpaddr & RPTE_LEAF) != 0) { 5221 /* 5222 * Are we removing the entire large page? If not, 5223 * demote the mapping and fall through. 5224 */ 5225 if (sva + L3_PAGE_SIZE == va_next && eva >= va_next) { 5226 pmap_remove_l3e(pmap, l3e, sva, &free, &lock); 5227 continue; 5228 } else if (!pmap_demote_l3e_locked(pmap, l3e, sva, 5229 &lock)) { 5230 /* The large page mapping was destroyed. */ 5231 continue; 5232 } else 5233 ptpaddr = be64toh(*l3e); 5234 } 5235 5236 /* 5237 * Limit our scan to either the end of the va represented 5238 * by the current page table page, or to the end of the 5239 * range being removed. 5240 */ 5241 if (va_next > eva) 5242 va_next = eva; 5243 5244 if (pmap_remove_ptes(pmap, sva, va_next, l3e, &free, &lock)) 5245 anyvalid = true; 5246 } 5247 if (lock != NULL) 5248 rw_wunlock(lock); 5249 out: 5250 if (anyvalid) 5251 pmap_invalidate_all(pmap); 5252 PMAP_UNLOCK(pmap); 5253 vm_page_free_pages_toq(&free, true); 5254 } 5255 5256 void 5257 mmu_radix_remove_all(vm_page_t m) 5258 { 5259 struct md_page *pvh; 5260 pv_entry_t pv; 5261 pmap_t pmap; 5262 struct rwlock *lock; 5263 pt_entry_t *pte, tpte; 5264 pml3_entry_t *l3e; 5265 vm_offset_t va; 5266 struct spglist free; 5267 int pvh_gen, md_gen; 5268 5269 CTR2(KTR_PMAP, "%s(%p)", __func__, m); 5270 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 5271 ("pmap_remove_all: page %p is not managed", m)); 5272 SLIST_INIT(&free); 5273 lock = VM_PAGE_TO_PV_LIST_LOCK(m); 5274 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : 5275 pa_to_pvh(VM_PAGE_TO_PHYS(m)); 5276 retry: 5277 rw_wlock(lock); 5278 while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) { 5279 pmap = PV_PMAP(pv); 5280 if (!PMAP_TRYLOCK(pmap)) { 5281 pvh_gen = pvh->pv_gen; 5282 rw_wunlock(lock); 5283 PMAP_LOCK(pmap); 5284 rw_wlock(lock); 5285 if (pvh_gen != pvh->pv_gen) { 5286 rw_wunlock(lock); 5287 PMAP_UNLOCK(pmap); 5288 goto retry; 5289 } 5290 } 5291 va = pv->pv_va; 5292 l3e = pmap_pml3e(pmap, va); 5293 (void)pmap_demote_l3e_locked(pmap, l3e, va, &lock); 5294 PMAP_UNLOCK(pmap); 5295 } 5296 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) { 5297 pmap = PV_PMAP(pv); 5298 if (!PMAP_TRYLOCK(pmap)) { 5299 pvh_gen = pvh->pv_gen; 5300 md_gen = m->md.pv_gen; 5301 rw_wunlock(lock); 5302 PMAP_LOCK(pmap); 5303 rw_wlock(lock); 5304 if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) { 5305 rw_wunlock(lock); 5306 PMAP_UNLOCK(pmap); 5307 goto retry; 5308 } 5309 } 5310 pmap_resident_count_dec(pmap, 1); 5311 l3e = pmap_pml3e(pmap, pv->pv_va); 5312 KASSERT((be64toh(*l3e) & RPTE_LEAF) == 0, ("pmap_remove_all: found" 5313 " a 2mpage in page %p's pv list", m)); 5314 pte = pmap_l3e_to_pte(l3e, pv->pv_va); 5315 tpte = be64toh(pte_load_clear(pte)); 5316 if (tpte & PG_W) 5317 pmap->pm_stats.wired_count--; 5318 if (tpte & PG_A) 5319 vm_page_aflag_set(m, PGA_REFERENCED); 5320 5321 /* 5322 * Update the vm_page_t clean and reference bits. 5323 */ 5324 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) 5325 vm_page_dirty(m); 5326 pmap_unuse_pt(pmap, pv->pv_va, be64toh(*l3e), &free); 5327 pmap_invalidate_page(pmap, pv->pv_va); 5328 TAILQ_REMOVE(&m->md.pv_list, pv, pv_link); 5329 m->md.pv_gen++; 5330 free_pv_entry(pmap, pv); 5331 PMAP_UNLOCK(pmap); 5332 } 5333 vm_page_aflag_clear(m, PGA_WRITEABLE); 5334 rw_wunlock(lock); 5335 vm_page_free_pages_toq(&free, true); 5336 } 5337 5338 /* 5339 * Destroy all managed, non-wired mappings in the given user-space 5340 * pmap. This pmap cannot be active on any processor besides the 5341 * caller. 5342 * 5343 * This function cannot be applied to the kernel pmap. Moreover, it 5344 * is not intended for general use. It is only to be used during 5345 * process termination. Consequently, it can be implemented in ways 5346 * that make it faster than pmap_remove(). First, it can more quickly 5347 * destroy mappings by iterating over the pmap's collection of PV 5348 * entries, rather than searching the page table. Second, it doesn't 5349 * have to test and clear the page table entries atomically, because 5350 * no processor is currently accessing the user address space. In 5351 * particular, a page table entry's dirty bit won't change state once 5352 * this function starts. 5353 * 5354 * Although this function destroys all of the pmap's managed, 5355 * non-wired mappings, it can delay and batch the invalidation of TLB 5356 * entries without calling pmap_delayed_invl_started() and 5357 * pmap_delayed_invl_finished(). Because the pmap is not active on 5358 * any other processor, none of these TLB entries will ever be used 5359 * before their eventual invalidation. Consequently, there is no need 5360 * for either pmap_remove_all() or pmap_remove_write() to wait for 5361 * that eventual TLB invalidation. 5362 */ 5363 5364 void 5365 mmu_radix_remove_pages(pmap_t pmap) 5366 { 5367 5368 CTR2(KTR_PMAP, "%s(%p)", __func__, pmap); 5369 pml3_entry_t ptel3e; 5370 pt_entry_t *pte, tpte; 5371 struct spglist free; 5372 vm_page_t m, mpte, mt; 5373 pv_entry_t pv; 5374 struct md_page *pvh; 5375 struct pv_chunk *pc, *npc; 5376 struct rwlock *lock; 5377 int64_t bit; 5378 uint64_t inuse, bitmask; 5379 int allfree, field, freed, idx; 5380 boolean_t superpage; 5381 vm_paddr_t pa; 5382 5383 /* 5384 * Assert that the given pmap is only active on the current 5385 * CPU. Unfortunately, we cannot block another CPU from 5386 * activating the pmap while this function is executing. 5387 */ 5388 KASSERT(pmap->pm_pid == mfspr(SPR_PID), 5389 ("non-current asid %lu - expected %lu", pmap->pm_pid, 5390 mfspr(SPR_PID))); 5391 5392 lock = NULL; 5393 5394 SLIST_INIT(&free); 5395 PMAP_LOCK(pmap); 5396 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) { 5397 allfree = 1; 5398 freed = 0; 5399 for (field = 0; field < _NPCM; field++) { 5400 inuse = ~pc->pc_map[field] & pc_freemask[field]; 5401 while (inuse != 0) { 5402 bit = cnttzd(inuse); 5403 bitmask = 1UL << bit; 5404 idx = field * 64 + bit; 5405 pv = &pc->pc_pventry[idx]; 5406 inuse &= ~bitmask; 5407 5408 pte = pmap_pml2e(pmap, pv->pv_va); 5409 ptel3e = be64toh(*pte); 5410 pte = pmap_l2e_to_l3e(pte, pv->pv_va); 5411 tpte = be64toh(*pte); 5412 if ((tpte & (RPTE_LEAF | PG_V)) == PG_V) { 5413 superpage = FALSE; 5414 ptel3e = tpte; 5415 pte = (pt_entry_t *)PHYS_TO_DMAP(tpte & 5416 PG_FRAME); 5417 pte = &pte[pmap_pte_index(pv->pv_va)]; 5418 tpte = be64toh(*pte); 5419 } else { 5420 /* 5421 * Keep track whether 'tpte' is a 5422 * superpage explicitly instead of 5423 * relying on RPTE_LEAF being set. 5424 * 5425 * This is because RPTE_LEAF is numerically 5426 * identical to PG_PTE_PAT and thus a 5427 * regular page could be mistaken for 5428 * a superpage. 5429 */ 5430 superpage = TRUE; 5431 } 5432 5433 if ((tpte & PG_V) == 0) { 5434 panic("bad pte va %lx pte %lx", 5435 pv->pv_va, tpte); 5436 } 5437 5438 /* 5439 * We cannot remove wired pages from a process' mapping at this time 5440 */ 5441 if (tpte & PG_W) { 5442 allfree = 0; 5443 continue; 5444 } 5445 5446 if (superpage) 5447 pa = tpte & PG_PS_FRAME; 5448 else 5449 pa = tpte & PG_FRAME; 5450 5451 m = PHYS_TO_VM_PAGE(pa); 5452 KASSERT(m->phys_addr == pa, 5453 ("vm_page_t %p phys_addr mismatch %016jx %016jx", 5454 m, (uintmax_t)m->phys_addr, 5455 (uintmax_t)tpte)); 5456 5457 KASSERT((m->flags & PG_FICTITIOUS) != 0 || 5458 m < &vm_page_array[vm_page_array_size], 5459 ("pmap_remove_pages: bad tpte %#jx", 5460 (uintmax_t)tpte)); 5461 5462 pte_clear(pte); 5463 5464 /* 5465 * Update the vm_page_t clean/reference bits. 5466 */ 5467 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) { 5468 if (superpage) { 5469 for (mt = m; mt < &m[L3_PAGE_SIZE / PAGE_SIZE]; mt++) 5470 vm_page_dirty(mt); 5471 } else 5472 vm_page_dirty(m); 5473 } 5474 5475 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(&lock, m); 5476 5477 /* Mark free */ 5478 pc->pc_map[field] |= bitmask; 5479 if (superpage) { 5480 pmap_resident_count_dec(pmap, L3_PAGE_SIZE / PAGE_SIZE); 5481 pvh = pa_to_pvh(tpte & PG_PS_FRAME); 5482 TAILQ_REMOVE(&pvh->pv_list, pv, pv_link); 5483 pvh->pv_gen++; 5484 if (TAILQ_EMPTY(&pvh->pv_list)) { 5485 for (mt = m; mt < &m[L3_PAGE_SIZE / PAGE_SIZE]; mt++) 5486 if ((mt->a.flags & PGA_WRITEABLE) != 0 && 5487 TAILQ_EMPTY(&mt->md.pv_list)) 5488 vm_page_aflag_clear(mt, PGA_WRITEABLE); 5489 } 5490 mpte = pmap_remove_pt_page(pmap, pv->pv_va); 5491 if (mpte != NULL) { 5492 pmap_resident_count_dec(pmap, 1); 5493 KASSERT(mpte->ref_count == NPTEPG, 5494 ("pmap_remove_pages: pte page wire count error")); 5495 mpte->ref_count = 0; 5496 pmap_add_delayed_free_list(mpte, &free, FALSE); 5497 } 5498 } else { 5499 pmap_resident_count_dec(pmap, 1); 5500 #ifdef VERBOSE_PV 5501 printf("freeing pv (%p, %p)\n", 5502 pmap, pv); 5503 #endif 5504 TAILQ_REMOVE(&m->md.pv_list, pv, pv_link); 5505 m->md.pv_gen++; 5506 if ((m->a.flags & PGA_WRITEABLE) != 0 && 5507 TAILQ_EMPTY(&m->md.pv_list) && 5508 (m->flags & PG_FICTITIOUS) == 0) { 5509 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m)); 5510 if (TAILQ_EMPTY(&pvh->pv_list)) 5511 vm_page_aflag_clear(m, PGA_WRITEABLE); 5512 } 5513 } 5514 pmap_unuse_pt(pmap, pv->pv_va, ptel3e, &free); 5515 freed++; 5516 } 5517 } 5518 PV_STAT(atomic_add_long(&pv_entry_frees, freed)); 5519 PV_STAT(atomic_add_int(&pv_entry_spare, freed)); 5520 PV_STAT(atomic_subtract_long(&pv_entry_count, freed)); 5521 if (allfree) { 5522 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list); 5523 free_pv_chunk(pc); 5524 } 5525 } 5526 if (lock != NULL) 5527 rw_wunlock(lock); 5528 pmap_invalidate_all(pmap); 5529 PMAP_UNLOCK(pmap); 5530 vm_page_free_pages_toq(&free, true); 5531 } 5532 5533 void 5534 mmu_radix_remove_write(vm_page_t m) 5535 { 5536 struct md_page *pvh; 5537 pmap_t pmap; 5538 struct rwlock *lock; 5539 pv_entry_t next_pv, pv; 5540 pml3_entry_t *l3e; 5541 pt_entry_t oldpte, *pte; 5542 int pvh_gen, md_gen; 5543 5544 CTR2(KTR_PMAP, "%s(%p)", __func__, m); 5545 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 5546 ("pmap_remove_write: page %p is not managed", m)); 5547 vm_page_assert_busied(m); 5548 5549 if (!pmap_page_is_write_mapped(m)) 5550 return; 5551 lock = VM_PAGE_TO_PV_LIST_LOCK(m); 5552 pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : 5553 pa_to_pvh(VM_PAGE_TO_PHYS(m)); 5554 retry_pv_loop: 5555 rw_wlock(lock); 5556 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_link, next_pv) { 5557 pmap = PV_PMAP(pv); 5558 if (!PMAP_TRYLOCK(pmap)) { 5559 pvh_gen = pvh->pv_gen; 5560 rw_wunlock(lock); 5561 PMAP_LOCK(pmap); 5562 rw_wlock(lock); 5563 if (pvh_gen != pvh->pv_gen) { 5564 PMAP_UNLOCK(pmap); 5565 rw_wunlock(lock); 5566 goto retry_pv_loop; 5567 } 5568 } 5569 l3e = pmap_pml3e(pmap, pv->pv_va); 5570 if ((be64toh(*l3e) & PG_RW) != 0) 5571 (void)pmap_demote_l3e_locked(pmap, l3e, pv->pv_va, &lock); 5572 KASSERT(lock == VM_PAGE_TO_PV_LIST_LOCK(m), 5573 ("inconsistent pv lock %p %p for page %p", 5574 lock, VM_PAGE_TO_PV_LIST_LOCK(m), m)); 5575 PMAP_UNLOCK(pmap); 5576 } 5577 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) { 5578 pmap = PV_PMAP(pv); 5579 if (!PMAP_TRYLOCK(pmap)) { 5580 pvh_gen = pvh->pv_gen; 5581 md_gen = m->md.pv_gen; 5582 rw_wunlock(lock); 5583 PMAP_LOCK(pmap); 5584 rw_wlock(lock); 5585 if (pvh_gen != pvh->pv_gen || 5586 md_gen != m->md.pv_gen) { 5587 PMAP_UNLOCK(pmap); 5588 rw_wunlock(lock); 5589 goto retry_pv_loop; 5590 } 5591 } 5592 l3e = pmap_pml3e(pmap, pv->pv_va); 5593 KASSERT((be64toh(*l3e) & RPTE_LEAF) == 0, 5594 ("pmap_remove_write: found a 2mpage in page %p's pv list", 5595 m)); 5596 pte = pmap_l3e_to_pte(l3e, pv->pv_va); 5597 retry: 5598 oldpte = be64toh(*pte); 5599 if (oldpte & PG_RW) { 5600 if (!atomic_cmpset_long(pte, htobe64(oldpte), 5601 htobe64((oldpte | RPTE_EAA_R) & ~(PG_RW | PG_M)))) 5602 goto retry; 5603 if ((oldpte & PG_M) != 0) 5604 vm_page_dirty(m); 5605 pmap_invalidate_page(pmap, pv->pv_va); 5606 } 5607 PMAP_UNLOCK(pmap); 5608 } 5609 rw_wunlock(lock); 5610 vm_page_aflag_clear(m, PGA_WRITEABLE); 5611 } 5612 5613 /* 5614 * Clear the wired attribute from the mappings for the specified range of 5615 * addresses in the given pmap. Every valid mapping within that range 5616 * must have the wired attribute set. In contrast, invalid mappings 5617 * cannot have the wired attribute set, so they are ignored. 5618 * 5619 * The wired attribute of the page table entry is not a hardware 5620 * feature, so there is no need to invalidate any TLB entries. 5621 * Since pmap_demote_l3e() for the wired entry must never fail, 5622 * pmap_delayed_invl_started()/finished() calls around the 5623 * function are not needed. 5624 */ 5625 void 5626 mmu_radix_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva) 5627 { 5628 vm_offset_t va_next; 5629 pml1_entry_t *l1e; 5630 pml2_entry_t *l2e; 5631 pml3_entry_t *l3e; 5632 pt_entry_t *pte; 5633 5634 CTR4(KTR_PMAP, "%s(%p, %#x, %#x)", __func__, pmap, sva, eva); 5635 PMAP_LOCK(pmap); 5636 for (; sva < eva; sva = va_next) { 5637 l1e = pmap_pml1e(pmap, sva); 5638 if ((be64toh(*l1e) & PG_V) == 0) { 5639 va_next = (sva + L1_PAGE_SIZE) & ~L1_PAGE_MASK; 5640 if (va_next < sva) 5641 va_next = eva; 5642 continue; 5643 } 5644 l2e = pmap_l1e_to_l2e(l1e, sva); 5645 if ((be64toh(*l2e) & PG_V) == 0) { 5646 va_next = (sva + L2_PAGE_SIZE) & ~L2_PAGE_MASK; 5647 if (va_next < sva) 5648 va_next = eva; 5649 continue; 5650 } 5651 va_next = (sva + L3_PAGE_SIZE) & ~L3_PAGE_MASK; 5652 if (va_next < sva) 5653 va_next = eva; 5654 l3e = pmap_l2e_to_l3e(l2e, sva); 5655 if ((be64toh(*l3e) & PG_V) == 0) 5656 continue; 5657 if ((be64toh(*l3e) & RPTE_LEAF) != 0) { 5658 if ((be64toh(*l3e) & PG_W) == 0) 5659 panic("pmap_unwire: pde %#jx is missing PG_W", 5660 (uintmax_t)(be64toh(*l3e))); 5661 5662 /* 5663 * Are we unwiring the entire large page? If not, 5664 * demote the mapping and fall through. 5665 */ 5666 if (sva + L3_PAGE_SIZE == va_next && eva >= va_next) { 5667 atomic_clear_long(l3e, htobe64(PG_W)); 5668 pmap->pm_stats.wired_count -= L3_PAGE_SIZE / 5669 PAGE_SIZE; 5670 continue; 5671 } else if (!pmap_demote_l3e(pmap, l3e, sva)) 5672 panic("pmap_unwire: demotion failed"); 5673 } 5674 if (va_next > eva) 5675 va_next = eva; 5676 for (pte = pmap_l3e_to_pte(l3e, sva); sva != va_next; pte++, 5677 sva += PAGE_SIZE) { 5678 MPASS(pte == pmap_pte(pmap, sva)); 5679 if ((be64toh(*pte) & PG_V) == 0) 5680 continue; 5681 if ((be64toh(*pte) & PG_W) == 0) 5682 panic("pmap_unwire: pte %#jx is missing PG_W", 5683 (uintmax_t)(be64toh(*pte))); 5684 5685 /* 5686 * PG_W must be cleared atomically. Although the pmap 5687 * lock synchronizes access to PG_W, another processor 5688 * could be setting PG_M and/or PG_A concurrently. 5689 */ 5690 atomic_clear_long(pte, htobe64(PG_W)); 5691 pmap->pm_stats.wired_count--; 5692 } 5693 } 5694 PMAP_UNLOCK(pmap); 5695 } 5696 5697 void 5698 mmu_radix_zero_page(vm_page_t m) 5699 { 5700 vm_offset_t addr; 5701 5702 CTR2(KTR_PMAP, "%s(%p)", __func__, m); 5703 addr = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)); 5704 pagezero(addr); 5705 } 5706 5707 void 5708 mmu_radix_zero_page_area(vm_page_t m, int off, int size) 5709 { 5710 caddr_t addr; 5711 5712 CTR4(KTR_PMAP, "%s(%p, %d, %d)", __func__, m, off, size); 5713 MPASS(off + size <= PAGE_SIZE); 5714 addr = (caddr_t)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)); 5715 memset(addr + off, 0, size); 5716 } 5717 5718 static int 5719 mmu_radix_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa) 5720 { 5721 pml3_entry_t *l3ep; 5722 pt_entry_t pte; 5723 vm_paddr_t pa; 5724 int val; 5725 5726 CTR3(KTR_PMAP, "%s(%p, %#x)", __func__, pmap, addr); 5727 PMAP_LOCK(pmap); 5728 5729 l3ep = pmap_pml3e(pmap, addr); 5730 if (l3ep != NULL && (be64toh(*l3ep) & PG_V)) { 5731 if (be64toh(*l3ep) & RPTE_LEAF) { 5732 pte = be64toh(*l3ep); 5733 /* Compute the physical address of the 4KB page. */ 5734 pa = ((be64toh(*l3ep) & PG_PS_FRAME) | (addr & L3_PAGE_MASK)) & 5735 PG_FRAME; 5736 val = MINCORE_PSIND(1); 5737 } else { 5738 /* Native endian PTE, do not pass to functions */ 5739 pte = be64toh(*pmap_l3e_to_pte(l3ep, addr)); 5740 pa = pte & PG_FRAME; 5741 val = 0; 5742 } 5743 } else { 5744 pte = 0; 5745 pa = 0; 5746 val = 0; 5747 } 5748 if ((pte & PG_V) != 0) { 5749 val |= MINCORE_INCORE; 5750 if ((pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) 5751 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER; 5752 if ((pte & PG_A) != 0) 5753 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER; 5754 } 5755 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) != 5756 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) && 5757 (pte & (PG_MANAGED | PG_V)) == (PG_MANAGED | PG_V)) { 5758 *locked_pa = pa; 5759 } 5760 PMAP_UNLOCK(pmap); 5761 return (val); 5762 } 5763 5764 void 5765 mmu_radix_activate(struct thread *td) 5766 { 5767 pmap_t pmap; 5768 uint32_t curpid; 5769 5770 CTR2(KTR_PMAP, "%s(%p)", __func__, td); 5771 critical_enter(); 5772 pmap = vmspace_pmap(td->td_proc->p_vmspace); 5773 curpid = mfspr(SPR_PID); 5774 if (pmap->pm_pid > isa3_base_pid && 5775 curpid != pmap->pm_pid) { 5776 mmu_radix_pid_set(pmap); 5777 } 5778 critical_exit(); 5779 } 5780 5781 /* 5782 * Increase the starting virtual address of the given mapping if a 5783 * different alignment might result in more superpage mappings. 5784 */ 5785 void 5786 mmu_radix_align_superpage(vm_object_t object, vm_ooffset_t offset, 5787 vm_offset_t *addr, vm_size_t size) 5788 { 5789 5790 CTR5(KTR_PMAP, "%s(%p, %#x, %p, %#x)", __func__, object, offset, addr, 5791 size); 5792 vm_offset_t superpage_offset; 5793 5794 if (size < L3_PAGE_SIZE) 5795 return; 5796 if (object != NULL && (object->flags & OBJ_COLORED) != 0) 5797 offset += ptoa(object->pg_color); 5798 superpage_offset = offset & L3_PAGE_MASK; 5799 if (size - ((L3_PAGE_SIZE - superpage_offset) & L3_PAGE_MASK) < L3_PAGE_SIZE || 5800 (*addr & L3_PAGE_MASK) == superpage_offset) 5801 return; 5802 if ((*addr & L3_PAGE_MASK) < superpage_offset) 5803 *addr = (*addr & ~L3_PAGE_MASK) + superpage_offset; 5804 else 5805 *addr = ((*addr + L3_PAGE_MASK) & ~L3_PAGE_MASK) + superpage_offset; 5806 } 5807 5808 static void * 5809 mmu_radix_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t attr) 5810 { 5811 vm_offset_t va, tmpva, ppa, offset; 5812 5813 ppa = trunc_page(pa); 5814 offset = pa & PAGE_MASK; 5815 size = roundup2(offset + size, PAGE_SIZE); 5816 if (pa < powerpc_ptob(Maxmem)) 5817 panic("bad pa: %#lx less than Maxmem %#lx\n", 5818 pa, powerpc_ptob(Maxmem)); 5819 va = kva_alloc(size); 5820 if (bootverbose) 5821 printf("%s(%#lx, %lu, %d)\n", __func__, pa, size, attr); 5822 KASSERT(size > 0, ("%s(%#lx, %lu, %d)", __func__, pa, size, attr)); 5823 5824 if (!va) 5825 panic("%s: Couldn't alloc kernel virtual memory", __func__); 5826 5827 for (tmpva = va; size > 0;) { 5828 mmu_radix_kenter_attr(tmpva, ppa, attr); 5829 size -= PAGE_SIZE; 5830 tmpva += PAGE_SIZE; 5831 ppa += PAGE_SIZE; 5832 } 5833 ptesync(); 5834 5835 return ((void *)(va + offset)); 5836 } 5837 5838 static void * 5839 mmu_radix_mapdev(vm_paddr_t pa, vm_size_t size) 5840 { 5841 5842 CTR3(KTR_PMAP, "%s(%#x, %#x)", __func__, pa, size); 5843 5844 return (mmu_radix_mapdev_attr(pa, size, VM_MEMATTR_DEFAULT)); 5845 } 5846 5847 void 5848 mmu_radix_page_set_memattr(vm_page_t m, vm_memattr_t ma) 5849 { 5850 5851 CTR3(KTR_PMAP, "%s(%p, %#x)", __func__, m, ma); 5852 m->md.mdpg_cache_attrs = ma; 5853 5854 /* 5855 * If "m" is a normal page, update its direct mapping. This update 5856 * can be relied upon to perform any cache operations that are 5857 * required for data coherence. 5858 */ 5859 if ((m->flags & PG_FICTITIOUS) == 0 && 5860 mmu_radix_change_attr(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)), 5861 PAGE_SIZE, m->md.mdpg_cache_attrs)) 5862 panic("memory attribute change on the direct map failed"); 5863 } 5864 5865 static void 5866 mmu_radix_unmapdev(vm_offset_t va, vm_size_t size) 5867 { 5868 vm_offset_t offset; 5869 5870 CTR3(KTR_PMAP, "%s(%#x, %#x)", __func__, va, size); 5871 /* If we gave a direct map region in pmap_mapdev, do nothing */ 5872 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) 5873 return; 5874 5875 offset = va & PAGE_MASK; 5876 size = round_page(offset + size); 5877 va = trunc_page(va); 5878 5879 if (pmap_initialized) { 5880 mmu_radix_qremove(va, atop(size)); 5881 kva_free(va, size); 5882 } 5883 } 5884 5885 static __inline void 5886 pmap_pte_attr(pt_entry_t *pte, uint64_t cache_bits, uint64_t mask) 5887 { 5888 uint64_t opte, npte; 5889 5890 /* 5891 * The cache mode bits are all in the low 32-bits of the 5892 * PTE, so we can just spin on updating the low 32-bits. 5893 */ 5894 do { 5895 opte = be64toh(*pte); 5896 npte = opte & ~mask; 5897 npte |= cache_bits; 5898 } while (npte != opte && !atomic_cmpset_long(pte, htobe64(opte), htobe64(npte))); 5899 } 5900 5901 /* 5902 * Tries to demote a 1GB page mapping. 5903 */ 5904 static boolean_t 5905 pmap_demote_l2e(pmap_t pmap, pml2_entry_t *l2e, vm_offset_t va) 5906 { 5907 pml2_entry_t oldpdpe; 5908 pml3_entry_t *firstpde, newpde, *pde; 5909 vm_paddr_t pdpgpa; 5910 vm_page_t pdpg; 5911 5912 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 5913 oldpdpe = be64toh(*l2e); 5914 KASSERT((oldpdpe & (RPTE_LEAF | PG_V)) == (RPTE_LEAF | PG_V), 5915 ("pmap_demote_pdpe: oldpdpe is missing PG_PS and/or PG_V")); 5916 pdpg = vm_page_alloc(NULL, va >> L2_PAGE_SIZE_SHIFT, 5917 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED); 5918 if (pdpg == NULL) { 5919 CTR2(KTR_PMAP, "pmap_demote_pdpe: failure for va %#lx" 5920 " in pmap %p", va, pmap); 5921 return (FALSE); 5922 } 5923 pdpgpa = VM_PAGE_TO_PHYS(pdpg); 5924 firstpde = (pml3_entry_t *)PHYS_TO_DMAP(pdpgpa); 5925 KASSERT((oldpdpe & PG_A) != 0, 5926 ("pmap_demote_pdpe: oldpdpe is missing PG_A")); 5927 KASSERT((oldpdpe & (PG_M | PG_RW)) != PG_RW, 5928 ("pmap_demote_pdpe: oldpdpe is missing PG_M")); 5929 newpde = oldpdpe; 5930 5931 /* 5932 * Initialize the page directory page. 5933 */ 5934 for (pde = firstpde; pde < firstpde + NPDEPG; pde++) { 5935 *pde = htobe64(newpde); 5936 newpde += L3_PAGE_SIZE; 5937 } 5938 5939 /* 5940 * Demote the mapping. 5941 */ 5942 pde_store(l2e, pdpgpa); 5943 5944 /* 5945 * Flush PWC --- XXX revisit 5946 */ 5947 pmap_invalidate_all(pmap); 5948 5949 pmap_l2e_demotions++; 5950 CTR2(KTR_PMAP, "pmap_demote_pdpe: success for va %#lx" 5951 " in pmap %p", va, pmap); 5952 return (TRUE); 5953 } 5954 5955 vm_paddr_t 5956 mmu_radix_kextract(vm_offset_t va) 5957 { 5958 pml3_entry_t l3e; 5959 vm_paddr_t pa; 5960 5961 CTR2(KTR_PMAP, "%s(%#x)", __func__, va); 5962 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) { 5963 pa = DMAP_TO_PHYS(va); 5964 } else { 5965 /* Big-endian PTE on stack */ 5966 l3e = *pmap_pml3e(kernel_pmap, va); 5967 if (be64toh(l3e) & RPTE_LEAF) { 5968 pa = (be64toh(l3e) & PG_PS_FRAME) | (va & L3_PAGE_MASK); 5969 pa |= (va & L3_PAGE_MASK); 5970 } else { 5971 /* 5972 * Beware of a concurrent promotion that changes the 5973 * PDE at this point! For example, vtopte() must not 5974 * be used to access the PTE because it would use the 5975 * new PDE. It is, however, safe to use the old PDE 5976 * because the page table page is preserved by the 5977 * promotion. 5978 */ 5979 pa = be64toh(*pmap_l3e_to_pte(&l3e, va)); 5980 pa = (pa & PG_FRAME) | (va & PAGE_MASK); 5981 pa |= (va & PAGE_MASK); 5982 } 5983 } 5984 return (pa); 5985 } 5986 5987 static pt_entry_t 5988 mmu_radix_calc_wimg(vm_paddr_t pa, vm_memattr_t ma) 5989 { 5990 5991 if (ma != VM_MEMATTR_DEFAULT) { 5992 return pmap_cache_bits(ma); 5993 } 5994 5995 /* 5996 * Assume the page is cache inhibited and access is guarded unless 5997 * it's in our available memory array. 5998 */ 5999 for (int i = 0; i < pregions_sz; i++) { 6000 if ((pa >= pregions[i].mr_start) && 6001 (pa < (pregions[i].mr_start + pregions[i].mr_size))) 6002 return (RPTE_ATTR_MEM); 6003 } 6004 return (RPTE_ATTR_GUARDEDIO); 6005 } 6006 6007 static void 6008 mmu_radix_kenter_attr(vm_offset_t va, vm_paddr_t pa, vm_memattr_t ma) 6009 { 6010 pt_entry_t *pte, pteval; 6011 uint64_t cache_bits; 6012 6013 pte = kvtopte(va); 6014 MPASS(pte != NULL); 6015 pteval = pa | RPTE_EAA_R | RPTE_EAA_W | RPTE_EAA_P | PG_M | PG_A; 6016 cache_bits = mmu_radix_calc_wimg(pa, ma); 6017 pte_store(pte, pteval | cache_bits); 6018 } 6019 6020 void 6021 mmu_radix_kremove(vm_offset_t va) 6022 { 6023 pt_entry_t *pte; 6024 6025 CTR2(KTR_PMAP, "%s(%#x)", __func__, va); 6026 6027 pte = kvtopte(va); 6028 pte_clear(pte); 6029 } 6030 6031 int 6032 mmu_radix_decode_kernel_ptr(vm_offset_t addr, 6033 int *is_user, vm_offset_t *decoded) 6034 { 6035 6036 CTR2(KTR_PMAP, "%s(%#jx)", __func__, (uintmax_t)addr); 6037 *decoded = addr; 6038 *is_user = (addr < VM_MAXUSER_ADDRESS); 6039 return (0); 6040 } 6041 6042 static boolean_t 6043 mmu_radix_dev_direct_mapped(vm_paddr_t pa, vm_size_t size) 6044 { 6045 6046 CTR3(KTR_PMAP, "%s(%#x, %#x)", __func__, pa, size); 6047 return (mem_valid(pa, size)); 6048 } 6049 6050 static void 6051 mmu_radix_scan_init() 6052 { 6053 6054 CTR1(KTR_PMAP, "%s()", __func__); 6055 UNIMPLEMENTED(); 6056 } 6057 6058 static void 6059 mmu_radix_dumpsys_map(vm_paddr_t pa, size_t sz, 6060 void **va) 6061 { 6062 CTR4(KTR_PMAP, "%s(%#jx, %#zx, %p)", __func__, (uintmax_t)pa, sz, va); 6063 UNIMPLEMENTED(); 6064 } 6065 6066 vm_offset_t 6067 mmu_radix_quick_enter_page(vm_page_t m) 6068 { 6069 vm_paddr_t paddr; 6070 6071 CTR2(KTR_PMAP, "%s(%p)", __func__, m); 6072 paddr = VM_PAGE_TO_PHYS(m); 6073 return (PHYS_TO_DMAP(paddr)); 6074 } 6075 6076 void 6077 mmu_radix_quick_remove_page(vm_offset_t addr __unused) 6078 { 6079 /* no work to do here */ 6080 CTR2(KTR_PMAP, "%s(%#x)", __func__, addr); 6081 } 6082 6083 static void 6084 pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva) 6085 { 6086 cpu_flush_dcache((void *)sva, eva - sva); 6087 } 6088 6089 int 6090 mmu_radix_change_attr(vm_offset_t va, vm_size_t size, 6091 vm_memattr_t mode) 6092 { 6093 int error; 6094 6095 CTR4(KTR_PMAP, "%s(%#x, %#zx, %d)", __func__, va, size, mode); 6096 PMAP_LOCK(kernel_pmap); 6097 error = pmap_change_attr_locked(va, size, mode, true); 6098 PMAP_UNLOCK(kernel_pmap); 6099 return (error); 6100 } 6101 6102 static int 6103 pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode, bool flush) 6104 { 6105 vm_offset_t base, offset, tmpva; 6106 vm_paddr_t pa_start, pa_end, pa_end1; 6107 pml2_entry_t *l2e; 6108 pml3_entry_t *l3e; 6109 pt_entry_t *pte; 6110 int cache_bits, error; 6111 boolean_t changed; 6112 6113 PMAP_LOCK_ASSERT(kernel_pmap, MA_OWNED); 6114 base = trunc_page(va); 6115 offset = va & PAGE_MASK; 6116 size = round_page(offset + size); 6117 6118 /* 6119 * Only supported on kernel virtual addresses, including the direct 6120 * map but excluding the recursive map. 6121 */ 6122 if (base < DMAP_MIN_ADDRESS) 6123 return (EINVAL); 6124 6125 cache_bits = pmap_cache_bits(mode); 6126 changed = FALSE; 6127 6128 /* 6129 * Pages that aren't mapped aren't supported. Also break down 2MB pages 6130 * into 4KB pages if required. 6131 */ 6132 for (tmpva = base; tmpva < base + size; ) { 6133 l2e = pmap_pml2e(kernel_pmap, tmpva); 6134 if (l2e == NULL || *l2e == 0) 6135 return (EINVAL); 6136 if (be64toh(*l2e) & RPTE_LEAF) { 6137 /* 6138 * If the current 1GB page already has the required 6139 * memory type, then we need not demote this page. Just 6140 * increment tmpva to the next 1GB page frame. 6141 */ 6142 if ((be64toh(*l2e) & RPTE_ATTR_MASK) == cache_bits) { 6143 tmpva = trunc_1gpage(tmpva) + L2_PAGE_SIZE; 6144 continue; 6145 } 6146 6147 /* 6148 * If the current offset aligns with a 1GB page frame 6149 * and there is at least 1GB left within the range, then 6150 * we need not break down this page into 2MB pages. 6151 */ 6152 if ((tmpva & L2_PAGE_MASK) == 0 && 6153 tmpva + L2_PAGE_MASK < base + size) { 6154 tmpva += L2_PAGE_MASK; 6155 continue; 6156 } 6157 if (!pmap_demote_l2e(kernel_pmap, l2e, tmpva)) 6158 return (ENOMEM); 6159 } 6160 l3e = pmap_l2e_to_l3e(l2e, tmpva); 6161 KASSERT(l3e != NULL, ("no l3e entry for %#lx in %p\n", 6162 tmpva, l2e)); 6163 if (*l3e == 0) 6164 return (EINVAL); 6165 if (be64toh(*l3e) & RPTE_LEAF) { 6166 /* 6167 * If the current 2MB page already has the required 6168 * memory type, then we need not demote this page. Just 6169 * increment tmpva to the next 2MB page frame. 6170 */ 6171 if ((be64toh(*l3e) & RPTE_ATTR_MASK) == cache_bits) { 6172 tmpva = trunc_2mpage(tmpva) + L3_PAGE_SIZE; 6173 continue; 6174 } 6175 6176 /* 6177 * If the current offset aligns with a 2MB page frame 6178 * and there is at least 2MB left within the range, then 6179 * we need not break down this page into 4KB pages. 6180 */ 6181 if ((tmpva & L3_PAGE_MASK) == 0 && 6182 tmpva + L3_PAGE_MASK < base + size) { 6183 tmpva += L3_PAGE_SIZE; 6184 continue; 6185 } 6186 if (!pmap_demote_l3e(kernel_pmap, l3e, tmpva)) 6187 return (ENOMEM); 6188 } 6189 pte = pmap_l3e_to_pte(l3e, tmpva); 6190 if (*pte == 0) 6191 return (EINVAL); 6192 tmpva += PAGE_SIZE; 6193 } 6194 error = 0; 6195 6196 /* 6197 * Ok, all the pages exist, so run through them updating their 6198 * cache mode if required. 6199 */ 6200 pa_start = pa_end = 0; 6201 for (tmpva = base; tmpva < base + size; ) { 6202 l2e = pmap_pml2e(kernel_pmap, tmpva); 6203 if (be64toh(*l2e) & RPTE_LEAF) { 6204 if ((be64toh(*l2e) & RPTE_ATTR_MASK) != cache_bits) { 6205 pmap_pte_attr(l2e, cache_bits, 6206 RPTE_ATTR_MASK); 6207 changed = TRUE; 6208 } 6209 if (tmpva >= VM_MIN_KERNEL_ADDRESS && 6210 (*l2e & PG_PS_FRAME) < dmaplimit) { 6211 if (pa_start == pa_end) { 6212 /* Start physical address run. */ 6213 pa_start = be64toh(*l2e) & PG_PS_FRAME; 6214 pa_end = pa_start + L2_PAGE_SIZE; 6215 } else if (pa_end == (be64toh(*l2e) & PG_PS_FRAME)) 6216 pa_end += L2_PAGE_SIZE; 6217 else { 6218 /* Run ended, update direct map. */ 6219 error = pmap_change_attr_locked( 6220 PHYS_TO_DMAP(pa_start), 6221 pa_end - pa_start, mode, flush); 6222 if (error != 0) 6223 break; 6224 /* Start physical address run. */ 6225 pa_start = be64toh(*l2e) & PG_PS_FRAME; 6226 pa_end = pa_start + L2_PAGE_SIZE; 6227 } 6228 } 6229 tmpva = trunc_1gpage(tmpva) + L2_PAGE_SIZE; 6230 continue; 6231 } 6232 l3e = pmap_l2e_to_l3e(l2e, tmpva); 6233 if (be64toh(*l3e) & RPTE_LEAF) { 6234 if ((be64toh(*l3e) & RPTE_ATTR_MASK) != cache_bits) { 6235 pmap_pte_attr(l3e, cache_bits, 6236 RPTE_ATTR_MASK); 6237 changed = TRUE; 6238 } 6239 if (tmpva >= VM_MIN_KERNEL_ADDRESS && 6240 (be64toh(*l3e) & PG_PS_FRAME) < dmaplimit) { 6241 if (pa_start == pa_end) { 6242 /* Start physical address run. */ 6243 pa_start = be64toh(*l3e) & PG_PS_FRAME; 6244 pa_end = pa_start + L3_PAGE_SIZE; 6245 } else if (pa_end == (be64toh(*l3e) & PG_PS_FRAME)) 6246 pa_end += L3_PAGE_SIZE; 6247 else { 6248 /* Run ended, update direct map. */ 6249 error = pmap_change_attr_locked( 6250 PHYS_TO_DMAP(pa_start), 6251 pa_end - pa_start, mode, flush); 6252 if (error != 0) 6253 break; 6254 /* Start physical address run. */ 6255 pa_start = be64toh(*l3e) & PG_PS_FRAME; 6256 pa_end = pa_start + L3_PAGE_SIZE; 6257 } 6258 } 6259 tmpva = trunc_2mpage(tmpva) + L3_PAGE_SIZE; 6260 } else { 6261 pte = pmap_l3e_to_pte(l3e, tmpva); 6262 if ((be64toh(*pte) & RPTE_ATTR_MASK) != cache_bits) { 6263 pmap_pte_attr(pte, cache_bits, 6264 RPTE_ATTR_MASK); 6265 changed = TRUE; 6266 } 6267 if (tmpva >= VM_MIN_KERNEL_ADDRESS && 6268 (be64toh(*pte) & PG_FRAME) < dmaplimit) { 6269 if (pa_start == pa_end) { 6270 /* Start physical address run. */ 6271 pa_start = be64toh(*pte) & PG_FRAME; 6272 pa_end = pa_start + PAGE_SIZE; 6273 } else if (pa_end == (be64toh(*pte) & PG_FRAME)) 6274 pa_end += PAGE_SIZE; 6275 else { 6276 /* Run ended, update direct map. */ 6277 error = pmap_change_attr_locked( 6278 PHYS_TO_DMAP(pa_start), 6279 pa_end - pa_start, mode, flush); 6280 if (error != 0) 6281 break; 6282 /* Start physical address run. */ 6283 pa_start = be64toh(*pte) & PG_FRAME; 6284 pa_end = pa_start + PAGE_SIZE; 6285 } 6286 } 6287 tmpva += PAGE_SIZE; 6288 } 6289 } 6290 if (error == 0 && pa_start != pa_end && pa_start < dmaplimit) { 6291 pa_end1 = MIN(pa_end, dmaplimit); 6292 if (pa_start != pa_end1) 6293 error = pmap_change_attr_locked(PHYS_TO_DMAP(pa_start), 6294 pa_end1 - pa_start, mode, flush); 6295 } 6296 6297 /* 6298 * Flush CPU caches if required to make sure any data isn't cached that 6299 * shouldn't be, etc. 6300 */ 6301 if (changed) { 6302 pmap_invalidate_all(kernel_pmap); 6303 6304 if (flush) 6305 pmap_invalidate_cache_range(base, tmpva); 6306 } 6307 return (error); 6308 } 6309 6310 /* 6311 * Allocate physical memory for the vm_page array and map it into KVA, 6312 * attempting to back the vm_pages with domain-local memory. 6313 */ 6314 void 6315 mmu_radix_page_array_startup(long pages) 6316 { 6317 #ifdef notyet 6318 pml2_entry_t *l2e; 6319 pml3_entry_t *pde; 6320 pml3_entry_t newl3; 6321 vm_offset_t va; 6322 long pfn; 6323 int domain, i; 6324 #endif 6325 vm_paddr_t pa; 6326 vm_offset_t start, end; 6327 6328 vm_page_array_size = pages; 6329 6330 start = VM_MIN_KERNEL_ADDRESS; 6331 end = start + pages * sizeof(struct vm_page); 6332 6333 pa = vm_phys_early_alloc(0, end - start); 6334 6335 start = mmu_radix_map(&start, pa, end - start, VM_MEMATTR_DEFAULT); 6336 #ifdef notyet 6337 /* TODO: NUMA vm_page_array. Blocked out until then (copied from amd64). */ 6338 for (va = start; va < end; va += L3_PAGE_SIZE) { 6339 pfn = first_page + (va - start) / sizeof(struct vm_page); 6340 domain = vm_phys_domain(ptoa(pfn)); 6341 l2e = pmap_pml2e(kernel_pmap, va); 6342 if ((be64toh(*l2e) & PG_V) == 0) { 6343 pa = vm_phys_early_alloc(domain, PAGE_SIZE); 6344 dump_add_page(pa); 6345 pagezero(PHYS_TO_DMAP(pa)); 6346 pde_store(l2e, (pml2_entry_t)pa); 6347 } 6348 pde = pmap_l2e_to_l3e(l2e, va); 6349 if ((be64toh(*pde) & PG_V) != 0) 6350 panic("Unexpected pde %p", pde); 6351 pa = vm_phys_early_alloc(domain, L3_PAGE_SIZE); 6352 for (i = 0; i < NPDEPG; i++) 6353 dump_add_page(pa + i * PAGE_SIZE); 6354 newl3 = (pml3_entry_t)(pa | RPTE_EAA_P | RPTE_EAA_R | RPTE_EAA_W); 6355 pte_store(pde, newl3); 6356 } 6357 #endif 6358 vm_page_array = (vm_page_t)start; 6359 } 6360 6361 #ifdef DDB 6362 #include <sys/kdb.h> 6363 #include <ddb/ddb.h> 6364 6365 static void 6366 pmap_pte_walk(pml1_entry_t *l1, vm_offset_t va) 6367 { 6368 pml1_entry_t *l1e; 6369 pml2_entry_t *l2e; 6370 pml3_entry_t *l3e; 6371 pt_entry_t *pte; 6372 6373 l1e = &l1[pmap_pml1e_index(va)]; 6374 db_printf("VA %#016lx l1e %#016lx", va, be64toh(*l1e)); 6375 if ((be64toh(*l1e) & PG_V) == 0) { 6376 db_printf("\n"); 6377 return; 6378 } 6379 l2e = pmap_l1e_to_l2e(l1e, va); 6380 db_printf(" l2e %#016lx", be64toh(*l2e)); 6381 if ((be64toh(*l2e) & PG_V) == 0 || (be64toh(*l2e) & RPTE_LEAF) != 0) { 6382 db_printf("\n"); 6383 return; 6384 } 6385 l3e = pmap_l2e_to_l3e(l2e, va); 6386 db_printf(" l3e %#016lx", be64toh(*l3e)); 6387 if ((be64toh(*l3e) & PG_V) == 0 || (be64toh(*l3e) & RPTE_LEAF) != 0) { 6388 db_printf("\n"); 6389 return; 6390 } 6391 pte = pmap_l3e_to_pte(l3e, va); 6392 db_printf(" pte %#016lx\n", be64toh(*pte)); 6393 } 6394 6395 void 6396 pmap_page_print_mappings(vm_page_t m) 6397 { 6398 pmap_t pmap; 6399 pv_entry_t pv; 6400 6401 db_printf("page %p(%lx)\n", m, m->phys_addr); 6402 /* need to elide locks if running in ddb */ 6403 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) { 6404 db_printf("pv: %p ", pv); 6405 db_printf("va: %#016lx ", pv->pv_va); 6406 pmap = PV_PMAP(pv); 6407 db_printf("pmap %p ", pmap); 6408 if (pmap != NULL) { 6409 db_printf("asid: %lu\n", pmap->pm_pid); 6410 pmap_pte_walk(pmap->pm_pml1, pv->pv_va); 6411 } 6412 } 6413 } 6414 6415 DB_SHOW_COMMAND(pte, pmap_print_pte) 6416 { 6417 vm_offset_t va; 6418 pmap_t pmap; 6419 6420 if (!have_addr) { 6421 db_printf("show pte addr\n"); 6422 return; 6423 } 6424 va = (vm_offset_t)addr; 6425 6426 if (va >= DMAP_MIN_ADDRESS) 6427 pmap = kernel_pmap; 6428 else if (kdb_thread != NULL) 6429 pmap = vmspace_pmap(kdb_thread->td_proc->p_vmspace); 6430 else 6431 pmap = vmspace_pmap(curthread->td_proc->p_vmspace); 6432 6433 pmap_pte_walk(pmap->pm_pml1, va); 6434 } 6435 6436 #endif 6437