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 Spitfire MMU. 30 * 31 * This file implements the machine specific hardware translation 32 * needed by the VM system. The machine independent interface is 33 * described in <vm/hat.h> while the machine dependent interface 34 * and data structures are described in <vm/hat_sfmmu.h>. 35 * 36 * The hat layer manages the address translation hardware as a cache 37 * driven by calls from the higher levels in the VM system. 38 */ 39 40 #include <sys/types.h> 41 #include <sys/kstat.h> 42 #include <vm/hat.h> 43 #include <vm/hat_sfmmu.h> 44 #include <vm/page.h> 45 #include <sys/pte.h> 46 #include <sys/systm.h> 47 #include <sys/mman.h> 48 #include <sys/sysmacros.h> 49 #include <sys/machparam.h> 50 #include <sys/vtrace.h> 51 #include <sys/kmem.h> 52 #include <sys/mmu.h> 53 #include <sys/cmn_err.h> 54 #include <sys/cpu.h> 55 #include <sys/cpuvar.h> 56 #include <sys/debug.h> 57 #include <sys/lgrp.h> 58 #include <sys/archsystm.h> 59 #include <sys/machsystm.h> 60 #include <sys/vmsystm.h> 61 #include <vm/as.h> 62 #include <vm/seg.h> 63 #include <vm/seg_kp.h> 64 #include <vm/seg_kmem.h> 65 #include <vm/seg_kpm.h> 66 #include <vm/rm.h> 67 #include <sys/t_lock.h> 68 #include <sys/obpdefs.h> 69 #include <sys/vm_machparam.h> 70 #include <sys/var.h> 71 #include <sys/trap.h> 72 #include <sys/machtrap.h> 73 #include <sys/scb.h> 74 #include <sys/bitmap.h> 75 #include <sys/machlock.h> 76 #include <sys/membar.h> 77 #include <sys/atomic.h> 78 #include <sys/cpu_module.h> 79 #include <sys/prom_debug.h> 80 #include <sys/ksynch.h> 81 #include <sys/mem_config.h> 82 #include <sys/mem_cage.h> 83 #include <sys/dtrace.h> 84 #include <vm/vm_dep.h> 85 #include <vm/xhat_sfmmu.h> 86 #include <sys/fpu/fpusystm.h> 87 #include <vm/mach_kpm.h> 88 89 #if defined(SF_ERRATA_57) 90 extern caddr_t errata57_limit; 91 #endif 92 93 #define HME8BLK_SZ_RND ((roundup(HME8BLK_SZ, sizeof (int64_t))) / \ 94 (sizeof (int64_t))) 95 #define HBLK_RESERVE ((struct hme_blk *)hblk_reserve) 96 97 #define HBLK_RESERVE_CNT 128 98 #define HBLK_RESERVE_MIN 20 99 100 static struct hme_blk *freehblkp; 101 static kmutex_t freehblkp_lock; 102 static int freehblkcnt; 103 104 static int64_t hblk_reserve[HME8BLK_SZ_RND]; 105 static kmutex_t hblk_reserve_lock; 106 static kthread_t *hblk_reserve_thread; 107 108 static nucleus_hblk8_info_t nucleus_hblk8; 109 static nucleus_hblk1_info_t nucleus_hblk1; 110 111 /* 112 * SFMMU specific hat functions 113 */ 114 void hat_pagecachectl(struct page *, int); 115 116 /* flags for hat_pagecachectl */ 117 #define HAT_CACHE 0x1 118 #define HAT_UNCACHE 0x2 119 #define HAT_TMPNC 0x4 120 121 /* 122 * Flag to allow the creation of non-cacheable translations 123 * to system memory. It is off by default. At the moment this 124 * flag is used by the ecache error injector. The error injector 125 * will turn it on when creating such a translation then shut it 126 * off when it's finished. 127 */ 128 129 int sfmmu_allow_nc_trans = 0; 130 131 /* 132 * Flag to disable large page support. 133 * value of 1 => disable all large pages. 134 * bits 1, 2, and 3 are to disable 64K, 512K and 4M pages respectively. 135 * 136 * For example, use the value 0x4 to disable 512K pages. 137 * 138 */ 139 #define LARGE_PAGES_OFF 0x1 140 141 /* 142 * WARNING: 512K pages MUST be disabled for ISM/DISM. If not 143 * a process would page fault indefinitely if it tried to 144 * access a 512K page. 145 */ 146 int disable_ism_large_pages = (1 << TTE512K); 147 int disable_large_pages = 0; 148 int disable_auto_large_pages = 0; 149 150 /* 151 * Private sfmmu data structures for hat management 152 */ 153 static struct kmem_cache *sfmmuid_cache; 154 static struct kmem_cache *mmuctxdom_cache; 155 156 /* 157 * Private sfmmu data structures for tsb management 158 */ 159 static struct kmem_cache *sfmmu_tsbinfo_cache; 160 static struct kmem_cache *sfmmu_tsb8k_cache; 161 static struct kmem_cache *sfmmu_tsb_cache[NLGRPS_MAX]; 162 static vmem_t *kmem_tsb_arena; 163 164 /* 165 * sfmmu static variables for hmeblk resource management. 166 */ 167 static vmem_t *hat_memload1_arena; /* HAT translation arena for sfmmu1_cache */ 168 static struct kmem_cache *sfmmu8_cache; 169 static struct kmem_cache *sfmmu1_cache; 170 static struct kmem_cache *pa_hment_cache; 171 172 static kmutex_t ism_mlist_lock; /* mutex for ism mapping list */ 173 /* 174 * private data for ism 175 */ 176 static struct kmem_cache *ism_blk_cache; 177 static struct kmem_cache *ism_ment_cache; 178 #define ISMID_STARTADDR NULL 179 180 /* 181 * Whether to delay TLB flushes and use Cheetah's flush-all support 182 * when removing contexts from the dirty list. 183 */ 184 int delay_tlb_flush; 185 int disable_delay_tlb_flush; 186 187 /* 188 * ``hat_lock'' is a hashed mutex lock for protecting sfmmu TSB lists, 189 * HAT flags, synchronizing TLB/TSB coherency, and context management. 190 * The lock is hashed on the sfmmup since the case where we need to lock 191 * all processes is rare but does occur (e.g. we need to unload a shared 192 * mapping from all processes using the mapping). We have a lot of buckets, 193 * and each slab of sfmmu_t's can use about a quarter of them, giving us 194 * a fairly good distribution without wasting too much space and overhead 195 * when we have to grab them all. 196 */ 197 #define SFMMU_NUM_LOCK 128 /* must be power of two */ 198 hatlock_t hat_lock[SFMMU_NUM_LOCK]; 199 200 /* 201 * Hash algorithm optimized for a small number of slabs. 202 * 7 is (highbit((sizeof sfmmu_t)) - 1) 203 * This hash algorithm is based upon the knowledge that sfmmu_t's come from a 204 * kmem_cache, and thus they will be sequential within that cache. In 205 * addition, each new slab will have a different "color" up to cache_maxcolor 206 * which will skew the hashing for each successive slab which is allocated. 207 * If the size of sfmmu_t changed to a larger size, this algorithm may need 208 * to be revisited. 209 */ 210 #define TSB_HASH_SHIFT_BITS (7) 211 #define PTR_HASH(x) ((uintptr_t)x >> TSB_HASH_SHIFT_BITS) 212 213 #ifdef DEBUG 214 int tsb_hash_debug = 0; 215 #define TSB_HASH(sfmmup) \ 216 (tsb_hash_debug ? &hat_lock[0] : \ 217 &hat_lock[PTR_HASH(sfmmup) & (SFMMU_NUM_LOCK-1)]) 218 #else /* DEBUG */ 219 #define TSB_HASH(sfmmup) &hat_lock[PTR_HASH(sfmmup) & (SFMMU_NUM_LOCK-1)] 220 #endif /* DEBUG */ 221 222 223 /* sfmmu_replace_tsb() return codes. */ 224 typedef enum tsb_replace_rc { 225 TSB_SUCCESS, 226 TSB_ALLOCFAIL, 227 TSB_LOSTRACE, 228 TSB_ALREADY_SWAPPED, 229 TSB_CANTGROW 230 } tsb_replace_rc_t; 231 232 /* 233 * Flags for TSB allocation routines. 234 */ 235 #define TSB_ALLOC 0x01 236 #define TSB_FORCEALLOC 0x02 237 #define TSB_GROW 0x04 238 #define TSB_SHRINK 0x08 239 #define TSB_SWAPIN 0x10 240 241 /* 242 * Support for HAT callbacks. 243 */ 244 #define SFMMU_MAX_RELOC_CALLBACKS 10 245 int sfmmu_max_cb_id = SFMMU_MAX_RELOC_CALLBACKS; 246 static id_t sfmmu_cb_nextid = 0; 247 static id_t sfmmu_tsb_cb_id; 248 struct sfmmu_callback *sfmmu_cb_table; 249 250 /* 251 * Kernel page relocation is enabled by default for non-caged 252 * kernel pages. This has little effect unless segkmem_reloc is 253 * set, since by default kernel memory comes from inside the 254 * kernel cage. 255 */ 256 int hat_kpr_enabled = 1; 257 258 kmutex_t kpr_mutex; 259 kmutex_t kpr_suspendlock; 260 kthread_t *kreloc_thread; 261 262 /* 263 * Enable VA->PA translation sanity checking on DEBUG kernels. 264 * Disabled by default. This is incompatible with some 265 * drivers (error injector, RSM) so if it breaks you get 266 * to keep both pieces. 267 */ 268 int hat_check_vtop = 0; 269 270 /* 271 * Private sfmmu routines (prototypes) 272 */ 273 static struct hme_blk *sfmmu_shadow_hcreate(sfmmu_t *, caddr_t, int, uint_t); 274 static struct hme_blk *sfmmu_hblk_alloc(sfmmu_t *, caddr_t, 275 struct hmehash_bucket *, uint_t, hmeblk_tag, uint_t); 276 static caddr_t sfmmu_hblk_unload(struct hat *, struct hme_blk *, caddr_t, 277 caddr_t, demap_range_t *, uint_t); 278 static caddr_t sfmmu_hblk_sync(struct hat *, struct hme_blk *, caddr_t, 279 caddr_t, int); 280 static void sfmmu_hblk_free(struct hmehash_bucket *, struct hme_blk *, 281 uint64_t, struct hme_blk **); 282 static void sfmmu_hblks_list_purge(struct hme_blk **); 283 static uint_t sfmmu_get_free_hblk(struct hme_blk **, uint_t); 284 static uint_t sfmmu_put_free_hblk(struct hme_blk *, uint_t); 285 static struct hme_blk *sfmmu_hblk_steal(int); 286 static int sfmmu_steal_this_hblk(struct hmehash_bucket *, 287 struct hme_blk *, uint64_t, uint64_t, 288 struct hme_blk *); 289 static caddr_t sfmmu_hblk_unlock(struct hme_blk *, caddr_t, caddr_t); 290 291 static void sfmmu_memload_batchsmall(struct hat *, caddr_t, page_t **, 292 uint_t, uint_t, pgcnt_t); 293 void sfmmu_tteload(struct hat *, tte_t *, caddr_t, page_t *, 294 uint_t); 295 static int sfmmu_tteload_array(sfmmu_t *, tte_t *, caddr_t, page_t **, 296 uint_t); 297 static struct hmehash_bucket *sfmmu_tteload_acquire_hashbucket(sfmmu_t *, 298 caddr_t, int); 299 static struct hme_blk *sfmmu_tteload_find_hmeblk(sfmmu_t *, 300 struct hmehash_bucket *, caddr_t, uint_t, uint_t); 301 static int sfmmu_tteload_addentry(sfmmu_t *, struct hme_blk *, tte_t *, 302 caddr_t, page_t **, uint_t); 303 static void sfmmu_tteload_release_hashbucket(struct hmehash_bucket *); 304 305 static int sfmmu_pagearray_setup(caddr_t, page_t **, tte_t *, int); 306 pfn_t sfmmu_uvatopfn(caddr_t, sfmmu_t *); 307 void sfmmu_memtte(tte_t *, pfn_t, uint_t, int); 308 #ifdef VAC 309 static void sfmmu_vac_conflict(struct hat *, caddr_t, page_t *); 310 static int sfmmu_vacconflict_array(caddr_t, page_t *, int *); 311 int tst_tnc(page_t *pp, pgcnt_t); 312 void conv_tnc(page_t *pp, int); 313 #endif 314 315 static void sfmmu_get_ctx(sfmmu_t *); 316 static void sfmmu_free_sfmmu(sfmmu_t *); 317 318 static void sfmmu_gettte(struct hat *, caddr_t, tte_t *); 319 static void sfmmu_ttesync(struct hat *, caddr_t, tte_t *, page_t *); 320 static void sfmmu_chgattr(struct hat *, caddr_t, size_t, uint_t, int); 321 322 cpuset_t sfmmu_pageunload(page_t *, struct sf_hment *, int); 323 static void hat_pagereload(struct page *, struct page *); 324 static cpuset_t sfmmu_pagesync(page_t *, struct sf_hment *, uint_t); 325 #ifdef VAC 326 void sfmmu_page_cache_array(page_t *, int, int, pgcnt_t); 327 static void sfmmu_page_cache(page_t *, int, int, int); 328 #endif 329 330 static void sfmmu_tlbcache_demap(caddr_t, sfmmu_t *, struct hme_blk *, 331 pfn_t, int, int, int, int); 332 static void sfmmu_ismtlbcache_demap(caddr_t, sfmmu_t *, struct hme_blk *, 333 pfn_t, int); 334 static void sfmmu_tlb_demap(caddr_t, sfmmu_t *, struct hme_blk *, int, int); 335 static void sfmmu_tlb_range_demap(demap_range_t *); 336 static void sfmmu_invalidate_ctx(sfmmu_t *); 337 static void sfmmu_sync_mmustate(sfmmu_t *); 338 339 static void sfmmu_tsbinfo_setup_phys(struct tsb_info *, pfn_t); 340 static int sfmmu_tsbinfo_alloc(struct tsb_info **, int, int, uint_t, 341 sfmmu_t *); 342 static void sfmmu_tsb_free(struct tsb_info *); 343 static void sfmmu_tsbinfo_free(struct tsb_info *); 344 static int sfmmu_init_tsbinfo(struct tsb_info *, int, int, uint_t, 345 sfmmu_t *); 346 347 static void sfmmu_tsb_swapin(sfmmu_t *, hatlock_t *); 348 static int sfmmu_select_tsb_szc(pgcnt_t); 349 static void sfmmu_mod_tsb(sfmmu_t *, caddr_t, tte_t *, int); 350 #define sfmmu_load_tsb(sfmmup, vaddr, tte, szc) \ 351 sfmmu_mod_tsb(sfmmup, vaddr, tte, szc) 352 #define sfmmu_unload_tsb(sfmmup, vaddr, szc) \ 353 sfmmu_mod_tsb(sfmmup, vaddr, NULL, szc) 354 static void sfmmu_copy_tsb(struct tsb_info *, struct tsb_info *); 355 static tsb_replace_rc_t sfmmu_replace_tsb(sfmmu_t *, struct tsb_info *, uint_t, 356 hatlock_t *, uint_t); 357 static void sfmmu_size_tsb(sfmmu_t *, int, uint64_t, uint64_t, int); 358 359 #ifdef VAC 360 void sfmmu_cache_flush(pfn_t, int); 361 void sfmmu_cache_flushcolor(int, pfn_t); 362 #endif 363 static caddr_t sfmmu_hblk_chgattr(sfmmu_t *, struct hme_blk *, caddr_t, 364 caddr_t, demap_range_t *, uint_t, int); 365 366 static uint64_t sfmmu_vtop_attr(uint_t, int mode, tte_t *); 367 static uint_t sfmmu_ptov_attr(tte_t *); 368 static caddr_t sfmmu_hblk_chgprot(sfmmu_t *, struct hme_blk *, caddr_t, 369 caddr_t, demap_range_t *, uint_t); 370 static uint_t sfmmu_vtop_prot(uint_t, uint_t *); 371 static int sfmmu_idcache_constructor(void *, void *, int); 372 static void sfmmu_idcache_destructor(void *, void *); 373 static int sfmmu_hblkcache_constructor(void *, void *, int); 374 static void sfmmu_hblkcache_destructor(void *, void *); 375 static void sfmmu_hblkcache_reclaim(void *); 376 static void sfmmu_shadow_hcleanup(sfmmu_t *, struct hme_blk *, 377 struct hmehash_bucket *); 378 static void sfmmu_free_hblks(sfmmu_t *, caddr_t, caddr_t, int); 379 static void sfmmu_rm_large_mappings(page_t *, int); 380 381 static void hat_lock_init(void); 382 static void hat_kstat_init(void); 383 static int sfmmu_kstat_percpu_update(kstat_t *ksp, int rw); 384 static void sfmmu_check_page_sizes(sfmmu_t *, int); 385 int fnd_mapping_sz(page_t *); 386 static void iment_add(struct ism_ment *, struct hat *); 387 static void iment_sub(struct ism_ment *, struct hat *); 388 static pgcnt_t ism_tsb_entries(sfmmu_t *, int szc); 389 extern void sfmmu_setup_tsbinfo(sfmmu_t *); 390 extern void sfmmu_clear_utsbinfo(void); 391 392 static void sfmmu_ctx_wrap_around(mmu_ctx_t *); 393 394 /* kpm globals */ 395 #ifdef DEBUG 396 /* 397 * Enable trap level tsbmiss handling 398 */ 399 int kpm_tsbmtl = 1; 400 401 /* 402 * Flush the TLB on kpm mapout. Note: Xcalls are used (again) for the 403 * required TLB shootdowns in this case, so handle w/ care. Off by default. 404 */ 405 int kpm_tlb_flush; 406 #endif /* DEBUG */ 407 408 static void *sfmmu_vmem_xalloc_aligned_wrapper(vmem_t *, size_t, int); 409 410 #ifdef DEBUG 411 static void sfmmu_check_hblk_flist(); 412 #endif 413 414 /* 415 * Semi-private sfmmu data structures. Some of them are initialize in 416 * startup or in hat_init. Some of them are private but accessed by 417 * assembly code or mach_sfmmu.c 418 */ 419 struct hmehash_bucket *uhme_hash; /* user hmeblk hash table */ 420 struct hmehash_bucket *khme_hash; /* kernel hmeblk hash table */ 421 uint64_t uhme_hash_pa; /* PA of uhme_hash */ 422 uint64_t khme_hash_pa; /* PA of khme_hash */ 423 int uhmehash_num; /* # of buckets in user hash table */ 424 int khmehash_num; /* # of buckets in kernel hash table */ 425 426 uint_t max_mmu_ctxdoms = 0; /* max context domains in the system */ 427 mmu_ctx_t **mmu_ctxs_tbl; /* global array of context domains */ 428 uint64_t mmu_saved_gnum = 0; /* to init incoming MMUs' gnums */ 429 430 #define DEFAULT_NUM_CTXS_PER_MMU 8192 431 static uint_t nctxs = DEFAULT_NUM_CTXS_PER_MMU; 432 433 int cache; /* describes system cache */ 434 435 caddr_t ktsb_base; /* kernel 8k-indexed tsb base address */ 436 uint64_t ktsb_pbase; /* kernel 8k-indexed tsb phys address */ 437 int ktsb_szcode; /* kernel 8k-indexed tsb size code */ 438 int ktsb_sz; /* kernel 8k-indexed tsb size */ 439 440 caddr_t ktsb4m_base; /* kernel 4m-indexed tsb base address */ 441 uint64_t ktsb4m_pbase; /* kernel 4m-indexed tsb phys address */ 442 int ktsb4m_szcode; /* kernel 4m-indexed tsb size code */ 443 int ktsb4m_sz; /* kernel 4m-indexed tsb size */ 444 445 uint64_t kpm_tsbbase; /* kernel seg_kpm 4M TSB base address */ 446 int kpm_tsbsz; /* kernel seg_kpm 4M TSB size code */ 447 uint64_t kpmsm_tsbbase; /* kernel seg_kpm 8K TSB base address */ 448 int kpmsm_tsbsz; /* kernel seg_kpm 8K TSB size code */ 449 450 #ifndef sun4v 451 int utsb_dtlb_ttenum = -1; /* index in TLB for utsb locked TTE */ 452 int utsb4m_dtlb_ttenum = -1; /* index in TLB for 4M TSB TTE */ 453 int dtlb_resv_ttenum; /* index in TLB of first reserved TTE */ 454 caddr_t utsb_vabase; /* reserved kernel virtual memory */ 455 caddr_t utsb4m_vabase; /* for trap handler TSB accesses */ 456 #endif /* sun4v */ 457 uint64_t tsb_alloc_bytes = 0; /* bytes allocated to TSBs */ 458 vmem_t *kmem_tsb_default_arena[NLGRPS_MAX]; /* For dynamic TSBs */ 459 460 /* 461 * Size to use for TSB slabs. Future platforms that support page sizes 462 * larger than 4M may wish to change these values, and provide their own 463 * assembly macros for building and decoding the TSB base register contents. 464 * Note disable_large_pages will override the value set here. 465 */ 466 uint_t tsb_slab_ttesz = TTE4M; 467 uint_t tsb_slab_size; 468 uint_t tsb_slab_shift; 469 uint_t tsb_slab_mask; /* PFN mask for TTE */ 470 471 /* largest TSB size to grow to, will be smaller on smaller memory systems */ 472 int tsb_max_growsize = UTSB_MAX_SZCODE; 473 474 /* 475 * Tunable parameters dealing with TSB policies. 476 */ 477 478 /* 479 * This undocumented tunable forces all 8K TSBs to be allocated from 480 * the kernel heap rather than from the kmem_tsb_default_arena arenas. 481 */ 482 #ifdef DEBUG 483 int tsb_forceheap = 0; 484 #endif /* DEBUG */ 485 486 /* 487 * Decide whether to use per-lgroup arenas, or one global set of 488 * TSB arenas. The default is not to break up per-lgroup, since 489 * most platforms don't recognize any tangible benefit from it. 490 */ 491 int tsb_lgrp_affinity = 0; 492 493 /* 494 * Used for growing the TSB based on the process RSS. 495 * tsb_rss_factor is based on the smallest TSB, and is 496 * shifted by the TSB size to determine if we need to grow. 497 * The default will grow the TSB if the number of TTEs for 498 * this page size exceeds 75% of the number of TSB entries, 499 * which should _almost_ eliminate all conflict misses 500 * (at the expense of using up lots and lots of memory). 501 */ 502 #define TSB_RSS_FACTOR (TSB_ENTRIES(TSB_MIN_SZCODE) * 0.75) 503 #define SFMMU_RSS_TSBSIZE(tsbszc) (tsb_rss_factor << tsbszc) 504 #define SELECT_TSB_SIZECODE(pgcnt) ( \ 505 (enable_tsb_rss_sizing)? sfmmu_select_tsb_szc(pgcnt) : \ 506 default_tsb_size) 507 #define TSB_OK_SHRINK() \ 508 (tsb_alloc_bytes > tsb_alloc_hiwater || freemem < desfree) 509 #define TSB_OK_GROW() \ 510 (tsb_alloc_bytes < tsb_alloc_hiwater && freemem > desfree) 511 512 int enable_tsb_rss_sizing = 1; 513 int tsb_rss_factor = (int)TSB_RSS_FACTOR; 514 515 /* which TSB size code to use for new address spaces or if rss sizing off */ 516 int default_tsb_size = TSB_8K_SZCODE; 517 518 static uint64_t tsb_alloc_hiwater; /* limit TSB reserved memory */ 519 uint64_t tsb_alloc_hiwater_factor; /* tsb_alloc_hiwater = physmem / this */ 520 #define TSB_ALLOC_HIWATER_FACTOR_DEFAULT 32 521 522 #ifdef DEBUG 523 static int tsb_random_size = 0; /* set to 1 to test random tsb sizes on alloc */ 524 static int tsb_grow_stress = 0; /* if set to 1, keep replacing TSB w/ random */ 525 static int tsb_alloc_mtbf = 0; /* fail allocation every n attempts */ 526 static int tsb_alloc_fail_mtbf = 0; 527 static int tsb_alloc_count = 0; 528 #endif /* DEBUG */ 529 530 /* if set to 1, will remap valid TTEs when growing TSB. */ 531 int tsb_remap_ttes = 1; 532 533 /* 534 * If we have more than this many mappings, allocate a second TSB. 535 * This default is chosen because the I/D fully associative TLBs are 536 * assumed to have at least 8 available entries. Platforms with a 537 * larger fully-associative TLB could probably override the default. 538 */ 539 int tsb_sectsb_threshold = 8; 540 541 /* 542 * kstat data 543 */ 544 struct sfmmu_global_stat sfmmu_global_stat; 545 struct sfmmu_tsbsize_stat sfmmu_tsbsize_stat; 546 547 /* 548 * Global data 549 */ 550 sfmmu_t *ksfmmup; /* kernel's hat id */ 551 552 #ifdef DEBUG 553 static void chk_tte(tte_t *, tte_t *, tte_t *, struct hme_blk *); 554 #endif 555 556 /* sfmmu locking operations */ 557 static kmutex_t *sfmmu_mlspl_enter(struct page *, int); 558 static int sfmmu_mlspl_held(struct page *, int); 559 560 kmutex_t *sfmmu_page_enter(page_t *); 561 void sfmmu_page_exit(kmutex_t *); 562 int sfmmu_page_spl_held(struct page *); 563 564 /* sfmmu internal locking operations - accessed directly */ 565 static void sfmmu_mlist_reloc_enter(page_t *, page_t *, 566 kmutex_t **, kmutex_t **); 567 static void sfmmu_mlist_reloc_exit(kmutex_t *, kmutex_t *); 568 static hatlock_t * 569 sfmmu_hat_enter(sfmmu_t *); 570 static hatlock_t * 571 sfmmu_hat_tryenter(sfmmu_t *); 572 static void sfmmu_hat_exit(hatlock_t *); 573 static void sfmmu_hat_lock_all(void); 574 static void sfmmu_hat_unlock_all(void); 575 static void sfmmu_ismhat_enter(sfmmu_t *, int); 576 static void sfmmu_ismhat_exit(sfmmu_t *, int); 577 578 /* 579 * Array of mutexes protecting a page's mapping list and p_nrm field. 580 * 581 * The hash function looks complicated, but is made up so that: 582 * 583 * "pp" not shifted, so adjacent pp values will hash to different cache lines 584 * (8 byte alignment * 8 bytes/mutes == 64 byte coherency subblock) 585 * 586 * "pp" >> mml_shift, incorporates more source bits into the hash result 587 * 588 * "& (mml_table_size - 1), should be faster than using remainder "%" 589 * 590 * Hopefully, mml_table, mml_table_size and mml_shift are all in the same 591 * cacheline, since they get declared next to each other below. We'll trust 592 * ld not to do something random. 593 */ 594 #ifdef DEBUG 595 int mlist_hash_debug = 0; 596 #define MLIST_HASH(pp) (mlist_hash_debug ? &mml_table[0] : \ 597 &mml_table[((uintptr_t)(pp) + \ 598 ((uintptr_t)(pp) >> mml_shift)) & (mml_table_sz - 1)]) 599 #else /* !DEBUG */ 600 #define MLIST_HASH(pp) &mml_table[ \ 601 ((uintptr_t)(pp) + ((uintptr_t)(pp) >> mml_shift)) & (mml_table_sz - 1)] 602 #endif /* !DEBUG */ 603 604 kmutex_t *mml_table; 605 uint_t mml_table_sz; /* must be a power of 2 */ 606 uint_t mml_shift; /* log2(mml_table_sz) + 3 for align */ 607 608 kpm_hlk_t *kpmp_table; 609 uint_t kpmp_table_sz; /* must be a power of 2 */ 610 uchar_t kpmp_shift; 611 612 kpm_shlk_t *kpmp_stable; 613 uint_t kpmp_stable_sz; /* must be a power of 2 */ 614 615 /* 616 * SPL_HASH was improved to avoid false cache line sharing 617 */ 618 #define SPL_TABLE_SIZE 128 619 #define SPL_MASK (SPL_TABLE_SIZE - 1) 620 #define SPL_SHIFT 7 /* log2(SPL_TABLE_SIZE) */ 621 622 #define SPL_INDEX(pp) \ 623 ((((uintptr_t)(pp) >> SPL_SHIFT) ^ \ 624 ((uintptr_t)(pp) >> (SPL_SHIFT << 1))) & \ 625 (SPL_TABLE_SIZE - 1)) 626 627 #define SPL_HASH(pp) \ 628 (&sfmmu_page_lock[SPL_INDEX(pp) & SPL_MASK].pad_mutex) 629 630 static pad_mutex_t sfmmu_page_lock[SPL_TABLE_SIZE]; 631 632 633 /* 634 * hat_unload_callback() will group together callbacks in order 635 * to avoid xt_sync() calls. This is the maximum size of the group. 636 */ 637 #define MAX_CB_ADDR 32 638 639 tte_t hw_tte; 640 static ulong_t sfmmu_dmr_maxbit = DMR_MAXBIT; 641 642 static char *mmu_ctx_kstat_names[] = { 643 "mmu_ctx_tsb_exceptions", 644 "mmu_ctx_tsb_raise_exception", 645 "mmu_ctx_wrap_around", 646 }; 647 648 /* 649 * Wrapper for vmem_xalloc since vmem_create only allows limited 650 * parameters for vm_source_alloc functions. This function allows us 651 * to specify alignment consistent with the size of the object being 652 * allocated. 653 */ 654 static void * 655 sfmmu_vmem_xalloc_aligned_wrapper(vmem_t *vmp, size_t size, int vmflag) 656 { 657 return (vmem_xalloc(vmp, size, size, 0, 0, NULL, NULL, vmflag)); 658 } 659 660 /* Common code for setting tsb_alloc_hiwater. */ 661 #define SFMMU_SET_TSB_ALLOC_HIWATER(pages) tsb_alloc_hiwater = \ 662 ptob(pages) / tsb_alloc_hiwater_factor 663 664 /* 665 * Set tsb_max_growsize to allow at most all of physical memory to be mapped by 666 * a single TSB. physmem is the number of physical pages so we need physmem 8K 667 * TTEs to represent all those physical pages. We round this up by using 668 * 1<<highbit(). To figure out which size code to use, remember that the size 669 * code is just an amount to shift the smallest TSB size to get the size of 670 * this TSB. So we subtract that size, TSB_START_SIZE, from highbit() (or 671 * highbit() - 1) to get the size code for the smallest TSB that can represent 672 * all of physical memory, while erring on the side of too much. 673 * 674 * If the computed size code is less than the current tsb_max_growsize, we set 675 * tsb_max_growsize to the computed size code. In the case where the computed 676 * size code is greater than tsb_max_growsize, we have these restrictions that 677 * apply to increasing tsb_max_growsize: 678 * 1) TSBs can't grow larger than the TSB slab size 679 * 2) TSBs can't grow larger than UTSB_MAX_SZCODE. 680 */ 681 #define SFMMU_SET_TSB_MAX_GROWSIZE(pages) { \ 682 int i, szc; \ 683 \ 684 i = highbit(pages); \ 685 if ((1 << (i - 1)) == (pages)) \ 686 i--; /* 2^n case, round down */ \ 687 szc = i - TSB_START_SIZE; \ 688 if (szc < tsb_max_growsize) \ 689 tsb_max_growsize = szc; \ 690 else if ((szc > tsb_max_growsize) && \ 691 (szc <= tsb_slab_shift - (TSB_START_SIZE + TSB_ENTRY_SHIFT))) \ 692 tsb_max_growsize = MIN(szc, UTSB_MAX_SZCODE); \ 693 } 694 695 /* 696 * Given a pointer to an sfmmu and a TTE size code, return a pointer to the 697 * tsb_info which handles that TTE size. 698 */ 699 #define SFMMU_GET_TSBINFO(tsbinfop, sfmmup, tte_szc) \ 700 (tsbinfop) = (sfmmup)->sfmmu_tsb; \ 701 ASSERT(sfmmu_hat_lock_held(sfmmup)); \ 702 if ((tte_szc) >= TTE4M) \ 703 (tsbinfop) = (tsbinfop)->tsb_next; 704 705 /* 706 * Return the number of mappings present in the HAT 707 * for a particular process and page size. 708 */ 709 #define SFMMU_TTE_CNT(sfmmup, szc) \ 710 (sfmmup)->sfmmu_iblk? \ 711 (sfmmup)->sfmmu_ismttecnt[(szc)] + \ 712 (sfmmup)->sfmmu_ttecnt[(szc)] : \ 713 (sfmmup)->sfmmu_ttecnt[(szc)]; 714 715 /* 716 * Macro to use to unload entries from the TSB. 717 * It has knowledge of which page sizes get replicated in the TSB 718 * and will call the appropriate unload routine for the appropriate size. 719 */ 720 #define SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp) \ 721 { \ 722 int ttesz = get_hblk_ttesz(hmeblkp); \ 723 if (ttesz == TTE8K || ttesz == TTE4M) { \ 724 sfmmu_unload_tsb(sfmmup, addr, ttesz); \ 725 } else { \ 726 caddr_t sva = (caddr_t)get_hblk_base(hmeblkp); \ 727 caddr_t eva = sva + get_hblk_span(hmeblkp); \ 728 ASSERT(addr >= sva && addr < eva); \ 729 sfmmu_unload_tsb_range(sfmmup, sva, eva, ttesz); \ 730 } \ 731 } 732 733 734 /* Update tsb_alloc_hiwater after memory is configured. */ 735 /*ARGSUSED*/ 736 static void 737 sfmmu_update_tsb_post_add(void *arg, pgcnt_t delta_pages) 738 { 739 /* Assumes physmem has already been updated. */ 740 SFMMU_SET_TSB_ALLOC_HIWATER(physmem); 741 SFMMU_SET_TSB_MAX_GROWSIZE(physmem); 742 } 743 744 /* 745 * Update tsb_alloc_hiwater before memory is deleted. We'll do nothing here 746 * and update tsb_alloc_hiwater and tsb_max_growsize after the memory is 747 * deleted. 748 */ 749 /*ARGSUSED*/ 750 static int 751 sfmmu_update_tsb_pre_del(void *arg, pgcnt_t delta_pages) 752 { 753 return (0); 754 } 755 756 /* Update tsb_alloc_hiwater after memory fails to be unconfigured. */ 757 /*ARGSUSED*/ 758 static void 759 sfmmu_update_tsb_post_del(void *arg, pgcnt_t delta_pages, int cancelled) 760 { 761 /* 762 * Whether the delete was cancelled or not, just go ahead and update 763 * tsb_alloc_hiwater and tsb_max_growsize. 764 */ 765 SFMMU_SET_TSB_ALLOC_HIWATER(physmem); 766 SFMMU_SET_TSB_MAX_GROWSIZE(physmem); 767 } 768 769 static kphysm_setup_vector_t sfmmu_update_tsb_vec = { 770 KPHYSM_SETUP_VECTOR_VERSION, /* version */ 771 sfmmu_update_tsb_post_add, /* post_add */ 772 sfmmu_update_tsb_pre_del, /* pre_del */ 773 sfmmu_update_tsb_post_del /* post_del */ 774 }; 775 776 777 /* 778 * HME_BLK HASH PRIMITIVES 779 */ 780 781 /* 782 * Enter a hme on the mapping list for page pp. 783 * When large pages are more prevalent in the system we might want to 784 * keep the mapping list in ascending order by the hment size. For now, 785 * small pages are more frequent, so don't slow it down. 786 */ 787 #define HME_ADD(hme, pp) \ 788 { \ 789 ASSERT(sfmmu_mlist_held(pp)); \ 790 \ 791 hme->hme_prev = NULL; \ 792 hme->hme_next = pp->p_mapping; \ 793 hme->hme_page = pp; \ 794 if (pp->p_mapping) { \ 795 ((struct sf_hment *)(pp->p_mapping))->hme_prev = hme;\ 796 ASSERT(pp->p_share > 0); \ 797 } else { \ 798 /* EMPTY */ \ 799 ASSERT(pp->p_share == 0); \ 800 } \ 801 pp->p_mapping = hme; \ 802 pp->p_share++; \ 803 } 804 805 /* 806 * Enter a hme on the mapping list for page pp. 807 * If we are unmapping a large translation, we need to make sure that the 808 * change is reflect in the corresponding bit of the p_index field. 809 */ 810 #define HME_SUB(hme, pp) \ 811 { \ 812 ASSERT(sfmmu_mlist_held(pp)); \ 813 ASSERT(hme->hme_page == pp || IS_PAHME(hme)); \ 814 \ 815 if (pp->p_mapping == NULL) { \ 816 panic("hme_remove - no mappings"); \ 817 } \ 818 \ 819 membar_stst(); /* ensure previous stores finish */ \ 820 \ 821 ASSERT(pp->p_share > 0); \ 822 pp->p_share--; \ 823 \ 824 if (hme->hme_prev) { \ 825 ASSERT(pp->p_mapping != hme); \ 826 ASSERT(hme->hme_prev->hme_page == pp || \ 827 IS_PAHME(hme->hme_prev)); \ 828 hme->hme_prev->hme_next = hme->hme_next; \ 829 } else { \ 830 ASSERT(pp->p_mapping == hme); \ 831 pp->p_mapping = hme->hme_next; \ 832 ASSERT((pp->p_mapping == NULL) ? \ 833 (pp->p_share == 0) : 1); \ 834 } \ 835 \ 836 if (hme->hme_next) { \ 837 ASSERT(hme->hme_next->hme_page == pp || \ 838 IS_PAHME(hme->hme_next)); \ 839 hme->hme_next->hme_prev = hme->hme_prev; \ 840 } \ 841 \ 842 /* zero out the entry */ \ 843 hme->hme_next = NULL; \ 844 hme->hme_prev = NULL; \ 845 hme->hme_page = NULL; \ 846 \ 847 if (hme_size(hme) > TTE8K) { \ 848 /* remove mappings for remainder of large pg */ \ 849 sfmmu_rm_large_mappings(pp, hme_size(hme)); \ 850 } \ 851 } 852 853 /* 854 * This function returns the hment given the hme_blk and a vaddr. 855 * It assumes addr has already been checked to belong to hme_blk's 856 * range. 857 */ 858 #define HBLKTOHME(hment, hmeblkp, addr) \ 859 { \ 860 int index; \ 861 HBLKTOHME_IDX(hment, hmeblkp, addr, index) \ 862 } 863 864 /* 865 * Version of HBLKTOHME that also returns the index in hmeblkp 866 * of the hment. 867 */ 868 #define HBLKTOHME_IDX(hment, hmeblkp, addr, idx) \ 869 { \ 870 ASSERT(in_hblk_range((hmeblkp), (addr))); \ 871 \ 872 if (get_hblk_ttesz(hmeblkp) == TTE8K) { \ 873 idx = (((uintptr_t)(addr) >> MMU_PAGESHIFT) & (NHMENTS-1)); \ 874 } else \ 875 idx = 0; \ 876 \ 877 (hment) = &(hmeblkp)->hblk_hme[idx]; \ 878 } 879 880 /* 881 * Disable any page sizes not supported by the CPU 882 */ 883 void 884 hat_init_pagesizes() 885 { 886 int i; 887 888 mmu_exported_page_sizes = 0; 889 for (i = TTE8K; i < max_mmu_page_sizes; i++) { 890 extern int disable_text_largepages; 891 extern int disable_initdata_largepages; 892 893 szc_2_userszc[i] = (uint_t)-1; 894 userszc_2_szc[i] = (uint_t)-1; 895 896 if ((mmu_exported_pagesize_mask & (1 << i)) == 0) { 897 disable_large_pages |= (1 << i); 898 disable_ism_large_pages |= (1 << i); 899 disable_text_largepages |= (1 << i); 900 disable_initdata_largepages |= (1 << i); 901 } else { 902 szc_2_userszc[i] = mmu_exported_page_sizes; 903 userszc_2_szc[mmu_exported_page_sizes] = i; 904 mmu_exported_page_sizes++; 905 } 906 } 907 908 disable_auto_large_pages = disable_large_pages; 909 910 /* 911 * Initialize mmu-specific large page sizes. 912 */ 913 if ((mmu_page_sizes == max_mmu_page_sizes) && 914 (&mmu_large_pages_disabled)) { 915 disable_large_pages |= mmu_large_pages_disabled(HAT_LOAD); 916 disable_ism_large_pages |= 917 mmu_large_pages_disabled(HAT_LOAD_SHARE); 918 disable_auto_large_pages |= 919 mmu_large_pages_disabled(HAT_LOAD_AUTOLPG); 920 } 921 922 } 923 924 /* 925 * Initialize the hardware address translation structures. 926 */ 927 void 928 hat_init(void) 929 { 930 int i; 931 uint_t sz; 932 uint_t maxtsb; 933 size_t size; 934 935 hat_lock_init(); 936 hat_kstat_init(); 937 938 /* 939 * Hardware-only bits in a TTE 940 */ 941 MAKE_TTE_MASK(&hw_tte); 942 943 hat_init_pagesizes(); 944 945 /* Initialize the hash locks */ 946 for (i = 0; i < khmehash_num; i++) { 947 mutex_init(&khme_hash[i].hmehash_mutex, NULL, 948 MUTEX_DEFAULT, NULL); 949 } 950 for (i = 0; i < uhmehash_num; i++) { 951 mutex_init(&uhme_hash[i].hmehash_mutex, NULL, 952 MUTEX_DEFAULT, NULL); 953 } 954 khmehash_num--; /* make sure counter starts from 0 */ 955 uhmehash_num--; /* make sure counter starts from 0 */ 956 957 /* 958 * Allocate context domain structures. 959 * 960 * A platform may choose to modify max_mmu_ctxdoms in 961 * set_platform_defaults(). If a platform does not define 962 * a set_platform_defaults() or does not choose to modify 963 * max_mmu_ctxdoms, it gets one MMU context domain for every CPU. 964 * 965 * For sun4v, there will be one global context domain, this is to 966 * avoid the ldom cpu substitution problem. 967 * 968 * For all platforms that have CPUs sharing MMUs, this 969 * value must be defined. 970 */ 971 if (max_mmu_ctxdoms == 0) { 972 #ifndef sun4v 973 max_mmu_ctxdoms = max_ncpus; 974 #else /* sun4v */ 975 max_mmu_ctxdoms = 1; 976 #endif /* sun4v */ 977 } 978 979 size = max_mmu_ctxdoms * sizeof (mmu_ctx_t *); 980 mmu_ctxs_tbl = kmem_zalloc(size, KM_SLEEP); 981 982 /* mmu_ctx_t is 64 bytes aligned */ 983 mmuctxdom_cache = kmem_cache_create("mmuctxdom_cache", 984 sizeof (mmu_ctx_t), 64, NULL, NULL, NULL, NULL, NULL, 0); 985 /* 986 * MMU context domain initialization for the Boot CPU. 987 * This needs the context domains array allocated above. 988 */ 989 mutex_enter(&cpu_lock); 990 sfmmu_cpu_init(CPU); 991 mutex_exit(&cpu_lock); 992 993 /* 994 * Intialize ism mapping list lock. 995 */ 996 997 mutex_init(&ism_mlist_lock, NULL, MUTEX_DEFAULT, NULL); 998 999 /* 1000 * Each sfmmu structure carries an array of MMU context info 1001 * structures, one per context domain. The size of this array depends 1002 * on the maximum number of context domains. So, the size of the 1003 * sfmmu structure varies per platform. 1004 * 1005 * sfmmu is allocated from static arena, because trap 1006 * handler at TL > 0 is not allowed to touch kernel relocatable 1007 * memory. sfmmu's alignment is changed to 64 bytes from 1008 * default 8 bytes, as the lower 6 bits will be used to pass 1009 * pgcnt to vtag_flush_pgcnt_tl1. 1010 */ 1011 size = sizeof (sfmmu_t) + sizeof (sfmmu_ctx_t) * (max_mmu_ctxdoms - 1); 1012 1013 sfmmuid_cache = kmem_cache_create("sfmmuid_cache", size, 1014 64, sfmmu_idcache_constructor, sfmmu_idcache_destructor, 1015 NULL, NULL, static_arena, 0); 1016 1017 sfmmu_tsbinfo_cache = kmem_cache_create("sfmmu_tsbinfo_cache", 1018 sizeof (struct tsb_info), 0, NULL, NULL, NULL, NULL, NULL, 0); 1019 1020 /* 1021 * Since we only use the tsb8k cache to "borrow" pages for TSBs 1022 * from the heap when low on memory or when TSB_FORCEALLOC is 1023 * specified, don't use magazines to cache them--we want to return 1024 * them to the system as quickly as possible. 1025 */ 1026 sfmmu_tsb8k_cache = kmem_cache_create("sfmmu_tsb8k_cache", 1027 MMU_PAGESIZE, MMU_PAGESIZE, NULL, NULL, NULL, NULL, 1028 static_arena, KMC_NOMAGAZINE); 1029 1030 /* 1031 * Set tsb_alloc_hiwater to 1/tsb_alloc_hiwater_factor of physical 1032 * memory, which corresponds to the old static reserve for TSBs. 1033 * tsb_alloc_hiwater_factor defaults to 32. This caps the amount of 1034 * memory we'll allocate for TSB slabs; beyond this point TSB 1035 * allocations will be taken from the kernel heap (via 1036 * sfmmu_tsb8k_cache) and will be throttled as would any other kmem 1037 * consumer. 1038 */ 1039 if (tsb_alloc_hiwater_factor == 0) { 1040 tsb_alloc_hiwater_factor = TSB_ALLOC_HIWATER_FACTOR_DEFAULT; 1041 } 1042 SFMMU_SET_TSB_ALLOC_HIWATER(physmem); 1043 1044 /* Set tsb_max_growsize. */ 1045 SFMMU_SET_TSB_MAX_GROWSIZE(physmem); 1046 1047 /* 1048 * On smaller memory systems, allocate TSB memory in smaller chunks 1049 * than the default 4M slab size. We also honor disable_large_pages 1050 * here. 1051 * 1052 * The trap handlers need to be patched with the final slab shift, 1053 * since they need to be able to construct the TSB pointer at runtime. 1054 */ 1055 if (tsb_max_growsize <= TSB_512K_SZCODE) 1056 tsb_slab_ttesz = TTE512K; 1057 1058 for (sz = tsb_slab_ttesz; sz > 0; sz--) { 1059 if (!(disable_large_pages & (1 << sz))) 1060 break; 1061 } 1062 1063 tsb_slab_ttesz = sz; 1064 tsb_slab_shift = MMU_PAGESHIFT + (sz << 1) + sz; 1065 tsb_slab_size = 1 << tsb_slab_shift; 1066 tsb_slab_mask = (1 << (tsb_slab_shift - MMU_PAGESHIFT)) - 1; 1067 1068 maxtsb = tsb_slab_shift - (TSB_START_SIZE + TSB_ENTRY_SHIFT); 1069 if (tsb_max_growsize > maxtsb) 1070 tsb_max_growsize = maxtsb; 1071 1072 /* 1073 * Set up memory callback to update tsb_alloc_hiwater and 1074 * tsb_max_growsize. 1075 */ 1076 i = kphysm_setup_func_register(&sfmmu_update_tsb_vec, (void *) 0); 1077 ASSERT(i == 0); 1078 1079 /* 1080 * kmem_tsb_arena is the source from which large TSB slabs are 1081 * drawn. The quantum of this arena corresponds to the largest 1082 * TSB size we can dynamically allocate for user processes. 1083 * Currently it must also be a supported page size since we 1084 * use exactly one translation entry to map each slab page. 1085 * 1086 * The per-lgroup kmem_tsb_default_arena arenas are the arenas from 1087 * which most TSBs are allocated. Since most TSB allocations are 1088 * typically 8K we have a kmem cache we stack on top of each 1089 * kmem_tsb_default_arena to speed up those allocations. 1090 * 1091 * Note the two-level scheme of arenas is required only 1092 * because vmem_create doesn't allow us to specify alignment 1093 * requirements. If this ever changes the code could be 1094 * simplified to use only one level of arenas. 1095 */ 1096 kmem_tsb_arena = vmem_create("kmem_tsb", NULL, 0, tsb_slab_size, 1097 sfmmu_vmem_xalloc_aligned_wrapper, vmem_xfree, heap_arena, 1098 0, VM_SLEEP); 1099 1100 if (tsb_lgrp_affinity) { 1101 char s[50]; 1102 for (i = 0; i < NLGRPS_MAX; i++) { 1103 (void) sprintf(s, "kmem_tsb_lgrp%d", i); 1104 kmem_tsb_default_arena[i] = 1105 vmem_create(s, NULL, 0, PAGESIZE, 1106 sfmmu_tsb_segkmem_alloc, sfmmu_tsb_segkmem_free, 1107 kmem_tsb_arena, 0, VM_SLEEP | VM_BESTFIT); 1108 (void) sprintf(s, "sfmmu_tsb_lgrp%d_cache", i); 1109 sfmmu_tsb_cache[i] = kmem_cache_create(s, PAGESIZE, 1110 PAGESIZE, NULL, NULL, NULL, NULL, 1111 kmem_tsb_default_arena[i], 0); 1112 } 1113 } else { 1114 kmem_tsb_default_arena[0] = vmem_create("kmem_tsb_default", 1115 NULL, 0, PAGESIZE, sfmmu_tsb_segkmem_alloc, 1116 sfmmu_tsb_segkmem_free, kmem_tsb_arena, 0, 1117 VM_SLEEP | VM_BESTFIT); 1118 1119 sfmmu_tsb_cache[0] = kmem_cache_create("sfmmu_tsb_cache", 1120 PAGESIZE, PAGESIZE, NULL, NULL, NULL, NULL, 1121 kmem_tsb_default_arena[0], 0); 1122 } 1123 1124 sfmmu8_cache = kmem_cache_create("sfmmu8_cache", HME8BLK_SZ, 1125 HMEBLK_ALIGN, sfmmu_hblkcache_constructor, 1126 sfmmu_hblkcache_destructor, 1127 sfmmu_hblkcache_reclaim, (void *)HME8BLK_SZ, 1128 hat_memload_arena, KMC_NOHASH); 1129 1130 hat_memload1_arena = vmem_create("hat_memload1", NULL, 0, PAGESIZE, 1131 segkmem_alloc_permanent, segkmem_free, heap_arena, 0, VM_SLEEP); 1132 1133 sfmmu1_cache = kmem_cache_create("sfmmu1_cache", HME1BLK_SZ, 1134 HMEBLK_ALIGN, sfmmu_hblkcache_constructor, 1135 sfmmu_hblkcache_destructor, 1136 NULL, (void *)HME1BLK_SZ, 1137 hat_memload1_arena, KMC_NOHASH); 1138 1139 pa_hment_cache = kmem_cache_create("pa_hment_cache", PAHME_SZ, 1140 0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH); 1141 1142 ism_blk_cache = kmem_cache_create("ism_blk_cache", 1143 sizeof (ism_blk_t), ecache_alignsize, NULL, NULL, 1144 NULL, NULL, static_arena, KMC_NOHASH); 1145 1146 ism_ment_cache = kmem_cache_create("ism_ment_cache", 1147 sizeof (ism_ment_t), 0, NULL, NULL, 1148 NULL, NULL, NULL, 0); 1149 1150 /* 1151 * We grab the first hat for the kernel, 1152 */ 1153 AS_LOCK_ENTER(&kas, &kas.a_lock, RW_WRITER); 1154 kas.a_hat = hat_alloc(&kas); 1155 AS_LOCK_EXIT(&kas, &kas.a_lock); 1156 1157 /* 1158 * Initialize hblk_reserve. 1159 */ 1160 ((struct hme_blk *)hblk_reserve)->hblk_nextpa = 1161 va_to_pa((caddr_t)hblk_reserve); 1162 1163 #ifndef UTSB_PHYS 1164 /* 1165 * Reserve some kernel virtual address space for the locked TTEs 1166 * that allow us to probe the TSB from TL>0. 1167 */ 1168 utsb_vabase = vmem_xalloc(heap_arena, tsb_slab_size, tsb_slab_size, 1169 0, 0, NULL, NULL, VM_SLEEP); 1170 utsb4m_vabase = vmem_xalloc(heap_arena, tsb_slab_size, tsb_slab_size, 1171 0, 0, NULL, NULL, VM_SLEEP); 1172 #endif 1173 1174 #ifdef VAC 1175 /* 1176 * The big page VAC handling code assumes VAC 1177 * will not be bigger than the smallest big 1178 * page- which is 64K. 1179 */ 1180 if (TTEPAGES(TTE64K) < CACHE_NUM_COLOR) { 1181 cmn_err(CE_PANIC, "VAC too big!"); 1182 } 1183 #endif 1184 1185 (void) xhat_init(); 1186 1187 uhme_hash_pa = va_to_pa(uhme_hash); 1188 khme_hash_pa = va_to_pa(khme_hash); 1189 1190 /* 1191 * Initialize relocation locks. kpr_suspendlock is held 1192 * at PIL_MAX to prevent interrupts from pinning the holder 1193 * of a suspended TTE which may access it leading to a 1194 * deadlock condition. 1195 */ 1196 mutex_init(&kpr_mutex, NULL, MUTEX_DEFAULT, NULL); 1197 mutex_init(&kpr_suspendlock, NULL, MUTEX_SPIN, (void *)PIL_MAX); 1198 } 1199 1200 /* 1201 * Initialize locking for the hat layer, called early during boot. 1202 */ 1203 static void 1204 hat_lock_init() 1205 { 1206 int i; 1207 1208 /* 1209 * initialize the array of mutexes protecting a page's mapping 1210 * list and p_nrm field. 1211 */ 1212 for (i = 0; i < mml_table_sz; i++) 1213 mutex_init(&mml_table[i], NULL, MUTEX_DEFAULT, NULL); 1214 1215 if (kpm_enable) { 1216 for (i = 0; i < kpmp_table_sz; i++) { 1217 mutex_init(&kpmp_table[i].khl_mutex, NULL, 1218 MUTEX_DEFAULT, NULL); 1219 } 1220 } 1221 1222 /* 1223 * Initialize array of mutex locks that protects sfmmu fields and 1224 * TSB lists. 1225 */ 1226 for (i = 0; i < SFMMU_NUM_LOCK; i++) 1227 mutex_init(HATLOCK_MUTEXP(&hat_lock[i]), NULL, MUTEX_DEFAULT, 1228 NULL); 1229 } 1230 1231 extern caddr_t kmem64_base, kmem64_end; 1232 1233 #define SFMMU_KERNEL_MAXVA \ 1234 (kmem64_base ? (uintptr_t)kmem64_end : (SYSLIMIT)) 1235 1236 /* 1237 * Allocate a hat structure. 1238 * Called when an address space first uses a hat. 1239 */ 1240 struct hat * 1241 hat_alloc(struct as *as) 1242 { 1243 sfmmu_t *sfmmup; 1244 int i; 1245 uint64_t cnum; 1246 extern uint_t get_color_start(struct as *); 1247 1248 ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 1249 sfmmup = kmem_cache_alloc(sfmmuid_cache, KM_SLEEP); 1250 sfmmup->sfmmu_as = as; 1251 sfmmup->sfmmu_flags = 0; 1252 LOCK_INIT_CLEAR(&sfmmup->sfmmu_ctx_lock); 1253 1254 if (as == &kas) { 1255 ksfmmup = sfmmup; 1256 sfmmup->sfmmu_cext = 0; 1257 cnum = KCONTEXT; 1258 1259 sfmmup->sfmmu_clrstart = 0; 1260 sfmmup->sfmmu_tsb = NULL; 1261 /* 1262 * hat_kern_setup() will call sfmmu_init_ktsbinfo() 1263 * to setup tsb_info for ksfmmup. 1264 */ 1265 } else { 1266 1267 /* 1268 * Just set to invalid ctx. When it faults, it will 1269 * get a valid ctx. This would avoid the situation 1270 * where we get a ctx, but it gets stolen and then 1271 * we fault when we try to run and so have to get 1272 * another ctx. 1273 */ 1274 sfmmup->sfmmu_cext = 0; 1275 cnum = INVALID_CONTEXT; 1276 1277 /* initialize original physical page coloring bin */ 1278 sfmmup->sfmmu_clrstart = get_color_start(as); 1279 #ifdef DEBUG 1280 if (tsb_random_size) { 1281 uint32_t randval = (uint32_t)gettick() >> 4; 1282 int size = randval % (tsb_max_growsize + 1); 1283 1284 /* chose a random tsb size for stress testing */ 1285 (void) sfmmu_tsbinfo_alloc(&sfmmup->sfmmu_tsb, size, 1286 TSB8K|TSB64K|TSB512K, 0, sfmmup); 1287 } else 1288 #endif /* DEBUG */ 1289 (void) sfmmu_tsbinfo_alloc(&sfmmup->sfmmu_tsb, 1290 default_tsb_size, 1291 TSB8K|TSB64K|TSB512K, 0, sfmmup); 1292 sfmmup->sfmmu_flags = HAT_SWAPPED; 1293 ASSERT(sfmmup->sfmmu_tsb != NULL); 1294 } 1295 1296 ASSERT(max_mmu_ctxdoms > 0); 1297 for (i = 0; i < max_mmu_ctxdoms; i++) { 1298 sfmmup->sfmmu_ctxs[i].cnum = cnum; 1299 sfmmup->sfmmu_ctxs[i].gnum = 0; 1300 } 1301 1302 sfmmu_setup_tsbinfo(sfmmup); 1303 for (i = 0; i < max_mmu_page_sizes; i++) { 1304 sfmmup->sfmmu_ttecnt[i] = 0; 1305 sfmmup->sfmmu_ismttecnt[i] = 0; 1306 sfmmup->sfmmu_pgsz[i] = TTE8K; 1307 } 1308 1309 sfmmup->sfmmu_iblk = NULL; 1310 sfmmup->sfmmu_ismhat = 0; 1311 sfmmup->sfmmu_ismblkpa = (uint64_t)-1; 1312 if (sfmmup == ksfmmup) { 1313 CPUSET_ALL(sfmmup->sfmmu_cpusran); 1314 } else { 1315 CPUSET_ZERO(sfmmup->sfmmu_cpusran); 1316 } 1317 sfmmup->sfmmu_free = 0; 1318 sfmmup->sfmmu_rmstat = 0; 1319 sfmmup->sfmmu_clrbin = sfmmup->sfmmu_clrstart; 1320 sfmmup->sfmmu_xhat_provider = NULL; 1321 cv_init(&sfmmup->sfmmu_tsb_cv, NULL, CV_DEFAULT, NULL); 1322 return (sfmmup); 1323 } 1324 1325 /* 1326 * Create per-MMU context domain kstats for a given MMU ctx. 1327 */ 1328 static void 1329 sfmmu_mmu_kstat_create(mmu_ctx_t *mmu_ctxp) 1330 { 1331 mmu_ctx_stat_t stat; 1332 kstat_t *mmu_kstat; 1333 1334 ASSERT(MUTEX_HELD(&cpu_lock)); 1335 ASSERT(mmu_ctxp->mmu_kstat == NULL); 1336 1337 mmu_kstat = kstat_create("unix", mmu_ctxp->mmu_idx, "mmu_ctx", 1338 "hat", KSTAT_TYPE_NAMED, MMU_CTX_NUM_STATS, KSTAT_FLAG_VIRTUAL); 1339 1340 if (mmu_kstat == NULL) { 1341 cmn_err(CE_WARN, "kstat_create for MMU %d failed", 1342 mmu_ctxp->mmu_idx); 1343 } else { 1344 mmu_kstat->ks_data = mmu_ctxp->mmu_kstat_data; 1345 for (stat = 0; stat < MMU_CTX_NUM_STATS; stat++) 1346 kstat_named_init(&mmu_ctxp->mmu_kstat_data[stat], 1347 mmu_ctx_kstat_names[stat], KSTAT_DATA_INT64); 1348 mmu_ctxp->mmu_kstat = mmu_kstat; 1349 kstat_install(mmu_kstat); 1350 } 1351 } 1352 1353 /* 1354 * plat_cpuid_to_mmu_ctx_info() is a platform interface that returns MMU 1355 * context domain information for a given CPU. If a platform does not 1356 * specify that interface, then the function below is used instead to return 1357 * default information. The defaults are as follows: 1358 * 1359 * - For sun4u systems there's one MMU context domain per CPU. 1360 * This default is used by all sun4u systems except OPL. OPL systems 1361 * provide platform specific interface to map CPU ids to MMU ids 1362 * because on OPL more than 1 CPU shares a single MMU. 1363 * Note that on sun4v, there is one global context domain for 1364 * the entire system. This is to avoid running into potential problem 1365 * with ldom physical cpu substitution feature. 1366 * - The number of MMU context IDs supported on any CPU in the 1367 * system is 8K. 1368 */ 1369 /*ARGSUSED*/ 1370 static void 1371 sfmmu_cpuid_to_mmu_ctx_info(processorid_t cpuid, mmu_ctx_info_t *infop) 1372 { 1373 infop->mmu_nctxs = nctxs; 1374 #ifndef sun4v 1375 infop->mmu_idx = cpu[cpuid]->cpu_seqid; 1376 #else /* sun4v */ 1377 infop->mmu_idx = 0; 1378 #endif /* sun4v */ 1379 } 1380 1381 /* 1382 * Called during CPU initialization to set the MMU context-related information 1383 * for a CPU. 1384 * 1385 * cpu_lock serializes accesses to mmu_ctxs and mmu_saved_gnum. 1386 */ 1387 void 1388 sfmmu_cpu_init(cpu_t *cp) 1389 { 1390 mmu_ctx_info_t info; 1391 mmu_ctx_t *mmu_ctxp; 1392 1393 ASSERT(MUTEX_HELD(&cpu_lock)); 1394 1395 if (&plat_cpuid_to_mmu_ctx_info == NULL) 1396 sfmmu_cpuid_to_mmu_ctx_info(cp->cpu_id, &info); 1397 else 1398 plat_cpuid_to_mmu_ctx_info(cp->cpu_id, &info); 1399 1400 ASSERT(info.mmu_idx < max_mmu_ctxdoms); 1401 1402 if ((mmu_ctxp = mmu_ctxs_tbl[info.mmu_idx]) == NULL) { 1403 /* Each mmu_ctx is cacheline aligned. */ 1404 mmu_ctxp = kmem_cache_alloc(mmuctxdom_cache, KM_SLEEP); 1405 bzero(mmu_ctxp, sizeof (mmu_ctx_t)); 1406 1407 mutex_init(&mmu_ctxp->mmu_lock, NULL, MUTEX_SPIN, 1408 (void *)ipltospl(DISP_LEVEL)); 1409 mmu_ctxp->mmu_idx = info.mmu_idx; 1410 mmu_ctxp->mmu_nctxs = info.mmu_nctxs; 1411 /* 1412 * Globally for lifetime of a system, 1413 * gnum must always increase. 1414 * mmu_saved_gnum is protected by the cpu_lock. 1415 */ 1416 mmu_ctxp->mmu_gnum = mmu_saved_gnum + 1; 1417 mmu_ctxp->mmu_cnum = NUM_LOCKED_CTXS; 1418 1419 sfmmu_mmu_kstat_create(mmu_ctxp); 1420 1421 mmu_ctxs_tbl[info.mmu_idx] = mmu_ctxp; 1422 } else { 1423 ASSERT(mmu_ctxp->mmu_idx == info.mmu_idx); 1424 } 1425 1426 /* 1427 * The mmu_lock is acquired here to prevent races with 1428 * the wrap-around code. 1429 */ 1430 mutex_enter(&mmu_ctxp->mmu_lock); 1431 1432 1433 mmu_ctxp->mmu_ncpus++; 1434 CPUSET_ADD(mmu_ctxp->mmu_cpuset, cp->cpu_id); 1435 CPU_MMU_IDX(cp) = info.mmu_idx; 1436 CPU_MMU_CTXP(cp) = mmu_ctxp; 1437 1438 mutex_exit(&mmu_ctxp->mmu_lock); 1439 } 1440 1441 /* 1442 * Called to perform MMU context-related cleanup for a CPU. 1443 */ 1444 void 1445 sfmmu_cpu_cleanup(cpu_t *cp) 1446 { 1447 mmu_ctx_t *mmu_ctxp; 1448 1449 ASSERT(MUTEX_HELD(&cpu_lock)); 1450 1451 mmu_ctxp = CPU_MMU_CTXP(cp); 1452 ASSERT(mmu_ctxp != NULL); 1453 1454 /* 1455 * The mmu_lock is acquired here to prevent races with 1456 * the wrap-around code. 1457 */ 1458 mutex_enter(&mmu_ctxp->mmu_lock); 1459 1460 CPU_MMU_CTXP(cp) = NULL; 1461 1462 CPUSET_DEL(mmu_ctxp->mmu_cpuset, cp->cpu_id); 1463 if (--mmu_ctxp->mmu_ncpus == 0) { 1464 mmu_ctxs_tbl[mmu_ctxp->mmu_idx] = NULL; 1465 mutex_exit(&mmu_ctxp->mmu_lock); 1466 mutex_destroy(&mmu_ctxp->mmu_lock); 1467 1468 if (mmu_ctxp->mmu_kstat) 1469 kstat_delete(mmu_ctxp->mmu_kstat); 1470 1471 /* mmu_saved_gnum is protected by the cpu_lock. */ 1472 if (mmu_saved_gnum < mmu_ctxp->mmu_gnum) 1473 mmu_saved_gnum = mmu_ctxp->mmu_gnum; 1474 1475 kmem_cache_free(mmuctxdom_cache, mmu_ctxp); 1476 1477 return; 1478 } 1479 1480 mutex_exit(&mmu_ctxp->mmu_lock); 1481 } 1482 1483 /* 1484 * Hat_setup, makes an address space context the current active one. 1485 * In sfmmu this translates to setting the secondary context with the 1486 * corresponding context. 1487 */ 1488 void 1489 hat_setup(struct hat *sfmmup, int allocflag) 1490 { 1491 hatlock_t *hatlockp; 1492 1493 /* Init needs some special treatment. */ 1494 if (allocflag == HAT_INIT) { 1495 /* 1496 * Make sure that we have 1497 * 1. a TSB 1498 * 2. a valid ctx that doesn't get stolen after this point. 1499 */ 1500 hatlockp = sfmmu_hat_enter(sfmmup); 1501 1502 /* 1503 * Swap in the TSB. hat_init() allocates tsbinfos without 1504 * TSBs, but we need one for init, since the kernel does some 1505 * special things to set up its stack and needs the TSB to 1506 * resolve page faults. 1507 */ 1508 sfmmu_tsb_swapin(sfmmup, hatlockp); 1509 1510 sfmmu_get_ctx(sfmmup); 1511 1512 sfmmu_hat_exit(hatlockp); 1513 } else { 1514 ASSERT(allocflag == HAT_ALLOC); 1515 1516 hatlockp = sfmmu_hat_enter(sfmmup); 1517 kpreempt_disable(); 1518 1519 CPUSET_ADD(sfmmup->sfmmu_cpusran, CPU->cpu_id); 1520 1521 /* 1522 * sfmmu_setctx_sec takes <pgsz|cnum> as a parameter, 1523 * pagesize bits don't matter in this case since we are passing 1524 * INVALID_CONTEXT to it. 1525 */ 1526 sfmmu_setctx_sec(INVALID_CONTEXT); 1527 sfmmu_clear_utsbinfo(); 1528 1529 kpreempt_enable(); 1530 sfmmu_hat_exit(hatlockp); 1531 } 1532 } 1533 1534 /* 1535 * Free all the translation resources for the specified address space. 1536 * Called from as_free when an address space is being destroyed. 1537 */ 1538 void 1539 hat_free_start(struct hat *sfmmup) 1540 { 1541 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 1542 ASSERT(sfmmup != ksfmmup); 1543 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 1544 1545 sfmmup->sfmmu_free = 1; 1546 } 1547 1548 void 1549 hat_free_end(struct hat *sfmmup) 1550 { 1551 int i; 1552 1553 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 1554 if (sfmmup->sfmmu_ismhat) { 1555 for (i = 0; i < mmu_page_sizes; i++) { 1556 sfmmup->sfmmu_ttecnt[i] = 0; 1557 sfmmup->sfmmu_ismttecnt[i] = 0; 1558 } 1559 } else { 1560 /* EMPTY */ 1561 ASSERT(sfmmup->sfmmu_ttecnt[TTE8K] == 0); 1562 ASSERT(sfmmup->sfmmu_ttecnt[TTE64K] == 0); 1563 ASSERT(sfmmup->sfmmu_ttecnt[TTE512K] == 0); 1564 ASSERT(sfmmup->sfmmu_ttecnt[TTE4M] == 0); 1565 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0); 1566 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0); 1567 } 1568 1569 if (sfmmup->sfmmu_rmstat) { 1570 hat_freestat(sfmmup->sfmmu_as, NULL); 1571 } 1572 1573 while (sfmmup->sfmmu_tsb != NULL) { 1574 struct tsb_info *next = sfmmup->sfmmu_tsb->tsb_next; 1575 sfmmu_tsbinfo_free(sfmmup->sfmmu_tsb); 1576 sfmmup->sfmmu_tsb = next; 1577 } 1578 sfmmu_free_sfmmu(sfmmup); 1579 1580 kmem_cache_free(sfmmuid_cache, sfmmup); 1581 } 1582 1583 /* 1584 * Set up any translation structures, for the specified address space, 1585 * that are needed or preferred when the process is being swapped in. 1586 */ 1587 /* ARGSUSED */ 1588 void 1589 hat_swapin(struct hat *hat) 1590 { 1591 ASSERT(hat->sfmmu_xhat_provider == NULL); 1592 } 1593 1594 /* 1595 * Free all of the translation resources, for the specified address space, 1596 * that can be freed while the process is swapped out. Called from as_swapout. 1597 * Also, free up the ctx that this process was using. 1598 */ 1599 void 1600 hat_swapout(struct hat *sfmmup) 1601 { 1602 struct hmehash_bucket *hmebp; 1603 struct hme_blk *hmeblkp; 1604 struct hme_blk *pr_hblk = NULL; 1605 struct hme_blk *nx_hblk; 1606 int i; 1607 uint64_t hblkpa, prevpa, nx_pa; 1608 struct hme_blk *list = NULL; 1609 hatlock_t *hatlockp; 1610 struct tsb_info *tsbinfop; 1611 struct free_tsb { 1612 struct free_tsb *next; 1613 struct tsb_info *tsbinfop; 1614 }; /* free list of TSBs */ 1615 struct free_tsb *freelist, *last, *next; 1616 1617 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 1618 SFMMU_STAT(sf_swapout); 1619 1620 /* 1621 * There is no way to go from an as to all its translations in sfmmu. 1622 * Here is one of the times when we take the big hit and traverse 1623 * the hash looking for hme_blks to free up. Not only do we free up 1624 * this as hme_blks but all those that are free. We are obviously 1625 * swapping because we need memory so let's free up as much 1626 * as we can. 1627 * 1628 * Note that we don't flush TLB/TSB here -- it's not necessary 1629 * because: 1630 * 1) we free the ctx we're using and throw away the TSB(s); 1631 * 2) processes aren't runnable while being swapped out. 1632 */ 1633 ASSERT(sfmmup != KHATID); 1634 for (i = 0; i <= UHMEHASH_SZ; i++) { 1635 hmebp = &uhme_hash[i]; 1636 SFMMU_HASH_LOCK(hmebp); 1637 hmeblkp = hmebp->hmeblkp; 1638 hblkpa = hmebp->hmeh_nextpa; 1639 prevpa = 0; 1640 pr_hblk = NULL; 1641 while (hmeblkp) { 1642 1643 ASSERT(!hmeblkp->hblk_xhat_bit); 1644 1645 if ((hmeblkp->hblk_tag.htag_id == sfmmup) && 1646 !hmeblkp->hblk_shw_bit && !hmeblkp->hblk_lckcnt) { 1647 (void) sfmmu_hblk_unload(sfmmup, hmeblkp, 1648 (caddr_t)get_hblk_base(hmeblkp), 1649 get_hblk_endaddr(hmeblkp), 1650 NULL, HAT_UNLOAD); 1651 } 1652 nx_hblk = hmeblkp->hblk_next; 1653 nx_pa = hmeblkp->hblk_nextpa; 1654 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) { 1655 ASSERT(!hmeblkp->hblk_lckcnt); 1656 sfmmu_hblk_hash_rm(hmebp, hmeblkp, 1657 prevpa, pr_hblk); 1658 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 1659 } else { 1660 pr_hblk = hmeblkp; 1661 prevpa = hblkpa; 1662 } 1663 hmeblkp = nx_hblk; 1664 hblkpa = nx_pa; 1665 } 1666 SFMMU_HASH_UNLOCK(hmebp); 1667 } 1668 1669 sfmmu_hblks_list_purge(&list); 1670 1671 /* 1672 * Now free up the ctx so that others can reuse it. 1673 */ 1674 hatlockp = sfmmu_hat_enter(sfmmup); 1675 1676 sfmmu_invalidate_ctx(sfmmup); 1677 1678 /* 1679 * Free TSBs, but not tsbinfos, and set SWAPPED flag. 1680 * If TSBs were never swapped in, just return. 1681 * This implies that we don't support partial swapping 1682 * of TSBs -- either all are swapped out, or none are. 1683 * 1684 * We must hold the HAT lock here to prevent racing with another 1685 * thread trying to unmap TTEs from the TSB or running the post- 1686 * relocator after relocating the TSB's memory. Unfortunately, we 1687 * can't free memory while holding the HAT lock or we could 1688 * deadlock, so we build a list of TSBs to be freed after marking 1689 * the tsbinfos as swapped out and free them after dropping the 1690 * lock. 1691 */ 1692 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) { 1693 sfmmu_hat_exit(hatlockp); 1694 return; 1695 } 1696 1697 SFMMU_FLAGS_SET(sfmmup, HAT_SWAPPED); 1698 last = freelist = NULL; 1699 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL; 1700 tsbinfop = tsbinfop->tsb_next) { 1701 ASSERT((tsbinfop->tsb_flags & TSB_SWAPPED) == 0); 1702 1703 /* 1704 * Cast the TSB into a struct free_tsb and put it on the free 1705 * list. 1706 */ 1707 if (freelist == NULL) { 1708 last = freelist = (struct free_tsb *)tsbinfop->tsb_va; 1709 } else { 1710 last->next = (struct free_tsb *)tsbinfop->tsb_va; 1711 last = last->next; 1712 } 1713 last->next = NULL; 1714 last->tsbinfop = tsbinfop; 1715 tsbinfop->tsb_flags |= TSB_SWAPPED; 1716 /* 1717 * Zero out the TTE to clear the valid bit. 1718 * Note we can't use a value like 0xbad because we want to 1719 * ensure diagnostic bits are NEVER set on TTEs that might 1720 * be loaded. The intent is to catch any invalid access 1721 * to the swapped TSB, such as a thread running with a valid 1722 * context without first calling sfmmu_tsb_swapin() to 1723 * allocate TSB memory. 1724 */ 1725 tsbinfop->tsb_tte.ll = 0; 1726 } 1727 1728 /* Now we can drop the lock and free the TSB memory. */ 1729 sfmmu_hat_exit(hatlockp); 1730 for (; freelist != NULL; freelist = next) { 1731 next = freelist->next; 1732 sfmmu_tsb_free(freelist->tsbinfop); 1733 } 1734 } 1735 1736 /* 1737 * Duplicate the translations of an as into another newas 1738 */ 1739 /* ARGSUSED */ 1740 int 1741 hat_dup(struct hat *hat, struct hat *newhat, caddr_t addr, size_t len, 1742 uint_t flag) 1743 { 1744 ASSERT(hat->sfmmu_xhat_provider == NULL); 1745 ASSERT((flag == 0) || (flag == HAT_DUP_ALL) || (flag == HAT_DUP_COW)); 1746 1747 if (flag == HAT_DUP_COW) { 1748 panic("hat_dup: HAT_DUP_COW not supported"); 1749 } 1750 return (0); 1751 } 1752 1753 /* 1754 * Set up addr to map to page pp with protection prot. 1755 * As an optimization we also load the TSB with the 1756 * corresponding tte but it is no big deal if the tte gets kicked out. 1757 */ 1758 void 1759 hat_memload(struct hat *hat, caddr_t addr, struct page *pp, 1760 uint_t attr, uint_t flags) 1761 { 1762 tte_t tte; 1763 1764 1765 ASSERT(hat != NULL); 1766 ASSERT(PAGE_LOCKED(pp)); 1767 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET)); 1768 ASSERT(!(flags & ~SFMMU_LOAD_ALLFLAG)); 1769 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR)); 1770 1771 if (PP_ISFREE(pp)) { 1772 panic("hat_memload: loading a mapping to free page %p", 1773 (void *)pp); 1774 } 1775 1776 if (hat->sfmmu_xhat_provider) { 1777 XHAT_MEMLOAD(hat, addr, pp, attr, flags); 1778 return; 1779 } 1780 1781 ASSERT((hat == ksfmmup) || 1782 AS_LOCK_HELD(hat->sfmmu_as, &hat->sfmmu_as->a_lock)); 1783 1784 if (flags & ~SFMMU_LOAD_ALLFLAG) 1785 cmn_err(CE_NOTE, "hat_memload: unsupported flags %d", 1786 flags & ~SFMMU_LOAD_ALLFLAG); 1787 1788 if (hat->sfmmu_rmstat) 1789 hat_resvstat(MMU_PAGESIZE, hat->sfmmu_as, addr); 1790 1791 #if defined(SF_ERRATA_57) 1792 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) && 1793 (addr < errata57_limit) && (attr & PROT_EXEC) && 1794 !(flags & HAT_LOAD_SHARE)) { 1795 cmn_err(CE_WARN, "hat_memload: illegal attempt to make user " 1796 " page executable"); 1797 attr &= ~PROT_EXEC; 1798 } 1799 #endif 1800 1801 sfmmu_memtte(&tte, pp->p_pagenum, attr, TTE8K); 1802 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, flags); 1803 1804 /* 1805 * Check TSB and TLB page sizes. 1806 */ 1807 if ((flags & HAT_LOAD_SHARE) == 0) { 1808 sfmmu_check_page_sizes(hat, 1); 1809 } 1810 } 1811 1812 /* 1813 * hat_devload can be called to map real memory (e.g. 1814 * /dev/kmem) and even though hat_devload will determine pf is 1815 * for memory, it will be unable to get a shared lock on the 1816 * page (because someone else has it exclusively) and will 1817 * pass dp = NULL. If tteload doesn't get a non-NULL 1818 * page pointer it can't cache memory. 1819 */ 1820 void 1821 hat_devload(struct hat *hat, caddr_t addr, size_t len, pfn_t pfn, 1822 uint_t attr, int flags) 1823 { 1824 tte_t tte; 1825 struct page *pp = NULL; 1826 int use_lgpg = 0; 1827 1828 ASSERT(hat != NULL); 1829 1830 if (hat->sfmmu_xhat_provider) { 1831 XHAT_DEVLOAD(hat, addr, len, pfn, attr, flags); 1832 return; 1833 } 1834 1835 ASSERT(!(flags & ~SFMMU_LOAD_ALLFLAG)); 1836 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR)); 1837 ASSERT((hat == ksfmmup) || 1838 AS_LOCK_HELD(hat->sfmmu_as, &hat->sfmmu_as->a_lock)); 1839 if (len == 0) 1840 panic("hat_devload: zero len"); 1841 if (flags & ~SFMMU_LOAD_ALLFLAG) 1842 cmn_err(CE_NOTE, "hat_devload: unsupported flags %d", 1843 flags & ~SFMMU_LOAD_ALLFLAG); 1844 1845 #if defined(SF_ERRATA_57) 1846 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) && 1847 (addr < errata57_limit) && (attr & PROT_EXEC) && 1848 !(flags & HAT_LOAD_SHARE)) { 1849 cmn_err(CE_WARN, "hat_devload: illegal attempt to make user " 1850 " page executable"); 1851 attr &= ~PROT_EXEC; 1852 } 1853 #endif 1854 1855 /* 1856 * If it's a memory page find its pp 1857 */ 1858 if (!(flags & HAT_LOAD_NOCONSIST) && pf_is_memory(pfn)) { 1859 pp = page_numtopp_nolock(pfn); 1860 if (pp == NULL) { 1861 flags |= HAT_LOAD_NOCONSIST; 1862 } else { 1863 if (PP_ISFREE(pp)) { 1864 panic("hat_memload: loading " 1865 "a mapping to free page %p", 1866 (void *)pp); 1867 } 1868 if (!PAGE_LOCKED(pp) && !PP_ISNORELOC(pp)) { 1869 panic("hat_memload: loading a mapping " 1870 "to unlocked relocatable page %p", 1871 (void *)pp); 1872 } 1873 ASSERT(len == MMU_PAGESIZE); 1874 } 1875 } 1876 1877 if (hat->sfmmu_rmstat) 1878 hat_resvstat(len, hat->sfmmu_as, addr); 1879 1880 if (flags & HAT_LOAD_NOCONSIST) { 1881 attr |= SFMMU_UNCACHEVTTE; 1882 use_lgpg = 1; 1883 } 1884 if (!pf_is_memory(pfn)) { 1885 attr |= SFMMU_UNCACHEPTTE | HAT_NOSYNC; 1886 use_lgpg = 1; 1887 switch (attr & HAT_ORDER_MASK) { 1888 case HAT_STRICTORDER: 1889 case HAT_UNORDERED_OK: 1890 /* 1891 * we set the side effect bit for all non 1892 * memory mappings unless merging is ok 1893 */ 1894 attr |= SFMMU_SIDEFFECT; 1895 break; 1896 case HAT_MERGING_OK: 1897 case HAT_LOADCACHING_OK: 1898 case HAT_STORECACHING_OK: 1899 break; 1900 default: 1901 panic("hat_devload: bad attr"); 1902 break; 1903 } 1904 } 1905 while (len) { 1906 if (!use_lgpg) { 1907 sfmmu_memtte(&tte, pfn, attr, TTE8K); 1908 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, 1909 flags); 1910 len -= MMU_PAGESIZE; 1911 addr += MMU_PAGESIZE; 1912 pfn++; 1913 continue; 1914 } 1915 /* 1916 * try to use large pages, check va/pa alignments 1917 * Note that 32M/256M page sizes are not (yet) supported. 1918 */ 1919 if ((len >= MMU_PAGESIZE4M) && 1920 !((uintptr_t)addr & MMU_PAGEOFFSET4M) && 1921 !(disable_large_pages & (1 << TTE4M)) && 1922 !(mmu_ptob(pfn) & MMU_PAGEOFFSET4M)) { 1923 sfmmu_memtte(&tte, pfn, attr, TTE4M); 1924 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, 1925 flags); 1926 len -= MMU_PAGESIZE4M; 1927 addr += MMU_PAGESIZE4M; 1928 pfn += MMU_PAGESIZE4M / MMU_PAGESIZE; 1929 } else if ((len >= MMU_PAGESIZE512K) && 1930 !((uintptr_t)addr & MMU_PAGEOFFSET512K) && 1931 !(disable_large_pages & (1 << TTE512K)) && 1932 !(mmu_ptob(pfn) & MMU_PAGEOFFSET512K)) { 1933 sfmmu_memtte(&tte, pfn, attr, TTE512K); 1934 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, 1935 flags); 1936 len -= MMU_PAGESIZE512K; 1937 addr += MMU_PAGESIZE512K; 1938 pfn += MMU_PAGESIZE512K / MMU_PAGESIZE; 1939 } else if ((len >= MMU_PAGESIZE64K) && 1940 !((uintptr_t)addr & MMU_PAGEOFFSET64K) && 1941 !(disable_large_pages & (1 << TTE64K)) && 1942 !(mmu_ptob(pfn) & MMU_PAGEOFFSET64K)) { 1943 sfmmu_memtte(&tte, pfn, attr, TTE64K); 1944 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, 1945 flags); 1946 len -= MMU_PAGESIZE64K; 1947 addr += MMU_PAGESIZE64K; 1948 pfn += MMU_PAGESIZE64K / MMU_PAGESIZE; 1949 } else { 1950 sfmmu_memtte(&tte, pfn, attr, TTE8K); 1951 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, 1952 flags); 1953 len -= MMU_PAGESIZE; 1954 addr += MMU_PAGESIZE; 1955 pfn++; 1956 } 1957 } 1958 1959 /* 1960 * Check TSB and TLB page sizes. 1961 */ 1962 if ((flags & HAT_LOAD_SHARE) == 0) { 1963 sfmmu_check_page_sizes(hat, 1); 1964 } 1965 } 1966 1967 /* 1968 * Map the largest extend possible out of the page array. The array may NOT 1969 * be in order. The largest possible mapping a page can have 1970 * is specified in the p_szc field. The p_szc field 1971 * cannot change as long as there any mappings (large or small) 1972 * to any of the pages that make up the large page. (ie. any 1973 * promotion/demotion of page size is not up to the hat but up to 1974 * the page free list manager). The array 1975 * should consist of properly aligned contigous pages that are 1976 * part of a big page for a large mapping to be created. 1977 */ 1978 void 1979 hat_memload_array(struct hat *hat, caddr_t addr, size_t len, 1980 struct page **pps, uint_t attr, uint_t flags) 1981 { 1982 int ttesz; 1983 size_t mapsz; 1984 pgcnt_t numpg, npgs; 1985 tte_t tte; 1986 page_t *pp; 1987 int large_pages_disable; 1988 1989 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET)); 1990 1991 if (hat->sfmmu_xhat_provider) { 1992 XHAT_MEMLOAD_ARRAY(hat, addr, len, pps, attr, flags); 1993 return; 1994 } 1995 1996 if (hat->sfmmu_rmstat) 1997 hat_resvstat(len, hat->sfmmu_as, addr); 1998 1999 #if defined(SF_ERRATA_57) 2000 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) && 2001 (addr < errata57_limit) && (attr & PROT_EXEC) && 2002 !(flags & HAT_LOAD_SHARE)) { 2003 cmn_err(CE_WARN, "hat_memload_array: illegal attempt to make " 2004 "user page executable"); 2005 attr &= ~PROT_EXEC; 2006 } 2007 #endif 2008 2009 /* Get number of pages */ 2010 npgs = len >> MMU_PAGESHIFT; 2011 2012 if (flags & HAT_LOAD_SHARE) { 2013 large_pages_disable = disable_ism_large_pages; 2014 } else { 2015 large_pages_disable = disable_large_pages; 2016 } 2017 2018 if (npgs < NHMENTS || large_pages_disable == LARGE_PAGES_OFF) { 2019 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags, npgs); 2020 return; 2021 } 2022 2023 while (npgs >= NHMENTS) { 2024 pp = *pps; 2025 for (ttesz = pp->p_szc; ttesz != TTE8K; ttesz--) { 2026 /* 2027 * Check if this page size is disabled. 2028 */ 2029 if (large_pages_disable & (1 << ttesz)) 2030 continue; 2031 2032 numpg = TTEPAGES(ttesz); 2033 mapsz = numpg << MMU_PAGESHIFT; 2034 if ((npgs >= numpg) && 2035 IS_P2ALIGNED(addr, mapsz) && 2036 IS_P2ALIGNED(pp->p_pagenum, numpg)) { 2037 /* 2038 * At this point we have enough pages and 2039 * we know the virtual address and the pfn 2040 * are properly aligned. We still need 2041 * to check for physical contiguity but since 2042 * it is very likely that this is the case 2043 * we will assume they are so and undo 2044 * the request if necessary. It would 2045 * be great if we could get a hint flag 2046 * like HAT_CONTIG which would tell us 2047 * the pages are contigous for sure. 2048 */ 2049 sfmmu_memtte(&tte, (*pps)->p_pagenum, 2050 attr, ttesz); 2051 if (!sfmmu_tteload_array(hat, &tte, addr, 2052 pps, flags)) { 2053 break; 2054 } 2055 } 2056 } 2057 if (ttesz == TTE8K) { 2058 /* 2059 * We were not able to map array using a large page 2060 * batch a hmeblk or fraction at a time. 2061 */ 2062 numpg = ((uintptr_t)addr >> MMU_PAGESHIFT) 2063 & (NHMENTS-1); 2064 numpg = NHMENTS - numpg; 2065 ASSERT(numpg <= npgs); 2066 mapsz = numpg * MMU_PAGESIZE; 2067 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags, 2068 numpg); 2069 } 2070 addr += mapsz; 2071 npgs -= numpg; 2072 pps += numpg; 2073 } 2074 2075 if (npgs) { 2076 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags, npgs); 2077 } 2078 2079 /* 2080 * Check TSB and TLB page sizes. 2081 */ 2082 if ((flags & HAT_LOAD_SHARE) == 0) { 2083 sfmmu_check_page_sizes(hat, 1); 2084 } 2085 } 2086 2087 /* 2088 * Function tries to batch 8K pages into the same hme blk. 2089 */ 2090 static void 2091 sfmmu_memload_batchsmall(struct hat *hat, caddr_t vaddr, page_t **pps, 2092 uint_t attr, uint_t flags, pgcnt_t npgs) 2093 { 2094 tte_t tte; 2095 page_t *pp; 2096 struct hmehash_bucket *hmebp; 2097 struct hme_blk *hmeblkp; 2098 int index; 2099 2100 while (npgs) { 2101 /* 2102 * Acquire the hash bucket. 2103 */ 2104 hmebp = sfmmu_tteload_acquire_hashbucket(hat, vaddr, TTE8K); 2105 ASSERT(hmebp); 2106 2107 /* 2108 * Find the hment block. 2109 */ 2110 hmeblkp = sfmmu_tteload_find_hmeblk(hat, hmebp, vaddr, 2111 TTE8K, flags); 2112 ASSERT(hmeblkp); 2113 2114 do { 2115 /* 2116 * Make the tte. 2117 */ 2118 pp = *pps; 2119 sfmmu_memtte(&tte, pp->p_pagenum, attr, TTE8K); 2120 2121 /* 2122 * Add the translation. 2123 */ 2124 (void) sfmmu_tteload_addentry(hat, hmeblkp, &tte, 2125 vaddr, pps, flags); 2126 2127 /* 2128 * Goto next page. 2129 */ 2130 pps++; 2131 npgs--; 2132 2133 /* 2134 * Goto next address. 2135 */ 2136 vaddr += MMU_PAGESIZE; 2137 2138 /* 2139 * Don't crossover into a different hmentblk. 2140 */ 2141 index = (int)(((uintptr_t)vaddr >> MMU_PAGESHIFT) & 2142 (NHMENTS-1)); 2143 2144 } while (index != 0 && npgs != 0); 2145 2146 /* 2147 * Release the hash bucket. 2148 */ 2149 2150 sfmmu_tteload_release_hashbucket(hmebp); 2151 } 2152 } 2153 2154 /* 2155 * Construct a tte for a page: 2156 * 2157 * tte_valid = 1 2158 * tte_size2 = size & TTE_SZ2_BITS (Panther and Olympus-C only) 2159 * tte_size = size 2160 * tte_nfo = attr & HAT_NOFAULT 2161 * tte_ie = attr & HAT_STRUCTURE_LE 2162 * tte_hmenum = hmenum 2163 * tte_pahi = pp->p_pagenum >> TTE_PASHIFT; 2164 * tte_palo = pp->p_pagenum & TTE_PALOMASK; 2165 * tte_ref = 1 (optimization) 2166 * tte_wr_perm = attr & PROT_WRITE; 2167 * tte_no_sync = attr & HAT_NOSYNC 2168 * tte_lock = attr & SFMMU_LOCKTTE 2169 * tte_cp = !(attr & SFMMU_UNCACHEPTTE) 2170 * tte_cv = !(attr & SFMMU_UNCACHEVTTE) 2171 * tte_e = attr & SFMMU_SIDEFFECT 2172 * tte_priv = !(attr & PROT_USER) 2173 * tte_hwwr = if nosync is set and it is writable we set the mod bit (opt) 2174 * tte_glb = 0 2175 */ 2176 void 2177 sfmmu_memtte(tte_t *ttep, pfn_t pfn, uint_t attr, int tte_sz) 2178 { 2179 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR)); 2180 2181 ttep->tte_inthi = MAKE_TTE_INTHI(pfn, attr, tte_sz, 0 /* hmenum */); 2182 ttep->tte_intlo = MAKE_TTE_INTLO(pfn, attr, tte_sz, 0 /* hmenum */); 2183 2184 if (TTE_IS_NOSYNC(ttep)) { 2185 TTE_SET_REF(ttep); 2186 if (TTE_IS_WRITABLE(ttep)) { 2187 TTE_SET_MOD(ttep); 2188 } 2189 } 2190 if (TTE_IS_NFO(ttep) && TTE_IS_EXECUTABLE(ttep)) { 2191 panic("sfmmu_memtte: can't set both NFO and EXEC bits"); 2192 } 2193 } 2194 2195 /* 2196 * This function will add a translation to the hme_blk and allocate the 2197 * hme_blk if one does not exist. 2198 * If a page structure is specified then it will add the 2199 * corresponding hment to the mapping list. 2200 * It will also update the hmenum field for the tte. 2201 */ 2202 void 2203 sfmmu_tteload(struct hat *sfmmup, tte_t *ttep, caddr_t vaddr, page_t *pp, 2204 uint_t flags) 2205 { 2206 (void) sfmmu_tteload_array(sfmmup, ttep, vaddr, &pp, flags); 2207 } 2208 2209 /* 2210 * Load (ttep != NULL) or unload (ttep == NULL) one entry in the TSB. 2211 * Assumes that a particular page size may only be resident in one TSB. 2212 */ 2213 static void 2214 sfmmu_mod_tsb(sfmmu_t *sfmmup, caddr_t vaddr, tte_t *ttep, int ttesz) 2215 { 2216 struct tsb_info *tsbinfop = NULL; 2217 uint64_t tag; 2218 struct tsbe *tsbe_addr; 2219 uint64_t tsb_base; 2220 uint_t tsb_size; 2221 int vpshift = MMU_PAGESHIFT; 2222 int phys = 0; 2223 2224 if (sfmmup == ksfmmup) { /* No support for 32/256M ksfmmu pages */ 2225 phys = ktsb_phys; 2226 if (ttesz >= TTE4M) { 2227 #ifndef sun4v 2228 ASSERT((ttesz != TTE32M) && (ttesz != TTE256M)); 2229 #endif 2230 tsb_base = (phys)? ktsb4m_pbase : (uint64_t)ktsb4m_base; 2231 tsb_size = ktsb4m_szcode; 2232 } else { 2233 tsb_base = (phys)? ktsb_pbase : (uint64_t)ktsb_base; 2234 tsb_size = ktsb_szcode; 2235 } 2236 } else { 2237 SFMMU_GET_TSBINFO(tsbinfop, sfmmup, ttesz); 2238 2239 /* 2240 * If there isn't a TSB for this page size, or the TSB is 2241 * swapped out, there is nothing to do. Note that the latter 2242 * case seems impossible but can occur if hat_pageunload() 2243 * is called on an ISM mapping while the process is swapped 2244 * out. 2245 */ 2246 if (tsbinfop == NULL || (tsbinfop->tsb_flags & TSB_SWAPPED)) 2247 return; 2248 2249 /* 2250 * If another thread is in the middle of relocating a TSB 2251 * we can't unload the entry so set a flag so that the 2252 * TSB will be flushed before it can be accessed by the 2253 * process. 2254 */ 2255 if ((tsbinfop->tsb_flags & TSB_RELOC_FLAG) != 0) { 2256 if (ttep == NULL) 2257 tsbinfop->tsb_flags |= TSB_FLUSH_NEEDED; 2258 return; 2259 } 2260 #if defined(UTSB_PHYS) 2261 phys = 1; 2262 tsb_base = (uint64_t)tsbinfop->tsb_pa; 2263 #else 2264 tsb_base = (uint64_t)tsbinfop->tsb_va; 2265 #endif 2266 tsb_size = tsbinfop->tsb_szc; 2267 } 2268 if (ttesz >= TTE4M) 2269 vpshift = MMU_PAGESHIFT4M; 2270 2271 tsbe_addr = sfmmu_get_tsbe(tsb_base, vaddr, vpshift, tsb_size); 2272 tag = sfmmu_make_tsbtag(vaddr); 2273 2274 if (ttep == NULL) { 2275 sfmmu_unload_tsbe(tsbe_addr, tag, phys); 2276 } else { 2277 if (ttesz >= TTE4M) { 2278 SFMMU_STAT(sf_tsb_load4m); 2279 } else { 2280 SFMMU_STAT(sf_tsb_load8k); 2281 } 2282 2283 sfmmu_load_tsbe(tsbe_addr, tag, ttep, phys); 2284 } 2285 } 2286 2287 /* 2288 * Unmap all entries from [start, end) matching the given page size. 2289 * 2290 * This function is used primarily to unmap replicated 64K or 512K entries 2291 * from the TSB that are inserted using the base page size TSB pointer, but 2292 * it may also be called to unmap a range of addresses from the TSB. 2293 */ 2294 void 2295 sfmmu_unload_tsb_range(sfmmu_t *sfmmup, caddr_t start, caddr_t end, int ttesz) 2296 { 2297 struct tsb_info *tsbinfop; 2298 uint64_t tag; 2299 struct tsbe *tsbe_addr; 2300 caddr_t vaddr; 2301 uint64_t tsb_base; 2302 int vpshift, vpgsz; 2303 uint_t tsb_size; 2304 int phys = 0; 2305 2306 /* 2307 * Assumptions: 2308 * If ttesz == 8K, 64K or 512K, we walk through the range 8K 2309 * at a time shooting down any valid entries we encounter. 2310 * 2311 * If ttesz >= 4M we walk the range 4M at a time shooting 2312 * down any valid mappings we find. 2313 */ 2314 if (sfmmup == ksfmmup) { 2315 phys = ktsb_phys; 2316 if (ttesz >= TTE4M) { 2317 #ifndef sun4v 2318 ASSERT((ttesz != TTE32M) && (ttesz != TTE256M)); 2319 #endif 2320 tsb_base = (phys)? ktsb4m_pbase : (uint64_t)ktsb4m_base; 2321 tsb_size = ktsb4m_szcode; 2322 } else { 2323 tsb_base = (phys)? ktsb_pbase : (uint64_t)ktsb_base; 2324 tsb_size = ktsb_szcode; 2325 } 2326 } else { 2327 SFMMU_GET_TSBINFO(tsbinfop, sfmmup, ttesz); 2328 2329 /* 2330 * If there isn't a TSB for this page size, or the TSB is 2331 * swapped out, there is nothing to do. Note that the latter 2332 * case seems impossible but can occur if hat_pageunload() 2333 * is called on an ISM mapping while the process is swapped 2334 * out. 2335 */ 2336 if (tsbinfop == NULL || (tsbinfop->tsb_flags & TSB_SWAPPED)) 2337 return; 2338 2339 /* 2340 * If another thread is in the middle of relocating a TSB 2341 * we can't unload the entry so set a flag so that the 2342 * TSB will be flushed before it can be accessed by the 2343 * process. 2344 */ 2345 if ((tsbinfop->tsb_flags & TSB_RELOC_FLAG) != 0) { 2346 tsbinfop->tsb_flags |= TSB_FLUSH_NEEDED; 2347 return; 2348 } 2349 #if defined(UTSB_PHYS) 2350 phys = 1; 2351 tsb_base = (uint64_t)tsbinfop->tsb_pa; 2352 #else 2353 tsb_base = (uint64_t)tsbinfop->tsb_va; 2354 #endif 2355 tsb_size = tsbinfop->tsb_szc; 2356 } 2357 if (ttesz >= TTE4M) { 2358 vpshift = MMU_PAGESHIFT4M; 2359 vpgsz = MMU_PAGESIZE4M; 2360 } else { 2361 vpshift = MMU_PAGESHIFT; 2362 vpgsz = MMU_PAGESIZE; 2363 } 2364 2365 for (vaddr = start; vaddr < end; vaddr += vpgsz) { 2366 tag = sfmmu_make_tsbtag(vaddr); 2367 tsbe_addr = sfmmu_get_tsbe(tsb_base, vaddr, vpshift, tsb_size); 2368 sfmmu_unload_tsbe(tsbe_addr, tag, phys); 2369 } 2370 } 2371 2372 /* 2373 * Select the optimum TSB size given the number of mappings 2374 * that need to be cached. 2375 */ 2376 static int 2377 sfmmu_select_tsb_szc(pgcnt_t pgcnt) 2378 { 2379 int szc = 0; 2380 2381 #ifdef DEBUG 2382 if (tsb_grow_stress) { 2383 uint32_t randval = (uint32_t)gettick() >> 4; 2384 return (randval % (tsb_max_growsize + 1)); 2385 } 2386 #endif /* DEBUG */ 2387 2388 while ((szc < tsb_max_growsize) && (pgcnt > SFMMU_RSS_TSBSIZE(szc))) 2389 szc++; 2390 return (szc); 2391 } 2392 2393 /* 2394 * This function will add a translation to the hme_blk and allocate the 2395 * hme_blk if one does not exist. 2396 * If a page structure is specified then it will add the 2397 * corresponding hment to the mapping list. 2398 * It will also update the hmenum field for the tte. 2399 * Furthermore, it attempts to create a large page translation 2400 * for <addr,hat> at page array pps. It assumes addr and first 2401 * pp is correctly aligned. It returns 0 if successful and 1 otherwise. 2402 */ 2403 static int 2404 sfmmu_tteload_array(sfmmu_t *sfmmup, tte_t *ttep, caddr_t vaddr, 2405 page_t **pps, uint_t flags) 2406 { 2407 struct hmehash_bucket *hmebp; 2408 struct hme_blk *hmeblkp; 2409 int ret; 2410 uint_t size; 2411 2412 /* 2413 * Get mapping size. 2414 */ 2415 size = TTE_CSZ(ttep); 2416 ASSERT(!((uintptr_t)vaddr & TTE_PAGE_OFFSET(size))); 2417 2418 /* 2419 * Acquire the hash bucket. 2420 */ 2421 hmebp = sfmmu_tteload_acquire_hashbucket(sfmmup, vaddr, size); 2422 ASSERT(hmebp); 2423 2424 /* 2425 * Find the hment block. 2426 */ 2427 hmeblkp = sfmmu_tteload_find_hmeblk(sfmmup, hmebp, vaddr, size, flags); 2428 ASSERT(hmeblkp); 2429 2430 /* 2431 * Add the translation. 2432 */ 2433 ret = sfmmu_tteload_addentry(sfmmup, hmeblkp, ttep, vaddr, pps, flags); 2434 2435 /* 2436 * Release the hash bucket. 2437 */ 2438 sfmmu_tteload_release_hashbucket(hmebp); 2439 2440 return (ret); 2441 } 2442 2443 /* 2444 * Function locks and returns a pointer to the hash bucket for vaddr and size. 2445 */ 2446 static struct hmehash_bucket * 2447 sfmmu_tteload_acquire_hashbucket(sfmmu_t *sfmmup, caddr_t vaddr, int size) 2448 { 2449 struct hmehash_bucket *hmebp; 2450 int hmeshift; 2451 2452 hmeshift = HME_HASH_SHIFT(size); 2453 2454 hmebp = HME_HASH_FUNCTION(sfmmup, vaddr, hmeshift); 2455 2456 SFMMU_HASH_LOCK(hmebp); 2457 2458 return (hmebp); 2459 } 2460 2461 /* 2462 * Function returns a pointer to an hmeblk in the hash bucket, hmebp. If the 2463 * hmeblk doesn't exists for the [sfmmup, vaddr & size] signature, a hmeblk is 2464 * allocated. 2465 */ 2466 static struct hme_blk * 2467 sfmmu_tteload_find_hmeblk(sfmmu_t *sfmmup, struct hmehash_bucket *hmebp, 2468 caddr_t vaddr, uint_t size, uint_t flags) 2469 { 2470 hmeblk_tag hblktag; 2471 int hmeshift; 2472 struct hme_blk *hmeblkp, *pr_hblk, *list = NULL; 2473 uint64_t hblkpa, prevpa; 2474 struct kmem_cache *sfmmu_cache; 2475 uint_t forcefree; 2476 2477 hblktag.htag_id = sfmmup; 2478 hmeshift = HME_HASH_SHIFT(size); 2479 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift); 2480 hblktag.htag_rehash = HME_HASH_REHASH(size); 2481 2482 ttearray_realloc: 2483 2484 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, hblkpa, 2485 pr_hblk, prevpa, &list); 2486 2487 /* 2488 * We block until hblk_reserve_lock is released; it's held by 2489 * the thread, temporarily using hblk_reserve, until hblk_reserve is 2490 * replaced by a hblk from sfmmu8_cache. 2491 */ 2492 if (hmeblkp == (struct hme_blk *)hblk_reserve && 2493 hblk_reserve_thread != curthread) { 2494 SFMMU_HASH_UNLOCK(hmebp); 2495 mutex_enter(&hblk_reserve_lock); 2496 mutex_exit(&hblk_reserve_lock); 2497 SFMMU_STAT(sf_hblk_reserve_hit); 2498 SFMMU_HASH_LOCK(hmebp); 2499 goto ttearray_realloc; 2500 } 2501 2502 if (hmeblkp == NULL) { 2503 hmeblkp = sfmmu_hblk_alloc(sfmmup, vaddr, hmebp, size, 2504 hblktag, flags); 2505 } else { 2506 /* 2507 * It is possible for 8k and 64k hblks to collide since they 2508 * have the same rehash value. This is because we 2509 * lazily free hblks and 8K/64K blks could be lingering. 2510 * If we find size mismatch we free the block and & try again. 2511 */ 2512 if (get_hblk_ttesz(hmeblkp) != size) { 2513 ASSERT(!hmeblkp->hblk_vcnt); 2514 ASSERT(!hmeblkp->hblk_hmecnt); 2515 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, pr_hblk); 2516 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 2517 goto ttearray_realloc; 2518 } 2519 if (hmeblkp->hblk_shw_bit) { 2520 /* 2521 * if the hblk was previously used as a shadow hblk then 2522 * we will change it to a normal hblk 2523 */ 2524 if (hmeblkp->hblk_shw_mask) { 2525 sfmmu_shadow_hcleanup(sfmmup, hmeblkp, hmebp); 2526 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 2527 goto ttearray_realloc; 2528 } else { 2529 hmeblkp->hblk_shw_bit = 0; 2530 } 2531 } 2532 SFMMU_STAT(sf_hblk_hit); 2533 } 2534 2535 /* 2536 * hat_memload() should never call kmem_cache_free(); see block 2537 * comment showing the stacktrace in sfmmu_hblk_alloc(); 2538 * enqueue each hblk in the list to reserve list if it's created 2539 * from sfmmu8_cache *and* sfmmup == KHATID. 2540 */ 2541 forcefree = (sfmmup == KHATID) ? 1 : 0; 2542 while ((pr_hblk = list) != NULL) { 2543 list = pr_hblk->hblk_next; 2544 sfmmu_cache = get_hblk_cache(pr_hblk); 2545 if ((sfmmu_cache == sfmmu8_cache) && 2546 sfmmu_put_free_hblk(pr_hblk, forcefree)) 2547 continue; 2548 2549 ASSERT(sfmmup != KHATID); 2550 kmem_cache_free(sfmmu_cache, pr_hblk); 2551 } 2552 2553 ASSERT(get_hblk_ttesz(hmeblkp) == size); 2554 ASSERT(!hmeblkp->hblk_shw_bit); 2555 2556 return (hmeblkp); 2557 } 2558 2559 /* 2560 * Function adds a tte entry into the hmeblk. It returns 0 if successful and 1 2561 * otherwise. 2562 */ 2563 static int 2564 sfmmu_tteload_addentry(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, tte_t *ttep, 2565 caddr_t vaddr, page_t **pps, uint_t flags) 2566 { 2567 page_t *pp = *pps; 2568 int hmenum, size, remap; 2569 tte_t tteold, flush_tte; 2570 #ifdef DEBUG 2571 tte_t orig_old; 2572 #endif /* DEBUG */ 2573 struct sf_hment *sfhme; 2574 kmutex_t *pml, *pmtx; 2575 hatlock_t *hatlockp; 2576 2577 /* 2578 * remove this panic when we decide to let user virtual address 2579 * space be >= USERLIMIT. 2580 */ 2581 if (!TTE_IS_PRIVILEGED(ttep) && vaddr >= (caddr_t)USERLIMIT) 2582 panic("user addr %p in kernel space", vaddr); 2583 #if defined(TTE_IS_GLOBAL) 2584 if (TTE_IS_GLOBAL(ttep)) 2585 panic("sfmmu_tteload: creating global tte"); 2586 #endif 2587 2588 #ifdef DEBUG 2589 if (pf_is_memory(sfmmu_ttetopfn(ttep, vaddr)) && 2590 !TTE_IS_PCACHEABLE(ttep) && !sfmmu_allow_nc_trans) 2591 panic("sfmmu_tteload: non cacheable memory tte"); 2592 #endif /* DEBUG */ 2593 2594 if ((flags & HAT_LOAD_SHARE) || !TTE_IS_REF(ttep) || 2595 !TTE_IS_MOD(ttep)) { 2596 /* 2597 * Don't load TSB for dummy as in ISM. Also don't preload 2598 * the TSB if the TTE isn't writable since we're likely to 2599 * fault on it again -- preloading can be fairly expensive. 2600 */ 2601 flags |= SFMMU_NO_TSBLOAD; 2602 } 2603 2604 size = TTE_CSZ(ttep); 2605 switch (size) { 2606 case TTE8K: 2607 SFMMU_STAT(sf_tteload8k); 2608 break; 2609 case TTE64K: 2610 SFMMU_STAT(sf_tteload64k); 2611 break; 2612 case TTE512K: 2613 SFMMU_STAT(sf_tteload512k); 2614 break; 2615 case TTE4M: 2616 SFMMU_STAT(sf_tteload4m); 2617 break; 2618 case (TTE32M): 2619 SFMMU_STAT(sf_tteload32m); 2620 ASSERT(mmu_page_sizes == max_mmu_page_sizes); 2621 break; 2622 case (TTE256M): 2623 SFMMU_STAT(sf_tteload256m); 2624 ASSERT(mmu_page_sizes == max_mmu_page_sizes); 2625 break; 2626 } 2627 2628 ASSERT(!((uintptr_t)vaddr & TTE_PAGE_OFFSET(size))); 2629 2630 HBLKTOHME_IDX(sfhme, hmeblkp, vaddr, hmenum); 2631 2632 /* 2633 * Need to grab mlist lock here so that pageunload 2634 * will not change tte behind us. 2635 */ 2636 if (pp) { 2637 pml = sfmmu_mlist_enter(pp); 2638 } 2639 2640 sfmmu_copytte(&sfhme->hme_tte, &tteold); 2641 /* 2642 * Look for corresponding hment and if valid verify 2643 * pfns are equal. 2644 */ 2645 remap = TTE_IS_VALID(&tteold); 2646 if (remap) { 2647 pfn_t new_pfn, old_pfn; 2648 2649 old_pfn = TTE_TO_PFN(vaddr, &tteold); 2650 new_pfn = TTE_TO_PFN(vaddr, ttep); 2651 2652 if (flags & HAT_LOAD_REMAP) { 2653 /* make sure we are remapping same type of pages */ 2654 if (pf_is_memory(old_pfn) != pf_is_memory(new_pfn)) { 2655 panic("sfmmu_tteload - tte remap io<->memory"); 2656 } 2657 if (old_pfn != new_pfn && 2658 (pp != NULL || sfhme->hme_page != NULL)) { 2659 panic("sfmmu_tteload - tte remap pp != NULL"); 2660 } 2661 } else if (old_pfn != new_pfn) { 2662 panic("sfmmu_tteload - tte remap, hmeblkp 0x%p", 2663 (void *)hmeblkp); 2664 } 2665 ASSERT(TTE_CSZ(&tteold) == TTE_CSZ(ttep)); 2666 } 2667 2668 if (pp) { 2669 if (size == TTE8K) { 2670 #ifdef VAC 2671 /* 2672 * Handle VAC consistency 2673 */ 2674 if (!remap && (cache & CACHE_VAC) && !PP_ISNC(pp)) { 2675 sfmmu_vac_conflict(sfmmup, vaddr, pp); 2676 } 2677 #endif 2678 2679 if (TTE_IS_WRITABLE(ttep) && PP_ISRO(pp)) { 2680 pmtx = sfmmu_page_enter(pp); 2681 PP_CLRRO(pp); 2682 sfmmu_page_exit(pmtx); 2683 } else if (!PP_ISMAPPED(pp) && 2684 (!TTE_IS_WRITABLE(ttep)) && !(PP_ISMOD(pp))) { 2685 pmtx = sfmmu_page_enter(pp); 2686 if (!(PP_ISMOD(pp))) { 2687 PP_SETRO(pp); 2688 } 2689 sfmmu_page_exit(pmtx); 2690 } 2691 2692 } else if (sfmmu_pagearray_setup(vaddr, pps, ttep, remap)) { 2693 /* 2694 * sfmmu_pagearray_setup failed so return 2695 */ 2696 sfmmu_mlist_exit(pml); 2697 return (1); 2698 } 2699 } 2700 2701 /* 2702 * Make sure hment is not on a mapping list. 2703 */ 2704 ASSERT(remap || (sfhme->hme_page == NULL)); 2705 2706 /* if it is not a remap then hme->next better be NULL */ 2707 ASSERT((!remap) ? sfhme->hme_next == NULL : 1); 2708 2709 if (flags & HAT_LOAD_LOCK) { 2710 if (((int)hmeblkp->hblk_lckcnt + 1) >= MAX_HBLK_LCKCNT) { 2711 panic("too high lckcnt-hmeblk %p", 2712 (void *)hmeblkp); 2713 } 2714 atomic_add_16(&hmeblkp->hblk_lckcnt, 1); 2715 2716 HBLK_STACK_TRACE(hmeblkp, HBLK_LOCK); 2717 } 2718 2719 #ifdef VAC 2720 if (pp && PP_ISNC(pp)) { 2721 /* 2722 * If the physical page is marked to be uncacheable, like 2723 * by a vac conflict, make sure the new mapping is also 2724 * uncacheable. 2725 */ 2726 TTE_CLR_VCACHEABLE(ttep); 2727 ASSERT(PP_GET_VCOLOR(pp) == NO_VCOLOR); 2728 } 2729 #endif 2730 ttep->tte_hmenum = hmenum; 2731 2732 #ifdef DEBUG 2733 orig_old = tteold; 2734 #endif /* DEBUG */ 2735 2736 while (sfmmu_modifytte_try(&tteold, ttep, &sfhme->hme_tte) < 0) { 2737 if ((sfmmup == KHATID) && 2738 (flags & (HAT_LOAD_LOCK | HAT_LOAD_REMAP))) { 2739 sfmmu_copytte(&sfhme->hme_tte, &tteold); 2740 } 2741 #ifdef DEBUG 2742 chk_tte(&orig_old, &tteold, ttep, hmeblkp); 2743 #endif /* DEBUG */ 2744 } 2745 2746 if (!TTE_IS_VALID(&tteold)) { 2747 2748 atomic_add_16(&hmeblkp->hblk_vcnt, 1); 2749 atomic_add_long(&sfmmup->sfmmu_ttecnt[size], 1); 2750 2751 /* 2752 * HAT_RELOAD_SHARE has been deprecated with lpg DISM. 2753 */ 2754 2755 if (size > TTE8K && (flags & HAT_LOAD_SHARE) == 0 && 2756 sfmmup != ksfmmup) { 2757 /* 2758 * If this is the first large mapping for the process 2759 * we must force any CPUs running this process to TL=0 2760 * where they will reload the HAT flags from the 2761 * tsbmiss area. This is necessary to make the large 2762 * mappings we are about to load visible to those CPUs; 2763 * otherwise they'll loop forever calling pagefault() 2764 * since we don't search large hash chains by default. 2765 */ 2766 hatlockp = sfmmu_hat_enter(sfmmup); 2767 if (size == TTE512K && 2768 !SFMMU_FLAGS_ISSET(sfmmup, HAT_512K_FLAG)) { 2769 SFMMU_FLAGS_SET(sfmmup, HAT_512K_FLAG); 2770 sfmmu_sync_mmustate(sfmmup); 2771 } else if (size == TTE4M && 2772 !SFMMU_FLAGS_ISSET(sfmmup, HAT_4M_FLAG)) { 2773 SFMMU_FLAGS_SET(sfmmup, HAT_4M_FLAG); 2774 sfmmu_sync_mmustate(sfmmup); 2775 } else if (size == TTE64K && 2776 !SFMMU_FLAGS_ISSET(sfmmup, HAT_64K_FLAG)) { 2777 SFMMU_FLAGS_SET(sfmmup, HAT_64K_FLAG); 2778 /* no sync mmustate; 64K shares 8K hashes */ 2779 } else if (mmu_page_sizes == max_mmu_page_sizes) { 2780 if (size == TTE32M && 2781 !SFMMU_FLAGS_ISSET(sfmmup, HAT_32M_FLAG)) { 2782 SFMMU_FLAGS_SET(sfmmup, HAT_32M_FLAG); 2783 sfmmu_sync_mmustate(sfmmup); 2784 } else if (size == TTE256M && 2785 !SFMMU_FLAGS_ISSET(sfmmup, HAT_256M_FLAG)) { 2786 SFMMU_FLAGS_SET(sfmmup, HAT_256M_FLAG); 2787 sfmmu_sync_mmustate(sfmmup); 2788 } 2789 } 2790 if (size >= TTE4M && (flags & HAT_LOAD_TEXT) && 2791 !SFMMU_FLAGS_ISSET(sfmmup, HAT_4MTEXT_FLAG)) { 2792 SFMMU_FLAGS_SET(sfmmup, HAT_4MTEXT_FLAG); 2793 } 2794 sfmmu_hat_exit(hatlockp); 2795 } 2796 } 2797 ASSERT(TTE_IS_VALID(&sfhme->hme_tte)); 2798 2799 flush_tte.tte_intlo = (tteold.tte_intlo ^ ttep->tte_intlo) & 2800 hw_tte.tte_intlo; 2801 flush_tte.tte_inthi = (tteold.tte_inthi ^ ttep->tte_inthi) & 2802 hw_tte.tte_inthi; 2803 2804 if (remap && (flush_tte.tte_inthi || flush_tte.tte_intlo)) { 2805 /* 2806 * If remap and new tte differs from old tte we need 2807 * to sync the mod bit and flush TLB/TSB. We don't 2808 * need to sync ref bit because we currently always set 2809 * ref bit in tteload. 2810 */ 2811 ASSERT(TTE_IS_REF(ttep)); 2812 if (TTE_IS_MOD(&tteold)) { 2813 sfmmu_ttesync(sfmmup, vaddr, &tteold, pp); 2814 } 2815 sfmmu_tlb_demap(vaddr, sfmmup, hmeblkp, 0, 0); 2816 xt_sync(sfmmup->sfmmu_cpusran); 2817 } 2818 2819 if ((flags & SFMMU_NO_TSBLOAD) == 0) { 2820 /* 2821 * We only preload 8K and 4M mappings into the TSB, since 2822 * 64K and 512K mappings are replicated and hence don't 2823 * have a single, unique TSB entry. Ditto for 32M/256M. 2824 */ 2825 if (size == TTE8K || size == TTE4M) { 2826 hatlockp = sfmmu_hat_enter(sfmmup); 2827 sfmmu_load_tsb(sfmmup, vaddr, &sfhme->hme_tte, size); 2828 sfmmu_hat_exit(hatlockp); 2829 } 2830 } 2831 if (pp) { 2832 if (!remap) { 2833 HME_ADD(sfhme, pp); 2834 atomic_add_16(&hmeblkp->hblk_hmecnt, 1); 2835 ASSERT(hmeblkp->hblk_hmecnt > 0); 2836 2837 /* 2838 * Cannot ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS) 2839 * see pageunload() for comment. 2840 */ 2841 } 2842 sfmmu_mlist_exit(pml); 2843 } 2844 2845 return (0); 2846 } 2847 /* 2848 * Function unlocks hash bucket. 2849 */ 2850 static void 2851 sfmmu_tteload_release_hashbucket(struct hmehash_bucket *hmebp) 2852 { 2853 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 2854 SFMMU_HASH_UNLOCK(hmebp); 2855 } 2856 2857 /* 2858 * function which checks and sets up page array for a large 2859 * translation. Will set p_vcolor, p_index, p_ro fields. 2860 * Assumes addr and pfnum of first page are properly aligned. 2861 * Will check for physical contiguity. If check fails it return 2862 * non null. 2863 */ 2864 static int 2865 sfmmu_pagearray_setup(caddr_t addr, page_t **pps, tte_t *ttep, int remap) 2866 { 2867 int i, index, ttesz; 2868 pfn_t pfnum; 2869 pgcnt_t npgs; 2870 page_t *pp, *pp1; 2871 kmutex_t *pmtx; 2872 #ifdef VAC 2873 int osz; 2874 int cflags = 0; 2875 int vac_err = 0; 2876 #endif 2877 int newidx = 0; 2878 2879 ttesz = TTE_CSZ(ttep); 2880 2881 ASSERT(ttesz > TTE8K); 2882 2883 npgs = TTEPAGES(ttesz); 2884 index = PAGESZ_TO_INDEX(ttesz); 2885 2886 pfnum = (*pps)->p_pagenum; 2887 ASSERT(IS_P2ALIGNED(pfnum, npgs)); 2888 2889 /* 2890 * Save the first pp so we can do HAT_TMPNC at the end. 2891 */ 2892 pp1 = *pps; 2893 #ifdef VAC 2894 osz = fnd_mapping_sz(pp1); 2895 #endif 2896 2897 for (i = 0; i < npgs; i++, pps++) { 2898 pp = *pps; 2899 ASSERT(PAGE_LOCKED(pp)); 2900 ASSERT(pp->p_szc >= ttesz); 2901 ASSERT(pp->p_szc == pp1->p_szc); 2902 ASSERT(sfmmu_mlist_held(pp)); 2903 2904 /* 2905 * XXX is it possible to maintain P_RO on the root only? 2906 */ 2907 if (TTE_IS_WRITABLE(ttep) && PP_ISRO(pp)) { 2908 pmtx = sfmmu_page_enter(pp); 2909 PP_CLRRO(pp); 2910 sfmmu_page_exit(pmtx); 2911 } else if (!PP_ISMAPPED(pp) && !TTE_IS_WRITABLE(ttep) && 2912 !PP_ISMOD(pp)) { 2913 pmtx = sfmmu_page_enter(pp); 2914 if (!(PP_ISMOD(pp))) { 2915 PP_SETRO(pp); 2916 } 2917 sfmmu_page_exit(pmtx); 2918 } 2919 2920 /* 2921 * If this is a remap we skip vac & contiguity checks. 2922 */ 2923 if (remap) 2924 continue; 2925 2926 /* 2927 * set p_vcolor and detect any vac conflicts. 2928 */ 2929 #ifdef VAC 2930 if (vac_err == 0) { 2931 vac_err = sfmmu_vacconflict_array(addr, pp, &cflags); 2932 2933 } 2934 #endif 2935 2936 /* 2937 * Save current index in case we need to undo it. 2938 * Note: "PAGESZ_TO_INDEX(sz) (1 << (sz))" 2939 * "SFMMU_INDEX_SHIFT 6" 2940 * "SFMMU_INDEX_MASK ((1 << SFMMU_INDEX_SHIFT) - 1)" 2941 * "PP_MAPINDEX(p_index) (p_index & SFMMU_INDEX_MASK)" 2942 * 2943 * So: index = PAGESZ_TO_INDEX(ttesz); 2944 * if ttesz == 1 then index = 0x2 2945 * 2 then index = 0x4 2946 * 3 then index = 0x8 2947 * 4 then index = 0x10 2948 * 5 then index = 0x20 2949 * The code below checks if it's a new pagesize (ie, newidx) 2950 * in case we need to take it back out of p_index, 2951 * and then or's the new index into the existing index. 2952 */ 2953 if ((PP_MAPINDEX(pp) & index) == 0) 2954 newidx = 1; 2955 pp->p_index = (PP_MAPINDEX(pp) | index); 2956 2957 /* 2958 * contiguity check 2959 */ 2960 if (pp->p_pagenum != pfnum) { 2961 /* 2962 * If we fail the contiguity test then 2963 * the only thing we need to fix is the p_index field. 2964 * We might get a few extra flushes but since this 2965 * path is rare that is ok. The p_ro field will 2966 * get automatically fixed on the next tteload to 2967 * the page. NO TNC bit is set yet. 2968 */ 2969 while (i >= 0) { 2970 pp = *pps; 2971 if (newidx) 2972 pp->p_index = (PP_MAPINDEX(pp) & 2973 ~index); 2974 pps--; 2975 i--; 2976 } 2977 return (1); 2978 } 2979 pfnum++; 2980 addr += MMU_PAGESIZE; 2981 } 2982 2983 #ifdef VAC 2984 if (vac_err) { 2985 if (ttesz > osz) { 2986 /* 2987 * There are some smaller mappings that causes vac 2988 * conflicts. Convert all existing small mappings to 2989 * TNC. 2990 */ 2991 SFMMU_STAT_ADD(sf_uncache_conflict, npgs); 2992 sfmmu_page_cache_array(pp1, HAT_TMPNC, CACHE_FLUSH, 2993 npgs); 2994 } else { 2995 /* EMPTY */ 2996 /* 2997 * If there exists an big page mapping, 2998 * that means the whole existing big page 2999 * has TNC setting already. No need to covert to 3000 * TNC again. 3001 */ 3002 ASSERT(PP_ISTNC(pp1)); 3003 } 3004 } 3005 #endif /* VAC */ 3006 3007 return (0); 3008 } 3009 3010 #ifdef VAC 3011 /* 3012 * Routine that detects vac consistency for a large page. It also 3013 * sets virtual color for all pp's for this big mapping. 3014 */ 3015 static int 3016 sfmmu_vacconflict_array(caddr_t addr, page_t *pp, int *cflags) 3017 { 3018 int vcolor, ocolor; 3019 3020 ASSERT(sfmmu_mlist_held(pp)); 3021 3022 if (PP_ISNC(pp)) { 3023 return (HAT_TMPNC); 3024 } 3025 3026 vcolor = addr_to_vcolor(addr); 3027 if (PP_NEWPAGE(pp)) { 3028 PP_SET_VCOLOR(pp, vcolor); 3029 return (0); 3030 } 3031 3032 ocolor = PP_GET_VCOLOR(pp); 3033 if (ocolor == vcolor) { 3034 return (0); 3035 } 3036 3037 if (!PP_ISMAPPED(pp)) { 3038 /* 3039 * Previous user of page had a differnet color 3040 * but since there are no current users 3041 * we just flush the cache and change the color. 3042 * As an optimization for large pages we flush the 3043 * entire cache of that color and set a flag. 3044 */ 3045 SFMMU_STAT(sf_pgcolor_conflict); 3046 if (!CacheColor_IsFlushed(*cflags, ocolor)) { 3047 CacheColor_SetFlushed(*cflags, ocolor); 3048 sfmmu_cache_flushcolor(ocolor, pp->p_pagenum); 3049 } 3050 PP_SET_VCOLOR(pp, vcolor); 3051 return (0); 3052 } 3053 3054 /* 3055 * We got a real conflict with a current mapping. 3056 * set flags to start unencaching all mappings 3057 * and return failure so we restart looping 3058 * the pp array from the beginning. 3059 */ 3060 return (HAT_TMPNC); 3061 } 3062 #endif /* VAC */ 3063 3064 /* 3065 * creates a large page shadow hmeblk for a tte. 3066 * The purpose of this routine is to allow us to do quick unloads because 3067 * the vm layer can easily pass a very large but sparsely populated range. 3068 */ 3069 static struct hme_blk * 3070 sfmmu_shadow_hcreate(sfmmu_t *sfmmup, caddr_t vaddr, int ttesz, uint_t flags) 3071 { 3072 struct hmehash_bucket *hmebp; 3073 hmeblk_tag hblktag; 3074 int hmeshift, size, vshift; 3075 uint_t shw_mask, newshw_mask; 3076 struct hme_blk *hmeblkp; 3077 3078 ASSERT(sfmmup != KHATID); 3079 if (mmu_page_sizes == max_mmu_page_sizes) { 3080 ASSERT(ttesz < TTE256M); 3081 } else { 3082 ASSERT(ttesz < TTE4M); 3083 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0); 3084 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0); 3085 } 3086 3087 if (ttesz == TTE8K) { 3088 size = TTE512K; 3089 } else { 3090 size = ++ttesz; 3091 } 3092 3093 hblktag.htag_id = sfmmup; 3094 hmeshift = HME_HASH_SHIFT(size); 3095 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift); 3096 hblktag.htag_rehash = HME_HASH_REHASH(size); 3097 hmebp = HME_HASH_FUNCTION(sfmmup, vaddr, hmeshift); 3098 3099 SFMMU_HASH_LOCK(hmebp); 3100 3101 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 3102 ASSERT(hmeblkp != (struct hme_blk *)hblk_reserve); 3103 if (hmeblkp == NULL) { 3104 hmeblkp = sfmmu_hblk_alloc(sfmmup, vaddr, hmebp, size, 3105 hblktag, flags); 3106 } 3107 ASSERT(hmeblkp); 3108 if (!hmeblkp->hblk_shw_mask) { 3109 /* 3110 * if this is a unused hblk it was just allocated or could 3111 * potentially be a previous large page hblk so we need to 3112 * set the shadow bit. 3113 */ 3114 hmeblkp->hblk_shw_bit = 1; 3115 } 3116 ASSERT(hmeblkp->hblk_shw_bit == 1); 3117 vshift = vaddr_to_vshift(hblktag, vaddr, size); 3118 ASSERT(vshift < 8); 3119 /* 3120 * Atomically set shw mask bit 3121 */ 3122 do { 3123 shw_mask = hmeblkp->hblk_shw_mask; 3124 newshw_mask = shw_mask | (1 << vshift); 3125 newshw_mask = cas32(&hmeblkp->hblk_shw_mask, shw_mask, 3126 newshw_mask); 3127 } while (newshw_mask != shw_mask); 3128 3129 SFMMU_HASH_UNLOCK(hmebp); 3130 3131 return (hmeblkp); 3132 } 3133 3134 /* 3135 * This routine cleanup a previous shadow hmeblk and changes it to 3136 * a regular hblk. This happens rarely but it is possible 3137 * when a process wants to use large pages and there are hblks still 3138 * lying around from the previous as that used these hmeblks. 3139 * The alternative was to cleanup the shadow hblks at unload time 3140 * but since so few user processes actually use large pages, it is 3141 * better to be lazy and cleanup at this time. 3142 */ 3143 static void 3144 sfmmu_shadow_hcleanup(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, 3145 struct hmehash_bucket *hmebp) 3146 { 3147 caddr_t addr, endaddr; 3148 int hashno, size; 3149 3150 ASSERT(hmeblkp->hblk_shw_bit); 3151 3152 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 3153 3154 if (!hmeblkp->hblk_shw_mask) { 3155 hmeblkp->hblk_shw_bit = 0; 3156 return; 3157 } 3158 addr = (caddr_t)get_hblk_base(hmeblkp); 3159 endaddr = get_hblk_endaddr(hmeblkp); 3160 size = get_hblk_ttesz(hmeblkp); 3161 hashno = size - 1; 3162 ASSERT(hashno > 0); 3163 SFMMU_HASH_UNLOCK(hmebp); 3164 3165 sfmmu_free_hblks(sfmmup, addr, endaddr, hashno); 3166 3167 SFMMU_HASH_LOCK(hmebp); 3168 } 3169 3170 static void 3171 sfmmu_free_hblks(sfmmu_t *sfmmup, caddr_t addr, caddr_t endaddr, 3172 int hashno) 3173 { 3174 int hmeshift, shadow = 0; 3175 hmeblk_tag hblktag; 3176 struct hmehash_bucket *hmebp; 3177 struct hme_blk *hmeblkp; 3178 struct hme_blk *nx_hblk, *pr_hblk, *list = NULL; 3179 uint64_t hblkpa, prevpa, nx_pa; 3180 3181 ASSERT(hashno > 0); 3182 hblktag.htag_id = sfmmup; 3183 hblktag.htag_rehash = hashno; 3184 3185 hmeshift = HME_HASH_SHIFT(hashno); 3186 3187 while (addr < endaddr) { 3188 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 3189 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 3190 SFMMU_HASH_LOCK(hmebp); 3191 /* inline HME_HASH_SEARCH */ 3192 hmeblkp = hmebp->hmeblkp; 3193 hblkpa = hmebp->hmeh_nextpa; 3194 prevpa = 0; 3195 pr_hblk = NULL; 3196 while (hmeblkp) { 3197 ASSERT(hblkpa == va_to_pa((caddr_t)hmeblkp)); 3198 if (HTAGS_EQ(hmeblkp->hblk_tag, hblktag)) { 3199 /* found hme_blk */ 3200 if (hmeblkp->hblk_shw_bit) { 3201 if (hmeblkp->hblk_shw_mask) { 3202 shadow = 1; 3203 sfmmu_shadow_hcleanup(sfmmup, 3204 hmeblkp, hmebp); 3205 break; 3206 } else { 3207 hmeblkp->hblk_shw_bit = 0; 3208 } 3209 } 3210 3211 /* 3212 * Hblk_hmecnt and hblk_vcnt could be non zero 3213 * since hblk_unload() does not gurantee that. 3214 * 3215 * XXX - this could cause tteload() to spin 3216 * where sfmmu_shadow_hcleanup() is called. 3217 */ 3218 } 3219 3220 nx_hblk = hmeblkp->hblk_next; 3221 nx_pa = hmeblkp->hblk_nextpa; 3222 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) { 3223 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, 3224 pr_hblk); 3225 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 3226 } else { 3227 pr_hblk = hmeblkp; 3228 prevpa = hblkpa; 3229 } 3230 hmeblkp = nx_hblk; 3231 hblkpa = nx_pa; 3232 } 3233 3234 SFMMU_HASH_UNLOCK(hmebp); 3235 3236 if (shadow) { 3237 /* 3238 * We found another shadow hblk so cleaned its 3239 * children. We need to go back and cleanup 3240 * the original hblk so we don't change the 3241 * addr. 3242 */ 3243 shadow = 0; 3244 } else { 3245 addr = (caddr_t)roundup((uintptr_t)addr + 1, 3246 (1 << hmeshift)); 3247 } 3248 } 3249 sfmmu_hblks_list_purge(&list); 3250 } 3251 3252 /* 3253 * Release one hardware address translation lock on the given address range. 3254 */ 3255 void 3256 hat_unlock(struct hat *sfmmup, caddr_t addr, size_t len) 3257 { 3258 struct hmehash_bucket *hmebp; 3259 hmeblk_tag hblktag; 3260 int hmeshift, hashno = 1; 3261 struct hme_blk *hmeblkp, *list = NULL; 3262 caddr_t endaddr; 3263 3264 ASSERT(sfmmup != NULL); 3265 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 3266 3267 ASSERT((sfmmup == ksfmmup) || 3268 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 3269 ASSERT((len & MMU_PAGEOFFSET) == 0); 3270 endaddr = addr + len; 3271 hblktag.htag_id = sfmmup; 3272 3273 /* 3274 * Spitfire supports 4 page sizes. 3275 * Most pages are expected to be of the smallest page size (8K) and 3276 * these will not need to be rehashed. 64K pages also don't need to be 3277 * rehashed because an hmeblk spans 64K of address space. 512K pages 3278 * might need 1 rehash and and 4M pages might need 2 rehashes. 3279 */ 3280 while (addr < endaddr) { 3281 hmeshift = HME_HASH_SHIFT(hashno); 3282 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 3283 hblktag.htag_rehash = hashno; 3284 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 3285 3286 SFMMU_HASH_LOCK(hmebp); 3287 3288 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 3289 if (hmeblkp != NULL) { 3290 /* 3291 * If we encounter a shadow hmeblk then 3292 * we know there are no valid hmeblks mapping 3293 * this address at this size or larger. 3294 * Just increment address by the smallest 3295 * page size. 3296 */ 3297 if (hmeblkp->hblk_shw_bit) { 3298 addr += MMU_PAGESIZE; 3299 } else { 3300 addr = sfmmu_hblk_unlock(hmeblkp, addr, 3301 endaddr); 3302 } 3303 SFMMU_HASH_UNLOCK(hmebp); 3304 hashno = 1; 3305 continue; 3306 } 3307 SFMMU_HASH_UNLOCK(hmebp); 3308 3309 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 3310 /* 3311 * We have traversed the whole list and rehashed 3312 * if necessary without finding the address to unlock 3313 * which should never happen. 3314 */ 3315 panic("sfmmu_unlock: addr not found. " 3316 "addr %p hat %p", (void *)addr, (void *)sfmmup); 3317 } else { 3318 hashno++; 3319 } 3320 } 3321 3322 sfmmu_hblks_list_purge(&list); 3323 } 3324 3325 /* 3326 * Function to unlock a range of addresses in an hmeblk. It returns the 3327 * next address that needs to be unlocked. 3328 * Should be called with the hash lock held. 3329 */ 3330 static caddr_t 3331 sfmmu_hblk_unlock(struct hme_blk *hmeblkp, caddr_t addr, caddr_t endaddr) 3332 { 3333 struct sf_hment *sfhme; 3334 tte_t tteold, ttemod; 3335 int ttesz, ret; 3336 3337 ASSERT(in_hblk_range(hmeblkp, addr)); 3338 ASSERT(hmeblkp->hblk_shw_bit == 0); 3339 3340 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 3341 ttesz = get_hblk_ttesz(hmeblkp); 3342 3343 HBLKTOHME(sfhme, hmeblkp, addr); 3344 while (addr < endaddr) { 3345 readtte: 3346 sfmmu_copytte(&sfhme->hme_tte, &tteold); 3347 if (TTE_IS_VALID(&tteold)) { 3348 3349 ttemod = tteold; 3350 3351 ret = sfmmu_modifytte_try(&tteold, &ttemod, 3352 &sfhme->hme_tte); 3353 3354 if (ret < 0) 3355 goto readtte; 3356 3357 if (hmeblkp->hblk_lckcnt == 0) 3358 panic("zero hblk lckcnt"); 3359 3360 if (((uintptr_t)addr + TTEBYTES(ttesz)) > 3361 (uintptr_t)endaddr) 3362 panic("can't unlock large tte"); 3363 3364 ASSERT(hmeblkp->hblk_lckcnt > 0); 3365 atomic_add_16(&hmeblkp->hblk_lckcnt, -1); 3366 HBLK_STACK_TRACE(hmeblkp, HBLK_UNLOCK); 3367 } else { 3368 panic("sfmmu_hblk_unlock: invalid tte"); 3369 } 3370 addr += TTEBYTES(ttesz); 3371 sfhme++; 3372 } 3373 return (addr); 3374 } 3375 3376 /* 3377 * Physical Address Mapping Framework 3378 * 3379 * General rules: 3380 * 3381 * (1) Applies only to seg_kmem memory pages. To make things easier, 3382 * seg_kpm addresses are also accepted by the routines, but nothing 3383 * is done with them since by definition their PA mappings are static. 3384 * (2) hat_add_callback() may only be called while holding the page lock 3385 * SE_SHARED or SE_EXCL of the underlying page (e.g., as_pagelock()), 3386 * or passing HAC_PAGELOCK flag. 3387 * (3) prehandler() and posthandler() may not call hat_add_callback() or 3388 * hat_delete_callback(), nor should they allocate memory. Post quiesce 3389 * callbacks may not sleep or acquire adaptive mutex locks. 3390 * (4) Either prehandler() or posthandler() (but not both) may be specified 3391 * as being NULL. Specifying an errhandler() is optional. 3392 * 3393 * Details of using the framework: 3394 * 3395 * registering a callback (hat_register_callback()) 3396 * 3397 * Pass prehandler, posthandler, errhandler addresses 3398 * as described below. If capture_cpus argument is nonzero, 3399 * suspend callback to the prehandler will occur with CPUs 3400 * captured and executing xc_loop() and CPUs will remain 3401 * captured until after the posthandler suspend callback 3402 * occurs. 3403 * 3404 * adding a callback (hat_add_callback()) 3405 * 3406 * as_pagelock(); 3407 * hat_add_callback(); 3408 * save returned pfn in private data structures or program registers; 3409 * as_pageunlock(); 3410 * 3411 * prehandler() 3412 * 3413 * Stop all accesses by physical address to this memory page. 3414 * Called twice: the first, PRESUSPEND, is a context safe to acquire 3415 * adaptive locks. The second, SUSPEND, is called at high PIL with 3416 * CPUs captured so adaptive locks may NOT be acquired (and all spin 3417 * locks must be XCALL_PIL or higher locks). 3418 * 3419 * May return the following errors: 3420 * EIO: A fatal error has occurred. This will result in panic. 3421 * EAGAIN: The page cannot be suspended. This will fail the 3422 * relocation. 3423 * 0: Success. 3424 * 3425 * posthandler() 3426 * 3427 * Save new pfn in private data structures or program registers; 3428 * not allowed to fail (non-zero return values will result in panic). 3429 * 3430 * errhandler() 3431 * 3432 * called when an error occurs related to the callback. Currently 3433 * the only such error is HAT_CB_ERR_LEAKED which indicates that 3434 * a page is being freed, but there are still outstanding callback(s) 3435 * registered on the page. 3436 * 3437 * removing a callback (hat_delete_callback(); e.g., prior to freeing memory) 3438 * 3439 * stop using physical address 3440 * hat_delete_callback(); 3441 * 3442 */ 3443 3444 /* 3445 * Register a callback class. Each subsystem should do this once and 3446 * cache the id_t returned for use in setting up and tearing down callbacks. 3447 * 3448 * There is no facility for removing callback IDs once they are created; 3449 * the "key" should be unique for each module, so in case a module is unloaded 3450 * and subsequently re-loaded, we can recycle the module's previous entry. 3451 */ 3452 id_t 3453 hat_register_callback(int key, 3454 int (*prehandler)(caddr_t, uint_t, uint_t, void *), 3455 int (*posthandler)(caddr_t, uint_t, uint_t, void *, pfn_t), 3456 int (*errhandler)(caddr_t, uint_t, uint_t, void *), 3457 int capture_cpus) 3458 { 3459 id_t id; 3460 3461 /* 3462 * Search the table for a pre-existing callback associated with 3463 * the identifier "key". If one exists, we re-use that entry in 3464 * the table for this instance, otherwise we assign the next 3465 * available table slot. 3466 */ 3467 for (id = 0; id < sfmmu_max_cb_id; id++) { 3468 if (sfmmu_cb_table[id].key == key) 3469 break; 3470 } 3471 3472 if (id == sfmmu_max_cb_id) { 3473 id = sfmmu_cb_nextid++; 3474 if (id >= sfmmu_max_cb_id) 3475 panic("hat_register_callback: out of callback IDs"); 3476 } 3477 3478 ASSERT(prehandler != NULL || posthandler != NULL); 3479 3480 sfmmu_cb_table[id].key = key; 3481 sfmmu_cb_table[id].prehandler = prehandler; 3482 sfmmu_cb_table[id].posthandler = posthandler; 3483 sfmmu_cb_table[id].errhandler = errhandler; 3484 sfmmu_cb_table[id].capture_cpus = capture_cpus; 3485 3486 return (id); 3487 } 3488 3489 #define HAC_COOKIE_NONE (void *)-1 3490 3491 /* 3492 * Add relocation callbacks to the specified addr/len which will be called 3493 * when relocating the associated page. See the description of pre and 3494 * posthandler above for more details. 3495 * 3496 * If HAC_PAGELOCK is included in flags, the underlying memory page is 3497 * locked internally so the caller must be able to deal with the callback 3498 * running even before this function has returned. If HAC_PAGELOCK is not 3499 * set, it is assumed that the underlying memory pages are locked. 3500 * 3501 * Since the caller must track the individual page boundaries anyway, 3502 * we only allow a callback to be added to a single page (large 3503 * or small). Thus [addr, addr + len) MUST be contained within a single 3504 * page. 3505 * 3506 * Registering multiple callbacks on the same [addr, addr+len) is supported, 3507 * _provided_that_ a unique parameter is specified for each callback. 3508 * If multiple callbacks are registered on the same range the callback will 3509 * be invoked with each unique parameter. Registering the same callback with 3510 * the same argument more than once will result in corrupted kernel state. 3511 * 3512 * Returns the pfn of the underlying kernel page in *rpfn 3513 * on success, or PFN_INVALID on failure. 3514 * 3515 * cookiep (if passed) provides storage space for an opaque cookie 3516 * to return later to hat_delete_callback(). This cookie makes the callback 3517 * deletion significantly quicker by avoiding a potentially lengthy hash 3518 * search. 3519 * 3520 * Returns values: 3521 * 0: success 3522 * ENOMEM: memory allocation failure (e.g. flags was passed as HAC_NOSLEEP) 3523 * EINVAL: callback ID is not valid 3524 * ENXIO: ["vaddr", "vaddr" + len) is not mapped in the kernel's address 3525 * space 3526 * ERANGE: ["vaddr", "vaddr" + len) crosses a page boundary 3527 */ 3528 int 3529 hat_add_callback(id_t callback_id, caddr_t vaddr, uint_t len, uint_t flags, 3530 void *pvt, pfn_t *rpfn, void **cookiep) 3531 { 3532 struct hmehash_bucket *hmebp; 3533 hmeblk_tag hblktag; 3534 struct hme_blk *hmeblkp; 3535 int hmeshift, hashno; 3536 caddr_t saddr, eaddr, baseaddr; 3537 struct pa_hment *pahmep; 3538 struct sf_hment *sfhmep, *osfhmep; 3539 kmutex_t *pml; 3540 tte_t tte; 3541 page_t *pp; 3542 vnode_t *vp; 3543 u_offset_t off; 3544 pfn_t pfn; 3545 int kmflags = (flags & HAC_SLEEP)? KM_SLEEP : KM_NOSLEEP; 3546 int locked = 0; 3547 3548 /* 3549 * For KPM mappings, just return the physical address since we 3550 * don't need to register any callbacks. 3551 */ 3552 if (IS_KPM_ADDR(vaddr)) { 3553 uint64_t paddr; 3554 SFMMU_KPM_VTOP(vaddr, paddr); 3555 *rpfn = btop(paddr); 3556 if (cookiep != NULL) 3557 *cookiep = HAC_COOKIE_NONE; 3558 return (0); 3559 } 3560 3561 if (callback_id < (id_t)0 || callback_id >= sfmmu_cb_nextid) { 3562 *rpfn = PFN_INVALID; 3563 return (EINVAL); 3564 } 3565 3566 if ((pahmep = kmem_cache_alloc(pa_hment_cache, kmflags)) == NULL) { 3567 *rpfn = PFN_INVALID; 3568 return (ENOMEM); 3569 } 3570 3571 sfhmep = &pahmep->sfment; 3572 3573 saddr = (caddr_t)((uintptr_t)vaddr & MMU_PAGEMASK); 3574 eaddr = saddr + len; 3575 3576 rehash: 3577 /* Find the mapping(s) for this page */ 3578 for (hashno = TTE64K, hmeblkp = NULL; 3579 hmeblkp == NULL && hashno <= mmu_hashcnt; 3580 hashno++) { 3581 hmeshift = HME_HASH_SHIFT(hashno); 3582 hblktag.htag_id = ksfmmup; 3583 hblktag.htag_bspage = HME_HASH_BSPAGE(saddr, hmeshift); 3584 hblktag.htag_rehash = hashno; 3585 hmebp = HME_HASH_FUNCTION(ksfmmup, saddr, hmeshift); 3586 3587 SFMMU_HASH_LOCK(hmebp); 3588 3589 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 3590 3591 if (hmeblkp == NULL) 3592 SFMMU_HASH_UNLOCK(hmebp); 3593 } 3594 3595 if (hmeblkp == NULL) { 3596 kmem_cache_free(pa_hment_cache, pahmep); 3597 *rpfn = PFN_INVALID; 3598 return (ENXIO); 3599 } 3600 3601 HBLKTOHME(osfhmep, hmeblkp, saddr); 3602 sfmmu_copytte(&osfhmep->hme_tte, &tte); 3603 3604 if (!TTE_IS_VALID(&tte)) { 3605 SFMMU_HASH_UNLOCK(hmebp); 3606 kmem_cache_free(pa_hment_cache, pahmep); 3607 *rpfn = PFN_INVALID; 3608 return (ENXIO); 3609 } 3610 3611 /* 3612 * Make sure the boundaries for the callback fall within this 3613 * single mapping. 3614 */ 3615 baseaddr = (caddr_t)get_hblk_base(hmeblkp); 3616 ASSERT(saddr >= baseaddr); 3617 if (eaddr > saddr + TTEBYTES(TTE_CSZ(&tte))) { 3618 SFMMU_HASH_UNLOCK(hmebp); 3619 kmem_cache_free(pa_hment_cache, pahmep); 3620 *rpfn = PFN_INVALID; 3621 return (ERANGE); 3622 } 3623 3624 pfn = sfmmu_ttetopfn(&tte, vaddr); 3625 3626 /* 3627 * The pfn may not have a page_t underneath in which case we 3628 * just return it. This can happen if we are doing I/O to a 3629 * static portion of the kernel's address space, for instance. 3630 */ 3631 pp = osfhmep->hme_page; 3632 if (pp == NULL) { 3633 SFMMU_HASH_UNLOCK(hmebp); 3634 kmem_cache_free(pa_hment_cache, pahmep); 3635 *rpfn = pfn; 3636 if (cookiep) 3637 *cookiep = HAC_COOKIE_NONE; 3638 return (0); 3639 } 3640 ASSERT(pp == PP_PAGEROOT(pp)); 3641 3642 vp = pp->p_vnode; 3643 off = pp->p_offset; 3644 3645 pml = sfmmu_mlist_enter(pp); 3646 3647 if (flags & HAC_PAGELOCK) { 3648 if (!page_trylock(pp, SE_SHARED)) { 3649 /* 3650 * Somebody is holding SE_EXCL lock. Might 3651 * even be hat_page_relocate(). Drop all 3652 * our locks, lookup the page in &kvp, and 3653 * retry. If it doesn't exist in &kvp, then 3654 * we must be dealing with a kernel mapped 3655 * page which doesn't actually belong to 3656 * segkmem so we punt. 3657 */ 3658 sfmmu_mlist_exit(pml); 3659 SFMMU_HASH_UNLOCK(hmebp); 3660 pp = page_lookup(&kvp, (u_offset_t)saddr, SE_SHARED); 3661 if (pp == NULL) { 3662 kmem_cache_free(pa_hment_cache, pahmep); 3663 *rpfn = pfn; 3664 if (cookiep) 3665 *cookiep = HAC_COOKIE_NONE; 3666 return (0); 3667 } 3668 page_unlock(pp); 3669 goto rehash; 3670 } 3671 locked = 1; 3672 } 3673 3674 if (!PAGE_LOCKED(pp) && !panicstr) 3675 panic("hat_add_callback: page 0x%p not locked", pp); 3676 3677 if (osfhmep->hme_page != pp || pp->p_vnode != vp || 3678 pp->p_offset != off) { 3679 /* 3680 * The page moved before we got our hands on it. Drop 3681 * all the locks and try again. 3682 */ 3683 ASSERT((flags & HAC_PAGELOCK) != 0); 3684 sfmmu_mlist_exit(pml); 3685 SFMMU_HASH_UNLOCK(hmebp); 3686 page_unlock(pp); 3687 locked = 0; 3688 goto rehash; 3689 } 3690 3691 if (vp != &kvp) { 3692 /* 3693 * This is not a segkmem page but another page which 3694 * has been kernel mapped. It had better have at least 3695 * a share lock on it. Return the pfn. 3696 */ 3697 sfmmu_mlist_exit(pml); 3698 SFMMU_HASH_UNLOCK(hmebp); 3699 if (locked) 3700 page_unlock(pp); 3701 kmem_cache_free(pa_hment_cache, pahmep); 3702 ASSERT(PAGE_LOCKED(pp)); 3703 *rpfn = pfn; 3704 if (cookiep) 3705 *cookiep = HAC_COOKIE_NONE; 3706 return (0); 3707 } 3708 3709 /* 3710 * Setup this pa_hment and link its embedded dummy sf_hment into 3711 * the mapping list. 3712 */ 3713 pp->p_share++; 3714 pahmep->cb_id = callback_id; 3715 pahmep->addr = vaddr; 3716 pahmep->len = len; 3717 pahmep->refcnt = 1; 3718 pahmep->flags = 0; 3719 pahmep->pvt = pvt; 3720 3721 sfhmep->hme_tte.ll = 0; 3722 sfhmep->hme_data = pahmep; 3723 sfhmep->hme_prev = osfhmep; 3724 sfhmep->hme_next = osfhmep->hme_next; 3725 3726 if (osfhmep->hme_next) 3727 osfhmep->hme_next->hme_prev = sfhmep; 3728 3729 osfhmep->hme_next = sfhmep; 3730 3731 sfmmu_mlist_exit(pml); 3732 SFMMU_HASH_UNLOCK(hmebp); 3733 3734 if (locked) 3735 page_unlock(pp); 3736 3737 *rpfn = pfn; 3738 if (cookiep) 3739 *cookiep = (void *)pahmep; 3740 3741 return (0); 3742 } 3743 3744 /* 3745 * Remove the relocation callbacks from the specified addr/len. 3746 */ 3747 void 3748 hat_delete_callback(caddr_t vaddr, uint_t len, void *pvt, uint_t flags, 3749 void *cookie) 3750 { 3751 struct hmehash_bucket *hmebp; 3752 hmeblk_tag hblktag; 3753 struct hme_blk *hmeblkp; 3754 int hmeshift, hashno; 3755 caddr_t saddr; 3756 struct pa_hment *pahmep; 3757 struct sf_hment *sfhmep, *osfhmep; 3758 kmutex_t *pml; 3759 tte_t tte; 3760 page_t *pp; 3761 vnode_t *vp; 3762 u_offset_t off; 3763 int locked = 0; 3764 3765 /* 3766 * If the cookie is HAC_COOKIE_NONE then there is no pa_hment to 3767 * remove so just return. 3768 */ 3769 if (cookie == HAC_COOKIE_NONE || IS_KPM_ADDR(vaddr)) 3770 return; 3771 3772 saddr = (caddr_t)((uintptr_t)vaddr & MMU_PAGEMASK); 3773 3774 rehash: 3775 /* Find the mapping(s) for this page */ 3776 for (hashno = TTE64K, hmeblkp = NULL; 3777 hmeblkp == NULL && hashno <= mmu_hashcnt; 3778 hashno++) { 3779 hmeshift = HME_HASH_SHIFT(hashno); 3780 hblktag.htag_id = ksfmmup; 3781 hblktag.htag_bspage = HME_HASH_BSPAGE(saddr, hmeshift); 3782 hblktag.htag_rehash = hashno; 3783 hmebp = HME_HASH_FUNCTION(ksfmmup, saddr, hmeshift); 3784 3785 SFMMU_HASH_LOCK(hmebp); 3786 3787 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 3788 3789 if (hmeblkp == NULL) 3790 SFMMU_HASH_UNLOCK(hmebp); 3791 } 3792 3793 if (hmeblkp == NULL) 3794 return; 3795 3796 HBLKTOHME(osfhmep, hmeblkp, saddr); 3797 3798 sfmmu_copytte(&osfhmep->hme_tte, &tte); 3799 if (!TTE_IS_VALID(&tte)) { 3800 SFMMU_HASH_UNLOCK(hmebp); 3801 return; 3802 } 3803 3804 pp = osfhmep->hme_page; 3805 if (pp == NULL) { 3806 SFMMU_HASH_UNLOCK(hmebp); 3807 ASSERT(cookie == NULL); 3808 return; 3809 } 3810 3811 vp = pp->p_vnode; 3812 off = pp->p_offset; 3813 3814 pml = sfmmu_mlist_enter(pp); 3815 3816 if (flags & HAC_PAGELOCK) { 3817 if (!page_trylock(pp, SE_SHARED)) { 3818 /* 3819 * Somebody is holding SE_EXCL lock. Might 3820 * even be hat_page_relocate(). Drop all 3821 * our locks, lookup the page in &kvp, and 3822 * retry. If it doesn't exist in &kvp, then 3823 * we must be dealing with a kernel mapped 3824 * page which doesn't actually belong to 3825 * segkmem so we punt. 3826 */ 3827 sfmmu_mlist_exit(pml); 3828 SFMMU_HASH_UNLOCK(hmebp); 3829 pp = page_lookup(&kvp, (u_offset_t)saddr, SE_SHARED); 3830 if (pp == NULL) { 3831 ASSERT(cookie == NULL); 3832 return; 3833 } 3834 page_unlock(pp); 3835 goto rehash; 3836 } 3837 locked = 1; 3838 } 3839 3840 ASSERT(PAGE_LOCKED(pp)); 3841 3842 if (osfhmep->hme_page != pp || pp->p_vnode != vp || 3843 pp->p_offset != off) { 3844 /* 3845 * The page moved before we got our hands on it. Drop 3846 * all the locks and try again. 3847 */ 3848 ASSERT((flags & HAC_PAGELOCK) != 0); 3849 sfmmu_mlist_exit(pml); 3850 SFMMU_HASH_UNLOCK(hmebp); 3851 page_unlock(pp); 3852 locked = 0; 3853 goto rehash; 3854 } 3855 3856 if (vp != &kvp) { 3857 /* 3858 * This is not a segkmem page but another page which 3859 * has been kernel mapped. 3860 */ 3861 sfmmu_mlist_exit(pml); 3862 SFMMU_HASH_UNLOCK(hmebp); 3863 if (locked) 3864 page_unlock(pp); 3865 ASSERT(cookie == NULL); 3866 return; 3867 } 3868 3869 if (cookie != NULL) { 3870 pahmep = (struct pa_hment *)cookie; 3871 sfhmep = &pahmep->sfment; 3872 } else { 3873 for (sfhmep = pp->p_mapping; sfhmep != NULL; 3874 sfhmep = sfhmep->hme_next) { 3875 3876 /* 3877 * skip va<->pa mappings 3878 */ 3879 if (!IS_PAHME(sfhmep)) 3880 continue; 3881 3882 pahmep = sfhmep->hme_data; 3883 ASSERT(pahmep != NULL); 3884 3885 /* 3886 * if pa_hment matches, remove it 3887 */ 3888 if ((pahmep->pvt == pvt) && 3889 (pahmep->addr == vaddr) && 3890 (pahmep->len == len)) { 3891 break; 3892 } 3893 } 3894 } 3895 3896 if (sfhmep == NULL) { 3897 if (!panicstr) { 3898 panic("hat_delete_callback: pa_hment not found, pp %p", 3899 (void *)pp); 3900 } 3901 return; 3902 } 3903 3904 /* 3905 * Note: at this point a valid kernel mapping must still be 3906 * present on this page. 3907 */ 3908 pp->p_share--; 3909 if (pp->p_share <= 0) 3910 panic("hat_delete_callback: zero p_share"); 3911 3912 if (--pahmep->refcnt == 0) { 3913 if (pahmep->flags != 0) 3914 panic("hat_delete_callback: pa_hment is busy"); 3915 3916 /* 3917 * Remove sfhmep from the mapping list for the page. 3918 */ 3919 if (sfhmep->hme_prev) { 3920 sfhmep->hme_prev->hme_next = sfhmep->hme_next; 3921 } else { 3922 pp->p_mapping = sfhmep->hme_next; 3923 } 3924 3925 if (sfhmep->hme_next) 3926 sfhmep->hme_next->hme_prev = sfhmep->hme_prev; 3927 3928 sfmmu_mlist_exit(pml); 3929 SFMMU_HASH_UNLOCK(hmebp); 3930 3931 if (locked) 3932 page_unlock(pp); 3933 3934 kmem_cache_free(pa_hment_cache, pahmep); 3935 return; 3936 } 3937 3938 sfmmu_mlist_exit(pml); 3939 SFMMU_HASH_UNLOCK(hmebp); 3940 if (locked) 3941 page_unlock(pp); 3942 } 3943 3944 /* 3945 * hat_probe returns 1 if the translation for the address 'addr' is 3946 * loaded, zero otherwise. 3947 * 3948 * hat_probe should be used only for advisorary purposes because it may 3949 * occasionally return the wrong value. The implementation must guarantee that 3950 * returning the wrong value is a very rare event. hat_probe is used 3951 * to implement optimizations in the segment drivers. 3952 * 3953 */ 3954 int 3955 hat_probe(struct hat *sfmmup, caddr_t addr) 3956 { 3957 pfn_t pfn; 3958 tte_t tte; 3959 3960 ASSERT(sfmmup != NULL); 3961 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 3962 3963 ASSERT((sfmmup == ksfmmup) || 3964 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 3965 3966 if (sfmmup == ksfmmup) { 3967 while ((pfn = sfmmu_vatopfn(addr, sfmmup, &tte)) 3968 == PFN_SUSPENDED) { 3969 sfmmu_vatopfn_suspended(addr, sfmmup, &tte); 3970 } 3971 } else { 3972 pfn = sfmmu_uvatopfn(addr, sfmmup); 3973 } 3974 3975 if (pfn != PFN_INVALID) 3976 return (1); 3977 else 3978 return (0); 3979 } 3980 3981 ssize_t 3982 hat_getpagesize(struct hat *sfmmup, caddr_t addr) 3983 { 3984 tte_t tte; 3985 3986 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 3987 3988 sfmmu_gettte(sfmmup, addr, &tte); 3989 if (TTE_IS_VALID(&tte)) { 3990 return (TTEBYTES(TTE_CSZ(&tte))); 3991 } 3992 return (-1); 3993 } 3994 3995 static void 3996 sfmmu_gettte(struct hat *sfmmup, caddr_t addr, tte_t *ttep) 3997 { 3998 struct hmehash_bucket *hmebp; 3999 hmeblk_tag hblktag; 4000 int hmeshift, hashno = 1; 4001 struct hme_blk *hmeblkp, *list = NULL; 4002 struct sf_hment *sfhmep; 4003 4004 /* support for ISM */ 4005 ism_map_t *ism_map; 4006 ism_blk_t *ism_blkp; 4007 int i; 4008 sfmmu_t *ism_hatid = NULL; 4009 sfmmu_t *locked_hatid = NULL; 4010 4011 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET)); 4012 4013 ism_blkp = sfmmup->sfmmu_iblk; 4014 if (ism_blkp) { 4015 sfmmu_ismhat_enter(sfmmup, 0); 4016 locked_hatid = sfmmup; 4017 } 4018 while (ism_blkp && ism_hatid == NULL) { 4019 ism_map = ism_blkp->iblk_maps; 4020 for (i = 0; ism_map[i].imap_ismhat && i < ISM_MAP_SLOTS; i++) { 4021 if (addr >= ism_start(ism_map[i]) && 4022 addr < ism_end(ism_map[i])) { 4023 sfmmup = ism_hatid = ism_map[i].imap_ismhat; 4024 addr = (caddr_t)(addr - 4025 ism_start(ism_map[i])); 4026 break; 4027 } 4028 } 4029 ism_blkp = ism_blkp->iblk_next; 4030 } 4031 if (locked_hatid) { 4032 sfmmu_ismhat_exit(locked_hatid, 0); 4033 } 4034 4035 hblktag.htag_id = sfmmup; 4036 ttep->ll = 0; 4037 4038 do { 4039 hmeshift = HME_HASH_SHIFT(hashno); 4040 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 4041 hblktag.htag_rehash = hashno; 4042 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 4043 4044 SFMMU_HASH_LOCK(hmebp); 4045 4046 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 4047 if (hmeblkp != NULL) { 4048 HBLKTOHME(sfhmep, hmeblkp, addr); 4049 sfmmu_copytte(&sfhmep->hme_tte, ttep); 4050 SFMMU_HASH_UNLOCK(hmebp); 4051 break; 4052 } 4053 SFMMU_HASH_UNLOCK(hmebp); 4054 hashno++; 4055 } while (HME_REHASH(sfmmup) && (hashno <= mmu_hashcnt)); 4056 4057 sfmmu_hblks_list_purge(&list); 4058 } 4059 4060 uint_t 4061 hat_getattr(struct hat *sfmmup, caddr_t addr, uint_t *attr) 4062 { 4063 tte_t tte; 4064 4065 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 4066 4067 sfmmu_gettte(sfmmup, addr, &tte); 4068 if (TTE_IS_VALID(&tte)) { 4069 *attr = sfmmu_ptov_attr(&tte); 4070 return (0); 4071 } 4072 *attr = 0; 4073 return ((uint_t)0xffffffff); 4074 } 4075 4076 /* 4077 * Enables more attributes on specified address range (ie. logical OR) 4078 */ 4079 void 4080 hat_setattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr) 4081 { 4082 if (hat->sfmmu_xhat_provider) { 4083 XHAT_SETATTR(hat, addr, len, attr); 4084 return; 4085 } else { 4086 /* 4087 * This must be a CPU HAT. If the address space has 4088 * XHATs attached, change attributes for all of them, 4089 * just in case 4090 */ 4091 ASSERT(hat->sfmmu_as != NULL); 4092 if (hat->sfmmu_as->a_xhat != NULL) 4093 xhat_setattr_all(hat->sfmmu_as, addr, len, attr); 4094 } 4095 4096 sfmmu_chgattr(hat, addr, len, attr, SFMMU_SETATTR); 4097 } 4098 4099 /* 4100 * Assigns attributes to the specified address range. All the attributes 4101 * are specified. 4102 */ 4103 void 4104 hat_chgattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr) 4105 { 4106 if (hat->sfmmu_xhat_provider) { 4107 XHAT_CHGATTR(hat, addr, len, attr); 4108 return; 4109 } else { 4110 /* 4111 * This must be a CPU HAT. If the address space has 4112 * XHATs attached, change attributes for all of them, 4113 * just in case 4114 */ 4115 ASSERT(hat->sfmmu_as != NULL); 4116 if (hat->sfmmu_as->a_xhat != NULL) 4117 xhat_chgattr_all(hat->sfmmu_as, addr, len, attr); 4118 } 4119 4120 sfmmu_chgattr(hat, addr, len, attr, SFMMU_CHGATTR); 4121 } 4122 4123 /* 4124 * Remove attributes on the specified address range (ie. loginal NAND) 4125 */ 4126 void 4127 hat_clrattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr) 4128 { 4129 if (hat->sfmmu_xhat_provider) { 4130 XHAT_CLRATTR(hat, addr, len, attr); 4131 return; 4132 } else { 4133 /* 4134 * This must be a CPU HAT. If the address space has 4135 * XHATs attached, change attributes for all of them, 4136 * just in case 4137 */ 4138 ASSERT(hat->sfmmu_as != NULL); 4139 if (hat->sfmmu_as->a_xhat != NULL) 4140 xhat_clrattr_all(hat->sfmmu_as, addr, len, attr); 4141 } 4142 4143 sfmmu_chgattr(hat, addr, len, attr, SFMMU_CLRATTR); 4144 } 4145 4146 /* 4147 * Change attributes on an address range to that specified by attr and mode. 4148 */ 4149 static void 4150 sfmmu_chgattr(struct hat *sfmmup, caddr_t addr, size_t len, uint_t attr, 4151 int mode) 4152 { 4153 struct hmehash_bucket *hmebp; 4154 hmeblk_tag hblktag; 4155 int hmeshift, hashno = 1; 4156 struct hme_blk *hmeblkp, *list = NULL; 4157 caddr_t endaddr; 4158 cpuset_t cpuset; 4159 demap_range_t dmr; 4160 4161 CPUSET_ZERO(cpuset); 4162 4163 ASSERT((sfmmup == ksfmmup) || 4164 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 4165 ASSERT((len & MMU_PAGEOFFSET) == 0); 4166 ASSERT(((uintptr_t)addr & MMU_PAGEOFFSET) == 0); 4167 4168 if ((attr & PROT_USER) && (mode != SFMMU_CLRATTR) && 4169 ((addr + len) > (caddr_t)USERLIMIT)) { 4170 panic("user addr %p in kernel space", 4171 (void *)addr); 4172 } 4173 4174 endaddr = addr + len; 4175 hblktag.htag_id = sfmmup; 4176 DEMAP_RANGE_INIT(sfmmup, &dmr); 4177 4178 while (addr < endaddr) { 4179 hmeshift = HME_HASH_SHIFT(hashno); 4180 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 4181 hblktag.htag_rehash = hashno; 4182 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 4183 4184 SFMMU_HASH_LOCK(hmebp); 4185 4186 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 4187 if (hmeblkp != NULL) { 4188 /* 4189 * We've encountered a shadow hmeblk so skip the range 4190 * of the next smaller mapping size. 4191 */ 4192 if (hmeblkp->hblk_shw_bit) { 4193 ASSERT(sfmmup != ksfmmup); 4194 ASSERT(hashno > 1); 4195 addr = (caddr_t)P2END((uintptr_t)addr, 4196 TTEBYTES(hashno - 1)); 4197 } else { 4198 addr = sfmmu_hblk_chgattr(sfmmup, 4199 hmeblkp, addr, endaddr, &dmr, attr, mode); 4200 } 4201 SFMMU_HASH_UNLOCK(hmebp); 4202 hashno = 1; 4203 continue; 4204 } 4205 SFMMU_HASH_UNLOCK(hmebp); 4206 4207 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 4208 /* 4209 * We have traversed the whole list and rehashed 4210 * if necessary without finding the address to chgattr. 4211 * This is ok, so we increment the address by the 4212 * smallest hmeblk range for kernel mappings or for 4213 * user mappings with no large pages, and the largest 4214 * hmeblk range, to account for shadow hmeblks, for 4215 * user mappings with large pages and continue. 4216 */ 4217 if (sfmmup == ksfmmup) 4218 addr = (caddr_t)P2END((uintptr_t)addr, 4219 TTEBYTES(1)); 4220 else 4221 addr = (caddr_t)P2END((uintptr_t)addr, 4222 TTEBYTES(hashno)); 4223 hashno = 1; 4224 } else { 4225 hashno++; 4226 } 4227 } 4228 4229 sfmmu_hblks_list_purge(&list); 4230 DEMAP_RANGE_FLUSH(&dmr); 4231 cpuset = sfmmup->sfmmu_cpusran; 4232 xt_sync(cpuset); 4233 } 4234 4235 /* 4236 * This function chgattr on a range of addresses in an hmeblk. It returns the 4237 * next addres that needs to be chgattr. 4238 * It should be called with the hash lock held. 4239 * XXX It should be possible to optimize chgattr by not flushing every time but 4240 * on the other hand: 4241 * 1. do one flush crosscall. 4242 * 2. only flush if we are increasing permissions (make sure this will work) 4243 */ 4244 static caddr_t 4245 sfmmu_hblk_chgattr(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 4246 caddr_t endaddr, demap_range_t *dmrp, uint_t attr, int mode) 4247 { 4248 tte_t tte, tteattr, tteflags, ttemod; 4249 struct sf_hment *sfhmep; 4250 int ttesz; 4251 struct page *pp = NULL; 4252 kmutex_t *pml, *pmtx; 4253 int ret; 4254 int use_demap_range; 4255 #if defined(SF_ERRATA_57) 4256 int check_exec; 4257 #endif 4258 4259 ASSERT(in_hblk_range(hmeblkp, addr)); 4260 ASSERT(hmeblkp->hblk_shw_bit == 0); 4261 4262 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 4263 ttesz = get_hblk_ttesz(hmeblkp); 4264 4265 /* 4266 * Flush the current demap region if addresses have been 4267 * skipped or the page size doesn't match. 4268 */ 4269 use_demap_range = (TTEBYTES(ttesz) == DEMAP_RANGE_PGSZ(dmrp)); 4270 if (use_demap_range) { 4271 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr); 4272 } else { 4273 DEMAP_RANGE_FLUSH(dmrp); 4274 } 4275 4276 tteattr.ll = sfmmu_vtop_attr(attr, mode, &tteflags); 4277 #if defined(SF_ERRATA_57) 4278 check_exec = (sfmmup != ksfmmup) && 4279 AS_TYPE_64BIT(sfmmup->sfmmu_as) && 4280 TTE_IS_EXECUTABLE(&tteattr); 4281 #endif 4282 HBLKTOHME(sfhmep, hmeblkp, addr); 4283 while (addr < endaddr) { 4284 sfmmu_copytte(&sfhmep->hme_tte, &tte); 4285 if (TTE_IS_VALID(&tte)) { 4286 if ((tte.ll & tteflags.ll) == tteattr.ll) { 4287 /* 4288 * if the new attr is the same as old 4289 * continue 4290 */ 4291 goto next_addr; 4292 } 4293 if (!TTE_IS_WRITABLE(&tteattr)) { 4294 /* 4295 * make sure we clear hw modify bit if we 4296 * removing write protections 4297 */ 4298 tteflags.tte_intlo |= TTE_HWWR_INT; 4299 } 4300 4301 pml = NULL; 4302 pp = sfhmep->hme_page; 4303 if (pp) { 4304 pml = sfmmu_mlist_enter(pp); 4305 } 4306 4307 if (pp != sfhmep->hme_page) { 4308 /* 4309 * tte must have been unloaded. 4310 */ 4311 ASSERT(pml); 4312 sfmmu_mlist_exit(pml); 4313 continue; 4314 } 4315 4316 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 4317 4318 ttemod = tte; 4319 ttemod.ll = (ttemod.ll & ~tteflags.ll) | tteattr.ll; 4320 ASSERT(TTE_TO_TTEPFN(&ttemod) == TTE_TO_TTEPFN(&tte)); 4321 4322 #if defined(SF_ERRATA_57) 4323 if (check_exec && addr < errata57_limit) 4324 ttemod.tte_exec_perm = 0; 4325 #endif 4326 ret = sfmmu_modifytte_try(&tte, &ttemod, 4327 &sfhmep->hme_tte); 4328 4329 if (ret < 0) { 4330 /* tte changed underneath us */ 4331 if (pml) { 4332 sfmmu_mlist_exit(pml); 4333 } 4334 continue; 4335 } 4336 4337 if (tteflags.tte_intlo & TTE_HWWR_INT) { 4338 /* 4339 * need to sync if we are clearing modify bit. 4340 */ 4341 sfmmu_ttesync(sfmmup, addr, &tte, pp); 4342 } 4343 4344 if (pp && PP_ISRO(pp)) { 4345 if (tteattr.tte_intlo & TTE_WRPRM_INT) { 4346 pmtx = sfmmu_page_enter(pp); 4347 PP_CLRRO(pp); 4348 sfmmu_page_exit(pmtx); 4349 } 4350 } 4351 4352 if (ret > 0 && use_demap_range) { 4353 DEMAP_RANGE_MARKPG(dmrp, addr); 4354 } else if (ret > 0) { 4355 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 4356 } 4357 4358 if (pml) { 4359 sfmmu_mlist_exit(pml); 4360 } 4361 } 4362 next_addr: 4363 addr += TTEBYTES(ttesz); 4364 sfhmep++; 4365 DEMAP_RANGE_NEXTPG(dmrp); 4366 } 4367 return (addr); 4368 } 4369 4370 /* 4371 * This routine converts virtual attributes to physical ones. It will 4372 * update the tteflags field with the tte mask corresponding to the attributes 4373 * affected and it returns the new attributes. It will also clear the modify 4374 * bit if we are taking away write permission. This is necessary since the 4375 * modify bit is the hardware permission bit and we need to clear it in order 4376 * to detect write faults. 4377 */ 4378 static uint64_t 4379 sfmmu_vtop_attr(uint_t attr, int mode, tte_t *ttemaskp) 4380 { 4381 tte_t ttevalue; 4382 4383 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR)); 4384 4385 switch (mode) { 4386 case SFMMU_CHGATTR: 4387 /* all attributes specified */ 4388 ttevalue.tte_inthi = MAKE_TTEATTR_INTHI(attr); 4389 ttevalue.tte_intlo = MAKE_TTEATTR_INTLO(attr); 4390 ttemaskp->tte_inthi = TTEINTHI_ATTR; 4391 ttemaskp->tte_intlo = TTEINTLO_ATTR; 4392 break; 4393 case SFMMU_SETATTR: 4394 ASSERT(!(attr & ~HAT_PROT_MASK)); 4395 ttemaskp->ll = 0; 4396 ttevalue.ll = 0; 4397 /* 4398 * a valid tte implies exec and read for sfmmu 4399 * so no need to do anything about them. 4400 * since priviledged access implies user access 4401 * PROT_USER doesn't make sense either. 4402 */ 4403 if (attr & PROT_WRITE) { 4404 ttemaskp->tte_intlo |= TTE_WRPRM_INT; 4405 ttevalue.tte_intlo |= TTE_WRPRM_INT; 4406 } 4407 break; 4408 case SFMMU_CLRATTR: 4409 /* attributes will be nand with current ones */ 4410 if (attr & ~(PROT_WRITE | PROT_USER)) { 4411 panic("sfmmu: attr %x not supported", attr); 4412 } 4413 ttemaskp->ll = 0; 4414 ttevalue.ll = 0; 4415 if (attr & PROT_WRITE) { 4416 /* clear both writable and modify bit */ 4417 ttemaskp->tte_intlo |= TTE_WRPRM_INT | TTE_HWWR_INT; 4418 } 4419 if (attr & PROT_USER) { 4420 ttemaskp->tte_intlo |= TTE_PRIV_INT; 4421 ttevalue.tte_intlo |= TTE_PRIV_INT; 4422 } 4423 break; 4424 default: 4425 panic("sfmmu_vtop_attr: bad mode %x", mode); 4426 } 4427 ASSERT(TTE_TO_TTEPFN(&ttevalue) == 0); 4428 return (ttevalue.ll); 4429 } 4430 4431 static uint_t 4432 sfmmu_ptov_attr(tte_t *ttep) 4433 { 4434 uint_t attr; 4435 4436 ASSERT(TTE_IS_VALID(ttep)); 4437 4438 attr = PROT_READ; 4439 4440 if (TTE_IS_WRITABLE(ttep)) { 4441 attr |= PROT_WRITE; 4442 } 4443 if (TTE_IS_EXECUTABLE(ttep)) { 4444 attr |= PROT_EXEC; 4445 } 4446 if (!TTE_IS_PRIVILEGED(ttep)) { 4447 attr |= PROT_USER; 4448 } 4449 if (TTE_IS_NFO(ttep)) { 4450 attr |= HAT_NOFAULT; 4451 } 4452 if (TTE_IS_NOSYNC(ttep)) { 4453 attr |= HAT_NOSYNC; 4454 } 4455 if (TTE_IS_SIDEFFECT(ttep)) { 4456 attr |= SFMMU_SIDEFFECT; 4457 } 4458 if (!TTE_IS_VCACHEABLE(ttep)) { 4459 attr |= SFMMU_UNCACHEVTTE; 4460 } 4461 if (!TTE_IS_PCACHEABLE(ttep)) { 4462 attr |= SFMMU_UNCACHEPTTE; 4463 } 4464 return (attr); 4465 } 4466 4467 /* 4468 * hat_chgprot is a deprecated hat call. New segment drivers 4469 * should store all attributes and use hat_*attr calls. 4470 * 4471 * Change the protections in the virtual address range 4472 * given to the specified virtual protection. If vprot is ~PROT_WRITE, 4473 * then remove write permission, leaving the other 4474 * permissions unchanged. If vprot is ~PROT_USER, remove user permissions. 4475 * 4476 */ 4477 void 4478 hat_chgprot(struct hat *sfmmup, caddr_t addr, size_t len, uint_t vprot) 4479 { 4480 struct hmehash_bucket *hmebp; 4481 hmeblk_tag hblktag; 4482 int hmeshift, hashno = 1; 4483 struct hme_blk *hmeblkp, *list = NULL; 4484 caddr_t endaddr; 4485 cpuset_t cpuset; 4486 demap_range_t dmr; 4487 4488 ASSERT((len & MMU_PAGEOFFSET) == 0); 4489 ASSERT(((uintptr_t)addr & MMU_PAGEOFFSET) == 0); 4490 4491 if (sfmmup->sfmmu_xhat_provider) { 4492 XHAT_CHGPROT(sfmmup, addr, len, vprot); 4493 return; 4494 } else { 4495 /* 4496 * This must be a CPU HAT. If the address space has 4497 * XHATs attached, change attributes for all of them, 4498 * just in case 4499 */ 4500 ASSERT(sfmmup->sfmmu_as != NULL); 4501 if (sfmmup->sfmmu_as->a_xhat != NULL) 4502 xhat_chgprot_all(sfmmup->sfmmu_as, addr, len, vprot); 4503 } 4504 4505 CPUSET_ZERO(cpuset); 4506 4507 if ((vprot != (uint_t)~PROT_WRITE) && (vprot & PROT_USER) && 4508 ((addr + len) > (caddr_t)USERLIMIT)) { 4509 panic("user addr %p vprot %x in kernel space", 4510 (void *)addr, vprot); 4511 } 4512 endaddr = addr + len; 4513 hblktag.htag_id = sfmmup; 4514 DEMAP_RANGE_INIT(sfmmup, &dmr); 4515 4516 while (addr < endaddr) { 4517 hmeshift = HME_HASH_SHIFT(hashno); 4518 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 4519 hblktag.htag_rehash = hashno; 4520 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 4521 4522 SFMMU_HASH_LOCK(hmebp); 4523 4524 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 4525 if (hmeblkp != NULL) { 4526 /* 4527 * We've encountered a shadow hmeblk so skip the range 4528 * of the next smaller mapping size. 4529 */ 4530 if (hmeblkp->hblk_shw_bit) { 4531 ASSERT(sfmmup != ksfmmup); 4532 ASSERT(hashno > 1); 4533 addr = (caddr_t)P2END((uintptr_t)addr, 4534 TTEBYTES(hashno - 1)); 4535 } else { 4536 addr = sfmmu_hblk_chgprot(sfmmup, hmeblkp, 4537 addr, endaddr, &dmr, vprot); 4538 } 4539 SFMMU_HASH_UNLOCK(hmebp); 4540 hashno = 1; 4541 continue; 4542 } 4543 SFMMU_HASH_UNLOCK(hmebp); 4544 4545 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 4546 /* 4547 * We have traversed the whole list and rehashed 4548 * if necessary without finding the address to chgprot. 4549 * This is ok so we increment the address by the 4550 * smallest hmeblk range for kernel mappings and the 4551 * largest hmeblk range, to account for shadow hmeblks, 4552 * for user mappings and continue. 4553 */ 4554 if (sfmmup == ksfmmup) 4555 addr = (caddr_t)P2END((uintptr_t)addr, 4556 TTEBYTES(1)); 4557 else 4558 addr = (caddr_t)P2END((uintptr_t)addr, 4559 TTEBYTES(hashno)); 4560 hashno = 1; 4561 } else { 4562 hashno++; 4563 } 4564 } 4565 4566 sfmmu_hblks_list_purge(&list); 4567 DEMAP_RANGE_FLUSH(&dmr); 4568 cpuset = sfmmup->sfmmu_cpusran; 4569 xt_sync(cpuset); 4570 } 4571 4572 /* 4573 * This function chgprots a range of addresses in an hmeblk. It returns the 4574 * next addres that needs to be chgprot. 4575 * It should be called with the hash lock held. 4576 * XXX It shold be possible to optimize chgprot by not flushing every time but 4577 * on the other hand: 4578 * 1. do one flush crosscall. 4579 * 2. only flush if we are increasing permissions (make sure this will work) 4580 */ 4581 static caddr_t 4582 sfmmu_hblk_chgprot(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 4583 caddr_t endaddr, demap_range_t *dmrp, uint_t vprot) 4584 { 4585 uint_t pprot; 4586 tte_t tte, ttemod; 4587 struct sf_hment *sfhmep; 4588 uint_t tteflags; 4589 int ttesz; 4590 struct page *pp = NULL; 4591 kmutex_t *pml, *pmtx; 4592 int ret; 4593 int use_demap_range; 4594 #if defined(SF_ERRATA_57) 4595 int check_exec; 4596 #endif 4597 4598 ASSERT(in_hblk_range(hmeblkp, addr)); 4599 ASSERT(hmeblkp->hblk_shw_bit == 0); 4600 4601 #ifdef DEBUG 4602 if (get_hblk_ttesz(hmeblkp) != TTE8K && 4603 (endaddr < get_hblk_endaddr(hmeblkp))) { 4604 panic("sfmmu_hblk_chgprot: partial chgprot of large page"); 4605 } 4606 #endif /* DEBUG */ 4607 4608 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 4609 ttesz = get_hblk_ttesz(hmeblkp); 4610 4611 pprot = sfmmu_vtop_prot(vprot, &tteflags); 4612 #if defined(SF_ERRATA_57) 4613 check_exec = (sfmmup != ksfmmup) && 4614 AS_TYPE_64BIT(sfmmup->sfmmu_as) && 4615 ((vprot & PROT_EXEC) == PROT_EXEC); 4616 #endif 4617 HBLKTOHME(sfhmep, hmeblkp, addr); 4618 4619 /* 4620 * Flush the current demap region if addresses have been 4621 * skipped or the page size doesn't match. 4622 */ 4623 use_demap_range = (TTEBYTES(ttesz) == MMU_PAGESIZE); 4624 if (use_demap_range) { 4625 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr); 4626 } else { 4627 DEMAP_RANGE_FLUSH(dmrp); 4628 } 4629 4630 while (addr < endaddr) { 4631 sfmmu_copytte(&sfhmep->hme_tte, &tte); 4632 if (TTE_IS_VALID(&tte)) { 4633 if (TTE_GET_LOFLAGS(&tte, tteflags) == pprot) { 4634 /* 4635 * if the new protection is the same as old 4636 * continue 4637 */ 4638 goto next_addr; 4639 } 4640 pml = NULL; 4641 pp = sfhmep->hme_page; 4642 if (pp) { 4643 pml = sfmmu_mlist_enter(pp); 4644 } 4645 if (pp != sfhmep->hme_page) { 4646 /* 4647 * tte most have been unloaded 4648 * underneath us. Recheck 4649 */ 4650 ASSERT(pml); 4651 sfmmu_mlist_exit(pml); 4652 continue; 4653 } 4654 4655 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 4656 4657 ttemod = tte; 4658 TTE_SET_LOFLAGS(&ttemod, tteflags, pprot); 4659 #if defined(SF_ERRATA_57) 4660 if (check_exec && addr < errata57_limit) 4661 ttemod.tte_exec_perm = 0; 4662 #endif 4663 ret = sfmmu_modifytte_try(&tte, &ttemod, 4664 &sfhmep->hme_tte); 4665 4666 if (ret < 0) { 4667 /* tte changed underneath us */ 4668 if (pml) { 4669 sfmmu_mlist_exit(pml); 4670 } 4671 continue; 4672 } 4673 4674 if (tteflags & TTE_HWWR_INT) { 4675 /* 4676 * need to sync if we are clearing modify bit. 4677 */ 4678 sfmmu_ttesync(sfmmup, addr, &tte, pp); 4679 } 4680 4681 if (pp && PP_ISRO(pp)) { 4682 if (pprot & TTE_WRPRM_INT) { 4683 pmtx = sfmmu_page_enter(pp); 4684 PP_CLRRO(pp); 4685 sfmmu_page_exit(pmtx); 4686 } 4687 } 4688 4689 if (ret > 0 && use_demap_range) { 4690 DEMAP_RANGE_MARKPG(dmrp, addr); 4691 } else if (ret > 0) { 4692 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 4693 } 4694 4695 if (pml) { 4696 sfmmu_mlist_exit(pml); 4697 } 4698 } 4699 next_addr: 4700 addr += TTEBYTES(ttesz); 4701 sfhmep++; 4702 DEMAP_RANGE_NEXTPG(dmrp); 4703 } 4704 return (addr); 4705 } 4706 4707 /* 4708 * This routine is deprecated and should only be used by hat_chgprot. 4709 * The correct routine is sfmmu_vtop_attr. 4710 * This routine converts virtual page protections to physical ones. It will 4711 * update the tteflags field with the tte mask corresponding to the protections 4712 * affected and it returns the new protections. It will also clear the modify 4713 * bit if we are taking away write permission. This is necessary since the 4714 * modify bit is the hardware permission bit and we need to clear it in order 4715 * to detect write faults. 4716 * It accepts the following special protections: 4717 * ~PROT_WRITE = remove write permissions. 4718 * ~PROT_USER = remove user permissions. 4719 */ 4720 static uint_t 4721 sfmmu_vtop_prot(uint_t vprot, uint_t *tteflagsp) 4722 { 4723 if (vprot == (uint_t)~PROT_WRITE) { 4724 *tteflagsp = TTE_WRPRM_INT | TTE_HWWR_INT; 4725 return (0); /* will cause wrprm to be cleared */ 4726 } 4727 if (vprot == (uint_t)~PROT_USER) { 4728 *tteflagsp = TTE_PRIV_INT; 4729 return (0); /* will cause privprm to be cleared */ 4730 } 4731 if ((vprot == 0) || (vprot == PROT_USER) || 4732 ((vprot & PROT_ALL) != vprot)) { 4733 panic("sfmmu_vtop_prot -- bad prot %x", vprot); 4734 } 4735 4736 switch (vprot) { 4737 case (PROT_READ): 4738 case (PROT_EXEC): 4739 case (PROT_EXEC | PROT_READ): 4740 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT | TTE_HWWR_INT; 4741 return (TTE_PRIV_INT); /* set prv and clr wrt */ 4742 case (PROT_WRITE): 4743 case (PROT_WRITE | PROT_READ): 4744 case (PROT_EXEC | PROT_WRITE): 4745 case (PROT_EXEC | PROT_WRITE | PROT_READ): 4746 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT; 4747 return (TTE_PRIV_INT | TTE_WRPRM_INT); /* set prv and wrt */ 4748 case (PROT_USER | PROT_READ): 4749 case (PROT_USER | PROT_EXEC): 4750 case (PROT_USER | PROT_EXEC | PROT_READ): 4751 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT | TTE_HWWR_INT; 4752 return (0); /* clr prv and wrt */ 4753 case (PROT_USER | PROT_WRITE): 4754 case (PROT_USER | PROT_WRITE | PROT_READ): 4755 case (PROT_USER | PROT_EXEC | PROT_WRITE): 4756 case (PROT_USER | PROT_EXEC | PROT_WRITE | PROT_READ): 4757 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT; 4758 return (TTE_WRPRM_INT); /* clr prv and set wrt */ 4759 default: 4760 panic("sfmmu_vtop_prot -- bad prot %x", vprot); 4761 } 4762 return (0); 4763 } 4764 4765 /* 4766 * Alternate unload for very large virtual ranges. With a true 64 bit VA, 4767 * the normal algorithm would take too long for a very large VA range with 4768 * few real mappings. This routine just walks thru all HMEs in the global 4769 * hash table to find and remove mappings. 4770 */ 4771 static void 4772 hat_unload_large_virtual( 4773 struct hat *sfmmup, 4774 caddr_t startaddr, 4775 size_t len, 4776 uint_t flags, 4777 hat_callback_t *callback) 4778 { 4779 struct hmehash_bucket *hmebp; 4780 struct hme_blk *hmeblkp; 4781 struct hme_blk *pr_hblk = NULL; 4782 struct hme_blk *nx_hblk; 4783 struct hme_blk *list = NULL; 4784 int i; 4785 uint64_t hblkpa, prevpa, nx_pa; 4786 demap_range_t dmr, *dmrp; 4787 cpuset_t cpuset; 4788 caddr_t endaddr = startaddr + len; 4789 caddr_t sa; 4790 caddr_t ea; 4791 caddr_t cb_sa[MAX_CB_ADDR]; 4792 caddr_t cb_ea[MAX_CB_ADDR]; 4793 int addr_cnt = 0; 4794 int a = 0; 4795 4796 if (sfmmup->sfmmu_free) { 4797 dmrp = NULL; 4798 } else { 4799 dmrp = &dmr; 4800 DEMAP_RANGE_INIT(sfmmup, dmrp); 4801 } 4802 4803 /* 4804 * Loop through all the hash buckets of HME blocks looking for matches. 4805 */ 4806 for (i = 0; i <= UHMEHASH_SZ; i++) { 4807 hmebp = &uhme_hash[i]; 4808 SFMMU_HASH_LOCK(hmebp); 4809 hmeblkp = hmebp->hmeblkp; 4810 hblkpa = hmebp->hmeh_nextpa; 4811 prevpa = 0; 4812 pr_hblk = NULL; 4813 while (hmeblkp) { 4814 nx_hblk = hmeblkp->hblk_next; 4815 nx_pa = hmeblkp->hblk_nextpa; 4816 4817 /* 4818 * skip if not this context, if a shadow block or 4819 * if the mapping is not in the requested range 4820 */ 4821 if (hmeblkp->hblk_tag.htag_id != sfmmup || 4822 hmeblkp->hblk_shw_bit || 4823 (sa = (caddr_t)get_hblk_base(hmeblkp)) >= endaddr || 4824 (ea = get_hblk_endaddr(hmeblkp)) <= startaddr) { 4825 pr_hblk = hmeblkp; 4826 prevpa = hblkpa; 4827 goto next_block; 4828 } 4829 4830 /* 4831 * unload if there are any current valid mappings 4832 */ 4833 if (hmeblkp->hblk_vcnt != 0 || 4834 hmeblkp->hblk_hmecnt != 0) 4835 (void) sfmmu_hblk_unload(sfmmup, hmeblkp, 4836 sa, ea, dmrp, flags); 4837 4838 /* 4839 * on unmap we also release the HME block itself, once 4840 * all mappings are gone. 4841 */ 4842 if ((flags & HAT_UNLOAD_UNMAP) != 0 && 4843 !hmeblkp->hblk_vcnt && 4844 !hmeblkp->hblk_hmecnt) { 4845 ASSERT(!hmeblkp->hblk_lckcnt); 4846 sfmmu_hblk_hash_rm(hmebp, hmeblkp, 4847 prevpa, pr_hblk); 4848 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 4849 } else { 4850 pr_hblk = hmeblkp; 4851 prevpa = hblkpa; 4852 } 4853 4854 if (callback == NULL) 4855 goto next_block; 4856 4857 /* 4858 * HME blocks may span more than one page, but we may be 4859 * unmapping only one page, so check for a smaller range 4860 * for the callback 4861 */ 4862 if (sa < startaddr) 4863 sa = startaddr; 4864 if (--ea > endaddr) 4865 ea = endaddr - 1; 4866 4867 cb_sa[addr_cnt] = sa; 4868 cb_ea[addr_cnt] = ea; 4869 if (++addr_cnt == MAX_CB_ADDR) { 4870 if (dmrp != NULL) { 4871 DEMAP_RANGE_FLUSH(dmrp); 4872 cpuset = sfmmup->sfmmu_cpusran; 4873 xt_sync(cpuset); 4874 } 4875 4876 for (a = 0; a < MAX_CB_ADDR; ++a) { 4877 callback->hcb_start_addr = cb_sa[a]; 4878 callback->hcb_end_addr = cb_ea[a]; 4879 callback->hcb_function(callback); 4880 } 4881 addr_cnt = 0; 4882 } 4883 4884 next_block: 4885 hmeblkp = nx_hblk; 4886 hblkpa = nx_pa; 4887 } 4888 SFMMU_HASH_UNLOCK(hmebp); 4889 } 4890 4891 sfmmu_hblks_list_purge(&list); 4892 if (dmrp != NULL) { 4893 DEMAP_RANGE_FLUSH(dmrp); 4894 cpuset = sfmmup->sfmmu_cpusran; 4895 xt_sync(cpuset); 4896 } 4897 4898 for (a = 0; a < addr_cnt; ++a) { 4899 callback->hcb_start_addr = cb_sa[a]; 4900 callback->hcb_end_addr = cb_ea[a]; 4901 callback->hcb_function(callback); 4902 } 4903 4904 /* 4905 * Check TSB and TLB page sizes if the process isn't exiting. 4906 */ 4907 if (!sfmmup->sfmmu_free) 4908 sfmmu_check_page_sizes(sfmmup, 0); 4909 } 4910 4911 /* 4912 * Unload all the mappings in the range [addr..addr+len). addr and len must 4913 * be MMU_PAGESIZE aligned. 4914 */ 4915 4916 extern struct seg *segkmap; 4917 #define ISSEGKMAP(sfmmup, addr) (sfmmup == ksfmmup && \ 4918 segkmap->s_base <= (addr) && (addr) < (segkmap->s_base + segkmap->s_size)) 4919 4920 4921 void 4922 hat_unload_callback( 4923 struct hat *sfmmup, 4924 caddr_t addr, 4925 size_t len, 4926 uint_t flags, 4927 hat_callback_t *callback) 4928 { 4929 struct hmehash_bucket *hmebp; 4930 hmeblk_tag hblktag; 4931 int hmeshift, hashno, iskernel; 4932 struct hme_blk *hmeblkp, *pr_hblk, *list = NULL; 4933 caddr_t endaddr; 4934 cpuset_t cpuset; 4935 uint64_t hblkpa, prevpa; 4936 int addr_count = 0; 4937 int a; 4938 caddr_t cb_start_addr[MAX_CB_ADDR]; 4939 caddr_t cb_end_addr[MAX_CB_ADDR]; 4940 int issegkmap = ISSEGKMAP(sfmmup, addr); 4941 demap_range_t dmr, *dmrp; 4942 4943 if (sfmmup->sfmmu_xhat_provider) { 4944 XHAT_UNLOAD_CALLBACK(sfmmup, addr, len, flags, callback); 4945 return; 4946 } else { 4947 /* 4948 * This must be a CPU HAT. If the address space has 4949 * XHATs attached, unload the mappings for all of them, 4950 * just in case 4951 */ 4952 ASSERT(sfmmup->sfmmu_as != NULL); 4953 if (sfmmup->sfmmu_as->a_xhat != NULL) 4954 xhat_unload_callback_all(sfmmup->sfmmu_as, addr, 4955 len, flags, callback); 4956 } 4957 4958 ASSERT((sfmmup == ksfmmup) || (flags & HAT_UNLOAD_OTHER) || \ 4959 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 4960 4961 ASSERT(sfmmup != NULL); 4962 ASSERT((len & MMU_PAGEOFFSET) == 0); 4963 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET)); 4964 4965 /* 4966 * Probing through a large VA range (say 63 bits) will be slow, even 4967 * at 4 Meg steps between the probes. So, when the virtual address range 4968 * is very large, search the HME entries for what to unload. 4969 * 4970 * len >> TTE_PAGE_SHIFT(TTE4M) is the # of 4Meg probes we'd need 4971 * 4972 * UHMEHASH_SZ is number of hash buckets to examine 4973 * 4974 */ 4975 if (sfmmup != KHATID && (len >> TTE_PAGE_SHIFT(TTE4M)) > UHMEHASH_SZ) { 4976 hat_unload_large_virtual(sfmmup, addr, len, flags, callback); 4977 return; 4978 } 4979 4980 CPUSET_ZERO(cpuset); 4981 4982 /* 4983 * If the process is exiting, we can save a lot of fuss since 4984 * we'll flush the TLB when we free the ctx anyway. 4985 */ 4986 if (sfmmup->sfmmu_free) 4987 dmrp = NULL; 4988 else 4989 dmrp = &dmr; 4990 4991 DEMAP_RANGE_INIT(sfmmup, dmrp); 4992 endaddr = addr + len; 4993 hblktag.htag_id = sfmmup; 4994 4995 /* 4996 * It is likely for the vm to call unload over a wide range of 4997 * addresses that are actually very sparsely populated by 4998 * translations. In order to speed this up the sfmmu hat supports 4999 * the concept of shadow hmeblks. Dummy large page hmeblks that 5000 * correspond to actual small translations are allocated at tteload 5001 * time and are referred to as shadow hmeblks. Now, during unload 5002 * time, we first check if we have a shadow hmeblk for that 5003 * translation. The absence of one means the corresponding address 5004 * range is empty and can be skipped. 5005 * 5006 * The kernel is an exception to above statement and that is why 5007 * we don't use shadow hmeblks and hash starting from the smallest 5008 * page size. 5009 */ 5010 if (sfmmup == KHATID) { 5011 iskernel = 1; 5012 hashno = TTE64K; 5013 } else { 5014 iskernel = 0; 5015 if (mmu_page_sizes == max_mmu_page_sizes) { 5016 hashno = TTE256M; 5017 } else { 5018 hashno = TTE4M; 5019 } 5020 } 5021 while (addr < endaddr) { 5022 hmeshift = HME_HASH_SHIFT(hashno); 5023 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 5024 hblktag.htag_rehash = hashno; 5025 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 5026 5027 SFMMU_HASH_LOCK(hmebp); 5028 5029 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, hblkpa, pr_hblk, 5030 prevpa, &list); 5031 if (hmeblkp == NULL) { 5032 /* 5033 * didn't find an hmeblk. skip the appropiate 5034 * address range. 5035 */ 5036 SFMMU_HASH_UNLOCK(hmebp); 5037 if (iskernel) { 5038 if (hashno < mmu_hashcnt) { 5039 hashno++; 5040 continue; 5041 } else { 5042 hashno = TTE64K; 5043 addr = (caddr_t)roundup((uintptr_t)addr 5044 + 1, MMU_PAGESIZE64K); 5045 continue; 5046 } 5047 } 5048 addr = (caddr_t)roundup((uintptr_t)addr + 1, 5049 (1 << hmeshift)); 5050 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) { 5051 ASSERT(hashno == TTE64K); 5052 continue; 5053 } 5054 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) { 5055 hashno = TTE512K; 5056 continue; 5057 } 5058 if (mmu_page_sizes == max_mmu_page_sizes) { 5059 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) { 5060 hashno = TTE4M; 5061 continue; 5062 } 5063 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) { 5064 hashno = TTE32M; 5065 continue; 5066 } 5067 hashno = TTE256M; 5068 continue; 5069 } else { 5070 hashno = TTE4M; 5071 continue; 5072 } 5073 } 5074 ASSERT(hmeblkp); 5075 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) { 5076 /* 5077 * If the valid count is zero we can skip the range 5078 * mapped by this hmeblk. 5079 * We free hblks in the case of HAT_UNMAP. HAT_UNMAP 5080 * is used by segment drivers as a hint 5081 * that the mapping resource won't be used any longer. 5082 * The best example of this is during exit(). 5083 */ 5084 addr = (caddr_t)roundup((uintptr_t)addr + 1, 5085 get_hblk_span(hmeblkp)); 5086 if ((flags & HAT_UNLOAD_UNMAP) || 5087 (iskernel && !issegkmap)) { 5088 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, 5089 pr_hblk); 5090 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 5091 } 5092 SFMMU_HASH_UNLOCK(hmebp); 5093 5094 if (iskernel) { 5095 hashno = TTE64K; 5096 continue; 5097 } 5098 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) { 5099 ASSERT(hashno == TTE64K); 5100 continue; 5101 } 5102 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) { 5103 hashno = TTE512K; 5104 continue; 5105 } 5106 if (mmu_page_sizes == max_mmu_page_sizes) { 5107 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) { 5108 hashno = TTE4M; 5109 continue; 5110 } 5111 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) { 5112 hashno = TTE32M; 5113 continue; 5114 } 5115 hashno = TTE256M; 5116 continue; 5117 } else { 5118 hashno = TTE4M; 5119 continue; 5120 } 5121 } 5122 if (hmeblkp->hblk_shw_bit) { 5123 /* 5124 * If we encounter a shadow hmeblk we know there is 5125 * smaller sized hmeblks mapping the same address space. 5126 * Decrement the hash size and rehash. 5127 */ 5128 ASSERT(sfmmup != KHATID); 5129 hashno--; 5130 SFMMU_HASH_UNLOCK(hmebp); 5131 continue; 5132 } 5133 5134 /* 5135 * track callback address ranges. 5136 * only start a new range when it's not contiguous 5137 */ 5138 if (callback != NULL) { 5139 if (addr_count > 0 && 5140 addr == cb_end_addr[addr_count - 1]) 5141 --addr_count; 5142 else 5143 cb_start_addr[addr_count] = addr; 5144 } 5145 5146 addr = sfmmu_hblk_unload(sfmmup, hmeblkp, addr, endaddr, 5147 dmrp, flags); 5148 5149 if (callback != NULL) 5150 cb_end_addr[addr_count++] = addr; 5151 5152 if (((flags & HAT_UNLOAD_UNMAP) || (iskernel && !issegkmap)) && 5153 !hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) { 5154 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, 5155 pr_hblk); 5156 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 5157 } 5158 SFMMU_HASH_UNLOCK(hmebp); 5159 5160 /* 5161 * Notify our caller as to exactly which pages 5162 * have been unloaded. We do these in clumps, 5163 * to minimize the number of xt_sync()s that need to occur. 5164 */ 5165 if (callback != NULL && addr_count == MAX_CB_ADDR) { 5166 DEMAP_RANGE_FLUSH(dmrp); 5167 if (dmrp != NULL) { 5168 cpuset = sfmmup->sfmmu_cpusran; 5169 xt_sync(cpuset); 5170 } 5171 5172 for (a = 0; a < MAX_CB_ADDR; ++a) { 5173 callback->hcb_start_addr = cb_start_addr[a]; 5174 callback->hcb_end_addr = cb_end_addr[a]; 5175 callback->hcb_function(callback); 5176 } 5177 addr_count = 0; 5178 } 5179 if (iskernel) { 5180 hashno = TTE64K; 5181 continue; 5182 } 5183 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) { 5184 ASSERT(hashno == TTE64K); 5185 continue; 5186 } 5187 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) { 5188 hashno = TTE512K; 5189 continue; 5190 } 5191 if (mmu_page_sizes == max_mmu_page_sizes) { 5192 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) { 5193 hashno = TTE4M; 5194 continue; 5195 } 5196 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) { 5197 hashno = TTE32M; 5198 continue; 5199 } 5200 hashno = TTE256M; 5201 } else { 5202 hashno = TTE4M; 5203 } 5204 } 5205 5206 sfmmu_hblks_list_purge(&list); 5207 DEMAP_RANGE_FLUSH(dmrp); 5208 if (dmrp != NULL) { 5209 cpuset = sfmmup->sfmmu_cpusran; 5210 xt_sync(cpuset); 5211 } 5212 if (callback && addr_count != 0) { 5213 for (a = 0; a < addr_count; ++a) { 5214 callback->hcb_start_addr = cb_start_addr[a]; 5215 callback->hcb_end_addr = cb_end_addr[a]; 5216 callback->hcb_function(callback); 5217 } 5218 } 5219 5220 /* 5221 * Check TSB and TLB page sizes if the process isn't exiting. 5222 */ 5223 if (!sfmmup->sfmmu_free) 5224 sfmmu_check_page_sizes(sfmmup, 0); 5225 } 5226 5227 /* 5228 * Unload all the mappings in the range [addr..addr+len). addr and len must 5229 * be MMU_PAGESIZE aligned. 5230 */ 5231 void 5232 hat_unload(struct hat *sfmmup, caddr_t addr, size_t len, uint_t flags) 5233 { 5234 if (sfmmup->sfmmu_xhat_provider) { 5235 XHAT_UNLOAD(sfmmup, addr, len, flags); 5236 return; 5237 } 5238 hat_unload_callback(sfmmup, addr, len, flags, NULL); 5239 } 5240 5241 5242 /* 5243 * Find the largest mapping size for this page. 5244 */ 5245 int 5246 fnd_mapping_sz(page_t *pp) 5247 { 5248 int sz; 5249 int p_index; 5250 5251 p_index = PP_MAPINDEX(pp); 5252 5253 sz = 0; 5254 p_index >>= 1; /* don't care about 8K bit */ 5255 for (; p_index; p_index >>= 1) { 5256 sz++; 5257 } 5258 5259 return (sz); 5260 } 5261 5262 /* 5263 * This function unloads a range of addresses for an hmeblk. 5264 * It returns the next address to be unloaded. 5265 * It should be called with the hash lock held. 5266 */ 5267 static caddr_t 5268 sfmmu_hblk_unload(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 5269 caddr_t endaddr, demap_range_t *dmrp, uint_t flags) 5270 { 5271 tte_t tte, ttemod; 5272 struct sf_hment *sfhmep; 5273 int ttesz; 5274 long ttecnt; 5275 page_t *pp; 5276 kmutex_t *pml; 5277 int ret; 5278 int use_demap_range; 5279 5280 ASSERT(in_hblk_range(hmeblkp, addr)); 5281 ASSERT(!hmeblkp->hblk_shw_bit); 5282 #ifdef DEBUG 5283 if (get_hblk_ttesz(hmeblkp) != TTE8K && 5284 (endaddr < get_hblk_endaddr(hmeblkp))) { 5285 panic("sfmmu_hblk_unload: partial unload of large page"); 5286 } 5287 #endif /* DEBUG */ 5288 5289 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 5290 ttesz = get_hblk_ttesz(hmeblkp); 5291 5292 use_demap_range = (do_virtual_coloring && 5293 ((dmrp == NULL) || TTEBYTES(ttesz) == DEMAP_RANGE_PGSZ(dmrp))); 5294 if (use_demap_range) { 5295 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr); 5296 } else { 5297 DEMAP_RANGE_FLUSH(dmrp); 5298 } 5299 ttecnt = 0; 5300 HBLKTOHME(sfhmep, hmeblkp, addr); 5301 5302 while (addr < endaddr) { 5303 pml = NULL; 5304 again: 5305 sfmmu_copytte(&sfhmep->hme_tte, &tte); 5306 if (TTE_IS_VALID(&tte)) { 5307 pp = sfhmep->hme_page; 5308 if (pp && pml == NULL) { 5309 pml = sfmmu_mlist_enter(pp); 5310 } 5311 5312 /* 5313 * Verify if hme still points to 'pp' now that 5314 * we have p_mapping lock. 5315 */ 5316 if (sfhmep->hme_page != pp) { 5317 if (pp != NULL && sfhmep->hme_page != NULL) { 5318 if (pml) { 5319 sfmmu_mlist_exit(pml); 5320 } 5321 /* Re-start this iteration. */ 5322 continue; 5323 } 5324 ASSERT((pp != NULL) && 5325 (sfhmep->hme_page == NULL)); 5326 goto tte_unloaded; 5327 } 5328 5329 /* 5330 * This point on we have both HASH and p_mapping 5331 * lock. 5332 */ 5333 ASSERT(pp == sfhmep->hme_page); 5334 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 5335 5336 /* 5337 * We need to loop on modify tte because it is 5338 * possible for pagesync to come along and 5339 * change the software bits beneath us. 5340 * 5341 * Page_unload can also invalidate the tte after 5342 * we read tte outside of p_mapping lock. 5343 */ 5344 ttemod = tte; 5345 5346 TTE_SET_INVALID(&ttemod); 5347 ret = sfmmu_modifytte_try(&tte, &ttemod, 5348 &sfhmep->hme_tte); 5349 5350 if (ret <= 0) { 5351 if (TTE_IS_VALID(&tte)) { 5352 goto again; 5353 } else { 5354 /* 5355 * We read in a valid pte, but it 5356 * is unloaded by page_unload. 5357 * hme_page has become NULL and 5358 * we hold no p_mapping lock. 5359 */ 5360 ASSERT(pp == NULL && pml == NULL); 5361 goto tte_unloaded; 5362 } 5363 } 5364 5365 if (!(flags & HAT_UNLOAD_NOSYNC)) { 5366 sfmmu_ttesync(sfmmup, addr, &tte, pp); 5367 } 5368 5369 /* 5370 * Ok- we invalidated the tte. Do the rest of the job. 5371 */ 5372 ttecnt++; 5373 5374 if (flags & HAT_UNLOAD_UNLOCK) { 5375 ASSERT(hmeblkp->hblk_lckcnt > 0); 5376 atomic_add_16(&hmeblkp->hblk_lckcnt, -1); 5377 HBLK_STACK_TRACE(hmeblkp, HBLK_UNLOCK); 5378 } 5379 5380 /* 5381 * Normally we would need to flush the page 5382 * from the virtual cache at this point in 5383 * order to prevent a potential cache alias 5384 * inconsistency. 5385 * The particular scenario we need to worry 5386 * about is: 5387 * Given: va1 and va2 are two virtual address 5388 * that alias and map the same physical 5389 * address. 5390 * 1. mapping exists from va1 to pa and data 5391 * has been read into the cache. 5392 * 2. unload va1. 5393 * 3. load va2 and modify data using va2. 5394 * 4 unload va2. 5395 * 5. load va1 and reference data. Unless we 5396 * flush the data cache when we unload we will 5397 * get stale data. 5398 * Fortunately, page coloring eliminates the 5399 * above scenario by remembering the color a 5400 * physical page was last or is currently 5401 * mapped to. Now, we delay the flush until 5402 * the loading of translations. Only when the 5403 * new translation is of a different color 5404 * are we forced to flush. 5405 */ 5406 if (use_demap_range) { 5407 /* 5408 * Mark this page as needing a demap. 5409 */ 5410 DEMAP_RANGE_MARKPG(dmrp, addr); 5411 } else { 5412 if (do_virtual_coloring) { 5413 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 5414 sfmmup->sfmmu_free, 0); 5415 } else { 5416 pfn_t pfnum; 5417 5418 pfnum = TTE_TO_PFN(addr, &tte); 5419 sfmmu_tlbcache_demap(addr, sfmmup, 5420 hmeblkp, pfnum, sfmmup->sfmmu_free, 5421 FLUSH_NECESSARY_CPUS, 5422 CACHE_FLUSH, 0); 5423 } 5424 } 5425 5426 if (pp) { 5427 /* 5428 * Remove the hment from the mapping list 5429 */ 5430 ASSERT(hmeblkp->hblk_hmecnt > 0); 5431 5432 /* 5433 * Again, we cannot 5434 * ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS); 5435 */ 5436 HME_SUB(sfhmep, pp); 5437 membar_stst(); 5438 atomic_add_16(&hmeblkp->hblk_hmecnt, -1); 5439 } 5440 5441 ASSERT(hmeblkp->hblk_vcnt > 0); 5442 atomic_add_16(&hmeblkp->hblk_vcnt, -1); 5443 5444 ASSERT(hmeblkp->hblk_hmecnt || hmeblkp->hblk_vcnt || 5445 !hmeblkp->hblk_lckcnt); 5446 5447 #ifdef VAC 5448 if (pp && (pp->p_nrm & (P_KPMC | P_KPMS | P_TNC))) { 5449 if (PP_ISTNC(pp)) { 5450 /* 5451 * If page was temporary 5452 * uncached, try to recache 5453 * it. Note that HME_SUB() was 5454 * called above so p_index and 5455 * mlist had been updated. 5456 */ 5457 conv_tnc(pp, ttesz); 5458 } else if (pp->p_mapping == NULL) { 5459 ASSERT(kpm_enable); 5460 /* 5461 * Page is marked to be in VAC conflict 5462 * to an existing kpm mapping and/or is 5463 * kpm mapped using only the regular 5464 * pagesize. 5465 */ 5466 sfmmu_kpm_hme_unload(pp); 5467 } 5468 } 5469 #endif /* VAC */ 5470 } else if ((pp = sfhmep->hme_page) != NULL) { 5471 /* 5472 * TTE is invalid but the hme 5473 * still exists. let pageunload 5474 * complete its job. 5475 */ 5476 ASSERT(pml == NULL); 5477 pml = sfmmu_mlist_enter(pp); 5478 if (sfhmep->hme_page != NULL) { 5479 sfmmu_mlist_exit(pml); 5480 pml = NULL; 5481 goto again; 5482 } 5483 ASSERT(sfhmep->hme_page == NULL); 5484 } else if (hmeblkp->hblk_hmecnt != 0) { 5485 /* 5486 * pageunload may have not finished decrementing 5487 * hblk_vcnt and hblk_hmecnt. Find page_t if any and 5488 * wait for pageunload to finish. Rely on pageunload 5489 * to decrement hblk_hmecnt after hblk_vcnt. 5490 */ 5491 pfn_t pfn = TTE_TO_TTEPFN(&tte); 5492 ASSERT(pml == NULL); 5493 if (pf_is_memory(pfn)) { 5494 pp = page_numtopp_nolock(pfn); 5495 if (pp != NULL) { 5496 pml = sfmmu_mlist_enter(pp); 5497 sfmmu_mlist_exit(pml); 5498 pml = NULL; 5499 } 5500 } 5501 } 5502 5503 tte_unloaded: 5504 /* 5505 * At this point, the tte we are looking at 5506 * should be unloaded, and hme has been unlinked 5507 * from page too. This is important because in 5508 * pageunload, it does ttesync() then HME_SUB. 5509 * We need to make sure HME_SUB has been completed 5510 * so we know ttesync() has been completed. Otherwise, 5511 * at exit time, after return from hat layer, VM will 5512 * release as structure which hat_setstat() (called 5513 * by ttesync()) needs. 5514 */ 5515 #ifdef DEBUG 5516 { 5517 tte_t dtte; 5518 5519 ASSERT(sfhmep->hme_page == NULL); 5520 5521 sfmmu_copytte(&sfhmep->hme_tte, &dtte); 5522 ASSERT(!TTE_IS_VALID(&dtte)); 5523 } 5524 #endif 5525 5526 if (pml) { 5527 sfmmu_mlist_exit(pml); 5528 } 5529 5530 addr += TTEBYTES(ttesz); 5531 sfhmep++; 5532 DEMAP_RANGE_NEXTPG(dmrp); 5533 } 5534 if (ttecnt > 0) 5535 atomic_add_long(&sfmmup->sfmmu_ttecnt[ttesz], -ttecnt); 5536 return (addr); 5537 } 5538 5539 /* 5540 * Synchronize all the mappings in the range [addr..addr+len). 5541 * Can be called with clearflag having two states: 5542 * HAT_SYNC_DONTZERO means just return the rm stats 5543 * HAT_SYNC_ZERORM means zero rm bits in the tte and return the stats 5544 */ 5545 void 5546 hat_sync(struct hat *sfmmup, caddr_t addr, size_t len, uint_t clearflag) 5547 { 5548 struct hmehash_bucket *hmebp; 5549 hmeblk_tag hblktag; 5550 int hmeshift, hashno = 1; 5551 struct hme_blk *hmeblkp, *list = NULL; 5552 caddr_t endaddr; 5553 cpuset_t cpuset; 5554 5555 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 5556 ASSERT((sfmmup == ksfmmup) || 5557 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 5558 ASSERT((len & MMU_PAGEOFFSET) == 0); 5559 ASSERT((clearflag == HAT_SYNC_DONTZERO) || 5560 (clearflag == HAT_SYNC_ZERORM)); 5561 5562 CPUSET_ZERO(cpuset); 5563 5564 endaddr = addr + len; 5565 hblktag.htag_id = sfmmup; 5566 /* 5567 * Spitfire supports 4 page sizes. 5568 * Most pages are expected to be of the smallest page 5569 * size (8K) and these will not need to be rehashed. 64K 5570 * pages also don't need to be rehashed because the an hmeblk 5571 * spans 64K of address space. 512K pages might need 1 rehash and 5572 * and 4M pages 2 rehashes. 5573 */ 5574 while (addr < endaddr) { 5575 hmeshift = HME_HASH_SHIFT(hashno); 5576 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 5577 hblktag.htag_rehash = hashno; 5578 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 5579 5580 SFMMU_HASH_LOCK(hmebp); 5581 5582 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 5583 if (hmeblkp != NULL) { 5584 /* 5585 * We've encountered a shadow hmeblk so skip the range 5586 * of the next smaller mapping size. 5587 */ 5588 if (hmeblkp->hblk_shw_bit) { 5589 ASSERT(sfmmup != ksfmmup); 5590 ASSERT(hashno > 1); 5591 addr = (caddr_t)P2END((uintptr_t)addr, 5592 TTEBYTES(hashno - 1)); 5593 } else { 5594 addr = sfmmu_hblk_sync(sfmmup, hmeblkp, 5595 addr, endaddr, clearflag); 5596 } 5597 SFMMU_HASH_UNLOCK(hmebp); 5598 hashno = 1; 5599 continue; 5600 } 5601 SFMMU_HASH_UNLOCK(hmebp); 5602 5603 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 5604 /* 5605 * We have traversed the whole list and rehashed 5606 * if necessary without finding the address to sync. 5607 * This is ok so we increment the address by the 5608 * smallest hmeblk range for kernel mappings and the 5609 * largest hmeblk range, to account for shadow hmeblks, 5610 * for user mappings and continue. 5611 */ 5612 if (sfmmup == ksfmmup) 5613 addr = (caddr_t)P2END((uintptr_t)addr, 5614 TTEBYTES(1)); 5615 else 5616 addr = (caddr_t)P2END((uintptr_t)addr, 5617 TTEBYTES(hashno)); 5618 hashno = 1; 5619 } else { 5620 hashno++; 5621 } 5622 } 5623 sfmmu_hblks_list_purge(&list); 5624 cpuset = sfmmup->sfmmu_cpusran; 5625 xt_sync(cpuset); 5626 } 5627 5628 static caddr_t 5629 sfmmu_hblk_sync(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 5630 caddr_t endaddr, int clearflag) 5631 { 5632 tte_t tte, ttemod; 5633 struct sf_hment *sfhmep; 5634 int ttesz; 5635 struct page *pp; 5636 kmutex_t *pml; 5637 int ret; 5638 5639 ASSERT(hmeblkp->hblk_shw_bit == 0); 5640 5641 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 5642 5643 ttesz = get_hblk_ttesz(hmeblkp); 5644 HBLKTOHME(sfhmep, hmeblkp, addr); 5645 5646 while (addr < endaddr) { 5647 sfmmu_copytte(&sfhmep->hme_tte, &tte); 5648 if (TTE_IS_VALID(&tte)) { 5649 pml = NULL; 5650 pp = sfhmep->hme_page; 5651 if (pp) { 5652 pml = sfmmu_mlist_enter(pp); 5653 } 5654 if (pp != sfhmep->hme_page) { 5655 /* 5656 * tte most have been unloaded 5657 * underneath us. Recheck 5658 */ 5659 ASSERT(pml); 5660 sfmmu_mlist_exit(pml); 5661 continue; 5662 } 5663 5664 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 5665 5666 if (clearflag == HAT_SYNC_ZERORM) { 5667 ttemod = tte; 5668 TTE_CLR_RM(&ttemod); 5669 ret = sfmmu_modifytte_try(&tte, &ttemod, 5670 &sfhmep->hme_tte); 5671 if (ret < 0) { 5672 if (pml) { 5673 sfmmu_mlist_exit(pml); 5674 } 5675 continue; 5676 } 5677 5678 if (ret > 0) { 5679 sfmmu_tlb_demap(addr, sfmmup, 5680 hmeblkp, 0, 0); 5681 } 5682 } 5683 sfmmu_ttesync(sfmmup, addr, &tte, pp); 5684 if (pml) { 5685 sfmmu_mlist_exit(pml); 5686 } 5687 } 5688 addr += TTEBYTES(ttesz); 5689 sfhmep++; 5690 } 5691 return (addr); 5692 } 5693 5694 /* 5695 * This function will sync a tte to the page struct and it will 5696 * update the hat stats. Currently it allows us to pass a NULL pp 5697 * and we will simply update the stats. We may want to change this 5698 * so we only keep stats for pages backed by pp's. 5699 */ 5700 static void 5701 sfmmu_ttesync(struct hat *sfmmup, caddr_t addr, tte_t *ttep, page_t *pp) 5702 { 5703 uint_t rm = 0; 5704 int sz; 5705 pgcnt_t npgs; 5706 5707 ASSERT(TTE_IS_VALID(ttep)); 5708 5709 if (TTE_IS_NOSYNC(ttep)) { 5710 return; 5711 } 5712 5713 if (TTE_IS_REF(ttep)) { 5714 rm = P_REF; 5715 } 5716 if (TTE_IS_MOD(ttep)) { 5717 rm |= P_MOD; 5718 } 5719 5720 if (rm == 0) { 5721 return; 5722 } 5723 5724 sz = TTE_CSZ(ttep); 5725 if (sfmmup->sfmmu_rmstat) { 5726 int i; 5727 caddr_t vaddr = addr; 5728 5729 for (i = 0; i < TTEPAGES(sz); i++, vaddr += MMU_PAGESIZE) { 5730 hat_setstat(sfmmup->sfmmu_as, vaddr, MMU_PAGESIZE, rm); 5731 } 5732 5733 } 5734 5735 /* 5736 * XXX I want to use cas to update nrm bits but they 5737 * currently belong in common/vm and not in hat where 5738 * they should be. 5739 * The nrm bits are protected by the same mutex as 5740 * the one that protects the page's mapping list. 5741 */ 5742 if (!pp) 5743 return; 5744 ASSERT(sfmmu_mlist_held(pp)); 5745 /* 5746 * If the tte is for a large page, we need to sync all the 5747 * pages covered by the tte. 5748 */ 5749 if (sz != TTE8K) { 5750 ASSERT(pp->p_szc != 0); 5751 pp = PP_GROUPLEADER(pp, sz); 5752 ASSERT(sfmmu_mlist_held(pp)); 5753 } 5754 5755 /* Get number of pages from tte size. */ 5756 npgs = TTEPAGES(sz); 5757 5758 do { 5759 ASSERT(pp); 5760 ASSERT(sfmmu_mlist_held(pp)); 5761 if (((rm & P_REF) != 0 && !PP_ISREF(pp)) || 5762 ((rm & P_MOD) != 0 && !PP_ISMOD(pp))) 5763 hat_page_setattr(pp, rm); 5764 5765 /* 5766 * Are we done? If not, we must have a large mapping. 5767 * For large mappings we need to sync the rest of the pages 5768 * covered by this tte; goto the next page. 5769 */ 5770 } while (--npgs > 0 && (pp = PP_PAGENEXT(pp))); 5771 } 5772 5773 /* 5774 * Execute pre-callback handler of each pa_hment linked to pp 5775 * 5776 * Inputs: 5777 * flag: either HAT_PRESUSPEND or HAT_SUSPEND. 5778 * capture_cpus: pointer to return value (below) 5779 * 5780 * Returns: 5781 * Propagates the subsystem callback return values back to the caller; 5782 * returns 0 on success. If capture_cpus is non-NULL, the value returned 5783 * is zero if all of the pa_hments are of a type that do not require 5784 * capturing CPUs prior to suspending the mapping, else it is 1. 5785 */ 5786 static int 5787 hat_pageprocess_precallbacks(struct page *pp, uint_t flag, int *capture_cpus) 5788 { 5789 struct sf_hment *sfhmep; 5790 struct pa_hment *pahmep; 5791 int (*f)(caddr_t, uint_t, uint_t, void *); 5792 int ret; 5793 id_t id; 5794 int locked = 0; 5795 kmutex_t *pml; 5796 5797 ASSERT(PAGE_EXCL(pp)); 5798 if (!sfmmu_mlist_held(pp)) { 5799 pml = sfmmu_mlist_enter(pp); 5800 locked = 1; 5801 } 5802 5803 if (capture_cpus) 5804 *capture_cpus = 0; 5805 5806 top: 5807 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) { 5808 /* 5809 * skip sf_hments corresponding to VA<->PA mappings; 5810 * for pa_hment's, hme_tte.ll is zero 5811 */ 5812 if (!IS_PAHME(sfhmep)) 5813 continue; 5814 5815 pahmep = sfhmep->hme_data; 5816 ASSERT(pahmep != NULL); 5817 5818 /* 5819 * skip if pre-handler has been called earlier in this loop 5820 */ 5821 if (pahmep->flags & flag) 5822 continue; 5823 5824 id = pahmep->cb_id; 5825 ASSERT(id >= (id_t)0 && id < sfmmu_cb_nextid); 5826 if (capture_cpus && sfmmu_cb_table[id].capture_cpus != 0) 5827 *capture_cpus = 1; 5828 if ((f = sfmmu_cb_table[id].prehandler) == NULL) { 5829 pahmep->flags |= flag; 5830 continue; 5831 } 5832 5833 /* 5834 * Drop the mapping list lock to avoid locking order issues. 5835 */ 5836 if (locked) 5837 sfmmu_mlist_exit(pml); 5838 5839 ret = f(pahmep->addr, pahmep->len, flag, pahmep->pvt); 5840 if (ret != 0) 5841 return (ret); /* caller must do the cleanup */ 5842 5843 if (locked) { 5844 pml = sfmmu_mlist_enter(pp); 5845 pahmep->flags |= flag; 5846 goto top; 5847 } 5848 5849 pahmep->flags |= flag; 5850 } 5851 5852 if (locked) 5853 sfmmu_mlist_exit(pml); 5854 5855 return (0); 5856 } 5857 5858 /* 5859 * Execute post-callback handler of each pa_hment linked to pp 5860 * 5861 * Same overall assumptions and restrictions apply as for 5862 * hat_pageprocess_precallbacks(). 5863 */ 5864 static void 5865 hat_pageprocess_postcallbacks(struct page *pp, uint_t flag) 5866 { 5867 pfn_t pgpfn = pp->p_pagenum; 5868 pfn_t pgmask = btop(page_get_pagesize(pp->p_szc)) - 1; 5869 pfn_t newpfn; 5870 struct sf_hment *sfhmep; 5871 struct pa_hment *pahmep; 5872 int (*f)(caddr_t, uint_t, uint_t, void *, pfn_t); 5873 id_t id; 5874 int locked = 0; 5875 kmutex_t *pml; 5876 5877 ASSERT(PAGE_EXCL(pp)); 5878 if (!sfmmu_mlist_held(pp)) { 5879 pml = sfmmu_mlist_enter(pp); 5880 locked = 1; 5881 } 5882 5883 top: 5884 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) { 5885 /* 5886 * skip sf_hments corresponding to VA<->PA mappings; 5887 * for pa_hment's, hme_tte.ll is zero 5888 */ 5889 if (!IS_PAHME(sfhmep)) 5890 continue; 5891 5892 pahmep = sfhmep->hme_data; 5893 ASSERT(pahmep != NULL); 5894 5895 if ((pahmep->flags & flag) == 0) 5896 continue; 5897 5898 pahmep->flags &= ~flag; 5899 5900 id = pahmep->cb_id; 5901 ASSERT(id >= (id_t)0 && id < sfmmu_cb_nextid); 5902 if ((f = sfmmu_cb_table[id].posthandler) == NULL) 5903 continue; 5904 5905 /* 5906 * Convert the base page PFN into the constituent PFN 5907 * which is needed by the callback handler. 5908 */ 5909 newpfn = pgpfn | (btop((uintptr_t)pahmep->addr) & pgmask); 5910 5911 /* 5912 * Drop the mapping list lock to avoid locking order issues. 5913 */ 5914 if (locked) 5915 sfmmu_mlist_exit(pml); 5916 5917 if (f(pahmep->addr, pahmep->len, flag, pahmep->pvt, newpfn) 5918 != 0) 5919 panic("sfmmu: posthandler failed"); 5920 5921 if (locked) { 5922 pml = sfmmu_mlist_enter(pp); 5923 goto top; 5924 } 5925 } 5926 5927 if (locked) 5928 sfmmu_mlist_exit(pml); 5929 } 5930 5931 /* 5932 * Suspend locked kernel mapping 5933 */ 5934 void 5935 hat_pagesuspend(struct page *pp) 5936 { 5937 struct sf_hment *sfhmep; 5938 sfmmu_t *sfmmup; 5939 tte_t tte, ttemod; 5940 struct hme_blk *hmeblkp; 5941 caddr_t addr; 5942 int index, cons; 5943 cpuset_t cpuset; 5944 5945 ASSERT(PAGE_EXCL(pp)); 5946 ASSERT(sfmmu_mlist_held(pp)); 5947 5948 mutex_enter(&kpr_suspendlock); 5949 5950 /* 5951 * Call into dtrace to tell it we're about to suspend a 5952 * kernel mapping. This prevents us from running into issues 5953 * with probe context trying to touch a suspended page 5954 * in the relocation codepath itself. 5955 */ 5956 if (dtrace_kreloc_init) 5957 (*dtrace_kreloc_init)(); 5958 5959 index = PP_MAPINDEX(pp); 5960 cons = TTE8K; 5961 5962 retry: 5963 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) { 5964 5965 if (IS_PAHME(sfhmep)) 5966 continue; 5967 5968 if (get_hblk_ttesz(sfmmu_hmetohblk(sfhmep)) != cons) 5969 continue; 5970 5971 /* 5972 * Loop until we successfully set the suspend bit in 5973 * the TTE. 5974 */ 5975 again: 5976 sfmmu_copytte(&sfhmep->hme_tte, &tte); 5977 ASSERT(TTE_IS_VALID(&tte)); 5978 5979 ttemod = tte; 5980 TTE_SET_SUSPEND(&ttemod); 5981 if (sfmmu_modifytte_try(&tte, &ttemod, 5982 &sfhmep->hme_tte) < 0) 5983 goto again; 5984 5985 /* 5986 * Invalidate TSB entry 5987 */ 5988 hmeblkp = sfmmu_hmetohblk(sfhmep); 5989 5990 sfmmup = hblktosfmmu(hmeblkp); 5991 ASSERT(sfmmup == ksfmmup); 5992 5993 addr = tte_to_vaddr(hmeblkp, tte); 5994 5995 /* 5996 * No need to make sure that the TSB for this sfmmu is 5997 * not being relocated since it is ksfmmup and thus it 5998 * will never be relocated. 5999 */ 6000 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp); 6001 6002 /* 6003 * Update xcall stats 6004 */ 6005 cpuset = cpu_ready_set; 6006 CPUSET_DEL(cpuset, CPU->cpu_id); 6007 6008 /* LINTED: constant in conditional context */ 6009 SFMMU_XCALL_STATS(ksfmmup); 6010 6011 /* 6012 * Flush TLB entry on remote CPU's 6013 */ 6014 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr, 6015 (uint64_t)ksfmmup); 6016 xt_sync(cpuset); 6017 6018 /* 6019 * Flush TLB entry on local CPU 6020 */ 6021 vtag_flushpage(addr, (uint64_t)ksfmmup); 6022 } 6023 6024 while (index != 0) { 6025 index = index >> 1; 6026 if (index != 0) 6027 cons++; 6028 if (index & 0x1) { 6029 pp = PP_GROUPLEADER(pp, cons); 6030 goto retry; 6031 } 6032 } 6033 } 6034 6035 #ifdef DEBUG 6036 6037 #define N_PRLE 1024 6038 struct prle { 6039 page_t *targ; 6040 page_t *repl; 6041 int status; 6042 int pausecpus; 6043 hrtime_t whence; 6044 }; 6045 6046 static struct prle page_relocate_log[N_PRLE]; 6047 static int prl_entry; 6048 static kmutex_t prl_mutex; 6049 6050 #define PAGE_RELOCATE_LOG(t, r, s, p) \ 6051 mutex_enter(&prl_mutex); \ 6052 page_relocate_log[prl_entry].targ = *(t); \ 6053 page_relocate_log[prl_entry].repl = *(r); \ 6054 page_relocate_log[prl_entry].status = (s); \ 6055 page_relocate_log[prl_entry].pausecpus = (p); \ 6056 page_relocate_log[prl_entry].whence = gethrtime(); \ 6057 prl_entry = (prl_entry == (N_PRLE - 1))? 0 : prl_entry + 1; \ 6058 mutex_exit(&prl_mutex); 6059 6060 #else /* !DEBUG */ 6061 #define PAGE_RELOCATE_LOG(t, r, s, p) 6062 #endif 6063 6064 /* 6065 * Core Kernel Page Relocation Algorithm 6066 * 6067 * Input: 6068 * 6069 * target : constituent pages are SE_EXCL locked. 6070 * replacement: constituent pages are SE_EXCL locked. 6071 * 6072 * Output: 6073 * 6074 * nrelocp: number of pages relocated 6075 */ 6076 int 6077 hat_page_relocate(page_t **target, page_t **replacement, spgcnt_t *nrelocp) 6078 { 6079 page_t *targ, *repl; 6080 page_t *tpp, *rpp; 6081 kmutex_t *low, *high; 6082 spgcnt_t npages, i; 6083 page_t *pl = NULL; 6084 int old_pil; 6085 cpuset_t cpuset; 6086 int cap_cpus; 6087 int ret; 6088 6089 if (hat_kpr_enabled == 0 || !kcage_on || PP_ISNORELOC(*target)) { 6090 PAGE_RELOCATE_LOG(target, replacement, EAGAIN, -1); 6091 return (EAGAIN); 6092 } 6093 6094 mutex_enter(&kpr_mutex); 6095 kreloc_thread = curthread; 6096 6097 targ = *target; 6098 repl = *replacement; 6099 ASSERT(repl != NULL); 6100 ASSERT(targ->p_szc == repl->p_szc); 6101 6102 npages = page_get_pagecnt(targ->p_szc); 6103 6104 /* 6105 * unload VA<->PA mappings that are not locked 6106 */ 6107 tpp = targ; 6108 for (i = 0; i < npages; i++) { 6109 (void) hat_pageunload(tpp, SFMMU_KERNEL_RELOC); 6110 tpp++; 6111 } 6112 6113 /* 6114 * Do "presuspend" callbacks, in a context from which we can still 6115 * block as needed. Note that we don't hold the mapping list lock 6116 * of "targ" at this point due to potential locking order issues; 6117 * we assume that between the hat_pageunload() above and holding 6118 * the SE_EXCL lock that the mapping list *cannot* change at this 6119 * point. 6120 */ 6121 ret = hat_pageprocess_precallbacks(targ, HAT_PRESUSPEND, &cap_cpus); 6122 if (ret != 0) { 6123 /* 6124 * EIO translates to fatal error, for all others cleanup 6125 * and return EAGAIN. 6126 */ 6127 ASSERT(ret != EIO); 6128 hat_pageprocess_postcallbacks(targ, HAT_POSTUNSUSPEND); 6129 PAGE_RELOCATE_LOG(target, replacement, ret, -1); 6130 kreloc_thread = NULL; 6131 mutex_exit(&kpr_mutex); 6132 return (EAGAIN); 6133 } 6134 6135 /* 6136 * acquire p_mapping list lock for both the target and replacement 6137 * root pages. 6138 * 6139 * low and high refer to the need to grab the mlist locks in a 6140 * specific order in order to prevent race conditions. Thus the 6141 * lower lock must be grabbed before the higher lock. 6142 * 6143 * This will block hat_unload's accessing p_mapping list. Since 6144 * we have SE_EXCL lock, hat_memload and hat_pageunload will be 6145 * blocked. Thus, no one else will be accessing the p_mapping list 6146 * while we suspend and reload the locked mapping below. 6147 */ 6148 tpp = targ; 6149 rpp = repl; 6150 sfmmu_mlist_reloc_enter(tpp, rpp, &low, &high); 6151 6152 kpreempt_disable(); 6153 6154 #ifdef VAC 6155 /* 6156 * If the replacement page is of a different virtual color 6157 * than the page it is replacing, we need to handle the VAC 6158 * consistency for it just as we would if we were setting up 6159 * a new mapping to a page. 6160 */ 6161 if ((tpp->p_szc == 0) && (PP_GET_VCOLOR(rpp) != NO_VCOLOR)) { 6162 if (tpp->p_vcolor != rpp->p_vcolor) { 6163 sfmmu_cache_flushcolor(PP_GET_VCOLOR(rpp), 6164 rpp->p_pagenum); 6165 } 6166 } 6167 #endif 6168 6169 /* 6170 * We raise our PIL to 13 so that we don't get captured by 6171 * another CPU or pinned by an interrupt thread. We can't go to 6172 * PIL 14 since the nexus driver(s) may need to interrupt at 6173 * that level in the case of IOMMU pseudo mappings. 6174 */ 6175 cpuset = cpu_ready_set; 6176 CPUSET_DEL(cpuset, CPU->cpu_id); 6177 if (!cap_cpus || CPUSET_ISNULL(cpuset)) { 6178 old_pil = splr(XCALL_PIL); 6179 } else { 6180 old_pil = -1; 6181 xc_attention(cpuset); 6182 } 6183 ASSERT(getpil() == XCALL_PIL); 6184 6185 /* 6186 * Now do suspend callbacks. In the case of an IOMMU mapping 6187 * this will suspend all DMA activity to the page while it is 6188 * being relocated. Since we are well above LOCK_LEVEL and CPUs 6189 * may be captured at this point we should have acquired any needed 6190 * locks in the presuspend callback. 6191 */ 6192 ret = hat_pageprocess_precallbacks(targ, HAT_SUSPEND, NULL); 6193 if (ret != 0) { 6194 repl = targ; 6195 goto suspend_fail; 6196 } 6197 6198 /* 6199 * Raise the PIL yet again, this time to block all high-level 6200 * interrupts on this CPU. This is necessary to prevent an 6201 * interrupt routine from pinning the thread which holds the 6202 * mapping suspended and then touching the suspended page. 6203 * 6204 * Once the page is suspended we also need to be careful to 6205 * avoid calling any functions which touch any seg_kmem memory 6206 * since that memory may be backed by the very page we are 6207 * relocating in here! 6208 */ 6209 hat_pagesuspend(targ); 6210 6211 /* 6212 * Now that we are confident everybody has stopped using this page, 6213 * copy the page contents. Note we use a physical copy to prevent 6214 * locking issues and to avoid fpRAS because we can't handle it in 6215 * this context. 6216 */ 6217 for (i = 0; i < npages; i++, tpp++, rpp++) { 6218 /* 6219 * Copy the contents of the page. 6220 */ 6221 ppcopy_kernel(tpp, rpp); 6222 } 6223 6224 tpp = targ; 6225 rpp = repl; 6226 for (i = 0; i < npages; i++, tpp++, rpp++) { 6227 /* 6228 * Copy attributes. VAC consistency was handled above, 6229 * if required. 6230 */ 6231 rpp->p_nrm = tpp->p_nrm; 6232 tpp->p_nrm = 0; 6233 rpp->p_index = tpp->p_index; 6234 tpp->p_index = 0; 6235 #ifdef VAC 6236 rpp->p_vcolor = tpp->p_vcolor; 6237 #endif 6238 } 6239 6240 /* 6241 * First, unsuspend the page, if we set the suspend bit, and transfer 6242 * the mapping list from the target page to the replacement page. 6243 * Next process postcallbacks; since pa_hment's are linked only to the 6244 * p_mapping list of root page, we don't iterate over the constituent 6245 * pages. 6246 */ 6247 hat_pagereload(targ, repl); 6248 6249 suspend_fail: 6250 hat_pageprocess_postcallbacks(repl, HAT_UNSUSPEND); 6251 6252 /* 6253 * Now lower our PIL and release any captured CPUs since we 6254 * are out of the "danger zone". After this it will again be 6255 * safe to acquire adaptive mutex locks, or to drop them... 6256 */ 6257 if (old_pil != -1) { 6258 splx(old_pil); 6259 } else { 6260 xc_dismissed(cpuset); 6261 } 6262 6263 kpreempt_enable(); 6264 6265 sfmmu_mlist_reloc_exit(low, high); 6266 6267 /* 6268 * Postsuspend callbacks should drop any locks held across 6269 * the suspend callbacks. As before, we don't hold the mapping 6270 * list lock at this point.. our assumption is that the mapping 6271 * list still can't change due to our holding SE_EXCL lock and 6272 * there being no unlocked mappings left. Hence the restriction 6273 * on calling context to hat_delete_callback() 6274 */ 6275 hat_pageprocess_postcallbacks(repl, HAT_POSTUNSUSPEND); 6276 if (ret != 0) { 6277 /* 6278 * The second presuspend call failed: we got here through 6279 * the suspend_fail label above. 6280 */ 6281 ASSERT(ret != EIO); 6282 PAGE_RELOCATE_LOG(target, replacement, ret, cap_cpus); 6283 kreloc_thread = NULL; 6284 mutex_exit(&kpr_mutex); 6285 return (EAGAIN); 6286 } 6287 6288 /* 6289 * Now that we're out of the performance critical section we can 6290 * take care of updating the hash table, since we still 6291 * hold all the pages locked SE_EXCL at this point we 6292 * needn't worry about things changing out from under us. 6293 */ 6294 tpp = targ; 6295 rpp = repl; 6296 for (i = 0; i < npages; i++, tpp++, rpp++) { 6297 6298 /* 6299 * replace targ with replacement in page_hash table 6300 */ 6301 targ = tpp; 6302 page_relocate_hash(rpp, targ); 6303 6304 /* 6305 * concatenate target; caller of platform_page_relocate() 6306 * expects target to be concatenated after returning. 6307 */ 6308 ASSERT(targ->p_next == targ); 6309 ASSERT(targ->p_prev == targ); 6310 page_list_concat(&pl, &targ); 6311 } 6312 6313 ASSERT(*target == pl); 6314 *nrelocp = npages; 6315 PAGE_RELOCATE_LOG(target, replacement, 0, cap_cpus); 6316 kreloc_thread = NULL; 6317 mutex_exit(&kpr_mutex); 6318 return (0); 6319 } 6320 6321 /* 6322 * Called when stray pa_hments are found attached to a page which is 6323 * being freed. Notify the subsystem which attached the pa_hment of 6324 * the error if it registered a suitable handler, else panic. 6325 */ 6326 static void 6327 sfmmu_pahment_leaked(struct pa_hment *pahmep) 6328 { 6329 id_t cb_id = pahmep->cb_id; 6330 6331 ASSERT(cb_id >= (id_t)0 && cb_id < sfmmu_cb_nextid); 6332 if (sfmmu_cb_table[cb_id].errhandler != NULL) { 6333 if (sfmmu_cb_table[cb_id].errhandler(pahmep->addr, pahmep->len, 6334 HAT_CB_ERR_LEAKED, pahmep->pvt) == 0) 6335 return; /* non-fatal */ 6336 } 6337 panic("pa_hment leaked: 0x%p", pahmep); 6338 } 6339 6340 /* 6341 * Remove all mappings to page 'pp'. 6342 */ 6343 int 6344 hat_pageunload(struct page *pp, uint_t forceflag) 6345 { 6346 struct page *origpp = pp; 6347 struct sf_hment *sfhme, *tmphme; 6348 struct hme_blk *hmeblkp; 6349 kmutex_t *pml; 6350 #ifdef VAC 6351 kmutex_t *pmtx; 6352 #endif 6353 cpuset_t cpuset, tset; 6354 int index, cons; 6355 int xhme_blks; 6356 int pa_hments; 6357 6358 ASSERT(PAGE_EXCL(pp)); 6359 6360 retry_xhat: 6361 tmphme = NULL; 6362 xhme_blks = 0; 6363 pa_hments = 0; 6364 CPUSET_ZERO(cpuset); 6365 6366 pml = sfmmu_mlist_enter(pp); 6367 6368 #ifdef VAC 6369 if (pp->p_kpmref) 6370 sfmmu_kpm_pageunload(pp); 6371 ASSERT(!PP_ISMAPPED_KPM(pp)); 6372 #endif 6373 6374 index = PP_MAPINDEX(pp); 6375 cons = TTE8K; 6376 retry: 6377 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 6378 tmphme = sfhme->hme_next; 6379 6380 if (IS_PAHME(sfhme)) { 6381 ASSERT(sfhme->hme_data != NULL); 6382 pa_hments++; 6383 continue; 6384 } 6385 6386 hmeblkp = sfmmu_hmetohblk(sfhme); 6387 if (hmeblkp->hblk_xhat_bit) { 6388 struct xhat_hme_blk *xblk = 6389 (struct xhat_hme_blk *)hmeblkp; 6390 6391 (void) XHAT_PAGEUNLOAD(xblk->xhat_hme_blk_hat, 6392 pp, forceflag, XBLK2PROVBLK(xblk)); 6393 6394 xhme_blks = 1; 6395 continue; 6396 } 6397 6398 /* 6399 * If there are kernel mappings don't unload them, they will 6400 * be suspended. 6401 */ 6402 if (forceflag == SFMMU_KERNEL_RELOC && hmeblkp->hblk_lckcnt && 6403 hmeblkp->hblk_tag.htag_id == ksfmmup) 6404 continue; 6405 6406 tset = sfmmu_pageunload(pp, sfhme, cons); 6407 CPUSET_OR(cpuset, tset); 6408 } 6409 6410 while (index != 0) { 6411 index = index >> 1; 6412 if (index != 0) 6413 cons++; 6414 if (index & 0x1) { 6415 /* Go to leading page */ 6416 pp = PP_GROUPLEADER(pp, cons); 6417 ASSERT(sfmmu_mlist_held(pp)); 6418 goto retry; 6419 } 6420 } 6421 6422 /* 6423 * cpuset may be empty if the page was only mapped by segkpm, 6424 * in which case we won't actually cross-trap. 6425 */ 6426 xt_sync(cpuset); 6427 6428 /* 6429 * The page should have no mappings at this point, unless 6430 * we were called from hat_page_relocate() in which case we 6431 * leave the locked mappings which will be suspended later. 6432 */ 6433 ASSERT(!PP_ISMAPPED(origpp) || xhme_blks || pa_hments || 6434 (forceflag == SFMMU_KERNEL_RELOC)); 6435 6436 #ifdef VAC 6437 if (PP_ISTNC(pp)) { 6438 if (cons == TTE8K) { 6439 pmtx = sfmmu_page_enter(pp); 6440 PP_CLRTNC(pp); 6441 sfmmu_page_exit(pmtx); 6442 } else { 6443 conv_tnc(pp, cons); 6444 } 6445 } 6446 #endif /* VAC */ 6447 6448 if (pa_hments && forceflag != SFMMU_KERNEL_RELOC) { 6449 /* 6450 * Unlink any pa_hments and free them, calling back 6451 * the responsible subsystem to notify it of the error. 6452 * This can occur in situations such as drivers leaking 6453 * DMA handles: naughty, but common enough that we'd like 6454 * to keep the system running rather than bringing it 6455 * down with an obscure error like "pa_hment leaked" 6456 * which doesn't aid the user in debugging their driver. 6457 */ 6458 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 6459 tmphme = sfhme->hme_next; 6460 if (IS_PAHME(sfhme)) { 6461 struct pa_hment *pahmep = sfhme->hme_data; 6462 sfmmu_pahment_leaked(pahmep); 6463 HME_SUB(sfhme, pp); 6464 kmem_cache_free(pa_hment_cache, pahmep); 6465 } 6466 } 6467 6468 ASSERT(!PP_ISMAPPED(origpp) || xhme_blks); 6469 } 6470 6471 sfmmu_mlist_exit(pml); 6472 6473 /* 6474 * XHAT may not have finished unloading pages 6475 * because some other thread was waiting for 6476 * mlist lock and XHAT_PAGEUNLOAD let it do 6477 * the job. 6478 */ 6479 if (xhme_blks) { 6480 pp = origpp; 6481 goto retry_xhat; 6482 } 6483 6484 return (0); 6485 } 6486 6487 cpuset_t 6488 sfmmu_pageunload(page_t *pp, struct sf_hment *sfhme, int cons) 6489 { 6490 struct hme_blk *hmeblkp; 6491 sfmmu_t *sfmmup; 6492 tte_t tte, ttemod; 6493 #ifdef DEBUG 6494 tte_t orig_old; 6495 #endif /* DEBUG */ 6496 caddr_t addr; 6497 int ttesz; 6498 int ret; 6499 cpuset_t cpuset; 6500 6501 ASSERT(pp != NULL); 6502 ASSERT(sfmmu_mlist_held(pp)); 6503 ASSERT(pp->p_vnode != &kvp); 6504 6505 CPUSET_ZERO(cpuset); 6506 6507 hmeblkp = sfmmu_hmetohblk(sfhme); 6508 6509 readtte: 6510 sfmmu_copytte(&sfhme->hme_tte, &tte); 6511 if (TTE_IS_VALID(&tte)) { 6512 sfmmup = hblktosfmmu(hmeblkp); 6513 ttesz = get_hblk_ttesz(hmeblkp); 6514 /* 6515 * Only unload mappings of 'cons' size. 6516 */ 6517 if (ttesz != cons) 6518 return (cpuset); 6519 6520 /* 6521 * Note that we have p_mapping lock, but no hash lock here. 6522 * hblk_unload() has to have both hash lock AND p_mapping 6523 * lock before it tries to modify tte. So, the tte could 6524 * not become invalid in the sfmmu_modifytte_try() below. 6525 */ 6526 ttemod = tte; 6527 #ifdef DEBUG 6528 orig_old = tte; 6529 #endif /* DEBUG */ 6530 6531 TTE_SET_INVALID(&ttemod); 6532 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte); 6533 if (ret < 0) { 6534 #ifdef DEBUG 6535 /* only R/M bits can change. */ 6536 chk_tte(&orig_old, &tte, &ttemod, hmeblkp); 6537 #endif /* DEBUG */ 6538 goto readtte; 6539 } 6540 6541 if (ret == 0) { 6542 panic("pageunload: cas failed?"); 6543 } 6544 6545 addr = tte_to_vaddr(hmeblkp, tte); 6546 6547 sfmmu_ttesync(sfmmup, addr, &tte, pp); 6548 6549 atomic_add_long(&sfmmup->sfmmu_ttecnt[ttesz], -1); 6550 6551 /* 6552 * We need to flush the page from the virtual cache 6553 * in order to prevent a virtual cache alias 6554 * inconsistency. The particular scenario we need 6555 * to worry about is: 6556 * Given: va1 and va2 are two virtual address that 6557 * alias and will map the same physical address. 6558 * 1. mapping exists from va1 to pa and data has 6559 * been read into the cache. 6560 * 2. unload va1. 6561 * 3. load va2 and modify data using va2. 6562 * 4 unload va2. 6563 * 5. load va1 and reference data. Unless we flush 6564 * the data cache when we unload we will get 6565 * stale data. 6566 * This scenario is taken care of by using virtual 6567 * page coloring. 6568 */ 6569 if (sfmmup->sfmmu_ismhat) { 6570 /* 6571 * Flush TSBs, TLBs and caches 6572 * of every process 6573 * sharing this ism segment. 6574 */ 6575 sfmmu_hat_lock_all(); 6576 mutex_enter(&ism_mlist_lock); 6577 kpreempt_disable(); 6578 if (do_virtual_coloring) 6579 sfmmu_ismtlbcache_demap(addr, sfmmup, hmeblkp, 6580 pp->p_pagenum, CACHE_NO_FLUSH); 6581 else 6582 sfmmu_ismtlbcache_demap(addr, sfmmup, hmeblkp, 6583 pp->p_pagenum, CACHE_FLUSH); 6584 kpreempt_enable(); 6585 mutex_exit(&ism_mlist_lock); 6586 sfmmu_hat_unlock_all(); 6587 cpuset = cpu_ready_set; 6588 } else if (do_virtual_coloring) { 6589 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 6590 cpuset = sfmmup->sfmmu_cpusran; 6591 } else { 6592 sfmmu_tlbcache_demap(addr, sfmmup, hmeblkp, 6593 pp->p_pagenum, 0, FLUSH_NECESSARY_CPUS, 6594 CACHE_FLUSH, 0); 6595 cpuset = sfmmup->sfmmu_cpusran; 6596 } 6597 6598 /* 6599 * Hme_sub has to run after ttesync() and a_rss update. 6600 * See hblk_unload(). 6601 */ 6602 HME_SUB(sfhme, pp); 6603 membar_stst(); 6604 6605 /* 6606 * We can not make ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS) 6607 * since pteload may have done a HME_ADD() right after 6608 * we did the HME_SUB() above. Hmecnt is now maintained 6609 * by cas only. no lock guranteed its value. The only 6610 * gurantee we have is the hmecnt should not be less than 6611 * what it should be so the hblk will not be taken away. 6612 * It's also important that we decremented the hmecnt after 6613 * we are done with hmeblkp so that this hmeblk won't be 6614 * stolen. 6615 */ 6616 ASSERT(hmeblkp->hblk_hmecnt > 0); 6617 ASSERT(hmeblkp->hblk_vcnt > 0); 6618 atomic_add_16(&hmeblkp->hblk_vcnt, -1); 6619 atomic_add_16(&hmeblkp->hblk_hmecnt, -1); 6620 /* 6621 * This is bug 4063182. 6622 * XXX: fixme 6623 * ASSERT(hmeblkp->hblk_hmecnt || hmeblkp->hblk_vcnt || 6624 * !hmeblkp->hblk_lckcnt); 6625 */ 6626 } else { 6627 panic("invalid tte? pp %p &tte %p", 6628 (void *)pp, (void *)&tte); 6629 } 6630 6631 return (cpuset); 6632 } 6633 6634 /* 6635 * While relocating a kernel page, this function will move the mappings 6636 * from tpp to dpp and modify any associated data with these mappings. 6637 * It also unsuspends the suspended kernel mapping. 6638 */ 6639 static void 6640 hat_pagereload(struct page *tpp, struct page *dpp) 6641 { 6642 struct sf_hment *sfhme; 6643 tte_t tte, ttemod; 6644 int index, cons; 6645 6646 ASSERT(getpil() == PIL_MAX); 6647 ASSERT(sfmmu_mlist_held(tpp)); 6648 ASSERT(sfmmu_mlist_held(dpp)); 6649 6650 index = PP_MAPINDEX(tpp); 6651 cons = TTE8K; 6652 6653 /* Update real mappings to the page */ 6654 retry: 6655 for (sfhme = tpp->p_mapping; sfhme != NULL; sfhme = sfhme->hme_next) { 6656 if (IS_PAHME(sfhme)) 6657 continue; 6658 sfmmu_copytte(&sfhme->hme_tte, &tte); 6659 ttemod = tte; 6660 6661 /* 6662 * replace old pfn with new pfn in TTE 6663 */ 6664 PFN_TO_TTE(ttemod, dpp->p_pagenum); 6665 6666 /* 6667 * clear suspend bit 6668 */ 6669 ASSERT(TTE_IS_SUSPEND(&ttemod)); 6670 TTE_CLR_SUSPEND(&ttemod); 6671 6672 if (sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte) < 0) 6673 panic("hat_pagereload(): sfmmu_modifytte_try() failed"); 6674 6675 /* 6676 * set hme_page point to new page 6677 */ 6678 sfhme->hme_page = dpp; 6679 } 6680 6681 /* 6682 * move p_mapping list from old page to new page 6683 */ 6684 dpp->p_mapping = tpp->p_mapping; 6685 tpp->p_mapping = NULL; 6686 dpp->p_share = tpp->p_share; 6687 tpp->p_share = 0; 6688 6689 while (index != 0) { 6690 index = index >> 1; 6691 if (index != 0) 6692 cons++; 6693 if (index & 0x1) { 6694 tpp = PP_GROUPLEADER(tpp, cons); 6695 dpp = PP_GROUPLEADER(dpp, cons); 6696 goto retry; 6697 } 6698 } 6699 6700 if (dtrace_kreloc_fini) 6701 (*dtrace_kreloc_fini)(); 6702 mutex_exit(&kpr_suspendlock); 6703 } 6704 6705 uint_t 6706 hat_pagesync(struct page *pp, uint_t clearflag) 6707 { 6708 struct sf_hment *sfhme, *tmphme = NULL; 6709 struct hme_blk *hmeblkp; 6710 kmutex_t *pml; 6711 cpuset_t cpuset, tset; 6712 int index, cons; 6713 extern ulong_t po_share; 6714 page_t *save_pp = pp; 6715 6716 CPUSET_ZERO(cpuset); 6717 6718 if (PP_ISRO(pp) && (clearflag & HAT_SYNC_STOPON_MOD)) { 6719 return (PP_GENERIC_ATTR(pp)); 6720 } 6721 6722 if ((clearflag == (HAT_SYNC_STOPON_REF | HAT_SYNC_DONTZERO)) && 6723 PP_ISREF(pp)) { 6724 return (PP_GENERIC_ATTR(pp)); 6725 } 6726 6727 if ((clearflag == (HAT_SYNC_STOPON_MOD | HAT_SYNC_DONTZERO)) && 6728 PP_ISMOD(pp)) { 6729 return (PP_GENERIC_ATTR(pp)); 6730 } 6731 6732 if ((clearflag & HAT_SYNC_STOPON_SHARED) != 0 && 6733 (pp->p_share > po_share) && 6734 !(clearflag & HAT_SYNC_ZERORM)) { 6735 if (PP_ISRO(pp)) 6736 hat_page_setattr(pp, P_REF); 6737 return (PP_GENERIC_ATTR(pp)); 6738 } 6739 6740 clearflag &= ~HAT_SYNC_STOPON_SHARED; 6741 pml = sfmmu_mlist_enter(pp); 6742 index = PP_MAPINDEX(pp); 6743 cons = TTE8K; 6744 retry: 6745 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 6746 /* 6747 * We need to save the next hment on the list since 6748 * it is possible for pagesync to remove an invalid hment 6749 * from the list. 6750 */ 6751 tmphme = sfhme->hme_next; 6752 /* 6753 * If we are looking for large mappings and this hme doesn't 6754 * reach the range we are seeking, just ignore its. 6755 */ 6756 hmeblkp = sfmmu_hmetohblk(sfhme); 6757 if (hmeblkp->hblk_xhat_bit) 6758 continue; 6759 6760 if (hme_size(sfhme) < cons) 6761 continue; 6762 tset = sfmmu_pagesync(pp, sfhme, 6763 clearflag & ~HAT_SYNC_STOPON_RM); 6764 CPUSET_OR(cpuset, tset); 6765 /* 6766 * If clearflag is HAT_SYNC_DONTZERO, break out as soon 6767 * as the "ref" or "mod" is set. 6768 */ 6769 if ((clearflag & ~HAT_SYNC_STOPON_RM) == HAT_SYNC_DONTZERO && 6770 ((clearflag & HAT_SYNC_STOPON_MOD) && PP_ISMOD(save_pp)) || 6771 ((clearflag & HAT_SYNC_STOPON_REF) && PP_ISREF(save_pp))) { 6772 index = 0; 6773 break; 6774 } 6775 } 6776 6777 while (index) { 6778 index = index >> 1; 6779 cons++; 6780 if (index & 0x1) { 6781 /* Go to leading page */ 6782 pp = PP_GROUPLEADER(pp, cons); 6783 goto retry; 6784 } 6785 } 6786 6787 xt_sync(cpuset); 6788 sfmmu_mlist_exit(pml); 6789 return (PP_GENERIC_ATTR(save_pp)); 6790 } 6791 6792 /* 6793 * Get all the hardware dependent attributes for a page struct 6794 */ 6795 static cpuset_t 6796 sfmmu_pagesync(struct page *pp, struct sf_hment *sfhme, 6797 uint_t clearflag) 6798 { 6799 caddr_t addr; 6800 tte_t tte, ttemod; 6801 struct hme_blk *hmeblkp; 6802 int ret; 6803 sfmmu_t *sfmmup; 6804 cpuset_t cpuset; 6805 6806 ASSERT(pp != NULL); 6807 ASSERT(sfmmu_mlist_held(pp)); 6808 ASSERT((clearflag == HAT_SYNC_DONTZERO) || 6809 (clearflag == HAT_SYNC_ZERORM)); 6810 6811 SFMMU_STAT(sf_pagesync); 6812 6813 CPUSET_ZERO(cpuset); 6814 6815 sfmmu_pagesync_retry: 6816 6817 sfmmu_copytte(&sfhme->hme_tte, &tte); 6818 if (TTE_IS_VALID(&tte)) { 6819 hmeblkp = sfmmu_hmetohblk(sfhme); 6820 sfmmup = hblktosfmmu(hmeblkp); 6821 addr = tte_to_vaddr(hmeblkp, tte); 6822 if (clearflag == HAT_SYNC_ZERORM) { 6823 ttemod = tte; 6824 TTE_CLR_RM(&ttemod); 6825 ret = sfmmu_modifytte_try(&tte, &ttemod, 6826 &sfhme->hme_tte); 6827 if (ret < 0) { 6828 /* 6829 * cas failed and the new value is not what 6830 * we want. 6831 */ 6832 goto sfmmu_pagesync_retry; 6833 } 6834 6835 if (ret > 0) { 6836 /* we win the cas */ 6837 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 6838 cpuset = sfmmup->sfmmu_cpusran; 6839 } 6840 } 6841 6842 sfmmu_ttesync(sfmmup, addr, &tte, pp); 6843 } 6844 return (cpuset); 6845 } 6846 6847 /* 6848 * Remove write permission from a mappings to a page, so that 6849 * we can detect the next modification of it. This requires modifying 6850 * the TTE then invalidating (demap) any TLB entry using that TTE. 6851 * This code is similar to sfmmu_pagesync(). 6852 */ 6853 static cpuset_t 6854 sfmmu_pageclrwrt(struct page *pp, struct sf_hment *sfhme) 6855 { 6856 caddr_t addr; 6857 tte_t tte; 6858 tte_t ttemod; 6859 struct hme_blk *hmeblkp; 6860 int ret; 6861 sfmmu_t *sfmmup; 6862 cpuset_t cpuset; 6863 6864 ASSERT(pp != NULL); 6865 ASSERT(sfmmu_mlist_held(pp)); 6866 6867 CPUSET_ZERO(cpuset); 6868 SFMMU_STAT(sf_clrwrt); 6869 6870 retry: 6871 6872 sfmmu_copytte(&sfhme->hme_tte, &tte); 6873 if (TTE_IS_VALID(&tte) && TTE_IS_WRITABLE(&tte)) { 6874 hmeblkp = sfmmu_hmetohblk(sfhme); 6875 6876 /* 6877 * xhat mappings should never be to a VMODSORT page. 6878 */ 6879 ASSERT(hmeblkp->hblk_xhat_bit == 0); 6880 6881 sfmmup = hblktosfmmu(hmeblkp); 6882 addr = tte_to_vaddr(hmeblkp, tte); 6883 6884 ttemod = tte; 6885 TTE_CLR_WRT(&ttemod); 6886 TTE_CLR_MOD(&ttemod); 6887 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte); 6888 6889 /* 6890 * if cas failed and the new value is not what 6891 * we want retry 6892 */ 6893 if (ret < 0) 6894 goto retry; 6895 6896 /* we win the cas */ 6897 if (ret > 0) { 6898 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 6899 cpuset = sfmmup->sfmmu_cpusran; 6900 } 6901 } 6902 6903 return (cpuset); 6904 } 6905 6906 /* 6907 * Walk all mappings of a page, removing write permission and clearing the 6908 * ref/mod bits. This code is similar to hat_pagesync() 6909 */ 6910 static void 6911 hat_page_clrwrt(page_t *pp) 6912 { 6913 struct sf_hment *sfhme; 6914 struct sf_hment *tmphme = NULL; 6915 kmutex_t *pml; 6916 cpuset_t cpuset; 6917 cpuset_t tset; 6918 int index; 6919 int cons; 6920 6921 CPUSET_ZERO(cpuset); 6922 6923 pml = sfmmu_mlist_enter(pp); 6924 index = PP_MAPINDEX(pp); 6925 cons = TTE8K; 6926 retry: 6927 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 6928 tmphme = sfhme->hme_next; 6929 6930 /* 6931 * If we are looking for large mappings and this hme doesn't 6932 * reach the range we are seeking, just ignore its. 6933 */ 6934 6935 if (hme_size(sfhme) < cons) 6936 continue; 6937 6938 tset = sfmmu_pageclrwrt(pp, sfhme); 6939 CPUSET_OR(cpuset, tset); 6940 } 6941 6942 while (index) { 6943 index = index >> 1; 6944 cons++; 6945 if (index & 0x1) { 6946 /* Go to leading page */ 6947 pp = PP_GROUPLEADER(pp, cons); 6948 goto retry; 6949 } 6950 } 6951 6952 xt_sync(cpuset); 6953 sfmmu_mlist_exit(pml); 6954 } 6955 6956 /* 6957 * Set the given REF/MOD/RO bits for the given page. 6958 * For a vnode with a sorted v_pages list, we need to change 6959 * the attributes and the v_pages list together under page_vnode_mutex. 6960 */ 6961 void 6962 hat_page_setattr(page_t *pp, uint_t flag) 6963 { 6964 vnode_t *vp = pp->p_vnode; 6965 page_t **listp; 6966 kmutex_t *pmtx; 6967 kmutex_t *vphm = NULL; 6968 6969 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO))); 6970 6971 /* 6972 * nothing to do if attribute already set 6973 */ 6974 if ((pp->p_nrm & flag) == flag) 6975 return; 6976 6977 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp)) { 6978 vphm = page_vnode_mutex(vp); 6979 mutex_enter(vphm); 6980 } 6981 6982 pmtx = sfmmu_page_enter(pp); 6983 pp->p_nrm |= flag; 6984 sfmmu_page_exit(pmtx); 6985 6986 if (vphm != NULL) { 6987 /* 6988 * Some File Systems examine v_pages for NULL w/o 6989 * grabbing the vphm mutex. Must not let it become NULL when 6990 * pp is the only page on the list. 6991 */ 6992 if (pp->p_vpnext != pp) { 6993 page_vpsub(&vp->v_pages, pp); 6994 if (vp->v_pages != NULL) 6995 listp = &vp->v_pages->p_vpprev->p_vpnext; 6996 else 6997 listp = &vp->v_pages; 6998 page_vpadd(listp, pp); 6999 } 7000 mutex_exit(vphm); 7001 } 7002 } 7003 7004 void 7005 hat_page_clrattr(page_t *pp, uint_t flag) 7006 { 7007 vnode_t *vp = pp->p_vnode; 7008 kmutex_t *vphm = NULL; 7009 kmutex_t *pmtx; 7010 7011 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO))); 7012 7013 /* 7014 * For vnode with a sorted v_pages list, we need to change 7015 * the attributes and the v_pages list together under page_vnode_mutex. 7016 */ 7017 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp)) { 7018 vphm = page_vnode_mutex(vp); 7019 mutex_enter(vphm); 7020 } 7021 7022 pmtx = sfmmu_page_enter(pp); 7023 pp->p_nrm &= ~flag; 7024 sfmmu_page_exit(pmtx); 7025 7026 if (vphm != NULL) { 7027 /* 7028 * Some File Systems examine v_pages for NULL w/o 7029 * grabbing the vphm mutex. Must not let it become NULL when 7030 * pp is the only page on the list. 7031 */ 7032 if (pp->p_vpnext != pp) { 7033 page_vpsub(&vp->v_pages, pp); 7034 page_vpadd(&vp->v_pages, pp); 7035 } 7036 mutex_exit(vphm); 7037 7038 /* 7039 * VMODSORT works by removing write permissions and getting 7040 * a fault when a page is made dirty. At this point 7041 * we need to remove write permission from all mappings 7042 * to this page. 7043 */ 7044 hat_page_clrwrt(pp); 7045 } 7046 } 7047 7048 7049 uint_t 7050 hat_page_getattr(page_t *pp, uint_t flag) 7051 { 7052 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO))); 7053 return ((uint_t)(pp->p_nrm & flag)); 7054 } 7055 7056 /* 7057 * DEBUG kernels: verify that a kernel va<->pa translation 7058 * is safe by checking the underlying page_t is in a page 7059 * relocation-safe state. 7060 */ 7061 #ifdef DEBUG 7062 void 7063 sfmmu_check_kpfn(pfn_t pfn) 7064 { 7065 page_t *pp; 7066 int index, cons; 7067 7068 if (hat_check_vtop == 0) 7069 return; 7070 7071 if (hat_kpr_enabled == 0 || kvseg.s_base == NULL || panicstr) 7072 return; 7073 7074 pp = page_numtopp_nolock(pfn); 7075 if (!pp) 7076 return; 7077 7078 if (PAGE_LOCKED(pp) || PP_ISNORELOC(pp)) 7079 return; 7080 7081 /* 7082 * Handed a large kernel page, we dig up the root page since we 7083 * know the root page might have the lock also. 7084 */ 7085 if (pp->p_szc != 0) { 7086 index = PP_MAPINDEX(pp); 7087 cons = TTE8K; 7088 again: 7089 while (index != 0) { 7090 index >>= 1; 7091 if (index != 0) 7092 cons++; 7093 if (index & 0x1) { 7094 pp = PP_GROUPLEADER(pp, cons); 7095 goto again; 7096 } 7097 } 7098 } 7099 7100 if (PAGE_LOCKED(pp) || PP_ISNORELOC(pp)) 7101 return; 7102 7103 /* 7104 * Pages need to be locked or allocated "permanent" (either from 7105 * static_arena arena or explicitly setting PG_NORELOC when calling 7106 * page_create_va()) for VA->PA translations to be valid. 7107 */ 7108 if (!PP_ISNORELOC(pp)) 7109 panic("Illegal VA->PA translation, pp 0x%p not permanent", pp); 7110 else 7111 panic("Illegal VA->PA translation, pp 0x%p not locked", pp); 7112 } 7113 #endif /* DEBUG */ 7114 7115 /* 7116 * Returns a page frame number for a given virtual address. 7117 * Returns PFN_INVALID to indicate an invalid mapping 7118 */ 7119 pfn_t 7120 hat_getpfnum(struct hat *hat, caddr_t addr) 7121 { 7122 pfn_t pfn; 7123 tte_t tte; 7124 7125 /* 7126 * We would like to 7127 * ASSERT(AS_LOCK_HELD(as, &as->a_lock)); 7128 * but we can't because the iommu driver will call this 7129 * routine at interrupt time and it can't grab the as lock 7130 * or it will deadlock: A thread could have the as lock 7131 * and be waiting for io. The io can't complete 7132 * because the interrupt thread is blocked trying to grab 7133 * the as lock. 7134 */ 7135 7136 ASSERT(hat->sfmmu_xhat_provider == NULL); 7137 7138 if (hat == ksfmmup) { 7139 if (segkpm && IS_KPM_ADDR(addr)) 7140 return (sfmmu_kpm_vatopfn(addr)); 7141 while ((pfn = sfmmu_vatopfn(addr, ksfmmup, &tte)) 7142 == PFN_SUSPENDED) { 7143 sfmmu_vatopfn_suspended(addr, ksfmmup, &tte); 7144 } 7145 sfmmu_check_kpfn(pfn); 7146 return (pfn); 7147 } else { 7148 return (sfmmu_uvatopfn(addr, hat)); 7149 } 7150 } 7151 7152 /* 7153 * hat_getkpfnum() is an obsolete DDI routine, and its use is discouraged. 7154 * Use hat_getpfnum(kas.a_hat, ...) instead. 7155 * 7156 * We'd like to return PFN_INVALID if the mappings have underlying page_t's 7157 * but can't right now due to the fact that some software has grown to use 7158 * this interface incorrectly. So for now when the interface is misused, 7159 * return a warning to the user that in the future it won't work in the 7160 * way they're abusing it, and carry on (after disabling page relocation). 7161 */ 7162 pfn_t 7163 hat_getkpfnum(caddr_t addr) 7164 { 7165 pfn_t pfn; 7166 tte_t tte; 7167 int badcaller = 0; 7168 extern int segkmem_reloc; 7169 7170 if (segkpm && IS_KPM_ADDR(addr)) { 7171 badcaller = 1; 7172 pfn = sfmmu_kpm_vatopfn(addr); 7173 } else { 7174 while ((pfn = sfmmu_vatopfn(addr, ksfmmup, &tte)) 7175 == PFN_SUSPENDED) { 7176 sfmmu_vatopfn_suspended(addr, ksfmmup, &tte); 7177 } 7178 badcaller = pf_is_memory(pfn); 7179 } 7180 7181 if (badcaller) { 7182 /* 7183 * We can't return PFN_INVALID or the caller may panic 7184 * or corrupt the system. The only alternative is to 7185 * disable page relocation at this point for all kernel 7186 * memory. This will impact any callers of page_relocate() 7187 * such as FMA or DR. 7188 * 7189 * RFE: Add junk here to spit out an ereport so the sysadmin 7190 * can be advised that he should upgrade his device driver 7191 * so that this doesn't happen. 7192 */ 7193 hat_getkpfnum_badcall(caller()); 7194 if (hat_kpr_enabled && segkmem_reloc) { 7195 hat_kpr_enabled = 0; 7196 segkmem_reloc = 0; 7197 cmn_err(CE_WARN, "Kernel Page Relocation is DISABLED"); 7198 } 7199 } 7200 return (pfn); 7201 } 7202 7203 pfn_t 7204 sfmmu_uvatopfn(caddr_t vaddr, struct hat *sfmmup) 7205 { 7206 struct hmehash_bucket *hmebp; 7207 hmeblk_tag hblktag; 7208 int hmeshift, hashno = 1; 7209 struct hme_blk *hmeblkp = NULL; 7210 7211 struct sf_hment *sfhmep; 7212 tte_t tte; 7213 pfn_t pfn; 7214 7215 /* support for ISM */ 7216 ism_map_t *ism_map; 7217 ism_blk_t *ism_blkp; 7218 int i; 7219 sfmmu_t *ism_hatid = NULL; 7220 sfmmu_t *locked_hatid = NULL; 7221 7222 7223 ASSERT(sfmmup != ksfmmup); 7224 SFMMU_STAT(sf_user_vtop); 7225 /* 7226 * Set ism_hatid if vaddr falls in a ISM segment. 7227 */ 7228 ism_blkp = sfmmup->sfmmu_iblk; 7229 if (ism_blkp) { 7230 sfmmu_ismhat_enter(sfmmup, 0); 7231 locked_hatid = sfmmup; 7232 } 7233 while (ism_blkp && ism_hatid == NULL) { 7234 ism_map = ism_blkp->iblk_maps; 7235 for (i = 0; ism_map[i].imap_ismhat && i < ISM_MAP_SLOTS; i++) { 7236 if (vaddr >= ism_start(ism_map[i]) && 7237 vaddr < ism_end(ism_map[i])) { 7238 sfmmup = ism_hatid = ism_map[i].imap_ismhat; 7239 vaddr = (caddr_t)(vaddr - 7240 ism_start(ism_map[i])); 7241 break; 7242 } 7243 } 7244 ism_blkp = ism_blkp->iblk_next; 7245 } 7246 if (locked_hatid) { 7247 sfmmu_ismhat_exit(locked_hatid, 0); 7248 } 7249 7250 hblktag.htag_id = sfmmup; 7251 do { 7252 hmeshift = HME_HASH_SHIFT(hashno); 7253 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift); 7254 hblktag.htag_rehash = hashno; 7255 hmebp = HME_HASH_FUNCTION(sfmmup, vaddr, hmeshift); 7256 7257 SFMMU_HASH_LOCK(hmebp); 7258 7259 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 7260 if (hmeblkp != NULL) { 7261 HBLKTOHME(sfhmep, hmeblkp, vaddr); 7262 sfmmu_copytte(&sfhmep->hme_tte, &tte); 7263 if (TTE_IS_VALID(&tte)) { 7264 pfn = TTE_TO_PFN(vaddr, &tte); 7265 } else { 7266 pfn = PFN_INVALID; 7267 } 7268 SFMMU_HASH_UNLOCK(hmebp); 7269 return (pfn); 7270 } 7271 SFMMU_HASH_UNLOCK(hmebp); 7272 hashno++; 7273 } while (HME_REHASH(sfmmup) && (hashno <= mmu_hashcnt)); 7274 return (PFN_INVALID); 7275 } 7276 7277 7278 /* 7279 * For compatability with AT&T and later optimizations 7280 */ 7281 /* ARGSUSED */ 7282 void 7283 hat_map(struct hat *hat, caddr_t addr, size_t len, uint_t flags) 7284 { 7285 ASSERT(hat != NULL); 7286 ASSERT(hat->sfmmu_xhat_provider == NULL); 7287 } 7288 7289 /* 7290 * Return the number of mappings to a particular page. 7291 * This number is an approximation of the number of 7292 * number of people sharing the page. 7293 */ 7294 ulong_t 7295 hat_page_getshare(page_t *pp) 7296 { 7297 page_t *spp = pp; /* start page */ 7298 kmutex_t *pml; 7299 ulong_t cnt; 7300 int index, sz = TTE64K; 7301 7302 /* 7303 * We need to grab the mlist lock to make sure any outstanding 7304 * load/unloads complete. Otherwise we could return zero 7305 * even though the unload(s) hasn't finished yet. 7306 */ 7307 pml = sfmmu_mlist_enter(spp); 7308 cnt = spp->p_share; 7309 7310 #ifdef VAC 7311 if (kpm_enable) 7312 cnt += spp->p_kpmref; 7313 #endif 7314 7315 /* 7316 * If we have any large mappings, we count the number of 7317 * mappings that this large page is part of. 7318 */ 7319 index = PP_MAPINDEX(spp); 7320 index >>= 1; 7321 while (index) { 7322 pp = PP_GROUPLEADER(spp, sz); 7323 if ((index & 0x1) && pp != spp) { 7324 cnt += pp->p_share; 7325 spp = pp; 7326 } 7327 index >>= 1; 7328 sz++; 7329 } 7330 sfmmu_mlist_exit(pml); 7331 return (cnt); 7332 } 7333 7334 /* 7335 * Unload all large mappings to the pp and reset the p_szc field of every 7336 * constituent page according to the remaining mappings. 7337 * 7338 * pp must be locked SE_EXCL. Even though no other constituent pages are 7339 * locked it's legal to unload the large mappings to the pp because all 7340 * constituent pages of large locked mappings have to be locked SE_SHARED. 7341 * This means if we have SE_EXCL lock on one of constituent pages none of the 7342 * large mappings to pp are locked. 7343 * 7344 * Decrease p_szc field starting from the last constituent page and ending 7345 * with the root page. This method is used because other threads rely on the 7346 * root's p_szc to find the lock to syncronize on. After a root page_t's p_szc 7347 * is demoted then other threads will succeed in sfmmu_mlspl_enter(). This 7348 * ensures that p_szc changes of the constituent pages appears atomic for all 7349 * threads that use sfmmu_mlspl_enter() to examine p_szc field. 7350 * 7351 * This mechanism is only used for file system pages where it's not always 7352 * possible to get SE_EXCL locks on all constituent pages to demote the size 7353 * code (as is done for anonymous or kernel large pages). 7354 * 7355 * See more comments in front of sfmmu_mlspl_enter(). 7356 */ 7357 void 7358 hat_page_demote(page_t *pp) 7359 { 7360 int index; 7361 int sz; 7362 cpuset_t cpuset; 7363 int sync = 0; 7364 page_t *rootpp; 7365 struct sf_hment *sfhme; 7366 struct sf_hment *tmphme = NULL; 7367 struct hme_blk *hmeblkp; 7368 uint_t pszc; 7369 page_t *lastpp; 7370 cpuset_t tset; 7371 pgcnt_t npgs; 7372 kmutex_t *pml; 7373 kmutex_t *pmtx = NULL; 7374 7375 ASSERT(PAGE_EXCL(pp)); 7376 ASSERT(!PP_ISFREE(pp)); 7377 ASSERT(page_szc_lock_assert(pp)); 7378 pml = sfmmu_mlist_enter(pp); 7379 7380 pszc = pp->p_szc; 7381 if (pszc == 0) { 7382 goto out; 7383 } 7384 7385 index = PP_MAPINDEX(pp) >> 1; 7386 7387 if (index) { 7388 CPUSET_ZERO(cpuset); 7389 sz = TTE64K; 7390 sync = 1; 7391 } 7392 7393 while (index) { 7394 if (!(index & 0x1)) { 7395 index >>= 1; 7396 sz++; 7397 continue; 7398 } 7399 ASSERT(sz <= pszc); 7400 rootpp = PP_GROUPLEADER(pp, sz); 7401 for (sfhme = rootpp->p_mapping; sfhme; sfhme = tmphme) { 7402 tmphme = sfhme->hme_next; 7403 hmeblkp = sfmmu_hmetohblk(sfhme); 7404 if (hme_size(sfhme) != sz) { 7405 continue; 7406 } 7407 if (hmeblkp->hblk_xhat_bit) { 7408 cmn_err(CE_PANIC, 7409 "hat_page_demote: xhat hmeblk"); 7410 } 7411 tset = sfmmu_pageunload(rootpp, sfhme, sz); 7412 CPUSET_OR(cpuset, tset); 7413 } 7414 if (index >>= 1) { 7415 sz++; 7416 } 7417 } 7418 7419 ASSERT(!PP_ISMAPPED_LARGE(pp)); 7420 7421 if (sync) { 7422 xt_sync(cpuset); 7423 #ifdef VAC 7424 if (PP_ISTNC(pp)) { 7425 conv_tnc(rootpp, sz); 7426 } 7427 #endif /* VAC */ 7428 } 7429 7430 pmtx = sfmmu_page_enter(pp); 7431 7432 ASSERT(pp->p_szc == pszc); 7433 rootpp = PP_PAGEROOT(pp); 7434 ASSERT(rootpp->p_szc == pszc); 7435 lastpp = PP_PAGENEXT_N(rootpp, TTEPAGES(pszc) - 1); 7436 7437 while (lastpp != rootpp) { 7438 sz = PP_MAPINDEX(lastpp) ? fnd_mapping_sz(lastpp) : 0; 7439 ASSERT(sz < pszc); 7440 npgs = (sz == 0) ? 1 : TTEPAGES(sz); 7441 ASSERT(P2PHASE(lastpp->p_pagenum, npgs) == npgs - 1); 7442 while (--npgs > 0) { 7443 lastpp->p_szc = (uchar_t)sz; 7444 lastpp = PP_PAGEPREV(lastpp); 7445 } 7446 if (sz) { 7447 /* 7448 * make sure before current root's pszc 7449 * is updated all updates to constituent pages pszc 7450 * fields are globally visible. 7451 */ 7452 membar_producer(); 7453 } 7454 lastpp->p_szc = sz; 7455 ASSERT(IS_P2ALIGNED(lastpp->p_pagenum, TTEPAGES(sz))); 7456 if (lastpp != rootpp) { 7457 lastpp = PP_PAGEPREV(lastpp); 7458 } 7459 } 7460 if (sz == 0) { 7461 /* the loop above doesn't cover this case */ 7462 rootpp->p_szc = 0; 7463 } 7464 out: 7465 ASSERT(pp->p_szc == 0); 7466 if (pmtx != NULL) { 7467 sfmmu_page_exit(pmtx); 7468 } 7469 sfmmu_mlist_exit(pml); 7470 } 7471 7472 /* 7473 * Refresh the HAT ismttecnt[] element for size szc. 7474 * Caller must have set ISM busy flag to prevent mapping 7475 * lists from changing while we're traversing them. 7476 */ 7477 pgcnt_t 7478 ism_tsb_entries(sfmmu_t *sfmmup, int szc) 7479 { 7480 ism_blk_t *ism_blkp = sfmmup->sfmmu_iblk; 7481 ism_map_t *ism_map; 7482 pgcnt_t npgs = 0; 7483 int j; 7484 7485 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)); 7486 for (; ism_blkp != NULL; ism_blkp = ism_blkp->iblk_next) { 7487 ism_map = ism_blkp->iblk_maps; 7488 for (j = 0; ism_map[j].imap_ismhat && j < ISM_MAP_SLOTS; j++) 7489 npgs += ism_map[j].imap_ismhat->sfmmu_ttecnt[szc]; 7490 } 7491 sfmmup->sfmmu_ismttecnt[szc] = npgs; 7492 return (npgs); 7493 } 7494 7495 /* 7496 * Yield the memory claim requirement for an address space. 7497 * 7498 * This is currently implemented as the number of bytes that have active 7499 * hardware translations that have page structures. Therefore, it can 7500 * underestimate the traditional resident set size, eg, if the 7501 * physical page is present and the hardware translation is missing; 7502 * and it can overestimate the rss, eg, if there are active 7503 * translations to a frame buffer with page structs. 7504 * Also, it does not take sharing into account. 7505 * 7506 * Note that we don't acquire locks here since this function is most often 7507 * called from the clock thread. 7508 */ 7509 size_t 7510 hat_get_mapped_size(struct hat *hat) 7511 { 7512 size_t assize = 0; 7513 int i; 7514 7515 if (hat == NULL) 7516 return (0); 7517 7518 ASSERT(hat->sfmmu_xhat_provider == NULL); 7519 7520 for (i = 0; i < mmu_page_sizes; i++) 7521 assize += (pgcnt_t)hat->sfmmu_ttecnt[i] * TTEBYTES(i); 7522 7523 if (hat->sfmmu_iblk == NULL) 7524 return (assize); 7525 7526 for (i = 0; i < mmu_page_sizes; i++) 7527 assize += (pgcnt_t)hat->sfmmu_ismttecnt[i] * TTEBYTES(i); 7528 7529 return (assize); 7530 } 7531 7532 int 7533 hat_stats_enable(struct hat *hat) 7534 { 7535 hatlock_t *hatlockp; 7536 7537 ASSERT(hat->sfmmu_xhat_provider == NULL); 7538 7539 hatlockp = sfmmu_hat_enter(hat); 7540 hat->sfmmu_rmstat++; 7541 sfmmu_hat_exit(hatlockp); 7542 return (1); 7543 } 7544 7545 void 7546 hat_stats_disable(struct hat *hat) 7547 { 7548 hatlock_t *hatlockp; 7549 7550 ASSERT(hat->sfmmu_xhat_provider == NULL); 7551 7552 hatlockp = sfmmu_hat_enter(hat); 7553 hat->sfmmu_rmstat--; 7554 sfmmu_hat_exit(hatlockp); 7555 } 7556 7557 /* 7558 * Routines for entering or removing ourselves from the 7559 * ism_hat's mapping list. 7560 */ 7561 static void 7562 iment_add(struct ism_ment *iment, struct hat *ism_hat) 7563 { 7564 ASSERT(MUTEX_HELD(&ism_mlist_lock)); 7565 7566 iment->iment_prev = NULL; 7567 iment->iment_next = ism_hat->sfmmu_iment; 7568 if (ism_hat->sfmmu_iment) { 7569 ism_hat->sfmmu_iment->iment_prev = iment; 7570 } 7571 ism_hat->sfmmu_iment = iment; 7572 } 7573 7574 static void 7575 iment_sub(struct ism_ment *iment, struct hat *ism_hat) 7576 { 7577 ASSERT(MUTEX_HELD(&ism_mlist_lock)); 7578 7579 if (ism_hat->sfmmu_iment == NULL) { 7580 panic("ism map entry remove - no entries"); 7581 } 7582 7583 if (iment->iment_prev) { 7584 ASSERT(ism_hat->sfmmu_iment != iment); 7585 iment->iment_prev->iment_next = iment->iment_next; 7586 } else { 7587 ASSERT(ism_hat->sfmmu_iment == iment); 7588 ism_hat->sfmmu_iment = iment->iment_next; 7589 } 7590 7591 if (iment->iment_next) { 7592 iment->iment_next->iment_prev = iment->iment_prev; 7593 } 7594 7595 /* 7596 * zero out the entry 7597 */ 7598 iment->iment_next = NULL; 7599 iment->iment_prev = NULL; 7600 iment->iment_hat = NULL; 7601 } 7602 7603 /* 7604 * Hat_share()/unshare() return an (non-zero) error 7605 * when saddr and daddr are not properly aligned. 7606 * 7607 * The top level mapping element determines the alignment 7608 * requirement for saddr and daddr, depending on different 7609 * architectures. 7610 * 7611 * When hat_share()/unshare() are not supported, 7612 * HATOP_SHARE()/UNSHARE() return 0 7613 */ 7614 int 7615 hat_share(struct hat *sfmmup, caddr_t addr, 7616 struct hat *ism_hatid, caddr_t sptaddr, size_t len, uint_t ismszc) 7617 { 7618 ism_blk_t *ism_blkp; 7619 ism_blk_t *new_iblk; 7620 ism_map_t *ism_map; 7621 ism_ment_t *ism_ment; 7622 int i, added; 7623 hatlock_t *hatlockp; 7624 int reload_mmu = 0; 7625 uint_t ismshift = page_get_shift(ismszc); 7626 size_t ismpgsz = page_get_pagesize(ismszc); 7627 uint_t ismmask = (uint_t)ismpgsz - 1; 7628 size_t sh_size = ISM_SHIFT(ismshift, len); 7629 ushort_t ismhatflag; 7630 7631 #ifdef DEBUG 7632 caddr_t eaddr = addr + len; 7633 #endif /* DEBUG */ 7634 7635 ASSERT(ism_hatid != NULL && sfmmup != NULL); 7636 ASSERT(sptaddr == ISMID_STARTADDR); 7637 /* 7638 * Check the alignment. 7639 */ 7640 if (!ISM_ALIGNED(ismshift, addr) || !ISM_ALIGNED(ismshift, sptaddr)) 7641 return (EINVAL); 7642 7643 /* 7644 * Check size alignment. 7645 */ 7646 if (!ISM_ALIGNED(ismshift, len)) 7647 return (EINVAL); 7648 7649 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 7650 7651 /* 7652 * Allocate ism_ment for the ism_hat's mapping list, and an 7653 * ism map blk in case we need one. We must do our 7654 * allocations before acquiring locks to prevent a deadlock 7655 * in the kmem allocator on the mapping list lock. 7656 */ 7657 new_iblk = kmem_cache_alloc(ism_blk_cache, KM_SLEEP); 7658 ism_ment = kmem_cache_alloc(ism_ment_cache, KM_SLEEP); 7659 7660 /* 7661 * Serialize ISM mappings with the ISM busy flag, and also the 7662 * trap handlers. 7663 */ 7664 sfmmu_ismhat_enter(sfmmup, 0); 7665 7666 /* 7667 * Allocate an ism map blk if necessary. 7668 */ 7669 if (sfmmup->sfmmu_iblk == NULL) { 7670 sfmmup->sfmmu_iblk = new_iblk; 7671 bzero(new_iblk, sizeof (*new_iblk)); 7672 new_iblk->iblk_nextpa = (uint64_t)-1; 7673 membar_stst(); /* make sure next ptr visible to all CPUs */ 7674 sfmmup->sfmmu_ismblkpa = va_to_pa((caddr_t)new_iblk); 7675 reload_mmu = 1; 7676 new_iblk = NULL; 7677 } 7678 7679 #ifdef DEBUG 7680 /* 7681 * Make sure mapping does not already exist. 7682 */ 7683 ism_blkp = sfmmup->sfmmu_iblk; 7684 while (ism_blkp) { 7685 ism_map = ism_blkp->iblk_maps; 7686 for (i = 0; i < ISM_MAP_SLOTS && ism_map[i].imap_ismhat; i++) { 7687 if ((addr >= ism_start(ism_map[i]) && 7688 addr < ism_end(ism_map[i])) || 7689 eaddr > ism_start(ism_map[i]) && 7690 eaddr <= ism_end(ism_map[i])) { 7691 panic("sfmmu_share: Already mapped!"); 7692 } 7693 } 7694 ism_blkp = ism_blkp->iblk_next; 7695 } 7696 #endif /* DEBUG */ 7697 7698 ASSERT(ismszc >= TTE4M); 7699 if (ismszc == TTE4M) { 7700 ismhatflag = HAT_4M_FLAG; 7701 } else if (ismszc == TTE32M) { 7702 ismhatflag = HAT_32M_FLAG; 7703 } else if (ismszc == TTE256M) { 7704 ismhatflag = HAT_256M_FLAG; 7705 } 7706 /* 7707 * Add mapping to first available mapping slot. 7708 */ 7709 ism_blkp = sfmmup->sfmmu_iblk; 7710 added = 0; 7711 while (!added) { 7712 ism_map = ism_blkp->iblk_maps; 7713 for (i = 0; i < ISM_MAP_SLOTS; i++) { 7714 if (ism_map[i].imap_ismhat == NULL) { 7715 7716 ism_map[i].imap_ismhat = ism_hatid; 7717 ism_map[i].imap_vb_shift = (ushort_t)ismshift; 7718 ism_map[i].imap_hatflags = ismhatflag; 7719 ism_map[i].imap_sz_mask = ismmask; 7720 /* 7721 * imap_seg is checked in ISM_CHECK to see if 7722 * non-NULL, then other info assumed valid. 7723 */ 7724 membar_stst(); 7725 ism_map[i].imap_seg = (uintptr_t)addr | sh_size; 7726 ism_map[i].imap_ment = ism_ment; 7727 7728 /* 7729 * Now add ourselves to the ism_hat's 7730 * mapping list. 7731 */ 7732 ism_ment->iment_hat = sfmmup; 7733 ism_ment->iment_base_va = addr; 7734 ism_hatid->sfmmu_ismhat = 1; 7735 ism_hatid->sfmmu_flags = 0; 7736 mutex_enter(&ism_mlist_lock); 7737 iment_add(ism_ment, ism_hatid); 7738 mutex_exit(&ism_mlist_lock); 7739 added = 1; 7740 break; 7741 } 7742 } 7743 if (!added && ism_blkp->iblk_next == NULL) { 7744 ism_blkp->iblk_next = new_iblk; 7745 new_iblk = NULL; 7746 bzero(ism_blkp->iblk_next, 7747 sizeof (*ism_blkp->iblk_next)); 7748 ism_blkp->iblk_next->iblk_nextpa = (uint64_t)-1; 7749 membar_stst(); 7750 ism_blkp->iblk_nextpa = 7751 va_to_pa((caddr_t)ism_blkp->iblk_next); 7752 } 7753 ism_blkp = ism_blkp->iblk_next; 7754 } 7755 7756 /* 7757 * Update our counters for this sfmmup's ism mappings. 7758 */ 7759 for (i = 0; i <= ismszc; i++) { 7760 if (!(disable_ism_large_pages & (1 << i))) 7761 (void) ism_tsb_entries(sfmmup, i); 7762 } 7763 7764 hatlockp = sfmmu_hat_enter(sfmmup); 7765 7766 /* 7767 * For ISM and DISM we do not support 512K pages, so we only 7768 * only search the 4M and 8K/64K hashes for 4 pagesize cpus, and search 7769 * the 256M or 32M, and 4M and 8K/64K hashes for 6 pagesize cpus. 7770 */ 7771 ASSERT((disable_ism_large_pages & (1 << TTE512K)) != 0); 7772 7773 if (ismszc > TTE4M && !SFMMU_FLAGS_ISSET(sfmmup, HAT_4M_FLAG)) 7774 SFMMU_FLAGS_SET(sfmmup, HAT_4M_FLAG); 7775 7776 if (!SFMMU_FLAGS_ISSET(sfmmup, HAT_64K_FLAG)) 7777 SFMMU_FLAGS_SET(sfmmup, HAT_64K_FLAG); 7778 7779 /* 7780 * If we updated the ismblkpa for this HAT or we need 7781 * to start searching the 256M or 32M or 4M hash, we must 7782 * make sure all CPUs running this process reload their 7783 * tsbmiss area. Otherwise they will fail to load the mappings 7784 * in the tsbmiss handler and will loop calling pagefault(). 7785 */ 7786 switch (ismszc) { 7787 case TTE256M: 7788 if (reload_mmu || !SFMMU_FLAGS_ISSET(sfmmup, HAT_256M_FLAG)) { 7789 SFMMU_FLAGS_SET(sfmmup, HAT_256M_FLAG); 7790 sfmmu_sync_mmustate(sfmmup); 7791 } 7792 break; 7793 case TTE32M: 7794 if (reload_mmu || !SFMMU_FLAGS_ISSET(sfmmup, HAT_32M_FLAG)) { 7795 SFMMU_FLAGS_SET(sfmmup, HAT_32M_FLAG); 7796 sfmmu_sync_mmustate(sfmmup); 7797 } 7798 break; 7799 case TTE4M: 7800 if (reload_mmu || !SFMMU_FLAGS_ISSET(sfmmup, HAT_4M_FLAG)) { 7801 SFMMU_FLAGS_SET(sfmmup, HAT_4M_FLAG); 7802 sfmmu_sync_mmustate(sfmmup); 7803 } 7804 break; 7805 default: 7806 break; 7807 } 7808 7809 /* 7810 * Now we can drop the locks. 7811 */ 7812 sfmmu_ismhat_exit(sfmmup, 1); 7813 sfmmu_hat_exit(hatlockp); 7814 7815 /* 7816 * Free up ismblk if we didn't use it. 7817 */ 7818 if (new_iblk != NULL) 7819 kmem_cache_free(ism_blk_cache, new_iblk); 7820 7821 /* 7822 * Check TSB and TLB page sizes. 7823 */ 7824 sfmmu_check_page_sizes(sfmmup, 1); 7825 7826 return (0); 7827 } 7828 7829 /* 7830 * hat_unshare removes exactly one ism_map from 7831 * this process's as. It expects multiple calls 7832 * to hat_unshare for multiple shm segments. 7833 */ 7834 void 7835 hat_unshare(struct hat *sfmmup, caddr_t addr, size_t len, uint_t ismszc) 7836 { 7837 ism_map_t *ism_map; 7838 ism_ment_t *free_ment = NULL; 7839 ism_blk_t *ism_blkp; 7840 struct hat *ism_hatid; 7841 int found, i; 7842 hatlock_t *hatlockp; 7843 struct tsb_info *tsbinfo; 7844 uint_t ismshift = page_get_shift(ismszc); 7845 size_t sh_size = ISM_SHIFT(ismshift, len); 7846 7847 ASSERT(ISM_ALIGNED(ismshift, addr)); 7848 ASSERT(ISM_ALIGNED(ismshift, len)); 7849 ASSERT(sfmmup != NULL); 7850 ASSERT(sfmmup != ksfmmup); 7851 7852 if (sfmmup->sfmmu_xhat_provider) { 7853 XHAT_UNSHARE(sfmmup, addr, len); 7854 return; 7855 } else { 7856 /* 7857 * This must be a CPU HAT. If the address space has 7858 * XHATs attached, inform all XHATs that ISM segment 7859 * is going away 7860 */ 7861 ASSERT(sfmmup->sfmmu_as != NULL); 7862 if (sfmmup->sfmmu_as->a_xhat != NULL) 7863 xhat_unshare_all(sfmmup->sfmmu_as, addr, len); 7864 } 7865 7866 /* 7867 * Make sure that during the entire time ISM mappings are removed, 7868 * the trap handlers serialize behind us, and that no one else 7869 * can be mucking with ISM mappings. This also lets us get away 7870 * with not doing expensive cross calls to flush the TLB -- we 7871 * just discard the context, flush the entire TSB, and call it 7872 * a day. 7873 */ 7874 sfmmu_ismhat_enter(sfmmup, 0); 7875 7876 /* 7877 * Remove the mapping. 7878 * 7879 * We can't have any holes in the ism map. 7880 * The tsb miss code while searching the ism map will 7881 * stop on an empty map slot. So we must move 7882 * everyone past the hole up 1 if any. 7883 * 7884 * Also empty ism map blks are not freed until the 7885 * process exits. This is to prevent a MT race condition 7886 * between sfmmu_unshare() and sfmmu_tsbmiss_exception(). 7887 */ 7888 found = 0; 7889 ism_blkp = sfmmup->sfmmu_iblk; 7890 while (!found && ism_blkp) { 7891 ism_map = ism_blkp->iblk_maps; 7892 for (i = 0; i < ISM_MAP_SLOTS; i++) { 7893 if (addr == ism_start(ism_map[i]) && 7894 sh_size == (size_t)(ism_size(ism_map[i]))) { 7895 found = 1; 7896 break; 7897 } 7898 } 7899 if (!found) 7900 ism_blkp = ism_blkp->iblk_next; 7901 } 7902 7903 if (found) { 7904 ism_hatid = ism_map[i].imap_ismhat; 7905 ASSERT(ism_hatid != NULL); 7906 ASSERT(ism_hatid->sfmmu_ismhat == 1); 7907 7908 /* 7909 * First remove ourselves from the ism mapping list. 7910 */ 7911 mutex_enter(&ism_mlist_lock); 7912 iment_sub(ism_map[i].imap_ment, ism_hatid); 7913 mutex_exit(&ism_mlist_lock); 7914 free_ment = ism_map[i].imap_ment; 7915 7916 /* 7917 * Now gurantee that any other cpu 7918 * that tries to process an ISM miss 7919 * will go to tl=0. 7920 */ 7921 hatlockp = sfmmu_hat_enter(sfmmup); 7922 7923 sfmmu_invalidate_ctx(sfmmup); 7924 7925 sfmmu_hat_exit(hatlockp); 7926 7927 /* 7928 * We delete the ism map by copying 7929 * the next map over the current one. 7930 * We will take the next one in the maps 7931 * array or from the next ism_blk. 7932 */ 7933 while (ism_blkp) { 7934 ism_map = ism_blkp->iblk_maps; 7935 while (i < (ISM_MAP_SLOTS - 1)) { 7936 ism_map[i] = ism_map[i + 1]; 7937 i++; 7938 } 7939 /* i == (ISM_MAP_SLOTS - 1) */ 7940 ism_blkp = ism_blkp->iblk_next; 7941 if (ism_blkp) { 7942 ism_map[i] = ism_blkp->iblk_maps[0]; 7943 i = 0; 7944 } else { 7945 ism_map[i].imap_seg = 0; 7946 ism_map[i].imap_vb_shift = 0; 7947 ism_map[i].imap_hatflags = 0; 7948 ism_map[i].imap_sz_mask = 0; 7949 ism_map[i].imap_ismhat = NULL; 7950 ism_map[i].imap_ment = NULL; 7951 } 7952 } 7953 7954 /* 7955 * Now flush entire TSB for the process, since 7956 * demapping page by page can be too expensive. 7957 * We don't have to flush the TLB here anymore 7958 * since we switch to a new TLB ctx instead. 7959 * Also, there is no need to flush if the process 7960 * is exiting since the TSB will be freed later. 7961 */ 7962 if (!sfmmup->sfmmu_free) { 7963 hatlockp = sfmmu_hat_enter(sfmmup); 7964 for (tsbinfo = sfmmup->sfmmu_tsb; tsbinfo != NULL; 7965 tsbinfo = tsbinfo->tsb_next) { 7966 if (tsbinfo->tsb_flags & TSB_SWAPPED) 7967 continue; 7968 sfmmu_inv_tsb(tsbinfo->tsb_va, 7969 TSB_BYTES(tsbinfo->tsb_szc)); 7970 } 7971 sfmmu_hat_exit(hatlockp); 7972 } 7973 } 7974 7975 /* 7976 * Update our counters for this sfmmup's ism mappings. 7977 */ 7978 for (i = 0; i <= ismszc; i++) { 7979 if (!(disable_ism_large_pages & (1 << i))) 7980 (void) ism_tsb_entries(sfmmup, i); 7981 } 7982 7983 sfmmu_ismhat_exit(sfmmup, 0); 7984 7985 /* 7986 * We must do our freeing here after dropping locks 7987 * to prevent a deadlock in the kmem allocator on the 7988 * mapping list lock. 7989 */ 7990 if (free_ment != NULL) 7991 kmem_cache_free(ism_ment_cache, free_ment); 7992 7993 /* 7994 * Check TSB and TLB page sizes if the process isn't exiting. 7995 */ 7996 if (!sfmmup->sfmmu_free) 7997 sfmmu_check_page_sizes(sfmmup, 0); 7998 } 7999 8000 /* ARGSUSED */ 8001 static int 8002 sfmmu_idcache_constructor(void *buf, void *cdrarg, int kmflags) 8003 { 8004 /* void *buf is sfmmu_t pointer */ 8005 return (0); 8006 } 8007 8008 /* ARGSUSED */ 8009 static void 8010 sfmmu_idcache_destructor(void *buf, void *cdrarg) 8011 { 8012 /* void *buf is sfmmu_t pointer */ 8013 } 8014 8015 /* 8016 * setup kmem hmeblks by bzeroing all members and initializing the nextpa 8017 * field to be the pa of this hmeblk 8018 */ 8019 /* ARGSUSED */ 8020 static int 8021 sfmmu_hblkcache_constructor(void *buf, void *cdrarg, int kmflags) 8022 { 8023 struct hme_blk *hmeblkp; 8024 8025 bzero(buf, (size_t)cdrarg); 8026 hmeblkp = (struct hme_blk *)buf; 8027 hmeblkp->hblk_nextpa = va_to_pa((caddr_t)hmeblkp); 8028 8029 #ifdef HBLK_TRACE 8030 mutex_init(&hmeblkp->hblk_audit_lock, NULL, MUTEX_DEFAULT, NULL); 8031 #endif /* HBLK_TRACE */ 8032 8033 return (0); 8034 } 8035 8036 /* ARGSUSED */ 8037 static void 8038 sfmmu_hblkcache_destructor(void *buf, void *cdrarg) 8039 { 8040 8041 #ifdef HBLK_TRACE 8042 8043 struct hme_blk *hmeblkp; 8044 8045 hmeblkp = (struct hme_blk *)buf; 8046 mutex_destroy(&hmeblkp->hblk_audit_lock); 8047 8048 #endif /* HBLK_TRACE */ 8049 } 8050 8051 #define SFMMU_CACHE_RECLAIM_SCAN_RATIO 8 8052 static int sfmmu_cache_reclaim_scan_ratio = SFMMU_CACHE_RECLAIM_SCAN_RATIO; 8053 /* 8054 * The kmem allocator will callback into our reclaim routine when the system 8055 * is running low in memory. We traverse the hash and free up all unused but 8056 * still cached hme_blks. We also traverse the free list and free them up 8057 * as well. 8058 */ 8059 /*ARGSUSED*/ 8060 static void 8061 sfmmu_hblkcache_reclaim(void *cdrarg) 8062 { 8063 int i; 8064 uint64_t hblkpa, prevpa, nx_pa; 8065 struct hmehash_bucket *hmebp; 8066 struct hme_blk *hmeblkp, *nx_hblk, *pr_hblk = NULL; 8067 static struct hmehash_bucket *uhmehash_reclaim_hand; 8068 static struct hmehash_bucket *khmehash_reclaim_hand; 8069 struct hme_blk *list = NULL; 8070 8071 hmebp = uhmehash_reclaim_hand; 8072 if (hmebp == NULL || hmebp > &uhme_hash[UHMEHASH_SZ]) 8073 uhmehash_reclaim_hand = hmebp = uhme_hash; 8074 uhmehash_reclaim_hand += UHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; 8075 8076 for (i = UHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; i; i--) { 8077 if (SFMMU_HASH_LOCK_TRYENTER(hmebp) != 0) { 8078 hmeblkp = hmebp->hmeblkp; 8079 hblkpa = hmebp->hmeh_nextpa; 8080 prevpa = 0; 8081 pr_hblk = NULL; 8082 while (hmeblkp) { 8083 nx_hblk = hmeblkp->hblk_next; 8084 nx_pa = hmeblkp->hblk_nextpa; 8085 if (!hmeblkp->hblk_vcnt && 8086 !hmeblkp->hblk_hmecnt) { 8087 sfmmu_hblk_hash_rm(hmebp, hmeblkp, 8088 prevpa, pr_hblk); 8089 sfmmu_hblk_free(hmebp, hmeblkp, 8090 hblkpa, &list); 8091 } else { 8092 pr_hblk = hmeblkp; 8093 prevpa = hblkpa; 8094 } 8095 hmeblkp = nx_hblk; 8096 hblkpa = nx_pa; 8097 } 8098 SFMMU_HASH_UNLOCK(hmebp); 8099 } 8100 if (hmebp++ == &uhme_hash[UHMEHASH_SZ]) 8101 hmebp = uhme_hash; 8102 } 8103 8104 hmebp = khmehash_reclaim_hand; 8105 if (hmebp == NULL || hmebp > &khme_hash[KHMEHASH_SZ]) 8106 khmehash_reclaim_hand = hmebp = khme_hash; 8107 khmehash_reclaim_hand += KHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; 8108 8109 for (i = KHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; i; i--) { 8110 if (SFMMU_HASH_LOCK_TRYENTER(hmebp) != 0) { 8111 hmeblkp = hmebp->hmeblkp; 8112 hblkpa = hmebp->hmeh_nextpa; 8113 prevpa = 0; 8114 pr_hblk = NULL; 8115 while (hmeblkp) { 8116 nx_hblk = hmeblkp->hblk_next; 8117 nx_pa = hmeblkp->hblk_nextpa; 8118 if (!hmeblkp->hblk_vcnt && 8119 !hmeblkp->hblk_hmecnt) { 8120 sfmmu_hblk_hash_rm(hmebp, hmeblkp, 8121 prevpa, pr_hblk); 8122 sfmmu_hblk_free(hmebp, hmeblkp, 8123 hblkpa, &list); 8124 } else { 8125 pr_hblk = hmeblkp; 8126 prevpa = hblkpa; 8127 } 8128 hmeblkp = nx_hblk; 8129 hblkpa = nx_pa; 8130 } 8131 SFMMU_HASH_UNLOCK(hmebp); 8132 } 8133 if (hmebp++ == &khme_hash[KHMEHASH_SZ]) 8134 hmebp = khme_hash; 8135 } 8136 sfmmu_hblks_list_purge(&list); 8137 } 8138 8139 /* 8140 * sfmmu_get_ppvcolor should become a vm_machdep or hatop interface. 8141 * same goes for sfmmu_get_addrvcolor(). 8142 * 8143 * This function will return the virtual color for the specified page. The 8144 * virtual color corresponds to this page current mapping or its last mapping. 8145 * It is used by memory allocators to choose addresses with the correct 8146 * alignment so vac consistency is automatically maintained. If the page 8147 * has no color it returns -1. 8148 */ 8149 /*ARGSUSED*/ 8150 int 8151 sfmmu_get_ppvcolor(struct page *pp) 8152 { 8153 #ifdef VAC 8154 int color; 8155 8156 if (!(cache & CACHE_VAC) || PP_NEWPAGE(pp)) { 8157 return (-1); 8158 } 8159 color = PP_GET_VCOLOR(pp); 8160 ASSERT(color < mmu_btop(shm_alignment)); 8161 return (color); 8162 #else 8163 return (-1); 8164 #endif /* VAC */ 8165 } 8166 8167 /* 8168 * This function will return the desired alignment for vac consistency 8169 * (vac color) given a virtual address. If no vac is present it returns -1. 8170 */ 8171 /*ARGSUSED*/ 8172 int 8173 sfmmu_get_addrvcolor(caddr_t vaddr) 8174 { 8175 #ifdef VAC 8176 if (cache & CACHE_VAC) { 8177 return (addr_to_vcolor(vaddr)); 8178 } else { 8179 return (-1); 8180 } 8181 #else 8182 return (-1); 8183 #endif /* VAC */ 8184 } 8185 8186 #ifdef VAC 8187 /* 8188 * Check for conflicts. 8189 * A conflict exists if the new and existent mappings do not match in 8190 * their "shm_alignment fields. If conflicts exist, the existant mappings 8191 * are flushed unless one of them is locked. If one of them is locked, then 8192 * the mappings are flushed and converted to non-cacheable mappings. 8193 */ 8194 static void 8195 sfmmu_vac_conflict(struct hat *hat, caddr_t addr, page_t *pp) 8196 { 8197 struct hat *tmphat; 8198 struct sf_hment *sfhmep, *tmphme = NULL; 8199 struct hme_blk *hmeblkp; 8200 int vcolor; 8201 tte_t tte; 8202 8203 ASSERT(sfmmu_mlist_held(pp)); 8204 ASSERT(!PP_ISNC(pp)); /* page better be cacheable */ 8205 8206 vcolor = addr_to_vcolor(addr); 8207 if (PP_NEWPAGE(pp)) { 8208 PP_SET_VCOLOR(pp, vcolor); 8209 return; 8210 } 8211 8212 if (PP_GET_VCOLOR(pp) == vcolor) { 8213 return; 8214 } 8215 8216 if (!PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp)) { 8217 /* 8218 * Previous user of page had a different color 8219 * but since there are no current users 8220 * we just flush the cache and change the color. 8221 */ 8222 SFMMU_STAT(sf_pgcolor_conflict); 8223 sfmmu_cache_flush(pp->p_pagenum, PP_GET_VCOLOR(pp)); 8224 PP_SET_VCOLOR(pp, vcolor); 8225 return; 8226 } 8227 8228 /* 8229 * If we get here we have a vac conflict with a current 8230 * mapping. VAC conflict policy is as follows. 8231 * - The default is to unload the other mappings unless: 8232 * - If we have a large mapping we uncache the page. 8233 * We need to uncache the rest of the large page too. 8234 * - If any of the mappings are locked we uncache the page. 8235 * - If the requested mapping is inconsistent 8236 * with another mapping and that mapping 8237 * is in the same address space we have to 8238 * make it non-cached. The default thing 8239 * to do is unload the inconsistent mapping 8240 * but if they are in the same address space 8241 * we run the risk of unmapping the pc or the 8242 * stack which we will use as we return to the user, 8243 * in which case we can then fault on the thing 8244 * we just unloaded and get into an infinite loop. 8245 */ 8246 if (PP_ISMAPPED_LARGE(pp)) { 8247 int sz; 8248 8249 /* 8250 * Existing mapping is for big pages. We don't unload 8251 * existing big mappings to satisfy new mappings. 8252 * Always convert all mappings to TNC. 8253 */ 8254 sz = fnd_mapping_sz(pp); 8255 pp = PP_GROUPLEADER(pp, sz); 8256 SFMMU_STAT_ADD(sf_uncache_conflict, TTEPAGES(sz)); 8257 sfmmu_page_cache_array(pp, HAT_TMPNC, CACHE_FLUSH, 8258 TTEPAGES(sz)); 8259 8260 return; 8261 } 8262 8263 /* 8264 * check if any mapping is in same as or if it is locked 8265 * since in that case we need to uncache. 8266 */ 8267 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = tmphme) { 8268 tmphme = sfhmep->hme_next; 8269 hmeblkp = sfmmu_hmetohblk(sfhmep); 8270 if (hmeblkp->hblk_xhat_bit) 8271 continue; 8272 tmphat = hblktosfmmu(hmeblkp); 8273 sfmmu_copytte(&sfhmep->hme_tte, &tte); 8274 ASSERT(TTE_IS_VALID(&tte)); 8275 if ((tmphat == hat) || hmeblkp->hblk_lckcnt) { 8276 /* 8277 * We have an uncache conflict 8278 */ 8279 SFMMU_STAT(sf_uncache_conflict); 8280 sfmmu_page_cache_array(pp, HAT_TMPNC, CACHE_FLUSH, 1); 8281 return; 8282 } 8283 } 8284 8285 /* 8286 * We have an unload conflict 8287 * We have already checked for LARGE mappings, therefore 8288 * the remaining mapping(s) must be TTE8K. 8289 */ 8290 SFMMU_STAT(sf_unload_conflict); 8291 8292 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = tmphme) { 8293 tmphme = sfhmep->hme_next; 8294 hmeblkp = sfmmu_hmetohblk(sfhmep); 8295 if (hmeblkp->hblk_xhat_bit) 8296 continue; 8297 (void) sfmmu_pageunload(pp, sfhmep, TTE8K); 8298 } 8299 8300 if (PP_ISMAPPED_KPM(pp)) 8301 sfmmu_kpm_vac_unload(pp, addr); 8302 8303 /* 8304 * Unloads only do TLB flushes so we need to flush the 8305 * cache here. 8306 */ 8307 sfmmu_cache_flush(pp->p_pagenum, PP_GET_VCOLOR(pp)); 8308 PP_SET_VCOLOR(pp, vcolor); 8309 } 8310 8311 /* 8312 * Whenever a mapping is unloaded and the page is in TNC state, 8313 * we see if the page can be made cacheable again. 'pp' is 8314 * the page that we just unloaded a mapping from, the size 8315 * of mapping that was unloaded is 'ottesz'. 8316 * Remark: 8317 * The recache policy for mpss pages can leave a performance problem 8318 * under the following circumstances: 8319 * . A large page in uncached mode has just been unmapped. 8320 * . All constituent pages are TNC due to a conflicting small mapping. 8321 * . There are many other, non conflicting, small mappings around for 8322 * a lot of the constituent pages. 8323 * . We're called w/ the "old" groupleader page and the old ottesz, 8324 * but this is irrelevant, since we're no more "PP_ISMAPPED_LARGE", so 8325 * we end up w/ TTE8K or npages == 1. 8326 * . We call tst_tnc w/ the old groupleader only, and if there is no 8327 * conflict, we re-cache only this page. 8328 * . All other small mappings are not checked and will be left in TNC mode. 8329 * The problem is not very serious because: 8330 * . mpss is actually only defined for heap and stack, so the probability 8331 * is not very high that a large page mapping exists in parallel to a small 8332 * one (this is possible, but seems to be bad programming style in the 8333 * appl). 8334 * . The problem gets a little bit more serious, when those TNC pages 8335 * have to be mapped into kernel space, e.g. for networking. 8336 * . When VAC alias conflicts occur in applications, this is regarded 8337 * as an application bug. So if kstat's show them, the appl should 8338 * be changed anyway. 8339 */ 8340 void 8341 conv_tnc(page_t *pp, int ottesz) 8342 { 8343 int cursz, dosz; 8344 pgcnt_t curnpgs, dopgs; 8345 pgcnt_t pg64k; 8346 page_t *pp2; 8347 8348 /* 8349 * Determine how big a range we check for TNC and find 8350 * leader page. cursz is the size of the biggest 8351 * mapping that still exist on 'pp'. 8352 */ 8353 if (PP_ISMAPPED_LARGE(pp)) { 8354 cursz = fnd_mapping_sz(pp); 8355 } else { 8356 cursz = TTE8K; 8357 } 8358 8359 if (ottesz >= cursz) { 8360 dosz = ottesz; 8361 pp2 = pp; 8362 } else { 8363 dosz = cursz; 8364 pp2 = PP_GROUPLEADER(pp, dosz); 8365 } 8366 8367 pg64k = TTEPAGES(TTE64K); 8368 dopgs = TTEPAGES(dosz); 8369 8370 ASSERT(dopgs == 1 || ((dopgs & (pg64k - 1)) == 0)); 8371 8372 while (dopgs != 0) { 8373 curnpgs = TTEPAGES(cursz); 8374 if (tst_tnc(pp2, curnpgs)) { 8375 SFMMU_STAT_ADD(sf_recache, curnpgs); 8376 sfmmu_page_cache_array(pp2, HAT_CACHE, CACHE_NO_FLUSH, 8377 curnpgs); 8378 } 8379 8380 ASSERT(dopgs >= curnpgs); 8381 dopgs -= curnpgs; 8382 8383 if (dopgs == 0) { 8384 break; 8385 } 8386 8387 pp2 = PP_PAGENEXT_N(pp2, curnpgs); 8388 if (((dopgs & (pg64k - 1)) == 0) && PP_ISMAPPED_LARGE(pp2)) { 8389 cursz = fnd_mapping_sz(pp2); 8390 } else { 8391 cursz = TTE8K; 8392 } 8393 } 8394 } 8395 8396 /* 8397 * Returns 1 if page(s) can be converted from TNC to cacheable setting, 8398 * returns 0 otherwise. Note that oaddr argument is valid for only 8399 * 8k pages. 8400 */ 8401 int 8402 tst_tnc(page_t *pp, pgcnt_t npages) 8403 { 8404 struct sf_hment *sfhme; 8405 struct hme_blk *hmeblkp; 8406 tte_t tte; 8407 caddr_t vaddr; 8408 int clr_valid = 0; 8409 int color, color1, bcolor; 8410 int i, ncolors; 8411 8412 ASSERT(pp != NULL); 8413 ASSERT(!(cache & CACHE_WRITEBACK)); 8414 8415 if (npages > 1) { 8416 ncolors = CACHE_NUM_COLOR; 8417 } 8418 8419 for (i = 0; i < npages; i++) { 8420 ASSERT(sfmmu_mlist_held(pp)); 8421 ASSERT(PP_ISTNC(pp)); 8422 ASSERT(PP_GET_VCOLOR(pp) == NO_VCOLOR); 8423 8424 if (PP_ISPNC(pp)) { 8425 return (0); 8426 } 8427 8428 clr_valid = 0; 8429 if (PP_ISMAPPED_KPM(pp)) { 8430 caddr_t kpmvaddr; 8431 8432 ASSERT(kpm_enable); 8433 kpmvaddr = hat_kpm_page2va(pp, 1); 8434 ASSERT(!(npages > 1 && IS_KPM_ALIAS_RANGE(kpmvaddr))); 8435 color1 = addr_to_vcolor(kpmvaddr); 8436 clr_valid = 1; 8437 } 8438 8439 for (sfhme = pp->p_mapping; sfhme; sfhme = sfhme->hme_next) { 8440 hmeblkp = sfmmu_hmetohblk(sfhme); 8441 if (hmeblkp->hblk_xhat_bit) 8442 continue; 8443 8444 sfmmu_copytte(&sfhme->hme_tte, &tte); 8445 ASSERT(TTE_IS_VALID(&tte)); 8446 8447 vaddr = tte_to_vaddr(hmeblkp, tte); 8448 color = addr_to_vcolor(vaddr); 8449 8450 if (npages > 1) { 8451 /* 8452 * If there is a big mapping, make sure 8453 * 8K mapping is consistent with the big 8454 * mapping. 8455 */ 8456 bcolor = i % ncolors; 8457 if (color != bcolor) { 8458 return (0); 8459 } 8460 } 8461 if (!clr_valid) { 8462 clr_valid = 1; 8463 color1 = color; 8464 } 8465 8466 if (color1 != color) { 8467 return (0); 8468 } 8469 } 8470 8471 pp = PP_PAGENEXT(pp); 8472 } 8473 8474 return (1); 8475 } 8476 8477 void 8478 sfmmu_page_cache_array(page_t *pp, int flags, int cache_flush_flag, 8479 pgcnt_t npages) 8480 { 8481 kmutex_t *pmtx; 8482 int i, ncolors, bcolor; 8483 kpm_hlk_t *kpmp; 8484 cpuset_t cpuset; 8485 8486 ASSERT(pp != NULL); 8487 ASSERT(!(cache & CACHE_WRITEBACK)); 8488 8489 kpmp = sfmmu_kpm_kpmp_enter(pp, npages); 8490 pmtx = sfmmu_page_enter(pp); 8491 8492 /* 8493 * Fast path caching single unmapped page 8494 */ 8495 if (npages == 1 && !PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp) && 8496 flags == HAT_CACHE) { 8497 PP_CLRTNC(pp); 8498 PP_CLRPNC(pp); 8499 sfmmu_page_exit(pmtx); 8500 sfmmu_kpm_kpmp_exit(kpmp); 8501 return; 8502 } 8503 8504 /* 8505 * We need to capture all cpus in order to change cacheability 8506 * because we can't allow one cpu to access the same physical 8507 * page using a cacheable and a non-cachebale mapping at the same 8508 * time. Since we may end up walking the ism mapping list 8509 * have to grab it's lock now since we can't after all the 8510 * cpus have been captured. 8511 */ 8512 sfmmu_hat_lock_all(); 8513 mutex_enter(&ism_mlist_lock); 8514 kpreempt_disable(); 8515 cpuset = cpu_ready_set; 8516 xc_attention(cpuset); 8517 8518 if (npages > 1) { 8519 /* 8520 * Make sure all colors are flushed since the 8521 * sfmmu_page_cache() only flushes one color- 8522 * it does not know big pages. 8523 */ 8524 ncolors = CACHE_NUM_COLOR; 8525 if (flags & HAT_TMPNC) { 8526 for (i = 0; i < ncolors; i++) { 8527 sfmmu_cache_flushcolor(i, pp->p_pagenum); 8528 } 8529 cache_flush_flag = CACHE_NO_FLUSH; 8530 } 8531 } 8532 8533 for (i = 0; i < npages; i++) { 8534 8535 ASSERT(sfmmu_mlist_held(pp)); 8536 8537 if (!(flags == HAT_TMPNC && PP_ISTNC(pp))) { 8538 8539 if (npages > 1) { 8540 bcolor = i % ncolors; 8541 } else { 8542 bcolor = NO_VCOLOR; 8543 } 8544 8545 sfmmu_page_cache(pp, flags, cache_flush_flag, 8546 bcolor); 8547 } 8548 8549 pp = PP_PAGENEXT(pp); 8550 } 8551 8552 xt_sync(cpuset); 8553 xc_dismissed(cpuset); 8554 mutex_exit(&ism_mlist_lock); 8555 sfmmu_hat_unlock_all(); 8556 sfmmu_page_exit(pmtx); 8557 sfmmu_kpm_kpmp_exit(kpmp); 8558 kpreempt_enable(); 8559 } 8560 8561 /* 8562 * This function changes the virtual cacheability of all mappings to a 8563 * particular page. When changing from uncache to cacheable the mappings will 8564 * only be changed if all of them have the same virtual color. 8565 * We need to flush the cache in all cpus. It is possible that 8566 * a process referenced a page as cacheable but has sinced exited 8567 * and cleared the mapping list. We still to flush it but have no 8568 * state so all cpus is the only alternative. 8569 */ 8570 static void 8571 sfmmu_page_cache(page_t *pp, int flags, int cache_flush_flag, int bcolor) 8572 { 8573 struct sf_hment *sfhme; 8574 struct hme_blk *hmeblkp; 8575 sfmmu_t *sfmmup; 8576 tte_t tte, ttemod; 8577 caddr_t vaddr; 8578 int ret, color; 8579 pfn_t pfn; 8580 8581 color = bcolor; 8582 pfn = pp->p_pagenum; 8583 8584 for (sfhme = pp->p_mapping; sfhme; sfhme = sfhme->hme_next) { 8585 8586 hmeblkp = sfmmu_hmetohblk(sfhme); 8587 8588 if (hmeblkp->hblk_xhat_bit) 8589 continue; 8590 8591 sfmmu_copytte(&sfhme->hme_tte, &tte); 8592 ASSERT(TTE_IS_VALID(&tte)); 8593 vaddr = tte_to_vaddr(hmeblkp, tte); 8594 color = addr_to_vcolor(vaddr); 8595 8596 #ifdef DEBUG 8597 if ((flags & HAT_CACHE) && bcolor != NO_VCOLOR) { 8598 ASSERT(color == bcolor); 8599 } 8600 #endif 8601 8602 ASSERT(flags != HAT_TMPNC || color == PP_GET_VCOLOR(pp)); 8603 8604 ttemod = tte; 8605 if (flags & (HAT_UNCACHE | HAT_TMPNC)) { 8606 TTE_CLR_VCACHEABLE(&ttemod); 8607 } else { /* flags & HAT_CACHE */ 8608 TTE_SET_VCACHEABLE(&ttemod); 8609 } 8610 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte); 8611 if (ret < 0) { 8612 /* 8613 * Since all cpus are captured modifytte should not 8614 * fail. 8615 */ 8616 panic("sfmmu_page_cache: write to tte failed"); 8617 } 8618 8619 sfmmup = hblktosfmmu(hmeblkp); 8620 if (cache_flush_flag == CACHE_FLUSH) { 8621 /* 8622 * Flush TSBs, TLBs and caches 8623 */ 8624 if (sfmmup->sfmmu_ismhat) { 8625 if (flags & HAT_CACHE) { 8626 SFMMU_STAT(sf_ism_recache); 8627 } else { 8628 SFMMU_STAT(sf_ism_uncache); 8629 } 8630 sfmmu_ismtlbcache_demap(vaddr, sfmmup, hmeblkp, 8631 pfn, CACHE_FLUSH); 8632 } else { 8633 sfmmu_tlbcache_demap(vaddr, sfmmup, hmeblkp, 8634 pfn, 0, FLUSH_ALL_CPUS, CACHE_FLUSH, 1); 8635 } 8636 8637 /* 8638 * all cache entries belonging to this pfn are 8639 * now flushed. 8640 */ 8641 cache_flush_flag = CACHE_NO_FLUSH; 8642 } else { 8643 8644 /* 8645 * Flush only TSBs and TLBs. 8646 */ 8647 if (sfmmup->sfmmu_ismhat) { 8648 if (flags & HAT_CACHE) { 8649 SFMMU_STAT(sf_ism_recache); 8650 } else { 8651 SFMMU_STAT(sf_ism_uncache); 8652 } 8653 sfmmu_ismtlbcache_demap(vaddr, sfmmup, hmeblkp, 8654 pfn, CACHE_NO_FLUSH); 8655 } else { 8656 sfmmu_tlb_demap(vaddr, sfmmup, hmeblkp, 0, 1); 8657 } 8658 } 8659 } 8660 8661 if (PP_ISMAPPED_KPM(pp)) 8662 sfmmu_kpm_page_cache(pp, flags, cache_flush_flag); 8663 8664 switch (flags) { 8665 8666 default: 8667 panic("sfmmu_pagecache: unknown flags"); 8668 break; 8669 8670 case HAT_CACHE: 8671 PP_CLRTNC(pp); 8672 PP_CLRPNC(pp); 8673 PP_SET_VCOLOR(pp, color); 8674 break; 8675 8676 case HAT_TMPNC: 8677 PP_SETTNC(pp); 8678 PP_SET_VCOLOR(pp, NO_VCOLOR); 8679 break; 8680 8681 case HAT_UNCACHE: 8682 PP_SETPNC(pp); 8683 PP_CLRTNC(pp); 8684 PP_SET_VCOLOR(pp, NO_VCOLOR); 8685 break; 8686 } 8687 } 8688 #endif /* VAC */ 8689 8690 8691 /* 8692 * Wrapper routine used to return a context. 8693 * 8694 * It's the responsibility of the caller to guarantee that the 8695 * process serializes on calls here by taking the HAT lock for 8696 * the hat. 8697 * 8698 */ 8699 static void 8700 sfmmu_get_ctx(sfmmu_t *sfmmup) 8701 { 8702 mmu_ctx_t *mmu_ctxp; 8703 uint_t pstate_save; 8704 8705 ASSERT(sfmmu_hat_lock_held(sfmmup)); 8706 ASSERT(sfmmup != ksfmmup); 8707 8708 kpreempt_disable(); 8709 8710 mmu_ctxp = CPU_MMU_CTXP(CPU); 8711 ASSERT(mmu_ctxp); 8712 ASSERT(mmu_ctxp->mmu_idx < max_mmu_ctxdoms); 8713 ASSERT(mmu_ctxp == mmu_ctxs_tbl[mmu_ctxp->mmu_idx]); 8714 8715 /* 8716 * Do a wrap-around if cnum reaches the max # cnum supported by a MMU. 8717 */ 8718 if (mmu_ctxp->mmu_cnum == mmu_ctxp->mmu_nctxs) 8719 sfmmu_ctx_wrap_around(mmu_ctxp); 8720 8721 /* 8722 * Let the MMU set up the page sizes to use for 8723 * this context in the TLB. Don't program 2nd dtlb for ism hat. 8724 */ 8725 if ((&mmu_set_ctx_page_sizes) && (sfmmup->sfmmu_ismhat == 0)) { 8726 mmu_set_ctx_page_sizes(sfmmup); 8727 } 8728 8729 /* 8730 * sfmmu_alloc_ctx and sfmmu_load_mmustate will be performed with 8731 * interrupts disabled to prevent race condition with wrap-around 8732 * ctx invalidatation. In sun4v, ctx invalidation also involves 8733 * a HV call to set the number of TSBs to 0. If interrupts are not 8734 * disabled until after sfmmu_load_mmustate is complete TSBs may 8735 * become assigned to INVALID_CONTEXT. This is not allowed. 8736 */ 8737 pstate_save = sfmmu_disable_intrs(); 8738 8739 sfmmu_alloc_ctx(sfmmup, 1, CPU); 8740 sfmmu_load_mmustate(sfmmup); 8741 8742 sfmmu_enable_intrs(pstate_save); 8743 8744 kpreempt_enable(); 8745 } 8746 8747 /* 8748 * When all cnums are used up in a MMU, cnum will wrap around to the 8749 * next generation and start from 2. 8750 */ 8751 static void 8752 sfmmu_ctx_wrap_around(mmu_ctx_t *mmu_ctxp) 8753 { 8754 8755 /* caller must have disabled the preemption */ 8756 ASSERT(curthread->t_preempt >= 1); 8757 ASSERT(mmu_ctxp != NULL); 8758 8759 /* acquire Per-MMU (PM) spin lock */ 8760 mutex_enter(&mmu_ctxp->mmu_lock); 8761 8762 /* re-check to see if wrap-around is needed */ 8763 if (mmu_ctxp->mmu_cnum < mmu_ctxp->mmu_nctxs) 8764 goto done; 8765 8766 SFMMU_MMU_STAT(mmu_wrap_around); 8767 8768 /* update gnum */ 8769 ASSERT(mmu_ctxp->mmu_gnum != 0); 8770 mmu_ctxp->mmu_gnum++; 8771 if (mmu_ctxp->mmu_gnum == 0 || 8772 mmu_ctxp->mmu_gnum > MAX_SFMMU_GNUM_VAL) { 8773 cmn_err(CE_PANIC, "mmu_gnum of mmu_ctx 0x%p is out of bound.", 8774 (void *)mmu_ctxp); 8775 } 8776 8777 if (mmu_ctxp->mmu_ncpus > 1) { 8778 cpuset_t cpuset; 8779 8780 membar_enter(); /* make sure updated gnum visible */ 8781 8782 SFMMU_XCALL_STATS(NULL); 8783 8784 /* xcall to others on the same MMU to invalidate ctx */ 8785 cpuset = mmu_ctxp->mmu_cpuset; 8786 ASSERT(CPU_IN_SET(cpuset, CPU->cpu_id)); 8787 CPUSET_DEL(cpuset, CPU->cpu_id); 8788 CPUSET_AND(cpuset, cpu_ready_set); 8789 8790 /* 8791 * Pass in INVALID_CONTEXT as the first parameter to 8792 * sfmmu_raise_tsb_exception, which invalidates the context 8793 * of any process running on the CPUs in the MMU. 8794 */ 8795 xt_some(cpuset, sfmmu_raise_tsb_exception, 8796 INVALID_CONTEXT, INVALID_CONTEXT); 8797 xt_sync(cpuset); 8798 8799 SFMMU_MMU_STAT(mmu_tsb_raise_exception); 8800 } 8801 8802 if (sfmmu_getctx_sec() != INVALID_CONTEXT) { 8803 sfmmu_setctx_sec(INVALID_CONTEXT); 8804 sfmmu_clear_utsbinfo(); 8805 } 8806 8807 /* 8808 * No xcall is needed here. For sun4u systems all CPUs in context 8809 * domain share a single physical MMU therefore it's enough to flush 8810 * TLB on local CPU. On sun4v systems we use 1 global context 8811 * domain and flush all remote TLBs in sfmmu_raise_tsb_exception 8812 * handler. Note that vtag_flushall_uctxs() is called 8813 * for Ultra II machine, where the equivalent flushall functionality 8814 * is implemented in SW, and only user ctx TLB entries are flushed. 8815 */ 8816 if (&vtag_flushall_uctxs != NULL) { 8817 vtag_flushall_uctxs(); 8818 } else { 8819 vtag_flushall(); 8820 } 8821 8822 /* reset mmu cnum, skips cnum 0 and 1 */ 8823 mmu_ctxp->mmu_cnum = NUM_LOCKED_CTXS; 8824 8825 done: 8826 mutex_exit(&mmu_ctxp->mmu_lock); 8827 } 8828 8829 8830 /* 8831 * For multi-threaded process, set the process context to INVALID_CONTEXT 8832 * so that it faults and reloads the MMU state from TL=0. For single-threaded 8833 * process, we can just load the MMU state directly without having to 8834 * set context invalid. Caller must hold the hat lock since we don't 8835 * acquire it here. 8836 */ 8837 static void 8838 sfmmu_sync_mmustate(sfmmu_t *sfmmup) 8839 { 8840 uint_t cnum; 8841 uint_t pstate_save; 8842 8843 ASSERT(sfmmup != ksfmmup); 8844 ASSERT(sfmmu_hat_lock_held(sfmmup)); 8845 8846 kpreempt_disable(); 8847 8848 /* 8849 * We check whether the pass'ed-in sfmmup is the same as the 8850 * current running proc. This is to makes sure the current proc 8851 * stays single-threaded if it already is. 8852 */ 8853 if ((sfmmup == curthread->t_procp->p_as->a_hat) && 8854 (curthread->t_procp->p_lwpcnt == 1)) { 8855 /* single-thread */ 8856 cnum = sfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum; 8857 if (cnum != INVALID_CONTEXT) { 8858 uint_t curcnum; 8859 /* 8860 * Disable interrupts to prevent race condition 8861 * with sfmmu_ctx_wrap_around ctx invalidation. 8862 * In sun4v, ctx invalidation involves setting 8863 * TSB to NULL, hence, interrupts should be disabled 8864 * untill after sfmmu_load_mmustate is completed. 8865 */ 8866 pstate_save = sfmmu_disable_intrs(); 8867 curcnum = sfmmu_getctx_sec(); 8868 if (curcnum == cnum) 8869 sfmmu_load_mmustate(sfmmup); 8870 sfmmu_enable_intrs(pstate_save); 8871 ASSERT(curcnum == cnum || curcnum == INVALID_CONTEXT); 8872 } 8873 } else { 8874 /* 8875 * multi-thread 8876 * or when sfmmup is not the same as the curproc. 8877 */ 8878 sfmmu_invalidate_ctx(sfmmup); 8879 } 8880 8881 kpreempt_enable(); 8882 } 8883 8884 8885 /* 8886 * Replace the specified TSB with a new TSB. This function gets called when 8887 * we grow, shrink or swapin a TSB. When swapping in a TSB (TSB_SWAPIN), the 8888 * TSB_FORCEALLOC flag may be used to force allocation of a minimum-sized TSB 8889 * (8K). 8890 * 8891 * Caller must hold the HAT lock, but should assume any tsb_info 8892 * pointers it has are no longer valid after calling this function. 8893 * 8894 * Return values: 8895 * TSB_ALLOCFAIL Failed to allocate a TSB, due to memory constraints 8896 * TSB_LOSTRACE HAT is busy, i.e. another thread is already doing 8897 * something to this tsbinfo/TSB 8898 * TSB_SUCCESS Operation succeeded 8899 */ 8900 static tsb_replace_rc_t 8901 sfmmu_replace_tsb(sfmmu_t *sfmmup, struct tsb_info *old_tsbinfo, uint_t szc, 8902 hatlock_t *hatlockp, uint_t flags) 8903 { 8904 struct tsb_info *new_tsbinfo = NULL; 8905 struct tsb_info *curtsb, *prevtsb; 8906 uint_t tte_sz_mask; 8907 int i; 8908 8909 ASSERT(sfmmup != ksfmmup); 8910 ASSERT(sfmmup->sfmmu_ismhat == 0); 8911 ASSERT(sfmmu_hat_lock_held(sfmmup)); 8912 ASSERT(szc <= tsb_max_growsize); 8913 8914 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_BUSY)) 8915 return (TSB_LOSTRACE); 8916 8917 /* 8918 * Find the tsb_info ahead of this one in the list, and 8919 * also make sure that the tsb_info passed in really 8920 * exists! 8921 */ 8922 for (prevtsb = NULL, curtsb = sfmmup->sfmmu_tsb; 8923 curtsb != old_tsbinfo && curtsb != NULL; 8924 prevtsb = curtsb, curtsb = curtsb->tsb_next); 8925 ASSERT(curtsb != NULL); 8926 8927 if (!(flags & TSB_SWAPIN) && SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) { 8928 /* 8929 * The process is swapped out, so just set the new size 8930 * code. When it swaps back in, we'll allocate a new one 8931 * of the new chosen size. 8932 */ 8933 curtsb->tsb_szc = szc; 8934 return (TSB_SUCCESS); 8935 } 8936 SFMMU_FLAGS_SET(sfmmup, HAT_BUSY); 8937 8938 tte_sz_mask = old_tsbinfo->tsb_ttesz_mask; 8939 8940 /* 8941 * All initialization is done inside of sfmmu_tsbinfo_alloc(). 8942 * If we fail to allocate a TSB, exit. 8943 */ 8944 sfmmu_hat_exit(hatlockp); 8945 if (sfmmu_tsbinfo_alloc(&new_tsbinfo, szc, tte_sz_mask, 8946 flags, sfmmup)) { 8947 (void) sfmmu_hat_enter(sfmmup); 8948 if (!(flags & TSB_SWAPIN)) 8949 SFMMU_STAT(sf_tsb_resize_failures); 8950 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY); 8951 return (TSB_ALLOCFAIL); 8952 } 8953 (void) sfmmu_hat_enter(sfmmup); 8954 8955 /* 8956 * Re-check to make sure somebody else didn't muck with us while we 8957 * didn't hold the HAT lock. If the process swapped out, fine, just 8958 * exit; this can happen if we try to shrink the TSB from the context 8959 * of another process (such as on an ISM unmap), though it is rare. 8960 */ 8961 if (!(flags & TSB_SWAPIN) && SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) { 8962 SFMMU_STAT(sf_tsb_resize_failures); 8963 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY); 8964 sfmmu_hat_exit(hatlockp); 8965 sfmmu_tsbinfo_free(new_tsbinfo); 8966 (void) sfmmu_hat_enter(sfmmup); 8967 return (TSB_LOSTRACE); 8968 } 8969 8970 #ifdef DEBUG 8971 /* Reverify that the tsb_info still exists.. for debugging only */ 8972 for (prevtsb = NULL, curtsb = sfmmup->sfmmu_tsb; 8973 curtsb != old_tsbinfo && curtsb != NULL; 8974 prevtsb = curtsb, curtsb = curtsb->tsb_next); 8975 ASSERT(curtsb != NULL); 8976 #endif /* DEBUG */ 8977 8978 /* 8979 * Quiesce any CPUs running this process on their next TLB miss 8980 * so they atomically see the new tsb_info. We temporarily set the 8981 * context to invalid context so new threads that come on processor 8982 * after we do the xcall to cpusran will also serialize behind the 8983 * HAT lock on TLB miss and will see the new TSB. Since this short 8984 * race with a new thread coming on processor is relatively rare, 8985 * this synchronization mechanism should be cheaper than always 8986 * pausing all CPUs for the duration of the setup, which is what 8987 * the old implementation did. This is particuarly true if we are 8988 * copying a huge chunk of memory around during that window. 8989 * 8990 * The memory barriers are to make sure things stay consistent 8991 * with resume() since it does not hold the HAT lock while 8992 * walking the list of tsb_info structures. 8993 */ 8994 if ((flags & TSB_SWAPIN) != TSB_SWAPIN) { 8995 /* The TSB is either growing or shrinking. */ 8996 sfmmu_invalidate_ctx(sfmmup); 8997 } else { 8998 /* 8999 * It is illegal to swap in TSBs from a process other 9000 * than a process being swapped in. This in turn 9001 * implies we do not have a valid MMU context here 9002 * since a process needs one to resolve translation 9003 * misses. 9004 */ 9005 ASSERT(curthread->t_procp->p_as->a_hat == sfmmup); 9006 } 9007 9008 #ifdef DEBUG 9009 ASSERT(max_mmu_ctxdoms > 0); 9010 9011 /* 9012 * Process should have INVALID_CONTEXT on all MMUs 9013 */ 9014 for (i = 0; i < max_mmu_ctxdoms; i++) { 9015 9016 ASSERT(sfmmup->sfmmu_ctxs[i].cnum == INVALID_CONTEXT); 9017 } 9018 #endif 9019 9020 new_tsbinfo->tsb_next = old_tsbinfo->tsb_next; 9021 membar_stst(); /* strict ordering required */ 9022 if (prevtsb) 9023 prevtsb->tsb_next = new_tsbinfo; 9024 else 9025 sfmmup->sfmmu_tsb = new_tsbinfo; 9026 membar_enter(); /* make sure new TSB globally visible */ 9027 sfmmu_setup_tsbinfo(sfmmup); 9028 9029 /* 9030 * We need to migrate TSB entries from the old TSB to the new TSB 9031 * if tsb_remap_ttes is set and the TSB is growing. 9032 */ 9033 if (tsb_remap_ttes && ((flags & TSB_GROW) == TSB_GROW)) 9034 sfmmu_copy_tsb(old_tsbinfo, new_tsbinfo); 9035 9036 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY); 9037 9038 /* 9039 * Drop the HAT lock to free our old tsb_info. 9040 */ 9041 sfmmu_hat_exit(hatlockp); 9042 9043 if ((flags & TSB_GROW) == TSB_GROW) { 9044 SFMMU_STAT(sf_tsb_grow); 9045 } else if ((flags & TSB_SHRINK) == TSB_SHRINK) { 9046 SFMMU_STAT(sf_tsb_shrink); 9047 } 9048 9049 sfmmu_tsbinfo_free(old_tsbinfo); 9050 9051 (void) sfmmu_hat_enter(sfmmup); 9052 return (TSB_SUCCESS); 9053 } 9054 9055 /* 9056 * This function will re-program hat pgsz array, and invalidate the 9057 * process' context, forcing the process to switch to another 9058 * context on the next TLB miss, and therefore start using the 9059 * TLB that is reprogrammed for the new page sizes. 9060 */ 9061 void 9062 sfmmu_reprog_pgsz_arr(sfmmu_t *sfmmup, uint8_t *tmp_pgsz) 9063 { 9064 int i; 9065 hatlock_t *hatlockp = NULL; 9066 9067 hatlockp = sfmmu_hat_enter(sfmmup); 9068 /* USIII+-IV+ optimization, requires hat lock */ 9069 if (tmp_pgsz) { 9070 for (i = 0; i < mmu_page_sizes; i++) 9071 sfmmup->sfmmu_pgsz[i] = tmp_pgsz[i]; 9072 } 9073 SFMMU_STAT(sf_tlb_reprog_pgsz); 9074 9075 sfmmu_invalidate_ctx(sfmmup); 9076 9077 sfmmu_hat_exit(hatlockp); 9078 } 9079 9080 /* 9081 * This function assumes that there are either four or six supported page 9082 * sizes and at most two programmable TLBs, so we need to decide which 9083 * page sizes are most important and then tell the MMU layer so it 9084 * can adjust the TLB page sizes accordingly (if supported). 9085 * 9086 * If these assumptions change, this function will need to be 9087 * updated to support whatever the new limits are. 9088 * 9089 * The growing flag is nonzero if we are growing the address space, 9090 * and zero if it is shrinking. This allows us to decide whether 9091 * to grow or shrink our TSB, depending upon available memory 9092 * conditions. 9093 */ 9094 static void 9095 sfmmu_check_page_sizes(sfmmu_t *sfmmup, int growing) 9096 { 9097 uint64_t ttecnt[MMU_PAGE_SIZES]; 9098 uint64_t tte8k_cnt, tte4m_cnt; 9099 uint8_t i; 9100 int sectsb_thresh; 9101 9102 /* 9103 * Kernel threads, processes with small address spaces not using 9104 * large pages, and dummy ISM HATs need not apply. 9105 */ 9106 if (sfmmup == ksfmmup || sfmmup->sfmmu_ismhat != NULL) 9107 return; 9108 9109 if ((sfmmup->sfmmu_flags & HAT_LGPG_FLAGS) == 0 && 9110 sfmmup->sfmmu_ttecnt[TTE8K] <= tsb_rss_factor) 9111 return; 9112 9113 for (i = 0; i < mmu_page_sizes; i++) { 9114 ttecnt[i] = SFMMU_TTE_CNT(sfmmup, i); 9115 } 9116 9117 /* Check pagesizes in use, and possibly reprogram DTLB. */ 9118 if (&mmu_check_page_sizes) 9119 mmu_check_page_sizes(sfmmup, ttecnt); 9120 9121 /* 9122 * Calculate the number of 8k ttes to represent the span of these 9123 * pages. 9124 */ 9125 tte8k_cnt = ttecnt[TTE8K] + 9126 (ttecnt[TTE64K] << (MMU_PAGESHIFT64K - MMU_PAGESHIFT)) + 9127 (ttecnt[TTE512K] << (MMU_PAGESHIFT512K - MMU_PAGESHIFT)); 9128 if (mmu_page_sizes == max_mmu_page_sizes) { 9129 tte4m_cnt = ttecnt[TTE4M] + 9130 (ttecnt[TTE32M] << (MMU_PAGESHIFT32M - MMU_PAGESHIFT4M)) + 9131 (ttecnt[TTE256M] << (MMU_PAGESHIFT256M - MMU_PAGESHIFT4M)); 9132 } else { 9133 tte4m_cnt = ttecnt[TTE4M]; 9134 } 9135 9136 /* 9137 * Inflate TSB sizes by a factor of 2 if this process 9138 * uses 4M text pages to minimize extra conflict misses 9139 * in the first TSB since without counting text pages 9140 * 8K TSB may become too small. 9141 * 9142 * Also double the size of the second TSB to minimize 9143 * extra conflict misses due to competition between 4M text pages 9144 * and data pages. 9145 * 9146 * We need to adjust the second TSB allocation threshold by the 9147 * inflation factor, since there is no point in creating a second 9148 * TSB when we know all the mappings can fit in the I/D TLBs. 9149 */ 9150 sectsb_thresh = tsb_sectsb_threshold; 9151 if (sfmmup->sfmmu_flags & HAT_4MTEXT_FLAG) { 9152 tte8k_cnt <<= 1; 9153 tte4m_cnt <<= 1; 9154 sectsb_thresh <<= 1; 9155 } 9156 9157 /* 9158 * Check to see if our TSB is the right size; we may need to 9159 * grow or shrink it. If the process is small, our work is 9160 * finished at this point. 9161 */ 9162 if (tte8k_cnt <= tsb_rss_factor && tte4m_cnt <= sectsb_thresh) { 9163 return; 9164 } 9165 sfmmu_size_tsb(sfmmup, growing, tte8k_cnt, tte4m_cnt, sectsb_thresh); 9166 } 9167 9168 static void 9169 sfmmu_size_tsb(sfmmu_t *sfmmup, int growing, uint64_t tte8k_cnt, 9170 uint64_t tte4m_cnt, int sectsb_thresh) 9171 { 9172 int tsb_bits; 9173 uint_t tsb_szc; 9174 struct tsb_info *tsbinfop; 9175 hatlock_t *hatlockp = NULL; 9176 9177 hatlockp = sfmmu_hat_enter(sfmmup); 9178 ASSERT(hatlockp != NULL); 9179 tsbinfop = sfmmup->sfmmu_tsb; 9180 ASSERT(tsbinfop != NULL); 9181 9182 /* 9183 * If we're growing, select the size based on RSS. If we're 9184 * shrinking, leave some room so we don't have to turn around and 9185 * grow again immediately. 9186 */ 9187 if (growing) 9188 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt); 9189 else 9190 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt << 1); 9191 9192 if (!growing && (tsb_szc < tsbinfop->tsb_szc) && 9193 (tsb_szc >= default_tsb_size) && TSB_OK_SHRINK()) { 9194 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, tsb_szc, 9195 hatlockp, TSB_SHRINK); 9196 } else if (growing && tsb_szc > tsbinfop->tsb_szc && TSB_OK_GROW()) { 9197 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, tsb_szc, 9198 hatlockp, TSB_GROW); 9199 } 9200 tsbinfop = sfmmup->sfmmu_tsb; 9201 9202 /* 9203 * With the TLB and first TSB out of the way, we need to see if 9204 * we need a second TSB for 4M pages. If we managed to reprogram 9205 * the TLB page sizes above, the process will start using this new 9206 * TSB right away; otherwise, it will start using it on the next 9207 * context switch. Either way, it's no big deal so there's no 9208 * synchronization with the trap handlers here unless we grow the 9209 * TSB (in which case it's required to prevent using the old one 9210 * after it's freed). Note: second tsb is required for 32M/256M 9211 * page sizes. 9212 */ 9213 if (tte4m_cnt > sectsb_thresh) { 9214 /* 9215 * If we're growing, select the size based on RSS. If we're 9216 * shrinking, leave some room so we don't have to turn 9217 * around and grow again immediately. 9218 */ 9219 if (growing) 9220 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt); 9221 else 9222 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt << 1); 9223 if (tsbinfop->tsb_next == NULL) { 9224 struct tsb_info *newtsb; 9225 int allocflags = SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)? 9226 0 : TSB_ALLOC; 9227 9228 sfmmu_hat_exit(hatlockp); 9229 9230 /* 9231 * Try to allocate a TSB for 4[32|256]M pages. If we 9232 * can't get the size we want, retry w/a minimum sized 9233 * TSB. If that still didn't work, give up; we can 9234 * still run without one. 9235 */ 9236 tsb_bits = (mmu_page_sizes == max_mmu_page_sizes)? 9237 TSB4M|TSB32M|TSB256M:TSB4M; 9238 if ((sfmmu_tsbinfo_alloc(&newtsb, tsb_szc, tsb_bits, 9239 allocflags, sfmmup) != 0) && 9240 (sfmmu_tsbinfo_alloc(&newtsb, TSB_MIN_SZCODE, 9241 tsb_bits, allocflags, sfmmup) != 0)) { 9242 return; 9243 } 9244 9245 hatlockp = sfmmu_hat_enter(sfmmup); 9246 9247 if (sfmmup->sfmmu_tsb->tsb_next == NULL) { 9248 sfmmup->sfmmu_tsb->tsb_next = newtsb; 9249 SFMMU_STAT(sf_tsb_sectsb_create); 9250 sfmmu_setup_tsbinfo(sfmmup); 9251 sfmmu_hat_exit(hatlockp); 9252 return; 9253 } else { 9254 /* 9255 * It's annoying, but possible for us 9256 * to get here.. we dropped the HAT lock 9257 * because of locking order in the kmem 9258 * allocator, and while we were off getting 9259 * our memory, some other thread decided to 9260 * do us a favor and won the race to get a 9261 * second TSB for this process. Sigh. 9262 */ 9263 sfmmu_hat_exit(hatlockp); 9264 sfmmu_tsbinfo_free(newtsb); 9265 return; 9266 } 9267 } 9268 9269 /* 9270 * We have a second TSB, see if it's big enough. 9271 */ 9272 tsbinfop = tsbinfop->tsb_next; 9273 9274 /* 9275 * Check to see if our second TSB is the right size; 9276 * we may need to grow or shrink it. 9277 * To prevent thrashing (e.g. growing the TSB on a 9278 * subsequent map operation), only try to shrink if 9279 * the TSB reach exceeds twice the virtual address 9280 * space size. 9281 */ 9282 if (!growing && (tsb_szc < tsbinfop->tsb_szc) && 9283 (tsb_szc >= default_tsb_size) && TSB_OK_SHRINK()) { 9284 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, 9285 tsb_szc, hatlockp, TSB_SHRINK); 9286 } else if (growing && tsb_szc > tsbinfop->tsb_szc && 9287 TSB_OK_GROW()) { 9288 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, 9289 tsb_szc, hatlockp, TSB_GROW); 9290 } 9291 } 9292 9293 sfmmu_hat_exit(hatlockp); 9294 } 9295 9296 /* 9297 * Get the preferred page size code for a hat. 9298 * This is only advice, so locking is not done; 9299 * this transitory information could change 9300 * following the call anyway. This interface is 9301 * sun4 private. 9302 */ 9303 /*ARGSUSED*/ 9304 uint_t 9305 hat_preferred_pgsz(struct hat *hat, caddr_t vaddr, size_t maplen, int maptype) 9306 { 9307 sfmmu_t *sfmmup = (sfmmu_t *)hat; 9308 uint_t szc, maxszc = mmu_page_sizes - 1; 9309 size_t pgsz; 9310 9311 if (maptype == MAPPGSZ_ISM) { 9312 for (szc = maxszc; szc >= TTE4M; szc--) { 9313 if (disable_ism_large_pages & (1 << szc)) 9314 continue; 9315 9316 pgsz = hw_page_array[szc].hp_size; 9317 if ((maplen >= pgsz) && IS_P2ALIGNED(vaddr, pgsz)) 9318 return (szc); 9319 } 9320 return (TTE4M); 9321 } else if (&mmu_preferred_pgsz) { /* USIII+-USIV+ */ 9322 return (mmu_preferred_pgsz(sfmmup, vaddr, maplen)); 9323 } else { /* USIII, USII, Niagara */ 9324 for (szc = maxszc; szc > TTE8K; szc--) { 9325 if (disable_large_pages & (1 << szc)) 9326 continue; 9327 9328 pgsz = hw_page_array[szc].hp_size; 9329 if ((maplen >= pgsz) && IS_P2ALIGNED(vaddr, pgsz)) 9330 return (szc); 9331 } 9332 return (TTE8K); 9333 } 9334 } 9335 9336 /* 9337 * Free up a sfmmu 9338 * Since the sfmmu is currently embedded in the hat struct we simply zero 9339 * out our fields and free up the ism map blk list if any. 9340 */ 9341 static void 9342 sfmmu_free_sfmmu(sfmmu_t *sfmmup) 9343 { 9344 ism_blk_t *blkp, *nx_blkp; 9345 #ifdef DEBUG 9346 ism_map_t *map; 9347 int i; 9348 #endif 9349 9350 ASSERT(sfmmup->sfmmu_ttecnt[TTE8K] == 0); 9351 ASSERT(sfmmup->sfmmu_ttecnt[TTE64K] == 0); 9352 ASSERT(sfmmup->sfmmu_ttecnt[TTE512K] == 0); 9353 ASSERT(sfmmup->sfmmu_ttecnt[TTE4M] == 0); 9354 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0); 9355 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0); 9356 9357 sfmmup->sfmmu_free = 0; 9358 sfmmup->sfmmu_ismhat = 0; 9359 9360 blkp = sfmmup->sfmmu_iblk; 9361 sfmmup->sfmmu_iblk = NULL; 9362 9363 while (blkp) { 9364 #ifdef DEBUG 9365 map = blkp->iblk_maps; 9366 for (i = 0; i < ISM_MAP_SLOTS; i++) { 9367 ASSERT(map[i].imap_seg == 0); 9368 ASSERT(map[i].imap_ismhat == NULL); 9369 ASSERT(map[i].imap_ment == NULL); 9370 } 9371 #endif 9372 nx_blkp = blkp->iblk_next; 9373 blkp->iblk_next = NULL; 9374 blkp->iblk_nextpa = (uint64_t)-1; 9375 kmem_cache_free(ism_blk_cache, blkp); 9376 blkp = nx_blkp; 9377 } 9378 } 9379 9380 /* 9381 * Locking primitves accessed by HATLOCK macros 9382 */ 9383 9384 #define SFMMU_SPL_MTX (0x0) 9385 #define SFMMU_ML_MTX (0x1) 9386 9387 #define SFMMU_MLSPL_MTX(type, pg) (((type) == SFMMU_SPL_MTX) ? \ 9388 SPL_HASH(pg) : MLIST_HASH(pg)) 9389 9390 kmutex_t * 9391 sfmmu_page_enter(struct page *pp) 9392 { 9393 return (sfmmu_mlspl_enter(pp, SFMMU_SPL_MTX)); 9394 } 9395 9396 void 9397 sfmmu_page_exit(kmutex_t *spl) 9398 { 9399 mutex_exit(spl); 9400 } 9401 9402 int 9403 sfmmu_page_spl_held(struct page *pp) 9404 { 9405 return (sfmmu_mlspl_held(pp, SFMMU_SPL_MTX)); 9406 } 9407 9408 kmutex_t * 9409 sfmmu_mlist_enter(struct page *pp) 9410 { 9411 return (sfmmu_mlspl_enter(pp, SFMMU_ML_MTX)); 9412 } 9413 9414 void 9415 sfmmu_mlist_exit(kmutex_t *mml) 9416 { 9417 mutex_exit(mml); 9418 } 9419 9420 int 9421 sfmmu_mlist_held(struct page *pp) 9422 { 9423 9424 return (sfmmu_mlspl_held(pp, SFMMU_ML_MTX)); 9425 } 9426 9427 /* 9428 * Common code for sfmmu_mlist_enter() and sfmmu_page_enter(). For 9429 * sfmmu_mlist_enter() case mml_table lock array is used and for 9430 * sfmmu_page_enter() sfmmu_page_lock lock array is used. 9431 * 9432 * The lock is taken on a root page so that it protects an operation on all 9433 * constituent pages of a large page pp belongs to. 9434 * 9435 * The routine takes a lock from the appropriate array. The lock is determined 9436 * by hashing the root page. After taking the lock this routine checks if the 9437 * root page has the same size code that was used to determine the root (i.e 9438 * that root hasn't changed). If root page has the expected p_szc field we 9439 * have the right lock and it's returned to the caller. If root's p_szc 9440 * decreased we release the lock and retry from the beginning. This case can 9441 * happen due to hat_page_demote() decreasing p_szc between our load of p_szc 9442 * value and taking the lock. The number of retries due to p_szc decrease is 9443 * limited by the maximum p_szc value. If p_szc is 0 we return the lock 9444 * determined by hashing pp itself. 9445 * 9446 * If our caller doesn't hold a SE_SHARED or SE_EXCL lock on pp it's also 9447 * possible that p_szc can increase. To increase p_szc a thread has to lock 9448 * all constituent pages EXCL and do hat_pageunload() on all of them. All the 9449 * callers that don't hold a page locked recheck if hmeblk through which pp 9450 * was found still maps this pp. If it doesn't map it anymore returned lock 9451 * is immediately dropped. Therefore if sfmmu_mlspl_enter() hits the case of 9452 * p_szc increase after taking the lock it returns this lock without further 9453 * retries because in this case the caller doesn't care about which lock was 9454 * taken. The caller will drop it right away. 9455 * 9456 * After the routine returns it's guaranteed that hat_page_demote() can't 9457 * change p_szc field of any of constituent pages of a large page pp belongs 9458 * to as long as pp was either locked at least SHARED prior to this call or 9459 * the caller finds that hment that pointed to this pp still references this 9460 * pp (this also assumes that the caller holds hme hash bucket lock so that 9461 * the same pp can't be remapped into the same hmeblk after it was unmapped by 9462 * hat_pageunload()). 9463 */ 9464 static kmutex_t * 9465 sfmmu_mlspl_enter(struct page *pp, int type) 9466 { 9467 kmutex_t *mtx; 9468 uint_t prev_rszc = UINT_MAX; 9469 page_t *rootpp; 9470 uint_t szc; 9471 uint_t rszc; 9472 uint_t pszc = pp->p_szc; 9473 9474 ASSERT(pp != NULL); 9475 9476 again: 9477 if (pszc == 0) { 9478 mtx = SFMMU_MLSPL_MTX(type, pp); 9479 mutex_enter(mtx); 9480 return (mtx); 9481 } 9482 9483 /* The lock lives in the root page */ 9484 rootpp = PP_GROUPLEADER(pp, pszc); 9485 mtx = SFMMU_MLSPL_MTX(type, rootpp); 9486 mutex_enter(mtx); 9487 9488 /* 9489 * Return mml in the following 3 cases: 9490 * 9491 * 1) If pp itself is root since if its p_szc decreased before we took 9492 * the lock pp is still the root of smaller szc page. And if its p_szc 9493 * increased it doesn't matter what lock we return (see comment in 9494 * front of this routine). 9495 * 9496 * 2) If pp's not root but rootpp is the root of a rootpp->p_szc size 9497 * large page we have the right lock since any previous potential 9498 * hat_page_demote() is done demoting from greater than current root's 9499 * p_szc because hat_page_demote() changes root's p_szc last. No 9500 * further hat_page_demote() can start or be in progress since it 9501 * would need the same lock we currently hold. 9502 * 9503 * 3) If rootpp's p_szc increased since previous iteration it doesn't 9504 * matter what lock we return (see comment in front of this routine). 9505 */ 9506 if (pp == rootpp || (rszc = rootpp->p_szc) == pszc || 9507 rszc >= prev_rszc) { 9508 return (mtx); 9509 } 9510 9511 /* 9512 * hat_page_demote() could have decreased root's p_szc. 9513 * In this case pp's p_szc must also be smaller than pszc. 9514 * Retry. 9515 */ 9516 if (rszc < pszc) { 9517 szc = pp->p_szc; 9518 if (szc < pszc) { 9519 mutex_exit(mtx); 9520 pszc = szc; 9521 goto again; 9522 } 9523 /* 9524 * pp's p_szc increased after it was decreased. 9525 * page cannot be mapped. Return current lock. The caller 9526 * will drop it right away. 9527 */ 9528 return (mtx); 9529 } 9530 9531 /* 9532 * root's p_szc is greater than pp's p_szc. 9533 * hat_page_demote() is not done with all pages 9534 * yet. Wait for it to complete. 9535 */ 9536 mutex_exit(mtx); 9537 rootpp = PP_GROUPLEADER(rootpp, rszc); 9538 mtx = SFMMU_MLSPL_MTX(type, rootpp); 9539 mutex_enter(mtx); 9540 mutex_exit(mtx); 9541 prev_rszc = rszc; 9542 goto again; 9543 } 9544 9545 static int 9546 sfmmu_mlspl_held(struct page *pp, int type) 9547 { 9548 kmutex_t *mtx; 9549 9550 ASSERT(pp != NULL); 9551 /* The lock lives in the root page */ 9552 pp = PP_PAGEROOT(pp); 9553 ASSERT(pp != NULL); 9554 9555 mtx = SFMMU_MLSPL_MTX(type, pp); 9556 return (MUTEX_HELD(mtx)); 9557 } 9558 9559 static uint_t 9560 sfmmu_get_free_hblk(struct hme_blk **hmeblkpp, uint_t critical) 9561 { 9562 struct hme_blk *hblkp; 9563 9564 if (freehblkp != NULL) { 9565 mutex_enter(&freehblkp_lock); 9566 if (freehblkp != NULL) { 9567 /* 9568 * If the current thread is owning hblk_reserve, 9569 * let it succede even if freehblkcnt is really low. 9570 */ 9571 if (freehblkcnt <= HBLK_RESERVE_MIN && !critical) { 9572 SFMMU_STAT(sf_get_free_throttle); 9573 mutex_exit(&freehblkp_lock); 9574 return (0); 9575 } 9576 freehblkcnt--; 9577 *hmeblkpp = freehblkp; 9578 hblkp = *hmeblkpp; 9579 freehblkp = hblkp->hblk_next; 9580 mutex_exit(&freehblkp_lock); 9581 hblkp->hblk_next = NULL; 9582 SFMMU_STAT(sf_get_free_success); 9583 return (1); 9584 } 9585 mutex_exit(&freehblkp_lock); 9586 } 9587 SFMMU_STAT(sf_get_free_fail); 9588 return (0); 9589 } 9590 9591 static uint_t 9592 sfmmu_put_free_hblk(struct hme_blk *hmeblkp, uint_t critical) 9593 { 9594 struct hme_blk *hblkp; 9595 9596 /* 9597 * If the current thread is mapping into kernel space, 9598 * let it succede even if freehblkcnt is max 9599 * so that it will avoid freeing it to kmem. 9600 * This will prevent stack overflow due to 9601 * possible recursion since kmem_cache_free() 9602 * might require creation of a slab which 9603 * in turn needs an hmeblk to map that slab; 9604 * let's break this vicious chain at the first 9605 * opportunity. 9606 */ 9607 if (freehblkcnt < HBLK_RESERVE_CNT || critical) { 9608 mutex_enter(&freehblkp_lock); 9609 if (freehblkcnt < HBLK_RESERVE_CNT || critical) { 9610 SFMMU_STAT(sf_put_free_success); 9611 freehblkcnt++; 9612 hmeblkp->hblk_next = freehblkp; 9613 freehblkp = hmeblkp; 9614 mutex_exit(&freehblkp_lock); 9615 return (1); 9616 } 9617 mutex_exit(&freehblkp_lock); 9618 } 9619 9620 /* 9621 * Bring down freehblkcnt to HBLK_RESERVE_CNT. We are here 9622 * only if freehblkcnt is at least HBLK_RESERVE_CNT *and* 9623 * we are not in the process of mapping into kernel space. 9624 */ 9625 ASSERT(!critical); 9626 while (freehblkcnt > HBLK_RESERVE_CNT) { 9627 mutex_enter(&freehblkp_lock); 9628 if (freehblkcnt > HBLK_RESERVE_CNT) { 9629 freehblkcnt--; 9630 hblkp = freehblkp; 9631 freehblkp = hblkp->hblk_next; 9632 mutex_exit(&freehblkp_lock); 9633 ASSERT(get_hblk_cache(hblkp) == sfmmu8_cache); 9634 kmem_cache_free(sfmmu8_cache, hblkp); 9635 continue; 9636 } 9637 mutex_exit(&freehblkp_lock); 9638 } 9639 SFMMU_STAT(sf_put_free_fail); 9640 return (0); 9641 } 9642 9643 static void 9644 sfmmu_hblk_swap(struct hme_blk *new) 9645 { 9646 struct hme_blk *old, *hblkp, *prev; 9647 uint64_t hblkpa, prevpa, newpa; 9648 caddr_t base, vaddr, endaddr; 9649 struct hmehash_bucket *hmebp; 9650 struct sf_hment *osfhme, *nsfhme; 9651 page_t *pp; 9652 kmutex_t *pml; 9653 tte_t tte; 9654 9655 #ifdef DEBUG 9656 hmeblk_tag hblktag; 9657 struct hme_blk *found; 9658 #endif 9659 old = HBLK_RESERVE; 9660 9661 /* 9662 * save pa before bcopy clobbers it 9663 */ 9664 newpa = new->hblk_nextpa; 9665 9666 base = (caddr_t)get_hblk_base(old); 9667 endaddr = base + get_hblk_span(old); 9668 9669 /* 9670 * acquire hash bucket lock. 9671 */ 9672 hmebp = sfmmu_tteload_acquire_hashbucket(ksfmmup, base, TTE8K); 9673 9674 /* 9675 * copy contents from old to new 9676 */ 9677 bcopy((void *)old, (void *)new, HME8BLK_SZ); 9678 9679 /* 9680 * add new to hash chain 9681 */ 9682 sfmmu_hblk_hash_add(hmebp, new, newpa); 9683 9684 /* 9685 * search hash chain for hblk_reserve; this needs to be performed 9686 * after adding new, otherwise prevpa and prev won't correspond 9687 * to the hblk which is prior to old in hash chain when we call 9688 * sfmmu_hblk_hash_rm to remove old later. 9689 */ 9690 for (prevpa = 0, prev = NULL, 9691 hblkpa = hmebp->hmeh_nextpa, hblkp = hmebp->hmeblkp; 9692 hblkp != NULL && hblkp != old; 9693 prevpa = hblkpa, prev = hblkp, 9694 hblkpa = hblkp->hblk_nextpa, hblkp = hblkp->hblk_next); 9695 9696 if (hblkp != old) 9697 panic("sfmmu_hblk_swap: hblk_reserve not found"); 9698 9699 /* 9700 * p_mapping list is still pointing to hments in hblk_reserve; 9701 * fix up p_mapping list so that they point to hments in new. 9702 * 9703 * Since all these mappings are created by hblk_reserve_thread 9704 * on the way and it's using at least one of the buffers from each of 9705 * the newly minted slabs, there is no danger of any of these 9706 * mappings getting unloaded by another thread. 9707 * 9708 * tsbmiss could only modify ref/mod bits of hments in old/new. 9709 * Since all of these hments hold mappings established by segkmem 9710 * and mappings in segkmem are setup with HAT_NOSYNC, ref/mod bits 9711 * have no meaning for the mappings in hblk_reserve. hments in 9712 * old and new are identical except for ref/mod bits. 9713 */ 9714 for (vaddr = base; vaddr < endaddr; vaddr += TTEBYTES(TTE8K)) { 9715 9716 HBLKTOHME(osfhme, old, vaddr); 9717 sfmmu_copytte(&osfhme->hme_tte, &tte); 9718 9719 if (TTE_IS_VALID(&tte)) { 9720 if ((pp = osfhme->hme_page) == NULL) 9721 panic("sfmmu_hblk_swap: page not mapped"); 9722 9723 pml = sfmmu_mlist_enter(pp); 9724 9725 if (pp != osfhme->hme_page) 9726 panic("sfmmu_hblk_swap: mapping changed"); 9727 9728 HBLKTOHME(nsfhme, new, vaddr); 9729 9730 HME_ADD(nsfhme, pp); 9731 HME_SUB(osfhme, pp); 9732 9733 sfmmu_mlist_exit(pml); 9734 } 9735 } 9736 9737 /* 9738 * remove old from hash chain 9739 */ 9740 sfmmu_hblk_hash_rm(hmebp, old, prevpa, prev); 9741 9742 #ifdef DEBUG 9743 9744 hblktag.htag_id = ksfmmup; 9745 hblktag.htag_bspage = HME_HASH_BSPAGE(base, HME_HASH_SHIFT(TTE8K)); 9746 hblktag.htag_rehash = HME_HASH_REHASH(TTE8K); 9747 HME_HASH_FAST_SEARCH(hmebp, hblktag, found); 9748 9749 if (found != new) 9750 panic("sfmmu_hblk_swap: new hblk not found"); 9751 #endif 9752 9753 SFMMU_HASH_UNLOCK(hmebp); 9754 9755 /* 9756 * Reset hblk_reserve 9757 */ 9758 bzero((void *)old, HME8BLK_SZ); 9759 old->hblk_nextpa = va_to_pa((caddr_t)old); 9760 } 9761 9762 /* 9763 * Grab the mlist mutex for both pages passed in. 9764 * 9765 * low and high will be returned as pointers to the mutexes for these pages. 9766 * low refers to the mutex residing in the lower bin of the mlist hash, while 9767 * high refers to the mutex residing in the higher bin of the mlist hash. This 9768 * is due to the locking order restrictions on the same thread grabbing 9769 * multiple mlist mutexes. The low lock must be acquired before the high lock. 9770 * 9771 * If both pages hash to the same mutex, only grab that single mutex, and 9772 * high will be returned as NULL 9773 * If the pages hash to different bins in the hash, grab the lower addressed 9774 * lock first and then the higher addressed lock in order to follow the locking 9775 * rules involved with the same thread grabbing multiple mlist mutexes. 9776 * low and high will both have non-NULL values. 9777 */ 9778 static void 9779 sfmmu_mlist_reloc_enter(struct page *targ, struct page *repl, 9780 kmutex_t **low, kmutex_t **high) 9781 { 9782 kmutex_t *mml_targ, *mml_repl; 9783 9784 /* 9785 * no need to do the dance around szc as in sfmmu_mlist_enter() 9786 * because this routine is only called by hat_page_relocate() and all 9787 * targ and repl pages are already locked EXCL so szc can't change. 9788 */ 9789 9790 mml_targ = MLIST_HASH(PP_PAGEROOT(targ)); 9791 mml_repl = MLIST_HASH(PP_PAGEROOT(repl)); 9792 9793 if (mml_targ == mml_repl) { 9794 *low = mml_targ; 9795 *high = NULL; 9796 } else { 9797 if (mml_targ < mml_repl) { 9798 *low = mml_targ; 9799 *high = mml_repl; 9800 } else { 9801 *low = mml_repl; 9802 *high = mml_targ; 9803 } 9804 } 9805 9806 mutex_enter(*low); 9807 if (*high) 9808 mutex_enter(*high); 9809 } 9810 9811 static void 9812 sfmmu_mlist_reloc_exit(kmutex_t *low, kmutex_t *high) 9813 { 9814 if (high) 9815 mutex_exit(high); 9816 mutex_exit(low); 9817 } 9818 9819 static hatlock_t * 9820 sfmmu_hat_enter(sfmmu_t *sfmmup) 9821 { 9822 hatlock_t *hatlockp; 9823 9824 if (sfmmup != ksfmmup) { 9825 hatlockp = TSB_HASH(sfmmup); 9826 mutex_enter(HATLOCK_MUTEXP(hatlockp)); 9827 return (hatlockp); 9828 } 9829 return (NULL); 9830 } 9831 9832 static hatlock_t * 9833 sfmmu_hat_tryenter(sfmmu_t *sfmmup) 9834 { 9835 hatlock_t *hatlockp; 9836 9837 if (sfmmup != ksfmmup) { 9838 hatlockp = TSB_HASH(sfmmup); 9839 if (mutex_tryenter(HATLOCK_MUTEXP(hatlockp)) == 0) 9840 return (NULL); 9841 return (hatlockp); 9842 } 9843 return (NULL); 9844 } 9845 9846 static void 9847 sfmmu_hat_exit(hatlock_t *hatlockp) 9848 { 9849 if (hatlockp != NULL) 9850 mutex_exit(HATLOCK_MUTEXP(hatlockp)); 9851 } 9852 9853 static void 9854 sfmmu_hat_lock_all(void) 9855 { 9856 int i; 9857 for (i = 0; i < SFMMU_NUM_LOCK; i++) 9858 mutex_enter(HATLOCK_MUTEXP(&hat_lock[i])); 9859 } 9860 9861 static void 9862 sfmmu_hat_unlock_all(void) 9863 { 9864 int i; 9865 for (i = SFMMU_NUM_LOCK - 1; i >= 0; i--) 9866 mutex_exit(HATLOCK_MUTEXP(&hat_lock[i])); 9867 } 9868 9869 int 9870 sfmmu_hat_lock_held(sfmmu_t *sfmmup) 9871 { 9872 ASSERT(sfmmup != ksfmmup); 9873 return (MUTEX_HELD(HATLOCK_MUTEXP(TSB_HASH(sfmmup)))); 9874 } 9875 9876 /* 9877 * Locking primitives to provide consistency between ISM unmap 9878 * and other operations. Since ISM unmap can take a long time, we 9879 * use HAT_ISMBUSY flag (protected by the hatlock) to avoid creating 9880 * contention on the hatlock buckets while ISM segments are being 9881 * unmapped. The tradeoff is that the flags don't prevent priority 9882 * inversion from occurring, so we must request kernel priority in 9883 * case we have to sleep to keep from getting buried while holding 9884 * the HAT_ISMBUSY flag set, which in turn could block other kernel 9885 * threads from running (for example, in sfmmu_uvatopfn()). 9886 */ 9887 static void 9888 sfmmu_ismhat_enter(sfmmu_t *sfmmup, int hatlock_held) 9889 { 9890 hatlock_t *hatlockp; 9891 9892 THREAD_KPRI_REQUEST(); 9893 if (!hatlock_held) 9894 hatlockp = sfmmu_hat_enter(sfmmup); 9895 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)) 9896 cv_wait(&sfmmup->sfmmu_tsb_cv, HATLOCK_MUTEXP(hatlockp)); 9897 SFMMU_FLAGS_SET(sfmmup, HAT_ISMBUSY); 9898 if (!hatlock_held) 9899 sfmmu_hat_exit(hatlockp); 9900 } 9901 9902 static void 9903 sfmmu_ismhat_exit(sfmmu_t *sfmmup, int hatlock_held) 9904 { 9905 hatlock_t *hatlockp; 9906 9907 if (!hatlock_held) 9908 hatlockp = sfmmu_hat_enter(sfmmup); 9909 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)); 9910 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ISMBUSY); 9911 cv_broadcast(&sfmmup->sfmmu_tsb_cv); 9912 if (!hatlock_held) 9913 sfmmu_hat_exit(hatlockp); 9914 THREAD_KPRI_RELEASE(); 9915 } 9916 9917 /* 9918 * 9919 * Algorithm: 9920 * 9921 * (1) if segkmem is not ready, allocate hblk from an array of pre-alloc'ed 9922 * hblks. 9923 * 9924 * (2) if we are allocating an hblk for mapping a slab in sfmmu_cache, 9925 * 9926 * (a) try to return an hblk from reserve pool of free hblks; 9927 * (b) if the reserve pool is empty, acquire hblk_reserve_lock 9928 * and return hblk_reserve. 9929 * 9930 * (3) call kmem_cache_alloc() to allocate hblk; 9931 * 9932 * (a) if hblk_reserve_lock is held by the current thread, 9933 * atomically replace hblk_reserve by the hblk that is 9934 * returned by kmem_cache_alloc; release hblk_reserve_lock 9935 * and call kmem_cache_alloc() again. 9936 * (b) if reserve pool is not full, add the hblk that is 9937 * returned by kmem_cache_alloc to reserve pool and 9938 * call kmem_cache_alloc again. 9939 * 9940 */ 9941 static struct hme_blk * 9942 sfmmu_hblk_alloc(sfmmu_t *sfmmup, caddr_t vaddr, 9943 struct hmehash_bucket *hmebp, uint_t size, hmeblk_tag hblktag, 9944 uint_t flags) 9945 { 9946 struct hme_blk *hmeblkp = NULL; 9947 struct hme_blk *newhblkp; 9948 struct hme_blk *shw_hblkp = NULL; 9949 struct kmem_cache *sfmmu_cache = NULL; 9950 uint64_t hblkpa; 9951 ulong_t index; 9952 uint_t owner; /* set to 1 if using hblk_reserve */ 9953 uint_t forcefree; 9954 int sleep; 9955 9956 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 9957 9958 /* 9959 * If segkmem is not created yet, allocate from static hmeblks 9960 * created at the end of startup_modules(). See the block comment 9961 * in startup_modules() describing how we estimate the number of 9962 * static hmeblks that will be needed during re-map. 9963 */ 9964 if (!hblk_alloc_dynamic) { 9965 9966 if (size == TTE8K) { 9967 index = nucleus_hblk8.index; 9968 if (index >= nucleus_hblk8.len) { 9969 /* 9970 * If we panic here, see startup_modules() to 9971 * make sure that we are calculating the 9972 * number of hblk8's that we need correctly. 9973 */ 9974 panic("no nucleus hblk8 to allocate"); 9975 } 9976 hmeblkp = 9977 (struct hme_blk *)&nucleus_hblk8.list[index]; 9978 nucleus_hblk8.index++; 9979 SFMMU_STAT(sf_hblk8_nalloc); 9980 } else { 9981 index = nucleus_hblk1.index; 9982 if (nucleus_hblk1.index >= nucleus_hblk1.len) { 9983 /* 9984 * If we panic here, see startup_modules() 9985 * and H8TOH1; most likely you need to 9986 * update the calculation of the number 9987 * of hblk1's the kernel needs to boot. 9988 */ 9989 panic("no nucleus hblk1 to allocate"); 9990 } 9991 hmeblkp = 9992 (struct hme_blk *)&nucleus_hblk1.list[index]; 9993 nucleus_hblk1.index++; 9994 SFMMU_STAT(sf_hblk1_nalloc); 9995 } 9996 9997 goto hblk_init; 9998 } 9999 10000 SFMMU_HASH_UNLOCK(hmebp); 10001 10002 if (sfmmup != KHATID) { 10003 if (mmu_page_sizes == max_mmu_page_sizes) { 10004 if (size < TTE256M) 10005 shw_hblkp = sfmmu_shadow_hcreate(sfmmup, vaddr, 10006 size, flags); 10007 } else { 10008 if (size < TTE4M) 10009 shw_hblkp = sfmmu_shadow_hcreate(sfmmup, vaddr, 10010 size, flags); 10011 } 10012 } 10013 10014 fill_hblk: 10015 owner = (hblk_reserve_thread == curthread) ? 1 : 0; 10016 10017 if (owner && size == TTE8K) { 10018 10019 /* 10020 * We are really in a tight spot. We already own 10021 * hblk_reserve and we need another hblk. In anticipation 10022 * of this kind of scenario, we specifically set aside 10023 * HBLK_RESERVE_MIN number of hblks to be used exclusively 10024 * by owner of hblk_reserve. 10025 */ 10026 SFMMU_STAT(sf_hblk_recurse_cnt); 10027 10028 if (!sfmmu_get_free_hblk(&hmeblkp, 1)) 10029 panic("sfmmu_hblk_alloc: reserve list is empty"); 10030 10031 goto hblk_verify; 10032 } 10033 10034 ASSERT(!owner); 10035 10036 if ((flags & HAT_NO_KALLOC) == 0) { 10037 10038 sfmmu_cache = ((size == TTE8K) ? sfmmu8_cache : sfmmu1_cache); 10039 sleep = ((sfmmup == KHATID) ? KM_NOSLEEP : KM_SLEEP); 10040 10041 if ((hmeblkp = kmem_cache_alloc(sfmmu_cache, sleep)) == NULL) { 10042 hmeblkp = sfmmu_hblk_steal(size); 10043 } else { 10044 /* 10045 * if we are the owner of hblk_reserve, 10046 * swap hblk_reserve with hmeblkp and 10047 * start a fresh life. Hope things go 10048 * better this time. 10049 */ 10050 if (hblk_reserve_thread == curthread) { 10051 ASSERT(sfmmu_cache == sfmmu8_cache); 10052 sfmmu_hblk_swap(hmeblkp); 10053 hblk_reserve_thread = NULL; 10054 mutex_exit(&hblk_reserve_lock); 10055 goto fill_hblk; 10056 } 10057 /* 10058 * let's donate this hblk to our reserve list if 10059 * we are not mapping kernel range 10060 */ 10061 if (size == TTE8K && sfmmup != KHATID) 10062 if (sfmmu_put_free_hblk(hmeblkp, 0)) 10063 goto fill_hblk; 10064 } 10065 } else { 10066 /* 10067 * We are here to map the slab in sfmmu8_cache; let's 10068 * check if we could tap our reserve list; if successful, 10069 * this will avoid the pain of going thru sfmmu_hblk_swap 10070 */ 10071 SFMMU_STAT(sf_hblk_slab_cnt); 10072 if (!sfmmu_get_free_hblk(&hmeblkp, 0)) { 10073 /* 10074 * let's start hblk_reserve dance 10075 */ 10076 SFMMU_STAT(sf_hblk_reserve_cnt); 10077 owner = 1; 10078 mutex_enter(&hblk_reserve_lock); 10079 hmeblkp = HBLK_RESERVE; 10080 hblk_reserve_thread = curthread; 10081 } 10082 } 10083 10084 hblk_verify: 10085 ASSERT(hmeblkp != NULL); 10086 set_hblk_sz(hmeblkp, size); 10087 ASSERT(hmeblkp->hblk_nextpa == va_to_pa((caddr_t)hmeblkp)); 10088 SFMMU_HASH_LOCK(hmebp); 10089 HME_HASH_FAST_SEARCH(hmebp, hblktag, newhblkp); 10090 if (newhblkp != NULL) { 10091 SFMMU_HASH_UNLOCK(hmebp); 10092 if (hmeblkp != HBLK_RESERVE) { 10093 /* 10094 * This is really tricky! 10095 * 10096 * vmem_alloc(vmem_seg_arena) 10097 * vmem_alloc(vmem_internal_arena) 10098 * segkmem_alloc(heap_arena) 10099 * vmem_alloc(heap_arena) 10100 * page_create() 10101 * hat_memload() 10102 * kmem_cache_free() 10103 * kmem_cache_alloc() 10104 * kmem_slab_create() 10105 * vmem_alloc(kmem_internal_arena) 10106 * segkmem_alloc(heap_arena) 10107 * vmem_alloc(heap_arena) 10108 * page_create() 10109 * hat_memload() 10110 * kmem_cache_free() 10111 * ... 10112 * 10113 * Thus, hat_memload() could call kmem_cache_free 10114 * for enough number of times that we could easily 10115 * hit the bottom of the stack or run out of reserve 10116 * list of vmem_seg structs. So, we must donate 10117 * this hblk to reserve list if it's allocated 10118 * from sfmmu8_cache *and* mapping kernel range. 10119 * We don't need to worry about freeing hmeblk1's 10120 * to kmem since they don't map any kmem slabs. 10121 * 10122 * Note: When segkmem supports largepages, we must 10123 * free hmeblk1's to reserve list as well. 10124 */ 10125 forcefree = (sfmmup == KHATID) ? 1 : 0; 10126 if (size == TTE8K && 10127 sfmmu_put_free_hblk(hmeblkp, forcefree)) { 10128 goto re_verify; 10129 } 10130 ASSERT(sfmmup != KHATID); 10131 kmem_cache_free(get_hblk_cache(hmeblkp), hmeblkp); 10132 } else { 10133 /* 10134 * Hey! we don't need hblk_reserve any more. 10135 */ 10136 ASSERT(owner); 10137 hblk_reserve_thread = NULL; 10138 mutex_exit(&hblk_reserve_lock); 10139 owner = 0; 10140 } 10141 re_verify: 10142 /* 10143 * let's check if the goodies are still present 10144 */ 10145 SFMMU_HASH_LOCK(hmebp); 10146 HME_HASH_FAST_SEARCH(hmebp, hblktag, newhblkp); 10147 if (newhblkp != NULL) { 10148 /* 10149 * return newhblkp if it's not hblk_reserve; 10150 * if newhblkp is hblk_reserve, return it 10151 * _only if_ we are the owner of hblk_reserve. 10152 */ 10153 if (newhblkp != HBLK_RESERVE || owner) { 10154 return (newhblkp); 10155 } else { 10156 /* 10157 * we just hit hblk_reserve in the hash and 10158 * we are not the owner of that; 10159 * 10160 * block until hblk_reserve_thread completes 10161 * swapping hblk_reserve and try the dance 10162 * once again. 10163 */ 10164 SFMMU_HASH_UNLOCK(hmebp); 10165 mutex_enter(&hblk_reserve_lock); 10166 mutex_exit(&hblk_reserve_lock); 10167 SFMMU_STAT(sf_hblk_reserve_hit); 10168 goto fill_hblk; 10169 } 10170 } else { 10171 /* 10172 * it's no more! try the dance once again. 10173 */ 10174 SFMMU_HASH_UNLOCK(hmebp); 10175 goto fill_hblk; 10176 } 10177 } 10178 10179 hblk_init: 10180 set_hblk_sz(hmeblkp, size); 10181 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 10182 hmeblkp->hblk_next = (struct hme_blk *)NULL; 10183 hmeblkp->hblk_tag = hblktag; 10184 hmeblkp->hblk_shadow = shw_hblkp; 10185 hblkpa = hmeblkp->hblk_nextpa; 10186 hmeblkp->hblk_nextpa = 0; 10187 10188 ASSERT(get_hblk_ttesz(hmeblkp) == size); 10189 ASSERT(get_hblk_span(hmeblkp) == HMEBLK_SPAN(size)); 10190 ASSERT(hmeblkp->hblk_hmecnt == 0); 10191 ASSERT(hmeblkp->hblk_vcnt == 0); 10192 ASSERT(hmeblkp->hblk_lckcnt == 0); 10193 ASSERT(hblkpa == va_to_pa((caddr_t)hmeblkp)); 10194 sfmmu_hblk_hash_add(hmebp, hmeblkp, hblkpa); 10195 return (hmeblkp); 10196 } 10197 10198 /* 10199 * This function performs any cleanup required on the hme_blk 10200 * and returns it to the free list. 10201 */ 10202 /* ARGSUSED */ 10203 static void 10204 sfmmu_hblk_free(struct hmehash_bucket *hmebp, struct hme_blk *hmeblkp, 10205 uint64_t hblkpa, struct hme_blk **listp) 10206 { 10207 int shw_size, vshift; 10208 struct hme_blk *shw_hblkp; 10209 uint_t shw_mask, newshw_mask; 10210 uintptr_t vaddr; 10211 int size; 10212 uint_t critical; 10213 10214 ASSERT(hmeblkp); 10215 ASSERT(!hmeblkp->hblk_hmecnt); 10216 ASSERT(!hmeblkp->hblk_vcnt); 10217 ASSERT(!hmeblkp->hblk_lckcnt); 10218 ASSERT(hblkpa == va_to_pa((caddr_t)hmeblkp)); 10219 ASSERT(hmeblkp != (struct hme_blk *)hblk_reserve); 10220 10221 critical = (hblktosfmmu(hmeblkp) == KHATID) ? 1 : 0; 10222 10223 size = get_hblk_ttesz(hmeblkp); 10224 shw_hblkp = hmeblkp->hblk_shadow; 10225 if (shw_hblkp) { 10226 ASSERT(hblktosfmmu(hmeblkp) != KHATID); 10227 if (mmu_page_sizes == max_mmu_page_sizes) { 10228 ASSERT(size < TTE256M); 10229 } else { 10230 ASSERT(size < TTE4M); 10231 } 10232 10233 shw_size = get_hblk_ttesz(shw_hblkp); 10234 vaddr = get_hblk_base(hmeblkp); 10235 vshift = vaddr_to_vshift(shw_hblkp->hblk_tag, vaddr, shw_size); 10236 ASSERT(vshift < 8); 10237 /* 10238 * Atomically clear shadow mask bit 10239 */ 10240 do { 10241 shw_mask = shw_hblkp->hblk_shw_mask; 10242 ASSERT(shw_mask & (1 << vshift)); 10243 newshw_mask = shw_mask & ~(1 << vshift); 10244 newshw_mask = cas32(&shw_hblkp->hblk_shw_mask, 10245 shw_mask, newshw_mask); 10246 } while (newshw_mask != shw_mask); 10247 hmeblkp->hblk_shadow = NULL; 10248 } 10249 hmeblkp->hblk_next = NULL; 10250 hmeblkp->hblk_nextpa = hblkpa; 10251 hmeblkp->hblk_shw_bit = 0; 10252 10253 if (hmeblkp->hblk_nuc_bit == 0) { 10254 10255 if (size == TTE8K && sfmmu_put_free_hblk(hmeblkp, critical)) 10256 return; 10257 10258 hmeblkp->hblk_next = *listp; 10259 *listp = hmeblkp; 10260 } 10261 } 10262 10263 static void 10264 sfmmu_hblks_list_purge(struct hme_blk **listp) 10265 { 10266 struct hme_blk *hmeblkp; 10267 10268 while ((hmeblkp = *listp) != NULL) { 10269 *listp = hmeblkp->hblk_next; 10270 kmem_cache_free(get_hblk_cache(hmeblkp), hmeblkp); 10271 } 10272 } 10273 10274 #define BUCKETS_TO_SEARCH_BEFORE_UNLOAD 30 10275 10276 static uint_t sfmmu_hblk_steal_twice; 10277 static uint_t sfmmu_hblk_steal_count, sfmmu_hblk_steal_unload_count; 10278 10279 /* 10280 * Steal a hmeblk 10281 * Enough hmeblks were allocated at startup (nucleus hmeblks) and also 10282 * hmeblks were added dynamically. We should never ever not be able to 10283 * find one. Look for an unused/unlocked hmeblk in user hash table. 10284 */ 10285 static struct hme_blk * 10286 sfmmu_hblk_steal(int size) 10287 { 10288 static struct hmehash_bucket *uhmehash_steal_hand = NULL; 10289 struct hmehash_bucket *hmebp; 10290 struct hme_blk *hmeblkp = NULL, *pr_hblk; 10291 uint64_t hblkpa, prevpa; 10292 int i; 10293 10294 for (;;) { 10295 hmebp = (uhmehash_steal_hand == NULL) ? uhme_hash : 10296 uhmehash_steal_hand; 10297 ASSERT(hmebp >= uhme_hash && hmebp <= &uhme_hash[UHMEHASH_SZ]); 10298 10299 for (i = 0; hmeblkp == NULL && i <= UHMEHASH_SZ + 10300 BUCKETS_TO_SEARCH_BEFORE_UNLOAD; i++) { 10301 SFMMU_HASH_LOCK(hmebp); 10302 hmeblkp = hmebp->hmeblkp; 10303 hblkpa = hmebp->hmeh_nextpa; 10304 prevpa = 0; 10305 pr_hblk = NULL; 10306 while (hmeblkp) { 10307 /* 10308 * check if it is a hmeblk that is not locked 10309 * and not shared. skip shadow hmeblks with 10310 * shadow_mask set i.e valid count non zero. 10311 */ 10312 if ((get_hblk_ttesz(hmeblkp) == size) && 10313 (hmeblkp->hblk_shw_bit == 0 || 10314 hmeblkp->hblk_vcnt == 0) && 10315 (hmeblkp->hblk_lckcnt == 0)) { 10316 /* 10317 * there is a high probability that we 10318 * will find a free one. search some 10319 * buckets for a free hmeblk initially 10320 * before unloading a valid hmeblk. 10321 */ 10322 if ((hmeblkp->hblk_vcnt == 0 && 10323 hmeblkp->hblk_hmecnt == 0) || (i >= 10324 BUCKETS_TO_SEARCH_BEFORE_UNLOAD)) { 10325 if (sfmmu_steal_this_hblk(hmebp, 10326 hmeblkp, hblkpa, prevpa, 10327 pr_hblk)) { 10328 /* 10329 * Hblk is unloaded 10330 * successfully 10331 */ 10332 break; 10333 } 10334 } 10335 } 10336 pr_hblk = hmeblkp; 10337 prevpa = hblkpa; 10338 hblkpa = hmeblkp->hblk_nextpa; 10339 hmeblkp = hmeblkp->hblk_next; 10340 } 10341 10342 SFMMU_HASH_UNLOCK(hmebp); 10343 if (hmebp++ == &uhme_hash[UHMEHASH_SZ]) 10344 hmebp = uhme_hash; 10345 } 10346 uhmehash_steal_hand = hmebp; 10347 10348 if (hmeblkp != NULL) 10349 break; 10350 10351 /* 10352 * in the worst case, look for a free one in the kernel 10353 * hash table. 10354 */ 10355 for (i = 0, hmebp = khme_hash; i <= KHMEHASH_SZ; i++) { 10356 SFMMU_HASH_LOCK(hmebp); 10357 hmeblkp = hmebp->hmeblkp; 10358 hblkpa = hmebp->hmeh_nextpa; 10359 prevpa = 0; 10360 pr_hblk = NULL; 10361 while (hmeblkp) { 10362 /* 10363 * check if it is free hmeblk 10364 */ 10365 if ((get_hblk_ttesz(hmeblkp) == size) && 10366 (hmeblkp->hblk_lckcnt == 0) && 10367 (hmeblkp->hblk_vcnt == 0) && 10368 (hmeblkp->hblk_hmecnt == 0)) { 10369 if (sfmmu_steal_this_hblk(hmebp, 10370 hmeblkp, hblkpa, prevpa, pr_hblk)) { 10371 break; 10372 } else { 10373 /* 10374 * Cannot fail since we have 10375 * hash lock. 10376 */ 10377 panic("fail to steal?"); 10378 } 10379 } 10380 10381 pr_hblk = hmeblkp; 10382 prevpa = hblkpa; 10383 hblkpa = hmeblkp->hblk_nextpa; 10384 hmeblkp = hmeblkp->hblk_next; 10385 } 10386 10387 SFMMU_HASH_UNLOCK(hmebp); 10388 if (hmebp++ == &khme_hash[KHMEHASH_SZ]) 10389 hmebp = khme_hash; 10390 } 10391 10392 if (hmeblkp != NULL) 10393 break; 10394 sfmmu_hblk_steal_twice++; 10395 } 10396 return (hmeblkp); 10397 } 10398 10399 /* 10400 * This routine does real work to prepare a hblk to be "stolen" by 10401 * unloading the mappings, updating shadow counts .... 10402 * It returns 1 if the block is ready to be reused (stolen), or 0 10403 * means the block cannot be stolen yet- pageunload is still working 10404 * on this hblk. 10405 */ 10406 static int 10407 sfmmu_steal_this_hblk(struct hmehash_bucket *hmebp, struct hme_blk *hmeblkp, 10408 uint64_t hblkpa, uint64_t prevpa, struct hme_blk *pr_hblk) 10409 { 10410 int shw_size, vshift; 10411 struct hme_blk *shw_hblkp; 10412 uintptr_t vaddr; 10413 uint_t shw_mask, newshw_mask; 10414 10415 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 10416 10417 /* 10418 * check if the hmeblk is free, unload if necessary 10419 */ 10420 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) { 10421 sfmmu_t *sfmmup; 10422 demap_range_t dmr; 10423 10424 sfmmup = hblktosfmmu(hmeblkp); 10425 DEMAP_RANGE_INIT(sfmmup, &dmr); 10426 (void) sfmmu_hblk_unload(sfmmup, hmeblkp, 10427 (caddr_t)get_hblk_base(hmeblkp), 10428 get_hblk_endaddr(hmeblkp), &dmr, HAT_UNLOAD); 10429 DEMAP_RANGE_FLUSH(&dmr); 10430 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) { 10431 /* 10432 * Pageunload is working on the same hblk. 10433 */ 10434 return (0); 10435 } 10436 10437 sfmmu_hblk_steal_unload_count++; 10438 } 10439 10440 ASSERT(hmeblkp->hblk_lckcnt == 0); 10441 ASSERT(hmeblkp->hblk_vcnt == 0 && hmeblkp->hblk_hmecnt == 0); 10442 10443 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, pr_hblk); 10444 hmeblkp->hblk_nextpa = hblkpa; 10445 10446 shw_hblkp = hmeblkp->hblk_shadow; 10447 if (shw_hblkp) { 10448 shw_size = get_hblk_ttesz(shw_hblkp); 10449 vaddr = get_hblk_base(hmeblkp); 10450 vshift = vaddr_to_vshift(shw_hblkp->hblk_tag, vaddr, shw_size); 10451 ASSERT(vshift < 8); 10452 /* 10453 * Atomically clear shadow mask bit 10454 */ 10455 do { 10456 shw_mask = shw_hblkp->hblk_shw_mask; 10457 ASSERT(shw_mask & (1 << vshift)); 10458 newshw_mask = shw_mask & ~(1 << vshift); 10459 newshw_mask = cas32(&shw_hblkp->hblk_shw_mask, 10460 shw_mask, newshw_mask); 10461 } while (newshw_mask != shw_mask); 10462 hmeblkp->hblk_shadow = NULL; 10463 } 10464 10465 /* 10466 * remove shadow bit if we are stealing an unused shadow hmeblk. 10467 * sfmmu_hblk_alloc needs it that way, will set shadow bit later if 10468 * we are indeed allocating a shadow hmeblk. 10469 */ 10470 hmeblkp->hblk_shw_bit = 0; 10471 10472 sfmmu_hblk_steal_count++; 10473 SFMMU_STAT(sf_steal_count); 10474 10475 return (1); 10476 } 10477 10478 struct hme_blk * 10479 sfmmu_hmetohblk(struct sf_hment *sfhme) 10480 { 10481 struct hme_blk *hmeblkp; 10482 struct sf_hment *sfhme0; 10483 struct hme_blk *hblk_dummy = 0; 10484 10485 /* 10486 * No dummy sf_hments, please. 10487 */ 10488 ASSERT(sfhme->hme_tte.ll != 0); 10489 10490 sfhme0 = sfhme - sfhme->hme_tte.tte_hmenum; 10491 hmeblkp = (struct hme_blk *)((uintptr_t)sfhme0 - 10492 (uintptr_t)&hblk_dummy->hblk_hme[0]); 10493 10494 return (hmeblkp); 10495 } 10496 10497 /* 10498 * On swapin, get appropriately sized TSB(s) and clear the HAT_SWAPPED flag. 10499 * If we can't get appropriately sized TSB(s), try for 8K TSB(s) using 10500 * KM_SLEEP allocation. 10501 * 10502 * Return 0 on success, -1 otherwise. 10503 */ 10504 static void 10505 sfmmu_tsb_swapin(sfmmu_t *sfmmup, hatlock_t *hatlockp) 10506 { 10507 struct tsb_info *tsbinfop, *next; 10508 tsb_replace_rc_t rc; 10509 boolean_t gotfirst = B_FALSE; 10510 10511 ASSERT(sfmmup != ksfmmup); 10512 ASSERT(sfmmu_hat_lock_held(sfmmup)); 10513 10514 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPIN)) { 10515 cv_wait(&sfmmup->sfmmu_tsb_cv, HATLOCK_MUTEXP(hatlockp)); 10516 } 10517 10518 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) { 10519 SFMMU_FLAGS_SET(sfmmup, HAT_SWAPIN); 10520 } else { 10521 return; 10522 } 10523 10524 ASSERT(sfmmup->sfmmu_tsb != NULL); 10525 10526 /* 10527 * Loop over all tsbinfo's replacing them with ones that actually have 10528 * a TSB. If any of the replacements ever fail, bail out of the loop. 10529 */ 10530 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL; tsbinfop = next) { 10531 ASSERT(tsbinfop->tsb_flags & TSB_SWAPPED); 10532 next = tsbinfop->tsb_next; 10533 rc = sfmmu_replace_tsb(sfmmup, tsbinfop, tsbinfop->tsb_szc, 10534 hatlockp, TSB_SWAPIN); 10535 if (rc != TSB_SUCCESS) { 10536 break; 10537 } 10538 gotfirst = B_TRUE; 10539 } 10540 10541 switch (rc) { 10542 case TSB_SUCCESS: 10543 SFMMU_FLAGS_CLEAR(sfmmup, HAT_SWAPPED|HAT_SWAPIN); 10544 cv_broadcast(&sfmmup->sfmmu_tsb_cv); 10545 return; 10546 case TSB_ALLOCFAIL: 10547 break; 10548 default: 10549 panic("sfmmu_replace_tsb returned unrecognized failure code " 10550 "%d", rc); 10551 } 10552 10553 /* 10554 * In this case, we failed to get one of our TSBs. If we failed to 10555 * get the first TSB, get one of minimum size (8KB). Walk the list 10556 * and throw away the tsbinfos, starting where the allocation failed; 10557 * we can get by with just one TSB as long as we don't leave the 10558 * SWAPPED tsbinfo structures lying around. 10559 */ 10560 tsbinfop = sfmmup->sfmmu_tsb; 10561 next = tsbinfop->tsb_next; 10562 tsbinfop->tsb_next = NULL; 10563 10564 sfmmu_hat_exit(hatlockp); 10565 for (tsbinfop = next; tsbinfop != NULL; tsbinfop = next) { 10566 next = tsbinfop->tsb_next; 10567 sfmmu_tsbinfo_free(tsbinfop); 10568 } 10569 hatlockp = sfmmu_hat_enter(sfmmup); 10570 10571 /* 10572 * If we don't have any TSBs, get a single 8K TSB for 8K, 64K and 512K 10573 * pages. 10574 */ 10575 if (!gotfirst) { 10576 tsbinfop = sfmmup->sfmmu_tsb; 10577 rc = sfmmu_replace_tsb(sfmmup, tsbinfop, TSB_MIN_SZCODE, 10578 hatlockp, TSB_SWAPIN | TSB_FORCEALLOC); 10579 ASSERT(rc == TSB_SUCCESS); 10580 } 10581 10582 SFMMU_FLAGS_CLEAR(sfmmup, HAT_SWAPPED|HAT_SWAPIN); 10583 cv_broadcast(&sfmmup->sfmmu_tsb_cv); 10584 } 10585 10586 /* 10587 * Handle exceptions for low level tsb_handler. 10588 * 10589 * There are many scenarios that could land us here: 10590 * 10591 * If the context is invalid we land here. The context can be invalid 10592 * for 3 reasons: 1) we couldn't allocate a new context and now need to 10593 * perform a wrap around operation in order to allocate a new context. 10594 * 2) Context was invalidated to change pagesize programming 3) ISMs or 10595 * TSBs configuration is changeing for this process and we are forced into 10596 * here to do a syncronization operation. If the context is valid we can 10597 * be here from window trap hanlder. In this case just call trap to handle 10598 * the fault. 10599 * 10600 * Note that the process will run in INVALID_CONTEXT before 10601 * faulting into here and subsequently loading the MMU registers 10602 * (including the TSB base register) associated with this process. 10603 * For this reason, the trap handlers must all test for 10604 * INVALID_CONTEXT before attempting to access any registers other 10605 * than the context registers. 10606 */ 10607 void 10608 sfmmu_tsbmiss_exception(struct regs *rp, uintptr_t tagaccess, uint_t traptype) 10609 { 10610 sfmmu_t *sfmmup; 10611 uint_t ctxnum; 10612 klwp_id_t lwp; 10613 char lwp_save_state; 10614 hatlock_t *hatlockp; 10615 struct tsb_info *tsbinfop; 10616 10617 SFMMU_STAT(sf_tsb_exceptions); 10618 SFMMU_MMU_STAT(mmu_tsb_exceptions); 10619 sfmmup = astosfmmu(curthread->t_procp->p_as); 10620 ctxnum = tagaccess & TAGACC_CTX_MASK; 10621 10622 ASSERT(sfmmup != ksfmmup && ctxnum != KCONTEXT); 10623 ASSERT(sfmmup->sfmmu_ismhat == 0); 10624 /* 10625 * First, make sure we come out of here with a valid ctx, 10626 * since if we don't get one we'll simply loop on the 10627 * faulting instruction. 10628 * 10629 * If the ISM mappings are changing, the TSB is being relocated, or 10630 * the process is swapped out we serialize behind the controlling 10631 * thread with the sfmmu_flags and sfmmu_tsb_cv condition variable. 10632 * Otherwise we synchronize with the context stealer or the thread 10633 * that required us to change out our MMU registers (such 10634 * as a thread changing out our TSB while we were running) by 10635 * locking the HAT and grabbing the rwlock on the context as a 10636 * reader temporarily. 10637 */ 10638 ASSERT(!SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED) || 10639 ctxnum == INVALID_CONTEXT); 10640 10641 if (ctxnum == INVALID_CONTEXT) { 10642 /* 10643 * Must set lwp state to LWP_SYS before 10644 * trying to acquire any adaptive lock 10645 */ 10646 lwp = ttolwp(curthread); 10647 ASSERT(lwp); 10648 lwp_save_state = lwp->lwp_state; 10649 lwp->lwp_state = LWP_SYS; 10650 10651 hatlockp = sfmmu_hat_enter(sfmmup); 10652 retry: 10653 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL; 10654 tsbinfop = tsbinfop->tsb_next) { 10655 if (tsbinfop->tsb_flags & TSB_RELOC_FLAG) { 10656 cv_wait(&sfmmup->sfmmu_tsb_cv, 10657 HATLOCK_MUTEXP(hatlockp)); 10658 goto retry; 10659 } 10660 } 10661 10662 /* 10663 * Wait for ISM maps to be updated. 10664 */ 10665 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)) { 10666 cv_wait(&sfmmup->sfmmu_tsb_cv, 10667 HATLOCK_MUTEXP(hatlockp)); 10668 goto retry; 10669 } 10670 10671 /* 10672 * If we're swapping in, get TSB(s). Note that we must do 10673 * this before we get a ctx or load the MMU state. Once 10674 * we swap in we have to recheck to make sure the TSB(s) and 10675 * ISM mappings didn't change while we slept. 10676 */ 10677 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) { 10678 sfmmu_tsb_swapin(sfmmup, hatlockp); 10679 goto retry; 10680 } 10681 10682 sfmmu_get_ctx(sfmmup); 10683 10684 sfmmu_hat_exit(hatlockp); 10685 /* 10686 * Must restore lwp_state if not calling 10687 * trap() for further processing. Restore 10688 * it anyway. 10689 */ 10690 lwp->lwp_state = lwp_save_state; 10691 if (sfmmup->sfmmu_ttecnt[TTE8K] != 0 || 10692 sfmmup->sfmmu_ttecnt[TTE64K] != 0 || 10693 sfmmup->sfmmu_ttecnt[TTE512K] != 0 || 10694 sfmmup->sfmmu_ttecnt[TTE4M] != 0 || 10695 sfmmup->sfmmu_ttecnt[TTE32M] != 0 || 10696 sfmmup->sfmmu_ttecnt[TTE256M] != 0) { 10697 return; 10698 } 10699 if (traptype == T_DATA_PROT) { 10700 traptype = T_DATA_MMU_MISS; 10701 } 10702 } 10703 trap(rp, (caddr_t)tagaccess, traptype, 0); 10704 } 10705 10706 /* 10707 * sfmmu_vatopfn_suspended is called from GET_TTE when TL=0 and 10708 * TTE_SUSPENDED bit set in tte we block on aquiring a page lock 10709 * rather than spinning to avoid send mondo timeouts with 10710 * interrupts enabled. When the lock is acquired it is immediately 10711 * released and we return back to sfmmu_vatopfn just after 10712 * the GET_TTE call. 10713 */ 10714 void 10715 sfmmu_vatopfn_suspended(caddr_t vaddr, sfmmu_t *sfmmu, tte_t *ttep) 10716 { 10717 struct page **pp; 10718 10719 (void) as_pagelock(sfmmu->sfmmu_as, &pp, vaddr, TTE_CSZ(ttep), S_WRITE); 10720 as_pageunlock(sfmmu->sfmmu_as, pp, vaddr, TTE_CSZ(ttep), S_WRITE); 10721 } 10722 10723 /* 10724 * sfmmu_tsbmiss_suspended is called from GET_TTE when TL>0 and 10725 * TTE_SUSPENDED bit set in tte. We do this so that we can handle 10726 * cross traps which cannot be handled while spinning in the 10727 * trap handlers. Simply enter and exit the kpr_suspendlock spin 10728 * mutex, which is held by the holder of the suspend bit, and then 10729 * retry the trapped instruction after unwinding. 10730 */ 10731 /*ARGSUSED*/ 10732 void 10733 sfmmu_tsbmiss_suspended(struct regs *rp, uintptr_t tagacc, uint_t traptype) 10734 { 10735 ASSERT(curthread != kreloc_thread); 10736 mutex_enter(&kpr_suspendlock); 10737 mutex_exit(&kpr_suspendlock); 10738 } 10739 10740 /* 10741 * Special routine to flush out ism mappings- TSBs, TLBs and D-caches. 10742 * This routine may be called with all cpu's captured. Therefore, the 10743 * caller is responsible for holding all locks and disabling kernel 10744 * preemption. 10745 */ 10746 /* ARGSUSED */ 10747 static void 10748 sfmmu_ismtlbcache_demap(caddr_t addr, sfmmu_t *ism_sfmmup, 10749 struct hme_blk *hmeblkp, pfn_t pfnum, int cache_flush_flag) 10750 { 10751 cpuset_t cpuset; 10752 caddr_t va; 10753 ism_ment_t *ment; 10754 sfmmu_t *sfmmup; 10755 #ifdef VAC 10756 int vcolor; 10757 #endif 10758 int ttesz; 10759 10760 /* 10761 * Walk the ism_hat's mapping list and flush the page 10762 * from every hat sharing this ism_hat. This routine 10763 * may be called while all cpu's have been captured. 10764 * Therefore we can't attempt to grab any locks. For now 10765 * this means we will protect the ism mapping list under 10766 * a single lock which will be grabbed by the caller. 10767 * If hat_share/unshare scalibility becomes a performance 10768 * problem then we may need to re-think ism mapping list locking. 10769 */ 10770 ASSERT(ism_sfmmup->sfmmu_ismhat); 10771 ASSERT(MUTEX_HELD(&ism_mlist_lock)); 10772 addr = addr - ISMID_STARTADDR; 10773 for (ment = ism_sfmmup->sfmmu_iment; ment; ment = ment->iment_next) { 10774 10775 sfmmup = ment->iment_hat; 10776 10777 va = ment->iment_base_va; 10778 va = (caddr_t)((uintptr_t)va + (uintptr_t)addr); 10779 10780 /* 10781 * Flush TSB of ISM mappings. 10782 */ 10783 ttesz = get_hblk_ttesz(hmeblkp); 10784 if (ttesz == TTE8K || ttesz == TTE4M) { 10785 sfmmu_unload_tsb(sfmmup, va, ttesz); 10786 } else { 10787 caddr_t sva = va; 10788 caddr_t eva; 10789 ASSERT(addr == (caddr_t)get_hblk_base(hmeblkp)); 10790 eva = sva + get_hblk_span(hmeblkp); 10791 sfmmu_unload_tsb_range(sfmmup, sva, eva, ttesz); 10792 } 10793 10794 cpuset = sfmmup->sfmmu_cpusran; 10795 CPUSET_AND(cpuset, cpu_ready_set); 10796 CPUSET_DEL(cpuset, CPU->cpu_id); 10797 10798 SFMMU_XCALL_STATS(sfmmup); 10799 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)va, 10800 (uint64_t)sfmmup); 10801 10802 vtag_flushpage(va, (uint64_t)sfmmup); 10803 10804 #ifdef VAC 10805 /* 10806 * Flush D$ 10807 * When flushing D$ we must flush all 10808 * cpu's. See sfmmu_cache_flush(). 10809 */ 10810 if (cache_flush_flag == CACHE_FLUSH) { 10811 cpuset = cpu_ready_set; 10812 CPUSET_DEL(cpuset, CPU->cpu_id); 10813 10814 SFMMU_XCALL_STATS(sfmmup); 10815 vcolor = addr_to_vcolor(va); 10816 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor); 10817 vac_flushpage(pfnum, vcolor); 10818 } 10819 #endif /* VAC */ 10820 } 10821 } 10822 10823 /* 10824 * Demaps the TSB, CPU caches, and flushes all TLBs on all CPUs of 10825 * a particular virtual address and ctx. If noflush is set we do not 10826 * flush the TLB/TSB. This function may or may not be called with the 10827 * HAT lock held. 10828 */ 10829 static void 10830 sfmmu_tlbcache_demap(caddr_t addr, sfmmu_t *sfmmup, struct hme_blk *hmeblkp, 10831 pfn_t pfnum, int tlb_noflush, int cpu_flag, int cache_flush_flag, 10832 int hat_lock_held) 10833 { 10834 #ifdef VAC 10835 int vcolor; 10836 #endif 10837 cpuset_t cpuset; 10838 hatlock_t *hatlockp; 10839 10840 #if defined(lint) && !defined(VAC) 10841 pfnum = pfnum; 10842 cpu_flag = cpu_flag; 10843 cache_flush_flag = cache_flush_flag; 10844 #endif 10845 /* 10846 * There is no longer a need to protect against ctx being 10847 * stolen here since we don't store the ctx in the TSB anymore. 10848 */ 10849 #ifdef VAC 10850 vcolor = addr_to_vcolor(addr); 10851 #endif 10852 10853 /* 10854 * We must hold the hat lock during the flush of TLB, 10855 * to avoid a race with sfmmu_invalidate_ctx(), where 10856 * sfmmu_cnum on a MMU could be set to INVALID_CONTEXT, 10857 * causing TLB demap routine to skip flush on that MMU. 10858 * If the context on a MMU has already been set to 10859 * INVALID_CONTEXT, we just get an extra flush on 10860 * that MMU. 10861 */ 10862 if (!hat_lock_held && !tlb_noflush) 10863 hatlockp = sfmmu_hat_enter(sfmmup); 10864 10865 kpreempt_disable(); 10866 if (!tlb_noflush) { 10867 /* 10868 * Flush the TSB and TLB. 10869 */ 10870 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp); 10871 10872 cpuset = sfmmup->sfmmu_cpusran; 10873 CPUSET_AND(cpuset, cpu_ready_set); 10874 CPUSET_DEL(cpuset, CPU->cpu_id); 10875 10876 SFMMU_XCALL_STATS(sfmmup); 10877 10878 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr, 10879 (uint64_t)sfmmup); 10880 10881 vtag_flushpage(addr, (uint64_t)sfmmup); 10882 } 10883 10884 if (!hat_lock_held && !tlb_noflush) 10885 sfmmu_hat_exit(hatlockp); 10886 10887 #ifdef VAC 10888 /* 10889 * Flush the D$ 10890 * 10891 * Even if the ctx is stolen, we need to flush the 10892 * cache. Our ctx stealer only flushes the TLBs. 10893 */ 10894 if (cache_flush_flag == CACHE_FLUSH) { 10895 if (cpu_flag & FLUSH_ALL_CPUS) { 10896 cpuset = cpu_ready_set; 10897 } else { 10898 cpuset = sfmmup->sfmmu_cpusran; 10899 CPUSET_AND(cpuset, cpu_ready_set); 10900 } 10901 CPUSET_DEL(cpuset, CPU->cpu_id); 10902 SFMMU_XCALL_STATS(sfmmup); 10903 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor); 10904 vac_flushpage(pfnum, vcolor); 10905 } 10906 #endif /* VAC */ 10907 kpreempt_enable(); 10908 } 10909 10910 /* 10911 * Demaps the TSB and flushes all TLBs on all cpus for a particular virtual 10912 * address and ctx. If noflush is set we do not currently do anything. 10913 * This function may or may not be called with the HAT lock held. 10914 */ 10915 static void 10916 sfmmu_tlb_demap(caddr_t addr, sfmmu_t *sfmmup, struct hme_blk *hmeblkp, 10917 int tlb_noflush, int hat_lock_held) 10918 { 10919 cpuset_t cpuset; 10920 hatlock_t *hatlockp; 10921 10922 /* 10923 * If the process is exiting we have nothing to do. 10924 */ 10925 if (tlb_noflush) 10926 return; 10927 10928 /* 10929 * Flush TSB. 10930 */ 10931 if (!hat_lock_held) 10932 hatlockp = sfmmu_hat_enter(sfmmup); 10933 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp); 10934 10935 kpreempt_disable(); 10936 10937 cpuset = sfmmup->sfmmu_cpusran; 10938 CPUSET_AND(cpuset, cpu_ready_set); 10939 CPUSET_DEL(cpuset, CPU->cpu_id); 10940 10941 SFMMU_XCALL_STATS(sfmmup); 10942 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr, (uint64_t)sfmmup); 10943 10944 vtag_flushpage(addr, (uint64_t)sfmmup); 10945 10946 if (!hat_lock_held) 10947 sfmmu_hat_exit(hatlockp); 10948 10949 kpreempt_enable(); 10950 10951 } 10952 10953 /* 10954 * Special case of sfmmu_tlb_demap for MMU_PAGESIZE hblks. Use the xcall 10955 * call handler that can flush a range of pages to save on xcalls. 10956 */ 10957 static int sfmmu_xcall_save; 10958 10959 static void 10960 sfmmu_tlb_range_demap(demap_range_t *dmrp) 10961 { 10962 sfmmu_t *sfmmup = dmrp->dmr_sfmmup; 10963 hatlock_t *hatlockp; 10964 cpuset_t cpuset; 10965 uint64_t sfmmu_pgcnt; 10966 pgcnt_t pgcnt = 0; 10967 int pgunload = 0; 10968 int dirtypg = 0; 10969 caddr_t addr = dmrp->dmr_addr; 10970 caddr_t eaddr; 10971 uint64_t bitvec = dmrp->dmr_bitvec; 10972 10973 ASSERT(bitvec & 1); 10974 10975 /* 10976 * Flush TSB and calculate number of pages to flush. 10977 */ 10978 while (bitvec != 0) { 10979 dirtypg = 0; 10980 /* 10981 * Find the first page to flush and then count how many 10982 * pages there are after it that also need to be flushed. 10983 * This way the number of TSB flushes is minimized. 10984 */ 10985 while ((bitvec & 1) == 0) { 10986 pgcnt++; 10987 addr += MMU_PAGESIZE; 10988 bitvec >>= 1; 10989 } 10990 while (bitvec & 1) { 10991 dirtypg++; 10992 bitvec >>= 1; 10993 } 10994 eaddr = addr + ptob(dirtypg); 10995 hatlockp = sfmmu_hat_enter(sfmmup); 10996 sfmmu_unload_tsb_range(sfmmup, addr, eaddr, TTE8K); 10997 sfmmu_hat_exit(hatlockp); 10998 pgunload += dirtypg; 10999 addr = eaddr; 11000 pgcnt += dirtypg; 11001 } 11002 11003 ASSERT((pgcnt<<MMU_PAGESHIFT) <= dmrp->dmr_endaddr - dmrp->dmr_addr); 11004 if (sfmmup->sfmmu_free == 0) { 11005 addr = dmrp->dmr_addr; 11006 bitvec = dmrp->dmr_bitvec; 11007 11008 /* 11009 * make sure it has SFMMU_PGCNT_SHIFT bits only, 11010 * as it will be used to pack argument for xt_some 11011 */ 11012 ASSERT((pgcnt > 0) && 11013 (pgcnt <= (1 << SFMMU_PGCNT_SHIFT))); 11014 11015 /* 11016 * Encode pgcnt as (pgcnt -1 ), and pass (pgcnt - 1) in 11017 * the low 6 bits of sfmmup. This is doable since pgcnt 11018 * always >= 1. 11019 */ 11020 ASSERT(!((uint64_t)sfmmup & SFMMU_PGCNT_MASK)); 11021 sfmmu_pgcnt = (uint64_t)sfmmup | 11022 ((pgcnt - 1) & SFMMU_PGCNT_MASK); 11023 11024 /* 11025 * We must hold the hat lock during the flush of TLB, 11026 * to avoid a race with sfmmu_invalidate_ctx(), where 11027 * sfmmu_cnum on a MMU could be set to INVALID_CONTEXT, 11028 * causing TLB demap routine to skip flush on that MMU. 11029 * If the context on a MMU has already been set to 11030 * INVALID_CONTEXT, we just get an extra flush on 11031 * that MMU. 11032 */ 11033 hatlockp = sfmmu_hat_enter(sfmmup); 11034 kpreempt_disable(); 11035 11036 cpuset = sfmmup->sfmmu_cpusran; 11037 CPUSET_AND(cpuset, cpu_ready_set); 11038 CPUSET_DEL(cpuset, CPU->cpu_id); 11039 11040 SFMMU_XCALL_STATS(sfmmup); 11041 xt_some(cpuset, vtag_flush_pgcnt_tl1, (uint64_t)addr, 11042 sfmmu_pgcnt); 11043 11044 for (; bitvec != 0; bitvec >>= 1) { 11045 if (bitvec & 1) 11046 vtag_flushpage(addr, (uint64_t)sfmmup); 11047 addr += MMU_PAGESIZE; 11048 } 11049 kpreempt_enable(); 11050 sfmmu_hat_exit(hatlockp); 11051 11052 sfmmu_xcall_save += (pgunload-1); 11053 } 11054 dmrp->dmr_bitvec = 0; 11055 } 11056 11057 /* 11058 * In cases where we need to synchronize with TLB/TSB miss trap 11059 * handlers, _and_ need to flush the TLB, it's a lot easier to 11060 * throw away the context from the process than to do a 11061 * special song and dance to keep things consistent for the 11062 * handlers. 11063 * 11064 * Since the process suddenly ends up without a context and our caller 11065 * holds the hat lock, threads that fault after this function is called 11066 * will pile up on the lock. We can then do whatever we need to 11067 * atomically from the context of the caller. The first blocked thread 11068 * to resume executing will get the process a new context, and the 11069 * process will resume executing. 11070 * 11071 * One added advantage of this approach is that on MMUs that 11072 * support a "flush all" operation, we will delay the flush until 11073 * cnum wrap-around, and then flush the TLB one time. This 11074 * is rather rare, so it's a lot less expensive than making 8000 11075 * x-calls to flush the TLB 8000 times. 11076 * 11077 * A per-process (PP) lock is used to synchronize ctx allocations in 11078 * resume() and ctx invalidations here. 11079 */ 11080 static void 11081 sfmmu_invalidate_ctx(sfmmu_t *sfmmup) 11082 { 11083 cpuset_t cpuset; 11084 int cnum, currcnum; 11085 mmu_ctx_t *mmu_ctxp; 11086 int i; 11087 uint_t pstate_save; 11088 11089 SFMMU_STAT(sf_ctx_inv); 11090 11091 ASSERT(sfmmu_hat_lock_held(sfmmup)); 11092 ASSERT(sfmmup != ksfmmup); 11093 11094 kpreempt_disable(); 11095 11096 mmu_ctxp = CPU_MMU_CTXP(CPU); 11097 ASSERT(mmu_ctxp); 11098 ASSERT(mmu_ctxp->mmu_idx < max_mmu_ctxdoms); 11099 ASSERT(mmu_ctxp == mmu_ctxs_tbl[mmu_ctxp->mmu_idx]); 11100 11101 currcnum = sfmmup->sfmmu_ctxs[mmu_ctxp->mmu_idx].cnum; 11102 11103 pstate_save = sfmmu_disable_intrs(); 11104 11105 lock_set(&sfmmup->sfmmu_ctx_lock); /* acquire PP lock */ 11106 /* set HAT cnum invalid across all context domains. */ 11107 for (i = 0; i < max_mmu_ctxdoms; i++) { 11108 11109 cnum = sfmmup->sfmmu_ctxs[i].cnum; 11110 if (cnum == INVALID_CONTEXT) { 11111 continue; 11112 } 11113 11114 sfmmup->sfmmu_ctxs[i].cnum = INVALID_CONTEXT; 11115 } 11116 membar_enter(); /* make sure globally visible to all CPUs */ 11117 lock_clear(&sfmmup->sfmmu_ctx_lock); /* release PP lock */ 11118 11119 sfmmu_enable_intrs(pstate_save); 11120 11121 cpuset = sfmmup->sfmmu_cpusran; 11122 CPUSET_DEL(cpuset, CPU->cpu_id); 11123 CPUSET_AND(cpuset, cpu_ready_set); 11124 if (!CPUSET_ISNULL(cpuset)) { 11125 SFMMU_XCALL_STATS(sfmmup); 11126 xt_some(cpuset, sfmmu_raise_tsb_exception, 11127 (uint64_t)sfmmup, INVALID_CONTEXT); 11128 xt_sync(cpuset); 11129 SFMMU_STAT(sf_tsb_raise_exception); 11130 SFMMU_MMU_STAT(mmu_tsb_raise_exception); 11131 } 11132 11133 /* 11134 * If the hat to-be-invalidated is the same as the current 11135 * process on local CPU we need to invalidate 11136 * this CPU context as well. 11137 */ 11138 if ((sfmmu_getctx_sec() == currcnum) && 11139 (currcnum != INVALID_CONTEXT)) { 11140 sfmmu_setctx_sec(INVALID_CONTEXT); 11141 sfmmu_clear_utsbinfo(); 11142 } 11143 11144 kpreempt_enable(); 11145 11146 /* 11147 * we hold the hat lock, so nobody should allocate a context 11148 * for us yet 11149 */ 11150 ASSERT(sfmmup->sfmmu_ctxs[mmu_ctxp->mmu_idx].cnum == INVALID_CONTEXT); 11151 } 11152 11153 #ifdef VAC 11154 /* 11155 * We need to flush the cache in all cpus. It is possible that 11156 * a process referenced a page as cacheable but has sinced exited 11157 * and cleared the mapping list. We still to flush it but have no 11158 * state so all cpus is the only alternative. 11159 */ 11160 void 11161 sfmmu_cache_flush(pfn_t pfnum, int vcolor) 11162 { 11163 cpuset_t cpuset; 11164 11165 kpreempt_disable(); 11166 cpuset = cpu_ready_set; 11167 CPUSET_DEL(cpuset, CPU->cpu_id); 11168 SFMMU_XCALL_STATS(NULL); /* account to any ctx */ 11169 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor); 11170 xt_sync(cpuset); 11171 vac_flushpage(pfnum, vcolor); 11172 kpreempt_enable(); 11173 } 11174 11175 void 11176 sfmmu_cache_flushcolor(int vcolor, pfn_t pfnum) 11177 { 11178 cpuset_t cpuset; 11179 11180 ASSERT(vcolor >= 0); 11181 11182 kpreempt_disable(); 11183 cpuset = cpu_ready_set; 11184 CPUSET_DEL(cpuset, CPU->cpu_id); 11185 SFMMU_XCALL_STATS(NULL); /* account to any ctx */ 11186 xt_some(cpuset, vac_flushcolor_tl1, vcolor, pfnum); 11187 xt_sync(cpuset); 11188 vac_flushcolor(vcolor, pfnum); 11189 kpreempt_enable(); 11190 } 11191 #endif /* VAC */ 11192 11193 /* 11194 * We need to prevent processes from accessing the TSB using a cached physical 11195 * address. It's alright if they try to access the TSB via virtual address 11196 * since they will just fault on that virtual address once the mapping has 11197 * been suspended. 11198 */ 11199 #pragma weak sendmondo_in_recover 11200 11201 /* ARGSUSED */ 11202 static int 11203 sfmmu_tsb_pre_relocator(caddr_t va, uint_t tsbsz, uint_t flags, void *tsbinfo) 11204 { 11205 hatlock_t *hatlockp; 11206 struct tsb_info *tsbinfop = (struct tsb_info *)tsbinfo; 11207 sfmmu_t *sfmmup = tsbinfop->tsb_sfmmu; 11208 extern uint32_t sendmondo_in_recover; 11209 11210 if (flags != HAT_PRESUSPEND) 11211 return (0); 11212 11213 hatlockp = sfmmu_hat_enter(sfmmup); 11214 11215 tsbinfop->tsb_flags |= TSB_RELOC_FLAG; 11216 11217 /* 11218 * For Cheetah+ Erratum 25: 11219 * Wait for any active recovery to finish. We can't risk 11220 * relocating the TSB of the thread running mondo_recover_proc() 11221 * since, if we did that, we would deadlock. The scenario we are 11222 * trying to avoid is as follows: 11223 * 11224 * THIS CPU RECOVER CPU 11225 * -------- ----------- 11226 * Begins recovery, walking through TSB 11227 * hat_pagesuspend() TSB TTE 11228 * TLB miss on TSB TTE, spins at TL1 11229 * xt_sync() 11230 * send_mondo_timeout() 11231 * mondo_recover_proc() 11232 * ((deadlocked)) 11233 * 11234 * The second half of the workaround is that mondo_recover_proc() 11235 * checks to see if the tsb_info has the RELOC flag set, and if it 11236 * does, it skips over that TSB without ever touching tsbinfop->tsb_va 11237 * and hence avoiding the TLB miss that could result in a deadlock. 11238 */ 11239 if (&sendmondo_in_recover) { 11240 membar_enter(); /* make sure RELOC flag visible */ 11241 while (sendmondo_in_recover) { 11242 drv_usecwait(1); 11243 membar_consumer(); 11244 } 11245 } 11246 11247 sfmmu_invalidate_ctx(sfmmup); 11248 sfmmu_hat_exit(hatlockp); 11249 11250 return (0); 11251 } 11252 11253 /* ARGSUSED */ 11254 static int 11255 sfmmu_tsb_post_relocator(caddr_t va, uint_t tsbsz, uint_t flags, 11256 void *tsbinfo, pfn_t newpfn) 11257 { 11258 hatlock_t *hatlockp; 11259 struct tsb_info *tsbinfop = (struct tsb_info *)tsbinfo; 11260 sfmmu_t *sfmmup = tsbinfop->tsb_sfmmu; 11261 11262 if (flags != HAT_POSTUNSUSPEND) 11263 return (0); 11264 11265 hatlockp = sfmmu_hat_enter(sfmmup); 11266 11267 SFMMU_STAT(sf_tsb_reloc); 11268 11269 /* 11270 * The process may have swapped out while we were relocating one 11271 * of its TSBs. If so, don't bother doing the setup since the 11272 * process can't be using the memory anymore. 11273 */ 11274 if ((tsbinfop->tsb_flags & TSB_SWAPPED) == 0) { 11275 ASSERT(va == tsbinfop->tsb_va); 11276 sfmmu_tsbinfo_setup_phys(tsbinfop, newpfn); 11277 sfmmu_setup_tsbinfo(sfmmup); 11278 11279 if (tsbinfop->tsb_flags & TSB_FLUSH_NEEDED) { 11280 sfmmu_inv_tsb(tsbinfop->tsb_va, 11281 TSB_BYTES(tsbinfop->tsb_szc)); 11282 tsbinfop->tsb_flags &= ~TSB_FLUSH_NEEDED; 11283 } 11284 } 11285 11286 membar_exit(); 11287 tsbinfop->tsb_flags &= ~TSB_RELOC_FLAG; 11288 cv_broadcast(&sfmmup->sfmmu_tsb_cv); 11289 11290 sfmmu_hat_exit(hatlockp); 11291 11292 return (0); 11293 } 11294 11295 /* 11296 * Allocate and initialize a tsb_info structure. Note that we may or may not 11297 * allocate a TSB here, depending on the flags passed in. 11298 */ 11299 static int 11300 sfmmu_tsbinfo_alloc(struct tsb_info **tsbinfopp, int tsb_szc, int tte_sz_mask, 11301 uint_t flags, sfmmu_t *sfmmup) 11302 { 11303 int err; 11304 11305 *tsbinfopp = (struct tsb_info *)kmem_cache_alloc( 11306 sfmmu_tsbinfo_cache, KM_SLEEP); 11307 11308 if ((err = sfmmu_init_tsbinfo(*tsbinfopp, tte_sz_mask, 11309 tsb_szc, flags, sfmmup)) != 0) { 11310 kmem_cache_free(sfmmu_tsbinfo_cache, *tsbinfopp); 11311 SFMMU_STAT(sf_tsb_allocfail); 11312 *tsbinfopp = NULL; 11313 return (err); 11314 } 11315 SFMMU_STAT(sf_tsb_alloc); 11316 11317 /* 11318 * Bump the TSB size counters for this TSB size. 11319 */ 11320 (*(((int *)&sfmmu_tsbsize_stat) + tsb_szc))++; 11321 return (0); 11322 } 11323 11324 static void 11325 sfmmu_tsb_free(struct tsb_info *tsbinfo) 11326 { 11327 caddr_t tsbva = tsbinfo->tsb_va; 11328 uint_t tsb_size = TSB_BYTES(tsbinfo->tsb_szc); 11329 struct kmem_cache *kmem_cachep = tsbinfo->tsb_cache; 11330 vmem_t *vmp = tsbinfo->tsb_vmp; 11331 11332 /* 11333 * If we allocated this TSB from relocatable kernel memory, then we 11334 * need to uninstall the callback handler. 11335 */ 11336 if (tsbinfo->tsb_cache != sfmmu_tsb8k_cache) { 11337 uintptr_t slab_mask = ~((uintptr_t)tsb_slab_mask) << PAGESHIFT; 11338 caddr_t slab_vaddr = (caddr_t)((uintptr_t)tsbva & slab_mask); 11339 page_t **ppl; 11340 int ret; 11341 11342 ret = as_pagelock(&kas, &ppl, slab_vaddr, PAGESIZE, S_WRITE); 11343 ASSERT(ret == 0); 11344 hat_delete_callback(tsbva, (uint_t)tsb_size, (void *)tsbinfo, 11345 0, NULL); 11346 as_pageunlock(&kas, ppl, slab_vaddr, PAGESIZE, S_WRITE); 11347 } 11348 11349 if (kmem_cachep != NULL) { 11350 kmem_cache_free(kmem_cachep, tsbva); 11351 } else { 11352 vmem_xfree(vmp, (void *)tsbva, tsb_size); 11353 } 11354 tsbinfo->tsb_va = (caddr_t)0xbad00bad; 11355 atomic_add_64(&tsb_alloc_bytes, -(int64_t)tsb_size); 11356 } 11357 11358 static void 11359 sfmmu_tsbinfo_free(struct tsb_info *tsbinfo) 11360 { 11361 if ((tsbinfo->tsb_flags & TSB_SWAPPED) == 0) { 11362 sfmmu_tsb_free(tsbinfo); 11363 } 11364 kmem_cache_free(sfmmu_tsbinfo_cache, tsbinfo); 11365 11366 } 11367 11368 /* 11369 * Setup all the references to physical memory for this tsbinfo. 11370 * The underlying page(s) must be locked. 11371 */ 11372 static void 11373 sfmmu_tsbinfo_setup_phys(struct tsb_info *tsbinfo, pfn_t pfn) 11374 { 11375 ASSERT(pfn != PFN_INVALID); 11376 ASSERT(pfn == va_to_pfn(tsbinfo->tsb_va)); 11377 11378 #ifndef sun4v 11379 if (tsbinfo->tsb_szc == 0) { 11380 sfmmu_memtte(&tsbinfo->tsb_tte, pfn, 11381 PROT_WRITE|PROT_READ, TTE8K); 11382 } else { 11383 /* 11384 * Round down PA and use a large mapping; the handlers will 11385 * compute the TSB pointer at the correct offset into the 11386 * big virtual page. NOTE: this assumes all TSBs larger 11387 * than 8K must come from physically contiguous slabs of 11388 * size tsb_slab_size. 11389 */ 11390 sfmmu_memtte(&tsbinfo->tsb_tte, pfn & ~tsb_slab_mask, 11391 PROT_WRITE|PROT_READ, tsb_slab_ttesz); 11392 } 11393 tsbinfo->tsb_pa = ptob(pfn); 11394 11395 TTE_SET_LOCKED(&tsbinfo->tsb_tte); /* lock the tte into dtlb */ 11396 TTE_SET_MOD(&tsbinfo->tsb_tte); /* enable writes */ 11397 11398 ASSERT(TTE_IS_PRIVILEGED(&tsbinfo->tsb_tte)); 11399 ASSERT(TTE_IS_LOCKED(&tsbinfo->tsb_tte)); 11400 #else /* sun4v */ 11401 tsbinfo->tsb_pa = ptob(pfn); 11402 #endif /* sun4v */ 11403 } 11404 11405 11406 /* 11407 * Returns zero on success, ENOMEM if over the high water mark, 11408 * or EAGAIN if the caller needs to retry with a smaller TSB 11409 * size (or specify TSB_FORCEALLOC if the allocation can't fail). 11410 * 11411 * This call cannot fail to allocate a TSB if TSB_FORCEALLOC 11412 * is specified and the TSB requested is PAGESIZE, though it 11413 * may sleep waiting for memory if sufficient memory is not 11414 * available. 11415 */ 11416 static int 11417 sfmmu_init_tsbinfo(struct tsb_info *tsbinfo, int tteszmask, 11418 int tsbcode, uint_t flags, sfmmu_t *sfmmup) 11419 { 11420 caddr_t vaddr = NULL; 11421 caddr_t slab_vaddr; 11422 uintptr_t slab_mask = ~((uintptr_t)tsb_slab_mask) << PAGESHIFT; 11423 int tsbbytes = TSB_BYTES(tsbcode); 11424 int lowmem = 0; 11425 struct kmem_cache *kmem_cachep = NULL; 11426 vmem_t *vmp = NULL; 11427 lgrp_id_t lgrpid = LGRP_NONE; 11428 pfn_t pfn; 11429 uint_t cbflags = HAC_SLEEP; 11430 page_t **pplist; 11431 int ret; 11432 11433 if (flags & (TSB_FORCEALLOC | TSB_SWAPIN | TSB_GROW | TSB_SHRINK)) 11434 flags |= TSB_ALLOC; 11435 11436 ASSERT((flags & TSB_FORCEALLOC) == 0 || tsbcode == TSB_MIN_SZCODE); 11437 11438 tsbinfo->tsb_sfmmu = sfmmup; 11439 11440 /* 11441 * If not allocating a TSB, set up the tsbinfo, set TSB_SWAPPED, and 11442 * return. 11443 */ 11444 if ((flags & TSB_ALLOC) == 0) { 11445 tsbinfo->tsb_szc = tsbcode; 11446 tsbinfo->tsb_ttesz_mask = tteszmask; 11447 tsbinfo->tsb_va = (caddr_t)0xbadbadbeef; 11448 tsbinfo->tsb_pa = -1; 11449 tsbinfo->tsb_tte.ll = 0; 11450 tsbinfo->tsb_next = NULL; 11451 tsbinfo->tsb_flags = TSB_SWAPPED; 11452 tsbinfo->tsb_cache = NULL; 11453 tsbinfo->tsb_vmp = NULL; 11454 return (0); 11455 } 11456 11457 #ifdef DEBUG 11458 /* 11459 * For debugging: 11460 * Randomly force allocation failures every tsb_alloc_mtbf 11461 * tries if TSB_FORCEALLOC is not specified. This will 11462 * return ENOMEM if tsb_alloc_mtbf is odd, or EAGAIN if 11463 * it is even, to allow testing of both failure paths... 11464 */ 11465 if (tsb_alloc_mtbf && ((flags & TSB_FORCEALLOC) == 0) && 11466 (tsb_alloc_count++ == tsb_alloc_mtbf)) { 11467 tsb_alloc_count = 0; 11468 tsb_alloc_fail_mtbf++; 11469 return ((tsb_alloc_mtbf & 1)? ENOMEM : EAGAIN); 11470 } 11471 #endif /* DEBUG */ 11472 11473 /* 11474 * Enforce high water mark if we are not doing a forced allocation 11475 * and are not shrinking a process' TSB. 11476 */ 11477 if ((flags & TSB_SHRINK) == 0 && 11478 (tsbbytes + tsb_alloc_bytes) > tsb_alloc_hiwater) { 11479 if ((flags & TSB_FORCEALLOC) == 0) 11480 return (ENOMEM); 11481 lowmem = 1; 11482 } 11483 11484 /* 11485 * Allocate from the correct location based upon the size of the TSB 11486 * compared to the base page size, and what memory conditions dictate. 11487 * Note we always do nonblocking allocations from the TSB arena since 11488 * we don't want memory fragmentation to cause processes to block 11489 * indefinitely waiting for memory; until the kernel algorithms that 11490 * coalesce large pages are improved this is our best option. 11491 * 11492 * Algorithm: 11493 * If allocating a "large" TSB (>8K), allocate from the 11494 * appropriate kmem_tsb_default_arena vmem arena 11495 * else if low on memory or the TSB_FORCEALLOC flag is set or 11496 * tsb_forceheap is set 11497 * Allocate from kernel heap via sfmmu_tsb8k_cache with 11498 * KM_SLEEP (never fails) 11499 * else 11500 * Allocate from appropriate sfmmu_tsb_cache with 11501 * KM_NOSLEEP 11502 * endif 11503 */ 11504 if (tsb_lgrp_affinity) 11505 lgrpid = lgrp_home_id(curthread); 11506 if (lgrpid == LGRP_NONE) 11507 lgrpid = 0; /* use lgrp of boot CPU */ 11508 11509 if (tsbbytes > MMU_PAGESIZE) { 11510 vmp = kmem_tsb_default_arena[lgrpid]; 11511 vaddr = (caddr_t)vmem_xalloc(vmp, tsbbytes, tsbbytes, 0, 0, 11512 NULL, NULL, VM_NOSLEEP); 11513 #ifdef DEBUG 11514 } else if (lowmem || (flags & TSB_FORCEALLOC) || tsb_forceheap) { 11515 #else /* !DEBUG */ 11516 } else if (lowmem || (flags & TSB_FORCEALLOC)) { 11517 #endif /* DEBUG */ 11518 kmem_cachep = sfmmu_tsb8k_cache; 11519 vaddr = (caddr_t)kmem_cache_alloc(kmem_cachep, KM_SLEEP); 11520 ASSERT(vaddr != NULL); 11521 } else { 11522 kmem_cachep = sfmmu_tsb_cache[lgrpid]; 11523 vaddr = (caddr_t)kmem_cache_alloc(kmem_cachep, KM_NOSLEEP); 11524 } 11525 11526 tsbinfo->tsb_cache = kmem_cachep; 11527 tsbinfo->tsb_vmp = vmp; 11528 11529 if (vaddr == NULL) { 11530 return (EAGAIN); 11531 } 11532 11533 atomic_add_64(&tsb_alloc_bytes, (int64_t)tsbbytes); 11534 kmem_cachep = tsbinfo->tsb_cache; 11535 11536 /* 11537 * If we are allocating from outside the cage, then we need to 11538 * register a relocation callback handler. Note that for now 11539 * since pseudo mappings always hang off of the slab's root page, 11540 * we need only lock the first 8K of the TSB slab. This is a bit 11541 * hacky but it is good for performance. 11542 */ 11543 if (kmem_cachep != sfmmu_tsb8k_cache) { 11544 slab_vaddr = (caddr_t)((uintptr_t)vaddr & slab_mask); 11545 ret = as_pagelock(&kas, &pplist, slab_vaddr, PAGESIZE, S_WRITE); 11546 ASSERT(ret == 0); 11547 ret = hat_add_callback(sfmmu_tsb_cb_id, vaddr, (uint_t)tsbbytes, 11548 cbflags, (void *)tsbinfo, &pfn, NULL); 11549 11550 /* 11551 * Need to free up resources if we could not successfully 11552 * add the callback function and return an error condition. 11553 */ 11554 if (ret != 0) { 11555 if (kmem_cachep) { 11556 kmem_cache_free(kmem_cachep, vaddr); 11557 } else { 11558 vmem_xfree(vmp, (void *)vaddr, tsbbytes); 11559 } 11560 as_pageunlock(&kas, pplist, slab_vaddr, PAGESIZE, 11561 S_WRITE); 11562 return (EAGAIN); 11563 } 11564 } else { 11565 /* 11566 * Since allocation of 8K TSBs from heap is rare and occurs 11567 * during memory pressure we allocate them from permanent 11568 * memory rather than using callbacks to get the PFN. 11569 */ 11570 pfn = hat_getpfnum(kas.a_hat, vaddr); 11571 } 11572 11573 tsbinfo->tsb_va = vaddr; 11574 tsbinfo->tsb_szc = tsbcode; 11575 tsbinfo->tsb_ttesz_mask = tteszmask; 11576 tsbinfo->tsb_next = NULL; 11577 tsbinfo->tsb_flags = 0; 11578 11579 sfmmu_tsbinfo_setup_phys(tsbinfo, pfn); 11580 11581 if (kmem_cachep != sfmmu_tsb8k_cache) { 11582 as_pageunlock(&kas, pplist, slab_vaddr, PAGESIZE, S_WRITE); 11583 } 11584 11585 sfmmu_inv_tsb(vaddr, tsbbytes); 11586 return (0); 11587 } 11588 11589 /* 11590 * Initialize per cpu tsb and per cpu tsbmiss_area 11591 */ 11592 void 11593 sfmmu_init_tsbs(void) 11594 { 11595 int i; 11596 struct tsbmiss *tsbmissp; 11597 struct kpmtsbm *kpmtsbmp; 11598 #ifndef sun4v 11599 extern int dcache_line_mask; 11600 #endif /* sun4v */ 11601 extern uint_t vac_colors; 11602 11603 /* 11604 * Init. tsb miss area. 11605 */ 11606 tsbmissp = tsbmiss_area; 11607 11608 for (i = 0; i < NCPU; tsbmissp++, i++) { 11609 /* 11610 * initialize the tsbmiss area. 11611 * Do this for all possible CPUs as some may be added 11612 * while the system is running. There is no cost to this. 11613 */ 11614 tsbmissp->ksfmmup = ksfmmup; 11615 #ifndef sun4v 11616 tsbmissp->dcache_line_mask = (uint16_t)dcache_line_mask; 11617 #endif /* sun4v */ 11618 tsbmissp->khashstart = 11619 (struct hmehash_bucket *)va_to_pa((caddr_t)khme_hash); 11620 tsbmissp->uhashstart = 11621 (struct hmehash_bucket *)va_to_pa((caddr_t)uhme_hash); 11622 tsbmissp->khashsz = khmehash_num; 11623 tsbmissp->uhashsz = uhmehash_num; 11624 } 11625 11626 sfmmu_tsb_cb_id = hat_register_callback('T'<<16 | 'S' << 8 | 'B', 11627 sfmmu_tsb_pre_relocator, sfmmu_tsb_post_relocator, NULL, 0); 11628 11629 if (kpm_enable == 0) 11630 return; 11631 11632 /* -- Begin KPM specific init -- */ 11633 11634 if (kpm_smallpages) { 11635 /* 11636 * If we're using base pagesize pages for seg_kpm 11637 * mappings, we use the kernel TSB since we can't afford 11638 * to allocate a second huge TSB for these mappings. 11639 */ 11640 kpm_tsbbase = ktsb_phys? ktsb_pbase : (uint64_t)ktsb_base; 11641 kpm_tsbsz = ktsb_szcode; 11642 kpmsm_tsbbase = kpm_tsbbase; 11643 kpmsm_tsbsz = kpm_tsbsz; 11644 } else { 11645 /* 11646 * In VAC conflict case, just put the entries in the 11647 * kernel 8K indexed TSB for now so we can find them. 11648 * This could really be changed in the future if we feel 11649 * the need... 11650 */ 11651 kpmsm_tsbbase = ktsb_phys? ktsb_pbase : (uint64_t)ktsb_base; 11652 kpmsm_tsbsz = ktsb_szcode; 11653 kpm_tsbbase = ktsb_phys? ktsb4m_pbase : (uint64_t)ktsb4m_base; 11654 kpm_tsbsz = ktsb4m_szcode; 11655 } 11656 11657 kpmtsbmp = kpmtsbm_area; 11658 for (i = 0; i < NCPU; kpmtsbmp++, i++) { 11659 /* 11660 * Initialize the kpmtsbm area. 11661 * Do this for all possible CPUs as some may be added 11662 * while the system is running. There is no cost to this. 11663 */ 11664 kpmtsbmp->vbase = kpm_vbase; 11665 kpmtsbmp->vend = kpm_vbase + kpm_size * vac_colors; 11666 kpmtsbmp->sz_shift = kpm_size_shift; 11667 kpmtsbmp->kpmp_shift = kpmp_shift; 11668 kpmtsbmp->kpmp2pshft = (uchar_t)kpmp2pshft; 11669 if (kpm_smallpages == 0) { 11670 kpmtsbmp->kpmp_table_sz = kpmp_table_sz; 11671 kpmtsbmp->kpmp_tablepa = va_to_pa(kpmp_table); 11672 } else { 11673 kpmtsbmp->kpmp_table_sz = kpmp_stable_sz; 11674 kpmtsbmp->kpmp_tablepa = va_to_pa(kpmp_stable); 11675 } 11676 kpmtsbmp->msegphashpa = va_to_pa(memseg_phash); 11677 kpmtsbmp->flags = KPMTSBM_ENABLE_FLAG; 11678 #ifdef DEBUG 11679 kpmtsbmp->flags |= (kpm_tsbmtl) ? KPMTSBM_TLTSBM_FLAG : 0; 11680 #endif /* DEBUG */ 11681 if (ktsb_phys) 11682 kpmtsbmp->flags |= KPMTSBM_TSBPHYS_FLAG; 11683 } 11684 11685 /* -- End KPM specific init -- */ 11686 } 11687 11688 /* Avoid using sfmmu_tsbinfo_alloc() to avoid kmem_alloc - no real reason */ 11689 struct tsb_info ktsb_info[2]; 11690 11691 /* 11692 * Called from hat_kern_setup() to setup the tsb_info for ksfmmup. 11693 */ 11694 void 11695 sfmmu_init_ktsbinfo() 11696 { 11697 ASSERT(ksfmmup != NULL); 11698 ASSERT(ksfmmup->sfmmu_tsb == NULL); 11699 /* 11700 * Allocate tsbinfos for kernel and copy in data 11701 * to make debug easier and sun4v setup easier. 11702 */ 11703 ktsb_info[0].tsb_sfmmu = ksfmmup; 11704 ktsb_info[0].tsb_szc = ktsb_szcode; 11705 ktsb_info[0].tsb_ttesz_mask = TSB8K|TSB64K|TSB512K; 11706 ktsb_info[0].tsb_va = ktsb_base; 11707 ktsb_info[0].tsb_pa = ktsb_pbase; 11708 ktsb_info[0].tsb_flags = 0; 11709 ktsb_info[0].tsb_tte.ll = 0; 11710 ktsb_info[0].tsb_cache = NULL; 11711 11712 ktsb_info[1].tsb_sfmmu = ksfmmup; 11713 ktsb_info[1].tsb_szc = ktsb4m_szcode; 11714 ktsb_info[1].tsb_ttesz_mask = TSB4M; 11715 ktsb_info[1].tsb_va = ktsb4m_base; 11716 ktsb_info[1].tsb_pa = ktsb4m_pbase; 11717 ktsb_info[1].tsb_flags = 0; 11718 ktsb_info[1].tsb_tte.ll = 0; 11719 ktsb_info[1].tsb_cache = NULL; 11720 11721 /* Link them into ksfmmup. */ 11722 ktsb_info[0].tsb_next = &ktsb_info[1]; 11723 ktsb_info[1].tsb_next = NULL; 11724 ksfmmup->sfmmu_tsb = &ktsb_info[0]; 11725 11726 sfmmu_setup_tsbinfo(ksfmmup); 11727 } 11728 11729 /* 11730 * Cache the last value returned from va_to_pa(). If the VA specified 11731 * in the current call to cached_va_to_pa() maps to the same Page (as the 11732 * previous call to cached_va_to_pa()), then compute the PA using 11733 * cached info, else call va_to_pa(). 11734 * 11735 * Note: this function is neither MT-safe nor consistent in the presence 11736 * of multiple, interleaved threads. This function was created to enable 11737 * an optimization used during boot (at a point when there's only one thread 11738 * executing on the "boot CPU", and before startup_vm() has been called). 11739 */ 11740 static uint64_t 11741 cached_va_to_pa(void *vaddr) 11742 { 11743 static uint64_t prev_vaddr_base = 0; 11744 static uint64_t prev_pfn = 0; 11745 11746 if ((((uint64_t)vaddr) & MMU_PAGEMASK) == prev_vaddr_base) { 11747 return (prev_pfn | ((uint64_t)vaddr & MMU_PAGEOFFSET)); 11748 } else { 11749 uint64_t pa = va_to_pa(vaddr); 11750 11751 if (pa != ((uint64_t)-1)) { 11752 /* 11753 * Computed physical address is valid. Cache its 11754 * related info for the next cached_va_to_pa() call. 11755 */ 11756 prev_pfn = pa & MMU_PAGEMASK; 11757 prev_vaddr_base = ((uint64_t)vaddr) & MMU_PAGEMASK; 11758 } 11759 11760 return (pa); 11761 } 11762 } 11763 11764 /* 11765 * Carve up our nucleus hblk region. We may allocate more hblks than 11766 * asked due to rounding errors but we are guaranteed to have at least 11767 * enough space to allocate the requested number of hblk8's and hblk1's. 11768 */ 11769 void 11770 sfmmu_init_nucleus_hblks(caddr_t addr, size_t size, int nhblk8, int nhblk1) 11771 { 11772 struct hme_blk *hmeblkp; 11773 size_t hme8blk_sz, hme1blk_sz; 11774 size_t i; 11775 size_t hblk8_bound; 11776 ulong_t j = 0, k = 0; 11777 11778 ASSERT(addr != NULL && size != 0); 11779 11780 /* Need to use proper structure alignment */ 11781 hme8blk_sz = roundup(HME8BLK_SZ, sizeof (int64_t)); 11782 hme1blk_sz = roundup(HME1BLK_SZ, sizeof (int64_t)); 11783 11784 nucleus_hblk8.list = (void *)addr; 11785 nucleus_hblk8.index = 0; 11786 11787 /* 11788 * Use as much memory as possible for hblk8's since we 11789 * expect all bop_alloc'ed memory to be allocated in 8k chunks. 11790 * We need to hold back enough space for the hblk1's which 11791 * we'll allocate next. 11792 */ 11793 hblk8_bound = size - (nhblk1 * hme1blk_sz) - hme8blk_sz; 11794 for (i = 0; i <= hblk8_bound; i += hme8blk_sz, j++) { 11795 hmeblkp = (struct hme_blk *)addr; 11796 addr += hme8blk_sz; 11797 hmeblkp->hblk_nuc_bit = 1; 11798 hmeblkp->hblk_nextpa = cached_va_to_pa((caddr_t)hmeblkp); 11799 } 11800 nucleus_hblk8.len = j; 11801 ASSERT(j >= nhblk8); 11802 SFMMU_STAT_ADD(sf_hblk8_ncreate, j); 11803 11804 nucleus_hblk1.list = (void *)addr; 11805 nucleus_hblk1.index = 0; 11806 for (; i <= (size - hme1blk_sz); i += hme1blk_sz, k++) { 11807 hmeblkp = (struct hme_blk *)addr; 11808 addr += hme1blk_sz; 11809 hmeblkp->hblk_nuc_bit = 1; 11810 hmeblkp->hblk_nextpa = cached_va_to_pa((caddr_t)hmeblkp); 11811 } 11812 ASSERT(k >= nhblk1); 11813 nucleus_hblk1.len = k; 11814 SFMMU_STAT_ADD(sf_hblk1_ncreate, k); 11815 } 11816 11817 /* 11818 * This function is currently not supported on this platform. For what 11819 * it's supposed to do, see hat.c and hat_srmmu.c 11820 */ 11821 /* ARGSUSED */ 11822 faultcode_t 11823 hat_softlock(struct hat *hat, caddr_t addr, size_t *lenp, page_t **ppp, 11824 uint_t flags) 11825 { 11826 ASSERT(hat->sfmmu_xhat_provider == NULL); 11827 return (FC_NOSUPPORT); 11828 } 11829 11830 /* 11831 * Searchs the mapping list of the page for a mapping of the same size. If not 11832 * found the corresponding bit is cleared in the p_index field. When large 11833 * pages are more prevalent in the system, we can maintain the mapping list 11834 * in order and we don't have to traverse the list each time. Just check the 11835 * next and prev entries, and if both are of different size, we clear the bit. 11836 */ 11837 static void 11838 sfmmu_rm_large_mappings(page_t *pp, int ttesz) 11839 { 11840 struct sf_hment *sfhmep; 11841 struct hme_blk *hmeblkp; 11842 int index; 11843 pgcnt_t npgs; 11844 11845 ASSERT(ttesz > TTE8K); 11846 11847 ASSERT(sfmmu_mlist_held(pp)); 11848 11849 ASSERT(PP_ISMAPPED_LARGE(pp)); 11850 11851 /* 11852 * Traverse mapping list looking for another mapping of same size. 11853 * since we only want to clear index field if all mappings of 11854 * that size are gone. 11855 */ 11856 11857 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) { 11858 hmeblkp = sfmmu_hmetohblk(sfhmep); 11859 if (hmeblkp->hblk_xhat_bit) 11860 continue; 11861 if (hme_size(sfhmep) == ttesz) { 11862 /* 11863 * another mapping of the same size. don't clear index. 11864 */ 11865 return; 11866 } 11867 } 11868 11869 /* 11870 * Clear the p_index bit for large page. 11871 */ 11872 index = PAGESZ_TO_INDEX(ttesz); 11873 npgs = TTEPAGES(ttesz); 11874 while (npgs-- > 0) { 11875 ASSERT(pp->p_index & index); 11876 pp->p_index &= ~index; 11877 pp = PP_PAGENEXT(pp); 11878 } 11879 } 11880 11881 /* 11882 * return supported features 11883 */ 11884 /* ARGSUSED */ 11885 int 11886 hat_supported(enum hat_features feature, void *arg) 11887 { 11888 switch (feature) { 11889 case HAT_SHARED_PT: 11890 case HAT_DYNAMIC_ISM_UNMAP: 11891 case HAT_VMODSORT: 11892 return (1); 11893 default: 11894 return (0); 11895 } 11896 } 11897 11898 void 11899 hat_enter(struct hat *hat) 11900 { 11901 hatlock_t *hatlockp; 11902 11903 if (hat != ksfmmup) { 11904 hatlockp = TSB_HASH(hat); 11905 mutex_enter(HATLOCK_MUTEXP(hatlockp)); 11906 } 11907 } 11908 11909 void 11910 hat_exit(struct hat *hat) 11911 { 11912 hatlock_t *hatlockp; 11913 11914 if (hat != ksfmmup) { 11915 hatlockp = TSB_HASH(hat); 11916 mutex_exit(HATLOCK_MUTEXP(hatlockp)); 11917 } 11918 } 11919 11920 /*ARGSUSED*/ 11921 void 11922 hat_reserve(struct as *as, caddr_t addr, size_t len) 11923 { 11924 } 11925 11926 static void 11927 hat_kstat_init(void) 11928 { 11929 kstat_t *ksp; 11930 11931 ksp = kstat_create("unix", 0, "sfmmu_global_stat", "hat", 11932 KSTAT_TYPE_RAW, sizeof (struct sfmmu_global_stat), 11933 KSTAT_FLAG_VIRTUAL); 11934 if (ksp) { 11935 ksp->ks_data = (void *) &sfmmu_global_stat; 11936 kstat_install(ksp); 11937 } 11938 ksp = kstat_create("unix", 0, "sfmmu_tsbsize_stat", "hat", 11939 KSTAT_TYPE_RAW, sizeof (struct sfmmu_tsbsize_stat), 11940 KSTAT_FLAG_VIRTUAL); 11941 if (ksp) { 11942 ksp->ks_data = (void *) &sfmmu_tsbsize_stat; 11943 kstat_install(ksp); 11944 } 11945 ksp = kstat_create("unix", 0, "sfmmu_percpu_stat", "hat", 11946 KSTAT_TYPE_RAW, sizeof (struct sfmmu_percpu_stat) * NCPU, 11947 KSTAT_FLAG_WRITABLE); 11948 if (ksp) { 11949 ksp->ks_update = sfmmu_kstat_percpu_update; 11950 kstat_install(ksp); 11951 } 11952 } 11953 11954 /* ARGSUSED */ 11955 static int 11956 sfmmu_kstat_percpu_update(kstat_t *ksp, int rw) 11957 { 11958 struct sfmmu_percpu_stat *cpu_kstat = ksp->ks_data; 11959 struct tsbmiss *tsbm = tsbmiss_area; 11960 struct kpmtsbm *kpmtsbm = kpmtsbm_area; 11961 int i; 11962 11963 ASSERT(cpu_kstat); 11964 if (rw == KSTAT_READ) { 11965 for (i = 0; i < NCPU; cpu_kstat++, tsbm++, kpmtsbm++, i++) { 11966 cpu_kstat->sf_itlb_misses = tsbm->itlb_misses; 11967 cpu_kstat->sf_dtlb_misses = tsbm->dtlb_misses; 11968 cpu_kstat->sf_utsb_misses = tsbm->utsb_misses - 11969 tsbm->uprot_traps; 11970 cpu_kstat->sf_ktsb_misses = tsbm->ktsb_misses + 11971 kpmtsbm->kpm_tsb_misses - tsbm->kprot_traps; 11972 11973 if (tsbm->itlb_misses > 0 && tsbm->dtlb_misses > 0) { 11974 cpu_kstat->sf_tsb_hits = 11975 (tsbm->itlb_misses + tsbm->dtlb_misses) - 11976 (tsbm->utsb_misses + tsbm->ktsb_misses + 11977 kpmtsbm->kpm_tsb_misses); 11978 } else { 11979 cpu_kstat->sf_tsb_hits = 0; 11980 } 11981 cpu_kstat->sf_umod_faults = tsbm->uprot_traps; 11982 cpu_kstat->sf_kmod_faults = tsbm->kprot_traps; 11983 } 11984 } else { 11985 /* KSTAT_WRITE is used to clear stats */ 11986 for (i = 0; i < NCPU; tsbm++, kpmtsbm++, i++) { 11987 tsbm->itlb_misses = 0; 11988 tsbm->dtlb_misses = 0; 11989 tsbm->utsb_misses = 0; 11990 tsbm->ktsb_misses = 0; 11991 tsbm->uprot_traps = 0; 11992 tsbm->kprot_traps = 0; 11993 kpmtsbm->kpm_dtlb_misses = 0; 11994 kpmtsbm->kpm_tsb_misses = 0; 11995 } 11996 } 11997 return (0); 11998 } 11999 12000 #ifdef DEBUG 12001 12002 tte_t *gorig[NCPU], *gcur[NCPU], *gnew[NCPU]; 12003 12004 /* 12005 * A tte checker. *orig_old is the value we read before cas. 12006 * *cur is the value returned by cas. 12007 * *new is the desired value when we do the cas. 12008 * 12009 * *hmeblkp is currently unused. 12010 */ 12011 12012 /* ARGSUSED */ 12013 void 12014 chk_tte(tte_t *orig_old, tte_t *cur, tte_t *new, struct hme_blk *hmeblkp) 12015 { 12016 pfn_t i, j, k; 12017 int cpuid = CPU->cpu_id; 12018 12019 gorig[cpuid] = orig_old; 12020 gcur[cpuid] = cur; 12021 gnew[cpuid] = new; 12022 12023 #ifdef lint 12024 hmeblkp = hmeblkp; 12025 #endif 12026 12027 if (TTE_IS_VALID(orig_old)) { 12028 if (TTE_IS_VALID(cur)) { 12029 i = TTE_TO_TTEPFN(orig_old); 12030 j = TTE_TO_TTEPFN(cur); 12031 k = TTE_TO_TTEPFN(new); 12032 if (i != j) { 12033 /* remap error? */ 12034 panic("chk_tte: bad pfn, 0x%lx, 0x%lx", i, j); 12035 } 12036 12037 if (i != k) { 12038 /* remap error? */ 12039 panic("chk_tte: bad pfn2, 0x%lx, 0x%lx", i, k); 12040 } 12041 } else { 12042 if (TTE_IS_VALID(new)) { 12043 panic("chk_tte: invalid cur? "); 12044 } 12045 12046 i = TTE_TO_TTEPFN(orig_old); 12047 k = TTE_TO_TTEPFN(new); 12048 if (i != k) { 12049 panic("chk_tte: bad pfn3, 0x%lx, 0x%lx", i, k); 12050 } 12051 } 12052 } else { 12053 if (TTE_IS_VALID(cur)) { 12054 j = TTE_TO_TTEPFN(cur); 12055 if (TTE_IS_VALID(new)) { 12056 k = TTE_TO_TTEPFN(new); 12057 if (j != k) { 12058 panic("chk_tte: bad pfn4, 0x%lx, 0x%lx", 12059 j, k); 12060 } 12061 } else { 12062 panic("chk_tte: why here?"); 12063 } 12064 } else { 12065 if (!TTE_IS_VALID(new)) { 12066 panic("chk_tte: why here2 ?"); 12067 } 12068 } 12069 } 12070 } 12071 12072 #endif /* DEBUG */ 12073 12074 extern void prefetch_tsbe_read(struct tsbe *); 12075 extern void prefetch_tsbe_write(struct tsbe *); 12076 12077 12078 /* 12079 * We want to prefetch 7 cache lines ahead for our read prefetch. This gives 12080 * us optimal performance on Cheetah+. You can only have 8 outstanding 12081 * prefetches at any one time, so we opted for 7 read prefetches and 1 write 12082 * prefetch to make the most utilization of the prefetch capability. 12083 */ 12084 #define TSBE_PREFETCH_STRIDE (7) 12085 12086 void 12087 sfmmu_copy_tsb(struct tsb_info *old_tsbinfo, struct tsb_info *new_tsbinfo) 12088 { 12089 int old_bytes = TSB_BYTES(old_tsbinfo->tsb_szc); 12090 int new_bytes = TSB_BYTES(new_tsbinfo->tsb_szc); 12091 int old_entries = TSB_ENTRIES(old_tsbinfo->tsb_szc); 12092 int new_entries = TSB_ENTRIES(new_tsbinfo->tsb_szc); 12093 struct tsbe *old; 12094 struct tsbe *new; 12095 struct tsbe *new_base = (struct tsbe *)new_tsbinfo->tsb_va; 12096 uint64_t va; 12097 int new_offset; 12098 int i; 12099 int vpshift; 12100 int last_prefetch; 12101 12102 if (old_bytes == new_bytes) { 12103 bcopy(old_tsbinfo->tsb_va, new_tsbinfo->tsb_va, new_bytes); 12104 } else { 12105 12106 /* 12107 * A TSBE is 16 bytes which means there are four TSBE's per 12108 * P$ line (64 bytes), thus every 4 TSBE's we prefetch. 12109 */ 12110 old = (struct tsbe *)old_tsbinfo->tsb_va; 12111 last_prefetch = old_entries - (4*(TSBE_PREFETCH_STRIDE+1)); 12112 for (i = 0; i < old_entries; i++, old++) { 12113 if (((i & (4-1)) == 0) && (i < last_prefetch)) 12114 prefetch_tsbe_read(old); 12115 if (!old->tte_tag.tag_invalid) { 12116 /* 12117 * We have a valid TTE to remap. Check the 12118 * size. We won't remap 64K or 512K TTEs 12119 * because they span more than one TSB entry 12120 * and are indexed using an 8K virt. page. 12121 * Ditto for 32M and 256M TTEs. 12122 */ 12123 if (TTE_CSZ(&old->tte_data) == TTE64K || 12124 TTE_CSZ(&old->tte_data) == TTE512K) 12125 continue; 12126 if (mmu_page_sizes == max_mmu_page_sizes) { 12127 if (TTE_CSZ(&old->tte_data) == TTE32M || 12128 TTE_CSZ(&old->tte_data) == TTE256M) 12129 continue; 12130 } 12131 12132 /* clear the lower 22 bits of the va */ 12133 va = *(uint64_t *)old << 22; 12134 /* turn va into a virtual pfn */ 12135 va >>= 22 - TSB_START_SIZE; 12136 /* 12137 * or in bits from the offset in the tsb 12138 * to get the real virtual pfn. These 12139 * correspond to bits [21:13] in the va 12140 */ 12141 vpshift = 12142 TTE_BSZS_SHIFT(TTE_CSZ(&old->tte_data)) & 12143 0x1ff; 12144 va |= (i << vpshift); 12145 va >>= vpshift; 12146 new_offset = va & (new_entries - 1); 12147 new = new_base + new_offset; 12148 prefetch_tsbe_write(new); 12149 *new = *old; 12150 } 12151 } 12152 } 12153 } 12154 12155 /* 12156 * unused in sfmmu 12157 */ 12158 void 12159 hat_dump(void) 12160 { 12161 } 12162 12163 /* 12164 * Called when a thread is exiting and we have switched to the kernel address 12165 * space. Perform the same VM initialization resume() uses when switching 12166 * processes. 12167 * 12168 * Note that sfmmu_load_mmustate() is currently a no-op for kernel threads, but 12169 * we call it anyway in case the semantics change in the future. 12170 */ 12171 /*ARGSUSED*/ 12172 void 12173 hat_thread_exit(kthread_t *thd) 12174 { 12175 uint64_t pgsz_cnum; 12176 uint_t pstate_save; 12177 12178 ASSERT(thd->t_procp->p_as == &kas); 12179 12180 pgsz_cnum = KCONTEXT; 12181 #ifdef sun4u 12182 pgsz_cnum |= (ksfmmup->sfmmu_cext << CTXREG_EXT_SHIFT); 12183 #endif 12184 /* 12185 * Note that sfmmu_load_mmustate() is currently a no-op for 12186 * kernel threads. We need to disable interrupts here, 12187 * simply because otherwise sfmmu_load_mmustate() would panic 12188 * if the caller does not disable interrupts. 12189 */ 12190 pstate_save = sfmmu_disable_intrs(); 12191 sfmmu_setctx_sec(pgsz_cnum); 12192 sfmmu_load_mmustate(ksfmmup); 12193 sfmmu_enable_intrs(pstate_save); 12194 } 12195