1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * VM - Hardware Address Translation management for i386 and amd64 30 * 31 * Implementation of the interfaces described in <common/vm/hat.h> 32 * 33 * Nearly all the details of how the hardware is managed should not be 34 * visible outside this layer except for misc. machine specific functions 35 * that work in conjunction with this code. 36 * 37 * Routines used only inside of i86pc/vm start with hati_ for HAT Internal. 38 */ 39 40 #include <sys/machparam.h> 41 #include <sys/machsystm.h> 42 #include <sys/mman.h> 43 #include <sys/types.h> 44 #include <sys/systm.h> 45 #include <sys/cpuvar.h> 46 #include <sys/thread.h> 47 #include <sys/proc.h> 48 #include <sys/cpu.h> 49 #include <sys/kmem.h> 50 #include <sys/disp.h> 51 #include <sys/shm.h> 52 #include <sys/sysmacros.h> 53 #include <sys/machparam.h> 54 #include <sys/vmem.h> 55 #include <sys/vmsystm.h> 56 #include <sys/promif.h> 57 #include <sys/var.h> 58 #include <sys/x86_archext.h> 59 #include <sys/atomic.h> 60 #include <sys/bitmap.h> 61 #include <sys/controlregs.h> 62 #include <sys/bootconf.h> 63 #include <sys/bootsvcs.h> 64 #include <sys/bootinfo.h> 65 66 #include <vm/seg_kmem.h> 67 #include <vm/hat_i86.h> 68 #include <vm/as.h> 69 #include <vm/seg.h> 70 #include <vm/page.h> 71 #include <vm/seg_kp.h> 72 #include <vm/seg_kpm.h> 73 #include <vm/vm_dep.h> 74 #include <vm/kboot_mmu.h> 75 76 #include <sys/cmn_err.h> 77 78 /* 79 * Basic parameters for hat operation. 80 */ 81 struct hat_mmu_info mmu; 82 83 /* 84 * The page that is the kernel's top level pagetable. 85 * 86 * For 32 bit VLP support, the kernel hat will use the 1st 4 entries 87 * on this 4K page for its top level page table. The remaining groups of 88 * 4 entries are used for per processor copies of user VLP pagetables for 89 * running threads. See hat_switch() and reload_pae32() for details. 90 * 91 * vlp_page[0] - 0th level==2 PTE for kernel HAT (will be zero) 92 * vlp_page[1] - 1st level==2 PTE for kernel HAT (will be zero) 93 * vlp_page[2] - 2nd level==2 PTE for kernel HAT (zero for small memory) 94 * vlp_page[3] - 3rd level==2 PTE for kernel 95 * 96 * vlp_page[4] - 0th level==2 PTE for user thread on cpu 0 97 * vlp_page[5] - 1st level==2 PTE for user thread on cpu 0 98 * vlp_page[6] - 2nd level==2 PTE for user thread on cpu 0 99 * vlp_page[7] - probably copy of kernel PTE 100 * 101 * vlp_page[8] - 0th level==2 PTE for user thread on cpu 1 102 * vlp_page[9] - 1st level==2 PTE for user thread on cpu 1 103 * vlp_page[10] - 2nd level==2 PTE for user thread on cpu 1 104 * vlp_page[11] - probably copy of kernel PTE 105 * ... 106 * 107 * when / where the kernel PTE's are (entry 2 or 3 or none) depends 108 * on kernelbase. 109 */ 110 static x86pte_t *vlp_page; 111 112 /* 113 * forward declaration of internal utility routines 114 */ 115 static x86pte_t hati_update_pte(htable_t *ht, uint_t entry, x86pte_t expected, 116 x86pte_t new); 117 118 /* 119 * The kernel address space exists in all HATs. To implement this the 120 * kernel reserves a fixed number of entries in every topmost level page 121 * table. The values are setup in hat_init() and then copied to every hat 122 * created by hat_alloc(). This means that kernelbase must be: 123 * 124 * 4Meg aligned for 32 bit kernels 125 * 512Gig aligned for x86_64 64 bit kernel 126 * 127 * The PAE 32 bit hat is handled as a special case. Otherwise requiring 1Gig 128 * alignment would use too much VA for the kernel. 129 * 130 */ 131 static uint_t khat_start; /* index of 1st entry in kernel's top ptable */ 132 static uint_t khat_entries; /* number of entries in kernel's top ptable */ 133 134 #if defined(__i386) 135 136 static htable_t *khat_pae32_htable = NULL; 137 static uint_t khat_pae32_start; 138 static uint_t khat_pae32_entries; 139 140 #endif 141 142 /* 143 * Locks, etc. to control use of the hat reserves when recursively 144 * allocating pagetables for the hat data structures. 145 */ 146 static kmutex_t hat_reserves_lock; 147 static kcondvar_t hat_reserves_cv; 148 kthread_t *hat_reserves_thread; 149 uint_t use_boot_reserve = 1; /* cleared after early boot process */ 150 uint_t can_steal_post_boot = 0; /* set late in boot to enable stealing */ 151 152 /* 153 * A cpuset for all cpus. This is used for kernel address cross calls, since 154 * the kernel addresses apply to all cpus. 155 */ 156 cpuset_t khat_cpuset; 157 158 /* 159 * management stuff for hat structures 160 */ 161 kmutex_t hat_list_lock; 162 kcondvar_t hat_list_cv; 163 kmem_cache_t *hat_cache; 164 kmem_cache_t *hat_hash_cache; 165 kmem_cache_t *vlp_hash_cache; 166 167 /* 168 * Simple statistics 169 */ 170 struct hatstats hatstat; 171 172 /* 173 * useful stuff for atomic access/clearing/setting REF/MOD/RO bits in page_t's. 174 */ 175 extern void atomic_orb(uchar_t *addr, uchar_t val); 176 extern void atomic_andb(uchar_t *addr, uchar_t val); 177 178 #define PP_GETRM(pp, rmmask) (pp->p_nrm & rmmask) 179 #define PP_ISMOD(pp) PP_GETRM(pp, P_MOD) 180 #define PP_ISREF(pp) PP_GETRM(pp, P_REF) 181 #define PP_ISRO(pp) PP_GETRM(pp, P_RO) 182 183 #define PP_SETRM(pp, rm) atomic_orb(&(pp->p_nrm), rm) 184 #define PP_SETMOD(pp) PP_SETRM(pp, P_MOD) 185 #define PP_SETREF(pp) PP_SETRM(pp, P_REF) 186 #define PP_SETRO(pp) PP_SETRM(pp, P_RO) 187 188 #define PP_CLRRM(pp, rm) atomic_andb(&(pp->p_nrm), ~(rm)) 189 #define PP_CLRMOD(pp) PP_CLRRM(pp, P_MOD) 190 #define PP_CLRREF(pp) PP_CLRRM(pp, P_REF) 191 #define PP_CLRRO(pp) PP_CLRRM(pp, P_RO) 192 #define PP_CLRALL(pp) PP_CLRRM(pp, P_MOD | P_REF | P_RO) 193 194 /* 195 * some useful tracing macros 196 */ 197 198 int hattrace = 0; 199 #ifdef DEBUG 200 201 #define HATIN(r, h, a, l) \ 202 if (hattrace) prom_printf("->%s hat=%p, adr=%p, len=%lx\n", #r, h, a, l) 203 204 #define HATOUT(r, h, a) \ 205 if (hattrace) prom_printf("<-%s hat=%p, adr=%p\n", #r, h, a) 206 #else 207 208 #define HATIN(r, h, a, l) 209 #define HATOUT(r, h, a) 210 211 #endif 212 213 214 /* 215 * kmem cache constructor for struct hat 216 */ 217 /*ARGSUSED*/ 218 static int 219 hati_constructor(void *buf, void *handle, int kmflags) 220 { 221 hat_t *hat = buf; 222 223 mutex_init(&hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL); 224 bzero(hat->hat_pages_mapped, 225 sizeof (pgcnt_t) * (mmu.max_page_level + 1)); 226 hat->hat_stats = 0; 227 hat->hat_flags = 0; 228 mutex_init(&hat->hat_switch_mutex, NULL, MUTEX_DRIVER, 229 (void *)ipltospl(DISP_LEVEL)); 230 CPUSET_ZERO(hat->hat_cpus); 231 hat->hat_htable = NULL; 232 hat->hat_ht_hash = NULL; 233 return (0); 234 } 235 236 /* 237 * Allocate a hat structure for as. We also create the top level 238 * htable and initialize it to contain the kernel hat entries. 239 */ 240 hat_t * 241 hat_alloc(struct as *as) 242 { 243 hat_t *hat; 244 htable_t *ht; /* top level htable */ 245 uint_t use_vlp; 246 247 /* 248 * Once we start creating user process HATs we can enable 249 * the htable_steal() code. 250 */ 251 if (can_steal_post_boot == 0) 252 can_steal_post_boot = 1; 253 254 ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 255 hat = kmem_cache_alloc(hat_cache, KM_SLEEP); 256 hat->hat_as = as; 257 mutex_init(&hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL); 258 ASSERT(hat->hat_flags == 0); 259 260 /* 261 * a 32 bit process uses a VLP style hat when using PAE 262 */ 263 #if defined(__amd64) 264 use_vlp = (ttoproc(curthread)->p_model == DATAMODEL_ILP32); 265 #elif defined(__i386) 266 use_vlp = mmu.pae_hat; 267 #endif 268 if (use_vlp) { 269 hat->hat_flags = HAT_VLP; 270 bzero(hat->hat_vlp_ptes, VLP_SIZE); 271 } 272 273 /* 274 * Allocate the htable hash 275 */ 276 if ((hat->hat_flags & HAT_VLP)) { 277 hat->hat_num_hash = mmu.vlp_hash_cnt; 278 hat->hat_ht_hash = kmem_cache_alloc(vlp_hash_cache, KM_SLEEP); 279 } else { 280 hat->hat_num_hash = mmu.hash_cnt; 281 hat->hat_ht_hash = kmem_cache_alloc(hat_hash_cache, KM_SLEEP); 282 } 283 bzero(hat->hat_ht_hash, hat->hat_num_hash * sizeof (htable_t *)); 284 285 /* 286 * Initialize Kernel HAT entries at the top of the top level page 287 * table for the new hat. 288 * 289 * Note that we don't call htable_release() for the top level, that 290 * happens when the hat is destroyed in hat_free_end() 291 */ 292 hat->hat_htable = NULL; 293 hat->hat_ht_cached = NULL; 294 ht = htable_create(hat, (uintptr_t)0, TOP_LEVEL(hat), NULL); 295 296 if (!(hat->hat_flags & HAT_VLP)) 297 x86pte_copy(kas.a_hat->hat_htable, ht, khat_start, 298 khat_entries); 299 #if defined(__i386) 300 else if (khat_entries > 0) 301 bcopy(vlp_page + khat_start, hat->hat_vlp_ptes + khat_start, 302 khat_entries * sizeof (x86pte_t)); 303 #endif 304 hat->hat_htable = ht; 305 306 #if defined(__i386) 307 /* 308 * PAE32 HAT alignment is less restrictive than the others to keep 309 * the kernel from using too much VA. Because of this we may need 310 * one layer further down when kernelbase isn't 1Gig aligned. 311 * See hat_free_end() for the htable_release() that goes with this 312 * htable_create() 313 */ 314 if (khat_pae32_htable != NULL) { 315 ht = htable_create(hat, kernelbase, 316 khat_pae32_htable->ht_level, NULL); 317 x86pte_copy(khat_pae32_htable, ht, khat_pae32_start, 318 khat_pae32_entries); 319 ht->ht_valid_cnt = khat_pae32_entries; 320 } 321 #endif 322 323 /* 324 * Put it at the start of the global list of all hats (used by stealing) 325 * 326 * kas.a_hat is not in the list but is instead used to find the 327 * first and last items in the list. 328 * 329 * - kas.a_hat->hat_next points to the start of the user hats. 330 * The list ends where hat->hat_next == NULL 331 * 332 * - kas.a_hat->hat_prev points to the last of the user hats. 333 * The list begins where hat->hat_prev == NULL 334 */ 335 mutex_enter(&hat_list_lock); 336 hat->hat_prev = NULL; 337 hat->hat_next = kas.a_hat->hat_next; 338 if (hat->hat_next) 339 hat->hat_next->hat_prev = hat; 340 else 341 kas.a_hat->hat_prev = hat; 342 kas.a_hat->hat_next = hat; 343 mutex_exit(&hat_list_lock); 344 345 return (hat); 346 } 347 348 /* 349 * process has finished executing but as has not been cleaned up yet. 350 */ 351 /*ARGSUSED*/ 352 void 353 hat_free_start(hat_t *hat) 354 { 355 ASSERT(AS_WRITE_HELD(hat->hat_as, &hat->hat_as->a_lock)); 356 357 /* 358 * If the hat is currently a stealing victim, wait for the stealing 359 * to finish. Once we mark it as HAT_FREEING, htable_steal() 360 * won't look at its pagetables anymore. 361 */ 362 mutex_enter(&hat_list_lock); 363 while (hat->hat_flags & HAT_VICTIM) 364 cv_wait(&hat_list_cv, &hat_list_lock); 365 hat->hat_flags |= HAT_FREEING; 366 mutex_exit(&hat_list_lock); 367 } 368 369 /* 370 * An address space is being destroyed, so we destroy the associated hat. 371 */ 372 void 373 hat_free_end(hat_t *hat) 374 { 375 int i; 376 kmem_cache_t *cache; 377 378 #ifdef DEBUG 379 for (i = 0; i <= mmu.max_page_level; i++) 380 ASSERT(hat->hat_pages_mapped[i] == 0); 381 #endif 382 ASSERT(hat->hat_flags & HAT_FREEING); 383 384 /* 385 * must not be running on the given hat 386 */ 387 ASSERT(CPU->cpu_current_hat != hat); 388 389 /* 390 * Remove it from the list of HATs 391 */ 392 mutex_enter(&hat_list_lock); 393 if (hat->hat_prev) 394 hat->hat_prev->hat_next = hat->hat_next; 395 else 396 kas.a_hat->hat_next = hat->hat_next; 397 if (hat->hat_next) 398 hat->hat_next->hat_prev = hat->hat_prev; 399 else 400 kas.a_hat->hat_prev = hat->hat_prev; 401 mutex_exit(&hat_list_lock); 402 hat->hat_next = hat->hat_prev = NULL; 403 404 /* 405 * Make a pass through the htables freeing them all up. 406 */ 407 htable_purge_hat(hat); 408 409 /* 410 * Decide which kmem cache the hash table came from, then free it. 411 */ 412 if (hat->hat_flags & HAT_VLP) 413 cache = vlp_hash_cache; 414 else 415 cache = hat_hash_cache; 416 kmem_cache_free(cache, hat->hat_ht_hash); 417 hat->hat_ht_hash = NULL; 418 419 hat->hat_flags = 0; 420 kmem_cache_free(hat_cache, hat); 421 } 422 423 /* 424 * round kernelbase down to a supported value to use for _userlimit 425 * 426 * userlimit must be aligned down to an entry in the top level htable. 427 * The one exception is for 32 bit HAT's running PAE. 428 */ 429 uintptr_t 430 hat_kernelbase(uintptr_t va) 431 { 432 #if defined(__i386) 433 va &= LEVEL_MASK(1); 434 #endif 435 if (IN_VA_HOLE(va)) 436 panic("_userlimit %p will fall in VA hole\n", (void *)va); 437 return (va); 438 } 439 440 /* 441 * Initialize hat data structures based on processor MMU information. 442 */ 443 void 444 mmu_init(void) 445 { 446 uint_t max_htables; 447 uint_t pa_bits; 448 uint_t va_bits; 449 int i; 450 451 /* 452 * If CPU enabled the page table global bit, use it for the kernel 453 * This is bit 7 in CR4 (PGE - Page Global Enable). 454 */ 455 if ((x86_feature & X86_PGE) != 0 && (getcr4() & CR4_PGE) != 0) 456 mmu.pt_global = PT_GLOBAL; 457 458 /* 459 * Detect NX and PAE usage. 460 */ 461 mmu.pae_hat = kbm_pae_support; 462 if (kbm_nx_support) 463 mmu.pt_nx = PT_NX; 464 else 465 mmu.pt_nx = 0; 466 467 /* 468 * Use CPU info to set various MMU parameters 469 */ 470 cpuid_get_addrsize(CPU, &pa_bits, &va_bits); 471 472 if (va_bits < sizeof (void *) * NBBY) { 473 mmu.hole_start = (1ul << (va_bits - 1)); 474 mmu.hole_end = 0ul - mmu.hole_start - 1; 475 } else { 476 mmu.hole_end = 0; 477 mmu.hole_start = mmu.hole_end - 1; 478 } 479 #if defined(OPTERON_ERRATUM_121) 480 /* 481 * If erratum 121 has already been detected at this time, hole_start 482 * contains the value to be subtracted from mmu.hole_start. 483 */ 484 ASSERT(hole_start == 0 || opteron_erratum_121 != 0); 485 hole_start = mmu.hole_start - hole_start; 486 #else 487 hole_start = mmu.hole_start; 488 #endif 489 hole_end = mmu.hole_end; 490 491 mmu.highest_pfn = mmu_btop((1ull << pa_bits) - 1); 492 if (mmu.pae_hat == 0 && pa_bits > 32) 493 mmu.highest_pfn = PFN_4G - 1; 494 495 if (mmu.pae_hat) { 496 mmu.pte_size = 8; /* 8 byte PTEs */ 497 mmu.pte_size_shift = 3; 498 } else { 499 mmu.pte_size = 4; /* 4 byte PTEs */ 500 mmu.pte_size_shift = 2; 501 } 502 503 if (mmu.pae_hat && (x86_feature & X86_PAE) == 0) 504 panic("Processor does not support PAE"); 505 506 if ((x86_feature & X86_CX8) == 0) 507 panic("Processor does not support cmpxchg8b instruction"); 508 509 /* 510 * Initialize parameters based on the 64 or 32 bit kernels and 511 * for the 32 bit kernel decide if we should use PAE. 512 */ 513 if (kbm_largepage_support) 514 mmu.max_page_level = 1; 515 else 516 mmu.max_page_level = 0; 517 mmu_page_sizes = mmu.max_page_level + 1; 518 mmu_exported_page_sizes = mmu_page_sizes; 519 520 #if defined(__amd64) 521 522 mmu.num_level = 4; 523 mmu.max_level = 3; 524 mmu.ptes_per_table = 512; 525 mmu.top_level_count = 512; 526 527 mmu.level_shift[0] = 12; 528 mmu.level_shift[1] = 21; 529 mmu.level_shift[2] = 30; 530 mmu.level_shift[3] = 39; 531 532 #elif defined(__i386) 533 534 if (mmu.pae_hat) { 535 mmu.num_level = 3; 536 mmu.max_level = 2; 537 mmu.ptes_per_table = 512; 538 mmu.top_level_count = 4; 539 540 mmu.level_shift[0] = 12; 541 mmu.level_shift[1] = 21; 542 mmu.level_shift[2] = 30; 543 544 } else { 545 mmu.num_level = 2; 546 mmu.max_level = 1; 547 mmu.ptes_per_table = 1024; 548 mmu.top_level_count = 1024; 549 550 mmu.level_shift[0] = 12; 551 mmu.level_shift[1] = 22; 552 } 553 554 #endif /* __i386 */ 555 556 for (i = 0; i < mmu.num_level; ++i) { 557 mmu.level_size[i] = 1UL << mmu.level_shift[i]; 558 mmu.level_offset[i] = mmu.level_size[i] - 1; 559 mmu.level_mask[i] = ~mmu.level_offset[i]; 560 } 561 562 for (i = 0; i <= mmu.max_page_level; ++i) { 563 mmu.pte_bits[i] = PT_VALID; 564 if (i > 0) 565 mmu.pte_bits[i] |= PT_PAGESIZE; 566 } 567 568 /* 569 * NOTE Legacy 32 bit PAE mode only has the P_VALID bit at top level. 570 */ 571 for (i = 1; i < mmu.num_level; ++i) 572 mmu.ptp_bits[i] = PT_PTPBITS; 573 574 #if defined(__i386) 575 mmu.ptp_bits[2] = PT_VALID; 576 #endif 577 578 /* 579 * Compute how many hash table entries to have per process for htables. 580 * We start with 1 page's worth of entries. 581 * 582 * If physical memory is small, reduce the amount need to cover it. 583 */ 584 max_htables = physmax / mmu.ptes_per_table; 585 mmu.hash_cnt = MMU_PAGESIZE / sizeof (htable_t *); 586 while (mmu.hash_cnt > 16 && mmu.hash_cnt >= max_htables) 587 mmu.hash_cnt >>= 1; 588 mmu.vlp_hash_cnt = mmu.hash_cnt; 589 590 #if defined(__amd64) 591 /* 592 * If running in 64 bits and physical memory is large, 593 * increase the size of the cache to cover all of memory for 594 * a 64 bit process. 595 */ 596 #define HASH_MAX_LENGTH 4 597 while (mmu.hash_cnt * HASH_MAX_LENGTH < max_htables) 598 mmu.hash_cnt <<= 1; 599 #endif 600 } 601 602 603 /* 604 * initialize hat data structures 605 */ 606 void 607 hat_init() 608 { 609 #if defined(__i386) 610 /* 611 * _userlimit must be aligned correctly 612 */ 613 if ((_userlimit & LEVEL_MASK(1)) != _userlimit) { 614 prom_printf("hat_init(): _userlimit=%p, not aligned at %p\n", 615 (void *)_userlimit, (void *)LEVEL_SIZE(1)); 616 halt("hat_init(): Unable to continue"); 617 } 618 #endif 619 620 cv_init(&hat_list_cv, NULL, CV_DEFAULT, NULL); 621 622 /* 623 * initialize kmem caches 624 */ 625 htable_init(); 626 hment_init(); 627 628 hat_cache = kmem_cache_create("hat_t", 629 sizeof (hat_t), 0, hati_constructor, NULL, NULL, 630 NULL, 0, 0); 631 632 hat_hash_cache = kmem_cache_create("HatHash", 633 mmu.hash_cnt * sizeof (htable_t *), 0, NULL, NULL, NULL, 634 NULL, 0, 0); 635 636 /* 637 * VLP hats can use a smaller hash table size on large memroy machines 638 */ 639 if (mmu.hash_cnt == mmu.vlp_hash_cnt) { 640 vlp_hash_cache = hat_hash_cache; 641 } else { 642 vlp_hash_cache = kmem_cache_create("HatVlpHash", 643 mmu.vlp_hash_cnt * sizeof (htable_t *), 0, NULL, NULL, NULL, 644 NULL, 0, 0); 645 } 646 647 /* 648 * Set up the kernel's hat 649 */ 650 AS_LOCK_ENTER(&kas, &kas.a_lock, RW_WRITER); 651 kas.a_hat = kmem_cache_alloc(hat_cache, KM_NOSLEEP); 652 mutex_init(&kas.a_hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL); 653 kas.a_hat->hat_as = &kas; 654 kas.a_hat->hat_flags = 0; 655 AS_LOCK_EXIT(&kas, &kas.a_lock); 656 657 CPUSET_ZERO(khat_cpuset); 658 CPUSET_ADD(khat_cpuset, CPU->cpu_id); 659 660 /* 661 * The kernel hat's next pointer serves as the head of the hat list . 662 * The kernel hat's prev pointer tracks the last hat on the list for 663 * htable_steal() to use. 664 */ 665 kas.a_hat->hat_next = NULL; 666 kas.a_hat->hat_prev = NULL; 667 668 /* 669 * Allocate an htable hash bucket for the kernel 670 * XX64 - tune for 64 bit procs 671 */ 672 kas.a_hat->hat_num_hash = mmu.hash_cnt; 673 kas.a_hat->hat_ht_hash = kmem_cache_alloc(hat_hash_cache, KM_NOSLEEP); 674 bzero(kas.a_hat->hat_ht_hash, mmu.hash_cnt * sizeof (htable_t *)); 675 676 /* 677 * zero out the top level and cached htable pointers 678 */ 679 kas.a_hat->hat_ht_cached = NULL; 680 kas.a_hat->hat_htable = NULL; 681 682 /* 683 * Pre-allocate hrm_hashtab before enabling the collection of 684 * refmod statistics. Allocating on the fly would mean us 685 * running the risk of suffering recursive mutex enters or 686 * deadlocks. 687 */ 688 hrm_hashtab = kmem_zalloc(HRM_HASHSIZE * sizeof (struct hrmstat *), 689 KM_SLEEP); 690 } 691 692 /* 693 * Prepare CPU specific pagetables for VLP processes on 64 bit kernels. 694 * 695 * Each CPU has a set of 2 pagetables that are reused for any 32 bit 696 * process it runs. They are the top level pagetable, hci_vlp_l3ptes, and 697 * the next to top level table for the bottom 512 Gig, hci_vlp_l2ptes. 698 */ 699 /*ARGSUSED*/ 700 static void 701 hat_vlp_setup(struct cpu *cpu) 702 { 703 #if defined(__amd64) 704 struct hat_cpu_info *hci = cpu->cpu_hat_info; 705 pfn_t pfn; 706 707 /* 708 * allocate the level==2 page table for the bottom most 709 * 512Gig of address space (this is where 32 bit apps live) 710 */ 711 ASSERT(hci != NULL); 712 hci->hci_vlp_l2ptes = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP); 713 714 /* 715 * Allocate a top level pagetable and copy the kernel's 716 * entries into it. Then link in hci_vlp_l2ptes in the 1st entry. 717 */ 718 hci->hci_vlp_l3ptes = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP); 719 hci->hci_vlp_pfn = 720 hat_getpfnum(kas.a_hat, (caddr_t)hci->hci_vlp_l3ptes); 721 ASSERT(hci->hci_vlp_pfn != PFN_INVALID); 722 bcopy(vlp_page + khat_start, hci->hci_vlp_l3ptes + khat_start, 723 khat_entries * sizeof (x86pte_t)); 724 725 pfn = hat_getpfnum(kas.a_hat, (caddr_t)hci->hci_vlp_l2ptes); 726 ASSERT(pfn != PFN_INVALID); 727 hci->hci_vlp_l3ptes[0] = MAKEPTP(pfn, 2); 728 #endif /* __amd64 */ 729 } 730 731 /*ARGSUSED*/ 732 static void 733 hat_vlp_teardown(cpu_t *cpu) 734 { 735 #if defined(__amd64) 736 struct hat_cpu_info *hci; 737 738 if ((hci = cpu->cpu_hat_info) == NULL) 739 return; 740 if (hci->hci_vlp_l2ptes) 741 kmem_free(hci->hci_vlp_l2ptes, MMU_PAGESIZE); 742 if (hci->hci_vlp_l3ptes) 743 kmem_free(hci->hci_vlp_l3ptes, MMU_PAGESIZE); 744 #endif /* __amd64 */ 745 } 746 747 /* 748 * Finish filling in the kernel hat. 749 * Pre fill in all top level kernel page table entries for the kernel's 750 * part of the address range. From this point on we can't use any new 751 * kernel large pages if they need PTE's at max_level 752 * 753 * create the kmap mappings. 754 */ 755 void 756 hat_init_finish(void) 757 { 758 htable_t *top = kas.a_hat->hat_htable; 759 htable_t *ht; 760 uint_t e; 761 x86pte_t pte; 762 uintptr_t va = kernelbase; 763 size_t size; 764 765 766 #if defined(__i386) 767 ASSERT((va & LEVEL_MASK(1)) == va); 768 769 /* 770 * Deal with kernelbase not 1Gig aligned for 32 bit PAE hats. 771 */ 772 if (!mmu.pae_hat || (va & LEVEL_OFFSET(mmu.max_level)) == 0) { 773 khat_pae32_htable = NULL; 774 } else { 775 ASSERT(mmu.max_level == 2); 776 ASSERT((va & LEVEL_OFFSET(mmu.max_level - 1)) == 0); 777 khat_pae32_htable = 778 htable_create(kas.a_hat, va, mmu.max_level - 1, NULL); 779 khat_pae32_start = htable_va2entry(va, khat_pae32_htable); 780 khat_pae32_entries = mmu.ptes_per_table - khat_pae32_start; 781 for (e = khat_pae32_start; e < mmu.ptes_per_table; 782 ++e, va += LEVEL_SIZE(mmu.max_level - 1)) { 783 pte = x86pte_get(khat_pae32_htable, e); 784 if (PTE_ISVALID(pte)) 785 continue; 786 ht = htable_create(kas.a_hat, va, mmu.max_level - 2, 787 NULL); 788 ASSERT(ht != NULL); 789 } 790 } 791 #endif 792 793 /* 794 * The kernel hat will need fixed values in the highest level 795 * ptable for copying to all other hat's. This implies 796 * alignment restrictions on _userlimit. 797 * 798 * Note we don't htable_release() these htables. This keeps them 799 * from ever being stolen or free'd. 800 * 801 * top_level_count is used instead of ptes_per_table, since 802 * on 32-bit PAE we only have 4 usable entries at the top level ptable. 803 */ 804 if (va == 0) 805 khat_start = mmu.top_level_count; 806 else 807 khat_start = htable_va2entry(va, kas.a_hat->hat_htable); 808 khat_entries = mmu.top_level_count - khat_start; 809 for (e = khat_start; e < mmu.top_level_count; 810 ++e, va += LEVEL_SIZE(mmu.max_level)) { 811 if (IN_HYPERVISOR_VA(va)) 812 continue; 813 pte = x86pte_get(top, e); 814 if (PTE_ISVALID(pte)) 815 continue; 816 ht = htable_create(kas.a_hat, va, mmu.max_level - 1, NULL); 817 ASSERT(ht != NULL); 818 } 819 820 /* 821 * We are now effectively running on the kernel hat. 822 * Clearing use_boot_reserve shuts off using the pre-allocated boot 823 * reserve for all HAT allocations. From here on, the reserves are 824 * only used when mapping in memory for the hat's own allocations. 825 */ 826 use_boot_reserve = 0; 827 htable_adjust_reserve(); 828 829 /* 830 * 32 bit kernels use only 4 of the 512 entries in its top level 831 * pagetable. We'll use the remainder for the "per CPU" page tables 832 * for VLP processes. 833 * 834 * We also map the top level kernel pagetable into the kernel to make 835 * it easy to use bcopy to initialize new address spaces. 836 */ 837 if (mmu.pae_hat) { 838 vlp_page = vmem_alloc(heap_arena, MMU_PAGESIZE, VM_SLEEP); 839 hat_devload(kas.a_hat, (caddr_t)vlp_page, MMU_PAGESIZE, 840 kas.a_hat->hat_htable->ht_pfn, 841 PROT_WRITE | 842 PROT_READ | HAT_NOSYNC | HAT_UNORDERED_OK, 843 HAT_LOAD | HAT_LOAD_NOCONSIST); 844 } 845 hat_vlp_setup(CPU); 846 847 /* 848 * Create kmap (cached mappings of kernel PTEs) 849 * for 32 bit we map from segmap_start .. ekernelheap 850 * for 64 bit we map from segmap_start .. segmap_start + segmapsize; 851 */ 852 #if defined(__i386) 853 size = (uintptr_t)ekernelheap - segmap_start; 854 #elif defined(__amd64) 855 size = segmapsize; 856 #endif 857 hat_kmap_init((uintptr_t)segmap_start, size); 858 } 859 860 /* 861 * On 32 bit PAE mode, PTE's are 64 bits, but ordinary atomic memory references 862 * are 32 bit, so for safety we must use cas64() to install these. 863 */ 864 #ifdef __i386 865 static void 866 reload_pae32(hat_t *hat, cpu_t *cpu) 867 { 868 x86pte_t *src; 869 x86pte_t *dest; 870 x86pte_t pte; 871 int i; 872 873 /* 874 * Load the 4 entries of the level 2 page table into this 875 * cpu's range of the vlp_page and point cr3 at them. 876 */ 877 ASSERT(mmu.pae_hat); 878 src = hat->hat_vlp_ptes; 879 dest = vlp_page + (cpu->cpu_id + 1) * VLP_NUM_PTES; 880 for (i = 0; i < VLP_NUM_PTES; ++i) { 881 for (;;) { 882 pte = dest[i]; 883 if (pte == src[i]) 884 break; 885 if (cas64(dest + i, pte, src[i]) != src[i]) 886 break; 887 } 888 } 889 } 890 #endif 891 892 /* 893 * Switch to a new active hat, maintaining bit masks to track active CPUs. 894 */ 895 void 896 hat_switch(hat_t *hat) 897 { 898 uintptr_t newcr3; 899 cpu_t *cpu = CPU; 900 hat_t *old = cpu->cpu_current_hat; 901 902 /* 903 * set up this information first, so we don't miss any cross calls 904 */ 905 if (old != NULL) { 906 if (old == hat) 907 return; 908 if (old != kas.a_hat) 909 CPUSET_ATOMIC_DEL(old->hat_cpus, cpu->cpu_id); 910 } 911 912 /* 913 * Wait for any in flight pagetable invalidates on this hat to finish. 914 * This is a spin lock at DISP_LEVEL 915 */ 916 if (hat != kas.a_hat) { 917 mutex_enter(&hat->hat_switch_mutex); 918 CPUSET_ATOMIC_ADD(hat->hat_cpus, cpu->cpu_id); 919 mutex_exit(&hat->hat_switch_mutex); 920 } 921 cpu->cpu_current_hat = hat; 922 923 /* 924 * now go ahead and load cr3 925 */ 926 if (hat->hat_flags & HAT_VLP) { 927 #if defined(__amd64) 928 x86pte_t *vlpptep = cpu->cpu_hat_info->hci_vlp_l2ptes; 929 930 VLP_COPY(hat->hat_vlp_ptes, vlpptep); 931 newcr3 = MAKECR3(cpu->cpu_hat_info->hci_vlp_pfn); 932 #elif defined(__i386) 933 reload_pae32(hat, cpu); 934 newcr3 = MAKECR3(kas.a_hat->hat_htable->ht_pfn) + 935 (cpu->cpu_id + 1) * VLP_SIZE; 936 #endif 937 } else { 938 newcr3 = MAKECR3(hat->hat_htable->ht_pfn); 939 } 940 setcr3(newcr3); 941 ASSERT(cpu == CPU); 942 } 943 944 /* 945 * Utility to return a valid x86pte_t from protections, pfn, and level number 946 */ 947 static x86pte_t 948 hati_mkpte(pfn_t pfn, uint_t attr, level_t level, uint_t flags) 949 { 950 x86pte_t pte; 951 uint_t cache_attr = attr & HAT_ORDER_MASK; 952 953 pte = MAKEPTE(pfn, level); 954 955 if (attr & PROT_WRITE) 956 PTE_SET(pte, PT_WRITABLE); 957 958 if (attr & PROT_USER) 959 PTE_SET(pte, PT_USER); 960 961 if (!(attr & PROT_EXEC)) 962 PTE_SET(pte, mmu.pt_nx); 963 964 /* 965 * Set the software bits used track ref/mod sync's and hments. 966 * If not using REF/MOD, set them to avoid h/w rewriting PTEs. 967 */ 968 if (flags & HAT_LOAD_NOCONSIST) 969 PTE_SET(pte, PT_NOCONSIST | PT_REF | PT_MOD); 970 else if (attr & HAT_NOSYNC) 971 PTE_SET(pte, PT_NOSYNC | PT_REF | PT_MOD); 972 973 /* 974 * Set the caching attributes in the PTE. The combination 975 * of attributes are poorly defined, so we pay attention 976 * to them in the given order. 977 * 978 * The test for HAT_STRICTORDER is different because it's defined 979 * as "0" - which was a stupid thing to do, but is too late to change! 980 */ 981 if (cache_attr == HAT_STRICTORDER) { 982 PTE_SET(pte, PT_NOCACHE); 983 /*LINTED [Lint hates empty ifs, but it's the obvious way to do this] */ 984 } else if (cache_attr & (HAT_UNORDERED_OK | HAT_STORECACHING_OK)) { 985 /* nothing to set */; 986 } else if (cache_attr & (HAT_MERGING_OK | HAT_LOADCACHING_OK)) { 987 PTE_SET(pte, PT_NOCACHE); 988 if (x86_feature & X86_PAT) 989 PTE_SET(pte, (level == 0) ? PT_PAT_4K : PT_PAT_LARGE); 990 else 991 PTE_SET(pte, PT_WRITETHRU); 992 } else { 993 panic("hati_mkpte(): bad caching attributes: %x\n", cache_attr); 994 } 995 996 return (pte); 997 } 998 999 /* 1000 * Duplicate address translations of the parent to the child. 1001 * This function really isn't used anymore. 1002 */ 1003 /*ARGSUSED*/ 1004 int 1005 hat_dup(hat_t *old, hat_t *new, caddr_t addr, size_t len, uint_t flag) 1006 { 1007 ASSERT((uintptr_t)addr < kernelbase); 1008 ASSERT(new != kas.a_hat); 1009 ASSERT(old != kas.a_hat); 1010 return (0); 1011 } 1012 1013 /* 1014 * Allocate any hat resources required for a process being swapped in. 1015 */ 1016 /*ARGSUSED*/ 1017 void 1018 hat_swapin(hat_t *hat) 1019 { 1020 /* do nothing - we let everything fault back in */ 1021 } 1022 1023 /* 1024 * Unload all translations associated with an address space of a process 1025 * that is being swapped out. 1026 */ 1027 void 1028 hat_swapout(hat_t *hat) 1029 { 1030 uintptr_t vaddr = (uintptr_t)0; 1031 uintptr_t eaddr = _userlimit; 1032 htable_t *ht = NULL; 1033 level_t l; 1034 1035 /* 1036 * We can't just call hat_unload(hat, 0, _userlimit...) here, because 1037 * seg_spt and shared pagetables can't be swapped out. 1038 * Take a look at segspt_shmswapout() - it's a big no-op. 1039 * 1040 * Instead we'll walk through all the address space and unload 1041 * any mappings which we are sure are not shared, not locked. 1042 */ 1043 ASSERT(IS_PAGEALIGNED(vaddr)); 1044 ASSERT(IS_PAGEALIGNED(eaddr)); 1045 ASSERT(AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock)); 1046 if ((uintptr_t)hat->hat_as->a_userlimit < eaddr) 1047 eaddr = (uintptr_t)hat->hat_as->a_userlimit; 1048 1049 while (vaddr < eaddr) { 1050 (void) htable_walk(hat, &ht, &vaddr, eaddr); 1051 if (ht == NULL) 1052 break; 1053 1054 ASSERT(!IN_VA_HOLE(vaddr)); 1055 1056 /* 1057 * If the page table is shared skip its entire range. 1058 * This code knows that only level 0 page tables are shared 1059 */ 1060 l = ht->ht_level; 1061 if (ht->ht_flags & HTABLE_SHARED_PFN) { 1062 ASSERT(l == 0); 1063 vaddr = ht->ht_vaddr + LEVEL_SIZE(1); 1064 htable_release(ht); 1065 ht = NULL; 1066 continue; 1067 } 1068 1069 /* 1070 * If the page table has no locked entries, unload this one. 1071 */ 1072 if (ht->ht_lock_cnt == 0) 1073 hat_unload(hat, (caddr_t)vaddr, LEVEL_SIZE(l), 1074 HAT_UNLOAD_UNMAP); 1075 1076 /* 1077 * If we have a level 0 page table with locked entries, 1078 * skip the entire page table, otherwise skip just one entry. 1079 */ 1080 if (ht->ht_lock_cnt > 0 && l == 0) 1081 vaddr = ht->ht_vaddr + LEVEL_SIZE(1); 1082 else 1083 vaddr += LEVEL_SIZE(l); 1084 } 1085 if (ht) 1086 htable_release(ht); 1087 1088 /* 1089 * We're in swapout because the system is low on memory, so 1090 * go back and flush all the htables off the cached list. 1091 */ 1092 htable_purge_hat(hat); 1093 } 1094 1095 /* 1096 * returns number of bytes that have valid mappings in hat. 1097 */ 1098 size_t 1099 hat_get_mapped_size(hat_t *hat) 1100 { 1101 size_t total = 0; 1102 int l; 1103 1104 for (l = 0; l <= mmu.max_page_level; l++) 1105 total += (hat->hat_pages_mapped[l] << LEVEL_SHIFT(l)); 1106 1107 return (total); 1108 } 1109 1110 /* 1111 * enable/disable collection of stats for hat. 1112 */ 1113 int 1114 hat_stats_enable(hat_t *hat) 1115 { 1116 atomic_add_32(&hat->hat_stats, 1); 1117 return (1); 1118 } 1119 1120 void 1121 hat_stats_disable(hat_t *hat) 1122 { 1123 atomic_add_32(&hat->hat_stats, -1); 1124 } 1125 1126 /* 1127 * Utility to sync the ref/mod bits from a page table entry to the page_t 1128 * We must be holding the mapping list lock when this is called. 1129 */ 1130 static void 1131 hati_sync_pte_to_page(page_t *pp, x86pte_t pte, level_t level) 1132 { 1133 uint_t rm = 0; 1134 pgcnt_t pgcnt; 1135 1136 if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC) 1137 return; 1138 1139 if (PTE_GET(pte, PT_REF)) 1140 rm |= P_REF; 1141 1142 if (PTE_GET(pte, PT_MOD)) 1143 rm |= P_MOD; 1144 1145 if (rm == 0) 1146 return; 1147 1148 /* 1149 * sync to all constituent pages of a large page 1150 */ 1151 ASSERT(x86_hm_held(pp)); 1152 pgcnt = page_get_pagecnt(level); 1153 ASSERT(IS_P2ALIGNED(pp->p_pagenum, pgcnt)); 1154 for (; pgcnt > 0; --pgcnt) { 1155 /* 1156 * hat_page_demote() can't decrease 1157 * pszc below this mapping size 1158 * since this large mapping existed after we 1159 * took mlist lock. 1160 */ 1161 ASSERT(pp->p_szc >= level); 1162 hat_page_setattr(pp, rm); 1163 ++pp; 1164 } 1165 } 1166 1167 /* 1168 * This the set of PTE bits for PFN, permissions and caching 1169 * that require a TLB flush (hat_tlb_inval) if changed on a HAT_LOAD_REMAP 1170 */ 1171 #define PT_REMAP_BITS \ 1172 (PT_PADDR | PT_NX | PT_WRITABLE | PT_WRITETHRU | \ 1173 PT_NOCACHE | PT_PAT_4K | PT_PAT_LARGE) 1174 1175 #define REMAPASSERT(EX) if (!(EX)) panic("hati_pte_map: " #EX) 1176 /* 1177 * Do the low-level work to get a mapping entered into a HAT's pagetables 1178 * and in the mapping list of the associated page_t. 1179 */ 1180 static int 1181 hati_pte_map( 1182 htable_t *ht, 1183 uint_t entry, 1184 page_t *pp, 1185 x86pte_t pte, 1186 int flags, 1187 void *pte_ptr) 1188 { 1189 hat_t *hat = ht->ht_hat; 1190 x86pte_t old_pte; 1191 level_t l = ht->ht_level; 1192 hment_t *hm; 1193 uint_t is_consist; 1194 int rv = 0; 1195 1196 /* 1197 * Is this a consistant (ie. need mapping list lock) mapping? 1198 */ 1199 is_consist = (pp != NULL && (flags & HAT_LOAD_NOCONSIST) == 0); 1200 1201 /* 1202 * Track locked mapping count in the htable. Do this first, 1203 * as we track locking even if there already is a mapping present. 1204 */ 1205 if ((flags & HAT_LOAD_LOCK) != 0 && hat != kas.a_hat) 1206 HTABLE_LOCK_INC(ht); 1207 1208 /* 1209 * Acquire the page's mapping list lock and get an hment to use. 1210 * Note that hment_prepare() might return NULL. 1211 */ 1212 if (is_consist) { 1213 x86_hm_enter(pp); 1214 hm = hment_prepare(ht, entry, pp); 1215 } 1216 1217 /* 1218 * Set the new pte, retrieving the old one at the same time. 1219 */ 1220 old_pte = x86pte_set(ht, entry, pte, pte_ptr); 1221 1222 /* 1223 * did we get a large page / page table collision? 1224 */ 1225 if (old_pte == LPAGE_ERROR) { 1226 rv = -1; 1227 goto done; 1228 } 1229 1230 /* 1231 * If the mapping didn't change there is nothing more to do. 1232 */ 1233 if (PTE_EQUIV(pte, old_pte)) 1234 goto done; 1235 1236 /* 1237 * Install a new mapping in the page's mapping list 1238 */ 1239 if (!PTE_ISVALID(old_pte)) { 1240 if (is_consist) { 1241 hment_assign(ht, entry, pp, hm); 1242 x86_hm_exit(pp); 1243 } else { 1244 ASSERT(flags & HAT_LOAD_NOCONSIST); 1245 } 1246 HTABLE_INC(ht->ht_valid_cnt); 1247 PGCNT_INC(hat, l); 1248 return (rv); 1249 } 1250 1251 /* 1252 * Remap's are more complicated: 1253 * - HAT_LOAD_REMAP must be specified if changing the pfn. 1254 * We also require that NOCONSIST be specified. 1255 * - Otherwise only permission or caching bits may change. 1256 */ 1257 if (!PTE_ISPAGE(old_pte, l)) 1258 panic("non-null/page mapping pte=" FMT_PTE, old_pte); 1259 1260 if (PTE2PFN(old_pte, l) != PTE2PFN(pte, l)) { 1261 REMAPASSERT(flags & HAT_LOAD_REMAP); 1262 REMAPASSERT(flags & HAT_LOAD_NOCONSIST); 1263 REMAPASSERT(PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST); 1264 REMAPASSERT(pf_is_memory(PTE2PFN(old_pte, l)) == 1265 pf_is_memory(PTE2PFN(pte, l))); 1266 REMAPASSERT(!is_consist); 1267 } 1268 1269 /* 1270 * We only let remaps change the bits for PFNs, permissions 1271 * or caching type. 1272 */ 1273 ASSERT(PTE_GET(old_pte, ~(PT_REMAP_BITS | PT_REF | PT_MOD)) == 1274 PTE_GET(pte, ~PT_REMAP_BITS)); 1275 1276 /* 1277 * We don't create any mapping list entries on a remap, so release 1278 * any allocated hment after we drop the mapping list lock. 1279 */ 1280 done: 1281 if (is_consist) { 1282 x86_hm_exit(pp); 1283 if (hm != NULL) 1284 hment_free(hm); 1285 } 1286 return (rv); 1287 } 1288 1289 /* 1290 * The t_hatdepth field is an 8-bit counter. We use the lower seven bits 1291 * to track exactly how deep we are in the memload->kmem_alloc recursion. 1292 * If the depth is greater than 1, that indicates that we are performing a 1293 * hat operation to satisfy another hat operation. To prevent infinite 1294 * recursion, we switch over to using pre-allocated "reserves" of htables 1295 * and hments. 1296 * 1297 * The uppermost bit is used to indicate that we are transitioning away 1298 * from being the reserves thread. See hati_reserves_exit() for the 1299 * details. 1300 */ 1301 #define EXITING_FLAG (1 << 7) 1302 #define DEPTH_MASK (~EXITING_FLAG) 1303 #define HAT_DEPTH(t) ((t)->t_hatdepth & DEPTH_MASK) 1304 #define EXITING_RESERVES(t) ((t)->t_hatdepth & EXITING_FLAG) 1305 1306 /* 1307 * Access to reserves for HAT_NO_KALLOC is single threaded. 1308 * If someone else is in the reserves, we'll politely wait for them 1309 * to finish. This keeps normal hat_memload()s from eating up 1310 * the mappings needed to replenish the reserve. 1311 */ 1312 static void 1313 hati_reserves_enter(uint_t kmem_for_hat) 1314 { 1315 /* 1316 * 64 is an arbitrary number to catch serious problems. I'm not 1317 * sure what the absolute maximum depth is, but it should be 1318 * substantially less than this. 1319 */ 1320 ASSERT(HAT_DEPTH(curthread) < 64); 1321 1322 /* 1323 * If we are doing a memload to satisfy a kmem operation, we enter 1324 * the reserves immediately; we don't wait to recurse to a second 1325 * level of memload. 1326 */ 1327 ASSERT(kmem_for_hat < 2); 1328 curthread->t_hatdepth += (1 + kmem_for_hat); 1329 1330 if (hat_reserves_thread == curthread || use_boot_reserve) 1331 return; 1332 1333 if (HAT_DEPTH(curthread) > 1 || hat_reserves_thread != NULL) { 1334 mutex_enter(&hat_reserves_lock); 1335 while (hat_reserves_thread != NULL) 1336 cv_wait(&hat_reserves_cv, &hat_reserves_lock); 1337 1338 if (HAT_DEPTH(curthread) > 1) 1339 hat_reserves_thread = curthread; 1340 1341 mutex_exit(&hat_reserves_lock); 1342 } 1343 } 1344 1345 /* 1346 * If we are the reserves_thread and we've finally finished with all our 1347 * memloads (ie. no longer doing hat slabs), we can release our use of the 1348 * reserve. 1349 */ 1350 static void 1351 hati_reserves_exit(uint_t kmem_for_hat) 1352 { 1353 ASSERT(kmem_for_hat < 2); 1354 curthread->t_hatdepth -= (1 + kmem_for_hat); 1355 1356 /* 1357 * Simple case: either we are not the reserves thread, or we are 1358 * the reserves thread and we are nested deeply enough that we 1359 * should still be the reserves thread. 1360 * 1361 * Note: we may not become the reserves thread after we recursively 1362 * enter our second HAT routine, but we don't stop being the 1363 * reserves thread until we exit the toplevel HAT routine. This is 1364 * to work around vmem's inability to determine when an allocation 1365 * should be satisfied from the hat_memload arena, which can lead 1366 * to an infinite loop of memload->vmem_populate->memload->. 1367 */ 1368 if (curthread != hat_reserves_thread || HAT_DEPTH(curthread) > 0 || 1369 use_boot_reserve) 1370 return; 1371 1372 mutex_enter(&hat_reserves_lock); 1373 ASSERT(hat_reserves_thread == curthread); 1374 hat_reserves_thread = NULL; 1375 cv_broadcast(&hat_reserves_cv); 1376 mutex_exit(&hat_reserves_lock); 1377 1378 /* 1379 * As we leave the reserves, we want to be sure the reserve lists 1380 * aren't overstocked. Freeing excess reserves requires that we 1381 * call kmem_free(), which may require additional allocations, 1382 * causing us to re-enter the reserves. To avoid infinite 1383 * recursion, we only try to adjust reserves at the very top level. 1384 */ 1385 if (!kmem_for_hat && !EXITING_RESERVES(curthread)) { 1386 curthread->t_hatdepth |= EXITING_FLAG; 1387 htable_adjust_reserve(); 1388 hment_adjust_reserve(); 1389 curthread->t_hatdepth &= (~EXITING_FLAG); 1390 } 1391 1392 /* 1393 * just in case something went wrong in doing adjust reserves 1394 */ 1395 ASSERT(hat_reserves_thread != curthread); 1396 } 1397 1398 /* 1399 * Internal routine to load a single page table entry. This only fails if 1400 * we attempt to overwrite a page table link with a large page. 1401 */ 1402 static int 1403 hati_load_common( 1404 hat_t *hat, 1405 uintptr_t va, 1406 page_t *pp, 1407 uint_t attr, 1408 uint_t flags, 1409 level_t level, 1410 pfn_t pfn) 1411 { 1412 htable_t *ht; 1413 uint_t entry; 1414 x86pte_t pte; 1415 uint_t kmem_for_hat = (flags & HAT_NO_KALLOC) ? 1 : 0; 1416 int rv = 0; 1417 1418 ASSERT(hat == kas.a_hat || 1419 AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock)); 1420 1421 if (flags & HAT_LOAD_SHARE) 1422 hat->hat_flags |= HAT_SHARED; 1423 1424 /* 1425 * Find the page table that maps this page if it already exists. 1426 */ 1427 ht = htable_lookup(hat, va, level); 1428 1429 /* 1430 * All threads go through hati_reserves_enter() to at least wait 1431 * for any existing reserves user to finish. This helps reduce 1432 * pressure on the reserves. In addition, if this thread needs 1433 * to become the new reserve user it will. 1434 */ 1435 hati_reserves_enter(kmem_for_hat); 1436 1437 ASSERT(HAT_DEPTH(curthread) == 1 || va >= kernelbase); 1438 1439 /* 1440 * Kernel memloads for HAT data should never use hments! 1441 * If it did that would seriously complicate the reserves system, since 1442 * hment_alloc() would need to know about HAT_NO_KALLOC. 1443 * 1444 * We also must have HAT_LOAD_NOCONSIST if page_t is NULL. 1445 */ 1446 if (HAT_DEPTH(curthread) > 1 || pp == NULL) 1447 flags |= HAT_LOAD_NOCONSIST; 1448 1449 if (ht == NULL) { 1450 ht = htable_create(hat, va, level, NULL); 1451 ASSERT(ht != NULL); 1452 } 1453 entry = htable_va2entry(va, ht); 1454 1455 /* 1456 * a bunch of paranoid error checking 1457 */ 1458 ASSERT(ht->ht_busy > 0); 1459 if (ht->ht_vaddr > va || va > HTABLE_LAST_PAGE(ht)) 1460 panic("hati_load_common: bad htable %p, va %p", ht, (void *)va); 1461 ASSERT(ht->ht_level == level); 1462 1463 /* 1464 * construct the new PTE 1465 */ 1466 if (hat == kas.a_hat) 1467 attr &= ~PROT_USER; 1468 pte = hati_mkpte(pfn, attr, level, flags); 1469 if (hat == kas.a_hat && va >= kernelbase) 1470 PTE_SET(pte, mmu.pt_global); 1471 1472 /* 1473 * establish the mapping 1474 */ 1475 rv = hati_pte_map(ht, entry, pp, pte, flags, NULL); 1476 1477 /* 1478 * release the htable and any reserves 1479 */ 1480 htable_release(ht); 1481 hati_reserves_exit(kmem_for_hat); 1482 return (rv); 1483 } 1484 1485 /* 1486 * special case of hat_memload to deal with some kernel addrs for performance 1487 */ 1488 static void 1489 hat_kmap_load( 1490 caddr_t addr, 1491 page_t *pp, 1492 uint_t attr, 1493 uint_t flags) 1494 { 1495 uintptr_t va = (uintptr_t)addr; 1496 x86pte_t pte; 1497 pfn_t pfn = page_pptonum(pp); 1498 pgcnt_t pg_off = mmu_btop(va - mmu.kmap_addr); 1499 htable_t *ht; 1500 uint_t entry; 1501 void *pte_ptr; 1502 1503 /* 1504 * construct the requested PTE 1505 */ 1506 attr &= ~PROT_USER; 1507 attr |= HAT_STORECACHING_OK; 1508 pte = hati_mkpte(pfn, attr, 0, flags); 1509 PTE_SET(pte, mmu.pt_global); 1510 1511 /* 1512 * Figure out the pte_ptr and htable and use common code to finish up 1513 */ 1514 if (mmu.pae_hat) 1515 pte_ptr = mmu.kmap_ptes + pg_off; 1516 else 1517 pte_ptr = (x86pte32_t *)mmu.kmap_ptes + pg_off; 1518 ht = mmu.kmap_htables[(va - mmu.kmap_htables[0]->ht_vaddr) >> 1519 LEVEL_SHIFT(1)]; 1520 entry = htable_va2entry(va, ht); 1521 (void) hati_pte_map(ht, entry, pp, pte, flags, pte_ptr); 1522 } 1523 1524 /* 1525 * hat_memload() - load a translation to the given page struct 1526 * 1527 * Flags for hat_memload/hat_devload/hat_*attr. 1528 * 1529 * HAT_LOAD Default flags to load a translation to the page. 1530 * 1531 * HAT_LOAD_LOCK Lock down mapping resources; hat_map(), hat_memload(), 1532 * and hat_devload(). 1533 * 1534 * HAT_LOAD_NOCONSIST Do not add mapping to page_t mapping list. 1535 * sets PT_NOCONSIST 1536 * 1537 * HAT_LOAD_SHARE A flag to hat_memload() to indicate h/w page tables 1538 * that map some user pages (not kas) is shared by more 1539 * than one process (eg. ISM). 1540 * 1541 * HAT_LOAD_REMAP Reload a valid pte with a different page frame. 1542 * 1543 * HAT_NO_KALLOC Do not kmem_alloc while creating the mapping; at this 1544 * point, it's setting up mapping to allocate internal 1545 * hat layer data structures. This flag forces hat layer 1546 * to tap its reserves in order to prevent infinite 1547 * recursion. 1548 * 1549 * The following is a protection attribute (like PROT_READ, etc.) 1550 * 1551 * HAT_NOSYNC set PT_NOSYNC - this mapping's ref/mod bits 1552 * are never cleared. 1553 * 1554 * Installing new valid PTE's and creation of the mapping list 1555 * entry are controlled under the same lock. It's derived from the 1556 * page_t being mapped. 1557 */ 1558 static uint_t supported_memload_flags = 1559 HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_ADV | HAT_LOAD_NOCONSIST | 1560 HAT_LOAD_SHARE | HAT_NO_KALLOC | HAT_LOAD_REMAP | HAT_LOAD_TEXT; 1561 1562 void 1563 hat_memload( 1564 hat_t *hat, 1565 caddr_t addr, 1566 page_t *pp, 1567 uint_t attr, 1568 uint_t flags) 1569 { 1570 uintptr_t va = (uintptr_t)addr; 1571 level_t level = 0; 1572 pfn_t pfn = page_pptonum(pp); 1573 1574 HATIN(hat_memload, hat, addr, (size_t)MMU_PAGESIZE); 1575 ASSERT(IS_PAGEALIGNED(va)); 1576 ASSERT(hat == kas.a_hat || va < _userlimit); 1577 ASSERT(hat == kas.a_hat || 1578 AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock)); 1579 ASSERT((flags & supported_memload_flags) == flags); 1580 1581 ASSERT(!IN_VA_HOLE(va)); 1582 ASSERT(!PP_ISFREE(pp)); 1583 1584 /* 1585 * kernel address special case for performance. 1586 */ 1587 if (mmu.kmap_addr <= va && va < mmu.kmap_eaddr) { 1588 ASSERT(hat == kas.a_hat); 1589 hat_kmap_load(addr, pp, attr, flags); 1590 return; 1591 } 1592 1593 /* 1594 * This is used for memory with normal caching enabled, so 1595 * always set HAT_STORECACHING_OK. 1596 */ 1597 attr |= HAT_STORECACHING_OK; 1598 if (hati_load_common(hat, va, pp, attr, flags, level, pfn) != 0) 1599 panic("unexpected hati_load_common() failure"); 1600 HATOUT(hat_memload, hat, addr); 1601 } 1602 1603 /* 1604 * Load the given array of page structs using large pages when possible 1605 */ 1606 void 1607 hat_memload_array( 1608 hat_t *hat, 1609 caddr_t addr, 1610 size_t len, 1611 page_t **pages, 1612 uint_t attr, 1613 uint_t flags) 1614 { 1615 uintptr_t va = (uintptr_t)addr; 1616 uintptr_t eaddr = va + len; 1617 level_t level; 1618 size_t pgsize; 1619 pgcnt_t pgindx = 0; 1620 pfn_t pfn; 1621 pgcnt_t i; 1622 1623 HATIN(hat_memload_array, hat, addr, len); 1624 ASSERT(IS_PAGEALIGNED(va)); 1625 ASSERT(hat == kas.a_hat || va + len <= _userlimit); 1626 ASSERT(hat == kas.a_hat || 1627 AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock)); 1628 ASSERT((flags & supported_memload_flags) == flags); 1629 1630 /* 1631 * memload is used for memory with full caching enabled, so 1632 * set HAT_STORECACHING_OK. 1633 */ 1634 attr |= HAT_STORECACHING_OK; 1635 1636 /* 1637 * handle all pages using largest possible pagesize 1638 */ 1639 while (va < eaddr) { 1640 /* 1641 * decide what level mapping to use (ie. pagesize) 1642 */ 1643 pfn = page_pptonum(pages[pgindx]); 1644 for (level = mmu.max_page_level; ; --level) { 1645 pgsize = LEVEL_SIZE(level); 1646 if (level == 0) 1647 break; 1648 1649 if (!IS_P2ALIGNED(va, pgsize) || 1650 (eaddr - va) < pgsize || 1651 !IS_P2ALIGNED(pfn_to_pa(pfn), pgsize)) 1652 continue; 1653 1654 /* 1655 * To use a large mapping of this size, all the 1656 * pages we are passed must be sequential subpages 1657 * of the large page. 1658 * hat_page_demote() can't change p_szc because 1659 * all pages are locked. 1660 */ 1661 if (pages[pgindx]->p_szc >= level) { 1662 for (i = 0; i < mmu_btop(pgsize); ++i) { 1663 if (pfn + i != 1664 page_pptonum(pages[pgindx + i])) 1665 break; 1666 ASSERT(pages[pgindx + i]->p_szc >= 1667 level); 1668 ASSERT(pages[pgindx] + i == 1669 pages[pgindx + i]); 1670 } 1671 if (i == mmu_btop(pgsize)) 1672 break; 1673 } 1674 } 1675 1676 /* 1677 * Load this page mapping. If the load fails, try a smaller 1678 * pagesize. 1679 */ 1680 ASSERT(!IN_VA_HOLE(va)); 1681 while (hati_load_common(hat, va, pages[pgindx], attr, 1682 flags, level, pfn) != 0) { 1683 if (level == 0) 1684 panic("unexpected hati_load_common() failure"); 1685 --level; 1686 pgsize = LEVEL_SIZE(level); 1687 } 1688 1689 /* 1690 * move to next page 1691 */ 1692 va += pgsize; 1693 pgindx += mmu_btop(pgsize); 1694 } 1695 HATOUT(hat_memload_array, hat, addr); 1696 } 1697 1698 /* 1699 * void hat_devload(hat, addr, len, pf, attr, flags) 1700 * load/lock the given page frame number 1701 * 1702 * Advisory ordering attributes. Apply only to device mappings. 1703 * 1704 * HAT_STRICTORDER: the CPU must issue the references in order, as the 1705 * programmer specified. This is the default. 1706 * HAT_UNORDERED_OK: the CPU may reorder the references (this is all kinds 1707 * of reordering; store or load with store or load). 1708 * HAT_MERGING_OK: merging and batching: the CPU may merge individual stores 1709 * to consecutive locations (for example, turn two consecutive byte 1710 * stores into one halfword store), and it may batch individual loads 1711 * (for example, turn two consecutive byte loads into one halfword load). 1712 * This also implies re-ordering. 1713 * HAT_LOADCACHING_OK: the CPU may cache the data it fetches and reuse it 1714 * until another store occurs. The default is to fetch new data 1715 * on every load. This also implies merging. 1716 * HAT_STORECACHING_OK: the CPU may keep the data in the cache and push it to 1717 * the device (perhaps with other data) at a later time. The default is 1718 * to push the data right away. This also implies load caching. 1719 * 1720 * Equivalent of hat_memload(), but can be used for device memory where 1721 * there are no page_t's and we support additional flags (write merging, etc). 1722 * Note that we can have large page mappings with this interface. 1723 */ 1724 int supported_devload_flags = HAT_LOAD | HAT_LOAD_LOCK | 1725 HAT_LOAD_NOCONSIST | HAT_STRICTORDER | HAT_UNORDERED_OK | 1726 HAT_MERGING_OK | HAT_LOADCACHING_OK | HAT_STORECACHING_OK; 1727 1728 void 1729 hat_devload( 1730 hat_t *hat, 1731 caddr_t addr, 1732 size_t len, 1733 pfn_t pfn, 1734 uint_t attr, 1735 int flags) 1736 { 1737 uintptr_t va = ALIGN2PAGE(addr); 1738 uintptr_t eva = va + len; 1739 level_t level; 1740 size_t pgsize; 1741 page_t *pp; 1742 int f; /* per PTE copy of flags - maybe modified */ 1743 uint_t a; /* per PTE copy of attr */ 1744 1745 HATIN(hat_devload, hat, addr, len); 1746 ASSERT(IS_PAGEALIGNED(va)); 1747 ASSERT(hat == kas.a_hat || eva <= _userlimit); 1748 ASSERT(hat == kas.a_hat || 1749 AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock)); 1750 ASSERT((flags & supported_devload_flags) == flags); 1751 1752 /* 1753 * handle all pages 1754 */ 1755 while (va < eva) { 1756 1757 /* 1758 * decide what level mapping to use (ie. pagesize) 1759 */ 1760 for (level = mmu.max_page_level; ; --level) { 1761 pgsize = LEVEL_SIZE(level); 1762 if (level == 0) 1763 break; 1764 if (IS_P2ALIGNED(va, pgsize) && 1765 (eva - va) >= pgsize && 1766 IS_P2ALIGNED(pfn, mmu_btop(pgsize))) 1767 break; 1768 } 1769 1770 /* 1771 * If this is just memory then allow caching (this happens 1772 * for the nucleus pages) - though HAT_PLAT_NOCACHE can be used 1773 * to override that. If we don't have a page_t then make sure 1774 * NOCONSIST is set. 1775 */ 1776 a = attr; 1777 f = flags; 1778 if (pf_is_memory(pfn)) { 1779 if (!(a & HAT_PLAT_NOCACHE)) 1780 a |= HAT_STORECACHING_OK; 1781 1782 if (f & HAT_LOAD_NOCONSIST) 1783 pp = NULL; 1784 else 1785 pp = page_numtopp_nolock(pfn); 1786 } else { 1787 pp = NULL; 1788 f |= HAT_LOAD_NOCONSIST; 1789 } 1790 1791 /* 1792 * load this page mapping 1793 */ 1794 ASSERT(!IN_VA_HOLE(va)); 1795 while (hati_load_common(hat, va, pp, a, f, level, pfn) != 0) { 1796 if (level == 0) 1797 panic("unexpected hati_load_common() failure"); 1798 --level; 1799 pgsize = LEVEL_SIZE(level); 1800 } 1801 1802 /* 1803 * move to next page 1804 */ 1805 va += pgsize; 1806 pfn += mmu_btop(pgsize); 1807 } 1808 HATOUT(hat_devload, hat, addr); 1809 } 1810 1811 /* 1812 * void hat_unlock(hat, addr, len) 1813 * unlock the mappings to a given range of addresses 1814 * 1815 * Locks are tracked by ht_lock_cnt in the htable. 1816 */ 1817 void 1818 hat_unlock(hat_t *hat, caddr_t addr, size_t len) 1819 { 1820 uintptr_t vaddr = (uintptr_t)addr; 1821 uintptr_t eaddr = vaddr + len; 1822 htable_t *ht = NULL; 1823 1824 /* 1825 * kernel entries are always locked, we don't track lock counts 1826 */ 1827 ASSERT(hat == kas.a_hat || eaddr <= _userlimit); 1828 ASSERT(IS_PAGEALIGNED(vaddr)); 1829 ASSERT(IS_PAGEALIGNED(eaddr)); 1830 if (hat == kas.a_hat) 1831 return; 1832 if (eaddr > _userlimit) 1833 panic("hat_unlock() address out of range - above _userlimit"); 1834 1835 ASSERT(AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock)); 1836 while (vaddr < eaddr) { 1837 (void) htable_walk(hat, &ht, &vaddr, eaddr); 1838 if (ht == NULL) 1839 break; 1840 1841 ASSERT(!IN_VA_HOLE(vaddr)); 1842 1843 if (ht->ht_lock_cnt < 1) 1844 panic("hat_unlock(): lock_cnt < 1, " 1845 "htable=%p, vaddr=%p\n", ht, (caddr_t)vaddr); 1846 HTABLE_LOCK_DEC(ht); 1847 1848 vaddr += LEVEL_SIZE(ht->ht_level); 1849 } 1850 if (ht) 1851 htable_release(ht); 1852 } 1853 1854 /* 1855 * Cross call service routine to demap a virtual page on 1856 * the current CPU or flush all mappings in TLB. 1857 */ 1858 /*ARGSUSED*/ 1859 static int 1860 hati_demap_func(xc_arg_t a1, xc_arg_t a2, xc_arg_t a3) 1861 { 1862 hat_t *hat = (hat_t *)a1; 1863 caddr_t addr = (caddr_t)a2; 1864 1865 /* 1866 * If the target hat isn't the kernel and this CPU isn't operating 1867 * in the target hat, we can ignore the cross call. 1868 */ 1869 if (hat != kas.a_hat && hat != CPU->cpu_current_hat) 1870 return (0); 1871 1872 /* 1873 * For a normal address, we just flush one page mapping 1874 */ 1875 if ((uintptr_t)addr != DEMAP_ALL_ADDR) { 1876 mmu_tlbflush_entry(addr); 1877 return (0); 1878 } 1879 1880 /* 1881 * Otherwise we reload cr3 to effect a complete TLB flush. 1882 * 1883 * A reload of cr3 on a VLP process also means we must also recopy in 1884 * the pte values from the struct hat 1885 */ 1886 if (hat->hat_flags & HAT_VLP) { 1887 #if defined(__amd64) 1888 x86pte_t *vlpptep = CPU->cpu_hat_info->hci_vlp_l2ptes; 1889 1890 VLP_COPY(hat->hat_vlp_ptes, vlpptep); 1891 #elif defined(__i386) 1892 reload_pae32(hat, CPU); 1893 #endif 1894 } 1895 reload_cr3(); 1896 return (0); 1897 } 1898 1899 /* 1900 * Internal routine to do cross calls to invalidate a range of pages on 1901 * all CPUs using a given hat. 1902 */ 1903 void 1904 hat_tlb_inval(hat_t *hat, uintptr_t va) 1905 { 1906 extern int flushes_require_xcalls; /* from mp_startup.c */ 1907 cpuset_t justme; 1908 cpuset_t cpus_to_shootdown; 1909 1910 /* 1911 * If the hat is being destroyed, there are no more users, so 1912 * demap need not do anything. 1913 */ 1914 if (hat->hat_flags & HAT_FREEING) 1915 return; 1916 1917 /* 1918 * If demapping from a shared pagetable, we best demap the 1919 * entire set of user TLBs, since we don't know what addresses 1920 * these were shared at. 1921 */ 1922 if (hat->hat_flags & HAT_SHARED) { 1923 hat = kas.a_hat; 1924 va = DEMAP_ALL_ADDR; 1925 } 1926 1927 /* 1928 * if not running with multiple CPUs, don't use cross calls 1929 */ 1930 if (panicstr || !flushes_require_xcalls) { 1931 (void) hati_demap_func((xc_arg_t)hat, (xc_arg_t)va, NULL); 1932 return; 1933 } 1934 1935 1936 /* 1937 * Determine CPUs to shootdown. Kernel changes always do all CPUs. 1938 * Otherwise it's just CPUs currently executing in this hat. 1939 */ 1940 kpreempt_disable(); 1941 CPUSET_ONLY(justme, CPU->cpu_id); 1942 if (hat == kas.a_hat) 1943 cpus_to_shootdown = khat_cpuset; 1944 else 1945 cpus_to_shootdown = hat->hat_cpus; 1946 1947 if (CPUSET_ISNULL(cpus_to_shootdown) || 1948 CPUSET_ISEQUAL(cpus_to_shootdown, justme)) { 1949 1950 (void) hati_demap_func((xc_arg_t)hat, (xc_arg_t)va, NULL); 1951 1952 } else { 1953 1954 CPUSET_ADD(cpus_to_shootdown, CPU->cpu_id); 1955 xc_call((xc_arg_t)hat, (xc_arg_t)va, NULL, X_CALL_HIPRI, 1956 cpus_to_shootdown, hati_demap_func); 1957 1958 } 1959 kpreempt_enable(); 1960 } 1961 1962 /* 1963 * Interior routine for HAT_UNLOADs from hat_unload_callback(), 1964 * hat_kmap_unload() OR from hat_steal() code. This routine doesn't 1965 * handle releasing of the htables. 1966 */ 1967 void 1968 hat_pte_unmap( 1969 htable_t *ht, 1970 uint_t entry, 1971 uint_t flags, 1972 x86pte_t old_pte, 1973 void *pte_ptr) 1974 { 1975 hat_t *hat = ht->ht_hat; 1976 hment_t *hm = NULL; 1977 page_t *pp = NULL; 1978 level_t l = ht->ht_level; 1979 pfn_t pfn; 1980 1981 /* 1982 * We always track the locking counts, even if nothing is unmapped 1983 */ 1984 if ((flags & HAT_UNLOAD_UNLOCK) != 0 && hat != kas.a_hat) { 1985 ASSERT(ht->ht_lock_cnt > 0); 1986 HTABLE_LOCK_DEC(ht); 1987 } 1988 1989 /* 1990 * Figure out which page's mapping list lock to acquire using the PFN 1991 * passed in "old" PTE. We then attempt to invalidate the PTE. 1992 * If another thread, probably a hat_pageunload, has asynchronously 1993 * unmapped/remapped this address we'll loop here. 1994 */ 1995 ASSERT(ht->ht_busy > 0); 1996 while (PTE_ISVALID(old_pte)) { 1997 pfn = PTE2PFN(old_pte, l); 1998 if (PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST) { 1999 pp = NULL; 2000 } else { 2001 pp = page_numtopp_nolock(pfn); 2002 if (pp == NULL) { 2003 panic("no page_t, not NOCONSIST: old_pte=" 2004 FMT_PTE " ht=%lx entry=0x%x pte_ptr=%lx", 2005 old_pte, (uintptr_t)ht, entry, 2006 (uintptr_t)pte_ptr); 2007 } 2008 x86_hm_enter(pp); 2009 } 2010 2011 /* 2012 * If freeing the address space, check that the PTE 2013 * hasn't changed, as the mappings are no longer in use by 2014 * any thread, invalidation is unnecessary. 2015 * If not freeing, do a full invalidate. 2016 */ 2017 if (hat->hat_flags & HAT_FREEING) 2018 old_pte = x86pte_get(ht, entry); 2019 else 2020 old_pte = x86pte_inval(ht, entry, old_pte, pte_ptr); 2021 2022 /* 2023 * If the page hadn't changed we've unmapped it and can proceed 2024 */ 2025 if (PTE_ISVALID(old_pte) && PTE2PFN(old_pte, l) == pfn) 2026 break; 2027 2028 /* 2029 * Otherwise, we'll have to retry with the current old_pte. 2030 * Drop the hment lock, since the pfn may have changed. 2031 */ 2032 if (pp != NULL) { 2033 x86_hm_exit(pp); 2034 pp = NULL; 2035 } else { 2036 ASSERT(PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST); 2037 } 2038 } 2039 2040 /* 2041 * If the old mapping wasn't valid, there's nothing more to do 2042 */ 2043 if (!PTE_ISVALID(old_pte)) { 2044 if (pp != NULL) 2045 x86_hm_exit(pp); 2046 return; 2047 } 2048 2049 /* 2050 * Take care of syncing any MOD/REF bits and removing the hment. 2051 */ 2052 if (pp != NULL) { 2053 if (!(flags & HAT_UNLOAD_NOSYNC)) 2054 hati_sync_pte_to_page(pp, old_pte, l); 2055 hm = hment_remove(pp, ht, entry); 2056 x86_hm_exit(pp); 2057 if (hm != NULL) 2058 hment_free(hm); 2059 } 2060 2061 /* 2062 * Handle book keeping in the htable and hat 2063 */ 2064 ASSERT(ht->ht_valid_cnt > 0); 2065 HTABLE_DEC(ht->ht_valid_cnt); 2066 PGCNT_DEC(hat, l); 2067 } 2068 2069 /* 2070 * very cheap unload implementation to special case some kernel addresses 2071 */ 2072 static void 2073 hat_kmap_unload(caddr_t addr, size_t len, uint_t flags) 2074 { 2075 uintptr_t va = (uintptr_t)addr; 2076 uintptr_t eva = va + len; 2077 pgcnt_t pg_index; 2078 htable_t *ht; 2079 uint_t entry; 2080 x86pte_t *pte_ptr; 2081 x86pte_t old_pte; 2082 2083 for (; va < eva; va += MMU_PAGESIZE) { 2084 /* 2085 * Get the PTE 2086 */ 2087 pg_index = mmu_btop(va - mmu.kmap_addr); 2088 pte_ptr = PT_INDEX_PTR(mmu.kmap_ptes, pg_index); 2089 old_pte = GET_PTE(pte_ptr); 2090 2091 /* 2092 * get the htable / entry 2093 */ 2094 ht = mmu.kmap_htables[(va - mmu.kmap_htables[0]->ht_vaddr) 2095 >> LEVEL_SHIFT(1)]; 2096 entry = htable_va2entry(va, ht); 2097 2098 /* 2099 * use mostly common code to unmap it. 2100 */ 2101 hat_pte_unmap(ht, entry, flags, old_pte, pte_ptr); 2102 } 2103 } 2104 2105 2106 /* 2107 * unload a range of virtual address space (no callback) 2108 */ 2109 void 2110 hat_unload(hat_t *hat, caddr_t addr, size_t len, uint_t flags) 2111 { 2112 uintptr_t va = (uintptr_t)addr; 2113 2114 ASSERT(hat == kas.a_hat || va + len <= _userlimit); 2115 2116 /* 2117 * special case for performance. 2118 */ 2119 if (mmu.kmap_addr <= va && va < mmu.kmap_eaddr) { 2120 ASSERT(hat == kas.a_hat); 2121 hat_kmap_unload(addr, len, flags); 2122 } else { 2123 hat_unload_callback(hat, addr, len, flags, NULL); 2124 } 2125 } 2126 2127 /* 2128 * Do the callbacks for ranges being unloaded. 2129 */ 2130 typedef struct range_info { 2131 uintptr_t rng_va; 2132 ulong_t rng_cnt; 2133 level_t rng_level; 2134 } range_info_t; 2135 2136 static void 2137 handle_ranges(hat_callback_t *cb, uint_t cnt, range_info_t *range) 2138 { 2139 /* 2140 * do callbacks to upper level VM system 2141 */ 2142 while (cb != NULL && cnt > 0) { 2143 --cnt; 2144 cb->hcb_start_addr = (caddr_t)range[cnt].rng_va; 2145 cb->hcb_end_addr = cb->hcb_start_addr; 2146 cb->hcb_end_addr += 2147 range[cnt].rng_cnt << LEVEL_SIZE(range[cnt].rng_level); 2148 cb->hcb_function(cb); 2149 } 2150 } 2151 2152 /* 2153 * Unload a given range of addresses (has optional callback) 2154 * 2155 * Flags: 2156 * define HAT_UNLOAD 0x00 2157 * define HAT_UNLOAD_NOSYNC 0x02 2158 * define HAT_UNLOAD_UNLOCK 0x04 2159 * define HAT_UNLOAD_OTHER 0x08 - not used 2160 * define HAT_UNLOAD_UNMAP 0x10 - same as HAT_UNLOAD 2161 */ 2162 #define MAX_UNLOAD_CNT (8) 2163 void 2164 hat_unload_callback( 2165 hat_t *hat, 2166 caddr_t addr, 2167 size_t len, 2168 uint_t flags, 2169 hat_callback_t *cb) 2170 { 2171 uintptr_t vaddr = (uintptr_t)addr; 2172 uintptr_t eaddr = vaddr + len; 2173 htable_t *ht = NULL; 2174 uint_t entry; 2175 uintptr_t contig_va = (uintptr_t)-1L; 2176 range_info_t r[MAX_UNLOAD_CNT]; 2177 uint_t r_cnt = 0; 2178 x86pte_t old_pte; 2179 2180 HATIN(hat_unload_callback, hat, addr, len); 2181 ASSERT(hat == kas.a_hat || eaddr <= _userlimit); 2182 ASSERT(IS_PAGEALIGNED(vaddr)); 2183 ASSERT(IS_PAGEALIGNED(eaddr)); 2184 2185 /* 2186 * Special case a single page being unloaded for speed. This happens 2187 * quite frequently, COW faults after a fork() for example. 2188 */ 2189 if (cb == NULL && len == MMU_PAGESIZE) { 2190 ht = htable_getpte(hat, vaddr, &entry, &old_pte, 0); 2191 if (ht != NULL) { 2192 if (PTE_ISVALID(old_pte)) 2193 hat_pte_unmap(ht, entry, flags, old_pte, NULL); 2194 htable_release(ht); 2195 } 2196 return; 2197 } 2198 2199 while (vaddr < eaddr) { 2200 old_pte = htable_walk(hat, &ht, &vaddr, eaddr); 2201 if (ht == NULL) 2202 break; 2203 2204 ASSERT(!IN_VA_HOLE(vaddr)); 2205 2206 if (vaddr < (uintptr_t)addr) 2207 panic("hat_unload_callback(): unmap inside large page"); 2208 2209 /* 2210 * We'll do the call backs for contiguous ranges 2211 */ 2212 if (vaddr != contig_va || 2213 (r_cnt > 0 && r[r_cnt - 1].rng_level != ht->ht_level)) { 2214 if (r_cnt == MAX_UNLOAD_CNT) { 2215 handle_ranges(cb, r_cnt, r); 2216 r_cnt = 0; 2217 } 2218 r[r_cnt].rng_va = vaddr; 2219 r[r_cnt].rng_cnt = 0; 2220 r[r_cnt].rng_level = ht->ht_level; 2221 ++r_cnt; 2222 } 2223 2224 /* 2225 * Unload one mapping from the page tables. 2226 */ 2227 entry = htable_va2entry(vaddr, ht); 2228 hat_pte_unmap(ht, entry, flags, old_pte, NULL); 2229 ASSERT(ht->ht_level <= mmu.max_page_level); 2230 vaddr += LEVEL_SIZE(ht->ht_level); 2231 contig_va = vaddr; 2232 ++r[r_cnt - 1].rng_cnt; 2233 } 2234 if (ht) 2235 htable_release(ht); 2236 2237 /* 2238 * handle last range for callbacks 2239 */ 2240 if (r_cnt > 0) 2241 handle_ranges(cb, r_cnt, r); 2242 2243 HATOUT(hat_unload_callback, hat, addr); 2244 } 2245 2246 /* 2247 * synchronize mapping with software data structures 2248 * 2249 * This interface is currently only used by the working set monitor 2250 * driver. 2251 */ 2252 /*ARGSUSED*/ 2253 void 2254 hat_sync(hat_t *hat, caddr_t addr, size_t len, uint_t flags) 2255 { 2256 uintptr_t vaddr = (uintptr_t)addr; 2257 uintptr_t eaddr = vaddr + len; 2258 htable_t *ht = NULL; 2259 uint_t entry; 2260 x86pte_t pte; 2261 x86pte_t save_pte; 2262 x86pte_t new; 2263 page_t *pp; 2264 2265 ASSERT(!IN_VA_HOLE(vaddr)); 2266 ASSERT(IS_PAGEALIGNED(vaddr)); 2267 ASSERT(IS_PAGEALIGNED(eaddr)); 2268 ASSERT(hat == kas.a_hat || eaddr <= _userlimit); 2269 2270 for (; vaddr < eaddr; vaddr += LEVEL_SIZE(ht->ht_level)) { 2271 try_again: 2272 pte = htable_walk(hat, &ht, &vaddr, eaddr); 2273 if (ht == NULL) 2274 break; 2275 entry = htable_va2entry(vaddr, ht); 2276 2277 if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC || 2278 PTE_GET(pte, PT_REF | PT_MOD) == 0) 2279 continue; 2280 2281 /* 2282 * We need to acquire the mapping list lock to protect 2283 * against hat_pageunload(), hat_unload(), etc. 2284 */ 2285 pp = page_numtopp_nolock(PTE2PFN(pte, ht->ht_level)); 2286 if (pp == NULL) 2287 break; 2288 x86_hm_enter(pp); 2289 save_pte = pte; 2290 pte = x86pte_get(ht, entry); 2291 if (pte != save_pte) { 2292 x86_hm_exit(pp); 2293 goto try_again; 2294 } 2295 if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC || 2296 PTE_GET(pte, PT_REF | PT_MOD) == 0) { 2297 x86_hm_exit(pp); 2298 continue; 2299 } 2300 2301 /* 2302 * Need to clear ref or mod bits. We may compete with 2303 * hardware updating the R/M bits and have to try again. 2304 */ 2305 if (flags == HAT_SYNC_ZERORM) { 2306 new = pte; 2307 PTE_CLR(new, PT_REF | PT_MOD); 2308 pte = hati_update_pte(ht, entry, pte, new); 2309 if (pte != 0) { 2310 x86_hm_exit(pp); 2311 goto try_again; 2312 } 2313 } else { 2314 /* 2315 * sync the PTE to the page_t 2316 */ 2317 hati_sync_pte_to_page(pp, save_pte, ht->ht_level); 2318 } 2319 x86_hm_exit(pp); 2320 } 2321 if (ht) 2322 htable_release(ht); 2323 } 2324 2325 /* 2326 * void hat_map(hat, addr, len, flags) 2327 */ 2328 /*ARGSUSED*/ 2329 void 2330 hat_map(hat_t *hat, caddr_t addr, size_t len, uint_t flags) 2331 { 2332 /* does nothing */ 2333 } 2334 2335 /* 2336 * uint_t hat_getattr(hat, addr, *attr) 2337 * returns attr for <hat,addr> in *attr. returns 0 if there was a 2338 * mapping and *attr is valid, nonzero if there was no mapping and 2339 * *attr is not valid. 2340 */ 2341 uint_t 2342 hat_getattr(hat_t *hat, caddr_t addr, uint_t *attr) 2343 { 2344 uintptr_t vaddr = ALIGN2PAGE(addr); 2345 htable_t *ht = NULL; 2346 x86pte_t pte; 2347 2348 ASSERT(hat == kas.a_hat || vaddr <= _userlimit); 2349 2350 if (IN_VA_HOLE(vaddr)) 2351 return ((uint_t)-1); 2352 2353 ht = htable_getpte(hat, vaddr, NULL, &pte, mmu.max_page_level); 2354 if (ht == NULL) 2355 return ((uint_t)-1); 2356 2357 if (!PTE_ISVALID(pte) || !PTE_ISPAGE(pte, ht->ht_level)) { 2358 htable_release(ht); 2359 return ((uint_t)-1); 2360 } 2361 2362 *attr = PROT_READ; 2363 if (PTE_GET(pte, PT_WRITABLE)) 2364 *attr |= PROT_WRITE; 2365 if (PTE_GET(pte, PT_USER)) 2366 *attr |= PROT_USER; 2367 if (!PTE_GET(pte, mmu.pt_nx)) 2368 *attr |= PROT_EXEC; 2369 if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC) 2370 *attr |= HAT_NOSYNC; 2371 htable_release(ht); 2372 return (0); 2373 } 2374 2375 /* 2376 * hat_updateattr() applies the given attribute change to an existing mapping 2377 */ 2378 #define HAT_LOAD_ATTR 1 2379 #define HAT_SET_ATTR 2 2380 #define HAT_CLR_ATTR 3 2381 2382 static void 2383 hat_updateattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr, int what) 2384 { 2385 uintptr_t vaddr = (uintptr_t)addr; 2386 uintptr_t eaddr = (uintptr_t)addr + len; 2387 htable_t *ht = NULL; 2388 uint_t entry; 2389 x86pte_t oldpte, newpte; 2390 page_t *pp; 2391 2392 ASSERT(IS_PAGEALIGNED(vaddr)); 2393 ASSERT(IS_PAGEALIGNED(eaddr)); 2394 ASSERT(hat == kas.a_hat || 2395 AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock)); 2396 for (; vaddr < eaddr; vaddr += LEVEL_SIZE(ht->ht_level)) { 2397 try_again: 2398 oldpte = htable_walk(hat, &ht, &vaddr, eaddr); 2399 if (ht == NULL) 2400 break; 2401 if (PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOCONSIST) 2402 continue; 2403 2404 pp = page_numtopp_nolock(PTE2PFN(oldpte, ht->ht_level)); 2405 if (pp == NULL) 2406 continue; 2407 x86_hm_enter(pp); 2408 2409 newpte = oldpte; 2410 /* 2411 * We found a page table entry in the desired range, 2412 * figure out the new attributes. 2413 */ 2414 if (what == HAT_SET_ATTR || what == HAT_LOAD_ATTR) { 2415 if ((attr & PROT_WRITE) && 2416 !PTE_GET(oldpte, PT_WRITABLE)) 2417 newpte |= PT_WRITABLE; 2418 2419 if ((attr & HAT_NOSYNC) && 2420 PTE_GET(oldpte, PT_SOFTWARE) < PT_NOSYNC) 2421 newpte |= PT_NOSYNC; 2422 2423 if ((attr & PROT_EXEC) && PTE_GET(oldpte, mmu.pt_nx)) 2424 newpte &= ~mmu.pt_nx; 2425 } 2426 2427 if (what == HAT_LOAD_ATTR) { 2428 if (!(attr & PROT_WRITE) && 2429 PTE_GET(oldpte, PT_WRITABLE)) 2430 newpte &= ~PT_WRITABLE; 2431 2432 if (!(attr & HAT_NOSYNC) && 2433 PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOSYNC) 2434 newpte &= ~PT_SOFTWARE; 2435 2436 if (!(attr & PROT_EXEC) && !PTE_GET(oldpte, mmu.pt_nx)) 2437 newpte |= mmu.pt_nx; 2438 } 2439 2440 if (what == HAT_CLR_ATTR) { 2441 if ((attr & PROT_WRITE) && PTE_GET(oldpte, PT_WRITABLE)) 2442 newpte &= ~PT_WRITABLE; 2443 2444 if ((attr & HAT_NOSYNC) && 2445 PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOSYNC) 2446 newpte &= ~PT_SOFTWARE; 2447 2448 if ((attr & PROT_EXEC) && !PTE_GET(oldpte, mmu.pt_nx)) 2449 newpte |= mmu.pt_nx; 2450 } 2451 2452 /* 2453 * Ensure NOSYNC/NOCONSIST mappings have REF and MOD set. 2454 * x86pte_set() depends on this. 2455 */ 2456 if (PTE_GET(newpte, PT_SOFTWARE) >= PT_NOSYNC) 2457 newpte |= PT_REF | PT_MOD; 2458 2459 /* 2460 * what about PROT_READ or others? this code only handles: 2461 * EXEC, WRITE, NOSYNC 2462 */ 2463 2464 /* 2465 * If new PTE really changed, update the table. 2466 */ 2467 if (newpte != oldpte) { 2468 entry = htable_va2entry(vaddr, ht); 2469 oldpte = hati_update_pte(ht, entry, oldpte, newpte); 2470 if (oldpte != 0) { 2471 x86_hm_exit(pp); 2472 goto try_again; 2473 } 2474 } 2475 x86_hm_exit(pp); 2476 } 2477 if (ht) 2478 htable_release(ht); 2479 } 2480 2481 /* 2482 * Various wrappers for hat_updateattr() 2483 */ 2484 void 2485 hat_setattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr) 2486 { 2487 ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit); 2488 hat_updateattr(hat, addr, len, attr, HAT_SET_ATTR); 2489 } 2490 2491 void 2492 hat_clrattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr) 2493 { 2494 ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit); 2495 hat_updateattr(hat, addr, len, attr, HAT_CLR_ATTR); 2496 } 2497 2498 void 2499 hat_chgattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr) 2500 { 2501 ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit); 2502 hat_updateattr(hat, addr, len, attr, HAT_LOAD_ATTR); 2503 } 2504 2505 void 2506 hat_chgprot(hat_t *hat, caddr_t addr, size_t len, uint_t vprot) 2507 { 2508 ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit); 2509 hat_updateattr(hat, addr, len, vprot & HAT_PROT_MASK, HAT_LOAD_ATTR); 2510 } 2511 2512 /* 2513 * size_t hat_getpagesize(hat, addr) 2514 * returns pagesize in bytes for <hat, addr>. returns -1 of there is 2515 * no mapping. This is an advisory call. 2516 */ 2517 ssize_t 2518 hat_getpagesize(hat_t *hat, caddr_t addr) 2519 { 2520 uintptr_t vaddr = ALIGN2PAGE(addr); 2521 htable_t *ht; 2522 size_t pagesize; 2523 2524 ASSERT(hat == kas.a_hat || vaddr <= _userlimit); 2525 if (IN_VA_HOLE(vaddr)) 2526 return (-1); 2527 ht = htable_getpage(hat, vaddr, NULL); 2528 if (ht == NULL) 2529 return (-1); 2530 pagesize = LEVEL_SIZE(ht->ht_level); 2531 htable_release(ht); 2532 return (pagesize); 2533 } 2534 2535 2536 2537 /* 2538 * pfn_t hat_getpfnum(hat, addr) 2539 * returns pfn for <hat, addr> or PFN_INVALID if mapping is invalid. 2540 */ 2541 pfn_t 2542 hat_getpfnum(hat_t *hat, caddr_t addr) 2543 { 2544 uintptr_t vaddr = ALIGN2PAGE(addr); 2545 htable_t *ht; 2546 uint_t entry; 2547 pfn_t pfn = PFN_INVALID; 2548 2549 ASSERT(hat == kas.a_hat || vaddr <= _userlimit); 2550 if (khat_running == 0) 2551 return (PFN_INVALID); 2552 2553 if (IN_VA_HOLE(vaddr)) 2554 return (PFN_INVALID); 2555 2556 /* 2557 * A very common use of hat_getpfnum() is from the DDI for kernel pages. 2558 * Use the kmap_ptes (which also covers the 32 bit heap) to speed 2559 * this up. 2560 */ 2561 if (mmu.kmap_addr <= vaddr && vaddr < mmu.kmap_eaddr) { 2562 x86pte_t pte; 2563 pgcnt_t pg_index; 2564 2565 pg_index = mmu_btop(vaddr - mmu.kmap_addr); 2566 pte = GET_PTE(PT_INDEX_PTR(mmu.kmap_ptes, pg_index)); 2567 if (!PTE_ISVALID(pte)) 2568 return (PFN_INVALID); 2569 /*LINTED [use of constant 0 causes a silly lint warning] */ 2570 return (PTE2PFN(pte, 0)); 2571 } 2572 2573 ht = htable_getpage(hat, vaddr, &entry); 2574 if (ht == NULL) 2575 return (PFN_INVALID); 2576 ASSERT(vaddr >= ht->ht_vaddr); 2577 ASSERT(vaddr <= HTABLE_LAST_PAGE(ht)); 2578 pfn = PTE2PFN(x86pte_get(ht, entry), ht->ht_level); 2579 if (ht->ht_level > 0) 2580 pfn += mmu_btop(vaddr & LEVEL_OFFSET(ht->ht_level)); 2581 htable_release(ht); 2582 return (pfn); 2583 } 2584 2585 /* 2586 * hat_getkpfnum() is an obsolete DDI routine, and its use is discouraged. 2587 * Use hat_getpfnum(kas.a_hat, ...) instead. 2588 * 2589 * We'd like to return PFN_INVALID if the mappings have underlying page_t's 2590 * but can't right now due to the fact that some software has grown to use 2591 * this interface incorrectly. So for now when the interface is misused, 2592 * return a warning to the user that in the future it won't work in the 2593 * way they're abusing it, and carry on. 2594 * 2595 * Note that hat_getkpfnum() is never supported on amd64. 2596 */ 2597 #if !defined(__amd64) 2598 pfn_t 2599 hat_getkpfnum(caddr_t addr) 2600 { 2601 pfn_t pfn; 2602 int badcaller = 0; 2603 2604 if (khat_running == 0) 2605 panic("hat_getkpfnum(): called too early\n"); 2606 if ((uintptr_t)addr < kernelbase) 2607 return (PFN_INVALID); 2608 2609 2610 if (segkpm && IS_KPM_ADDR(addr)) { 2611 badcaller = 1; 2612 pfn = hat_kpm_va2pfn(addr); 2613 } else { 2614 pfn = hat_getpfnum(kas.a_hat, addr); 2615 badcaller = pf_is_memory(pfn); 2616 } 2617 2618 if (badcaller) 2619 hat_getkpfnum_badcall(caller()); 2620 return (pfn); 2621 } 2622 #endif /* __amd64 */ 2623 2624 /* 2625 * int hat_probe(hat, addr) 2626 * return 0 if no valid mapping is present. Faster version 2627 * of hat_getattr in certain architectures. 2628 */ 2629 int 2630 hat_probe(hat_t *hat, caddr_t addr) 2631 { 2632 uintptr_t vaddr = ALIGN2PAGE(addr); 2633 uint_t entry; 2634 htable_t *ht; 2635 pgcnt_t pg_off; 2636 2637 ASSERT(hat == kas.a_hat || vaddr <= _userlimit); 2638 ASSERT(hat == kas.a_hat || 2639 AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock)); 2640 if (IN_VA_HOLE(vaddr)) 2641 return (0); 2642 2643 /* 2644 * Most common use of hat_probe is from segmap. We special case it 2645 * for performance. 2646 */ 2647 if (mmu.kmap_addr <= vaddr && vaddr < mmu.kmap_eaddr) { 2648 pg_off = mmu_btop(vaddr - mmu.kmap_addr); 2649 if (mmu.pae_hat) 2650 return (PTE_ISVALID(mmu.kmap_ptes[pg_off])); 2651 else 2652 return (PTE_ISVALID( 2653 ((x86pte32_t *)mmu.kmap_ptes)[pg_off])); 2654 } 2655 2656 ht = htable_getpage(hat, vaddr, &entry); 2657 if (ht == NULL) 2658 return (0); 2659 htable_release(ht); 2660 return (1); 2661 } 2662 2663 /* 2664 * Simple implementation of ISM. hat_share() is just like hat_memload_array(), 2665 * except that we use the ism_hat's existing mappings to determine the pages 2666 * and protections to use for this hat. In case we find a properly aligned 2667 * and sized pagetable of 4K mappings, we will attempt to share the pagetable 2668 * itself. 2669 */ 2670 /*ARGSUSED*/ 2671 int 2672 hat_share( 2673 hat_t *hat, 2674 caddr_t addr, 2675 hat_t *ism_hat, 2676 caddr_t src_addr, 2677 size_t len, /* almost useless value, see below.. */ 2678 uint_t ismszc) 2679 { 2680 uintptr_t vaddr_start = (uintptr_t)addr; 2681 uintptr_t vaddr; 2682 uintptr_t pt_vaddr; 2683 uintptr_t eaddr = vaddr_start + len; 2684 uintptr_t ism_addr_start = (uintptr_t)src_addr; 2685 uintptr_t ism_addr = ism_addr_start; 2686 uintptr_t e_ism_addr = ism_addr + len; 2687 htable_t *ism_ht = NULL; 2688 htable_t *ht; 2689 x86pte_t pte; 2690 page_t *pp; 2691 pfn_t pfn; 2692 level_t l; 2693 pgcnt_t pgcnt; 2694 uint_t prot; 2695 uint_t valid_cnt; 2696 2697 /* 2698 * We might be asked to share an empty DISM hat by as_dup() 2699 */ 2700 ASSERT(hat != kas.a_hat); 2701 ASSERT(eaddr <= _userlimit); 2702 if (!(ism_hat->hat_flags & HAT_SHARED)) { 2703 ASSERT(hat_get_mapped_size(ism_hat) == 0); 2704 return (0); 2705 } 2706 2707 /* 2708 * The SPT segment driver often passes us a size larger than there are 2709 * valid mappings. That's because it rounds the segment size up to a 2710 * large pagesize, even if the actual memory mapped by ism_hat is less. 2711 */ 2712 HATIN(hat_share, hat, addr, len); 2713 ASSERT(IS_PAGEALIGNED(vaddr_start)); 2714 ASSERT(IS_PAGEALIGNED(ism_addr_start)); 2715 ASSERT(ism_hat->hat_flags & HAT_SHARED); 2716 while (ism_addr < e_ism_addr) { 2717 /* 2718 * use htable_walk to get the next valid ISM mapping 2719 */ 2720 pte = htable_walk(ism_hat, &ism_ht, &ism_addr, e_ism_addr); 2721 if (ism_ht == NULL) 2722 break; 2723 2724 /* 2725 * Find the largest page size we can use, based on the 2726 * ISM mapping size, our address alignment and the remaining 2727 * map length. 2728 */ 2729 vaddr = vaddr_start + (ism_addr - ism_addr_start); 2730 for (l = ism_ht->ht_level; l > 0; --l) { 2731 if (LEVEL_SIZE(l) <= eaddr - vaddr && 2732 (vaddr & LEVEL_OFFSET(l)) == 0) 2733 break; 2734 } 2735 2736 /* 2737 * attempt to share the pagetable 2738 * 2739 * - only 4K pagetables are shared (ie. level == 0) 2740 * - the hat_share() length must cover the whole pagetable 2741 * - the shared address must align at level 1 2742 * - a shared PTE for this address already exists OR 2743 * - no page table for this address exists yet 2744 */ 2745 pt_vaddr = 2746 vaddr_start + (ism_ht->ht_vaddr - ism_addr_start); 2747 if (ism_ht->ht_level == 0 && 2748 ism_ht->ht_vaddr + LEVEL_SIZE(1) <= e_ism_addr && 2749 (pt_vaddr & LEVEL_OFFSET(1)) == 0) { 2750 2751 ht = htable_lookup(hat, pt_vaddr, 0); 2752 if (ht == NULL) 2753 ht = htable_create(hat, pt_vaddr, 0, ism_ht); 2754 2755 if (ht->ht_level > 0 || 2756 !(ht->ht_flags & HTABLE_SHARED_PFN)) { 2757 2758 htable_release(ht); 2759 2760 } else { 2761 2762 /* 2763 * share the page table 2764 */ 2765 ASSERT(ht->ht_level == 0); 2766 ASSERT(ht->ht_shares == ism_ht); 2767 valid_cnt = ism_ht->ht_valid_cnt; 2768 atomic_add_long(&hat->hat_pages_mapped[0], 2769 valid_cnt - ht->ht_valid_cnt); 2770 ht->ht_valid_cnt = valid_cnt; 2771 htable_release(ht); 2772 ism_addr = ism_ht->ht_vaddr + LEVEL_SIZE(1); 2773 htable_release(ism_ht); 2774 ism_ht = NULL; 2775 continue; 2776 } 2777 } 2778 2779 /* 2780 * Unable to share the page table. Instead we will 2781 * create new mappings from the values in the ISM mappings. 2782 * 2783 * The ISM mapping might be larger than the share area, 2784 * be careful to trunctate it if needed. 2785 */ 2786 if (eaddr - vaddr >= LEVEL_SIZE(ism_ht->ht_level)) { 2787 pgcnt = mmu_btop(LEVEL_SIZE(ism_ht->ht_level)); 2788 } else { 2789 pgcnt = mmu_btop(eaddr - vaddr); 2790 l = 0; 2791 } 2792 2793 pfn = PTE2PFN(pte, ism_ht->ht_level); 2794 ASSERT(pfn != PFN_INVALID); 2795 while (pgcnt > 0) { 2796 /* 2797 * Make a new pte for the PFN for this level. 2798 * Copy protections for the pte from the ISM pte. 2799 */ 2800 pp = page_numtopp_nolock(pfn); 2801 ASSERT(pp != NULL); 2802 2803 prot = PROT_USER | PROT_READ | HAT_UNORDERED_OK; 2804 if (PTE_GET(pte, PT_WRITABLE)) 2805 prot |= PROT_WRITE; 2806 if (!PTE_GET(pte, PT_NX)) 2807 prot |= PROT_EXEC; 2808 2809 /* 2810 * XX64 -- can shm ever be written to swap? 2811 * if not we could use HAT_NOSYNC here. 2812 */ 2813 while (hati_load_common(hat, vaddr, pp, prot, HAT_LOAD, 2814 l, pfn) != 0) { 2815 if (l == 0) 2816 panic("hati_load_common() failure"); 2817 --l; 2818 } 2819 2820 vaddr += LEVEL_SIZE(l); 2821 ism_addr += LEVEL_SIZE(l); 2822 pfn += mmu_btop(LEVEL_SIZE(l)); 2823 pgcnt -= mmu_btop(LEVEL_SIZE(l)); 2824 } 2825 } 2826 if (ism_ht != NULL) 2827 htable_release(ism_ht); 2828 2829 HATOUT(hat_share, hat, addr); 2830 return (0); 2831 } 2832 2833 2834 /* 2835 * hat_unshare() is similar to hat_unload_callback(), but 2836 * we have to look for empty shared pagetables. Note that 2837 * hat_unshare() is always invoked against an entire segment. 2838 */ 2839 /*ARGSUSED*/ 2840 void 2841 hat_unshare(hat_t *hat, caddr_t addr, size_t len, uint_t ismszc) 2842 { 2843 uintptr_t vaddr = (uintptr_t)addr; 2844 uintptr_t eaddr = vaddr + len; 2845 htable_t *ht = NULL; 2846 uint_t need_demaps = 0; 2847 2848 ASSERT(hat != kas.a_hat); 2849 ASSERT(eaddr <= _userlimit); 2850 HATIN(hat_unshare, hat, addr, len); 2851 ASSERT(IS_PAGEALIGNED(vaddr)); 2852 ASSERT(IS_PAGEALIGNED(eaddr)); 2853 2854 /* 2855 * First go through and remove any shared pagetables. 2856 * 2857 * Note that it's ok to delay the TLB shootdown till the entire range is 2858 * finished, because if hat_pageunload() were to unload a shared 2859 * pagetable page, its hat_tlb_inval() will do a global TLB invalidate. 2860 */ 2861 while (vaddr < eaddr) { 2862 ASSERT(!IN_VA_HOLE(vaddr)); 2863 /* 2864 * find the pagetable that would map the current address 2865 */ 2866 ht = htable_lookup(hat, vaddr, 0); 2867 if (ht != NULL) { 2868 if (ht->ht_flags & HTABLE_SHARED_PFN) { 2869 /* 2870 * clear mapped pages count, set valid_cnt to 0 2871 * and let htable_release() finish the job 2872 */ 2873 atomic_add_long(&hat->hat_pages_mapped[0], 2874 -ht->ht_valid_cnt); 2875 ht->ht_valid_cnt = 0; 2876 need_demaps = 1; 2877 } 2878 htable_release(ht); 2879 } 2880 vaddr = (vaddr & LEVEL_MASK(1)) + LEVEL_SIZE(1); 2881 } 2882 2883 /* 2884 * flush the TLBs - since we're probably dealing with MANY mappings 2885 * we do just one CR3 reload. 2886 */ 2887 if (!(hat->hat_flags & HAT_FREEING) && need_demaps) 2888 hat_tlb_inval(hat, DEMAP_ALL_ADDR); 2889 2890 /* 2891 * Now go back and clean up any unaligned mappings that 2892 * couldn't share pagetables. 2893 */ 2894 hat_unload(hat, addr, len, HAT_UNLOAD_UNMAP); 2895 2896 HATOUT(hat_unshare, hat, addr); 2897 } 2898 2899 2900 /* 2901 * hat_reserve() does nothing 2902 */ 2903 /*ARGSUSED*/ 2904 void 2905 hat_reserve(struct as *as, caddr_t addr, size_t len) 2906 { 2907 } 2908 2909 2910 /* 2911 * Called when all mappings to a page should have write permission removed. 2912 * Mostly stolem from hat_pagesync() 2913 */ 2914 static void 2915 hati_page_clrwrt(struct page *pp) 2916 { 2917 hment_t *hm = NULL; 2918 htable_t *ht; 2919 uint_t entry; 2920 x86pte_t old; 2921 x86pte_t new; 2922 uint_t pszc = 0; 2923 2924 next_size: 2925 /* 2926 * walk thru the mapping list clearing write permission 2927 */ 2928 x86_hm_enter(pp); 2929 while ((hm = hment_walk(pp, &ht, &entry, hm)) != NULL) { 2930 if (ht->ht_level < pszc) 2931 continue; 2932 old = x86pte_get(ht, entry); 2933 2934 for (;;) { 2935 /* 2936 * Is this mapping of interest? 2937 */ 2938 if (PTE2PFN(old, ht->ht_level) != pp->p_pagenum || 2939 PTE_GET(old, PT_WRITABLE) == 0) 2940 break; 2941 2942 /* 2943 * Clear ref/mod writable bits. This requires cross 2944 * calls to ensure any executing TLBs see cleared bits. 2945 */ 2946 new = old; 2947 PTE_CLR(new, PT_REF | PT_MOD | PT_WRITABLE); 2948 old = hati_update_pte(ht, entry, old, new); 2949 if (old != 0) 2950 continue; 2951 2952 break; 2953 } 2954 } 2955 x86_hm_exit(pp); 2956 while (pszc < pp->p_szc) { 2957 page_t *tpp; 2958 pszc++; 2959 tpp = PP_GROUPLEADER(pp, pszc); 2960 if (pp != tpp) { 2961 pp = tpp; 2962 goto next_size; 2963 } 2964 } 2965 } 2966 2967 /* 2968 * void hat_page_setattr(pp, flag) 2969 * void hat_page_clrattr(pp, flag) 2970 * used to set/clr ref/mod bits. 2971 */ 2972 void 2973 hat_page_setattr(struct page *pp, uint_t flag) 2974 { 2975 vnode_t *vp = pp->p_vnode; 2976 kmutex_t *vphm = NULL; 2977 page_t **listp; 2978 2979 if (PP_GETRM(pp, flag) == flag) 2980 return; 2981 2982 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp)) { 2983 vphm = page_vnode_mutex(vp); 2984 mutex_enter(vphm); 2985 } 2986 2987 PP_SETRM(pp, flag); 2988 2989 if (vphm != NULL) { 2990 2991 /* 2992 * Some File Systems examine v_pages for NULL w/o 2993 * grabbing the vphm mutex. Must not let it become NULL when 2994 * pp is the only page on the list. 2995 */ 2996 if (pp->p_vpnext != pp) { 2997 page_vpsub(&vp->v_pages, pp); 2998 if (vp->v_pages != NULL) 2999 listp = &vp->v_pages->p_vpprev->p_vpnext; 3000 else 3001 listp = &vp->v_pages; 3002 page_vpadd(listp, pp); 3003 } 3004 mutex_exit(vphm); 3005 } 3006 } 3007 3008 void 3009 hat_page_clrattr(struct page *pp, uint_t flag) 3010 { 3011 vnode_t *vp = pp->p_vnode; 3012 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO))); 3013 3014 /* 3015 * Caller is expected to hold page's io lock for VMODSORT to work 3016 * correctly with pvn_vplist_dirty() and pvn_getdirty() when mod 3017 * bit is cleared. 3018 * We don't have assert to avoid tripping some existing third party 3019 * code. The dirty page is moved back to top of the v_page list 3020 * after IO is done in pvn_write_done(). 3021 */ 3022 PP_CLRRM(pp, flag); 3023 3024 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp)) { 3025 3026 /* 3027 * VMODSORT works by removing write permissions and getting 3028 * a fault when a page is made dirty. At this point 3029 * we need to remove write permission from all mappings 3030 * to this page. 3031 */ 3032 hati_page_clrwrt(pp); 3033 } 3034 } 3035 3036 /* 3037 * If flag is specified, returns 0 if attribute is disabled 3038 * and non zero if enabled. If flag specifes multiple attributs 3039 * then returns 0 if ALL atriibutes are disabled. This is an advisory 3040 * call. 3041 */ 3042 uint_t 3043 hat_page_getattr(struct page *pp, uint_t flag) 3044 { 3045 return (PP_GETRM(pp, flag)); 3046 } 3047 3048 3049 /* 3050 * common code used by hat_pageunload() and hment_steal() 3051 */ 3052 hment_t * 3053 hati_page_unmap(page_t *pp, htable_t *ht, uint_t entry) 3054 { 3055 x86pte_t old_pte; 3056 pfn_t pfn = pp->p_pagenum; 3057 hment_t *hm; 3058 3059 /* 3060 * We need to acquire a hold on the htable in order to 3061 * do the invalidate. We know the htable must exist, since 3062 * unmap's don't release the htable until after removing any 3063 * hment. Having x86_hm_enter() keeps that from proceeding. 3064 */ 3065 htable_acquire(ht); 3066 3067 /* 3068 * Invalidate the PTE and remove the hment. 3069 */ 3070 old_pte = x86pte_inval(ht, entry, 0, NULL); 3071 if (PTE2PFN(old_pte, ht->ht_level) != pfn) { 3072 panic("x86pte_inval() failure found PTE = " FMT_PTE 3073 " pfn being unmapped is %lx ht=0x%lx entry=0x%x", 3074 old_pte, pfn, (uintptr_t)ht, entry); 3075 } 3076 3077 /* 3078 * Clean up all the htable information for this mapping 3079 */ 3080 ASSERT(ht->ht_valid_cnt > 0); 3081 HTABLE_DEC(ht->ht_valid_cnt); 3082 PGCNT_DEC(ht->ht_hat, ht->ht_level); 3083 3084 /* 3085 * sync ref/mod bits to the page_t 3086 */ 3087 if (PTE_GET(old_pte, PT_SOFTWARE) < PT_NOSYNC) 3088 hati_sync_pte_to_page(pp, old_pte, ht->ht_level); 3089 3090 /* 3091 * Remove the mapping list entry for this page. 3092 */ 3093 hm = hment_remove(pp, ht, entry); 3094 3095 /* 3096 * drop the mapping list lock so that we might free the 3097 * hment and htable. 3098 */ 3099 x86_hm_exit(pp); 3100 htable_release(ht); 3101 return (hm); 3102 } 3103 3104 extern int vpm_enable; 3105 /* 3106 * Unload all translations to a page. If the page is a subpage of a large 3107 * page, the large page mappings are also removed. 3108 * 3109 * The forceflags are unused. 3110 */ 3111 3112 /*ARGSUSED*/ 3113 static int 3114 hati_pageunload(struct page *pp, uint_t pg_szcd, uint_t forceflag) 3115 { 3116 page_t *cur_pp = pp; 3117 hment_t *hm; 3118 hment_t *prev; 3119 htable_t *ht; 3120 uint_t entry; 3121 level_t level; 3122 3123 #if defined(__amd64) 3124 /* 3125 * clear the vpm ref. 3126 */ 3127 if (vpm_enable) { 3128 pp->p_vpmref = 0; 3129 } 3130 #endif 3131 /* 3132 * The loop with next_size handles pages with multiple pagesize mappings 3133 */ 3134 next_size: 3135 for (;;) { 3136 3137 /* 3138 * Get a mapping list entry 3139 */ 3140 x86_hm_enter(cur_pp); 3141 for (prev = NULL; ; prev = hm) { 3142 hm = hment_walk(cur_pp, &ht, &entry, prev); 3143 if (hm == NULL) { 3144 x86_hm_exit(cur_pp); 3145 3146 /* 3147 * If not part of a larger page, we're done. 3148 */ 3149 if (cur_pp->p_szc <= pg_szcd) { 3150 return (0); 3151 } 3152 3153 /* 3154 * Else check the next larger page size. 3155 * hat_page_demote() may decrease p_szc 3156 * but that's ok we'll just take an extra 3157 * trip discover there're no larger mappings 3158 * and return. 3159 */ 3160 ++pg_szcd; 3161 cur_pp = PP_GROUPLEADER(cur_pp, pg_szcd); 3162 goto next_size; 3163 } 3164 3165 /* 3166 * If this mapping size matches, remove it. 3167 */ 3168 level = ht->ht_level; 3169 if (level == pg_szcd) 3170 break; 3171 } 3172 3173 /* 3174 * Remove the mapping list entry for this page. 3175 * Note this does the x86_hm_exit() for us. 3176 */ 3177 hm = hati_page_unmap(cur_pp, ht, entry); 3178 if (hm != NULL) 3179 hment_free(hm); 3180 } 3181 } 3182 3183 int 3184 hat_pageunload(struct page *pp, uint_t forceflag) 3185 { 3186 ASSERT(PAGE_EXCL(pp)); 3187 return (hati_pageunload(pp, 0, forceflag)); 3188 } 3189 3190 /* 3191 * Unload all large mappings to pp and reduce by 1 p_szc field of every large 3192 * page level that included pp. 3193 * 3194 * pp must be locked EXCL. Even though no other constituent pages are locked 3195 * it's legal to unload large mappings to pp because all constituent pages of 3196 * large locked mappings have to be locked SHARED. therefore if we have EXCL 3197 * lock on one of constituent pages none of the large mappings to pp are 3198 * locked. 3199 * 3200 * Change (always decrease) p_szc field starting from the last constituent 3201 * page and ending with root constituent page so that root's pszc always shows 3202 * the area where hat_page_demote() may be active. 3203 * 3204 * This mechanism is only used for file system pages where it's not always 3205 * possible to get EXCL locks on all constituent pages to demote the size code 3206 * (as is done for anonymous or kernel large pages). 3207 */ 3208 void 3209 hat_page_demote(page_t *pp) 3210 { 3211 uint_t pszc; 3212 uint_t rszc; 3213 uint_t szc; 3214 page_t *rootpp; 3215 page_t *firstpp; 3216 page_t *lastpp; 3217 pgcnt_t pgcnt; 3218 3219 ASSERT(PAGE_EXCL(pp)); 3220 ASSERT(!PP_ISFREE(pp)); 3221 ASSERT(page_szc_lock_assert(pp)); 3222 3223 if (pp->p_szc == 0) 3224 return; 3225 3226 rootpp = PP_GROUPLEADER(pp, 1); 3227 (void) hati_pageunload(rootpp, 1, HAT_FORCE_PGUNLOAD); 3228 3229 /* 3230 * all large mappings to pp are gone 3231 * and no new can be setup since pp is locked exclusively. 3232 * 3233 * Lock the root to make sure there's only one hat_page_demote() 3234 * outstanding within the area of this root's pszc. 3235 * 3236 * Second potential hat_page_demote() is already eliminated by upper 3237 * VM layer via page_szc_lock() but we don't rely on it and use our 3238 * own locking (so that upper layer locking can be changed without 3239 * assumptions that hat depends on upper layer VM to prevent multiple 3240 * hat_page_demote() to be issued simultaneously to the same large 3241 * page). 3242 */ 3243 again: 3244 pszc = pp->p_szc; 3245 if (pszc == 0) 3246 return; 3247 rootpp = PP_GROUPLEADER(pp, pszc); 3248 x86_hm_enter(rootpp); 3249 /* 3250 * If root's p_szc is different from pszc we raced with another 3251 * hat_page_demote(). Drop the lock and try to find the root again. 3252 * If root's p_szc is greater than pszc previous hat_page_demote() is 3253 * not done yet. Take and release mlist lock of root's root to wait 3254 * for previous hat_page_demote() to complete. 3255 */ 3256 if ((rszc = rootpp->p_szc) != pszc) { 3257 x86_hm_exit(rootpp); 3258 if (rszc > pszc) { 3259 /* p_szc of a locked non free page can't increase */ 3260 ASSERT(pp != rootpp); 3261 3262 rootpp = PP_GROUPLEADER(rootpp, rszc); 3263 x86_hm_enter(rootpp); 3264 x86_hm_exit(rootpp); 3265 } 3266 goto again; 3267 } 3268 ASSERT(pp->p_szc == pszc); 3269 3270 /* 3271 * Decrement by 1 p_szc of every constituent page of a region that 3272 * covered pp. For example if original szc is 3 it gets changed to 2 3273 * everywhere except in region 2 that covered pp. Region 2 that 3274 * covered pp gets demoted to 1 everywhere except in region 1 that 3275 * covered pp. The region 1 that covered pp is demoted to region 3276 * 0. It's done this way because from region 3 we removed level 3 3277 * mappings, from region 2 that covered pp we removed level 2 mappings 3278 * and from region 1 that covered pp we removed level 1 mappings. All 3279 * changes are done from from high pfn's to low pfn's so that roots 3280 * are changed last allowing one to know the largest region where 3281 * hat_page_demote() is stil active by only looking at the root page. 3282 * 3283 * This algorithm is implemented in 2 while loops. First loop changes 3284 * p_szc of pages to the right of pp's level 1 region and second 3285 * loop changes p_szc of pages of level 1 region that covers pp 3286 * and all pages to the left of level 1 region that covers pp. 3287 * In the first loop p_szc keeps dropping with every iteration 3288 * and in the second loop it keeps increasing with every iteration. 3289 * 3290 * First loop description: Demote pages to the right of pp outside of 3291 * level 1 region that covers pp. In every iteration of the while 3292 * loop below find the last page of szc region and the first page of 3293 * (szc - 1) region that is immediately to the right of (szc - 1) 3294 * region that covers pp. From last such page to first such page 3295 * change every page's szc to szc - 1. Decrement szc and continue 3296 * looping until szc is 1. If pp belongs to the last (szc - 1) region 3297 * of szc region skip to the next iteration. 3298 */ 3299 szc = pszc; 3300 while (szc > 1) { 3301 lastpp = PP_GROUPLEADER(pp, szc); 3302 pgcnt = page_get_pagecnt(szc); 3303 lastpp += pgcnt - 1; 3304 firstpp = PP_GROUPLEADER(pp, (szc - 1)); 3305 pgcnt = page_get_pagecnt(szc - 1); 3306 if (lastpp - firstpp < pgcnt) { 3307 szc--; 3308 continue; 3309 } 3310 firstpp += pgcnt; 3311 while (lastpp != firstpp) { 3312 ASSERT(lastpp->p_szc == pszc); 3313 lastpp->p_szc = szc - 1; 3314 lastpp--; 3315 } 3316 firstpp->p_szc = szc - 1; 3317 szc--; 3318 } 3319 3320 /* 3321 * Second loop description: 3322 * First iteration changes p_szc to 0 of every 3323 * page of level 1 region that covers pp. 3324 * Subsequent iterations find last page of szc region 3325 * immediately to the left of szc region that covered pp 3326 * and first page of (szc + 1) region that covers pp. 3327 * From last to first page change p_szc of every page to szc. 3328 * Increment szc and continue looping until szc is pszc. 3329 * If pp belongs to the fist szc region of (szc + 1) region 3330 * skip to the next iteration. 3331 * 3332 */ 3333 szc = 0; 3334 while (szc < pszc) { 3335 firstpp = PP_GROUPLEADER(pp, (szc + 1)); 3336 if (szc == 0) { 3337 pgcnt = page_get_pagecnt(1); 3338 lastpp = firstpp + (pgcnt - 1); 3339 } else { 3340 lastpp = PP_GROUPLEADER(pp, szc); 3341 if (firstpp == lastpp) { 3342 szc++; 3343 continue; 3344 } 3345 lastpp--; 3346 pgcnt = page_get_pagecnt(szc); 3347 } 3348 while (lastpp != firstpp) { 3349 ASSERT(lastpp->p_szc == pszc); 3350 lastpp->p_szc = szc; 3351 lastpp--; 3352 } 3353 firstpp->p_szc = szc; 3354 if (firstpp == rootpp) 3355 break; 3356 szc++; 3357 } 3358 x86_hm_exit(rootpp); 3359 } 3360 3361 /* 3362 * get hw stats from hardware into page struct and reset hw stats 3363 * returns attributes of page 3364 * Flags for hat_pagesync, hat_getstat, hat_sync 3365 * 3366 * define HAT_SYNC_ZERORM 0x01 3367 * 3368 * Additional flags for hat_pagesync 3369 * 3370 * define HAT_SYNC_STOPON_REF 0x02 3371 * define HAT_SYNC_STOPON_MOD 0x04 3372 * define HAT_SYNC_STOPON_RM 0x06 3373 * define HAT_SYNC_STOPON_SHARED 0x08 3374 */ 3375 uint_t 3376 hat_pagesync(struct page *pp, uint_t flags) 3377 { 3378 hment_t *hm = NULL; 3379 htable_t *ht; 3380 uint_t entry; 3381 x86pte_t old, save_old; 3382 x86pte_t new; 3383 uchar_t nrmbits = P_REF|P_MOD|P_RO; 3384 extern ulong_t po_share; 3385 page_t *save_pp = pp; 3386 uint_t pszc = 0; 3387 3388 ASSERT(PAGE_LOCKED(pp) || panicstr); 3389 3390 if (PP_ISRO(pp) && (flags & HAT_SYNC_STOPON_MOD)) 3391 return (pp->p_nrm & nrmbits); 3392 3393 if ((flags & HAT_SYNC_ZERORM) == 0) { 3394 3395 if ((flags & HAT_SYNC_STOPON_REF) != 0 && PP_ISREF(pp)) 3396 return (pp->p_nrm & nrmbits); 3397 3398 if ((flags & HAT_SYNC_STOPON_MOD) != 0 && PP_ISMOD(pp)) 3399 return (pp->p_nrm & nrmbits); 3400 3401 if ((flags & HAT_SYNC_STOPON_SHARED) != 0 && 3402 hat_page_getshare(pp) > po_share) { 3403 if (PP_ISRO(pp)) 3404 PP_SETREF(pp); 3405 return (pp->p_nrm & nrmbits); 3406 } 3407 } 3408 3409 next_size: 3410 /* 3411 * walk thru the mapping list syncing (and clearing) ref/mod bits. 3412 */ 3413 x86_hm_enter(pp); 3414 while ((hm = hment_walk(pp, &ht, &entry, hm)) != NULL) { 3415 if (ht->ht_level < pszc) 3416 continue; 3417 old = x86pte_get(ht, entry); 3418 try_again: 3419 3420 ASSERT(PTE2PFN(old, ht->ht_level) == pp->p_pagenum); 3421 3422 if (PTE_GET(old, PT_REF | PT_MOD) == 0) 3423 continue; 3424 3425 save_old = old; 3426 if ((flags & HAT_SYNC_ZERORM) != 0) { 3427 3428 /* 3429 * Need to clear ref or mod bits. Need to demap 3430 * to make sure any executing TLBs see cleared bits. 3431 */ 3432 new = old; 3433 PTE_CLR(new, PT_REF | PT_MOD); 3434 old = hati_update_pte(ht, entry, old, new); 3435 if (old != 0) 3436 goto try_again; 3437 3438 old = save_old; 3439 } 3440 3441 /* 3442 * Sync the PTE 3443 */ 3444 if (!(flags & HAT_SYNC_ZERORM) && 3445 PTE_GET(old, PT_SOFTWARE) <= PT_NOSYNC) 3446 hati_sync_pte_to_page(pp, old, ht->ht_level); 3447 3448 /* 3449 * can stop short if we found a ref'd or mod'd page 3450 */ 3451 if ((flags & HAT_SYNC_STOPON_MOD) && PP_ISMOD(save_pp) || 3452 (flags & HAT_SYNC_STOPON_REF) && PP_ISREF(save_pp)) { 3453 x86_hm_exit(pp); 3454 goto done; 3455 } 3456 } 3457 x86_hm_exit(pp); 3458 while (pszc < pp->p_szc) { 3459 page_t *tpp; 3460 pszc++; 3461 tpp = PP_GROUPLEADER(pp, pszc); 3462 if (pp != tpp) { 3463 pp = tpp; 3464 goto next_size; 3465 } 3466 } 3467 done: 3468 return (save_pp->p_nrm & nrmbits); 3469 } 3470 3471 /* 3472 * returns approx number of mappings to this pp. A return of 0 implies 3473 * there are no mappings to the page. 3474 */ 3475 ulong_t 3476 hat_page_getshare(page_t *pp) 3477 { 3478 uint_t cnt; 3479 cnt = hment_mapcnt(pp); 3480 #if defined(__amd64) 3481 if (vpm_enable && pp->p_vpmref) { 3482 cnt += 1; 3483 } 3484 #endif 3485 return (cnt); 3486 } 3487 3488 /* 3489 * hat_softlock isn't supported anymore 3490 */ 3491 /*ARGSUSED*/ 3492 faultcode_t 3493 hat_softlock( 3494 hat_t *hat, 3495 caddr_t addr, 3496 size_t *len, 3497 struct page **page_array, 3498 uint_t flags) 3499 { 3500 return (FC_NOSUPPORT); 3501 } 3502 3503 3504 3505 /* 3506 * Routine to expose supported HAT features to platform independent code. 3507 */ 3508 /*ARGSUSED*/ 3509 int 3510 hat_supported(enum hat_features feature, void *arg) 3511 { 3512 switch (feature) { 3513 3514 case HAT_SHARED_PT: /* this is really ISM */ 3515 return (1); 3516 3517 case HAT_DYNAMIC_ISM_UNMAP: 3518 return (0); 3519 3520 case HAT_VMODSORT: 3521 return (1); 3522 3523 default: 3524 panic("hat_supported() - unknown feature"); 3525 } 3526 return (0); 3527 } 3528 3529 /* 3530 * Called when a thread is exiting and has been switched to the kernel AS 3531 */ 3532 void 3533 hat_thread_exit(kthread_t *thd) 3534 { 3535 ASSERT(thd->t_procp->p_as == &kas); 3536 hat_switch(thd->t_procp->p_as->a_hat); 3537 } 3538 3539 /* 3540 * Setup the given brand new hat structure as the new HAT on this cpu's mmu. 3541 */ 3542 /*ARGSUSED*/ 3543 void 3544 hat_setup(hat_t *hat, int flags) 3545 { 3546 kpreempt_disable(); 3547 3548 hat_switch(hat); 3549 3550 kpreempt_enable(); 3551 } 3552 3553 /* 3554 * Prepare for a CPU private mapping for the given address. 3555 * 3556 * The address can only be used from a single CPU and can be remapped 3557 * using hat_mempte_remap(). Return the address of the PTE. 3558 * 3559 * We do the htable_create() if necessary and increment the valid count so 3560 * the htable can't disappear. We also hat_devload() the page table into 3561 * kernel so that the PTE is quickly accessed. 3562 */ 3563 hat_mempte_t 3564 hat_mempte_setup(caddr_t addr) 3565 { 3566 uintptr_t va = (uintptr_t)addr; 3567 htable_t *ht; 3568 uint_t entry; 3569 x86pte_t oldpte; 3570 hat_mempte_t p; 3571 uint_t created = 0; 3572 3573 ASSERT(IS_PAGEALIGNED(va)); 3574 ASSERT(!IN_VA_HOLE(va)); 3575 ht = htable_getpte(kas.a_hat, va, &entry, &oldpte, 0); 3576 if (ht == NULL) { 3577 ht = htable_create(kas.a_hat, va, 0, NULL); 3578 entry = htable_va2entry(va, ht); 3579 ASSERT(ht->ht_level == 0); 3580 oldpte = x86pte_get(ht, entry); 3581 created = 1; 3582 } 3583 if (PTE_ISVALID(oldpte)) 3584 panic("hat_mempte_setup(): address already mapped" 3585 "ht=%p, entry=%d, pte=" FMT_PTE, ht, entry, oldpte); 3586 3587 /* 3588 * increment ht_valid_cnt so that the pagetable can't disappear 3589 */ 3590 HTABLE_INC(ht->ht_valid_cnt); 3591 3592 /* 3593 * return the PTE physical address to the caller. 3594 */ 3595 htable_release(ht); 3596 p = PT_INDEX_PHYSADDR(pfn_to_pa(ht->ht_pfn), entry); 3597 if (created) 3598 hati_reserves_exit(0); 3599 return (p); 3600 } 3601 3602 /* 3603 * Release a CPU private mapping for the given address. 3604 * We decrement the htable valid count so it might be destroyed. 3605 */ 3606 /*ARGSUSED1*/ 3607 void 3608 hat_mempte_release(caddr_t addr, hat_mempte_t pte_pa) 3609 { 3610 htable_t *ht; 3611 3612 /* 3613 * invalidate any left over mapping and decrement the htable valid count 3614 */ 3615 { 3616 x86pte_t *pteptr; 3617 3618 pteptr = x86pte_mapin(mmu_btop(pte_pa), 3619 (pte_pa & MMU_PAGEOFFSET) >> mmu.pte_size_shift, NULL); 3620 if (mmu.pae_hat) 3621 *pteptr = 0; 3622 else 3623 *(x86pte32_t *)pteptr = 0; 3624 mmu_tlbflush_entry(addr); 3625 x86pte_mapout(); 3626 } 3627 3628 ht = htable_getpte(kas.a_hat, ALIGN2PAGE(addr), NULL, NULL, 0); 3629 if (ht == NULL) 3630 panic("hat_mempte_release(): invalid address"); 3631 ASSERT(ht->ht_level == 0); 3632 HTABLE_DEC(ht->ht_valid_cnt); 3633 htable_release(ht); 3634 } 3635 3636 /* 3637 * Apply a temporary CPU private mapping to a page. We flush the TLB only 3638 * on this CPU, so this ought to have been called with preemption disabled. 3639 */ 3640 void 3641 hat_mempte_remap( 3642 pfn_t pfn, 3643 caddr_t addr, 3644 hat_mempte_t pte_pa, 3645 uint_t attr, 3646 uint_t flags) 3647 { 3648 uintptr_t va = (uintptr_t)addr; 3649 x86pte_t pte; 3650 3651 /* 3652 * Remap the given PTE to the new page's PFN. Invalidate only 3653 * on this CPU. 3654 */ 3655 #ifdef DEBUG 3656 htable_t *ht; 3657 uint_t entry; 3658 3659 ASSERT(IS_PAGEALIGNED(va)); 3660 ASSERT(!IN_VA_HOLE(va)); 3661 ht = htable_getpte(kas.a_hat, va, &entry, NULL, 0); 3662 ASSERT(ht != NULL); 3663 ASSERT(ht->ht_level == 0); 3664 ASSERT(ht->ht_valid_cnt > 0); 3665 ASSERT(ht->ht_pfn == mmu_btop(pte_pa)); 3666 htable_release(ht); 3667 #endif 3668 pte = hati_mkpte(pfn, attr, 0, flags); 3669 { 3670 x86pte_t *pteptr; 3671 3672 pteptr = x86pte_mapin(mmu_btop(pte_pa), 3673 (pte_pa & MMU_PAGEOFFSET) >> mmu.pte_size_shift, NULL); 3674 if (mmu.pae_hat) 3675 *(x86pte_t *)pteptr = pte; 3676 else 3677 *(x86pte32_t *)pteptr = (x86pte32_t)pte; 3678 mmu_tlbflush_entry(addr); 3679 x86pte_mapout(); 3680 } 3681 } 3682 3683 3684 3685 /* 3686 * Hat locking functions 3687 * XXX - these two functions are currently being used by hatstats 3688 * they can be removed by using a per-as mutex for hatstats. 3689 */ 3690 void 3691 hat_enter(hat_t *hat) 3692 { 3693 mutex_enter(&hat->hat_mutex); 3694 } 3695 3696 void 3697 hat_exit(hat_t *hat) 3698 { 3699 mutex_exit(&hat->hat_mutex); 3700 } 3701 3702 /* 3703 * HAT part of cpu initialization. 3704 */ 3705 void 3706 hat_cpu_online(struct cpu *cpup) 3707 { 3708 if (cpup != CPU) { 3709 x86pte_cpu_init(cpup); 3710 hat_vlp_setup(cpup); 3711 } 3712 CPUSET_ATOMIC_ADD(khat_cpuset, cpup->cpu_id); 3713 } 3714 3715 /* 3716 * HAT part of cpu deletion. 3717 * (currently, we only call this after the cpu is safely passivated.) 3718 */ 3719 void 3720 hat_cpu_offline(struct cpu *cpup) 3721 { 3722 ASSERT(cpup != CPU); 3723 3724 CPUSET_ATOMIC_DEL(khat_cpuset, cpup->cpu_id); 3725 x86pte_cpu_fini(cpup); 3726 hat_vlp_teardown(cpup); 3727 } 3728 3729 /* 3730 * Function called after all CPUs are brought online. 3731 * Used to remove low address boot mappings. 3732 */ 3733 void 3734 clear_boot_mappings(uintptr_t low, uintptr_t high) 3735 { 3736 uintptr_t vaddr = low; 3737 htable_t *ht = NULL; 3738 level_t level; 3739 uint_t entry; 3740 x86pte_t pte; 3741 3742 /* 3743 * On 1st CPU we can unload the prom mappings, basically we blow away 3744 * all virtual mappings under _userlimit. 3745 */ 3746 while (vaddr < high) { 3747 pte = htable_walk(kas.a_hat, &ht, &vaddr, high); 3748 if (ht == NULL) 3749 break; 3750 3751 level = ht->ht_level; 3752 entry = htable_va2entry(vaddr, ht); 3753 ASSERT(level <= mmu.max_page_level); 3754 ASSERT(PTE_ISPAGE(pte, level)); 3755 3756 /* 3757 * Unload the mapping from the page tables. 3758 */ 3759 (void) x86pte_inval(ht, entry, 0, NULL); 3760 ASSERT(ht->ht_valid_cnt > 0); 3761 HTABLE_DEC(ht->ht_valid_cnt); 3762 PGCNT_DEC(ht->ht_hat, ht->ht_level); 3763 3764 vaddr += LEVEL_SIZE(ht->ht_level); 3765 } 3766 if (ht) 3767 htable_release(ht); 3768 } 3769 3770 /* 3771 * Atomically update a new translation for a single page. If the 3772 * currently installed PTE doesn't match the value we expect to find, 3773 * it's not updated and we return the PTE we found. 3774 * 3775 * If activating nosync or NOWRITE and the page was modified we need to sync 3776 * with the page_t. Also sync with page_t if clearing ref/mod bits. 3777 */ 3778 static x86pte_t 3779 hati_update_pte(htable_t *ht, uint_t entry, x86pte_t expected, x86pte_t new) 3780 { 3781 page_t *pp; 3782 uint_t rm = 0; 3783 x86pte_t replaced; 3784 3785 if (PTE_GET(expected, PT_SOFTWARE) < PT_NOSYNC && 3786 PTE_GET(expected, PT_MOD | PT_REF) && 3787 (PTE_GET(new, PT_NOSYNC) || !PTE_GET(new, PT_WRITABLE) || 3788 !PTE_GET(new, PT_MOD | PT_REF))) { 3789 3790 ASSERT(!pfn_is_foreign(PTE2PFN(expected, ht->ht_level))); 3791 pp = page_numtopp_nolock(PTE2PFN(expected, ht->ht_level)); 3792 ASSERT(pp != NULL); 3793 if (PTE_GET(expected, PT_MOD)) 3794 rm |= P_MOD; 3795 if (PTE_GET(expected, PT_REF)) 3796 rm |= P_REF; 3797 PTE_CLR(new, PT_MOD | PT_REF); 3798 } 3799 3800 replaced = x86pte_update(ht, entry, expected, new); 3801 if (replaced != expected) 3802 return (replaced); 3803 3804 if (rm) { 3805 /* 3806 * sync to all constituent pages of a large page 3807 */ 3808 pgcnt_t pgcnt = page_get_pagecnt(ht->ht_level); 3809 ASSERT(IS_P2ALIGNED(pp->p_pagenum, pgcnt)); 3810 while (pgcnt-- > 0) { 3811 /* 3812 * hat_page_demote() can't decrease 3813 * pszc below this mapping size 3814 * since large mapping existed after we 3815 * took mlist lock. 3816 */ 3817 ASSERT(pp->p_szc >= ht->ht_level); 3818 hat_page_setattr(pp, rm); 3819 ++pp; 3820 } 3821 } 3822 3823 return (0); 3824 } 3825 3826 /* 3827 * Kernel Physical Mapping (kpm) facility 3828 * 3829 * Most of the routines needed to support segkpm are almost no-ops on the 3830 * x86 platform. We map in the entire segment when it is created and leave 3831 * it mapped in, so there is no additional work required to set up and tear 3832 * down individual mappings. All of these routines were created to support 3833 * SPARC platforms that have to avoid aliasing in their virtually indexed 3834 * caches. 3835 * 3836 * Most of the routines have sanity checks in them (e.g. verifying that the 3837 * passed-in page is locked). We don't actually care about most of these 3838 * checks on x86, but we leave them in place to identify problems in the 3839 * upper levels. 3840 */ 3841 3842 /* 3843 * Map in a locked page and return the vaddr. 3844 */ 3845 /*ARGSUSED*/ 3846 caddr_t 3847 hat_kpm_mapin(struct page *pp, struct kpme *kpme) 3848 { 3849 caddr_t vaddr; 3850 3851 #ifdef DEBUG 3852 if (kpm_enable == 0) { 3853 cmn_err(CE_WARN, "hat_kpm_mapin: kpm_enable not set\n"); 3854 return ((caddr_t)NULL); 3855 } 3856 3857 if (pp == NULL || PAGE_LOCKED(pp) == 0) { 3858 cmn_err(CE_WARN, "hat_kpm_mapin: pp zero or not locked\n"); 3859 return ((caddr_t)NULL); 3860 } 3861 #endif 3862 3863 vaddr = hat_kpm_page2va(pp, 1); 3864 3865 return (vaddr); 3866 } 3867 3868 /* 3869 * Mapout a locked page. 3870 */ 3871 /*ARGSUSED*/ 3872 void 3873 hat_kpm_mapout(struct page *pp, struct kpme *kpme, caddr_t vaddr) 3874 { 3875 #ifdef DEBUG 3876 if (kpm_enable == 0) { 3877 cmn_err(CE_WARN, "hat_kpm_mapout: kpm_enable not set\n"); 3878 return; 3879 } 3880 3881 if (IS_KPM_ADDR(vaddr) == 0) { 3882 cmn_err(CE_WARN, "hat_kpm_mapout: no kpm address\n"); 3883 return; 3884 } 3885 3886 if (pp == NULL || PAGE_LOCKED(pp) == 0) { 3887 cmn_err(CE_WARN, "hat_kpm_mapout: page zero or not locked\n"); 3888 return; 3889 } 3890 #endif 3891 } 3892 3893 /* 3894 * Return the kpm virtual address for a specific pfn 3895 */ 3896 caddr_t 3897 hat_kpm_pfn2va(pfn_t pfn) 3898 { 3899 uintptr_t vaddr = (uintptr_t)kpm_vbase + mmu_ptob(pfn); 3900 3901 return ((caddr_t)vaddr); 3902 } 3903 3904 /* 3905 * Return the kpm virtual address for the page at pp. 3906 */ 3907 /*ARGSUSED*/ 3908 caddr_t 3909 hat_kpm_page2va(struct page *pp, int checkswap) 3910 { 3911 return (hat_kpm_pfn2va(pp->p_pagenum)); 3912 } 3913 3914 /* 3915 * Return the page frame number for the kpm virtual address vaddr. 3916 */ 3917 pfn_t 3918 hat_kpm_va2pfn(caddr_t vaddr) 3919 { 3920 pfn_t pfn; 3921 3922 ASSERT(IS_KPM_ADDR(vaddr)); 3923 3924 pfn = (pfn_t)btop(vaddr - kpm_vbase); 3925 3926 return (pfn); 3927 } 3928 3929 3930 /* 3931 * Return the page for the kpm virtual address vaddr. 3932 */ 3933 page_t * 3934 hat_kpm_vaddr2page(caddr_t vaddr) 3935 { 3936 pfn_t pfn; 3937 3938 ASSERT(IS_KPM_ADDR(vaddr)); 3939 3940 pfn = hat_kpm_va2pfn(vaddr); 3941 3942 return (page_numtopp_nolock(pfn)); 3943 } 3944 3945 /* 3946 * hat_kpm_fault is called from segkpm_fault when we take a page fault on a 3947 * KPM page. This should never happen on x86 3948 */ 3949 int 3950 hat_kpm_fault(hat_t *hat, caddr_t vaddr) 3951 { 3952 panic("pagefault in seg_kpm. hat: 0x%p vaddr: 0x%p", hat, vaddr); 3953 3954 return (0); 3955 } 3956 3957 /*ARGSUSED*/ 3958 void 3959 hat_kpm_mseghash_clear(int nentries) 3960 {} 3961 3962 /*ARGSUSED*/ 3963 void 3964 hat_kpm_mseghash_update(pgcnt_t inx, struct memseg *msp) 3965 {} 3966