Print this page
patch vm-cleanup
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/sfmmu/vm/hat_sfmmu.c
+++ new/usr/src/uts/sfmmu/vm/hat_sfmmu.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 */
24 24 /*
25 25 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 26 */
27 27
28 28 /*
29 29 * VM - Hardware Address Translation management for Spitfire MMU.
30 30 *
31 31 * This file implements the machine specific hardware translation
32 32 * needed by the VM system. The machine independent interface is
33 33 * described in <vm/hat.h> while the machine dependent interface
34 34 * and data structures are described in <vm/hat_sfmmu.h>.
35 35 *
36 36 * The hat layer manages the address translation hardware as a cache
37 37 * driven by calls from the higher levels in the VM system.
38 38 */
39 39
40 40 #include <sys/types.h>
41 41 #include <sys/kstat.h>
42 42 #include <vm/hat.h>
43 43 #include <vm/hat_sfmmu.h>
44 44 #include <vm/page.h>
45 45 #include <sys/pte.h>
46 46 #include <sys/systm.h>
47 47 #include <sys/mman.h>
48 48 #include <sys/sysmacros.h>
49 49 #include <sys/machparam.h>
50 50 #include <sys/vtrace.h>
51 51 #include <sys/kmem.h>
52 52 #include <sys/mmu.h>
53 53 #include <sys/cmn_err.h>
54 54 #include <sys/cpu.h>
55 55 #include <sys/cpuvar.h>
56 56 #include <sys/debug.h>
57 57 #include <sys/lgrp.h>
58 58 #include <sys/archsystm.h>
59 59 #include <sys/machsystm.h>
60 60 #include <sys/vmsystm.h>
61 61 #include <vm/as.h>
62 62 #include <vm/seg.h>
63 63 #include <vm/seg_kp.h>
64 64 #include <vm/seg_kmem.h>
65 65 #include <vm/seg_kpm.h>
66 66 #include <vm/rm.h>
67 67 #include <sys/t_lock.h>
68 68 #include <sys/obpdefs.h>
69 69 #include <sys/vm_machparam.h>
70 70 #include <sys/var.h>
71 71 #include <sys/trap.h>
72 72 #include <sys/machtrap.h>
73 73 #include <sys/scb.h>
74 74 #include <sys/bitmap.h>
75 75 #include <sys/machlock.h>
76 76 #include <sys/membar.h>
77 77 #include <sys/atomic.h>
78 78 #include <sys/cpu_module.h>
79 79 #include <sys/prom_debug.h>
80 80 #include <sys/ksynch.h>
81 81 #include <sys/mem_config.h>
82 82 #include <sys/mem_cage.h>
83 83 #include <vm/vm_dep.h>
84 84 #include <vm/xhat_sfmmu.h>
85 85 #include <sys/fpu/fpusystm.h>
86 86 #include <vm/mach_kpm.h>
87 87 #include <sys/callb.h>
88 88
89 89 #ifdef DEBUG
90 90 #define SFMMU_VALIDATE_HMERID(hat, rid, saddr, len) \
91 91 if (SFMMU_IS_SHMERID_VALID(rid)) { \
92 92 caddr_t _eaddr = (saddr) + (len); \
93 93 sf_srd_t *_srdp; \
94 94 sf_region_t *_rgnp; \
95 95 ASSERT((rid) < SFMMU_MAX_HME_REGIONS); \
96 96 ASSERT(SF_RGNMAP_TEST(hat->sfmmu_hmeregion_map, rid)); \
97 97 ASSERT((hat) != ksfmmup); \
98 98 _srdp = (hat)->sfmmu_srdp; \
99 99 ASSERT(_srdp != NULL); \
100 100 ASSERT(_srdp->srd_refcnt != 0); \
101 101 _rgnp = _srdp->srd_hmergnp[(rid)]; \
102 102 ASSERT(_rgnp != NULL && _rgnp->rgn_id == rid); \
103 103 ASSERT(_rgnp->rgn_refcnt != 0); \
104 104 ASSERT(!(_rgnp->rgn_flags & SFMMU_REGION_FREE)); \
105 105 ASSERT((_rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == \
106 106 SFMMU_REGION_HME); \
107 107 ASSERT((saddr) >= _rgnp->rgn_saddr); \
108 108 ASSERT((saddr) < _rgnp->rgn_saddr + _rgnp->rgn_size); \
109 109 ASSERT(_eaddr > _rgnp->rgn_saddr); \
110 110 ASSERT(_eaddr <= _rgnp->rgn_saddr + _rgnp->rgn_size); \
111 111 }
112 112
113 113 #define SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid) \
114 114 { \
115 115 caddr_t _hsva; \
116 116 caddr_t _heva; \
117 117 caddr_t _rsva; \
118 118 caddr_t _reva; \
119 119 int _ttesz = get_hblk_ttesz(hmeblkp); \
120 120 int _flagtte; \
121 121 ASSERT((srdp)->srd_refcnt != 0); \
122 122 ASSERT((rid) < SFMMU_MAX_HME_REGIONS); \
123 123 ASSERT((rgnp)->rgn_id == rid); \
124 124 ASSERT(!((rgnp)->rgn_flags & SFMMU_REGION_FREE)); \
125 125 ASSERT(((rgnp)->rgn_flags & SFMMU_REGION_TYPE_MASK) == \
126 126 SFMMU_REGION_HME); \
127 127 ASSERT(_ttesz <= (rgnp)->rgn_pgszc); \
128 128 _hsva = (caddr_t)get_hblk_base(hmeblkp); \
129 129 _heva = get_hblk_endaddr(hmeblkp); \
130 130 _rsva = (caddr_t)P2ALIGN( \
131 131 (uintptr_t)(rgnp)->rgn_saddr, HBLK_MIN_BYTES); \
132 132 _reva = (caddr_t)P2ROUNDUP( \
133 133 (uintptr_t)((rgnp)->rgn_saddr + (rgnp)->rgn_size), \
134 134 HBLK_MIN_BYTES); \
135 135 ASSERT(_hsva >= _rsva); \
136 136 ASSERT(_hsva < _reva); \
137 137 ASSERT(_heva > _rsva); \
138 138 ASSERT(_heva <= _reva); \
139 139 _flagtte = (_ttesz < HBLK_MIN_TTESZ) ? HBLK_MIN_TTESZ : \
140 140 _ttesz; \
141 141 ASSERT(rgnp->rgn_hmeflags & (0x1 << _flagtte)); \
142 142 }
143 143
144 144 #else /* DEBUG */
145 145 #define SFMMU_VALIDATE_HMERID(hat, rid, addr, len)
146 146 #define SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid)
147 147 #endif /* DEBUG */
148 148
149 149 #if defined(SF_ERRATA_57)
150 150 extern caddr_t errata57_limit;
151 151 #endif
152 152
153 153 #define HME8BLK_SZ_RND ((roundup(HME8BLK_SZ, sizeof (int64_t))) / \
154 154 (sizeof (int64_t)))
155 155 #define HBLK_RESERVE ((struct hme_blk *)hblk_reserve)
156 156
157 157 #define HBLK_RESERVE_CNT 128
158 158 #define HBLK_RESERVE_MIN 20
159 159
160 160 static struct hme_blk *freehblkp;
161 161 static kmutex_t freehblkp_lock;
162 162 static int freehblkcnt;
163 163
164 164 static int64_t hblk_reserve[HME8BLK_SZ_RND];
165 165 static kmutex_t hblk_reserve_lock;
166 166 static kthread_t *hblk_reserve_thread;
167 167
168 168 static nucleus_hblk8_info_t nucleus_hblk8;
169 169 static nucleus_hblk1_info_t nucleus_hblk1;
170 170
171 171 /*
172 172 * Data to manage per-cpu hmeblk pending queues, hmeblks are queued here
173 173 * after the initial phase of removing an hmeblk from the hash chain, see
174 174 * the detailed comment in sfmmu_hblk_hash_rm() for further details.
175 175 */
176 176 static cpu_hme_pend_t *cpu_hme_pend;
177 177 static uint_t cpu_hme_pend_thresh;
178 178 /*
179 179 * SFMMU specific hat functions
180 180 */
181 181 void hat_pagecachectl(struct page *, int);
182 182
183 183 /* flags for hat_pagecachectl */
184 184 #define HAT_CACHE 0x1
185 185 #define HAT_UNCACHE 0x2
186 186 #define HAT_TMPNC 0x4
187 187
188 188 /*
189 189 * Flag to allow the creation of non-cacheable translations
190 190 * to system memory. It is off by default. At the moment this
191 191 * flag is used by the ecache error injector. The error injector
192 192 * will turn it on when creating such a translation then shut it
193 193 * off when it's finished.
194 194 */
195 195
196 196 int sfmmu_allow_nc_trans = 0;
197 197
198 198 /*
199 199 * Flag to disable large page support.
200 200 * value of 1 => disable all large pages.
201 201 * bits 1, 2, and 3 are to disable 64K, 512K and 4M pages respectively.
202 202 *
203 203 * For example, use the value 0x4 to disable 512K pages.
204 204 *
205 205 */
206 206 #define LARGE_PAGES_OFF 0x1
207 207
208 208 /*
209 209 * The disable_large_pages and disable_ism_large_pages variables control
210 210 * hat_memload_array and the page sizes to be used by ISM and the kernel.
211 211 *
212 212 * The disable_auto_data_large_pages and disable_auto_text_large_pages variables
213 213 * are only used to control which OOB pages to use at upper VM segment creation
214 214 * time, and are set in hat_init_pagesizes and used in the map_pgsz* routines.
215 215 * Their values may come from platform or CPU specific code to disable page
216 216 * sizes that should not be used.
217 217 *
218 218 * WARNING: 512K pages are currently not supported for ISM/DISM.
219 219 */
220 220 uint_t disable_large_pages = 0;
221 221 uint_t disable_ism_large_pages = (1 << TTE512K);
222 222 uint_t disable_auto_data_large_pages = 0;
223 223 uint_t disable_auto_text_large_pages = 0;
224 224
225 225 /*
226 226 * Private sfmmu data structures for hat management
227 227 */
228 228 static struct kmem_cache *sfmmuid_cache;
229 229 static struct kmem_cache *mmuctxdom_cache;
230 230
231 231 /*
232 232 * Private sfmmu data structures for tsb management
233 233 */
234 234 static struct kmem_cache *sfmmu_tsbinfo_cache;
235 235 static struct kmem_cache *sfmmu_tsb8k_cache;
236 236 static struct kmem_cache *sfmmu_tsb_cache[NLGRPS_MAX];
237 237 static vmem_t *kmem_bigtsb_arena;
238 238 static vmem_t *kmem_tsb_arena;
239 239
240 240 /*
241 241 * sfmmu static variables for hmeblk resource management.
242 242 */
243 243 static vmem_t *hat_memload1_arena; /* HAT translation arena for sfmmu1_cache */
244 244 static struct kmem_cache *sfmmu8_cache;
245 245 static struct kmem_cache *sfmmu1_cache;
246 246 static struct kmem_cache *pa_hment_cache;
247 247
248 248 static kmutex_t ism_mlist_lock; /* mutex for ism mapping list */
249 249 /*
250 250 * private data for ism
251 251 */
252 252 static struct kmem_cache *ism_blk_cache;
253 253 static struct kmem_cache *ism_ment_cache;
254 254 #define ISMID_STARTADDR NULL
255 255
256 256 /*
257 257 * Region management data structures and function declarations.
258 258 */
259 259
260 260 static void sfmmu_leave_srd(sfmmu_t *);
261 261 static int sfmmu_srdcache_constructor(void *, void *, int);
262 262 static void sfmmu_srdcache_destructor(void *, void *);
263 263 static int sfmmu_rgncache_constructor(void *, void *, int);
264 264 static void sfmmu_rgncache_destructor(void *, void *);
265 265 static int sfrgnmap_isnull(sf_region_map_t *);
266 266 static int sfhmergnmap_isnull(sf_hmeregion_map_t *);
267 267 static int sfmmu_scdcache_constructor(void *, void *, int);
268 268 static void sfmmu_scdcache_destructor(void *, void *);
269 269 static void sfmmu_rgn_cb_noop(caddr_t, caddr_t, caddr_t,
270 270 size_t, void *, u_offset_t);
271 271
272 272 static uint_t srd_hashmask = SFMMU_MAX_SRD_BUCKETS - 1;
273 273 static sf_srd_bucket_t *srd_buckets;
274 274 static struct kmem_cache *srd_cache;
275 275 static uint_t srd_rgn_hashmask = SFMMU_MAX_REGION_BUCKETS - 1;
276 276 static struct kmem_cache *region_cache;
277 277 static struct kmem_cache *scd_cache;
278 278
279 279 #ifdef sun4v
280 280 int use_bigtsb_arena = 1;
281 281 #else
282 282 int use_bigtsb_arena = 0;
283 283 #endif
284 284
285 285 /* External /etc/system tunable, for turning on&off the shctx support */
286 286 int disable_shctx = 0;
287 287 /* Internal variable, set by MD if the HW supports shctx feature */
288 288 int shctx_on = 0;
289 289
290 290 #ifdef DEBUG
291 291 static void check_scd_sfmmu_list(sfmmu_t **, sfmmu_t *, int);
292 292 #endif
293 293 static void sfmmu_to_scd_list(sfmmu_t **, sfmmu_t *);
294 294 static void sfmmu_from_scd_list(sfmmu_t **, sfmmu_t *);
295 295
296 296 static sf_scd_t *sfmmu_alloc_scd(sf_srd_t *, sf_region_map_t *);
297 297 static void sfmmu_find_scd(sfmmu_t *);
298 298 static void sfmmu_join_scd(sf_scd_t *, sfmmu_t *);
299 299 static void sfmmu_finish_join_scd(sfmmu_t *);
300 300 static void sfmmu_leave_scd(sfmmu_t *, uchar_t);
301 301 static void sfmmu_destroy_scd(sf_srd_t *, sf_scd_t *, sf_region_map_t *);
302 302 static int sfmmu_alloc_scd_tsbs(sf_srd_t *, sf_scd_t *);
303 303 static void sfmmu_free_scd_tsbs(sfmmu_t *);
304 304 static void sfmmu_tsb_inv_ctx(sfmmu_t *);
305 305 static int find_ism_rid(sfmmu_t *, sfmmu_t *, caddr_t, uint_t *);
306 306 static void sfmmu_ism_hatflags(sfmmu_t *, int);
307 307 static int sfmmu_srd_lock_held(sf_srd_t *);
308 308 static void sfmmu_remove_scd(sf_scd_t **, sf_scd_t *);
309 309 static void sfmmu_add_scd(sf_scd_t **headp, sf_scd_t *);
310 310 static void sfmmu_link_scd_to_regions(sf_srd_t *, sf_scd_t *);
311 311 static void sfmmu_unlink_scd_from_regions(sf_srd_t *, sf_scd_t *);
312 312 static void sfmmu_link_to_hmeregion(sfmmu_t *, sf_region_t *);
313 313 static void sfmmu_unlink_from_hmeregion(sfmmu_t *, sf_region_t *);
314 314
315 315 /*
316 316 * ``hat_lock'' is a hashed mutex lock for protecting sfmmu TSB lists,
317 317 * HAT flags, synchronizing TLB/TSB coherency, and context management.
318 318 * The lock is hashed on the sfmmup since the case where we need to lock
319 319 * all processes is rare but does occur (e.g. we need to unload a shared
320 320 * mapping from all processes using the mapping). We have a lot of buckets,
321 321 * and each slab of sfmmu_t's can use about a quarter of them, giving us
322 322 * a fairly good distribution without wasting too much space and overhead
323 323 * when we have to grab them all.
324 324 */
325 325 #define SFMMU_NUM_LOCK 128 /* must be power of two */
326 326 hatlock_t hat_lock[SFMMU_NUM_LOCK];
327 327
328 328 /*
329 329 * Hash algorithm optimized for a small number of slabs.
330 330 * 7 is (highbit((sizeof sfmmu_t)) - 1)
331 331 * This hash algorithm is based upon the knowledge that sfmmu_t's come from a
332 332 * kmem_cache, and thus they will be sequential within that cache. In
333 333 * addition, each new slab will have a different "color" up to cache_maxcolor
334 334 * which will skew the hashing for each successive slab which is allocated.
335 335 * If the size of sfmmu_t changed to a larger size, this algorithm may need
336 336 * to be revisited.
337 337 */
338 338 #define TSB_HASH_SHIFT_BITS (7)
339 339 #define PTR_HASH(x) ((uintptr_t)x >> TSB_HASH_SHIFT_BITS)
340 340
341 341 #ifdef DEBUG
342 342 int tsb_hash_debug = 0;
343 343 #define TSB_HASH(sfmmup) \
344 344 (tsb_hash_debug ? &hat_lock[0] : \
345 345 &hat_lock[PTR_HASH(sfmmup) & (SFMMU_NUM_LOCK-1)])
346 346 #else /* DEBUG */
347 347 #define TSB_HASH(sfmmup) &hat_lock[PTR_HASH(sfmmup) & (SFMMU_NUM_LOCK-1)]
348 348 #endif /* DEBUG */
349 349
350 350
351 351 /* sfmmu_replace_tsb() return codes. */
352 352 typedef enum tsb_replace_rc {
353 353 TSB_SUCCESS,
354 354 TSB_ALLOCFAIL,
355 355 TSB_LOSTRACE,
356 356 TSB_ALREADY_SWAPPED,
357 357 TSB_CANTGROW
358 358 } tsb_replace_rc_t;
359 359
360 360 /*
361 361 * Flags for TSB allocation routines.
362 362 */
363 363 #define TSB_ALLOC 0x01
364 364 #define TSB_FORCEALLOC 0x02
365 365 #define TSB_GROW 0x04
366 366 #define TSB_SHRINK 0x08
367 367 #define TSB_SWAPIN 0x10
368 368
369 369 /*
370 370 * Support for HAT callbacks.
371 371 */
372 372 #define SFMMU_MAX_RELOC_CALLBACKS 10
373 373 int sfmmu_max_cb_id = SFMMU_MAX_RELOC_CALLBACKS;
374 374 static id_t sfmmu_cb_nextid = 0;
375 375 static id_t sfmmu_tsb_cb_id;
376 376 struct sfmmu_callback *sfmmu_cb_table;
377 377
378 378 kmutex_t kpr_mutex;
379 379 kmutex_t kpr_suspendlock;
380 380 kthread_t *kreloc_thread;
381 381
382 382 /*
383 383 * Enable VA->PA translation sanity checking on DEBUG kernels.
384 384 * Disabled by default. This is incompatible with some
385 385 * drivers (error injector, RSM) so if it breaks you get
386 386 * to keep both pieces.
387 387 */
388 388 int hat_check_vtop = 0;
389 389
390 390 /*
391 391 * Private sfmmu routines (prototypes)
392 392 */
393 393 static struct hme_blk *sfmmu_shadow_hcreate(sfmmu_t *, caddr_t, int, uint_t);
394 394 static struct hme_blk *sfmmu_hblk_alloc(sfmmu_t *, caddr_t,
395 395 struct hmehash_bucket *, uint_t, hmeblk_tag, uint_t,
396 396 uint_t);
397 397 static caddr_t sfmmu_hblk_unload(struct hat *, struct hme_blk *, caddr_t,
398 398 caddr_t, demap_range_t *, uint_t);
399 399 static caddr_t sfmmu_hblk_sync(struct hat *, struct hme_blk *, caddr_t,
400 400 caddr_t, int);
401 401 static void sfmmu_hblk_free(struct hme_blk **);
402 402 static void sfmmu_hblks_list_purge(struct hme_blk **, int);
403 403 static uint_t sfmmu_get_free_hblk(struct hme_blk **, uint_t);
404 404 static uint_t sfmmu_put_free_hblk(struct hme_blk *, uint_t);
405 405 static struct hme_blk *sfmmu_hblk_steal(int);
406 406 static int sfmmu_steal_this_hblk(struct hmehash_bucket *,
407 407 struct hme_blk *, uint64_t, struct hme_blk *);
408 408 static caddr_t sfmmu_hblk_unlock(struct hme_blk *, caddr_t, caddr_t);
409 409
410 410 static void hat_do_memload_array(struct hat *, caddr_t, size_t,
411 411 struct page **, uint_t, uint_t, uint_t);
412 412 static void hat_do_memload(struct hat *, caddr_t, struct page *,
413 413 uint_t, uint_t, uint_t);
414 414 static void sfmmu_memload_batchsmall(struct hat *, caddr_t, page_t **,
415 415 uint_t, uint_t, pgcnt_t, uint_t);
416 416 void sfmmu_tteload(struct hat *, tte_t *, caddr_t, page_t *,
417 417 uint_t);
418 418 static int sfmmu_tteload_array(sfmmu_t *, tte_t *, caddr_t, page_t **,
419 419 uint_t, uint_t);
420 420 static struct hmehash_bucket *sfmmu_tteload_acquire_hashbucket(sfmmu_t *,
421 421 caddr_t, int, uint_t);
422 422 static struct hme_blk *sfmmu_tteload_find_hmeblk(sfmmu_t *,
423 423 struct hmehash_bucket *, caddr_t, uint_t, uint_t,
424 424 uint_t);
425 425 static int sfmmu_tteload_addentry(sfmmu_t *, struct hme_blk *, tte_t *,
426 426 caddr_t, page_t **, uint_t, uint_t);
427 427 static void sfmmu_tteload_release_hashbucket(struct hmehash_bucket *);
428 428
429 429 static int sfmmu_pagearray_setup(caddr_t, page_t **, tte_t *, int);
430 430 static pfn_t sfmmu_uvatopfn(caddr_t, sfmmu_t *, tte_t *);
431 431 void sfmmu_memtte(tte_t *, pfn_t, uint_t, int);
432 432 #ifdef VAC
433 433 static void sfmmu_vac_conflict(struct hat *, caddr_t, page_t *);
434 434 static int sfmmu_vacconflict_array(caddr_t, page_t *, int *);
435 435 int tst_tnc(page_t *pp, pgcnt_t);
436 436 void conv_tnc(page_t *pp, int);
437 437 #endif
438 438
439 439 static void sfmmu_get_ctx(sfmmu_t *);
440 440 static void sfmmu_free_sfmmu(sfmmu_t *);
441 441
442 442 static void sfmmu_ttesync(struct hat *, caddr_t, tte_t *, page_t *);
443 443 static void sfmmu_chgattr(struct hat *, caddr_t, size_t, uint_t, int);
444 444
445 445 cpuset_t sfmmu_pageunload(page_t *, struct sf_hment *, int);
446 446 static void hat_pagereload(struct page *, struct page *);
447 447 static cpuset_t sfmmu_pagesync(page_t *, struct sf_hment *, uint_t);
448 448 #ifdef VAC
449 449 void sfmmu_page_cache_array(page_t *, int, int, pgcnt_t);
450 450 static void sfmmu_page_cache(page_t *, int, int, int);
451 451 #endif
452 452
453 453 cpuset_t sfmmu_rgntlb_demap(caddr_t, sf_region_t *,
454 454 struct hme_blk *, int);
455 455 static void sfmmu_tlbcache_demap(caddr_t, sfmmu_t *, struct hme_blk *,
456 456 pfn_t, int, int, int, int);
457 457 static void sfmmu_ismtlbcache_demap(caddr_t, sfmmu_t *, struct hme_blk *,
458 458 pfn_t, int);
459 459 static void sfmmu_tlb_demap(caddr_t, sfmmu_t *, struct hme_blk *, int, int);
460 460 static void sfmmu_tlb_range_demap(demap_range_t *);
461 461 static void sfmmu_invalidate_ctx(sfmmu_t *);
462 462 static void sfmmu_sync_mmustate(sfmmu_t *);
463 463
464 464 static void sfmmu_tsbinfo_setup_phys(struct tsb_info *, pfn_t);
465 465 static int sfmmu_tsbinfo_alloc(struct tsb_info **, int, int, uint_t,
466 466 sfmmu_t *);
467 467 static void sfmmu_tsb_free(struct tsb_info *);
468 468 static void sfmmu_tsbinfo_free(struct tsb_info *);
469 469 static int sfmmu_init_tsbinfo(struct tsb_info *, int, int, uint_t,
470 470 sfmmu_t *);
471 471 static void sfmmu_tsb_chk_reloc(sfmmu_t *, hatlock_t *);
472 472 static void sfmmu_tsb_swapin(sfmmu_t *, hatlock_t *);
473 473 static int sfmmu_select_tsb_szc(pgcnt_t);
474 474 static void sfmmu_mod_tsb(sfmmu_t *, caddr_t, tte_t *, int);
475 475 #define sfmmu_load_tsb(sfmmup, vaddr, tte, szc) \
476 476 sfmmu_mod_tsb(sfmmup, vaddr, tte, szc)
477 477 #define sfmmu_unload_tsb(sfmmup, vaddr, szc) \
478 478 sfmmu_mod_tsb(sfmmup, vaddr, NULL, szc)
479 479 static void sfmmu_copy_tsb(struct tsb_info *, struct tsb_info *);
480 480 static tsb_replace_rc_t sfmmu_replace_tsb(sfmmu_t *, struct tsb_info *, uint_t,
481 481 hatlock_t *, uint_t);
482 482 static void sfmmu_size_tsb(sfmmu_t *, int, uint64_t, uint64_t, int);
483 483
484 484 #ifdef VAC
485 485 void sfmmu_cache_flush(pfn_t, int);
486 486 void sfmmu_cache_flushcolor(int, pfn_t);
487 487 #endif
488 488 static caddr_t sfmmu_hblk_chgattr(sfmmu_t *, struct hme_blk *, caddr_t,
489 489 caddr_t, demap_range_t *, uint_t, int);
490 490
491 491 static uint64_t sfmmu_vtop_attr(uint_t, int mode, tte_t *);
492 492 static uint_t sfmmu_ptov_attr(tte_t *);
493 493 static caddr_t sfmmu_hblk_chgprot(sfmmu_t *, struct hme_blk *, caddr_t,
494 494 caddr_t, demap_range_t *, uint_t);
495 495 static uint_t sfmmu_vtop_prot(uint_t, uint_t *);
496 496 static int sfmmu_idcache_constructor(void *, void *, int);
497 497 static void sfmmu_idcache_destructor(void *, void *);
498 498 static int sfmmu_hblkcache_constructor(void *, void *, int);
499 499 static void sfmmu_hblkcache_destructor(void *, void *);
500 500 static void sfmmu_hblkcache_reclaim(void *);
501 501 static void sfmmu_shadow_hcleanup(sfmmu_t *, struct hme_blk *,
502 502 struct hmehash_bucket *);
503 503 static void sfmmu_hblk_hash_rm(struct hmehash_bucket *, struct hme_blk *,
504 504 struct hme_blk *, struct hme_blk **, int);
505 505 static void sfmmu_hblk_hash_add(struct hmehash_bucket *, struct hme_blk *,
506 506 uint64_t);
507 507 static struct hme_blk *sfmmu_check_pending_hblks(int);
508 508 static void sfmmu_free_hblks(sfmmu_t *, caddr_t, caddr_t, int);
509 509 static void sfmmu_cleanup_rhblk(sf_srd_t *, caddr_t, uint_t, int);
510 510 static void sfmmu_unload_hmeregion_va(sf_srd_t *, uint_t, caddr_t, caddr_t,
511 511 int, caddr_t *);
512 512 static void sfmmu_unload_hmeregion(sf_srd_t *, sf_region_t *);
513 513
514 514 static void sfmmu_rm_large_mappings(page_t *, int);
515 515
516 516 static void hat_lock_init(void);
517 517 static void hat_kstat_init(void);
518 518 static int sfmmu_kstat_percpu_update(kstat_t *ksp, int rw);
519 519 static void sfmmu_set_scd_rttecnt(sf_srd_t *, sf_scd_t *);
520 520 static int sfmmu_is_rgnva(sf_srd_t *, caddr_t, ulong_t, ulong_t);
521 521 static void sfmmu_check_page_sizes(sfmmu_t *, int);
522 522 int fnd_mapping_sz(page_t *);
523 523 static void iment_add(struct ism_ment *, struct hat *);
524 524 static void iment_sub(struct ism_ment *, struct hat *);
525 525 static pgcnt_t ism_tsb_entries(sfmmu_t *, int szc);
526 526 extern void sfmmu_setup_tsbinfo(sfmmu_t *);
527 527 extern void sfmmu_clear_utsbinfo(void);
528 528
529 529 static void sfmmu_ctx_wrap_around(mmu_ctx_t *, boolean_t);
530 530
531 531 extern int vpm_enable;
532 532
533 533 /* kpm globals */
534 534 #ifdef DEBUG
535 535 /*
536 536 * Enable trap level tsbmiss handling
537 537 */
538 538 int kpm_tsbmtl = 1;
539 539
540 540 /*
541 541 * Flush the TLB on kpm mapout. Note: Xcalls are used (again) for the
542 542 * required TLB shootdowns in this case, so handle w/ care. Off by default.
543 543 */
544 544 int kpm_tlb_flush;
545 545 #endif /* DEBUG */
546 546
547 547 static void *sfmmu_vmem_xalloc_aligned_wrapper(vmem_t *, size_t, int);
548 548
549 549 #ifdef DEBUG
550 550 static void sfmmu_check_hblk_flist();
551 551 #endif
552 552
553 553 /*
554 554 * Semi-private sfmmu data structures. Some of them are initialize in
555 555 * startup or in hat_init. Some of them are private but accessed by
556 556 * assembly code or mach_sfmmu.c
557 557 */
558 558 struct hmehash_bucket *uhme_hash; /* user hmeblk hash table */
559 559 struct hmehash_bucket *khme_hash; /* kernel hmeblk hash table */
560 560 uint64_t uhme_hash_pa; /* PA of uhme_hash */
561 561 uint64_t khme_hash_pa; /* PA of khme_hash */
562 562 int uhmehash_num; /* # of buckets in user hash table */
563 563 int khmehash_num; /* # of buckets in kernel hash table */
564 564
565 565 uint_t max_mmu_ctxdoms = 0; /* max context domains in the system */
566 566 mmu_ctx_t **mmu_ctxs_tbl; /* global array of context domains */
567 567 uint64_t mmu_saved_gnum = 0; /* to init incoming MMUs' gnums */
568 568
569 569 #define DEFAULT_NUM_CTXS_PER_MMU 8192
570 570 static uint_t nctxs = DEFAULT_NUM_CTXS_PER_MMU;
571 571
572 572 int cache; /* describes system cache */
573 573
574 574 caddr_t ktsb_base; /* kernel 8k-indexed tsb base address */
575 575 uint64_t ktsb_pbase; /* kernel 8k-indexed tsb phys address */
576 576 int ktsb_szcode; /* kernel 8k-indexed tsb size code */
577 577 int ktsb_sz; /* kernel 8k-indexed tsb size */
578 578
579 579 caddr_t ktsb4m_base; /* kernel 4m-indexed tsb base address */
580 580 uint64_t ktsb4m_pbase; /* kernel 4m-indexed tsb phys address */
581 581 int ktsb4m_szcode; /* kernel 4m-indexed tsb size code */
582 582 int ktsb4m_sz; /* kernel 4m-indexed tsb size */
583 583
584 584 uint64_t kpm_tsbbase; /* kernel seg_kpm 4M TSB base address */
585 585 int kpm_tsbsz; /* kernel seg_kpm 4M TSB size code */
586 586 uint64_t kpmsm_tsbbase; /* kernel seg_kpm 8K TSB base address */
587 587 int kpmsm_tsbsz; /* kernel seg_kpm 8K TSB size code */
588 588
589 589 #ifndef sun4v
590 590 int utsb_dtlb_ttenum = -1; /* index in TLB for utsb locked TTE */
591 591 int utsb4m_dtlb_ttenum = -1; /* index in TLB for 4M TSB TTE */
592 592 int dtlb_resv_ttenum; /* index in TLB of first reserved TTE */
593 593 caddr_t utsb_vabase; /* reserved kernel virtual memory */
594 594 caddr_t utsb4m_vabase; /* for trap handler TSB accesses */
595 595 #endif /* sun4v */
596 596 uint64_t tsb_alloc_bytes = 0; /* bytes allocated to TSBs */
597 597 vmem_t *kmem_tsb_default_arena[NLGRPS_MAX]; /* For dynamic TSBs */
598 598 vmem_t *kmem_bigtsb_default_arena[NLGRPS_MAX]; /* dynamic 256M TSBs */
599 599
600 600 /*
601 601 * Size to use for TSB slabs. Future platforms that support page sizes
602 602 * larger than 4M may wish to change these values, and provide their own
603 603 * assembly macros for building and decoding the TSB base register contents.
604 604 * Note disable_large_pages will override the value set here.
605 605 */
606 606 static uint_t tsb_slab_ttesz = TTE4M;
607 607 size_t tsb_slab_size = MMU_PAGESIZE4M;
608 608 uint_t tsb_slab_shift = MMU_PAGESHIFT4M;
609 609 /* PFN mask for TTE */
610 610 size_t tsb_slab_mask = MMU_PAGEOFFSET4M >> MMU_PAGESHIFT;
611 611
612 612 /*
613 613 * Size to use for TSB slabs. These are used only when 256M tsb arenas
614 614 * exist.
615 615 */
616 616 static uint_t bigtsb_slab_ttesz = TTE256M;
617 617 static size_t bigtsb_slab_size = MMU_PAGESIZE256M;
618 618 static uint_t bigtsb_slab_shift = MMU_PAGESHIFT256M;
619 619 /* 256M page alignment for 8K pfn */
620 620 static size_t bigtsb_slab_mask = MMU_PAGEOFFSET256M >> MMU_PAGESHIFT;
621 621
622 622 /* largest TSB size to grow to, will be smaller on smaller memory systems */
623 623 static int tsb_max_growsize = 0;
624 624
625 625 /*
626 626 * Tunable parameters dealing with TSB policies.
627 627 */
628 628
629 629 /*
630 630 * This undocumented tunable forces all 8K TSBs to be allocated from
631 631 * the kernel heap rather than from the kmem_tsb_default_arena arenas.
632 632 */
633 633 #ifdef DEBUG
634 634 int tsb_forceheap = 0;
635 635 #endif /* DEBUG */
636 636
637 637 /*
638 638 * Decide whether to use per-lgroup arenas, or one global set of
639 639 * TSB arenas. The default is not to break up per-lgroup, since
640 640 * most platforms don't recognize any tangible benefit from it.
641 641 */
642 642 int tsb_lgrp_affinity = 0;
643 643
644 644 /*
645 645 * Used for growing the TSB based on the process RSS.
646 646 * tsb_rss_factor is based on the smallest TSB, and is
647 647 * shifted by the TSB size to determine if we need to grow.
648 648 * The default will grow the TSB if the number of TTEs for
649 649 * this page size exceeds 75% of the number of TSB entries,
650 650 * which should _almost_ eliminate all conflict misses
651 651 * (at the expense of using up lots and lots of memory).
652 652 */
653 653 #define TSB_RSS_FACTOR (TSB_ENTRIES(TSB_MIN_SZCODE) * 0.75)
654 654 #define SFMMU_RSS_TSBSIZE(tsbszc) (tsb_rss_factor << tsbszc)
655 655 #define SELECT_TSB_SIZECODE(pgcnt) ( \
656 656 (enable_tsb_rss_sizing)? sfmmu_select_tsb_szc(pgcnt) : \
657 657 default_tsb_size)
658 658 #define TSB_OK_SHRINK() \
659 659 (tsb_alloc_bytes > tsb_alloc_hiwater || freemem < desfree)
660 660 #define TSB_OK_GROW() \
661 661 (tsb_alloc_bytes < tsb_alloc_hiwater && freemem > desfree)
662 662
663 663 int enable_tsb_rss_sizing = 1;
664 664 int tsb_rss_factor = (int)TSB_RSS_FACTOR;
665 665
666 666 /* which TSB size code to use for new address spaces or if rss sizing off */
667 667 int default_tsb_size = TSB_8K_SZCODE;
668 668
669 669 static uint64_t tsb_alloc_hiwater; /* limit TSB reserved memory */
670 670 uint64_t tsb_alloc_hiwater_factor; /* tsb_alloc_hiwater = physmem / this */
671 671 #define TSB_ALLOC_HIWATER_FACTOR_DEFAULT 32
672 672
673 673 #ifdef DEBUG
674 674 static int tsb_random_size = 0; /* set to 1 to test random tsb sizes on alloc */
675 675 static int tsb_grow_stress = 0; /* if set to 1, keep replacing TSB w/ random */
676 676 static int tsb_alloc_mtbf = 0; /* fail allocation every n attempts */
677 677 static int tsb_alloc_fail_mtbf = 0;
678 678 static int tsb_alloc_count = 0;
679 679 #endif /* DEBUG */
680 680
681 681 /* if set to 1, will remap valid TTEs when growing TSB. */
682 682 int tsb_remap_ttes = 1;
683 683
684 684 /*
685 685 * If we have more than this many mappings, allocate a second TSB.
686 686 * This default is chosen because the I/D fully associative TLBs are
687 687 * assumed to have at least 8 available entries. Platforms with a
688 688 * larger fully-associative TLB could probably override the default.
689 689 */
690 690
691 691 #ifdef sun4v
692 692 int tsb_sectsb_threshold = 0;
693 693 #else
694 694 int tsb_sectsb_threshold = 8;
695 695 #endif
696 696
697 697 /*
698 698 * kstat data
699 699 */
700 700 struct sfmmu_global_stat sfmmu_global_stat;
701 701 struct sfmmu_tsbsize_stat sfmmu_tsbsize_stat;
702 702
703 703 /*
704 704 * Global data
705 705 */
706 706 sfmmu_t *ksfmmup; /* kernel's hat id */
707 707
708 708 #ifdef DEBUG
709 709 static void chk_tte(tte_t *, tte_t *, tte_t *, struct hme_blk *);
710 710 #endif
711 711
712 712 /* sfmmu locking operations */
713 713 static kmutex_t *sfmmu_mlspl_enter(struct page *, int);
714 714 static int sfmmu_mlspl_held(struct page *, int);
715 715
716 716 kmutex_t *sfmmu_page_enter(page_t *);
717 717 void sfmmu_page_exit(kmutex_t *);
718 718 int sfmmu_page_spl_held(struct page *);
719 719
720 720 /* sfmmu internal locking operations - accessed directly */
721 721 static void sfmmu_mlist_reloc_enter(page_t *, page_t *,
722 722 kmutex_t **, kmutex_t **);
723 723 static void sfmmu_mlist_reloc_exit(kmutex_t *, kmutex_t *);
724 724 static hatlock_t *
725 725 sfmmu_hat_enter(sfmmu_t *);
726 726 static hatlock_t *
727 727 sfmmu_hat_tryenter(sfmmu_t *);
728 728 static void sfmmu_hat_exit(hatlock_t *);
729 729 static void sfmmu_hat_lock_all(void);
730 730 static void sfmmu_hat_unlock_all(void);
731 731 static void sfmmu_ismhat_enter(sfmmu_t *, int);
732 732 static void sfmmu_ismhat_exit(sfmmu_t *, int);
733 733
734 734 kpm_hlk_t *kpmp_table;
735 735 uint_t kpmp_table_sz; /* must be a power of 2 */
736 736 uchar_t kpmp_shift;
737 737
738 738 kpm_shlk_t *kpmp_stable;
739 739 uint_t kpmp_stable_sz; /* must be a power of 2 */
740 740
741 741 /*
742 742 * SPL_TABLE_SIZE is 2 * NCPU, but no smaller than 128.
743 743 * SPL_SHIFT is log2(SPL_TABLE_SIZE).
744 744 */
745 745 #if ((2*NCPU_P2) > 128)
746 746 #define SPL_SHIFT ((unsigned)(NCPU_LOG2 + 1))
747 747 #else
748 748 #define SPL_SHIFT 7U
749 749 #endif
750 750 #define SPL_TABLE_SIZE (1U << SPL_SHIFT)
751 751 #define SPL_MASK (SPL_TABLE_SIZE - 1)
752 752
753 753 /*
754 754 * We shift by PP_SHIFT to take care of the low-order 0 bits of a page_t
755 755 * and by multiples of SPL_SHIFT to get as many varied bits as we can.
756 756 */
757 757 #define SPL_INDEX(pp) \
758 758 ((((uintptr_t)(pp) >> PP_SHIFT) ^ \
759 759 ((uintptr_t)(pp) >> (PP_SHIFT + SPL_SHIFT)) ^ \
760 760 ((uintptr_t)(pp) >> (PP_SHIFT + SPL_SHIFT * 2)) ^ \
761 761 ((uintptr_t)(pp) >> (PP_SHIFT + SPL_SHIFT * 3))) & \
762 762 SPL_MASK)
763 763
764 764 #define SPL_HASH(pp) \
765 765 (&sfmmu_page_lock[SPL_INDEX(pp)].pad_mutex)
766 766
767 767 static pad_mutex_t sfmmu_page_lock[SPL_TABLE_SIZE];
768 768
769 769 /* Array of mutexes protecting a page's mapping list and p_nrm field. */
770 770
771 771 #define MML_TABLE_SIZE SPL_TABLE_SIZE
772 772 #define MLIST_HASH(pp) (&mml_table[SPL_INDEX(pp)].pad_mutex)
773 773
774 774 static pad_mutex_t mml_table[MML_TABLE_SIZE];
775 775
776 776 /*
777 777 * hat_unload_callback() will group together callbacks in order
778 778 * to avoid xt_sync() calls. This is the maximum size of the group.
779 779 */
780 780 #define MAX_CB_ADDR 32
781 781
782 782 tte_t hw_tte;
783 783 static ulong_t sfmmu_dmr_maxbit = DMR_MAXBIT;
784 784
785 785 static char *mmu_ctx_kstat_names[] = {
786 786 "mmu_ctx_tsb_exceptions",
787 787 "mmu_ctx_tsb_raise_exception",
788 788 "mmu_ctx_wrap_around",
789 789 };
790 790
791 791 /*
792 792 * Wrapper for vmem_xalloc since vmem_create only allows limited
793 793 * parameters for vm_source_alloc functions. This function allows us
794 794 * to specify alignment consistent with the size of the object being
795 795 * allocated.
796 796 */
797 797 static void *
798 798 sfmmu_vmem_xalloc_aligned_wrapper(vmem_t *vmp, size_t size, int vmflag)
799 799 {
800 800 return (vmem_xalloc(vmp, size, size, 0, 0, NULL, NULL, vmflag));
801 801 }
802 802
803 803 /* Common code for setting tsb_alloc_hiwater. */
804 804 #define SFMMU_SET_TSB_ALLOC_HIWATER(pages) tsb_alloc_hiwater = \
805 805 ptob(pages) / tsb_alloc_hiwater_factor
806 806
807 807 /*
808 808 * Set tsb_max_growsize to allow at most all of physical memory to be mapped by
809 809 * a single TSB. physmem is the number of physical pages so we need physmem 8K
810 810 * TTEs to represent all those physical pages. We round this up by using
811 811 * 1<<highbit(). To figure out which size code to use, remember that the size
812 812 * code is just an amount to shift the smallest TSB size to get the size of
813 813 * this TSB. So we subtract that size, TSB_START_SIZE, from highbit() (or
814 814 * highbit() - 1) to get the size code for the smallest TSB that can represent
815 815 * all of physical memory, while erring on the side of too much.
816 816 *
817 817 * Restrict tsb_max_growsize to make sure that:
818 818 * 1) TSBs can't grow larger than the TSB slab size
819 819 * 2) TSBs can't grow larger than UTSB_MAX_SZCODE.
820 820 */
821 821 #define SFMMU_SET_TSB_MAX_GROWSIZE(pages) { \
822 822 int _i, _szc, _slabszc, _tsbszc; \
823 823 \
824 824 _i = highbit(pages); \
825 825 if ((1 << (_i - 1)) == (pages)) \
826 826 _i--; /* 2^n case, round down */ \
827 827 _szc = _i - TSB_START_SIZE; \
828 828 _slabszc = bigtsb_slab_shift - (TSB_START_SIZE + TSB_ENTRY_SHIFT); \
829 829 _tsbszc = MIN(_szc, _slabszc); \
830 830 tsb_max_growsize = MIN(_tsbszc, UTSB_MAX_SZCODE); \
831 831 }
832 832
833 833 /*
834 834 * Given a pointer to an sfmmu and a TTE size code, return a pointer to the
835 835 * tsb_info which handles that TTE size.
836 836 */
837 837 #define SFMMU_GET_TSBINFO(tsbinfop, sfmmup, tte_szc) { \
838 838 (tsbinfop) = (sfmmup)->sfmmu_tsb; \
839 839 ASSERT(((tsbinfop)->tsb_flags & TSB_SHAREDCTX) || \
840 840 sfmmu_hat_lock_held(sfmmup)); \
841 841 if ((tte_szc) >= TTE4M) { \
842 842 ASSERT((tsbinfop) != NULL); \
843 843 (tsbinfop) = (tsbinfop)->tsb_next; \
844 844 } \
845 845 }
846 846
847 847 /*
848 848 * Macro to use to unload entries from the TSB.
849 849 * It has knowledge of which page sizes get replicated in the TSB
850 850 * and will call the appropriate unload routine for the appropriate size.
851 851 */
852 852 #define SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, ismhat) \
853 853 { \
854 854 int ttesz = get_hblk_ttesz(hmeblkp); \
855 855 if (ttesz == TTE8K || ttesz == TTE4M) { \
856 856 sfmmu_unload_tsb(sfmmup, addr, ttesz); \
857 857 } else { \
858 858 caddr_t sva = ismhat ? addr : \
859 859 (caddr_t)get_hblk_base(hmeblkp); \
860 860 caddr_t eva = sva + get_hblk_span(hmeblkp); \
861 861 ASSERT(addr >= sva && addr < eva); \
862 862 sfmmu_unload_tsb_range(sfmmup, sva, eva, ttesz); \
863 863 } \
864 864 }
865 865
866 866
867 867 /* Update tsb_alloc_hiwater after memory is configured. */
868 868 /*ARGSUSED*/
869 869 static void
870 870 sfmmu_update_post_add(void *arg, pgcnt_t delta_pages)
871 871 {
872 872 /* Assumes physmem has already been updated. */
873 873 SFMMU_SET_TSB_ALLOC_HIWATER(physmem);
874 874 SFMMU_SET_TSB_MAX_GROWSIZE(physmem);
875 875 }
876 876
877 877 /*
878 878 * Update tsb_alloc_hiwater before memory is deleted. We'll do nothing here
879 879 * and update tsb_alloc_hiwater and tsb_max_growsize after the memory is
880 880 * deleted.
881 881 */
882 882 /*ARGSUSED*/
883 883 static int
884 884 sfmmu_update_pre_del(void *arg, pgcnt_t delta_pages)
885 885 {
886 886 return (0);
887 887 }
888 888
889 889 /* Update tsb_alloc_hiwater after memory fails to be unconfigured. */
890 890 /*ARGSUSED*/
891 891 static void
892 892 sfmmu_update_post_del(void *arg, pgcnt_t delta_pages, int cancelled)
893 893 {
894 894 /*
895 895 * Whether the delete was cancelled or not, just go ahead and update
896 896 * tsb_alloc_hiwater and tsb_max_growsize.
897 897 */
898 898 SFMMU_SET_TSB_ALLOC_HIWATER(physmem);
899 899 SFMMU_SET_TSB_MAX_GROWSIZE(physmem);
900 900 }
901 901
902 902 static kphysm_setup_vector_t sfmmu_update_vec = {
903 903 KPHYSM_SETUP_VECTOR_VERSION, /* version */
904 904 sfmmu_update_post_add, /* post_add */
905 905 sfmmu_update_pre_del, /* pre_del */
906 906 sfmmu_update_post_del /* post_del */
907 907 };
908 908
909 909
910 910 /*
911 911 * HME_BLK HASH PRIMITIVES
912 912 */
913 913
914 914 /*
915 915 * Enter a hme on the mapping list for page pp.
916 916 * When large pages are more prevalent in the system we might want to
917 917 * keep the mapping list in ascending order by the hment size. For now,
918 918 * small pages are more frequent, so don't slow it down.
919 919 */
920 920 #define HME_ADD(hme, pp) \
921 921 { \
922 922 ASSERT(sfmmu_mlist_held(pp)); \
923 923 \
924 924 hme->hme_prev = NULL; \
925 925 hme->hme_next = pp->p_mapping; \
926 926 hme->hme_page = pp; \
927 927 if (pp->p_mapping) { \
928 928 ((struct sf_hment *)(pp->p_mapping))->hme_prev = hme;\
929 929 ASSERT(pp->p_share > 0); \
930 930 } else { \
931 931 /* EMPTY */ \
932 932 ASSERT(pp->p_share == 0); \
933 933 } \
934 934 pp->p_mapping = hme; \
935 935 pp->p_share++; \
936 936 }
937 937
938 938 /*
939 939 * Enter a hme on the mapping list for page pp.
940 940 * If we are unmapping a large translation, we need to make sure that the
941 941 * change is reflect in the corresponding bit of the p_index field.
942 942 */
943 943 #define HME_SUB(hme, pp) \
944 944 { \
945 945 ASSERT(sfmmu_mlist_held(pp)); \
946 946 ASSERT(hme->hme_page == pp || IS_PAHME(hme)); \
947 947 \
948 948 if (pp->p_mapping == NULL) { \
949 949 panic("hme_remove - no mappings"); \
950 950 } \
951 951 \
952 952 membar_stst(); /* ensure previous stores finish */ \
953 953 \
954 954 ASSERT(pp->p_share > 0); \
955 955 pp->p_share--; \
956 956 \
957 957 if (hme->hme_prev) { \
958 958 ASSERT(pp->p_mapping != hme); \
959 959 ASSERT(hme->hme_prev->hme_page == pp || \
960 960 IS_PAHME(hme->hme_prev)); \
961 961 hme->hme_prev->hme_next = hme->hme_next; \
962 962 } else { \
963 963 ASSERT(pp->p_mapping == hme); \
964 964 pp->p_mapping = hme->hme_next; \
965 965 ASSERT((pp->p_mapping == NULL) ? \
966 966 (pp->p_share == 0) : 1); \
967 967 } \
968 968 \
969 969 if (hme->hme_next) { \
970 970 ASSERT(hme->hme_next->hme_page == pp || \
971 971 IS_PAHME(hme->hme_next)); \
972 972 hme->hme_next->hme_prev = hme->hme_prev; \
973 973 } \
974 974 \
975 975 /* zero out the entry */ \
976 976 hme->hme_next = NULL; \
977 977 hme->hme_prev = NULL; \
978 978 hme->hme_page = NULL; \
979 979 \
980 980 if (hme_size(hme) > TTE8K) { \
981 981 /* remove mappings for remainder of large pg */ \
982 982 sfmmu_rm_large_mappings(pp, hme_size(hme)); \
983 983 } \
984 984 }
985 985
986 986 /*
987 987 * This function returns the hment given the hme_blk and a vaddr.
988 988 * It assumes addr has already been checked to belong to hme_blk's
989 989 * range.
990 990 */
991 991 #define HBLKTOHME(hment, hmeblkp, addr) \
992 992 { \
993 993 int index; \
994 994 HBLKTOHME_IDX(hment, hmeblkp, addr, index) \
995 995 }
996 996
997 997 /*
998 998 * Version of HBLKTOHME that also returns the index in hmeblkp
999 999 * of the hment.
1000 1000 */
1001 1001 #define HBLKTOHME_IDX(hment, hmeblkp, addr, idx) \
1002 1002 { \
1003 1003 ASSERT(in_hblk_range((hmeblkp), (addr))); \
1004 1004 \
1005 1005 if (get_hblk_ttesz(hmeblkp) == TTE8K) { \
1006 1006 idx = (((uintptr_t)(addr) >> MMU_PAGESHIFT) & (NHMENTS-1)); \
1007 1007 } else \
1008 1008 idx = 0; \
1009 1009 \
1010 1010 (hment) = &(hmeblkp)->hblk_hme[idx]; \
1011 1011 }
1012 1012
1013 1013 /*
1014 1014 * Disable any page sizes not supported by the CPU
1015 1015 */
1016 1016 void
1017 1017 hat_init_pagesizes()
1018 1018 {
1019 1019 int i;
1020 1020
1021 1021 mmu_exported_page_sizes = 0;
1022 1022 for (i = TTE8K; i < max_mmu_page_sizes; i++) {
1023 1023
1024 1024 szc_2_userszc[i] = (uint_t)-1;
1025 1025 userszc_2_szc[i] = (uint_t)-1;
1026 1026
1027 1027 if ((mmu_exported_pagesize_mask & (1 << i)) == 0) {
1028 1028 disable_large_pages |= (1 << i);
1029 1029 } else {
1030 1030 szc_2_userszc[i] = mmu_exported_page_sizes;
1031 1031 userszc_2_szc[mmu_exported_page_sizes] = i;
1032 1032 mmu_exported_page_sizes++;
1033 1033 }
1034 1034 }
1035 1035
1036 1036 disable_ism_large_pages |= disable_large_pages;
1037 1037 disable_auto_data_large_pages = disable_large_pages;
1038 1038 disable_auto_text_large_pages = disable_large_pages;
1039 1039
1040 1040 /*
1041 1041 * Initialize mmu-specific large page sizes.
1042 1042 */
1043 1043 if (&mmu_large_pages_disabled) {
1044 1044 disable_large_pages |= mmu_large_pages_disabled(HAT_LOAD);
1045 1045 disable_ism_large_pages |=
1046 1046 mmu_large_pages_disabled(HAT_LOAD_SHARE);
1047 1047 disable_auto_data_large_pages |=
1048 1048 mmu_large_pages_disabled(HAT_AUTO_DATA);
1049 1049 disable_auto_text_large_pages |=
1050 1050 mmu_large_pages_disabled(HAT_AUTO_TEXT);
1051 1051 }
1052 1052 }
1053 1053
1054 1054 /*
1055 1055 * Initialize the hardware address translation structures.
1056 1056 */
1057 1057 void
1058 1058 hat_init(void)
1059 1059 {
1060 1060 int i;
1061 1061 uint_t sz;
1062 1062 size_t size;
1063 1063
1064 1064 hat_lock_init();
1065 1065 hat_kstat_init();
1066 1066
1067 1067 /*
1068 1068 * Hardware-only bits in a TTE
1069 1069 */
1070 1070 MAKE_TTE_MASK(&hw_tte);
1071 1071
1072 1072 hat_init_pagesizes();
1073 1073
1074 1074 /* Initialize the hash locks */
1075 1075 for (i = 0; i < khmehash_num; i++) {
1076 1076 mutex_init(&khme_hash[i].hmehash_mutex, NULL,
1077 1077 MUTEX_DEFAULT, NULL);
1078 1078 khme_hash[i].hmeh_nextpa = HMEBLK_ENDPA;
1079 1079 }
1080 1080 for (i = 0; i < uhmehash_num; i++) {
1081 1081 mutex_init(&uhme_hash[i].hmehash_mutex, NULL,
1082 1082 MUTEX_DEFAULT, NULL);
1083 1083 uhme_hash[i].hmeh_nextpa = HMEBLK_ENDPA;
1084 1084 }
1085 1085 khmehash_num--; /* make sure counter starts from 0 */
1086 1086 uhmehash_num--; /* make sure counter starts from 0 */
1087 1087
1088 1088 /*
1089 1089 * Allocate context domain structures.
1090 1090 *
1091 1091 * A platform may choose to modify max_mmu_ctxdoms in
1092 1092 * set_platform_defaults(). If a platform does not define
1093 1093 * a set_platform_defaults() or does not choose to modify
1094 1094 * max_mmu_ctxdoms, it gets one MMU context domain for every CPU.
1095 1095 *
1096 1096 * For all platforms that have CPUs sharing MMUs, this
1097 1097 * value must be defined.
1098 1098 */
1099 1099 if (max_mmu_ctxdoms == 0)
1100 1100 max_mmu_ctxdoms = max_ncpus;
1101 1101
1102 1102 size = max_mmu_ctxdoms * sizeof (mmu_ctx_t *);
1103 1103 mmu_ctxs_tbl = kmem_zalloc(size, KM_SLEEP);
1104 1104
1105 1105 /* mmu_ctx_t is 64 bytes aligned */
1106 1106 mmuctxdom_cache = kmem_cache_create("mmuctxdom_cache",
1107 1107 sizeof (mmu_ctx_t), 64, NULL, NULL, NULL, NULL, NULL, 0);
1108 1108 /*
1109 1109 * MMU context domain initialization for the Boot CPU.
1110 1110 * This needs the context domains array allocated above.
1111 1111 */
1112 1112 mutex_enter(&cpu_lock);
1113 1113 sfmmu_cpu_init(CPU);
1114 1114 mutex_exit(&cpu_lock);
1115 1115
1116 1116 /*
1117 1117 * Intialize ism mapping list lock.
1118 1118 */
1119 1119
1120 1120 mutex_init(&ism_mlist_lock, NULL, MUTEX_DEFAULT, NULL);
1121 1121
1122 1122 /*
1123 1123 * Each sfmmu structure carries an array of MMU context info
1124 1124 * structures, one per context domain. The size of this array depends
1125 1125 * on the maximum number of context domains. So, the size of the
1126 1126 * sfmmu structure varies per platform.
1127 1127 *
1128 1128 * sfmmu is allocated from static arena, because trap
1129 1129 * handler at TL > 0 is not allowed to touch kernel relocatable
1130 1130 * memory. sfmmu's alignment is changed to 64 bytes from
1131 1131 * default 8 bytes, as the lower 6 bits will be used to pass
1132 1132 * pgcnt to vtag_flush_pgcnt_tl1.
1133 1133 */
1134 1134 size = sizeof (sfmmu_t) + sizeof (sfmmu_ctx_t) * (max_mmu_ctxdoms - 1);
1135 1135
1136 1136 sfmmuid_cache = kmem_cache_create("sfmmuid_cache", size,
1137 1137 64, sfmmu_idcache_constructor, sfmmu_idcache_destructor,
1138 1138 NULL, NULL, static_arena, 0);
1139 1139
1140 1140 sfmmu_tsbinfo_cache = kmem_cache_create("sfmmu_tsbinfo_cache",
1141 1141 sizeof (struct tsb_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
1142 1142
1143 1143 /*
1144 1144 * Since we only use the tsb8k cache to "borrow" pages for TSBs
1145 1145 * from the heap when low on memory or when TSB_FORCEALLOC is
1146 1146 * specified, don't use magazines to cache them--we want to return
1147 1147 * them to the system as quickly as possible.
1148 1148 */
1149 1149 sfmmu_tsb8k_cache = kmem_cache_create("sfmmu_tsb8k_cache",
1150 1150 MMU_PAGESIZE, MMU_PAGESIZE, NULL, NULL, NULL, NULL,
1151 1151 static_arena, KMC_NOMAGAZINE);
1152 1152
1153 1153 /*
1154 1154 * Set tsb_alloc_hiwater to 1/tsb_alloc_hiwater_factor of physical
1155 1155 * memory, which corresponds to the old static reserve for TSBs.
1156 1156 * tsb_alloc_hiwater_factor defaults to 32. This caps the amount of
1157 1157 * memory we'll allocate for TSB slabs; beyond this point TSB
1158 1158 * allocations will be taken from the kernel heap (via
1159 1159 * sfmmu_tsb8k_cache) and will be throttled as would any other kmem
1160 1160 * consumer.
1161 1161 */
1162 1162 if (tsb_alloc_hiwater_factor == 0) {
1163 1163 tsb_alloc_hiwater_factor = TSB_ALLOC_HIWATER_FACTOR_DEFAULT;
1164 1164 }
1165 1165 SFMMU_SET_TSB_ALLOC_HIWATER(physmem);
1166 1166
1167 1167 for (sz = tsb_slab_ttesz; sz > 0; sz--) {
1168 1168 if (!(disable_large_pages & (1 << sz)))
1169 1169 break;
1170 1170 }
1171 1171
1172 1172 if (sz < tsb_slab_ttesz) {
1173 1173 tsb_slab_ttesz = sz;
1174 1174 tsb_slab_shift = MMU_PAGESHIFT + (sz << 1) + sz;
1175 1175 tsb_slab_size = 1 << tsb_slab_shift;
1176 1176 tsb_slab_mask = (1 << (tsb_slab_shift - MMU_PAGESHIFT)) - 1;
1177 1177 use_bigtsb_arena = 0;
1178 1178 } else if (use_bigtsb_arena &&
1179 1179 (disable_large_pages & (1 << bigtsb_slab_ttesz))) {
1180 1180 use_bigtsb_arena = 0;
1181 1181 }
1182 1182
1183 1183 if (!use_bigtsb_arena) {
1184 1184 bigtsb_slab_shift = tsb_slab_shift;
1185 1185 }
1186 1186 SFMMU_SET_TSB_MAX_GROWSIZE(physmem);
1187 1187
1188 1188 /*
1189 1189 * On smaller memory systems, allocate TSB memory in smaller chunks
1190 1190 * than the default 4M slab size. We also honor disable_large_pages
1191 1191 * here.
1192 1192 *
1193 1193 * The trap handlers need to be patched with the final slab shift,
1194 1194 * since they need to be able to construct the TSB pointer at runtime.
1195 1195 */
1196 1196 if ((tsb_max_growsize <= TSB_512K_SZCODE) &&
1197 1197 !(disable_large_pages & (1 << TTE512K))) {
1198 1198 tsb_slab_ttesz = TTE512K;
1199 1199 tsb_slab_shift = MMU_PAGESHIFT512K;
1200 1200 tsb_slab_size = MMU_PAGESIZE512K;
1201 1201 tsb_slab_mask = MMU_PAGEOFFSET512K >> MMU_PAGESHIFT;
1202 1202 use_bigtsb_arena = 0;
1203 1203 }
1204 1204
1205 1205 if (!use_bigtsb_arena) {
1206 1206 bigtsb_slab_ttesz = tsb_slab_ttesz;
1207 1207 bigtsb_slab_shift = tsb_slab_shift;
1208 1208 bigtsb_slab_size = tsb_slab_size;
1209 1209 bigtsb_slab_mask = tsb_slab_mask;
1210 1210 }
1211 1211
1212 1212
1213 1213 /*
1214 1214 * Set up memory callback to update tsb_alloc_hiwater and
1215 1215 * tsb_max_growsize.
1216 1216 */
1217 1217 i = kphysm_setup_func_register(&sfmmu_update_vec, (void *) 0);
1218 1218 ASSERT(i == 0);
1219 1219
1220 1220 /*
1221 1221 * kmem_tsb_arena is the source from which large TSB slabs are
1222 1222 * drawn. The quantum of this arena corresponds to the largest
1223 1223 * TSB size we can dynamically allocate for user processes.
1224 1224 * Currently it must also be a supported page size since we
1225 1225 * use exactly one translation entry to map each slab page.
1226 1226 *
1227 1227 * The per-lgroup kmem_tsb_default_arena arenas are the arenas from
1228 1228 * which most TSBs are allocated. Since most TSB allocations are
1229 1229 * typically 8K we have a kmem cache we stack on top of each
1230 1230 * kmem_tsb_default_arena to speed up those allocations.
1231 1231 *
1232 1232 * Note the two-level scheme of arenas is required only
1233 1233 * because vmem_create doesn't allow us to specify alignment
1234 1234 * requirements. If this ever changes the code could be
1235 1235 * simplified to use only one level of arenas.
1236 1236 *
1237 1237 * If 256M page support exists on sun4v, 256MB kmem_bigtsb_arena
1238 1238 * will be provided in addition to the 4M kmem_tsb_arena.
1239 1239 */
1240 1240 if (use_bigtsb_arena) {
1241 1241 kmem_bigtsb_arena = vmem_create("kmem_bigtsb", NULL, 0,
1242 1242 bigtsb_slab_size, sfmmu_vmem_xalloc_aligned_wrapper,
1243 1243 vmem_xfree, heap_arena, 0, VM_SLEEP);
1244 1244 }
1245 1245
1246 1246 kmem_tsb_arena = vmem_create("kmem_tsb", NULL, 0, tsb_slab_size,
1247 1247 sfmmu_vmem_xalloc_aligned_wrapper,
1248 1248 vmem_xfree, heap_arena, 0, VM_SLEEP);
1249 1249
1250 1250 if (tsb_lgrp_affinity) {
1251 1251 char s[50];
1252 1252 for (i = 0; i < NLGRPS_MAX; i++) {
1253 1253 if (use_bigtsb_arena) {
1254 1254 (void) sprintf(s, "kmem_bigtsb_lgrp%d", i);
1255 1255 kmem_bigtsb_default_arena[i] = vmem_create(s,
1256 1256 NULL, 0, 2 * tsb_slab_size,
1257 1257 sfmmu_tsb_segkmem_alloc,
1258 1258 sfmmu_tsb_segkmem_free, kmem_bigtsb_arena,
1259 1259 0, VM_SLEEP | VM_BESTFIT);
1260 1260 }
1261 1261
1262 1262 (void) sprintf(s, "kmem_tsb_lgrp%d", i);
1263 1263 kmem_tsb_default_arena[i] = vmem_create(s,
1264 1264 NULL, 0, PAGESIZE, sfmmu_tsb_segkmem_alloc,
1265 1265 sfmmu_tsb_segkmem_free, kmem_tsb_arena, 0,
1266 1266 VM_SLEEP | VM_BESTFIT);
1267 1267
1268 1268 (void) sprintf(s, "sfmmu_tsb_lgrp%d_cache", i);
1269 1269 sfmmu_tsb_cache[i] = kmem_cache_create(s,
1270 1270 PAGESIZE, PAGESIZE, NULL, NULL, NULL, NULL,
1271 1271 kmem_tsb_default_arena[i], 0);
1272 1272 }
1273 1273 } else {
1274 1274 if (use_bigtsb_arena) {
1275 1275 kmem_bigtsb_default_arena[0] =
1276 1276 vmem_create("kmem_bigtsb_default", NULL, 0,
1277 1277 2 * tsb_slab_size, sfmmu_tsb_segkmem_alloc,
1278 1278 sfmmu_tsb_segkmem_free, kmem_bigtsb_arena, 0,
1279 1279 VM_SLEEP | VM_BESTFIT);
1280 1280 }
1281 1281
1282 1282 kmem_tsb_default_arena[0] = vmem_create("kmem_tsb_default",
1283 1283 NULL, 0, PAGESIZE, sfmmu_tsb_segkmem_alloc,
1284 1284 sfmmu_tsb_segkmem_free, kmem_tsb_arena, 0,
1285 1285 VM_SLEEP | VM_BESTFIT);
1286 1286 sfmmu_tsb_cache[0] = kmem_cache_create("sfmmu_tsb_cache",
1287 1287 PAGESIZE, PAGESIZE, NULL, NULL, NULL, NULL,
1288 1288 kmem_tsb_default_arena[0], 0);
1289 1289 }
1290 1290
1291 1291 sfmmu8_cache = kmem_cache_create("sfmmu8_cache", HME8BLK_SZ,
1292 1292 HMEBLK_ALIGN, sfmmu_hblkcache_constructor,
1293 1293 sfmmu_hblkcache_destructor,
1294 1294 sfmmu_hblkcache_reclaim, (void *)HME8BLK_SZ,
1295 1295 hat_memload_arena, KMC_NOHASH);
1296 1296
1297 1297 hat_memload1_arena = vmem_create("hat_memload1", NULL, 0, PAGESIZE,
1298 1298 segkmem_alloc_permanent, segkmem_free, heap_arena, 0,
1299 1299 VMC_DUMPSAFE | VM_SLEEP);
1300 1300
1301 1301 sfmmu1_cache = kmem_cache_create("sfmmu1_cache", HME1BLK_SZ,
1302 1302 HMEBLK_ALIGN, sfmmu_hblkcache_constructor,
1303 1303 sfmmu_hblkcache_destructor,
1304 1304 NULL, (void *)HME1BLK_SZ,
1305 1305 hat_memload1_arena, KMC_NOHASH);
1306 1306
1307 1307 pa_hment_cache = kmem_cache_create("pa_hment_cache", PAHME_SZ,
1308 1308 0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH);
1309 1309
1310 1310 ism_blk_cache = kmem_cache_create("ism_blk_cache",
1311 1311 sizeof (ism_blk_t), ecache_alignsize, NULL, NULL,
1312 1312 NULL, NULL, static_arena, KMC_NOHASH);
1313 1313
1314 1314 ism_ment_cache = kmem_cache_create("ism_ment_cache",
1315 1315 sizeof (ism_ment_t), 0, NULL, NULL,
1316 1316 NULL, NULL, NULL, 0);
1317 1317
1318 1318 /*
1319 1319 * We grab the first hat for the kernel,
1320 1320 */
1321 1321 AS_LOCK_ENTER(&kas, &kas.a_lock, RW_WRITER);
1322 1322 kas.a_hat = hat_alloc(&kas);
1323 1323 AS_LOCK_EXIT(&kas, &kas.a_lock);
1324 1324
1325 1325 /*
1326 1326 * Initialize hblk_reserve.
1327 1327 */
1328 1328 ((struct hme_blk *)hblk_reserve)->hblk_nextpa =
1329 1329 va_to_pa((caddr_t)hblk_reserve);
1330 1330
1331 1331 #ifndef UTSB_PHYS
1332 1332 /*
1333 1333 * Reserve some kernel virtual address space for the locked TTEs
1334 1334 * that allow us to probe the TSB from TL>0.
1335 1335 */
1336 1336 utsb_vabase = vmem_xalloc(heap_arena, tsb_slab_size, tsb_slab_size,
1337 1337 0, 0, NULL, NULL, VM_SLEEP);
1338 1338 utsb4m_vabase = vmem_xalloc(heap_arena, tsb_slab_size, tsb_slab_size,
1339 1339 0, 0, NULL, NULL, VM_SLEEP);
1340 1340 #endif
1341 1341
1342 1342 #ifdef VAC
1343 1343 /*
1344 1344 * The big page VAC handling code assumes VAC
1345 1345 * will not be bigger than the smallest big
1346 1346 * page- which is 64K.
1347 1347 */
1348 1348 if (TTEPAGES(TTE64K) < CACHE_NUM_COLOR) {
1349 1349 cmn_err(CE_PANIC, "VAC too big!");
1350 1350 }
1351 1351 #endif
1352 1352
1353 1353 (void) xhat_init();
1354 1354
1355 1355 uhme_hash_pa = va_to_pa(uhme_hash);
1356 1356 khme_hash_pa = va_to_pa(khme_hash);
1357 1357
1358 1358 /*
1359 1359 * Initialize relocation locks. kpr_suspendlock is held
1360 1360 * at PIL_MAX to prevent interrupts from pinning the holder
1361 1361 * of a suspended TTE which may access it leading to a
1362 1362 * deadlock condition.
1363 1363 */
1364 1364 mutex_init(&kpr_mutex, NULL, MUTEX_DEFAULT, NULL);
1365 1365 mutex_init(&kpr_suspendlock, NULL, MUTEX_SPIN, (void *)PIL_MAX);
1366 1366
1367 1367 /*
1368 1368 * If Shared context support is disabled via /etc/system
1369 1369 * set shctx_on to 0 here if it was set to 1 earlier in boot
1370 1370 * sequence by cpu module initialization code.
1371 1371 */
1372 1372 if (shctx_on && disable_shctx) {
1373 1373 shctx_on = 0;
1374 1374 }
1375 1375
1376 1376 if (shctx_on) {
1377 1377 srd_buckets = kmem_zalloc(SFMMU_MAX_SRD_BUCKETS *
1378 1378 sizeof (srd_buckets[0]), KM_SLEEP);
1379 1379 for (i = 0; i < SFMMU_MAX_SRD_BUCKETS; i++) {
1380 1380 mutex_init(&srd_buckets[i].srdb_lock, NULL,
1381 1381 MUTEX_DEFAULT, NULL);
1382 1382 }
1383 1383
1384 1384 srd_cache = kmem_cache_create("srd_cache", sizeof (sf_srd_t),
1385 1385 0, sfmmu_srdcache_constructor, sfmmu_srdcache_destructor,
1386 1386 NULL, NULL, NULL, 0);
1387 1387 region_cache = kmem_cache_create("region_cache",
1388 1388 sizeof (sf_region_t), 0, sfmmu_rgncache_constructor,
1389 1389 sfmmu_rgncache_destructor, NULL, NULL, NULL, 0);
1390 1390 scd_cache = kmem_cache_create("scd_cache", sizeof (sf_scd_t),
1391 1391 0, sfmmu_scdcache_constructor, sfmmu_scdcache_destructor,
1392 1392 NULL, NULL, NULL, 0);
1393 1393 }
1394 1394
1395 1395 /*
1396 1396 * Pre-allocate hrm_hashtab before enabling the collection of
1397 1397 * refmod statistics. Allocating on the fly would mean us
1398 1398 * running the risk of suffering recursive mutex enters or
1399 1399 * deadlocks.
1400 1400 */
1401 1401 hrm_hashtab = kmem_zalloc(HRM_HASHSIZE * sizeof (struct hrmstat *),
1402 1402 KM_SLEEP);
1403 1403
1404 1404 /* Allocate per-cpu pending freelist of hmeblks */
1405 1405 cpu_hme_pend = kmem_zalloc((NCPU * sizeof (cpu_hme_pend_t)) + 64,
1406 1406 KM_SLEEP);
1407 1407 cpu_hme_pend = (cpu_hme_pend_t *)P2ROUNDUP(
1408 1408 (uintptr_t)cpu_hme_pend, 64);
1409 1409
1410 1410 for (i = 0; i < NCPU; i++) {
1411 1411 mutex_init(&cpu_hme_pend[i].chp_mutex, NULL, MUTEX_DEFAULT,
1412 1412 NULL);
1413 1413 }
1414 1414
1415 1415 if (cpu_hme_pend_thresh == 0) {
1416 1416 cpu_hme_pend_thresh = CPU_HME_PEND_THRESH;
1417 1417 }
1418 1418 }
1419 1419
1420 1420 /*
1421 1421 * Initialize locking for the hat layer, called early during boot.
1422 1422 */
1423 1423 static void
1424 1424 hat_lock_init()
1425 1425 {
1426 1426 int i;
1427 1427
1428 1428 /*
1429 1429 * initialize the array of mutexes protecting a page's mapping
1430 1430 * list and p_nrm field.
1431 1431 */
1432 1432 for (i = 0; i < MML_TABLE_SIZE; i++)
1433 1433 mutex_init(&mml_table[i].pad_mutex, NULL, MUTEX_DEFAULT, NULL);
1434 1434
1435 1435 if (kpm_enable) {
1436 1436 for (i = 0; i < kpmp_table_sz; i++) {
1437 1437 mutex_init(&kpmp_table[i].khl_mutex, NULL,
1438 1438 MUTEX_DEFAULT, NULL);
1439 1439 }
1440 1440 }
1441 1441
1442 1442 /*
1443 1443 * Initialize array of mutex locks that protects sfmmu fields and
1444 1444 * TSB lists.
1445 1445 */
1446 1446 for (i = 0; i < SFMMU_NUM_LOCK; i++)
1447 1447 mutex_init(HATLOCK_MUTEXP(&hat_lock[i]), NULL, MUTEX_DEFAULT,
1448 1448 NULL);
1449 1449 }
1450 1450
1451 1451 #define SFMMU_KERNEL_MAXVA \
1452 1452 (kmem64_base ? (uintptr_t)kmem64_end : (SYSLIMIT))
1453 1453
1454 1454 /*
1455 1455 * Allocate a hat structure.
1456 1456 * Called when an address space first uses a hat.
1457 1457 */
1458 1458 struct hat *
1459 1459 hat_alloc(struct as *as)
1460 1460 {
1461 1461 sfmmu_t *sfmmup;
1462 1462 int i;
1463 1463 uint64_t cnum;
1464 1464 extern uint_t get_color_start(struct as *);
1465 1465
1466 1466 ASSERT(AS_WRITE_HELD(as, &as->a_lock));
1467 1467 sfmmup = kmem_cache_alloc(sfmmuid_cache, KM_SLEEP);
1468 1468 sfmmup->sfmmu_as = as;
1469 1469 sfmmup->sfmmu_flags = 0;
1470 1470 sfmmup->sfmmu_tteflags = 0;
1471 1471 sfmmup->sfmmu_rtteflags = 0;
1472 1472 LOCK_INIT_CLEAR(&sfmmup->sfmmu_ctx_lock);
1473 1473
1474 1474 if (as == &kas) {
1475 1475 ksfmmup = sfmmup;
1476 1476 sfmmup->sfmmu_cext = 0;
1477 1477 cnum = KCONTEXT;
1478 1478
1479 1479 sfmmup->sfmmu_clrstart = 0;
1480 1480 sfmmup->sfmmu_tsb = NULL;
1481 1481 /*
1482 1482 * hat_kern_setup() will call sfmmu_init_ktsbinfo()
1483 1483 * to setup tsb_info for ksfmmup.
1484 1484 */
1485 1485 } else {
1486 1486
1487 1487 /*
1488 1488 * Just set to invalid ctx. When it faults, it will
1489 1489 * get a valid ctx. This would avoid the situation
1490 1490 * where we get a ctx, but it gets stolen and then
1491 1491 * we fault when we try to run and so have to get
1492 1492 * another ctx.
1493 1493 */
1494 1494 sfmmup->sfmmu_cext = 0;
1495 1495 cnum = INVALID_CONTEXT;
1496 1496
1497 1497 /* initialize original physical page coloring bin */
1498 1498 sfmmup->sfmmu_clrstart = get_color_start(as);
1499 1499 #ifdef DEBUG
1500 1500 if (tsb_random_size) {
1501 1501 uint32_t randval = (uint32_t)gettick() >> 4;
1502 1502 int size = randval % (tsb_max_growsize + 1);
1503 1503
1504 1504 /* chose a random tsb size for stress testing */
1505 1505 (void) sfmmu_tsbinfo_alloc(&sfmmup->sfmmu_tsb, size,
1506 1506 TSB8K|TSB64K|TSB512K, 0, sfmmup);
1507 1507 } else
1508 1508 #endif /* DEBUG */
1509 1509 (void) sfmmu_tsbinfo_alloc(&sfmmup->sfmmu_tsb,
1510 1510 default_tsb_size,
1511 1511 TSB8K|TSB64K|TSB512K, 0, sfmmup);
1512 1512 sfmmup->sfmmu_flags = HAT_SWAPPED | HAT_ALLCTX_INVALID;
1513 1513 ASSERT(sfmmup->sfmmu_tsb != NULL);
1514 1514 }
1515 1515
1516 1516 ASSERT(max_mmu_ctxdoms > 0);
1517 1517 for (i = 0; i < max_mmu_ctxdoms; i++) {
1518 1518 sfmmup->sfmmu_ctxs[i].cnum = cnum;
1519 1519 sfmmup->sfmmu_ctxs[i].gnum = 0;
1520 1520 }
1521 1521
1522 1522 for (i = 0; i < max_mmu_page_sizes; i++) {
1523 1523 sfmmup->sfmmu_ttecnt[i] = 0;
1524 1524 sfmmup->sfmmu_scdrttecnt[i] = 0;
1525 1525 sfmmup->sfmmu_ismttecnt[i] = 0;
1526 1526 sfmmup->sfmmu_scdismttecnt[i] = 0;
1527 1527 sfmmup->sfmmu_pgsz[i] = TTE8K;
1528 1528 }
1529 1529 sfmmup->sfmmu_tsb0_4minflcnt = 0;
1530 1530 sfmmup->sfmmu_iblk = NULL;
1531 1531 sfmmup->sfmmu_ismhat = 0;
1532 1532 sfmmup->sfmmu_scdhat = 0;
1533 1533 sfmmup->sfmmu_ismblkpa = (uint64_t)-1;
1534 1534 if (sfmmup == ksfmmup) {
1535 1535 CPUSET_ALL(sfmmup->sfmmu_cpusran);
1536 1536 } else {
1537 1537 CPUSET_ZERO(sfmmup->sfmmu_cpusran);
1538 1538 }
1539 1539 sfmmup->sfmmu_free = 0;
1540 1540 sfmmup->sfmmu_rmstat = 0;
1541 1541 sfmmup->sfmmu_clrbin = sfmmup->sfmmu_clrstart;
1542 1542 sfmmup->sfmmu_xhat_provider = NULL;
1543 1543 cv_init(&sfmmup->sfmmu_tsb_cv, NULL, CV_DEFAULT, NULL);
1544 1544 sfmmup->sfmmu_srdp = NULL;
1545 1545 SF_RGNMAP_ZERO(sfmmup->sfmmu_region_map);
1546 1546 bzero(sfmmup->sfmmu_hmeregion_links, SFMMU_L1_HMERLINKS_SIZE);
1547 1547 sfmmup->sfmmu_scdp = NULL;
1548 1548 sfmmup->sfmmu_scd_link.next = NULL;
1549 1549 sfmmup->sfmmu_scd_link.prev = NULL;
1550 1550 return (sfmmup);
1551 1551 }
1552 1552
1553 1553 /*
1554 1554 * Create per-MMU context domain kstats for a given MMU ctx.
1555 1555 */
1556 1556 static void
1557 1557 sfmmu_mmu_kstat_create(mmu_ctx_t *mmu_ctxp)
1558 1558 {
1559 1559 mmu_ctx_stat_t stat;
1560 1560 kstat_t *mmu_kstat;
1561 1561
1562 1562 ASSERT(MUTEX_HELD(&cpu_lock));
1563 1563 ASSERT(mmu_ctxp->mmu_kstat == NULL);
1564 1564
1565 1565 mmu_kstat = kstat_create("unix", mmu_ctxp->mmu_idx, "mmu_ctx",
1566 1566 "hat", KSTAT_TYPE_NAMED, MMU_CTX_NUM_STATS, KSTAT_FLAG_VIRTUAL);
1567 1567
1568 1568 if (mmu_kstat == NULL) {
1569 1569 cmn_err(CE_WARN, "kstat_create for MMU %d failed",
1570 1570 mmu_ctxp->mmu_idx);
1571 1571 } else {
1572 1572 mmu_kstat->ks_data = mmu_ctxp->mmu_kstat_data;
1573 1573 for (stat = 0; stat < MMU_CTX_NUM_STATS; stat++)
1574 1574 kstat_named_init(&mmu_ctxp->mmu_kstat_data[stat],
1575 1575 mmu_ctx_kstat_names[stat], KSTAT_DATA_INT64);
1576 1576 mmu_ctxp->mmu_kstat = mmu_kstat;
1577 1577 kstat_install(mmu_kstat);
1578 1578 }
1579 1579 }
1580 1580
1581 1581 /*
1582 1582 * plat_cpuid_to_mmu_ctx_info() is a platform interface that returns MMU
1583 1583 * context domain information for a given CPU. If a platform does not
1584 1584 * specify that interface, then the function below is used instead to return
1585 1585 * default information. The defaults are as follows:
1586 1586 *
1587 1587 * - The number of MMU context IDs supported on any CPU in the
1588 1588 * system is 8K.
1589 1589 * - There is one MMU context domain per CPU.
1590 1590 */
1591 1591 /*ARGSUSED*/
1592 1592 static void
1593 1593 sfmmu_cpuid_to_mmu_ctx_info(processorid_t cpuid, mmu_ctx_info_t *infop)
1594 1594 {
1595 1595 infop->mmu_nctxs = nctxs;
1596 1596 infop->mmu_idx = cpu[cpuid]->cpu_seqid;
1597 1597 }
1598 1598
1599 1599 /*
1600 1600 * Called during CPU initialization to set the MMU context-related information
1601 1601 * for a CPU.
1602 1602 *
1603 1603 * cpu_lock serializes accesses to mmu_ctxs and mmu_saved_gnum.
1604 1604 */
1605 1605 void
1606 1606 sfmmu_cpu_init(cpu_t *cp)
1607 1607 {
1608 1608 mmu_ctx_info_t info;
1609 1609 mmu_ctx_t *mmu_ctxp;
1610 1610
1611 1611 ASSERT(MUTEX_HELD(&cpu_lock));
1612 1612
1613 1613 if (&plat_cpuid_to_mmu_ctx_info == NULL)
1614 1614 sfmmu_cpuid_to_mmu_ctx_info(cp->cpu_id, &info);
1615 1615 else
1616 1616 plat_cpuid_to_mmu_ctx_info(cp->cpu_id, &info);
1617 1617
1618 1618 ASSERT(info.mmu_idx < max_mmu_ctxdoms);
1619 1619
1620 1620 if ((mmu_ctxp = mmu_ctxs_tbl[info.mmu_idx]) == NULL) {
1621 1621 /* Each mmu_ctx is cacheline aligned. */
1622 1622 mmu_ctxp = kmem_cache_alloc(mmuctxdom_cache, KM_SLEEP);
1623 1623 bzero(mmu_ctxp, sizeof (mmu_ctx_t));
1624 1624
1625 1625 mutex_init(&mmu_ctxp->mmu_lock, NULL, MUTEX_SPIN,
1626 1626 (void *)ipltospl(DISP_LEVEL));
1627 1627 mmu_ctxp->mmu_idx = info.mmu_idx;
1628 1628 mmu_ctxp->mmu_nctxs = info.mmu_nctxs;
1629 1629 /*
1630 1630 * Globally for lifetime of a system,
1631 1631 * gnum must always increase.
1632 1632 * mmu_saved_gnum is protected by the cpu_lock.
1633 1633 */
1634 1634 mmu_ctxp->mmu_gnum = mmu_saved_gnum + 1;
1635 1635 mmu_ctxp->mmu_cnum = NUM_LOCKED_CTXS;
1636 1636
1637 1637 sfmmu_mmu_kstat_create(mmu_ctxp);
1638 1638
1639 1639 mmu_ctxs_tbl[info.mmu_idx] = mmu_ctxp;
1640 1640 } else {
1641 1641 ASSERT(mmu_ctxp->mmu_idx == info.mmu_idx);
1642 1642 ASSERT(mmu_ctxp->mmu_nctxs <= info.mmu_nctxs);
1643 1643 }
1644 1644
1645 1645 /*
1646 1646 * The mmu_lock is acquired here to prevent races with
1647 1647 * the wrap-around code.
1648 1648 */
1649 1649 mutex_enter(&mmu_ctxp->mmu_lock);
1650 1650
1651 1651
1652 1652 mmu_ctxp->mmu_ncpus++;
1653 1653 CPUSET_ADD(mmu_ctxp->mmu_cpuset, cp->cpu_id);
1654 1654 CPU_MMU_IDX(cp) = info.mmu_idx;
1655 1655 CPU_MMU_CTXP(cp) = mmu_ctxp;
1656 1656
1657 1657 mutex_exit(&mmu_ctxp->mmu_lock);
1658 1658 }
1659 1659
1660 1660 static void
1661 1661 sfmmu_ctxdom_free(mmu_ctx_t *mmu_ctxp)
1662 1662 {
1663 1663 ASSERT(MUTEX_HELD(&cpu_lock));
1664 1664 ASSERT(!MUTEX_HELD(&mmu_ctxp->mmu_lock));
1665 1665
1666 1666 mutex_destroy(&mmu_ctxp->mmu_lock);
1667 1667
1668 1668 if (mmu_ctxp->mmu_kstat)
1669 1669 kstat_delete(mmu_ctxp->mmu_kstat);
1670 1670
1671 1671 /* mmu_saved_gnum is protected by the cpu_lock. */
1672 1672 if (mmu_saved_gnum < mmu_ctxp->mmu_gnum)
1673 1673 mmu_saved_gnum = mmu_ctxp->mmu_gnum;
1674 1674
1675 1675 kmem_cache_free(mmuctxdom_cache, mmu_ctxp);
1676 1676 }
1677 1677
1678 1678 /*
1679 1679 * Called to perform MMU context-related cleanup for a CPU.
1680 1680 */
1681 1681 void
1682 1682 sfmmu_cpu_cleanup(cpu_t *cp)
1683 1683 {
1684 1684 mmu_ctx_t *mmu_ctxp;
1685 1685
1686 1686 ASSERT(MUTEX_HELD(&cpu_lock));
1687 1687
1688 1688 mmu_ctxp = CPU_MMU_CTXP(cp);
1689 1689 ASSERT(mmu_ctxp != NULL);
1690 1690
1691 1691 /*
1692 1692 * The mmu_lock is acquired here to prevent races with
1693 1693 * the wrap-around code.
1694 1694 */
1695 1695 mutex_enter(&mmu_ctxp->mmu_lock);
1696 1696
1697 1697 CPU_MMU_CTXP(cp) = NULL;
1698 1698
1699 1699 CPUSET_DEL(mmu_ctxp->mmu_cpuset, cp->cpu_id);
1700 1700 if (--mmu_ctxp->mmu_ncpus == 0) {
1701 1701 mmu_ctxs_tbl[mmu_ctxp->mmu_idx] = NULL;
1702 1702 mutex_exit(&mmu_ctxp->mmu_lock);
1703 1703 sfmmu_ctxdom_free(mmu_ctxp);
1704 1704 return;
1705 1705 }
1706 1706
1707 1707 mutex_exit(&mmu_ctxp->mmu_lock);
1708 1708 }
1709 1709
1710 1710 uint_t
1711 1711 sfmmu_ctxdom_nctxs(int idx)
1712 1712 {
1713 1713 return (mmu_ctxs_tbl[idx]->mmu_nctxs);
1714 1714 }
1715 1715
1716 1716 #ifdef sun4v
1717 1717 /*
1718 1718 * sfmmu_ctxdoms_* is an interface provided to help keep context domains
1719 1719 * consistant after suspend/resume on system that can resume on a different
1720 1720 * hardware than it was suspended.
1721 1721 *
1722 1722 * sfmmu_ctxdom_lock(void) locks all context domains and prevents new contexts
1723 1723 * from being allocated. It acquires all hat_locks, which blocks most access to
1724 1724 * context data, except for a few cases that are handled separately or are
1725 1725 * harmless. It wraps each domain to increment gnum and invalidate on-CPU
1726 1726 * contexts, and forces cnum to its max. As a result of this call all user
1727 1727 * threads that are running on CPUs trap and try to perform wrap around but
1728 1728 * can't because hat_locks are taken. Threads that were not on CPUs but started
1729 1729 * by scheduler go to sfmmu_alloc_ctx() to aquire context without checking
1730 1730 * hat_lock, but fail, because cnum == nctxs, and therefore also trap and block
1731 1731 * on hat_lock trying to wrap. sfmmu_ctxdom_lock() must be called before CPUs
1732 1732 * are paused, else it could deadlock acquiring locks held by paused CPUs.
1733 1733 *
1734 1734 * sfmmu_ctxdoms_remove() removes context domains from every CPUs and records
1735 1735 * the CPUs that had them. It must be called after CPUs have been paused. This
1736 1736 * ensures that no threads are in sfmmu_alloc_ctx() accessing domain data,
1737 1737 * because pause_cpus sends a mondo interrupt to every CPU, and sfmmu_alloc_ctx
1738 1738 * runs with interrupts disabled. When CPUs are later resumed, they may enter
1739 1739 * sfmmu_alloc_ctx, but it will check for CPU_MMU_CTXP = NULL and immediately
1740 1740 * return failure. Or, they will be blocked trying to acquire hat_lock. Thus
1741 1741 * after sfmmu_ctxdoms_remove returns, we are guaranteed that no one is
1742 1742 * accessing the old context domains.
1743 1743 *
1744 1744 * sfmmu_ctxdoms_update(void) frees space used by old context domains and
1745 1745 * allocates new context domains based on hardware layout. It initializes
1746 1746 * every CPU that had context domain before migration to have one again.
1747 1747 * sfmmu_ctxdoms_update must be called after CPUs are resumed, else it
1748 1748 * could deadlock acquiring locks held by paused CPUs.
1749 1749 *
1750 1750 * sfmmu_ctxdoms_unlock(void) releases all hat_locks after which user threads
1751 1751 * acquire new context ids and continue execution.
1752 1752 *
1753 1753 * Therefore functions should be called in the following order:
1754 1754 * suspend_routine()
1755 1755 * sfmmu_ctxdom_lock()
1756 1756 * pause_cpus()
1757 1757 * suspend()
1758 1758 * if (suspend failed)
1759 1759 * sfmmu_ctxdom_unlock()
1760 1760 * ...
1761 1761 * sfmmu_ctxdom_remove()
1762 1762 * resume_cpus()
1763 1763 * sfmmu_ctxdom_update()
1764 1764 * sfmmu_ctxdom_unlock()
1765 1765 */
1766 1766 static cpuset_t sfmmu_ctxdoms_pset;
1767 1767
1768 1768 void
1769 1769 sfmmu_ctxdoms_remove()
1770 1770 {
1771 1771 processorid_t id;
1772 1772 cpu_t *cp;
1773 1773
1774 1774 /*
1775 1775 * Record the CPUs that have domains in sfmmu_ctxdoms_pset, so they can
1776 1776 * be restored post-migration. A CPU may be powered off and not have a
1777 1777 * domain, for example.
1778 1778 */
1779 1779 CPUSET_ZERO(sfmmu_ctxdoms_pset);
1780 1780
1781 1781 for (id = 0; id < NCPU; id++) {
1782 1782 if ((cp = cpu[id]) != NULL && CPU_MMU_CTXP(cp) != NULL) {
1783 1783 CPUSET_ADD(sfmmu_ctxdoms_pset, id);
1784 1784 CPU_MMU_CTXP(cp) = NULL;
1785 1785 }
1786 1786 }
1787 1787 }
1788 1788
1789 1789 void
1790 1790 sfmmu_ctxdoms_lock(void)
1791 1791 {
1792 1792 int idx;
1793 1793 mmu_ctx_t *mmu_ctxp;
1794 1794
1795 1795 sfmmu_hat_lock_all();
1796 1796
1797 1797 /*
1798 1798 * At this point, no thread can be in sfmmu_ctx_wrap_around, because
1799 1799 * hat_lock is always taken before calling it.
1800 1800 *
1801 1801 * For each domain, set mmu_cnum to max so no more contexts can be
1802 1802 * allocated, and wrap to flush on-CPU contexts and force threads to
1803 1803 * acquire a new context when we later drop hat_lock after migration.
1804 1804 * Setting mmu_cnum may race with sfmmu_alloc_ctx which also sets cnum,
1805 1805 * but the latter uses CAS and will miscompare and not overwrite it.
1806 1806 */
1807 1807 kpreempt_disable(); /* required by sfmmu_ctx_wrap_around */
1808 1808 for (idx = 0; idx < max_mmu_ctxdoms; idx++) {
1809 1809 if ((mmu_ctxp = mmu_ctxs_tbl[idx]) != NULL) {
1810 1810 mutex_enter(&mmu_ctxp->mmu_lock);
1811 1811 mmu_ctxp->mmu_cnum = mmu_ctxp->mmu_nctxs;
1812 1812 /* make sure updated cnum visible */
1813 1813 membar_enter();
1814 1814 mutex_exit(&mmu_ctxp->mmu_lock);
1815 1815 sfmmu_ctx_wrap_around(mmu_ctxp, B_FALSE);
1816 1816 }
1817 1817 }
1818 1818 kpreempt_enable();
1819 1819 }
1820 1820
1821 1821 void
1822 1822 sfmmu_ctxdoms_unlock(void)
1823 1823 {
1824 1824 sfmmu_hat_unlock_all();
1825 1825 }
1826 1826
1827 1827 void
1828 1828 sfmmu_ctxdoms_update(void)
1829 1829 {
1830 1830 processorid_t id;
1831 1831 cpu_t *cp;
1832 1832 uint_t idx;
1833 1833 mmu_ctx_t *mmu_ctxp;
1834 1834
1835 1835 /*
1836 1836 * Free all context domains. As side effect, this increases
1837 1837 * mmu_saved_gnum to the maximum gnum over all domains, which is used to
1838 1838 * init gnum in the new domains, which therefore will be larger than the
1839 1839 * sfmmu gnum for any process, guaranteeing that every process will see
1840 1840 * a new generation and allocate a new context regardless of what new
1841 1841 * domain it runs in.
1842 1842 */
1843 1843 mutex_enter(&cpu_lock);
1844 1844
1845 1845 for (idx = 0; idx < max_mmu_ctxdoms; idx++) {
1846 1846 if (mmu_ctxs_tbl[idx] != NULL) {
1847 1847 mmu_ctxp = mmu_ctxs_tbl[idx];
1848 1848 mmu_ctxs_tbl[idx] = NULL;
1849 1849 sfmmu_ctxdom_free(mmu_ctxp);
1850 1850 }
1851 1851 }
1852 1852
1853 1853 for (id = 0; id < NCPU; id++) {
1854 1854 if (CPU_IN_SET(sfmmu_ctxdoms_pset, id) &&
1855 1855 (cp = cpu[id]) != NULL)
1856 1856 sfmmu_cpu_init(cp);
1857 1857 }
1858 1858 mutex_exit(&cpu_lock);
1859 1859 }
1860 1860 #endif
1861 1861
1862 1862 /*
1863 1863 * Hat_setup, makes an address space context the current active one.
1864 1864 * In sfmmu this translates to setting the secondary context with the
1865 1865 * corresponding context.
1866 1866 */
1867 1867 void
1868 1868 hat_setup(struct hat *sfmmup, int allocflag)
1869 1869 {
1870 1870 hatlock_t *hatlockp;
1871 1871
1872 1872 /* Init needs some special treatment. */
1873 1873 if (allocflag == HAT_INIT) {
1874 1874 /*
1875 1875 * Make sure that we have
1876 1876 * 1. a TSB
1877 1877 * 2. a valid ctx that doesn't get stolen after this point.
1878 1878 */
1879 1879 hatlockp = sfmmu_hat_enter(sfmmup);
1880 1880
1881 1881 /*
1882 1882 * Swap in the TSB. hat_init() allocates tsbinfos without
1883 1883 * TSBs, but we need one for init, since the kernel does some
1884 1884 * special things to set up its stack and needs the TSB to
1885 1885 * resolve page faults.
1886 1886 */
1887 1887 sfmmu_tsb_swapin(sfmmup, hatlockp);
1888 1888
1889 1889 sfmmu_get_ctx(sfmmup);
1890 1890
1891 1891 sfmmu_hat_exit(hatlockp);
1892 1892 } else {
1893 1893 ASSERT(allocflag == HAT_ALLOC);
1894 1894
1895 1895 hatlockp = sfmmu_hat_enter(sfmmup);
1896 1896 kpreempt_disable();
1897 1897
1898 1898 CPUSET_ADD(sfmmup->sfmmu_cpusran, CPU->cpu_id);
1899 1899 /*
1900 1900 * sfmmu_setctx_sec takes <pgsz|cnum> as a parameter,
1901 1901 * pagesize bits don't matter in this case since we are passing
1902 1902 * INVALID_CONTEXT to it.
1903 1903 * Compatibility Note: hw takes care of MMU_SCONTEXT1
1904 1904 */
1905 1905 sfmmu_setctx_sec(INVALID_CONTEXT);
1906 1906 sfmmu_clear_utsbinfo();
1907 1907
1908 1908 kpreempt_enable();
1909 1909 sfmmu_hat_exit(hatlockp);
1910 1910 }
1911 1911 }
1912 1912
1913 1913 /*
1914 1914 * Free all the translation resources for the specified address space.
1915 1915 * Called from as_free when an address space is being destroyed.
1916 1916 */
1917 1917 void
1918 1918 hat_free_start(struct hat *sfmmup)
1919 1919 {
1920 1920 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
1921 1921 ASSERT(sfmmup != ksfmmup);
1922 1922 ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
1923 1923
1924 1924 sfmmup->sfmmu_free = 1;
1925 1925 if (sfmmup->sfmmu_scdp != NULL) {
1926 1926 sfmmu_leave_scd(sfmmup, 0);
1927 1927 }
1928 1928
1929 1929 ASSERT(sfmmup->sfmmu_scdp == NULL);
1930 1930 }
1931 1931
1932 1932 void
1933 1933 hat_free_end(struct hat *sfmmup)
1934 1934 {
1935 1935 int i;
1936 1936
1937 1937 ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
1938 1938 ASSERT(sfmmup->sfmmu_free == 1);
1939 1939 ASSERT(sfmmup->sfmmu_ttecnt[TTE8K] == 0);
1940 1940 ASSERT(sfmmup->sfmmu_ttecnt[TTE64K] == 0);
1941 1941 ASSERT(sfmmup->sfmmu_ttecnt[TTE512K] == 0);
1942 1942 ASSERT(sfmmup->sfmmu_ttecnt[TTE4M] == 0);
1943 1943 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0);
1944 1944 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0);
1945 1945
1946 1946 if (sfmmup->sfmmu_rmstat) {
1947 1947 hat_freestat(sfmmup->sfmmu_as, NULL);
1948 1948 }
1949 1949
1950 1950 while (sfmmup->sfmmu_tsb != NULL) {
1951 1951 struct tsb_info *next = sfmmup->sfmmu_tsb->tsb_next;
1952 1952 sfmmu_tsbinfo_free(sfmmup->sfmmu_tsb);
1953 1953 sfmmup->sfmmu_tsb = next;
1954 1954 }
1955 1955
1956 1956 if (sfmmup->sfmmu_srdp != NULL) {
1957 1957 sfmmu_leave_srd(sfmmup);
1958 1958 ASSERT(sfmmup->sfmmu_srdp == NULL);
1959 1959 for (i = 0; i < SFMMU_L1_HMERLINKS; i++) {
1960 1960 if (sfmmup->sfmmu_hmeregion_links[i] != NULL) {
1961 1961 kmem_free(sfmmup->sfmmu_hmeregion_links[i],
1962 1962 SFMMU_L2_HMERLINKS_SIZE);
1963 1963 sfmmup->sfmmu_hmeregion_links[i] = NULL;
1964 1964 }
1965 1965 }
1966 1966 }
1967 1967 sfmmu_free_sfmmu(sfmmup);
1968 1968
↓ open down ↓ |
1968 lines elided |
↑ open up ↑ |
1969 1969 #ifdef DEBUG
1970 1970 for (i = 0; i < SFMMU_L1_HMERLINKS; i++) {
1971 1971 ASSERT(sfmmup->sfmmu_hmeregion_links[i] == NULL);
1972 1972 }
1973 1973 #endif
1974 1974
1975 1975 kmem_cache_free(sfmmuid_cache, sfmmup);
1976 1976 }
1977 1977
1978 1978 /*
1979 - * Set up any translation structures, for the specified address space,
1980 - * that are needed or preferred when the process is being swapped in.
1981 - */
1982 -/* ARGSUSED */
1983 -void
1984 -hat_swapin(struct hat *hat)
1985 -{
1986 - ASSERT(hat->sfmmu_xhat_provider == NULL);
1987 -}
1988 -
1989 -/*
1990 - * Free all of the translation resources, for the specified address space,
1991 - * that can be freed while the process is swapped out. Called from as_swapout.
1992 - * Also, free up the ctx that this process was using.
1993 - */
1994 -void
1995 -hat_swapout(struct hat *sfmmup)
1996 -{
1997 - struct hmehash_bucket *hmebp;
1998 - struct hme_blk *hmeblkp;
1999 - struct hme_blk *pr_hblk = NULL;
2000 - struct hme_blk *nx_hblk;
2001 - int i;
2002 - struct hme_blk *list = NULL;
2003 - hatlock_t *hatlockp;
2004 - struct tsb_info *tsbinfop;
2005 - struct free_tsb {
2006 - struct free_tsb *next;
2007 - struct tsb_info *tsbinfop;
2008 - }; /* free list of TSBs */
2009 - struct free_tsb *freelist, *last, *next;
2010 -
2011 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
2012 - SFMMU_STAT(sf_swapout);
2013 -
2014 - /*
2015 - * There is no way to go from an as to all its translations in sfmmu.
2016 - * Here is one of the times when we take the big hit and traverse
2017 - * the hash looking for hme_blks to free up. Not only do we free up
2018 - * this as hme_blks but all those that are free. We are obviously
2019 - * swapping because we need memory so let's free up as much
2020 - * as we can.
2021 - *
2022 - * Note that we don't flush TLB/TSB here -- it's not necessary
2023 - * because:
2024 - * 1) we free the ctx we're using and throw away the TSB(s);
2025 - * 2) processes aren't runnable while being swapped out.
2026 - */
2027 - ASSERT(sfmmup != KHATID);
2028 - for (i = 0; i <= UHMEHASH_SZ; i++) {
2029 - hmebp = &uhme_hash[i];
2030 - SFMMU_HASH_LOCK(hmebp);
2031 - hmeblkp = hmebp->hmeblkp;
2032 - pr_hblk = NULL;
2033 - while (hmeblkp) {
2034 -
2035 - ASSERT(!hmeblkp->hblk_xhat_bit);
2036 -
2037 - if ((hmeblkp->hblk_tag.htag_id == sfmmup) &&
2038 - !hmeblkp->hblk_shw_bit && !hmeblkp->hblk_lckcnt) {
2039 - ASSERT(!hmeblkp->hblk_shared);
2040 - (void) sfmmu_hblk_unload(sfmmup, hmeblkp,
2041 - (caddr_t)get_hblk_base(hmeblkp),
2042 - get_hblk_endaddr(hmeblkp),
2043 - NULL, HAT_UNLOAD);
2044 - }
2045 - nx_hblk = hmeblkp->hblk_next;
2046 - if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) {
2047 - ASSERT(!hmeblkp->hblk_lckcnt);
2048 - sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
2049 - &list, 0);
2050 - } else {
2051 - pr_hblk = hmeblkp;
2052 - }
2053 - hmeblkp = nx_hblk;
2054 - }
2055 - SFMMU_HASH_UNLOCK(hmebp);
2056 - }
2057 -
2058 - sfmmu_hblks_list_purge(&list, 0);
2059 -
2060 - /*
2061 - * Now free up the ctx so that others can reuse it.
2062 - */
2063 - hatlockp = sfmmu_hat_enter(sfmmup);
2064 -
2065 - sfmmu_invalidate_ctx(sfmmup);
2066 -
2067 - /*
2068 - * Free TSBs, but not tsbinfos, and set SWAPPED flag.
2069 - * If TSBs were never swapped in, just return.
2070 - * This implies that we don't support partial swapping
2071 - * of TSBs -- either all are swapped out, or none are.
2072 - *
2073 - * We must hold the HAT lock here to prevent racing with another
2074 - * thread trying to unmap TTEs from the TSB or running the post-
2075 - * relocator after relocating the TSB's memory. Unfortunately, we
2076 - * can't free memory while holding the HAT lock or we could
2077 - * deadlock, so we build a list of TSBs to be freed after marking
2078 - * the tsbinfos as swapped out and free them after dropping the
2079 - * lock.
2080 - */
2081 - if (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) {
2082 - sfmmu_hat_exit(hatlockp);
2083 - return;
2084 - }
2085 -
2086 - SFMMU_FLAGS_SET(sfmmup, HAT_SWAPPED);
2087 - last = freelist = NULL;
2088 - for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL;
2089 - tsbinfop = tsbinfop->tsb_next) {
2090 - ASSERT((tsbinfop->tsb_flags & TSB_SWAPPED) == 0);
2091 -
2092 - /*
2093 - * Cast the TSB into a struct free_tsb and put it on the free
2094 - * list.
2095 - */
2096 - if (freelist == NULL) {
2097 - last = freelist = (struct free_tsb *)tsbinfop->tsb_va;
2098 - } else {
2099 - last->next = (struct free_tsb *)tsbinfop->tsb_va;
2100 - last = last->next;
2101 - }
2102 - last->next = NULL;
2103 - last->tsbinfop = tsbinfop;
2104 - tsbinfop->tsb_flags |= TSB_SWAPPED;
2105 - /*
2106 - * Zero out the TTE to clear the valid bit.
2107 - * Note we can't use a value like 0xbad because we want to
2108 - * ensure diagnostic bits are NEVER set on TTEs that might
2109 - * be loaded. The intent is to catch any invalid access
2110 - * to the swapped TSB, such as a thread running with a valid
2111 - * context without first calling sfmmu_tsb_swapin() to
2112 - * allocate TSB memory.
2113 - */
2114 - tsbinfop->tsb_tte.ll = 0;
2115 - }
2116 -
2117 - /* Now we can drop the lock and free the TSB memory. */
2118 - sfmmu_hat_exit(hatlockp);
2119 - for (; freelist != NULL; freelist = next) {
2120 - next = freelist->next;
2121 - sfmmu_tsb_free(freelist->tsbinfop);
2122 - }
2123 -}
2124 -
2125 -/*
2126 1979 * Duplicate the translations of an as into another newas
2127 1980 */
2128 1981 /* ARGSUSED */
2129 1982 int
2130 1983 hat_dup(struct hat *hat, struct hat *newhat, caddr_t addr, size_t len,
2131 1984 uint_t flag)
2132 1985 {
2133 1986 sf_srd_t *srdp;
2134 1987 sf_scd_t *scdp;
2135 1988 int i;
2136 1989 extern uint_t get_color_start(struct as *);
2137 1990
2138 1991 ASSERT(hat->sfmmu_xhat_provider == NULL);
2139 1992 ASSERT((flag == 0) || (flag == HAT_DUP_ALL) || (flag == HAT_DUP_COW) ||
2140 1993 (flag == HAT_DUP_SRD));
2141 1994 ASSERT(hat != ksfmmup);
2142 1995 ASSERT(newhat != ksfmmup);
2143 1996 ASSERT(flag != HAT_DUP_ALL || hat->sfmmu_srdp == newhat->sfmmu_srdp);
2144 1997
2145 1998 if (flag == HAT_DUP_COW) {
2146 1999 panic("hat_dup: HAT_DUP_COW not supported");
2147 2000 }
2148 2001
2149 2002 if (flag == HAT_DUP_SRD && ((srdp = hat->sfmmu_srdp) != NULL)) {
2150 2003 ASSERT(srdp->srd_evp != NULL);
2151 2004 VN_HOLD(srdp->srd_evp);
2152 2005 ASSERT(srdp->srd_refcnt > 0);
2153 2006 newhat->sfmmu_srdp = srdp;
2154 2007 atomic_add_32((volatile uint_t *)&srdp->srd_refcnt, 1);
2155 2008 }
2156 2009
2157 2010 /*
2158 2011 * HAT_DUP_ALL flag is used after as duplication is done.
2159 2012 */
2160 2013 if (flag == HAT_DUP_ALL && ((srdp = newhat->sfmmu_srdp) != NULL)) {
2161 2014 ASSERT(newhat->sfmmu_srdp->srd_refcnt >= 2);
2162 2015 newhat->sfmmu_rtteflags = hat->sfmmu_rtteflags;
2163 2016 if (hat->sfmmu_flags & HAT_4MTEXT_FLAG) {
2164 2017 newhat->sfmmu_flags |= HAT_4MTEXT_FLAG;
2165 2018 }
2166 2019
2167 2020 /* check if need to join scd */
2168 2021 if ((scdp = hat->sfmmu_scdp) != NULL &&
2169 2022 newhat->sfmmu_scdp != scdp) {
2170 2023 int ret;
2171 2024 SF_RGNMAP_IS_SUBSET(&newhat->sfmmu_region_map,
2172 2025 &scdp->scd_region_map, ret);
2173 2026 ASSERT(ret);
2174 2027 sfmmu_join_scd(scdp, newhat);
2175 2028 ASSERT(newhat->sfmmu_scdp == scdp &&
2176 2029 scdp->scd_refcnt >= 2);
2177 2030 for (i = 0; i < max_mmu_page_sizes; i++) {
2178 2031 newhat->sfmmu_ismttecnt[i] =
2179 2032 hat->sfmmu_ismttecnt[i];
2180 2033 newhat->sfmmu_scdismttecnt[i] =
2181 2034 hat->sfmmu_scdismttecnt[i];
2182 2035 }
2183 2036 }
2184 2037
2185 2038 sfmmu_check_page_sizes(newhat, 1);
2186 2039 }
2187 2040
2188 2041 if (flag == HAT_DUP_ALL && consistent_coloring == 0 &&
2189 2042 update_proc_pgcolorbase_after_fork != 0) {
2190 2043 hat->sfmmu_clrbin = get_color_start(hat->sfmmu_as);
2191 2044 }
2192 2045 return (0);
2193 2046 }
2194 2047
2195 2048 void
2196 2049 hat_memload(struct hat *hat, caddr_t addr, struct page *pp,
2197 2050 uint_t attr, uint_t flags)
2198 2051 {
2199 2052 hat_do_memload(hat, addr, pp, attr, flags,
2200 2053 SFMMU_INVALID_SHMERID);
2201 2054 }
2202 2055
2203 2056 void
2204 2057 hat_memload_region(struct hat *hat, caddr_t addr, struct page *pp,
2205 2058 uint_t attr, uint_t flags, hat_region_cookie_t rcookie)
2206 2059 {
2207 2060 uint_t rid;
2208 2061 if (rcookie == HAT_INVALID_REGION_COOKIE ||
2209 2062 hat->sfmmu_xhat_provider != NULL) {
2210 2063 hat_do_memload(hat, addr, pp, attr, flags,
2211 2064 SFMMU_INVALID_SHMERID);
2212 2065 return;
2213 2066 }
2214 2067 rid = (uint_t)((uint64_t)rcookie);
2215 2068 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
2216 2069 hat_do_memload(hat, addr, pp, attr, flags, rid);
2217 2070 }
2218 2071
2219 2072 /*
2220 2073 * Set up addr to map to page pp with protection prot.
2221 2074 * As an optimization we also load the TSB with the
2222 2075 * corresponding tte but it is no big deal if the tte gets kicked out.
2223 2076 */
2224 2077 static void
2225 2078 hat_do_memload(struct hat *hat, caddr_t addr, struct page *pp,
2226 2079 uint_t attr, uint_t flags, uint_t rid)
2227 2080 {
2228 2081 tte_t tte;
2229 2082
2230 2083
2231 2084 ASSERT(hat != NULL);
2232 2085 ASSERT(PAGE_LOCKED(pp));
2233 2086 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET));
2234 2087 ASSERT(!(flags & ~SFMMU_LOAD_ALLFLAG));
2235 2088 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR));
2236 2089 SFMMU_VALIDATE_HMERID(hat, rid, addr, MMU_PAGESIZE);
2237 2090
2238 2091 if (PP_ISFREE(pp)) {
2239 2092 panic("hat_memload: loading a mapping to free page %p",
2240 2093 (void *)pp);
2241 2094 }
2242 2095
2243 2096 if (hat->sfmmu_xhat_provider) {
2244 2097 /* no regions for xhats */
2245 2098 ASSERT(!SFMMU_IS_SHMERID_VALID(rid));
2246 2099 XHAT_MEMLOAD(hat, addr, pp, attr, flags);
2247 2100 return;
2248 2101 }
2249 2102
2250 2103 ASSERT((hat == ksfmmup) ||
2251 2104 AS_LOCK_HELD(hat->sfmmu_as, &hat->sfmmu_as->a_lock));
2252 2105
2253 2106 if (flags & ~SFMMU_LOAD_ALLFLAG)
2254 2107 cmn_err(CE_NOTE, "hat_memload: unsupported flags %d",
2255 2108 flags & ~SFMMU_LOAD_ALLFLAG);
2256 2109
2257 2110 if (hat->sfmmu_rmstat)
2258 2111 hat_resvstat(MMU_PAGESIZE, hat->sfmmu_as, addr);
2259 2112
2260 2113 #if defined(SF_ERRATA_57)
2261 2114 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) &&
2262 2115 (addr < errata57_limit) && (attr & PROT_EXEC) &&
2263 2116 !(flags & HAT_LOAD_SHARE)) {
2264 2117 cmn_err(CE_WARN, "hat_memload: illegal attempt to make user "
2265 2118 " page executable");
2266 2119 attr &= ~PROT_EXEC;
2267 2120 }
2268 2121 #endif
2269 2122
2270 2123 sfmmu_memtte(&tte, pp->p_pagenum, attr, TTE8K);
2271 2124 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, flags, rid);
2272 2125
2273 2126 /*
2274 2127 * Check TSB and TLB page sizes.
2275 2128 */
2276 2129 if ((flags & HAT_LOAD_SHARE) == 0) {
2277 2130 sfmmu_check_page_sizes(hat, 1);
2278 2131 }
2279 2132 }
2280 2133
2281 2134 /*
2282 2135 * hat_devload can be called to map real memory (e.g.
2283 2136 * /dev/kmem) and even though hat_devload will determine pf is
2284 2137 * for memory, it will be unable to get a shared lock on the
2285 2138 * page (because someone else has it exclusively) and will
2286 2139 * pass dp = NULL. If tteload doesn't get a non-NULL
2287 2140 * page pointer it can't cache memory.
2288 2141 */
2289 2142 void
2290 2143 hat_devload(struct hat *hat, caddr_t addr, size_t len, pfn_t pfn,
2291 2144 uint_t attr, int flags)
2292 2145 {
2293 2146 tte_t tte;
2294 2147 struct page *pp = NULL;
2295 2148 int use_lgpg = 0;
2296 2149
2297 2150 ASSERT(hat != NULL);
2298 2151
2299 2152 if (hat->sfmmu_xhat_provider) {
2300 2153 XHAT_DEVLOAD(hat, addr, len, pfn, attr, flags);
2301 2154 return;
2302 2155 }
2303 2156
2304 2157 ASSERT(!(flags & ~SFMMU_LOAD_ALLFLAG));
2305 2158 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR));
2306 2159 ASSERT((hat == ksfmmup) ||
2307 2160 AS_LOCK_HELD(hat->sfmmu_as, &hat->sfmmu_as->a_lock));
2308 2161 if (len == 0)
2309 2162 panic("hat_devload: zero len");
2310 2163 if (flags & ~SFMMU_LOAD_ALLFLAG)
2311 2164 cmn_err(CE_NOTE, "hat_devload: unsupported flags %d",
2312 2165 flags & ~SFMMU_LOAD_ALLFLAG);
2313 2166
2314 2167 #if defined(SF_ERRATA_57)
2315 2168 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) &&
2316 2169 (addr < errata57_limit) && (attr & PROT_EXEC) &&
2317 2170 !(flags & HAT_LOAD_SHARE)) {
2318 2171 cmn_err(CE_WARN, "hat_devload: illegal attempt to make user "
2319 2172 " page executable");
2320 2173 attr &= ~PROT_EXEC;
2321 2174 }
2322 2175 #endif
2323 2176
2324 2177 /*
2325 2178 * If it's a memory page find its pp
2326 2179 */
2327 2180 if (!(flags & HAT_LOAD_NOCONSIST) && pf_is_memory(pfn)) {
2328 2181 pp = page_numtopp_nolock(pfn);
2329 2182 if (pp == NULL) {
2330 2183 flags |= HAT_LOAD_NOCONSIST;
2331 2184 } else {
2332 2185 if (PP_ISFREE(pp)) {
2333 2186 panic("hat_memload: loading "
2334 2187 "a mapping to free page %p",
2335 2188 (void *)pp);
2336 2189 }
2337 2190 if (!PAGE_LOCKED(pp) && !PP_ISNORELOC(pp)) {
2338 2191 panic("hat_memload: loading a mapping "
2339 2192 "to unlocked relocatable page %p",
2340 2193 (void *)pp);
2341 2194 }
2342 2195 ASSERT(len == MMU_PAGESIZE);
2343 2196 }
2344 2197 }
2345 2198
2346 2199 if (hat->sfmmu_rmstat)
2347 2200 hat_resvstat(len, hat->sfmmu_as, addr);
2348 2201
2349 2202 if (flags & HAT_LOAD_NOCONSIST) {
2350 2203 attr |= SFMMU_UNCACHEVTTE;
2351 2204 use_lgpg = 1;
2352 2205 }
2353 2206 if (!pf_is_memory(pfn)) {
2354 2207 attr |= SFMMU_UNCACHEPTTE | HAT_NOSYNC;
2355 2208 use_lgpg = 1;
2356 2209 switch (attr & HAT_ORDER_MASK) {
2357 2210 case HAT_STRICTORDER:
2358 2211 case HAT_UNORDERED_OK:
2359 2212 /*
2360 2213 * we set the side effect bit for all non
2361 2214 * memory mappings unless merging is ok
2362 2215 */
2363 2216 attr |= SFMMU_SIDEFFECT;
2364 2217 break;
2365 2218 case HAT_MERGING_OK:
2366 2219 case HAT_LOADCACHING_OK:
2367 2220 case HAT_STORECACHING_OK:
2368 2221 break;
2369 2222 default:
2370 2223 panic("hat_devload: bad attr");
2371 2224 break;
2372 2225 }
2373 2226 }
2374 2227 while (len) {
2375 2228 if (!use_lgpg) {
2376 2229 sfmmu_memtte(&tte, pfn, attr, TTE8K);
2377 2230 (void) sfmmu_tteload_array(hat, &tte, addr, &pp,
2378 2231 flags, SFMMU_INVALID_SHMERID);
2379 2232 len -= MMU_PAGESIZE;
2380 2233 addr += MMU_PAGESIZE;
2381 2234 pfn++;
2382 2235 continue;
2383 2236 }
2384 2237 /*
2385 2238 * try to use large pages, check va/pa alignments
2386 2239 * Note that 32M/256M page sizes are not (yet) supported.
2387 2240 */
2388 2241 if ((len >= MMU_PAGESIZE4M) &&
2389 2242 !((uintptr_t)addr & MMU_PAGEOFFSET4M) &&
2390 2243 !(disable_large_pages & (1 << TTE4M)) &&
2391 2244 !(mmu_ptob(pfn) & MMU_PAGEOFFSET4M)) {
2392 2245 sfmmu_memtte(&tte, pfn, attr, TTE4M);
2393 2246 (void) sfmmu_tteload_array(hat, &tte, addr, &pp,
2394 2247 flags, SFMMU_INVALID_SHMERID);
2395 2248 len -= MMU_PAGESIZE4M;
2396 2249 addr += MMU_PAGESIZE4M;
2397 2250 pfn += MMU_PAGESIZE4M / MMU_PAGESIZE;
2398 2251 } else if ((len >= MMU_PAGESIZE512K) &&
2399 2252 !((uintptr_t)addr & MMU_PAGEOFFSET512K) &&
2400 2253 !(disable_large_pages & (1 << TTE512K)) &&
2401 2254 !(mmu_ptob(pfn) & MMU_PAGEOFFSET512K)) {
2402 2255 sfmmu_memtte(&tte, pfn, attr, TTE512K);
2403 2256 (void) sfmmu_tteload_array(hat, &tte, addr, &pp,
2404 2257 flags, SFMMU_INVALID_SHMERID);
2405 2258 len -= MMU_PAGESIZE512K;
2406 2259 addr += MMU_PAGESIZE512K;
2407 2260 pfn += MMU_PAGESIZE512K / MMU_PAGESIZE;
2408 2261 } else if ((len >= MMU_PAGESIZE64K) &&
2409 2262 !((uintptr_t)addr & MMU_PAGEOFFSET64K) &&
2410 2263 !(disable_large_pages & (1 << TTE64K)) &&
2411 2264 !(mmu_ptob(pfn) & MMU_PAGEOFFSET64K)) {
2412 2265 sfmmu_memtte(&tte, pfn, attr, TTE64K);
2413 2266 (void) sfmmu_tteload_array(hat, &tte, addr, &pp,
2414 2267 flags, SFMMU_INVALID_SHMERID);
2415 2268 len -= MMU_PAGESIZE64K;
2416 2269 addr += MMU_PAGESIZE64K;
2417 2270 pfn += MMU_PAGESIZE64K / MMU_PAGESIZE;
2418 2271 } else {
2419 2272 sfmmu_memtte(&tte, pfn, attr, TTE8K);
2420 2273 (void) sfmmu_tteload_array(hat, &tte, addr, &pp,
2421 2274 flags, SFMMU_INVALID_SHMERID);
2422 2275 len -= MMU_PAGESIZE;
2423 2276 addr += MMU_PAGESIZE;
2424 2277 pfn++;
2425 2278 }
2426 2279 }
2427 2280
2428 2281 /*
2429 2282 * Check TSB and TLB page sizes.
2430 2283 */
2431 2284 if ((flags & HAT_LOAD_SHARE) == 0) {
2432 2285 sfmmu_check_page_sizes(hat, 1);
2433 2286 }
2434 2287 }
2435 2288
2436 2289 void
2437 2290 hat_memload_array(struct hat *hat, caddr_t addr, size_t len,
2438 2291 struct page **pps, uint_t attr, uint_t flags)
2439 2292 {
2440 2293 hat_do_memload_array(hat, addr, len, pps, attr, flags,
2441 2294 SFMMU_INVALID_SHMERID);
2442 2295 }
2443 2296
2444 2297 void
2445 2298 hat_memload_array_region(struct hat *hat, caddr_t addr, size_t len,
2446 2299 struct page **pps, uint_t attr, uint_t flags,
2447 2300 hat_region_cookie_t rcookie)
2448 2301 {
2449 2302 uint_t rid;
2450 2303 if (rcookie == HAT_INVALID_REGION_COOKIE ||
2451 2304 hat->sfmmu_xhat_provider != NULL) {
2452 2305 hat_do_memload_array(hat, addr, len, pps, attr, flags,
2453 2306 SFMMU_INVALID_SHMERID);
2454 2307 return;
2455 2308 }
2456 2309 rid = (uint_t)((uint64_t)rcookie);
2457 2310 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
2458 2311 hat_do_memload_array(hat, addr, len, pps, attr, flags, rid);
2459 2312 }
2460 2313
2461 2314 /*
2462 2315 * Map the largest extend possible out of the page array. The array may NOT
2463 2316 * be in order. The largest possible mapping a page can have
2464 2317 * is specified in the p_szc field. The p_szc field
2465 2318 * cannot change as long as there any mappings (large or small)
2466 2319 * to any of the pages that make up the large page. (ie. any
2467 2320 * promotion/demotion of page size is not up to the hat but up to
2468 2321 * the page free list manager). The array
2469 2322 * should consist of properly aligned contigous pages that are
2470 2323 * part of a big page for a large mapping to be created.
2471 2324 */
2472 2325 static void
2473 2326 hat_do_memload_array(struct hat *hat, caddr_t addr, size_t len,
2474 2327 struct page **pps, uint_t attr, uint_t flags, uint_t rid)
2475 2328 {
2476 2329 int ttesz;
2477 2330 size_t mapsz;
2478 2331 pgcnt_t numpg, npgs;
2479 2332 tte_t tte;
2480 2333 page_t *pp;
2481 2334 uint_t large_pages_disable;
2482 2335
2483 2336 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET));
2484 2337 SFMMU_VALIDATE_HMERID(hat, rid, addr, len);
2485 2338
2486 2339 if (hat->sfmmu_xhat_provider) {
2487 2340 ASSERT(!SFMMU_IS_SHMERID_VALID(rid));
2488 2341 XHAT_MEMLOAD_ARRAY(hat, addr, len, pps, attr, flags);
2489 2342 return;
2490 2343 }
2491 2344
2492 2345 if (hat->sfmmu_rmstat)
2493 2346 hat_resvstat(len, hat->sfmmu_as, addr);
2494 2347
2495 2348 #if defined(SF_ERRATA_57)
2496 2349 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) &&
2497 2350 (addr < errata57_limit) && (attr & PROT_EXEC) &&
2498 2351 !(flags & HAT_LOAD_SHARE)) {
2499 2352 cmn_err(CE_WARN, "hat_memload_array: illegal attempt to make "
2500 2353 "user page executable");
2501 2354 attr &= ~PROT_EXEC;
2502 2355 }
2503 2356 #endif
2504 2357
2505 2358 /* Get number of pages */
2506 2359 npgs = len >> MMU_PAGESHIFT;
2507 2360
2508 2361 if (flags & HAT_LOAD_SHARE) {
2509 2362 large_pages_disable = disable_ism_large_pages;
2510 2363 } else {
2511 2364 large_pages_disable = disable_large_pages;
2512 2365 }
2513 2366
2514 2367 if (npgs < NHMENTS || large_pages_disable == LARGE_PAGES_OFF) {
2515 2368 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags, npgs,
2516 2369 rid);
2517 2370 return;
2518 2371 }
2519 2372
2520 2373 while (npgs >= NHMENTS) {
2521 2374 pp = *pps;
2522 2375 for (ttesz = pp->p_szc; ttesz != TTE8K; ttesz--) {
2523 2376 /*
2524 2377 * Check if this page size is disabled.
2525 2378 */
2526 2379 if (large_pages_disable & (1 << ttesz))
2527 2380 continue;
2528 2381
2529 2382 numpg = TTEPAGES(ttesz);
2530 2383 mapsz = numpg << MMU_PAGESHIFT;
2531 2384 if ((npgs >= numpg) &&
2532 2385 IS_P2ALIGNED(addr, mapsz) &&
2533 2386 IS_P2ALIGNED(pp->p_pagenum, numpg)) {
2534 2387 /*
2535 2388 * At this point we have enough pages and
2536 2389 * we know the virtual address and the pfn
2537 2390 * are properly aligned. We still need
2538 2391 * to check for physical contiguity but since
2539 2392 * it is very likely that this is the case
2540 2393 * we will assume they are so and undo
2541 2394 * the request if necessary. It would
2542 2395 * be great if we could get a hint flag
2543 2396 * like HAT_CONTIG which would tell us
2544 2397 * the pages are contigous for sure.
2545 2398 */
2546 2399 sfmmu_memtte(&tte, (*pps)->p_pagenum,
2547 2400 attr, ttesz);
2548 2401 if (!sfmmu_tteload_array(hat, &tte, addr,
2549 2402 pps, flags, rid)) {
2550 2403 break;
2551 2404 }
2552 2405 }
2553 2406 }
2554 2407 if (ttesz == TTE8K) {
2555 2408 /*
2556 2409 * We were not able to map array using a large page
2557 2410 * batch a hmeblk or fraction at a time.
2558 2411 */
2559 2412 numpg = ((uintptr_t)addr >> MMU_PAGESHIFT)
2560 2413 & (NHMENTS-1);
2561 2414 numpg = NHMENTS - numpg;
2562 2415 ASSERT(numpg <= npgs);
2563 2416 mapsz = numpg * MMU_PAGESIZE;
2564 2417 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags,
2565 2418 numpg, rid);
2566 2419 }
2567 2420 addr += mapsz;
2568 2421 npgs -= numpg;
2569 2422 pps += numpg;
2570 2423 }
2571 2424
2572 2425 if (npgs) {
2573 2426 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags, npgs,
2574 2427 rid);
2575 2428 }
2576 2429
2577 2430 /*
2578 2431 * Check TSB and TLB page sizes.
2579 2432 */
2580 2433 if ((flags & HAT_LOAD_SHARE) == 0) {
2581 2434 sfmmu_check_page_sizes(hat, 1);
2582 2435 }
2583 2436 }
2584 2437
2585 2438 /*
2586 2439 * Function tries to batch 8K pages into the same hme blk.
2587 2440 */
2588 2441 static void
2589 2442 sfmmu_memload_batchsmall(struct hat *hat, caddr_t vaddr, page_t **pps,
2590 2443 uint_t attr, uint_t flags, pgcnt_t npgs, uint_t rid)
2591 2444 {
2592 2445 tte_t tte;
2593 2446 page_t *pp;
2594 2447 struct hmehash_bucket *hmebp;
2595 2448 struct hme_blk *hmeblkp;
2596 2449 int index;
2597 2450
2598 2451 while (npgs) {
2599 2452 /*
2600 2453 * Acquire the hash bucket.
2601 2454 */
2602 2455 hmebp = sfmmu_tteload_acquire_hashbucket(hat, vaddr, TTE8K,
2603 2456 rid);
2604 2457 ASSERT(hmebp);
2605 2458
2606 2459 /*
2607 2460 * Find the hment block.
2608 2461 */
2609 2462 hmeblkp = sfmmu_tteload_find_hmeblk(hat, hmebp, vaddr,
2610 2463 TTE8K, flags, rid);
2611 2464 ASSERT(hmeblkp);
2612 2465
2613 2466 do {
2614 2467 /*
2615 2468 * Make the tte.
2616 2469 */
2617 2470 pp = *pps;
2618 2471 sfmmu_memtte(&tte, pp->p_pagenum, attr, TTE8K);
2619 2472
2620 2473 /*
2621 2474 * Add the translation.
2622 2475 */
2623 2476 (void) sfmmu_tteload_addentry(hat, hmeblkp, &tte,
2624 2477 vaddr, pps, flags, rid);
2625 2478
2626 2479 /*
2627 2480 * Goto next page.
2628 2481 */
2629 2482 pps++;
2630 2483 npgs--;
2631 2484
2632 2485 /*
2633 2486 * Goto next address.
2634 2487 */
2635 2488 vaddr += MMU_PAGESIZE;
2636 2489
2637 2490 /*
2638 2491 * Don't crossover into a different hmentblk.
2639 2492 */
2640 2493 index = (int)(((uintptr_t)vaddr >> MMU_PAGESHIFT) &
2641 2494 (NHMENTS-1));
2642 2495
2643 2496 } while (index != 0 && npgs != 0);
2644 2497
2645 2498 /*
2646 2499 * Release the hash bucket.
2647 2500 */
2648 2501
2649 2502 sfmmu_tteload_release_hashbucket(hmebp);
2650 2503 }
2651 2504 }
2652 2505
2653 2506 /*
2654 2507 * Construct a tte for a page:
2655 2508 *
2656 2509 * tte_valid = 1
2657 2510 * tte_size2 = size & TTE_SZ2_BITS (Panther and Olympus-C only)
2658 2511 * tte_size = size
2659 2512 * tte_nfo = attr & HAT_NOFAULT
2660 2513 * tte_ie = attr & HAT_STRUCTURE_LE
2661 2514 * tte_hmenum = hmenum
2662 2515 * tte_pahi = pp->p_pagenum >> TTE_PASHIFT;
2663 2516 * tte_palo = pp->p_pagenum & TTE_PALOMASK;
2664 2517 * tte_ref = 1 (optimization)
2665 2518 * tte_wr_perm = attr & PROT_WRITE;
2666 2519 * tte_no_sync = attr & HAT_NOSYNC
2667 2520 * tte_lock = attr & SFMMU_LOCKTTE
2668 2521 * tte_cp = !(attr & SFMMU_UNCACHEPTTE)
2669 2522 * tte_cv = !(attr & SFMMU_UNCACHEVTTE)
2670 2523 * tte_e = attr & SFMMU_SIDEFFECT
2671 2524 * tte_priv = !(attr & PROT_USER)
2672 2525 * tte_hwwr = if nosync is set and it is writable we set the mod bit (opt)
2673 2526 * tte_glb = 0
2674 2527 */
2675 2528 void
2676 2529 sfmmu_memtte(tte_t *ttep, pfn_t pfn, uint_t attr, int tte_sz)
2677 2530 {
2678 2531 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR));
2679 2532
2680 2533 ttep->tte_inthi = MAKE_TTE_INTHI(pfn, attr, tte_sz, 0 /* hmenum */);
2681 2534 ttep->tte_intlo = MAKE_TTE_INTLO(pfn, attr, tte_sz, 0 /* hmenum */);
2682 2535
2683 2536 if (TTE_IS_NOSYNC(ttep)) {
2684 2537 TTE_SET_REF(ttep);
2685 2538 if (TTE_IS_WRITABLE(ttep)) {
2686 2539 TTE_SET_MOD(ttep);
2687 2540 }
2688 2541 }
2689 2542 if (TTE_IS_NFO(ttep) && TTE_IS_EXECUTABLE(ttep)) {
2690 2543 panic("sfmmu_memtte: can't set both NFO and EXEC bits");
2691 2544 }
2692 2545 }
2693 2546
2694 2547 /*
2695 2548 * This function will add a translation to the hme_blk and allocate the
2696 2549 * hme_blk if one does not exist.
2697 2550 * If a page structure is specified then it will add the
2698 2551 * corresponding hment to the mapping list.
2699 2552 * It will also update the hmenum field for the tte.
2700 2553 *
2701 2554 * Currently this function is only used for kernel mappings.
2702 2555 * So pass invalid region to sfmmu_tteload_array().
2703 2556 */
2704 2557 void
2705 2558 sfmmu_tteload(struct hat *sfmmup, tte_t *ttep, caddr_t vaddr, page_t *pp,
2706 2559 uint_t flags)
2707 2560 {
2708 2561 ASSERT(sfmmup == ksfmmup);
2709 2562 (void) sfmmu_tteload_array(sfmmup, ttep, vaddr, &pp, flags,
2710 2563 SFMMU_INVALID_SHMERID);
2711 2564 }
2712 2565
2713 2566 /*
2714 2567 * Load (ttep != NULL) or unload (ttep == NULL) one entry in the TSB.
2715 2568 * Assumes that a particular page size may only be resident in one TSB.
2716 2569 */
2717 2570 static void
2718 2571 sfmmu_mod_tsb(sfmmu_t *sfmmup, caddr_t vaddr, tte_t *ttep, int ttesz)
2719 2572 {
2720 2573 struct tsb_info *tsbinfop = NULL;
2721 2574 uint64_t tag;
2722 2575 struct tsbe *tsbe_addr;
2723 2576 uint64_t tsb_base;
2724 2577 uint_t tsb_size;
2725 2578 int vpshift = MMU_PAGESHIFT;
2726 2579 int phys = 0;
2727 2580
2728 2581 if (sfmmup == ksfmmup) { /* No support for 32/256M ksfmmu pages */
2729 2582 phys = ktsb_phys;
2730 2583 if (ttesz >= TTE4M) {
2731 2584 #ifndef sun4v
2732 2585 ASSERT((ttesz != TTE32M) && (ttesz != TTE256M));
2733 2586 #endif
2734 2587 tsb_base = (phys)? ktsb4m_pbase : (uint64_t)ktsb4m_base;
2735 2588 tsb_size = ktsb4m_szcode;
2736 2589 } else {
2737 2590 tsb_base = (phys)? ktsb_pbase : (uint64_t)ktsb_base;
2738 2591 tsb_size = ktsb_szcode;
2739 2592 }
2740 2593 } else {
2741 2594 SFMMU_GET_TSBINFO(tsbinfop, sfmmup, ttesz);
2742 2595
2743 2596 /*
2744 2597 * If there isn't a TSB for this page size, or the TSB is
2745 2598 * swapped out, there is nothing to do. Note that the latter
2746 2599 * case seems impossible but can occur if hat_pageunload()
2747 2600 * is called on an ISM mapping while the process is swapped
2748 2601 * out.
2749 2602 */
2750 2603 if (tsbinfop == NULL || (tsbinfop->tsb_flags & TSB_SWAPPED))
2751 2604 return;
2752 2605
2753 2606 /*
2754 2607 * If another thread is in the middle of relocating a TSB
2755 2608 * we can't unload the entry so set a flag so that the
2756 2609 * TSB will be flushed before it can be accessed by the
2757 2610 * process.
2758 2611 */
2759 2612 if ((tsbinfop->tsb_flags & TSB_RELOC_FLAG) != 0) {
2760 2613 if (ttep == NULL)
2761 2614 tsbinfop->tsb_flags |= TSB_FLUSH_NEEDED;
2762 2615 return;
2763 2616 }
2764 2617 #if defined(UTSB_PHYS)
2765 2618 phys = 1;
2766 2619 tsb_base = (uint64_t)tsbinfop->tsb_pa;
2767 2620 #else
2768 2621 tsb_base = (uint64_t)tsbinfop->tsb_va;
2769 2622 #endif
2770 2623 tsb_size = tsbinfop->tsb_szc;
2771 2624 }
2772 2625 if (ttesz >= TTE4M)
2773 2626 vpshift = MMU_PAGESHIFT4M;
2774 2627
2775 2628 tsbe_addr = sfmmu_get_tsbe(tsb_base, vaddr, vpshift, tsb_size);
2776 2629 tag = sfmmu_make_tsbtag(vaddr);
2777 2630
2778 2631 if (ttep == NULL) {
2779 2632 sfmmu_unload_tsbe(tsbe_addr, tag, phys);
2780 2633 } else {
2781 2634 if (ttesz >= TTE4M) {
2782 2635 SFMMU_STAT(sf_tsb_load4m);
2783 2636 } else {
2784 2637 SFMMU_STAT(sf_tsb_load8k);
2785 2638 }
2786 2639
2787 2640 sfmmu_load_tsbe(tsbe_addr, tag, ttep, phys);
2788 2641 }
2789 2642 }
2790 2643
2791 2644 /*
2792 2645 * Unmap all entries from [start, end) matching the given page size.
2793 2646 *
2794 2647 * This function is used primarily to unmap replicated 64K or 512K entries
2795 2648 * from the TSB that are inserted using the base page size TSB pointer, but
2796 2649 * it may also be called to unmap a range of addresses from the TSB.
2797 2650 */
2798 2651 void
2799 2652 sfmmu_unload_tsb_range(sfmmu_t *sfmmup, caddr_t start, caddr_t end, int ttesz)
2800 2653 {
2801 2654 struct tsb_info *tsbinfop;
2802 2655 uint64_t tag;
2803 2656 struct tsbe *tsbe_addr;
2804 2657 caddr_t vaddr;
2805 2658 uint64_t tsb_base;
2806 2659 int vpshift, vpgsz;
2807 2660 uint_t tsb_size;
2808 2661 int phys = 0;
2809 2662
2810 2663 /*
2811 2664 * Assumptions:
2812 2665 * If ttesz == 8K, 64K or 512K, we walk through the range 8K
2813 2666 * at a time shooting down any valid entries we encounter.
2814 2667 *
2815 2668 * If ttesz >= 4M we walk the range 4M at a time shooting
2816 2669 * down any valid mappings we find.
2817 2670 */
2818 2671 if (sfmmup == ksfmmup) {
2819 2672 phys = ktsb_phys;
2820 2673 if (ttesz >= TTE4M) {
2821 2674 #ifndef sun4v
2822 2675 ASSERT((ttesz != TTE32M) && (ttesz != TTE256M));
2823 2676 #endif
2824 2677 tsb_base = (phys)? ktsb4m_pbase : (uint64_t)ktsb4m_base;
2825 2678 tsb_size = ktsb4m_szcode;
2826 2679 } else {
2827 2680 tsb_base = (phys)? ktsb_pbase : (uint64_t)ktsb_base;
2828 2681 tsb_size = ktsb_szcode;
2829 2682 }
2830 2683 } else {
2831 2684 SFMMU_GET_TSBINFO(tsbinfop, sfmmup, ttesz);
2832 2685
2833 2686 /*
2834 2687 * If there isn't a TSB for this page size, or the TSB is
2835 2688 * swapped out, there is nothing to do. Note that the latter
2836 2689 * case seems impossible but can occur if hat_pageunload()
2837 2690 * is called on an ISM mapping while the process is swapped
2838 2691 * out.
2839 2692 */
2840 2693 if (tsbinfop == NULL || (tsbinfop->tsb_flags & TSB_SWAPPED))
2841 2694 return;
2842 2695
2843 2696 /*
2844 2697 * If another thread is in the middle of relocating a TSB
2845 2698 * we can't unload the entry so set a flag so that the
2846 2699 * TSB will be flushed before it can be accessed by the
2847 2700 * process.
2848 2701 */
2849 2702 if ((tsbinfop->tsb_flags & TSB_RELOC_FLAG) != 0) {
2850 2703 tsbinfop->tsb_flags |= TSB_FLUSH_NEEDED;
2851 2704 return;
2852 2705 }
2853 2706 #if defined(UTSB_PHYS)
2854 2707 phys = 1;
2855 2708 tsb_base = (uint64_t)tsbinfop->tsb_pa;
2856 2709 #else
2857 2710 tsb_base = (uint64_t)tsbinfop->tsb_va;
2858 2711 #endif
2859 2712 tsb_size = tsbinfop->tsb_szc;
2860 2713 }
2861 2714 if (ttesz >= TTE4M) {
2862 2715 vpshift = MMU_PAGESHIFT4M;
2863 2716 vpgsz = MMU_PAGESIZE4M;
2864 2717 } else {
2865 2718 vpshift = MMU_PAGESHIFT;
2866 2719 vpgsz = MMU_PAGESIZE;
2867 2720 }
2868 2721
2869 2722 for (vaddr = start; vaddr < end; vaddr += vpgsz) {
2870 2723 tag = sfmmu_make_tsbtag(vaddr);
2871 2724 tsbe_addr = sfmmu_get_tsbe(tsb_base, vaddr, vpshift, tsb_size);
2872 2725 sfmmu_unload_tsbe(tsbe_addr, tag, phys);
2873 2726 }
2874 2727 }
2875 2728
2876 2729 /*
2877 2730 * Select the optimum TSB size given the number of mappings
2878 2731 * that need to be cached.
2879 2732 */
2880 2733 static int
2881 2734 sfmmu_select_tsb_szc(pgcnt_t pgcnt)
2882 2735 {
2883 2736 int szc = 0;
2884 2737
2885 2738 #ifdef DEBUG
2886 2739 if (tsb_grow_stress) {
2887 2740 uint32_t randval = (uint32_t)gettick() >> 4;
2888 2741 return (randval % (tsb_max_growsize + 1));
2889 2742 }
2890 2743 #endif /* DEBUG */
2891 2744
2892 2745 while ((szc < tsb_max_growsize) && (pgcnt > SFMMU_RSS_TSBSIZE(szc)))
2893 2746 szc++;
2894 2747 return (szc);
2895 2748 }
2896 2749
2897 2750 /*
2898 2751 * This function will add a translation to the hme_blk and allocate the
2899 2752 * hme_blk if one does not exist.
2900 2753 * If a page structure is specified then it will add the
2901 2754 * corresponding hment to the mapping list.
2902 2755 * It will also update the hmenum field for the tte.
2903 2756 * Furthermore, it attempts to create a large page translation
2904 2757 * for <addr,hat> at page array pps. It assumes addr and first
2905 2758 * pp is correctly aligned. It returns 0 if successful and 1 otherwise.
2906 2759 */
2907 2760 static int
2908 2761 sfmmu_tteload_array(sfmmu_t *sfmmup, tte_t *ttep, caddr_t vaddr,
2909 2762 page_t **pps, uint_t flags, uint_t rid)
2910 2763 {
2911 2764 struct hmehash_bucket *hmebp;
2912 2765 struct hme_blk *hmeblkp;
2913 2766 int ret;
2914 2767 uint_t size;
2915 2768
2916 2769 /*
2917 2770 * Get mapping size.
2918 2771 */
2919 2772 size = TTE_CSZ(ttep);
2920 2773 ASSERT(!((uintptr_t)vaddr & TTE_PAGE_OFFSET(size)));
2921 2774
2922 2775 /*
2923 2776 * Acquire the hash bucket.
2924 2777 */
2925 2778 hmebp = sfmmu_tteload_acquire_hashbucket(sfmmup, vaddr, size, rid);
2926 2779 ASSERT(hmebp);
2927 2780
2928 2781 /*
2929 2782 * Find the hment block.
2930 2783 */
2931 2784 hmeblkp = sfmmu_tteload_find_hmeblk(sfmmup, hmebp, vaddr, size, flags,
2932 2785 rid);
2933 2786 ASSERT(hmeblkp);
2934 2787
2935 2788 /*
2936 2789 * Add the translation.
2937 2790 */
2938 2791 ret = sfmmu_tteload_addentry(sfmmup, hmeblkp, ttep, vaddr, pps, flags,
2939 2792 rid);
2940 2793
2941 2794 /*
2942 2795 * Release the hash bucket.
2943 2796 */
2944 2797 sfmmu_tteload_release_hashbucket(hmebp);
2945 2798
2946 2799 return (ret);
2947 2800 }
2948 2801
2949 2802 /*
2950 2803 * Function locks and returns a pointer to the hash bucket for vaddr and size.
2951 2804 */
2952 2805 static struct hmehash_bucket *
2953 2806 sfmmu_tteload_acquire_hashbucket(sfmmu_t *sfmmup, caddr_t vaddr, int size,
2954 2807 uint_t rid)
2955 2808 {
2956 2809 struct hmehash_bucket *hmebp;
2957 2810 int hmeshift;
2958 2811 void *htagid = sfmmutohtagid(sfmmup, rid);
2959 2812
2960 2813 ASSERT(htagid != NULL);
2961 2814
2962 2815 hmeshift = HME_HASH_SHIFT(size);
2963 2816
2964 2817 hmebp = HME_HASH_FUNCTION(htagid, vaddr, hmeshift);
2965 2818
2966 2819 SFMMU_HASH_LOCK(hmebp);
2967 2820
2968 2821 return (hmebp);
2969 2822 }
2970 2823
2971 2824 /*
2972 2825 * Function returns a pointer to an hmeblk in the hash bucket, hmebp. If the
2973 2826 * hmeblk doesn't exists for the [sfmmup, vaddr & size] signature, a hmeblk is
2974 2827 * allocated.
2975 2828 */
2976 2829 static struct hme_blk *
2977 2830 sfmmu_tteload_find_hmeblk(sfmmu_t *sfmmup, struct hmehash_bucket *hmebp,
2978 2831 caddr_t vaddr, uint_t size, uint_t flags, uint_t rid)
2979 2832 {
2980 2833 hmeblk_tag hblktag;
2981 2834 int hmeshift;
2982 2835 struct hme_blk *hmeblkp, *pr_hblk, *list = NULL;
2983 2836
2984 2837 SFMMU_VALIDATE_HMERID(sfmmup, rid, vaddr, TTEBYTES(size));
2985 2838
2986 2839 hblktag.htag_id = sfmmutohtagid(sfmmup, rid);
2987 2840 ASSERT(hblktag.htag_id != NULL);
2988 2841 hmeshift = HME_HASH_SHIFT(size);
2989 2842 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift);
2990 2843 hblktag.htag_rehash = HME_HASH_REHASH(size);
2991 2844 hblktag.htag_rid = rid;
2992 2845
2993 2846 ttearray_realloc:
2994 2847
2995 2848 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, pr_hblk, &list);
2996 2849
2997 2850 /*
2998 2851 * We block until hblk_reserve_lock is released; it's held by
2999 2852 * the thread, temporarily using hblk_reserve, until hblk_reserve is
3000 2853 * replaced by a hblk from sfmmu8_cache.
3001 2854 */
3002 2855 if (hmeblkp == (struct hme_blk *)hblk_reserve &&
3003 2856 hblk_reserve_thread != curthread) {
3004 2857 SFMMU_HASH_UNLOCK(hmebp);
3005 2858 mutex_enter(&hblk_reserve_lock);
3006 2859 mutex_exit(&hblk_reserve_lock);
3007 2860 SFMMU_STAT(sf_hblk_reserve_hit);
3008 2861 SFMMU_HASH_LOCK(hmebp);
3009 2862 goto ttearray_realloc;
3010 2863 }
3011 2864
3012 2865 if (hmeblkp == NULL) {
3013 2866 hmeblkp = sfmmu_hblk_alloc(sfmmup, vaddr, hmebp, size,
3014 2867 hblktag, flags, rid);
3015 2868 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || hmeblkp->hblk_shared);
3016 2869 ASSERT(SFMMU_IS_SHMERID_VALID(rid) || !hmeblkp->hblk_shared);
3017 2870 } else {
3018 2871 /*
3019 2872 * It is possible for 8k and 64k hblks to collide since they
3020 2873 * have the same rehash value. This is because we
3021 2874 * lazily free hblks and 8K/64K blks could be lingering.
3022 2875 * If we find size mismatch we free the block and & try again.
3023 2876 */
3024 2877 if (get_hblk_ttesz(hmeblkp) != size) {
3025 2878 ASSERT(!hmeblkp->hblk_vcnt);
3026 2879 ASSERT(!hmeblkp->hblk_hmecnt);
3027 2880 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
3028 2881 &list, 0);
3029 2882 goto ttearray_realloc;
3030 2883 }
3031 2884 if (hmeblkp->hblk_shw_bit) {
3032 2885 /*
3033 2886 * if the hblk was previously used as a shadow hblk then
3034 2887 * we will change it to a normal hblk
3035 2888 */
3036 2889 ASSERT(!hmeblkp->hblk_shared);
3037 2890 if (hmeblkp->hblk_shw_mask) {
3038 2891 sfmmu_shadow_hcleanup(sfmmup, hmeblkp, hmebp);
3039 2892 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
3040 2893 goto ttearray_realloc;
3041 2894 } else {
3042 2895 hmeblkp->hblk_shw_bit = 0;
3043 2896 }
3044 2897 }
3045 2898 SFMMU_STAT(sf_hblk_hit);
3046 2899 }
3047 2900
3048 2901 /*
3049 2902 * hat_memload() should never call kmem_cache_free() for kernel hmeblks;
3050 2903 * see block comment showing the stacktrace in sfmmu_hblk_alloc();
3051 2904 * set the flag parameter to 1 so that sfmmu_hblks_list_purge() will
3052 2905 * just add these hmeblks to the per-cpu pending queue.
3053 2906 */
3054 2907 sfmmu_hblks_list_purge(&list, 1);
3055 2908
3056 2909 ASSERT(get_hblk_ttesz(hmeblkp) == size);
3057 2910 ASSERT(!hmeblkp->hblk_shw_bit);
3058 2911 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || hmeblkp->hblk_shared);
3059 2912 ASSERT(SFMMU_IS_SHMERID_VALID(rid) || !hmeblkp->hblk_shared);
3060 2913 ASSERT(hmeblkp->hblk_tag.htag_rid == rid);
3061 2914
3062 2915 return (hmeblkp);
3063 2916 }
3064 2917
3065 2918 /*
3066 2919 * Function adds a tte entry into the hmeblk. It returns 0 if successful and 1
3067 2920 * otherwise.
3068 2921 */
3069 2922 static int
3070 2923 sfmmu_tteload_addentry(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, tte_t *ttep,
3071 2924 caddr_t vaddr, page_t **pps, uint_t flags, uint_t rid)
3072 2925 {
3073 2926 page_t *pp = *pps;
3074 2927 int hmenum, size, remap;
3075 2928 tte_t tteold, flush_tte;
3076 2929 #ifdef DEBUG
3077 2930 tte_t orig_old;
3078 2931 #endif /* DEBUG */
3079 2932 struct sf_hment *sfhme;
3080 2933 kmutex_t *pml, *pmtx;
3081 2934 hatlock_t *hatlockp;
3082 2935 int myflt;
3083 2936
3084 2937 /*
3085 2938 * remove this panic when we decide to let user virtual address
3086 2939 * space be >= USERLIMIT.
3087 2940 */
3088 2941 if (!TTE_IS_PRIVILEGED(ttep) && vaddr >= (caddr_t)USERLIMIT)
3089 2942 panic("user addr %p in kernel space", (void *)vaddr);
3090 2943 #if defined(TTE_IS_GLOBAL)
3091 2944 if (TTE_IS_GLOBAL(ttep))
3092 2945 panic("sfmmu_tteload: creating global tte");
3093 2946 #endif
3094 2947
3095 2948 #ifdef DEBUG
3096 2949 if (pf_is_memory(sfmmu_ttetopfn(ttep, vaddr)) &&
3097 2950 !TTE_IS_PCACHEABLE(ttep) && !sfmmu_allow_nc_trans)
3098 2951 panic("sfmmu_tteload: non cacheable memory tte");
3099 2952 #endif /* DEBUG */
3100 2953
3101 2954 /* don't simulate dirty bit for writeable ISM/DISM mappings */
3102 2955 if ((flags & HAT_LOAD_SHARE) && TTE_IS_WRITABLE(ttep)) {
3103 2956 TTE_SET_REF(ttep);
3104 2957 TTE_SET_MOD(ttep);
3105 2958 }
3106 2959
3107 2960 if ((flags & HAT_LOAD_SHARE) || !TTE_IS_REF(ttep) ||
3108 2961 !TTE_IS_MOD(ttep)) {
3109 2962 /*
3110 2963 * Don't load TSB for dummy as in ISM. Also don't preload
3111 2964 * the TSB if the TTE isn't writable since we're likely to
3112 2965 * fault on it again -- preloading can be fairly expensive.
3113 2966 */
3114 2967 flags |= SFMMU_NO_TSBLOAD;
3115 2968 }
3116 2969
3117 2970 size = TTE_CSZ(ttep);
3118 2971 switch (size) {
3119 2972 case TTE8K:
3120 2973 SFMMU_STAT(sf_tteload8k);
3121 2974 break;
3122 2975 case TTE64K:
3123 2976 SFMMU_STAT(sf_tteload64k);
3124 2977 break;
3125 2978 case TTE512K:
3126 2979 SFMMU_STAT(sf_tteload512k);
3127 2980 break;
3128 2981 case TTE4M:
3129 2982 SFMMU_STAT(sf_tteload4m);
3130 2983 break;
3131 2984 case (TTE32M):
3132 2985 SFMMU_STAT(sf_tteload32m);
3133 2986 ASSERT(mmu_page_sizes == max_mmu_page_sizes);
3134 2987 break;
3135 2988 case (TTE256M):
3136 2989 SFMMU_STAT(sf_tteload256m);
3137 2990 ASSERT(mmu_page_sizes == max_mmu_page_sizes);
3138 2991 break;
3139 2992 }
3140 2993
3141 2994 ASSERT(!((uintptr_t)vaddr & TTE_PAGE_OFFSET(size)));
3142 2995 SFMMU_VALIDATE_HMERID(sfmmup, rid, vaddr, TTEBYTES(size));
3143 2996 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || hmeblkp->hblk_shared);
3144 2997 ASSERT(SFMMU_IS_SHMERID_VALID(rid) || !hmeblkp->hblk_shared);
3145 2998
3146 2999 HBLKTOHME_IDX(sfhme, hmeblkp, vaddr, hmenum);
3147 3000
3148 3001 /*
3149 3002 * Need to grab mlist lock here so that pageunload
3150 3003 * will not change tte behind us.
3151 3004 */
3152 3005 if (pp) {
3153 3006 pml = sfmmu_mlist_enter(pp);
3154 3007 }
3155 3008
3156 3009 sfmmu_copytte(&sfhme->hme_tte, &tteold);
3157 3010 /*
3158 3011 * Look for corresponding hment and if valid verify
3159 3012 * pfns are equal.
3160 3013 */
3161 3014 remap = TTE_IS_VALID(&tteold);
3162 3015 if (remap) {
3163 3016 pfn_t new_pfn, old_pfn;
3164 3017
3165 3018 old_pfn = TTE_TO_PFN(vaddr, &tteold);
3166 3019 new_pfn = TTE_TO_PFN(vaddr, ttep);
3167 3020
3168 3021 if (flags & HAT_LOAD_REMAP) {
3169 3022 /* make sure we are remapping same type of pages */
3170 3023 if (pf_is_memory(old_pfn) != pf_is_memory(new_pfn)) {
3171 3024 panic("sfmmu_tteload - tte remap io<->memory");
3172 3025 }
3173 3026 if (old_pfn != new_pfn &&
3174 3027 (pp != NULL || sfhme->hme_page != NULL)) {
3175 3028 panic("sfmmu_tteload - tte remap pp != NULL");
3176 3029 }
3177 3030 } else if (old_pfn != new_pfn) {
3178 3031 panic("sfmmu_tteload - tte remap, hmeblkp 0x%p",
3179 3032 (void *)hmeblkp);
3180 3033 }
3181 3034 ASSERT(TTE_CSZ(&tteold) == TTE_CSZ(ttep));
3182 3035 }
3183 3036
3184 3037 if (pp) {
3185 3038 if (size == TTE8K) {
3186 3039 #ifdef VAC
3187 3040 /*
3188 3041 * Handle VAC consistency
3189 3042 */
3190 3043 if (!remap && (cache & CACHE_VAC) && !PP_ISNC(pp)) {
3191 3044 sfmmu_vac_conflict(sfmmup, vaddr, pp);
3192 3045 }
3193 3046 #endif
3194 3047
3195 3048 if (TTE_IS_WRITABLE(ttep) && PP_ISRO(pp)) {
3196 3049 pmtx = sfmmu_page_enter(pp);
3197 3050 PP_CLRRO(pp);
3198 3051 sfmmu_page_exit(pmtx);
3199 3052 } else if (!PP_ISMAPPED(pp) &&
3200 3053 (!TTE_IS_WRITABLE(ttep)) && !(PP_ISMOD(pp))) {
3201 3054 pmtx = sfmmu_page_enter(pp);
3202 3055 if (!(PP_ISMOD(pp))) {
3203 3056 PP_SETRO(pp);
3204 3057 }
3205 3058 sfmmu_page_exit(pmtx);
3206 3059 }
3207 3060
3208 3061 } else if (sfmmu_pagearray_setup(vaddr, pps, ttep, remap)) {
3209 3062 /*
3210 3063 * sfmmu_pagearray_setup failed so return
3211 3064 */
3212 3065 sfmmu_mlist_exit(pml);
3213 3066 return (1);
3214 3067 }
3215 3068 }
3216 3069
3217 3070 /*
3218 3071 * Make sure hment is not on a mapping list.
3219 3072 */
3220 3073 ASSERT(remap || (sfhme->hme_page == NULL));
3221 3074
3222 3075 /* if it is not a remap then hme->next better be NULL */
3223 3076 ASSERT((!remap) ? sfhme->hme_next == NULL : 1);
3224 3077
3225 3078 if (flags & HAT_LOAD_LOCK) {
3226 3079 if ((hmeblkp->hblk_lckcnt + 1) >= MAX_HBLK_LCKCNT) {
3227 3080 panic("too high lckcnt-hmeblk %p",
3228 3081 (void *)hmeblkp);
3229 3082 }
3230 3083 atomic_add_32(&hmeblkp->hblk_lckcnt, 1);
3231 3084
3232 3085 HBLK_STACK_TRACE(hmeblkp, HBLK_LOCK);
3233 3086 }
3234 3087
3235 3088 #ifdef VAC
3236 3089 if (pp && PP_ISNC(pp)) {
3237 3090 /*
3238 3091 * If the physical page is marked to be uncacheable, like
3239 3092 * by a vac conflict, make sure the new mapping is also
3240 3093 * uncacheable.
3241 3094 */
3242 3095 TTE_CLR_VCACHEABLE(ttep);
3243 3096 ASSERT(PP_GET_VCOLOR(pp) == NO_VCOLOR);
3244 3097 }
3245 3098 #endif
3246 3099 ttep->tte_hmenum = hmenum;
3247 3100
3248 3101 #ifdef DEBUG
3249 3102 orig_old = tteold;
3250 3103 #endif /* DEBUG */
3251 3104
3252 3105 while (sfmmu_modifytte_try(&tteold, ttep, &sfhme->hme_tte) < 0) {
3253 3106 if ((sfmmup == KHATID) &&
3254 3107 (flags & (HAT_LOAD_LOCK | HAT_LOAD_REMAP))) {
3255 3108 sfmmu_copytte(&sfhme->hme_tte, &tteold);
3256 3109 }
3257 3110 #ifdef DEBUG
3258 3111 chk_tte(&orig_old, &tteold, ttep, hmeblkp);
3259 3112 #endif /* DEBUG */
3260 3113 }
3261 3114 ASSERT(TTE_IS_VALID(&sfhme->hme_tte));
3262 3115
3263 3116 if (!TTE_IS_VALID(&tteold)) {
3264 3117
3265 3118 atomic_add_16(&hmeblkp->hblk_vcnt, 1);
3266 3119 if (rid == SFMMU_INVALID_SHMERID) {
3267 3120 atomic_add_long(&sfmmup->sfmmu_ttecnt[size], 1);
3268 3121 } else {
3269 3122 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
3270 3123 sf_region_t *rgnp = srdp->srd_hmergnp[rid];
3271 3124 /*
3272 3125 * We already accounted for region ttecnt's in sfmmu
3273 3126 * during hat_join_region() processing. Here we
3274 3127 * only update ttecnt's in region struture.
3275 3128 */
3276 3129 atomic_add_long(&rgnp->rgn_ttecnt[size], 1);
3277 3130 }
3278 3131 }
3279 3132
3280 3133 myflt = (astosfmmu(curthread->t_procp->p_as) == sfmmup);
3281 3134 if (size > TTE8K && (flags & HAT_LOAD_SHARE) == 0 &&
3282 3135 sfmmup != ksfmmup) {
3283 3136 uchar_t tteflag = 1 << size;
3284 3137 if (rid == SFMMU_INVALID_SHMERID) {
3285 3138 if (!(sfmmup->sfmmu_tteflags & tteflag)) {
3286 3139 hatlockp = sfmmu_hat_enter(sfmmup);
3287 3140 sfmmup->sfmmu_tteflags |= tteflag;
3288 3141 sfmmu_hat_exit(hatlockp);
3289 3142 }
3290 3143 } else if (!(sfmmup->sfmmu_rtteflags & tteflag)) {
3291 3144 hatlockp = sfmmu_hat_enter(sfmmup);
3292 3145 sfmmup->sfmmu_rtteflags |= tteflag;
3293 3146 sfmmu_hat_exit(hatlockp);
3294 3147 }
3295 3148 /*
3296 3149 * Update the current CPU tsbmiss area, so the current thread
3297 3150 * won't need to take the tsbmiss for the new pagesize.
3298 3151 * The other threads in the process will update their tsb
3299 3152 * miss area lazily in sfmmu_tsbmiss_exception() when they
3300 3153 * fail to find the translation for a newly added pagesize.
3301 3154 */
3302 3155 if (size > TTE64K && myflt) {
3303 3156 struct tsbmiss *tsbmp;
3304 3157 kpreempt_disable();
3305 3158 tsbmp = &tsbmiss_area[CPU->cpu_id];
3306 3159 if (rid == SFMMU_INVALID_SHMERID) {
3307 3160 if (!(tsbmp->uhat_tteflags & tteflag)) {
3308 3161 tsbmp->uhat_tteflags |= tteflag;
3309 3162 }
3310 3163 } else {
3311 3164 if (!(tsbmp->uhat_rtteflags & tteflag)) {
3312 3165 tsbmp->uhat_rtteflags |= tteflag;
3313 3166 }
3314 3167 }
3315 3168 kpreempt_enable();
3316 3169 }
3317 3170 }
3318 3171
3319 3172 if (size >= TTE4M && (flags & HAT_LOAD_TEXT) &&
3320 3173 !SFMMU_FLAGS_ISSET(sfmmup, HAT_4MTEXT_FLAG)) {
3321 3174 hatlockp = sfmmu_hat_enter(sfmmup);
3322 3175 SFMMU_FLAGS_SET(sfmmup, HAT_4MTEXT_FLAG);
3323 3176 sfmmu_hat_exit(hatlockp);
3324 3177 }
3325 3178
3326 3179 flush_tte.tte_intlo = (tteold.tte_intlo ^ ttep->tte_intlo) &
3327 3180 hw_tte.tte_intlo;
3328 3181 flush_tte.tte_inthi = (tteold.tte_inthi ^ ttep->tte_inthi) &
3329 3182 hw_tte.tte_inthi;
3330 3183
3331 3184 if (remap && (flush_tte.tte_inthi || flush_tte.tte_intlo)) {
3332 3185 /*
3333 3186 * If remap and new tte differs from old tte we need
3334 3187 * to sync the mod bit and flush TLB/TSB. We don't
3335 3188 * need to sync ref bit because we currently always set
3336 3189 * ref bit in tteload.
3337 3190 */
3338 3191 ASSERT(TTE_IS_REF(ttep));
3339 3192 if (TTE_IS_MOD(&tteold)) {
3340 3193 sfmmu_ttesync(sfmmup, vaddr, &tteold, pp);
3341 3194 }
3342 3195 /*
3343 3196 * hwtte bits shouldn't change for SRD hmeblks as long as SRD
3344 3197 * hmes are only used for read only text. Adding this code for
3345 3198 * completeness and future use of shared hmeblks with writable
3346 3199 * mappings of VMODSORT vnodes.
3347 3200 */
3348 3201 if (hmeblkp->hblk_shared) {
3349 3202 cpuset_t cpuset = sfmmu_rgntlb_demap(vaddr,
3350 3203 sfmmup->sfmmu_srdp->srd_hmergnp[rid], hmeblkp, 1);
3351 3204 xt_sync(cpuset);
3352 3205 SFMMU_STAT_ADD(sf_region_remap_demap, 1);
3353 3206 } else {
3354 3207 sfmmu_tlb_demap(vaddr, sfmmup, hmeblkp, 0, 0);
3355 3208 xt_sync(sfmmup->sfmmu_cpusran);
3356 3209 }
3357 3210 }
3358 3211
3359 3212 if ((flags & SFMMU_NO_TSBLOAD) == 0) {
3360 3213 /*
3361 3214 * We only preload 8K and 4M mappings into the TSB, since
3362 3215 * 64K and 512K mappings are replicated and hence don't
3363 3216 * have a single, unique TSB entry. Ditto for 32M/256M.
3364 3217 */
3365 3218 if (size == TTE8K || size == TTE4M) {
3366 3219 sf_scd_t *scdp;
3367 3220 hatlockp = sfmmu_hat_enter(sfmmup);
3368 3221 /*
3369 3222 * Don't preload private TSB if the mapping is used
3370 3223 * by the shctx in the SCD.
3371 3224 */
3372 3225 scdp = sfmmup->sfmmu_scdp;
3373 3226 if (rid == SFMMU_INVALID_SHMERID || scdp == NULL ||
3374 3227 !SF_RGNMAP_TEST(scdp->scd_hmeregion_map, rid)) {
3375 3228 sfmmu_load_tsb(sfmmup, vaddr, &sfhme->hme_tte,
3376 3229 size);
3377 3230 }
3378 3231 sfmmu_hat_exit(hatlockp);
3379 3232 }
3380 3233 }
3381 3234 if (pp) {
3382 3235 if (!remap) {
3383 3236 HME_ADD(sfhme, pp);
3384 3237 atomic_add_16(&hmeblkp->hblk_hmecnt, 1);
3385 3238 ASSERT(hmeblkp->hblk_hmecnt > 0);
3386 3239
3387 3240 /*
3388 3241 * Cannot ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS)
3389 3242 * see pageunload() for comment.
3390 3243 */
3391 3244 }
3392 3245 sfmmu_mlist_exit(pml);
3393 3246 }
3394 3247
3395 3248 return (0);
3396 3249 }
3397 3250 /*
3398 3251 * Function unlocks hash bucket.
3399 3252 */
3400 3253 static void
3401 3254 sfmmu_tteload_release_hashbucket(struct hmehash_bucket *hmebp)
3402 3255 {
3403 3256 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
3404 3257 SFMMU_HASH_UNLOCK(hmebp);
3405 3258 }
3406 3259
3407 3260 /*
3408 3261 * function which checks and sets up page array for a large
3409 3262 * translation. Will set p_vcolor, p_index, p_ro fields.
3410 3263 * Assumes addr and pfnum of first page are properly aligned.
3411 3264 * Will check for physical contiguity. If check fails it return
3412 3265 * non null.
3413 3266 */
3414 3267 static int
3415 3268 sfmmu_pagearray_setup(caddr_t addr, page_t **pps, tte_t *ttep, int remap)
3416 3269 {
3417 3270 int i, index, ttesz;
3418 3271 pfn_t pfnum;
3419 3272 pgcnt_t npgs;
3420 3273 page_t *pp, *pp1;
3421 3274 kmutex_t *pmtx;
3422 3275 #ifdef VAC
3423 3276 int osz;
3424 3277 int cflags = 0;
3425 3278 int vac_err = 0;
3426 3279 #endif
3427 3280 int newidx = 0;
3428 3281
3429 3282 ttesz = TTE_CSZ(ttep);
3430 3283
3431 3284 ASSERT(ttesz > TTE8K);
3432 3285
3433 3286 npgs = TTEPAGES(ttesz);
3434 3287 index = PAGESZ_TO_INDEX(ttesz);
3435 3288
3436 3289 pfnum = (*pps)->p_pagenum;
3437 3290 ASSERT(IS_P2ALIGNED(pfnum, npgs));
3438 3291
3439 3292 /*
3440 3293 * Save the first pp so we can do HAT_TMPNC at the end.
3441 3294 */
3442 3295 pp1 = *pps;
3443 3296 #ifdef VAC
3444 3297 osz = fnd_mapping_sz(pp1);
3445 3298 #endif
3446 3299
3447 3300 for (i = 0; i < npgs; i++, pps++) {
3448 3301 pp = *pps;
3449 3302 ASSERT(PAGE_LOCKED(pp));
3450 3303 ASSERT(pp->p_szc >= ttesz);
3451 3304 ASSERT(pp->p_szc == pp1->p_szc);
3452 3305 ASSERT(sfmmu_mlist_held(pp));
3453 3306
3454 3307 /*
3455 3308 * XXX is it possible to maintain P_RO on the root only?
3456 3309 */
3457 3310 if (TTE_IS_WRITABLE(ttep) && PP_ISRO(pp)) {
3458 3311 pmtx = sfmmu_page_enter(pp);
3459 3312 PP_CLRRO(pp);
3460 3313 sfmmu_page_exit(pmtx);
3461 3314 } else if (!PP_ISMAPPED(pp) && !TTE_IS_WRITABLE(ttep) &&
3462 3315 !PP_ISMOD(pp)) {
3463 3316 pmtx = sfmmu_page_enter(pp);
3464 3317 if (!(PP_ISMOD(pp))) {
3465 3318 PP_SETRO(pp);
3466 3319 }
3467 3320 sfmmu_page_exit(pmtx);
3468 3321 }
3469 3322
3470 3323 /*
3471 3324 * If this is a remap we skip vac & contiguity checks.
3472 3325 */
3473 3326 if (remap)
3474 3327 continue;
3475 3328
3476 3329 /*
3477 3330 * set p_vcolor and detect any vac conflicts.
3478 3331 */
3479 3332 #ifdef VAC
3480 3333 if (vac_err == 0) {
3481 3334 vac_err = sfmmu_vacconflict_array(addr, pp, &cflags);
3482 3335
3483 3336 }
3484 3337 #endif
3485 3338
3486 3339 /*
3487 3340 * Save current index in case we need to undo it.
3488 3341 * Note: "PAGESZ_TO_INDEX(sz) (1 << (sz))"
3489 3342 * "SFMMU_INDEX_SHIFT 6"
3490 3343 * "SFMMU_INDEX_MASK ((1 << SFMMU_INDEX_SHIFT) - 1)"
3491 3344 * "PP_MAPINDEX(p_index) (p_index & SFMMU_INDEX_MASK)"
3492 3345 *
3493 3346 * So: index = PAGESZ_TO_INDEX(ttesz);
3494 3347 * if ttesz == 1 then index = 0x2
3495 3348 * 2 then index = 0x4
3496 3349 * 3 then index = 0x8
3497 3350 * 4 then index = 0x10
3498 3351 * 5 then index = 0x20
3499 3352 * The code below checks if it's a new pagesize (ie, newidx)
3500 3353 * in case we need to take it back out of p_index,
3501 3354 * and then or's the new index into the existing index.
3502 3355 */
3503 3356 if ((PP_MAPINDEX(pp) & index) == 0)
3504 3357 newidx = 1;
3505 3358 pp->p_index = (PP_MAPINDEX(pp) | index);
3506 3359
3507 3360 /*
3508 3361 * contiguity check
3509 3362 */
3510 3363 if (pp->p_pagenum != pfnum) {
3511 3364 /*
3512 3365 * If we fail the contiguity test then
3513 3366 * the only thing we need to fix is the p_index field.
3514 3367 * We might get a few extra flushes but since this
3515 3368 * path is rare that is ok. The p_ro field will
3516 3369 * get automatically fixed on the next tteload to
3517 3370 * the page. NO TNC bit is set yet.
3518 3371 */
3519 3372 while (i >= 0) {
3520 3373 pp = *pps;
3521 3374 if (newidx)
3522 3375 pp->p_index = (PP_MAPINDEX(pp) &
3523 3376 ~index);
3524 3377 pps--;
3525 3378 i--;
3526 3379 }
3527 3380 return (1);
3528 3381 }
3529 3382 pfnum++;
3530 3383 addr += MMU_PAGESIZE;
3531 3384 }
3532 3385
3533 3386 #ifdef VAC
3534 3387 if (vac_err) {
3535 3388 if (ttesz > osz) {
3536 3389 /*
3537 3390 * There are some smaller mappings that causes vac
3538 3391 * conflicts. Convert all existing small mappings to
3539 3392 * TNC.
3540 3393 */
3541 3394 SFMMU_STAT_ADD(sf_uncache_conflict, npgs);
3542 3395 sfmmu_page_cache_array(pp1, HAT_TMPNC, CACHE_FLUSH,
3543 3396 npgs);
3544 3397 } else {
3545 3398 /* EMPTY */
3546 3399 /*
3547 3400 * If there exists an big page mapping,
3548 3401 * that means the whole existing big page
3549 3402 * has TNC setting already. No need to covert to
3550 3403 * TNC again.
3551 3404 */
3552 3405 ASSERT(PP_ISTNC(pp1));
3553 3406 }
3554 3407 }
3555 3408 #endif /* VAC */
3556 3409
3557 3410 return (0);
3558 3411 }
3559 3412
3560 3413 #ifdef VAC
3561 3414 /*
3562 3415 * Routine that detects vac consistency for a large page. It also
3563 3416 * sets virtual color for all pp's for this big mapping.
3564 3417 */
3565 3418 static int
3566 3419 sfmmu_vacconflict_array(caddr_t addr, page_t *pp, int *cflags)
3567 3420 {
3568 3421 int vcolor, ocolor;
3569 3422
3570 3423 ASSERT(sfmmu_mlist_held(pp));
3571 3424
3572 3425 if (PP_ISNC(pp)) {
3573 3426 return (HAT_TMPNC);
3574 3427 }
3575 3428
3576 3429 vcolor = addr_to_vcolor(addr);
3577 3430 if (PP_NEWPAGE(pp)) {
3578 3431 PP_SET_VCOLOR(pp, vcolor);
3579 3432 return (0);
3580 3433 }
3581 3434
3582 3435 ocolor = PP_GET_VCOLOR(pp);
3583 3436 if (ocolor == vcolor) {
3584 3437 return (0);
3585 3438 }
3586 3439
3587 3440 if (!PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp)) {
3588 3441 /*
3589 3442 * Previous user of page had a differnet color
3590 3443 * but since there are no current users
3591 3444 * we just flush the cache and change the color.
3592 3445 * As an optimization for large pages we flush the
3593 3446 * entire cache of that color and set a flag.
3594 3447 */
3595 3448 SFMMU_STAT(sf_pgcolor_conflict);
3596 3449 if (!CacheColor_IsFlushed(*cflags, ocolor)) {
3597 3450 CacheColor_SetFlushed(*cflags, ocolor);
3598 3451 sfmmu_cache_flushcolor(ocolor, pp->p_pagenum);
3599 3452 }
3600 3453 PP_SET_VCOLOR(pp, vcolor);
3601 3454 return (0);
3602 3455 }
3603 3456
3604 3457 /*
3605 3458 * We got a real conflict with a current mapping.
3606 3459 * set flags to start unencaching all mappings
3607 3460 * and return failure so we restart looping
3608 3461 * the pp array from the beginning.
3609 3462 */
3610 3463 return (HAT_TMPNC);
3611 3464 }
3612 3465 #endif /* VAC */
3613 3466
3614 3467 /*
3615 3468 * creates a large page shadow hmeblk for a tte.
3616 3469 * The purpose of this routine is to allow us to do quick unloads because
3617 3470 * the vm layer can easily pass a very large but sparsely populated range.
3618 3471 */
3619 3472 static struct hme_blk *
3620 3473 sfmmu_shadow_hcreate(sfmmu_t *sfmmup, caddr_t vaddr, int ttesz, uint_t flags)
3621 3474 {
3622 3475 struct hmehash_bucket *hmebp;
3623 3476 hmeblk_tag hblktag;
3624 3477 int hmeshift, size, vshift;
3625 3478 uint_t shw_mask, newshw_mask;
3626 3479 struct hme_blk *hmeblkp;
3627 3480
3628 3481 ASSERT(sfmmup != KHATID);
3629 3482 if (mmu_page_sizes == max_mmu_page_sizes) {
3630 3483 ASSERT(ttesz < TTE256M);
3631 3484 } else {
3632 3485 ASSERT(ttesz < TTE4M);
3633 3486 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0);
3634 3487 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0);
3635 3488 }
3636 3489
3637 3490 if (ttesz == TTE8K) {
3638 3491 size = TTE512K;
3639 3492 } else {
3640 3493 size = ++ttesz;
3641 3494 }
3642 3495
3643 3496 hblktag.htag_id = sfmmup;
3644 3497 hmeshift = HME_HASH_SHIFT(size);
3645 3498 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift);
3646 3499 hblktag.htag_rehash = HME_HASH_REHASH(size);
3647 3500 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
3648 3501 hmebp = HME_HASH_FUNCTION(sfmmup, vaddr, hmeshift);
3649 3502
3650 3503 SFMMU_HASH_LOCK(hmebp);
3651 3504
3652 3505 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp);
3653 3506 ASSERT(hmeblkp != (struct hme_blk *)hblk_reserve);
3654 3507 if (hmeblkp == NULL) {
3655 3508 hmeblkp = sfmmu_hblk_alloc(sfmmup, vaddr, hmebp, size,
3656 3509 hblktag, flags, SFMMU_INVALID_SHMERID);
3657 3510 }
3658 3511 ASSERT(hmeblkp);
3659 3512 if (!hmeblkp->hblk_shw_mask) {
3660 3513 /*
3661 3514 * if this is a unused hblk it was just allocated or could
3662 3515 * potentially be a previous large page hblk so we need to
3663 3516 * set the shadow bit.
3664 3517 */
3665 3518 ASSERT(!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt);
3666 3519 hmeblkp->hblk_shw_bit = 1;
3667 3520 } else if (hmeblkp->hblk_shw_bit == 0) {
3668 3521 panic("sfmmu_shadow_hcreate: shw bit not set in hmeblkp 0x%p",
3669 3522 (void *)hmeblkp);
3670 3523 }
3671 3524 ASSERT(hmeblkp->hblk_shw_bit == 1);
3672 3525 ASSERT(!hmeblkp->hblk_shared);
3673 3526 vshift = vaddr_to_vshift(hblktag, vaddr, size);
3674 3527 ASSERT(vshift < 8);
3675 3528 /*
3676 3529 * Atomically set shw mask bit
3677 3530 */
3678 3531 do {
3679 3532 shw_mask = hmeblkp->hblk_shw_mask;
3680 3533 newshw_mask = shw_mask | (1 << vshift);
3681 3534 newshw_mask = cas32(&hmeblkp->hblk_shw_mask, shw_mask,
3682 3535 newshw_mask);
3683 3536 } while (newshw_mask != shw_mask);
3684 3537
3685 3538 SFMMU_HASH_UNLOCK(hmebp);
3686 3539
3687 3540 return (hmeblkp);
3688 3541 }
3689 3542
3690 3543 /*
3691 3544 * This routine cleanup a previous shadow hmeblk and changes it to
3692 3545 * a regular hblk. This happens rarely but it is possible
3693 3546 * when a process wants to use large pages and there are hblks still
3694 3547 * lying around from the previous as that used these hmeblks.
3695 3548 * The alternative was to cleanup the shadow hblks at unload time
3696 3549 * but since so few user processes actually use large pages, it is
3697 3550 * better to be lazy and cleanup at this time.
3698 3551 */
3699 3552 static void
3700 3553 sfmmu_shadow_hcleanup(sfmmu_t *sfmmup, struct hme_blk *hmeblkp,
3701 3554 struct hmehash_bucket *hmebp)
3702 3555 {
3703 3556 caddr_t addr, endaddr;
3704 3557 int hashno, size;
3705 3558
3706 3559 ASSERT(hmeblkp->hblk_shw_bit);
3707 3560 ASSERT(!hmeblkp->hblk_shared);
3708 3561
3709 3562 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
3710 3563
3711 3564 if (!hmeblkp->hblk_shw_mask) {
3712 3565 hmeblkp->hblk_shw_bit = 0;
3713 3566 return;
3714 3567 }
3715 3568 addr = (caddr_t)get_hblk_base(hmeblkp);
3716 3569 endaddr = get_hblk_endaddr(hmeblkp);
3717 3570 size = get_hblk_ttesz(hmeblkp);
3718 3571 hashno = size - 1;
3719 3572 ASSERT(hashno > 0);
3720 3573 SFMMU_HASH_UNLOCK(hmebp);
3721 3574
3722 3575 sfmmu_free_hblks(sfmmup, addr, endaddr, hashno);
3723 3576
3724 3577 SFMMU_HASH_LOCK(hmebp);
3725 3578 }
3726 3579
3727 3580 static void
3728 3581 sfmmu_free_hblks(sfmmu_t *sfmmup, caddr_t addr, caddr_t endaddr,
3729 3582 int hashno)
3730 3583 {
3731 3584 int hmeshift, shadow = 0;
3732 3585 hmeblk_tag hblktag;
3733 3586 struct hmehash_bucket *hmebp;
3734 3587 struct hme_blk *hmeblkp;
3735 3588 struct hme_blk *nx_hblk, *pr_hblk, *list = NULL;
3736 3589
3737 3590 ASSERT(hashno > 0);
3738 3591 hblktag.htag_id = sfmmup;
3739 3592 hblktag.htag_rehash = hashno;
3740 3593 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
3741 3594
3742 3595 hmeshift = HME_HASH_SHIFT(hashno);
3743 3596
3744 3597 while (addr < endaddr) {
3745 3598 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
3746 3599 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
3747 3600 SFMMU_HASH_LOCK(hmebp);
3748 3601 /* inline HME_HASH_SEARCH */
3749 3602 hmeblkp = hmebp->hmeblkp;
3750 3603 pr_hblk = NULL;
3751 3604 while (hmeblkp) {
3752 3605 if (HTAGS_EQ(hmeblkp->hblk_tag, hblktag)) {
3753 3606 /* found hme_blk */
3754 3607 ASSERT(!hmeblkp->hblk_shared);
3755 3608 if (hmeblkp->hblk_shw_bit) {
3756 3609 if (hmeblkp->hblk_shw_mask) {
3757 3610 shadow = 1;
3758 3611 sfmmu_shadow_hcleanup(sfmmup,
3759 3612 hmeblkp, hmebp);
3760 3613 break;
3761 3614 } else {
3762 3615 hmeblkp->hblk_shw_bit = 0;
3763 3616 }
3764 3617 }
3765 3618
3766 3619 /*
3767 3620 * Hblk_hmecnt and hblk_vcnt could be non zero
3768 3621 * since hblk_unload() does not gurantee that.
3769 3622 *
3770 3623 * XXX - this could cause tteload() to spin
3771 3624 * where sfmmu_shadow_hcleanup() is called.
3772 3625 */
3773 3626 }
3774 3627
3775 3628 nx_hblk = hmeblkp->hblk_next;
3776 3629 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) {
3777 3630 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
3778 3631 &list, 0);
3779 3632 } else {
3780 3633 pr_hblk = hmeblkp;
3781 3634 }
3782 3635 hmeblkp = nx_hblk;
3783 3636 }
3784 3637
3785 3638 SFMMU_HASH_UNLOCK(hmebp);
3786 3639
3787 3640 if (shadow) {
3788 3641 /*
3789 3642 * We found another shadow hblk so cleaned its
3790 3643 * children. We need to go back and cleanup
3791 3644 * the original hblk so we don't change the
3792 3645 * addr.
3793 3646 */
3794 3647 shadow = 0;
3795 3648 } else {
3796 3649 addr = (caddr_t)roundup((uintptr_t)addr + 1,
3797 3650 (1 << hmeshift));
3798 3651 }
3799 3652 }
3800 3653 sfmmu_hblks_list_purge(&list, 0);
3801 3654 }
3802 3655
3803 3656 /*
3804 3657 * This routine's job is to delete stale invalid shared hmeregions hmeblks that
3805 3658 * may still linger on after pageunload.
3806 3659 */
3807 3660 static void
3808 3661 sfmmu_cleanup_rhblk(sf_srd_t *srdp, caddr_t addr, uint_t rid, int ttesz)
3809 3662 {
3810 3663 int hmeshift;
3811 3664 hmeblk_tag hblktag;
3812 3665 struct hmehash_bucket *hmebp;
3813 3666 struct hme_blk *hmeblkp;
3814 3667 struct hme_blk *pr_hblk;
3815 3668 struct hme_blk *list = NULL;
3816 3669
3817 3670 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
3818 3671 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
3819 3672
3820 3673 hmeshift = HME_HASH_SHIFT(ttesz);
3821 3674 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
3822 3675 hblktag.htag_rehash = ttesz;
3823 3676 hblktag.htag_rid = rid;
3824 3677 hblktag.htag_id = srdp;
3825 3678 hmebp = HME_HASH_FUNCTION(srdp, addr, hmeshift);
3826 3679
3827 3680 SFMMU_HASH_LOCK(hmebp);
3828 3681 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, pr_hblk, &list);
3829 3682 if (hmeblkp != NULL) {
3830 3683 ASSERT(hmeblkp->hblk_shared);
3831 3684 ASSERT(!hmeblkp->hblk_shw_bit);
3832 3685 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) {
3833 3686 panic("sfmmu_cleanup_rhblk: valid hmeblk");
3834 3687 }
3835 3688 ASSERT(!hmeblkp->hblk_lckcnt);
3836 3689 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
3837 3690 &list, 0);
3838 3691 }
3839 3692 SFMMU_HASH_UNLOCK(hmebp);
3840 3693 sfmmu_hblks_list_purge(&list, 0);
3841 3694 }
3842 3695
3843 3696 /* ARGSUSED */
3844 3697 static void
3845 3698 sfmmu_rgn_cb_noop(caddr_t saddr, caddr_t eaddr, caddr_t r_saddr,
3846 3699 size_t r_size, void *r_obj, u_offset_t r_objoff)
3847 3700 {
3848 3701 }
3849 3702
3850 3703 /*
3851 3704 * Searches for an hmeblk which maps addr, then unloads this mapping
3852 3705 * and updates *eaddrp, if the hmeblk is found.
3853 3706 */
3854 3707 static void
3855 3708 sfmmu_unload_hmeregion_va(sf_srd_t *srdp, uint_t rid, caddr_t addr,
3856 3709 caddr_t eaddr, int ttesz, caddr_t *eaddrp)
3857 3710 {
3858 3711 int hmeshift;
3859 3712 hmeblk_tag hblktag;
3860 3713 struct hmehash_bucket *hmebp;
3861 3714 struct hme_blk *hmeblkp;
3862 3715 struct hme_blk *pr_hblk;
3863 3716 struct hme_blk *list = NULL;
3864 3717
3865 3718 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
3866 3719 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
3867 3720 ASSERT(ttesz >= HBLK_MIN_TTESZ);
3868 3721
3869 3722 hmeshift = HME_HASH_SHIFT(ttesz);
3870 3723 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
3871 3724 hblktag.htag_rehash = ttesz;
3872 3725 hblktag.htag_rid = rid;
3873 3726 hblktag.htag_id = srdp;
3874 3727 hmebp = HME_HASH_FUNCTION(srdp, addr, hmeshift);
3875 3728
3876 3729 SFMMU_HASH_LOCK(hmebp);
3877 3730 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, pr_hblk, &list);
3878 3731 if (hmeblkp != NULL) {
3879 3732 ASSERT(hmeblkp->hblk_shared);
3880 3733 ASSERT(!hmeblkp->hblk_lckcnt);
3881 3734 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) {
3882 3735 *eaddrp = sfmmu_hblk_unload(NULL, hmeblkp, addr,
3883 3736 eaddr, NULL, HAT_UNLOAD);
3884 3737 ASSERT(*eaddrp > addr);
3885 3738 }
3886 3739 ASSERT(!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt);
3887 3740 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
3888 3741 &list, 0);
3889 3742 }
3890 3743 SFMMU_HASH_UNLOCK(hmebp);
3891 3744 sfmmu_hblks_list_purge(&list, 0);
3892 3745 }
3893 3746
3894 3747 static void
3895 3748 sfmmu_unload_hmeregion(sf_srd_t *srdp, sf_region_t *rgnp)
3896 3749 {
3897 3750 int ttesz = rgnp->rgn_pgszc;
3898 3751 size_t rsz = rgnp->rgn_size;
3899 3752 caddr_t rsaddr = rgnp->rgn_saddr;
3900 3753 caddr_t readdr = rsaddr + rsz;
3901 3754 caddr_t rhsaddr;
3902 3755 caddr_t va;
3903 3756 uint_t rid = rgnp->rgn_id;
3904 3757 caddr_t cbsaddr;
3905 3758 caddr_t cbeaddr;
3906 3759 hat_rgn_cb_func_t rcbfunc;
3907 3760 ulong_t cnt;
3908 3761
3909 3762 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
3910 3763 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
3911 3764
3912 3765 ASSERT(IS_P2ALIGNED(rsaddr, TTEBYTES(ttesz)));
3913 3766 ASSERT(IS_P2ALIGNED(rsz, TTEBYTES(ttesz)));
3914 3767 if (ttesz < HBLK_MIN_TTESZ) {
3915 3768 ttesz = HBLK_MIN_TTESZ;
3916 3769 rhsaddr = (caddr_t)P2ALIGN((uintptr_t)rsaddr, HBLK_MIN_BYTES);
3917 3770 } else {
3918 3771 rhsaddr = rsaddr;
3919 3772 }
3920 3773
3921 3774 if ((rcbfunc = rgnp->rgn_cb_function) == NULL) {
3922 3775 rcbfunc = sfmmu_rgn_cb_noop;
3923 3776 }
3924 3777
3925 3778 while (ttesz >= HBLK_MIN_TTESZ) {
3926 3779 cbsaddr = rsaddr;
3927 3780 cbeaddr = rsaddr;
3928 3781 if (!(rgnp->rgn_hmeflags & (1 << ttesz))) {
3929 3782 ttesz--;
3930 3783 continue;
3931 3784 }
3932 3785 cnt = 0;
3933 3786 va = rsaddr;
3934 3787 while (va < readdr) {
3935 3788 ASSERT(va >= rhsaddr);
3936 3789 if (va != cbeaddr) {
3937 3790 if (cbeaddr != cbsaddr) {
3938 3791 ASSERT(cbeaddr > cbsaddr);
3939 3792 (*rcbfunc)(cbsaddr, cbeaddr,
3940 3793 rsaddr, rsz, rgnp->rgn_obj,
3941 3794 rgnp->rgn_objoff);
3942 3795 }
3943 3796 cbsaddr = va;
3944 3797 cbeaddr = va;
3945 3798 }
3946 3799 sfmmu_unload_hmeregion_va(srdp, rid, va, readdr,
3947 3800 ttesz, &cbeaddr);
3948 3801 cnt++;
3949 3802 va = rhsaddr + (cnt << TTE_PAGE_SHIFT(ttesz));
3950 3803 }
3951 3804 if (cbeaddr != cbsaddr) {
3952 3805 ASSERT(cbeaddr > cbsaddr);
3953 3806 (*rcbfunc)(cbsaddr, cbeaddr, rsaddr,
3954 3807 rsz, rgnp->rgn_obj,
3955 3808 rgnp->rgn_objoff);
3956 3809 }
3957 3810 ttesz--;
3958 3811 }
3959 3812 }
3960 3813
3961 3814 /*
3962 3815 * Release one hardware address translation lock on the given address range.
3963 3816 */
3964 3817 void
3965 3818 hat_unlock(struct hat *sfmmup, caddr_t addr, size_t len)
3966 3819 {
3967 3820 struct hmehash_bucket *hmebp;
3968 3821 hmeblk_tag hblktag;
3969 3822 int hmeshift, hashno = 1;
3970 3823 struct hme_blk *hmeblkp, *list = NULL;
3971 3824 caddr_t endaddr;
3972 3825
3973 3826 ASSERT(sfmmup != NULL);
3974 3827 ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
3975 3828
3976 3829 ASSERT((sfmmup == ksfmmup) ||
3977 3830 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
3978 3831 ASSERT((len & MMU_PAGEOFFSET) == 0);
3979 3832 endaddr = addr + len;
3980 3833 hblktag.htag_id = sfmmup;
3981 3834 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
3982 3835
3983 3836 /*
3984 3837 * Spitfire supports 4 page sizes.
3985 3838 * Most pages are expected to be of the smallest page size (8K) and
3986 3839 * these will not need to be rehashed. 64K pages also don't need to be
3987 3840 * rehashed because an hmeblk spans 64K of address space. 512K pages
3988 3841 * might need 1 rehash and and 4M pages might need 2 rehashes.
3989 3842 */
3990 3843 while (addr < endaddr) {
3991 3844 hmeshift = HME_HASH_SHIFT(hashno);
3992 3845 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
3993 3846 hblktag.htag_rehash = hashno;
3994 3847 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
3995 3848
3996 3849 SFMMU_HASH_LOCK(hmebp);
3997 3850
3998 3851 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list);
3999 3852 if (hmeblkp != NULL) {
4000 3853 ASSERT(!hmeblkp->hblk_shared);
4001 3854 /*
4002 3855 * If we encounter a shadow hmeblk then
4003 3856 * we know there are no valid hmeblks mapping
4004 3857 * this address at this size or larger.
4005 3858 * Just increment address by the smallest
4006 3859 * page size.
4007 3860 */
4008 3861 if (hmeblkp->hblk_shw_bit) {
4009 3862 addr += MMU_PAGESIZE;
4010 3863 } else {
4011 3864 addr = sfmmu_hblk_unlock(hmeblkp, addr,
4012 3865 endaddr);
4013 3866 }
4014 3867 SFMMU_HASH_UNLOCK(hmebp);
4015 3868 hashno = 1;
4016 3869 continue;
4017 3870 }
4018 3871 SFMMU_HASH_UNLOCK(hmebp);
4019 3872
4020 3873 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) {
4021 3874 /*
4022 3875 * We have traversed the whole list and rehashed
4023 3876 * if necessary without finding the address to unlock
4024 3877 * which should never happen.
4025 3878 */
4026 3879 panic("sfmmu_unlock: addr not found. "
4027 3880 "addr %p hat %p", (void *)addr, (void *)sfmmup);
4028 3881 } else {
4029 3882 hashno++;
4030 3883 }
4031 3884 }
4032 3885
4033 3886 sfmmu_hblks_list_purge(&list, 0);
4034 3887 }
4035 3888
4036 3889 void
4037 3890 hat_unlock_region(struct hat *sfmmup, caddr_t addr, size_t len,
4038 3891 hat_region_cookie_t rcookie)
4039 3892 {
4040 3893 sf_srd_t *srdp;
4041 3894 sf_region_t *rgnp;
4042 3895 int ttesz;
4043 3896 uint_t rid;
4044 3897 caddr_t eaddr;
4045 3898 caddr_t va;
4046 3899 int hmeshift;
4047 3900 hmeblk_tag hblktag;
4048 3901 struct hmehash_bucket *hmebp;
4049 3902 struct hme_blk *hmeblkp;
4050 3903 struct hme_blk *pr_hblk;
4051 3904 struct hme_blk *list;
4052 3905
4053 3906 if (rcookie == HAT_INVALID_REGION_COOKIE) {
4054 3907 hat_unlock(sfmmup, addr, len);
4055 3908 return;
4056 3909 }
4057 3910
4058 3911 ASSERT(sfmmup != NULL);
4059 3912 ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
4060 3913 ASSERT(sfmmup != ksfmmup);
4061 3914
4062 3915 srdp = sfmmup->sfmmu_srdp;
4063 3916 rid = (uint_t)((uint64_t)rcookie);
4064 3917 VERIFY3U(rid, <, SFMMU_MAX_HME_REGIONS);
4065 3918 eaddr = addr + len;
4066 3919 va = addr;
4067 3920 list = NULL;
4068 3921 rgnp = srdp->srd_hmergnp[rid];
4069 3922 SFMMU_VALIDATE_HMERID(sfmmup, rid, addr, len);
4070 3923
4071 3924 ASSERT(IS_P2ALIGNED(addr, TTEBYTES(rgnp->rgn_pgszc)));
4072 3925 ASSERT(IS_P2ALIGNED(len, TTEBYTES(rgnp->rgn_pgszc)));
4073 3926 if (rgnp->rgn_pgszc < HBLK_MIN_TTESZ) {
4074 3927 ttesz = HBLK_MIN_TTESZ;
4075 3928 } else {
4076 3929 ttesz = rgnp->rgn_pgszc;
4077 3930 }
4078 3931 while (va < eaddr) {
4079 3932 while (ttesz < rgnp->rgn_pgszc &&
4080 3933 IS_P2ALIGNED(va, TTEBYTES(ttesz + 1))) {
4081 3934 ttesz++;
4082 3935 }
4083 3936 while (ttesz >= HBLK_MIN_TTESZ) {
4084 3937 if (!(rgnp->rgn_hmeflags & (1 << ttesz))) {
4085 3938 ttesz--;
4086 3939 continue;
4087 3940 }
4088 3941 hmeshift = HME_HASH_SHIFT(ttesz);
4089 3942 hblktag.htag_bspage = HME_HASH_BSPAGE(va, hmeshift);
4090 3943 hblktag.htag_rehash = ttesz;
4091 3944 hblktag.htag_rid = rid;
4092 3945 hblktag.htag_id = srdp;
4093 3946 hmebp = HME_HASH_FUNCTION(srdp, va, hmeshift);
4094 3947 SFMMU_HASH_LOCK(hmebp);
4095 3948 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, pr_hblk,
4096 3949 &list);
4097 3950 if (hmeblkp == NULL) {
4098 3951 SFMMU_HASH_UNLOCK(hmebp);
4099 3952 ttesz--;
4100 3953 continue;
4101 3954 }
4102 3955 ASSERT(hmeblkp->hblk_shared);
4103 3956 va = sfmmu_hblk_unlock(hmeblkp, va, eaddr);
4104 3957 ASSERT(va >= eaddr ||
4105 3958 IS_P2ALIGNED((uintptr_t)va, TTEBYTES(ttesz)));
4106 3959 SFMMU_HASH_UNLOCK(hmebp);
4107 3960 break;
4108 3961 }
4109 3962 if (ttesz < HBLK_MIN_TTESZ) {
4110 3963 panic("hat_unlock_region: addr not found "
4111 3964 "addr %p hat %p", (void *)va, (void *)sfmmup);
4112 3965 }
4113 3966 }
4114 3967 sfmmu_hblks_list_purge(&list, 0);
4115 3968 }
4116 3969
4117 3970 /*
4118 3971 * Function to unlock a range of addresses in an hmeblk. It returns the
4119 3972 * next address that needs to be unlocked.
4120 3973 * Should be called with the hash lock held.
4121 3974 */
4122 3975 static caddr_t
4123 3976 sfmmu_hblk_unlock(struct hme_blk *hmeblkp, caddr_t addr, caddr_t endaddr)
4124 3977 {
4125 3978 struct sf_hment *sfhme;
4126 3979 tte_t tteold, ttemod;
4127 3980 int ttesz, ret;
4128 3981
4129 3982 ASSERT(in_hblk_range(hmeblkp, addr));
4130 3983 ASSERT(hmeblkp->hblk_shw_bit == 0);
4131 3984
4132 3985 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp));
4133 3986 ttesz = get_hblk_ttesz(hmeblkp);
4134 3987
4135 3988 HBLKTOHME(sfhme, hmeblkp, addr);
4136 3989 while (addr < endaddr) {
4137 3990 readtte:
4138 3991 sfmmu_copytte(&sfhme->hme_tte, &tteold);
4139 3992 if (TTE_IS_VALID(&tteold)) {
4140 3993
4141 3994 ttemod = tteold;
4142 3995
4143 3996 ret = sfmmu_modifytte_try(&tteold, &ttemod,
4144 3997 &sfhme->hme_tte);
4145 3998
4146 3999 if (ret < 0)
4147 4000 goto readtte;
4148 4001
4149 4002 if (hmeblkp->hblk_lckcnt == 0)
4150 4003 panic("zero hblk lckcnt");
4151 4004
4152 4005 if (((uintptr_t)addr + TTEBYTES(ttesz)) >
4153 4006 (uintptr_t)endaddr)
4154 4007 panic("can't unlock large tte");
4155 4008
4156 4009 ASSERT(hmeblkp->hblk_lckcnt > 0);
4157 4010 atomic_add_32(&hmeblkp->hblk_lckcnt, -1);
4158 4011 HBLK_STACK_TRACE(hmeblkp, HBLK_UNLOCK);
4159 4012 } else {
4160 4013 panic("sfmmu_hblk_unlock: invalid tte");
4161 4014 }
4162 4015 addr += TTEBYTES(ttesz);
4163 4016 sfhme++;
4164 4017 }
4165 4018 return (addr);
4166 4019 }
4167 4020
4168 4021 /*
4169 4022 * Physical Address Mapping Framework
4170 4023 *
4171 4024 * General rules:
4172 4025 *
4173 4026 * (1) Applies only to seg_kmem memory pages. To make things easier,
4174 4027 * seg_kpm addresses are also accepted by the routines, but nothing
4175 4028 * is done with them since by definition their PA mappings are static.
4176 4029 * (2) hat_add_callback() may only be called while holding the page lock
4177 4030 * SE_SHARED or SE_EXCL of the underlying page (e.g., as_pagelock()),
4178 4031 * or passing HAC_PAGELOCK flag.
4179 4032 * (3) prehandler() and posthandler() may not call hat_add_callback() or
4180 4033 * hat_delete_callback(), nor should they allocate memory. Post quiesce
4181 4034 * callbacks may not sleep or acquire adaptive mutex locks.
4182 4035 * (4) Either prehandler() or posthandler() (but not both) may be specified
4183 4036 * as being NULL. Specifying an errhandler() is optional.
4184 4037 *
4185 4038 * Details of using the framework:
4186 4039 *
4187 4040 * registering a callback (hat_register_callback())
4188 4041 *
4189 4042 * Pass prehandler, posthandler, errhandler addresses
4190 4043 * as described below. If capture_cpus argument is nonzero,
4191 4044 * suspend callback to the prehandler will occur with CPUs
4192 4045 * captured and executing xc_loop() and CPUs will remain
4193 4046 * captured until after the posthandler suspend callback
4194 4047 * occurs.
4195 4048 *
4196 4049 * adding a callback (hat_add_callback())
4197 4050 *
4198 4051 * as_pagelock();
4199 4052 * hat_add_callback();
4200 4053 * save returned pfn in private data structures or program registers;
4201 4054 * as_pageunlock();
4202 4055 *
4203 4056 * prehandler()
4204 4057 *
4205 4058 * Stop all accesses by physical address to this memory page.
4206 4059 * Called twice: the first, PRESUSPEND, is a context safe to acquire
4207 4060 * adaptive locks. The second, SUSPEND, is called at high PIL with
4208 4061 * CPUs captured so adaptive locks may NOT be acquired (and all spin
4209 4062 * locks must be XCALL_PIL or higher locks).
4210 4063 *
4211 4064 * May return the following errors:
4212 4065 * EIO: A fatal error has occurred. This will result in panic.
4213 4066 * EAGAIN: The page cannot be suspended. This will fail the
4214 4067 * relocation.
4215 4068 * 0: Success.
4216 4069 *
4217 4070 * posthandler()
4218 4071 *
4219 4072 * Save new pfn in private data structures or program registers;
4220 4073 * not allowed to fail (non-zero return values will result in panic).
4221 4074 *
4222 4075 * errhandler()
4223 4076 *
4224 4077 * called when an error occurs related to the callback. Currently
4225 4078 * the only such error is HAT_CB_ERR_LEAKED which indicates that
4226 4079 * a page is being freed, but there are still outstanding callback(s)
4227 4080 * registered on the page.
4228 4081 *
4229 4082 * removing a callback (hat_delete_callback(); e.g., prior to freeing memory)
4230 4083 *
4231 4084 * stop using physical address
4232 4085 * hat_delete_callback();
4233 4086 *
4234 4087 */
4235 4088
4236 4089 /*
4237 4090 * Register a callback class. Each subsystem should do this once and
4238 4091 * cache the id_t returned for use in setting up and tearing down callbacks.
4239 4092 *
4240 4093 * There is no facility for removing callback IDs once they are created;
4241 4094 * the "key" should be unique for each module, so in case a module is unloaded
4242 4095 * and subsequently re-loaded, we can recycle the module's previous entry.
4243 4096 */
4244 4097 id_t
4245 4098 hat_register_callback(int key,
4246 4099 int (*prehandler)(caddr_t, uint_t, uint_t, void *),
4247 4100 int (*posthandler)(caddr_t, uint_t, uint_t, void *, pfn_t),
4248 4101 int (*errhandler)(caddr_t, uint_t, uint_t, void *),
4249 4102 int capture_cpus)
4250 4103 {
4251 4104 id_t id;
4252 4105
4253 4106 /*
4254 4107 * Search the table for a pre-existing callback associated with
4255 4108 * the identifier "key". If one exists, we re-use that entry in
4256 4109 * the table for this instance, otherwise we assign the next
4257 4110 * available table slot.
4258 4111 */
4259 4112 for (id = 0; id < sfmmu_max_cb_id; id++) {
4260 4113 if (sfmmu_cb_table[id].key == key)
4261 4114 break;
4262 4115 }
4263 4116
4264 4117 if (id == sfmmu_max_cb_id) {
4265 4118 id = sfmmu_cb_nextid++;
4266 4119 if (id >= sfmmu_max_cb_id)
4267 4120 panic("hat_register_callback: out of callback IDs");
4268 4121 }
4269 4122
4270 4123 ASSERT(prehandler != NULL || posthandler != NULL);
4271 4124
4272 4125 sfmmu_cb_table[id].key = key;
4273 4126 sfmmu_cb_table[id].prehandler = prehandler;
4274 4127 sfmmu_cb_table[id].posthandler = posthandler;
4275 4128 sfmmu_cb_table[id].errhandler = errhandler;
4276 4129 sfmmu_cb_table[id].capture_cpus = capture_cpus;
4277 4130
4278 4131 return (id);
4279 4132 }
4280 4133
4281 4134 #define HAC_COOKIE_NONE (void *)-1
4282 4135
4283 4136 /*
4284 4137 * Add relocation callbacks to the specified addr/len which will be called
4285 4138 * when relocating the associated page. See the description of pre and
4286 4139 * posthandler above for more details.
4287 4140 *
4288 4141 * If HAC_PAGELOCK is included in flags, the underlying memory page is
4289 4142 * locked internally so the caller must be able to deal with the callback
4290 4143 * running even before this function has returned. If HAC_PAGELOCK is not
4291 4144 * set, it is assumed that the underlying memory pages are locked.
4292 4145 *
4293 4146 * Since the caller must track the individual page boundaries anyway,
4294 4147 * we only allow a callback to be added to a single page (large
4295 4148 * or small). Thus [addr, addr + len) MUST be contained within a single
4296 4149 * page.
4297 4150 *
4298 4151 * Registering multiple callbacks on the same [addr, addr+len) is supported,
4299 4152 * _provided_that_ a unique parameter is specified for each callback.
4300 4153 * If multiple callbacks are registered on the same range the callback will
4301 4154 * be invoked with each unique parameter. Registering the same callback with
4302 4155 * the same argument more than once will result in corrupted kernel state.
4303 4156 *
4304 4157 * Returns the pfn of the underlying kernel page in *rpfn
4305 4158 * on success, or PFN_INVALID on failure.
4306 4159 *
4307 4160 * cookiep (if passed) provides storage space for an opaque cookie
4308 4161 * to return later to hat_delete_callback(). This cookie makes the callback
4309 4162 * deletion significantly quicker by avoiding a potentially lengthy hash
4310 4163 * search.
4311 4164 *
4312 4165 * Returns values:
4313 4166 * 0: success
4314 4167 * ENOMEM: memory allocation failure (e.g. flags was passed as HAC_NOSLEEP)
4315 4168 * EINVAL: callback ID is not valid
4316 4169 * ENXIO: ["vaddr", "vaddr" + len) is not mapped in the kernel's address
4317 4170 * space
4318 4171 * ERANGE: ["vaddr", "vaddr" + len) crosses a page boundary
4319 4172 */
4320 4173 int
4321 4174 hat_add_callback(id_t callback_id, caddr_t vaddr, uint_t len, uint_t flags,
4322 4175 void *pvt, pfn_t *rpfn, void **cookiep)
4323 4176 {
4324 4177 struct hmehash_bucket *hmebp;
4325 4178 hmeblk_tag hblktag;
4326 4179 struct hme_blk *hmeblkp;
4327 4180 int hmeshift, hashno;
4328 4181 caddr_t saddr, eaddr, baseaddr;
4329 4182 struct pa_hment *pahmep;
4330 4183 struct sf_hment *sfhmep, *osfhmep;
4331 4184 kmutex_t *pml;
4332 4185 tte_t tte;
4333 4186 page_t *pp;
4334 4187 vnode_t *vp;
4335 4188 u_offset_t off;
4336 4189 pfn_t pfn;
4337 4190 int kmflags = (flags & HAC_SLEEP)? KM_SLEEP : KM_NOSLEEP;
4338 4191 int locked = 0;
4339 4192
4340 4193 /*
4341 4194 * For KPM mappings, just return the physical address since we
4342 4195 * don't need to register any callbacks.
4343 4196 */
4344 4197 if (IS_KPM_ADDR(vaddr)) {
4345 4198 uint64_t paddr;
4346 4199 SFMMU_KPM_VTOP(vaddr, paddr);
4347 4200 *rpfn = btop(paddr);
4348 4201 if (cookiep != NULL)
4349 4202 *cookiep = HAC_COOKIE_NONE;
4350 4203 return (0);
4351 4204 }
4352 4205
4353 4206 if (callback_id < (id_t)0 || callback_id >= sfmmu_cb_nextid) {
4354 4207 *rpfn = PFN_INVALID;
4355 4208 return (EINVAL);
4356 4209 }
4357 4210
4358 4211 if ((pahmep = kmem_cache_alloc(pa_hment_cache, kmflags)) == NULL) {
4359 4212 *rpfn = PFN_INVALID;
4360 4213 return (ENOMEM);
4361 4214 }
4362 4215
4363 4216 sfhmep = &pahmep->sfment;
4364 4217
4365 4218 saddr = (caddr_t)((uintptr_t)vaddr & MMU_PAGEMASK);
4366 4219 eaddr = saddr + len;
4367 4220
4368 4221 rehash:
4369 4222 /* Find the mapping(s) for this page */
4370 4223 for (hashno = TTE64K, hmeblkp = NULL;
4371 4224 hmeblkp == NULL && hashno <= mmu_hashcnt;
4372 4225 hashno++) {
4373 4226 hmeshift = HME_HASH_SHIFT(hashno);
4374 4227 hblktag.htag_id = ksfmmup;
4375 4228 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
4376 4229 hblktag.htag_bspage = HME_HASH_BSPAGE(saddr, hmeshift);
4377 4230 hblktag.htag_rehash = hashno;
4378 4231 hmebp = HME_HASH_FUNCTION(ksfmmup, saddr, hmeshift);
4379 4232
4380 4233 SFMMU_HASH_LOCK(hmebp);
4381 4234
4382 4235 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp);
4383 4236
4384 4237 if (hmeblkp == NULL)
4385 4238 SFMMU_HASH_UNLOCK(hmebp);
4386 4239 }
4387 4240
4388 4241 if (hmeblkp == NULL) {
4389 4242 kmem_cache_free(pa_hment_cache, pahmep);
4390 4243 *rpfn = PFN_INVALID;
4391 4244 return (ENXIO);
4392 4245 }
4393 4246
4394 4247 ASSERT(!hmeblkp->hblk_shared);
4395 4248
4396 4249 HBLKTOHME(osfhmep, hmeblkp, saddr);
4397 4250 sfmmu_copytte(&osfhmep->hme_tte, &tte);
4398 4251
4399 4252 if (!TTE_IS_VALID(&tte)) {
4400 4253 SFMMU_HASH_UNLOCK(hmebp);
4401 4254 kmem_cache_free(pa_hment_cache, pahmep);
4402 4255 *rpfn = PFN_INVALID;
4403 4256 return (ENXIO);
4404 4257 }
4405 4258
4406 4259 /*
4407 4260 * Make sure the boundaries for the callback fall within this
4408 4261 * single mapping.
4409 4262 */
4410 4263 baseaddr = (caddr_t)get_hblk_base(hmeblkp);
4411 4264 ASSERT(saddr >= baseaddr);
4412 4265 if (eaddr > saddr + TTEBYTES(TTE_CSZ(&tte))) {
4413 4266 SFMMU_HASH_UNLOCK(hmebp);
4414 4267 kmem_cache_free(pa_hment_cache, pahmep);
4415 4268 *rpfn = PFN_INVALID;
4416 4269 return (ERANGE);
4417 4270 }
4418 4271
4419 4272 pfn = sfmmu_ttetopfn(&tte, vaddr);
4420 4273
4421 4274 /*
4422 4275 * The pfn may not have a page_t underneath in which case we
4423 4276 * just return it. This can happen if we are doing I/O to a
4424 4277 * static portion of the kernel's address space, for instance.
4425 4278 */
4426 4279 pp = osfhmep->hme_page;
4427 4280 if (pp == NULL) {
4428 4281 SFMMU_HASH_UNLOCK(hmebp);
4429 4282 kmem_cache_free(pa_hment_cache, pahmep);
4430 4283 *rpfn = pfn;
4431 4284 if (cookiep)
4432 4285 *cookiep = HAC_COOKIE_NONE;
4433 4286 return (0);
4434 4287 }
4435 4288 ASSERT(pp == PP_PAGEROOT(pp));
4436 4289
4437 4290 vp = pp->p_vnode;
4438 4291 off = pp->p_offset;
4439 4292
4440 4293 pml = sfmmu_mlist_enter(pp);
4441 4294
4442 4295 if (flags & HAC_PAGELOCK) {
4443 4296 if (!page_trylock(pp, SE_SHARED)) {
4444 4297 /*
4445 4298 * Somebody is holding SE_EXCL lock. Might
4446 4299 * even be hat_page_relocate(). Drop all
4447 4300 * our locks, lookup the page in &kvp, and
4448 4301 * retry. If it doesn't exist in &kvp and &zvp,
4449 4302 * then we must be dealing with a kernel mapped
4450 4303 * page which doesn't actually belong to
4451 4304 * segkmem so we punt.
4452 4305 */
4453 4306 sfmmu_mlist_exit(pml);
4454 4307 SFMMU_HASH_UNLOCK(hmebp);
4455 4308 pp = page_lookup(&kvp, (u_offset_t)saddr, SE_SHARED);
4456 4309
4457 4310 /* check zvp before giving up */
4458 4311 if (pp == NULL)
4459 4312 pp = page_lookup(&zvp, (u_offset_t)saddr,
4460 4313 SE_SHARED);
4461 4314
4462 4315 /* Okay, we didn't find it, give up */
4463 4316 if (pp == NULL) {
4464 4317 kmem_cache_free(pa_hment_cache, pahmep);
4465 4318 *rpfn = pfn;
4466 4319 if (cookiep)
4467 4320 *cookiep = HAC_COOKIE_NONE;
4468 4321 return (0);
4469 4322 }
4470 4323 page_unlock(pp);
4471 4324 goto rehash;
4472 4325 }
4473 4326 locked = 1;
4474 4327 }
4475 4328
4476 4329 if (!PAGE_LOCKED(pp) && !panicstr)
4477 4330 panic("hat_add_callback: page 0x%p not locked", (void *)pp);
4478 4331
4479 4332 if (osfhmep->hme_page != pp || pp->p_vnode != vp ||
4480 4333 pp->p_offset != off) {
4481 4334 /*
4482 4335 * The page moved before we got our hands on it. Drop
4483 4336 * all the locks and try again.
4484 4337 */
4485 4338 ASSERT((flags & HAC_PAGELOCK) != 0);
4486 4339 sfmmu_mlist_exit(pml);
4487 4340 SFMMU_HASH_UNLOCK(hmebp);
4488 4341 page_unlock(pp);
4489 4342 locked = 0;
4490 4343 goto rehash;
4491 4344 }
4492 4345
4493 4346 if (!VN_ISKAS(vp)) {
4494 4347 /*
4495 4348 * This is not a segkmem page but another page which
4496 4349 * has been kernel mapped. It had better have at least
4497 4350 * a share lock on it. Return the pfn.
4498 4351 */
4499 4352 sfmmu_mlist_exit(pml);
4500 4353 SFMMU_HASH_UNLOCK(hmebp);
4501 4354 if (locked)
4502 4355 page_unlock(pp);
4503 4356 kmem_cache_free(pa_hment_cache, pahmep);
4504 4357 ASSERT(PAGE_LOCKED(pp));
4505 4358 *rpfn = pfn;
4506 4359 if (cookiep)
4507 4360 *cookiep = HAC_COOKIE_NONE;
4508 4361 return (0);
4509 4362 }
4510 4363
4511 4364 /*
4512 4365 * Setup this pa_hment and link its embedded dummy sf_hment into
4513 4366 * the mapping list.
4514 4367 */
4515 4368 pp->p_share++;
4516 4369 pahmep->cb_id = callback_id;
4517 4370 pahmep->addr = vaddr;
4518 4371 pahmep->len = len;
4519 4372 pahmep->refcnt = 1;
4520 4373 pahmep->flags = 0;
4521 4374 pahmep->pvt = pvt;
4522 4375
4523 4376 sfhmep->hme_tte.ll = 0;
4524 4377 sfhmep->hme_data = pahmep;
4525 4378 sfhmep->hme_prev = osfhmep;
4526 4379 sfhmep->hme_next = osfhmep->hme_next;
4527 4380
4528 4381 if (osfhmep->hme_next)
4529 4382 osfhmep->hme_next->hme_prev = sfhmep;
4530 4383
4531 4384 osfhmep->hme_next = sfhmep;
4532 4385
4533 4386 sfmmu_mlist_exit(pml);
4534 4387 SFMMU_HASH_UNLOCK(hmebp);
4535 4388
4536 4389 if (locked)
4537 4390 page_unlock(pp);
4538 4391
4539 4392 *rpfn = pfn;
4540 4393 if (cookiep)
4541 4394 *cookiep = (void *)pahmep;
4542 4395
4543 4396 return (0);
4544 4397 }
4545 4398
4546 4399 /*
4547 4400 * Remove the relocation callbacks from the specified addr/len.
4548 4401 */
4549 4402 void
4550 4403 hat_delete_callback(caddr_t vaddr, uint_t len, void *pvt, uint_t flags,
4551 4404 void *cookie)
4552 4405 {
4553 4406 struct hmehash_bucket *hmebp;
4554 4407 hmeblk_tag hblktag;
4555 4408 struct hme_blk *hmeblkp;
4556 4409 int hmeshift, hashno;
4557 4410 caddr_t saddr;
4558 4411 struct pa_hment *pahmep;
4559 4412 struct sf_hment *sfhmep, *osfhmep;
4560 4413 kmutex_t *pml;
4561 4414 tte_t tte;
4562 4415 page_t *pp;
4563 4416 vnode_t *vp;
4564 4417 u_offset_t off;
4565 4418 int locked = 0;
4566 4419
4567 4420 /*
4568 4421 * If the cookie is HAC_COOKIE_NONE then there is no pa_hment to
4569 4422 * remove so just return.
4570 4423 */
4571 4424 if (cookie == HAC_COOKIE_NONE || IS_KPM_ADDR(vaddr))
4572 4425 return;
4573 4426
4574 4427 saddr = (caddr_t)((uintptr_t)vaddr & MMU_PAGEMASK);
4575 4428
4576 4429 rehash:
4577 4430 /* Find the mapping(s) for this page */
4578 4431 for (hashno = TTE64K, hmeblkp = NULL;
4579 4432 hmeblkp == NULL && hashno <= mmu_hashcnt;
4580 4433 hashno++) {
4581 4434 hmeshift = HME_HASH_SHIFT(hashno);
4582 4435 hblktag.htag_id = ksfmmup;
4583 4436 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
4584 4437 hblktag.htag_bspage = HME_HASH_BSPAGE(saddr, hmeshift);
4585 4438 hblktag.htag_rehash = hashno;
4586 4439 hmebp = HME_HASH_FUNCTION(ksfmmup, saddr, hmeshift);
4587 4440
4588 4441 SFMMU_HASH_LOCK(hmebp);
4589 4442
4590 4443 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp);
4591 4444
4592 4445 if (hmeblkp == NULL)
4593 4446 SFMMU_HASH_UNLOCK(hmebp);
4594 4447 }
4595 4448
4596 4449 if (hmeblkp == NULL)
4597 4450 return;
4598 4451
4599 4452 ASSERT(!hmeblkp->hblk_shared);
4600 4453
4601 4454 HBLKTOHME(osfhmep, hmeblkp, saddr);
4602 4455
4603 4456 sfmmu_copytte(&osfhmep->hme_tte, &tte);
4604 4457 if (!TTE_IS_VALID(&tte)) {
4605 4458 SFMMU_HASH_UNLOCK(hmebp);
4606 4459 return;
4607 4460 }
4608 4461
4609 4462 pp = osfhmep->hme_page;
4610 4463 if (pp == NULL) {
4611 4464 SFMMU_HASH_UNLOCK(hmebp);
4612 4465 ASSERT(cookie == NULL);
4613 4466 return;
4614 4467 }
4615 4468
4616 4469 vp = pp->p_vnode;
4617 4470 off = pp->p_offset;
4618 4471
4619 4472 pml = sfmmu_mlist_enter(pp);
4620 4473
4621 4474 if (flags & HAC_PAGELOCK) {
4622 4475 if (!page_trylock(pp, SE_SHARED)) {
4623 4476 /*
4624 4477 * Somebody is holding SE_EXCL lock. Might
4625 4478 * even be hat_page_relocate(). Drop all
4626 4479 * our locks, lookup the page in &kvp, and
4627 4480 * retry. If it doesn't exist in &kvp and &zvp,
4628 4481 * then we must be dealing with a kernel mapped
4629 4482 * page which doesn't actually belong to
4630 4483 * segkmem so we punt.
4631 4484 */
4632 4485 sfmmu_mlist_exit(pml);
4633 4486 SFMMU_HASH_UNLOCK(hmebp);
4634 4487 pp = page_lookup(&kvp, (u_offset_t)saddr, SE_SHARED);
4635 4488 /* check zvp before giving up */
4636 4489 if (pp == NULL)
4637 4490 pp = page_lookup(&zvp, (u_offset_t)saddr,
4638 4491 SE_SHARED);
4639 4492
4640 4493 if (pp == NULL) {
4641 4494 ASSERT(cookie == NULL);
4642 4495 return;
4643 4496 }
4644 4497 page_unlock(pp);
4645 4498 goto rehash;
4646 4499 }
4647 4500 locked = 1;
4648 4501 }
4649 4502
4650 4503 ASSERT(PAGE_LOCKED(pp));
4651 4504
4652 4505 if (osfhmep->hme_page != pp || pp->p_vnode != vp ||
4653 4506 pp->p_offset != off) {
4654 4507 /*
4655 4508 * The page moved before we got our hands on it. Drop
4656 4509 * all the locks and try again.
4657 4510 */
4658 4511 ASSERT((flags & HAC_PAGELOCK) != 0);
4659 4512 sfmmu_mlist_exit(pml);
4660 4513 SFMMU_HASH_UNLOCK(hmebp);
4661 4514 page_unlock(pp);
4662 4515 locked = 0;
4663 4516 goto rehash;
4664 4517 }
4665 4518
4666 4519 if (!VN_ISKAS(vp)) {
4667 4520 /*
4668 4521 * This is not a segkmem page but another page which
4669 4522 * has been kernel mapped.
4670 4523 */
4671 4524 sfmmu_mlist_exit(pml);
4672 4525 SFMMU_HASH_UNLOCK(hmebp);
4673 4526 if (locked)
4674 4527 page_unlock(pp);
4675 4528 ASSERT(cookie == NULL);
4676 4529 return;
4677 4530 }
4678 4531
4679 4532 if (cookie != NULL) {
4680 4533 pahmep = (struct pa_hment *)cookie;
4681 4534 sfhmep = &pahmep->sfment;
4682 4535 } else {
4683 4536 for (sfhmep = pp->p_mapping; sfhmep != NULL;
4684 4537 sfhmep = sfhmep->hme_next) {
4685 4538
4686 4539 /*
4687 4540 * skip va<->pa mappings
4688 4541 */
4689 4542 if (!IS_PAHME(sfhmep))
4690 4543 continue;
4691 4544
4692 4545 pahmep = sfhmep->hme_data;
4693 4546 ASSERT(pahmep != NULL);
4694 4547
4695 4548 /*
4696 4549 * if pa_hment matches, remove it
4697 4550 */
4698 4551 if ((pahmep->pvt == pvt) &&
4699 4552 (pahmep->addr == vaddr) &&
4700 4553 (pahmep->len == len)) {
4701 4554 break;
4702 4555 }
4703 4556 }
4704 4557 }
4705 4558
4706 4559 if (sfhmep == NULL) {
4707 4560 if (!panicstr) {
4708 4561 panic("hat_delete_callback: pa_hment not found, pp %p",
4709 4562 (void *)pp);
4710 4563 }
4711 4564 return;
4712 4565 }
4713 4566
4714 4567 /*
4715 4568 * Note: at this point a valid kernel mapping must still be
4716 4569 * present on this page.
4717 4570 */
4718 4571 pp->p_share--;
4719 4572 if (pp->p_share <= 0)
4720 4573 panic("hat_delete_callback: zero p_share");
4721 4574
4722 4575 if (--pahmep->refcnt == 0) {
4723 4576 if (pahmep->flags != 0)
4724 4577 panic("hat_delete_callback: pa_hment is busy");
4725 4578
4726 4579 /*
4727 4580 * Remove sfhmep from the mapping list for the page.
4728 4581 */
4729 4582 if (sfhmep->hme_prev) {
4730 4583 sfhmep->hme_prev->hme_next = sfhmep->hme_next;
4731 4584 } else {
4732 4585 pp->p_mapping = sfhmep->hme_next;
4733 4586 }
4734 4587
4735 4588 if (sfhmep->hme_next)
4736 4589 sfhmep->hme_next->hme_prev = sfhmep->hme_prev;
4737 4590
4738 4591 sfmmu_mlist_exit(pml);
4739 4592 SFMMU_HASH_UNLOCK(hmebp);
4740 4593
4741 4594 if (locked)
4742 4595 page_unlock(pp);
4743 4596
4744 4597 kmem_cache_free(pa_hment_cache, pahmep);
4745 4598 return;
4746 4599 }
4747 4600
4748 4601 sfmmu_mlist_exit(pml);
4749 4602 SFMMU_HASH_UNLOCK(hmebp);
4750 4603 if (locked)
4751 4604 page_unlock(pp);
4752 4605 }
4753 4606
4754 4607 /*
4755 4608 * hat_probe returns 1 if the translation for the address 'addr' is
4756 4609 * loaded, zero otherwise.
4757 4610 *
4758 4611 * hat_probe should be used only for advisorary purposes because it may
4759 4612 * occasionally return the wrong value. The implementation must guarantee that
4760 4613 * returning the wrong value is a very rare event. hat_probe is used
4761 4614 * to implement optimizations in the segment drivers.
4762 4615 *
4763 4616 */
4764 4617 int
4765 4618 hat_probe(struct hat *sfmmup, caddr_t addr)
4766 4619 {
4767 4620 pfn_t pfn;
4768 4621 tte_t tte;
4769 4622
4770 4623 ASSERT(sfmmup != NULL);
4771 4624 ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
4772 4625
4773 4626 ASSERT((sfmmup == ksfmmup) ||
4774 4627 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
4775 4628
4776 4629 if (sfmmup == ksfmmup) {
4777 4630 while ((pfn = sfmmu_vatopfn(addr, sfmmup, &tte))
4778 4631 == PFN_SUSPENDED) {
4779 4632 sfmmu_vatopfn_suspended(addr, sfmmup, &tte);
4780 4633 }
4781 4634 } else {
4782 4635 pfn = sfmmu_uvatopfn(addr, sfmmup, NULL);
4783 4636 }
4784 4637
4785 4638 if (pfn != PFN_INVALID)
4786 4639 return (1);
4787 4640 else
4788 4641 return (0);
4789 4642 }
4790 4643
4791 4644 ssize_t
4792 4645 hat_getpagesize(struct hat *sfmmup, caddr_t addr)
4793 4646 {
4794 4647 tte_t tte;
4795 4648
4796 4649 ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
4797 4650
4798 4651 if (sfmmup == ksfmmup) {
4799 4652 if (sfmmu_vatopfn(addr, sfmmup, &tte) == PFN_INVALID) {
4800 4653 return (-1);
4801 4654 }
4802 4655 } else {
4803 4656 if (sfmmu_uvatopfn(addr, sfmmup, &tte) == PFN_INVALID) {
4804 4657 return (-1);
4805 4658 }
4806 4659 }
4807 4660
4808 4661 ASSERT(TTE_IS_VALID(&tte));
4809 4662 return (TTEBYTES(TTE_CSZ(&tte)));
4810 4663 }
4811 4664
4812 4665 uint_t
4813 4666 hat_getattr(struct hat *sfmmup, caddr_t addr, uint_t *attr)
4814 4667 {
4815 4668 tte_t tte;
4816 4669
4817 4670 ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
4818 4671
4819 4672 if (sfmmup == ksfmmup) {
4820 4673 if (sfmmu_vatopfn(addr, sfmmup, &tte) == PFN_INVALID) {
4821 4674 tte.ll = 0;
4822 4675 }
4823 4676 } else {
4824 4677 if (sfmmu_uvatopfn(addr, sfmmup, &tte) == PFN_INVALID) {
4825 4678 tte.ll = 0;
4826 4679 }
4827 4680 }
4828 4681 if (TTE_IS_VALID(&tte)) {
4829 4682 *attr = sfmmu_ptov_attr(&tte);
4830 4683 return (0);
4831 4684 }
4832 4685 *attr = 0;
4833 4686 return ((uint_t)0xffffffff);
4834 4687 }
4835 4688
4836 4689 /*
4837 4690 * Enables more attributes on specified address range (ie. logical OR)
4838 4691 */
4839 4692 void
4840 4693 hat_setattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr)
4841 4694 {
4842 4695 if (hat->sfmmu_xhat_provider) {
4843 4696 XHAT_SETATTR(hat, addr, len, attr);
4844 4697 return;
4845 4698 } else {
4846 4699 /*
4847 4700 * This must be a CPU HAT. If the address space has
4848 4701 * XHATs attached, change attributes for all of them,
4849 4702 * just in case
4850 4703 */
4851 4704 ASSERT(hat->sfmmu_as != NULL);
4852 4705 if (hat->sfmmu_as->a_xhat != NULL)
4853 4706 xhat_setattr_all(hat->sfmmu_as, addr, len, attr);
4854 4707 }
4855 4708
4856 4709 sfmmu_chgattr(hat, addr, len, attr, SFMMU_SETATTR);
4857 4710 }
4858 4711
4859 4712 /*
4860 4713 * Assigns attributes to the specified address range. All the attributes
4861 4714 * are specified.
4862 4715 */
4863 4716 void
4864 4717 hat_chgattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr)
4865 4718 {
4866 4719 if (hat->sfmmu_xhat_provider) {
4867 4720 XHAT_CHGATTR(hat, addr, len, attr);
4868 4721 return;
4869 4722 } else {
4870 4723 /*
4871 4724 * This must be a CPU HAT. If the address space has
4872 4725 * XHATs attached, change attributes for all of them,
4873 4726 * just in case
4874 4727 */
4875 4728 ASSERT(hat->sfmmu_as != NULL);
4876 4729 if (hat->sfmmu_as->a_xhat != NULL)
4877 4730 xhat_chgattr_all(hat->sfmmu_as, addr, len, attr);
4878 4731 }
4879 4732
4880 4733 sfmmu_chgattr(hat, addr, len, attr, SFMMU_CHGATTR);
4881 4734 }
4882 4735
4883 4736 /*
4884 4737 * Remove attributes on the specified address range (ie. loginal NAND)
4885 4738 */
4886 4739 void
4887 4740 hat_clrattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr)
4888 4741 {
4889 4742 if (hat->sfmmu_xhat_provider) {
4890 4743 XHAT_CLRATTR(hat, addr, len, attr);
4891 4744 return;
4892 4745 } else {
4893 4746 /*
4894 4747 * This must be a CPU HAT. If the address space has
4895 4748 * XHATs attached, change attributes for all of them,
4896 4749 * just in case
4897 4750 */
4898 4751 ASSERT(hat->sfmmu_as != NULL);
4899 4752 if (hat->sfmmu_as->a_xhat != NULL)
4900 4753 xhat_clrattr_all(hat->sfmmu_as, addr, len, attr);
4901 4754 }
4902 4755
4903 4756 sfmmu_chgattr(hat, addr, len, attr, SFMMU_CLRATTR);
4904 4757 }
4905 4758
4906 4759 /*
4907 4760 * Change attributes on an address range to that specified by attr and mode.
4908 4761 */
4909 4762 static void
4910 4763 sfmmu_chgattr(struct hat *sfmmup, caddr_t addr, size_t len, uint_t attr,
4911 4764 int mode)
4912 4765 {
4913 4766 struct hmehash_bucket *hmebp;
4914 4767 hmeblk_tag hblktag;
4915 4768 int hmeshift, hashno = 1;
4916 4769 struct hme_blk *hmeblkp, *list = NULL;
4917 4770 caddr_t endaddr;
4918 4771 cpuset_t cpuset;
4919 4772 demap_range_t dmr;
4920 4773
4921 4774 CPUSET_ZERO(cpuset);
4922 4775
4923 4776 ASSERT((sfmmup == ksfmmup) ||
4924 4777 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
4925 4778 ASSERT((len & MMU_PAGEOFFSET) == 0);
4926 4779 ASSERT(((uintptr_t)addr & MMU_PAGEOFFSET) == 0);
4927 4780
4928 4781 if ((attr & PROT_USER) && (mode != SFMMU_CLRATTR) &&
4929 4782 ((addr + len) > (caddr_t)USERLIMIT)) {
4930 4783 panic("user addr %p in kernel space",
4931 4784 (void *)addr);
4932 4785 }
4933 4786
4934 4787 endaddr = addr + len;
4935 4788 hblktag.htag_id = sfmmup;
4936 4789 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
4937 4790 DEMAP_RANGE_INIT(sfmmup, &dmr);
4938 4791
4939 4792 while (addr < endaddr) {
4940 4793 hmeshift = HME_HASH_SHIFT(hashno);
4941 4794 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
4942 4795 hblktag.htag_rehash = hashno;
4943 4796 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
4944 4797
4945 4798 SFMMU_HASH_LOCK(hmebp);
4946 4799
4947 4800 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list);
4948 4801 if (hmeblkp != NULL) {
4949 4802 ASSERT(!hmeblkp->hblk_shared);
4950 4803 /*
4951 4804 * We've encountered a shadow hmeblk so skip the range
4952 4805 * of the next smaller mapping size.
4953 4806 */
4954 4807 if (hmeblkp->hblk_shw_bit) {
4955 4808 ASSERT(sfmmup != ksfmmup);
4956 4809 ASSERT(hashno > 1);
4957 4810 addr = (caddr_t)P2END((uintptr_t)addr,
4958 4811 TTEBYTES(hashno - 1));
4959 4812 } else {
4960 4813 addr = sfmmu_hblk_chgattr(sfmmup,
4961 4814 hmeblkp, addr, endaddr, &dmr, attr, mode);
4962 4815 }
4963 4816 SFMMU_HASH_UNLOCK(hmebp);
4964 4817 hashno = 1;
4965 4818 continue;
4966 4819 }
4967 4820 SFMMU_HASH_UNLOCK(hmebp);
4968 4821
4969 4822 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) {
4970 4823 /*
4971 4824 * We have traversed the whole list and rehashed
4972 4825 * if necessary without finding the address to chgattr.
4973 4826 * This is ok, so we increment the address by the
4974 4827 * smallest hmeblk range for kernel mappings or for
4975 4828 * user mappings with no large pages, and the largest
4976 4829 * hmeblk range, to account for shadow hmeblks, for
4977 4830 * user mappings with large pages and continue.
4978 4831 */
4979 4832 if (sfmmup == ksfmmup)
4980 4833 addr = (caddr_t)P2END((uintptr_t)addr,
4981 4834 TTEBYTES(1));
4982 4835 else
4983 4836 addr = (caddr_t)P2END((uintptr_t)addr,
4984 4837 TTEBYTES(hashno));
4985 4838 hashno = 1;
4986 4839 } else {
4987 4840 hashno++;
4988 4841 }
4989 4842 }
4990 4843
4991 4844 sfmmu_hblks_list_purge(&list, 0);
4992 4845 DEMAP_RANGE_FLUSH(&dmr);
4993 4846 cpuset = sfmmup->sfmmu_cpusran;
4994 4847 xt_sync(cpuset);
4995 4848 }
4996 4849
4997 4850 /*
4998 4851 * This function chgattr on a range of addresses in an hmeblk. It returns the
4999 4852 * next addres that needs to be chgattr.
5000 4853 * It should be called with the hash lock held.
5001 4854 * XXX It should be possible to optimize chgattr by not flushing every time but
5002 4855 * on the other hand:
5003 4856 * 1. do one flush crosscall.
5004 4857 * 2. only flush if we are increasing permissions (make sure this will work)
5005 4858 */
5006 4859 static caddr_t
5007 4860 sfmmu_hblk_chgattr(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr,
5008 4861 caddr_t endaddr, demap_range_t *dmrp, uint_t attr, int mode)
5009 4862 {
5010 4863 tte_t tte, tteattr, tteflags, ttemod;
5011 4864 struct sf_hment *sfhmep;
5012 4865 int ttesz;
5013 4866 struct page *pp = NULL;
5014 4867 kmutex_t *pml, *pmtx;
5015 4868 int ret;
5016 4869 int use_demap_range;
5017 4870 #if defined(SF_ERRATA_57)
5018 4871 int check_exec;
5019 4872 #endif
5020 4873
5021 4874 ASSERT(in_hblk_range(hmeblkp, addr));
5022 4875 ASSERT(hmeblkp->hblk_shw_bit == 0);
5023 4876 ASSERT(!hmeblkp->hblk_shared);
5024 4877
5025 4878 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp));
5026 4879 ttesz = get_hblk_ttesz(hmeblkp);
5027 4880
5028 4881 /*
5029 4882 * Flush the current demap region if addresses have been
5030 4883 * skipped or the page size doesn't match.
5031 4884 */
5032 4885 use_demap_range = (TTEBYTES(ttesz) == DEMAP_RANGE_PGSZ(dmrp));
5033 4886 if (use_demap_range) {
5034 4887 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr);
5035 4888 } else if (dmrp != NULL) {
5036 4889 DEMAP_RANGE_FLUSH(dmrp);
5037 4890 }
5038 4891
5039 4892 tteattr.ll = sfmmu_vtop_attr(attr, mode, &tteflags);
5040 4893 #if defined(SF_ERRATA_57)
5041 4894 check_exec = (sfmmup != ksfmmup) &&
5042 4895 AS_TYPE_64BIT(sfmmup->sfmmu_as) &&
5043 4896 TTE_IS_EXECUTABLE(&tteattr);
5044 4897 #endif
5045 4898 HBLKTOHME(sfhmep, hmeblkp, addr);
5046 4899 while (addr < endaddr) {
5047 4900 sfmmu_copytte(&sfhmep->hme_tte, &tte);
5048 4901 if (TTE_IS_VALID(&tte)) {
5049 4902 if ((tte.ll & tteflags.ll) == tteattr.ll) {
5050 4903 /*
5051 4904 * if the new attr is the same as old
5052 4905 * continue
5053 4906 */
5054 4907 goto next_addr;
5055 4908 }
5056 4909 if (!TTE_IS_WRITABLE(&tteattr)) {
5057 4910 /*
5058 4911 * make sure we clear hw modify bit if we
5059 4912 * removing write protections
5060 4913 */
5061 4914 tteflags.tte_intlo |= TTE_HWWR_INT;
5062 4915 }
5063 4916
5064 4917 pml = NULL;
5065 4918 pp = sfhmep->hme_page;
5066 4919 if (pp) {
5067 4920 pml = sfmmu_mlist_enter(pp);
5068 4921 }
5069 4922
5070 4923 if (pp != sfhmep->hme_page) {
5071 4924 /*
5072 4925 * tte must have been unloaded.
5073 4926 */
5074 4927 ASSERT(pml);
5075 4928 sfmmu_mlist_exit(pml);
5076 4929 continue;
5077 4930 }
5078 4931
5079 4932 ASSERT(pp == NULL || sfmmu_mlist_held(pp));
5080 4933
5081 4934 ttemod = tte;
5082 4935 ttemod.ll = (ttemod.ll & ~tteflags.ll) | tteattr.ll;
5083 4936 ASSERT(TTE_TO_TTEPFN(&ttemod) == TTE_TO_TTEPFN(&tte));
5084 4937
5085 4938 #if defined(SF_ERRATA_57)
5086 4939 if (check_exec && addr < errata57_limit)
5087 4940 ttemod.tte_exec_perm = 0;
5088 4941 #endif
5089 4942 ret = sfmmu_modifytte_try(&tte, &ttemod,
5090 4943 &sfhmep->hme_tte);
5091 4944
5092 4945 if (ret < 0) {
5093 4946 /* tte changed underneath us */
5094 4947 if (pml) {
5095 4948 sfmmu_mlist_exit(pml);
5096 4949 }
5097 4950 continue;
5098 4951 }
5099 4952
5100 4953 if (tteflags.tte_intlo & TTE_HWWR_INT) {
5101 4954 /*
5102 4955 * need to sync if we are clearing modify bit.
5103 4956 */
5104 4957 sfmmu_ttesync(sfmmup, addr, &tte, pp);
5105 4958 }
5106 4959
5107 4960 if (pp && PP_ISRO(pp)) {
5108 4961 if (tteattr.tte_intlo & TTE_WRPRM_INT) {
5109 4962 pmtx = sfmmu_page_enter(pp);
5110 4963 PP_CLRRO(pp);
5111 4964 sfmmu_page_exit(pmtx);
5112 4965 }
5113 4966 }
5114 4967
5115 4968 if (ret > 0 && use_demap_range) {
5116 4969 DEMAP_RANGE_MARKPG(dmrp, addr);
5117 4970 } else if (ret > 0) {
5118 4971 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0);
5119 4972 }
5120 4973
5121 4974 if (pml) {
5122 4975 sfmmu_mlist_exit(pml);
5123 4976 }
5124 4977 }
5125 4978 next_addr:
5126 4979 addr += TTEBYTES(ttesz);
5127 4980 sfhmep++;
5128 4981 DEMAP_RANGE_NEXTPG(dmrp);
5129 4982 }
5130 4983 return (addr);
5131 4984 }
5132 4985
5133 4986 /*
5134 4987 * This routine converts virtual attributes to physical ones. It will
5135 4988 * update the tteflags field with the tte mask corresponding to the attributes
5136 4989 * affected and it returns the new attributes. It will also clear the modify
5137 4990 * bit if we are taking away write permission. This is necessary since the
5138 4991 * modify bit is the hardware permission bit and we need to clear it in order
5139 4992 * to detect write faults.
5140 4993 */
5141 4994 static uint64_t
5142 4995 sfmmu_vtop_attr(uint_t attr, int mode, tte_t *ttemaskp)
5143 4996 {
5144 4997 tte_t ttevalue;
5145 4998
5146 4999 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR));
5147 5000
5148 5001 switch (mode) {
5149 5002 case SFMMU_CHGATTR:
5150 5003 /* all attributes specified */
5151 5004 ttevalue.tte_inthi = MAKE_TTEATTR_INTHI(attr);
5152 5005 ttevalue.tte_intlo = MAKE_TTEATTR_INTLO(attr);
5153 5006 ttemaskp->tte_inthi = TTEINTHI_ATTR;
5154 5007 ttemaskp->tte_intlo = TTEINTLO_ATTR;
5155 5008 break;
5156 5009 case SFMMU_SETATTR:
5157 5010 ASSERT(!(attr & ~HAT_PROT_MASK));
5158 5011 ttemaskp->ll = 0;
5159 5012 ttevalue.ll = 0;
5160 5013 /*
5161 5014 * a valid tte implies exec and read for sfmmu
5162 5015 * so no need to do anything about them.
5163 5016 * since priviledged access implies user access
5164 5017 * PROT_USER doesn't make sense either.
5165 5018 */
5166 5019 if (attr & PROT_WRITE) {
5167 5020 ttemaskp->tte_intlo |= TTE_WRPRM_INT;
5168 5021 ttevalue.tte_intlo |= TTE_WRPRM_INT;
5169 5022 }
5170 5023 break;
5171 5024 case SFMMU_CLRATTR:
5172 5025 /* attributes will be nand with current ones */
5173 5026 if (attr & ~(PROT_WRITE | PROT_USER)) {
5174 5027 panic("sfmmu: attr %x not supported", attr);
5175 5028 }
5176 5029 ttemaskp->ll = 0;
5177 5030 ttevalue.ll = 0;
5178 5031 if (attr & PROT_WRITE) {
5179 5032 /* clear both writable and modify bit */
5180 5033 ttemaskp->tte_intlo |= TTE_WRPRM_INT | TTE_HWWR_INT;
5181 5034 }
5182 5035 if (attr & PROT_USER) {
5183 5036 ttemaskp->tte_intlo |= TTE_PRIV_INT;
5184 5037 ttevalue.tte_intlo |= TTE_PRIV_INT;
5185 5038 }
5186 5039 break;
5187 5040 default:
5188 5041 panic("sfmmu_vtop_attr: bad mode %x", mode);
5189 5042 }
5190 5043 ASSERT(TTE_TO_TTEPFN(&ttevalue) == 0);
5191 5044 return (ttevalue.ll);
5192 5045 }
5193 5046
5194 5047 static uint_t
5195 5048 sfmmu_ptov_attr(tte_t *ttep)
5196 5049 {
5197 5050 uint_t attr;
5198 5051
5199 5052 ASSERT(TTE_IS_VALID(ttep));
5200 5053
5201 5054 attr = PROT_READ;
5202 5055
5203 5056 if (TTE_IS_WRITABLE(ttep)) {
5204 5057 attr |= PROT_WRITE;
5205 5058 }
5206 5059 if (TTE_IS_EXECUTABLE(ttep)) {
5207 5060 attr |= PROT_EXEC;
5208 5061 }
5209 5062 if (!TTE_IS_PRIVILEGED(ttep)) {
5210 5063 attr |= PROT_USER;
5211 5064 }
5212 5065 if (TTE_IS_NFO(ttep)) {
5213 5066 attr |= HAT_NOFAULT;
5214 5067 }
5215 5068 if (TTE_IS_NOSYNC(ttep)) {
5216 5069 attr |= HAT_NOSYNC;
5217 5070 }
5218 5071 if (TTE_IS_SIDEFFECT(ttep)) {
5219 5072 attr |= SFMMU_SIDEFFECT;
5220 5073 }
5221 5074 if (!TTE_IS_VCACHEABLE(ttep)) {
5222 5075 attr |= SFMMU_UNCACHEVTTE;
5223 5076 }
5224 5077 if (!TTE_IS_PCACHEABLE(ttep)) {
5225 5078 attr |= SFMMU_UNCACHEPTTE;
5226 5079 }
5227 5080 return (attr);
5228 5081 }
5229 5082
5230 5083 /*
5231 5084 * hat_chgprot is a deprecated hat call. New segment drivers
5232 5085 * should store all attributes and use hat_*attr calls.
5233 5086 *
5234 5087 * Change the protections in the virtual address range
5235 5088 * given to the specified virtual protection. If vprot is ~PROT_WRITE,
5236 5089 * then remove write permission, leaving the other
5237 5090 * permissions unchanged. If vprot is ~PROT_USER, remove user permissions.
5238 5091 *
5239 5092 */
5240 5093 void
5241 5094 hat_chgprot(struct hat *sfmmup, caddr_t addr, size_t len, uint_t vprot)
5242 5095 {
5243 5096 struct hmehash_bucket *hmebp;
5244 5097 hmeblk_tag hblktag;
5245 5098 int hmeshift, hashno = 1;
5246 5099 struct hme_blk *hmeblkp, *list = NULL;
5247 5100 caddr_t endaddr;
5248 5101 cpuset_t cpuset;
5249 5102 demap_range_t dmr;
5250 5103
5251 5104 ASSERT((len & MMU_PAGEOFFSET) == 0);
5252 5105 ASSERT(((uintptr_t)addr & MMU_PAGEOFFSET) == 0);
5253 5106
5254 5107 if (sfmmup->sfmmu_xhat_provider) {
5255 5108 XHAT_CHGPROT(sfmmup, addr, len, vprot);
5256 5109 return;
5257 5110 } else {
5258 5111 /*
5259 5112 * This must be a CPU HAT. If the address space has
5260 5113 * XHATs attached, change attributes for all of them,
5261 5114 * just in case
5262 5115 */
5263 5116 ASSERT(sfmmup->sfmmu_as != NULL);
5264 5117 if (sfmmup->sfmmu_as->a_xhat != NULL)
5265 5118 xhat_chgprot_all(sfmmup->sfmmu_as, addr, len, vprot);
5266 5119 }
5267 5120
5268 5121 CPUSET_ZERO(cpuset);
5269 5122
5270 5123 if ((vprot != (uint_t)~PROT_WRITE) && (vprot & PROT_USER) &&
5271 5124 ((addr + len) > (caddr_t)USERLIMIT)) {
5272 5125 panic("user addr %p vprot %x in kernel space",
5273 5126 (void *)addr, vprot);
5274 5127 }
5275 5128 endaddr = addr + len;
5276 5129 hblktag.htag_id = sfmmup;
5277 5130 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
5278 5131 DEMAP_RANGE_INIT(sfmmup, &dmr);
5279 5132
5280 5133 while (addr < endaddr) {
5281 5134 hmeshift = HME_HASH_SHIFT(hashno);
5282 5135 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
5283 5136 hblktag.htag_rehash = hashno;
5284 5137 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
5285 5138
5286 5139 SFMMU_HASH_LOCK(hmebp);
5287 5140
5288 5141 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list);
5289 5142 if (hmeblkp != NULL) {
5290 5143 ASSERT(!hmeblkp->hblk_shared);
5291 5144 /*
5292 5145 * We've encountered a shadow hmeblk so skip the range
5293 5146 * of the next smaller mapping size.
5294 5147 */
5295 5148 if (hmeblkp->hblk_shw_bit) {
5296 5149 ASSERT(sfmmup != ksfmmup);
5297 5150 ASSERT(hashno > 1);
5298 5151 addr = (caddr_t)P2END((uintptr_t)addr,
5299 5152 TTEBYTES(hashno - 1));
5300 5153 } else {
5301 5154 addr = sfmmu_hblk_chgprot(sfmmup, hmeblkp,
5302 5155 addr, endaddr, &dmr, vprot);
5303 5156 }
5304 5157 SFMMU_HASH_UNLOCK(hmebp);
5305 5158 hashno = 1;
5306 5159 continue;
5307 5160 }
5308 5161 SFMMU_HASH_UNLOCK(hmebp);
5309 5162
5310 5163 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) {
5311 5164 /*
5312 5165 * We have traversed the whole list and rehashed
5313 5166 * if necessary without finding the address to chgprot.
5314 5167 * This is ok so we increment the address by the
5315 5168 * smallest hmeblk range for kernel mappings and the
5316 5169 * largest hmeblk range, to account for shadow hmeblks,
5317 5170 * for user mappings and continue.
5318 5171 */
5319 5172 if (sfmmup == ksfmmup)
5320 5173 addr = (caddr_t)P2END((uintptr_t)addr,
5321 5174 TTEBYTES(1));
5322 5175 else
5323 5176 addr = (caddr_t)P2END((uintptr_t)addr,
5324 5177 TTEBYTES(hashno));
5325 5178 hashno = 1;
5326 5179 } else {
5327 5180 hashno++;
5328 5181 }
5329 5182 }
5330 5183
5331 5184 sfmmu_hblks_list_purge(&list, 0);
5332 5185 DEMAP_RANGE_FLUSH(&dmr);
5333 5186 cpuset = sfmmup->sfmmu_cpusran;
5334 5187 xt_sync(cpuset);
5335 5188 }
5336 5189
5337 5190 /*
5338 5191 * This function chgprots a range of addresses in an hmeblk. It returns the
5339 5192 * next addres that needs to be chgprot.
5340 5193 * It should be called with the hash lock held.
5341 5194 * XXX It shold be possible to optimize chgprot by not flushing every time but
5342 5195 * on the other hand:
5343 5196 * 1. do one flush crosscall.
5344 5197 * 2. only flush if we are increasing permissions (make sure this will work)
5345 5198 */
5346 5199 static caddr_t
5347 5200 sfmmu_hblk_chgprot(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, caddr_t addr,
5348 5201 caddr_t endaddr, demap_range_t *dmrp, uint_t vprot)
5349 5202 {
5350 5203 uint_t pprot;
5351 5204 tte_t tte, ttemod;
5352 5205 struct sf_hment *sfhmep;
5353 5206 uint_t tteflags;
5354 5207 int ttesz;
5355 5208 struct page *pp = NULL;
5356 5209 kmutex_t *pml, *pmtx;
5357 5210 int ret;
5358 5211 int use_demap_range;
5359 5212 #if defined(SF_ERRATA_57)
5360 5213 int check_exec;
5361 5214 #endif
5362 5215
5363 5216 ASSERT(in_hblk_range(hmeblkp, addr));
5364 5217 ASSERT(hmeblkp->hblk_shw_bit == 0);
5365 5218 ASSERT(!hmeblkp->hblk_shared);
5366 5219
5367 5220 #ifdef DEBUG
5368 5221 if (get_hblk_ttesz(hmeblkp) != TTE8K &&
5369 5222 (endaddr < get_hblk_endaddr(hmeblkp))) {
5370 5223 panic("sfmmu_hblk_chgprot: partial chgprot of large page");
5371 5224 }
5372 5225 #endif /* DEBUG */
5373 5226
5374 5227 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp));
5375 5228 ttesz = get_hblk_ttesz(hmeblkp);
5376 5229
5377 5230 pprot = sfmmu_vtop_prot(vprot, &tteflags);
5378 5231 #if defined(SF_ERRATA_57)
5379 5232 check_exec = (sfmmup != ksfmmup) &&
5380 5233 AS_TYPE_64BIT(sfmmup->sfmmu_as) &&
5381 5234 ((vprot & PROT_EXEC) == PROT_EXEC);
5382 5235 #endif
5383 5236 HBLKTOHME(sfhmep, hmeblkp, addr);
5384 5237
5385 5238 /*
5386 5239 * Flush the current demap region if addresses have been
5387 5240 * skipped or the page size doesn't match.
5388 5241 */
5389 5242 use_demap_range = (TTEBYTES(ttesz) == MMU_PAGESIZE);
5390 5243 if (use_demap_range) {
5391 5244 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr);
5392 5245 } else if (dmrp != NULL) {
5393 5246 DEMAP_RANGE_FLUSH(dmrp);
5394 5247 }
5395 5248
5396 5249 while (addr < endaddr) {
5397 5250 sfmmu_copytte(&sfhmep->hme_tte, &tte);
5398 5251 if (TTE_IS_VALID(&tte)) {
5399 5252 if (TTE_GET_LOFLAGS(&tte, tteflags) == pprot) {
5400 5253 /*
5401 5254 * if the new protection is the same as old
5402 5255 * continue
5403 5256 */
5404 5257 goto next_addr;
5405 5258 }
5406 5259 pml = NULL;
5407 5260 pp = sfhmep->hme_page;
5408 5261 if (pp) {
5409 5262 pml = sfmmu_mlist_enter(pp);
5410 5263 }
5411 5264 if (pp != sfhmep->hme_page) {
5412 5265 /*
5413 5266 * tte most have been unloaded
5414 5267 * underneath us. Recheck
5415 5268 */
5416 5269 ASSERT(pml);
5417 5270 sfmmu_mlist_exit(pml);
5418 5271 continue;
5419 5272 }
5420 5273
5421 5274 ASSERT(pp == NULL || sfmmu_mlist_held(pp));
5422 5275
5423 5276 ttemod = tte;
5424 5277 TTE_SET_LOFLAGS(&ttemod, tteflags, pprot);
5425 5278 #if defined(SF_ERRATA_57)
5426 5279 if (check_exec && addr < errata57_limit)
5427 5280 ttemod.tte_exec_perm = 0;
5428 5281 #endif
5429 5282 ret = sfmmu_modifytte_try(&tte, &ttemod,
5430 5283 &sfhmep->hme_tte);
5431 5284
5432 5285 if (ret < 0) {
5433 5286 /* tte changed underneath us */
5434 5287 if (pml) {
5435 5288 sfmmu_mlist_exit(pml);
5436 5289 }
5437 5290 continue;
5438 5291 }
5439 5292
5440 5293 if (tteflags & TTE_HWWR_INT) {
5441 5294 /*
5442 5295 * need to sync if we are clearing modify bit.
5443 5296 */
5444 5297 sfmmu_ttesync(sfmmup, addr, &tte, pp);
5445 5298 }
5446 5299
5447 5300 if (pp && PP_ISRO(pp)) {
5448 5301 if (pprot & TTE_WRPRM_INT) {
5449 5302 pmtx = sfmmu_page_enter(pp);
5450 5303 PP_CLRRO(pp);
5451 5304 sfmmu_page_exit(pmtx);
5452 5305 }
5453 5306 }
5454 5307
5455 5308 if (ret > 0 && use_demap_range) {
5456 5309 DEMAP_RANGE_MARKPG(dmrp, addr);
5457 5310 } else if (ret > 0) {
5458 5311 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0);
5459 5312 }
5460 5313
5461 5314 if (pml) {
5462 5315 sfmmu_mlist_exit(pml);
5463 5316 }
5464 5317 }
5465 5318 next_addr:
5466 5319 addr += TTEBYTES(ttesz);
5467 5320 sfhmep++;
5468 5321 DEMAP_RANGE_NEXTPG(dmrp);
5469 5322 }
5470 5323 return (addr);
5471 5324 }
5472 5325
5473 5326 /*
5474 5327 * This routine is deprecated and should only be used by hat_chgprot.
5475 5328 * The correct routine is sfmmu_vtop_attr.
5476 5329 * This routine converts virtual page protections to physical ones. It will
5477 5330 * update the tteflags field with the tte mask corresponding to the protections
5478 5331 * affected and it returns the new protections. It will also clear the modify
5479 5332 * bit if we are taking away write permission. This is necessary since the
5480 5333 * modify bit is the hardware permission bit and we need to clear it in order
5481 5334 * to detect write faults.
5482 5335 * It accepts the following special protections:
5483 5336 * ~PROT_WRITE = remove write permissions.
5484 5337 * ~PROT_USER = remove user permissions.
5485 5338 */
5486 5339 static uint_t
5487 5340 sfmmu_vtop_prot(uint_t vprot, uint_t *tteflagsp)
5488 5341 {
5489 5342 if (vprot == (uint_t)~PROT_WRITE) {
5490 5343 *tteflagsp = TTE_WRPRM_INT | TTE_HWWR_INT;
5491 5344 return (0); /* will cause wrprm to be cleared */
5492 5345 }
5493 5346 if (vprot == (uint_t)~PROT_USER) {
5494 5347 *tteflagsp = TTE_PRIV_INT;
5495 5348 return (0); /* will cause privprm to be cleared */
5496 5349 }
5497 5350 if ((vprot == 0) || (vprot == PROT_USER) ||
5498 5351 ((vprot & PROT_ALL) != vprot)) {
5499 5352 panic("sfmmu_vtop_prot -- bad prot %x", vprot);
5500 5353 }
5501 5354
5502 5355 switch (vprot) {
5503 5356 case (PROT_READ):
5504 5357 case (PROT_EXEC):
5505 5358 case (PROT_EXEC | PROT_READ):
5506 5359 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT | TTE_HWWR_INT;
5507 5360 return (TTE_PRIV_INT); /* set prv and clr wrt */
5508 5361 case (PROT_WRITE):
5509 5362 case (PROT_WRITE | PROT_READ):
5510 5363 case (PROT_EXEC | PROT_WRITE):
5511 5364 case (PROT_EXEC | PROT_WRITE | PROT_READ):
5512 5365 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT;
5513 5366 return (TTE_PRIV_INT | TTE_WRPRM_INT); /* set prv and wrt */
5514 5367 case (PROT_USER | PROT_READ):
5515 5368 case (PROT_USER | PROT_EXEC):
5516 5369 case (PROT_USER | PROT_EXEC | PROT_READ):
5517 5370 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT | TTE_HWWR_INT;
5518 5371 return (0); /* clr prv and wrt */
5519 5372 case (PROT_USER | PROT_WRITE):
5520 5373 case (PROT_USER | PROT_WRITE | PROT_READ):
5521 5374 case (PROT_USER | PROT_EXEC | PROT_WRITE):
5522 5375 case (PROT_USER | PROT_EXEC | PROT_WRITE | PROT_READ):
5523 5376 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT;
5524 5377 return (TTE_WRPRM_INT); /* clr prv and set wrt */
5525 5378 default:
5526 5379 panic("sfmmu_vtop_prot -- bad prot %x", vprot);
5527 5380 }
5528 5381 return (0);
5529 5382 }
5530 5383
5531 5384 /*
5532 5385 * Alternate unload for very large virtual ranges. With a true 64 bit VA,
5533 5386 * the normal algorithm would take too long for a very large VA range with
5534 5387 * few real mappings. This routine just walks thru all HMEs in the global
5535 5388 * hash table to find and remove mappings.
5536 5389 */
5537 5390 static void
5538 5391 hat_unload_large_virtual(
5539 5392 struct hat *sfmmup,
5540 5393 caddr_t startaddr,
5541 5394 size_t len,
5542 5395 uint_t flags,
5543 5396 hat_callback_t *callback)
5544 5397 {
5545 5398 struct hmehash_bucket *hmebp;
5546 5399 struct hme_blk *hmeblkp;
5547 5400 struct hme_blk *pr_hblk = NULL;
5548 5401 struct hme_blk *nx_hblk;
5549 5402 struct hme_blk *list = NULL;
5550 5403 int i;
5551 5404 demap_range_t dmr, *dmrp;
5552 5405 cpuset_t cpuset;
5553 5406 caddr_t endaddr = startaddr + len;
5554 5407 caddr_t sa;
5555 5408 caddr_t ea;
5556 5409 caddr_t cb_sa[MAX_CB_ADDR];
5557 5410 caddr_t cb_ea[MAX_CB_ADDR];
5558 5411 int addr_cnt = 0;
5559 5412 int a = 0;
5560 5413
5561 5414 if (sfmmup->sfmmu_free) {
5562 5415 dmrp = NULL;
5563 5416 } else {
5564 5417 dmrp = &dmr;
5565 5418 DEMAP_RANGE_INIT(sfmmup, dmrp);
5566 5419 }
5567 5420
5568 5421 /*
5569 5422 * Loop through all the hash buckets of HME blocks looking for matches.
5570 5423 */
5571 5424 for (i = 0; i <= UHMEHASH_SZ; i++) {
5572 5425 hmebp = &uhme_hash[i];
5573 5426 SFMMU_HASH_LOCK(hmebp);
5574 5427 hmeblkp = hmebp->hmeblkp;
5575 5428 pr_hblk = NULL;
5576 5429 while (hmeblkp) {
5577 5430 nx_hblk = hmeblkp->hblk_next;
5578 5431
5579 5432 /*
5580 5433 * skip if not this context, if a shadow block or
5581 5434 * if the mapping is not in the requested range
5582 5435 */
5583 5436 if (hmeblkp->hblk_tag.htag_id != sfmmup ||
5584 5437 hmeblkp->hblk_shw_bit ||
5585 5438 (sa = (caddr_t)get_hblk_base(hmeblkp)) >= endaddr ||
5586 5439 (ea = get_hblk_endaddr(hmeblkp)) <= startaddr) {
5587 5440 pr_hblk = hmeblkp;
5588 5441 goto next_block;
5589 5442 }
5590 5443
5591 5444 ASSERT(!hmeblkp->hblk_shared);
5592 5445 /*
5593 5446 * unload if there are any current valid mappings
5594 5447 */
5595 5448 if (hmeblkp->hblk_vcnt != 0 ||
5596 5449 hmeblkp->hblk_hmecnt != 0)
5597 5450 (void) sfmmu_hblk_unload(sfmmup, hmeblkp,
5598 5451 sa, ea, dmrp, flags);
5599 5452
5600 5453 /*
5601 5454 * on unmap we also release the HME block itself, once
5602 5455 * all mappings are gone.
5603 5456 */
5604 5457 if ((flags & HAT_UNLOAD_UNMAP) != 0 &&
5605 5458 !hmeblkp->hblk_vcnt &&
5606 5459 !hmeblkp->hblk_hmecnt) {
5607 5460 ASSERT(!hmeblkp->hblk_lckcnt);
5608 5461 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
5609 5462 &list, 0);
5610 5463 } else {
5611 5464 pr_hblk = hmeblkp;
5612 5465 }
5613 5466
5614 5467 if (callback == NULL)
5615 5468 goto next_block;
5616 5469
5617 5470 /*
5618 5471 * HME blocks may span more than one page, but we may be
5619 5472 * unmapping only one page, so check for a smaller range
5620 5473 * for the callback
5621 5474 */
5622 5475 if (sa < startaddr)
5623 5476 sa = startaddr;
5624 5477 if (--ea > endaddr)
5625 5478 ea = endaddr - 1;
5626 5479
5627 5480 cb_sa[addr_cnt] = sa;
5628 5481 cb_ea[addr_cnt] = ea;
5629 5482 if (++addr_cnt == MAX_CB_ADDR) {
5630 5483 if (dmrp != NULL) {
5631 5484 DEMAP_RANGE_FLUSH(dmrp);
5632 5485 cpuset = sfmmup->sfmmu_cpusran;
5633 5486 xt_sync(cpuset);
5634 5487 }
5635 5488
5636 5489 for (a = 0; a < MAX_CB_ADDR; ++a) {
5637 5490 callback->hcb_start_addr = cb_sa[a];
5638 5491 callback->hcb_end_addr = cb_ea[a];
5639 5492 callback->hcb_function(callback);
5640 5493 }
5641 5494 addr_cnt = 0;
5642 5495 }
5643 5496
5644 5497 next_block:
5645 5498 hmeblkp = nx_hblk;
5646 5499 }
5647 5500 SFMMU_HASH_UNLOCK(hmebp);
5648 5501 }
5649 5502
5650 5503 sfmmu_hblks_list_purge(&list, 0);
5651 5504 if (dmrp != NULL) {
5652 5505 DEMAP_RANGE_FLUSH(dmrp);
5653 5506 cpuset = sfmmup->sfmmu_cpusran;
5654 5507 xt_sync(cpuset);
5655 5508 }
5656 5509
5657 5510 for (a = 0; a < addr_cnt; ++a) {
5658 5511 callback->hcb_start_addr = cb_sa[a];
5659 5512 callback->hcb_end_addr = cb_ea[a];
5660 5513 callback->hcb_function(callback);
5661 5514 }
5662 5515
5663 5516 /*
5664 5517 * Check TSB and TLB page sizes if the process isn't exiting.
5665 5518 */
5666 5519 if (!sfmmup->sfmmu_free)
5667 5520 sfmmu_check_page_sizes(sfmmup, 0);
5668 5521 }
5669 5522
5670 5523 /*
5671 5524 * Unload all the mappings in the range [addr..addr+len). addr and len must
5672 5525 * be MMU_PAGESIZE aligned.
5673 5526 */
5674 5527
5675 5528 extern struct seg *segkmap;
5676 5529 #define ISSEGKMAP(sfmmup, addr) (sfmmup == ksfmmup && \
5677 5530 segkmap->s_base <= (addr) && (addr) < (segkmap->s_base + segkmap->s_size))
5678 5531
5679 5532
5680 5533 void
5681 5534 hat_unload_callback(
5682 5535 struct hat *sfmmup,
5683 5536 caddr_t addr,
5684 5537 size_t len,
5685 5538 uint_t flags,
5686 5539 hat_callback_t *callback)
5687 5540 {
5688 5541 struct hmehash_bucket *hmebp;
5689 5542 hmeblk_tag hblktag;
5690 5543 int hmeshift, hashno, iskernel;
5691 5544 struct hme_blk *hmeblkp, *pr_hblk, *list = NULL;
5692 5545 caddr_t endaddr;
5693 5546 cpuset_t cpuset;
5694 5547 int addr_count = 0;
5695 5548 int a;
5696 5549 caddr_t cb_start_addr[MAX_CB_ADDR];
5697 5550 caddr_t cb_end_addr[MAX_CB_ADDR];
5698 5551 int issegkmap = ISSEGKMAP(sfmmup, addr);
5699 5552 demap_range_t dmr, *dmrp;
5700 5553
5701 5554 if (sfmmup->sfmmu_xhat_provider) {
5702 5555 XHAT_UNLOAD_CALLBACK(sfmmup, addr, len, flags, callback);
5703 5556 return;
5704 5557 } else {
5705 5558 /*
5706 5559 * This must be a CPU HAT. If the address space has
5707 5560 * XHATs attached, unload the mappings for all of them,
5708 5561 * just in case
5709 5562 */
5710 5563 ASSERT(sfmmup->sfmmu_as != NULL);
5711 5564 if (sfmmup->sfmmu_as->a_xhat != NULL)
5712 5565 xhat_unload_callback_all(sfmmup->sfmmu_as, addr,
5713 5566 len, flags, callback);
5714 5567 }
5715 5568
5716 5569 ASSERT((sfmmup == ksfmmup) || (flags & HAT_UNLOAD_OTHER) || \
5717 5570 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
5718 5571
5719 5572 ASSERT(sfmmup != NULL);
5720 5573 ASSERT((len & MMU_PAGEOFFSET) == 0);
5721 5574 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET));
5722 5575
5723 5576 /*
5724 5577 * Probing through a large VA range (say 63 bits) will be slow, even
5725 5578 * at 4 Meg steps between the probes. So, when the virtual address range
5726 5579 * is very large, search the HME entries for what to unload.
5727 5580 *
5728 5581 * len >> TTE_PAGE_SHIFT(TTE4M) is the # of 4Meg probes we'd need
5729 5582 *
5730 5583 * UHMEHASH_SZ is number of hash buckets to examine
5731 5584 *
5732 5585 */
5733 5586 if (sfmmup != KHATID && (len >> TTE_PAGE_SHIFT(TTE4M)) > UHMEHASH_SZ) {
5734 5587 hat_unload_large_virtual(sfmmup, addr, len, flags, callback);
5735 5588 return;
5736 5589 }
5737 5590
5738 5591 CPUSET_ZERO(cpuset);
5739 5592
5740 5593 /*
5741 5594 * If the process is exiting, we can save a lot of fuss since
5742 5595 * we'll flush the TLB when we free the ctx anyway.
5743 5596 */
5744 5597 if (sfmmup->sfmmu_free) {
5745 5598 dmrp = NULL;
5746 5599 } else {
5747 5600 dmrp = &dmr;
5748 5601 DEMAP_RANGE_INIT(sfmmup, dmrp);
5749 5602 }
5750 5603
5751 5604 endaddr = addr + len;
5752 5605 hblktag.htag_id = sfmmup;
5753 5606 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
5754 5607
5755 5608 /*
5756 5609 * It is likely for the vm to call unload over a wide range of
5757 5610 * addresses that are actually very sparsely populated by
5758 5611 * translations. In order to speed this up the sfmmu hat supports
5759 5612 * the concept of shadow hmeblks. Dummy large page hmeblks that
5760 5613 * correspond to actual small translations are allocated at tteload
5761 5614 * time and are referred to as shadow hmeblks. Now, during unload
5762 5615 * time, we first check if we have a shadow hmeblk for that
5763 5616 * translation. The absence of one means the corresponding address
5764 5617 * range is empty and can be skipped.
5765 5618 *
5766 5619 * The kernel is an exception to above statement and that is why
5767 5620 * we don't use shadow hmeblks and hash starting from the smallest
5768 5621 * page size.
5769 5622 */
5770 5623 if (sfmmup == KHATID) {
5771 5624 iskernel = 1;
5772 5625 hashno = TTE64K;
5773 5626 } else {
5774 5627 iskernel = 0;
5775 5628 if (mmu_page_sizes == max_mmu_page_sizes) {
5776 5629 hashno = TTE256M;
5777 5630 } else {
5778 5631 hashno = TTE4M;
5779 5632 }
5780 5633 }
5781 5634 while (addr < endaddr) {
5782 5635 hmeshift = HME_HASH_SHIFT(hashno);
5783 5636 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
5784 5637 hblktag.htag_rehash = hashno;
5785 5638 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
5786 5639
5787 5640 SFMMU_HASH_LOCK(hmebp);
5788 5641
5789 5642 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, pr_hblk, &list);
5790 5643 if (hmeblkp == NULL) {
5791 5644 /*
5792 5645 * didn't find an hmeblk. skip the appropiate
5793 5646 * address range.
5794 5647 */
5795 5648 SFMMU_HASH_UNLOCK(hmebp);
5796 5649 if (iskernel) {
5797 5650 if (hashno < mmu_hashcnt) {
5798 5651 hashno++;
5799 5652 continue;
5800 5653 } else {
5801 5654 hashno = TTE64K;
5802 5655 addr = (caddr_t)roundup((uintptr_t)addr
5803 5656 + 1, MMU_PAGESIZE64K);
5804 5657 continue;
5805 5658 }
5806 5659 }
5807 5660 addr = (caddr_t)roundup((uintptr_t)addr + 1,
5808 5661 (1 << hmeshift));
5809 5662 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) {
5810 5663 ASSERT(hashno == TTE64K);
5811 5664 continue;
5812 5665 }
5813 5666 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) {
5814 5667 hashno = TTE512K;
5815 5668 continue;
5816 5669 }
5817 5670 if (mmu_page_sizes == max_mmu_page_sizes) {
5818 5671 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) {
5819 5672 hashno = TTE4M;
5820 5673 continue;
5821 5674 }
5822 5675 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) {
5823 5676 hashno = TTE32M;
5824 5677 continue;
5825 5678 }
5826 5679 hashno = TTE256M;
5827 5680 continue;
5828 5681 } else {
5829 5682 hashno = TTE4M;
5830 5683 continue;
5831 5684 }
5832 5685 }
5833 5686 ASSERT(hmeblkp);
5834 5687 ASSERT(!hmeblkp->hblk_shared);
5835 5688 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) {
5836 5689 /*
5837 5690 * If the valid count is zero we can skip the range
5838 5691 * mapped by this hmeblk.
5839 5692 * We free hblks in the case of HAT_UNMAP. HAT_UNMAP
5840 5693 * is used by segment drivers as a hint
5841 5694 * that the mapping resource won't be used any longer.
5842 5695 * The best example of this is during exit().
5843 5696 */
5844 5697 addr = (caddr_t)roundup((uintptr_t)addr + 1,
5845 5698 get_hblk_span(hmeblkp));
5846 5699 if ((flags & HAT_UNLOAD_UNMAP) ||
5847 5700 (iskernel && !issegkmap)) {
5848 5701 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
5849 5702 &list, 0);
5850 5703 }
5851 5704 SFMMU_HASH_UNLOCK(hmebp);
5852 5705
5853 5706 if (iskernel) {
5854 5707 hashno = TTE64K;
5855 5708 continue;
5856 5709 }
5857 5710 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) {
5858 5711 ASSERT(hashno == TTE64K);
5859 5712 continue;
5860 5713 }
5861 5714 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) {
5862 5715 hashno = TTE512K;
5863 5716 continue;
5864 5717 }
5865 5718 if (mmu_page_sizes == max_mmu_page_sizes) {
5866 5719 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) {
5867 5720 hashno = TTE4M;
5868 5721 continue;
5869 5722 }
5870 5723 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) {
5871 5724 hashno = TTE32M;
5872 5725 continue;
5873 5726 }
5874 5727 hashno = TTE256M;
5875 5728 continue;
5876 5729 } else {
5877 5730 hashno = TTE4M;
5878 5731 continue;
5879 5732 }
5880 5733 }
5881 5734 if (hmeblkp->hblk_shw_bit) {
5882 5735 /*
5883 5736 * If we encounter a shadow hmeblk we know there is
5884 5737 * smaller sized hmeblks mapping the same address space.
5885 5738 * Decrement the hash size and rehash.
5886 5739 */
5887 5740 ASSERT(sfmmup != KHATID);
5888 5741 hashno--;
5889 5742 SFMMU_HASH_UNLOCK(hmebp);
5890 5743 continue;
5891 5744 }
5892 5745
5893 5746 /*
5894 5747 * track callback address ranges.
5895 5748 * only start a new range when it's not contiguous
5896 5749 */
5897 5750 if (callback != NULL) {
5898 5751 if (addr_count > 0 &&
5899 5752 addr == cb_end_addr[addr_count - 1])
5900 5753 --addr_count;
5901 5754 else
5902 5755 cb_start_addr[addr_count] = addr;
5903 5756 }
5904 5757
5905 5758 addr = sfmmu_hblk_unload(sfmmup, hmeblkp, addr, endaddr,
5906 5759 dmrp, flags);
5907 5760
5908 5761 if (callback != NULL)
5909 5762 cb_end_addr[addr_count++] = addr;
5910 5763
5911 5764 if (((flags & HAT_UNLOAD_UNMAP) || (iskernel && !issegkmap)) &&
5912 5765 !hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) {
5913 5766 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk, &list, 0);
5914 5767 }
5915 5768 SFMMU_HASH_UNLOCK(hmebp);
5916 5769
5917 5770 /*
5918 5771 * Notify our caller as to exactly which pages
5919 5772 * have been unloaded. We do these in clumps,
5920 5773 * to minimize the number of xt_sync()s that need to occur.
5921 5774 */
5922 5775 if (callback != NULL && addr_count == MAX_CB_ADDR) {
5923 5776 if (dmrp != NULL) {
5924 5777 DEMAP_RANGE_FLUSH(dmrp);
5925 5778 cpuset = sfmmup->sfmmu_cpusran;
5926 5779 xt_sync(cpuset);
5927 5780 }
5928 5781
5929 5782 for (a = 0; a < MAX_CB_ADDR; ++a) {
5930 5783 callback->hcb_start_addr = cb_start_addr[a];
5931 5784 callback->hcb_end_addr = cb_end_addr[a];
5932 5785 callback->hcb_function(callback);
5933 5786 }
5934 5787 addr_count = 0;
5935 5788 }
5936 5789 if (iskernel) {
5937 5790 hashno = TTE64K;
5938 5791 continue;
5939 5792 }
5940 5793 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) {
5941 5794 ASSERT(hashno == TTE64K);
5942 5795 continue;
5943 5796 }
5944 5797 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) {
5945 5798 hashno = TTE512K;
5946 5799 continue;
5947 5800 }
5948 5801 if (mmu_page_sizes == max_mmu_page_sizes) {
5949 5802 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) {
5950 5803 hashno = TTE4M;
5951 5804 continue;
5952 5805 }
5953 5806 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) {
5954 5807 hashno = TTE32M;
5955 5808 continue;
5956 5809 }
5957 5810 hashno = TTE256M;
5958 5811 } else {
5959 5812 hashno = TTE4M;
5960 5813 }
5961 5814 }
5962 5815
5963 5816 sfmmu_hblks_list_purge(&list, 0);
5964 5817 if (dmrp != NULL) {
5965 5818 DEMAP_RANGE_FLUSH(dmrp);
5966 5819 cpuset = sfmmup->sfmmu_cpusran;
5967 5820 xt_sync(cpuset);
5968 5821 }
5969 5822 if (callback && addr_count != 0) {
5970 5823 for (a = 0; a < addr_count; ++a) {
5971 5824 callback->hcb_start_addr = cb_start_addr[a];
5972 5825 callback->hcb_end_addr = cb_end_addr[a];
5973 5826 callback->hcb_function(callback);
5974 5827 }
5975 5828 }
5976 5829
5977 5830 /*
5978 5831 * Check TSB and TLB page sizes if the process isn't exiting.
5979 5832 */
5980 5833 if (!sfmmup->sfmmu_free)
5981 5834 sfmmu_check_page_sizes(sfmmup, 0);
5982 5835 }
5983 5836
5984 5837 /*
5985 5838 * Unload all the mappings in the range [addr..addr+len). addr and len must
5986 5839 * be MMU_PAGESIZE aligned.
5987 5840 */
5988 5841 void
5989 5842 hat_unload(struct hat *sfmmup, caddr_t addr, size_t len, uint_t flags)
5990 5843 {
5991 5844 if (sfmmup->sfmmu_xhat_provider) {
5992 5845 XHAT_UNLOAD(sfmmup, addr, len, flags);
5993 5846 return;
5994 5847 }
5995 5848 hat_unload_callback(sfmmup, addr, len, flags, NULL);
5996 5849 }
5997 5850
5998 5851
5999 5852 /*
6000 5853 * Find the largest mapping size for this page.
6001 5854 */
6002 5855 int
6003 5856 fnd_mapping_sz(page_t *pp)
6004 5857 {
6005 5858 int sz;
6006 5859 int p_index;
6007 5860
6008 5861 p_index = PP_MAPINDEX(pp);
6009 5862
6010 5863 sz = 0;
6011 5864 p_index >>= 1; /* don't care about 8K bit */
6012 5865 for (; p_index; p_index >>= 1) {
6013 5866 sz++;
6014 5867 }
6015 5868
6016 5869 return (sz);
6017 5870 }
6018 5871
6019 5872 /*
6020 5873 * This function unloads a range of addresses for an hmeblk.
6021 5874 * It returns the next address to be unloaded.
6022 5875 * It should be called with the hash lock held.
6023 5876 */
6024 5877 static caddr_t
6025 5878 sfmmu_hblk_unload(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr,
6026 5879 caddr_t endaddr, demap_range_t *dmrp, uint_t flags)
6027 5880 {
6028 5881 tte_t tte, ttemod;
6029 5882 struct sf_hment *sfhmep;
6030 5883 int ttesz;
6031 5884 long ttecnt;
6032 5885 page_t *pp;
6033 5886 kmutex_t *pml;
6034 5887 int ret;
6035 5888 int use_demap_range;
6036 5889
6037 5890 ASSERT(in_hblk_range(hmeblkp, addr));
6038 5891 ASSERT(!hmeblkp->hblk_shw_bit);
6039 5892 ASSERT(sfmmup != NULL || hmeblkp->hblk_shared);
6040 5893 ASSERT(sfmmup == NULL || !hmeblkp->hblk_shared);
6041 5894 ASSERT(dmrp == NULL || !hmeblkp->hblk_shared);
6042 5895
6043 5896 #ifdef DEBUG
6044 5897 if (get_hblk_ttesz(hmeblkp) != TTE8K &&
6045 5898 (endaddr < get_hblk_endaddr(hmeblkp))) {
6046 5899 panic("sfmmu_hblk_unload: partial unload of large page");
6047 5900 }
6048 5901 #endif /* DEBUG */
6049 5902
6050 5903 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp));
6051 5904 ttesz = get_hblk_ttesz(hmeblkp);
6052 5905
6053 5906 use_demap_range = ((dmrp == NULL) ||
6054 5907 (TTEBYTES(ttesz) == DEMAP_RANGE_PGSZ(dmrp)));
6055 5908
6056 5909 if (use_demap_range) {
6057 5910 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr);
6058 5911 } else if (dmrp != NULL) {
6059 5912 DEMAP_RANGE_FLUSH(dmrp);
6060 5913 }
6061 5914 ttecnt = 0;
6062 5915 HBLKTOHME(sfhmep, hmeblkp, addr);
6063 5916
6064 5917 while (addr < endaddr) {
6065 5918 pml = NULL;
6066 5919 sfmmu_copytte(&sfhmep->hme_tte, &tte);
6067 5920 if (TTE_IS_VALID(&tte)) {
6068 5921 pp = sfhmep->hme_page;
6069 5922 if (pp != NULL) {
6070 5923 pml = sfmmu_mlist_enter(pp);
6071 5924 }
6072 5925
6073 5926 /*
6074 5927 * Verify if hme still points to 'pp' now that
6075 5928 * we have p_mapping lock.
6076 5929 */
6077 5930 if (sfhmep->hme_page != pp) {
6078 5931 if (pp != NULL && sfhmep->hme_page != NULL) {
6079 5932 ASSERT(pml != NULL);
6080 5933 sfmmu_mlist_exit(pml);
6081 5934 /* Re-start this iteration. */
6082 5935 continue;
6083 5936 }
6084 5937 ASSERT((pp != NULL) &&
6085 5938 (sfhmep->hme_page == NULL));
6086 5939 goto tte_unloaded;
6087 5940 }
6088 5941
6089 5942 /*
6090 5943 * This point on we have both HASH and p_mapping
6091 5944 * lock.
6092 5945 */
6093 5946 ASSERT(pp == sfhmep->hme_page);
6094 5947 ASSERT(pp == NULL || sfmmu_mlist_held(pp));
6095 5948
6096 5949 /*
6097 5950 * We need to loop on modify tte because it is
6098 5951 * possible for pagesync to come along and
6099 5952 * change the software bits beneath us.
6100 5953 *
6101 5954 * Page_unload can also invalidate the tte after
6102 5955 * we read tte outside of p_mapping lock.
6103 5956 */
6104 5957 again:
6105 5958 ttemod = tte;
6106 5959
6107 5960 TTE_SET_INVALID(&ttemod);
6108 5961 ret = sfmmu_modifytte_try(&tte, &ttemod,
6109 5962 &sfhmep->hme_tte);
6110 5963
6111 5964 if (ret <= 0) {
6112 5965 if (TTE_IS_VALID(&tte)) {
6113 5966 ASSERT(ret < 0);
6114 5967 goto again;
6115 5968 }
6116 5969 if (pp != NULL) {
6117 5970 panic("sfmmu_hblk_unload: pp = 0x%p "
6118 5971 "tte became invalid under mlist"
6119 5972 " lock = 0x%p", (void *)pp,
6120 5973 (void *)pml);
6121 5974 }
6122 5975 continue;
6123 5976 }
6124 5977
6125 5978 if (!(flags & HAT_UNLOAD_NOSYNC)) {
6126 5979 sfmmu_ttesync(sfmmup, addr, &tte, pp);
6127 5980 }
6128 5981
6129 5982 /*
6130 5983 * Ok- we invalidated the tte. Do the rest of the job.
6131 5984 */
6132 5985 ttecnt++;
6133 5986
6134 5987 if (flags & HAT_UNLOAD_UNLOCK) {
6135 5988 ASSERT(hmeblkp->hblk_lckcnt > 0);
6136 5989 atomic_add_32(&hmeblkp->hblk_lckcnt, -1);
6137 5990 HBLK_STACK_TRACE(hmeblkp, HBLK_UNLOCK);
6138 5991 }
6139 5992
6140 5993 /*
6141 5994 * Normally we would need to flush the page
6142 5995 * from the virtual cache at this point in
6143 5996 * order to prevent a potential cache alias
6144 5997 * inconsistency.
6145 5998 * The particular scenario we need to worry
6146 5999 * about is:
6147 6000 * Given: va1 and va2 are two virtual address
6148 6001 * that alias and map the same physical
6149 6002 * address.
6150 6003 * 1. mapping exists from va1 to pa and data
6151 6004 * has been read into the cache.
6152 6005 * 2. unload va1.
6153 6006 * 3. load va2 and modify data using va2.
6154 6007 * 4 unload va2.
6155 6008 * 5. load va1 and reference data. Unless we
6156 6009 * flush the data cache when we unload we will
6157 6010 * get stale data.
6158 6011 * Fortunately, page coloring eliminates the
6159 6012 * above scenario by remembering the color a
6160 6013 * physical page was last or is currently
6161 6014 * mapped to. Now, we delay the flush until
6162 6015 * the loading of translations. Only when the
6163 6016 * new translation is of a different color
6164 6017 * are we forced to flush.
6165 6018 */
6166 6019 if (use_demap_range) {
6167 6020 /*
6168 6021 * Mark this page as needing a demap.
6169 6022 */
6170 6023 DEMAP_RANGE_MARKPG(dmrp, addr);
6171 6024 } else {
6172 6025 ASSERT(sfmmup != NULL);
6173 6026 ASSERT(!hmeblkp->hblk_shared);
6174 6027 sfmmu_tlb_demap(addr, sfmmup, hmeblkp,
6175 6028 sfmmup->sfmmu_free, 0);
6176 6029 }
6177 6030
6178 6031 if (pp) {
6179 6032 /*
6180 6033 * Remove the hment from the mapping list
6181 6034 */
6182 6035 ASSERT(hmeblkp->hblk_hmecnt > 0);
6183 6036
6184 6037 /*
6185 6038 * Again, we cannot
6186 6039 * ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS);
6187 6040 */
6188 6041 HME_SUB(sfhmep, pp);
6189 6042 membar_stst();
6190 6043 atomic_add_16(&hmeblkp->hblk_hmecnt, -1);
6191 6044 }
6192 6045
6193 6046 ASSERT(hmeblkp->hblk_vcnt > 0);
6194 6047 atomic_add_16(&hmeblkp->hblk_vcnt, -1);
6195 6048
6196 6049 ASSERT(hmeblkp->hblk_hmecnt || hmeblkp->hblk_vcnt ||
6197 6050 !hmeblkp->hblk_lckcnt);
6198 6051
6199 6052 #ifdef VAC
6200 6053 if (pp && (pp->p_nrm & (P_KPMC | P_KPMS | P_TNC))) {
6201 6054 if (PP_ISTNC(pp)) {
6202 6055 /*
6203 6056 * If page was temporary
6204 6057 * uncached, try to recache
6205 6058 * it. Note that HME_SUB() was
6206 6059 * called above so p_index and
6207 6060 * mlist had been updated.
6208 6061 */
6209 6062 conv_tnc(pp, ttesz);
6210 6063 } else if (pp->p_mapping == NULL) {
6211 6064 ASSERT(kpm_enable);
6212 6065 /*
6213 6066 * Page is marked to be in VAC conflict
6214 6067 * to an existing kpm mapping and/or is
6215 6068 * kpm mapped using only the regular
6216 6069 * pagesize.
6217 6070 */
6218 6071 sfmmu_kpm_hme_unload(pp);
6219 6072 }
6220 6073 }
6221 6074 #endif /* VAC */
6222 6075 } else if ((pp = sfhmep->hme_page) != NULL) {
6223 6076 /*
6224 6077 * TTE is invalid but the hme
6225 6078 * still exists. let pageunload
6226 6079 * complete its job.
6227 6080 */
6228 6081 ASSERT(pml == NULL);
6229 6082 pml = sfmmu_mlist_enter(pp);
6230 6083 if (sfhmep->hme_page != NULL) {
6231 6084 sfmmu_mlist_exit(pml);
6232 6085 continue;
6233 6086 }
6234 6087 ASSERT(sfhmep->hme_page == NULL);
6235 6088 } else if (hmeblkp->hblk_hmecnt != 0) {
6236 6089 /*
6237 6090 * pageunload may have not finished decrementing
6238 6091 * hblk_vcnt and hblk_hmecnt. Find page_t if any and
6239 6092 * wait for pageunload to finish. Rely on pageunload
6240 6093 * to decrement hblk_hmecnt after hblk_vcnt.
6241 6094 */
6242 6095 pfn_t pfn = TTE_TO_TTEPFN(&tte);
6243 6096 ASSERT(pml == NULL);
6244 6097 if (pf_is_memory(pfn)) {
6245 6098 pp = page_numtopp_nolock(pfn);
6246 6099 if (pp != NULL) {
6247 6100 pml = sfmmu_mlist_enter(pp);
6248 6101 sfmmu_mlist_exit(pml);
6249 6102 pml = NULL;
6250 6103 }
6251 6104 }
6252 6105 }
6253 6106
6254 6107 tte_unloaded:
6255 6108 /*
6256 6109 * At this point, the tte we are looking at
6257 6110 * should be unloaded, and hme has been unlinked
6258 6111 * from page too. This is important because in
6259 6112 * pageunload, it does ttesync() then HME_SUB.
6260 6113 * We need to make sure HME_SUB has been completed
6261 6114 * so we know ttesync() has been completed. Otherwise,
6262 6115 * at exit time, after return from hat layer, VM will
6263 6116 * release as structure which hat_setstat() (called
6264 6117 * by ttesync()) needs.
6265 6118 */
6266 6119 #ifdef DEBUG
6267 6120 {
6268 6121 tte_t dtte;
6269 6122
6270 6123 ASSERT(sfhmep->hme_page == NULL);
6271 6124
6272 6125 sfmmu_copytte(&sfhmep->hme_tte, &dtte);
6273 6126 ASSERT(!TTE_IS_VALID(&dtte));
6274 6127 }
6275 6128 #endif
6276 6129
6277 6130 if (pml) {
6278 6131 sfmmu_mlist_exit(pml);
6279 6132 }
6280 6133
6281 6134 addr += TTEBYTES(ttesz);
6282 6135 sfhmep++;
6283 6136 DEMAP_RANGE_NEXTPG(dmrp);
6284 6137 }
6285 6138 /*
6286 6139 * For shared hmeblks this routine is only called when region is freed
6287 6140 * and no longer referenced. So no need to decrement ttecnt
6288 6141 * in the region structure here.
6289 6142 */
6290 6143 if (ttecnt > 0 && sfmmup != NULL) {
6291 6144 atomic_add_long(&sfmmup->sfmmu_ttecnt[ttesz], -ttecnt);
6292 6145 }
6293 6146 return (addr);
6294 6147 }
6295 6148
6296 6149 /*
6297 6150 * Invalidate a virtual address range for the local CPU.
6298 6151 * For best performance ensure that the va range is completely
6299 6152 * mapped, otherwise the entire TLB will be flushed.
6300 6153 */
6301 6154 void
6302 6155 hat_flush_range(struct hat *sfmmup, caddr_t va, size_t size)
6303 6156 {
6304 6157 ssize_t sz;
6305 6158 caddr_t endva = va + size;
6306 6159
6307 6160 while (va < endva) {
6308 6161 sz = hat_getpagesize(sfmmup, va);
6309 6162 if (sz < 0) {
6310 6163 vtag_flushall();
6311 6164 break;
6312 6165 }
6313 6166 vtag_flushpage(va, (uint64_t)sfmmup);
6314 6167 va += sz;
6315 6168 }
6316 6169 }
6317 6170
6318 6171 /*
6319 6172 * Synchronize all the mappings in the range [addr..addr+len).
6320 6173 * Can be called with clearflag having two states:
6321 6174 * HAT_SYNC_DONTZERO means just return the rm stats
6322 6175 * HAT_SYNC_ZERORM means zero rm bits in the tte and return the stats
6323 6176 */
6324 6177 void
6325 6178 hat_sync(struct hat *sfmmup, caddr_t addr, size_t len, uint_t clearflag)
6326 6179 {
6327 6180 struct hmehash_bucket *hmebp;
6328 6181 hmeblk_tag hblktag;
6329 6182 int hmeshift, hashno = 1;
6330 6183 struct hme_blk *hmeblkp, *list = NULL;
6331 6184 caddr_t endaddr;
6332 6185 cpuset_t cpuset;
6333 6186
6334 6187 ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
6335 6188 ASSERT((sfmmup == ksfmmup) ||
6336 6189 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
6337 6190 ASSERT((len & MMU_PAGEOFFSET) == 0);
6338 6191 ASSERT((clearflag == HAT_SYNC_DONTZERO) ||
6339 6192 (clearflag == HAT_SYNC_ZERORM));
6340 6193
6341 6194 CPUSET_ZERO(cpuset);
6342 6195
6343 6196 endaddr = addr + len;
6344 6197 hblktag.htag_id = sfmmup;
6345 6198 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
6346 6199
6347 6200 /*
6348 6201 * Spitfire supports 4 page sizes.
6349 6202 * Most pages are expected to be of the smallest page
6350 6203 * size (8K) and these will not need to be rehashed. 64K
6351 6204 * pages also don't need to be rehashed because the an hmeblk
6352 6205 * spans 64K of address space. 512K pages might need 1 rehash and
6353 6206 * and 4M pages 2 rehashes.
6354 6207 */
6355 6208 while (addr < endaddr) {
6356 6209 hmeshift = HME_HASH_SHIFT(hashno);
6357 6210 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
6358 6211 hblktag.htag_rehash = hashno;
6359 6212 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
6360 6213
6361 6214 SFMMU_HASH_LOCK(hmebp);
6362 6215
6363 6216 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list);
6364 6217 if (hmeblkp != NULL) {
6365 6218 ASSERT(!hmeblkp->hblk_shared);
6366 6219 /*
6367 6220 * We've encountered a shadow hmeblk so skip the range
6368 6221 * of the next smaller mapping size.
6369 6222 */
6370 6223 if (hmeblkp->hblk_shw_bit) {
6371 6224 ASSERT(sfmmup != ksfmmup);
6372 6225 ASSERT(hashno > 1);
6373 6226 addr = (caddr_t)P2END((uintptr_t)addr,
6374 6227 TTEBYTES(hashno - 1));
6375 6228 } else {
6376 6229 addr = sfmmu_hblk_sync(sfmmup, hmeblkp,
6377 6230 addr, endaddr, clearflag);
6378 6231 }
6379 6232 SFMMU_HASH_UNLOCK(hmebp);
6380 6233 hashno = 1;
6381 6234 continue;
6382 6235 }
6383 6236 SFMMU_HASH_UNLOCK(hmebp);
6384 6237
6385 6238 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) {
6386 6239 /*
6387 6240 * We have traversed the whole list and rehashed
6388 6241 * if necessary without finding the address to sync.
6389 6242 * This is ok so we increment the address by the
6390 6243 * smallest hmeblk range for kernel mappings and the
6391 6244 * largest hmeblk range, to account for shadow hmeblks,
6392 6245 * for user mappings and continue.
6393 6246 */
6394 6247 if (sfmmup == ksfmmup)
6395 6248 addr = (caddr_t)P2END((uintptr_t)addr,
6396 6249 TTEBYTES(1));
6397 6250 else
6398 6251 addr = (caddr_t)P2END((uintptr_t)addr,
6399 6252 TTEBYTES(hashno));
6400 6253 hashno = 1;
6401 6254 } else {
6402 6255 hashno++;
6403 6256 }
6404 6257 }
6405 6258 sfmmu_hblks_list_purge(&list, 0);
6406 6259 cpuset = sfmmup->sfmmu_cpusran;
6407 6260 xt_sync(cpuset);
6408 6261 }
6409 6262
6410 6263 static caddr_t
6411 6264 sfmmu_hblk_sync(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr,
6412 6265 caddr_t endaddr, int clearflag)
6413 6266 {
6414 6267 tte_t tte, ttemod;
6415 6268 struct sf_hment *sfhmep;
6416 6269 int ttesz;
6417 6270 struct page *pp;
6418 6271 kmutex_t *pml;
6419 6272 int ret;
6420 6273
6421 6274 ASSERT(hmeblkp->hblk_shw_bit == 0);
6422 6275 ASSERT(!hmeblkp->hblk_shared);
6423 6276
6424 6277 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp));
6425 6278
6426 6279 ttesz = get_hblk_ttesz(hmeblkp);
6427 6280 HBLKTOHME(sfhmep, hmeblkp, addr);
6428 6281
6429 6282 while (addr < endaddr) {
6430 6283 sfmmu_copytte(&sfhmep->hme_tte, &tte);
6431 6284 if (TTE_IS_VALID(&tte)) {
6432 6285 pml = NULL;
6433 6286 pp = sfhmep->hme_page;
6434 6287 if (pp) {
6435 6288 pml = sfmmu_mlist_enter(pp);
6436 6289 }
6437 6290 if (pp != sfhmep->hme_page) {
6438 6291 /*
6439 6292 * tte most have been unloaded
6440 6293 * underneath us. Recheck
6441 6294 */
6442 6295 ASSERT(pml);
6443 6296 sfmmu_mlist_exit(pml);
6444 6297 continue;
6445 6298 }
6446 6299
6447 6300 ASSERT(pp == NULL || sfmmu_mlist_held(pp));
6448 6301
6449 6302 if (clearflag == HAT_SYNC_ZERORM) {
6450 6303 ttemod = tte;
6451 6304 TTE_CLR_RM(&ttemod);
6452 6305 ret = sfmmu_modifytte_try(&tte, &ttemod,
6453 6306 &sfhmep->hme_tte);
6454 6307 if (ret < 0) {
6455 6308 if (pml) {
6456 6309 sfmmu_mlist_exit(pml);
6457 6310 }
6458 6311 continue;
6459 6312 }
6460 6313
6461 6314 if (ret > 0) {
6462 6315 sfmmu_tlb_demap(addr, sfmmup,
6463 6316 hmeblkp, 0, 0);
6464 6317 }
6465 6318 }
6466 6319 sfmmu_ttesync(sfmmup, addr, &tte, pp);
6467 6320 if (pml) {
6468 6321 sfmmu_mlist_exit(pml);
6469 6322 }
6470 6323 }
6471 6324 addr += TTEBYTES(ttesz);
6472 6325 sfhmep++;
6473 6326 }
6474 6327 return (addr);
6475 6328 }
6476 6329
6477 6330 /*
6478 6331 * This function will sync a tte to the page struct and it will
6479 6332 * update the hat stats. Currently it allows us to pass a NULL pp
6480 6333 * and we will simply update the stats. We may want to change this
6481 6334 * so we only keep stats for pages backed by pp's.
6482 6335 */
6483 6336 static void
6484 6337 sfmmu_ttesync(struct hat *sfmmup, caddr_t addr, tte_t *ttep, page_t *pp)
6485 6338 {
6486 6339 uint_t rm = 0;
6487 6340 int sz;
6488 6341 pgcnt_t npgs;
6489 6342
6490 6343 ASSERT(TTE_IS_VALID(ttep));
6491 6344
6492 6345 if (TTE_IS_NOSYNC(ttep)) {
6493 6346 return;
6494 6347 }
6495 6348
6496 6349 if (TTE_IS_REF(ttep)) {
6497 6350 rm = P_REF;
6498 6351 }
6499 6352 if (TTE_IS_MOD(ttep)) {
6500 6353 rm |= P_MOD;
6501 6354 }
6502 6355
6503 6356 if (rm == 0) {
6504 6357 return;
6505 6358 }
6506 6359
6507 6360 sz = TTE_CSZ(ttep);
6508 6361 if (sfmmup != NULL && sfmmup->sfmmu_rmstat) {
6509 6362 int i;
6510 6363 caddr_t vaddr = addr;
6511 6364
6512 6365 for (i = 0; i < TTEPAGES(sz); i++, vaddr += MMU_PAGESIZE) {
6513 6366 hat_setstat(sfmmup->sfmmu_as, vaddr, MMU_PAGESIZE, rm);
6514 6367 }
6515 6368
6516 6369 }
6517 6370
6518 6371 /*
6519 6372 * XXX I want to use cas to update nrm bits but they
6520 6373 * currently belong in common/vm and not in hat where
6521 6374 * they should be.
6522 6375 * The nrm bits are protected by the same mutex as
6523 6376 * the one that protects the page's mapping list.
6524 6377 */
6525 6378 if (!pp)
6526 6379 return;
6527 6380 ASSERT(sfmmu_mlist_held(pp));
6528 6381 /*
6529 6382 * If the tte is for a large page, we need to sync all the
6530 6383 * pages covered by the tte.
6531 6384 */
6532 6385 if (sz != TTE8K) {
6533 6386 ASSERT(pp->p_szc != 0);
6534 6387 pp = PP_GROUPLEADER(pp, sz);
6535 6388 ASSERT(sfmmu_mlist_held(pp));
6536 6389 }
6537 6390
6538 6391 /* Get number of pages from tte size. */
6539 6392 npgs = TTEPAGES(sz);
6540 6393
6541 6394 do {
6542 6395 ASSERT(pp);
6543 6396 ASSERT(sfmmu_mlist_held(pp));
6544 6397 if (((rm & P_REF) != 0 && !PP_ISREF(pp)) ||
6545 6398 ((rm & P_MOD) != 0 && !PP_ISMOD(pp)))
6546 6399 hat_page_setattr(pp, rm);
6547 6400
6548 6401 /*
6549 6402 * Are we done? If not, we must have a large mapping.
6550 6403 * For large mappings we need to sync the rest of the pages
6551 6404 * covered by this tte; goto the next page.
6552 6405 */
6553 6406 } while (--npgs > 0 && (pp = PP_PAGENEXT(pp)));
6554 6407 }
6555 6408
6556 6409 /*
6557 6410 * Execute pre-callback handler of each pa_hment linked to pp
6558 6411 *
6559 6412 * Inputs:
6560 6413 * flag: either HAT_PRESUSPEND or HAT_SUSPEND.
6561 6414 * capture_cpus: pointer to return value (below)
6562 6415 *
6563 6416 * Returns:
6564 6417 * Propagates the subsystem callback return values back to the caller;
6565 6418 * returns 0 on success. If capture_cpus is non-NULL, the value returned
6566 6419 * is zero if all of the pa_hments are of a type that do not require
6567 6420 * capturing CPUs prior to suspending the mapping, else it is 1.
6568 6421 */
6569 6422 static int
6570 6423 hat_pageprocess_precallbacks(struct page *pp, uint_t flag, int *capture_cpus)
6571 6424 {
6572 6425 struct sf_hment *sfhmep;
6573 6426 struct pa_hment *pahmep;
6574 6427 int (*f)(caddr_t, uint_t, uint_t, void *);
6575 6428 int ret;
6576 6429 id_t id;
6577 6430 int locked = 0;
6578 6431 kmutex_t *pml;
6579 6432
6580 6433 ASSERT(PAGE_EXCL(pp));
6581 6434 if (!sfmmu_mlist_held(pp)) {
6582 6435 pml = sfmmu_mlist_enter(pp);
6583 6436 locked = 1;
6584 6437 }
6585 6438
6586 6439 if (capture_cpus)
6587 6440 *capture_cpus = 0;
6588 6441
6589 6442 top:
6590 6443 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) {
6591 6444 /*
6592 6445 * skip sf_hments corresponding to VA<->PA mappings;
6593 6446 * for pa_hment's, hme_tte.ll is zero
6594 6447 */
6595 6448 if (!IS_PAHME(sfhmep))
6596 6449 continue;
6597 6450
6598 6451 pahmep = sfhmep->hme_data;
6599 6452 ASSERT(pahmep != NULL);
6600 6453
6601 6454 /*
6602 6455 * skip if pre-handler has been called earlier in this loop
6603 6456 */
6604 6457 if (pahmep->flags & flag)
6605 6458 continue;
6606 6459
6607 6460 id = pahmep->cb_id;
6608 6461 ASSERT(id >= (id_t)0 && id < sfmmu_cb_nextid);
6609 6462 if (capture_cpus && sfmmu_cb_table[id].capture_cpus != 0)
6610 6463 *capture_cpus = 1;
6611 6464 if ((f = sfmmu_cb_table[id].prehandler) == NULL) {
6612 6465 pahmep->flags |= flag;
6613 6466 continue;
6614 6467 }
6615 6468
6616 6469 /*
6617 6470 * Drop the mapping list lock to avoid locking order issues.
6618 6471 */
6619 6472 if (locked)
6620 6473 sfmmu_mlist_exit(pml);
6621 6474
6622 6475 ret = f(pahmep->addr, pahmep->len, flag, pahmep->pvt);
6623 6476 if (ret != 0)
6624 6477 return (ret); /* caller must do the cleanup */
6625 6478
6626 6479 if (locked) {
6627 6480 pml = sfmmu_mlist_enter(pp);
6628 6481 pahmep->flags |= flag;
6629 6482 goto top;
6630 6483 }
6631 6484
6632 6485 pahmep->flags |= flag;
6633 6486 }
6634 6487
6635 6488 if (locked)
6636 6489 sfmmu_mlist_exit(pml);
6637 6490
6638 6491 return (0);
6639 6492 }
6640 6493
6641 6494 /*
6642 6495 * Execute post-callback handler of each pa_hment linked to pp
6643 6496 *
6644 6497 * Same overall assumptions and restrictions apply as for
6645 6498 * hat_pageprocess_precallbacks().
6646 6499 */
6647 6500 static void
6648 6501 hat_pageprocess_postcallbacks(struct page *pp, uint_t flag)
6649 6502 {
6650 6503 pfn_t pgpfn = pp->p_pagenum;
6651 6504 pfn_t pgmask = btop(page_get_pagesize(pp->p_szc)) - 1;
6652 6505 pfn_t newpfn;
6653 6506 struct sf_hment *sfhmep;
6654 6507 struct pa_hment *pahmep;
6655 6508 int (*f)(caddr_t, uint_t, uint_t, void *, pfn_t);
6656 6509 id_t id;
6657 6510 int locked = 0;
6658 6511 kmutex_t *pml;
6659 6512
6660 6513 ASSERT(PAGE_EXCL(pp));
6661 6514 if (!sfmmu_mlist_held(pp)) {
6662 6515 pml = sfmmu_mlist_enter(pp);
6663 6516 locked = 1;
6664 6517 }
6665 6518
6666 6519 top:
6667 6520 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) {
6668 6521 /*
6669 6522 * skip sf_hments corresponding to VA<->PA mappings;
6670 6523 * for pa_hment's, hme_tte.ll is zero
6671 6524 */
6672 6525 if (!IS_PAHME(sfhmep))
6673 6526 continue;
6674 6527
6675 6528 pahmep = sfhmep->hme_data;
6676 6529 ASSERT(pahmep != NULL);
6677 6530
6678 6531 if ((pahmep->flags & flag) == 0)
6679 6532 continue;
6680 6533
6681 6534 pahmep->flags &= ~flag;
6682 6535
6683 6536 id = pahmep->cb_id;
6684 6537 ASSERT(id >= (id_t)0 && id < sfmmu_cb_nextid);
6685 6538 if ((f = sfmmu_cb_table[id].posthandler) == NULL)
6686 6539 continue;
6687 6540
6688 6541 /*
6689 6542 * Convert the base page PFN into the constituent PFN
6690 6543 * which is needed by the callback handler.
6691 6544 */
6692 6545 newpfn = pgpfn | (btop((uintptr_t)pahmep->addr) & pgmask);
6693 6546
6694 6547 /*
6695 6548 * Drop the mapping list lock to avoid locking order issues.
6696 6549 */
6697 6550 if (locked)
6698 6551 sfmmu_mlist_exit(pml);
6699 6552
6700 6553 if (f(pahmep->addr, pahmep->len, flag, pahmep->pvt, newpfn)
6701 6554 != 0)
6702 6555 panic("sfmmu: posthandler failed");
6703 6556
6704 6557 if (locked) {
6705 6558 pml = sfmmu_mlist_enter(pp);
6706 6559 goto top;
6707 6560 }
6708 6561 }
6709 6562
6710 6563 if (locked)
6711 6564 sfmmu_mlist_exit(pml);
6712 6565 }
6713 6566
6714 6567 /*
6715 6568 * Suspend locked kernel mapping
6716 6569 */
6717 6570 void
6718 6571 hat_pagesuspend(struct page *pp)
6719 6572 {
6720 6573 struct sf_hment *sfhmep;
6721 6574 sfmmu_t *sfmmup;
6722 6575 tte_t tte, ttemod;
6723 6576 struct hme_blk *hmeblkp;
6724 6577 caddr_t addr;
6725 6578 int index, cons;
6726 6579 cpuset_t cpuset;
6727 6580
6728 6581 ASSERT(PAGE_EXCL(pp));
6729 6582 ASSERT(sfmmu_mlist_held(pp));
6730 6583
6731 6584 mutex_enter(&kpr_suspendlock);
6732 6585
6733 6586 /*
6734 6587 * We're about to suspend a kernel mapping so mark this thread as
6735 6588 * non-traceable by DTrace. This prevents us from running into issues
6736 6589 * with probe context trying to touch a suspended page
6737 6590 * in the relocation codepath itself.
6738 6591 */
6739 6592 curthread->t_flag |= T_DONTDTRACE;
6740 6593
6741 6594 index = PP_MAPINDEX(pp);
6742 6595 cons = TTE8K;
6743 6596
6744 6597 retry:
6745 6598 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) {
6746 6599
6747 6600 if (IS_PAHME(sfhmep))
6748 6601 continue;
6749 6602
6750 6603 if (get_hblk_ttesz(sfmmu_hmetohblk(sfhmep)) != cons)
6751 6604 continue;
6752 6605
6753 6606 /*
6754 6607 * Loop until we successfully set the suspend bit in
6755 6608 * the TTE.
6756 6609 */
6757 6610 again:
6758 6611 sfmmu_copytte(&sfhmep->hme_tte, &tte);
6759 6612 ASSERT(TTE_IS_VALID(&tte));
6760 6613
6761 6614 ttemod = tte;
6762 6615 TTE_SET_SUSPEND(&ttemod);
6763 6616 if (sfmmu_modifytte_try(&tte, &ttemod,
6764 6617 &sfhmep->hme_tte) < 0)
6765 6618 goto again;
6766 6619
6767 6620 /*
6768 6621 * Invalidate TSB entry
6769 6622 */
6770 6623 hmeblkp = sfmmu_hmetohblk(sfhmep);
6771 6624
6772 6625 sfmmup = hblktosfmmu(hmeblkp);
6773 6626 ASSERT(sfmmup == ksfmmup);
6774 6627 ASSERT(!hmeblkp->hblk_shared);
6775 6628
6776 6629 addr = tte_to_vaddr(hmeblkp, tte);
6777 6630
6778 6631 /*
6779 6632 * No need to make sure that the TSB for this sfmmu is
6780 6633 * not being relocated since it is ksfmmup and thus it
6781 6634 * will never be relocated.
6782 6635 */
6783 6636 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0);
6784 6637
6785 6638 /*
6786 6639 * Update xcall stats
6787 6640 */
6788 6641 cpuset = cpu_ready_set;
6789 6642 CPUSET_DEL(cpuset, CPU->cpu_id);
6790 6643
6791 6644 /* LINTED: constant in conditional context */
6792 6645 SFMMU_XCALL_STATS(ksfmmup);
6793 6646
6794 6647 /*
6795 6648 * Flush TLB entry on remote CPU's
6796 6649 */
6797 6650 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr,
6798 6651 (uint64_t)ksfmmup);
6799 6652 xt_sync(cpuset);
6800 6653
6801 6654 /*
6802 6655 * Flush TLB entry on local CPU
6803 6656 */
6804 6657 vtag_flushpage(addr, (uint64_t)ksfmmup);
6805 6658 }
6806 6659
6807 6660 while (index != 0) {
6808 6661 index = index >> 1;
6809 6662 if (index != 0)
6810 6663 cons++;
6811 6664 if (index & 0x1) {
6812 6665 pp = PP_GROUPLEADER(pp, cons);
6813 6666 goto retry;
6814 6667 }
6815 6668 }
6816 6669 }
6817 6670
6818 6671 #ifdef DEBUG
6819 6672
6820 6673 #define N_PRLE 1024
6821 6674 struct prle {
6822 6675 page_t *targ;
6823 6676 page_t *repl;
6824 6677 int status;
6825 6678 int pausecpus;
6826 6679 hrtime_t whence;
6827 6680 };
6828 6681
6829 6682 static struct prle page_relocate_log[N_PRLE];
6830 6683 static int prl_entry;
6831 6684 static kmutex_t prl_mutex;
6832 6685
6833 6686 #define PAGE_RELOCATE_LOG(t, r, s, p) \
6834 6687 mutex_enter(&prl_mutex); \
6835 6688 page_relocate_log[prl_entry].targ = *(t); \
6836 6689 page_relocate_log[prl_entry].repl = *(r); \
6837 6690 page_relocate_log[prl_entry].status = (s); \
6838 6691 page_relocate_log[prl_entry].pausecpus = (p); \
6839 6692 page_relocate_log[prl_entry].whence = gethrtime(); \
6840 6693 prl_entry = (prl_entry == (N_PRLE - 1))? 0 : prl_entry + 1; \
6841 6694 mutex_exit(&prl_mutex);
6842 6695
6843 6696 #else /* !DEBUG */
6844 6697 #define PAGE_RELOCATE_LOG(t, r, s, p)
6845 6698 #endif
6846 6699
6847 6700 /*
6848 6701 * Core Kernel Page Relocation Algorithm
6849 6702 *
6850 6703 * Input:
6851 6704 *
6852 6705 * target : constituent pages are SE_EXCL locked.
6853 6706 * replacement: constituent pages are SE_EXCL locked.
6854 6707 *
6855 6708 * Output:
6856 6709 *
6857 6710 * nrelocp: number of pages relocated
6858 6711 */
6859 6712 int
6860 6713 hat_page_relocate(page_t **target, page_t **replacement, spgcnt_t *nrelocp)
6861 6714 {
6862 6715 page_t *targ, *repl;
6863 6716 page_t *tpp, *rpp;
6864 6717 kmutex_t *low, *high;
6865 6718 spgcnt_t npages, i;
6866 6719 page_t *pl = NULL;
6867 6720 int old_pil;
6868 6721 cpuset_t cpuset;
6869 6722 int cap_cpus;
6870 6723 int ret;
6871 6724 #ifdef VAC
6872 6725 int cflags = 0;
6873 6726 #endif
6874 6727
6875 6728 if (!kcage_on || PP_ISNORELOC(*target)) {
6876 6729 PAGE_RELOCATE_LOG(target, replacement, EAGAIN, -1);
6877 6730 return (EAGAIN);
6878 6731 }
6879 6732
6880 6733 mutex_enter(&kpr_mutex);
6881 6734 kreloc_thread = curthread;
6882 6735
6883 6736 targ = *target;
6884 6737 repl = *replacement;
6885 6738 ASSERT(repl != NULL);
6886 6739 ASSERT(targ->p_szc == repl->p_szc);
6887 6740
6888 6741 npages = page_get_pagecnt(targ->p_szc);
6889 6742
6890 6743 /*
6891 6744 * unload VA<->PA mappings that are not locked
6892 6745 */
6893 6746 tpp = targ;
6894 6747 for (i = 0; i < npages; i++) {
6895 6748 (void) hat_pageunload(tpp, SFMMU_KERNEL_RELOC);
6896 6749 tpp++;
6897 6750 }
6898 6751
6899 6752 /*
6900 6753 * Do "presuspend" callbacks, in a context from which we can still
6901 6754 * block as needed. Note that we don't hold the mapping list lock
6902 6755 * of "targ" at this point due to potential locking order issues;
6903 6756 * we assume that between the hat_pageunload() above and holding
6904 6757 * the SE_EXCL lock that the mapping list *cannot* change at this
6905 6758 * point.
6906 6759 */
6907 6760 ret = hat_pageprocess_precallbacks(targ, HAT_PRESUSPEND, &cap_cpus);
6908 6761 if (ret != 0) {
6909 6762 /*
6910 6763 * EIO translates to fatal error, for all others cleanup
6911 6764 * and return EAGAIN.
6912 6765 */
6913 6766 ASSERT(ret != EIO);
6914 6767 hat_pageprocess_postcallbacks(targ, HAT_POSTUNSUSPEND);
6915 6768 PAGE_RELOCATE_LOG(target, replacement, ret, -1);
6916 6769 kreloc_thread = NULL;
6917 6770 mutex_exit(&kpr_mutex);
6918 6771 return (EAGAIN);
6919 6772 }
6920 6773
6921 6774 /*
6922 6775 * acquire p_mapping list lock for both the target and replacement
6923 6776 * root pages.
6924 6777 *
6925 6778 * low and high refer to the need to grab the mlist locks in a
6926 6779 * specific order in order to prevent race conditions. Thus the
6927 6780 * lower lock must be grabbed before the higher lock.
6928 6781 *
6929 6782 * This will block hat_unload's accessing p_mapping list. Since
6930 6783 * we have SE_EXCL lock, hat_memload and hat_pageunload will be
6931 6784 * blocked. Thus, no one else will be accessing the p_mapping list
6932 6785 * while we suspend and reload the locked mapping below.
6933 6786 */
6934 6787 tpp = targ;
6935 6788 rpp = repl;
6936 6789 sfmmu_mlist_reloc_enter(tpp, rpp, &low, &high);
6937 6790
6938 6791 kpreempt_disable();
6939 6792
6940 6793 /*
6941 6794 * We raise our PIL to 13 so that we don't get captured by
6942 6795 * another CPU or pinned by an interrupt thread. We can't go to
6943 6796 * PIL 14 since the nexus driver(s) may need to interrupt at
6944 6797 * that level in the case of IOMMU pseudo mappings.
6945 6798 */
6946 6799 cpuset = cpu_ready_set;
6947 6800 CPUSET_DEL(cpuset, CPU->cpu_id);
6948 6801 if (!cap_cpus || CPUSET_ISNULL(cpuset)) {
6949 6802 old_pil = splr(XCALL_PIL);
6950 6803 } else {
6951 6804 old_pil = -1;
6952 6805 xc_attention(cpuset);
6953 6806 }
6954 6807 ASSERT(getpil() == XCALL_PIL);
6955 6808
6956 6809 /*
6957 6810 * Now do suspend callbacks. In the case of an IOMMU mapping
6958 6811 * this will suspend all DMA activity to the page while it is
6959 6812 * being relocated. Since we are well above LOCK_LEVEL and CPUs
6960 6813 * may be captured at this point we should have acquired any needed
6961 6814 * locks in the presuspend callback.
6962 6815 */
6963 6816 ret = hat_pageprocess_precallbacks(targ, HAT_SUSPEND, NULL);
6964 6817 if (ret != 0) {
6965 6818 repl = targ;
6966 6819 goto suspend_fail;
6967 6820 }
6968 6821
6969 6822 /*
6970 6823 * Raise the PIL yet again, this time to block all high-level
6971 6824 * interrupts on this CPU. This is necessary to prevent an
6972 6825 * interrupt routine from pinning the thread which holds the
6973 6826 * mapping suspended and then touching the suspended page.
6974 6827 *
6975 6828 * Once the page is suspended we also need to be careful to
6976 6829 * avoid calling any functions which touch any seg_kmem memory
6977 6830 * since that memory may be backed by the very page we are
6978 6831 * relocating in here!
6979 6832 */
6980 6833 hat_pagesuspend(targ);
6981 6834
6982 6835 /*
6983 6836 * Now that we are confident everybody has stopped using this page,
6984 6837 * copy the page contents. Note we use a physical copy to prevent
6985 6838 * locking issues and to avoid fpRAS because we can't handle it in
6986 6839 * this context.
6987 6840 */
6988 6841 for (i = 0; i < npages; i++, tpp++, rpp++) {
6989 6842 #ifdef VAC
6990 6843 /*
6991 6844 * If the replacement has a different vcolor than
6992 6845 * the one being replacd, we need to handle VAC
6993 6846 * consistency for it just as we were setting up
6994 6847 * a new mapping to it.
6995 6848 */
6996 6849 if ((PP_GET_VCOLOR(rpp) != NO_VCOLOR) &&
6997 6850 (tpp->p_vcolor != rpp->p_vcolor) &&
6998 6851 !CacheColor_IsFlushed(cflags, PP_GET_VCOLOR(rpp))) {
6999 6852 CacheColor_SetFlushed(cflags, PP_GET_VCOLOR(rpp));
7000 6853 sfmmu_cache_flushcolor(PP_GET_VCOLOR(rpp),
7001 6854 rpp->p_pagenum);
7002 6855 }
7003 6856 #endif
7004 6857 /*
7005 6858 * Copy the contents of the page.
7006 6859 */
7007 6860 ppcopy_kernel(tpp, rpp);
7008 6861 }
7009 6862
7010 6863 tpp = targ;
7011 6864 rpp = repl;
7012 6865 for (i = 0; i < npages; i++, tpp++, rpp++) {
7013 6866 /*
7014 6867 * Copy attributes. VAC consistency was handled above,
7015 6868 * if required.
7016 6869 */
7017 6870 rpp->p_nrm = tpp->p_nrm;
7018 6871 tpp->p_nrm = 0;
7019 6872 rpp->p_index = tpp->p_index;
7020 6873 tpp->p_index = 0;
7021 6874 #ifdef VAC
7022 6875 rpp->p_vcolor = tpp->p_vcolor;
7023 6876 #endif
7024 6877 }
7025 6878
7026 6879 /*
7027 6880 * First, unsuspend the page, if we set the suspend bit, and transfer
7028 6881 * the mapping list from the target page to the replacement page.
7029 6882 * Next process postcallbacks; since pa_hment's are linked only to the
7030 6883 * p_mapping list of root page, we don't iterate over the constituent
7031 6884 * pages.
7032 6885 */
7033 6886 hat_pagereload(targ, repl);
7034 6887
7035 6888 suspend_fail:
7036 6889 hat_pageprocess_postcallbacks(repl, HAT_UNSUSPEND);
7037 6890
7038 6891 /*
7039 6892 * Now lower our PIL and release any captured CPUs since we
7040 6893 * are out of the "danger zone". After this it will again be
7041 6894 * safe to acquire adaptive mutex locks, or to drop them...
7042 6895 */
7043 6896 if (old_pil != -1) {
7044 6897 splx(old_pil);
7045 6898 } else {
7046 6899 xc_dismissed(cpuset);
7047 6900 }
7048 6901
7049 6902 kpreempt_enable();
7050 6903
7051 6904 sfmmu_mlist_reloc_exit(low, high);
7052 6905
7053 6906 /*
7054 6907 * Postsuspend callbacks should drop any locks held across
7055 6908 * the suspend callbacks. As before, we don't hold the mapping
7056 6909 * list lock at this point.. our assumption is that the mapping
7057 6910 * list still can't change due to our holding SE_EXCL lock and
7058 6911 * there being no unlocked mappings left. Hence the restriction
7059 6912 * on calling context to hat_delete_callback()
7060 6913 */
7061 6914 hat_pageprocess_postcallbacks(repl, HAT_POSTUNSUSPEND);
7062 6915 if (ret != 0) {
7063 6916 /*
7064 6917 * The second presuspend call failed: we got here through
7065 6918 * the suspend_fail label above.
7066 6919 */
7067 6920 ASSERT(ret != EIO);
7068 6921 PAGE_RELOCATE_LOG(target, replacement, ret, cap_cpus);
7069 6922 kreloc_thread = NULL;
7070 6923 mutex_exit(&kpr_mutex);
7071 6924 return (EAGAIN);
7072 6925 }
7073 6926
7074 6927 /*
7075 6928 * Now that we're out of the performance critical section we can
7076 6929 * take care of updating the hash table, since we still
7077 6930 * hold all the pages locked SE_EXCL at this point we
7078 6931 * needn't worry about things changing out from under us.
7079 6932 */
7080 6933 tpp = targ;
7081 6934 rpp = repl;
7082 6935 for (i = 0; i < npages; i++, tpp++, rpp++) {
7083 6936
7084 6937 /*
7085 6938 * replace targ with replacement in page_hash table
7086 6939 */
7087 6940 targ = tpp;
7088 6941 page_relocate_hash(rpp, targ);
7089 6942
7090 6943 /*
7091 6944 * concatenate target; caller of platform_page_relocate()
7092 6945 * expects target to be concatenated after returning.
7093 6946 */
7094 6947 ASSERT(targ->p_next == targ);
7095 6948 ASSERT(targ->p_prev == targ);
7096 6949 page_list_concat(&pl, &targ);
7097 6950 }
7098 6951
7099 6952 ASSERT(*target == pl);
7100 6953 *nrelocp = npages;
7101 6954 PAGE_RELOCATE_LOG(target, replacement, 0, cap_cpus);
7102 6955 kreloc_thread = NULL;
7103 6956 mutex_exit(&kpr_mutex);
7104 6957 return (0);
7105 6958 }
7106 6959
7107 6960 /*
7108 6961 * Called when stray pa_hments are found attached to a page which is
7109 6962 * being freed. Notify the subsystem which attached the pa_hment of
7110 6963 * the error if it registered a suitable handler, else panic.
7111 6964 */
7112 6965 static void
7113 6966 sfmmu_pahment_leaked(struct pa_hment *pahmep)
7114 6967 {
7115 6968 id_t cb_id = pahmep->cb_id;
7116 6969
7117 6970 ASSERT(cb_id >= (id_t)0 && cb_id < sfmmu_cb_nextid);
7118 6971 if (sfmmu_cb_table[cb_id].errhandler != NULL) {
7119 6972 if (sfmmu_cb_table[cb_id].errhandler(pahmep->addr, pahmep->len,
7120 6973 HAT_CB_ERR_LEAKED, pahmep->pvt) == 0)
7121 6974 return; /* non-fatal */
7122 6975 }
7123 6976 panic("pa_hment leaked: 0x%p", (void *)pahmep);
7124 6977 }
7125 6978
7126 6979 /*
7127 6980 * Remove all mappings to page 'pp'.
7128 6981 */
7129 6982 int
7130 6983 hat_pageunload(struct page *pp, uint_t forceflag)
7131 6984 {
7132 6985 struct page *origpp = pp;
7133 6986 struct sf_hment *sfhme, *tmphme;
7134 6987 struct hme_blk *hmeblkp;
7135 6988 kmutex_t *pml;
7136 6989 #ifdef VAC
7137 6990 kmutex_t *pmtx;
7138 6991 #endif
7139 6992 cpuset_t cpuset, tset;
7140 6993 int index, cons;
7141 6994 int xhme_blks;
7142 6995 int pa_hments;
7143 6996
7144 6997 ASSERT(PAGE_EXCL(pp));
7145 6998
7146 6999 retry_xhat:
7147 7000 tmphme = NULL;
7148 7001 xhme_blks = 0;
7149 7002 pa_hments = 0;
7150 7003 CPUSET_ZERO(cpuset);
7151 7004
7152 7005 pml = sfmmu_mlist_enter(pp);
7153 7006
7154 7007 #ifdef VAC
7155 7008 if (pp->p_kpmref)
7156 7009 sfmmu_kpm_pageunload(pp);
7157 7010 ASSERT(!PP_ISMAPPED_KPM(pp));
7158 7011 #endif
7159 7012 /*
7160 7013 * Clear vpm reference. Since the page is exclusively locked
7161 7014 * vpm cannot be referencing it.
7162 7015 */
7163 7016 if (vpm_enable) {
7164 7017 pp->p_vpmref = 0;
7165 7018 }
7166 7019
7167 7020 index = PP_MAPINDEX(pp);
7168 7021 cons = TTE8K;
7169 7022 retry:
7170 7023 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) {
7171 7024 tmphme = sfhme->hme_next;
7172 7025
7173 7026 if (IS_PAHME(sfhme)) {
7174 7027 ASSERT(sfhme->hme_data != NULL);
7175 7028 pa_hments++;
7176 7029 continue;
7177 7030 }
7178 7031
7179 7032 hmeblkp = sfmmu_hmetohblk(sfhme);
7180 7033 if (hmeblkp->hblk_xhat_bit) {
7181 7034 struct xhat_hme_blk *xblk =
7182 7035 (struct xhat_hme_blk *)hmeblkp;
7183 7036
7184 7037 (void) XHAT_PAGEUNLOAD(xblk->xhat_hme_blk_hat,
7185 7038 pp, forceflag, XBLK2PROVBLK(xblk));
7186 7039
7187 7040 xhme_blks = 1;
7188 7041 continue;
7189 7042 }
7190 7043
7191 7044 /*
7192 7045 * If there are kernel mappings don't unload them, they will
7193 7046 * be suspended.
7194 7047 */
7195 7048 if (forceflag == SFMMU_KERNEL_RELOC && hmeblkp->hblk_lckcnt &&
7196 7049 hmeblkp->hblk_tag.htag_id == ksfmmup)
7197 7050 continue;
7198 7051
7199 7052 tset = sfmmu_pageunload(pp, sfhme, cons);
7200 7053 CPUSET_OR(cpuset, tset);
7201 7054 }
7202 7055
7203 7056 while (index != 0) {
7204 7057 index = index >> 1;
7205 7058 if (index != 0)
7206 7059 cons++;
7207 7060 if (index & 0x1) {
7208 7061 /* Go to leading page */
7209 7062 pp = PP_GROUPLEADER(pp, cons);
7210 7063 ASSERT(sfmmu_mlist_held(pp));
7211 7064 goto retry;
7212 7065 }
7213 7066 }
7214 7067
7215 7068 /*
7216 7069 * cpuset may be empty if the page was only mapped by segkpm,
7217 7070 * in which case we won't actually cross-trap.
7218 7071 */
7219 7072 xt_sync(cpuset);
7220 7073
7221 7074 /*
7222 7075 * The page should have no mappings at this point, unless
7223 7076 * we were called from hat_page_relocate() in which case we
7224 7077 * leave the locked mappings which will be suspended later.
7225 7078 */
7226 7079 ASSERT(!PP_ISMAPPED(origpp) || xhme_blks || pa_hments ||
7227 7080 (forceflag == SFMMU_KERNEL_RELOC));
7228 7081
7229 7082 #ifdef VAC
7230 7083 if (PP_ISTNC(pp)) {
7231 7084 if (cons == TTE8K) {
7232 7085 pmtx = sfmmu_page_enter(pp);
7233 7086 PP_CLRTNC(pp);
7234 7087 sfmmu_page_exit(pmtx);
7235 7088 } else {
7236 7089 conv_tnc(pp, cons);
7237 7090 }
7238 7091 }
7239 7092 #endif /* VAC */
7240 7093
7241 7094 if (pa_hments && forceflag != SFMMU_KERNEL_RELOC) {
7242 7095 /*
7243 7096 * Unlink any pa_hments and free them, calling back
7244 7097 * the responsible subsystem to notify it of the error.
7245 7098 * This can occur in situations such as drivers leaking
7246 7099 * DMA handles: naughty, but common enough that we'd like
7247 7100 * to keep the system running rather than bringing it
7248 7101 * down with an obscure error like "pa_hment leaked"
7249 7102 * which doesn't aid the user in debugging their driver.
7250 7103 */
7251 7104 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) {
7252 7105 tmphme = sfhme->hme_next;
7253 7106 if (IS_PAHME(sfhme)) {
7254 7107 struct pa_hment *pahmep = sfhme->hme_data;
7255 7108 sfmmu_pahment_leaked(pahmep);
7256 7109 HME_SUB(sfhme, pp);
7257 7110 kmem_cache_free(pa_hment_cache, pahmep);
7258 7111 }
7259 7112 }
7260 7113
7261 7114 ASSERT(!PP_ISMAPPED(origpp) || xhme_blks);
7262 7115 }
7263 7116
7264 7117 sfmmu_mlist_exit(pml);
7265 7118
7266 7119 /*
7267 7120 * XHAT may not have finished unloading pages
7268 7121 * because some other thread was waiting for
7269 7122 * mlist lock and XHAT_PAGEUNLOAD let it do
7270 7123 * the job.
7271 7124 */
7272 7125 if (xhme_blks) {
7273 7126 pp = origpp;
7274 7127 goto retry_xhat;
7275 7128 }
7276 7129
7277 7130 return (0);
7278 7131 }
7279 7132
7280 7133 cpuset_t
7281 7134 sfmmu_pageunload(page_t *pp, struct sf_hment *sfhme, int cons)
7282 7135 {
7283 7136 struct hme_blk *hmeblkp;
7284 7137 sfmmu_t *sfmmup;
7285 7138 tte_t tte, ttemod;
7286 7139 #ifdef DEBUG
7287 7140 tte_t orig_old;
7288 7141 #endif /* DEBUG */
7289 7142 caddr_t addr;
7290 7143 int ttesz;
7291 7144 int ret;
7292 7145 cpuset_t cpuset;
7293 7146
7294 7147 ASSERT(pp != NULL);
7295 7148 ASSERT(sfmmu_mlist_held(pp));
7296 7149 ASSERT(!PP_ISKAS(pp));
7297 7150
7298 7151 CPUSET_ZERO(cpuset);
7299 7152
7300 7153 hmeblkp = sfmmu_hmetohblk(sfhme);
7301 7154
7302 7155 readtte:
7303 7156 sfmmu_copytte(&sfhme->hme_tte, &tte);
7304 7157 if (TTE_IS_VALID(&tte)) {
7305 7158 sfmmup = hblktosfmmu(hmeblkp);
7306 7159 ttesz = get_hblk_ttesz(hmeblkp);
7307 7160 /*
7308 7161 * Only unload mappings of 'cons' size.
7309 7162 */
7310 7163 if (ttesz != cons)
7311 7164 return (cpuset);
7312 7165
7313 7166 /*
7314 7167 * Note that we have p_mapping lock, but no hash lock here.
7315 7168 * hblk_unload() has to have both hash lock AND p_mapping
7316 7169 * lock before it tries to modify tte. So, the tte could
7317 7170 * not become invalid in the sfmmu_modifytte_try() below.
7318 7171 */
7319 7172 ttemod = tte;
7320 7173 #ifdef DEBUG
7321 7174 orig_old = tte;
7322 7175 #endif /* DEBUG */
7323 7176
7324 7177 TTE_SET_INVALID(&ttemod);
7325 7178 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte);
7326 7179 if (ret < 0) {
7327 7180 #ifdef DEBUG
7328 7181 /* only R/M bits can change. */
7329 7182 chk_tte(&orig_old, &tte, &ttemod, hmeblkp);
7330 7183 #endif /* DEBUG */
7331 7184 goto readtte;
7332 7185 }
7333 7186
7334 7187 if (ret == 0) {
7335 7188 panic("pageunload: cas failed?");
7336 7189 }
7337 7190
7338 7191 addr = tte_to_vaddr(hmeblkp, tte);
7339 7192
7340 7193 if (hmeblkp->hblk_shared) {
7341 7194 sf_srd_t *srdp = (sf_srd_t *)sfmmup;
7342 7195 uint_t rid = hmeblkp->hblk_tag.htag_rid;
7343 7196 sf_region_t *rgnp;
7344 7197 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
7345 7198 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
7346 7199 ASSERT(srdp != NULL);
7347 7200 rgnp = srdp->srd_hmergnp[rid];
7348 7201 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid);
7349 7202 cpuset = sfmmu_rgntlb_demap(addr, rgnp, hmeblkp, 1);
7350 7203 sfmmu_ttesync(NULL, addr, &tte, pp);
7351 7204 ASSERT(rgnp->rgn_ttecnt[ttesz] > 0);
7352 7205 atomic_add_long(&rgnp->rgn_ttecnt[ttesz], -1);
7353 7206 } else {
7354 7207 sfmmu_ttesync(sfmmup, addr, &tte, pp);
7355 7208 atomic_add_long(&sfmmup->sfmmu_ttecnt[ttesz], -1);
7356 7209
7357 7210 /*
7358 7211 * We need to flush the page from the virtual cache
7359 7212 * in order to prevent a virtual cache alias
7360 7213 * inconsistency. The particular scenario we need
7361 7214 * to worry about is:
7362 7215 * Given: va1 and va2 are two virtual address that
7363 7216 * alias and will map the same physical address.
7364 7217 * 1. mapping exists from va1 to pa and data has
7365 7218 * been read into the cache.
7366 7219 * 2. unload va1.
7367 7220 * 3. load va2 and modify data using va2.
7368 7221 * 4 unload va2.
7369 7222 * 5. load va1 and reference data. Unless we flush
7370 7223 * the data cache when we unload we will get
7371 7224 * stale data.
7372 7225 * This scenario is taken care of by using virtual
7373 7226 * page coloring.
7374 7227 */
7375 7228 if (sfmmup->sfmmu_ismhat) {
7376 7229 /*
7377 7230 * Flush TSBs, TLBs and caches
7378 7231 * of every process
7379 7232 * sharing this ism segment.
7380 7233 */
7381 7234 sfmmu_hat_lock_all();
7382 7235 mutex_enter(&ism_mlist_lock);
7383 7236 kpreempt_disable();
7384 7237 sfmmu_ismtlbcache_demap(addr, sfmmup, hmeblkp,
7385 7238 pp->p_pagenum, CACHE_NO_FLUSH);
7386 7239 kpreempt_enable();
7387 7240 mutex_exit(&ism_mlist_lock);
7388 7241 sfmmu_hat_unlock_all();
7389 7242 cpuset = cpu_ready_set;
7390 7243 } else {
7391 7244 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0);
7392 7245 cpuset = sfmmup->sfmmu_cpusran;
7393 7246 }
7394 7247 }
7395 7248
7396 7249 /*
7397 7250 * Hme_sub has to run after ttesync() and a_rss update.
7398 7251 * See hblk_unload().
7399 7252 */
7400 7253 HME_SUB(sfhme, pp);
7401 7254 membar_stst();
7402 7255
7403 7256 /*
7404 7257 * We can not make ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS)
7405 7258 * since pteload may have done a HME_ADD() right after
7406 7259 * we did the HME_SUB() above. Hmecnt is now maintained
7407 7260 * by cas only. no lock guranteed its value. The only
7408 7261 * gurantee we have is the hmecnt should not be less than
7409 7262 * what it should be so the hblk will not be taken away.
7410 7263 * It's also important that we decremented the hmecnt after
7411 7264 * we are done with hmeblkp so that this hmeblk won't be
7412 7265 * stolen.
7413 7266 */
7414 7267 ASSERT(hmeblkp->hblk_hmecnt > 0);
7415 7268 ASSERT(hmeblkp->hblk_vcnt > 0);
7416 7269 atomic_add_16(&hmeblkp->hblk_vcnt, -1);
7417 7270 atomic_add_16(&hmeblkp->hblk_hmecnt, -1);
7418 7271 /*
7419 7272 * This is bug 4063182.
7420 7273 * XXX: fixme
7421 7274 * ASSERT(hmeblkp->hblk_hmecnt || hmeblkp->hblk_vcnt ||
7422 7275 * !hmeblkp->hblk_lckcnt);
7423 7276 */
7424 7277 } else {
7425 7278 panic("invalid tte? pp %p &tte %p",
7426 7279 (void *)pp, (void *)&tte);
7427 7280 }
7428 7281
7429 7282 return (cpuset);
7430 7283 }
7431 7284
7432 7285 /*
7433 7286 * While relocating a kernel page, this function will move the mappings
7434 7287 * from tpp to dpp and modify any associated data with these mappings.
7435 7288 * It also unsuspends the suspended kernel mapping.
7436 7289 */
7437 7290 static void
7438 7291 hat_pagereload(struct page *tpp, struct page *dpp)
7439 7292 {
7440 7293 struct sf_hment *sfhme;
7441 7294 tte_t tte, ttemod;
7442 7295 int index, cons;
7443 7296
7444 7297 ASSERT(getpil() == PIL_MAX);
7445 7298 ASSERT(sfmmu_mlist_held(tpp));
7446 7299 ASSERT(sfmmu_mlist_held(dpp));
7447 7300
7448 7301 index = PP_MAPINDEX(tpp);
7449 7302 cons = TTE8K;
7450 7303
7451 7304 /* Update real mappings to the page */
7452 7305 retry:
7453 7306 for (sfhme = tpp->p_mapping; sfhme != NULL; sfhme = sfhme->hme_next) {
7454 7307 if (IS_PAHME(sfhme))
7455 7308 continue;
7456 7309 sfmmu_copytte(&sfhme->hme_tte, &tte);
7457 7310 ttemod = tte;
7458 7311
7459 7312 /*
7460 7313 * replace old pfn with new pfn in TTE
7461 7314 */
7462 7315 PFN_TO_TTE(ttemod, dpp->p_pagenum);
7463 7316
7464 7317 /*
7465 7318 * clear suspend bit
7466 7319 */
7467 7320 ASSERT(TTE_IS_SUSPEND(&ttemod));
7468 7321 TTE_CLR_SUSPEND(&ttemod);
7469 7322
7470 7323 if (sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte) < 0)
7471 7324 panic("hat_pagereload(): sfmmu_modifytte_try() failed");
7472 7325
7473 7326 /*
7474 7327 * set hme_page point to new page
7475 7328 */
7476 7329 sfhme->hme_page = dpp;
7477 7330 }
7478 7331
7479 7332 /*
7480 7333 * move p_mapping list from old page to new page
7481 7334 */
7482 7335 dpp->p_mapping = tpp->p_mapping;
7483 7336 tpp->p_mapping = NULL;
7484 7337 dpp->p_share = tpp->p_share;
7485 7338 tpp->p_share = 0;
7486 7339
7487 7340 while (index != 0) {
7488 7341 index = index >> 1;
7489 7342 if (index != 0)
7490 7343 cons++;
7491 7344 if (index & 0x1) {
7492 7345 tpp = PP_GROUPLEADER(tpp, cons);
7493 7346 dpp = PP_GROUPLEADER(dpp, cons);
7494 7347 goto retry;
7495 7348 }
7496 7349 }
7497 7350
7498 7351 curthread->t_flag &= ~T_DONTDTRACE;
7499 7352 mutex_exit(&kpr_suspendlock);
7500 7353 }
7501 7354
7502 7355 uint_t
7503 7356 hat_pagesync(struct page *pp, uint_t clearflag)
7504 7357 {
7505 7358 struct sf_hment *sfhme, *tmphme = NULL;
7506 7359 struct hme_blk *hmeblkp;
7507 7360 kmutex_t *pml;
7508 7361 cpuset_t cpuset, tset;
7509 7362 int index, cons;
7510 7363 extern ulong_t po_share;
7511 7364 page_t *save_pp = pp;
7512 7365 int stop_on_sh = 0;
7513 7366 uint_t shcnt;
7514 7367
7515 7368 CPUSET_ZERO(cpuset);
7516 7369
7517 7370 if (PP_ISRO(pp) && (clearflag & HAT_SYNC_STOPON_MOD)) {
7518 7371 return (PP_GENERIC_ATTR(pp));
7519 7372 }
7520 7373
7521 7374 if ((clearflag & HAT_SYNC_ZERORM) == 0) {
7522 7375 if ((clearflag & HAT_SYNC_STOPON_REF) && PP_ISREF(pp)) {
7523 7376 return (PP_GENERIC_ATTR(pp));
7524 7377 }
7525 7378 if ((clearflag & HAT_SYNC_STOPON_MOD) && PP_ISMOD(pp)) {
7526 7379 return (PP_GENERIC_ATTR(pp));
7527 7380 }
7528 7381 if (clearflag & HAT_SYNC_STOPON_SHARED) {
7529 7382 if (pp->p_share > po_share) {
7530 7383 hat_page_setattr(pp, P_REF);
7531 7384 return (PP_GENERIC_ATTR(pp));
7532 7385 }
7533 7386 stop_on_sh = 1;
7534 7387 shcnt = 0;
7535 7388 }
7536 7389 }
7537 7390
7538 7391 clearflag &= ~HAT_SYNC_STOPON_SHARED;
7539 7392 pml = sfmmu_mlist_enter(pp);
7540 7393 index = PP_MAPINDEX(pp);
7541 7394 cons = TTE8K;
7542 7395 retry:
7543 7396 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) {
7544 7397 /*
7545 7398 * We need to save the next hment on the list since
7546 7399 * it is possible for pagesync to remove an invalid hment
7547 7400 * from the list.
7548 7401 */
7549 7402 tmphme = sfhme->hme_next;
7550 7403 if (IS_PAHME(sfhme))
7551 7404 continue;
7552 7405 /*
7553 7406 * If we are looking for large mappings and this hme doesn't
7554 7407 * reach the range we are seeking, just ignore it.
7555 7408 */
7556 7409 hmeblkp = sfmmu_hmetohblk(sfhme);
7557 7410 if (hmeblkp->hblk_xhat_bit)
7558 7411 continue;
7559 7412
7560 7413 if (hme_size(sfhme) < cons)
7561 7414 continue;
7562 7415
7563 7416 if (stop_on_sh) {
7564 7417 if (hmeblkp->hblk_shared) {
7565 7418 sf_srd_t *srdp = hblktosrd(hmeblkp);
7566 7419 uint_t rid = hmeblkp->hblk_tag.htag_rid;
7567 7420 sf_region_t *rgnp;
7568 7421 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
7569 7422 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
7570 7423 ASSERT(srdp != NULL);
7571 7424 rgnp = srdp->srd_hmergnp[rid];
7572 7425 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp,
7573 7426 rgnp, rid);
7574 7427 shcnt += rgnp->rgn_refcnt;
7575 7428 } else {
7576 7429 shcnt++;
7577 7430 }
7578 7431 if (shcnt > po_share) {
7579 7432 /*
7580 7433 * tell the pager to spare the page this time
7581 7434 * around.
7582 7435 */
7583 7436 hat_page_setattr(save_pp, P_REF);
7584 7437 index = 0;
7585 7438 break;
7586 7439 }
7587 7440 }
7588 7441 tset = sfmmu_pagesync(pp, sfhme,
7589 7442 clearflag & ~HAT_SYNC_STOPON_RM);
7590 7443 CPUSET_OR(cpuset, tset);
7591 7444
7592 7445 /*
7593 7446 * If clearflag is HAT_SYNC_DONTZERO, break out as soon
7594 7447 * as the "ref" or "mod" is set or share cnt exceeds po_share.
7595 7448 */
7596 7449 if ((clearflag & ~HAT_SYNC_STOPON_RM) == HAT_SYNC_DONTZERO &&
7597 7450 (((clearflag & HAT_SYNC_STOPON_MOD) && PP_ISMOD(save_pp)) ||
7598 7451 ((clearflag & HAT_SYNC_STOPON_REF) && PP_ISREF(save_pp)))) {
7599 7452 index = 0;
7600 7453 break;
7601 7454 }
7602 7455 }
7603 7456
7604 7457 while (index) {
7605 7458 index = index >> 1;
7606 7459 cons++;
7607 7460 if (index & 0x1) {
7608 7461 /* Go to leading page */
7609 7462 pp = PP_GROUPLEADER(pp, cons);
7610 7463 goto retry;
7611 7464 }
7612 7465 }
7613 7466
7614 7467 xt_sync(cpuset);
7615 7468 sfmmu_mlist_exit(pml);
7616 7469 return (PP_GENERIC_ATTR(save_pp));
7617 7470 }
7618 7471
7619 7472 /*
7620 7473 * Get all the hardware dependent attributes for a page struct
7621 7474 */
7622 7475 static cpuset_t
7623 7476 sfmmu_pagesync(struct page *pp, struct sf_hment *sfhme,
7624 7477 uint_t clearflag)
7625 7478 {
7626 7479 caddr_t addr;
7627 7480 tte_t tte, ttemod;
7628 7481 struct hme_blk *hmeblkp;
7629 7482 int ret;
7630 7483 sfmmu_t *sfmmup;
7631 7484 cpuset_t cpuset;
7632 7485
7633 7486 ASSERT(pp != NULL);
7634 7487 ASSERT(sfmmu_mlist_held(pp));
7635 7488 ASSERT((clearflag == HAT_SYNC_DONTZERO) ||
7636 7489 (clearflag == HAT_SYNC_ZERORM));
7637 7490
7638 7491 SFMMU_STAT(sf_pagesync);
7639 7492
7640 7493 CPUSET_ZERO(cpuset);
7641 7494
7642 7495 sfmmu_pagesync_retry:
7643 7496
7644 7497 sfmmu_copytte(&sfhme->hme_tte, &tte);
7645 7498 if (TTE_IS_VALID(&tte)) {
7646 7499 hmeblkp = sfmmu_hmetohblk(sfhme);
7647 7500 sfmmup = hblktosfmmu(hmeblkp);
7648 7501 addr = tte_to_vaddr(hmeblkp, tte);
7649 7502 if (clearflag == HAT_SYNC_ZERORM) {
7650 7503 ttemod = tte;
7651 7504 TTE_CLR_RM(&ttemod);
7652 7505 ret = sfmmu_modifytte_try(&tte, &ttemod,
7653 7506 &sfhme->hme_tte);
7654 7507 if (ret < 0) {
7655 7508 /*
7656 7509 * cas failed and the new value is not what
7657 7510 * we want.
7658 7511 */
7659 7512 goto sfmmu_pagesync_retry;
7660 7513 }
7661 7514
7662 7515 if (ret > 0) {
7663 7516 /* we win the cas */
7664 7517 if (hmeblkp->hblk_shared) {
7665 7518 sf_srd_t *srdp = (sf_srd_t *)sfmmup;
7666 7519 uint_t rid =
7667 7520 hmeblkp->hblk_tag.htag_rid;
7668 7521 sf_region_t *rgnp;
7669 7522 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
7670 7523 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
7671 7524 ASSERT(srdp != NULL);
7672 7525 rgnp = srdp->srd_hmergnp[rid];
7673 7526 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp,
7674 7527 srdp, rgnp, rid);
7675 7528 cpuset = sfmmu_rgntlb_demap(addr,
7676 7529 rgnp, hmeblkp, 1);
7677 7530 } else {
7678 7531 sfmmu_tlb_demap(addr, sfmmup, hmeblkp,
7679 7532 0, 0);
7680 7533 cpuset = sfmmup->sfmmu_cpusran;
7681 7534 }
7682 7535 }
7683 7536 }
7684 7537 sfmmu_ttesync(hmeblkp->hblk_shared ? NULL : sfmmup, addr,
7685 7538 &tte, pp);
7686 7539 }
7687 7540 return (cpuset);
7688 7541 }
7689 7542
7690 7543 /*
7691 7544 * Remove write permission from a mappings to a page, so that
7692 7545 * we can detect the next modification of it. This requires modifying
7693 7546 * the TTE then invalidating (demap) any TLB entry using that TTE.
7694 7547 * This code is similar to sfmmu_pagesync().
7695 7548 */
7696 7549 static cpuset_t
7697 7550 sfmmu_pageclrwrt(struct page *pp, struct sf_hment *sfhme)
7698 7551 {
7699 7552 caddr_t addr;
7700 7553 tte_t tte;
7701 7554 tte_t ttemod;
7702 7555 struct hme_blk *hmeblkp;
7703 7556 int ret;
7704 7557 sfmmu_t *sfmmup;
7705 7558 cpuset_t cpuset;
7706 7559
7707 7560 ASSERT(pp != NULL);
7708 7561 ASSERT(sfmmu_mlist_held(pp));
7709 7562
7710 7563 CPUSET_ZERO(cpuset);
7711 7564 SFMMU_STAT(sf_clrwrt);
7712 7565
7713 7566 retry:
7714 7567
7715 7568 sfmmu_copytte(&sfhme->hme_tte, &tte);
7716 7569 if (TTE_IS_VALID(&tte) && TTE_IS_WRITABLE(&tte)) {
7717 7570 hmeblkp = sfmmu_hmetohblk(sfhme);
7718 7571
7719 7572 /*
7720 7573 * xhat mappings should never be to a VMODSORT page.
7721 7574 */
7722 7575 ASSERT(hmeblkp->hblk_xhat_bit == 0);
7723 7576
7724 7577 sfmmup = hblktosfmmu(hmeblkp);
7725 7578 addr = tte_to_vaddr(hmeblkp, tte);
7726 7579
7727 7580 ttemod = tte;
7728 7581 TTE_CLR_WRT(&ttemod);
7729 7582 TTE_CLR_MOD(&ttemod);
7730 7583 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte);
7731 7584
7732 7585 /*
7733 7586 * if cas failed and the new value is not what
7734 7587 * we want retry
7735 7588 */
7736 7589 if (ret < 0)
7737 7590 goto retry;
7738 7591
7739 7592 /* we win the cas */
7740 7593 if (ret > 0) {
7741 7594 if (hmeblkp->hblk_shared) {
7742 7595 sf_srd_t *srdp = (sf_srd_t *)sfmmup;
7743 7596 uint_t rid = hmeblkp->hblk_tag.htag_rid;
7744 7597 sf_region_t *rgnp;
7745 7598 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
7746 7599 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
7747 7600 ASSERT(srdp != NULL);
7748 7601 rgnp = srdp->srd_hmergnp[rid];
7749 7602 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp,
7750 7603 srdp, rgnp, rid);
7751 7604 cpuset = sfmmu_rgntlb_demap(addr,
7752 7605 rgnp, hmeblkp, 1);
7753 7606 } else {
7754 7607 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0);
7755 7608 cpuset = sfmmup->sfmmu_cpusran;
7756 7609 }
7757 7610 }
7758 7611 }
7759 7612
7760 7613 return (cpuset);
7761 7614 }
7762 7615
7763 7616 /*
7764 7617 * Walk all mappings of a page, removing write permission and clearing the
7765 7618 * ref/mod bits. This code is similar to hat_pagesync()
7766 7619 */
7767 7620 static void
7768 7621 hat_page_clrwrt(page_t *pp)
7769 7622 {
7770 7623 struct sf_hment *sfhme;
7771 7624 struct sf_hment *tmphme = NULL;
7772 7625 kmutex_t *pml;
7773 7626 cpuset_t cpuset;
7774 7627 cpuset_t tset;
7775 7628 int index;
7776 7629 int cons;
7777 7630
7778 7631 CPUSET_ZERO(cpuset);
7779 7632
7780 7633 pml = sfmmu_mlist_enter(pp);
7781 7634 index = PP_MAPINDEX(pp);
7782 7635 cons = TTE8K;
7783 7636 retry:
7784 7637 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) {
7785 7638 tmphme = sfhme->hme_next;
7786 7639
7787 7640 /*
7788 7641 * If we are looking for large mappings and this hme doesn't
7789 7642 * reach the range we are seeking, just ignore its.
7790 7643 */
7791 7644
7792 7645 if (hme_size(sfhme) < cons)
7793 7646 continue;
7794 7647
7795 7648 tset = sfmmu_pageclrwrt(pp, sfhme);
7796 7649 CPUSET_OR(cpuset, tset);
7797 7650 }
7798 7651
7799 7652 while (index) {
7800 7653 index = index >> 1;
7801 7654 cons++;
7802 7655 if (index & 0x1) {
7803 7656 /* Go to leading page */
7804 7657 pp = PP_GROUPLEADER(pp, cons);
7805 7658 goto retry;
7806 7659 }
7807 7660 }
7808 7661
7809 7662 xt_sync(cpuset);
7810 7663 sfmmu_mlist_exit(pml);
7811 7664 }
7812 7665
7813 7666 /*
7814 7667 * Set the given REF/MOD/RO bits for the given page.
7815 7668 * For a vnode with a sorted v_pages list, we need to change
7816 7669 * the attributes and the v_pages list together under page_vnode_mutex.
7817 7670 */
7818 7671 void
7819 7672 hat_page_setattr(page_t *pp, uint_t flag)
7820 7673 {
7821 7674 vnode_t *vp = pp->p_vnode;
7822 7675 page_t **listp;
7823 7676 kmutex_t *pmtx;
7824 7677 kmutex_t *vphm = NULL;
7825 7678 int noshuffle;
7826 7679
7827 7680 noshuffle = flag & P_NSH;
7828 7681 flag &= ~P_NSH;
7829 7682
7830 7683 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO)));
7831 7684
7832 7685 /*
7833 7686 * nothing to do if attribute already set
7834 7687 */
7835 7688 if ((pp->p_nrm & flag) == flag)
7836 7689 return;
7837 7690
7838 7691 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp) &&
7839 7692 !noshuffle) {
7840 7693 vphm = page_vnode_mutex(vp);
7841 7694 mutex_enter(vphm);
7842 7695 }
7843 7696
7844 7697 pmtx = sfmmu_page_enter(pp);
7845 7698 pp->p_nrm |= flag;
7846 7699 sfmmu_page_exit(pmtx);
7847 7700
7848 7701 if (vphm != NULL) {
7849 7702 /*
7850 7703 * Some File Systems examine v_pages for NULL w/o
7851 7704 * grabbing the vphm mutex. Must not let it become NULL when
7852 7705 * pp is the only page on the list.
7853 7706 */
7854 7707 if (pp->p_vpnext != pp) {
7855 7708 page_vpsub(&vp->v_pages, pp);
7856 7709 if (vp->v_pages != NULL)
7857 7710 listp = &vp->v_pages->p_vpprev->p_vpnext;
7858 7711 else
7859 7712 listp = &vp->v_pages;
7860 7713 page_vpadd(listp, pp);
7861 7714 }
7862 7715 mutex_exit(vphm);
7863 7716 }
7864 7717 }
7865 7718
7866 7719 void
7867 7720 hat_page_clrattr(page_t *pp, uint_t flag)
7868 7721 {
7869 7722 vnode_t *vp = pp->p_vnode;
7870 7723 kmutex_t *pmtx;
7871 7724
7872 7725 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO)));
7873 7726
7874 7727 pmtx = sfmmu_page_enter(pp);
7875 7728
7876 7729 /*
7877 7730 * Caller is expected to hold page's io lock for VMODSORT to work
7878 7731 * correctly with pvn_vplist_dirty() and pvn_getdirty() when mod
7879 7732 * bit is cleared.
7880 7733 * We don't have assert to avoid tripping some existing third party
7881 7734 * code. The dirty page is moved back to top of the v_page list
7882 7735 * after IO is done in pvn_write_done().
7883 7736 */
7884 7737 pp->p_nrm &= ~flag;
7885 7738 sfmmu_page_exit(pmtx);
7886 7739
7887 7740 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp)) {
7888 7741
7889 7742 /*
7890 7743 * VMODSORT works by removing write permissions and getting
7891 7744 * a fault when a page is made dirty. At this point
7892 7745 * we need to remove write permission from all mappings
7893 7746 * to this page.
7894 7747 */
7895 7748 hat_page_clrwrt(pp);
7896 7749 }
7897 7750 }
7898 7751
7899 7752 uint_t
7900 7753 hat_page_getattr(page_t *pp, uint_t flag)
7901 7754 {
7902 7755 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO)));
7903 7756 return ((uint_t)(pp->p_nrm & flag));
7904 7757 }
7905 7758
7906 7759 /*
7907 7760 * DEBUG kernels: verify that a kernel va<->pa translation
7908 7761 * is safe by checking the underlying page_t is in a page
7909 7762 * relocation-safe state.
7910 7763 */
7911 7764 #ifdef DEBUG
7912 7765 void
7913 7766 sfmmu_check_kpfn(pfn_t pfn)
7914 7767 {
7915 7768 page_t *pp;
7916 7769 int index, cons;
7917 7770
7918 7771 if (hat_check_vtop == 0)
7919 7772 return;
7920 7773
7921 7774 if (kvseg.s_base == NULL || panicstr)
7922 7775 return;
7923 7776
7924 7777 pp = page_numtopp_nolock(pfn);
7925 7778 if (!pp)
7926 7779 return;
7927 7780
7928 7781 if (PAGE_LOCKED(pp) || PP_ISNORELOC(pp))
7929 7782 return;
7930 7783
7931 7784 /*
7932 7785 * Handed a large kernel page, we dig up the root page since we
7933 7786 * know the root page might have the lock also.
7934 7787 */
7935 7788 if (pp->p_szc != 0) {
7936 7789 index = PP_MAPINDEX(pp);
7937 7790 cons = TTE8K;
7938 7791 again:
7939 7792 while (index != 0) {
7940 7793 index >>= 1;
7941 7794 if (index != 0)
7942 7795 cons++;
7943 7796 if (index & 0x1) {
7944 7797 pp = PP_GROUPLEADER(pp, cons);
7945 7798 goto again;
7946 7799 }
7947 7800 }
7948 7801 }
7949 7802
7950 7803 if (PAGE_LOCKED(pp) || PP_ISNORELOC(pp))
7951 7804 return;
7952 7805
7953 7806 /*
7954 7807 * Pages need to be locked or allocated "permanent" (either from
7955 7808 * static_arena arena or explicitly setting PG_NORELOC when calling
7956 7809 * page_create_va()) for VA->PA translations to be valid.
7957 7810 */
7958 7811 if (!PP_ISNORELOC(pp))
7959 7812 panic("Illegal VA->PA translation, pp 0x%p not permanent",
7960 7813 (void *)pp);
7961 7814 else
7962 7815 panic("Illegal VA->PA translation, pp 0x%p not locked",
7963 7816 (void *)pp);
7964 7817 }
7965 7818 #endif /* DEBUG */
7966 7819
7967 7820 /*
7968 7821 * Returns a page frame number for a given virtual address.
7969 7822 * Returns PFN_INVALID to indicate an invalid mapping
7970 7823 */
7971 7824 pfn_t
7972 7825 hat_getpfnum(struct hat *hat, caddr_t addr)
7973 7826 {
7974 7827 pfn_t pfn;
7975 7828 tte_t tte;
7976 7829
7977 7830 /*
7978 7831 * We would like to
7979 7832 * ASSERT(AS_LOCK_HELD(as, &as->a_lock));
7980 7833 * but we can't because the iommu driver will call this
7981 7834 * routine at interrupt time and it can't grab the as lock
7982 7835 * or it will deadlock: A thread could have the as lock
7983 7836 * and be waiting for io. The io can't complete
7984 7837 * because the interrupt thread is blocked trying to grab
7985 7838 * the as lock.
7986 7839 */
7987 7840
7988 7841 ASSERT(hat->sfmmu_xhat_provider == NULL);
7989 7842
7990 7843 if (hat == ksfmmup) {
7991 7844 if (IS_KMEM_VA_LARGEPAGE(addr)) {
7992 7845 ASSERT(segkmem_lpszc > 0);
7993 7846 pfn = sfmmu_kvaszc2pfn(addr, segkmem_lpszc);
7994 7847 if (pfn != PFN_INVALID) {
7995 7848 sfmmu_check_kpfn(pfn);
7996 7849 return (pfn);
7997 7850 }
7998 7851 } else if (segkpm && IS_KPM_ADDR(addr)) {
7999 7852 return (sfmmu_kpm_vatopfn(addr));
8000 7853 }
8001 7854 while ((pfn = sfmmu_vatopfn(addr, ksfmmup, &tte))
8002 7855 == PFN_SUSPENDED) {
8003 7856 sfmmu_vatopfn_suspended(addr, ksfmmup, &tte);
8004 7857 }
8005 7858 sfmmu_check_kpfn(pfn);
8006 7859 return (pfn);
8007 7860 } else {
8008 7861 return (sfmmu_uvatopfn(addr, hat, NULL));
8009 7862 }
8010 7863 }
8011 7864
8012 7865 /*
8013 7866 * This routine will return both pfn and tte for the vaddr.
8014 7867 */
8015 7868 static pfn_t
8016 7869 sfmmu_uvatopfn(caddr_t vaddr, struct hat *sfmmup, tte_t *ttep)
8017 7870 {
8018 7871 struct hmehash_bucket *hmebp;
8019 7872 hmeblk_tag hblktag;
8020 7873 int hmeshift, hashno = 1;
8021 7874 struct hme_blk *hmeblkp = NULL;
8022 7875 tte_t tte;
8023 7876
8024 7877 struct sf_hment *sfhmep;
8025 7878 pfn_t pfn;
8026 7879
8027 7880 /* support for ISM */
8028 7881 ism_map_t *ism_map;
8029 7882 ism_blk_t *ism_blkp;
8030 7883 int i;
8031 7884 sfmmu_t *ism_hatid = NULL;
8032 7885 sfmmu_t *locked_hatid = NULL;
8033 7886 sfmmu_t *sv_sfmmup = sfmmup;
8034 7887 caddr_t sv_vaddr = vaddr;
8035 7888 sf_srd_t *srdp;
8036 7889
8037 7890 if (ttep == NULL) {
8038 7891 ttep = &tte;
8039 7892 } else {
8040 7893 ttep->ll = 0;
8041 7894 }
8042 7895
8043 7896 ASSERT(sfmmup != ksfmmup);
8044 7897 SFMMU_STAT(sf_user_vtop);
8045 7898 /*
8046 7899 * Set ism_hatid if vaddr falls in a ISM segment.
8047 7900 */
8048 7901 ism_blkp = sfmmup->sfmmu_iblk;
8049 7902 if (ism_blkp != NULL) {
8050 7903 sfmmu_ismhat_enter(sfmmup, 0);
8051 7904 locked_hatid = sfmmup;
8052 7905 }
8053 7906 while (ism_blkp != NULL && ism_hatid == NULL) {
8054 7907 ism_map = ism_blkp->iblk_maps;
8055 7908 for (i = 0; ism_map[i].imap_ismhat && i < ISM_MAP_SLOTS; i++) {
8056 7909 if (vaddr >= ism_start(ism_map[i]) &&
8057 7910 vaddr < ism_end(ism_map[i])) {
8058 7911 sfmmup = ism_hatid = ism_map[i].imap_ismhat;
8059 7912 vaddr = (caddr_t)(vaddr -
8060 7913 ism_start(ism_map[i]));
8061 7914 break;
8062 7915 }
8063 7916 }
8064 7917 ism_blkp = ism_blkp->iblk_next;
8065 7918 }
8066 7919 if (locked_hatid) {
8067 7920 sfmmu_ismhat_exit(locked_hatid, 0);
8068 7921 }
8069 7922
8070 7923 hblktag.htag_id = sfmmup;
8071 7924 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
8072 7925 do {
8073 7926 hmeshift = HME_HASH_SHIFT(hashno);
8074 7927 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift);
8075 7928 hblktag.htag_rehash = hashno;
8076 7929 hmebp = HME_HASH_FUNCTION(sfmmup, vaddr, hmeshift);
8077 7930
8078 7931 SFMMU_HASH_LOCK(hmebp);
8079 7932
8080 7933 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp);
8081 7934 if (hmeblkp != NULL) {
8082 7935 ASSERT(!hmeblkp->hblk_shared);
8083 7936 HBLKTOHME(sfhmep, hmeblkp, vaddr);
8084 7937 sfmmu_copytte(&sfhmep->hme_tte, ttep);
8085 7938 SFMMU_HASH_UNLOCK(hmebp);
8086 7939 if (TTE_IS_VALID(ttep)) {
8087 7940 pfn = TTE_TO_PFN(vaddr, ttep);
8088 7941 return (pfn);
8089 7942 }
8090 7943 break;
8091 7944 }
8092 7945 SFMMU_HASH_UNLOCK(hmebp);
8093 7946 hashno++;
8094 7947 } while (HME_REHASH(sfmmup) && (hashno <= mmu_hashcnt));
8095 7948
8096 7949 if (SF_HMERGNMAP_ISNULL(sv_sfmmup)) {
8097 7950 return (PFN_INVALID);
8098 7951 }
8099 7952 srdp = sv_sfmmup->sfmmu_srdp;
8100 7953 ASSERT(srdp != NULL);
8101 7954 ASSERT(srdp->srd_refcnt != 0);
8102 7955 hblktag.htag_id = srdp;
8103 7956 hashno = 1;
8104 7957 do {
8105 7958 hmeshift = HME_HASH_SHIFT(hashno);
8106 7959 hblktag.htag_bspage = HME_HASH_BSPAGE(sv_vaddr, hmeshift);
8107 7960 hblktag.htag_rehash = hashno;
8108 7961 hmebp = HME_HASH_FUNCTION(srdp, sv_vaddr, hmeshift);
8109 7962
8110 7963 SFMMU_HASH_LOCK(hmebp);
8111 7964 for (hmeblkp = hmebp->hmeblkp; hmeblkp != NULL;
8112 7965 hmeblkp = hmeblkp->hblk_next) {
8113 7966 uint_t rid;
8114 7967 sf_region_t *rgnp;
8115 7968 caddr_t rsaddr;
8116 7969 caddr_t readdr;
8117 7970
8118 7971 if (!HTAGS_EQ_SHME(hmeblkp->hblk_tag, hblktag,
8119 7972 sv_sfmmup->sfmmu_hmeregion_map)) {
8120 7973 continue;
8121 7974 }
8122 7975 ASSERT(hmeblkp->hblk_shared);
8123 7976 rid = hmeblkp->hblk_tag.htag_rid;
8124 7977 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
8125 7978 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
8126 7979 rgnp = srdp->srd_hmergnp[rid];
8127 7980 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid);
8128 7981 HBLKTOHME(sfhmep, hmeblkp, sv_vaddr);
8129 7982 sfmmu_copytte(&sfhmep->hme_tte, ttep);
8130 7983 rsaddr = rgnp->rgn_saddr;
8131 7984 readdr = rsaddr + rgnp->rgn_size;
8132 7985 #ifdef DEBUG
8133 7986 if (TTE_IS_VALID(ttep) ||
8134 7987 get_hblk_ttesz(hmeblkp) > TTE8K) {
8135 7988 caddr_t eva = tte_to_evaddr(hmeblkp, ttep);
8136 7989 ASSERT(eva > sv_vaddr);
8137 7990 ASSERT(sv_vaddr >= rsaddr);
8138 7991 ASSERT(sv_vaddr < readdr);
8139 7992 ASSERT(eva <= readdr);
8140 7993 }
8141 7994 #endif /* DEBUG */
8142 7995 /*
8143 7996 * Continue the search if we
8144 7997 * found an invalid 8K tte outside of the area
8145 7998 * covered by this hmeblk's region.
8146 7999 */
8147 8000 if (TTE_IS_VALID(ttep)) {
8148 8001 SFMMU_HASH_UNLOCK(hmebp);
8149 8002 pfn = TTE_TO_PFN(sv_vaddr, ttep);
8150 8003 return (pfn);
8151 8004 } else if (get_hblk_ttesz(hmeblkp) > TTE8K ||
8152 8005 (sv_vaddr >= rsaddr && sv_vaddr < readdr)) {
8153 8006 SFMMU_HASH_UNLOCK(hmebp);
8154 8007 pfn = PFN_INVALID;
8155 8008 return (pfn);
8156 8009 }
8157 8010 }
8158 8011 SFMMU_HASH_UNLOCK(hmebp);
8159 8012 hashno++;
8160 8013 } while (hashno <= mmu_hashcnt);
8161 8014 return (PFN_INVALID);
8162 8015 }
8163 8016
8164 8017
8165 8018 /*
8166 8019 * For compatability with AT&T and later optimizations
8167 8020 */
8168 8021 /* ARGSUSED */
8169 8022 void
8170 8023 hat_map(struct hat *hat, caddr_t addr, size_t len, uint_t flags)
8171 8024 {
8172 8025 ASSERT(hat != NULL);
8173 8026 ASSERT(hat->sfmmu_xhat_provider == NULL);
8174 8027 }
8175 8028
8176 8029 /*
8177 8030 * Return the number of mappings to a particular page. This number is an
8178 8031 * approximation of the number of people sharing the page.
8179 8032 *
8180 8033 * shared hmeblks or ism hmeblks are counted as 1 mapping here.
8181 8034 * hat_page_checkshare() can be used to compare threshold to share
8182 8035 * count that reflects the number of region sharers albeit at higher cost.
8183 8036 */
8184 8037 ulong_t
8185 8038 hat_page_getshare(page_t *pp)
8186 8039 {
8187 8040 page_t *spp = pp; /* start page */
8188 8041 kmutex_t *pml;
8189 8042 ulong_t cnt;
8190 8043 int index, sz = TTE64K;
8191 8044
8192 8045 /*
8193 8046 * We need to grab the mlist lock to make sure any outstanding
8194 8047 * load/unloads complete. Otherwise we could return zero
8195 8048 * even though the unload(s) hasn't finished yet.
8196 8049 */
8197 8050 pml = sfmmu_mlist_enter(spp);
8198 8051 cnt = spp->p_share;
8199 8052
8200 8053 #ifdef VAC
8201 8054 if (kpm_enable)
8202 8055 cnt += spp->p_kpmref;
8203 8056 #endif
8204 8057 if (vpm_enable && pp->p_vpmref) {
8205 8058 cnt += 1;
8206 8059 }
8207 8060
8208 8061 /*
8209 8062 * If we have any large mappings, we count the number of
8210 8063 * mappings that this large page is part of.
8211 8064 */
8212 8065 index = PP_MAPINDEX(spp);
8213 8066 index >>= 1;
8214 8067 while (index) {
8215 8068 pp = PP_GROUPLEADER(spp, sz);
8216 8069 if ((index & 0x1) && pp != spp) {
8217 8070 cnt += pp->p_share;
8218 8071 spp = pp;
8219 8072 }
8220 8073 index >>= 1;
8221 8074 sz++;
8222 8075 }
8223 8076 sfmmu_mlist_exit(pml);
8224 8077 return (cnt);
8225 8078 }
8226 8079
8227 8080 /*
8228 8081 * Return 1 if the number of mappings exceeds sh_thresh. Return 0
8229 8082 * otherwise. Count shared hmeblks by region's refcnt.
8230 8083 */
8231 8084 int
8232 8085 hat_page_checkshare(page_t *pp, ulong_t sh_thresh)
8233 8086 {
8234 8087 kmutex_t *pml;
8235 8088 ulong_t cnt = 0;
8236 8089 int index, sz = TTE8K;
8237 8090 struct sf_hment *sfhme, *tmphme = NULL;
8238 8091 struct hme_blk *hmeblkp;
8239 8092
8240 8093 pml = sfmmu_mlist_enter(pp);
8241 8094
8242 8095 #ifdef VAC
8243 8096 if (kpm_enable)
8244 8097 cnt = pp->p_kpmref;
8245 8098 #endif
8246 8099
8247 8100 if (vpm_enable && pp->p_vpmref) {
8248 8101 cnt += 1;
8249 8102 }
8250 8103
8251 8104 if (pp->p_share + cnt > sh_thresh) {
8252 8105 sfmmu_mlist_exit(pml);
8253 8106 return (1);
8254 8107 }
8255 8108
8256 8109 index = PP_MAPINDEX(pp);
8257 8110
8258 8111 again:
8259 8112 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) {
8260 8113 tmphme = sfhme->hme_next;
8261 8114 if (IS_PAHME(sfhme)) {
8262 8115 continue;
8263 8116 }
8264 8117
8265 8118 hmeblkp = sfmmu_hmetohblk(sfhme);
8266 8119 if (hmeblkp->hblk_xhat_bit) {
8267 8120 cnt++;
8268 8121 if (cnt > sh_thresh) {
8269 8122 sfmmu_mlist_exit(pml);
8270 8123 return (1);
8271 8124 }
8272 8125 continue;
8273 8126 }
8274 8127 if (hme_size(sfhme) != sz) {
8275 8128 continue;
8276 8129 }
8277 8130
8278 8131 if (hmeblkp->hblk_shared) {
8279 8132 sf_srd_t *srdp = hblktosrd(hmeblkp);
8280 8133 uint_t rid = hmeblkp->hblk_tag.htag_rid;
8281 8134 sf_region_t *rgnp;
8282 8135 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
8283 8136 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
8284 8137 ASSERT(srdp != NULL);
8285 8138 rgnp = srdp->srd_hmergnp[rid];
8286 8139 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp,
8287 8140 rgnp, rid);
8288 8141 cnt += rgnp->rgn_refcnt;
8289 8142 } else {
8290 8143 cnt++;
8291 8144 }
8292 8145 if (cnt > sh_thresh) {
8293 8146 sfmmu_mlist_exit(pml);
8294 8147 return (1);
8295 8148 }
8296 8149 }
8297 8150
8298 8151 index >>= 1;
8299 8152 sz++;
8300 8153 while (index) {
8301 8154 pp = PP_GROUPLEADER(pp, sz);
8302 8155 ASSERT(sfmmu_mlist_held(pp));
8303 8156 if (index & 0x1) {
8304 8157 goto again;
8305 8158 }
8306 8159 index >>= 1;
8307 8160 sz++;
8308 8161 }
8309 8162 sfmmu_mlist_exit(pml);
8310 8163 return (0);
8311 8164 }
8312 8165
8313 8166 /*
8314 8167 * Unload all large mappings to the pp and reset the p_szc field of every
8315 8168 * constituent page according to the remaining mappings.
8316 8169 *
8317 8170 * pp must be locked SE_EXCL. Even though no other constituent pages are
8318 8171 * locked it's legal to unload the large mappings to the pp because all
8319 8172 * constituent pages of large locked mappings have to be locked SE_SHARED.
8320 8173 * This means if we have SE_EXCL lock on one of constituent pages none of the
8321 8174 * large mappings to pp are locked.
8322 8175 *
8323 8176 * Decrease p_szc field starting from the last constituent page and ending
8324 8177 * with the root page. This method is used because other threads rely on the
8325 8178 * root's p_szc to find the lock to syncronize on. After a root page_t's p_szc
8326 8179 * is demoted then other threads will succeed in sfmmu_mlspl_enter(). This
8327 8180 * ensures that p_szc changes of the constituent pages appears atomic for all
8328 8181 * threads that use sfmmu_mlspl_enter() to examine p_szc field.
8329 8182 *
8330 8183 * This mechanism is only used for file system pages where it's not always
8331 8184 * possible to get SE_EXCL locks on all constituent pages to demote the size
8332 8185 * code (as is done for anonymous or kernel large pages).
8333 8186 *
8334 8187 * See more comments in front of sfmmu_mlspl_enter().
8335 8188 */
8336 8189 void
8337 8190 hat_page_demote(page_t *pp)
8338 8191 {
8339 8192 int index;
8340 8193 int sz;
8341 8194 cpuset_t cpuset;
8342 8195 int sync = 0;
8343 8196 page_t *rootpp;
8344 8197 struct sf_hment *sfhme;
8345 8198 struct sf_hment *tmphme = NULL;
8346 8199 struct hme_blk *hmeblkp;
8347 8200 uint_t pszc;
8348 8201 page_t *lastpp;
8349 8202 cpuset_t tset;
8350 8203 pgcnt_t npgs;
8351 8204 kmutex_t *pml;
8352 8205 kmutex_t *pmtx = NULL;
8353 8206
8354 8207 ASSERT(PAGE_EXCL(pp));
8355 8208 ASSERT(!PP_ISFREE(pp));
8356 8209 ASSERT(!PP_ISKAS(pp));
8357 8210 ASSERT(page_szc_lock_assert(pp));
8358 8211 pml = sfmmu_mlist_enter(pp);
8359 8212
8360 8213 pszc = pp->p_szc;
8361 8214 if (pszc == 0) {
8362 8215 goto out;
8363 8216 }
8364 8217
8365 8218 index = PP_MAPINDEX(pp) >> 1;
8366 8219
8367 8220 if (index) {
8368 8221 CPUSET_ZERO(cpuset);
8369 8222 sz = TTE64K;
8370 8223 sync = 1;
8371 8224 }
8372 8225
8373 8226 while (index) {
8374 8227 if (!(index & 0x1)) {
8375 8228 index >>= 1;
8376 8229 sz++;
8377 8230 continue;
8378 8231 }
8379 8232 ASSERT(sz <= pszc);
8380 8233 rootpp = PP_GROUPLEADER(pp, sz);
8381 8234 for (sfhme = rootpp->p_mapping; sfhme; sfhme = tmphme) {
8382 8235 tmphme = sfhme->hme_next;
8383 8236 ASSERT(!IS_PAHME(sfhme));
8384 8237 hmeblkp = sfmmu_hmetohblk(sfhme);
8385 8238 if (hme_size(sfhme) != sz) {
8386 8239 continue;
8387 8240 }
8388 8241 if (hmeblkp->hblk_xhat_bit) {
8389 8242 cmn_err(CE_PANIC,
8390 8243 "hat_page_demote: xhat hmeblk");
8391 8244 }
8392 8245 tset = sfmmu_pageunload(rootpp, sfhme, sz);
8393 8246 CPUSET_OR(cpuset, tset);
8394 8247 }
8395 8248 if (index >>= 1) {
8396 8249 sz++;
8397 8250 }
8398 8251 }
8399 8252
8400 8253 ASSERT(!PP_ISMAPPED_LARGE(pp));
8401 8254
8402 8255 if (sync) {
8403 8256 xt_sync(cpuset);
8404 8257 #ifdef VAC
8405 8258 if (PP_ISTNC(pp)) {
8406 8259 conv_tnc(rootpp, sz);
8407 8260 }
8408 8261 #endif /* VAC */
8409 8262 }
8410 8263
8411 8264 pmtx = sfmmu_page_enter(pp);
8412 8265
8413 8266 ASSERT(pp->p_szc == pszc);
8414 8267 rootpp = PP_PAGEROOT(pp);
8415 8268 ASSERT(rootpp->p_szc == pszc);
8416 8269 lastpp = PP_PAGENEXT_N(rootpp, TTEPAGES(pszc) - 1);
8417 8270
8418 8271 while (lastpp != rootpp) {
8419 8272 sz = PP_MAPINDEX(lastpp) ? fnd_mapping_sz(lastpp) : 0;
8420 8273 ASSERT(sz < pszc);
8421 8274 npgs = (sz == 0) ? 1 : TTEPAGES(sz);
8422 8275 ASSERT(P2PHASE(lastpp->p_pagenum, npgs) == npgs - 1);
8423 8276 while (--npgs > 0) {
8424 8277 lastpp->p_szc = (uchar_t)sz;
8425 8278 lastpp = PP_PAGEPREV(lastpp);
8426 8279 }
8427 8280 if (sz) {
8428 8281 /*
8429 8282 * make sure before current root's pszc
8430 8283 * is updated all updates to constituent pages pszc
8431 8284 * fields are globally visible.
8432 8285 */
8433 8286 membar_producer();
8434 8287 }
8435 8288 lastpp->p_szc = sz;
8436 8289 ASSERT(IS_P2ALIGNED(lastpp->p_pagenum, TTEPAGES(sz)));
8437 8290 if (lastpp != rootpp) {
8438 8291 lastpp = PP_PAGEPREV(lastpp);
8439 8292 }
8440 8293 }
8441 8294 if (sz == 0) {
8442 8295 /* the loop above doesn't cover this case */
8443 8296 rootpp->p_szc = 0;
8444 8297 }
8445 8298 out:
8446 8299 ASSERT(pp->p_szc == 0);
8447 8300 if (pmtx != NULL) {
8448 8301 sfmmu_page_exit(pmtx);
8449 8302 }
8450 8303 sfmmu_mlist_exit(pml);
8451 8304 }
8452 8305
8453 8306 /*
8454 8307 * Refresh the HAT ismttecnt[] element for size szc.
8455 8308 * Caller must have set ISM busy flag to prevent mapping
8456 8309 * lists from changing while we're traversing them.
8457 8310 */
8458 8311 pgcnt_t
8459 8312 ism_tsb_entries(sfmmu_t *sfmmup, int szc)
8460 8313 {
8461 8314 ism_blk_t *ism_blkp = sfmmup->sfmmu_iblk;
8462 8315 ism_map_t *ism_map;
8463 8316 pgcnt_t npgs = 0;
8464 8317 pgcnt_t npgs_scd = 0;
8465 8318 int j;
8466 8319 sf_scd_t *scdp;
8467 8320 uchar_t rid;
8468 8321
8469 8322 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY));
8470 8323 scdp = sfmmup->sfmmu_scdp;
8471 8324
8472 8325 for (; ism_blkp != NULL; ism_blkp = ism_blkp->iblk_next) {
8473 8326 ism_map = ism_blkp->iblk_maps;
8474 8327 for (j = 0; ism_map[j].imap_ismhat && j < ISM_MAP_SLOTS; j++) {
8475 8328 rid = ism_map[j].imap_rid;
8476 8329 ASSERT(rid == SFMMU_INVALID_ISMRID ||
8477 8330 rid < sfmmup->sfmmu_srdp->srd_next_ismrid);
8478 8331
8479 8332 if (scdp != NULL && rid != SFMMU_INVALID_ISMRID &&
8480 8333 SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid)) {
8481 8334 /* ISM is in sfmmup's SCD */
8482 8335 npgs_scd +=
8483 8336 ism_map[j].imap_ismhat->sfmmu_ttecnt[szc];
8484 8337 } else {
8485 8338 /* ISMs is not in SCD */
8486 8339 npgs +=
8487 8340 ism_map[j].imap_ismhat->sfmmu_ttecnt[szc];
8488 8341 }
8489 8342 }
8490 8343 }
8491 8344 sfmmup->sfmmu_ismttecnt[szc] = npgs;
8492 8345 sfmmup->sfmmu_scdismttecnt[szc] = npgs_scd;
8493 8346 return (npgs);
8494 8347 }
8495 8348
8496 8349 /*
8497 8350 * Yield the memory claim requirement for an address space.
8498 8351 *
8499 8352 * This is currently implemented as the number of bytes that have active
8500 8353 * hardware translations that have page structures. Therefore, it can
8501 8354 * underestimate the traditional resident set size, eg, if the
8502 8355 * physical page is present and the hardware translation is missing;
8503 8356 * and it can overestimate the rss, eg, if there are active
8504 8357 * translations to a frame buffer with page structs.
8505 8358 * Also, it does not take sharing into account.
8506 8359 *
8507 8360 * Note that we don't acquire locks here since this function is most often
8508 8361 * called from the clock thread.
8509 8362 */
8510 8363 size_t
8511 8364 hat_get_mapped_size(struct hat *hat)
8512 8365 {
8513 8366 size_t assize = 0;
8514 8367 int i;
8515 8368
8516 8369 if (hat == NULL)
8517 8370 return (0);
8518 8371
8519 8372 ASSERT(hat->sfmmu_xhat_provider == NULL);
8520 8373
8521 8374 for (i = 0; i < mmu_page_sizes; i++)
8522 8375 assize += ((pgcnt_t)hat->sfmmu_ttecnt[i] +
8523 8376 (pgcnt_t)hat->sfmmu_scdrttecnt[i]) * TTEBYTES(i);
8524 8377
8525 8378 if (hat->sfmmu_iblk == NULL)
8526 8379 return (assize);
8527 8380
8528 8381 for (i = 0; i < mmu_page_sizes; i++)
8529 8382 assize += ((pgcnt_t)hat->sfmmu_ismttecnt[i] +
8530 8383 (pgcnt_t)hat->sfmmu_scdismttecnt[i]) * TTEBYTES(i);
8531 8384
8532 8385 return (assize);
8533 8386 }
8534 8387
8535 8388 int
8536 8389 hat_stats_enable(struct hat *hat)
8537 8390 {
8538 8391 hatlock_t *hatlockp;
8539 8392
8540 8393 ASSERT(hat->sfmmu_xhat_provider == NULL);
8541 8394
8542 8395 hatlockp = sfmmu_hat_enter(hat);
8543 8396 hat->sfmmu_rmstat++;
8544 8397 sfmmu_hat_exit(hatlockp);
8545 8398 return (1);
8546 8399 }
8547 8400
8548 8401 void
8549 8402 hat_stats_disable(struct hat *hat)
8550 8403 {
8551 8404 hatlock_t *hatlockp;
8552 8405
8553 8406 ASSERT(hat->sfmmu_xhat_provider == NULL);
8554 8407
8555 8408 hatlockp = sfmmu_hat_enter(hat);
8556 8409 hat->sfmmu_rmstat--;
8557 8410 sfmmu_hat_exit(hatlockp);
8558 8411 }
8559 8412
8560 8413 /*
8561 8414 * Routines for entering or removing ourselves from the
8562 8415 * ism_hat's mapping list. This is used for both private and
8563 8416 * SCD hats.
8564 8417 */
8565 8418 static void
8566 8419 iment_add(struct ism_ment *iment, struct hat *ism_hat)
8567 8420 {
8568 8421 ASSERT(MUTEX_HELD(&ism_mlist_lock));
8569 8422
8570 8423 iment->iment_prev = NULL;
8571 8424 iment->iment_next = ism_hat->sfmmu_iment;
8572 8425 if (ism_hat->sfmmu_iment) {
8573 8426 ism_hat->sfmmu_iment->iment_prev = iment;
8574 8427 }
8575 8428 ism_hat->sfmmu_iment = iment;
8576 8429 }
8577 8430
8578 8431 static void
8579 8432 iment_sub(struct ism_ment *iment, struct hat *ism_hat)
8580 8433 {
8581 8434 ASSERT(MUTEX_HELD(&ism_mlist_lock));
8582 8435
8583 8436 if (ism_hat->sfmmu_iment == NULL) {
8584 8437 panic("ism map entry remove - no entries");
8585 8438 }
8586 8439
8587 8440 if (iment->iment_prev) {
8588 8441 ASSERT(ism_hat->sfmmu_iment != iment);
8589 8442 iment->iment_prev->iment_next = iment->iment_next;
8590 8443 } else {
8591 8444 ASSERT(ism_hat->sfmmu_iment == iment);
8592 8445 ism_hat->sfmmu_iment = iment->iment_next;
8593 8446 }
8594 8447
8595 8448 if (iment->iment_next) {
8596 8449 iment->iment_next->iment_prev = iment->iment_prev;
8597 8450 }
8598 8451
8599 8452 /*
8600 8453 * zero out the entry
8601 8454 */
8602 8455 iment->iment_next = NULL;
8603 8456 iment->iment_prev = NULL;
8604 8457 iment->iment_hat = NULL;
8605 8458 iment->iment_base_va = 0;
8606 8459 }
8607 8460
8608 8461 /*
8609 8462 * Hat_share()/unshare() return an (non-zero) error
8610 8463 * when saddr and daddr are not properly aligned.
8611 8464 *
8612 8465 * The top level mapping element determines the alignment
8613 8466 * requirement for saddr and daddr, depending on different
8614 8467 * architectures.
8615 8468 *
8616 8469 * When hat_share()/unshare() are not supported,
8617 8470 * HATOP_SHARE()/UNSHARE() return 0
8618 8471 */
8619 8472 int
8620 8473 hat_share(struct hat *sfmmup, caddr_t addr,
8621 8474 struct hat *ism_hatid, caddr_t sptaddr, size_t len, uint_t ismszc)
8622 8475 {
8623 8476 ism_blk_t *ism_blkp;
8624 8477 ism_blk_t *new_iblk;
8625 8478 ism_map_t *ism_map;
8626 8479 ism_ment_t *ism_ment;
8627 8480 int i, added;
8628 8481 hatlock_t *hatlockp;
8629 8482 int reload_mmu = 0;
8630 8483 uint_t ismshift = page_get_shift(ismszc);
8631 8484 size_t ismpgsz = page_get_pagesize(ismszc);
8632 8485 uint_t ismmask = (uint_t)ismpgsz - 1;
8633 8486 size_t sh_size = ISM_SHIFT(ismshift, len);
8634 8487 ushort_t ismhatflag;
8635 8488 hat_region_cookie_t rcookie;
8636 8489 sf_scd_t *old_scdp;
8637 8490
8638 8491 #ifdef DEBUG
8639 8492 caddr_t eaddr = addr + len;
8640 8493 #endif /* DEBUG */
8641 8494
8642 8495 ASSERT(ism_hatid != NULL && sfmmup != NULL);
8643 8496 ASSERT(sptaddr == ISMID_STARTADDR);
8644 8497 /*
8645 8498 * Check the alignment.
8646 8499 */
8647 8500 if (!ISM_ALIGNED(ismshift, addr) || !ISM_ALIGNED(ismshift, sptaddr))
8648 8501 return (EINVAL);
8649 8502
8650 8503 /*
8651 8504 * Check size alignment.
8652 8505 */
8653 8506 if (!ISM_ALIGNED(ismshift, len))
8654 8507 return (EINVAL);
8655 8508
8656 8509 ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
8657 8510
8658 8511 /*
8659 8512 * Allocate ism_ment for the ism_hat's mapping list, and an
8660 8513 * ism map blk in case we need one. We must do our
8661 8514 * allocations before acquiring locks to prevent a deadlock
8662 8515 * in the kmem allocator on the mapping list lock.
8663 8516 */
8664 8517 new_iblk = kmem_cache_alloc(ism_blk_cache, KM_SLEEP);
8665 8518 ism_ment = kmem_cache_alloc(ism_ment_cache, KM_SLEEP);
8666 8519
8667 8520 /*
8668 8521 * Serialize ISM mappings with the ISM busy flag, and also the
8669 8522 * trap handlers.
8670 8523 */
8671 8524 sfmmu_ismhat_enter(sfmmup, 0);
8672 8525
8673 8526 /*
8674 8527 * Allocate an ism map blk if necessary.
8675 8528 */
8676 8529 if (sfmmup->sfmmu_iblk == NULL) {
8677 8530 sfmmup->sfmmu_iblk = new_iblk;
8678 8531 bzero(new_iblk, sizeof (*new_iblk));
8679 8532 new_iblk->iblk_nextpa = (uint64_t)-1;
8680 8533 membar_stst(); /* make sure next ptr visible to all CPUs */
8681 8534 sfmmup->sfmmu_ismblkpa = va_to_pa((caddr_t)new_iblk);
8682 8535 reload_mmu = 1;
8683 8536 new_iblk = NULL;
8684 8537 }
8685 8538
8686 8539 #ifdef DEBUG
8687 8540 /*
8688 8541 * Make sure mapping does not already exist.
8689 8542 */
8690 8543 ism_blkp = sfmmup->sfmmu_iblk;
8691 8544 while (ism_blkp != NULL) {
8692 8545 ism_map = ism_blkp->iblk_maps;
8693 8546 for (i = 0; i < ISM_MAP_SLOTS && ism_map[i].imap_ismhat; i++) {
8694 8547 if ((addr >= ism_start(ism_map[i]) &&
8695 8548 addr < ism_end(ism_map[i])) ||
8696 8549 eaddr > ism_start(ism_map[i]) &&
8697 8550 eaddr <= ism_end(ism_map[i])) {
8698 8551 panic("sfmmu_share: Already mapped!");
8699 8552 }
8700 8553 }
8701 8554 ism_blkp = ism_blkp->iblk_next;
8702 8555 }
8703 8556 #endif /* DEBUG */
8704 8557
8705 8558 ASSERT(ismszc >= TTE4M);
8706 8559 if (ismszc == TTE4M) {
8707 8560 ismhatflag = HAT_4M_FLAG;
8708 8561 } else if (ismszc == TTE32M) {
8709 8562 ismhatflag = HAT_32M_FLAG;
8710 8563 } else if (ismszc == TTE256M) {
8711 8564 ismhatflag = HAT_256M_FLAG;
8712 8565 }
8713 8566 /*
8714 8567 * Add mapping to first available mapping slot.
8715 8568 */
8716 8569 ism_blkp = sfmmup->sfmmu_iblk;
8717 8570 added = 0;
8718 8571 while (!added) {
8719 8572 ism_map = ism_blkp->iblk_maps;
8720 8573 for (i = 0; i < ISM_MAP_SLOTS; i++) {
8721 8574 if (ism_map[i].imap_ismhat == NULL) {
8722 8575
8723 8576 ism_map[i].imap_ismhat = ism_hatid;
8724 8577 ism_map[i].imap_vb_shift = (uchar_t)ismshift;
8725 8578 ism_map[i].imap_rid = SFMMU_INVALID_ISMRID;
8726 8579 ism_map[i].imap_hatflags = ismhatflag;
8727 8580 ism_map[i].imap_sz_mask = ismmask;
8728 8581 /*
8729 8582 * imap_seg is checked in ISM_CHECK to see if
8730 8583 * non-NULL, then other info assumed valid.
8731 8584 */
8732 8585 membar_stst();
8733 8586 ism_map[i].imap_seg = (uintptr_t)addr | sh_size;
8734 8587 ism_map[i].imap_ment = ism_ment;
8735 8588
8736 8589 /*
8737 8590 * Now add ourselves to the ism_hat's
8738 8591 * mapping list.
8739 8592 */
8740 8593 ism_ment->iment_hat = sfmmup;
8741 8594 ism_ment->iment_base_va = addr;
8742 8595 ism_hatid->sfmmu_ismhat = 1;
8743 8596 mutex_enter(&ism_mlist_lock);
8744 8597 iment_add(ism_ment, ism_hatid);
8745 8598 mutex_exit(&ism_mlist_lock);
8746 8599 added = 1;
8747 8600 break;
8748 8601 }
8749 8602 }
8750 8603 if (!added && ism_blkp->iblk_next == NULL) {
8751 8604 ism_blkp->iblk_next = new_iblk;
8752 8605 new_iblk = NULL;
8753 8606 bzero(ism_blkp->iblk_next,
8754 8607 sizeof (*ism_blkp->iblk_next));
8755 8608 ism_blkp->iblk_next->iblk_nextpa = (uint64_t)-1;
8756 8609 membar_stst();
8757 8610 ism_blkp->iblk_nextpa =
8758 8611 va_to_pa((caddr_t)ism_blkp->iblk_next);
8759 8612 }
8760 8613 ism_blkp = ism_blkp->iblk_next;
8761 8614 }
8762 8615
8763 8616 /*
8764 8617 * After calling hat_join_region, sfmmup may join a new SCD or
8765 8618 * move from the old scd to a new scd, in which case, we want to
8766 8619 * shrink the sfmmup's private tsb size, i.e., pass shrink to
8767 8620 * sfmmu_check_page_sizes at the end of this routine.
8768 8621 */
8769 8622 old_scdp = sfmmup->sfmmu_scdp;
8770 8623
8771 8624 rcookie = hat_join_region(sfmmup, addr, len, (void *)ism_hatid, 0,
8772 8625 PROT_ALL, ismszc, NULL, HAT_REGION_ISM);
8773 8626 if (rcookie != HAT_INVALID_REGION_COOKIE) {
8774 8627 ism_map[i].imap_rid = (uchar_t)((uint64_t)rcookie);
8775 8628 }
8776 8629 /*
8777 8630 * Update our counters for this sfmmup's ism mappings.
8778 8631 */
8779 8632 for (i = 0; i <= ismszc; i++) {
8780 8633 if (!(disable_ism_large_pages & (1 << i)))
8781 8634 (void) ism_tsb_entries(sfmmup, i);
8782 8635 }
8783 8636
8784 8637 /*
8785 8638 * For ISM and DISM we do not support 512K pages, so we only only
8786 8639 * search the 4M and 8K/64K hashes for 4 pagesize cpus, and search the
8787 8640 * 256M or 32M, and 4M and 8K/64K hashes for 6 pagesize cpus.
8788 8641 *
8789 8642 * Need to set 32M/256M ISM flags to make sure
8790 8643 * sfmmu_check_page_sizes() enables them on Panther.
8791 8644 */
8792 8645 ASSERT((disable_ism_large_pages & (1 << TTE512K)) != 0);
8793 8646
8794 8647 switch (ismszc) {
8795 8648 case TTE256M:
8796 8649 if (!SFMMU_FLAGS_ISSET(sfmmup, HAT_256M_ISM)) {
8797 8650 hatlockp = sfmmu_hat_enter(sfmmup);
8798 8651 SFMMU_FLAGS_SET(sfmmup, HAT_256M_ISM);
8799 8652 sfmmu_hat_exit(hatlockp);
8800 8653 }
8801 8654 break;
8802 8655 case TTE32M:
8803 8656 if (!SFMMU_FLAGS_ISSET(sfmmup, HAT_32M_ISM)) {
8804 8657 hatlockp = sfmmu_hat_enter(sfmmup);
8805 8658 SFMMU_FLAGS_SET(sfmmup, HAT_32M_ISM);
8806 8659 sfmmu_hat_exit(hatlockp);
8807 8660 }
8808 8661 break;
8809 8662 default:
8810 8663 break;
8811 8664 }
8812 8665
8813 8666 /*
8814 8667 * If we updated the ismblkpa for this HAT we must make
8815 8668 * sure all CPUs running this process reload their tsbmiss area.
8816 8669 * Otherwise they will fail to load the mappings in the tsbmiss
8817 8670 * handler and will loop calling pagefault().
8818 8671 */
8819 8672 if (reload_mmu) {
8820 8673 hatlockp = sfmmu_hat_enter(sfmmup);
8821 8674 sfmmu_sync_mmustate(sfmmup);
8822 8675 sfmmu_hat_exit(hatlockp);
8823 8676 }
8824 8677
8825 8678 sfmmu_ismhat_exit(sfmmup, 0);
8826 8679
8827 8680 /*
8828 8681 * Free up ismblk if we didn't use it.
8829 8682 */
8830 8683 if (new_iblk != NULL)
8831 8684 kmem_cache_free(ism_blk_cache, new_iblk);
8832 8685
8833 8686 /*
8834 8687 * Check TSB and TLB page sizes.
8835 8688 */
8836 8689 if (sfmmup->sfmmu_scdp != NULL && old_scdp != sfmmup->sfmmu_scdp) {
8837 8690 sfmmu_check_page_sizes(sfmmup, 0);
8838 8691 } else {
8839 8692 sfmmu_check_page_sizes(sfmmup, 1);
8840 8693 }
8841 8694 return (0);
8842 8695 }
8843 8696
8844 8697 /*
8845 8698 * hat_unshare removes exactly one ism_map from
8846 8699 * this process's as. It expects multiple calls
8847 8700 * to hat_unshare for multiple shm segments.
8848 8701 */
8849 8702 void
8850 8703 hat_unshare(struct hat *sfmmup, caddr_t addr, size_t len, uint_t ismszc)
8851 8704 {
8852 8705 ism_map_t *ism_map;
8853 8706 ism_ment_t *free_ment = NULL;
8854 8707 ism_blk_t *ism_blkp;
8855 8708 struct hat *ism_hatid;
8856 8709 int found, i;
8857 8710 hatlock_t *hatlockp;
8858 8711 struct tsb_info *tsbinfo;
8859 8712 uint_t ismshift = page_get_shift(ismszc);
8860 8713 size_t sh_size = ISM_SHIFT(ismshift, len);
8861 8714 uchar_t ism_rid;
8862 8715 sf_scd_t *old_scdp;
8863 8716
8864 8717 ASSERT(ISM_ALIGNED(ismshift, addr));
8865 8718 ASSERT(ISM_ALIGNED(ismshift, len));
8866 8719 ASSERT(sfmmup != NULL);
8867 8720 ASSERT(sfmmup != ksfmmup);
8868 8721
8869 8722 if (sfmmup->sfmmu_xhat_provider) {
8870 8723 XHAT_UNSHARE(sfmmup, addr, len);
8871 8724 return;
8872 8725 } else {
8873 8726 /*
8874 8727 * This must be a CPU HAT. If the address space has
8875 8728 * XHATs attached, inform all XHATs that ISM segment
8876 8729 * is going away
8877 8730 */
8878 8731 ASSERT(sfmmup->sfmmu_as != NULL);
8879 8732 if (sfmmup->sfmmu_as->a_xhat != NULL)
8880 8733 xhat_unshare_all(sfmmup->sfmmu_as, addr, len);
8881 8734 }
8882 8735
8883 8736 /*
8884 8737 * Make sure that during the entire time ISM mappings are removed,
8885 8738 * the trap handlers serialize behind us, and that no one else
8886 8739 * can be mucking with ISM mappings. This also lets us get away
8887 8740 * with not doing expensive cross calls to flush the TLB -- we
8888 8741 * just discard the context, flush the entire TSB, and call it
8889 8742 * a day.
8890 8743 */
8891 8744 sfmmu_ismhat_enter(sfmmup, 0);
8892 8745
8893 8746 /*
8894 8747 * Remove the mapping.
8895 8748 *
8896 8749 * We can't have any holes in the ism map.
8897 8750 * The tsb miss code while searching the ism map will
8898 8751 * stop on an empty map slot. So we must move
8899 8752 * everyone past the hole up 1 if any.
8900 8753 *
8901 8754 * Also empty ism map blks are not freed until the
8902 8755 * process exits. This is to prevent a MT race condition
8903 8756 * between sfmmu_unshare() and sfmmu_tsbmiss_exception().
8904 8757 */
8905 8758 found = 0;
8906 8759 ism_blkp = sfmmup->sfmmu_iblk;
8907 8760 while (!found && ism_blkp != NULL) {
8908 8761 ism_map = ism_blkp->iblk_maps;
8909 8762 for (i = 0; i < ISM_MAP_SLOTS; i++) {
8910 8763 if (addr == ism_start(ism_map[i]) &&
8911 8764 sh_size == (size_t)(ism_size(ism_map[i]))) {
8912 8765 found = 1;
8913 8766 break;
8914 8767 }
8915 8768 }
8916 8769 if (!found)
8917 8770 ism_blkp = ism_blkp->iblk_next;
8918 8771 }
8919 8772
8920 8773 if (found) {
8921 8774 ism_hatid = ism_map[i].imap_ismhat;
8922 8775 ism_rid = ism_map[i].imap_rid;
8923 8776 ASSERT(ism_hatid != NULL);
8924 8777 ASSERT(ism_hatid->sfmmu_ismhat == 1);
8925 8778
8926 8779 /*
8927 8780 * After hat_leave_region, the sfmmup may leave SCD,
8928 8781 * in which case, we want to grow the private tsb size when
8929 8782 * calling sfmmu_check_page_sizes at the end of the routine.
8930 8783 */
8931 8784 old_scdp = sfmmup->sfmmu_scdp;
8932 8785 /*
8933 8786 * Then remove ourselves from the region.
8934 8787 */
8935 8788 if (ism_rid != SFMMU_INVALID_ISMRID) {
8936 8789 hat_leave_region(sfmmup, (void *)((uint64_t)ism_rid),
8937 8790 HAT_REGION_ISM);
8938 8791 }
8939 8792
8940 8793 /*
8941 8794 * And now guarantee that any other cpu
8942 8795 * that tries to process an ISM miss
8943 8796 * will go to tl=0.
8944 8797 */
8945 8798 hatlockp = sfmmu_hat_enter(sfmmup);
8946 8799 sfmmu_invalidate_ctx(sfmmup);
8947 8800 sfmmu_hat_exit(hatlockp);
8948 8801
8949 8802 /*
8950 8803 * Remove ourselves from the ism mapping list.
8951 8804 */
8952 8805 mutex_enter(&ism_mlist_lock);
8953 8806 iment_sub(ism_map[i].imap_ment, ism_hatid);
8954 8807 mutex_exit(&ism_mlist_lock);
8955 8808 free_ment = ism_map[i].imap_ment;
8956 8809
8957 8810 /*
8958 8811 * We delete the ism map by copying
8959 8812 * the next map over the current one.
8960 8813 * We will take the next one in the maps
8961 8814 * array or from the next ism_blk.
8962 8815 */
8963 8816 while (ism_blkp != NULL) {
8964 8817 ism_map = ism_blkp->iblk_maps;
8965 8818 while (i < (ISM_MAP_SLOTS - 1)) {
8966 8819 ism_map[i] = ism_map[i + 1];
8967 8820 i++;
8968 8821 }
8969 8822 /* i == (ISM_MAP_SLOTS - 1) */
8970 8823 ism_blkp = ism_blkp->iblk_next;
8971 8824 if (ism_blkp != NULL) {
8972 8825 ism_map[i] = ism_blkp->iblk_maps[0];
8973 8826 i = 0;
8974 8827 } else {
8975 8828 ism_map[i].imap_seg = 0;
8976 8829 ism_map[i].imap_vb_shift = 0;
8977 8830 ism_map[i].imap_rid = SFMMU_INVALID_ISMRID;
8978 8831 ism_map[i].imap_hatflags = 0;
8979 8832 ism_map[i].imap_sz_mask = 0;
8980 8833 ism_map[i].imap_ismhat = NULL;
8981 8834 ism_map[i].imap_ment = NULL;
8982 8835 }
8983 8836 }
8984 8837
8985 8838 /*
8986 8839 * Now flush entire TSB for the process, since
8987 8840 * demapping page by page can be too expensive.
8988 8841 * We don't have to flush the TLB here anymore
8989 8842 * since we switch to a new TLB ctx instead.
8990 8843 * Also, there is no need to flush if the process
8991 8844 * is exiting since the TSB will be freed later.
8992 8845 */
8993 8846 if (!sfmmup->sfmmu_free) {
8994 8847 hatlockp = sfmmu_hat_enter(sfmmup);
8995 8848 for (tsbinfo = sfmmup->sfmmu_tsb; tsbinfo != NULL;
8996 8849 tsbinfo = tsbinfo->tsb_next) {
8997 8850 if (tsbinfo->tsb_flags & TSB_SWAPPED)
8998 8851 continue;
8999 8852 if (tsbinfo->tsb_flags & TSB_RELOC_FLAG) {
9000 8853 tsbinfo->tsb_flags |=
9001 8854 TSB_FLUSH_NEEDED;
9002 8855 continue;
9003 8856 }
9004 8857
9005 8858 sfmmu_inv_tsb(tsbinfo->tsb_va,
9006 8859 TSB_BYTES(tsbinfo->tsb_szc));
9007 8860 }
9008 8861 sfmmu_hat_exit(hatlockp);
9009 8862 }
9010 8863 }
9011 8864
9012 8865 /*
9013 8866 * Update our counters for this sfmmup's ism mappings.
9014 8867 */
9015 8868 for (i = 0; i <= ismszc; i++) {
9016 8869 if (!(disable_ism_large_pages & (1 << i)))
9017 8870 (void) ism_tsb_entries(sfmmup, i);
9018 8871 }
9019 8872
9020 8873 sfmmu_ismhat_exit(sfmmup, 0);
9021 8874
9022 8875 /*
9023 8876 * We must do our freeing here after dropping locks
9024 8877 * to prevent a deadlock in the kmem allocator on the
9025 8878 * mapping list lock.
9026 8879 */
9027 8880 if (free_ment != NULL)
9028 8881 kmem_cache_free(ism_ment_cache, free_ment);
9029 8882
9030 8883 /*
9031 8884 * Check TSB and TLB page sizes if the process isn't exiting.
9032 8885 */
9033 8886 if (!sfmmup->sfmmu_free) {
9034 8887 if (found && old_scdp != NULL && sfmmup->sfmmu_scdp == NULL) {
9035 8888 sfmmu_check_page_sizes(sfmmup, 1);
9036 8889 } else {
9037 8890 sfmmu_check_page_sizes(sfmmup, 0);
9038 8891 }
9039 8892 }
9040 8893 }
9041 8894
9042 8895 /* ARGSUSED */
9043 8896 static int
9044 8897 sfmmu_idcache_constructor(void *buf, void *cdrarg, int kmflags)
9045 8898 {
9046 8899 /* void *buf is sfmmu_t pointer */
9047 8900 bzero(buf, sizeof (sfmmu_t));
9048 8901
9049 8902 return (0);
9050 8903 }
9051 8904
9052 8905 /* ARGSUSED */
9053 8906 static void
9054 8907 sfmmu_idcache_destructor(void *buf, void *cdrarg)
9055 8908 {
9056 8909 /* void *buf is sfmmu_t pointer */
9057 8910 }
9058 8911
9059 8912 /*
9060 8913 * setup kmem hmeblks by bzeroing all members and initializing the nextpa
9061 8914 * field to be the pa of this hmeblk
9062 8915 */
9063 8916 /* ARGSUSED */
9064 8917 static int
9065 8918 sfmmu_hblkcache_constructor(void *buf, void *cdrarg, int kmflags)
9066 8919 {
9067 8920 struct hme_blk *hmeblkp;
9068 8921
9069 8922 bzero(buf, (size_t)cdrarg);
9070 8923 hmeblkp = (struct hme_blk *)buf;
9071 8924 hmeblkp->hblk_nextpa = va_to_pa((caddr_t)hmeblkp);
9072 8925
9073 8926 #ifdef HBLK_TRACE
9074 8927 mutex_init(&hmeblkp->hblk_audit_lock, NULL, MUTEX_DEFAULT, NULL);
9075 8928 #endif /* HBLK_TRACE */
9076 8929
9077 8930 return (0);
9078 8931 }
9079 8932
9080 8933 /* ARGSUSED */
9081 8934 static void
9082 8935 sfmmu_hblkcache_destructor(void *buf, void *cdrarg)
9083 8936 {
9084 8937
9085 8938 #ifdef HBLK_TRACE
9086 8939
9087 8940 struct hme_blk *hmeblkp;
9088 8941
9089 8942 hmeblkp = (struct hme_blk *)buf;
9090 8943 mutex_destroy(&hmeblkp->hblk_audit_lock);
9091 8944
9092 8945 #endif /* HBLK_TRACE */
9093 8946 }
9094 8947
9095 8948 #define SFMMU_CACHE_RECLAIM_SCAN_RATIO 8
9096 8949 static int sfmmu_cache_reclaim_scan_ratio = SFMMU_CACHE_RECLAIM_SCAN_RATIO;
9097 8950 /*
9098 8951 * The kmem allocator will callback into our reclaim routine when the system
9099 8952 * is running low in memory. We traverse the hash and free up all unused but
9100 8953 * still cached hme_blks. We also traverse the free list and free them up
9101 8954 * as well.
9102 8955 */
9103 8956 /*ARGSUSED*/
9104 8957 static void
9105 8958 sfmmu_hblkcache_reclaim(void *cdrarg)
9106 8959 {
9107 8960 int i;
9108 8961 struct hmehash_bucket *hmebp;
9109 8962 struct hme_blk *hmeblkp, *nx_hblk, *pr_hblk = NULL;
9110 8963 static struct hmehash_bucket *uhmehash_reclaim_hand;
9111 8964 static struct hmehash_bucket *khmehash_reclaim_hand;
9112 8965 struct hme_blk *list = NULL, *last_hmeblkp;
9113 8966 cpuset_t cpuset = cpu_ready_set;
9114 8967 cpu_hme_pend_t *cpuhp;
9115 8968
9116 8969 /* Free up hmeblks on the cpu pending lists */
9117 8970 for (i = 0; i < NCPU; i++) {
9118 8971 cpuhp = &cpu_hme_pend[i];
9119 8972 if (cpuhp->chp_listp != NULL) {
9120 8973 mutex_enter(&cpuhp->chp_mutex);
9121 8974 if (cpuhp->chp_listp == NULL) {
9122 8975 mutex_exit(&cpuhp->chp_mutex);
9123 8976 continue;
9124 8977 }
9125 8978 for (last_hmeblkp = cpuhp->chp_listp;
9126 8979 last_hmeblkp->hblk_next != NULL;
9127 8980 last_hmeblkp = last_hmeblkp->hblk_next)
9128 8981 ;
9129 8982 last_hmeblkp->hblk_next = list;
9130 8983 list = cpuhp->chp_listp;
9131 8984 cpuhp->chp_listp = NULL;
9132 8985 cpuhp->chp_count = 0;
9133 8986 mutex_exit(&cpuhp->chp_mutex);
9134 8987 }
9135 8988
9136 8989 }
9137 8990
9138 8991 if (list != NULL) {
9139 8992 kpreempt_disable();
9140 8993 CPUSET_DEL(cpuset, CPU->cpu_id);
9141 8994 xt_sync(cpuset);
9142 8995 xt_sync(cpuset);
9143 8996 kpreempt_enable();
9144 8997 sfmmu_hblk_free(&list);
9145 8998 list = NULL;
9146 8999 }
9147 9000
9148 9001 hmebp = uhmehash_reclaim_hand;
9149 9002 if (hmebp == NULL || hmebp > &uhme_hash[UHMEHASH_SZ])
9150 9003 uhmehash_reclaim_hand = hmebp = uhme_hash;
9151 9004 uhmehash_reclaim_hand += UHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio;
9152 9005
9153 9006 for (i = UHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; i; i--) {
9154 9007 if (SFMMU_HASH_LOCK_TRYENTER(hmebp) != 0) {
9155 9008 hmeblkp = hmebp->hmeblkp;
9156 9009 pr_hblk = NULL;
9157 9010 while (hmeblkp) {
9158 9011 nx_hblk = hmeblkp->hblk_next;
9159 9012 if (!hmeblkp->hblk_vcnt &&
9160 9013 !hmeblkp->hblk_hmecnt) {
9161 9014 sfmmu_hblk_hash_rm(hmebp, hmeblkp,
9162 9015 pr_hblk, &list, 0);
9163 9016 } else {
9164 9017 pr_hblk = hmeblkp;
9165 9018 }
9166 9019 hmeblkp = nx_hblk;
9167 9020 }
9168 9021 SFMMU_HASH_UNLOCK(hmebp);
9169 9022 }
9170 9023 if (hmebp++ == &uhme_hash[UHMEHASH_SZ])
9171 9024 hmebp = uhme_hash;
9172 9025 }
9173 9026
9174 9027 hmebp = khmehash_reclaim_hand;
9175 9028 if (hmebp == NULL || hmebp > &khme_hash[KHMEHASH_SZ])
9176 9029 khmehash_reclaim_hand = hmebp = khme_hash;
9177 9030 khmehash_reclaim_hand += KHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio;
9178 9031
9179 9032 for (i = KHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; i; i--) {
9180 9033 if (SFMMU_HASH_LOCK_TRYENTER(hmebp) != 0) {
9181 9034 hmeblkp = hmebp->hmeblkp;
9182 9035 pr_hblk = NULL;
9183 9036 while (hmeblkp) {
9184 9037 nx_hblk = hmeblkp->hblk_next;
9185 9038 if (!hmeblkp->hblk_vcnt &&
9186 9039 !hmeblkp->hblk_hmecnt) {
9187 9040 sfmmu_hblk_hash_rm(hmebp, hmeblkp,
9188 9041 pr_hblk, &list, 0);
9189 9042 } else {
9190 9043 pr_hblk = hmeblkp;
9191 9044 }
9192 9045 hmeblkp = nx_hblk;
9193 9046 }
9194 9047 SFMMU_HASH_UNLOCK(hmebp);
9195 9048 }
9196 9049 if (hmebp++ == &khme_hash[KHMEHASH_SZ])
9197 9050 hmebp = khme_hash;
9198 9051 }
9199 9052 sfmmu_hblks_list_purge(&list, 0);
9200 9053 }
9201 9054
9202 9055 /*
9203 9056 * sfmmu_get_ppvcolor should become a vm_machdep or hatop interface.
9204 9057 * same goes for sfmmu_get_addrvcolor().
9205 9058 *
9206 9059 * This function will return the virtual color for the specified page. The
9207 9060 * virtual color corresponds to this page current mapping or its last mapping.
9208 9061 * It is used by memory allocators to choose addresses with the correct
9209 9062 * alignment so vac consistency is automatically maintained. If the page
9210 9063 * has no color it returns -1.
9211 9064 */
9212 9065 /*ARGSUSED*/
9213 9066 int
9214 9067 sfmmu_get_ppvcolor(struct page *pp)
9215 9068 {
9216 9069 #ifdef VAC
9217 9070 int color;
9218 9071
9219 9072 if (!(cache & CACHE_VAC) || PP_NEWPAGE(pp)) {
9220 9073 return (-1);
9221 9074 }
9222 9075 color = PP_GET_VCOLOR(pp);
9223 9076 ASSERT(color < mmu_btop(shm_alignment));
9224 9077 return (color);
9225 9078 #else
9226 9079 return (-1);
9227 9080 #endif /* VAC */
9228 9081 }
9229 9082
9230 9083 /*
9231 9084 * This function will return the desired alignment for vac consistency
9232 9085 * (vac color) given a virtual address. If no vac is present it returns -1.
9233 9086 */
9234 9087 /*ARGSUSED*/
9235 9088 int
9236 9089 sfmmu_get_addrvcolor(caddr_t vaddr)
9237 9090 {
9238 9091 #ifdef VAC
9239 9092 if (cache & CACHE_VAC) {
9240 9093 return (addr_to_vcolor(vaddr));
9241 9094 } else {
9242 9095 return (-1);
9243 9096 }
9244 9097 #else
9245 9098 return (-1);
9246 9099 #endif /* VAC */
9247 9100 }
9248 9101
9249 9102 #ifdef VAC
9250 9103 /*
9251 9104 * Check for conflicts.
9252 9105 * A conflict exists if the new and existent mappings do not match in
9253 9106 * their "shm_alignment fields. If conflicts exist, the existant mappings
9254 9107 * are flushed unless one of them is locked. If one of them is locked, then
9255 9108 * the mappings are flushed and converted to non-cacheable mappings.
9256 9109 */
9257 9110 static void
9258 9111 sfmmu_vac_conflict(struct hat *hat, caddr_t addr, page_t *pp)
9259 9112 {
9260 9113 struct hat *tmphat;
9261 9114 struct sf_hment *sfhmep, *tmphme = NULL;
9262 9115 struct hme_blk *hmeblkp;
9263 9116 int vcolor;
9264 9117 tte_t tte;
9265 9118
9266 9119 ASSERT(sfmmu_mlist_held(pp));
9267 9120 ASSERT(!PP_ISNC(pp)); /* page better be cacheable */
9268 9121
9269 9122 vcolor = addr_to_vcolor(addr);
9270 9123 if (PP_NEWPAGE(pp)) {
9271 9124 PP_SET_VCOLOR(pp, vcolor);
9272 9125 return;
9273 9126 }
9274 9127
9275 9128 if (PP_GET_VCOLOR(pp) == vcolor) {
9276 9129 return;
9277 9130 }
9278 9131
9279 9132 if (!PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp)) {
9280 9133 /*
9281 9134 * Previous user of page had a different color
9282 9135 * but since there are no current users
9283 9136 * we just flush the cache and change the color.
9284 9137 */
9285 9138 SFMMU_STAT(sf_pgcolor_conflict);
9286 9139 sfmmu_cache_flush(pp->p_pagenum, PP_GET_VCOLOR(pp));
9287 9140 PP_SET_VCOLOR(pp, vcolor);
9288 9141 return;
9289 9142 }
9290 9143
9291 9144 /*
9292 9145 * If we get here we have a vac conflict with a current
9293 9146 * mapping. VAC conflict policy is as follows.
9294 9147 * - The default is to unload the other mappings unless:
9295 9148 * - If we have a large mapping we uncache the page.
9296 9149 * We need to uncache the rest of the large page too.
9297 9150 * - If any of the mappings are locked we uncache the page.
9298 9151 * - If the requested mapping is inconsistent
9299 9152 * with another mapping and that mapping
9300 9153 * is in the same address space we have to
9301 9154 * make it non-cached. The default thing
9302 9155 * to do is unload the inconsistent mapping
9303 9156 * but if they are in the same address space
9304 9157 * we run the risk of unmapping the pc or the
9305 9158 * stack which we will use as we return to the user,
9306 9159 * in which case we can then fault on the thing
9307 9160 * we just unloaded and get into an infinite loop.
9308 9161 */
9309 9162 if (PP_ISMAPPED_LARGE(pp)) {
9310 9163 int sz;
9311 9164
9312 9165 /*
9313 9166 * Existing mapping is for big pages. We don't unload
9314 9167 * existing big mappings to satisfy new mappings.
9315 9168 * Always convert all mappings to TNC.
9316 9169 */
9317 9170 sz = fnd_mapping_sz(pp);
9318 9171 pp = PP_GROUPLEADER(pp, sz);
9319 9172 SFMMU_STAT_ADD(sf_uncache_conflict, TTEPAGES(sz));
9320 9173 sfmmu_page_cache_array(pp, HAT_TMPNC, CACHE_FLUSH,
9321 9174 TTEPAGES(sz));
9322 9175
9323 9176 return;
9324 9177 }
9325 9178
9326 9179 /*
9327 9180 * check if any mapping is in same as or if it is locked
9328 9181 * since in that case we need to uncache.
9329 9182 */
9330 9183 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = tmphme) {
9331 9184 tmphme = sfhmep->hme_next;
9332 9185 if (IS_PAHME(sfhmep))
9333 9186 continue;
9334 9187 hmeblkp = sfmmu_hmetohblk(sfhmep);
9335 9188 if (hmeblkp->hblk_xhat_bit)
9336 9189 continue;
9337 9190 tmphat = hblktosfmmu(hmeblkp);
9338 9191 sfmmu_copytte(&sfhmep->hme_tte, &tte);
9339 9192 ASSERT(TTE_IS_VALID(&tte));
9340 9193 if (hmeblkp->hblk_shared || tmphat == hat ||
9341 9194 hmeblkp->hblk_lckcnt) {
9342 9195 /*
9343 9196 * We have an uncache conflict
9344 9197 */
9345 9198 SFMMU_STAT(sf_uncache_conflict);
9346 9199 sfmmu_page_cache_array(pp, HAT_TMPNC, CACHE_FLUSH, 1);
9347 9200 return;
9348 9201 }
9349 9202 }
9350 9203
9351 9204 /*
9352 9205 * We have an unload conflict
9353 9206 * We have already checked for LARGE mappings, therefore
9354 9207 * the remaining mapping(s) must be TTE8K.
9355 9208 */
9356 9209 SFMMU_STAT(sf_unload_conflict);
9357 9210
9358 9211 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = tmphme) {
9359 9212 tmphme = sfhmep->hme_next;
9360 9213 if (IS_PAHME(sfhmep))
9361 9214 continue;
9362 9215 hmeblkp = sfmmu_hmetohblk(sfhmep);
9363 9216 if (hmeblkp->hblk_xhat_bit)
9364 9217 continue;
9365 9218 ASSERT(!hmeblkp->hblk_shared);
9366 9219 (void) sfmmu_pageunload(pp, sfhmep, TTE8K);
9367 9220 }
9368 9221
9369 9222 if (PP_ISMAPPED_KPM(pp))
9370 9223 sfmmu_kpm_vac_unload(pp, addr);
9371 9224
9372 9225 /*
9373 9226 * Unloads only do TLB flushes so we need to flush the
9374 9227 * cache here.
9375 9228 */
9376 9229 sfmmu_cache_flush(pp->p_pagenum, PP_GET_VCOLOR(pp));
9377 9230 PP_SET_VCOLOR(pp, vcolor);
9378 9231 }
9379 9232
9380 9233 /*
9381 9234 * Whenever a mapping is unloaded and the page is in TNC state,
9382 9235 * we see if the page can be made cacheable again. 'pp' is
9383 9236 * the page that we just unloaded a mapping from, the size
9384 9237 * of mapping that was unloaded is 'ottesz'.
9385 9238 * Remark:
9386 9239 * The recache policy for mpss pages can leave a performance problem
9387 9240 * under the following circumstances:
9388 9241 * . A large page in uncached mode has just been unmapped.
9389 9242 * . All constituent pages are TNC due to a conflicting small mapping.
9390 9243 * . There are many other, non conflicting, small mappings around for
9391 9244 * a lot of the constituent pages.
9392 9245 * . We're called w/ the "old" groupleader page and the old ottesz,
9393 9246 * but this is irrelevant, since we're no more "PP_ISMAPPED_LARGE", so
9394 9247 * we end up w/ TTE8K or npages == 1.
9395 9248 * . We call tst_tnc w/ the old groupleader only, and if there is no
9396 9249 * conflict, we re-cache only this page.
9397 9250 * . All other small mappings are not checked and will be left in TNC mode.
9398 9251 * The problem is not very serious because:
9399 9252 * . mpss is actually only defined for heap and stack, so the probability
9400 9253 * is not very high that a large page mapping exists in parallel to a small
9401 9254 * one (this is possible, but seems to be bad programming style in the
9402 9255 * appl).
9403 9256 * . The problem gets a little bit more serious, when those TNC pages
9404 9257 * have to be mapped into kernel space, e.g. for networking.
9405 9258 * . When VAC alias conflicts occur in applications, this is regarded
9406 9259 * as an application bug. So if kstat's show them, the appl should
9407 9260 * be changed anyway.
9408 9261 */
9409 9262 void
9410 9263 conv_tnc(page_t *pp, int ottesz)
9411 9264 {
9412 9265 int cursz, dosz;
9413 9266 pgcnt_t curnpgs, dopgs;
9414 9267 pgcnt_t pg64k;
9415 9268 page_t *pp2;
9416 9269
9417 9270 /*
9418 9271 * Determine how big a range we check for TNC and find
9419 9272 * leader page. cursz is the size of the biggest
9420 9273 * mapping that still exist on 'pp'.
9421 9274 */
9422 9275 if (PP_ISMAPPED_LARGE(pp)) {
9423 9276 cursz = fnd_mapping_sz(pp);
9424 9277 } else {
9425 9278 cursz = TTE8K;
9426 9279 }
9427 9280
9428 9281 if (ottesz >= cursz) {
9429 9282 dosz = ottesz;
9430 9283 pp2 = pp;
9431 9284 } else {
9432 9285 dosz = cursz;
9433 9286 pp2 = PP_GROUPLEADER(pp, dosz);
9434 9287 }
9435 9288
9436 9289 pg64k = TTEPAGES(TTE64K);
9437 9290 dopgs = TTEPAGES(dosz);
9438 9291
9439 9292 ASSERT(dopgs == 1 || ((dopgs & (pg64k - 1)) == 0));
9440 9293
9441 9294 while (dopgs != 0) {
9442 9295 curnpgs = TTEPAGES(cursz);
9443 9296 if (tst_tnc(pp2, curnpgs)) {
9444 9297 SFMMU_STAT_ADD(sf_recache, curnpgs);
9445 9298 sfmmu_page_cache_array(pp2, HAT_CACHE, CACHE_NO_FLUSH,
9446 9299 curnpgs);
9447 9300 }
9448 9301
9449 9302 ASSERT(dopgs >= curnpgs);
9450 9303 dopgs -= curnpgs;
9451 9304
9452 9305 if (dopgs == 0) {
9453 9306 break;
9454 9307 }
9455 9308
9456 9309 pp2 = PP_PAGENEXT_N(pp2, curnpgs);
9457 9310 if (((dopgs & (pg64k - 1)) == 0) && PP_ISMAPPED_LARGE(pp2)) {
9458 9311 cursz = fnd_mapping_sz(pp2);
9459 9312 } else {
9460 9313 cursz = TTE8K;
9461 9314 }
9462 9315 }
9463 9316 }
9464 9317
9465 9318 /*
9466 9319 * Returns 1 if page(s) can be converted from TNC to cacheable setting,
9467 9320 * returns 0 otherwise. Note that oaddr argument is valid for only
9468 9321 * 8k pages.
9469 9322 */
9470 9323 int
9471 9324 tst_tnc(page_t *pp, pgcnt_t npages)
9472 9325 {
9473 9326 struct sf_hment *sfhme;
9474 9327 struct hme_blk *hmeblkp;
9475 9328 tte_t tte;
9476 9329 caddr_t vaddr;
9477 9330 int clr_valid = 0;
9478 9331 int color, color1, bcolor;
9479 9332 int i, ncolors;
9480 9333
9481 9334 ASSERT(pp != NULL);
9482 9335 ASSERT(!(cache & CACHE_WRITEBACK));
9483 9336
9484 9337 if (npages > 1) {
9485 9338 ncolors = CACHE_NUM_COLOR;
9486 9339 }
9487 9340
9488 9341 for (i = 0; i < npages; i++) {
9489 9342 ASSERT(sfmmu_mlist_held(pp));
9490 9343 ASSERT(PP_ISTNC(pp));
9491 9344 ASSERT(PP_GET_VCOLOR(pp) == NO_VCOLOR);
9492 9345
9493 9346 if (PP_ISPNC(pp)) {
9494 9347 return (0);
9495 9348 }
9496 9349
9497 9350 clr_valid = 0;
9498 9351 if (PP_ISMAPPED_KPM(pp)) {
9499 9352 caddr_t kpmvaddr;
9500 9353
9501 9354 ASSERT(kpm_enable);
9502 9355 kpmvaddr = hat_kpm_page2va(pp, 1);
9503 9356 ASSERT(!(npages > 1 && IS_KPM_ALIAS_RANGE(kpmvaddr)));
9504 9357 color1 = addr_to_vcolor(kpmvaddr);
9505 9358 clr_valid = 1;
9506 9359 }
9507 9360
9508 9361 for (sfhme = pp->p_mapping; sfhme; sfhme = sfhme->hme_next) {
9509 9362 if (IS_PAHME(sfhme))
9510 9363 continue;
9511 9364 hmeblkp = sfmmu_hmetohblk(sfhme);
9512 9365 if (hmeblkp->hblk_xhat_bit)
9513 9366 continue;
9514 9367
9515 9368 sfmmu_copytte(&sfhme->hme_tte, &tte);
9516 9369 ASSERT(TTE_IS_VALID(&tte));
9517 9370
9518 9371 vaddr = tte_to_vaddr(hmeblkp, tte);
9519 9372 color = addr_to_vcolor(vaddr);
9520 9373
9521 9374 if (npages > 1) {
9522 9375 /*
9523 9376 * If there is a big mapping, make sure
9524 9377 * 8K mapping is consistent with the big
9525 9378 * mapping.
9526 9379 */
9527 9380 bcolor = i % ncolors;
9528 9381 if (color != bcolor) {
9529 9382 return (0);
9530 9383 }
9531 9384 }
9532 9385 if (!clr_valid) {
9533 9386 clr_valid = 1;
9534 9387 color1 = color;
9535 9388 }
9536 9389
9537 9390 if (color1 != color) {
9538 9391 return (0);
9539 9392 }
9540 9393 }
9541 9394
9542 9395 pp = PP_PAGENEXT(pp);
9543 9396 }
9544 9397
9545 9398 return (1);
9546 9399 }
9547 9400
9548 9401 void
9549 9402 sfmmu_page_cache_array(page_t *pp, int flags, int cache_flush_flag,
9550 9403 pgcnt_t npages)
9551 9404 {
9552 9405 kmutex_t *pmtx;
9553 9406 int i, ncolors, bcolor;
9554 9407 kpm_hlk_t *kpmp;
9555 9408 cpuset_t cpuset;
9556 9409
9557 9410 ASSERT(pp != NULL);
9558 9411 ASSERT(!(cache & CACHE_WRITEBACK));
9559 9412
9560 9413 kpmp = sfmmu_kpm_kpmp_enter(pp, npages);
9561 9414 pmtx = sfmmu_page_enter(pp);
9562 9415
9563 9416 /*
9564 9417 * Fast path caching single unmapped page
9565 9418 */
9566 9419 if (npages == 1 && !PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp) &&
9567 9420 flags == HAT_CACHE) {
9568 9421 PP_CLRTNC(pp);
9569 9422 PP_CLRPNC(pp);
9570 9423 sfmmu_page_exit(pmtx);
9571 9424 sfmmu_kpm_kpmp_exit(kpmp);
9572 9425 return;
9573 9426 }
9574 9427
9575 9428 /*
9576 9429 * We need to capture all cpus in order to change cacheability
9577 9430 * because we can't allow one cpu to access the same physical
9578 9431 * page using a cacheable and a non-cachebale mapping at the same
9579 9432 * time. Since we may end up walking the ism mapping list
9580 9433 * have to grab it's lock now since we can't after all the
9581 9434 * cpus have been captured.
9582 9435 */
9583 9436 sfmmu_hat_lock_all();
9584 9437 mutex_enter(&ism_mlist_lock);
9585 9438 kpreempt_disable();
9586 9439 cpuset = cpu_ready_set;
9587 9440 xc_attention(cpuset);
9588 9441
9589 9442 if (npages > 1) {
9590 9443 /*
9591 9444 * Make sure all colors are flushed since the
9592 9445 * sfmmu_page_cache() only flushes one color-
9593 9446 * it does not know big pages.
9594 9447 */
9595 9448 ncolors = CACHE_NUM_COLOR;
9596 9449 if (flags & HAT_TMPNC) {
9597 9450 for (i = 0; i < ncolors; i++) {
9598 9451 sfmmu_cache_flushcolor(i, pp->p_pagenum);
9599 9452 }
9600 9453 cache_flush_flag = CACHE_NO_FLUSH;
9601 9454 }
9602 9455 }
9603 9456
9604 9457 for (i = 0; i < npages; i++) {
9605 9458
9606 9459 ASSERT(sfmmu_mlist_held(pp));
9607 9460
9608 9461 if (!(flags == HAT_TMPNC && PP_ISTNC(pp))) {
9609 9462
9610 9463 if (npages > 1) {
9611 9464 bcolor = i % ncolors;
9612 9465 } else {
9613 9466 bcolor = NO_VCOLOR;
9614 9467 }
9615 9468
9616 9469 sfmmu_page_cache(pp, flags, cache_flush_flag,
9617 9470 bcolor);
9618 9471 }
9619 9472
9620 9473 pp = PP_PAGENEXT(pp);
9621 9474 }
9622 9475
9623 9476 xt_sync(cpuset);
9624 9477 xc_dismissed(cpuset);
9625 9478 mutex_exit(&ism_mlist_lock);
9626 9479 sfmmu_hat_unlock_all();
9627 9480 sfmmu_page_exit(pmtx);
9628 9481 sfmmu_kpm_kpmp_exit(kpmp);
9629 9482 kpreempt_enable();
9630 9483 }
9631 9484
9632 9485 /*
9633 9486 * This function changes the virtual cacheability of all mappings to a
9634 9487 * particular page. When changing from uncache to cacheable the mappings will
9635 9488 * only be changed if all of them have the same virtual color.
9636 9489 * We need to flush the cache in all cpus. It is possible that
9637 9490 * a process referenced a page as cacheable but has sinced exited
9638 9491 * and cleared the mapping list. We still to flush it but have no
9639 9492 * state so all cpus is the only alternative.
9640 9493 */
9641 9494 static void
9642 9495 sfmmu_page_cache(page_t *pp, int flags, int cache_flush_flag, int bcolor)
9643 9496 {
9644 9497 struct sf_hment *sfhme;
9645 9498 struct hme_blk *hmeblkp;
9646 9499 sfmmu_t *sfmmup;
9647 9500 tte_t tte, ttemod;
9648 9501 caddr_t vaddr;
9649 9502 int ret, color;
9650 9503 pfn_t pfn;
9651 9504
9652 9505 color = bcolor;
9653 9506 pfn = pp->p_pagenum;
9654 9507
9655 9508 for (sfhme = pp->p_mapping; sfhme; sfhme = sfhme->hme_next) {
9656 9509
9657 9510 if (IS_PAHME(sfhme))
9658 9511 continue;
9659 9512 hmeblkp = sfmmu_hmetohblk(sfhme);
9660 9513
9661 9514 if (hmeblkp->hblk_xhat_bit)
9662 9515 continue;
9663 9516
9664 9517 sfmmu_copytte(&sfhme->hme_tte, &tte);
9665 9518 ASSERT(TTE_IS_VALID(&tte));
9666 9519 vaddr = tte_to_vaddr(hmeblkp, tte);
9667 9520 color = addr_to_vcolor(vaddr);
9668 9521
9669 9522 #ifdef DEBUG
9670 9523 if ((flags & HAT_CACHE) && bcolor != NO_VCOLOR) {
9671 9524 ASSERT(color == bcolor);
9672 9525 }
9673 9526 #endif
9674 9527
9675 9528 ASSERT(flags != HAT_TMPNC || color == PP_GET_VCOLOR(pp));
9676 9529
9677 9530 ttemod = tte;
9678 9531 if (flags & (HAT_UNCACHE | HAT_TMPNC)) {
9679 9532 TTE_CLR_VCACHEABLE(&ttemod);
9680 9533 } else { /* flags & HAT_CACHE */
9681 9534 TTE_SET_VCACHEABLE(&ttemod);
9682 9535 }
9683 9536 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte);
9684 9537 if (ret < 0) {
9685 9538 /*
9686 9539 * Since all cpus are captured modifytte should not
9687 9540 * fail.
9688 9541 */
9689 9542 panic("sfmmu_page_cache: write to tte failed");
9690 9543 }
9691 9544
9692 9545 sfmmup = hblktosfmmu(hmeblkp);
9693 9546 if (cache_flush_flag == CACHE_FLUSH) {
9694 9547 /*
9695 9548 * Flush TSBs, TLBs and caches
9696 9549 */
9697 9550 if (hmeblkp->hblk_shared) {
9698 9551 sf_srd_t *srdp = (sf_srd_t *)sfmmup;
9699 9552 uint_t rid = hmeblkp->hblk_tag.htag_rid;
9700 9553 sf_region_t *rgnp;
9701 9554 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
9702 9555 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
9703 9556 ASSERT(srdp != NULL);
9704 9557 rgnp = srdp->srd_hmergnp[rid];
9705 9558 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp,
9706 9559 srdp, rgnp, rid);
9707 9560 (void) sfmmu_rgntlb_demap(vaddr, rgnp,
9708 9561 hmeblkp, 0);
9709 9562 sfmmu_cache_flush(pfn, addr_to_vcolor(vaddr));
9710 9563 } else if (sfmmup->sfmmu_ismhat) {
9711 9564 if (flags & HAT_CACHE) {
9712 9565 SFMMU_STAT(sf_ism_recache);
9713 9566 } else {
9714 9567 SFMMU_STAT(sf_ism_uncache);
9715 9568 }
9716 9569 sfmmu_ismtlbcache_demap(vaddr, sfmmup, hmeblkp,
9717 9570 pfn, CACHE_FLUSH);
9718 9571 } else {
9719 9572 sfmmu_tlbcache_demap(vaddr, sfmmup, hmeblkp,
9720 9573 pfn, 0, FLUSH_ALL_CPUS, CACHE_FLUSH, 1);
9721 9574 }
9722 9575
9723 9576 /*
9724 9577 * all cache entries belonging to this pfn are
9725 9578 * now flushed.
9726 9579 */
9727 9580 cache_flush_flag = CACHE_NO_FLUSH;
9728 9581 } else {
9729 9582 /*
9730 9583 * Flush only TSBs and TLBs.
9731 9584 */
9732 9585 if (hmeblkp->hblk_shared) {
9733 9586 sf_srd_t *srdp = (sf_srd_t *)sfmmup;
9734 9587 uint_t rid = hmeblkp->hblk_tag.htag_rid;
9735 9588 sf_region_t *rgnp;
9736 9589 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
9737 9590 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
9738 9591 ASSERT(srdp != NULL);
9739 9592 rgnp = srdp->srd_hmergnp[rid];
9740 9593 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp,
9741 9594 srdp, rgnp, rid);
9742 9595 (void) sfmmu_rgntlb_demap(vaddr, rgnp,
9743 9596 hmeblkp, 0);
9744 9597 } else if (sfmmup->sfmmu_ismhat) {
9745 9598 if (flags & HAT_CACHE) {
9746 9599 SFMMU_STAT(sf_ism_recache);
9747 9600 } else {
9748 9601 SFMMU_STAT(sf_ism_uncache);
9749 9602 }
9750 9603 sfmmu_ismtlbcache_demap(vaddr, sfmmup, hmeblkp,
9751 9604 pfn, CACHE_NO_FLUSH);
9752 9605 } else {
9753 9606 sfmmu_tlb_demap(vaddr, sfmmup, hmeblkp, 0, 1);
9754 9607 }
9755 9608 }
9756 9609 }
9757 9610
9758 9611 if (PP_ISMAPPED_KPM(pp))
9759 9612 sfmmu_kpm_page_cache(pp, flags, cache_flush_flag);
9760 9613
9761 9614 switch (flags) {
9762 9615
9763 9616 default:
9764 9617 panic("sfmmu_pagecache: unknown flags");
9765 9618 break;
9766 9619
9767 9620 case HAT_CACHE:
9768 9621 PP_CLRTNC(pp);
9769 9622 PP_CLRPNC(pp);
9770 9623 PP_SET_VCOLOR(pp, color);
9771 9624 break;
9772 9625
9773 9626 case HAT_TMPNC:
9774 9627 PP_SETTNC(pp);
9775 9628 PP_SET_VCOLOR(pp, NO_VCOLOR);
9776 9629 break;
9777 9630
9778 9631 case HAT_UNCACHE:
9779 9632 PP_SETPNC(pp);
9780 9633 PP_CLRTNC(pp);
9781 9634 PP_SET_VCOLOR(pp, NO_VCOLOR);
9782 9635 break;
9783 9636 }
9784 9637 }
9785 9638 #endif /* VAC */
9786 9639
9787 9640
9788 9641 /*
9789 9642 * Wrapper routine used to return a context.
9790 9643 *
9791 9644 * It's the responsibility of the caller to guarantee that the
9792 9645 * process serializes on calls here by taking the HAT lock for
9793 9646 * the hat.
9794 9647 *
9795 9648 */
9796 9649 static void
9797 9650 sfmmu_get_ctx(sfmmu_t *sfmmup)
9798 9651 {
9799 9652 mmu_ctx_t *mmu_ctxp;
9800 9653 uint_t pstate_save;
9801 9654 int ret;
9802 9655
9803 9656 ASSERT(sfmmu_hat_lock_held(sfmmup));
9804 9657 ASSERT(sfmmup != ksfmmup);
9805 9658
9806 9659 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_ALLCTX_INVALID)) {
9807 9660 sfmmu_setup_tsbinfo(sfmmup);
9808 9661 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ALLCTX_INVALID);
9809 9662 }
9810 9663
9811 9664 kpreempt_disable();
9812 9665
9813 9666 mmu_ctxp = CPU_MMU_CTXP(CPU);
9814 9667 ASSERT(mmu_ctxp);
9815 9668 ASSERT(mmu_ctxp->mmu_idx < max_mmu_ctxdoms);
9816 9669 ASSERT(mmu_ctxp == mmu_ctxs_tbl[mmu_ctxp->mmu_idx]);
9817 9670
9818 9671 /*
9819 9672 * Do a wrap-around if cnum reaches the max # cnum supported by a MMU.
9820 9673 */
9821 9674 if (mmu_ctxp->mmu_cnum == mmu_ctxp->mmu_nctxs)
9822 9675 sfmmu_ctx_wrap_around(mmu_ctxp, B_TRUE);
9823 9676
9824 9677 /*
9825 9678 * Let the MMU set up the page sizes to use for
9826 9679 * this context in the TLB. Don't program 2nd dtlb for ism hat.
9827 9680 */
9828 9681 if ((&mmu_set_ctx_page_sizes) && (sfmmup->sfmmu_ismhat == 0)) {
9829 9682 mmu_set_ctx_page_sizes(sfmmup);
9830 9683 }
9831 9684
9832 9685 /*
9833 9686 * sfmmu_alloc_ctx and sfmmu_load_mmustate will be performed with
9834 9687 * interrupts disabled to prevent race condition with wrap-around
9835 9688 * ctx invalidatation. In sun4v, ctx invalidation also involves
9836 9689 * a HV call to set the number of TSBs to 0. If interrupts are not
9837 9690 * disabled until after sfmmu_load_mmustate is complete TSBs may
9838 9691 * become assigned to INVALID_CONTEXT. This is not allowed.
9839 9692 */
9840 9693 pstate_save = sfmmu_disable_intrs();
9841 9694
9842 9695 if (sfmmu_alloc_ctx(sfmmup, 1, CPU, SFMMU_PRIVATE) &&
9843 9696 sfmmup->sfmmu_scdp != NULL) {
9844 9697 sf_scd_t *scdp = sfmmup->sfmmu_scdp;
9845 9698 sfmmu_t *scsfmmup = scdp->scd_sfmmup;
9846 9699 ret = sfmmu_alloc_ctx(scsfmmup, 1, CPU, SFMMU_SHARED);
9847 9700 /* debug purpose only */
9848 9701 ASSERT(!ret || scsfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum
9849 9702 != INVALID_CONTEXT);
9850 9703 }
9851 9704 sfmmu_load_mmustate(sfmmup);
9852 9705
9853 9706 sfmmu_enable_intrs(pstate_save);
9854 9707
9855 9708 kpreempt_enable();
9856 9709 }
9857 9710
9858 9711 /*
9859 9712 * When all cnums are used up in a MMU, cnum will wrap around to the
9860 9713 * next generation and start from 2.
9861 9714 */
9862 9715 static void
9863 9716 sfmmu_ctx_wrap_around(mmu_ctx_t *mmu_ctxp, boolean_t reset_cnum)
9864 9717 {
9865 9718
9866 9719 /* caller must have disabled the preemption */
9867 9720 ASSERT(curthread->t_preempt >= 1);
9868 9721 ASSERT(mmu_ctxp != NULL);
9869 9722
9870 9723 /* acquire Per-MMU (PM) spin lock */
9871 9724 mutex_enter(&mmu_ctxp->mmu_lock);
9872 9725
9873 9726 /* re-check to see if wrap-around is needed */
9874 9727 if (mmu_ctxp->mmu_cnum < mmu_ctxp->mmu_nctxs)
9875 9728 goto done;
9876 9729
9877 9730 SFMMU_MMU_STAT(mmu_wrap_around);
9878 9731
9879 9732 /* update gnum */
9880 9733 ASSERT(mmu_ctxp->mmu_gnum != 0);
9881 9734 mmu_ctxp->mmu_gnum++;
9882 9735 if (mmu_ctxp->mmu_gnum == 0 ||
9883 9736 mmu_ctxp->mmu_gnum > MAX_SFMMU_GNUM_VAL) {
9884 9737 cmn_err(CE_PANIC, "mmu_gnum of mmu_ctx 0x%p is out of bound.",
9885 9738 (void *)mmu_ctxp);
9886 9739 }
9887 9740
9888 9741 if (mmu_ctxp->mmu_ncpus > 1) {
9889 9742 cpuset_t cpuset;
9890 9743
9891 9744 membar_enter(); /* make sure updated gnum visible */
9892 9745
9893 9746 SFMMU_XCALL_STATS(NULL);
9894 9747
9895 9748 /* xcall to others on the same MMU to invalidate ctx */
9896 9749 cpuset = mmu_ctxp->mmu_cpuset;
9897 9750 ASSERT(CPU_IN_SET(cpuset, CPU->cpu_id) || !reset_cnum);
9898 9751 CPUSET_DEL(cpuset, CPU->cpu_id);
9899 9752 CPUSET_AND(cpuset, cpu_ready_set);
9900 9753
9901 9754 /*
9902 9755 * Pass in INVALID_CONTEXT as the first parameter to
9903 9756 * sfmmu_raise_tsb_exception, which invalidates the context
9904 9757 * of any process running on the CPUs in the MMU.
9905 9758 */
9906 9759 xt_some(cpuset, sfmmu_raise_tsb_exception,
9907 9760 INVALID_CONTEXT, INVALID_CONTEXT);
9908 9761 xt_sync(cpuset);
9909 9762
9910 9763 SFMMU_MMU_STAT(mmu_tsb_raise_exception);
9911 9764 }
9912 9765
9913 9766 if (sfmmu_getctx_sec() != INVALID_CONTEXT) {
9914 9767 sfmmu_setctx_sec(INVALID_CONTEXT);
9915 9768 sfmmu_clear_utsbinfo();
9916 9769 }
9917 9770
9918 9771 /*
9919 9772 * No xcall is needed here. For sun4u systems all CPUs in context
9920 9773 * domain share a single physical MMU therefore it's enough to flush
9921 9774 * TLB on local CPU. On sun4v systems we use 1 global context
9922 9775 * domain and flush all remote TLBs in sfmmu_raise_tsb_exception
9923 9776 * handler. Note that vtag_flushall_uctxs() is called
9924 9777 * for Ultra II machine, where the equivalent flushall functionality
9925 9778 * is implemented in SW, and only user ctx TLB entries are flushed.
9926 9779 */
9927 9780 if (&vtag_flushall_uctxs != NULL) {
9928 9781 vtag_flushall_uctxs();
9929 9782 } else {
9930 9783 vtag_flushall();
9931 9784 }
9932 9785
9933 9786 /* reset mmu cnum, skips cnum 0 and 1 */
9934 9787 if (reset_cnum == B_TRUE)
9935 9788 mmu_ctxp->mmu_cnum = NUM_LOCKED_CTXS;
9936 9789
9937 9790 done:
9938 9791 mutex_exit(&mmu_ctxp->mmu_lock);
9939 9792 }
9940 9793
9941 9794
9942 9795 /*
9943 9796 * For multi-threaded process, set the process context to INVALID_CONTEXT
9944 9797 * so that it faults and reloads the MMU state from TL=0. For single-threaded
9945 9798 * process, we can just load the MMU state directly without having to
9946 9799 * set context invalid. Caller must hold the hat lock since we don't
9947 9800 * acquire it here.
9948 9801 */
9949 9802 static void
9950 9803 sfmmu_sync_mmustate(sfmmu_t *sfmmup)
9951 9804 {
9952 9805 uint_t cnum;
9953 9806 uint_t pstate_save;
9954 9807
9955 9808 ASSERT(sfmmup != ksfmmup);
9956 9809 ASSERT(sfmmu_hat_lock_held(sfmmup));
9957 9810
9958 9811 kpreempt_disable();
9959 9812
9960 9813 /*
9961 9814 * We check whether the pass'ed-in sfmmup is the same as the
9962 9815 * current running proc. This is to makes sure the current proc
9963 9816 * stays single-threaded if it already is.
9964 9817 */
9965 9818 if ((sfmmup == curthread->t_procp->p_as->a_hat) &&
9966 9819 (curthread->t_procp->p_lwpcnt == 1)) {
9967 9820 /* single-thread */
9968 9821 cnum = sfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum;
9969 9822 if (cnum != INVALID_CONTEXT) {
9970 9823 uint_t curcnum;
9971 9824 /*
9972 9825 * Disable interrupts to prevent race condition
9973 9826 * with sfmmu_ctx_wrap_around ctx invalidation.
9974 9827 * In sun4v, ctx invalidation involves setting
9975 9828 * TSB to NULL, hence, interrupts should be disabled
9976 9829 * untill after sfmmu_load_mmustate is completed.
9977 9830 */
9978 9831 pstate_save = sfmmu_disable_intrs();
9979 9832 curcnum = sfmmu_getctx_sec();
9980 9833 if (curcnum == cnum)
9981 9834 sfmmu_load_mmustate(sfmmup);
9982 9835 sfmmu_enable_intrs(pstate_save);
9983 9836 ASSERT(curcnum == cnum || curcnum == INVALID_CONTEXT);
9984 9837 }
9985 9838 } else {
9986 9839 /*
9987 9840 * multi-thread
9988 9841 * or when sfmmup is not the same as the curproc.
↓ open down ↓ |
7853 lines elided |
↑ open up ↑ |
9989 9842 */
9990 9843 sfmmu_invalidate_ctx(sfmmup);
9991 9844 }
9992 9845
9993 9846 kpreempt_enable();
9994 9847 }
9995 9848
9996 9849
9997 9850 /*
9998 9851 * Replace the specified TSB with a new TSB. This function gets called when
9999 - * we grow, shrink or swapin a TSB. When swapping in a TSB (TSB_SWAPIN), the
9852 + * we grow, or shrink a TSB. When swapping in a TSB (TSB_SWAPIN), the
10000 9853 * TSB_FORCEALLOC flag may be used to force allocation of a minimum-sized TSB
10001 9854 * (8K).
10002 9855 *
10003 9856 * Caller must hold the HAT lock, but should assume any tsb_info
10004 9857 * pointers it has are no longer valid after calling this function.
10005 9858 *
10006 9859 * Return values:
10007 9860 * TSB_ALLOCFAIL Failed to allocate a TSB, due to memory constraints
10008 9861 * TSB_LOSTRACE HAT is busy, i.e. another thread is already doing
10009 9862 * something to this tsbinfo/TSB
10010 9863 * TSB_SUCCESS Operation succeeded
10011 9864 */
10012 9865 static tsb_replace_rc_t
10013 9866 sfmmu_replace_tsb(sfmmu_t *sfmmup, struct tsb_info *old_tsbinfo, uint_t szc,
10014 9867 hatlock_t *hatlockp, uint_t flags)
10015 9868 {
10016 9869 struct tsb_info *new_tsbinfo = NULL;
10017 9870 struct tsb_info *curtsb, *prevtsb;
10018 9871 uint_t tte_sz_mask;
10019 9872 int i;
10020 9873
10021 9874 ASSERT(sfmmup != ksfmmup);
10022 9875 ASSERT(sfmmup->sfmmu_ismhat == 0);
10023 9876 ASSERT(sfmmu_hat_lock_held(sfmmup));
10024 9877 ASSERT(szc <= tsb_max_growsize);
10025 9878
10026 9879 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_BUSY))
10027 9880 return (TSB_LOSTRACE);
10028 9881
10029 9882 /*
10030 9883 * Find the tsb_info ahead of this one in the list, and
10031 9884 * also make sure that the tsb_info passed in really
10032 9885 * exists!
10033 9886 */
10034 9887 for (prevtsb = NULL, curtsb = sfmmup->sfmmu_tsb;
10035 9888 curtsb != old_tsbinfo && curtsb != NULL;
10036 9889 prevtsb = curtsb, curtsb = curtsb->tsb_next)
10037 9890 ;
10038 9891 ASSERT(curtsb != NULL);
10039 9892
10040 9893 if (!(flags & TSB_SWAPIN) && SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) {
10041 9894 /*
10042 9895 * The process is swapped out, so just set the new size
10043 9896 * code. When it swaps back in, we'll allocate a new one
10044 9897 * of the new chosen size.
10045 9898 */
10046 9899 curtsb->tsb_szc = szc;
10047 9900 return (TSB_SUCCESS);
10048 9901 }
10049 9902 SFMMU_FLAGS_SET(sfmmup, HAT_BUSY);
10050 9903
10051 9904 tte_sz_mask = old_tsbinfo->tsb_ttesz_mask;
10052 9905
10053 9906 /*
10054 9907 * All initialization is done inside of sfmmu_tsbinfo_alloc().
10055 9908 * If we fail to allocate a TSB, exit.
10056 9909 *
10057 9910 * If tsb grows with new tsb size > 4M and old tsb size < 4M,
10058 9911 * then try 4M slab after the initial alloc fails.
10059 9912 *
10060 9913 * If tsb swapin with tsb size > 4M, then try 4M after the
10061 9914 * initial alloc fails.
10062 9915 */
10063 9916 sfmmu_hat_exit(hatlockp);
10064 9917 if (sfmmu_tsbinfo_alloc(&new_tsbinfo, szc,
10065 9918 tte_sz_mask, flags, sfmmup) &&
10066 9919 (!(flags & (TSB_GROW | TSB_SWAPIN)) || (szc <= TSB_4M_SZCODE) ||
10067 9920 (!(flags & TSB_SWAPIN) &&
10068 9921 (old_tsbinfo->tsb_szc >= TSB_4M_SZCODE)) ||
10069 9922 sfmmu_tsbinfo_alloc(&new_tsbinfo, TSB_4M_SZCODE,
10070 9923 tte_sz_mask, flags, sfmmup))) {
10071 9924 (void) sfmmu_hat_enter(sfmmup);
10072 9925 if (!(flags & TSB_SWAPIN))
10073 9926 SFMMU_STAT(sf_tsb_resize_failures);
10074 9927 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY);
10075 9928 return (TSB_ALLOCFAIL);
10076 9929 }
10077 9930 (void) sfmmu_hat_enter(sfmmup);
10078 9931
10079 9932 /*
10080 9933 * Re-check to make sure somebody else didn't muck with us while we
10081 9934 * didn't hold the HAT lock. If the process swapped out, fine, just
10082 9935 * exit; this can happen if we try to shrink the TSB from the context
10083 9936 * of another process (such as on an ISM unmap), though it is rare.
10084 9937 */
10085 9938 if (!(flags & TSB_SWAPIN) && SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) {
10086 9939 SFMMU_STAT(sf_tsb_resize_failures);
10087 9940 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY);
10088 9941 sfmmu_hat_exit(hatlockp);
10089 9942 sfmmu_tsbinfo_free(new_tsbinfo);
10090 9943 (void) sfmmu_hat_enter(sfmmup);
10091 9944 return (TSB_LOSTRACE);
10092 9945 }
10093 9946
10094 9947 #ifdef DEBUG
10095 9948 /* Reverify that the tsb_info still exists.. for debugging only */
10096 9949 for (prevtsb = NULL, curtsb = sfmmup->sfmmu_tsb;
10097 9950 curtsb != old_tsbinfo && curtsb != NULL;
10098 9951 prevtsb = curtsb, curtsb = curtsb->tsb_next)
10099 9952 ;
10100 9953 ASSERT(curtsb != NULL);
10101 9954 #endif /* DEBUG */
10102 9955
10103 9956 /*
10104 9957 * Quiesce any CPUs running this process on their next TLB miss
10105 9958 * so they atomically see the new tsb_info. We temporarily set the
10106 9959 * context to invalid context so new threads that come on processor
10107 9960 * after we do the xcall to cpusran will also serialize behind the
10108 9961 * HAT lock on TLB miss and will see the new TSB. Since this short
10109 9962 * race with a new thread coming on processor is relatively rare,
10110 9963 * this synchronization mechanism should be cheaper than always
10111 9964 * pausing all CPUs for the duration of the setup, which is what
10112 9965 * the old implementation did. This is particuarly true if we are
10113 9966 * copying a huge chunk of memory around during that window.
10114 9967 *
10115 9968 * The memory barriers are to make sure things stay consistent
10116 9969 * with resume() since it does not hold the HAT lock while
10117 9970 * walking the list of tsb_info structures.
10118 9971 */
10119 9972 if ((flags & TSB_SWAPIN) != TSB_SWAPIN) {
10120 9973 /* The TSB is either growing or shrinking. */
10121 9974 sfmmu_invalidate_ctx(sfmmup);
10122 9975 } else {
10123 9976 /*
10124 9977 * It is illegal to swap in TSBs from a process other
10125 9978 * than a process being swapped in. This in turn
10126 9979 * implies we do not have a valid MMU context here
10127 9980 * since a process needs one to resolve translation
10128 9981 * misses.
10129 9982 */
10130 9983 ASSERT(curthread->t_procp->p_as->a_hat == sfmmup);
10131 9984 }
10132 9985
10133 9986 #ifdef DEBUG
10134 9987 ASSERT(max_mmu_ctxdoms > 0);
10135 9988
10136 9989 /*
10137 9990 * Process should have INVALID_CONTEXT on all MMUs
10138 9991 */
10139 9992 for (i = 0; i < max_mmu_ctxdoms; i++) {
10140 9993
10141 9994 ASSERT(sfmmup->sfmmu_ctxs[i].cnum == INVALID_CONTEXT);
10142 9995 }
10143 9996 #endif
10144 9997
10145 9998 new_tsbinfo->tsb_next = old_tsbinfo->tsb_next;
10146 9999 membar_stst(); /* strict ordering required */
10147 10000 if (prevtsb)
10148 10001 prevtsb->tsb_next = new_tsbinfo;
10149 10002 else
10150 10003 sfmmup->sfmmu_tsb = new_tsbinfo;
10151 10004 membar_enter(); /* make sure new TSB globally visible */
10152 10005
10153 10006 /*
10154 10007 * We need to migrate TSB entries from the old TSB to the new TSB
10155 10008 * if tsb_remap_ttes is set and the TSB is growing.
10156 10009 */
10157 10010 if (tsb_remap_ttes && ((flags & TSB_GROW) == TSB_GROW))
10158 10011 sfmmu_copy_tsb(old_tsbinfo, new_tsbinfo);
10159 10012
10160 10013 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY);
10161 10014
10162 10015 /*
10163 10016 * Drop the HAT lock to free our old tsb_info.
10164 10017 */
10165 10018 sfmmu_hat_exit(hatlockp);
10166 10019
10167 10020 if ((flags & TSB_GROW) == TSB_GROW) {
10168 10021 SFMMU_STAT(sf_tsb_grow);
10169 10022 } else if ((flags & TSB_SHRINK) == TSB_SHRINK) {
10170 10023 SFMMU_STAT(sf_tsb_shrink);
10171 10024 }
10172 10025
10173 10026 sfmmu_tsbinfo_free(old_tsbinfo);
10174 10027
10175 10028 (void) sfmmu_hat_enter(sfmmup);
10176 10029 return (TSB_SUCCESS);
10177 10030 }
10178 10031
10179 10032 /*
10180 10033 * This function will re-program hat pgsz array, and invalidate the
10181 10034 * process' context, forcing the process to switch to another
10182 10035 * context on the next TLB miss, and therefore start using the
10183 10036 * TLB that is reprogrammed for the new page sizes.
10184 10037 */
10185 10038 void
10186 10039 sfmmu_reprog_pgsz_arr(sfmmu_t *sfmmup, uint8_t *tmp_pgsz)
10187 10040 {
10188 10041 int i;
10189 10042 hatlock_t *hatlockp = NULL;
10190 10043
10191 10044 hatlockp = sfmmu_hat_enter(sfmmup);
10192 10045 /* USIII+-IV+ optimization, requires hat lock */
10193 10046 if (tmp_pgsz) {
10194 10047 for (i = 0; i < mmu_page_sizes; i++)
10195 10048 sfmmup->sfmmu_pgsz[i] = tmp_pgsz[i];
10196 10049 }
10197 10050 SFMMU_STAT(sf_tlb_reprog_pgsz);
10198 10051
10199 10052 sfmmu_invalidate_ctx(sfmmup);
10200 10053
10201 10054 sfmmu_hat_exit(hatlockp);
10202 10055 }
10203 10056
10204 10057 /*
10205 10058 * The scd_rttecnt field in the SCD must be updated to take account of the
10206 10059 * regions which it contains.
10207 10060 */
10208 10061 static void
10209 10062 sfmmu_set_scd_rttecnt(sf_srd_t *srdp, sf_scd_t *scdp)
10210 10063 {
10211 10064 uint_t rid;
10212 10065 uint_t i, j;
10213 10066 ulong_t w;
10214 10067 sf_region_t *rgnp;
10215 10068
10216 10069 ASSERT(srdp != NULL);
10217 10070
10218 10071 for (i = 0; i < SFMMU_HMERGNMAP_WORDS; i++) {
10219 10072 if ((w = scdp->scd_region_map.bitmap[i]) == 0) {
10220 10073 continue;
10221 10074 }
10222 10075
10223 10076 j = 0;
10224 10077 while (w) {
10225 10078 if (!(w & 0x1)) {
10226 10079 j++;
10227 10080 w >>= 1;
10228 10081 continue;
10229 10082 }
10230 10083 rid = (i << BT_ULSHIFT) | j;
10231 10084 j++;
10232 10085 w >>= 1;
10233 10086
10234 10087 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
10235 10088 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
10236 10089 rgnp = srdp->srd_hmergnp[rid];
10237 10090 ASSERT(rgnp->rgn_refcnt > 0);
10238 10091 ASSERT(rgnp->rgn_id == rid);
10239 10092
10240 10093 scdp->scd_rttecnt[rgnp->rgn_pgszc] +=
10241 10094 rgnp->rgn_size >> TTE_PAGE_SHIFT(rgnp->rgn_pgszc);
10242 10095
10243 10096 /*
10244 10097 * Maintain the tsb0 inflation cnt for the regions
10245 10098 * in the SCD.
10246 10099 */
10247 10100 if (rgnp->rgn_pgszc >= TTE4M) {
10248 10101 scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt +=
10249 10102 rgnp->rgn_size >>
10250 10103 (TTE_PAGE_SHIFT(TTE8K) + 2);
10251 10104 }
10252 10105 }
10253 10106 }
10254 10107 }
10255 10108
10256 10109 /*
10257 10110 * This function assumes that there are either four or six supported page
10258 10111 * sizes and at most two programmable TLBs, so we need to decide which
10259 10112 * page sizes are most important and then tell the MMU layer so it
10260 10113 * can adjust the TLB page sizes accordingly (if supported).
10261 10114 *
10262 10115 * If these assumptions change, this function will need to be
10263 10116 * updated to support whatever the new limits are.
10264 10117 *
10265 10118 * The growing flag is nonzero if we are growing the address space,
10266 10119 * and zero if it is shrinking. This allows us to decide whether
10267 10120 * to grow or shrink our TSB, depending upon available memory
10268 10121 * conditions.
10269 10122 */
10270 10123 static void
10271 10124 sfmmu_check_page_sizes(sfmmu_t *sfmmup, int growing)
10272 10125 {
10273 10126 uint64_t ttecnt[MMU_PAGE_SIZES];
10274 10127 uint64_t tte8k_cnt, tte4m_cnt;
10275 10128 uint8_t i;
10276 10129 int sectsb_thresh;
10277 10130
10278 10131 /*
10279 10132 * Kernel threads, processes with small address spaces not using
10280 10133 * large pages, and dummy ISM HATs need not apply.
10281 10134 */
10282 10135 if (sfmmup == ksfmmup || sfmmup->sfmmu_ismhat != NULL)
10283 10136 return;
10284 10137
10285 10138 if (!SFMMU_LGPGS_INUSE(sfmmup) &&
10286 10139 sfmmup->sfmmu_ttecnt[TTE8K] <= tsb_rss_factor)
10287 10140 return;
10288 10141
10289 10142 for (i = 0; i < mmu_page_sizes; i++) {
10290 10143 ttecnt[i] = sfmmup->sfmmu_ttecnt[i] +
10291 10144 sfmmup->sfmmu_ismttecnt[i];
10292 10145 }
10293 10146
10294 10147 /* Check pagesizes in use, and possibly reprogram DTLB. */
10295 10148 if (&mmu_check_page_sizes)
10296 10149 mmu_check_page_sizes(sfmmup, ttecnt);
10297 10150
10298 10151 /*
10299 10152 * Calculate the number of 8k ttes to represent the span of these
10300 10153 * pages.
10301 10154 */
10302 10155 tte8k_cnt = ttecnt[TTE8K] +
10303 10156 (ttecnt[TTE64K] << (MMU_PAGESHIFT64K - MMU_PAGESHIFT)) +
10304 10157 (ttecnt[TTE512K] << (MMU_PAGESHIFT512K - MMU_PAGESHIFT));
10305 10158 if (mmu_page_sizes == max_mmu_page_sizes) {
10306 10159 tte4m_cnt = ttecnt[TTE4M] +
10307 10160 (ttecnt[TTE32M] << (MMU_PAGESHIFT32M - MMU_PAGESHIFT4M)) +
10308 10161 (ttecnt[TTE256M] << (MMU_PAGESHIFT256M - MMU_PAGESHIFT4M));
10309 10162 } else {
10310 10163 tte4m_cnt = ttecnt[TTE4M];
10311 10164 }
10312 10165
10313 10166 /*
10314 10167 * Inflate tte8k_cnt to allow for region large page allocation failure.
10315 10168 */
10316 10169 tte8k_cnt += sfmmup->sfmmu_tsb0_4minflcnt;
10317 10170
10318 10171 /*
10319 10172 * Inflate TSB sizes by a factor of 2 if this process
10320 10173 * uses 4M text pages to minimize extra conflict misses
10321 10174 * in the first TSB since without counting text pages
10322 10175 * 8K TSB may become too small.
10323 10176 *
10324 10177 * Also double the size of the second TSB to minimize
10325 10178 * extra conflict misses due to competition between 4M text pages
10326 10179 * and data pages.
10327 10180 *
10328 10181 * We need to adjust the second TSB allocation threshold by the
10329 10182 * inflation factor, since there is no point in creating a second
10330 10183 * TSB when we know all the mappings can fit in the I/D TLBs.
10331 10184 */
10332 10185 sectsb_thresh = tsb_sectsb_threshold;
10333 10186 if (sfmmup->sfmmu_flags & HAT_4MTEXT_FLAG) {
10334 10187 tte8k_cnt <<= 1;
10335 10188 tte4m_cnt <<= 1;
10336 10189 sectsb_thresh <<= 1;
10337 10190 }
10338 10191
10339 10192 /*
10340 10193 * Check to see if our TSB is the right size; we may need to
10341 10194 * grow or shrink it. If the process is small, our work is
10342 10195 * finished at this point.
10343 10196 */
10344 10197 if (tte8k_cnt <= tsb_rss_factor && tte4m_cnt <= sectsb_thresh) {
10345 10198 return;
10346 10199 }
10347 10200 sfmmu_size_tsb(sfmmup, growing, tte8k_cnt, tte4m_cnt, sectsb_thresh);
10348 10201 }
10349 10202
10350 10203 static void
10351 10204 sfmmu_size_tsb(sfmmu_t *sfmmup, int growing, uint64_t tte8k_cnt,
10352 10205 uint64_t tte4m_cnt, int sectsb_thresh)
10353 10206 {
10354 10207 int tsb_bits;
10355 10208 uint_t tsb_szc;
10356 10209 struct tsb_info *tsbinfop;
10357 10210 hatlock_t *hatlockp = NULL;
10358 10211
10359 10212 hatlockp = sfmmu_hat_enter(sfmmup);
10360 10213 ASSERT(hatlockp != NULL);
10361 10214 tsbinfop = sfmmup->sfmmu_tsb;
10362 10215 ASSERT(tsbinfop != NULL);
10363 10216
10364 10217 /*
10365 10218 * If we're growing, select the size based on RSS. If we're
10366 10219 * shrinking, leave some room so we don't have to turn around and
10367 10220 * grow again immediately.
10368 10221 */
10369 10222 if (growing)
10370 10223 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt);
10371 10224 else
10372 10225 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt << 1);
10373 10226
10374 10227 if (!growing && (tsb_szc < tsbinfop->tsb_szc) &&
10375 10228 (tsb_szc >= default_tsb_size) && TSB_OK_SHRINK()) {
10376 10229 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, tsb_szc,
10377 10230 hatlockp, TSB_SHRINK);
10378 10231 } else if (growing && tsb_szc > tsbinfop->tsb_szc && TSB_OK_GROW()) {
10379 10232 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, tsb_szc,
10380 10233 hatlockp, TSB_GROW);
10381 10234 }
10382 10235 tsbinfop = sfmmup->sfmmu_tsb;
10383 10236
10384 10237 /*
10385 10238 * With the TLB and first TSB out of the way, we need to see if
10386 10239 * we need a second TSB for 4M pages. If we managed to reprogram
10387 10240 * the TLB page sizes above, the process will start using this new
10388 10241 * TSB right away; otherwise, it will start using it on the next
10389 10242 * context switch. Either way, it's no big deal so there's no
10390 10243 * synchronization with the trap handlers here unless we grow the
10391 10244 * TSB (in which case it's required to prevent using the old one
10392 10245 * after it's freed). Note: second tsb is required for 32M/256M
10393 10246 * page sizes.
10394 10247 */
10395 10248 if (tte4m_cnt > sectsb_thresh) {
10396 10249 /*
10397 10250 * If we're growing, select the size based on RSS. If we're
10398 10251 * shrinking, leave some room so we don't have to turn
10399 10252 * around and grow again immediately.
10400 10253 */
10401 10254 if (growing)
10402 10255 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt);
10403 10256 else
10404 10257 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt << 1);
10405 10258 if (tsbinfop->tsb_next == NULL) {
10406 10259 struct tsb_info *newtsb;
10407 10260 int allocflags = SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)?
10408 10261 0 : TSB_ALLOC;
10409 10262
10410 10263 sfmmu_hat_exit(hatlockp);
10411 10264
10412 10265 /*
10413 10266 * Try to allocate a TSB for 4[32|256]M pages. If we
10414 10267 * can't get the size we want, retry w/a minimum sized
10415 10268 * TSB. If that still didn't work, give up; we can
10416 10269 * still run without one.
10417 10270 */
10418 10271 tsb_bits = (mmu_page_sizes == max_mmu_page_sizes)?
10419 10272 TSB4M|TSB32M|TSB256M:TSB4M;
10420 10273 if ((sfmmu_tsbinfo_alloc(&newtsb, tsb_szc, tsb_bits,
10421 10274 allocflags, sfmmup)) &&
10422 10275 (tsb_szc <= TSB_4M_SZCODE ||
10423 10276 sfmmu_tsbinfo_alloc(&newtsb, TSB_4M_SZCODE,
10424 10277 tsb_bits, allocflags, sfmmup)) &&
10425 10278 sfmmu_tsbinfo_alloc(&newtsb, TSB_MIN_SZCODE,
10426 10279 tsb_bits, allocflags, sfmmup)) {
10427 10280 return;
10428 10281 }
10429 10282
10430 10283 hatlockp = sfmmu_hat_enter(sfmmup);
10431 10284
10432 10285 sfmmu_invalidate_ctx(sfmmup);
10433 10286
10434 10287 if (sfmmup->sfmmu_tsb->tsb_next == NULL) {
10435 10288 sfmmup->sfmmu_tsb->tsb_next = newtsb;
10436 10289 SFMMU_STAT(sf_tsb_sectsb_create);
10437 10290 sfmmu_hat_exit(hatlockp);
10438 10291 return;
10439 10292 } else {
10440 10293 /*
10441 10294 * It's annoying, but possible for us
10442 10295 * to get here.. we dropped the HAT lock
10443 10296 * because of locking order in the kmem
10444 10297 * allocator, and while we were off getting
10445 10298 * our memory, some other thread decided to
10446 10299 * do us a favor and won the race to get a
10447 10300 * second TSB for this process. Sigh.
10448 10301 */
10449 10302 sfmmu_hat_exit(hatlockp);
10450 10303 sfmmu_tsbinfo_free(newtsb);
10451 10304 return;
10452 10305 }
10453 10306 }
10454 10307
10455 10308 /*
10456 10309 * We have a second TSB, see if it's big enough.
10457 10310 */
10458 10311 tsbinfop = tsbinfop->tsb_next;
10459 10312
10460 10313 /*
10461 10314 * Check to see if our second TSB is the right size;
10462 10315 * we may need to grow or shrink it.
10463 10316 * To prevent thrashing (e.g. growing the TSB on a
10464 10317 * subsequent map operation), only try to shrink if
10465 10318 * the TSB reach exceeds twice the virtual address
10466 10319 * space size.
10467 10320 */
10468 10321 if (!growing && (tsb_szc < tsbinfop->tsb_szc) &&
10469 10322 (tsb_szc >= default_tsb_size) && TSB_OK_SHRINK()) {
10470 10323 (void) sfmmu_replace_tsb(sfmmup, tsbinfop,
10471 10324 tsb_szc, hatlockp, TSB_SHRINK);
10472 10325 } else if (growing && tsb_szc > tsbinfop->tsb_szc &&
10473 10326 TSB_OK_GROW()) {
10474 10327 (void) sfmmu_replace_tsb(sfmmup, tsbinfop,
10475 10328 tsb_szc, hatlockp, TSB_GROW);
10476 10329 }
10477 10330 }
10478 10331
10479 10332 sfmmu_hat_exit(hatlockp);
10480 10333 }
10481 10334
10482 10335 /*
10483 10336 * Free up a sfmmu
10484 10337 * Since the sfmmu is currently embedded in the hat struct we simply zero
10485 10338 * out our fields and free up the ism map blk list if any.
10486 10339 */
10487 10340 static void
10488 10341 sfmmu_free_sfmmu(sfmmu_t *sfmmup)
10489 10342 {
10490 10343 ism_blk_t *blkp, *nx_blkp;
10491 10344 #ifdef DEBUG
10492 10345 ism_map_t *map;
10493 10346 int i;
10494 10347 #endif
10495 10348
10496 10349 ASSERT(sfmmup->sfmmu_ttecnt[TTE8K] == 0);
10497 10350 ASSERT(sfmmup->sfmmu_ttecnt[TTE64K] == 0);
10498 10351 ASSERT(sfmmup->sfmmu_ttecnt[TTE512K] == 0);
10499 10352 ASSERT(sfmmup->sfmmu_ttecnt[TTE4M] == 0);
10500 10353 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0);
10501 10354 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0);
10502 10355 ASSERT(SF_RGNMAP_ISNULL(sfmmup));
10503 10356
10504 10357 sfmmup->sfmmu_free = 0;
10505 10358 sfmmup->sfmmu_ismhat = 0;
10506 10359
10507 10360 blkp = sfmmup->sfmmu_iblk;
10508 10361 sfmmup->sfmmu_iblk = NULL;
10509 10362
10510 10363 while (blkp) {
10511 10364 #ifdef DEBUG
10512 10365 map = blkp->iblk_maps;
10513 10366 for (i = 0; i < ISM_MAP_SLOTS; i++) {
10514 10367 ASSERT(map[i].imap_seg == 0);
10515 10368 ASSERT(map[i].imap_ismhat == NULL);
10516 10369 ASSERT(map[i].imap_ment == NULL);
10517 10370 }
10518 10371 #endif
10519 10372 nx_blkp = blkp->iblk_next;
10520 10373 blkp->iblk_next = NULL;
10521 10374 blkp->iblk_nextpa = (uint64_t)-1;
10522 10375 kmem_cache_free(ism_blk_cache, blkp);
10523 10376 blkp = nx_blkp;
10524 10377 }
10525 10378 }
10526 10379
10527 10380 /*
10528 10381 * Locking primitves accessed by HATLOCK macros
10529 10382 */
10530 10383
10531 10384 #define SFMMU_SPL_MTX (0x0)
10532 10385 #define SFMMU_ML_MTX (0x1)
10533 10386
10534 10387 #define SFMMU_MLSPL_MTX(type, pg) (((type) == SFMMU_SPL_MTX) ? \
10535 10388 SPL_HASH(pg) : MLIST_HASH(pg))
10536 10389
10537 10390 kmutex_t *
10538 10391 sfmmu_page_enter(struct page *pp)
10539 10392 {
10540 10393 return (sfmmu_mlspl_enter(pp, SFMMU_SPL_MTX));
10541 10394 }
10542 10395
10543 10396 void
10544 10397 sfmmu_page_exit(kmutex_t *spl)
10545 10398 {
10546 10399 mutex_exit(spl);
10547 10400 }
10548 10401
10549 10402 int
10550 10403 sfmmu_page_spl_held(struct page *pp)
10551 10404 {
10552 10405 return (sfmmu_mlspl_held(pp, SFMMU_SPL_MTX));
10553 10406 }
10554 10407
10555 10408 kmutex_t *
10556 10409 sfmmu_mlist_enter(struct page *pp)
10557 10410 {
10558 10411 return (sfmmu_mlspl_enter(pp, SFMMU_ML_MTX));
10559 10412 }
10560 10413
10561 10414 void
10562 10415 sfmmu_mlist_exit(kmutex_t *mml)
10563 10416 {
10564 10417 mutex_exit(mml);
10565 10418 }
10566 10419
10567 10420 int
10568 10421 sfmmu_mlist_held(struct page *pp)
10569 10422 {
10570 10423
10571 10424 return (sfmmu_mlspl_held(pp, SFMMU_ML_MTX));
10572 10425 }
10573 10426
10574 10427 /*
10575 10428 * Common code for sfmmu_mlist_enter() and sfmmu_page_enter(). For
10576 10429 * sfmmu_mlist_enter() case mml_table lock array is used and for
10577 10430 * sfmmu_page_enter() sfmmu_page_lock lock array is used.
10578 10431 *
10579 10432 * The lock is taken on a root page so that it protects an operation on all
10580 10433 * constituent pages of a large page pp belongs to.
10581 10434 *
10582 10435 * The routine takes a lock from the appropriate array. The lock is determined
10583 10436 * by hashing the root page. After taking the lock this routine checks if the
10584 10437 * root page has the same size code that was used to determine the root (i.e
10585 10438 * that root hasn't changed). If root page has the expected p_szc field we
10586 10439 * have the right lock and it's returned to the caller. If root's p_szc
10587 10440 * decreased we release the lock and retry from the beginning. This case can
10588 10441 * happen due to hat_page_demote() decreasing p_szc between our load of p_szc
10589 10442 * value and taking the lock. The number of retries due to p_szc decrease is
10590 10443 * limited by the maximum p_szc value. If p_szc is 0 we return the lock
10591 10444 * determined by hashing pp itself.
10592 10445 *
10593 10446 * If our caller doesn't hold a SE_SHARED or SE_EXCL lock on pp it's also
10594 10447 * possible that p_szc can increase. To increase p_szc a thread has to lock
10595 10448 * all constituent pages EXCL and do hat_pageunload() on all of them. All the
10596 10449 * callers that don't hold a page locked recheck if hmeblk through which pp
10597 10450 * was found still maps this pp. If it doesn't map it anymore returned lock
10598 10451 * is immediately dropped. Therefore if sfmmu_mlspl_enter() hits the case of
10599 10452 * p_szc increase after taking the lock it returns this lock without further
10600 10453 * retries because in this case the caller doesn't care about which lock was
10601 10454 * taken. The caller will drop it right away.
10602 10455 *
10603 10456 * After the routine returns it's guaranteed that hat_page_demote() can't
10604 10457 * change p_szc field of any of constituent pages of a large page pp belongs
10605 10458 * to as long as pp was either locked at least SHARED prior to this call or
10606 10459 * the caller finds that hment that pointed to this pp still references this
10607 10460 * pp (this also assumes that the caller holds hme hash bucket lock so that
10608 10461 * the same pp can't be remapped into the same hmeblk after it was unmapped by
10609 10462 * hat_pageunload()).
10610 10463 */
10611 10464 static kmutex_t *
10612 10465 sfmmu_mlspl_enter(struct page *pp, int type)
10613 10466 {
10614 10467 kmutex_t *mtx;
10615 10468 uint_t prev_rszc = UINT_MAX;
10616 10469 page_t *rootpp;
10617 10470 uint_t szc;
10618 10471 uint_t rszc;
10619 10472 uint_t pszc = pp->p_szc;
10620 10473
10621 10474 ASSERT(pp != NULL);
10622 10475
10623 10476 again:
10624 10477 if (pszc == 0) {
10625 10478 mtx = SFMMU_MLSPL_MTX(type, pp);
10626 10479 mutex_enter(mtx);
10627 10480 return (mtx);
10628 10481 }
10629 10482
10630 10483 /* The lock lives in the root page */
10631 10484 rootpp = PP_GROUPLEADER(pp, pszc);
10632 10485 mtx = SFMMU_MLSPL_MTX(type, rootpp);
10633 10486 mutex_enter(mtx);
10634 10487
10635 10488 /*
10636 10489 * Return mml in the following 3 cases:
10637 10490 *
10638 10491 * 1) If pp itself is root since if its p_szc decreased before we took
10639 10492 * the lock pp is still the root of smaller szc page. And if its p_szc
10640 10493 * increased it doesn't matter what lock we return (see comment in
10641 10494 * front of this routine).
10642 10495 *
10643 10496 * 2) If pp's not root but rootpp is the root of a rootpp->p_szc size
10644 10497 * large page we have the right lock since any previous potential
10645 10498 * hat_page_demote() is done demoting from greater than current root's
10646 10499 * p_szc because hat_page_demote() changes root's p_szc last. No
10647 10500 * further hat_page_demote() can start or be in progress since it
10648 10501 * would need the same lock we currently hold.
10649 10502 *
10650 10503 * 3) If rootpp's p_szc increased since previous iteration it doesn't
10651 10504 * matter what lock we return (see comment in front of this routine).
10652 10505 */
10653 10506 if (pp == rootpp || (rszc = rootpp->p_szc) == pszc ||
10654 10507 rszc >= prev_rszc) {
10655 10508 return (mtx);
10656 10509 }
10657 10510
10658 10511 /*
10659 10512 * hat_page_demote() could have decreased root's p_szc.
10660 10513 * In this case pp's p_szc must also be smaller than pszc.
10661 10514 * Retry.
10662 10515 */
10663 10516 if (rszc < pszc) {
10664 10517 szc = pp->p_szc;
10665 10518 if (szc < pszc) {
10666 10519 mutex_exit(mtx);
10667 10520 pszc = szc;
10668 10521 goto again;
10669 10522 }
10670 10523 /*
10671 10524 * pp's p_szc increased after it was decreased.
10672 10525 * page cannot be mapped. Return current lock. The caller
10673 10526 * will drop it right away.
10674 10527 */
10675 10528 return (mtx);
10676 10529 }
10677 10530
10678 10531 /*
10679 10532 * root's p_szc is greater than pp's p_szc.
10680 10533 * hat_page_demote() is not done with all pages
10681 10534 * yet. Wait for it to complete.
10682 10535 */
10683 10536 mutex_exit(mtx);
10684 10537 rootpp = PP_GROUPLEADER(rootpp, rszc);
10685 10538 mtx = SFMMU_MLSPL_MTX(type, rootpp);
10686 10539 mutex_enter(mtx);
10687 10540 mutex_exit(mtx);
10688 10541 prev_rszc = rszc;
10689 10542 goto again;
10690 10543 }
10691 10544
10692 10545 static int
10693 10546 sfmmu_mlspl_held(struct page *pp, int type)
10694 10547 {
10695 10548 kmutex_t *mtx;
10696 10549
10697 10550 ASSERT(pp != NULL);
10698 10551 /* The lock lives in the root page */
10699 10552 pp = PP_PAGEROOT(pp);
10700 10553 ASSERT(pp != NULL);
10701 10554
10702 10555 mtx = SFMMU_MLSPL_MTX(type, pp);
10703 10556 return (MUTEX_HELD(mtx));
10704 10557 }
10705 10558
10706 10559 static uint_t
10707 10560 sfmmu_get_free_hblk(struct hme_blk **hmeblkpp, uint_t critical)
10708 10561 {
10709 10562 struct hme_blk *hblkp;
10710 10563
10711 10564
10712 10565 if (freehblkp != NULL) {
10713 10566 mutex_enter(&freehblkp_lock);
10714 10567 if (freehblkp != NULL) {
10715 10568 /*
10716 10569 * If the current thread is owning hblk_reserve OR
10717 10570 * critical request from sfmmu_hblk_steal()
10718 10571 * let it succeed even if freehblkcnt is really low.
10719 10572 */
10720 10573 if (freehblkcnt <= HBLK_RESERVE_MIN && !critical) {
10721 10574 SFMMU_STAT(sf_get_free_throttle);
10722 10575 mutex_exit(&freehblkp_lock);
10723 10576 return (0);
10724 10577 }
10725 10578 freehblkcnt--;
10726 10579 *hmeblkpp = freehblkp;
10727 10580 hblkp = *hmeblkpp;
10728 10581 freehblkp = hblkp->hblk_next;
10729 10582 mutex_exit(&freehblkp_lock);
10730 10583 hblkp->hblk_next = NULL;
10731 10584 SFMMU_STAT(sf_get_free_success);
10732 10585
10733 10586 ASSERT(hblkp->hblk_hmecnt == 0);
10734 10587 ASSERT(hblkp->hblk_vcnt == 0);
10735 10588 ASSERT(hblkp->hblk_nextpa == va_to_pa((caddr_t)hblkp));
10736 10589
10737 10590 return (1);
10738 10591 }
10739 10592 mutex_exit(&freehblkp_lock);
10740 10593 }
10741 10594
10742 10595 /* Check cpu hblk pending queues */
10743 10596 if ((*hmeblkpp = sfmmu_check_pending_hblks(TTE8K)) != NULL) {
10744 10597 hblkp = *hmeblkpp;
10745 10598 hblkp->hblk_next = NULL;
10746 10599 hblkp->hblk_nextpa = va_to_pa((caddr_t)hblkp);
10747 10600
10748 10601 ASSERT(hblkp->hblk_hmecnt == 0);
10749 10602 ASSERT(hblkp->hblk_vcnt == 0);
10750 10603
10751 10604 return (1);
10752 10605 }
10753 10606
10754 10607 SFMMU_STAT(sf_get_free_fail);
10755 10608 return (0);
10756 10609 }
10757 10610
10758 10611 static uint_t
10759 10612 sfmmu_put_free_hblk(struct hme_blk *hmeblkp, uint_t critical)
10760 10613 {
10761 10614 struct hme_blk *hblkp;
10762 10615
10763 10616 ASSERT(hmeblkp->hblk_hmecnt == 0);
10764 10617 ASSERT(hmeblkp->hblk_vcnt == 0);
10765 10618 ASSERT(hmeblkp->hblk_nextpa == va_to_pa((caddr_t)hmeblkp));
10766 10619
10767 10620 /*
10768 10621 * If the current thread is mapping into kernel space,
10769 10622 * let it succede even if freehblkcnt is max
10770 10623 * so that it will avoid freeing it to kmem.
10771 10624 * This will prevent stack overflow due to
10772 10625 * possible recursion since kmem_cache_free()
10773 10626 * might require creation of a slab which
10774 10627 * in turn needs an hmeblk to map that slab;
10775 10628 * let's break this vicious chain at the first
10776 10629 * opportunity.
10777 10630 */
10778 10631 if (freehblkcnt < HBLK_RESERVE_CNT || critical) {
10779 10632 mutex_enter(&freehblkp_lock);
10780 10633 if (freehblkcnt < HBLK_RESERVE_CNT || critical) {
10781 10634 SFMMU_STAT(sf_put_free_success);
10782 10635 freehblkcnt++;
10783 10636 hmeblkp->hblk_next = freehblkp;
10784 10637 freehblkp = hmeblkp;
10785 10638 mutex_exit(&freehblkp_lock);
10786 10639 return (1);
10787 10640 }
10788 10641 mutex_exit(&freehblkp_lock);
10789 10642 }
10790 10643
10791 10644 /*
10792 10645 * Bring down freehblkcnt to HBLK_RESERVE_CNT. We are here
10793 10646 * only if freehblkcnt is at least HBLK_RESERVE_CNT *and*
10794 10647 * we are not in the process of mapping into kernel space.
10795 10648 */
10796 10649 ASSERT(!critical);
10797 10650 while (freehblkcnt > HBLK_RESERVE_CNT) {
10798 10651 mutex_enter(&freehblkp_lock);
10799 10652 if (freehblkcnt > HBLK_RESERVE_CNT) {
10800 10653 freehblkcnt--;
10801 10654 hblkp = freehblkp;
10802 10655 freehblkp = hblkp->hblk_next;
10803 10656 mutex_exit(&freehblkp_lock);
10804 10657 ASSERT(get_hblk_cache(hblkp) == sfmmu8_cache);
10805 10658 kmem_cache_free(sfmmu8_cache, hblkp);
10806 10659 continue;
10807 10660 }
10808 10661 mutex_exit(&freehblkp_lock);
10809 10662 }
10810 10663 SFMMU_STAT(sf_put_free_fail);
10811 10664 return (0);
10812 10665 }
10813 10666
10814 10667 static void
10815 10668 sfmmu_hblk_swap(struct hme_blk *new)
10816 10669 {
10817 10670 struct hme_blk *old, *hblkp, *prev;
10818 10671 uint64_t newpa;
10819 10672 caddr_t base, vaddr, endaddr;
10820 10673 struct hmehash_bucket *hmebp;
10821 10674 struct sf_hment *osfhme, *nsfhme;
10822 10675 page_t *pp;
10823 10676 kmutex_t *pml;
10824 10677 tte_t tte;
10825 10678 struct hme_blk *list = NULL;
10826 10679
10827 10680 #ifdef DEBUG
10828 10681 hmeblk_tag hblktag;
10829 10682 struct hme_blk *found;
10830 10683 #endif
10831 10684 old = HBLK_RESERVE;
10832 10685 ASSERT(!old->hblk_shared);
10833 10686
10834 10687 /*
10835 10688 * save pa before bcopy clobbers it
10836 10689 */
10837 10690 newpa = new->hblk_nextpa;
10838 10691
10839 10692 base = (caddr_t)get_hblk_base(old);
10840 10693 endaddr = base + get_hblk_span(old);
10841 10694
10842 10695 /*
10843 10696 * acquire hash bucket lock.
10844 10697 */
10845 10698 hmebp = sfmmu_tteload_acquire_hashbucket(ksfmmup, base, TTE8K,
10846 10699 SFMMU_INVALID_SHMERID);
10847 10700
10848 10701 /*
10849 10702 * copy contents from old to new
10850 10703 */
10851 10704 bcopy((void *)old, (void *)new, HME8BLK_SZ);
10852 10705
10853 10706 /*
10854 10707 * add new to hash chain
10855 10708 */
10856 10709 sfmmu_hblk_hash_add(hmebp, new, newpa);
10857 10710
10858 10711 /*
10859 10712 * search hash chain for hblk_reserve; this needs to be performed
10860 10713 * after adding new, otherwise prev won't correspond to the hblk which
10861 10714 * is prior to old in hash chain when we call sfmmu_hblk_hash_rm to
10862 10715 * remove old later.
10863 10716 */
10864 10717 for (prev = NULL,
10865 10718 hblkp = hmebp->hmeblkp; hblkp != NULL && hblkp != old;
10866 10719 prev = hblkp, hblkp = hblkp->hblk_next)
10867 10720 ;
10868 10721
10869 10722 if (hblkp != old)
10870 10723 panic("sfmmu_hblk_swap: hblk_reserve not found");
10871 10724
10872 10725 /*
10873 10726 * p_mapping list is still pointing to hments in hblk_reserve;
10874 10727 * fix up p_mapping list so that they point to hments in new.
10875 10728 *
10876 10729 * Since all these mappings are created by hblk_reserve_thread
10877 10730 * on the way and it's using at least one of the buffers from each of
10878 10731 * the newly minted slabs, there is no danger of any of these
10879 10732 * mappings getting unloaded by another thread.
10880 10733 *
10881 10734 * tsbmiss could only modify ref/mod bits of hments in old/new.
10882 10735 * Since all of these hments hold mappings established by segkmem
10883 10736 * and mappings in segkmem are setup with HAT_NOSYNC, ref/mod bits
10884 10737 * have no meaning for the mappings in hblk_reserve. hments in
10885 10738 * old and new are identical except for ref/mod bits.
10886 10739 */
10887 10740 for (vaddr = base; vaddr < endaddr; vaddr += TTEBYTES(TTE8K)) {
10888 10741
10889 10742 HBLKTOHME(osfhme, old, vaddr);
10890 10743 sfmmu_copytte(&osfhme->hme_tte, &tte);
10891 10744
10892 10745 if (TTE_IS_VALID(&tte)) {
10893 10746 if ((pp = osfhme->hme_page) == NULL)
10894 10747 panic("sfmmu_hblk_swap: page not mapped");
10895 10748
10896 10749 pml = sfmmu_mlist_enter(pp);
10897 10750
10898 10751 if (pp != osfhme->hme_page)
10899 10752 panic("sfmmu_hblk_swap: mapping changed");
10900 10753
10901 10754 HBLKTOHME(nsfhme, new, vaddr);
10902 10755
10903 10756 HME_ADD(nsfhme, pp);
10904 10757 HME_SUB(osfhme, pp);
10905 10758
10906 10759 sfmmu_mlist_exit(pml);
10907 10760 }
10908 10761 }
10909 10762
10910 10763 /*
10911 10764 * remove old from hash chain
10912 10765 */
10913 10766 sfmmu_hblk_hash_rm(hmebp, old, prev, &list, 1);
10914 10767
10915 10768 #ifdef DEBUG
10916 10769
10917 10770 hblktag.htag_id = ksfmmup;
10918 10771 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
10919 10772 hblktag.htag_bspage = HME_HASH_BSPAGE(base, HME_HASH_SHIFT(TTE8K));
10920 10773 hblktag.htag_rehash = HME_HASH_REHASH(TTE8K);
10921 10774 HME_HASH_FAST_SEARCH(hmebp, hblktag, found);
10922 10775
10923 10776 if (found != new)
10924 10777 panic("sfmmu_hblk_swap: new hblk not found");
10925 10778 #endif
10926 10779
10927 10780 SFMMU_HASH_UNLOCK(hmebp);
10928 10781
10929 10782 /*
10930 10783 * Reset hblk_reserve
10931 10784 */
10932 10785 bzero((void *)old, HME8BLK_SZ);
10933 10786 old->hblk_nextpa = va_to_pa((caddr_t)old);
10934 10787 }
10935 10788
10936 10789 /*
10937 10790 * Grab the mlist mutex for both pages passed in.
10938 10791 *
10939 10792 * low and high will be returned as pointers to the mutexes for these pages.
10940 10793 * low refers to the mutex residing in the lower bin of the mlist hash, while
10941 10794 * high refers to the mutex residing in the higher bin of the mlist hash. This
10942 10795 * is due to the locking order restrictions on the same thread grabbing
10943 10796 * multiple mlist mutexes. The low lock must be acquired before the high lock.
10944 10797 *
10945 10798 * If both pages hash to the same mutex, only grab that single mutex, and
10946 10799 * high will be returned as NULL
10947 10800 * If the pages hash to different bins in the hash, grab the lower addressed
10948 10801 * lock first and then the higher addressed lock in order to follow the locking
10949 10802 * rules involved with the same thread grabbing multiple mlist mutexes.
10950 10803 * low and high will both have non-NULL values.
10951 10804 */
10952 10805 static void
10953 10806 sfmmu_mlist_reloc_enter(struct page *targ, struct page *repl,
10954 10807 kmutex_t **low, kmutex_t **high)
10955 10808 {
10956 10809 kmutex_t *mml_targ, *mml_repl;
10957 10810
10958 10811 /*
10959 10812 * no need to do the dance around szc as in sfmmu_mlist_enter()
10960 10813 * because this routine is only called by hat_page_relocate() and all
10961 10814 * targ and repl pages are already locked EXCL so szc can't change.
10962 10815 */
10963 10816
10964 10817 mml_targ = MLIST_HASH(PP_PAGEROOT(targ));
10965 10818 mml_repl = MLIST_HASH(PP_PAGEROOT(repl));
10966 10819
10967 10820 if (mml_targ == mml_repl) {
10968 10821 *low = mml_targ;
10969 10822 *high = NULL;
10970 10823 } else {
10971 10824 if (mml_targ < mml_repl) {
10972 10825 *low = mml_targ;
10973 10826 *high = mml_repl;
10974 10827 } else {
10975 10828 *low = mml_repl;
10976 10829 *high = mml_targ;
10977 10830 }
10978 10831 }
10979 10832
10980 10833 mutex_enter(*low);
10981 10834 if (*high)
10982 10835 mutex_enter(*high);
10983 10836 }
10984 10837
10985 10838 static void
10986 10839 sfmmu_mlist_reloc_exit(kmutex_t *low, kmutex_t *high)
10987 10840 {
10988 10841 if (high)
10989 10842 mutex_exit(high);
10990 10843 mutex_exit(low);
10991 10844 }
10992 10845
10993 10846 static hatlock_t *
10994 10847 sfmmu_hat_enter(sfmmu_t *sfmmup)
10995 10848 {
10996 10849 hatlock_t *hatlockp;
10997 10850
10998 10851 if (sfmmup != ksfmmup) {
10999 10852 hatlockp = TSB_HASH(sfmmup);
11000 10853 mutex_enter(HATLOCK_MUTEXP(hatlockp));
11001 10854 return (hatlockp);
11002 10855 }
11003 10856 return (NULL);
11004 10857 }
11005 10858
11006 10859 static hatlock_t *
11007 10860 sfmmu_hat_tryenter(sfmmu_t *sfmmup)
11008 10861 {
11009 10862 hatlock_t *hatlockp;
11010 10863
11011 10864 if (sfmmup != ksfmmup) {
11012 10865 hatlockp = TSB_HASH(sfmmup);
11013 10866 if (mutex_tryenter(HATLOCK_MUTEXP(hatlockp)) == 0)
11014 10867 return (NULL);
11015 10868 return (hatlockp);
11016 10869 }
11017 10870 return (NULL);
11018 10871 }
11019 10872
11020 10873 static void
11021 10874 sfmmu_hat_exit(hatlock_t *hatlockp)
11022 10875 {
11023 10876 if (hatlockp != NULL)
11024 10877 mutex_exit(HATLOCK_MUTEXP(hatlockp));
11025 10878 }
11026 10879
11027 10880 static void
11028 10881 sfmmu_hat_lock_all(void)
11029 10882 {
11030 10883 int i;
11031 10884 for (i = 0; i < SFMMU_NUM_LOCK; i++)
11032 10885 mutex_enter(HATLOCK_MUTEXP(&hat_lock[i]));
11033 10886 }
11034 10887
11035 10888 static void
11036 10889 sfmmu_hat_unlock_all(void)
11037 10890 {
11038 10891 int i;
11039 10892 for (i = SFMMU_NUM_LOCK - 1; i >= 0; i--)
11040 10893 mutex_exit(HATLOCK_MUTEXP(&hat_lock[i]));
11041 10894 }
11042 10895
11043 10896 int
11044 10897 sfmmu_hat_lock_held(sfmmu_t *sfmmup)
11045 10898 {
11046 10899 ASSERT(sfmmup != ksfmmup);
11047 10900 return (MUTEX_HELD(HATLOCK_MUTEXP(TSB_HASH(sfmmup))));
11048 10901 }
11049 10902
11050 10903 /*
11051 10904 * Locking primitives to provide consistency between ISM unmap
11052 10905 * and other operations. Since ISM unmap can take a long time, we
11053 10906 * use HAT_ISMBUSY flag (protected by the hatlock) to avoid creating
11054 10907 * contention on the hatlock buckets while ISM segments are being
11055 10908 * unmapped. The tradeoff is that the flags don't prevent priority
11056 10909 * inversion from occurring, so we must request kernel priority in
11057 10910 * case we have to sleep to keep from getting buried while holding
11058 10911 * the HAT_ISMBUSY flag set, which in turn could block other kernel
11059 10912 * threads from running (for example, in sfmmu_uvatopfn()).
11060 10913 */
11061 10914 static void
11062 10915 sfmmu_ismhat_enter(sfmmu_t *sfmmup, int hatlock_held)
11063 10916 {
11064 10917 hatlock_t *hatlockp;
11065 10918
11066 10919 THREAD_KPRI_REQUEST();
11067 10920 if (!hatlock_held)
11068 10921 hatlockp = sfmmu_hat_enter(sfmmup);
11069 10922 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY))
11070 10923 cv_wait(&sfmmup->sfmmu_tsb_cv, HATLOCK_MUTEXP(hatlockp));
11071 10924 SFMMU_FLAGS_SET(sfmmup, HAT_ISMBUSY);
11072 10925 if (!hatlock_held)
11073 10926 sfmmu_hat_exit(hatlockp);
11074 10927 }
11075 10928
11076 10929 static void
11077 10930 sfmmu_ismhat_exit(sfmmu_t *sfmmup, int hatlock_held)
11078 10931 {
11079 10932 hatlock_t *hatlockp;
11080 10933
11081 10934 if (!hatlock_held)
11082 10935 hatlockp = sfmmu_hat_enter(sfmmup);
11083 10936 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY));
11084 10937 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ISMBUSY);
11085 10938 cv_broadcast(&sfmmup->sfmmu_tsb_cv);
11086 10939 if (!hatlock_held)
11087 10940 sfmmu_hat_exit(hatlockp);
11088 10941 THREAD_KPRI_RELEASE();
11089 10942 }
11090 10943
11091 10944 /*
11092 10945 *
11093 10946 * Algorithm:
11094 10947 *
11095 10948 * (1) if segkmem is not ready, allocate hblk from an array of pre-alloc'ed
11096 10949 * hblks.
11097 10950 *
11098 10951 * (2) if we are allocating an hblk for mapping a slab in sfmmu_cache,
11099 10952 *
11100 10953 * (a) try to return an hblk from reserve pool of free hblks;
11101 10954 * (b) if the reserve pool is empty, acquire hblk_reserve_lock
11102 10955 * and return hblk_reserve.
11103 10956 *
11104 10957 * (3) call kmem_cache_alloc() to allocate hblk;
11105 10958 *
11106 10959 * (a) if hblk_reserve_lock is held by the current thread,
11107 10960 * atomically replace hblk_reserve by the hblk that is
11108 10961 * returned by kmem_cache_alloc; release hblk_reserve_lock
11109 10962 * and call kmem_cache_alloc() again.
11110 10963 * (b) if reserve pool is not full, add the hblk that is
11111 10964 * returned by kmem_cache_alloc to reserve pool and
11112 10965 * call kmem_cache_alloc again.
11113 10966 *
11114 10967 */
11115 10968 static struct hme_blk *
11116 10969 sfmmu_hblk_alloc(sfmmu_t *sfmmup, caddr_t vaddr,
11117 10970 struct hmehash_bucket *hmebp, uint_t size, hmeblk_tag hblktag,
11118 10971 uint_t flags, uint_t rid)
11119 10972 {
11120 10973 struct hme_blk *hmeblkp = NULL;
11121 10974 struct hme_blk *newhblkp;
11122 10975 struct hme_blk *shw_hblkp = NULL;
11123 10976 struct kmem_cache *sfmmu_cache = NULL;
11124 10977 uint64_t hblkpa;
11125 10978 ulong_t index;
11126 10979 uint_t owner; /* set to 1 if using hblk_reserve */
11127 10980 uint_t forcefree;
11128 10981 int sleep;
11129 10982 sf_srd_t *srdp;
11130 10983 sf_region_t *rgnp;
11131 10984
11132 10985 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
11133 10986 ASSERT(hblktag.htag_rid == rid);
11134 10987 SFMMU_VALIDATE_HMERID(sfmmup, rid, vaddr, TTEBYTES(size));
11135 10988 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) ||
11136 10989 IS_P2ALIGNED(vaddr, TTEBYTES(size)));
11137 10990
11138 10991 /*
11139 10992 * If segkmem is not created yet, allocate from static hmeblks
11140 10993 * created at the end of startup_modules(). See the block comment
11141 10994 * in startup_modules() describing how we estimate the number of
11142 10995 * static hmeblks that will be needed during re-map.
11143 10996 */
11144 10997 if (!hblk_alloc_dynamic) {
11145 10998
11146 10999 ASSERT(!SFMMU_IS_SHMERID_VALID(rid));
11147 11000
11148 11001 if (size == TTE8K) {
11149 11002 index = nucleus_hblk8.index;
11150 11003 if (index >= nucleus_hblk8.len) {
11151 11004 /*
11152 11005 * If we panic here, see startup_modules() to
11153 11006 * make sure that we are calculating the
11154 11007 * number of hblk8's that we need correctly.
11155 11008 */
11156 11009 prom_panic("no nucleus hblk8 to allocate");
11157 11010 }
11158 11011 hmeblkp =
11159 11012 (struct hme_blk *)&nucleus_hblk8.list[index];
11160 11013 nucleus_hblk8.index++;
11161 11014 SFMMU_STAT(sf_hblk8_nalloc);
11162 11015 } else {
11163 11016 index = nucleus_hblk1.index;
11164 11017 if (nucleus_hblk1.index >= nucleus_hblk1.len) {
11165 11018 /*
11166 11019 * If we panic here, see startup_modules().
11167 11020 * Most likely you need to update the
11168 11021 * calculation of the number of hblk1 elements
11169 11022 * that the kernel needs to boot.
11170 11023 */
11171 11024 prom_panic("no nucleus hblk1 to allocate");
11172 11025 }
11173 11026 hmeblkp =
11174 11027 (struct hme_blk *)&nucleus_hblk1.list[index];
11175 11028 nucleus_hblk1.index++;
11176 11029 SFMMU_STAT(sf_hblk1_nalloc);
11177 11030 }
11178 11031
11179 11032 goto hblk_init;
11180 11033 }
11181 11034
11182 11035 SFMMU_HASH_UNLOCK(hmebp);
11183 11036
11184 11037 if (sfmmup != KHATID && !SFMMU_IS_SHMERID_VALID(rid)) {
11185 11038 if (mmu_page_sizes == max_mmu_page_sizes) {
11186 11039 if (size < TTE256M)
11187 11040 shw_hblkp = sfmmu_shadow_hcreate(sfmmup, vaddr,
11188 11041 size, flags);
11189 11042 } else {
11190 11043 if (size < TTE4M)
11191 11044 shw_hblkp = sfmmu_shadow_hcreate(sfmmup, vaddr,
11192 11045 size, flags);
11193 11046 }
11194 11047 } else if (SFMMU_IS_SHMERID_VALID(rid)) {
11195 11048 /*
11196 11049 * Shared hmes use per region bitmaps in rgn_hmeflag
11197 11050 * rather than shadow hmeblks to keep track of the
11198 11051 * mapping sizes which have been allocated for the region.
11199 11052 * Here we cleanup old invalid hmeblks with this rid,
11200 11053 * which may be left around by pageunload().
11201 11054 */
11202 11055 int ttesz;
11203 11056 caddr_t va;
11204 11057 caddr_t eva = vaddr + TTEBYTES(size);
11205 11058
11206 11059 ASSERT(sfmmup != KHATID);
11207 11060
11208 11061 srdp = sfmmup->sfmmu_srdp;
11209 11062 ASSERT(srdp != NULL && srdp->srd_refcnt != 0);
11210 11063 rgnp = srdp->srd_hmergnp[rid];
11211 11064 ASSERT(rgnp != NULL && rgnp->rgn_id == rid);
11212 11065 ASSERT(rgnp->rgn_refcnt != 0);
11213 11066 ASSERT(size <= rgnp->rgn_pgszc);
11214 11067
11215 11068 ttesz = HBLK_MIN_TTESZ;
11216 11069 do {
11217 11070 if (!(rgnp->rgn_hmeflags & (0x1 << ttesz))) {
11218 11071 continue;
11219 11072 }
11220 11073
11221 11074 if (ttesz > size && ttesz != HBLK_MIN_TTESZ) {
11222 11075 sfmmu_cleanup_rhblk(srdp, vaddr, rid, ttesz);
11223 11076 } else if (ttesz < size) {
11224 11077 for (va = vaddr; va < eva;
11225 11078 va += TTEBYTES(ttesz)) {
11226 11079 sfmmu_cleanup_rhblk(srdp, va, rid,
11227 11080 ttesz);
11228 11081 }
11229 11082 }
11230 11083 } while (++ttesz <= rgnp->rgn_pgszc);
11231 11084 }
11232 11085
11233 11086 fill_hblk:
11234 11087 owner = (hblk_reserve_thread == curthread) ? 1 : 0;
11235 11088
11236 11089 if (owner && size == TTE8K) {
11237 11090
11238 11091 ASSERT(!SFMMU_IS_SHMERID_VALID(rid));
11239 11092 /*
11240 11093 * We are really in a tight spot. We already own
11241 11094 * hblk_reserve and we need another hblk. In anticipation
11242 11095 * of this kind of scenario, we specifically set aside
11243 11096 * HBLK_RESERVE_MIN number of hblks to be used exclusively
11244 11097 * by owner of hblk_reserve.
11245 11098 */
11246 11099 SFMMU_STAT(sf_hblk_recurse_cnt);
11247 11100
11248 11101 if (!sfmmu_get_free_hblk(&hmeblkp, 1))
11249 11102 panic("sfmmu_hblk_alloc: reserve list is empty");
11250 11103
11251 11104 goto hblk_verify;
11252 11105 }
11253 11106
11254 11107 ASSERT(!owner);
11255 11108
11256 11109 if ((flags & HAT_NO_KALLOC) == 0) {
11257 11110
11258 11111 sfmmu_cache = ((size == TTE8K) ? sfmmu8_cache : sfmmu1_cache);
11259 11112 sleep = ((sfmmup == KHATID) ? KM_NOSLEEP : KM_SLEEP);
11260 11113
11261 11114 if ((hmeblkp = kmem_cache_alloc(sfmmu_cache, sleep)) == NULL) {
11262 11115 hmeblkp = sfmmu_hblk_steal(size);
11263 11116 } else {
11264 11117 /*
11265 11118 * if we are the owner of hblk_reserve,
11266 11119 * swap hblk_reserve with hmeblkp and
11267 11120 * start a fresh life. Hope things go
11268 11121 * better this time.
11269 11122 */
11270 11123 if (hblk_reserve_thread == curthread) {
11271 11124 ASSERT(sfmmu_cache == sfmmu8_cache);
11272 11125 sfmmu_hblk_swap(hmeblkp);
11273 11126 hblk_reserve_thread = NULL;
11274 11127 mutex_exit(&hblk_reserve_lock);
11275 11128 goto fill_hblk;
11276 11129 }
11277 11130 /*
11278 11131 * let's donate this hblk to our reserve list if
11279 11132 * we are not mapping kernel range
11280 11133 */
11281 11134 if (size == TTE8K && sfmmup != KHATID) {
11282 11135 if (sfmmu_put_free_hblk(hmeblkp, 0))
11283 11136 goto fill_hblk;
11284 11137 }
11285 11138 }
11286 11139 } else {
11287 11140 /*
11288 11141 * We are here to map the slab in sfmmu8_cache; let's
11289 11142 * check if we could tap our reserve list; if successful,
11290 11143 * this will avoid the pain of going thru sfmmu_hblk_swap
11291 11144 */
11292 11145 SFMMU_STAT(sf_hblk_slab_cnt);
11293 11146 if (!sfmmu_get_free_hblk(&hmeblkp, 0)) {
11294 11147 /*
11295 11148 * let's start hblk_reserve dance
11296 11149 */
11297 11150 SFMMU_STAT(sf_hblk_reserve_cnt);
11298 11151 owner = 1;
11299 11152 mutex_enter(&hblk_reserve_lock);
11300 11153 hmeblkp = HBLK_RESERVE;
11301 11154 hblk_reserve_thread = curthread;
11302 11155 }
11303 11156 }
11304 11157
11305 11158 hblk_verify:
11306 11159 ASSERT(hmeblkp != NULL);
11307 11160 set_hblk_sz(hmeblkp, size);
11308 11161 ASSERT(hmeblkp->hblk_nextpa == va_to_pa((caddr_t)hmeblkp));
11309 11162 SFMMU_HASH_LOCK(hmebp);
11310 11163 HME_HASH_FAST_SEARCH(hmebp, hblktag, newhblkp);
11311 11164 if (newhblkp != NULL) {
11312 11165 SFMMU_HASH_UNLOCK(hmebp);
11313 11166 if (hmeblkp != HBLK_RESERVE) {
11314 11167 /*
11315 11168 * This is really tricky!
11316 11169 *
11317 11170 * vmem_alloc(vmem_seg_arena)
11318 11171 * vmem_alloc(vmem_internal_arena)
11319 11172 * segkmem_alloc(heap_arena)
11320 11173 * vmem_alloc(heap_arena)
11321 11174 * page_create()
11322 11175 * hat_memload()
11323 11176 * kmem_cache_free()
11324 11177 * kmem_cache_alloc()
11325 11178 * kmem_slab_create()
11326 11179 * vmem_alloc(kmem_internal_arena)
11327 11180 * segkmem_alloc(heap_arena)
11328 11181 * vmem_alloc(heap_arena)
11329 11182 * page_create()
11330 11183 * hat_memload()
11331 11184 * kmem_cache_free()
11332 11185 * ...
11333 11186 *
11334 11187 * Thus, hat_memload() could call kmem_cache_free
11335 11188 * for enough number of times that we could easily
11336 11189 * hit the bottom of the stack or run out of reserve
11337 11190 * list of vmem_seg structs. So, we must donate
11338 11191 * this hblk to reserve list if it's allocated
11339 11192 * from sfmmu8_cache *and* mapping kernel range.
11340 11193 * We don't need to worry about freeing hmeblk1's
11341 11194 * to kmem since they don't map any kmem slabs.
11342 11195 *
11343 11196 * Note: When segkmem supports largepages, we must
11344 11197 * free hmeblk1's to reserve list as well.
11345 11198 */
11346 11199 forcefree = (sfmmup == KHATID) ? 1 : 0;
11347 11200 if (size == TTE8K &&
11348 11201 sfmmu_put_free_hblk(hmeblkp, forcefree)) {
11349 11202 goto re_verify;
11350 11203 }
11351 11204 ASSERT(sfmmup != KHATID);
11352 11205 kmem_cache_free(get_hblk_cache(hmeblkp), hmeblkp);
11353 11206 } else {
11354 11207 /*
11355 11208 * Hey! we don't need hblk_reserve any more.
11356 11209 */
11357 11210 ASSERT(owner);
11358 11211 hblk_reserve_thread = NULL;
11359 11212 mutex_exit(&hblk_reserve_lock);
11360 11213 owner = 0;
11361 11214 }
11362 11215 re_verify:
11363 11216 /*
11364 11217 * let's check if the goodies are still present
11365 11218 */
11366 11219 SFMMU_HASH_LOCK(hmebp);
11367 11220 HME_HASH_FAST_SEARCH(hmebp, hblktag, newhblkp);
11368 11221 if (newhblkp != NULL) {
11369 11222 /*
11370 11223 * return newhblkp if it's not hblk_reserve;
11371 11224 * if newhblkp is hblk_reserve, return it
11372 11225 * _only if_ we are the owner of hblk_reserve.
11373 11226 */
11374 11227 if (newhblkp != HBLK_RESERVE || owner) {
11375 11228 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) ||
11376 11229 newhblkp->hblk_shared);
11377 11230 ASSERT(SFMMU_IS_SHMERID_VALID(rid) ||
11378 11231 !newhblkp->hblk_shared);
11379 11232 return (newhblkp);
11380 11233 } else {
11381 11234 /*
11382 11235 * we just hit hblk_reserve in the hash and
11383 11236 * we are not the owner of that;
11384 11237 *
11385 11238 * block until hblk_reserve_thread completes
11386 11239 * swapping hblk_reserve and try the dance
11387 11240 * once again.
11388 11241 */
11389 11242 SFMMU_HASH_UNLOCK(hmebp);
11390 11243 mutex_enter(&hblk_reserve_lock);
11391 11244 mutex_exit(&hblk_reserve_lock);
11392 11245 SFMMU_STAT(sf_hblk_reserve_hit);
11393 11246 goto fill_hblk;
11394 11247 }
11395 11248 } else {
11396 11249 /*
11397 11250 * it's no more! try the dance once again.
11398 11251 */
11399 11252 SFMMU_HASH_UNLOCK(hmebp);
11400 11253 goto fill_hblk;
11401 11254 }
11402 11255 }
11403 11256
11404 11257 hblk_init:
11405 11258 if (SFMMU_IS_SHMERID_VALID(rid)) {
11406 11259 uint16_t tteflag = 0x1 <<
11407 11260 ((size < HBLK_MIN_TTESZ) ? HBLK_MIN_TTESZ : size);
11408 11261
11409 11262 if (!(rgnp->rgn_hmeflags & tteflag)) {
11410 11263 atomic_or_16(&rgnp->rgn_hmeflags, tteflag);
11411 11264 }
11412 11265 hmeblkp->hblk_shared = 1;
11413 11266 } else {
11414 11267 hmeblkp->hblk_shared = 0;
11415 11268 }
11416 11269 set_hblk_sz(hmeblkp, size);
11417 11270 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
11418 11271 hmeblkp->hblk_next = (struct hme_blk *)NULL;
11419 11272 hmeblkp->hblk_tag = hblktag;
11420 11273 hmeblkp->hblk_shadow = shw_hblkp;
11421 11274 hblkpa = hmeblkp->hblk_nextpa;
11422 11275 hmeblkp->hblk_nextpa = HMEBLK_ENDPA;
11423 11276
11424 11277 ASSERT(get_hblk_ttesz(hmeblkp) == size);
11425 11278 ASSERT(get_hblk_span(hmeblkp) == HMEBLK_SPAN(size));
11426 11279 ASSERT(hmeblkp->hblk_hmecnt == 0);
11427 11280 ASSERT(hmeblkp->hblk_vcnt == 0);
11428 11281 ASSERT(hmeblkp->hblk_lckcnt == 0);
11429 11282 ASSERT(hblkpa == va_to_pa((caddr_t)hmeblkp));
11430 11283 sfmmu_hblk_hash_add(hmebp, hmeblkp, hblkpa);
11431 11284 return (hmeblkp);
11432 11285 }
11433 11286
11434 11287 /*
11435 11288 * This function cleans up the hme_blk and returns it to the free list.
11436 11289 */
11437 11290 /* ARGSUSED */
11438 11291 static void
11439 11292 sfmmu_hblk_free(struct hme_blk **listp)
11440 11293 {
11441 11294 struct hme_blk *hmeblkp, *next_hmeblkp;
11442 11295 int size;
11443 11296 uint_t critical;
11444 11297 uint64_t hblkpa;
11445 11298
11446 11299 ASSERT(*listp != NULL);
11447 11300
11448 11301 hmeblkp = *listp;
11449 11302 while (hmeblkp != NULL) {
11450 11303 next_hmeblkp = hmeblkp->hblk_next;
11451 11304 ASSERT(!hmeblkp->hblk_hmecnt);
11452 11305 ASSERT(!hmeblkp->hblk_vcnt);
11453 11306 ASSERT(!hmeblkp->hblk_lckcnt);
11454 11307 ASSERT(hmeblkp != (struct hme_blk *)hblk_reserve);
11455 11308 ASSERT(hmeblkp->hblk_shared == 0);
11456 11309 ASSERT(hmeblkp->hblk_shw_bit == 0);
11457 11310 ASSERT(hmeblkp->hblk_shadow == NULL);
11458 11311
11459 11312 hblkpa = va_to_pa((caddr_t)hmeblkp);
11460 11313 ASSERT(hblkpa != (uint64_t)-1);
11461 11314 critical = (hblktosfmmu(hmeblkp) == KHATID) ? 1 : 0;
11462 11315
11463 11316 size = get_hblk_ttesz(hmeblkp);
11464 11317 hmeblkp->hblk_next = NULL;
11465 11318 hmeblkp->hblk_nextpa = hblkpa;
11466 11319
11467 11320 if (hmeblkp->hblk_nuc_bit == 0) {
11468 11321
11469 11322 if (size != TTE8K ||
11470 11323 !sfmmu_put_free_hblk(hmeblkp, critical))
11471 11324 kmem_cache_free(get_hblk_cache(hmeblkp),
11472 11325 hmeblkp);
11473 11326 }
11474 11327 hmeblkp = next_hmeblkp;
11475 11328 }
11476 11329 }
11477 11330
11478 11331 #define BUCKETS_TO_SEARCH_BEFORE_UNLOAD 30
11479 11332 #define SFMMU_HBLK_STEAL_THRESHOLD 5
11480 11333
11481 11334 static uint_t sfmmu_hblk_steal_twice;
11482 11335 static uint_t sfmmu_hblk_steal_count, sfmmu_hblk_steal_unload_count;
11483 11336
11484 11337 /*
11485 11338 * Steal a hmeblk from user or kernel hme hash lists.
11486 11339 * For 8K tte grab one from reserve pool (freehblkp) before proceeding to
11487 11340 * steal and if we fail to steal after SFMMU_HBLK_STEAL_THRESHOLD attempts
11488 11341 * tap into critical reserve of freehblkp.
11489 11342 * Note: We remain looping in this routine until we find one.
11490 11343 */
11491 11344 static struct hme_blk *
11492 11345 sfmmu_hblk_steal(int size)
11493 11346 {
11494 11347 static struct hmehash_bucket *uhmehash_steal_hand = NULL;
11495 11348 struct hmehash_bucket *hmebp;
11496 11349 struct hme_blk *hmeblkp = NULL, *pr_hblk;
11497 11350 uint64_t hblkpa;
11498 11351 int i;
11499 11352 uint_t loop_cnt = 0, critical;
11500 11353
11501 11354 for (;;) {
11502 11355 /* Check cpu hblk pending queues */
11503 11356 if ((hmeblkp = sfmmu_check_pending_hblks(size)) != NULL) {
11504 11357 hmeblkp->hblk_nextpa = va_to_pa((caddr_t)hmeblkp);
11505 11358 ASSERT(hmeblkp->hblk_hmecnt == 0);
11506 11359 ASSERT(hmeblkp->hblk_vcnt == 0);
11507 11360 return (hmeblkp);
11508 11361 }
11509 11362
11510 11363 if (size == TTE8K) {
11511 11364 critical =
11512 11365 (++loop_cnt > SFMMU_HBLK_STEAL_THRESHOLD) ? 1 : 0;
11513 11366 if (sfmmu_get_free_hblk(&hmeblkp, critical))
11514 11367 return (hmeblkp);
11515 11368 }
11516 11369
11517 11370 hmebp = (uhmehash_steal_hand == NULL) ? uhme_hash :
11518 11371 uhmehash_steal_hand;
11519 11372 ASSERT(hmebp >= uhme_hash && hmebp <= &uhme_hash[UHMEHASH_SZ]);
11520 11373
11521 11374 for (i = 0; hmeblkp == NULL && i <= UHMEHASH_SZ +
11522 11375 BUCKETS_TO_SEARCH_BEFORE_UNLOAD; i++) {
11523 11376 SFMMU_HASH_LOCK(hmebp);
11524 11377 hmeblkp = hmebp->hmeblkp;
11525 11378 hblkpa = hmebp->hmeh_nextpa;
11526 11379 pr_hblk = NULL;
11527 11380 while (hmeblkp) {
11528 11381 /*
11529 11382 * check if it is a hmeblk that is not locked
11530 11383 * and not shared. skip shadow hmeblks with
11531 11384 * shadow_mask set i.e valid count non zero.
11532 11385 */
11533 11386 if ((get_hblk_ttesz(hmeblkp) == size) &&
11534 11387 (hmeblkp->hblk_shw_bit == 0 ||
11535 11388 hmeblkp->hblk_vcnt == 0) &&
11536 11389 (hmeblkp->hblk_lckcnt == 0)) {
11537 11390 /*
11538 11391 * there is a high probability that we
11539 11392 * will find a free one. search some
11540 11393 * buckets for a free hmeblk initially
11541 11394 * before unloading a valid hmeblk.
11542 11395 */
11543 11396 if ((hmeblkp->hblk_vcnt == 0 &&
11544 11397 hmeblkp->hblk_hmecnt == 0) || (i >=
11545 11398 BUCKETS_TO_SEARCH_BEFORE_UNLOAD)) {
11546 11399 if (sfmmu_steal_this_hblk(hmebp,
11547 11400 hmeblkp, hblkpa, pr_hblk)) {
11548 11401 /*
11549 11402 * Hblk is unloaded
11550 11403 * successfully
11551 11404 */
11552 11405 break;
11553 11406 }
11554 11407 }
11555 11408 }
11556 11409 pr_hblk = hmeblkp;
11557 11410 hblkpa = hmeblkp->hblk_nextpa;
11558 11411 hmeblkp = hmeblkp->hblk_next;
11559 11412 }
11560 11413
11561 11414 SFMMU_HASH_UNLOCK(hmebp);
11562 11415 if (hmebp++ == &uhme_hash[UHMEHASH_SZ])
11563 11416 hmebp = uhme_hash;
11564 11417 }
11565 11418 uhmehash_steal_hand = hmebp;
11566 11419
11567 11420 if (hmeblkp != NULL)
11568 11421 break;
11569 11422
11570 11423 /*
11571 11424 * in the worst case, look for a free one in the kernel
11572 11425 * hash table.
11573 11426 */
11574 11427 for (i = 0, hmebp = khme_hash; i <= KHMEHASH_SZ; i++) {
11575 11428 SFMMU_HASH_LOCK(hmebp);
11576 11429 hmeblkp = hmebp->hmeblkp;
11577 11430 hblkpa = hmebp->hmeh_nextpa;
11578 11431 pr_hblk = NULL;
11579 11432 while (hmeblkp) {
11580 11433 /*
11581 11434 * check if it is free hmeblk
11582 11435 */
11583 11436 if ((get_hblk_ttesz(hmeblkp) == size) &&
11584 11437 (hmeblkp->hblk_lckcnt == 0) &&
11585 11438 (hmeblkp->hblk_vcnt == 0) &&
11586 11439 (hmeblkp->hblk_hmecnt == 0)) {
11587 11440 if (sfmmu_steal_this_hblk(hmebp,
11588 11441 hmeblkp, hblkpa, pr_hblk)) {
11589 11442 break;
11590 11443 } else {
11591 11444 /*
11592 11445 * Cannot fail since we have
11593 11446 * hash lock.
11594 11447 */
11595 11448 panic("fail to steal?");
11596 11449 }
11597 11450 }
11598 11451
11599 11452 pr_hblk = hmeblkp;
11600 11453 hblkpa = hmeblkp->hblk_nextpa;
11601 11454 hmeblkp = hmeblkp->hblk_next;
11602 11455 }
11603 11456
11604 11457 SFMMU_HASH_UNLOCK(hmebp);
11605 11458 if (hmebp++ == &khme_hash[KHMEHASH_SZ])
11606 11459 hmebp = khme_hash;
11607 11460 }
11608 11461
11609 11462 if (hmeblkp != NULL)
11610 11463 break;
11611 11464 sfmmu_hblk_steal_twice++;
11612 11465 }
11613 11466 return (hmeblkp);
11614 11467 }
11615 11468
11616 11469 /*
11617 11470 * This routine does real work to prepare a hblk to be "stolen" by
11618 11471 * unloading the mappings, updating shadow counts ....
11619 11472 * It returns 1 if the block is ready to be reused (stolen), or 0
11620 11473 * means the block cannot be stolen yet- pageunload is still working
11621 11474 * on this hblk.
11622 11475 */
11623 11476 static int
11624 11477 sfmmu_steal_this_hblk(struct hmehash_bucket *hmebp, struct hme_blk *hmeblkp,
11625 11478 uint64_t hblkpa, struct hme_blk *pr_hblk)
11626 11479 {
11627 11480 int shw_size, vshift;
11628 11481 struct hme_blk *shw_hblkp;
11629 11482 caddr_t vaddr;
11630 11483 uint_t shw_mask, newshw_mask;
11631 11484 struct hme_blk *list = NULL;
11632 11485
11633 11486 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
11634 11487
11635 11488 /*
11636 11489 * check if the hmeblk is free, unload if necessary
11637 11490 */
11638 11491 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) {
11639 11492 sfmmu_t *sfmmup;
11640 11493 demap_range_t dmr;
11641 11494
11642 11495 sfmmup = hblktosfmmu(hmeblkp);
11643 11496 if (hmeblkp->hblk_shared || sfmmup->sfmmu_ismhat) {
11644 11497 return (0);
11645 11498 }
11646 11499 DEMAP_RANGE_INIT(sfmmup, &dmr);
11647 11500 (void) sfmmu_hblk_unload(sfmmup, hmeblkp,
11648 11501 (caddr_t)get_hblk_base(hmeblkp),
11649 11502 get_hblk_endaddr(hmeblkp), &dmr, HAT_UNLOAD);
11650 11503 DEMAP_RANGE_FLUSH(&dmr);
11651 11504 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) {
11652 11505 /*
11653 11506 * Pageunload is working on the same hblk.
11654 11507 */
11655 11508 return (0);
11656 11509 }
11657 11510
11658 11511 sfmmu_hblk_steal_unload_count++;
11659 11512 }
11660 11513
11661 11514 ASSERT(hmeblkp->hblk_lckcnt == 0);
11662 11515 ASSERT(hmeblkp->hblk_vcnt == 0 && hmeblkp->hblk_hmecnt == 0);
11663 11516
11664 11517 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk, &list, 1);
11665 11518 hmeblkp->hblk_nextpa = hblkpa;
11666 11519
11667 11520 shw_hblkp = hmeblkp->hblk_shadow;
11668 11521 if (shw_hblkp) {
11669 11522 ASSERT(!hmeblkp->hblk_shared);
11670 11523 shw_size = get_hblk_ttesz(shw_hblkp);
11671 11524 vaddr = (caddr_t)get_hblk_base(hmeblkp);
11672 11525 vshift = vaddr_to_vshift(shw_hblkp->hblk_tag, vaddr, shw_size);
11673 11526 ASSERT(vshift < 8);
11674 11527 /*
11675 11528 * Atomically clear shadow mask bit
11676 11529 */
11677 11530 do {
11678 11531 shw_mask = shw_hblkp->hblk_shw_mask;
11679 11532 ASSERT(shw_mask & (1 << vshift));
11680 11533 newshw_mask = shw_mask & ~(1 << vshift);
11681 11534 newshw_mask = cas32(&shw_hblkp->hblk_shw_mask,
11682 11535 shw_mask, newshw_mask);
11683 11536 } while (newshw_mask != shw_mask);
11684 11537 hmeblkp->hblk_shadow = NULL;
11685 11538 }
11686 11539
11687 11540 /*
11688 11541 * remove shadow bit if we are stealing an unused shadow hmeblk.
11689 11542 * sfmmu_hblk_alloc needs it that way, will set shadow bit later if
11690 11543 * we are indeed allocating a shadow hmeblk.
11691 11544 */
11692 11545 hmeblkp->hblk_shw_bit = 0;
11693 11546
11694 11547 if (hmeblkp->hblk_shared) {
11695 11548 sf_srd_t *srdp;
11696 11549 sf_region_t *rgnp;
11697 11550 uint_t rid;
11698 11551
11699 11552 srdp = hblktosrd(hmeblkp);
11700 11553 ASSERT(srdp != NULL && srdp->srd_refcnt != 0);
11701 11554 rid = hmeblkp->hblk_tag.htag_rid;
11702 11555 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
11703 11556 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
11704 11557 rgnp = srdp->srd_hmergnp[rid];
11705 11558 ASSERT(rgnp != NULL);
11706 11559 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid);
11707 11560 hmeblkp->hblk_shared = 0;
11708 11561 }
11709 11562
11710 11563 sfmmu_hblk_steal_count++;
11711 11564 SFMMU_STAT(sf_steal_count);
11712 11565
11713 11566 return (1);
11714 11567 }
11715 11568
11716 11569 struct hme_blk *
11717 11570 sfmmu_hmetohblk(struct sf_hment *sfhme)
11718 11571 {
11719 11572 struct hme_blk *hmeblkp;
11720 11573 struct sf_hment *sfhme0;
11721 11574 struct hme_blk *hblk_dummy = 0;
11722 11575
11723 11576 /*
11724 11577 * No dummy sf_hments, please.
11725 11578 */
11726 11579 ASSERT(sfhme->hme_tte.ll != 0);
11727 11580
11728 11581 sfhme0 = sfhme - sfhme->hme_tte.tte_hmenum;
11729 11582 hmeblkp = (struct hme_blk *)((uintptr_t)sfhme0 -
11730 11583 (uintptr_t)&hblk_dummy->hblk_hme[0]);
11731 11584
11732 11585 return (hmeblkp);
11733 11586 }
11734 11587
11735 11588 /*
11736 11589 * On swapin, get appropriately sized TSB(s) and clear the HAT_SWAPPED flag.
11737 11590 * If we can't get appropriately sized TSB(s), try for 8K TSB(s) using
11738 11591 * KM_SLEEP allocation.
11739 11592 *
11740 11593 * Return 0 on success, -1 otherwise.
11741 11594 */
11742 11595 static void
11743 11596 sfmmu_tsb_swapin(sfmmu_t *sfmmup, hatlock_t *hatlockp)
11744 11597 {
11745 11598 struct tsb_info *tsbinfop, *next;
11746 11599 tsb_replace_rc_t rc;
11747 11600 boolean_t gotfirst = B_FALSE;
11748 11601
11749 11602 ASSERT(sfmmup != ksfmmup);
11750 11603 ASSERT(sfmmu_hat_lock_held(sfmmup));
11751 11604
11752 11605 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPIN)) {
11753 11606 cv_wait(&sfmmup->sfmmu_tsb_cv, HATLOCK_MUTEXP(hatlockp));
11754 11607 }
11755 11608
11756 11609 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) {
11757 11610 SFMMU_FLAGS_SET(sfmmup, HAT_SWAPIN);
11758 11611 } else {
11759 11612 return;
11760 11613 }
11761 11614
11762 11615 ASSERT(sfmmup->sfmmu_tsb != NULL);
11763 11616
11764 11617 /*
11765 11618 * Loop over all tsbinfo's replacing them with ones that actually have
11766 11619 * a TSB. If any of the replacements ever fail, bail out of the loop.
11767 11620 */
11768 11621 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL; tsbinfop = next) {
11769 11622 ASSERT(tsbinfop->tsb_flags & TSB_SWAPPED);
11770 11623 next = tsbinfop->tsb_next;
11771 11624 rc = sfmmu_replace_tsb(sfmmup, tsbinfop, tsbinfop->tsb_szc,
11772 11625 hatlockp, TSB_SWAPIN);
11773 11626 if (rc != TSB_SUCCESS) {
11774 11627 break;
11775 11628 }
11776 11629 gotfirst = B_TRUE;
11777 11630 }
11778 11631
11779 11632 switch (rc) {
11780 11633 case TSB_SUCCESS:
11781 11634 SFMMU_FLAGS_CLEAR(sfmmup, HAT_SWAPPED|HAT_SWAPIN);
11782 11635 cv_broadcast(&sfmmup->sfmmu_tsb_cv);
11783 11636 return;
11784 11637 case TSB_LOSTRACE:
11785 11638 break;
11786 11639 case TSB_ALLOCFAIL:
11787 11640 break;
11788 11641 default:
11789 11642 panic("sfmmu_replace_tsb returned unrecognized failure code "
11790 11643 "%d", rc);
11791 11644 }
11792 11645
11793 11646 /*
11794 11647 * In this case, we failed to get one of our TSBs. If we failed to
11795 11648 * get the first TSB, get one of minimum size (8KB). Walk the list
11796 11649 * and throw away the tsbinfos, starting where the allocation failed;
11797 11650 * we can get by with just one TSB as long as we don't leave the
11798 11651 * SWAPPED tsbinfo structures lying around.
11799 11652 */
11800 11653 tsbinfop = sfmmup->sfmmu_tsb;
11801 11654 next = tsbinfop->tsb_next;
11802 11655 tsbinfop->tsb_next = NULL;
11803 11656
11804 11657 sfmmu_hat_exit(hatlockp);
11805 11658 for (tsbinfop = next; tsbinfop != NULL; tsbinfop = next) {
11806 11659 next = tsbinfop->tsb_next;
11807 11660 sfmmu_tsbinfo_free(tsbinfop);
11808 11661 }
11809 11662 hatlockp = sfmmu_hat_enter(sfmmup);
11810 11663
11811 11664 /*
11812 11665 * If we don't have any TSBs, get a single 8K TSB for 8K, 64K and 512K
11813 11666 * pages.
11814 11667 */
11815 11668 if (!gotfirst) {
11816 11669 tsbinfop = sfmmup->sfmmu_tsb;
11817 11670 rc = sfmmu_replace_tsb(sfmmup, tsbinfop, TSB_MIN_SZCODE,
11818 11671 hatlockp, TSB_SWAPIN | TSB_FORCEALLOC);
11819 11672 ASSERT(rc == TSB_SUCCESS);
11820 11673 }
11821 11674
11822 11675 SFMMU_FLAGS_CLEAR(sfmmup, HAT_SWAPPED|HAT_SWAPIN);
11823 11676 cv_broadcast(&sfmmup->sfmmu_tsb_cv);
11824 11677 }
11825 11678
11826 11679 static int
11827 11680 sfmmu_is_rgnva(sf_srd_t *srdp, caddr_t addr, ulong_t w, ulong_t bmw)
11828 11681 {
11829 11682 ulong_t bix = 0;
11830 11683 uint_t rid;
11831 11684 sf_region_t *rgnp;
11832 11685
11833 11686 ASSERT(srdp != NULL);
11834 11687 ASSERT(srdp->srd_refcnt != 0);
11835 11688
11836 11689 w <<= BT_ULSHIFT;
11837 11690 while (bmw) {
11838 11691 if (!(bmw & 0x1)) {
11839 11692 bix++;
11840 11693 bmw >>= 1;
11841 11694 continue;
11842 11695 }
11843 11696 rid = w | bix;
11844 11697 rgnp = srdp->srd_hmergnp[rid];
11845 11698 ASSERT(rgnp->rgn_refcnt > 0);
11846 11699 ASSERT(rgnp->rgn_id == rid);
11847 11700 if (addr < rgnp->rgn_saddr ||
11848 11701 addr >= (rgnp->rgn_saddr + rgnp->rgn_size)) {
11849 11702 bix++;
11850 11703 bmw >>= 1;
11851 11704 } else {
11852 11705 return (1);
11853 11706 }
11854 11707 }
11855 11708 return (0);
11856 11709 }
11857 11710
11858 11711 /*
11859 11712 * Handle exceptions for low level tsb_handler.
11860 11713 *
11861 11714 * There are many scenarios that could land us here:
11862 11715 *
11863 11716 * If the context is invalid we land here. The context can be invalid
11864 11717 * for 3 reasons: 1) we couldn't allocate a new context and now need to
11865 11718 * perform a wrap around operation in order to allocate a new context.
11866 11719 * 2) Context was invalidated to change pagesize programming 3) ISMs or
11867 11720 * TSBs configuration is changeing for this process and we are forced into
11868 11721 * here to do a syncronization operation. If the context is valid we can
11869 11722 * be here from window trap hanlder. In this case just call trap to handle
11870 11723 * the fault.
11871 11724 *
11872 11725 * Note that the process will run in INVALID_CONTEXT before
11873 11726 * faulting into here and subsequently loading the MMU registers
11874 11727 * (including the TSB base register) associated with this process.
11875 11728 * For this reason, the trap handlers must all test for
11876 11729 * INVALID_CONTEXT before attempting to access any registers other
11877 11730 * than the context registers.
11878 11731 */
11879 11732 void
11880 11733 sfmmu_tsbmiss_exception(struct regs *rp, uintptr_t tagaccess, uint_t traptype)
11881 11734 {
11882 11735 sfmmu_t *sfmmup, *shsfmmup;
11883 11736 uint_t ctxtype;
11884 11737 klwp_id_t lwp;
11885 11738 char lwp_save_state;
11886 11739 hatlock_t *hatlockp, *shatlockp;
11887 11740 struct tsb_info *tsbinfop;
11888 11741 struct tsbmiss *tsbmp;
11889 11742 sf_scd_t *scdp;
11890 11743
11891 11744 SFMMU_STAT(sf_tsb_exceptions);
11892 11745 SFMMU_MMU_STAT(mmu_tsb_exceptions);
11893 11746 sfmmup = astosfmmu(curthread->t_procp->p_as);
11894 11747 /*
11895 11748 * note that in sun4u, tagacces register contains ctxnum
11896 11749 * while sun4v passes ctxtype in the tagaccess register.
11897 11750 */
11898 11751 ctxtype = tagaccess & TAGACC_CTX_MASK;
11899 11752
11900 11753 ASSERT(sfmmup != ksfmmup && ctxtype != KCONTEXT);
11901 11754 ASSERT(sfmmup->sfmmu_ismhat == 0);
11902 11755 ASSERT(!SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED) ||
11903 11756 ctxtype == INVALID_CONTEXT);
11904 11757
11905 11758 if (ctxtype != INVALID_CONTEXT && traptype != T_DATA_PROT) {
11906 11759 /*
11907 11760 * We may land here because shme bitmap and pagesize
11908 11761 * flags are updated lazily in tsbmiss area on other cpus.
11909 11762 * If we detect here that tsbmiss area is out of sync with
11910 11763 * sfmmu update it and retry the trapped instruction.
11911 11764 * Otherwise call trap().
11912 11765 */
11913 11766 int ret = 0;
11914 11767 uchar_t tteflag_mask = (1 << TTE64K) | (1 << TTE8K);
11915 11768 caddr_t addr = (caddr_t)(tagaccess & TAGACC_VADDR_MASK);
11916 11769
11917 11770 /*
11918 11771 * Must set lwp state to LWP_SYS before
11919 11772 * trying to acquire any adaptive lock
11920 11773 */
11921 11774 lwp = ttolwp(curthread);
11922 11775 ASSERT(lwp);
11923 11776 lwp_save_state = lwp->lwp_state;
11924 11777 lwp->lwp_state = LWP_SYS;
11925 11778
11926 11779 hatlockp = sfmmu_hat_enter(sfmmup);
11927 11780 kpreempt_disable();
11928 11781 tsbmp = &tsbmiss_area[CPU->cpu_id];
11929 11782 ASSERT(sfmmup == tsbmp->usfmmup);
11930 11783 if (((tsbmp->uhat_tteflags ^ sfmmup->sfmmu_tteflags) &
11931 11784 ~tteflag_mask) ||
11932 11785 ((tsbmp->uhat_rtteflags ^ sfmmup->sfmmu_rtteflags) &
11933 11786 ~tteflag_mask)) {
11934 11787 tsbmp->uhat_tteflags = sfmmup->sfmmu_tteflags;
11935 11788 tsbmp->uhat_rtteflags = sfmmup->sfmmu_rtteflags;
11936 11789 ret = 1;
11937 11790 }
11938 11791 if (sfmmup->sfmmu_srdp != NULL) {
11939 11792 ulong_t *sm = sfmmup->sfmmu_hmeregion_map.bitmap;
11940 11793 ulong_t *tm = tsbmp->shmermap;
11941 11794 ulong_t i;
11942 11795 for (i = 0; i < SFMMU_HMERGNMAP_WORDS; i++) {
11943 11796 ulong_t d = tm[i] ^ sm[i];
11944 11797 if (d) {
11945 11798 if (d & sm[i]) {
11946 11799 if (!ret && sfmmu_is_rgnva(
11947 11800 sfmmup->sfmmu_srdp,
11948 11801 addr, i, d & sm[i])) {
11949 11802 ret = 1;
11950 11803 }
11951 11804 }
11952 11805 tm[i] = sm[i];
11953 11806 }
11954 11807 }
11955 11808 }
11956 11809 kpreempt_enable();
11957 11810 sfmmu_hat_exit(hatlockp);
11958 11811 lwp->lwp_state = lwp_save_state;
11959 11812 if (ret) {
11960 11813 return;
11961 11814 }
11962 11815 } else if (ctxtype == INVALID_CONTEXT) {
11963 11816 /*
11964 11817 * First, make sure we come out of here with a valid ctx,
11965 11818 * since if we don't get one we'll simply loop on the
11966 11819 * faulting instruction.
11967 11820 *
11968 11821 * If the ISM mappings are changing, the TSB is relocated,
11969 11822 * the process is swapped, the process is joining SCD or
11970 11823 * leaving SCD or shared regions we serialize behind the
11971 11824 * controlling thread with hat lock, sfmmu_flags and
11972 11825 * sfmmu_tsb_cv condition variable.
11973 11826 */
11974 11827
11975 11828 /*
11976 11829 * Must set lwp state to LWP_SYS before
11977 11830 * trying to acquire any adaptive lock
11978 11831 */
11979 11832 lwp = ttolwp(curthread);
11980 11833 ASSERT(lwp);
11981 11834 lwp_save_state = lwp->lwp_state;
11982 11835 lwp->lwp_state = LWP_SYS;
11983 11836
11984 11837 hatlockp = sfmmu_hat_enter(sfmmup);
11985 11838 retry:
11986 11839 if ((scdp = sfmmup->sfmmu_scdp) != NULL) {
11987 11840 shsfmmup = scdp->scd_sfmmup;
11988 11841 ASSERT(shsfmmup != NULL);
11989 11842
11990 11843 for (tsbinfop = shsfmmup->sfmmu_tsb; tsbinfop != NULL;
11991 11844 tsbinfop = tsbinfop->tsb_next) {
11992 11845 if (tsbinfop->tsb_flags & TSB_RELOC_FLAG) {
11993 11846 /* drop the private hat lock */
11994 11847 sfmmu_hat_exit(hatlockp);
11995 11848 /* acquire the shared hat lock */
11996 11849 shatlockp = sfmmu_hat_enter(shsfmmup);
11997 11850 /*
11998 11851 * recheck to see if anything changed
11999 11852 * after we drop the private hat lock.
12000 11853 */
12001 11854 if (sfmmup->sfmmu_scdp == scdp &&
12002 11855 shsfmmup == scdp->scd_sfmmup) {
12003 11856 sfmmu_tsb_chk_reloc(shsfmmup,
12004 11857 shatlockp);
12005 11858 }
12006 11859 sfmmu_hat_exit(shatlockp);
12007 11860 hatlockp = sfmmu_hat_enter(sfmmup);
12008 11861 goto retry;
12009 11862 }
12010 11863 }
12011 11864 }
12012 11865
12013 11866 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL;
12014 11867 tsbinfop = tsbinfop->tsb_next) {
12015 11868 if (tsbinfop->tsb_flags & TSB_RELOC_FLAG) {
12016 11869 cv_wait(&sfmmup->sfmmu_tsb_cv,
12017 11870 HATLOCK_MUTEXP(hatlockp));
12018 11871 goto retry;
12019 11872 }
12020 11873 }
12021 11874
12022 11875 /*
12023 11876 * Wait for ISM maps to be updated.
12024 11877 */
12025 11878 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)) {
12026 11879 cv_wait(&sfmmup->sfmmu_tsb_cv,
12027 11880 HATLOCK_MUTEXP(hatlockp));
12028 11881 goto retry;
12029 11882 }
12030 11883
12031 11884 /* Is this process joining an SCD? */
12032 11885 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)) {
12033 11886 /*
12034 11887 * Flush private TSB and setup shared TSB.
12035 11888 * sfmmu_finish_join_scd() does not drop the
12036 11889 * hat lock.
12037 11890 */
12038 11891 sfmmu_finish_join_scd(sfmmup);
12039 11892 SFMMU_FLAGS_CLEAR(sfmmup, HAT_JOIN_SCD);
12040 11893 }
12041 11894
12042 11895 /*
12043 11896 * If we're swapping in, get TSB(s). Note that we must do
12044 11897 * this before we get a ctx or load the MMU state. Once
12045 11898 * we swap in we have to recheck to make sure the TSB(s) and
12046 11899 * ISM mappings didn't change while we slept.
12047 11900 */
12048 11901 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) {
12049 11902 sfmmu_tsb_swapin(sfmmup, hatlockp);
12050 11903 goto retry;
12051 11904 }
12052 11905
12053 11906 sfmmu_get_ctx(sfmmup);
12054 11907
12055 11908 sfmmu_hat_exit(hatlockp);
12056 11909 /*
12057 11910 * Must restore lwp_state if not calling
12058 11911 * trap() for further processing. Restore
12059 11912 * it anyway.
12060 11913 */
12061 11914 lwp->lwp_state = lwp_save_state;
12062 11915 return;
12063 11916 }
12064 11917 trap(rp, (caddr_t)tagaccess, traptype, 0);
12065 11918 }
12066 11919
12067 11920 static void
12068 11921 sfmmu_tsb_chk_reloc(sfmmu_t *sfmmup, hatlock_t *hatlockp)
12069 11922 {
12070 11923 struct tsb_info *tp;
12071 11924
12072 11925 ASSERT(sfmmu_hat_lock_held(sfmmup));
12073 11926
12074 11927 for (tp = sfmmup->sfmmu_tsb; tp != NULL; tp = tp->tsb_next) {
12075 11928 if (tp->tsb_flags & TSB_RELOC_FLAG) {
12076 11929 cv_wait(&sfmmup->sfmmu_tsb_cv,
12077 11930 HATLOCK_MUTEXP(hatlockp));
12078 11931 break;
12079 11932 }
12080 11933 }
12081 11934 }
12082 11935
12083 11936 /*
12084 11937 * sfmmu_vatopfn_suspended is called from GET_TTE when TL=0 and
12085 11938 * TTE_SUSPENDED bit set in tte we block on aquiring a page lock
12086 11939 * rather than spinning to avoid send mondo timeouts with
12087 11940 * interrupts enabled. When the lock is acquired it is immediately
12088 11941 * released and we return back to sfmmu_vatopfn just after
12089 11942 * the GET_TTE call.
12090 11943 */
12091 11944 void
12092 11945 sfmmu_vatopfn_suspended(caddr_t vaddr, sfmmu_t *sfmmu, tte_t *ttep)
12093 11946 {
12094 11947 struct page **pp;
12095 11948
12096 11949 (void) as_pagelock(sfmmu->sfmmu_as, &pp, vaddr, TTE_CSZ(ttep), S_WRITE);
12097 11950 as_pageunlock(sfmmu->sfmmu_as, pp, vaddr, TTE_CSZ(ttep), S_WRITE);
12098 11951 }
12099 11952
12100 11953 /*
12101 11954 * sfmmu_tsbmiss_suspended is called from GET_TTE when TL>0 and
12102 11955 * TTE_SUSPENDED bit set in tte. We do this so that we can handle
12103 11956 * cross traps which cannot be handled while spinning in the
12104 11957 * trap handlers. Simply enter and exit the kpr_suspendlock spin
12105 11958 * mutex, which is held by the holder of the suspend bit, and then
12106 11959 * retry the trapped instruction after unwinding.
12107 11960 */
12108 11961 /*ARGSUSED*/
12109 11962 void
12110 11963 sfmmu_tsbmiss_suspended(struct regs *rp, uintptr_t tagacc, uint_t traptype)
12111 11964 {
12112 11965 ASSERT(curthread != kreloc_thread);
12113 11966 mutex_enter(&kpr_suspendlock);
12114 11967 mutex_exit(&kpr_suspendlock);
12115 11968 }
12116 11969
12117 11970 /*
12118 11971 * This routine could be optimized to reduce the number of xcalls by flushing
12119 11972 * the entire TLBs if region reference count is above some threshold but the
12120 11973 * tradeoff will depend on the size of the TLB. So for now flush the specific
12121 11974 * page a context at a time.
12122 11975 *
12123 11976 * If uselocks is 0 then it's called after all cpus were captured and all the
12124 11977 * hat locks were taken. In this case don't take the region lock by relying on
12125 11978 * the order of list region update operations in hat_join_region(),
12126 11979 * hat_leave_region() and hat_dup_region(). The ordering in those routines
12127 11980 * guarantees that list is always forward walkable and reaches active sfmmus
12128 11981 * regardless of where xc_attention() captures a cpu.
12129 11982 */
12130 11983 cpuset_t
12131 11984 sfmmu_rgntlb_demap(caddr_t addr, sf_region_t *rgnp,
12132 11985 struct hme_blk *hmeblkp, int uselocks)
12133 11986 {
12134 11987 sfmmu_t *sfmmup;
12135 11988 cpuset_t cpuset;
12136 11989 cpuset_t rcpuset;
12137 11990 hatlock_t *hatlockp;
12138 11991 uint_t rid = rgnp->rgn_id;
12139 11992 sf_rgn_link_t *rlink;
12140 11993 sf_scd_t *scdp;
12141 11994
12142 11995 ASSERT(hmeblkp->hblk_shared);
12143 11996 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
12144 11997 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
12145 11998
12146 11999 CPUSET_ZERO(rcpuset);
12147 12000 if (uselocks) {
12148 12001 mutex_enter(&rgnp->rgn_mutex);
12149 12002 }
12150 12003 sfmmup = rgnp->rgn_sfmmu_head;
12151 12004 while (sfmmup != NULL) {
12152 12005 if (uselocks) {
12153 12006 hatlockp = sfmmu_hat_enter(sfmmup);
12154 12007 }
12155 12008
12156 12009 /*
12157 12010 * When an SCD is created the SCD hat is linked on the sfmmu
12158 12011 * region lists for each hme region which is part of the
12159 12012 * SCD. If we find an SCD hat, when walking these lists,
12160 12013 * then we flush the shared TSBs, if we find a private hat,
12161 12014 * which is part of an SCD, but where the region
12162 12015 * is not part of the SCD then we flush the private TSBs.
12163 12016 */
12164 12017 if (!sfmmup->sfmmu_scdhat && sfmmup->sfmmu_scdp != NULL &&
12165 12018 !SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)) {
12166 12019 scdp = sfmmup->sfmmu_scdp;
12167 12020 if (SF_RGNMAP_TEST(scdp->scd_hmeregion_map, rid)) {
12168 12021 if (uselocks) {
12169 12022 sfmmu_hat_exit(hatlockp);
12170 12023 }
12171 12024 goto next;
12172 12025 }
12173 12026 }
12174 12027
12175 12028 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0);
12176 12029
12177 12030 kpreempt_disable();
12178 12031 cpuset = sfmmup->sfmmu_cpusran;
12179 12032 CPUSET_AND(cpuset, cpu_ready_set);
12180 12033 CPUSET_DEL(cpuset, CPU->cpu_id);
12181 12034 SFMMU_XCALL_STATS(sfmmup);
12182 12035 xt_some(cpuset, vtag_flushpage_tl1,
12183 12036 (uint64_t)addr, (uint64_t)sfmmup);
12184 12037 vtag_flushpage(addr, (uint64_t)sfmmup);
12185 12038 if (uselocks) {
12186 12039 sfmmu_hat_exit(hatlockp);
12187 12040 }
12188 12041 kpreempt_enable();
12189 12042 CPUSET_OR(rcpuset, cpuset);
12190 12043
12191 12044 next:
12192 12045 /* LINTED: constant in conditional context */
12193 12046 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 0, 0);
12194 12047 ASSERT(rlink != NULL);
12195 12048 sfmmup = rlink->next;
12196 12049 }
12197 12050 if (uselocks) {
12198 12051 mutex_exit(&rgnp->rgn_mutex);
12199 12052 }
12200 12053 return (rcpuset);
12201 12054 }
12202 12055
12203 12056 /*
12204 12057 * This routine takes an sfmmu pointer and the va for an adddress in an
12205 12058 * ISM region as input and returns the corresponding region id in ism_rid.
12206 12059 * The return value of 1 indicates that a region has been found and ism_rid
12207 12060 * is valid, otherwise 0 is returned.
12208 12061 */
12209 12062 static int
12210 12063 find_ism_rid(sfmmu_t *sfmmup, sfmmu_t *ism_sfmmup, caddr_t va, uint_t *ism_rid)
12211 12064 {
12212 12065 ism_blk_t *ism_blkp;
12213 12066 int i;
12214 12067 ism_map_t *ism_map;
12215 12068 #ifdef DEBUG
12216 12069 struct hat *ism_hatid;
12217 12070 #endif
12218 12071 ASSERT(sfmmu_hat_lock_held(sfmmup));
12219 12072
12220 12073 ism_blkp = sfmmup->sfmmu_iblk;
12221 12074 while (ism_blkp != NULL) {
12222 12075 ism_map = ism_blkp->iblk_maps;
12223 12076 for (i = 0; i < ISM_MAP_SLOTS && ism_map[i].imap_ismhat; i++) {
12224 12077 if ((va >= ism_start(ism_map[i])) &&
12225 12078 (va < ism_end(ism_map[i]))) {
12226 12079
12227 12080 *ism_rid = ism_map[i].imap_rid;
12228 12081 #ifdef DEBUG
12229 12082 ism_hatid = ism_map[i].imap_ismhat;
12230 12083 ASSERT(ism_hatid == ism_sfmmup);
12231 12084 ASSERT(ism_hatid->sfmmu_ismhat);
12232 12085 #endif
12233 12086 return (1);
12234 12087 }
12235 12088 }
12236 12089 ism_blkp = ism_blkp->iblk_next;
12237 12090 }
12238 12091 return (0);
12239 12092 }
12240 12093
12241 12094 /*
12242 12095 * Special routine to flush out ism mappings- TSBs, TLBs and D-caches.
12243 12096 * This routine may be called with all cpu's captured. Therefore, the
12244 12097 * caller is responsible for holding all locks and disabling kernel
12245 12098 * preemption.
12246 12099 */
12247 12100 /* ARGSUSED */
12248 12101 static void
12249 12102 sfmmu_ismtlbcache_demap(caddr_t addr, sfmmu_t *ism_sfmmup,
12250 12103 struct hme_blk *hmeblkp, pfn_t pfnum, int cache_flush_flag)
12251 12104 {
12252 12105 cpuset_t cpuset;
12253 12106 caddr_t va;
12254 12107 ism_ment_t *ment;
12255 12108 sfmmu_t *sfmmup;
12256 12109 #ifdef VAC
12257 12110 int vcolor;
12258 12111 #endif
12259 12112
12260 12113 sf_scd_t *scdp;
12261 12114 uint_t ism_rid;
12262 12115
12263 12116 ASSERT(!hmeblkp->hblk_shared);
12264 12117 /*
12265 12118 * Walk the ism_hat's mapping list and flush the page
12266 12119 * from every hat sharing this ism_hat. This routine
12267 12120 * may be called while all cpu's have been captured.
12268 12121 * Therefore we can't attempt to grab any locks. For now
12269 12122 * this means we will protect the ism mapping list under
12270 12123 * a single lock which will be grabbed by the caller.
12271 12124 * If hat_share/unshare scalibility becomes a performance
12272 12125 * problem then we may need to re-think ism mapping list locking.
12273 12126 */
12274 12127 ASSERT(ism_sfmmup->sfmmu_ismhat);
12275 12128 ASSERT(MUTEX_HELD(&ism_mlist_lock));
12276 12129 addr = addr - ISMID_STARTADDR;
12277 12130
12278 12131 for (ment = ism_sfmmup->sfmmu_iment; ment; ment = ment->iment_next) {
12279 12132
12280 12133 sfmmup = ment->iment_hat;
12281 12134
12282 12135 va = ment->iment_base_va;
12283 12136 va = (caddr_t)((uintptr_t)va + (uintptr_t)addr);
12284 12137
12285 12138 /*
12286 12139 * When an SCD is created the SCD hat is linked on the ism
12287 12140 * mapping lists for each ISM segment which is part of the
12288 12141 * SCD. If we find an SCD hat, when walking these lists,
12289 12142 * then we flush the shared TSBs, if we find a private hat,
12290 12143 * which is part of an SCD, but where the region
12291 12144 * corresponding to this va is not part of the SCD then we
12292 12145 * flush the private TSBs.
12293 12146 */
12294 12147 if (!sfmmup->sfmmu_scdhat && sfmmup->sfmmu_scdp != NULL &&
12295 12148 !SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD) &&
12296 12149 !SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)) {
12297 12150 if (!find_ism_rid(sfmmup, ism_sfmmup, va,
12298 12151 &ism_rid)) {
12299 12152 cmn_err(CE_PANIC,
12300 12153 "can't find matching ISM rid!");
12301 12154 }
12302 12155
12303 12156 scdp = sfmmup->sfmmu_scdp;
12304 12157 if (SFMMU_IS_ISMRID_VALID(ism_rid) &&
12305 12158 SF_RGNMAP_TEST(scdp->scd_ismregion_map,
12306 12159 ism_rid)) {
12307 12160 continue;
12308 12161 }
12309 12162 }
12310 12163 SFMMU_UNLOAD_TSB(va, sfmmup, hmeblkp, 1);
12311 12164
12312 12165 cpuset = sfmmup->sfmmu_cpusran;
12313 12166 CPUSET_AND(cpuset, cpu_ready_set);
12314 12167 CPUSET_DEL(cpuset, CPU->cpu_id);
12315 12168 SFMMU_XCALL_STATS(sfmmup);
12316 12169 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)va,
12317 12170 (uint64_t)sfmmup);
12318 12171 vtag_flushpage(va, (uint64_t)sfmmup);
12319 12172
12320 12173 #ifdef VAC
12321 12174 /*
12322 12175 * Flush D$
12323 12176 * When flushing D$ we must flush all
12324 12177 * cpu's. See sfmmu_cache_flush().
12325 12178 */
12326 12179 if (cache_flush_flag == CACHE_FLUSH) {
12327 12180 cpuset = cpu_ready_set;
12328 12181 CPUSET_DEL(cpuset, CPU->cpu_id);
12329 12182
12330 12183 SFMMU_XCALL_STATS(sfmmup);
12331 12184 vcolor = addr_to_vcolor(va);
12332 12185 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor);
12333 12186 vac_flushpage(pfnum, vcolor);
12334 12187 }
12335 12188 #endif /* VAC */
12336 12189 }
12337 12190 }
12338 12191
12339 12192 /*
12340 12193 * Demaps the TSB, CPU caches, and flushes all TLBs on all CPUs of
12341 12194 * a particular virtual address and ctx. If noflush is set we do not
12342 12195 * flush the TLB/TSB. This function may or may not be called with the
12343 12196 * HAT lock held.
12344 12197 */
12345 12198 static void
12346 12199 sfmmu_tlbcache_demap(caddr_t addr, sfmmu_t *sfmmup, struct hme_blk *hmeblkp,
12347 12200 pfn_t pfnum, int tlb_noflush, int cpu_flag, int cache_flush_flag,
12348 12201 int hat_lock_held)
12349 12202 {
12350 12203 #ifdef VAC
12351 12204 int vcolor;
12352 12205 #endif
12353 12206 cpuset_t cpuset;
12354 12207 hatlock_t *hatlockp;
12355 12208
12356 12209 ASSERT(!hmeblkp->hblk_shared);
12357 12210
12358 12211 #if defined(lint) && !defined(VAC)
12359 12212 pfnum = pfnum;
12360 12213 cpu_flag = cpu_flag;
12361 12214 cache_flush_flag = cache_flush_flag;
12362 12215 #endif
12363 12216
12364 12217 /*
12365 12218 * There is no longer a need to protect against ctx being
12366 12219 * stolen here since we don't store the ctx in the TSB anymore.
12367 12220 */
12368 12221 #ifdef VAC
12369 12222 vcolor = addr_to_vcolor(addr);
12370 12223 #endif
12371 12224
12372 12225 /*
12373 12226 * We must hold the hat lock during the flush of TLB,
12374 12227 * to avoid a race with sfmmu_invalidate_ctx(), where
12375 12228 * sfmmu_cnum on a MMU could be set to INVALID_CONTEXT,
12376 12229 * causing TLB demap routine to skip flush on that MMU.
12377 12230 * If the context on a MMU has already been set to
12378 12231 * INVALID_CONTEXT, we just get an extra flush on
12379 12232 * that MMU.
12380 12233 */
12381 12234 if (!hat_lock_held && !tlb_noflush)
12382 12235 hatlockp = sfmmu_hat_enter(sfmmup);
12383 12236
12384 12237 kpreempt_disable();
12385 12238 if (!tlb_noflush) {
12386 12239 /*
12387 12240 * Flush the TSB and TLB.
12388 12241 */
12389 12242 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0);
12390 12243
12391 12244 cpuset = sfmmup->sfmmu_cpusran;
12392 12245 CPUSET_AND(cpuset, cpu_ready_set);
12393 12246 CPUSET_DEL(cpuset, CPU->cpu_id);
12394 12247
12395 12248 SFMMU_XCALL_STATS(sfmmup);
12396 12249
12397 12250 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr,
12398 12251 (uint64_t)sfmmup);
12399 12252
12400 12253 vtag_flushpage(addr, (uint64_t)sfmmup);
12401 12254 }
12402 12255
12403 12256 if (!hat_lock_held && !tlb_noflush)
12404 12257 sfmmu_hat_exit(hatlockp);
12405 12258
12406 12259 #ifdef VAC
12407 12260 /*
12408 12261 * Flush the D$
12409 12262 *
12410 12263 * Even if the ctx is stolen, we need to flush the
12411 12264 * cache. Our ctx stealer only flushes the TLBs.
12412 12265 */
12413 12266 if (cache_flush_flag == CACHE_FLUSH) {
12414 12267 if (cpu_flag & FLUSH_ALL_CPUS) {
12415 12268 cpuset = cpu_ready_set;
12416 12269 } else {
12417 12270 cpuset = sfmmup->sfmmu_cpusran;
12418 12271 CPUSET_AND(cpuset, cpu_ready_set);
12419 12272 }
12420 12273 CPUSET_DEL(cpuset, CPU->cpu_id);
12421 12274 SFMMU_XCALL_STATS(sfmmup);
12422 12275 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor);
12423 12276 vac_flushpage(pfnum, vcolor);
12424 12277 }
12425 12278 #endif /* VAC */
12426 12279 kpreempt_enable();
12427 12280 }
12428 12281
12429 12282 /*
12430 12283 * Demaps the TSB and flushes all TLBs on all cpus for a particular virtual
12431 12284 * address and ctx. If noflush is set we do not currently do anything.
12432 12285 * This function may or may not be called with the HAT lock held.
12433 12286 */
12434 12287 static void
12435 12288 sfmmu_tlb_demap(caddr_t addr, sfmmu_t *sfmmup, struct hme_blk *hmeblkp,
12436 12289 int tlb_noflush, int hat_lock_held)
12437 12290 {
12438 12291 cpuset_t cpuset;
12439 12292 hatlock_t *hatlockp;
12440 12293
12441 12294 ASSERT(!hmeblkp->hblk_shared);
12442 12295
12443 12296 /*
12444 12297 * If the process is exiting we have nothing to do.
12445 12298 */
12446 12299 if (tlb_noflush)
12447 12300 return;
12448 12301
12449 12302 /*
12450 12303 * Flush TSB.
12451 12304 */
12452 12305 if (!hat_lock_held)
12453 12306 hatlockp = sfmmu_hat_enter(sfmmup);
12454 12307 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0);
12455 12308
12456 12309 kpreempt_disable();
12457 12310
12458 12311 cpuset = sfmmup->sfmmu_cpusran;
12459 12312 CPUSET_AND(cpuset, cpu_ready_set);
12460 12313 CPUSET_DEL(cpuset, CPU->cpu_id);
12461 12314
12462 12315 SFMMU_XCALL_STATS(sfmmup);
12463 12316 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr, (uint64_t)sfmmup);
12464 12317
12465 12318 vtag_flushpage(addr, (uint64_t)sfmmup);
12466 12319
12467 12320 if (!hat_lock_held)
12468 12321 sfmmu_hat_exit(hatlockp);
12469 12322
12470 12323 kpreempt_enable();
12471 12324
12472 12325 }
12473 12326
12474 12327 /*
12475 12328 * Special case of sfmmu_tlb_demap for MMU_PAGESIZE hblks. Use the xcall
12476 12329 * call handler that can flush a range of pages to save on xcalls.
12477 12330 */
12478 12331 static int sfmmu_xcall_save;
12479 12332
12480 12333 /*
12481 12334 * this routine is never used for demaping addresses backed by SRD hmeblks.
12482 12335 */
12483 12336 static void
12484 12337 sfmmu_tlb_range_demap(demap_range_t *dmrp)
12485 12338 {
12486 12339 sfmmu_t *sfmmup = dmrp->dmr_sfmmup;
12487 12340 hatlock_t *hatlockp;
12488 12341 cpuset_t cpuset;
12489 12342 uint64_t sfmmu_pgcnt;
12490 12343 pgcnt_t pgcnt = 0;
12491 12344 int pgunload = 0;
12492 12345 int dirtypg = 0;
12493 12346 caddr_t addr = dmrp->dmr_addr;
12494 12347 caddr_t eaddr;
12495 12348 uint64_t bitvec = dmrp->dmr_bitvec;
12496 12349
12497 12350 ASSERT(bitvec & 1);
12498 12351
12499 12352 /*
12500 12353 * Flush TSB and calculate number of pages to flush.
12501 12354 */
12502 12355 while (bitvec != 0) {
12503 12356 dirtypg = 0;
12504 12357 /*
12505 12358 * Find the first page to flush and then count how many
12506 12359 * pages there are after it that also need to be flushed.
12507 12360 * This way the number of TSB flushes is minimized.
12508 12361 */
12509 12362 while ((bitvec & 1) == 0) {
12510 12363 pgcnt++;
12511 12364 addr += MMU_PAGESIZE;
12512 12365 bitvec >>= 1;
12513 12366 }
12514 12367 while (bitvec & 1) {
12515 12368 dirtypg++;
12516 12369 bitvec >>= 1;
12517 12370 }
12518 12371 eaddr = addr + ptob(dirtypg);
12519 12372 hatlockp = sfmmu_hat_enter(sfmmup);
12520 12373 sfmmu_unload_tsb_range(sfmmup, addr, eaddr, TTE8K);
12521 12374 sfmmu_hat_exit(hatlockp);
12522 12375 pgunload += dirtypg;
12523 12376 addr = eaddr;
12524 12377 pgcnt += dirtypg;
12525 12378 }
12526 12379
12527 12380 ASSERT((pgcnt<<MMU_PAGESHIFT) <= dmrp->dmr_endaddr - dmrp->dmr_addr);
12528 12381 if (sfmmup->sfmmu_free == 0) {
12529 12382 addr = dmrp->dmr_addr;
12530 12383 bitvec = dmrp->dmr_bitvec;
12531 12384
12532 12385 /*
12533 12386 * make sure it has SFMMU_PGCNT_SHIFT bits only,
12534 12387 * as it will be used to pack argument for xt_some
12535 12388 */
12536 12389 ASSERT((pgcnt > 0) &&
12537 12390 (pgcnt <= (1 << SFMMU_PGCNT_SHIFT)));
12538 12391
12539 12392 /*
12540 12393 * Encode pgcnt as (pgcnt -1 ), and pass (pgcnt - 1) in
12541 12394 * the low 6 bits of sfmmup. This is doable since pgcnt
12542 12395 * always >= 1.
12543 12396 */
12544 12397 ASSERT(!((uint64_t)sfmmup & SFMMU_PGCNT_MASK));
12545 12398 sfmmu_pgcnt = (uint64_t)sfmmup |
12546 12399 ((pgcnt - 1) & SFMMU_PGCNT_MASK);
12547 12400
12548 12401 /*
12549 12402 * We must hold the hat lock during the flush of TLB,
12550 12403 * to avoid a race with sfmmu_invalidate_ctx(), where
12551 12404 * sfmmu_cnum on a MMU could be set to INVALID_CONTEXT,
12552 12405 * causing TLB demap routine to skip flush on that MMU.
12553 12406 * If the context on a MMU has already been set to
12554 12407 * INVALID_CONTEXT, we just get an extra flush on
12555 12408 * that MMU.
12556 12409 */
12557 12410 hatlockp = sfmmu_hat_enter(sfmmup);
12558 12411 kpreempt_disable();
12559 12412
12560 12413 cpuset = sfmmup->sfmmu_cpusran;
12561 12414 CPUSET_AND(cpuset, cpu_ready_set);
12562 12415 CPUSET_DEL(cpuset, CPU->cpu_id);
12563 12416
12564 12417 SFMMU_XCALL_STATS(sfmmup);
12565 12418 xt_some(cpuset, vtag_flush_pgcnt_tl1, (uint64_t)addr,
12566 12419 sfmmu_pgcnt);
12567 12420
12568 12421 for (; bitvec != 0; bitvec >>= 1) {
12569 12422 if (bitvec & 1)
12570 12423 vtag_flushpage(addr, (uint64_t)sfmmup);
12571 12424 addr += MMU_PAGESIZE;
12572 12425 }
12573 12426 kpreempt_enable();
12574 12427 sfmmu_hat_exit(hatlockp);
12575 12428
12576 12429 sfmmu_xcall_save += (pgunload-1);
12577 12430 }
12578 12431 dmrp->dmr_bitvec = 0;
12579 12432 }
12580 12433
12581 12434 /*
12582 12435 * In cases where we need to synchronize with TLB/TSB miss trap
12583 12436 * handlers, _and_ need to flush the TLB, it's a lot easier to
12584 12437 * throw away the context from the process than to do a
12585 12438 * special song and dance to keep things consistent for the
12586 12439 * handlers.
12587 12440 *
12588 12441 * Since the process suddenly ends up without a context and our caller
12589 12442 * holds the hat lock, threads that fault after this function is called
12590 12443 * will pile up on the lock. We can then do whatever we need to
12591 12444 * atomically from the context of the caller. The first blocked thread
12592 12445 * to resume executing will get the process a new context, and the
12593 12446 * process will resume executing.
12594 12447 *
12595 12448 * One added advantage of this approach is that on MMUs that
12596 12449 * support a "flush all" operation, we will delay the flush until
12597 12450 * cnum wrap-around, and then flush the TLB one time. This
12598 12451 * is rather rare, so it's a lot less expensive than making 8000
12599 12452 * x-calls to flush the TLB 8000 times.
12600 12453 *
12601 12454 * A per-process (PP) lock is used to synchronize ctx allocations in
12602 12455 * resume() and ctx invalidations here.
12603 12456 */
12604 12457 static void
12605 12458 sfmmu_invalidate_ctx(sfmmu_t *sfmmup)
12606 12459 {
12607 12460 cpuset_t cpuset;
12608 12461 int cnum, currcnum;
12609 12462 mmu_ctx_t *mmu_ctxp;
12610 12463 int i;
12611 12464 uint_t pstate_save;
12612 12465
12613 12466 SFMMU_STAT(sf_ctx_inv);
12614 12467
12615 12468 ASSERT(sfmmu_hat_lock_held(sfmmup));
12616 12469 ASSERT(sfmmup != ksfmmup);
12617 12470
12618 12471 kpreempt_disable();
12619 12472
12620 12473 mmu_ctxp = CPU_MMU_CTXP(CPU);
12621 12474 ASSERT(mmu_ctxp);
12622 12475 ASSERT(mmu_ctxp->mmu_idx < max_mmu_ctxdoms);
12623 12476 ASSERT(mmu_ctxp == mmu_ctxs_tbl[mmu_ctxp->mmu_idx]);
12624 12477
12625 12478 currcnum = sfmmup->sfmmu_ctxs[mmu_ctxp->mmu_idx].cnum;
12626 12479
12627 12480 pstate_save = sfmmu_disable_intrs();
12628 12481
12629 12482 lock_set(&sfmmup->sfmmu_ctx_lock); /* acquire PP lock */
12630 12483 /* set HAT cnum invalid across all context domains. */
12631 12484 for (i = 0; i < max_mmu_ctxdoms; i++) {
12632 12485
12633 12486 cnum = sfmmup->sfmmu_ctxs[i].cnum;
12634 12487 if (cnum == INVALID_CONTEXT) {
12635 12488 continue;
12636 12489 }
12637 12490
12638 12491 sfmmup->sfmmu_ctxs[i].cnum = INVALID_CONTEXT;
12639 12492 }
12640 12493 membar_enter(); /* make sure globally visible to all CPUs */
12641 12494 lock_clear(&sfmmup->sfmmu_ctx_lock); /* release PP lock */
12642 12495
12643 12496 sfmmu_enable_intrs(pstate_save);
12644 12497
12645 12498 cpuset = sfmmup->sfmmu_cpusran;
12646 12499 CPUSET_DEL(cpuset, CPU->cpu_id);
12647 12500 CPUSET_AND(cpuset, cpu_ready_set);
12648 12501 if (!CPUSET_ISNULL(cpuset)) {
12649 12502 SFMMU_XCALL_STATS(sfmmup);
12650 12503 xt_some(cpuset, sfmmu_raise_tsb_exception,
12651 12504 (uint64_t)sfmmup, INVALID_CONTEXT);
12652 12505 xt_sync(cpuset);
12653 12506 SFMMU_STAT(sf_tsb_raise_exception);
12654 12507 SFMMU_MMU_STAT(mmu_tsb_raise_exception);
12655 12508 }
12656 12509
12657 12510 /*
12658 12511 * If the hat to-be-invalidated is the same as the current
12659 12512 * process on local CPU we need to invalidate
12660 12513 * this CPU context as well.
12661 12514 */
12662 12515 if ((sfmmu_getctx_sec() == currcnum) &&
12663 12516 (currcnum != INVALID_CONTEXT)) {
12664 12517 /* sets shared context to INVALID too */
12665 12518 sfmmu_setctx_sec(INVALID_CONTEXT);
12666 12519 sfmmu_clear_utsbinfo();
12667 12520 }
12668 12521
12669 12522 SFMMU_FLAGS_SET(sfmmup, HAT_ALLCTX_INVALID);
12670 12523
12671 12524 kpreempt_enable();
12672 12525
12673 12526 /*
12674 12527 * we hold the hat lock, so nobody should allocate a context
12675 12528 * for us yet
12676 12529 */
12677 12530 ASSERT(sfmmup->sfmmu_ctxs[mmu_ctxp->mmu_idx].cnum == INVALID_CONTEXT);
12678 12531 }
12679 12532
12680 12533 #ifdef VAC
12681 12534 /*
12682 12535 * We need to flush the cache in all cpus. It is possible that
12683 12536 * a process referenced a page as cacheable but has sinced exited
12684 12537 * and cleared the mapping list. We still to flush it but have no
12685 12538 * state so all cpus is the only alternative.
12686 12539 */
12687 12540 void
12688 12541 sfmmu_cache_flush(pfn_t pfnum, int vcolor)
12689 12542 {
12690 12543 cpuset_t cpuset;
12691 12544
12692 12545 kpreempt_disable();
12693 12546 cpuset = cpu_ready_set;
12694 12547 CPUSET_DEL(cpuset, CPU->cpu_id);
12695 12548 SFMMU_XCALL_STATS(NULL); /* account to any ctx */
12696 12549 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor);
12697 12550 xt_sync(cpuset);
12698 12551 vac_flushpage(pfnum, vcolor);
12699 12552 kpreempt_enable();
12700 12553 }
12701 12554
12702 12555 void
12703 12556 sfmmu_cache_flushcolor(int vcolor, pfn_t pfnum)
12704 12557 {
12705 12558 cpuset_t cpuset;
12706 12559
12707 12560 ASSERT(vcolor >= 0);
12708 12561
12709 12562 kpreempt_disable();
12710 12563 cpuset = cpu_ready_set;
12711 12564 CPUSET_DEL(cpuset, CPU->cpu_id);
12712 12565 SFMMU_XCALL_STATS(NULL); /* account to any ctx */
12713 12566 xt_some(cpuset, vac_flushcolor_tl1, vcolor, pfnum);
12714 12567 xt_sync(cpuset);
12715 12568 vac_flushcolor(vcolor, pfnum);
12716 12569 kpreempt_enable();
12717 12570 }
12718 12571 #endif /* VAC */
12719 12572
12720 12573 /*
12721 12574 * We need to prevent processes from accessing the TSB using a cached physical
12722 12575 * address. It's alright if they try to access the TSB via virtual address
12723 12576 * since they will just fault on that virtual address once the mapping has
12724 12577 * been suspended.
12725 12578 */
12726 12579 #pragma weak sendmondo_in_recover
12727 12580
12728 12581 /* ARGSUSED */
12729 12582 static int
12730 12583 sfmmu_tsb_pre_relocator(caddr_t va, uint_t tsbsz, uint_t flags, void *tsbinfo)
12731 12584 {
12732 12585 struct tsb_info *tsbinfop = (struct tsb_info *)tsbinfo;
12733 12586 sfmmu_t *sfmmup = tsbinfop->tsb_sfmmu;
12734 12587 hatlock_t *hatlockp;
12735 12588 sf_scd_t *scdp;
12736 12589
12737 12590 if (flags != HAT_PRESUSPEND)
12738 12591 return (0);
12739 12592
12740 12593 /*
12741 12594 * If tsb is a shared TSB with TSB_SHAREDCTX set, sfmmup must
12742 12595 * be a shared hat, then set SCD's tsbinfo's flag.
12743 12596 * If tsb is not shared, sfmmup is a private hat, then set
12744 12597 * its private tsbinfo's flag.
12745 12598 */
12746 12599 hatlockp = sfmmu_hat_enter(sfmmup);
12747 12600 tsbinfop->tsb_flags |= TSB_RELOC_FLAG;
12748 12601
12749 12602 if (!(tsbinfop->tsb_flags & TSB_SHAREDCTX)) {
12750 12603 sfmmu_tsb_inv_ctx(sfmmup);
12751 12604 sfmmu_hat_exit(hatlockp);
12752 12605 } else {
12753 12606 /* release lock on the shared hat */
12754 12607 sfmmu_hat_exit(hatlockp);
12755 12608 /* sfmmup is a shared hat */
12756 12609 ASSERT(sfmmup->sfmmu_scdhat);
12757 12610 scdp = sfmmup->sfmmu_scdp;
12758 12611 ASSERT(scdp != NULL);
12759 12612 /* get private hat from the scd list */
12760 12613 mutex_enter(&scdp->scd_mutex);
12761 12614 sfmmup = scdp->scd_sf_list;
12762 12615 while (sfmmup != NULL) {
12763 12616 hatlockp = sfmmu_hat_enter(sfmmup);
12764 12617 /*
12765 12618 * We do not call sfmmu_tsb_inv_ctx here because
12766 12619 * sendmondo_in_recover check is only needed for
12767 12620 * sun4u.
12768 12621 */
12769 12622 sfmmu_invalidate_ctx(sfmmup);
12770 12623 sfmmu_hat_exit(hatlockp);
12771 12624 sfmmup = sfmmup->sfmmu_scd_link.next;
12772 12625
12773 12626 }
12774 12627 mutex_exit(&scdp->scd_mutex);
12775 12628 }
12776 12629 return (0);
12777 12630 }
12778 12631
12779 12632 static void
12780 12633 sfmmu_tsb_inv_ctx(sfmmu_t *sfmmup)
12781 12634 {
12782 12635 extern uint32_t sendmondo_in_recover;
12783 12636
12784 12637 ASSERT(sfmmu_hat_lock_held(sfmmup));
12785 12638
12786 12639 /*
12787 12640 * For Cheetah+ Erratum 25:
12788 12641 * Wait for any active recovery to finish. We can't risk
12789 12642 * relocating the TSB of the thread running mondo_recover_proc()
12790 12643 * since, if we did that, we would deadlock. The scenario we are
12791 12644 * trying to avoid is as follows:
12792 12645 *
12793 12646 * THIS CPU RECOVER CPU
12794 12647 * -------- -----------
12795 12648 * Begins recovery, walking through TSB
12796 12649 * hat_pagesuspend() TSB TTE
12797 12650 * TLB miss on TSB TTE, spins at TL1
12798 12651 * xt_sync()
12799 12652 * send_mondo_timeout()
12800 12653 * mondo_recover_proc()
12801 12654 * ((deadlocked))
12802 12655 *
12803 12656 * The second half of the workaround is that mondo_recover_proc()
12804 12657 * checks to see if the tsb_info has the RELOC flag set, and if it
12805 12658 * does, it skips over that TSB without ever touching tsbinfop->tsb_va
12806 12659 * and hence avoiding the TLB miss that could result in a deadlock.
12807 12660 */
12808 12661 if (&sendmondo_in_recover) {
12809 12662 membar_enter(); /* make sure RELOC flag visible */
12810 12663 while (sendmondo_in_recover) {
12811 12664 drv_usecwait(1);
12812 12665 membar_consumer();
12813 12666 }
12814 12667 }
12815 12668
12816 12669 sfmmu_invalidate_ctx(sfmmup);
12817 12670 }
12818 12671
12819 12672 /* ARGSUSED */
12820 12673 static int
12821 12674 sfmmu_tsb_post_relocator(caddr_t va, uint_t tsbsz, uint_t flags,
12822 12675 void *tsbinfo, pfn_t newpfn)
12823 12676 {
12824 12677 hatlock_t *hatlockp;
12825 12678 struct tsb_info *tsbinfop = (struct tsb_info *)tsbinfo;
12826 12679 sfmmu_t *sfmmup = tsbinfop->tsb_sfmmu;
12827 12680
12828 12681 if (flags != HAT_POSTUNSUSPEND)
12829 12682 return (0);
12830 12683
12831 12684 hatlockp = sfmmu_hat_enter(sfmmup);
12832 12685
12833 12686 SFMMU_STAT(sf_tsb_reloc);
12834 12687
12835 12688 /*
12836 12689 * The process may have swapped out while we were relocating one
12837 12690 * of its TSBs. If so, don't bother doing the setup since the
12838 12691 * process can't be using the memory anymore.
12839 12692 */
12840 12693 if ((tsbinfop->tsb_flags & TSB_SWAPPED) == 0) {
12841 12694 ASSERT(va == tsbinfop->tsb_va);
12842 12695 sfmmu_tsbinfo_setup_phys(tsbinfop, newpfn);
12843 12696
12844 12697 if (tsbinfop->tsb_flags & TSB_FLUSH_NEEDED) {
12845 12698 sfmmu_inv_tsb(tsbinfop->tsb_va,
12846 12699 TSB_BYTES(tsbinfop->tsb_szc));
12847 12700 tsbinfop->tsb_flags &= ~TSB_FLUSH_NEEDED;
12848 12701 }
12849 12702 }
12850 12703
12851 12704 membar_exit();
12852 12705 tsbinfop->tsb_flags &= ~TSB_RELOC_FLAG;
12853 12706 cv_broadcast(&sfmmup->sfmmu_tsb_cv);
12854 12707
12855 12708 sfmmu_hat_exit(hatlockp);
12856 12709
12857 12710 return (0);
12858 12711 }
12859 12712
12860 12713 /*
12861 12714 * Allocate and initialize a tsb_info structure. Note that we may or may not
12862 12715 * allocate a TSB here, depending on the flags passed in.
12863 12716 */
12864 12717 static int
12865 12718 sfmmu_tsbinfo_alloc(struct tsb_info **tsbinfopp, int tsb_szc, int tte_sz_mask,
12866 12719 uint_t flags, sfmmu_t *sfmmup)
12867 12720 {
12868 12721 int err;
12869 12722
12870 12723 *tsbinfopp = (struct tsb_info *)kmem_cache_alloc(
12871 12724 sfmmu_tsbinfo_cache, KM_SLEEP);
12872 12725
12873 12726 if ((err = sfmmu_init_tsbinfo(*tsbinfopp, tte_sz_mask,
12874 12727 tsb_szc, flags, sfmmup)) != 0) {
12875 12728 kmem_cache_free(sfmmu_tsbinfo_cache, *tsbinfopp);
12876 12729 SFMMU_STAT(sf_tsb_allocfail);
12877 12730 *tsbinfopp = NULL;
12878 12731 return (err);
12879 12732 }
12880 12733 SFMMU_STAT(sf_tsb_alloc);
12881 12734
12882 12735 /*
12883 12736 * Bump the TSB size counters for this TSB size.
12884 12737 */
12885 12738 (*(((int *)&sfmmu_tsbsize_stat) + tsb_szc))++;
12886 12739 return (0);
12887 12740 }
12888 12741
12889 12742 static void
12890 12743 sfmmu_tsb_free(struct tsb_info *tsbinfo)
12891 12744 {
12892 12745 caddr_t tsbva = tsbinfo->tsb_va;
12893 12746 uint_t tsb_size = TSB_BYTES(tsbinfo->tsb_szc);
12894 12747 struct kmem_cache *kmem_cachep = tsbinfo->tsb_cache;
12895 12748 vmem_t *vmp = tsbinfo->tsb_vmp;
12896 12749
12897 12750 /*
12898 12751 * If we allocated this TSB from relocatable kernel memory, then we
12899 12752 * need to uninstall the callback handler.
12900 12753 */
12901 12754 if (tsbinfo->tsb_cache != sfmmu_tsb8k_cache) {
12902 12755 uintptr_t slab_mask;
12903 12756 caddr_t slab_vaddr;
12904 12757 page_t **ppl;
12905 12758 int ret;
12906 12759
12907 12760 ASSERT(tsb_size <= MMU_PAGESIZE4M || use_bigtsb_arena);
12908 12761 if (tsb_size > MMU_PAGESIZE4M)
12909 12762 slab_mask = ~((uintptr_t)bigtsb_slab_mask) << PAGESHIFT;
12910 12763 else
12911 12764 slab_mask = ~((uintptr_t)tsb_slab_mask) << PAGESHIFT;
12912 12765 slab_vaddr = (caddr_t)((uintptr_t)tsbva & slab_mask);
12913 12766
12914 12767 ret = as_pagelock(&kas, &ppl, slab_vaddr, PAGESIZE, S_WRITE);
12915 12768 ASSERT(ret == 0);
12916 12769 hat_delete_callback(tsbva, (uint_t)tsb_size, (void *)tsbinfo,
12917 12770 0, NULL);
12918 12771 as_pageunlock(&kas, ppl, slab_vaddr, PAGESIZE, S_WRITE);
12919 12772 }
12920 12773
12921 12774 if (kmem_cachep != NULL) {
12922 12775 kmem_cache_free(kmem_cachep, tsbva);
12923 12776 } else {
12924 12777 vmem_xfree(vmp, (void *)tsbva, tsb_size);
12925 12778 }
12926 12779 tsbinfo->tsb_va = (caddr_t)0xbad00bad;
12927 12780 atomic_add_64(&tsb_alloc_bytes, -(int64_t)tsb_size);
12928 12781 }
12929 12782
12930 12783 static void
12931 12784 sfmmu_tsbinfo_free(struct tsb_info *tsbinfo)
12932 12785 {
12933 12786 if ((tsbinfo->tsb_flags & TSB_SWAPPED) == 0) {
12934 12787 sfmmu_tsb_free(tsbinfo);
12935 12788 }
12936 12789 kmem_cache_free(sfmmu_tsbinfo_cache, tsbinfo);
12937 12790
12938 12791 }
12939 12792
12940 12793 /*
12941 12794 * Setup all the references to physical memory for this tsbinfo.
12942 12795 * The underlying page(s) must be locked.
12943 12796 */
12944 12797 static void
12945 12798 sfmmu_tsbinfo_setup_phys(struct tsb_info *tsbinfo, pfn_t pfn)
12946 12799 {
12947 12800 ASSERT(pfn != PFN_INVALID);
12948 12801 ASSERT(pfn == va_to_pfn(tsbinfo->tsb_va));
12949 12802
12950 12803 #ifndef sun4v
12951 12804 if (tsbinfo->tsb_szc == 0) {
12952 12805 sfmmu_memtte(&tsbinfo->tsb_tte, pfn,
12953 12806 PROT_WRITE|PROT_READ, TTE8K);
12954 12807 } else {
12955 12808 /*
12956 12809 * Round down PA and use a large mapping; the handlers will
12957 12810 * compute the TSB pointer at the correct offset into the
12958 12811 * big virtual page. NOTE: this assumes all TSBs larger
12959 12812 * than 8K must come from physically contiguous slabs of
12960 12813 * size tsb_slab_size.
12961 12814 */
12962 12815 sfmmu_memtte(&tsbinfo->tsb_tte, pfn & ~tsb_slab_mask,
12963 12816 PROT_WRITE|PROT_READ, tsb_slab_ttesz);
12964 12817 }
12965 12818 tsbinfo->tsb_pa = ptob(pfn);
12966 12819
12967 12820 TTE_SET_LOCKED(&tsbinfo->tsb_tte); /* lock the tte into dtlb */
12968 12821 TTE_SET_MOD(&tsbinfo->tsb_tte); /* enable writes */
12969 12822
12970 12823 ASSERT(TTE_IS_PRIVILEGED(&tsbinfo->tsb_tte));
12971 12824 ASSERT(TTE_IS_LOCKED(&tsbinfo->tsb_tte));
12972 12825 #else /* sun4v */
12973 12826 tsbinfo->tsb_pa = ptob(pfn);
12974 12827 #endif /* sun4v */
12975 12828 }
12976 12829
12977 12830
12978 12831 /*
12979 12832 * Returns zero on success, ENOMEM if over the high water mark,
12980 12833 * or EAGAIN if the caller needs to retry with a smaller TSB
12981 12834 * size (or specify TSB_FORCEALLOC if the allocation can't fail).
12982 12835 *
12983 12836 * This call cannot fail to allocate a TSB if TSB_FORCEALLOC
12984 12837 * is specified and the TSB requested is PAGESIZE, though it
12985 12838 * may sleep waiting for memory if sufficient memory is not
12986 12839 * available.
12987 12840 */
12988 12841 static int
12989 12842 sfmmu_init_tsbinfo(struct tsb_info *tsbinfo, int tteszmask,
12990 12843 int tsbcode, uint_t flags, sfmmu_t *sfmmup)
12991 12844 {
12992 12845 caddr_t vaddr = NULL;
12993 12846 caddr_t slab_vaddr;
12994 12847 uintptr_t slab_mask;
12995 12848 int tsbbytes = TSB_BYTES(tsbcode);
12996 12849 int lowmem = 0;
12997 12850 struct kmem_cache *kmem_cachep = NULL;
12998 12851 vmem_t *vmp = NULL;
12999 12852 lgrp_id_t lgrpid = LGRP_NONE;
13000 12853 pfn_t pfn;
13001 12854 uint_t cbflags = HAC_SLEEP;
13002 12855 page_t **pplist;
13003 12856 int ret;
13004 12857
13005 12858 ASSERT(tsbbytes <= MMU_PAGESIZE4M || use_bigtsb_arena);
13006 12859 if (tsbbytes > MMU_PAGESIZE4M)
13007 12860 slab_mask = ~((uintptr_t)bigtsb_slab_mask) << PAGESHIFT;
13008 12861 else
13009 12862 slab_mask = ~((uintptr_t)tsb_slab_mask) << PAGESHIFT;
13010 12863
13011 12864 if (flags & (TSB_FORCEALLOC | TSB_SWAPIN | TSB_GROW | TSB_SHRINK))
13012 12865 flags |= TSB_ALLOC;
13013 12866
13014 12867 ASSERT((flags & TSB_FORCEALLOC) == 0 || tsbcode == TSB_MIN_SZCODE);
13015 12868
13016 12869 tsbinfo->tsb_sfmmu = sfmmup;
13017 12870
13018 12871 /*
13019 12872 * If not allocating a TSB, set up the tsbinfo, set TSB_SWAPPED, and
13020 12873 * return.
13021 12874 */
13022 12875 if ((flags & TSB_ALLOC) == 0) {
13023 12876 tsbinfo->tsb_szc = tsbcode;
13024 12877 tsbinfo->tsb_ttesz_mask = tteszmask;
13025 12878 tsbinfo->tsb_va = (caddr_t)0xbadbadbeef;
13026 12879 tsbinfo->tsb_pa = -1;
13027 12880 tsbinfo->tsb_tte.ll = 0;
13028 12881 tsbinfo->tsb_next = NULL;
13029 12882 tsbinfo->tsb_flags = TSB_SWAPPED;
13030 12883 tsbinfo->tsb_cache = NULL;
13031 12884 tsbinfo->tsb_vmp = NULL;
13032 12885 return (0);
13033 12886 }
13034 12887
13035 12888 #ifdef DEBUG
13036 12889 /*
13037 12890 * For debugging:
13038 12891 * Randomly force allocation failures every tsb_alloc_mtbf
13039 12892 * tries if TSB_FORCEALLOC is not specified. This will
13040 12893 * return ENOMEM if tsb_alloc_mtbf is odd, or EAGAIN if
13041 12894 * it is even, to allow testing of both failure paths...
13042 12895 */
13043 12896 if (tsb_alloc_mtbf && ((flags & TSB_FORCEALLOC) == 0) &&
13044 12897 (tsb_alloc_count++ == tsb_alloc_mtbf)) {
13045 12898 tsb_alloc_count = 0;
13046 12899 tsb_alloc_fail_mtbf++;
13047 12900 return ((tsb_alloc_mtbf & 1)? ENOMEM : EAGAIN);
13048 12901 }
13049 12902 #endif /* DEBUG */
13050 12903
13051 12904 /*
13052 12905 * Enforce high water mark if we are not doing a forced allocation
13053 12906 * and are not shrinking a process' TSB.
13054 12907 */
13055 12908 if ((flags & TSB_SHRINK) == 0 &&
13056 12909 (tsbbytes + tsb_alloc_bytes) > tsb_alloc_hiwater) {
13057 12910 if ((flags & TSB_FORCEALLOC) == 0)
13058 12911 return (ENOMEM);
13059 12912 lowmem = 1;
13060 12913 }
13061 12914
13062 12915 /*
13063 12916 * Allocate from the correct location based upon the size of the TSB
13064 12917 * compared to the base page size, and what memory conditions dictate.
13065 12918 * Note we always do nonblocking allocations from the TSB arena since
13066 12919 * we don't want memory fragmentation to cause processes to block
13067 12920 * indefinitely waiting for memory; until the kernel algorithms that
13068 12921 * coalesce large pages are improved this is our best option.
13069 12922 *
13070 12923 * Algorithm:
13071 12924 * If allocating a "large" TSB (>8K), allocate from the
13072 12925 * appropriate kmem_tsb_default_arena vmem arena
13073 12926 * else if low on memory or the TSB_FORCEALLOC flag is set or
13074 12927 * tsb_forceheap is set
13075 12928 * Allocate from kernel heap via sfmmu_tsb8k_cache with
13076 12929 * KM_SLEEP (never fails)
13077 12930 * else
13078 12931 * Allocate from appropriate sfmmu_tsb_cache with
13079 12932 * KM_NOSLEEP
13080 12933 * endif
13081 12934 */
13082 12935 if (tsb_lgrp_affinity)
13083 12936 lgrpid = lgrp_home_id(curthread);
13084 12937 if (lgrpid == LGRP_NONE)
13085 12938 lgrpid = 0; /* use lgrp of boot CPU */
13086 12939
13087 12940 if (tsbbytes > MMU_PAGESIZE) {
13088 12941 if (tsbbytes > MMU_PAGESIZE4M) {
13089 12942 vmp = kmem_bigtsb_default_arena[lgrpid];
13090 12943 vaddr = (caddr_t)vmem_xalloc(vmp, tsbbytes, tsbbytes,
13091 12944 0, 0, NULL, NULL, VM_NOSLEEP);
13092 12945 } else {
13093 12946 vmp = kmem_tsb_default_arena[lgrpid];
13094 12947 vaddr = (caddr_t)vmem_xalloc(vmp, tsbbytes, tsbbytes,
13095 12948 0, 0, NULL, NULL, VM_NOSLEEP);
13096 12949 }
13097 12950 #ifdef DEBUG
13098 12951 } else if (lowmem || (flags & TSB_FORCEALLOC) || tsb_forceheap) {
13099 12952 #else /* !DEBUG */
13100 12953 } else if (lowmem || (flags & TSB_FORCEALLOC)) {
13101 12954 #endif /* DEBUG */
13102 12955 kmem_cachep = sfmmu_tsb8k_cache;
13103 12956 vaddr = (caddr_t)kmem_cache_alloc(kmem_cachep, KM_SLEEP);
13104 12957 ASSERT(vaddr != NULL);
13105 12958 } else {
13106 12959 kmem_cachep = sfmmu_tsb_cache[lgrpid];
13107 12960 vaddr = (caddr_t)kmem_cache_alloc(kmem_cachep, KM_NOSLEEP);
13108 12961 }
13109 12962
13110 12963 tsbinfo->tsb_cache = kmem_cachep;
13111 12964 tsbinfo->tsb_vmp = vmp;
13112 12965
13113 12966 if (vaddr == NULL) {
13114 12967 return (EAGAIN);
13115 12968 }
13116 12969
13117 12970 atomic_add_64(&tsb_alloc_bytes, (int64_t)tsbbytes);
13118 12971 kmem_cachep = tsbinfo->tsb_cache;
13119 12972
13120 12973 /*
13121 12974 * If we are allocating from outside the cage, then we need to
13122 12975 * register a relocation callback handler. Note that for now
13123 12976 * since pseudo mappings always hang off of the slab's root page,
13124 12977 * we need only lock the first 8K of the TSB slab. This is a bit
13125 12978 * hacky but it is good for performance.
13126 12979 */
13127 12980 if (kmem_cachep != sfmmu_tsb8k_cache) {
13128 12981 slab_vaddr = (caddr_t)((uintptr_t)vaddr & slab_mask);
13129 12982 ret = as_pagelock(&kas, &pplist, slab_vaddr, PAGESIZE, S_WRITE);
13130 12983 ASSERT(ret == 0);
13131 12984 ret = hat_add_callback(sfmmu_tsb_cb_id, vaddr, (uint_t)tsbbytes,
13132 12985 cbflags, (void *)tsbinfo, &pfn, NULL);
13133 12986
13134 12987 /*
13135 12988 * Need to free up resources if we could not successfully
13136 12989 * add the callback function and return an error condition.
13137 12990 */
13138 12991 if (ret != 0) {
13139 12992 if (kmem_cachep) {
13140 12993 kmem_cache_free(kmem_cachep, vaddr);
13141 12994 } else {
13142 12995 vmem_xfree(vmp, (void *)vaddr, tsbbytes);
13143 12996 }
13144 12997 as_pageunlock(&kas, pplist, slab_vaddr, PAGESIZE,
13145 12998 S_WRITE);
13146 12999 return (EAGAIN);
13147 13000 }
13148 13001 } else {
13149 13002 /*
13150 13003 * Since allocation of 8K TSBs from heap is rare and occurs
13151 13004 * during memory pressure we allocate them from permanent
13152 13005 * memory rather than using callbacks to get the PFN.
13153 13006 */
13154 13007 pfn = hat_getpfnum(kas.a_hat, vaddr);
13155 13008 }
13156 13009
13157 13010 tsbinfo->tsb_va = vaddr;
13158 13011 tsbinfo->tsb_szc = tsbcode;
13159 13012 tsbinfo->tsb_ttesz_mask = tteszmask;
13160 13013 tsbinfo->tsb_next = NULL;
13161 13014 tsbinfo->tsb_flags = 0;
13162 13015
13163 13016 sfmmu_tsbinfo_setup_phys(tsbinfo, pfn);
13164 13017
13165 13018 sfmmu_inv_tsb(vaddr, tsbbytes);
13166 13019
13167 13020 if (kmem_cachep != sfmmu_tsb8k_cache) {
13168 13021 as_pageunlock(&kas, pplist, slab_vaddr, PAGESIZE, S_WRITE);
13169 13022 }
13170 13023
13171 13024 return (0);
13172 13025 }
13173 13026
13174 13027 /*
13175 13028 * Initialize per cpu tsb and per cpu tsbmiss_area
13176 13029 */
13177 13030 void
13178 13031 sfmmu_init_tsbs(void)
13179 13032 {
13180 13033 int i;
13181 13034 struct tsbmiss *tsbmissp;
13182 13035 struct kpmtsbm *kpmtsbmp;
13183 13036 #ifndef sun4v
13184 13037 extern int dcache_line_mask;
13185 13038 #endif /* sun4v */
13186 13039 extern uint_t vac_colors;
13187 13040
13188 13041 /*
13189 13042 * Init. tsb miss area.
13190 13043 */
13191 13044 tsbmissp = tsbmiss_area;
13192 13045
13193 13046 for (i = 0; i < NCPU; tsbmissp++, i++) {
13194 13047 /*
13195 13048 * initialize the tsbmiss area.
13196 13049 * Do this for all possible CPUs as some may be added
13197 13050 * while the system is running. There is no cost to this.
13198 13051 */
13199 13052 tsbmissp->ksfmmup = ksfmmup;
13200 13053 #ifndef sun4v
13201 13054 tsbmissp->dcache_line_mask = (uint16_t)dcache_line_mask;
13202 13055 #endif /* sun4v */
13203 13056 tsbmissp->khashstart =
13204 13057 (struct hmehash_bucket *)va_to_pa((caddr_t)khme_hash);
13205 13058 tsbmissp->uhashstart =
13206 13059 (struct hmehash_bucket *)va_to_pa((caddr_t)uhme_hash);
13207 13060 tsbmissp->khashsz = khmehash_num;
13208 13061 tsbmissp->uhashsz = uhmehash_num;
13209 13062 }
13210 13063
13211 13064 sfmmu_tsb_cb_id = hat_register_callback('T'<<16 | 'S' << 8 | 'B',
13212 13065 sfmmu_tsb_pre_relocator, sfmmu_tsb_post_relocator, NULL, 0);
13213 13066
13214 13067 if (kpm_enable == 0)
13215 13068 return;
13216 13069
13217 13070 /* -- Begin KPM specific init -- */
13218 13071
13219 13072 if (kpm_smallpages) {
13220 13073 /*
13221 13074 * If we're using base pagesize pages for seg_kpm
13222 13075 * mappings, we use the kernel TSB since we can't afford
13223 13076 * to allocate a second huge TSB for these mappings.
13224 13077 */
13225 13078 kpm_tsbbase = ktsb_phys? ktsb_pbase : (uint64_t)ktsb_base;
13226 13079 kpm_tsbsz = ktsb_szcode;
13227 13080 kpmsm_tsbbase = kpm_tsbbase;
13228 13081 kpmsm_tsbsz = kpm_tsbsz;
13229 13082 } else {
13230 13083 /*
13231 13084 * In VAC conflict case, just put the entries in the
13232 13085 * kernel 8K indexed TSB for now so we can find them.
13233 13086 * This could really be changed in the future if we feel
13234 13087 * the need...
13235 13088 */
13236 13089 kpmsm_tsbbase = ktsb_phys? ktsb_pbase : (uint64_t)ktsb_base;
13237 13090 kpmsm_tsbsz = ktsb_szcode;
13238 13091 kpm_tsbbase = ktsb_phys? ktsb4m_pbase : (uint64_t)ktsb4m_base;
13239 13092 kpm_tsbsz = ktsb4m_szcode;
13240 13093 }
13241 13094
13242 13095 kpmtsbmp = kpmtsbm_area;
13243 13096 for (i = 0; i < NCPU; kpmtsbmp++, i++) {
13244 13097 /*
13245 13098 * Initialize the kpmtsbm area.
13246 13099 * Do this for all possible CPUs as some may be added
13247 13100 * while the system is running. There is no cost to this.
13248 13101 */
13249 13102 kpmtsbmp->vbase = kpm_vbase;
13250 13103 kpmtsbmp->vend = kpm_vbase + kpm_size * vac_colors;
13251 13104 kpmtsbmp->sz_shift = kpm_size_shift;
13252 13105 kpmtsbmp->kpmp_shift = kpmp_shift;
13253 13106 kpmtsbmp->kpmp2pshft = (uchar_t)kpmp2pshft;
13254 13107 if (kpm_smallpages == 0) {
13255 13108 kpmtsbmp->kpmp_table_sz = kpmp_table_sz;
13256 13109 kpmtsbmp->kpmp_tablepa = va_to_pa(kpmp_table);
13257 13110 } else {
13258 13111 kpmtsbmp->kpmp_table_sz = kpmp_stable_sz;
13259 13112 kpmtsbmp->kpmp_tablepa = va_to_pa(kpmp_stable);
13260 13113 }
13261 13114 kpmtsbmp->msegphashpa = va_to_pa(memseg_phash);
13262 13115 kpmtsbmp->flags = KPMTSBM_ENABLE_FLAG;
13263 13116 #ifdef DEBUG
13264 13117 kpmtsbmp->flags |= (kpm_tsbmtl) ? KPMTSBM_TLTSBM_FLAG : 0;
13265 13118 #endif /* DEBUG */
13266 13119 if (ktsb_phys)
13267 13120 kpmtsbmp->flags |= KPMTSBM_TSBPHYS_FLAG;
13268 13121 }
13269 13122
13270 13123 /* -- End KPM specific init -- */
13271 13124 }
13272 13125
13273 13126 /* Avoid using sfmmu_tsbinfo_alloc() to avoid kmem_alloc - no real reason */
13274 13127 struct tsb_info ktsb_info[2];
13275 13128
13276 13129 /*
13277 13130 * Called from hat_kern_setup() to setup the tsb_info for ksfmmup.
13278 13131 */
13279 13132 void
13280 13133 sfmmu_init_ktsbinfo()
13281 13134 {
13282 13135 ASSERT(ksfmmup != NULL);
13283 13136 ASSERT(ksfmmup->sfmmu_tsb == NULL);
13284 13137 /*
13285 13138 * Allocate tsbinfos for kernel and copy in data
13286 13139 * to make debug easier and sun4v setup easier.
13287 13140 */
13288 13141 ktsb_info[0].tsb_sfmmu = ksfmmup;
13289 13142 ktsb_info[0].tsb_szc = ktsb_szcode;
13290 13143 ktsb_info[0].tsb_ttesz_mask = TSB8K|TSB64K|TSB512K;
13291 13144 ktsb_info[0].tsb_va = ktsb_base;
13292 13145 ktsb_info[0].tsb_pa = ktsb_pbase;
13293 13146 ktsb_info[0].tsb_flags = 0;
13294 13147 ktsb_info[0].tsb_tte.ll = 0;
13295 13148 ktsb_info[0].tsb_cache = NULL;
13296 13149
13297 13150 ktsb_info[1].tsb_sfmmu = ksfmmup;
13298 13151 ktsb_info[1].tsb_szc = ktsb4m_szcode;
13299 13152 ktsb_info[1].tsb_ttesz_mask = TSB4M;
13300 13153 ktsb_info[1].tsb_va = ktsb4m_base;
13301 13154 ktsb_info[1].tsb_pa = ktsb4m_pbase;
13302 13155 ktsb_info[1].tsb_flags = 0;
13303 13156 ktsb_info[1].tsb_tte.ll = 0;
13304 13157 ktsb_info[1].tsb_cache = NULL;
13305 13158
13306 13159 /* Link them into ksfmmup. */
13307 13160 ktsb_info[0].tsb_next = &ktsb_info[1];
13308 13161 ktsb_info[1].tsb_next = NULL;
13309 13162 ksfmmup->sfmmu_tsb = &ktsb_info[0];
13310 13163
13311 13164 sfmmu_setup_tsbinfo(ksfmmup);
13312 13165 }
13313 13166
13314 13167 /*
13315 13168 * Cache the last value returned from va_to_pa(). If the VA specified
13316 13169 * in the current call to cached_va_to_pa() maps to the same Page (as the
13317 13170 * previous call to cached_va_to_pa()), then compute the PA using
13318 13171 * cached info, else call va_to_pa().
13319 13172 *
13320 13173 * Note: this function is neither MT-safe nor consistent in the presence
13321 13174 * of multiple, interleaved threads. This function was created to enable
13322 13175 * an optimization used during boot (at a point when there's only one thread
13323 13176 * executing on the "boot CPU", and before startup_vm() has been called).
13324 13177 */
13325 13178 static uint64_t
13326 13179 cached_va_to_pa(void *vaddr)
13327 13180 {
13328 13181 static uint64_t prev_vaddr_base = 0;
13329 13182 static uint64_t prev_pfn = 0;
13330 13183
13331 13184 if ((((uint64_t)vaddr) & MMU_PAGEMASK) == prev_vaddr_base) {
13332 13185 return (prev_pfn | ((uint64_t)vaddr & MMU_PAGEOFFSET));
13333 13186 } else {
13334 13187 uint64_t pa = va_to_pa(vaddr);
13335 13188
13336 13189 if (pa != ((uint64_t)-1)) {
13337 13190 /*
13338 13191 * Computed physical address is valid. Cache its
13339 13192 * related info for the next cached_va_to_pa() call.
13340 13193 */
13341 13194 prev_pfn = pa & MMU_PAGEMASK;
13342 13195 prev_vaddr_base = ((uint64_t)vaddr) & MMU_PAGEMASK;
13343 13196 }
13344 13197
13345 13198 return (pa);
13346 13199 }
13347 13200 }
13348 13201
13349 13202 /*
13350 13203 * Carve up our nucleus hblk region. We may allocate more hblks than
13351 13204 * asked due to rounding errors but we are guaranteed to have at least
13352 13205 * enough space to allocate the requested number of hblk8's and hblk1's.
13353 13206 */
13354 13207 void
13355 13208 sfmmu_init_nucleus_hblks(caddr_t addr, size_t size, int nhblk8, int nhblk1)
13356 13209 {
13357 13210 struct hme_blk *hmeblkp;
13358 13211 size_t hme8blk_sz, hme1blk_sz;
13359 13212 size_t i;
13360 13213 size_t hblk8_bound;
13361 13214 ulong_t j = 0, k = 0;
13362 13215
13363 13216 ASSERT(addr != NULL && size != 0);
13364 13217
13365 13218 /* Need to use proper structure alignment */
13366 13219 hme8blk_sz = roundup(HME8BLK_SZ, sizeof (int64_t));
13367 13220 hme1blk_sz = roundup(HME1BLK_SZ, sizeof (int64_t));
13368 13221
13369 13222 nucleus_hblk8.list = (void *)addr;
13370 13223 nucleus_hblk8.index = 0;
13371 13224
13372 13225 /*
13373 13226 * Use as much memory as possible for hblk8's since we
13374 13227 * expect all bop_alloc'ed memory to be allocated in 8k chunks.
13375 13228 * We need to hold back enough space for the hblk1's which
13376 13229 * we'll allocate next.
13377 13230 */
13378 13231 hblk8_bound = size - (nhblk1 * hme1blk_sz) - hme8blk_sz;
13379 13232 for (i = 0; i <= hblk8_bound; i += hme8blk_sz, j++) {
13380 13233 hmeblkp = (struct hme_blk *)addr;
13381 13234 addr += hme8blk_sz;
13382 13235 hmeblkp->hblk_nuc_bit = 1;
13383 13236 hmeblkp->hblk_nextpa = cached_va_to_pa((caddr_t)hmeblkp);
13384 13237 }
13385 13238 nucleus_hblk8.len = j;
13386 13239 ASSERT(j >= nhblk8);
13387 13240 SFMMU_STAT_ADD(sf_hblk8_ncreate, j);
13388 13241
13389 13242 nucleus_hblk1.list = (void *)addr;
13390 13243 nucleus_hblk1.index = 0;
13391 13244 for (; i <= (size - hme1blk_sz); i += hme1blk_sz, k++) {
13392 13245 hmeblkp = (struct hme_blk *)addr;
13393 13246 addr += hme1blk_sz;
13394 13247 hmeblkp->hblk_nuc_bit = 1;
13395 13248 hmeblkp->hblk_nextpa = cached_va_to_pa((caddr_t)hmeblkp);
13396 13249 }
13397 13250 ASSERT(k >= nhblk1);
13398 13251 nucleus_hblk1.len = k;
13399 13252 SFMMU_STAT_ADD(sf_hblk1_ncreate, k);
13400 13253 }
13401 13254
13402 13255 /*
13403 13256 * This function is currently not supported on this platform. For what
13404 13257 * it's supposed to do, see hat.c and hat_srmmu.c
13405 13258 */
13406 13259 /* ARGSUSED */
13407 13260 faultcode_t
13408 13261 hat_softlock(struct hat *hat, caddr_t addr, size_t *lenp, page_t **ppp,
13409 13262 uint_t flags)
13410 13263 {
13411 13264 ASSERT(hat->sfmmu_xhat_provider == NULL);
13412 13265 return (FC_NOSUPPORT);
13413 13266 }
13414 13267
13415 13268 /*
13416 13269 * Searchs the mapping list of the page for a mapping of the same size. If not
13417 13270 * found the corresponding bit is cleared in the p_index field. When large
13418 13271 * pages are more prevalent in the system, we can maintain the mapping list
13419 13272 * in order and we don't have to traverse the list each time. Just check the
13420 13273 * next and prev entries, and if both are of different size, we clear the bit.
13421 13274 */
13422 13275 static void
13423 13276 sfmmu_rm_large_mappings(page_t *pp, int ttesz)
13424 13277 {
13425 13278 struct sf_hment *sfhmep;
13426 13279 struct hme_blk *hmeblkp;
13427 13280 int index;
13428 13281 pgcnt_t npgs;
13429 13282
13430 13283 ASSERT(ttesz > TTE8K);
13431 13284
13432 13285 ASSERT(sfmmu_mlist_held(pp));
13433 13286
13434 13287 ASSERT(PP_ISMAPPED_LARGE(pp));
13435 13288
13436 13289 /*
13437 13290 * Traverse mapping list looking for another mapping of same size.
13438 13291 * since we only want to clear index field if all mappings of
13439 13292 * that size are gone.
13440 13293 */
13441 13294
13442 13295 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) {
13443 13296 if (IS_PAHME(sfhmep))
13444 13297 continue;
13445 13298 hmeblkp = sfmmu_hmetohblk(sfhmep);
13446 13299 if (hmeblkp->hblk_xhat_bit)
13447 13300 continue;
13448 13301 if (hme_size(sfhmep) == ttesz) {
13449 13302 /*
13450 13303 * another mapping of the same size. don't clear index.
13451 13304 */
13452 13305 return;
13453 13306 }
13454 13307 }
13455 13308
13456 13309 /*
13457 13310 * Clear the p_index bit for large page.
13458 13311 */
13459 13312 index = PAGESZ_TO_INDEX(ttesz);
13460 13313 npgs = TTEPAGES(ttesz);
13461 13314 while (npgs-- > 0) {
13462 13315 ASSERT(pp->p_index & index);
13463 13316 pp->p_index &= ~index;
13464 13317 pp = PP_PAGENEXT(pp);
13465 13318 }
13466 13319 }
13467 13320
13468 13321 /*
13469 13322 * return supported features
13470 13323 */
13471 13324 /* ARGSUSED */
13472 13325 int
13473 13326 hat_supported(enum hat_features feature, void *arg)
13474 13327 {
13475 13328 switch (feature) {
13476 13329 case HAT_SHARED_PT:
13477 13330 case HAT_DYNAMIC_ISM_UNMAP:
13478 13331 case HAT_VMODSORT:
13479 13332 return (1);
13480 13333 case HAT_SHARED_REGIONS:
13481 13334 if (shctx_on)
13482 13335 return (1);
13483 13336 else
13484 13337 return (0);
13485 13338 default:
13486 13339 return (0);
13487 13340 }
13488 13341 }
13489 13342
13490 13343 void
13491 13344 hat_enter(struct hat *hat)
13492 13345 {
13493 13346 hatlock_t *hatlockp;
13494 13347
13495 13348 if (hat != ksfmmup) {
13496 13349 hatlockp = TSB_HASH(hat);
13497 13350 mutex_enter(HATLOCK_MUTEXP(hatlockp));
13498 13351 }
13499 13352 }
13500 13353
13501 13354 void
13502 13355 hat_exit(struct hat *hat)
13503 13356 {
13504 13357 hatlock_t *hatlockp;
13505 13358
13506 13359 if (hat != ksfmmup) {
13507 13360 hatlockp = TSB_HASH(hat);
13508 13361 mutex_exit(HATLOCK_MUTEXP(hatlockp));
13509 13362 }
13510 13363 }
13511 13364
13512 13365 /*ARGSUSED*/
13513 13366 void
13514 13367 hat_reserve(struct as *as, caddr_t addr, size_t len)
13515 13368 {
13516 13369 }
13517 13370
13518 13371 static void
13519 13372 hat_kstat_init(void)
13520 13373 {
13521 13374 kstat_t *ksp;
13522 13375
13523 13376 ksp = kstat_create("unix", 0, "sfmmu_global_stat", "hat",
13524 13377 KSTAT_TYPE_RAW, sizeof (struct sfmmu_global_stat),
13525 13378 KSTAT_FLAG_VIRTUAL);
13526 13379 if (ksp) {
13527 13380 ksp->ks_data = (void *) &sfmmu_global_stat;
13528 13381 kstat_install(ksp);
13529 13382 }
13530 13383 ksp = kstat_create("unix", 0, "sfmmu_tsbsize_stat", "hat",
13531 13384 KSTAT_TYPE_RAW, sizeof (struct sfmmu_tsbsize_stat),
13532 13385 KSTAT_FLAG_VIRTUAL);
13533 13386 if (ksp) {
13534 13387 ksp->ks_data = (void *) &sfmmu_tsbsize_stat;
13535 13388 kstat_install(ksp);
13536 13389 }
13537 13390 ksp = kstat_create("unix", 0, "sfmmu_percpu_stat", "hat",
13538 13391 KSTAT_TYPE_RAW, sizeof (struct sfmmu_percpu_stat) * NCPU,
13539 13392 KSTAT_FLAG_WRITABLE);
13540 13393 if (ksp) {
13541 13394 ksp->ks_update = sfmmu_kstat_percpu_update;
13542 13395 kstat_install(ksp);
13543 13396 }
13544 13397 }
13545 13398
13546 13399 /* ARGSUSED */
13547 13400 static int
13548 13401 sfmmu_kstat_percpu_update(kstat_t *ksp, int rw)
13549 13402 {
13550 13403 struct sfmmu_percpu_stat *cpu_kstat = ksp->ks_data;
13551 13404 struct tsbmiss *tsbm = tsbmiss_area;
13552 13405 struct kpmtsbm *kpmtsbm = kpmtsbm_area;
13553 13406 int i;
13554 13407
13555 13408 ASSERT(cpu_kstat);
13556 13409 if (rw == KSTAT_READ) {
13557 13410 for (i = 0; i < NCPU; cpu_kstat++, tsbm++, kpmtsbm++, i++) {
13558 13411 cpu_kstat->sf_itlb_misses = 0;
13559 13412 cpu_kstat->sf_dtlb_misses = 0;
13560 13413 cpu_kstat->sf_utsb_misses = tsbm->utsb_misses -
13561 13414 tsbm->uprot_traps;
13562 13415 cpu_kstat->sf_ktsb_misses = tsbm->ktsb_misses +
13563 13416 kpmtsbm->kpm_tsb_misses - tsbm->kprot_traps;
13564 13417 cpu_kstat->sf_tsb_hits = 0;
13565 13418 cpu_kstat->sf_umod_faults = tsbm->uprot_traps;
13566 13419 cpu_kstat->sf_kmod_faults = tsbm->kprot_traps;
13567 13420 }
13568 13421 } else {
13569 13422 /* KSTAT_WRITE is used to clear stats */
13570 13423 for (i = 0; i < NCPU; tsbm++, kpmtsbm++, i++) {
13571 13424 tsbm->utsb_misses = 0;
13572 13425 tsbm->ktsb_misses = 0;
13573 13426 tsbm->uprot_traps = 0;
13574 13427 tsbm->kprot_traps = 0;
13575 13428 kpmtsbm->kpm_dtlb_misses = 0;
13576 13429 kpmtsbm->kpm_tsb_misses = 0;
13577 13430 }
13578 13431 }
13579 13432 return (0);
13580 13433 }
13581 13434
13582 13435 #ifdef DEBUG
13583 13436
13584 13437 tte_t *gorig[NCPU], *gcur[NCPU], *gnew[NCPU];
13585 13438
13586 13439 /*
13587 13440 * A tte checker. *orig_old is the value we read before cas.
13588 13441 * *cur is the value returned by cas.
13589 13442 * *new is the desired value when we do the cas.
13590 13443 *
13591 13444 * *hmeblkp is currently unused.
13592 13445 */
13593 13446
13594 13447 /* ARGSUSED */
13595 13448 void
13596 13449 chk_tte(tte_t *orig_old, tte_t *cur, tte_t *new, struct hme_blk *hmeblkp)
13597 13450 {
13598 13451 pfn_t i, j, k;
13599 13452 int cpuid = CPU->cpu_id;
13600 13453
13601 13454 gorig[cpuid] = orig_old;
13602 13455 gcur[cpuid] = cur;
13603 13456 gnew[cpuid] = new;
13604 13457
13605 13458 #ifdef lint
13606 13459 hmeblkp = hmeblkp;
13607 13460 #endif
13608 13461
13609 13462 if (TTE_IS_VALID(orig_old)) {
13610 13463 if (TTE_IS_VALID(cur)) {
13611 13464 i = TTE_TO_TTEPFN(orig_old);
13612 13465 j = TTE_TO_TTEPFN(cur);
13613 13466 k = TTE_TO_TTEPFN(new);
13614 13467 if (i != j) {
13615 13468 /* remap error? */
13616 13469 panic("chk_tte: bad pfn, 0x%lx, 0x%lx", i, j);
13617 13470 }
13618 13471
13619 13472 if (i != k) {
13620 13473 /* remap error? */
13621 13474 panic("chk_tte: bad pfn2, 0x%lx, 0x%lx", i, k);
13622 13475 }
13623 13476 } else {
13624 13477 if (TTE_IS_VALID(new)) {
13625 13478 panic("chk_tte: invalid cur? ");
13626 13479 }
13627 13480
13628 13481 i = TTE_TO_TTEPFN(orig_old);
13629 13482 k = TTE_TO_TTEPFN(new);
13630 13483 if (i != k) {
13631 13484 panic("chk_tte: bad pfn3, 0x%lx, 0x%lx", i, k);
13632 13485 }
13633 13486 }
13634 13487 } else {
13635 13488 if (TTE_IS_VALID(cur)) {
13636 13489 j = TTE_TO_TTEPFN(cur);
13637 13490 if (TTE_IS_VALID(new)) {
13638 13491 k = TTE_TO_TTEPFN(new);
13639 13492 if (j != k) {
13640 13493 panic("chk_tte: bad pfn4, 0x%lx, 0x%lx",
13641 13494 j, k);
13642 13495 }
13643 13496 } else {
13644 13497 panic("chk_tte: why here?");
13645 13498 }
13646 13499 } else {
13647 13500 if (!TTE_IS_VALID(new)) {
13648 13501 panic("chk_tte: why here2 ?");
13649 13502 }
13650 13503 }
13651 13504 }
13652 13505 }
13653 13506
13654 13507 #endif /* DEBUG */
13655 13508
13656 13509 extern void prefetch_tsbe_read(struct tsbe *);
13657 13510 extern void prefetch_tsbe_write(struct tsbe *);
13658 13511
13659 13512
13660 13513 /*
13661 13514 * We want to prefetch 7 cache lines ahead for our read prefetch. This gives
13662 13515 * us optimal performance on Cheetah+. You can only have 8 outstanding
13663 13516 * prefetches at any one time, so we opted for 7 read prefetches and 1 write
13664 13517 * prefetch to make the most utilization of the prefetch capability.
13665 13518 */
13666 13519 #define TSBE_PREFETCH_STRIDE (7)
13667 13520
13668 13521 void
13669 13522 sfmmu_copy_tsb(struct tsb_info *old_tsbinfo, struct tsb_info *new_tsbinfo)
13670 13523 {
13671 13524 int old_bytes = TSB_BYTES(old_tsbinfo->tsb_szc);
13672 13525 int new_bytes = TSB_BYTES(new_tsbinfo->tsb_szc);
13673 13526 int old_entries = TSB_ENTRIES(old_tsbinfo->tsb_szc);
13674 13527 int new_entries = TSB_ENTRIES(new_tsbinfo->tsb_szc);
13675 13528 struct tsbe *old;
13676 13529 struct tsbe *new;
13677 13530 struct tsbe *new_base = (struct tsbe *)new_tsbinfo->tsb_va;
13678 13531 uint64_t va;
13679 13532 int new_offset;
13680 13533 int i;
13681 13534 int vpshift;
13682 13535 int last_prefetch;
13683 13536
13684 13537 if (old_bytes == new_bytes) {
13685 13538 bcopy(old_tsbinfo->tsb_va, new_tsbinfo->tsb_va, new_bytes);
13686 13539 } else {
13687 13540
13688 13541 /*
13689 13542 * A TSBE is 16 bytes which means there are four TSBE's per
13690 13543 * P$ line (64 bytes), thus every 4 TSBE's we prefetch.
13691 13544 */
13692 13545 old = (struct tsbe *)old_tsbinfo->tsb_va;
13693 13546 last_prefetch = old_entries - (4*(TSBE_PREFETCH_STRIDE+1));
13694 13547 for (i = 0; i < old_entries; i++, old++) {
13695 13548 if (((i & (4-1)) == 0) && (i < last_prefetch))
13696 13549 prefetch_tsbe_read(old);
13697 13550 if (!old->tte_tag.tag_invalid) {
13698 13551 /*
13699 13552 * We have a valid TTE to remap. Check the
13700 13553 * size. We won't remap 64K or 512K TTEs
13701 13554 * because they span more than one TSB entry
13702 13555 * and are indexed using an 8K virt. page.
13703 13556 * Ditto for 32M and 256M TTEs.
13704 13557 */
13705 13558 if (TTE_CSZ(&old->tte_data) == TTE64K ||
13706 13559 TTE_CSZ(&old->tte_data) == TTE512K)
13707 13560 continue;
13708 13561 if (mmu_page_sizes == max_mmu_page_sizes) {
13709 13562 if (TTE_CSZ(&old->tte_data) == TTE32M ||
13710 13563 TTE_CSZ(&old->tte_data) == TTE256M)
13711 13564 continue;
13712 13565 }
13713 13566
13714 13567 /* clear the lower 22 bits of the va */
13715 13568 va = *(uint64_t *)old << 22;
13716 13569 /* turn va into a virtual pfn */
13717 13570 va >>= 22 - TSB_START_SIZE;
13718 13571 /*
13719 13572 * or in bits from the offset in the tsb
13720 13573 * to get the real virtual pfn. These
13721 13574 * correspond to bits [21:13] in the va
13722 13575 */
13723 13576 vpshift =
13724 13577 TTE_BSZS_SHIFT(TTE_CSZ(&old->tte_data)) &
13725 13578 0x1ff;
13726 13579 va |= (i << vpshift);
13727 13580 va >>= vpshift;
13728 13581 new_offset = va & (new_entries - 1);
13729 13582 new = new_base + new_offset;
13730 13583 prefetch_tsbe_write(new);
13731 13584 *new = *old;
13732 13585 }
13733 13586 }
13734 13587 }
13735 13588 }
13736 13589
13737 13590 /*
13738 13591 * unused in sfmmu
13739 13592 */
13740 13593 void
13741 13594 hat_dump(void)
13742 13595 {
13743 13596 }
13744 13597
13745 13598 /*
13746 13599 * Called when a thread is exiting and we have switched to the kernel address
13747 13600 * space. Perform the same VM initialization resume() uses when switching
13748 13601 * processes.
13749 13602 *
13750 13603 * Note that sfmmu_load_mmustate() is currently a no-op for kernel threads, but
13751 13604 * we call it anyway in case the semantics change in the future.
13752 13605 */
13753 13606 /*ARGSUSED*/
13754 13607 void
13755 13608 hat_thread_exit(kthread_t *thd)
13756 13609 {
13757 13610 uint_t pgsz_cnum;
13758 13611 uint_t pstate_save;
13759 13612
13760 13613 ASSERT(thd->t_procp->p_as == &kas);
13761 13614
13762 13615 pgsz_cnum = KCONTEXT;
13763 13616 #ifdef sun4u
13764 13617 pgsz_cnum |= (ksfmmup->sfmmu_cext << CTXREG_EXT_SHIFT);
13765 13618 #endif
13766 13619
13767 13620 /*
13768 13621 * Note that sfmmu_load_mmustate() is currently a no-op for
13769 13622 * kernel threads. We need to disable interrupts here,
13770 13623 * simply because otherwise sfmmu_load_mmustate() would panic
13771 13624 * if the caller does not disable interrupts.
13772 13625 */
13773 13626 pstate_save = sfmmu_disable_intrs();
13774 13627
13775 13628 /* Compatibility Note: hw takes care of MMU_SCONTEXT1 */
13776 13629 sfmmu_setctx_sec(pgsz_cnum);
13777 13630 sfmmu_load_mmustate(ksfmmup);
13778 13631 sfmmu_enable_intrs(pstate_save);
13779 13632 }
13780 13633
13781 13634
13782 13635 /*
13783 13636 * SRD support
13784 13637 */
13785 13638 #define SRD_HASH_FUNCTION(vp) (((((uintptr_t)(vp)) >> 4) ^ \
13786 13639 (((uintptr_t)(vp)) >> 11)) & \
13787 13640 srd_hashmask)
13788 13641
13789 13642 /*
13790 13643 * Attach the process to the srd struct associated with the exec vnode
13791 13644 * from which the process is started.
13792 13645 */
13793 13646 void
13794 13647 hat_join_srd(struct hat *sfmmup, vnode_t *evp)
13795 13648 {
13796 13649 uint_t hash = SRD_HASH_FUNCTION(evp);
13797 13650 sf_srd_t *srdp;
13798 13651 sf_srd_t *newsrdp;
13799 13652
13800 13653 ASSERT(sfmmup != ksfmmup);
13801 13654 ASSERT(sfmmup->sfmmu_srdp == NULL);
13802 13655
13803 13656 if (!shctx_on) {
13804 13657 return;
13805 13658 }
13806 13659
13807 13660 VN_HOLD(evp);
13808 13661
13809 13662 if (srd_buckets[hash].srdb_srdp != NULL) {
13810 13663 mutex_enter(&srd_buckets[hash].srdb_lock);
13811 13664 for (srdp = srd_buckets[hash].srdb_srdp; srdp != NULL;
13812 13665 srdp = srdp->srd_hash) {
13813 13666 if (srdp->srd_evp == evp) {
13814 13667 ASSERT(srdp->srd_refcnt >= 0);
13815 13668 sfmmup->sfmmu_srdp = srdp;
13816 13669 atomic_add_32(
13817 13670 (volatile uint_t *)&srdp->srd_refcnt, 1);
13818 13671 mutex_exit(&srd_buckets[hash].srdb_lock);
13819 13672 return;
13820 13673 }
13821 13674 }
13822 13675 mutex_exit(&srd_buckets[hash].srdb_lock);
13823 13676 }
13824 13677 newsrdp = kmem_cache_alloc(srd_cache, KM_SLEEP);
13825 13678 ASSERT(newsrdp->srd_next_ismrid == 0 && newsrdp->srd_next_hmerid == 0);
13826 13679
13827 13680 newsrdp->srd_evp = evp;
13828 13681 newsrdp->srd_refcnt = 1;
13829 13682 newsrdp->srd_hmergnfree = NULL;
13830 13683 newsrdp->srd_ismrgnfree = NULL;
13831 13684
13832 13685 mutex_enter(&srd_buckets[hash].srdb_lock);
13833 13686 for (srdp = srd_buckets[hash].srdb_srdp; srdp != NULL;
13834 13687 srdp = srdp->srd_hash) {
13835 13688 if (srdp->srd_evp == evp) {
13836 13689 ASSERT(srdp->srd_refcnt >= 0);
13837 13690 sfmmup->sfmmu_srdp = srdp;
13838 13691 atomic_add_32((volatile uint_t *)&srdp->srd_refcnt, 1);
13839 13692 mutex_exit(&srd_buckets[hash].srdb_lock);
13840 13693 kmem_cache_free(srd_cache, newsrdp);
13841 13694 return;
13842 13695 }
13843 13696 }
13844 13697 newsrdp->srd_hash = srd_buckets[hash].srdb_srdp;
13845 13698 srd_buckets[hash].srdb_srdp = newsrdp;
13846 13699 sfmmup->sfmmu_srdp = newsrdp;
13847 13700
13848 13701 mutex_exit(&srd_buckets[hash].srdb_lock);
13849 13702
13850 13703 }
13851 13704
13852 13705 static void
13853 13706 sfmmu_leave_srd(sfmmu_t *sfmmup)
13854 13707 {
13855 13708 vnode_t *evp;
13856 13709 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
13857 13710 uint_t hash;
13858 13711 sf_srd_t **prev_srdpp;
13859 13712 sf_region_t *rgnp;
13860 13713 sf_region_t *nrgnp;
13861 13714 #ifdef DEBUG
13862 13715 int rgns = 0;
13863 13716 #endif
13864 13717 int i;
13865 13718
13866 13719 ASSERT(sfmmup != ksfmmup);
13867 13720 ASSERT(srdp != NULL);
13868 13721 ASSERT(srdp->srd_refcnt > 0);
13869 13722 ASSERT(sfmmup->sfmmu_scdp == NULL);
13870 13723 ASSERT(sfmmup->sfmmu_free == 1);
13871 13724
13872 13725 sfmmup->sfmmu_srdp = NULL;
13873 13726 evp = srdp->srd_evp;
13874 13727 ASSERT(evp != NULL);
13875 13728 if (atomic_add_32_nv(
13876 13729 (volatile uint_t *)&srdp->srd_refcnt, -1)) {
13877 13730 VN_RELE(evp);
13878 13731 return;
13879 13732 }
13880 13733
13881 13734 hash = SRD_HASH_FUNCTION(evp);
13882 13735 mutex_enter(&srd_buckets[hash].srdb_lock);
13883 13736 for (prev_srdpp = &srd_buckets[hash].srdb_srdp;
13884 13737 (srdp = *prev_srdpp) != NULL; prev_srdpp = &srdp->srd_hash) {
13885 13738 if (srdp->srd_evp == evp) {
13886 13739 break;
13887 13740 }
13888 13741 }
13889 13742 if (srdp == NULL || srdp->srd_refcnt) {
13890 13743 mutex_exit(&srd_buckets[hash].srdb_lock);
13891 13744 VN_RELE(evp);
13892 13745 return;
13893 13746 }
13894 13747 *prev_srdpp = srdp->srd_hash;
13895 13748 mutex_exit(&srd_buckets[hash].srdb_lock);
13896 13749
13897 13750 ASSERT(srdp->srd_refcnt == 0);
13898 13751 VN_RELE(evp);
13899 13752
13900 13753 #ifdef DEBUG
13901 13754 for (i = 0; i < SFMMU_MAX_REGION_BUCKETS; i++) {
13902 13755 ASSERT(srdp->srd_rgnhash[i] == NULL);
13903 13756 }
13904 13757 #endif /* DEBUG */
13905 13758
13906 13759 /* free each hme regions in the srd */
13907 13760 for (rgnp = srdp->srd_hmergnfree; rgnp != NULL; rgnp = nrgnp) {
13908 13761 nrgnp = rgnp->rgn_next;
13909 13762 ASSERT(rgnp->rgn_id < srdp->srd_next_hmerid);
13910 13763 ASSERT(rgnp->rgn_refcnt == 0);
13911 13764 ASSERT(rgnp->rgn_sfmmu_head == NULL);
13912 13765 ASSERT(rgnp->rgn_flags & SFMMU_REGION_FREE);
13913 13766 ASSERT(rgnp->rgn_hmeflags == 0);
13914 13767 ASSERT(srdp->srd_hmergnp[rgnp->rgn_id] == rgnp);
13915 13768 #ifdef DEBUG
13916 13769 for (i = 0; i < MMU_PAGE_SIZES; i++) {
13917 13770 ASSERT(rgnp->rgn_ttecnt[i] == 0);
13918 13771 }
13919 13772 rgns++;
13920 13773 #endif /* DEBUG */
13921 13774 kmem_cache_free(region_cache, rgnp);
13922 13775 }
13923 13776 ASSERT(rgns == srdp->srd_next_hmerid);
13924 13777
13925 13778 #ifdef DEBUG
13926 13779 rgns = 0;
13927 13780 #endif
13928 13781 /* free each ism rgns in the srd */
13929 13782 for (rgnp = srdp->srd_ismrgnfree; rgnp != NULL; rgnp = nrgnp) {
13930 13783 nrgnp = rgnp->rgn_next;
13931 13784 ASSERT(rgnp->rgn_id < srdp->srd_next_ismrid);
13932 13785 ASSERT(rgnp->rgn_refcnt == 0);
13933 13786 ASSERT(rgnp->rgn_sfmmu_head == NULL);
13934 13787 ASSERT(rgnp->rgn_flags & SFMMU_REGION_FREE);
13935 13788 ASSERT(srdp->srd_ismrgnp[rgnp->rgn_id] == rgnp);
13936 13789 #ifdef DEBUG
13937 13790 for (i = 0; i < MMU_PAGE_SIZES; i++) {
13938 13791 ASSERT(rgnp->rgn_ttecnt[i] == 0);
13939 13792 }
13940 13793 rgns++;
13941 13794 #endif /* DEBUG */
13942 13795 kmem_cache_free(region_cache, rgnp);
13943 13796 }
13944 13797 ASSERT(rgns == srdp->srd_next_ismrid);
13945 13798 ASSERT(srdp->srd_ismbusyrgns == 0);
13946 13799 ASSERT(srdp->srd_hmebusyrgns == 0);
13947 13800
13948 13801 srdp->srd_next_ismrid = 0;
13949 13802 srdp->srd_next_hmerid = 0;
13950 13803
13951 13804 bzero((void *)srdp->srd_ismrgnp,
13952 13805 sizeof (sf_region_t *) * SFMMU_MAX_ISM_REGIONS);
13953 13806 bzero((void *)srdp->srd_hmergnp,
13954 13807 sizeof (sf_region_t *) * SFMMU_MAX_HME_REGIONS);
13955 13808
13956 13809 ASSERT(srdp->srd_scdp == NULL);
13957 13810 kmem_cache_free(srd_cache, srdp);
13958 13811 }
13959 13812
13960 13813 /* ARGSUSED */
13961 13814 static int
13962 13815 sfmmu_srdcache_constructor(void *buf, void *cdrarg, int kmflags)
13963 13816 {
13964 13817 sf_srd_t *srdp = (sf_srd_t *)buf;
13965 13818 bzero(buf, sizeof (*srdp));
13966 13819
13967 13820 mutex_init(&srdp->srd_mutex, NULL, MUTEX_DEFAULT, NULL);
13968 13821 mutex_init(&srdp->srd_scd_mutex, NULL, MUTEX_DEFAULT, NULL);
13969 13822 return (0);
13970 13823 }
13971 13824
13972 13825 /* ARGSUSED */
13973 13826 static void
13974 13827 sfmmu_srdcache_destructor(void *buf, void *cdrarg)
13975 13828 {
13976 13829 sf_srd_t *srdp = (sf_srd_t *)buf;
13977 13830
13978 13831 mutex_destroy(&srdp->srd_mutex);
13979 13832 mutex_destroy(&srdp->srd_scd_mutex);
13980 13833 }
13981 13834
13982 13835 /*
13983 13836 * The caller makes sure hat_join_region()/hat_leave_region() can't be called
13984 13837 * at the same time for the same process and address range. This is ensured by
13985 13838 * the fact that address space is locked as writer when a process joins the
13986 13839 * regions. Therefore there's no need to hold an srd lock during the entire
13987 13840 * execution of hat_join_region()/hat_leave_region().
13988 13841 */
13989 13842
13990 13843 #define RGN_HASH_FUNCTION(obj) (((((uintptr_t)(obj)) >> 4) ^ \
13991 13844 (((uintptr_t)(obj)) >> 11)) & \
13992 13845 srd_rgn_hashmask)
13993 13846 /*
13994 13847 * This routine implements the shared context functionality required when
13995 13848 * attaching a segment to an address space. It must be called from
13996 13849 * hat_share() for D(ISM) segments and from segvn_create() for segments
13997 13850 * with the MAP_PRIVATE and MAP_TEXT flags set. It returns a region_cookie
13998 13851 * which is saved in the private segment data for hme segments and
13999 13852 * the ism_map structure for ism segments.
14000 13853 */
14001 13854 hat_region_cookie_t
14002 13855 hat_join_region(struct hat *sfmmup,
14003 13856 caddr_t r_saddr,
14004 13857 size_t r_size,
14005 13858 void *r_obj,
14006 13859 u_offset_t r_objoff,
14007 13860 uchar_t r_perm,
14008 13861 uchar_t r_pgszc,
14009 13862 hat_rgn_cb_func_t r_cb_function,
14010 13863 uint_t flags)
14011 13864 {
14012 13865 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
14013 13866 uint_t rhash;
14014 13867 uint_t rid;
14015 13868 hatlock_t *hatlockp;
14016 13869 sf_region_t *rgnp;
14017 13870 sf_region_t *new_rgnp = NULL;
14018 13871 int i;
14019 13872 uint16_t *nextidp;
14020 13873 sf_region_t **freelistp;
14021 13874 int maxids;
14022 13875 sf_region_t **rarrp;
14023 13876 uint16_t *busyrgnsp;
14024 13877 ulong_t rttecnt;
14025 13878 uchar_t tteflag;
14026 13879 uchar_t r_type = flags & HAT_REGION_TYPE_MASK;
14027 13880 int text = (r_type == HAT_REGION_TEXT);
14028 13881
14029 13882 if (srdp == NULL || r_size == 0) {
14030 13883 return (HAT_INVALID_REGION_COOKIE);
14031 13884 }
14032 13885
14033 13886 ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
14034 13887 ASSERT(sfmmup != ksfmmup);
14035 13888 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
14036 13889 ASSERT(srdp->srd_refcnt > 0);
14037 13890 ASSERT(!(flags & ~HAT_REGION_TYPE_MASK));
14038 13891 ASSERT(flags == HAT_REGION_TEXT || flags == HAT_REGION_ISM);
14039 13892 ASSERT(r_pgszc < mmu_page_sizes);
14040 13893 if (!IS_P2ALIGNED(r_saddr, TTEBYTES(r_pgszc)) ||
14041 13894 !IS_P2ALIGNED(r_size, TTEBYTES(r_pgszc))) {
14042 13895 panic("hat_join_region: region addr or size is not aligned\n");
14043 13896 }
14044 13897
14045 13898
14046 13899 r_type = (r_type == HAT_REGION_ISM) ? SFMMU_REGION_ISM :
14047 13900 SFMMU_REGION_HME;
14048 13901 /*
14049 13902 * Currently only support shared hmes for the read only main text
14050 13903 * region.
14051 13904 */
14052 13905 if (r_type == SFMMU_REGION_HME && ((r_obj != srdp->srd_evp) ||
14053 13906 (r_perm & PROT_WRITE))) {
14054 13907 return (HAT_INVALID_REGION_COOKIE);
14055 13908 }
14056 13909
14057 13910 rhash = RGN_HASH_FUNCTION(r_obj);
14058 13911
14059 13912 if (r_type == SFMMU_REGION_ISM) {
14060 13913 nextidp = &srdp->srd_next_ismrid;
14061 13914 freelistp = &srdp->srd_ismrgnfree;
14062 13915 maxids = SFMMU_MAX_ISM_REGIONS;
14063 13916 rarrp = srdp->srd_ismrgnp;
14064 13917 busyrgnsp = &srdp->srd_ismbusyrgns;
14065 13918 } else {
14066 13919 nextidp = &srdp->srd_next_hmerid;
14067 13920 freelistp = &srdp->srd_hmergnfree;
14068 13921 maxids = SFMMU_MAX_HME_REGIONS;
14069 13922 rarrp = srdp->srd_hmergnp;
14070 13923 busyrgnsp = &srdp->srd_hmebusyrgns;
14071 13924 }
14072 13925
14073 13926 mutex_enter(&srdp->srd_mutex);
14074 13927
14075 13928 for (rgnp = srdp->srd_rgnhash[rhash]; rgnp != NULL;
14076 13929 rgnp = rgnp->rgn_hash) {
14077 13930 if (rgnp->rgn_saddr == r_saddr && rgnp->rgn_size == r_size &&
14078 13931 rgnp->rgn_obj == r_obj && rgnp->rgn_objoff == r_objoff &&
14079 13932 rgnp->rgn_perm == r_perm && rgnp->rgn_pgszc == r_pgszc) {
14080 13933 break;
14081 13934 }
14082 13935 }
14083 13936
14084 13937 rfound:
14085 13938 if (rgnp != NULL) {
14086 13939 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type);
14087 13940 ASSERT(rgnp->rgn_cb_function == r_cb_function);
14088 13941 ASSERT(rgnp->rgn_refcnt >= 0);
14089 13942 rid = rgnp->rgn_id;
14090 13943 ASSERT(rid < maxids);
14091 13944 ASSERT(rarrp[rid] == rgnp);
14092 13945 ASSERT(rid < *nextidp);
14093 13946 atomic_add_32((volatile uint_t *)&rgnp->rgn_refcnt, 1);
14094 13947 mutex_exit(&srdp->srd_mutex);
14095 13948 if (new_rgnp != NULL) {
14096 13949 kmem_cache_free(region_cache, new_rgnp);
14097 13950 }
14098 13951 if (r_type == SFMMU_REGION_HME) {
14099 13952 int myjoin =
14100 13953 (sfmmup == astosfmmu(curthread->t_procp->p_as));
14101 13954
14102 13955 sfmmu_link_to_hmeregion(sfmmup, rgnp);
14103 13956 /*
14104 13957 * bitmap should be updated after linking sfmmu on
14105 13958 * region list so that pageunload() doesn't skip
14106 13959 * TSB/TLB flush. As soon as bitmap is updated another
14107 13960 * thread in this process can already start accessing
14108 13961 * this region.
14109 13962 */
14110 13963 /*
14111 13964 * Normally ttecnt accounting is done as part of
14112 13965 * pagefault handling. But a process may not take any
14113 13966 * pagefaults on shared hmeblks created by some other
14114 13967 * process. To compensate for this assume that the
14115 13968 * entire region will end up faulted in using
14116 13969 * the region's pagesize.
14117 13970 *
14118 13971 */
14119 13972 if (r_pgszc > TTE8K) {
14120 13973 tteflag = 1 << r_pgszc;
14121 13974 if (disable_large_pages & tteflag) {
14122 13975 tteflag = 0;
14123 13976 }
14124 13977 } else {
14125 13978 tteflag = 0;
14126 13979 }
14127 13980 if (tteflag && !(sfmmup->sfmmu_rtteflags & tteflag)) {
14128 13981 hatlockp = sfmmu_hat_enter(sfmmup);
14129 13982 sfmmup->sfmmu_rtteflags |= tteflag;
14130 13983 sfmmu_hat_exit(hatlockp);
14131 13984 }
14132 13985 hatlockp = sfmmu_hat_enter(sfmmup);
14133 13986
14134 13987 /*
14135 13988 * Preallocate 1/4 of ttecnt's in 8K TSB for >= 4M
14136 13989 * region to allow for large page allocation failure.
14137 13990 */
14138 13991 if (r_pgszc >= TTE4M) {
14139 13992 sfmmup->sfmmu_tsb0_4minflcnt +=
14140 13993 r_size >> (TTE_PAGE_SHIFT(TTE8K) + 2);
14141 13994 }
14142 13995
14143 13996 /* update sfmmu_ttecnt with the shme rgn ttecnt */
14144 13997 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc);
14145 13998 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc],
14146 13999 rttecnt);
14147 14000
14148 14001 if (text && r_pgszc >= TTE4M &&
14149 14002 (tteflag || ((disable_large_pages >> TTE4M) &
14150 14003 ((1 << (r_pgszc - TTE4M + 1)) - 1))) &&
14151 14004 !SFMMU_FLAGS_ISSET(sfmmup, HAT_4MTEXT_FLAG)) {
14152 14005 SFMMU_FLAGS_SET(sfmmup, HAT_4MTEXT_FLAG);
14153 14006 }
14154 14007
14155 14008 sfmmu_hat_exit(hatlockp);
14156 14009 /*
14157 14010 * On Panther we need to make sure TLB is programmed
14158 14011 * to accept 32M/256M pages. Call
14159 14012 * sfmmu_check_page_sizes() now to make sure TLB is
14160 14013 * setup before making hmeregions visible to other
14161 14014 * threads.
14162 14015 */
14163 14016 sfmmu_check_page_sizes(sfmmup, 1);
14164 14017 hatlockp = sfmmu_hat_enter(sfmmup);
14165 14018 SF_RGNMAP_ADD(sfmmup->sfmmu_hmeregion_map, rid);
14166 14019
14167 14020 /*
14168 14021 * if context is invalid tsb miss exception code will
14169 14022 * call sfmmu_check_page_sizes() and update tsbmiss
14170 14023 * area later.
14171 14024 */
14172 14025 kpreempt_disable();
14173 14026 if (myjoin &&
14174 14027 (sfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum
14175 14028 != INVALID_CONTEXT)) {
14176 14029 struct tsbmiss *tsbmp;
14177 14030
14178 14031 tsbmp = &tsbmiss_area[CPU->cpu_id];
14179 14032 ASSERT(sfmmup == tsbmp->usfmmup);
14180 14033 BT_SET(tsbmp->shmermap, rid);
14181 14034 if (r_pgszc > TTE64K) {
14182 14035 tsbmp->uhat_rtteflags |= tteflag;
14183 14036 }
14184 14037
14185 14038 }
14186 14039 kpreempt_enable();
14187 14040
14188 14041 sfmmu_hat_exit(hatlockp);
14189 14042 ASSERT((hat_region_cookie_t)((uint64_t)rid) !=
14190 14043 HAT_INVALID_REGION_COOKIE);
14191 14044 } else {
14192 14045 hatlockp = sfmmu_hat_enter(sfmmup);
14193 14046 SF_RGNMAP_ADD(sfmmup->sfmmu_ismregion_map, rid);
14194 14047 sfmmu_hat_exit(hatlockp);
14195 14048 }
14196 14049 ASSERT(rid < maxids);
14197 14050
14198 14051 if (r_type == SFMMU_REGION_ISM) {
14199 14052 sfmmu_find_scd(sfmmup);
14200 14053 }
14201 14054 return ((hat_region_cookie_t)((uint64_t)rid));
14202 14055 }
14203 14056
14204 14057 ASSERT(new_rgnp == NULL);
14205 14058
14206 14059 if (*busyrgnsp >= maxids) {
14207 14060 mutex_exit(&srdp->srd_mutex);
14208 14061 return (HAT_INVALID_REGION_COOKIE);
14209 14062 }
14210 14063
14211 14064 ASSERT(MUTEX_HELD(&srdp->srd_mutex));
14212 14065 if (*freelistp != NULL) {
14213 14066 rgnp = *freelistp;
14214 14067 *freelistp = rgnp->rgn_next;
14215 14068 ASSERT(rgnp->rgn_id < *nextidp);
14216 14069 ASSERT(rgnp->rgn_id < maxids);
14217 14070 ASSERT(rgnp->rgn_flags & SFMMU_REGION_FREE);
14218 14071 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK)
14219 14072 == r_type);
14220 14073 ASSERT(rarrp[rgnp->rgn_id] == rgnp);
14221 14074 ASSERT(rgnp->rgn_hmeflags == 0);
14222 14075 } else {
14223 14076 /*
14224 14077 * release local locks before memory allocation.
14225 14078 */
14226 14079 mutex_exit(&srdp->srd_mutex);
14227 14080
14228 14081 new_rgnp = kmem_cache_alloc(region_cache, KM_SLEEP);
14229 14082
14230 14083 mutex_enter(&srdp->srd_mutex);
14231 14084 for (rgnp = srdp->srd_rgnhash[rhash]; rgnp != NULL;
14232 14085 rgnp = rgnp->rgn_hash) {
14233 14086 if (rgnp->rgn_saddr == r_saddr &&
14234 14087 rgnp->rgn_size == r_size &&
14235 14088 rgnp->rgn_obj == r_obj &&
14236 14089 rgnp->rgn_objoff == r_objoff &&
14237 14090 rgnp->rgn_perm == r_perm &&
14238 14091 rgnp->rgn_pgszc == r_pgszc) {
14239 14092 break;
14240 14093 }
14241 14094 }
14242 14095 if (rgnp != NULL) {
14243 14096 goto rfound;
14244 14097 }
14245 14098
14246 14099 if (*nextidp >= maxids) {
14247 14100 mutex_exit(&srdp->srd_mutex);
14248 14101 goto fail;
14249 14102 }
14250 14103 rgnp = new_rgnp;
14251 14104 new_rgnp = NULL;
14252 14105 rgnp->rgn_id = (*nextidp)++;
14253 14106 ASSERT(rgnp->rgn_id < maxids);
14254 14107 ASSERT(rarrp[rgnp->rgn_id] == NULL);
14255 14108 rarrp[rgnp->rgn_id] = rgnp;
14256 14109 }
14257 14110
14258 14111 ASSERT(rgnp->rgn_sfmmu_head == NULL);
14259 14112 ASSERT(rgnp->rgn_hmeflags == 0);
14260 14113 #ifdef DEBUG
14261 14114 for (i = 0; i < MMU_PAGE_SIZES; i++) {
14262 14115 ASSERT(rgnp->rgn_ttecnt[i] == 0);
14263 14116 }
14264 14117 #endif
14265 14118 rgnp->rgn_saddr = r_saddr;
14266 14119 rgnp->rgn_size = r_size;
14267 14120 rgnp->rgn_obj = r_obj;
14268 14121 rgnp->rgn_objoff = r_objoff;
14269 14122 rgnp->rgn_perm = r_perm;
14270 14123 rgnp->rgn_pgszc = r_pgszc;
14271 14124 rgnp->rgn_flags = r_type;
14272 14125 rgnp->rgn_refcnt = 0;
14273 14126 rgnp->rgn_cb_function = r_cb_function;
14274 14127 rgnp->rgn_hash = srdp->srd_rgnhash[rhash];
14275 14128 srdp->srd_rgnhash[rhash] = rgnp;
14276 14129 (*busyrgnsp)++;
14277 14130 ASSERT(*busyrgnsp <= maxids);
14278 14131 goto rfound;
14279 14132
14280 14133 fail:
14281 14134 ASSERT(new_rgnp != NULL);
14282 14135 kmem_cache_free(region_cache, new_rgnp);
14283 14136 return (HAT_INVALID_REGION_COOKIE);
14284 14137 }
14285 14138
14286 14139 /*
14287 14140 * This function implements the shared context functionality required
14288 14141 * when detaching a segment from an address space. It must be called
14289 14142 * from hat_unshare() for all D(ISM) segments and from segvn_unmap(),
14290 14143 * for segments with a valid region_cookie.
14291 14144 * It will also be called from all seg_vn routines which change a
14292 14145 * segment's attributes such as segvn_setprot(), segvn_setpagesize(),
14293 14146 * segvn_clrszc() & segvn_advise(), as well as in the case of COW fault
14294 14147 * from segvn_fault().
14295 14148 */
14296 14149 void
14297 14150 hat_leave_region(struct hat *sfmmup, hat_region_cookie_t rcookie, uint_t flags)
14298 14151 {
14299 14152 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
14300 14153 sf_scd_t *scdp;
14301 14154 uint_t rhash;
14302 14155 uint_t rid = (uint_t)((uint64_t)rcookie);
14303 14156 hatlock_t *hatlockp = NULL;
14304 14157 sf_region_t *rgnp;
14305 14158 sf_region_t **prev_rgnpp;
14306 14159 sf_region_t *cur_rgnp;
14307 14160 void *r_obj;
14308 14161 int i;
14309 14162 caddr_t r_saddr;
14310 14163 caddr_t r_eaddr;
14311 14164 size_t r_size;
14312 14165 uchar_t r_pgszc;
14313 14166 uchar_t r_type = flags & HAT_REGION_TYPE_MASK;
14314 14167
14315 14168 ASSERT(sfmmup != ksfmmup);
14316 14169 ASSERT(srdp != NULL);
14317 14170 ASSERT(srdp->srd_refcnt > 0);
14318 14171 ASSERT(!(flags & ~HAT_REGION_TYPE_MASK));
14319 14172 ASSERT(flags == HAT_REGION_TEXT || flags == HAT_REGION_ISM);
14320 14173 ASSERT(!sfmmup->sfmmu_free || sfmmup->sfmmu_scdp == NULL);
14321 14174
14322 14175 r_type = (r_type == HAT_REGION_ISM) ? SFMMU_REGION_ISM :
14323 14176 SFMMU_REGION_HME;
14324 14177
14325 14178 if (r_type == SFMMU_REGION_ISM) {
14326 14179 ASSERT(SFMMU_IS_ISMRID_VALID(rid));
14327 14180 ASSERT(rid < SFMMU_MAX_ISM_REGIONS);
14328 14181 rgnp = srdp->srd_ismrgnp[rid];
14329 14182 } else {
14330 14183 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
14331 14184 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
14332 14185 rgnp = srdp->srd_hmergnp[rid];
14333 14186 }
14334 14187 ASSERT(rgnp != NULL);
14335 14188 ASSERT(rgnp->rgn_id == rid);
14336 14189 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type);
14337 14190 ASSERT(!(rgnp->rgn_flags & SFMMU_REGION_FREE));
14338 14191 ASSERT(AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
14339 14192
14340 14193 ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
14341 14194 if (r_type == SFMMU_REGION_HME && sfmmup->sfmmu_as->a_xhat != NULL) {
14342 14195 xhat_unload_callback_all(sfmmup->sfmmu_as, rgnp->rgn_saddr,
14343 14196 rgnp->rgn_size, 0, NULL);
14344 14197 }
14345 14198
14346 14199 if (sfmmup->sfmmu_free) {
14347 14200 ulong_t rttecnt;
14348 14201 r_pgszc = rgnp->rgn_pgszc;
14349 14202 r_size = rgnp->rgn_size;
14350 14203
14351 14204 ASSERT(sfmmup->sfmmu_scdp == NULL);
14352 14205 if (r_type == SFMMU_REGION_ISM) {
14353 14206 SF_RGNMAP_DEL(sfmmup->sfmmu_ismregion_map, rid);
14354 14207 } else {
14355 14208 /* update shme rgns ttecnt in sfmmu_ttecnt */
14356 14209 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc);
14357 14210 ASSERT(sfmmup->sfmmu_ttecnt[r_pgszc] >= rttecnt);
14358 14211
14359 14212 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc],
14360 14213 -rttecnt);
14361 14214
14362 14215 SF_RGNMAP_DEL(sfmmup->sfmmu_hmeregion_map, rid);
14363 14216 }
14364 14217 } else if (r_type == SFMMU_REGION_ISM) {
14365 14218 hatlockp = sfmmu_hat_enter(sfmmup);
14366 14219 ASSERT(rid < srdp->srd_next_ismrid);
14367 14220 SF_RGNMAP_DEL(sfmmup->sfmmu_ismregion_map, rid);
14368 14221 scdp = sfmmup->sfmmu_scdp;
14369 14222 if (scdp != NULL &&
14370 14223 SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid)) {
14371 14224 sfmmu_leave_scd(sfmmup, r_type);
14372 14225 ASSERT(sfmmu_hat_lock_held(sfmmup));
14373 14226 }
14374 14227 sfmmu_hat_exit(hatlockp);
14375 14228 } else {
14376 14229 ulong_t rttecnt;
14377 14230 r_pgszc = rgnp->rgn_pgszc;
14378 14231 r_saddr = rgnp->rgn_saddr;
14379 14232 r_size = rgnp->rgn_size;
14380 14233 r_eaddr = r_saddr + r_size;
14381 14234
14382 14235 ASSERT(r_type == SFMMU_REGION_HME);
14383 14236 hatlockp = sfmmu_hat_enter(sfmmup);
14384 14237 ASSERT(rid < srdp->srd_next_hmerid);
14385 14238 SF_RGNMAP_DEL(sfmmup->sfmmu_hmeregion_map, rid);
14386 14239
14387 14240 /*
14388 14241 * If region is part of an SCD call sfmmu_leave_scd().
14389 14242 * Otherwise if process is not exiting and has valid context
14390 14243 * just drop the context on the floor to lose stale TLB
14391 14244 * entries and force the update of tsb miss area to reflect
14392 14245 * the new region map. After that clean our TSB entries.
14393 14246 */
14394 14247 scdp = sfmmup->sfmmu_scdp;
14395 14248 if (scdp != NULL &&
14396 14249 SF_RGNMAP_TEST(scdp->scd_hmeregion_map, rid)) {
14397 14250 sfmmu_leave_scd(sfmmup, r_type);
14398 14251 ASSERT(sfmmu_hat_lock_held(sfmmup));
14399 14252 }
14400 14253 sfmmu_invalidate_ctx(sfmmup);
14401 14254
14402 14255 i = TTE8K;
14403 14256 while (i < mmu_page_sizes) {
14404 14257 if (rgnp->rgn_ttecnt[i] != 0) {
14405 14258 sfmmu_unload_tsb_range(sfmmup, r_saddr,
14406 14259 r_eaddr, i);
14407 14260 if (i < TTE4M) {
14408 14261 i = TTE4M;
14409 14262 continue;
14410 14263 } else {
14411 14264 break;
14412 14265 }
14413 14266 }
14414 14267 i++;
14415 14268 }
14416 14269 /* Remove the preallocated 1/4 8k ttecnt for 4M regions. */
14417 14270 if (r_pgszc >= TTE4M) {
14418 14271 rttecnt = r_size >> (TTE_PAGE_SHIFT(TTE8K) + 2);
14419 14272 ASSERT(sfmmup->sfmmu_tsb0_4minflcnt >=
14420 14273 rttecnt);
14421 14274 sfmmup->sfmmu_tsb0_4minflcnt -= rttecnt;
14422 14275 }
14423 14276
14424 14277 /* update shme rgns ttecnt in sfmmu_ttecnt */
14425 14278 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc);
14426 14279 ASSERT(sfmmup->sfmmu_ttecnt[r_pgszc] >= rttecnt);
14427 14280 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc], -rttecnt);
14428 14281
14429 14282 sfmmu_hat_exit(hatlockp);
14430 14283 if (scdp != NULL && sfmmup->sfmmu_scdp == NULL) {
14431 14284 /* sfmmup left the scd, grow private tsb */
14432 14285 sfmmu_check_page_sizes(sfmmup, 1);
14433 14286 } else {
14434 14287 sfmmu_check_page_sizes(sfmmup, 0);
14435 14288 }
14436 14289 }
14437 14290
14438 14291 if (r_type == SFMMU_REGION_HME) {
14439 14292 sfmmu_unlink_from_hmeregion(sfmmup, rgnp);
14440 14293 }
14441 14294
14442 14295 r_obj = rgnp->rgn_obj;
14443 14296 if (atomic_add_32_nv((volatile uint_t *)&rgnp->rgn_refcnt, -1)) {
14444 14297 return;
14445 14298 }
14446 14299
14447 14300 /*
14448 14301 * looks like nobody uses this region anymore. Free it.
14449 14302 */
14450 14303 rhash = RGN_HASH_FUNCTION(r_obj);
14451 14304 mutex_enter(&srdp->srd_mutex);
14452 14305 for (prev_rgnpp = &srdp->srd_rgnhash[rhash];
14453 14306 (cur_rgnp = *prev_rgnpp) != NULL;
14454 14307 prev_rgnpp = &cur_rgnp->rgn_hash) {
14455 14308 if (cur_rgnp == rgnp && cur_rgnp->rgn_refcnt == 0) {
14456 14309 break;
14457 14310 }
14458 14311 }
14459 14312
14460 14313 if (cur_rgnp == NULL) {
14461 14314 mutex_exit(&srdp->srd_mutex);
14462 14315 return;
14463 14316 }
14464 14317
14465 14318 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type);
14466 14319 *prev_rgnpp = rgnp->rgn_hash;
14467 14320 if (r_type == SFMMU_REGION_ISM) {
14468 14321 rgnp->rgn_flags |= SFMMU_REGION_FREE;
14469 14322 ASSERT(rid < srdp->srd_next_ismrid);
14470 14323 rgnp->rgn_next = srdp->srd_ismrgnfree;
14471 14324 srdp->srd_ismrgnfree = rgnp;
14472 14325 ASSERT(srdp->srd_ismbusyrgns > 0);
14473 14326 srdp->srd_ismbusyrgns--;
14474 14327 mutex_exit(&srdp->srd_mutex);
14475 14328 return;
14476 14329 }
14477 14330 mutex_exit(&srdp->srd_mutex);
14478 14331
14479 14332 /*
14480 14333 * Destroy region's hmeblks.
14481 14334 */
14482 14335 sfmmu_unload_hmeregion(srdp, rgnp);
14483 14336
14484 14337 rgnp->rgn_hmeflags = 0;
14485 14338
14486 14339 ASSERT(rgnp->rgn_sfmmu_head == NULL);
14487 14340 ASSERT(rgnp->rgn_id == rid);
14488 14341 for (i = 0; i < MMU_PAGE_SIZES; i++) {
14489 14342 rgnp->rgn_ttecnt[i] = 0;
14490 14343 }
14491 14344 rgnp->rgn_flags |= SFMMU_REGION_FREE;
14492 14345 mutex_enter(&srdp->srd_mutex);
14493 14346 ASSERT(rid < srdp->srd_next_hmerid);
14494 14347 rgnp->rgn_next = srdp->srd_hmergnfree;
14495 14348 srdp->srd_hmergnfree = rgnp;
14496 14349 ASSERT(srdp->srd_hmebusyrgns > 0);
14497 14350 srdp->srd_hmebusyrgns--;
14498 14351 mutex_exit(&srdp->srd_mutex);
14499 14352 }
14500 14353
14501 14354 /*
14502 14355 * For now only called for hmeblk regions and not for ISM regions.
14503 14356 */
14504 14357 void
14505 14358 hat_dup_region(struct hat *sfmmup, hat_region_cookie_t rcookie)
14506 14359 {
14507 14360 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
14508 14361 uint_t rid = (uint_t)((uint64_t)rcookie);
14509 14362 sf_region_t *rgnp;
14510 14363 sf_rgn_link_t *rlink;
14511 14364 sf_rgn_link_t *hrlink;
14512 14365 ulong_t rttecnt;
14513 14366
14514 14367 ASSERT(sfmmup != ksfmmup);
14515 14368 ASSERT(srdp != NULL);
14516 14369 ASSERT(srdp->srd_refcnt > 0);
14517 14370
14518 14371 ASSERT(rid < srdp->srd_next_hmerid);
14519 14372 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
14520 14373 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
14521 14374
14522 14375 rgnp = srdp->srd_hmergnp[rid];
14523 14376 ASSERT(rgnp->rgn_refcnt > 0);
14524 14377 ASSERT(rgnp->rgn_id == rid);
14525 14378 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == SFMMU_REGION_HME);
14526 14379 ASSERT(!(rgnp->rgn_flags & SFMMU_REGION_FREE));
14527 14380
14528 14381 atomic_add_32((volatile uint_t *)&rgnp->rgn_refcnt, 1);
14529 14382
14530 14383 /* LINTED: constant in conditional context */
14531 14384 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 1, 0);
14532 14385 ASSERT(rlink != NULL);
14533 14386 mutex_enter(&rgnp->rgn_mutex);
14534 14387 ASSERT(rgnp->rgn_sfmmu_head != NULL);
14535 14388 /* LINTED: constant in conditional context */
14536 14389 SFMMU_HMERID2RLINKP(rgnp->rgn_sfmmu_head, rid, hrlink, 0, 0);
14537 14390 ASSERT(hrlink != NULL);
14538 14391 ASSERT(hrlink->prev == NULL);
14539 14392 rlink->next = rgnp->rgn_sfmmu_head;
14540 14393 rlink->prev = NULL;
14541 14394 hrlink->prev = sfmmup;
14542 14395 /*
14543 14396 * make sure rlink's next field is correct
14544 14397 * before making this link visible.
14545 14398 */
14546 14399 membar_stst();
14547 14400 rgnp->rgn_sfmmu_head = sfmmup;
14548 14401 mutex_exit(&rgnp->rgn_mutex);
14549 14402
14550 14403 /* update sfmmu_ttecnt with the shme rgn ttecnt */
14551 14404 rttecnt = rgnp->rgn_size >> TTE_PAGE_SHIFT(rgnp->rgn_pgszc);
14552 14405 atomic_add_long(&sfmmup->sfmmu_ttecnt[rgnp->rgn_pgszc], rttecnt);
14553 14406 /* update tsb0 inflation count */
14554 14407 if (rgnp->rgn_pgszc >= TTE4M) {
14555 14408 sfmmup->sfmmu_tsb0_4minflcnt +=
14556 14409 rgnp->rgn_size >> (TTE_PAGE_SHIFT(TTE8K) + 2);
14557 14410 }
14558 14411 /*
14559 14412 * Update regionid bitmask without hat lock since no other thread
14560 14413 * can update this region bitmask right now.
14561 14414 */
14562 14415 SF_RGNMAP_ADD(sfmmup->sfmmu_hmeregion_map, rid);
14563 14416 }
14564 14417
14565 14418 /* ARGSUSED */
14566 14419 static int
14567 14420 sfmmu_rgncache_constructor(void *buf, void *cdrarg, int kmflags)
14568 14421 {
14569 14422 sf_region_t *rgnp = (sf_region_t *)buf;
14570 14423 bzero(buf, sizeof (*rgnp));
14571 14424
14572 14425 mutex_init(&rgnp->rgn_mutex, NULL, MUTEX_DEFAULT, NULL);
14573 14426
14574 14427 return (0);
14575 14428 }
14576 14429
14577 14430 /* ARGSUSED */
14578 14431 static void
14579 14432 sfmmu_rgncache_destructor(void *buf, void *cdrarg)
14580 14433 {
14581 14434 sf_region_t *rgnp = (sf_region_t *)buf;
14582 14435 mutex_destroy(&rgnp->rgn_mutex);
14583 14436 }
14584 14437
14585 14438 static int
14586 14439 sfrgnmap_isnull(sf_region_map_t *map)
14587 14440 {
14588 14441 int i;
14589 14442
14590 14443 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) {
14591 14444 if (map->bitmap[i] != 0) {
14592 14445 return (0);
14593 14446 }
14594 14447 }
14595 14448 return (1);
14596 14449 }
14597 14450
14598 14451 static int
14599 14452 sfhmergnmap_isnull(sf_hmeregion_map_t *map)
14600 14453 {
14601 14454 int i;
14602 14455
14603 14456 for (i = 0; i < SFMMU_HMERGNMAP_WORDS; i++) {
14604 14457 if (map->bitmap[i] != 0) {
14605 14458 return (0);
14606 14459 }
14607 14460 }
14608 14461 return (1);
14609 14462 }
14610 14463
14611 14464 #ifdef DEBUG
14612 14465 static void
14613 14466 check_scd_sfmmu_list(sfmmu_t **headp, sfmmu_t *sfmmup, int onlist)
14614 14467 {
14615 14468 sfmmu_t *sp;
14616 14469 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
14617 14470
14618 14471 for (sp = *headp; sp != NULL; sp = sp->sfmmu_scd_link.next) {
14619 14472 ASSERT(srdp == sp->sfmmu_srdp);
14620 14473 if (sp == sfmmup) {
14621 14474 if (onlist) {
14622 14475 return;
14623 14476 } else {
14624 14477 panic("shctx: sfmmu 0x%p found on scd"
14625 14478 "list 0x%p", (void *)sfmmup,
14626 14479 (void *)*headp);
14627 14480 }
14628 14481 }
14629 14482 }
14630 14483 if (onlist) {
14631 14484 panic("shctx: sfmmu 0x%p not found on scd list 0x%p",
14632 14485 (void *)sfmmup, (void *)*headp);
14633 14486 } else {
14634 14487 return;
14635 14488 }
14636 14489 }
14637 14490 #else /* DEBUG */
14638 14491 #define check_scd_sfmmu_list(headp, sfmmup, onlist)
14639 14492 #endif /* DEBUG */
14640 14493
14641 14494 /*
14642 14495 * Removes an sfmmu from the SCD sfmmu list.
14643 14496 */
14644 14497 static void
14645 14498 sfmmu_from_scd_list(sfmmu_t **headp, sfmmu_t *sfmmup)
14646 14499 {
14647 14500 ASSERT(sfmmup->sfmmu_srdp != NULL);
14648 14501 check_scd_sfmmu_list(headp, sfmmup, 1);
14649 14502 if (sfmmup->sfmmu_scd_link.prev != NULL) {
14650 14503 ASSERT(*headp != sfmmup);
14651 14504 sfmmup->sfmmu_scd_link.prev->sfmmu_scd_link.next =
14652 14505 sfmmup->sfmmu_scd_link.next;
14653 14506 } else {
14654 14507 ASSERT(*headp == sfmmup);
14655 14508 *headp = sfmmup->sfmmu_scd_link.next;
14656 14509 }
14657 14510 if (sfmmup->sfmmu_scd_link.next != NULL) {
14658 14511 sfmmup->sfmmu_scd_link.next->sfmmu_scd_link.prev =
14659 14512 sfmmup->sfmmu_scd_link.prev;
14660 14513 }
14661 14514 }
14662 14515
14663 14516
14664 14517 /*
14665 14518 * Adds an sfmmu to the start of the queue.
14666 14519 */
14667 14520 static void
14668 14521 sfmmu_to_scd_list(sfmmu_t **headp, sfmmu_t *sfmmup)
14669 14522 {
14670 14523 check_scd_sfmmu_list(headp, sfmmup, 0);
14671 14524 sfmmup->sfmmu_scd_link.prev = NULL;
14672 14525 sfmmup->sfmmu_scd_link.next = *headp;
14673 14526 if (*headp != NULL)
14674 14527 (*headp)->sfmmu_scd_link.prev = sfmmup;
14675 14528 *headp = sfmmup;
14676 14529 }
14677 14530
14678 14531 /*
14679 14532 * Remove an scd from the start of the queue.
14680 14533 */
14681 14534 static void
14682 14535 sfmmu_remove_scd(sf_scd_t **headp, sf_scd_t *scdp)
14683 14536 {
14684 14537 if (scdp->scd_prev != NULL) {
14685 14538 ASSERT(*headp != scdp);
14686 14539 scdp->scd_prev->scd_next = scdp->scd_next;
14687 14540 } else {
14688 14541 ASSERT(*headp == scdp);
14689 14542 *headp = scdp->scd_next;
14690 14543 }
14691 14544
14692 14545 if (scdp->scd_next != NULL) {
14693 14546 scdp->scd_next->scd_prev = scdp->scd_prev;
14694 14547 }
14695 14548 }
14696 14549
14697 14550 /*
14698 14551 * Add an scd to the start of the queue.
14699 14552 */
14700 14553 static void
14701 14554 sfmmu_add_scd(sf_scd_t **headp, sf_scd_t *scdp)
14702 14555 {
14703 14556 scdp->scd_prev = NULL;
14704 14557 scdp->scd_next = *headp;
14705 14558 if (*headp != NULL) {
14706 14559 (*headp)->scd_prev = scdp;
14707 14560 }
14708 14561 *headp = scdp;
14709 14562 }
14710 14563
14711 14564 static int
14712 14565 sfmmu_alloc_scd_tsbs(sf_srd_t *srdp, sf_scd_t *scdp)
14713 14566 {
14714 14567 uint_t rid;
14715 14568 uint_t i;
14716 14569 uint_t j;
14717 14570 ulong_t w;
14718 14571 sf_region_t *rgnp;
14719 14572 ulong_t tte8k_cnt = 0;
14720 14573 ulong_t tte4m_cnt = 0;
14721 14574 uint_t tsb_szc;
14722 14575 sfmmu_t *scsfmmup = scdp->scd_sfmmup;
14723 14576 sfmmu_t *ism_hatid;
14724 14577 struct tsb_info *newtsb;
14725 14578 int szc;
14726 14579
14727 14580 ASSERT(srdp != NULL);
14728 14581
14729 14582 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) {
14730 14583 if ((w = scdp->scd_region_map.bitmap[i]) == 0) {
14731 14584 continue;
14732 14585 }
14733 14586 j = 0;
14734 14587 while (w) {
14735 14588 if (!(w & 0x1)) {
14736 14589 j++;
14737 14590 w >>= 1;
14738 14591 continue;
14739 14592 }
14740 14593 rid = (i << BT_ULSHIFT) | j;
14741 14594 j++;
14742 14595 w >>= 1;
14743 14596
14744 14597 if (rid < SFMMU_MAX_HME_REGIONS) {
14745 14598 rgnp = srdp->srd_hmergnp[rid];
14746 14599 ASSERT(rgnp->rgn_id == rid);
14747 14600 ASSERT(rgnp->rgn_refcnt > 0);
14748 14601
14749 14602 if (rgnp->rgn_pgszc < TTE4M) {
14750 14603 tte8k_cnt += rgnp->rgn_size >>
14751 14604 TTE_PAGE_SHIFT(TTE8K);
14752 14605 } else {
14753 14606 ASSERT(rgnp->rgn_pgszc >= TTE4M);
14754 14607 tte4m_cnt += rgnp->rgn_size >>
14755 14608 TTE_PAGE_SHIFT(TTE4M);
14756 14609 /*
14757 14610 * Inflate SCD tsb0 by preallocating
14758 14611 * 1/4 8k ttecnt for 4M regions to
14759 14612 * allow for lgpg alloc failure.
14760 14613 */
14761 14614 tte8k_cnt += rgnp->rgn_size >>
14762 14615 (TTE_PAGE_SHIFT(TTE8K) + 2);
14763 14616 }
14764 14617 } else {
14765 14618 rid -= SFMMU_MAX_HME_REGIONS;
14766 14619 rgnp = srdp->srd_ismrgnp[rid];
14767 14620 ASSERT(rgnp->rgn_id == rid);
14768 14621 ASSERT(rgnp->rgn_refcnt > 0);
14769 14622
14770 14623 ism_hatid = (sfmmu_t *)rgnp->rgn_obj;
14771 14624 ASSERT(ism_hatid->sfmmu_ismhat);
14772 14625
14773 14626 for (szc = 0; szc < TTE4M; szc++) {
14774 14627 tte8k_cnt +=
14775 14628 ism_hatid->sfmmu_ttecnt[szc] <<
14776 14629 TTE_BSZS_SHIFT(szc);
14777 14630 }
14778 14631
14779 14632 ASSERT(rgnp->rgn_pgszc >= TTE4M);
14780 14633 if (rgnp->rgn_pgszc >= TTE4M) {
14781 14634 tte4m_cnt += rgnp->rgn_size >>
14782 14635 TTE_PAGE_SHIFT(TTE4M);
14783 14636 }
14784 14637 }
14785 14638 }
14786 14639 }
14787 14640
14788 14641 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt);
14789 14642
14790 14643 /* Allocate both the SCD TSBs here. */
14791 14644 if (sfmmu_tsbinfo_alloc(&scsfmmup->sfmmu_tsb,
14792 14645 tsb_szc, TSB8K|TSB64K|TSB512K, TSB_ALLOC, scsfmmup) &&
14793 14646 (tsb_szc <= TSB_4M_SZCODE ||
14794 14647 sfmmu_tsbinfo_alloc(&scsfmmup->sfmmu_tsb,
14795 14648 TSB_4M_SZCODE, TSB8K|TSB64K|TSB512K,
14796 14649 TSB_ALLOC, scsfmmup))) {
14797 14650
14798 14651 SFMMU_STAT(sf_scd_1sttsb_allocfail);
14799 14652 return (TSB_ALLOCFAIL);
14800 14653 } else {
14801 14654 scsfmmup->sfmmu_tsb->tsb_flags |= TSB_SHAREDCTX;
14802 14655
14803 14656 if (tte4m_cnt) {
14804 14657 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt);
14805 14658 if (sfmmu_tsbinfo_alloc(&newtsb, tsb_szc,
14806 14659 TSB4M|TSB32M|TSB256M, TSB_ALLOC, scsfmmup) &&
14807 14660 (tsb_szc <= TSB_4M_SZCODE ||
14808 14661 sfmmu_tsbinfo_alloc(&newtsb, TSB_4M_SZCODE,
14809 14662 TSB4M|TSB32M|TSB256M,
14810 14663 TSB_ALLOC, scsfmmup))) {
14811 14664 /*
14812 14665 * If we fail to allocate the 2nd shared tsb,
14813 14666 * just free the 1st tsb, return failure.
14814 14667 */
14815 14668 sfmmu_tsbinfo_free(scsfmmup->sfmmu_tsb);
14816 14669 SFMMU_STAT(sf_scd_2ndtsb_allocfail);
14817 14670 return (TSB_ALLOCFAIL);
14818 14671 } else {
14819 14672 ASSERT(scsfmmup->sfmmu_tsb->tsb_next == NULL);
14820 14673 newtsb->tsb_flags |= TSB_SHAREDCTX;
14821 14674 scsfmmup->sfmmu_tsb->tsb_next = newtsb;
14822 14675 SFMMU_STAT(sf_scd_2ndtsb_alloc);
14823 14676 }
14824 14677 }
14825 14678 SFMMU_STAT(sf_scd_1sttsb_alloc);
14826 14679 }
14827 14680 return (TSB_SUCCESS);
14828 14681 }
14829 14682
14830 14683 static void
14831 14684 sfmmu_free_scd_tsbs(sfmmu_t *scd_sfmmu)
14832 14685 {
14833 14686 while (scd_sfmmu->sfmmu_tsb != NULL) {
14834 14687 struct tsb_info *next = scd_sfmmu->sfmmu_tsb->tsb_next;
14835 14688 sfmmu_tsbinfo_free(scd_sfmmu->sfmmu_tsb);
14836 14689 scd_sfmmu->sfmmu_tsb = next;
14837 14690 }
14838 14691 }
14839 14692
14840 14693 /*
14841 14694 * Link the sfmmu onto the hme region list.
14842 14695 */
14843 14696 void
14844 14697 sfmmu_link_to_hmeregion(sfmmu_t *sfmmup, sf_region_t *rgnp)
14845 14698 {
14846 14699 uint_t rid;
14847 14700 sf_rgn_link_t *rlink;
14848 14701 sfmmu_t *head;
14849 14702 sf_rgn_link_t *hrlink;
14850 14703
14851 14704 rid = rgnp->rgn_id;
14852 14705 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
14853 14706
14854 14707 /* LINTED: constant in conditional context */
14855 14708 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 1, 1);
14856 14709 ASSERT(rlink != NULL);
14857 14710 mutex_enter(&rgnp->rgn_mutex);
14858 14711 if ((head = rgnp->rgn_sfmmu_head) == NULL) {
14859 14712 rlink->next = NULL;
14860 14713 rlink->prev = NULL;
14861 14714 /*
14862 14715 * make sure rlink's next field is NULL
14863 14716 * before making this link visible.
14864 14717 */
14865 14718 membar_stst();
14866 14719 rgnp->rgn_sfmmu_head = sfmmup;
14867 14720 } else {
14868 14721 /* LINTED: constant in conditional context */
14869 14722 SFMMU_HMERID2RLINKP(head, rid, hrlink, 0, 0);
14870 14723 ASSERT(hrlink != NULL);
14871 14724 ASSERT(hrlink->prev == NULL);
14872 14725 rlink->next = head;
14873 14726 rlink->prev = NULL;
14874 14727 hrlink->prev = sfmmup;
14875 14728 /*
14876 14729 * make sure rlink's next field is correct
14877 14730 * before making this link visible.
14878 14731 */
14879 14732 membar_stst();
14880 14733 rgnp->rgn_sfmmu_head = sfmmup;
14881 14734 }
14882 14735 mutex_exit(&rgnp->rgn_mutex);
14883 14736 }
14884 14737
14885 14738 /*
14886 14739 * Unlink the sfmmu from the hme region list.
14887 14740 */
14888 14741 void
14889 14742 sfmmu_unlink_from_hmeregion(sfmmu_t *sfmmup, sf_region_t *rgnp)
14890 14743 {
14891 14744 uint_t rid;
14892 14745 sf_rgn_link_t *rlink;
14893 14746
14894 14747 rid = rgnp->rgn_id;
14895 14748 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
14896 14749
14897 14750 /* LINTED: constant in conditional context */
14898 14751 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 0, 0);
14899 14752 ASSERT(rlink != NULL);
14900 14753 mutex_enter(&rgnp->rgn_mutex);
14901 14754 if (rgnp->rgn_sfmmu_head == sfmmup) {
14902 14755 sfmmu_t *next = rlink->next;
14903 14756 rgnp->rgn_sfmmu_head = next;
14904 14757 /*
14905 14758 * if we are stopped by xc_attention() after this
14906 14759 * point the forward link walking in
14907 14760 * sfmmu_rgntlb_demap() will work correctly since the
14908 14761 * head correctly points to the next element.
14909 14762 */
14910 14763 membar_stst();
14911 14764 rlink->next = NULL;
14912 14765 ASSERT(rlink->prev == NULL);
14913 14766 if (next != NULL) {
14914 14767 sf_rgn_link_t *nrlink;
14915 14768 /* LINTED: constant in conditional context */
14916 14769 SFMMU_HMERID2RLINKP(next, rid, nrlink, 0, 0);
14917 14770 ASSERT(nrlink != NULL);
14918 14771 ASSERT(nrlink->prev == sfmmup);
14919 14772 nrlink->prev = NULL;
14920 14773 }
14921 14774 } else {
14922 14775 sfmmu_t *next = rlink->next;
14923 14776 sfmmu_t *prev = rlink->prev;
14924 14777 sf_rgn_link_t *prlink;
14925 14778
14926 14779 ASSERT(prev != NULL);
14927 14780 /* LINTED: constant in conditional context */
14928 14781 SFMMU_HMERID2RLINKP(prev, rid, prlink, 0, 0);
14929 14782 ASSERT(prlink != NULL);
14930 14783 ASSERT(prlink->next == sfmmup);
14931 14784 prlink->next = next;
14932 14785 /*
14933 14786 * if we are stopped by xc_attention()
14934 14787 * after this point the forward link walking
14935 14788 * will work correctly since the prev element
14936 14789 * correctly points to the next element.
14937 14790 */
14938 14791 membar_stst();
14939 14792 rlink->next = NULL;
14940 14793 rlink->prev = NULL;
14941 14794 if (next != NULL) {
14942 14795 sf_rgn_link_t *nrlink;
14943 14796 /* LINTED: constant in conditional context */
14944 14797 SFMMU_HMERID2RLINKP(next, rid, nrlink, 0, 0);
14945 14798 ASSERT(nrlink != NULL);
14946 14799 ASSERT(nrlink->prev == sfmmup);
14947 14800 nrlink->prev = prev;
14948 14801 }
14949 14802 }
14950 14803 mutex_exit(&rgnp->rgn_mutex);
14951 14804 }
14952 14805
14953 14806 /*
14954 14807 * Link scd sfmmu onto ism or hme region list for each region in the
14955 14808 * scd region map.
14956 14809 */
14957 14810 void
14958 14811 sfmmu_link_scd_to_regions(sf_srd_t *srdp, sf_scd_t *scdp)
14959 14812 {
14960 14813 uint_t rid;
14961 14814 uint_t i;
14962 14815 uint_t j;
14963 14816 ulong_t w;
14964 14817 sf_region_t *rgnp;
14965 14818 sfmmu_t *scsfmmup;
14966 14819
14967 14820 scsfmmup = scdp->scd_sfmmup;
14968 14821 ASSERT(scsfmmup->sfmmu_scdhat);
14969 14822 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) {
14970 14823 if ((w = scdp->scd_region_map.bitmap[i]) == 0) {
14971 14824 continue;
14972 14825 }
14973 14826 j = 0;
14974 14827 while (w) {
14975 14828 if (!(w & 0x1)) {
14976 14829 j++;
14977 14830 w >>= 1;
14978 14831 continue;
14979 14832 }
14980 14833 rid = (i << BT_ULSHIFT) | j;
14981 14834 j++;
14982 14835 w >>= 1;
14983 14836
14984 14837 if (rid < SFMMU_MAX_HME_REGIONS) {
14985 14838 rgnp = srdp->srd_hmergnp[rid];
14986 14839 ASSERT(rgnp->rgn_id == rid);
14987 14840 ASSERT(rgnp->rgn_refcnt > 0);
14988 14841 sfmmu_link_to_hmeregion(scsfmmup, rgnp);
14989 14842 } else {
14990 14843 sfmmu_t *ism_hatid = NULL;
14991 14844 ism_ment_t *ism_ment;
14992 14845 rid -= SFMMU_MAX_HME_REGIONS;
14993 14846 rgnp = srdp->srd_ismrgnp[rid];
14994 14847 ASSERT(rgnp->rgn_id == rid);
14995 14848 ASSERT(rgnp->rgn_refcnt > 0);
14996 14849
14997 14850 ism_hatid = (sfmmu_t *)rgnp->rgn_obj;
14998 14851 ASSERT(ism_hatid->sfmmu_ismhat);
14999 14852 ism_ment = &scdp->scd_ism_links[rid];
15000 14853 ism_ment->iment_hat = scsfmmup;
15001 14854 ism_ment->iment_base_va = rgnp->rgn_saddr;
15002 14855 mutex_enter(&ism_mlist_lock);
15003 14856 iment_add(ism_ment, ism_hatid);
15004 14857 mutex_exit(&ism_mlist_lock);
15005 14858
15006 14859 }
15007 14860 }
15008 14861 }
15009 14862 }
15010 14863 /*
15011 14864 * Unlink scd sfmmu from ism or hme region list for each region in the
15012 14865 * scd region map.
15013 14866 */
15014 14867 void
15015 14868 sfmmu_unlink_scd_from_regions(sf_srd_t *srdp, sf_scd_t *scdp)
15016 14869 {
15017 14870 uint_t rid;
15018 14871 uint_t i;
15019 14872 uint_t j;
15020 14873 ulong_t w;
15021 14874 sf_region_t *rgnp;
15022 14875 sfmmu_t *scsfmmup;
15023 14876
15024 14877 scsfmmup = scdp->scd_sfmmup;
15025 14878 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) {
15026 14879 if ((w = scdp->scd_region_map.bitmap[i]) == 0) {
15027 14880 continue;
15028 14881 }
15029 14882 j = 0;
15030 14883 while (w) {
15031 14884 if (!(w & 0x1)) {
15032 14885 j++;
15033 14886 w >>= 1;
15034 14887 continue;
15035 14888 }
15036 14889 rid = (i << BT_ULSHIFT) | j;
15037 14890 j++;
15038 14891 w >>= 1;
15039 14892
15040 14893 if (rid < SFMMU_MAX_HME_REGIONS) {
15041 14894 rgnp = srdp->srd_hmergnp[rid];
15042 14895 ASSERT(rgnp->rgn_id == rid);
15043 14896 ASSERT(rgnp->rgn_refcnt > 0);
15044 14897 sfmmu_unlink_from_hmeregion(scsfmmup,
15045 14898 rgnp);
15046 14899
15047 14900 } else {
15048 14901 sfmmu_t *ism_hatid = NULL;
15049 14902 ism_ment_t *ism_ment;
15050 14903 rid -= SFMMU_MAX_HME_REGIONS;
15051 14904 rgnp = srdp->srd_ismrgnp[rid];
15052 14905 ASSERT(rgnp->rgn_id == rid);
15053 14906 ASSERT(rgnp->rgn_refcnt > 0);
15054 14907
15055 14908 ism_hatid = (sfmmu_t *)rgnp->rgn_obj;
15056 14909 ASSERT(ism_hatid->sfmmu_ismhat);
15057 14910 ism_ment = &scdp->scd_ism_links[rid];
15058 14911 ASSERT(ism_ment->iment_hat == scdp->scd_sfmmup);
15059 14912 ASSERT(ism_ment->iment_base_va ==
15060 14913 rgnp->rgn_saddr);
15061 14914 mutex_enter(&ism_mlist_lock);
15062 14915 iment_sub(ism_ment, ism_hatid);
15063 14916 mutex_exit(&ism_mlist_lock);
15064 14917
15065 14918 }
15066 14919 }
15067 14920 }
15068 14921 }
15069 14922 /*
15070 14923 * Allocates and initialises a new SCD structure, this is called with
15071 14924 * the srd_scd_mutex held and returns with the reference count
15072 14925 * initialised to 1.
15073 14926 */
15074 14927 static sf_scd_t *
15075 14928 sfmmu_alloc_scd(sf_srd_t *srdp, sf_region_map_t *new_map)
15076 14929 {
15077 14930 sf_scd_t *new_scdp;
15078 14931 sfmmu_t *scsfmmup;
15079 14932 int i;
15080 14933
15081 14934 ASSERT(MUTEX_HELD(&srdp->srd_scd_mutex));
15082 14935 new_scdp = kmem_cache_alloc(scd_cache, KM_SLEEP);
15083 14936
15084 14937 scsfmmup = kmem_cache_alloc(sfmmuid_cache, KM_SLEEP);
15085 14938 new_scdp->scd_sfmmup = scsfmmup;
15086 14939 scsfmmup->sfmmu_srdp = srdp;
15087 14940 scsfmmup->sfmmu_scdp = new_scdp;
15088 14941 scsfmmup->sfmmu_tsb0_4minflcnt = 0;
15089 14942 scsfmmup->sfmmu_scdhat = 1;
15090 14943 CPUSET_ALL(scsfmmup->sfmmu_cpusran);
15091 14944 bzero(scsfmmup->sfmmu_hmeregion_links, SFMMU_L1_HMERLINKS_SIZE);
15092 14945
15093 14946 ASSERT(max_mmu_ctxdoms > 0);
15094 14947 for (i = 0; i < max_mmu_ctxdoms; i++) {
15095 14948 scsfmmup->sfmmu_ctxs[i].cnum = INVALID_CONTEXT;
15096 14949 scsfmmup->sfmmu_ctxs[i].gnum = 0;
15097 14950 }
15098 14951
15099 14952 for (i = 0; i < MMU_PAGE_SIZES; i++) {
15100 14953 new_scdp->scd_rttecnt[i] = 0;
15101 14954 }
15102 14955
15103 14956 new_scdp->scd_region_map = *new_map;
15104 14957 new_scdp->scd_refcnt = 1;
15105 14958 if (sfmmu_alloc_scd_tsbs(srdp, new_scdp) != TSB_SUCCESS) {
15106 14959 kmem_cache_free(scd_cache, new_scdp);
15107 14960 kmem_cache_free(sfmmuid_cache, scsfmmup);
15108 14961 return (NULL);
15109 14962 }
15110 14963 if (&mmu_init_scd) {
15111 14964 mmu_init_scd(new_scdp);
15112 14965 }
15113 14966 return (new_scdp);
15114 14967 }
15115 14968
15116 14969 /*
15117 14970 * The first phase of a process joining an SCD. The hat structure is
15118 14971 * linked to the SCD queue and then the HAT_JOIN_SCD sfmmu flag is set
15119 14972 * and a cross-call with context invalidation is used to cause the
15120 14973 * remaining work to be carried out in the sfmmu_tsbmiss_exception()
15121 14974 * routine.
15122 14975 */
15123 14976 static void
15124 14977 sfmmu_join_scd(sf_scd_t *scdp, sfmmu_t *sfmmup)
15125 14978 {
15126 14979 hatlock_t *hatlockp;
15127 14980 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
15128 14981 int i;
15129 14982 sf_scd_t *old_scdp;
15130 14983
15131 14984 ASSERT(srdp != NULL);
15132 14985 ASSERT(scdp != NULL);
15133 14986 ASSERT(scdp->scd_refcnt > 0);
15134 14987 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
15135 14988
15136 14989 if ((old_scdp = sfmmup->sfmmu_scdp) != NULL) {
15137 14990 ASSERT(old_scdp != scdp);
15138 14991
15139 14992 mutex_enter(&old_scdp->scd_mutex);
15140 14993 sfmmu_from_scd_list(&old_scdp->scd_sf_list, sfmmup);
15141 14994 mutex_exit(&old_scdp->scd_mutex);
15142 14995 /*
15143 14996 * sfmmup leaves the old scd. Update sfmmu_ttecnt to
15144 14997 * include the shme rgn ttecnt for rgns that
15145 14998 * were in the old SCD
15146 14999 */
15147 15000 for (i = 0; i < mmu_page_sizes; i++) {
15148 15001 ASSERT(sfmmup->sfmmu_scdrttecnt[i] ==
15149 15002 old_scdp->scd_rttecnt[i]);
15150 15003 atomic_add_long(&sfmmup->sfmmu_ttecnt[i],
15151 15004 sfmmup->sfmmu_scdrttecnt[i]);
15152 15005 }
15153 15006 }
15154 15007
15155 15008 /*
15156 15009 * Move sfmmu to the scd lists.
15157 15010 */
15158 15011 mutex_enter(&scdp->scd_mutex);
15159 15012 sfmmu_to_scd_list(&scdp->scd_sf_list, sfmmup);
15160 15013 mutex_exit(&scdp->scd_mutex);
15161 15014 SF_SCD_INCR_REF(scdp);
15162 15015
15163 15016 hatlockp = sfmmu_hat_enter(sfmmup);
15164 15017 /*
15165 15018 * For a multi-thread process, we must stop
15166 15019 * all the other threads before joining the scd.
15167 15020 */
15168 15021
15169 15022 SFMMU_FLAGS_SET(sfmmup, HAT_JOIN_SCD);
15170 15023
15171 15024 sfmmu_invalidate_ctx(sfmmup);
15172 15025 sfmmup->sfmmu_scdp = scdp;
15173 15026
15174 15027 /*
15175 15028 * Copy scd_rttecnt into sfmmup's sfmmu_scdrttecnt, and update
15176 15029 * sfmmu_ttecnt to not include the rgn ttecnt just joined in SCD.
15177 15030 */
15178 15031 for (i = 0; i < mmu_page_sizes; i++) {
15179 15032 sfmmup->sfmmu_scdrttecnt[i] = scdp->scd_rttecnt[i];
15180 15033 ASSERT(sfmmup->sfmmu_ttecnt[i] >= scdp->scd_rttecnt[i]);
15181 15034 atomic_add_long(&sfmmup->sfmmu_ttecnt[i],
15182 15035 -sfmmup->sfmmu_scdrttecnt[i]);
15183 15036 }
15184 15037 /* update tsb0 inflation count */
15185 15038 if (old_scdp != NULL) {
15186 15039 sfmmup->sfmmu_tsb0_4minflcnt +=
15187 15040 old_scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt;
15188 15041 }
15189 15042 ASSERT(sfmmup->sfmmu_tsb0_4minflcnt >=
15190 15043 scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt);
15191 15044 sfmmup->sfmmu_tsb0_4minflcnt -= scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt;
15192 15045
15193 15046 sfmmu_hat_exit(hatlockp);
15194 15047
15195 15048 if (old_scdp != NULL) {
15196 15049 SF_SCD_DECR_REF(srdp, old_scdp);
15197 15050 }
15198 15051
15199 15052 }
15200 15053
15201 15054 /*
15202 15055 * This routine is called by a process to become part of an SCD. It is called
15203 15056 * from sfmmu_tsbmiss_exception() once most of the initial work has been
15204 15057 * done by sfmmu_join_scd(). This routine must not drop the hat lock.
15205 15058 */
15206 15059 static void
15207 15060 sfmmu_finish_join_scd(sfmmu_t *sfmmup)
15208 15061 {
15209 15062 struct tsb_info *tsbinfop;
15210 15063
15211 15064 ASSERT(sfmmu_hat_lock_held(sfmmup));
15212 15065 ASSERT(sfmmup->sfmmu_scdp != NULL);
15213 15066 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD));
15214 15067 ASSERT(!SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY));
15215 15068 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ALLCTX_INVALID));
15216 15069
15217 15070 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL;
15218 15071 tsbinfop = tsbinfop->tsb_next) {
15219 15072 if (tsbinfop->tsb_flags & TSB_SWAPPED) {
15220 15073 continue;
15221 15074 }
15222 15075 ASSERT(!(tsbinfop->tsb_flags & TSB_RELOC_FLAG));
15223 15076
15224 15077 sfmmu_inv_tsb(tsbinfop->tsb_va,
15225 15078 TSB_BYTES(tsbinfop->tsb_szc));
15226 15079 }
15227 15080
15228 15081 /* Set HAT_CTX1_FLAG for all SCD ISMs */
15229 15082 sfmmu_ism_hatflags(sfmmup, 1);
15230 15083
15231 15084 SFMMU_STAT(sf_join_scd);
15232 15085 }
15233 15086
15234 15087 /*
15235 15088 * This routine is called in order to check if there is an SCD which matches
15236 15089 * the process's region map if not then a new SCD may be created.
15237 15090 */
15238 15091 static void
15239 15092 sfmmu_find_scd(sfmmu_t *sfmmup)
15240 15093 {
15241 15094 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
15242 15095 sf_scd_t *scdp, *new_scdp;
15243 15096 int ret;
15244 15097
15245 15098 ASSERT(srdp != NULL);
15246 15099 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
15247 15100
15248 15101 mutex_enter(&srdp->srd_scd_mutex);
15249 15102 for (scdp = srdp->srd_scdp; scdp != NULL;
15250 15103 scdp = scdp->scd_next) {
15251 15104 SF_RGNMAP_EQUAL(&scdp->scd_region_map,
15252 15105 &sfmmup->sfmmu_region_map, ret);
15253 15106 if (ret == 1) {
15254 15107 SF_SCD_INCR_REF(scdp);
15255 15108 mutex_exit(&srdp->srd_scd_mutex);
15256 15109 sfmmu_join_scd(scdp, sfmmup);
15257 15110 ASSERT(scdp->scd_refcnt >= 2);
15258 15111 atomic_add_32((volatile uint32_t *)
15259 15112 &scdp->scd_refcnt, -1);
15260 15113 return;
15261 15114 } else {
15262 15115 /*
15263 15116 * If the sfmmu region map is a subset of the scd
15264 15117 * region map, then the assumption is that this process
15265 15118 * will continue attaching to ISM segments until the
15266 15119 * region maps are equal.
15267 15120 */
15268 15121 SF_RGNMAP_IS_SUBSET(&scdp->scd_region_map,
15269 15122 &sfmmup->sfmmu_region_map, ret);
15270 15123 if (ret == 1) {
15271 15124 mutex_exit(&srdp->srd_scd_mutex);
15272 15125 return;
15273 15126 }
15274 15127 }
15275 15128 }
15276 15129
15277 15130 ASSERT(scdp == NULL);
15278 15131 /*
15279 15132 * No matching SCD has been found, create a new one.
15280 15133 */
15281 15134 if ((new_scdp = sfmmu_alloc_scd(srdp, &sfmmup->sfmmu_region_map)) ==
15282 15135 NULL) {
15283 15136 mutex_exit(&srdp->srd_scd_mutex);
15284 15137 return;
15285 15138 }
15286 15139
15287 15140 /*
15288 15141 * sfmmu_alloc_scd() returns with a ref count of 1 on the scd.
15289 15142 */
15290 15143
15291 15144 /* Set scd_rttecnt for shme rgns in SCD */
15292 15145 sfmmu_set_scd_rttecnt(srdp, new_scdp);
15293 15146
15294 15147 /*
15295 15148 * Link scd onto srd_scdp list and scd sfmmu onto region/iment lists.
15296 15149 */
15297 15150 sfmmu_link_scd_to_regions(srdp, new_scdp);
15298 15151 sfmmu_add_scd(&srdp->srd_scdp, new_scdp);
15299 15152 SFMMU_STAT_ADD(sf_create_scd, 1);
15300 15153
15301 15154 mutex_exit(&srdp->srd_scd_mutex);
15302 15155 sfmmu_join_scd(new_scdp, sfmmup);
15303 15156 ASSERT(new_scdp->scd_refcnt >= 2);
15304 15157 atomic_add_32((volatile uint32_t *)&new_scdp->scd_refcnt, -1);
15305 15158 }
15306 15159
15307 15160 /*
15308 15161 * This routine is called by a process to remove itself from an SCD. It is
15309 15162 * either called when the processes has detached from a segment or from
15310 15163 * hat_free_start() as a result of calling exit.
15311 15164 */
15312 15165 static void
15313 15166 sfmmu_leave_scd(sfmmu_t *sfmmup, uchar_t r_type)
15314 15167 {
15315 15168 sf_scd_t *scdp = sfmmup->sfmmu_scdp;
15316 15169 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
15317 15170 hatlock_t *hatlockp = TSB_HASH(sfmmup);
15318 15171 int i;
15319 15172
15320 15173 ASSERT(scdp != NULL);
15321 15174 ASSERT(srdp != NULL);
15322 15175
15323 15176 if (sfmmup->sfmmu_free) {
15324 15177 /*
15325 15178 * If the process is part of an SCD the sfmmu is unlinked
15326 15179 * from scd_sf_list.
15327 15180 */
15328 15181 mutex_enter(&scdp->scd_mutex);
15329 15182 sfmmu_from_scd_list(&scdp->scd_sf_list, sfmmup);
15330 15183 mutex_exit(&scdp->scd_mutex);
15331 15184 /*
15332 15185 * Update sfmmu_ttecnt to include the rgn ttecnt for rgns that
15333 15186 * are about to leave the SCD
15334 15187 */
15335 15188 for (i = 0; i < mmu_page_sizes; i++) {
15336 15189 ASSERT(sfmmup->sfmmu_scdrttecnt[i] ==
15337 15190 scdp->scd_rttecnt[i]);
15338 15191 atomic_add_long(&sfmmup->sfmmu_ttecnt[i],
15339 15192 sfmmup->sfmmu_scdrttecnt[i]);
15340 15193 sfmmup->sfmmu_scdrttecnt[i] = 0;
15341 15194 }
15342 15195 sfmmup->sfmmu_scdp = NULL;
15343 15196
15344 15197 SF_SCD_DECR_REF(srdp, scdp);
15345 15198 return;
15346 15199 }
15347 15200
15348 15201 ASSERT(r_type != SFMMU_REGION_ISM ||
15349 15202 SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY));
15350 15203 ASSERT(scdp->scd_refcnt);
15351 15204 ASSERT(!sfmmup->sfmmu_free);
15352 15205 ASSERT(sfmmu_hat_lock_held(sfmmup));
15353 15206 ASSERT(AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
15354 15207
15355 15208 /*
15356 15209 * Wait for ISM maps to be updated.
15357 15210 */
15358 15211 if (r_type != SFMMU_REGION_ISM) {
15359 15212 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY) &&
15360 15213 sfmmup->sfmmu_scdp != NULL) {
15361 15214 cv_wait(&sfmmup->sfmmu_tsb_cv,
15362 15215 HATLOCK_MUTEXP(hatlockp));
15363 15216 }
15364 15217
15365 15218 if (sfmmup->sfmmu_scdp == NULL) {
15366 15219 sfmmu_hat_exit(hatlockp);
15367 15220 return;
15368 15221 }
15369 15222 SFMMU_FLAGS_SET(sfmmup, HAT_ISMBUSY);
15370 15223 }
15371 15224
15372 15225 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)) {
15373 15226 SFMMU_FLAGS_CLEAR(sfmmup, HAT_JOIN_SCD);
15374 15227 /*
15375 15228 * Since HAT_JOIN_SCD was set our context
15376 15229 * is still invalid.
15377 15230 */
15378 15231 } else {
15379 15232 /*
15380 15233 * For a multi-thread process, we must stop
15381 15234 * all the other threads before leaving the scd.
15382 15235 */
15383 15236
15384 15237 sfmmu_invalidate_ctx(sfmmup);
15385 15238 }
15386 15239
15387 15240 /* Clear all the rid's for ISM, delete flags, etc */
15388 15241 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY));
15389 15242 sfmmu_ism_hatflags(sfmmup, 0);
15390 15243
15391 15244 /*
15392 15245 * Update sfmmu_ttecnt to include the rgn ttecnt for rgns that
15393 15246 * are in SCD before this sfmmup leaves the SCD.
15394 15247 */
15395 15248 for (i = 0; i < mmu_page_sizes; i++) {
15396 15249 ASSERT(sfmmup->sfmmu_scdrttecnt[i] ==
15397 15250 scdp->scd_rttecnt[i]);
15398 15251 atomic_add_long(&sfmmup->sfmmu_ttecnt[i],
15399 15252 sfmmup->sfmmu_scdrttecnt[i]);
15400 15253 sfmmup->sfmmu_scdrttecnt[i] = 0;
15401 15254 /* update ismttecnt to include SCD ism before hat leaves SCD */
15402 15255 sfmmup->sfmmu_ismttecnt[i] += sfmmup->sfmmu_scdismttecnt[i];
15403 15256 sfmmup->sfmmu_scdismttecnt[i] = 0;
15404 15257 }
15405 15258 /* update tsb0 inflation count */
15406 15259 sfmmup->sfmmu_tsb0_4minflcnt += scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt;
15407 15260
15408 15261 if (r_type != SFMMU_REGION_ISM) {
15409 15262 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ISMBUSY);
15410 15263 }
15411 15264 sfmmup->sfmmu_scdp = NULL;
15412 15265
15413 15266 sfmmu_hat_exit(hatlockp);
15414 15267
15415 15268 /*
15416 15269 * Unlink sfmmu from scd_sf_list this can be done without holding
15417 15270 * the hat lock as we hold the sfmmu_as lock which prevents
15418 15271 * hat_join_region from adding this thread to the scd again. Other
15419 15272 * threads check if sfmmu_scdp is NULL under hat lock and if it's NULL
15420 15273 * they won't get here, since sfmmu_leave_scd() clears sfmmu_scdp
15421 15274 * while holding the hat lock.
15422 15275 */
15423 15276 mutex_enter(&scdp->scd_mutex);
15424 15277 sfmmu_from_scd_list(&scdp->scd_sf_list, sfmmup);
15425 15278 mutex_exit(&scdp->scd_mutex);
15426 15279 SFMMU_STAT(sf_leave_scd);
15427 15280
15428 15281 SF_SCD_DECR_REF(srdp, scdp);
15429 15282 hatlockp = sfmmu_hat_enter(sfmmup);
15430 15283
15431 15284 }
15432 15285
15433 15286 /*
15434 15287 * Unlink and free up an SCD structure with a reference count of 0.
15435 15288 */
15436 15289 static void
15437 15290 sfmmu_destroy_scd(sf_srd_t *srdp, sf_scd_t *scdp, sf_region_map_t *scd_rmap)
15438 15291 {
15439 15292 sfmmu_t *scsfmmup;
15440 15293 sf_scd_t *sp;
15441 15294 hatlock_t *shatlockp;
15442 15295 int i, ret;
15443 15296
15444 15297 mutex_enter(&srdp->srd_scd_mutex);
15445 15298 for (sp = srdp->srd_scdp; sp != NULL; sp = sp->scd_next) {
15446 15299 if (sp == scdp)
15447 15300 break;
15448 15301 }
15449 15302 if (sp == NULL || sp->scd_refcnt) {
15450 15303 mutex_exit(&srdp->srd_scd_mutex);
15451 15304 return;
15452 15305 }
15453 15306
15454 15307 /*
15455 15308 * It is possible that the scd has been freed and reallocated with a
15456 15309 * different region map while we've been waiting for the srd_scd_mutex.
15457 15310 */
15458 15311 SF_RGNMAP_EQUAL(scd_rmap, &sp->scd_region_map, ret);
15459 15312 if (ret != 1) {
15460 15313 mutex_exit(&srdp->srd_scd_mutex);
15461 15314 return;
15462 15315 }
15463 15316
15464 15317 ASSERT(scdp->scd_sf_list == NULL);
15465 15318 /*
15466 15319 * Unlink scd from srd_scdp list.
15467 15320 */
15468 15321 sfmmu_remove_scd(&srdp->srd_scdp, scdp);
15469 15322 mutex_exit(&srdp->srd_scd_mutex);
15470 15323
15471 15324 sfmmu_unlink_scd_from_regions(srdp, scdp);
15472 15325
15473 15326 /* Clear shared context tsb and release ctx */
15474 15327 scsfmmup = scdp->scd_sfmmup;
15475 15328
15476 15329 /*
15477 15330 * create a barrier so that scd will not be destroyed
15478 15331 * if other thread still holds the same shared hat lock.
15479 15332 * E.g., sfmmu_tsbmiss_exception() needs to acquire the
15480 15333 * shared hat lock before checking the shared tsb reloc flag.
15481 15334 */
15482 15335 shatlockp = sfmmu_hat_enter(scsfmmup);
15483 15336 sfmmu_hat_exit(shatlockp);
15484 15337
15485 15338 sfmmu_free_scd_tsbs(scsfmmup);
15486 15339
15487 15340 for (i = 0; i < SFMMU_L1_HMERLINKS; i++) {
15488 15341 if (scsfmmup->sfmmu_hmeregion_links[i] != NULL) {
15489 15342 kmem_free(scsfmmup->sfmmu_hmeregion_links[i],
15490 15343 SFMMU_L2_HMERLINKS_SIZE);
15491 15344 scsfmmup->sfmmu_hmeregion_links[i] = NULL;
15492 15345 }
15493 15346 }
15494 15347 kmem_cache_free(sfmmuid_cache, scsfmmup);
15495 15348 kmem_cache_free(scd_cache, scdp);
15496 15349 SFMMU_STAT(sf_destroy_scd);
15497 15350 }
15498 15351
15499 15352 /*
15500 15353 * Modifies the HAT_CTX1_FLAG for each of the ISM segments which correspond to
15501 15354 * bits which are set in the ism_region_map parameter. This flag indicates to
15502 15355 * the tsbmiss handler that mapping for these segments should be loaded using
15503 15356 * the shared context.
15504 15357 */
15505 15358 static void
15506 15359 sfmmu_ism_hatflags(sfmmu_t *sfmmup, int addflag)
15507 15360 {
15508 15361 sf_scd_t *scdp = sfmmup->sfmmu_scdp;
15509 15362 ism_blk_t *ism_blkp;
15510 15363 ism_map_t *ism_map;
15511 15364 int i, rid;
15512 15365
15513 15366 ASSERT(sfmmup->sfmmu_iblk != NULL);
15514 15367 ASSERT(scdp != NULL);
15515 15368 /*
15516 15369 * Note that the caller either set HAT_ISMBUSY flag or checked
15517 15370 * under hat lock that HAT_ISMBUSY was not set by another thread.
15518 15371 */
15519 15372 ASSERT(sfmmu_hat_lock_held(sfmmup));
15520 15373
15521 15374 ism_blkp = sfmmup->sfmmu_iblk;
15522 15375 while (ism_blkp != NULL) {
15523 15376 ism_map = ism_blkp->iblk_maps;
15524 15377 for (i = 0; ism_map[i].imap_ismhat && i < ISM_MAP_SLOTS; i++) {
15525 15378 rid = ism_map[i].imap_rid;
15526 15379 if (rid == SFMMU_INVALID_ISMRID) {
15527 15380 continue;
15528 15381 }
15529 15382 ASSERT(rid >= 0 && rid < SFMMU_MAX_ISM_REGIONS);
15530 15383 if (SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid) &&
15531 15384 addflag) {
15532 15385 ism_map[i].imap_hatflags |=
15533 15386 HAT_CTX1_FLAG;
15534 15387 } else {
15535 15388 ism_map[i].imap_hatflags &=
15536 15389 ~HAT_CTX1_FLAG;
15537 15390 }
15538 15391 }
15539 15392 ism_blkp = ism_blkp->iblk_next;
15540 15393 }
15541 15394 }
15542 15395
15543 15396 static int
15544 15397 sfmmu_srd_lock_held(sf_srd_t *srdp)
15545 15398 {
15546 15399 return (MUTEX_HELD(&srdp->srd_mutex));
15547 15400 }
15548 15401
15549 15402 /* ARGSUSED */
15550 15403 static int
15551 15404 sfmmu_scdcache_constructor(void *buf, void *cdrarg, int kmflags)
15552 15405 {
15553 15406 sf_scd_t *scdp = (sf_scd_t *)buf;
15554 15407
15555 15408 bzero(buf, sizeof (sf_scd_t));
15556 15409 mutex_init(&scdp->scd_mutex, NULL, MUTEX_DEFAULT, NULL);
15557 15410 return (0);
15558 15411 }
15559 15412
15560 15413 /* ARGSUSED */
15561 15414 static void
15562 15415 sfmmu_scdcache_destructor(void *buf, void *cdrarg)
15563 15416 {
15564 15417 sf_scd_t *scdp = (sf_scd_t *)buf;
15565 15418
15566 15419 mutex_destroy(&scdp->scd_mutex);
15567 15420 }
15568 15421
15569 15422 /*
15570 15423 * The listp parameter is a pointer to a list of hmeblks which are partially
15571 15424 * freed as result of calling sfmmu_hblk_hash_rm(), the last phase of the
15572 15425 * freeing process is to cross-call all cpus to ensure that there are no
15573 15426 * remaining cached references.
15574 15427 *
15575 15428 * If the local generation number is less than the global then we can free
15576 15429 * hmeblks which are already on the pending queue as another cpu has completed
15577 15430 * the cross-call.
15578 15431 *
15579 15432 * We cross-call to make sure that there are no threads on other cpus accessing
15580 15433 * these hmblks and then complete the process of freeing them under the
15581 15434 * following conditions:
15582 15435 * The total number of pending hmeblks is greater than the threshold
15583 15436 * The reserve list has fewer than HBLK_RESERVE_CNT hmeblks
15584 15437 * It is at least 1 second since the last time we cross-called
15585 15438 *
15586 15439 * Otherwise, we add the hmeblks to the per-cpu pending queue.
15587 15440 */
15588 15441 static void
15589 15442 sfmmu_hblks_list_purge(struct hme_blk **listp, int dontfree)
15590 15443 {
15591 15444 struct hme_blk *hblkp, *pr_hblkp = NULL;
15592 15445 int count = 0;
15593 15446 cpuset_t cpuset = cpu_ready_set;
15594 15447 cpu_hme_pend_t *cpuhp;
15595 15448 timestruc_t now;
15596 15449 int one_second_expired = 0;
15597 15450
15598 15451 gethrestime_lasttick(&now);
15599 15452
15600 15453 for (hblkp = *listp; hblkp != NULL; hblkp = hblkp->hblk_next) {
15601 15454 ASSERT(hblkp->hblk_shw_bit == 0);
15602 15455 ASSERT(hblkp->hblk_shared == 0);
15603 15456 count++;
15604 15457 pr_hblkp = hblkp;
15605 15458 }
15606 15459
15607 15460 cpuhp = &cpu_hme_pend[CPU->cpu_seqid];
15608 15461 mutex_enter(&cpuhp->chp_mutex);
15609 15462
15610 15463 if ((cpuhp->chp_count + count) == 0) {
15611 15464 mutex_exit(&cpuhp->chp_mutex);
15612 15465 return;
15613 15466 }
15614 15467
15615 15468 if ((now.tv_sec - cpuhp->chp_timestamp) > 1) {
15616 15469 one_second_expired = 1;
15617 15470 }
15618 15471
15619 15472 if (!dontfree && (freehblkcnt < HBLK_RESERVE_CNT ||
15620 15473 (cpuhp->chp_count + count) > cpu_hme_pend_thresh ||
15621 15474 one_second_expired)) {
15622 15475 /* Append global list to local */
15623 15476 if (pr_hblkp == NULL) {
15624 15477 *listp = cpuhp->chp_listp;
15625 15478 } else {
15626 15479 pr_hblkp->hblk_next = cpuhp->chp_listp;
15627 15480 }
15628 15481 cpuhp->chp_listp = NULL;
15629 15482 cpuhp->chp_count = 0;
15630 15483 cpuhp->chp_timestamp = now.tv_sec;
15631 15484 mutex_exit(&cpuhp->chp_mutex);
15632 15485
15633 15486 kpreempt_disable();
15634 15487 CPUSET_DEL(cpuset, CPU->cpu_id);
15635 15488 xt_sync(cpuset);
15636 15489 xt_sync(cpuset);
15637 15490 kpreempt_enable();
15638 15491
15639 15492 /*
15640 15493 * At this stage we know that no trap handlers on other
15641 15494 * cpus can have references to hmeblks on the list.
15642 15495 */
15643 15496 sfmmu_hblk_free(listp);
15644 15497 } else if (*listp != NULL) {
15645 15498 pr_hblkp->hblk_next = cpuhp->chp_listp;
15646 15499 cpuhp->chp_listp = *listp;
15647 15500 cpuhp->chp_count += count;
15648 15501 *listp = NULL;
15649 15502 mutex_exit(&cpuhp->chp_mutex);
15650 15503 } else {
15651 15504 mutex_exit(&cpuhp->chp_mutex);
15652 15505 }
15653 15506 }
15654 15507
15655 15508 /*
15656 15509 * Add an hmeblk to the the hash list.
15657 15510 */
15658 15511 void
15659 15512 sfmmu_hblk_hash_add(struct hmehash_bucket *hmebp, struct hme_blk *hmeblkp,
15660 15513 uint64_t hblkpa)
15661 15514 {
15662 15515 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
15663 15516 #ifdef DEBUG
15664 15517 if (hmebp->hmeblkp == NULL) {
15665 15518 ASSERT(hmebp->hmeh_nextpa == HMEBLK_ENDPA);
15666 15519 }
15667 15520 #endif /* DEBUG */
15668 15521
15669 15522 hmeblkp->hblk_nextpa = hmebp->hmeh_nextpa;
15670 15523 /*
15671 15524 * Since the TSB miss handler now does not lock the hash chain before
15672 15525 * walking it, make sure that the hmeblks nextpa is globally visible
15673 15526 * before we make the hmeblk globally visible by updating the chain root
15674 15527 * pointer in the hash bucket.
15675 15528 */
15676 15529 membar_producer();
15677 15530 hmebp->hmeh_nextpa = hblkpa;
15678 15531 hmeblkp->hblk_next = hmebp->hmeblkp;
15679 15532 hmebp->hmeblkp = hmeblkp;
15680 15533
15681 15534 }
15682 15535
15683 15536 /*
15684 15537 * This function is the first part of a 2 part process to remove an hmeblk
15685 15538 * from the hash chain. In this phase we unlink the hmeblk from the hash chain
15686 15539 * but leave the next physical pointer unchanged. The hmeblk is then linked onto
15687 15540 * a per-cpu pending list using the virtual address pointer.
15688 15541 *
15689 15542 * TSB miss trap handlers that start after this phase will no longer see
15690 15543 * this hmeblk. TSB miss handlers that still cache this hmeblk in a register
15691 15544 * can still use it for further chain traversal because we haven't yet modifed
15692 15545 * the next physical pointer or freed it.
15693 15546 *
15694 15547 * In the second phase of hmeblk removal we'll issue a barrier xcall before
15695 15548 * we reuse or free this hmeblk. This will make sure all lingering references to
15696 15549 * the hmeblk after first phase disappear before we finally reclaim it.
15697 15550 * This scheme eliminates the need for TSB miss handlers to lock hmeblk chains
15698 15551 * during their traversal.
15699 15552 *
15700 15553 * The hmehash_mutex must be held when calling this function.
15701 15554 *
15702 15555 * Input:
15703 15556 * hmebp - hme hash bucket pointer
15704 15557 * hmeblkp - address of hmeblk to be removed
15705 15558 * pr_hblk - virtual address of previous hmeblkp
15706 15559 * listp - pointer to list of hmeblks linked by virtual address
15707 15560 * free_now flag - indicates that a complete removal from the hash chains
15708 15561 * is necessary.
15709 15562 *
15710 15563 * It is inefficient to use the free_now flag as a cross-call is required to
15711 15564 * remove a single hmeblk from the hash chain but is necessary when hmeblks are
15712 15565 * in short supply.
15713 15566 */
15714 15567 void
15715 15568 sfmmu_hblk_hash_rm(struct hmehash_bucket *hmebp, struct hme_blk *hmeblkp,
15716 15569 struct hme_blk *pr_hblk, struct hme_blk **listp,
15717 15570 int free_now)
15718 15571 {
15719 15572 int shw_size, vshift;
15720 15573 struct hme_blk *shw_hblkp;
15721 15574 uint_t shw_mask, newshw_mask;
15722 15575 caddr_t vaddr;
15723 15576 int size;
15724 15577 cpuset_t cpuset = cpu_ready_set;
15725 15578
15726 15579 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
15727 15580
15728 15581 if (hmebp->hmeblkp == hmeblkp) {
15729 15582 hmebp->hmeh_nextpa = hmeblkp->hblk_nextpa;
15730 15583 hmebp->hmeblkp = hmeblkp->hblk_next;
15731 15584 } else {
15732 15585 pr_hblk->hblk_nextpa = hmeblkp->hblk_nextpa;
15733 15586 pr_hblk->hblk_next = hmeblkp->hblk_next;
15734 15587 }
15735 15588
15736 15589 size = get_hblk_ttesz(hmeblkp);
15737 15590 shw_hblkp = hmeblkp->hblk_shadow;
15738 15591 if (shw_hblkp) {
15739 15592 ASSERT(hblktosfmmu(hmeblkp) != KHATID);
15740 15593 ASSERT(!hmeblkp->hblk_shared);
15741 15594 #ifdef DEBUG
15742 15595 if (mmu_page_sizes == max_mmu_page_sizes) {
15743 15596 ASSERT(size < TTE256M);
15744 15597 } else {
15745 15598 ASSERT(size < TTE4M);
15746 15599 }
15747 15600 #endif /* DEBUG */
15748 15601
15749 15602 shw_size = get_hblk_ttesz(shw_hblkp);
15750 15603 vaddr = (caddr_t)get_hblk_base(hmeblkp);
15751 15604 vshift = vaddr_to_vshift(shw_hblkp->hblk_tag, vaddr, shw_size);
15752 15605 ASSERT(vshift < 8);
15753 15606 /*
15754 15607 * Atomically clear shadow mask bit
15755 15608 */
15756 15609 do {
15757 15610 shw_mask = shw_hblkp->hblk_shw_mask;
15758 15611 ASSERT(shw_mask & (1 << vshift));
15759 15612 newshw_mask = shw_mask & ~(1 << vshift);
15760 15613 newshw_mask = cas32(&shw_hblkp->hblk_shw_mask,
15761 15614 shw_mask, newshw_mask);
15762 15615 } while (newshw_mask != shw_mask);
15763 15616 hmeblkp->hblk_shadow = NULL;
15764 15617 }
15765 15618 hmeblkp->hblk_shw_bit = 0;
15766 15619
15767 15620 if (hmeblkp->hblk_shared) {
15768 15621 #ifdef DEBUG
15769 15622 sf_srd_t *srdp;
15770 15623 sf_region_t *rgnp;
15771 15624 uint_t rid;
15772 15625
15773 15626 srdp = hblktosrd(hmeblkp);
15774 15627 ASSERT(srdp != NULL && srdp->srd_refcnt != 0);
15775 15628 rid = hmeblkp->hblk_tag.htag_rid;
15776 15629 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
15777 15630 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
15778 15631 rgnp = srdp->srd_hmergnp[rid];
15779 15632 ASSERT(rgnp != NULL);
15780 15633 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid);
15781 15634 #endif /* DEBUG */
15782 15635 hmeblkp->hblk_shared = 0;
15783 15636 }
15784 15637 if (free_now) {
15785 15638 kpreempt_disable();
15786 15639 CPUSET_DEL(cpuset, CPU->cpu_id);
15787 15640 xt_sync(cpuset);
15788 15641 xt_sync(cpuset);
15789 15642 kpreempt_enable();
15790 15643
15791 15644 hmeblkp->hblk_nextpa = HMEBLK_ENDPA;
15792 15645 hmeblkp->hblk_next = NULL;
15793 15646 } else {
15794 15647 /* Append hmeblkp to listp for processing later. */
15795 15648 hmeblkp->hblk_next = *listp;
15796 15649 *listp = hmeblkp;
15797 15650 }
15798 15651 }
15799 15652
15800 15653 /*
15801 15654 * This routine is called when memory is in short supply and returns a free
15802 15655 * hmeblk of the requested size from the cpu pending lists.
15803 15656 */
15804 15657 static struct hme_blk *
15805 15658 sfmmu_check_pending_hblks(int size)
15806 15659 {
15807 15660 int i;
15808 15661 struct hme_blk *hmeblkp = NULL, *last_hmeblkp;
15809 15662 int found_hmeblk;
15810 15663 cpuset_t cpuset = cpu_ready_set;
15811 15664 cpu_hme_pend_t *cpuhp;
15812 15665
15813 15666 /* Flush cpu hblk pending queues */
15814 15667 for (i = 0; i < NCPU; i++) {
15815 15668 cpuhp = &cpu_hme_pend[i];
15816 15669 if (cpuhp->chp_listp != NULL) {
15817 15670 mutex_enter(&cpuhp->chp_mutex);
15818 15671 if (cpuhp->chp_listp == NULL) {
15819 15672 mutex_exit(&cpuhp->chp_mutex);
15820 15673 continue;
15821 15674 }
15822 15675 found_hmeblk = 0;
15823 15676 last_hmeblkp = NULL;
15824 15677 for (hmeblkp = cpuhp->chp_listp; hmeblkp != NULL;
15825 15678 hmeblkp = hmeblkp->hblk_next) {
15826 15679 if (get_hblk_ttesz(hmeblkp) == size) {
15827 15680 if (last_hmeblkp == NULL) {
15828 15681 cpuhp->chp_listp =
15829 15682 hmeblkp->hblk_next;
15830 15683 } else {
15831 15684 last_hmeblkp->hblk_next =
15832 15685 hmeblkp->hblk_next;
15833 15686 }
15834 15687 ASSERT(cpuhp->chp_count > 0);
15835 15688 cpuhp->chp_count--;
15836 15689 found_hmeblk = 1;
15837 15690 break;
15838 15691 } else {
15839 15692 last_hmeblkp = hmeblkp;
15840 15693 }
15841 15694 }
15842 15695 mutex_exit(&cpuhp->chp_mutex);
15843 15696
15844 15697 if (found_hmeblk) {
15845 15698 kpreempt_disable();
15846 15699 CPUSET_DEL(cpuset, CPU->cpu_id);
15847 15700 xt_sync(cpuset);
15848 15701 xt_sync(cpuset);
15849 15702 kpreempt_enable();
15850 15703 return (hmeblkp);
15851 15704 }
15852 15705 }
15853 15706 }
15854 15707 return (NULL);
15855 15708 }
↓ open down ↓ |
5846 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX