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 /* don't simulate dirty bit for writeable ISM/DISM mappings */ 2971 if ((flags & HAT_LOAD_SHARE) && TTE_IS_WRITABLE(ttep)) { 2972 TTE_SET_REF(ttep); 2973 TTE_SET_MOD(ttep); 2974 } 2975 2976 if ((flags & HAT_LOAD_SHARE) || !TTE_IS_REF(ttep) || 2977 !TTE_IS_MOD(ttep)) { 2978 /* 2979 * Don't load TSB for dummy as in ISM. Also don't preload 2980 * the TSB if the TTE isn't writable since we're likely to 2981 * fault on it again -- preloading can be fairly expensive. 2982 */ 2983 flags |= SFMMU_NO_TSBLOAD; 2984 } 2985 2986 size = TTE_CSZ(ttep); 2987 switch (size) { 2988 case TTE8K: 2989 SFMMU_STAT(sf_tteload8k); 2990 break; 2991 case TTE64K: 2992 SFMMU_STAT(sf_tteload64k); 2993 break; 2994 case TTE512K: 2995 SFMMU_STAT(sf_tteload512k); 2996 break; 2997 case TTE4M: 2998 SFMMU_STAT(sf_tteload4m); 2999 break; 3000 case (TTE32M): 3001 SFMMU_STAT(sf_tteload32m); 3002 ASSERT(mmu_page_sizes == max_mmu_page_sizes); 3003 break; 3004 case (TTE256M): 3005 SFMMU_STAT(sf_tteload256m); 3006 ASSERT(mmu_page_sizes == max_mmu_page_sizes); 3007 break; 3008 } 3009 3010 ASSERT(!((uintptr_t)vaddr & TTE_PAGE_OFFSET(size))); 3011 SFMMU_VALIDATE_HMERID(sfmmup, rid, vaddr, TTEBYTES(size)); 3012 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || hmeblkp->hblk_shared); 3013 ASSERT(SFMMU_IS_SHMERID_VALID(rid) || !hmeblkp->hblk_shared); 3014 3015 HBLKTOHME_IDX(sfhme, hmeblkp, vaddr, hmenum); 3016 3017 /* 3018 * Need to grab mlist lock here so that pageunload 3019 * will not change tte behind us. 3020 */ 3021 if (pp) { 3022 pml = sfmmu_mlist_enter(pp); 3023 } 3024 3025 sfmmu_copytte(&sfhme->hme_tte, &tteold); 3026 /* 3027 * Look for corresponding hment and if valid verify 3028 * pfns are equal. 3029 */ 3030 remap = TTE_IS_VALID(&tteold); 3031 if (remap) { 3032 pfn_t new_pfn, old_pfn; 3033 3034 old_pfn = TTE_TO_PFN(vaddr, &tteold); 3035 new_pfn = TTE_TO_PFN(vaddr, ttep); 3036 3037 if (flags & HAT_LOAD_REMAP) { 3038 /* make sure we are remapping same type of pages */ 3039 if (pf_is_memory(old_pfn) != pf_is_memory(new_pfn)) { 3040 panic("sfmmu_tteload - tte remap io<->memory"); 3041 } 3042 if (old_pfn != new_pfn && 3043 (pp != NULL || sfhme->hme_page != NULL)) { 3044 panic("sfmmu_tteload - tte remap pp != NULL"); 3045 } 3046 } else if (old_pfn != new_pfn) { 3047 panic("sfmmu_tteload - tte remap, hmeblkp 0x%p", 3048 (void *)hmeblkp); 3049 } 3050 ASSERT(TTE_CSZ(&tteold) == TTE_CSZ(ttep)); 3051 } 3052 3053 if (pp) { 3054 if (size == TTE8K) { 3055 #ifdef VAC 3056 /* 3057 * Handle VAC consistency 3058 */ 3059 if (!remap && (cache & CACHE_VAC) && !PP_ISNC(pp)) { 3060 sfmmu_vac_conflict(sfmmup, vaddr, pp); 3061 } 3062 #endif 3063 3064 if (TTE_IS_WRITABLE(ttep) && PP_ISRO(pp)) { 3065 pmtx = sfmmu_page_enter(pp); 3066 PP_CLRRO(pp); 3067 sfmmu_page_exit(pmtx); 3068 } else if (!PP_ISMAPPED(pp) && 3069 (!TTE_IS_WRITABLE(ttep)) && !(PP_ISMOD(pp))) { 3070 pmtx = sfmmu_page_enter(pp); 3071 if (!(PP_ISMOD(pp))) { 3072 PP_SETRO(pp); 3073 } 3074 sfmmu_page_exit(pmtx); 3075 } 3076 3077 } else if (sfmmu_pagearray_setup(vaddr, pps, ttep, remap)) { 3078 /* 3079 * sfmmu_pagearray_setup failed so return 3080 */ 3081 sfmmu_mlist_exit(pml); 3082 return (1); 3083 } 3084 } 3085 3086 /* 3087 * Make sure hment is not on a mapping list. 3088 */ 3089 ASSERT(remap || (sfhme->hme_page == NULL)); 3090 3091 /* if it is not a remap then hme->next better be NULL */ 3092 ASSERT((!remap) ? sfhme->hme_next == NULL : 1); 3093 3094 if (flags & HAT_LOAD_LOCK) { 3095 if ((hmeblkp->hblk_lckcnt + 1) >= MAX_HBLK_LCKCNT) { 3096 panic("too high lckcnt-hmeblk %p", 3097 (void *)hmeblkp); 3098 } 3099 atomic_add_32(&hmeblkp->hblk_lckcnt, 1); 3100 3101 HBLK_STACK_TRACE(hmeblkp, HBLK_LOCK); 3102 } 3103 3104 #ifdef VAC 3105 if (pp && PP_ISNC(pp)) { 3106 /* 3107 * If the physical page is marked to be uncacheable, like 3108 * by a vac conflict, make sure the new mapping is also 3109 * uncacheable. 3110 */ 3111 TTE_CLR_VCACHEABLE(ttep); 3112 ASSERT(PP_GET_VCOLOR(pp) == NO_VCOLOR); 3113 } 3114 #endif 3115 ttep->tte_hmenum = hmenum; 3116 3117 #ifdef DEBUG 3118 orig_old = tteold; 3119 #endif /* DEBUG */ 3120 3121 while (sfmmu_modifytte_try(&tteold, ttep, &sfhme->hme_tte) < 0) { 3122 if ((sfmmup == KHATID) && 3123 (flags & (HAT_LOAD_LOCK | HAT_LOAD_REMAP))) { 3124 sfmmu_copytte(&sfhme->hme_tte, &tteold); 3125 } 3126 #ifdef DEBUG 3127 chk_tte(&orig_old, &tteold, ttep, hmeblkp); 3128 #endif /* DEBUG */ 3129 } 3130 ASSERT(TTE_IS_VALID(&sfhme->hme_tte)); 3131 3132 if (!TTE_IS_VALID(&tteold)) { 3133 3134 atomic_add_16(&hmeblkp->hblk_vcnt, 1); 3135 if (rid == SFMMU_INVALID_SHMERID) { 3136 atomic_add_long(&sfmmup->sfmmu_ttecnt[size], 1); 3137 } else { 3138 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 3139 sf_region_t *rgnp = srdp->srd_hmergnp[rid]; 3140 /* 3141 * We already accounted for region ttecnt's in sfmmu 3142 * during hat_join_region() processing. Here we 3143 * only update ttecnt's in region struture. 3144 */ 3145 atomic_add_long(&rgnp->rgn_ttecnt[size], 1); 3146 } 3147 } 3148 3149 myflt = (astosfmmu(curthread->t_procp->p_as) == sfmmup); 3150 if (size > TTE8K && (flags & HAT_LOAD_SHARE) == 0 && 3151 sfmmup != ksfmmup) { 3152 uchar_t tteflag = 1 << size; 3153 if (rid == SFMMU_INVALID_SHMERID) { 3154 if (!(sfmmup->sfmmu_tteflags & tteflag)) { 3155 hatlockp = sfmmu_hat_enter(sfmmup); 3156 sfmmup->sfmmu_tteflags |= tteflag; 3157 sfmmu_hat_exit(hatlockp); 3158 } 3159 } else if (!(sfmmup->sfmmu_rtteflags & tteflag)) { 3160 hatlockp = sfmmu_hat_enter(sfmmup); 3161 sfmmup->sfmmu_rtteflags |= tteflag; 3162 sfmmu_hat_exit(hatlockp); 3163 } 3164 /* 3165 * Update the current CPU tsbmiss area, so the current thread 3166 * won't need to take the tsbmiss for the new pagesize. 3167 * The other threads in the process will update their tsb 3168 * miss area lazily in sfmmu_tsbmiss_exception() when they 3169 * fail to find the translation for a newly added pagesize. 3170 */ 3171 if (size > TTE64K && myflt) { 3172 struct tsbmiss *tsbmp; 3173 kpreempt_disable(); 3174 tsbmp = &tsbmiss_area[CPU->cpu_id]; 3175 if (rid == SFMMU_INVALID_SHMERID) { 3176 if (!(tsbmp->uhat_tteflags & tteflag)) { 3177 tsbmp->uhat_tteflags |= tteflag; 3178 } 3179 } else { 3180 if (!(tsbmp->uhat_rtteflags & tteflag)) { 3181 tsbmp->uhat_rtteflags |= tteflag; 3182 } 3183 } 3184 kpreempt_enable(); 3185 } 3186 } 3187 3188 if (size >= TTE4M && (flags & HAT_LOAD_TEXT) && 3189 !SFMMU_FLAGS_ISSET(sfmmup, HAT_4MTEXT_FLAG)) { 3190 hatlockp = sfmmu_hat_enter(sfmmup); 3191 SFMMU_FLAGS_SET(sfmmup, HAT_4MTEXT_FLAG); 3192 sfmmu_hat_exit(hatlockp); 3193 } 3194 3195 flush_tte.tte_intlo = (tteold.tte_intlo ^ ttep->tte_intlo) & 3196 hw_tte.tte_intlo; 3197 flush_tte.tte_inthi = (tteold.tte_inthi ^ ttep->tte_inthi) & 3198 hw_tte.tte_inthi; 3199 3200 if (remap && (flush_tte.tte_inthi || flush_tte.tte_intlo)) { 3201 /* 3202 * If remap and new tte differs from old tte we need 3203 * to sync the mod bit and flush TLB/TSB. We don't 3204 * need to sync ref bit because we currently always set 3205 * ref bit in tteload. 3206 */ 3207 ASSERT(TTE_IS_REF(ttep)); 3208 if (TTE_IS_MOD(&tteold)) { 3209 sfmmu_ttesync(sfmmup, vaddr, &tteold, pp); 3210 } 3211 /* 3212 * hwtte bits shouldn't change for SRD hmeblks as long as SRD 3213 * hmes are only used for read only text. Adding this code for 3214 * completeness and future use of shared hmeblks with writable 3215 * mappings of VMODSORT vnodes. 3216 */ 3217 if (hmeblkp->hblk_shared) { 3218 cpuset_t cpuset = sfmmu_rgntlb_demap(vaddr, 3219 sfmmup->sfmmu_srdp->srd_hmergnp[rid], hmeblkp, 1); 3220 xt_sync(cpuset); 3221 SFMMU_STAT_ADD(sf_region_remap_demap, 1); 3222 } else { 3223 sfmmu_tlb_demap(vaddr, sfmmup, hmeblkp, 0, 0); 3224 xt_sync(sfmmup->sfmmu_cpusran); 3225 } 3226 } 3227 3228 if ((flags & SFMMU_NO_TSBLOAD) == 0) { 3229 /* 3230 * We only preload 8K and 4M mappings into the TSB, since 3231 * 64K and 512K mappings are replicated and hence don't 3232 * have a single, unique TSB entry. Ditto for 32M/256M. 3233 */ 3234 if (size == TTE8K || size == TTE4M) { 3235 sf_scd_t *scdp; 3236 hatlockp = sfmmu_hat_enter(sfmmup); 3237 /* 3238 * Don't preload private TSB if the mapping is used 3239 * by the shctx in the SCD. 3240 */ 3241 scdp = sfmmup->sfmmu_scdp; 3242 if (rid == SFMMU_INVALID_SHMERID || scdp == NULL || 3243 !SF_RGNMAP_TEST(scdp->scd_hmeregion_map, rid)) { 3244 sfmmu_load_tsb(sfmmup, vaddr, &sfhme->hme_tte, 3245 size); 3246 } 3247 sfmmu_hat_exit(hatlockp); 3248 } 3249 } 3250 if (pp) { 3251 if (!remap) { 3252 HME_ADD(sfhme, pp); 3253 atomic_add_16(&hmeblkp->hblk_hmecnt, 1); 3254 ASSERT(hmeblkp->hblk_hmecnt > 0); 3255 3256 /* 3257 * Cannot ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS) 3258 * see pageunload() for comment. 3259 */ 3260 } 3261 sfmmu_mlist_exit(pml); 3262 } 3263 3264 return (0); 3265 } 3266 /* 3267 * Function unlocks hash bucket. 3268 */ 3269 static void 3270 sfmmu_tteload_release_hashbucket(struct hmehash_bucket *hmebp) 3271 { 3272 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 3273 SFMMU_HASH_UNLOCK(hmebp); 3274 } 3275 3276 /* 3277 * function which checks and sets up page array for a large 3278 * translation. Will set p_vcolor, p_index, p_ro fields. 3279 * Assumes addr and pfnum of first page are properly aligned. 3280 * Will check for physical contiguity. If check fails it return 3281 * non null. 3282 */ 3283 static int 3284 sfmmu_pagearray_setup(caddr_t addr, page_t **pps, tte_t *ttep, int remap) 3285 { 3286 int i, index, ttesz; 3287 pfn_t pfnum; 3288 pgcnt_t npgs; 3289 page_t *pp, *pp1; 3290 kmutex_t *pmtx; 3291 #ifdef VAC 3292 int osz; 3293 int cflags = 0; 3294 int vac_err = 0; 3295 #endif 3296 int newidx = 0; 3297 3298 ttesz = TTE_CSZ(ttep); 3299 3300 ASSERT(ttesz > TTE8K); 3301 3302 npgs = TTEPAGES(ttesz); 3303 index = PAGESZ_TO_INDEX(ttesz); 3304 3305 pfnum = (*pps)->p_pagenum; 3306 ASSERT(IS_P2ALIGNED(pfnum, npgs)); 3307 3308 /* 3309 * Save the first pp so we can do HAT_TMPNC at the end. 3310 */ 3311 pp1 = *pps; 3312 #ifdef VAC 3313 osz = fnd_mapping_sz(pp1); 3314 #endif 3315 3316 for (i = 0; i < npgs; i++, pps++) { 3317 pp = *pps; 3318 ASSERT(PAGE_LOCKED(pp)); 3319 ASSERT(pp->p_szc >= ttesz); 3320 ASSERT(pp->p_szc == pp1->p_szc); 3321 ASSERT(sfmmu_mlist_held(pp)); 3322 3323 /* 3324 * XXX is it possible to maintain P_RO on the root only? 3325 */ 3326 if (TTE_IS_WRITABLE(ttep) && PP_ISRO(pp)) { 3327 pmtx = sfmmu_page_enter(pp); 3328 PP_CLRRO(pp); 3329 sfmmu_page_exit(pmtx); 3330 } else if (!PP_ISMAPPED(pp) && !TTE_IS_WRITABLE(ttep) && 3331 !PP_ISMOD(pp)) { 3332 pmtx = sfmmu_page_enter(pp); 3333 if (!(PP_ISMOD(pp))) { 3334 PP_SETRO(pp); 3335 } 3336 sfmmu_page_exit(pmtx); 3337 } 3338 3339 /* 3340 * If this is a remap we skip vac & contiguity checks. 3341 */ 3342 if (remap) 3343 continue; 3344 3345 /* 3346 * set p_vcolor and detect any vac conflicts. 3347 */ 3348 #ifdef VAC 3349 if (vac_err == 0) { 3350 vac_err = sfmmu_vacconflict_array(addr, pp, &cflags); 3351 3352 } 3353 #endif 3354 3355 /* 3356 * Save current index in case we need to undo it. 3357 * Note: "PAGESZ_TO_INDEX(sz) (1 << (sz))" 3358 * "SFMMU_INDEX_SHIFT 6" 3359 * "SFMMU_INDEX_MASK ((1 << SFMMU_INDEX_SHIFT) - 1)" 3360 * "PP_MAPINDEX(p_index) (p_index & SFMMU_INDEX_MASK)" 3361 * 3362 * So: index = PAGESZ_TO_INDEX(ttesz); 3363 * if ttesz == 1 then index = 0x2 3364 * 2 then index = 0x4 3365 * 3 then index = 0x8 3366 * 4 then index = 0x10 3367 * 5 then index = 0x20 3368 * The code below checks if it's a new pagesize (ie, newidx) 3369 * in case we need to take it back out of p_index, 3370 * and then or's the new index into the existing index. 3371 */ 3372 if ((PP_MAPINDEX(pp) & index) == 0) 3373 newidx = 1; 3374 pp->p_index = (PP_MAPINDEX(pp) | index); 3375 3376 /* 3377 * contiguity check 3378 */ 3379 if (pp->p_pagenum != pfnum) { 3380 /* 3381 * If we fail the contiguity test then 3382 * the only thing we need to fix is the p_index field. 3383 * We might get a few extra flushes but since this 3384 * path is rare that is ok. The p_ro field will 3385 * get automatically fixed on the next tteload to 3386 * the page. NO TNC bit is set yet. 3387 */ 3388 while (i >= 0) { 3389 pp = *pps; 3390 if (newidx) 3391 pp->p_index = (PP_MAPINDEX(pp) & 3392 ~index); 3393 pps--; 3394 i--; 3395 } 3396 return (1); 3397 } 3398 pfnum++; 3399 addr += MMU_PAGESIZE; 3400 } 3401 3402 #ifdef VAC 3403 if (vac_err) { 3404 if (ttesz > osz) { 3405 /* 3406 * There are some smaller mappings that causes vac 3407 * conflicts. Convert all existing small mappings to 3408 * TNC. 3409 */ 3410 SFMMU_STAT_ADD(sf_uncache_conflict, npgs); 3411 sfmmu_page_cache_array(pp1, HAT_TMPNC, CACHE_FLUSH, 3412 npgs); 3413 } else { 3414 /* EMPTY */ 3415 /* 3416 * If there exists an big page mapping, 3417 * that means the whole existing big page 3418 * has TNC setting already. No need to covert to 3419 * TNC again. 3420 */ 3421 ASSERT(PP_ISTNC(pp1)); 3422 } 3423 } 3424 #endif /* VAC */ 3425 3426 return (0); 3427 } 3428 3429 #ifdef VAC 3430 /* 3431 * Routine that detects vac consistency for a large page. It also 3432 * sets virtual color for all pp's for this big mapping. 3433 */ 3434 static int 3435 sfmmu_vacconflict_array(caddr_t addr, page_t *pp, int *cflags) 3436 { 3437 int vcolor, ocolor; 3438 3439 ASSERT(sfmmu_mlist_held(pp)); 3440 3441 if (PP_ISNC(pp)) { 3442 return (HAT_TMPNC); 3443 } 3444 3445 vcolor = addr_to_vcolor(addr); 3446 if (PP_NEWPAGE(pp)) { 3447 PP_SET_VCOLOR(pp, vcolor); 3448 return (0); 3449 } 3450 3451 ocolor = PP_GET_VCOLOR(pp); 3452 if (ocolor == vcolor) { 3453 return (0); 3454 } 3455 3456 if (!PP_ISMAPPED(pp)) { 3457 /* 3458 * Previous user of page had a differnet color 3459 * but since there are no current users 3460 * we just flush the cache and change the color. 3461 * As an optimization for large pages we flush the 3462 * entire cache of that color and set a flag. 3463 */ 3464 SFMMU_STAT(sf_pgcolor_conflict); 3465 if (!CacheColor_IsFlushed(*cflags, ocolor)) { 3466 CacheColor_SetFlushed(*cflags, ocolor); 3467 sfmmu_cache_flushcolor(ocolor, pp->p_pagenum); 3468 } 3469 PP_SET_VCOLOR(pp, vcolor); 3470 return (0); 3471 } 3472 3473 /* 3474 * We got a real conflict with a current mapping. 3475 * set flags to start unencaching all mappings 3476 * and return failure so we restart looping 3477 * the pp array from the beginning. 3478 */ 3479 return (HAT_TMPNC); 3480 } 3481 #endif /* VAC */ 3482 3483 /* 3484 * creates a large page shadow hmeblk for a tte. 3485 * The purpose of this routine is to allow us to do quick unloads because 3486 * the vm layer can easily pass a very large but sparsely populated range. 3487 */ 3488 static struct hme_blk * 3489 sfmmu_shadow_hcreate(sfmmu_t *sfmmup, caddr_t vaddr, int ttesz, uint_t flags) 3490 { 3491 struct hmehash_bucket *hmebp; 3492 hmeblk_tag hblktag; 3493 int hmeshift, size, vshift; 3494 uint_t shw_mask, newshw_mask; 3495 struct hme_blk *hmeblkp; 3496 3497 ASSERT(sfmmup != KHATID); 3498 if (mmu_page_sizes == max_mmu_page_sizes) { 3499 ASSERT(ttesz < TTE256M); 3500 } else { 3501 ASSERT(ttesz < TTE4M); 3502 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0); 3503 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0); 3504 } 3505 3506 if (ttesz == TTE8K) { 3507 size = TTE512K; 3508 } else { 3509 size = ++ttesz; 3510 } 3511 3512 hblktag.htag_id = sfmmup; 3513 hmeshift = HME_HASH_SHIFT(size); 3514 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift); 3515 hblktag.htag_rehash = HME_HASH_REHASH(size); 3516 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 3517 hmebp = HME_HASH_FUNCTION(sfmmup, vaddr, hmeshift); 3518 3519 SFMMU_HASH_LOCK(hmebp); 3520 3521 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 3522 ASSERT(hmeblkp != (struct hme_blk *)hblk_reserve); 3523 if (hmeblkp == NULL) { 3524 hmeblkp = sfmmu_hblk_alloc(sfmmup, vaddr, hmebp, size, 3525 hblktag, flags, SFMMU_INVALID_SHMERID); 3526 } 3527 ASSERT(hmeblkp); 3528 if (!hmeblkp->hblk_shw_mask) { 3529 /* 3530 * if this is a unused hblk it was just allocated or could 3531 * potentially be a previous large page hblk so we need to 3532 * set the shadow bit. 3533 */ 3534 ASSERT(!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt); 3535 hmeblkp->hblk_shw_bit = 1; 3536 } else if (hmeblkp->hblk_shw_bit == 0) { 3537 panic("sfmmu_shadow_hcreate: shw bit not set in hmeblkp 0x%p", 3538 (void *)hmeblkp); 3539 } 3540 ASSERT(hmeblkp->hblk_shw_bit == 1); 3541 ASSERT(!hmeblkp->hblk_shared); 3542 vshift = vaddr_to_vshift(hblktag, vaddr, size); 3543 ASSERT(vshift < 8); 3544 /* 3545 * Atomically set shw mask bit 3546 */ 3547 do { 3548 shw_mask = hmeblkp->hblk_shw_mask; 3549 newshw_mask = shw_mask | (1 << vshift); 3550 newshw_mask = cas32(&hmeblkp->hblk_shw_mask, shw_mask, 3551 newshw_mask); 3552 } while (newshw_mask != shw_mask); 3553 3554 SFMMU_HASH_UNLOCK(hmebp); 3555 3556 return (hmeblkp); 3557 } 3558 3559 /* 3560 * This routine cleanup a previous shadow hmeblk and changes it to 3561 * a regular hblk. This happens rarely but it is possible 3562 * when a process wants to use large pages and there are hblks still 3563 * lying around from the previous as that used these hmeblks. 3564 * The alternative was to cleanup the shadow hblks at unload time 3565 * but since so few user processes actually use large pages, it is 3566 * better to be lazy and cleanup at this time. 3567 */ 3568 static void 3569 sfmmu_shadow_hcleanup(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, 3570 struct hmehash_bucket *hmebp) 3571 { 3572 caddr_t addr, endaddr; 3573 int hashno, size; 3574 3575 ASSERT(hmeblkp->hblk_shw_bit); 3576 ASSERT(!hmeblkp->hblk_shared); 3577 3578 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp)); 3579 3580 if (!hmeblkp->hblk_shw_mask) { 3581 hmeblkp->hblk_shw_bit = 0; 3582 return; 3583 } 3584 addr = (caddr_t)get_hblk_base(hmeblkp); 3585 endaddr = get_hblk_endaddr(hmeblkp); 3586 size = get_hblk_ttesz(hmeblkp); 3587 hashno = size - 1; 3588 ASSERT(hashno > 0); 3589 SFMMU_HASH_UNLOCK(hmebp); 3590 3591 sfmmu_free_hblks(sfmmup, addr, endaddr, hashno); 3592 3593 SFMMU_HASH_LOCK(hmebp); 3594 } 3595 3596 static void 3597 sfmmu_free_hblks(sfmmu_t *sfmmup, caddr_t addr, caddr_t endaddr, 3598 int hashno) 3599 { 3600 int hmeshift, shadow = 0; 3601 hmeblk_tag hblktag; 3602 struct hmehash_bucket *hmebp; 3603 struct hme_blk *hmeblkp; 3604 struct hme_blk *nx_hblk, *pr_hblk, *list = NULL; 3605 uint64_t hblkpa, prevpa, nx_pa; 3606 3607 ASSERT(hashno > 0); 3608 hblktag.htag_id = sfmmup; 3609 hblktag.htag_rehash = hashno; 3610 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 3611 3612 hmeshift = HME_HASH_SHIFT(hashno); 3613 3614 while (addr < endaddr) { 3615 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 3616 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 3617 SFMMU_HASH_LOCK(hmebp); 3618 /* inline HME_HASH_SEARCH */ 3619 hmeblkp = hmebp->hmeblkp; 3620 hblkpa = hmebp->hmeh_nextpa; 3621 prevpa = 0; 3622 pr_hblk = NULL; 3623 while (hmeblkp) { 3624 ASSERT(hblkpa == va_to_pa((caddr_t)hmeblkp)); 3625 if (HTAGS_EQ(hmeblkp->hblk_tag, hblktag)) { 3626 /* found hme_blk */ 3627 ASSERT(!hmeblkp->hblk_shared); 3628 if (hmeblkp->hblk_shw_bit) { 3629 if (hmeblkp->hblk_shw_mask) { 3630 shadow = 1; 3631 sfmmu_shadow_hcleanup(sfmmup, 3632 hmeblkp, hmebp); 3633 break; 3634 } else { 3635 hmeblkp->hblk_shw_bit = 0; 3636 } 3637 } 3638 3639 /* 3640 * Hblk_hmecnt and hblk_vcnt could be non zero 3641 * since hblk_unload() does not gurantee that. 3642 * 3643 * XXX - this could cause tteload() to spin 3644 * where sfmmu_shadow_hcleanup() is called. 3645 */ 3646 } 3647 3648 nx_hblk = hmeblkp->hblk_next; 3649 nx_pa = hmeblkp->hblk_nextpa; 3650 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) { 3651 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, 3652 pr_hblk); 3653 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 3654 } else { 3655 pr_hblk = hmeblkp; 3656 prevpa = hblkpa; 3657 } 3658 hmeblkp = nx_hblk; 3659 hblkpa = nx_pa; 3660 } 3661 3662 SFMMU_HASH_UNLOCK(hmebp); 3663 3664 if (shadow) { 3665 /* 3666 * We found another shadow hblk so cleaned its 3667 * children. We need to go back and cleanup 3668 * the original hblk so we don't change the 3669 * addr. 3670 */ 3671 shadow = 0; 3672 } else { 3673 addr = (caddr_t)roundup((uintptr_t)addr + 1, 3674 (1 << hmeshift)); 3675 } 3676 } 3677 sfmmu_hblks_list_purge(&list); 3678 } 3679 3680 /* 3681 * This routine's job is to delete stale invalid shared hmeregions hmeblks that 3682 * may still linger on after pageunload. 3683 */ 3684 static void 3685 sfmmu_cleanup_rhblk(sf_srd_t *srdp, caddr_t addr, uint_t rid, int ttesz) 3686 { 3687 int hmeshift; 3688 hmeblk_tag hblktag; 3689 struct hmehash_bucket *hmebp; 3690 struct hme_blk *hmeblkp; 3691 struct hme_blk *pr_hblk; 3692 struct hme_blk *list = NULL; 3693 uint64_t hblkpa, prevpa; 3694 3695 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 3696 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 3697 3698 hmeshift = HME_HASH_SHIFT(ttesz); 3699 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 3700 hblktag.htag_rehash = ttesz; 3701 hblktag.htag_rid = rid; 3702 hblktag.htag_id = srdp; 3703 hmebp = HME_HASH_FUNCTION(srdp, addr, hmeshift); 3704 3705 SFMMU_HASH_LOCK(hmebp); 3706 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, hblkpa, pr_hblk, 3707 prevpa, &list); 3708 if (hmeblkp != NULL) { 3709 ASSERT(hmeblkp->hblk_shared); 3710 ASSERT(!hmeblkp->hblk_shw_bit); 3711 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) { 3712 panic("sfmmu_cleanup_rhblk: valid hmeblk"); 3713 } 3714 ASSERT(!hmeblkp->hblk_lckcnt); 3715 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, pr_hblk); 3716 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 3717 } 3718 SFMMU_HASH_UNLOCK(hmebp); 3719 sfmmu_hblks_list_purge(&list); 3720 } 3721 3722 /* ARGSUSED */ 3723 static void 3724 sfmmu_rgn_cb_noop(caddr_t saddr, caddr_t eaddr, caddr_t r_saddr, 3725 size_t r_size, void *r_obj, u_offset_t r_objoff) 3726 { 3727 } 3728 3729 /* 3730 * Searches for an hmeblk which maps addr, then unloads this mapping 3731 * and updates *eaddrp, if the hmeblk is found. 3732 */ 3733 static void 3734 sfmmu_unload_hmeregion_va(sf_srd_t *srdp, uint_t rid, caddr_t addr, 3735 caddr_t eaddr, int ttesz, caddr_t *eaddrp) 3736 { 3737 int hmeshift; 3738 hmeblk_tag hblktag; 3739 struct hmehash_bucket *hmebp; 3740 struct hme_blk *hmeblkp; 3741 struct hme_blk *pr_hblk; 3742 struct hme_blk *list = NULL; 3743 uint64_t hblkpa, prevpa; 3744 3745 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 3746 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 3747 ASSERT(ttesz >= HBLK_MIN_TTESZ); 3748 3749 hmeshift = HME_HASH_SHIFT(ttesz); 3750 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 3751 hblktag.htag_rehash = ttesz; 3752 hblktag.htag_rid = rid; 3753 hblktag.htag_id = srdp; 3754 hmebp = HME_HASH_FUNCTION(srdp, addr, hmeshift); 3755 3756 SFMMU_HASH_LOCK(hmebp); 3757 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, hblkpa, pr_hblk, 3758 prevpa, &list); 3759 if (hmeblkp != NULL) { 3760 ASSERT(hmeblkp->hblk_shared); 3761 ASSERT(!hmeblkp->hblk_lckcnt); 3762 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) { 3763 *eaddrp = sfmmu_hblk_unload(NULL, hmeblkp, addr, 3764 eaddr, NULL, HAT_UNLOAD); 3765 ASSERT(*eaddrp > addr); 3766 } 3767 ASSERT(!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt); 3768 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, pr_hblk); 3769 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 3770 } 3771 SFMMU_HASH_UNLOCK(hmebp); 3772 sfmmu_hblks_list_purge(&list); 3773 } 3774 3775 static void 3776 sfmmu_unload_hmeregion(sf_srd_t *srdp, sf_region_t *rgnp) 3777 { 3778 int ttesz = rgnp->rgn_pgszc; 3779 size_t rsz = rgnp->rgn_size; 3780 caddr_t rsaddr = rgnp->rgn_saddr; 3781 caddr_t readdr = rsaddr + rsz; 3782 caddr_t rhsaddr; 3783 caddr_t va; 3784 uint_t rid = rgnp->rgn_id; 3785 caddr_t cbsaddr; 3786 caddr_t cbeaddr; 3787 hat_rgn_cb_func_t rcbfunc; 3788 ulong_t cnt; 3789 3790 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 3791 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 3792 3793 ASSERT(IS_P2ALIGNED(rsaddr, TTEBYTES(ttesz))); 3794 ASSERT(IS_P2ALIGNED(rsz, TTEBYTES(ttesz))); 3795 if (ttesz < HBLK_MIN_TTESZ) { 3796 ttesz = HBLK_MIN_TTESZ; 3797 rhsaddr = (caddr_t)P2ALIGN((uintptr_t)rsaddr, HBLK_MIN_BYTES); 3798 } else { 3799 rhsaddr = rsaddr; 3800 } 3801 3802 if ((rcbfunc = rgnp->rgn_cb_function) == NULL) { 3803 rcbfunc = sfmmu_rgn_cb_noop; 3804 } 3805 3806 while (ttesz >= HBLK_MIN_TTESZ) { 3807 cbsaddr = rsaddr; 3808 cbeaddr = rsaddr; 3809 if (!(rgnp->rgn_hmeflags & (1 << ttesz))) { 3810 ttesz--; 3811 continue; 3812 } 3813 cnt = 0; 3814 va = rsaddr; 3815 while (va < readdr) { 3816 ASSERT(va >= rhsaddr); 3817 if (va != cbeaddr) { 3818 if (cbeaddr != cbsaddr) { 3819 ASSERT(cbeaddr > cbsaddr); 3820 (*rcbfunc)(cbsaddr, cbeaddr, 3821 rsaddr, rsz, rgnp->rgn_obj, 3822 rgnp->rgn_objoff); 3823 } 3824 cbsaddr = va; 3825 cbeaddr = va; 3826 } 3827 sfmmu_unload_hmeregion_va(srdp, rid, va, readdr, 3828 ttesz, &cbeaddr); 3829 cnt++; 3830 va = rhsaddr + (cnt << TTE_PAGE_SHIFT(ttesz)); 3831 } 3832 if (cbeaddr != cbsaddr) { 3833 ASSERT(cbeaddr > cbsaddr); 3834 (*rcbfunc)(cbsaddr, cbeaddr, rsaddr, 3835 rsz, rgnp->rgn_obj, 3836 rgnp->rgn_objoff); 3837 } 3838 ttesz--; 3839 } 3840 } 3841 3842 /* 3843 * Release one hardware address translation lock on the given address range. 3844 */ 3845 void 3846 hat_unlock(struct hat *sfmmup, caddr_t addr, size_t len) 3847 { 3848 struct hmehash_bucket *hmebp; 3849 hmeblk_tag hblktag; 3850 int hmeshift, hashno = 1; 3851 struct hme_blk *hmeblkp, *list = NULL; 3852 caddr_t endaddr; 3853 3854 ASSERT(sfmmup != NULL); 3855 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 3856 3857 ASSERT((sfmmup == ksfmmup) || 3858 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 3859 ASSERT((len & MMU_PAGEOFFSET) == 0); 3860 endaddr = addr + len; 3861 hblktag.htag_id = sfmmup; 3862 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 3863 3864 /* 3865 * Spitfire supports 4 page sizes. 3866 * Most pages are expected to be of the smallest page size (8K) and 3867 * these will not need to be rehashed. 64K pages also don't need to be 3868 * rehashed because an hmeblk spans 64K of address space. 512K pages 3869 * might need 1 rehash and and 4M pages might need 2 rehashes. 3870 */ 3871 while (addr < endaddr) { 3872 hmeshift = HME_HASH_SHIFT(hashno); 3873 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 3874 hblktag.htag_rehash = hashno; 3875 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 3876 3877 SFMMU_HASH_LOCK(hmebp); 3878 3879 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 3880 if (hmeblkp != NULL) { 3881 ASSERT(!hmeblkp->hblk_shared); 3882 /* 3883 * If we encounter a shadow hmeblk then 3884 * we know there are no valid hmeblks mapping 3885 * this address at this size or larger. 3886 * Just increment address by the smallest 3887 * page size. 3888 */ 3889 if (hmeblkp->hblk_shw_bit) { 3890 addr += MMU_PAGESIZE; 3891 } else { 3892 addr = sfmmu_hblk_unlock(hmeblkp, addr, 3893 endaddr); 3894 } 3895 SFMMU_HASH_UNLOCK(hmebp); 3896 hashno = 1; 3897 continue; 3898 } 3899 SFMMU_HASH_UNLOCK(hmebp); 3900 3901 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 3902 /* 3903 * We have traversed the whole list and rehashed 3904 * if necessary without finding the address to unlock 3905 * which should never happen. 3906 */ 3907 panic("sfmmu_unlock: addr not found. " 3908 "addr %p hat %p", (void *)addr, (void *)sfmmup); 3909 } else { 3910 hashno++; 3911 } 3912 } 3913 3914 sfmmu_hblks_list_purge(&list); 3915 } 3916 3917 void 3918 hat_unlock_region(struct hat *sfmmup, caddr_t addr, size_t len, 3919 hat_region_cookie_t rcookie) 3920 { 3921 sf_srd_t *srdp; 3922 sf_region_t *rgnp; 3923 int ttesz; 3924 uint_t rid; 3925 caddr_t eaddr; 3926 caddr_t va; 3927 int hmeshift; 3928 hmeblk_tag hblktag; 3929 struct hmehash_bucket *hmebp; 3930 struct hme_blk *hmeblkp; 3931 struct hme_blk *pr_hblk; 3932 struct hme_blk *list; 3933 uint64_t hblkpa, prevpa; 3934 3935 if (rcookie == HAT_INVALID_REGION_COOKIE) { 3936 hat_unlock(sfmmup, addr, len); 3937 return; 3938 } 3939 3940 ASSERT(sfmmup != NULL); 3941 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 3942 ASSERT(sfmmup != ksfmmup); 3943 3944 srdp = sfmmup->sfmmu_srdp; 3945 rid = (uint_t)((uint64_t)rcookie); 3946 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 3947 eaddr = addr + len; 3948 va = addr; 3949 list = NULL; 3950 rgnp = srdp->srd_hmergnp[rid]; 3951 SFMMU_VALIDATE_HMERID(sfmmup, rid, addr, len); 3952 3953 ASSERT(IS_P2ALIGNED(addr, TTEBYTES(rgnp->rgn_pgszc))); 3954 ASSERT(IS_P2ALIGNED(len, TTEBYTES(rgnp->rgn_pgszc))); 3955 if (rgnp->rgn_pgszc < HBLK_MIN_TTESZ) { 3956 ttesz = HBLK_MIN_TTESZ; 3957 } else { 3958 ttesz = rgnp->rgn_pgszc; 3959 } 3960 while (va < eaddr) { 3961 while (ttesz < rgnp->rgn_pgszc && 3962 IS_P2ALIGNED(va, TTEBYTES(ttesz + 1))) { 3963 ttesz++; 3964 } 3965 while (ttesz >= HBLK_MIN_TTESZ) { 3966 if (!(rgnp->rgn_hmeflags & (1 << ttesz))) { 3967 ttesz--; 3968 continue; 3969 } 3970 hmeshift = HME_HASH_SHIFT(ttesz); 3971 hblktag.htag_bspage = HME_HASH_BSPAGE(va, hmeshift); 3972 hblktag.htag_rehash = ttesz; 3973 hblktag.htag_rid = rid; 3974 hblktag.htag_id = srdp; 3975 hmebp = HME_HASH_FUNCTION(srdp, addr, hmeshift); 3976 SFMMU_HASH_LOCK(hmebp); 3977 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, hblkpa, 3978 pr_hblk, prevpa, &list); 3979 if (hmeblkp == NULL) { 3980 SFMMU_HASH_UNLOCK(hmebp); 3981 ttesz--; 3982 continue; 3983 } 3984 ASSERT(hmeblkp->hblk_shared); 3985 va = sfmmu_hblk_unlock(hmeblkp, va, eaddr); 3986 ASSERT(va >= eaddr || 3987 IS_P2ALIGNED((uintptr_t)va, TTEBYTES(ttesz))); 3988 SFMMU_HASH_UNLOCK(hmebp); 3989 break; 3990 } 3991 if (ttesz < HBLK_MIN_TTESZ) { 3992 panic("hat_unlock_region: addr not found " 3993 "addr %p hat %p", va, sfmmup); 3994 } 3995 } 3996 sfmmu_hblks_list_purge(&list); 3997 } 3998 3999 /* 4000 * Function to unlock a range of addresses in an hmeblk. It returns the 4001 * next address that needs to be unlocked. 4002 * Should be called with the hash lock held. 4003 */ 4004 static caddr_t 4005 sfmmu_hblk_unlock(struct hme_blk *hmeblkp, caddr_t addr, caddr_t endaddr) 4006 { 4007 struct sf_hment *sfhme; 4008 tte_t tteold, ttemod; 4009 int ttesz, ret; 4010 4011 ASSERT(in_hblk_range(hmeblkp, addr)); 4012 ASSERT(hmeblkp->hblk_shw_bit == 0); 4013 4014 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 4015 ttesz = get_hblk_ttesz(hmeblkp); 4016 4017 HBLKTOHME(sfhme, hmeblkp, addr); 4018 while (addr < endaddr) { 4019 readtte: 4020 sfmmu_copytte(&sfhme->hme_tte, &tteold); 4021 if (TTE_IS_VALID(&tteold)) { 4022 4023 ttemod = tteold; 4024 4025 ret = sfmmu_modifytte_try(&tteold, &ttemod, 4026 &sfhme->hme_tte); 4027 4028 if (ret < 0) 4029 goto readtte; 4030 4031 if (hmeblkp->hblk_lckcnt == 0) 4032 panic("zero hblk lckcnt"); 4033 4034 if (((uintptr_t)addr + TTEBYTES(ttesz)) > 4035 (uintptr_t)endaddr) 4036 panic("can't unlock large tte"); 4037 4038 ASSERT(hmeblkp->hblk_lckcnt > 0); 4039 atomic_add_32(&hmeblkp->hblk_lckcnt, -1); 4040 HBLK_STACK_TRACE(hmeblkp, HBLK_UNLOCK); 4041 } else { 4042 panic("sfmmu_hblk_unlock: invalid tte"); 4043 } 4044 addr += TTEBYTES(ttesz); 4045 sfhme++; 4046 } 4047 return (addr); 4048 } 4049 4050 /* 4051 * Physical Address Mapping Framework 4052 * 4053 * General rules: 4054 * 4055 * (1) Applies only to seg_kmem memory pages. To make things easier, 4056 * seg_kpm addresses are also accepted by the routines, but nothing 4057 * is done with them since by definition their PA mappings are static. 4058 * (2) hat_add_callback() may only be called while holding the page lock 4059 * SE_SHARED or SE_EXCL of the underlying page (e.g., as_pagelock()), 4060 * or passing HAC_PAGELOCK flag. 4061 * (3) prehandler() and posthandler() may not call hat_add_callback() or 4062 * hat_delete_callback(), nor should they allocate memory. Post quiesce 4063 * callbacks may not sleep or acquire adaptive mutex locks. 4064 * (4) Either prehandler() or posthandler() (but not both) may be specified 4065 * as being NULL. Specifying an errhandler() is optional. 4066 * 4067 * Details of using the framework: 4068 * 4069 * registering a callback (hat_register_callback()) 4070 * 4071 * Pass prehandler, posthandler, errhandler addresses 4072 * as described below. If capture_cpus argument is nonzero, 4073 * suspend callback to the prehandler will occur with CPUs 4074 * captured and executing xc_loop() and CPUs will remain 4075 * captured until after the posthandler suspend callback 4076 * occurs. 4077 * 4078 * adding a callback (hat_add_callback()) 4079 * 4080 * as_pagelock(); 4081 * hat_add_callback(); 4082 * save returned pfn in private data structures or program registers; 4083 * as_pageunlock(); 4084 * 4085 * prehandler() 4086 * 4087 * Stop all accesses by physical address to this memory page. 4088 * Called twice: the first, PRESUSPEND, is a context safe to acquire 4089 * adaptive locks. The second, SUSPEND, is called at high PIL with 4090 * CPUs captured so adaptive locks may NOT be acquired (and all spin 4091 * locks must be XCALL_PIL or higher locks). 4092 * 4093 * May return the following errors: 4094 * EIO: A fatal error has occurred. This will result in panic. 4095 * EAGAIN: The page cannot be suspended. This will fail the 4096 * relocation. 4097 * 0: Success. 4098 * 4099 * posthandler() 4100 * 4101 * Save new pfn in private data structures or program registers; 4102 * not allowed to fail (non-zero return values will result in panic). 4103 * 4104 * errhandler() 4105 * 4106 * called when an error occurs related to the callback. Currently 4107 * the only such error is HAT_CB_ERR_LEAKED which indicates that 4108 * a page is being freed, but there are still outstanding callback(s) 4109 * registered on the page. 4110 * 4111 * removing a callback (hat_delete_callback(); e.g., prior to freeing memory) 4112 * 4113 * stop using physical address 4114 * hat_delete_callback(); 4115 * 4116 */ 4117 4118 /* 4119 * Register a callback class. Each subsystem should do this once and 4120 * cache the id_t returned for use in setting up and tearing down callbacks. 4121 * 4122 * There is no facility for removing callback IDs once they are created; 4123 * the "key" should be unique for each module, so in case a module is unloaded 4124 * and subsequently re-loaded, we can recycle the module's previous entry. 4125 */ 4126 id_t 4127 hat_register_callback(int key, 4128 int (*prehandler)(caddr_t, uint_t, uint_t, void *), 4129 int (*posthandler)(caddr_t, uint_t, uint_t, void *, pfn_t), 4130 int (*errhandler)(caddr_t, uint_t, uint_t, void *), 4131 int capture_cpus) 4132 { 4133 id_t id; 4134 4135 /* 4136 * Search the table for a pre-existing callback associated with 4137 * the identifier "key". If one exists, we re-use that entry in 4138 * the table for this instance, otherwise we assign the next 4139 * available table slot. 4140 */ 4141 for (id = 0; id < sfmmu_max_cb_id; id++) { 4142 if (sfmmu_cb_table[id].key == key) 4143 break; 4144 } 4145 4146 if (id == sfmmu_max_cb_id) { 4147 id = sfmmu_cb_nextid++; 4148 if (id >= sfmmu_max_cb_id) 4149 panic("hat_register_callback: out of callback IDs"); 4150 } 4151 4152 ASSERT(prehandler != NULL || posthandler != NULL); 4153 4154 sfmmu_cb_table[id].key = key; 4155 sfmmu_cb_table[id].prehandler = prehandler; 4156 sfmmu_cb_table[id].posthandler = posthandler; 4157 sfmmu_cb_table[id].errhandler = errhandler; 4158 sfmmu_cb_table[id].capture_cpus = capture_cpus; 4159 4160 return (id); 4161 } 4162 4163 #define HAC_COOKIE_NONE (void *)-1 4164 4165 /* 4166 * Add relocation callbacks to the specified addr/len which will be called 4167 * when relocating the associated page. See the description of pre and 4168 * posthandler above for more details. 4169 * 4170 * If HAC_PAGELOCK is included in flags, the underlying memory page is 4171 * locked internally so the caller must be able to deal with the callback 4172 * running even before this function has returned. If HAC_PAGELOCK is not 4173 * set, it is assumed that the underlying memory pages are locked. 4174 * 4175 * Since the caller must track the individual page boundaries anyway, 4176 * we only allow a callback to be added to a single page (large 4177 * or small). Thus [addr, addr + len) MUST be contained within a single 4178 * page. 4179 * 4180 * Registering multiple callbacks on the same [addr, addr+len) is supported, 4181 * _provided_that_ a unique parameter is specified for each callback. 4182 * If multiple callbacks are registered on the same range the callback will 4183 * be invoked with each unique parameter. Registering the same callback with 4184 * the same argument more than once will result in corrupted kernel state. 4185 * 4186 * Returns the pfn of the underlying kernel page in *rpfn 4187 * on success, or PFN_INVALID on failure. 4188 * 4189 * cookiep (if passed) provides storage space for an opaque cookie 4190 * to return later to hat_delete_callback(). This cookie makes the callback 4191 * deletion significantly quicker by avoiding a potentially lengthy hash 4192 * search. 4193 * 4194 * Returns values: 4195 * 0: success 4196 * ENOMEM: memory allocation failure (e.g. flags was passed as HAC_NOSLEEP) 4197 * EINVAL: callback ID is not valid 4198 * ENXIO: ["vaddr", "vaddr" + len) is not mapped in the kernel's address 4199 * space 4200 * ERANGE: ["vaddr", "vaddr" + len) crosses a page boundary 4201 */ 4202 int 4203 hat_add_callback(id_t callback_id, caddr_t vaddr, uint_t len, uint_t flags, 4204 void *pvt, pfn_t *rpfn, void **cookiep) 4205 { 4206 struct hmehash_bucket *hmebp; 4207 hmeblk_tag hblktag; 4208 struct hme_blk *hmeblkp; 4209 int hmeshift, hashno; 4210 caddr_t saddr, eaddr, baseaddr; 4211 struct pa_hment *pahmep; 4212 struct sf_hment *sfhmep, *osfhmep; 4213 kmutex_t *pml; 4214 tte_t tte; 4215 page_t *pp; 4216 vnode_t *vp; 4217 u_offset_t off; 4218 pfn_t pfn; 4219 int kmflags = (flags & HAC_SLEEP)? KM_SLEEP : KM_NOSLEEP; 4220 int locked = 0; 4221 4222 /* 4223 * For KPM mappings, just return the physical address since we 4224 * don't need to register any callbacks. 4225 */ 4226 if (IS_KPM_ADDR(vaddr)) { 4227 uint64_t paddr; 4228 SFMMU_KPM_VTOP(vaddr, paddr); 4229 *rpfn = btop(paddr); 4230 if (cookiep != NULL) 4231 *cookiep = HAC_COOKIE_NONE; 4232 return (0); 4233 } 4234 4235 if (callback_id < (id_t)0 || callback_id >= sfmmu_cb_nextid) { 4236 *rpfn = PFN_INVALID; 4237 return (EINVAL); 4238 } 4239 4240 if ((pahmep = kmem_cache_alloc(pa_hment_cache, kmflags)) == NULL) { 4241 *rpfn = PFN_INVALID; 4242 return (ENOMEM); 4243 } 4244 4245 sfhmep = &pahmep->sfment; 4246 4247 saddr = (caddr_t)((uintptr_t)vaddr & MMU_PAGEMASK); 4248 eaddr = saddr + len; 4249 4250 rehash: 4251 /* Find the mapping(s) for this page */ 4252 for (hashno = TTE64K, hmeblkp = NULL; 4253 hmeblkp == NULL && hashno <= mmu_hashcnt; 4254 hashno++) { 4255 hmeshift = HME_HASH_SHIFT(hashno); 4256 hblktag.htag_id = ksfmmup; 4257 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 4258 hblktag.htag_bspage = HME_HASH_BSPAGE(saddr, hmeshift); 4259 hblktag.htag_rehash = hashno; 4260 hmebp = HME_HASH_FUNCTION(ksfmmup, saddr, hmeshift); 4261 4262 SFMMU_HASH_LOCK(hmebp); 4263 4264 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 4265 4266 if (hmeblkp == NULL) 4267 SFMMU_HASH_UNLOCK(hmebp); 4268 } 4269 4270 if (hmeblkp == NULL) { 4271 kmem_cache_free(pa_hment_cache, pahmep); 4272 *rpfn = PFN_INVALID; 4273 return (ENXIO); 4274 } 4275 4276 ASSERT(!hmeblkp->hblk_shared); 4277 4278 HBLKTOHME(osfhmep, hmeblkp, saddr); 4279 sfmmu_copytte(&osfhmep->hme_tte, &tte); 4280 4281 if (!TTE_IS_VALID(&tte)) { 4282 SFMMU_HASH_UNLOCK(hmebp); 4283 kmem_cache_free(pa_hment_cache, pahmep); 4284 *rpfn = PFN_INVALID; 4285 return (ENXIO); 4286 } 4287 4288 /* 4289 * Make sure the boundaries for the callback fall within this 4290 * single mapping. 4291 */ 4292 baseaddr = (caddr_t)get_hblk_base(hmeblkp); 4293 ASSERT(saddr >= baseaddr); 4294 if (eaddr > saddr + TTEBYTES(TTE_CSZ(&tte))) { 4295 SFMMU_HASH_UNLOCK(hmebp); 4296 kmem_cache_free(pa_hment_cache, pahmep); 4297 *rpfn = PFN_INVALID; 4298 return (ERANGE); 4299 } 4300 4301 pfn = sfmmu_ttetopfn(&tte, vaddr); 4302 4303 /* 4304 * The pfn may not have a page_t underneath in which case we 4305 * just return it. This can happen if we are doing I/O to a 4306 * static portion of the kernel's address space, for instance. 4307 */ 4308 pp = osfhmep->hme_page; 4309 if (pp == NULL) { 4310 SFMMU_HASH_UNLOCK(hmebp); 4311 kmem_cache_free(pa_hment_cache, pahmep); 4312 *rpfn = pfn; 4313 if (cookiep) 4314 *cookiep = HAC_COOKIE_NONE; 4315 return (0); 4316 } 4317 ASSERT(pp == PP_PAGEROOT(pp)); 4318 4319 vp = pp->p_vnode; 4320 off = pp->p_offset; 4321 4322 pml = sfmmu_mlist_enter(pp); 4323 4324 if (flags & HAC_PAGELOCK) { 4325 if (!page_trylock(pp, SE_SHARED)) { 4326 /* 4327 * Somebody is holding SE_EXCL lock. Might 4328 * even be hat_page_relocate(). Drop all 4329 * our locks, lookup the page in &kvp, and 4330 * retry. If it doesn't exist in &kvp and &zvp, 4331 * then we must be dealing with a kernel mapped 4332 * page which doesn't actually belong to 4333 * segkmem so we punt. 4334 */ 4335 sfmmu_mlist_exit(pml); 4336 SFMMU_HASH_UNLOCK(hmebp); 4337 pp = page_lookup(&kvp, (u_offset_t)saddr, SE_SHARED); 4338 4339 /* check zvp before giving up */ 4340 if (pp == NULL) 4341 pp = page_lookup(&zvp, (u_offset_t)saddr, 4342 SE_SHARED); 4343 4344 /* Okay, we didn't find it, give up */ 4345 if (pp == NULL) { 4346 kmem_cache_free(pa_hment_cache, pahmep); 4347 *rpfn = pfn; 4348 if (cookiep) 4349 *cookiep = HAC_COOKIE_NONE; 4350 return (0); 4351 } 4352 page_unlock(pp); 4353 goto rehash; 4354 } 4355 locked = 1; 4356 } 4357 4358 if (!PAGE_LOCKED(pp) && !panicstr) 4359 panic("hat_add_callback: page 0x%p not locked", pp); 4360 4361 if (osfhmep->hme_page != pp || pp->p_vnode != vp || 4362 pp->p_offset != off) { 4363 /* 4364 * The page moved before we got our hands on it. Drop 4365 * all the locks and try again. 4366 */ 4367 ASSERT((flags & HAC_PAGELOCK) != 0); 4368 sfmmu_mlist_exit(pml); 4369 SFMMU_HASH_UNLOCK(hmebp); 4370 page_unlock(pp); 4371 locked = 0; 4372 goto rehash; 4373 } 4374 4375 if (!VN_ISKAS(vp)) { 4376 /* 4377 * This is not a segkmem page but another page which 4378 * has been kernel mapped. It had better have at least 4379 * a share lock on it. Return the pfn. 4380 */ 4381 sfmmu_mlist_exit(pml); 4382 SFMMU_HASH_UNLOCK(hmebp); 4383 if (locked) 4384 page_unlock(pp); 4385 kmem_cache_free(pa_hment_cache, pahmep); 4386 ASSERT(PAGE_LOCKED(pp)); 4387 *rpfn = pfn; 4388 if (cookiep) 4389 *cookiep = HAC_COOKIE_NONE; 4390 return (0); 4391 } 4392 4393 /* 4394 * Setup this pa_hment and link its embedded dummy sf_hment into 4395 * the mapping list. 4396 */ 4397 pp->p_share++; 4398 pahmep->cb_id = callback_id; 4399 pahmep->addr = vaddr; 4400 pahmep->len = len; 4401 pahmep->refcnt = 1; 4402 pahmep->flags = 0; 4403 pahmep->pvt = pvt; 4404 4405 sfhmep->hme_tte.ll = 0; 4406 sfhmep->hme_data = pahmep; 4407 sfhmep->hme_prev = osfhmep; 4408 sfhmep->hme_next = osfhmep->hme_next; 4409 4410 if (osfhmep->hme_next) 4411 osfhmep->hme_next->hme_prev = sfhmep; 4412 4413 osfhmep->hme_next = sfhmep; 4414 4415 sfmmu_mlist_exit(pml); 4416 SFMMU_HASH_UNLOCK(hmebp); 4417 4418 if (locked) 4419 page_unlock(pp); 4420 4421 *rpfn = pfn; 4422 if (cookiep) 4423 *cookiep = (void *)pahmep; 4424 4425 return (0); 4426 } 4427 4428 /* 4429 * Remove the relocation callbacks from the specified addr/len. 4430 */ 4431 void 4432 hat_delete_callback(caddr_t vaddr, uint_t len, void *pvt, uint_t flags, 4433 void *cookie) 4434 { 4435 struct hmehash_bucket *hmebp; 4436 hmeblk_tag hblktag; 4437 struct hme_blk *hmeblkp; 4438 int hmeshift, hashno; 4439 caddr_t saddr; 4440 struct pa_hment *pahmep; 4441 struct sf_hment *sfhmep, *osfhmep; 4442 kmutex_t *pml; 4443 tte_t tte; 4444 page_t *pp; 4445 vnode_t *vp; 4446 u_offset_t off; 4447 int locked = 0; 4448 4449 /* 4450 * If the cookie is HAC_COOKIE_NONE then there is no pa_hment to 4451 * remove so just return. 4452 */ 4453 if (cookie == HAC_COOKIE_NONE || IS_KPM_ADDR(vaddr)) 4454 return; 4455 4456 saddr = (caddr_t)((uintptr_t)vaddr & MMU_PAGEMASK); 4457 4458 rehash: 4459 /* Find the mapping(s) for this page */ 4460 for (hashno = TTE64K, hmeblkp = NULL; 4461 hmeblkp == NULL && hashno <= mmu_hashcnt; 4462 hashno++) { 4463 hmeshift = HME_HASH_SHIFT(hashno); 4464 hblktag.htag_id = ksfmmup; 4465 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 4466 hblktag.htag_bspage = HME_HASH_BSPAGE(saddr, hmeshift); 4467 hblktag.htag_rehash = hashno; 4468 hmebp = HME_HASH_FUNCTION(ksfmmup, saddr, hmeshift); 4469 4470 SFMMU_HASH_LOCK(hmebp); 4471 4472 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 4473 4474 if (hmeblkp == NULL) 4475 SFMMU_HASH_UNLOCK(hmebp); 4476 } 4477 4478 if (hmeblkp == NULL) 4479 return; 4480 4481 ASSERT(!hmeblkp->hblk_shared); 4482 4483 HBLKTOHME(osfhmep, hmeblkp, saddr); 4484 4485 sfmmu_copytte(&osfhmep->hme_tte, &tte); 4486 if (!TTE_IS_VALID(&tte)) { 4487 SFMMU_HASH_UNLOCK(hmebp); 4488 return; 4489 } 4490 4491 pp = osfhmep->hme_page; 4492 if (pp == NULL) { 4493 SFMMU_HASH_UNLOCK(hmebp); 4494 ASSERT(cookie == NULL); 4495 return; 4496 } 4497 4498 vp = pp->p_vnode; 4499 off = pp->p_offset; 4500 4501 pml = sfmmu_mlist_enter(pp); 4502 4503 if (flags & HAC_PAGELOCK) { 4504 if (!page_trylock(pp, SE_SHARED)) { 4505 /* 4506 * Somebody is holding SE_EXCL lock. Might 4507 * even be hat_page_relocate(). Drop all 4508 * our locks, lookup the page in &kvp, and 4509 * retry. If it doesn't exist in &kvp and &zvp, 4510 * then we must be dealing with a kernel mapped 4511 * page which doesn't actually belong to 4512 * segkmem so we punt. 4513 */ 4514 sfmmu_mlist_exit(pml); 4515 SFMMU_HASH_UNLOCK(hmebp); 4516 pp = page_lookup(&kvp, (u_offset_t)saddr, SE_SHARED); 4517 /* check zvp before giving up */ 4518 if (pp == NULL) 4519 pp = page_lookup(&zvp, (u_offset_t)saddr, 4520 SE_SHARED); 4521 4522 if (pp == NULL) { 4523 ASSERT(cookie == NULL); 4524 return; 4525 } 4526 page_unlock(pp); 4527 goto rehash; 4528 } 4529 locked = 1; 4530 } 4531 4532 ASSERT(PAGE_LOCKED(pp)); 4533 4534 if (osfhmep->hme_page != pp || pp->p_vnode != vp || 4535 pp->p_offset != off) { 4536 /* 4537 * The page moved before we got our hands on it. Drop 4538 * all the locks and try again. 4539 */ 4540 ASSERT((flags & HAC_PAGELOCK) != 0); 4541 sfmmu_mlist_exit(pml); 4542 SFMMU_HASH_UNLOCK(hmebp); 4543 page_unlock(pp); 4544 locked = 0; 4545 goto rehash; 4546 } 4547 4548 if (!VN_ISKAS(vp)) { 4549 /* 4550 * This is not a segkmem page but another page which 4551 * has been kernel mapped. 4552 */ 4553 sfmmu_mlist_exit(pml); 4554 SFMMU_HASH_UNLOCK(hmebp); 4555 if (locked) 4556 page_unlock(pp); 4557 ASSERT(cookie == NULL); 4558 return; 4559 } 4560 4561 if (cookie != NULL) { 4562 pahmep = (struct pa_hment *)cookie; 4563 sfhmep = &pahmep->sfment; 4564 } else { 4565 for (sfhmep = pp->p_mapping; sfhmep != NULL; 4566 sfhmep = sfhmep->hme_next) { 4567 4568 /* 4569 * skip va<->pa mappings 4570 */ 4571 if (!IS_PAHME(sfhmep)) 4572 continue; 4573 4574 pahmep = sfhmep->hme_data; 4575 ASSERT(pahmep != NULL); 4576 4577 /* 4578 * if pa_hment matches, remove it 4579 */ 4580 if ((pahmep->pvt == pvt) && 4581 (pahmep->addr == vaddr) && 4582 (pahmep->len == len)) { 4583 break; 4584 } 4585 } 4586 } 4587 4588 if (sfhmep == NULL) { 4589 if (!panicstr) { 4590 panic("hat_delete_callback: pa_hment not found, pp %p", 4591 (void *)pp); 4592 } 4593 return; 4594 } 4595 4596 /* 4597 * Note: at this point a valid kernel mapping must still be 4598 * present on this page. 4599 */ 4600 pp->p_share--; 4601 if (pp->p_share <= 0) 4602 panic("hat_delete_callback: zero p_share"); 4603 4604 if (--pahmep->refcnt == 0) { 4605 if (pahmep->flags != 0) 4606 panic("hat_delete_callback: pa_hment is busy"); 4607 4608 /* 4609 * Remove sfhmep from the mapping list for the page. 4610 */ 4611 if (sfhmep->hme_prev) { 4612 sfhmep->hme_prev->hme_next = sfhmep->hme_next; 4613 } else { 4614 pp->p_mapping = sfhmep->hme_next; 4615 } 4616 4617 if (sfhmep->hme_next) 4618 sfhmep->hme_next->hme_prev = sfhmep->hme_prev; 4619 4620 sfmmu_mlist_exit(pml); 4621 SFMMU_HASH_UNLOCK(hmebp); 4622 4623 if (locked) 4624 page_unlock(pp); 4625 4626 kmem_cache_free(pa_hment_cache, pahmep); 4627 return; 4628 } 4629 4630 sfmmu_mlist_exit(pml); 4631 SFMMU_HASH_UNLOCK(hmebp); 4632 if (locked) 4633 page_unlock(pp); 4634 } 4635 4636 /* 4637 * hat_probe returns 1 if the translation for the address 'addr' is 4638 * loaded, zero otherwise. 4639 * 4640 * hat_probe should be used only for advisorary purposes because it may 4641 * occasionally return the wrong value. The implementation must guarantee that 4642 * returning the wrong value is a very rare event. hat_probe is used 4643 * to implement optimizations in the segment drivers. 4644 * 4645 */ 4646 int 4647 hat_probe(struct hat *sfmmup, caddr_t addr) 4648 { 4649 pfn_t pfn; 4650 tte_t tte; 4651 4652 ASSERT(sfmmup != NULL); 4653 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 4654 4655 ASSERT((sfmmup == ksfmmup) || 4656 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 4657 4658 if (sfmmup == ksfmmup) { 4659 while ((pfn = sfmmu_vatopfn(addr, sfmmup, &tte)) 4660 == PFN_SUSPENDED) { 4661 sfmmu_vatopfn_suspended(addr, sfmmup, &tte); 4662 } 4663 } else { 4664 pfn = sfmmu_uvatopfn(addr, sfmmup, NULL); 4665 } 4666 4667 if (pfn != PFN_INVALID) 4668 return (1); 4669 else 4670 return (0); 4671 } 4672 4673 ssize_t 4674 hat_getpagesize(struct hat *sfmmup, caddr_t addr) 4675 { 4676 tte_t tte; 4677 4678 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 4679 4680 if (sfmmup == ksfmmup) { 4681 if (sfmmu_vatopfn(addr, sfmmup, &tte) == PFN_INVALID) { 4682 return (-1); 4683 } 4684 } else { 4685 if (sfmmu_uvatopfn(addr, sfmmup, &tte) == PFN_INVALID) { 4686 return (-1); 4687 } 4688 } 4689 4690 ASSERT(TTE_IS_VALID(&tte)); 4691 return (TTEBYTES(TTE_CSZ(&tte))); 4692 } 4693 4694 uint_t 4695 hat_getattr(struct hat *sfmmup, caddr_t addr, uint_t *attr) 4696 { 4697 tte_t tte; 4698 4699 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 4700 4701 if (sfmmup == ksfmmup) { 4702 if (sfmmu_vatopfn(addr, sfmmup, &tte) == PFN_INVALID) { 4703 tte.ll = 0; 4704 } 4705 } else { 4706 if (sfmmu_uvatopfn(addr, sfmmup, &tte) == PFN_INVALID) { 4707 tte.ll = 0; 4708 } 4709 } 4710 if (TTE_IS_VALID(&tte)) { 4711 *attr = sfmmu_ptov_attr(&tte); 4712 return (0); 4713 } 4714 *attr = 0; 4715 return ((uint_t)0xffffffff); 4716 } 4717 4718 /* 4719 * Enables more attributes on specified address range (ie. logical OR) 4720 */ 4721 void 4722 hat_setattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr) 4723 { 4724 if (hat->sfmmu_xhat_provider) { 4725 XHAT_SETATTR(hat, addr, len, attr); 4726 return; 4727 } else { 4728 /* 4729 * This must be a CPU HAT. If the address space has 4730 * XHATs attached, change attributes for all of them, 4731 * just in case 4732 */ 4733 ASSERT(hat->sfmmu_as != NULL); 4734 if (hat->sfmmu_as->a_xhat != NULL) 4735 xhat_setattr_all(hat->sfmmu_as, addr, len, attr); 4736 } 4737 4738 sfmmu_chgattr(hat, addr, len, attr, SFMMU_SETATTR); 4739 } 4740 4741 /* 4742 * Assigns attributes to the specified address range. All the attributes 4743 * are specified. 4744 */ 4745 void 4746 hat_chgattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr) 4747 { 4748 if (hat->sfmmu_xhat_provider) { 4749 XHAT_CHGATTR(hat, addr, len, attr); 4750 return; 4751 } else { 4752 /* 4753 * This must be a CPU HAT. If the address space has 4754 * XHATs attached, change attributes for all of them, 4755 * just in case 4756 */ 4757 ASSERT(hat->sfmmu_as != NULL); 4758 if (hat->sfmmu_as->a_xhat != NULL) 4759 xhat_chgattr_all(hat->sfmmu_as, addr, len, attr); 4760 } 4761 4762 sfmmu_chgattr(hat, addr, len, attr, SFMMU_CHGATTR); 4763 } 4764 4765 /* 4766 * Remove attributes on the specified address range (ie. loginal NAND) 4767 */ 4768 void 4769 hat_clrattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr) 4770 { 4771 if (hat->sfmmu_xhat_provider) { 4772 XHAT_CLRATTR(hat, addr, len, attr); 4773 return; 4774 } else { 4775 /* 4776 * This must be a CPU HAT. If the address space has 4777 * XHATs attached, change attributes for all of them, 4778 * just in case 4779 */ 4780 ASSERT(hat->sfmmu_as != NULL); 4781 if (hat->sfmmu_as->a_xhat != NULL) 4782 xhat_clrattr_all(hat->sfmmu_as, addr, len, attr); 4783 } 4784 4785 sfmmu_chgattr(hat, addr, len, attr, SFMMU_CLRATTR); 4786 } 4787 4788 /* 4789 * Change attributes on an address range to that specified by attr and mode. 4790 */ 4791 static void 4792 sfmmu_chgattr(struct hat *sfmmup, caddr_t addr, size_t len, uint_t attr, 4793 int mode) 4794 { 4795 struct hmehash_bucket *hmebp; 4796 hmeblk_tag hblktag; 4797 int hmeshift, hashno = 1; 4798 struct hme_blk *hmeblkp, *list = NULL; 4799 caddr_t endaddr; 4800 cpuset_t cpuset; 4801 demap_range_t dmr; 4802 4803 CPUSET_ZERO(cpuset); 4804 4805 ASSERT((sfmmup == ksfmmup) || 4806 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 4807 ASSERT((len & MMU_PAGEOFFSET) == 0); 4808 ASSERT(((uintptr_t)addr & MMU_PAGEOFFSET) == 0); 4809 4810 if ((attr & PROT_USER) && (mode != SFMMU_CLRATTR) && 4811 ((addr + len) > (caddr_t)USERLIMIT)) { 4812 panic("user addr %p in kernel space", 4813 (void *)addr); 4814 } 4815 4816 endaddr = addr + len; 4817 hblktag.htag_id = sfmmup; 4818 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 4819 DEMAP_RANGE_INIT(sfmmup, &dmr); 4820 4821 while (addr < endaddr) { 4822 hmeshift = HME_HASH_SHIFT(hashno); 4823 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 4824 hblktag.htag_rehash = hashno; 4825 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 4826 4827 SFMMU_HASH_LOCK(hmebp); 4828 4829 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 4830 if (hmeblkp != NULL) { 4831 ASSERT(!hmeblkp->hblk_shared); 4832 /* 4833 * We've encountered a shadow hmeblk so skip the range 4834 * of the next smaller mapping size. 4835 */ 4836 if (hmeblkp->hblk_shw_bit) { 4837 ASSERT(sfmmup != ksfmmup); 4838 ASSERT(hashno > 1); 4839 addr = (caddr_t)P2END((uintptr_t)addr, 4840 TTEBYTES(hashno - 1)); 4841 } else { 4842 addr = sfmmu_hblk_chgattr(sfmmup, 4843 hmeblkp, addr, endaddr, &dmr, attr, mode); 4844 } 4845 SFMMU_HASH_UNLOCK(hmebp); 4846 hashno = 1; 4847 continue; 4848 } 4849 SFMMU_HASH_UNLOCK(hmebp); 4850 4851 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 4852 /* 4853 * We have traversed the whole list and rehashed 4854 * if necessary without finding the address to chgattr. 4855 * This is ok, so we increment the address by the 4856 * smallest hmeblk range for kernel mappings or for 4857 * user mappings with no large pages, and the largest 4858 * hmeblk range, to account for shadow hmeblks, for 4859 * user mappings with large pages and continue. 4860 */ 4861 if (sfmmup == ksfmmup) 4862 addr = (caddr_t)P2END((uintptr_t)addr, 4863 TTEBYTES(1)); 4864 else 4865 addr = (caddr_t)P2END((uintptr_t)addr, 4866 TTEBYTES(hashno)); 4867 hashno = 1; 4868 } else { 4869 hashno++; 4870 } 4871 } 4872 4873 sfmmu_hblks_list_purge(&list); 4874 DEMAP_RANGE_FLUSH(&dmr); 4875 cpuset = sfmmup->sfmmu_cpusran; 4876 xt_sync(cpuset); 4877 } 4878 4879 /* 4880 * This function chgattr on a range of addresses in an hmeblk. It returns the 4881 * next addres that needs to be chgattr. 4882 * It should be called with the hash lock held. 4883 * XXX It should be possible to optimize chgattr by not flushing every time but 4884 * on the other hand: 4885 * 1. do one flush crosscall. 4886 * 2. only flush if we are increasing permissions (make sure this will work) 4887 */ 4888 static caddr_t 4889 sfmmu_hblk_chgattr(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 4890 caddr_t endaddr, demap_range_t *dmrp, uint_t attr, int mode) 4891 { 4892 tte_t tte, tteattr, tteflags, ttemod; 4893 struct sf_hment *sfhmep; 4894 int ttesz; 4895 struct page *pp = NULL; 4896 kmutex_t *pml, *pmtx; 4897 int ret; 4898 int use_demap_range; 4899 #if defined(SF_ERRATA_57) 4900 int check_exec; 4901 #endif 4902 4903 ASSERT(in_hblk_range(hmeblkp, addr)); 4904 ASSERT(hmeblkp->hblk_shw_bit == 0); 4905 ASSERT(!hmeblkp->hblk_shared); 4906 4907 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 4908 ttesz = get_hblk_ttesz(hmeblkp); 4909 4910 /* 4911 * Flush the current demap region if addresses have been 4912 * skipped or the page size doesn't match. 4913 */ 4914 use_demap_range = (TTEBYTES(ttesz) == DEMAP_RANGE_PGSZ(dmrp)); 4915 if (use_demap_range) { 4916 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr); 4917 } else { 4918 DEMAP_RANGE_FLUSH(dmrp); 4919 } 4920 4921 tteattr.ll = sfmmu_vtop_attr(attr, mode, &tteflags); 4922 #if defined(SF_ERRATA_57) 4923 check_exec = (sfmmup != ksfmmup) && 4924 AS_TYPE_64BIT(sfmmup->sfmmu_as) && 4925 TTE_IS_EXECUTABLE(&tteattr); 4926 #endif 4927 HBLKTOHME(sfhmep, hmeblkp, addr); 4928 while (addr < endaddr) { 4929 sfmmu_copytte(&sfhmep->hme_tte, &tte); 4930 if (TTE_IS_VALID(&tte)) { 4931 if ((tte.ll & tteflags.ll) == tteattr.ll) { 4932 /* 4933 * if the new attr is the same as old 4934 * continue 4935 */ 4936 goto next_addr; 4937 } 4938 if (!TTE_IS_WRITABLE(&tteattr)) { 4939 /* 4940 * make sure we clear hw modify bit if we 4941 * removing write protections 4942 */ 4943 tteflags.tte_intlo |= TTE_HWWR_INT; 4944 } 4945 4946 pml = NULL; 4947 pp = sfhmep->hme_page; 4948 if (pp) { 4949 pml = sfmmu_mlist_enter(pp); 4950 } 4951 4952 if (pp != sfhmep->hme_page) { 4953 /* 4954 * tte must have been unloaded. 4955 */ 4956 ASSERT(pml); 4957 sfmmu_mlist_exit(pml); 4958 continue; 4959 } 4960 4961 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 4962 4963 ttemod = tte; 4964 ttemod.ll = (ttemod.ll & ~tteflags.ll) | tteattr.ll; 4965 ASSERT(TTE_TO_TTEPFN(&ttemod) == TTE_TO_TTEPFN(&tte)); 4966 4967 #if defined(SF_ERRATA_57) 4968 if (check_exec && addr < errata57_limit) 4969 ttemod.tte_exec_perm = 0; 4970 #endif 4971 ret = sfmmu_modifytte_try(&tte, &ttemod, 4972 &sfhmep->hme_tte); 4973 4974 if (ret < 0) { 4975 /* tte changed underneath us */ 4976 if (pml) { 4977 sfmmu_mlist_exit(pml); 4978 } 4979 continue; 4980 } 4981 4982 if (tteflags.tte_intlo & TTE_HWWR_INT) { 4983 /* 4984 * need to sync if we are clearing modify bit. 4985 */ 4986 sfmmu_ttesync(sfmmup, addr, &tte, pp); 4987 } 4988 4989 if (pp && PP_ISRO(pp)) { 4990 if (tteattr.tte_intlo & TTE_WRPRM_INT) { 4991 pmtx = sfmmu_page_enter(pp); 4992 PP_CLRRO(pp); 4993 sfmmu_page_exit(pmtx); 4994 } 4995 } 4996 4997 if (ret > 0 && use_demap_range) { 4998 DEMAP_RANGE_MARKPG(dmrp, addr); 4999 } else if (ret > 0) { 5000 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 5001 } 5002 5003 if (pml) { 5004 sfmmu_mlist_exit(pml); 5005 } 5006 } 5007 next_addr: 5008 addr += TTEBYTES(ttesz); 5009 sfhmep++; 5010 DEMAP_RANGE_NEXTPG(dmrp); 5011 } 5012 return (addr); 5013 } 5014 5015 /* 5016 * This routine converts virtual attributes to physical ones. It will 5017 * update the tteflags field with the tte mask corresponding to the attributes 5018 * affected and it returns the new attributes. It will also clear the modify 5019 * bit if we are taking away write permission. This is necessary since the 5020 * modify bit is the hardware permission bit and we need to clear it in order 5021 * to detect write faults. 5022 */ 5023 static uint64_t 5024 sfmmu_vtop_attr(uint_t attr, int mode, tte_t *ttemaskp) 5025 { 5026 tte_t ttevalue; 5027 5028 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR)); 5029 5030 switch (mode) { 5031 case SFMMU_CHGATTR: 5032 /* all attributes specified */ 5033 ttevalue.tte_inthi = MAKE_TTEATTR_INTHI(attr); 5034 ttevalue.tte_intlo = MAKE_TTEATTR_INTLO(attr); 5035 ttemaskp->tte_inthi = TTEINTHI_ATTR; 5036 ttemaskp->tte_intlo = TTEINTLO_ATTR; 5037 break; 5038 case SFMMU_SETATTR: 5039 ASSERT(!(attr & ~HAT_PROT_MASK)); 5040 ttemaskp->ll = 0; 5041 ttevalue.ll = 0; 5042 /* 5043 * a valid tte implies exec and read for sfmmu 5044 * so no need to do anything about them. 5045 * since priviledged access implies user access 5046 * PROT_USER doesn't make sense either. 5047 */ 5048 if (attr & PROT_WRITE) { 5049 ttemaskp->tte_intlo |= TTE_WRPRM_INT; 5050 ttevalue.tte_intlo |= TTE_WRPRM_INT; 5051 } 5052 break; 5053 case SFMMU_CLRATTR: 5054 /* attributes will be nand with current ones */ 5055 if (attr & ~(PROT_WRITE | PROT_USER)) { 5056 panic("sfmmu: attr %x not supported", attr); 5057 } 5058 ttemaskp->ll = 0; 5059 ttevalue.ll = 0; 5060 if (attr & PROT_WRITE) { 5061 /* clear both writable and modify bit */ 5062 ttemaskp->tte_intlo |= TTE_WRPRM_INT | TTE_HWWR_INT; 5063 } 5064 if (attr & PROT_USER) { 5065 ttemaskp->tte_intlo |= TTE_PRIV_INT; 5066 ttevalue.tte_intlo |= TTE_PRIV_INT; 5067 } 5068 break; 5069 default: 5070 panic("sfmmu_vtop_attr: bad mode %x", mode); 5071 } 5072 ASSERT(TTE_TO_TTEPFN(&ttevalue) == 0); 5073 return (ttevalue.ll); 5074 } 5075 5076 static uint_t 5077 sfmmu_ptov_attr(tte_t *ttep) 5078 { 5079 uint_t attr; 5080 5081 ASSERT(TTE_IS_VALID(ttep)); 5082 5083 attr = PROT_READ; 5084 5085 if (TTE_IS_WRITABLE(ttep)) { 5086 attr |= PROT_WRITE; 5087 } 5088 if (TTE_IS_EXECUTABLE(ttep)) { 5089 attr |= PROT_EXEC; 5090 } 5091 if (!TTE_IS_PRIVILEGED(ttep)) { 5092 attr |= PROT_USER; 5093 } 5094 if (TTE_IS_NFO(ttep)) { 5095 attr |= HAT_NOFAULT; 5096 } 5097 if (TTE_IS_NOSYNC(ttep)) { 5098 attr |= HAT_NOSYNC; 5099 } 5100 if (TTE_IS_SIDEFFECT(ttep)) { 5101 attr |= SFMMU_SIDEFFECT; 5102 } 5103 if (!TTE_IS_VCACHEABLE(ttep)) { 5104 attr |= SFMMU_UNCACHEVTTE; 5105 } 5106 if (!TTE_IS_PCACHEABLE(ttep)) { 5107 attr |= SFMMU_UNCACHEPTTE; 5108 } 5109 return (attr); 5110 } 5111 5112 /* 5113 * hat_chgprot is a deprecated hat call. New segment drivers 5114 * should store all attributes and use hat_*attr calls. 5115 * 5116 * Change the protections in the virtual address range 5117 * given to the specified virtual protection. If vprot is ~PROT_WRITE, 5118 * then remove write permission, leaving the other 5119 * permissions unchanged. If vprot is ~PROT_USER, remove user permissions. 5120 * 5121 */ 5122 void 5123 hat_chgprot(struct hat *sfmmup, caddr_t addr, size_t len, uint_t vprot) 5124 { 5125 struct hmehash_bucket *hmebp; 5126 hmeblk_tag hblktag; 5127 int hmeshift, hashno = 1; 5128 struct hme_blk *hmeblkp, *list = NULL; 5129 caddr_t endaddr; 5130 cpuset_t cpuset; 5131 demap_range_t dmr; 5132 5133 ASSERT((len & MMU_PAGEOFFSET) == 0); 5134 ASSERT(((uintptr_t)addr & MMU_PAGEOFFSET) == 0); 5135 5136 if (sfmmup->sfmmu_xhat_provider) { 5137 XHAT_CHGPROT(sfmmup, addr, len, vprot); 5138 return; 5139 } else { 5140 /* 5141 * This must be a CPU HAT. If the address space has 5142 * XHATs attached, change attributes for all of them, 5143 * just in case 5144 */ 5145 ASSERT(sfmmup->sfmmu_as != NULL); 5146 if (sfmmup->sfmmu_as->a_xhat != NULL) 5147 xhat_chgprot_all(sfmmup->sfmmu_as, addr, len, vprot); 5148 } 5149 5150 CPUSET_ZERO(cpuset); 5151 5152 if ((vprot != (uint_t)~PROT_WRITE) && (vprot & PROT_USER) && 5153 ((addr + len) > (caddr_t)USERLIMIT)) { 5154 panic("user addr %p vprot %x in kernel space", 5155 (void *)addr, vprot); 5156 } 5157 endaddr = addr + len; 5158 hblktag.htag_id = sfmmup; 5159 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 5160 DEMAP_RANGE_INIT(sfmmup, &dmr); 5161 5162 while (addr < endaddr) { 5163 hmeshift = HME_HASH_SHIFT(hashno); 5164 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 5165 hblktag.htag_rehash = hashno; 5166 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 5167 5168 SFMMU_HASH_LOCK(hmebp); 5169 5170 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 5171 if (hmeblkp != NULL) { 5172 ASSERT(!hmeblkp->hblk_shared); 5173 /* 5174 * We've encountered a shadow hmeblk so skip the range 5175 * of the next smaller mapping size. 5176 */ 5177 if (hmeblkp->hblk_shw_bit) { 5178 ASSERT(sfmmup != ksfmmup); 5179 ASSERT(hashno > 1); 5180 addr = (caddr_t)P2END((uintptr_t)addr, 5181 TTEBYTES(hashno - 1)); 5182 } else { 5183 addr = sfmmu_hblk_chgprot(sfmmup, hmeblkp, 5184 addr, endaddr, &dmr, vprot); 5185 } 5186 SFMMU_HASH_UNLOCK(hmebp); 5187 hashno = 1; 5188 continue; 5189 } 5190 SFMMU_HASH_UNLOCK(hmebp); 5191 5192 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 5193 /* 5194 * We have traversed the whole list and rehashed 5195 * if necessary without finding the address to chgprot. 5196 * This is ok so we increment the address by the 5197 * smallest hmeblk range for kernel mappings and the 5198 * largest hmeblk range, to account for shadow hmeblks, 5199 * for user mappings and continue. 5200 */ 5201 if (sfmmup == ksfmmup) 5202 addr = (caddr_t)P2END((uintptr_t)addr, 5203 TTEBYTES(1)); 5204 else 5205 addr = (caddr_t)P2END((uintptr_t)addr, 5206 TTEBYTES(hashno)); 5207 hashno = 1; 5208 } else { 5209 hashno++; 5210 } 5211 } 5212 5213 sfmmu_hblks_list_purge(&list); 5214 DEMAP_RANGE_FLUSH(&dmr); 5215 cpuset = sfmmup->sfmmu_cpusran; 5216 xt_sync(cpuset); 5217 } 5218 5219 /* 5220 * This function chgprots a range of addresses in an hmeblk. It returns the 5221 * next addres that needs to be chgprot. 5222 * It should be called with the hash lock held. 5223 * XXX It shold be possible to optimize chgprot by not flushing every time but 5224 * on the other hand: 5225 * 1. do one flush crosscall. 5226 * 2. only flush if we are increasing permissions (make sure this will work) 5227 */ 5228 static caddr_t 5229 sfmmu_hblk_chgprot(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 5230 caddr_t endaddr, demap_range_t *dmrp, uint_t vprot) 5231 { 5232 uint_t pprot; 5233 tte_t tte, ttemod; 5234 struct sf_hment *sfhmep; 5235 uint_t tteflags; 5236 int ttesz; 5237 struct page *pp = NULL; 5238 kmutex_t *pml, *pmtx; 5239 int ret; 5240 int use_demap_range; 5241 #if defined(SF_ERRATA_57) 5242 int check_exec; 5243 #endif 5244 5245 ASSERT(in_hblk_range(hmeblkp, addr)); 5246 ASSERT(hmeblkp->hblk_shw_bit == 0); 5247 ASSERT(!hmeblkp->hblk_shared); 5248 5249 #ifdef DEBUG 5250 if (get_hblk_ttesz(hmeblkp) != TTE8K && 5251 (endaddr < get_hblk_endaddr(hmeblkp))) { 5252 panic("sfmmu_hblk_chgprot: partial chgprot of large page"); 5253 } 5254 #endif /* DEBUG */ 5255 5256 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 5257 ttesz = get_hblk_ttesz(hmeblkp); 5258 5259 pprot = sfmmu_vtop_prot(vprot, &tteflags); 5260 #if defined(SF_ERRATA_57) 5261 check_exec = (sfmmup != ksfmmup) && 5262 AS_TYPE_64BIT(sfmmup->sfmmu_as) && 5263 ((vprot & PROT_EXEC) == PROT_EXEC); 5264 #endif 5265 HBLKTOHME(sfhmep, hmeblkp, addr); 5266 5267 /* 5268 * Flush the current demap region if addresses have been 5269 * skipped or the page size doesn't match. 5270 */ 5271 use_demap_range = (TTEBYTES(ttesz) == MMU_PAGESIZE); 5272 if (use_demap_range) { 5273 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr); 5274 } else { 5275 DEMAP_RANGE_FLUSH(dmrp); 5276 } 5277 5278 while (addr < endaddr) { 5279 sfmmu_copytte(&sfhmep->hme_tte, &tte); 5280 if (TTE_IS_VALID(&tte)) { 5281 if (TTE_GET_LOFLAGS(&tte, tteflags) == pprot) { 5282 /* 5283 * if the new protection is the same as old 5284 * continue 5285 */ 5286 goto next_addr; 5287 } 5288 pml = NULL; 5289 pp = sfhmep->hme_page; 5290 if (pp) { 5291 pml = sfmmu_mlist_enter(pp); 5292 } 5293 if (pp != sfhmep->hme_page) { 5294 /* 5295 * tte most have been unloaded 5296 * underneath us. Recheck 5297 */ 5298 ASSERT(pml); 5299 sfmmu_mlist_exit(pml); 5300 continue; 5301 } 5302 5303 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 5304 5305 ttemod = tte; 5306 TTE_SET_LOFLAGS(&ttemod, tteflags, pprot); 5307 #if defined(SF_ERRATA_57) 5308 if (check_exec && addr < errata57_limit) 5309 ttemod.tte_exec_perm = 0; 5310 #endif 5311 ret = sfmmu_modifytte_try(&tte, &ttemod, 5312 &sfhmep->hme_tte); 5313 5314 if (ret < 0) { 5315 /* tte changed underneath us */ 5316 if (pml) { 5317 sfmmu_mlist_exit(pml); 5318 } 5319 continue; 5320 } 5321 5322 if (tteflags & TTE_HWWR_INT) { 5323 /* 5324 * need to sync if we are clearing modify bit. 5325 */ 5326 sfmmu_ttesync(sfmmup, addr, &tte, pp); 5327 } 5328 5329 if (pp && PP_ISRO(pp)) { 5330 if (pprot & TTE_WRPRM_INT) { 5331 pmtx = sfmmu_page_enter(pp); 5332 PP_CLRRO(pp); 5333 sfmmu_page_exit(pmtx); 5334 } 5335 } 5336 5337 if (ret > 0 && use_demap_range) { 5338 DEMAP_RANGE_MARKPG(dmrp, addr); 5339 } else if (ret > 0) { 5340 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 5341 } 5342 5343 if (pml) { 5344 sfmmu_mlist_exit(pml); 5345 } 5346 } 5347 next_addr: 5348 addr += TTEBYTES(ttesz); 5349 sfhmep++; 5350 DEMAP_RANGE_NEXTPG(dmrp); 5351 } 5352 return (addr); 5353 } 5354 5355 /* 5356 * This routine is deprecated and should only be used by hat_chgprot. 5357 * The correct routine is sfmmu_vtop_attr. 5358 * This routine converts virtual page protections to physical ones. It will 5359 * update the tteflags field with the tte mask corresponding to the protections 5360 * affected and it returns the new protections. It will also clear the modify 5361 * bit if we are taking away write permission. This is necessary since the 5362 * modify bit is the hardware permission bit and we need to clear it in order 5363 * to detect write faults. 5364 * It accepts the following special protections: 5365 * ~PROT_WRITE = remove write permissions. 5366 * ~PROT_USER = remove user permissions. 5367 */ 5368 static uint_t 5369 sfmmu_vtop_prot(uint_t vprot, uint_t *tteflagsp) 5370 { 5371 if (vprot == (uint_t)~PROT_WRITE) { 5372 *tteflagsp = TTE_WRPRM_INT | TTE_HWWR_INT; 5373 return (0); /* will cause wrprm to be cleared */ 5374 } 5375 if (vprot == (uint_t)~PROT_USER) { 5376 *tteflagsp = TTE_PRIV_INT; 5377 return (0); /* will cause privprm to be cleared */ 5378 } 5379 if ((vprot == 0) || (vprot == PROT_USER) || 5380 ((vprot & PROT_ALL) != vprot)) { 5381 panic("sfmmu_vtop_prot -- bad prot %x", vprot); 5382 } 5383 5384 switch (vprot) { 5385 case (PROT_READ): 5386 case (PROT_EXEC): 5387 case (PROT_EXEC | PROT_READ): 5388 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT | TTE_HWWR_INT; 5389 return (TTE_PRIV_INT); /* set prv and clr wrt */ 5390 case (PROT_WRITE): 5391 case (PROT_WRITE | PROT_READ): 5392 case (PROT_EXEC | PROT_WRITE): 5393 case (PROT_EXEC | PROT_WRITE | PROT_READ): 5394 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT; 5395 return (TTE_PRIV_INT | TTE_WRPRM_INT); /* set prv and wrt */ 5396 case (PROT_USER | PROT_READ): 5397 case (PROT_USER | PROT_EXEC): 5398 case (PROT_USER | PROT_EXEC | PROT_READ): 5399 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT | TTE_HWWR_INT; 5400 return (0); /* clr prv and wrt */ 5401 case (PROT_USER | PROT_WRITE): 5402 case (PROT_USER | PROT_WRITE | PROT_READ): 5403 case (PROT_USER | PROT_EXEC | PROT_WRITE): 5404 case (PROT_USER | PROT_EXEC | PROT_WRITE | PROT_READ): 5405 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT; 5406 return (TTE_WRPRM_INT); /* clr prv and set wrt */ 5407 default: 5408 panic("sfmmu_vtop_prot -- bad prot %x", vprot); 5409 } 5410 return (0); 5411 } 5412 5413 /* 5414 * Alternate unload for very large virtual ranges. With a true 64 bit VA, 5415 * the normal algorithm would take too long for a very large VA range with 5416 * few real mappings. This routine just walks thru all HMEs in the global 5417 * hash table to find and remove mappings. 5418 */ 5419 static void 5420 hat_unload_large_virtual( 5421 struct hat *sfmmup, 5422 caddr_t startaddr, 5423 size_t len, 5424 uint_t flags, 5425 hat_callback_t *callback) 5426 { 5427 struct hmehash_bucket *hmebp; 5428 struct hme_blk *hmeblkp; 5429 struct hme_blk *pr_hblk = NULL; 5430 struct hme_blk *nx_hblk; 5431 struct hme_blk *list = NULL; 5432 int i; 5433 uint64_t hblkpa, prevpa, nx_pa; 5434 demap_range_t dmr, *dmrp; 5435 cpuset_t cpuset; 5436 caddr_t endaddr = startaddr + len; 5437 caddr_t sa; 5438 caddr_t ea; 5439 caddr_t cb_sa[MAX_CB_ADDR]; 5440 caddr_t cb_ea[MAX_CB_ADDR]; 5441 int addr_cnt = 0; 5442 int a = 0; 5443 5444 if (sfmmup->sfmmu_free) { 5445 dmrp = NULL; 5446 } else { 5447 dmrp = &dmr; 5448 DEMAP_RANGE_INIT(sfmmup, dmrp); 5449 } 5450 5451 /* 5452 * Loop through all the hash buckets of HME blocks looking for matches. 5453 */ 5454 for (i = 0; i <= UHMEHASH_SZ; i++) { 5455 hmebp = &uhme_hash[i]; 5456 SFMMU_HASH_LOCK(hmebp); 5457 hmeblkp = hmebp->hmeblkp; 5458 hblkpa = hmebp->hmeh_nextpa; 5459 prevpa = 0; 5460 pr_hblk = NULL; 5461 while (hmeblkp) { 5462 nx_hblk = hmeblkp->hblk_next; 5463 nx_pa = hmeblkp->hblk_nextpa; 5464 5465 /* 5466 * skip if not this context, if a shadow block or 5467 * if the mapping is not in the requested range 5468 */ 5469 if (hmeblkp->hblk_tag.htag_id != sfmmup || 5470 hmeblkp->hblk_shw_bit || 5471 (sa = (caddr_t)get_hblk_base(hmeblkp)) >= endaddr || 5472 (ea = get_hblk_endaddr(hmeblkp)) <= startaddr) { 5473 pr_hblk = hmeblkp; 5474 prevpa = hblkpa; 5475 goto next_block; 5476 } 5477 5478 ASSERT(!hmeblkp->hblk_shared); 5479 /* 5480 * unload if there are any current valid mappings 5481 */ 5482 if (hmeblkp->hblk_vcnt != 0 || 5483 hmeblkp->hblk_hmecnt != 0) 5484 (void) sfmmu_hblk_unload(sfmmup, hmeblkp, 5485 sa, ea, dmrp, flags); 5486 5487 /* 5488 * on unmap we also release the HME block itself, once 5489 * all mappings are gone. 5490 */ 5491 if ((flags & HAT_UNLOAD_UNMAP) != 0 && 5492 !hmeblkp->hblk_vcnt && 5493 !hmeblkp->hblk_hmecnt) { 5494 ASSERT(!hmeblkp->hblk_lckcnt); 5495 sfmmu_hblk_hash_rm(hmebp, hmeblkp, 5496 prevpa, pr_hblk); 5497 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 5498 } else { 5499 pr_hblk = hmeblkp; 5500 prevpa = hblkpa; 5501 } 5502 5503 if (callback == NULL) 5504 goto next_block; 5505 5506 /* 5507 * HME blocks may span more than one page, but we may be 5508 * unmapping only one page, so check for a smaller range 5509 * for the callback 5510 */ 5511 if (sa < startaddr) 5512 sa = startaddr; 5513 if (--ea > endaddr) 5514 ea = endaddr - 1; 5515 5516 cb_sa[addr_cnt] = sa; 5517 cb_ea[addr_cnt] = ea; 5518 if (++addr_cnt == MAX_CB_ADDR) { 5519 if (dmrp != NULL) { 5520 DEMAP_RANGE_FLUSH(dmrp); 5521 cpuset = sfmmup->sfmmu_cpusran; 5522 xt_sync(cpuset); 5523 } 5524 5525 for (a = 0; a < MAX_CB_ADDR; ++a) { 5526 callback->hcb_start_addr = cb_sa[a]; 5527 callback->hcb_end_addr = cb_ea[a]; 5528 callback->hcb_function(callback); 5529 } 5530 addr_cnt = 0; 5531 } 5532 5533 next_block: 5534 hmeblkp = nx_hblk; 5535 hblkpa = nx_pa; 5536 } 5537 SFMMU_HASH_UNLOCK(hmebp); 5538 } 5539 5540 sfmmu_hblks_list_purge(&list); 5541 if (dmrp != NULL) { 5542 DEMAP_RANGE_FLUSH(dmrp); 5543 cpuset = sfmmup->sfmmu_cpusran; 5544 xt_sync(cpuset); 5545 } 5546 5547 for (a = 0; a < addr_cnt; ++a) { 5548 callback->hcb_start_addr = cb_sa[a]; 5549 callback->hcb_end_addr = cb_ea[a]; 5550 callback->hcb_function(callback); 5551 } 5552 5553 /* 5554 * Check TSB and TLB page sizes if the process isn't exiting. 5555 */ 5556 if (!sfmmup->sfmmu_free) 5557 sfmmu_check_page_sizes(sfmmup, 0); 5558 } 5559 5560 /* 5561 * Unload all the mappings in the range [addr..addr+len). addr and len must 5562 * be MMU_PAGESIZE aligned. 5563 */ 5564 5565 extern struct seg *segkmap; 5566 #define ISSEGKMAP(sfmmup, addr) (sfmmup == ksfmmup && \ 5567 segkmap->s_base <= (addr) && (addr) < (segkmap->s_base + segkmap->s_size)) 5568 5569 5570 void 5571 hat_unload_callback( 5572 struct hat *sfmmup, 5573 caddr_t addr, 5574 size_t len, 5575 uint_t flags, 5576 hat_callback_t *callback) 5577 { 5578 struct hmehash_bucket *hmebp; 5579 hmeblk_tag hblktag; 5580 int hmeshift, hashno, iskernel; 5581 struct hme_blk *hmeblkp, *pr_hblk, *list = NULL; 5582 caddr_t endaddr; 5583 cpuset_t cpuset; 5584 uint64_t hblkpa, prevpa; 5585 int addr_count = 0; 5586 int a; 5587 caddr_t cb_start_addr[MAX_CB_ADDR]; 5588 caddr_t cb_end_addr[MAX_CB_ADDR]; 5589 int issegkmap = ISSEGKMAP(sfmmup, addr); 5590 demap_range_t dmr, *dmrp; 5591 5592 if (sfmmup->sfmmu_xhat_provider) { 5593 XHAT_UNLOAD_CALLBACK(sfmmup, addr, len, flags, callback); 5594 return; 5595 } else { 5596 /* 5597 * This must be a CPU HAT. If the address space has 5598 * XHATs attached, unload the mappings for all of them, 5599 * just in case 5600 */ 5601 ASSERT(sfmmup->sfmmu_as != NULL); 5602 if (sfmmup->sfmmu_as->a_xhat != NULL) 5603 xhat_unload_callback_all(sfmmup->sfmmu_as, addr, 5604 len, flags, callback); 5605 } 5606 5607 ASSERT((sfmmup == ksfmmup) || (flags & HAT_UNLOAD_OTHER) || \ 5608 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 5609 5610 ASSERT(sfmmup != NULL); 5611 ASSERT((len & MMU_PAGEOFFSET) == 0); 5612 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET)); 5613 5614 /* 5615 * Probing through a large VA range (say 63 bits) will be slow, even 5616 * at 4 Meg steps between the probes. So, when the virtual address range 5617 * is very large, search the HME entries for what to unload. 5618 * 5619 * len >> TTE_PAGE_SHIFT(TTE4M) is the # of 4Meg probes we'd need 5620 * 5621 * UHMEHASH_SZ is number of hash buckets to examine 5622 * 5623 */ 5624 if (sfmmup != KHATID && (len >> TTE_PAGE_SHIFT(TTE4M)) > UHMEHASH_SZ) { 5625 hat_unload_large_virtual(sfmmup, addr, len, flags, callback); 5626 return; 5627 } 5628 5629 CPUSET_ZERO(cpuset); 5630 5631 /* 5632 * If the process is exiting, we can save a lot of fuss since 5633 * we'll flush the TLB when we free the ctx anyway. 5634 */ 5635 if (sfmmup->sfmmu_free) 5636 dmrp = NULL; 5637 else 5638 dmrp = &dmr; 5639 5640 DEMAP_RANGE_INIT(sfmmup, dmrp); 5641 endaddr = addr + len; 5642 hblktag.htag_id = sfmmup; 5643 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 5644 5645 /* 5646 * It is likely for the vm to call unload over a wide range of 5647 * addresses that are actually very sparsely populated by 5648 * translations. In order to speed this up the sfmmu hat supports 5649 * the concept of shadow hmeblks. Dummy large page hmeblks that 5650 * correspond to actual small translations are allocated at tteload 5651 * time and are referred to as shadow hmeblks. Now, during unload 5652 * time, we first check if we have a shadow hmeblk for that 5653 * translation. The absence of one means the corresponding address 5654 * range is empty and can be skipped. 5655 * 5656 * The kernel is an exception to above statement and that is why 5657 * we don't use shadow hmeblks and hash starting from the smallest 5658 * page size. 5659 */ 5660 if (sfmmup == KHATID) { 5661 iskernel = 1; 5662 hashno = TTE64K; 5663 } else { 5664 iskernel = 0; 5665 if (mmu_page_sizes == max_mmu_page_sizes) { 5666 hashno = TTE256M; 5667 } else { 5668 hashno = TTE4M; 5669 } 5670 } 5671 while (addr < endaddr) { 5672 hmeshift = HME_HASH_SHIFT(hashno); 5673 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 5674 hblktag.htag_rehash = hashno; 5675 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 5676 5677 SFMMU_HASH_LOCK(hmebp); 5678 5679 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, hblkpa, pr_hblk, 5680 prevpa, &list); 5681 if (hmeblkp == NULL) { 5682 /* 5683 * didn't find an hmeblk. skip the appropiate 5684 * address range. 5685 */ 5686 SFMMU_HASH_UNLOCK(hmebp); 5687 if (iskernel) { 5688 if (hashno < mmu_hashcnt) { 5689 hashno++; 5690 continue; 5691 } else { 5692 hashno = TTE64K; 5693 addr = (caddr_t)roundup((uintptr_t)addr 5694 + 1, MMU_PAGESIZE64K); 5695 continue; 5696 } 5697 } 5698 addr = (caddr_t)roundup((uintptr_t)addr + 1, 5699 (1 << hmeshift)); 5700 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) { 5701 ASSERT(hashno == TTE64K); 5702 continue; 5703 } 5704 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) { 5705 hashno = TTE512K; 5706 continue; 5707 } 5708 if (mmu_page_sizes == max_mmu_page_sizes) { 5709 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) { 5710 hashno = TTE4M; 5711 continue; 5712 } 5713 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) { 5714 hashno = TTE32M; 5715 continue; 5716 } 5717 hashno = TTE256M; 5718 continue; 5719 } else { 5720 hashno = TTE4M; 5721 continue; 5722 } 5723 } 5724 ASSERT(hmeblkp); 5725 ASSERT(!hmeblkp->hblk_shared); 5726 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) { 5727 /* 5728 * If the valid count is zero we can skip the range 5729 * mapped by this hmeblk. 5730 * We free hblks in the case of HAT_UNMAP. HAT_UNMAP 5731 * is used by segment drivers as a hint 5732 * that the mapping resource won't be used any longer. 5733 * The best example of this is during exit(). 5734 */ 5735 addr = (caddr_t)roundup((uintptr_t)addr + 1, 5736 get_hblk_span(hmeblkp)); 5737 if ((flags & HAT_UNLOAD_UNMAP) || 5738 (iskernel && !issegkmap)) { 5739 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, 5740 pr_hblk); 5741 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 5742 } 5743 SFMMU_HASH_UNLOCK(hmebp); 5744 5745 if (iskernel) { 5746 hashno = TTE64K; 5747 continue; 5748 } 5749 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) { 5750 ASSERT(hashno == TTE64K); 5751 continue; 5752 } 5753 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) { 5754 hashno = TTE512K; 5755 continue; 5756 } 5757 if (mmu_page_sizes == max_mmu_page_sizes) { 5758 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) { 5759 hashno = TTE4M; 5760 continue; 5761 } 5762 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) { 5763 hashno = TTE32M; 5764 continue; 5765 } 5766 hashno = TTE256M; 5767 continue; 5768 } else { 5769 hashno = TTE4M; 5770 continue; 5771 } 5772 } 5773 if (hmeblkp->hblk_shw_bit) { 5774 /* 5775 * If we encounter a shadow hmeblk we know there is 5776 * smaller sized hmeblks mapping the same address space. 5777 * Decrement the hash size and rehash. 5778 */ 5779 ASSERT(sfmmup != KHATID); 5780 hashno--; 5781 SFMMU_HASH_UNLOCK(hmebp); 5782 continue; 5783 } 5784 5785 /* 5786 * track callback address ranges. 5787 * only start a new range when it's not contiguous 5788 */ 5789 if (callback != NULL) { 5790 if (addr_count > 0 && 5791 addr == cb_end_addr[addr_count - 1]) 5792 --addr_count; 5793 else 5794 cb_start_addr[addr_count] = addr; 5795 } 5796 5797 addr = sfmmu_hblk_unload(sfmmup, hmeblkp, addr, endaddr, 5798 dmrp, flags); 5799 5800 if (callback != NULL) 5801 cb_end_addr[addr_count++] = addr; 5802 5803 if (((flags & HAT_UNLOAD_UNMAP) || (iskernel && !issegkmap)) && 5804 !hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) { 5805 sfmmu_hblk_hash_rm(hmebp, hmeblkp, prevpa, 5806 pr_hblk); 5807 sfmmu_hblk_free(hmebp, hmeblkp, hblkpa, &list); 5808 } 5809 SFMMU_HASH_UNLOCK(hmebp); 5810 5811 /* 5812 * Notify our caller as to exactly which pages 5813 * have been unloaded. We do these in clumps, 5814 * to minimize the number of xt_sync()s that need to occur. 5815 */ 5816 if (callback != NULL && addr_count == MAX_CB_ADDR) { 5817 DEMAP_RANGE_FLUSH(dmrp); 5818 if (dmrp != NULL) { 5819 cpuset = sfmmup->sfmmu_cpusran; 5820 xt_sync(cpuset); 5821 } 5822 5823 for (a = 0; a < MAX_CB_ADDR; ++a) { 5824 callback->hcb_start_addr = cb_start_addr[a]; 5825 callback->hcb_end_addr = cb_end_addr[a]; 5826 callback->hcb_function(callback); 5827 } 5828 addr_count = 0; 5829 } 5830 if (iskernel) { 5831 hashno = TTE64K; 5832 continue; 5833 } 5834 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) { 5835 ASSERT(hashno == TTE64K); 5836 continue; 5837 } 5838 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) { 5839 hashno = TTE512K; 5840 continue; 5841 } 5842 if (mmu_page_sizes == max_mmu_page_sizes) { 5843 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) { 5844 hashno = TTE4M; 5845 continue; 5846 } 5847 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) { 5848 hashno = TTE32M; 5849 continue; 5850 } 5851 hashno = TTE256M; 5852 } else { 5853 hashno = TTE4M; 5854 } 5855 } 5856 5857 sfmmu_hblks_list_purge(&list); 5858 DEMAP_RANGE_FLUSH(dmrp); 5859 if (dmrp != NULL) { 5860 cpuset = sfmmup->sfmmu_cpusran; 5861 xt_sync(cpuset); 5862 } 5863 if (callback && addr_count != 0) { 5864 for (a = 0; a < addr_count; ++a) { 5865 callback->hcb_start_addr = cb_start_addr[a]; 5866 callback->hcb_end_addr = cb_end_addr[a]; 5867 callback->hcb_function(callback); 5868 } 5869 } 5870 5871 /* 5872 * Check TSB and TLB page sizes if the process isn't exiting. 5873 */ 5874 if (!sfmmup->sfmmu_free) 5875 sfmmu_check_page_sizes(sfmmup, 0); 5876 } 5877 5878 /* 5879 * Unload all the mappings in the range [addr..addr+len). addr and len must 5880 * be MMU_PAGESIZE aligned. 5881 */ 5882 void 5883 hat_unload(struct hat *sfmmup, caddr_t addr, size_t len, uint_t flags) 5884 { 5885 if (sfmmup->sfmmu_xhat_provider) { 5886 XHAT_UNLOAD(sfmmup, addr, len, flags); 5887 return; 5888 } 5889 hat_unload_callback(sfmmup, addr, len, flags, NULL); 5890 } 5891 5892 5893 /* 5894 * Find the largest mapping size for this page. 5895 */ 5896 int 5897 fnd_mapping_sz(page_t *pp) 5898 { 5899 int sz; 5900 int p_index; 5901 5902 p_index = PP_MAPINDEX(pp); 5903 5904 sz = 0; 5905 p_index >>= 1; /* don't care about 8K bit */ 5906 for (; p_index; p_index >>= 1) { 5907 sz++; 5908 } 5909 5910 return (sz); 5911 } 5912 5913 /* 5914 * This function unloads a range of addresses for an hmeblk. 5915 * It returns the next address to be unloaded. 5916 * It should be called with the hash lock held. 5917 */ 5918 static caddr_t 5919 sfmmu_hblk_unload(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 5920 caddr_t endaddr, demap_range_t *dmrp, uint_t flags) 5921 { 5922 tte_t tte, ttemod; 5923 struct sf_hment *sfhmep; 5924 int ttesz; 5925 long ttecnt; 5926 page_t *pp; 5927 kmutex_t *pml; 5928 int ret; 5929 int use_demap_range; 5930 5931 ASSERT(in_hblk_range(hmeblkp, addr)); 5932 ASSERT(!hmeblkp->hblk_shw_bit); 5933 ASSERT(sfmmup != NULL || hmeblkp->hblk_shared); 5934 ASSERT(sfmmup == NULL || !hmeblkp->hblk_shared); 5935 ASSERT(dmrp == NULL || !hmeblkp->hblk_shared); 5936 5937 #ifdef DEBUG 5938 if (get_hblk_ttesz(hmeblkp) != TTE8K && 5939 (endaddr < get_hblk_endaddr(hmeblkp))) { 5940 panic("sfmmu_hblk_unload: partial unload of large page"); 5941 } 5942 #endif /* DEBUG */ 5943 5944 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 5945 ttesz = get_hblk_ttesz(hmeblkp); 5946 5947 use_demap_range = ((dmrp == NULL) || 5948 (TTEBYTES(ttesz) == DEMAP_RANGE_PGSZ(dmrp))); 5949 5950 if (use_demap_range) { 5951 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr); 5952 } else { 5953 DEMAP_RANGE_FLUSH(dmrp); 5954 } 5955 ttecnt = 0; 5956 HBLKTOHME(sfhmep, hmeblkp, addr); 5957 5958 while (addr < endaddr) { 5959 pml = NULL; 5960 sfmmu_copytte(&sfhmep->hme_tte, &tte); 5961 if (TTE_IS_VALID(&tte)) { 5962 pp = sfhmep->hme_page; 5963 if (pp != NULL) { 5964 pml = sfmmu_mlist_enter(pp); 5965 } 5966 5967 /* 5968 * Verify if hme still points to 'pp' now that 5969 * we have p_mapping lock. 5970 */ 5971 if (sfhmep->hme_page != pp) { 5972 if (pp != NULL && sfhmep->hme_page != NULL) { 5973 ASSERT(pml != NULL); 5974 sfmmu_mlist_exit(pml); 5975 /* Re-start this iteration. */ 5976 continue; 5977 } 5978 ASSERT((pp != NULL) && 5979 (sfhmep->hme_page == NULL)); 5980 goto tte_unloaded; 5981 } 5982 5983 /* 5984 * This point on we have both HASH and p_mapping 5985 * lock. 5986 */ 5987 ASSERT(pp == sfhmep->hme_page); 5988 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 5989 5990 /* 5991 * We need to loop on modify tte because it is 5992 * possible for pagesync to come along and 5993 * change the software bits beneath us. 5994 * 5995 * Page_unload can also invalidate the tte after 5996 * we read tte outside of p_mapping lock. 5997 */ 5998 again: 5999 ttemod = tte; 6000 6001 TTE_SET_INVALID(&ttemod); 6002 ret = sfmmu_modifytte_try(&tte, &ttemod, 6003 &sfhmep->hme_tte); 6004 6005 if (ret <= 0) { 6006 if (TTE_IS_VALID(&tte)) { 6007 ASSERT(ret < 0); 6008 goto again; 6009 } 6010 if (pp != NULL) { 6011 panic("sfmmu_hblk_unload: pp = 0x%p " 6012 "tte became invalid under mlist" 6013 " lock = 0x%p", pp, pml); 6014 } 6015 continue; 6016 } 6017 6018 if (!(flags & HAT_UNLOAD_NOSYNC)) { 6019 sfmmu_ttesync(sfmmup, addr, &tte, pp); 6020 } 6021 6022 /* 6023 * Ok- we invalidated the tte. Do the rest of the job. 6024 */ 6025 ttecnt++; 6026 6027 if (flags & HAT_UNLOAD_UNLOCK) { 6028 ASSERT(hmeblkp->hblk_lckcnt > 0); 6029 atomic_add_32(&hmeblkp->hblk_lckcnt, -1); 6030 HBLK_STACK_TRACE(hmeblkp, HBLK_UNLOCK); 6031 } 6032 6033 /* 6034 * Normally we would need to flush the page 6035 * from the virtual cache at this point in 6036 * order to prevent a potential cache alias 6037 * inconsistency. 6038 * The particular scenario we need to worry 6039 * about is: 6040 * Given: va1 and va2 are two virtual address 6041 * that alias and map the same physical 6042 * address. 6043 * 1. mapping exists from va1 to pa and data 6044 * has been read into the cache. 6045 * 2. unload va1. 6046 * 3. load va2 and modify data using va2. 6047 * 4 unload va2. 6048 * 5. load va1 and reference data. Unless we 6049 * flush the data cache when we unload we will 6050 * get stale data. 6051 * Fortunately, page coloring eliminates the 6052 * above scenario by remembering the color a 6053 * physical page was last or is currently 6054 * mapped to. Now, we delay the flush until 6055 * the loading of translations. Only when the 6056 * new translation is of a different color 6057 * are we forced to flush. 6058 */ 6059 if (use_demap_range) { 6060 /* 6061 * Mark this page as needing a demap. 6062 */ 6063 DEMAP_RANGE_MARKPG(dmrp, addr); 6064 } else { 6065 ASSERT(sfmmup != NULL); 6066 ASSERT(!hmeblkp->hblk_shared); 6067 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 6068 sfmmup->sfmmu_free, 0); 6069 } 6070 6071 if (pp) { 6072 /* 6073 * Remove the hment from the mapping list 6074 */ 6075 ASSERT(hmeblkp->hblk_hmecnt > 0); 6076 6077 /* 6078 * Again, we cannot 6079 * ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS); 6080 */ 6081 HME_SUB(sfhmep, pp); 6082 membar_stst(); 6083 atomic_add_16(&hmeblkp->hblk_hmecnt, -1); 6084 } 6085 6086 ASSERT(hmeblkp->hblk_vcnt > 0); 6087 atomic_add_16(&hmeblkp->hblk_vcnt, -1); 6088 6089 ASSERT(hmeblkp->hblk_hmecnt || hmeblkp->hblk_vcnt || 6090 !hmeblkp->hblk_lckcnt); 6091 6092 #ifdef VAC 6093 if (pp && (pp->p_nrm & (P_KPMC | P_KPMS | P_TNC))) { 6094 if (PP_ISTNC(pp)) { 6095 /* 6096 * If page was temporary 6097 * uncached, try to recache 6098 * it. Note that HME_SUB() was 6099 * called above so p_index and 6100 * mlist had been updated. 6101 */ 6102 conv_tnc(pp, ttesz); 6103 } else if (pp->p_mapping == NULL) { 6104 ASSERT(kpm_enable); 6105 /* 6106 * Page is marked to be in VAC conflict 6107 * to an existing kpm mapping and/or is 6108 * kpm mapped using only the regular 6109 * pagesize. 6110 */ 6111 sfmmu_kpm_hme_unload(pp); 6112 } 6113 } 6114 #endif /* VAC */ 6115 } else if ((pp = sfhmep->hme_page) != NULL) { 6116 /* 6117 * TTE is invalid but the hme 6118 * still exists. let pageunload 6119 * complete its job. 6120 */ 6121 ASSERT(pml == NULL); 6122 pml = sfmmu_mlist_enter(pp); 6123 if (sfhmep->hme_page != NULL) { 6124 sfmmu_mlist_exit(pml); 6125 continue; 6126 } 6127 ASSERT(sfhmep->hme_page == NULL); 6128 } else if (hmeblkp->hblk_hmecnt != 0) { 6129 /* 6130 * pageunload may have not finished decrementing 6131 * hblk_vcnt and hblk_hmecnt. Find page_t if any and 6132 * wait for pageunload to finish. Rely on pageunload 6133 * to decrement hblk_hmecnt after hblk_vcnt. 6134 */ 6135 pfn_t pfn = TTE_TO_TTEPFN(&tte); 6136 ASSERT(pml == NULL); 6137 if (pf_is_memory(pfn)) { 6138 pp = page_numtopp_nolock(pfn); 6139 if (pp != NULL) { 6140 pml = sfmmu_mlist_enter(pp); 6141 sfmmu_mlist_exit(pml); 6142 pml = NULL; 6143 } 6144 } 6145 } 6146 6147 tte_unloaded: 6148 /* 6149 * At this point, the tte we are looking at 6150 * should be unloaded, and hme has been unlinked 6151 * from page too. This is important because in 6152 * pageunload, it does ttesync() then HME_SUB. 6153 * We need to make sure HME_SUB has been completed 6154 * so we know ttesync() has been completed. Otherwise, 6155 * at exit time, after return from hat layer, VM will 6156 * release as structure which hat_setstat() (called 6157 * by ttesync()) needs. 6158 */ 6159 #ifdef DEBUG 6160 { 6161 tte_t dtte; 6162 6163 ASSERT(sfhmep->hme_page == NULL); 6164 6165 sfmmu_copytte(&sfhmep->hme_tte, &dtte); 6166 ASSERT(!TTE_IS_VALID(&dtte)); 6167 } 6168 #endif 6169 6170 if (pml) { 6171 sfmmu_mlist_exit(pml); 6172 } 6173 6174 addr += TTEBYTES(ttesz); 6175 sfhmep++; 6176 DEMAP_RANGE_NEXTPG(dmrp); 6177 } 6178 /* 6179 * For shared hmeblks this routine is only called when region is freed 6180 * and no longer referenced. So no need to decrement ttecnt 6181 * in the region structure here. 6182 */ 6183 if (ttecnt > 0 && sfmmup != NULL) { 6184 atomic_add_long(&sfmmup->sfmmu_ttecnt[ttesz], -ttecnt); 6185 } 6186 return (addr); 6187 } 6188 6189 /* 6190 * Synchronize all the mappings in the range [addr..addr+len). 6191 * Can be called with clearflag having two states: 6192 * HAT_SYNC_DONTZERO means just return the rm stats 6193 * HAT_SYNC_ZERORM means zero rm bits in the tte and return the stats 6194 */ 6195 void 6196 hat_sync(struct hat *sfmmup, caddr_t addr, size_t len, uint_t clearflag) 6197 { 6198 struct hmehash_bucket *hmebp; 6199 hmeblk_tag hblktag; 6200 int hmeshift, hashno = 1; 6201 struct hme_blk *hmeblkp, *list = NULL; 6202 caddr_t endaddr; 6203 cpuset_t cpuset; 6204 6205 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 6206 ASSERT((sfmmup == ksfmmup) || 6207 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 6208 ASSERT((len & MMU_PAGEOFFSET) == 0); 6209 ASSERT((clearflag == HAT_SYNC_DONTZERO) || 6210 (clearflag == HAT_SYNC_ZERORM)); 6211 6212 CPUSET_ZERO(cpuset); 6213 6214 endaddr = addr + len; 6215 hblktag.htag_id = sfmmup; 6216 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 6217 6218 /* 6219 * Spitfire supports 4 page sizes. 6220 * Most pages are expected to be of the smallest page 6221 * size (8K) and these will not need to be rehashed. 64K 6222 * pages also don't need to be rehashed because the an hmeblk 6223 * spans 64K of address space. 512K pages might need 1 rehash and 6224 * and 4M pages 2 rehashes. 6225 */ 6226 while (addr < endaddr) { 6227 hmeshift = HME_HASH_SHIFT(hashno); 6228 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift); 6229 hblktag.htag_rehash = hashno; 6230 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift); 6231 6232 SFMMU_HASH_LOCK(hmebp); 6233 6234 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list); 6235 if (hmeblkp != NULL) { 6236 ASSERT(!hmeblkp->hblk_shared); 6237 /* 6238 * We've encountered a shadow hmeblk so skip the range 6239 * of the next smaller mapping size. 6240 */ 6241 if (hmeblkp->hblk_shw_bit) { 6242 ASSERT(sfmmup != ksfmmup); 6243 ASSERT(hashno > 1); 6244 addr = (caddr_t)P2END((uintptr_t)addr, 6245 TTEBYTES(hashno - 1)); 6246 } else { 6247 addr = sfmmu_hblk_sync(sfmmup, hmeblkp, 6248 addr, endaddr, clearflag); 6249 } 6250 SFMMU_HASH_UNLOCK(hmebp); 6251 hashno = 1; 6252 continue; 6253 } 6254 SFMMU_HASH_UNLOCK(hmebp); 6255 6256 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) { 6257 /* 6258 * We have traversed the whole list and rehashed 6259 * if necessary without finding the address to sync. 6260 * This is ok so we increment the address by the 6261 * smallest hmeblk range for kernel mappings and the 6262 * largest hmeblk range, to account for shadow hmeblks, 6263 * for user mappings and continue. 6264 */ 6265 if (sfmmup == ksfmmup) 6266 addr = (caddr_t)P2END((uintptr_t)addr, 6267 TTEBYTES(1)); 6268 else 6269 addr = (caddr_t)P2END((uintptr_t)addr, 6270 TTEBYTES(hashno)); 6271 hashno = 1; 6272 } else { 6273 hashno++; 6274 } 6275 } 6276 sfmmu_hblks_list_purge(&list); 6277 cpuset = sfmmup->sfmmu_cpusran; 6278 xt_sync(cpuset); 6279 } 6280 6281 static caddr_t 6282 sfmmu_hblk_sync(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr, 6283 caddr_t endaddr, int clearflag) 6284 { 6285 tte_t tte, ttemod; 6286 struct sf_hment *sfhmep; 6287 int ttesz; 6288 struct page *pp; 6289 kmutex_t *pml; 6290 int ret; 6291 6292 ASSERT(hmeblkp->hblk_shw_bit == 0); 6293 ASSERT(!hmeblkp->hblk_shared); 6294 6295 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp)); 6296 6297 ttesz = get_hblk_ttesz(hmeblkp); 6298 HBLKTOHME(sfhmep, hmeblkp, addr); 6299 6300 while (addr < endaddr) { 6301 sfmmu_copytte(&sfhmep->hme_tte, &tte); 6302 if (TTE_IS_VALID(&tte)) { 6303 pml = NULL; 6304 pp = sfhmep->hme_page; 6305 if (pp) { 6306 pml = sfmmu_mlist_enter(pp); 6307 } 6308 if (pp != sfhmep->hme_page) { 6309 /* 6310 * tte most have been unloaded 6311 * underneath us. Recheck 6312 */ 6313 ASSERT(pml); 6314 sfmmu_mlist_exit(pml); 6315 continue; 6316 } 6317 6318 ASSERT(pp == NULL || sfmmu_mlist_held(pp)); 6319 6320 if (clearflag == HAT_SYNC_ZERORM) { 6321 ttemod = tte; 6322 TTE_CLR_RM(&ttemod); 6323 ret = sfmmu_modifytte_try(&tte, &ttemod, 6324 &sfhmep->hme_tte); 6325 if (ret < 0) { 6326 if (pml) { 6327 sfmmu_mlist_exit(pml); 6328 } 6329 continue; 6330 } 6331 6332 if (ret > 0) { 6333 sfmmu_tlb_demap(addr, sfmmup, 6334 hmeblkp, 0, 0); 6335 } 6336 } 6337 sfmmu_ttesync(sfmmup, addr, &tte, pp); 6338 if (pml) { 6339 sfmmu_mlist_exit(pml); 6340 } 6341 } 6342 addr += TTEBYTES(ttesz); 6343 sfhmep++; 6344 } 6345 return (addr); 6346 } 6347 6348 /* 6349 * This function will sync a tte to the page struct and it will 6350 * update the hat stats. Currently it allows us to pass a NULL pp 6351 * and we will simply update the stats. We may want to change this 6352 * so we only keep stats for pages backed by pp's. 6353 */ 6354 static void 6355 sfmmu_ttesync(struct hat *sfmmup, caddr_t addr, tte_t *ttep, page_t *pp) 6356 { 6357 uint_t rm = 0; 6358 int sz; 6359 pgcnt_t npgs; 6360 6361 ASSERT(TTE_IS_VALID(ttep)); 6362 6363 if (TTE_IS_NOSYNC(ttep)) { 6364 return; 6365 } 6366 6367 if (TTE_IS_REF(ttep)) { 6368 rm = P_REF; 6369 } 6370 if (TTE_IS_MOD(ttep)) { 6371 rm |= P_MOD; 6372 } 6373 6374 if (rm == 0) { 6375 return; 6376 } 6377 6378 sz = TTE_CSZ(ttep); 6379 if (sfmmup != NULL && sfmmup->sfmmu_rmstat) { 6380 int i; 6381 caddr_t vaddr = addr; 6382 6383 for (i = 0; i < TTEPAGES(sz); i++, vaddr += MMU_PAGESIZE) { 6384 hat_setstat(sfmmup->sfmmu_as, vaddr, MMU_PAGESIZE, rm); 6385 } 6386 6387 } 6388 6389 /* 6390 * XXX I want to use cas to update nrm bits but they 6391 * currently belong in common/vm and not in hat where 6392 * they should be. 6393 * The nrm bits are protected by the same mutex as 6394 * the one that protects the page's mapping list. 6395 */ 6396 if (!pp) 6397 return; 6398 ASSERT(sfmmu_mlist_held(pp)); 6399 /* 6400 * If the tte is for a large page, we need to sync all the 6401 * pages covered by the tte. 6402 */ 6403 if (sz != TTE8K) { 6404 ASSERT(pp->p_szc != 0); 6405 pp = PP_GROUPLEADER(pp, sz); 6406 ASSERT(sfmmu_mlist_held(pp)); 6407 } 6408 6409 /* Get number of pages from tte size. */ 6410 npgs = TTEPAGES(sz); 6411 6412 do { 6413 ASSERT(pp); 6414 ASSERT(sfmmu_mlist_held(pp)); 6415 if (((rm & P_REF) != 0 && !PP_ISREF(pp)) || 6416 ((rm & P_MOD) != 0 && !PP_ISMOD(pp))) 6417 hat_page_setattr(pp, rm); 6418 6419 /* 6420 * Are we done? If not, we must have a large mapping. 6421 * For large mappings we need to sync the rest of the pages 6422 * covered by this tte; goto the next page. 6423 */ 6424 } while (--npgs > 0 && (pp = PP_PAGENEXT(pp))); 6425 } 6426 6427 /* 6428 * Execute pre-callback handler of each pa_hment linked to pp 6429 * 6430 * Inputs: 6431 * flag: either HAT_PRESUSPEND or HAT_SUSPEND. 6432 * capture_cpus: pointer to return value (below) 6433 * 6434 * Returns: 6435 * Propagates the subsystem callback return values back to the caller; 6436 * returns 0 on success. If capture_cpus is non-NULL, the value returned 6437 * is zero if all of the pa_hments are of a type that do not require 6438 * capturing CPUs prior to suspending the mapping, else it is 1. 6439 */ 6440 static int 6441 hat_pageprocess_precallbacks(struct page *pp, uint_t flag, int *capture_cpus) 6442 { 6443 struct sf_hment *sfhmep; 6444 struct pa_hment *pahmep; 6445 int (*f)(caddr_t, uint_t, uint_t, void *); 6446 int ret; 6447 id_t id; 6448 int locked = 0; 6449 kmutex_t *pml; 6450 6451 ASSERT(PAGE_EXCL(pp)); 6452 if (!sfmmu_mlist_held(pp)) { 6453 pml = sfmmu_mlist_enter(pp); 6454 locked = 1; 6455 } 6456 6457 if (capture_cpus) 6458 *capture_cpus = 0; 6459 6460 top: 6461 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) { 6462 /* 6463 * skip sf_hments corresponding to VA<->PA mappings; 6464 * for pa_hment's, hme_tte.ll is zero 6465 */ 6466 if (!IS_PAHME(sfhmep)) 6467 continue; 6468 6469 pahmep = sfhmep->hme_data; 6470 ASSERT(pahmep != NULL); 6471 6472 /* 6473 * skip if pre-handler has been called earlier in this loop 6474 */ 6475 if (pahmep->flags & flag) 6476 continue; 6477 6478 id = pahmep->cb_id; 6479 ASSERT(id >= (id_t)0 && id < sfmmu_cb_nextid); 6480 if (capture_cpus && sfmmu_cb_table[id].capture_cpus != 0) 6481 *capture_cpus = 1; 6482 if ((f = sfmmu_cb_table[id].prehandler) == NULL) { 6483 pahmep->flags |= flag; 6484 continue; 6485 } 6486 6487 /* 6488 * Drop the mapping list lock to avoid locking order issues. 6489 */ 6490 if (locked) 6491 sfmmu_mlist_exit(pml); 6492 6493 ret = f(pahmep->addr, pahmep->len, flag, pahmep->pvt); 6494 if (ret != 0) 6495 return (ret); /* caller must do the cleanup */ 6496 6497 if (locked) { 6498 pml = sfmmu_mlist_enter(pp); 6499 pahmep->flags |= flag; 6500 goto top; 6501 } 6502 6503 pahmep->flags |= flag; 6504 } 6505 6506 if (locked) 6507 sfmmu_mlist_exit(pml); 6508 6509 return (0); 6510 } 6511 6512 /* 6513 * Execute post-callback handler of each pa_hment linked to pp 6514 * 6515 * Same overall assumptions and restrictions apply as for 6516 * hat_pageprocess_precallbacks(). 6517 */ 6518 static void 6519 hat_pageprocess_postcallbacks(struct page *pp, uint_t flag) 6520 { 6521 pfn_t pgpfn = pp->p_pagenum; 6522 pfn_t pgmask = btop(page_get_pagesize(pp->p_szc)) - 1; 6523 pfn_t newpfn; 6524 struct sf_hment *sfhmep; 6525 struct pa_hment *pahmep; 6526 int (*f)(caddr_t, uint_t, uint_t, void *, pfn_t); 6527 id_t id; 6528 int locked = 0; 6529 kmutex_t *pml; 6530 6531 ASSERT(PAGE_EXCL(pp)); 6532 if (!sfmmu_mlist_held(pp)) { 6533 pml = sfmmu_mlist_enter(pp); 6534 locked = 1; 6535 } 6536 6537 top: 6538 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) { 6539 /* 6540 * skip sf_hments corresponding to VA<->PA mappings; 6541 * for pa_hment's, hme_tte.ll is zero 6542 */ 6543 if (!IS_PAHME(sfhmep)) 6544 continue; 6545 6546 pahmep = sfhmep->hme_data; 6547 ASSERT(pahmep != NULL); 6548 6549 if ((pahmep->flags & flag) == 0) 6550 continue; 6551 6552 pahmep->flags &= ~flag; 6553 6554 id = pahmep->cb_id; 6555 ASSERT(id >= (id_t)0 && id < sfmmu_cb_nextid); 6556 if ((f = sfmmu_cb_table[id].posthandler) == NULL) 6557 continue; 6558 6559 /* 6560 * Convert the base page PFN into the constituent PFN 6561 * which is needed by the callback handler. 6562 */ 6563 newpfn = pgpfn | (btop((uintptr_t)pahmep->addr) & pgmask); 6564 6565 /* 6566 * Drop the mapping list lock to avoid locking order issues. 6567 */ 6568 if (locked) 6569 sfmmu_mlist_exit(pml); 6570 6571 if (f(pahmep->addr, pahmep->len, flag, pahmep->pvt, newpfn) 6572 != 0) 6573 panic("sfmmu: posthandler failed"); 6574 6575 if (locked) { 6576 pml = sfmmu_mlist_enter(pp); 6577 goto top; 6578 } 6579 } 6580 6581 if (locked) 6582 sfmmu_mlist_exit(pml); 6583 } 6584 6585 /* 6586 * Suspend locked kernel mapping 6587 */ 6588 void 6589 hat_pagesuspend(struct page *pp) 6590 { 6591 struct sf_hment *sfhmep; 6592 sfmmu_t *sfmmup; 6593 tte_t tte, ttemod; 6594 struct hme_blk *hmeblkp; 6595 caddr_t addr; 6596 int index, cons; 6597 cpuset_t cpuset; 6598 6599 ASSERT(PAGE_EXCL(pp)); 6600 ASSERT(sfmmu_mlist_held(pp)); 6601 6602 mutex_enter(&kpr_suspendlock); 6603 6604 /* 6605 * We're about to suspend a kernel mapping so mark this thread as 6606 * non-traceable by DTrace. This prevents us from running into issues 6607 * with probe context trying to touch a suspended page 6608 * in the relocation codepath itself. 6609 */ 6610 curthread->t_flag |= T_DONTDTRACE; 6611 6612 index = PP_MAPINDEX(pp); 6613 cons = TTE8K; 6614 6615 retry: 6616 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) { 6617 6618 if (IS_PAHME(sfhmep)) 6619 continue; 6620 6621 if (get_hblk_ttesz(sfmmu_hmetohblk(sfhmep)) != cons) 6622 continue; 6623 6624 /* 6625 * Loop until we successfully set the suspend bit in 6626 * the TTE. 6627 */ 6628 again: 6629 sfmmu_copytte(&sfhmep->hme_tte, &tte); 6630 ASSERT(TTE_IS_VALID(&tte)); 6631 6632 ttemod = tte; 6633 TTE_SET_SUSPEND(&ttemod); 6634 if (sfmmu_modifytte_try(&tte, &ttemod, 6635 &sfhmep->hme_tte) < 0) 6636 goto again; 6637 6638 /* 6639 * Invalidate TSB entry 6640 */ 6641 hmeblkp = sfmmu_hmetohblk(sfhmep); 6642 6643 sfmmup = hblktosfmmu(hmeblkp); 6644 ASSERT(sfmmup == ksfmmup); 6645 ASSERT(!hmeblkp->hblk_shared); 6646 6647 addr = tte_to_vaddr(hmeblkp, tte); 6648 6649 /* 6650 * No need to make sure that the TSB for this sfmmu is 6651 * not being relocated since it is ksfmmup and thus it 6652 * will never be relocated. 6653 */ 6654 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0); 6655 6656 /* 6657 * Update xcall stats 6658 */ 6659 cpuset = cpu_ready_set; 6660 CPUSET_DEL(cpuset, CPU->cpu_id); 6661 6662 /* LINTED: constant in conditional context */ 6663 SFMMU_XCALL_STATS(ksfmmup); 6664 6665 /* 6666 * Flush TLB entry on remote CPU's 6667 */ 6668 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr, 6669 (uint64_t)ksfmmup); 6670 xt_sync(cpuset); 6671 6672 /* 6673 * Flush TLB entry on local CPU 6674 */ 6675 vtag_flushpage(addr, (uint64_t)ksfmmup); 6676 } 6677 6678 while (index != 0) { 6679 index = index >> 1; 6680 if (index != 0) 6681 cons++; 6682 if (index & 0x1) { 6683 pp = PP_GROUPLEADER(pp, cons); 6684 goto retry; 6685 } 6686 } 6687 } 6688 6689 #ifdef DEBUG 6690 6691 #define N_PRLE 1024 6692 struct prle { 6693 page_t *targ; 6694 page_t *repl; 6695 int status; 6696 int pausecpus; 6697 hrtime_t whence; 6698 }; 6699 6700 static struct prle page_relocate_log[N_PRLE]; 6701 static int prl_entry; 6702 static kmutex_t prl_mutex; 6703 6704 #define PAGE_RELOCATE_LOG(t, r, s, p) \ 6705 mutex_enter(&prl_mutex); \ 6706 page_relocate_log[prl_entry].targ = *(t); \ 6707 page_relocate_log[prl_entry].repl = *(r); \ 6708 page_relocate_log[prl_entry].status = (s); \ 6709 page_relocate_log[prl_entry].pausecpus = (p); \ 6710 page_relocate_log[prl_entry].whence = gethrtime(); \ 6711 prl_entry = (prl_entry == (N_PRLE - 1))? 0 : prl_entry + 1; \ 6712 mutex_exit(&prl_mutex); 6713 6714 #else /* !DEBUG */ 6715 #define PAGE_RELOCATE_LOG(t, r, s, p) 6716 #endif 6717 6718 /* 6719 * Core Kernel Page Relocation Algorithm 6720 * 6721 * Input: 6722 * 6723 * target : constituent pages are SE_EXCL locked. 6724 * replacement: constituent pages are SE_EXCL locked. 6725 * 6726 * Output: 6727 * 6728 * nrelocp: number of pages relocated 6729 */ 6730 int 6731 hat_page_relocate(page_t **target, page_t **replacement, spgcnt_t *nrelocp) 6732 { 6733 page_t *targ, *repl; 6734 page_t *tpp, *rpp; 6735 kmutex_t *low, *high; 6736 spgcnt_t npages, i; 6737 page_t *pl = NULL; 6738 int old_pil; 6739 cpuset_t cpuset; 6740 int cap_cpus; 6741 int ret; 6742 6743 if (hat_kpr_enabled == 0 || !kcage_on || PP_ISNORELOC(*target)) { 6744 PAGE_RELOCATE_LOG(target, replacement, EAGAIN, -1); 6745 return (EAGAIN); 6746 } 6747 6748 mutex_enter(&kpr_mutex); 6749 kreloc_thread = curthread; 6750 6751 targ = *target; 6752 repl = *replacement; 6753 ASSERT(repl != NULL); 6754 ASSERT(targ->p_szc == repl->p_szc); 6755 6756 npages = page_get_pagecnt(targ->p_szc); 6757 6758 /* 6759 * unload VA<->PA mappings that are not locked 6760 */ 6761 tpp = targ; 6762 for (i = 0; i < npages; i++) { 6763 (void) hat_pageunload(tpp, SFMMU_KERNEL_RELOC); 6764 tpp++; 6765 } 6766 6767 /* 6768 * Do "presuspend" callbacks, in a context from which we can still 6769 * block as needed. Note that we don't hold the mapping list lock 6770 * of "targ" at this point due to potential locking order issues; 6771 * we assume that between the hat_pageunload() above and holding 6772 * the SE_EXCL lock that the mapping list *cannot* change at this 6773 * point. 6774 */ 6775 ret = hat_pageprocess_precallbacks(targ, HAT_PRESUSPEND, &cap_cpus); 6776 if (ret != 0) { 6777 /* 6778 * EIO translates to fatal error, for all others cleanup 6779 * and return EAGAIN. 6780 */ 6781 ASSERT(ret != EIO); 6782 hat_pageprocess_postcallbacks(targ, HAT_POSTUNSUSPEND); 6783 PAGE_RELOCATE_LOG(target, replacement, ret, -1); 6784 kreloc_thread = NULL; 6785 mutex_exit(&kpr_mutex); 6786 return (EAGAIN); 6787 } 6788 6789 /* 6790 * acquire p_mapping list lock for both the target and replacement 6791 * root pages. 6792 * 6793 * low and high refer to the need to grab the mlist locks in a 6794 * specific order in order to prevent race conditions. Thus the 6795 * lower lock must be grabbed before the higher lock. 6796 * 6797 * This will block hat_unload's accessing p_mapping list. Since 6798 * we have SE_EXCL lock, hat_memload and hat_pageunload will be 6799 * blocked. Thus, no one else will be accessing the p_mapping list 6800 * while we suspend and reload the locked mapping below. 6801 */ 6802 tpp = targ; 6803 rpp = repl; 6804 sfmmu_mlist_reloc_enter(tpp, rpp, &low, &high); 6805 6806 kpreempt_disable(); 6807 6808 #ifdef VAC 6809 /* 6810 * If the replacement page is of a different virtual color 6811 * than the page it is replacing, we need to handle the VAC 6812 * consistency for it just as we would if we were setting up 6813 * a new mapping to a page. 6814 */ 6815 if ((tpp->p_szc == 0) && (PP_GET_VCOLOR(rpp) != NO_VCOLOR)) { 6816 if (tpp->p_vcolor != rpp->p_vcolor) { 6817 sfmmu_cache_flushcolor(PP_GET_VCOLOR(rpp), 6818 rpp->p_pagenum); 6819 } 6820 } 6821 #endif 6822 6823 /* 6824 * We raise our PIL to 13 so that we don't get captured by 6825 * another CPU or pinned by an interrupt thread. We can't go to 6826 * PIL 14 since the nexus driver(s) may need to interrupt at 6827 * that level in the case of IOMMU pseudo mappings. 6828 */ 6829 cpuset = cpu_ready_set; 6830 CPUSET_DEL(cpuset, CPU->cpu_id); 6831 if (!cap_cpus || CPUSET_ISNULL(cpuset)) { 6832 old_pil = splr(XCALL_PIL); 6833 } else { 6834 old_pil = -1; 6835 xc_attention(cpuset); 6836 } 6837 ASSERT(getpil() == XCALL_PIL); 6838 6839 /* 6840 * Now do suspend callbacks. In the case of an IOMMU mapping 6841 * this will suspend all DMA activity to the page while it is 6842 * being relocated. Since we are well above LOCK_LEVEL and CPUs 6843 * may be captured at this point we should have acquired any needed 6844 * locks in the presuspend callback. 6845 */ 6846 ret = hat_pageprocess_precallbacks(targ, HAT_SUSPEND, NULL); 6847 if (ret != 0) { 6848 repl = targ; 6849 goto suspend_fail; 6850 } 6851 6852 /* 6853 * Raise the PIL yet again, this time to block all high-level 6854 * interrupts on this CPU. This is necessary to prevent an 6855 * interrupt routine from pinning the thread which holds the 6856 * mapping suspended and then touching the suspended page. 6857 * 6858 * Once the page is suspended we also need to be careful to 6859 * avoid calling any functions which touch any seg_kmem memory 6860 * since that memory may be backed by the very page we are 6861 * relocating in here! 6862 */ 6863 hat_pagesuspend(targ); 6864 6865 /* 6866 * Now that we are confident everybody has stopped using this page, 6867 * copy the page contents. Note we use a physical copy to prevent 6868 * locking issues and to avoid fpRAS because we can't handle it in 6869 * this context. 6870 */ 6871 for (i = 0; i < npages; i++, tpp++, rpp++) { 6872 /* 6873 * Copy the contents of the page. 6874 */ 6875 ppcopy_kernel(tpp, rpp); 6876 } 6877 6878 tpp = targ; 6879 rpp = repl; 6880 for (i = 0; i < npages; i++, tpp++, rpp++) { 6881 /* 6882 * Copy attributes. VAC consistency was handled above, 6883 * if required. 6884 */ 6885 rpp->p_nrm = tpp->p_nrm; 6886 tpp->p_nrm = 0; 6887 rpp->p_index = tpp->p_index; 6888 tpp->p_index = 0; 6889 #ifdef VAC 6890 rpp->p_vcolor = tpp->p_vcolor; 6891 #endif 6892 } 6893 6894 /* 6895 * First, unsuspend the page, if we set the suspend bit, and transfer 6896 * the mapping list from the target page to the replacement page. 6897 * Next process postcallbacks; since pa_hment's are linked only to the 6898 * p_mapping list of root page, we don't iterate over the constituent 6899 * pages. 6900 */ 6901 hat_pagereload(targ, repl); 6902 6903 suspend_fail: 6904 hat_pageprocess_postcallbacks(repl, HAT_UNSUSPEND); 6905 6906 /* 6907 * Now lower our PIL and release any captured CPUs since we 6908 * are out of the "danger zone". After this it will again be 6909 * safe to acquire adaptive mutex locks, or to drop them... 6910 */ 6911 if (old_pil != -1) { 6912 splx(old_pil); 6913 } else { 6914 xc_dismissed(cpuset); 6915 } 6916 6917 kpreempt_enable(); 6918 6919 sfmmu_mlist_reloc_exit(low, high); 6920 6921 /* 6922 * Postsuspend callbacks should drop any locks held across 6923 * the suspend callbacks. As before, we don't hold the mapping 6924 * list lock at this point.. our assumption is that the mapping 6925 * list still can't change due to our holding SE_EXCL lock and 6926 * there being no unlocked mappings left. Hence the restriction 6927 * on calling context to hat_delete_callback() 6928 */ 6929 hat_pageprocess_postcallbacks(repl, HAT_POSTUNSUSPEND); 6930 if (ret != 0) { 6931 /* 6932 * The second presuspend call failed: we got here through 6933 * the suspend_fail label above. 6934 */ 6935 ASSERT(ret != EIO); 6936 PAGE_RELOCATE_LOG(target, replacement, ret, cap_cpus); 6937 kreloc_thread = NULL; 6938 mutex_exit(&kpr_mutex); 6939 return (EAGAIN); 6940 } 6941 6942 /* 6943 * Now that we're out of the performance critical section we can 6944 * take care of updating the hash table, since we still 6945 * hold all the pages locked SE_EXCL at this point we 6946 * needn't worry about things changing out from under us. 6947 */ 6948 tpp = targ; 6949 rpp = repl; 6950 for (i = 0; i < npages; i++, tpp++, rpp++) { 6951 6952 /* 6953 * replace targ with replacement in page_hash table 6954 */ 6955 targ = tpp; 6956 page_relocate_hash(rpp, targ); 6957 6958 /* 6959 * concatenate target; caller of platform_page_relocate() 6960 * expects target to be concatenated after returning. 6961 */ 6962 ASSERT(targ->p_next == targ); 6963 ASSERT(targ->p_prev == targ); 6964 page_list_concat(&pl, &targ); 6965 } 6966 6967 ASSERT(*target == pl); 6968 *nrelocp = npages; 6969 PAGE_RELOCATE_LOG(target, replacement, 0, cap_cpus); 6970 kreloc_thread = NULL; 6971 mutex_exit(&kpr_mutex); 6972 return (0); 6973 } 6974 6975 /* 6976 * Called when stray pa_hments are found attached to a page which is 6977 * being freed. Notify the subsystem which attached the pa_hment of 6978 * the error if it registered a suitable handler, else panic. 6979 */ 6980 static void 6981 sfmmu_pahment_leaked(struct pa_hment *pahmep) 6982 { 6983 id_t cb_id = pahmep->cb_id; 6984 6985 ASSERT(cb_id >= (id_t)0 && cb_id < sfmmu_cb_nextid); 6986 if (sfmmu_cb_table[cb_id].errhandler != NULL) { 6987 if (sfmmu_cb_table[cb_id].errhandler(pahmep->addr, pahmep->len, 6988 HAT_CB_ERR_LEAKED, pahmep->pvt) == 0) 6989 return; /* non-fatal */ 6990 } 6991 panic("pa_hment leaked: 0x%p", pahmep); 6992 } 6993 6994 /* 6995 * Remove all mappings to page 'pp'. 6996 */ 6997 int 6998 hat_pageunload(struct page *pp, uint_t forceflag) 6999 { 7000 struct page *origpp = pp; 7001 struct sf_hment *sfhme, *tmphme; 7002 struct hme_blk *hmeblkp; 7003 kmutex_t *pml; 7004 #ifdef VAC 7005 kmutex_t *pmtx; 7006 #endif 7007 cpuset_t cpuset, tset; 7008 int index, cons; 7009 int xhme_blks; 7010 int pa_hments; 7011 7012 ASSERT(PAGE_EXCL(pp)); 7013 7014 retry_xhat: 7015 tmphme = NULL; 7016 xhme_blks = 0; 7017 pa_hments = 0; 7018 CPUSET_ZERO(cpuset); 7019 7020 pml = sfmmu_mlist_enter(pp); 7021 7022 #ifdef VAC 7023 if (pp->p_kpmref) 7024 sfmmu_kpm_pageunload(pp); 7025 ASSERT(!PP_ISMAPPED_KPM(pp)); 7026 #endif 7027 7028 index = PP_MAPINDEX(pp); 7029 cons = TTE8K; 7030 retry: 7031 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 7032 tmphme = sfhme->hme_next; 7033 7034 if (IS_PAHME(sfhme)) { 7035 ASSERT(sfhme->hme_data != NULL); 7036 pa_hments++; 7037 continue; 7038 } 7039 7040 hmeblkp = sfmmu_hmetohblk(sfhme); 7041 if (hmeblkp->hblk_xhat_bit) { 7042 struct xhat_hme_blk *xblk = 7043 (struct xhat_hme_blk *)hmeblkp; 7044 7045 (void) XHAT_PAGEUNLOAD(xblk->xhat_hme_blk_hat, 7046 pp, forceflag, XBLK2PROVBLK(xblk)); 7047 7048 xhme_blks = 1; 7049 continue; 7050 } 7051 7052 /* 7053 * If there are kernel mappings don't unload them, they will 7054 * be suspended. 7055 */ 7056 if (forceflag == SFMMU_KERNEL_RELOC && hmeblkp->hblk_lckcnt && 7057 hmeblkp->hblk_tag.htag_id == ksfmmup) 7058 continue; 7059 7060 tset = sfmmu_pageunload(pp, sfhme, cons); 7061 CPUSET_OR(cpuset, tset); 7062 } 7063 7064 while (index != 0) { 7065 index = index >> 1; 7066 if (index != 0) 7067 cons++; 7068 if (index & 0x1) { 7069 /* Go to leading page */ 7070 pp = PP_GROUPLEADER(pp, cons); 7071 ASSERT(sfmmu_mlist_held(pp)); 7072 goto retry; 7073 } 7074 } 7075 7076 /* 7077 * cpuset may be empty if the page was only mapped by segkpm, 7078 * in which case we won't actually cross-trap. 7079 */ 7080 xt_sync(cpuset); 7081 7082 /* 7083 * The page should have no mappings at this point, unless 7084 * we were called from hat_page_relocate() in which case we 7085 * leave the locked mappings which will be suspended later. 7086 */ 7087 ASSERT(!PP_ISMAPPED(origpp) || xhme_blks || pa_hments || 7088 (forceflag == SFMMU_KERNEL_RELOC)); 7089 7090 #ifdef VAC 7091 if (PP_ISTNC(pp)) { 7092 if (cons == TTE8K) { 7093 pmtx = sfmmu_page_enter(pp); 7094 PP_CLRTNC(pp); 7095 sfmmu_page_exit(pmtx); 7096 } else { 7097 conv_tnc(pp, cons); 7098 } 7099 } 7100 #endif /* VAC */ 7101 7102 if (pa_hments && forceflag != SFMMU_KERNEL_RELOC) { 7103 /* 7104 * Unlink any pa_hments and free them, calling back 7105 * the responsible subsystem to notify it of the error. 7106 * This can occur in situations such as drivers leaking 7107 * DMA handles: naughty, but common enough that we'd like 7108 * to keep the system running rather than bringing it 7109 * down with an obscure error like "pa_hment leaked" 7110 * which doesn't aid the user in debugging their driver. 7111 */ 7112 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 7113 tmphme = sfhme->hme_next; 7114 if (IS_PAHME(sfhme)) { 7115 struct pa_hment *pahmep = sfhme->hme_data; 7116 sfmmu_pahment_leaked(pahmep); 7117 HME_SUB(sfhme, pp); 7118 kmem_cache_free(pa_hment_cache, pahmep); 7119 } 7120 } 7121 7122 ASSERT(!PP_ISMAPPED(origpp) || xhme_blks); 7123 } 7124 7125 sfmmu_mlist_exit(pml); 7126 7127 /* 7128 * XHAT may not have finished unloading pages 7129 * because some other thread was waiting for 7130 * mlist lock and XHAT_PAGEUNLOAD let it do 7131 * the job. 7132 */ 7133 if (xhme_blks) { 7134 pp = origpp; 7135 goto retry_xhat; 7136 } 7137 7138 return (0); 7139 } 7140 7141 cpuset_t 7142 sfmmu_pageunload(page_t *pp, struct sf_hment *sfhme, int cons) 7143 { 7144 struct hme_blk *hmeblkp; 7145 sfmmu_t *sfmmup; 7146 tte_t tte, ttemod; 7147 #ifdef DEBUG 7148 tte_t orig_old; 7149 #endif /* DEBUG */ 7150 caddr_t addr; 7151 int ttesz; 7152 int ret; 7153 cpuset_t cpuset; 7154 7155 ASSERT(pp != NULL); 7156 ASSERT(sfmmu_mlist_held(pp)); 7157 ASSERT(!PP_ISKAS(pp)); 7158 7159 CPUSET_ZERO(cpuset); 7160 7161 hmeblkp = sfmmu_hmetohblk(sfhme); 7162 7163 readtte: 7164 sfmmu_copytte(&sfhme->hme_tte, &tte); 7165 if (TTE_IS_VALID(&tte)) { 7166 sfmmup = hblktosfmmu(hmeblkp); 7167 ttesz = get_hblk_ttesz(hmeblkp); 7168 /* 7169 * Only unload mappings of 'cons' size. 7170 */ 7171 if (ttesz != cons) 7172 return (cpuset); 7173 7174 /* 7175 * Note that we have p_mapping lock, but no hash lock here. 7176 * hblk_unload() has to have both hash lock AND p_mapping 7177 * lock before it tries to modify tte. So, the tte could 7178 * not become invalid in the sfmmu_modifytte_try() below. 7179 */ 7180 ttemod = tte; 7181 #ifdef DEBUG 7182 orig_old = tte; 7183 #endif /* DEBUG */ 7184 7185 TTE_SET_INVALID(&ttemod); 7186 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte); 7187 if (ret < 0) { 7188 #ifdef DEBUG 7189 /* only R/M bits can change. */ 7190 chk_tte(&orig_old, &tte, &ttemod, hmeblkp); 7191 #endif /* DEBUG */ 7192 goto readtte; 7193 } 7194 7195 if (ret == 0) { 7196 panic("pageunload: cas failed?"); 7197 } 7198 7199 addr = tte_to_vaddr(hmeblkp, tte); 7200 7201 if (hmeblkp->hblk_shared) { 7202 sf_srd_t *srdp = (sf_srd_t *)sfmmup; 7203 uint_t rid = hmeblkp->hblk_tag.htag_rid; 7204 sf_region_t *rgnp; 7205 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 7206 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 7207 ASSERT(srdp != NULL); 7208 rgnp = srdp->srd_hmergnp[rid]; 7209 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid); 7210 cpuset = sfmmu_rgntlb_demap(addr, rgnp, hmeblkp, 1); 7211 sfmmu_ttesync(NULL, addr, &tte, pp); 7212 ASSERT(rgnp->rgn_ttecnt[ttesz] > 0); 7213 atomic_add_long(&rgnp->rgn_ttecnt[ttesz], -1); 7214 } else { 7215 sfmmu_ttesync(sfmmup, addr, &tte, pp); 7216 atomic_add_long(&sfmmup->sfmmu_ttecnt[ttesz], -1); 7217 7218 /* 7219 * We need to flush the page from the virtual cache 7220 * in order to prevent a virtual cache alias 7221 * inconsistency. The particular scenario we need 7222 * to worry about is: 7223 * Given: va1 and va2 are two virtual address that 7224 * alias and will map the same physical address. 7225 * 1. mapping exists from va1 to pa and data has 7226 * been read into the cache. 7227 * 2. unload va1. 7228 * 3. load va2 and modify data using va2. 7229 * 4 unload va2. 7230 * 5. load va1 and reference data. Unless we flush 7231 * the data cache when we unload we will get 7232 * stale data. 7233 * This scenario is taken care of by using virtual 7234 * page coloring. 7235 */ 7236 if (sfmmup->sfmmu_ismhat) { 7237 /* 7238 * Flush TSBs, TLBs and caches 7239 * of every process 7240 * sharing this ism segment. 7241 */ 7242 sfmmu_hat_lock_all(); 7243 mutex_enter(&ism_mlist_lock); 7244 kpreempt_disable(); 7245 sfmmu_ismtlbcache_demap(addr, sfmmup, hmeblkp, 7246 pp->p_pagenum, CACHE_NO_FLUSH); 7247 kpreempt_enable(); 7248 mutex_exit(&ism_mlist_lock); 7249 sfmmu_hat_unlock_all(); 7250 cpuset = cpu_ready_set; 7251 } else { 7252 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 7253 cpuset = sfmmup->sfmmu_cpusran; 7254 } 7255 } 7256 7257 /* 7258 * Hme_sub has to run after ttesync() and a_rss update. 7259 * See hblk_unload(). 7260 */ 7261 HME_SUB(sfhme, pp); 7262 membar_stst(); 7263 7264 /* 7265 * We can not make ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS) 7266 * since pteload may have done a HME_ADD() right after 7267 * we did the HME_SUB() above. Hmecnt is now maintained 7268 * by cas only. no lock guranteed its value. The only 7269 * gurantee we have is the hmecnt should not be less than 7270 * what it should be so the hblk will not be taken away. 7271 * It's also important that we decremented the hmecnt after 7272 * we are done with hmeblkp so that this hmeblk won't be 7273 * stolen. 7274 */ 7275 ASSERT(hmeblkp->hblk_hmecnt > 0); 7276 ASSERT(hmeblkp->hblk_vcnt > 0); 7277 atomic_add_16(&hmeblkp->hblk_vcnt, -1); 7278 atomic_add_16(&hmeblkp->hblk_hmecnt, -1); 7279 /* 7280 * This is bug 4063182. 7281 * XXX: fixme 7282 * ASSERT(hmeblkp->hblk_hmecnt || hmeblkp->hblk_vcnt || 7283 * !hmeblkp->hblk_lckcnt); 7284 */ 7285 } else { 7286 panic("invalid tte? pp %p &tte %p", 7287 (void *)pp, (void *)&tte); 7288 } 7289 7290 return (cpuset); 7291 } 7292 7293 /* 7294 * While relocating a kernel page, this function will move the mappings 7295 * from tpp to dpp and modify any associated data with these mappings. 7296 * It also unsuspends the suspended kernel mapping. 7297 */ 7298 static void 7299 hat_pagereload(struct page *tpp, struct page *dpp) 7300 { 7301 struct sf_hment *sfhme; 7302 tte_t tte, ttemod; 7303 int index, cons; 7304 7305 ASSERT(getpil() == PIL_MAX); 7306 ASSERT(sfmmu_mlist_held(tpp)); 7307 ASSERT(sfmmu_mlist_held(dpp)); 7308 7309 index = PP_MAPINDEX(tpp); 7310 cons = TTE8K; 7311 7312 /* Update real mappings to the page */ 7313 retry: 7314 for (sfhme = tpp->p_mapping; sfhme != NULL; sfhme = sfhme->hme_next) { 7315 if (IS_PAHME(sfhme)) 7316 continue; 7317 sfmmu_copytte(&sfhme->hme_tte, &tte); 7318 ttemod = tte; 7319 7320 /* 7321 * replace old pfn with new pfn in TTE 7322 */ 7323 PFN_TO_TTE(ttemod, dpp->p_pagenum); 7324 7325 /* 7326 * clear suspend bit 7327 */ 7328 ASSERT(TTE_IS_SUSPEND(&ttemod)); 7329 TTE_CLR_SUSPEND(&ttemod); 7330 7331 if (sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte) < 0) 7332 panic("hat_pagereload(): sfmmu_modifytte_try() failed"); 7333 7334 /* 7335 * set hme_page point to new page 7336 */ 7337 sfhme->hme_page = dpp; 7338 } 7339 7340 /* 7341 * move p_mapping list from old page to new page 7342 */ 7343 dpp->p_mapping = tpp->p_mapping; 7344 tpp->p_mapping = NULL; 7345 dpp->p_share = tpp->p_share; 7346 tpp->p_share = 0; 7347 7348 while (index != 0) { 7349 index = index >> 1; 7350 if (index != 0) 7351 cons++; 7352 if (index & 0x1) { 7353 tpp = PP_GROUPLEADER(tpp, cons); 7354 dpp = PP_GROUPLEADER(dpp, cons); 7355 goto retry; 7356 } 7357 } 7358 7359 curthread->t_flag &= ~T_DONTDTRACE; 7360 mutex_exit(&kpr_suspendlock); 7361 } 7362 7363 uint_t 7364 hat_pagesync(struct page *pp, uint_t clearflag) 7365 { 7366 struct sf_hment *sfhme, *tmphme = NULL; 7367 struct hme_blk *hmeblkp; 7368 kmutex_t *pml; 7369 cpuset_t cpuset, tset; 7370 int index, cons; 7371 extern ulong_t po_share; 7372 page_t *save_pp = pp; 7373 int stop_on_sh = 0; 7374 uint_t shcnt; 7375 7376 CPUSET_ZERO(cpuset); 7377 7378 if (PP_ISRO(pp) && (clearflag & HAT_SYNC_STOPON_MOD)) { 7379 return (PP_GENERIC_ATTR(pp)); 7380 } 7381 7382 if ((clearflag == (HAT_SYNC_STOPON_REF | HAT_SYNC_DONTZERO)) && 7383 PP_ISREF(pp)) { 7384 return (PP_GENERIC_ATTR(pp)); 7385 } 7386 7387 if ((clearflag == (HAT_SYNC_STOPON_MOD | HAT_SYNC_DONTZERO)) && 7388 PP_ISMOD(pp)) { 7389 return (PP_GENERIC_ATTR(pp)); 7390 } 7391 7392 if ((clearflag & HAT_SYNC_STOPON_SHARED) != 0 && 7393 (pp->p_share > po_share) && 7394 !(clearflag & HAT_SYNC_ZERORM)) { 7395 hat_page_setattr(pp, P_REF); 7396 return (PP_GENERIC_ATTR(pp)); 7397 } 7398 7399 if ((clearflag & HAT_SYNC_STOPON_SHARED) && 7400 !(clearflag & HAT_SYNC_ZERORM)) { 7401 stop_on_sh = 1; 7402 shcnt = 0; 7403 } 7404 clearflag &= ~HAT_SYNC_STOPON_SHARED; 7405 pml = sfmmu_mlist_enter(pp); 7406 index = PP_MAPINDEX(pp); 7407 cons = TTE8K; 7408 retry: 7409 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 7410 /* 7411 * We need to save the next hment on the list since 7412 * it is possible for pagesync to remove an invalid hment 7413 * from the list. 7414 */ 7415 tmphme = sfhme->hme_next; 7416 if (IS_PAHME(sfhme)) 7417 continue; 7418 /* 7419 * If we are looking for large mappings and this hme doesn't 7420 * reach the range we are seeking, just ignore it. 7421 */ 7422 hmeblkp = sfmmu_hmetohblk(sfhme); 7423 if (hmeblkp->hblk_xhat_bit) 7424 continue; 7425 7426 if (hme_size(sfhme) < cons) 7427 continue; 7428 7429 if (stop_on_sh) { 7430 if (hmeblkp->hblk_shared) { 7431 sf_srd_t *srdp = hblktosrd(hmeblkp); 7432 uint_t rid = hmeblkp->hblk_tag.htag_rid; 7433 sf_region_t *rgnp; 7434 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 7435 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 7436 ASSERT(srdp != NULL); 7437 rgnp = srdp->srd_hmergnp[rid]; 7438 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, 7439 rgnp, rid); 7440 shcnt += rgnp->rgn_refcnt; 7441 } else { 7442 shcnt++; 7443 } 7444 if (shcnt > po_share) { 7445 /* 7446 * tell the pager to spare the page this time 7447 * around. 7448 */ 7449 hat_page_setattr(save_pp, P_REF); 7450 index = 0; 7451 break; 7452 } 7453 } 7454 tset = sfmmu_pagesync(pp, sfhme, 7455 clearflag & ~HAT_SYNC_STOPON_RM); 7456 CPUSET_OR(cpuset, tset); 7457 7458 /* 7459 * If clearflag is HAT_SYNC_DONTZERO, break out as soon 7460 * as the "ref" or "mod" is set or share cnt exceeds po_share. 7461 */ 7462 if ((clearflag & ~HAT_SYNC_STOPON_RM) == HAT_SYNC_DONTZERO && 7463 (((clearflag & HAT_SYNC_STOPON_MOD) && PP_ISMOD(save_pp)) || 7464 ((clearflag & HAT_SYNC_STOPON_REF) && PP_ISREF(save_pp)))) { 7465 index = 0; 7466 break; 7467 } 7468 } 7469 7470 while (index) { 7471 index = index >> 1; 7472 cons++; 7473 if (index & 0x1) { 7474 /* Go to leading page */ 7475 pp = PP_GROUPLEADER(pp, cons); 7476 goto retry; 7477 } 7478 } 7479 7480 xt_sync(cpuset); 7481 sfmmu_mlist_exit(pml); 7482 return (PP_GENERIC_ATTR(save_pp)); 7483 } 7484 7485 /* 7486 * Get all the hardware dependent attributes for a page struct 7487 */ 7488 static cpuset_t 7489 sfmmu_pagesync(struct page *pp, struct sf_hment *sfhme, 7490 uint_t clearflag) 7491 { 7492 caddr_t addr; 7493 tte_t tte, ttemod; 7494 struct hme_blk *hmeblkp; 7495 int ret; 7496 sfmmu_t *sfmmup; 7497 cpuset_t cpuset; 7498 7499 ASSERT(pp != NULL); 7500 ASSERT(sfmmu_mlist_held(pp)); 7501 ASSERT((clearflag == HAT_SYNC_DONTZERO) || 7502 (clearflag == HAT_SYNC_ZERORM)); 7503 7504 SFMMU_STAT(sf_pagesync); 7505 7506 CPUSET_ZERO(cpuset); 7507 7508 sfmmu_pagesync_retry: 7509 7510 sfmmu_copytte(&sfhme->hme_tte, &tte); 7511 if (TTE_IS_VALID(&tte)) { 7512 hmeblkp = sfmmu_hmetohblk(sfhme); 7513 sfmmup = hblktosfmmu(hmeblkp); 7514 addr = tte_to_vaddr(hmeblkp, tte); 7515 if (clearflag == HAT_SYNC_ZERORM) { 7516 ttemod = tte; 7517 TTE_CLR_RM(&ttemod); 7518 ret = sfmmu_modifytte_try(&tte, &ttemod, 7519 &sfhme->hme_tte); 7520 if (ret < 0) { 7521 /* 7522 * cas failed and the new value is not what 7523 * we want. 7524 */ 7525 goto sfmmu_pagesync_retry; 7526 } 7527 7528 if (ret > 0) { 7529 /* we win the cas */ 7530 if (hmeblkp->hblk_shared) { 7531 sf_srd_t *srdp = (sf_srd_t *)sfmmup; 7532 uint_t rid = 7533 hmeblkp->hblk_tag.htag_rid; 7534 sf_region_t *rgnp; 7535 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 7536 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 7537 ASSERT(srdp != NULL); 7538 rgnp = srdp->srd_hmergnp[rid]; 7539 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, 7540 srdp, rgnp, rid); 7541 cpuset = sfmmu_rgntlb_demap(addr, 7542 rgnp, hmeblkp, 1); 7543 } else { 7544 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 7545 0, 0); 7546 cpuset = sfmmup->sfmmu_cpusran; 7547 } 7548 } 7549 } 7550 sfmmu_ttesync(hmeblkp->hblk_shared ? NULL : sfmmup, addr, 7551 &tte, pp); 7552 } 7553 return (cpuset); 7554 } 7555 7556 /* 7557 * Remove write permission from a mappings to a page, so that 7558 * we can detect the next modification of it. This requires modifying 7559 * the TTE then invalidating (demap) any TLB entry using that TTE. 7560 * This code is similar to sfmmu_pagesync(). 7561 */ 7562 static cpuset_t 7563 sfmmu_pageclrwrt(struct page *pp, struct sf_hment *sfhme) 7564 { 7565 caddr_t addr; 7566 tte_t tte; 7567 tte_t ttemod; 7568 struct hme_blk *hmeblkp; 7569 int ret; 7570 sfmmu_t *sfmmup; 7571 cpuset_t cpuset; 7572 7573 ASSERT(pp != NULL); 7574 ASSERT(sfmmu_mlist_held(pp)); 7575 7576 CPUSET_ZERO(cpuset); 7577 SFMMU_STAT(sf_clrwrt); 7578 7579 retry: 7580 7581 sfmmu_copytte(&sfhme->hme_tte, &tte); 7582 if (TTE_IS_VALID(&tte) && TTE_IS_WRITABLE(&tte)) { 7583 hmeblkp = sfmmu_hmetohblk(sfhme); 7584 7585 /* 7586 * xhat mappings should never be to a VMODSORT page. 7587 */ 7588 ASSERT(hmeblkp->hblk_xhat_bit == 0); 7589 7590 sfmmup = hblktosfmmu(hmeblkp); 7591 addr = tte_to_vaddr(hmeblkp, tte); 7592 7593 ttemod = tte; 7594 TTE_CLR_WRT(&ttemod); 7595 TTE_CLR_MOD(&ttemod); 7596 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte); 7597 7598 /* 7599 * if cas failed and the new value is not what 7600 * we want retry 7601 */ 7602 if (ret < 0) 7603 goto retry; 7604 7605 /* we win the cas */ 7606 if (ret > 0) { 7607 if (hmeblkp->hblk_shared) { 7608 sf_srd_t *srdp = (sf_srd_t *)sfmmup; 7609 uint_t rid = hmeblkp->hblk_tag.htag_rid; 7610 sf_region_t *rgnp; 7611 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 7612 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 7613 ASSERT(srdp != NULL); 7614 rgnp = srdp->srd_hmergnp[rid]; 7615 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, 7616 srdp, rgnp, rid); 7617 cpuset = sfmmu_rgntlb_demap(addr, 7618 rgnp, hmeblkp, 1); 7619 } else { 7620 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0); 7621 cpuset = sfmmup->sfmmu_cpusran; 7622 } 7623 } 7624 } 7625 7626 return (cpuset); 7627 } 7628 7629 /* 7630 * Walk all mappings of a page, removing write permission and clearing the 7631 * ref/mod bits. This code is similar to hat_pagesync() 7632 */ 7633 static void 7634 hat_page_clrwrt(page_t *pp) 7635 { 7636 struct sf_hment *sfhme; 7637 struct sf_hment *tmphme = NULL; 7638 kmutex_t *pml; 7639 cpuset_t cpuset; 7640 cpuset_t tset; 7641 int index; 7642 int cons; 7643 7644 CPUSET_ZERO(cpuset); 7645 7646 pml = sfmmu_mlist_enter(pp); 7647 index = PP_MAPINDEX(pp); 7648 cons = TTE8K; 7649 retry: 7650 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 7651 tmphme = sfhme->hme_next; 7652 7653 /* 7654 * If we are looking for large mappings and this hme doesn't 7655 * reach the range we are seeking, just ignore its. 7656 */ 7657 7658 if (hme_size(sfhme) < cons) 7659 continue; 7660 7661 tset = sfmmu_pageclrwrt(pp, sfhme); 7662 CPUSET_OR(cpuset, tset); 7663 } 7664 7665 while (index) { 7666 index = index >> 1; 7667 cons++; 7668 if (index & 0x1) { 7669 /* Go to leading page */ 7670 pp = PP_GROUPLEADER(pp, cons); 7671 goto retry; 7672 } 7673 } 7674 7675 xt_sync(cpuset); 7676 sfmmu_mlist_exit(pml); 7677 } 7678 7679 /* 7680 * Set the given REF/MOD/RO bits for the given page. 7681 * For a vnode with a sorted v_pages list, we need to change 7682 * the attributes and the v_pages list together under page_vnode_mutex. 7683 */ 7684 void 7685 hat_page_setattr(page_t *pp, uint_t flag) 7686 { 7687 vnode_t *vp = pp->p_vnode; 7688 page_t **listp; 7689 kmutex_t *pmtx; 7690 kmutex_t *vphm = NULL; 7691 int noshuffle; 7692 7693 noshuffle = flag & P_NSH; 7694 flag &= ~P_NSH; 7695 7696 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO))); 7697 7698 /* 7699 * nothing to do if attribute already set 7700 */ 7701 if ((pp->p_nrm & flag) == flag) 7702 return; 7703 7704 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp) && 7705 !noshuffle) { 7706 vphm = page_vnode_mutex(vp); 7707 mutex_enter(vphm); 7708 } 7709 7710 pmtx = sfmmu_page_enter(pp); 7711 pp->p_nrm |= flag; 7712 sfmmu_page_exit(pmtx); 7713 7714 if (vphm != NULL) { 7715 /* 7716 * Some File Systems examine v_pages for NULL w/o 7717 * grabbing the vphm mutex. Must not let it become NULL when 7718 * pp is the only page on the list. 7719 */ 7720 if (pp->p_vpnext != pp) { 7721 page_vpsub(&vp->v_pages, pp); 7722 if (vp->v_pages != NULL) 7723 listp = &vp->v_pages->p_vpprev->p_vpnext; 7724 else 7725 listp = &vp->v_pages; 7726 page_vpadd(listp, pp); 7727 } 7728 mutex_exit(vphm); 7729 } 7730 } 7731 7732 void 7733 hat_page_clrattr(page_t *pp, uint_t flag) 7734 { 7735 vnode_t *vp = pp->p_vnode; 7736 kmutex_t *pmtx; 7737 7738 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO))); 7739 7740 pmtx = sfmmu_page_enter(pp); 7741 7742 /* 7743 * Caller is expected to hold page's io lock for VMODSORT to work 7744 * correctly with pvn_vplist_dirty() and pvn_getdirty() when mod 7745 * bit is cleared. 7746 * We don't have assert to avoid tripping some existing third party 7747 * code. The dirty page is moved back to top of the v_page list 7748 * after IO is done in pvn_write_done(). 7749 */ 7750 pp->p_nrm &= ~flag; 7751 sfmmu_page_exit(pmtx); 7752 7753 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp)) { 7754 7755 /* 7756 * VMODSORT works by removing write permissions and getting 7757 * a fault when a page is made dirty. At this point 7758 * we need to remove write permission from all mappings 7759 * to this page. 7760 */ 7761 hat_page_clrwrt(pp); 7762 } 7763 } 7764 7765 uint_t 7766 hat_page_getattr(page_t *pp, uint_t flag) 7767 { 7768 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO))); 7769 return ((uint_t)(pp->p_nrm & flag)); 7770 } 7771 7772 /* 7773 * DEBUG kernels: verify that a kernel va<->pa translation 7774 * is safe by checking the underlying page_t is in a page 7775 * relocation-safe state. 7776 */ 7777 #ifdef DEBUG 7778 void 7779 sfmmu_check_kpfn(pfn_t pfn) 7780 { 7781 page_t *pp; 7782 int index, cons; 7783 7784 if (hat_check_vtop == 0) 7785 return; 7786 7787 if (hat_kpr_enabled == 0 || kvseg.s_base == NULL || panicstr) 7788 return; 7789 7790 pp = page_numtopp_nolock(pfn); 7791 if (!pp) 7792 return; 7793 7794 if (PAGE_LOCKED(pp) || PP_ISNORELOC(pp)) 7795 return; 7796 7797 /* 7798 * Handed a large kernel page, we dig up the root page since we 7799 * know the root page might have the lock also. 7800 */ 7801 if (pp->p_szc != 0) { 7802 index = PP_MAPINDEX(pp); 7803 cons = TTE8K; 7804 again: 7805 while (index != 0) { 7806 index >>= 1; 7807 if (index != 0) 7808 cons++; 7809 if (index & 0x1) { 7810 pp = PP_GROUPLEADER(pp, cons); 7811 goto again; 7812 } 7813 } 7814 } 7815 7816 if (PAGE_LOCKED(pp) || PP_ISNORELOC(pp)) 7817 return; 7818 7819 /* 7820 * Pages need to be locked or allocated "permanent" (either from 7821 * static_arena arena or explicitly setting PG_NORELOC when calling 7822 * page_create_va()) for VA->PA translations to be valid. 7823 */ 7824 if (!PP_ISNORELOC(pp)) 7825 panic("Illegal VA->PA translation, pp 0x%p not permanent", pp); 7826 else 7827 panic("Illegal VA->PA translation, pp 0x%p not locked", pp); 7828 } 7829 #endif /* DEBUG */ 7830 7831 /* 7832 * Returns a page frame number for a given virtual address. 7833 * Returns PFN_INVALID to indicate an invalid mapping 7834 */ 7835 pfn_t 7836 hat_getpfnum(struct hat *hat, caddr_t addr) 7837 { 7838 pfn_t pfn; 7839 tte_t tte; 7840 7841 /* 7842 * We would like to 7843 * ASSERT(AS_LOCK_HELD(as, &as->a_lock)); 7844 * but we can't because the iommu driver will call this 7845 * routine at interrupt time and it can't grab the as lock 7846 * or it will deadlock: A thread could have the as lock 7847 * and be waiting for io. The io can't complete 7848 * because the interrupt thread is blocked trying to grab 7849 * the as lock. 7850 */ 7851 7852 ASSERT(hat->sfmmu_xhat_provider == NULL); 7853 7854 if (hat == ksfmmup) { 7855 if (IS_KMEM_VA_LARGEPAGE(addr)) { 7856 ASSERT(segkmem_lpszc > 0); 7857 pfn = sfmmu_kvaszc2pfn(addr, segkmem_lpszc); 7858 if (pfn != PFN_INVALID) { 7859 sfmmu_check_kpfn(pfn); 7860 return (pfn); 7861 } 7862 } else if (segkpm && IS_KPM_ADDR(addr)) { 7863 return (sfmmu_kpm_vatopfn(addr)); 7864 } 7865 while ((pfn = sfmmu_vatopfn(addr, ksfmmup, &tte)) 7866 == PFN_SUSPENDED) { 7867 sfmmu_vatopfn_suspended(addr, ksfmmup, &tte); 7868 } 7869 sfmmu_check_kpfn(pfn); 7870 return (pfn); 7871 } else { 7872 return (sfmmu_uvatopfn(addr, hat, NULL)); 7873 } 7874 } 7875 7876 /* 7877 * hat_getkpfnum() is an obsolete DDI routine, and its use is discouraged. 7878 * Use hat_getpfnum(kas.a_hat, ...) instead. 7879 * 7880 * We'd like to return PFN_INVALID if the mappings have underlying page_t's 7881 * but can't right now due to the fact that some software has grown to use 7882 * this interface incorrectly. So for now when the interface is misused, 7883 * return a warning to the user that in the future it won't work in the 7884 * way they're abusing it, and carry on (after disabling page relocation). 7885 */ 7886 pfn_t 7887 hat_getkpfnum(caddr_t addr) 7888 { 7889 pfn_t pfn; 7890 tte_t tte; 7891 int badcaller = 0; 7892 extern int segkmem_reloc; 7893 7894 if (segkpm && IS_KPM_ADDR(addr)) { 7895 badcaller = 1; 7896 pfn = sfmmu_kpm_vatopfn(addr); 7897 } else { 7898 while ((pfn = sfmmu_vatopfn(addr, ksfmmup, &tte)) 7899 == PFN_SUSPENDED) { 7900 sfmmu_vatopfn_suspended(addr, ksfmmup, &tte); 7901 } 7902 badcaller = pf_is_memory(pfn); 7903 } 7904 7905 if (badcaller) { 7906 /* 7907 * We can't return PFN_INVALID or the caller may panic 7908 * or corrupt the system. The only alternative is to 7909 * disable page relocation at this point for all kernel 7910 * memory. This will impact any callers of page_relocate() 7911 * such as FMA or DR. 7912 * 7913 * RFE: Add junk here to spit out an ereport so the sysadmin 7914 * can be advised that he should upgrade his device driver 7915 * so that this doesn't happen. 7916 */ 7917 hat_getkpfnum_badcall(caller()); 7918 if (hat_kpr_enabled && segkmem_reloc) { 7919 hat_kpr_enabled = 0; 7920 segkmem_reloc = 0; 7921 cmn_err(CE_WARN, "Kernel Page Relocation is DISABLED"); 7922 } 7923 } 7924 return (pfn); 7925 } 7926 7927 /* 7928 * This routine will return both pfn and tte for the vaddr. 7929 */ 7930 static pfn_t 7931 sfmmu_uvatopfn(caddr_t vaddr, struct hat *sfmmup, tte_t *ttep) 7932 { 7933 struct hmehash_bucket *hmebp; 7934 hmeblk_tag hblktag; 7935 int hmeshift, hashno = 1; 7936 struct hme_blk *hmeblkp = NULL; 7937 tte_t tte; 7938 7939 struct sf_hment *sfhmep; 7940 pfn_t pfn; 7941 7942 /* support for ISM */ 7943 ism_map_t *ism_map; 7944 ism_blk_t *ism_blkp; 7945 int i; 7946 sfmmu_t *ism_hatid = NULL; 7947 sfmmu_t *locked_hatid = NULL; 7948 sfmmu_t *sv_sfmmup = sfmmup; 7949 caddr_t sv_vaddr = vaddr; 7950 sf_srd_t *srdp; 7951 7952 if (ttep == NULL) { 7953 ttep = &tte; 7954 } else { 7955 ttep->ll = 0; 7956 } 7957 7958 ASSERT(sfmmup != ksfmmup); 7959 SFMMU_STAT(sf_user_vtop); 7960 /* 7961 * Set ism_hatid if vaddr falls in a ISM segment. 7962 */ 7963 ism_blkp = sfmmup->sfmmu_iblk; 7964 if (ism_blkp != NULL) { 7965 sfmmu_ismhat_enter(sfmmup, 0); 7966 locked_hatid = sfmmup; 7967 } 7968 while (ism_blkp != NULL && ism_hatid == NULL) { 7969 ism_map = ism_blkp->iblk_maps; 7970 for (i = 0; ism_map[i].imap_ismhat && i < ISM_MAP_SLOTS; i++) { 7971 if (vaddr >= ism_start(ism_map[i]) && 7972 vaddr < ism_end(ism_map[i])) { 7973 sfmmup = ism_hatid = ism_map[i].imap_ismhat; 7974 vaddr = (caddr_t)(vaddr - 7975 ism_start(ism_map[i])); 7976 break; 7977 } 7978 } 7979 ism_blkp = ism_blkp->iblk_next; 7980 } 7981 if (locked_hatid) { 7982 sfmmu_ismhat_exit(locked_hatid, 0); 7983 } 7984 7985 hblktag.htag_id = sfmmup; 7986 hblktag.htag_rid = SFMMU_INVALID_SHMERID; 7987 do { 7988 hmeshift = HME_HASH_SHIFT(hashno); 7989 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift); 7990 hblktag.htag_rehash = hashno; 7991 hmebp = HME_HASH_FUNCTION(sfmmup, vaddr, hmeshift); 7992 7993 SFMMU_HASH_LOCK(hmebp); 7994 7995 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp); 7996 if (hmeblkp != NULL) { 7997 ASSERT(!hmeblkp->hblk_shared); 7998 HBLKTOHME(sfhmep, hmeblkp, vaddr); 7999 sfmmu_copytte(&sfhmep->hme_tte, ttep); 8000 SFMMU_HASH_UNLOCK(hmebp); 8001 if (TTE_IS_VALID(ttep)) { 8002 pfn = TTE_TO_PFN(vaddr, ttep); 8003 return (pfn); 8004 } 8005 break; 8006 } 8007 SFMMU_HASH_UNLOCK(hmebp); 8008 hashno++; 8009 } while (HME_REHASH(sfmmup) && (hashno <= mmu_hashcnt)); 8010 8011 if (SF_HMERGNMAP_ISNULL(sv_sfmmup)) { 8012 return (PFN_INVALID); 8013 } 8014 srdp = sv_sfmmup->sfmmu_srdp; 8015 ASSERT(srdp != NULL); 8016 ASSERT(srdp->srd_refcnt != 0); 8017 hblktag.htag_id = srdp; 8018 hashno = 1; 8019 do { 8020 hmeshift = HME_HASH_SHIFT(hashno); 8021 hblktag.htag_bspage = HME_HASH_BSPAGE(sv_vaddr, hmeshift); 8022 hblktag.htag_rehash = hashno; 8023 hmebp = HME_HASH_FUNCTION(srdp, sv_vaddr, hmeshift); 8024 8025 SFMMU_HASH_LOCK(hmebp); 8026 for (hmeblkp = hmebp->hmeblkp; hmeblkp != NULL; 8027 hmeblkp = hmeblkp->hblk_next) { 8028 uint_t rid; 8029 sf_region_t *rgnp; 8030 caddr_t rsaddr; 8031 caddr_t readdr; 8032 8033 if (!HTAGS_EQ_SHME(hmeblkp->hblk_tag, hblktag, 8034 sv_sfmmup->sfmmu_hmeregion_map)) { 8035 continue; 8036 } 8037 ASSERT(hmeblkp->hblk_shared); 8038 rid = hmeblkp->hblk_tag.htag_rid; 8039 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 8040 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 8041 rgnp = srdp->srd_hmergnp[rid]; 8042 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid); 8043 HBLKTOHME(sfhmep, hmeblkp, sv_vaddr); 8044 sfmmu_copytte(&sfhmep->hme_tte, ttep); 8045 rsaddr = rgnp->rgn_saddr; 8046 readdr = rsaddr + rgnp->rgn_size; 8047 #ifdef DEBUG 8048 if (TTE_IS_VALID(ttep) || 8049 get_hblk_ttesz(hmeblkp) > TTE8K) { 8050 caddr_t eva = tte_to_evaddr(hmeblkp, ttep); 8051 ASSERT(eva > sv_vaddr); 8052 ASSERT(sv_vaddr >= rsaddr); 8053 ASSERT(sv_vaddr < readdr); 8054 ASSERT(eva <= readdr); 8055 } 8056 #endif /* DEBUG */ 8057 /* 8058 * Continue the search if we 8059 * found an invalid 8K tte outside of the area 8060 * covered by this hmeblk's region. 8061 */ 8062 if (TTE_IS_VALID(ttep)) { 8063 SFMMU_HASH_UNLOCK(hmebp); 8064 pfn = TTE_TO_PFN(sv_vaddr, ttep); 8065 return (pfn); 8066 } else if (get_hblk_ttesz(hmeblkp) > TTE8K || 8067 (sv_vaddr >= rsaddr && sv_vaddr < readdr)) { 8068 SFMMU_HASH_UNLOCK(hmebp); 8069 pfn = PFN_INVALID; 8070 return (pfn); 8071 } 8072 } 8073 SFMMU_HASH_UNLOCK(hmebp); 8074 hashno++; 8075 } while (hashno <= mmu_hashcnt); 8076 return (PFN_INVALID); 8077 } 8078 8079 8080 /* 8081 * For compatability with AT&T and later optimizations 8082 */ 8083 /* ARGSUSED */ 8084 void 8085 hat_map(struct hat *hat, caddr_t addr, size_t len, uint_t flags) 8086 { 8087 ASSERT(hat != NULL); 8088 ASSERT(hat->sfmmu_xhat_provider == NULL); 8089 } 8090 8091 /* 8092 * Return the number of mappings to a particular page. This number is an 8093 * approximation of the number of people sharing the page. 8094 * 8095 * shared hmeblks or ism hmeblks are counted as 1 mapping here. 8096 * hat_page_checkshare() can be used to compare threshold to share 8097 * count that reflects the number of region sharers albeit at higher cost. 8098 */ 8099 ulong_t 8100 hat_page_getshare(page_t *pp) 8101 { 8102 page_t *spp = pp; /* start page */ 8103 kmutex_t *pml; 8104 ulong_t cnt; 8105 int index, sz = TTE64K; 8106 8107 /* 8108 * We need to grab the mlist lock to make sure any outstanding 8109 * load/unloads complete. Otherwise we could return zero 8110 * even though the unload(s) hasn't finished yet. 8111 */ 8112 pml = sfmmu_mlist_enter(spp); 8113 cnt = spp->p_share; 8114 8115 #ifdef VAC 8116 if (kpm_enable) 8117 cnt += spp->p_kpmref; 8118 #endif 8119 8120 /* 8121 * If we have any large mappings, we count the number of 8122 * mappings that this large page is part of. 8123 */ 8124 index = PP_MAPINDEX(spp); 8125 index >>= 1; 8126 while (index) { 8127 pp = PP_GROUPLEADER(spp, sz); 8128 if ((index & 0x1) && pp != spp) { 8129 cnt += pp->p_share; 8130 spp = pp; 8131 } 8132 index >>= 1; 8133 sz++; 8134 } 8135 sfmmu_mlist_exit(pml); 8136 return (cnt); 8137 } 8138 8139 /* 8140 * Return 1 if the number of mappings exceeds sh_thresh. Return 0 8141 * otherwise. Count shared hmeblks by region's refcnt. 8142 */ 8143 int 8144 hat_page_checkshare(page_t *pp, ulong_t sh_thresh) 8145 { 8146 kmutex_t *pml; 8147 ulong_t cnt = 0; 8148 int index, sz = TTE8K; 8149 struct sf_hment *sfhme, *tmphme = NULL; 8150 struct hme_blk *hmeblkp; 8151 8152 pml = sfmmu_mlist_enter(pp); 8153 8154 if (kpm_enable) 8155 cnt = pp->p_kpmref; 8156 8157 if (pp->p_share + cnt > sh_thresh) { 8158 sfmmu_mlist_exit(pml); 8159 return (1); 8160 } 8161 8162 index = PP_MAPINDEX(pp); 8163 8164 again: 8165 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) { 8166 tmphme = sfhme->hme_next; 8167 if (IS_PAHME(sfhme)) { 8168 continue; 8169 } 8170 8171 hmeblkp = sfmmu_hmetohblk(sfhme); 8172 if (hmeblkp->hblk_xhat_bit) { 8173 cnt++; 8174 if (cnt > sh_thresh) { 8175 sfmmu_mlist_exit(pml); 8176 return (1); 8177 } 8178 continue; 8179 } 8180 if (hme_size(sfhme) != sz) { 8181 continue; 8182 } 8183 8184 if (hmeblkp->hblk_shared) { 8185 sf_srd_t *srdp = hblktosrd(hmeblkp); 8186 uint_t rid = hmeblkp->hblk_tag.htag_rid; 8187 sf_region_t *rgnp; 8188 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 8189 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 8190 ASSERT(srdp != NULL); 8191 rgnp = srdp->srd_hmergnp[rid]; 8192 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, 8193 rgnp, rid); 8194 cnt += rgnp->rgn_refcnt; 8195 } else { 8196 cnt++; 8197 } 8198 if (cnt > sh_thresh) { 8199 sfmmu_mlist_exit(pml); 8200 return (1); 8201 } 8202 } 8203 8204 index >>= 1; 8205 sz++; 8206 while (index) { 8207 pp = PP_GROUPLEADER(pp, sz); 8208 ASSERT(sfmmu_mlist_held(pp)); 8209 if (index & 0x1) { 8210 goto again; 8211 } 8212 index >>= 1; 8213 sz++; 8214 } 8215 sfmmu_mlist_exit(pml); 8216 return (0); 8217 } 8218 8219 /* 8220 * Unload all large mappings to the pp and reset the p_szc field of every 8221 * constituent page according to the remaining mappings. 8222 * 8223 * pp must be locked SE_EXCL. Even though no other constituent pages are 8224 * locked it's legal to unload the large mappings to the pp because all 8225 * constituent pages of large locked mappings have to be locked SE_SHARED. 8226 * This means if we have SE_EXCL lock on one of constituent pages none of the 8227 * large mappings to pp are locked. 8228 * 8229 * Decrease p_szc field starting from the last constituent page and ending 8230 * with the root page. This method is used because other threads rely on the 8231 * root's p_szc to find the lock to syncronize on. After a root page_t's p_szc 8232 * is demoted then other threads will succeed in sfmmu_mlspl_enter(). This 8233 * ensures that p_szc changes of the constituent pages appears atomic for all 8234 * threads that use sfmmu_mlspl_enter() to examine p_szc field. 8235 * 8236 * This mechanism is only used for file system pages where it's not always 8237 * possible to get SE_EXCL locks on all constituent pages to demote the size 8238 * code (as is done for anonymous or kernel large pages). 8239 * 8240 * See more comments in front of sfmmu_mlspl_enter(). 8241 */ 8242 void 8243 hat_page_demote(page_t *pp) 8244 { 8245 int index; 8246 int sz; 8247 cpuset_t cpuset; 8248 int sync = 0; 8249 page_t *rootpp; 8250 struct sf_hment *sfhme; 8251 struct sf_hment *tmphme = NULL; 8252 struct hme_blk *hmeblkp; 8253 uint_t pszc; 8254 page_t *lastpp; 8255 cpuset_t tset; 8256 pgcnt_t npgs; 8257 kmutex_t *pml; 8258 kmutex_t *pmtx = NULL; 8259 8260 ASSERT(PAGE_EXCL(pp)); 8261 ASSERT(!PP_ISFREE(pp)); 8262 ASSERT(!PP_ISKAS(pp)); 8263 ASSERT(page_szc_lock_assert(pp)); 8264 pml = sfmmu_mlist_enter(pp); 8265 8266 pszc = pp->p_szc; 8267 if (pszc == 0) { 8268 goto out; 8269 } 8270 8271 index = PP_MAPINDEX(pp) >> 1; 8272 8273 if (index) { 8274 CPUSET_ZERO(cpuset); 8275 sz = TTE64K; 8276 sync = 1; 8277 } 8278 8279 while (index) { 8280 if (!(index & 0x1)) { 8281 index >>= 1; 8282 sz++; 8283 continue; 8284 } 8285 ASSERT(sz <= pszc); 8286 rootpp = PP_GROUPLEADER(pp, sz); 8287 for (sfhme = rootpp->p_mapping; sfhme; sfhme = tmphme) { 8288 tmphme = sfhme->hme_next; 8289 ASSERT(!IS_PAHME(sfhme)); 8290 hmeblkp = sfmmu_hmetohblk(sfhme); 8291 if (hme_size(sfhme) != sz) { 8292 continue; 8293 } 8294 if (hmeblkp->hblk_xhat_bit) { 8295 cmn_err(CE_PANIC, 8296 "hat_page_demote: xhat hmeblk"); 8297 } 8298 tset = sfmmu_pageunload(rootpp, sfhme, sz); 8299 CPUSET_OR(cpuset, tset); 8300 } 8301 if (index >>= 1) { 8302 sz++; 8303 } 8304 } 8305 8306 ASSERT(!PP_ISMAPPED_LARGE(pp)); 8307 8308 if (sync) { 8309 xt_sync(cpuset); 8310 #ifdef VAC 8311 if (PP_ISTNC(pp)) { 8312 conv_tnc(rootpp, sz); 8313 } 8314 #endif /* VAC */ 8315 } 8316 8317 pmtx = sfmmu_page_enter(pp); 8318 8319 ASSERT(pp->p_szc == pszc); 8320 rootpp = PP_PAGEROOT(pp); 8321 ASSERT(rootpp->p_szc == pszc); 8322 lastpp = PP_PAGENEXT_N(rootpp, TTEPAGES(pszc) - 1); 8323 8324 while (lastpp != rootpp) { 8325 sz = PP_MAPINDEX(lastpp) ? fnd_mapping_sz(lastpp) : 0; 8326 ASSERT(sz < pszc); 8327 npgs = (sz == 0) ? 1 : TTEPAGES(sz); 8328 ASSERT(P2PHASE(lastpp->p_pagenum, npgs) == npgs - 1); 8329 while (--npgs > 0) { 8330 lastpp->p_szc = (uchar_t)sz; 8331 lastpp = PP_PAGEPREV(lastpp); 8332 } 8333 if (sz) { 8334 /* 8335 * make sure before current root's pszc 8336 * is updated all updates to constituent pages pszc 8337 * fields are globally visible. 8338 */ 8339 membar_producer(); 8340 } 8341 lastpp->p_szc = sz; 8342 ASSERT(IS_P2ALIGNED(lastpp->p_pagenum, TTEPAGES(sz))); 8343 if (lastpp != rootpp) { 8344 lastpp = PP_PAGEPREV(lastpp); 8345 } 8346 } 8347 if (sz == 0) { 8348 /* the loop above doesn't cover this case */ 8349 rootpp->p_szc = 0; 8350 } 8351 out: 8352 ASSERT(pp->p_szc == 0); 8353 if (pmtx != NULL) { 8354 sfmmu_page_exit(pmtx); 8355 } 8356 sfmmu_mlist_exit(pml); 8357 } 8358 8359 /* 8360 * Refresh the HAT ismttecnt[] element for size szc. 8361 * Caller must have set ISM busy flag to prevent mapping 8362 * lists from changing while we're traversing them. 8363 */ 8364 pgcnt_t 8365 ism_tsb_entries(sfmmu_t *sfmmup, int szc) 8366 { 8367 ism_blk_t *ism_blkp = sfmmup->sfmmu_iblk; 8368 ism_map_t *ism_map; 8369 pgcnt_t npgs = 0; 8370 pgcnt_t npgs_scd = 0; 8371 int j; 8372 sf_scd_t *scdp; 8373 uchar_t rid; 8374 8375 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)); 8376 scdp = sfmmup->sfmmu_scdp; 8377 8378 for (; ism_blkp != NULL; ism_blkp = ism_blkp->iblk_next) { 8379 ism_map = ism_blkp->iblk_maps; 8380 for (j = 0; ism_map[j].imap_ismhat && j < ISM_MAP_SLOTS; j++) { 8381 rid = ism_map[j].imap_rid; 8382 ASSERT(rid == SFMMU_INVALID_ISMRID || 8383 rid < sfmmup->sfmmu_srdp->srd_next_ismrid); 8384 8385 if (scdp != NULL && rid != SFMMU_INVALID_ISMRID && 8386 SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid)) { 8387 /* ISM is in sfmmup's SCD */ 8388 npgs_scd += 8389 ism_map[j].imap_ismhat->sfmmu_ttecnt[szc]; 8390 } else { 8391 /* ISMs is not in SCD */ 8392 npgs += 8393 ism_map[j].imap_ismhat->sfmmu_ttecnt[szc]; 8394 } 8395 } 8396 } 8397 sfmmup->sfmmu_ismttecnt[szc] = npgs; 8398 sfmmup->sfmmu_scdismttecnt[szc] = npgs_scd; 8399 return (npgs); 8400 } 8401 8402 /* 8403 * Yield the memory claim requirement for an address space. 8404 * 8405 * This is currently implemented as the number of bytes that have active 8406 * hardware translations that have page structures. Therefore, it can 8407 * underestimate the traditional resident set size, eg, if the 8408 * physical page is present and the hardware translation is missing; 8409 * and it can overestimate the rss, eg, if there are active 8410 * translations to a frame buffer with page structs. 8411 * Also, it does not take sharing into account. 8412 * 8413 * Note that we don't acquire locks here since this function is most often 8414 * called from the clock thread. 8415 */ 8416 size_t 8417 hat_get_mapped_size(struct hat *hat) 8418 { 8419 size_t assize = 0; 8420 int i; 8421 8422 if (hat == NULL) 8423 return (0); 8424 8425 ASSERT(hat->sfmmu_xhat_provider == NULL); 8426 8427 for (i = 0; i < mmu_page_sizes; i++) 8428 assize += ((pgcnt_t)hat->sfmmu_ttecnt[i] + 8429 (pgcnt_t)hat->sfmmu_scdrttecnt[i]) * TTEBYTES(i); 8430 8431 if (hat->sfmmu_iblk == NULL) 8432 return (assize); 8433 8434 for (i = 0; i < mmu_page_sizes; i++) 8435 assize += ((pgcnt_t)hat->sfmmu_ismttecnt[i] + 8436 (pgcnt_t)hat->sfmmu_scdismttecnt[i]) * TTEBYTES(i); 8437 8438 return (assize); 8439 } 8440 8441 int 8442 hat_stats_enable(struct hat *hat) 8443 { 8444 hatlock_t *hatlockp; 8445 8446 ASSERT(hat->sfmmu_xhat_provider == NULL); 8447 8448 hatlockp = sfmmu_hat_enter(hat); 8449 hat->sfmmu_rmstat++; 8450 sfmmu_hat_exit(hatlockp); 8451 return (1); 8452 } 8453 8454 void 8455 hat_stats_disable(struct hat *hat) 8456 { 8457 hatlock_t *hatlockp; 8458 8459 ASSERT(hat->sfmmu_xhat_provider == NULL); 8460 8461 hatlockp = sfmmu_hat_enter(hat); 8462 hat->sfmmu_rmstat--; 8463 sfmmu_hat_exit(hatlockp); 8464 } 8465 8466 /* 8467 * Routines for entering or removing ourselves from the 8468 * ism_hat's mapping list. This is used for both private and 8469 * SCD hats. 8470 */ 8471 static void 8472 iment_add(struct ism_ment *iment, struct hat *ism_hat) 8473 { 8474 ASSERT(MUTEX_HELD(&ism_mlist_lock)); 8475 8476 iment->iment_prev = NULL; 8477 iment->iment_next = ism_hat->sfmmu_iment; 8478 if (ism_hat->sfmmu_iment) { 8479 ism_hat->sfmmu_iment->iment_prev = iment; 8480 } 8481 ism_hat->sfmmu_iment = iment; 8482 } 8483 8484 static void 8485 iment_sub(struct ism_ment *iment, struct hat *ism_hat) 8486 { 8487 ASSERT(MUTEX_HELD(&ism_mlist_lock)); 8488 8489 if (ism_hat->sfmmu_iment == NULL) { 8490 panic("ism map entry remove - no entries"); 8491 } 8492 8493 if (iment->iment_prev) { 8494 ASSERT(ism_hat->sfmmu_iment != iment); 8495 iment->iment_prev->iment_next = iment->iment_next; 8496 } else { 8497 ASSERT(ism_hat->sfmmu_iment == iment); 8498 ism_hat->sfmmu_iment = iment->iment_next; 8499 } 8500 8501 if (iment->iment_next) { 8502 iment->iment_next->iment_prev = iment->iment_prev; 8503 } 8504 8505 /* 8506 * zero out the entry 8507 */ 8508 iment->iment_next = NULL; 8509 iment->iment_prev = NULL; 8510 iment->iment_hat = NULL; 8511 } 8512 8513 /* 8514 * Hat_share()/unshare() return an (non-zero) error 8515 * when saddr and daddr are not properly aligned. 8516 * 8517 * The top level mapping element determines the alignment 8518 * requirement for saddr and daddr, depending on different 8519 * architectures. 8520 * 8521 * When hat_share()/unshare() are not supported, 8522 * HATOP_SHARE()/UNSHARE() return 0 8523 */ 8524 int 8525 hat_share(struct hat *sfmmup, caddr_t addr, 8526 struct hat *ism_hatid, caddr_t sptaddr, size_t len, uint_t ismszc) 8527 { 8528 ism_blk_t *ism_blkp; 8529 ism_blk_t *new_iblk; 8530 ism_map_t *ism_map; 8531 ism_ment_t *ism_ment; 8532 int i, added; 8533 hatlock_t *hatlockp; 8534 int reload_mmu = 0; 8535 uint_t ismshift = page_get_shift(ismszc); 8536 size_t ismpgsz = page_get_pagesize(ismszc); 8537 uint_t ismmask = (uint_t)ismpgsz - 1; 8538 size_t sh_size = ISM_SHIFT(ismshift, len); 8539 ushort_t ismhatflag; 8540 hat_region_cookie_t rcookie; 8541 sf_scd_t *old_scdp; 8542 8543 #ifdef DEBUG 8544 caddr_t eaddr = addr + len; 8545 #endif /* DEBUG */ 8546 8547 ASSERT(ism_hatid != NULL && sfmmup != NULL); 8548 ASSERT(sptaddr == ISMID_STARTADDR); 8549 /* 8550 * Check the alignment. 8551 */ 8552 if (!ISM_ALIGNED(ismshift, addr) || !ISM_ALIGNED(ismshift, sptaddr)) 8553 return (EINVAL); 8554 8555 /* 8556 * Check size alignment. 8557 */ 8558 if (!ISM_ALIGNED(ismshift, len)) 8559 return (EINVAL); 8560 8561 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 8562 8563 /* 8564 * Allocate ism_ment for the ism_hat's mapping list, and an 8565 * ism map blk in case we need one. We must do our 8566 * allocations before acquiring locks to prevent a deadlock 8567 * in the kmem allocator on the mapping list lock. 8568 */ 8569 new_iblk = kmem_cache_alloc(ism_blk_cache, KM_SLEEP); 8570 ism_ment = kmem_cache_alloc(ism_ment_cache, KM_SLEEP); 8571 8572 /* 8573 * Serialize ISM mappings with the ISM busy flag, and also the 8574 * trap handlers. 8575 */ 8576 sfmmu_ismhat_enter(sfmmup, 0); 8577 8578 /* 8579 * Allocate an ism map blk if necessary. 8580 */ 8581 if (sfmmup->sfmmu_iblk == NULL) { 8582 sfmmup->sfmmu_iblk = new_iblk; 8583 bzero(new_iblk, sizeof (*new_iblk)); 8584 new_iblk->iblk_nextpa = (uint64_t)-1; 8585 membar_stst(); /* make sure next ptr visible to all CPUs */ 8586 sfmmup->sfmmu_ismblkpa = va_to_pa((caddr_t)new_iblk); 8587 reload_mmu = 1; 8588 new_iblk = NULL; 8589 } 8590 8591 #ifdef DEBUG 8592 /* 8593 * Make sure mapping does not already exist. 8594 */ 8595 ism_blkp = sfmmup->sfmmu_iblk; 8596 while (ism_blkp != NULL) { 8597 ism_map = ism_blkp->iblk_maps; 8598 for (i = 0; i < ISM_MAP_SLOTS && ism_map[i].imap_ismhat; i++) { 8599 if ((addr >= ism_start(ism_map[i]) && 8600 addr < ism_end(ism_map[i])) || 8601 eaddr > ism_start(ism_map[i]) && 8602 eaddr <= ism_end(ism_map[i])) { 8603 panic("sfmmu_share: Already mapped!"); 8604 } 8605 } 8606 ism_blkp = ism_blkp->iblk_next; 8607 } 8608 #endif /* DEBUG */ 8609 8610 ASSERT(ismszc >= TTE4M); 8611 if (ismszc == TTE4M) { 8612 ismhatflag = HAT_4M_FLAG; 8613 } else if (ismszc == TTE32M) { 8614 ismhatflag = HAT_32M_FLAG; 8615 } else if (ismszc == TTE256M) { 8616 ismhatflag = HAT_256M_FLAG; 8617 } 8618 /* 8619 * Add mapping to first available mapping slot. 8620 */ 8621 ism_blkp = sfmmup->sfmmu_iblk; 8622 added = 0; 8623 while (!added) { 8624 ism_map = ism_blkp->iblk_maps; 8625 for (i = 0; i < ISM_MAP_SLOTS; i++) { 8626 if (ism_map[i].imap_ismhat == NULL) { 8627 8628 ism_map[i].imap_ismhat = ism_hatid; 8629 ism_map[i].imap_vb_shift = (uchar_t)ismshift; 8630 ism_map[i].imap_rid = SFMMU_INVALID_ISMRID; 8631 ism_map[i].imap_hatflags = ismhatflag; 8632 ism_map[i].imap_sz_mask = ismmask; 8633 /* 8634 * imap_seg is checked in ISM_CHECK to see if 8635 * non-NULL, then other info assumed valid. 8636 */ 8637 membar_stst(); 8638 ism_map[i].imap_seg = (uintptr_t)addr | sh_size; 8639 ism_map[i].imap_ment = ism_ment; 8640 8641 /* 8642 * Now add ourselves to the ism_hat's 8643 * mapping list. 8644 */ 8645 ism_ment->iment_hat = sfmmup; 8646 ism_ment->iment_base_va = addr; 8647 ism_hatid->sfmmu_ismhat = 1; 8648 mutex_enter(&ism_mlist_lock); 8649 iment_add(ism_ment, ism_hatid); 8650 mutex_exit(&ism_mlist_lock); 8651 added = 1; 8652 break; 8653 } 8654 } 8655 if (!added && ism_blkp->iblk_next == NULL) { 8656 ism_blkp->iblk_next = new_iblk; 8657 new_iblk = NULL; 8658 bzero(ism_blkp->iblk_next, 8659 sizeof (*ism_blkp->iblk_next)); 8660 ism_blkp->iblk_next->iblk_nextpa = (uint64_t)-1; 8661 membar_stst(); 8662 ism_blkp->iblk_nextpa = 8663 va_to_pa((caddr_t)ism_blkp->iblk_next); 8664 } 8665 ism_blkp = ism_blkp->iblk_next; 8666 } 8667 8668 /* 8669 * After calling hat_join_region, sfmmup may join a new SCD or 8670 * move from the old scd to a new scd, in which case, we want to 8671 * shrink the sfmmup's private tsb size, i.e., pass shrink to 8672 * sfmmu_check_page_sizes at the end of this routine. 8673 */ 8674 old_scdp = sfmmup->sfmmu_scdp; 8675 8676 rcookie = hat_join_region(sfmmup, addr, len, (void *)ism_hatid, 0, 8677 PROT_ALL, ismszc, NULL, HAT_REGION_ISM); 8678 if (rcookie != HAT_INVALID_REGION_COOKIE) { 8679 ism_map[i].imap_rid = (uchar_t)((uint64_t)rcookie); 8680 } 8681 /* 8682 * Update our counters for this sfmmup's ism mappings. 8683 */ 8684 for (i = 0; i <= ismszc; i++) { 8685 if (!(disable_ism_large_pages & (1 << i))) 8686 (void) ism_tsb_entries(sfmmup, i); 8687 } 8688 8689 /* 8690 * For ISM and DISM we do not support 512K pages, so we only only 8691 * search the 4M and 8K/64K hashes for 4 pagesize cpus, and search the 8692 * 256M or 32M, and 4M and 8K/64K hashes for 6 pagesize cpus. 8693 * 8694 * Need to set 32M/256M ISM flags to make sure 8695 * sfmmu_check_page_sizes() enables them on Panther. 8696 */ 8697 ASSERT((disable_ism_large_pages & (1 << TTE512K)) != 0); 8698 8699 switch (ismszc) { 8700 case TTE256M: 8701 if (!SFMMU_FLAGS_ISSET(sfmmup, HAT_256M_ISM)) { 8702 hatlockp = sfmmu_hat_enter(sfmmup); 8703 SFMMU_FLAGS_SET(sfmmup, HAT_256M_ISM); 8704 sfmmu_hat_exit(hatlockp); 8705 } 8706 break; 8707 case TTE32M: 8708 if (!SFMMU_FLAGS_ISSET(sfmmup, HAT_32M_ISM)) { 8709 hatlockp = sfmmu_hat_enter(sfmmup); 8710 SFMMU_FLAGS_SET(sfmmup, HAT_32M_ISM); 8711 sfmmu_hat_exit(hatlockp); 8712 } 8713 break; 8714 default: 8715 break; 8716 } 8717 8718 /* 8719 * If we updated the ismblkpa for this HAT we must make 8720 * sure all CPUs running this process reload their tsbmiss area. 8721 * Otherwise they will fail to load the mappings in the tsbmiss 8722 * handler and will loop calling pagefault(). 8723 */ 8724 if (reload_mmu) { 8725 hatlockp = sfmmu_hat_enter(sfmmup); 8726 sfmmu_sync_mmustate(sfmmup); 8727 sfmmu_hat_exit(hatlockp); 8728 } 8729 8730 sfmmu_ismhat_exit(sfmmup, 0); 8731 8732 /* 8733 * Free up ismblk if we didn't use it. 8734 */ 8735 if (new_iblk != NULL) 8736 kmem_cache_free(ism_blk_cache, new_iblk); 8737 8738 /* 8739 * Check TSB and TLB page sizes. 8740 */ 8741 if (sfmmup->sfmmu_scdp != NULL && old_scdp != sfmmup->sfmmu_scdp) { 8742 sfmmu_check_page_sizes(sfmmup, 0); 8743 } else { 8744 sfmmu_check_page_sizes(sfmmup, 1); 8745 } 8746 return (0); 8747 } 8748 8749 /* 8750 * hat_unshare removes exactly one ism_map from 8751 * this process's as. It expects multiple calls 8752 * to hat_unshare for multiple shm segments. 8753 */ 8754 void 8755 hat_unshare(struct hat *sfmmup, caddr_t addr, size_t len, uint_t ismszc) 8756 { 8757 ism_map_t *ism_map; 8758 ism_ment_t *free_ment = NULL; 8759 ism_blk_t *ism_blkp; 8760 struct hat *ism_hatid; 8761 int found, i; 8762 hatlock_t *hatlockp; 8763 struct tsb_info *tsbinfo; 8764 uint_t ismshift = page_get_shift(ismszc); 8765 size_t sh_size = ISM_SHIFT(ismshift, len); 8766 uchar_t ism_rid; 8767 sf_scd_t *old_scdp; 8768 8769 ASSERT(ISM_ALIGNED(ismshift, addr)); 8770 ASSERT(ISM_ALIGNED(ismshift, len)); 8771 ASSERT(sfmmup != NULL); 8772 ASSERT(sfmmup != ksfmmup); 8773 8774 if (sfmmup->sfmmu_xhat_provider) { 8775 XHAT_UNSHARE(sfmmup, addr, len); 8776 return; 8777 } else { 8778 /* 8779 * This must be a CPU HAT. If the address space has 8780 * XHATs attached, inform all XHATs that ISM segment 8781 * is going away 8782 */ 8783 ASSERT(sfmmup->sfmmu_as != NULL); 8784 if (sfmmup->sfmmu_as->a_xhat != NULL) 8785 xhat_unshare_all(sfmmup->sfmmu_as, addr, len); 8786 } 8787 8788 /* 8789 * Make sure that during the entire time ISM mappings are removed, 8790 * the trap handlers serialize behind us, and that no one else 8791 * can be mucking with ISM mappings. This also lets us get away 8792 * with not doing expensive cross calls to flush the TLB -- we 8793 * just discard the context, flush the entire TSB, and call it 8794 * a day. 8795 */ 8796 sfmmu_ismhat_enter(sfmmup, 0); 8797 8798 /* 8799 * Remove the mapping. 8800 * 8801 * We can't have any holes in the ism map. 8802 * The tsb miss code while searching the ism map will 8803 * stop on an empty map slot. So we must move 8804 * everyone past the hole up 1 if any. 8805 * 8806 * Also empty ism map blks are not freed until the 8807 * process exits. This is to prevent a MT race condition 8808 * between sfmmu_unshare() and sfmmu_tsbmiss_exception(). 8809 */ 8810 found = 0; 8811 ism_blkp = sfmmup->sfmmu_iblk; 8812 while (!found && ism_blkp != NULL) { 8813 ism_map = ism_blkp->iblk_maps; 8814 for (i = 0; i < ISM_MAP_SLOTS; i++) { 8815 if (addr == ism_start(ism_map[i]) && 8816 sh_size == (size_t)(ism_size(ism_map[i]))) { 8817 found = 1; 8818 break; 8819 } 8820 } 8821 if (!found) 8822 ism_blkp = ism_blkp->iblk_next; 8823 } 8824 8825 if (found) { 8826 ism_hatid = ism_map[i].imap_ismhat; 8827 ism_rid = ism_map[i].imap_rid; 8828 ASSERT(ism_hatid != NULL); 8829 ASSERT(ism_hatid->sfmmu_ismhat == 1); 8830 8831 /* 8832 * After hat_leave_region, the sfmmup may leave SCD, 8833 * in which case, we want to grow the private tsb size when 8834 * calling sfmmu_check_page_sizes at the end of the routine. 8835 */ 8836 old_scdp = sfmmup->sfmmu_scdp; 8837 /* 8838 * Then remove ourselves from the region. 8839 */ 8840 if (ism_rid != SFMMU_INVALID_ISMRID) { 8841 hat_leave_region(sfmmup, (void *)((uint64_t)ism_rid), 8842 HAT_REGION_ISM); 8843 } 8844 8845 /* 8846 * And now guarantee that any other cpu 8847 * that tries to process an ISM miss 8848 * will go to tl=0. 8849 */ 8850 hatlockp = sfmmu_hat_enter(sfmmup); 8851 sfmmu_invalidate_ctx(sfmmup); 8852 sfmmu_hat_exit(hatlockp); 8853 8854 /* 8855 * Remove ourselves from the ism mapping list. 8856 */ 8857 mutex_enter(&ism_mlist_lock); 8858 iment_sub(ism_map[i].imap_ment, ism_hatid); 8859 mutex_exit(&ism_mlist_lock); 8860 free_ment = ism_map[i].imap_ment; 8861 8862 /* 8863 * We delete the ism map by copying 8864 * the next map over the current one. 8865 * We will take the next one in the maps 8866 * array or from the next ism_blk. 8867 */ 8868 while (ism_blkp != NULL) { 8869 ism_map = ism_blkp->iblk_maps; 8870 while (i < (ISM_MAP_SLOTS - 1)) { 8871 ism_map[i] = ism_map[i + 1]; 8872 i++; 8873 } 8874 /* i == (ISM_MAP_SLOTS - 1) */ 8875 ism_blkp = ism_blkp->iblk_next; 8876 if (ism_blkp != NULL) { 8877 ism_map[i] = ism_blkp->iblk_maps[0]; 8878 i = 0; 8879 } else { 8880 ism_map[i].imap_seg = 0; 8881 ism_map[i].imap_vb_shift = 0; 8882 ism_map[i].imap_rid = SFMMU_INVALID_ISMRID; 8883 ism_map[i].imap_hatflags = 0; 8884 ism_map[i].imap_sz_mask = 0; 8885 ism_map[i].imap_ismhat = NULL; 8886 ism_map[i].imap_ment = NULL; 8887 } 8888 } 8889 8890 /* 8891 * Now flush entire TSB for the process, since 8892 * demapping page by page can be too expensive. 8893 * We don't have to flush the TLB here anymore 8894 * since we switch to a new TLB ctx instead. 8895 * Also, there is no need to flush if the process 8896 * is exiting since the TSB will be freed later. 8897 */ 8898 if (!sfmmup->sfmmu_free) { 8899 hatlockp = sfmmu_hat_enter(sfmmup); 8900 for (tsbinfo = sfmmup->sfmmu_tsb; tsbinfo != NULL; 8901 tsbinfo = tsbinfo->tsb_next) { 8902 if (tsbinfo->tsb_flags & TSB_SWAPPED) 8903 continue; 8904 if (tsbinfo->tsb_flags & TSB_RELOC_FLAG) { 8905 tsbinfo->tsb_flags |= 8906 TSB_FLUSH_NEEDED; 8907 continue; 8908 } 8909 8910 sfmmu_inv_tsb(tsbinfo->tsb_va, 8911 TSB_BYTES(tsbinfo->tsb_szc)); 8912 } 8913 sfmmu_hat_exit(hatlockp); 8914 } 8915 } 8916 8917 /* 8918 * Update our counters for this sfmmup's ism mappings. 8919 */ 8920 for (i = 0; i <= ismszc; i++) { 8921 if (!(disable_ism_large_pages & (1 << i))) 8922 (void) ism_tsb_entries(sfmmup, i); 8923 } 8924 8925 sfmmu_ismhat_exit(sfmmup, 0); 8926 8927 /* 8928 * We must do our freeing here after dropping locks 8929 * to prevent a deadlock in the kmem allocator on the 8930 * mapping list lock. 8931 */ 8932 if (free_ment != NULL) 8933 kmem_cache_free(ism_ment_cache, free_ment); 8934 8935 /* 8936 * Check TSB and TLB page sizes if the process isn't exiting. 8937 */ 8938 if (!sfmmup->sfmmu_free) { 8939 if (found && old_scdp != NULL && sfmmup->sfmmu_scdp == NULL) { 8940 sfmmu_check_page_sizes(sfmmup, 1); 8941 } else { 8942 sfmmu_check_page_sizes(sfmmup, 0); 8943 } 8944 } 8945 } 8946 8947 /* ARGSUSED */ 8948 static int 8949 sfmmu_idcache_constructor(void *buf, void *cdrarg, int kmflags) 8950 { 8951 /* void *buf is sfmmu_t pointer */ 8952 bzero(buf, sizeof (sfmmu_t)); 8953 8954 return (0); 8955 } 8956 8957 /* ARGSUSED */ 8958 static void 8959 sfmmu_idcache_destructor(void *buf, void *cdrarg) 8960 { 8961 /* void *buf is sfmmu_t pointer */ 8962 } 8963 8964 /* 8965 * setup kmem hmeblks by bzeroing all members and initializing the nextpa 8966 * field to be the pa of this hmeblk 8967 */ 8968 /* ARGSUSED */ 8969 static int 8970 sfmmu_hblkcache_constructor(void *buf, void *cdrarg, int kmflags) 8971 { 8972 struct hme_blk *hmeblkp; 8973 8974 bzero(buf, (size_t)cdrarg); 8975 hmeblkp = (struct hme_blk *)buf; 8976 hmeblkp->hblk_nextpa = va_to_pa((caddr_t)hmeblkp); 8977 8978 #ifdef HBLK_TRACE 8979 mutex_init(&hmeblkp->hblk_audit_lock, NULL, MUTEX_DEFAULT, NULL); 8980 #endif /* HBLK_TRACE */ 8981 8982 return (0); 8983 } 8984 8985 /* ARGSUSED */ 8986 static void 8987 sfmmu_hblkcache_destructor(void *buf, void *cdrarg) 8988 { 8989 8990 #ifdef HBLK_TRACE 8991 8992 struct hme_blk *hmeblkp; 8993 8994 hmeblkp = (struct hme_blk *)buf; 8995 mutex_destroy(&hmeblkp->hblk_audit_lock); 8996 8997 #endif /* HBLK_TRACE */ 8998 } 8999 9000 #define SFMMU_CACHE_RECLAIM_SCAN_RATIO 8 9001 static int sfmmu_cache_reclaim_scan_ratio = SFMMU_CACHE_RECLAIM_SCAN_RATIO; 9002 /* 9003 * The kmem allocator will callback into our reclaim routine when the system 9004 * is running low in memory. We traverse the hash and free up all unused but 9005 * still cached hme_blks. We also traverse the free list and free them up 9006 * as well. 9007 */ 9008 /*ARGSUSED*/ 9009 static void 9010 sfmmu_hblkcache_reclaim(void *cdrarg) 9011 { 9012 int i; 9013 uint64_t hblkpa, prevpa, nx_pa; 9014 struct hmehash_bucket *hmebp; 9015 struct hme_blk *hmeblkp, *nx_hblk, *pr_hblk = NULL; 9016 static struct hmehash_bucket *uhmehash_reclaim_hand; 9017 static struct hmehash_bucket *khmehash_reclaim_hand; 9018 struct hme_blk *list = NULL; 9019 9020 hmebp = uhmehash_reclaim_hand; 9021 if (hmebp == NULL || hmebp > &uhme_hash[UHMEHASH_SZ]) 9022 uhmehash_reclaim_hand = hmebp = uhme_hash; 9023 uhmehash_reclaim_hand += UHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; 9024 9025 for (i = UHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; i; i--) { 9026 if (SFMMU_HASH_LOCK_TRYENTER(hmebp) != 0) { 9027 hmeblkp = hmebp->hmeblkp; 9028 hblkpa = hmebp->hmeh_nextpa; 9029 prevpa = 0; 9030 pr_hblk = NULL; 9031 while (hmeblkp) { 9032 nx_hblk = hmeblkp->hblk_next; 9033 nx_pa = hmeblkp->hblk_nextpa; 9034 if (!hmeblkp->hblk_vcnt && 9035 !hmeblkp->hblk_hmecnt) { 9036 sfmmu_hblk_hash_rm(hmebp, hmeblkp, 9037 prevpa, pr_hblk); 9038 sfmmu_hblk_free(hmebp, hmeblkp, 9039 hblkpa, &list); 9040 } else { 9041 pr_hblk = hmeblkp; 9042 prevpa = hblkpa; 9043 } 9044 hmeblkp = nx_hblk; 9045 hblkpa = nx_pa; 9046 } 9047 SFMMU_HASH_UNLOCK(hmebp); 9048 } 9049 if (hmebp++ == &uhme_hash[UHMEHASH_SZ]) 9050 hmebp = uhme_hash; 9051 } 9052 9053 hmebp = khmehash_reclaim_hand; 9054 if (hmebp == NULL || hmebp > &khme_hash[KHMEHASH_SZ]) 9055 khmehash_reclaim_hand = hmebp = khme_hash; 9056 khmehash_reclaim_hand += KHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; 9057 9058 for (i = KHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; i; i--) { 9059 if (SFMMU_HASH_LOCK_TRYENTER(hmebp) != 0) { 9060 hmeblkp = hmebp->hmeblkp; 9061 hblkpa = hmebp->hmeh_nextpa; 9062 prevpa = 0; 9063 pr_hblk = NULL; 9064 while (hmeblkp) { 9065 nx_hblk = hmeblkp->hblk_next; 9066 nx_pa = hmeblkp->hblk_nextpa; 9067 if (!hmeblkp->hblk_vcnt && 9068 !hmeblkp->hblk_hmecnt) { 9069 sfmmu_hblk_hash_rm(hmebp, hmeblkp, 9070 prevpa, pr_hblk); 9071 sfmmu_hblk_free(hmebp, hmeblkp, 9072 hblkpa, &list); 9073 } else { 9074 pr_hblk = hmeblkp; 9075 prevpa = hblkpa; 9076 } 9077 hmeblkp = nx_hblk; 9078 hblkpa = nx_pa; 9079 } 9080 SFMMU_HASH_UNLOCK(hmebp); 9081 } 9082 if (hmebp++ == &khme_hash[KHMEHASH_SZ]) 9083 hmebp = khme_hash; 9084 } 9085 sfmmu_hblks_list_purge(&list); 9086 } 9087 9088 /* 9089 * sfmmu_get_ppvcolor should become a vm_machdep or hatop interface. 9090 * same goes for sfmmu_get_addrvcolor(). 9091 * 9092 * This function will return the virtual color for the specified page. The 9093 * virtual color corresponds to this page current mapping or its last mapping. 9094 * It is used by memory allocators to choose addresses with the correct 9095 * alignment so vac consistency is automatically maintained. If the page 9096 * has no color it returns -1. 9097 */ 9098 /*ARGSUSED*/ 9099 int 9100 sfmmu_get_ppvcolor(struct page *pp) 9101 { 9102 #ifdef VAC 9103 int color; 9104 9105 if (!(cache & CACHE_VAC) || PP_NEWPAGE(pp)) { 9106 return (-1); 9107 } 9108 color = PP_GET_VCOLOR(pp); 9109 ASSERT(color < mmu_btop(shm_alignment)); 9110 return (color); 9111 #else 9112 return (-1); 9113 #endif /* VAC */ 9114 } 9115 9116 /* 9117 * This function will return the desired alignment for vac consistency 9118 * (vac color) given a virtual address. If no vac is present it returns -1. 9119 */ 9120 /*ARGSUSED*/ 9121 int 9122 sfmmu_get_addrvcolor(caddr_t vaddr) 9123 { 9124 #ifdef VAC 9125 if (cache & CACHE_VAC) { 9126 return (addr_to_vcolor(vaddr)); 9127 } else { 9128 return (-1); 9129 } 9130 #else 9131 return (-1); 9132 #endif /* VAC */ 9133 } 9134 9135 #ifdef VAC 9136 /* 9137 * Check for conflicts. 9138 * A conflict exists if the new and existent mappings do not match in 9139 * their "shm_alignment fields. If conflicts exist, the existant mappings 9140 * are flushed unless one of them is locked. If one of them is locked, then 9141 * the mappings are flushed and converted to non-cacheable mappings. 9142 */ 9143 static void 9144 sfmmu_vac_conflict(struct hat *hat, caddr_t addr, page_t *pp) 9145 { 9146 struct hat *tmphat; 9147 struct sf_hment *sfhmep, *tmphme = NULL; 9148 struct hme_blk *hmeblkp; 9149 int vcolor; 9150 tte_t tte; 9151 9152 ASSERT(sfmmu_mlist_held(pp)); 9153 ASSERT(!PP_ISNC(pp)); /* page better be cacheable */ 9154 9155 vcolor = addr_to_vcolor(addr); 9156 if (PP_NEWPAGE(pp)) { 9157 PP_SET_VCOLOR(pp, vcolor); 9158 return; 9159 } 9160 9161 if (PP_GET_VCOLOR(pp) == vcolor) { 9162 return; 9163 } 9164 9165 if (!PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp)) { 9166 /* 9167 * Previous user of page had a different color 9168 * but since there are no current users 9169 * we just flush the cache and change the color. 9170 */ 9171 SFMMU_STAT(sf_pgcolor_conflict); 9172 sfmmu_cache_flush(pp->p_pagenum, PP_GET_VCOLOR(pp)); 9173 PP_SET_VCOLOR(pp, vcolor); 9174 return; 9175 } 9176 9177 /* 9178 * If we get here we have a vac conflict with a current 9179 * mapping. VAC conflict policy is as follows. 9180 * - The default is to unload the other mappings unless: 9181 * - If we have a large mapping we uncache the page. 9182 * We need to uncache the rest of the large page too. 9183 * - If any of the mappings are locked we uncache the page. 9184 * - If the requested mapping is inconsistent 9185 * with another mapping and that mapping 9186 * is in the same address space we have to 9187 * make it non-cached. The default thing 9188 * to do is unload the inconsistent mapping 9189 * but if they are in the same address space 9190 * we run the risk of unmapping the pc or the 9191 * stack which we will use as we return to the user, 9192 * in which case we can then fault on the thing 9193 * we just unloaded and get into an infinite loop. 9194 */ 9195 if (PP_ISMAPPED_LARGE(pp)) { 9196 int sz; 9197 9198 /* 9199 * Existing mapping is for big pages. We don't unload 9200 * existing big mappings to satisfy new mappings. 9201 * Always convert all mappings to TNC. 9202 */ 9203 sz = fnd_mapping_sz(pp); 9204 pp = PP_GROUPLEADER(pp, sz); 9205 SFMMU_STAT_ADD(sf_uncache_conflict, TTEPAGES(sz)); 9206 sfmmu_page_cache_array(pp, HAT_TMPNC, CACHE_FLUSH, 9207 TTEPAGES(sz)); 9208 9209 return; 9210 } 9211 9212 /* 9213 * check if any mapping is in same as or if it is locked 9214 * since in that case we need to uncache. 9215 */ 9216 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = tmphme) { 9217 tmphme = sfhmep->hme_next; 9218 if (IS_PAHME(sfhmep)) 9219 continue; 9220 hmeblkp = sfmmu_hmetohblk(sfhmep); 9221 if (hmeblkp->hblk_xhat_bit) 9222 continue; 9223 tmphat = hblktosfmmu(hmeblkp); 9224 sfmmu_copytte(&sfhmep->hme_tte, &tte); 9225 ASSERT(TTE_IS_VALID(&tte)); 9226 if (hmeblkp->hblk_shared || tmphat == hat || 9227 hmeblkp->hblk_lckcnt) { 9228 /* 9229 * We have an uncache conflict 9230 */ 9231 SFMMU_STAT(sf_uncache_conflict); 9232 sfmmu_page_cache_array(pp, HAT_TMPNC, CACHE_FLUSH, 1); 9233 return; 9234 } 9235 } 9236 9237 /* 9238 * We have an unload conflict 9239 * We have already checked for LARGE mappings, therefore 9240 * the remaining mapping(s) must be TTE8K. 9241 */ 9242 SFMMU_STAT(sf_unload_conflict); 9243 9244 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = tmphme) { 9245 tmphme = sfhmep->hme_next; 9246 if (IS_PAHME(sfhmep)) 9247 continue; 9248 hmeblkp = sfmmu_hmetohblk(sfhmep); 9249 if (hmeblkp->hblk_xhat_bit) 9250 continue; 9251 ASSERT(!hmeblkp->hblk_shared); 9252 (void) sfmmu_pageunload(pp, sfhmep, TTE8K); 9253 } 9254 9255 if (PP_ISMAPPED_KPM(pp)) 9256 sfmmu_kpm_vac_unload(pp, addr); 9257 9258 /* 9259 * Unloads only do TLB flushes so we need to flush the 9260 * cache here. 9261 */ 9262 sfmmu_cache_flush(pp->p_pagenum, PP_GET_VCOLOR(pp)); 9263 PP_SET_VCOLOR(pp, vcolor); 9264 } 9265 9266 /* 9267 * Whenever a mapping is unloaded and the page is in TNC state, 9268 * we see if the page can be made cacheable again. 'pp' is 9269 * the page that we just unloaded a mapping from, the size 9270 * of mapping that was unloaded is 'ottesz'. 9271 * Remark: 9272 * The recache policy for mpss pages can leave a performance problem 9273 * under the following circumstances: 9274 * . A large page in uncached mode has just been unmapped. 9275 * . All constituent pages are TNC due to a conflicting small mapping. 9276 * . There are many other, non conflicting, small mappings around for 9277 * a lot of the constituent pages. 9278 * . We're called w/ the "old" groupleader page and the old ottesz, 9279 * but this is irrelevant, since we're no more "PP_ISMAPPED_LARGE", so 9280 * we end up w/ TTE8K or npages == 1. 9281 * . We call tst_tnc w/ the old groupleader only, and if there is no 9282 * conflict, we re-cache only this page. 9283 * . All other small mappings are not checked and will be left in TNC mode. 9284 * The problem is not very serious because: 9285 * . mpss is actually only defined for heap and stack, so the probability 9286 * is not very high that a large page mapping exists in parallel to a small 9287 * one (this is possible, but seems to be bad programming style in the 9288 * appl). 9289 * . The problem gets a little bit more serious, when those TNC pages 9290 * have to be mapped into kernel space, e.g. for networking. 9291 * . When VAC alias conflicts occur in applications, this is regarded 9292 * as an application bug. So if kstat's show them, the appl should 9293 * be changed anyway. 9294 */ 9295 void 9296 conv_tnc(page_t *pp, int ottesz) 9297 { 9298 int cursz, dosz; 9299 pgcnt_t curnpgs, dopgs; 9300 pgcnt_t pg64k; 9301 page_t *pp2; 9302 9303 /* 9304 * Determine how big a range we check for TNC and find 9305 * leader page. cursz is the size of the biggest 9306 * mapping that still exist on 'pp'. 9307 */ 9308 if (PP_ISMAPPED_LARGE(pp)) { 9309 cursz = fnd_mapping_sz(pp); 9310 } else { 9311 cursz = TTE8K; 9312 } 9313 9314 if (ottesz >= cursz) { 9315 dosz = ottesz; 9316 pp2 = pp; 9317 } else { 9318 dosz = cursz; 9319 pp2 = PP_GROUPLEADER(pp, dosz); 9320 } 9321 9322 pg64k = TTEPAGES(TTE64K); 9323 dopgs = TTEPAGES(dosz); 9324 9325 ASSERT(dopgs == 1 || ((dopgs & (pg64k - 1)) == 0)); 9326 9327 while (dopgs != 0) { 9328 curnpgs = TTEPAGES(cursz); 9329 if (tst_tnc(pp2, curnpgs)) { 9330 SFMMU_STAT_ADD(sf_recache, curnpgs); 9331 sfmmu_page_cache_array(pp2, HAT_CACHE, CACHE_NO_FLUSH, 9332 curnpgs); 9333 } 9334 9335 ASSERT(dopgs >= curnpgs); 9336 dopgs -= curnpgs; 9337 9338 if (dopgs == 0) { 9339 break; 9340 } 9341 9342 pp2 = PP_PAGENEXT_N(pp2, curnpgs); 9343 if (((dopgs & (pg64k - 1)) == 0) && PP_ISMAPPED_LARGE(pp2)) { 9344 cursz = fnd_mapping_sz(pp2); 9345 } else { 9346 cursz = TTE8K; 9347 } 9348 } 9349 } 9350 9351 /* 9352 * Returns 1 if page(s) can be converted from TNC to cacheable setting, 9353 * returns 0 otherwise. Note that oaddr argument is valid for only 9354 * 8k pages. 9355 */ 9356 int 9357 tst_tnc(page_t *pp, pgcnt_t npages) 9358 { 9359 struct sf_hment *sfhme; 9360 struct hme_blk *hmeblkp; 9361 tte_t tte; 9362 caddr_t vaddr; 9363 int clr_valid = 0; 9364 int color, color1, bcolor; 9365 int i, ncolors; 9366 9367 ASSERT(pp != NULL); 9368 ASSERT(!(cache & CACHE_WRITEBACK)); 9369 9370 if (npages > 1) { 9371 ncolors = CACHE_NUM_COLOR; 9372 } 9373 9374 for (i = 0; i < npages; i++) { 9375 ASSERT(sfmmu_mlist_held(pp)); 9376 ASSERT(PP_ISTNC(pp)); 9377 ASSERT(PP_GET_VCOLOR(pp) == NO_VCOLOR); 9378 9379 if (PP_ISPNC(pp)) { 9380 return (0); 9381 } 9382 9383 clr_valid = 0; 9384 if (PP_ISMAPPED_KPM(pp)) { 9385 caddr_t kpmvaddr; 9386 9387 ASSERT(kpm_enable); 9388 kpmvaddr = hat_kpm_page2va(pp, 1); 9389 ASSERT(!(npages > 1 && IS_KPM_ALIAS_RANGE(kpmvaddr))); 9390 color1 = addr_to_vcolor(kpmvaddr); 9391 clr_valid = 1; 9392 } 9393 9394 for (sfhme = pp->p_mapping; sfhme; sfhme = sfhme->hme_next) { 9395 if (IS_PAHME(sfhme)) 9396 continue; 9397 hmeblkp = sfmmu_hmetohblk(sfhme); 9398 if (hmeblkp->hblk_xhat_bit) 9399 continue; 9400 9401 sfmmu_copytte(&sfhme->hme_tte, &tte); 9402 ASSERT(TTE_IS_VALID(&tte)); 9403 9404 vaddr = tte_to_vaddr(hmeblkp, tte); 9405 color = addr_to_vcolor(vaddr); 9406 9407 if (npages > 1) { 9408 /* 9409 * If there is a big mapping, make sure 9410 * 8K mapping is consistent with the big 9411 * mapping. 9412 */ 9413 bcolor = i % ncolors; 9414 if (color != bcolor) { 9415 return (0); 9416 } 9417 } 9418 if (!clr_valid) { 9419 clr_valid = 1; 9420 color1 = color; 9421 } 9422 9423 if (color1 != color) { 9424 return (0); 9425 } 9426 } 9427 9428 pp = PP_PAGENEXT(pp); 9429 } 9430 9431 return (1); 9432 } 9433 9434 void 9435 sfmmu_page_cache_array(page_t *pp, int flags, int cache_flush_flag, 9436 pgcnt_t npages) 9437 { 9438 kmutex_t *pmtx; 9439 int i, ncolors, bcolor; 9440 kpm_hlk_t *kpmp; 9441 cpuset_t cpuset; 9442 9443 ASSERT(pp != NULL); 9444 ASSERT(!(cache & CACHE_WRITEBACK)); 9445 9446 kpmp = sfmmu_kpm_kpmp_enter(pp, npages); 9447 pmtx = sfmmu_page_enter(pp); 9448 9449 /* 9450 * Fast path caching single unmapped page 9451 */ 9452 if (npages == 1 && !PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp) && 9453 flags == HAT_CACHE) { 9454 PP_CLRTNC(pp); 9455 PP_CLRPNC(pp); 9456 sfmmu_page_exit(pmtx); 9457 sfmmu_kpm_kpmp_exit(kpmp); 9458 return; 9459 } 9460 9461 /* 9462 * We need to capture all cpus in order to change cacheability 9463 * because we can't allow one cpu to access the same physical 9464 * page using a cacheable and a non-cachebale mapping at the same 9465 * time. Since we may end up walking the ism mapping list 9466 * have to grab it's lock now since we can't after all the 9467 * cpus have been captured. 9468 */ 9469 sfmmu_hat_lock_all(); 9470 mutex_enter(&ism_mlist_lock); 9471 kpreempt_disable(); 9472 cpuset = cpu_ready_set; 9473 xc_attention(cpuset); 9474 9475 if (npages > 1) { 9476 /* 9477 * Make sure all colors are flushed since the 9478 * sfmmu_page_cache() only flushes one color- 9479 * it does not know big pages. 9480 */ 9481 ncolors = CACHE_NUM_COLOR; 9482 if (flags & HAT_TMPNC) { 9483 for (i = 0; i < ncolors; i++) { 9484 sfmmu_cache_flushcolor(i, pp->p_pagenum); 9485 } 9486 cache_flush_flag = CACHE_NO_FLUSH; 9487 } 9488 } 9489 9490 for (i = 0; i < npages; i++) { 9491 9492 ASSERT(sfmmu_mlist_held(pp)); 9493 9494 if (!(flags == HAT_TMPNC && PP_ISTNC(pp))) { 9495 9496 if (npages > 1) { 9497 bcolor = i % ncolors; 9498 } else { 9499 bcolor = NO_VCOLOR; 9500 } 9501 9502 sfmmu_page_cache(pp, flags, cache_flush_flag, 9503 bcolor); 9504 } 9505 9506 pp = PP_PAGENEXT(pp); 9507 } 9508 9509 xt_sync(cpuset); 9510 xc_dismissed(cpuset); 9511 mutex_exit(&ism_mlist_lock); 9512 sfmmu_hat_unlock_all(); 9513 sfmmu_page_exit(pmtx); 9514 sfmmu_kpm_kpmp_exit(kpmp); 9515 kpreempt_enable(); 9516 } 9517 9518 /* 9519 * This function changes the virtual cacheability of all mappings to a 9520 * particular page. When changing from uncache to cacheable the mappings will 9521 * only be changed if all of them have the same virtual color. 9522 * We need to flush the cache in all cpus. It is possible that 9523 * a process referenced a page as cacheable but has sinced exited 9524 * and cleared the mapping list. We still to flush it but have no 9525 * state so all cpus is the only alternative. 9526 */ 9527 static void 9528 sfmmu_page_cache(page_t *pp, int flags, int cache_flush_flag, int bcolor) 9529 { 9530 struct sf_hment *sfhme; 9531 struct hme_blk *hmeblkp; 9532 sfmmu_t *sfmmup; 9533 tte_t tte, ttemod; 9534 caddr_t vaddr; 9535 int ret, color; 9536 pfn_t pfn; 9537 9538 color = bcolor; 9539 pfn = pp->p_pagenum; 9540 9541 for (sfhme = pp->p_mapping; sfhme; sfhme = sfhme->hme_next) { 9542 9543 if (IS_PAHME(sfhme)) 9544 continue; 9545 hmeblkp = sfmmu_hmetohblk(sfhme); 9546 9547 if (hmeblkp->hblk_xhat_bit) 9548 continue; 9549 9550 sfmmu_copytte(&sfhme->hme_tte, &tte); 9551 ASSERT(TTE_IS_VALID(&tte)); 9552 vaddr = tte_to_vaddr(hmeblkp, tte); 9553 color = addr_to_vcolor(vaddr); 9554 9555 #ifdef DEBUG 9556 if ((flags & HAT_CACHE) && bcolor != NO_VCOLOR) { 9557 ASSERT(color == bcolor); 9558 } 9559 #endif 9560 9561 ASSERT(flags != HAT_TMPNC || color == PP_GET_VCOLOR(pp)); 9562 9563 ttemod = tte; 9564 if (flags & (HAT_UNCACHE | HAT_TMPNC)) { 9565 TTE_CLR_VCACHEABLE(&ttemod); 9566 } else { /* flags & HAT_CACHE */ 9567 TTE_SET_VCACHEABLE(&ttemod); 9568 } 9569 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte); 9570 if (ret < 0) { 9571 /* 9572 * Since all cpus are captured modifytte should not 9573 * fail. 9574 */ 9575 panic("sfmmu_page_cache: write to tte failed"); 9576 } 9577 9578 sfmmup = hblktosfmmu(hmeblkp); 9579 if (cache_flush_flag == CACHE_FLUSH) { 9580 /* 9581 * Flush TSBs, TLBs and caches 9582 */ 9583 if (hmeblkp->hblk_shared) { 9584 sf_srd_t *srdp = (sf_srd_t *)sfmmup; 9585 uint_t rid = hmeblkp->hblk_tag.htag_rid; 9586 sf_region_t *rgnp; 9587 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 9588 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 9589 ASSERT(srdp != NULL); 9590 rgnp = srdp->srd_hmergnp[rid]; 9591 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, 9592 srdp, rgnp, rid); 9593 (void) sfmmu_rgntlb_demap(vaddr, rgnp, 9594 hmeblkp, 0); 9595 sfmmu_cache_flush(pfn, addr_to_vcolor(vaddr)); 9596 } else if (sfmmup->sfmmu_ismhat) { 9597 if (flags & HAT_CACHE) { 9598 SFMMU_STAT(sf_ism_recache); 9599 } else { 9600 SFMMU_STAT(sf_ism_uncache); 9601 } 9602 sfmmu_ismtlbcache_demap(vaddr, sfmmup, hmeblkp, 9603 pfn, CACHE_FLUSH); 9604 } else { 9605 sfmmu_tlbcache_demap(vaddr, sfmmup, hmeblkp, 9606 pfn, 0, FLUSH_ALL_CPUS, CACHE_FLUSH, 1); 9607 } 9608 9609 /* 9610 * all cache entries belonging to this pfn are 9611 * now flushed. 9612 */ 9613 cache_flush_flag = CACHE_NO_FLUSH; 9614 } else { 9615 /* 9616 * Flush only TSBs and TLBs. 9617 */ 9618 if (hmeblkp->hblk_shared) { 9619 sf_srd_t *srdp = (sf_srd_t *)sfmmup; 9620 uint_t rid = hmeblkp->hblk_tag.htag_rid; 9621 sf_region_t *rgnp; 9622 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 9623 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 9624 ASSERT(srdp != NULL); 9625 rgnp = srdp->srd_hmergnp[rid]; 9626 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, 9627 srdp, rgnp, rid); 9628 (void) sfmmu_rgntlb_demap(vaddr, rgnp, 9629 hmeblkp, 0); 9630 } else if (sfmmup->sfmmu_ismhat) { 9631 if (flags & HAT_CACHE) { 9632 SFMMU_STAT(sf_ism_recache); 9633 } else { 9634 SFMMU_STAT(sf_ism_uncache); 9635 } 9636 sfmmu_ismtlbcache_demap(vaddr, sfmmup, hmeblkp, 9637 pfn, CACHE_NO_FLUSH); 9638 } else { 9639 sfmmu_tlb_demap(vaddr, sfmmup, hmeblkp, 0, 1); 9640 } 9641 } 9642 } 9643 9644 if (PP_ISMAPPED_KPM(pp)) 9645 sfmmu_kpm_page_cache(pp, flags, cache_flush_flag); 9646 9647 switch (flags) { 9648 9649 default: 9650 panic("sfmmu_pagecache: unknown flags"); 9651 break; 9652 9653 case HAT_CACHE: 9654 PP_CLRTNC(pp); 9655 PP_CLRPNC(pp); 9656 PP_SET_VCOLOR(pp, color); 9657 break; 9658 9659 case HAT_TMPNC: 9660 PP_SETTNC(pp); 9661 PP_SET_VCOLOR(pp, NO_VCOLOR); 9662 break; 9663 9664 case HAT_UNCACHE: 9665 PP_SETPNC(pp); 9666 PP_CLRTNC(pp); 9667 PP_SET_VCOLOR(pp, NO_VCOLOR); 9668 break; 9669 } 9670 } 9671 #endif /* VAC */ 9672 9673 9674 /* 9675 * Wrapper routine used to return a context. 9676 * 9677 * It's the responsibility of the caller to guarantee that the 9678 * process serializes on calls here by taking the HAT lock for 9679 * the hat. 9680 * 9681 */ 9682 static void 9683 sfmmu_get_ctx(sfmmu_t *sfmmup) 9684 { 9685 mmu_ctx_t *mmu_ctxp; 9686 uint_t pstate_save; 9687 int ret; 9688 9689 ASSERT(sfmmu_hat_lock_held(sfmmup)); 9690 ASSERT(sfmmup != ksfmmup); 9691 9692 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_ALLCTX_INVALID)) { 9693 sfmmu_setup_tsbinfo(sfmmup); 9694 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ALLCTX_INVALID); 9695 } 9696 9697 kpreempt_disable(); 9698 9699 mmu_ctxp = CPU_MMU_CTXP(CPU); 9700 ASSERT(mmu_ctxp); 9701 ASSERT(mmu_ctxp->mmu_idx < max_mmu_ctxdoms); 9702 ASSERT(mmu_ctxp == mmu_ctxs_tbl[mmu_ctxp->mmu_idx]); 9703 9704 /* 9705 * Do a wrap-around if cnum reaches the max # cnum supported by a MMU. 9706 */ 9707 if (mmu_ctxp->mmu_cnum == mmu_ctxp->mmu_nctxs) 9708 sfmmu_ctx_wrap_around(mmu_ctxp); 9709 9710 /* 9711 * Let the MMU set up the page sizes to use for 9712 * this context in the TLB. Don't program 2nd dtlb for ism hat. 9713 */ 9714 if ((&mmu_set_ctx_page_sizes) && (sfmmup->sfmmu_ismhat == 0)) { 9715 mmu_set_ctx_page_sizes(sfmmup); 9716 } 9717 9718 /* 9719 * sfmmu_alloc_ctx and sfmmu_load_mmustate will be performed with 9720 * interrupts disabled to prevent race condition with wrap-around 9721 * ctx invalidatation. In sun4v, ctx invalidation also involves 9722 * a HV call to set the number of TSBs to 0. If interrupts are not 9723 * disabled until after sfmmu_load_mmustate is complete TSBs may 9724 * become assigned to INVALID_CONTEXT. This is not allowed. 9725 */ 9726 pstate_save = sfmmu_disable_intrs(); 9727 9728 if (sfmmu_alloc_ctx(sfmmup, 1, CPU, SFMMU_PRIVATE) && 9729 sfmmup->sfmmu_scdp != NULL) { 9730 sf_scd_t *scdp = sfmmup->sfmmu_scdp; 9731 sfmmu_t *scsfmmup = scdp->scd_sfmmup; 9732 ret = sfmmu_alloc_ctx(scsfmmup, 1, CPU, SFMMU_SHARED); 9733 /* debug purpose only */ 9734 ASSERT(!ret || scsfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum 9735 != INVALID_CONTEXT); 9736 } 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 read only main text 13957 * region. 13958 */ 13959 if (r_type == SFMMU_REGION_HME && ((r_obj != srdp->srd_evp) || 13960 (r_perm & PROT_WRITE))) { 13961 return (HAT_INVALID_REGION_COOKIE); 13962 } 13963 13964 rhash = RGN_HASH_FUNCTION(r_obj); 13965 13966 if (r_type == SFMMU_REGION_ISM) { 13967 nextidp = &srdp->srd_next_ismrid; 13968 freelistp = &srdp->srd_ismrgnfree; 13969 maxids = SFMMU_MAX_ISM_REGIONS; 13970 rarrp = srdp->srd_ismrgnp; 13971 busyrgnsp = &srdp->srd_ismbusyrgns; 13972 } else { 13973 nextidp = &srdp->srd_next_hmerid; 13974 freelistp = &srdp->srd_hmergnfree; 13975 maxids = SFMMU_MAX_HME_REGIONS; 13976 rarrp = srdp->srd_hmergnp; 13977 busyrgnsp = &srdp->srd_hmebusyrgns; 13978 } 13979 13980 mutex_enter(&srdp->srd_mutex); 13981 13982 for (rgnp = srdp->srd_rgnhash[rhash]; rgnp != NULL; 13983 rgnp = rgnp->rgn_hash) { 13984 if (rgnp->rgn_saddr == r_saddr && rgnp->rgn_size == r_size && 13985 rgnp->rgn_obj == r_obj && rgnp->rgn_objoff == r_objoff && 13986 rgnp->rgn_perm == r_perm && rgnp->rgn_pgszc == r_pgszc) { 13987 break; 13988 } 13989 } 13990 13991 rfound: 13992 if (rgnp != NULL) { 13993 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type); 13994 ASSERT(rgnp->rgn_cb_function == r_cb_function); 13995 ASSERT(rgnp->rgn_refcnt >= 0); 13996 rid = rgnp->rgn_id; 13997 ASSERT(rid < maxids); 13998 ASSERT(rarrp[rid] == rgnp); 13999 ASSERT(rid < *nextidp); 14000 atomic_add_32((volatile uint_t *)&rgnp->rgn_refcnt, 1); 14001 mutex_exit(&srdp->srd_mutex); 14002 if (new_rgnp != NULL) { 14003 kmem_cache_free(region_cache, new_rgnp); 14004 } 14005 if (r_type == SFMMU_REGION_HME) { 14006 int myjoin = 14007 (sfmmup == astosfmmu(curthread->t_procp->p_as)); 14008 14009 sfmmu_link_to_hmeregion(sfmmup, rgnp); 14010 /* 14011 * bitmap should be updated after linking sfmmu on 14012 * region list so that pageunload() doesn't skip 14013 * TSB/TLB flush. As soon as bitmap is updated another 14014 * thread in this process can already start accessing 14015 * this region. 14016 */ 14017 /* 14018 * Normally ttecnt accounting is done as part of 14019 * pagefault handling. But a process may not take any 14020 * pagefaults on shared hmeblks created by some other 14021 * process. To compensate for this assume that the 14022 * entire region will end up faulted in using 14023 * the region's pagesize. 14024 * 14025 */ 14026 if (r_pgszc > TTE8K) { 14027 tteflag = 1 << r_pgszc; 14028 if (disable_large_pages & tteflag) { 14029 tteflag = 0; 14030 } 14031 } else { 14032 tteflag = 0; 14033 } 14034 if (tteflag && !(sfmmup->sfmmu_rtteflags & tteflag)) { 14035 hatlockp = sfmmu_hat_enter(sfmmup); 14036 sfmmup->sfmmu_rtteflags |= tteflag; 14037 sfmmu_hat_exit(hatlockp); 14038 } 14039 hatlockp = sfmmu_hat_enter(sfmmup); 14040 14041 /* 14042 * Preallocate 1/4 of ttecnt's in 8K TSB for >= 4M 14043 * region to allow for large page allocation failure. 14044 */ 14045 if (r_pgszc >= TTE4M) { 14046 sfmmup->sfmmu_tsb0_4minflcnt += 14047 r_size >> (TTE_PAGE_SHIFT(TTE8K) + 2); 14048 } 14049 14050 /* update sfmmu_ttecnt with the shme rgn ttecnt */ 14051 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc); 14052 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc], 14053 rttecnt); 14054 14055 if (text && r_pgszc >= TTE4M && 14056 (tteflag || ((disable_large_pages >> TTE4M) & 14057 ((1 << (r_pgszc - TTE4M + 1)) - 1))) && 14058 !SFMMU_FLAGS_ISSET(sfmmup, HAT_4MTEXT_FLAG)) { 14059 SFMMU_FLAGS_SET(sfmmup, HAT_4MTEXT_FLAG); 14060 } 14061 14062 sfmmu_hat_exit(hatlockp); 14063 /* 14064 * On Panther we need to make sure TLB is programmed 14065 * to accept 32M/256M pages. Call 14066 * sfmmu_check_page_sizes() now to make sure TLB is 14067 * setup before making hmeregions visible to other 14068 * threads. 14069 */ 14070 sfmmu_check_page_sizes(sfmmup, 1); 14071 hatlockp = sfmmu_hat_enter(sfmmup); 14072 SF_RGNMAP_ADD(sfmmup->sfmmu_hmeregion_map, rid); 14073 14074 /* 14075 * if context is invalid tsb miss exception code will 14076 * call sfmmu_check_page_sizes() and update tsbmiss 14077 * area later. 14078 */ 14079 kpreempt_disable(); 14080 if (myjoin && 14081 (sfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum 14082 != INVALID_CONTEXT)) { 14083 struct tsbmiss *tsbmp; 14084 14085 tsbmp = &tsbmiss_area[CPU->cpu_id]; 14086 ASSERT(sfmmup == tsbmp->usfmmup); 14087 BT_SET(tsbmp->shmermap, rid); 14088 if (r_pgszc > TTE64K) { 14089 tsbmp->uhat_rtteflags |= tteflag; 14090 } 14091 14092 } 14093 kpreempt_enable(); 14094 14095 sfmmu_hat_exit(hatlockp); 14096 ASSERT((hat_region_cookie_t)((uint64_t)rid) != 14097 HAT_INVALID_REGION_COOKIE); 14098 } else { 14099 hatlockp = sfmmu_hat_enter(sfmmup); 14100 SF_RGNMAP_ADD(sfmmup->sfmmu_ismregion_map, rid); 14101 sfmmu_hat_exit(hatlockp); 14102 } 14103 ASSERT(rid < maxids); 14104 14105 if (r_type == SFMMU_REGION_ISM) { 14106 sfmmu_find_scd(sfmmup); 14107 } 14108 return ((hat_region_cookie_t)((uint64_t)rid)); 14109 } 14110 14111 ASSERT(new_rgnp == NULL); 14112 14113 if (*busyrgnsp >= maxids) { 14114 mutex_exit(&srdp->srd_mutex); 14115 return (HAT_INVALID_REGION_COOKIE); 14116 } 14117 14118 ASSERT(MUTEX_HELD(&srdp->srd_mutex)); 14119 if (*freelistp != NULL) { 14120 rgnp = *freelistp; 14121 *freelistp = rgnp->rgn_next; 14122 ASSERT(rgnp->rgn_id < *nextidp); 14123 ASSERT(rgnp->rgn_id < maxids); 14124 ASSERT(rgnp->rgn_flags & SFMMU_REGION_FREE); 14125 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) 14126 == r_type); 14127 ASSERT(rarrp[rgnp->rgn_id] == rgnp); 14128 ASSERT(rgnp->rgn_hmeflags == 0); 14129 } else { 14130 /* 14131 * release local locks before memory allocation. 14132 */ 14133 mutex_exit(&srdp->srd_mutex); 14134 14135 new_rgnp = kmem_cache_alloc(region_cache, KM_SLEEP); 14136 14137 mutex_enter(&srdp->srd_mutex); 14138 for (rgnp = srdp->srd_rgnhash[rhash]; rgnp != NULL; 14139 rgnp = rgnp->rgn_hash) { 14140 if (rgnp->rgn_saddr == r_saddr && 14141 rgnp->rgn_size == r_size && 14142 rgnp->rgn_obj == r_obj && 14143 rgnp->rgn_objoff == r_objoff && 14144 rgnp->rgn_perm == r_perm && 14145 rgnp->rgn_pgszc == r_pgszc) { 14146 break; 14147 } 14148 } 14149 if (rgnp != NULL) { 14150 goto rfound; 14151 } 14152 14153 if (*nextidp >= maxids) { 14154 mutex_exit(&srdp->srd_mutex); 14155 goto fail; 14156 } 14157 rgnp = new_rgnp; 14158 new_rgnp = NULL; 14159 rgnp->rgn_id = (*nextidp)++; 14160 ASSERT(rgnp->rgn_id < maxids); 14161 ASSERT(rarrp[rgnp->rgn_id] == NULL); 14162 rarrp[rgnp->rgn_id] = rgnp; 14163 } 14164 14165 ASSERT(rgnp->rgn_sfmmu_head == NULL); 14166 ASSERT(rgnp->rgn_hmeflags == 0); 14167 #ifdef DEBUG 14168 for (i = 0; i < MMU_PAGE_SIZES; i++) { 14169 ASSERT(rgnp->rgn_ttecnt[i] == 0); 14170 } 14171 #endif 14172 rgnp->rgn_saddr = r_saddr; 14173 rgnp->rgn_size = r_size; 14174 rgnp->rgn_obj = r_obj; 14175 rgnp->rgn_objoff = r_objoff; 14176 rgnp->rgn_perm = r_perm; 14177 rgnp->rgn_pgszc = r_pgszc; 14178 rgnp->rgn_flags = r_type; 14179 rgnp->rgn_refcnt = 0; 14180 rgnp->rgn_cb_function = r_cb_function; 14181 rgnp->rgn_hash = srdp->srd_rgnhash[rhash]; 14182 srdp->srd_rgnhash[rhash] = rgnp; 14183 (*busyrgnsp)++; 14184 ASSERT(*busyrgnsp <= maxids); 14185 goto rfound; 14186 14187 fail: 14188 ASSERT(new_rgnp != NULL); 14189 kmem_cache_free(region_cache, new_rgnp); 14190 return (HAT_INVALID_REGION_COOKIE); 14191 } 14192 14193 /* 14194 * This function implements the shared context functionality required 14195 * when detaching a segment from an address space. It must be called 14196 * from hat_unshare() for all D(ISM) segments and from segvn_unmap(), 14197 * for segments with a valid region_cookie. 14198 * It will also be called from all seg_vn routines which change a 14199 * segment's attributes such as segvn_setprot(), segvn_setpagesize(), 14200 * segvn_clrszc() & segvn_advise(), as well as in the case of COW fault 14201 * from segvn_fault(). 14202 */ 14203 void 14204 hat_leave_region(struct hat *sfmmup, hat_region_cookie_t rcookie, uint_t flags) 14205 { 14206 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 14207 sf_scd_t *scdp; 14208 uint_t rhash; 14209 uint_t rid = (uint_t)((uint64_t)rcookie); 14210 hatlock_t *hatlockp = NULL; 14211 sf_region_t *rgnp; 14212 sf_region_t **prev_rgnpp; 14213 sf_region_t *cur_rgnp; 14214 void *r_obj; 14215 int i; 14216 caddr_t r_saddr; 14217 caddr_t r_eaddr; 14218 size_t r_size; 14219 uchar_t r_pgszc; 14220 uchar_t r_type = flags & HAT_REGION_TYPE_MASK; 14221 14222 ASSERT(sfmmup != ksfmmup); 14223 ASSERT(srdp != NULL); 14224 ASSERT(srdp->srd_refcnt > 0); 14225 ASSERT(!(flags & ~HAT_REGION_TYPE_MASK)); 14226 ASSERT(flags == HAT_REGION_TEXT || flags == HAT_REGION_ISM); 14227 ASSERT(!sfmmup->sfmmu_free || sfmmup->sfmmu_scdp == NULL); 14228 14229 r_type = (r_type == HAT_REGION_ISM) ? SFMMU_REGION_ISM : 14230 SFMMU_REGION_HME; 14231 14232 if (r_type == SFMMU_REGION_ISM) { 14233 ASSERT(SFMMU_IS_ISMRID_VALID(rid)); 14234 ASSERT(rid < SFMMU_MAX_ISM_REGIONS); 14235 rgnp = srdp->srd_ismrgnp[rid]; 14236 } else { 14237 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 14238 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 14239 rgnp = srdp->srd_hmergnp[rid]; 14240 } 14241 ASSERT(rgnp != NULL); 14242 ASSERT(rgnp->rgn_id == rid); 14243 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type); 14244 ASSERT(!(rgnp->rgn_flags & SFMMU_REGION_FREE)); 14245 ASSERT(AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 14246 14247 ASSERT(sfmmup->sfmmu_xhat_provider == NULL); 14248 if (r_type == SFMMU_REGION_HME && sfmmup->sfmmu_as->a_xhat != NULL) { 14249 xhat_unload_callback_all(sfmmup->sfmmu_as, rgnp->rgn_saddr, 14250 rgnp->rgn_size, 0, NULL); 14251 } 14252 14253 if (sfmmup->sfmmu_free) { 14254 ulong_t rttecnt; 14255 r_pgszc = rgnp->rgn_pgszc; 14256 r_size = rgnp->rgn_size; 14257 14258 ASSERT(sfmmup->sfmmu_scdp == NULL); 14259 if (r_type == SFMMU_REGION_ISM) { 14260 SF_RGNMAP_DEL(sfmmup->sfmmu_ismregion_map, rid); 14261 } else { 14262 /* update shme rgns ttecnt in sfmmu_ttecnt */ 14263 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc); 14264 ASSERT(sfmmup->sfmmu_ttecnt[r_pgszc] >= rttecnt); 14265 14266 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc], 14267 -rttecnt); 14268 14269 SF_RGNMAP_DEL(sfmmup->sfmmu_hmeregion_map, rid); 14270 } 14271 } else if (r_type == SFMMU_REGION_ISM) { 14272 hatlockp = sfmmu_hat_enter(sfmmup); 14273 ASSERT(rid < srdp->srd_next_ismrid); 14274 SF_RGNMAP_DEL(sfmmup->sfmmu_ismregion_map, rid); 14275 scdp = sfmmup->sfmmu_scdp; 14276 if (scdp != NULL && 14277 SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid)) { 14278 sfmmu_leave_scd(sfmmup, r_type); 14279 ASSERT(sfmmu_hat_lock_held(sfmmup)); 14280 } 14281 sfmmu_hat_exit(hatlockp); 14282 } else { 14283 ulong_t rttecnt; 14284 r_pgszc = rgnp->rgn_pgszc; 14285 r_saddr = rgnp->rgn_saddr; 14286 r_size = rgnp->rgn_size; 14287 r_eaddr = r_saddr + r_size; 14288 14289 ASSERT(r_type == SFMMU_REGION_HME); 14290 hatlockp = sfmmu_hat_enter(sfmmup); 14291 ASSERT(rid < srdp->srd_next_hmerid); 14292 SF_RGNMAP_DEL(sfmmup->sfmmu_hmeregion_map, rid); 14293 14294 /* 14295 * If region is part of an SCD call sfmmu_leave_scd(). 14296 * Otherwise if process is not exiting and has valid context 14297 * just drop the context on the floor to lose stale TLB 14298 * entries and force the update of tsb miss area to reflect 14299 * the new region map. After that clean our TSB entries. 14300 */ 14301 scdp = sfmmup->sfmmu_scdp; 14302 if (scdp != NULL && 14303 SF_RGNMAP_TEST(scdp->scd_hmeregion_map, rid)) { 14304 sfmmu_leave_scd(sfmmup, r_type); 14305 ASSERT(sfmmu_hat_lock_held(sfmmup)); 14306 } 14307 sfmmu_invalidate_ctx(sfmmup); 14308 14309 i = TTE8K; 14310 while (i < mmu_page_sizes) { 14311 if (rgnp->rgn_ttecnt[i] != 0) { 14312 sfmmu_unload_tsb_range(sfmmup, r_saddr, 14313 r_eaddr, i); 14314 if (i < TTE4M) { 14315 i = TTE4M; 14316 continue; 14317 } else { 14318 break; 14319 } 14320 } 14321 i++; 14322 } 14323 /* Remove the preallocated 1/4 8k ttecnt for 4M regions. */ 14324 if (r_pgszc >= TTE4M) { 14325 rttecnt = r_size >> (TTE_PAGE_SHIFT(TTE8K) + 2); 14326 ASSERT(sfmmup->sfmmu_tsb0_4minflcnt >= 14327 rttecnt); 14328 sfmmup->sfmmu_tsb0_4minflcnt -= rttecnt; 14329 } 14330 14331 /* update shme rgns ttecnt in sfmmu_ttecnt */ 14332 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc); 14333 ASSERT(sfmmup->sfmmu_ttecnt[r_pgszc] >= rttecnt); 14334 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc], -rttecnt); 14335 14336 sfmmu_hat_exit(hatlockp); 14337 if (scdp != NULL && sfmmup->sfmmu_scdp == NULL) { 14338 /* sfmmup left the scd, grow private tsb */ 14339 sfmmu_check_page_sizes(sfmmup, 1); 14340 } else { 14341 sfmmu_check_page_sizes(sfmmup, 0); 14342 } 14343 } 14344 14345 if (r_type == SFMMU_REGION_HME) { 14346 sfmmu_unlink_from_hmeregion(sfmmup, rgnp); 14347 } 14348 14349 r_obj = rgnp->rgn_obj; 14350 if (atomic_add_32_nv((volatile uint_t *)&rgnp->rgn_refcnt, -1)) { 14351 return; 14352 } 14353 14354 /* 14355 * looks like nobody uses this region anymore. Free it. 14356 */ 14357 rhash = RGN_HASH_FUNCTION(r_obj); 14358 mutex_enter(&srdp->srd_mutex); 14359 for (prev_rgnpp = &srdp->srd_rgnhash[rhash]; 14360 (cur_rgnp = *prev_rgnpp) != NULL; 14361 prev_rgnpp = &cur_rgnp->rgn_hash) { 14362 if (cur_rgnp == rgnp && cur_rgnp->rgn_refcnt == 0) { 14363 break; 14364 } 14365 } 14366 14367 if (cur_rgnp == NULL) { 14368 mutex_exit(&srdp->srd_mutex); 14369 return; 14370 } 14371 14372 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type); 14373 *prev_rgnpp = rgnp->rgn_hash; 14374 if (r_type == SFMMU_REGION_ISM) { 14375 rgnp->rgn_flags |= SFMMU_REGION_FREE; 14376 ASSERT(rid < srdp->srd_next_ismrid); 14377 rgnp->rgn_next = srdp->srd_ismrgnfree; 14378 srdp->srd_ismrgnfree = rgnp; 14379 ASSERT(srdp->srd_ismbusyrgns > 0); 14380 srdp->srd_ismbusyrgns--; 14381 mutex_exit(&srdp->srd_mutex); 14382 return; 14383 } 14384 mutex_exit(&srdp->srd_mutex); 14385 14386 /* 14387 * Destroy region's hmeblks. 14388 */ 14389 sfmmu_unload_hmeregion(srdp, rgnp); 14390 14391 rgnp->rgn_hmeflags = 0; 14392 14393 ASSERT(rgnp->rgn_sfmmu_head == NULL); 14394 ASSERT(rgnp->rgn_id == rid); 14395 for (i = 0; i < MMU_PAGE_SIZES; i++) { 14396 rgnp->rgn_ttecnt[i] = 0; 14397 } 14398 rgnp->rgn_flags |= SFMMU_REGION_FREE; 14399 mutex_enter(&srdp->srd_mutex); 14400 ASSERT(rid < srdp->srd_next_hmerid); 14401 rgnp->rgn_next = srdp->srd_hmergnfree; 14402 srdp->srd_hmergnfree = rgnp; 14403 ASSERT(srdp->srd_hmebusyrgns > 0); 14404 srdp->srd_hmebusyrgns--; 14405 mutex_exit(&srdp->srd_mutex); 14406 } 14407 14408 /* 14409 * For now only called for hmeblk regions and not for ISM regions. 14410 */ 14411 void 14412 hat_dup_region(struct hat *sfmmup, hat_region_cookie_t rcookie) 14413 { 14414 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 14415 uint_t rid = (uint_t)((uint64_t)rcookie); 14416 sf_region_t *rgnp; 14417 sf_rgn_link_t *rlink; 14418 sf_rgn_link_t *hrlink; 14419 ulong_t rttecnt; 14420 14421 ASSERT(sfmmup != ksfmmup); 14422 ASSERT(srdp != NULL); 14423 ASSERT(srdp->srd_refcnt > 0); 14424 14425 ASSERT(rid < srdp->srd_next_hmerid); 14426 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 14427 ASSERT(rid < SFMMU_MAX_HME_REGIONS); 14428 14429 rgnp = srdp->srd_hmergnp[rid]; 14430 ASSERT(rgnp->rgn_refcnt > 0); 14431 ASSERT(rgnp->rgn_id == rid); 14432 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == SFMMU_REGION_HME); 14433 ASSERT(!(rgnp->rgn_flags & SFMMU_REGION_FREE)); 14434 14435 atomic_add_32((volatile uint_t *)&rgnp->rgn_refcnt, 1); 14436 14437 /* LINTED: constant in conditional context */ 14438 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 1, 0); 14439 ASSERT(rlink != NULL); 14440 mutex_enter(&rgnp->rgn_mutex); 14441 ASSERT(rgnp->rgn_sfmmu_head != NULL); 14442 /* LINTED: constant in conditional context */ 14443 SFMMU_HMERID2RLINKP(rgnp->rgn_sfmmu_head, rid, hrlink, 0, 0); 14444 ASSERT(hrlink != NULL); 14445 ASSERT(hrlink->prev == NULL); 14446 rlink->next = rgnp->rgn_sfmmu_head; 14447 rlink->prev = NULL; 14448 hrlink->prev = sfmmup; 14449 /* 14450 * make sure rlink's next field is correct 14451 * before making this link visible. 14452 */ 14453 membar_stst(); 14454 rgnp->rgn_sfmmu_head = sfmmup; 14455 mutex_exit(&rgnp->rgn_mutex); 14456 14457 /* update sfmmu_ttecnt with the shme rgn ttecnt */ 14458 rttecnt = rgnp->rgn_size >> TTE_PAGE_SHIFT(rgnp->rgn_pgszc); 14459 atomic_add_long(&sfmmup->sfmmu_ttecnt[rgnp->rgn_pgszc], rttecnt); 14460 /* update tsb0 inflation count */ 14461 if (rgnp->rgn_pgszc >= TTE4M) { 14462 sfmmup->sfmmu_tsb0_4minflcnt += 14463 rgnp->rgn_size >> (TTE_PAGE_SHIFT(TTE8K) + 2); 14464 } 14465 /* 14466 * Update regionid bitmask without hat lock since no other thread 14467 * can update this region bitmask right now. 14468 */ 14469 SF_RGNMAP_ADD(sfmmup->sfmmu_hmeregion_map, rid); 14470 } 14471 14472 /* ARGSUSED */ 14473 static int 14474 sfmmu_rgncache_constructor(void *buf, void *cdrarg, int kmflags) 14475 { 14476 sf_region_t *rgnp = (sf_region_t *)buf; 14477 bzero(buf, sizeof (*rgnp)); 14478 14479 mutex_init(&rgnp->rgn_mutex, NULL, MUTEX_DEFAULT, NULL); 14480 14481 return (0); 14482 } 14483 14484 /* ARGSUSED */ 14485 static void 14486 sfmmu_rgncache_destructor(void *buf, void *cdrarg) 14487 { 14488 sf_region_t *rgnp = (sf_region_t *)buf; 14489 mutex_destroy(&rgnp->rgn_mutex); 14490 } 14491 14492 static int 14493 sfrgnmap_isnull(sf_region_map_t *map) 14494 { 14495 int i; 14496 14497 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) { 14498 if (map->bitmap[i] != 0) { 14499 return (0); 14500 } 14501 } 14502 return (1); 14503 } 14504 14505 static int 14506 sfhmergnmap_isnull(sf_hmeregion_map_t *map) 14507 { 14508 int i; 14509 14510 for (i = 0; i < SFMMU_HMERGNMAP_WORDS; i++) { 14511 if (map->bitmap[i] != 0) { 14512 return (0); 14513 } 14514 } 14515 return (1); 14516 } 14517 14518 #ifdef DEBUG 14519 static void 14520 check_scd_sfmmu_list(sfmmu_t **headp, sfmmu_t *sfmmup, int onlist) 14521 { 14522 sfmmu_t *sp; 14523 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 14524 14525 for (sp = *headp; sp != NULL; sp = sp->sfmmu_scd_link.next) { 14526 ASSERT(srdp == sp->sfmmu_srdp); 14527 if (sp == sfmmup) { 14528 if (onlist) { 14529 return; 14530 } else { 14531 panic("shctx: sfmmu 0x%p found on scd" 14532 "list 0x%p", sfmmup, *headp); 14533 } 14534 } 14535 } 14536 if (onlist) { 14537 panic("shctx: sfmmu 0x%p not found on scd list 0x%p", 14538 sfmmup, *headp); 14539 } else { 14540 return; 14541 } 14542 } 14543 #else /* DEBUG */ 14544 #define check_scd_sfmmu_list(headp, sfmmup, onlist) 14545 #endif /* DEBUG */ 14546 14547 /* 14548 * Removes an sfmmu from the SCD sfmmu list. 14549 */ 14550 static void 14551 sfmmu_from_scd_list(sfmmu_t **headp, sfmmu_t *sfmmup) 14552 { 14553 ASSERT(sfmmup->sfmmu_srdp != NULL); 14554 check_scd_sfmmu_list(headp, sfmmup, 1); 14555 if (sfmmup->sfmmu_scd_link.prev != NULL) { 14556 ASSERT(*headp != sfmmup); 14557 sfmmup->sfmmu_scd_link.prev->sfmmu_scd_link.next = 14558 sfmmup->sfmmu_scd_link.next; 14559 } else { 14560 ASSERT(*headp == sfmmup); 14561 *headp = sfmmup->sfmmu_scd_link.next; 14562 } 14563 if (sfmmup->sfmmu_scd_link.next != NULL) { 14564 sfmmup->sfmmu_scd_link.next->sfmmu_scd_link.prev = 14565 sfmmup->sfmmu_scd_link.prev; 14566 } 14567 } 14568 14569 14570 /* 14571 * Adds an sfmmu to the start of the queue. 14572 */ 14573 static void 14574 sfmmu_to_scd_list(sfmmu_t **headp, sfmmu_t *sfmmup) 14575 { 14576 check_scd_sfmmu_list(headp, sfmmup, 0); 14577 sfmmup->sfmmu_scd_link.prev = NULL; 14578 sfmmup->sfmmu_scd_link.next = *headp; 14579 if (*headp != NULL) 14580 (*headp)->sfmmu_scd_link.prev = sfmmup; 14581 *headp = sfmmup; 14582 } 14583 14584 /* 14585 * Remove an scd from the start of the queue. 14586 */ 14587 static void 14588 sfmmu_remove_scd(sf_scd_t **headp, sf_scd_t *scdp) 14589 { 14590 if (scdp->scd_prev != NULL) { 14591 ASSERT(*headp != scdp); 14592 scdp->scd_prev->scd_next = scdp->scd_next; 14593 } else { 14594 ASSERT(*headp == scdp); 14595 *headp = scdp->scd_next; 14596 } 14597 14598 if (scdp->scd_next != NULL) { 14599 scdp->scd_next->scd_prev = scdp->scd_prev; 14600 } 14601 } 14602 14603 /* 14604 * Add an scd to the start of the queue. 14605 */ 14606 static void 14607 sfmmu_add_scd(sf_scd_t **headp, sf_scd_t *scdp) 14608 { 14609 scdp->scd_prev = NULL; 14610 scdp->scd_next = *headp; 14611 if (*headp != NULL) { 14612 (*headp)->scd_prev = scdp; 14613 } 14614 *headp = scdp; 14615 } 14616 14617 static int 14618 sfmmu_alloc_scd_tsbs(sf_srd_t *srdp, sf_scd_t *scdp) 14619 { 14620 uint_t rid; 14621 uint_t i; 14622 uint_t j; 14623 ulong_t w; 14624 sf_region_t *rgnp; 14625 ulong_t tte8k_cnt = 0; 14626 ulong_t tte4m_cnt = 0; 14627 uint_t tsb_szc; 14628 sfmmu_t *scsfmmup = scdp->scd_sfmmup; 14629 sfmmu_t *ism_hatid; 14630 struct tsb_info *newtsb; 14631 int szc; 14632 14633 ASSERT(srdp != NULL); 14634 14635 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) { 14636 if ((w = scdp->scd_region_map.bitmap[i]) == 0) { 14637 continue; 14638 } 14639 j = 0; 14640 while (w) { 14641 if (!(w & 0x1)) { 14642 j++; 14643 w >>= 1; 14644 continue; 14645 } 14646 rid = (i << BT_ULSHIFT) | j; 14647 j++; 14648 w >>= 1; 14649 14650 if (rid < SFMMU_MAX_HME_REGIONS) { 14651 rgnp = srdp->srd_hmergnp[rid]; 14652 ASSERT(rgnp->rgn_id == rid); 14653 ASSERT(rgnp->rgn_refcnt > 0); 14654 14655 if (rgnp->rgn_pgszc < TTE4M) { 14656 tte8k_cnt += rgnp->rgn_size >> 14657 TTE_PAGE_SHIFT(TTE8K); 14658 } else { 14659 ASSERT(rgnp->rgn_pgszc >= TTE4M); 14660 tte4m_cnt += rgnp->rgn_size >> 14661 TTE_PAGE_SHIFT(TTE4M); 14662 /* 14663 * Inflate SCD tsb0 by preallocating 14664 * 1/4 8k ttecnt for 4M regions to 14665 * allow for lgpg alloc failure. 14666 */ 14667 tte8k_cnt += rgnp->rgn_size >> 14668 (TTE_PAGE_SHIFT(TTE8K) + 2); 14669 } 14670 } else { 14671 rid -= SFMMU_MAX_HME_REGIONS; 14672 rgnp = srdp->srd_ismrgnp[rid]; 14673 ASSERT(rgnp->rgn_id == rid); 14674 ASSERT(rgnp->rgn_refcnt > 0); 14675 14676 ism_hatid = (sfmmu_t *)rgnp->rgn_obj; 14677 ASSERT(ism_hatid->sfmmu_ismhat); 14678 14679 for (szc = 0; szc < TTE4M; szc++) { 14680 tte8k_cnt += 14681 ism_hatid->sfmmu_ttecnt[szc] << 14682 TTE_BSZS_SHIFT(szc); 14683 } 14684 14685 ASSERT(rgnp->rgn_pgszc >= TTE4M); 14686 if (rgnp->rgn_pgszc >= TTE4M) { 14687 tte4m_cnt += rgnp->rgn_size >> 14688 TTE_PAGE_SHIFT(TTE4M); 14689 } 14690 } 14691 } 14692 } 14693 14694 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt); 14695 14696 /* Allocate both the SCD TSBs here. */ 14697 if (sfmmu_tsbinfo_alloc(&scsfmmup->sfmmu_tsb, 14698 tsb_szc, TSB8K|TSB64K|TSB512K, TSB_ALLOC, scsfmmup) && 14699 (tsb_szc <= TSB_4M_SZCODE || 14700 sfmmu_tsbinfo_alloc(&scsfmmup->sfmmu_tsb, 14701 TSB_4M_SZCODE, TSB8K|TSB64K|TSB512K, 14702 TSB_ALLOC, scsfmmup))) { 14703 14704 SFMMU_STAT(sf_scd_1sttsb_allocfail); 14705 return (TSB_ALLOCFAIL); 14706 } else { 14707 scsfmmup->sfmmu_tsb->tsb_flags |= TSB_SHAREDCTX; 14708 14709 if (tte4m_cnt) { 14710 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt); 14711 if (sfmmu_tsbinfo_alloc(&newtsb, tsb_szc, 14712 TSB4M|TSB32M|TSB256M, TSB_ALLOC, scsfmmup) && 14713 (tsb_szc <= TSB_4M_SZCODE || 14714 sfmmu_tsbinfo_alloc(&newtsb, TSB_4M_SZCODE, 14715 TSB4M|TSB32M|TSB256M, 14716 TSB_ALLOC, scsfmmup))) { 14717 /* 14718 * If we fail to allocate the 2nd shared tsb, 14719 * just free the 1st tsb, return failure. 14720 */ 14721 sfmmu_tsbinfo_free(scsfmmup->sfmmu_tsb); 14722 SFMMU_STAT(sf_scd_2ndtsb_allocfail); 14723 return (TSB_ALLOCFAIL); 14724 } else { 14725 ASSERT(scsfmmup->sfmmu_tsb->tsb_next == NULL); 14726 newtsb->tsb_flags |= TSB_SHAREDCTX; 14727 scsfmmup->sfmmu_tsb->tsb_next = newtsb; 14728 SFMMU_STAT(sf_scd_2ndtsb_alloc); 14729 } 14730 } 14731 SFMMU_STAT(sf_scd_1sttsb_alloc); 14732 } 14733 return (TSB_SUCCESS); 14734 } 14735 14736 static void 14737 sfmmu_free_scd_tsbs(sfmmu_t *scd_sfmmu) 14738 { 14739 while (scd_sfmmu->sfmmu_tsb != NULL) { 14740 struct tsb_info *next = scd_sfmmu->sfmmu_tsb->tsb_next; 14741 sfmmu_tsbinfo_free(scd_sfmmu->sfmmu_tsb); 14742 scd_sfmmu->sfmmu_tsb = next; 14743 } 14744 } 14745 14746 /* 14747 * Link the sfmmu onto the hme region list. 14748 */ 14749 void 14750 sfmmu_link_to_hmeregion(sfmmu_t *sfmmup, sf_region_t *rgnp) 14751 { 14752 uint_t rid; 14753 sf_rgn_link_t *rlink; 14754 sfmmu_t *head; 14755 sf_rgn_link_t *hrlink; 14756 14757 rid = rgnp->rgn_id; 14758 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 14759 14760 /* LINTED: constant in conditional context */ 14761 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 1, 1); 14762 ASSERT(rlink != NULL); 14763 mutex_enter(&rgnp->rgn_mutex); 14764 if ((head = rgnp->rgn_sfmmu_head) == NULL) { 14765 rlink->next = NULL; 14766 rlink->prev = NULL; 14767 /* 14768 * make sure rlink's next field is NULL 14769 * before making this link visible. 14770 */ 14771 membar_stst(); 14772 rgnp->rgn_sfmmu_head = sfmmup; 14773 } else { 14774 /* LINTED: constant in conditional context */ 14775 SFMMU_HMERID2RLINKP(head, rid, hrlink, 0, 0); 14776 ASSERT(hrlink != NULL); 14777 ASSERT(hrlink->prev == NULL); 14778 rlink->next = head; 14779 rlink->prev = NULL; 14780 hrlink->prev = sfmmup; 14781 /* 14782 * make sure rlink's next field is correct 14783 * before making this link visible. 14784 */ 14785 membar_stst(); 14786 rgnp->rgn_sfmmu_head = sfmmup; 14787 } 14788 mutex_exit(&rgnp->rgn_mutex); 14789 } 14790 14791 /* 14792 * Unlink the sfmmu from the hme region list. 14793 */ 14794 void 14795 sfmmu_unlink_from_hmeregion(sfmmu_t *sfmmup, sf_region_t *rgnp) 14796 { 14797 uint_t rid; 14798 sf_rgn_link_t *rlink; 14799 14800 rid = rgnp->rgn_id; 14801 ASSERT(SFMMU_IS_SHMERID_VALID(rid)); 14802 14803 /* LINTED: constant in conditional context */ 14804 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 0, 0); 14805 ASSERT(rlink != NULL); 14806 mutex_enter(&rgnp->rgn_mutex); 14807 if (rgnp->rgn_sfmmu_head == sfmmup) { 14808 sfmmu_t *next = rlink->next; 14809 rgnp->rgn_sfmmu_head = next; 14810 /* 14811 * if we are stopped by xc_attention() after this 14812 * point the forward link walking in 14813 * sfmmu_rgntlb_demap() will work correctly since the 14814 * head correctly points to the next element. 14815 */ 14816 membar_stst(); 14817 rlink->next = NULL; 14818 ASSERT(rlink->prev == NULL); 14819 if (next != NULL) { 14820 sf_rgn_link_t *nrlink; 14821 /* LINTED: constant in conditional context */ 14822 SFMMU_HMERID2RLINKP(next, rid, nrlink, 0, 0); 14823 ASSERT(nrlink != NULL); 14824 ASSERT(nrlink->prev == sfmmup); 14825 nrlink->prev = NULL; 14826 } 14827 } else { 14828 sfmmu_t *next = rlink->next; 14829 sfmmu_t *prev = rlink->prev; 14830 sf_rgn_link_t *prlink; 14831 14832 ASSERT(prev != NULL); 14833 /* LINTED: constant in conditional context */ 14834 SFMMU_HMERID2RLINKP(prev, rid, prlink, 0, 0); 14835 ASSERT(prlink != NULL); 14836 ASSERT(prlink->next == sfmmup); 14837 prlink->next = next; 14838 /* 14839 * if we are stopped by xc_attention() 14840 * after this point the forward link walking 14841 * will work correctly since the prev element 14842 * correctly points to the next element. 14843 */ 14844 membar_stst(); 14845 rlink->next = NULL; 14846 rlink->prev = NULL; 14847 if (next != NULL) { 14848 sf_rgn_link_t *nrlink; 14849 /* LINTED: constant in conditional context */ 14850 SFMMU_HMERID2RLINKP(next, rid, nrlink, 0, 0); 14851 ASSERT(nrlink != NULL); 14852 ASSERT(nrlink->prev == sfmmup); 14853 nrlink->prev = prev; 14854 } 14855 } 14856 mutex_exit(&rgnp->rgn_mutex); 14857 } 14858 14859 /* 14860 * Link scd sfmmu onto ism or hme region list for each region in the 14861 * scd region map. 14862 */ 14863 void 14864 sfmmu_link_scd_to_regions(sf_srd_t *srdp, sf_scd_t *scdp) 14865 { 14866 uint_t rid; 14867 uint_t i; 14868 uint_t j; 14869 ulong_t w; 14870 sf_region_t *rgnp; 14871 sfmmu_t *scsfmmup; 14872 14873 scsfmmup = scdp->scd_sfmmup; 14874 ASSERT(scsfmmup->sfmmu_scdhat); 14875 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) { 14876 if ((w = scdp->scd_region_map.bitmap[i]) == 0) { 14877 continue; 14878 } 14879 j = 0; 14880 while (w) { 14881 if (!(w & 0x1)) { 14882 j++; 14883 w >>= 1; 14884 continue; 14885 } 14886 rid = (i << BT_ULSHIFT) | j; 14887 j++; 14888 w >>= 1; 14889 14890 if (rid < SFMMU_MAX_HME_REGIONS) { 14891 rgnp = srdp->srd_hmergnp[rid]; 14892 ASSERT(rgnp->rgn_id == rid); 14893 ASSERT(rgnp->rgn_refcnt > 0); 14894 sfmmu_link_to_hmeregion(scsfmmup, rgnp); 14895 } else { 14896 sfmmu_t *ism_hatid = NULL; 14897 ism_ment_t *ism_ment; 14898 rid -= SFMMU_MAX_HME_REGIONS; 14899 rgnp = srdp->srd_ismrgnp[rid]; 14900 ASSERT(rgnp->rgn_id == rid); 14901 ASSERT(rgnp->rgn_refcnt > 0); 14902 14903 ism_hatid = (sfmmu_t *)rgnp->rgn_obj; 14904 ASSERT(ism_hatid->sfmmu_ismhat); 14905 ism_ment = &scdp->scd_ism_links[rid]; 14906 ism_ment->iment_hat = scsfmmup; 14907 ism_ment->iment_base_va = rgnp->rgn_saddr; 14908 mutex_enter(&ism_mlist_lock); 14909 iment_add(ism_ment, ism_hatid); 14910 mutex_exit(&ism_mlist_lock); 14911 14912 } 14913 } 14914 } 14915 } 14916 /* 14917 * Unlink scd sfmmu from ism or hme region list for each region in the 14918 * scd region map. 14919 */ 14920 void 14921 sfmmu_unlink_scd_from_regions(sf_srd_t *srdp, sf_scd_t *scdp) 14922 { 14923 uint_t rid; 14924 uint_t i; 14925 uint_t j; 14926 ulong_t w; 14927 sf_region_t *rgnp; 14928 sfmmu_t *scsfmmup; 14929 14930 scsfmmup = scdp->scd_sfmmup; 14931 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) { 14932 if ((w = scdp->scd_region_map.bitmap[i]) == 0) { 14933 continue; 14934 } 14935 j = 0; 14936 while (w) { 14937 if (!(w & 0x1)) { 14938 j++; 14939 w >>= 1; 14940 continue; 14941 } 14942 rid = (i << BT_ULSHIFT) | j; 14943 j++; 14944 w >>= 1; 14945 14946 if (rid < SFMMU_MAX_HME_REGIONS) { 14947 rgnp = srdp->srd_hmergnp[rid]; 14948 ASSERT(rgnp->rgn_id == rid); 14949 ASSERT(rgnp->rgn_refcnt > 0); 14950 sfmmu_unlink_from_hmeregion(scsfmmup, 14951 rgnp); 14952 14953 } else { 14954 sfmmu_t *ism_hatid = NULL; 14955 ism_ment_t *ism_ment; 14956 rid -= SFMMU_MAX_HME_REGIONS; 14957 rgnp = srdp->srd_ismrgnp[rid]; 14958 ASSERT(rgnp->rgn_id == rid); 14959 ASSERT(rgnp->rgn_refcnt > 0); 14960 14961 ism_hatid = (sfmmu_t *)rgnp->rgn_obj; 14962 ASSERT(ism_hatid->sfmmu_ismhat); 14963 ism_ment = &scdp->scd_ism_links[rid]; 14964 ASSERT(ism_ment->iment_hat == scdp->scd_sfmmup); 14965 ASSERT(ism_ment->iment_base_va == 14966 rgnp->rgn_saddr); 14967 ism_ment->iment_hat = NULL; 14968 ism_ment->iment_base_va = 0; 14969 mutex_enter(&ism_mlist_lock); 14970 iment_sub(ism_ment, ism_hatid); 14971 mutex_exit(&ism_mlist_lock); 14972 14973 } 14974 } 14975 } 14976 } 14977 /* 14978 * Allocates and initialises a new SCD structure, this is called with 14979 * the srd_scd_mutex held and returns with the reference count 14980 * initialised to 1. 14981 */ 14982 static sf_scd_t * 14983 sfmmu_alloc_scd(sf_srd_t *srdp, sf_region_map_t *new_map) 14984 { 14985 sf_scd_t *new_scdp; 14986 sfmmu_t *scsfmmup; 14987 int i; 14988 14989 ASSERT(MUTEX_HELD(&srdp->srd_scd_mutex)); 14990 new_scdp = kmem_cache_alloc(scd_cache, KM_SLEEP); 14991 14992 scsfmmup = kmem_cache_alloc(sfmmuid_cache, KM_SLEEP); 14993 new_scdp->scd_sfmmup = scsfmmup; 14994 scsfmmup->sfmmu_srdp = srdp; 14995 scsfmmup->sfmmu_scdp = new_scdp; 14996 scsfmmup->sfmmu_tsb0_4minflcnt = 0; 14997 scsfmmup->sfmmu_scdhat = 1; 14998 CPUSET_ALL(scsfmmup->sfmmu_cpusran); 14999 bzero(scsfmmup->sfmmu_hmeregion_links, SFMMU_L1_HMERLINKS_SIZE); 15000 15001 ASSERT(max_mmu_ctxdoms > 0); 15002 for (i = 0; i < max_mmu_ctxdoms; i++) { 15003 scsfmmup->sfmmu_ctxs[i].cnum = INVALID_CONTEXT; 15004 scsfmmup->sfmmu_ctxs[i].gnum = 0; 15005 } 15006 15007 for (i = 0; i < MMU_PAGE_SIZES; i++) { 15008 new_scdp->scd_rttecnt[i] = 0; 15009 } 15010 15011 new_scdp->scd_region_map = *new_map; 15012 new_scdp->scd_refcnt = 1; 15013 if (sfmmu_alloc_scd_tsbs(srdp, new_scdp) != TSB_SUCCESS) { 15014 kmem_cache_free(scd_cache, new_scdp); 15015 kmem_cache_free(sfmmuid_cache, scsfmmup); 15016 return (NULL); 15017 } 15018 return (new_scdp); 15019 } 15020 15021 /* 15022 * The first phase of a process joining an SCD. The hat structure is 15023 * linked to the SCD queue and then the HAT_JOIN_SCD sfmmu flag is set 15024 * and a cross-call with context invalidation is used to cause the 15025 * remaining work to be carried out in the sfmmu_tsbmiss_exception() 15026 * routine. 15027 */ 15028 static void 15029 sfmmu_join_scd(sf_scd_t *scdp, sfmmu_t *sfmmup) 15030 { 15031 hatlock_t *hatlockp; 15032 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 15033 int i; 15034 sf_scd_t *old_scdp; 15035 15036 ASSERT(srdp != NULL); 15037 ASSERT(scdp != NULL); 15038 ASSERT(scdp->scd_refcnt > 0); 15039 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 15040 15041 if ((old_scdp = sfmmup->sfmmu_scdp) != NULL) { 15042 ASSERT(old_scdp != scdp); 15043 15044 mutex_enter(&old_scdp->scd_mutex); 15045 sfmmu_from_scd_list(&old_scdp->scd_sf_list, sfmmup); 15046 mutex_exit(&old_scdp->scd_mutex); 15047 /* 15048 * sfmmup leaves the old scd. Update sfmmu_ttecnt to 15049 * include the shme rgn ttecnt for rgns that 15050 * were in the old SCD 15051 */ 15052 for (i = 0; i < mmu_page_sizes; i++) { 15053 ASSERT(sfmmup->sfmmu_scdrttecnt[i] == 15054 old_scdp->scd_rttecnt[i]); 15055 atomic_add_long(&sfmmup->sfmmu_ttecnt[i], 15056 sfmmup->sfmmu_scdrttecnt[i]); 15057 } 15058 } 15059 15060 /* 15061 * Move sfmmu to the scd lists. 15062 */ 15063 mutex_enter(&scdp->scd_mutex); 15064 sfmmu_to_scd_list(&scdp->scd_sf_list, sfmmup); 15065 mutex_exit(&scdp->scd_mutex); 15066 SF_SCD_INCR_REF(scdp); 15067 15068 hatlockp = sfmmu_hat_enter(sfmmup); 15069 /* 15070 * For a multi-thread process, we must stop 15071 * all the other threads before joining the scd. 15072 */ 15073 15074 SFMMU_FLAGS_SET(sfmmup, HAT_JOIN_SCD); 15075 15076 sfmmu_invalidate_ctx(sfmmup); 15077 sfmmup->sfmmu_scdp = scdp; 15078 15079 /* 15080 * Copy scd_rttecnt into sfmmup's sfmmu_scdrttecnt, and update 15081 * sfmmu_ttecnt to not include the rgn ttecnt just joined in SCD. 15082 */ 15083 for (i = 0; i < mmu_page_sizes; i++) { 15084 sfmmup->sfmmu_scdrttecnt[i] = scdp->scd_rttecnt[i]; 15085 ASSERT(sfmmup->sfmmu_ttecnt[i] >= scdp->scd_rttecnt[i]); 15086 atomic_add_long(&sfmmup->sfmmu_ttecnt[i], 15087 -sfmmup->sfmmu_scdrttecnt[i]); 15088 } 15089 /* update tsb0 inflation count */ 15090 if (old_scdp != NULL) { 15091 sfmmup->sfmmu_tsb0_4minflcnt += 15092 old_scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt; 15093 } 15094 ASSERT(sfmmup->sfmmu_tsb0_4minflcnt >= 15095 scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt); 15096 sfmmup->sfmmu_tsb0_4minflcnt -= scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt; 15097 15098 sfmmu_hat_exit(hatlockp); 15099 15100 if (old_scdp != NULL) { 15101 SF_SCD_DECR_REF(srdp, old_scdp); 15102 } 15103 15104 } 15105 15106 /* 15107 * This routine is called by a process to become part of an SCD. It is called 15108 * from sfmmu_tsbmiss_exception() once most of the initial work has been 15109 * done by sfmmu_join_scd(). This routine must not drop the hat lock. 15110 */ 15111 static void 15112 sfmmu_finish_join_scd(sfmmu_t *sfmmup) 15113 { 15114 struct tsb_info *tsbinfop; 15115 15116 ASSERT(sfmmu_hat_lock_held(sfmmup)); 15117 ASSERT(sfmmup->sfmmu_scdp != NULL); 15118 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)); 15119 ASSERT(!SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)); 15120 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ALLCTX_INVALID)); 15121 15122 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL; 15123 tsbinfop = tsbinfop->tsb_next) { 15124 if (tsbinfop->tsb_flags & TSB_SWAPPED) { 15125 continue; 15126 } 15127 ASSERT(!(tsbinfop->tsb_flags & TSB_RELOC_FLAG)); 15128 15129 sfmmu_inv_tsb(tsbinfop->tsb_va, 15130 TSB_BYTES(tsbinfop->tsb_szc)); 15131 } 15132 15133 /* Set HAT_CTX1_FLAG for all SCD ISMs */ 15134 sfmmu_ism_hatflags(sfmmup, 1); 15135 15136 SFMMU_STAT(sf_join_scd); 15137 } 15138 15139 /* 15140 * This routine is called in order to check if there is an SCD which matches 15141 * the process's region map if not then a new SCD may be created. 15142 */ 15143 static void 15144 sfmmu_find_scd(sfmmu_t *sfmmup) 15145 { 15146 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 15147 sf_scd_t *scdp, *new_scdp; 15148 int ret; 15149 15150 ASSERT(srdp != NULL); 15151 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 15152 15153 mutex_enter(&srdp->srd_scd_mutex); 15154 for (scdp = srdp->srd_scdp; scdp != NULL; 15155 scdp = scdp->scd_next) { 15156 SF_RGNMAP_EQUAL(&scdp->scd_region_map, 15157 &sfmmup->sfmmu_region_map, ret); 15158 if (ret == 1) { 15159 SF_SCD_INCR_REF(scdp); 15160 mutex_exit(&srdp->srd_scd_mutex); 15161 sfmmu_join_scd(scdp, sfmmup); 15162 ASSERT(scdp->scd_refcnt >= 2); 15163 atomic_add_32((volatile uint32_t *) 15164 &scdp->scd_refcnt, -1); 15165 return; 15166 } else { 15167 /* 15168 * If the sfmmu region map is a subset of the scd 15169 * region map, then the assumption is that this process 15170 * will continue attaching to ISM segments until the 15171 * region maps are equal. 15172 */ 15173 SF_RGNMAP_IS_SUBSET(&scdp->scd_region_map, 15174 &sfmmup->sfmmu_region_map, ret); 15175 if (ret == 1) { 15176 mutex_exit(&srdp->srd_scd_mutex); 15177 return; 15178 } 15179 } 15180 } 15181 15182 ASSERT(scdp == NULL); 15183 /* 15184 * No matching SCD has been found, create a new one. 15185 */ 15186 if ((new_scdp = sfmmu_alloc_scd(srdp, &sfmmup->sfmmu_region_map)) == 15187 NULL) { 15188 mutex_exit(&srdp->srd_scd_mutex); 15189 return; 15190 } 15191 15192 /* 15193 * sfmmu_alloc_scd() returns with a ref count of 1 on the scd. 15194 */ 15195 15196 /* Set scd_rttecnt for shme rgns in SCD */ 15197 sfmmu_set_scd_rttecnt(srdp, new_scdp); 15198 15199 /* 15200 * Link scd onto srd_scdp list and scd sfmmu onto region/iment lists. 15201 */ 15202 sfmmu_link_scd_to_regions(srdp, new_scdp); 15203 sfmmu_add_scd(&srdp->srd_scdp, new_scdp); 15204 SFMMU_STAT_ADD(sf_create_scd, 1); 15205 15206 mutex_exit(&srdp->srd_scd_mutex); 15207 sfmmu_join_scd(new_scdp, sfmmup); 15208 ASSERT(new_scdp->scd_refcnt >= 2); 15209 atomic_add_32((volatile uint32_t *)&new_scdp->scd_refcnt, -1); 15210 } 15211 15212 /* 15213 * This routine is called by a process to remove itself from an SCD. It is 15214 * either called when the processes has detached from a segment or from 15215 * hat_free_start() as a result of calling exit. 15216 */ 15217 static void 15218 sfmmu_leave_scd(sfmmu_t *sfmmup, uchar_t r_type) 15219 { 15220 sf_scd_t *scdp = sfmmup->sfmmu_scdp; 15221 sf_srd_t *srdp = sfmmup->sfmmu_srdp; 15222 hatlock_t *hatlockp = TSB_HASH(sfmmup); 15223 int i; 15224 15225 ASSERT(scdp != NULL); 15226 ASSERT(srdp != NULL); 15227 15228 if (sfmmup->sfmmu_free) { 15229 /* 15230 * If the process is part of an SCD the sfmmu is unlinked 15231 * from scd_sf_list. 15232 */ 15233 mutex_enter(&scdp->scd_mutex); 15234 sfmmu_from_scd_list(&scdp->scd_sf_list, sfmmup); 15235 mutex_exit(&scdp->scd_mutex); 15236 /* 15237 * Update sfmmu_ttecnt to include the rgn ttecnt for rgns that 15238 * are about to leave the SCD 15239 */ 15240 for (i = 0; i < mmu_page_sizes; i++) { 15241 ASSERT(sfmmup->sfmmu_scdrttecnt[i] == 15242 scdp->scd_rttecnt[i]); 15243 atomic_add_long(&sfmmup->sfmmu_ttecnt[i], 15244 sfmmup->sfmmu_scdrttecnt[i]); 15245 sfmmup->sfmmu_scdrttecnt[i] = 0; 15246 } 15247 sfmmup->sfmmu_scdp = NULL; 15248 15249 SF_SCD_DECR_REF(srdp, scdp); 15250 return; 15251 } 15252 15253 ASSERT(r_type != SFMMU_REGION_ISM || 15254 SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)); 15255 ASSERT(scdp->scd_refcnt); 15256 ASSERT(!sfmmup->sfmmu_free); 15257 ASSERT(sfmmu_hat_lock_held(sfmmup)); 15258 ASSERT(AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock)); 15259 15260 /* 15261 * Wait for ISM maps to be updated. 15262 */ 15263 if (r_type != SFMMU_REGION_ISM) { 15264 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY) && 15265 sfmmup->sfmmu_scdp != NULL) { 15266 cv_wait(&sfmmup->sfmmu_tsb_cv, 15267 HATLOCK_MUTEXP(hatlockp)); 15268 } 15269 15270 if (sfmmup->sfmmu_scdp == NULL) { 15271 sfmmu_hat_exit(hatlockp); 15272 return; 15273 } 15274 SFMMU_FLAGS_SET(sfmmup, HAT_ISMBUSY); 15275 } 15276 15277 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)) { 15278 SFMMU_FLAGS_CLEAR(sfmmup, HAT_JOIN_SCD); 15279 /* 15280 * Since HAT_JOIN_SCD was set our context 15281 * is still invalid. 15282 */ 15283 } else { 15284 /* 15285 * For a multi-thread process, we must stop 15286 * all the other threads before leaving the scd. 15287 */ 15288 15289 sfmmu_invalidate_ctx(sfmmup); 15290 } 15291 15292 /* Clear all the rid's for ISM, delete flags, etc */ 15293 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)); 15294 sfmmu_ism_hatflags(sfmmup, 0); 15295 15296 /* 15297 * Update sfmmu_ttecnt to include the rgn ttecnt for rgns that 15298 * are in SCD before this sfmmup leaves the SCD. 15299 */ 15300 for (i = 0; i < mmu_page_sizes; i++) { 15301 ASSERT(sfmmup->sfmmu_scdrttecnt[i] == 15302 scdp->scd_rttecnt[i]); 15303 atomic_add_long(&sfmmup->sfmmu_ttecnt[i], 15304 sfmmup->sfmmu_scdrttecnt[i]); 15305 sfmmup->sfmmu_scdrttecnt[i] = 0; 15306 /* update ismttecnt to include SCD ism before hat leaves SCD */ 15307 sfmmup->sfmmu_ismttecnt[i] += sfmmup->sfmmu_scdismttecnt[i]; 15308 sfmmup->sfmmu_scdismttecnt[i] = 0; 15309 } 15310 /* update tsb0 inflation count */ 15311 sfmmup->sfmmu_tsb0_4minflcnt += scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt; 15312 15313 if (r_type != SFMMU_REGION_ISM) { 15314 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ISMBUSY); 15315 } 15316 sfmmup->sfmmu_scdp = NULL; 15317 15318 sfmmu_hat_exit(hatlockp); 15319 15320 /* 15321 * Unlink sfmmu from scd_sf_list this can be done without holding 15322 * the hat lock as we hold the sfmmu_as lock which prevents 15323 * hat_join_region from adding this thread to the scd again. Other 15324 * threads check if sfmmu_scdp is NULL under hat lock and if it's NULL 15325 * they won't get here, since sfmmu_leave_scd() clears sfmmu_scdp 15326 * while holding the hat lock. 15327 */ 15328 mutex_enter(&scdp->scd_mutex); 15329 sfmmu_from_scd_list(&scdp->scd_sf_list, sfmmup); 15330 mutex_exit(&scdp->scd_mutex); 15331 SFMMU_STAT(sf_leave_scd); 15332 15333 SF_SCD_DECR_REF(srdp, scdp); 15334 hatlockp = sfmmu_hat_enter(sfmmup); 15335 15336 } 15337 15338 /* 15339 * Unlink and free up an SCD structure with a reference count of 0. 15340 */ 15341 static void 15342 sfmmu_destroy_scd(sf_srd_t *srdp, sf_scd_t *scdp, sf_region_map_t *scd_rmap) 15343 { 15344 sfmmu_t *scsfmmup; 15345 sf_scd_t *sp; 15346 hatlock_t *shatlockp; 15347 int i, ret; 15348 15349 mutex_enter(&srdp->srd_scd_mutex); 15350 for (sp = srdp->srd_scdp; sp != NULL; sp = sp->scd_next) { 15351 if (sp == scdp) 15352 break; 15353 } 15354 if (sp == NULL || sp->scd_refcnt) { 15355 mutex_exit(&srdp->srd_scd_mutex); 15356 return; 15357 } 15358 15359 /* 15360 * It is possible that the scd has been freed and reallocated with a 15361 * different region map while we've been waiting for the srd_scd_mutex. 15362 */ 15363 SF_RGNMAP_EQUAL(scd_rmap, &sp->scd_region_map, ret); 15364 if (ret != 1) { 15365 mutex_exit(&srdp->srd_scd_mutex); 15366 return; 15367 } 15368 15369 ASSERT(scdp->scd_sf_list == NULL); 15370 /* 15371 * Unlink scd from srd_scdp list. 15372 */ 15373 sfmmu_remove_scd(&srdp->srd_scdp, scdp); 15374 mutex_exit(&srdp->srd_scd_mutex); 15375 15376 sfmmu_unlink_scd_from_regions(srdp, scdp); 15377 15378 /* Clear shared context tsb and release ctx */ 15379 scsfmmup = scdp->scd_sfmmup; 15380 15381 /* 15382 * create a barrier so that scd will not be destroyed 15383 * if other thread still holds the same shared hat lock. 15384 * E.g., sfmmu_tsbmiss_exception() needs to acquire the 15385 * shared hat lock before checking the shared tsb reloc flag. 15386 */ 15387 shatlockp = sfmmu_hat_enter(scsfmmup); 15388 sfmmu_hat_exit(shatlockp); 15389 15390 sfmmu_free_scd_tsbs(scsfmmup); 15391 15392 for (i = 0; i < SFMMU_L1_HMERLINKS; i++) { 15393 if (scsfmmup->sfmmu_hmeregion_links[i] != NULL) { 15394 kmem_free(scsfmmup->sfmmu_hmeregion_links[i], 15395 SFMMU_L2_HMERLINKS_SIZE); 15396 scsfmmup->sfmmu_hmeregion_links[i] = NULL; 15397 } 15398 } 15399 kmem_cache_free(sfmmuid_cache, scsfmmup); 15400 kmem_cache_free(scd_cache, scdp); 15401 SFMMU_STAT(sf_destroy_scd); 15402 } 15403 15404 /* 15405 * Modifies the HAT_CTX1_FLAG for each of the ISM segments which correspond to 15406 * bits which are set in the ism_region_map parameter. This flag indicates to 15407 * the tsbmiss handler that mapping for these segments should be loaded using 15408 * the shared context. 15409 */ 15410 static void 15411 sfmmu_ism_hatflags(sfmmu_t *sfmmup, int addflag) 15412 { 15413 sf_scd_t *scdp = sfmmup->sfmmu_scdp; 15414 ism_blk_t *ism_blkp; 15415 ism_map_t *ism_map; 15416 int i, rid; 15417 15418 ASSERT(sfmmup->sfmmu_iblk != NULL); 15419 ASSERT(scdp != NULL); 15420 /* 15421 * Note that the caller either set HAT_ISMBUSY flag or checked 15422 * under hat lock that HAT_ISMBUSY was not set by another thread. 15423 */ 15424 ASSERT(sfmmu_hat_lock_held(sfmmup)); 15425 15426 ism_blkp = sfmmup->sfmmu_iblk; 15427 while (ism_blkp != NULL) { 15428 ism_map = ism_blkp->iblk_maps; 15429 for (i = 0; ism_map[i].imap_ismhat && i < ISM_MAP_SLOTS; i++) { 15430 rid = ism_map[i].imap_rid; 15431 if (rid == SFMMU_INVALID_ISMRID) { 15432 continue; 15433 } 15434 ASSERT(rid >= 0 && rid < SFMMU_MAX_ISM_REGIONS); 15435 if (SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid) && 15436 addflag) { 15437 ism_map[i].imap_hatflags |= 15438 HAT_CTX1_FLAG; 15439 } else { 15440 ism_map[i].imap_hatflags &= 15441 ~HAT_CTX1_FLAG; 15442 } 15443 } 15444 ism_blkp = ism_blkp->iblk_next; 15445 } 15446 } 15447 15448 static int 15449 sfmmu_srd_lock_held(sf_srd_t *srdp) 15450 { 15451 return (MUTEX_HELD(&srdp->srd_mutex)); 15452 } 15453 15454 /* ARGSUSED */ 15455 static int 15456 sfmmu_scdcache_constructor(void *buf, void *cdrarg, int kmflags) 15457 { 15458 sf_scd_t *scdp = (sf_scd_t *)buf; 15459 15460 bzero(buf, sizeof (sf_scd_t)); 15461 mutex_init(&scdp->scd_mutex, NULL, MUTEX_DEFAULT, NULL); 15462 return (0); 15463 } 15464 15465 /* ARGSUSED */ 15466 static void 15467 sfmmu_scdcache_destructor(void *buf, void *cdrarg) 15468 { 15469 sf_scd_t *scdp = (sf_scd_t *)buf; 15470 15471 mutex_destroy(&scdp->scd_mutex); 15472 } 15473