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