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 2008 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 <vm/vm_dep.h> 84 #include <vm/xhat_sfmmu.h> 85 #include <sys/fpu/fpusystm.h> 86 #include <vm/mach_kpm.h> 87 #include <sys/callb.h> 88 89 #ifdef DEBUG 90 #define SFMMU_VALIDATE_HMERID(hat, rid, saddr, len) \ 91 if (SFMMU_IS_SHMERID_VALID(rid)) { \ 92 caddr_t _eaddr = (saddr) + (len); \ 93 sf_srd_t *_srdp; \ 94 sf_region_t *_rgnp; \ 95 ASSERT((rid) < SFMMU_MAX_HME_REGIONS); \ 96 ASSERT(SF_RGNMAP_TEST(hat->sfmmu_hmeregion_map, rid)); \ 97 ASSERT((hat) != ksfmmup); \ 98 _srdp = (hat)->sfmmu_srdp; \ 99 ASSERT(_srdp != NULL); \ 100 ASSERT(_srdp->srd_refcnt != 0); \ 101 _rgnp = _srdp->srd_hmergnp[(rid)]; \ 102 ASSERT(_rgnp != NULL && _rgnp->rgn_id == rid); \ 103 ASSERT(_rgnp->rgn_refcnt != 0); \ 104 ASSERT(!(_rgnp->rgn_flags & SFMMU_REGION_FREE)); \ 105 ASSERT((_rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == \ 106 SFMMU_REGION_HME); \ 107 ASSERT((saddr) >= _rgnp->rgn_saddr); \ 108 ASSERT((saddr) < _rgnp->rgn_saddr + _rgnp->rgn_size); \ 109 ASSERT(_eaddr > _rgnp->rgn_saddr); \ 110 ASSERT(_eaddr <= _rgnp->rgn_saddr + _rgnp->rgn_size); \ 111 } 112 113 #define SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid) \ 114 { \ 115 caddr_t _hsva; \ 116 caddr_t _heva; \ 117 caddr_t _rsva; \ 118 caddr_t _reva; \ 119 int _ttesz = get_hblk_ttesz(hmeblkp); \ 120 int _flagtte; \ 121 ASSERT((srdp)->srd_refcnt != 0); \ 122 ASSERT((rid) < SFMMU_MAX_HME_REGIONS); \ 123 ASSERT((rgnp)->rgn_id == rid); \ 124 ASSERT(!((rgnp)->rgn_flags & SFMMU_REGION_FREE)); \ 125 ASSERT(((rgnp)->rgn_flags & SFMMU_REGION_TYPE_MASK) == \ 126 SFMMU_REGION_HME); \ 127 ASSERT(_ttesz <= (rgnp)->rgn_pgszc); \ 128 _hsva = (caddr_t)get_hblk_base(hmeblkp); \ 129 _heva = get_hblk_endaddr(hmeblkp); \ 130 _rsva = (caddr_t)P2ALIGN( \ 131 (uintptr_t)(rgnp)->rgn_saddr, HBLK_MIN_BYTES); \ 132 _reva = (caddr_t)P2ROUNDUP( \ 133 (uintptr_t)((rgnp)->rgn_saddr + (rgnp)->rgn_size), \ 134 HBLK_MIN_BYTES); \ 135 ASSERT(_hsva >= _rsva); \ 136 ASSERT(_hsva < _reva); \ 137 ASSERT(_heva > _rsva); \ 138 ASSERT(_heva <= _reva); \ 139 _flagtte = (_ttesz < HBLK_MIN_TTESZ) ? HBLK_MIN_TTESZ : \ 140 _ttesz; \ 141 ASSERT(rgnp->rgn_hmeflags & (0x1 << _flagtte)); \ 142 } 143 144 #else /* DEBUG */ 145 #define SFMMU_VALIDATE_HMERID(hat, rid, addr, len) 146 #define SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid) 147 #endif /* DEBUG */ 148 149 #if defined(SF_ERRATA_57) 150 extern caddr_t errata57_limit; 151 #endif 152 153 #define HME8BLK_SZ_RND ((roundup(HME8BLK_SZ, sizeof (int64_t))) / \ 154 (sizeof (int64_t))) 155 #define HBLK_RESERVE ((struct hme_blk *)hblk_reserve) 156 157 #define HBLK_RESERVE_CNT 128 158 #define HBLK_RESERVE_MIN 20 159 160 static struct hme_blk *freehblkp; 161 static kmutex_t freehblkp_lock; 162 static int freehblkcnt; 163 164 static int64_t hblk_reserve[HME8BLK_SZ_RND]; 165 static kmutex_t hblk_reserve_lock; 166 static kthread_t *hblk_reserve_thread; 167 168 static nucleus_hblk8_info_t nucleus_hblk8; 169 static nucleus_hblk1_info_t nucleus_hblk1; 170 171 /* 172 * SFMMU specific hat functions 173 */ 174 void hat_pagecachectl(struct page *, int); 175 176 /* flags for hat_pagecachectl */ 177 #define HAT_CACHE 0x1 178 #define HAT_UNCACHE 0x2 179 #define HAT_TMPNC 0x4 180 181 /* 182 * Flag to allow the creation of non-cacheable translations 183 * to system memory. It is off by default. At the moment this 184 * flag is used by the ecache error injector. The error injector 185 * will turn it on when creating such a translation then shut it 186 * off when it's finished. 187 */ 188 189 int sfmmu_allow_nc_trans = 0; 190 191 /* 192 * Flag to disable large page support. 193 * value of 1 => disable all large pages. 194 * bits 1, 2, and 3 are to disable 64K, 512K and 4M pages respectively. 195 * 196 * For example, use the value 0x4 to disable 512K pages. 197 * 198 */ 199 #define LARGE_PAGES_OFF 0x1 200 201 /* 202 * The disable_large_pages and disable_ism_large_pages variables control 203 * hat_memload_array and the page sizes to be used by ISM and the kernel. 204 * 205 * The disable_auto_data_large_pages and disable_auto_text_large_pages variables 206 * are only used to control which OOB pages to use at upper VM segment creation 207 * time, and are set in hat_init_pagesizes and used in the map_pgsz* routines. 208 * Their values may come from platform or CPU specific code to disable page 209 * sizes that should not be used. 210 * 211 * WARNING: 512K pages are currently not supported for ISM/DISM. 212 */ 213 uint_t disable_large_pages = 0; 214 uint_t disable_ism_large_pages = (1 << TTE512K); 215 uint_t disable_auto_data_large_pages = 0; 216 uint_t disable_auto_text_large_pages = 0; 217 218 /* 219 * Private sfmmu data structures for hat management 220 */ 221 static struct kmem_cache *sfmmuid_cache; 222 static struct kmem_cache *mmuctxdom_cache; 223 224 /* 225 * Private sfmmu data structures for tsb management 226 */ 227 static struct kmem_cache *sfmmu_tsbinfo_cache; 228 static struct kmem_cache *sfmmu_tsb8k_cache; 229 static struct kmem_cache *sfmmu_tsb_cache[NLGRPS_MAX]; 230 static vmem_t *kmem_bigtsb_arena; 231 static vmem_t *kmem_tsb_arena; 232 233 /* 234 * sfmmu static variables for hmeblk resource management. 235 */ 236 static vmem_t *hat_memload1_arena; /* HAT translation arena for sfmmu1_cache */ 237 static struct kmem_cache *sfmmu8_cache; 238 static struct kmem_cache *sfmmu1_cache; 239 static struct kmem_cache *pa_hment_cache; 240 241 static kmutex_t ism_mlist_lock; /* mutex for ism mapping list */ 242 /* 243 * private data for ism 244 */ 245 static struct kmem_cache *ism_blk_cache; 246 static struct kmem_cache *ism_ment_cache; 247 #define ISMID_STARTADDR NULL 248 249 /* 250 * Region management data structures and function declarations. 251 */ 252 253 static void sfmmu_leave_srd(sfmmu_t *); 254 static int sfmmu_srdcache_constructor(void *, void *, int); 255 static void sfmmu_srdcache_destructor(void *, void *); 256 static int sfmmu_rgncache_constructor(void *, void *, int); 257 static void sfmmu_rgncache_destructor(void *, void *); 258 static int sfrgnmap_isnull(sf_region_map_t *); 259 static int sfhmergnmap_isnull(sf_hmeregion_map_t *); 260 static int sfmmu_scdcache_constructor(void *, void *, int); 261 static void sfmmu_scdcache_destructor(void *, void *); 262 static void sfmmu_rgn_cb_noop(caddr_t, caddr_t, caddr_t, 263 size_t, void *, u_offset_t); 264 265 static uint_t srd_hashmask = SFMMU_MAX_SRD_BUCKETS - 1; 266 static sf_srd_bucket_t *srd_buckets; 267 static struct kmem_cache *srd_cache; 268 static uint_t srd_rgn_hashmask = SFMMU_MAX_REGION_BUCKETS - 1; 269 static struct kmem_cache *region_cache; 270 static struct kmem_cache *scd_cache; 271 272 #ifdef sun4v 273 int use_bigtsb_arena = 1; 274 #else 275 int use_bigtsb_arena = 0; 276 #endif 277 278 /* External /etc/system tunable, for turning on&off the shctx support */ 279 int disable_shctx = 0; 280 /* Internal variable, set by MD if the HW supports shctx feature */ 281 int shctx_on = 0; 282 283 #ifdef DEBUG 284 static void check_scd_sfmmu_list(sfmmu_t **, sfmmu_t *, int); 285 #endif 286 static void sfmmu_to_scd_list(sfmmu_t **, sfmmu_t *); 287 static void sfmmu_from_scd_list(sfmmu_t **, sfmmu_t *); 288 289 static sf_scd_t *sfmmu_alloc_scd(sf_srd_t *, sf_region_map_t *); 290 static void sfmmu_find_scd(sfmmu_t *); 291 static void sfmmu_join_scd(sf_scd_t *, sfmmu_t *); 292 static void sfmmu_finish_join_scd(sfmmu_t *); 293 static void sfmmu_leave_scd(sfmmu_t *, uchar_t); 294 static void sfmmu_destroy_scd(sf_srd_t *, sf_scd_t *, sf_region_map_t *); 295 static int sfmmu_alloc_scd_tsbs(sf_srd_t *, sf_scd_t *); 296 static void sfmmu_free_scd_tsbs(sfmmu_t *); 297 static void sfmmu_tsb_inv_ctx(sfmmu_t *); 298 static int find_ism_rid(sfmmu_t *, sfmmu_t *, caddr_t, uint_t *); 299 static void sfmmu_ism_hatflags(sfmmu_t *, int); 300 static int sfmmu_srd_lock_held(sf_srd_t *); 301 static void sfmmu_remove_scd(sf_scd_t **, sf_scd_t *); 302 static void sfmmu_add_scd(sf_scd_t **headp, sf_scd_t *); 303 static void sfmmu_link_scd_to_regions(sf_srd_t *, sf_scd_t *); 304 static void sfmmu_unlink_scd_from_regions(sf_srd_t *, sf_scd_t *); 305 static void sfmmu_link_to_hmeregion(sfmmu_t *, sf_region_t *); 306 static void sfmmu_unlink_from_hmeregion(sfmmu_t *, sf_region_t *); 307 308 /* 309 * ``hat_lock'' is a hashed mutex lock for protecting sfmmu TSB lists, 310 * HAT flags, synchronizing TLB/TSB coherency, and context management. 311 * The lock is hashed on the sfmmup since the case where we need to lock 312 * all processes is rare but does occur (e.g. we need to unload a shared 313 * mapping from all processes using the mapping). We have a lot of buckets, 314 * and each slab of sfmmu_t's can use about a quarter of them, giving us 315 * a fairly good distribution without wasting too much space and overhead 316 * when we have to grab them all. 317 */ 318 #define SFMMU_NUM_LOCK 128 /* must be power of two */ 319 hatlock_t hat_lock[SFMMU_NUM_LOCK]; 320 321 /* 322 * Hash algorithm optimized for a small number of slabs. 323 * 7 is (highbit((sizeof sfmmu_t)) - 1) 324 * This hash algorithm is based upon the knowledge that sfmmu_t's come from a 325 * kmem_cache, and thus they will be sequential within that cache. In 326 * addition, each new slab will have a different "color" up to cache_maxcolor 327 * which will skew the hashing for each successive slab which is allocated. 328 * If the size of sfmmu_t changed to a larger size, this algorithm may need 329 * to be revisited. 330 */ 331 #define TSB_HASH_SHIFT_BITS (7) 332 #define PTR_HASH(x) ((uintptr_t)x >> TSB_HASH_SHIFT_BITS) 333 334 #ifdef DEBUG 335 int tsb_hash_debug = 0; 336 #define TSB_HASH(sfmmup) \ 337 (tsb_hash_debug ? &hat_lock[0] : \ 338 &hat_lock[PTR_HASH(sfmmup) & (SFMMU_NUM_LOCK-1)]) 339 #else /* DEBUG */ 340 #define TSB_HASH(sfmmup) &hat_lock[PTR_HASH(sfmmup) & (SFMMU_NUM_LOCK-1)] 341 #endif /* DEBUG */ 342 343 344 /* sfmmu_replace_tsb() return codes. */ 345 typedef enum tsb_replace_rc { 346 TSB_SUCCESS, 347 TSB_ALLOCFAIL, 348 TSB_LOSTRACE, 349 TSB_ALREADY_SWAPPED, 350 TSB_CANTGROW 351 } tsb_replace_rc_t; 352 353 /* 354 * Flags for TSB allocation routines. 355 */ 356 #define TSB_ALLOC 0x01 357 #define TSB_FORCEALLOC 0x02 358 #define TSB_GROW 0x04 359 #define TSB_SHRINK 0x08 360 #define TSB_SWAPIN 0x10 361 362 /* 363 * Support for HAT callbacks. 364 */ 365 #define SFMMU_MAX_RELOC_CALLBACKS 10 366 int sfmmu_max_cb_id = SFMMU_MAX_RELOC_CALLBACKS; 367 static id_t sfmmu_cb_nextid = 0; 368 static id_t sfmmu_tsb_cb_id; 369 struct sfmmu_callback *sfmmu_cb_table; 370 371 /* 372 * Kernel page relocation is enabled by default for non-caged 373 * kernel pages. This has little effect unless segkmem_reloc is 374 * set, since by default kernel memory comes from inside the 375 * kernel cage. 376 */ 377 int hat_kpr_enabled = 1; 378 379 kmutex_t kpr_mutex; 380 kmutex_t kpr_suspendlock; 381 kthread_t *kreloc_thread; 382 383 /* 384 * Enable VA->PA translation sanity checking on DEBUG kernels. 385 * Disabled by default. This is incompatible with some 386 * drivers (error injector, RSM) so if it breaks you get 387 * to keep both pieces. 388 */ 389 int hat_check_vtop = 0; 390 391 /* 392 * Private sfmmu routines (prototypes) 393 */ 394 static struct hme_blk *sfmmu_shadow_hcreate(sfmmu_t *, caddr_t, int, uint_t); 395 static struct hme_blk *sfmmu_hblk_alloc(sfmmu_t *, caddr_t, 396 struct hmehash_bucket *, uint_t, hmeblk_tag, uint_t, 397 uint_t); 398 static caddr_t sfmmu_hblk_unload(struct hat *, struct hme_blk *, caddr_t, 399 caddr_t, demap_range_t *, uint_t); 400 static caddr_t sfmmu_hblk_sync(struct hat *, struct hme_blk *, caddr_t, 401 caddr_t, int); 402 static void sfmmu_hblk_free(struct hmehash_bucket *, struct hme_blk *, 403 uint64_t, struct hme_blk **); 404 static void sfmmu_hblks_list_purge(struct hme_blk **); 405 static uint_t sfmmu_get_free_hblk(struct hme_blk **, uint_t); 406 static uint_t sfmmu_put_free_hblk(struct hme_blk *, uint_t); 407 static struct hme_blk *sfmmu_hblk_steal(int); 408 static int sfmmu_steal_this_hblk(struct hmehash_bucket *, 409 struct hme_blk *, uint64_t, uint64_t, 410 struct hme_blk *); 411 static caddr_t sfmmu_hblk_unlock(struct hme_blk *, caddr_t, caddr_t); 412 413 static void hat_do_memload_array(struct hat *, caddr_t, size_t, 414 struct page **, uint_t, uint_t, uint_t); 415 static void hat_do_memload(struct hat *, caddr_t, struct page *, 416 uint_t, uint_t, uint_t); 417 static void sfmmu_memload_batchsmall(struct hat *, caddr_t, page_t **, 418 uint_t, uint_t, pgcnt_t, uint_t); 419 void sfmmu_tteload(struct hat *, tte_t *, caddr_t, page_t *, 420 uint_t); 421 static int sfmmu_tteload_array(sfmmu_t *, tte_t *, caddr_t, page_t **, 422 uint_t, uint_t); 423 static struct hmehash_bucket *sfmmu_tteload_acquire_hashbucket(sfmmu_t *, 424 caddr_t, int, uint_t); 425 static struct hme_blk *sfmmu_tteload_find_hmeblk(sfmmu_t *, 426 struct hmehash_bucket *, caddr_t, uint_t, uint_t, 427 uint_t); 428 static int sfmmu_tteload_addentry(sfmmu_t *, struct hme_blk *, tte_t *, 429 caddr_t, page_t **, uint_t, uint_t); 430 static void sfmmu_tteload_release_hashbucket(struct hmehash_bucket *); 431 432 static int sfmmu_pagearray_setup(caddr_t, page_t **, tte_t *, int); 433 static pfn_t sfmmu_uvatopfn(caddr_t, sfmmu_t *, tte_t *); 434 void sfmmu_memtte(tte_t *, pfn_t, uint_t, int); 435 #ifdef VAC 436 static void sfmmu_vac_conflict(struct hat *, caddr_t, page_t *); 437 static int sfmmu_vacconflict_array(caddr_t, page_t *, int *); 438 int tst_tnc(page_t *pp, pgcnt_t); 439 void conv_tnc(page_t *pp, int); 440 #endif 441 442 static void sfmmu_get_ctx(sfmmu_t *); 443 static void sfmmu_free_sfmmu(sfmmu_t *); 444 445 static void sfmmu_ttesync(struct hat *, caddr_t, tte_t *, page_t *); 446 static void sfmmu_chgattr(struct hat *, caddr_t, size_t, uint_t, int); 447 448 cpuset_t sfmmu_pageunload(page_t *, struct sf_hment *, int); 449 static void hat_pagereload(struct page *, struct page *); 450 static cpuset_t sfmmu_pagesync(page_t *, struct sf_hment *, uint_t); 451 #ifdef VAC 452 void sfmmu_page_cache_array(page_t *, int, int, pgcnt_t); 453 static void sfmmu_page_cache(page_t *, int, int, int); 454 #endif 455 456 cpuset_t sfmmu_rgntlb_demap(caddr_t, sf_region_t *, 457 struct hme_blk *, int); 458 static void sfmmu_tlbcache_demap(caddr_t, sfmmu_t *, struct hme_blk *, 459 pfn_t, int, int, int, int); 460 static void sfmmu_ismtlbcache_demap(caddr_t, sfmmu_t *, struct hme_blk *, 461 pfn_t, int); 462 static void sfmmu_tlb_demap(caddr_t, sfmmu_t *, struct hme_blk *, int, int); 463 static void sfmmu_tlb_range_demap(demap_range_t *); 464 static void sfmmu_invalidate_ctx(sfmmu_t *); 465 static void sfmmu_sync_mmustate(sfmmu_t *); 466 467 static void sfmmu_tsbinfo_setup_phys(struct tsb_info *, pfn_t); 468 static int sfmmu_tsbinfo_alloc(struct tsb_info **, int, int, uint_t, 469 sfmmu_t *); 470 static void sfmmu_tsb_free(struct tsb_info *); 471 static void sfmmu_tsbinfo_free(struct tsb_info *); 472 static int sfmmu_init_tsbinfo(struct tsb_info *, int, int, uint_t, 473 sfmmu_t *); 474 static void sfmmu_tsb_chk_reloc(sfmmu_t *, hatlock_t *); 475 static void sfmmu_tsb_swapin(sfmmu_t *, hatlock_t *); 476 static int sfmmu_select_tsb_szc(pgcnt_t); 477 static void sfmmu_mod_tsb(sfmmu_t *, caddr_t, tte_t *, int); 478 #define sfmmu_load_tsb(sfmmup, vaddr, tte, szc) \ 479 sfmmu_mod_tsb(sfmmup, vaddr, tte, szc) 480 #define sfmmu_unload_tsb(sfmmup, vaddr, szc) \ 481 sfmmu_mod_tsb(sfmmup, vaddr, NULL, szc) 482 static void sfmmu_copy_tsb(struct tsb_info *, struct tsb_info *); 483 static tsb_replace_rc_t sfmmu_replace_tsb(sfmmu_t *, struct tsb_info *, uint_t, 484 hatlock_t *, uint_t); 485 static void sfmmu_size_tsb(sfmmu_t *, int, uint64_t, uint64_t, int); 486 487 #ifdef VAC 488 void sfmmu_cache_flush(pfn_t, int); 489 void sfmmu_cache_flushcolor(int, pfn_t); 490 #endif 491 static caddr_t sfmmu_hblk_chgattr(sfmmu_t *, struct hme_blk *, caddr_t, 492 caddr_t, demap_range_t *, uint_t, int); 493 494 static uint64_t sfmmu_vtop_attr(uint_t, int mode, tte_t *); 495 static uint_t sfmmu_ptov_attr(tte_t *); 496 static caddr_t sfmmu_hblk_chgprot(sfmmu_t *, struct hme_blk *, caddr_t, 497 caddr_t, demap_range_t *, uint_t); 498 static uint_t sfmmu_vtop_prot(uint_t, uint_t *); 499 static int sfmmu_idcache_constructor(void *, void *, int); 500 static void sfmmu_idcache_destructor(void *, void *); 501 static int sfmmu_hblkcache_constructor(void *, void *, int); 502 static void sfmmu_hblkcache_destructor(void *, void *); 503 static void sfmmu_hblkcache_reclaim(void *); 504 static void sfmmu_shadow_hcleanup(sfmmu_t *, struct hme_blk *, 505 struct hmehash_bucket *); 506 static void sfmmu_free_hblks(sfmmu_t *, caddr_t, caddr_t, int); 507 static void sfmmu_cleanup_rhblk(sf_srd_t *, caddr_t, uint_t, int); 508 static void sfmmu_unload_hmeregion_va(sf_srd_t *, uint_t, caddr_t, caddr_t, 509 int, caddr_t *); 510 static void sfmmu_unload_hmeregion(sf_srd_t *, sf_region_t *); 511 512 static void sfmmu_rm_large_mappings(page_t *, int); 513 514 static void hat_lock_init(void); 515 static void hat_kstat_init(void); 516 static int sfmmu_kstat_percpu_update(kstat_t *ksp, int rw); 517 static void sfmmu_set_scd_rttecnt(sf_srd_t *, sf_scd_t *); 518 static int sfmmu_is_rgnva(sf_srd_t *, caddr_t, ulong_t, ulong_t); 519 static void sfmmu_check_page_sizes(sfmmu_t *, int); 520 int fnd_mapping_sz(page_t *); 521 static void iment_add(struct ism_ment *, struct hat *); 522 static void iment_sub(struct ism_ment *, struct hat *); 523 static pgcnt_t ism_tsb_entries(sfmmu_t *, int szc); 524 extern void sfmmu_setup_tsbinfo(sfmmu_t *); 525 extern void sfmmu_clear_utsbinfo(void); 526 527 static void sfmmu_ctx_wrap_around(mmu_ctx_t *); 528 529 /* kpm globals */ 530 #ifdef DEBUG 531 /* 532 * Enable trap level tsbmiss handling 533 */ 534 int kpm_tsbmtl = 1; 535 536 /* 537 * Flush the TLB on kpm mapout. Note: Xcalls are used (again) for the 538 * required TLB shootdowns in this case, so handle w/ care. Off by default. 539 */ 540 int kpm_tlb_flush; 541 #endif /* DEBUG */ 542 543 static void *sfmmu_vmem_xalloc_aligned_wrapper(vmem_t *, size_t, int); 544 545 #ifdef DEBUG 546 static void sfmmu_check_hblk_flist(); 547 #endif 548 549 /* 550 * Semi-private sfmmu data structures. Some of them are initialize in 551 * startup or in hat_init. Some of them are private but accessed by 552 * assembly code or mach_sfmmu.c 553 */ 554 struct hmehash_bucket *uhme_hash; /* user hmeblk hash table */ 555 struct hmehash_bucket *khme_hash; /* kernel hmeblk hash table */ 556 uint64_t uhme_hash_pa; /* PA of uhme_hash */ 557 uint64_t khme_hash_pa; /* PA of khme_hash */ 558 int uhmehash_num; /* # of buckets in user hash table */ 559 int khmehash_num; /* # of buckets in kernel hash table */ 560 561 uint_t max_mmu_ctxdoms = 0; /* max context domains in the system */ 562 mmu_ctx_t **mmu_ctxs_tbl; /* global array of context domains */ 563 uint64_t mmu_saved_gnum = 0; /* to init incoming MMUs' gnums */ 564 565 #define DEFAULT_NUM_CTXS_PER_MMU 8192 566 static uint_t nctxs = DEFAULT_NUM_CTXS_PER_MMU; 567 568 int cache; /* describes system cache */ 569 570 caddr_t ktsb_base; /* kernel 8k-indexed tsb base address */ 571 uint64_t ktsb_pbase; /* kernel 8k-indexed tsb phys address */ 572 int ktsb_szcode; /* kernel 8k-indexed tsb size code */ 573 int ktsb_sz; /* kernel 8k-indexed tsb size */ 574 575 caddr_t ktsb4m_base; /* kernel 4m-indexed tsb base address */ 576 uint64_t ktsb4m_pbase; /* kernel 4m-indexed tsb phys address */ 577 int ktsb4m_szcode; /* kernel 4m-indexed tsb size code */ 578 int ktsb4m_sz; /* kernel 4m-indexed tsb size */ 579 580 uint64_t kpm_tsbbase; /* kernel seg_kpm 4M TSB base address */ 581 int kpm_tsbsz; /* kernel seg_kpm 4M TSB size code */ 582 uint64_t kpmsm_tsbbase; /* kernel seg_kpm 8K TSB base address */ 583 int kpmsm_tsbsz; /* kernel seg_kpm 8K TSB size code */ 584 585 #ifndef sun4v 586 int utsb_dtlb_ttenum = -1; /* index in TLB for utsb locked TTE */ 587 int utsb4m_dtlb_ttenum = -1; /* index in TLB for 4M TSB TTE */ 588 int dtlb_resv_ttenum; /* index in TLB of first reserved TTE */ 589 caddr_t utsb_vabase; /* reserved kernel virtual memory */ 590 caddr_t utsb4m_vabase; /* for trap handler TSB accesses */ 591 #endif /* sun4v */ 592 uint64_t tsb_alloc_bytes = 0; /* bytes allocated to TSBs */ 593 vmem_t *kmem_tsb_default_arena[NLGRPS_MAX]; /* For dynamic TSBs */ 594 vmem_t *kmem_bigtsb_default_arena[NLGRPS_MAX]; /* dynamic 256M TSBs */ 595 596 /* 597 * Size to use for TSB slabs. Future platforms that support page sizes 598 * larger than 4M may wish to change these values, and provide their own 599 * assembly macros for building and decoding the TSB base register contents. 600 * Note disable_large_pages will override the value set here. 601 */ 602 static uint_t tsb_slab_ttesz = TTE4M; 603 size_t tsb_slab_size = MMU_PAGESIZE4M; 604 uint_t tsb_slab_shift = MMU_PAGESHIFT4M; 605 /* PFN mask for TTE */ 606 size_t tsb_slab_mask = MMU_PAGEOFFSET4M >> MMU_PAGESHIFT; 607 608 /* 609 * Size to use for TSB slabs. These are used only when 256M tsb arenas 610 * exist. 611 */ 612 static uint_t bigtsb_slab_ttesz = TTE256M; 613 static size_t bigtsb_slab_size = MMU_PAGESIZE256M; 614 static uint_t bigtsb_slab_shift = MMU_PAGESHIFT256M; 615 /* 256M page alignment for 8K pfn */ 616 static size_t bigtsb_slab_mask = MMU_PAGEOFFSET256M >> MMU_PAGESHIFT; 617 618 /* largest TSB size to grow to, will be smaller on smaller memory systems */ 619 static int tsb_max_growsize = 0; 620 621 /* 622 * Tunable parameters dealing with TSB policies. 623 */ 624 625 /* 626 * This undocumented tunable forces all 8K TSBs to be allocated from 627 * the kernel heap rather than from the kmem_tsb_default_arena arenas. 628 */ 629 #ifdef DEBUG 630 int tsb_forceheap = 0; 631 #endif /* DEBUG */ 632 633 /* 634 * Decide whether to use per-lgroup arenas, or one global set of 635 * TSB arenas. The default is not to break up per-lgroup, since 636 * most platforms don't recognize any tangible benefit from it. 637 */ 638 int tsb_lgrp_affinity = 0; 639 640 /* 641 * Used for growing the TSB based on the process RSS. 642 * tsb_rss_factor is based on the smallest TSB, and is 643 * shifted by the TSB size to determine if we need to grow. 644 * The default will grow the TSB if the number of TTEs for 645 * this page size exceeds 75% of the number of TSB entries, 646 * which should _almost_ eliminate all conflict misses 647 * (at the expense of using up lots and lots of memory). 648 */ 649 #define TSB_RSS_FACTOR (TSB_ENTRIES(TSB_MIN_SZCODE) * 0.75) 650 #define SFMMU_RSS_TSBSIZE(tsbszc) (tsb_rss_factor << tsbszc) 651 #define SELECT_TSB_SIZECODE(pgcnt) ( \ 652 (enable_tsb_rss_sizing)? sfmmu_select_tsb_szc(pgcnt) : \ 653 default_tsb_size) 654 #define TSB_OK_SHRINK() \ 655 (tsb_alloc_bytes > tsb_alloc_hiwater || freemem < desfree) 656 #define TSB_OK_GROW() \ 657 (tsb_alloc_bytes < tsb_alloc_hiwater && freemem > desfree) 658 659 int enable_tsb_rss_sizing = 1; 660 int tsb_rss_factor = (int)TSB_RSS_FACTOR; 661 662 /* which TSB size code to use for new address spaces or if rss sizing off */ 663 int default_tsb_size = TSB_8K_SZCODE; 664 665 static uint64_t tsb_alloc_hiwater; /* limit TSB reserved memory */ 666 uint64_t tsb_alloc_hiwater_factor; /* tsb_alloc_hiwater = physmem / this */ 667 #define TSB_ALLOC_HIWATER_FACTOR_DEFAULT 32 668 669 #ifdef DEBUG 670 static int tsb_random_size = 0; /* set to 1 to test random tsb sizes on alloc */ 671 static int tsb_grow_stress = 0; /* if set to 1, keep replacing TSB w/ random */ 672 static int tsb_alloc_mtbf = 0; /* fail allocation every n attempts */ 673 static int tsb_alloc_fail_mtbf = 0; 674 static int tsb_alloc_count = 0; 675 #endif /* DEBUG */ 676 677 /* if set to 1, will remap valid TTEs when growing TSB. */ 678 int tsb_remap_ttes = 1; 679 680 /* 681 * If we have more than this many mappings, allocate a second TSB. 682 * This default is chosen because the I/D fully associative TLBs are 683 * assumed to have at least 8 available entries. Platforms with a 684 * larger fully-associative TLB could probably override the default. 685 */ 686 687 #ifdef sun4v 688 int tsb_sectsb_threshold = 0; 689 #else 690 int tsb_sectsb_threshold = 8; 691 #endif 692 693 /* 694 * kstat data 695 */ 696 struct sfmmu_global_stat sfmmu_global_stat; 697 struct sfmmu_tsbsize_stat sfmmu_tsbsize_stat; 698 699 /* 700 * Global data 701 */ 702 sfmmu_t *ksfmmup; /* kernel's hat id */ 703 704 #ifdef DEBUG 705 static void chk_tte(tte_t *, tte_t *, tte_t *, struct hme_blk *); 706 #endif 707 708 /* sfmmu locking operations */ 709 static kmutex_t *sfmmu_mlspl_enter(struct page *, int); 710 static int sfmmu_mlspl_held(struct page *, int); 711 712 kmutex_t *sfmmu_page_enter(page_t *); 713 void sfmmu_page_exit(kmutex_t *); 714 int sfmmu_page_spl_held(struct page *); 715 716 /* sfmmu internal locking operations - accessed directly */ 717 static void sfmmu_mlist_reloc_enter(page_t *, page_t *, 718 kmutex_t **, kmutex_t **); 719 static void sfmmu_mlist_reloc_exit(kmutex_t *, kmutex_t *); 720 static hatlock_t * 721 sfmmu_hat_enter(sfmmu_t *); 722 static hatlock_t * 723 sfmmu_hat_tryenter(sfmmu_t *); 724 static void sfmmu_hat_exit(hatlock_t *); 725 static void sfmmu_hat_lock_all(void); 726 static void sfmmu_hat_unlock_all(void); 727 static void sfmmu_ismhat_enter(sfmmu_t *, int); 728 static void sfmmu_ismhat_exit(sfmmu_t *, int); 729 730 /* 731 * Array of mutexes protecting a page's mapping list and p_nrm field. 732 * 733 * The hash function looks complicated, but is made up so that: 734 * 735 * "pp" not shifted, so adjacent pp values will hash to different cache lines 736 * (8 byte alignment * 8 bytes/mutes == 64 byte coherency subblock) 737 * 738 * "pp" >> mml_shift, incorporates more source bits into the hash result 739 * 740 * "& (mml_table_size - 1), should be faster than using remainder "%" 741 * 742 * Hopefully, mml_table, mml_table_size and mml_shift are all in the same 743 * cacheline, since they get declared next to each other below. We'll trust 744 * ld not to do something random. 745 */ 746 #ifdef DEBUG 747 int mlist_hash_debug = 0; 748 #define MLIST_HASH(pp) (mlist_hash_debug ? &mml_table[0] : \ 749 &mml_table[((uintptr_t)(pp) + \ 750 ((uintptr_t)(pp) >> mml_shift)) & (mml_table_sz - 1)]) 751 #else /* !DEBUG */ 752 #define MLIST_HASH(pp) &mml_table[ \ 753 ((uintptr_t)(pp) + ((uintptr_t)(pp) >> mml_shift)) & (mml_table_sz - 1)] 754 #endif /* !DEBUG */ 755 756 kmutex_t *mml_table; 757 uint_t mml_table_sz; /* must be a power of 2 */ 758 uint_t mml_shift; /* log2(mml_table_sz) + 3 for align */ 759 760 kpm_hlk_t *kpmp_table; 761 uint_t kpmp_table_sz; /* must be a power of 2 */ 762 uchar_t kpmp_shift; 763 764 kpm_shlk_t *kpmp_stable; 765 uint_t kpmp_stable_sz; /* must be a power of 2 */ 766 767 /* 768 * SPL_HASH was improved to avoid false cache line sharing 769 */ 770 #define SPL_TABLE_SIZE 128 771 #define SPL_MASK (SPL_TABLE_SIZE - 1) 772 #define SPL_SHIFT 7 /* log2(SPL_TABLE_SIZE) */ 773 774 #define SPL_INDEX(pp) \ 775 ((((uintptr_t)(pp) >> SPL_SHIFT) ^ \ 776 ((uintptr_t)(pp) >> (SPL_SHIFT << 1))) & \ 777 (SPL_TABLE_SIZE - 1)) 778 779 #define SPL_HASH(pp) \ 780 (&sfmmu_page_lock[SPL_INDEX(pp) & SPL_MASK].pad_mutex) 781 782 static pad_mutex_t sfmmu_page_lock[SPL_TABLE_SIZE]; 783 784 785 /* 786 * hat_unload_callback() will group together callbacks in order 787 * to avoid xt_sync() calls. This is the maximum size of the group. 788 */ 789 #define MAX_CB_ADDR 32 790 791 tte_t hw_tte; 792 static ulong_t sfmmu_dmr_maxbit = DMR_MAXBIT; 793 794 static char *mmu_ctx_kstat_names[] = { 795 "mmu_ctx_tsb_exceptions", 796 "mmu_ctx_tsb_raise_exception", 797 "mmu_ctx_wrap_around", 798 }; 799 800 /* 801 * Wrapper for vmem_xalloc since vmem_create only allows limited 802 * parameters for vm_source_alloc functions. This function allows us 803 * to specify alignment consistent with the size of the object being 804 * allocated. 805 */ 806 static void * 807 sfmmu_vmem_xalloc_aligned_wrapper(vmem_t *vmp, size_t size, int vmflag) 808 { 809 return (vmem_xalloc(vmp, size, size, 0, 0, NULL, NULL, vmflag)); 810 } 811 812 /* Common code for setting tsb_alloc_hiwater. */ 813 #define SFMMU_SET_TSB_ALLOC_HIWATER(pages) tsb_alloc_hiwater = \ 814 ptob(pages) / tsb_alloc_hiwater_factor 815 816 /* 817 * Set tsb_max_growsize to allow at most all of physical memory to be mapped by 818 * a single TSB. physmem is the number of physical pages so we need physmem 8K 819 * TTEs to represent all those physical pages. We round this up by using 820 * 1<<highbit(). To figure out which size code to use, remember that the size 821 * code is just an amount to shift the smallest TSB size to get the size of 822 * this TSB. So we subtract that size, TSB_START_SIZE, from highbit() (or 823 * highbit() - 1) to get the size code for the smallest TSB that can represent 824 * all of physical memory, while erring on the side of too much. 825 * 826 * Restrict tsb_max_growsize to make sure that: 827 * 1) TSBs can't grow larger than the TSB slab size 828 * 2) TSBs can't grow larger than UTSB_MAX_SZCODE. 829 */ 830 #define SFMMU_SET_TSB_MAX_GROWSIZE(pages) { \ 831 int _i, _szc, _slabszc, _tsbszc; \ 832 \ 833 _i = highbit(pages); \ 834 if ((1 << (_i - 1)) == (pages)) \ 835 _i--; /* 2^n case, round down */ \ 836 _szc = _i - TSB_START_SIZE; \ 837 _slabszc = bigtsb_slab_shift - (TSB_START_SIZE + TSB_ENTRY_SHIFT); \ 838 _tsbszc = MIN(_szc, _slabszc); \ 839 tsb_max_growsize = MIN(_tsbszc, UTSB_MAX_SZCODE); \ 840 } 841 842 /* 843 * Given a pointer to an sfmmu and a TTE size code, return a pointer to the 844 * tsb_info which handles that TTE size. 845 */ 846 #define SFMMU_GET_TSBINFO(tsbinfop, sfmmup, tte_szc) { \ 847 (tsbinfop) = (sfmmup)->sfmmu_tsb; \ 848 ASSERT(((tsbinfop)->tsb_flags & TSB_SHAREDCTX) || \ 849 sfmmu_hat_lock_held(sfmmup)); \ 850 if ((tte_szc) >= TTE4M) { \ 851 ASSERT((tsbinfop) != NULL); \ 852 (tsbinfop) = (tsbinfop)->tsb_next; \ 853 } \ 854 } 855 856 /* 857 * Macro to use to unload entries from the TSB. 858 * It has knowledge of which page sizes get replicated in the TSB 859 * and will call the appropriate unload routine for the appropriate size. 860 */ 861 #define SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, ismhat) \ 862 { \ 863 int ttesz = get_hblk_ttesz(hmeblkp); \ 864 if (ttesz == TTE8K || ttesz == TTE4M) { \ 865 sfmmu_unload_tsb(sfmmup, addr, ttesz); \ 866 } else { \ 867 caddr_t sva = ismhat ? addr : \ 868 (caddr_t)get_hblk_base(hmeblkp); \ 869 caddr_t eva = sva + get_hblk_span(hmeblkp); \ 870 ASSERT(addr >= sva && addr < eva); \ 871 sfmmu_unload_tsb_range(sfmmup, sva, eva, ttesz); \ 872 } \ 873 } 874 875 876 /* Update tsb_alloc_hiwater after memory is configured. */ 877 /*ARGSUSED*/ 878 static void 879 sfmmu_update_post_add(void *arg, pgcnt_t delta_pages) 880 { 881 /* Assumes physmem has already been updated. */ 882 SFMMU_SET_TSB_ALLOC_HIWATER(physmem); 883 SFMMU_SET_TSB_MAX_GROWSIZE(physmem); 884 } 885 886 /* 887 * Update tsb_alloc_hiwater before memory is deleted. We'll do nothing here 888 * and update tsb_alloc_hiwater and tsb_max_growsize after the memory is 889 * deleted. 890 */ 891 /*ARGSUSED*/ 892 static int 893 sfmmu_update_pre_del(void *arg, pgcnt_t delta_pages) 894 { 895 return (0); 896 } 897 898 /* Update tsb_alloc_hiwater after memory fails to be unconfigured. */ 899 /*ARGSUSED*/ 900 static void 901 sfmmu_update_post_del(void *arg, pgcnt_t delta_pages, int cancelled) 902 { 903 /* 904 * Whether the delete was cancelled or not, just go ahead and update 905 * tsb_alloc_hiwater and tsb_max_growsize. 906 */ 907 SFMMU_SET_TSB_ALLOC_HIWATER(physmem); 908 SFMMU_SET_TSB_MAX_GROWSIZE(physmem); 909 } 910 911 static kphysm_setup_vector_t sfmmu_update_vec = { 912 KPHYSM_SETUP_VECTOR_VERSION, /* version */ 913 sfmmu_update_post_add, /* post_add */ 914 sfmmu_update_pre_del, /* pre_del */ 915 sfmmu_update_post_del /* post_del */ 916 }; 917 918 919 /* 920 * HME_BLK HASH PRIMITIVES 921 */ 922 923 /* 924 * Enter a hme on the mapping list for page pp. 925 * When large pages are more prevalent in the system we might want to 926 * keep the mapping list in ascending order by the hment size. For now, 927 * small pages are more frequent, so don't slow it down. 928 */ 929 #define HME_ADD(hme, pp) \ 930 { \ 931 ASSERT(sfmmu_mlist_held(pp)); \ 932 \ 933 hme->hme_prev = NULL; \ 934 hme->hme_next = pp->p_mapping; \ 935 hme->hme_page = pp; \ 936 if (pp->p_mapping) { \ 937 ((struct sf_hment *)(pp->p_mapping))->hme_prev = hme;\ 938 ASSERT(pp->p_share > 0); \ 939 } else { \ 940 /* EMPTY */ \ 941 ASSERT(pp->p_share == 0); \ 942 } \ 943 pp->p_mapping = hme; \ 944 pp->p_share++; \ 945 } 946 947 /* 948 * Enter a hme on the mapping list for page pp. 949 * If we are unmapping a large translation, we need to make sure that the 950 * change is reflect in the corresponding bit of the p_index field. 951 */ 952 #define HME_SUB(hme, pp) \ 953 { \ 954 ASSERT(sfmmu_mlist_held(pp)); \ 955 ASSERT(hme->hme_page == pp || IS_PAHME(hme)); \ 956 \ 957 if (pp->p_mapping == NULL) { \ 958 panic("hme_remove - no mappings"); \ 959 } \ 960 \ 961 membar_stst(); /* ensure previous stores finish */ \ 962 \ 963 ASSERT(pp->p_share > 0); \ 964 pp->p_share--; \ 965 \ 966 if (hme->hme_prev) { \ 967 ASSERT(pp->p_mapping != hme); \ 968 ASSERT(hme->hme_prev->hme_page == pp || \ 969 IS_PAHME(hme->hme_prev)); \ 970 hme->hme_prev->hme_next = hme->hme_next; \ 971 } else { \ 972 ASSERT(pp->p_mapping == hme); \ 973 pp->p_mapping = hme->hme_next; \ 974 ASSERT((pp->p_mapping == NULL) ? \ 975 (pp->p_share == 0) : 1); \ 976 } \ 977 \ 978 if (hme->hme_next) { \ 979 ASSERT(hme->hme_next->hme_page == pp || \ 980 IS_PAHME(hme->hme_next)); \ 981 hme->hme_next->hme_prev = hme->hme_prev; \ 982 } \ 983 \ 984 /* zero out the entry */ \ 985 hme->hme_next = NULL; \ 986 hme->hme_prev = NULL; \ 987 hme->hme_page = NULL; \ 988 \ 989 if (hme_size(hme) > TTE8K) { \ 990 /* remove mappings for remainder of large pg */ \ 991 sfmmu_rm_large_mappings(pp, hme_size(hme)); \ 992 } \ 993 } 994 995 /* 996 * This function returns the hment given the hme_blk and a vaddr. 997 * It assumes addr has already been checked to belong to hme_blk's 998 * range. 999 */ 1000 #define HBLKTOHME(hment, hmeblkp, addr) \ 1001 { \ 1002 int index; \ 1003 HBLKTOHME_IDX(hment, hmeblkp, addr, index) \ 1004 } 1005 1006 /* 1007 * Version of HBLKTOHME that also returns the index in hmeblkp 1008 * of the hment. 1009 */ 1010 #define HBLKTOHME_IDX(hment, hmeblkp, addr, idx) \ 1011 { \ 1012 ASSERT(in_hblk_range((hmeblkp), (addr))); \ 1013 \ 1014 if (get_hblk_ttesz(hmeblkp) == TTE8K) { \ 1015 idx = (((uintptr_t)(addr) >> MMU_PAGESHIFT) & (NHMENTS-1)); \ 1016 } else \ 1017 idx = 0; \ 1018 \ 1019 (hment) = &(hmeblkp)->hblk_hme[idx]; \ 1020 } 1021 1022 /* 1023 * Disable any page sizes not supported by the CPU 1024 */ 1025 void 1026 hat_init_pagesizes() 1027 { 1028 int i; 1029 1030 mmu_exported_page_sizes = 0; 1031 for (i = TTE8K; i < max_mmu_page_sizes; i++) { 1032 1033 szc_2_userszc[i] = (uint_t)-1; 1034 userszc_2_szc[i] = (uint_t)-1; 1035 1036 if ((mmu_exported_pagesize_mask & (1 << i)) == 0) { 1037 disable_large_pages |= (1 << i); 1038 } else { 1039 szc_2_userszc[i] = mmu_exported_page_sizes; 1040 userszc_2_szc[mmu_exported_page_sizes] = i; 1041 mmu_exported_page_sizes++; 1042 } 1043 } 1044 1045 disable_ism_large_pages |= disable_large_pages; 1046 disable_auto_data_large_pages = disable_large_pages; 1047 disable_auto_text_large_pages = disable_large_pages; 1048 1049 /* 1050 * Initialize mmu-specific large page sizes. 1051 */ 1052 if (&mmu_large_pages_disabled) { 1053 disable_large_pages |= mmu_large_pages_disabled(HAT_LOAD); 1054 disable_ism_large_pages |= 1055 mmu_large_pages_disabled(HAT_LOAD_SHARE); 1056 disable_auto_data_large_pages |= 1057 mmu_large_pages_disabled(HAT_AUTO_DATA); 1058 disable_auto_text_large_pages |= 1059 mmu_large_pages_disabled(HAT_AUTO_TEXT); 1060 } 1061 } 1062 1063 /* 1064 * Initialize the hardware address translation structures. 1065 */ 1066 void 1067 hat_init(void) 1068 { 1069 int i; 1070 uint_t sz; 1071 size_t size; 1072 1073 hat_lock_init(); 1074 hat_kstat_init(); 1075 1076 /* 1077 * Hardware-only bits in a TTE 1078 */ 1079 MAKE_TTE_MASK(&hw_tte); 1080 1081 hat_init_pagesizes(); 1082 1083 /* Initialize the hash locks */ 1084 for (i = 0; i < khmehash_num; i++) { 1085 mutex_init(&khme_hash[i].hmehash_mutex, NULL, 1086 MUTEX_DEFAULT, NULL); 1087 } 1088 for (i = 0; i < uhmehash_num; i++) { 1089 mutex_init(&uhme_hash[i].hmehash_mutex, NULL, 1090 MUTEX_DEFAULT, NULL); 1091 } 1092 khmehash_num--; /* make sure counter starts from 0 */ 1093 uhmehash_num--; /* make sure counter starts from 0 */ 1094 1095 /* 1096 * Allocate context domain structures. 1097 * 1098 * A platform may choose to modify max_mmu_ctxdoms in 1099 * set_platform_defaults(). If a platform does not define 1100 * a set_platform_defaults() or does not choose to modify 1101 * max_mmu_ctxdoms, it gets one MMU context domain for every CPU. 1102 * 1103 * For sun4v, there will be one global context domain, this is to 1104 * avoid the ldom cpu substitution problem. 1105 * 1106 * For all platforms that have CPUs sharing MMUs, this 1107 * value must be defined. 1108 */ 1109 if (max_mmu_ctxdoms == 0) { 1110 #ifndef sun4v 1111 max_mmu_ctxdoms = max_ncpus; 1112 #else /* sun4v */ 1113 max_mmu_ctxdoms = 1; 1114 #endif /* sun4v */ 1115 } 1116 1117 size = max_mmu_ctxdoms * sizeof (mmu_ctx_t *); 1118 mmu_ctxs_tbl = kmem_zalloc(size, KM_SLEEP); 1119 1120 /* mmu_ctx_t is 64 bytes aligned */ 1121 mmuctxdom_cache = kmem_cache_create("mmuctxdom_cache", 1122 sizeof (mmu_ctx_t), 64, NULL, NULL, NULL, NULL, NULL, 0); 1123 /* 1124 * MMU context domain initialization for the Boot CPU. 1125 * This needs the context domains array allocated above. 1126 */ 1127 mutex_enter(&cpu_lock); 1128 sfmmu_cpu_init(CPU); 1129 mutex_exit(&cpu_lock); 1130 1131 /* 1132 * Intialize ism mapping list lock. 1133 */ 1134 1135 mutex_init(&ism_mlist_lock, NULL, MUTEX_DEFAULT, NULL); 1136 1137 /* 1138 * Each sfmmu structure carries an array of MMU context info 1139 * structures, one per context domain. The size of this array depends 1140 * on the maximum number of context domains. So, the size of the 1141 * sfmmu structure varies per platform. 1142 * 1143 * sfmmu is allocated from static arena, because trap 1144 * handler at TL > 0 is not allowed to touch kernel relocatable 1145 * memory. sfmmu's alignment is changed to 64 bytes from 1146 * default 8 bytes, as the lower 6 bits will be used to pass 1147 * pgcnt to vtag_flush_pgcnt_tl1. 1148 */ 1149 size = sizeof (sfmmu_t) + sizeof (sfmmu_ctx_t) * (max_mmu_ctxdoms - 1); 1150 1151 sfmmuid_cache = kmem_cache_create("sfmmuid_cache", size, 1152 64, sfmmu_idcache_constructor, sfmmu_idcache_destructor, 1153 NULL, NULL, static_arena, 0); 1154 1155 sfmmu_tsbinfo_cache = kmem_cache_create("sfmmu_tsbinfo_cache", 1156 sizeof (struct tsb_info), 0, NULL, NULL, NULL, NULL, NULL, 0); 1157 1158 /* 1159 * Since we only use the tsb8k cache to "borrow" pages for TSBs 1160 * from the heap when low on memory or when TSB_FORCEALLOC is 1161 * specified, don't use magazines to cache them--we want to return 1162 * them to the system as quickly as possible. 1163 */ 1164 sfmmu_tsb8k_cache = kmem_cache_create("sfmmu_tsb8k_cache", 1165 MMU_PAGESIZE, MMU_PAGESIZE, NULL, NULL, NULL, NULL, 1166 static_arena, KMC_NOMAGAZINE); 1167 1168 /* 1169 * Set tsb_alloc_hiwater to 1/tsb_alloc_hiwater_factor of physical 1170 * memory, which corresponds to the old static reserve for TSBs. 1171 * tsb_alloc_hiwater_factor defaults to 32. This caps the amount of 1172 * memory we'll allocate for TSB slabs; beyond this point TSB 1173 * allocations will be taken from the kernel heap (via 1174 * sfmmu_tsb8k_cache) and will be throttled as would any other kmem 1175 * consumer. 1176 */ 1177 if (tsb_alloc_hiwater_factor == 0) { 1178 tsb_alloc_hiwater_factor = TSB_ALLOC_HIWATER_FACTOR_DEFAULT; 1179 } 1180 SFMMU_SET_TSB_ALLOC_HIWATER(physmem); 1181 1182 for (sz = tsb_slab_ttesz; sz > 0; sz--) { 1183 if (!(disable_large_pages & (1 << sz))) 1184 break; 1185 } 1186 1187 if (sz < tsb_slab_ttesz) { 1188 tsb_slab_ttesz = sz; 1189 tsb_slab_shift = MMU_PAGESHIFT + (sz << 1) + sz; 1190 tsb_slab_size = 1 << tsb_slab_shift; 1191 tsb_slab_mask = (1 << (tsb_slab_shift - MMU_PAGESHIFT)) - 1; 1192 use_bigtsb_arena = 0; 1193 } else if (use_bigtsb_arena && 1194 (disable_large_pages & (1 << bigtsb_slab_ttesz))) { 1195 use_bigtsb_arena = 0; 1196 } 1197 1198 if (!use_bigtsb_arena) { 1199 bigtsb_slab_shift = tsb_slab_shift; 1200 } 1201 SFMMU_SET_TSB_MAX_GROWSIZE(physmem); 1202 1203 /* 1204 * On smaller memory systems, allocate TSB memory in smaller chunks 1205 * than the default 4M slab size. We also honor disable_large_pages 1206 * here. 1207 * 1208 * The trap handlers need to be patched with the final slab shift, 1209 * since they need to be able to construct the TSB pointer at runtime. 1210 */ 1211 if ((tsb_max_growsize <= TSB_512K_SZCODE) && 1212 !(disable_large_pages & (1 << TTE512K))) { 1213 tsb_slab_ttesz = TTE512K; 1214 tsb_slab_shift = MMU_PAGESHIFT512K; 1215 tsb_slab_size = MMU_PAGESIZE512K; 1216 tsb_slab_mask = MMU_PAGEOFFSET512K >> MMU_PAGESHIFT; 1217 use_bigtsb_arena = 0; 1218 } 1219 1220 if (!use_bigtsb_arena) { 1221 bigtsb_slab_ttesz = tsb_slab_ttesz; 1222 bigtsb_slab_shift = tsb_slab_shift; 1223 bigtsb_slab_size = tsb_slab_size; 1224 bigtsb_slab_mask = tsb_slab_mask; 1225 } 1226 1227 1228 /* 1229 * Set up memory callback to update tsb_alloc_hiwater and 1230 * tsb_max_growsize. 1231 */ 1232 i = kphysm_setup_func_register(&sfmmu_update_vec, (void *) 0); 1233 ASSERT(i == 0); 1234 1235 /* 1236 * kmem_tsb_arena is the source from which large TSB slabs are 1237 * drawn. The quantum of this arena corresponds to the largest 1238 * TSB size we can dynamically allocate for user processes. 1239 * Currently it must also be a supported page size since we 1240 * use exactly one translation entry to map each slab page. 1241 * 1242 * The per-lgroup kmem_tsb_default_arena arenas are the arenas from 1243 * which most TSBs are allocated. Since most TSB allocations are 1244 * typically 8K we have a kmem cache we stack on top of each 1245 * kmem_tsb_default_arena to speed up those allocations. 1246 * 1247 * Note the two-level scheme of arenas is required only 1248 * because vmem_create doesn't allow us to specify alignment 1249 * requirements. If this ever changes the code could be 1250 * simplified to use only one level of arenas. 1251 * 1252 * If 256M page support exists on sun4v, 256MB kmem_bigtsb_arena 1253 * will be provided in addition to the 4M kmem_tsb_arena. 1254 */ 1255 if (use_bigtsb_arena) { 1256 kmem_bigtsb_arena = vmem_create("kmem_bigtsb", NULL, 0, 1257 bigtsb_slab_size, sfmmu_vmem_xalloc_aligned_wrapper, 1258 vmem_xfree, heap_arena, 0, VM_SLEEP); 1259 } 1260 1261 kmem_tsb_arena = vmem_create("kmem_tsb", NULL, 0, tsb_slab_size, 1262 sfmmu_vmem_xalloc_aligned_wrapper, 1263 vmem_xfree, heap_arena, 0, VM_SLEEP); 1264 1265 if (tsb_lgrp_affinity) { 1266 char s[50]; 1267 for (i = 0; i < NLGRPS_MAX; i++) { 1268 if (use_bigtsb_arena) { 1269 (void) sprintf(s, "kmem_bigtsb_lgrp%d", i); 1270 kmem_bigtsb_default_arena[i] = vmem_create(s, 1271 NULL, 0, 2 * tsb_slab_size, 1272 sfmmu_tsb_segkmem_alloc, 1273 sfmmu_tsb_segkmem_free, kmem_bigtsb_arena, 1274 0, VM_SLEEP | VM_BESTFIT); 1275 } 1276 1277 (void) sprintf(s, "kmem_tsb_lgrp%d", i); 1278 kmem_tsb_default_arena[i] = vmem_create(s, 1279 NULL, 0, PAGESIZE, sfmmu_tsb_segkmem_alloc, 1280 sfmmu_tsb_segkmem_free, kmem_tsb_arena, 0, 1281 VM_SLEEP | VM_BESTFIT); 1282 1283 (void) sprintf(s, "sfmmu_tsb_lgrp%d_cache", i); 1284 sfmmu_tsb_cache[i] = kmem_cache_create(s, 1285 PAGESIZE, PAGESIZE, NULL, NULL, NULL, NULL, 1286 kmem_tsb_default_arena[i], 0); 1287 } 1288 } else { 1289 if (use_bigtsb_arena) { 1290 kmem_bigtsb_default_arena[0] = 1291 vmem_create("kmem_bigtsb_default", NULL, 0, 1292 2 * tsb_slab_size, sfmmu_tsb_segkmem_alloc, 1293 sfmmu_tsb_segkmem_free, kmem_bigtsb_arena, 0, 1294 VM_SLEEP | VM_BESTFIT); 1295 } 1296 1297 kmem_tsb_default_arena[0] = vmem_create("kmem_tsb_default", 1298 NULL, 0, PAGESIZE, sfmmu_tsb_segkmem_alloc, 1299 sfmmu_tsb_segkmem_free, kmem_tsb_arena, 0, 1300 VM_SLEEP | VM_BESTFIT); 1301 sfmmu_tsb_cache[0] = kmem_cache_create("sfmmu_tsb_cache", 1302 PAGESIZE, PAGESIZE, NULL, NULL, NULL, NULL, 1303 kmem_tsb_default_arena[0], 0); 1304 } 1305 1306 sfmmu8_cache = kmem_cache_create("sfmmu8_cache", HME8BLK_SZ, 1307 HMEBLK_ALIGN, sfmmu_hblkcache_constructor, 1308 sfmmu_hblkcache_destructor, 1309 sfmmu_hblkcache_reclaim, (void *)HME8BLK_SZ, 1310 hat_memload_arena, KMC_NOHASH); 1311 1312 hat_memload1_arena = vmem_create("hat_memload1", NULL, 0, PAGESIZE, 1313 segkmem_alloc_permanent, segkmem_free, heap_arena, 0, VM_SLEEP); 1314 1315 sfmmu1_cache = kmem_cache_create("sfmmu1_cache", HME1BLK_SZ, 1316 HMEBLK_ALIGN, sfmmu_hblkcache_constructor, 1317 sfmmu_hblkcache_destructor, 1318 NULL, (void *)HME1BLK_SZ, 1319 hat_memload1_arena, KMC_NOHASH); 1320 1321 pa_hment_cache = kmem_cache_create("pa_hment_cache", PAHME_SZ, 1322 0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH); 1323 1324 ism_blk_cache = kmem_cache_create("ism_blk_cache", 1325 sizeof (ism_blk_t), ecache_alignsize, NULL, NULL, 1326 NULL, NULL, static_arena, KMC_NOHASH); 1327 1328 ism_ment_cache = kmem_cache_create("ism_ment_cache", 1329 sizeof (ism_ment_t), 0, NULL, NULL, 1330 NULL, NULL, NULL, 0); 1331 1332 /* 1333 * We grab the first hat for the kernel, 1334 */ 1335 AS_LOCK_ENTER(&kas, &kas.a_lock, RW_WRITER); 1336 kas.a_hat = hat_alloc(&kas); 1337 AS_LOCK_EXIT(&kas, &kas.a_lock); 1338 1339 /* 1340 * Initialize hblk_reserve. 1341 */ 1342 ((struct hme_blk *)hblk_reserve)->hblk_nextpa = 1343 va_to_pa((caddr_t)hblk_reserve); 1344 1345 #ifndef UTSB_PHYS 1346 /* 1347 * Reserve some kernel virtual address space for the locked TTEs 1348 * that allow us to probe the TSB from TL>0. 1349 */ 1350 utsb_vabase = vmem_xalloc(heap_arena, tsb_slab_size, tsb_slab_size, 1351 0, 0, NULL, NULL, VM_SLEEP); 1352 utsb4m_vabase = vmem_xalloc(heap_arena, tsb_slab_size, tsb_slab_size, 1353 0, 0, NULL, NULL, VM_SLEEP); 1354 #endif 1355 1356 #ifdef VAC 1357 /* 1358 * The big page VAC handling code assumes VAC 1359 * will not be bigger than the smallest big 1360 * page- which is 64K. 1361 */ 1362 if (TTEPAGES(TTE64K) < CACHE_NUM_COLOR) { 1363 cmn_err(CE_PANIC, "VAC too big!"); 1364 } 1365 #endif 1366 1367 (void) xhat_init(); 1368 1369 uhme_hash_pa = va_to_pa(uhme_hash); 1370 khme_hash_pa = va_to_pa(khme_hash); 1371 1372 /* 1373 * Initialize relocation locks. kpr_suspendlock is held 1374 * at PIL_MAX to prevent interrupts from pinning the holder 1375 * of a suspended TTE which may access it leading to a 1376 * deadlock condition. 1377 */ 1378 mutex_init(&kpr_mutex, NULL, MUTEX_DEFAULT, NULL); 1379 mutex_init(&kpr_suspendlock, NULL, MUTEX_SPIN, (void *)PIL_MAX); 1380 1381 /* 1382 * If Shared context support is disabled via /etc/system 1383 * set shctx_on to 0 here if it was set to 1 earlier in boot 1384 * sequence by cpu module initialization code. 1385 */ 1386 if (shctx_on && disable_shctx) { 1387 shctx_on = 0; 1388 } 1389 1390 if (shctx_on) { 1391 srd_buckets = kmem_zalloc(SFMMU_MAX_SRD_BUCKETS * 1392 sizeof (srd_buckets[0]), KM_SLEEP); 1393 for (i = 0; i < SFMMU_MAX_SRD_BUCKETS; i++) { 1394 mutex_init(&srd_buckets[i].srdb_lock, NULL, 1395 MUTEX_DEFAULT, NULL); 1396 } 1397 1398 srd_cache = kmem_cache_create("srd_cache", sizeof (sf_srd_t), 1399 0, sfmmu_srdcache_constructor, sfmmu_srdcache_destructor, 1400 NULL, NULL, NULL, 0); 1401 region_cache = kmem_cache_create("region_cache", 1402 sizeof (sf_region_t), 0, sfmmu_rgncache_constructor, 1403 sfmmu_rgncache_destructor, NULL, NULL, NULL, 0); 1404 scd_cache = kmem_cache_create("scd_cache", sizeof (sf_scd_t), 1405 0, sfmmu_scdcache_constructor, sfmmu_scdcache_destructor, 1406 NULL, NULL, NULL, 0); 1407 } 1408 1409 /* 1410 * Pre-allocate hrm_hashtab before enabling the collection of 1411 * refmod statistics. Allocating on the fly would mean us 1412 * running the risk of suffering recursive mutex enters or 1413 * deadlocks. 1414 */ 1415 hrm_hashtab = kmem_zalloc(HRM_HASHSIZE * sizeof (struct hrmstat *), 1416 KM_SLEEP); 1417 } 1418 1419 /* 1420 * Initialize locking for the hat layer, called early during boot. 1421 */ 1422 static void 1423 hat_lock_init() 1424 { 1425 int i; 1426 1427 /* 1428 * initialize the array of mutexes protecting a page's mapping 1429 * list and p_nrm field. 1430 */ 1431 for (i = 0; i < mml_table_sz; i++) 1432 mutex_init(&mml_table[i], NULL, MUTEX_DEFAULT, NULL); 1433 1434 if (kpm_enable) { 1435 for (i = 0; i < kpmp_table_sz; i++) { 1436 mutex_init(&kpmp_table[i].khl_mutex, NULL, 1437 MUTEX_DEFAULT, NULL); 1438 } 1439 } 1440 1441 /* 1442 * Initialize array of mutex locks that protects sfmmu fields and 1443 * TSB lists. 1444 */ 1445 for (i = 0; i < SFMMU_NUM_LOCK; i++) 1446 mutex_init(HATLOCK_MUTEXP(&hat_lock[i]), NULL, MUTEX_DEFAULT, 1447 NULL); 1448 } 1449 1450 #define SFMMU_KERNEL_MAXVA \ 1451 (kmem64_base ? (uintptr_t)kmem64_end : (SYSLIMIT)) 1452 1453 /* 1454 * Allocate a hat structure. 1455 * Called when an address space first uses a hat. 1456 */ 1457 struct hat * 1458 hat_alloc(struct as *as) 1459 { 1460 sfmmu_t *sfmmup; 1461 int i; 1462 uint64_t cnum; 1463 extern uint_t get_color_start(struct as *); 1464 1465 ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 1466 sfmmup = kmem_cache_alloc(sfmmuid_cache, KM_SLEEP); 1467 sfmmup->sfmmu_as = as; 1468 sfmmup->sfmmu_flags = 0; 1469 sfmmup->sfmmu_tteflags = 0; 1470 sfmmup->sfmmu_rtteflags = 0; 1471 LOCK_INIT_CLEAR(&sfmmup->sfmmu_ctx_lock); 1472 1473 if (as == &kas) { 1474 ksfmmup = sfmmup; 1475 sfmmup->sfmmu_cext = 0; 1476 cnum = KCONTEXT; 1477 1478 sfmmup->sfmmu_clrstart = 0; 1479 sfmmup->sfmmu_tsb = NULL; 1480 /* 1481 * hat_kern_setup() will call sfmmu_init_ktsbinfo() 1482 * to setup tsb_info for ksfmmup. 1483 */ 1484 } else { 1485 1486 /* 1487 * Just set to invalid ctx. When it faults, it will 1488 * get a valid ctx. This would avoid the situation 1489 * where we get a ctx, but it gets stolen and then 1490 * we fault when we try to run and so have to get 1491 * another ctx. 1492 */ 1493 sfmmup->sfmmu_cext = 0; 1494 cnum = INVALID_CONTEXT; 1495 1496 /* initialize original physical page coloring bin */ 1497 sfmmup->sfmmu_clrstart = get_color_start(as); 1498 #ifdef DEBUG 1499 if (tsb_random_size) { 1500 uint32_t randval = (uint32_t)gettick() >> 4; 1501 int size = randval % (tsb_max_growsize + 1); 1502 1503 /* chose a random tsb size for stress testing */ 1504 (void) sfmmu_tsbinfo_alloc(&sfmmup->sfmmu_tsb, size, 1505 TSB8K|TSB64K|TSB512K, 0, sfmmup); 1506 } else 1507 #endif /* DEBUG */ 1508 (void) sfmmu_tsbinfo_alloc(&sfmmup->sfmmu_tsb, 1509 default_tsb_size, 1510 TSB8K|TSB64K|TSB512K, 0, sfmmup); 1511 sfmmup->sfmmu_flags = HAT_SWAPPED | HAT_ALLCTX_INVALID; 1512 ASSERT(sfmmup->sfmmu_tsb != NULL); 1513 } 1514 1515 ASSERT(max_mmu_ctxdoms > 0); 1516 for (i = 0; i < max_mmu_ctxdoms; i++) { 1517 sfmmup->sfmmu_ctxs[i].cnum = cnum; 1518 sfmmup->sfmmu_ctxs[i].gnum = 0; 1519 } 1520 1521 for (i = 0; i < max_mmu_page_sizes; i++) { 1522 sfmmup->sfmmu_ttecnt[i] = 0; 1523 sfmmup->sfmmu_scdrttecnt[i] = 0; 1524 sfmmup->sfmmu_ismttecnt[i] = 0; 1525 sfmmup->sfmmu_scdismttecnt[i] = 0; 1526 sfmmup->sfmmu_pgsz[i] = TTE8K; 1527 } 1528 sfmmup->sfmmu_tsb0_4minflcnt = 0; 1529 sfmmup->sfmmu_iblk = NULL; 1530 sfmmup->sfmmu_ismhat = 0; 1531 sfmmup->sfmmu_scdhat = 0; 1532 sfmmup->sfmmu_ismblkpa = (uint64_t)-1; 1533 if (sfmmup == ksfmmup) { 1534 CPUSET_ALL(sfmmup->sfmmu_cpusran); 1535 } else { 1536 CPUSET_ZERO(sfmmup->sfmmu_cpusran); 1537 } 1538 sfmmup->sfmmu_free = 0; 1539 sfmmup->sfmmu_rmstat = 0; 1540 sfmmup->sfmmu_clrbin = sfmmup->sfmmu_clrstart; 1541 sfmmup->sfmmu_xhat_provider = NULL; 1542 cv_init(&sfmmup->sfmmu_tsb_cv, NULL, CV_DEFAULT, NULL); 1543 sfmmup->sfmmu_srdp = NULL; 1544 SF_RGNMAP_ZERO(sfmmup->sfmmu_region_map); 1545 bzero(sfmmup->sfmmu_hmeregion_links, SFMMU_L1_HMERLINKS_SIZE); 1546 sfmmup->sfmmu_scdp = NULL; 1547 sfmmup->sfmmu_scd_link.next = NULL; 1548 sfmmup->sfmmu_scd_link.prev = NULL; 1549 return (sfmmup); 1550 } 1551 1552 /* 1553 * Create per-MMU context domain kstats for a given MMU ctx. 1554 */ 1555 static void 1556 sfmmu_mmu_kstat_create(mmu_ctx_t *mmu_ctxp) 1557 { 1558 mmu_ctx_stat_t stat; 1559 kstat_t *mmu_kstat; 1560 1561 ASSERT(MUTEX_HELD(&cpu_lock)); 1562 ASSERT(mmu_ctxp->mmu_kstat == NULL); 1563 1564 mmu_kstat = kstat_create("unix", mmu_ctxp->mmu_idx, "mmu_ctx", 1565 "hat", KSTAT_TYPE_NAMED, MMU_CTX_NUM_STATS, KSTAT_FLAG_VIRTUAL); 1566 1567 if (mmu_kstat == NULL) { 1568 cmn_err(CE_WARN, "kstat_create for MMU %d failed", 1569 mmu_ctxp->mmu_idx); 1570 } else { 1571 mmu_kstat->ks_data = mmu_ctxp->mmu_kstat_data; 1572 for (stat = 0; stat < MMU_CTX_NUM_STATS; stat++) 1573 kstat_named_init(&mmu_ctxp->mmu_kstat_data[stat], 1574 mmu_ctx_kstat_names[stat], KSTAT_DATA_INT64); 1575 mmu_ctxp->mmu_kstat = mmu_kstat; 1576 kstat_install(mmu_kstat); 1577 } 1578 } 1579 1580 /* 1581 * plat_cpuid_to_mmu_ctx_info() is a platform interface that returns MMU 1582 * context domain information for a given CPU. If a platform does not 1583 * specify that interface, then the function below is used instead to return 1584 * default information. The defaults are as follows: 1585 * 1586 * - For sun4u systems there's one MMU context domain per CPU. 1587 * This default is used by all sun4u systems except OPL. OPL systems 1588 * provide platform specific interface to map CPU ids to MMU ids 1589 * because on OPL more than 1 CPU shares a single MMU. 1590 * Note that on sun4v, there is one global context domain for 1591 * the entire system. This is to avoid running into potential problem 1592 * with ldom physical cpu substitution feature. 1593 * - The number of MMU context IDs supported on any CPU in the 1594 * system is 8K. 1595 */ 1596 /*ARGSUSED*/ 1597 static void 1598 sfmmu_cpuid_to_mmu_ctx_info(processorid_t cpuid, mmu_ctx_info_t *infop) 1599 { 1600 infop->mmu_nctxs = nctxs; 1601 #ifndef sun4v 1602 infop->mmu_idx = cpu[cpuid]->cpu_seqid; 1603 #else /* sun4v */ 1604 infop->mmu_idx = 0; 1605 #endif /* sun4v */ 1606 } 1607 1608 /* 1609 * Called during CPU initialization to set the MMU context-related information 1610 * for a CPU. 1611 * 1612 * cpu_lock serializes accesses to mmu_ctxs and mmu_saved_gnum. 1613 */ 1614 void 1615 sfmmu_cpu_init(cpu_t *cp) 1616 { 1617 mmu_ctx_info_t info; 1618 mmu_ctx_t *mmu_ctxp; 1619 1620 ASSERT(MUTEX_HELD(&cpu_lock)); 1621 1622 if (&plat_cpuid_to_mmu_ctx_info == NULL) 1623 sfmmu_cpuid_to_mmu_ctx_info(cp->cpu_id, &info); 1624 else 1625 plat_cpuid_to_mmu_ctx_info(cp->cpu_id, &info); 1626 1627 ASSERT(info.mmu_idx < max_mmu_ctxdoms); 1628 1629 if ((mmu_ctxp = mmu_ctxs_tbl[info.mmu_idx]) == NULL) { 1630 /* Each mmu_ctx is cacheline aligned. */ 1631 mmu_ctxp = kmem_cache_alloc(mmuctxdom_cache, KM_SLEEP); 1632 bzero(mmu_ctxp, sizeof (mmu_ctx_t)); 1633 1634 mutex_init(&mmu_ctxp->mmu_lock, NULL, MUTEX_SPIN, 1635 (void *)ipltospl(DISP_LEVEL)); 1636 mmu_ctxp->mmu_idx = info.mmu_idx; 1637 mmu_ctxp->mmu_nctxs = info.mmu_nctxs; 1638 /* 1639 * Globally for lifetime of a system, 1640 * gnum must always increase. 1641 * mmu_saved_gnum is protected by the cpu_lock. 1642 */ 1643 mmu_ctxp->mmu_gnum = mmu_saved_gnum + 1; 1644 mmu_ctxp->mmu_cnum = NUM_LOCKED_CTXS; 1645 1646 sfmmu_mmu_kstat_create(mmu_ctxp); 1647 1648 mmu_ctxs_tbl[info.mmu_idx] = mmu_ctxp; 1649 } else { 1650 ASSERT(mmu_ctxp->mmu_idx == info.mmu_idx); 1651 } 1652 1653 /* 1654 * The mmu_lock is acquired here to prevent races with 1655 * the wrap-around code. 1656 */ 1657 mutex_enter(&mmu_ctxp->mmu_lock); 1658 1659 1660 mmu_ctxp->mmu_ncpus++; 1661 CPUSET_ADD(mmu_ctxp->mmu_cpuset, cp->cpu_id); 1662 CPU_MMU_IDX(cp) = info.mmu_idx; 1663 CPU_MMU_CTXP(cp) = mmu_ctxp; 1664 1665 mutex_exit(&mmu_ctxp->mmu_lock); 1666 } 1667 1668 /* 1669 * Called to perform MMU context-related cleanup for a CPU. 1670 */ 1671 void 1672 sfmmu_cpu_cleanup(cpu_t *cp) 1673 { 1674 mmu_ctx_t *mmu_ctxp; 1675 1676 ASSERT(MUTEX_HELD(&cpu_lock)); 1677 1678 mmu_ctxp = CPU_MMU_CTXP(cp); 1679 ASSERT(mmu_ctxp != NULL); 1680 1681 /* 1682 * The mmu_lock is acquired here to prevent races with 1683 * the wrap-around code. 1684 */ 1685 mutex_enter(&mmu_ctxp->mmu_lock); 1686 1687 CPU_MMU_CTXP(cp) = NULL; 1688 1689 CPUSET_DEL(mmu_ctxp->mmu_cpuset, cp->cpu_id); 1690 if (--mmu_ctxp->mmu_ncpus == 0) { 1691 mmu_ctxs_tbl[mmu_ctxp->mmu_idx] = NULL; 1692 mutex_exit(&mmu_ctxp->mmu_lock); 1693 mutex_destroy(&mmu_ctxp->mmu_lock); 1694 1695 if (mmu_ctxp->mmu_kstat) 1696 kstat_delete(mmu_ctxp->mmu_kstat); 1697 1698 /* mmu_saved_gnum is protected by the cpu_lock. */ 1699 if (mmu_saved_gnum < mmu_ctxp->mmu_gnum) 1700 mmu_saved_gnum = mmu_ctxp->mmu_gnum; 1701 1702 kmem_cache_free(mmuctxdom_cache, mmu_ctxp); 1703 1704 return; 1705 } 1706 1707 mutex_exit(&mmu_ctxp->mmu_lock); 1708 } 1709 1710 /* 1711 * Hat_setup, makes an address space context the current active one. 1712 * In sfmmu this translates to setting the secondary context with the 1713 * corresponding context. 1714 */ 1715 void 1716 hat_setup(struct hat *sfmmup, int allocflag) 1717 { 1718 hatlock_t *hatlockp; 1719 1720 /* Init needs some special treatment. */ 1721 if (allocflag == HAT_INIT) { 1722 /* 1723 * Make sure that we have 1724 * 1. a TSB 1725 * 2. a valid ctx that doesn't get stolen after this point. 1726 */ 1727 hatlockp = sfmmu_hat_enter(sfmmup); 1728 1729 /* 1730 * Swap in the TSB. hat_init() allocates tsbinfos without 1731 * TSBs, but we need one for init, since the kernel does some 1732 * special things to set up its stack and needs the TSB to 1733 * resolve page faults. 1734 */ 1735 sfmmu_tsb_swapin(sfmmup, hatlockp); 1736 1737 sfmmu_get_ctx(sfmmup); 1738 1739 sfmmu_hat_exit(hatlockp); 1740 } else { 1741 ASSERT(allocflag == HAT_ALLOC); 1742 1743 hatlockp = sfmmu_hat_enter(sfmmup); 1744 kpreempt_disable(); 1745 1746 CPUSET_ADD(sfmmup->sfmmu_cpusran, CPU->cpu_id); 1747 /* 1748 * sfmmu_setctx_sec takes <pgsz|cnum> as a parameter, 1749 * pagesize bits don't matter in this case since we are passing 1750 * INVALID_CONTEXT to it. 1751 * Compatibility Note: hw takes care of MMU_SCONTEXT1 1752 */ 1753 sfmmu_setctx_sec(INVALID_CONTEXT); 1754 sfmmu_clear_utsbinfo(); 1755 1756 kpreempt_enable(); 1757 sfmmu_hat_exit(hatlockp); 1758 } 1759 } 1760 1761 /* 1762 * Free all the translation resources for the specified address space. 1763 * Called from as_free when an address space is being destroyed. 1764 */ 1765 void 1766 hat_free_start(struct hat *sfmmup) 1767 { 1768 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 1769 ASSERT(sfmmup != ksfmmup); 1770 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 1771 1772 sfmmup->sfmmu_free = 1; 1773 if (sfmmup->sfmmu_scdp != NULL) { 1774 sfmmu_leave_scd(sfmmup, 0); 1775 } 1776 1777 ASSERT(sfmmup->sfmmu_scdp == NULL); 1778 } 1779 1780 void 1781 hat_free_end(struct hat *sfmmup) 1782 { 1783 int i; 1784 1785 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 1786 ASSERT(sfmmup->sfmmu_free == 1); 1787 ASSERT(sfmmup->sfmmu_ttecnt[TTE8K] == 0); 1788 ASSERT(sfmmup->sfmmu_ttecnt[TTE64K] == 0); 1789 ASSERT(sfmmup->sfmmu_ttecnt[TTE512K] == 0); 1790 ASSERT(sfmmup->sfmmu_ttecnt[TTE4M] == 0); 1791 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0); 1792 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0); 1793 1794 if (sfmmup->sfmmu_rmstat) { 1795 hat_freestat(sfmmup->sfmmu_as, NULL); 1796 } 1797 1798 while (sfmmup->sfmmu_tsb != NULL) { 1799 struct tsb_info *next = sfmmup->sfmmu_tsb->tsb_next; 1800 sfmmu_tsbinfo_free(sfmmup->sfmmu_tsb); 1801 sfmmup->sfmmu_tsb = next; 1802 } 1803 1804 if (sfmmup->sfmmu_srdp != NULL) { 1805 sfmmu_leave_srd(sfmmup); 1806 ASSERT(sfmmup->sfmmu_srdp == NULL); 1807 for (i = 0; i < SFMMU_L1_HMERLINKS; i++) { 1808 if (sfmmup->sfmmu_hmeregion_links[i] != NULL) { 1809 kmem_free(sfmmup->sfmmu_hmeregion_links[i], 1810 SFMMU_L2_HMERLINKS_SIZE); 1811 sfmmup->sfmmu_hmeregion_links[i] = NULL; 1812 } 1813 } 1814 } 1815 sfmmu_free_sfmmu(sfmmup); 1816 1817 #ifdef DEBUG 1818 for (i = 0; i < SFMMU_L1_HMERLINKS; i++) { 1819 ASSERT(sfmmup->sfmmu_hmeregion_links[i] == NULL); 1820 } 1821 #endif 1822 1823 kmem_cache_free(sfmmuid_cache, sfmmup); 1824 } 1825 1826 /* 1827 * Set up any translation structures, for the specified address space, 1828 * that are needed or preferred when the process is being swapped in. 1829 */ 1830 /* ARGSUSED */ 1831 void 1832 hat_swapin(struct hat *hat) 1833 { 1834 ASSERT(hat->sfmmu_xhat_provider == NULL); 1835 } 1836 1837 /* 1838 * Free all of the translation resources, for the specified address space, 1839 * that can be freed while the process is swapped out. Called from as_swapout. 1840 * Also, free up the ctx that this process was using. 1841 */ 1842 void 1843 hat_swapout(struct hat *sfmmup) 1844 { 1845 struct hmehash_bucket *hmebp; 1846 struct hme_blk *hmeblkp; 1847 struct hme_blk *pr_hblk = NULL; 1848 struct hme_blk *nx_hblk; 1849 int i; 1850 uint64_t hblkpa, prevpa, nx_pa; 1851 struct hme_blk *list = NULL; 1852 hatlock_t *hatlockp; 1853 struct tsb_info *tsbinfop; 1854 struct free_tsb { 1855 struct free_tsb *next; 1856 struct tsb_info *tsbinfop; 1857 }; /* free list of TSBs */ 1858 struct free_tsb *freelist, *last, *next; 1859 1860 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 1861 SFMMU_STAT(sf_swapout); 1862 1863 /* 1864 * There is no way to go from an as to all its translations in sfmmu. 1865 * Here is one of the times when we take the big hit and traverse 1866 * the hash looking for hme_blks to free up. Not only do we free up 1867 * this as hme_blks but all those that are free. We are obviously 1868 * swapping because we need memory so let's free up as much 1869 * as we can. 1870 * 1871 * Note that we don't flush TLB/TSB here -- it's not necessary 1872 * because: 1873 * 1) we free the ctx we're using and throw away the TSB(s); 1874 * 2) processes aren't runnable while being swapped out. 1875 */ 1876 ASSERT(sfmmup != KHATID); 1877 for (i = 0; i <= UHMEHASH_SZ; i++) { 1878 hmebp = &uhme_hash[i]; 1879 SFMMU_HASH_LOCK(hmebp); 1880 hmeblkp = hmebp->hmeblkp; 1881 hblkpa = hmebp->hmeh_nextpa; 1882 prevpa = 0; 1883 pr_hblk = NULL; 1884 while (hmeblkp) { 1885 1886 ASSERT(!hmeblkp->hblk_xhat_bit); 1887 1888 if ((hmeblkp->hblk_tag.htag_id == sfmmup) && 1889 !hmeblkp->hblk_shw_bit && !hmeblkp->hblk_lckcnt) { 1890 ASSERT(!hmeblkp->hblk_shared); 1891 (void) sfmmu_hblk_unload(sfmmup, hmeblkp, 1892 (caddr_t)get_hblk_base(hmeblkp), 1893 get_hblk_endaddr(hmeblkp), 1894 NULL, HAT_UNLOAD); 1895 } 1896 nx_hblk = hmeblkp->hblk_next; 1897 nx_pa = hmeblkp->hblk_nextpa; 1898 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) { 1899 ASSERT(!hmeblkp->hblk_lckcnt); 1900 sfmmu_hblk_hash_rm(hmebp, hmeblkp, 1901 prevpa, pr_hblk); 1902 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 1903 } else { 1904 pr_hblk = hmeblkp; 1905 prevpa = hblkpa; 1906 } 1907 hmeblkp = nx_hblk; 1908 hblkpa = nx_pa; 1909 } 1910 SFMMU_HASH_UNLOCK(hmebp); 1911 } 1912 1913 sfmmu_hblks_list_purge(&list); 1914 1915 /* 1916 * Now free up the ctx so that others can reuse it. 1917 */ 1918 hatlockp = sfmmu_hat_enter(sfmmup); 1919 1920 sfmmu_invalidate_ctx(sfmmup); 1921 1922 /* 1923 * Free TSBs, but not tsbinfos, and set SWAPPED flag. 1924 * If TSBs were never swapped in, just return. 1925 * This implies that we don't support partial swapping 1926 * of TSBs -- either all are swapped out, or none are. 1927 * 1928 * We must hold the HAT lock here to prevent racing with another 1929 * thread trying to unmap TTEs from the TSB or running the post- 1930 * relocator after relocating the TSB's memory. Unfortunately, we 1931 * can't free memory while holding the HAT lock or we could 1932 * deadlock, so we build a list of TSBs to be freed after marking 1933 * the tsbinfos as swapped out and free them after dropping the 1934 * lock. 1935 */ 1936 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) { 1937 sfmmu_hat_exit(hatlockp); 1938 return; 1939 } 1940 1941 SFMMU_FLAGS_SET(sfmmup, HAT_SWAPPED); 1942 last = freelist = NULL; 1943 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL; 1944 tsbinfop = tsbinfop->tsb_next) { 1945 ASSERT((tsbinfop->tsb_flags & TSB_SWAPPED) == 0); 1946 1947 /* 1948 * Cast the TSB into a struct free_tsb and put it on the free 1949 * list. 1950 */ 1951 if (freelist == NULL) { 1952 last = freelist = (struct free_tsb *)tsbinfop->tsb_va; 1953 } else { 1954 last->next = (struct free_tsb *)tsbinfop->tsb_va; 1955 last = last->next; 1956 } 1957 last->next = NULL; 1958 last->tsbinfop = tsbinfop; 1959 tsbinfop->tsb_flags |= TSB_SWAPPED; 1960 /* 1961 * Zero out the TTE to clear the valid bit. 1962 * Note we can't use a value like 0xbad because we want to 1963 * ensure diagnostic bits are NEVER set on TTEs that might 1964 * be loaded. The intent is to catch any invalid access 1965 * to the swapped TSB, such as a thread running with a valid 1966 * context without first calling sfmmu_tsb_swapin() to 1967 * allocate TSB memory. 1968 */ 1969 tsbinfop->tsb_tte.ll = 0; 1970 } 1971 1972 /* Now we can drop the lock and free the TSB memory. */ 1973 sfmmu_hat_exit(hatlockp); 1974 for (; freelist != NULL; freelist = next) { 1975 next = freelist->next; 1976 sfmmu_tsb_free(freelist->tsbinfop); 1977 } 1978 } 1979 1980 /* 1981 * Duplicate the translations of an as into another newas 1982 */ 1983 /* ARGSUSED */ 1984 int 1985 hat_dup(struct hat *hat, struct hat *newhat, caddr_t addr, size_t len, 1986 uint_t flag) 1987 { 1988 sf_srd_t *srdp; 1989 sf_scd_t *scdp; 1990 int i; 1991 extern uint_t get_color_start(struct as *); 1992 1993 ASSERT(hat->sfmmu_xhat_provider == NULL); 1994 ASSERT((flag == 0) || (flag == HAT_DUP_ALL) || (flag == HAT_DUP_COW) || 1995 (flag == HAT_DUP_SRD)); 1996 ASSERT(hat != ksfmmup); 1997 ASSERT(newhat != ksfmmup); 1998 ASSERT(flag != HAT_DUP_ALL || hat->sfmmu_srdp == newhat->sfmmu_srdp); 1999 2000 if (flag == HAT_DUP_COW) { 2001 panic("hat_dup: HAT_DUP_COW not supported"); 2002 } 2003 2004 if (flag == HAT_DUP_SRD && ((srdp = hat->sfmmu_srdp) != NULL)) { 2005 ASSERT(srdp->srd_evp != NULL); 2006 VN_HOLD(srdp->srd_evp); 2007 ASSERT(srdp->srd_refcnt > 0); 2008 newhat->sfmmu_srdp = srdp; 2009 atomic_add_32((volatile uint_t *)&srdp->srd_refcnt, 1); 2010 } 2011 2012 /* 2013 * HAT_DUP_ALL flag is used after as duplication is done. 2014 */ 2015 if (flag == HAT_DUP_ALL && ((srdp = newhat->sfmmu_srdp) != NULL)) { 2016 ASSERT(newhat->sfmmu_srdp->srd_refcnt >= 2); 2017 newhat->sfmmu_rtteflags = hat->sfmmu_rtteflags; 2018 if (hat->sfmmu_flags & HAT_4MTEXT_FLAG) { 2019 newhat->sfmmu_flags |= HAT_4MTEXT_FLAG; 2020 } 2021 2022 /* check if need to join scd */ 2023 if ((scdp = hat->sfmmu_scdp) != NULL && 2024 newhat->sfmmu_scdp != scdp) { 2025 int ret; 2026 SF_RGNMAP_IS_SUBSET(&newhat->sfmmu_region_map, 2027 &scdp->scd_region_map, ret); 2028 ASSERT(ret); 2029 sfmmu_join_scd(scdp, newhat); 2030 ASSERT(newhat->sfmmu_scdp == scdp && 2031 scdp->scd_refcnt >= 2); 2032 for (i = 0; i < max_mmu_page_sizes; i++) { 2033 newhat->sfmmu_ismttecnt[i] = 2034 hat->sfmmu_ismttecnt[i]; 2035 newhat->sfmmu_scdismttecnt[i] = 2036 hat->sfmmu_scdismttecnt[i]; 2037 } 2038 } 2039 2040 sfmmu_check_page_sizes(newhat, 1); 2041 } 2042 2043 if (flag == HAT_DUP_ALL && consistent_coloring == 0 && 2044 update_proc_pgcolorbase_after_fork != 0) { 2045 hat->sfmmu_clrbin = get_color_start(hat->sfmmu_as); 2046 } 2047 return (0); 2048 } 2049 2050 void 2051 hat_memload(struct hat *hat, caddr_t addr, struct page *pp, 2052 uint_t attr, uint_t flags) 2053 { 2054 hat_do_memload(hat, addr, pp, attr, flags, 2055 SFMMU_INVALID_SHMERID); 2056 } 2057 2058 void 2059 hat_memload_region(struct hat *hat, caddr_t addr, struct page *pp, 2060 uint_t attr, uint_t flags, hat_region_cookie_t rcookie) 2061 { 2062 uint_t rid; 2063 if (rcookie == HAT_INVALID_REGION_COOKIE || 2064 hat->sfmmu_xhat_provider != NULL) { 2065 hat_do_memload(hat, addr, pp, attr, flags, 2066 SFMMU_INVALID_SHMERID); 2067 return; 2068 } 2069 rid = (uint_t)((uint64_t)rcookie); 2070 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 2071 hat_do_memload(hat, addr, pp, attr, flags, rid); 2072 } 2073 2074 /* 2075 * Set up addr to map to page pp with protection prot. 2076 * As an optimization we also load the TSB with the 2077 * corresponding tte but it is no big deal if the tte gets kicked out. 2078 */ 2079 static void 2080 hat_do_memload(struct hat *hat, caddr_t addr, struct page *pp, 2081 uint_t attr, uint_t flags, uint_t rid) 2082 { 2083 tte_t tte; 2084 2085 2086 ASSERT(hat != NULL); 2087 ASSERT(PAGE_LOCKED(pp)); 2088 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET)); 2089 ASSERT(!(flags & ~SFMMU_LOAD_ALLFLAG)); 2090 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR)); 2091 SFMMU_VALIDATE_HMERID(hat, rid, addr, MMU_PAGESIZE); 2092 2093 if (PP_ISFREE(pp)) { 2094 panic("hat_memload: loading a mapping to free page %p", 2095 (void *)pp); 2096 } 2097 2098 if (hat->sfmmu_xhat_provider) { 2099 /* no regions for xhats */ 2100 ASSERT(!SFMMU_IS_SHMERID_VALID(rid)); 2101 XHAT_MEMLOAD(hat, addr, pp, attr, flags); 2102 return; 2103 } 2104 2105 ASSERT((hat == ksfmmup) || 2106 AS_LOCK_HELD(hat->sfmmu_as, &hat->sfmmu_as->a_lock)); 2107 2108 if (flags & ~SFMMU_LOAD_ALLFLAG) 2109 cmn_err(CE_NOTE, "hat_memload: unsupported flags %d", 2110 flags & ~SFMMU_LOAD_ALLFLAG); 2111 2112 if (hat->sfmmu_rmstat) 2113 hat_resvstat(MMU_PAGESIZE, hat->sfmmu_as, addr); 2114 2115 #if defined(SF_ERRATA_57) 2116 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) && 2117 (addr < errata57_limit) && (attr & PROT_EXEC) && 2118 !(flags & HAT_LOAD_SHARE)) { 2119 cmn_err(CE_WARN, "hat_memload: illegal attempt to make user " 2120 " page executable"); 2121 attr &= ~PROT_EXEC; 2122 } 2123 #endif 2124 2125 sfmmu_memtte(&tte, pp->p_pagenum, attr, TTE8K); 2126 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, flags, rid); 2127 2128 /* 2129 * Check TSB and TLB page sizes. 2130 */ 2131 if ((flags & HAT_LOAD_SHARE) == 0) { 2132 sfmmu_check_page_sizes(hat, 1); 2133 } 2134 } 2135 2136 /* 2137 * hat_devload can be called to map real memory (e.g. 2138 * /dev/kmem) and even though hat_devload will determine pf is 2139 * for memory, it will be unable to get a shared lock on the 2140 * page (because someone else has it exclusively) and will 2141 * pass dp = NULL. If tteload doesn't get a non-NULL 2142 * page pointer it can't cache memory. 2143 */ 2144 void 2145 hat_devload(struct hat *hat, caddr_t addr, size_t len, pfn_t pfn, 2146 uint_t attr, int flags) 2147 { 2148 tte_t tte; 2149 struct page *pp = NULL; 2150 int use_lgpg = 0; 2151 2152 ASSERT(hat != NULL); 2153 2154 if (hat->sfmmu_xhat_provider) { 2155 XHAT_DEVLOAD(hat, addr, len, pfn, attr, flags); 2156 return; 2157 } 2158 2159 ASSERT(!(flags & ~SFMMU_LOAD_ALLFLAG)); 2160 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR)); 2161 ASSERT((hat == ksfmmup) || 2162 AS_LOCK_HELD(hat->sfmmu_as, &hat->sfmmu_as->a_lock)); 2163 if (len == 0) 2164 panic("hat_devload: zero len"); 2165 if (flags & ~SFMMU_LOAD_ALLFLAG) 2166 cmn_err(CE_NOTE, "hat_devload: unsupported flags %d", 2167 flags & ~SFMMU_LOAD_ALLFLAG); 2168 2169 #if defined(SF_ERRATA_57) 2170 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) && 2171 (addr < errata57_limit) && (attr & PROT_EXEC) && 2172 !(flags & HAT_LOAD_SHARE)) { 2173 cmn_err(CE_WARN, "hat_devload: illegal attempt to make user " 2174 " page executable"); 2175 attr &= ~PROT_EXEC; 2176 } 2177 #endif 2178 2179 /* 2180 * If it's a memory page find its pp 2181 */ 2182 if (!(flags & HAT_LOAD_NOCONSIST) && pf_is_memory(pfn)) { 2183 pp = page_numtopp_nolock(pfn); 2184 if (pp == NULL) { 2185 flags |= HAT_LOAD_NOCONSIST; 2186 } else { 2187 if (PP_ISFREE(pp)) { 2188 panic("hat_memload: loading " 2189 "a mapping to free page %p", 2190 (void *)pp); 2191 } 2192 if (!PAGE_LOCKED(pp) && !PP_ISNORELOC(pp)) { 2193 panic("hat_memload: loading a mapping " 2194 "to unlocked relocatable page %p", 2195 (void *)pp); 2196 } 2197 ASSERT(len == MMU_PAGESIZE); 2198 } 2199 } 2200 2201 if (hat->sfmmu_rmstat) 2202 hat_resvstat(len, hat->sfmmu_as, addr); 2203 2204 if (flags & HAT_LOAD_NOCONSIST) { 2205 attr |= SFMMU_UNCACHEVTTE; 2206 use_lgpg = 1; 2207 } 2208 if (!pf_is_memory(pfn)) { 2209 attr |= SFMMU_UNCACHEPTTE | HAT_NOSYNC; 2210 use_lgpg = 1; 2211 switch (attr & HAT_ORDER_MASK) { 2212 case HAT_STRICTORDER: 2213 case HAT_UNORDERED_OK: 2214 /* 2215 * we set the side effect bit for all non 2216 * memory mappings unless merging is ok 2217 */ 2218 attr |= SFMMU_SIDEFFECT; 2219 break; 2220 case HAT_MERGING_OK: 2221 case HAT_LOADCACHING_OK: 2222 case HAT_STORECACHING_OK: 2223 break; 2224 default: 2225 panic("hat_devload: bad attr"); 2226 break; 2227 } 2228 } 2229 while (len) { 2230 if (!use_lgpg) { 2231 sfmmu_memtte(&tte, pfn, attr, TTE8K); 2232 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, 2233 flags, SFMMU_INVALID_SHMERID); 2234 len -= MMU_PAGESIZE; 2235 addr += MMU_PAGESIZE; 2236 pfn++; 2237 continue; 2238 } 2239 /* 2240 * try to use large pages, check va/pa alignments 2241 * Note that 32M/256M page sizes are not (yet) supported. 2242 */ 2243 if ((len >= MMU_PAGESIZE4M) && 2244 !((uintptr_t)addr & MMU_PAGEOFFSET4M) && 2245 !(disable_large_pages & (1 << TTE4M)) && 2246 !(mmu_ptob(pfn) & MMU_PAGEOFFSET4M)) { 2247 sfmmu_memtte(&tte, pfn, attr, TTE4M); 2248 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, 2249 flags, SFMMU_INVALID_SHMERID); 2250 len -= MMU_PAGESIZE4M; 2251 addr += MMU_PAGESIZE4M; 2252 pfn += MMU_PAGESIZE4M / MMU_PAGESIZE; 2253 } else if ((len >= MMU_PAGESIZE512K) && 2254 !((uintptr_t)addr & MMU_PAGEOFFSET512K) && 2255 !(disable_large_pages & (1 << TTE512K)) && 2256 !(mmu_ptob(pfn) & MMU_PAGEOFFSET512K)) { 2257 sfmmu_memtte(&tte, pfn, attr, TTE512K); 2258 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, 2259 flags, SFMMU_INVALID_SHMERID); 2260 len -= MMU_PAGESIZE512K; 2261 addr += MMU_PAGESIZE512K; 2262 pfn += MMU_PAGESIZE512K / MMU_PAGESIZE; 2263 } else if ((len >= MMU_PAGESIZE64K) && 2264 !((uintptr_t)addr & MMU_PAGEOFFSET64K) && 2265 !(disable_large_pages & (1 << TTE64K)) && 2266 !(mmu_ptob(pfn) & MMU_PAGEOFFSET64K)) { 2267 sfmmu_memtte(&tte, pfn, attr, TTE64K); 2268 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, 2269 flags, SFMMU_INVALID_SHMERID); 2270 len -= MMU_PAGESIZE64K; 2271 addr += MMU_PAGESIZE64K; 2272 pfn += MMU_PAGESIZE64K / MMU_PAGESIZE; 2273 } else { 2274 sfmmu_memtte(&tte, pfn, attr, TTE8K); 2275 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, 2276 flags, SFMMU_INVALID_SHMERID); 2277 len -= MMU_PAGESIZE; 2278 addr += MMU_PAGESIZE; 2279 pfn++; 2280 } 2281 } 2282 2283 /* 2284 * Check TSB and TLB page sizes. 2285 */ 2286 if ((flags & HAT_LOAD_SHARE) == 0) { 2287 sfmmu_check_page_sizes(hat, 1); 2288 } 2289 } 2290 2291 void 2292 hat_memload_array(struct hat *hat, caddr_t addr, size_t len, 2293 struct page **pps, uint_t attr, uint_t flags) 2294 { 2295 hat_do_memload_array(hat, addr, len, pps, attr, flags, 2296 SFMMU_INVALID_SHMERID); 2297 } 2298 2299 void 2300 hat_memload_array_region(struct hat *hat, caddr_t addr, size_t len, 2301 struct page **pps, uint_t attr, uint_t flags, 2302 hat_region_cookie_t rcookie) 2303 { 2304 uint_t rid; 2305 if (rcookie == HAT_INVALID_REGION_COOKIE || 2306 hat->sfmmu_xhat_provider != NULL) { 2307 hat_do_memload_array(hat, addr, len, pps, attr, flags, 2308 SFMMU_INVALID_SHMERID); 2309 return; 2310 } 2311 rid = (uint_t)((uint64_t)rcookie); 2312 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 2313 hat_do_memload_array(hat, addr, len, pps, attr, flags, rid); 2314 } 2315 2316 /* 2317 * Map the largest extend possible out of the page array. The array may NOT 2318 * be in order. The largest possible mapping a page can have 2319 * is specified in the p_szc field. The p_szc field 2320 * cannot change as long as there any mappings (large or small) 2321 * to any of the pages that make up the large page. (ie. any 2322 * promotion/demotion of page size is not up to the hat but up to 2323 * the page free list manager). The array 2324 * should consist of properly aligned contigous pages that are 2325 * part of a big page for a large mapping to be created. 2326 */ 2327 static void 2328 hat_do_memload_array(struct hat *hat, caddr_t addr, size_t len, 2329 struct page **pps, uint_t attr, uint_t flags, uint_t rid) 2330 { 2331 int ttesz; 2332 size_t mapsz; 2333 pgcnt_t numpg, npgs; 2334 tte_t tte; 2335 page_t *pp; 2336 uint_t large_pages_disable; 2337 2338 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET)); 2339 SFMMU_VALIDATE_HMERID(hat, rid, addr, len); 2340 2341 if (hat->sfmmu_xhat_provider) { 2342 ASSERT(!SFMMU_IS_SHMERID_VALID(rid)); 2343 XHAT_MEMLOAD_ARRAY(hat, addr, len, pps, attr, flags); 2344 return; 2345 } 2346 2347 if (hat->sfmmu_rmstat) 2348 hat_resvstat(len, hat->sfmmu_as, addr); 2349 2350 #if defined(SF_ERRATA_57) 2351 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) && 2352 (addr < errata57_limit) && (attr & PROT_EXEC) && 2353 !(flags & HAT_LOAD_SHARE)) { 2354 cmn_err(CE_WARN, "hat_memload_array: illegal attempt to make " 2355 "user page executable"); 2356 attr &= ~PROT_EXEC; 2357 } 2358 #endif 2359 2360 /* Get number of pages */ 2361 npgs = len >> MMU_PAGESHIFT; 2362 2363 if (flags & HAT_LOAD_SHARE) { 2364 large_pages_disable = disable_ism_large_pages; 2365 } else { 2366 large_pages_disable = disable_large_pages; 2367 } 2368 2369 if (npgs < NHMENTS || large_pages_disable == LARGE_PAGES_OFF) { 2370 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags, npgs, 2371 rid); 2372 return; 2373 } 2374 2375 while (npgs >= NHMENTS) { 2376 pp = *pps; 2377 for (ttesz = pp->p_szc; ttesz != TTE8K; ttesz--) { 2378 /* 2379 * Check if this page size is disabled. 2380 */ 2381 if (large_pages_disable & (1 << ttesz)) 2382 continue; 2383 2384 numpg = TTEPAGES(ttesz); 2385 mapsz = numpg << MMU_PAGESHIFT; 2386 if ((npgs >= numpg) && 2387 IS_P2ALIGNED(addr, mapsz) && 2388 IS_P2ALIGNED(pp->p_pagenum, numpg)) { 2389 /* 2390 * At this point we have enough pages and 2391 * we know the virtual address and the pfn 2392 * are properly aligned. We still need 2393 * to check for physical contiguity but since 2394 * it is very likely that this is the case 2395 * we will assume they are so and undo 2396 * the request if necessary. It would 2397 * be great if we could get a hint flag 2398 * like HAT_CONTIG which would tell us 2399 * the pages are contigous for sure. 2400 */ 2401 sfmmu_memtte(&tte, (*pps)->p_pagenum, 2402 attr, ttesz); 2403 if (!sfmmu_tteload_array(hat, &tte, addr, 2404 pps, flags, rid)) { 2405 break; 2406 } 2407 } 2408 } 2409 if (ttesz == TTE8K) { 2410 /* 2411 * We were not able to map array using a large page 2412 * batch a hmeblk or fraction at a time. 2413 */ 2414 numpg = ((uintptr_t)addr >> MMU_PAGESHIFT) 2415 & (NHMENTS-1); 2416 numpg = NHMENTS - numpg; 2417 ASSERT(numpg <= npgs); 2418 mapsz = numpg * MMU_PAGESIZE; 2419 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags, 2420 numpg, rid); 2421 } 2422 addr += mapsz; 2423 npgs -= numpg; 2424 pps += numpg; 2425 } 2426 2427 if (npgs) { 2428 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags, npgs, 2429 rid); 2430 } 2431 2432 /* 2433 * Check TSB and TLB page sizes. 2434 */ 2435 if ((flags & HAT_LOAD_SHARE) == 0) { 2436 sfmmu_check_page_sizes(hat, 1); 2437 } 2438 } 2439 2440 /* 2441 * Function tries to batch 8K pages into the same hme blk. 2442 */ 2443 static void 2444 sfmmu_memload_batchsmall(struct hat *hat, caddr_t vaddr, page_t **pps, 2445 uint_t attr, uint_t flags, pgcnt_t npgs, uint_t rid) 2446 { 2447 tte_t tte; 2448 page_t *pp; 2449 struct hmehash_bucket *hmebp; 2450 struct hme_blk *hmeblkp; 2451 int index; 2452 2453 while (npgs) { 2454 /* 2455 * Acquire the hash bucket. 2456 */ 2457 hmebp = sfmmu_tteload_acquire_hashbucket(hat, vaddr, TTE8K, 2458 rid); 2459 ASSERT(hmebp); 2460 2461 /* 2462 * Find the hment block. 2463 */ 2464 hmeblkp = sfmmu_tteload_find_hmeblk(hat, hmebp, vaddr, 2465 TTE8K, flags, rid); 2466 ASSERT(hmeblkp); 2467 2468 do { 2469 /* 2470 * Make the tte. 2471 */ 2472 pp = *pps; 2473 sfmmu_memtte(&tte, pp->p_pagenum, attr, TTE8K); 2474 2475 /* 2476 * Add the translation. 2477 */ 2478 (void) sfmmu_tteload_addentry(hat, hmeblkp, &tte, 2479 vaddr, pps, flags, rid); 2480 2481 /* 2482 * Goto next page. 2483 */ 2484 pps++; 2485 npgs--; 2486 2487 /* 2488 * Goto next address. 2489 */ 2490 vaddr += MMU_PAGESIZE; 2491 2492 /* 2493 * Don't crossover into a different hmentblk. 2494 */ 2495 index = (int)(((uintptr_t)vaddr >> MMU_PAGESHIFT) & 2496 (NHMENTS-1)); 2497 2498 } while (index != 0 && npgs != 0); 2499 2500 /* 2501 * Release the hash bucket. 2502 */ 2503 2504 sfmmu_tteload_release_hashbucket(hmebp); 2505 } 2506 } 2507 2508 /* 2509 * Construct a tte for a page: 2510 * 2511 * tte_valid = 1 2512 * tte_size2 = size & TTE_SZ2_BITS (Panther and Olympus-C only) 2513 * tte_size = size 2514 * tte_nfo = attr & HAT_NOFAULT 2515 * tte_ie = attr & HAT_STRUCTURE_LE 2516 * tte_hmenum = hmenum 2517 * tte_pahi = pp->p_pagenum >> TTE_PASHIFT; 2518 * tte_palo = pp->p_pagenum & TTE_PALOMASK; 2519 * tte_ref = 1 (optimization) 2520 * tte_wr_perm = attr & PROT_WRITE; 2521 * tte_no_sync = attr & HAT_NOSYNC 2522 * tte_lock = attr & SFMMU_LOCKTTE 2523 * tte_cp = !(attr & SFMMU_UNCACHEPTTE) 2524 * tte_cv = !(attr & SFMMU_UNCACHEVTTE) 2525 * tte_e = attr & SFMMU_SIDEFFECT 2526 * tte_priv = !(attr & PROT_USER) 2527 * tte_hwwr = if nosync is set and it is writable we set the mod bit (opt) 2528 * tte_glb = 0 2529 */ 2530 void 2531 sfmmu_memtte(tte_t *ttep, pfn_t pfn, uint_t attr, int tte_sz) 2532 { 2533 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR)); 2534 2535 ttep->tte_inthi = MAKE_TTE_INTHI(pfn, attr, tte_sz, 0 /* hmenum */); 2536 ttep->tte_intlo = MAKE_TTE_INTLO(pfn, attr, tte_sz, 0 /* hmenum */); 2537 2538 if (TTE_IS_NOSYNC(ttep)) { 2539 TTE_SET_REF(ttep); 2540 if (TTE_IS_WRITABLE(ttep)) { 2541 TTE_SET_MOD(ttep); 2542 } 2543 } 2544 if (TTE_IS_NFO(ttep) && TTE_IS_EXECUTABLE(ttep)) { 2545 panic("sfmmu_memtte: can't set both NFO and EXEC bits"); 2546 } 2547 } 2548 2549 /* 2550 * This function will add a translation to the hme_blk and allocate the 2551 * hme_blk if one does not exist. 2552 * If a page structure is specified then it will add the 2553 * corresponding hment to the mapping list. 2554 * It will also update the hmenum field for the tte. 2555 * 2556 * Currently this function is only used for kernel mappings. 2557 * So pass invalid region to sfmmu_tteload_array(). 2558 */ 2559 void 2560 sfmmu_tteload(struct hat *sfmmup, tte_t *ttep, caddr_t vaddr, page_t *pp, 2561 uint_t flags) 2562 { 2563 ASSERT(sfmmup == ksfmmup); 2564 (void) sfmmu_tteload_array(sfmmup, ttep, vaddr, &pp, flags, 2565 SFMMU_INVALID_SHMERID); 2566 } 2567 2568 /* 2569 * Load (ttep != NULL) or unload (ttep == NULL) one entry in the TSB. 2570 * Assumes that a particular page size may only be resident in one TSB. 2571 */ 2572 static void 2573 sfmmu_mod_tsb(sfmmu_t *sfmmup, caddr_t vaddr, tte_t *ttep, int ttesz) 2574 { 2575 struct tsb_info *tsbinfop = NULL; 2576 uint64_t tag; 2577 struct tsbe *tsbe_addr; 2578 uint64_t tsb_base; 2579 uint_t tsb_size; 2580 int vpshift = MMU_PAGESHIFT; 2581 int phys = 0; 2582 2583 if (sfmmup == ksfmmup) { /* No support for 32/256M ksfmmu pages */ 2584 phys = ktsb_phys; 2585 if (ttesz >= TTE4M) { 2586 #ifndef sun4v 2587 ASSERT((ttesz != TTE32M) && (ttesz != TTE256M)); 2588 #endif 2589 tsb_base = (phys)? ktsb4m_pbase : (uint64_t)ktsb4m_base; 2590 tsb_size = ktsb4m_szcode; 2591 } else { 2592 tsb_base = (phys)? ktsb_pbase : (uint64_t)ktsb_base; 2593 tsb_size = ktsb_szcode; 2594 } 2595 } else { 2596 SFMMU_GET_TSBINFO(tsbinfop, sfmmup, ttesz); 2597 2598 /* 2599 * If there isn't a TSB for this page size, or the TSB is 2600 * swapped out, there is nothing to do. Note that the latter 2601 * case seems impossible but can occur if hat_pageunload() 2602 * is called on an ISM mapping while the process is swapped 2603 * out. 2604 */ 2605 if (tsbinfop == NULL || (tsbinfop->tsb_flags & TSB_SWAPPED)) 2606 return; 2607 2608 /* 2609 * If another thread is in the middle of relocating a TSB 2610 * we can't unload the entry so set a flag so that the 2611 * TSB will be flushed before it can be accessed by the 2612 * process. 2613 */ 2614 if ((tsbinfop->tsb_flags & TSB_RELOC_FLAG) != 0) { 2615 if (ttep == NULL) 2616 tsbinfop->tsb_flags |= TSB_FLUSH_NEEDED; 2617 return; 2618 } 2619 #if defined(UTSB_PHYS) 2620 phys = 1; 2621 tsb_base = (uint64_t)tsbinfop->tsb_pa; 2622 #else 2623 tsb_base = (uint64_t)tsbinfop->tsb_va; 2624 #endif 2625 tsb_size = tsbinfop->tsb_szc; 2626 } 2627 if (ttesz >= TTE4M) 2628 vpshift = MMU_PAGESHIFT4M; 2629 2630 tsbe_addr = sfmmu_get_tsbe(tsb_base, vaddr, vpshift, tsb_size); 2631 tag = sfmmu_make_tsbtag(vaddr); 2632 2633 if (ttep == NULL) { 2634 sfmmu_unload_tsbe(tsbe_addr, tag, phys); 2635 } else { 2636 if (ttesz >= TTE4M) { 2637 SFMMU_STAT(sf_tsb_load4m); 2638 } else { 2639 SFMMU_STAT(sf_tsb_load8k); 2640 } 2641 2642 sfmmu_load_tsbe(tsbe_addr, tag, ttep, phys); 2643 } 2644 } 2645 2646 /* 2647 * Unmap all entries from [start, end) matching the given page size. 2648 * 2649 * This function is used primarily to unmap replicated 64K or 512K entries 2650 * from the TSB that are inserted using the base page size TSB pointer, but 2651 * it may also be called to unmap a range of addresses from the TSB. 2652 */ 2653 void 2654 sfmmu_unload_tsb_range(sfmmu_t *sfmmup, caddr_t start, caddr_t end, int ttesz) 2655 { 2656 struct tsb_info *tsbinfop; 2657 uint64_t tag; 2658 struct tsbe *tsbe_addr; 2659 caddr_t vaddr; 2660 uint64_t tsb_base; 2661 int vpshift, vpgsz; 2662 uint_t tsb_size; 2663 int phys = 0; 2664 2665 /* 2666 * Assumptions: 2667 * If ttesz == 8K, 64K or 512K, we walk through the range 8K 2668 * at a time shooting down any valid entries we encounter. 2669 * 2670 * If ttesz >= 4M we walk the range 4M at a time shooting 2671 * down any valid mappings we find. 2672 */ 2673 if (sfmmup == ksfmmup) { 2674 phys = ktsb_phys; 2675 if (ttesz >= TTE4M) { 2676 #ifndef sun4v 2677 ASSERT((ttesz != TTE32M) && (ttesz != TTE256M)); 2678 #endif 2679 tsb_base = (phys)? ktsb4m_pbase : (uint64_t)ktsb4m_base; 2680 tsb_size = ktsb4m_szcode; 2681 } else { 2682 tsb_base = (phys)? ktsb_pbase : (uint64_t)ktsb_base; 2683 tsb_size = ktsb_szcode; 2684 } 2685 } else { 2686 SFMMU_GET_TSBINFO(tsbinfop, sfmmup, ttesz); 2687 2688 /* 2689 * If there isn't a TSB for this page size, or the TSB is 2690 * swapped out, there is nothing to do. Note that the latter 2691 * case seems impossible but can occur if hat_pageunload() 2692 * is called on an ISM mapping while the process is swapped 2693 * out. 2694 */ 2695 if (tsbinfop == NULL || (tsbinfop->tsb_flags & TSB_SWAPPED)) 2696 return; 2697 2698 /* 2699 * If another thread is in the middle of relocating a TSB 2700 * we can't unload the entry so set a flag so that the 2701 * TSB will be flushed before it can be accessed by the 2702 * process. 2703 */ 2704 if ((tsbinfop->tsb_flags & TSB_RELOC_FLAG) != 0) { 2705 tsbinfop->tsb_flags |= TSB_FLUSH_NEEDED; 2706 return; 2707 } 2708 #if defined(UTSB_PHYS) 2709 phys = 1; 2710 tsb_base = (uint64_t)tsbinfop->tsb_pa; 2711 #else 2712 tsb_base = (uint64_t)tsbinfop->tsb_va; 2713 #endif 2714 tsb_size = tsbinfop->tsb_szc; 2715 } 2716 if (ttesz >= TTE4M) { 2717 vpshift = MMU_PAGESHIFT4M; 2718 vpgsz = MMU_PAGESIZE4M; 2719 } else { 2720 vpshift = MMU_PAGESHIFT; 2721 vpgsz = MMU_PAGESIZE; 2722 } 2723 2724 for (vaddr = start; vaddr < end; vaddr += vpgsz) { 2725 tag = sfmmu_make_tsbtag(vaddr); 2726 tsbe_addr = sfmmu_get_tsbe(tsb_base, vaddr, vpshift, tsb_size); 2727 sfmmu_unload_tsbe(tsbe_addr, tag, phys); 2728 } 2729 } 2730 2731 /* 2732 * Select the optimum TSB size given the number of mappings 2733 * that need to be cached. 2734 */ 2735 static int 2736 sfmmu_select_tsb_szc(pgcnt_t pgcnt) 2737 { 2738 int szc = 0; 2739 2740 #ifdef DEBUG 2741 if (tsb_grow_stress) { 2742 uint32_t randval = (uint32_t)gettick() >> 4; 2743 return (randval % (tsb_max_growsize + 1)); 2744 } 2745 #endif /* DEBUG */ 2746 2747 while ((szc < tsb_max_growsize) && (pgcnt > SFMMU_RSS_TSBSIZE(szc))) 2748 szc++; 2749 return (szc); 2750 } 2751 2752 /* 2753 * This function will add a translation to the hme_blk and allocate the 2754 * hme_blk if one does not exist. 2755 * If a page structure is specified then it will add the 2756 * corresponding hment to the mapping list. 2757 * It will also update the hmenum field for the tte. 2758 * Furthermore, it attempts to create a large page translation 2759 * for <addr,hat> at page array pps. It assumes addr and first 2760 * pp is correctly aligned. It returns 0 if successful and 1 otherwise. 2761 */ 2762 static int 2763 sfmmu_tteload_array(sfmmu_t *sfmmup, tte_t *ttep, caddr_t vaddr, 2764 page_t **pps, uint_t flags, uint_t rid) 2765 { 2766 struct hmehash_bucket *hmebp; 2767 struct hme_blk *hmeblkp; 2768 int ret; 2769 uint_t size; 2770 2771 /* 2772 * Get mapping size. 2773 */ 2774 size = TTE_CSZ(ttep); 2775 ASSERT(!((uintptr_t)vaddr & TTE_PAGE_OFFSET(size))); 2776 2777 /* 2778 * Acquire the hash bucket. 2779 */ 2780 hmebp = sfmmu_tteload_acquire_hashbucket(sfmmup, vaddr, size, rid); 2781 ASSERT(hmebp); 2782 2783 /* 2784 * Find the hment block. 2785 */ 2786 hmeblkp = sfmmu_tteload_find_hmeblk(sfmmup, hmebp, vaddr, size, flags, 2787 rid); 2788 ASSERT(hmeblkp); 2789 2790 /* 2791 * Add the translation. 2792 */ 2793 ret = sfmmu_tteload_addentry(sfmmup, hmeblkp, ttep, vaddr, pps, flags, 2794 rid); 2795 2796 /* 2797 * Release the hash bucket. 2798 */ 2799 sfmmu_tteload_release_hashbucket(hmebp); 2800 2801 return (ret); 2802 } 2803 2804 /* 2805 * Function locks and returns a pointer to the hash bucket for vaddr and size. 2806 */ 2807 static struct hmehash_bucket * 2808 sfmmu_tteload_acquire_hashbucket(sfmmu_t *sfmmup, caddr_t vaddr, int size, 2809 uint_t rid) 2810 { 2811 struct hmehash_bucket *hmebp; 2812 int hmeshift; 2813 void *htagid = sfmmutohtagid(sfmmup, rid); 2814 2815 ASSERT(htagid != NULL); 2816 2817 hmeshift = HME_HASH_SHIFT(size); 2818 2819 hmebp = HME_HASH_FUNCTION(htagid, vaddr, hmeshift); 2820 2821 SFMMU_HASH_LOCK(hmebp); 2822 2823 return (hmebp); 2824 } 2825 2826 /* 2827 * Function returns a pointer to an hmeblk in the hash bucket, hmebp. If the 2828 * hmeblk doesn't exists for the [sfmmup, vaddr & size] signature, a hmeblk is 2829 * allocated. 2830 */ 2831 static struct hme_blk * 2832 sfmmu_tteload_find_hmeblk(sfmmu_t *sfmmup, struct hmehash_bucket *hmebp, 2833 caddr_t vaddr, uint_t size, uint_t flags, uint_t rid) 2834 { 2835 hmeblk_tag hblktag; 2836 int hmeshift; 2837 struct hme_blk *hmeblkp, *pr_hblk, *list = NULL; 2838 uint64_t hblkpa, prevpa; 2839 struct kmem_cache *sfmmu_cache; 2840 uint_t forcefree; 2841 2842 SFMMU_VALIDATE_HMERID(sfmmup, rid, vaddr, TTEBYTES(size)); 2843 2844 hblktag.htag_id = sfmmutohtagid(sfmmup, rid); 2845 ASSERT(hblktag.htag_id != NULL); 2846 hmeshift = HME_HASH_SHIFT(size); 2847 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift); 2848 hblktag.htag_rehash = HME_HASH_REHASH(size); 2849 hblktag.htag_rid = rid; 2850 2851 ttearray_realloc: 2852 2853 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, hblkpa, 2854 pr_hblk, prevpa, &list); 2855 2856 /* 2857 * We block until hblk_reserve_lock is released; it's held by 2858 * the thread, temporarily using hblk_reserve, until hblk_reserve is 2859 * replaced by a hblk from sfmmu8_cache. 2860 */ 2861 if (hmeblkp == (struct hme_blk *)hblk_reserve && 2862 hblk_reserve_thread != curthread) { 2863 SFMMU_HASH_UNLOCK(hmebp); 2864 mutex_enter(&hblk_reserve_lock); 2865 mutex_exit(&hblk_reserve_lock); 2866 SFMMU_STAT(sf_hblk_reserve_hit); 2867 SFMMU_HASH_LOCK(hmebp); 2868 goto ttearray_realloc; 2869 } 2870 2871 if (hmeblkp == NULL) { 2872 hmeblkp = sfmmu_hblk_alloc(sfmmup, vaddr, hmebp, size, 2873 hblktag, flags, rid); 2874 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || hmeblkp->hblk_shared); 2875 ASSERT(SFMMU_IS_SHMERID_VALID(rid) || !hmeblkp->hblk_shared); 2876 } else { 2877 /* 2878 * It is possible for 8k and 64k hblks to collide since they 2879 * have the same rehash value. This is because we 2880 * lazily free hblks and 8K/64K blks could be lingering. 2881 * If we find size mismatch we free the block and & try again. 2882 */ 2883 if (get_hblk_ttesz(hmeblkp) != size) { 2884 ASSERT(!hmeblkp->hblk_vcnt); 2885 ASSERT(!hmeblkp->hblk_hmecnt); 2886 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, pr_hblk); 2887 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 2888 goto ttearray_realloc; 2889 } 2890 if (hmeblkp->hblk_shw_bit) { 2891 /* 2892 * if the hblk was previously used as a shadow hblk then 2893 * we will change it to a normal hblk 2894 */ 2895 ASSERT(!hmeblkp->hblk_shared); 2896 if (hmeblkp->hblk_shw_mask) { 2897 sfmmu_shadow_hcleanup(sfmmup, hmeblkp, hmebp); 2898 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 2899 goto ttearray_realloc; 2900 } else { 2901 hmeblkp->hblk_shw_bit = 0; 2902 } 2903 } 2904 SFMMU_STAT(sf_hblk_hit); 2905 } 2906 2907 /* 2908 * hat_memload() should never call kmem_cache_free(); see block 2909 * comment showing the stacktrace in sfmmu_hblk_alloc(); 2910 * enqueue each hblk in the list to reserve list if it's created 2911 * from sfmmu8_cache *and* sfmmup == KHATID. 2912 */ 2913 forcefree = (sfmmup == KHATID) ? 1 : 0; 2914 while ((pr_hblk = list) != NULL) { 2915 list = pr_hblk->hblk_next; 2916 sfmmu_cache = get_hblk_cache(pr_hblk); 2917 if ((sfmmu_cache == sfmmu8_cache) && 2918 sfmmu_put_free_hblk(pr_hblk, forcefree)) 2919 continue; 2920 2921 ASSERT(sfmmup != KHATID); 2922 kmem_cache_free(sfmmu_cache, pr_hblk); 2923 } 2924 2925 ASSERT(get_hblk_ttesz(hmeblkp) == size); 2926 ASSERT(!hmeblkp->hblk_shw_bit); 2927 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || hmeblkp->hblk_shared); 2928 ASSERT(SFMMU_IS_SHMERID_VALID(rid) || !hmeblkp->hblk_shared); 2929 ASSERT(hmeblkp->hblk_tag.htag_rid == rid); 2930 2931 return (hmeblkp); 2932 } 2933 2934 /* 2935 * Function adds a tte entry into the hmeblk. It returns 0 if successful and 1 2936 * otherwise. 2937 */ 2938 static int 2939 sfmmu_tteload_addentry(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, tte_t *ttep, 2940 caddr_t vaddr, page_t **pps, uint_t flags, uint_t rid) 2941 { 2942 page_t *pp = *pps; 2943 int hmenum, size, remap; 2944 tte_t tteold, flush_tte; 2945 #ifdef DEBUG 2946 tte_t orig_old; 2947 #endif /* DEBUG */ 2948 struct sf_hment *sfhme; 2949 kmutex_t *pml, *pmtx; 2950 hatlock_t *hatlockp; 2951 int myflt; 2952 2953 /* 2954 * remove this panic when we decide to let user virtual address 2955 * space be >= USERLIMIT. 2956 */ 2957 if (!TTE_IS_PRIVILEGED(ttep) && vaddr >= (caddr_t)USERLIMIT) 2958 panic("user addr %p in kernel space", vaddr); 2959 #if defined(TTE_IS_GLOBAL) 2960 if (TTE_IS_GLOBAL(ttep)) 2961 panic("sfmmu_tteload: creating global tte"); 2962 #endif 2963 2964 #ifdef DEBUG 2965 if (pf_is_memory(sfmmu_ttetopfn(ttep, vaddr)) && 2966 !TTE_IS_PCACHEABLE(ttep) && !sfmmu_allow_nc_trans) 2967 panic("sfmmu_tteload: non cacheable memory tte"); 2968 #endif /* DEBUG */ 2969 2970 if ((flags & HAT_LOAD_SHARE) || !TTE_IS_REF(ttep) || 2971 !TTE_IS_MOD(ttep)) { 2972 /* 2973 * Don't load TSB for dummy as in ISM. Also don't preload 2974 * the TSB if the TTE isn't writable since we're likely to 2975 * fault on it again -- preloading can be fairly expensive. 2976 */ 2977 flags |= SFMMU_NO_TSBLOAD; 2978 } 2979 2980 size = TTE_CSZ(ttep); 2981 switch (size) { 2982 case TTE8K: 2983 SFMMU_STAT(sf_tteload8k); 2984 break; 2985 case TTE64K: 2986 SFMMU_STAT(sf_tteload64k); 2987 break; 2988 case TTE512K: 2989 SFMMU_STAT(sf_tteload512k); 2990 break; 2991 case TTE4M: 2992 SFMMU_STAT(sf_tteload4m); 2993 break; 2994 case (TTE32M): 2995 SFMMU_STAT(sf_tteload32m); 2996 ASSERT(mmu_page_sizes == max_mmu_page_sizes); 2997 break; 2998 case (TTE256M): 2999 SFMMU_STAT(sf_tteload256m); 3000 ASSERT(mmu_page_sizes == max_mmu_page_sizes); 3001 break; 3002 } 3003 3004 ASSERT(!((uintptr_t)vaddr & TTE_PAGE_OFFSET(size))); 3005 SFMMU_VALIDATE_HMERID(sfmmup, rid, vaddr, TTEBYTES(size)); 3006 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || hmeblkp->hblk_shared); 3007 ASSERT(SFMMU_IS_SHMERID_VALID(rid) || !hmeblkp->hblk_shared); 3008 3009 HBLKTOHME_IDX(sfhme, hmeblkp, vaddr, hmenum); 3010 3011 /* 3012 * Need to grab mlist lock here so that pageunload 3013 * will not change tte behind us. 3014 */ 3015 if (pp) { 3016 pml = sfmmu_mlist_enter(pp); 3017 } 3018 3019 sfmmu_copytte(&sfhme->hme_tte, &tteold); 3020 /* 3021 * Look for corresponding hment and if valid verify 3022 * pfns are equal. 3023 */ 3024 remap = TTE_IS_VALID(&tteold); 3025 if (remap) { 3026 pfn_t new_pfn, old_pfn; 3027 3028 old_pfn = TTE_TO_PFN(vaddr, &tteold); 3029 new_pfn = TTE_TO_PFN(vaddr, ttep); 3030 3031 if (flags & HAT_LOAD_REMAP) { 3032 /* make sure we are remapping same type of pages */ 3033 if (pf_is_memory(old_pfn) != pf_is_memory(new_pfn)) { 3034 panic("sfmmu_tteload - tte remap io<->memory"); 3035 } 3036 if (old_pfn != new_pfn && 3037 (pp != NULL || sfhme->hme_page != NULL)) { 3038 panic("sfmmu_tteload - tte remap pp != NULL"); 3039 } 3040 } else if (old_pfn != new_pfn) { 3041 panic("sfmmu_tteload - tte remap, hmeblkp 0x%p", 3042 (void *)hmeblkp); 3043 } 3044 ASSERT(TTE_CSZ(&tteold) == TTE_CSZ(ttep)); 3045 } 3046 3047 if (pp) { 3048 if (size == TTE8K) { 3049 #ifdef VAC 3050 /* 3051 * Handle VAC consistency 3052 */ 3053 if (!remap && (cache & CACHE_VAC) && !PP_ISNC(pp)) { 3054 sfmmu_vac_conflict(sfmmup, vaddr, pp); 3055 } 3056 #endif 3057 3058 if (TTE_IS_WRITABLE(ttep) && PP_ISRO(pp)) { 3059 pmtx = sfmmu_page_enter(pp); 3060 PP_CLRRO(pp); 3061 sfmmu_page_exit(pmtx); 3062 } else if (!PP_ISMAPPED(pp) && 3063 (!TTE_IS_WRITABLE(ttep)) && !(PP_ISMOD(pp))) { 3064 pmtx = sfmmu_page_enter(pp); 3065 if (!(PP_ISMOD(pp))) { 3066 PP_SETRO(pp); 3067 } 3068 sfmmu_page_exit(pmtx); 3069 } 3070 3071 } else if (sfmmu_pagearray_setup(vaddr, pps, ttep, remap)) { 3072 /* 3073 * sfmmu_pagearray_setup failed so return 3074 */ 3075 sfmmu_mlist_exit(pml); 3076 return (1); 3077 } 3078 } 3079 3080 /* 3081 * Make sure hment is not on a mapping list. 3082 */ 3083 ASSERT(remap || (sfhme->hme_page == NULL)); 3084 3085 /* if it is not a remap then hme->next better be NULL */ 3086 ASSERT((!remap) ? sfhme->hme_next == NULL : 1); 3087 3088 if (flags & HAT_LOAD_LOCK) { 3089 if ((hmeblkp->hblk_lckcnt + 1) >= MAX_HBLK_LCKCNT) { 3090 panic("too high lckcnt-hmeblk %p", 3091 (void *)hmeblkp); 3092 } 3093 atomic_add_32(&hmeblkp->hblk_lckcnt, 1); 3094 3095 HBLK_STACK_TRACE(hmeblkp, HBLK_LOCK); 3096 } 3097 3098 #ifdef VAC 3099 if (pp && PP_ISNC(pp)) { 3100 /* 3101 * If the physical page is marked to be uncacheable, like 3102 * by a vac conflict, make sure the new mapping is also 3103 * uncacheable. 3104 */ 3105 TTE_CLR_VCACHEABLE(ttep); 3106 ASSERT(PP_GET_VCOLOR(pp) == NO_VCOLOR); 3107 } 3108 #endif 3109 ttep->tte_hmenum = hmenum; 3110 3111 #ifdef DEBUG 3112 orig_old = tteold; 3113 #endif /* DEBUG */ 3114 3115 while (sfmmu_modifytte_try(&tteold, ttep, &sfhme->hme_tte) < 0) { 3116 if ((sfmmup == KHATID) && 3117 (flags & (HAT_LOAD_LOCK | HAT_LOAD_REMAP))) { 3118 sfmmu_copytte(&sfhme->hme_tte, &tteold); 3119 } 3120 #ifdef DEBUG 3121 chk_tte(&orig_old, &tteold, ttep, hmeblkp); 3122 #endif /* DEBUG */ 3123 } 3124 ASSERT(TTE_IS_VALID(&sfhme->hme_tte)); 3125 3126 if (!TTE_IS_VALID(&tteold)) { 3127 3128 atomic_add_16(&hmeblkp->hblk_vcnt, 1); 3129 if (rid == SFMMU_INVALID_SHMERID) { 3130 atomic_add_long(&sfmmup->sfmmu_ttecnt[size], 1); 3131 } else { 3132 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 3133 sf_region_t *rgnp = srdp->srd_hmergnp[rid]; 3134 /* 3135 * We already accounted for region ttecnt's in sfmmu 3136 * during hat_join_region() processing. Here we 3137 * only update ttecnt's in region struture. 3138 */ 3139 atomic_add_long(&rgnp->rgn_ttecnt[size], 1); 3140 } 3141 } 3142 3143 myflt = (astosfmmu(curthread->t_procp->p_as) == sfmmup); 3144 if (size > TTE8K && (flags & HAT_LOAD_SHARE) == 0 && 3145 sfmmup != ksfmmup) { 3146 uchar_t tteflag = 1 << size; 3147 if (rid == SFMMU_INVALID_SHMERID) { 3148 if (!(sfmmup->sfmmu_tteflags & tteflag)) { 3149 hatlockp = sfmmu_hat_enter(sfmmup); 3150 sfmmup->sfmmu_tteflags |= tteflag; 3151 sfmmu_hat_exit(hatlockp); 3152 } 3153 } else if (!(sfmmup->sfmmu_rtteflags & tteflag)) { 3154 hatlockp = sfmmu_hat_enter(sfmmup); 3155 sfmmup->sfmmu_rtteflags |= tteflag; 3156 sfmmu_hat_exit(hatlockp); 3157 } 3158 /* 3159 * Update the current CPU tsbmiss area, so the current thread 3160 * won't need to take the tsbmiss for the new pagesize. 3161 * The other threads in the process will update their tsb 3162 * miss area lazily in sfmmu_tsbmiss_exception() when they 3163 * fail to find the translation for a newly added pagesize. 3164 */ 3165 if (size > TTE64K && myflt) { 3166 struct tsbmiss *tsbmp; 3167 kpreempt_disable(); 3168 tsbmp = &tsbmiss_area[CPU->cpu_id]; 3169 if (rid == SFMMU_INVALID_SHMERID) { 3170 if (!(tsbmp->uhat_tteflags & tteflag)) { 3171 tsbmp->uhat_tteflags |= tteflag; 3172 } 3173 } else { 3174 if (!(tsbmp->uhat_rtteflags & tteflag)) { 3175 tsbmp->uhat_rtteflags |= tteflag; 3176 } 3177 } 3178 kpreempt_enable(); 3179 } 3180 } 3181 3182 if (size >= TTE4M && (flags & HAT_LOAD_TEXT) && 3183 !SFMMU_FLAGS_ISSET(sfmmup, HAT_4MTEXT_FLAG)) { 3184 hatlockp = sfmmu_hat_enter(sfmmup); 3185 SFMMU_FLAGS_SET(sfmmup, HAT_4MTEXT_FLAG); 3186 sfmmu_hat_exit(hatlockp); 3187 } 3188 3189 flush_tte.tte_intlo = (tteold.tte_intlo ^ ttep->tte_intlo) & 3190 hw_tte.tte_intlo; 3191 flush_tte.tte_inthi = (tteold.tte_inthi ^ ttep->tte_inthi) & 3192 hw_tte.tte_inthi; 3193 3194 if (remap && (flush_tte.tte_inthi || flush_tte.tte_intlo)) { 3195 /* 3196 * If remap and new tte differs from old tte we need 3197 * to sync the mod bit and flush TLB/TSB. We don't 3198 * need to sync ref bit because we currently always set 3199 * ref bit in tteload. 3200 */ 3201 ASSERT(TTE_IS_REF(ttep)); 3202 if (TTE_IS_MOD(&tteold)) { 3203 sfmmu_ttesync(sfmmup, vaddr, &tteold, pp); 3204 } 3205 /* 3206 * hwtte bits shouldn't change for SRD hmeblks as long as SRD 3207 * hmes are only used for read only text. Adding this code for 3208 * completeness and future use of shared hmeblks with writable 3209 * mappings of VMODSORT vnodes. 3210 */ 3211 if (hmeblkp->hblk_shared) { 3212 cpuset_t cpuset = sfmmu_rgntlb_demap(vaddr, 3213 sfmmup->sfmmu_srdp->srd_hmergnp[rid], hmeblkp, 1); 3214 xt_sync(cpuset); 3215 SFMMU_STAT_ADD(sf_region_remap_demap, 1); 3216 } else { 3217 sfmmu_tlb_demap(vaddr, sfmmup, hmeblkp, 0, 0); 3218 xt_sync(sfmmup->sfmmu_cpusran); 3219 } 3220 } 3221 3222 if ((flags & SFMMU_NO_TSBLOAD) == 0) { 3223 /* 3224 * We only preload 8K and 4M mappings into the TSB, since 3225 * 64K and 512K mappings are replicated and hence don't 3226 * have a single, unique TSB entry. Ditto for 32M/256M. 3227 */ 3228 if (size == TTE8K || size == TTE4M) { 3229 sf_scd_t *scdp; 3230 hatlockp = sfmmu_hat_enter(sfmmup); 3231 /* 3232 * Don't preload private TSB if the mapping is used 3233 * by the shctx in the SCD. 3234 */ 3235 scdp = sfmmup->sfmmu_scdp; 3236 if (rid == SFMMU_INVALID_SHMERID || scdp == NULL || 3237 !SF_RGNMAP_TEST(scdp->scd_hmeregion_map, rid)) { 3238 sfmmu_load_tsb(sfmmup, vaddr, &sfhme->hme_tte, 3239 size); 3240 } 3241 sfmmu_hat_exit(hatlockp); 3242 } 3243 } 3244 if (pp) { 3245 if (!remap) { 3246 HME_ADD(sfhme, pp); 3247 atomic_add_16(&hmeblkp->hblk_hmecnt, 1); 3248 ASSERT(hmeblkp->hblk_hmecnt > 0); 3249 3250 /* 3251 * Cannot ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS) 3252 * see pageunload() for comment. 3253 */ 3254 } 3255 sfmmu_mlist_exit(pml); 3256 } 3257 3258 return (0); 3259 } 3260 /* 3261 * Function unlocks hash bucket. 3262 */ 3263 static void 3264 sfmmu_tteload_release_hashbucket(struct hmehash_bucket *hmebp) 3265 { 3266 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 3267 SFMMU_HASH_UNLOCK(hmebp); 3268 } 3269 3270 /* 3271 * function which checks and sets up page array for a large 3272 * translation. Will set p_vcolor, p_index, p_ro fields. 3273 * Assumes addr and pfnum of first page are properly aligned. 3274 * Will check for physical contiguity. If check fails it return 3275 * non null. 3276 */ 3277 static int 3278 sfmmu_pagearray_setup(caddr_t addr, page_t **pps, tte_t *ttep, int remap) 3279 { 3280 int i, index, ttesz; 3281 pfn_t pfnum; 3282 pgcnt_t npgs; 3283 page_t *pp, *pp1; 3284 kmutex_t *pmtx; 3285 #ifdef VAC 3286 int osz; 3287 int cflags = 0; 3288 int vac_err = 0; 3289 #endif 3290 int newidx = 0; 3291 3292 ttesz = TTE_CSZ(ttep); 3293 3294 ASSERT(ttesz > TTE8K); 3295 3296 npgs = TTEPAGES(ttesz); 3297 index = PAGESZ_TO_INDEX(ttesz); 3298 3299 pfnum = (*pps)->p_pagenum; 3300 ASSERT(IS_P2ALIGNED(pfnum, npgs)); 3301 3302 /* 3303 * Save the first pp so we can do HAT_TMPNC at the end. 3304 */ 3305 pp1 = *pps; 3306 #ifdef VAC 3307 osz = fnd_mapping_sz(pp1); 3308 #endif 3309 3310 for (i = 0; i < npgs; i++, pps++) { 3311 pp = *pps; 3312 ASSERT(PAGE_LOCKED(pp)); 3313 ASSERT(pp->p_szc >= ttesz); 3314 ASSERT(pp->p_szc == pp1->p_szc); 3315 ASSERT(sfmmu_mlist_held(pp)); 3316 3317 /* 3318 * XXX is it possible to maintain P_RO on the root only? 3319 */ 3320 if (TTE_IS_WRITABLE(ttep) && PP_ISRO(pp)) { 3321 pmtx = sfmmu_page_enter(pp); 3322 PP_CLRRO(pp); 3323 sfmmu_page_exit(pmtx); 3324 } else if (!PP_ISMAPPED(pp) && !TTE_IS_WRITABLE(ttep) && 3325 !PP_ISMOD(pp)) { 3326 pmtx = sfmmu_page_enter(pp); 3327 if (!(PP_ISMOD(pp))) { 3328 PP_SETRO(pp); 3329 } 3330 sfmmu_page_exit(pmtx); 3331 } 3332 3333 /* 3334 * If this is a remap we skip vac & contiguity checks. 3335 */ 3336 if (remap) 3337 continue; 3338 3339 /* 3340 * set p_vcolor and detect any vac conflicts. 3341 */ 3342 #ifdef VAC 3343 if (vac_err == 0) { 3344 vac_err = sfmmu_vacconflict_array(addr, pp, &cflags); 3345 3346 } 3347 #endif 3348 3349 /* 3350 * Save current index in case we need to undo it. 3351 * Note: "PAGESZ_TO_INDEX(sz) (1 << (sz))" 3352 * "SFMMU_INDEX_SHIFT 6" 3353 * "SFMMU_INDEX_MASK ((1 << SFMMU_INDEX_SHIFT) - 1)" 3354 * "PP_MAPINDEX(p_index) (p_index & SFMMU_INDEX_MASK)" 3355 * 3356 * So: index = PAGESZ_TO_INDEX(ttesz); 3357 * if ttesz == 1 then index = 0x2 3358 * 2 then index = 0x4 3359 * 3 then index = 0x8 3360 * 4 then index = 0x10 3361 * 5 then index = 0x20 3362 * The code below checks if it's a new pagesize (ie, newidx) 3363 * in case we need to take it back out of p_index, 3364 * and then or's the new index into the existing index. 3365 */ 3366 if ((PP_MAPINDEX(pp) & index) == 0) 3367 newidx = 1; 3368 pp->p_index = (PP_MAPINDEX(pp) | index); 3369 3370 /* 3371 * contiguity check 3372 */ 3373 if (pp->p_pagenum != pfnum) { 3374 /* 3375 * If we fail the contiguity test then 3376 * the only thing we need to fix is the p_index field. 3377 * We might get a few extra flushes but since this 3378 * path is rare that is ok. The p_ro field will 3379 * get automatically fixed on the next tteload to 3380 * the page. NO TNC bit is set yet. 3381 */ 3382 while (i >= 0) { 3383 pp = *pps; 3384 if (newidx) 3385 pp->p_index = (PP_MAPINDEX(pp) & 3386 ~index); 3387 pps--; 3388 i--; 3389 } 3390 return (1); 3391 } 3392 pfnum++; 3393 addr += MMU_PAGESIZE; 3394 } 3395 3396 #ifdef VAC 3397 if (vac_err) { 3398 if (ttesz > osz) { 3399 /* 3400 * There are some smaller mappings that causes vac 3401 * conflicts. Convert all existing small mappings to 3402 * TNC. 3403 */ 3404 SFMMU_STAT_ADD(sf_uncache_conflict, npgs); 3405 sfmmu_page_cache_array(pp1, HAT_TMPNC, CACHE_FLUSH, 3406 npgs); 3407 } else { 3408 /* EMPTY */ 3409 /* 3410 * If there exists an big page mapping, 3411 * that means the whole existing big page 3412 * has TNC setting already. No need to covert to 3413 * TNC again. 3414 */ 3415 ASSERT(PP_ISTNC(pp1)); 3416 } 3417 } 3418 #endif /* VAC */ 3419 3420 return (0); 3421 } 3422 3423 #ifdef VAC 3424 /* 3425 * Routine that detects vac consistency for a large page. It also 3426 * sets virtual color for all pp's for this big mapping. 3427 */ 3428 static int 3429 sfmmu_vacconflict_array(caddr_t addr, page_t *pp, int *cflags) 3430 { 3431 int vcolor, ocolor; 3432 3433 ASSERT(sfmmu_mlist_held(pp)); 3434 3435 if (PP_ISNC(pp)) { 3436 return (HAT_TMPNC); 3437 } 3438 3439 vcolor = addr_to_vcolor(addr); 3440 if (PP_NEWPAGE(pp)) { 3441 PP_SET_VCOLOR(pp, vcolor); 3442 return (0); 3443 } 3444 3445 ocolor = PP_GET_VCOLOR(pp); 3446 if (ocolor == vcolor) { 3447 return (0); 3448 } 3449 3450 if (!PP_ISMAPPED(pp)) { 3451 /* 3452 * Previous user of page had a differnet color 3453 * but since there are no current users 3454 * we just flush the cache and change the color. 3455 * As an optimization for large pages we flush the 3456 * entire cache of that color and set a flag. 3457 */ 3458 SFMMU_STAT(sf_pgcolor_conflict); 3459 if (!CacheColor_IsFlushed(*cflags, ocolor)) { 3460 CacheColor_SetFlushed(*cflags, ocolor); 3461 sfmmu_cache_flushcolor(ocolor, pp->p_pagenum); 3462 } 3463 PP_SET_VCOLOR(pp, vcolor); 3464 return (0); 3465 } 3466 3467 /* 3468 * We got a real conflict with a current mapping. 3469 * set flags to start unencaching all mappings 3470 * and return failure so we restart looping 3471 * the pp array from the beginning. 3472 */ 3473 return (HAT_TMPNC); 3474 } 3475 #endif /* VAC */ 3476 3477 /* 3478 * creates a large page shadow hmeblk for a tte. 3479 * The purpose of this routine is to allow us to do quick unloads because 3480 * the vm layer can easily pass a very large but sparsely populated range. 3481 */ 3482 static struct hme_blk * 3483 sfmmu_shadow_hcreate(sfmmu_t *sfmmup, caddr_t vaddr, int ttesz, uint_t flags) 3484 { 3485 struct hmehash_bucket *hmebp; 3486 hmeblk_tag hblktag; 3487 int hmeshift, size, vshift; 3488 uint_t shw_mask, newshw_mask; 3489 struct hme_blk *hmeblkp; 3490 3491 ASSERT(sfmmup != KHATID); 3492 if (mmu_page_sizes == max_mmu_page_sizes) { 3493 ASSERT(ttesz < TTE256M); 3494 } else { 3495 ASSERT(ttesz < TTE4M); 3496 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0); 3497 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0); 3498 } 3499 3500 if (ttesz == TTE8K) { 3501 size = TTE512K; 3502 } else { 3503 size = ++ttesz; 3504 } 3505 3506 hblktag.htag_id = sfmmup; 3507 hmeshift = HME_HASH_SHIFT(size); 3508 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift); 3509 hblktag.htag_rehash = HME_HASH_REHASH(size); 3510 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 3511 hmebp = HME_HASH_FUNCTION(sfmmup, vaddr, hmeshift); 3512 3513 SFMMU_HASH_LOCK(hmebp); 3514 3515 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 3516 ASSERT(hmeblkp != (struct hme_blk *)hblk_reserve); 3517 if (hmeblkp == NULL) { 3518 hmeblkp = sfmmu_hblk_alloc(sfmmup, vaddr, hmebp, size, 3519 hblktag, flags, SFMMU_INVALID_SHMERID); 3520 } 3521 ASSERT(hmeblkp); 3522 if (!hmeblkp->hblk_shw_mask) { 3523 /* 3524 * if this is a unused hblk it was just allocated or could 3525 * potentially be a previous large page hblk so we need to 3526 * set the shadow bit. 3527 */ 3528 ASSERT(!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt); 3529 hmeblkp->hblk_shw_bit = 1; 3530 } else if (hmeblkp->hblk_shw_bit == 0) { 3531 panic("sfmmu_shadow_hcreate: shw bit not set in hmeblkp 0x%p", 3532 (void *)hmeblkp); 3533 } 3534 ASSERT(hmeblkp->hblk_shw_bit == 1); 3535 ASSERT(!hmeblkp->hblk_shared); 3536 vshift = vaddr_to_vshift(hblktag, vaddr, size); 3537 ASSERT(vshift < 8); 3538 /* 3539 * Atomically set shw mask bit 3540 */ 3541 do { 3542 shw_mask = hmeblkp->hblk_shw_mask; 3543 newshw_mask = shw_mask | (1 << vshift); 3544 newshw_mask = cas32(&hmeblkp->hblk_shw_mask, shw_mask, 3545 newshw_mask); 3546 } while (newshw_mask != shw_mask); 3547 3548 SFMMU_HASH_UNLOCK(hmebp); 3549 3550 return (hmeblkp); 3551 } 3552 3553 /* 3554 * This routine cleanup a previous shadow hmeblk and changes it to 3555 * a regular hblk. This happens rarely but it is possible 3556 * when a process wants to use large pages and there are hblks still 3557 * lying around from the previous as that used these hmeblks. 3558 * The alternative was to cleanup the shadow hblks at unload time 3559 * but since so few user processes actually use large pages, it is 3560 * better to be lazy and cleanup at this time. 3561 */ 3562 static void 3563 sfmmu_shadow_hcleanup(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, 3564 struct hmehash_bucket *hmebp) 3565 { 3566 caddr_t addr, endaddr; 3567 int hashno, size; 3568 3569 ASSERT(hmeblkp->hblk_shw_bit); 3570 ASSERT(!hmeblkp->hblk_shared); 3571 3572 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 3573 3574 if (!hmeblkp->hblk_shw_mask) { 3575 hmeblkp->hblk_shw_bit = 0; 3576 return; 3577 } 3578 addr = (caddr_t)get_hblk_base(hmeblkp); 3579 endaddr = get_hblk_endaddr(hmeblkp); 3580 size = get_hblk_ttesz(hmeblkp); 3581 hashno = size - 1; 3582 ASSERT(hashno > 0); 3583 SFMMU_HASH_UNLOCK(hmebp); 3584 3585 sfmmu_free_hblks(sfmmup, addr, endaddr, hashno); 3586 3587 SFMMU_HASH_LOCK(hmebp); 3588 } 3589 3590 static void 3591 sfmmu_free_hblks(sfmmu_t *sfmmup, caddr_t addr, caddr_t endaddr, 3592 int hashno) 3593 { 3594 int hmeshift, shadow = 0; 3595 hmeblk_tag hblktag; 3596 struct hmehash_bucket *hmebp; 3597 struct hme_blk *hmeblkp; 3598 struct hme_blk *nx_hblk, *pr_hblk, *list = NULL; 3599 uint64_t hblkpa, prevpa, nx_pa; 3600 3601 ASSERT(hashno > 0); 3602 hblktag.htag_id = sfmmup; 3603 hblktag.htag_rehash = hashno; 3604 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 3605 3606 hmeshift = HME_HASH_SHIFT(hashno); 3607 3608 while (addr < endaddr) { 3609 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 3610 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 3611 SFMMU_HASH_LOCK(hmebp); 3612 /* inline HME_HASH_SEARCH */ 3613 hmeblkp = hmebp->hmeblkp; 3614 hblkpa = hmebp->hmeh_nextpa; 3615 prevpa = 0; 3616 pr_hblk = NULL; 3617 while (hmeblkp) { 3618 ASSERT(hblkpa == va_to_pa((caddr_t)hmeblkp)); 3619 if (HTAGS_EQ(hmeblkp->hblk_tag, hblktag)) { 3620 /* found hme_blk */ 3621 ASSERT(!hmeblkp->hblk_shared); 3622 if (hmeblkp->hblk_shw_bit) { 3623 if (hmeblkp->hblk_shw_mask) { 3624 shadow = 1; 3625 sfmmu_shadow_hcleanup(sfmmup, 3626 hmeblkp, hmebp); 3627 break; 3628 } else { 3629 hmeblkp->hblk_shw_bit = 0; 3630 } 3631 } 3632 3633 /* 3634 * Hblk_hmecnt and hblk_vcnt could be non zero 3635 * since hblk_unload() does not gurantee that. 3636 * 3637 * XXX - this could cause tteload() to spin 3638 * where sfmmu_shadow_hcleanup() is called. 3639 */ 3640 } 3641 3642 nx_hblk = hmeblkp->hblk_next; 3643 nx_pa = hmeblkp->hblk_nextpa; 3644 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) { 3645 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, 3646 pr_hblk); 3647 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 3648 } else { 3649 pr_hblk = hmeblkp; 3650 prevpa = hblkpa; 3651 } 3652 hmeblkp = nx_hblk; 3653 hblkpa = nx_pa; 3654 } 3655 3656 SFMMU_HASH_UNLOCK(hmebp); 3657 3658 if (shadow) { 3659 /* 3660 * We found another shadow hblk so cleaned its 3661 * children. We need to go back and cleanup 3662 * the original hblk so we don't change the 3663 * addr. 3664 */ 3665 shadow = 0; 3666 } else { 3667 addr = (caddr_t)roundup((uintptr_t)addr + 1, 3668 (1 << hmeshift)); 3669 } 3670 } 3671 sfmmu_hblks_list_purge(&list); 3672 } 3673 3674 /* 3675 * This routine's job is to delete stale invalid shared hmeregions hmeblks that 3676 * may still linger on after pageunload. 3677 */ 3678 static void 3679 sfmmu_cleanup_rhblk(sf_srd_t *srdp, caddr_t addr, uint_t rid, int ttesz) 3680 { 3681 int hmeshift; 3682 hmeblk_tag hblktag; 3683 struct hmehash_bucket *hmebp; 3684 struct hme_blk *hmeblkp; 3685 struct hme_blk *pr_hblk; 3686 struct hme_blk *list = NULL; 3687 uint64_t hblkpa, prevpa; 3688 3689 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 3690 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 3691 3692 hmeshift = HME_HASH_SHIFT(ttesz); 3693 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 3694 hblktag.htag_rehash = ttesz; 3695 hblktag.htag_rid = rid; 3696 hblktag.htag_id = srdp; 3697 hmebp = HME_HASH_FUNCTION(srdp, addr, hmeshift); 3698 3699 SFMMU_HASH_LOCK(hmebp); 3700 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, hblkpa, pr_hblk, 3701 prevpa, &list); 3702 if (hmeblkp != NULL) { 3703 ASSERT(hmeblkp->hblk_shared); 3704 ASSERT(!hmeblkp->hblk_shw_bit); 3705 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) { 3706 panic("sfmmu_cleanup_rhblk: valid hmeblk"); 3707 } 3708 ASSERT(!hmeblkp->hblk_lckcnt); 3709 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, pr_hblk); 3710 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 3711 } 3712 SFMMU_HASH_UNLOCK(hmebp); 3713 sfmmu_hblks_list_purge(&list); 3714 } 3715 3716 /* ARGSUSED */ 3717 static void 3718 sfmmu_rgn_cb_noop(caddr_t saddr, caddr_t eaddr, caddr_t r_saddr, 3719 size_t r_size, void *r_obj, u_offset_t r_objoff) 3720 { 3721 } 3722 3723 /* 3724 * Searches for an hmeblk which maps addr, then unloads this mapping 3725 * and updates *eaddrp, if the hmeblk is found. 3726 */ 3727 static void 3728 sfmmu_unload_hmeregion_va(sf_srd_t *srdp, uint_t rid, caddr_t addr, 3729 caddr_t eaddr, int ttesz, caddr_t *eaddrp) 3730 { 3731 int hmeshift; 3732 hmeblk_tag hblktag; 3733 struct hmehash_bucket *hmebp; 3734 struct hme_blk *hmeblkp; 3735 struct hme_blk *pr_hblk; 3736 struct hme_blk *list = NULL; 3737 uint64_t hblkpa, prevpa; 3738 3739 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 3740 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 3741 ASSERT(ttesz >= HBLK_MIN_TTESZ); 3742 3743 hmeshift = HME_HASH_SHIFT(ttesz); 3744 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 3745 hblktag.htag_rehash = ttesz; 3746 hblktag.htag_rid = rid; 3747 hblktag.htag_id = srdp; 3748 hmebp = HME_HASH_FUNCTION(srdp, addr, hmeshift); 3749 3750 SFMMU_HASH_LOCK(hmebp); 3751 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, hblkpa, pr_hblk, 3752 prevpa, &list); 3753 if (hmeblkp != NULL) { 3754 ASSERT(hmeblkp->hblk_shared); 3755 ASSERT(!hmeblkp->hblk_lckcnt); 3756 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) { 3757 *eaddrp = sfmmu_hblk_unload(NULL, hmeblkp, addr, 3758 eaddr, NULL, HAT_UNLOAD); 3759 ASSERT(*eaddrp > addr); 3760 } 3761 ASSERT(!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt); 3762 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, pr_hblk); 3763 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 3764 } 3765 SFMMU_HASH_UNLOCK(hmebp); 3766 sfmmu_hblks_list_purge(&list); 3767 } 3768 3769 static void 3770 sfmmu_unload_hmeregion(sf_srd_t *srdp, sf_region_t *rgnp) 3771 { 3772 int ttesz = rgnp->rgn_pgszc; 3773 size_t rsz = rgnp->rgn_size; 3774 caddr_t rsaddr = rgnp->rgn_saddr; 3775 caddr_t readdr = rsaddr + rsz; 3776 caddr_t rhsaddr; 3777 caddr_t va; 3778 uint_t rid = rgnp->rgn_id; 3779 caddr_t cbsaddr; 3780 caddr_t cbeaddr; 3781 hat_rgn_cb_func_t rcbfunc; 3782 ulong_t cnt; 3783 3784 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 3785 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 3786 3787 ASSERT(IS_P2ALIGNED(rsaddr, TTEBYTES(ttesz))); 3788 ASSERT(IS_P2ALIGNED(rsz, TTEBYTES(ttesz))); 3789 if (ttesz < HBLK_MIN_TTESZ) { 3790 ttesz = HBLK_MIN_TTESZ; 3791 rhsaddr = (caddr_t)P2ALIGN((uintptr_t)rsaddr, HBLK_MIN_BYTES); 3792 } else { 3793 rhsaddr = rsaddr; 3794 } 3795 3796 if ((rcbfunc = rgnp->rgn_cb_function) == NULL) { 3797 rcbfunc = sfmmu_rgn_cb_noop; 3798 } 3799 3800 while (ttesz >= HBLK_MIN_TTESZ) { 3801 cbsaddr = rsaddr; 3802 cbeaddr = rsaddr; 3803 if (!(rgnp->rgn_hmeflags & (1 << ttesz))) { 3804 ttesz--; 3805 continue; 3806 } 3807 cnt = 0; 3808 va = rsaddr; 3809 while (va < readdr) { 3810 ASSERT(va >= rhsaddr); 3811 if (va != cbeaddr) { 3812 if (cbeaddr != cbsaddr) { 3813 ASSERT(cbeaddr > cbsaddr); 3814 (*rcbfunc)(cbsaddr, cbeaddr, 3815 rsaddr, rsz, rgnp->rgn_obj, 3816 rgnp->rgn_objoff); 3817 } 3818 cbsaddr = va; 3819 cbeaddr = va; 3820 } 3821 sfmmu_unload_hmeregion_va(srdp, rid, va, readdr, 3822 ttesz, &cbeaddr); 3823 cnt++; 3824 va = rhsaddr + (cnt << TTE_PAGE_SHIFT(ttesz)); 3825 } 3826 if (cbeaddr != cbsaddr) { 3827 ASSERT(cbeaddr > cbsaddr); 3828 (*rcbfunc)(cbsaddr, cbeaddr, rsaddr, 3829 rsz, rgnp->rgn_obj, 3830 rgnp->rgn_objoff); 3831 } 3832 ttesz--; 3833 } 3834 } 3835 3836 /* 3837 * Release one hardware address translation lock on the given address range. 3838 */ 3839 void 3840 hat_unlock(struct hat *sfmmup, caddr_t addr, size_t len) 3841 { 3842 struct hmehash_bucket *hmebp; 3843 hmeblk_tag hblktag; 3844 int hmeshift, hashno = 1; 3845 struct hme_blk *hmeblkp, *list = NULL; 3846 caddr_t endaddr; 3847 3848 ASSERT(sfmmup != NULL); 3849 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 3850 3851 ASSERT((sfmmup == ksfmmup) || 3852 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 3853 ASSERT((len & MMU_PAGEOFFSET) == 0); 3854 endaddr = addr + len; 3855 hblktag.htag_id = sfmmup; 3856 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 3857 3858 /* 3859 * Spitfire supports 4 page sizes. 3860 * Most pages are expected to be of the smallest page size (8K) and 3861 * these will not need to be rehashed. 64K pages also don't need to be 3862 * rehashed because an hmeblk spans 64K of address space. 512K pages 3863 * might need 1 rehash and and 4M pages might need 2 rehashes. 3864 */ 3865 while (addr < endaddr) { 3866 hmeshift = HME_HASH_SHIFT(hashno); 3867 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 3868 hblktag.htag_rehash = hashno; 3869 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 3870 3871 SFMMU_HASH_LOCK(hmebp); 3872 3873 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 3874 if (hmeblkp != NULL) { 3875 ASSERT(!hmeblkp->hblk_shared); 3876 /* 3877 * If we encounter a shadow hmeblk then 3878 * we know there are no valid hmeblks mapping 3879 * this address at this size or larger. 3880 * Just increment address by the smallest 3881 * page size. 3882 */ 3883 if (hmeblkp->hblk_shw_bit) { 3884 addr += MMU_PAGESIZE; 3885 } else { 3886 addr = sfmmu_hblk_unlock(hmeblkp, addr, 3887 endaddr); 3888 } 3889 SFMMU_HASH_UNLOCK(hmebp); 3890 hashno = 1; 3891 continue; 3892 } 3893 SFMMU_HASH_UNLOCK(hmebp); 3894 3895 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 3896 /* 3897 * We have traversed the whole list and rehashed 3898 * if necessary without finding the address to unlock 3899 * which should never happen. 3900 */ 3901 panic("sfmmu_unlock: addr not found. " 3902 "addr %p hat %p", (void *)addr, (void *)sfmmup); 3903 } else { 3904 hashno++; 3905 } 3906 } 3907 3908 sfmmu_hblks_list_purge(&list); 3909 } 3910 3911 void 3912 hat_unlock_region(struct hat *sfmmup, caddr_t addr, size_t len, 3913 hat_region_cookie_t rcookie) 3914 { 3915 sf_srd_t *srdp; 3916 sf_region_t *rgnp; 3917 int ttesz; 3918 uint_t rid; 3919 caddr_t eaddr; 3920 caddr_t va; 3921 int hmeshift; 3922 hmeblk_tag hblktag; 3923 struct hmehash_bucket *hmebp; 3924 struct hme_blk *hmeblkp; 3925 struct hme_blk *pr_hblk; 3926 struct hme_blk *list; 3927 uint64_t hblkpa, prevpa; 3928 3929 if (rcookie == HAT_INVALID_REGION_COOKIE) { 3930 hat_unlock(sfmmup, addr, len); 3931 return; 3932 } 3933 3934 ASSERT(sfmmup != NULL); 3935 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 3936 ASSERT(sfmmup != ksfmmup); 3937 3938 srdp = sfmmup->sfmmu_srdp; 3939 rid = (uint_t)((uint64_t)rcookie); 3940 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 3941 eaddr = addr + len; 3942 va = addr; 3943 list = NULL; 3944 rgnp = srdp->srd_hmergnp[rid]; 3945 SFMMU_VALIDATE_HMERID(sfmmup, rid, addr, len); 3946 3947 ASSERT(IS_P2ALIGNED(addr, TTEBYTES(rgnp->rgn_pgszc))); 3948 ASSERT(IS_P2ALIGNED(len, TTEBYTES(rgnp->rgn_pgszc))); 3949 if (rgnp->rgn_pgszc < HBLK_MIN_TTESZ) { 3950 ttesz = HBLK_MIN_TTESZ; 3951 } else { 3952 ttesz = rgnp->rgn_pgszc; 3953 } 3954 while (va < eaddr) { 3955 while (ttesz < rgnp->rgn_pgszc && 3956 IS_P2ALIGNED(va, TTEBYTES(ttesz + 1))) { 3957 ttesz++; 3958 } 3959 while (ttesz >= HBLK_MIN_TTESZ) { 3960 if (!(rgnp->rgn_hmeflags & (1 << ttesz))) { 3961 ttesz--; 3962 continue; 3963 } 3964 hmeshift = HME_HASH_SHIFT(ttesz); 3965 hblktag.htag_bspage = HME_HASH_BSPAGE(va, hmeshift); 3966 hblktag.htag_rehash = ttesz; 3967 hblktag.htag_rid = rid; 3968 hblktag.htag_id = srdp; 3969 hmebp = HME_HASH_FUNCTION(srdp, addr, hmeshift); 3970 SFMMU_HASH_LOCK(hmebp); 3971 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, hblkpa, 3972 pr_hblk, prevpa, &list); 3973 if (hmeblkp == NULL) { 3974 SFMMU_HASH_UNLOCK(hmebp); 3975 ttesz--; 3976 continue; 3977 } 3978 ASSERT(hmeblkp->hblk_shared); 3979 va = sfmmu_hblk_unlock(hmeblkp, va, eaddr); 3980 ASSERT(va >= eaddr || 3981 IS_P2ALIGNED((uintptr_t)va, TTEBYTES(ttesz))); 3982 SFMMU_HASH_UNLOCK(hmebp); 3983 break; 3984 } 3985 if (ttesz < HBLK_MIN_TTESZ) { 3986 panic("hat_unlock_region: addr not found " 3987 "addr %p hat %p", va, sfmmup); 3988 } 3989 } 3990 sfmmu_hblks_list_purge(&list); 3991 } 3992 3993 /* 3994 * Function to unlock a range of addresses in an hmeblk. It returns the 3995 * next address that needs to be unlocked. 3996 * Should be called with the hash lock held. 3997 */ 3998 static caddr_t 3999 sfmmu_hblk_unlock(struct hme_blk *hmeblkp, caddr_t addr, caddr_t endaddr) 4000 { 4001 struct sf_hment *sfhme; 4002 tte_t tteold, ttemod; 4003 int ttesz, ret; 4004 4005 ASSERT(in_hblk_range(hmeblkp, addr)); 4006 ASSERT(hmeblkp->hblk_shw_bit == 0); 4007 4008 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 4009 ttesz = get_hblk_ttesz(hmeblkp); 4010 4011 HBLKTOHME(sfhme, hmeblkp, addr); 4012 while (addr < endaddr) { 4013 readtte: 4014 sfmmu_copytte(&sfhme->hme_tte, &tteold); 4015 if (TTE_IS_VALID(&tteold)) { 4016 4017 ttemod = tteold; 4018 4019 ret = sfmmu_modifytte_try(&tteold, &ttemod, 4020 &sfhme->hme_tte); 4021 4022 if (ret < 0) 4023 goto readtte; 4024 4025 if (hmeblkp->hblk_lckcnt == 0) 4026 panic("zero hblk lckcnt"); 4027 4028 if (((uintptr_t)addr + TTEBYTES(ttesz)) > 4029 (uintptr_t)endaddr) 4030 panic("can't unlock large tte"); 4031 4032 ASSERT(hmeblkp->hblk_lckcnt > 0); 4033 atomic_add_32(&hmeblkp->hblk_lckcnt, -1); 4034 HBLK_STACK_TRACE(hmeblkp, HBLK_UNLOCK); 4035 } else { 4036 panic("sfmmu_hblk_unlock: invalid tte"); 4037 } 4038 addr += TTEBYTES(ttesz); 4039 sfhme++; 4040 } 4041 return (addr); 4042 } 4043 4044 /* 4045 * Physical Address Mapping Framework 4046 * 4047 * General rules: 4048 * 4049 * (1) Applies only to seg_kmem memory pages. To make things easier, 4050 * seg_kpm addresses are also accepted by the routines, but nothing 4051 * is done with them since by definition their PA mappings are static. 4052 * (2) hat_add_callback() may only be called while holding the page lock 4053 * SE_SHARED or SE_EXCL of the underlying page (e.g., as_pagelock()), 4054 * or passing HAC_PAGELOCK flag. 4055 * (3) prehandler() and posthandler() may not call hat_add_callback() or 4056 * hat_delete_callback(), nor should they allocate memory. Post quiesce 4057 * callbacks may not sleep or acquire adaptive mutex locks. 4058 * (4) Either prehandler() or posthandler() (but not both) may be specified 4059 * as being NULL. Specifying an errhandler() is optional. 4060 * 4061 * Details of using the framework: 4062 * 4063 * registering a callback (hat_register_callback()) 4064 * 4065 * Pass prehandler, posthandler, errhandler addresses 4066 * as described below. If capture_cpus argument is nonzero, 4067 * suspend callback to the prehandler will occur with CPUs 4068 * captured and executing xc_loop() and CPUs will remain 4069 * captured until after the posthandler suspend callback 4070 * occurs. 4071 * 4072 * adding a callback (hat_add_callback()) 4073 * 4074 * as_pagelock(); 4075 * hat_add_callback(); 4076 * save returned pfn in private data structures or program registers; 4077 * as_pageunlock(); 4078 * 4079 * prehandler() 4080 * 4081 * Stop all accesses by physical address to this memory page. 4082 * Called twice: the first, PRESUSPEND, is a context safe to acquire 4083 * adaptive locks. The second, SUSPEND, is called at high PIL with 4084 * CPUs captured so adaptive locks may NOT be acquired (and all spin 4085 * locks must be XCALL_PIL or higher locks). 4086 * 4087 * May return the following errors: 4088 * EIO: A fatal error has occurred. This will result in panic. 4089 * EAGAIN: The page cannot be suspended. This will fail the 4090 * relocation. 4091 * 0: Success. 4092 * 4093 * posthandler() 4094 * 4095 * Save new pfn in private data structures or program registers; 4096 * not allowed to fail (non-zero return values will result in panic). 4097 * 4098 * errhandler() 4099 * 4100 * called when an error occurs related to the callback. Currently 4101 * the only such error is HAT_CB_ERR_LEAKED which indicates that 4102 * a page is being freed, but there are still outstanding callback(s) 4103 * registered on the page. 4104 * 4105 * removing a callback (hat_delete_callback(); e.g., prior to freeing memory) 4106 * 4107 * stop using physical address 4108 * hat_delete_callback(); 4109 * 4110 */ 4111 4112 /* 4113 * Register a callback class. Each subsystem should do this once and 4114 * cache the id_t returned for use in setting up and tearing down callbacks. 4115 * 4116 * There is no facility for removing callback IDs once they are created; 4117 * the "key" should be unique for each module, so in case a module is unloaded 4118 * and subsequently re-loaded, we can recycle the module's previous entry. 4119 */ 4120 id_t 4121 hat_register_callback(int key, 4122 int (*prehandler)(caddr_t, uint_t, uint_t, void *), 4123 int (*posthandler)(caddr_t, uint_t, uint_t, void *, pfn_t), 4124 int (*errhandler)(caddr_t, uint_t, uint_t, void *), 4125 int capture_cpus) 4126 { 4127 id_t id; 4128 4129 /* 4130 * Search the table for a pre-existing callback associated with 4131 * the identifier "key". If one exists, we re-use that entry in 4132 * the table for this instance, otherwise we assign the next 4133 * available table slot. 4134 */ 4135 for (id = 0; id < sfmmu_max_cb_id; id++) { 4136 if (sfmmu_cb_table[id].key == key) 4137 break; 4138 } 4139 4140 if (id == sfmmu_max_cb_id) { 4141 id = sfmmu_cb_nextid++; 4142 if (id >= sfmmu_max_cb_id) 4143 panic("hat_register_callback: out of callback IDs"); 4144 } 4145 4146 ASSERT(prehandler != NULL || posthandler != NULL); 4147 4148 sfmmu_cb_table[id].key = key; 4149 sfmmu_cb_table[id].prehandler = prehandler; 4150 sfmmu_cb_table[id].posthandler = posthandler; 4151 sfmmu_cb_table[id].errhandler = errhandler; 4152 sfmmu_cb_table[id].capture_cpus = capture_cpus; 4153 4154 return (id); 4155 } 4156 4157 #define HAC_COOKIE_NONE (void *)-1 4158 4159 /* 4160 * Add relocation callbacks to the specified addr/len which will be called 4161 * when relocating the associated page. See the description of pre and 4162 * posthandler above for more details. 4163 * 4164 * If HAC_PAGELOCK is included in flags, the underlying memory page is 4165 * locked internally so the caller must be able to deal with the callback 4166 * running even before this function has returned. If HAC_PAGELOCK is not 4167 * set, it is assumed that the underlying memory pages are locked. 4168 * 4169 * Since the caller must track the individual page boundaries anyway, 4170 * we only allow a callback to be added to a single page (large 4171 * or small). Thus [addr, addr + len) MUST be contained within a single 4172 * page. 4173 * 4174 * Registering multiple callbacks on the same [addr, addr+len) is supported, 4175 * _provided_that_ a unique parameter is specified for each callback. 4176 * If multiple callbacks are registered on the same range the callback will 4177 * be invoked with each unique parameter. Registering the same callback with 4178 * the same argument more than once will result in corrupted kernel state. 4179 * 4180 * Returns the pfn of the underlying kernel page in *rpfn 4181 * on success, or PFN_INVALID on failure. 4182 * 4183 * cookiep (if passed) provides storage space for an opaque cookie 4184 * to return later to hat_delete_callback(). This cookie makes the callback 4185 * deletion significantly quicker by avoiding a potentially lengthy hash 4186 * search. 4187 * 4188 * Returns values: 4189 * 0: success 4190 * ENOMEM: memory allocation failure (e.g. flags was passed as HAC_NOSLEEP) 4191 * EINVAL: callback ID is not valid 4192 * ENXIO: ["vaddr", "vaddr" + len) is not mapped in the kernel's address 4193 * space 4194 * ERANGE: ["vaddr", "vaddr" + len) crosses a page boundary 4195 */ 4196 int 4197 hat_add_callback(id_t callback_id, caddr_t vaddr, uint_t len, uint_t flags, 4198 void *pvt, pfn_t *rpfn, void **cookiep) 4199 { 4200 struct hmehash_bucket *hmebp; 4201 hmeblk_tag hblktag; 4202 struct hme_blk *hmeblkp; 4203 int hmeshift, hashno; 4204 caddr_t saddr, eaddr, baseaddr; 4205 struct pa_hment *pahmep; 4206 struct sf_hment *sfhmep, *osfhmep; 4207 kmutex_t *pml; 4208 tte_t tte; 4209 page_t *pp; 4210 vnode_t *vp; 4211 u_offset_t off; 4212 pfn_t pfn; 4213 int kmflags = (flags & HAC_SLEEP)? KM_SLEEP : KM_NOSLEEP; 4214 int locked = 0; 4215 4216 /* 4217 * For KPM mappings, just return the physical address since we 4218 * don't need to register any callbacks. 4219 */ 4220 if (IS_KPM_ADDR(vaddr)) { 4221 uint64_t paddr; 4222 SFMMU_KPM_VTOP(vaddr, paddr); 4223 *rpfn = btop(paddr); 4224 if (cookiep != NULL) 4225 *cookiep = HAC_COOKIE_NONE; 4226 return (0); 4227 } 4228 4229 if (callback_id < (id_t)0 || callback_id >= sfmmu_cb_nextid) { 4230 *rpfn = PFN_INVALID; 4231 return (EINVAL); 4232 } 4233 4234 if ((pahmep = kmem_cache_alloc(pa_hment_cache, kmflags)) == NULL) { 4235 *rpfn = PFN_INVALID; 4236 return (ENOMEM); 4237 } 4238 4239 sfhmep = &pahmep->sfment; 4240 4241 saddr = (caddr_t)((uintptr_t)vaddr & MMU_PAGEMASK); 4242 eaddr = saddr + len; 4243 4244 rehash: 4245 /* Find the mapping(s) for this page */ 4246 for (hashno = TTE64K, hmeblkp = NULL; 4247 hmeblkp == NULL && hashno <= mmu_hashcnt; 4248 hashno++) { 4249 hmeshift = HME_HASH_SHIFT(hashno); 4250 hblktag.htag_id = ksfmmup; 4251 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 4252 hblktag.htag_bspage = HME_HASH_BSPAGE(saddr, hmeshift); 4253 hblktag.htag_rehash = hashno; 4254 hmebp = HME_HASH_FUNCTION(ksfmmup, saddr, hmeshift); 4255 4256 SFMMU_HASH_LOCK(hmebp); 4257 4258 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 4259 4260 if (hmeblkp == NULL) 4261 SFMMU_HASH_UNLOCK(hmebp); 4262 } 4263 4264 if (hmeblkp == NULL) { 4265 kmem_cache_free(pa_hment_cache, pahmep); 4266 *rpfn = PFN_INVALID; 4267 return (ENXIO); 4268 } 4269 4270 ASSERT(!hmeblkp->hblk_shared); 4271 4272 HBLKTOHME(osfhmep, hmeblkp, saddr); 4273 sfmmu_copytte(&osfhmep->hme_tte, &tte); 4274 4275 if (!TTE_IS_VALID(&tte)) { 4276 SFMMU_HASH_UNLOCK(hmebp); 4277 kmem_cache_free(pa_hment_cache, pahmep); 4278 *rpfn = PFN_INVALID; 4279 return (ENXIO); 4280 } 4281 4282 /* 4283 * Make sure the boundaries for the callback fall within this 4284 * single mapping. 4285 */ 4286 baseaddr = (caddr_t)get_hblk_base(hmeblkp); 4287 ASSERT(saddr >= baseaddr); 4288 if (eaddr > saddr + TTEBYTES(TTE_CSZ(&tte))) { 4289 SFMMU_HASH_UNLOCK(hmebp); 4290 kmem_cache_free(pa_hment_cache, pahmep); 4291 *rpfn = PFN_INVALID; 4292 return (ERANGE); 4293 } 4294 4295 pfn = sfmmu_ttetopfn(&tte, vaddr); 4296 4297 /* 4298 * The pfn may not have a page_t underneath in which case we 4299 * just return it. This can happen if we are doing I/O to a 4300 * static portion of the kernel's address space, for instance. 4301 */ 4302 pp = osfhmep->hme_page; 4303 if (pp == NULL) { 4304 SFMMU_HASH_UNLOCK(hmebp); 4305 kmem_cache_free(pa_hment_cache, pahmep); 4306 *rpfn = pfn; 4307 if (cookiep) 4308 *cookiep = HAC_COOKIE_NONE; 4309 return (0); 4310 } 4311 ASSERT(pp == PP_PAGEROOT(pp)); 4312 4313 vp = pp->p_vnode; 4314 off = pp->p_offset; 4315 4316 pml = sfmmu_mlist_enter(pp); 4317 4318 if (flags & HAC_PAGELOCK) { 4319 if (!page_trylock(pp, SE_SHARED)) { 4320 /* 4321 * Somebody is holding SE_EXCL lock. Might 4322 * even be hat_page_relocate(). Drop all 4323 * our locks, lookup the page in &kvp, and 4324 * retry. If it doesn't exist in &kvp and &zvp, 4325 * then we must be dealing with a kernel mapped 4326 * page which doesn't actually belong to 4327 * segkmem so we punt. 4328 */ 4329 sfmmu_mlist_exit(pml); 4330 SFMMU_HASH_UNLOCK(hmebp); 4331 pp = page_lookup(&kvp, (u_offset_t)saddr, SE_SHARED); 4332 4333 /* check zvp before giving up */ 4334 if (pp == NULL) 4335 pp = page_lookup(&zvp, (u_offset_t)saddr, 4336 SE_SHARED); 4337 4338 /* Okay, we didn't find it, give up */ 4339 if (pp == NULL) { 4340 kmem_cache_free(pa_hment_cache, pahmep); 4341 *rpfn = pfn; 4342 if (cookiep) 4343 *cookiep = HAC_COOKIE_NONE; 4344 return (0); 4345 } 4346 page_unlock(pp); 4347 goto rehash; 4348 } 4349 locked = 1; 4350 } 4351 4352 if (!PAGE_LOCKED(pp) && !panicstr) 4353 panic("hat_add_callback: page 0x%p not locked", pp); 4354 4355 if (osfhmep->hme_page != pp || pp->p_vnode != vp || 4356 pp->p_offset != off) { 4357 /* 4358 * The page moved before we got our hands on it. Drop 4359 * all the locks and try again. 4360 */ 4361 ASSERT((flags & HAC_PAGELOCK) != 0); 4362 sfmmu_mlist_exit(pml); 4363 SFMMU_HASH_UNLOCK(hmebp); 4364 page_unlock(pp); 4365 locked = 0; 4366 goto rehash; 4367 } 4368 4369 if (!VN_ISKAS(vp)) { 4370 /* 4371 * This is not a segkmem page but another page which 4372 * has been kernel mapped. It had better have at least 4373 * a share lock on it. Return the pfn. 4374 */ 4375 sfmmu_mlist_exit(pml); 4376 SFMMU_HASH_UNLOCK(hmebp); 4377 if (locked) 4378 page_unlock(pp); 4379 kmem_cache_free(pa_hment_cache, pahmep); 4380 ASSERT(PAGE_LOCKED(pp)); 4381 *rpfn = pfn; 4382 if (cookiep) 4383 *cookiep = HAC_COOKIE_NONE; 4384 return (0); 4385 } 4386 4387 /* 4388 * Setup this pa_hment and link its embedded dummy sf_hment into 4389 * the mapping list. 4390 */ 4391 pp->p_share++; 4392 pahmep->cb_id = callback_id; 4393 pahmep->addr = vaddr; 4394 pahmep->len = len; 4395 pahmep->refcnt = 1; 4396 pahmep->flags = 0; 4397 pahmep->pvt = pvt; 4398 4399 sfhmep->hme_tte.ll = 0; 4400 sfhmep->hme_data = pahmep; 4401 sfhmep->hme_prev = osfhmep; 4402 sfhmep->hme_next = osfhmep->hme_next; 4403 4404 if (osfhmep->hme_next) 4405 osfhmep->hme_next->hme_prev = sfhmep; 4406 4407 osfhmep->hme_next = sfhmep; 4408 4409 sfmmu_mlist_exit(pml); 4410 SFMMU_HASH_UNLOCK(hmebp); 4411 4412 if (locked) 4413 page_unlock(pp); 4414 4415 *rpfn = pfn; 4416 if (cookiep) 4417 *cookiep = (void *)pahmep; 4418 4419 return (0); 4420 } 4421 4422 /* 4423 * Remove the relocation callbacks from the specified addr/len. 4424 */ 4425 void 4426 hat_delete_callback(caddr_t vaddr, uint_t len, void *pvt, uint_t flags, 4427 void *cookie) 4428 { 4429 struct hmehash_bucket *hmebp; 4430 hmeblk_tag hblktag; 4431 struct hme_blk *hmeblkp; 4432 int hmeshift, hashno; 4433 caddr_t saddr; 4434 struct pa_hment *pahmep; 4435 struct sf_hment *sfhmep, *osfhmep; 4436 kmutex_t *pml; 4437 tte_t tte; 4438 page_t *pp; 4439 vnode_t *vp; 4440 u_offset_t off; 4441 int locked = 0; 4442 4443 /* 4444 * If the cookie is HAC_COOKIE_NONE then there is no pa_hment to 4445 * remove so just return. 4446 */ 4447 if (cookie == HAC_COOKIE_NONE || IS_KPM_ADDR(vaddr)) 4448 return; 4449 4450 saddr = (caddr_t)((uintptr_t)vaddr & MMU_PAGEMASK); 4451 4452 rehash: 4453 /* Find the mapping(s) for this page */ 4454 for (hashno = TTE64K, hmeblkp = NULL; 4455 hmeblkp == NULL && hashno <= mmu_hashcnt; 4456 hashno++) { 4457 hmeshift = HME_HASH_SHIFT(hashno); 4458 hblktag.htag_id = ksfmmup; 4459 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 4460 hblktag.htag_bspage = HME_HASH_BSPAGE(saddr, hmeshift); 4461 hblktag.htag_rehash = hashno; 4462 hmebp = HME_HASH_FUNCTION(ksfmmup, saddr, hmeshift); 4463 4464 SFMMU_HASH_LOCK(hmebp); 4465 4466 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 4467 4468 if (hmeblkp == NULL) 4469 SFMMU_HASH_UNLOCK(hmebp); 4470 } 4471 4472 if (hmeblkp == NULL) 4473 return; 4474 4475 ASSERT(!hmeblkp->hblk_shared); 4476 4477 HBLKTOHME(osfhmep, hmeblkp, saddr); 4478 4479 sfmmu_copytte(&osfhmep->hme_tte, &tte); 4480 if (!TTE_IS_VALID(&tte)) { 4481 SFMMU_HASH_UNLOCK(hmebp); 4482 return; 4483 } 4484 4485 pp = osfhmep->hme_page; 4486 if (pp == NULL) { 4487 SFMMU_HASH_UNLOCK(hmebp); 4488 ASSERT(cookie == NULL); 4489 return; 4490 } 4491 4492 vp = pp->p_vnode; 4493 off = pp->p_offset; 4494 4495 pml = sfmmu_mlist_enter(pp); 4496 4497 if (flags & HAC_PAGELOCK) { 4498 if (!page_trylock(pp, SE_SHARED)) { 4499 /* 4500 * Somebody is holding SE_EXCL lock. Might 4501 * even be hat_page_relocate(). Drop all 4502 * our locks, lookup the page in &kvp, and 4503 * retry. If it doesn't exist in &kvp and &zvp, 4504 * then we must be dealing with a kernel mapped 4505 * page which doesn't actually belong to 4506 * segkmem so we punt. 4507 */ 4508 sfmmu_mlist_exit(pml); 4509 SFMMU_HASH_UNLOCK(hmebp); 4510 pp = page_lookup(&kvp, (u_offset_t)saddr, SE_SHARED); 4511 /* check zvp before giving up */ 4512 if (pp == NULL) 4513 pp = page_lookup(&zvp, (u_offset_t)saddr, 4514 SE_SHARED); 4515 4516 if (pp == NULL) { 4517 ASSERT(cookie == NULL); 4518 return; 4519 } 4520 page_unlock(pp); 4521 goto rehash; 4522 } 4523 locked = 1; 4524 } 4525 4526 ASSERT(PAGE_LOCKED(pp)); 4527 4528 if (osfhmep->hme_page != pp || pp->p_vnode != vp || 4529 pp->p_offset != off) { 4530 /* 4531 * The page moved before we got our hands on it. Drop 4532 * all the locks and try again. 4533 */ 4534 ASSERT((flags & HAC_PAGELOCK) != 0); 4535 sfmmu_mlist_exit(pml); 4536 SFMMU_HASH_UNLOCK(hmebp); 4537 page_unlock(pp); 4538 locked = 0; 4539 goto rehash; 4540 } 4541 4542 if (!VN_ISKAS(vp)) { 4543 /* 4544 * This is not a segkmem page but another page which 4545 * has been kernel mapped. 4546 */ 4547 sfmmu_mlist_exit(pml); 4548 SFMMU_HASH_UNLOCK(hmebp); 4549 if (locked) 4550 page_unlock(pp); 4551 ASSERT(cookie == NULL); 4552 return; 4553 } 4554 4555 if (cookie != NULL) { 4556 pahmep = (struct pa_hment *)cookie; 4557 sfhmep = &pahmep->sfment; 4558 } else { 4559 for (sfhmep = pp->p_mapping; sfhmep != NULL; 4560 sfhmep = sfhmep->hme_next) { 4561 4562 /* 4563 * skip va<->pa mappings 4564 */ 4565 if (!IS_PAHME(sfhmep)) 4566 continue; 4567 4568 pahmep = sfhmep->hme_data; 4569 ASSERT(pahmep != NULL); 4570 4571 /* 4572 * if pa_hment matches, remove it 4573 */ 4574 if ((pahmep->pvt == pvt) && 4575 (pahmep->addr == vaddr) && 4576 (pahmep->len == len)) { 4577 break; 4578 } 4579 } 4580 } 4581 4582 if (sfhmep == NULL) { 4583 if (!panicstr) { 4584 panic("hat_delete_callback: pa_hment not found, pp %p", 4585 (void *)pp); 4586 } 4587 return; 4588 } 4589 4590 /* 4591 * Note: at this point a valid kernel mapping must still be 4592 * present on this page. 4593 */ 4594 pp->p_share--; 4595 if (pp->p_share <= 0) 4596 panic("hat_delete_callback: zero p_share"); 4597 4598 if (--pahmep->refcnt == 0) { 4599 if (pahmep->flags != 0) 4600 panic("hat_delete_callback: pa_hment is busy"); 4601 4602 /* 4603 * Remove sfhmep from the mapping list for the page. 4604 */ 4605 if (sfhmep->hme_prev) { 4606 sfhmep->hme_prev->hme_next = sfhmep->hme_next; 4607 } else { 4608 pp->p_mapping = sfhmep->hme_next; 4609 } 4610 4611 if (sfhmep->hme_next) 4612 sfhmep->hme_next->hme_prev = sfhmep->hme_prev; 4613 4614 sfmmu_mlist_exit(pml); 4615 SFMMU_HASH_UNLOCK(hmebp); 4616 4617 if (locked) 4618 page_unlock(pp); 4619 4620 kmem_cache_free(pa_hment_cache, pahmep); 4621 return; 4622 } 4623 4624 sfmmu_mlist_exit(pml); 4625 SFMMU_HASH_UNLOCK(hmebp); 4626 if (locked) 4627 page_unlock(pp); 4628 } 4629 4630 /* 4631 * hat_probe returns 1 if the translation for the address 'addr' is 4632 * loaded, zero otherwise. 4633 * 4634 * hat_probe should be used only for advisorary purposes because it may 4635 * occasionally return the wrong value. The implementation must guarantee that 4636 * returning the wrong value is a very rare event. hat_probe is used 4637 * to implement optimizations in the segment drivers. 4638 * 4639 */ 4640 int 4641 hat_probe(struct hat *sfmmup, caddr_t addr) 4642 { 4643 pfn_t pfn; 4644 tte_t tte; 4645 4646 ASSERT(sfmmup != NULL); 4647 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 4648 4649 ASSERT((sfmmup == ksfmmup) || 4650 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 4651 4652 if (sfmmup == ksfmmup) { 4653 while ((pfn = sfmmu_vatopfn(addr, sfmmup, &tte)) 4654 == PFN_SUSPENDED) { 4655 sfmmu_vatopfn_suspended(addr, sfmmup, &tte); 4656 } 4657 } else { 4658 pfn = sfmmu_uvatopfn(addr, sfmmup, NULL); 4659 } 4660 4661 if (pfn != PFN_INVALID) 4662 return (1); 4663 else 4664 return (0); 4665 } 4666 4667 ssize_t 4668 hat_getpagesize(struct hat *sfmmup, caddr_t addr) 4669 { 4670 tte_t tte; 4671 4672 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 4673 4674 if (sfmmup == ksfmmup) { 4675 if (sfmmu_vatopfn(addr, sfmmup, &tte) == PFN_INVALID) { 4676 return (-1); 4677 } 4678 } else { 4679 if (sfmmu_uvatopfn(addr, sfmmup, &tte) == PFN_INVALID) { 4680 return (-1); 4681 } 4682 } 4683 4684 ASSERT(TTE_IS_VALID(&tte)); 4685 return (TTEBYTES(TTE_CSZ(&tte))); 4686 } 4687 4688 uint_t 4689 hat_getattr(struct hat *sfmmup, caddr_t addr, uint_t *attr) 4690 { 4691 tte_t tte; 4692 4693 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 4694 4695 if (sfmmup == ksfmmup) { 4696 if (sfmmu_vatopfn(addr, sfmmup, &tte) == PFN_INVALID) { 4697 tte.ll = 0; 4698 } 4699 } else { 4700 if (sfmmu_uvatopfn(addr, sfmmup, &tte) == PFN_INVALID) { 4701 tte.ll = 0; 4702 } 4703 } 4704 if (TTE_IS_VALID(&tte)) { 4705 *attr = sfmmu_ptov_attr(&tte); 4706 return (0); 4707 } 4708 *attr = 0; 4709 return ((uint_t)0xffffffff); 4710 } 4711 4712 /* 4713 * Enables more attributes on specified address range (ie. logical OR) 4714 */ 4715 void 4716 hat_setattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr) 4717 { 4718 if (hat->sfmmu_xhat_provider) { 4719 XHAT_SETATTR(hat, addr, len, attr); 4720 return; 4721 } else { 4722 /* 4723 * This must be a CPU HAT. If the address space has 4724 * XHATs attached, change attributes for all of them, 4725 * just in case 4726 */ 4727 ASSERT(hat->sfmmu_as != NULL); 4728 if (hat->sfmmu_as->a_xhat != NULL) 4729 xhat_setattr_all(hat->sfmmu_as, addr, len, attr); 4730 } 4731 4732 sfmmu_chgattr(hat, addr, len, attr, SFMMU_SETATTR); 4733 } 4734 4735 /* 4736 * Assigns attributes to the specified address range. All the attributes 4737 * are specified. 4738 */ 4739 void 4740 hat_chgattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr) 4741 { 4742 if (hat->sfmmu_xhat_provider) { 4743 XHAT_CHGATTR(hat, addr, len, attr); 4744 return; 4745 } else { 4746 /* 4747 * This must be a CPU HAT. If the address space has 4748 * XHATs attached, change attributes for all of them, 4749 * just in case 4750 */ 4751 ASSERT(hat->sfmmu_as != NULL); 4752 if (hat->sfmmu_as->a_xhat != NULL) 4753 xhat_chgattr_all(hat->sfmmu_as, addr, len, attr); 4754 } 4755 4756 sfmmu_chgattr(hat, addr, len, attr, SFMMU_CHGATTR); 4757 } 4758 4759 /* 4760 * Remove attributes on the specified address range (ie. loginal NAND) 4761 */ 4762 void 4763 hat_clrattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr) 4764 { 4765 if (hat->sfmmu_xhat_provider) { 4766 XHAT_CLRATTR(hat, addr, len, attr); 4767 return; 4768 } else { 4769 /* 4770 * This must be a CPU HAT. If the address space has 4771 * XHATs attached, change attributes for all of them, 4772 * just in case 4773 */ 4774 ASSERT(hat->sfmmu_as != NULL); 4775 if (hat->sfmmu_as->a_xhat != NULL) 4776 xhat_clrattr_all(hat->sfmmu_as, addr, len, attr); 4777 } 4778 4779 sfmmu_chgattr(hat, addr, len, attr, SFMMU_CLRATTR); 4780 } 4781 4782 /* 4783 * Change attributes on an address range to that specified by attr and mode. 4784 */ 4785 static void 4786 sfmmu_chgattr(struct hat *sfmmup, caddr_t addr, size_t len, uint_t attr, 4787 int mode) 4788 { 4789 struct hmehash_bucket *hmebp; 4790 hmeblk_tag hblktag; 4791 int hmeshift, hashno = 1; 4792 struct hme_blk *hmeblkp, *list = NULL; 4793 caddr_t endaddr; 4794 cpuset_t cpuset; 4795 demap_range_t dmr; 4796 4797 CPUSET_ZERO(cpuset); 4798 4799 ASSERT((sfmmup == ksfmmup) || 4800 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 4801 ASSERT((len & MMU_PAGEOFFSET) == 0); 4802 ASSERT(((uintptr_t)addr & MMU_PAGEOFFSET) == 0); 4803 4804 if ((attr & PROT_USER) && (mode != SFMMU_CLRATTR) && 4805 ((addr + len) > (caddr_t)USERLIMIT)) { 4806 panic("user addr %p in kernel space", 4807 (void *)addr); 4808 } 4809 4810 endaddr = addr + len; 4811 hblktag.htag_id = sfmmup; 4812 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 4813 DEMAP_RANGE_INIT(sfmmup, &dmr); 4814 4815 while (addr < endaddr) { 4816 hmeshift = HME_HASH_SHIFT(hashno); 4817 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 4818 hblktag.htag_rehash = hashno; 4819 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 4820 4821 SFMMU_HASH_LOCK(hmebp); 4822 4823 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 4824 if (hmeblkp != NULL) { 4825 ASSERT(!hmeblkp->hblk_shared); 4826 /* 4827 * We've encountered a shadow hmeblk so skip the range 4828 * of the next smaller mapping size. 4829 */ 4830 if (hmeblkp->hblk_shw_bit) { 4831 ASSERT(sfmmup != ksfmmup); 4832 ASSERT(hashno > 1); 4833 addr = (caddr_t)P2END((uintptr_t)addr, 4834 TTEBYTES(hashno - 1)); 4835 } else { 4836 addr = sfmmu_hblk_chgattr(sfmmup, 4837 hmeblkp, addr, endaddr, &dmr, attr, mode); 4838 } 4839 SFMMU_HASH_UNLOCK(hmebp); 4840 hashno = 1; 4841 continue; 4842 } 4843 SFMMU_HASH_UNLOCK(hmebp); 4844 4845 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 4846 /* 4847 * We have traversed the whole list and rehashed 4848 * if necessary without finding the address to chgattr. 4849 * This is ok, so we increment the address by the 4850 * smallest hmeblk range for kernel mappings or for 4851 * user mappings with no large pages, and the largest 4852 * hmeblk range, to account for shadow hmeblks, for 4853 * user mappings with large pages and continue. 4854 */ 4855 if (sfmmup == ksfmmup) 4856 addr = (caddr_t)P2END((uintptr_t)addr, 4857 TTEBYTES(1)); 4858 else 4859 addr = (caddr_t)P2END((uintptr_t)addr, 4860 TTEBYTES(hashno)); 4861 hashno = 1; 4862 } else { 4863 hashno++; 4864 } 4865 } 4866 4867 sfmmu_hblks_list_purge(&list); 4868 DEMAP_RANGE_FLUSH(&dmr); 4869 cpuset = sfmmup->sfmmu_cpusran; 4870 xt_sync(cpuset); 4871 } 4872 4873 /* 4874 * This function chgattr on a range of addresses in an hmeblk. It returns the 4875 * next addres that needs to be chgattr. 4876 * It should be called with the hash lock held. 4877 * XXX It should be possible to optimize chgattr by not flushing every time but 4878 * on the other hand: 4879 * 1. do one flush crosscall. 4880 * 2. only flush if we are increasing permissions (make sure this will work) 4881 */ 4882 static caddr_t 4883 sfmmu_hblk_chgattr(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 4884 caddr_t endaddr, demap_range_t *dmrp, uint_t attr, int mode) 4885 { 4886 tte_t tte, tteattr, tteflags, ttemod; 4887 struct sf_hment *sfhmep; 4888 int ttesz; 4889 struct page *pp = NULL; 4890 kmutex_t *pml, *pmtx; 4891 int ret; 4892 int use_demap_range; 4893 #if defined(SF_ERRATA_57) 4894 int check_exec; 4895 #endif 4896 4897 ASSERT(in_hblk_range(hmeblkp, addr)); 4898 ASSERT(hmeblkp->hblk_shw_bit == 0); 4899 ASSERT(!hmeblkp->hblk_shared); 4900 4901 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 4902 ttesz = get_hblk_ttesz(hmeblkp); 4903 4904 /* 4905 * Flush the current demap region if addresses have been 4906 * skipped or the page size doesn't match. 4907 */ 4908 use_demap_range = (TTEBYTES(ttesz) == DEMAP_RANGE_PGSZ(dmrp)); 4909 if (use_demap_range) { 4910 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr); 4911 } else { 4912 DEMAP_RANGE_FLUSH(dmrp); 4913 } 4914 4915 tteattr.ll = sfmmu_vtop_attr(attr, mode, &tteflags); 4916 #if defined(SF_ERRATA_57) 4917 check_exec = (sfmmup != ksfmmup) && 4918 AS_TYPE_64BIT(sfmmup->sfmmu_as) && 4919 TTE_IS_EXECUTABLE(&tteattr); 4920 #endif 4921 HBLKTOHME(sfhmep, hmeblkp, addr); 4922 while (addr < endaddr) { 4923 sfmmu_copytte(&sfhmep->hme_tte, &tte); 4924 if (TTE_IS_VALID(&tte)) { 4925 if ((tte.ll & tteflags.ll) == tteattr.ll) { 4926 /* 4927 * if the new attr is the same as old 4928 * continue 4929 */ 4930 goto next_addr; 4931 } 4932 if (!TTE_IS_WRITABLE(&tteattr)) { 4933 /* 4934 * make sure we clear hw modify bit if we 4935 * removing write protections 4936 */ 4937 tteflags.tte_intlo |= TTE_HWWR_INT; 4938 } 4939 4940 pml = NULL; 4941 pp = sfhmep->hme_page; 4942 if (pp) { 4943 pml = sfmmu_mlist_enter(pp); 4944 } 4945 4946 if (pp != sfhmep->hme_page) { 4947 /* 4948 * tte must have been unloaded. 4949 */ 4950 ASSERT(pml); 4951 sfmmu_mlist_exit(pml); 4952 continue; 4953 } 4954 4955 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 4956 4957 ttemod = tte; 4958 ttemod.ll = (ttemod.ll & ~tteflags.ll) | tteattr.ll; 4959 ASSERT(TTE_TO_TTEPFN(&ttemod) == TTE_TO_TTEPFN(&tte)); 4960 4961 #if defined(SF_ERRATA_57) 4962 if (check_exec && addr < errata57_limit) 4963 ttemod.tte_exec_perm = 0; 4964 #endif 4965 ret = sfmmu_modifytte_try(&tte, &ttemod, 4966 &sfhmep->hme_tte); 4967 4968 if (ret < 0) { 4969 /* tte changed underneath us */ 4970 if (pml) { 4971 sfmmu_mlist_exit(pml); 4972 } 4973 continue; 4974 } 4975 4976 if (tteflags.tte_intlo & TTE_HWWR_INT) { 4977 /* 4978 * need to sync if we are clearing modify bit. 4979 */ 4980 sfmmu_ttesync(sfmmup, addr, &tte, pp); 4981 } 4982 4983 if (pp && PP_ISRO(pp)) { 4984 if (tteattr.tte_intlo & TTE_WRPRM_INT) { 4985 pmtx = sfmmu_page_enter(pp); 4986 PP_CLRRO(pp); 4987 sfmmu_page_exit(pmtx); 4988 } 4989 } 4990 4991 if (ret > 0 && use_demap_range) { 4992 DEMAP_RANGE_MARKPG(dmrp, addr); 4993 } else if (ret > 0) { 4994 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 4995 } 4996 4997 if (pml) { 4998 sfmmu_mlist_exit(pml); 4999 } 5000 } 5001 next_addr: 5002 addr += TTEBYTES(ttesz); 5003 sfhmep++; 5004 DEMAP_RANGE_NEXTPG(dmrp); 5005 } 5006 return (addr); 5007 } 5008 5009 /* 5010 * This routine converts virtual attributes to physical ones. It will 5011 * update the tteflags field with the tte mask corresponding to the attributes 5012 * affected and it returns the new attributes. It will also clear the modify 5013 * bit if we are taking away write permission. This is necessary since the 5014 * modify bit is the hardware permission bit and we need to clear it in order 5015 * to detect write faults. 5016 */ 5017 static uint64_t 5018 sfmmu_vtop_attr(uint_t attr, int mode, tte_t *ttemaskp) 5019 { 5020 tte_t ttevalue; 5021 5022 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR)); 5023 5024 switch (mode) { 5025 case SFMMU_CHGATTR: 5026 /* all attributes specified */ 5027 ttevalue.tte_inthi = MAKE_TTEATTR_INTHI(attr); 5028 ttevalue.tte_intlo = MAKE_TTEATTR_INTLO(attr); 5029 ttemaskp->tte_inthi = TTEINTHI_ATTR; 5030 ttemaskp->tte_intlo = TTEINTLO_ATTR; 5031 break; 5032 case SFMMU_SETATTR: 5033 ASSERT(!(attr & ~HAT_PROT_MASK)); 5034 ttemaskp->ll = 0; 5035 ttevalue.ll = 0; 5036 /* 5037 * a valid tte implies exec and read for sfmmu 5038 * so no need to do anything about them. 5039 * since priviledged access implies user access 5040 * PROT_USER doesn't make sense either. 5041 */ 5042 if (attr & PROT_WRITE) { 5043 ttemaskp->tte_intlo |= TTE_WRPRM_INT; 5044 ttevalue.tte_intlo |= TTE_WRPRM_INT; 5045 } 5046 break; 5047 case SFMMU_CLRATTR: 5048 /* attributes will be nand with current ones */ 5049 if (attr & ~(PROT_WRITE | PROT_USER)) { 5050 panic("sfmmu: attr %x not supported", attr); 5051 } 5052 ttemaskp->ll = 0; 5053 ttevalue.ll = 0; 5054 if (attr & PROT_WRITE) { 5055 /* clear both writable and modify bit */ 5056 ttemaskp->tte_intlo |= TTE_WRPRM_INT | TTE_HWWR_INT; 5057 } 5058 if (attr & PROT_USER) { 5059 ttemaskp->tte_intlo |= TTE_PRIV_INT; 5060 ttevalue.tte_intlo |= TTE_PRIV_INT; 5061 } 5062 break; 5063 default: 5064 panic("sfmmu_vtop_attr: bad mode %x", mode); 5065 } 5066 ASSERT(TTE_TO_TTEPFN(&ttevalue) == 0); 5067 return (ttevalue.ll); 5068 } 5069 5070 static uint_t 5071 sfmmu_ptov_attr(tte_t *ttep) 5072 { 5073 uint_t attr; 5074 5075 ASSERT(TTE_IS_VALID(ttep)); 5076 5077 attr = PROT_READ; 5078 5079 if (TTE_IS_WRITABLE(ttep)) { 5080 attr |= PROT_WRITE; 5081 } 5082 if (TTE_IS_EXECUTABLE(ttep)) { 5083 attr |= PROT_EXEC; 5084 } 5085 if (!TTE_IS_PRIVILEGED(ttep)) { 5086 attr |= PROT_USER; 5087 } 5088 if (TTE_IS_NFO(ttep)) { 5089 attr |= HAT_NOFAULT; 5090 } 5091 if (TTE_IS_NOSYNC(ttep)) { 5092 attr |= HAT_NOSYNC; 5093 } 5094 if (TTE_IS_SIDEFFECT(ttep)) { 5095 attr |= SFMMU_SIDEFFECT; 5096 } 5097 if (!TTE_IS_VCACHEABLE(ttep)) { 5098 attr |= SFMMU_UNCACHEVTTE; 5099 } 5100 if (!TTE_IS_PCACHEABLE(ttep)) { 5101 attr |= SFMMU_UNCACHEPTTE; 5102 } 5103 return (attr); 5104 } 5105 5106 /* 5107 * hat_chgprot is a deprecated hat call. New segment drivers 5108 * should store all attributes and use hat_*attr calls. 5109 * 5110 * Change the protections in the virtual address range 5111 * given to the specified virtual protection. If vprot is ~PROT_WRITE, 5112 * then remove write permission, leaving the other 5113 * permissions unchanged. If vprot is ~PROT_USER, remove user permissions. 5114 * 5115 */ 5116 void 5117 hat_chgprot(struct hat *sfmmup, caddr_t addr, size_t len, uint_t vprot) 5118 { 5119 struct hmehash_bucket *hmebp; 5120 hmeblk_tag hblktag; 5121 int hmeshift, hashno = 1; 5122 struct hme_blk *hmeblkp, *list = NULL; 5123 caddr_t endaddr; 5124 cpuset_t cpuset; 5125 demap_range_t dmr; 5126 5127 ASSERT((len & MMU_PAGEOFFSET) == 0); 5128 ASSERT(((uintptr_t)addr & MMU_PAGEOFFSET) == 0); 5129 5130 if (sfmmup->sfmmu_xhat_provider) { 5131 XHAT_CHGPROT(sfmmup, addr, len, vprot); 5132 return; 5133 } else { 5134 /* 5135 * This must be a CPU HAT. If the address space has 5136 * XHATs attached, change attributes for all of them, 5137 * just in case 5138 */ 5139 ASSERT(sfmmup->sfmmu_as != NULL); 5140 if (sfmmup->sfmmu_as->a_xhat != NULL) 5141 xhat_chgprot_all(sfmmup->sfmmu_as, addr, len, vprot); 5142 } 5143 5144 CPUSET_ZERO(cpuset); 5145 5146 if ((vprot != (uint_t)~PROT_WRITE) && (vprot & PROT_USER) && 5147 ((addr + len) > (caddr_t)USERLIMIT)) { 5148 panic("user addr %p vprot %x in kernel space", 5149 (void *)addr, vprot); 5150 } 5151 endaddr = addr + len; 5152 hblktag.htag_id = sfmmup; 5153 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 5154 DEMAP_RANGE_INIT(sfmmup, &dmr); 5155 5156 while (addr < endaddr) { 5157 hmeshift = HME_HASH_SHIFT(hashno); 5158 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 5159 hblktag.htag_rehash = hashno; 5160 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 5161 5162 SFMMU_HASH_LOCK(hmebp); 5163 5164 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 5165 if (hmeblkp != NULL) { 5166 ASSERT(!hmeblkp->hblk_shared); 5167 /* 5168 * We've encountered a shadow hmeblk so skip the range 5169 * of the next smaller mapping size. 5170 */ 5171 if (hmeblkp->hblk_shw_bit) { 5172 ASSERT(sfmmup != ksfmmup); 5173 ASSERT(hashno > 1); 5174 addr = (caddr_t)P2END((uintptr_t)addr, 5175 TTEBYTES(hashno - 1)); 5176 } else { 5177 addr = sfmmu_hblk_chgprot(sfmmup, hmeblkp, 5178 addr, endaddr, &dmr, vprot); 5179 } 5180 SFMMU_HASH_UNLOCK(hmebp); 5181 hashno = 1; 5182 continue; 5183 } 5184 SFMMU_HASH_UNLOCK(hmebp); 5185 5186 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 5187 /* 5188 * We have traversed the whole list and rehashed 5189 * if necessary without finding the address to chgprot. 5190 * This is ok so we increment the address by the 5191 * smallest hmeblk range for kernel mappings and the 5192 * largest hmeblk range, to account for shadow hmeblks, 5193 * for user mappings and continue. 5194 */ 5195 if (sfmmup == ksfmmup) 5196 addr = (caddr_t)P2END((uintptr_t)addr, 5197 TTEBYTES(1)); 5198 else 5199 addr = (caddr_t)P2END((uintptr_t)addr, 5200 TTEBYTES(hashno)); 5201 hashno = 1; 5202 } else { 5203 hashno++; 5204 } 5205 } 5206 5207 sfmmu_hblks_list_purge(&list); 5208 DEMAP_RANGE_FLUSH(&dmr); 5209 cpuset = sfmmup->sfmmu_cpusran; 5210 xt_sync(cpuset); 5211 } 5212 5213 /* 5214 * This function chgprots a range of addresses in an hmeblk. It returns the 5215 * next addres that needs to be chgprot. 5216 * It should be called with the hash lock held. 5217 * XXX It shold be possible to optimize chgprot by not flushing every time but 5218 * on the other hand: 5219 * 1. do one flush crosscall. 5220 * 2. only flush if we are increasing permissions (make sure this will work) 5221 */ 5222 static caddr_t 5223 sfmmu_hblk_chgprot(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 5224 caddr_t endaddr, demap_range_t *dmrp, uint_t vprot) 5225 { 5226 uint_t pprot; 5227 tte_t tte, ttemod; 5228 struct sf_hment *sfhmep; 5229 uint_t tteflags; 5230 int ttesz; 5231 struct page *pp = NULL; 5232 kmutex_t *pml, *pmtx; 5233 int ret; 5234 int use_demap_range; 5235 #if defined(SF_ERRATA_57) 5236 int check_exec; 5237 #endif 5238 5239 ASSERT(in_hblk_range(hmeblkp, addr)); 5240 ASSERT(hmeblkp->hblk_shw_bit == 0); 5241 ASSERT(!hmeblkp->hblk_shared); 5242 5243 #ifdef DEBUG 5244 if (get_hblk_ttesz(hmeblkp) != TTE8K && 5245 (endaddr < get_hblk_endaddr(hmeblkp))) { 5246 panic("sfmmu_hblk_chgprot: partial chgprot of large page"); 5247 } 5248 #endif /* DEBUG */ 5249 5250 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 5251 ttesz = get_hblk_ttesz(hmeblkp); 5252 5253 pprot = sfmmu_vtop_prot(vprot, &tteflags); 5254 #if defined(SF_ERRATA_57) 5255 check_exec = (sfmmup != ksfmmup) && 5256 AS_TYPE_64BIT(sfmmup->sfmmu_as) && 5257 ((vprot & PROT_EXEC) == PROT_EXEC); 5258 #endif 5259 HBLKTOHME(sfhmep, hmeblkp, addr); 5260 5261 /* 5262 * Flush the current demap region if addresses have been 5263 * skipped or the page size doesn't match. 5264 */ 5265 use_demap_range = (TTEBYTES(ttesz) == MMU_PAGESIZE); 5266 if (use_demap_range) { 5267 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr); 5268 } else { 5269 DEMAP_RANGE_FLUSH(dmrp); 5270 } 5271 5272 while (addr < endaddr) { 5273 sfmmu_copytte(&sfhmep->hme_tte, &tte); 5274 if (TTE_IS_VALID(&tte)) { 5275 if (TTE_GET_LOFLAGS(&tte, tteflags) == pprot) { 5276 /* 5277 * if the new protection is the same as old 5278 * continue 5279 */ 5280 goto next_addr; 5281 } 5282 pml = NULL; 5283 pp = sfhmep->hme_page; 5284 if (pp) { 5285 pml = sfmmu_mlist_enter(pp); 5286 } 5287 if (pp != sfhmep->hme_page) { 5288 /* 5289 * tte most have been unloaded 5290 * underneath us. Recheck 5291 */ 5292 ASSERT(pml); 5293 sfmmu_mlist_exit(pml); 5294 continue; 5295 } 5296 5297 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 5298 5299 ttemod = tte; 5300 TTE_SET_LOFLAGS(&ttemod, tteflags, pprot); 5301 #if defined(SF_ERRATA_57) 5302 if (check_exec && addr < errata57_limit) 5303 ttemod.tte_exec_perm = 0; 5304 #endif 5305 ret = sfmmu_modifytte_try(&tte, &ttemod, 5306 &sfhmep->hme_tte); 5307 5308 if (ret < 0) { 5309 /* tte changed underneath us */ 5310 if (pml) { 5311 sfmmu_mlist_exit(pml); 5312 } 5313 continue; 5314 } 5315 5316 if (tteflags & TTE_HWWR_INT) { 5317 /* 5318 * need to sync if we are clearing modify bit. 5319 */ 5320 sfmmu_ttesync(sfmmup, addr, &tte, pp); 5321 } 5322 5323 if (pp && PP_ISRO(pp)) { 5324 if (pprot & TTE_WRPRM_INT) { 5325 pmtx = sfmmu_page_enter(pp); 5326 PP_CLRRO(pp); 5327 sfmmu_page_exit(pmtx); 5328 } 5329 } 5330 5331 if (ret > 0 && use_demap_range) { 5332 DEMAP_RANGE_MARKPG(dmrp, addr); 5333 } else if (ret > 0) { 5334 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 5335 } 5336 5337 if (pml) { 5338 sfmmu_mlist_exit(pml); 5339 } 5340 } 5341 next_addr: 5342 addr += TTEBYTES(ttesz); 5343 sfhmep++; 5344 DEMAP_RANGE_NEXTPG(dmrp); 5345 } 5346 return (addr); 5347 } 5348 5349 /* 5350 * This routine is deprecated and should only be used by hat_chgprot. 5351 * The correct routine is sfmmu_vtop_attr. 5352 * This routine converts virtual page protections to physical ones. It will 5353 * update the tteflags field with the tte mask corresponding to the protections 5354 * affected and it returns the new protections. It will also clear the modify 5355 * bit if we are taking away write permission. This is necessary since the 5356 * modify bit is the hardware permission bit and we need to clear it in order 5357 * to detect write faults. 5358 * It accepts the following special protections: 5359 * ~PROT_WRITE = remove write permissions. 5360 * ~PROT_USER = remove user permissions. 5361 */ 5362 static uint_t 5363 sfmmu_vtop_prot(uint_t vprot, uint_t *tteflagsp) 5364 { 5365 if (vprot == (uint_t)~PROT_WRITE) { 5366 *tteflagsp = TTE_WRPRM_INT | TTE_HWWR_INT; 5367 return (0); /* will cause wrprm to be cleared */ 5368 } 5369 if (vprot == (uint_t)~PROT_USER) { 5370 *tteflagsp = TTE_PRIV_INT; 5371 return (0); /* will cause privprm to be cleared */ 5372 } 5373 if ((vprot == 0) || (vprot == PROT_USER) || 5374 ((vprot & PROT_ALL) != vprot)) { 5375 panic("sfmmu_vtop_prot -- bad prot %x", vprot); 5376 } 5377 5378 switch (vprot) { 5379 case (PROT_READ): 5380 case (PROT_EXEC): 5381 case (PROT_EXEC | PROT_READ): 5382 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT | TTE_HWWR_INT; 5383 return (TTE_PRIV_INT); /* set prv and clr wrt */ 5384 case (PROT_WRITE): 5385 case (PROT_WRITE | PROT_READ): 5386 case (PROT_EXEC | PROT_WRITE): 5387 case (PROT_EXEC | PROT_WRITE | PROT_READ): 5388 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT; 5389 return (TTE_PRIV_INT | TTE_WRPRM_INT); /* set prv and wrt */ 5390 case (PROT_USER | PROT_READ): 5391 case (PROT_USER | PROT_EXEC): 5392 case (PROT_USER | PROT_EXEC | PROT_READ): 5393 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT | TTE_HWWR_INT; 5394 return (0); /* clr prv and wrt */ 5395 case (PROT_USER | PROT_WRITE): 5396 case (PROT_USER | PROT_WRITE | PROT_READ): 5397 case (PROT_USER | PROT_EXEC | PROT_WRITE): 5398 case (PROT_USER | PROT_EXEC | PROT_WRITE | PROT_READ): 5399 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT; 5400 return (TTE_WRPRM_INT); /* clr prv and set wrt */ 5401 default: 5402 panic("sfmmu_vtop_prot -- bad prot %x", vprot); 5403 } 5404 return (0); 5405 } 5406 5407 /* 5408 * Alternate unload for very large virtual ranges. With a true 64 bit VA, 5409 * the normal algorithm would take too long for a very large VA range with 5410 * few real mappings. This routine just walks thru all HMEs in the global 5411 * hash table to find and remove mappings. 5412 */ 5413 static void 5414 hat_unload_large_virtual( 5415 struct hat *sfmmup, 5416 caddr_t startaddr, 5417 size_t len, 5418 uint_t flags, 5419 hat_callback_t *callback) 5420 { 5421 struct hmehash_bucket *hmebp; 5422 struct hme_blk *hmeblkp; 5423 struct hme_blk *pr_hblk = NULL; 5424 struct hme_blk *nx_hblk; 5425 struct hme_blk *list = NULL; 5426 int i; 5427 uint64_t hblkpa, prevpa, nx_pa; 5428 demap_range_t dmr, *dmrp; 5429 cpuset_t cpuset; 5430 caddr_t endaddr = startaddr + len; 5431 caddr_t sa; 5432 caddr_t ea; 5433 caddr_t cb_sa[MAX_CB_ADDR]; 5434 caddr_t cb_ea[MAX_CB_ADDR]; 5435 int addr_cnt = 0; 5436 int a = 0; 5437 5438 if (sfmmup->sfmmu_free) { 5439 dmrp = NULL; 5440 } else { 5441 dmrp = &dmr; 5442 DEMAP_RANGE_INIT(sfmmup, dmrp); 5443 } 5444 5445 /* 5446 * Loop through all the hash buckets of HME blocks looking for matches. 5447 */ 5448 for (i = 0; i <= UHMEHASH_SZ; i++) { 5449 hmebp = &uhme_hash[i]; 5450 SFMMU_HASH_LOCK(hmebp); 5451 hmeblkp = hmebp->hmeblkp; 5452 hblkpa = hmebp->hmeh_nextpa; 5453 prevpa = 0; 5454 pr_hblk = NULL; 5455 while (hmeblkp) { 5456 nx_hblk = hmeblkp->hblk_next; 5457 nx_pa = hmeblkp->hblk_nextpa; 5458 5459 /* 5460 * skip if not this context, if a shadow block or 5461 * if the mapping is not in the requested range 5462 */ 5463 if (hmeblkp->hblk_tag.htag_id != sfmmup || 5464 hmeblkp->hblk_shw_bit || 5465 (sa = (caddr_t)get_hblk_base(hmeblkp)) >= endaddr || 5466 (ea = get_hblk_endaddr(hmeblkp)) <= startaddr) { 5467 pr_hblk = hmeblkp; 5468 prevpa = hblkpa; 5469 goto next_block; 5470 } 5471 5472 ASSERT(!hmeblkp->hblk_shared); 5473 /* 5474 * unload if there are any current valid mappings 5475 */ 5476 if (hmeblkp->hblk_vcnt != 0 || 5477 hmeblkp->hblk_hmecnt != 0) 5478 (void) sfmmu_hblk_unload(sfmmup, hmeblkp, 5479 sa, ea, dmrp, flags); 5480 5481 /* 5482 * on unmap we also release the HME block itself, once 5483 * all mappings are gone. 5484 */ 5485 if ((flags & HAT_UNLOAD_UNMAP) != 0 && 5486 !hmeblkp->hblk_vcnt && 5487 !hmeblkp->hblk_hmecnt) { 5488 ASSERT(!hmeblkp->hblk_lckcnt); 5489 sfmmu_hblk_hash_rm(hmebp, hmeblkp, 5490 prevpa, pr_hblk); 5491 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 5492 } else { 5493 pr_hblk = hmeblkp; 5494 prevpa = hblkpa; 5495 } 5496 5497 if (callback == NULL) 5498 goto next_block; 5499 5500 /* 5501 * HME blocks may span more than one page, but we may be 5502 * unmapping only one page, so check for a smaller range 5503 * for the callback 5504 */ 5505 if (sa < startaddr) 5506 sa = startaddr; 5507 if (--ea > endaddr) 5508 ea = endaddr - 1; 5509 5510 cb_sa[addr_cnt] = sa; 5511 cb_ea[addr_cnt] = ea; 5512 if (++addr_cnt == MAX_CB_ADDR) { 5513 if (dmrp != NULL) { 5514 DEMAP_RANGE_FLUSH(dmrp); 5515 cpuset = sfmmup->sfmmu_cpusran; 5516 xt_sync(cpuset); 5517 } 5518 5519 for (a = 0; a < MAX_CB_ADDR; ++a) { 5520 callback->hcb_start_addr = cb_sa[a]; 5521 callback->hcb_end_addr = cb_ea[a]; 5522 callback->hcb_function(callback); 5523 } 5524 addr_cnt = 0; 5525 } 5526 5527 next_block: 5528 hmeblkp = nx_hblk; 5529 hblkpa = nx_pa; 5530 } 5531 SFMMU_HASH_UNLOCK(hmebp); 5532 } 5533 5534 sfmmu_hblks_list_purge(&list); 5535 if (dmrp != NULL) { 5536 DEMAP_RANGE_FLUSH(dmrp); 5537 cpuset = sfmmup->sfmmu_cpusran; 5538 xt_sync(cpuset); 5539 } 5540 5541 for (a = 0; a < addr_cnt; ++a) { 5542 callback->hcb_start_addr = cb_sa[a]; 5543 callback->hcb_end_addr = cb_ea[a]; 5544 callback->hcb_function(callback); 5545 } 5546 5547 /* 5548 * Check TSB and TLB page sizes if the process isn't exiting. 5549 */ 5550 if (!sfmmup->sfmmu_free) 5551 sfmmu_check_page_sizes(sfmmup, 0); 5552 } 5553 5554 /* 5555 * Unload all the mappings in the range [addr..addr+len). addr and len must 5556 * be MMU_PAGESIZE aligned. 5557 */ 5558 5559 extern struct seg *segkmap; 5560 #define ISSEGKMAP(sfmmup, addr) (sfmmup == ksfmmup && \ 5561 segkmap->s_base <= (addr) && (addr) < (segkmap->s_base + segkmap->s_size)) 5562 5563 5564 void 5565 hat_unload_callback( 5566 struct hat *sfmmup, 5567 caddr_t addr, 5568 size_t len, 5569 uint_t flags, 5570 hat_callback_t *callback) 5571 { 5572 struct hmehash_bucket *hmebp; 5573 hmeblk_tag hblktag; 5574 int hmeshift, hashno, iskernel; 5575 struct hme_blk *hmeblkp, *pr_hblk, *list = NULL; 5576 caddr_t endaddr; 5577 cpuset_t cpuset; 5578 uint64_t hblkpa, prevpa; 5579 int addr_count = 0; 5580 int a; 5581 caddr_t cb_start_addr[MAX_CB_ADDR]; 5582 caddr_t cb_end_addr[MAX_CB_ADDR]; 5583 int issegkmap = ISSEGKMAP(sfmmup, addr); 5584 demap_range_t dmr, *dmrp; 5585 5586 if (sfmmup->sfmmu_xhat_provider) { 5587 XHAT_UNLOAD_CALLBACK(sfmmup, addr, len, flags, callback); 5588 return; 5589 } else { 5590 /* 5591 * This must be a CPU HAT. If the address space has 5592 * XHATs attached, unload the mappings for all of them, 5593 * just in case 5594 */ 5595 ASSERT(sfmmup->sfmmu_as != NULL); 5596 if (sfmmup->sfmmu_as->a_xhat != NULL) 5597 xhat_unload_callback_all(sfmmup->sfmmu_as, addr, 5598 len, flags, callback); 5599 } 5600 5601 ASSERT((sfmmup == ksfmmup) || (flags & HAT_UNLOAD_OTHER) || \ 5602 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 5603 5604 ASSERT(sfmmup != NULL); 5605 ASSERT((len & MMU_PAGEOFFSET) == 0); 5606 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET)); 5607 5608 /* 5609 * Probing through a large VA range (say 63 bits) will be slow, even 5610 * at 4 Meg steps between the probes. So, when the virtual address range 5611 * is very large, search the HME entries for what to unload. 5612 * 5613 * len >> TTE_PAGE_SHIFT(TTE4M) is the # of 4Meg probes we'd need 5614 * 5615 * UHMEHASH_SZ is number of hash buckets to examine 5616 * 5617 */ 5618 if (sfmmup != KHATID && (len >> TTE_PAGE_SHIFT(TTE4M)) > UHMEHASH_SZ) { 5619 hat_unload_large_virtual(sfmmup, addr, len, flags, callback); 5620 return; 5621 } 5622 5623 CPUSET_ZERO(cpuset); 5624 5625 /* 5626 * If the process is exiting, we can save a lot of fuss since 5627 * we'll flush the TLB when we free the ctx anyway. 5628 */ 5629 if (sfmmup->sfmmu_free) 5630 dmrp = NULL; 5631 else 5632 dmrp = &dmr; 5633 5634 DEMAP_RANGE_INIT(sfmmup, dmrp); 5635 endaddr = addr + len; 5636 hblktag.htag_id = sfmmup; 5637 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 5638 5639 /* 5640 * It is likely for the vm to call unload over a wide range of 5641 * addresses that are actually very sparsely populated by 5642 * translations. In order to speed this up the sfmmu hat supports 5643 * the concept of shadow hmeblks. Dummy large page hmeblks that 5644 * correspond to actual small translations are allocated at tteload 5645 * time and are referred to as shadow hmeblks. Now, during unload 5646 * time, we first check if we have a shadow hmeblk for that 5647 * translation. The absence of one means the corresponding address 5648 * range is empty and can be skipped. 5649 * 5650 * The kernel is an exception to above statement and that is why 5651 * we don't use shadow hmeblks and hash starting from the smallest 5652 * page size. 5653 */ 5654 if (sfmmup == KHATID) { 5655 iskernel = 1; 5656 hashno = TTE64K; 5657 } else { 5658 iskernel = 0; 5659 if (mmu_page_sizes == max_mmu_page_sizes) { 5660 hashno = TTE256M; 5661 } else { 5662 hashno = TTE4M; 5663 } 5664 } 5665 while (addr < endaddr) { 5666 hmeshift = HME_HASH_SHIFT(hashno); 5667 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 5668 hblktag.htag_rehash = hashno; 5669 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 5670 5671 SFMMU_HASH_LOCK(hmebp); 5672 5673 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, hblkpa, pr_hblk, 5674 prevpa, &list); 5675 if (hmeblkp == NULL) { 5676 /* 5677 * didn't find an hmeblk. skip the appropiate 5678 * address range. 5679 */ 5680 SFMMU_HASH_UNLOCK(hmebp); 5681 if (iskernel) { 5682 if (hashno < mmu_hashcnt) { 5683 hashno++; 5684 continue; 5685 } else { 5686 hashno = TTE64K; 5687 addr = (caddr_t)roundup((uintptr_t)addr 5688 + 1, MMU_PAGESIZE64K); 5689 continue; 5690 } 5691 } 5692 addr = (caddr_t)roundup((uintptr_t)addr + 1, 5693 (1 << hmeshift)); 5694 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) { 5695 ASSERT(hashno == TTE64K); 5696 continue; 5697 } 5698 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) { 5699 hashno = TTE512K; 5700 continue; 5701 } 5702 if (mmu_page_sizes == max_mmu_page_sizes) { 5703 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) { 5704 hashno = TTE4M; 5705 continue; 5706 } 5707 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) { 5708 hashno = TTE32M; 5709 continue; 5710 } 5711 hashno = TTE256M; 5712 continue; 5713 } else { 5714 hashno = TTE4M; 5715 continue; 5716 } 5717 } 5718 ASSERT(hmeblkp); 5719 ASSERT(!hmeblkp->hblk_shared); 5720 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) { 5721 /* 5722 * If the valid count is zero we can skip the range 5723 * mapped by this hmeblk. 5724 * We free hblks in the case of HAT_UNMAP. HAT_UNMAP 5725 * is used by segment drivers as a hint 5726 * that the mapping resource won't be used any longer. 5727 * The best example of this is during exit(). 5728 */ 5729 addr = (caddr_t)roundup((uintptr_t)addr + 1, 5730 get_hblk_span(hmeblkp)); 5731 if ((flags & HAT_UNLOAD_UNMAP) || 5732 (iskernel && !issegkmap)) { 5733 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, 5734 pr_hblk); 5735 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 5736 } 5737 SFMMU_HASH_UNLOCK(hmebp); 5738 5739 if (iskernel) { 5740 hashno = TTE64K; 5741 continue; 5742 } 5743 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) { 5744 ASSERT(hashno == TTE64K); 5745 continue; 5746 } 5747 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) { 5748 hashno = TTE512K; 5749 continue; 5750 } 5751 if (mmu_page_sizes == max_mmu_page_sizes) { 5752 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) { 5753 hashno = TTE4M; 5754 continue; 5755 } 5756 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) { 5757 hashno = TTE32M; 5758 continue; 5759 } 5760 hashno = TTE256M; 5761 continue; 5762 } else { 5763 hashno = TTE4M; 5764 continue; 5765 } 5766 } 5767 if (hmeblkp->hblk_shw_bit) { 5768 /* 5769 * If we encounter a shadow hmeblk we know there is 5770 * smaller sized hmeblks mapping the same address space. 5771 * Decrement the hash size and rehash. 5772 */ 5773 ASSERT(sfmmup != KHATID); 5774 hashno--; 5775 SFMMU_HASH_UNLOCK(hmebp); 5776 continue; 5777 } 5778 5779 /* 5780 * track callback address ranges. 5781 * only start a new range when it's not contiguous 5782 */ 5783 if (callback != NULL) { 5784 if (addr_count > 0 && 5785 addr == cb_end_addr[addr_count - 1]) 5786 --addr_count; 5787 else 5788 cb_start_addr[addr_count] = addr; 5789 } 5790 5791 addr = sfmmu_hblk_unload(sfmmup, hmeblkp, addr, endaddr, 5792 dmrp, flags); 5793 5794 if (callback != NULL) 5795 cb_end_addr[addr_count++] = addr; 5796 5797 if (((flags & HAT_UNLOAD_UNMAP) || (iskernel && !issegkmap)) && 5798 !hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) { 5799 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, 5800 pr_hblk); 5801 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 5802 } 5803 SFMMU_HASH_UNLOCK(hmebp); 5804 5805 /* 5806 * Notify our caller as to exactly which pages 5807 * have been unloaded. We do these in clumps, 5808 * to minimize the number of xt_sync()s that need to occur. 5809 */ 5810 if (callback != NULL && addr_count == MAX_CB_ADDR) { 5811 DEMAP_RANGE_FLUSH(dmrp); 5812 if (dmrp != NULL) { 5813 cpuset = sfmmup->sfmmu_cpusran; 5814 xt_sync(cpuset); 5815 } 5816 5817 for (a = 0; a < MAX_CB_ADDR; ++a) { 5818 callback->hcb_start_addr = cb_start_addr[a]; 5819 callback->hcb_end_addr = cb_end_addr[a]; 5820 callback->hcb_function(callback); 5821 } 5822 addr_count = 0; 5823 } 5824 if (iskernel) { 5825 hashno = TTE64K; 5826 continue; 5827 } 5828 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) { 5829 ASSERT(hashno == TTE64K); 5830 continue; 5831 } 5832 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) { 5833 hashno = TTE512K; 5834 continue; 5835 } 5836 if (mmu_page_sizes == max_mmu_page_sizes) { 5837 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) { 5838 hashno = TTE4M; 5839 continue; 5840 } 5841 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) { 5842 hashno = TTE32M; 5843 continue; 5844 } 5845 hashno = TTE256M; 5846 } else { 5847 hashno = TTE4M; 5848 } 5849 } 5850 5851 sfmmu_hblks_list_purge(&list); 5852 DEMAP_RANGE_FLUSH(dmrp); 5853 if (dmrp != NULL) { 5854 cpuset = sfmmup->sfmmu_cpusran; 5855 xt_sync(cpuset); 5856 } 5857 if (callback && addr_count != 0) { 5858 for (a = 0; a < addr_count; ++a) { 5859 callback->hcb_start_addr = cb_start_addr[a]; 5860 callback->hcb_end_addr = cb_end_addr[a]; 5861 callback->hcb_function(callback); 5862 } 5863 } 5864 5865 /* 5866 * Check TSB and TLB page sizes if the process isn't exiting. 5867 */ 5868 if (!sfmmup->sfmmu_free) 5869 sfmmu_check_page_sizes(sfmmup, 0); 5870 } 5871 5872 /* 5873 * Unload all the mappings in the range [addr..addr+len). addr and len must 5874 * be MMU_PAGESIZE aligned. 5875 */ 5876 void 5877 hat_unload(struct hat *sfmmup, caddr_t addr, size_t len, uint_t flags) 5878 { 5879 if (sfmmup->sfmmu_xhat_provider) { 5880 XHAT_UNLOAD(sfmmup, addr, len, flags); 5881 return; 5882 } 5883 hat_unload_callback(sfmmup, addr, len, flags, NULL); 5884 } 5885 5886 5887 /* 5888 * Find the largest mapping size for this page. 5889 */ 5890 int 5891 fnd_mapping_sz(page_t *pp) 5892 { 5893 int sz; 5894 int p_index; 5895 5896 p_index = PP_MAPINDEX(pp); 5897 5898 sz = 0; 5899 p_index >>= 1; /* don't care about 8K bit */ 5900 for (; p_index; p_index >>= 1) { 5901 sz++; 5902 } 5903 5904 return (sz); 5905 } 5906 5907 /* 5908 * This function unloads a range of addresses for an hmeblk. 5909 * It returns the next address to be unloaded. 5910 * It should be called with the hash lock held. 5911 */ 5912 static caddr_t 5913 sfmmu_hblk_unload(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 5914 caddr_t endaddr, demap_range_t *dmrp, uint_t flags) 5915 { 5916 tte_t tte, ttemod; 5917 struct sf_hment *sfhmep; 5918 int ttesz; 5919 long ttecnt; 5920 page_t *pp; 5921 kmutex_t *pml; 5922 int ret; 5923 int use_demap_range; 5924 5925 ASSERT(in_hblk_range(hmeblkp, addr)); 5926 ASSERT(!hmeblkp->hblk_shw_bit); 5927 ASSERT(sfmmup != NULL || hmeblkp->hblk_shared); 5928 ASSERT(sfmmup == NULL || !hmeblkp->hblk_shared); 5929 ASSERT(dmrp == NULL || !hmeblkp->hblk_shared); 5930 5931 #ifdef DEBUG 5932 if (get_hblk_ttesz(hmeblkp) != TTE8K && 5933 (endaddr < get_hblk_endaddr(hmeblkp))) { 5934 panic("sfmmu_hblk_unload: partial unload of large page"); 5935 } 5936 #endif /* DEBUG */ 5937 5938 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 5939 ttesz = get_hblk_ttesz(hmeblkp); 5940 5941 use_demap_range = ((dmrp == NULL) || 5942 (TTEBYTES(ttesz) == DEMAP_RANGE_PGSZ(dmrp))); 5943 5944 if (use_demap_range) { 5945 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr); 5946 } else { 5947 DEMAP_RANGE_FLUSH(dmrp); 5948 } 5949 ttecnt = 0; 5950 HBLKTOHME(sfhmep, hmeblkp, addr); 5951 5952 while (addr < endaddr) { 5953 pml = NULL; 5954 sfmmu_copytte(&sfhmep->hme_tte, &tte); 5955 if (TTE_IS_VALID(&tte)) { 5956 pp = sfhmep->hme_page; 5957 if (pp != NULL) { 5958 pml = sfmmu_mlist_enter(pp); 5959 } 5960 5961 /* 5962 * Verify if hme still points to 'pp' now that 5963 * we have p_mapping lock. 5964 */ 5965 if (sfhmep->hme_page != pp) { 5966 if (pp != NULL && sfhmep->hme_page != NULL) { 5967 ASSERT(pml != NULL); 5968 sfmmu_mlist_exit(pml); 5969 /* Re-start this iteration. */ 5970 continue; 5971 } 5972 ASSERT((pp != NULL) && 5973 (sfhmep->hme_page == NULL)); 5974 goto tte_unloaded; 5975 } 5976 5977 /* 5978 * This point on we have both HASH and p_mapping 5979 * lock. 5980 */ 5981 ASSERT(pp == sfhmep->hme_page); 5982 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 5983 5984 /* 5985 * We need to loop on modify tte because it is 5986 * possible for pagesync to come along and 5987 * change the software bits beneath us. 5988 * 5989 * Page_unload can also invalidate the tte after 5990 * we read tte outside of p_mapping lock. 5991 */ 5992 again: 5993 ttemod = tte; 5994 5995 TTE_SET_INVALID(&ttemod); 5996 ret = sfmmu_modifytte_try(&tte, &ttemod, 5997 &sfhmep->hme_tte); 5998 5999 if (ret <= 0) { 6000 if (TTE_IS_VALID(&tte)) { 6001 ASSERT(ret < 0); 6002 goto again; 6003 } 6004 if (pp != NULL) { 6005 panic("sfmmu_hblk_unload: pp = 0x%p " 6006 "tte became invalid under mlist" 6007 " lock = 0x%p", pp, pml); 6008 } 6009 continue; 6010 } 6011 6012 if (!(flags & HAT_UNLOAD_NOSYNC)) { 6013 sfmmu_ttesync(sfmmup, addr, &tte, pp); 6014 } 6015 6016 /* 6017 * Ok- we invalidated the tte. Do the rest of the job. 6018 */ 6019 ttecnt++; 6020 6021 if (flags & HAT_UNLOAD_UNLOCK) { 6022 ASSERT(hmeblkp->hblk_lckcnt > 0); 6023 atomic_add_32(&hmeblkp->hblk_lckcnt, -1); 6024 HBLK_STACK_TRACE(hmeblkp, HBLK_UNLOCK); 6025 } 6026 6027 /* 6028 * Normally we would need to flush the page 6029 * from the virtual cache at this point in 6030 * order to prevent a potential cache alias 6031 * inconsistency. 6032 * The particular scenario we need to worry 6033 * about is: 6034 * Given: va1 and va2 are two virtual address 6035 * that alias and map the same physical 6036 * address. 6037 * 1. mapping exists from va1 to pa and data 6038 * has been read into the cache. 6039 * 2. unload va1. 6040 * 3. load va2 and modify data using va2. 6041 * 4 unload va2. 6042 * 5. load va1 and reference data. Unless we 6043 * flush the data cache when we unload we will 6044 * get stale data. 6045 * Fortunately, page coloring eliminates the 6046 * above scenario by remembering the color a 6047 * physical page was last or is currently 6048 * mapped to. Now, we delay the flush until 6049 * the loading of translations. Only when the 6050 * new translation is of a different color 6051 * are we forced to flush. 6052 */ 6053 if (use_demap_range) { 6054 /* 6055 * Mark this page as needing a demap. 6056 */ 6057 DEMAP_RANGE_MARKPG(dmrp, addr); 6058 } else { 6059 ASSERT(sfmmup != NULL); 6060 ASSERT(!hmeblkp->hblk_shared); 6061 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 6062 sfmmup->sfmmu_free, 0); 6063 } 6064 6065 if (pp) { 6066 /* 6067 * Remove the hment from the mapping list 6068 */ 6069 ASSERT(hmeblkp->hblk_hmecnt > 0); 6070 6071 /* 6072 * Again, we cannot 6073 * ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS); 6074 */ 6075 HME_SUB(sfhmep, pp); 6076 membar_stst(); 6077 atomic_add_16(&hmeblkp->hblk_hmecnt, -1); 6078 } 6079 6080 ASSERT(hmeblkp->hblk_vcnt > 0); 6081 atomic_add_16(&hmeblkp->hblk_vcnt, -1); 6082 6083 ASSERT(hmeblkp->hblk_hmecnt || hmeblkp->hblk_vcnt || 6084 !hmeblkp->hblk_lckcnt); 6085 6086 #ifdef VAC 6087 if (pp && (pp->p_nrm & (P_KPMC | P_KPMS | P_TNC))) { 6088 if (PP_ISTNC(pp)) { 6089 /* 6090 * If page was temporary 6091 * uncached, try to recache 6092 * it. Note that HME_SUB() was 6093 * called above so p_index and 6094 * mlist had been updated. 6095 */ 6096 conv_tnc(pp, ttesz); 6097 } else if (pp->p_mapping == NULL) { 6098 ASSERT(kpm_enable); 6099 /* 6100 * Page is marked to be in VAC conflict 6101 * to an existing kpm mapping and/or is 6102 * kpm mapped using only the regular 6103 * pagesize. 6104 */ 6105 sfmmu_kpm_hme_unload(pp); 6106 } 6107 } 6108 #endif /* VAC */ 6109 } else if ((pp = sfhmep->hme_page) != NULL) { 6110 /* 6111 * TTE is invalid but the hme 6112 * still exists. let pageunload 6113 * complete its job. 6114 */ 6115 ASSERT(pml == NULL); 6116 pml = sfmmu_mlist_enter(pp); 6117 if (sfhmep->hme_page != NULL) { 6118 sfmmu_mlist_exit(pml); 6119 continue; 6120 } 6121 ASSERT(sfhmep->hme_page == NULL); 6122 } else if (hmeblkp->hblk_hmecnt != 0) { 6123 /* 6124 * pageunload may have not finished decrementing 6125 * hblk_vcnt and hblk_hmecnt. Find page_t if any and 6126 * wait for pageunload to finish. Rely on pageunload 6127 * to decrement hblk_hmecnt after hblk_vcnt. 6128 */ 6129 pfn_t pfn = TTE_TO_TTEPFN(&tte); 6130 ASSERT(pml == NULL); 6131 if (pf_is_memory(pfn)) { 6132 pp = page_numtopp_nolock(pfn); 6133 if (pp != NULL) { 6134 pml = sfmmu_mlist_enter(pp); 6135 sfmmu_mlist_exit(pml); 6136 pml = NULL; 6137 } 6138 } 6139 } 6140 6141 tte_unloaded: 6142 /* 6143 * At this point, the tte we are looking at 6144 * should be unloaded, and hme has been unlinked 6145 * from page too. This is important because in 6146 * pageunload, it does ttesync() then HME_SUB. 6147 * We need to make sure HME_SUB has been completed 6148 * so we know ttesync() has been completed. Otherwise, 6149 * at exit time, after return from hat layer, VM will 6150 * release as structure which hat_setstat() (called 6151 * by ttesync()) needs. 6152 */ 6153 #ifdef DEBUG 6154 { 6155 tte_t dtte; 6156 6157 ASSERT(sfhmep->hme_page == NULL); 6158 6159 sfmmu_copytte(&sfhmep->hme_tte, &dtte); 6160 ASSERT(!TTE_IS_VALID(&dtte)); 6161 } 6162 #endif 6163 6164 if (pml) { 6165 sfmmu_mlist_exit(pml); 6166 } 6167 6168 addr += TTEBYTES(ttesz); 6169 sfhmep++; 6170 DEMAP_RANGE_NEXTPG(dmrp); 6171 } 6172 /* 6173 * For shared hmeblks this routine is only called when region is freed 6174 * and no longer referenced. So no need to decrement ttecnt 6175 * in the region structure here. 6176 */ 6177 if (ttecnt > 0 && sfmmup != NULL) { 6178 atomic_add_long(&sfmmup->sfmmu_ttecnt[ttesz], -ttecnt); 6179 } 6180 return (addr); 6181 } 6182 6183 /* 6184 * Synchronize all the mappings in the range [addr..addr+len). 6185 * Can be called with clearflag having two states: 6186 * HAT_SYNC_DONTZERO means just return the rm stats 6187 * HAT_SYNC_ZERORM means zero rm bits in the tte and return the stats 6188 */ 6189 void 6190 hat_sync(struct hat *sfmmup, caddr_t addr, size_t len, uint_t clearflag) 6191 { 6192 struct hmehash_bucket *hmebp; 6193 hmeblk_tag hblktag; 6194 int hmeshift, hashno = 1; 6195 struct hme_blk *hmeblkp, *list = NULL; 6196 caddr_t endaddr; 6197 cpuset_t cpuset; 6198 6199 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 6200 ASSERT((sfmmup == ksfmmup) || 6201 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 6202 ASSERT((len & MMU_PAGEOFFSET) == 0); 6203 ASSERT((clearflag == HAT_SYNC_DONTZERO) || 6204 (clearflag == HAT_SYNC_ZERORM)); 6205 6206 CPUSET_ZERO(cpuset); 6207 6208 endaddr = addr + len; 6209 hblktag.htag_id = sfmmup; 6210 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 6211 6212 /* 6213 * Spitfire supports 4 page sizes. 6214 * Most pages are expected to be of the smallest page 6215 * size (8K) and these will not need to be rehashed. 64K 6216 * pages also don't need to be rehashed because the an hmeblk 6217 * spans 64K of address space. 512K pages might need 1 rehash and 6218 * and 4M pages 2 rehashes. 6219 */ 6220 while (addr < endaddr) { 6221 hmeshift = HME_HASH_SHIFT(hashno); 6222 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 6223 hblktag.htag_rehash = hashno; 6224 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 6225 6226 SFMMU_HASH_LOCK(hmebp); 6227 6228 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 6229 if (hmeblkp != NULL) { 6230 ASSERT(!hmeblkp->hblk_shared); 6231 /* 6232 * We've encountered a shadow hmeblk so skip the range 6233 * of the next smaller mapping size. 6234 */ 6235 if (hmeblkp->hblk_shw_bit) { 6236 ASSERT(sfmmup != ksfmmup); 6237 ASSERT(hashno > 1); 6238 addr = (caddr_t)P2END((uintptr_t)addr, 6239 TTEBYTES(hashno - 1)); 6240 } else { 6241 addr = sfmmu_hblk_sync(sfmmup, hmeblkp, 6242 addr, endaddr, clearflag); 6243 } 6244 SFMMU_HASH_UNLOCK(hmebp); 6245 hashno = 1; 6246 continue; 6247 } 6248 SFMMU_HASH_UNLOCK(hmebp); 6249 6250 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 6251 /* 6252 * We have traversed the whole list and rehashed 6253 * if necessary without finding the address to sync. 6254 * This is ok so we increment the address by the 6255 * smallest hmeblk range for kernel mappings and the 6256 * largest hmeblk range, to account for shadow hmeblks, 6257 * for user mappings and continue. 6258 */ 6259 if (sfmmup == ksfmmup) 6260 addr = (caddr_t)P2END((uintptr_t)addr, 6261 TTEBYTES(1)); 6262 else 6263 addr = (caddr_t)P2END((uintptr_t)addr, 6264 TTEBYTES(hashno)); 6265 hashno = 1; 6266 } else { 6267 hashno++; 6268 } 6269 } 6270 sfmmu_hblks_list_purge(&list); 6271 cpuset = sfmmup->sfmmu_cpusran; 6272 xt_sync(cpuset); 6273 } 6274 6275 static caddr_t 6276 sfmmu_hblk_sync(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 6277 caddr_t endaddr, int clearflag) 6278 { 6279 tte_t tte, ttemod; 6280 struct sf_hment *sfhmep; 6281 int ttesz; 6282 struct page *pp; 6283 kmutex_t *pml; 6284 int ret; 6285 6286 ASSERT(hmeblkp->hblk_shw_bit == 0); 6287 ASSERT(!hmeblkp->hblk_shared); 6288 6289 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 6290 6291 ttesz = get_hblk_ttesz(hmeblkp); 6292 HBLKTOHME(sfhmep, hmeblkp, addr); 6293 6294 while (addr < endaddr) { 6295 sfmmu_copytte(&sfhmep->hme_tte, &tte); 6296 if (TTE_IS_VALID(&tte)) { 6297 pml = NULL; 6298 pp = sfhmep->hme_page; 6299 if (pp) { 6300 pml = sfmmu_mlist_enter(pp); 6301 } 6302 if (pp != sfhmep->hme_page) { 6303 /* 6304 * tte most have been unloaded 6305 * underneath us. Recheck 6306 */ 6307 ASSERT(pml); 6308 sfmmu_mlist_exit(pml); 6309 continue; 6310 } 6311 6312 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 6313 6314 if (clearflag == HAT_SYNC_ZERORM) { 6315 ttemod = tte; 6316 TTE_CLR_RM(&ttemod); 6317 ret = sfmmu_modifytte_try(&tte, &ttemod, 6318 &sfhmep->hme_tte); 6319 if (ret < 0) { 6320 if (pml) { 6321 sfmmu_mlist_exit(pml); 6322 } 6323 continue; 6324 } 6325 6326 if (ret > 0) { 6327 sfmmu_tlb_demap(addr, sfmmup, 6328 hmeblkp, 0, 0); 6329 } 6330 } 6331 sfmmu_ttesync(sfmmup, addr, &tte, pp); 6332 if (pml) { 6333 sfmmu_mlist_exit(pml); 6334 } 6335 } 6336 addr += TTEBYTES(ttesz); 6337 sfhmep++; 6338 } 6339 return (addr); 6340 } 6341 6342 /* 6343 * This function will sync a tte to the page struct and it will 6344 * update the hat stats. Currently it allows us to pass a NULL pp 6345 * and we will simply update the stats. We may want to change this 6346 * so we only keep stats for pages backed by pp's. 6347 */ 6348 static void 6349 sfmmu_ttesync(struct hat *sfmmup, caddr_t addr, tte_t *ttep, page_t *pp) 6350 { 6351 uint_t rm = 0; 6352 int sz; 6353 pgcnt_t npgs; 6354 6355 ASSERT(TTE_IS_VALID(ttep)); 6356 6357 if (TTE_IS_NOSYNC(ttep)) { 6358 return; 6359 } 6360 6361 if (TTE_IS_REF(ttep)) { 6362 rm = P_REF; 6363 } 6364 if (TTE_IS_MOD(ttep)) { 6365 rm |= P_MOD; 6366 } 6367 6368 if (rm == 0) { 6369 return; 6370 } 6371 6372 sz = TTE_CSZ(ttep); 6373 if (sfmmup != NULL && sfmmup->sfmmu_rmstat) { 6374 int i; 6375 caddr_t vaddr = addr; 6376 6377 for (i = 0; i < TTEPAGES(sz); i++, vaddr += MMU_PAGESIZE) { 6378 hat_setstat(sfmmup->sfmmu_as, vaddr, MMU_PAGESIZE, rm); 6379 } 6380 6381 } 6382 6383 /* 6384 * XXX I want to use cas to update nrm bits but they 6385 * currently belong in common/vm and not in hat where 6386 * they should be. 6387 * The nrm bits are protected by the same mutex as 6388 * the one that protects the page's mapping list. 6389 */ 6390 if (!pp) 6391 return; 6392 ASSERT(sfmmu_mlist_held(pp)); 6393 /* 6394 * If the tte is for a large page, we need to sync all the 6395 * pages covered by the tte. 6396 */ 6397 if (sz != TTE8K) { 6398 ASSERT(pp->p_szc != 0); 6399 pp = PP_GROUPLEADER(pp, sz); 6400 ASSERT(sfmmu_mlist_held(pp)); 6401 } 6402 6403 /* Get number of pages from tte size. */ 6404 npgs = TTEPAGES(sz); 6405 6406 do { 6407 ASSERT(pp); 6408 ASSERT(sfmmu_mlist_held(pp)); 6409 if (((rm & P_REF) != 0 && !PP_ISREF(pp)) || 6410 ((rm & P_MOD) != 0 && !PP_ISMOD(pp))) 6411 hat_page_setattr(pp, rm); 6412 6413 /* 6414 * Are we done? If not, we must have a large mapping. 6415 * For large mappings we need to sync the rest of the pages 6416 * covered by this tte; goto the next page. 6417 */ 6418 } while (--npgs > 0 && (pp = PP_PAGENEXT(pp))); 6419 } 6420 6421 /* 6422 * Execute pre-callback handler of each pa_hment linked to pp 6423 * 6424 * Inputs: 6425 * flag: either HAT_PRESUSPEND or HAT_SUSPEND. 6426 * capture_cpus: pointer to return value (below) 6427 * 6428 * Returns: 6429 * Propagates the subsystem callback return values back to the caller; 6430 * returns 0 on success. If capture_cpus is non-NULL, the value returned 6431 * is zero if all of the pa_hments are of a type that do not require 6432 * capturing CPUs prior to suspending the mapping, else it is 1. 6433 */ 6434 static int 6435 hat_pageprocess_precallbacks(struct page *pp, uint_t flag, int *capture_cpus) 6436 { 6437 struct sf_hment *sfhmep; 6438 struct pa_hment *pahmep; 6439 int (*f)(caddr_t, uint_t, uint_t, void *); 6440 int ret; 6441 id_t id; 6442 int locked = 0; 6443 kmutex_t *pml; 6444 6445 ASSERT(PAGE_EXCL(pp)); 6446 if (!sfmmu_mlist_held(pp)) { 6447 pml = sfmmu_mlist_enter(pp); 6448 locked = 1; 6449 } 6450 6451 if (capture_cpus) 6452 *capture_cpus = 0; 6453 6454 top: 6455 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) { 6456 /* 6457 * skip sf_hments corresponding to VA<->PA mappings; 6458 * for pa_hment's, hme_tte.ll is zero 6459 */ 6460 if (!IS_PAHME(sfhmep)) 6461 continue; 6462 6463 pahmep = sfhmep->hme_data; 6464 ASSERT(pahmep != NULL); 6465 6466 /* 6467 * skip if pre-handler has been called earlier in this loop 6468 */ 6469 if (pahmep->flags & flag) 6470 continue; 6471 6472 id = pahmep->cb_id; 6473 ASSERT(id >= (id_t)0 && id < sfmmu_cb_nextid); 6474 if (capture_cpus && sfmmu_cb_table[id].capture_cpus != 0) 6475 *capture_cpus = 1; 6476 if ((f = sfmmu_cb_table[id].prehandler) == NULL) { 6477 pahmep->flags |= flag; 6478 continue; 6479 } 6480 6481 /* 6482 * Drop the mapping list lock to avoid locking order issues. 6483 */ 6484 if (locked) 6485 sfmmu_mlist_exit(pml); 6486 6487 ret = f(pahmep->addr, pahmep->len, flag, pahmep->pvt); 6488 if (ret != 0) 6489 return (ret); /* caller must do the cleanup */ 6490 6491 if (locked) { 6492 pml = sfmmu_mlist_enter(pp); 6493 pahmep->flags |= flag; 6494 goto top; 6495 } 6496 6497 pahmep->flags |= flag; 6498 } 6499 6500 if (locked) 6501 sfmmu_mlist_exit(pml); 6502 6503 return (0); 6504 } 6505 6506 /* 6507 * Execute post-callback handler of each pa_hment linked to pp 6508 * 6509 * Same overall assumptions and restrictions apply as for 6510 * hat_pageprocess_precallbacks(). 6511 */ 6512 static void 6513 hat_pageprocess_postcallbacks(struct page *pp, uint_t flag) 6514 { 6515 pfn_t pgpfn = pp->p_pagenum; 6516 pfn_t pgmask = btop(page_get_pagesize(pp->p_szc)) - 1; 6517 pfn_t newpfn; 6518 struct sf_hment *sfhmep; 6519 struct pa_hment *pahmep; 6520 int (*f)(caddr_t, uint_t, uint_t, void *, pfn_t); 6521 id_t id; 6522 int locked = 0; 6523 kmutex_t *pml; 6524 6525 ASSERT(PAGE_EXCL(pp)); 6526 if (!sfmmu_mlist_held(pp)) { 6527 pml = sfmmu_mlist_enter(pp); 6528 locked = 1; 6529 } 6530 6531 top: 6532 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) { 6533 /* 6534 * skip sf_hments corresponding to VA<->PA mappings; 6535 * for pa_hment's, hme_tte.ll is zero 6536 */ 6537 if (!IS_PAHME(sfhmep)) 6538 continue; 6539 6540 pahmep = sfhmep->hme_data; 6541 ASSERT(pahmep != NULL); 6542 6543 if ((pahmep->flags & flag) == 0) 6544 continue; 6545 6546 pahmep->flags &= ~flag; 6547 6548 id = pahmep->cb_id; 6549 ASSERT(id >= (id_t)0 && id < sfmmu_cb_nextid); 6550 if ((f = sfmmu_cb_table[id].posthandler) == NULL) 6551 continue; 6552 6553 /* 6554 * Convert the base page PFN into the constituent PFN 6555 * which is needed by the callback handler. 6556 */ 6557 newpfn = pgpfn | (btop((uintptr_t)pahmep->addr) & pgmask); 6558 6559 /* 6560 * Drop the mapping list lock to avoid locking order issues. 6561 */ 6562 if (locked) 6563 sfmmu_mlist_exit(pml); 6564 6565 if (f(pahmep->addr, pahmep->len, flag, pahmep->pvt, newpfn) 6566 != 0) 6567 panic("sfmmu: posthandler failed"); 6568 6569 if (locked) { 6570 pml = sfmmu_mlist_enter(pp); 6571 goto top; 6572 } 6573 } 6574 6575 if (locked) 6576 sfmmu_mlist_exit(pml); 6577 } 6578 6579 /* 6580 * Suspend locked kernel mapping 6581 */ 6582 void 6583 hat_pagesuspend(struct page *pp) 6584 { 6585 struct sf_hment *sfhmep; 6586 sfmmu_t *sfmmup; 6587 tte_t tte, ttemod; 6588 struct hme_blk *hmeblkp; 6589 caddr_t addr; 6590 int index, cons; 6591 cpuset_t cpuset; 6592 6593 ASSERT(PAGE_EXCL(pp)); 6594 ASSERT(sfmmu_mlist_held(pp)); 6595 6596 mutex_enter(&kpr_suspendlock); 6597 6598 /* 6599 * We're about to suspend a kernel mapping so mark this thread as 6600 * non-traceable by DTrace. This prevents us from running into issues 6601 * with probe context trying to touch a suspended page 6602 * in the relocation codepath itself. 6603 */ 6604 curthread->t_flag |= T_DONTDTRACE; 6605 6606 index = PP_MAPINDEX(pp); 6607 cons = TTE8K; 6608 6609 retry: 6610 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) { 6611 6612 if (IS_PAHME(sfhmep)) 6613 continue; 6614 6615 if (get_hblk_ttesz(sfmmu_hmetohblk(sfhmep)) != cons) 6616 continue; 6617 6618 /* 6619 * Loop until we successfully set the suspend bit in 6620 * the TTE. 6621 */ 6622 again: 6623 sfmmu_copytte(&sfhmep->hme_tte, &tte); 6624 ASSERT(TTE_IS_VALID(&tte)); 6625 6626 ttemod = tte; 6627 TTE_SET_SUSPEND(&ttemod); 6628 if (sfmmu_modifytte_try(&tte, &ttemod, 6629 &sfhmep->hme_tte) < 0) 6630 goto again; 6631 6632 /* 6633 * Invalidate TSB entry 6634 */ 6635 hmeblkp = sfmmu_hmetohblk(sfhmep); 6636 6637 sfmmup = hblktosfmmu(hmeblkp); 6638 ASSERT(sfmmup == ksfmmup); 6639 ASSERT(!hmeblkp->hblk_shared); 6640 6641 addr = tte_to_vaddr(hmeblkp, tte); 6642 6643 /* 6644 * No need to make sure that the TSB for this sfmmu is 6645 * not being relocated since it is ksfmmup and thus it 6646 * will never be relocated. 6647 */ 6648 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0); 6649 6650 /* 6651 * Update xcall stats 6652 */ 6653 cpuset = cpu_ready_set; 6654 CPUSET_DEL(cpuset, CPU->cpu_id); 6655 6656 /* LINTED: constant in conditional context */ 6657 SFMMU_XCALL_STATS(ksfmmup); 6658 6659 /* 6660 * Flush TLB entry on remote CPU's 6661 */ 6662 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr, 6663 (uint64_t)ksfmmup); 6664 xt_sync(cpuset); 6665 6666 /* 6667 * Flush TLB entry on local CPU 6668 */ 6669 vtag_flushpage(addr, (uint64_t)ksfmmup); 6670 } 6671 6672 while (index != 0) { 6673 index = index >> 1; 6674 if (index != 0) 6675 cons++; 6676 if (index & 0x1) { 6677 pp = PP_GROUPLEADER(pp, cons); 6678 goto retry; 6679 } 6680 } 6681 } 6682 6683 #ifdef DEBUG 6684 6685 #define N_PRLE 1024 6686 struct prle { 6687 page_t *targ; 6688 page_t *repl; 6689 int status; 6690 int pausecpus; 6691 hrtime_t whence; 6692 }; 6693 6694 static struct prle page_relocate_log[N_PRLE]; 6695 static int prl_entry; 6696 static kmutex_t prl_mutex; 6697 6698 #define PAGE_RELOCATE_LOG(t, r, s, p) \ 6699 mutex_enter(&prl_mutex); \ 6700 page_relocate_log[prl_entry].targ = *(t); \ 6701 page_relocate_log[prl_entry].repl = *(r); \ 6702 page_relocate_log[prl_entry].status = (s); \ 6703 page_relocate_log[prl_entry].pausecpus = (p); \ 6704 page_relocate_log[prl_entry].whence = gethrtime(); \ 6705 prl_entry = (prl_entry == (N_PRLE - 1))? 0 : prl_entry + 1; \ 6706 mutex_exit(&prl_mutex); 6707 6708 #else /* !DEBUG */ 6709 #define PAGE_RELOCATE_LOG(t, r, s, p) 6710 #endif 6711 6712 /* 6713 * Core Kernel Page Relocation Algorithm 6714 * 6715 * Input: 6716 * 6717 * target : constituent pages are SE_EXCL locked. 6718 * replacement: constituent pages are SE_EXCL locked. 6719 * 6720 * Output: 6721 * 6722 * nrelocp: number of pages relocated 6723 */ 6724 int 6725 hat_page_relocate(page_t **target, page_t **replacement, spgcnt_t *nrelocp) 6726 { 6727 page_t *targ, *repl; 6728 page_t *tpp, *rpp; 6729 kmutex_t *low, *high; 6730 spgcnt_t npages, i; 6731 page_t *pl = NULL; 6732 int old_pil; 6733 cpuset_t cpuset; 6734 int cap_cpus; 6735 int ret; 6736 6737 if (hat_kpr_enabled == 0 || !kcage_on || PP_ISNORELOC(*target)) { 6738 PAGE_RELOCATE_LOG(target, replacement, EAGAIN, -1); 6739 return (EAGAIN); 6740 } 6741 6742 mutex_enter(&kpr_mutex); 6743 kreloc_thread = curthread; 6744 6745 targ = *target; 6746 repl = *replacement; 6747 ASSERT(repl != NULL); 6748 ASSERT(targ->p_szc == repl->p_szc); 6749 6750 npages = page_get_pagecnt(targ->p_szc); 6751 6752 /* 6753 * unload VA<->PA mappings that are not locked 6754 */ 6755 tpp = targ; 6756 for (i = 0; i < npages; i++) { 6757 (void) hat_pageunload(tpp, SFMMU_KERNEL_RELOC); 6758 tpp++; 6759 } 6760 6761 /* 6762 * Do "presuspend" callbacks, in a context from which we can still 6763 * block as needed. Note that we don't hold the mapping list lock 6764 * of "targ" at this point due to potential locking order issues; 6765 * we assume that between the hat_pageunload() above and holding 6766 * the SE_EXCL lock that the mapping list *cannot* change at this 6767 * point. 6768 */ 6769 ret = hat_pageprocess_precallbacks(targ, HAT_PRESUSPEND, &cap_cpus); 6770 if (ret != 0) { 6771 /* 6772 * EIO translates to fatal error, for all others cleanup 6773 * and return EAGAIN. 6774 */ 6775 ASSERT(ret != EIO); 6776 hat_pageprocess_postcallbacks(targ, HAT_POSTUNSUSPEND); 6777 PAGE_RELOCATE_LOG(target, replacement, ret, -1); 6778 kreloc_thread = NULL; 6779 mutex_exit(&kpr_mutex); 6780 return (EAGAIN); 6781 } 6782 6783 /* 6784 * acquire p_mapping list lock for both the target and replacement 6785 * root pages. 6786 * 6787 * low and high refer to the need to grab the mlist locks in a 6788 * specific order in order to prevent race conditions. Thus the 6789 * lower lock must be grabbed before the higher lock. 6790 * 6791 * This will block hat_unload's accessing p_mapping list. Since 6792 * we have SE_EXCL lock, hat_memload and hat_pageunload will be 6793 * blocked. Thus, no one else will be accessing the p_mapping list 6794 * while we suspend and reload the locked mapping below. 6795 */ 6796 tpp = targ; 6797 rpp = repl; 6798 sfmmu_mlist_reloc_enter(tpp, rpp, &low, &high); 6799 6800 kpreempt_disable(); 6801 6802 #ifdef VAC 6803 /* 6804 * If the replacement page is of a different virtual color 6805 * than the page it is replacing, we need to handle the VAC 6806 * consistency for it just as we would if we were setting up 6807 * a new mapping to a page. 6808 */ 6809 if ((tpp->p_szc == 0) && (PP_GET_VCOLOR(rpp) != NO_VCOLOR)) { 6810 if (tpp->p_vcolor != rpp->p_vcolor) { 6811 sfmmu_cache_flushcolor(PP_GET_VCOLOR(rpp), 6812 rpp->p_pagenum); 6813 } 6814 } 6815 #endif 6816 6817 /* 6818 * We raise our PIL to 13 so that we don't get captured by 6819 * another CPU or pinned by an interrupt thread. We can't go to 6820 * PIL 14 since the nexus driver(s) may need to interrupt at 6821 * that level in the case of IOMMU pseudo mappings. 6822 */ 6823 cpuset = cpu_ready_set; 6824 CPUSET_DEL(cpuset, CPU->cpu_id); 6825 if (!cap_cpus || CPUSET_ISNULL(cpuset)) { 6826 old_pil = splr(XCALL_PIL); 6827 } else { 6828 old_pil = -1; 6829 xc_attention(cpuset); 6830 } 6831 ASSERT(getpil() == XCALL_PIL); 6832 6833 /* 6834 * Now do suspend callbacks. In the case of an IOMMU mapping 6835 * this will suspend all DMA activity to the page while it is 6836 * being relocated. Since we are well above LOCK_LEVEL and CPUs 6837 * may be captured at this point we should have acquired any needed 6838 * locks in the presuspend callback. 6839 */ 6840 ret = hat_pageprocess_precallbacks(targ, HAT_SUSPEND, NULL); 6841 if (ret != 0) { 6842 repl = targ; 6843 goto suspend_fail; 6844 } 6845 6846 /* 6847 * Raise the PIL yet again, this time to block all high-level 6848 * interrupts on this CPU. This is necessary to prevent an 6849 * interrupt routine from pinning the thread which holds the 6850 * mapping suspended and then touching the suspended page. 6851 * 6852 * Once the page is suspended we also need to be careful to 6853 * avoid calling any functions which touch any seg_kmem memory 6854 * since that memory may be backed by the very page we are 6855 * relocating in here! 6856 */ 6857 hat_pagesuspend(targ); 6858 6859 /* 6860 * Now that we are confident everybody has stopped using this page, 6861 * copy the page contents. Note we use a physical copy to prevent 6862 * locking issues and to avoid fpRAS because we can't handle it in 6863 * this context. 6864 */ 6865 for (i = 0; i < npages; i++, tpp++, rpp++) { 6866 /* 6867 * Copy the contents of the page. 6868 */ 6869 ppcopy_kernel(tpp, rpp); 6870 } 6871 6872 tpp = targ; 6873 rpp = repl; 6874 for (i = 0; i < npages; i++, tpp++, rpp++) { 6875 /* 6876 * Copy attributes. VAC consistency was handled above, 6877 * if required. 6878 */ 6879 rpp->p_nrm = tpp->p_nrm; 6880 tpp->p_nrm = 0; 6881 rpp->p_index = tpp->p_index; 6882 tpp->p_index = 0; 6883 #ifdef VAC 6884 rpp->p_vcolor = tpp->p_vcolor; 6885 #endif 6886 } 6887 6888 /* 6889 * First, unsuspend the page, if we set the suspend bit, and transfer 6890 * the mapping list from the target page to the replacement page. 6891 * Next process postcallbacks; since pa_hment's are linked only to the 6892 * p_mapping list of root page, we don't iterate over the constituent 6893 * pages. 6894 */ 6895 hat_pagereload(targ, repl); 6896 6897 suspend_fail: 6898 hat_pageprocess_postcallbacks(repl, HAT_UNSUSPEND); 6899 6900 /* 6901 * Now lower our PIL and release any captured CPUs since we 6902 * are out of the "danger zone". After this it will again be 6903 * safe to acquire adaptive mutex locks, or to drop them... 6904 */ 6905 if (old_pil != -1) { 6906 splx(old_pil); 6907 } else { 6908 xc_dismissed(cpuset); 6909 } 6910 6911 kpreempt_enable(); 6912 6913 sfmmu_mlist_reloc_exit(low, high); 6914 6915 /* 6916 * Postsuspend callbacks should drop any locks held across 6917 * the suspend callbacks. As before, we don't hold the mapping 6918 * list lock at this point.. our assumption is that the mapping 6919 * list still can't change due to our holding SE_EXCL lock and 6920 * there being no unlocked mappings left. Hence the restriction 6921 * on calling context to hat_delete_callback() 6922 */ 6923 hat_pageprocess_postcallbacks(repl, HAT_POSTUNSUSPEND); 6924 if (ret != 0) { 6925 /* 6926 * The second presuspend call failed: we got here through 6927 * the suspend_fail label above. 6928 */ 6929 ASSERT(ret != EIO); 6930 PAGE_RELOCATE_LOG(target, replacement, ret, cap_cpus); 6931 kreloc_thread = NULL; 6932 mutex_exit(&kpr_mutex); 6933 return (EAGAIN); 6934 } 6935 6936 /* 6937 * Now that we're out of the performance critical section we can 6938 * take care of updating the hash table, since we still 6939 * hold all the pages locked SE_EXCL at this point we 6940 * needn't worry about things changing out from under us. 6941 */ 6942 tpp = targ; 6943 rpp = repl; 6944 for (i = 0; i < npages; i++, tpp++, rpp++) { 6945 6946 /* 6947 * replace targ with replacement in page_hash table 6948 */ 6949 targ = tpp; 6950 page_relocate_hash(rpp, targ); 6951 6952 /* 6953 * concatenate target; caller of platform_page_relocate() 6954 * expects target to be concatenated after returning. 6955 */ 6956 ASSERT(targ->p_next == targ); 6957 ASSERT(targ->p_prev == targ); 6958 page_list_concat(&pl, &targ); 6959 } 6960 6961 ASSERT(*target == pl); 6962 *nrelocp = npages; 6963 PAGE_RELOCATE_LOG(target, replacement, 0, cap_cpus); 6964 kreloc_thread = NULL; 6965 mutex_exit(&kpr_mutex); 6966 return (0); 6967 } 6968 6969 /* 6970 * Called when stray pa_hments are found attached to a page which is 6971 * being freed. Notify the subsystem which attached the pa_hment of 6972 * the error if it registered a suitable handler, else panic. 6973 */ 6974 static void 6975 sfmmu_pahment_leaked(struct pa_hment *pahmep) 6976 { 6977 id_t cb_id = pahmep->cb_id; 6978 6979 ASSERT(cb_id >= (id_t)0 && cb_id < sfmmu_cb_nextid); 6980 if (sfmmu_cb_table[cb_id].errhandler != NULL) { 6981 if (sfmmu_cb_table[cb_id].errhandler(pahmep->addr, pahmep->len, 6982 HAT_CB_ERR_LEAKED, pahmep->pvt) == 0) 6983 return; /* non-fatal */ 6984 } 6985 panic("pa_hment leaked: 0x%p", pahmep); 6986 } 6987 6988 /* 6989 * Remove all mappings to page 'pp'. 6990 */ 6991 int 6992 hat_pageunload(struct page *pp, uint_t forceflag) 6993 { 6994 struct page *origpp = pp; 6995 struct sf_hment *sfhme, *tmphme; 6996 struct hme_blk *hmeblkp; 6997 kmutex_t *pml; 6998 #ifdef VAC 6999 kmutex_t *pmtx; 7000 #endif 7001 cpuset_t cpuset, tset; 7002 int index, cons; 7003 int xhme_blks; 7004 int pa_hments; 7005 7006 ASSERT(PAGE_EXCL(pp)); 7007 7008 retry_xhat: 7009 tmphme = NULL; 7010 xhme_blks = 0; 7011 pa_hments = 0; 7012 CPUSET_ZERO(cpuset); 7013 7014 pml = sfmmu_mlist_enter(pp); 7015 7016 #ifdef VAC 7017 if (pp->p_kpmref) 7018 sfmmu_kpm_pageunload(pp); 7019 ASSERT(!PP_ISMAPPED_KPM(pp)); 7020 #endif 7021 7022 index = PP_MAPINDEX(pp); 7023 cons = TTE8K; 7024 retry: 7025 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 7026 tmphme = sfhme->hme_next; 7027 7028 if (IS_PAHME(sfhme)) { 7029 ASSERT(sfhme->hme_data != NULL); 7030 pa_hments++; 7031 continue; 7032 } 7033 7034 hmeblkp = sfmmu_hmetohblk(sfhme); 7035 if (hmeblkp->hblk_xhat_bit) { 7036 struct xhat_hme_blk *xblk = 7037 (struct xhat_hme_blk *)hmeblkp; 7038 7039 (void) XHAT_PAGEUNLOAD(xblk->xhat_hme_blk_hat, 7040 pp, forceflag, XBLK2PROVBLK(xblk)); 7041 7042 xhme_blks = 1; 7043 continue; 7044 } 7045 7046 /* 7047 * If there are kernel mappings don't unload them, they will 7048 * be suspended. 7049 */ 7050 if (forceflag == SFMMU_KERNEL_RELOC && hmeblkp->hblk_lckcnt && 7051 hmeblkp->hblk_tag.htag_id == ksfmmup) 7052 continue; 7053 7054 tset = sfmmu_pageunload(pp, sfhme, cons); 7055 CPUSET_OR(cpuset, tset); 7056 } 7057 7058 while (index != 0) { 7059 index = index >> 1; 7060 if (index != 0) 7061 cons++; 7062 if (index & 0x1) { 7063 /* Go to leading page */ 7064 pp = PP_GROUPLEADER(pp, cons); 7065 ASSERT(sfmmu_mlist_held(pp)); 7066 goto retry; 7067 } 7068 } 7069 7070 /* 7071 * cpuset may be empty if the page was only mapped by segkpm, 7072 * in which case we won't actually cross-trap. 7073 */ 7074 xt_sync(cpuset); 7075 7076 /* 7077 * The page should have no mappings at this point, unless 7078 * we were called from hat_page_relocate() in which case we 7079 * leave the locked mappings which will be suspended later. 7080 */ 7081 ASSERT(!PP_ISMAPPED(origpp) || xhme_blks || pa_hments || 7082 (forceflag == SFMMU_KERNEL_RELOC)); 7083 7084 #ifdef VAC 7085 if (PP_ISTNC(pp)) { 7086 if (cons == TTE8K) { 7087 pmtx = sfmmu_page_enter(pp); 7088 PP_CLRTNC(pp); 7089 sfmmu_page_exit(pmtx); 7090 } else { 7091 conv_tnc(pp, cons); 7092 } 7093 } 7094 #endif /* VAC */ 7095 7096 if (pa_hments && forceflag != SFMMU_KERNEL_RELOC) { 7097 /* 7098 * Unlink any pa_hments and free them, calling back 7099 * the responsible subsystem to notify it of the error. 7100 * This can occur in situations such as drivers leaking 7101 * DMA handles: naughty, but common enough that we'd like 7102 * to keep the system running rather than bringing it 7103 * down with an obscure error like "pa_hment leaked" 7104 * which doesn't aid the user in debugging their driver. 7105 */ 7106 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 7107 tmphme = sfhme->hme_next; 7108 if (IS_PAHME(sfhme)) { 7109 struct pa_hment *pahmep = sfhme->hme_data; 7110 sfmmu_pahment_leaked(pahmep); 7111 HME_SUB(sfhme, pp); 7112 kmem_cache_free(pa_hment_cache, pahmep); 7113 } 7114 } 7115 7116 ASSERT(!PP_ISMAPPED(origpp) || xhme_blks); 7117 } 7118 7119 sfmmu_mlist_exit(pml); 7120 7121 /* 7122 * XHAT may not have finished unloading pages 7123 * because some other thread was waiting for 7124 * mlist lock and XHAT_PAGEUNLOAD let it do 7125 * the job. 7126 */ 7127 if (xhme_blks) { 7128 pp = origpp; 7129 goto retry_xhat; 7130 } 7131 7132 return (0); 7133 } 7134 7135 cpuset_t 7136 sfmmu_pageunload(page_t *pp, struct sf_hment *sfhme, int cons) 7137 { 7138 struct hme_blk *hmeblkp; 7139 sfmmu_t *sfmmup; 7140 tte_t tte, ttemod; 7141 #ifdef DEBUG 7142 tte_t orig_old; 7143 #endif /* DEBUG */ 7144 caddr_t addr; 7145 int ttesz; 7146 int ret; 7147 cpuset_t cpuset; 7148 7149 ASSERT(pp != NULL); 7150 ASSERT(sfmmu_mlist_held(pp)); 7151 ASSERT(!PP_ISKAS(pp)); 7152 7153 CPUSET_ZERO(cpuset); 7154 7155 hmeblkp = sfmmu_hmetohblk(sfhme); 7156 7157 readtte: 7158 sfmmu_copytte(&sfhme->hme_tte, &tte); 7159 if (TTE_IS_VALID(&tte)) { 7160 sfmmup = hblktosfmmu(hmeblkp); 7161 ttesz = get_hblk_ttesz(hmeblkp); 7162 /* 7163 * Only unload mappings of 'cons' size. 7164 */ 7165 if (ttesz != cons) 7166 return (cpuset); 7167 7168 /* 7169 * Note that we have p_mapping lock, but no hash lock here. 7170 * hblk_unload() has to have both hash lock AND p_mapping 7171 * lock before it tries to modify tte. So, the tte could 7172 * not become invalid in the sfmmu_modifytte_try() below. 7173 */ 7174 ttemod = tte; 7175 #ifdef DEBUG 7176 orig_old = tte; 7177 #endif /* DEBUG */ 7178 7179 TTE_SET_INVALID(&ttemod); 7180 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte); 7181 if (ret < 0) { 7182 #ifdef DEBUG 7183 /* only R/M bits can change. */ 7184 chk_tte(&orig_old, &tte, &ttemod, hmeblkp); 7185 #endif /* DEBUG */ 7186 goto readtte; 7187 } 7188 7189 if (ret == 0) { 7190 panic("pageunload: cas failed?"); 7191 } 7192 7193 addr = tte_to_vaddr(hmeblkp, tte); 7194 7195 if (hmeblkp->hblk_shared) { 7196 sf_srd_t *srdp = (sf_srd_t *)sfmmup; 7197 uint_t rid = hmeblkp->hblk_tag.htag_rid; 7198 sf_region_t *rgnp; 7199 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 7200 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 7201 ASSERT(srdp != NULL); 7202 rgnp = srdp->srd_hmergnp[rid]; 7203 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid); 7204 cpuset = sfmmu_rgntlb_demap(addr, rgnp, hmeblkp, 1); 7205 sfmmu_ttesync(NULL, addr, &tte, pp); 7206 ASSERT(rgnp->rgn_ttecnt[ttesz] > 0); 7207 atomic_add_long(&rgnp->rgn_ttecnt[ttesz], -1); 7208 } else { 7209 sfmmu_ttesync(sfmmup, addr, &tte, pp); 7210 atomic_add_long(&sfmmup->sfmmu_ttecnt[ttesz], -1); 7211 7212 /* 7213 * We need to flush the page from the virtual cache 7214 * in order to prevent a virtual cache alias 7215 * inconsistency. The particular scenario we need 7216 * to worry about is: 7217 * Given: va1 and va2 are two virtual address that 7218 * alias and will map the same physical address. 7219 * 1. mapping exists from va1 to pa and data has 7220 * been read into the cache. 7221 * 2. unload va1. 7222 * 3. load va2 and modify data using va2. 7223 * 4 unload va2. 7224 * 5. load va1 and reference data. Unless we flush 7225 * the data cache when we unload we will get 7226 * stale data. 7227 * This scenario is taken care of by using virtual 7228 * page coloring. 7229 */ 7230 if (sfmmup->sfmmu_ismhat) { 7231 /* 7232 * Flush TSBs, TLBs and caches 7233 * of every process 7234 * sharing this ism segment. 7235 */ 7236 sfmmu_hat_lock_all(); 7237 mutex_enter(&ism_mlist_lock); 7238 kpreempt_disable(); 7239 sfmmu_ismtlbcache_demap(addr, sfmmup, hmeblkp, 7240 pp->p_pagenum, CACHE_NO_FLUSH); 7241 kpreempt_enable(); 7242 mutex_exit(&ism_mlist_lock); 7243 sfmmu_hat_unlock_all(); 7244 cpuset = cpu_ready_set; 7245 } else { 7246 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 7247 cpuset = sfmmup->sfmmu_cpusran; 7248 } 7249 } 7250 7251 /* 7252 * Hme_sub has to run after ttesync() and a_rss update. 7253 * See hblk_unload(). 7254 */ 7255 HME_SUB(sfhme, pp); 7256 membar_stst(); 7257 7258 /* 7259 * We can not make ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS) 7260 * since pteload may have done a HME_ADD() right after 7261 * we did the HME_SUB() above. Hmecnt is now maintained 7262 * by cas only. no lock guranteed its value. The only 7263 * gurantee we have is the hmecnt should not be less than 7264 * what it should be so the hblk will not be taken away. 7265 * It's also important that we decremented the hmecnt after 7266 * we are done with hmeblkp so that this hmeblk won't be 7267 * stolen. 7268 */ 7269 ASSERT(hmeblkp->hblk_hmecnt > 0); 7270 ASSERT(hmeblkp->hblk_vcnt > 0); 7271 atomic_add_16(&hmeblkp->hblk_vcnt, -1); 7272 atomic_add_16(&hmeblkp->hblk_hmecnt, -1); 7273 /* 7274 * This is bug 4063182. 7275 * XXX: fixme 7276 * ASSERT(hmeblkp->hblk_hmecnt || hmeblkp->hblk_vcnt || 7277 * !hmeblkp->hblk_lckcnt); 7278 */ 7279 } else { 7280 panic("invalid tte? pp %p &tte %p", 7281 (void *)pp, (void *)&tte); 7282 } 7283 7284 return (cpuset); 7285 } 7286 7287 /* 7288 * While relocating a kernel page, this function will move the mappings 7289 * from tpp to dpp and modify any associated data with these mappings. 7290 * It also unsuspends the suspended kernel mapping. 7291 */ 7292 static void 7293 hat_pagereload(struct page *tpp, struct page *dpp) 7294 { 7295 struct sf_hment *sfhme; 7296 tte_t tte, ttemod; 7297 int index, cons; 7298 7299 ASSERT(getpil() == PIL_MAX); 7300 ASSERT(sfmmu_mlist_held(tpp)); 7301 ASSERT(sfmmu_mlist_held(dpp)); 7302 7303 index = PP_MAPINDEX(tpp); 7304 cons = TTE8K; 7305 7306 /* Update real mappings to the page */ 7307 retry: 7308 for (sfhme = tpp->p_mapping; sfhme != NULL; sfhme = sfhme->hme_next) { 7309 if (IS_PAHME(sfhme)) 7310 continue; 7311 sfmmu_copytte(&sfhme->hme_tte, &tte); 7312 ttemod = tte; 7313 7314 /* 7315 * replace old pfn with new pfn in TTE 7316 */ 7317 PFN_TO_TTE(ttemod, dpp->p_pagenum); 7318 7319 /* 7320 * clear suspend bit 7321 */ 7322 ASSERT(TTE_IS_SUSPEND(&ttemod)); 7323 TTE_CLR_SUSPEND(&ttemod); 7324 7325 if (sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte) < 0) 7326 panic("hat_pagereload(): sfmmu_modifytte_try() failed"); 7327 7328 /* 7329 * set hme_page point to new page 7330 */ 7331 sfhme->hme_page = dpp; 7332 } 7333 7334 /* 7335 * move p_mapping list from old page to new page 7336 */ 7337 dpp->p_mapping = tpp->p_mapping; 7338 tpp->p_mapping = NULL; 7339 dpp->p_share = tpp->p_share; 7340 tpp->p_share = 0; 7341 7342 while (index != 0) { 7343 index = index >> 1; 7344 if (index != 0) 7345 cons++; 7346 if (index & 0x1) { 7347 tpp = PP_GROUPLEADER(tpp, cons); 7348 dpp = PP_GROUPLEADER(dpp, cons); 7349 goto retry; 7350 } 7351 } 7352 7353 curthread->t_flag &= ~T_DONTDTRACE; 7354 mutex_exit(&kpr_suspendlock); 7355 } 7356 7357 uint_t 7358 hat_pagesync(struct page *pp, uint_t clearflag) 7359 { 7360 struct sf_hment *sfhme, *tmphme = NULL; 7361 struct hme_blk *hmeblkp; 7362 kmutex_t *pml; 7363 cpuset_t cpuset, tset; 7364 int index, cons; 7365 extern ulong_t po_share; 7366 page_t *save_pp = pp; 7367 int stop_on_sh = 0; 7368 uint_t shcnt; 7369 7370 CPUSET_ZERO(cpuset); 7371 7372 if (PP_ISRO(pp) && (clearflag & HAT_SYNC_STOPON_MOD)) { 7373 return (PP_GENERIC_ATTR(pp)); 7374 } 7375 7376 if ((clearflag == (HAT_SYNC_STOPON_REF | HAT_SYNC_DONTZERO)) && 7377 PP_ISREF(pp)) { 7378 return (PP_GENERIC_ATTR(pp)); 7379 } 7380 7381 if ((clearflag == (HAT_SYNC_STOPON_MOD | HAT_SYNC_DONTZERO)) && 7382 PP_ISMOD(pp)) { 7383 return (PP_GENERIC_ATTR(pp)); 7384 } 7385 7386 if ((clearflag & HAT_SYNC_STOPON_SHARED) != 0 && 7387 (pp->p_share > po_share) && 7388 !(clearflag & HAT_SYNC_ZERORM)) { 7389 hat_page_setattr(pp, P_REF); 7390 return (PP_GENERIC_ATTR(pp)); 7391 } 7392 7393 if ((clearflag & HAT_SYNC_STOPON_SHARED) && 7394 !(clearflag & HAT_SYNC_ZERORM)) { 7395 stop_on_sh = 1; 7396 shcnt = 0; 7397 } 7398 clearflag &= ~HAT_SYNC_STOPON_SHARED; 7399 pml = sfmmu_mlist_enter(pp); 7400 index = PP_MAPINDEX(pp); 7401 cons = TTE8K; 7402 retry: 7403 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 7404 /* 7405 * We need to save the next hment on the list since 7406 * it is possible for pagesync to remove an invalid hment 7407 * from the list. 7408 */ 7409 tmphme = sfhme->hme_next; 7410 if (IS_PAHME(sfhme)) 7411 continue; 7412 /* 7413 * If we are looking for large mappings and this hme doesn't 7414 * reach the range we are seeking, just ignore it. 7415 */ 7416 hmeblkp = sfmmu_hmetohblk(sfhme); 7417 if (hmeblkp->hblk_xhat_bit) 7418 continue; 7419 7420 if (hme_size(sfhme) < cons) 7421 continue; 7422 7423 if (stop_on_sh) { 7424 if (hmeblkp->hblk_shared) { 7425 sf_srd_t *srdp = hblktosrd(hmeblkp); 7426 uint_t rid = hmeblkp->hblk_tag.htag_rid; 7427 sf_region_t *rgnp; 7428 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 7429 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 7430 ASSERT(srdp != NULL); 7431 rgnp = srdp->srd_hmergnp[rid]; 7432 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, 7433 rgnp, rid); 7434 shcnt += rgnp->rgn_refcnt; 7435 } else { 7436 shcnt++; 7437 } 7438 if (shcnt > po_share) { 7439 /* 7440 * tell the pager to spare the page this time 7441 * around. 7442 */ 7443 hat_page_setattr(save_pp, P_REF); 7444 index = 0; 7445 break; 7446 } 7447 } 7448 tset = sfmmu_pagesync(pp, sfhme, 7449 clearflag & ~HAT_SYNC_STOPON_RM); 7450 CPUSET_OR(cpuset, tset); 7451 7452 /* 7453 * If clearflag is HAT_SYNC_DONTZERO, break out as soon 7454 * as the "ref" or "mod" is set or share cnt exceeds po_share. 7455 */ 7456 if ((clearflag & ~HAT_SYNC_STOPON_RM) == HAT_SYNC_DONTZERO && 7457 (((clearflag & HAT_SYNC_STOPON_MOD) && PP_ISMOD(save_pp)) || 7458 ((clearflag & HAT_SYNC_STOPON_REF) && PP_ISREF(save_pp)))) { 7459 index = 0; 7460 break; 7461 } 7462 } 7463 7464 while (index) { 7465 index = index >> 1; 7466 cons++; 7467 if (index & 0x1) { 7468 /* Go to leading page */ 7469 pp = PP_GROUPLEADER(pp, cons); 7470 goto retry; 7471 } 7472 } 7473 7474 xt_sync(cpuset); 7475 sfmmu_mlist_exit(pml); 7476 return (PP_GENERIC_ATTR(save_pp)); 7477 } 7478 7479 /* 7480 * Get all the hardware dependent attributes for a page struct 7481 */ 7482 static cpuset_t 7483 sfmmu_pagesync(struct page *pp, struct sf_hment *sfhme, 7484 uint_t clearflag) 7485 { 7486 caddr_t addr; 7487 tte_t tte, ttemod; 7488 struct hme_blk *hmeblkp; 7489 int ret; 7490 sfmmu_t *sfmmup; 7491 cpuset_t cpuset; 7492 7493 ASSERT(pp != NULL); 7494 ASSERT(sfmmu_mlist_held(pp)); 7495 ASSERT((clearflag == HAT_SYNC_DONTZERO) || 7496 (clearflag == HAT_SYNC_ZERORM)); 7497 7498 SFMMU_STAT(sf_pagesync); 7499 7500 CPUSET_ZERO(cpuset); 7501 7502 sfmmu_pagesync_retry: 7503 7504 sfmmu_copytte(&sfhme->hme_tte, &tte); 7505 if (TTE_IS_VALID(&tte)) { 7506 hmeblkp = sfmmu_hmetohblk(sfhme); 7507 sfmmup = hblktosfmmu(hmeblkp); 7508 addr = tte_to_vaddr(hmeblkp, tte); 7509 if (clearflag == HAT_SYNC_ZERORM) { 7510 ttemod = tte; 7511 TTE_CLR_RM(&ttemod); 7512 ret = sfmmu_modifytte_try(&tte, &ttemod, 7513 &sfhme->hme_tte); 7514 if (ret < 0) { 7515 /* 7516 * cas failed and the new value is not what 7517 * we want. 7518 */ 7519 goto sfmmu_pagesync_retry; 7520 } 7521 7522 if (ret > 0) { 7523 /* we win the cas */ 7524 if (hmeblkp->hblk_shared) { 7525 sf_srd_t *srdp = (sf_srd_t *)sfmmup; 7526 uint_t rid = 7527 hmeblkp->hblk_tag.htag_rid; 7528 sf_region_t *rgnp; 7529 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 7530 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 7531 ASSERT(srdp != NULL); 7532 rgnp = srdp->srd_hmergnp[rid]; 7533 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, 7534 srdp, rgnp, rid); 7535 cpuset = sfmmu_rgntlb_demap(addr, 7536 rgnp, hmeblkp, 1); 7537 } else { 7538 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 7539 0, 0); 7540 cpuset = sfmmup->sfmmu_cpusran; 7541 } 7542 } 7543 } 7544 sfmmu_ttesync(hmeblkp->hblk_shared ? NULL : sfmmup, addr, 7545 &tte, pp); 7546 } 7547 return (cpuset); 7548 } 7549 7550 /* 7551 * Remove write permission from a mappings to a page, so that 7552 * we can detect the next modification of it. This requires modifying 7553 * the TTE then invalidating (demap) any TLB entry using that TTE. 7554 * This code is similar to sfmmu_pagesync(). 7555 */ 7556 static cpuset_t 7557 sfmmu_pageclrwrt(struct page *pp, struct sf_hment *sfhme) 7558 { 7559 caddr_t addr; 7560 tte_t tte; 7561 tte_t ttemod; 7562 struct hme_blk *hmeblkp; 7563 int ret; 7564 sfmmu_t *sfmmup; 7565 cpuset_t cpuset; 7566 7567 ASSERT(pp != NULL); 7568 ASSERT(sfmmu_mlist_held(pp)); 7569 7570 CPUSET_ZERO(cpuset); 7571 SFMMU_STAT(sf_clrwrt); 7572 7573 retry: 7574 7575 sfmmu_copytte(&sfhme->hme_tte, &tte); 7576 if (TTE_IS_VALID(&tte) && TTE_IS_WRITABLE(&tte)) { 7577 hmeblkp = sfmmu_hmetohblk(sfhme); 7578 7579 /* 7580 * xhat mappings should never be to a VMODSORT page. 7581 */ 7582 ASSERT(hmeblkp->hblk_xhat_bit == 0); 7583 7584 sfmmup = hblktosfmmu(hmeblkp); 7585 addr = tte_to_vaddr(hmeblkp, tte); 7586 7587 ttemod = tte; 7588 TTE_CLR_WRT(&ttemod); 7589 TTE_CLR_MOD(&ttemod); 7590 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte); 7591 7592 /* 7593 * if cas failed and the new value is not what 7594 * we want retry 7595 */ 7596 if (ret < 0) 7597 goto retry; 7598 7599 /* we win the cas */ 7600 if (ret > 0) { 7601 if (hmeblkp->hblk_shared) { 7602 sf_srd_t *srdp = (sf_srd_t *)sfmmup; 7603 uint_t rid = hmeblkp->hblk_tag.htag_rid; 7604 sf_region_t *rgnp; 7605 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 7606 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 7607 ASSERT(srdp != NULL); 7608 rgnp = srdp->srd_hmergnp[rid]; 7609 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, 7610 srdp, rgnp, rid); 7611 cpuset = sfmmu_rgntlb_demap(addr, 7612 rgnp, hmeblkp, 1); 7613 } else { 7614 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 7615 cpuset = sfmmup->sfmmu_cpusran; 7616 } 7617 } 7618 } 7619 7620 return (cpuset); 7621 } 7622 7623 /* 7624 * Walk all mappings of a page, removing write permission and clearing the 7625 * ref/mod bits. This code is similar to hat_pagesync() 7626 */ 7627 static void 7628 hat_page_clrwrt(page_t *pp) 7629 { 7630 struct sf_hment *sfhme; 7631 struct sf_hment *tmphme = NULL; 7632 kmutex_t *pml; 7633 cpuset_t cpuset; 7634 cpuset_t tset; 7635 int index; 7636 int cons; 7637 7638 CPUSET_ZERO(cpuset); 7639 7640 pml = sfmmu_mlist_enter(pp); 7641 index = PP_MAPINDEX(pp); 7642 cons = TTE8K; 7643 retry: 7644 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 7645 tmphme = sfhme->hme_next; 7646 7647 /* 7648 * If we are looking for large mappings and this hme doesn't 7649 * reach the range we are seeking, just ignore its. 7650 */ 7651 7652 if (hme_size(sfhme) < cons) 7653 continue; 7654 7655 tset = sfmmu_pageclrwrt(pp, sfhme); 7656 CPUSET_OR(cpuset, tset); 7657 } 7658 7659 while (index) { 7660 index = index >> 1; 7661 cons++; 7662 if (index & 0x1) { 7663 /* Go to leading page */ 7664 pp = PP_GROUPLEADER(pp, cons); 7665 goto retry; 7666 } 7667 } 7668 7669 xt_sync(cpuset); 7670 sfmmu_mlist_exit(pml); 7671 } 7672 7673 /* 7674 * Set the given REF/MOD/RO bits for the given page. 7675 * For a vnode with a sorted v_pages list, we need to change 7676 * the attributes and the v_pages list together under page_vnode_mutex. 7677 */ 7678 void 7679 hat_page_setattr(page_t *pp, uint_t flag) 7680 { 7681 vnode_t *vp = pp->p_vnode; 7682 page_t **listp; 7683 kmutex_t *pmtx; 7684 kmutex_t *vphm = NULL; 7685 int noshuffle; 7686 7687 noshuffle = flag & P_NSH; 7688 flag &= ~P_NSH; 7689 7690 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO))); 7691 7692 /* 7693 * nothing to do if attribute already set 7694 */ 7695 if ((pp->p_nrm & flag) == flag) 7696 return; 7697 7698 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp) && 7699 !noshuffle) { 7700 vphm = page_vnode_mutex(vp); 7701 mutex_enter(vphm); 7702 } 7703 7704 pmtx = sfmmu_page_enter(pp); 7705 pp->p_nrm |= flag; 7706 sfmmu_page_exit(pmtx); 7707 7708 if (vphm != NULL) { 7709 /* 7710 * Some File Systems examine v_pages for NULL w/o 7711 * grabbing the vphm mutex. Must not let it become NULL when 7712 * pp is the only page on the list. 7713 */ 7714 if (pp->p_vpnext != pp) { 7715 page_vpsub(&vp->v_pages, pp); 7716 if (vp->v_pages != NULL) 7717 listp = &vp->v_pages->p_vpprev->p_vpnext; 7718 else 7719 listp = &vp->v_pages; 7720 page_vpadd(listp, pp); 7721 } 7722 mutex_exit(vphm); 7723 } 7724 } 7725 7726 void 7727 hat_page_clrattr(page_t *pp, uint_t flag) 7728 { 7729 vnode_t *vp = pp->p_vnode; 7730 kmutex_t *pmtx; 7731 7732 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO))); 7733 7734 pmtx = sfmmu_page_enter(pp); 7735 7736 /* 7737 * Caller is expected to hold page's io lock for VMODSORT to work 7738 * correctly with pvn_vplist_dirty() and pvn_getdirty() when mod 7739 * bit is cleared. 7740 * We don't have assert to avoid tripping some existing third party 7741 * code. The dirty page is moved back to top of the v_page list 7742 * after IO is done in pvn_write_done(). 7743 */ 7744 pp->p_nrm &= ~flag; 7745 sfmmu_page_exit(pmtx); 7746 7747 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp)) { 7748 7749 /* 7750 * VMODSORT works by removing write permissions and getting 7751 * a fault when a page is made dirty. At this point 7752 * we need to remove write permission from all mappings 7753 * to this page. 7754 */ 7755 hat_page_clrwrt(pp); 7756 } 7757 } 7758 7759 uint_t 7760 hat_page_getattr(page_t *pp, uint_t flag) 7761 { 7762 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO))); 7763 return ((uint_t)(pp->p_nrm & flag)); 7764 } 7765 7766 /* 7767 * DEBUG kernels: verify that a kernel va<->pa translation 7768 * is safe by checking the underlying page_t is in a page 7769 * relocation-safe state. 7770 */ 7771 #ifdef DEBUG 7772 void 7773 sfmmu_check_kpfn(pfn_t pfn) 7774 { 7775 page_t *pp; 7776 int index, cons; 7777 7778 if (hat_check_vtop == 0) 7779 return; 7780 7781 if (hat_kpr_enabled == 0 || kvseg.s_base == NULL || panicstr) 7782 return; 7783 7784 pp = page_numtopp_nolock(pfn); 7785 if (!pp) 7786 return; 7787 7788 if (PAGE_LOCKED(pp) || PP_ISNORELOC(pp)) 7789 return; 7790 7791 /* 7792 * Handed a large kernel page, we dig up the root page since we 7793 * know the root page might have the lock also. 7794 */ 7795 if (pp->p_szc != 0) { 7796 index = PP_MAPINDEX(pp); 7797 cons = TTE8K; 7798 again: 7799 while (index != 0) { 7800 index >>= 1; 7801 if (index != 0) 7802 cons++; 7803 if (index & 0x1) { 7804 pp = PP_GROUPLEADER(pp, cons); 7805 goto again; 7806 } 7807 } 7808 } 7809 7810 if (PAGE_LOCKED(pp) || PP_ISNORELOC(pp)) 7811 return; 7812 7813 /* 7814 * Pages need to be locked or allocated "permanent" (either from 7815 * static_arena arena or explicitly setting PG_NORELOC when calling 7816 * page_create_va()) for VA->PA translations to be valid. 7817 */ 7818 if (!PP_ISNORELOC(pp)) 7819 panic("Illegal VA->PA translation, pp 0x%p not permanent", pp); 7820 else 7821 panic("Illegal VA->PA translation, pp 0x%p not locked", pp); 7822 } 7823 #endif /* DEBUG */ 7824 7825 /* 7826 * Returns a page frame number for a given virtual address. 7827 * Returns PFN_INVALID to indicate an invalid mapping 7828 */ 7829 pfn_t 7830 hat_getpfnum(struct hat *hat, caddr_t addr) 7831 { 7832 pfn_t pfn; 7833 tte_t tte; 7834 7835 /* 7836 * We would like to 7837 * ASSERT(AS_LOCK_HELD(as, &as->a_lock)); 7838 * but we can't because the iommu driver will call this 7839 * routine at interrupt time and it can't grab the as lock 7840 * or it will deadlock: A thread could have the as lock 7841 * and be waiting for io. The io can't complete 7842 * because the interrupt thread is blocked trying to grab 7843 * the as lock. 7844 */ 7845 7846 ASSERT(hat->sfmmu_xhat_provider == NULL); 7847 7848 if (hat == ksfmmup) { 7849 if (IS_KMEM_VA_LARGEPAGE(addr)) { 7850 ASSERT(segkmem_lpszc > 0); 7851 pfn = sfmmu_kvaszc2pfn(addr, segkmem_lpszc); 7852 if (pfn != PFN_INVALID) { 7853 sfmmu_check_kpfn(pfn); 7854 return (pfn); 7855 } 7856 } else if (segkpm && IS_KPM_ADDR(addr)) { 7857 return (sfmmu_kpm_vatopfn(addr)); 7858 } 7859 while ((pfn = sfmmu_vatopfn(addr, ksfmmup, &tte)) 7860 == PFN_SUSPENDED) { 7861 sfmmu_vatopfn_suspended(addr, ksfmmup, &tte); 7862 } 7863 sfmmu_check_kpfn(pfn); 7864 return (pfn); 7865 } else { 7866 return (sfmmu_uvatopfn(addr, hat, NULL)); 7867 } 7868 } 7869 7870 /* 7871 * hat_getkpfnum() is an obsolete DDI routine, and its use is discouraged. 7872 * Use hat_getpfnum(kas.a_hat, ...) instead. 7873 * 7874 * We'd like to return PFN_INVALID if the mappings have underlying page_t's 7875 * but can't right now due to the fact that some software has grown to use 7876 * this interface incorrectly. So for now when the interface is misused, 7877 * return a warning to the user that in the future it won't work in the 7878 * way they're abusing it, and carry on (after disabling page relocation). 7879 */ 7880 pfn_t 7881 hat_getkpfnum(caddr_t addr) 7882 { 7883 pfn_t pfn; 7884 tte_t tte; 7885 int badcaller = 0; 7886 extern int segkmem_reloc; 7887 7888 if (segkpm && IS_KPM_ADDR(addr)) { 7889 badcaller = 1; 7890 pfn = sfmmu_kpm_vatopfn(addr); 7891 } else { 7892 while ((pfn = sfmmu_vatopfn(addr, ksfmmup, &tte)) 7893 == PFN_SUSPENDED) { 7894 sfmmu_vatopfn_suspended(addr, ksfmmup, &tte); 7895 } 7896 badcaller = pf_is_memory(pfn); 7897 } 7898 7899 if (badcaller) { 7900 /* 7901 * We can't return PFN_INVALID or the caller may panic 7902 * or corrupt the system. The only alternative is to 7903 * disable page relocation at this point for all kernel 7904 * memory. This will impact any callers of page_relocate() 7905 * such as FMA or DR. 7906 * 7907 * RFE: Add junk here to spit out an ereport so the sysadmin 7908 * can be advised that he should upgrade his device driver 7909 * so that this doesn't happen. 7910 */ 7911 hat_getkpfnum_badcall(caller()); 7912 if (hat_kpr_enabled && segkmem_reloc) { 7913 hat_kpr_enabled = 0; 7914 segkmem_reloc = 0; 7915 cmn_err(CE_WARN, "Kernel Page Relocation is DISABLED"); 7916 } 7917 } 7918 return (pfn); 7919 } 7920 7921 /* 7922 * This routine will return both pfn and tte for the vaddr. 7923 */ 7924 static pfn_t 7925 sfmmu_uvatopfn(caddr_t vaddr, struct hat *sfmmup, tte_t *ttep) 7926 { 7927 struct hmehash_bucket *hmebp; 7928 hmeblk_tag hblktag; 7929 int hmeshift, hashno = 1; 7930 struct hme_blk *hmeblkp = NULL; 7931 tte_t tte; 7932 7933 struct sf_hment *sfhmep; 7934 pfn_t pfn; 7935 7936 /* support for ISM */ 7937 ism_map_t *ism_map; 7938 ism_blk_t *ism_blkp; 7939 int i; 7940 sfmmu_t *ism_hatid = NULL; 7941 sfmmu_t *locked_hatid = NULL; 7942 sfmmu_t *sv_sfmmup = sfmmup; 7943 caddr_t sv_vaddr = vaddr; 7944 sf_srd_t *srdp; 7945 7946 if (ttep == NULL) { 7947 ttep = &tte; 7948 } else { 7949 ttep->ll = 0; 7950 } 7951 7952 ASSERT(sfmmup != ksfmmup); 7953 SFMMU_STAT(sf_user_vtop); 7954 /* 7955 * Set ism_hatid if vaddr falls in a ISM segment. 7956 */ 7957 ism_blkp = sfmmup->sfmmu_iblk; 7958 if (ism_blkp != NULL) { 7959 sfmmu_ismhat_enter(sfmmup, 0); 7960 locked_hatid = sfmmup; 7961 } 7962 while (ism_blkp != NULL && ism_hatid == NULL) { 7963 ism_map = ism_blkp->iblk_maps; 7964 for (i = 0; ism_map[i].imap_ismhat && i < ISM_MAP_SLOTS; i++) { 7965 if (vaddr >= ism_start(ism_map[i]) && 7966 vaddr < ism_end(ism_map[i])) { 7967 sfmmup = ism_hatid = ism_map[i].imap_ismhat; 7968 vaddr = (caddr_t)(vaddr - 7969 ism_start(ism_map[i])); 7970 break; 7971 } 7972 } 7973 ism_blkp = ism_blkp->iblk_next; 7974 } 7975 if (locked_hatid) { 7976 sfmmu_ismhat_exit(locked_hatid, 0); 7977 } 7978 7979 hblktag.htag_id = sfmmup; 7980 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 7981 do { 7982 hmeshift = HME_HASH_SHIFT(hashno); 7983 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift); 7984 hblktag.htag_rehash = hashno; 7985 hmebp = HME_HASH_FUNCTION(sfmmup, vaddr, hmeshift); 7986 7987 SFMMU_HASH_LOCK(hmebp); 7988 7989 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 7990 if (hmeblkp != NULL) { 7991 ASSERT(!hmeblkp->hblk_shared); 7992 HBLKTOHME(sfhmep, hmeblkp, vaddr); 7993 sfmmu_copytte(&sfhmep->hme_tte, ttep); 7994 SFMMU_HASH_UNLOCK(hmebp); 7995 if (TTE_IS_VALID(ttep)) { 7996 pfn = TTE_TO_PFN(vaddr, ttep); 7997 return (pfn); 7998 } 7999 break; 8000 } 8001 SFMMU_HASH_UNLOCK(hmebp); 8002 hashno++; 8003 } while (HME_REHASH(sfmmup) && (hashno <= mmu_hashcnt)); 8004 8005 if (SF_HMERGNMAP_ISNULL(sv_sfmmup)) { 8006 return (PFN_INVALID); 8007 } 8008 srdp = sv_sfmmup->sfmmu_srdp; 8009 ASSERT(srdp != NULL); 8010 ASSERT(srdp->srd_refcnt != 0); 8011 hblktag.htag_id = srdp; 8012 hashno = 1; 8013 do { 8014 hmeshift = HME_HASH_SHIFT(hashno); 8015 hblktag.htag_bspage = HME_HASH_BSPAGE(sv_vaddr, hmeshift); 8016 hblktag.htag_rehash = hashno; 8017 hmebp = HME_HASH_FUNCTION(srdp, sv_vaddr, hmeshift); 8018 8019 SFMMU_HASH_LOCK(hmebp); 8020 for (hmeblkp = hmebp->hmeblkp; hmeblkp != NULL; 8021 hmeblkp = hmeblkp->hblk_next) { 8022 uint_t rid; 8023 sf_region_t *rgnp; 8024 caddr_t rsaddr; 8025 caddr_t readdr; 8026 8027 if (!HTAGS_EQ_SHME(hmeblkp->hblk_tag, hblktag, 8028 sv_sfmmup->sfmmu_hmeregion_map)) { 8029 continue; 8030 } 8031 ASSERT(hmeblkp->hblk_shared); 8032 rid = hmeblkp->hblk_tag.htag_rid; 8033 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 8034 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 8035 rgnp = srdp->srd_hmergnp[rid]; 8036 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid); 8037 HBLKTOHME(sfhmep, hmeblkp, sv_vaddr); 8038 sfmmu_copytte(&sfhmep->hme_tte, ttep); 8039 rsaddr = rgnp->rgn_saddr; 8040 readdr = rsaddr + rgnp->rgn_size; 8041 #ifdef DEBUG 8042 if (TTE_IS_VALID(ttep) || 8043 get_hblk_ttesz(hmeblkp) > TTE8K) { 8044 caddr_t eva = tte_to_evaddr(hmeblkp, ttep); 8045 ASSERT(eva > sv_vaddr); 8046 ASSERT(sv_vaddr >= rsaddr); 8047 ASSERT(sv_vaddr < readdr); 8048 ASSERT(eva <= readdr); 8049 } 8050 #endif /* DEBUG */ 8051 /* 8052 * Continue the search if we 8053 * found an invalid 8K tte outside of the area 8054 * covered by this hmeblk's region. 8055 */ 8056 if (TTE_IS_VALID(ttep)) { 8057 SFMMU_HASH_UNLOCK(hmebp); 8058 pfn = TTE_TO_PFN(sv_vaddr, ttep); 8059 return (pfn); 8060 } else if (get_hblk_ttesz(hmeblkp) > TTE8K || 8061 (sv_vaddr >= rsaddr && sv_vaddr < readdr)) { 8062 SFMMU_HASH_UNLOCK(hmebp); 8063 pfn = PFN_INVALID; 8064 return (pfn); 8065 } 8066 } 8067 SFMMU_HASH_UNLOCK(hmebp); 8068 hashno++; 8069 } while (hashno <= mmu_hashcnt); 8070 return (PFN_INVALID); 8071 } 8072 8073 8074 /* 8075 * For compatability with AT&T and later optimizations 8076 */ 8077 /* ARGSUSED */ 8078 void 8079 hat_map(struct hat *hat, caddr_t addr, size_t len, uint_t flags) 8080 { 8081 ASSERT(hat != NULL); 8082 ASSERT(hat->sfmmu_xhat_provider == NULL); 8083 } 8084 8085 /* 8086 * Return the number of mappings to a particular page. This number is an 8087 * approximation of the number of people sharing the page. 8088 * 8089 * shared hmeblks or ism hmeblks are counted as 1 mapping here. 8090 * hat_page_checkshare() can be used to compare threshold to share 8091 * count that reflects the number of region sharers albeit at higher cost. 8092 */ 8093 ulong_t 8094 hat_page_getshare(page_t *pp) 8095 { 8096 page_t *spp = pp; /* start page */ 8097 kmutex_t *pml; 8098 ulong_t cnt; 8099 int index, sz = TTE64K; 8100 8101 /* 8102 * We need to grab the mlist lock to make sure any outstanding 8103 * load/unloads complete. Otherwise we could return zero 8104 * even though the unload(s) hasn't finished yet. 8105 */ 8106 pml = sfmmu_mlist_enter(spp); 8107 cnt = spp->p_share; 8108 8109 #ifdef VAC 8110 if (kpm_enable) 8111 cnt += spp->p_kpmref; 8112 #endif 8113 8114 /* 8115 * If we have any large mappings, we count the number of 8116 * mappings that this large page is part of. 8117 */ 8118 index = PP_MAPINDEX(spp); 8119 index >>= 1; 8120 while (index) { 8121 pp = PP_GROUPLEADER(spp, sz); 8122 if ((index & 0x1) && pp != spp) { 8123 cnt += pp->p_share; 8124 spp = pp; 8125 } 8126 index >>= 1; 8127 sz++; 8128 } 8129 sfmmu_mlist_exit(pml); 8130 return (cnt); 8131 } 8132 8133 /* 8134 * Return 1 if the number of mappings exceeds sh_thresh. Return 0 8135 * otherwise. Count shared hmeblks by region's refcnt. 8136 */ 8137 int 8138 hat_page_checkshare(page_t *pp, ulong_t sh_thresh) 8139 { 8140 kmutex_t *pml; 8141 ulong_t cnt = 0; 8142 int index, sz = TTE8K; 8143 struct sf_hment *sfhme, *tmphme = NULL; 8144 struct hme_blk *hmeblkp; 8145 8146 pml = sfmmu_mlist_enter(pp); 8147 8148 if (kpm_enable) 8149 cnt = pp->p_kpmref; 8150 8151 if (pp->p_share + cnt > sh_thresh) { 8152 sfmmu_mlist_exit(pml); 8153 return (1); 8154 } 8155 8156 index = PP_MAPINDEX(pp); 8157 8158 again: 8159 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 8160 tmphme = sfhme->hme_next; 8161 if (IS_PAHME(sfhme)) { 8162 continue; 8163 } 8164 8165 hmeblkp = sfmmu_hmetohblk(sfhme); 8166 if (hmeblkp->hblk_xhat_bit) { 8167 cnt++; 8168 if (cnt > sh_thresh) { 8169 sfmmu_mlist_exit(pml); 8170 return (1); 8171 } 8172 continue; 8173 } 8174 if (hme_size(sfhme) != sz) { 8175 continue; 8176 } 8177 8178 if (hmeblkp->hblk_shared) { 8179 sf_srd_t *srdp = hblktosrd(hmeblkp); 8180 uint_t rid = hmeblkp->hblk_tag.htag_rid; 8181 sf_region_t *rgnp; 8182 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 8183 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 8184 ASSERT(srdp != NULL); 8185 rgnp = srdp->srd_hmergnp[rid]; 8186 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, 8187 rgnp, rid); 8188 cnt += rgnp->rgn_refcnt; 8189 } else { 8190 cnt++; 8191 } 8192 if (cnt > sh_thresh) { 8193 sfmmu_mlist_exit(pml); 8194 return (1); 8195 } 8196 } 8197 8198 index >>= 1; 8199 sz++; 8200 while (index) { 8201 pp = PP_GROUPLEADER(pp, sz); 8202 ASSERT(sfmmu_mlist_held(pp)); 8203 if (index & 0x1) { 8204 goto again; 8205 } 8206 index >>= 1; 8207 sz++; 8208 } 8209 sfmmu_mlist_exit(pml); 8210 return (0); 8211 } 8212 8213 /* 8214 * Unload all large mappings to the pp and reset the p_szc field of every 8215 * constituent page according to the remaining mappings. 8216 * 8217 * pp must be locked SE_EXCL. Even though no other constituent pages are 8218 * locked it's legal to unload the large mappings to the pp because all 8219 * constituent pages of large locked mappings have to be locked SE_SHARED. 8220 * This means if we have SE_EXCL lock on one of constituent pages none of the 8221 * large mappings to pp are locked. 8222 * 8223 * Decrease p_szc field starting from the last constituent page and ending 8224 * with the root page. This method is used because other threads rely on the 8225 * root's p_szc to find the lock to syncronize on. After a root page_t's p_szc 8226 * is demoted then other threads will succeed in sfmmu_mlspl_enter(). This 8227 * ensures that p_szc changes of the constituent pages appears atomic for all 8228 * threads that use sfmmu_mlspl_enter() to examine p_szc field. 8229 * 8230 * This mechanism is only used for file system pages where it's not always 8231 * possible to get SE_EXCL locks on all constituent pages to demote the size 8232 * code (as is done for anonymous or kernel large pages). 8233 * 8234 * See more comments in front of sfmmu_mlspl_enter(). 8235 */ 8236 void 8237 hat_page_demote(page_t *pp) 8238 { 8239 int index; 8240 int sz; 8241 cpuset_t cpuset; 8242 int sync = 0; 8243 page_t *rootpp; 8244 struct sf_hment *sfhme; 8245 struct sf_hment *tmphme = NULL; 8246 struct hme_blk *hmeblkp; 8247 uint_t pszc; 8248 page_t *lastpp; 8249 cpuset_t tset; 8250 pgcnt_t npgs; 8251 kmutex_t *pml; 8252 kmutex_t *pmtx = NULL; 8253 8254 ASSERT(PAGE_EXCL(pp)); 8255 ASSERT(!PP_ISFREE(pp)); 8256 ASSERT(!PP_ISKAS(pp)); 8257 ASSERT(page_szc_lock_assert(pp)); 8258 pml = sfmmu_mlist_enter(pp); 8259 8260 pszc = pp->p_szc; 8261 if (pszc == 0) { 8262 goto out; 8263 } 8264 8265 index = PP_MAPINDEX(pp) >> 1; 8266 8267 if (index) { 8268 CPUSET_ZERO(cpuset); 8269 sz = TTE64K; 8270 sync = 1; 8271 } 8272 8273 while (index) { 8274 if (!(index & 0x1)) { 8275 index >>= 1; 8276 sz++; 8277 continue; 8278 } 8279 ASSERT(sz <= pszc); 8280 rootpp = PP_GROUPLEADER(pp, sz); 8281 for (sfhme = rootpp->p_mapping; sfhme; sfhme = tmphme) { 8282 tmphme = sfhme->hme_next; 8283 ASSERT(!IS_PAHME(sfhme)); 8284 hmeblkp = sfmmu_hmetohblk(sfhme); 8285 if (hme_size(sfhme) != sz) { 8286 continue; 8287 } 8288 if (hmeblkp->hblk_xhat_bit) { 8289 cmn_err(CE_PANIC, 8290 "hat_page_demote: xhat hmeblk"); 8291 } 8292 tset = sfmmu_pageunload(rootpp, sfhme, sz); 8293 CPUSET_OR(cpuset, tset); 8294 } 8295 if (index >>= 1) { 8296 sz++; 8297 } 8298 } 8299 8300 ASSERT(!PP_ISMAPPED_LARGE(pp)); 8301 8302 if (sync) { 8303 xt_sync(cpuset); 8304 #ifdef VAC 8305 if (PP_ISTNC(pp)) { 8306 conv_tnc(rootpp, sz); 8307 } 8308 #endif /* VAC */ 8309 } 8310 8311 pmtx = sfmmu_page_enter(pp); 8312 8313 ASSERT(pp->p_szc == pszc); 8314 rootpp = PP_PAGEROOT(pp); 8315 ASSERT(rootpp->p_szc == pszc); 8316 lastpp = PP_PAGENEXT_N(rootpp, TTEPAGES(pszc) - 1); 8317 8318 while (lastpp != rootpp) { 8319 sz = PP_MAPINDEX(lastpp) ? fnd_mapping_sz(lastpp) : 0; 8320 ASSERT(sz < pszc); 8321 npgs = (sz == 0) ? 1 : TTEPAGES(sz); 8322 ASSERT(P2PHASE(lastpp->p_pagenum, npgs) == npgs - 1); 8323 while (--npgs > 0) { 8324 lastpp->p_szc = (uchar_t)sz; 8325 lastpp = PP_PAGEPREV(lastpp); 8326 } 8327 if (sz) { 8328 /* 8329 * make sure before current root's pszc 8330 * is updated all updates to constituent pages pszc 8331 * fields are globally visible. 8332 */ 8333 membar_producer(); 8334 } 8335 lastpp->p_szc = sz; 8336 ASSERT(IS_P2ALIGNED(lastpp->p_pagenum, TTEPAGES(sz))); 8337 if (lastpp != rootpp) { 8338 lastpp = PP_PAGEPREV(lastpp); 8339 } 8340 } 8341 if (sz == 0) { 8342 /* the loop above doesn't cover this case */ 8343 rootpp->p_szc = 0; 8344 } 8345 out: 8346 ASSERT(pp->p_szc == 0); 8347 if (pmtx != NULL) { 8348 sfmmu_page_exit(pmtx); 8349 } 8350 sfmmu_mlist_exit(pml); 8351 } 8352 8353 /* 8354 * Refresh the HAT ismttecnt[] element for size szc. 8355 * Caller must have set ISM busy flag to prevent mapping 8356 * lists from changing while we're traversing them. 8357 */ 8358 pgcnt_t 8359 ism_tsb_entries(sfmmu_t *sfmmup, int szc) 8360 { 8361 ism_blk_t *ism_blkp = sfmmup->sfmmu_iblk; 8362 ism_map_t *ism_map; 8363 pgcnt_t npgs = 0; 8364 pgcnt_t npgs_scd = 0; 8365 int j; 8366 sf_scd_t *scdp; 8367 uchar_t rid; 8368 8369 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)); 8370 scdp = sfmmup->sfmmu_scdp; 8371 8372 for (; ism_blkp != NULL; ism_blkp = ism_blkp->iblk_next) { 8373 ism_map = ism_blkp->iblk_maps; 8374 for (j = 0; ism_map[j].imap_ismhat && j < ISM_MAP_SLOTS; j++) { 8375 rid = ism_map[j].imap_rid; 8376 ASSERT(rid == SFMMU_INVALID_ISMRID || 8377 rid < sfmmup->sfmmu_srdp->srd_next_ismrid); 8378 8379 if (scdp != NULL && rid != SFMMU_INVALID_ISMRID && 8380 SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid)) { 8381 /* ISM is in sfmmup's SCD */ 8382 npgs_scd += 8383 ism_map[j].imap_ismhat->sfmmu_ttecnt[szc]; 8384 } else { 8385 /* ISMs is not in SCD */ 8386 npgs += 8387 ism_map[j].imap_ismhat->sfmmu_ttecnt[szc]; 8388 } 8389 } 8390 } 8391 sfmmup->sfmmu_ismttecnt[szc] = npgs; 8392 sfmmup->sfmmu_scdismttecnt[szc] = npgs_scd; 8393 return (npgs); 8394 } 8395 8396 /* 8397 * Yield the memory claim requirement for an address space. 8398 * 8399 * This is currently implemented as the number of bytes that have active 8400 * hardware translations that have page structures. Therefore, it can 8401 * underestimate the traditional resident set size, eg, if the 8402 * physical page is present and the hardware translation is missing; 8403 * and it can overestimate the rss, eg, if there are active 8404 * translations to a frame buffer with page structs. 8405 * Also, it does not take sharing into account. 8406 * 8407 * Note that we don't acquire locks here since this function is most often 8408 * called from the clock thread. 8409 */ 8410 size_t 8411 hat_get_mapped_size(struct hat *hat) 8412 { 8413 size_t assize = 0; 8414 int i; 8415 8416 if (hat == NULL) 8417 return (0); 8418 8419 ASSERT(hat->sfmmu_xhat_provider == NULL); 8420 8421 for (i = 0; i < mmu_page_sizes; i++) 8422 assize += ((pgcnt_t)hat->sfmmu_ttecnt[i] + 8423 (pgcnt_t)hat->sfmmu_scdrttecnt[i]) * TTEBYTES(i); 8424 8425 if (hat->sfmmu_iblk == NULL) 8426 return (assize); 8427 8428 for (i = 0; i < mmu_page_sizes; i++) 8429 assize += ((pgcnt_t)hat->sfmmu_ismttecnt[i] + 8430 (pgcnt_t)hat->sfmmu_scdismttecnt[i]) * TTEBYTES(i); 8431 8432 return (assize); 8433 } 8434 8435 int 8436 hat_stats_enable(struct hat *hat) 8437 { 8438 hatlock_t *hatlockp; 8439 8440 ASSERT(hat->sfmmu_xhat_provider == NULL); 8441 8442 hatlockp = sfmmu_hat_enter(hat); 8443 hat->sfmmu_rmstat++; 8444 sfmmu_hat_exit(hatlockp); 8445 return (1); 8446 } 8447 8448 void 8449 hat_stats_disable(struct hat *hat) 8450 { 8451 hatlock_t *hatlockp; 8452 8453 ASSERT(hat->sfmmu_xhat_provider == NULL); 8454 8455 hatlockp = sfmmu_hat_enter(hat); 8456 hat->sfmmu_rmstat--; 8457 sfmmu_hat_exit(hatlockp); 8458 } 8459 8460 /* 8461 * Routines for entering or removing ourselves from the 8462 * ism_hat's mapping list. This is used for both private and 8463 * SCD hats. 8464 */ 8465 static void 8466 iment_add(struct ism_ment *iment, struct hat *ism_hat) 8467 { 8468 ASSERT(MUTEX_HELD(&ism_mlist_lock)); 8469 8470 iment->iment_prev = NULL; 8471 iment->iment_next = ism_hat->sfmmu_iment; 8472 if (ism_hat->sfmmu_iment) { 8473 ism_hat->sfmmu_iment->iment_prev = iment; 8474 } 8475 ism_hat->sfmmu_iment = iment; 8476 } 8477 8478 static void 8479 iment_sub(struct ism_ment *iment, struct hat *ism_hat) 8480 { 8481 ASSERT(MUTEX_HELD(&ism_mlist_lock)); 8482 8483 if (ism_hat->sfmmu_iment == NULL) { 8484 panic("ism map entry remove - no entries"); 8485 } 8486 8487 if (iment->iment_prev) { 8488 ASSERT(ism_hat->sfmmu_iment != iment); 8489 iment->iment_prev->iment_next = iment->iment_next; 8490 } else { 8491 ASSERT(ism_hat->sfmmu_iment == iment); 8492 ism_hat->sfmmu_iment = iment->iment_next; 8493 } 8494 8495 if (iment->iment_next) { 8496 iment->iment_next->iment_prev = iment->iment_prev; 8497 } 8498 8499 /* 8500 * zero out the entry 8501 */ 8502 iment->iment_next = NULL; 8503 iment->iment_prev = NULL; 8504 iment->iment_hat = NULL; 8505 } 8506 8507 /* 8508 * Hat_share()/unshare() return an (non-zero) error 8509 * when saddr and daddr are not properly aligned. 8510 * 8511 * The top level mapping element determines the alignment 8512 * requirement for saddr and daddr, depending on different 8513 * architectures. 8514 * 8515 * When hat_share()/unshare() are not supported, 8516 * HATOP_SHARE()/UNSHARE() return 0 8517 */ 8518 int 8519 hat_share(struct hat *sfmmup, caddr_t addr, 8520 struct hat *ism_hatid, caddr_t sptaddr, size_t len, uint_t ismszc) 8521 { 8522 ism_blk_t *ism_blkp; 8523 ism_blk_t *new_iblk; 8524 ism_map_t *ism_map; 8525 ism_ment_t *ism_ment; 8526 int i, added; 8527 hatlock_t *hatlockp; 8528 int reload_mmu = 0; 8529 uint_t ismshift = page_get_shift(ismszc); 8530 size_t ismpgsz = page_get_pagesize(ismszc); 8531 uint_t ismmask = (uint_t)ismpgsz - 1; 8532 size_t sh_size = ISM_SHIFT(ismshift, len); 8533 ushort_t ismhatflag; 8534 hat_region_cookie_t rcookie; 8535 sf_scd_t *old_scdp; 8536 8537 #ifdef DEBUG 8538 caddr_t eaddr = addr + len; 8539 #endif /* DEBUG */ 8540 8541 ASSERT(ism_hatid != NULL && sfmmup != NULL); 8542 ASSERT(sptaddr == ISMID_STARTADDR); 8543 /* 8544 * Check the alignment. 8545 */ 8546 if (!ISM_ALIGNED(ismshift, addr) || !ISM_ALIGNED(ismshift, sptaddr)) 8547 return (EINVAL); 8548 8549 /* 8550 * Check size alignment. 8551 */ 8552 if (!ISM_ALIGNED(ismshift, len)) 8553 return (EINVAL); 8554 8555 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 8556 8557 /* 8558 * Allocate ism_ment for the ism_hat's mapping list, and an 8559 * ism map blk in case we need one. We must do our 8560 * allocations before acquiring locks to prevent a deadlock 8561 * in the kmem allocator on the mapping list lock. 8562 */ 8563 new_iblk = kmem_cache_alloc(ism_blk_cache, KM_SLEEP); 8564 ism_ment = kmem_cache_alloc(ism_ment_cache, KM_SLEEP); 8565 8566 /* 8567 * Serialize ISM mappings with the ISM busy flag, and also the 8568 * trap handlers. 8569 */ 8570 sfmmu_ismhat_enter(sfmmup, 0); 8571 8572 /* 8573 * Allocate an ism map blk if necessary. 8574 */ 8575 if (sfmmup->sfmmu_iblk == NULL) { 8576 sfmmup->sfmmu_iblk = new_iblk; 8577 bzero(new_iblk, sizeof (*new_iblk)); 8578 new_iblk->iblk_nextpa = (uint64_t)-1; 8579 membar_stst(); /* make sure next ptr visible to all CPUs */ 8580 sfmmup->sfmmu_ismblkpa = va_to_pa((caddr_t)new_iblk); 8581 reload_mmu = 1; 8582 new_iblk = NULL; 8583 } 8584 8585 #ifdef DEBUG 8586 /* 8587 * Make sure mapping does not already exist. 8588 */ 8589 ism_blkp = sfmmup->sfmmu_iblk; 8590 while (ism_blkp != NULL) { 8591 ism_map = ism_blkp->iblk_maps; 8592 for (i = 0; i < ISM_MAP_SLOTS && ism_map[i].imap_ismhat; i++) { 8593 if ((addr >= ism_start(ism_map[i]) && 8594 addr < ism_end(ism_map[i])) || 8595 eaddr > ism_start(ism_map[i]) && 8596 eaddr <= ism_end(ism_map[i])) { 8597 panic("sfmmu_share: Already mapped!"); 8598 } 8599 } 8600 ism_blkp = ism_blkp->iblk_next; 8601 } 8602 #endif /* DEBUG */ 8603 8604 ASSERT(ismszc >= TTE4M); 8605 if (ismszc == TTE4M) { 8606 ismhatflag = HAT_4M_FLAG; 8607 } else if (ismszc == TTE32M) { 8608 ismhatflag = HAT_32M_FLAG; 8609 } else if (ismszc == TTE256M) { 8610 ismhatflag = HAT_256M_FLAG; 8611 } 8612 /* 8613 * Add mapping to first available mapping slot. 8614 */ 8615 ism_blkp = sfmmup->sfmmu_iblk; 8616 added = 0; 8617 while (!added) { 8618 ism_map = ism_blkp->iblk_maps; 8619 for (i = 0; i < ISM_MAP_SLOTS; i++) { 8620 if (ism_map[i].imap_ismhat == NULL) { 8621 8622 ism_map[i].imap_ismhat = ism_hatid; 8623 ism_map[i].imap_vb_shift = (uchar_t)ismshift; 8624 ism_map[i].imap_rid = SFMMU_INVALID_ISMRID; 8625 ism_map[i].imap_hatflags = ismhatflag; 8626 ism_map[i].imap_sz_mask = ismmask; 8627 /* 8628 * imap_seg is checked in ISM_CHECK to see if 8629 * non-NULL, then other info assumed valid. 8630 */ 8631 membar_stst(); 8632 ism_map[i].imap_seg = (uintptr_t)addr | sh_size; 8633 ism_map[i].imap_ment = ism_ment; 8634 8635 /* 8636 * Now add ourselves to the ism_hat's 8637 * mapping list. 8638 */ 8639 ism_ment->iment_hat = sfmmup; 8640 ism_ment->iment_base_va = addr; 8641 ism_hatid->sfmmu_ismhat = 1; 8642 mutex_enter(&ism_mlist_lock); 8643 iment_add(ism_ment, ism_hatid); 8644 mutex_exit(&ism_mlist_lock); 8645 added = 1; 8646 break; 8647 } 8648 } 8649 if (!added && ism_blkp->iblk_next == NULL) { 8650 ism_blkp->iblk_next = new_iblk; 8651 new_iblk = NULL; 8652 bzero(ism_blkp->iblk_next, 8653 sizeof (*ism_blkp->iblk_next)); 8654 ism_blkp->iblk_next->iblk_nextpa = (uint64_t)-1; 8655 membar_stst(); 8656 ism_blkp->iblk_nextpa = 8657 va_to_pa((caddr_t)ism_blkp->iblk_next); 8658 } 8659 ism_blkp = ism_blkp->iblk_next; 8660 } 8661 8662 /* 8663 * After calling hat_join_region, sfmmup may join a new SCD or 8664 * move from the old scd to a new scd, in which case, we want to 8665 * shrink the sfmmup's private tsb size, i.e., pass shrink to 8666 * sfmmu_check_page_sizes at the end of this routine. 8667 */ 8668 old_scdp = sfmmup->sfmmu_scdp; 8669 8670 rcookie = hat_join_region(sfmmup, addr, len, (void *)ism_hatid, 0, 8671 PROT_ALL, ismszc, NULL, HAT_REGION_ISM); 8672 if (rcookie != HAT_INVALID_REGION_COOKIE) { 8673 ism_map[i].imap_rid = (uchar_t)((uint64_t)rcookie); 8674 } 8675 /* 8676 * Update our counters for this sfmmup's ism mappings. 8677 */ 8678 for (i = 0; i <= ismszc; i++) { 8679 if (!(disable_ism_large_pages & (1 << i))) 8680 (void) ism_tsb_entries(sfmmup, i); 8681 } 8682 8683 /* 8684 * For ISM and DISM we do not support 512K pages, so we only only 8685 * search the 4M and 8K/64K hashes for 4 pagesize cpus, and search the 8686 * 256M or 32M, and 4M and 8K/64K hashes for 6 pagesize cpus. 8687 * 8688 * Need to set 32M/256M ISM flags to make sure 8689 * sfmmu_check_page_sizes() enables them on Panther. 8690 */ 8691 ASSERT((disable_ism_large_pages & (1 << TTE512K)) != 0); 8692 8693 switch (ismszc) { 8694 case TTE256M: 8695 if (!SFMMU_FLAGS_ISSET(sfmmup, HAT_256M_ISM)) { 8696 hatlockp = sfmmu_hat_enter(sfmmup); 8697 SFMMU_FLAGS_SET(sfmmup, HAT_256M_ISM); 8698 sfmmu_hat_exit(hatlockp); 8699 } 8700 break; 8701 case TTE32M: 8702 if (!SFMMU_FLAGS_ISSET(sfmmup, HAT_32M_ISM)) { 8703 hatlockp = sfmmu_hat_enter(sfmmup); 8704 SFMMU_FLAGS_SET(sfmmup, HAT_32M_ISM); 8705 sfmmu_hat_exit(hatlockp); 8706 } 8707 break; 8708 default: 8709 break; 8710 } 8711 8712 /* 8713 * If we updated the ismblkpa for this HAT we must make 8714 * sure all CPUs running this process reload their tsbmiss area. 8715 * Otherwise they will fail to load the mappings in the tsbmiss 8716 * handler and will loop calling pagefault(). 8717 */ 8718 if (reload_mmu) { 8719 hatlockp = sfmmu_hat_enter(sfmmup); 8720 sfmmu_sync_mmustate(sfmmup); 8721 sfmmu_hat_exit(hatlockp); 8722 } 8723 8724 sfmmu_ismhat_exit(sfmmup, 0); 8725 8726 /* 8727 * Free up ismblk if we didn't use it. 8728 */ 8729 if (new_iblk != NULL) 8730 kmem_cache_free(ism_blk_cache, new_iblk); 8731 8732 /* 8733 * Check TSB and TLB page sizes. 8734 */ 8735 if (sfmmup->sfmmu_scdp != NULL && old_scdp != sfmmup->sfmmu_scdp) { 8736 sfmmu_check_page_sizes(sfmmup, 0); 8737 } else { 8738 sfmmu_check_page_sizes(sfmmup, 1); 8739 } 8740 return (0); 8741 } 8742 8743 /* 8744 * hat_unshare removes exactly one ism_map from 8745 * this process's as. It expects multiple calls 8746 * to hat_unshare for multiple shm segments. 8747 */ 8748 void 8749 hat_unshare(struct hat *sfmmup, caddr_t addr, size_t len, uint_t ismszc) 8750 { 8751 ism_map_t *ism_map; 8752 ism_ment_t *free_ment = NULL; 8753 ism_blk_t *ism_blkp; 8754 struct hat *ism_hatid; 8755 int found, i; 8756 hatlock_t *hatlockp; 8757 struct tsb_info *tsbinfo; 8758 uint_t ismshift = page_get_shift(ismszc); 8759 size_t sh_size = ISM_SHIFT(ismshift, len); 8760 uchar_t ism_rid; 8761 sf_scd_t *old_scdp; 8762 8763 ASSERT(ISM_ALIGNED(ismshift, addr)); 8764 ASSERT(ISM_ALIGNED(ismshift, len)); 8765 ASSERT(sfmmup != NULL); 8766 ASSERT(sfmmup != ksfmmup); 8767 8768 if (sfmmup->sfmmu_xhat_provider) { 8769 XHAT_UNSHARE(sfmmup, addr, len); 8770 return; 8771 } else { 8772 /* 8773 * This must be a CPU HAT. If the address space has 8774 * XHATs attached, inform all XHATs that ISM segment 8775 * is going away 8776 */ 8777 ASSERT(sfmmup->sfmmu_as != NULL); 8778 if (sfmmup->sfmmu_as->a_xhat != NULL) 8779 xhat_unshare_all(sfmmup->sfmmu_as, addr, len); 8780 } 8781 8782 /* 8783 * Make sure that during the entire time ISM mappings are removed, 8784 * the trap handlers serialize behind us, and that no one else 8785 * can be mucking with ISM mappings. This also lets us get away 8786 * with not doing expensive cross calls to flush the TLB -- we 8787 * just discard the context, flush the entire TSB, and call it 8788 * a day. 8789 */ 8790 sfmmu_ismhat_enter(sfmmup, 0); 8791 8792 /* 8793 * Remove the mapping. 8794 * 8795 * We can't have any holes in the ism map. 8796 * The tsb miss code while searching the ism map will 8797 * stop on an empty map slot. So we must move 8798 * everyone past the hole up 1 if any. 8799 * 8800 * Also empty ism map blks are not freed until the 8801 * process exits. This is to prevent a MT race condition 8802 * between sfmmu_unshare() and sfmmu_tsbmiss_exception(). 8803 */ 8804 found = 0; 8805 ism_blkp = sfmmup->sfmmu_iblk; 8806 while (!found && ism_blkp != NULL) { 8807 ism_map = ism_blkp->iblk_maps; 8808 for (i = 0; i < ISM_MAP_SLOTS; i++) { 8809 if (addr == ism_start(ism_map[i]) && 8810 sh_size == (size_t)(ism_size(ism_map[i]))) { 8811 found = 1; 8812 break; 8813 } 8814 } 8815 if (!found) 8816 ism_blkp = ism_blkp->iblk_next; 8817 } 8818 8819 if (found) { 8820 ism_hatid = ism_map[i].imap_ismhat; 8821 ism_rid = ism_map[i].imap_rid; 8822 ASSERT(ism_hatid != NULL); 8823 ASSERT(ism_hatid->sfmmu_ismhat == 1); 8824 8825 /* 8826 * After hat_leave_region, the sfmmup may leave SCD, 8827 * in which case, we want to grow the private tsb size when 8828 * calling sfmmu_check_page_sizes at the end of the routine. 8829 */ 8830 old_scdp = sfmmup->sfmmu_scdp; 8831 /* 8832 * Then remove ourselves from the region. 8833 */ 8834 if (ism_rid != SFMMU_INVALID_ISMRID) { 8835 hat_leave_region(sfmmup, (void *)((uint64_t)ism_rid), 8836 HAT_REGION_ISM); 8837 } 8838 8839 /* 8840 * And now guarantee that any other cpu 8841 * that tries to process an ISM miss 8842 * will go to tl=0. 8843 */ 8844 hatlockp = sfmmu_hat_enter(sfmmup); 8845 sfmmu_invalidate_ctx(sfmmup); 8846 sfmmu_hat_exit(hatlockp); 8847 8848 /* 8849 * Remove ourselves from the ism mapping list. 8850 */ 8851 mutex_enter(&ism_mlist_lock); 8852 iment_sub(ism_map[i].imap_ment, ism_hatid); 8853 mutex_exit(&ism_mlist_lock); 8854 free_ment = ism_map[i].imap_ment; 8855 8856 /* 8857 * We delete the ism map by copying 8858 * the next map over the current one. 8859 * We will take the next one in the maps 8860 * array or from the next ism_blk. 8861 */ 8862 while (ism_blkp != NULL) { 8863 ism_map = ism_blkp->iblk_maps; 8864 while (i < (ISM_MAP_SLOTS - 1)) { 8865 ism_map[i] = ism_map[i + 1]; 8866 i++; 8867 } 8868 /* i == (ISM_MAP_SLOTS - 1) */ 8869 ism_blkp = ism_blkp->iblk_next; 8870 if (ism_blkp != NULL) { 8871 ism_map[i] = ism_blkp->iblk_maps[0]; 8872 i = 0; 8873 } else { 8874 ism_map[i].imap_seg = 0; 8875 ism_map[i].imap_vb_shift = 0; 8876 ism_map[i].imap_rid = SFMMU_INVALID_ISMRID; 8877 ism_map[i].imap_hatflags = 0; 8878 ism_map[i].imap_sz_mask = 0; 8879 ism_map[i].imap_ismhat = NULL; 8880 ism_map[i].imap_ment = NULL; 8881 } 8882 } 8883 8884 /* 8885 * Now flush entire TSB for the process, since 8886 * demapping page by page can be too expensive. 8887 * We don't have to flush the TLB here anymore 8888 * since we switch to a new TLB ctx instead. 8889 * Also, there is no need to flush if the process 8890 * is exiting since the TSB will be freed later. 8891 */ 8892 if (!sfmmup->sfmmu_free) { 8893 hatlockp = sfmmu_hat_enter(sfmmup); 8894 for (tsbinfo = sfmmup->sfmmu_tsb; tsbinfo != NULL; 8895 tsbinfo = tsbinfo->tsb_next) { 8896 if (tsbinfo->tsb_flags & TSB_SWAPPED) 8897 continue; 8898 if (tsbinfo->tsb_flags & TSB_RELOC_FLAG) { 8899 tsbinfo->tsb_flags |= 8900 TSB_FLUSH_NEEDED; 8901 continue; 8902 } 8903 8904 sfmmu_inv_tsb(tsbinfo->tsb_va, 8905 TSB_BYTES(tsbinfo->tsb_szc)); 8906 } 8907 sfmmu_hat_exit(hatlockp); 8908 } 8909 } 8910 8911 /* 8912 * Update our counters for this sfmmup's ism mappings. 8913 */ 8914 for (i = 0; i <= ismszc; i++) { 8915 if (!(disable_ism_large_pages & (1 << i))) 8916 (void) ism_tsb_entries(sfmmup, i); 8917 } 8918 8919 sfmmu_ismhat_exit(sfmmup, 0); 8920 8921 /* 8922 * We must do our freeing here after dropping locks 8923 * to prevent a deadlock in the kmem allocator on the 8924 * mapping list lock. 8925 */ 8926 if (free_ment != NULL) 8927 kmem_cache_free(ism_ment_cache, free_ment); 8928 8929 /* 8930 * Check TSB and TLB page sizes if the process isn't exiting. 8931 */ 8932 if (!sfmmup->sfmmu_free) { 8933 if (found && old_scdp != NULL && sfmmup->sfmmu_scdp == NULL) { 8934 sfmmu_check_page_sizes(sfmmup, 1); 8935 } else { 8936 sfmmu_check_page_sizes(sfmmup, 0); 8937 } 8938 } 8939 } 8940 8941 /* ARGSUSED */ 8942 static int 8943 sfmmu_idcache_constructor(void *buf, void *cdrarg, int kmflags) 8944 { 8945 /* void *buf is sfmmu_t pointer */ 8946 bzero(buf, sizeof (sfmmu_t)); 8947 8948 return (0); 8949 } 8950 8951 /* ARGSUSED */ 8952 static void 8953 sfmmu_idcache_destructor(void *buf, void *cdrarg) 8954 { 8955 /* void *buf is sfmmu_t pointer */ 8956 } 8957 8958 /* 8959 * setup kmem hmeblks by bzeroing all members and initializing the nextpa 8960 * field to be the pa of this hmeblk 8961 */ 8962 /* ARGSUSED */ 8963 static int 8964 sfmmu_hblkcache_constructor(void *buf, void *cdrarg, int kmflags) 8965 { 8966 struct hme_blk *hmeblkp; 8967 8968 bzero(buf, (size_t)cdrarg); 8969 hmeblkp = (struct hme_blk *)buf; 8970 hmeblkp->hblk_nextpa = va_to_pa((caddr_t)hmeblkp); 8971 8972 #ifdef HBLK_TRACE 8973 mutex_init(&hmeblkp->hblk_audit_lock, NULL, MUTEX_DEFAULT, NULL); 8974 #endif /* HBLK_TRACE */ 8975 8976 return (0); 8977 } 8978 8979 /* ARGSUSED */ 8980 static void 8981 sfmmu_hblkcache_destructor(void *buf, void *cdrarg) 8982 { 8983 8984 #ifdef HBLK_TRACE 8985 8986 struct hme_blk *hmeblkp; 8987 8988 hmeblkp = (struct hme_blk *)buf; 8989 mutex_destroy(&hmeblkp->hblk_audit_lock); 8990 8991 #endif /* HBLK_TRACE */ 8992 } 8993 8994 #define SFMMU_CACHE_RECLAIM_SCAN_RATIO 8 8995 static int sfmmu_cache_reclaim_scan_ratio = SFMMU_CACHE_RECLAIM_SCAN_RATIO; 8996 /* 8997 * The kmem allocator will callback into our reclaim routine when the system 8998 * is running low in memory. We traverse the hash and free up all unused but 8999 * still cached hme_blks. We also traverse the free list and free them up 9000 * as well. 9001 */ 9002 /*ARGSUSED*/ 9003 static void 9004 sfmmu_hblkcache_reclaim(void *cdrarg) 9005 { 9006 int i; 9007 uint64_t hblkpa, prevpa, nx_pa; 9008 struct hmehash_bucket *hmebp; 9009 struct hme_blk *hmeblkp, *nx_hblk, *pr_hblk = NULL; 9010 static struct hmehash_bucket *uhmehash_reclaim_hand; 9011 static struct hmehash_bucket *khmehash_reclaim_hand; 9012 struct hme_blk *list = NULL; 9013 9014 hmebp = uhmehash_reclaim_hand; 9015 if (hmebp == NULL || hmebp > &uhme_hash[UHMEHASH_SZ]) 9016 uhmehash_reclaim_hand = hmebp = uhme_hash; 9017 uhmehash_reclaim_hand += UHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; 9018 9019 for (i = UHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; i; i--) { 9020 if (SFMMU_HASH_LOCK_TRYENTER(hmebp) != 0) { 9021 hmeblkp = hmebp->hmeblkp; 9022 hblkpa = hmebp->hmeh_nextpa; 9023 prevpa = 0; 9024 pr_hblk = NULL; 9025 while (hmeblkp) { 9026 nx_hblk = hmeblkp->hblk_next; 9027 nx_pa = hmeblkp->hblk_nextpa; 9028 if (!hmeblkp->hblk_vcnt && 9029 !hmeblkp->hblk_hmecnt) { 9030 sfmmu_hblk_hash_rm(hmebp, hmeblkp, 9031 prevpa, pr_hblk); 9032 sfmmu_hblk_free(hmebp, hmeblkp, 9033 hblkpa, &list); 9034 } else { 9035 pr_hblk = hmeblkp; 9036 prevpa = hblkpa; 9037 } 9038 hmeblkp = nx_hblk; 9039 hblkpa = nx_pa; 9040 } 9041 SFMMU_HASH_UNLOCK(hmebp); 9042 } 9043 if (hmebp++ == &uhme_hash[UHMEHASH_SZ]) 9044 hmebp = uhme_hash; 9045 } 9046 9047 hmebp = khmehash_reclaim_hand; 9048 if (hmebp == NULL || hmebp > &khme_hash[KHMEHASH_SZ]) 9049 khmehash_reclaim_hand = hmebp = khme_hash; 9050 khmehash_reclaim_hand += KHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; 9051 9052 for (i = KHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; i; i--) { 9053 if (SFMMU_HASH_LOCK_TRYENTER(hmebp) != 0) { 9054 hmeblkp = hmebp->hmeblkp; 9055 hblkpa = hmebp->hmeh_nextpa; 9056 prevpa = 0; 9057 pr_hblk = NULL; 9058 while (hmeblkp) { 9059 nx_hblk = hmeblkp->hblk_next; 9060 nx_pa = hmeblkp->hblk_nextpa; 9061 if (!hmeblkp->hblk_vcnt && 9062 !hmeblkp->hblk_hmecnt) { 9063 sfmmu_hblk_hash_rm(hmebp, hmeblkp, 9064 prevpa, pr_hblk); 9065 sfmmu_hblk_free(hmebp, hmeblkp, 9066 hblkpa, &list); 9067 } else { 9068 pr_hblk = hmeblkp; 9069 prevpa = hblkpa; 9070 } 9071 hmeblkp = nx_hblk; 9072 hblkpa = nx_pa; 9073 } 9074 SFMMU_HASH_UNLOCK(hmebp); 9075 } 9076 if (hmebp++ == &khme_hash[KHMEHASH_SZ]) 9077 hmebp = khme_hash; 9078 } 9079 sfmmu_hblks_list_purge(&list); 9080 } 9081 9082 /* 9083 * sfmmu_get_ppvcolor should become a vm_machdep or hatop interface. 9084 * same goes for sfmmu_get_addrvcolor(). 9085 * 9086 * This function will return the virtual color for the specified page. The 9087 * virtual color corresponds to this page current mapping or its last mapping. 9088 * It is used by memory allocators to choose addresses with the correct 9089 * alignment so vac consistency is automatically maintained. If the page 9090 * has no color it returns -1. 9091 */ 9092 /*ARGSUSED*/ 9093 int 9094 sfmmu_get_ppvcolor(struct page *pp) 9095 { 9096 #ifdef VAC 9097 int color; 9098 9099 if (!(cache & CACHE_VAC) || PP_NEWPAGE(pp)) { 9100 return (-1); 9101 } 9102 color = PP_GET_VCOLOR(pp); 9103 ASSERT(color < mmu_btop(shm_alignment)); 9104 return (color); 9105 #else 9106 return (-1); 9107 #endif /* VAC */ 9108 } 9109 9110 /* 9111 * This function will return the desired alignment for vac consistency 9112 * (vac color) given a virtual address. If no vac is present it returns -1. 9113 */ 9114 /*ARGSUSED*/ 9115 int 9116 sfmmu_get_addrvcolor(caddr_t vaddr) 9117 { 9118 #ifdef VAC 9119 if (cache & CACHE_VAC) { 9120 return (addr_to_vcolor(vaddr)); 9121 } else { 9122 return (-1); 9123 } 9124 #else 9125 return (-1); 9126 #endif /* VAC */ 9127 } 9128 9129 #ifdef VAC 9130 /* 9131 * Check for conflicts. 9132 * A conflict exists if the new and existent mappings do not match in 9133 * their "shm_alignment fields. If conflicts exist, the existant mappings 9134 * are flushed unless one of them is locked. If one of them is locked, then 9135 * the mappings are flushed and converted to non-cacheable mappings. 9136 */ 9137 static void 9138 sfmmu_vac_conflict(struct hat *hat, caddr_t addr, page_t *pp) 9139 { 9140 struct hat *tmphat; 9141 struct sf_hment *sfhmep, *tmphme = NULL; 9142 struct hme_blk *hmeblkp; 9143 int vcolor; 9144 tte_t tte; 9145 9146 ASSERT(sfmmu_mlist_held(pp)); 9147 ASSERT(!PP_ISNC(pp)); /* page better be cacheable */ 9148 9149 vcolor = addr_to_vcolor(addr); 9150 if (PP_NEWPAGE(pp)) { 9151 PP_SET_VCOLOR(pp, vcolor); 9152 return; 9153 } 9154 9155 if (PP_GET_VCOLOR(pp) == vcolor) { 9156 return; 9157 } 9158 9159 if (!PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp)) { 9160 /* 9161 * Previous user of page had a different color 9162 * but since there are no current users 9163 * we just flush the cache and change the color. 9164 */ 9165 SFMMU_STAT(sf_pgcolor_conflict); 9166 sfmmu_cache_flush(pp->p_pagenum, PP_GET_VCOLOR(pp)); 9167 PP_SET_VCOLOR(pp, vcolor); 9168 return; 9169 } 9170 9171 /* 9172 * If we get here we have a vac conflict with a current 9173 * mapping. VAC conflict policy is as follows. 9174 * - The default is to unload the other mappings unless: 9175 * - If we have a large mapping we uncache the page. 9176 * We need to uncache the rest of the large page too. 9177 * - If any of the mappings are locked we uncache the page. 9178 * - If the requested mapping is inconsistent 9179 * with another mapping and that mapping 9180 * is in the same address space we have to 9181 * make it non-cached. The default thing 9182 * to do is unload the inconsistent mapping 9183 * but if they are in the same address space 9184 * we run the risk of unmapping the pc or the 9185 * stack which we will use as we return to the user, 9186 * in which case we can then fault on the thing 9187 * we just unloaded and get into an infinite loop. 9188 */ 9189 if (PP_ISMAPPED_LARGE(pp)) { 9190 int sz; 9191 9192 /* 9193 * Existing mapping is for big pages. We don't unload 9194 * existing big mappings to satisfy new mappings. 9195 * Always convert all mappings to TNC. 9196 */ 9197 sz = fnd_mapping_sz(pp); 9198 pp = PP_GROUPLEADER(pp, sz); 9199 SFMMU_STAT_ADD(sf_uncache_conflict, TTEPAGES(sz)); 9200 sfmmu_page_cache_array(pp, HAT_TMPNC, CACHE_FLUSH, 9201 TTEPAGES(sz)); 9202 9203 return; 9204 } 9205 9206 /* 9207 * check if any mapping is in same as or if it is locked 9208 * since in that case we need to uncache. 9209 */ 9210 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = tmphme) { 9211 tmphme = sfhmep->hme_next; 9212 if (IS_PAHME(sfhmep)) 9213 continue; 9214 hmeblkp = sfmmu_hmetohblk(sfhmep); 9215 if (hmeblkp->hblk_xhat_bit) 9216 continue; 9217 tmphat = hblktosfmmu(hmeblkp); 9218 sfmmu_copytte(&sfhmep->hme_tte, &tte); 9219 ASSERT(TTE_IS_VALID(&tte)); 9220 if (hmeblkp->hblk_shared || tmphat == hat || 9221 hmeblkp->hblk_lckcnt) { 9222 /* 9223 * We have an uncache conflict 9224 */ 9225 SFMMU_STAT(sf_uncache_conflict); 9226 sfmmu_page_cache_array(pp, HAT_TMPNC, CACHE_FLUSH, 1); 9227 return; 9228 } 9229 } 9230 9231 /* 9232 * We have an unload conflict 9233 * We have already checked for LARGE mappings, therefore 9234 * the remaining mapping(s) must be TTE8K. 9235 */ 9236 SFMMU_STAT(sf_unload_conflict); 9237 9238 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = tmphme) { 9239 tmphme = sfhmep->hme_next; 9240 if (IS_PAHME(sfhmep)) 9241 continue; 9242 hmeblkp = sfmmu_hmetohblk(sfhmep); 9243 if (hmeblkp->hblk_xhat_bit) 9244 continue; 9245 ASSERT(!hmeblkp->hblk_shared); 9246 (void) sfmmu_pageunload(pp, sfhmep, TTE8K); 9247 } 9248 9249 if (PP_ISMAPPED_KPM(pp)) 9250 sfmmu_kpm_vac_unload(pp, addr); 9251 9252 /* 9253 * Unloads only do TLB flushes so we need to flush the 9254 * cache here. 9255 */ 9256 sfmmu_cache_flush(pp->p_pagenum, PP_GET_VCOLOR(pp)); 9257 PP_SET_VCOLOR(pp, vcolor); 9258 } 9259 9260 /* 9261 * Whenever a mapping is unloaded and the page is in TNC state, 9262 * we see if the page can be made cacheable again. 'pp' is 9263 * the page that we just unloaded a mapping from, the size 9264 * of mapping that was unloaded is 'ottesz'. 9265 * Remark: 9266 * The recache policy for mpss pages can leave a performance problem 9267 * under the following circumstances: 9268 * . A large page in uncached mode has just been unmapped. 9269 * . All constituent pages are TNC due to a conflicting small mapping. 9270 * . There are many other, non conflicting, small mappings around for 9271 * a lot of the constituent pages. 9272 * . We're called w/ the "old" groupleader page and the old ottesz, 9273 * but this is irrelevant, since we're no more "PP_ISMAPPED_LARGE", so 9274 * we end up w/ TTE8K or npages == 1. 9275 * . We call tst_tnc w/ the old groupleader only, and if there is no 9276 * conflict, we re-cache only this page. 9277 * . All other small mappings are not checked and will be left in TNC mode. 9278 * The problem is not very serious because: 9279 * . mpss is actually only defined for heap and stack, so the probability 9280 * is not very high that a large page mapping exists in parallel to a small 9281 * one (this is possible, but seems to be bad programming style in the 9282 * appl). 9283 * . The problem gets a little bit more serious, when those TNC pages 9284 * have to be mapped into kernel space, e.g. for networking. 9285 * . When VAC alias conflicts occur in applications, this is regarded 9286 * as an application bug. So if kstat's show them, the appl should 9287 * be changed anyway. 9288 */ 9289 void 9290 conv_tnc(page_t *pp, int ottesz) 9291 { 9292 int cursz, dosz; 9293 pgcnt_t curnpgs, dopgs; 9294 pgcnt_t pg64k; 9295 page_t *pp2; 9296 9297 /* 9298 * Determine how big a range we check for TNC and find 9299 * leader page. cursz is the size of the biggest 9300 * mapping that still exist on 'pp'. 9301 */ 9302 if (PP_ISMAPPED_LARGE(pp)) { 9303 cursz = fnd_mapping_sz(pp); 9304 } else { 9305 cursz = TTE8K; 9306 } 9307 9308 if (ottesz >= cursz) { 9309 dosz = ottesz; 9310 pp2 = pp; 9311 } else { 9312 dosz = cursz; 9313 pp2 = PP_GROUPLEADER(pp, dosz); 9314 } 9315 9316 pg64k = TTEPAGES(TTE64K); 9317 dopgs = TTEPAGES(dosz); 9318 9319 ASSERT(dopgs == 1 || ((dopgs & (pg64k - 1)) == 0)); 9320 9321 while (dopgs != 0) { 9322 curnpgs = TTEPAGES(cursz); 9323 if (tst_tnc(pp2, curnpgs)) { 9324 SFMMU_STAT_ADD(sf_recache, curnpgs); 9325 sfmmu_page_cache_array(pp2, HAT_CACHE, CACHE_NO_FLUSH, 9326 curnpgs); 9327 } 9328 9329 ASSERT(dopgs >= curnpgs); 9330 dopgs -= curnpgs; 9331 9332 if (dopgs == 0) { 9333 break; 9334 } 9335 9336 pp2 = PP_PAGENEXT_N(pp2, curnpgs); 9337 if (((dopgs & (pg64k - 1)) == 0) && PP_ISMAPPED_LARGE(pp2)) { 9338 cursz = fnd_mapping_sz(pp2); 9339 } else { 9340 cursz = TTE8K; 9341 } 9342 } 9343 } 9344 9345 /* 9346 * Returns 1 if page(s) can be converted from TNC to cacheable setting, 9347 * returns 0 otherwise. Note that oaddr argument is valid for only 9348 * 8k pages. 9349 */ 9350 int 9351 tst_tnc(page_t *pp, pgcnt_t npages) 9352 { 9353 struct sf_hment *sfhme; 9354 struct hme_blk *hmeblkp; 9355 tte_t tte; 9356 caddr_t vaddr; 9357 int clr_valid = 0; 9358 int color, color1, bcolor; 9359 int i, ncolors; 9360 9361 ASSERT(pp != NULL); 9362 ASSERT(!(cache & CACHE_WRITEBACK)); 9363 9364 if (npages > 1) { 9365 ncolors = CACHE_NUM_COLOR; 9366 } 9367 9368 for (i = 0; i < npages; i++) { 9369 ASSERT(sfmmu_mlist_held(pp)); 9370 ASSERT(PP_ISTNC(pp)); 9371 ASSERT(PP_GET_VCOLOR(pp) == NO_VCOLOR); 9372 9373 if (PP_ISPNC(pp)) { 9374 return (0); 9375 } 9376 9377 clr_valid = 0; 9378 if (PP_ISMAPPED_KPM(pp)) { 9379 caddr_t kpmvaddr; 9380 9381 ASSERT(kpm_enable); 9382 kpmvaddr = hat_kpm_page2va(pp, 1); 9383 ASSERT(!(npages > 1 && IS_KPM_ALIAS_RANGE(kpmvaddr))); 9384 color1 = addr_to_vcolor(kpmvaddr); 9385 clr_valid = 1; 9386 } 9387 9388 for (sfhme = pp->p_mapping; sfhme; sfhme = sfhme->hme_next) { 9389 if (IS_PAHME(sfhme)) 9390 continue; 9391 hmeblkp = sfmmu_hmetohblk(sfhme); 9392 if (hmeblkp->hblk_xhat_bit) 9393 continue; 9394 9395 sfmmu_copytte(&sfhme->hme_tte, &tte); 9396 ASSERT(TTE_IS_VALID(&tte)); 9397 9398 vaddr = tte_to_vaddr(hmeblkp, tte); 9399 color = addr_to_vcolor(vaddr); 9400 9401 if (npages > 1) { 9402 /* 9403 * If there is a big mapping, make sure 9404 * 8K mapping is consistent with the big 9405 * mapping. 9406 */ 9407 bcolor = i % ncolors; 9408 if (color != bcolor) { 9409 return (0); 9410 } 9411 } 9412 if (!clr_valid) { 9413 clr_valid = 1; 9414 color1 = color; 9415 } 9416 9417 if (color1 != color) { 9418 return (0); 9419 } 9420 } 9421 9422 pp = PP_PAGENEXT(pp); 9423 } 9424 9425 return (1); 9426 } 9427 9428 void 9429 sfmmu_page_cache_array(page_t *pp, int flags, int cache_flush_flag, 9430 pgcnt_t npages) 9431 { 9432 kmutex_t *pmtx; 9433 int i, ncolors, bcolor; 9434 kpm_hlk_t *kpmp; 9435 cpuset_t cpuset; 9436 9437 ASSERT(pp != NULL); 9438 ASSERT(!(cache & CACHE_WRITEBACK)); 9439 9440 kpmp = sfmmu_kpm_kpmp_enter(pp, npages); 9441 pmtx = sfmmu_page_enter(pp); 9442 9443 /* 9444 * Fast path caching single unmapped page 9445 */ 9446 if (npages == 1 && !PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp) && 9447 flags == HAT_CACHE) { 9448 PP_CLRTNC(pp); 9449 PP_CLRPNC(pp); 9450 sfmmu_page_exit(pmtx); 9451 sfmmu_kpm_kpmp_exit(kpmp); 9452 return; 9453 } 9454 9455 /* 9456 * We need to capture all cpus in order to change cacheability 9457 * because we can't allow one cpu to access the same physical 9458 * page using a cacheable and a non-cachebale mapping at the same 9459 * time. Since we may end up walking the ism mapping list 9460 * have to grab it's lock now since we can't after all the 9461 * cpus have been captured. 9462 */ 9463 sfmmu_hat_lock_all(); 9464 mutex_enter(&ism_mlist_lock); 9465 kpreempt_disable(); 9466 cpuset = cpu_ready_set; 9467 xc_attention(cpuset); 9468 9469 if (npages > 1) { 9470 /* 9471 * Make sure all colors are flushed since the 9472 * sfmmu_page_cache() only flushes one color- 9473 * it does not know big pages. 9474 */ 9475 ncolors = CACHE_NUM_COLOR; 9476 if (flags & HAT_TMPNC) { 9477 for (i = 0; i < ncolors; i++) { 9478 sfmmu_cache_flushcolor(i, pp->p_pagenum); 9479 } 9480 cache_flush_flag = CACHE_NO_FLUSH; 9481 } 9482 } 9483 9484 for (i = 0; i < npages; i++) { 9485 9486 ASSERT(sfmmu_mlist_held(pp)); 9487 9488 if (!(flags == HAT_TMPNC && PP_ISTNC(pp))) { 9489 9490 if (npages > 1) { 9491 bcolor = i % ncolors; 9492 } else { 9493 bcolor = NO_VCOLOR; 9494 } 9495 9496 sfmmu_page_cache(pp, flags, cache_flush_flag, 9497 bcolor); 9498 } 9499 9500 pp = PP_PAGENEXT(pp); 9501 } 9502 9503 xt_sync(cpuset); 9504 xc_dismissed(cpuset); 9505 mutex_exit(&ism_mlist_lock); 9506 sfmmu_hat_unlock_all(); 9507 sfmmu_page_exit(pmtx); 9508 sfmmu_kpm_kpmp_exit(kpmp); 9509 kpreempt_enable(); 9510 } 9511 9512 /* 9513 * This function changes the virtual cacheability of all mappings to a 9514 * particular page. When changing from uncache to cacheable the mappings will 9515 * only be changed if all of them have the same virtual color. 9516 * We need to flush the cache in all cpus. It is possible that 9517 * a process referenced a page as cacheable but has sinced exited 9518 * and cleared the mapping list. We still to flush it but have no 9519 * state so all cpus is the only alternative. 9520 */ 9521 static void 9522 sfmmu_page_cache(page_t *pp, int flags, int cache_flush_flag, int bcolor) 9523 { 9524 struct sf_hment *sfhme; 9525 struct hme_blk *hmeblkp; 9526 sfmmu_t *sfmmup; 9527 tte_t tte, ttemod; 9528 caddr_t vaddr; 9529 int ret, color; 9530 pfn_t pfn; 9531 9532 color = bcolor; 9533 pfn = pp->p_pagenum; 9534 9535 for (sfhme = pp->p_mapping; sfhme; sfhme = sfhme->hme_next) { 9536 9537 if (IS_PAHME(sfhme)) 9538 continue; 9539 hmeblkp = sfmmu_hmetohblk(sfhme); 9540 9541 if (hmeblkp->hblk_xhat_bit) 9542 continue; 9543 9544 sfmmu_copytte(&sfhme->hme_tte, &tte); 9545 ASSERT(TTE_IS_VALID(&tte)); 9546 vaddr = tte_to_vaddr(hmeblkp, tte); 9547 color = addr_to_vcolor(vaddr); 9548 9549 #ifdef DEBUG 9550 if ((flags & HAT_CACHE) && bcolor != NO_VCOLOR) { 9551 ASSERT(color == bcolor); 9552 } 9553 #endif 9554 9555 ASSERT(flags != HAT_TMPNC || color == PP_GET_VCOLOR(pp)); 9556 9557 ttemod = tte; 9558 if (flags & (HAT_UNCACHE | HAT_TMPNC)) { 9559 TTE_CLR_VCACHEABLE(&ttemod); 9560 } else { /* flags & HAT_CACHE */ 9561 TTE_SET_VCACHEABLE(&ttemod); 9562 } 9563 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte); 9564 if (ret < 0) { 9565 /* 9566 * Since all cpus are captured modifytte should not 9567 * fail. 9568 */ 9569 panic("sfmmu_page_cache: write to tte failed"); 9570 } 9571 9572 sfmmup = hblktosfmmu(hmeblkp); 9573 if (cache_flush_flag == CACHE_FLUSH) { 9574 /* 9575 * Flush TSBs, TLBs and caches 9576 */ 9577 if (hmeblkp->hblk_shared) { 9578 sf_srd_t *srdp = (sf_srd_t *)sfmmup; 9579 uint_t rid = hmeblkp->hblk_tag.htag_rid; 9580 sf_region_t *rgnp; 9581 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 9582 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 9583 ASSERT(srdp != NULL); 9584 rgnp = srdp->srd_hmergnp[rid]; 9585 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, 9586 srdp, rgnp, rid); 9587 (void) sfmmu_rgntlb_demap(vaddr, rgnp, 9588 hmeblkp, 0); 9589 sfmmu_cache_flush(pfn, addr_to_vcolor(vaddr)); 9590 } else if (sfmmup->sfmmu_ismhat) { 9591 if (flags & HAT_CACHE) { 9592 SFMMU_STAT(sf_ism_recache); 9593 } else { 9594 SFMMU_STAT(sf_ism_uncache); 9595 } 9596 sfmmu_ismtlbcache_demap(vaddr, sfmmup, hmeblkp, 9597 pfn, CACHE_FLUSH); 9598 } else { 9599 sfmmu_tlbcache_demap(vaddr, sfmmup, hmeblkp, 9600 pfn, 0, FLUSH_ALL_CPUS, CACHE_FLUSH, 1); 9601 } 9602 9603 /* 9604 * all cache entries belonging to this pfn are 9605 * now flushed. 9606 */ 9607 cache_flush_flag = CACHE_NO_FLUSH; 9608 } else { 9609 /* 9610 * Flush only TSBs and TLBs. 9611 */ 9612 if (hmeblkp->hblk_shared) { 9613 sf_srd_t *srdp = (sf_srd_t *)sfmmup; 9614 uint_t rid = hmeblkp->hblk_tag.htag_rid; 9615 sf_region_t *rgnp; 9616 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 9617 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 9618 ASSERT(srdp != NULL); 9619 rgnp = srdp->srd_hmergnp[rid]; 9620 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, 9621 srdp, rgnp, rid); 9622 (void) sfmmu_rgntlb_demap(vaddr, rgnp, 9623 hmeblkp, 0); 9624 } else if (sfmmup->sfmmu_ismhat) { 9625 if (flags & HAT_CACHE) { 9626 SFMMU_STAT(sf_ism_recache); 9627 } else { 9628 SFMMU_STAT(sf_ism_uncache); 9629 } 9630 sfmmu_ismtlbcache_demap(vaddr, sfmmup, hmeblkp, 9631 pfn, CACHE_NO_FLUSH); 9632 } else { 9633 sfmmu_tlb_demap(vaddr, sfmmup, hmeblkp, 0, 1); 9634 } 9635 } 9636 } 9637 9638 if (PP_ISMAPPED_KPM(pp)) 9639 sfmmu_kpm_page_cache(pp, flags, cache_flush_flag); 9640 9641 switch (flags) { 9642 9643 default: 9644 panic("sfmmu_pagecache: unknown flags"); 9645 break; 9646 9647 case HAT_CACHE: 9648 PP_CLRTNC(pp); 9649 PP_CLRPNC(pp); 9650 PP_SET_VCOLOR(pp, color); 9651 break; 9652 9653 case HAT_TMPNC: 9654 PP_SETTNC(pp); 9655 PP_SET_VCOLOR(pp, NO_VCOLOR); 9656 break; 9657 9658 case HAT_UNCACHE: 9659 PP_SETPNC(pp); 9660 PP_CLRTNC(pp); 9661 PP_SET_VCOLOR(pp, NO_VCOLOR); 9662 break; 9663 } 9664 } 9665 #endif /* VAC */ 9666 9667 9668 /* 9669 * Wrapper routine used to return a context. 9670 * 9671 * It's the responsibility of the caller to guarantee that the 9672 * process serializes on calls here by taking the HAT lock for 9673 * the hat. 9674 * 9675 */ 9676 static void 9677 sfmmu_get_ctx(sfmmu_t *sfmmup) 9678 { 9679 mmu_ctx_t *mmu_ctxp; 9680 uint_t pstate_save; 9681 #ifdef sun4v 9682 int ret; 9683 #endif 9684 9685 ASSERT(sfmmu_hat_lock_held(sfmmup)); 9686 ASSERT(sfmmup != ksfmmup); 9687 9688 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_ALLCTX_INVALID)) { 9689 sfmmu_setup_tsbinfo(sfmmup); 9690 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ALLCTX_INVALID); 9691 } 9692 9693 kpreempt_disable(); 9694 9695 mmu_ctxp = CPU_MMU_CTXP(CPU); 9696 ASSERT(mmu_ctxp); 9697 ASSERT(mmu_ctxp->mmu_idx < max_mmu_ctxdoms); 9698 ASSERT(mmu_ctxp == mmu_ctxs_tbl[mmu_ctxp->mmu_idx]); 9699 9700 /* 9701 * Do a wrap-around if cnum reaches the max # cnum supported by a MMU. 9702 */ 9703 if (mmu_ctxp->mmu_cnum == mmu_ctxp->mmu_nctxs) 9704 sfmmu_ctx_wrap_around(mmu_ctxp); 9705 9706 /* 9707 * Let the MMU set up the page sizes to use for 9708 * this context in the TLB. Don't program 2nd dtlb for ism hat. 9709 */ 9710 if ((&mmu_set_ctx_page_sizes) && (sfmmup->sfmmu_ismhat == 0)) { 9711 mmu_set_ctx_page_sizes(sfmmup); 9712 } 9713 9714 /* 9715 * sfmmu_alloc_ctx and sfmmu_load_mmustate will be performed with 9716 * interrupts disabled to prevent race condition with wrap-around 9717 * ctx invalidatation. In sun4v, ctx invalidation also involves 9718 * a HV call to set the number of TSBs to 0. If interrupts are not 9719 * disabled until after sfmmu_load_mmustate is complete TSBs may 9720 * become assigned to INVALID_CONTEXT. This is not allowed. 9721 */ 9722 pstate_save = sfmmu_disable_intrs(); 9723 9724 #ifdef sun4u 9725 (void) sfmmu_alloc_ctx(sfmmup, 1, CPU, SFMMU_PRIVATE); 9726 #else 9727 if (sfmmu_alloc_ctx(sfmmup, 1, CPU, SFMMU_PRIVATE) && 9728 sfmmup->sfmmu_scdp != NULL) { 9729 sf_scd_t *scdp = sfmmup->sfmmu_scdp; 9730 sfmmu_t *scsfmmup = scdp->scd_sfmmup; 9731 ret = sfmmu_alloc_ctx(scsfmmup, 1, CPU, SFMMU_SHARED); 9732 /* debug purpose only */ 9733 ASSERT(!ret || scsfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum 9734 != INVALID_CONTEXT); 9735 } 9736 #endif 9737 sfmmu_load_mmustate(sfmmup); 9738 9739 sfmmu_enable_intrs(pstate_save); 9740 9741 kpreempt_enable(); 9742 } 9743 9744 /* 9745 * When all cnums are used up in a MMU, cnum will wrap around to the 9746 * next generation and start from 2. 9747 */ 9748 static void 9749 sfmmu_ctx_wrap_around(mmu_ctx_t *mmu_ctxp) 9750 { 9751 9752 /* caller must have disabled the preemption */ 9753 ASSERT(curthread->t_preempt >= 1); 9754 ASSERT(mmu_ctxp != NULL); 9755 9756 /* acquire Per-MMU (PM) spin lock */ 9757 mutex_enter(&mmu_ctxp->mmu_lock); 9758 9759 /* re-check to see if wrap-around is needed */ 9760 if (mmu_ctxp->mmu_cnum < mmu_ctxp->mmu_nctxs) 9761 goto done; 9762 9763 SFMMU_MMU_STAT(mmu_wrap_around); 9764 9765 /* update gnum */ 9766 ASSERT(mmu_ctxp->mmu_gnum != 0); 9767 mmu_ctxp->mmu_gnum++; 9768 if (mmu_ctxp->mmu_gnum == 0 || 9769 mmu_ctxp->mmu_gnum > MAX_SFMMU_GNUM_VAL) { 9770 cmn_err(CE_PANIC, "mmu_gnum of mmu_ctx 0x%p is out of bound.", 9771 (void *)mmu_ctxp); 9772 } 9773 9774 if (mmu_ctxp->mmu_ncpus > 1) { 9775 cpuset_t cpuset; 9776 9777 membar_enter(); /* make sure updated gnum visible */ 9778 9779 SFMMU_XCALL_STATS(NULL); 9780 9781 /* xcall to others on the same MMU to invalidate ctx */ 9782 cpuset = mmu_ctxp->mmu_cpuset; 9783 ASSERT(CPU_IN_SET(cpuset, CPU->cpu_id)); 9784 CPUSET_DEL(cpuset, CPU->cpu_id); 9785 CPUSET_AND(cpuset, cpu_ready_set); 9786 9787 /* 9788 * Pass in INVALID_CONTEXT as the first parameter to 9789 * sfmmu_raise_tsb_exception, which invalidates the context 9790 * of any process running on the CPUs in the MMU. 9791 */ 9792 xt_some(cpuset, sfmmu_raise_tsb_exception, 9793 INVALID_CONTEXT, INVALID_CONTEXT); 9794 xt_sync(cpuset); 9795 9796 SFMMU_MMU_STAT(mmu_tsb_raise_exception); 9797 } 9798 9799 if (sfmmu_getctx_sec() != INVALID_CONTEXT) { 9800 sfmmu_setctx_sec(INVALID_CONTEXT); 9801 sfmmu_clear_utsbinfo(); 9802 } 9803 9804 /* 9805 * No xcall is needed here. For sun4u systems all CPUs in context 9806 * domain share a single physical MMU therefore it's enough to flush 9807 * TLB on local CPU. On sun4v systems we use 1 global context 9808 * domain and flush all remote TLBs in sfmmu_raise_tsb_exception 9809 * handler. Note that vtag_flushall_uctxs() is called 9810 * for Ultra II machine, where the equivalent flushall functionality 9811 * is implemented in SW, and only user ctx TLB entries are flushed. 9812 */ 9813 if (&vtag_flushall_uctxs != NULL) { 9814 vtag_flushall_uctxs(); 9815 } else { 9816 vtag_flushall(); 9817 } 9818 9819 /* reset mmu cnum, skips cnum 0 and 1 */ 9820 mmu_ctxp->mmu_cnum = NUM_LOCKED_CTXS; 9821 9822 done: 9823 mutex_exit(&mmu_ctxp->mmu_lock); 9824 } 9825 9826 9827 /* 9828 * For multi-threaded process, set the process context to INVALID_CONTEXT 9829 * so that it faults and reloads the MMU state from TL=0. For single-threaded 9830 * process, we can just load the MMU state directly without having to 9831 * set context invalid. Caller must hold the hat lock since we don't 9832 * acquire it here. 9833 */ 9834 static void 9835 sfmmu_sync_mmustate(sfmmu_t *sfmmup) 9836 { 9837 uint_t cnum; 9838 uint_t pstate_save; 9839 9840 ASSERT(sfmmup != ksfmmup); 9841 ASSERT(sfmmu_hat_lock_held(sfmmup)); 9842 9843 kpreempt_disable(); 9844 9845 /* 9846 * We check whether the pass'ed-in sfmmup is the same as the 9847 * current running proc. This is to makes sure the current proc 9848 * stays single-threaded if it already is. 9849 */ 9850 if ((sfmmup == curthread->t_procp->p_as->a_hat) && 9851 (curthread->t_procp->p_lwpcnt == 1)) { 9852 /* single-thread */ 9853 cnum = sfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum; 9854 if (cnum != INVALID_CONTEXT) { 9855 uint_t curcnum; 9856 /* 9857 * Disable interrupts to prevent race condition 9858 * with sfmmu_ctx_wrap_around ctx invalidation. 9859 * In sun4v, ctx invalidation involves setting 9860 * TSB to NULL, hence, interrupts should be disabled 9861 * untill after sfmmu_load_mmustate is completed. 9862 */ 9863 pstate_save = sfmmu_disable_intrs(); 9864 curcnum = sfmmu_getctx_sec(); 9865 if (curcnum == cnum) 9866 sfmmu_load_mmustate(sfmmup); 9867 sfmmu_enable_intrs(pstate_save); 9868 ASSERT(curcnum == cnum || curcnum == INVALID_CONTEXT); 9869 } 9870 } else { 9871 /* 9872 * multi-thread 9873 * or when sfmmup is not the same as the curproc. 9874 */ 9875 sfmmu_invalidate_ctx(sfmmup); 9876 } 9877 9878 kpreempt_enable(); 9879 } 9880 9881 9882 /* 9883 * Replace the specified TSB with a new TSB. This function gets called when 9884 * we grow, shrink or swapin a TSB. When swapping in a TSB (TSB_SWAPIN), the 9885 * TSB_FORCEALLOC flag may be used to force allocation of a minimum-sized TSB 9886 * (8K). 9887 * 9888 * Caller must hold the HAT lock, but should assume any tsb_info 9889 * pointers it has are no longer valid after calling this function. 9890 * 9891 * Return values: 9892 * TSB_ALLOCFAIL Failed to allocate a TSB, due to memory constraints 9893 * TSB_LOSTRACE HAT is busy, i.e. another thread is already doing 9894 * something to this tsbinfo/TSB 9895 * TSB_SUCCESS Operation succeeded 9896 */ 9897 static tsb_replace_rc_t 9898 sfmmu_replace_tsb(sfmmu_t *sfmmup, struct tsb_info *old_tsbinfo, uint_t szc, 9899 hatlock_t *hatlockp, uint_t flags) 9900 { 9901 struct tsb_info *new_tsbinfo = NULL; 9902 struct tsb_info *curtsb, *prevtsb; 9903 uint_t tte_sz_mask; 9904 int i; 9905 9906 ASSERT(sfmmup != ksfmmup); 9907 ASSERT(sfmmup->sfmmu_ismhat == 0); 9908 ASSERT(sfmmu_hat_lock_held(sfmmup)); 9909 ASSERT(szc <= tsb_max_growsize); 9910 9911 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_BUSY)) 9912 return (TSB_LOSTRACE); 9913 9914 /* 9915 * Find the tsb_info ahead of this one in the list, and 9916 * also make sure that the tsb_info passed in really 9917 * exists! 9918 */ 9919 for (prevtsb = NULL, curtsb = sfmmup->sfmmu_tsb; 9920 curtsb != old_tsbinfo && curtsb != NULL; 9921 prevtsb = curtsb, curtsb = curtsb->tsb_next) 9922 ; 9923 ASSERT(curtsb != NULL); 9924 9925 if (!(flags & TSB_SWAPIN) && SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) { 9926 /* 9927 * The process is swapped out, so just set the new size 9928 * code. When it swaps back in, we'll allocate a new one 9929 * of the new chosen size. 9930 */ 9931 curtsb->tsb_szc = szc; 9932 return (TSB_SUCCESS); 9933 } 9934 SFMMU_FLAGS_SET(sfmmup, HAT_BUSY); 9935 9936 tte_sz_mask = old_tsbinfo->tsb_ttesz_mask; 9937 9938 /* 9939 * All initialization is done inside of sfmmu_tsbinfo_alloc(). 9940 * If we fail to allocate a TSB, exit. 9941 * 9942 * If tsb grows with new tsb size > 4M and old tsb size < 4M, 9943 * then try 4M slab after the initial alloc fails. 9944 * 9945 * If tsb swapin with tsb size > 4M, then try 4M after the 9946 * initial alloc fails. 9947 */ 9948 sfmmu_hat_exit(hatlockp); 9949 if (sfmmu_tsbinfo_alloc(&new_tsbinfo, szc, 9950 tte_sz_mask, flags, sfmmup) && 9951 (!(flags & (TSB_GROW | TSB_SWAPIN)) || (szc <= TSB_4M_SZCODE) || 9952 (!(flags & TSB_SWAPIN) && 9953 (old_tsbinfo->tsb_szc >= TSB_4M_SZCODE)) || 9954 sfmmu_tsbinfo_alloc(&new_tsbinfo, TSB_4M_SZCODE, 9955 tte_sz_mask, flags, sfmmup))) { 9956 (void) sfmmu_hat_enter(sfmmup); 9957 if (!(flags & TSB_SWAPIN)) 9958 SFMMU_STAT(sf_tsb_resize_failures); 9959 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY); 9960 return (TSB_ALLOCFAIL); 9961 } 9962 (void) sfmmu_hat_enter(sfmmup); 9963 9964 /* 9965 * Re-check to make sure somebody else didn't muck with us while we 9966 * didn't hold the HAT lock. If the process swapped out, fine, just 9967 * exit; this can happen if we try to shrink the TSB from the context 9968 * of another process (such as on an ISM unmap), though it is rare. 9969 */ 9970 if (!(flags & TSB_SWAPIN) && SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) { 9971 SFMMU_STAT(sf_tsb_resize_failures); 9972 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY); 9973 sfmmu_hat_exit(hatlockp); 9974 sfmmu_tsbinfo_free(new_tsbinfo); 9975 (void) sfmmu_hat_enter(sfmmup); 9976 return (TSB_LOSTRACE); 9977 } 9978 9979 #ifdef DEBUG 9980 /* Reverify that the tsb_info still exists.. for debugging only */ 9981 for (prevtsb = NULL, curtsb = sfmmup->sfmmu_tsb; 9982 curtsb != old_tsbinfo && curtsb != NULL; 9983 prevtsb = curtsb, curtsb = curtsb->tsb_next) 9984 ; 9985 ASSERT(curtsb != NULL); 9986 #endif /* DEBUG */ 9987 9988 /* 9989 * Quiesce any CPUs running this process on their next TLB miss 9990 * so they atomically see the new tsb_info. We temporarily set the 9991 * context to invalid context so new threads that come on processor 9992 * after we do the xcall to cpusran will also serialize behind the 9993 * HAT lock on TLB miss and will see the new TSB. Since this short 9994 * race with a new thread coming on processor is relatively rare, 9995 * this synchronization mechanism should be cheaper than always 9996 * pausing all CPUs for the duration of the setup, which is what 9997 * the old implementation did. This is particuarly true if we are 9998 * copying a huge chunk of memory around during that window. 9999 * 10000 * The memory barriers are to make sure things stay consistent 10001 * with resume() since it does not hold the HAT lock while 10002 * walking the list of tsb_info structures. 10003 */ 10004 if ((flags & TSB_SWAPIN) != TSB_SWAPIN) { 10005 /* The TSB is either growing or shrinking. */ 10006 sfmmu_invalidate_ctx(sfmmup); 10007 } else { 10008 /* 10009 * It is illegal to swap in TSBs from a process other 10010 * than a process being swapped in. This in turn 10011 * implies we do not have a valid MMU context here 10012 * since a process needs one to resolve translation 10013 * misses. 10014 */ 10015 ASSERT(curthread->t_procp->p_as->a_hat == sfmmup); 10016 } 10017 10018 #ifdef DEBUG 10019 ASSERT(max_mmu_ctxdoms > 0); 10020 10021 /* 10022 * Process should have INVALID_CONTEXT on all MMUs 10023 */ 10024 for (i = 0; i < max_mmu_ctxdoms; i++) { 10025 10026 ASSERT(sfmmup->sfmmu_ctxs[i].cnum == INVALID_CONTEXT); 10027 } 10028 #endif 10029 10030 new_tsbinfo->tsb_next = old_tsbinfo->tsb_next; 10031 membar_stst(); /* strict ordering required */ 10032 if (prevtsb) 10033 prevtsb->tsb_next = new_tsbinfo; 10034 else 10035 sfmmup->sfmmu_tsb = new_tsbinfo; 10036 membar_enter(); /* make sure new TSB globally visible */ 10037 10038 /* 10039 * We need to migrate TSB entries from the old TSB to the new TSB 10040 * if tsb_remap_ttes is set and the TSB is growing. 10041 */ 10042 if (tsb_remap_ttes && ((flags & TSB_GROW) == TSB_GROW)) 10043 sfmmu_copy_tsb(old_tsbinfo, new_tsbinfo); 10044 10045 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY); 10046 10047 /* 10048 * Drop the HAT lock to free our old tsb_info. 10049 */ 10050 sfmmu_hat_exit(hatlockp); 10051 10052 if ((flags & TSB_GROW) == TSB_GROW) { 10053 SFMMU_STAT(sf_tsb_grow); 10054 } else if ((flags & TSB_SHRINK) == TSB_SHRINK) { 10055 SFMMU_STAT(sf_tsb_shrink); 10056 } 10057 10058 sfmmu_tsbinfo_free(old_tsbinfo); 10059 10060 (void) sfmmu_hat_enter(sfmmup); 10061 return (TSB_SUCCESS); 10062 } 10063 10064 /* 10065 * This function will re-program hat pgsz array, and invalidate the 10066 * process' context, forcing the process to switch to another 10067 * context on the next TLB miss, and therefore start using the 10068 * TLB that is reprogrammed for the new page sizes. 10069 */ 10070 void 10071 sfmmu_reprog_pgsz_arr(sfmmu_t *sfmmup, uint8_t *tmp_pgsz) 10072 { 10073 int i; 10074 hatlock_t *hatlockp = NULL; 10075 10076 hatlockp = sfmmu_hat_enter(sfmmup); 10077 /* USIII+-IV+ optimization, requires hat lock */ 10078 if (tmp_pgsz) { 10079 for (i = 0; i < mmu_page_sizes; i++) 10080 sfmmup->sfmmu_pgsz[i] = tmp_pgsz[i]; 10081 } 10082 SFMMU_STAT(sf_tlb_reprog_pgsz); 10083 10084 sfmmu_invalidate_ctx(sfmmup); 10085 10086 sfmmu_hat_exit(hatlockp); 10087 } 10088 10089 /* 10090 * The scd_rttecnt field in the SCD must be updated to take account of the 10091 * regions which it contains. 10092 */ 10093 static void 10094 sfmmu_set_scd_rttecnt(sf_srd_t *srdp, sf_scd_t *scdp) 10095 { 10096 uint_t rid; 10097 uint_t i, j; 10098 ulong_t w; 10099 sf_region_t *rgnp; 10100 10101 ASSERT(srdp != NULL); 10102 10103 for (i = 0; i < SFMMU_HMERGNMAP_WORDS; i++) { 10104 if ((w = scdp->scd_region_map.bitmap[i]) == 0) { 10105 continue; 10106 } 10107 10108 j = 0; 10109 while (w) { 10110 if (!(w & 0x1)) { 10111 j++; 10112 w >>= 1; 10113 continue; 10114 } 10115 rid = (i << BT_ULSHIFT) | j; 10116 j++; 10117 w >>= 1; 10118 10119 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 10120 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 10121 rgnp = srdp->srd_hmergnp[rid]; 10122 ASSERT(rgnp->rgn_refcnt > 0); 10123 ASSERT(rgnp->rgn_id == rid); 10124 10125 scdp->scd_rttecnt[rgnp->rgn_pgszc] += 10126 rgnp->rgn_size >> TTE_PAGE_SHIFT(rgnp->rgn_pgszc); 10127 10128 /* 10129 * Maintain the tsb0 inflation cnt for the regions 10130 * in the SCD. 10131 */ 10132 if (rgnp->rgn_pgszc >= TTE4M) { 10133 scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt += 10134 rgnp->rgn_size >> 10135 (TTE_PAGE_SHIFT(TTE8K) + 2); 10136 } 10137 } 10138 } 10139 } 10140 10141 /* 10142 * This function assumes that there are either four or six supported page 10143 * sizes and at most two programmable TLBs, so we need to decide which 10144 * page sizes are most important and then tell the MMU layer so it 10145 * can adjust the TLB page sizes accordingly (if supported). 10146 * 10147 * If these assumptions change, this function will need to be 10148 * updated to support whatever the new limits are. 10149 * 10150 * The growing flag is nonzero if we are growing the address space, 10151 * and zero if it is shrinking. This allows us to decide whether 10152 * to grow or shrink our TSB, depending upon available memory 10153 * conditions. 10154 */ 10155 static void 10156 sfmmu_check_page_sizes(sfmmu_t *sfmmup, int growing) 10157 { 10158 uint64_t ttecnt[MMU_PAGE_SIZES]; 10159 uint64_t tte8k_cnt, tte4m_cnt; 10160 uint8_t i; 10161 int sectsb_thresh; 10162 10163 /* 10164 * Kernel threads, processes with small address spaces not using 10165 * large pages, and dummy ISM HATs need not apply. 10166 */ 10167 if (sfmmup == ksfmmup || sfmmup->sfmmu_ismhat != NULL) 10168 return; 10169 10170 if (!SFMMU_LGPGS_INUSE(sfmmup) && 10171 sfmmup->sfmmu_ttecnt[TTE8K] <= tsb_rss_factor) 10172 return; 10173 10174 for (i = 0; i < mmu_page_sizes; i++) { 10175 ttecnt[i] = sfmmup->sfmmu_ttecnt[i] + 10176 sfmmup->sfmmu_ismttecnt[i]; 10177 } 10178 10179 /* Check pagesizes in use, and possibly reprogram DTLB. */ 10180 if (&mmu_check_page_sizes) 10181 mmu_check_page_sizes(sfmmup, ttecnt); 10182 10183 /* 10184 * Calculate the number of 8k ttes to represent the span of these 10185 * pages. 10186 */ 10187 tte8k_cnt = ttecnt[TTE8K] + 10188 (ttecnt[TTE64K] << (MMU_PAGESHIFT64K - MMU_PAGESHIFT)) + 10189 (ttecnt[TTE512K] << (MMU_PAGESHIFT512K - MMU_PAGESHIFT)); 10190 if (mmu_page_sizes == max_mmu_page_sizes) { 10191 tte4m_cnt = ttecnt[TTE4M] + 10192 (ttecnt[TTE32M] << (MMU_PAGESHIFT32M - MMU_PAGESHIFT4M)) + 10193 (ttecnt[TTE256M] << (MMU_PAGESHIFT256M - MMU_PAGESHIFT4M)); 10194 } else { 10195 tte4m_cnt = ttecnt[TTE4M]; 10196 } 10197 10198 /* 10199 * Inflate tte8k_cnt to allow for region large page allocation failure. 10200 */ 10201 tte8k_cnt += sfmmup->sfmmu_tsb0_4minflcnt; 10202 10203 /* 10204 * Inflate TSB sizes by a factor of 2 if this process 10205 * uses 4M text pages to minimize extra conflict misses 10206 * in the first TSB since without counting text pages 10207 * 8K TSB may become too small. 10208 * 10209 * Also double the size of the second TSB to minimize 10210 * extra conflict misses due to competition between 4M text pages 10211 * and data pages. 10212 * 10213 * We need to adjust the second TSB allocation threshold by the 10214 * inflation factor, since there is no point in creating a second 10215 * TSB when we know all the mappings can fit in the I/D TLBs. 10216 */ 10217 sectsb_thresh = tsb_sectsb_threshold; 10218 if (sfmmup->sfmmu_flags & HAT_4MTEXT_FLAG) { 10219 tte8k_cnt <<= 1; 10220 tte4m_cnt <<= 1; 10221 sectsb_thresh <<= 1; 10222 } 10223 10224 /* 10225 * Check to see if our TSB is the right size; we may need to 10226 * grow or shrink it. If the process is small, our work is 10227 * finished at this point. 10228 */ 10229 if (tte8k_cnt <= tsb_rss_factor && tte4m_cnt <= sectsb_thresh) { 10230 return; 10231 } 10232 sfmmu_size_tsb(sfmmup, growing, tte8k_cnt, tte4m_cnt, sectsb_thresh); 10233 } 10234 10235 static void 10236 sfmmu_size_tsb(sfmmu_t *sfmmup, int growing, uint64_t tte8k_cnt, 10237 uint64_t tte4m_cnt, int sectsb_thresh) 10238 { 10239 int tsb_bits; 10240 uint_t tsb_szc; 10241 struct tsb_info *tsbinfop; 10242 hatlock_t *hatlockp = NULL; 10243 10244 hatlockp = sfmmu_hat_enter(sfmmup); 10245 ASSERT(hatlockp != NULL); 10246 tsbinfop = sfmmup->sfmmu_tsb; 10247 ASSERT(tsbinfop != NULL); 10248 10249 /* 10250 * If we're growing, select the size based on RSS. If we're 10251 * shrinking, leave some room so we don't have to turn around and 10252 * grow again immediately. 10253 */ 10254 if (growing) 10255 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt); 10256 else 10257 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt << 1); 10258 10259 if (!growing && (tsb_szc < tsbinfop->tsb_szc) && 10260 (tsb_szc >= default_tsb_size) && TSB_OK_SHRINK()) { 10261 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, tsb_szc, 10262 hatlockp, TSB_SHRINK); 10263 } else if (growing && tsb_szc > tsbinfop->tsb_szc && TSB_OK_GROW()) { 10264 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, tsb_szc, 10265 hatlockp, TSB_GROW); 10266 } 10267 tsbinfop = sfmmup->sfmmu_tsb; 10268 10269 /* 10270 * With the TLB and first TSB out of the way, we need to see if 10271 * we need a second TSB for 4M pages. If we managed to reprogram 10272 * the TLB page sizes above, the process will start using this new 10273 * TSB right away; otherwise, it will start using it on the next 10274 * context switch. Either way, it's no big deal so there's no 10275 * synchronization with the trap handlers here unless we grow the 10276 * TSB (in which case it's required to prevent using the old one 10277 * after it's freed). Note: second tsb is required for 32M/256M 10278 * page sizes. 10279 */ 10280 if (tte4m_cnt > sectsb_thresh) { 10281 /* 10282 * If we're growing, select the size based on RSS. If we're 10283 * shrinking, leave some room so we don't have to turn 10284 * around and grow again immediately. 10285 */ 10286 if (growing) 10287 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt); 10288 else 10289 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt << 1); 10290 if (tsbinfop->tsb_next == NULL) { 10291 struct tsb_info *newtsb; 10292 int allocflags = SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)? 10293 0 : TSB_ALLOC; 10294 10295 sfmmu_hat_exit(hatlockp); 10296 10297 /* 10298 * Try to allocate a TSB for 4[32|256]M pages. If we 10299 * can't get the size we want, retry w/a minimum sized 10300 * TSB. If that still didn't work, give up; we can 10301 * still run without one. 10302 */ 10303 tsb_bits = (mmu_page_sizes == max_mmu_page_sizes)? 10304 TSB4M|TSB32M|TSB256M:TSB4M; 10305 if ((sfmmu_tsbinfo_alloc(&newtsb, tsb_szc, tsb_bits, 10306 allocflags, sfmmup)) && 10307 (tsb_szc <= TSB_4M_SZCODE || 10308 sfmmu_tsbinfo_alloc(&newtsb, TSB_4M_SZCODE, 10309 tsb_bits, allocflags, sfmmup)) && 10310 sfmmu_tsbinfo_alloc(&newtsb, TSB_MIN_SZCODE, 10311 tsb_bits, allocflags, sfmmup)) { 10312 return; 10313 } 10314 10315 hatlockp = sfmmu_hat_enter(sfmmup); 10316 10317 sfmmu_invalidate_ctx(sfmmup); 10318 10319 if (sfmmup->sfmmu_tsb->tsb_next == NULL) { 10320 sfmmup->sfmmu_tsb->tsb_next = newtsb; 10321 SFMMU_STAT(sf_tsb_sectsb_create); 10322 sfmmu_hat_exit(hatlockp); 10323 return; 10324 } else { 10325 /* 10326 * It's annoying, but possible for us 10327 * to get here.. we dropped the HAT lock 10328 * because of locking order in the kmem 10329 * allocator, and while we were off getting 10330 * our memory, some other thread decided to 10331 * do us a favor and won the race to get a 10332 * second TSB for this process. Sigh. 10333 */ 10334 sfmmu_hat_exit(hatlockp); 10335 sfmmu_tsbinfo_free(newtsb); 10336 return; 10337 } 10338 } 10339 10340 /* 10341 * We have a second TSB, see if it's big enough. 10342 */ 10343 tsbinfop = tsbinfop->tsb_next; 10344 10345 /* 10346 * Check to see if our second TSB is the right size; 10347 * we may need to grow or shrink it. 10348 * To prevent thrashing (e.g. growing the TSB on a 10349 * subsequent map operation), only try to shrink if 10350 * the TSB reach exceeds twice the virtual address 10351 * space size. 10352 */ 10353 if (!growing && (tsb_szc < tsbinfop->tsb_szc) && 10354 (tsb_szc >= default_tsb_size) && TSB_OK_SHRINK()) { 10355 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, 10356 tsb_szc, hatlockp, TSB_SHRINK); 10357 } else if (growing && tsb_szc > tsbinfop->tsb_szc && 10358 TSB_OK_GROW()) { 10359 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, 10360 tsb_szc, hatlockp, TSB_GROW); 10361 } 10362 } 10363 10364 sfmmu_hat_exit(hatlockp); 10365 } 10366 10367 /* 10368 * Free up a sfmmu 10369 * Since the sfmmu is currently embedded in the hat struct we simply zero 10370 * out our fields and free up the ism map blk list if any. 10371 */ 10372 static void 10373 sfmmu_free_sfmmu(sfmmu_t *sfmmup) 10374 { 10375 ism_blk_t *blkp, *nx_blkp; 10376 #ifdef DEBUG 10377 ism_map_t *map; 10378 int i; 10379 #endif 10380 10381 ASSERT(sfmmup->sfmmu_ttecnt[TTE8K] == 0); 10382 ASSERT(sfmmup->sfmmu_ttecnt[TTE64K] == 0); 10383 ASSERT(sfmmup->sfmmu_ttecnt[TTE512K] == 0); 10384 ASSERT(sfmmup->sfmmu_ttecnt[TTE4M] == 0); 10385 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0); 10386 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0); 10387 ASSERT(SF_RGNMAP_ISNULL(sfmmup)); 10388 10389 sfmmup->sfmmu_free = 0; 10390 sfmmup->sfmmu_ismhat = 0; 10391 10392 blkp = sfmmup->sfmmu_iblk; 10393 sfmmup->sfmmu_iblk = NULL; 10394 10395 while (blkp) { 10396 #ifdef DEBUG 10397 map = blkp->iblk_maps; 10398 for (i = 0; i < ISM_MAP_SLOTS; i++) { 10399 ASSERT(map[i].imap_seg == 0); 10400 ASSERT(map[i].imap_ismhat == NULL); 10401 ASSERT(map[i].imap_ment == NULL); 10402 } 10403 #endif 10404 nx_blkp = blkp->iblk_next; 10405 blkp->iblk_next = NULL; 10406 blkp->iblk_nextpa = (uint64_t)-1; 10407 kmem_cache_free(ism_blk_cache, blkp); 10408 blkp = nx_blkp; 10409 } 10410 } 10411 10412 /* 10413 * Locking primitves accessed by HATLOCK macros 10414 */ 10415 10416 #define SFMMU_SPL_MTX (0x0) 10417 #define SFMMU_ML_MTX (0x1) 10418 10419 #define SFMMU_MLSPL_MTX(type, pg) (((type) == SFMMU_SPL_MTX) ? \ 10420 SPL_HASH(pg) : MLIST_HASH(pg)) 10421 10422 kmutex_t * 10423 sfmmu_page_enter(struct page *pp) 10424 { 10425 return (sfmmu_mlspl_enter(pp, SFMMU_SPL_MTX)); 10426 } 10427 10428 void 10429 sfmmu_page_exit(kmutex_t *spl) 10430 { 10431 mutex_exit(spl); 10432 } 10433 10434 int 10435 sfmmu_page_spl_held(struct page *pp) 10436 { 10437 return (sfmmu_mlspl_held(pp, SFMMU_SPL_MTX)); 10438 } 10439 10440 kmutex_t * 10441 sfmmu_mlist_enter(struct page *pp) 10442 { 10443 return (sfmmu_mlspl_enter(pp, SFMMU_ML_MTX)); 10444 } 10445 10446 void 10447 sfmmu_mlist_exit(kmutex_t *mml) 10448 { 10449 mutex_exit(mml); 10450 } 10451 10452 int 10453 sfmmu_mlist_held(struct page *pp) 10454 { 10455 10456 return (sfmmu_mlspl_held(pp, SFMMU_ML_MTX)); 10457 } 10458 10459 /* 10460 * Common code for sfmmu_mlist_enter() and sfmmu_page_enter(). For 10461 * sfmmu_mlist_enter() case mml_table lock array is used and for 10462 * sfmmu_page_enter() sfmmu_page_lock lock array is used. 10463 * 10464 * The lock is taken on a root page so that it protects an operation on all 10465 * constituent pages of a large page pp belongs to. 10466 * 10467 * The routine takes a lock from the appropriate array. The lock is determined 10468 * by hashing the root page. After taking the lock this routine checks if the 10469 * root page has the same size code that was used to determine the root (i.e 10470 * that root hasn't changed). If root page has the expected p_szc field we 10471 * have the right lock and it's returned to the caller. If root's p_szc 10472 * decreased we release the lock and retry from the beginning. This case can 10473 * happen due to hat_page_demote() decreasing p_szc between our load of p_szc 10474 * value and taking the lock. The number of retries due to p_szc decrease is 10475 * limited by the maximum p_szc value. If p_szc is 0 we return the lock 10476 * determined by hashing pp itself. 10477 * 10478 * If our caller doesn't hold a SE_SHARED or SE_EXCL lock on pp it's also 10479 * possible that p_szc can increase. To increase p_szc a thread has to lock 10480 * all constituent pages EXCL and do hat_pageunload() on all of them. All the 10481 * callers that don't hold a page locked recheck if hmeblk through which pp 10482 * was found still maps this pp. If it doesn't map it anymore returned lock 10483 * is immediately dropped. Therefore if sfmmu_mlspl_enter() hits the case of 10484 * p_szc increase after taking the lock it returns this lock without further 10485 * retries because in this case the caller doesn't care about which lock was 10486 * taken. The caller will drop it right away. 10487 * 10488 * After the routine returns it's guaranteed that hat_page_demote() can't 10489 * change p_szc field of any of constituent pages of a large page pp belongs 10490 * to as long as pp was either locked at least SHARED prior to this call or 10491 * the caller finds that hment that pointed to this pp still references this 10492 * pp (this also assumes that the caller holds hme hash bucket lock so that 10493 * the same pp can't be remapped into the same hmeblk after it was unmapped by 10494 * hat_pageunload()). 10495 */ 10496 static kmutex_t * 10497 sfmmu_mlspl_enter(struct page *pp, int type) 10498 { 10499 kmutex_t *mtx; 10500 uint_t prev_rszc = UINT_MAX; 10501 page_t *rootpp; 10502 uint_t szc; 10503 uint_t rszc; 10504 uint_t pszc = pp->p_szc; 10505 10506 ASSERT(pp != NULL); 10507 10508 again: 10509 if (pszc == 0) { 10510 mtx = SFMMU_MLSPL_MTX(type, pp); 10511 mutex_enter(mtx); 10512 return (mtx); 10513 } 10514 10515 /* The lock lives in the root page */ 10516 rootpp = PP_GROUPLEADER(pp, pszc); 10517 mtx = SFMMU_MLSPL_MTX(type, rootpp); 10518 mutex_enter(mtx); 10519 10520 /* 10521 * Return mml in the following 3 cases: 10522 * 10523 * 1) If pp itself is root since if its p_szc decreased before we took 10524 * the lock pp is still the root of smaller szc page. And if its p_szc 10525 * increased it doesn't matter what lock we return (see comment in 10526 * front of this routine). 10527 * 10528 * 2) If pp's not root but rootpp is the root of a rootpp->p_szc size 10529 * large page we have the right lock since any previous potential 10530 * hat_page_demote() is done demoting from greater than current root's 10531 * p_szc because hat_page_demote() changes root's p_szc last. No 10532 * further hat_page_demote() can start or be in progress since it 10533 * would need the same lock we currently hold. 10534 * 10535 * 3) If rootpp's p_szc increased since previous iteration it doesn't 10536 * matter what lock we return (see comment in front of this routine). 10537 */ 10538 if (pp == rootpp || (rszc = rootpp->p_szc) == pszc || 10539 rszc >= prev_rszc) { 10540 return (mtx); 10541 } 10542 10543 /* 10544 * hat_page_demote() could have decreased root's p_szc. 10545 * In this case pp's p_szc must also be smaller than pszc. 10546 * Retry. 10547 */ 10548 if (rszc < pszc) { 10549 szc = pp->p_szc; 10550 if (szc < pszc) { 10551 mutex_exit(mtx); 10552 pszc = szc; 10553 goto again; 10554 } 10555 /* 10556 * pp's p_szc increased after it was decreased. 10557 * page cannot be mapped. Return current lock. The caller 10558 * will drop it right away. 10559 */ 10560 return (mtx); 10561 } 10562 10563 /* 10564 * root's p_szc is greater than pp's p_szc. 10565 * hat_page_demote() is not done with all pages 10566 * yet. Wait for it to complete. 10567 */ 10568 mutex_exit(mtx); 10569 rootpp = PP_GROUPLEADER(rootpp, rszc); 10570 mtx = SFMMU_MLSPL_MTX(type, rootpp); 10571 mutex_enter(mtx); 10572 mutex_exit(mtx); 10573 prev_rszc = rszc; 10574 goto again; 10575 } 10576 10577 static int 10578 sfmmu_mlspl_held(struct page *pp, int type) 10579 { 10580 kmutex_t *mtx; 10581 10582 ASSERT(pp != NULL); 10583 /* The lock lives in the root page */ 10584 pp = PP_PAGEROOT(pp); 10585 ASSERT(pp != NULL); 10586 10587 mtx = SFMMU_MLSPL_MTX(type, pp); 10588 return (MUTEX_HELD(mtx)); 10589 } 10590 10591 static uint_t 10592 sfmmu_get_free_hblk(struct hme_blk **hmeblkpp, uint_t critical) 10593 { 10594 struct hme_blk *hblkp; 10595 10596 if (freehblkp != NULL) { 10597 mutex_enter(&freehblkp_lock); 10598 if (freehblkp != NULL) { 10599 /* 10600 * If the current thread is owning hblk_reserve OR 10601 * critical request from sfmmu_hblk_steal() 10602 * let it succeed even if freehblkcnt is really low. 10603 */ 10604 if (freehblkcnt <= HBLK_RESERVE_MIN && !critical) { 10605 SFMMU_STAT(sf_get_free_throttle); 10606 mutex_exit(&freehblkp_lock); 10607 return (0); 10608 } 10609 freehblkcnt--; 10610 *hmeblkpp = freehblkp; 10611 hblkp = *hmeblkpp; 10612 freehblkp = hblkp->hblk_next; 10613 mutex_exit(&freehblkp_lock); 10614 hblkp->hblk_next = NULL; 10615 SFMMU_STAT(sf_get_free_success); 10616 return (1); 10617 } 10618 mutex_exit(&freehblkp_lock); 10619 } 10620 SFMMU_STAT(sf_get_free_fail); 10621 return (0); 10622 } 10623 10624 static uint_t 10625 sfmmu_put_free_hblk(struct hme_blk *hmeblkp, uint_t critical) 10626 { 10627 struct hme_blk *hblkp; 10628 10629 /* 10630 * If the current thread is mapping into kernel space, 10631 * let it succede even if freehblkcnt is max 10632 * so that it will avoid freeing it to kmem. 10633 * This will prevent stack overflow due to 10634 * possible recursion since kmem_cache_free() 10635 * might require creation of a slab which 10636 * in turn needs an hmeblk to map that slab; 10637 * let's break this vicious chain at the first 10638 * opportunity. 10639 */ 10640 if (freehblkcnt < HBLK_RESERVE_CNT || critical) { 10641 mutex_enter(&freehblkp_lock); 10642 if (freehblkcnt < HBLK_RESERVE_CNT || critical) { 10643 SFMMU_STAT(sf_put_free_success); 10644 freehblkcnt++; 10645 hmeblkp->hblk_next = freehblkp; 10646 freehblkp = hmeblkp; 10647 mutex_exit(&freehblkp_lock); 10648 return (1); 10649 } 10650 mutex_exit(&freehblkp_lock); 10651 } 10652 10653 /* 10654 * Bring down freehblkcnt to HBLK_RESERVE_CNT. We are here 10655 * only if freehblkcnt is at least HBLK_RESERVE_CNT *and* 10656 * we are not in the process of mapping into kernel space. 10657 */ 10658 ASSERT(!critical); 10659 while (freehblkcnt > HBLK_RESERVE_CNT) { 10660 mutex_enter(&freehblkp_lock); 10661 if (freehblkcnt > HBLK_RESERVE_CNT) { 10662 freehblkcnt--; 10663 hblkp = freehblkp; 10664 freehblkp = hblkp->hblk_next; 10665 mutex_exit(&freehblkp_lock); 10666 ASSERT(get_hblk_cache(hblkp) == sfmmu8_cache); 10667 kmem_cache_free(sfmmu8_cache, hblkp); 10668 continue; 10669 } 10670 mutex_exit(&freehblkp_lock); 10671 } 10672 SFMMU_STAT(sf_put_free_fail); 10673 return (0); 10674 } 10675 10676 static void 10677 sfmmu_hblk_swap(struct hme_blk *new) 10678 { 10679 struct hme_blk *old, *hblkp, *prev; 10680 uint64_t hblkpa, prevpa, newpa; 10681 caddr_t base, vaddr, endaddr; 10682 struct hmehash_bucket *hmebp; 10683 struct sf_hment *osfhme, *nsfhme; 10684 page_t *pp; 10685 kmutex_t *pml; 10686 tte_t tte; 10687 10688 #ifdef DEBUG 10689 hmeblk_tag hblktag; 10690 struct hme_blk *found; 10691 #endif 10692 old = HBLK_RESERVE; 10693 ASSERT(!old->hblk_shared); 10694 10695 /* 10696 * save pa before bcopy clobbers it 10697 */ 10698 newpa = new->hblk_nextpa; 10699 10700 base = (caddr_t)get_hblk_base(old); 10701 endaddr = base + get_hblk_span(old); 10702 10703 /* 10704 * acquire hash bucket lock. 10705 */ 10706 hmebp = sfmmu_tteload_acquire_hashbucket(ksfmmup, base, TTE8K, 10707 SFMMU_INVALID_SHMERID); 10708 10709 /* 10710 * copy contents from old to new 10711 */ 10712 bcopy((void *)old, (void *)new, HME8BLK_SZ); 10713 10714 /* 10715 * add new to hash chain 10716 */ 10717 sfmmu_hblk_hash_add(hmebp, new, newpa); 10718 10719 /* 10720 * search hash chain for hblk_reserve; this needs to be performed 10721 * after adding new, otherwise prevpa and prev won't correspond 10722 * to the hblk which is prior to old in hash chain when we call 10723 * sfmmu_hblk_hash_rm to remove old later. 10724 */ 10725 for (prevpa = 0, prev = NULL, 10726 hblkpa = hmebp->hmeh_nextpa, hblkp = hmebp->hmeblkp; 10727 hblkp != NULL && hblkp != old; 10728 prevpa = hblkpa, prev = hblkp, 10729 hblkpa = hblkp->hblk_nextpa, hblkp = hblkp->hblk_next) 10730 ; 10731 10732 if (hblkp != old) 10733 panic("sfmmu_hblk_swap: hblk_reserve not found"); 10734 10735 /* 10736 * p_mapping list is still pointing to hments in hblk_reserve; 10737 * fix up p_mapping list so that they point to hments in new. 10738 * 10739 * Since all these mappings are created by hblk_reserve_thread 10740 * on the way and it's using at least one of the buffers from each of 10741 * the newly minted slabs, there is no danger of any of these 10742 * mappings getting unloaded by another thread. 10743 * 10744 * tsbmiss could only modify ref/mod bits of hments in old/new. 10745 * Since all of these hments hold mappings established by segkmem 10746 * and mappings in segkmem are setup with HAT_NOSYNC, ref/mod bits 10747 * have no meaning for the mappings in hblk_reserve. hments in 10748 * old and new are identical except for ref/mod bits. 10749 */ 10750 for (vaddr = base; vaddr < endaddr; vaddr += TTEBYTES(TTE8K)) { 10751 10752 HBLKTOHME(osfhme, old, vaddr); 10753 sfmmu_copytte(&osfhme->hme_tte, &tte); 10754 10755 if (TTE_IS_VALID(&tte)) { 10756 if ((pp = osfhme->hme_page) == NULL) 10757 panic("sfmmu_hblk_swap: page not mapped"); 10758 10759 pml = sfmmu_mlist_enter(pp); 10760 10761 if (pp != osfhme->hme_page) 10762 panic("sfmmu_hblk_swap: mapping changed"); 10763 10764 HBLKTOHME(nsfhme, new, vaddr); 10765 10766 HME_ADD(nsfhme, pp); 10767 HME_SUB(osfhme, pp); 10768 10769 sfmmu_mlist_exit(pml); 10770 } 10771 } 10772 10773 /* 10774 * remove old from hash chain 10775 */ 10776 sfmmu_hblk_hash_rm(hmebp, old, prevpa, prev); 10777 10778 #ifdef DEBUG 10779 10780 hblktag.htag_id = ksfmmup; 10781 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 10782 hblktag.htag_bspage = HME_HASH_BSPAGE(base, HME_HASH_SHIFT(TTE8K)); 10783 hblktag.htag_rehash = HME_HASH_REHASH(TTE8K); 10784 HME_HASH_FAST_SEARCH(hmebp, hblktag, found); 10785 10786 if (found != new) 10787 panic("sfmmu_hblk_swap: new hblk not found"); 10788 #endif 10789 10790 SFMMU_HASH_UNLOCK(hmebp); 10791 10792 /* 10793 * Reset hblk_reserve 10794 */ 10795 bzero((void *)old, HME8BLK_SZ); 10796 old->hblk_nextpa = va_to_pa((caddr_t)old); 10797 } 10798 10799 /* 10800 * Grab the mlist mutex for both pages passed in. 10801 * 10802 * low and high will be returned as pointers to the mutexes for these pages. 10803 * low refers to the mutex residing in the lower bin of the mlist hash, while 10804 * high refers to the mutex residing in the higher bin of the mlist hash. This 10805 * is due to the locking order restrictions on the same thread grabbing 10806 * multiple mlist mutexes. The low lock must be acquired before the high lock. 10807 * 10808 * If both pages hash to the same mutex, only grab that single mutex, and 10809 * high will be returned as NULL 10810 * If the pages hash to different bins in the hash, grab the lower addressed 10811 * lock first and then the higher addressed lock in order to follow the locking 10812 * rules involved with the same thread grabbing multiple mlist mutexes. 10813 * low and high will both have non-NULL values. 10814 */ 10815 static void 10816 sfmmu_mlist_reloc_enter(struct page *targ, struct page *repl, 10817 kmutex_t **low, kmutex_t **high) 10818 { 10819 kmutex_t *mml_targ, *mml_repl; 10820 10821 /* 10822 * no need to do the dance around szc as in sfmmu_mlist_enter() 10823 * because this routine is only called by hat_page_relocate() and all 10824 * targ and repl pages are already locked EXCL so szc can't change. 10825 */ 10826 10827 mml_targ = MLIST_HASH(PP_PAGEROOT(targ)); 10828 mml_repl = MLIST_HASH(PP_PAGEROOT(repl)); 10829 10830 if (mml_targ == mml_repl) { 10831 *low = mml_targ; 10832 *high = NULL; 10833 } else { 10834 if (mml_targ < mml_repl) { 10835 *low = mml_targ; 10836 *high = mml_repl; 10837 } else { 10838 *low = mml_repl; 10839 *high = mml_targ; 10840 } 10841 } 10842 10843 mutex_enter(*low); 10844 if (*high) 10845 mutex_enter(*high); 10846 } 10847 10848 static void 10849 sfmmu_mlist_reloc_exit(kmutex_t *low, kmutex_t *high) 10850 { 10851 if (high) 10852 mutex_exit(high); 10853 mutex_exit(low); 10854 } 10855 10856 static hatlock_t * 10857 sfmmu_hat_enter(sfmmu_t *sfmmup) 10858 { 10859 hatlock_t *hatlockp; 10860 10861 if (sfmmup != ksfmmup) { 10862 hatlockp = TSB_HASH(sfmmup); 10863 mutex_enter(HATLOCK_MUTEXP(hatlockp)); 10864 return (hatlockp); 10865 } 10866 return (NULL); 10867 } 10868 10869 static hatlock_t * 10870 sfmmu_hat_tryenter(sfmmu_t *sfmmup) 10871 { 10872 hatlock_t *hatlockp; 10873 10874 if (sfmmup != ksfmmup) { 10875 hatlockp = TSB_HASH(sfmmup); 10876 if (mutex_tryenter(HATLOCK_MUTEXP(hatlockp)) == 0) 10877 return (NULL); 10878 return (hatlockp); 10879 } 10880 return (NULL); 10881 } 10882 10883 static void 10884 sfmmu_hat_exit(hatlock_t *hatlockp) 10885 { 10886 if (hatlockp != NULL) 10887 mutex_exit(HATLOCK_MUTEXP(hatlockp)); 10888 } 10889 10890 static void 10891 sfmmu_hat_lock_all(void) 10892 { 10893 int i; 10894 for (i = 0; i < SFMMU_NUM_LOCK; i++) 10895 mutex_enter(HATLOCK_MUTEXP(&hat_lock[i])); 10896 } 10897 10898 static void 10899 sfmmu_hat_unlock_all(void) 10900 { 10901 int i; 10902 for (i = SFMMU_NUM_LOCK - 1; i >= 0; i--) 10903 mutex_exit(HATLOCK_MUTEXP(&hat_lock[i])); 10904 } 10905 10906 int 10907 sfmmu_hat_lock_held(sfmmu_t *sfmmup) 10908 { 10909 ASSERT(sfmmup != ksfmmup); 10910 return (MUTEX_HELD(HATLOCK_MUTEXP(TSB_HASH(sfmmup)))); 10911 } 10912 10913 /* 10914 * Locking primitives to provide consistency between ISM unmap 10915 * and other operations. Since ISM unmap can take a long time, we 10916 * use HAT_ISMBUSY flag (protected by the hatlock) to avoid creating 10917 * contention on the hatlock buckets while ISM segments are being 10918 * unmapped. The tradeoff is that the flags don't prevent priority 10919 * inversion from occurring, so we must request kernel priority in 10920 * case we have to sleep to keep from getting buried while holding 10921 * the HAT_ISMBUSY flag set, which in turn could block other kernel 10922 * threads from running (for example, in sfmmu_uvatopfn()). 10923 */ 10924 static void 10925 sfmmu_ismhat_enter(sfmmu_t *sfmmup, int hatlock_held) 10926 { 10927 hatlock_t *hatlockp; 10928 10929 THREAD_KPRI_REQUEST(); 10930 if (!hatlock_held) 10931 hatlockp = sfmmu_hat_enter(sfmmup); 10932 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)) 10933 cv_wait(&sfmmup->sfmmu_tsb_cv, HATLOCK_MUTEXP(hatlockp)); 10934 SFMMU_FLAGS_SET(sfmmup, HAT_ISMBUSY); 10935 if (!hatlock_held) 10936 sfmmu_hat_exit(hatlockp); 10937 } 10938 10939 static void 10940 sfmmu_ismhat_exit(sfmmu_t *sfmmup, int hatlock_held) 10941 { 10942 hatlock_t *hatlockp; 10943 10944 if (!hatlock_held) 10945 hatlockp = sfmmu_hat_enter(sfmmup); 10946 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)); 10947 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ISMBUSY); 10948 cv_broadcast(&sfmmup->sfmmu_tsb_cv); 10949 if (!hatlock_held) 10950 sfmmu_hat_exit(hatlockp); 10951 THREAD_KPRI_RELEASE(); 10952 } 10953 10954 /* 10955 * 10956 * Algorithm: 10957 * 10958 * (1) if segkmem is not ready, allocate hblk from an array of pre-alloc'ed 10959 * hblks. 10960 * 10961 * (2) if we are allocating an hblk for mapping a slab in sfmmu_cache, 10962 * 10963 * (a) try to return an hblk from reserve pool of free hblks; 10964 * (b) if the reserve pool is empty, acquire hblk_reserve_lock 10965 * and return hblk_reserve. 10966 * 10967 * (3) call kmem_cache_alloc() to allocate hblk; 10968 * 10969 * (a) if hblk_reserve_lock is held by the current thread, 10970 * atomically replace hblk_reserve by the hblk that is 10971 * returned by kmem_cache_alloc; release hblk_reserve_lock 10972 * and call kmem_cache_alloc() again. 10973 * (b) if reserve pool is not full, add the hblk that is 10974 * returned by kmem_cache_alloc to reserve pool and 10975 * call kmem_cache_alloc again. 10976 * 10977 */ 10978 static struct hme_blk * 10979 sfmmu_hblk_alloc(sfmmu_t *sfmmup, caddr_t vaddr, 10980 struct hmehash_bucket *hmebp, uint_t size, hmeblk_tag hblktag, 10981 uint_t flags, uint_t rid) 10982 { 10983 struct hme_blk *hmeblkp = NULL; 10984 struct hme_blk *newhblkp; 10985 struct hme_blk *shw_hblkp = NULL; 10986 struct kmem_cache *sfmmu_cache = NULL; 10987 uint64_t hblkpa; 10988 ulong_t index; 10989 uint_t owner; /* set to 1 if using hblk_reserve */ 10990 uint_t forcefree; 10991 int sleep; 10992 sf_srd_t *srdp; 10993 sf_region_t *rgnp; 10994 10995 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 10996 ASSERT(hblktag.htag_rid == rid); 10997 SFMMU_VALIDATE_HMERID(sfmmup, rid, vaddr, TTEBYTES(size)); 10998 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || 10999 IS_P2ALIGNED(vaddr, TTEBYTES(size))); 11000 11001 /* 11002 * If segkmem is not created yet, allocate from static hmeblks 11003 * created at the end of startup_modules(). See the block comment 11004 * in startup_modules() describing how we estimate the number of 11005 * static hmeblks that will be needed during re-map. 11006 */ 11007 if (!hblk_alloc_dynamic) { 11008 11009 ASSERT(!SFMMU_IS_SHMERID_VALID(rid)); 11010 11011 if (size == TTE8K) { 11012 index = nucleus_hblk8.index; 11013 if (index >= nucleus_hblk8.len) { 11014 /* 11015 * If we panic here, see startup_modules() to 11016 * make sure that we are calculating the 11017 * number of hblk8's that we need correctly. 11018 */ 11019 prom_panic("no nucleus hblk8 to allocate"); 11020 } 11021 hmeblkp = 11022 (struct hme_blk *)&nucleus_hblk8.list[index]; 11023 nucleus_hblk8.index++; 11024 SFMMU_STAT(sf_hblk8_nalloc); 11025 } else { 11026 index = nucleus_hblk1.index; 11027 if (nucleus_hblk1.index >= nucleus_hblk1.len) { 11028 /* 11029 * If we panic here, see startup_modules(). 11030 * Most likely you need to update the 11031 * calculation of the number of hblk1 elements 11032 * that the kernel needs to boot. 11033 */ 11034 prom_panic("no nucleus hblk1 to allocate"); 11035 } 11036 hmeblkp = 11037 (struct hme_blk *)&nucleus_hblk1.list[index]; 11038 nucleus_hblk1.index++; 11039 SFMMU_STAT(sf_hblk1_nalloc); 11040 } 11041 11042 goto hblk_init; 11043 } 11044 11045 SFMMU_HASH_UNLOCK(hmebp); 11046 11047 if (sfmmup != KHATID && !SFMMU_IS_SHMERID_VALID(rid)) { 11048 if (mmu_page_sizes == max_mmu_page_sizes) { 11049 if (size < TTE256M) 11050 shw_hblkp = sfmmu_shadow_hcreate(sfmmup, vaddr, 11051 size, flags); 11052 } else { 11053 if (size < TTE4M) 11054 shw_hblkp = sfmmu_shadow_hcreate(sfmmup, vaddr, 11055 size, flags); 11056 } 11057 } else if (SFMMU_IS_SHMERID_VALID(rid)) { 11058 /* 11059 * Shared hmes use per region bitmaps in rgn_hmeflag 11060 * rather than shadow hmeblks to keep track of the 11061 * mapping sizes which have been allocated for the region. 11062 * Here we cleanup old invalid hmeblks with this rid, 11063 * which may be left around by pageunload(). 11064 */ 11065 int ttesz; 11066 caddr_t va; 11067 caddr_t eva = vaddr + TTEBYTES(size); 11068 11069 ASSERT(sfmmup != KHATID); 11070 11071 srdp = sfmmup->sfmmu_srdp; 11072 ASSERT(srdp != NULL && srdp->srd_refcnt != 0); 11073 rgnp = srdp->srd_hmergnp[rid]; 11074 ASSERT(rgnp != NULL && rgnp->rgn_id == rid); 11075 ASSERT(rgnp->rgn_refcnt != 0); 11076 ASSERT(size <= rgnp->rgn_pgszc); 11077 11078 ttesz = HBLK_MIN_TTESZ; 11079 do { 11080 if (!(rgnp->rgn_hmeflags & (0x1 << ttesz))) { 11081 continue; 11082 } 11083 11084 if (ttesz > size && ttesz != HBLK_MIN_TTESZ) { 11085 sfmmu_cleanup_rhblk(srdp, vaddr, rid, ttesz); 11086 } else if (ttesz < size) { 11087 for (va = vaddr; va < eva; 11088 va += TTEBYTES(ttesz)) { 11089 sfmmu_cleanup_rhblk(srdp, va, rid, 11090 ttesz); 11091 } 11092 } 11093 } while (++ttesz <= rgnp->rgn_pgszc); 11094 } 11095 11096 fill_hblk: 11097 owner = (hblk_reserve_thread == curthread) ? 1 : 0; 11098 11099 if (owner && size == TTE8K) { 11100 11101 ASSERT(!SFMMU_IS_SHMERID_VALID(rid)); 11102 /* 11103 * We are really in a tight spot. We already own 11104 * hblk_reserve and we need another hblk. In anticipation 11105 * of this kind of scenario, we specifically set aside 11106 * HBLK_RESERVE_MIN number of hblks to be used exclusively 11107 * by owner of hblk_reserve. 11108 */ 11109 SFMMU_STAT(sf_hblk_recurse_cnt); 11110 11111 if (!sfmmu_get_free_hblk(&hmeblkp, 1)) 11112 panic("sfmmu_hblk_alloc: reserve list is empty"); 11113 11114 goto hblk_verify; 11115 } 11116 11117 ASSERT(!owner); 11118 11119 if ((flags & HAT_NO_KALLOC) == 0) { 11120 11121 sfmmu_cache = ((size == TTE8K) ? sfmmu8_cache : sfmmu1_cache); 11122 sleep = ((sfmmup == KHATID) ? KM_NOSLEEP : KM_SLEEP); 11123 11124 if ((hmeblkp = kmem_cache_alloc(sfmmu_cache, sleep)) == NULL) { 11125 hmeblkp = sfmmu_hblk_steal(size); 11126 } else { 11127 /* 11128 * if we are the owner of hblk_reserve, 11129 * swap hblk_reserve with hmeblkp and 11130 * start a fresh life. Hope things go 11131 * better this time. 11132 */ 11133 if (hblk_reserve_thread == curthread) { 11134 ASSERT(sfmmu_cache == sfmmu8_cache); 11135 sfmmu_hblk_swap(hmeblkp); 11136 hblk_reserve_thread = NULL; 11137 mutex_exit(&hblk_reserve_lock); 11138 goto fill_hblk; 11139 } 11140 /* 11141 * let's donate this hblk to our reserve list if 11142 * we are not mapping kernel range 11143 */ 11144 if (size == TTE8K && sfmmup != KHATID) 11145 if (sfmmu_put_free_hblk(hmeblkp, 0)) 11146 goto fill_hblk; 11147 } 11148 } else { 11149 /* 11150 * We are here to map the slab in sfmmu8_cache; let's 11151 * check if we could tap our reserve list; if successful, 11152 * this will avoid the pain of going thru sfmmu_hblk_swap 11153 */ 11154 SFMMU_STAT(sf_hblk_slab_cnt); 11155 if (!sfmmu_get_free_hblk(&hmeblkp, 0)) { 11156 /* 11157 * let's start hblk_reserve dance 11158 */ 11159 SFMMU_STAT(sf_hblk_reserve_cnt); 11160 owner = 1; 11161 mutex_enter(&hblk_reserve_lock); 11162 hmeblkp = HBLK_RESERVE; 11163 hblk_reserve_thread = curthread; 11164 } 11165 } 11166 11167 hblk_verify: 11168 ASSERT(hmeblkp != NULL); 11169 set_hblk_sz(hmeblkp, size); 11170 ASSERT(hmeblkp->hblk_nextpa == va_to_pa((caddr_t)hmeblkp)); 11171 SFMMU_HASH_LOCK(hmebp); 11172 HME_HASH_FAST_SEARCH(hmebp, hblktag, newhblkp); 11173 if (newhblkp != NULL) { 11174 SFMMU_HASH_UNLOCK(hmebp); 11175 if (hmeblkp != HBLK_RESERVE) { 11176 /* 11177 * This is really tricky! 11178 * 11179 * vmem_alloc(vmem_seg_arena) 11180 * vmem_alloc(vmem_internal_arena) 11181 * segkmem_alloc(heap_arena) 11182 * vmem_alloc(heap_arena) 11183 * page_create() 11184 * hat_memload() 11185 * kmem_cache_free() 11186 * kmem_cache_alloc() 11187 * kmem_slab_create() 11188 * vmem_alloc(kmem_internal_arena) 11189 * segkmem_alloc(heap_arena) 11190 * vmem_alloc(heap_arena) 11191 * page_create() 11192 * hat_memload() 11193 * kmem_cache_free() 11194 * ... 11195 * 11196 * Thus, hat_memload() could call kmem_cache_free 11197 * for enough number of times that we could easily 11198 * hit the bottom of the stack or run out of reserve 11199 * list of vmem_seg structs. So, we must donate 11200 * this hblk to reserve list if it's allocated 11201 * from sfmmu8_cache *and* mapping kernel range. 11202 * We don't need to worry about freeing hmeblk1's 11203 * to kmem since they don't map any kmem slabs. 11204 * 11205 * Note: When segkmem supports largepages, we must 11206 * free hmeblk1's to reserve list as well. 11207 */ 11208 forcefree = (sfmmup == KHATID) ? 1 : 0; 11209 if (size == TTE8K && 11210 sfmmu_put_free_hblk(hmeblkp, forcefree)) { 11211 goto re_verify; 11212 } 11213 ASSERT(sfmmup != KHATID); 11214 kmem_cache_free(get_hblk_cache(hmeblkp), hmeblkp); 11215 } else { 11216 /* 11217 * Hey! we don't need hblk_reserve any more. 11218 */ 11219 ASSERT(owner); 11220 hblk_reserve_thread = NULL; 11221 mutex_exit(&hblk_reserve_lock); 11222 owner = 0; 11223 } 11224 re_verify: 11225 /* 11226 * let's check if the goodies are still present 11227 */ 11228 SFMMU_HASH_LOCK(hmebp); 11229 HME_HASH_FAST_SEARCH(hmebp, hblktag, newhblkp); 11230 if (newhblkp != NULL) { 11231 /* 11232 * return newhblkp if it's not hblk_reserve; 11233 * if newhblkp is hblk_reserve, return it 11234 * _only if_ we are the owner of hblk_reserve. 11235 */ 11236 if (newhblkp != HBLK_RESERVE || owner) { 11237 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || 11238 newhblkp->hblk_shared); 11239 ASSERT(SFMMU_IS_SHMERID_VALID(rid) || 11240 !newhblkp->hblk_shared); 11241 return (newhblkp); 11242 } else { 11243 /* 11244 * we just hit hblk_reserve in the hash and 11245 * we are not the owner of that; 11246 * 11247 * block until hblk_reserve_thread completes 11248 * swapping hblk_reserve and try the dance 11249 * once again. 11250 */ 11251 SFMMU_HASH_UNLOCK(hmebp); 11252 mutex_enter(&hblk_reserve_lock); 11253 mutex_exit(&hblk_reserve_lock); 11254 SFMMU_STAT(sf_hblk_reserve_hit); 11255 goto fill_hblk; 11256 } 11257 } else { 11258 /* 11259 * it's no more! try the dance once again. 11260 */ 11261 SFMMU_HASH_UNLOCK(hmebp); 11262 goto fill_hblk; 11263 } 11264 } 11265 11266 hblk_init: 11267 if (SFMMU_IS_SHMERID_VALID(rid)) { 11268 uint16_t tteflag = 0x1 << 11269 ((size < HBLK_MIN_TTESZ) ? HBLK_MIN_TTESZ : size); 11270 11271 if (!(rgnp->rgn_hmeflags & tteflag)) { 11272 atomic_or_16(&rgnp->rgn_hmeflags, tteflag); 11273 } 11274 hmeblkp->hblk_shared = 1; 11275 } else { 11276 hmeblkp->hblk_shared = 0; 11277 } 11278 set_hblk_sz(hmeblkp, size); 11279 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 11280 hmeblkp->hblk_next = (struct hme_blk *)NULL; 11281 hmeblkp->hblk_tag = hblktag; 11282 hmeblkp->hblk_shadow = shw_hblkp; 11283 hblkpa = hmeblkp->hblk_nextpa; 11284 hmeblkp->hblk_nextpa = 0; 11285 11286 ASSERT(get_hblk_ttesz(hmeblkp) == size); 11287 ASSERT(get_hblk_span(hmeblkp) == HMEBLK_SPAN(size)); 11288 ASSERT(hmeblkp->hblk_hmecnt == 0); 11289 ASSERT(hmeblkp->hblk_vcnt == 0); 11290 ASSERT(hmeblkp->hblk_lckcnt == 0); 11291 ASSERT(hblkpa == va_to_pa((caddr_t)hmeblkp)); 11292 sfmmu_hblk_hash_add(hmebp, hmeblkp, hblkpa); 11293 return (hmeblkp); 11294 } 11295 11296 /* 11297 * This function performs any cleanup required on the hme_blk 11298 * and returns it to the free list. 11299 */ 11300 /* ARGSUSED */ 11301 static void 11302 sfmmu_hblk_free(struct hmehash_bucket *hmebp, struct hme_blk *hmeblkp, 11303 uint64_t hblkpa, struct hme_blk **listp) 11304 { 11305 int shw_size, vshift; 11306 struct hme_blk *shw_hblkp; 11307 uint_t shw_mask, newshw_mask; 11308 caddr_t vaddr; 11309 int size; 11310 uint_t critical; 11311 11312 ASSERT(hmeblkp); 11313 ASSERT(!hmeblkp->hblk_hmecnt); 11314 ASSERT(!hmeblkp->hblk_vcnt); 11315 ASSERT(!hmeblkp->hblk_lckcnt); 11316 ASSERT(hblkpa == va_to_pa((caddr_t)hmeblkp)); 11317 ASSERT(hmeblkp != (struct hme_blk *)hblk_reserve); 11318 11319 critical = (hblktosfmmu(hmeblkp) == KHATID) ? 1 : 0; 11320 11321 size = get_hblk_ttesz(hmeblkp); 11322 shw_hblkp = hmeblkp->hblk_shadow; 11323 if (shw_hblkp) { 11324 ASSERT(hblktosfmmu(hmeblkp) != KHATID); 11325 ASSERT(!hmeblkp->hblk_shared); 11326 if (mmu_page_sizes == max_mmu_page_sizes) { 11327 ASSERT(size < TTE256M); 11328 } else { 11329 ASSERT(size < TTE4M); 11330 } 11331 11332 shw_size = get_hblk_ttesz(shw_hblkp); 11333 vaddr = (caddr_t)get_hblk_base(hmeblkp); 11334 vshift = vaddr_to_vshift(shw_hblkp->hblk_tag, vaddr, shw_size); 11335 ASSERT(vshift < 8); 11336 /* 11337 * Atomically clear shadow mask bit 11338 */ 11339 do { 11340 shw_mask = shw_hblkp->hblk_shw_mask; 11341 ASSERT(shw_mask & (1 << vshift)); 11342 newshw_mask = shw_mask & ~(1 << vshift); 11343 newshw_mask = cas32(&shw_hblkp->hblk_shw_mask, 11344 shw_mask, newshw_mask); 11345 } while (newshw_mask != shw_mask); 11346 hmeblkp->hblk_shadow = NULL; 11347 } 11348 hmeblkp->hblk_next = NULL; 11349 hmeblkp->hblk_nextpa = hblkpa; 11350 hmeblkp->hblk_shw_bit = 0; 11351 11352 if (hmeblkp->hblk_shared) { 11353 sf_srd_t *srdp; 11354 sf_region_t *rgnp; 11355 uint_t rid; 11356 11357 srdp = hblktosrd(hmeblkp); 11358 ASSERT(srdp != NULL && srdp->srd_refcnt != 0); 11359 rid = hmeblkp->hblk_tag.htag_rid; 11360 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 11361 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 11362 rgnp = srdp->srd_hmergnp[rid]; 11363 ASSERT(rgnp != NULL); 11364 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid); 11365 hmeblkp->hblk_shared = 0; 11366 } 11367 11368 if (hmeblkp->hblk_nuc_bit == 0) { 11369 11370 if (size == TTE8K && sfmmu_put_free_hblk(hmeblkp, critical)) 11371 return; 11372 11373 hmeblkp->hblk_next = *listp; 11374 *listp = hmeblkp; 11375 } 11376 } 11377 11378 static void 11379 sfmmu_hblks_list_purge(struct hme_blk **listp) 11380 { 11381 struct hme_blk *hmeblkp; 11382 11383 while ((hmeblkp = *listp) != NULL) { 11384 *listp = hmeblkp->hblk_next; 11385 kmem_cache_free(get_hblk_cache(hmeblkp), hmeblkp); 11386 } 11387 } 11388 11389 #define BUCKETS_TO_SEARCH_BEFORE_UNLOAD 30 11390 #define SFMMU_HBLK_STEAL_THRESHOLD 5 11391 11392 static uint_t sfmmu_hblk_steal_twice; 11393 static uint_t sfmmu_hblk_steal_count, sfmmu_hblk_steal_unload_count; 11394 11395 /* 11396 * Steal a hmeblk from user or kernel hme hash lists. 11397 * For 8K tte grab one from reserve pool (freehblkp) before proceeding to 11398 * steal and if we fail to steal after SFMMU_HBLK_STEAL_THRESHOLD attempts 11399 * tap into critical reserve of freehblkp. 11400 * Note: We remain looping in this routine until we find one. 11401 */ 11402 static struct hme_blk * 11403 sfmmu_hblk_steal(int size) 11404 { 11405 static struct hmehash_bucket *uhmehash_steal_hand = NULL; 11406 struct hmehash_bucket *hmebp; 11407 struct hme_blk *hmeblkp = NULL, *pr_hblk; 11408 uint64_t hblkpa, prevpa; 11409 int i; 11410 uint_t loop_cnt = 0, critical; 11411 11412 for (;;) { 11413 if (size == TTE8K) { 11414 critical = 11415 (++loop_cnt > SFMMU_HBLK_STEAL_THRESHOLD) ? 1 : 0; 11416 if (sfmmu_get_free_hblk(&hmeblkp, critical)) 11417 return (hmeblkp); 11418 } 11419 11420 hmebp = (uhmehash_steal_hand == NULL) ? uhme_hash : 11421 uhmehash_steal_hand; 11422 ASSERT(hmebp >= uhme_hash && hmebp <= &uhme_hash[UHMEHASH_SZ]); 11423 11424 for (i = 0; hmeblkp == NULL && i <= UHMEHASH_SZ + 11425 BUCKETS_TO_SEARCH_BEFORE_UNLOAD; i++) { 11426 SFMMU_HASH_LOCK(hmebp); 11427 hmeblkp = hmebp->hmeblkp; 11428 hblkpa = hmebp->hmeh_nextpa; 11429 prevpa = 0; 11430 pr_hblk = NULL; 11431 while (hmeblkp) { 11432 /* 11433 * check if it is a hmeblk that is not locked 11434 * and not shared. skip shadow hmeblks with 11435 * shadow_mask set i.e valid count non zero. 11436 */ 11437 if ((get_hblk_ttesz(hmeblkp) == size) && 11438 (hmeblkp->hblk_shw_bit == 0 || 11439 hmeblkp->hblk_vcnt == 0) && 11440 (hmeblkp->hblk_lckcnt == 0)) { 11441 /* 11442 * there is a high probability that we 11443 * will find a free one. search some 11444 * buckets for a free hmeblk initially 11445 * before unloading a valid hmeblk. 11446 */ 11447 if ((hmeblkp->hblk_vcnt == 0 && 11448 hmeblkp->hblk_hmecnt == 0) || (i >= 11449 BUCKETS_TO_SEARCH_BEFORE_UNLOAD)) { 11450 if (sfmmu_steal_this_hblk(hmebp, 11451 hmeblkp, hblkpa, prevpa, 11452 pr_hblk)) { 11453 /* 11454 * Hblk is unloaded 11455 * successfully 11456 */ 11457 break; 11458 } 11459 } 11460 } 11461 pr_hblk = hmeblkp; 11462 prevpa = hblkpa; 11463 hblkpa = hmeblkp->hblk_nextpa; 11464 hmeblkp = hmeblkp->hblk_next; 11465 } 11466 11467 SFMMU_HASH_UNLOCK(hmebp); 11468 if (hmebp++ == &uhme_hash[UHMEHASH_SZ]) 11469 hmebp = uhme_hash; 11470 } 11471 uhmehash_steal_hand = hmebp; 11472 11473 if (hmeblkp != NULL) 11474 break; 11475 11476 /* 11477 * in the worst case, look for a free one in the kernel 11478 * hash table. 11479 */ 11480 for (i = 0, hmebp = khme_hash; i <= KHMEHASH_SZ; i++) { 11481 SFMMU_HASH_LOCK(hmebp); 11482 hmeblkp = hmebp->hmeblkp; 11483 hblkpa = hmebp->hmeh_nextpa; 11484 prevpa = 0; 11485 pr_hblk = NULL; 11486 while (hmeblkp) { 11487 /* 11488 * check if it is free hmeblk 11489 */ 11490 if ((get_hblk_ttesz(hmeblkp) == size) && 11491 (hmeblkp->hblk_lckcnt == 0) && 11492 (hmeblkp->hblk_vcnt == 0) && 11493 (hmeblkp->hblk_hmecnt == 0)) { 11494 if (sfmmu_steal_this_hblk(hmebp, 11495 hmeblkp, hblkpa, prevpa, pr_hblk)) { 11496 break; 11497 } else { 11498 /* 11499 * Cannot fail since we have 11500 * hash lock. 11501 */ 11502 panic("fail to steal?"); 11503 } 11504 } 11505 11506 pr_hblk = hmeblkp; 11507 prevpa = hblkpa; 11508 hblkpa = hmeblkp->hblk_nextpa; 11509 hmeblkp = hmeblkp->hblk_next; 11510 } 11511 11512 SFMMU_HASH_UNLOCK(hmebp); 11513 if (hmebp++ == &khme_hash[KHMEHASH_SZ]) 11514 hmebp = khme_hash; 11515 } 11516 11517 if (hmeblkp != NULL) 11518 break; 11519 sfmmu_hblk_steal_twice++; 11520 } 11521 return (hmeblkp); 11522 } 11523 11524 /* 11525 * This routine does real work to prepare a hblk to be "stolen" by 11526 * unloading the mappings, updating shadow counts .... 11527 * It returns 1 if the block is ready to be reused (stolen), or 0 11528 * means the block cannot be stolen yet- pageunload is still working 11529 * on this hblk. 11530 */ 11531 static int 11532 sfmmu_steal_this_hblk(struct hmehash_bucket *hmebp, struct hme_blk *hmeblkp, 11533 uint64_t hblkpa, uint64_t prevpa, struct hme_blk *pr_hblk) 11534 { 11535 int shw_size, vshift; 11536 struct hme_blk *shw_hblkp; 11537 caddr_t vaddr; 11538 uint_t shw_mask, newshw_mask; 11539 11540 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 11541 11542 /* 11543 * check if the hmeblk is free, unload if necessary 11544 */ 11545 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) { 11546 sfmmu_t *sfmmup; 11547 demap_range_t dmr; 11548 11549 sfmmup = hblktosfmmu(hmeblkp); 11550 if (hmeblkp->hblk_shared || sfmmup->sfmmu_ismhat) { 11551 return (0); 11552 } 11553 DEMAP_RANGE_INIT(sfmmup, &dmr); 11554 (void) sfmmu_hblk_unload(sfmmup, hmeblkp, 11555 (caddr_t)get_hblk_base(hmeblkp), 11556 get_hblk_endaddr(hmeblkp), &dmr, HAT_UNLOAD); 11557 DEMAP_RANGE_FLUSH(&dmr); 11558 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) { 11559 /* 11560 * Pageunload is working on the same hblk. 11561 */ 11562 return (0); 11563 } 11564 11565 sfmmu_hblk_steal_unload_count++; 11566 } 11567 11568 ASSERT(hmeblkp->hblk_lckcnt == 0); 11569 ASSERT(hmeblkp->hblk_vcnt == 0 && hmeblkp->hblk_hmecnt == 0); 11570 11571 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, pr_hblk); 11572 hmeblkp->hblk_nextpa = hblkpa; 11573 11574 shw_hblkp = hmeblkp->hblk_shadow; 11575 if (shw_hblkp) { 11576 ASSERT(!hmeblkp->hblk_shared); 11577 shw_size = get_hblk_ttesz(shw_hblkp); 11578 vaddr = (caddr_t)get_hblk_base(hmeblkp); 11579 vshift = vaddr_to_vshift(shw_hblkp->hblk_tag, vaddr, shw_size); 11580 ASSERT(vshift < 8); 11581 /* 11582 * Atomically clear shadow mask bit 11583 */ 11584 do { 11585 shw_mask = shw_hblkp->hblk_shw_mask; 11586 ASSERT(shw_mask & (1 << vshift)); 11587 newshw_mask = shw_mask & ~(1 << vshift); 11588 newshw_mask = cas32(&shw_hblkp->hblk_shw_mask, 11589 shw_mask, newshw_mask); 11590 } while (newshw_mask != shw_mask); 11591 hmeblkp->hblk_shadow = NULL; 11592 } 11593 11594 /* 11595 * remove shadow bit if we are stealing an unused shadow hmeblk. 11596 * sfmmu_hblk_alloc needs it that way, will set shadow bit later if 11597 * we are indeed allocating a shadow hmeblk. 11598 */ 11599 hmeblkp->hblk_shw_bit = 0; 11600 11601 if (hmeblkp->hblk_shared) { 11602 sf_srd_t *srdp; 11603 sf_region_t *rgnp; 11604 uint_t rid; 11605 11606 srdp = hblktosrd(hmeblkp); 11607 ASSERT(srdp != NULL && srdp->srd_refcnt != 0); 11608 rid = hmeblkp->hblk_tag.htag_rid; 11609 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 11610 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 11611 rgnp = srdp->srd_hmergnp[rid]; 11612 ASSERT(rgnp != NULL); 11613 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid); 11614 hmeblkp->hblk_shared = 0; 11615 } 11616 11617 sfmmu_hblk_steal_count++; 11618 SFMMU_STAT(sf_steal_count); 11619 11620 return (1); 11621 } 11622 11623 struct hme_blk * 11624 sfmmu_hmetohblk(struct sf_hment *sfhme) 11625 { 11626 struct hme_blk *hmeblkp; 11627 struct sf_hment *sfhme0; 11628 struct hme_blk *hblk_dummy = 0; 11629 11630 /* 11631 * No dummy sf_hments, please. 11632 */ 11633 ASSERT(sfhme->hme_tte.ll != 0); 11634 11635 sfhme0 = sfhme - sfhme->hme_tte.tte_hmenum; 11636 hmeblkp = (struct hme_blk *)((uintptr_t)sfhme0 - 11637 (uintptr_t)&hblk_dummy->hblk_hme[0]); 11638 11639 return (hmeblkp); 11640 } 11641 11642 /* 11643 * On swapin, get appropriately sized TSB(s) and clear the HAT_SWAPPED flag. 11644 * If we can't get appropriately sized TSB(s), try for 8K TSB(s) using 11645 * KM_SLEEP allocation. 11646 * 11647 * Return 0 on success, -1 otherwise. 11648 */ 11649 static void 11650 sfmmu_tsb_swapin(sfmmu_t *sfmmup, hatlock_t *hatlockp) 11651 { 11652 struct tsb_info *tsbinfop, *next; 11653 tsb_replace_rc_t rc; 11654 boolean_t gotfirst = B_FALSE; 11655 11656 ASSERT(sfmmup != ksfmmup); 11657 ASSERT(sfmmu_hat_lock_held(sfmmup)); 11658 11659 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPIN)) { 11660 cv_wait(&sfmmup->sfmmu_tsb_cv, HATLOCK_MUTEXP(hatlockp)); 11661 } 11662 11663 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) { 11664 SFMMU_FLAGS_SET(sfmmup, HAT_SWAPIN); 11665 } else { 11666 return; 11667 } 11668 11669 ASSERT(sfmmup->sfmmu_tsb != NULL); 11670 11671 /* 11672 * Loop over all tsbinfo's replacing them with ones that actually have 11673 * a TSB. If any of the replacements ever fail, bail out of the loop. 11674 */ 11675 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL; tsbinfop = next) { 11676 ASSERT(tsbinfop->tsb_flags & TSB_SWAPPED); 11677 next = tsbinfop->tsb_next; 11678 rc = sfmmu_replace_tsb(sfmmup, tsbinfop, tsbinfop->tsb_szc, 11679 hatlockp, TSB_SWAPIN); 11680 if (rc != TSB_SUCCESS) { 11681 break; 11682 } 11683 gotfirst = B_TRUE; 11684 } 11685 11686 switch (rc) { 11687 case TSB_SUCCESS: 11688 SFMMU_FLAGS_CLEAR(sfmmup, HAT_SWAPPED|HAT_SWAPIN); 11689 cv_broadcast(&sfmmup->sfmmu_tsb_cv); 11690 return; 11691 case TSB_LOSTRACE: 11692 break; 11693 case TSB_ALLOCFAIL: 11694 break; 11695 default: 11696 panic("sfmmu_replace_tsb returned unrecognized failure code " 11697 "%d", rc); 11698 } 11699 11700 /* 11701 * In this case, we failed to get one of our TSBs. If we failed to 11702 * get the first TSB, get one of minimum size (8KB). Walk the list 11703 * and throw away the tsbinfos, starting where the allocation failed; 11704 * we can get by with just one TSB as long as we don't leave the 11705 * SWAPPED tsbinfo structures lying around. 11706 */ 11707 tsbinfop = sfmmup->sfmmu_tsb; 11708 next = tsbinfop->tsb_next; 11709 tsbinfop->tsb_next = NULL; 11710 11711 sfmmu_hat_exit(hatlockp); 11712 for (tsbinfop = next; tsbinfop != NULL; tsbinfop = next) { 11713 next = tsbinfop->tsb_next; 11714 sfmmu_tsbinfo_free(tsbinfop); 11715 } 11716 hatlockp = sfmmu_hat_enter(sfmmup); 11717 11718 /* 11719 * If we don't have any TSBs, get a single 8K TSB for 8K, 64K and 512K 11720 * pages. 11721 */ 11722 if (!gotfirst) { 11723 tsbinfop = sfmmup->sfmmu_tsb; 11724 rc = sfmmu_replace_tsb(sfmmup, tsbinfop, TSB_MIN_SZCODE, 11725 hatlockp, TSB_SWAPIN | TSB_FORCEALLOC); 11726 ASSERT(rc == TSB_SUCCESS); 11727 } 11728 11729 SFMMU_FLAGS_CLEAR(sfmmup, HAT_SWAPPED|HAT_SWAPIN); 11730 cv_broadcast(&sfmmup->sfmmu_tsb_cv); 11731 } 11732 11733 static int 11734 sfmmu_is_rgnva(sf_srd_t *srdp, caddr_t addr, ulong_t w, ulong_t bmw) 11735 { 11736 ulong_t bix = 0; 11737 uint_t rid; 11738 sf_region_t *rgnp; 11739 11740 ASSERT(srdp != NULL); 11741 ASSERT(srdp->srd_refcnt != 0); 11742 11743 w <<= BT_ULSHIFT; 11744 while (bmw) { 11745 if (!(bmw & 0x1)) { 11746 bix++; 11747 bmw >>= 1; 11748 continue; 11749 } 11750 rid = w | bix; 11751 rgnp = srdp->srd_hmergnp[rid]; 11752 ASSERT(rgnp->rgn_refcnt > 0); 11753 ASSERT(rgnp->rgn_id == rid); 11754 if (addr < rgnp->rgn_saddr || 11755 addr >= (rgnp->rgn_saddr + rgnp->rgn_size)) { 11756 bix++; 11757 bmw >>= 1; 11758 } else { 11759 return (1); 11760 } 11761 } 11762 return (0); 11763 } 11764 11765 /* 11766 * Handle exceptions for low level tsb_handler. 11767 * 11768 * There are many scenarios that could land us here: 11769 * 11770 * If the context is invalid we land here. The context can be invalid 11771 * for 3 reasons: 1) we couldn't allocate a new context and now need to 11772 * perform a wrap around operation in order to allocate a new context. 11773 * 2) Context was invalidated to change pagesize programming 3) ISMs or 11774 * TSBs configuration is changeing for this process and we are forced into 11775 * here to do a syncronization operation. If the context is valid we can 11776 * be here from window trap hanlder. In this case just call trap to handle 11777 * the fault. 11778 * 11779 * Note that the process will run in INVALID_CONTEXT before 11780 * faulting into here and subsequently loading the MMU registers 11781 * (including the TSB base register) associated with this process. 11782 * For this reason, the trap handlers must all test for 11783 * INVALID_CONTEXT before attempting to access any registers other 11784 * than the context registers. 11785 */ 11786 void 11787 sfmmu_tsbmiss_exception(struct regs *rp, uintptr_t tagaccess, uint_t traptype) 11788 { 11789 sfmmu_t *sfmmup, *shsfmmup; 11790 uint_t ctxtype; 11791 klwp_id_t lwp; 11792 char lwp_save_state; 11793 hatlock_t *hatlockp, *shatlockp; 11794 struct tsb_info *tsbinfop; 11795 struct tsbmiss *tsbmp; 11796 sf_scd_t *scdp; 11797 11798 SFMMU_STAT(sf_tsb_exceptions); 11799 SFMMU_MMU_STAT(mmu_tsb_exceptions); 11800 sfmmup = astosfmmu(curthread->t_procp->p_as); 11801 /* 11802 * note that in sun4u, tagacces register contains ctxnum 11803 * while sun4v passes ctxtype in the tagaccess register. 11804 */ 11805 ctxtype = tagaccess & TAGACC_CTX_MASK; 11806 11807 ASSERT(sfmmup != ksfmmup && ctxtype != KCONTEXT); 11808 ASSERT(sfmmup->sfmmu_ismhat == 0); 11809 ASSERT(!SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED) || 11810 ctxtype == INVALID_CONTEXT); 11811 11812 if (ctxtype != INVALID_CONTEXT && traptype != T_DATA_PROT) { 11813 /* 11814 * We may land here because shme bitmap and pagesize 11815 * flags are updated lazily in tsbmiss area on other cpus. 11816 * If we detect here that tsbmiss area is out of sync with 11817 * sfmmu update it and retry the trapped instruction. 11818 * Otherwise call trap(). 11819 */ 11820 int ret = 0; 11821 uchar_t tteflag_mask = (1 << TTE64K) | (1 << TTE8K); 11822 caddr_t addr = (caddr_t)(tagaccess & TAGACC_VADDR_MASK); 11823 11824 /* 11825 * Must set lwp state to LWP_SYS before 11826 * trying to acquire any adaptive lock 11827 */ 11828 lwp = ttolwp(curthread); 11829 ASSERT(lwp); 11830 lwp_save_state = lwp->lwp_state; 11831 lwp->lwp_state = LWP_SYS; 11832 11833 hatlockp = sfmmu_hat_enter(sfmmup); 11834 kpreempt_disable(); 11835 tsbmp = &tsbmiss_area[CPU->cpu_id]; 11836 ASSERT(sfmmup == tsbmp->usfmmup); 11837 if (((tsbmp->uhat_tteflags ^ sfmmup->sfmmu_tteflags) & 11838 ~tteflag_mask) || 11839 ((tsbmp->uhat_rtteflags ^ sfmmup->sfmmu_rtteflags) & 11840 ~tteflag_mask)) { 11841 tsbmp->uhat_tteflags = sfmmup->sfmmu_tteflags; 11842 tsbmp->uhat_rtteflags = sfmmup->sfmmu_rtteflags; 11843 ret = 1; 11844 } 11845 if (sfmmup->sfmmu_srdp != NULL) { 11846 ulong_t *sm = sfmmup->sfmmu_hmeregion_map.bitmap; 11847 ulong_t *tm = tsbmp->shmermap; 11848 ulong_t i; 11849 for (i = 0; i < SFMMU_HMERGNMAP_WORDS; i++) { 11850 ulong_t d = tm[i] ^ sm[i]; 11851 if (d) { 11852 if (d & sm[i]) { 11853 if (!ret && sfmmu_is_rgnva( 11854 sfmmup->sfmmu_srdp, 11855 addr, i, d & sm[i])) { 11856 ret = 1; 11857 } 11858 } 11859 tm[i] = sm[i]; 11860 } 11861 } 11862 } 11863 kpreempt_enable(); 11864 sfmmu_hat_exit(hatlockp); 11865 lwp->lwp_state = lwp_save_state; 11866 if (ret) { 11867 return; 11868 } 11869 } else if (ctxtype == INVALID_CONTEXT) { 11870 /* 11871 * First, make sure we come out of here with a valid ctx, 11872 * since if we don't get one we'll simply loop on the 11873 * faulting instruction. 11874 * 11875 * If the ISM mappings are changing, the TSB is relocated, 11876 * the process is swapped, the process is joining SCD or 11877 * leaving SCD or shared regions we serialize behind the 11878 * controlling thread with hat lock, sfmmu_flags and 11879 * sfmmu_tsb_cv condition variable. 11880 */ 11881 11882 /* 11883 * Must set lwp state to LWP_SYS before 11884 * trying to acquire any adaptive lock 11885 */ 11886 lwp = ttolwp(curthread); 11887 ASSERT(lwp); 11888 lwp_save_state = lwp->lwp_state; 11889 lwp->lwp_state = LWP_SYS; 11890 11891 hatlockp = sfmmu_hat_enter(sfmmup); 11892 retry: 11893 if ((scdp = sfmmup->sfmmu_scdp) != NULL) { 11894 shsfmmup = scdp->scd_sfmmup; 11895 ASSERT(shsfmmup != NULL); 11896 11897 for (tsbinfop = shsfmmup->sfmmu_tsb; tsbinfop != NULL; 11898 tsbinfop = tsbinfop->tsb_next) { 11899 if (tsbinfop->tsb_flags & TSB_RELOC_FLAG) { 11900 /* drop the private hat lock */ 11901 sfmmu_hat_exit(hatlockp); 11902 /* acquire the shared hat lock */ 11903 shatlockp = sfmmu_hat_enter(shsfmmup); 11904 /* 11905 * recheck to see if anything changed 11906 * after we drop the private hat lock. 11907 */ 11908 if (sfmmup->sfmmu_scdp == scdp && 11909 shsfmmup == scdp->scd_sfmmup) { 11910 sfmmu_tsb_chk_reloc(shsfmmup, 11911 shatlockp); 11912 } 11913 sfmmu_hat_exit(shatlockp); 11914 hatlockp = sfmmu_hat_enter(sfmmup); 11915 goto retry; 11916 } 11917 } 11918 } 11919 11920 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL; 11921 tsbinfop = tsbinfop->tsb_next) { 11922 if (tsbinfop->tsb_flags & TSB_RELOC_FLAG) { 11923 cv_wait(&sfmmup->sfmmu_tsb_cv, 11924 HATLOCK_MUTEXP(hatlockp)); 11925 goto retry; 11926 } 11927 } 11928 11929 /* 11930 * Wait for ISM maps to be updated. 11931 */ 11932 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)) { 11933 cv_wait(&sfmmup->sfmmu_tsb_cv, 11934 HATLOCK_MUTEXP(hatlockp)); 11935 goto retry; 11936 } 11937 11938 /* Is this process joining an SCD? */ 11939 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)) { 11940 /* 11941 * Flush private TSB and setup shared TSB. 11942 * sfmmu_finish_join_scd() does not drop the 11943 * hat lock. 11944 */ 11945 sfmmu_finish_join_scd(sfmmup); 11946 SFMMU_FLAGS_CLEAR(sfmmup, HAT_JOIN_SCD); 11947 } 11948 11949 /* 11950 * If we're swapping in, get TSB(s). Note that we must do 11951 * this before we get a ctx or load the MMU state. Once 11952 * we swap in we have to recheck to make sure the TSB(s) and 11953 * ISM mappings didn't change while we slept. 11954 */ 11955 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) { 11956 sfmmu_tsb_swapin(sfmmup, hatlockp); 11957 goto retry; 11958 } 11959 11960 sfmmu_get_ctx(sfmmup); 11961 11962 sfmmu_hat_exit(hatlockp); 11963 /* 11964 * Must restore lwp_state if not calling 11965 * trap() for further processing. Restore 11966 * it anyway. 11967 */ 11968 lwp->lwp_state = lwp_save_state; 11969 return; 11970 } 11971 trap(rp, (caddr_t)tagaccess, traptype, 0); 11972 } 11973 11974 static void 11975 sfmmu_tsb_chk_reloc(sfmmu_t *sfmmup, hatlock_t *hatlockp) 11976 { 11977 struct tsb_info *tp; 11978 11979 ASSERT(sfmmu_hat_lock_held(sfmmup)); 11980 11981 for (tp = sfmmup->sfmmu_tsb; tp != NULL; tp = tp->tsb_next) { 11982 if (tp->tsb_flags & TSB_RELOC_FLAG) { 11983 cv_wait(&sfmmup->sfmmu_tsb_cv, 11984 HATLOCK_MUTEXP(hatlockp)); 11985 break; 11986 } 11987 } 11988 } 11989 11990 /* 11991 * sfmmu_vatopfn_suspended is called from GET_TTE when TL=0 and 11992 * TTE_SUSPENDED bit set in tte we block on aquiring a page lock 11993 * rather than spinning to avoid send mondo timeouts with 11994 * interrupts enabled. When the lock is acquired it is immediately 11995 * released and we return back to sfmmu_vatopfn just after 11996 * the GET_TTE call. 11997 */ 11998 void 11999 sfmmu_vatopfn_suspended(caddr_t vaddr, sfmmu_t *sfmmu, tte_t *ttep) 12000 { 12001 struct page **pp; 12002 12003 (void) as_pagelock(sfmmu->sfmmu_as, &pp, vaddr, TTE_CSZ(ttep), S_WRITE); 12004 as_pageunlock(sfmmu->sfmmu_as, pp, vaddr, TTE_CSZ(ttep), S_WRITE); 12005 } 12006 12007 /* 12008 * sfmmu_tsbmiss_suspended is called from GET_TTE when TL>0 and 12009 * TTE_SUSPENDED bit set in tte. We do this so that we can handle 12010 * cross traps which cannot be handled while spinning in the 12011 * trap handlers. Simply enter and exit the kpr_suspendlock spin 12012 * mutex, which is held by the holder of the suspend bit, and then 12013 * retry the trapped instruction after unwinding. 12014 */ 12015 /*ARGSUSED*/ 12016 void 12017 sfmmu_tsbmiss_suspended(struct regs *rp, uintptr_t tagacc, uint_t traptype) 12018 { 12019 ASSERT(curthread != kreloc_thread); 12020 mutex_enter(&kpr_suspendlock); 12021 mutex_exit(&kpr_suspendlock); 12022 } 12023 12024 /* 12025 * This routine could be optimized to reduce the number of xcalls by flushing 12026 * the entire TLBs if region reference count is above some threshold but the 12027 * tradeoff will depend on the size of the TLB. So for now flush the specific 12028 * page a context at a time. 12029 * 12030 * If uselocks is 0 then it's called after all cpus were captured and all the 12031 * hat locks were taken. In this case don't take the region lock by relying on 12032 * the order of list region update operations in hat_join_region(), 12033 * hat_leave_region() and hat_dup_region(). The ordering in those routines 12034 * guarantees that list is always forward walkable and reaches active sfmmus 12035 * regardless of where xc_attention() captures a cpu. 12036 */ 12037 cpuset_t 12038 sfmmu_rgntlb_demap(caddr_t addr, sf_region_t *rgnp, 12039 struct hme_blk *hmeblkp, int uselocks) 12040 { 12041 sfmmu_t *sfmmup; 12042 cpuset_t cpuset; 12043 cpuset_t rcpuset; 12044 hatlock_t *hatlockp; 12045 uint_t rid = rgnp->rgn_id; 12046 sf_rgn_link_t *rlink; 12047 sf_scd_t *scdp; 12048 12049 ASSERT(hmeblkp->hblk_shared); 12050 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 12051 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 12052 12053 CPUSET_ZERO(rcpuset); 12054 if (uselocks) { 12055 mutex_enter(&rgnp->rgn_mutex); 12056 } 12057 sfmmup = rgnp->rgn_sfmmu_head; 12058 while (sfmmup != NULL) { 12059 if (uselocks) { 12060 hatlockp = sfmmu_hat_enter(sfmmup); 12061 } 12062 12063 /* 12064 * When an SCD is created the SCD hat is linked on the sfmmu 12065 * region lists for each hme region which is part of the 12066 * SCD. If we find an SCD hat, when walking these lists, 12067 * then we flush the shared TSBs, if we find a private hat, 12068 * which is part of an SCD, but where the region 12069 * is not part of the SCD then we flush the private TSBs. 12070 */ 12071 if (!sfmmup->sfmmu_scdhat && sfmmup->sfmmu_scdp != NULL && 12072 !SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)) { 12073 scdp = sfmmup->sfmmu_scdp; 12074 if (SF_RGNMAP_TEST(scdp->scd_hmeregion_map, rid)) { 12075 if (uselocks) { 12076 sfmmu_hat_exit(hatlockp); 12077 } 12078 goto next; 12079 } 12080 } 12081 12082 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0); 12083 12084 kpreempt_disable(); 12085 cpuset = sfmmup->sfmmu_cpusran; 12086 CPUSET_AND(cpuset, cpu_ready_set); 12087 CPUSET_DEL(cpuset, CPU->cpu_id); 12088 SFMMU_XCALL_STATS(sfmmup); 12089 xt_some(cpuset, vtag_flushpage_tl1, 12090 (uint64_t)addr, (uint64_t)sfmmup); 12091 vtag_flushpage(addr, (uint64_t)sfmmup); 12092 if (uselocks) { 12093 sfmmu_hat_exit(hatlockp); 12094 } 12095 kpreempt_enable(); 12096 CPUSET_OR(rcpuset, cpuset); 12097 12098 next: 12099 /* LINTED: constant in conditional context */ 12100 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 0, 0); 12101 ASSERT(rlink != NULL); 12102 sfmmup = rlink->next; 12103 } 12104 if (uselocks) { 12105 mutex_exit(&rgnp->rgn_mutex); 12106 } 12107 return (rcpuset); 12108 } 12109 12110 /* 12111 * This routine takes an sfmmu pointer and the va for an adddress in an 12112 * ISM region as input and returns the corresponding region id in ism_rid. 12113 * The return value of 1 indicates that a region has been found and ism_rid 12114 * is valid, otherwise 0 is returned. 12115 */ 12116 static int 12117 find_ism_rid(sfmmu_t *sfmmup, sfmmu_t *ism_sfmmup, caddr_t va, uint_t *ism_rid) 12118 { 12119 ism_blk_t *ism_blkp; 12120 int i; 12121 ism_map_t *ism_map; 12122 #ifdef DEBUG 12123 struct hat *ism_hatid; 12124 #endif 12125 ASSERT(sfmmu_hat_lock_held(sfmmup)); 12126 12127 ism_blkp = sfmmup->sfmmu_iblk; 12128 while (ism_blkp != NULL) { 12129 ism_map = ism_blkp->iblk_maps; 12130 for (i = 0; i < ISM_MAP_SLOTS && ism_map[i].imap_ismhat; i++) { 12131 if ((va >= ism_start(ism_map[i])) && 12132 (va < ism_end(ism_map[i]))) { 12133 12134 *ism_rid = ism_map[i].imap_rid; 12135 #ifdef DEBUG 12136 ism_hatid = ism_map[i].imap_ismhat; 12137 ASSERT(ism_hatid == ism_sfmmup); 12138 ASSERT(ism_hatid->sfmmu_ismhat); 12139 #endif 12140 return (1); 12141 } 12142 } 12143 ism_blkp = ism_blkp->iblk_next; 12144 } 12145 return (0); 12146 } 12147 12148 /* 12149 * Special routine to flush out ism mappings- TSBs, TLBs and D-caches. 12150 * This routine may be called with all cpu's captured. Therefore, the 12151 * caller is responsible for holding all locks and disabling kernel 12152 * preemption. 12153 */ 12154 /* ARGSUSED */ 12155 static void 12156 sfmmu_ismtlbcache_demap(caddr_t addr, sfmmu_t *ism_sfmmup, 12157 struct hme_blk *hmeblkp, pfn_t pfnum, int cache_flush_flag) 12158 { 12159 cpuset_t cpuset; 12160 caddr_t va; 12161 ism_ment_t *ment; 12162 sfmmu_t *sfmmup; 12163 #ifdef VAC 12164 int vcolor; 12165 #endif 12166 12167 sf_scd_t *scdp; 12168 uint_t ism_rid; 12169 12170 ASSERT(!hmeblkp->hblk_shared); 12171 /* 12172 * Walk the ism_hat's mapping list and flush the page 12173 * from every hat sharing this ism_hat. This routine 12174 * may be called while all cpu's have been captured. 12175 * Therefore we can't attempt to grab any locks. For now 12176 * this means we will protect the ism mapping list under 12177 * a single lock which will be grabbed by the caller. 12178 * If hat_share/unshare scalibility becomes a performance 12179 * problem then we may need to re-think ism mapping list locking. 12180 */ 12181 ASSERT(ism_sfmmup->sfmmu_ismhat); 12182 ASSERT(MUTEX_HELD(&ism_mlist_lock)); 12183 addr = addr - ISMID_STARTADDR; 12184 12185 for (ment = ism_sfmmup->sfmmu_iment; ment; ment = ment->iment_next) { 12186 12187 sfmmup = ment->iment_hat; 12188 12189 va = ment->iment_base_va; 12190 va = (caddr_t)((uintptr_t)va + (uintptr_t)addr); 12191 12192 /* 12193 * When an SCD is created the SCD hat is linked on the ism 12194 * mapping lists for each ISM segment which is part of the 12195 * SCD. If we find an SCD hat, when walking these lists, 12196 * then we flush the shared TSBs, if we find a private hat, 12197 * which is part of an SCD, but where the region 12198 * corresponding to this va is not part of the SCD then we 12199 * flush the private TSBs. 12200 */ 12201 if (!sfmmup->sfmmu_scdhat && sfmmup->sfmmu_scdp != NULL && 12202 !SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD) && 12203 !SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)) { 12204 if (!find_ism_rid(sfmmup, ism_sfmmup, va, 12205 &ism_rid)) { 12206 cmn_err(CE_PANIC, 12207 "can't find matching ISM rid!"); 12208 } 12209 12210 scdp = sfmmup->sfmmu_scdp; 12211 if (SFMMU_IS_ISMRID_VALID(ism_rid) && 12212 SF_RGNMAP_TEST(scdp->scd_ismregion_map, 12213 ism_rid)) { 12214 continue; 12215 } 12216 } 12217 SFMMU_UNLOAD_TSB(va, sfmmup, hmeblkp, 1); 12218 12219 cpuset = sfmmup->sfmmu_cpusran; 12220 CPUSET_AND(cpuset, cpu_ready_set); 12221 CPUSET_DEL(cpuset, CPU->cpu_id); 12222 SFMMU_XCALL_STATS(sfmmup); 12223 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)va, 12224 (uint64_t)sfmmup); 12225 vtag_flushpage(va, (uint64_t)sfmmup); 12226 12227 #ifdef VAC 12228 /* 12229 * Flush D$ 12230 * When flushing D$ we must flush all 12231 * cpu's. See sfmmu_cache_flush(). 12232 */ 12233 if (cache_flush_flag == CACHE_FLUSH) { 12234 cpuset = cpu_ready_set; 12235 CPUSET_DEL(cpuset, CPU->cpu_id); 12236 12237 SFMMU_XCALL_STATS(sfmmup); 12238 vcolor = addr_to_vcolor(va); 12239 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor); 12240 vac_flushpage(pfnum, vcolor); 12241 } 12242 #endif /* VAC */ 12243 } 12244 } 12245 12246 /* 12247 * Demaps the TSB, CPU caches, and flushes all TLBs on all CPUs of 12248 * a particular virtual address and ctx. If noflush is set we do not 12249 * flush the TLB/TSB. This function may or may not be called with the 12250 * HAT lock held. 12251 */ 12252 static void 12253 sfmmu_tlbcache_demap(caddr_t addr, sfmmu_t *sfmmup, struct hme_blk *hmeblkp, 12254 pfn_t pfnum, int tlb_noflush, int cpu_flag, int cache_flush_flag, 12255 int hat_lock_held) 12256 { 12257 #ifdef VAC 12258 int vcolor; 12259 #endif 12260 cpuset_t cpuset; 12261 hatlock_t *hatlockp; 12262 12263 ASSERT(!hmeblkp->hblk_shared); 12264 12265 #if defined(lint) && !defined(VAC) 12266 pfnum = pfnum; 12267 cpu_flag = cpu_flag; 12268 cache_flush_flag = cache_flush_flag; 12269 #endif 12270 12271 /* 12272 * There is no longer a need to protect against ctx being 12273 * stolen here since we don't store the ctx in the TSB anymore. 12274 */ 12275 #ifdef VAC 12276 vcolor = addr_to_vcolor(addr); 12277 #endif 12278 12279 /* 12280 * We must hold the hat lock during the flush of TLB, 12281 * to avoid a race with sfmmu_invalidate_ctx(), where 12282 * sfmmu_cnum on a MMU could be set to INVALID_CONTEXT, 12283 * causing TLB demap routine to skip flush on that MMU. 12284 * If the context on a MMU has already been set to 12285 * INVALID_CONTEXT, we just get an extra flush on 12286 * that MMU. 12287 */ 12288 if (!hat_lock_held && !tlb_noflush) 12289 hatlockp = sfmmu_hat_enter(sfmmup); 12290 12291 kpreempt_disable(); 12292 if (!tlb_noflush) { 12293 /* 12294 * Flush the TSB and TLB. 12295 */ 12296 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0); 12297 12298 cpuset = sfmmup->sfmmu_cpusran; 12299 CPUSET_AND(cpuset, cpu_ready_set); 12300 CPUSET_DEL(cpuset, CPU->cpu_id); 12301 12302 SFMMU_XCALL_STATS(sfmmup); 12303 12304 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr, 12305 (uint64_t)sfmmup); 12306 12307 vtag_flushpage(addr, (uint64_t)sfmmup); 12308 } 12309 12310 if (!hat_lock_held && !tlb_noflush) 12311 sfmmu_hat_exit(hatlockp); 12312 12313 #ifdef VAC 12314 /* 12315 * Flush the D$ 12316 * 12317 * Even if the ctx is stolen, we need to flush the 12318 * cache. Our ctx stealer only flushes the TLBs. 12319 */ 12320 if (cache_flush_flag == CACHE_FLUSH) { 12321 if (cpu_flag & FLUSH_ALL_CPUS) { 12322 cpuset = cpu_ready_set; 12323 } else { 12324 cpuset = sfmmup->sfmmu_cpusran; 12325 CPUSET_AND(cpuset, cpu_ready_set); 12326 } 12327 CPUSET_DEL(cpuset, CPU->cpu_id); 12328 SFMMU_XCALL_STATS(sfmmup); 12329 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor); 12330 vac_flushpage(pfnum, vcolor); 12331 } 12332 #endif /* VAC */ 12333 kpreempt_enable(); 12334 } 12335 12336 /* 12337 * Demaps the TSB and flushes all TLBs on all cpus for a particular virtual 12338 * address and ctx. If noflush is set we do not currently do anything. 12339 * This function may or may not be called with the HAT lock held. 12340 */ 12341 static void 12342 sfmmu_tlb_demap(caddr_t addr, sfmmu_t *sfmmup, struct hme_blk *hmeblkp, 12343 int tlb_noflush, int hat_lock_held) 12344 { 12345 cpuset_t cpuset; 12346 hatlock_t *hatlockp; 12347 12348 ASSERT(!hmeblkp->hblk_shared); 12349 12350 /* 12351 * If the process is exiting we have nothing to do. 12352 */ 12353 if (tlb_noflush) 12354 return; 12355 12356 /* 12357 * Flush TSB. 12358 */ 12359 if (!hat_lock_held) 12360 hatlockp = sfmmu_hat_enter(sfmmup); 12361 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0); 12362 12363 kpreempt_disable(); 12364 12365 cpuset = sfmmup->sfmmu_cpusran; 12366 CPUSET_AND(cpuset, cpu_ready_set); 12367 CPUSET_DEL(cpuset, CPU->cpu_id); 12368 12369 SFMMU_XCALL_STATS(sfmmup); 12370 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr, (uint64_t)sfmmup); 12371 12372 vtag_flushpage(addr, (uint64_t)sfmmup); 12373 12374 if (!hat_lock_held) 12375 sfmmu_hat_exit(hatlockp); 12376 12377 kpreempt_enable(); 12378 12379 } 12380 12381 /* 12382 * Special case of sfmmu_tlb_demap for MMU_PAGESIZE hblks. Use the xcall 12383 * call handler that can flush a range of pages to save on xcalls. 12384 */ 12385 static int sfmmu_xcall_save; 12386 12387 /* 12388 * this routine is never used for demaping addresses backed by SRD hmeblks. 12389 */ 12390 static void 12391 sfmmu_tlb_range_demap(demap_range_t *dmrp) 12392 { 12393 sfmmu_t *sfmmup = dmrp->dmr_sfmmup; 12394 hatlock_t *hatlockp; 12395 cpuset_t cpuset; 12396 uint64_t sfmmu_pgcnt; 12397 pgcnt_t pgcnt = 0; 12398 int pgunload = 0; 12399 int dirtypg = 0; 12400 caddr_t addr = dmrp->dmr_addr; 12401 caddr_t eaddr; 12402 uint64_t bitvec = dmrp->dmr_bitvec; 12403 12404 ASSERT(bitvec & 1); 12405 12406 /* 12407 * Flush TSB and calculate number of pages to flush. 12408 */ 12409 while (bitvec != 0) { 12410 dirtypg = 0; 12411 /* 12412 * Find the first page to flush and then count how many 12413 * pages there are after it that also need to be flushed. 12414 * This way the number of TSB flushes is minimized. 12415 */ 12416 while ((bitvec & 1) == 0) { 12417 pgcnt++; 12418 addr += MMU_PAGESIZE; 12419 bitvec >>= 1; 12420 } 12421 while (bitvec & 1) { 12422 dirtypg++; 12423 bitvec >>= 1; 12424 } 12425 eaddr = addr + ptob(dirtypg); 12426 hatlockp = sfmmu_hat_enter(sfmmup); 12427 sfmmu_unload_tsb_range(sfmmup, addr, eaddr, TTE8K); 12428 sfmmu_hat_exit(hatlockp); 12429 pgunload += dirtypg; 12430 addr = eaddr; 12431 pgcnt += dirtypg; 12432 } 12433 12434 ASSERT((pgcnt<<MMU_PAGESHIFT) <= dmrp->dmr_endaddr - dmrp->dmr_addr); 12435 if (sfmmup->sfmmu_free == 0) { 12436 addr = dmrp->dmr_addr; 12437 bitvec = dmrp->dmr_bitvec; 12438 12439 /* 12440 * make sure it has SFMMU_PGCNT_SHIFT bits only, 12441 * as it will be used to pack argument for xt_some 12442 */ 12443 ASSERT((pgcnt > 0) && 12444 (pgcnt <= (1 << SFMMU_PGCNT_SHIFT))); 12445 12446 /* 12447 * Encode pgcnt as (pgcnt -1 ), and pass (pgcnt - 1) in 12448 * the low 6 bits of sfmmup. This is doable since pgcnt 12449 * always >= 1. 12450 */ 12451 ASSERT(!((uint64_t)sfmmup & SFMMU_PGCNT_MASK)); 12452 sfmmu_pgcnt = (uint64_t)sfmmup | 12453 ((pgcnt - 1) & SFMMU_PGCNT_MASK); 12454 12455 /* 12456 * We must hold the hat lock during the flush of TLB, 12457 * to avoid a race with sfmmu_invalidate_ctx(), where 12458 * sfmmu_cnum on a MMU could be set to INVALID_CONTEXT, 12459 * causing TLB demap routine to skip flush on that MMU. 12460 * If the context on a MMU has already been set to 12461 * INVALID_CONTEXT, we just get an extra flush on 12462 * that MMU. 12463 */ 12464 hatlockp = sfmmu_hat_enter(sfmmup); 12465 kpreempt_disable(); 12466 12467 cpuset = sfmmup->sfmmu_cpusran; 12468 CPUSET_AND(cpuset, cpu_ready_set); 12469 CPUSET_DEL(cpuset, CPU->cpu_id); 12470 12471 SFMMU_XCALL_STATS(sfmmup); 12472 xt_some(cpuset, vtag_flush_pgcnt_tl1, (uint64_t)addr, 12473 sfmmu_pgcnt); 12474 12475 for (; bitvec != 0; bitvec >>= 1) { 12476 if (bitvec & 1) 12477 vtag_flushpage(addr, (uint64_t)sfmmup); 12478 addr += MMU_PAGESIZE; 12479 } 12480 kpreempt_enable(); 12481 sfmmu_hat_exit(hatlockp); 12482 12483 sfmmu_xcall_save += (pgunload-1); 12484 } 12485 dmrp->dmr_bitvec = 0; 12486 } 12487 12488 /* 12489 * In cases where we need to synchronize with TLB/TSB miss trap 12490 * handlers, _and_ need to flush the TLB, it's a lot easier to 12491 * throw away the context from the process than to do a 12492 * special song and dance to keep things consistent for the 12493 * handlers. 12494 * 12495 * Since the process suddenly ends up without a context and our caller 12496 * holds the hat lock, threads that fault after this function is called 12497 * will pile up on the lock. We can then do whatever we need to 12498 * atomically from the context of the caller. The first blocked thread 12499 * to resume executing will get the process a new context, and the 12500 * process will resume executing. 12501 * 12502 * One added advantage of this approach is that on MMUs that 12503 * support a "flush all" operation, we will delay the flush until 12504 * cnum wrap-around, and then flush the TLB one time. This 12505 * is rather rare, so it's a lot less expensive than making 8000 12506 * x-calls to flush the TLB 8000 times. 12507 * 12508 * A per-process (PP) lock is used to synchronize ctx allocations in 12509 * resume() and ctx invalidations here. 12510 */ 12511 static void 12512 sfmmu_invalidate_ctx(sfmmu_t *sfmmup) 12513 { 12514 cpuset_t cpuset; 12515 int cnum, currcnum; 12516 mmu_ctx_t *mmu_ctxp; 12517 int i; 12518 uint_t pstate_save; 12519 12520 SFMMU_STAT(sf_ctx_inv); 12521 12522 ASSERT(sfmmu_hat_lock_held(sfmmup)); 12523 ASSERT(sfmmup != ksfmmup); 12524 12525 kpreempt_disable(); 12526 12527 mmu_ctxp = CPU_MMU_CTXP(CPU); 12528 ASSERT(mmu_ctxp); 12529 ASSERT(mmu_ctxp->mmu_idx < max_mmu_ctxdoms); 12530 ASSERT(mmu_ctxp == mmu_ctxs_tbl[mmu_ctxp->mmu_idx]); 12531 12532 currcnum = sfmmup->sfmmu_ctxs[mmu_ctxp->mmu_idx].cnum; 12533 12534 pstate_save = sfmmu_disable_intrs(); 12535 12536 lock_set(&sfmmup->sfmmu_ctx_lock); /* acquire PP lock */ 12537 /* set HAT cnum invalid across all context domains. */ 12538 for (i = 0; i < max_mmu_ctxdoms; i++) { 12539 12540 cnum = sfmmup->sfmmu_ctxs[i].cnum; 12541 if (cnum == INVALID_CONTEXT) { 12542 continue; 12543 } 12544 12545 sfmmup->sfmmu_ctxs[i].cnum = INVALID_CONTEXT; 12546 } 12547 membar_enter(); /* make sure globally visible to all CPUs */ 12548 lock_clear(&sfmmup->sfmmu_ctx_lock); /* release PP lock */ 12549 12550 sfmmu_enable_intrs(pstate_save); 12551 12552 cpuset = sfmmup->sfmmu_cpusran; 12553 CPUSET_DEL(cpuset, CPU->cpu_id); 12554 CPUSET_AND(cpuset, cpu_ready_set); 12555 if (!CPUSET_ISNULL(cpuset)) { 12556 SFMMU_XCALL_STATS(sfmmup); 12557 xt_some(cpuset, sfmmu_raise_tsb_exception, 12558 (uint64_t)sfmmup, INVALID_CONTEXT); 12559 xt_sync(cpuset); 12560 SFMMU_STAT(sf_tsb_raise_exception); 12561 SFMMU_MMU_STAT(mmu_tsb_raise_exception); 12562 } 12563 12564 /* 12565 * If the hat to-be-invalidated is the same as the current 12566 * process on local CPU we need to invalidate 12567 * this CPU context as well. 12568 */ 12569 if ((sfmmu_getctx_sec() == currcnum) && 12570 (currcnum != INVALID_CONTEXT)) { 12571 /* sets shared context to INVALID too */ 12572 sfmmu_setctx_sec(INVALID_CONTEXT); 12573 sfmmu_clear_utsbinfo(); 12574 } 12575 12576 SFMMU_FLAGS_SET(sfmmup, HAT_ALLCTX_INVALID); 12577 12578 kpreempt_enable(); 12579 12580 /* 12581 * we hold the hat lock, so nobody should allocate a context 12582 * for us yet 12583 */ 12584 ASSERT(sfmmup->sfmmu_ctxs[mmu_ctxp->mmu_idx].cnum == INVALID_CONTEXT); 12585 } 12586 12587 #ifdef VAC 12588 /* 12589 * We need to flush the cache in all cpus. It is possible that 12590 * a process referenced a page as cacheable but has sinced exited 12591 * and cleared the mapping list. We still to flush it but have no 12592 * state so all cpus is the only alternative. 12593 */ 12594 void 12595 sfmmu_cache_flush(pfn_t pfnum, int vcolor) 12596 { 12597 cpuset_t cpuset; 12598 12599 kpreempt_disable(); 12600 cpuset = cpu_ready_set; 12601 CPUSET_DEL(cpuset, CPU->cpu_id); 12602 SFMMU_XCALL_STATS(NULL); /* account to any ctx */ 12603 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor); 12604 xt_sync(cpuset); 12605 vac_flushpage(pfnum, vcolor); 12606 kpreempt_enable(); 12607 } 12608 12609 void 12610 sfmmu_cache_flushcolor(int vcolor, pfn_t pfnum) 12611 { 12612 cpuset_t cpuset; 12613 12614 ASSERT(vcolor >= 0); 12615 12616 kpreempt_disable(); 12617 cpuset = cpu_ready_set; 12618 CPUSET_DEL(cpuset, CPU->cpu_id); 12619 SFMMU_XCALL_STATS(NULL); /* account to any ctx */ 12620 xt_some(cpuset, vac_flushcolor_tl1, vcolor, pfnum); 12621 xt_sync(cpuset); 12622 vac_flushcolor(vcolor, pfnum); 12623 kpreempt_enable(); 12624 } 12625 #endif /* VAC */ 12626 12627 /* 12628 * We need to prevent processes from accessing the TSB using a cached physical 12629 * address. It's alright if they try to access the TSB via virtual address 12630 * since they will just fault on that virtual address once the mapping has 12631 * been suspended. 12632 */ 12633 #pragma weak sendmondo_in_recover 12634 12635 /* ARGSUSED */ 12636 static int 12637 sfmmu_tsb_pre_relocator(caddr_t va, uint_t tsbsz, uint_t flags, void *tsbinfo) 12638 { 12639 struct tsb_info *tsbinfop = (struct tsb_info *)tsbinfo; 12640 sfmmu_t *sfmmup = tsbinfop->tsb_sfmmu; 12641 hatlock_t *hatlockp; 12642 sf_scd_t *scdp; 12643 12644 if (flags != HAT_PRESUSPEND) 12645 return (0); 12646 12647 /* 12648 * If tsb is a shared TSB with TSB_SHAREDCTX set, sfmmup must 12649 * be a shared hat, then set SCD's tsbinfo's flag. 12650 * If tsb is not shared, sfmmup is a private hat, then set 12651 * its private tsbinfo's flag. 12652 */ 12653 hatlockp = sfmmu_hat_enter(sfmmup); 12654 tsbinfop->tsb_flags |= TSB_RELOC_FLAG; 12655 12656 if (!(tsbinfop->tsb_flags & TSB_SHAREDCTX)) { 12657 sfmmu_tsb_inv_ctx(sfmmup); 12658 sfmmu_hat_exit(hatlockp); 12659 } else { 12660 /* release lock on the shared hat */ 12661 sfmmu_hat_exit(hatlockp); 12662 /* sfmmup is a shared hat */ 12663 ASSERT(sfmmup->sfmmu_scdhat); 12664 scdp = sfmmup->sfmmu_scdp; 12665 ASSERT(scdp != NULL); 12666 /* get private hat from the scd list */ 12667 mutex_enter(&scdp->scd_mutex); 12668 sfmmup = scdp->scd_sf_list; 12669 while (sfmmup != NULL) { 12670 hatlockp = sfmmu_hat_enter(sfmmup); 12671 /* 12672 * We do not call sfmmu_tsb_inv_ctx here because 12673 * sendmondo_in_recover check is only needed for 12674 * sun4u. 12675 */ 12676 sfmmu_invalidate_ctx(sfmmup); 12677 sfmmu_hat_exit(hatlockp); 12678 sfmmup = sfmmup->sfmmu_scd_link.next; 12679 12680 } 12681 mutex_exit(&scdp->scd_mutex); 12682 } 12683 return (0); 12684 } 12685 12686 static void 12687 sfmmu_tsb_inv_ctx(sfmmu_t *sfmmup) 12688 { 12689 extern uint32_t sendmondo_in_recover; 12690 12691 ASSERT(sfmmu_hat_lock_held(sfmmup)); 12692 12693 /* 12694 * For Cheetah+ Erratum 25: 12695 * Wait for any active recovery to finish. We can't risk 12696 * relocating the TSB of the thread running mondo_recover_proc() 12697 * since, if we did that, we would deadlock. The scenario we are 12698 * trying to avoid is as follows: 12699 * 12700 * THIS CPU RECOVER CPU 12701 * -------- ----------- 12702 * Begins recovery, walking through TSB 12703 * hat_pagesuspend() TSB TTE 12704 * TLB miss on TSB TTE, spins at TL1 12705 * xt_sync() 12706 * send_mondo_timeout() 12707 * mondo_recover_proc() 12708 * ((deadlocked)) 12709 * 12710 * The second half of the workaround is that mondo_recover_proc() 12711 * checks to see if the tsb_info has the RELOC flag set, and if it 12712 * does, it skips over that TSB without ever touching tsbinfop->tsb_va 12713 * and hence avoiding the TLB miss that could result in a deadlock. 12714 */ 12715 if (&sendmondo_in_recover) { 12716 membar_enter(); /* make sure RELOC flag visible */ 12717 while (sendmondo_in_recover) { 12718 drv_usecwait(1); 12719 membar_consumer(); 12720 } 12721 } 12722 12723 sfmmu_invalidate_ctx(sfmmup); 12724 } 12725 12726 /* ARGSUSED */ 12727 static int 12728 sfmmu_tsb_post_relocator(caddr_t va, uint_t tsbsz, uint_t flags, 12729 void *tsbinfo, pfn_t newpfn) 12730 { 12731 hatlock_t *hatlockp; 12732 struct tsb_info *tsbinfop = (struct tsb_info *)tsbinfo; 12733 sfmmu_t *sfmmup = tsbinfop->tsb_sfmmu; 12734 12735 if (flags != HAT_POSTUNSUSPEND) 12736 return (0); 12737 12738 hatlockp = sfmmu_hat_enter(sfmmup); 12739 12740 SFMMU_STAT(sf_tsb_reloc); 12741 12742 /* 12743 * The process may have swapped out while we were relocating one 12744 * of its TSBs. If so, don't bother doing the setup since the 12745 * process can't be using the memory anymore. 12746 */ 12747 if ((tsbinfop->tsb_flags & TSB_SWAPPED) == 0) { 12748 ASSERT(va == tsbinfop->tsb_va); 12749 sfmmu_tsbinfo_setup_phys(tsbinfop, newpfn); 12750 12751 if (tsbinfop->tsb_flags & TSB_FLUSH_NEEDED) { 12752 sfmmu_inv_tsb(tsbinfop->tsb_va, 12753 TSB_BYTES(tsbinfop->tsb_szc)); 12754 tsbinfop->tsb_flags &= ~TSB_FLUSH_NEEDED; 12755 } 12756 } 12757 12758 membar_exit(); 12759 tsbinfop->tsb_flags &= ~TSB_RELOC_FLAG; 12760 cv_broadcast(&sfmmup->sfmmu_tsb_cv); 12761 12762 sfmmu_hat_exit(hatlockp); 12763 12764 return (0); 12765 } 12766 12767 /* 12768 * Allocate and initialize a tsb_info structure. Note that we may or may not 12769 * allocate a TSB here, depending on the flags passed in. 12770 */ 12771 static int 12772 sfmmu_tsbinfo_alloc(struct tsb_info **tsbinfopp, int tsb_szc, int tte_sz_mask, 12773 uint_t flags, sfmmu_t *sfmmup) 12774 { 12775 int err; 12776 12777 *tsbinfopp = (struct tsb_info *)kmem_cache_alloc( 12778 sfmmu_tsbinfo_cache, KM_SLEEP); 12779 12780 if ((err = sfmmu_init_tsbinfo(*tsbinfopp, tte_sz_mask, 12781 tsb_szc, flags, sfmmup)) != 0) { 12782 kmem_cache_free(sfmmu_tsbinfo_cache, *tsbinfopp); 12783 SFMMU_STAT(sf_tsb_allocfail); 12784 *tsbinfopp = NULL; 12785 return (err); 12786 } 12787 SFMMU_STAT(sf_tsb_alloc); 12788 12789 /* 12790 * Bump the TSB size counters for this TSB size. 12791 */ 12792 (*(((int *)&sfmmu_tsbsize_stat) + tsb_szc))++; 12793 return (0); 12794 } 12795 12796 static void 12797 sfmmu_tsb_free(struct tsb_info *tsbinfo) 12798 { 12799 caddr_t tsbva = tsbinfo->tsb_va; 12800 uint_t tsb_size = TSB_BYTES(tsbinfo->tsb_szc); 12801 struct kmem_cache *kmem_cachep = tsbinfo->tsb_cache; 12802 vmem_t *vmp = tsbinfo->tsb_vmp; 12803 12804 /* 12805 * If we allocated this TSB from relocatable kernel memory, then we 12806 * need to uninstall the callback handler. 12807 */ 12808 if (tsbinfo->tsb_cache != sfmmu_tsb8k_cache) { 12809 uintptr_t slab_mask; 12810 caddr_t slab_vaddr; 12811 page_t **ppl; 12812 int ret; 12813 12814 ASSERT(tsb_size <= MMU_PAGESIZE4M || use_bigtsb_arena); 12815 if (tsb_size > MMU_PAGESIZE4M) 12816 slab_mask = ~((uintptr_t)bigtsb_slab_mask) << PAGESHIFT; 12817 else 12818 slab_mask = ~((uintptr_t)tsb_slab_mask) << PAGESHIFT; 12819 slab_vaddr = (caddr_t)((uintptr_t)tsbva & slab_mask); 12820 12821 ret = as_pagelock(&kas, &ppl, slab_vaddr, PAGESIZE, S_WRITE); 12822 ASSERT(ret == 0); 12823 hat_delete_callback(tsbva, (uint_t)tsb_size, (void *)tsbinfo, 12824 0, NULL); 12825 as_pageunlock(&kas, ppl, slab_vaddr, PAGESIZE, S_WRITE); 12826 } 12827 12828 if (kmem_cachep != NULL) { 12829 kmem_cache_free(kmem_cachep, tsbva); 12830 } else { 12831 vmem_xfree(vmp, (void *)tsbva, tsb_size); 12832 } 12833 tsbinfo->tsb_va = (caddr_t)0xbad00bad; 12834 atomic_add_64(&tsb_alloc_bytes, -(int64_t)tsb_size); 12835 } 12836 12837 static void 12838 sfmmu_tsbinfo_free(struct tsb_info *tsbinfo) 12839 { 12840 if ((tsbinfo->tsb_flags & TSB_SWAPPED) == 0) { 12841 sfmmu_tsb_free(tsbinfo); 12842 } 12843 kmem_cache_free(sfmmu_tsbinfo_cache, tsbinfo); 12844 12845 } 12846 12847 /* 12848 * Setup all the references to physical memory for this tsbinfo. 12849 * The underlying page(s) must be locked. 12850 */ 12851 static void 12852 sfmmu_tsbinfo_setup_phys(struct tsb_info *tsbinfo, pfn_t pfn) 12853 { 12854 ASSERT(pfn != PFN_INVALID); 12855 ASSERT(pfn == va_to_pfn(tsbinfo->tsb_va)); 12856 12857 #ifndef sun4v 12858 if (tsbinfo->tsb_szc == 0) { 12859 sfmmu_memtte(&tsbinfo->tsb_tte, pfn, 12860 PROT_WRITE|PROT_READ, TTE8K); 12861 } else { 12862 /* 12863 * Round down PA and use a large mapping; the handlers will 12864 * compute the TSB pointer at the correct offset into the 12865 * big virtual page. NOTE: this assumes all TSBs larger 12866 * than 8K must come from physically contiguous slabs of 12867 * size tsb_slab_size. 12868 */ 12869 sfmmu_memtte(&tsbinfo->tsb_tte, pfn & ~tsb_slab_mask, 12870 PROT_WRITE|PROT_READ, tsb_slab_ttesz); 12871 } 12872 tsbinfo->tsb_pa = ptob(pfn); 12873 12874 TTE_SET_LOCKED(&tsbinfo->tsb_tte); /* lock the tte into dtlb */ 12875 TTE_SET_MOD(&tsbinfo->tsb_tte); /* enable writes */ 12876 12877 ASSERT(TTE_IS_PRIVILEGED(&tsbinfo->tsb_tte)); 12878 ASSERT(TTE_IS_LOCKED(&tsbinfo->tsb_tte)); 12879 #else /* sun4v */ 12880 tsbinfo->tsb_pa = ptob(pfn); 12881 #endif /* sun4v */ 12882 } 12883 12884 12885 /* 12886 * Returns zero on success, ENOMEM if over the high water mark, 12887 * or EAGAIN if the caller needs to retry with a smaller TSB 12888 * size (or specify TSB_FORCEALLOC if the allocation can't fail). 12889 * 12890 * This call cannot fail to allocate a TSB if TSB_FORCEALLOC 12891 * is specified and the TSB requested is PAGESIZE, though it 12892 * may sleep waiting for memory if sufficient memory is not 12893 * available. 12894 */ 12895 static int 12896 sfmmu_init_tsbinfo(struct tsb_info *tsbinfo, int tteszmask, 12897 int tsbcode, uint_t flags, sfmmu_t *sfmmup) 12898 { 12899 caddr_t vaddr = NULL; 12900 caddr_t slab_vaddr; 12901 uintptr_t slab_mask; 12902 int tsbbytes = TSB_BYTES(tsbcode); 12903 int lowmem = 0; 12904 struct kmem_cache *kmem_cachep = NULL; 12905 vmem_t *vmp = NULL; 12906 lgrp_id_t lgrpid = LGRP_NONE; 12907 pfn_t pfn; 12908 uint_t cbflags = HAC_SLEEP; 12909 page_t **pplist; 12910 int ret; 12911 12912 ASSERT(tsbbytes <= MMU_PAGESIZE4M || use_bigtsb_arena); 12913 if (tsbbytes > MMU_PAGESIZE4M) 12914 slab_mask = ~((uintptr_t)bigtsb_slab_mask) << PAGESHIFT; 12915 else 12916 slab_mask = ~((uintptr_t)tsb_slab_mask) << PAGESHIFT; 12917 12918 if (flags & (TSB_FORCEALLOC | TSB_SWAPIN | TSB_GROW | TSB_SHRINK)) 12919 flags |= TSB_ALLOC; 12920 12921 ASSERT((flags & TSB_FORCEALLOC) == 0 || tsbcode == TSB_MIN_SZCODE); 12922 12923 tsbinfo->tsb_sfmmu = sfmmup; 12924 12925 /* 12926 * If not allocating a TSB, set up the tsbinfo, set TSB_SWAPPED, and 12927 * return. 12928 */ 12929 if ((flags & TSB_ALLOC) == 0) { 12930 tsbinfo->tsb_szc = tsbcode; 12931 tsbinfo->tsb_ttesz_mask = tteszmask; 12932 tsbinfo->tsb_va = (caddr_t)0xbadbadbeef; 12933 tsbinfo->tsb_pa = -1; 12934 tsbinfo->tsb_tte.ll = 0; 12935 tsbinfo->tsb_next = NULL; 12936 tsbinfo->tsb_flags = TSB_SWAPPED; 12937 tsbinfo->tsb_cache = NULL; 12938 tsbinfo->tsb_vmp = NULL; 12939 return (0); 12940 } 12941 12942 #ifdef DEBUG 12943 /* 12944 * For debugging: 12945 * Randomly force allocation failures every tsb_alloc_mtbf 12946 * tries if TSB_FORCEALLOC is not specified. This will 12947 * return ENOMEM if tsb_alloc_mtbf is odd, or EAGAIN if 12948 * it is even, to allow testing of both failure paths... 12949 */ 12950 if (tsb_alloc_mtbf && ((flags & TSB_FORCEALLOC) == 0) && 12951 (tsb_alloc_count++ == tsb_alloc_mtbf)) { 12952 tsb_alloc_count = 0; 12953 tsb_alloc_fail_mtbf++; 12954 return ((tsb_alloc_mtbf & 1)? ENOMEM : EAGAIN); 12955 } 12956 #endif /* DEBUG */ 12957 12958 /* 12959 * Enforce high water mark if we are not doing a forced allocation 12960 * and are not shrinking a process' TSB. 12961 */ 12962 if ((flags & TSB_SHRINK) == 0 && 12963 (tsbbytes + tsb_alloc_bytes) > tsb_alloc_hiwater) { 12964 if ((flags & TSB_FORCEALLOC) == 0) 12965 return (ENOMEM); 12966 lowmem = 1; 12967 } 12968 12969 /* 12970 * Allocate from the correct location based upon the size of the TSB 12971 * compared to the base page size, and what memory conditions dictate. 12972 * Note we always do nonblocking allocations from the TSB arena since 12973 * we don't want memory fragmentation to cause processes to block 12974 * indefinitely waiting for memory; until the kernel algorithms that 12975 * coalesce large pages are improved this is our best option. 12976 * 12977 * Algorithm: 12978 * If allocating a "large" TSB (>8K), allocate from the 12979 * appropriate kmem_tsb_default_arena vmem arena 12980 * else if low on memory or the TSB_FORCEALLOC flag is set or 12981 * tsb_forceheap is set 12982 * Allocate from kernel heap via sfmmu_tsb8k_cache with 12983 * KM_SLEEP (never fails) 12984 * else 12985 * Allocate from appropriate sfmmu_tsb_cache with 12986 * KM_NOSLEEP 12987 * endif 12988 */ 12989 if (tsb_lgrp_affinity) 12990 lgrpid = lgrp_home_id(curthread); 12991 if (lgrpid == LGRP_NONE) 12992 lgrpid = 0; /* use lgrp of boot CPU */ 12993 12994 if (tsbbytes > MMU_PAGESIZE) { 12995 if (tsbbytes > MMU_PAGESIZE4M) { 12996 vmp = kmem_bigtsb_default_arena[lgrpid]; 12997 vaddr = (caddr_t)vmem_xalloc(vmp, tsbbytes, tsbbytes, 12998 0, 0, NULL, NULL, VM_NOSLEEP); 12999 } else { 13000 vmp = kmem_tsb_default_arena[lgrpid]; 13001 vaddr = (caddr_t)vmem_xalloc(vmp, tsbbytes, tsbbytes, 13002 0, 0, NULL, NULL, VM_NOSLEEP); 13003 } 13004 #ifdef DEBUG 13005 } else if (lowmem || (flags & TSB_FORCEALLOC) || tsb_forceheap) { 13006 #else /* !DEBUG */ 13007 } else if (lowmem || (flags & TSB_FORCEALLOC)) { 13008 #endif /* DEBUG */ 13009 kmem_cachep = sfmmu_tsb8k_cache; 13010 vaddr = (caddr_t)kmem_cache_alloc(kmem_cachep, KM_SLEEP); 13011 ASSERT(vaddr != NULL); 13012 } else { 13013 kmem_cachep = sfmmu_tsb_cache[lgrpid]; 13014 vaddr = (caddr_t)kmem_cache_alloc(kmem_cachep, KM_NOSLEEP); 13015 } 13016 13017 tsbinfo->tsb_cache = kmem_cachep; 13018 tsbinfo->tsb_vmp = vmp; 13019 13020 if (vaddr == NULL) { 13021 return (EAGAIN); 13022 } 13023 13024 atomic_add_64(&tsb_alloc_bytes, (int64_t)tsbbytes); 13025 kmem_cachep = tsbinfo->tsb_cache; 13026 13027 /* 13028 * If we are allocating from outside the cage, then we need to 13029 * register a relocation callback handler. Note that for now 13030 * since pseudo mappings always hang off of the slab's root page, 13031 * we need only lock the first 8K of the TSB slab. This is a bit 13032 * hacky but it is good for performance. 13033 */ 13034 if (kmem_cachep != sfmmu_tsb8k_cache) { 13035 slab_vaddr = (caddr_t)((uintptr_t)vaddr & slab_mask); 13036 ret = as_pagelock(&kas, &pplist, slab_vaddr, PAGESIZE, S_WRITE); 13037 ASSERT(ret == 0); 13038 ret = hat_add_callback(sfmmu_tsb_cb_id, vaddr, (uint_t)tsbbytes, 13039 cbflags, (void *)tsbinfo, &pfn, NULL); 13040 13041 /* 13042 * Need to free up resources if we could not successfully 13043 * add the callback function and return an error condition. 13044 */ 13045 if (ret != 0) { 13046 if (kmem_cachep) { 13047 kmem_cache_free(kmem_cachep, vaddr); 13048 } else { 13049 vmem_xfree(vmp, (void *)vaddr, tsbbytes); 13050 } 13051 as_pageunlock(&kas, pplist, slab_vaddr, PAGESIZE, 13052 S_WRITE); 13053 return (EAGAIN); 13054 } 13055 } else { 13056 /* 13057 * Since allocation of 8K TSBs from heap is rare and occurs 13058 * during memory pressure we allocate them from permanent 13059 * memory rather than using callbacks to get the PFN. 13060 */ 13061 pfn = hat_getpfnum(kas.a_hat, vaddr); 13062 } 13063 13064 tsbinfo->tsb_va = vaddr; 13065 tsbinfo->tsb_szc = tsbcode; 13066 tsbinfo->tsb_ttesz_mask = tteszmask; 13067 tsbinfo->tsb_next = NULL; 13068 tsbinfo->tsb_flags = 0; 13069 13070 sfmmu_tsbinfo_setup_phys(tsbinfo, pfn); 13071 13072 sfmmu_inv_tsb(vaddr, tsbbytes); 13073 13074 if (kmem_cachep != sfmmu_tsb8k_cache) { 13075 as_pageunlock(&kas, pplist, slab_vaddr, PAGESIZE, S_WRITE); 13076 } 13077 13078 return (0); 13079 } 13080 13081 /* 13082 * Initialize per cpu tsb and per cpu tsbmiss_area 13083 */ 13084 void 13085 sfmmu_init_tsbs(void) 13086 { 13087 int i; 13088 struct tsbmiss *tsbmissp; 13089 struct kpmtsbm *kpmtsbmp; 13090 #ifndef sun4v 13091 extern int dcache_line_mask; 13092 #endif /* sun4v */ 13093 extern uint_t vac_colors; 13094 13095 /* 13096 * Init. tsb miss area. 13097 */ 13098 tsbmissp = tsbmiss_area; 13099 13100 for (i = 0; i < NCPU; tsbmissp++, i++) { 13101 /* 13102 * initialize the tsbmiss area. 13103 * Do this for all possible CPUs as some may be added 13104 * while the system is running. There is no cost to this. 13105 */ 13106 tsbmissp->ksfmmup = ksfmmup; 13107 #ifndef sun4v 13108 tsbmissp->dcache_line_mask = (uint16_t)dcache_line_mask; 13109 #endif /* sun4v */ 13110 tsbmissp->khashstart = 13111 (struct hmehash_bucket *)va_to_pa((caddr_t)khme_hash); 13112 tsbmissp->uhashstart = 13113 (struct hmehash_bucket *)va_to_pa((caddr_t)uhme_hash); 13114 tsbmissp->khashsz = khmehash_num; 13115 tsbmissp->uhashsz = uhmehash_num; 13116 } 13117 13118 sfmmu_tsb_cb_id = hat_register_callback('T'<<16 | 'S' << 8 | 'B', 13119 sfmmu_tsb_pre_relocator, sfmmu_tsb_post_relocator, NULL, 0); 13120 13121 if (kpm_enable == 0) 13122 return; 13123 13124 /* -- Begin KPM specific init -- */ 13125 13126 if (kpm_smallpages) { 13127 /* 13128 * If we're using base pagesize pages for seg_kpm 13129 * mappings, we use the kernel TSB since we can't afford 13130 * to allocate a second huge TSB for these mappings. 13131 */ 13132 kpm_tsbbase = ktsb_phys? ktsb_pbase : (uint64_t)ktsb_base; 13133 kpm_tsbsz = ktsb_szcode; 13134 kpmsm_tsbbase = kpm_tsbbase; 13135 kpmsm_tsbsz = kpm_tsbsz; 13136 } else { 13137 /* 13138 * In VAC conflict case, just put the entries in the 13139 * kernel 8K indexed TSB for now so we can find them. 13140 * This could really be changed in the future if we feel 13141 * the need... 13142 */ 13143 kpmsm_tsbbase = ktsb_phys? ktsb_pbase : (uint64_t)ktsb_base; 13144 kpmsm_tsbsz = ktsb_szcode; 13145 kpm_tsbbase = ktsb_phys? ktsb4m_pbase : (uint64_t)ktsb4m_base; 13146 kpm_tsbsz = ktsb4m_szcode; 13147 } 13148 13149 kpmtsbmp = kpmtsbm_area; 13150 for (i = 0; i < NCPU; kpmtsbmp++, i++) { 13151 /* 13152 * Initialize the kpmtsbm area. 13153 * Do this for all possible CPUs as some may be added 13154 * while the system is running. There is no cost to this. 13155 */ 13156 kpmtsbmp->vbase = kpm_vbase; 13157 kpmtsbmp->vend = kpm_vbase + kpm_size * vac_colors; 13158 kpmtsbmp->sz_shift = kpm_size_shift; 13159 kpmtsbmp->kpmp_shift = kpmp_shift; 13160 kpmtsbmp->kpmp2pshft = (uchar_t)kpmp2pshft; 13161 if (kpm_smallpages == 0) { 13162 kpmtsbmp->kpmp_table_sz = kpmp_table_sz; 13163 kpmtsbmp->kpmp_tablepa = va_to_pa(kpmp_table); 13164 } else { 13165 kpmtsbmp->kpmp_table_sz = kpmp_stable_sz; 13166 kpmtsbmp->kpmp_tablepa = va_to_pa(kpmp_stable); 13167 } 13168 kpmtsbmp->msegphashpa = va_to_pa(memseg_phash); 13169 kpmtsbmp->flags = KPMTSBM_ENABLE_FLAG; 13170 #ifdef DEBUG 13171 kpmtsbmp->flags |= (kpm_tsbmtl) ? KPMTSBM_TLTSBM_FLAG : 0; 13172 #endif /* DEBUG */ 13173 if (ktsb_phys) 13174 kpmtsbmp->flags |= KPMTSBM_TSBPHYS_FLAG; 13175 } 13176 13177 /* -- End KPM specific init -- */ 13178 } 13179 13180 /* Avoid using sfmmu_tsbinfo_alloc() to avoid kmem_alloc - no real reason */ 13181 struct tsb_info ktsb_info[2]; 13182 13183 /* 13184 * Called from hat_kern_setup() to setup the tsb_info for ksfmmup. 13185 */ 13186 void 13187 sfmmu_init_ktsbinfo() 13188 { 13189 ASSERT(ksfmmup != NULL); 13190 ASSERT(ksfmmup->sfmmu_tsb == NULL); 13191 /* 13192 * Allocate tsbinfos for kernel and copy in data 13193 * to make debug easier and sun4v setup easier. 13194 */ 13195 ktsb_info[0].tsb_sfmmu = ksfmmup; 13196 ktsb_info[0].tsb_szc = ktsb_szcode; 13197 ktsb_info[0].tsb_ttesz_mask = TSB8K|TSB64K|TSB512K; 13198 ktsb_info[0].tsb_va = ktsb_base; 13199 ktsb_info[0].tsb_pa = ktsb_pbase; 13200 ktsb_info[0].tsb_flags = 0; 13201 ktsb_info[0].tsb_tte.ll = 0; 13202 ktsb_info[0].tsb_cache = NULL; 13203 13204 ktsb_info[1].tsb_sfmmu = ksfmmup; 13205 ktsb_info[1].tsb_szc = ktsb4m_szcode; 13206 ktsb_info[1].tsb_ttesz_mask = TSB4M; 13207 ktsb_info[1].tsb_va = ktsb4m_base; 13208 ktsb_info[1].tsb_pa = ktsb4m_pbase; 13209 ktsb_info[1].tsb_flags = 0; 13210 ktsb_info[1].tsb_tte.ll = 0; 13211 ktsb_info[1].tsb_cache = NULL; 13212 13213 /* Link them into ksfmmup. */ 13214 ktsb_info[0].tsb_next = &ktsb_info[1]; 13215 ktsb_info[1].tsb_next = NULL; 13216 ksfmmup->sfmmu_tsb = &ktsb_info[0]; 13217 13218 sfmmu_setup_tsbinfo(ksfmmup); 13219 } 13220 13221 /* 13222 * Cache the last value returned from va_to_pa(). If the VA specified 13223 * in the current call to cached_va_to_pa() maps to the same Page (as the 13224 * previous call to cached_va_to_pa()), then compute the PA using 13225 * cached info, else call va_to_pa(). 13226 * 13227 * Note: this function is neither MT-safe nor consistent in the presence 13228 * of multiple, interleaved threads. This function was created to enable 13229 * an optimization used during boot (at a point when there's only one thread 13230 * executing on the "boot CPU", and before startup_vm() has been called). 13231 */ 13232 static uint64_t 13233 cached_va_to_pa(void *vaddr) 13234 { 13235 static uint64_t prev_vaddr_base = 0; 13236 static uint64_t prev_pfn = 0; 13237 13238 if ((((uint64_t)vaddr) & MMU_PAGEMASK) == prev_vaddr_base) { 13239 return (prev_pfn | ((uint64_t)vaddr & MMU_PAGEOFFSET)); 13240 } else { 13241 uint64_t pa = va_to_pa(vaddr); 13242 13243 if (pa != ((uint64_t)-1)) { 13244 /* 13245 * Computed physical address is valid. Cache its 13246 * related info for the next cached_va_to_pa() call. 13247 */ 13248 prev_pfn = pa & MMU_PAGEMASK; 13249 prev_vaddr_base = ((uint64_t)vaddr) & MMU_PAGEMASK; 13250 } 13251 13252 return (pa); 13253 } 13254 } 13255 13256 /* 13257 * Carve up our nucleus hblk region. We may allocate more hblks than 13258 * asked due to rounding errors but we are guaranteed to have at least 13259 * enough space to allocate the requested number of hblk8's and hblk1's. 13260 */ 13261 void 13262 sfmmu_init_nucleus_hblks(caddr_t addr, size_t size, int nhblk8, int nhblk1) 13263 { 13264 struct hme_blk *hmeblkp; 13265 size_t hme8blk_sz, hme1blk_sz; 13266 size_t i; 13267 size_t hblk8_bound; 13268 ulong_t j = 0, k = 0; 13269 13270 ASSERT(addr != NULL && size != 0); 13271 13272 /* Need to use proper structure alignment */ 13273 hme8blk_sz = roundup(HME8BLK_SZ, sizeof (int64_t)); 13274 hme1blk_sz = roundup(HME1BLK_SZ, sizeof (int64_t)); 13275 13276 nucleus_hblk8.list = (void *)addr; 13277 nucleus_hblk8.index = 0; 13278 13279 /* 13280 * Use as much memory as possible for hblk8's since we 13281 * expect all bop_alloc'ed memory to be allocated in 8k chunks. 13282 * We need to hold back enough space for the hblk1's which 13283 * we'll allocate next. 13284 */ 13285 hblk8_bound = size - (nhblk1 * hme1blk_sz) - hme8blk_sz; 13286 for (i = 0; i <= hblk8_bound; i += hme8blk_sz, j++) { 13287 hmeblkp = (struct hme_blk *)addr; 13288 addr += hme8blk_sz; 13289 hmeblkp->hblk_nuc_bit = 1; 13290 hmeblkp->hblk_nextpa = cached_va_to_pa((caddr_t)hmeblkp); 13291 } 13292 nucleus_hblk8.len = j; 13293 ASSERT(j >= nhblk8); 13294 SFMMU_STAT_ADD(sf_hblk8_ncreate, j); 13295 13296 nucleus_hblk1.list = (void *)addr; 13297 nucleus_hblk1.index = 0; 13298 for (; i <= (size - hme1blk_sz); i += hme1blk_sz, k++) { 13299 hmeblkp = (struct hme_blk *)addr; 13300 addr += hme1blk_sz; 13301 hmeblkp->hblk_nuc_bit = 1; 13302 hmeblkp->hblk_nextpa = cached_va_to_pa((caddr_t)hmeblkp); 13303 } 13304 ASSERT(k >= nhblk1); 13305 nucleus_hblk1.len = k; 13306 SFMMU_STAT_ADD(sf_hblk1_ncreate, k); 13307 } 13308 13309 /* 13310 * This function is currently not supported on this platform. For what 13311 * it's supposed to do, see hat.c and hat_srmmu.c 13312 */ 13313 /* ARGSUSED */ 13314 faultcode_t 13315 hat_softlock(struct hat *hat, caddr_t addr, size_t *lenp, page_t **ppp, 13316 uint_t flags) 13317 { 13318 ASSERT(hat->sfmmu_xhat_provider == NULL); 13319 return (FC_NOSUPPORT); 13320 } 13321 13322 /* 13323 * Searchs the mapping list of the page for a mapping of the same size. If not 13324 * found the corresponding bit is cleared in the p_index field. When large 13325 * pages are more prevalent in the system, we can maintain the mapping list 13326 * in order and we don't have to traverse the list each time. Just check the 13327 * next and prev entries, and if both are of different size, we clear the bit. 13328 */ 13329 static void 13330 sfmmu_rm_large_mappings(page_t *pp, int ttesz) 13331 { 13332 struct sf_hment *sfhmep; 13333 struct hme_blk *hmeblkp; 13334 int index; 13335 pgcnt_t npgs; 13336 13337 ASSERT(ttesz > TTE8K); 13338 13339 ASSERT(sfmmu_mlist_held(pp)); 13340 13341 ASSERT(PP_ISMAPPED_LARGE(pp)); 13342 13343 /* 13344 * Traverse mapping list looking for another mapping of same size. 13345 * since we only want to clear index field if all mappings of 13346 * that size are gone. 13347 */ 13348 13349 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) { 13350 if (IS_PAHME(sfhmep)) 13351 continue; 13352 hmeblkp = sfmmu_hmetohblk(sfhmep); 13353 if (hmeblkp->hblk_xhat_bit) 13354 continue; 13355 if (hme_size(sfhmep) == ttesz) { 13356 /* 13357 * another mapping of the same size. don't clear index. 13358 */ 13359 return; 13360 } 13361 } 13362 13363 /* 13364 * Clear the p_index bit for large page. 13365 */ 13366 index = PAGESZ_TO_INDEX(ttesz); 13367 npgs = TTEPAGES(ttesz); 13368 while (npgs-- > 0) { 13369 ASSERT(pp->p_index & index); 13370 pp->p_index &= ~index; 13371 pp = PP_PAGENEXT(pp); 13372 } 13373 } 13374 13375 /* 13376 * return supported features 13377 */ 13378 /* ARGSUSED */ 13379 int 13380 hat_supported(enum hat_features feature, void *arg) 13381 { 13382 switch (feature) { 13383 case HAT_SHARED_PT: 13384 case HAT_DYNAMIC_ISM_UNMAP: 13385 case HAT_VMODSORT: 13386 return (1); 13387 case HAT_SHARED_REGIONS: 13388 if (shctx_on) 13389 return (1); 13390 else 13391 return (0); 13392 default: 13393 return (0); 13394 } 13395 } 13396 13397 void 13398 hat_enter(struct hat *hat) 13399 { 13400 hatlock_t *hatlockp; 13401 13402 if (hat != ksfmmup) { 13403 hatlockp = TSB_HASH(hat); 13404 mutex_enter(HATLOCK_MUTEXP(hatlockp)); 13405 } 13406 } 13407 13408 void 13409 hat_exit(struct hat *hat) 13410 { 13411 hatlock_t *hatlockp; 13412 13413 if (hat != ksfmmup) { 13414 hatlockp = TSB_HASH(hat); 13415 mutex_exit(HATLOCK_MUTEXP(hatlockp)); 13416 } 13417 } 13418 13419 /*ARGSUSED*/ 13420 void 13421 hat_reserve(struct as *as, caddr_t addr, size_t len) 13422 { 13423 } 13424 13425 static void 13426 hat_kstat_init(void) 13427 { 13428 kstat_t *ksp; 13429 13430 ksp = kstat_create("unix", 0, "sfmmu_global_stat", "hat", 13431 KSTAT_TYPE_RAW, sizeof (struct sfmmu_global_stat), 13432 KSTAT_FLAG_VIRTUAL); 13433 if (ksp) { 13434 ksp->ks_data = (void *) &sfmmu_global_stat; 13435 kstat_install(ksp); 13436 } 13437 ksp = kstat_create("unix", 0, "sfmmu_tsbsize_stat", "hat", 13438 KSTAT_TYPE_RAW, sizeof (struct sfmmu_tsbsize_stat), 13439 KSTAT_FLAG_VIRTUAL); 13440 if (ksp) { 13441 ksp->ks_data = (void *) &sfmmu_tsbsize_stat; 13442 kstat_install(ksp); 13443 } 13444 ksp = kstat_create("unix", 0, "sfmmu_percpu_stat", "hat", 13445 KSTAT_TYPE_RAW, sizeof (struct sfmmu_percpu_stat) * NCPU, 13446 KSTAT_FLAG_WRITABLE); 13447 if (ksp) { 13448 ksp->ks_update = sfmmu_kstat_percpu_update; 13449 kstat_install(ksp); 13450 } 13451 } 13452 13453 /* ARGSUSED */ 13454 static int 13455 sfmmu_kstat_percpu_update(kstat_t *ksp, int rw) 13456 { 13457 struct sfmmu_percpu_stat *cpu_kstat = ksp->ks_data; 13458 struct tsbmiss *tsbm = tsbmiss_area; 13459 struct kpmtsbm *kpmtsbm = kpmtsbm_area; 13460 int i; 13461 13462 ASSERT(cpu_kstat); 13463 if (rw == KSTAT_READ) { 13464 for (i = 0; i < NCPU; cpu_kstat++, tsbm++, kpmtsbm++, i++) { 13465 cpu_kstat->sf_itlb_misses = 0; 13466 cpu_kstat->sf_dtlb_misses = 0; 13467 cpu_kstat->sf_utsb_misses = tsbm->utsb_misses - 13468 tsbm->uprot_traps; 13469 cpu_kstat->sf_ktsb_misses = tsbm->ktsb_misses + 13470 kpmtsbm->kpm_tsb_misses - tsbm->kprot_traps; 13471 cpu_kstat->sf_tsb_hits = 0; 13472 cpu_kstat->sf_umod_faults = tsbm->uprot_traps; 13473 cpu_kstat->sf_kmod_faults = tsbm->kprot_traps; 13474 } 13475 } else { 13476 /* KSTAT_WRITE is used to clear stats */ 13477 for (i = 0; i < NCPU; tsbm++, kpmtsbm++, i++) { 13478 tsbm->utsb_misses = 0; 13479 tsbm->ktsb_misses = 0; 13480 tsbm->uprot_traps = 0; 13481 tsbm->kprot_traps = 0; 13482 kpmtsbm->kpm_dtlb_misses = 0; 13483 kpmtsbm->kpm_tsb_misses = 0; 13484 } 13485 } 13486 return (0); 13487 } 13488 13489 #ifdef DEBUG 13490 13491 tte_t *gorig[NCPU], *gcur[NCPU], *gnew[NCPU]; 13492 13493 /* 13494 * A tte checker. *orig_old is the value we read before cas. 13495 * *cur is the value returned by cas. 13496 * *new is the desired value when we do the cas. 13497 * 13498 * *hmeblkp is currently unused. 13499 */ 13500 13501 /* ARGSUSED */ 13502 void 13503 chk_tte(tte_t *orig_old, tte_t *cur, tte_t *new, struct hme_blk *hmeblkp) 13504 { 13505 pfn_t i, j, k; 13506 int cpuid = CPU->cpu_id; 13507 13508 gorig[cpuid] = orig_old; 13509 gcur[cpuid] = cur; 13510 gnew[cpuid] = new; 13511 13512 #ifdef lint 13513 hmeblkp = hmeblkp; 13514 #endif 13515 13516 if (TTE_IS_VALID(orig_old)) { 13517 if (TTE_IS_VALID(cur)) { 13518 i = TTE_TO_TTEPFN(orig_old); 13519 j = TTE_TO_TTEPFN(cur); 13520 k = TTE_TO_TTEPFN(new); 13521 if (i != j) { 13522 /* remap error? */ 13523 panic("chk_tte: bad pfn, 0x%lx, 0x%lx", i, j); 13524 } 13525 13526 if (i != k) { 13527 /* remap error? */ 13528 panic("chk_tte: bad pfn2, 0x%lx, 0x%lx", i, k); 13529 } 13530 } else { 13531 if (TTE_IS_VALID(new)) { 13532 panic("chk_tte: invalid cur? "); 13533 } 13534 13535 i = TTE_TO_TTEPFN(orig_old); 13536 k = TTE_TO_TTEPFN(new); 13537 if (i != k) { 13538 panic("chk_tte: bad pfn3, 0x%lx, 0x%lx", i, k); 13539 } 13540 } 13541 } else { 13542 if (TTE_IS_VALID(cur)) { 13543 j = TTE_TO_TTEPFN(cur); 13544 if (TTE_IS_VALID(new)) { 13545 k = TTE_TO_TTEPFN(new); 13546 if (j != k) { 13547 panic("chk_tte: bad pfn4, 0x%lx, 0x%lx", 13548 j, k); 13549 } 13550 } else { 13551 panic("chk_tte: why here?"); 13552 } 13553 } else { 13554 if (!TTE_IS_VALID(new)) { 13555 panic("chk_tte: why here2 ?"); 13556 } 13557 } 13558 } 13559 } 13560 13561 #endif /* DEBUG */ 13562 13563 extern void prefetch_tsbe_read(struct tsbe *); 13564 extern void prefetch_tsbe_write(struct tsbe *); 13565 13566 13567 /* 13568 * We want to prefetch 7 cache lines ahead for our read prefetch. This gives 13569 * us optimal performance on Cheetah+. You can only have 8 outstanding 13570 * prefetches at any one time, so we opted for 7 read prefetches and 1 write 13571 * prefetch to make the most utilization of the prefetch capability. 13572 */ 13573 #define TSBE_PREFETCH_STRIDE (7) 13574 13575 void 13576 sfmmu_copy_tsb(struct tsb_info *old_tsbinfo, struct tsb_info *new_tsbinfo) 13577 { 13578 int old_bytes = TSB_BYTES(old_tsbinfo->tsb_szc); 13579 int new_bytes = TSB_BYTES(new_tsbinfo->tsb_szc); 13580 int old_entries = TSB_ENTRIES(old_tsbinfo->tsb_szc); 13581 int new_entries = TSB_ENTRIES(new_tsbinfo->tsb_szc); 13582 struct tsbe *old; 13583 struct tsbe *new; 13584 struct tsbe *new_base = (struct tsbe *)new_tsbinfo->tsb_va; 13585 uint64_t va; 13586 int new_offset; 13587 int i; 13588 int vpshift; 13589 int last_prefetch; 13590 13591 if (old_bytes == new_bytes) { 13592 bcopy(old_tsbinfo->tsb_va, new_tsbinfo->tsb_va, new_bytes); 13593 } else { 13594 13595 /* 13596 * A TSBE is 16 bytes which means there are four TSBE's per 13597 * P$ line (64 bytes), thus every 4 TSBE's we prefetch. 13598 */ 13599 old = (struct tsbe *)old_tsbinfo->tsb_va; 13600 last_prefetch = old_entries - (4*(TSBE_PREFETCH_STRIDE+1)); 13601 for (i = 0; i < old_entries; i++, old++) { 13602 if (((i & (4-1)) == 0) && (i < last_prefetch)) 13603 prefetch_tsbe_read(old); 13604 if (!old->tte_tag.tag_invalid) { 13605 /* 13606 * We have a valid TTE to remap. Check the 13607 * size. We won't remap 64K or 512K TTEs 13608 * because they span more than one TSB entry 13609 * and are indexed using an 8K virt. page. 13610 * Ditto for 32M and 256M TTEs. 13611 */ 13612 if (TTE_CSZ(&old->tte_data) == TTE64K || 13613 TTE_CSZ(&old->tte_data) == TTE512K) 13614 continue; 13615 if (mmu_page_sizes == max_mmu_page_sizes) { 13616 if (TTE_CSZ(&old->tte_data) == TTE32M || 13617 TTE_CSZ(&old->tte_data) == TTE256M) 13618 continue; 13619 } 13620 13621 /* clear the lower 22 bits of the va */ 13622 va = *(uint64_t *)old << 22; 13623 /* turn va into a virtual pfn */ 13624 va >>= 22 - TSB_START_SIZE; 13625 /* 13626 * or in bits from the offset in the tsb 13627 * to get the real virtual pfn. These 13628 * correspond to bits [21:13] in the va 13629 */ 13630 vpshift = 13631 TTE_BSZS_SHIFT(TTE_CSZ(&old->tte_data)) & 13632 0x1ff; 13633 va |= (i << vpshift); 13634 va >>= vpshift; 13635 new_offset = va & (new_entries - 1); 13636 new = new_base + new_offset; 13637 prefetch_tsbe_write(new); 13638 *new = *old; 13639 } 13640 } 13641 } 13642 } 13643 13644 /* 13645 * unused in sfmmu 13646 */ 13647 void 13648 hat_dump(void) 13649 { 13650 } 13651 13652 /* 13653 * Called when a thread is exiting and we have switched to the kernel address 13654 * space. Perform the same VM initialization resume() uses when switching 13655 * processes. 13656 * 13657 * Note that sfmmu_load_mmustate() is currently a no-op for kernel threads, but 13658 * we call it anyway in case the semantics change in the future. 13659 */ 13660 /*ARGSUSED*/ 13661 void 13662 hat_thread_exit(kthread_t *thd) 13663 { 13664 uint_t pgsz_cnum; 13665 uint_t pstate_save; 13666 13667 ASSERT(thd->t_procp->p_as == &kas); 13668 13669 pgsz_cnum = KCONTEXT; 13670 #ifdef sun4u 13671 pgsz_cnum |= (ksfmmup->sfmmu_cext << CTXREG_EXT_SHIFT); 13672 #endif 13673 13674 /* 13675 * Note that sfmmu_load_mmustate() is currently a no-op for 13676 * kernel threads. We need to disable interrupts here, 13677 * simply because otherwise sfmmu_load_mmustate() would panic 13678 * if the caller does not disable interrupts. 13679 */ 13680 pstate_save = sfmmu_disable_intrs(); 13681 13682 /* Compatibility Note: hw takes care of MMU_SCONTEXT1 */ 13683 sfmmu_setctx_sec(pgsz_cnum); 13684 sfmmu_load_mmustate(ksfmmup); 13685 sfmmu_enable_intrs(pstate_save); 13686 } 13687 13688 13689 /* 13690 * SRD support 13691 */ 13692 #define SRD_HASH_FUNCTION(vp) (((((uintptr_t)(vp)) >> 4) ^ \ 13693 (((uintptr_t)(vp)) >> 11)) & \ 13694 srd_hashmask) 13695 13696 /* 13697 * Attach the process to the srd struct associated with the exec vnode 13698 * from which the process is started. 13699 */ 13700 void 13701 hat_join_srd(struct hat *sfmmup, vnode_t *evp) 13702 { 13703 uint_t hash = SRD_HASH_FUNCTION(evp); 13704 sf_srd_t *srdp; 13705 sf_srd_t *newsrdp; 13706 13707 ASSERT(sfmmup != ksfmmup); 13708 ASSERT(sfmmup->sfmmu_srdp == NULL); 13709 13710 if (!shctx_on) { 13711 return; 13712 } 13713 13714 VN_HOLD(evp); 13715 13716 if (srd_buckets[hash].srdb_srdp != NULL) { 13717 mutex_enter(&srd_buckets[hash].srdb_lock); 13718 for (srdp = srd_buckets[hash].srdb_srdp; srdp != NULL; 13719 srdp = srdp->srd_hash) { 13720 if (srdp->srd_evp == evp) { 13721 ASSERT(srdp->srd_refcnt >= 0); 13722 sfmmup->sfmmu_srdp = srdp; 13723 atomic_add_32( 13724 (volatile uint_t *)&srdp->srd_refcnt, 1); 13725 mutex_exit(&srd_buckets[hash].srdb_lock); 13726 return; 13727 } 13728 } 13729 mutex_exit(&srd_buckets[hash].srdb_lock); 13730 } 13731 newsrdp = kmem_cache_alloc(srd_cache, KM_SLEEP); 13732 ASSERT(newsrdp->srd_next_ismrid == 0 && newsrdp->srd_next_hmerid == 0); 13733 13734 newsrdp->srd_evp = evp; 13735 newsrdp->srd_refcnt = 1; 13736 newsrdp->srd_hmergnfree = NULL; 13737 newsrdp->srd_ismrgnfree = NULL; 13738 13739 mutex_enter(&srd_buckets[hash].srdb_lock); 13740 for (srdp = srd_buckets[hash].srdb_srdp; srdp != NULL; 13741 srdp = srdp->srd_hash) { 13742 if (srdp->srd_evp == evp) { 13743 ASSERT(srdp->srd_refcnt >= 0); 13744 sfmmup->sfmmu_srdp = srdp; 13745 atomic_add_32((volatile uint_t *)&srdp->srd_refcnt, 1); 13746 mutex_exit(&srd_buckets[hash].srdb_lock); 13747 kmem_cache_free(srd_cache, newsrdp); 13748 return; 13749 } 13750 } 13751 newsrdp->srd_hash = srd_buckets[hash].srdb_srdp; 13752 srd_buckets[hash].srdb_srdp = newsrdp; 13753 sfmmup->sfmmu_srdp = newsrdp; 13754 13755 mutex_exit(&srd_buckets[hash].srdb_lock); 13756 13757 } 13758 13759 static void 13760 sfmmu_leave_srd(sfmmu_t *sfmmup) 13761 { 13762 vnode_t *evp; 13763 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 13764 uint_t hash; 13765 sf_srd_t **prev_srdpp; 13766 sf_region_t *rgnp; 13767 sf_region_t *nrgnp; 13768 #ifdef DEBUG 13769 int rgns = 0; 13770 #endif 13771 int i; 13772 13773 ASSERT(sfmmup != ksfmmup); 13774 ASSERT(srdp != NULL); 13775 ASSERT(srdp->srd_refcnt > 0); 13776 ASSERT(sfmmup->sfmmu_scdp == NULL); 13777 ASSERT(sfmmup->sfmmu_free == 1); 13778 13779 sfmmup->sfmmu_srdp = NULL; 13780 evp = srdp->srd_evp; 13781 ASSERT(evp != NULL); 13782 if (atomic_add_32_nv( 13783 (volatile uint_t *)&srdp->srd_refcnt, -1)) { 13784 VN_RELE(evp); 13785 return; 13786 } 13787 13788 hash = SRD_HASH_FUNCTION(evp); 13789 mutex_enter(&srd_buckets[hash].srdb_lock); 13790 for (prev_srdpp = &srd_buckets[hash].srdb_srdp; 13791 (srdp = *prev_srdpp) != NULL; prev_srdpp = &srdp->srd_hash) { 13792 if (srdp->srd_evp == evp) { 13793 break; 13794 } 13795 } 13796 if (srdp == NULL || srdp->srd_refcnt) { 13797 mutex_exit(&srd_buckets[hash].srdb_lock); 13798 VN_RELE(evp); 13799 return; 13800 } 13801 *prev_srdpp = srdp->srd_hash; 13802 mutex_exit(&srd_buckets[hash].srdb_lock); 13803 13804 ASSERT(srdp->srd_refcnt == 0); 13805 VN_RELE(evp); 13806 13807 #ifdef DEBUG 13808 for (i = 0; i < SFMMU_MAX_REGION_BUCKETS; i++) { 13809 ASSERT(srdp->srd_rgnhash[i] == NULL); 13810 } 13811 #endif /* DEBUG */ 13812 13813 /* free each hme regions in the srd */ 13814 for (rgnp = srdp->srd_hmergnfree; rgnp != NULL; rgnp = nrgnp) { 13815 nrgnp = rgnp->rgn_next; 13816 ASSERT(rgnp->rgn_id < srdp->srd_next_hmerid); 13817 ASSERT(rgnp->rgn_refcnt == 0); 13818 ASSERT(rgnp->rgn_sfmmu_head == NULL); 13819 ASSERT(rgnp->rgn_flags & SFMMU_REGION_FREE); 13820 ASSERT(rgnp->rgn_hmeflags == 0); 13821 ASSERT(srdp->srd_hmergnp[rgnp->rgn_id] == rgnp); 13822 #ifdef DEBUG 13823 for (i = 0; i < MMU_PAGE_SIZES; i++) { 13824 ASSERT(rgnp->rgn_ttecnt[i] == 0); 13825 } 13826 rgns++; 13827 #endif /* DEBUG */ 13828 kmem_cache_free(region_cache, rgnp); 13829 } 13830 ASSERT(rgns == srdp->srd_next_hmerid); 13831 13832 #ifdef DEBUG 13833 rgns = 0; 13834 #endif 13835 /* free each ism rgns in the srd */ 13836 for (rgnp = srdp->srd_ismrgnfree; rgnp != NULL; rgnp = nrgnp) { 13837 nrgnp = rgnp->rgn_next; 13838 ASSERT(rgnp->rgn_id < srdp->srd_next_ismrid); 13839 ASSERT(rgnp->rgn_refcnt == 0); 13840 ASSERT(rgnp->rgn_sfmmu_head == NULL); 13841 ASSERT(rgnp->rgn_flags & SFMMU_REGION_FREE); 13842 ASSERT(srdp->srd_ismrgnp[rgnp->rgn_id] == rgnp); 13843 #ifdef DEBUG 13844 for (i = 0; i < MMU_PAGE_SIZES; i++) { 13845 ASSERT(rgnp->rgn_ttecnt[i] == 0); 13846 } 13847 rgns++; 13848 #endif /* DEBUG */ 13849 kmem_cache_free(region_cache, rgnp); 13850 } 13851 ASSERT(rgns == srdp->srd_next_ismrid); 13852 ASSERT(srdp->srd_ismbusyrgns == 0); 13853 ASSERT(srdp->srd_hmebusyrgns == 0); 13854 13855 srdp->srd_next_ismrid = 0; 13856 srdp->srd_next_hmerid = 0; 13857 13858 bzero((void *)srdp->srd_ismrgnp, 13859 sizeof (sf_region_t *) * SFMMU_MAX_ISM_REGIONS); 13860 bzero((void *)srdp->srd_hmergnp, 13861 sizeof (sf_region_t *) * SFMMU_MAX_HME_REGIONS); 13862 13863 ASSERT(srdp->srd_scdp == NULL); 13864 kmem_cache_free(srd_cache, srdp); 13865 } 13866 13867 /* ARGSUSED */ 13868 static int 13869 sfmmu_srdcache_constructor(void *buf, void *cdrarg, int kmflags) 13870 { 13871 sf_srd_t *srdp = (sf_srd_t *)buf; 13872 bzero(buf, sizeof (*srdp)); 13873 13874 mutex_init(&srdp->srd_mutex, NULL, MUTEX_DEFAULT, NULL); 13875 mutex_init(&srdp->srd_scd_mutex, NULL, MUTEX_DEFAULT, NULL); 13876 return (0); 13877 } 13878 13879 /* ARGSUSED */ 13880 static void 13881 sfmmu_srdcache_destructor(void *buf, void *cdrarg) 13882 { 13883 sf_srd_t *srdp = (sf_srd_t *)buf; 13884 13885 mutex_destroy(&srdp->srd_mutex); 13886 mutex_destroy(&srdp->srd_scd_mutex); 13887 } 13888 13889 /* 13890 * The caller makes sure hat_join_region()/hat_leave_region() can't be called 13891 * at the same time for the same process and address range. This is ensured by 13892 * the fact that address space is locked as writer when a process joins the 13893 * regions. Therefore there's no need to hold an srd lock during the entire 13894 * execution of hat_join_region()/hat_leave_region(). 13895 */ 13896 13897 #define RGN_HASH_FUNCTION(obj) (((((uintptr_t)(obj)) >> 4) ^ \ 13898 (((uintptr_t)(obj)) >> 11)) & \ 13899 srd_rgn_hashmask) 13900 /* 13901 * This routine implements the shared context functionality required when 13902 * attaching a segment to an address space. It must be called from 13903 * hat_share() for D(ISM) segments and from segvn_create() for segments 13904 * with the MAP_PRIVATE and MAP_TEXT flags set. It returns a region_cookie 13905 * which is saved in the private segment data for hme segments and 13906 * the ism_map structure for ism segments. 13907 */ 13908 hat_region_cookie_t 13909 hat_join_region(struct hat *sfmmup, 13910 caddr_t r_saddr, 13911 size_t r_size, 13912 void *r_obj, 13913 u_offset_t r_objoff, 13914 uchar_t r_perm, 13915 uchar_t r_pgszc, 13916 hat_rgn_cb_func_t r_cb_function, 13917 uint_t flags) 13918 { 13919 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 13920 uint_t rhash; 13921 uint_t rid; 13922 hatlock_t *hatlockp; 13923 sf_region_t *rgnp; 13924 sf_region_t *new_rgnp = NULL; 13925 int i; 13926 uint16_t *nextidp; 13927 sf_region_t **freelistp; 13928 int maxids; 13929 sf_region_t **rarrp; 13930 uint16_t *busyrgnsp; 13931 ulong_t rttecnt; 13932 uchar_t tteflag; 13933 uchar_t r_type = flags & HAT_REGION_TYPE_MASK; 13934 int text = (r_type == HAT_REGION_TEXT); 13935 13936 if (srdp == NULL || r_size == 0) { 13937 return (HAT_INVALID_REGION_COOKIE); 13938 } 13939 13940 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 13941 ASSERT(sfmmup != ksfmmup); 13942 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 13943 ASSERT(srdp->srd_refcnt > 0); 13944 ASSERT(!(flags & ~HAT_REGION_TYPE_MASK)); 13945 ASSERT(flags == HAT_REGION_TEXT || flags == HAT_REGION_ISM); 13946 ASSERT(r_pgszc < mmu_page_sizes); 13947 if (!IS_P2ALIGNED(r_saddr, TTEBYTES(r_pgszc)) || 13948 !IS_P2ALIGNED(r_size, TTEBYTES(r_pgszc))) { 13949 panic("hat_join_region: region addr or size is not aligned\n"); 13950 } 13951 13952 13953 r_type = (r_type == HAT_REGION_ISM) ? SFMMU_REGION_ISM : 13954 SFMMU_REGION_HME; 13955 /* 13956 * Currently only support shared hmes for the main text region. 13957 */ 13958 if (r_type == SFMMU_REGION_HME && r_obj != srdp->srd_evp) { 13959 return (HAT_INVALID_REGION_COOKIE); 13960 } 13961 13962 rhash = RGN_HASH_FUNCTION(r_obj); 13963 13964 if (r_type == SFMMU_REGION_ISM) { 13965 nextidp = &srdp->srd_next_ismrid; 13966 freelistp = &srdp->srd_ismrgnfree; 13967 maxids = SFMMU_MAX_ISM_REGIONS; 13968 rarrp = srdp->srd_ismrgnp; 13969 busyrgnsp = &srdp->srd_ismbusyrgns; 13970 } else { 13971 nextidp = &srdp->srd_next_hmerid; 13972 freelistp = &srdp->srd_hmergnfree; 13973 maxids = SFMMU_MAX_HME_REGIONS; 13974 rarrp = srdp->srd_hmergnp; 13975 busyrgnsp = &srdp->srd_hmebusyrgns; 13976 } 13977 13978 mutex_enter(&srdp->srd_mutex); 13979 13980 for (rgnp = srdp->srd_rgnhash[rhash]; rgnp != NULL; 13981 rgnp = rgnp->rgn_hash) { 13982 if (rgnp->rgn_saddr == r_saddr && rgnp->rgn_size == r_size && 13983 rgnp->rgn_obj == r_obj && rgnp->rgn_objoff == r_objoff && 13984 rgnp->rgn_perm == r_perm && rgnp->rgn_pgszc == r_pgszc) { 13985 break; 13986 } 13987 } 13988 13989 rfound: 13990 if (rgnp != NULL) { 13991 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type); 13992 ASSERT(rgnp->rgn_cb_function == r_cb_function); 13993 ASSERT(rgnp->rgn_refcnt >= 0); 13994 rid = rgnp->rgn_id; 13995 ASSERT(rid < maxids); 13996 ASSERT(rarrp[rid] == rgnp); 13997 ASSERT(rid < *nextidp); 13998 atomic_add_32((volatile uint_t *)&rgnp->rgn_refcnt, 1); 13999 mutex_exit(&srdp->srd_mutex); 14000 if (new_rgnp != NULL) { 14001 kmem_cache_free(region_cache, new_rgnp); 14002 } 14003 if (r_type == SFMMU_REGION_HME) { 14004 int myjoin = 14005 (sfmmup == astosfmmu(curthread->t_procp->p_as)); 14006 14007 sfmmu_link_to_hmeregion(sfmmup, rgnp); 14008 /* 14009 * bitmap should be updated after linking sfmmu on 14010 * region list so that pageunload() doesn't skip 14011 * TSB/TLB flush. As soon as bitmap is updated another 14012 * thread in this process can already start accessing 14013 * this region. 14014 */ 14015 /* 14016 * Normally ttecnt accounting is done as part of 14017 * pagefault handling. But a process may not take any 14018 * pagefaults on shared hmeblks created by some other 14019 * process. To compensate for this assume that the 14020 * entire region will end up faulted in using 14021 * the region's pagesize. 14022 * 14023 */ 14024 if (r_pgszc > TTE8K) { 14025 tteflag = 1 << r_pgszc; 14026 if (disable_large_pages & tteflag) { 14027 tteflag = 0; 14028 } 14029 } else { 14030 tteflag = 0; 14031 } 14032 if (tteflag && !(sfmmup->sfmmu_rtteflags & tteflag)) { 14033 hatlockp = sfmmu_hat_enter(sfmmup); 14034 sfmmup->sfmmu_rtteflags |= tteflag; 14035 sfmmu_hat_exit(hatlockp); 14036 } 14037 hatlockp = sfmmu_hat_enter(sfmmup); 14038 14039 /* 14040 * Preallocate 1/4 of ttecnt's in 8K TSB for >= 4M 14041 * region to allow for large page allocation failure. 14042 */ 14043 if (r_pgszc >= TTE4M) { 14044 sfmmup->sfmmu_tsb0_4minflcnt += 14045 r_size >> (TTE_PAGE_SHIFT(TTE8K) + 2); 14046 } 14047 14048 /* update sfmmu_ttecnt with the shme rgn ttecnt */ 14049 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc); 14050 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc], 14051 rttecnt); 14052 14053 if (text && r_pgszc >= TTE4M && 14054 (tteflag || ((disable_large_pages >> TTE4M) & 14055 ((1 << (r_pgszc - TTE4M + 1)) - 1))) && 14056 !SFMMU_FLAGS_ISSET(sfmmup, HAT_4MTEXT_FLAG)) { 14057 SFMMU_FLAGS_SET(sfmmup, HAT_4MTEXT_FLAG); 14058 } 14059 14060 sfmmu_hat_exit(hatlockp); 14061 /* 14062 * On Panther we need to make sure TLB is programmed 14063 * to accept 32M/256M pages. Call 14064 * sfmmu_check_page_sizes() now to make sure TLB is 14065 * setup before making hmeregions visible to other 14066 * threads. 14067 */ 14068 sfmmu_check_page_sizes(sfmmup, 1); 14069 hatlockp = sfmmu_hat_enter(sfmmup); 14070 SF_RGNMAP_ADD(sfmmup->sfmmu_hmeregion_map, rid); 14071 14072 /* 14073 * if context is invalid tsb miss exception code will 14074 * call sfmmu_check_page_sizes() and update tsbmiss 14075 * area later. 14076 */ 14077 kpreempt_disable(); 14078 if (myjoin && 14079 (sfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum 14080 != INVALID_CONTEXT)) { 14081 struct tsbmiss *tsbmp; 14082 14083 tsbmp = &tsbmiss_area[CPU->cpu_id]; 14084 ASSERT(sfmmup == tsbmp->usfmmup); 14085 BT_SET(tsbmp->shmermap, rid); 14086 if (r_pgszc > TTE64K) { 14087 tsbmp->uhat_rtteflags |= tteflag; 14088 } 14089 14090 } 14091 kpreempt_enable(); 14092 14093 sfmmu_hat_exit(hatlockp); 14094 ASSERT((hat_region_cookie_t)((uint64_t)rid) != 14095 HAT_INVALID_REGION_COOKIE); 14096 } else { 14097 hatlockp = sfmmu_hat_enter(sfmmup); 14098 SF_RGNMAP_ADD(sfmmup->sfmmu_ismregion_map, rid); 14099 sfmmu_hat_exit(hatlockp); 14100 } 14101 ASSERT(rid < maxids); 14102 14103 if (r_type == SFMMU_REGION_ISM) { 14104 sfmmu_find_scd(sfmmup); 14105 } 14106 return ((hat_region_cookie_t)((uint64_t)rid)); 14107 } 14108 14109 ASSERT(new_rgnp == NULL); 14110 14111 if (*busyrgnsp >= maxids) { 14112 mutex_exit(&srdp->srd_mutex); 14113 return (HAT_INVALID_REGION_COOKIE); 14114 } 14115 14116 ASSERT(MUTEX_HELD(&srdp->srd_mutex)); 14117 if (*freelistp != NULL) { 14118 rgnp = *freelistp; 14119 *freelistp = rgnp->rgn_next; 14120 ASSERT(rgnp->rgn_id < *nextidp); 14121 ASSERT(rgnp->rgn_id < maxids); 14122 ASSERT(rgnp->rgn_flags & SFMMU_REGION_FREE); 14123 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) 14124 == r_type); 14125 ASSERT(rarrp[rgnp->rgn_id] == rgnp); 14126 ASSERT(rgnp->rgn_hmeflags == 0); 14127 } else { 14128 /* 14129 * release local locks before memory allocation. 14130 */ 14131 mutex_exit(&srdp->srd_mutex); 14132 14133 new_rgnp = kmem_cache_alloc(region_cache, KM_SLEEP); 14134 14135 mutex_enter(&srdp->srd_mutex); 14136 for (rgnp = srdp->srd_rgnhash[rhash]; rgnp != NULL; 14137 rgnp = rgnp->rgn_hash) { 14138 if (rgnp->rgn_saddr == r_saddr && 14139 rgnp->rgn_size == r_size && 14140 rgnp->rgn_obj == r_obj && 14141 rgnp->rgn_objoff == r_objoff && 14142 rgnp->rgn_perm == r_perm && 14143 rgnp->rgn_pgszc == r_pgszc) { 14144 break; 14145 } 14146 } 14147 if (rgnp != NULL) { 14148 goto rfound; 14149 } 14150 14151 if (*nextidp >= maxids) { 14152 mutex_exit(&srdp->srd_mutex); 14153 goto fail; 14154 } 14155 rgnp = new_rgnp; 14156 new_rgnp = NULL; 14157 rgnp->rgn_id = (*nextidp)++; 14158 ASSERT(rgnp->rgn_id < maxids); 14159 ASSERT(rarrp[rgnp->rgn_id] == NULL); 14160 rarrp[rgnp->rgn_id] = rgnp; 14161 } 14162 14163 ASSERT(rgnp->rgn_sfmmu_head == NULL); 14164 ASSERT(rgnp->rgn_hmeflags == 0); 14165 #ifdef DEBUG 14166 for (i = 0; i < MMU_PAGE_SIZES; i++) { 14167 ASSERT(rgnp->rgn_ttecnt[i] == 0); 14168 } 14169 #endif 14170 rgnp->rgn_saddr = r_saddr; 14171 rgnp->rgn_size = r_size; 14172 rgnp->rgn_obj = r_obj; 14173 rgnp->rgn_objoff = r_objoff; 14174 rgnp->rgn_perm = r_perm; 14175 rgnp->rgn_pgszc = r_pgszc; 14176 rgnp->rgn_flags = r_type; 14177 rgnp->rgn_refcnt = 0; 14178 rgnp->rgn_cb_function = r_cb_function; 14179 rgnp->rgn_hash = srdp->srd_rgnhash[rhash]; 14180 srdp->srd_rgnhash[rhash] = rgnp; 14181 (*busyrgnsp)++; 14182 ASSERT(*busyrgnsp <= maxids); 14183 goto rfound; 14184 14185 fail: 14186 ASSERT(new_rgnp != NULL); 14187 kmem_cache_free(region_cache, new_rgnp); 14188 return (HAT_INVALID_REGION_COOKIE); 14189 } 14190 14191 /* 14192 * This function implements the shared context functionality required 14193 * when detaching a segment from an address space. It must be called 14194 * from hat_unshare() for all D(ISM) segments and from segvn_unmap(), 14195 * for segments with a valid region_cookie. 14196 * It will also be called from all seg_vn routines which change a 14197 * segment's attributes such as segvn_setprot(), segvn_setpagesize(), 14198 * segvn_clrszc() & segvn_advise(), as well as in the case of COW fault 14199 * from segvn_fault(). 14200 */ 14201 void 14202 hat_leave_region(struct hat *sfmmup, hat_region_cookie_t rcookie, uint_t flags) 14203 { 14204 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 14205 sf_scd_t *scdp; 14206 uint_t rhash; 14207 uint_t rid = (uint_t)((uint64_t)rcookie); 14208 hatlock_t *hatlockp = NULL; 14209 sf_region_t *rgnp; 14210 sf_region_t **prev_rgnpp; 14211 sf_region_t *cur_rgnp; 14212 void *r_obj; 14213 int i; 14214 caddr_t r_saddr; 14215 caddr_t r_eaddr; 14216 size_t r_size; 14217 uchar_t r_pgszc; 14218 uchar_t r_type = flags & HAT_REGION_TYPE_MASK; 14219 14220 ASSERT(sfmmup != ksfmmup); 14221 ASSERT(srdp != NULL); 14222 ASSERT(srdp->srd_refcnt > 0); 14223 ASSERT(!(flags & ~HAT_REGION_TYPE_MASK)); 14224 ASSERT(flags == HAT_REGION_TEXT || flags == HAT_REGION_ISM); 14225 ASSERT(!sfmmup->sfmmu_free || sfmmup->sfmmu_scdp == NULL); 14226 14227 r_type = (r_type == HAT_REGION_ISM) ? SFMMU_REGION_ISM : 14228 SFMMU_REGION_HME; 14229 14230 if (r_type == SFMMU_REGION_ISM) { 14231 ASSERT(SFMMU_IS_ISMRID_VALID(rid)); 14232 ASSERT(rid < SFMMU_MAX_ISM_REGIONS); 14233 rgnp = srdp->srd_ismrgnp[rid]; 14234 } else { 14235 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 14236 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 14237 rgnp = srdp->srd_hmergnp[rid]; 14238 } 14239 ASSERT(rgnp != NULL); 14240 ASSERT(rgnp->rgn_id == rid); 14241 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type); 14242 ASSERT(!(rgnp->rgn_flags & SFMMU_REGION_FREE)); 14243 ASSERT(AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 14244 14245 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 14246 if (r_type == SFMMU_REGION_HME && sfmmup->sfmmu_as->a_xhat != NULL) { 14247 xhat_unload_callback_all(sfmmup->sfmmu_as, rgnp->rgn_saddr, 14248 rgnp->rgn_size, 0, NULL); 14249 } 14250 14251 if (sfmmup->sfmmu_free) { 14252 ulong_t rttecnt; 14253 r_pgszc = rgnp->rgn_pgszc; 14254 r_size = rgnp->rgn_size; 14255 14256 ASSERT(sfmmup->sfmmu_scdp == NULL); 14257 if (r_type == SFMMU_REGION_ISM) { 14258 SF_RGNMAP_DEL(sfmmup->sfmmu_ismregion_map, rid); 14259 } else { 14260 /* update shme rgns ttecnt in sfmmu_ttecnt */ 14261 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc); 14262 ASSERT(sfmmup->sfmmu_ttecnt[r_pgszc] >= rttecnt); 14263 14264 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc], 14265 -rttecnt); 14266 14267 SF_RGNMAP_DEL(sfmmup->sfmmu_hmeregion_map, rid); 14268 } 14269 } else if (r_type == SFMMU_REGION_ISM) { 14270 hatlockp = sfmmu_hat_enter(sfmmup); 14271 ASSERT(rid < srdp->srd_next_ismrid); 14272 SF_RGNMAP_DEL(sfmmup->sfmmu_ismregion_map, rid); 14273 scdp = sfmmup->sfmmu_scdp; 14274 if (scdp != NULL && 14275 SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid)) { 14276 sfmmu_leave_scd(sfmmup, r_type); 14277 ASSERT(sfmmu_hat_lock_held(sfmmup)); 14278 } 14279 sfmmu_hat_exit(hatlockp); 14280 } else { 14281 ulong_t rttecnt; 14282 r_pgszc = rgnp->rgn_pgszc; 14283 r_saddr = rgnp->rgn_saddr; 14284 r_size = rgnp->rgn_size; 14285 r_eaddr = r_saddr + r_size; 14286 14287 ASSERT(r_type == SFMMU_REGION_HME); 14288 hatlockp = sfmmu_hat_enter(sfmmup); 14289 ASSERT(rid < srdp->srd_next_hmerid); 14290 SF_RGNMAP_DEL(sfmmup->sfmmu_hmeregion_map, rid); 14291 14292 /* 14293 * If region is part of an SCD call sfmmu_leave_scd(). 14294 * Otherwise if process is not exiting and has valid context 14295 * just drop the context on the floor to lose stale TLB 14296 * entries and force the update of tsb miss area to reflect 14297 * the new region map. After that clean our TSB entries. 14298 */ 14299 scdp = sfmmup->sfmmu_scdp; 14300 if (scdp != NULL && 14301 SF_RGNMAP_TEST(scdp->scd_hmeregion_map, rid)) { 14302 sfmmu_leave_scd(sfmmup, r_type); 14303 ASSERT(sfmmu_hat_lock_held(sfmmup)); 14304 } 14305 sfmmu_invalidate_ctx(sfmmup); 14306 14307 i = TTE8K; 14308 while (i < mmu_page_sizes) { 14309 if (rgnp->rgn_ttecnt[i] != 0) { 14310 sfmmu_unload_tsb_range(sfmmup, r_saddr, 14311 r_eaddr, i); 14312 if (i < TTE4M) { 14313 i = TTE4M; 14314 continue; 14315 } else { 14316 break; 14317 } 14318 } 14319 i++; 14320 } 14321 /* Remove the preallocated 1/4 8k ttecnt for 4M regions. */ 14322 if (r_pgszc >= TTE4M) { 14323 rttecnt = r_size >> (TTE_PAGE_SHIFT(TTE8K) + 2); 14324 ASSERT(sfmmup->sfmmu_tsb0_4minflcnt >= 14325 rttecnt); 14326 sfmmup->sfmmu_tsb0_4minflcnt -= rttecnt; 14327 } 14328 14329 /* update shme rgns ttecnt in sfmmu_ttecnt */ 14330 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc); 14331 ASSERT(sfmmup->sfmmu_ttecnt[r_pgszc] >= rttecnt); 14332 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc], -rttecnt); 14333 14334 sfmmu_hat_exit(hatlockp); 14335 if (scdp != NULL && sfmmup->sfmmu_scdp == NULL) { 14336 /* sfmmup left the scd, grow private tsb */ 14337 sfmmu_check_page_sizes(sfmmup, 1); 14338 } else { 14339 sfmmu_check_page_sizes(sfmmup, 0); 14340 } 14341 } 14342 14343 if (r_type == SFMMU_REGION_HME) { 14344 sfmmu_unlink_from_hmeregion(sfmmup, rgnp); 14345 } 14346 14347 r_obj = rgnp->rgn_obj; 14348 if (atomic_add_32_nv((volatile uint_t *)&rgnp->rgn_refcnt, -1)) { 14349 return; 14350 } 14351 14352 /* 14353 * looks like nobody uses this region anymore. Free it. 14354 */ 14355 rhash = RGN_HASH_FUNCTION(r_obj); 14356 mutex_enter(&srdp->srd_mutex); 14357 for (prev_rgnpp = &srdp->srd_rgnhash[rhash]; 14358 (cur_rgnp = *prev_rgnpp) != NULL; 14359 prev_rgnpp = &cur_rgnp->rgn_hash) { 14360 if (cur_rgnp == rgnp && cur_rgnp->rgn_refcnt == 0) { 14361 break; 14362 } 14363 } 14364 14365 if (cur_rgnp == NULL) { 14366 mutex_exit(&srdp->srd_mutex); 14367 return; 14368 } 14369 14370 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type); 14371 *prev_rgnpp = rgnp->rgn_hash; 14372 if (r_type == SFMMU_REGION_ISM) { 14373 rgnp->rgn_flags |= SFMMU_REGION_FREE; 14374 ASSERT(rid < srdp->srd_next_ismrid); 14375 rgnp->rgn_next = srdp->srd_ismrgnfree; 14376 srdp->srd_ismrgnfree = rgnp; 14377 ASSERT(srdp->srd_ismbusyrgns > 0); 14378 srdp->srd_ismbusyrgns--; 14379 mutex_exit(&srdp->srd_mutex); 14380 return; 14381 } 14382 mutex_exit(&srdp->srd_mutex); 14383 14384 /* 14385 * Destroy region's hmeblks. 14386 */ 14387 sfmmu_unload_hmeregion(srdp, rgnp); 14388 14389 rgnp->rgn_hmeflags = 0; 14390 14391 ASSERT(rgnp->rgn_sfmmu_head == NULL); 14392 ASSERT(rgnp->rgn_id == rid); 14393 for (i = 0; i < MMU_PAGE_SIZES; i++) { 14394 rgnp->rgn_ttecnt[i] = 0; 14395 } 14396 rgnp->rgn_flags |= SFMMU_REGION_FREE; 14397 mutex_enter(&srdp->srd_mutex); 14398 ASSERT(rid < srdp->srd_next_hmerid); 14399 rgnp->rgn_next = srdp->srd_hmergnfree; 14400 srdp->srd_hmergnfree = rgnp; 14401 ASSERT(srdp->srd_hmebusyrgns > 0); 14402 srdp->srd_hmebusyrgns--; 14403 mutex_exit(&srdp->srd_mutex); 14404 } 14405 14406 /* 14407 * For now only called for hmeblk regions and not for ISM regions. 14408 */ 14409 void 14410 hat_dup_region(struct hat *sfmmup, hat_region_cookie_t rcookie) 14411 { 14412 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 14413 uint_t rid = (uint_t)((uint64_t)rcookie); 14414 sf_region_t *rgnp; 14415 sf_rgn_link_t *rlink; 14416 sf_rgn_link_t *hrlink; 14417 ulong_t rttecnt; 14418 14419 ASSERT(sfmmup != ksfmmup); 14420 ASSERT(srdp != NULL); 14421 ASSERT(srdp->srd_refcnt > 0); 14422 14423 ASSERT(rid < srdp->srd_next_hmerid); 14424 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 14425 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 14426 14427 rgnp = srdp->srd_hmergnp[rid]; 14428 ASSERT(rgnp->rgn_refcnt > 0); 14429 ASSERT(rgnp->rgn_id == rid); 14430 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == SFMMU_REGION_HME); 14431 ASSERT(!(rgnp->rgn_flags & SFMMU_REGION_FREE)); 14432 14433 atomic_add_32((volatile uint_t *)&rgnp->rgn_refcnt, 1); 14434 14435 /* LINTED: constant in conditional context */ 14436 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 1, 0); 14437 ASSERT(rlink != NULL); 14438 mutex_enter(&rgnp->rgn_mutex); 14439 ASSERT(rgnp->rgn_sfmmu_head != NULL); 14440 /* LINTED: constant in conditional context */ 14441 SFMMU_HMERID2RLINKP(rgnp->rgn_sfmmu_head, rid, hrlink, 0, 0); 14442 ASSERT(hrlink != NULL); 14443 ASSERT(hrlink->prev == NULL); 14444 rlink->next = rgnp->rgn_sfmmu_head; 14445 rlink->prev = NULL; 14446 hrlink->prev = sfmmup; 14447 /* 14448 * make sure rlink's next field is correct 14449 * before making this link visible. 14450 */ 14451 membar_stst(); 14452 rgnp->rgn_sfmmu_head = sfmmup; 14453 mutex_exit(&rgnp->rgn_mutex); 14454 14455 /* update sfmmu_ttecnt with the shme rgn ttecnt */ 14456 rttecnt = rgnp->rgn_size >> TTE_PAGE_SHIFT(rgnp->rgn_pgszc); 14457 atomic_add_long(&sfmmup->sfmmu_ttecnt[rgnp->rgn_pgszc], rttecnt); 14458 /* update tsb0 inflation count */ 14459 if (rgnp->rgn_pgszc >= TTE4M) { 14460 sfmmup->sfmmu_tsb0_4minflcnt += 14461 rgnp->rgn_size >> (TTE_PAGE_SHIFT(TTE8K) + 2); 14462 } 14463 /* 14464 * Update regionid bitmask without hat lock since no other thread 14465 * can update this region bitmask right now. 14466 */ 14467 SF_RGNMAP_ADD(sfmmup->sfmmu_hmeregion_map, rid); 14468 } 14469 14470 /* ARGSUSED */ 14471 static int 14472 sfmmu_rgncache_constructor(void *buf, void *cdrarg, int kmflags) 14473 { 14474 sf_region_t *rgnp = (sf_region_t *)buf; 14475 bzero(buf, sizeof (*rgnp)); 14476 14477 mutex_init(&rgnp->rgn_mutex, NULL, MUTEX_DEFAULT, NULL); 14478 14479 return (0); 14480 } 14481 14482 /* ARGSUSED */ 14483 static void 14484 sfmmu_rgncache_destructor(void *buf, void *cdrarg) 14485 { 14486 sf_region_t *rgnp = (sf_region_t *)buf; 14487 mutex_destroy(&rgnp->rgn_mutex); 14488 } 14489 14490 static int 14491 sfrgnmap_isnull(sf_region_map_t *map) 14492 { 14493 int i; 14494 14495 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) { 14496 if (map->bitmap[i] != 0) { 14497 return (0); 14498 } 14499 } 14500 return (1); 14501 } 14502 14503 static int 14504 sfhmergnmap_isnull(sf_hmeregion_map_t *map) 14505 { 14506 int i; 14507 14508 for (i = 0; i < SFMMU_HMERGNMAP_WORDS; i++) { 14509 if (map->bitmap[i] != 0) { 14510 return (0); 14511 } 14512 } 14513 return (1); 14514 } 14515 14516 #ifdef DEBUG 14517 static void 14518 check_scd_sfmmu_list(sfmmu_t **headp, sfmmu_t *sfmmup, int onlist) 14519 { 14520 sfmmu_t *sp; 14521 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 14522 14523 for (sp = *headp; sp != NULL; sp = sp->sfmmu_scd_link.next) { 14524 ASSERT(srdp == sp->sfmmu_srdp); 14525 if (sp == sfmmup) { 14526 if (onlist) { 14527 return; 14528 } else { 14529 panic("shctx: sfmmu 0x%p found on scd" 14530 "list 0x%p", sfmmup, *headp); 14531 } 14532 } 14533 } 14534 if (onlist) { 14535 panic("shctx: sfmmu 0x%p not found on scd list 0x%p", 14536 sfmmup, *headp); 14537 } else { 14538 return; 14539 } 14540 } 14541 #else /* DEBUG */ 14542 #define check_scd_sfmmu_list(headp, sfmmup, onlist) 14543 #endif /* DEBUG */ 14544 14545 /* 14546 * Removes an sfmmu from the SCD sfmmu list. 14547 */ 14548 static void 14549 sfmmu_from_scd_list(sfmmu_t **headp, sfmmu_t *sfmmup) 14550 { 14551 ASSERT(sfmmup->sfmmu_srdp != NULL); 14552 check_scd_sfmmu_list(headp, sfmmup, 1); 14553 if (sfmmup->sfmmu_scd_link.prev != NULL) { 14554 ASSERT(*headp != sfmmup); 14555 sfmmup->sfmmu_scd_link.prev->sfmmu_scd_link.next = 14556 sfmmup->sfmmu_scd_link.next; 14557 } else { 14558 ASSERT(*headp == sfmmup); 14559 *headp = sfmmup->sfmmu_scd_link.next; 14560 } 14561 if (sfmmup->sfmmu_scd_link.next != NULL) { 14562 sfmmup->sfmmu_scd_link.next->sfmmu_scd_link.prev = 14563 sfmmup->sfmmu_scd_link.prev; 14564 } 14565 } 14566 14567 14568 /* 14569 * Adds an sfmmu to the start of the queue. 14570 */ 14571 static void 14572 sfmmu_to_scd_list(sfmmu_t **headp, sfmmu_t *sfmmup) 14573 { 14574 check_scd_sfmmu_list(headp, sfmmup, 0); 14575 sfmmup->sfmmu_scd_link.prev = NULL; 14576 sfmmup->sfmmu_scd_link.next = *headp; 14577 if (*headp != NULL) 14578 (*headp)->sfmmu_scd_link.prev = sfmmup; 14579 *headp = sfmmup; 14580 } 14581 14582 /* 14583 * Remove an scd from the start of the queue. 14584 */ 14585 static void 14586 sfmmu_remove_scd(sf_scd_t **headp, sf_scd_t *scdp) 14587 { 14588 if (scdp->scd_prev != NULL) { 14589 ASSERT(*headp != scdp); 14590 scdp->scd_prev->scd_next = scdp->scd_next; 14591 } else { 14592 ASSERT(*headp == scdp); 14593 *headp = scdp->scd_next; 14594 } 14595 14596 if (scdp->scd_next != NULL) { 14597 scdp->scd_next->scd_prev = scdp->scd_prev; 14598 } 14599 } 14600 14601 /* 14602 * Add an scd to the start of the queue. 14603 */ 14604 static void 14605 sfmmu_add_scd(sf_scd_t **headp, sf_scd_t *scdp) 14606 { 14607 scdp->scd_prev = NULL; 14608 scdp->scd_next = *headp; 14609 if (*headp != NULL) { 14610 (*headp)->scd_prev = scdp; 14611 } 14612 *headp = scdp; 14613 } 14614 14615 static int 14616 sfmmu_alloc_scd_tsbs(sf_srd_t *srdp, sf_scd_t *scdp) 14617 { 14618 uint_t rid; 14619 uint_t i; 14620 uint_t j; 14621 ulong_t w; 14622 sf_region_t *rgnp; 14623 ulong_t tte8k_cnt = 0; 14624 ulong_t tte4m_cnt = 0; 14625 uint_t tsb_szc; 14626 sfmmu_t *scsfmmup = scdp->scd_sfmmup; 14627 sfmmu_t *ism_hatid; 14628 struct tsb_info *newtsb; 14629 int szc; 14630 14631 ASSERT(srdp != NULL); 14632 14633 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) { 14634 if ((w = scdp->scd_region_map.bitmap[i]) == 0) { 14635 continue; 14636 } 14637 j = 0; 14638 while (w) { 14639 if (!(w & 0x1)) { 14640 j++; 14641 w >>= 1; 14642 continue; 14643 } 14644 rid = (i << BT_ULSHIFT) | j; 14645 j++; 14646 w >>= 1; 14647 14648 if (rid < SFMMU_MAX_HME_REGIONS) { 14649 rgnp = srdp->srd_hmergnp[rid]; 14650 ASSERT(rgnp->rgn_id == rid); 14651 ASSERT(rgnp->rgn_refcnt > 0); 14652 14653 if (rgnp->rgn_pgszc < TTE4M) { 14654 tte8k_cnt += rgnp->rgn_size >> 14655 TTE_PAGE_SHIFT(TTE8K); 14656 } else { 14657 ASSERT(rgnp->rgn_pgszc >= TTE4M); 14658 tte4m_cnt += rgnp->rgn_size >> 14659 TTE_PAGE_SHIFT(TTE4M); 14660 /* 14661 * Inflate SCD tsb0 by preallocating 14662 * 1/4 8k ttecnt for 4M regions to 14663 * allow for lgpg alloc failure. 14664 */ 14665 tte8k_cnt += rgnp->rgn_size >> 14666 (TTE_PAGE_SHIFT(TTE8K) + 2); 14667 } 14668 } else { 14669 rid -= SFMMU_MAX_HME_REGIONS; 14670 rgnp = srdp->srd_ismrgnp[rid]; 14671 ASSERT(rgnp->rgn_id == rid); 14672 ASSERT(rgnp->rgn_refcnt > 0); 14673 14674 ism_hatid = (sfmmu_t *)rgnp->rgn_obj; 14675 ASSERT(ism_hatid->sfmmu_ismhat); 14676 14677 for (szc = 0; szc < TTE4M; szc++) { 14678 tte8k_cnt += 14679 ism_hatid->sfmmu_ttecnt[szc] << 14680 TTE_BSZS_SHIFT(szc); 14681 } 14682 14683 ASSERT(rgnp->rgn_pgszc >= TTE4M); 14684 if (rgnp->rgn_pgszc >= TTE4M) { 14685 tte4m_cnt += rgnp->rgn_size >> 14686 TTE_PAGE_SHIFT(TTE4M); 14687 } 14688 } 14689 } 14690 } 14691 14692 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt); 14693 14694 /* Allocate both the SCD TSBs here. */ 14695 if (sfmmu_tsbinfo_alloc(&scsfmmup->sfmmu_tsb, 14696 tsb_szc, TSB8K|TSB64K|TSB512K, TSB_ALLOC, scsfmmup) && 14697 (tsb_szc <= TSB_4M_SZCODE || 14698 sfmmu_tsbinfo_alloc(&scsfmmup->sfmmu_tsb, 14699 TSB_4M_SZCODE, TSB8K|TSB64K|TSB512K, 14700 TSB_ALLOC, scsfmmup))) { 14701 14702 SFMMU_STAT(sf_scd_1sttsb_allocfail); 14703 return (TSB_ALLOCFAIL); 14704 } else { 14705 scsfmmup->sfmmu_tsb->tsb_flags |= TSB_SHAREDCTX; 14706 14707 if (tte4m_cnt) { 14708 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt); 14709 if (sfmmu_tsbinfo_alloc(&newtsb, tsb_szc, 14710 TSB4M|TSB32M|TSB256M, TSB_ALLOC, scsfmmup) && 14711 (tsb_szc <= TSB_4M_SZCODE || 14712 sfmmu_tsbinfo_alloc(&newtsb, TSB_4M_SZCODE, 14713 TSB4M|TSB32M|TSB256M, 14714 TSB_ALLOC, scsfmmup))) { 14715 /* 14716 * If we fail to allocate the 2nd shared tsb, 14717 * just free the 1st tsb, return failure. 14718 */ 14719 sfmmu_tsbinfo_free(scsfmmup->sfmmu_tsb); 14720 SFMMU_STAT(sf_scd_2ndtsb_allocfail); 14721 return (TSB_ALLOCFAIL); 14722 } else { 14723 ASSERT(scsfmmup->sfmmu_tsb->tsb_next == NULL); 14724 newtsb->tsb_flags |= TSB_SHAREDCTX; 14725 scsfmmup->sfmmu_tsb->tsb_next = newtsb; 14726 SFMMU_STAT(sf_scd_2ndtsb_alloc); 14727 } 14728 } 14729 SFMMU_STAT(sf_scd_1sttsb_alloc); 14730 } 14731 return (TSB_SUCCESS); 14732 } 14733 14734 static void 14735 sfmmu_free_scd_tsbs(sfmmu_t *scd_sfmmu) 14736 { 14737 while (scd_sfmmu->sfmmu_tsb != NULL) { 14738 struct tsb_info *next = scd_sfmmu->sfmmu_tsb->tsb_next; 14739 sfmmu_tsbinfo_free(scd_sfmmu->sfmmu_tsb); 14740 scd_sfmmu->sfmmu_tsb = next; 14741 } 14742 } 14743 14744 /* 14745 * Link the sfmmu onto the hme region list. 14746 */ 14747 void 14748 sfmmu_link_to_hmeregion(sfmmu_t *sfmmup, sf_region_t *rgnp) 14749 { 14750 uint_t rid; 14751 sf_rgn_link_t *rlink; 14752 sfmmu_t *head; 14753 sf_rgn_link_t *hrlink; 14754 14755 rid = rgnp->rgn_id; 14756 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 14757 14758 /* LINTED: constant in conditional context */ 14759 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 1, 1); 14760 ASSERT(rlink != NULL); 14761 mutex_enter(&rgnp->rgn_mutex); 14762 if ((head = rgnp->rgn_sfmmu_head) == NULL) { 14763 rlink->next = NULL; 14764 rlink->prev = NULL; 14765 /* 14766 * make sure rlink's next field is NULL 14767 * before making this link visible. 14768 */ 14769 membar_stst(); 14770 rgnp->rgn_sfmmu_head = sfmmup; 14771 } else { 14772 /* LINTED: constant in conditional context */ 14773 SFMMU_HMERID2RLINKP(head, rid, hrlink, 0, 0); 14774 ASSERT(hrlink != NULL); 14775 ASSERT(hrlink->prev == NULL); 14776 rlink->next = head; 14777 rlink->prev = NULL; 14778 hrlink->prev = sfmmup; 14779 /* 14780 * make sure rlink's next field is correct 14781 * before making this link visible. 14782 */ 14783 membar_stst(); 14784 rgnp->rgn_sfmmu_head = sfmmup; 14785 } 14786 mutex_exit(&rgnp->rgn_mutex); 14787 } 14788 14789 /* 14790 * Unlink the sfmmu from the hme region list. 14791 */ 14792 void 14793 sfmmu_unlink_from_hmeregion(sfmmu_t *sfmmup, sf_region_t *rgnp) 14794 { 14795 uint_t rid; 14796 sf_rgn_link_t *rlink; 14797 14798 rid = rgnp->rgn_id; 14799 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 14800 14801 /* LINTED: constant in conditional context */ 14802 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 0, 0); 14803 ASSERT(rlink != NULL); 14804 mutex_enter(&rgnp->rgn_mutex); 14805 if (rgnp->rgn_sfmmu_head == sfmmup) { 14806 sfmmu_t *next = rlink->next; 14807 rgnp->rgn_sfmmu_head = next; 14808 /* 14809 * if we are stopped by xc_attention() after this 14810 * point the forward link walking in 14811 * sfmmu_rgntlb_demap() will work correctly since the 14812 * head correctly points to the next element. 14813 */ 14814 membar_stst(); 14815 rlink->next = NULL; 14816 ASSERT(rlink->prev == NULL); 14817 if (next != NULL) { 14818 sf_rgn_link_t *nrlink; 14819 /* LINTED: constant in conditional context */ 14820 SFMMU_HMERID2RLINKP(next, rid, nrlink, 0, 0); 14821 ASSERT(nrlink != NULL); 14822 ASSERT(nrlink->prev == sfmmup); 14823 nrlink->prev = NULL; 14824 } 14825 } else { 14826 sfmmu_t *next = rlink->next; 14827 sfmmu_t *prev = rlink->prev; 14828 sf_rgn_link_t *prlink; 14829 14830 ASSERT(prev != NULL); 14831 /* LINTED: constant in conditional context */ 14832 SFMMU_HMERID2RLINKP(prev, rid, prlink, 0, 0); 14833 ASSERT(prlink != NULL); 14834 ASSERT(prlink->next == sfmmup); 14835 prlink->next = next; 14836 /* 14837 * if we are stopped by xc_attention() 14838 * after this point the forward link walking 14839 * will work correctly since the prev element 14840 * correctly points to the next element. 14841 */ 14842 membar_stst(); 14843 rlink->next = NULL; 14844 rlink->prev = NULL; 14845 if (next != NULL) { 14846 sf_rgn_link_t *nrlink; 14847 /* LINTED: constant in conditional context */ 14848 SFMMU_HMERID2RLINKP(next, rid, nrlink, 0, 0); 14849 ASSERT(nrlink != NULL); 14850 ASSERT(nrlink->prev == sfmmup); 14851 nrlink->prev = prev; 14852 } 14853 } 14854 mutex_exit(&rgnp->rgn_mutex); 14855 } 14856 14857 /* 14858 * Link scd sfmmu onto ism or hme region list for each region in the 14859 * scd region map. 14860 */ 14861 void 14862 sfmmu_link_scd_to_regions(sf_srd_t *srdp, sf_scd_t *scdp) 14863 { 14864 uint_t rid; 14865 uint_t i; 14866 uint_t j; 14867 ulong_t w; 14868 sf_region_t *rgnp; 14869 sfmmu_t *scsfmmup; 14870 14871 scsfmmup = scdp->scd_sfmmup; 14872 ASSERT(scsfmmup->sfmmu_scdhat); 14873 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) { 14874 if ((w = scdp->scd_region_map.bitmap[i]) == 0) { 14875 continue; 14876 } 14877 j = 0; 14878 while (w) { 14879 if (!(w & 0x1)) { 14880 j++; 14881 w >>= 1; 14882 continue; 14883 } 14884 rid = (i << BT_ULSHIFT) | j; 14885 j++; 14886 w >>= 1; 14887 14888 if (rid < SFMMU_MAX_HME_REGIONS) { 14889 rgnp = srdp->srd_hmergnp[rid]; 14890 ASSERT(rgnp->rgn_id == rid); 14891 ASSERT(rgnp->rgn_refcnt > 0); 14892 sfmmu_link_to_hmeregion(scsfmmup, rgnp); 14893 } else { 14894 sfmmu_t *ism_hatid = NULL; 14895 ism_ment_t *ism_ment; 14896 rid -= SFMMU_MAX_HME_REGIONS; 14897 rgnp = srdp->srd_ismrgnp[rid]; 14898 ASSERT(rgnp->rgn_id == rid); 14899 ASSERT(rgnp->rgn_refcnt > 0); 14900 14901 ism_hatid = (sfmmu_t *)rgnp->rgn_obj; 14902 ASSERT(ism_hatid->sfmmu_ismhat); 14903 ism_ment = &scdp->scd_ism_links[rid]; 14904 ism_ment->iment_hat = scsfmmup; 14905 ism_ment->iment_base_va = rgnp->rgn_saddr; 14906 mutex_enter(&ism_mlist_lock); 14907 iment_add(ism_ment, ism_hatid); 14908 mutex_exit(&ism_mlist_lock); 14909 14910 } 14911 } 14912 } 14913 } 14914 /* 14915 * Unlink scd sfmmu from ism or hme region list for each region in the 14916 * scd region map. 14917 */ 14918 void 14919 sfmmu_unlink_scd_from_regions(sf_srd_t *srdp, sf_scd_t *scdp) 14920 { 14921 uint_t rid; 14922 uint_t i; 14923 uint_t j; 14924 ulong_t w; 14925 sf_region_t *rgnp; 14926 sfmmu_t *scsfmmup; 14927 14928 scsfmmup = scdp->scd_sfmmup; 14929 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) { 14930 if ((w = scdp->scd_region_map.bitmap[i]) == 0) { 14931 continue; 14932 } 14933 j = 0; 14934 while (w) { 14935 if (!(w & 0x1)) { 14936 j++; 14937 w >>= 1; 14938 continue; 14939 } 14940 rid = (i << BT_ULSHIFT) | j; 14941 j++; 14942 w >>= 1; 14943 14944 if (rid < SFMMU_MAX_HME_REGIONS) { 14945 rgnp = srdp->srd_hmergnp[rid]; 14946 ASSERT(rgnp->rgn_id == rid); 14947 ASSERT(rgnp->rgn_refcnt > 0); 14948 sfmmu_unlink_from_hmeregion(scsfmmup, 14949 rgnp); 14950 14951 } else { 14952 sfmmu_t *ism_hatid = NULL; 14953 ism_ment_t *ism_ment; 14954 rid -= SFMMU_MAX_HME_REGIONS; 14955 rgnp = srdp->srd_ismrgnp[rid]; 14956 ASSERT(rgnp->rgn_id == rid); 14957 ASSERT(rgnp->rgn_refcnt > 0); 14958 14959 ism_hatid = (sfmmu_t *)rgnp->rgn_obj; 14960 ASSERT(ism_hatid->sfmmu_ismhat); 14961 ism_ment = &scdp->scd_ism_links[rid]; 14962 ASSERT(ism_ment->iment_hat == scdp->scd_sfmmup); 14963 ASSERT(ism_ment->iment_base_va == 14964 rgnp->rgn_saddr); 14965 ism_ment->iment_hat = NULL; 14966 ism_ment->iment_base_va = 0; 14967 mutex_enter(&ism_mlist_lock); 14968 iment_sub(ism_ment, ism_hatid); 14969 mutex_exit(&ism_mlist_lock); 14970 14971 } 14972 } 14973 } 14974 } 14975 /* 14976 * Allocates and initialises a new SCD structure, this is called with 14977 * the srd_scd_mutex held and returns with the reference count 14978 * initialised to 1. 14979 */ 14980 static sf_scd_t * 14981 sfmmu_alloc_scd(sf_srd_t *srdp, sf_region_map_t *new_map) 14982 { 14983 sf_scd_t *new_scdp; 14984 sfmmu_t *scsfmmup; 14985 int i; 14986 14987 ASSERT(MUTEX_HELD(&srdp->srd_scd_mutex)); 14988 new_scdp = kmem_cache_alloc(scd_cache, KM_SLEEP); 14989 14990 scsfmmup = kmem_cache_alloc(sfmmuid_cache, KM_SLEEP); 14991 new_scdp->scd_sfmmup = scsfmmup; 14992 scsfmmup->sfmmu_srdp = srdp; 14993 scsfmmup->sfmmu_scdp = new_scdp; 14994 scsfmmup->sfmmu_tsb0_4minflcnt = 0; 14995 scsfmmup->sfmmu_scdhat = 1; 14996 CPUSET_ALL(scsfmmup->sfmmu_cpusran); 14997 bzero(scsfmmup->sfmmu_hmeregion_links, SFMMU_L1_HMERLINKS_SIZE); 14998 14999 ASSERT(max_mmu_ctxdoms > 0); 15000 for (i = 0; i < max_mmu_ctxdoms; i++) { 15001 scsfmmup->sfmmu_ctxs[i].cnum = INVALID_CONTEXT; 15002 scsfmmup->sfmmu_ctxs[i].gnum = 0; 15003 } 15004 15005 for (i = 0; i < MMU_PAGE_SIZES; i++) { 15006 new_scdp->scd_rttecnt[i] = 0; 15007 } 15008 15009 new_scdp->scd_region_map = *new_map; 15010 new_scdp->scd_refcnt = 1; 15011 if (sfmmu_alloc_scd_tsbs(srdp, new_scdp) != TSB_SUCCESS) { 15012 kmem_cache_free(scd_cache, new_scdp); 15013 kmem_cache_free(sfmmuid_cache, scsfmmup); 15014 return (NULL); 15015 } 15016 return (new_scdp); 15017 } 15018 15019 /* 15020 * The first phase of a process joining an SCD. The hat structure is 15021 * linked to the SCD queue and then the HAT_JOIN_SCD sfmmu flag is set 15022 * and a cross-call with context invalidation is used to cause the 15023 * remaining work to be carried out in the sfmmu_tsbmiss_exception() 15024 * routine. 15025 */ 15026 static void 15027 sfmmu_join_scd(sf_scd_t *scdp, sfmmu_t *sfmmup) 15028 { 15029 hatlock_t *hatlockp; 15030 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 15031 int i; 15032 sf_scd_t *old_scdp; 15033 15034 ASSERT(srdp != NULL); 15035 ASSERT(scdp != NULL); 15036 ASSERT(scdp->scd_refcnt > 0); 15037 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 15038 15039 if ((old_scdp = sfmmup->sfmmu_scdp) != NULL) { 15040 ASSERT(old_scdp != scdp); 15041 15042 mutex_enter(&old_scdp->scd_mutex); 15043 sfmmu_from_scd_list(&old_scdp->scd_sf_list, sfmmup); 15044 mutex_exit(&old_scdp->scd_mutex); 15045 /* 15046 * sfmmup leaves the old scd. Update sfmmu_ttecnt to 15047 * include the shme rgn ttecnt for rgns that 15048 * were in the old SCD 15049 */ 15050 for (i = 0; i < mmu_page_sizes; i++) { 15051 ASSERT(sfmmup->sfmmu_scdrttecnt[i] == 15052 old_scdp->scd_rttecnt[i]); 15053 atomic_add_long(&sfmmup->sfmmu_ttecnt[i], 15054 sfmmup->sfmmu_scdrttecnt[i]); 15055 } 15056 } 15057 15058 /* 15059 * Move sfmmu to the scd lists. 15060 */ 15061 mutex_enter(&scdp->scd_mutex); 15062 sfmmu_to_scd_list(&scdp->scd_sf_list, sfmmup); 15063 mutex_exit(&scdp->scd_mutex); 15064 SF_SCD_INCR_REF(scdp); 15065 15066 hatlockp = sfmmu_hat_enter(sfmmup); 15067 /* 15068 * For a multi-thread process, we must stop 15069 * all the other threads before joining the scd. 15070 */ 15071 15072 SFMMU_FLAGS_SET(sfmmup, HAT_JOIN_SCD); 15073 15074 sfmmu_invalidate_ctx(sfmmup); 15075 sfmmup->sfmmu_scdp = scdp; 15076 15077 /* 15078 * Copy scd_rttecnt into sfmmup's sfmmu_scdrttecnt, and update 15079 * sfmmu_ttecnt to not include the rgn ttecnt just joined in SCD. 15080 */ 15081 for (i = 0; i < mmu_page_sizes; i++) { 15082 sfmmup->sfmmu_scdrttecnt[i] = scdp->scd_rttecnt[i]; 15083 ASSERT(sfmmup->sfmmu_ttecnt[i] >= scdp->scd_rttecnt[i]); 15084 atomic_add_long(&sfmmup->sfmmu_ttecnt[i], 15085 -sfmmup->sfmmu_scdrttecnt[i]); 15086 } 15087 /* update tsb0 inflation count */ 15088 if (old_scdp != NULL) { 15089 sfmmup->sfmmu_tsb0_4minflcnt += 15090 old_scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt; 15091 } 15092 ASSERT(sfmmup->sfmmu_tsb0_4minflcnt >= 15093 scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt); 15094 sfmmup->sfmmu_tsb0_4minflcnt -= scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt; 15095 15096 sfmmu_hat_exit(hatlockp); 15097 15098 if (old_scdp != NULL) { 15099 SF_SCD_DECR_REF(srdp, old_scdp); 15100 } 15101 15102 } 15103 15104 /* 15105 * This routine is called by a process to become part of an SCD. It is called 15106 * from sfmmu_tsbmiss_exception() once most of the initial work has been 15107 * done by sfmmu_join_scd(). This routine must not drop the hat lock. 15108 */ 15109 static void 15110 sfmmu_finish_join_scd(sfmmu_t *sfmmup) 15111 { 15112 struct tsb_info *tsbinfop; 15113 15114 ASSERT(sfmmu_hat_lock_held(sfmmup)); 15115 ASSERT(sfmmup->sfmmu_scdp != NULL); 15116 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)); 15117 ASSERT(!SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)); 15118 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ALLCTX_INVALID)); 15119 15120 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL; 15121 tsbinfop = tsbinfop->tsb_next) { 15122 if (tsbinfop->tsb_flags & TSB_SWAPPED) { 15123 continue; 15124 } 15125 ASSERT(!(tsbinfop->tsb_flags & TSB_RELOC_FLAG)); 15126 15127 sfmmu_inv_tsb(tsbinfop->tsb_va, 15128 TSB_BYTES(tsbinfop->tsb_szc)); 15129 } 15130 15131 /* Set HAT_CTX1_FLAG for all SCD ISMs */ 15132 sfmmu_ism_hatflags(sfmmup, 1); 15133 15134 SFMMU_STAT(sf_join_scd); 15135 } 15136 15137 /* 15138 * This routine is called in order to check if there is an SCD which matches 15139 * the process's region map if not then a new SCD may be created. 15140 */ 15141 static void 15142 sfmmu_find_scd(sfmmu_t *sfmmup) 15143 { 15144 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 15145 sf_scd_t *scdp, *new_scdp; 15146 int ret; 15147 15148 ASSERT(srdp != NULL); 15149 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 15150 15151 mutex_enter(&srdp->srd_scd_mutex); 15152 for (scdp = srdp->srd_scdp; scdp != NULL; 15153 scdp = scdp->scd_next) { 15154 SF_RGNMAP_EQUAL(&scdp->scd_region_map, 15155 &sfmmup->sfmmu_region_map, ret); 15156 if (ret == 1) { 15157 SF_SCD_INCR_REF(scdp); 15158 mutex_exit(&srdp->srd_scd_mutex); 15159 sfmmu_join_scd(scdp, sfmmup); 15160 ASSERT(scdp->scd_refcnt >= 2); 15161 atomic_add_32((volatile uint32_t *) 15162 &scdp->scd_refcnt, -1); 15163 return; 15164 } else { 15165 /* 15166 * If the sfmmu region map is a subset of the scd 15167 * region map, then the assumption is that this process 15168 * will continue attaching to ISM segments until the 15169 * region maps are equal. 15170 */ 15171 SF_RGNMAP_IS_SUBSET(&scdp->scd_region_map, 15172 &sfmmup->sfmmu_region_map, ret); 15173 if (ret == 1) { 15174 mutex_exit(&srdp->srd_scd_mutex); 15175 return; 15176 } 15177 } 15178 } 15179 15180 ASSERT(scdp == NULL); 15181 /* 15182 * No matching SCD has been found, create a new one. 15183 */ 15184 if ((new_scdp = sfmmu_alloc_scd(srdp, &sfmmup->sfmmu_region_map)) == 15185 NULL) { 15186 mutex_exit(&srdp->srd_scd_mutex); 15187 return; 15188 } 15189 15190 /* 15191 * sfmmu_alloc_scd() returns with a ref count of 1 on the scd. 15192 */ 15193 15194 /* Set scd_rttecnt for shme rgns in SCD */ 15195 sfmmu_set_scd_rttecnt(srdp, new_scdp); 15196 15197 /* 15198 * Link scd onto srd_scdp list and scd sfmmu onto region/iment lists. 15199 */ 15200 sfmmu_link_scd_to_regions(srdp, new_scdp); 15201 sfmmu_add_scd(&srdp->srd_scdp, new_scdp); 15202 SFMMU_STAT_ADD(sf_create_scd, 1); 15203 15204 mutex_exit(&srdp->srd_scd_mutex); 15205 sfmmu_join_scd(new_scdp, sfmmup); 15206 ASSERT(new_scdp->scd_refcnt >= 2); 15207 atomic_add_32((volatile uint32_t *)&new_scdp->scd_refcnt, -1); 15208 } 15209 15210 /* 15211 * This routine is called by a process to remove itself from an SCD. It is 15212 * either called when the processes has detached from a segment or from 15213 * hat_free_start() as a result of calling exit. 15214 */ 15215 static void 15216 sfmmu_leave_scd(sfmmu_t *sfmmup, uchar_t r_type) 15217 { 15218 sf_scd_t *scdp = sfmmup->sfmmu_scdp; 15219 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 15220 hatlock_t *hatlockp = TSB_HASH(sfmmup); 15221 int i; 15222 15223 ASSERT(scdp != NULL); 15224 ASSERT(srdp != NULL); 15225 15226 if (sfmmup->sfmmu_free) { 15227 /* 15228 * If the process is part of an SCD the sfmmu is unlinked 15229 * from scd_sf_list. 15230 */ 15231 mutex_enter(&scdp->scd_mutex); 15232 sfmmu_from_scd_list(&scdp->scd_sf_list, sfmmup); 15233 mutex_exit(&scdp->scd_mutex); 15234 /* 15235 * Update sfmmu_ttecnt to include the rgn ttecnt for rgns that 15236 * are about to leave the SCD 15237 */ 15238 for (i = 0; i < mmu_page_sizes; i++) { 15239 ASSERT(sfmmup->sfmmu_scdrttecnt[i] == 15240 scdp->scd_rttecnt[i]); 15241 atomic_add_long(&sfmmup->sfmmu_ttecnt[i], 15242 sfmmup->sfmmu_scdrttecnt[i]); 15243 sfmmup->sfmmu_scdrttecnt[i] = 0; 15244 } 15245 sfmmup->sfmmu_scdp = NULL; 15246 15247 SF_SCD_DECR_REF(srdp, scdp); 15248 return; 15249 } 15250 15251 ASSERT(r_type != SFMMU_REGION_ISM || 15252 SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)); 15253 ASSERT(scdp->scd_refcnt); 15254 ASSERT(!sfmmup->sfmmu_free); 15255 ASSERT(sfmmu_hat_lock_held(sfmmup)); 15256 ASSERT(AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 15257 15258 /* 15259 * Wait for ISM maps to be updated. 15260 */ 15261 if (r_type != SFMMU_REGION_ISM) { 15262 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY) && 15263 sfmmup->sfmmu_scdp != NULL) { 15264 cv_wait(&sfmmup->sfmmu_tsb_cv, 15265 HATLOCK_MUTEXP(hatlockp)); 15266 } 15267 15268 if (sfmmup->sfmmu_scdp == NULL) { 15269 sfmmu_hat_exit(hatlockp); 15270 return; 15271 } 15272 SFMMU_FLAGS_SET(sfmmup, HAT_ISMBUSY); 15273 } 15274 15275 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)) { 15276 SFMMU_FLAGS_CLEAR(sfmmup, HAT_JOIN_SCD); 15277 /* 15278 * Since HAT_JOIN_SCD was set our context 15279 * is still invalid. 15280 */ 15281 } else { 15282 /* 15283 * For a multi-thread process, we must stop 15284 * all the other threads before leaving the scd. 15285 */ 15286 15287 sfmmu_invalidate_ctx(sfmmup); 15288 } 15289 15290 /* Clear all the rid's for ISM, delete flags, etc */ 15291 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)); 15292 sfmmu_ism_hatflags(sfmmup, 0); 15293 15294 /* 15295 * Update sfmmu_ttecnt to include the rgn ttecnt for rgns that 15296 * are in SCD before this sfmmup leaves the SCD. 15297 */ 15298 for (i = 0; i < mmu_page_sizes; i++) { 15299 ASSERT(sfmmup->sfmmu_scdrttecnt[i] == 15300 scdp->scd_rttecnt[i]); 15301 atomic_add_long(&sfmmup->sfmmu_ttecnt[i], 15302 sfmmup->sfmmu_scdrttecnt[i]); 15303 sfmmup->sfmmu_scdrttecnt[i] = 0; 15304 /* update ismttecnt to include SCD ism before hat leaves SCD */ 15305 sfmmup->sfmmu_ismttecnt[i] += sfmmup->sfmmu_scdismttecnt[i]; 15306 sfmmup->sfmmu_scdismttecnt[i] = 0; 15307 } 15308 /* update tsb0 inflation count */ 15309 sfmmup->sfmmu_tsb0_4minflcnt += scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt; 15310 15311 if (r_type != SFMMU_REGION_ISM) { 15312 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ISMBUSY); 15313 } 15314 sfmmup->sfmmu_scdp = NULL; 15315 15316 sfmmu_hat_exit(hatlockp); 15317 15318 /* 15319 * Unlink sfmmu from scd_sf_list this can be done without holding 15320 * the hat lock as we hold the sfmmu_as lock which prevents 15321 * hat_join_region from adding this thread to the scd again. Other 15322 * threads check if sfmmu_scdp is NULL under hat lock and if it's NULL 15323 * they won't get here, since sfmmu_leave_scd() clears sfmmu_scdp 15324 * while holding the hat lock. 15325 */ 15326 mutex_enter(&scdp->scd_mutex); 15327 sfmmu_from_scd_list(&scdp->scd_sf_list, sfmmup); 15328 mutex_exit(&scdp->scd_mutex); 15329 SFMMU_STAT(sf_leave_scd); 15330 15331 SF_SCD_DECR_REF(srdp, scdp); 15332 hatlockp = sfmmu_hat_enter(sfmmup); 15333 15334 } 15335 15336 /* 15337 * Unlink and free up an SCD structure with a reference count of 0. 15338 */ 15339 static void 15340 sfmmu_destroy_scd(sf_srd_t *srdp, sf_scd_t *scdp, sf_region_map_t *scd_rmap) 15341 { 15342 sfmmu_t *scsfmmup; 15343 sf_scd_t *sp; 15344 hatlock_t *shatlockp; 15345 int i, ret; 15346 15347 mutex_enter(&srdp->srd_scd_mutex); 15348 for (sp = srdp->srd_scdp; sp != NULL; sp = sp->scd_next) { 15349 if (sp == scdp) 15350 break; 15351 } 15352 if (sp == NULL || sp->scd_refcnt) { 15353 mutex_exit(&srdp->srd_scd_mutex); 15354 return; 15355 } 15356 15357 /* 15358 * It is possible that the scd has been freed and reallocated with a 15359 * different region map while we've been waiting for the srd_scd_mutex. 15360 */ 15361 SF_RGNMAP_EQUAL(scd_rmap, &sp->scd_region_map, ret); 15362 if (ret != 1) { 15363 mutex_exit(&srdp->srd_scd_mutex); 15364 return; 15365 } 15366 15367 ASSERT(scdp->scd_sf_list == NULL); 15368 /* 15369 * Unlink scd from srd_scdp list. 15370 */ 15371 sfmmu_remove_scd(&srdp->srd_scdp, scdp); 15372 mutex_exit(&srdp->srd_scd_mutex); 15373 15374 sfmmu_unlink_scd_from_regions(srdp, scdp); 15375 15376 /* Clear shared context tsb and release ctx */ 15377 scsfmmup = scdp->scd_sfmmup; 15378 15379 /* 15380 * create a barrier so that scd will not be destroyed 15381 * if other thread still holds the same shared hat lock. 15382 * E.g., sfmmu_tsbmiss_exception() needs to acquire the 15383 * shared hat lock before checking the shared tsb reloc flag. 15384 */ 15385 shatlockp = sfmmu_hat_enter(scsfmmup); 15386 sfmmu_hat_exit(shatlockp); 15387 15388 sfmmu_free_scd_tsbs(scsfmmup); 15389 15390 for (i = 0; i < SFMMU_L1_HMERLINKS; i++) { 15391 if (scsfmmup->sfmmu_hmeregion_links[i] != NULL) { 15392 kmem_free(scsfmmup->sfmmu_hmeregion_links[i], 15393 SFMMU_L2_HMERLINKS_SIZE); 15394 scsfmmup->sfmmu_hmeregion_links[i] = NULL; 15395 } 15396 } 15397 kmem_cache_free(sfmmuid_cache, scsfmmup); 15398 kmem_cache_free(scd_cache, scdp); 15399 SFMMU_STAT(sf_destroy_scd); 15400 } 15401 15402 /* 15403 * Modifies the HAT_CTX1_FLAG for each of the ISM segments which correspond to 15404 * bits which are set in the ism_region_map parameter. This flag indicates to 15405 * the tsbmiss handler that mapping for these segments should be loaded using 15406 * the shared context. 15407 */ 15408 static void 15409 sfmmu_ism_hatflags(sfmmu_t *sfmmup, int addflag) 15410 { 15411 sf_scd_t *scdp = sfmmup->sfmmu_scdp; 15412 ism_blk_t *ism_blkp; 15413 ism_map_t *ism_map; 15414 int i, rid; 15415 15416 ASSERT(sfmmup->sfmmu_iblk != NULL); 15417 ASSERT(scdp != NULL); 15418 /* 15419 * Note that the caller either set HAT_ISMBUSY flag or checked 15420 * under hat lock that HAT_ISMBUSY was not set by another thread. 15421 */ 15422 ASSERT(sfmmu_hat_lock_held(sfmmup)); 15423 15424 ism_blkp = sfmmup->sfmmu_iblk; 15425 while (ism_blkp != NULL) { 15426 ism_map = ism_blkp->iblk_maps; 15427 for (i = 0; ism_map[i].imap_ismhat && i < ISM_MAP_SLOTS; i++) { 15428 rid = ism_map[i].imap_rid; 15429 if (rid == SFMMU_INVALID_ISMRID) { 15430 continue; 15431 } 15432 ASSERT(rid >= 0 && rid < SFMMU_MAX_ISM_REGIONS); 15433 if (SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid) && 15434 addflag) { 15435 ism_map[i].imap_hatflags |= 15436 HAT_CTX1_FLAG; 15437 } else { 15438 ism_map[i].imap_hatflags &= 15439 ~HAT_CTX1_FLAG; 15440 } 15441 } 15442 ism_blkp = ism_blkp->iblk_next; 15443 } 15444 } 15445 15446 static int 15447 sfmmu_srd_lock_held(sf_srd_t *srdp) 15448 { 15449 return (MUTEX_HELD(&srdp->srd_mutex)); 15450 } 15451 15452 /* ARGSUSED */ 15453 static int 15454 sfmmu_scdcache_constructor(void *buf, void *cdrarg, int kmflags) 15455 { 15456 sf_scd_t *scdp = (sf_scd_t *)buf; 15457 15458 bzero(buf, sizeof (sf_scd_t)); 15459 mutex_init(&scdp->scd_mutex, NULL, MUTEX_DEFAULT, NULL); 15460 return (0); 15461 } 15462 15463 /* ARGSUSED */ 15464 static void 15465 sfmmu_scdcache_destructor(void *buf, void *cdrarg) 15466 { 15467 sf_scd_t *scdp = (sf_scd_t *)buf; 15468 15469 mutex_destroy(&scdp->scd_mutex); 15470 } 15471