Print this page
remove whole-process swapping
Long before Unix supported paging, it used process swapping to reclaim
memory. The code is there and in theory it runs when we get *extremely* low
on memory. In practice, it never runs since the definition of low-on-memory
is antiquated. (XXX: define what antiquated means)
You can check the number of swapout/swapin events with kstats:
$ kstat -p ::vm:swapin ::vm:swapout
remove xhat
The xhat infrastructure was added to support hardware such as the zulu
graphics card - hardware which had on-board MMUs. The VM used the xhat code
to keep the CPU's and Zulu's page tables in-sync. Since the only xhat user
was zulu (which is gone), we can safely remove it simplifying the whole VM
subsystem.
Assorted notes:
- AS_BUSY flag was used solely by xhat
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>
↓ open down ↓ |
73 lines elided |
↑ open up ↑ |
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 -#include <vm/xhat_sfmmu.h>
85 84 #include <sys/fpu/fpusystm.h>
86 85 #include <vm/mach_kpm.h>
87 86 #include <sys/callb.h>
88 87
89 88 #ifdef DEBUG
90 89 #define SFMMU_VALIDATE_HMERID(hat, rid, saddr, len) \
91 90 if (SFMMU_IS_SHMERID_VALID(rid)) { \
92 91 caddr_t _eaddr = (saddr) + (len); \
93 92 sf_srd_t *_srdp; \
94 93 sf_region_t *_rgnp; \
95 94 ASSERT((rid) < SFMMU_MAX_HME_REGIONS); \
96 95 ASSERT(SF_RGNMAP_TEST(hat->sfmmu_hmeregion_map, rid)); \
97 96 ASSERT((hat) != ksfmmup); \
98 97 _srdp = (hat)->sfmmu_srdp; \
99 98 ASSERT(_srdp != NULL); \
100 99 ASSERT(_srdp->srd_refcnt != 0); \
101 100 _rgnp = _srdp->srd_hmergnp[(rid)]; \
102 101 ASSERT(_rgnp != NULL && _rgnp->rgn_id == rid); \
103 102 ASSERT(_rgnp->rgn_refcnt != 0); \
104 103 ASSERT(!(_rgnp->rgn_flags & SFMMU_REGION_FREE)); \
105 104 ASSERT((_rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == \
106 105 SFMMU_REGION_HME); \
107 106 ASSERT((saddr) >= _rgnp->rgn_saddr); \
108 107 ASSERT((saddr) < _rgnp->rgn_saddr + _rgnp->rgn_size); \
109 108 ASSERT(_eaddr > _rgnp->rgn_saddr); \
110 109 ASSERT(_eaddr <= _rgnp->rgn_saddr + _rgnp->rgn_size); \
111 110 }
112 111
113 112 #define SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid) \
114 113 { \
115 114 caddr_t _hsva; \
116 115 caddr_t _heva; \
117 116 caddr_t _rsva; \
118 117 caddr_t _reva; \
119 118 int _ttesz = get_hblk_ttesz(hmeblkp); \
120 119 int _flagtte; \
121 120 ASSERT((srdp)->srd_refcnt != 0); \
122 121 ASSERT((rid) < SFMMU_MAX_HME_REGIONS); \
123 122 ASSERT((rgnp)->rgn_id == rid); \
124 123 ASSERT(!((rgnp)->rgn_flags & SFMMU_REGION_FREE)); \
125 124 ASSERT(((rgnp)->rgn_flags & SFMMU_REGION_TYPE_MASK) == \
126 125 SFMMU_REGION_HME); \
127 126 ASSERT(_ttesz <= (rgnp)->rgn_pgszc); \
128 127 _hsva = (caddr_t)get_hblk_base(hmeblkp); \
129 128 _heva = get_hblk_endaddr(hmeblkp); \
130 129 _rsva = (caddr_t)P2ALIGN( \
131 130 (uintptr_t)(rgnp)->rgn_saddr, HBLK_MIN_BYTES); \
132 131 _reva = (caddr_t)P2ROUNDUP( \
133 132 (uintptr_t)((rgnp)->rgn_saddr + (rgnp)->rgn_size), \
134 133 HBLK_MIN_BYTES); \
135 134 ASSERT(_hsva >= _rsva); \
136 135 ASSERT(_hsva < _reva); \
137 136 ASSERT(_heva > _rsva); \
138 137 ASSERT(_heva <= _reva); \
139 138 _flagtte = (_ttesz < HBLK_MIN_TTESZ) ? HBLK_MIN_TTESZ : \
140 139 _ttesz; \
141 140 ASSERT(rgnp->rgn_hmeflags & (0x1 << _flagtte)); \
142 141 }
143 142
144 143 #else /* DEBUG */
145 144 #define SFMMU_VALIDATE_HMERID(hat, rid, addr, len)
146 145 #define SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid)
147 146 #endif /* DEBUG */
148 147
149 148 #if defined(SF_ERRATA_57)
150 149 extern caddr_t errata57_limit;
151 150 #endif
152 151
153 152 #define HME8BLK_SZ_RND ((roundup(HME8BLK_SZ, sizeof (int64_t))) / \
154 153 (sizeof (int64_t)))
155 154 #define HBLK_RESERVE ((struct hme_blk *)hblk_reserve)
156 155
157 156 #define HBLK_RESERVE_CNT 128
158 157 #define HBLK_RESERVE_MIN 20
159 158
160 159 static struct hme_blk *freehblkp;
161 160 static kmutex_t freehblkp_lock;
162 161 static int freehblkcnt;
163 162
164 163 static int64_t hblk_reserve[HME8BLK_SZ_RND];
165 164 static kmutex_t hblk_reserve_lock;
166 165 static kthread_t *hblk_reserve_thread;
167 166
168 167 static nucleus_hblk8_info_t nucleus_hblk8;
169 168 static nucleus_hblk1_info_t nucleus_hblk1;
170 169
171 170 /*
172 171 * Data to manage per-cpu hmeblk pending queues, hmeblks are queued here
173 172 * after the initial phase of removing an hmeblk from the hash chain, see
174 173 * the detailed comment in sfmmu_hblk_hash_rm() for further details.
175 174 */
176 175 static cpu_hme_pend_t *cpu_hme_pend;
177 176 static uint_t cpu_hme_pend_thresh;
178 177 /*
179 178 * SFMMU specific hat functions
180 179 */
181 180 void hat_pagecachectl(struct page *, int);
182 181
183 182 /* flags for hat_pagecachectl */
184 183 #define HAT_CACHE 0x1
185 184 #define HAT_UNCACHE 0x2
186 185 #define HAT_TMPNC 0x4
187 186
188 187 /*
189 188 * Flag to allow the creation of non-cacheable translations
190 189 * to system memory. It is off by default. At the moment this
191 190 * flag is used by the ecache error injector. The error injector
192 191 * will turn it on when creating such a translation then shut it
193 192 * off when it's finished.
194 193 */
195 194
196 195 int sfmmu_allow_nc_trans = 0;
197 196
198 197 /*
199 198 * Flag to disable large page support.
200 199 * value of 1 => disable all large pages.
201 200 * bits 1, 2, and 3 are to disable 64K, 512K and 4M pages respectively.
202 201 *
203 202 * For example, use the value 0x4 to disable 512K pages.
204 203 *
205 204 */
206 205 #define LARGE_PAGES_OFF 0x1
207 206
208 207 /*
209 208 * The disable_large_pages and disable_ism_large_pages variables control
210 209 * hat_memload_array and the page sizes to be used by ISM and the kernel.
211 210 *
212 211 * The disable_auto_data_large_pages and disable_auto_text_large_pages variables
213 212 * are only used to control which OOB pages to use at upper VM segment creation
214 213 * time, and are set in hat_init_pagesizes and used in the map_pgsz* routines.
215 214 * Their values may come from platform or CPU specific code to disable page
216 215 * sizes that should not be used.
217 216 *
218 217 * WARNING: 512K pages are currently not supported for ISM/DISM.
219 218 */
220 219 uint_t disable_large_pages = 0;
221 220 uint_t disable_ism_large_pages = (1 << TTE512K);
222 221 uint_t disable_auto_data_large_pages = 0;
223 222 uint_t disable_auto_text_large_pages = 0;
224 223
225 224 /*
226 225 * Private sfmmu data structures for hat management
227 226 */
228 227 static struct kmem_cache *sfmmuid_cache;
229 228 static struct kmem_cache *mmuctxdom_cache;
230 229
231 230 /*
232 231 * Private sfmmu data structures for tsb management
233 232 */
234 233 static struct kmem_cache *sfmmu_tsbinfo_cache;
235 234 static struct kmem_cache *sfmmu_tsb8k_cache;
236 235 static struct kmem_cache *sfmmu_tsb_cache[NLGRPS_MAX];
237 236 static vmem_t *kmem_bigtsb_arena;
238 237 static vmem_t *kmem_tsb_arena;
239 238
240 239 /*
241 240 * sfmmu static variables for hmeblk resource management.
242 241 */
243 242 static vmem_t *hat_memload1_arena; /* HAT translation arena for sfmmu1_cache */
244 243 static struct kmem_cache *sfmmu8_cache;
245 244 static struct kmem_cache *sfmmu1_cache;
246 245 static struct kmem_cache *pa_hment_cache;
247 246
248 247 static kmutex_t ism_mlist_lock; /* mutex for ism mapping list */
249 248 /*
250 249 * private data for ism
251 250 */
252 251 static struct kmem_cache *ism_blk_cache;
253 252 static struct kmem_cache *ism_ment_cache;
254 253 #define ISMID_STARTADDR NULL
255 254
256 255 /*
257 256 * Region management data structures and function declarations.
258 257 */
259 258
260 259 static void sfmmu_leave_srd(sfmmu_t *);
261 260 static int sfmmu_srdcache_constructor(void *, void *, int);
262 261 static void sfmmu_srdcache_destructor(void *, void *);
263 262 static int sfmmu_rgncache_constructor(void *, void *, int);
264 263 static void sfmmu_rgncache_destructor(void *, void *);
265 264 static int sfrgnmap_isnull(sf_region_map_t *);
266 265 static int sfhmergnmap_isnull(sf_hmeregion_map_t *);
267 266 static int sfmmu_scdcache_constructor(void *, void *, int);
268 267 static void sfmmu_scdcache_destructor(void *, void *);
269 268 static void sfmmu_rgn_cb_noop(caddr_t, caddr_t, caddr_t,
270 269 size_t, void *, u_offset_t);
271 270
272 271 static uint_t srd_hashmask = SFMMU_MAX_SRD_BUCKETS - 1;
273 272 static sf_srd_bucket_t *srd_buckets;
274 273 static struct kmem_cache *srd_cache;
275 274 static uint_t srd_rgn_hashmask = SFMMU_MAX_REGION_BUCKETS - 1;
276 275 static struct kmem_cache *region_cache;
277 276 static struct kmem_cache *scd_cache;
278 277
279 278 #ifdef sun4v
280 279 int use_bigtsb_arena = 1;
281 280 #else
282 281 int use_bigtsb_arena = 0;
283 282 #endif
284 283
285 284 /* External /etc/system tunable, for turning on&off the shctx support */
286 285 int disable_shctx = 0;
287 286 /* Internal variable, set by MD if the HW supports shctx feature */
288 287 int shctx_on = 0;
289 288
290 289 #ifdef DEBUG
291 290 static void check_scd_sfmmu_list(sfmmu_t **, sfmmu_t *, int);
292 291 #endif
293 292 static void sfmmu_to_scd_list(sfmmu_t **, sfmmu_t *);
294 293 static void sfmmu_from_scd_list(sfmmu_t **, sfmmu_t *);
295 294
296 295 static sf_scd_t *sfmmu_alloc_scd(sf_srd_t *, sf_region_map_t *);
297 296 static void sfmmu_find_scd(sfmmu_t *);
298 297 static void sfmmu_join_scd(sf_scd_t *, sfmmu_t *);
299 298 static void sfmmu_finish_join_scd(sfmmu_t *);
300 299 static void sfmmu_leave_scd(sfmmu_t *, uchar_t);
301 300 static void sfmmu_destroy_scd(sf_srd_t *, sf_scd_t *, sf_region_map_t *);
302 301 static int sfmmu_alloc_scd_tsbs(sf_srd_t *, sf_scd_t *);
303 302 static void sfmmu_free_scd_tsbs(sfmmu_t *);
304 303 static void sfmmu_tsb_inv_ctx(sfmmu_t *);
305 304 static int find_ism_rid(sfmmu_t *, sfmmu_t *, caddr_t, uint_t *);
306 305 static void sfmmu_ism_hatflags(sfmmu_t *, int);
307 306 static int sfmmu_srd_lock_held(sf_srd_t *);
308 307 static void sfmmu_remove_scd(sf_scd_t **, sf_scd_t *);
309 308 static void sfmmu_add_scd(sf_scd_t **headp, sf_scd_t *);
310 309 static void sfmmu_link_scd_to_regions(sf_srd_t *, sf_scd_t *);
311 310 static void sfmmu_unlink_scd_from_regions(sf_srd_t *, sf_scd_t *);
312 311 static void sfmmu_link_to_hmeregion(sfmmu_t *, sf_region_t *);
313 312 static void sfmmu_unlink_from_hmeregion(sfmmu_t *, sf_region_t *);
314 313
315 314 /*
316 315 * ``hat_lock'' is a hashed mutex lock for protecting sfmmu TSB lists,
317 316 * HAT flags, synchronizing TLB/TSB coherency, and context management.
318 317 * The lock is hashed on the sfmmup since the case where we need to lock
319 318 * all processes is rare but does occur (e.g. we need to unload a shared
320 319 * mapping from all processes using the mapping). We have a lot of buckets,
321 320 * and each slab of sfmmu_t's can use about a quarter of them, giving us
322 321 * a fairly good distribution without wasting too much space and overhead
323 322 * when we have to grab them all.
324 323 */
325 324 #define SFMMU_NUM_LOCK 128 /* must be power of two */
326 325 hatlock_t hat_lock[SFMMU_NUM_LOCK];
327 326
328 327 /*
329 328 * Hash algorithm optimized for a small number of slabs.
330 329 * 7 is (highbit((sizeof sfmmu_t)) - 1)
331 330 * This hash algorithm is based upon the knowledge that sfmmu_t's come from a
332 331 * kmem_cache, and thus they will be sequential within that cache. In
333 332 * addition, each new slab will have a different "color" up to cache_maxcolor
334 333 * which will skew the hashing for each successive slab which is allocated.
335 334 * If the size of sfmmu_t changed to a larger size, this algorithm may need
336 335 * to be revisited.
337 336 */
338 337 #define TSB_HASH_SHIFT_BITS (7)
339 338 #define PTR_HASH(x) ((uintptr_t)x >> TSB_HASH_SHIFT_BITS)
340 339
341 340 #ifdef DEBUG
342 341 int tsb_hash_debug = 0;
343 342 #define TSB_HASH(sfmmup) \
344 343 (tsb_hash_debug ? &hat_lock[0] : \
345 344 &hat_lock[PTR_HASH(sfmmup) & (SFMMU_NUM_LOCK-1)])
346 345 #else /* DEBUG */
347 346 #define TSB_HASH(sfmmup) &hat_lock[PTR_HASH(sfmmup) & (SFMMU_NUM_LOCK-1)]
348 347 #endif /* DEBUG */
349 348
350 349
351 350 /* sfmmu_replace_tsb() return codes. */
352 351 typedef enum tsb_replace_rc {
353 352 TSB_SUCCESS,
354 353 TSB_ALLOCFAIL,
355 354 TSB_LOSTRACE,
356 355 TSB_ALREADY_SWAPPED,
357 356 TSB_CANTGROW
358 357 } tsb_replace_rc_t;
359 358
360 359 /*
361 360 * Flags for TSB allocation routines.
362 361 */
363 362 #define TSB_ALLOC 0x01
364 363 #define TSB_FORCEALLOC 0x02
365 364 #define TSB_GROW 0x04
366 365 #define TSB_SHRINK 0x08
367 366 #define TSB_SWAPIN 0x10
368 367
369 368 /*
370 369 * Support for HAT callbacks.
371 370 */
372 371 #define SFMMU_MAX_RELOC_CALLBACKS 10
373 372 int sfmmu_max_cb_id = SFMMU_MAX_RELOC_CALLBACKS;
374 373 static id_t sfmmu_cb_nextid = 0;
375 374 static id_t sfmmu_tsb_cb_id;
376 375 struct sfmmu_callback *sfmmu_cb_table;
377 376
378 377 kmutex_t kpr_mutex;
379 378 kmutex_t kpr_suspendlock;
380 379 kthread_t *kreloc_thread;
381 380
382 381 /*
383 382 * Enable VA->PA translation sanity checking on DEBUG kernels.
384 383 * Disabled by default. This is incompatible with some
385 384 * drivers (error injector, RSM) so if it breaks you get
386 385 * to keep both pieces.
387 386 */
388 387 int hat_check_vtop = 0;
389 388
390 389 /*
391 390 * Private sfmmu routines (prototypes)
392 391 */
393 392 static struct hme_blk *sfmmu_shadow_hcreate(sfmmu_t *, caddr_t, int, uint_t);
394 393 static struct hme_blk *sfmmu_hblk_alloc(sfmmu_t *, caddr_t,
395 394 struct hmehash_bucket *, uint_t, hmeblk_tag, uint_t,
396 395 uint_t);
397 396 static caddr_t sfmmu_hblk_unload(struct hat *, struct hme_blk *, caddr_t,
398 397 caddr_t, demap_range_t *, uint_t);
399 398 static caddr_t sfmmu_hblk_sync(struct hat *, struct hme_blk *, caddr_t,
400 399 caddr_t, int);
401 400 static void sfmmu_hblk_free(struct hme_blk **);
402 401 static void sfmmu_hblks_list_purge(struct hme_blk **, int);
403 402 static uint_t sfmmu_get_free_hblk(struct hme_blk **, uint_t);
404 403 static uint_t sfmmu_put_free_hblk(struct hme_blk *, uint_t);
405 404 static struct hme_blk *sfmmu_hblk_steal(int);
406 405 static int sfmmu_steal_this_hblk(struct hmehash_bucket *,
407 406 struct hme_blk *, uint64_t, struct hme_blk *);
408 407 static caddr_t sfmmu_hblk_unlock(struct hme_blk *, caddr_t, caddr_t);
409 408
410 409 static void hat_do_memload_array(struct hat *, caddr_t, size_t,
411 410 struct page **, uint_t, uint_t, uint_t);
412 411 static void hat_do_memload(struct hat *, caddr_t, struct page *,
413 412 uint_t, uint_t, uint_t);
414 413 static void sfmmu_memload_batchsmall(struct hat *, caddr_t, page_t **,
415 414 uint_t, uint_t, pgcnt_t, uint_t);
416 415 void sfmmu_tteload(struct hat *, tte_t *, caddr_t, page_t *,
417 416 uint_t);
418 417 static int sfmmu_tteload_array(sfmmu_t *, tte_t *, caddr_t, page_t **,
419 418 uint_t, uint_t);
420 419 static struct hmehash_bucket *sfmmu_tteload_acquire_hashbucket(sfmmu_t *,
421 420 caddr_t, int, uint_t);
422 421 static struct hme_blk *sfmmu_tteload_find_hmeblk(sfmmu_t *,
423 422 struct hmehash_bucket *, caddr_t, uint_t, uint_t,
424 423 uint_t);
425 424 static int sfmmu_tteload_addentry(sfmmu_t *, struct hme_blk *, tte_t *,
426 425 caddr_t, page_t **, uint_t, uint_t);
427 426 static void sfmmu_tteload_release_hashbucket(struct hmehash_bucket *);
428 427
429 428 static int sfmmu_pagearray_setup(caddr_t, page_t **, tte_t *, int);
430 429 static pfn_t sfmmu_uvatopfn(caddr_t, sfmmu_t *, tte_t *);
431 430 void sfmmu_memtte(tte_t *, pfn_t, uint_t, int);
432 431 #ifdef VAC
433 432 static void sfmmu_vac_conflict(struct hat *, caddr_t, page_t *);
434 433 static int sfmmu_vacconflict_array(caddr_t, page_t *, int *);
435 434 int tst_tnc(page_t *pp, pgcnt_t);
436 435 void conv_tnc(page_t *pp, int);
437 436 #endif
438 437
439 438 static void sfmmu_get_ctx(sfmmu_t *);
440 439 static void sfmmu_free_sfmmu(sfmmu_t *);
441 440
442 441 static void sfmmu_ttesync(struct hat *, caddr_t, tte_t *, page_t *);
443 442 static void sfmmu_chgattr(struct hat *, caddr_t, size_t, uint_t, int);
444 443
445 444 cpuset_t sfmmu_pageunload(page_t *, struct sf_hment *, int);
446 445 static void hat_pagereload(struct page *, struct page *);
447 446 static cpuset_t sfmmu_pagesync(page_t *, struct sf_hment *, uint_t);
448 447 #ifdef VAC
449 448 void sfmmu_page_cache_array(page_t *, int, int, pgcnt_t);
450 449 static void sfmmu_page_cache(page_t *, int, int, int);
451 450 #endif
452 451
453 452 cpuset_t sfmmu_rgntlb_demap(caddr_t, sf_region_t *,
454 453 struct hme_blk *, int);
455 454 static void sfmmu_tlbcache_demap(caddr_t, sfmmu_t *, struct hme_blk *,
456 455 pfn_t, int, int, int, int);
457 456 static void sfmmu_ismtlbcache_demap(caddr_t, sfmmu_t *, struct hme_blk *,
458 457 pfn_t, int);
459 458 static void sfmmu_tlb_demap(caddr_t, sfmmu_t *, struct hme_blk *, int, int);
460 459 static void sfmmu_tlb_range_demap(demap_range_t *);
461 460 static void sfmmu_invalidate_ctx(sfmmu_t *);
462 461 static void sfmmu_sync_mmustate(sfmmu_t *);
463 462
464 463 static void sfmmu_tsbinfo_setup_phys(struct tsb_info *, pfn_t);
465 464 static int sfmmu_tsbinfo_alloc(struct tsb_info **, int, int, uint_t,
466 465 sfmmu_t *);
467 466 static void sfmmu_tsb_free(struct tsb_info *);
468 467 static void sfmmu_tsbinfo_free(struct tsb_info *);
469 468 static int sfmmu_init_tsbinfo(struct tsb_info *, int, int, uint_t,
470 469 sfmmu_t *);
471 470 static void sfmmu_tsb_chk_reloc(sfmmu_t *, hatlock_t *);
472 471 static void sfmmu_tsb_swapin(sfmmu_t *, hatlock_t *);
473 472 static int sfmmu_select_tsb_szc(pgcnt_t);
474 473 static void sfmmu_mod_tsb(sfmmu_t *, caddr_t, tte_t *, int);
475 474 #define sfmmu_load_tsb(sfmmup, vaddr, tte, szc) \
476 475 sfmmu_mod_tsb(sfmmup, vaddr, tte, szc)
477 476 #define sfmmu_unload_tsb(sfmmup, vaddr, szc) \
478 477 sfmmu_mod_tsb(sfmmup, vaddr, NULL, szc)
479 478 static void sfmmu_copy_tsb(struct tsb_info *, struct tsb_info *);
480 479 static tsb_replace_rc_t sfmmu_replace_tsb(sfmmu_t *, struct tsb_info *, uint_t,
481 480 hatlock_t *, uint_t);
482 481 static void sfmmu_size_tsb(sfmmu_t *, int, uint64_t, uint64_t, int);
483 482
484 483 #ifdef VAC
485 484 void sfmmu_cache_flush(pfn_t, int);
486 485 void sfmmu_cache_flushcolor(int, pfn_t);
487 486 #endif
488 487 static caddr_t sfmmu_hblk_chgattr(sfmmu_t *, struct hme_blk *, caddr_t,
489 488 caddr_t, demap_range_t *, uint_t, int);
490 489
491 490 static uint64_t sfmmu_vtop_attr(uint_t, int mode, tte_t *);
492 491 static uint_t sfmmu_ptov_attr(tte_t *);
493 492 static caddr_t sfmmu_hblk_chgprot(sfmmu_t *, struct hme_blk *, caddr_t,
494 493 caddr_t, demap_range_t *, uint_t);
495 494 static uint_t sfmmu_vtop_prot(uint_t, uint_t *);
496 495 static int sfmmu_idcache_constructor(void *, void *, int);
497 496 static void sfmmu_idcache_destructor(void *, void *);
498 497 static int sfmmu_hblkcache_constructor(void *, void *, int);
499 498 static void sfmmu_hblkcache_destructor(void *, void *);
500 499 static void sfmmu_hblkcache_reclaim(void *);
501 500 static void sfmmu_shadow_hcleanup(sfmmu_t *, struct hme_blk *,
502 501 struct hmehash_bucket *);
503 502 static void sfmmu_hblk_hash_rm(struct hmehash_bucket *, struct hme_blk *,
504 503 struct hme_blk *, struct hme_blk **, int);
505 504 static void sfmmu_hblk_hash_add(struct hmehash_bucket *, struct hme_blk *,
506 505 uint64_t);
507 506 static struct hme_blk *sfmmu_check_pending_hblks(int);
508 507 static void sfmmu_free_hblks(sfmmu_t *, caddr_t, caddr_t, int);
509 508 static void sfmmu_cleanup_rhblk(sf_srd_t *, caddr_t, uint_t, int);
510 509 static void sfmmu_unload_hmeregion_va(sf_srd_t *, uint_t, caddr_t, caddr_t,
511 510 int, caddr_t *);
512 511 static void sfmmu_unload_hmeregion(sf_srd_t *, sf_region_t *);
513 512
514 513 static void sfmmu_rm_large_mappings(page_t *, int);
515 514
516 515 static void hat_lock_init(void);
517 516 static void hat_kstat_init(void);
518 517 static int sfmmu_kstat_percpu_update(kstat_t *ksp, int rw);
519 518 static void sfmmu_set_scd_rttecnt(sf_srd_t *, sf_scd_t *);
520 519 static int sfmmu_is_rgnva(sf_srd_t *, caddr_t, ulong_t, ulong_t);
521 520 static void sfmmu_check_page_sizes(sfmmu_t *, int);
522 521 int fnd_mapping_sz(page_t *);
523 522 static void iment_add(struct ism_ment *, struct hat *);
524 523 static void iment_sub(struct ism_ment *, struct hat *);
525 524 static pgcnt_t ism_tsb_entries(sfmmu_t *, int szc);
526 525 extern void sfmmu_setup_tsbinfo(sfmmu_t *);
527 526 extern void sfmmu_clear_utsbinfo(void);
528 527
529 528 static void sfmmu_ctx_wrap_around(mmu_ctx_t *, boolean_t);
530 529
531 530 extern int vpm_enable;
532 531
533 532 /* kpm globals */
534 533 #ifdef DEBUG
535 534 /*
536 535 * Enable trap level tsbmiss handling
537 536 */
538 537 int kpm_tsbmtl = 1;
539 538
540 539 /*
541 540 * Flush the TLB on kpm mapout. Note: Xcalls are used (again) for the
542 541 * required TLB shootdowns in this case, so handle w/ care. Off by default.
543 542 */
544 543 int kpm_tlb_flush;
545 544 #endif /* DEBUG */
546 545
547 546 static void *sfmmu_vmem_xalloc_aligned_wrapper(vmem_t *, size_t, int);
548 547
549 548 #ifdef DEBUG
550 549 static void sfmmu_check_hblk_flist();
551 550 #endif
552 551
553 552 /*
554 553 * Semi-private sfmmu data structures. Some of them are initialize in
555 554 * startup or in hat_init. Some of them are private but accessed by
556 555 * assembly code or mach_sfmmu.c
557 556 */
558 557 struct hmehash_bucket *uhme_hash; /* user hmeblk hash table */
559 558 struct hmehash_bucket *khme_hash; /* kernel hmeblk hash table */
560 559 uint64_t uhme_hash_pa; /* PA of uhme_hash */
561 560 uint64_t khme_hash_pa; /* PA of khme_hash */
562 561 int uhmehash_num; /* # of buckets in user hash table */
563 562 int khmehash_num; /* # of buckets in kernel hash table */
564 563
565 564 uint_t max_mmu_ctxdoms = 0; /* max context domains in the system */
566 565 mmu_ctx_t **mmu_ctxs_tbl; /* global array of context domains */
567 566 uint64_t mmu_saved_gnum = 0; /* to init incoming MMUs' gnums */
568 567
569 568 #define DEFAULT_NUM_CTXS_PER_MMU 8192
570 569 static uint_t nctxs = DEFAULT_NUM_CTXS_PER_MMU;
571 570
572 571 int cache; /* describes system cache */
573 572
574 573 caddr_t ktsb_base; /* kernel 8k-indexed tsb base address */
575 574 uint64_t ktsb_pbase; /* kernel 8k-indexed tsb phys address */
576 575 int ktsb_szcode; /* kernel 8k-indexed tsb size code */
577 576 int ktsb_sz; /* kernel 8k-indexed tsb size */
578 577
579 578 caddr_t ktsb4m_base; /* kernel 4m-indexed tsb base address */
580 579 uint64_t ktsb4m_pbase; /* kernel 4m-indexed tsb phys address */
581 580 int ktsb4m_szcode; /* kernel 4m-indexed tsb size code */
582 581 int ktsb4m_sz; /* kernel 4m-indexed tsb size */
583 582
584 583 uint64_t kpm_tsbbase; /* kernel seg_kpm 4M TSB base address */
585 584 int kpm_tsbsz; /* kernel seg_kpm 4M TSB size code */
586 585 uint64_t kpmsm_tsbbase; /* kernel seg_kpm 8K TSB base address */
587 586 int kpmsm_tsbsz; /* kernel seg_kpm 8K TSB size code */
588 587
589 588 #ifndef sun4v
590 589 int utsb_dtlb_ttenum = -1; /* index in TLB for utsb locked TTE */
591 590 int utsb4m_dtlb_ttenum = -1; /* index in TLB for 4M TSB TTE */
592 591 int dtlb_resv_ttenum; /* index in TLB of first reserved TTE */
593 592 caddr_t utsb_vabase; /* reserved kernel virtual memory */
594 593 caddr_t utsb4m_vabase; /* for trap handler TSB accesses */
595 594 #endif /* sun4v */
596 595 uint64_t tsb_alloc_bytes = 0; /* bytes allocated to TSBs */
597 596 vmem_t *kmem_tsb_default_arena[NLGRPS_MAX]; /* For dynamic TSBs */
598 597 vmem_t *kmem_bigtsb_default_arena[NLGRPS_MAX]; /* dynamic 256M TSBs */
599 598
600 599 /*
601 600 * Size to use for TSB slabs. Future platforms that support page sizes
602 601 * larger than 4M may wish to change these values, and provide their own
603 602 * assembly macros for building and decoding the TSB base register contents.
604 603 * Note disable_large_pages will override the value set here.
605 604 */
606 605 static uint_t tsb_slab_ttesz = TTE4M;
607 606 size_t tsb_slab_size = MMU_PAGESIZE4M;
608 607 uint_t tsb_slab_shift = MMU_PAGESHIFT4M;
609 608 /* PFN mask for TTE */
610 609 size_t tsb_slab_mask = MMU_PAGEOFFSET4M >> MMU_PAGESHIFT;
611 610
612 611 /*
613 612 * Size to use for TSB slabs. These are used only when 256M tsb arenas
614 613 * exist.
615 614 */
616 615 static uint_t bigtsb_slab_ttesz = TTE256M;
617 616 static size_t bigtsb_slab_size = MMU_PAGESIZE256M;
618 617 static uint_t bigtsb_slab_shift = MMU_PAGESHIFT256M;
619 618 /* 256M page alignment for 8K pfn */
620 619 static size_t bigtsb_slab_mask = MMU_PAGEOFFSET256M >> MMU_PAGESHIFT;
621 620
622 621 /* largest TSB size to grow to, will be smaller on smaller memory systems */
623 622 static int tsb_max_growsize = 0;
624 623
625 624 /*
626 625 * Tunable parameters dealing with TSB policies.
627 626 */
628 627
629 628 /*
630 629 * This undocumented tunable forces all 8K TSBs to be allocated from
631 630 * the kernel heap rather than from the kmem_tsb_default_arena arenas.
632 631 */
633 632 #ifdef DEBUG
634 633 int tsb_forceheap = 0;
635 634 #endif /* DEBUG */
636 635
637 636 /*
638 637 * Decide whether to use per-lgroup arenas, or one global set of
639 638 * TSB arenas. The default is not to break up per-lgroup, since
640 639 * most platforms don't recognize any tangible benefit from it.
641 640 */
642 641 int tsb_lgrp_affinity = 0;
643 642
644 643 /*
645 644 * Used for growing the TSB based on the process RSS.
646 645 * tsb_rss_factor is based on the smallest TSB, and is
647 646 * shifted by the TSB size to determine if we need to grow.
648 647 * The default will grow the TSB if the number of TTEs for
649 648 * this page size exceeds 75% of the number of TSB entries,
650 649 * which should _almost_ eliminate all conflict misses
651 650 * (at the expense of using up lots and lots of memory).
652 651 */
653 652 #define TSB_RSS_FACTOR (TSB_ENTRIES(TSB_MIN_SZCODE) * 0.75)
654 653 #define SFMMU_RSS_TSBSIZE(tsbszc) (tsb_rss_factor << tsbszc)
655 654 #define SELECT_TSB_SIZECODE(pgcnt) ( \
656 655 (enable_tsb_rss_sizing)? sfmmu_select_tsb_szc(pgcnt) : \
657 656 default_tsb_size)
658 657 #define TSB_OK_SHRINK() \
659 658 (tsb_alloc_bytes > tsb_alloc_hiwater || freemem < desfree)
660 659 #define TSB_OK_GROW() \
661 660 (tsb_alloc_bytes < tsb_alloc_hiwater && freemem > desfree)
662 661
663 662 int enable_tsb_rss_sizing = 1;
664 663 int tsb_rss_factor = (int)TSB_RSS_FACTOR;
665 664
666 665 /* which TSB size code to use for new address spaces or if rss sizing off */
667 666 int default_tsb_size = TSB_8K_SZCODE;
668 667
669 668 static uint64_t tsb_alloc_hiwater; /* limit TSB reserved memory */
670 669 uint64_t tsb_alloc_hiwater_factor; /* tsb_alloc_hiwater = physmem / this */
671 670 #define TSB_ALLOC_HIWATER_FACTOR_DEFAULT 32
672 671
673 672 #ifdef DEBUG
674 673 static int tsb_random_size = 0; /* set to 1 to test random tsb sizes on alloc */
675 674 static int tsb_grow_stress = 0; /* if set to 1, keep replacing TSB w/ random */
676 675 static int tsb_alloc_mtbf = 0; /* fail allocation every n attempts */
677 676 static int tsb_alloc_fail_mtbf = 0;
678 677 static int tsb_alloc_count = 0;
679 678 #endif /* DEBUG */
680 679
681 680 /* if set to 1, will remap valid TTEs when growing TSB. */
682 681 int tsb_remap_ttes = 1;
683 682
684 683 /*
685 684 * If we have more than this many mappings, allocate a second TSB.
686 685 * This default is chosen because the I/D fully associative TLBs are
687 686 * assumed to have at least 8 available entries. Platforms with a
688 687 * larger fully-associative TLB could probably override the default.
689 688 */
690 689
691 690 #ifdef sun4v
692 691 int tsb_sectsb_threshold = 0;
693 692 #else
694 693 int tsb_sectsb_threshold = 8;
695 694 #endif
696 695
697 696 /*
698 697 * kstat data
699 698 */
700 699 struct sfmmu_global_stat sfmmu_global_stat;
701 700 struct sfmmu_tsbsize_stat sfmmu_tsbsize_stat;
702 701
703 702 /*
704 703 * Global data
705 704 */
706 705 sfmmu_t *ksfmmup; /* kernel's hat id */
707 706
708 707 #ifdef DEBUG
709 708 static void chk_tte(tte_t *, tte_t *, tte_t *, struct hme_blk *);
710 709 #endif
711 710
712 711 /* sfmmu locking operations */
713 712 static kmutex_t *sfmmu_mlspl_enter(struct page *, int);
714 713 static int sfmmu_mlspl_held(struct page *, int);
715 714
716 715 kmutex_t *sfmmu_page_enter(page_t *);
717 716 void sfmmu_page_exit(kmutex_t *);
718 717 int sfmmu_page_spl_held(struct page *);
719 718
720 719 /* sfmmu internal locking operations - accessed directly */
721 720 static void sfmmu_mlist_reloc_enter(page_t *, page_t *,
722 721 kmutex_t **, kmutex_t **);
723 722 static void sfmmu_mlist_reloc_exit(kmutex_t *, kmutex_t *);
724 723 static hatlock_t *
725 724 sfmmu_hat_enter(sfmmu_t *);
726 725 static hatlock_t *
727 726 sfmmu_hat_tryenter(sfmmu_t *);
728 727 static void sfmmu_hat_exit(hatlock_t *);
729 728 static void sfmmu_hat_lock_all(void);
730 729 static void sfmmu_hat_unlock_all(void);
731 730 static void sfmmu_ismhat_enter(sfmmu_t *, int);
732 731 static void sfmmu_ismhat_exit(sfmmu_t *, int);
733 732
734 733 kpm_hlk_t *kpmp_table;
735 734 uint_t kpmp_table_sz; /* must be a power of 2 */
736 735 uchar_t kpmp_shift;
737 736
738 737 kpm_shlk_t *kpmp_stable;
739 738 uint_t kpmp_stable_sz; /* must be a power of 2 */
740 739
741 740 /*
742 741 * SPL_TABLE_SIZE is 2 * NCPU, but no smaller than 128.
743 742 * SPL_SHIFT is log2(SPL_TABLE_SIZE).
744 743 */
745 744 #if ((2*NCPU_P2) > 128)
746 745 #define SPL_SHIFT ((unsigned)(NCPU_LOG2 + 1))
747 746 #else
748 747 #define SPL_SHIFT 7U
749 748 #endif
750 749 #define SPL_TABLE_SIZE (1U << SPL_SHIFT)
751 750 #define SPL_MASK (SPL_TABLE_SIZE - 1)
752 751
753 752 /*
754 753 * We shift by PP_SHIFT to take care of the low-order 0 bits of a page_t
755 754 * and by multiples of SPL_SHIFT to get as many varied bits as we can.
756 755 */
757 756 #define SPL_INDEX(pp) \
758 757 ((((uintptr_t)(pp) >> PP_SHIFT) ^ \
759 758 ((uintptr_t)(pp) >> (PP_SHIFT + SPL_SHIFT)) ^ \
760 759 ((uintptr_t)(pp) >> (PP_SHIFT + SPL_SHIFT * 2)) ^ \
761 760 ((uintptr_t)(pp) >> (PP_SHIFT + SPL_SHIFT * 3))) & \
762 761 SPL_MASK)
763 762
764 763 #define SPL_HASH(pp) \
765 764 (&sfmmu_page_lock[SPL_INDEX(pp)].pad_mutex)
766 765
767 766 static pad_mutex_t sfmmu_page_lock[SPL_TABLE_SIZE];
768 767
769 768 /* Array of mutexes protecting a page's mapping list and p_nrm field. */
770 769
771 770 #define MML_TABLE_SIZE SPL_TABLE_SIZE
772 771 #define MLIST_HASH(pp) (&mml_table[SPL_INDEX(pp)].pad_mutex)
773 772
774 773 static pad_mutex_t mml_table[MML_TABLE_SIZE];
775 774
776 775 /*
777 776 * hat_unload_callback() will group together callbacks in order
778 777 * to avoid xt_sync() calls. This is the maximum size of the group.
779 778 */
780 779 #define MAX_CB_ADDR 32
781 780
782 781 tte_t hw_tte;
783 782 static ulong_t sfmmu_dmr_maxbit = DMR_MAXBIT;
784 783
785 784 static char *mmu_ctx_kstat_names[] = {
786 785 "mmu_ctx_tsb_exceptions",
787 786 "mmu_ctx_tsb_raise_exception",
788 787 "mmu_ctx_wrap_around",
789 788 };
790 789
791 790 /*
792 791 * Wrapper for vmem_xalloc since vmem_create only allows limited
793 792 * parameters for vm_source_alloc functions. This function allows us
794 793 * to specify alignment consistent with the size of the object being
795 794 * allocated.
796 795 */
797 796 static void *
798 797 sfmmu_vmem_xalloc_aligned_wrapper(vmem_t *vmp, size_t size, int vmflag)
799 798 {
800 799 return (vmem_xalloc(vmp, size, size, 0, 0, NULL, NULL, vmflag));
801 800 }
802 801
803 802 /* Common code for setting tsb_alloc_hiwater. */
804 803 #define SFMMU_SET_TSB_ALLOC_HIWATER(pages) tsb_alloc_hiwater = \
805 804 ptob(pages) / tsb_alloc_hiwater_factor
806 805
807 806 /*
808 807 * Set tsb_max_growsize to allow at most all of physical memory to be mapped by
809 808 * a single TSB. physmem is the number of physical pages so we need physmem 8K
810 809 * TTEs to represent all those physical pages. We round this up by using
811 810 * 1<<highbit(). To figure out which size code to use, remember that the size
812 811 * code is just an amount to shift the smallest TSB size to get the size of
813 812 * this TSB. So we subtract that size, TSB_START_SIZE, from highbit() (or
814 813 * highbit() - 1) to get the size code for the smallest TSB that can represent
815 814 * all of physical memory, while erring on the side of too much.
816 815 *
817 816 * Restrict tsb_max_growsize to make sure that:
818 817 * 1) TSBs can't grow larger than the TSB slab size
819 818 * 2) TSBs can't grow larger than UTSB_MAX_SZCODE.
820 819 */
821 820 #define SFMMU_SET_TSB_MAX_GROWSIZE(pages) { \
822 821 int _i, _szc, _slabszc, _tsbszc; \
823 822 \
824 823 _i = highbit(pages); \
825 824 if ((1 << (_i - 1)) == (pages)) \
826 825 _i--; /* 2^n case, round down */ \
827 826 _szc = _i - TSB_START_SIZE; \
828 827 _slabszc = bigtsb_slab_shift - (TSB_START_SIZE + TSB_ENTRY_SHIFT); \
829 828 _tsbszc = MIN(_szc, _slabszc); \
830 829 tsb_max_growsize = MIN(_tsbszc, UTSB_MAX_SZCODE); \
831 830 }
832 831
833 832 /*
834 833 * Given a pointer to an sfmmu and a TTE size code, return a pointer to the
835 834 * tsb_info which handles that TTE size.
836 835 */
837 836 #define SFMMU_GET_TSBINFO(tsbinfop, sfmmup, tte_szc) { \
838 837 (tsbinfop) = (sfmmup)->sfmmu_tsb; \
839 838 ASSERT(((tsbinfop)->tsb_flags & TSB_SHAREDCTX) || \
840 839 sfmmu_hat_lock_held(sfmmup)); \
841 840 if ((tte_szc) >= TTE4M) { \
842 841 ASSERT((tsbinfop) != NULL); \
843 842 (tsbinfop) = (tsbinfop)->tsb_next; \
844 843 } \
845 844 }
846 845
847 846 /*
848 847 * Macro to use to unload entries from the TSB.
849 848 * It has knowledge of which page sizes get replicated in the TSB
850 849 * and will call the appropriate unload routine for the appropriate size.
851 850 */
852 851 #define SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, ismhat) \
853 852 { \
854 853 int ttesz = get_hblk_ttesz(hmeblkp); \
855 854 if (ttesz == TTE8K || ttesz == TTE4M) { \
856 855 sfmmu_unload_tsb(sfmmup, addr, ttesz); \
857 856 } else { \
858 857 caddr_t sva = ismhat ? addr : \
859 858 (caddr_t)get_hblk_base(hmeblkp); \
860 859 caddr_t eva = sva + get_hblk_span(hmeblkp); \
861 860 ASSERT(addr >= sva && addr < eva); \
862 861 sfmmu_unload_tsb_range(sfmmup, sva, eva, ttesz); \
863 862 } \
864 863 }
865 864
866 865
867 866 /* Update tsb_alloc_hiwater after memory is configured. */
868 867 /*ARGSUSED*/
869 868 static void
870 869 sfmmu_update_post_add(void *arg, pgcnt_t delta_pages)
871 870 {
872 871 /* Assumes physmem has already been updated. */
873 872 SFMMU_SET_TSB_ALLOC_HIWATER(physmem);
874 873 SFMMU_SET_TSB_MAX_GROWSIZE(physmem);
875 874 }
876 875
877 876 /*
878 877 * Update tsb_alloc_hiwater before memory is deleted. We'll do nothing here
879 878 * and update tsb_alloc_hiwater and tsb_max_growsize after the memory is
880 879 * deleted.
881 880 */
882 881 /*ARGSUSED*/
883 882 static int
884 883 sfmmu_update_pre_del(void *arg, pgcnt_t delta_pages)
885 884 {
886 885 return (0);
887 886 }
888 887
889 888 /* Update tsb_alloc_hiwater after memory fails to be unconfigured. */
890 889 /*ARGSUSED*/
891 890 static void
892 891 sfmmu_update_post_del(void *arg, pgcnt_t delta_pages, int cancelled)
893 892 {
894 893 /*
895 894 * Whether the delete was cancelled or not, just go ahead and update
896 895 * tsb_alloc_hiwater and tsb_max_growsize.
897 896 */
898 897 SFMMU_SET_TSB_ALLOC_HIWATER(physmem);
899 898 SFMMU_SET_TSB_MAX_GROWSIZE(physmem);
900 899 }
901 900
902 901 static kphysm_setup_vector_t sfmmu_update_vec = {
903 902 KPHYSM_SETUP_VECTOR_VERSION, /* version */
904 903 sfmmu_update_post_add, /* post_add */
905 904 sfmmu_update_pre_del, /* pre_del */
906 905 sfmmu_update_post_del /* post_del */
907 906 };
908 907
909 908
910 909 /*
911 910 * HME_BLK HASH PRIMITIVES
912 911 */
913 912
914 913 /*
915 914 * Enter a hme on the mapping list for page pp.
916 915 * When large pages are more prevalent in the system we might want to
917 916 * keep the mapping list in ascending order by the hment size. For now,
918 917 * small pages are more frequent, so don't slow it down.
919 918 */
920 919 #define HME_ADD(hme, pp) \
921 920 { \
922 921 ASSERT(sfmmu_mlist_held(pp)); \
923 922 \
924 923 hme->hme_prev = NULL; \
925 924 hme->hme_next = pp->p_mapping; \
926 925 hme->hme_page = pp; \
927 926 if (pp->p_mapping) { \
928 927 ((struct sf_hment *)(pp->p_mapping))->hme_prev = hme;\
929 928 ASSERT(pp->p_share > 0); \
930 929 } else { \
931 930 /* EMPTY */ \
932 931 ASSERT(pp->p_share == 0); \
933 932 } \
934 933 pp->p_mapping = hme; \
935 934 pp->p_share++; \
936 935 }
937 936
938 937 /*
939 938 * Enter a hme on the mapping list for page pp.
940 939 * If we are unmapping a large translation, we need to make sure that the
941 940 * change is reflect in the corresponding bit of the p_index field.
942 941 */
943 942 #define HME_SUB(hme, pp) \
944 943 { \
945 944 ASSERT(sfmmu_mlist_held(pp)); \
946 945 ASSERT(hme->hme_page == pp || IS_PAHME(hme)); \
947 946 \
948 947 if (pp->p_mapping == NULL) { \
949 948 panic("hme_remove - no mappings"); \
950 949 } \
951 950 \
952 951 membar_stst(); /* ensure previous stores finish */ \
953 952 \
954 953 ASSERT(pp->p_share > 0); \
955 954 pp->p_share--; \
956 955 \
957 956 if (hme->hme_prev) { \
958 957 ASSERT(pp->p_mapping != hme); \
959 958 ASSERT(hme->hme_prev->hme_page == pp || \
960 959 IS_PAHME(hme->hme_prev)); \
961 960 hme->hme_prev->hme_next = hme->hme_next; \
962 961 } else { \
963 962 ASSERT(pp->p_mapping == hme); \
964 963 pp->p_mapping = hme->hme_next; \
965 964 ASSERT((pp->p_mapping == NULL) ? \
966 965 (pp->p_share == 0) : 1); \
967 966 } \
968 967 \
969 968 if (hme->hme_next) { \
970 969 ASSERT(hme->hme_next->hme_page == pp || \
971 970 IS_PAHME(hme->hme_next)); \
972 971 hme->hme_next->hme_prev = hme->hme_prev; \
973 972 } \
974 973 \
975 974 /* zero out the entry */ \
976 975 hme->hme_next = NULL; \
977 976 hme->hme_prev = NULL; \
978 977 hme->hme_page = NULL; \
979 978 \
980 979 if (hme_size(hme) > TTE8K) { \
981 980 /* remove mappings for remainder of large pg */ \
982 981 sfmmu_rm_large_mappings(pp, hme_size(hme)); \
983 982 } \
984 983 }
985 984
986 985 /*
987 986 * This function returns the hment given the hme_blk and a vaddr.
988 987 * It assumes addr has already been checked to belong to hme_blk's
989 988 * range.
990 989 */
991 990 #define HBLKTOHME(hment, hmeblkp, addr) \
992 991 { \
993 992 int index; \
994 993 HBLKTOHME_IDX(hment, hmeblkp, addr, index) \
995 994 }
996 995
997 996 /*
998 997 * Version of HBLKTOHME that also returns the index in hmeblkp
999 998 * of the hment.
1000 999 */
1001 1000 #define HBLKTOHME_IDX(hment, hmeblkp, addr, idx) \
1002 1001 { \
1003 1002 ASSERT(in_hblk_range((hmeblkp), (addr))); \
1004 1003 \
1005 1004 if (get_hblk_ttesz(hmeblkp) == TTE8K) { \
1006 1005 idx = (((uintptr_t)(addr) >> MMU_PAGESHIFT) & (NHMENTS-1)); \
1007 1006 } else \
1008 1007 idx = 0; \
1009 1008 \
1010 1009 (hment) = &(hmeblkp)->hblk_hme[idx]; \
1011 1010 }
1012 1011
1013 1012 /*
1014 1013 * Disable any page sizes not supported by the CPU
1015 1014 */
1016 1015 void
1017 1016 hat_init_pagesizes()
1018 1017 {
1019 1018 int i;
1020 1019
1021 1020 mmu_exported_page_sizes = 0;
1022 1021 for (i = TTE8K; i < max_mmu_page_sizes; i++) {
1023 1022
1024 1023 szc_2_userszc[i] = (uint_t)-1;
1025 1024 userszc_2_szc[i] = (uint_t)-1;
1026 1025
1027 1026 if ((mmu_exported_pagesize_mask & (1 << i)) == 0) {
1028 1027 disable_large_pages |= (1 << i);
1029 1028 } else {
1030 1029 szc_2_userszc[i] = mmu_exported_page_sizes;
1031 1030 userszc_2_szc[mmu_exported_page_sizes] = i;
1032 1031 mmu_exported_page_sizes++;
1033 1032 }
1034 1033 }
1035 1034
1036 1035 disable_ism_large_pages |= disable_large_pages;
1037 1036 disable_auto_data_large_pages = disable_large_pages;
1038 1037 disable_auto_text_large_pages = disable_large_pages;
1039 1038
1040 1039 /*
1041 1040 * Initialize mmu-specific large page sizes.
1042 1041 */
1043 1042 if (&mmu_large_pages_disabled) {
1044 1043 disable_large_pages |= mmu_large_pages_disabled(HAT_LOAD);
1045 1044 disable_ism_large_pages |=
1046 1045 mmu_large_pages_disabled(HAT_LOAD_SHARE);
1047 1046 disable_auto_data_large_pages |=
1048 1047 mmu_large_pages_disabled(HAT_AUTO_DATA);
1049 1048 disable_auto_text_large_pages |=
1050 1049 mmu_large_pages_disabled(HAT_AUTO_TEXT);
1051 1050 }
1052 1051 }
1053 1052
1054 1053 /*
1055 1054 * Initialize the hardware address translation structures.
1056 1055 */
1057 1056 void
1058 1057 hat_init(void)
1059 1058 {
1060 1059 int i;
1061 1060 uint_t sz;
1062 1061 size_t size;
1063 1062
1064 1063 hat_lock_init();
1065 1064 hat_kstat_init();
1066 1065
1067 1066 /*
1068 1067 * Hardware-only bits in a TTE
1069 1068 */
1070 1069 MAKE_TTE_MASK(&hw_tte);
1071 1070
1072 1071 hat_init_pagesizes();
1073 1072
1074 1073 /* Initialize the hash locks */
1075 1074 for (i = 0; i < khmehash_num; i++) {
1076 1075 mutex_init(&khme_hash[i].hmehash_mutex, NULL,
1077 1076 MUTEX_DEFAULT, NULL);
1078 1077 khme_hash[i].hmeh_nextpa = HMEBLK_ENDPA;
1079 1078 }
1080 1079 for (i = 0; i < uhmehash_num; i++) {
1081 1080 mutex_init(&uhme_hash[i].hmehash_mutex, NULL,
1082 1081 MUTEX_DEFAULT, NULL);
1083 1082 uhme_hash[i].hmeh_nextpa = HMEBLK_ENDPA;
1084 1083 }
1085 1084 khmehash_num--; /* make sure counter starts from 0 */
1086 1085 uhmehash_num--; /* make sure counter starts from 0 */
1087 1086
1088 1087 /*
1089 1088 * Allocate context domain structures.
1090 1089 *
1091 1090 * A platform may choose to modify max_mmu_ctxdoms in
1092 1091 * set_platform_defaults(). If a platform does not define
1093 1092 * a set_platform_defaults() or does not choose to modify
1094 1093 * max_mmu_ctxdoms, it gets one MMU context domain for every CPU.
1095 1094 *
1096 1095 * For all platforms that have CPUs sharing MMUs, this
1097 1096 * value must be defined.
1098 1097 */
1099 1098 if (max_mmu_ctxdoms == 0)
1100 1099 max_mmu_ctxdoms = max_ncpus;
1101 1100
1102 1101 size = max_mmu_ctxdoms * sizeof (mmu_ctx_t *);
1103 1102 mmu_ctxs_tbl = kmem_zalloc(size, KM_SLEEP);
1104 1103
1105 1104 /* mmu_ctx_t is 64 bytes aligned */
1106 1105 mmuctxdom_cache = kmem_cache_create("mmuctxdom_cache",
1107 1106 sizeof (mmu_ctx_t), 64, NULL, NULL, NULL, NULL, NULL, 0);
1108 1107 /*
1109 1108 * MMU context domain initialization for the Boot CPU.
1110 1109 * This needs the context domains array allocated above.
1111 1110 */
1112 1111 mutex_enter(&cpu_lock);
1113 1112 sfmmu_cpu_init(CPU);
1114 1113 mutex_exit(&cpu_lock);
1115 1114
1116 1115 /*
1117 1116 * Intialize ism mapping list lock.
1118 1117 */
1119 1118
1120 1119 mutex_init(&ism_mlist_lock, NULL, MUTEX_DEFAULT, NULL);
1121 1120
1122 1121 /*
1123 1122 * Each sfmmu structure carries an array of MMU context info
1124 1123 * structures, one per context domain. The size of this array depends
1125 1124 * on the maximum number of context domains. So, the size of the
1126 1125 * sfmmu structure varies per platform.
1127 1126 *
1128 1127 * sfmmu is allocated from static arena, because trap
1129 1128 * handler at TL > 0 is not allowed to touch kernel relocatable
1130 1129 * memory. sfmmu's alignment is changed to 64 bytes from
1131 1130 * default 8 bytes, as the lower 6 bits will be used to pass
1132 1131 * pgcnt to vtag_flush_pgcnt_tl1.
1133 1132 */
1134 1133 size = sizeof (sfmmu_t) + sizeof (sfmmu_ctx_t) * (max_mmu_ctxdoms - 1);
1135 1134
1136 1135 sfmmuid_cache = kmem_cache_create("sfmmuid_cache", size,
1137 1136 64, sfmmu_idcache_constructor, sfmmu_idcache_destructor,
1138 1137 NULL, NULL, static_arena, 0);
1139 1138
1140 1139 sfmmu_tsbinfo_cache = kmem_cache_create("sfmmu_tsbinfo_cache",
1141 1140 sizeof (struct tsb_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
1142 1141
1143 1142 /*
1144 1143 * Since we only use the tsb8k cache to "borrow" pages for TSBs
1145 1144 * from the heap when low on memory or when TSB_FORCEALLOC is
1146 1145 * specified, don't use magazines to cache them--we want to return
1147 1146 * them to the system as quickly as possible.
1148 1147 */
1149 1148 sfmmu_tsb8k_cache = kmem_cache_create("sfmmu_tsb8k_cache",
1150 1149 MMU_PAGESIZE, MMU_PAGESIZE, NULL, NULL, NULL, NULL,
1151 1150 static_arena, KMC_NOMAGAZINE);
1152 1151
1153 1152 /*
1154 1153 * Set tsb_alloc_hiwater to 1/tsb_alloc_hiwater_factor of physical
1155 1154 * memory, which corresponds to the old static reserve for TSBs.
1156 1155 * tsb_alloc_hiwater_factor defaults to 32. This caps the amount of
1157 1156 * memory we'll allocate for TSB slabs; beyond this point TSB
1158 1157 * allocations will be taken from the kernel heap (via
1159 1158 * sfmmu_tsb8k_cache) and will be throttled as would any other kmem
1160 1159 * consumer.
1161 1160 */
1162 1161 if (tsb_alloc_hiwater_factor == 0) {
1163 1162 tsb_alloc_hiwater_factor = TSB_ALLOC_HIWATER_FACTOR_DEFAULT;
1164 1163 }
1165 1164 SFMMU_SET_TSB_ALLOC_HIWATER(physmem);
1166 1165
1167 1166 for (sz = tsb_slab_ttesz; sz > 0; sz--) {
1168 1167 if (!(disable_large_pages & (1 << sz)))
1169 1168 break;
1170 1169 }
1171 1170
1172 1171 if (sz < tsb_slab_ttesz) {
1173 1172 tsb_slab_ttesz = sz;
1174 1173 tsb_slab_shift = MMU_PAGESHIFT + (sz << 1) + sz;
1175 1174 tsb_slab_size = 1 << tsb_slab_shift;
1176 1175 tsb_slab_mask = (1 << (tsb_slab_shift - MMU_PAGESHIFT)) - 1;
1177 1176 use_bigtsb_arena = 0;
1178 1177 } else if (use_bigtsb_arena &&
1179 1178 (disable_large_pages & (1 << bigtsb_slab_ttesz))) {
1180 1179 use_bigtsb_arena = 0;
1181 1180 }
1182 1181
1183 1182 if (!use_bigtsb_arena) {
1184 1183 bigtsb_slab_shift = tsb_slab_shift;
1185 1184 }
1186 1185 SFMMU_SET_TSB_MAX_GROWSIZE(physmem);
1187 1186
1188 1187 /*
1189 1188 * On smaller memory systems, allocate TSB memory in smaller chunks
1190 1189 * than the default 4M slab size. We also honor disable_large_pages
1191 1190 * here.
1192 1191 *
1193 1192 * The trap handlers need to be patched with the final slab shift,
1194 1193 * since they need to be able to construct the TSB pointer at runtime.
1195 1194 */
1196 1195 if ((tsb_max_growsize <= TSB_512K_SZCODE) &&
1197 1196 !(disable_large_pages & (1 << TTE512K))) {
1198 1197 tsb_slab_ttesz = TTE512K;
1199 1198 tsb_slab_shift = MMU_PAGESHIFT512K;
1200 1199 tsb_slab_size = MMU_PAGESIZE512K;
1201 1200 tsb_slab_mask = MMU_PAGEOFFSET512K >> MMU_PAGESHIFT;
1202 1201 use_bigtsb_arena = 0;
1203 1202 }
1204 1203
1205 1204 if (!use_bigtsb_arena) {
1206 1205 bigtsb_slab_ttesz = tsb_slab_ttesz;
1207 1206 bigtsb_slab_shift = tsb_slab_shift;
1208 1207 bigtsb_slab_size = tsb_slab_size;
1209 1208 bigtsb_slab_mask = tsb_slab_mask;
1210 1209 }
1211 1210
1212 1211
1213 1212 /*
1214 1213 * Set up memory callback to update tsb_alloc_hiwater and
1215 1214 * tsb_max_growsize.
1216 1215 */
1217 1216 i = kphysm_setup_func_register(&sfmmu_update_vec, (void *) 0);
1218 1217 ASSERT(i == 0);
1219 1218
1220 1219 /*
1221 1220 * kmem_tsb_arena is the source from which large TSB slabs are
1222 1221 * drawn. The quantum of this arena corresponds to the largest
1223 1222 * TSB size we can dynamically allocate for user processes.
1224 1223 * Currently it must also be a supported page size since we
1225 1224 * use exactly one translation entry to map each slab page.
1226 1225 *
1227 1226 * The per-lgroup kmem_tsb_default_arena arenas are the arenas from
1228 1227 * which most TSBs are allocated. Since most TSB allocations are
1229 1228 * typically 8K we have a kmem cache we stack on top of each
1230 1229 * kmem_tsb_default_arena to speed up those allocations.
1231 1230 *
1232 1231 * Note the two-level scheme of arenas is required only
1233 1232 * because vmem_create doesn't allow us to specify alignment
1234 1233 * requirements. If this ever changes the code could be
1235 1234 * simplified to use only one level of arenas.
1236 1235 *
1237 1236 * If 256M page support exists on sun4v, 256MB kmem_bigtsb_arena
1238 1237 * will be provided in addition to the 4M kmem_tsb_arena.
1239 1238 */
1240 1239 if (use_bigtsb_arena) {
1241 1240 kmem_bigtsb_arena = vmem_create("kmem_bigtsb", NULL, 0,
1242 1241 bigtsb_slab_size, sfmmu_vmem_xalloc_aligned_wrapper,
1243 1242 vmem_xfree, heap_arena, 0, VM_SLEEP);
1244 1243 }
1245 1244
1246 1245 kmem_tsb_arena = vmem_create("kmem_tsb", NULL, 0, tsb_slab_size,
1247 1246 sfmmu_vmem_xalloc_aligned_wrapper,
1248 1247 vmem_xfree, heap_arena, 0, VM_SLEEP);
1249 1248
1250 1249 if (tsb_lgrp_affinity) {
1251 1250 char s[50];
1252 1251 for (i = 0; i < NLGRPS_MAX; i++) {
1253 1252 if (use_bigtsb_arena) {
1254 1253 (void) sprintf(s, "kmem_bigtsb_lgrp%d", i);
1255 1254 kmem_bigtsb_default_arena[i] = vmem_create(s,
1256 1255 NULL, 0, 2 * tsb_slab_size,
1257 1256 sfmmu_tsb_segkmem_alloc,
1258 1257 sfmmu_tsb_segkmem_free, kmem_bigtsb_arena,
1259 1258 0, VM_SLEEP | VM_BESTFIT);
1260 1259 }
1261 1260
1262 1261 (void) sprintf(s, "kmem_tsb_lgrp%d", i);
1263 1262 kmem_tsb_default_arena[i] = vmem_create(s,
1264 1263 NULL, 0, PAGESIZE, sfmmu_tsb_segkmem_alloc,
1265 1264 sfmmu_tsb_segkmem_free, kmem_tsb_arena, 0,
1266 1265 VM_SLEEP | VM_BESTFIT);
1267 1266
1268 1267 (void) sprintf(s, "sfmmu_tsb_lgrp%d_cache", i);
1269 1268 sfmmu_tsb_cache[i] = kmem_cache_create(s,
1270 1269 PAGESIZE, PAGESIZE, NULL, NULL, NULL, NULL,
1271 1270 kmem_tsb_default_arena[i], 0);
1272 1271 }
1273 1272 } else {
1274 1273 if (use_bigtsb_arena) {
1275 1274 kmem_bigtsb_default_arena[0] =
1276 1275 vmem_create("kmem_bigtsb_default", NULL, 0,
1277 1276 2 * tsb_slab_size, sfmmu_tsb_segkmem_alloc,
1278 1277 sfmmu_tsb_segkmem_free, kmem_bigtsb_arena, 0,
1279 1278 VM_SLEEP | VM_BESTFIT);
1280 1279 }
1281 1280
1282 1281 kmem_tsb_default_arena[0] = vmem_create("kmem_tsb_default",
1283 1282 NULL, 0, PAGESIZE, sfmmu_tsb_segkmem_alloc,
1284 1283 sfmmu_tsb_segkmem_free, kmem_tsb_arena, 0,
1285 1284 VM_SLEEP | VM_BESTFIT);
1286 1285 sfmmu_tsb_cache[0] = kmem_cache_create("sfmmu_tsb_cache",
1287 1286 PAGESIZE, PAGESIZE, NULL, NULL, NULL, NULL,
1288 1287 kmem_tsb_default_arena[0], 0);
1289 1288 }
1290 1289
1291 1290 sfmmu8_cache = kmem_cache_create("sfmmu8_cache", HME8BLK_SZ,
1292 1291 HMEBLK_ALIGN, sfmmu_hblkcache_constructor,
1293 1292 sfmmu_hblkcache_destructor,
1294 1293 sfmmu_hblkcache_reclaim, (void *)HME8BLK_SZ,
1295 1294 hat_memload_arena, KMC_NOHASH);
1296 1295
1297 1296 hat_memload1_arena = vmem_create("hat_memload1", NULL, 0, PAGESIZE,
1298 1297 segkmem_alloc_permanent, segkmem_free, heap_arena, 0,
1299 1298 VMC_DUMPSAFE | VM_SLEEP);
1300 1299
1301 1300 sfmmu1_cache = kmem_cache_create("sfmmu1_cache", HME1BLK_SZ,
1302 1301 HMEBLK_ALIGN, sfmmu_hblkcache_constructor,
1303 1302 sfmmu_hblkcache_destructor,
1304 1303 NULL, (void *)HME1BLK_SZ,
1305 1304 hat_memload1_arena, KMC_NOHASH);
1306 1305
1307 1306 pa_hment_cache = kmem_cache_create("pa_hment_cache", PAHME_SZ,
1308 1307 0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH);
1309 1308
1310 1309 ism_blk_cache = kmem_cache_create("ism_blk_cache",
1311 1310 sizeof (ism_blk_t), ecache_alignsize, NULL, NULL,
1312 1311 NULL, NULL, static_arena, KMC_NOHASH);
1313 1312
1314 1313 ism_ment_cache = kmem_cache_create("ism_ment_cache",
1315 1314 sizeof (ism_ment_t), 0, NULL, NULL,
1316 1315 NULL, NULL, NULL, 0);
1317 1316
1318 1317 /*
1319 1318 * We grab the first hat for the kernel,
1320 1319 */
1321 1320 AS_LOCK_ENTER(&kas, &kas.a_lock, RW_WRITER);
1322 1321 kas.a_hat = hat_alloc(&kas);
1323 1322 AS_LOCK_EXIT(&kas, &kas.a_lock);
1324 1323
1325 1324 /*
1326 1325 * Initialize hblk_reserve.
1327 1326 */
1328 1327 ((struct hme_blk *)hblk_reserve)->hblk_nextpa =
1329 1328 va_to_pa((caddr_t)hblk_reserve);
1330 1329
1331 1330 #ifndef UTSB_PHYS
1332 1331 /*
1333 1332 * Reserve some kernel virtual address space for the locked TTEs
1334 1333 * that allow us to probe the TSB from TL>0.
1335 1334 */
1336 1335 utsb_vabase = vmem_xalloc(heap_arena, tsb_slab_size, tsb_slab_size,
1337 1336 0, 0, NULL, NULL, VM_SLEEP);
1338 1337 utsb4m_vabase = vmem_xalloc(heap_arena, tsb_slab_size, tsb_slab_size,
1339 1338 0, 0, NULL, NULL, VM_SLEEP);
1340 1339 #endif
1341 1340
1342 1341 #ifdef VAC
↓ open down ↓ |
1248 lines elided |
↑ open up ↑ |
1343 1342 /*
1344 1343 * The big page VAC handling code assumes VAC
1345 1344 * will not be bigger than the smallest big
1346 1345 * page- which is 64K.
1347 1346 */
1348 1347 if (TTEPAGES(TTE64K) < CACHE_NUM_COLOR) {
1349 1348 cmn_err(CE_PANIC, "VAC too big!");
1350 1349 }
1351 1350 #endif
1352 1351
1353 - (void) xhat_init();
1354 -
1355 1352 uhme_hash_pa = va_to_pa(uhme_hash);
1356 1353 khme_hash_pa = va_to_pa(khme_hash);
1357 1354
1358 1355 /*
1359 1356 * Initialize relocation locks. kpr_suspendlock is held
1360 1357 * at PIL_MAX to prevent interrupts from pinning the holder
1361 1358 * of a suspended TTE which may access it leading to a
1362 1359 * deadlock condition.
1363 1360 */
1364 1361 mutex_init(&kpr_mutex, NULL, MUTEX_DEFAULT, NULL);
1365 1362 mutex_init(&kpr_suspendlock, NULL, MUTEX_SPIN, (void *)PIL_MAX);
1366 1363
1367 1364 /*
1368 1365 * If Shared context support is disabled via /etc/system
1369 1366 * set shctx_on to 0 here if it was set to 1 earlier in boot
1370 1367 * sequence by cpu module initialization code.
1371 1368 */
1372 1369 if (shctx_on && disable_shctx) {
1373 1370 shctx_on = 0;
1374 1371 }
1375 1372
1376 1373 if (shctx_on) {
1377 1374 srd_buckets = kmem_zalloc(SFMMU_MAX_SRD_BUCKETS *
1378 1375 sizeof (srd_buckets[0]), KM_SLEEP);
1379 1376 for (i = 0; i < SFMMU_MAX_SRD_BUCKETS; i++) {
1380 1377 mutex_init(&srd_buckets[i].srdb_lock, NULL,
1381 1378 MUTEX_DEFAULT, NULL);
1382 1379 }
1383 1380
1384 1381 srd_cache = kmem_cache_create("srd_cache", sizeof (sf_srd_t),
1385 1382 0, sfmmu_srdcache_constructor, sfmmu_srdcache_destructor,
1386 1383 NULL, NULL, NULL, 0);
1387 1384 region_cache = kmem_cache_create("region_cache",
1388 1385 sizeof (sf_region_t), 0, sfmmu_rgncache_constructor,
1389 1386 sfmmu_rgncache_destructor, NULL, NULL, NULL, 0);
1390 1387 scd_cache = kmem_cache_create("scd_cache", sizeof (sf_scd_t),
1391 1388 0, sfmmu_scdcache_constructor, sfmmu_scdcache_destructor,
1392 1389 NULL, NULL, NULL, 0);
1393 1390 }
1394 1391
1395 1392 /*
1396 1393 * Pre-allocate hrm_hashtab before enabling the collection of
1397 1394 * refmod statistics. Allocating on the fly would mean us
1398 1395 * running the risk of suffering recursive mutex enters or
1399 1396 * deadlocks.
1400 1397 */
1401 1398 hrm_hashtab = kmem_zalloc(HRM_HASHSIZE * sizeof (struct hrmstat *),
1402 1399 KM_SLEEP);
1403 1400
1404 1401 /* Allocate per-cpu pending freelist of hmeblks */
1405 1402 cpu_hme_pend = kmem_zalloc((NCPU * sizeof (cpu_hme_pend_t)) + 64,
1406 1403 KM_SLEEP);
1407 1404 cpu_hme_pend = (cpu_hme_pend_t *)P2ROUNDUP(
1408 1405 (uintptr_t)cpu_hme_pend, 64);
1409 1406
1410 1407 for (i = 0; i < NCPU; i++) {
1411 1408 mutex_init(&cpu_hme_pend[i].chp_mutex, NULL, MUTEX_DEFAULT,
1412 1409 NULL);
1413 1410 }
1414 1411
1415 1412 if (cpu_hme_pend_thresh == 0) {
1416 1413 cpu_hme_pend_thresh = CPU_HME_PEND_THRESH;
1417 1414 }
1418 1415 }
1419 1416
1420 1417 /*
1421 1418 * Initialize locking for the hat layer, called early during boot.
1422 1419 */
1423 1420 static void
1424 1421 hat_lock_init()
1425 1422 {
1426 1423 int i;
1427 1424
1428 1425 /*
1429 1426 * initialize the array of mutexes protecting a page's mapping
1430 1427 * list and p_nrm field.
1431 1428 */
1432 1429 for (i = 0; i < MML_TABLE_SIZE; i++)
1433 1430 mutex_init(&mml_table[i].pad_mutex, NULL, MUTEX_DEFAULT, NULL);
1434 1431
1435 1432 if (kpm_enable) {
1436 1433 for (i = 0; i < kpmp_table_sz; i++) {
1437 1434 mutex_init(&kpmp_table[i].khl_mutex, NULL,
1438 1435 MUTEX_DEFAULT, NULL);
1439 1436 }
1440 1437 }
1441 1438
1442 1439 /*
1443 1440 * Initialize array of mutex locks that protects sfmmu fields and
1444 1441 * TSB lists.
1445 1442 */
1446 1443 for (i = 0; i < SFMMU_NUM_LOCK; i++)
1447 1444 mutex_init(HATLOCK_MUTEXP(&hat_lock[i]), NULL, MUTEX_DEFAULT,
1448 1445 NULL);
1449 1446 }
1450 1447
1451 1448 #define SFMMU_KERNEL_MAXVA \
1452 1449 (kmem64_base ? (uintptr_t)kmem64_end : (SYSLIMIT))
1453 1450
1454 1451 /*
1455 1452 * Allocate a hat structure.
1456 1453 * Called when an address space first uses a hat.
1457 1454 */
1458 1455 struct hat *
1459 1456 hat_alloc(struct as *as)
1460 1457 {
1461 1458 sfmmu_t *sfmmup;
1462 1459 int i;
1463 1460 uint64_t cnum;
1464 1461 extern uint_t get_color_start(struct as *);
1465 1462
1466 1463 ASSERT(AS_WRITE_HELD(as, &as->a_lock));
1467 1464 sfmmup = kmem_cache_alloc(sfmmuid_cache, KM_SLEEP);
1468 1465 sfmmup->sfmmu_as = as;
1469 1466 sfmmup->sfmmu_flags = 0;
1470 1467 sfmmup->sfmmu_tteflags = 0;
1471 1468 sfmmup->sfmmu_rtteflags = 0;
1472 1469 LOCK_INIT_CLEAR(&sfmmup->sfmmu_ctx_lock);
1473 1470
1474 1471 if (as == &kas) {
1475 1472 ksfmmup = sfmmup;
1476 1473 sfmmup->sfmmu_cext = 0;
1477 1474 cnum = KCONTEXT;
1478 1475
1479 1476 sfmmup->sfmmu_clrstart = 0;
1480 1477 sfmmup->sfmmu_tsb = NULL;
1481 1478 /*
1482 1479 * hat_kern_setup() will call sfmmu_init_ktsbinfo()
1483 1480 * to setup tsb_info for ksfmmup.
1484 1481 */
1485 1482 } else {
1486 1483
1487 1484 /*
1488 1485 * Just set to invalid ctx. When it faults, it will
1489 1486 * get a valid ctx. This would avoid the situation
1490 1487 * where we get a ctx, but it gets stolen and then
1491 1488 * we fault when we try to run and so have to get
1492 1489 * another ctx.
1493 1490 */
1494 1491 sfmmup->sfmmu_cext = 0;
1495 1492 cnum = INVALID_CONTEXT;
1496 1493
1497 1494 /* initialize original physical page coloring bin */
1498 1495 sfmmup->sfmmu_clrstart = get_color_start(as);
1499 1496 #ifdef DEBUG
1500 1497 if (tsb_random_size) {
1501 1498 uint32_t randval = (uint32_t)gettick() >> 4;
1502 1499 int size = randval % (tsb_max_growsize + 1);
1503 1500
1504 1501 /* chose a random tsb size for stress testing */
1505 1502 (void) sfmmu_tsbinfo_alloc(&sfmmup->sfmmu_tsb, size,
1506 1503 TSB8K|TSB64K|TSB512K, 0, sfmmup);
1507 1504 } else
1508 1505 #endif /* DEBUG */
1509 1506 (void) sfmmu_tsbinfo_alloc(&sfmmup->sfmmu_tsb,
1510 1507 default_tsb_size,
1511 1508 TSB8K|TSB64K|TSB512K, 0, sfmmup);
1512 1509 sfmmup->sfmmu_flags = HAT_SWAPPED | HAT_ALLCTX_INVALID;
1513 1510 ASSERT(sfmmup->sfmmu_tsb != NULL);
1514 1511 }
1515 1512
1516 1513 ASSERT(max_mmu_ctxdoms > 0);
1517 1514 for (i = 0; i < max_mmu_ctxdoms; i++) {
1518 1515 sfmmup->sfmmu_ctxs[i].cnum = cnum;
1519 1516 sfmmup->sfmmu_ctxs[i].gnum = 0;
1520 1517 }
1521 1518
1522 1519 for (i = 0; i < max_mmu_page_sizes; i++) {
1523 1520 sfmmup->sfmmu_ttecnt[i] = 0;
1524 1521 sfmmup->sfmmu_scdrttecnt[i] = 0;
1525 1522 sfmmup->sfmmu_ismttecnt[i] = 0;
1526 1523 sfmmup->sfmmu_scdismttecnt[i] = 0;
1527 1524 sfmmup->sfmmu_pgsz[i] = TTE8K;
1528 1525 }
1529 1526 sfmmup->sfmmu_tsb0_4minflcnt = 0;
1530 1527 sfmmup->sfmmu_iblk = NULL;
1531 1528 sfmmup->sfmmu_ismhat = 0;
↓ open down ↓ |
167 lines elided |
↑ open up ↑ |
1532 1529 sfmmup->sfmmu_scdhat = 0;
1533 1530 sfmmup->sfmmu_ismblkpa = (uint64_t)-1;
1534 1531 if (sfmmup == ksfmmup) {
1535 1532 CPUSET_ALL(sfmmup->sfmmu_cpusran);
1536 1533 } else {
1537 1534 CPUSET_ZERO(sfmmup->sfmmu_cpusran);
1538 1535 }
1539 1536 sfmmup->sfmmu_free = 0;
1540 1537 sfmmup->sfmmu_rmstat = 0;
1541 1538 sfmmup->sfmmu_clrbin = sfmmup->sfmmu_clrstart;
1542 - sfmmup->sfmmu_xhat_provider = NULL;
1543 1539 cv_init(&sfmmup->sfmmu_tsb_cv, NULL, CV_DEFAULT, NULL);
1544 1540 sfmmup->sfmmu_srdp = NULL;
1545 1541 SF_RGNMAP_ZERO(sfmmup->sfmmu_region_map);
1546 1542 bzero(sfmmup->sfmmu_hmeregion_links, SFMMU_L1_HMERLINKS_SIZE);
1547 1543 sfmmup->sfmmu_scdp = NULL;
1548 1544 sfmmup->sfmmu_scd_link.next = NULL;
1549 1545 sfmmup->sfmmu_scd_link.prev = NULL;
1550 1546 return (sfmmup);
1551 1547 }
1552 1548
1553 1549 /*
1554 1550 * Create per-MMU context domain kstats for a given MMU ctx.
1555 1551 */
1556 1552 static void
1557 1553 sfmmu_mmu_kstat_create(mmu_ctx_t *mmu_ctxp)
1558 1554 {
1559 1555 mmu_ctx_stat_t stat;
1560 1556 kstat_t *mmu_kstat;
1561 1557
1562 1558 ASSERT(MUTEX_HELD(&cpu_lock));
1563 1559 ASSERT(mmu_ctxp->mmu_kstat == NULL);
1564 1560
1565 1561 mmu_kstat = kstat_create("unix", mmu_ctxp->mmu_idx, "mmu_ctx",
1566 1562 "hat", KSTAT_TYPE_NAMED, MMU_CTX_NUM_STATS, KSTAT_FLAG_VIRTUAL);
1567 1563
1568 1564 if (mmu_kstat == NULL) {
1569 1565 cmn_err(CE_WARN, "kstat_create for MMU %d failed",
1570 1566 mmu_ctxp->mmu_idx);
1571 1567 } else {
1572 1568 mmu_kstat->ks_data = mmu_ctxp->mmu_kstat_data;
1573 1569 for (stat = 0; stat < MMU_CTX_NUM_STATS; stat++)
1574 1570 kstat_named_init(&mmu_ctxp->mmu_kstat_data[stat],
1575 1571 mmu_ctx_kstat_names[stat], KSTAT_DATA_INT64);
1576 1572 mmu_ctxp->mmu_kstat = mmu_kstat;
1577 1573 kstat_install(mmu_kstat);
1578 1574 }
1579 1575 }
1580 1576
1581 1577 /*
1582 1578 * plat_cpuid_to_mmu_ctx_info() is a platform interface that returns MMU
1583 1579 * context domain information for a given CPU. If a platform does not
1584 1580 * specify that interface, then the function below is used instead to return
1585 1581 * default information. The defaults are as follows:
1586 1582 *
1587 1583 * - The number of MMU context IDs supported on any CPU in the
1588 1584 * system is 8K.
1589 1585 * - There is one MMU context domain per CPU.
1590 1586 */
1591 1587 /*ARGSUSED*/
1592 1588 static void
1593 1589 sfmmu_cpuid_to_mmu_ctx_info(processorid_t cpuid, mmu_ctx_info_t *infop)
1594 1590 {
1595 1591 infop->mmu_nctxs = nctxs;
1596 1592 infop->mmu_idx = cpu[cpuid]->cpu_seqid;
1597 1593 }
1598 1594
1599 1595 /*
1600 1596 * Called during CPU initialization to set the MMU context-related information
1601 1597 * for a CPU.
1602 1598 *
1603 1599 * cpu_lock serializes accesses to mmu_ctxs and mmu_saved_gnum.
1604 1600 */
1605 1601 void
1606 1602 sfmmu_cpu_init(cpu_t *cp)
1607 1603 {
1608 1604 mmu_ctx_info_t info;
1609 1605 mmu_ctx_t *mmu_ctxp;
1610 1606
1611 1607 ASSERT(MUTEX_HELD(&cpu_lock));
1612 1608
1613 1609 if (&plat_cpuid_to_mmu_ctx_info == NULL)
1614 1610 sfmmu_cpuid_to_mmu_ctx_info(cp->cpu_id, &info);
1615 1611 else
1616 1612 plat_cpuid_to_mmu_ctx_info(cp->cpu_id, &info);
1617 1613
1618 1614 ASSERT(info.mmu_idx < max_mmu_ctxdoms);
1619 1615
1620 1616 if ((mmu_ctxp = mmu_ctxs_tbl[info.mmu_idx]) == NULL) {
1621 1617 /* Each mmu_ctx is cacheline aligned. */
1622 1618 mmu_ctxp = kmem_cache_alloc(mmuctxdom_cache, KM_SLEEP);
1623 1619 bzero(mmu_ctxp, sizeof (mmu_ctx_t));
1624 1620
1625 1621 mutex_init(&mmu_ctxp->mmu_lock, NULL, MUTEX_SPIN,
1626 1622 (void *)ipltospl(DISP_LEVEL));
1627 1623 mmu_ctxp->mmu_idx = info.mmu_idx;
1628 1624 mmu_ctxp->mmu_nctxs = info.mmu_nctxs;
1629 1625 /*
1630 1626 * Globally for lifetime of a system,
1631 1627 * gnum must always increase.
1632 1628 * mmu_saved_gnum is protected by the cpu_lock.
1633 1629 */
1634 1630 mmu_ctxp->mmu_gnum = mmu_saved_gnum + 1;
1635 1631 mmu_ctxp->mmu_cnum = NUM_LOCKED_CTXS;
1636 1632
1637 1633 sfmmu_mmu_kstat_create(mmu_ctxp);
1638 1634
1639 1635 mmu_ctxs_tbl[info.mmu_idx] = mmu_ctxp;
1640 1636 } else {
1641 1637 ASSERT(mmu_ctxp->mmu_idx == info.mmu_idx);
1642 1638 ASSERT(mmu_ctxp->mmu_nctxs <= info.mmu_nctxs);
1643 1639 }
1644 1640
1645 1641 /*
1646 1642 * The mmu_lock is acquired here to prevent races with
1647 1643 * the wrap-around code.
1648 1644 */
1649 1645 mutex_enter(&mmu_ctxp->mmu_lock);
1650 1646
1651 1647
1652 1648 mmu_ctxp->mmu_ncpus++;
1653 1649 CPUSET_ADD(mmu_ctxp->mmu_cpuset, cp->cpu_id);
1654 1650 CPU_MMU_IDX(cp) = info.mmu_idx;
1655 1651 CPU_MMU_CTXP(cp) = mmu_ctxp;
1656 1652
1657 1653 mutex_exit(&mmu_ctxp->mmu_lock);
1658 1654 }
1659 1655
1660 1656 static void
1661 1657 sfmmu_ctxdom_free(mmu_ctx_t *mmu_ctxp)
1662 1658 {
1663 1659 ASSERT(MUTEX_HELD(&cpu_lock));
1664 1660 ASSERT(!MUTEX_HELD(&mmu_ctxp->mmu_lock));
1665 1661
1666 1662 mutex_destroy(&mmu_ctxp->mmu_lock);
1667 1663
1668 1664 if (mmu_ctxp->mmu_kstat)
1669 1665 kstat_delete(mmu_ctxp->mmu_kstat);
1670 1666
1671 1667 /* mmu_saved_gnum is protected by the cpu_lock. */
1672 1668 if (mmu_saved_gnum < mmu_ctxp->mmu_gnum)
1673 1669 mmu_saved_gnum = mmu_ctxp->mmu_gnum;
1674 1670
1675 1671 kmem_cache_free(mmuctxdom_cache, mmu_ctxp);
1676 1672 }
1677 1673
1678 1674 /*
1679 1675 * Called to perform MMU context-related cleanup for a CPU.
1680 1676 */
1681 1677 void
1682 1678 sfmmu_cpu_cleanup(cpu_t *cp)
1683 1679 {
1684 1680 mmu_ctx_t *mmu_ctxp;
1685 1681
1686 1682 ASSERT(MUTEX_HELD(&cpu_lock));
1687 1683
1688 1684 mmu_ctxp = CPU_MMU_CTXP(cp);
1689 1685 ASSERT(mmu_ctxp != NULL);
1690 1686
1691 1687 /*
1692 1688 * The mmu_lock is acquired here to prevent races with
1693 1689 * the wrap-around code.
1694 1690 */
1695 1691 mutex_enter(&mmu_ctxp->mmu_lock);
1696 1692
1697 1693 CPU_MMU_CTXP(cp) = NULL;
1698 1694
1699 1695 CPUSET_DEL(mmu_ctxp->mmu_cpuset, cp->cpu_id);
1700 1696 if (--mmu_ctxp->mmu_ncpus == 0) {
1701 1697 mmu_ctxs_tbl[mmu_ctxp->mmu_idx] = NULL;
1702 1698 mutex_exit(&mmu_ctxp->mmu_lock);
1703 1699 sfmmu_ctxdom_free(mmu_ctxp);
1704 1700 return;
1705 1701 }
1706 1702
1707 1703 mutex_exit(&mmu_ctxp->mmu_lock);
1708 1704 }
1709 1705
1710 1706 uint_t
1711 1707 sfmmu_ctxdom_nctxs(int idx)
1712 1708 {
1713 1709 return (mmu_ctxs_tbl[idx]->mmu_nctxs);
1714 1710 }
1715 1711
1716 1712 #ifdef sun4v
1717 1713 /*
1718 1714 * sfmmu_ctxdoms_* is an interface provided to help keep context domains
1719 1715 * consistant after suspend/resume on system that can resume on a different
1720 1716 * hardware than it was suspended.
1721 1717 *
1722 1718 * sfmmu_ctxdom_lock(void) locks all context domains and prevents new contexts
1723 1719 * from being allocated. It acquires all hat_locks, which blocks most access to
1724 1720 * context data, except for a few cases that are handled separately or are
1725 1721 * harmless. It wraps each domain to increment gnum and invalidate on-CPU
1726 1722 * contexts, and forces cnum to its max. As a result of this call all user
1727 1723 * threads that are running on CPUs trap and try to perform wrap around but
1728 1724 * can't because hat_locks are taken. Threads that were not on CPUs but started
1729 1725 * by scheduler go to sfmmu_alloc_ctx() to aquire context without checking
1730 1726 * hat_lock, but fail, because cnum == nctxs, and therefore also trap and block
1731 1727 * on hat_lock trying to wrap. sfmmu_ctxdom_lock() must be called before CPUs
1732 1728 * are paused, else it could deadlock acquiring locks held by paused CPUs.
1733 1729 *
1734 1730 * sfmmu_ctxdoms_remove() removes context domains from every CPUs and records
1735 1731 * the CPUs that had them. It must be called after CPUs have been paused. This
1736 1732 * ensures that no threads are in sfmmu_alloc_ctx() accessing domain data,
1737 1733 * because pause_cpus sends a mondo interrupt to every CPU, and sfmmu_alloc_ctx
1738 1734 * runs with interrupts disabled. When CPUs are later resumed, they may enter
1739 1735 * sfmmu_alloc_ctx, but it will check for CPU_MMU_CTXP = NULL and immediately
1740 1736 * return failure. Or, they will be blocked trying to acquire hat_lock. Thus
1741 1737 * after sfmmu_ctxdoms_remove returns, we are guaranteed that no one is
1742 1738 * accessing the old context domains.
1743 1739 *
1744 1740 * sfmmu_ctxdoms_update(void) frees space used by old context domains and
1745 1741 * allocates new context domains based on hardware layout. It initializes
1746 1742 * every CPU that had context domain before migration to have one again.
1747 1743 * sfmmu_ctxdoms_update must be called after CPUs are resumed, else it
1748 1744 * could deadlock acquiring locks held by paused CPUs.
1749 1745 *
1750 1746 * sfmmu_ctxdoms_unlock(void) releases all hat_locks after which user threads
1751 1747 * acquire new context ids and continue execution.
1752 1748 *
1753 1749 * Therefore functions should be called in the following order:
1754 1750 * suspend_routine()
1755 1751 * sfmmu_ctxdom_lock()
1756 1752 * pause_cpus()
1757 1753 * suspend()
1758 1754 * if (suspend failed)
1759 1755 * sfmmu_ctxdom_unlock()
1760 1756 * ...
1761 1757 * sfmmu_ctxdom_remove()
1762 1758 * resume_cpus()
1763 1759 * sfmmu_ctxdom_update()
1764 1760 * sfmmu_ctxdom_unlock()
1765 1761 */
1766 1762 static cpuset_t sfmmu_ctxdoms_pset;
1767 1763
1768 1764 void
1769 1765 sfmmu_ctxdoms_remove()
1770 1766 {
1771 1767 processorid_t id;
1772 1768 cpu_t *cp;
1773 1769
1774 1770 /*
1775 1771 * Record the CPUs that have domains in sfmmu_ctxdoms_pset, so they can
1776 1772 * be restored post-migration. A CPU may be powered off and not have a
1777 1773 * domain, for example.
1778 1774 */
1779 1775 CPUSET_ZERO(sfmmu_ctxdoms_pset);
1780 1776
1781 1777 for (id = 0; id < NCPU; id++) {
1782 1778 if ((cp = cpu[id]) != NULL && CPU_MMU_CTXP(cp) != NULL) {
1783 1779 CPUSET_ADD(sfmmu_ctxdoms_pset, id);
1784 1780 CPU_MMU_CTXP(cp) = NULL;
1785 1781 }
1786 1782 }
1787 1783 }
1788 1784
1789 1785 void
1790 1786 sfmmu_ctxdoms_lock(void)
1791 1787 {
1792 1788 int idx;
1793 1789 mmu_ctx_t *mmu_ctxp;
1794 1790
1795 1791 sfmmu_hat_lock_all();
1796 1792
1797 1793 /*
1798 1794 * At this point, no thread can be in sfmmu_ctx_wrap_around, because
1799 1795 * hat_lock is always taken before calling it.
1800 1796 *
1801 1797 * For each domain, set mmu_cnum to max so no more contexts can be
1802 1798 * allocated, and wrap to flush on-CPU contexts and force threads to
1803 1799 * acquire a new context when we later drop hat_lock after migration.
1804 1800 * Setting mmu_cnum may race with sfmmu_alloc_ctx which also sets cnum,
1805 1801 * but the latter uses CAS and will miscompare and not overwrite it.
1806 1802 */
1807 1803 kpreempt_disable(); /* required by sfmmu_ctx_wrap_around */
1808 1804 for (idx = 0; idx < max_mmu_ctxdoms; idx++) {
1809 1805 if ((mmu_ctxp = mmu_ctxs_tbl[idx]) != NULL) {
1810 1806 mutex_enter(&mmu_ctxp->mmu_lock);
1811 1807 mmu_ctxp->mmu_cnum = mmu_ctxp->mmu_nctxs;
1812 1808 /* make sure updated cnum visible */
1813 1809 membar_enter();
1814 1810 mutex_exit(&mmu_ctxp->mmu_lock);
1815 1811 sfmmu_ctx_wrap_around(mmu_ctxp, B_FALSE);
1816 1812 }
1817 1813 }
1818 1814 kpreempt_enable();
1819 1815 }
1820 1816
1821 1817 void
1822 1818 sfmmu_ctxdoms_unlock(void)
1823 1819 {
1824 1820 sfmmu_hat_unlock_all();
1825 1821 }
1826 1822
1827 1823 void
1828 1824 sfmmu_ctxdoms_update(void)
1829 1825 {
1830 1826 processorid_t id;
1831 1827 cpu_t *cp;
1832 1828 uint_t idx;
1833 1829 mmu_ctx_t *mmu_ctxp;
1834 1830
1835 1831 /*
1836 1832 * Free all context domains. As side effect, this increases
1837 1833 * mmu_saved_gnum to the maximum gnum over all domains, which is used to
1838 1834 * init gnum in the new domains, which therefore will be larger than the
1839 1835 * sfmmu gnum for any process, guaranteeing that every process will see
1840 1836 * a new generation and allocate a new context regardless of what new
1841 1837 * domain it runs in.
1842 1838 */
1843 1839 mutex_enter(&cpu_lock);
1844 1840
1845 1841 for (idx = 0; idx < max_mmu_ctxdoms; idx++) {
1846 1842 if (mmu_ctxs_tbl[idx] != NULL) {
1847 1843 mmu_ctxp = mmu_ctxs_tbl[idx];
1848 1844 mmu_ctxs_tbl[idx] = NULL;
1849 1845 sfmmu_ctxdom_free(mmu_ctxp);
1850 1846 }
1851 1847 }
1852 1848
1853 1849 for (id = 0; id < NCPU; id++) {
1854 1850 if (CPU_IN_SET(sfmmu_ctxdoms_pset, id) &&
1855 1851 (cp = cpu[id]) != NULL)
1856 1852 sfmmu_cpu_init(cp);
1857 1853 }
1858 1854 mutex_exit(&cpu_lock);
1859 1855 }
1860 1856 #endif
1861 1857
1862 1858 /*
1863 1859 * Hat_setup, makes an address space context the current active one.
1864 1860 * In sfmmu this translates to setting the secondary context with the
1865 1861 * corresponding context.
1866 1862 */
1867 1863 void
1868 1864 hat_setup(struct hat *sfmmup, int allocflag)
1869 1865 {
1870 1866 hatlock_t *hatlockp;
1871 1867
1872 1868 /* Init needs some special treatment. */
1873 1869 if (allocflag == HAT_INIT) {
1874 1870 /*
1875 1871 * Make sure that we have
1876 1872 * 1. a TSB
1877 1873 * 2. a valid ctx that doesn't get stolen after this point.
1878 1874 */
1879 1875 hatlockp = sfmmu_hat_enter(sfmmup);
1880 1876
1881 1877 /*
1882 1878 * Swap in the TSB. hat_init() allocates tsbinfos without
1883 1879 * TSBs, but we need one for init, since the kernel does some
1884 1880 * special things to set up its stack and needs the TSB to
1885 1881 * resolve page faults.
1886 1882 */
1887 1883 sfmmu_tsb_swapin(sfmmup, hatlockp);
1888 1884
1889 1885 sfmmu_get_ctx(sfmmup);
1890 1886
1891 1887 sfmmu_hat_exit(hatlockp);
1892 1888 } else {
1893 1889 ASSERT(allocflag == HAT_ALLOC);
1894 1890
1895 1891 hatlockp = sfmmu_hat_enter(sfmmup);
1896 1892 kpreempt_disable();
1897 1893
1898 1894 CPUSET_ADD(sfmmup->sfmmu_cpusran, CPU->cpu_id);
1899 1895 /*
1900 1896 * sfmmu_setctx_sec takes <pgsz|cnum> as a parameter,
1901 1897 * pagesize bits don't matter in this case since we are passing
1902 1898 * INVALID_CONTEXT to it.
1903 1899 * Compatibility Note: hw takes care of MMU_SCONTEXT1
1904 1900 */
1905 1901 sfmmu_setctx_sec(INVALID_CONTEXT);
1906 1902 sfmmu_clear_utsbinfo();
1907 1903
1908 1904 kpreempt_enable();
1909 1905 sfmmu_hat_exit(hatlockp);
1910 1906 }
1911 1907 }
↓ open down ↓ |
359 lines elided |
↑ open up ↑ |
1912 1908
1913 1909 /*
1914 1910 * Free all the translation resources for the specified address space.
1915 1911 * Called from as_free when an address space is being destroyed.
1916 1912 */
1917 1913 void
1918 1914 hat_free_start(struct hat *sfmmup)
1919 1915 {
1920 1916 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
1921 1917 ASSERT(sfmmup != ksfmmup);
1922 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
1923 1918
1924 1919 sfmmup->sfmmu_free = 1;
1925 1920 if (sfmmup->sfmmu_scdp != NULL) {
1926 1921 sfmmu_leave_scd(sfmmup, 0);
1927 1922 }
1928 1923
1929 1924 ASSERT(sfmmup->sfmmu_scdp == NULL);
1930 1925 }
1931 1926
1932 1927 void
1933 1928 hat_free_end(struct hat *sfmmup)
1934 1929 {
1935 1930 int i;
1936 1931
1937 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
1938 1932 ASSERT(sfmmup->sfmmu_free == 1);
1939 1933 ASSERT(sfmmup->sfmmu_ttecnt[TTE8K] == 0);
1940 1934 ASSERT(sfmmup->sfmmu_ttecnt[TTE64K] == 0);
1941 1935 ASSERT(sfmmup->sfmmu_ttecnt[TTE512K] == 0);
1942 1936 ASSERT(sfmmup->sfmmu_ttecnt[TTE4M] == 0);
1943 1937 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0);
1944 1938 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0);
1945 1939
1946 1940 if (sfmmup->sfmmu_rmstat) {
1947 1941 hat_freestat(sfmmup->sfmmu_as, NULL);
1948 1942 }
1949 1943
1950 1944 while (sfmmup->sfmmu_tsb != NULL) {
1951 1945 struct tsb_info *next = sfmmup->sfmmu_tsb->tsb_next;
1952 1946 sfmmu_tsbinfo_free(sfmmup->sfmmu_tsb);
1953 1947 sfmmup->sfmmu_tsb = next;
1954 1948 }
1955 1949
1956 1950 if (sfmmup->sfmmu_srdp != NULL) {
1957 1951 sfmmu_leave_srd(sfmmup);
1958 1952 ASSERT(sfmmup->sfmmu_srdp == NULL);
1959 1953 for (i = 0; i < SFMMU_L1_HMERLINKS; i++) {
1960 1954 if (sfmmup->sfmmu_hmeregion_links[i] != NULL) {
1961 1955 kmem_free(sfmmup->sfmmu_hmeregion_links[i],
1962 1956 SFMMU_L2_HMERLINKS_SIZE);
1963 1957 sfmmup->sfmmu_hmeregion_links[i] = NULL;
1964 1958 }
1965 1959 }
1966 1960 }
1967 1961 sfmmu_free_sfmmu(sfmmup);
1968 1962
↓ open down ↓ |
21 lines elided |
↑ open up ↑ |
1969 1963 #ifdef DEBUG
1970 1964 for (i = 0; i < SFMMU_L1_HMERLINKS; i++) {
1971 1965 ASSERT(sfmmup->sfmmu_hmeregion_links[i] == NULL);
1972 1966 }
1973 1967 #endif
1974 1968
1975 1969 kmem_cache_free(sfmmuid_cache, sfmmup);
1976 1970 }
1977 1971
1978 1972 /*
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 1973 * Duplicate the translations of an as into another newas
2127 1974 */
2128 1975 /* ARGSUSED */
2129 1976 int
2130 1977 hat_dup(struct hat *hat, struct hat *newhat, caddr_t addr, size_t len,
2131 1978 uint_t flag)
2132 1979 {
2133 1980 sf_srd_t *srdp;
2134 1981 sf_scd_t *scdp;
2135 1982 int i;
2136 1983 extern uint_t get_color_start(struct as *);
2137 1984
2138 - ASSERT(hat->sfmmu_xhat_provider == NULL);
2139 1985 ASSERT((flag == 0) || (flag == HAT_DUP_ALL) || (flag == HAT_DUP_COW) ||
2140 1986 (flag == HAT_DUP_SRD));
2141 1987 ASSERT(hat != ksfmmup);
2142 1988 ASSERT(newhat != ksfmmup);
2143 1989 ASSERT(flag != HAT_DUP_ALL || hat->sfmmu_srdp == newhat->sfmmu_srdp);
2144 1990
2145 1991 if (flag == HAT_DUP_COW) {
2146 1992 panic("hat_dup: HAT_DUP_COW not supported");
2147 1993 }
2148 1994
2149 1995 if (flag == HAT_DUP_SRD && ((srdp = hat->sfmmu_srdp) != NULL)) {
2150 1996 ASSERT(srdp->srd_evp != NULL);
2151 1997 VN_HOLD(srdp->srd_evp);
2152 1998 ASSERT(srdp->srd_refcnt > 0);
2153 1999 newhat->sfmmu_srdp = srdp;
2154 2000 atomic_inc_32((volatile uint_t *)&srdp->srd_refcnt);
2155 2001 }
2156 2002
2157 2003 /*
2158 2004 * HAT_DUP_ALL flag is used after as duplication is done.
2159 2005 */
2160 2006 if (flag == HAT_DUP_ALL && ((srdp = newhat->sfmmu_srdp) != NULL)) {
2161 2007 ASSERT(newhat->sfmmu_srdp->srd_refcnt >= 2);
2162 2008 newhat->sfmmu_rtteflags = hat->sfmmu_rtteflags;
2163 2009 if (hat->sfmmu_flags & HAT_4MTEXT_FLAG) {
2164 2010 newhat->sfmmu_flags |= HAT_4MTEXT_FLAG;
2165 2011 }
2166 2012
2167 2013 /* check if need to join scd */
2168 2014 if ((scdp = hat->sfmmu_scdp) != NULL &&
2169 2015 newhat->sfmmu_scdp != scdp) {
2170 2016 int ret;
2171 2017 SF_RGNMAP_IS_SUBSET(&newhat->sfmmu_region_map,
2172 2018 &scdp->scd_region_map, ret);
2173 2019 ASSERT(ret);
2174 2020 sfmmu_join_scd(scdp, newhat);
2175 2021 ASSERT(newhat->sfmmu_scdp == scdp &&
2176 2022 scdp->scd_refcnt >= 2);
2177 2023 for (i = 0; i < max_mmu_page_sizes; i++) {
2178 2024 newhat->sfmmu_ismttecnt[i] =
2179 2025 hat->sfmmu_ismttecnt[i];
2180 2026 newhat->sfmmu_scdismttecnt[i] =
2181 2027 hat->sfmmu_scdismttecnt[i];
2182 2028 }
2183 2029 }
2184 2030
2185 2031 sfmmu_check_page_sizes(newhat, 1);
2186 2032 }
2187 2033
2188 2034 if (flag == HAT_DUP_ALL && consistent_coloring == 0 &&
2189 2035 update_proc_pgcolorbase_after_fork != 0) {
2190 2036 hat->sfmmu_clrbin = get_color_start(hat->sfmmu_as);
2191 2037 }
2192 2038 return (0);
2193 2039 }
2194 2040
2195 2041 void
2196 2042 hat_memload(struct hat *hat, caddr_t addr, struct page *pp,
2197 2043 uint_t attr, uint_t flags)
↓ open down ↓ |
49 lines elided |
↑ open up ↑ |
2198 2044 {
2199 2045 hat_do_memload(hat, addr, pp, attr, flags,
2200 2046 SFMMU_INVALID_SHMERID);
2201 2047 }
2202 2048
2203 2049 void
2204 2050 hat_memload_region(struct hat *hat, caddr_t addr, struct page *pp,
2205 2051 uint_t attr, uint_t flags, hat_region_cookie_t rcookie)
2206 2052 {
2207 2053 uint_t rid;
2208 - if (rcookie == HAT_INVALID_REGION_COOKIE ||
2209 - hat->sfmmu_xhat_provider != NULL) {
2054 + if (rcookie == HAT_INVALID_REGION_COOKIE) {
2210 2055 hat_do_memload(hat, addr, pp, attr, flags,
2211 2056 SFMMU_INVALID_SHMERID);
2212 2057 return;
2213 2058 }
2214 2059 rid = (uint_t)((uint64_t)rcookie);
2215 2060 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
2216 2061 hat_do_memload(hat, addr, pp, attr, flags, rid);
2217 2062 }
2218 2063
2219 2064 /*
2220 2065 * Set up addr to map to page pp with protection prot.
2221 2066 * As an optimization we also load the TSB with the
2222 2067 * corresponding tte but it is no big deal if the tte gets kicked out.
2223 2068 */
2224 2069 static void
2225 2070 hat_do_memload(struct hat *hat, caddr_t addr, struct page *pp,
2226 2071 uint_t attr, uint_t flags, uint_t rid)
2227 2072 {
2228 2073 tte_t tte;
2229 2074
2230 2075
2231 2076 ASSERT(hat != NULL);
2232 2077 ASSERT(PAGE_LOCKED(pp));
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
2233 2078 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET));
2234 2079 ASSERT(!(flags & ~SFMMU_LOAD_ALLFLAG));
2235 2080 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR));
2236 2081 SFMMU_VALIDATE_HMERID(hat, rid, addr, MMU_PAGESIZE);
2237 2082
2238 2083 if (PP_ISFREE(pp)) {
2239 2084 panic("hat_memload: loading a mapping to free page %p",
2240 2085 (void *)pp);
2241 2086 }
2242 2087
2243 - if (hat->sfmmu_xhat_provider) {
2244 - /* no regions for xhats */
2245 - ASSERT(!SFMMU_IS_SHMERID_VALID(rid));
2246 - XHAT_MEMLOAD(hat, addr, pp, attr, flags);
2247 - return;
2248 - }
2249 -
2250 2088 ASSERT((hat == ksfmmup) ||
2251 2089 AS_LOCK_HELD(hat->sfmmu_as, &hat->sfmmu_as->a_lock));
2252 2090
2253 2091 if (flags & ~SFMMU_LOAD_ALLFLAG)
2254 2092 cmn_err(CE_NOTE, "hat_memload: unsupported flags %d",
2255 2093 flags & ~SFMMU_LOAD_ALLFLAG);
2256 2094
2257 2095 if (hat->sfmmu_rmstat)
2258 2096 hat_resvstat(MMU_PAGESIZE, hat->sfmmu_as, addr);
2259 2097
2260 2098 #if defined(SF_ERRATA_57)
2261 2099 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) &&
2262 2100 (addr < errata57_limit) && (attr & PROT_EXEC) &&
2263 2101 !(flags & HAT_LOAD_SHARE)) {
2264 2102 cmn_err(CE_WARN, "hat_memload: illegal attempt to make user "
2265 2103 " page executable");
2266 2104 attr &= ~PROT_EXEC;
2267 2105 }
2268 2106 #endif
2269 2107
2270 2108 sfmmu_memtte(&tte, pp->p_pagenum, attr, TTE8K);
2271 2109 (void) sfmmu_tteload_array(hat, &tte, addr, &pp, flags, rid);
2272 2110
2273 2111 /*
2274 2112 * Check TSB and TLB page sizes.
2275 2113 */
2276 2114 if ((flags & HAT_LOAD_SHARE) == 0) {
2277 2115 sfmmu_check_page_sizes(hat, 1);
2278 2116 }
2279 2117 }
2280 2118
2281 2119 /*
2282 2120 * hat_devload can be called to map real memory (e.g.
2283 2121 * /dev/kmem) and even though hat_devload will determine pf is
2284 2122 * for memory, it will be unable to get a shared lock on the
2285 2123 * page (because someone else has it exclusively) and will
2286 2124 * pass dp = NULL. If tteload doesn't get a non-NULL
2287 2125 * page pointer it can't cache memory.
2288 2126 */
↓ open down ↓ |
29 lines elided |
↑ open up ↑ |
2289 2127 void
2290 2128 hat_devload(struct hat *hat, caddr_t addr, size_t len, pfn_t pfn,
2291 2129 uint_t attr, int flags)
2292 2130 {
2293 2131 tte_t tte;
2294 2132 struct page *pp = NULL;
2295 2133 int use_lgpg = 0;
2296 2134
2297 2135 ASSERT(hat != NULL);
2298 2136
2299 - if (hat->sfmmu_xhat_provider) {
2300 - XHAT_DEVLOAD(hat, addr, len, pfn, attr, flags);
2301 - return;
2302 - }
2303 -
2304 2137 ASSERT(!(flags & ~SFMMU_LOAD_ALLFLAG));
2305 2138 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR));
2306 2139 ASSERT((hat == ksfmmup) ||
2307 2140 AS_LOCK_HELD(hat->sfmmu_as, &hat->sfmmu_as->a_lock));
2308 2141 if (len == 0)
2309 2142 panic("hat_devload: zero len");
2310 2143 if (flags & ~SFMMU_LOAD_ALLFLAG)
2311 2144 cmn_err(CE_NOTE, "hat_devload: unsupported flags %d",
2312 2145 flags & ~SFMMU_LOAD_ALLFLAG);
2313 2146
2314 2147 #if defined(SF_ERRATA_57)
2315 2148 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) &&
2316 2149 (addr < errata57_limit) && (attr & PROT_EXEC) &&
2317 2150 !(flags & HAT_LOAD_SHARE)) {
2318 2151 cmn_err(CE_WARN, "hat_devload: illegal attempt to make user "
2319 2152 " page executable");
2320 2153 attr &= ~PROT_EXEC;
2321 2154 }
2322 2155 #endif
2323 2156
2324 2157 /*
2325 2158 * If it's a memory page find its pp
2326 2159 */
2327 2160 if (!(flags & HAT_LOAD_NOCONSIST) && pf_is_memory(pfn)) {
2328 2161 pp = page_numtopp_nolock(pfn);
2329 2162 if (pp == NULL) {
2330 2163 flags |= HAT_LOAD_NOCONSIST;
2331 2164 } else {
2332 2165 if (PP_ISFREE(pp)) {
2333 2166 panic("hat_memload: loading "
2334 2167 "a mapping to free page %p",
2335 2168 (void *)pp);
2336 2169 }
2337 2170 if (!PAGE_LOCKED(pp) && !PP_ISNORELOC(pp)) {
2338 2171 panic("hat_memload: loading a mapping "
2339 2172 "to unlocked relocatable page %p",
2340 2173 (void *)pp);
2341 2174 }
2342 2175 ASSERT(len == MMU_PAGESIZE);
2343 2176 }
2344 2177 }
2345 2178
2346 2179 if (hat->sfmmu_rmstat)
2347 2180 hat_resvstat(len, hat->sfmmu_as, addr);
2348 2181
2349 2182 if (flags & HAT_LOAD_NOCONSIST) {
2350 2183 attr |= SFMMU_UNCACHEVTTE;
2351 2184 use_lgpg = 1;
2352 2185 }
2353 2186 if (!pf_is_memory(pfn)) {
2354 2187 attr |= SFMMU_UNCACHEPTTE | HAT_NOSYNC;
2355 2188 use_lgpg = 1;
2356 2189 switch (attr & HAT_ORDER_MASK) {
2357 2190 case HAT_STRICTORDER:
2358 2191 case HAT_UNORDERED_OK:
2359 2192 /*
2360 2193 * we set the side effect bit for all non
2361 2194 * memory mappings unless merging is ok
2362 2195 */
2363 2196 attr |= SFMMU_SIDEFFECT;
2364 2197 break;
2365 2198 case HAT_MERGING_OK:
2366 2199 case HAT_LOADCACHING_OK:
2367 2200 case HAT_STORECACHING_OK:
2368 2201 break;
2369 2202 default:
2370 2203 panic("hat_devload: bad attr");
2371 2204 break;
2372 2205 }
2373 2206 }
2374 2207 while (len) {
2375 2208 if (!use_lgpg) {
2376 2209 sfmmu_memtte(&tte, pfn, attr, TTE8K);
2377 2210 (void) sfmmu_tteload_array(hat, &tte, addr, &pp,
2378 2211 flags, SFMMU_INVALID_SHMERID);
2379 2212 len -= MMU_PAGESIZE;
2380 2213 addr += MMU_PAGESIZE;
2381 2214 pfn++;
2382 2215 continue;
2383 2216 }
2384 2217 /*
2385 2218 * try to use large pages, check va/pa alignments
2386 2219 * Note that 32M/256M page sizes are not (yet) supported.
2387 2220 */
2388 2221 if ((len >= MMU_PAGESIZE4M) &&
2389 2222 !((uintptr_t)addr & MMU_PAGEOFFSET4M) &&
2390 2223 !(disable_large_pages & (1 << TTE4M)) &&
2391 2224 !(mmu_ptob(pfn) & MMU_PAGEOFFSET4M)) {
2392 2225 sfmmu_memtte(&tte, pfn, attr, TTE4M);
2393 2226 (void) sfmmu_tteload_array(hat, &tte, addr, &pp,
2394 2227 flags, SFMMU_INVALID_SHMERID);
2395 2228 len -= MMU_PAGESIZE4M;
2396 2229 addr += MMU_PAGESIZE4M;
2397 2230 pfn += MMU_PAGESIZE4M / MMU_PAGESIZE;
2398 2231 } else if ((len >= MMU_PAGESIZE512K) &&
2399 2232 !((uintptr_t)addr & MMU_PAGEOFFSET512K) &&
2400 2233 !(disable_large_pages & (1 << TTE512K)) &&
2401 2234 !(mmu_ptob(pfn) & MMU_PAGEOFFSET512K)) {
2402 2235 sfmmu_memtte(&tte, pfn, attr, TTE512K);
2403 2236 (void) sfmmu_tteload_array(hat, &tte, addr, &pp,
2404 2237 flags, SFMMU_INVALID_SHMERID);
2405 2238 len -= MMU_PAGESIZE512K;
2406 2239 addr += MMU_PAGESIZE512K;
2407 2240 pfn += MMU_PAGESIZE512K / MMU_PAGESIZE;
2408 2241 } else if ((len >= MMU_PAGESIZE64K) &&
2409 2242 !((uintptr_t)addr & MMU_PAGEOFFSET64K) &&
2410 2243 !(disable_large_pages & (1 << TTE64K)) &&
2411 2244 !(mmu_ptob(pfn) & MMU_PAGEOFFSET64K)) {
2412 2245 sfmmu_memtte(&tte, pfn, attr, TTE64K);
2413 2246 (void) sfmmu_tteload_array(hat, &tte, addr, &pp,
2414 2247 flags, SFMMU_INVALID_SHMERID);
2415 2248 len -= MMU_PAGESIZE64K;
2416 2249 addr += MMU_PAGESIZE64K;
2417 2250 pfn += MMU_PAGESIZE64K / MMU_PAGESIZE;
2418 2251 } else {
2419 2252 sfmmu_memtte(&tte, pfn, attr, TTE8K);
2420 2253 (void) sfmmu_tteload_array(hat, &tte, addr, &pp,
2421 2254 flags, SFMMU_INVALID_SHMERID);
2422 2255 len -= MMU_PAGESIZE;
2423 2256 addr += MMU_PAGESIZE;
2424 2257 pfn++;
2425 2258 }
2426 2259 }
2427 2260
2428 2261 /*
2429 2262 * Check TSB and TLB page sizes.
2430 2263 */
2431 2264 if ((flags & HAT_LOAD_SHARE) == 0) {
2432 2265 sfmmu_check_page_sizes(hat, 1);
2433 2266 }
2434 2267 }
2435 2268
2436 2269 void
2437 2270 hat_memload_array(struct hat *hat, caddr_t addr, size_t len,
2438 2271 struct page **pps, uint_t attr, uint_t flags)
2439 2272 {
↓ open down ↓ |
126 lines elided |
↑ open up ↑ |
2440 2273 hat_do_memload_array(hat, addr, len, pps, attr, flags,
2441 2274 SFMMU_INVALID_SHMERID);
2442 2275 }
2443 2276
2444 2277 void
2445 2278 hat_memload_array_region(struct hat *hat, caddr_t addr, size_t len,
2446 2279 struct page **pps, uint_t attr, uint_t flags,
2447 2280 hat_region_cookie_t rcookie)
2448 2281 {
2449 2282 uint_t rid;
2450 - if (rcookie == HAT_INVALID_REGION_COOKIE ||
2451 - hat->sfmmu_xhat_provider != NULL) {
2283 + if (rcookie == HAT_INVALID_REGION_COOKIE) {
2452 2284 hat_do_memload_array(hat, addr, len, pps, attr, flags,
2453 2285 SFMMU_INVALID_SHMERID);
2454 2286 return;
2455 2287 }
2456 2288 rid = (uint_t)((uint64_t)rcookie);
2457 2289 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
2458 2290 hat_do_memload_array(hat, addr, len, pps, attr, flags, rid);
2459 2291 }
2460 2292
2461 2293 /*
2462 2294 * Map the largest extend possible out of the page array. The array may NOT
2463 2295 * be in order. The largest possible mapping a page can have
2464 2296 * is specified in the p_szc field. The p_szc field
2465 2297 * cannot change as long as there any mappings (large or small)
2466 2298 * to any of the pages that make up the large page. (ie. any
2467 2299 * promotion/demotion of page size is not up to the hat but up to
2468 2300 * the page free list manager). The array
2469 2301 * should consist of properly aligned contigous pages that are
2470 2302 * part of a big page for a large mapping to be created.
2471 2303 */
2472 2304 static void
2473 2305 hat_do_memload_array(struct hat *hat, caddr_t addr, size_t len,
2474 2306 struct page **pps, uint_t attr, uint_t flags, uint_t rid)
2475 2307 {
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
2476 2308 int ttesz;
2477 2309 size_t mapsz;
2478 2310 pgcnt_t numpg, npgs;
2479 2311 tte_t tte;
2480 2312 page_t *pp;
2481 2313 uint_t large_pages_disable;
2482 2314
2483 2315 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET));
2484 2316 SFMMU_VALIDATE_HMERID(hat, rid, addr, len);
2485 2317
2486 - if (hat->sfmmu_xhat_provider) {
2487 - ASSERT(!SFMMU_IS_SHMERID_VALID(rid));
2488 - XHAT_MEMLOAD_ARRAY(hat, addr, len, pps, attr, flags);
2489 - return;
2490 - }
2491 -
2492 2318 if (hat->sfmmu_rmstat)
2493 2319 hat_resvstat(len, hat->sfmmu_as, addr);
2494 2320
2495 2321 #if defined(SF_ERRATA_57)
2496 2322 if ((hat != ksfmmup) && AS_TYPE_64BIT(hat->sfmmu_as) &&
2497 2323 (addr < errata57_limit) && (attr & PROT_EXEC) &&
2498 2324 !(flags & HAT_LOAD_SHARE)) {
2499 2325 cmn_err(CE_WARN, "hat_memload_array: illegal attempt to make "
2500 2326 "user page executable");
2501 2327 attr &= ~PROT_EXEC;
2502 2328 }
2503 2329 #endif
2504 2330
2505 2331 /* Get number of pages */
2506 2332 npgs = len >> MMU_PAGESHIFT;
2507 2333
2508 2334 if (flags & HAT_LOAD_SHARE) {
2509 2335 large_pages_disable = disable_ism_large_pages;
2510 2336 } else {
2511 2337 large_pages_disable = disable_large_pages;
2512 2338 }
2513 2339
2514 2340 if (npgs < NHMENTS || large_pages_disable == LARGE_PAGES_OFF) {
2515 2341 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags, npgs,
2516 2342 rid);
2517 2343 return;
2518 2344 }
2519 2345
2520 2346 while (npgs >= NHMENTS) {
2521 2347 pp = *pps;
2522 2348 for (ttesz = pp->p_szc; ttesz != TTE8K; ttesz--) {
2523 2349 /*
2524 2350 * Check if this page size is disabled.
2525 2351 */
2526 2352 if (large_pages_disable & (1 << ttesz))
2527 2353 continue;
2528 2354
2529 2355 numpg = TTEPAGES(ttesz);
2530 2356 mapsz = numpg << MMU_PAGESHIFT;
2531 2357 if ((npgs >= numpg) &&
2532 2358 IS_P2ALIGNED(addr, mapsz) &&
2533 2359 IS_P2ALIGNED(pp->p_pagenum, numpg)) {
2534 2360 /*
2535 2361 * At this point we have enough pages and
2536 2362 * we know the virtual address and the pfn
2537 2363 * are properly aligned. We still need
2538 2364 * to check for physical contiguity but since
2539 2365 * it is very likely that this is the case
2540 2366 * we will assume they are so and undo
2541 2367 * the request if necessary. It would
2542 2368 * be great if we could get a hint flag
2543 2369 * like HAT_CONTIG which would tell us
2544 2370 * the pages are contigous for sure.
2545 2371 */
2546 2372 sfmmu_memtte(&tte, (*pps)->p_pagenum,
2547 2373 attr, ttesz);
2548 2374 if (!sfmmu_tteload_array(hat, &tte, addr,
2549 2375 pps, flags, rid)) {
2550 2376 break;
2551 2377 }
2552 2378 }
2553 2379 }
2554 2380 if (ttesz == TTE8K) {
2555 2381 /*
2556 2382 * We were not able to map array using a large page
2557 2383 * batch a hmeblk or fraction at a time.
2558 2384 */
2559 2385 numpg = ((uintptr_t)addr >> MMU_PAGESHIFT)
2560 2386 & (NHMENTS-1);
2561 2387 numpg = NHMENTS - numpg;
2562 2388 ASSERT(numpg <= npgs);
2563 2389 mapsz = numpg * MMU_PAGESIZE;
2564 2390 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags,
2565 2391 numpg, rid);
2566 2392 }
2567 2393 addr += mapsz;
2568 2394 npgs -= numpg;
2569 2395 pps += numpg;
2570 2396 }
2571 2397
2572 2398 if (npgs) {
2573 2399 sfmmu_memload_batchsmall(hat, addr, pps, attr, flags, npgs,
2574 2400 rid);
2575 2401 }
2576 2402
2577 2403 /*
2578 2404 * Check TSB and TLB page sizes.
2579 2405 */
2580 2406 if ((flags & HAT_LOAD_SHARE) == 0) {
2581 2407 sfmmu_check_page_sizes(hat, 1);
2582 2408 }
2583 2409 }
2584 2410
2585 2411 /*
2586 2412 * Function tries to batch 8K pages into the same hme blk.
2587 2413 */
2588 2414 static void
2589 2415 sfmmu_memload_batchsmall(struct hat *hat, caddr_t vaddr, page_t **pps,
2590 2416 uint_t attr, uint_t flags, pgcnt_t npgs, uint_t rid)
2591 2417 {
2592 2418 tte_t tte;
2593 2419 page_t *pp;
2594 2420 struct hmehash_bucket *hmebp;
2595 2421 struct hme_blk *hmeblkp;
2596 2422 int index;
2597 2423
2598 2424 while (npgs) {
2599 2425 /*
2600 2426 * Acquire the hash bucket.
2601 2427 */
2602 2428 hmebp = sfmmu_tteload_acquire_hashbucket(hat, vaddr, TTE8K,
2603 2429 rid);
2604 2430 ASSERT(hmebp);
2605 2431
2606 2432 /*
2607 2433 * Find the hment block.
2608 2434 */
2609 2435 hmeblkp = sfmmu_tteload_find_hmeblk(hat, hmebp, vaddr,
2610 2436 TTE8K, flags, rid);
2611 2437 ASSERT(hmeblkp);
2612 2438
2613 2439 do {
2614 2440 /*
2615 2441 * Make the tte.
2616 2442 */
2617 2443 pp = *pps;
2618 2444 sfmmu_memtte(&tte, pp->p_pagenum, attr, TTE8K);
2619 2445
2620 2446 /*
2621 2447 * Add the translation.
2622 2448 */
2623 2449 (void) sfmmu_tteload_addentry(hat, hmeblkp, &tte,
2624 2450 vaddr, pps, flags, rid);
2625 2451
2626 2452 /*
2627 2453 * Goto next page.
2628 2454 */
2629 2455 pps++;
2630 2456 npgs--;
2631 2457
2632 2458 /*
2633 2459 * Goto next address.
2634 2460 */
2635 2461 vaddr += MMU_PAGESIZE;
2636 2462
2637 2463 /*
2638 2464 * Don't crossover into a different hmentblk.
2639 2465 */
2640 2466 index = (int)(((uintptr_t)vaddr >> MMU_PAGESHIFT) &
2641 2467 (NHMENTS-1));
2642 2468
2643 2469 } while (index != 0 && npgs != 0);
2644 2470
2645 2471 /*
2646 2472 * Release the hash bucket.
2647 2473 */
2648 2474
2649 2475 sfmmu_tteload_release_hashbucket(hmebp);
2650 2476 }
2651 2477 }
2652 2478
2653 2479 /*
2654 2480 * Construct a tte for a page:
2655 2481 *
2656 2482 * tte_valid = 1
2657 2483 * tte_size2 = size & TTE_SZ2_BITS (Panther and Olympus-C only)
2658 2484 * tte_size = size
2659 2485 * tte_nfo = attr & HAT_NOFAULT
2660 2486 * tte_ie = attr & HAT_STRUCTURE_LE
2661 2487 * tte_hmenum = hmenum
2662 2488 * tte_pahi = pp->p_pagenum >> TTE_PASHIFT;
2663 2489 * tte_palo = pp->p_pagenum & TTE_PALOMASK;
2664 2490 * tte_ref = 1 (optimization)
2665 2491 * tte_wr_perm = attr & PROT_WRITE;
2666 2492 * tte_no_sync = attr & HAT_NOSYNC
2667 2493 * tte_lock = attr & SFMMU_LOCKTTE
2668 2494 * tte_cp = !(attr & SFMMU_UNCACHEPTTE)
2669 2495 * tte_cv = !(attr & SFMMU_UNCACHEVTTE)
2670 2496 * tte_e = attr & SFMMU_SIDEFFECT
2671 2497 * tte_priv = !(attr & PROT_USER)
2672 2498 * tte_hwwr = if nosync is set and it is writable we set the mod bit (opt)
2673 2499 * tte_glb = 0
2674 2500 */
2675 2501 void
2676 2502 sfmmu_memtte(tte_t *ttep, pfn_t pfn, uint_t attr, int tte_sz)
2677 2503 {
2678 2504 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR));
2679 2505
2680 2506 ttep->tte_inthi = MAKE_TTE_INTHI(pfn, attr, tte_sz, 0 /* hmenum */);
2681 2507 ttep->tte_intlo = MAKE_TTE_INTLO(pfn, attr, tte_sz, 0 /* hmenum */);
2682 2508
2683 2509 if (TTE_IS_NOSYNC(ttep)) {
2684 2510 TTE_SET_REF(ttep);
2685 2511 if (TTE_IS_WRITABLE(ttep)) {
2686 2512 TTE_SET_MOD(ttep);
2687 2513 }
2688 2514 }
2689 2515 if (TTE_IS_NFO(ttep) && TTE_IS_EXECUTABLE(ttep)) {
2690 2516 panic("sfmmu_memtte: can't set both NFO and EXEC bits");
2691 2517 }
2692 2518 }
2693 2519
2694 2520 /*
2695 2521 * This function will add a translation to the hme_blk and allocate the
2696 2522 * hme_blk if one does not exist.
2697 2523 * If a page structure is specified then it will add the
2698 2524 * corresponding hment to the mapping list.
2699 2525 * It will also update the hmenum field for the tte.
2700 2526 *
2701 2527 * Currently this function is only used for kernel mappings.
2702 2528 * So pass invalid region to sfmmu_tteload_array().
2703 2529 */
2704 2530 void
2705 2531 sfmmu_tteload(struct hat *sfmmup, tte_t *ttep, caddr_t vaddr, page_t *pp,
2706 2532 uint_t flags)
2707 2533 {
2708 2534 ASSERT(sfmmup == ksfmmup);
2709 2535 (void) sfmmu_tteload_array(sfmmup, ttep, vaddr, &pp, flags,
2710 2536 SFMMU_INVALID_SHMERID);
2711 2537 }
2712 2538
2713 2539 /*
2714 2540 * Load (ttep != NULL) or unload (ttep == NULL) one entry in the TSB.
2715 2541 * Assumes that a particular page size may only be resident in one TSB.
2716 2542 */
2717 2543 static void
2718 2544 sfmmu_mod_tsb(sfmmu_t *sfmmup, caddr_t vaddr, tte_t *ttep, int ttesz)
2719 2545 {
2720 2546 struct tsb_info *tsbinfop = NULL;
2721 2547 uint64_t tag;
2722 2548 struct tsbe *tsbe_addr;
2723 2549 uint64_t tsb_base;
2724 2550 uint_t tsb_size;
2725 2551 int vpshift = MMU_PAGESHIFT;
2726 2552 int phys = 0;
2727 2553
2728 2554 if (sfmmup == ksfmmup) { /* No support for 32/256M ksfmmu pages */
2729 2555 phys = ktsb_phys;
2730 2556 if (ttesz >= TTE4M) {
2731 2557 #ifndef sun4v
2732 2558 ASSERT((ttesz != TTE32M) && (ttesz != TTE256M));
2733 2559 #endif
2734 2560 tsb_base = (phys)? ktsb4m_pbase : (uint64_t)ktsb4m_base;
2735 2561 tsb_size = ktsb4m_szcode;
2736 2562 } else {
2737 2563 tsb_base = (phys)? ktsb_pbase : (uint64_t)ktsb_base;
2738 2564 tsb_size = ktsb_szcode;
2739 2565 }
2740 2566 } else {
2741 2567 SFMMU_GET_TSBINFO(tsbinfop, sfmmup, ttesz);
2742 2568
2743 2569 /*
2744 2570 * If there isn't a TSB for this page size, or the TSB is
2745 2571 * swapped out, there is nothing to do. Note that the latter
2746 2572 * case seems impossible but can occur if hat_pageunload()
2747 2573 * is called on an ISM mapping while the process is swapped
2748 2574 * out.
2749 2575 */
2750 2576 if (tsbinfop == NULL || (tsbinfop->tsb_flags & TSB_SWAPPED))
2751 2577 return;
2752 2578
2753 2579 /*
2754 2580 * If another thread is in the middle of relocating a TSB
2755 2581 * we can't unload the entry so set a flag so that the
2756 2582 * TSB will be flushed before it can be accessed by the
2757 2583 * process.
2758 2584 */
2759 2585 if ((tsbinfop->tsb_flags & TSB_RELOC_FLAG) != 0) {
2760 2586 if (ttep == NULL)
2761 2587 tsbinfop->tsb_flags |= TSB_FLUSH_NEEDED;
2762 2588 return;
2763 2589 }
2764 2590 #if defined(UTSB_PHYS)
2765 2591 phys = 1;
2766 2592 tsb_base = (uint64_t)tsbinfop->tsb_pa;
2767 2593 #else
2768 2594 tsb_base = (uint64_t)tsbinfop->tsb_va;
2769 2595 #endif
2770 2596 tsb_size = tsbinfop->tsb_szc;
2771 2597 }
2772 2598 if (ttesz >= TTE4M)
2773 2599 vpshift = MMU_PAGESHIFT4M;
2774 2600
2775 2601 tsbe_addr = sfmmu_get_tsbe(tsb_base, vaddr, vpshift, tsb_size);
2776 2602 tag = sfmmu_make_tsbtag(vaddr);
2777 2603
2778 2604 if (ttep == NULL) {
2779 2605 sfmmu_unload_tsbe(tsbe_addr, tag, phys);
2780 2606 } else {
2781 2607 if (ttesz >= TTE4M) {
2782 2608 SFMMU_STAT(sf_tsb_load4m);
2783 2609 } else {
2784 2610 SFMMU_STAT(sf_tsb_load8k);
2785 2611 }
2786 2612
2787 2613 sfmmu_load_tsbe(tsbe_addr, tag, ttep, phys);
2788 2614 }
2789 2615 }
2790 2616
2791 2617 /*
2792 2618 * Unmap all entries from [start, end) matching the given page size.
2793 2619 *
2794 2620 * This function is used primarily to unmap replicated 64K or 512K entries
2795 2621 * from the TSB that are inserted using the base page size TSB pointer, but
2796 2622 * it may also be called to unmap a range of addresses from the TSB.
2797 2623 */
2798 2624 void
2799 2625 sfmmu_unload_tsb_range(sfmmu_t *sfmmup, caddr_t start, caddr_t end, int ttesz)
2800 2626 {
2801 2627 struct tsb_info *tsbinfop;
2802 2628 uint64_t tag;
2803 2629 struct tsbe *tsbe_addr;
2804 2630 caddr_t vaddr;
2805 2631 uint64_t tsb_base;
2806 2632 int vpshift, vpgsz;
2807 2633 uint_t tsb_size;
2808 2634 int phys = 0;
2809 2635
2810 2636 /*
2811 2637 * Assumptions:
2812 2638 * If ttesz == 8K, 64K or 512K, we walk through the range 8K
2813 2639 * at a time shooting down any valid entries we encounter.
2814 2640 *
2815 2641 * If ttesz >= 4M we walk the range 4M at a time shooting
2816 2642 * down any valid mappings we find.
2817 2643 */
2818 2644 if (sfmmup == ksfmmup) {
2819 2645 phys = ktsb_phys;
2820 2646 if (ttesz >= TTE4M) {
2821 2647 #ifndef sun4v
2822 2648 ASSERT((ttesz != TTE32M) && (ttesz != TTE256M));
2823 2649 #endif
2824 2650 tsb_base = (phys)? ktsb4m_pbase : (uint64_t)ktsb4m_base;
2825 2651 tsb_size = ktsb4m_szcode;
2826 2652 } else {
2827 2653 tsb_base = (phys)? ktsb_pbase : (uint64_t)ktsb_base;
2828 2654 tsb_size = ktsb_szcode;
2829 2655 }
2830 2656 } else {
2831 2657 SFMMU_GET_TSBINFO(tsbinfop, sfmmup, ttesz);
2832 2658
2833 2659 /*
2834 2660 * If there isn't a TSB for this page size, or the TSB is
2835 2661 * swapped out, there is nothing to do. Note that the latter
2836 2662 * case seems impossible but can occur if hat_pageunload()
2837 2663 * is called on an ISM mapping while the process is swapped
2838 2664 * out.
2839 2665 */
2840 2666 if (tsbinfop == NULL || (tsbinfop->tsb_flags & TSB_SWAPPED))
2841 2667 return;
2842 2668
2843 2669 /*
2844 2670 * If another thread is in the middle of relocating a TSB
2845 2671 * we can't unload the entry so set a flag so that the
2846 2672 * TSB will be flushed before it can be accessed by the
2847 2673 * process.
2848 2674 */
2849 2675 if ((tsbinfop->tsb_flags & TSB_RELOC_FLAG) != 0) {
2850 2676 tsbinfop->tsb_flags |= TSB_FLUSH_NEEDED;
2851 2677 return;
2852 2678 }
2853 2679 #if defined(UTSB_PHYS)
2854 2680 phys = 1;
2855 2681 tsb_base = (uint64_t)tsbinfop->tsb_pa;
2856 2682 #else
2857 2683 tsb_base = (uint64_t)tsbinfop->tsb_va;
2858 2684 #endif
2859 2685 tsb_size = tsbinfop->tsb_szc;
2860 2686 }
2861 2687 if (ttesz >= TTE4M) {
2862 2688 vpshift = MMU_PAGESHIFT4M;
2863 2689 vpgsz = MMU_PAGESIZE4M;
2864 2690 } else {
2865 2691 vpshift = MMU_PAGESHIFT;
2866 2692 vpgsz = MMU_PAGESIZE;
2867 2693 }
2868 2694
2869 2695 for (vaddr = start; vaddr < end; vaddr += vpgsz) {
2870 2696 tag = sfmmu_make_tsbtag(vaddr);
2871 2697 tsbe_addr = sfmmu_get_tsbe(tsb_base, vaddr, vpshift, tsb_size);
2872 2698 sfmmu_unload_tsbe(tsbe_addr, tag, phys);
2873 2699 }
2874 2700 }
2875 2701
2876 2702 /*
2877 2703 * Select the optimum TSB size given the number of mappings
2878 2704 * that need to be cached.
2879 2705 */
2880 2706 static int
2881 2707 sfmmu_select_tsb_szc(pgcnt_t pgcnt)
2882 2708 {
2883 2709 int szc = 0;
2884 2710
2885 2711 #ifdef DEBUG
2886 2712 if (tsb_grow_stress) {
2887 2713 uint32_t randval = (uint32_t)gettick() >> 4;
2888 2714 return (randval % (tsb_max_growsize + 1));
2889 2715 }
2890 2716 #endif /* DEBUG */
2891 2717
2892 2718 while ((szc < tsb_max_growsize) && (pgcnt > SFMMU_RSS_TSBSIZE(szc)))
2893 2719 szc++;
2894 2720 return (szc);
2895 2721 }
2896 2722
2897 2723 /*
2898 2724 * This function will add a translation to the hme_blk and allocate the
2899 2725 * hme_blk if one does not exist.
2900 2726 * If a page structure is specified then it will add the
2901 2727 * corresponding hment to the mapping list.
2902 2728 * It will also update the hmenum field for the tte.
2903 2729 * Furthermore, it attempts to create a large page translation
2904 2730 * for <addr,hat> at page array pps. It assumes addr and first
2905 2731 * pp is correctly aligned. It returns 0 if successful and 1 otherwise.
2906 2732 */
2907 2733 static int
2908 2734 sfmmu_tteload_array(sfmmu_t *sfmmup, tte_t *ttep, caddr_t vaddr,
2909 2735 page_t **pps, uint_t flags, uint_t rid)
2910 2736 {
2911 2737 struct hmehash_bucket *hmebp;
2912 2738 struct hme_blk *hmeblkp;
2913 2739 int ret;
2914 2740 uint_t size;
2915 2741
2916 2742 /*
2917 2743 * Get mapping size.
2918 2744 */
2919 2745 size = TTE_CSZ(ttep);
2920 2746 ASSERT(!((uintptr_t)vaddr & TTE_PAGE_OFFSET(size)));
2921 2747
2922 2748 /*
2923 2749 * Acquire the hash bucket.
2924 2750 */
2925 2751 hmebp = sfmmu_tteload_acquire_hashbucket(sfmmup, vaddr, size, rid);
2926 2752 ASSERT(hmebp);
2927 2753
2928 2754 /*
2929 2755 * Find the hment block.
2930 2756 */
2931 2757 hmeblkp = sfmmu_tteload_find_hmeblk(sfmmup, hmebp, vaddr, size, flags,
2932 2758 rid);
2933 2759 ASSERT(hmeblkp);
2934 2760
2935 2761 /*
2936 2762 * Add the translation.
2937 2763 */
2938 2764 ret = sfmmu_tteload_addentry(sfmmup, hmeblkp, ttep, vaddr, pps, flags,
2939 2765 rid);
2940 2766
2941 2767 /*
2942 2768 * Release the hash bucket.
2943 2769 */
2944 2770 sfmmu_tteload_release_hashbucket(hmebp);
2945 2771
2946 2772 return (ret);
2947 2773 }
2948 2774
2949 2775 /*
2950 2776 * Function locks and returns a pointer to the hash bucket for vaddr and size.
2951 2777 */
2952 2778 static struct hmehash_bucket *
2953 2779 sfmmu_tteload_acquire_hashbucket(sfmmu_t *sfmmup, caddr_t vaddr, int size,
2954 2780 uint_t rid)
2955 2781 {
2956 2782 struct hmehash_bucket *hmebp;
2957 2783 int hmeshift;
2958 2784 void *htagid = sfmmutohtagid(sfmmup, rid);
2959 2785
2960 2786 ASSERT(htagid != NULL);
2961 2787
2962 2788 hmeshift = HME_HASH_SHIFT(size);
2963 2789
2964 2790 hmebp = HME_HASH_FUNCTION(htagid, vaddr, hmeshift);
2965 2791
2966 2792 SFMMU_HASH_LOCK(hmebp);
2967 2793
2968 2794 return (hmebp);
2969 2795 }
2970 2796
2971 2797 /*
2972 2798 * Function returns a pointer to an hmeblk in the hash bucket, hmebp. If the
2973 2799 * hmeblk doesn't exists for the [sfmmup, vaddr & size] signature, a hmeblk is
2974 2800 * allocated.
2975 2801 */
2976 2802 static struct hme_blk *
2977 2803 sfmmu_tteload_find_hmeblk(sfmmu_t *sfmmup, struct hmehash_bucket *hmebp,
2978 2804 caddr_t vaddr, uint_t size, uint_t flags, uint_t rid)
2979 2805 {
2980 2806 hmeblk_tag hblktag;
2981 2807 int hmeshift;
2982 2808 struct hme_blk *hmeblkp, *pr_hblk, *list = NULL;
2983 2809
2984 2810 SFMMU_VALIDATE_HMERID(sfmmup, rid, vaddr, TTEBYTES(size));
2985 2811
2986 2812 hblktag.htag_id = sfmmutohtagid(sfmmup, rid);
2987 2813 ASSERT(hblktag.htag_id != NULL);
2988 2814 hmeshift = HME_HASH_SHIFT(size);
2989 2815 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift);
2990 2816 hblktag.htag_rehash = HME_HASH_REHASH(size);
2991 2817 hblktag.htag_rid = rid;
2992 2818
2993 2819 ttearray_realloc:
2994 2820
2995 2821 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, pr_hblk, &list);
2996 2822
2997 2823 /*
2998 2824 * We block until hblk_reserve_lock is released; it's held by
2999 2825 * the thread, temporarily using hblk_reserve, until hblk_reserve is
3000 2826 * replaced by a hblk from sfmmu8_cache.
3001 2827 */
3002 2828 if (hmeblkp == (struct hme_blk *)hblk_reserve &&
3003 2829 hblk_reserve_thread != curthread) {
3004 2830 SFMMU_HASH_UNLOCK(hmebp);
3005 2831 mutex_enter(&hblk_reserve_lock);
3006 2832 mutex_exit(&hblk_reserve_lock);
3007 2833 SFMMU_STAT(sf_hblk_reserve_hit);
3008 2834 SFMMU_HASH_LOCK(hmebp);
3009 2835 goto ttearray_realloc;
3010 2836 }
3011 2837
3012 2838 if (hmeblkp == NULL) {
3013 2839 hmeblkp = sfmmu_hblk_alloc(sfmmup, vaddr, hmebp, size,
3014 2840 hblktag, flags, rid);
3015 2841 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || hmeblkp->hblk_shared);
3016 2842 ASSERT(SFMMU_IS_SHMERID_VALID(rid) || !hmeblkp->hblk_shared);
3017 2843 } else {
3018 2844 /*
3019 2845 * It is possible for 8k and 64k hblks to collide since they
3020 2846 * have the same rehash value. This is because we
3021 2847 * lazily free hblks and 8K/64K blks could be lingering.
3022 2848 * If we find size mismatch we free the block and & try again.
3023 2849 */
3024 2850 if (get_hblk_ttesz(hmeblkp) != size) {
3025 2851 ASSERT(!hmeblkp->hblk_vcnt);
3026 2852 ASSERT(!hmeblkp->hblk_hmecnt);
3027 2853 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
3028 2854 &list, 0);
3029 2855 goto ttearray_realloc;
3030 2856 }
3031 2857 if (hmeblkp->hblk_shw_bit) {
3032 2858 /*
3033 2859 * if the hblk was previously used as a shadow hblk then
3034 2860 * we will change it to a normal hblk
3035 2861 */
3036 2862 ASSERT(!hmeblkp->hblk_shared);
3037 2863 if (hmeblkp->hblk_shw_mask) {
3038 2864 sfmmu_shadow_hcleanup(sfmmup, hmeblkp, hmebp);
3039 2865 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
3040 2866 goto ttearray_realloc;
3041 2867 } else {
3042 2868 hmeblkp->hblk_shw_bit = 0;
3043 2869 }
3044 2870 }
3045 2871 SFMMU_STAT(sf_hblk_hit);
3046 2872 }
3047 2873
3048 2874 /*
3049 2875 * hat_memload() should never call kmem_cache_free() for kernel hmeblks;
3050 2876 * see block comment showing the stacktrace in sfmmu_hblk_alloc();
3051 2877 * set the flag parameter to 1 so that sfmmu_hblks_list_purge() will
3052 2878 * just add these hmeblks to the per-cpu pending queue.
3053 2879 */
3054 2880 sfmmu_hblks_list_purge(&list, 1);
3055 2881
3056 2882 ASSERT(get_hblk_ttesz(hmeblkp) == size);
3057 2883 ASSERT(!hmeblkp->hblk_shw_bit);
3058 2884 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || hmeblkp->hblk_shared);
3059 2885 ASSERT(SFMMU_IS_SHMERID_VALID(rid) || !hmeblkp->hblk_shared);
3060 2886 ASSERT(hmeblkp->hblk_tag.htag_rid == rid);
3061 2887
3062 2888 return (hmeblkp);
3063 2889 }
3064 2890
3065 2891 /*
3066 2892 * Function adds a tte entry into the hmeblk. It returns 0 if successful and 1
3067 2893 * otherwise.
3068 2894 */
3069 2895 static int
3070 2896 sfmmu_tteload_addentry(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, tte_t *ttep,
3071 2897 caddr_t vaddr, page_t **pps, uint_t flags, uint_t rid)
3072 2898 {
3073 2899 page_t *pp = *pps;
3074 2900 int hmenum, size, remap;
3075 2901 tte_t tteold, flush_tte;
3076 2902 #ifdef DEBUG
3077 2903 tte_t orig_old;
3078 2904 #endif /* DEBUG */
3079 2905 struct sf_hment *sfhme;
3080 2906 kmutex_t *pml, *pmtx;
3081 2907 hatlock_t *hatlockp;
3082 2908 int myflt;
3083 2909
3084 2910 /*
3085 2911 * remove this panic when we decide to let user virtual address
3086 2912 * space be >= USERLIMIT.
3087 2913 */
3088 2914 if (!TTE_IS_PRIVILEGED(ttep) && vaddr >= (caddr_t)USERLIMIT)
3089 2915 panic("user addr %p in kernel space", (void *)vaddr);
3090 2916 #if defined(TTE_IS_GLOBAL)
3091 2917 if (TTE_IS_GLOBAL(ttep))
3092 2918 panic("sfmmu_tteload: creating global tte");
3093 2919 #endif
3094 2920
3095 2921 #ifdef DEBUG
3096 2922 if (pf_is_memory(sfmmu_ttetopfn(ttep, vaddr)) &&
3097 2923 !TTE_IS_PCACHEABLE(ttep) && !sfmmu_allow_nc_trans)
3098 2924 panic("sfmmu_tteload: non cacheable memory tte");
3099 2925 #endif /* DEBUG */
3100 2926
3101 2927 /* don't simulate dirty bit for writeable ISM/DISM mappings */
3102 2928 if ((flags & HAT_LOAD_SHARE) && TTE_IS_WRITABLE(ttep)) {
3103 2929 TTE_SET_REF(ttep);
3104 2930 TTE_SET_MOD(ttep);
3105 2931 }
3106 2932
3107 2933 if ((flags & HAT_LOAD_SHARE) || !TTE_IS_REF(ttep) ||
3108 2934 !TTE_IS_MOD(ttep)) {
3109 2935 /*
3110 2936 * Don't load TSB for dummy as in ISM. Also don't preload
3111 2937 * the TSB if the TTE isn't writable since we're likely to
3112 2938 * fault on it again -- preloading can be fairly expensive.
3113 2939 */
3114 2940 flags |= SFMMU_NO_TSBLOAD;
3115 2941 }
3116 2942
3117 2943 size = TTE_CSZ(ttep);
3118 2944 switch (size) {
3119 2945 case TTE8K:
3120 2946 SFMMU_STAT(sf_tteload8k);
3121 2947 break;
3122 2948 case TTE64K:
3123 2949 SFMMU_STAT(sf_tteload64k);
3124 2950 break;
3125 2951 case TTE512K:
3126 2952 SFMMU_STAT(sf_tteload512k);
3127 2953 break;
3128 2954 case TTE4M:
3129 2955 SFMMU_STAT(sf_tteload4m);
3130 2956 break;
3131 2957 case (TTE32M):
3132 2958 SFMMU_STAT(sf_tteload32m);
3133 2959 ASSERT(mmu_page_sizes == max_mmu_page_sizes);
3134 2960 break;
3135 2961 case (TTE256M):
3136 2962 SFMMU_STAT(sf_tteload256m);
3137 2963 ASSERT(mmu_page_sizes == max_mmu_page_sizes);
3138 2964 break;
3139 2965 }
3140 2966
3141 2967 ASSERT(!((uintptr_t)vaddr & TTE_PAGE_OFFSET(size)));
3142 2968 SFMMU_VALIDATE_HMERID(sfmmup, rid, vaddr, TTEBYTES(size));
3143 2969 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) || hmeblkp->hblk_shared);
3144 2970 ASSERT(SFMMU_IS_SHMERID_VALID(rid) || !hmeblkp->hblk_shared);
3145 2971
3146 2972 HBLKTOHME_IDX(sfhme, hmeblkp, vaddr, hmenum);
3147 2973
3148 2974 /*
3149 2975 * Need to grab mlist lock here so that pageunload
3150 2976 * will not change tte behind us.
3151 2977 */
3152 2978 if (pp) {
3153 2979 pml = sfmmu_mlist_enter(pp);
3154 2980 }
3155 2981
3156 2982 sfmmu_copytte(&sfhme->hme_tte, &tteold);
3157 2983 /*
3158 2984 * Look for corresponding hment and if valid verify
3159 2985 * pfns are equal.
3160 2986 */
3161 2987 remap = TTE_IS_VALID(&tteold);
3162 2988 if (remap) {
3163 2989 pfn_t new_pfn, old_pfn;
3164 2990
3165 2991 old_pfn = TTE_TO_PFN(vaddr, &tteold);
3166 2992 new_pfn = TTE_TO_PFN(vaddr, ttep);
3167 2993
3168 2994 if (flags & HAT_LOAD_REMAP) {
3169 2995 /* make sure we are remapping same type of pages */
3170 2996 if (pf_is_memory(old_pfn) != pf_is_memory(new_pfn)) {
3171 2997 panic("sfmmu_tteload - tte remap io<->memory");
3172 2998 }
3173 2999 if (old_pfn != new_pfn &&
3174 3000 (pp != NULL || sfhme->hme_page != NULL)) {
3175 3001 panic("sfmmu_tteload - tte remap pp != NULL");
3176 3002 }
3177 3003 } else if (old_pfn != new_pfn) {
3178 3004 panic("sfmmu_tteload - tte remap, hmeblkp 0x%p",
3179 3005 (void *)hmeblkp);
3180 3006 }
3181 3007 ASSERT(TTE_CSZ(&tteold) == TTE_CSZ(ttep));
3182 3008 }
3183 3009
3184 3010 if (pp) {
3185 3011 if (size == TTE8K) {
3186 3012 #ifdef VAC
3187 3013 /*
3188 3014 * Handle VAC consistency
3189 3015 */
3190 3016 if (!remap && (cache & CACHE_VAC) && !PP_ISNC(pp)) {
3191 3017 sfmmu_vac_conflict(sfmmup, vaddr, pp);
3192 3018 }
3193 3019 #endif
3194 3020
3195 3021 if (TTE_IS_WRITABLE(ttep) && PP_ISRO(pp)) {
3196 3022 pmtx = sfmmu_page_enter(pp);
3197 3023 PP_CLRRO(pp);
3198 3024 sfmmu_page_exit(pmtx);
3199 3025 } else if (!PP_ISMAPPED(pp) &&
3200 3026 (!TTE_IS_WRITABLE(ttep)) && !(PP_ISMOD(pp))) {
3201 3027 pmtx = sfmmu_page_enter(pp);
3202 3028 if (!(PP_ISMOD(pp))) {
3203 3029 PP_SETRO(pp);
3204 3030 }
3205 3031 sfmmu_page_exit(pmtx);
3206 3032 }
3207 3033
3208 3034 } else if (sfmmu_pagearray_setup(vaddr, pps, ttep, remap)) {
3209 3035 /*
3210 3036 * sfmmu_pagearray_setup failed so return
3211 3037 */
3212 3038 sfmmu_mlist_exit(pml);
3213 3039 return (1);
3214 3040 }
3215 3041 }
3216 3042
3217 3043 /*
3218 3044 * Make sure hment is not on a mapping list.
3219 3045 */
3220 3046 ASSERT(remap || (sfhme->hme_page == NULL));
3221 3047
3222 3048 /* if it is not a remap then hme->next better be NULL */
3223 3049 ASSERT((!remap) ? sfhme->hme_next == NULL : 1);
3224 3050
3225 3051 if (flags & HAT_LOAD_LOCK) {
3226 3052 if ((hmeblkp->hblk_lckcnt + 1) >= MAX_HBLK_LCKCNT) {
3227 3053 panic("too high lckcnt-hmeblk %p",
3228 3054 (void *)hmeblkp);
3229 3055 }
3230 3056 atomic_inc_32(&hmeblkp->hblk_lckcnt);
3231 3057
3232 3058 HBLK_STACK_TRACE(hmeblkp, HBLK_LOCK);
3233 3059 }
3234 3060
3235 3061 #ifdef VAC
3236 3062 if (pp && PP_ISNC(pp)) {
3237 3063 /*
3238 3064 * If the physical page is marked to be uncacheable, like
3239 3065 * by a vac conflict, make sure the new mapping is also
3240 3066 * uncacheable.
3241 3067 */
3242 3068 TTE_CLR_VCACHEABLE(ttep);
3243 3069 ASSERT(PP_GET_VCOLOR(pp) == NO_VCOLOR);
3244 3070 }
3245 3071 #endif
3246 3072 ttep->tte_hmenum = hmenum;
3247 3073
3248 3074 #ifdef DEBUG
3249 3075 orig_old = tteold;
3250 3076 #endif /* DEBUG */
3251 3077
3252 3078 while (sfmmu_modifytte_try(&tteold, ttep, &sfhme->hme_tte) < 0) {
3253 3079 if ((sfmmup == KHATID) &&
3254 3080 (flags & (HAT_LOAD_LOCK | HAT_LOAD_REMAP))) {
3255 3081 sfmmu_copytte(&sfhme->hme_tte, &tteold);
3256 3082 }
3257 3083 #ifdef DEBUG
3258 3084 chk_tte(&orig_old, &tteold, ttep, hmeblkp);
3259 3085 #endif /* DEBUG */
3260 3086 }
3261 3087 ASSERT(TTE_IS_VALID(&sfhme->hme_tte));
3262 3088
3263 3089 if (!TTE_IS_VALID(&tteold)) {
3264 3090
3265 3091 atomic_inc_16(&hmeblkp->hblk_vcnt);
3266 3092 if (rid == SFMMU_INVALID_SHMERID) {
3267 3093 atomic_inc_ulong(&sfmmup->sfmmu_ttecnt[size]);
3268 3094 } else {
3269 3095 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
3270 3096 sf_region_t *rgnp = srdp->srd_hmergnp[rid];
3271 3097 /*
3272 3098 * We already accounted for region ttecnt's in sfmmu
3273 3099 * during hat_join_region() processing. Here we
3274 3100 * only update ttecnt's in region struture.
3275 3101 */
3276 3102 atomic_inc_ulong(&rgnp->rgn_ttecnt[size]);
3277 3103 }
3278 3104 }
3279 3105
3280 3106 myflt = (astosfmmu(curthread->t_procp->p_as) == sfmmup);
3281 3107 if (size > TTE8K && (flags & HAT_LOAD_SHARE) == 0 &&
3282 3108 sfmmup != ksfmmup) {
3283 3109 uchar_t tteflag = 1 << size;
3284 3110 if (rid == SFMMU_INVALID_SHMERID) {
3285 3111 if (!(sfmmup->sfmmu_tteflags & tteflag)) {
3286 3112 hatlockp = sfmmu_hat_enter(sfmmup);
3287 3113 sfmmup->sfmmu_tteflags |= tteflag;
3288 3114 sfmmu_hat_exit(hatlockp);
3289 3115 }
3290 3116 } else if (!(sfmmup->sfmmu_rtteflags & tteflag)) {
3291 3117 hatlockp = sfmmu_hat_enter(sfmmup);
3292 3118 sfmmup->sfmmu_rtteflags |= tteflag;
3293 3119 sfmmu_hat_exit(hatlockp);
3294 3120 }
3295 3121 /*
3296 3122 * Update the current CPU tsbmiss area, so the current thread
3297 3123 * won't need to take the tsbmiss for the new pagesize.
3298 3124 * The other threads in the process will update their tsb
3299 3125 * miss area lazily in sfmmu_tsbmiss_exception() when they
3300 3126 * fail to find the translation for a newly added pagesize.
3301 3127 */
3302 3128 if (size > TTE64K && myflt) {
3303 3129 struct tsbmiss *tsbmp;
3304 3130 kpreempt_disable();
3305 3131 tsbmp = &tsbmiss_area[CPU->cpu_id];
3306 3132 if (rid == SFMMU_INVALID_SHMERID) {
3307 3133 if (!(tsbmp->uhat_tteflags & tteflag)) {
3308 3134 tsbmp->uhat_tteflags |= tteflag;
3309 3135 }
3310 3136 } else {
3311 3137 if (!(tsbmp->uhat_rtteflags & tteflag)) {
3312 3138 tsbmp->uhat_rtteflags |= tteflag;
3313 3139 }
3314 3140 }
3315 3141 kpreempt_enable();
3316 3142 }
3317 3143 }
3318 3144
3319 3145 if (size >= TTE4M && (flags & HAT_LOAD_TEXT) &&
3320 3146 !SFMMU_FLAGS_ISSET(sfmmup, HAT_4MTEXT_FLAG)) {
3321 3147 hatlockp = sfmmu_hat_enter(sfmmup);
3322 3148 SFMMU_FLAGS_SET(sfmmup, HAT_4MTEXT_FLAG);
3323 3149 sfmmu_hat_exit(hatlockp);
3324 3150 }
3325 3151
3326 3152 flush_tte.tte_intlo = (tteold.tte_intlo ^ ttep->tte_intlo) &
3327 3153 hw_tte.tte_intlo;
3328 3154 flush_tte.tte_inthi = (tteold.tte_inthi ^ ttep->tte_inthi) &
3329 3155 hw_tte.tte_inthi;
3330 3156
3331 3157 if (remap && (flush_tte.tte_inthi || flush_tte.tte_intlo)) {
3332 3158 /*
3333 3159 * If remap and new tte differs from old tte we need
3334 3160 * to sync the mod bit and flush TLB/TSB. We don't
3335 3161 * need to sync ref bit because we currently always set
3336 3162 * ref bit in tteload.
3337 3163 */
3338 3164 ASSERT(TTE_IS_REF(ttep));
3339 3165 if (TTE_IS_MOD(&tteold)) {
3340 3166 sfmmu_ttesync(sfmmup, vaddr, &tteold, pp);
3341 3167 }
3342 3168 /*
3343 3169 * hwtte bits shouldn't change for SRD hmeblks as long as SRD
3344 3170 * hmes are only used for read only text. Adding this code for
3345 3171 * completeness and future use of shared hmeblks with writable
3346 3172 * mappings of VMODSORT vnodes.
3347 3173 */
3348 3174 if (hmeblkp->hblk_shared) {
3349 3175 cpuset_t cpuset = sfmmu_rgntlb_demap(vaddr,
3350 3176 sfmmup->sfmmu_srdp->srd_hmergnp[rid], hmeblkp, 1);
3351 3177 xt_sync(cpuset);
3352 3178 SFMMU_STAT_ADD(sf_region_remap_demap, 1);
3353 3179 } else {
3354 3180 sfmmu_tlb_demap(vaddr, sfmmup, hmeblkp, 0, 0);
3355 3181 xt_sync(sfmmup->sfmmu_cpusran);
3356 3182 }
3357 3183 }
3358 3184
3359 3185 if ((flags & SFMMU_NO_TSBLOAD) == 0) {
3360 3186 /*
3361 3187 * We only preload 8K and 4M mappings into the TSB, since
3362 3188 * 64K and 512K mappings are replicated and hence don't
3363 3189 * have a single, unique TSB entry. Ditto for 32M/256M.
3364 3190 */
3365 3191 if (size == TTE8K || size == TTE4M) {
3366 3192 sf_scd_t *scdp;
3367 3193 hatlockp = sfmmu_hat_enter(sfmmup);
3368 3194 /*
3369 3195 * Don't preload private TSB if the mapping is used
3370 3196 * by the shctx in the SCD.
3371 3197 */
3372 3198 scdp = sfmmup->sfmmu_scdp;
3373 3199 if (rid == SFMMU_INVALID_SHMERID || scdp == NULL ||
3374 3200 !SF_RGNMAP_TEST(scdp->scd_hmeregion_map, rid)) {
3375 3201 sfmmu_load_tsb(sfmmup, vaddr, &sfhme->hme_tte,
3376 3202 size);
3377 3203 }
3378 3204 sfmmu_hat_exit(hatlockp);
3379 3205 }
3380 3206 }
3381 3207 if (pp) {
3382 3208 if (!remap) {
3383 3209 HME_ADD(sfhme, pp);
3384 3210 atomic_inc_16(&hmeblkp->hblk_hmecnt);
3385 3211 ASSERT(hmeblkp->hblk_hmecnt > 0);
3386 3212
3387 3213 /*
3388 3214 * Cannot ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS)
3389 3215 * see pageunload() for comment.
3390 3216 */
3391 3217 }
3392 3218 sfmmu_mlist_exit(pml);
3393 3219 }
3394 3220
3395 3221 return (0);
3396 3222 }
3397 3223 /*
3398 3224 * Function unlocks hash bucket.
3399 3225 */
3400 3226 static void
3401 3227 sfmmu_tteload_release_hashbucket(struct hmehash_bucket *hmebp)
3402 3228 {
3403 3229 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
3404 3230 SFMMU_HASH_UNLOCK(hmebp);
3405 3231 }
3406 3232
3407 3233 /*
3408 3234 * function which checks and sets up page array for a large
3409 3235 * translation. Will set p_vcolor, p_index, p_ro fields.
3410 3236 * Assumes addr and pfnum of first page are properly aligned.
3411 3237 * Will check for physical contiguity. If check fails it return
3412 3238 * non null.
3413 3239 */
3414 3240 static int
3415 3241 sfmmu_pagearray_setup(caddr_t addr, page_t **pps, tte_t *ttep, int remap)
3416 3242 {
3417 3243 int i, index, ttesz;
3418 3244 pfn_t pfnum;
3419 3245 pgcnt_t npgs;
3420 3246 page_t *pp, *pp1;
3421 3247 kmutex_t *pmtx;
3422 3248 #ifdef VAC
3423 3249 int osz;
3424 3250 int cflags = 0;
3425 3251 int vac_err = 0;
3426 3252 #endif
3427 3253 int newidx = 0;
3428 3254
3429 3255 ttesz = TTE_CSZ(ttep);
3430 3256
3431 3257 ASSERT(ttesz > TTE8K);
3432 3258
3433 3259 npgs = TTEPAGES(ttesz);
3434 3260 index = PAGESZ_TO_INDEX(ttesz);
3435 3261
3436 3262 pfnum = (*pps)->p_pagenum;
3437 3263 ASSERT(IS_P2ALIGNED(pfnum, npgs));
3438 3264
3439 3265 /*
3440 3266 * Save the first pp so we can do HAT_TMPNC at the end.
3441 3267 */
3442 3268 pp1 = *pps;
3443 3269 #ifdef VAC
3444 3270 osz = fnd_mapping_sz(pp1);
3445 3271 #endif
3446 3272
3447 3273 for (i = 0; i < npgs; i++, pps++) {
3448 3274 pp = *pps;
3449 3275 ASSERT(PAGE_LOCKED(pp));
3450 3276 ASSERT(pp->p_szc >= ttesz);
3451 3277 ASSERT(pp->p_szc == pp1->p_szc);
3452 3278 ASSERT(sfmmu_mlist_held(pp));
3453 3279
3454 3280 /*
3455 3281 * XXX is it possible to maintain P_RO on the root only?
3456 3282 */
3457 3283 if (TTE_IS_WRITABLE(ttep) && PP_ISRO(pp)) {
3458 3284 pmtx = sfmmu_page_enter(pp);
3459 3285 PP_CLRRO(pp);
3460 3286 sfmmu_page_exit(pmtx);
3461 3287 } else if (!PP_ISMAPPED(pp) && !TTE_IS_WRITABLE(ttep) &&
3462 3288 !PP_ISMOD(pp)) {
3463 3289 pmtx = sfmmu_page_enter(pp);
3464 3290 if (!(PP_ISMOD(pp))) {
3465 3291 PP_SETRO(pp);
3466 3292 }
3467 3293 sfmmu_page_exit(pmtx);
3468 3294 }
3469 3295
3470 3296 /*
3471 3297 * If this is a remap we skip vac & contiguity checks.
3472 3298 */
3473 3299 if (remap)
3474 3300 continue;
3475 3301
3476 3302 /*
3477 3303 * set p_vcolor and detect any vac conflicts.
3478 3304 */
3479 3305 #ifdef VAC
3480 3306 if (vac_err == 0) {
3481 3307 vac_err = sfmmu_vacconflict_array(addr, pp, &cflags);
3482 3308
3483 3309 }
3484 3310 #endif
3485 3311
3486 3312 /*
3487 3313 * Save current index in case we need to undo it.
3488 3314 * Note: "PAGESZ_TO_INDEX(sz) (1 << (sz))"
3489 3315 * "SFMMU_INDEX_SHIFT 6"
3490 3316 * "SFMMU_INDEX_MASK ((1 << SFMMU_INDEX_SHIFT) - 1)"
3491 3317 * "PP_MAPINDEX(p_index) (p_index & SFMMU_INDEX_MASK)"
3492 3318 *
3493 3319 * So: index = PAGESZ_TO_INDEX(ttesz);
3494 3320 * if ttesz == 1 then index = 0x2
3495 3321 * 2 then index = 0x4
3496 3322 * 3 then index = 0x8
3497 3323 * 4 then index = 0x10
3498 3324 * 5 then index = 0x20
3499 3325 * The code below checks if it's a new pagesize (ie, newidx)
3500 3326 * in case we need to take it back out of p_index,
3501 3327 * and then or's the new index into the existing index.
3502 3328 */
3503 3329 if ((PP_MAPINDEX(pp) & index) == 0)
3504 3330 newidx = 1;
3505 3331 pp->p_index = (PP_MAPINDEX(pp) | index);
3506 3332
3507 3333 /*
3508 3334 * contiguity check
3509 3335 */
3510 3336 if (pp->p_pagenum != pfnum) {
3511 3337 /*
3512 3338 * If we fail the contiguity test then
3513 3339 * the only thing we need to fix is the p_index field.
3514 3340 * We might get a few extra flushes but since this
3515 3341 * path is rare that is ok. The p_ro field will
3516 3342 * get automatically fixed on the next tteload to
3517 3343 * the page. NO TNC bit is set yet.
3518 3344 */
3519 3345 while (i >= 0) {
3520 3346 pp = *pps;
3521 3347 if (newidx)
3522 3348 pp->p_index = (PP_MAPINDEX(pp) &
3523 3349 ~index);
3524 3350 pps--;
3525 3351 i--;
3526 3352 }
3527 3353 return (1);
3528 3354 }
3529 3355 pfnum++;
3530 3356 addr += MMU_PAGESIZE;
3531 3357 }
3532 3358
3533 3359 #ifdef VAC
3534 3360 if (vac_err) {
3535 3361 if (ttesz > osz) {
3536 3362 /*
3537 3363 * There are some smaller mappings that causes vac
3538 3364 * conflicts. Convert all existing small mappings to
3539 3365 * TNC.
3540 3366 */
3541 3367 SFMMU_STAT_ADD(sf_uncache_conflict, npgs);
3542 3368 sfmmu_page_cache_array(pp1, HAT_TMPNC, CACHE_FLUSH,
3543 3369 npgs);
3544 3370 } else {
3545 3371 /* EMPTY */
3546 3372 /*
3547 3373 * If there exists an big page mapping,
3548 3374 * that means the whole existing big page
3549 3375 * has TNC setting already. No need to covert to
3550 3376 * TNC again.
3551 3377 */
3552 3378 ASSERT(PP_ISTNC(pp1));
3553 3379 }
3554 3380 }
3555 3381 #endif /* VAC */
3556 3382
3557 3383 return (0);
3558 3384 }
3559 3385
3560 3386 #ifdef VAC
3561 3387 /*
3562 3388 * Routine that detects vac consistency for a large page. It also
3563 3389 * sets virtual color for all pp's for this big mapping.
3564 3390 */
3565 3391 static int
3566 3392 sfmmu_vacconflict_array(caddr_t addr, page_t *pp, int *cflags)
3567 3393 {
3568 3394 int vcolor, ocolor;
3569 3395
3570 3396 ASSERT(sfmmu_mlist_held(pp));
3571 3397
3572 3398 if (PP_ISNC(pp)) {
3573 3399 return (HAT_TMPNC);
3574 3400 }
3575 3401
3576 3402 vcolor = addr_to_vcolor(addr);
3577 3403 if (PP_NEWPAGE(pp)) {
3578 3404 PP_SET_VCOLOR(pp, vcolor);
3579 3405 return (0);
3580 3406 }
3581 3407
3582 3408 ocolor = PP_GET_VCOLOR(pp);
3583 3409 if (ocolor == vcolor) {
3584 3410 return (0);
3585 3411 }
3586 3412
3587 3413 if (!PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp)) {
3588 3414 /*
3589 3415 * Previous user of page had a differnet color
3590 3416 * but since there are no current users
3591 3417 * we just flush the cache and change the color.
3592 3418 * As an optimization for large pages we flush the
3593 3419 * entire cache of that color and set a flag.
3594 3420 */
3595 3421 SFMMU_STAT(sf_pgcolor_conflict);
3596 3422 if (!CacheColor_IsFlushed(*cflags, ocolor)) {
3597 3423 CacheColor_SetFlushed(*cflags, ocolor);
3598 3424 sfmmu_cache_flushcolor(ocolor, pp->p_pagenum);
3599 3425 }
3600 3426 PP_SET_VCOLOR(pp, vcolor);
3601 3427 return (0);
3602 3428 }
3603 3429
3604 3430 /*
3605 3431 * We got a real conflict with a current mapping.
3606 3432 * set flags to start unencaching all mappings
3607 3433 * and return failure so we restart looping
3608 3434 * the pp array from the beginning.
3609 3435 */
3610 3436 return (HAT_TMPNC);
3611 3437 }
3612 3438 #endif /* VAC */
3613 3439
3614 3440 /*
3615 3441 * creates a large page shadow hmeblk for a tte.
3616 3442 * The purpose of this routine is to allow us to do quick unloads because
3617 3443 * the vm layer can easily pass a very large but sparsely populated range.
3618 3444 */
3619 3445 static struct hme_blk *
3620 3446 sfmmu_shadow_hcreate(sfmmu_t *sfmmup, caddr_t vaddr, int ttesz, uint_t flags)
3621 3447 {
3622 3448 struct hmehash_bucket *hmebp;
3623 3449 hmeblk_tag hblktag;
3624 3450 int hmeshift, size, vshift;
3625 3451 uint_t shw_mask, newshw_mask;
3626 3452 struct hme_blk *hmeblkp;
3627 3453
3628 3454 ASSERT(sfmmup != KHATID);
3629 3455 if (mmu_page_sizes == max_mmu_page_sizes) {
3630 3456 ASSERT(ttesz < TTE256M);
3631 3457 } else {
3632 3458 ASSERT(ttesz < TTE4M);
3633 3459 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0);
3634 3460 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0);
3635 3461 }
3636 3462
3637 3463 if (ttesz == TTE8K) {
3638 3464 size = TTE512K;
3639 3465 } else {
3640 3466 size = ++ttesz;
3641 3467 }
3642 3468
3643 3469 hblktag.htag_id = sfmmup;
3644 3470 hmeshift = HME_HASH_SHIFT(size);
3645 3471 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift);
3646 3472 hblktag.htag_rehash = HME_HASH_REHASH(size);
3647 3473 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
3648 3474 hmebp = HME_HASH_FUNCTION(sfmmup, vaddr, hmeshift);
3649 3475
3650 3476 SFMMU_HASH_LOCK(hmebp);
3651 3477
3652 3478 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp);
3653 3479 ASSERT(hmeblkp != (struct hme_blk *)hblk_reserve);
3654 3480 if (hmeblkp == NULL) {
3655 3481 hmeblkp = sfmmu_hblk_alloc(sfmmup, vaddr, hmebp, size,
3656 3482 hblktag, flags, SFMMU_INVALID_SHMERID);
3657 3483 }
3658 3484 ASSERT(hmeblkp);
3659 3485 if (!hmeblkp->hblk_shw_mask) {
3660 3486 /*
3661 3487 * if this is a unused hblk it was just allocated or could
3662 3488 * potentially be a previous large page hblk so we need to
3663 3489 * set the shadow bit.
3664 3490 */
3665 3491 ASSERT(!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt);
3666 3492 hmeblkp->hblk_shw_bit = 1;
3667 3493 } else if (hmeblkp->hblk_shw_bit == 0) {
3668 3494 panic("sfmmu_shadow_hcreate: shw bit not set in hmeblkp 0x%p",
3669 3495 (void *)hmeblkp);
3670 3496 }
3671 3497 ASSERT(hmeblkp->hblk_shw_bit == 1);
3672 3498 ASSERT(!hmeblkp->hblk_shared);
3673 3499 vshift = vaddr_to_vshift(hblktag, vaddr, size);
3674 3500 ASSERT(vshift < 8);
3675 3501 /*
3676 3502 * Atomically set shw mask bit
3677 3503 */
3678 3504 do {
3679 3505 shw_mask = hmeblkp->hblk_shw_mask;
3680 3506 newshw_mask = shw_mask | (1 << vshift);
3681 3507 newshw_mask = atomic_cas_32(&hmeblkp->hblk_shw_mask, shw_mask,
3682 3508 newshw_mask);
3683 3509 } while (newshw_mask != shw_mask);
3684 3510
3685 3511 SFMMU_HASH_UNLOCK(hmebp);
3686 3512
3687 3513 return (hmeblkp);
3688 3514 }
3689 3515
3690 3516 /*
3691 3517 * This routine cleanup a previous shadow hmeblk and changes it to
3692 3518 * a regular hblk. This happens rarely but it is possible
3693 3519 * when a process wants to use large pages and there are hblks still
3694 3520 * lying around from the previous as that used these hmeblks.
3695 3521 * The alternative was to cleanup the shadow hblks at unload time
3696 3522 * but since so few user processes actually use large pages, it is
3697 3523 * better to be lazy and cleanup at this time.
3698 3524 */
3699 3525 static void
3700 3526 sfmmu_shadow_hcleanup(sfmmu_t *sfmmup, struct hme_blk *hmeblkp,
3701 3527 struct hmehash_bucket *hmebp)
3702 3528 {
3703 3529 caddr_t addr, endaddr;
3704 3530 int hashno, size;
3705 3531
3706 3532 ASSERT(hmeblkp->hblk_shw_bit);
3707 3533 ASSERT(!hmeblkp->hblk_shared);
3708 3534
3709 3535 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
3710 3536
3711 3537 if (!hmeblkp->hblk_shw_mask) {
3712 3538 hmeblkp->hblk_shw_bit = 0;
3713 3539 return;
3714 3540 }
3715 3541 addr = (caddr_t)get_hblk_base(hmeblkp);
3716 3542 endaddr = get_hblk_endaddr(hmeblkp);
3717 3543 size = get_hblk_ttesz(hmeblkp);
3718 3544 hashno = size - 1;
3719 3545 ASSERT(hashno > 0);
3720 3546 SFMMU_HASH_UNLOCK(hmebp);
3721 3547
3722 3548 sfmmu_free_hblks(sfmmup, addr, endaddr, hashno);
3723 3549
3724 3550 SFMMU_HASH_LOCK(hmebp);
3725 3551 }
3726 3552
3727 3553 static void
3728 3554 sfmmu_free_hblks(sfmmu_t *sfmmup, caddr_t addr, caddr_t endaddr,
3729 3555 int hashno)
3730 3556 {
3731 3557 int hmeshift, shadow = 0;
3732 3558 hmeblk_tag hblktag;
3733 3559 struct hmehash_bucket *hmebp;
3734 3560 struct hme_blk *hmeblkp;
3735 3561 struct hme_blk *nx_hblk, *pr_hblk, *list = NULL;
3736 3562
3737 3563 ASSERT(hashno > 0);
3738 3564 hblktag.htag_id = sfmmup;
3739 3565 hblktag.htag_rehash = hashno;
3740 3566 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
3741 3567
3742 3568 hmeshift = HME_HASH_SHIFT(hashno);
3743 3569
3744 3570 while (addr < endaddr) {
3745 3571 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
3746 3572 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
3747 3573 SFMMU_HASH_LOCK(hmebp);
3748 3574 /* inline HME_HASH_SEARCH */
3749 3575 hmeblkp = hmebp->hmeblkp;
3750 3576 pr_hblk = NULL;
3751 3577 while (hmeblkp) {
3752 3578 if (HTAGS_EQ(hmeblkp->hblk_tag, hblktag)) {
3753 3579 /* found hme_blk */
3754 3580 ASSERT(!hmeblkp->hblk_shared);
3755 3581 if (hmeblkp->hblk_shw_bit) {
3756 3582 if (hmeblkp->hblk_shw_mask) {
3757 3583 shadow = 1;
3758 3584 sfmmu_shadow_hcleanup(sfmmup,
3759 3585 hmeblkp, hmebp);
3760 3586 break;
3761 3587 } else {
3762 3588 hmeblkp->hblk_shw_bit = 0;
3763 3589 }
3764 3590 }
3765 3591
3766 3592 /*
3767 3593 * Hblk_hmecnt and hblk_vcnt could be non zero
3768 3594 * since hblk_unload() does not gurantee that.
3769 3595 *
3770 3596 * XXX - this could cause tteload() to spin
3771 3597 * where sfmmu_shadow_hcleanup() is called.
3772 3598 */
3773 3599 }
3774 3600
3775 3601 nx_hblk = hmeblkp->hblk_next;
3776 3602 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) {
3777 3603 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
3778 3604 &list, 0);
3779 3605 } else {
3780 3606 pr_hblk = hmeblkp;
3781 3607 }
3782 3608 hmeblkp = nx_hblk;
3783 3609 }
3784 3610
3785 3611 SFMMU_HASH_UNLOCK(hmebp);
3786 3612
3787 3613 if (shadow) {
3788 3614 /*
3789 3615 * We found another shadow hblk so cleaned its
3790 3616 * children. We need to go back and cleanup
3791 3617 * the original hblk so we don't change the
3792 3618 * addr.
3793 3619 */
3794 3620 shadow = 0;
3795 3621 } else {
3796 3622 addr = (caddr_t)roundup((uintptr_t)addr + 1,
3797 3623 (1 << hmeshift));
3798 3624 }
3799 3625 }
3800 3626 sfmmu_hblks_list_purge(&list, 0);
3801 3627 }
3802 3628
3803 3629 /*
3804 3630 * This routine's job is to delete stale invalid shared hmeregions hmeblks that
3805 3631 * may still linger on after pageunload.
3806 3632 */
3807 3633 static void
3808 3634 sfmmu_cleanup_rhblk(sf_srd_t *srdp, caddr_t addr, uint_t rid, int ttesz)
3809 3635 {
3810 3636 int hmeshift;
3811 3637 hmeblk_tag hblktag;
3812 3638 struct hmehash_bucket *hmebp;
3813 3639 struct hme_blk *hmeblkp;
3814 3640 struct hme_blk *pr_hblk;
3815 3641 struct hme_blk *list = NULL;
3816 3642
3817 3643 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
3818 3644 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
3819 3645
3820 3646 hmeshift = HME_HASH_SHIFT(ttesz);
3821 3647 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
3822 3648 hblktag.htag_rehash = ttesz;
3823 3649 hblktag.htag_rid = rid;
3824 3650 hblktag.htag_id = srdp;
3825 3651 hmebp = HME_HASH_FUNCTION(srdp, addr, hmeshift);
3826 3652
3827 3653 SFMMU_HASH_LOCK(hmebp);
3828 3654 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, pr_hblk, &list);
3829 3655 if (hmeblkp != NULL) {
3830 3656 ASSERT(hmeblkp->hblk_shared);
3831 3657 ASSERT(!hmeblkp->hblk_shw_bit);
3832 3658 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) {
3833 3659 panic("sfmmu_cleanup_rhblk: valid hmeblk");
3834 3660 }
3835 3661 ASSERT(!hmeblkp->hblk_lckcnt);
3836 3662 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
3837 3663 &list, 0);
3838 3664 }
3839 3665 SFMMU_HASH_UNLOCK(hmebp);
3840 3666 sfmmu_hblks_list_purge(&list, 0);
3841 3667 }
3842 3668
3843 3669 /* ARGSUSED */
3844 3670 static void
3845 3671 sfmmu_rgn_cb_noop(caddr_t saddr, caddr_t eaddr, caddr_t r_saddr,
3846 3672 size_t r_size, void *r_obj, u_offset_t r_objoff)
3847 3673 {
3848 3674 }
3849 3675
3850 3676 /*
3851 3677 * Searches for an hmeblk which maps addr, then unloads this mapping
3852 3678 * and updates *eaddrp, if the hmeblk is found.
3853 3679 */
3854 3680 static void
3855 3681 sfmmu_unload_hmeregion_va(sf_srd_t *srdp, uint_t rid, caddr_t addr,
3856 3682 caddr_t eaddr, int ttesz, caddr_t *eaddrp)
3857 3683 {
3858 3684 int hmeshift;
3859 3685 hmeblk_tag hblktag;
3860 3686 struct hmehash_bucket *hmebp;
3861 3687 struct hme_blk *hmeblkp;
3862 3688 struct hme_blk *pr_hblk;
3863 3689 struct hme_blk *list = NULL;
3864 3690
3865 3691 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
3866 3692 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
3867 3693 ASSERT(ttesz >= HBLK_MIN_TTESZ);
3868 3694
3869 3695 hmeshift = HME_HASH_SHIFT(ttesz);
3870 3696 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
3871 3697 hblktag.htag_rehash = ttesz;
3872 3698 hblktag.htag_rid = rid;
3873 3699 hblktag.htag_id = srdp;
3874 3700 hmebp = HME_HASH_FUNCTION(srdp, addr, hmeshift);
3875 3701
3876 3702 SFMMU_HASH_LOCK(hmebp);
3877 3703 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, pr_hblk, &list);
3878 3704 if (hmeblkp != NULL) {
3879 3705 ASSERT(hmeblkp->hblk_shared);
3880 3706 ASSERT(!hmeblkp->hblk_lckcnt);
3881 3707 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) {
3882 3708 *eaddrp = sfmmu_hblk_unload(NULL, hmeblkp, addr,
3883 3709 eaddr, NULL, HAT_UNLOAD);
3884 3710 ASSERT(*eaddrp > addr);
3885 3711 }
3886 3712 ASSERT(!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt);
3887 3713 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
3888 3714 &list, 0);
3889 3715 }
3890 3716 SFMMU_HASH_UNLOCK(hmebp);
3891 3717 sfmmu_hblks_list_purge(&list, 0);
3892 3718 }
3893 3719
3894 3720 static void
3895 3721 sfmmu_unload_hmeregion(sf_srd_t *srdp, sf_region_t *rgnp)
3896 3722 {
3897 3723 int ttesz = rgnp->rgn_pgszc;
3898 3724 size_t rsz = rgnp->rgn_size;
3899 3725 caddr_t rsaddr = rgnp->rgn_saddr;
3900 3726 caddr_t readdr = rsaddr + rsz;
3901 3727 caddr_t rhsaddr;
3902 3728 caddr_t va;
3903 3729 uint_t rid = rgnp->rgn_id;
3904 3730 caddr_t cbsaddr;
3905 3731 caddr_t cbeaddr;
3906 3732 hat_rgn_cb_func_t rcbfunc;
3907 3733 ulong_t cnt;
3908 3734
3909 3735 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
3910 3736 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
3911 3737
3912 3738 ASSERT(IS_P2ALIGNED(rsaddr, TTEBYTES(ttesz)));
3913 3739 ASSERT(IS_P2ALIGNED(rsz, TTEBYTES(ttesz)));
3914 3740 if (ttesz < HBLK_MIN_TTESZ) {
3915 3741 ttesz = HBLK_MIN_TTESZ;
3916 3742 rhsaddr = (caddr_t)P2ALIGN((uintptr_t)rsaddr, HBLK_MIN_BYTES);
3917 3743 } else {
3918 3744 rhsaddr = rsaddr;
3919 3745 }
3920 3746
3921 3747 if ((rcbfunc = rgnp->rgn_cb_function) == NULL) {
3922 3748 rcbfunc = sfmmu_rgn_cb_noop;
3923 3749 }
3924 3750
3925 3751 while (ttesz >= HBLK_MIN_TTESZ) {
3926 3752 cbsaddr = rsaddr;
3927 3753 cbeaddr = rsaddr;
3928 3754 if (!(rgnp->rgn_hmeflags & (1 << ttesz))) {
3929 3755 ttesz--;
3930 3756 continue;
3931 3757 }
3932 3758 cnt = 0;
3933 3759 va = rsaddr;
3934 3760 while (va < readdr) {
3935 3761 ASSERT(va >= rhsaddr);
3936 3762 if (va != cbeaddr) {
3937 3763 if (cbeaddr != cbsaddr) {
3938 3764 ASSERT(cbeaddr > cbsaddr);
3939 3765 (*rcbfunc)(cbsaddr, cbeaddr,
3940 3766 rsaddr, rsz, rgnp->rgn_obj,
3941 3767 rgnp->rgn_objoff);
3942 3768 }
3943 3769 cbsaddr = va;
3944 3770 cbeaddr = va;
3945 3771 }
3946 3772 sfmmu_unload_hmeregion_va(srdp, rid, va, readdr,
3947 3773 ttesz, &cbeaddr);
3948 3774 cnt++;
3949 3775 va = rhsaddr + (cnt << TTE_PAGE_SHIFT(ttesz));
3950 3776 }
3951 3777 if (cbeaddr != cbsaddr) {
3952 3778 ASSERT(cbeaddr > cbsaddr);
3953 3779 (*rcbfunc)(cbsaddr, cbeaddr, rsaddr,
3954 3780 rsz, rgnp->rgn_obj,
3955 3781 rgnp->rgn_objoff);
3956 3782 }
3957 3783 ttesz--;
3958 3784 }
3959 3785 }
3960 3786
3961 3787 /*
3962 3788 * Release one hardware address translation lock on the given address range.
3963 3789 */
↓ open down ↓ |
1462 lines elided |
↑ open up ↑ |
3964 3790 void
3965 3791 hat_unlock(struct hat *sfmmup, caddr_t addr, size_t len)
3966 3792 {
3967 3793 struct hmehash_bucket *hmebp;
3968 3794 hmeblk_tag hblktag;
3969 3795 int hmeshift, hashno = 1;
3970 3796 struct hme_blk *hmeblkp, *list = NULL;
3971 3797 caddr_t endaddr;
3972 3798
3973 3799 ASSERT(sfmmup != NULL);
3974 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
3975 3800
3976 3801 ASSERT((sfmmup == ksfmmup) ||
3977 3802 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
3978 3803 ASSERT((len & MMU_PAGEOFFSET) == 0);
3979 3804 endaddr = addr + len;
3980 3805 hblktag.htag_id = sfmmup;
3981 3806 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
3982 3807
3983 3808 /*
3984 3809 * Spitfire supports 4 page sizes.
3985 3810 * Most pages are expected to be of the smallest page size (8K) and
3986 3811 * these will not need to be rehashed. 64K pages also don't need to be
3987 3812 * rehashed because an hmeblk spans 64K of address space. 512K pages
3988 3813 * might need 1 rehash and and 4M pages might need 2 rehashes.
3989 3814 */
3990 3815 while (addr < endaddr) {
3991 3816 hmeshift = HME_HASH_SHIFT(hashno);
3992 3817 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
3993 3818 hblktag.htag_rehash = hashno;
3994 3819 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
3995 3820
3996 3821 SFMMU_HASH_LOCK(hmebp);
3997 3822
3998 3823 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list);
3999 3824 if (hmeblkp != NULL) {
4000 3825 ASSERT(!hmeblkp->hblk_shared);
4001 3826 /*
4002 3827 * If we encounter a shadow hmeblk then
4003 3828 * we know there are no valid hmeblks mapping
4004 3829 * this address at this size or larger.
4005 3830 * Just increment address by the smallest
4006 3831 * page size.
4007 3832 */
4008 3833 if (hmeblkp->hblk_shw_bit) {
4009 3834 addr += MMU_PAGESIZE;
4010 3835 } else {
4011 3836 addr = sfmmu_hblk_unlock(hmeblkp, addr,
4012 3837 endaddr);
4013 3838 }
4014 3839 SFMMU_HASH_UNLOCK(hmebp);
4015 3840 hashno = 1;
4016 3841 continue;
4017 3842 }
4018 3843 SFMMU_HASH_UNLOCK(hmebp);
4019 3844
4020 3845 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) {
4021 3846 /*
4022 3847 * We have traversed the whole list and rehashed
4023 3848 * if necessary without finding the address to unlock
4024 3849 * which should never happen.
4025 3850 */
4026 3851 panic("sfmmu_unlock: addr not found. "
4027 3852 "addr %p hat %p", (void *)addr, (void *)sfmmup);
4028 3853 } else {
4029 3854 hashno++;
4030 3855 }
4031 3856 }
4032 3857
4033 3858 sfmmu_hblks_list_purge(&list, 0);
4034 3859 }
4035 3860
4036 3861 void
4037 3862 hat_unlock_region(struct hat *sfmmup, caddr_t addr, size_t len,
4038 3863 hat_region_cookie_t rcookie)
4039 3864 {
4040 3865 sf_srd_t *srdp;
4041 3866 sf_region_t *rgnp;
4042 3867 int ttesz;
4043 3868 uint_t rid;
4044 3869 caddr_t eaddr;
4045 3870 caddr_t va;
4046 3871 int hmeshift;
4047 3872 hmeblk_tag hblktag;
4048 3873 struct hmehash_bucket *hmebp;
↓ open down ↓ |
64 lines elided |
↑ open up ↑ |
4049 3874 struct hme_blk *hmeblkp;
4050 3875 struct hme_blk *pr_hblk;
4051 3876 struct hme_blk *list;
4052 3877
4053 3878 if (rcookie == HAT_INVALID_REGION_COOKIE) {
4054 3879 hat_unlock(sfmmup, addr, len);
4055 3880 return;
4056 3881 }
4057 3882
4058 3883 ASSERT(sfmmup != NULL);
4059 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
4060 3884 ASSERT(sfmmup != ksfmmup);
4061 3885
4062 3886 srdp = sfmmup->sfmmu_srdp;
4063 3887 rid = (uint_t)((uint64_t)rcookie);
4064 3888 VERIFY3U(rid, <, SFMMU_MAX_HME_REGIONS);
4065 3889 eaddr = addr + len;
4066 3890 va = addr;
4067 3891 list = NULL;
4068 3892 rgnp = srdp->srd_hmergnp[rid];
4069 3893 SFMMU_VALIDATE_HMERID(sfmmup, rid, addr, len);
4070 3894
4071 3895 ASSERT(IS_P2ALIGNED(addr, TTEBYTES(rgnp->rgn_pgszc)));
4072 3896 ASSERT(IS_P2ALIGNED(len, TTEBYTES(rgnp->rgn_pgszc)));
4073 3897 if (rgnp->rgn_pgszc < HBLK_MIN_TTESZ) {
4074 3898 ttesz = HBLK_MIN_TTESZ;
4075 3899 } else {
4076 3900 ttesz = rgnp->rgn_pgszc;
4077 3901 }
4078 3902 while (va < eaddr) {
4079 3903 while (ttesz < rgnp->rgn_pgszc &&
4080 3904 IS_P2ALIGNED(va, TTEBYTES(ttesz + 1))) {
4081 3905 ttesz++;
4082 3906 }
4083 3907 while (ttesz >= HBLK_MIN_TTESZ) {
4084 3908 if (!(rgnp->rgn_hmeflags & (1 << ttesz))) {
4085 3909 ttesz--;
4086 3910 continue;
4087 3911 }
4088 3912 hmeshift = HME_HASH_SHIFT(ttesz);
4089 3913 hblktag.htag_bspage = HME_HASH_BSPAGE(va, hmeshift);
4090 3914 hblktag.htag_rehash = ttesz;
4091 3915 hblktag.htag_rid = rid;
4092 3916 hblktag.htag_id = srdp;
4093 3917 hmebp = HME_HASH_FUNCTION(srdp, va, hmeshift);
4094 3918 SFMMU_HASH_LOCK(hmebp);
4095 3919 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, pr_hblk,
4096 3920 &list);
4097 3921 if (hmeblkp == NULL) {
4098 3922 SFMMU_HASH_UNLOCK(hmebp);
4099 3923 ttesz--;
4100 3924 continue;
4101 3925 }
4102 3926 ASSERT(hmeblkp->hblk_shared);
4103 3927 va = sfmmu_hblk_unlock(hmeblkp, va, eaddr);
4104 3928 ASSERT(va >= eaddr ||
4105 3929 IS_P2ALIGNED((uintptr_t)va, TTEBYTES(ttesz)));
4106 3930 SFMMU_HASH_UNLOCK(hmebp);
4107 3931 break;
4108 3932 }
4109 3933 if (ttesz < HBLK_MIN_TTESZ) {
4110 3934 panic("hat_unlock_region: addr not found "
4111 3935 "addr %p hat %p", (void *)va, (void *)sfmmup);
4112 3936 }
4113 3937 }
4114 3938 sfmmu_hblks_list_purge(&list, 0);
4115 3939 }
4116 3940
4117 3941 /*
4118 3942 * Function to unlock a range of addresses in an hmeblk. It returns the
4119 3943 * next address that needs to be unlocked.
4120 3944 * Should be called with the hash lock held.
4121 3945 */
4122 3946 static caddr_t
4123 3947 sfmmu_hblk_unlock(struct hme_blk *hmeblkp, caddr_t addr, caddr_t endaddr)
4124 3948 {
4125 3949 struct sf_hment *sfhme;
4126 3950 tte_t tteold, ttemod;
4127 3951 int ttesz, ret;
4128 3952
4129 3953 ASSERT(in_hblk_range(hmeblkp, addr));
4130 3954 ASSERT(hmeblkp->hblk_shw_bit == 0);
4131 3955
4132 3956 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp));
4133 3957 ttesz = get_hblk_ttesz(hmeblkp);
4134 3958
4135 3959 HBLKTOHME(sfhme, hmeblkp, addr);
4136 3960 while (addr < endaddr) {
4137 3961 readtte:
4138 3962 sfmmu_copytte(&sfhme->hme_tte, &tteold);
4139 3963 if (TTE_IS_VALID(&tteold)) {
4140 3964
4141 3965 ttemod = tteold;
4142 3966
4143 3967 ret = sfmmu_modifytte_try(&tteold, &ttemod,
4144 3968 &sfhme->hme_tte);
4145 3969
4146 3970 if (ret < 0)
4147 3971 goto readtte;
4148 3972
4149 3973 if (hmeblkp->hblk_lckcnt == 0)
4150 3974 panic("zero hblk lckcnt");
4151 3975
4152 3976 if (((uintptr_t)addr + TTEBYTES(ttesz)) >
4153 3977 (uintptr_t)endaddr)
4154 3978 panic("can't unlock large tte");
4155 3979
4156 3980 ASSERT(hmeblkp->hblk_lckcnt > 0);
4157 3981 atomic_dec_32(&hmeblkp->hblk_lckcnt);
4158 3982 HBLK_STACK_TRACE(hmeblkp, HBLK_UNLOCK);
4159 3983 } else {
4160 3984 panic("sfmmu_hblk_unlock: invalid tte");
4161 3985 }
4162 3986 addr += TTEBYTES(ttesz);
4163 3987 sfhme++;
4164 3988 }
4165 3989 return (addr);
4166 3990 }
4167 3991
4168 3992 /*
4169 3993 * Physical Address Mapping Framework
4170 3994 *
4171 3995 * General rules:
4172 3996 *
4173 3997 * (1) Applies only to seg_kmem memory pages. To make things easier,
4174 3998 * seg_kpm addresses are also accepted by the routines, but nothing
4175 3999 * is done with them since by definition their PA mappings are static.
4176 4000 * (2) hat_add_callback() may only be called while holding the page lock
4177 4001 * SE_SHARED or SE_EXCL of the underlying page (e.g., as_pagelock()),
4178 4002 * or passing HAC_PAGELOCK flag.
4179 4003 * (3) prehandler() and posthandler() may not call hat_add_callback() or
4180 4004 * hat_delete_callback(), nor should they allocate memory. Post quiesce
4181 4005 * callbacks may not sleep or acquire adaptive mutex locks.
4182 4006 * (4) Either prehandler() or posthandler() (but not both) may be specified
4183 4007 * as being NULL. Specifying an errhandler() is optional.
4184 4008 *
4185 4009 * Details of using the framework:
4186 4010 *
4187 4011 * registering a callback (hat_register_callback())
4188 4012 *
4189 4013 * Pass prehandler, posthandler, errhandler addresses
4190 4014 * as described below. If capture_cpus argument is nonzero,
4191 4015 * suspend callback to the prehandler will occur with CPUs
4192 4016 * captured and executing xc_loop() and CPUs will remain
4193 4017 * captured until after the posthandler suspend callback
4194 4018 * occurs.
4195 4019 *
4196 4020 * adding a callback (hat_add_callback())
4197 4021 *
4198 4022 * as_pagelock();
4199 4023 * hat_add_callback();
4200 4024 * save returned pfn in private data structures or program registers;
4201 4025 * as_pageunlock();
4202 4026 *
4203 4027 * prehandler()
4204 4028 *
4205 4029 * Stop all accesses by physical address to this memory page.
4206 4030 * Called twice: the first, PRESUSPEND, is a context safe to acquire
4207 4031 * adaptive locks. The second, SUSPEND, is called at high PIL with
4208 4032 * CPUs captured so adaptive locks may NOT be acquired (and all spin
4209 4033 * locks must be XCALL_PIL or higher locks).
4210 4034 *
4211 4035 * May return the following errors:
4212 4036 * EIO: A fatal error has occurred. This will result in panic.
4213 4037 * EAGAIN: The page cannot be suspended. This will fail the
4214 4038 * relocation.
4215 4039 * 0: Success.
4216 4040 *
4217 4041 * posthandler()
4218 4042 *
4219 4043 * Save new pfn in private data structures or program registers;
4220 4044 * not allowed to fail (non-zero return values will result in panic).
4221 4045 *
4222 4046 * errhandler()
4223 4047 *
4224 4048 * called when an error occurs related to the callback. Currently
4225 4049 * the only such error is HAT_CB_ERR_LEAKED which indicates that
4226 4050 * a page is being freed, but there are still outstanding callback(s)
4227 4051 * registered on the page.
4228 4052 *
4229 4053 * removing a callback (hat_delete_callback(); e.g., prior to freeing memory)
4230 4054 *
4231 4055 * stop using physical address
4232 4056 * hat_delete_callback();
4233 4057 *
4234 4058 */
4235 4059
4236 4060 /*
4237 4061 * Register a callback class. Each subsystem should do this once and
4238 4062 * cache the id_t returned for use in setting up and tearing down callbacks.
4239 4063 *
4240 4064 * There is no facility for removing callback IDs once they are created;
4241 4065 * the "key" should be unique for each module, so in case a module is unloaded
4242 4066 * and subsequently re-loaded, we can recycle the module's previous entry.
4243 4067 */
4244 4068 id_t
4245 4069 hat_register_callback(int key,
4246 4070 int (*prehandler)(caddr_t, uint_t, uint_t, void *),
4247 4071 int (*posthandler)(caddr_t, uint_t, uint_t, void *, pfn_t),
4248 4072 int (*errhandler)(caddr_t, uint_t, uint_t, void *),
4249 4073 int capture_cpus)
4250 4074 {
4251 4075 id_t id;
4252 4076
4253 4077 /*
4254 4078 * Search the table for a pre-existing callback associated with
4255 4079 * the identifier "key". If one exists, we re-use that entry in
4256 4080 * the table for this instance, otherwise we assign the next
4257 4081 * available table slot.
4258 4082 */
4259 4083 for (id = 0; id < sfmmu_max_cb_id; id++) {
4260 4084 if (sfmmu_cb_table[id].key == key)
4261 4085 break;
4262 4086 }
4263 4087
4264 4088 if (id == sfmmu_max_cb_id) {
4265 4089 id = sfmmu_cb_nextid++;
4266 4090 if (id >= sfmmu_max_cb_id)
4267 4091 panic("hat_register_callback: out of callback IDs");
4268 4092 }
4269 4093
4270 4094 ASSERT(prehandler != NULL || posthandler != NULL);
4271 4095
4272 4096 sfmmu_cb_table[id].key = key;
4273 4097 sfmmu_cb_table[id].prehandler = prehandler;
4274 4098 sfmmu_cb_table[id].posthandler = posthandler;
4275 4099 sfmmu_cb_table[id].errhandler = errhandler;
4276 4100 sfmmu_cb_table[id].capture_cpus = capture_cpus;
4277 4101
4278 4102 return (id);
4279 4103 }
4280 4104
4281 4105 #define HAC_COOKIE_NONE (void *)-1
4282 4106
4283 4107 /*
4284 4108 * Add relocation callbacks to the specified addr/len which will be called
4285 4109 * when relocating the associated page. See the description of pre and
4286 4110 * posthandler above for more details.
4287 4111 *
4288 4112 * If HAC_PAGELOCK is included in flags, the underlying memory page is
4289 4113 * locked internally so the caller must be able to deal with the callback
4290 4114 * running even before this function has returned. If HAC_PAGELOCK is not
4291 4115 * set, it is assumed that the underlying memory pages are locked.
4292 4116 *
4293 4117 * Since the caller must track the individual page boundaries anyway,
4294 4118 * we only allow a callback to be added to a single page (large
4295 4119 * or small). Thus [addr, addr + len) MUST be contained within a single
4296 4120 * page.
4297 4121 *
4298 4122 * Registering multiple callbacks on the same [addr, addr+len) is supported,
4299 4123 * _provided_that_ a unique parameter is specified for each callback.
4300 4124 * If multiple callbacks are registered on the same range the callback will
4301 4125 * be invoked with each unique parameter. Registering the same callback with
4302 4126 * the same argument more than once will result in corrupted kernel state.
4303 4127 *
4304 4128 * Returns the pfn of the underlying kernel page in *rpfn
4305 4129 * on success, or PFN_INVALID on failure.
4306 4130 *
4307 4131 * cookiep (if passed) provides storage space for an opaque cookie
4308 4132 * to return later to hat_delete_callback(). This cookie makes the callback
4309 4133 * deletion significantly quicker by avoiding a potentially lengthy hash
4310 4134 * search.
4311 4135 *
4312 4136 * Returns values:
4313 4137 * 0: success
4314 4138 * ENOMEM: memory allocation failure (e.g. flags was passed as HAC_NOSLEEP)
4315 4139 * EINVAL: callback ID is not valid
4316 4140 * ENXIO: ["vaddr", "vaddr" + len) is not mapped in the kernel's address
4317 4141 * space
4318 4142 * ERANGE: ["vaddr", "vaddr" + len) crosses a page boundary
4319 4143 */
4320 4144 int
4321 4145 hat_add_callback(id_t callback_id, caddr_t vaddr, uint_t len, uint_t flags,
4322 4146 void *pvt, pfn_t *rpfn, void **cookiep)
4323 4147 {
4324 4148 struct hmehash_bucket *hmebp;
4325 4149 hmeblk_tag hblktag;
4326 4150 struct hme_blk *hmeblkp;
4327 4151 int hmeshift, hashno;
4328 4152 caddr_t saddr, eaddr, baseaddr;
4329 4153 struct pa_hment *pahmep;
4330 4154 struct sf_hment *sfhmep, *osfhmep;
4331 4155 kmutex_t *pml;
4332 4156 tte_t tte;
4333 4157 page_t *pp;
4334 4158 vnode_t *vp;
4335 4159 u_offset_t off;
4336 4160 pfn_t pfn;
4337 4161 int kmflags = (flags & HAC_SLEEP)? KM_SLEEP : KM_NOSLEEP;
4338 4162 int locked = 0;
4339 4163
4340 4164 /*
4341 4165 * For KPM mappings, just return the physical address since we
4342 4166 * don't need to register any callbacks.
4343 4167 */
4344 4168 if (IS_KPM_ADDR(vaddr)) {
4345 4169 uint64_t paddr;
4346 4170 SFMMU_KPM_VTOP(vaddr, paddr);
4347 4171 *rpfn = btop(paddr);
4348 4172 if (cookiep != NULL)
4349 4173 *cookiep = HAC_COOKIE_NONE;
4350 4174 return (0);
4351 4175 }
4352 4176
4353 4177 if (callback_id < (id_t)0 || callback_id >= sfmmu_cb_nextid) {
4354 4178 *rpfn = PFN_INVALID;
4355 4179 return (EINVAL);
4356 4180 }
4357 4181
4358 4182 if ((pahmep = kmem_cache_alloc(pa_hment_cache, kmflags)) == NULL) {
4359 4183 *rpfn = PFN_INVALID;
4360 4184 return (ENOMEM);
4361 4185 }
4362 4186
4363 4187 sfhmep = &pahmep->sfment;
4364 4188
4365 4189 saddr = (caddr_t)((uintptr_t)vaddr & MMU_PAGEMASK);
4366 4190 eaddr = saddr + len;
4367 4191
4368 4192 rehash:
4369 4193 /* Find the mapping(s) for this page */
4370 4194 for (hashno = TTE64K, hmeblkp = NULL;
4371 4195 hmeblkp == NULL && hashno <= mmu_hashcnt;
4372 4196 hashno++) {
4373 4197 hmeshift = HME_HASH_SHIFT(hashno);
4374 4198 hblktag.htag_id = ksfmmup;
4375 4199 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
4376 4200 hblktag.htag_bspage = HME_HASH_BSPAGE(saddr, hmeshift);
4377 4201 hblktag.htag_rehash = hashno;
4378 4202 hmebp = HME_HASH_FUNCTION(ksfmmup, saddr, hmeshift);
4379 4203
4380 4204 SFMMU_HASH_LOCK(hmebp);
4381 4205
4382 4206 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp);
4383 4207
4384 4208 if (hmeblkp == NULL)
4385 4209 SFMMU_HASH_UNLOCK(hmebp);
4386 4210 }
4387 4211
4388 4212 if (hmeblkp == NULL) {
4389 4213 kmem_cache_free(pa_hment_cache, pahmep);
4390 4214 *rpfn = PFN_INVALID;
4391 4215 return (ENXIO);
4392 4216 }
4393 4217
4394 4218 ASSERT(!hmeblkp->hblk_shared);
4395 4219
4396 4220 HBLKTOHME(osfhmep, hmeblkp, saddr);
4397 4221 sfmmu_copytte(&osfhmep->hme_tte, &tte);
4398 4222
4399 4223 if (!TTE_IS_VALID(&tte)) {
4400 4224 SFMMU_HASH_UNLOCK(hmebp);
4401 4225 kmem_cache_free(pa_hment_cache, pahmep);
4402 4226 *rpfn = PFN_INVALID;
4403 4227 return (ENXIO);
4404 4228 }
4405 4229
4406 4230 /*
4407 4231 * Make sure the boundaries for the callback fall within this
4408 4232 * single mapping.
4409 4233 */
4410 4234 baseaddr = (caddr_t)get_hblk_base(hmeblkp);
4411 4235 ASSERT(saddr >= baseaddr);
4412 4236 if (eaddr > saddr + TTEBYTES(TTE_CSZ(&tte))) {
4413 4237 SFMMU_HASH_UNLOCK(hmebp);
4414 4238 kmem_cache_free(pa_hment_cache, pahmep);
4415 4239 *rpfn = PFN_INVALID;
4416 4240 return (ERANGE);
4417 4241 }
4418 4242
4419 4243 pfn = sfmmu_ttetopfn(&tte, vaddr);
4420 4244
4421 4245 /*
4422 4246 * The pfn may not have a page_t underneath in which case we
4423 4247 * just return it. This can happen if we are doing I/O to a
4424 4248 * static portion of the kernel's address space, for instance.
4425 4249 */
4426 4250 pp = osfhmep->hme_page;
4427 4251 if (pp == NULL) {
4428 4252 SFMMU_HASH_UNLOCK(hmebp);
4429 4253 kmem_cache_free(pa_hment_cache, pahmep);
4430 4254 *rpfn = pfn;
4431 4255 if (cookiep)
4432 4256 *cookiep = HAC_COOKIE_NONE;
4433 4257 return (0);
4434 4258 }
4435 4259 ASSERT(pp == PP_PAGEROOT(pp));
4436 4260
4437 4261 vp = pp->p_vnode;
4438 4262 off = pp->p_offset;
4439 4263
4440 4264 pml = sfmmu_mlist_enter(pp);
4441 4265
4442 4266 if (flags & HAC_PAGELOCK) {
4443 4267 if (!page_trylock(pp, SE_SHARED)) {
4444 4268 /*
4445 4269 * Somebody is holding SE_EXCL lock. Might
4446 4270 * even be hat_page_relocate(). Drop all
4447 4271 * our locks, lookup the page in &kvp, and
4448 4272 * retry. If it doesn't exist in &kvp and &zvp,
4449 4273 * then we must be dealing with a kernel mapped
4450 4274 * page which doesn't actually belong to
4451 4275 * segkmem so we punt.
4452 4276 */
4453 4277 sfmmu_mlist_exit(pml);
4454 4278 SFMMU_HASH_UNLOCK(hmebp);
4455 4279 pp = page_lookup(&kvp, (u_offset_t)saddr, SE_SHARED);
4456 4280
4457 4281 /* check zvp before giving up */
4458 4282 if (pp == NULL)
4459 4283 pp = page_lookup(&zvp, (u_offset_t)saddr,
4460 4284 SE_SHARED);
4461 4285
4462 4286 /* Okay, we didn't find it, give up */
4463 4287 if (pp == NULL) {
4464 4288 kmem_cache_free(pa_hment_cache, pahmep);
4465 4289 *rpfn = pfn;
4466 4290 if (cookiep)
4467 4291 *cookiep = HAC_COOKIE_NONE;
4468 4292 return (0);
4469 4293 }
4470 4294 page_unlock(pp);
4471 4295 goto rehash;
4472 4296 }
4473 4297 locked = 1;
4474 4298 }
4475 4299
4476 4300 if (!PAGE_LOCKED(pp) && !panicstr)
4477 4301 panic("hat_add_callback: page 0x%p not locked", (void *)pp);
4478 4302
4479 4303 if (osfhmep->hme_page != pp || pp->p_vnode != vp ||
4480 4304 pp->p_offset != off) {
4481 4305 /*
4482 4306 * The page moved before we got our hands on it. Drop
4483 4307 * all the locks and try again.
4484 4308 */
4485 4309 ASSERT((flags & HAC_PAGELOCK) != 0);
4486 4310 sfmmu_mlist_exit(pml);
4487 4311 SFMMU_HASH_UNLOCK(hmebp);
4488 4312 page_unlock(pp);
4489 4313 locked = 0;
4490 4314 goto rehash;
4491 4315 }
4492 4316
4493 4317 if (!VN_ISKAS(vp)) {
4494 4318 /*
4495 4319 * This is not a segkmem page but another page which
4496 4320 * has been kernel mapped. It had better have at least
4497 4321 * a share lock on it. Return the pfn.
4498 4322 */
4499 4323 sfmmu_mlist_exit(pml);
4500 4324 SFMMU_HASH_UNLOCK(hmebp);
4501 4325 if (locked)
4502 4326 page_unlock(pp);
4503 4327 kmem_cache_free(pa_hment_cache, pahmep);
4504 4328 ASSERT(PAGE_LOCKED(pp));
4505 4329 *rpfn = pfn;
4506 4330 if (cookiep)
4507 4331 *cookiep = HAC_COOKIE_NONE;
4508 4332 return (0);
4509 4333 }
4510 4334
4511 4335 /*
4512 4336 * Setup this pa_hment and link its embedded dummy sf_hment into
4513 4337 * the mapping list.
4514 4338 */
4515 4339 pp->p_share++;
4516 4340 pahmep->cb_id = callback_id;
4517 4341 pahmep->addr = vaddr;
4518 4342 pahmep->len = len;
4519 4343 pahmep->refcnt = 1;
4520 4344 pahmep->flags = 0;
4521 4345 pahmep->pvt = pvt;
4522 4346
4523 4347 sfhmep->hme_tte.ll = 0;
4524 4348 sfhmep->hme_data = pahmep;
4525 4349 sfhmep->hme_prev = osfhmep;
4526 4350 sfhmep->hme_next = osfhmep->hme_next;
4527 4351
4528 4352 if (osfhmep->hme_next)
4529 4353 osfhmep->hme_next->hme_prev = sfhmep;
4530 4354
4531 4355 osfhmep->hme_next = sfhmep;
4532 4356
4533 4357 sfmmu_mlist_exit(pml);
4534 4358 SFMMU_HASH_UNLOCK(hmebp);
4535 4359
4536 4360 if (locked)
4537 4361 page_unlock(pp);
4538 4362
4539 4363 *rpfn = pfn;
4540 4364 if (cookiep)
4541 4365 *cookiep = (void *)pahmep;
4542 4366
4543 4367 return (0);
4544 4368 }
4545 4369
4546 4370 /*
4547 4371 * Remove the relocation callbacks from the specified addr/len.
4548 4372 */
4549 4373 void
4550 4374 hat_delete_callback(caddr_t vaddr, uint_t len, void *pvt, uint_t flags,
4551 4375 void *cookie)
4552 4376 {
4553 4377 struct hmehash_bucket *hmebp;
4554 4378 hmeblk_tag hblktag;
4555 4379 struct hme_blk *hmeblkp;
4556 4380 int hmeshift, hashno;
4557 4381 caddr_t saddr;
4558 4382 struct pa_hment *pahmep;
4559 4383 struct sf_hment *sfhmep, *osfhmep;
4560 4384 kmutex_t *pml;
4561 4385 tte_t tte;
4562 4386 page_t *pp;
4563 4387 vnode_t *vp;
4564 4388 u_offset_t off;
4565 4389 int locked = 0;
4566 4390
4567 4391 /*
4568 4392 * If the cookie is HAC_COOKIE_NONE then there is no pa_hment to
4569 4393 * remove so just return.
4570 4394 */
4571 4395 if (cookie == HAC_COOKIE_NONE || IS_KPM_ADDR(vaddr))
4572 4396 return;
4573 4397
4574 4398 saddr = (caddr_t)((uintptr_t)vaddr & MMU_PAGEMASK);
4575 4399
4576 4400 rehash:
4577 4401 /* Find the mapping(s) for this page */
4578 4402 for (hashno = TTE64K, hmeblkp = NULL;
4579 4403 hmeblkp == NULL && hashno <= mmu_hashcnt;
4580 4404 hashno++) {
4581 4405 hmeshift = HME_HASH_SHIFT(hashno);
4582 4406 hblktag.htag_id = ksfmmup;
4583 4407 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
4584 4408 hblktag.htag_bspage = HME_HASH_BSPAGE(saddr, hmeshift);
4585 4409 hblktag.htag_rehash = hashno;
4586 4410 hmebp = HME_HASH_FUNCTION(ksfmmup, saddr, hmeshift);
4587 4411
4588 4412 SFMMU_HASH_LOCK(hmebp);
4589 4413
4590 4414 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp);
4591 4415
4592 4416 if (hmeblkp == NULL)
4593 4417 SFMMU_HASH_UNLOCK(hmebp);
4594 4418 }
4595 4419
4596 4420 if (hmeblkp == NULL)
4597 4421 return;
4598 4422
4599 4423 ASSERT(!hmeblkp->hblk_shared);
4600 4424
4601 4425 HBLKTOHME(osfhmep, hmeblkp, saddr);
4602 4426
4603 4427 sfmmu_copytte(&osfhmep->hme_tte, &tte);
4604 4428 if (!TTE_IS_VALID(&tte)) {
4605 4429 SFMMU_HASH_UNLOCK(hmebp);
4606 4430 return;
4607 4431 }
4608 4432
4609 4433 pp = osfhmep->hme_page;
4610 4434 if (pp == NULL) {
4611 4435 SFMMU_HASH_UNLOCK(hmebp);
4612 4436 ASSERT(cookie == NULL);
4613 4437 return;
4614 4438 }
4615 4439
4616 4440 vp = pp->p_vnode;
4617 4441 off = pp->p_offset;
4618 4442
4619 4443 pml = sfmmu_mlist_enter(pp);
4620 4444
4621 4445 if (flags & HAC_PAGELOCK) {
4622 4446 if (!page_trylock(pp, SE_SHARED)) {
4623 4447 /*
4624 4448 * Somebody is holding SE_EXCL lock. Might
4625 4449 * even be hat_page_relocate(). Drop all
4626 4450 * our locks, lookup the page in &kvp, and
4627 4451 * retry. If it doesn't exist in &kvp and &zvp,
4628 4452 * then we must be dealing with a kernel mapped
4629 4453 * page which doesn't actually belong to
4630 4454 * segkmem so we punt.
4631 4455 */
4632 4456 sfmmu_mlist_exit(pml);
4633 4457 SFMMU_HASH_UNLOCK(hmebp);
4634 4458 pp = page_lookup(&kvp, (u_offset_t)saddr, SE_SHARED);
4635 4459 /* check zvp before giving up */
4636 4460 if (pp == NULL)
4637 4461 pp = page_lookup(&zvp, (u_offset_t)saddr,
4638 4462 SE_SHARED);
4639 4463
4640 4464 if (pp == NULL) {
4641 4465 ASSERT(cookie == NULL);
4642 4466 return;
4643 4467 }
4644 4468 page_unlock(pp);
4645 4469 goto rehash;
4646 4470 }
4647 4471 locked = 1;
4648 4472 }
4649 4473
4650 4474 ASSERT(PAGE_LOCKED(pp));
4651 4475
4652 4476 if (osfhmep->hme_page != pp || pp->p_vnode != vp ||
4653 4477 pp->p_offset != off) {
4654 4478 /*
4655 4479 * The page moved before we got our hands on it. Drop
4656 4480 * all the locks and try again.
4657 4481 */
4658 4482 ASSERT((flags & HAC_PAGELOCK) != 0);
4659 4483 sfmmu_mlist_exit(pml);
4660 4484 SFMMU_HASH_UNLOCK(hmebp);
4661 4485 page_unlock(pp);
4662 4486 locked = 0;
4663 4487 goto rehash;
4664 4488 }
4665 4489
4666 4490 if (!VN_ISKAS(vp)) {
4667 4491 /*
4668 4492 * This is not a segkmem page but another page which
4669 4493 * has been kernel mapped.
4670 4494 */
4671 4495 sfmmu_mlist_exit(pml);
4672 4496 SFMMU_HASH_UNLOCK(hmebp);
4673 4497 if (locked)
4674 4498 page_unlock(pp);
4675 4499 ASSERT(cookie == NULL);
4676 4500 return;
4677 4501 }
4678 4502
4679 4503 if (cookie != NULL) {
4680 4504 pahmep = (struct pa_hment *)cookie;
4681 4505 sfhmep = &pahmep->sfment;
4682 4506 } else {
4683 4507 for (sfhmep = pp->p_mapping; sfhmep != NULL;
4684 4508 sfhmep = sfhmep->hme_next) {
4685 4509
4686 4510 /*
4687 4511 * skip va<->pa mappings
4688 4512 */
4689 4513 if (!IS_PAHME(sfhmep))
4690 4514 continue;
4691 4515
4692 4516 pahmep = sfhmep->hme_data;
4693 4517 ASSERT(pahmep != NULL);
4694 4518
4695 4519 /*
4696 4520 * if pa_hment matches, remove it
4697 4521 */
4698 4522 if ((pahmep->pvt == pvt) &&
4699 4523 (pahmep->addr == vaddr) &&
4700 4524 (pahmep->len == len)) {
4701 4525 break;
4702 4526 }
4703 4527 }
4704 4528 }
4705 4529
4706 4530 if (sfhmep == NULL) {
4707 4531 if (!panicstr) {
4708 4532 panic("hat_delete_callback: pa_hment not found, pp %p",
4709 4533 (void *)pp);
4710 4534 }
4711 4535 return;
4712 4536 }
4713 4537
4714 4538 /*
4715 4539 * Note: at this point a valid kernel mapping must still be
4716 4540 * present on this page.
4717 4541 */
4718 4542 pp->p_share--;
4719 4543 if (pp->p_share <= 0)
4720 4544 panic("hat_delete_callback: zero p_share");
4721 4545
4722 4546 if (--pahmep->refcnt == 0) {
4723 4547 if (pahmep->flags != 0)
4724 4548 panic("hat_delete_callback: pa_hment is busy");
4725 4549
4726 4550 /*
4727 4551 * Remove sfhmep from the mapping list for the page.
4728 4552 */
4729 4553 if (sfhmep->hme_prev) {
4730 4554 sfhmep->hme_prev->hme_next = sfhmep->hme_next;
4731 4555 } else {
4732 4556 pp->p_mapping = sfhmep->hme_next;
4733 4557 }
4734 4558
4735 4559 if (sfhmep->hme_next)
4736 4560 sfhmep->hme_next->hme_prev = sfhmep->hme_prev;
4737 4561
4738 4562 sfmmu_mlist_exit(pml);
4739 4563 SFMMU_HASH_UNLOCK(hmebp);
4740 4564
4741 4565 if (locked)
4742 4566 page_unlock(pp);
4743 4567
4744 4568 kmem_cache_free(pa_hment_cache, pahmep);
4745 4569 return;
4746 4570 }
4747 4571
4748 4572 sfmmu_mlist_exit(pml);
4749 4573 SFMMU_HASH_UNLOCK(hmebp);
4750 4574 if (locked)
4751 4575 page_unlock(pp);
4752 4576 }
4753 4577
4754 4578 /*
4755 4579 * hat_probe returns 1 if the translation for the address 'addr' is
4756 4580 * loaded, zero otherwise.
4757 4581 *
4758 4582 * hat_probe should be used only for advisorary purposes because it may
4759 4583 * occasionally return the wrong value. The implementation must guarantee that
4760 4584 * returning the wrong value is a very rare event. hat_probe is used
↓ open down ↓ |
691 lines elided |
↑ open up ↑ |
4761 4585 * to implement optimizations in the segment drivers.
4762 4586 *
4763 4587 */
4764 4588 int
4765 4589 hat_probe(struct hat *sfmmup, caddr_t addr)
4766 4590 {
4767 4591 pfn_t pfn;
4768 4592 tte_t tte;
4769 4593
4770 4594 ASSERT(sfmmup != NULL);
4771 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
4772 4595
4773 4596 ASSERT((sfmmup == ksfmmup) ||
4774 4597 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
4775 4598
4776 4599 if (sfmmup == ksfmmup) {
4777 4600 while ((pfn = sfmmu_vatopfn(addr, sfmmup, &tte))
4778 4601 == PFN_SUSPENDED) {
4779 4602 sfmmu_vatopfn_suspended(addr, sfmmup, &tte);
4780 4603 }
4781 4604 } else {
4782 4605 pfn = sfmmu_uvatopfn(addr, sfmmup, NULL);
4783 4606 }
4784 4607
4785 4608 if (pfn != PFN_INVALID)
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
4786 4609 return (1);
4787 4610 else
4788 4611 return (0);
4789 4612 }
4790 4613
4791 4614 ssize_t
4792 4615 hat_getpagesize(struct hat *sfmmup, caddr_t addr)
4793 4616 {
4794 4617 tte_t tte;
4795 4618
4796 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
4797 -
4798 4619 if (sfmmup == ksfmmup) {
4799 4620 if (sfmmu_vatopfn(addr, sfmmup, &tte) == PFN_INVALID) {
4800 4621 return (-1);
4801 4622 }
4802 4623 } else {
4803 4624 if (sfmmu_uvatopfn(addr, sfmmup, &tte) == PFN_INVALID) {
4804 4625 return (-1);
4805 4626 }
4806 4627 }
4807 4628
4808 4629 ASSERT(TTE_IS_VALID(&tte));
4809 4630 return (TTEBYTES(TTE_CSZ(&tte)));
4810 4631 }
4811 4632
4812 4633 uint_t
4813 4634 hat_getattr(struct hat *sfmmup, caddr_t addr, uint_t *attr)
4814 4635 {
4815 4636 tte_t tte;
4816 4637
4817 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
4818 -
4819 4638 if (sfmmup == ksfmmup) {
4820 4639 if (sfmmu_vatopfn(addr, sfmmup, &tte) == PFN_INVALID) {
4821 4640 tte.ll = 0;
4822 4641 }
4823 4642 } else {
4824 4643 if (sfmmu_uvatopfn(addr, sfmmup, &tte) == PFN_INVALID) {
4825 4644 tte.ll = 0;
4826 4645 }
4827 4646 }
4828 4647 if (TTE_IS_VALID(&tte)) {
4829 4648 *attr = sfmmu_ptov_attr(&tte);
4830 4649 return (0);
4831 4650 }
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
4832 4651 *attr = 0;
4833 4652 return ((uint_t)0xffffffff);
4834 4653 }
4835 4654
4836 4655 /*
4837 4656 * Enables more attributes on specified address range (ie. logical OR)
4838 4657 */
4839 4658 void
4840 4659 hat_setattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr)
4841 4660 {
4842 - if (hat->sfmmu_xhat_provider) {
4843 - XHAT_SETATTR(hat, addr, len, attr);
4844 - return;
4845 - } else {
4846 - /*
4847 - * This must be a CPU HAT. If the address space has
4848 - * XHATs attached, change attributes for all of them,
4849 - * just in case
4850 - */
4851 - ASSERT(hat->sfmmu_as != NULL);
4852 - if (hat->sfmmu_as->a_xhat != NULL)
4853 - xhat_setattr_all(hat->sfmmu_as, addr, len, attr);
4854 - }
4661 + ASSERT(hat->sfmmu_as != NULL);
4855 4662
4856 4663 sfmmu_chgattr(hat, addr, len, attr, SFMMU_SETATTR);
4857 4664 }
4858 4665
4859 4666 /*
4860 4667 * Assigns attributes to the specified address range. All the attributes
4861 4668 * are specified.
4862 4669 */
4863 4670 void
4864 4671 hat_chgattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr)
4865 4672 {
4866 - if (hat->sfmmu_xhat_provider) {
4867 - XHAT_CHGATTR(hat, addr, len, attr);
4868 - return;
4869 - } else {
4870 - /*
4871 - * This must be a CPU HAT. If the address space has
4872 - * XHATs attached, change attributes for all of them,
4873 - * just in case
4874 - */
4875 - ASSERT(hat->sfmmu_as != NULL);
4876 - if (hat->sfmmu_as->a_xhat != NULL)
4877 - xhat_chgattr_all(hat->sfmmu_as, addr, len, attr);
4878 - }
4673 + ASSERT(hat->sfmmu_as != NULL);
4879 4674
4880 4675 sfmmu_chgattr(hat, addr, len, attr, SFMMU_CHGATTR);
4881 4676 }
4882 4677
4883 4678 /*
4884 4679 * Remove attributes on the specified address range (ie. loginal NAND)
4885 4680 */
4886 4681 void
4887 4682 hat_clrattr(struct hat *hat, caddr_t addr, size_t len, uint_t attr)
4888 4683 {
4889 - if (hat->sfmmu_xhat_provider) {
4890 - XHAT_CLRATTR(hat, addr, len, attr);
4891 - return;
4892 - } else {
4893 - /*
4894 - * This must be a CPU HAT. If the address space has
4895 - * XHATs attached, change attributes for all of them,
4896 - * just in case
4897 - */
4898 - ASSERT(hat->sfmmu_as != NULL);
4899 - if (hat->sfmmu_as->a_xhat != NULL)
4900 - xhat_clrattr_all(hat->sfmmu_as, addr, len, attr);
4901 - }
4684 + ASSERT(hat->sfmmu_as != NULL);
4902 4685
4903 4686 sfmmu_chgattr(hat, addr, len, attr, SFMMU_CLRATTR);
4904 4687 }
4905 4688
4906 4689 /*
4907 4690 * Change attributes on an address range to that specified by attr and mode.
4908 4691 */
4909 4692 static void
4910 4693 sfmmu_chgattr(struct hat *sfmmup, caddr_t addr, size_t len, uint_t attr,
4911 4694 int mode)
4912 4695 {
4913 4696 struct hmehash_bucket *hmebp;
4914 4697 hmeblk_tag hblktag;
4915 4698 int hmeshift, hashno = 1;
4916 4699 struct hme_blk *hmeblkp, *list = NULL;
4917 4700 caddr_t endaddr;
4918 4701 cpuset_t cpuset;
4919 4702 demap_range_t dmr;
4920 4703
4921 4704 CPUSET_ZERO(cpuset);
4922 4705
4923 4706 ASSERT((sfmmup == ksfmmup) ||
4924 4707 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
4925 4708 ASSERT((len & MMU_PAGEOFFSET) == 0);
4926 4709 ASSERT(((uintptr_t)addr & MMU_PAGEOFFSET) == 0);
4927 4710
4928 4711 if ((attr & PROT_USER) && (mode != SFMMU_CLRATTR) &&
4929 4712 ((addr + len) > (caddr_t)USERLIMIT)) {
4930 4713 panic("user addr %p in kernel space",
4931 4714 (void *)addr);
4932 4715 }
4933 4716
4934 4717 endaddr = addr + len;
4935 4718 hblktag.htag_id = sfmmup;
4936 4719 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
4937 4720 DEMAP_RANGE_INIT(sfmmup, &dmr);
4938 4721
4939 4722 while (addr < endaddr) {
4940 4723 hmeshift = HME_HASH_SHIFT(hashno);
4941 4724 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
4942 4725 hblktag.htag_rehash = hashno;
4943 4726 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
4944 4727
4945 4728 SFMMU_HASH_LOCK(hmebp);
4946 4729
4947 4730 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list);
4948 4731 if (hmeblkp != NULL) {
4949 4732 ASSERT(!hmeblkp->hblk_shared);
4950 4733 /*
4951 4734 * We've encountered a shadow hmeblk so skip the range
4952 4735 * of the next smaller mapping size.
4953 4736 */
4954 4737 if (hmeblkp->hblk_shw_bit) {
4955 4738 ASSERT(sfmmup != ksfmmup);
4956 4739 ASSERT(hashno > 1);
4957 4740 addr = (caddr_t)P2END((uintptr_t)addr,
4958 4741 TTEBYTES(hashno - 1));
4959 4742 } else {
4960 4743 addr = sfmmu_hblk_chgattr(sfmmup,
4961 4744 hmeblkp, addr, endaddr, &dmr, attr, mode);
4962 4745 }
4963 4746 SFMMU_HASH_UNLOCK(hmebp);
4964 4747 hashno = 1;
4965 4748 continue;
4966 4749 }
4967 4750 SFMMU_HASH_UNLOCK(hmebp);
4968 4751
4969 4752 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) {
4970 4753 /*
4971 4754 * We have traversed the whole list and rehashed
4972 4755 * if necessary without finding the address to chgattr.
4973 4756 * This is ok, so we increment the address by the
4974 4757 * smallest hmeblk range for kernel mappings or for
4975 4758 * user mappings with no large pages, and the largest
4976 4759 * hmeblk range, to account for shadow hmeblks, for
4977 4760 * user mappings with large pages and continue.
4978 4761 */
4979 4762 if (sfmmup == ksfmmup)
4980 4763 addr = (caddr_t)P2END((uintptr_t)addr,
4981 4764 TTEBYTES(1));
4982 4765 else
4983 4766 addr = (caddr_t)P2END((uintptr_t)addr,
4984 4767 TTEBYTES(hashno));
4985 4768 hashno = 1;
4986 4769 } else {
4987 4770 hashno++;
4988 4771 }
4989 4772 }
4990 4773
4991 4774 sfmmu_hblks_list_purge(&list, 0);
4992 4775 DEMAP_RANGE_FLUSH(&dmr);
4993 4776 cpuset = sfmmup->sfmmu_cpusran;
4994 4777 xt_sync(cpuset);
4995 4778 }
4996 4779
4997 4780 /*
4998 4781 * This function chgattr on a range of addresses in an hmeblk. It returns the
4999 4782 * next addres that needs to be chgattr.
5000 4783 * It should be called with the hash lock held.
5001 4784 * XXX It should be possible to optimize chgattr by not flushing every time but
5002 4785 * on the other hand:
5003 4786 * 1. do one flush crosscall.
5004 4787 * 2. only flush if we are increasing permissions (make sure this will work)
5005 4788 */
5006 4789 static caddr_t
5007 4790 sfmmu_hblk_chgattr(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr,
5008 4791 caddr_t endaddr, demap_range_t *dmrp, uint_t attr, int mode)
5009 4792 {
5010 4793 tte_t tte, tteattr, tteflags, ttemod;
5011 4794 struct sf_hment *sfhmep;
5012 4795 int ttesz;
5013 4796 struct page *pp = NULL;
5014 4797 kmutex_t *pml, *pmtx;
5015 4798 int ret;
5016 4799 int use_demap_range;
5017 4800 #if defined(SF_ERRATA_57)
5018 4801 int check_exec;
5019 4802 #endif
5020 4803
5021 4804 ASSERT(in_hblk_range(hmeblkp, addr));
5022 4805 ASSERT(hmeblkp->hblk_shw_bit == 0);
5023 4806 ASSERT(!hmeblkp->hblk_shared);
5024 4807
5025 4808 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp));
5026 4809 ttesz = get_hblk_ttesz(hmeblkp);
5027 4810
5028 4811 /*
5029 4812 * Flush the current demap region if addresses have been
5030 4813 * skipped or the page size doesn't match.
5031 4814 */
5032 4815 use_demap_range = (TTEBYTES(ttesz) == DEMAP_RANGE_PGSZ(dmrp));
5033 4816 if (use_demap_range) {
5034 4817 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr);
5035 4818 } else if (dmrp != NULL) {
5036 4819 DEMAP_RANGE_FLUSH(dmrp);
5037 4820 }
5038 4821
5039 4822 tteattr.ll = sfmmu_vtop_attr(attr, mode, &tteflags);
5040 4823 #if defined(SF_ERRATA_57)
5041 4824 check_exec = (sfmmup != ksfmmup) &&
5042 4825 AS_TYPE_64BIT(sfmmup->sfmmu_as) &&
5043 4826 TTE_IS_EXECUTABLE(&tteattr);
5044 4827 #endif
5045 4828 HBLKTOHME(sfhmep, hmeblkp, addr);
5046 4829 while (addr < endaddr) {
5047 4830 sfmmu_copytte(&sfhmep->hme_tte, &tte);
5048 4831 if (TTE_IS_VALID(&tte)) {
5049 4832 if ((tte.ll & tteflags.ll) == tteattr.ll) {
5050 4833 /*
5051 4834 * if the new attr is the same as old
5052 4835 * continue
5053 4836 */
5054 4837 goto next_addr;
5055 4838 }
5056 4839 if (!TTE_IS_WRITABLE(&tteattr)) {
5057 4840 /*
5058 4841 * make sure we clear hw modify bit if we
5059 4842 * removing write protections
5060 4843 */
5061 4844 tteflags.tte_intlo |= TTE_HWWR_INT;
5062 4845 }
5063 4846
5064 4847 pml = NULL;
5065 4848 pp = sfhmep->hme_page;
5066 4849 if (pp) {
5067 4850 pml = sfmmu_mlist_enter(pp);
5068 4851 }
5069 4852
5070 4853 if (pp != sfhmep->hme_page) {
5071 4854 /*
5072 4855 * tte must have been unloaded.
5073 4856 */
5074 4857 ASSERT(pml);
5075 4858 sfmmu_mlist_exit(pml);
5076 4859 continue;
5077 4860 }
5078 4861
5079 4862 ASSERT(pp == NULL || sfmmu_mlist_held(pp));
5080 4863
5081 4864 ttemod = tte;
5082 4865 ttemod.ll = (ttemod.ll & ~tteflags.ll) | tteattr.ll;
5083 4866 ASSERT(TTE_TO_TTEPFN(&ttemod) == TTE_TO_TTEPFN(&tte));
5084 4867
5085 4868 #if defined(SF_ERRATA_57)
5086 4869 if (check_exec && addr < errata57_limit)
5087 4870 ttemod.tte_exec_perm = 0;
5088 4871 #endif
5089 4872 ret = sfmmu_modifytte_try(&tte, &ttemod,
5090 4873 &sfhmep->hme_tte);
5091 4874
5092 4875 if (ret < 0) {
5093 4876 /* tte changed underneath us */
5094 4877 if (pml) {
5095 4878 sfmmu_mlist_exit(pml);
5096 4879 }
5097 4880 continue;
5098 4881 }
5099 4882
5100 4883 if (tteflags.tte_intlo & TTE_HWWR_INT) {
5101 4884 /*
5102 4885 * need to sync if we are clearing modify bit.
5103 4886 */
5104 4887 sfmmu_ttesync(sfmmup, addr, &tte, pp);
5105 4888 }
5106 4889
5107 4890 if (pp && PP_ISRO(pp)) {
5108 4891 if (tteattr.tte_intlo & TTE_WRPRM_INT) {
5109 4892 pmtx = sfmmu_page_enter(pp);
5110 4893 PP_CLRRO(pp);
5111 4894 sfmmu_page_exit(pmtx);
5112 4895 }
5113 4896 }
5114 4897
5115 4898 if (ret > 0 && use_demap_range) {
5116 4899 DEMAP_RANGE_MARKPG(dmrp, addr);
5117 4900 } else if (ret > 0) {
5118 4901 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0);
5119 4902 }
5120 4903
5121 4904 if (pml) {
5122 4905 sfmmu_mlist_exit(pml);
5123 4906 }
5124 4907 }
5125 4908 next_addr:
5126 4909 addr += TTEBYTES(ttesz);
5127 4910 sfhmep++;
5128 4911 DEMAP_RANGE_NEXTPG(dmrp);
5129 4912 }
5130 4913 return (addr);
5131 4914 }
5132 4915
5133 4916 /*
5134 4917 * This routine converts virtual attributes to physical ones. It will
5135 4918 * update the tteflags field with the tte mask corresponding to the attributes
5136 4919 * affected and it returns the new attributes. It will also clear the modify
5137 4920 * bit if we are taking away write permission. This is necessary since the
5138 4921 * modify bit is the hardware permission bit and we need to clear it in order
5139 4922 * to detect write faults.
5140 4923 */
5141 4924 static uint64_t
5142 4925 sfmmu_vtop_attr(uint_t attr, int mode, tte_t *ttemaskp)
5143 4926 {
5144 4927 tte_t ttevalue;
5145 4928
5146 4929 ASSERT(!(attr & ~SFMMU_LOAD_ALLATTR));
5147 4930
5148 4931 switch (mode) {
5149 4932 case SFMMU_CHGATTR:
5150 4933 /* all attributes specified */
5151 4934 ttevalue.tte_inthi = MAKE_TTEATTR_INTHI(attr);
5152 4935 ttevalue.tte_intlo = MAKE_TTEATTR_INTLO(attr);
5153 4936 ttemaskp->tte_inthi = TTEINTHI_ATTR;
5154 4937 ttemaskp->tte_intlo = TTEINTLO_ATTR;
5155 4938 break;
5156 4939 case SFMMU_SETATTR:
5157 4940 ASSERT(!(attr & ~HAT_PROT_MASK));
5158 4941 ttemaskp->ll = 0;
5159 4942 ttevalue.ll = 0;
5160 4943 /*
5161 4944 * a valid tte implies exec and read for sfmmu
5162 4945 * so no need to do anything about them.
5163 4946 * since priviledged access implies user access
5164 4947 * PROT_USER doesn't make sense either.
5165 4948 */
5166 4949 if (attr & PROT_WRITE) {
5167 4950 ttemaskp->tte_intlo |= TTE_WRPRM_INT;
5168 4951 ttevalue.tte_intlo |= TTE_WRPRM_INT;
5169 4952 }
5170 4953 break;
5171 4954 case SFMMU_CLRATTR:
5172 4955 /* attributes will be nand with current ones */
5173 4956 if (attr & ~(PROT_WRITE | PROT_USER)) {
5174 4957 panic("sfmmu: attr %x not supported", attr);
5175 4958 }
5176 4959 ttemaskp->ll = 0;
5177 4960 ttevalue.ll = 0;
5178 4961 if (attr & PROT_WRITE) {
5179 4962 /* clear both writable and modify bit */
5180 4963 ttemaskp->tte_intlo |= TTE_WRPRM_INT | TTE_HWWR_INT;
5181 4964 }
5182 4965 if (attr & PROT_USER) {
5183 4966 ttemaskp->tte_intlo |= TTE_PRIV_INT;
5184 4967 ttevalue.tte_intlo |= TTE_PRIV_INT;
5185 4968 }
5186 4969 break;
5187 4970 default:
5188 4971 panic("sfmmu_vtop_attr: bad mode %x", mode);
5189 4972 }
5190 4973 ASSERT(TTE_TO_TTEPFN(&ttevalue) == 0);
5191 4974 return (ttevalue.ll);
5192 4975 }
5193 4976
5194 4977 static uint_t
5195 4978 sfmmu_ptov_attr(tte_t *ttep)
5196 4979 {
5197 4980 uint_t attr;
5198 4981
5199 4982 ASSERT(TTE_IS_VALID(ttep));
5200 4983
5201 4984 attr = PROT_READ;
5202 4985
5203 4986 if (TTE_IS_WRITABLE(ttep)) {
5204 4987 attr |= PROT_WRITE;
5205 4988 }
5206 4989 if (TTE_IS_EXECUTABLE(ttep)) {
5207 4990 attr |= PROT_EXEC;
5208 4991 }
5209 4992 if (!TTE_IS_PRIVILEGED(ttep)) {
5210 4993 attr |= PROT_USER;
5211 4994 }
5212 4995 if (TTE_IS_NFO(ttep)) {
5213 4996 attr |= HAT_NOFAULT;
5214 4997 }
5215 4998 if (TTE_IS_NOSYNC(ttep)) {
5216 4999 attr |= HAT_NOSYNC;
5217 5000 }
5218 5001 if (TTE_IS_SIDEFFECT(ttep)) {
5219 5002 attr |= SFMMU_SIDEFFECT;
5220 5003 }
5221 5004 if (!TTE_IS_VCACHEABLE(ttep)) {
5222 5005 attr |= SFMMU_UNCACHEVTTE;
5223 5006 }
5224 5007 if (!TTE_IS_PCACHEABLE(ttep)) {
5225 5008 attr |= SFMMU_UNCACHEPTTE;
5226 5009 }
5227 5010 return (attr);
5228 5011 }
5229 5012
5230 5013 /*
5231 5014 * hat_chgprot is a deprecated hat call. New segment drivers
5232 5015 * should store all attributes and use hat_*attr calls.
5233 5016 *
5234 5017 * Change the protections in the virtual address range
5235 5018 * given to the specified virtual protection. If vprot is ~PROT_WRITE,
5236 5019 * then remove write permission, leaving the other
5237 5020 * permissions unchanged. If vprot is ~PROT_USER, remove user permissions.
5238 5021 *
5239 5022 */
5240 5023 void
5241 5024 hat_chgprot(struct hat *sfmmup, caddr_t addr, size_t len, uint_t vprot)
5242 5025 {
5243 5026 struct hmehash_bucket *hmebp;
↓ open down ↓ |
332 lines elided |
↑ open up ↑ |
5244 5027 hmeblk_tag hblktag;
5245 5028 int hmeshift, hashno = 1;
5246 5029 struct hme_blk *hmeblkp, *list = NULL;
5247 5030 caddr_t endaddr;
5248 5031 cpuset_t cpuset;
5249 5032 demap_range_t dmr;
5250 5033
5251 5034 ASSERT((len & MMU_PAGEOFFSET) == 0);
5252 5035 ASSERT(((uintptr_t)addr & MMU_PAGEOFFSET) == 0);
5253 5036
5254 - if (sfmmup->sfmmu_xhat_provider) {
5255 - XHAT_CHGPROT(sfmmup, addr, len, vprot);
5256 - return;
5257 - } else {
5258 - /*
5259 - * This must be a CPU HAT. If the address space has
5260 - * XHATs attached, change attributes for all of them,
5261 - * just in case
5262 - */
5263 - ASSERT(sfmmup->sfmmu_as != NULL);
5264 - if (sfmmup->sfmmu_as->a_xhat != NULL)
5265 - xhat_chgprot_all(sfmmup->sfmmu_as, addr, len, vprot);
5266 - }
5037 + ASSERT(sfmmup->sfmmu_as != NULL);
5267 5038
5268 5039 CPUSET_ZERO(cpuset);
5269 5040
5270 5041 if ((vprot != (uint_t)~PROT_WRITE) && (vprot & PROT_USER) &&
5271 5042 ((addr + len) > (caddr_t)USERLIMIT)) {
5272 5043 panic("user addr %p vprot %x in kernel space",
5273 5044 (void *)addr, vprot);
5274 5045 }
5275 5046 endaddr = addr + len;
5276 5047 hblktag.htag_id = sfmmup;
5277 5048 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
5278 5049 DEMAP_RANGE_INIT(sfmmup, &dmr);
5279 5050
5280 5051 while (addr < endaddr) {
5281 5052 hmeshift = HME_HASH_SHIFT(hashno);
5282 5053 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
5283 5054 hblktag.htag_rehash = hashno;
5284 5055 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
5285 5056
5286 5057 SFMMU_HASH_LOCK(hmebp);
5287 5058
5288 5059 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list);
5289 5060 if (hmeblkp != NULL) {
5290 5061 ASSERT(!hmeblkp->hblk_shared);
5291 5062 /*
5292 5063 * We've encountered a shadow hmeblk so skip the range
5293 5064 * of the next smaller mapping size.
5294 5065 */
5295 5066 if (hmeblkp->hblk_shw_bit) {
5296 5067 ASSERT(sfmmup != ksfmmup);
5297 5068 ASSERT(hashno > 1);
5298 5069 addr = (caddr_t)P2END((uintptr_t)addr,
5299 5070 TTEBYTES(hashno - 1));
5300 5071 } else {
5301 5072 addr = sfmmu_hblk_chgprot(sfmmup, hmeblkp,
5302 5073 addr, endaddr, &dmr, vprot);
5303 5074 }
5304 5075 SFMMU_HASH_UNLOCK(hmebp);
5305 5076 hashno = 1;
5306 5077 continue;
5307 5078 }
5308 5079 SFMMU_HASH_UNLOCK(hmebp);
5309 5080
5310 5081 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) {
5311 5082 /*
5312 5083 * We have traversed the whole list and rehashed
5313 5084 * if necessary without finding the address to chgprot.
5314 5085 * This is ok so we increment the address by the
5315 5086 * smallest hmeblk range for kernel mappings and the
5316 5087 * largest hmeblk range, to account for shadow hmeblks,
5317 5088 * for user mappings and continue.
5318 5089 */
5319 5090 if (sfmmup == ksfmmup)
5320 5091 addr = (caddr_t)P2END((uintptr_t)addr,
5321 5092 TTEBYTES(1));
5322 5093 else
5323 5094 addr = (caddr_t)P2END((uintptr_t)addr,
5324 5095 TTEBYTES(hashno));
5325 5096 hashno = 1;
5326 5097 } else {
5327 5098 hashno++;
5328 5099 }
5329 5100 }
5330 5101
5331 5102 sfmmu_hblks_list_purge(&list, 0);
5332 5103 DEMAP_RANGE_FLUSH(&dmr);
5333 5104 cpuset = sfmmup->sfmmu_cpusran;
5334 5105 xt_sync(cpuset);
5335 5106 }
5336 5107
5337 5108 /*
5338 5109 * This function chgprots a range of addresses in an hmeblk. It returns the
5339 5110 * next addres that needs to be chgprot.
5340 5111 * It should be called with the hash lock held.
5341 5112 * XXX It shold be possible to optimize chgprot by not flushing every time but
5342 5113 * on the other hand:
5343 5114 * 1. do one flush crosscall.
5344 5115 * 2. only flush if we are increasing permissions (make sure this will work)
5345 5116 */
5346 5117 static caddr_t
5347 5118 sfmmu_hblk_chgprot(sfmmu_t *sfmmup, struct hme_blk *hmeblkp, caddr_t addr,
5348 5119 caddr_t endaddr, demap_range_t *dmrp, uint_t vprot)
5349 5120 {
5350 5121 uint_t pprot;
5351 5122 tte_t tte, ttemod;
5352 5123 struct sf_hment *sfhmep;
5353 5124 uint_t tteflags;
5354 5125 int ttesz;
5355 5126 struct page *pp = NULL;
5356 5127 kmutex_t *pml, *pmtx;
5357 5128 int ret;
5358 5129 int use_demap_range;
5359 5130 #if defined(SF_ERRATA_57)
5360 5131 int check_exec;
5361 5132 #endif
5362 5133
5363 5134 ASSERT(in_hblk_range(hmeblkp, addr));
5364 5135 ASSERT(hmeblkp->hblk_shw_bit == 0);
5365 5136 ASSERT(!hmeblkp->hblk_shared);
5366 5137
5367 5138 #ifdef DEBUG
5368 5139 if (get_hblk_ttesz(hmeblkp) != TTE8K &&
5369 5140 (endaddr < get_hblk_endaddr(hmeblkp))) {
5370 5141 panic("sfmmu_hblk_chgprot: partial chgprot of large page");
5371 5142 }
5372 5143 #endif /* DEBUG */
5373 5144
5374 5145 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp));
5375 5146 ttesz = get_hblk_ttesz(hmeblkp);
5376 5147
5377 5148 pprot = sfmmu_vtop_prot(vprot, &tteflags);
5378 5149 #if defined(SF_ERRATA_57)
5379 5150 check_exec = (sfmmup != ksfmmup) &&
5380 5151 AS_TYPE_64BIT(sfmmup->sfmmu_as) &&
5381 5152 ((vprot & PROT_EXEC) == PROT_EXEC);
5382 5153 #endif
5383 5154 HBLKTOHME(sfhmep, hmeblkp, addr);
5384 5155
5385 5156 /*
5386 5157 * Flush the current demap region if addresses have been
5387 5158 * skipped or the page size doesn't match.
5388 5159 */
5389 5160 use_demap_range = (TTEBYTES(ttesz) == MMU_PAGESIZE);
5390 5161 if (use_demap_range) {
5391 5162 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr);
5392 5163 } else if (dmrp != NULL) {
5393 5164 DEMAP_RANGE_FLUSH(dmrp);
5394 5165 }
5395 5166
5396 5167 while (addr < endaddr) {
5397 5168 sfmmu_copytte(&sfhmep->hme_tte, &tte);
5398 5169 if (TTE_IS_VALID(&tte)) {
5399 5170 if (TTE_GET_LOFLAGS(&tte, tteflags) == pprot) {
5400 5171 /*
5401 5172 * if the new protection is the same as old
5402 5173 * continue
5403 5174 */
5404 5175 goto next_addr;
5405 5176 }
5406 5177 pml = NULL;
5407 5178 pp = sfhmep->hme_page;
5408 5179 if (pp) {
5409 5180 pml = sfmmu_mlist_enter(pp);
5410 5181 }
5411 5182 if (pp != sfhmep->hme_page) {
5412 5183 /*
5413 5184 * tte most have been unloaded
5414 5185 * underneath us. Recheck
5415 5186 */
5416 5187 ASSERT(pml);
5417 5188 sfmmu_mlist_exit(pml);
5418 5189 continue;
5419 5190 }
5420 5191
5421 5192 ASSERT(pp == NULL || sfmmu_mlist_held(pp));
5422 5193
5423 5194 ttemod = tte;
5424 5195 TTE_SET_LOFLAGS(&ttemod, tteflags, pprot);
5425 5196 #if defined(SF_ERRATA_57)
5426 5197 if (check_exec && addr < errata57_limit)
5427 5198 ttemod.tte_exec_perm = 0;
5428 5199 #endif
5429 5200 ret = sfmmu_modifytte_try(&tte, &ttemod,
5430 5201 &sfhmep->hme_tte);
5431 5202
5432 5203 if (ret < 0) {
5433 5204 /* tte changed underneath us */
5434 5205 if (pml) {
5435 5206 sfmmu_mlist_exit(pml);
5436 5207 }
5437 5208 continue;
5438 5209 }
5439 5210
5440 5211 if (tteflags & TTE_HWWR_INT) {
5441 5212 /*
5442 5213 * need to sync if we are clearing modify bit.
5443 5214 */
5444 5215 sfmmu_ttesync(sfmmup, addr, &tte, pp);
5445 5216 }
5446 5217
5447 5218 if (pp && PP_ISRO(pp)) {
5448 5219 if (pprot & TTE_WRPRM_INT) {
5449 5220 pmtx = sfmmu_page_enter(pp);
5450 5221 PP_CLRRO(pp);
5451 5222 sfmmu_page_exit(pmtx);
5452 5223 }
5453 5224 }
5454 5225
5455 5226 if (ret > 0 && use_demap_range) {
5456 5227 DEMAP_RANGE_MARKPG(dmrp, addr);
5457 5228 } else if (ret > 0) {
5458 5229 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0);
5459 5230 }
5460 5231
5461 5232 if (pml) {
5462 5233 sfmmu_mlist_exit(pml);
5463 5234 }
5464 5235 }
5465 5236 next_addr:
5466 5237 addr += TTEBYTES(ttesz);
5467 5238 sfhmep++;
5468 5239 DEMAP_RANGE_NEXTPG(dmrp);
5469 5240 }
5470 5241 return (addr);
5471 5242 }
5472 5243
5473 5244 /*
5474 5245 * This routine is deprecated and should only be used by hat_chgprot.
5475 5246 * The correct routine is sfmmu_vtop_attr.
5476 5247 * This routine converts virtual page protections to physical ones. It will
5477 5248 * update the tteflags field with the tte mask corresponding to the protections
5478 5249 * affected and it returns the new protections. It will also clear the modify
5479 5250 * bit if we are taking away write permission. This is necessary since the
5480 5251 * modify bit is the hardware permission bit and we need to clear it in order
5481 5252 * to detect write faults.
5482 5253 * It accepts the following special protections:
5483 5254 * ~PROT_WRITE = remove write permissions.
5484 5255 * ~PROT_USER = remove user permissions.
5485 5256 */
5486 5257 static uint_t
5487 5258 sfmmu_vtop_prot(uint_t vprot, uint_t *tteflagsp)
5488 5259 {
5489 5260 if (vprot == (uint_t)~PROT_WRITE) {
5490 5261 *tteflagsp = TTE_WRPRM_INT | TTE_HWWR_INT;
5491 5262 return (0); /* will cause wrprm to be cleared */
5492 5263 }
5493 5264 if (vprot == (uint_t)~PROT_USER) {
5494 5265 *tteflagsp = TTE_PRIV_INT;
5495 5266 return (0); /* will cause privprm to be cleared */
5496 5267 }
5497 5268 if ((vprot == 0) || (vprot == PROT_USER) ||
5498 5269 ((vprot & PROT_ALL) != vprot)) {
5499 5270 panic("sfmmu_vtop_prot -- bad prot %x", vprot);
5500 5271 }
5501 5272
5502 5273 switch (vprot) {
5503 5274 case (PROT_READ):
5504 5275 case (PROT_EXEC):
5505 5276 case (PROT_EXEC | PROT_READ):
5506 5277 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT | TTE_HWWR_INT;
5507 5278 return (TTE_PRIV_INT); /* set prv and clr wrt */
5508 5279 case (PROT_WRITE):
5509 5280 case (PROT_WRITE | PROT_READ):
5510 5281 case (PROT_EXEC | PROT_WRITE):
5511 5282 case (PROT_EXEC | PROT_WRITE | PROT_READ):
5512 5283 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT;
5513 5284 return (TTE_PRIV_INT | TTE_WRPRM_INT); /* set prv and wrt */
5514 5285 case (PROT_USER | PROT_READ):
5515 5286 case (PROT_USER | PROT_EXEC):
5516 5287 case (PROT_USER | PROT_EXEC | PROT_READ):
5517 5288 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT | TTE_HWWR_INT;
5518 5289 return (0); /* clr prv and wrt */
5519 5290 case (PROT_USER | PROT_WRITE):
5520 5291 case (PROT_USER | PROT_WRITE | PROT_READ):
5521 5292 case (PROT_USER | PROT_EXEC | PROT_WRITE):
5522 5293 case (PROT_USER | PROT_EXEC | PROT_WRITE | PROT_READ):
5523 5294 *tteflagsp = TTE_PRIV_INT | TTE_WRPRM_INT;
5524 5295 return (TTE_WRPRM_INT); /* clr prv and set wrt */
5525 5296 default:
5526 5297 panic("sfmmu_vtop_prot -- bad prot %x", vprot);
5527 5298 }
5528 5299 return (0);
5529 5300 }
5530 5301
5531 5302 /*
5532 5303 * Alternate unload for very large virtual ranges. With a true 64 bit VA,
5533 5304 * the normal algorithm would take too long for a very large VA range with
5534 5305 * few real mappings. This routine just walks thru all HMEs in the global
5535 5306 * hash table to find and remove mappings.
5536 5307 */
5537 5308 static void
5538 5309 hat_unload_large_virtual(
5539 5310 struct hat *sfmmup,
5540 5311 caddr_t startaddr,
5541 5312 size_t len,
5542 5313 uint_t flags,
5543 5314 hat_callback_t *callback)
5544 5315 {
5545 5316 struct hmehash_bucket *hmebp;
5546 5317 struct hme_blk *hmeblkp;
5547 5318 struct hme_blk *pr_hblk = NULL;
5548 5319 struct hme_blk *nx_hblk;
5549 5320 struct hme_blk *list = NULL;
5550 5321 int i;
5551 5322 demap_range_t dmr, *dmrp;
5552 5323 cpuset_t cpuset;
5553 5324 caddr_t endaddr = startaddr + len;
5554 5325 caddr_t sa;
5555 5326 caddr_t ea;
5556 5327 caddr_t cb_sa[MAX_CB_ADDR];
5557 5328 caddr_t cb_ea[MAX_CB_ADDR];
5558 5329 int addr_cnt = 0;
5559 5330 int a = 0;
5560 5331
5561 5332 if (sfmmup->sfmmu_free) {
5562 5333 dmrp = NULL;
5563 5334 } else {
5564 5335 dmrp = &dmr;
5565 5336 DEMAP_RANGE_INIT(sfmmup, dmrp);
5566 5337 }
5567 5338
5568 5339 /*
5569 5340 * Loop through all the hash buckets of HME blocks looking for matches.
5570 5341 */
5571 5342 for (i = 0; i <= UHMEHASH_SZ; i++) {
5572 5343 hmebp = &uhme_hash[i];
5573 5344 SFMMU_HASH_LOCK(hmebp);
5574 5345 hmeblkp = hmebp->hmeblkp;
5575 5346 pr_hblk = NULL;
5576 5347 while (hmeblkp) {
5577 5348 nx_hblk = hmeblkp->hblk_next;
5578 5349
5579 5350 /*
5580 5351 * skip if not this context, if a shadow block or
5581 5352 * if the mapping is not in the requested range
5582 5353 */
5583 5354 if (hmeblkp->hblk_tag.htag_id != sfmmup ||
5584 5355 hmeblkp->hblk_shw_bit ||
5585 5356 (sa = (caddr_t)get_hblk_base(hmeblkp)) >= endaddr ||
5586 5357 (ea = get_hblk_endaddr(hmeblkp)) <= startaddr) {
5587 5358 pr_hblk = hmeblkp;
5588 5359 goto next_block;
5589 5360 }
5590 5361
5591 5362 ASSERT(!hmeblkp->hblk_shared);
5592 5363 /*
5593 5364 * unload if there are any current valid mappings
5594 5365 */
5595 5366 if (hmeblkp->hblk_vcnt != 0 ||
5596 5367 hmeblkp->hblk_hmecnt != 0)
5597 5368 (void) sfmmu_hblk_unload(sfmmup, hmeblkp,
5598 5369 sa, ea, dmrp, flags);
5599 5370
5600 5371 /*
5601 5372 * on unmap we also release the HME block itself, once
5602 5373 * all mappings are gone.
5603 5374 */
5604 5375 if ((flags & HAT_UNLOAD_UNMAP) != 0 &&
5605 5376 !hmeblkp->hblk_vcnt &&
5606 5377 !hmeblkp->hblk_hmecnt) {
5607 5378 ASSERT(!hmeblkp->hblk_lckcnt);
5608 5379 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
5609 5380 &list, 0);
5610 5381 } else {
5611 5382 pr_hblk = hmeblkp;
5612 5383 }
5613 5384
5614 5385 if (callback == NULL)
5615 5386 goto next_block;
5616 5387
5617 5388 /*
5618 5389 * HME blocks may span more than one page, but we may be
5619 5390 * unmapping only one page, so check for a smaller range
5620 5391 * for the callback
5621 5392 */
5622 5393 if (sa < startaddr)
5623 5394 sa = startaddr;
5624 5395 if (--ea > endaddr)
5625 5396 ea = endaddr - 1;
5626 5397
5627 5398 cb_sa[addr_cnt] = sa;
5628 5399 cb_ea[addr_cnt] = ea;
5629 5400 if (++addr_cnt == MAX_CB_ADDR) {
5630 5401 if (dmrp != NULL) {
5631 5402 DEMAP_RANGE_FLUSH(dmrp);
5632 5403 cpuset = sfmmup->sfmmu_cpusran;
5633 5404 xt_sync(cpuset);
5634 5405 }
5635 5406
5636 5407 for (a = 0; a < MAX_CB_ADDR; ++a) {
5637 5408 callback->hcb_start_addr = cb_sa[a];
5638 5409 callback->hcb_end_addr = cb_ea[a];
5639 5410 callback->hcb_function(callback);
5640 5411 }
5641 5412 addr_cnt = 0;
5642 5413 }
5643 5414
5644 5415 next_block:
5645 5416 hmeblkp = nx_hblk;
5646 5417 }
5647 5418 SFMMU_HASH_UNLOCK(hmebp);
5648 5419 }
5649 5420
5650 5421 sfmmu_hblks_list_purge(&list, 0);
5651 5422 if (dmrp != NULL) {
5652 5423 DEMAP_RANGE_FLUSH(dmrp);
5653 5424 cpuset = sfmmup->sfmmu_cpusran;
5654 5425 xt_sync(cpuset);
5655 5426 }
5656 5427
5657 5428 for (a = 0; a < addr_cnt; ++a) {
5658 5429 callback->hcb_start_addr = cb_sa[a];
5659 5430 callback->hcb_end_addr = cb_ea[a];
5660 5431 callback->hcb_function(callback);
5661 5432 }
5662 5433
5663 5434 /*
5664 5435 * Check TSB and TLB page sizes if the process isn't exiting.
5665 5436 */
5666 5437 if (!sfmmup->sfmmu_free)
5667 5438 sfmmu_check_page_sizes(sfmmup, 0);
5668 5439 }
5669 5440
5670 5441 /*
5671 5442 * Unload all the mappings in the range [addr..addr+len). addr and len must
5672 5443 * be MMU_PAGESIZE aligned.
5673 5444 */
5674 5445
5675 5446 extern struct seg *segkmap;
5676 5447 #define ISSEGKMAP(sfmmup, addr) (sfmmup == ksfmmup && \
5677 5448 segkmap->s_base <= (addr) && (addr) < (segkmap->s_base + segkmap->s_size))
5678 5449
5679 5450
5680 5451 void
5681 5452 hat_unload_callback(
5682 5453 struct hat *sfmmup,
5683 5454 caddr_t addr,
5684 5455 size_t len,
5685 5456 uint_t flags,
5686 5457 hat_callback_t *callback)
5687 5458 {
5688 5459 struct hmehash_bucket *hmebp;
5689 5460 hmeblk_tag hblktag;
5690 5461 int hmeshift, hashno, iskernel;
↓ open down ↓ |
414 lines elided |
↑ open up ↑ |
5691 5462 struct hme_blk *hmeblkp, *pr_hblk, *list = NULL;
5692 5463 caddr_t endaddr;
5693 5464 cpuset_t cpuset;
5694 5465 int addr_count = 0;
5695 5466 int a;
5696 5467 caddr_t cb_start_addr[MAX_CB_ADDR];
5697 5468 caddr_t cb_end_addr[MAX_CB_ADDR];
5698 5469 int issegkmap = ISSEGKMAP(sfmmup, addr);
5699 5470 demap_range_t dmr, *dmrp;
5700 5471
5701 - if (sfmmup->sfmmu_xhat_provider) {
5702 - XHAT_UNLOAD_CALLBACK(sfmmup, addr, len, flags, callback);
5703 - return;
5704 - } else {
5705 - /*
5706 - * This must be a CPU HAT. If the address space has
5707 - * XHATs attached, unload the mappings for all of them,
5708 - * just in case
5709 - */
5710 - ASSERT(sfmmup->sfmmu_as != NULL);
5711 - if (sfmmup->sfmmu_as->a_xhat != NULL)
5712 - xhat_unload_callback_all(sfmmup->sfmmu_as, addr,
5713 - len, flags, callback);
5714 - }
5472 + ASSERT(sfmmup->sfmmu_as != NULL);
5715 5473
5716 5474 ASSERT((sfmmup == ksfmmup) || (flags & HAT_UNLOAD_OTHER) || \
5717 5475 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
5718 5476
5719 5477 ASSERT(sfmmup != NULL);
5720 5478 ASSERT((len & MMU_PAGEOFFSET) == 0);
5721 5479 ASSERT(!((uintptr_t)addr & MMU_PAGEOFFSET));
5722 5480
5723 5481 /*
5724 5482 * Probing through a large VA range (say 63 bits) will be slow, even
5725 5483 * at 4 Meg steps between the probes. So, when the virtual address range
5726 5484 * is very large, search the HME entries for what to unload.
5727 5485 *
5728 5486 * len >> TTE_PAGE_SHIFT(TTE4M) is the # of 4Meg probes we'd need
5729 5487 *
5730 5488 * UHMEHASH_SZ is number of hash buckets to examine
5731 5489 *
5732 5490 */
5733 5491 if (sfmmup != KHATID && (len >> TTE_PAGE_SHIFT(TTE4M)) > UHMEHASH_SZ) {
5734 5492 hat_unload_large_virtual(sfmmup, addr, len, flags, callback);
5735 5493 return;
5736 5494 }
5737 5495
5738 5496 CPUSET_ZERO(cpuset);
5739 5497
5740 5498 /*
5741 5499 * If the process is exiting, we can save a lot of fuss since
5742 5500 * we'll flush the TLB when we free the ctx anyway.
5743 5501 */
5744 5502 if (sfmmup->sfmmu_free) {
5745 5503 dmrp = NULL;
5746 5504 } else {
5747 5505 dmrp = &dmr;
5748 5506 DEMAP_RANGE_INIT(sfmmup, dmrp);
5749 5507 }
5750 5508
5751 5509 endaddr = addr + len;
5752 5510 hblktag.htag_id = sfmmup;
5753 5511 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
5754 5512
5755 5513 /*
5756 5514 * It is likely for the vm to call unload over a wide range of
5757 5515 * addresses that are actually very sparsely populated by
5758 5516 * translations. In order to speed this up the sfmmu hat supports
5759 5517 * the concept of shadow hmeblks. Dummy large page hmeblks that
5760 5518 * correspond to actual small translations are allocated at tteload
5761 5519 * time and are referred to as shadow hmeblks. Now, during unload
5762 5520 * time, we first check if we have a shadow hmeblk for that
5763 5521 * translation. The absence of one means the corresponding address
5764 5522 * range is empty and can be skipped.
5765 5523 *
5766 5524 * The kernel is an exception to above statement and that is why
5767 5525 * we don't use shadow hmeblks and hash starting from the smallest
5768 5526 * page size.
5769 5527 */
5770 5528 if (sfmmup == KHATID) {
5771 5529 iskernel = 1;
5772 5530 hashno = TTE64K;
5773 5531 } else {
5774 5532 iskernel = 0;
5775 5533 if (mmu_page_sizes == max_mmu_page_sizes) {
5776 5534 hashno = TTE256M;
5777 5535 } else {
5778 5536 hashno = TTE4M;
5779 5537 }
5780 5538 }
5781 5539 while (addr < endaddr) {
5782 5540 hmeshift = HME_HASH_SHIFT(hashno);
5783 5541 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
5784 5542 hblktag.htag_rehash = hashno;
5785 5543 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
5786 5544
5787 5545 SFMMU_HASH_LOCK(hmebp);
5788 5546
5789 5547 HME_HASH_SEARCH_PREV(hmebp, hblktag, hmeblkp, pr_hblk, &list);
5790 5548 if (hmeblkp == NULL) {
5791 5549 /*
5792 5550 * didn't find an hmeblk. skip the appropiate
5793 5551 * address range.
5794 5552 */
5795 5553 SFMMU_HASH_UNLOCK(hmebp);
5796 5554 if (iskernel) {
5797 5555 if (hashno < mmu_hashcnt) {
5798 5556 hashno++;
5799 5557 continue;
5800 5558 } else {
5801 5559 hashno = TTE64K;
5802 5560 addr = (caddr_t)roundup((uintptr_t)addr
5803 5561 + 1, MMU_PAGESIZE64K);
5804 5562 continue;
5805 5563 }
5806 5564 }
5807 5565 addr = (caddr_t)roundup((uintptr_t)addr + 1,
5808 5566 (1 << hmeshift));
5809 5567 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) {
5810 5568 ASSERT(hashno == TTE64K);
5811 5569 continue;
5812 5570 }
5813 5571 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) {
5814 5572 hashno = TTE512K;
5815 5573 continue;
5816 5574 }
5817 5575 if (mmu_page_sizes == max_mmu_page_sizes) {
5818 5576 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) {
5819 5577 hashno = TTE4M;
5820 5578 continue;
5821 5579 }
5822 5580 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) {
5823 5581 hashno = TTE32M;
5824 5582 continue;
5825 5583 }
5826 5584 hashno = TTE256M;
5827 5585 continue;
5828 5586 } else {
5829 5587 hashno = TTE4M;
5830 5588 continue;
5831 5589 }
5832 5590 }
5833 5591 ASSERT(hmeblkp);
5834 5592 ASSERT(!hmeblkp->hblk_shared);
5835 5593 if (!hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) {
5836 5594 /*
5837 5595 * If the valid count is zero we can skip the range
5838 5596 * mapped by this hmeblk.
5839 5597 * We free hblks in the case of HAT_UNMAP. HAT_UNMAP
5840 5598 * is used by segment drivers as a hint
5841 5599 * that the mapping resource won't be used any longer.
5842 5600 * The best example of this is during exit().
5843 5601 */
5844 5602 addr = (caddr_t)roundup((uintptr_t)addr + 1,
5845 5603 get_hblk_span(hmeblkp));
5846 5604 if ((flags & HAT_UNLOAD_UNMAP) ||
5847 5605 (iskernel && !issegkmap)) {
5848 5606 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk,
5849 5607 &list, 0);
5850 5608 }
5851 5609 SFMMU_HASH_UNLOCK(hmebp);
5852 5610
5853 5611 if (iskernel) {
5854 5612 hashno = TTE64K;
5855 5613 continue;
5856 5614 }
5857 5615 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) {
5858 5616 ASSERT(hashno == TTE64K);
5859 5617 continue;
5860 5618 }
5861 5619 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) {
5862 5620 hashno = TTE512K;
5863 5621 continue;
5864 5622 }
5865 5623 if (mmu_page_sizes == max_mmu_page_sizes) {
5866 5624 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) {
5867 5625 hashno = TTE4M;
5868 5626 continue;
5869 5627 }
5870 5628 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) {
5871 5629 hashno = TTE32M;
5872 5630 continue;
5873 5631 }
5874 5632 hashno = TTE256M;
5875 5633 continue;
5876 5634 } else {
5877 5635 hashno = TTE4M;
5878 5636 continue;
5879 5637 }
5880 5638 }
5881 5639 if (hmeblkp->hblk_shw_bit) {
5882 5640 /*
5883 5641 * If we encounter a shadow hmeblk we know there is
5884 5642 * smaller sized hmeblks mapping the same address space.
5885 5643 * Decrement the hash size and rehash.
5886 5644 */
5887 5645 ASSERT(sfmmup != KHATID);
5888 5646 hashno--;
5889 5647 SFMMU_HASH_UNLOCK(hmebp);
5890 5648 continue;
5891 5649 }
5892 5650
5893 5651 /*
5894 5652 * track callback address ranges.
5895 5653 * only start a new range when it's not contiguous
5896 5654 */
5897 5655 if (callback != NULL) {
5898 5656 if (addr_count > 0 &&
5899 5657 addr == cb_end_addr[addr_count - 1])
5900 5658 --addr_count;
5901 5659 else
5902 5660 cb_start_addr[addr_count] = addr;
5903 5661 }
5904 5662
5905 5663 addr = sfmmu_hblk_unload(sfmmup, hmeblkp, addr, endaddr,
5906 5664 dmrp, flags);
5907 5665
5908 5666 if (callback != NULL)
5909 5667 cb_end_addr[addr_count++] = addr;
5910 5668
5911 5669 if (((flags & HAT_UNLOAD_UNMAP) || (iskernel && !issegkmap)) &&
5912 5670 !hmeblkp->hblk_vcnt && !hmeblkp->hblk_hmecnt) {
5913 5671 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk, &list, 0);
5914 5672 }
5915 5673 SFMMU_HASH_UNLOCK(hmebp);
5916 5674
5917 5675 /*
5918 5676 * Notify our caller as to exactly which pages
5919 5677 * have been unloaded. We do these in clumps,
5920 5678 * to minimize the number of xt_sync()s that need to occur.
5921 5679 */
5922 5680 if (callback != NULL && addr_count == MAX_CB_ADDR) {
5923 5681 if (dmrp != NULL) {
5924 5682 DEMAP_RANGE_FLUSH(dmrp);
5925 5683 cpuset = sfmmup->sfmmu_cpusran;
5926 5684 xt_sync(cpuset);
5927 5685 }
5928 5686
5929 5687 for (a = 0; a < MAX_CB_ADDR; ++a) {
5930 5688 callback->hcb_start_addr = cb_start_addr[a];
5931 5689 callback->hcb_end_addr = cb_end_addr[a];
5932 5690 callback->hcb_function(callback);
5933 5691 }
5934 5692 addr_count = 0;
5935 5693 }
5936 5694 if (iskernel) {
5937 5695 hashno = TTE64K;
5938 5696 continue;
5939 5697 }
5940 5698 if ((uintptr_t)addr & MMU_PAGEOFFSET512K) {
5941 5699 ASSERT(hashno == TTE64K);
5942 5700 continue;
5943 5701 }
5944 5702 if ((uintptr_t)addr & MMU_PAGEOFFSET4M) {
5945 5703 hashno = TTE512K;
5946 5704 continue;
5947 5705 }
5948 5706 if (mmu_page_sizes == max_mmu_page_sizes) {
5949 5707 if ((uintptr_t)addr & MMU_PAGEOFFSET32M) {
5950 5708 hashno = TTE4M;
5951 5709 continue;
5952 5710 }
5953 5711 if ((uintptr_t)addr & MMU_PAGEOFFSET256M) {
5954 5712 hashno = TTE32M;
5955 5713 continue;
5956 5714 }
5957 5715 hashno = TTE256M;
5958 5716 } else {
5959 5717 hashno = TTE4M;
5960 5718 }
5961 5719 }
5962 5720
5963 5721 sfmmu_hblks_list_purge(&list, 0);
5964 5722 if (dmrp != NULL) {
5965 5723 DEMAP_RANGE_FLUSH(dmrp);
5966 5724 cpuset = sfmmup->sfmmu_cpusran;
5967 5725 xt_sync(cpuset);
5968 5726 }
5969 5727 if (callback && addr_count != 0) {
5970 5728 for (a = 0; a < addr_count; ++a) {
5971 5729 callback->hcb_start_addr = cb_start_addr[a];
5972 5730 callback->hcb_end_addr = cb_end_addr[a];
5973 5731 callback->hcb_function(callback);
5974 5732 }
5975 5733 }
5976 5734
5977 5735 /*
5978 5736 * Check TSB and TLB page sizes if the process isn't exiting.
5979 5737 */
5980 5738 if (!sfmmup->sfmmu_free)
↓ open down ↓ |
256 lines elided |
↑ open up ↑ |
5981 5739 sfmmu_check_page_sizes(sfmmup, 0);
5982 5740 }
5983 5741
5984 5742 /*
5985 5743 * Unload all the mappings in the range [addr..addr+len). addr and len must
5986 5744 * be MMU_PAGESIZE aligned.
5987 5745 */
5988 5746 void
5989 5747 hat_unload(struct hat *sfmmup, caddr_t addr, size_t len, uint_t flags)
5990 5748 {
5991 - if (sfmmup->sfmmu_xhat_provider) {
5992 - XHAT_UNLOAD(sfmmup, addr, len, flags);
5993 - return;
5994 - }
5995 5749 hat_unload_callback(sfmmup, addr, len, flags, NULL);
5996 5750 }
5997 5751
5998 5752
5999 5753 /*
6000 5754 * Find the largest mapping size for this page.
6001 5755 */
6002 5756 int
6003 5757 fnd_mapping_sz(page_t *pp)
6004 5758 {
6005 5759 int sz;
6006 5760 int p_index;
6007 5761
6008 5762 p_index = PP_MAPINDEX(pp);
6009 5763
6010 5764 sz = 0;
6011 5765 p_index >>= 1; /* don't care about 8K bit */
6012 5766 for (; p_index; p_index >>= 1) {
6013 5767 sz++;
6014 5768 }
6015 5769
6016 5770 return (sz);
6017 5771 }
6018 5772
6019 5773 /*
6020 5774 * This function unloads a range of addresses for an hmeblk.
6021 5775 * It returns the next address to be unloaded.
6022 5776 * It should be called with the hash lock held.
6023 5777 */
6024 5778 static caddr_t
6025 5779 sfmmu_hblk_unload(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr,
6026 5780 caddr_t endaddr, demap_range_t *dmrp, uint_t flags)
6027 5781 {
6028 5782 tte_t tte, ttemod;
6029 5783 struct sf_hment *sfhmep;
6030 5784 int ttesz;
6031 5785 long ttecnt;
6032 5786 page_t *pp;
6033 5787 kmutex_t *pml;
6034 5788 int ret;
6035 5789 int use_demap_range;
6036 5790
6037 5791 ASSERT(in_hblk_range(hmeblkp, addr));
6038 5792 ASSERT(!hmeblkp->hblk_shw_bit);
6039 5793 ASSERT(sfmmup != NULL || hmeblkp->hblk_shared);
6040 5794 ASSERT(sfmmup == NULL || !hmeblkp->hblk_shared);
6041 5795 ASSERT(dmrp == NULL || !hmeblkp->hblk_shared);
6042 5796
6043 5797 #ifdef DEBUG
6044 5798 if (get_hblk_ttesz(hmeblkp) != TTE8K &&
6045 5799 (endaddr < get_hblk_endaddr(hmeblkp))) {
6046 5800 panic("sfmmu_hblk_unload: partial unload of large page");
6047 5801 }
6048 5802 #endif /* DEBUG */
6049 5803
6050 5804 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp));
6051 5805 ttesz = get_hblk_ttesz(hmeblkp);
6052 5806
6053 5807 use_demap_range = ((dmrp == NULL) ||
6054 5808 (TTEBYTES(ttesz) == DEMAP_RANGE_PGSZ(dmrp)));
6055 5809
6056 5810 if (use_demap_range) {
6057 5811 DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr);
6058 5812 } else if (dmrp != NULL) {
6059 5813 DEMAP_RANGE_FLUSH(dmrp);
6060 5814 }
6061 5815 ttecnt = 0;
6062 5816 HBLKTOHME(sfhmep, hmeblkp, addr);
6063 5817
6064 5818 while (addr < endaddr) {
6065 5819 pml = NULL;
6066 5820 sfmmu_copytte(&sfhmep->hme_tte, &tte);
6067 5821 if (TTE_IS_VALID(&tte)) {
6068 5822 pp = sfhmep->hme_page;
6069 5823 if (pp != NULL) {
6070 5824 pml = sfmmu_mlist_enter(pp);
6071 5825 }
6072 5826
6073 5827 /*
6074 5828 * Verify if hme still points to 'pp' now that
6075 5829 * we have p_mapping lock.
6076 5830 */
6077 5831 if (sfhmep->hme_page != pp) {
6078 5832 if (pp != NULL && sfhmep->hme_page != NULL) {
6079 5833 ASSERT(pml != NULL);
6080 5834 sfmmu_mlist_exit(pml);
6081 5835 /* Re-start this iteration. */
6082 5836 continue;
6083 5837 }
6084 5838 ASSERT((pp != NULL) &&
6085 5839 (sfhmep->hme_page == NULL));
6086 5840 goto tte_unloaded;
6087 5841 }
6088 5842
6089 5843 /*
6090 5844 * This point on we have both HASH and p_mapping
6091 5845 * lock.
6092 5846 */
6093 5847 ASSERT(pp == sfhmep->hme_page);
6094 5848 ASSERT(pp == NULL || sfmmu_mlist_held(pp));
6095 5849
6096 5850 /*
6097 5851 * We need to loop on modify tte because it is
6098 5852 * possible for pagesync to come along and
6099 5853 * change the software bits beneath us.
6100 5854 *
6101 5855 * Page_unload can also invalidate the tte after
6102 5856 * we read tte outside of p_mapping lock.
6103 5857 */
6104 5858 again:
6105 5859 ttemod = tte;
6106 5860
6107 5861 TTE_SET_INVALID(&ttemod);
6108 5862 ret = sfmmu_modifytte_try(&tte, &ttemod,
6109 5863 &sfhmep->hme_tte);
6110 5864
6111 5865 if (ret <= 0) {
6112 5866 if (TTE_IS_VALID(&tte)) {
6113 5867 ASSERT(ret < 0);
6114 5868 goto again;
6115 5869 }
6116 5870 if (pp != NULL) {
6117 5871 panic("sfmmu_hblk_unload: pp = 0x%p "
6118 5872 "tte became invalid under mlist"
6119 5873 " lock = 0x%p", (void *)pp,
6120 5874 (void *)pml);
6121 5875 }
6122 5876 continue;
6123 5877 }
6124 5878
6125 5879 if (!(flags & HAT_UNLOAD_NOSYNC)) {
6126 5880 sfmmu_ttesync(sfmmup, addr, &tte, pp);
6127 5881 }
6128 5882
6129 5883 /*
6130 5884 * Ok- we invalidated the tte. Do the rest of the job.
6131 5885 */
6132 5886 ttecnt++;
6133 5887
6134 5888 if (flags & HAT_UNLOAD_UNLOCK) {
6135 5889 ASSERT(hmeblkp->hblk_lckcnt > 0);
6136 5890 atomic_dec_32(&hmeblkp->hblk_lckcnt);
6137 5891 HBLK_STACK_TRACE(hmeblkp, HBLK_UNLOCK);
6138 5892 }
6139 5893
6140 5894 /*
6141 5895 * Normally we would need to flush the page
6142 5896 * from the virtual cache at this point in
6143 5897 * order to prevent a potential cache alias
6144 5898 * inconsistency.
6145 5899 * The particular scenario we need to worry
6146 5900 * about is:
6147 5901 * Given: va1 and va2 are two virtual address
6148 5902 * that alias and map the same physical
6149 5903 * address.
6150 5904 * 1. mapping exists from va1 to pa and data
6151 5905 * has been read into the cache.
6152 5906 * 2. unload va1.
6153 5907 * 3. load va2 and modify data using va2.
6154 5908 * 4 unload va2.
6155 5909 * 5. load va1 and reference data. Unless we
6156 5910 * flush the data cache when we unload we will
6157 5911 * get stale data.
6158 5912 * Fortunately, page coloring eliminates the
6159 5913 * above scenario by remembering the color a
6160 5914 * physical page was last or is currently
6161 5915 * mapped to. Now, we delay the flush until
6162 5916 * the loading of translations. Only when the
6163 5917 * new translation is of a different color
6164 5918 * are we forced to flush.
6165 5919 */
6166 5920 if (use_demap_range) {
6167 5921 /*
6168 5922 * Mark this page as needing a demap.
6169 5923 */
6170 5924 DEMAP_RANGE_MARKPG(dmrp, addr);
6171 5925 } else {
6172 5926 ASSERT(sfmmup != NULL);
6173 5927 ASSERT(!hmeblkp->hblk_shared);
6174 5928 sfmmu_tlb_demap(addr, sfmmup, hmeblkp,
6175 5929 sfmmup->sfmmu_free, 0);
6176 5930 }
6177 5931
6178 5932 if (pp) {
6179 5933 /*
6180 5934 * Remove the hment from the mapping list
6181 5935 */
6182 5936 ASSERT(hmeblkp->hblk_hmecnt > 0);
6183 5937
6184 5938 /*
6185 5939 * Again, we cannot
6186 5940 * ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS);
6187 5941 */
6188 5942 HME_SUB(sfhmep, pp);
6189 5943 membar_stst();
6190 5944 atomic_dec_16(&hmeblkp->hblk_hmecnt);
6191 5945 }
6192 5946
6193 5947 ASSERT(hmeblkp->hblk_vcnt > 0);
6194 5948 atomic_dec_16(&hmeblkp->hblk_vcnt);
6195 5949
6196 5950 ASSERT(hmeblkp->hblk_hmecnt || hmeblkp->hblk_vcnt ||
6197 5951 !hmeblkp->hblk_lckcnt);
6198 5952
6199 5953 #ifdef VAC
6200 5954 if (pp && (pp->p_nrm & (P_KPMC | P_KPMS | P_TNC))) {
6201 5955 if (PP_ISTNC(pp)) {
6202 5956 /*
6203 5957 * If page was temporary
6204 5958 * uncached, try to recache
6205 5959 * it. Note that HME_SUB() was
6206 5960 * called above so p_index and
6207 5961 * mlist had been updated.
6208 5962 */
6209 5963 conv_tnc(pp, ttesz);
6210 5964 } else if (pp->p_mapping == NULL) {
6211 5965 ASSERT(kpm_enable);
6212 5966 /*
6213 5967 * Page is marked to be in VAC conflict
6214 5968 * to an existing kpm mapping and/or is
6215 5969 * kpm mapped using only the regular
6216 5970 * pagesize.
6217 5971 */
6218 5972 sfmmu_kpm_hme_unload(pp);
6219 5973 }
6220 5974 }
6221 5975 #endif /* VAC */
6222 5976 } else if ((pp = sfhmep->hme_page) != NULL) {
6223 5977 /*
6224 5978 * TTE is invalid but the hme
6225 5979 * still exists. let pageunload
6226 5980 * complete its job.
6227 5981 */
6228 5982 ASSERT(pml == NULL);
6229 5983 pml = sfmmu_mlist_enter(pp);
6230 5984 if (sfhmep->hme_page != NULL) {
6231 5985 sfmmu_mlist_exit(pml);
6232 5986 continue;
6233 5987 }
6234 5988 ASSERT(sfhmep->hme_page == NULL);
6235 5989 } else if (hmeblkp->hblk_hmecnt != 0) {
6236 5990 /*
6237 5991 * pageunload may have not finished decrementing
6238 5992 * hblk_vcnt and hblk_hmecnt. Find page_t if any and
6239 5993 * wait for pageunload to finish. Rely on pageunload
6240 5994 * to decrement hblk_hmecnt after hblk_vcnt.
6241 5995 */
6242 5996 pfn_t pfn = TTE_TO_TTEPFN(&tte);
6243 5997 ASSERT(pml == NULL);
6244 5998 if (pf_is_memory(pfn)) {
6245 5999 pp = page_numtopp_nolock(pfn);
6246 6000 if (pp != NULL) {
6247 6001 pml = sfmmu_mlist_enter(pp);
6248 6002 sfmmu_mlist_exit(pml);
6249 6003 pml = NULL;
6250 6004 }
6251 6005 }
6252 6006 }
6253 6007
6254 6008 tte_unloaded:
6255 6009 /*
6256 6010 * At this point, the tte we are looking at
6257 6011 * should be unloaded, and hme has been unlinked
6258 6012 * from page too. This is important because in
6259 6013 * pageunload, it does ttesync() then HME_SUB.
6260 6014 * We need to make sure HME_SUB has been completed
6261 6015 * so we know ttesync() has been completed. Otherwise,
6262 6016 * at exit time, after return from hat layer, VM will
6263 6017 * release as structure which hat_setstat() (called
6264 6018 * by ttesync()) needs.
6265 6019 */
6266 6020 #ifdef DEBUG
6267 6021 {
6268 6022 tte_t dtte;
6269 6023
6270 6024 ASSERT(sfhmep->hme_page == NULL);
6271 6025
6272 6026 sfmmu_copytte(&sfhmep->hme_tte, &dtte);
6273 6027 ASSERT(!TTE_IS_VALID(&dtte));
6274 6028 }
6275 6029 #endif
6276 6030
6277 6031 if (pml) {
6278 6032 sfmmu_mlist_exit(pml);
6279 6033 }
6280 6034
6281 6035 addr += TTEBYTES(ttesz);
6282 6036 sfhmep++;
6283 6037 DEMAP_RANGE_NEXTPG(dmrp);
6284 6038 }
6285 6039 /*
6286 6040 * For shared hmeblks this routine is only called when region is freed
6287 6041 * and no longer referenced. So no need to decrement ttecnt
6288 6042 * in the region structure here.
6289 6043 */
6290 6044 if (ttecnt > 0 && sfmmup != NULL) {
6291 6045 atomic_add_long(&sfmmup->sfmmu_ttecnt[ttesz], -ttecnt);
6292 6046 }
6293 6047 return (addr);
6294 6048 }
6295 6049
6296 6050 /*
6297 6051 * Invalidate a virtual address range for the local CPU.
6298 6052 * For best performance ensure that the va range is completely
6299 6053 * mapped, otherwise the entire TLB will be flushed.
6300 6054 */
6301 6055 void
6302 6056 hat_flush_range(struct hat *sfmmup, caddr_t va, size_t size)
6303 6057 {
6304 6058 ssize_t sz;
6305 6059 caddr_t endva = va + size;
6306 6060
6307 6061 while (va < endva) {
6308 6062 sz = hat_getpagesize(sfmmup, va);
6309 6063 if (sz < 0) {
6310 6064 vtag_flushall();
6311 6065 break;
6312 6066 }
6313 6067 vtag_flushpage(va, (uint64_t)sfmmup);
6314 6068 va += sz;
6315 6069 }
6316 6070 }
6317 6071
6318 6072 /*
6319 6073 * Synchronize all the mappings in the range [addr..addr+len).
6320 6074 * Can be called with clearflag having two states:
6321 6075 * HAT_SYNC_DONTZERO means just return the rm stats
6322 6076 * HAT_SYNC_ZERORM means zero rm bits in the tte and return the stats
6323 6077 */
↓ open down ↓ |
319 lines elided |
↑ open up ↑ |
6324 6078 void
6325 6079 hat_sync(struct hat *sfmmup, caddr_t addr, size_t len, uint_t clearflag)
6326 6080 {
6327 6081 struct hmehash_bucket *hmebp;
6328 6082 hmeblk_tag hblktag;
6329 6083 int hmeshift, hashno = 1;
6330 6084 struct hme_blk *hmeblkp, *list = NULL;
6331 6085 caddr_t endaddr;
6332 6086 cpuset_t cpuset;
6333 6087
6334 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
6335 6088 ASSERT((sfmmup == ksfmmup) ||
6336 6089 AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
6337 6090 ASSERT((len & MMU_PAGEOFFSET) == 0);
6338 6091 ASSERT((clearflag == HAT_SYNC_DONTZERO) ||
6339 6092 (clearflag == HAT_SYNC_ZERORM));
6340 6093
6341 6094 CPUSET_ZERO(cpuset);
6342 6095
6343 6096 endaddr = addr + len;
6344 6097 hblktag.htag_id = sfmmup;
6345 6098 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
6346 6099
6347 6100 /*
6348 6101 * Spitfire supports 4 page sizes.
6349 6102 * Most pages are expected to be of the smallest page
6350 6103 * size (8K) and these will not need to be rehashed. 64K
6351 6104 * pages also don't need to be rehashed because the an hmeblk
6352 6105 * spans 64K of address space. 512K pages might need 1 rehash and
6353 6106 * and 4M pages 2 rehashes.
6354 6107 */
6355 6108 while (addr < endaddr) {
6356 6109 hmeshift = HME_HASH_SHIFT(hashno);
6357 6110 hblktag.htag_bspage = HME_HASH_BSPAGE(addr, hmeshift);
6358 6111 hblktag.htag_rehash = hashno;
6359 6112 hmebp = HME_HASH_FUNCTION(sfmmup, addr, hmeshift);
6360 6113
6361 6114 SFMMU_HASH_LOCK(hmebp);
6362 6115
6363 6116 HME_HASH_SEARCH(hmebp, hblktag, hmeblkp, &list);
6364 6117 if (hmeblkp != NULL) {
6365 6118 ASSERT(!hmeblkp->hblk_shared);
6366 6119 /*
6367 6120 * We've encountered a shadow hmeblk so skip the range
6368 6121 * of the next smaller mapping size.
6369 6122 */
6370 6123 if (hmeblkp->hblk_shw_bit) {
6371 6124 ASSERT(sfmmup != ksfmmup);
6372 6125 ASSERT(hashno > 1);
6373 6126 addr = (caddr_t)P2END((uintptr_t)addr,
6374 6127 TTEBYTES(hashno - 1));
6375 6128 } else {
6376 6129 addr = sfmmu_hblk_sync(sfmmup, hmeblkp,
6377 6130 addr, endaddr, clearflag);
6378 6131 }
6379 6132 SFMMU_HASH_UNLOCK(hmebp);
6380 6133 hashno = 1;
6381 6134 continue;
6382 6135 }
6383 6136 SFMMU_HASH_UNLOCK(hmebp);
6384 6137
6385 6138 if (!HME_REHASH(sfmmup) || (hashno >= mmu_hashcnt)) {
6386 6139 /*
6387 6140 * We have traversed the whole list and rehashed
6388 6141 * if necessary without finding the address to sync.
6389 6142 * This is ok so we increment the address by the
6390 6143 * smallest hmeblk range for kernel mappings and the
6391 6144 * largest hmeblk range, to account for shadow hmeblks,
6392 6145 * for user mappings and continue.
6393 6146 */
6394 6147 if (sfmmup == ksfmmup)
6395 6148 addr = (caddr_t)P2END((uintptr_t)addr,
6396 6149 TTEBYTES(1));
6397 6150 else
6398 6151 addr = (caddr_t)P2END((uintptr_t)addr,
6399 6152 TTEBYTES(hashno));
6400 6153 hashno = 1;
6401 6154 } else {
6402 6155 hashno++;
6403 6156 }
6404 6157 }
6405 6158 sfmmu_hblks_list_purge(&list, 0);
6406 6159 cpuset = sfmmup->sfmmu_cpusran;
6407 6160 xt_sync(cpuset);
6408 6161 }
6409 6162
6410 6163 static caddr_t
6411 6164 sfmmu_hblk_sync(struct hat *sfmmup, struct hme_blk *hmeblkp, caddr_t addr,
6412 6165 caddr_t endaddr, int clearflag)
6413 6166 {
6414 6167 tte_t tte, ttemod;
6415 6168 struct sf_hment *sfhmep;
6416 6169 int ttesz;
6417 6170 struct page *pp;
6418 6171 kmutex_t *pml;
6419 6172 int ret;
6420 6173
6421 6174 ASSERT(hmeblkp->hblk_shw_bit == 0);
6422 6175 ASSERT(!hmeblkp->hblk_shared);
6423 6176
6424 6177 endaddr = MIN(endaddr, get_hblk_endaddr(hmeblkp));
6425 6178
6426 6179 ttesz = get_hblk_ttesz(hmeblkp);
6427 6180 HBLKTOHME(sfhmep, hmeblkp, addr);
6428 6181
6429 6182 while (addr < endaddr) {
6430 6183 sfmmu_copytte(&sfhmep->hme_tte, &tte);
6431 6184 if (TTE_IS_VALID(&tte)) {
6432 6185 pml = NULL;
6433 6186 pp = sfhmep->hme_page;
6434 6187 if (pp) {
6435 6188 pml = sfmmu_mlist_enter(pp);
6436 6189 }
6437 6190 if (pp != sfhmep->hme_page) {
6438 6191 /*
6439 6192 * tte most have been unloaded
6440 6193 * underneath us. Recheck
6441 6194 */
6442 6195 ASSERT(pml);
6443 6196 sfmmu_mlist_exit(pml);
6444 6197 continue;
6445 6198 }
6446 6199
6447 6200 ASSERT(pp == NULL || sfmmu_mlist_held(pp));
6448 6201
6449 6202 if (clearflag == HAT_SYNC_ZERORM) {
6450 6203 ttemod = tte;
6451 6204 TTE_CLR_RM(&ttemod);
6452 6205 ret = sfmmu_modifytte_try(&tte, &ttemod,
6453 6206 &sfhmep->hme_tte);
6454 6207 if (ret < 0) {
6455 6208 if (pml) {
6456 6209 sfmmu_mlist_exit(pml);
6457 6210 }
6458 6211 continue;
6459 6212 }
6460 6213
6461 6214 if (ret > 0) {
6462 6215 sfmmu_tlb_demap(addr, sfmmup,
6463 6216 hmeblkp, 0, 0);
6464 6217 }
6465 6218 }
6466 6219 sfmmu_ttesync(sfmmup, addr, &tte, pp);
6467 6220 if (pml) {
6468 6221 sfmmu_mlist_exit(pml);
6469 6222 }
6470 6223 }
6471 6224 addr += TTEBYTES(ttesz);
6472 6225 sfhmep++;
6473 6226 }
6474 6227 return (addr);
6475 6228 }
6476 6229
6477 6230 /*
6478 6231 * This function will sync a tte to the page struct and it will
6479 6232 * update the hat stats. Currently it allows us to pass a NULL pp
6480 6233 * and we will simply update the stats. We may want to change this
6481 6234 * so we only keep stats for pages backed by pp's.
6482 6235 */
6483 6236 static void
6484 6237 sfmmu_ttesync(struct hat *sfmmup, caddr_t addr, tte_t *ttep, page_t *pp)
6485 6238 {
6486 6239 uint_t rm = 0;
6487 6240 int sz;
6488 6241 pgcnt_t npgs;
6489 6242
6490 6243 ASSERT(TTE_IS_VALID(ttep));
6491 6244
6492 6245 if (TTE_IS_NOSYNC(ttep)) {
6493 6246 return;
6494 6247 }
6495 6248
6496 6249 if (TTE_IS_REF(ttep)) {
6497 6250 rm = P_REF;
6498 6251 }
6499 6252 if (TTE_IS_MOD(ttep)) {
6500 6253 rm |= P_MOD;
6501 6254 }
6502 6255
6503 6256 if (rm == 0) {
6504 6257 return;
6505 6258 }
6506 6259
6507 6260 sz = TTE_CSZ(ttep);
6508 6261 if (sfmmup != NULL && sfmmup->sfmmu_rmstat) {
6509 6262 int i;
6510 6263 caddr_t vaddr = addr;
6511 6264
6512 6265 for (i = 0; i < TTEPAGES(sz); i++, vaddr += MMU_PAGESIZE) {
6513 6266 hat_setstat(sfmmup->sfmmu_as, vaddr, MMU_PAGESIZE, rm);
6514 6267 }
6515 6268
6516 6269 }
6517 6270
6518 6271 /*
6519 6272 * XXX I want to use cas to update nrm bits but they
6520 6273 * currently belong in common/vm and not in hat where
6521 6274 * they should be.
6522 6275 * The nrm bits are protected by the same mutex as
6523 6276 * the one that protects the page's mapping list.
6524 6277 */
6525 6278 if (!pp)
6526 6279 return;
6527 6280 ASSERT(sfmmu_mlist_held(pp));
6528 6281 /*
6529 6282 * If the tte is for a large page, we need to sync all the
6530 6283 * pages covered by the tte.
6531 6284 */
6532 6285 if (sz != TTE8K) {
6533 6286 ASSERT(pp->p_szc != 0);
6534 6287 pp = PP_GROUPLEADER(pp, sz);
6535 6288 ASSERT(sfmmu_mlist_held(pp));
6536 6289 }
6537 6290
6538 6291 /* Get number of pages from tte size. */
6539 6292 npgs = TTEPAGES(sz);
6540 6293
6541 6294 do {
6542 6295 ASSERT(pp);
6543 6296 ASSERT(sfmmu_mlist_held(pp));
6544 6297 if (((rm & P_REF) != 0 && !PP_ISREF(pp)) ||
6545 6298 ((rm & P_MOD) != 0 && !PP_ISMOD(pp)))
6546 6299 hat_page_setattr(pp, rm);
6547 6300
6548 6301 /*
6549 6302 * Are we done? If not, we must have a large mapping.
6550 6303 * For large mappings we need to sync the rest of the pages
6551 6304 * covered by this tte; goto the next page.
6552 6305 */
6553 6306 } while (--npgs > 0 && (pp = PP_PAGENEXT(pp)));
6554 6307 }
6555 6308
6556 6309 /*
6557 6310 * Execute pre-callback handler of each pa_hment linked to pp
6558 6311 *
6559 6312 * Inputs:
6560 6313 * flag: either HAT_PRESUSPEND or HAT_SUSPEND.
6561 6314 * capture_cpus: pointer to return value (below)
6562 6315 *
6563 6316 * Returns:
6564 6317 * Propagates the subsystem callback return values back to the caller;
6565 6318 * returns 0 on success. If capture_cpus is non-NULL, the value returned
6566 6319 * is zero if all of the pa_hments are of a type that do not require
6567 6320 * capturing CPUs prior to suspending the mapping, else it is 1.
6568 6321 */
6569 6322 static int
6570 6323 hat_pageprocess_precallbacks(struct page *pp, uint_t flag, int *capture_cpus)
6571 6324 {
6572 6325 struct sf_hment *sfhmep;
6573 6326 struct pa_hment *pahmep;
6574 6327 int (*f)(caddr_t, uint_t, uint_t, void *);
6575 6328 int ret;
6576 6329 id_t id;
6577 6330 int locked = 0;
6578 6331 kmutex_t *pml;
6579 6332
6580 6333 ASSERT(PAGE_EXCL(pp));
6581 6334 if (!sfmmu_mlist_held(pp)) {
6582 6335 pml = sfmmu_mlist_enter(pp);
6583 6336 locked = 1;
6584 6337 }
6585 6338
6586 6339 if (capture_cpus)
6587 6340 *capture_cpus = 0;
6588 6341
6589 6342 top:
6590 6343 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) {
6591 6344 /*
6592 6345 * skip sf_hments corresponding to VA<->PA mappings;
6593 6346 * for pa_hment's, hme_tte.ll is zero
6594 6347 */
6595 6348 if (!IS_PAHME(sfhmep))
6596 6349 continue;
6597 6350
6598 6351 pahmep = sfhmep->hme_data;
6599 6352 ASSERT(pahmep != NULL);
6600 6353
6601 6354 /*
6602 6355 * skip if pre-handler has been called earlier in this loop
6603 6356 */
6604 6357 if (pahmep->flags & flag)
6605 6358 continue;
6606 6359
6607 6360 id = pahmep->cb_id;
6608 6361 ASSERT(id >= (id_t)0 && id < sfmmu_cb_nextid);
6609 6362 if (capture_cpus && sfmmu_cb_table[id].capture_cpus != 0)
6610 6363 *capture_cpus = 1;
6611 6364 if ((f = sfmmu_cb_table[id].prehandler) == NULL) {
6612 6365 pahmep->flags |= flag;
6613 6366 continue;
6614 6367 }
6615 6368
6616 6369 /*
6617 6370 * Drop the mapping list lock to avoid locking order issues.
6618 6371 */
6619 6372 if (locked)
6620 6373 sfmmu_mlist_exit(pml);
6621 6374
6622 6375 ret = f(pahmep->addr, pahmep->len, flag, pahmep->pvt);
6623 6376 if (ret != 0)
6624 6377 return (ret); /* caller must do the cleanup */
6625 6378
6626 6379 if (locked) {
6627 6380 pml = sfmmu_mlist_enter(pp);
6628 6381 pahmep->flags |= flag;
6629 6382 goto top;
6630 6383 }
6631 6384
6632 6385 pahmep->flags |= flag;
6633 6386 }
6634 6387
6635 6388 if (locked)
6636 6389 sfmmu_mlist_exit(pml);
6637 6390
6638 6391 return (0);
6639 6392 }
6640 6393
6641 6394 /*
6642 6395 * Execute post-callback handler of each pa_hment linked to pp
6643 6396 *
6644 6397 * Same overall assumptions and restrictions apply as for
6645 6398 * hat_pageprocess_precallbacks().
6646 6399 */
6647 6400 static void
6648 6401 hat_pageprocess_postcallbacks(struct page *pp, uint_t flag)
6649 6402 {
6650 6403 pfn_t pgpfn = pp->p_pagenum;
6651 6404 pfn_t pgmask = btop(page_get_pagesize(pp->p_szc)) - 1;
6652 6405 pfn_t newpfn;
6653 6406 struct sf_hment *sfhmep;
6654 6407 struct pa_hment *pahmep;
6655 6408 int (*f)(caddr_t, uint_t, uint_t, void *, pfn_t);
6656 6409 id_t id;
6657 6410 int locked = 0;
6658 6411 kmutex_t *pml;
6659 6412
6660 6413 ASSERT(PAGE_EXCL(pp));
6661 6414 if (!sfmmu_mlist_held(pp)) {
6662 6415 pml = sfmmu_mlist_enter(pp);
6663 6416 locked = 1;
6664 6417 }
6665 6418
6666 6419 top:
6667 6420 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) {
6668 6421 /*
6669 6422 * skip sf_hments corresponding to VA<->PA mappings;
6670 6423 * for pa_hment's, hme_tte.ll is zero
6671 6424 */
6672 6425 if (!IS_PAHME(sfhmep))
6673 6426 continue;
6674 6427
6675 6428 pahmep = sfhmep->hme_data;
6676 6429 ASSERT(pahmep != NULL);
6677 6430
6678 6431 if ((pahmep->flags & flag) == 0)
6679 6432 continue;
6680 6433
6681 6434 pahmep->flags &= ~flag;
6682 6435
6683 6436 id = pahmep->cb_id;
6684 6437 ASSERT(id >= (id_t)0 && id < sfmmu_cb_nextid);
6685 6438 if ((f = sfmmu_cb_table[id].posthandler) == NULL)
6686 6439 continue;
6687 6440
6688 6441 /*
6689 6442 * Convert the base page PFN into the constituent PFN
6690 6443 * which is needed by the callback handler.
6691 6444 */
6692 6445 newpfn = pgpfn | (btop((uintptr_t)pahmep->addr) & pgmask);
6693 6446
6694 6447 /*
6695 6448 * Drop the mapping list lock to avoid locking order issues.
6696 6449 */
6697 6450 if (locked)
6698 6451 sfmmu_mlist_exit(pml);
6699 6452
6700 6453 if (f(pahmep->addr, pahmep->len, flag, pahmep->pvt, newpfn)
6701 6454 != 0)
6702 6455 panic("sfmmu: posthandler failed");
6703 6456
6704 6457 if (locked) {
6705 6458 pml = sfmmu_mlist_enter(pp);
6706 6459 goto top;
6707 6460 }
6708 6461 }
6709 6462
6710 6463 if (locked)
6711 6464 sfmmu_mlist_exit(pml);
6712 6465 }
6713 6466
6714 6467 /*
6715 6468 * Suspend locked kernel mapping
6716 6469 */
6717 6470 void
6718 6471 hat_pagesuspend(struct page *pp)
6719 6472 {
6720 6473 struct sf_hment *sfhmep;
6721 6474 sfmmu_t *sfmmup;
6722 6475 tte_t tte, ttemod;
6723 6476 struct hme_blk *hmeblkp;
6724 6477 caddr_t addr;
6725 6478 int index, cons;
6726 6479 cpuset_t cpuset;
6727 6480
6728 6481 ASSERT(PAGE_EXCL(pp));
6729 6482 ASSERT(sfmmu_mlist_held(pp));
6730 6483
6731 6484 mutex_enter(&kpr_suspendlock);
6732 6485
6733 6486 /*
6734 6487 * We're about to suspend a kernel mapping so mark this thread as
6735 6488 * non-traceable by DTrace. This prevents us from running into issues
6736 6489 * with probe context trying to touch a suspended page
6737 6490 * in the relocation codepath itself.
6738 6491 */
6739 6492 curthread->t_flag |= T_DONTDTRACE;
6740 6493
6741 6494 index = PP_MAPINDEX(pp);
6742 6495 cons = TTE8K;
6743 6496
6744 6497 retry:
6745 6498 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) {
6746 6499
6747 6500 if (IS_PAHME(sfhmep))
6748 6501 continue;
6749 6502
6750 6503 if (get_hblk_ttesz(sfmmu_hmetohblk(sfhmep)) != cons)
6751 6504 continue;
6752 6505
6753 6506 /*
6754 6507 * Loop until we successfully set the suspend bit in
6755 6508 * the TTE.
6756 6509 */
6757 6510 again:
6758 6511 sfmmu_copytte(&sfhmep->hme_tte, &tte);
6759 6512 ASSERT(TTE_IS_VALID(&tte));
6760 6513
6761 6514 ttemod = tte;
6762 6515 TTE_SET_SUSPEND(&ttemod);
6763 6516 if (sfmmu_modifytte_try(&tte, &ttemod,
6764 6517 &sfhmep->hme_tte) < 0)
6765 6518 goto again;
6766 6519
6767 6520 /*
6768 6521 * Invalidate TSB entry
6769 6522 */
6770 6523 hmeblkp = sfmmu_hmetohblk(sfhmep);
6771 6524
6772 6525 sfmmup = hblktosfmmu(hmeblkp);
6773 6526 ASSERT(sfmmup == ksfmmup);
6774 6527 ASSERT(!hmeblkp->hblk_shared);
6775 6528
6776 6529 addr = tte_to_vaddr(hmeblkp, tte);
6777 6530
6778 6531 /*
6779 6532 * No need to make sure that the TSB for this sfmmu is
6780 6533 * not being relocated since it is ksfmmup and thus it
6781 6534 * will never be relocated.
6782 6535 */
6783 6536 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0);
6784 6537
6785 6538 /*
6786 6539 * Update xcall stats
6787 6540 */
6788 6541 cpuset = cpu_ready_set;
6789 6542 CPUSET_DEL(cpuset, CPU->cpu_id);
6790 6543
6791 6544 /* LINTED: constant in conditional context */
6792 6545 SFMMU_XCALL_STATS(ksfmmup);
6793 6546
6794 6547 /*
6795 6548 * Flush TLB entry on remote CPU's
6796 6549 */
6797 6550 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr,
6798 6551 (uint64_t)ksfmmup);
6799 6552 xt_sync(cpuset);
6800 6553
6801 6554 /*
6802 6555 * Flush TLB entry on local CPU
6803 6556 */
6804 6557 vtag_flushpage(addr, (uint64_t)ksfmmup);
6805 6558 }
6806 6559
6807 6560 while (index != 0) {
6808 6561 index = index >> 1;
6809 6562 if (index != 0)
6810 6563 cons++;
6811 6564 if (index & 0x1) {
6812 6565 pp = PP_GROUPLEADER(pp, cons);
6813 6566 goto retry;
6814 6567 }
6815 6568 }
6816 6569 }
6817 6570
6818 6571 #ifdef DEBUG
6819 6572
6820 6573 #define N_PRLE 1024
6821 6574 struct prle {
6822 6575 page_t *targ;
6823 6576 page_t *repl;
6824 6577 int status;
6825 6578 int pausecpus;
6826 6579 hrtime_t whence;
6827 6580 };
6828 6581
6829 6582 static struct prle page_relocate_log[N_PRLE];
6830 6583 static int prl_entry;
6831 6584 static kmutex_t prl_mutex;
6832 6585
6833 6586 #define PAGE_RELOCATE_LOG(t, r, s, p) \
6834 6587 mutex_enter(&prl_mutex); \
6835 6588 page_relocate_log[prl_entry].targ = *(t); \
6836 6589 page_relocate_log[prl_entry].repl = *(r); \
6837 6590 page_relocate_log[prl_entry].status = (s); \
6838 6591 page_relocate_log[prl_entry].pausecpus = (p); \
6839 6592 page_relocate_log[prl_entry].whence = gethrtime(); \
6840 6593 prl_entry = (prl_entry == (N_PRLE - 1))? 0 : prl_entry + 1; \
6841 6594 mutex_exit(&prl_mutex);
6842 6595
6843 6596 #else /* !DEBUG */
6844 6597 #define PAGE_RELOCATE_LOG(t, r, s, p)
6845 6598 #endif
6846 6599
6847 6600 /*
6848 6601 * Core Kernel Page Relocation Algorithm
6849 6602 *
6850 6603 * Input:
6851 6604 *
6852 6605 * target : constituent pages are SE_EXCL locked.
6853 6606 * replacement: constituent pages are SE_EXCL locked.
6854 6607 *
6855 6608 * Output:
6856 6609 *
6857 6610 * nrelocp: number of pages relocated
6858 6611 */
6859 6612 int
6860 6613 hat_page_relocate(page_t **target, page_t **replacement, spgcnt_t *nrelocp)
6861 6614 {
6862 6615 page_t *targ, *repl;
6863 6616 page_t *tpp, *rpp;
6864 6617 kmutex_t *low, *high;
6865 6618 spgcnt_t npages, i;
6866 6619 page_t *pl = NULL;
6867 6620 int old_pil;
6868 6621 cpuset_t cpuset;
6869 6622 int cap_cpus;
6870 6623 int ret;
6871 6624 #ifdef VAC
6872 6625 int cflags = 0;
6873 6626 #endif
6874 6627
6875 6628 if (!kcage_on || PP_ISNORELOC(*target)) {
6876 6629 PAGE_RELOCATE_LOG(target, replacement, EAGAIN, -1);
6877 6630 return (EAGAIN);
6878 6631 }
6879 6632
6880 6633 mutex_enter(&kpr_mutex);
6881 6634 kreloc_thread = curthread;
6882 6635
6883 6636 targ = *target;
6884 6637 repl = *replacement;
6885 6638 ASSERT(repl != NULL);
6886 6639 ASSERT(targ->p_szc == repl->p_szc);
6887 6640
6888 6641 npages = page_get_pagecnt(targ->p_szc);
6889 6642
6890 6643 /*
6891 6644 * unload VA<->PA mappings that are not locked
6892 6645 */
6893 6646 tpp = targ;
6894 6647 for (i = 0; i < npages; i++) {
6895 6648 (void) hat_pageunload(tpp, SFMMU_KERNEL_RELOC);
6896 6649 tpp++;
6897 6650 }
6898 6651
6899 6652 /*
6900 6653 * Do "presuspend" callbacks, in a context from which we can still
6901 6654 * block as needed. Note that we don't hold the mapping list lock
6902 6655 * of "targ" at this point due to potential locking order issues;
6903 6656 * we assume that between the hat_pageunload() above and holding
6904 6657 * the SE_EXCL lock that the mapping list *cannot* change at this
6905 6658 * point.
6906 6659 */
6907 6660 ret = hat_pageprocess_precallbacks(targ, HAT_PRESUSPEND, &cap_cpus);
6908 6661 if (ret != 0) {
6909 6662 /*
6910 6663 * EIO translates to fatal error, for all others cleanup
6911 6664 * and return EAGAIN.
6912 6665 */
6913 6666 ASSERT(ret != EIO);
6914 6667 hat_pageprocess_postcallbacks(targ, HAT_POSTUNSUSPEND);
6915 6668 PAGE_RELOCATE_LOG(target, replacement, ret, -1);
6916 6669 kreloc_thread = NULL;
6917 6670 mutex_exit(&kpr_mutex);
6918 6671 return (EAGAIN);
6919 6672 }
6920 6673
6921 6674 /*
6922 6675 * acquire p_mapping list lock for both the target and replacement
6923 6676 * root pages.
6924 6677 *
6925 6678 * low and high refer to the need to grab the mlist locks in a
6926 6679 * specific order in order to prevent race conditions. Thus the
6927 6680 * lower lock must be grabbed before the higher lock.
6928 6681 *
6929 6682 * This will block hat_unload's accessing p_mapping list. Since
6930 6683 * we have SE_EXCL lock, hat_memload and hat_pageunload will be
6931 6684 * blocked. Thus, no one else will be accessing the p_mapping list
6932 6685 * while we suspend and reload the locked mapping below.
6933 6686 */
6934 6687 tpp = targ;
6935 6688 rpp = repl;
6936 6689 sfmmu_mlist_reloc_enter(tpp, rpp, &low, &high);
6937 6690
6938 6691 kpreempt_disable();
6939 6692
6940 6693 /*
6941 6694 * We raise our PIL to 13 so that we don't get captured by
6942 6695 * another CPU or pinned by an interrupt thread. We can't go to
6943 6696 * PIL 14 since the nexus driver(s) may need to interrupt at
6944 6697 * that level in the case of IOMMU pseudo mappings.
6945 6698 */
6946 6699 cpuset = cpu_ready_set;
6947 6700 CPUSET_DEL(cpuset, CPU->cpu_id);
6948 6701 if (!cap_cpus || CPUSET_ISNULL(cpuset)) {
6949 6702 old_pil = splr(XCALL_PIL);
6950 6703 } else {
6951 6704 old_pil = -1;
6952 6705 xc_attention(cpuset);
6953 6706 }
6954 6707 ASSERT(getpil() == XCALL_PIL);
6955 6708
6956 6709 /*
6957 6710 * Now do suspend callbacks. In the case of an IOMMU mapping
6958 6711 * this will suspend all DMA activity to the page while it is
6959 6712 * being relocated. Since we are well above LOCK_LEVEL and CPUs
6960 6713 * may be captured at this point we should have acquired any needed
6961 6714 * locks in the presuspend callback.
6962 6715 */
6963 6716 ret = hat_pageprocess_precallbacks(targ, HAT_SUSPEND, NULL);
6964 6717 if (ret != 0) {
6965 6718 repl = targ;
6966 6719 goto suspend_fail;
6967 6720 }
6968 6721
6969 6722 /*
6970 6723 * Raise the PIL yet again, this time to block all high-level
6971 6724 * interrupts on this CPU. This is necessary to prevent an
6972 6725 * interrupt routine from pinning the thread which holds the
6973 6726 * mapping suspended and then touching the suspended page.
6974 6727 *
6975 6728 * Once the page is suspended we also need to be careful to
6976 6729 * avoid calling any functions which touch any seg_kmem memory
6977 6730 * since that memory may be backed by the very page we are
6978 6731 * relocating in here!
6979 6732 */
6980 6733 hat_pagesuspend(targ);
6981 6734
6982 6735 /*
6983 6736 * Now that we are confident everybody has stopped using this page,
6984 6737 * copy the page contents. Note we use a physical copy to prevent
6985 6738 * locking issues and to avoid fpRAS because we can't handle it in
6986 6739 * this context.
6987 6740 */
6988 6741 for (i = 0; i < npages; i++, tpp++, rpp++) {
6989 6742 #ifdef VAC
6990 6743 /*
6991 6744 * If the replacement has a different vcolor than
6992 6745 * the one being replacd, we need to handle VAC
6993 6746 * consistency for it just as we were setting up
6994 6747 * a new mapping to it.
6995 6748 */
6996 6749 if ((PP_GET_VCOLOR(rpp) != NO_VCOLOR) &&
6997 6750 (tpp->p_vcolor != rpp->p_vcolor) &&
6998 6751 !CacheColor_IsFlushed(cflags, PP_GET_VCOLOR(rpp))) {
6999 6752 CacheColor_SetFlushed(cflags, PP_GET_VCOLOR(rpp));
7000 6753 sfmmu_cache_flushcolor(PP_GET_VCOLOR(rpp),
7001 6754 rpp->p_pagenum);
7002 6755 }
7003 6756 #endif
7004 6757 /*
7005 6758 * Copy the contents of the page.
7006 6759 */
7007 6760 ppcopy_kernel(tpp, rpp);
7008 6761 }
7009 6762
7010 6763 tpp = targ;
7011 6764 rpp = repl;
7012 6765 for (i = 0; i < npages; i++, tpp++, rpp++) {
7013 6766 /*
7014 6767 * Copy attributes. VAC consistency was handled above,
7015 6768 * if required.
7016 6769 */
7017 6770 rpp->p_nrm = tpp->p_nrm;
7018 6771 tpp->p_nrm = 0;
7019 6772 rpp->p_index = tpp->p_index;
7020 6773 tpp->p_index = 0;
7021 6774 #ifdef VAC
7022 6775 rpp->p_vcolor = tpp->p_vcolor;
7023 6776 #endif
7024 6777 }
7025 6778
7026 6779 /*
7027 6780 * First, unsuspend the page, if we set the suspend bit, and transfer
7028 6781 * the mapping list from the target page to the replacement page.
7029 6782 * Next process postcallbacks; since pa_hment's are linked only to the
7030 6783 * p_mapping list of root page, we don't iterate over the constituent
7031 6784 * pages.
7032 6785 */
7033 6786 hat_pagereload(targ, repl);
7034 6787
7035 6788 suspend_fail:
7036 6789 hat_pageprocess_postcallbacks(repl, HAT_UNSUSPEND);
7037 6790
7038 6791 /*
7039 6792 * Now lower our PIL and release any captured CPUs since we
7040 6793 * are out of the "danger zone". After this it will again be
7041 6794 * safe to acquire adaptive mutex locks, or to drop them...
7042 6795 */
7043 6796 if (old_pil != -1) {
7044 6797 splx(old_pil);
7045 6798 } else {
7046 6799 xc_dismissed(cpuset);
7047 6800 }
7048 6801
7049 6802 kpreempt_enable();
7050 6803
7051 6804 sfmmu_mlist_reloc_exit(low, high);
7052 6805
7053 6806 /*
7054 6807 * Postsuspend callbacks should drop any locks held across
7055 6808 * the suspend callbacks. As before, we don't hold the mapping
7056 6809 * list lock at this point.. our assumption is that the mapping
7057 6810 * list still can't change due to our holding SE_EXCL lock and
7058 6811 * there being no unlocked mappings left. Hence the restriction
7059 6812 * on calling context to hat_delete_callback()
7060 6813 */
7061 6814 hat_pageprocess_postcallbacks(repl, HAT_POSTUNSUSPEND);
7062 6815 if (ret != 0) {
7063 6816 /*
7064 6817 * The second presuspend call failed: we got here through
7065 6818 * the suspend_fail label above.
7066 6819 */
7067 6820 ASSERT(ret != EIO);
7068 6821 PAGE_RELOCATE_LOG(target, replacement, ret, cap_cpus);
7069 6822 kreloc_thread = NULL;
7070 6823 mutex_exit(&kpr_mutex);
7071 6824 return (EAGAIN);
7072 6825 }
7073 6826
7074 6827 /*
7075 6828 * Now that we're out of the performance critical section we can
7076 6829 * take care of updating the hash table, since we still
7077 6830 * hold all the pages locked SE_EXCL at this point we
7078 6831 * needn't worry about things changing out from under us.
7079 6832 */
7080 6833 tpp = targ;
7081 6834 rpp = repl;
7082 6835 for (i = 0; i < npages; i++, tpp++, rpp++) {
7083 6836
7084 6837 /*
7085 6838 * replace targ with replacement in page_hash table
7086 6839 */
7087 6840 targ = tpp;
7088 6841 page_relocate_hash(rpp, targ);
7089 6842
7090 6843 /*
7091 6844 * concatenate target; caller of platform_page_relocate()
7092 6845 * expects target to be concatenated after returning.
7093 6846 */
7094 6847 ASSERT(targ->p_next == targ);
7095 6848 ASSERT(targ->p_prev == targ);
7096 6849 page_list_concat(&pl, &targ);
7097 6850 }
7098 6851
7099 6852 ASSERT(*target == pl);
7100 6853 *nrelocp = npages;
7101 6854 PAGE_RELOCATE_LOG(target, replacement, 0, cap_cpus);
7102 6855 kreloc_thread = NULL;
7103 6856 mutex_exit(&kpr_mutex);
7104 6857 return (0);
7105 6858 }
7106 6859
7107 6860 /*
7108 6861 * Called when stray pa_hments are found attached to a page which is
7109 6862 * being freed. Notify the subsystem which attached the pa_hment of
7110 6863 * the error if it registered a suitable handler, else panic.
7111 6864 */
7112 6865 static void
7113 6866 sfmmu_pahment_leaked(struct pa_hment *pahmep)
7114 6867 {
7115 6868 id_t cb_id = pahmep->cb_id;
7116 6869
7117 6870 ASSERT(cb_id >= (id_t)0 && cb_id < sfmmu_cb_nextid);
7118 6871 if (sfmmu_cb_table[cb_id].errhandler != NULL) {
7119 6872 if (sfmmu_cb_table[cb_id].errhandler(pahmep->addr, pahmep->len,
7120 6873 HAT_CB_ERR_LEAKED, pahmep->pvt) == 0)
7121 6874 return; /* non-fatal */
7122 6875 }
7123 6876 panic("pa_hment leaked: 0x%p", (void *)pahmep);
7124 6877 }
7125 6878
7126 6879 /*
7127 6880 * Remove all mappings to page 'pp'.
7128 6881 */
7129 6882 int
7130 6883 hat_pageunload(struct page *pp, uint_t forceflag)
↓ open down ↓ |
786 lines elided |
↑ open up ↑ |
7131 6884 {
7132 6885 struct page *origpp = pp;
7133 6886 struct sf_hment *sfhme, *tmphme;
7134 6887 struct hme_blk *hmeblkp;
7135 6888 kmutex_t *pml;
7136 6889 #ifdef VAC
7137 6890 kmutex_t *pmtx;
7138 6891 #endif
7139 6892 cpuset_t cpuset, tset;
7140 6893 int index, cons;
7141 - int xhme_blks;
7142 6894 int pa_hments;
7143 6895
7144 6896 ASSERT(PAGE_EXCL(pp));
7145 6897
7146 -retry_xhat:
7147 6898 tmphme = NULL;
7148 - xhme_blks = 0;
7149 6899 pa_hments = 0;
7150 6900 CPUSET_ZERO(cpuset);
7151 6901
7152 6902 pml = sfmmu_mlist_enter(pp);
7153 6903
7154 6904 #ifdef VAC
7155 6905 if (pp->p_kpmref)
7156 6906 sfmmu_kpm_pageunload(pp);
7157 6907 ASSERT(!PP_ISMAPPED_KPM(pp));
7158 6908 #endif
7159 6909 /*
7160 6910 * Clear vpm reference. Since the page is exclusively locked
7161 6911 * vpm cannot be referencing it.
7162 6912 */
7163 6913 if (vpm_enable) {
7164 6914 pp->p_vpmref = 0;
7165 6915 }
7166 6916
7167 6917 index = PP_MAPINDEX(pp);
7168 6918 cons = TTE8K;
7169 6919 retry:
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
7170 6920 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) {
7171 6921 tmphme = sfhme->hme_next;
7172 6922
7173 6923 if (IS_PAHME(sfhme)) {
7174 6924 ASSERT(sfhme->hme_data != NULL);
7175 6925 pa_hments++;
7176 6926 continue;
7177 6927 }
7178 6928
7179 6929 hmeblkp = sfmmu_hmetohblk(sfhme);
7180 - if (hmeblkp->hblk_xhat_bit) {
7181 - struct xhat_hme_blk *xblk =
7182 - (struct xhat_hme_blk *)hmeblkp;
7183 -
7184 - (void) XHAT_PAGEUNLOAD(xblk->xhat_hme_blk_hat,
7185 - pp, forceflag, XBLK2PROVBLK(xblk));
7186 -
7187 - xhme_blks = 1;
7188 - continue;
7189 - }
7190 6930
7191 6931 /*
7192 6932 * If there are kernel mappings don't unload them, they will
7193 6933 * be suspended.
7194 6934 */
7195 6935 if (forceflag == SFMMU_KERNEL_RELOC && hmeblkp->hblk_lckcnt &&
7196 6936 hmeblkp->hblk_tag.htag_id == ksfmmup)
7197 6937 continue;
7198 6938
7199 6939 tset = sfmmu_pageunload(pp, sfhme, cons);
7200 6940 CPUSET_OR(cpuset, tset);
7201 6941 }
7202 6942
7203 6943 while (index != 0) {
7204 6944 index = index >> 1;
7205 6945 if (index != 0)
7206 6946 cons++;
7207 6947 if (index & 0x1) {
7208 6948 /* Go to leading page */
7209 6949 pp = PP_GROUPLEADER(pp, cons);
7210 6950 ASSERT(sfmmu_mlist_held(pp));
7211 6951 goto retry;
7212 6952 }
7213 6953 }
7214 6954
7215 6955 /*
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
7216 6956 * cpuset may be empty if the page was only mapped by segkpm,
7217 6957 * in which case we won't actually cross-trap.
7218 6958 */
7219 6959 xt_sync(cpuset);
7220 6960
7221 6961 /*
7222 6962 * The page should have no mappings at this point, unless
7223 6963 * we were called from hat_page_relocate() in which case we
7224 6964 * leave the locked mappings which will be suspended later.
7225 6965 */
7226 - ASSERT(!PP_ISMAPPED(origpp) || xhme_blks || pa_hments ||
6966 + ASSERT(!PP_ISMAPPED(origpp) || pa_hments ||
7227 6967 (forceflag == SFMMU_KERNEL_RELOC));
7228 6968
7229 6969 #ifdef VAC
7230 6970 if (PP_ISTNC(pp)) {
7231 6971 if (cons == TTE8K) {
7232 6972 pmtx = sfmmu_page_enter(pp);
7233 6973 PP_CLRTNC(pp);
7234 6974 sfmmu_page_exit(pmtx);
7235 6975 } else {
7236 6976 conv_tnc(pp, cons);
7237 6977 }
7238 6978 }
7239 6979 #endif /* VAC */
7240 6980
7241 6981 if (pa_hments && forceflag != SFMMU_KERNEL_RELOC) {
7242 6982 /*
7243 6983 * Unlink any pa_hments and free them, calling back
7244 6984 * the responsible subsystem to notify it of the error.
7245 6985 * This can occur in situations such as drivers leaking
7246 6986 * DMA handles: naughty, but common enough that we'd like
7247 6987 * to keep the system running rather than bringing it
7248 6988 * down with an obscure error like "pa_hment leaked"
7249 6989 * which doesn't aid the user in debugging their driver.
7250 6990 */
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
7251 6991 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) {
7252 6992 tmphme = sfhme->hme_next;
7253 6993 if (IS_PAHME(sfhme)) {
7254 6994 struct pa_hment *pahmep = sfhme->hme_data;
7255 6995 sfmmu_pahment_leaked(pahmep);
7256 6996 HME_SUB(sfhme, pp);
7257 6997 kmem_cache_free(pa_hment_cache, pahmep);
7258 6998 }
7259 6999 }
7260 7000
7261 - ASSERT(!PP_ISMAPPED(origpp) || xhme_blks);
7001 + ASSERT(!PP_ISMAPPED(origpp));
7262 7002 }
7263 7003
7264 7004 sfmmu_mlist_exit(pml);
7265 7005
7266 - /*
7267 - * XHAT may not have finished unloading pages
7268 - * because some other thread was waiting for
7269 - * mlist lock and XHAT_PAGEUNLOAD let it do
7270 - * the job.
7271 - */
7272 - if (xhme_blks) {
7273 - pp = origpp;
7274 - goto retry_xhat;
7275 - }
7276 -
7277 7006 return (0);
7278 7007 }
7279 7008
7280 7009 cpuset_t
7281 7010 sfmmu_pageunload(page_t *pp, struct sf_hment *sfhme, int cons)
7282 7011 {
7283 7012 struct hme_blk *hmeblkp;
7284 7013 sfmmu_t *sfmmup;
7285 7014 tte_t tte, ttemod;
7286 7015 #ifdef DEBUG
7287 7016 tte_t orig_old;
7288 7017 #endif /* DEBUG */
7289 7018 caddr_t addr;
7290 7019 int ttesz;
7291 7020 int ret;
7292 7021 cpuset_t cpuset;
7293 7022
7294 7023 ASSERT(pp != NULL);
7295 7024 ASSERT(sfmmu_mlist_held(pp));
7296 7025 ASSERT(!PP_ISKAS(pp));
7297 7026
7298 7027 CPUSET_ZERO(cpuset);
7299 7028
7300 7029 hmeblkp = sfmmu_hmetohblk(sfhme);
7301 7030
7302 7031 readtte:
7303 7032 sfmmu_copytte(&sfhme->hme_tte, &tte);
7304 7033 if (TTE_IS_VALID(&tte)) {
7305 7034 sfmmup = hblktosfmmu(hmeblkp);
7306 7035 ttesz = get_hblk_ttesz(hmeblkp);
7307 7036 /*
7308 7037 * Only unload mappings of 'cons' size.
7309 7038 */
7310 7039 if (ttesz != cons)
7311 7040 return (cpuset);
7312 7041
7313 7042 /*
7314 7043 * Note that we have p_mapping lock, but no hash lock here.
7315 7044 * hblk_unload() has to have both hash lock AND p_mapping
7316 7045 * lock before it tries to modify tte. So, the tte could
7317 7046 * not become invalid in the sfmmu_modifytte_try() below.
7318 7047 */
7319 7048 ttemod = tte;
7320 7049 #ifdef DEBUG
7321 7050 orig_old = tte;
7322 7051 #endif /* DEBUG */
7323 7052
7324 7053 TTE_SET_INVALID(&ttemod);
7325 7054 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte);
7326 7055 if (ret < 0) {
7327 7056 #ifdef DEBUG
7328 7057 /* only R/M bits can change. */
7329 7058 chk_tte(&orig_old, &tte, &ttemod, hmeblkp);
7330 7059 #endif /* DEBUG */
7331 7060 goto readtte;
7332 7061 }
7333 7062
7334 7063 if (ret == 0) {
7335 7064 panic("pageunload: cas failed?");
7336 7065 }
7337 7066
7338 7067 addr = tte_to_vaddr(hmeblkp, tte);
7339 7068
7340 7069 if (hmeblkp->hblk_shared) {
7341 7070 sf_srd_t *srdp = (sf_srd_t *)sfmmup;
7342 7071 uint_t rid = hmeblkp->hblk_tag.htag_rid;
7343 7072 sf_region_t *rgnp;
7344 7073 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
7345 7074 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
7346 7075 ASSERT(srdp != NULL);
7347 7076 rgnp = srdp->srd_hmergnp[rid];
7348 7077 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid);
7349 7078 cpuset = sfmmu_rgntlb_demap(addr, rgnp, hmeblkp, 1);
7350 7079 sfmmu_ttesync(NULL, addr, &tte, pp);
7351 7080 ASSERT(rgnp->rgn_ttecnt[ttesz] > 0);
7352 7081 atomic_dec_ulong(&rgnp->rgn_ttecnt[ttesz]);
7353 7082 } else {
7354 7083 sfmmu_ttesync(sfmmup, addr, &tte, pp);
7355 7084 atomic_dec_ulong(&sfmmup->sfmmu_ttecnt[ttesz]);
7356 7085
7357 7086 /*
7358 7087 * We need to flush the page from the virtual cache
7359 7088 * in order to prevent a virtual cache alias
7360 7089 * inconsistency. The particular scenario we need
7361 7090 * to worry about is:
7362 7091 * Given: va1 and va2 are two virtual address that
7363 7092 * alias and will map the same physical address.
7364 7093 * 1. mapping exists from va1 to pa and data has
7365 7094 * been read into the cache.
7366 7095 * 2. unload va1.
7367 7096 * 3. load va2 and modify data using va2.
7368 7097 * 4 unload va2.
7369 7098 * 5. load va1 and reference data. Unless we flush
7370 7099 * the data cache when we unload we will get
7371 7100 * stale data.
7372 7101 * This scenario is taken care of by using virtual
7373 7102 * page coloring.
7374 7103 */
7375 7104 if (sfmmup->sfmmu_ismhat) {
7376 7105 /*
7377 7106 * Flush TSBs, TLBs and caches
7378 7107 * of every process
7379 7108 * sharing this ism segment.
7380 7109 */
7381 7110 sfmmu_hat_lock_all();
7382 7111 mutex_enter(&ism_mlist_lock);
7383 7112 kpreempt_disable();
7384 7113 sfmmu_ismtlbcache_demap(addr, sfmmup, hmeblkp,
7385 7114 pp->p_pagenum, CACHE_NO_FLUSH);
7386 7115 kpreempt_enable();
7387 7116 mutex_exit(&ism_mlist_lock);
7388 7117 sfmmu_hat_unlock_all();
7389 7118 cpuset = cpu_ready_set;
7390 7119 } else {
7391 7120 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0);
7392 7121 cpuset = sfmmup->sfmmu_cpusran;
7393 7122 }
7394 7123 }
7395 7124
7396 7125 /*
7397 7126 * Hme_sub has to run after ttesync() and a_rss update.
7398 7127 * See hblk_unload().
7399 7128 */
7400 7129 HME_SUB(sfhme, pp);
7401 7130 membar_stst();
7402 7131
7403 7132 /*
7404 7133 * We can not make ASSERT(hmeblkp->hblk_hmecnt <= NHMENTS)
7405 7134 * since pteload may have done a HME_ADD() right after
7406 7135 * we did the HME_SUB() above. Hmecnt is now maintained
7407 7136 * by cas only. no lock guranteed its value. The only
7408 7137 * gurantee we have is the hmecnt should not be less than
7409 7138 * what it should be so the hblk will not be taken away.
7410 7139 * It's also important that we decremented the hmecnt after
7411 7140 * we are done with hmeblkp so that this hmeblk won't be
7412 7141 * stolen.
7413 7142 */
7414 7143 ASSERT(hmeblkp->hblk_hmecnt > 0);
7415 7144 ASSERT(hmeblkp->hblk_vcnt > 0);
7416 7145 atomic_dec_16(&hmeblkp->hblk_vcnt);
7417 7146 atomic_dec_16(&hmeblkp->hblk_hmecnt);
7418 7147 /*
7419 7148 * This is bug 4063182.
7420 7149 * XXX: fixme
7421 7150 * ASSERT(hmeblkp->hblk_hmecnt || hmeblkp->hblk_vcnt ||
7422 7151 * !hmeblkp->hblk_lckcnt);
7423 7152 */
7424 7153 } else {
7425 7154 panic("invalid tte? pp %p &tte %p",
7426 7155 (void *)pp, (void *)&tte);
7427 7156 }
7428 7157
7429 7158 return (cpuset);
7430 7159 }
7431 7160
7432 7161 /*
7433 7162 * While relocating a kernel page, this function will move the mappings
7434 7163 * from tpp to dpp and modify any associated data with these mappings.
7435 7164 * It also unsuspends the suspended kernel mapping.
7436 7165 */
7437 7166 static void
7438 7167 hat_pagereload(struct page *tpp, struct page *dpp)
7439 7168 {
7440 7169 struct sf_hment *sfhme;
7441 7170 tte_t tte, ttemod;
7442 7171 int index, cons;
7443 7172
7444 7173 ASSERT(getpil() == PIL_MAX);
7445 7174 ASSERT(sfmmu_mlist_held(tpp));
7446 7175 ASSERT(sfmmu_mlist_held(dpp));
7447 7176
7448 7177 index = PP_MAPINDEX(tpp);
7449 7178 cons = TTE8K;
7450 7179
7451 7180 /* Update real mappings to the page */
7452 7181 retry:
7453 7182 for (sfhme = tpp->p_mapping; sfhme != NULL; sfhme = sfhme->hme_next) {
7454 7183 if (IS_PAHME(sfhme))
7455 7184 continue;
7456 7185 sfmmu_copytte(&sfhme->hme_tte, &tte);
7457 7186 ttemod = tte;
7458 7187
7459 7188 /*
7460 7189 * replace old pfn with new pfn in TTE
7461 7190 */
7462 7191 PFN_TO_TTE(ttemod, dpp->p_pagenum);
7463 7192
7464 7193 /*
7465 7194 * clear suspend bit
7466 7195 */
7467 7196 ASSERT(TTE_IS_SUSPEND(&ttemod));
7468 7197 TTE_CLR_SUSPEND(&ttemod);
7469 7198
7470 7199 if (sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte) < 0)
7471 7200 panic("hat_pagereload(): sfmmu_modifytte_try() failed");
7472 7201
7473 7202 /*
7474 7203 * set hme_page point to new page
7475 7204 */
7476 7205 sfhme->hme_page = dpp;
7477 7206 }
7478 7207
7479 7208 /*
7480 7209 * move p_mapping list from old page to new page
7481 7210 */
7482 7211 dpp->p_mapping = tpp->p_mapping;
7483 7212 tpp->p_mapping = NULL;
7484 7213 dpp->p_share = tpp->p_share;
7485 7214 tpp->p_share = 0;
7486 7215
7487 7216 while (index != 0) {
7488 7217 index = index >> 1;
7489 7218 if (index != 0)
7490 7219 cons++;
7491 7220 if (index & 0x1) {
7492 7221 tpp = PP_GROUPLEADER(tpp, cons);
7493 7222 dpp = PP_GROUPLEADER(dpp, cons);
7494 7223 goto retry;
7495 7224 }
7496 7225 }
7497 7226
7498 7227 curthread->t_flag &= ~T_DONTDTRACE;
7499 7228 mutex_exit(&kpr_suspendlock);
7500 7229 }
7501 7230
7502 7231 uint_t
7503 7232 hat_pagesync(struct page *pp, uint_t clearflag)
7504 7233 {
7505 7234 struct sf_hment *sfhme, *tmphme = NULL;
7506 7235 struct hme_blk *hmeblkp;
7507 7236 kmutex_t *pml;
7508 7237 cpuset_t cpuset, tset;
7509 7238 int index, cons;
7510 7239 extern ulong_t po_share;
7511 7240 page_t *save_pp = pp;
7512 7241 int stop_on_sh = 0;
7513 7242 uint_t shcnt;
7514 7243
7515 7244 CPUSET_ZERO(cpuset);
7516 7245
7517 7246 if (PP_ISRO(pp) && (clearflag & HAT_SYNC_STOPON_MOD)) {
7518 7247 return (PP_GENERIC_ATTR(pp));
7519 7248 }
7520 7249
7521 7250 if ((clearflag & HAT_SYNC_ZERORM) == 0) {
7522 7251 if ((clearflag & HAT_SYNC_STOPON_REF) && PP_ISREF(pp)) {
7523 7252 return (PP_GENERIC_ATTR(pp));
7524 7253 }
7525 7254 if ((clearflag & HAT_SYNC_STOPON_MOD) && PP_ISMOD(pp)) {
7526 7255 return (PP_GENERIC_ATTR(pp));
7527 7256 }
7528 7257 if (clearflag & HAT_SYNC_STOPON_SHARED) {
7529 7258 if (pp->p_share > po_share) {
7530 7259 hat_page_setattr(pp, P_REF);
7531 7260 return (PP_GENERIC_ATTR(pp));
7532 7261 }
7533 7262 stop_on_sh = 1;
7534 7263 shcnt = 0;
7535 7264 }
7536 7265 }
7537 7266
7538 7267 clearflag &= ~HAT_SYNC_STOPON_SHARED;
7539 7268 pml = sfmmu_mlist_enter(pp);
7540 7269 index = PP_MAPINDEX(pp);
7541 7270 cons = TTE8K;
7542 7271 retry:
7543 7272 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) {
7544 7273 /*
7545 7274 * We need to save the next hment on the list since
7546 7275 * it is possible for pagesync to remove an invalid hment
↓ open down ↓ |
260 lines elided |
↑ open up ↑ |
7547 7276 * from the list.
7548 7277 */
7549 7278 tmphme = sfhme->hme_next;
7550 7279 if (IS_PAHME(sfhme))
7551 7280 continue;
7552 7281 /*
7553 7282 * If we are looking for large mappings and this hme doesn't
7554 7283 * reach the range we are seeking, just ignore it.
7555 7284 */
7556 7285 hmeblkp = sfmmu_hmetohblk(sfhme);
7557 - if (hmeblkp->hblk_xhat_bit)
7558 - continue;
7559 7286
7560 7287 if (hme_size(sfhme) < cons)
7561 7288 continue;
7562 7289
7563 7290 if (stop_on_sh) {
7564 7291 if (hmeblkp->hblk_shared) {
7565 7292 sf_srd_t *srdp = hblktosrd(hmeblkp);
7566 7293 uint_t rid = hmeblkp->hblk_tag.htag_rid;
7567 7294 sf_region_t *rgnp;
7568 7295 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
7569 7296 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
7570 7297 ASSERT(srdp != NULL);
7571 7298 rgnp = srdp->srd_hmergnp[rid];
7572 7299 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp,
7573 7300 rgnp, rid);
7574 7301 shcnt += rgnp->rgn_refcnt;
7575 7302 } else {
7576 7303 shcnt++;
7577 7304 }
7578 7305 if (shcnt > po_share) {
7579 7306 /*
7580 7307 * tell the pager to spare the page this time
7581 7308 * around.
7582 7309 */
7583 7310 hat_page_setattr(save_pp, P_REF);
7584 7311 index = 0;
7585 7312 break;
7586 7313 }
7587 7314 }
7588 7315 tset = sfmmu_pagesync(pp, sfhme,
7589 7316 clearflag & ~HAT_SYNC_STOPON_RM);
7590 7317 CPUSET_OR(cpuset, tset);
7591 7318
7592 7319 /*
7593 7320 * If clearflag is HAT_SYNC_DONTZERO, break out as soon
7594 7321 * as the "ref" or "mod" is set or share cnt exceeds po_share.
7595 7322 */
7596 7323 if ((clearflag & ~HAT_SYNC_STOPON_RM) == HAT_SYNC_DONTZERO &&
7597 7324 (((clearflag & HAT_SYNC_STOPON_MOD) && PP_ISMOD(save_pp)) ||
7598 7325 ((clearflag & HAT_SYNC_STOPON_REF) && PP_ISREF(save_pp)))) {
7599 7326 index = 0;
7600 7327 break;
7601 7328 }
7602 7329 }
7603 7330
7604 7331 while (index) {
7605 7332 index = index >> 1;
7606 7333 cons++;
7607 7334 if (index & 0x1) {
7608 7335 /* Go to leading page */
7609 7336 pp = PP_GROUPLEADER(pp, cons);
7610 7337 goto retry;
7611 7338 }
7612 7339 }
7613 7340
7614 7341 xt_sync(cpuset);
7615 7342 sfmmu_mlist_exit(pml);
7616 7343 return (PP_GENERIC_ATTR(save_pp));
7617 7344 }
7618 7345
7619 7346 /*
7620 7347 * Get all the hardware dependent attributes for a page struct
7621 7348 */
7622 7349 static cpuset_t
7623 7350 sfmmu_pagesync(struct page *pp, struct sf_hment *sfhme,
7624 7351 uint_t clearflag)
7625 7352 {
7626 7353 caddr_t addr;
7627 7354 tte_t tte, ttemod;
7628 7355 struct hme_blk *hmeblkp;
7629 7356 int ret;
7630 7357 sfmmu_t *sfmmup;
7631 7358 cpuset_t cpuset;
7632 7359
7633 7360 ASSERT(pp != NULL);
7634 7361 ASSERT(sfmmu_mlist_held(pp));
7635 7362 ASSERT((clearflag == HAT_SYNC_DONTZERO) ||
7636 7363 (clearflag == HAT_SYNC_ZERORM));
7637 7364
7638 7365 SFMMU_STAT(sf_pagesync);
7639 7366
7640 7367 CPUSET_ZERO(cpuset);
7641 7368
7642 7369 sfmmu_pagesync_retry:
7643 7370
7644 7371 sfmmu_copytte(&sfhme->hme_tte, &tte);
7645 7372 if (TTE_IS_VALID(&tte)) {
7646 7373 hmeblkp = sfmmu_hmetohblk(sfhme);
7647 7374 sfmmup = hblktosfmmu(hmeblkp);
7648 7375 addr = tte_to_vaddr(hmeblkp, tte);
7649 7376 if (clearflag == HAT_SYNC_ZERORM) {
7650 7377 ttemod = tte;
7651 7378 TTE_CLR_RM(&ttemod);
7652 7379 ret = sfmmu_modifytte_try(&tte, &ttemod,
7653 7380 &sfhme->hme_tte);
7654 7381 if (ret < 0) {
7655 7382 /*
7656 7383 * cas failed and the new value is not what
7657 7384 * we want.
7658 7385 */
7659 7386 goto sfmmu_pagesync_retry;
7660 7387 }
7661 7388
7662 7389 if (ret > 0) {
7663 7390 /* we win the cas */
7664 7391 if (hmeblkp->hblk_shared) {
7665 7392 sf_srd_t *srdp = (sf_srd_t *)sfmmup;
7666 7393 uint_t rid =
7667 7394 hmeblkp->hblk_tag.htag_rid;
7668 7395 sf_region_t *rgnp;
7669 7396 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
7670 7397 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
7671 7398 ASSERT(srdp != NULL);
7672 7399 rgnp = srdp->srd_hmergnp[rid];
7673 7400 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp,
7674 7401 srdp, rgnp, rid);
7675 7402 cpuset = sfmmu_rgntlb_demap(addr,
7676 7403 rgnp, hmeblkp, 1);
7677 7404 } else {
7678 7405 sfmmu_tlb_demap(addr, sfmmup, hmeblkp,
7679 7406 0, 0);
7680 7407 cpuset = sfmmup->sfmmu_cpusran;
7681 7408 }
7682 7409 }
7683 7410 }
7684 7411 sfmmu_ttesync(hmeblkp->hblk_shared ? NULL : sfmmup, addr,
7685 7412 &tte, pp);
7686 7413 }
7687 7414 return (cpuset);
7688 7415 }
7689 7416
7690 7417 /*
7691 7418 * Remove write permission from a mappings to a page, so that
7692 7419 * we can detect the next modification of it. This requires modifying
7693 7420 * the TTE then invalidating (demap) any TLB entry using that TTE.
7694 7421 * This code is similar to sfmmu_pagesync().
7695 7422 */
7696 7423 static cpuset_t
7697 7424 sfmmu_pageclrwrt(struct page *pp, struct sf_hment *sfhme)
7698 7425 {
7699 7426 caddr_t addr;
7700 7427 tte_t tte;
7701 7428 tte_t ttemod;
7702 7429 struct hme_blk *hmeblkp;
7703 7430 int ret;
7704 7431 sfmmu_t *sfmmup;
7705 7432 cpuset_t cpuset;
7706 7433
7707 7434 ASSERT(pp != NULL);
↓ open down ↓ |
139 lines elided |
↑ open up ↑ |
7708 7435 ASSERT(sfmmu_mlist_held(pp));
7709 7436
7710 7437 CPUSET_ZERO(cpuset);
7711 7438 SFMMU_STAT(sf_clrwrt);
7712 7439
7713 7440 retry:
7714 7441
7715 7442 sfmmu_copytte(&sfhme->hme_tte, &tte);
7716 7443 if (TTE_IS_VALID(&tte) && TTE_IS_WRITABLE(&tte)) {
7717 7444 hmeblkp = sfmmu_hmetohblk(sfhme);
7718 -
7719 - /*
7720 - * xhat mappings should never be to a VMODSORT page.
7721 - */
7722 - ASSERT(hmeblkp->hblk_xhat_bit == 0);
7723 -
7724 7445 sfmmup = hblktosfmmu(hmeblkp);
7725 7446 addr = tte_to_vaddr(hmeblkp, tte);
7726 7447
7727 7448 ttemod = tte;
7728 7449 TTE_CLR_WRT(&ttemod);
7729 7450 TTE_CLR_MOD(&ttemod);
7730 7451 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte);
7731 7452
7732 7453 /*
7733 7454 * if cas failed and the new value is not what
7734 7455 * we want retry
7735 7456 */
7736 7457 if (ret < 0)
7737 7458 goto retry;
7738 7459
7739 7460 /* we win the cas */
7740 7461 if (ret > 0) {
7741 7462 if (hmeblkp->hblk_shared) {
7742 7463 sf_srd_t *srdp = (sf_srd_t *)sfmmup;
7743 7464 uint_t rid = hmeblkp->hblk_tag.htag_rid;
7744 7465 sf_region_t *rgnp;
7745 7466 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
7746 7467 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
7747 7468 ASSERT(srdp != NULL);
7748 7469 rgnp = srdp->srd_hmergnp[rid];
7749 7470 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp,
7750 7471 srdp, rgnp, rid);
7751 7472 cpuset = sfmmu_rgntlb_demap(addr,
7752 7473 rgnp, hmeblkp, 1);
7753 7474 } else {
7754 7475 sfmmu_tlb_demap(addr, sfmmup, hmeblkp, 0, 0);
7755 7476 cpuset = sfmmup->sfmmu_cpusran;
7756 7477 }
7757 7478 }
7758 7479 }
7759 7480
7760 7481 return (cpuset);
7761 7482 }
7762 7483
7763 7484 /*
7764 7485 * Walk all mappings of a page, removing write permission and clearing the
7765 7486 * ref/mod bits. This code is similar to hat_pagesync()
7766 7487 */
7767 7488 static void
7768 7489 hat_page_clrwrt(page_t *pp)
7769 7490 {
7770 7491 struct sf_hment *sfhme;
7771 7492 struct sf_hment *tmphme = NULL;
7772 7493 kmutex_t *pml;
7773 7494 cpuset_t cpuset;
7774 7495 cpuset_t tset;
7775 7496 int index;
7776 7497 int cons;
7777 7498
7778 7499 CPUSET_ZERO(cpuset);
7779 7500
7780 7501 pml = sfmmu_mlist_enter(pp);
7781 7502 index = PP_MAPINDEX(pp);
7782 7503 cons = TTE8K;
7783 7504 retry:
7784 7505 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) {
7785 7506 tmphme = sfhme->hme_next;
7786 7507
7787 7508 /*
7788 7509 * If we are looking for large mappings and this hme doesn't
7789 7510 * reach the range we are seeking, just ignore its.
7790 7511 */
7791 7512
7792 7513 if (hme_size(sfhme) < cons)
7793 7514 continue;
7794 7515
7795 7516 tset = sfmmu_pageclrwrt(pp, sfhme);
7796 7517 CPUSET_OR(cpuset, tset);
7797 7518 }
7798 7519
7799 7520 while (index) {
7800 7521 index = index >> 1;
7801 7522 cons++;
7802 7523 if (index & 0x1) {
7803 7524 /* Go to leading page */
7804 7525 pp = PP_GROUPLEADER(pp, cons);
7805 7526 goto retry;
7806 7527 }
7807 7528 }
7808 7529
7809 7530 xt_sync(cpuset);
7810 7531 sfmmu_mlist_exit(pml);
7811 7532 }
7812 7533
7813 7534 /*
7814 7535 * Set the given REF/MOD/RO bits for the given page.
7815 7536 * For a vnode with a sorted v_pages list, we need to change
7816 7537 * the attributes and the v_pages list together under page_vnode_mutex.
7817 7538 */
7818 7539 void
7819 7540 hat_page_setattr(page_t *pp, uint_t flag)
7820 7541 {
7821 7542 vnode_t *vp = pp->p_vnode;
7822 7543 page_t **listp;
7823 7544 kmutex_t *pmtx;
7824 7545 kmutex_t *vphm = NULL;
7825 7546 int noshuffle;
7826 7547
7827 7548 noshuffle = flag & P_NSH;
7828 7549 flag &= ~P_NSH;
7829 7550
7830 7551 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO)));
7831 7552
7832 7553 /*
7833 7554 * nothing to do if attribute already set
7834 7555 */
7835 7556 if ((pp->p_nrm & flag) == flag)
7836 7557 return;
7837 7558
7838 7559 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp) &&
7839 7560 !noshuffle) {
7840 7561 vphm = page_vnode_mutex(vp);
7841 7562 mutex_enter(vphm);
7842 7563 }
7843 7564
7844 7565 pmtx = sfmmu_page_enter(pp);
7845 7566 pp->p_nrm |= flag;
7846 7567 sfmmu_page_exit(pmtx);
7847 7568
7848 7569 if (vphm != NULL) {
7849 7570 /*
7850 7571 * Some File Systems examine v_pages for NULL w/o
7851 7572 * grabbing the vphm mutex. Must not let it become NULL when
7852 7573 * pp is the only page on the list.
7853 7574 */
7854 7575 if (pp->p_vpnext != pp) {
7855 7576 page_vpsub(&vp->v_pages, pp);
7856 7577 if (vp->v_pages != NULL)
7857 7578 listp = &vp->v_pages->p_vpprev->p_vpnext;
7858 7579 else
7859 7580 listp = &vp->v_pages;
7860 7581 page_vpadd(listp, pp);
7861 7582 }
7862 7583 mutex_exit(vphm);
7863 7584 }
7864 7585 }
7865 7586
7866 7587 void
7867 7588 hat_page_clrattr(page_t *pp, uint_t flag)
7868 7589 {
7869 7590 vnode_t *vp = pp->p_vnode;
7870 7591 kmutex_t *pmtx;
7871 7592
7872 7593 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO)));
7873 7594
7874 7595 pmtx = sfmmu_page_enter(pp);
7875 7596
7876 7597 /*
7877 7598 * Caller is expected to hold page's io lock for VMODSORT to work
7878 7599 * correctly with pvn_vplist_dirty() and pvn_getdirty() when mod
7879 7600 * bit is cleared.
7880 7601 * We don't have assert to avoid tripping some existing third party
7881 7602 * code. The dirty page is moved back to top of the v_page list
7882 7603 * after IO is done in pvn_write_done().
7883 7604 */
7884 7605 pp->p_nrm &= ~flag;
7885 7606 sfmmu_page_exit(pmtx);
7886 7607
7887 7608 if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp)) {
7888 7609
7889 7610 /*
7890 7611 * VMODSORT works by removing write permissions and getting
7891 7612 * a fault when a page is made dirty. At this point
7892 7613 * we need to remove write permission from all mappings
7893 7614 * to this page.
7894 7615 */
7895 7616 hat_page_clrwrt(pp);
7896 7617 }
7897 7618 }
7898 7619
7899 7620 uint_t
7900 7621 hat_page_getattr(page_t *pp, uint_t flag)
7901 7622 {
7902 7623 ASSERT(!(flag & ~(P_MOD | P_REF | P_RO)));
7903 7624 return ((uint_t)(pp->p_nrm & flag));
7904 7625 }
7905 7626
7906 7627 /*
7907 7628 * DEBUG kernels: verify that a kernel va<->pa translation
7908 7629 * is safe by checking the underlying page_t is in a page
7909 7630 * relocation-safe state.
7910 7631 */
7911 7632 #ifdef DEBUG
7912 7633 void
7913 7634 sfmmu_check_kpfn(pfn_t pfn)
7914 7635 {
7915 7636 page_t *pp;
7916 7637 int index, cons;
7917 7638
7918 7639 if (hat_check_vtop == 0)
7919 7640 return;
7920 7641
7921 7642 if (kvseg.s_base == NULL || panicstr)
7922 7643 return;
7923 7644
7924 7645 pp = page_numtopp_nolock(pfn);
7925 7646 if (!pp)
7926 7647 return;
7927 7648
7928 7649 if (PAGE_LOCKED(pp) || PP_ISNORELOC(pp))
7929 7650 return;
7930 7651
7931 7652 /*
7932 7653 * Handed a large kernel page, we dig up the root page since we
7933 7654 * know the root page might have the lock also.
7934 7655 */
7935 7656 if (pp->p_szc != 0) {
7936 7657 index = PP_MAPINDEX(pp);
7937 7658 cons = TTE8K;
7938 7659 again:
7939 7660 while (index != 0) {
7940 7661 index >>= 1;
7941 7662 if (index != 0)
7942 7663 cons++;
7943 7664 if (index & 0x1) {
7944 7665 pp = PP_GROUPLEADER(pp, cons);
7945 7666 goto again;
7946 7667 }
7947 7668 }
7948 7669 }
7949 7670
7950 7671 if (PAGE_LOCKED(pp) || PP_ISNORELOC(pp))
7951 7672 return;
7952 7673
7953 7674 /*
7954 7675 * Pages need to be locked or allocated "permanent" (either from
7955 7676 * static_arena arena or explicitly setting PG_NORELOC when calling
7956 7677 * page_create_va()) for VA->PA translations to be valid.
7957 7678 */
7958 7679 if (!PP_ISNORELOC(pp))
7959 7680 panic("Illegal VA->PA translation, pp 0x%p not permanent",
7960 7681 (void *)pp);
7961 7682 else
7962 7683 panic("Illegal VA->PA translation, pp 0x%p not locked",
7963 7684 (void *)pp);
7964 7685 }
7965 7686 #endif /* DEBUG */
7966 7687
7967 7688 /*
7968 7689 * Returns a page frame number for a given virtual address.
7969 7690 * Returns PFN_INVALID to indicate an invalid mapping
7970 7691 */
7971 7692 pfn_t
7972 7693 hat_getpfnum(struct hat *hat, caddr_t addr)
7973 7694 {
7974 7695 pfn_t pfn;
7975 7696 tte_t tte;
7976 7697
7977 7698 /*
↓ open down ↓ |
244 lines elided |
↑ open up ↑ |
7978 7699 * We would like to
7979 7700 * ASSERT(AS_LOCK_HELD(as, &as->a_lock));
7980 7701 * but we can't because the iommu driver will call this
7981 7702 * routine at interrupt time and it can't grab the as lock
7982 7703 * or it will deadlock: A thread could have the as lock
7983 7704 * and be waiting for io. The io can't complete
7984 7705 * because the interrupt thread is blocked trying to grab
7985 7706 * the as lock.
7986 7707 */
7987 7708
7988 - ASSERT(hat->sfmmu_xhat_provider == NULL);
7989 -
7990 7709 if (hat == ksfmmup) {
7991 7710 if (IS_KMEM_VA_LARGEPAGE(addr)) {
7992 7711 ASSERT(segkmem_lpszc > 0);
7993 7712 pfn = sfmmu_kvaszc2pfn(addr, segkmem_lpszc);
7994 7713 if (pfn != PFN_INVALID) {
7995 7714 sfmmu_check_kpfn(pfn);
7996 7715 return (pfn);
7997 7716 }
7998 7717 } else if (segkpm && IS_KPM_ADDR(addr)) {
7999 7718 return (sfmmu_kpm_vatopfn(addr));
8000 7719 }
8001 7720 while ((pfn = sfmmu_vatopfn(addr, ksfmmup, &tte))
8002 7721 == PFN_SUSPENDED) {
8003 7722 sfmmu_vatopfn_suspended(addr, ksfmmup, &tte);
8004 7723 }
8005 7724 sfmmu_check_kpfn(pfn);
8006 7725 return (pfn);
8007 7726 } else {
8008 7727 return (sfmmu_uvatopfn(addr, hat, NULL));
8009 7728 }
8010 7729 }
8011 7730
8012 7731 /*
8013 7732 * This routine will return both pfn and tte for the vaddr.
8014 7733 */
8015 7734 static pfn_t
8016 7735 sfmmu_uvatopfn(caddr_t vaddr, struct hat *sfmmup, tte_t *ttep)
8017 7736 {
8018 7737 struct hmehash_bucket *hmebp;
8019 7738 hmeblk_tag hblktag;
8020 7739 int hmeshift, hashno = 1;
8021 7740 struct hme_blk *hmeblkp = NULL;
8022 7741 tte_t tte;
8023 7742
8024 7743 struct sf_hment *sfhmep;
8025 7744 pfn_t pfn;
8026 7745
8027 7746 /* support for ISM */
8028 7747 ism_map_t *ism_map;
8029 7748 ism_blk_t *ism_blkp;
8030 7749 int i;
8031 7750 sfmmu_t *ism_hatid = NULL;
8032 7751 sfmmu_t *locked_hatid = NULL;
8033 7752 sfmmu_t *sv_sfmmup = sfmmup;
8034 7753 caddr_t sv_vaddr = vaddr;
8035 7754 sf_srd_t *srdp;
8036 7755
8037 7756 if (ttep == NULL) {
8038 7757 ttep = &tte;
8039 7758 } else {
8040 7759 ttep->ll = 0;
8041 7760 }
8042 7761
8043 7762 ASSERT(sfmmup != ksfmmup);
8044 7763 SFMMU_STAT(sf_user_vtop);
8045 7764 /*
8046 7765 * Set ism_hatid if vaddr falls in a ISM segment.
8047 7766 */
8048 7767 ism_blkp = sfmmup->sfmmu_iblk;
8049 7768 if (ism_blkp != NULL) {
8050 7769 sfmmu_ismhat_enter(sfmmup, 0);
8051 7770 locked_hatid = sfmmup;
8052 7771 }
8053 7772 while (ism_blkp != NULL && ism_hatid == NULL) {
8054 7773 ism_map = ism_blkp->iblk_maps;
8055 7774 for (i = 0; ism_map[i].imap_ismhat && i < ISM_MAP_SLOTS; i++) {
8056 7775 if (vaddr >= ism_start(ism_map[i]) &&
8057 7776 vaddr < ism_end(ism_map[i])) {
8058 7777 sfmmup = ism_hatid = ism_map[i].imap_ismhat;
8059 7778 vaddr = (caddr_t)(vaddr -
8060 7779 ism_start(ism_map[i]));
8061 7780 break;
8062 7781 }
8063 7782 }
8064 7783 ism_blkp = ism_blkp->iblk_next;
8065 7784 }
8066 7785 if (locked_hatid) {
8067 7786 sfmmu_ismhat_exit(locked_hatid, 0);
8068 7787 }
8069 7788
8070 7789 hblktag.htag_id = sfmmup;
8071 7790 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
8072 7791 do {
8073 7792 hmeshift = HME_HASH_SHIFT(hashno);
8074 7793 hblktag.htag_bspage = HME_HASH_BSPAGE(vaddr, hmeshift);
8075 7794 hblktag.htag_rehash = hashno;
8076 7795 hmebp = HME_HASH_FUNCTION(sfmmup, vaddr, hmeshift);
8077 7796
8078 7797 SFMMU_HASH_LOCK(hmebp);
8079 7798
8080 7799 HME_HASH_FAST_SEARCH(hmebp, hblktag, hmeblkp);
8081 7800 if (hmeblkp != NULL) {
8082 7801 ASSERT(!hmeblkp->hblk_shared);
8083 7802 HBLKTOHME(sfhmep, hmeblkp, vaddr);
8084 7803 sfmmu_copytte(&sfhmep->hme_tte, ttep);
8085 7804 SFMMU_HASH_UNLOCK(hmebp);
8086 7805 if (TTE_IS_VALID(ttep)) {
8087 7806 pfn = TTE_TO_PFN(vaddr, ttep);
8088 7807 return (pfn);
8089 7808 }
8090 7809 break;
8091 7810 }
8092 7811 SFMMU_HASH_UNLOCK(hmebp);
8093 7812 hashno++;
8094 7813 } while (HME_REHASH(sfmmup) && (hashno <= mmu_hashcnt));
8095 7814
8096 7815 if (SF_HMERGNMAP_ISNULL(sv_sfmmup)) {
8097 7816 return (PFN_INVALID);
8098 7817 }
8099 7818 srdp = sv_sfmmup->sfmmu_srdp;
8100 7819 ASSERT(srdp != NULL);
8101 7820 ASSERT(srdp->srd_refcnt != 0);
8102 7821 hblktag.htag_id = srdp;
8103 7822 hashno = 1;
8104 7823 do {
8105 7824 hmeshift = HME_HASH_SHIFT(hashno);
8106 7825 hblktag.htag_bspage = HME_HASH_BSPAGE(sv_vaddr, hmeshift);
8107 7826 hblktag.htag_rehash = hashno;
8108 7827 hmebp = HME_HASH_FUNCTION(srdp, sv_vaddr, hmeshift);
8109 7828
8110 7829 SFMMU_HASH_LOCK(hmebp);
8111 7830 for (hmeblkp = hmebp->hmeblkp; hmeblkp != NULL;
8112 7831 hmeblkp = hmeblkp->hblk_next) {
8113 7832 uint_t rid;
8114 7833 sf_region_t *rgnp;
8115 7834 caddr_t rsaddr;
8116 7835 caddr_t readdr;
8117 7836
8118 7837 if (!HTAGS_EQ_SHME(hmeblkp->hblk_tag, hblktag,
8119 7838 sv_sfmmup->sfmmu_hmeregion_map)) {
8120 7839 continue;
8121 7840 }
8122 7841 ASSERT(hmeblkp->hblk_shared);
8123 7842 rid = hmeblkp->hblk_tag.htag_rid;
8124 7843 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
8125 7844 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
8126 7845 rgnp = srdp->srd_hmergnp[rid];
8127 7846 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid);
8128 7847 HBLKTOHME(sfhmep, hmeblkp, sv_vaddr);
8129 7848 sfmmu_copytte(&sfhmep->hme_tte, ttep);
8130 7849 rsaddr = rgnp->rgn_saddr;
8131 7850 readdr = rsaddr + rgnp->rgn_size;
8132 7851 #ifdef DEBUG
8133 7852 if (TTE_IS_VALID(ttep) ||
8134 7853 get_hblk_ttesz(hmeblkp) > TTE8K) {
8135 7854 caddr_t eva = tte_to_evaddr(hmeblkp, ttep);
8136 7855 ASSERT(eva > sv_vaddr);
8137 7856 ASSERT(sv_vaddr >= rsaddr);
8138 7857 ASSERT(sv_vaddr < readdr);
8139 7858 ASSERT(eva <= readdr);
8140 7859 }
8141 7860 #endif /* DEBUG */
8142 7861 /*
8143 7862 * Continue the search if we
8144 7863 * found an invalid 8K tte outside of the area
8145 7864 * covered by this hmeblk's region.
8146 7865 */
8147 7866 if (TTE_IS_VALID(ttep)) {
8148 7867 SFMMU_HASH_UNLOCK(hmebp);
8149 7868 pfn = TTE_TO_PFN(sv_vaddr, ttep);
8150 7869 return (pfn);
8151 7870 } else if (get_hblk_ttesz(hmeblkp) > TTE8K ||
8152 7871 (sv_vaddr >= rsaddr && sv_vaddr < readdr)) {
8153 7872 SFMMU_HASH_UNLOCK(hmebp);
8154 7873 pfn = PFN_INVALID;
8155 7874 return (pfn);
8156 7875 }
8157 7876 }
8158 7877 SFMMU_HASH_UNLOCK(hmebp);
8159 7878 hashno++;
8160 7879 } while (hashno <= mmu_hashcnt);
8161 7880 return (PFN_INVALID);
8162 7881 }
↓ open down ↓ |
163 lines elided |
↑ open up ↑ |
8163 7882
8164 7883
8165 7884 /*
8166 7885 * For compatability with AT&T and later optimizations
8167 7886 */
8168 7887 /* ARGSUSED */
8169 7888 void
8170 7889 hat_map(struct hat *hat, caddr_t addr, size_t len, uint_t flags)
8171 7890 {
8172 7891 ASSERT(hat != NULL);
8173 - ASSERT(hat->sfmmu_xhat_provider == NULL);
8174 7892 }
8175 7893
8176 7894 /*
8177 7895 * Return the number of mappings to a particular page. This number is an
8178 7896 * approximation of the number of people sharing the page.
8179 7897 *
8180 7898 * shared hmeblks or ism hmeblks are counted as 1 mapping here.
8181 7899 * hat_page_checkshare() can be used to compare threshold to share
8182 7900 * count that reflects the number of region sharers albeit at higher cost.
8183 7901 */
8184 7902 ulong_t
8185 7903 hat_page_getshare(page_t *pp)
8186 7904 {
8187 7905 page_t *spp = pp; /* start page */
8188 7906 kmutex_t *pml;
8189 7907 ulong_t cnt;
8190 7908 int index, sz = TTE64K;
8191 7909
8192 7910 /*
8193 7911 * We need to grab the mlist lock to make sure any outstanding
8194 7912 * load/unloads complete. Otherwise we could return zero
8195 7913 * even though the unload(s) hasn't finished yet.
8196 7914 */
8197 7915 pml = sfmmu_mlist_enter(spp);
8198 7916 cnt = spp->p_share;
8199 7917
8200 7918 #ifdef VAC
8201 7919 if (kpm_enable)
8202 7920 cnt += spp->p_kpmref;
8203 7921 #endif
8204 7922 if (vpm_enable && pp->p_vpmref) {
8205 7923 cnt += 1;
8206 7924 }
8207 7925
8208 7926 /*
8209 7927 * If we have any large mappings, we count the number of
8210 7928 * mappings that this large page is part of.
8211 7929 */
8212 7930 index = PP_MAPINDEX(spp);
8213 7931 index >>= 1;
8214 7932 while (index) {
8215 7933 pp = PP_GROUPLEADER(spp, sz);
8216 7934 if ((index & 0x1) && pp != spp) {
8217 7935 cnt += pp->p_share;
8218 7936 spp = pp;
8219 7937 }
8220 7938 index >>= 1;
8221 7939 sz++;
8222 7940 }
8223 7941 sfmmu_mlist_exit(pml);
8224 7942 return (cnt);
8225 7943 }
8226 7944
8227 7945 /*
8228 7946 * Return 1 if the number of mappings exceeds sh_thresh. Return 0
8229 7947 * otherwise. Count shared hmeblks by region's refcnt.
8230 7948 */
8231 7949 int
8232 7950 hat_page_checkshare(page_t *pp, ulong_t sh_thresh)
8233 7951 {
8234 7952 kmutex_t *pml;
8235 7953 ulong_t cnt = 0;
8236 7954 int index, sz = TTE8K;
8237 7955 struct sf_hment *sfhme, *tmphme = NULL;
8238 7956 struct hme_blk *hmeblkp;
8239 7957
8240 7958 pml = sfmmu_mlist_enter(pp);
8241 7959
8242 7960 #ifdef VAC
8243 7961 if (kpm_enable)
8244 7962 cnt = pp->p_kpmref;
8245 7963 #endif
8246 7964
8247 7965 if (vpm_enable && pp->p_vpmref) {
8248 7966 cnt += 1;
8249 7967 }
8250 7968
8251 7969 if (pp->p_share + cnt > sh_thresh) {
8252 7970 sfmmu_mlist_exit(pml);
8253 7971 return (1);
8254 7972 }
8255 7973
↓ open down ↓ |
72 lines elided |
↑ open up ↑ |
8256 7974 index = PP_MAPINDEX(pp);
8257 7975
8258 7976 again:
8259 7977 for (sfhme = pp->p_mapping; sfhme; sfhme = tmphme) {
8260 7978 tmphme = sfhme->hme_next;
8261 7979 if (IS_PAHME(sfhme)) {
8262 7980 continue;
8263 7981 }
8264 7982
8265 7983 hmeblkp = sfmmu_hmetohblk(sfhme);
8266 - if (hmeblkp->hblk_xhat_bit) {
8267 - cnt++;
8268 - if (cnt > sh_thresh) {
8269 - sfmmu_mlist_exit(pml);
8270 - return (1);
8271 - }
8272 - continue;
8273 - }
8274 7984 if (hme_size(sfhme) != sz) {
8275 7985 continue;
8276 7986 }
8277 7987
8278 7988 if (hmeblkp->hblk_shared) {
8279 7989 sf_srd_t *srdp = hblktosrd(hmeblkp);
8280 7990 uint_t rid = hmeblkp->hblk_tag.htag_rid;
8281 7991 sf_region_t *rgnp;
8282 7992 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
8283 7993 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
8284 7994 ASSERT(srdp != NULL);
8285 7995 rgnp = srdp->srd_hmergnp[rid];
8286 7996 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp,
8287 7997 rgnp, rid);
8288 7998 cnt += rgnp->rgn_refcnt;
8289 7999 } else {
8290 8000 cnt++;
8291 8001 }
8292 8002 if (cnt > sh_thresh) {
8293 8003 sfmmu_mlist_exit(pml);
8294 8004 return (1);
8295 8005 }
8296 8006 }
8297 8007
8298 8008 index >>= 1;
8299 8009 sz++;
8300 8010 while (index) {
8301 8011 pp = PP_GROUPLEADER(pp, sz);
8302 8012 ASSERT(sfmmu_mlist_held(pp));
8303 8013 if (index & 0x1) {
8304 8014 goto again;
8305 8015 }
8306 8016 index >>= 1;
8307 8017 sz++;
8308 8018 }
8309 8019 sfmmu_mlist_exit(pml);
8310 8020 return (0);
8311 8021 }
8312 8022
8313 8023 /*
8314 8024 * Unload all large mappings to the pp and reset the p_szc field of every
8315 8025 * constituent page according to the remaining mappings.
8316 8026 *
8317 8027 * pp must be locked SE_EXCL. Even though no other constituent pages are
8318 8028 * locked it's legal to unload the large mappings to the pp because all
8319 8029 * constituent pages of large locked mappings have to be locked SE_SHARED.
8320 8030 * This means if we have SE_EXCL lock on one of constituent pages none of the
8321 8031 * large mappings to pp are locked.
8322 8032 *
8323 8033 * Decrease p_szc field starting from the last constituent page and ending
8324 8034 * with the root page. This method is used because other threads rely on the
8325 8035 * root's p_szc to find the lock to syncronize on. After a root page_t's p_szc
8326 8036 * is demoted then other threads will succeed in sfmmu_mlspl_enter(). This
8327 8037 * ensures that p_szc changes of the constituent pages appears atomic for all
8328 8038 * threads that use sfmmu_mlspl_enter() to examine p_szc field.
8329 8039 *
8330 8040 * This mechanism is only used for file system pages where it's not always
8331 8041 * possible to get SE_EXCL locks on all constituent pages to demote the size
8332 8042 * code (as is done for anonymous or kernel large pages).
8333 8043 *
8334 8044 * See more comments in front of sfmmu_mlspl_enter().
8335 8045 */
8336 8046 void
8337 8047 hat_page_demote(page_t *pp)
8338 8048 {
8339 8049 int index;
8340 8050 int sz;
8341 8051 cpuset_t cpuset;
8342 8052 int sync = 0;
8343 8053 page_t *rootpp;
8344 8054 struct sf_hment *sfhme;
8345 8055 struct sf_hment *tmphme = NULL;
8346 8056 struct hme_blk *hmeblkp;
8347 8057 uint_t pszc;
8348 8058 page_t *lastpp;
8349 8059 cpuset_t tset;
8350 8060 pgcnt_t npgs;
8351 8061 kmutex_t *pml;
8352 8062 kmutex_t *pmtx = NULL;
8353 8063
8354 8064 ASSERT(PAGE_EXCL(pp));
8355 8065 ASSERT(!PP_ISFREE(pp));
8356 8066 ASSERT(!PP_ISKAS(pp));
8357 8067 ASSERT(page_szc_lock_assert(pp));
8358 8068 pml = sfmmu_mlist_enter(pp);
8359 8069
8360 8070 pszc = pp->p_szc;
8361 8071 if (pszc == 0) {
8362 8072 goto out;
8363 8073 }
8364 8074
8365 8075 index = PP_MAPINDEX(pp) >> 1;
8366 8076
8367 8077 if (index) {
8368 8078 CPUSET_ZERO(cpuset);
8369 8079 sz = TTE64K;
8370 8080 sync = 1;
8371 8081 }
8372 8082
8373 8083 while (index) {
8374 8084 if (!(index & 0x1)) {
8375 8085 index >>= 1;
8376 8086 sz++;
8377 8087 continue;
↓ open down ↓ |
94 lines elided |
↑ open up ↑ |
8378 8088 }
8379 8089 ASSERT(sz <= pszc);
8380 8090 rootpp = PP_GROUPLEADER(pp, sz);
8381 8091 for (sfhme = rootpp->p_mapping; sfhme; sfhme = tmphme) {
8382 8092 tmphme = sfhme->hme_next;
8383 8093 ASSERT(!IS_PAHME(sfhme));
8384 8094 hmeblkp = sfmmu_hmetohblk(sfhme);
8385 8095 if (hme_size(sfhme) != sz) {
8386 8096 continue;
8387 8097 }
8388 - if (hmeblkp->hblk_xhat_bit) {
8389 - cmn_err(CE_PANIC,
8390 - "hat_page_demote: xhat hmeblk");
8391 - }
8392 8098 tset = sfmmu_pageunload(rootpp, sfhme, sz);
8393 8099 CPUSET_OR(cpuset, tset);
8394 8100 }
8395 8101 if (index >>= 1) {
8396 8102 sz++;
8397 8103 }
8398 8104 }
8399 8105
8400 8106 ASSERT(!PP_ISMAPPED_LARGE(pp));
8401 8107
8402 8108 if (sync) {
8403 8109 xt_sync(cpuset);
8404 8110 #ifdef VAC
8405 8111 if (PP_ISTNC(pp)) {
8406 8112 conv_tnc(rootpp, sz);
8407 8113 }
8408 8114 #endif /* VAC */
8409 8115 }
8410 8116
8411 8117 pmtx = sfmmu_page_enter(pp);
8412 8118
8413 8119 ASSERT(pp->p_szc == pszc);
8414 8120 rootpp = PP_PAGEROOT(pp);
8415 8121 ASSERT(rootpp->p_szc == pszc);
8416 8122 lastpp = PP_PAGENEXT_N(rootpp, TTEPAGES(pszc) - 1);
8417 8123
8418 8124 while (lastpp != rootpp) {
8419 8125 sz = PP_MAPINDEX(lastpp) ? fnd_mapping_sz(lastpp) : 0;
8420 8126 ASSERT(sz < pszc);
8421 8127 npgs = (sz == 0) ? 1 : TTEPAGES(sz);
8422 8128 ASSERT(P2PHASE(lastpp->p_pagenum, npgs) == npgs - 1);
8423 8129 while (--npgs > 0) {
8424 8130 lastpp->p_szc = (uchar_t)sz;
8425 8131 lastpp = PP_PAGEPREV(lastpp);
8426 8132 }
8427 8133 if (sz) {
8428 8134 /*
8429 8135 * make sure before current root's pszc
8430 8136 * is updated all updates to constituent pages pszc
8431 8137 * fields are globally visible.
8432 8138 */
8433 8139 membar_producer();
8434 8140 }
8435 8141 lastpp->p_szc = sz;
8436 8142 ASSERT(IS_P2ALIGNED(lastpp->p_pagenum, TTEPAGES(sz)));
8437 8143 if (lastpp != rootpp) {
8438 8144 lastpp = PP_PAGEPREV(lastpp);
8439 8145 }
8440 8146 }
8441 8147 if (sz == 0) {
8442 8148 /* the loop above doesn't cover this case */
8443 8149 rootpp->p_szc = 0;
8444 8150 }
8445 8151 out:
8446 8152 ASSERT(pp->p_szc == 0);
8447 8153 if (pmtx != NULL) {
8448 8154 sfmmu_page_exit(pmtx);
8449 8155 }
8450 8156 sfmmu_mlist_exit(pml);
8451 8157 }
8452 8158
8453 8159 /*
8454 8160 * Refresh the HAT ismttecnt[] element for size szc.
8455 8161 * Caller must have set ISM busy flag to prevent mapping
8456 8162 * lists from changing while we're traversing them.
8457 8163 */
8458 8164 pgcnt_t
8459 8165 ism_tsb_entries(sfmmu_t *sfmmup, int szc)
8460 8166 {
8461 8167 ism_blk_t *ism_blkp = sfmmup->sfmmu_iblk;
8462 8168 ism_map_t *ism_map;
8463 8169 pgcnt_t npgs = 0;
8464 8170 pgcnt_t npgs_scd = 0;
8465 8171 int j;
8466 8172 sf_scd_t *scdp;
8467 8173 uchar_t rid;
8468 8174
8469 8175 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY));
8470 8176 scdp = sfmmup->sfmmu_scdp;
8471 8177
8472 8178 for (; ism_blkp != NULL; ism_blkp = ism_blkp->iblk_next) {
8473 8179 ism_map = ism_blkp->iblk_maps;
8474 8180 for (j = 0; ism_map[j].imap_ismhat && j < ISM_MAP_SLOTS; j++) {
8475 8181 rid = ism_map[j].imap_rid;
8476 8182 ASSERT(rid == SFMMU_INVALID_ISMRID ||
8477 8183 rid < sfmmup->sfmmu_srdp->srd_next_ismrid);
8478 8184
8479 8185 if (scdp != NULL && rid != SFMMU_INVALID_ISMRID &&
8480 8186 SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid)) {
8481 8187 /* ISM is in sfmmup's SCD */
8482 8188 npgs_scd +=
8483 8189 ism_map[j].imap_ismhat->sfmmu_ttecnt[szc];
8484 8190 } else {
8485 8191 /* ISMs is not in SCD */
8486 8192 npgs +=
8487 8193 ism_map[j].imap_ismhat->sfmmu_ttecnt[szc];
8488 8194 }
8489 8195 }
8490 8196 }
8491 8197 sfmmup->sfmmu_ismttecnt[szc] = npgs;
8492 8198 sfmmup->sfmmu_scdismttecnt[szc] = npgs_scd;
8493 8199 return (npgs);
8494 8200 }
8495 8201
8496 8202 /*
8497 8203 * Yield the memory claim requirement for an address space.
8498 8204 *
8499 8205 * This is currently implemented as the number of bytes that have active
8500 8206 * hardware translations that have page structures. Therefore, it can
8501 8207 * underestimate the traditional resident set size, eg, if the
8502 8208 * physical page is present and the hardware translation is missing;
8503 8209 * and it can overestimate the rss, eg, if there are active
8504 8210 * translations to a frame buffer with page structs.
8505 8211 * Also, it does not take sharing into account.
8506 8212 *
8507 8213 * Note that we don't acquire locks here since this function is most often
8508 8214 * called from the clock thread.
↓ open down ↓ |
107 lines elided |
↑ open up ↑ |
8509 8215 */
8510 8216 size_t
8511 8217 hat_get_mapped_size(struct hat *hat)
8512 8218 {
8513 8219 size_t assize = 0;
8514 8220 int i;
8515 8221
8516 8222 if (hat == NULL)
8517 8223 return (0);
8518 8224
8519 - ASSERT(hat->sfmmu_xhat_provider == NULL);
8520 -
8521 8225 for (i = 0; i < mmu_page_sizes; i++)
8522 8226 assize += ((pgcnt_t)hat->sfmmu_ttecnt[i] +
8523 8227 (pgcnt_t)hat->sfmmu_scdrttecnt[i]) * TTEBYTES(i);
8524 8228
8525 8229 if (hat->sfmmu_iblk == NULL)
8526 8230 return (assize);
8527 8231
8528 8232 for (i = 0; i < mmu_page_sizes; i++)
8529 8233 assize += ((pgcnt_t)hat->sfmmu_ismttecnt[i] +
8530 8234 (pgcnt_t)hat->sfmmu_scdismttecnt[i]) * TTEBYTES(i);
8531 8235
8532 8236 return (assize);
8533 8237 }
8534 8238
8535 8239 int
8536 8240 hat_stats_enable(struct hat *hat)
8537 8241 {
8538 8242 hatlock_t *hatlockp;
8539 8243
8540 - ASSERT(hat->sfmmu_xhat_provider == NULL);
8541 -
8542 8244 hatlockp = sfmmu_hat_enter(hat);
8543 8245 hat->sfmmu_rmstat++;
8544 8246 sfmmu_hat_exit(hatlockp);
8545 8247 return (1);
8546 8248 }
8547 8249
8548 8250 void
8549 8251 hat_stats_disable(struct hat *hat)
8550 8252 {
8551 8253 hatlock_t *hatlockp;
8552 8254
8553 - ASSERT(hat->sfmmu_xhat_provider == NULL);
8554 -
8555 8255 hatlockp = sfmmu_hat_enter(hat);
8556 8256 hat->sfmmu_rmstat--;
8557 8257 sfmmu_hat_exit(hatlockp);
8558 8258 }
8559 8259
8560 8260 /*
8561 8261 * Routines for entering or removing ourselves from the
8562 8262 * ism_hat's mapping list. This is used for both private and
8563 8263 * SCD hats.
8564 8264 */
8565 8265 static void
8566 8266 iment_add(struct ism_ment *iment, struct hat *ism_hat)
8567 8267 {
8568 8268 ASSERT(MUTEX_HELD(&ism_mlist_lock));
8569 8269
8570 8270 iment->iment_prev = NULL;
8571 8271 iment->iment_next = ism_hat->sfmmu_iment;
8572 8272 if (ism_hat->sfmmu_iment) {
8573 8273 ism_hat->sfmmu_iment->iment_prev = iment;
8574 8274 }
8575 8275 ism_hat->sfmmu_iment = iment;
8576 8276 }
8577 8277
8578 8278 static void
8579 8279 iment_sub(struct ism_ment *iment, struct hat *ism_hat)
8580 8280 {
8581 8281 ASSERT(MUTEX_HELD(&ism_mlist_lock));
8582 8282
8583 8283 if (ism_hat->sfmmu_iment == NULL) {
8584 8284 panic("ism map entry remove - no entries");
8585 8285 }
8586 8286
8587 8287 if (iment->iment_prev) {
8588 8288 ASSERT(ism_hat->sfmmu_iment != iment);
8589 8289 iment->iment_prev->iment_next = iment->iment_next;
8590 8290 } else {
8591 8291 ASSERT(ism_hat->sfmmu_iment == iment);
8592 8292 ism_hat->sfmmu_iment = iment->iment_next;
8593 8293 }
8594 8294
8595 8295 if (iment->iment_next) {
8596 8296 iment->iment_next->iment_prev = iment->iment_prev;
8597 8297 }
8598 8298
8599 8299 /*
8600 8300 * zero out the entry
8601 8301 */
8602 8302 iment->iment_next = NULL;
8603 8303 iment->iment_prev = NULL;
8604 8304 iment->iment_hat = NULL;
8605 8305 iment->iment_base_va = 0;
8606 8306 }
8607 8307
8608 8308 /*
8609 8309 * Hat_share()/unshare() return an (non-zero) error
8610 8310 * when saddr and daddr are not properly aligned.
8611 8311 *
8612 8312 * The top level mapping element determines the alignment
8613 8313 * requirement for saddr and daddr, depending on different
8614 8314 * architectures.
8615 8315 *
8616 8316 * When hat_share()/unshare() are not supported,
8617 8317 * HATOP_SHARE()/UNSHARE() return 0
8618 8318 */
8619 8319 int
8620 8320 hat_share(struct hat *sfmmup, caddr_t addr,
8621 8321 struct hat *ism_hatid, caddr_t sptaddr, size_t len, uint_t ismszc)
8622 8322 {
8623 8323 ism_blk_t *ism_blkp;
8624 8324 ism_blk_t *new_iblk;
8625 8325 ism_map_t *ism_map;
8626 8326 ism_ment_t *ism_ment;
8627 8327 int i, added;
8628 8328 hatlock_t *hatlockp;
8629 8329 int reload_mmu = 0;
8630 8330 uint_t ismshift = page_get_shift(ismszc);
8631 8331 size_t ismpgsz = page_get_pagesize(ismszc);
8632 8332 uint_t ismmask = (uint_t)ismpgsz - 1;
8633 8333 size_t sh_size = ISM_SHIFT(ismshift, len);
8634 8334 ushort_t ismhatflag;
8635 8335 hat_region_cookie_t rcookie;
8636 8336 sf_scd_t *old_scdp;
8637 8337
8638 8338 #ifdef DEBUG
8639 8339 caddr_t eaddr = addr + len;
8640 8340 #endif /* DEBUG */
8641 8341
8642 8342 ASSERT(ism_hatid != NULL && sfmmup != NULL);
8643 8343 ASSERT(sptaddr == ISMID_STARTADDR);
8644 8344 /*
8645 8345 * Check the alignment.
↓ open down ↓ |
81 lines elided |
↑ open up ↑ |
8646 8346 */
8647 8347 if (!ISM_ALIGNED(ismshift, addr) || !ISM_ALIGNED(ismshift, sptaddr))
8648 8348 return (EINVAL);
8649 8349
8650 8350 /*
8651 8351 * Check size alignment.
8652 8352 */
8653 8353 if (!ISM_ALIGNED(ismshift, len))
8654 8354 return (EINVAL);
8655 8355
8656 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
8657 -
8658 8356 /*
8659 8357 * Allocate ism_ment for the ism_hat's mapping list, and an
8660 8358 * ism map blk in case we need one. We must do our
8661 8359 * allocations before acquiring locks to prevent a deadlock
8662 8360 * in the kmem allocator on the mapping list lock.
8663 8361 */
8664 8362 new_iblk = kmem_cache_alloc(ism_blk_cache, KM_SLEEP);
8665 8363 ism_ment = kmem_cache_alloc(ism_ment_cache, KM_SLEEP);
8666 8364
8667 8365 /*
8668 8366 * Serialize ISM mappings with the ISM busy flag, and also the
8669 8367 * trap handlers.
8670 8368 */
8671 8369 sfmmu_ismhat_enter(sfmmup, 0);
8672 8370
8673 8371 /*
8674 8372 * Allocate an ism map blk if necessary.
8675 8373 */
8676 8374 if (sfmmup->sfmmu_iblk == NULL) {
8677 8375 sfmmup->sfmmu_iblk = new_iblk;
8678 8376 bzero(new_iblk, sizeof (*new_iblk));
8679 8377 new_iblk->iblk_nextpa = (uint64_t)-1;
8680 8378 membar_stst(); /* make sure next ptr visible to all CPUs */
8681 8379 sfmmup->sfmmu_ismblkpa = va_to_pa((caddr_t)new_iblk);
8682 8380 reload_mmu = 1;
8683 8381 new_iblk = NULL;
8684 8382 }
8685 8383
8686 8384 #ifdef DEBUG
8687 8385 /*
8688 8386 * Make sure mapping does not already exist.
8689 8387 */
8690 8388 ism_blkp = sfmmup->sfmmu_iblk;
8691 8389 while (ism_blkp != NULL) {
8692 8390 ism_map = ism_blkp->iblk_maps;
8693 8391 for (i = 0; i < ISM_MAP_SLOTS && ism_map[i].imap_ismhat; i++) {
8694 8392 if ((addr >= ism_start(ism_map[i]) &&
8695 8393 addr < ism_end(ism_map[i])) ||
8696 8394 eaddr > ism_start(ism_map[i]) &&
8697 8395 eaddr <= ism_end(ism_map[i])) {
8698 8396 panic("sfmmu_share: Already mapped!");
8699 8397 }
8700 8398 }
8701 8399 ism_blkp = ism_blkp->iblk_next;
8702 8400 }
8703 8401 #endif /* DEBUG */
8704 8402
8705 8403 ASSERT(ismszc >= TTE4M);
8706 8404 if (ismszc == TTE4M) {
8707 8405 ismhatflag = HAT_4M_FLAG;
8708 8406 } else if (ismszc == TTE32M) {
8709 8407 ismhatflag = HAT_32M_FLAG;
8710 8408 } else if (ismszc == TTE256M) {
8711 8409 ismhatflag = HAT_256M_FLAG;
8712 8410 }
8713 8411 /*
8714 8412 * Add mapping to first available mapping slot.
8715 8413 */
8716 8414 ism_blkp = sfmmup->sfmmu_iblk;
8717 8415 added = 0;
8718 8416 while (!added) {
8719 8417 ism_map = ism_blkp->iblk_maps;
8720 8418 for (i = 0; i < ISM_MAP_SLOTS; i++) {
8721 8419 if (ism_map[i].imap_ismhat == NULL) {
8722 8420
8723 8421 ism_map[i].imap_ismhat = ism_hatid;
8724 8422 ism_map[i].imap_vb_shift = (uchar_t)ismshift;
8725 8423 ism_map[i].imap_rid = SFMMU_INVALID_ISMRID;
8726 8424 ism_map[i].imap_hatflags = ismhatflag;
8727 8425 ism_map[i].imap_sz_mask = ismmask;
8728 8426 /*
8729 8427 * imap_seg is checked in ISM_CHECK to see if
8730 8428 * non-NULL, then other info assumed valid.
8731 8429 */
8732 8430 membar_stst();
8733 8431 ism_map[i].imap_seg = (uintptr_t)addr | sh_size;
8734 8432 ism_map[i].imap_ment = ism_ment;
8735 8433
8736 8434 /*
8737 8435 * Now add ourselves to the ism_hat's
8738 8436 * mapping list.
8739 8437 */
8740 8438 ism_ment->iment_hat = sfmmup;
8741 8439 ism_ment->iment_base_va = addr;
8742 8440 ism_hatid->sfmmu_ismhat = 1;
8743 8441 mutex_enter(&ism_mlist_lock);
8744 8442 iment_add(ism_ment, ism_hatid);
8745 8443 mutex_exit(&ism_mlist_lock);
8746 8444 added = 1;
8747 8445 break;
8748 8446 }
8749 8447 }
8750 8448 if (!added && ism_blkp->iblk_next == NULL) {
8751 8449 ism_blkp->iblk_next = new_iblk;
8752 8450 new_iblk = NULL;
8753 8451 bzero(ism_blkp->iblk_next,
8754 8452 sizeof (*ism_blkp->iblk_next));
8755 8453 ism_blkp->iblk_next->iblk_nextpa = (uint64_t)-1;
8756 8454 membar_stst();
8757 8455 ism_blkp->iblk_nextpa =
8758 8456 va_to_pa((caddr_t)ism_blkp->iblk_next);
8759 8457 }
8760 8458 ism_blkp = ism_blkp->iblk_next;
8761 8459 }
8762 8460
8763 8461 /*
8764 8462 * After calling hat_join_region, sfmmup may join a new SCD or
8765 8463 * move from the old scd to a new scd, in which case, we want to
8766 8464 * shrink the sfmmup's private tsb size, i.e., pass shrink to
8767 8465 * sfmmu_check_page_sizes at the end of this routine.
8768 8466 */
8769 8467 old_scdp = sfmmup->sfmmu_scdp;
8770 8468
8771 8469 rcookie = hat_join_region(sfmmup, addr, len, (void *)ism_hatid, 0,
8772 8470 PROT_ALL, ismszc, NULL, HAT_REGION_ISM);
8773 8471 if (rcookie != HAT_INVALID_REGION_COOKIE) {
8774 8472 ism_map[i].imap_rid = (uchar_t)((uint64_t)rcookie);
8775 8473 }
8776 8474 /*
8777 8475 * Update our counters for this sfmmup's ism mappings.
8778 8476 */
8779 8477 for (i = 0; i <= ismszc; i++) {
8780 8478 if (!(disable_ism_large_pages & (1 << i)))
8781 8479 (void) ism_tsb_entries(sfmmup, i);
8782 8480 }
8783 8481
8784 8482 /*
8785 8483 * For ISM and DISM we do not support 512K pages, so we only only
8786 8484 * search the 4M and 8K/64K hashes for 4 pagesize cpus, and search the
8787 8485 * 256M or 32M, and 4M and 8K/64K hashes for 6 pagesize cpus.
8788 8486 *
8789 8487 * Need to set 32M/256M ISM flags to make sure
8790 8488 * sfmmu_check_page_sizes() enables them on Panther.
8791 8489 */
8792 8490 ASSERT((disable_ism_large_pages & (1 << TTE512K)) != 0);
8793 8491
8794 8492 switch (ismszc) {
8795 8493 case TTE256M:
8796 8494 if (!SFMMU_FLAGS_ISSET(sfmmup, HAT_256M_ISM)) {
8797 8495 hatlockp = sfmmu_hat_enter(sfmmup);
8798 8496 SFMMU_FLAGS_SET(sfmmup, HAT_256M_ISM);
8799 8497 sfmmu_hat_exit(hatlockp);
8800 8498 }
8801 8499 break;
8802 8500 case TTE32M:
8803 8501 if (!SFMMU_FLAGS_ISSET(sfmmup, HAT_32M_ISM)) {
8804 8502 hatlockp = sfmmu_hat_enter(sfmmup);
8805 8503 SFMMU_FLAGS_SET(sfmmup, HAT_32M_ISM);
8806 8504 sfmmu_hat_exit(hatlockp);
8807 8505 }
8808 8506 break;
8809 8507 default:
8810 8508 break;
8811 8509 }
8812 8510
8813 8511 /*
8814 8512 * If we updated the ismblkpa for this HAT we must make
8815 8513 * sure all CPUs running this process reload their tsbmiss area.
8816 8514 * Otherwise they will fail to load the mappings in the tsbmiss
8817 8515 * handler and will loop calling pagefault().
8818 8516 */
8819 8517 if (reload_mmu) {
8820 8518 hatlockp = sfmmu_hat_enter(sfmmup);
8821 8519 sfmmu_sync_mmustate(sfmmup);
8822 8520 sfmmu_hat_exit(hatlockp);
8823 8521 }
8824 8522
8825 8523 sfmmu_ismhat_exit(sfmmup, 0);
8826 8524
8827 8525 /*
8828 8526 * Free up ismblk if we didn't use it.
8829 8527 */
8830 8528 if (new_iblk != NULL)
8831 8529 kmem_cache_free(ism_blk_cache, new_iblk);
8832 8530
8833 8531 /*
8834 8532 * Check TSB and TLB page sizes.
8835 8533 */
8836 8534 if (sfmmup->sfmmu_scdp != NULL && old_scdp != sfmmup->sfmmu_scdp) {
8837 8535 sfmmu_check_page_sizes(sfmmup, 0);
8838 8536 } else {
8839 8537 sfmmu_check_page_sizes(sfmmup, 1);
8840 8538 }
8841 8539 return (0);
8842 8540 }
8843 8541
8844 8542 /*
8845 8543 * hat_unshare removes exactly one ism_map from
8846 8544 * this process's as. It expects multiple calls
8847 8545 * to hat_unshare for multiple shm segments.
8848 8546 */
8849 8547 void
8850 8548 hat_unshare(struct hat *sfmmup, caddr_t addr, size_t len, uint_t ismszc)
8851 8549 {
8852 8550 ism_map_t *ism_map;
8853 8551 ism_ment_t *free_ment = NULL;
8854 8552 ism_blk_t *ism_blkp;
8855 8553 struct hat *ism_hatid;
8856 8554 int found, i;
8857 8555 hatlock_t *hatlockp;
8858 8556 struct tsb_info *tsbinfo;
↓ open down ↓ |
191 lines elided |
↑ open up ↑ |
8859 8557 uint_t ismshift = page_get_shift(ismszc);
8860 8558 size_t sh_size = ISM_SHIFT(ismshift, len);
8861 8559 uchar_t ism_rid;
8862 8560 sf_scd_t *old_scdp;
8863 8561
8864 8562 ASSERT(ISM_ALIGNED(ismshift, addr));
8865 8563 ASSERT(ISM_ALIGNED(ismshift, len));
8866 8564 ASSERT(sfmmup != NULL);
8867 8565 ASSERT(sfmmup != ksfmmup);
8868 8566
8869 - if (sfmmup->sfmmu_xhat_provider) {
8870 - XHAT_UNSHARE(sfmmup, addr, len);
8871 - return;
8872 - } else {
8873 - /*
8874 - * This must be a CPU HAT. If the address space has
8875 - * XHATs attached, inform all XHATs that ISM segment
8876 - * is going away
8877 - */
8878 - ASSERT(sfmmup->sfmmu_as != NULL);
8879 - if (sfmmup->sfmmu_as->a_xhat != NULL)
8880 - xhat_unshare_all(sfmmup->sfmmu_as, addr, len);
8881 - }
8567 + ASSERT(sfmmup->sfmmu_as != NULL);
8882 8568
8883 8569 /*
8884 8570 * Make sure that during the entire time ISM mappings are removed,
8885 8571 * the trap handlers serialize behind us, and that no one else
8886 8572 * can be mucking with ISM mappings. This also lets us get away
8887 8573 * with not doing expensive cross calls to flush the TLB -- we
8888 8574 * just discard the context, flush the entire TSB, and call it
8889 8575 * a day.
8890 8576 */
8891 8577 sfmmu_ismhat_enter(sfmmup, 0);
8892 8578
8893 8579 /*
8894 8580 * Remove the mapping.
8895 8581 *
8896 8582 * We can't have any holes in the ism map.
8897 8583 * The tsb miss code while searching the ism map will
8898 8584 * stop on an empty map slot. So we must move
8899 8585 * everyone past the hole up 1 if any.
8900 8586 *
8901 8587 * Also empty ism map blks are not freed until the
8902 8588 * process exits. This is to prevent a MT race condition
8903 8589 * between sfmmu_unshare() and sfmmu_tsbmiss_exception().
8904 8590 */
8905 8591 found = 0;
8906 8592 ism_blkp = sfmmup->sfmmu_iblk;
8907 8593 while (!found && ism_blkp != NULL) {
8908 8594 ism_map = ism_blkp->iblk_maps;
8909 8595 for (i = 0; i < ISM_MAP_SLOTS; i++) {
8910 8596 if (addr == ism_start(ism_map[i]) &&
8911 8597 sh_size == (size_t)(ism_size(ism_map[i]))) {
8912 8598 found = 1;
8913 8599 break;
8914 8600 }
8915 8601 }
8916 8602 if (!found)
8917 8603 ism_blkp = ism_blkp->iblk_next;
8918 8604 }
8919 8605
8920 8606 if (found) {
8921 8607 ism_hatid = ism_map[i].imap_ismhat;
8922 8608 ism_rid = ism_map[i].imap_rid;
8923 8609 ASSERT(ism_hatid != NULL);
8924 8610 ASSERT(ism_hatid->sfmmu_ismhat == 1);
8925 8611
8926 8612 /*
8927 8613 * After hat_leave_region, the sfmmup may leave SCD,
8928 8614 * in which case, we want to grow the private tsb size when
8929 8615 * calling sfmmu_check_page_sizes at the end of the routine.
8930 8616 */
8931 8617 old_scdp = sfmmup->sfmmu_scdp;
8932 8618 /*
8933 8619 * Then remove ourselves from the region.
8934 8620 */
8935 8621 if (ism_rid != SFMMU_INVALID_ISMRID) {
8936 8622 hat_leave_region(sfmmup, (void *)((uint64_t)ism_rid),
8937 8623 HAT_REGION_ISM);
8938 8624 }
8939 8625
8940 8626 /*
8941 8627 * And now guarantee that any other cpu
8942 8628 * that tries to process an ISM miss
8943 8629 * will go to tl=0.
8944 8630 */
8945 8631 hatlockp = sfmmu_hat_enter(sfmmup);
8946 8632 sfmmu_invalidate_ctx(sfmmup);
8947 8633 sfmmu_hat_exit(hatlockp);
8948 8634
8949 8635 /*
8950 8636 * Remove ourselves from the ism mapping list.
8951 8637 */
8952 8638 mutex_enter(&ism_mlist_lock);
8953 8639 iment_sub(ism_map[i].imap_ment, ism_hatid);
8954 8640 mutex_exit(&ism_mlist_lock);
8955 8641 free_ment = ism_map[i].imap_ment;
8956 8642
8957 8643 /*
8958 8644 * We delete the ism map by copying
8959 8645 * the next map over the current one.
8960 8646 * We will take the next one in the maps
8961 8647 * array or from the next ism_blk.
8962 8648 */
8963 8649 while (ism_blkp != NULL) {
8964 8650 ism_map = ism_blkp->iblk_maps;
8965 8651 while (i < (ISM_MAP_SLOTS - 1)) {
8966 8652 ism_map[i] = ism_map[i + 1];
8967 8653 i++;
8968 8654 }
8969 8655 /* i == (ISM_MAP_SLOTS - 1) */
8970 8656 ism_blkp = ism_blkp->iblk_next;
8971 8657 if (ism_blkp != NULL) {
8972 8658 ism_map[i] = ism_blkp->iblk_maps[0];
8973 8659 i = 0;
8974 8660 } else {
8975 8661 ism_map[i].imap_seg = 0;
8976 8662 ism_map[i].imap_vb_shift = 0;
8977 8663 ism_map[i].imap_rid = SFMMU_INVALID_ISMRID;
8978 8664 ism_map[i].imap_hatflags = 0;
8979 8665 ism_map[i].imap_sz_mask = 0;
8980 8666 ism_map[i].imap_ismhat = NULL;
8981 8667 ism_map[i].imap_ment = NULL;
8982 8668 }
8983 8669 }
8984 8670
8985 8671 /*
8986 8672 * Now flush entire TSB for the process, since
8987 8673 * demapping page by page can be too expensive.
8988 8674 * We don't have to flush the TLB here anymore
8989 8675 * since we switch to a new TLB ctx instead.
8990 8676 * Also, there is no need to flush if the process
8991 8677 * is exiting since the TSB will be freed later.
8992 8678 */
8993 8679 if (!sfmmup->sfmmu_free) {
8994 8680 hatlockp = sfmmu_hat_enter(sfmmup);
8995 8681 for (tsbinfo = sfmmup->sfmmu_tsb; tsbinfo != NULL;
8996 8682 tsbinfo = tsbinfo->tsb_next) {
8997 8683 if (tsbinfo->tsb_flags & TSB_SWAPPED)
8998 8684 continue;
8999 8685 if (tsbinfo->tsb_flags & TSB_RELOC_FLAG) {
9000 8686 tsbinfo->tsb_flags |=
9001 8687 TSB_FLUSH_NEEDED;
9002 8688 continue;
9003 8689 }
9004 8690
9005 8691 sfmmu_inv_tsb(tsbinfo->tsb_va,
9006 8692 TSB_BYTES(tsbinfo->tsb_szc));
9007 8693 }
9008 8694 sfmmu_hat_exit(hatlockp);
9009 8695 }
9010 8696 }
9011 8697
9012 8698 /*
9013 8699 * Update our counters for this sfmmup's ism mappings.
9014 8700 */
9015 8701 for (i = 0; i <= ismszc; i++) {
9016 8702 if (!(disable_ism_large_pages & (1 << i)))
9017 8703 (void) ism_tsb_entries(sfmmup, i);
9018 8704 }
9019 8705
9020 8706 sfmmu_ismhat_exit(sfmmup, 0);
9021 8707
9022 8708 /*
9023 8709 * We must do our freeing here after dropping locks
9024 8710 * to prevent a deadlock in the kmem allocator on the
9025 8711 * mapping list lock.
9026 8712 */
9027 8713 if (free_ment != NULL)
9028 8714 kmem_cache_free(ism_ment_cache, free_ment);
9029 8715
9030 8716 /*
9031 8717 * Check TSB and TLB page sizes if the process isn't exiting.
9032 8718 */
9033 8719 if (!sfmmup->sfmmu_free) {
9034 8720 if (found && old_scdp != NULL && sfmmup->sfmmu_scdp == NULL) {
9035 8721 sfmmu_check_page_sizes(sfmmup, 1);
9036 8722 } else {
9037 8723 sfmmu_check_page_sizes(sfmmup, 0);
9038 8724 }
9039 8725 }
9040 8726 }
9041 8727
9042 8728 /* ARGSUSED */
9043 8729 static int
9044 8730 sfmmu_idcache_constructor(void *buf, void *cdrarg, int kmflags)
9045 8731 {
9046 8732 /* void *buf is sfmmu_t pointer */
9047 8733 bzero(buf, sizeof (sfmmu_t));
9048 8734
9049 8735 return (0);
9050 8736 }
9051 8737
9052 8738 /* ARGSUSED */
9053 8739 static void
9054 8740 sfmmu_idcache_destructor(void *buf, void *cdrarg)
9055 8741 {
9056 8742 /* void *buf is sfmmu_t pointer */
9057 8743 }
9058 8744
9059 8745 /*
9060 8746 * setup kmem hmeblks by bzeroing all members and initializing the nextpa
9061 8747 * field to be the pa of this hmeblk
9062 8748 */
9063 8749 /* ARGSUSED */
9064 8750 static int
9065 8751 sfmmu_hblkcache_constructor(void *buf, void *cdrarg, int kmflags)
9066 8752 {
9067 8753 struct hme_blk *hmeblkp;
9068 8754
9069 8755 bzero(buf, (size_t)cdrarg);
9070 8756 hmeblkp = (struct hme_blk *)buf;
9071 8757 hmeblkp->hblk_nextpa = va_to_pa((caddr_t)hmeblkp);
9072 8758
9073 8759 #ifdef HBLK_TRACE
9074 8760 mutex_init(&hmeblkp->hblk_audit_lock, NULL, MUTEX_DEFAULT, NULL);
9075 8761 #endif /* HBLK_TRACE */
9076 8762
9077 8763 return (0);
9078 8764 }
9079 8765
9080 8766 /* ARGSUSED */
9081 8767 static void
9082 8768 sfmmu_hblkcache_destructor(void *buf, void *cdrarg)
9083 8769 {
9084 8770
9085 8771 #ifdef HBLK_TRACE
9086 8772
9087 8773 struct hme_blk *hmeblkp;
9088 8774
9089 8775 hmeblkp = (struct hme_blk *)buf;
9090 8776 mutex_destroy(&hmeblkp->hblk_audit_lock);
9091 8777
9092 8778 #endif /* HBLK_TRACE */
9093 8779 }
9094 8780
9095 8781 #define SFMMU_CACHE_RECLAIM_SCAN_RATIO 8
9096 8782 static int sfmmu_cache_reclaim_scan_ratio = SFMMU_CACHE_RECLAIM_SCAN_RATIO;
9097 8783 /*
9098 8784 * The kmem allocator will callback into our reclaim routine when the system
9099 8785 * is running low in memory. We traverse the hash and free up all unused but
9100 8786 * still cached hme_blks. We also traverse the free list and free them up
9101 8787 * as well.
9102 8788 */
9103 8789 /*ARGSUSED*/
9104 8790 static void
9105 8791 sfmmu_hblkcache_reclaim(void *cdrarg)
9106 8792 {
9107 8793 int i;
9108 8794 struct hmehash_bucket *hmebp;
9109 8795 struct hme_blk *hmeblkp, *nx_hblk, *pr_hblk = NULL;
9110 8796 static struct hmehash_bucket *uhmehash_reclaim_hand;
9111 8797 static struct hmehash_bucket *khmehash_reclaim_hand;
9112 8798 struct hme_blk *list = NULL, *last_hmeblkp;
9113 8799 cpuset_t cpuset = cpu_ready_set;
9114 8800 cpu_hme_pend_t *cpuhp;
9115 8801
9116 8802 /* Free up hmeblks on the cpu pending lists */
9117 8803 for (i = 0; i < NCPU; i++) {
9118 8804 cpuhp = &cpu_hme_pend[i];
9119 8805 if (cpuhp->chp_listp != NULL) {
9120 8806 mutex_enter(&cpuhp->chp_mutex);
9121 8807 if (cpuhp->chp_listp == NULL) {
9122 8808 mutex_exit(&cpuhp->chp_mutex);
9123 8809 continue;
9124 8810 }
9125 8811 for (last_hmeblkp = cpuhp->chp_listp;
9126 8812 last_hmeblkp->hblk_next != NULL;
9127 8813 last_hmeblkp = last_hmeblkp->hblk_next)
9128 8814 ;
9129 8815 last_hmeblkp->hblk_next = list;
9130 8816 list = cpuhp->chp_listp;
9131 8817 cpuhp->chp_listp = NULL;
9132 8818 cpuhp->chp_count = 0;
9133 8819 mutex_exit(&cpuhp->chp_mutex);
9134 8820 }
9135 8821
9136 8822 }
9137 8823
9138 8824 if (list != NULL) {
9139 8825 kpreempt_disable();
9140 8826 CPUSET_DEL(cpuset, CPU->cpu_id);
9141 8827 xt_sync(cpuset);
9142 8828 xt_sync(cpuset);
9143 8829 kpreempt_enable();
9144 8830 sfmmu_hblk_free(&list);
9145 8831 list = NULL;
9146 8832 }
9147 8833
9148 8834 hmebp = uhmehash_reclaim_hand;
9149 8835 if (hmebp == NULL || hmebp > &uhme_hash[UHMEHASH_SZ])
9150 8836 uhmehash_reclaim_hand = hmebp = uhme_hash;
9151 8837 uhmehash_reclaim_hand += UHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio;
9152 8838
9153 8839 for (i = UHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; i; i--) {
9154 8840 if (SFMMU_HASH_LOCK_TRYENTER(hmebp) != 0) {
9155 8841 hmeblkp = hmebp->hmeblkp;
9156 8842 pr_hblk = NULL;
9157 8843 while (hmeblkp) {
9158 8844 nx_hblk = hmeblkp->hblk_next;
9159 8845 if (!hmeblkp->hblk_vcnt &&
9160 8846 !hmeblkp->hblk_hmecnt) {
9161 8847 sfmmu_hblk_hash_rm(hmebp, hmeblkp,
9162 8848 pr_hblk, &list, 0);
9163 8849 } else {
9164 8850 pr_hblk = hmeblkp;
9165 8851 }
9166 8852 hmeblkp = nx_hblk;
9167 8853 }
9168 8854 SFMMU_HASH_UNLOCK(hmebp);
9169 8855 }
9170 8856 if (hmebp++ == &uhme_hash[UHMEHASH_SZ])
9171 8857 hmebp = uhme_hash;
9172 8858 }
9173 8859
9174 8860 hmebp = khmehash_reclaim_hand;
9175 8861 if (hmebp == NULL || hmebp > &khme_hash[KHMEHASH_SZ])
9176 8862 khmehash_reclaim_hand = hmebp = khme_hash;
9177 8863 khmehash_reclaim_hand += KHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio;
9178 8864
9179 8865 for (i = KHMEHASH_SZ / sfmmu_cache_reclaim_scan_ratio; i; i--) {
9180 8866 if (SFMMU_HASH_LOCK_TRYENTER(hmebp) != 0) {
9181 8867 hmeblkp = hmebp->hmeblkp;
9182 8868 pr_hblk = NULL;
9183 8869 while (hmeblkp) {
9184 8870 nx_hblk = hmeblkp->hblk_next;
9185 8871 if (!hmeblkp->hblk_vcnt &&
9186 8872 !hmeblkp->hblk_hmecnt) {
9187 8873 sfmmu_hblk_hash_rm(hmebp, hmeblkp,
9188 8874 pr_hblk, &list, 0);
9189 8875 } else {
9190 8876 pr_hblk = hmeblkp;
9191 8877 }
9192 8878 hmeblkp = nx_hblk;
9193 8879 }
9194 8880 SFMMU_HASH_UNLOCK(hmebp);
9195 8881 }
9196 8882 if (hmebp++ == &khme_hash[KHMEHASH_SZ])
9197 8883 hmebp = khme_hash;
9198 8884 }
9199 8885 sfmmu_hblks_list_purge(&list, 0);
9200 8886 }
9201 8887
9202 8888 /*
9203 8889 * sfmmu_get_ppvcolor should become a vm_machdep or hatop interface.
9204 8890 * same goes for sfmmu_get_addrvcolor().
9205 8891 *
9206 8892 * This function will return the virtual color for the specified page. The
9207 8893 * virtual color corresponds to this page current mapping or its last mapping.
9208 8894 * It is used by memory allocators to choose addresses with the correct
9209 8895 * alignment so vac consistency is automatically maintained. If the page
9210 8896 * has no color it returns -1.
9211 8897 */
9212 8898 /*ARGSUSED*/
9213 8899 int
9214 8900 sfmmu_get_ppvcolor(struct page *pp)
9215 8901 {
9216 8902 #ifdef VAC
9217 8903 int color;
9218 8904
9219 8905 if (!(cache & CACHE_VAC) || PP_NEWPAGE(pp)) {
9220 8906 return (-1);
9221 8907 }
9222 8908 color = PP_GET_VCOLOR(pp);
9223 8909 ASSERT(color < mmu_btop(shm_alignment));
9224 8910 return (color);
9225 8911 #else
9226 8912 return (-1);
9227 8913 #endif /* VAC */
9228 8914 }
9229 8915
9230 8916 /*
9231 8917 * This function will return the desired alignment for vac consistency
9232 8918 * (vac color) given a virtual address. If no vac is present it returns -1.
9233 8919 */
9234 8920 /*ARGSUSED*/
9235 8921 int
9236 8922 sfmmu_get_addrvcolor(caddr_t vaddr)
9237 8923 {
9238 8924 #ifdef VAC
9239 8925 if (cache & CACHE_VAC) {
9240 8926 return (addr_to_vcolor(vaddr));
9241 8927 } else {
9242 8928 return (-1);
9243 8929 }
9244 8930 #else
9245 8931 return (-1);
9246 8932 #endif /* VAC */
9247 8933 }
9248 8934
9249 8935 #ifdef VAC
9250 8936 /*
9251 8937 * Check for conflicts.
9252 8938 * A conflict exists if the new and existent mappings do not match in
9253 8939 * their "shm_alignment fields. If conflicts exist, the existant mappings
9254 8940 * are flushed unless one of them is locked. If one of them is locked, then
9255 8941 * the mappings are flushed and converted to non-cacheable mappings.
9256 8942 */
9257 8943 static void
9258 8944 sfmmu_vac_conflict(struct hat *hat, caddr_t addr, page_t *pp)
9259 8945 {
9260 8946 struct hat *tmphat;
9261 8947 struct sf_hment *sfhmep, *tmphme = NULL;
9262 8948 struct hme_blk *hmeblkp;
9263 8949 int vcolor;
9264 8950 tte_t tte;
9265 8951
9266 8952 ASSERT(sfmmu_mlist_held(pp));
9267 8953 ASSERT(!PP_ISNC(pp)); /* page better be cacheable */
9268 8954
9269 8955 vcolor = addr_to_vcolor(addr);
9270 8956 if (PP_NEWPAGE(pp)) {
9271 8957 PP_SET_VCOLOR(pp, vcolor);
9272 8958 return;
9273 8959 }
9274 8960
9275 8961 if (PP_GET_VCOLOR(pp) == vcolor) {
9276 8962 return;
9277 8963 }
9278 8964
9279 8965 if (!PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp)) {
9280 8966 /*
9281 8967 * Previous user of page had a different color
9282 8968 * but since there are no current users
9283 8969 * we just flush the cache and change the color.
9284 8970 */
9285 8971 SFMMU_STAT(sf_pgcolor_conflict);
9286 8972 sfmmu_cache_flush(pp->p_pagenum, PP_GET_VCOLOR(pp));
9287 8973 PP_SET_VCOLOR(pp, vcolor);
9288 8974 return;
9289 8975 }
9290 8976
9291 8977 /*
9292 8978 * If we get here we have a vac conflict with a current
9293 8979 * mapping. VAC conflict policy is as follows.
9294 8980 * - The default is to unload the other mappings unless:
9295 8981 * - If we have a large mapping we uncache the page.
9296 8982 * We need to uncache the rest of the large page too.
9297 8983 * - If any of the mappings are locked we uncache the page.
9298 8984 * - If the requested mapping is inconsistent
9299 8985 * with another mapping and that mapping
9300 8986 * is in the same address space we have to
9301 8987 * make it non-cached. The default thing
9302 8988 * to do is unload the inconsistent mapping
9303 8989 * but if they are in the same address space
9304 8990 * we run the risk of unmapping the pc or the
9305 8991 * stack which we will use as we return to the user,
9306 8992 * in which case we can then fault on the thing
9307 8993 * we just unloaded and get into an infinite loop.
9308 8994 */
9309 8995 if (PP_ISMAPPED_LARGE(pp)) {
9310 8996 int sz;
9311 8997
9312 8998 /*
9313 8999 * Existing mapping is for big pages. We don't unload
9314 9000 * existing big mappings to satisfy new mappings.
9315 9001 * Always convert all mappings to TNC.
9316 9002 */
9317 9003 sz = fnd_mapping_sz(pp);
9318 9004 pp = PP_GROUPLEADER(pp, sz);
9319 9005 SFMMU_STAT_ADD(sf_uncache_conflict, TTEPAGES(sz));
9320 9006 sfmmu_page_cache_array(pp, HAT_TMPNC, CACHE_FLUSH,
9321 9007 TTEPAGES(sz));
9322 9008
9323 9009 return;
9324 9010 }
↓ open down ↓ |
433 lines elided |
↑ open up ↑ |
9325 9011
9326 9012 /*
9327 9013 * check if any mapping is in same as or if it is locked
9328 9014 * since in that case we need to uncache.
9329 9015 */
9330 9016 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = tmphme) {
9331 9017 tmphme = sfhmep->hme_next;
9332 9018 if (IS_PAHME(sfhmep))
9333 9019 continue;
9334 9020 hmeblkp = sfmmu_hmetohblk(sfhmep);
9335 - if (hmeblkp->hblk_xhat_bit)
9336 - continue;
9337 9021 tmphat = hblktosfmmu(hmeblkp);
9338 9022 sfmmu_copytte(&sfhmep->hme_tte, &tte);
9339 9023 ASSERT(TTE_IS_VALID(&tte));
9340 9024 if (hmeblkp->hblk_shared || tmphat == hat ||
9341 9025 hmeblkp->hblk_lckcnt) {
9342 9026 /*
9343 9027 * We have an uncache conflict
9344 9028 */
9345 9029 SFMMU_STAT(sf_uncache_conflict);
9346 9030 sfmmu_page_cache_array(pp, HAT_TMPNC, CACHE_FLUSH, 1);
9347 9031 return;
9348 9032 }
9349 9033 }
9350 9034
9351 9035 /*
9352 9036 * We have an unload conflict
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
9353 9037 * We have already checked for LARGE mappings, therefore
9354 9038 * the remaining mapping(s) must be TTE8K.
9355 9039 */
9356 9040 SFMMU_STAT(sf_unload_conflict);
9357 9041
9358 9042 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = tmphme) {
9359 9043 tmphme = sfhmep->hme_next;
9360 9044 if (IS_PAHME(sfhmep))
9361 9045 continue;
9362 9046 hmeblkp = sfmmu_hmetohblk(sfhmep);
9363 - if (hmeblkp->hblk_xhat_bit)
9364 - continue;
9365 9047 ASSERT(!hmeblkp->hblk_shared);
9366 9048 (void) sfmmu_pageunload(pp, sfhmep, TTE8K);
9367 9049 }
9368 9050
9369 9051 if (PP_ISMAPPED_KPM(pp))
9370 9052 sfmmu_kpm_vac_unload(pp, addr);
9371 9053
9372 9054 /*
9373 9055 * Unloads only do TLB flushes so we need to flush the
9374 9056 * cache here.
9375 9057 */
9376 9058 sfmmu_cache_flush(pp->p_pagenum, PP_GET_VCOLOR(pp));
9377 9059 PP_SET_VCOLOR(pp, vcolor);
9378 9060 }
9379 9061
9380 9062 /*
9381 9063 * Whenever a mapping is unloaded and the page is in TNC state,
9382 9064 * we see if the page can be made cacheable again. 'pp' is
9383 9065 * the page that we just unloaded a mapping from, the size
9384 9066 * of mapping that was unloaded is 'ottesz'.
9385 9067 * Remark:
9386 9068 * The recache policy for mpss pages can leave a performance problem
9387 9069 * under the following circumstances:
9388 9070 * . A large page in uncached mode has just been unmapped.
9389 9071 * . All constituent pages are TNC due to a conflicting small mapping.
9390 9072 * . There are many other, non conflicting, small mappings around for
9391 9073 * a lot of the constituent pages.
9392 9074 * . We're called w/ the "old" groupleader page and the old ottesz,
9393 9075 * but this is irrelevant, since we're no more "PP_ISMAPPED_LARGE", so
9394 9076 * we end up w/ TTE8K or npages == 1.
9395 9077 * . We call tst_tnc w/ the old groupleader only, and if there is no
9396 9078 * conflict, we re-cache only this page.
9397 9079 * . All other small mappings are not checked and will be left in TNC mode.
9398 9080 * The problem is not very serious because:
9399 9081 * . mpss is actually only defined for heap and stack, so the probability
9400 9082 * is not very high that a large page mapping exists in parallel to a small
9401 9083 * one (this is possible, but seems to be bad programming style in the
9402 9084 * appl).
9403 9085 * . The problem gets a little bit more serious, when those TNC pages
9404 9086 * have to be mapped into kernel space, e.g. for networking.
9405 9087 * . When VAC alias conflicts occur in applications, this is regarded
9406 9088 * as an application bug. So if kstat's show them, the appl should
9407 9089 * be changed anyway.
9408 9090 */
9409 9091 void
9410 9092 conv_tnc(page_t *pp, int ottesz)
9411 9093 {
9412 9094 int cursz, dosz;
9413 9095 pgcnt_t curnpgs, dopgs;
9414 9096 pgcnt_t pg64k;
9415 9097 page_t *pp2;
9416 9098
9417 9099 /*
9418 9100 * Determine how big a range we check for TNC and find
9419 9101 * leader page. cursz is the size of the biggest
9420 9102 * mapping that still exist on 'pp'.
9421 9103 */
9422 9104 if (PP_ISMAPPED_LARGE(pp)) {
9423 9105 cursz = fnd_mapping_sz(pp);
9424 9106 } else {
9425 9107 cursz = TTE8K;
9426 9108 }
9427 9109
9428 9110 if (ottesz >= cursz) {
9429 9111 dosz = ottesz;
9430 9112 pp2 = pp;
9431 9113 } else {
9432 9114 dosz = cursz;
9433 9115 pp2 = PP_GROUPLEADER(pp, dosz);
9434 9116 }
9435 9117
9436 9118 pg64k = TTEPAGES(TTE64K);
9437 9119 dopgs = TTEPAGES(dosz);
9438 9120
9439 9121 ASSERT(dopgs == 1 || ((dopgs & (pg64k - 1)) == 0));
9440 9122
9441 9123 while (dopgs != 0) {
9442 9124 curnpgs = TTEPAGES(cursz);
9443 9125 if (tst_tnc(pp2, curnpgs)) {
9444 9126 SFMMU_STAT_ADD(sf_recache, curnpgs);
9445 9127 sfmmu_page_cache_array(pp2, HAT_CACHE, CACHE_NO_FLUSH,
9446 9128 curnpgs);
9447 9129 }
9448 9130
9449 9131 ASSERT(dopgs >= curnpgs);
9450 9132 dopgs -= curnpgs;
9451 9133
9452 9134 if (dopgs == 0) {
9453 9135 break;
9454 9136 }
9455 9137
9456 9138 pp2 = PP_PAGENEXT_N(pp2, curnpgs);
9457 9139 if (((dopgs & (pg64k - 1)) == 0) && PP_ISMAPPED_LARGE(pp2)) {
9458 9140 cursz = fnd_mapping_sz(pp2);
9459 9141 } else {
9460 9142 cursz = TTE8K;
9461 9143 }
9462 9144 }
9463 9145 }
9464 9146
9465 9147 /*
9466 9148 * Returns 1 if page(s) can be converted from TNC to cacheable setting,
9467 9149 * returns 0 otherwise. Note that oaddr argument is valid for only
9468 9150 * 8k pages.
9469 9151 */
9470 9152 int
9471 9153 tst_tnc(page_t *pp, pgcnt_t npages)
9472 9154 {
9473 9155 struct sf_hment *sfhme;
9474 9156 struct hme_blk *hmeblkp;
9475 9157 tte_t tte;
9476 9158 caddr_t vaddr;
9477 9159 int clr_valid = 0;
9478 9160 int color, color1, bcolor;
9479 9161 int i, ncolors;
9480 9162
9481 9163 ASSERT(pp != NULL);
9482 9164 ASSERT(!(cache & CACHE_WRITEBACK));
9483 9165
9484 9166 if (npages > 1) {
9485 9167 ncolors = CACHE_NUM_COLOR;
9486 9168 }
9487 9169
9488 9170 for (i = 0; i < npages; i++) {
9489 9171 ASSERT(sfmmu_mlist_held(pp));
9490 9172 ASSERT(PP_ISTNC(pp));
9491 9173 ASSERT(PP_GET_VCOLOR(pp) == NO_VCOLOR);
9492 9174
9493 9175 if (PP_ISPNC(pp)) {
9494 9176 return (0);
9495 9177 }
9496 9178
9497 9179 clr_valid = 0;
9498 9180 if (PP_ISMAPPED_KPM(pp)) {
9499 9181 caddr_t kpmvaddr;
9500 9182
9501 9183 ASSERT(kpm_enable);
↓ open down ↓ |
127 lines elided |
↑ open up ↑ |
9502 9184 kpmvaddr = hat_kpm_page2va(pp, 1);
9503 9185 ASSERT(!(npages > 1 && IS_KPM_ALIAS_RANGE(kpmvaddr)));
9504 9186 color1 = addr_to_vcolor(kpmvaddr);
9505 9187 clr_valid = 1;
9506 9188 }
9507 9189
9508 9190 for (sfhme = pp->p_mapping; sfhme; sfhme = sfhme->hme_next) {
9509 9191 if (IS_PAHME(sfhme))
9510 9192 continue;
9511 9193 hmeblkp = sfmmu_hmetohblk(sfhme);
9512 - if (hmeblkp->hblk_xhat_bit)
9513 - continue;
9514 9194
9515 9195 sfmmu_copytte(&sfhme->hme_tte, &tte);
9516 9196 ASSERT(TTE_IS_VALID(&tte));
9517 9197
9518 9198 vaddr = tte_to_vaddr(hmeblkp, tte);
9519 9199 color = addr_to_vcolor(vaddr);
9520 9200
9521 9201 if (npages > 1) {
9522 9202 /*
9523 9203 * If there is a big mapping, make sure
9524 9204 * 8K mapping is consistent with the big
9525 9205 * mapping.
9526 9206 */
9527 9207 bcolor = i % ncolors;
9528 9208 if (color != bcolor) {
9529 9209 return (0);
9530 9210 }
9531 9211 }
9532 9212 if (!clr_valid) {
9533 9213 clr_valid = 1;
9534 9214 color1 = color;
9535 9215 }
9536 9216
9537 9217 if (color1 != color) {
9538 9218 return (0);
9539 9219 }
9540 9220 }
9541 9221
9542 9222 pp = PP_PAGENEXT(pp);
9543 9223 }
9544 9224
9545 9225 return (1);
9546 9226 }
9547 9227
9548 9228 void
9549 9229 sfmmu_page_cache_array(page_t *pp, int flags, int cache_flush_flag,
9550 9230 pgcnt_t npages)
9551 9231 {
9552 9232 kmutex_t *pmtx;
9553 9233 int i, ncolors, bcolor;
9554 9234 kpm_hlk_t *kpmp;
9555 9235 cpuset_t cpuset;
9556 9236
9557 9237 ASSERT(pp != NULL);
9558 9238 ASSERT(!(cache & CACHE_WRITEBACK));
9559 9239
9560 9240 kpmp = sfmmu_kpm_kpmp_enter(pp, npages);
9561 9241 pmtx = sfmmu_page_enter(pp);
9562 9242
9563 9243 /*
9564 9244 * Fast path caching single unmapped page
9565 9245 */
9566 9246 if (npages == 1 && !PP_ISMAPPED(pp) && !PP_ISMAPPED_KPM(pp) &&
9567 9247 flags == HAT_CACHE) {
9568 9248 PP_CLRTNC(pp);
9569 9249 PP_CLRPNC(pp);
9570 9250 sfmmu_page_exit(pmtx);
9571 9251 sfmmu_kpm_kpmp_exit(kpmp);
9572 9252 return;
9573 9253 }
9574 9254
9575 9255 /*
9576 9256 * We need to capture all cpus in order to change cacheability
9577 9257 * because we can't allow one cpu to access the same physical
9578 9258 * page using a cacheable and a non-cachebale mapping at the same
9579 9259 * time. Since we may end up walking the ism mapping list
9580 9260 * have to grab it's lock now since we can't after all the
9581 9261 * cpus have been captured.
9582 9262 */
9583 9263 sfmmu_hat_lock_all();
9584 9264 mutex_enter(&ism_mlist_lock);
9585 9265 kpreempt_disable();
9586 9266 cpuset = cpu_ready_set;
9587 9267 xc_attention(cpuset);
9588 9268
9589 9269 if (npages > 1) {
9590 9270 /*
9591 9271 * Make sure all colors are flushed since the
9592 9272 * sfmmu_page_cache() only flushes one color-
9593 9273 * it does not know big pages.
9594 9274 */
9595 9275 ncolors = CACHE_NUM_COLOR;
9596 9276 if (flags & HAT_TMPNC) {
9597 9277 for (i = 0; i < ncolors; i++) {
9598 9278 sfmmu_cache_flushcolor(i, pp->p_pagenum);
9599 9279 }
9600 9280 cache_flush_flag = CACHE_NO_FLUSH;
9601 9281 }
9602 9282 }
9603 9283
9604 9284 for (i = 0; i < npages; i++) {
9605 9285
9606 9286 ASSERT(sfmmu_mlist_held(pp));
9607 9287
9608 9288 if (!(flags == HAT_TMPNC && PP_ISTNC(pp))) {
9609 9289
9610 9290 if (npages > 1) {
9611 9291 bcolor = i % ncolors;
9612 9292 } else {
9613 9293 bcolor = NO_VCOLOR;
9614 9294 }
9615 9295
9616 9296 sfmmu_page_cache(pp, flags, cache_flush_flag,
9617 9297 bcolor);
9618 9298 }
9619 9299
9620 9300 pp = PP_PAGENEXT(pp);
9621 9301 }
9622 9302
9623 9303 xt_sync(cpuset);
9624 9304 xc_dismissed(cpuset);
9625 9305 mutex_exit(&ism_mlist_lock);
9626 9306 sfmmu_hat_unlock_all();
9627 9307 sfmmu_page_exit(pmtx);
9628 9308 sfmmu_kpm_kpmp_exit(kpmp);
9629 9309 kpreempt_enable();
9630 9310 }
9631 9311
9632 9312 /*
9633 9313 * This function changes the virtual cacheability of all mappings to a
9634 9314 * particular page. When changing from uncache to cacheable the mappings will
9635 9315 * only be changed if all of them have the same virtual color.
9636 9316 * We need to flush the cache in all cpus. It is possible that
9637 9317 * a process referenced a page as cacheable but has sinced exited
9638 9318 * and cleared the mapping list. We still to flush it but have no
9639 9319 * state so all cpus is the only alternative.
9640 9320 */
9641 9321 static void
9642 9322 sfmmu_page_cache(page_t *pp, int flags, int cache_flush_flag, int bcolor)
9643 9323 {
9644 9324 struct sf_hment *sfhme;
9645 9325 struct hme_blk *hmeblkp;
9646 9326 sfmmu_t *sfmmup;
9647 9327 tte_t tte, ttemod;
9648 9328 caddr_t vaddr;
9649 9329 int ret, color;
9650 9330 pfn_t pfn;
↓ open down ↓ |
127 lines elided |
↑ open up ↑ |
9651 9331
9652 9332 color = bcolor;
9653 9333 pfn = pp->p_pagenum;
9654 9334
9655 9335 for (sfhme = pp->p_mapping; sfhme; sfhme = sfhme->hme_next) {
9656 9336
9657 9337 if (IS_PAHME(sfhme))
9658 9338 continue;
9659 9339 hmeblkp = sfmmu_hmetohblk(sfhme);
9660 9340
9661 - if (hmeblkp->hblk_xhat_bit)
9662 - continue;
9663 -
9664 9341 sfmmu_copytte(&sfhme->hme_tte, &tte);
9665 9342 ASSERT(TTE_IS_VALID(&tte));
9666 9343 vaddr = tte_to_vaddr(hmeblkp, tte);
9667 9344 color = addr_to_vcolor(vaddr);
9668 9345
9669 9346 #ifdef DEBUG
9670 9347 if ((flags & HAT_CACHE) && bcolor != NO_VCOLOR) {
9671 9348 ASSERT(color == bcolor);
9672 9349 }
9673 9350 #endif
9674 9351
9675 9352 ASSERT(flags != HAT_TMPNC || color == PP_GET_VCOLOR(pp));
9676 9353
9677 9354 ttemod = tte;
9678 9355 if (flags & (HAT_UNCACHE | HAT_TMPNC)) {
9679 9356 TTE_CLR_VCACHEABLE(&ttemod);
9680 9357 } else { /* flags & HAT_CACHE */
9681 9358 TTE_SET_VCACHEABLE(&ttemod);
9682 9359 }
9683 9360 ret = sfmmu_modifytte_try(&tte, &ttemod, &sfhme->hme_tte);
9684 9361 if (ret < 0) {
9685 9362 /*
9686 9363 * Since all cpus are captured modifytte should not
9687 9364 * fail.
9688 9365 */
9689 9366 panic("sfmmu_page_cache: write to tte failed");
9690 9367 }
9691 9368
9692 9369 sfmmup = hblktosfmmu(hmeblkp);
9693 9370 if (cache_flush_flag == CACHE_FLUSH) {
9694 9371 /*
9695 9372 * Flush TSBs, TLBs and caches
9696 9373 */
9697 9374 if (hmeblkp->hblk_shared) {
9698 9375 sf_srd_t *srdp = (sf_srd_t *)sfmmup;
9699 9376 uint_t rid = hmeblkp->hblk_tag.htag_rid;
9700 9377 sf_region_t *rgnp;
9701 9378 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
9702 9379 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
9703 9380 ASSERT(srdp != NULL);
9704 9381 rgnp = srdp->srd_hmergnp[rid];
9705 9382 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp,
9706 9383 srdp, rgnp, rid);
9707 9384 (void) sfmmu_rgntlb_demap(vaddr, rgnp,
9708 9385 hmeblkp, 0);
9709 9386 sfmmu_cache_flush(pfn, addr_to_vcolor(vaddr));
9710 9387 } else if (sfmmup->sfmmu_ismhat) {
9711 9388 if (flags & HAT_CACHE) {
9712 9389 SFMMU_STAT(sf_ism_recache);
9713 9390 } else {
9714 9391 SFMMU_STAT(sf_ism_uncache);
9715 9392 }
9716 9393 sfmmu_ismtlbcache_demap(vaddr, sfmmup, hmeblkp,
9717 9394 pfn, CACHE_FLUSH);
9718 9395 } else {
9719 9396 sfmmu_tlbcache_demap(vaddr, sfmmup, hmeblkp,
9720 9397 pfn, 0, FLUSH_ALL_CPUS, CACHE_FLUSH, 1);
9721 9398 }
9722 9399
9723 9400 /*
9724 9401 * all cache entries belonging to this pfn are
9725 9402 * now flushed.
9726 9403 */
9727 9404 cache_flush_flag = CACHE_NO_FLUSH;
9728 9405 } else {
9729 9406 /*
9730 9407 * Flush only TSBs and TLBs.
9731 9408 */
9732 9409 if (hmeblkp->hblk_shared) {
9733 9410 sf_srd_t *srdp = (sf_srd_t *)sfmmup;
9734 9411 uint_t rid = hmeblkp->hblk_tag.htag_rid;
9735 9412 sf_region_t *rgnp;
9736 9413 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
9737 9414 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
9738 9415 ASSERT(srdp != NULL);
9739 9416 rgnp = srdp->srd_hmergnp[rid];
9740 9417 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp,
9741 9418 srdp, rgnp, rid);
9742 9419 (void) sfmmu_rgntlb_demap(vaddr, rgnp,
9743 9420 hmeblkp, 0);
9744 9421 } else if (sfmmup->sfmmu_ismhat) {
9745 9422 if (flags & HAT_CACHE) {
9746 9423 SFMMU_STAT(sf_ism_recache);
9747 9424 } else {
9748 9425 SFMMU_STAT(sf_ism_uncache);
9749 9426 }
9750 9427 sfmmu_ismtlbcache_demap(vaddr, sfmmup, hmeblkp,
9751 9428 pfn, CACHE_NO_FLUSH);
9752 9429 } else {
9753 9430 sfmmu_tlb_demap(vaddr, sfmmup, hmeblkp, 0, 1);
9754 9431 }
9755 9432 }
9756 9433 }
9757 9434
9758 9435 if (PP_ISMAPPED_KPM(pp))
9759 9436 sfmmu_kpm_page_cache(pp, flags, cache_flush_flag);
9760 9437
9761 9438 switch (flags) {
9762 9439
9763 9440 default:
9764 9441 panic("sfmmu_pagecache: unknown flags");
9765 9442 break;
9766 9443
9767 9444 case HAT_CACHE:
9768 9445 PP_CLRTNC(pp);
9769 9446 PP_CLRPNC(pp);
9770 9447 PP_SET_VCOLOR(pp, color);
9771 9448 break;
9772 9449
9773 9450 case HAT_TMPNC:
9774 9451 PP_SETTNC(pp);
9775 9452 PP_SET_VCOLOR(pp, NO_VCOLOR);
9776 9453 break;
9777 9454
9778 9455 case HAT_UNCACHE:
9779 9456 PP_SETPNC(pp);
9780 9457 PP_CLRTNC(pp);
9781 9458 PP_SET_VCOLOR(pp, NO_VCOLOR);
9782 9459 break;
9783 9460 }
9784 9461 }
9785 9462 #endif /* VAC */
9786 9463
9787 9464
9788 9465 /*
9789 9466 * Wrapper routine used to return a context.
9790 9467 *
9791 9468 * It's the responsibility of the caller to guarantee that the
9792 9469 * process serializes on calls here by taking the HAT lock for
9793 9470 * the hat.
9794 9471 *
9795 9472 */
9796 9473 static void
9797 9474 sfmmu_get_ctx(sfmmu_t *sfmmup)
9798 9475 {
9799 9476 mmu_ctx_t *mmu_ctxp;
9800 9477 uint_t pstate_save;
9801 9478 int ret;
9802 9479
9803 9480 ASSERT(sfmmu_hat_lock_held(sfmmup));
9804 9481 ASSERT(sfmmup != ksfmmup);
9805 9482
9806 9483 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_ALLCTX_INVALID)) {
9807 9484 sfmmu_setup_tsbinfo(sfmmup);
9808 9485 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ALLCTX_INVALID);
9809 9486 }
9810 9487
9811 9488 kpreempt_disable();
9812 9489
9813 9490 mmu_ctxp = CPU_MMU_CTXP(CPU);
9814 9491 ASSERT(mmu_ctxp);
9815 9492 ASSERT(mmu_ctxp->mmu_idx < max_mmu_ctxdoms);
9816 9493 ASSERT(mmu_ctxp == mmu_ctxs_tbl[mmu_ctxp->mmu_idx]);
9817 9494
9818 9495 /*
9819 9496 * Do a wrap-around if cnum reaches the max # cnum supported by a MMU.
9820 9497 */
9821 9498 if (mmu_ctxp->mmu_cnum == mmu_ctxp->mmu_nctxs)
9822 9499 sfmmu_ctx_wrap_around(mmu_ctxp, B_TRUE);
9823 9500
9824 9501 /*
9825 9502 * Let the MMU set up the page sizes to use for
9826 9503 * this context in the TLB. Don't program 2nd dtlb for ism hat.
9827 9504 */
9828 9505 if ((&mmu_set_ctx_page_sizes) && (sfmmup->sfmmu_ismhat == 0)) {
9829 9506 mmu_set_ctx_page_sizes(sfmmup);
9830 9507 }
9831 9508
9832 9509 /*
9833 9510 * sfmmu_alloc_ctx and sfmmu_load_mmustate will be performed with
9834 9511 * interrupts disabled to prevent race condition with wrap-around
9835 9512 * ctx invalidatation. In sun4v, ctx invalidation also involves
9836 9513 * a HV call to set the number of TSBs to 0. If interrupts are not
9837 9514 * disabled until after sfmmu_load_mmustate is complete TSBs may
9838 9515 * become assigned to INVALID_CONTEXT. This is not allowed.
9839 9516 */
9840 9517 pstate_save = sfmmu_disable_intrs();
9841 9518
9842 9519 if (sfmmu_alloc_ctx(sfmmup, 1, CPU, SFMMU_PRIVATE) &&
9843 9520 sfmmup->sfmmu_scdp != NULL) {
9844 9521 sf_scd_t *scdp = sfmmup->sfmmu_scdp;
9845 9522 sfmmu_t *scsfmmup = scdp->scd_sfmmup;
9846 9523 ret = sfmmu_alloc_ctx(scsfmmup, 1, CPU, SFMMU_SHARED);
9847 9524 /* debug purpose only */
9848 9525 ASSERT(!ret || scsfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum
9849 9526 != INVALID_CONTEXT);
9850 9527 }
9851 9528 sfmmu_load_mmustate(sfmmup);
9852 9529
9853 9530 sfmmu_enable_intrs(pstate_save);
9854 9531
9855 9532 kpreempt_enable();
9856 9533 }
9857 9534
9858 9535 /*
9859 9536 * When all cnums are used up in a MMU, cnum will wrap around to the
9860 9537 * next generation and start from 2.
9861 9538 */
9862 9539 static void
9863 9540 sfmmu_ctx_wrap_around(mmu_ctx_t *mmu_ctxp, boolean_t reset_cnum)
9864 9541 {
9865 9542
9866 9543 /* caller must have disabled the preemption */
9867 9544 ASSERT(curthread->t_preempt >= 1);
9868 9545 ASSERT(mmu_ctxp != NULL);
9869 9546
9870 9547 /* acquire Per-MMU (PM) spin lock */
9871 9548 mutex_enter(&mmu_ctxp->mmu_lock);
9872 9549
9873 9550 /* re-check to see if wrap-around is needed */
9874 9551 if (mmu_ctxp->mmu_cnum < mmu_ctxp->mmu_nctxs)
9875 9552 goto done;
9876 9553
9877 9554 SFMMU_MMU_STAT(mmu_wrap_around);
9878 9555
9879 9556 /* update gnum */
9880 9557 ASSERT(mmu_ctxp->mmu_gnum != 0);
9881 9558 mmu_ctxp->mmu_gnum++;
9882 9559 if (mmu_ctxp->mmu_gnum == 0 ||
9883 9560 mmu_ctxp->mmu_gnum > MAX_SFMMU_GNUM_VAL) {
9884 9561 cmn_err(CE_PANIC, "mmu_gnum of mmu_ctx 0x%p is out of bound.",
9885 9562 (void *)mmu_ctxp);
9886 9563 }
9887 9564
9888 9565 if (mmu_ctxp->mmu_ncpus > 1) {
9889 9566 cpuset_t cpuset;
9890 9567
9891 9568 membar_enter(); /* make sure updated gnum visible */
9892 9569
9893 9570 SFMMU_XCALL_STATS(NULL);
9894 9571
9895 9572 /* xcall to others on the same MMU to invalidate ctx */
9896 9573 cpuset = mmu_ctxp->mmu_cpuset;
9897 9574 ASSERT(CPU_IN_SET(cpuset, CPU->cpu_id) || !reset_cnum);
9898 9575 CPUSET_DEL(cpuset, CPU->cpu_id);
9899 9576 CPUSET_AND(cpuset, cpu_ready_set);
9900 9577
9901 9578 /*
9902 9579 * Pass in INVALID_CONTEXT as the first parameter to
9903 9580 * sfmmu_raise_tsb_exception, which invalidates the context
9904 9581 * of any process running on the CPUs in the MMU.
9905 9582 */
9906 9583 xt_some(cpuset, sfmmu_raise_tsb_exception,
9907 9584 INVALID_CONTEXT, INVALID_CONTEXT);
9908 9585 xt_sync(cpuset);
9909 9586
9910 9587 SFMMU_MMU_STAT(mmu_tsb_raise_exception);
9911 9588 }
9912 9589
9913 9590 if (sfmmu_getctx_sec() != INVALID_CONTEXT) {
9914 9591 sfmmu_setctx_sec(INVALID_CONTEXT);
9915 9592 sfmmu_clear_utsbinfo();
9916 9593 }
9917 9594
9918 9595 /*
9919 9596 * No xcall is needed here. For sun4u systems all CPUs in context
9920 9597 * domain share a single physical MMU therefore it's enough to flush
9921 9598 * TLB on local CPU. On sun4v systems we use 1 global context
9922 9599 * domain and flush all remote TLBs in sfmmu_raise_tsb_exception
9923 9600 * handler. Note that vtag_flushall_uctxs() is called
9924 9601 * for Ultra II machine, where the equivalent flushall functionality
9925 9602 * is implemented in SW, and only user ctx TLB entries are flushed.
9926 9603 */
9927 9604 if (&vtag_flushall_uctxs != NULL) {
9928 9605 vtag_flushall_uctxs();
9929 9606 } else {
9930 9607 vtag_flushall();
9931 9608 }
9932 9609
9933 9610 /* reset mmu cnum, skips cnum 0 and 1 */
9934 9611 if (reset_cnum == B_TRUE)
9935 9612 mmu_ctxp->mmu_cnum = NUM_LOCKED_CTXS;
9936 9613
9937 9614 done:
9938 9615 mutex_exit(&mmu_ctxp->mmu_lock);
9939 9616 }
9940 9617
9941 9618
9942 9619 /*
9943 9620 * For multi-threaded process, set the process context to INVALID_CONTEXT
9944 9621 * so that it faults and reloads the MMU state from TL=0. For single-threaded
9945 9622 * process, we can just load the MMU state directly without having to
9946 9623 * set context invalid. Caller must hold the hat lock since we don't
9947 9624 * acquire it here.
9948 9625 */
9949 9626 static void
9950 9627 sfmmu_sync_mmustate(sfmmu_t *sfmmup)
9951 9628 {
9952 9629 uint_t cnum;
9953 9630 uint_t pstate_save;
9954 9631
9955 9632 ASSERT(sfmmup != ksfmmup);
9956 9633 ASSERT(sfmmu_hat_lock_held(sfmmup));
9957 9634
9958 9635 kpreempt_disable();
9959 9636
9960 9637 /*
9961 9638 * We check whether the pass'ed-in sfmmup is the same as the
9962 9639 * current running proc. This is to makes sure the current proc
9963 9640 * stays single-threaded if it already is.
9964 9641 */
9965 9642 if ((sfmmup == curthread->t_procp->p_as->a_hat) &&
9966 9643 (curthread->t_procp->p_lwpcnt == 1)) {
9967 9644 /* single-thread */
9968 9645 cnum = sfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum;
9969 9646 if (cnum != INVALID_CONTEXT) {
9970 9647 uint_t curcnum;
9971 9648 /*
9972 9649 * Disable interrupts to prevent race condition
9973 9650 * with sfmmu_ctx_wrap_around ctx invalidation.
9974 9651 * In sun4v, ctx invalidation involves setting
9975 9652 * TSB to NULL, hence, interrupts should be disabled
9976 9653 * untill after sfmmu_load_mmustate is completed.
9977 9654 */
9978 9655 pstate_save = sfmmu_disable_intrs();
9979 9656 curcnum = sfmmu_getctx_sec();
9980 9657 if (curcnum == cnum)
9981 9658 sfmmu_load_mmustate(sfmmup);
9982 9659 sfmmu_enable_intrs(pstate_save);
9983 9660 ASSERT(curcnum == cnum || curcnum == INVALID_CONTEXT);
9984 9661 }
9985 9662 } else {
9986 9663 /*
9987 9664 * multi-thread
9988 9665 * or when sfmmup is not the same as the curproc.
↓ open down ↓ |
315 lines elided |
↑ open up ↑ |
9989 9666 */
9990 9667 sfmmu_invalidate_ctx(sfmmup);
9991 9668 }
9992 9669
9993 9670 kpreempt_enable();
9994 9671 }
9995 9672
9996 9673
9997 9674 /*
9998 9675 * 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
9676 + * we grow, or shrink a TSB. When swapping in a TSB (TSB_SWAPIN), the
10000 9677 * TSB_FORCEALLOC flag may be used to force allocation of a minimum-sized TSB
10001 9678 * (8K).
10002 9679 *
10003 9680 * Caller must hold the HAT lock, but should assume any tsb_info
10004 9681 * pointers it has are no longer valid after calling this function.
10005 9682 *
10006 9683 * Return values:
10007 9684 * TSB_ALLOCFAIL Failed to allocate a TSB, due to memory constraints
10008 9685 * TSB_LOSTRACE HAT is busy, i.e. another thread is already doing
10009 9686 * something to this tsbinfo/TSB
10010 9687 * TSB_SUCCESS Operation succeeded
10011 9688 */
10012 9689 static tsb_replace_rc_t
10013 9690 sfmmu_replace_tsb(sfmmu_t *sfmmup, struct tsb_info *old_tsbinfo, uint_t szc,
10014 9691 hatlock_t *hatlockp, uint_t flags)
10015 9692 {
10016 9693 struct tsb_info *new_tsbinfo = NULL;
10017 9694 struct tsb_info *curtsb, *prevtsb;
10018 9695 uint_t tte_sz_mask;
10019 9696 int i;
10020 9697
10021 9698 ASSERT(sfmmup != ksfmmup);
10022 9699 ASSERT(sfmmup->sfmmu_ismhat == 0);
10023 9700 ASSERT(sfmmu_hat_lock_held(sfmmup));
10024 9701 ASSERT(szc <= tsb_max_growsize);
10025 9702
10026 9703 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_BUSY))
10027 9704 return (TSB_LOSTRACE);
10028 9705
10029 9706 /*
10030 9707 * Find the tsb_info ahead of this one in the list, and
10031 9708 * also make sure that the tsb_info passed in really
10032 9709 * exists!
10033 9710 */
10034 9711 for (prevtsb = NULL, curtsb = sfmmup->sfmmu_tsb;
10035 9712 curtsb != old_tsbinfo && curtsb != NULL;
10036 9713 prevtsb = curtsb, curtsb = curtsb->tsb_next)
10037 9714 ;
10038 9715 ASSERT(curtsb != NULL);
10039 9716
10040 9717 if (!(flags & TSB_SWAPIN) && SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) {
10041 9718 /*
10042 9719 * The process is swapped out, so just set the new size
10043 9720 * code. When it swaps back in, we'll allocate a new one
10044 9721 * of the new chosen size.
10045 9722 */
10046 9723 curtsb->tsb_szc = szc;
10047 9724 return (TSB_SUCCESS);
10048 9725 }
10049 9726 SFMMU_FLAGS_SET(sfmmup, HAT_BUSY);
10050 9727
10051 9728 tte_sz_mask = old_tsbinfo->tsb_ttesz_mask;
10052 9729
10053 9730 /*
10054 9731 * All initialization is done inside of sfmmu_tsbinfo_alloc().
10055 9732 * If we fail to allocate a TSB, exit.
10056 9733 *
10057 9734 * If tsb grows with new tsb size > 4M and old tsb size < 4M,
10058 9735 * then try 4M slab after the initial alloc fails.
10059 9736 *
10060 9737 * If tsb swapin with tsb size > 4M, then try 4M after the
10061 9738 * initial alloc fails.
10062 9739 */
10063 9740 sfmmu_hat_exit(hatlockp);
10064 9741 if (sfmmu_tsbinfo_alloc(&new_tsbinfo, szc,
10065 9742 tte_sz_mask, flags, sfmmup) &&
10066 9743 (!(flags & (TSB_GROW | TSB_SWAPIN)) || (szc <= TSB_4M_SZCODE) ||
10067 9744 (!(flags & TSB_SWAPIN) &&
10068 9745 (old_tsbinfo->tsb_szc >= TSB_4M_SZCODE)) ||
10069 9746 sfmmu_tsbinfo_alloc(&new_tsbinfo, TSB_4M_SZCODE,
10070 9747 tte_sz_mask, flags, sfmmup))) {
10071 9748 (void) sfmmu_hat_enter(sfmmup);
10072 9749 if (!(flags & TSB_SWAPIN))
10073 9750 SFMMU_STAT(sf_tsb_resize_failures);
10074 9751 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY);
10075 9752 return (TSB_ALLOCFAIL);
10076 9753 }
10077 9754 (void) sfmmu_hat_enter(sfmmup);
10078 9755
10079 9756 /*
10080 9757 * Re-check to make sure somebody else didn't muck with us while we
10081 9758 * didn't hold the HAT lock. If the process swapped out, fine, just
10082 9759 * exit; this can happen if we try to shrink the TSB from the context
10083 9760 * of another process (such as on an ISM unmap), though it is rare.
10084 9761 */
10085 9762 if (!(flags & TSB_SWAPIN) && SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) {
10086 9763 SFMMU_STAT(sf_tsb_resize_failures);
10087 9764 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY);
10088 9765 sfmmu_hat_exit(hatlockp);
10089 9766 sfmmu_tsbinfo_free(new_tsbinfo);
10090 9767 (void) sfmmu_hat_enter(sfmmup);
10091 9768 return (TSB_LOSTRACE);
10092 9769 }
10093 9770
10094 9771 #ifdef DEBUG
10095 9772 /* Reverify that the tsb_info still exists.. for debugging only */
10096 9773 for (prevtsb = NULL, curtsb = sfmmup->sfmmu_tsb;
10097 9774 curtsb != old_tsbinfo && curtsb != NULL;
10098 9775 prevtsb = curtsb, curtsb = curtsb->tsb_next)
10099 9776 ;
10100 9777 ASSERT(curtsb != NULL);
10101 9778 #endif /* DEBUG */
10102 9779
10103 9780 /*
10104 9781 * Quiesce any CPUs running this process on their next TLB miss
10105 9782 * so they atomically see the new tsb_info. We temporarily set the
10106 9783 * context to invalid context so new threads that come on processor
10107 9784 * after we do the xcall to cpusran will also serialize behind the
10108 9785 * HAT lock on TLB miss and will see the new TSB. Since this short
10109 9786 * race with a new thread coming on processor is relatively rare,
10110 9787 * this synchronization mechanism should be cheaper than always
10111 9788 * pausing all CPUs for the duration of the setup, which is what
10112 9789 * the old implementation did. This is particuarly true if we are
10113 9790 * copying a huge chunk of memory around during that window.
10114 9791 *
10115 9792 * The memory barriers are to make sure things stay consistent
10116 9793 * with resume() since it does not hold the HAT lock while
10117 9794 * walking the list of tsb_info structures.
10118 9795 */
10119 9796 if ((flags & TSB_SWAPIN) != TSB_SWAPIN) {
10120 9797 /* The TSB is either growing or shrinking. */
10121 9798 sfmmu_invalidate_ctx(sfmmup);
10122 9799 } else {
10123 9800 /*
10124 9801 * It is illegal to swap in TSBs from a process other
10125 9802 * than a process being swapped in. This in turn
10126 9803 * implies we do not have a valid MMU context here
10127 9804 * since a process needs one to resolve translation
10128 9805 * misses.
10129 9806 */
10130 9807 ASSERT(curthread->t_procp->p_as->a_hat == sfmmup);
10131 9808 }
10132 9809
10133 9810 #ifdef DEBUG
10134 9811 ASSERT(max_mmu_ctxdoms > 0);
10135 9812
10136 9813 /*
10137 9814 * Process should have INVALID_CONTEXT on all MMUs
10138 9815 */
10139 9816 for (i = 0; i < max_mmu_ctxdoms; i++) {
10140 9817
10141 9818 ASSERT(sfmmup->sfmmu_ctxs[i].cnum == INVALID_CONTEXT);
10142 9819 }
10143 9820 #endif
10144 9821
10145 9822 new_tsbinfo->tsb_next = old_tsbinfo->tsb_next;
10146 9823 membar_stst(); /* strict ordering required */
10147 9824 if (prevtsb)
10148 9825 prevtsb->tsb_next = new_tsbinfo;
10149 9826 else
10150 9827 sfmmup->sfmmu_tsb = new_tsbinfo;
10151 9828 membar_enter(); /* make sure new TSB globally visible */
10152 9829
10153 9830 /*
10154 9831 * We need to migrate TSB entries from the old TSB to the new TSB
10155 9832 * if tsb_remap_ttes is set and the TSB is growing.
10156 9833 */
10157 9834 if (tsb_remap_ttes && ((flags & TSB_GROW) == TSB_GROW))
10158 9835 sfmmu_copy_tsb(old_tsbinfo, new_tsbinfo);
10159 9836
10160 9837 SFMMU_FLAGS_CLEAR(sfmmup, HAT_BUSY);
10161 9838
10162 9839 /*
10163 9840 * Drop the HAT lock to free our old tsb_info.
10164 9841 */
10165 9842 sfmmu_hat_exit(hatlockp);
10166 9843
10167 9844 if ((flags & TSB_GROW) == TSB_GROW) {
10168 9845 SFMMU_STAT(sf_tsb_grow);
10169 9846 } else if ((flags & TSB_SHRINK) == TSB_SHRINK) {
10170 9847 SFMMU_STAT(sf_tsb_shrink);
10171 9848 }
10172 9849
10173 9850 sfmmu_tsbinfo_free(old_tsbinfo);
10174 9851
10175 9852 (void) sfmmu_hat_enter(sfmmup);
10176 9853 return (TSB_SUCCESS);
10177 9854 }
10178 9855
10179 9856 /*
10180 9857 * This function will re-program hat pgsz array, and invalidate the
10181 9858 * process' context, forcing the process to switch to another
10182 9859 * context on the next TLB miss, and therefore start using the
10183 9860 * TLB that is reprogrammed for the new page sizes.
10184 9861 */
10185 9862 void
10186 9863 sfmmu_reprog_pgsz_arr(sfmmu_t *sfmmup, uint8_t *tmp_pgsz)
10187 9864 {
10188 9865 int i;
10189 9866 hatlock_t *hatlockp = NULL;
10190 9867
10191 9868 hatlockp = sfmmu_hat_enter(sfmmup);
10192 9869 /* USIII+-IV+ optimization, requires hat lock */
10193 9870 if (tmp_pgsz) {
10194 9871 for (i = 0; i < mmu_page_sizes; i++)
10195 9872 sfmmup->sfmmu_pgsz[i] = tmp_pgsz[i];
10196 9873 }
10197 9874 SFMMU_STAT(sf_tlb_reprog_pgsz);
10198 9875
10199 9876 sfmmu_invalidate_ctx(sfmmup);
10200 9877
10201 9878 sfmmu_hat_exit(hatlockp);
10202 9879 }
10203 9880
10204 9881 /*
10205 9882 * The scd_rttecnt field in the SCD must be updated to take account of the
10206 9883 * regions which it contains.
10207 9884 */
10208 9885 static void
10209 9886 sfmmu_set_scd_rttecnt(sf_srd_t *srdp, sf_scd_t *scdp)
10210 9887 {
10211 9888 uint_t rid;
10212 9889 uint_t i, j;
10213 9890 ulong_t w;
10214 9891 sf_region_t *rgnp;
10215 9892
10216 9893 ASSERT(srdp != NULL);
10217 9894
10218 9895 for (i = 0; i < SFMMU_HMERGNMAP_WORDS; i++) {
10219 9896 if ((w = scdp->scd_region_map.bitmap[i]) == 0) {
10220 9897 continue;
10221 9898 }
10222 9899
10223 9900 j = 0;
10224 9901 while (w) {
10225 9902 if (!(w & 0x1)) {
10226 9903 j++;
10227 9904 w >>= 1;
10228 9905 continue;
10229 9906 }
10230 9907 rid = (i << BT_ULSHIFT) | j;
10231 9908 j++;
10232 9909 w >>= 1;
10233 9910
10234 9911 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
10235 9912 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
10236 9913 rgnp = srdp->srd_hmergnp[rid];
10237 9914 ASSERT(rgnp->rgn_refcnt > 0);
10238 9915 ASSERT(rgnp->rgn_id == rid);
10239 9916
10240 9917 scdp->scd_rttecnt[rgnp->rgn_pgszc] +=
10241 9918 rgnp->rgn_size >> TTE_PAGE_SHIFT(rgnp->rgn_pgszc);
10242 9919
10243 9920 /*
10244 9921 * Maintain the tsb0 inflation cnt for the regions
10245 9922 * in the SCD.
10246 9923 */
10247 9924 if (rgnp->rgn_pgszc >= TTE4M) {
10248 9925 scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt +=
10249 9926 rgnp->rgn_size >>
10250 9927 (TTE_PAGE_SHIFT(TTE8K) + 2);
10251 9928 }
10252 9929 }
10253 9930 }
10254 9931 }
10255 9932
10256 9933 /*
10257 9934 * This function assumes that there are either four or six supported page
10258 9935 * sizes and at most two programmable TLBs, so we need to decide which
10259 9936 * page sizes are most important and then tell the MMU layer so it
10260 9937 * can adjust the TLB page sizes accordingly (if supported).
10261 9938 *
10262 9939 * If these assumptions change, this function will need to be
10263 9940 * updated to support whatever the new limits are.
10264 9941 *
10265 9942 * The growing flag is nonzero if we are growing the address space,
10266 9943 * and zero if it is shrinking. This allows us to decide whether
10267 9944 * to grow or shrink our TSB, depending upon available memory
10268 9945 * conditions.
10269 9946 */
10270 9947 static void
10271 9948 sfmmu_check_page_sizes(sfmmu_t *sfmmup, int growing)
10272 9949 {
10273 9950 uint64_t ttecnt[MMU_PAGE_SIZES];
10274 9951 uint64_t tte8k_cnt, tte4m_cnt;
10275 9952 uint8_t i;
10276 9953 int sectsb_thresh;
10277 9954
10278 9955 /*
10279 9956 * Kernel threads, processes with small address spaces not using
10280 9957 * large pages, and dummy ISM HATs need not apply.
10281 9958 */
10282 9959 if (sfmmup == ksfmmup || sfmmup->sfmmu_ismhat != NULL)
10283 9960 return;
10284 9961
10285 9962 if (!SFMMU_LGPGS_INUSE(sfmmup) &&
10286 9963 sfmmup->sfmmu_ttecnt[TTE8K] <= tsb_rss_factor)
10287 9964 return;
10288 9965
10289 9966 for (i = 0; i < mmu_page_sizes; i++) {
10290 9967 ttecnt[i] = sfmmup->sfmmu_ttecnt[i] +
10291 9968 sfmmup->sfmmu_ismttecnt[i];
10292 9969 }
10293 9970
10294 9971 /* Check pagesizes in use, and possibly reprogram DTLB. */
10295 9972 if (&mmu_check_page_sizes)
10296 9973 mmu_check_page_sizes(sfmmup, ttecnt);
10297 9974
10298 9975 /*
10299 9976 * Calculate the number of 8k ttes to represent the span of these
10300 9977 * pages.
10301 9978 */
10302 9979 tte8k_cnt = ttecnt[TTE8K] +
10303 9980 (ttecnt[TTE64K] << (MMU_PAGESHIFT64K - MMU_PAGESHIFT)) +
10304 9981 (ttecnt[TTE512K] << (MMU_PAGESHIFT512K - MMU_PAGESHIFT));
10305 9982 if (mmu_page_sizes == max_mmu_page_sizes) {
10306 9983 tte4m_cnt = ttecnt[TTE4M] +
10307 9984 (ttecnt[TTE32M] << (MMU_PAGESHIFT32M - MMU_PAGESHIFT4M)) +
10308 9985 (ttecnt[TTE256M] << (MMU_PAGESHIFT256M - MMU_PAGESHIFT4M));
10309 9986 } else {
10310 9987 tte4m_cnt = ttecnt[TTE4M];
10311 9988 }
10312 9989
10313 9990 /*
10314 9991 * Inflate tte8k_cnt to allow for region large page allocation failure.
10315 9992 */
10316 9993 tte8k_cnt += sfmmup->sfmmu_tsb0_4minflcnt;
10317 9994
10318 9995 /*
10319 9996 * Inflate TSB sizes by a factor of 2 if this process
10320 9997 * uses 4M text pages to minimize extra conflict misses
10321 9998 * in the first TSB since without counting text pages
10322 9999 * 8K TSB may become too small.
10323 10000 *
10324 10001 * Also double the size of the second TSB to minimize
10325 10002 * extra conflict misses due to competition between 4M text pages
10326 10003 * and data pages.
10327 10004 *
10328 10005 * We need to adjust the second TSB allocation threshold by the
10329 10006 * inflation factor, since there is no point in creating a second
10330 10007 * TSB when we know all the mappings can fit in the I/D TLBs.
10331 10008 */
10332 10009 sectsb_thresh = tsb_sectsb_threshold;
10333 10010 if (sfmmup->sfmmu_flags & HAT_4MTEXT_FLAG) {
10334 10011 tte8k_cnt <<= 1;
10335 10012 tte4m_cnt <<= 1;
10336 10013 sectsb_thresh <<= 1;
10337 10014 }
10338 10015
10339 10016 /*
10340 10017 * Check to see if our TSB is the right size; we may need to
10341 10018 * grow or shrink it. If the process is small, our work is
10342 10019 * finished at this point.
10343 10020 */
10344 10021 if (tte8k_cnt <= tsb_rss_factor && tte4m_cnt <= sectsb_thresh) {
10345 10022 return;
10346 10023 }
10347 10024 sfmmu_size_tsb(sfmmup, growing, tte8k_cnt, tte4m_cnt, sectsb_thresh);
10348 10025 }
10349 10026
10350 10027 static void
10351 10028 sfmmu_size_tsb(sfmmu_t *sfmmup, int growing, uint64_t tte8k_cnt,
10352 10029 uint64_t tte4m_cnt, int sectsb_thresh)
10353 10030 {
10354 10031 int tsb_bits;
10355 10032 uint_t tsb_szc;
10356 10033 struct tsb_info *tsbinfop;
10357 10034 hatlock_t *hatlockp = NULL;
10358 10035
10359 10036 hatlockp = sfmmu_hat_enter(sfmmup);
10360 10037 ASSERT(hatlockp != NULL);
10361 10038 tsbinfop = sfmmup->sfmmu_tsb;
10362 10039 ASSERT(tsbinfop != NULL);
10363 10040
10364 10041 /*
10365 10042 * If we're growing, select the size based on RSS. If we're
10366 10043 * shrinking, leave some room so we don't have to turn around and
10367 10044 * grow again immediately.
10368 10045 */
10369 10046 if (growing)
10370 10047 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt);
10371 10048 else
10372 10049 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt << 1);
10373 10050
10374 10051 if (!growing && (tsb_szc < tsbinfop->tsb_szc) &&
10375 10052 (tsb_szc >= default_tsb_size) && TSB_OK_SHRINK()) {
10376 10053 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, tsb_szc,
10377 10054 hatlockp, TSB_SHRINK);
10378 10055 } else if (growing && tsb_szc > tsbinfop->tsb_szc && TSB_OK_GROW()) {
10379 10056 (void) sfmmu_replace_tsb(sfmmup, tsbinfop, tsb_szc,
10380 10057 hatlockp, TSB_GROW);
10381 10058 }
10382 10059 tsbinfop = sfmmup->sfmmu_tsb;
10383 10060
10384 10061 /*
10385 10062 * With the TLB and first TSB out of the way, we need to see if
10386 10063 * we need a second TSB for 4M pages. If we managed to reprogram
10387 10064 * the TLB page sizes above, the process will start using this new
10388 10065 * TSB right away; otherwise, it will start using it on the next
10389 10066 * context switch. Either way, it's no big deal so there's no
10390 10067 * synchronization with the trap handlers here unless we grow the
10391 10068 * TSB (in which case it's required to prevent using the old one
10392 10069 * after it's freed). Note: second tsb is required for 32M/256M
10393 10070 * page sizes.
10394 10071 */
10395 10072 if (tte4m_cnt > sectsb_thresh) {
10396 10073 /*
10397 10074 * If we're growing, select the size based on RSS. If we're
10398 10075 * shrinking, leave some room so we don't have to turn
10399 10076 * around and grow again immediately.
10400 10077 */
10401 10078 if (growing)
10402 10079 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt);
10403 10080 else
10404 10081 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt << 1);
10405 10082 if (tsbinfop->tsb_next == NULL) {
10406 10083 struct tsb_info *newtsb;
10407 10084 int allocflags = SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)?
10408 10085 0 : TSB_ALLOC;
10409 10086
10410 10087 sfmmu_hat_exit(hatlockp);
10411 10088
10412 10089 /*
10413 10090 * Try to allocate a TSB for 4[32|256]M pages. If we
10414 10091 * can't get the size we want, retry w/a minimum sized
10415 10092 * TSB. If that still didn't work, give up; we can
10416 10093 * still run without one.
10417 10094 */
10418 10095 tsb_bits = (mmu_page_sizes == max_mmu_page_sizes)?
10419 10096 TSB4M|TSB32M|TSB256M:TSB4M;
10420 10097 if ((sfmmu_tsbinfo_alloc(&newtsb, tsb_szc, tsb_bits,
10421 10098 allocflags, sfmmup)) &&
10422 10099 (tsb_szc <= TSB_4M_SZCODE ||
10423 10100 sfmmu_tsbinfo_alloc(&newtsb, TSB_4M_SZCODE,
10424 10101 tsb_bits, allocflags, sfmmup)) &&
10425 10102 sfmmu_tsbinfo_alloc(&newtsb, TSB_MIN_SZCODE,
10426 10103 tsb_bits, allocflags, sfmmup)) {
10427 10104 return;
10428 10105 }
10429 10106
10430 10107 hatlockp = sfmmu_hat_enter(sfmmup);
10431 10108
10432 10109 sfmmu_invalidate_ctx(sfmmup);
10433 10110
10434 10111 if (sfmmup->sfmmu_tsb->tsb_next == NULL) {
10435 10112 sfmmup->sfmmu_tsb->tsb_next = newtsb;
10436 10113 SFMMU_STAT(sf_tsb_sectsb_create);
10437 10114 sfmmu_hat_exit(hatlockp);
10438 10115 return;
10439 10116 } else {
10440 10117 /*
10441 10118 * It's annoying, but possible for us
10442 10119 * to get here.. we dropped the HAT lock
10443 10120 * because of locking order in the kmem
10444 10121 * allocator, and while we were off getting
10445 10122 * our memory, some other thread decided to
10446 10123 * do us a favor and won the race to get a
10447 10124 * second TSB for this process. Sigh.
10448 10125 */
10449 10126 sfmmu_hat_exit(hatlockp);
10450 10127 sfmmu_tsbinfo_free(newtsb);
10451 10128 return;
10452 10129 }
10453 10130 }
10454 10131
10455 10132 /*
10456 10133 * We have a second TSB, see if it's big enough.
10457 10134 */
10458 10135 tsbinfop = tsbinfop->tsb_next;
10459 10136
10460 10137 /*
10461 10138 * Check to see if our second TSB is the right size;
10462 10139 * we may need to grow or shrink it.
10463 10140 * To prevent thrashing (e.g. growing the TSB on a
10464 10141 * subsequent map operation), only try to shrink if
10465 10142 * the TSB reach exceeds twice the virtual address
10466 10143 * space size.
10467 10144 */
10468 10145 if (!growing && (tsb_szc < tsbinfop->tsb_szc) &&
10469 10146 (tsb_szc >= default_tsb_size) && TSB_OK_SHRINK()) {
10470 10147 (void) sfmmu_replace_tsb(sfmmup, tsbinfop,
10471 10148 tsb_szc, hatlockp, TSB_SHRINK);
10472 10149 } else if (growing && tsb_szc > tsbinfop->tsb_szc &&
10473 10150 TSB_OK_GROW()) {
10474 10151 (void) sfmmu_replace_tsb(sfmmup, tsbinfop,
10475 10152 tsb_szc, hatlockp, TSB_GROW);
10476 10153 }
10477 10154 }
10478 10155
10479 10156 sfmmu_hat_exit(hatlockp);
10480 10157 }
10481 10158
10482 10159 /*
10483 10160 * Free up a sfmmu
10484 10161 * Since the sfmmu is currently embedded in the hat struct we simply zero
10485 10162 * out our fields and free up the ism map blk list if any.
10486 10163 */
10487 10164 static void
10488 10165 sfmmu_free_sfmmu(sfmmu_t *sfmmup)
10489 10166 {
10490 10167 ism_blk_t *blkp, *nx_blkp;
10491 10168 #ifdef DEBUG
10492 10169 ism_map_t *map;
10493 10170 int i;
10494 10171 #endif
10495 10172
10496 10173 ASSERT(sfmmup->sfmmu_ttecnt[TTE8K] == 0);
10497 10174 ASSERT(sfmmup->sfmmu_ttecnt[TTE64K] == 0);
10498 10175 ASSERT(sfmmup->sfmmu_ttecnt[TTE512K] == 0);
10499 10176 ASSERT(sfmmup->sfmmu_ttecnt[TTE4M] == 0);
10500 10177 ASSERT(sfmmup->sfmmu_ttecnt[TTE32M] == 0);
10501 10178 ASSERT(sfmmup->sfmmu_ttecnt[TTE256M] == 0);
10502 10179 ASSERT(SF_RGNMAP_ISNULL(sfmmup));
10503 10180
10504 10181 sfmmup->sfmmu_free = 0;
10505 10182 sfmmup->sfmmu_ismhat = 0;
10506 10183
10507 10184 blkp = sfmmup->sfmmu_iblk;
10508 10185 sfmmup->sfmmu_iblk = NULL;
10509 10186
10510 10187 while (blkp) {
10511 10188 #ifdef DEBUG
10512 10189 map = blkp->iblk_maps;
10513 10190 for (i = 0; i < ISM_MAP_SLOTS; i++) {
10514 10191 ASSERT(map[i].imap_seg == 0);
10515 10192 ASSERT(map[i].imap_ismhat == NULL);
10516 10193 ASSERT(map[i].imap_ment == NULL);
10517 10194 }
10518 10195 #endif
10519 10196 nx_blkp = blkp->iblk_next;
10520 10197 blkp->iblk_next = NULL;
10521 10198 blkp->iblk_nextpa = (uint64_t)-1;
10522 10199 kmem_cache_free(ism_blk_cache, blkp);
10523 10200 blkp = nx_blkp;
10524 10201 }
10525 10202 }
10526 10203
10527 10204 /*
10528 10205 * Locking primitves accessed by HATLOCK macros
10529 10206 */
10530 10207
10531 10208 #define SFMMU_SPL_MTX (0x0)
10532 10209 #define SFMMU_ML_MTX (0x1)
10533 10210
10534 10211 #define SFMMU_MLSPL_MTX(type, pg) (((type) == SFMMU_SPL_MTX) ? \
10535 10212 SPL_HASH(pg) : MLIST_HASH(pg))
10536 10213
10537 10214 kmutex_t *
10538 10215 sfmmu_page_enter(struct page *pp)
10539 10216 {
10540 10217 return (sfmmu_mlspl_enter(pp, SFMMU_SPL_MTX));
10541 10218 }
10542 10219
10543 10220 void
10544 10221 sfmmu_page_exit(kmutex_t *spl)
10545 10222 {
10546 10223 mutex_exit(spl);
10547 10224 }
10548 10225
10549 10226 int
10550 10227 sfmmu_page_spl_held(struct page *pp)
10551 10228 {
10552 10229 return (sfmmu_mlspl_held(pp, SFMMU_SPL_MTX));
10553 10230 }
10554 10231
10555 10232 kmutex_t *
10556 10233 sfmmu_mlist_enter(struct page *pp)
10557 10234 {
10558 10235 return (sfmmu_mlspl_enter(pp, SFMMU_ML_MTX));
10559 10236 }
10560 10237
10561 10238 void
10562 10239 sfmmu_mlist_exit(kmutex_t *mml)
10563 10240 {
10564 10241 mutex_exit(mml);
10565 10242 }
10566 10243
10567 10244 int
10568 10245 sfmmu_mlist_held(struct page *pp)
10569 10246 {
10570 10247
10571 10248 return (sfmmu_mlspl_held(pp, SFMMU_ML_MTX));
10572 10249 }
10573 10250
10574 10251 /*
10575 10252 * Common code for sfmmu_mlist_enter() and sfmmu_page_enter(). For
10576 10253 * sfmmu_mlist_enter() case mml_table lock array is used and for
10577 10254 * sfmmu_page_enter() sfmmu_page_lock lock array is used.
10578 10255 *
10579 10256 * The lock is taken on a root page so that it protects an operation on all
10580 10257 * constituent pages of a large page pp belongs to.
10581 10258 *
10582 10259 * The routine takes a lock from the appropriate array. The lock is determined
10583 10260 * by hashing the root page. After taking the lock this routine checks if the
10584 10261 * root page has the same size code that was used to determine the root (i.e
10585 10262 * that root hasn't changed). If root page has the expected p_szc field we
10586 10263 * have the right lock and it's returned to the caller. If root's p_szc
10587 10264 * decreased we release the lock and retry from the beginning. This case can
10588 10265 * happen due to hat_page_demote() decreasing p_szc between our load of p_szc
10589 10266 * value and taking the lock. The number of retries due to p_szc decrease is
10590 10267 * limited by the maximum p_szc value. If p_szc is 0 we return the lock
10591 10268 * determined by hashing pp itself.
10592 10269 *
10593 10270 * If our caller doesn't hold a SE_SHARED or SE_EXCL lock on pp it's also
10594 10271 * possible that p_szc can increase. To increase p_szc a thread has to lock
10595 10272 * all constituent pages EXCL and do hat_pageunload() on all of them. All the
10596 10273 * callers that don't hold a page locked recheck if hmeblk through which pp
10597 10274 * was found still maps this pp. If it doesn't map it anymore returned lock
10598 10275 * is immediately dropped. Therefore if sfmmu_mlspl_enter() hits the case of
10599 10276 * p_szc increase after taking the lock it returns this lock without further
10600 10277 * retries because in this case the caller doesn't care about which lock was
10601 10278 * taken. The caller will drop it right away.
10602 10279 *
10603 10280 * After the routine returns it's guaranteed that hat_page_demote() can't
10604 10281 * change p_szc field of any of constituent pages of a large page pp belongs
10605 10282 * to as long as pp was either locked at least SHARED prior to this call or
10606 10283 * the caller finds that hment that pointed to this pp still references this
10607 10284 * pp (this also assumes that the caller holds hme hash bucket lock so that
10608 10285 * the same pp can't be remapped into the same hmeblk after it was unmapped by
10609 10286 * hat_pageunload()).
10610 10287 */
10611 10288 static kmutex_t *
10612 10289 sfmmu_mlspl_enter(struct page *pp, int type)
10613 10290 {
10614 10291 kmutex_t *mtx;
10615 10292 uint_t prev_rszc = UINT_MAX;
10616 10293 page_t *rootpp;
10617 10294 uint_t szc;
10618 10295 uint_t rszc;
10619 10296 uint_t pszc = pp->p_szc;
10620 10297
10621 10298 ASSERT(pp != NULL);
10622 10299
10623 10300 again:
10624 10301 if (pszc == 0) {
10625 10302 mtx = SFMMU_MLSPL_MTX(type, pp);
10626 10303 mutex_enter(mtx);
10627 10304 return (mtx);
10628 10305 }
10629 10306
10630 10307 /* The lock lives in the root page */
10631 10308 rootpp = PP_GROUPLEADER(pp, pszc);
10632 10309 mtx = SFMMU_MLSPL_MTX(type, rootpp);
10633 10310 mutex_enter(mtx);
10634 10311
10635 10312 /*
10636 10313 * Return mml in the following 3 cases:
10637 10314 *
10638 10315 * 1) If pp itself is root since if its p_szc decreased before we took
10639 10316 * the lock pp is still the root of smaller szc page. And if its p_szc
10640 10317 * increased it doesn't matter what lock we return (see comment in
10641 10318 * front of this routine).
10642 10319 *
10643 10320 * 2) If pp's not root but rootpp is the root of a rootpp->p_szc size
10644 10321 * large page we have the right lock since any previous potential
10645 10322 * hat_page_demote() is done demoting from greater than current root's
10646 10323 * p_szc because hat_page_demote() changes root's p_szc last. No
10647 10324 * further hat_page_demote() can start or be in progress since it
10648 10325 * would need the same lock we currently hold.
10649 10326 *
10650 10327 * 3) If rootpp's p_szc increased since previous iteration it doesn't
10651 10328 * matter what lock we return (see comment in front of this routine).
10652 10329 */
10653 10330 if (pp == rootpp || (rszc = rootpp->p_szc) == pszc ||
10654 10331 rszc >= prev_rszc) {
10655 10332 return (mtx);
10656 10333 }
10657 10334
10658 10335 /*
10659 10336 * hat_page_demote() could have decreased root's p_szc.
10660 10337 * In this case pp's p_szc must also be smaller than pszc.
10661 10338 * Retry.
10662 10339 */
10663 10340 if (rszc < pszc) {
10664 10341 szc = pp->p_szc;
10665 10342 if (szc < pszc) {
10666 10343 mutex_exit(mtx);
10667 10344 pszc = szc;
10668 10345 goto again;
10669 10346 }
10670 10347 /*
10671 10348 * pp's p_szc increased after it was decreased.
10672 10349 * page cannot be mapped. Return current lock. The caller
10673 10350 * will drop it right away.
10674 10351 */
10675 10352 return (mtx);
10676 10353 }
10677 10354
10678 10355 /*
10679 10356 * root's p_szc is greater than pp's p_szc.
10680 10357 * hat_page_demote() is not done with all pages
10681 10358 * yet. Wait for it to complete.
10682 10359 */
10683 10360 mutex_exit(mtx);
10684 10361 rootpp = PP_GROUPLEADER(rootpp, rszc);
10685 10362 mtx = SFMMU_MLSPL_MTX(type, rootpp);
10686 10363 mutex_enter(mtx);
10687 10364 mutex_exit(mtx);
10688 10365 prev_rszc = rszc;
10689 10366 goto again;
10690 10367 }
10691 10368
10692 10369 static int
10693 10370 sfmmu_mlspl_held(struct page *pp, int type)
10694 10371 {
10695 10372 kmutex_t *mtx;
10696 10373
10697 10374 ASSERT(pp != NULL);
10698 10375 /* The lock lives in the root page */
10699 10376 pp = PP_PAGEROOT(pp);
10700 10377 ASSERT(pp != NULL);
10701 10378
10702 10379 mtx = SFMMU_MLSPL_MTX(type, pp);
10703 10380 return (MUTEX_HELD(mtx));
10704 10381 }
10705 10382
10706 10383 static uint_t
10707 10384 sfmmu_get_free_hblk(struct hme_blk **hmeblkpp, uint_t critical)
10708 10385 {
10709 10386 struct hme_blk *hblkp;
10710 10387
10711 10388
10712 10389 if (freehblkp != NULL) {
10713 10390 mutex_enter(&freehblkp_lock);
10714 10391 if (freehblkp != NULL) {
10715 10392 /*
10716 10393 * If the current thread is owning hblk_reserve OR
10717 10394 * critical request from sfmmu_hblk_steal()
10718 10395 * let it succeed even if freehblkcnt is really low.
10719 10396 */
10720 10397 if (freehblkcnt <= HBLK_RESERVE_MIN && !critical) {
10721 10398 SFMMU_STAT(sf_get_free_throttle);
10722 10399 mutex_exit(&freehblkp_lock);
10723 10400 return (0);
10724 10401 }
10725 10402 freehblkcnt--;
10726 10403 *hmeblkpp = freehblkp;
10727 10404 hblkp = *hmeblkpp;
10728 10405 freehblkp = hblkp->hblk_next;
10729 10406 mutex_exit(&freehblkp_lock);
10730 10407 hblkp->hblk_next = NULL;
10731 10408 SFMMU_STAT(sf_get_free_success);
10732 10409
10733 10410 ASSERT(hblkp->hblk_hmecnt == 0);
10734 10411 ASSERT(hblkp->hblk_vcnt == 0);
10735 10412 ASSERT(hblkp->hblk_nextpa == va_to_pa((caddr_t)hblkp));
10736 10413
10737 10414 return (1);
10738 10415 }
10739 10416 mutex_exit(&freehblkp_lock);
10740 10417 }
10741 10418
10742 10419 /* Check cpu hblk pending queues */
10743 10420 if ((*hmeblkpp = sfmmu_check_pending_hblks(TTE8K)) != NULL) {
10744 10421 hblkp = *hmeblkpp;
10745 10422 hblkp->hblk_next = NULL;
10746 10423 hblkp->hblk_nextpa = va_to_pa((caddr_t)hblkp);
10747 10424
10748 10425 ASSERT(hblkp->hblk_hmecnt == 0);
10749 10426 ASSERT(hblkp->hblk_vcnt == 0);
10750 10427
10751 10428 return (1);
10752 10429 }
10753 10430
10754 10431 SFMMU_STAT(sf_get_free_fail);
10755 10432 return (0);
10756 10433 }
10757 10434
10758 10435 static uint_t
10759 10436 sfmmu_put_free_hblk(struct hme_blk *hmeblkp, uint_t critical)
10760 10437 {
10761 10438 struct hme_blk *hblkp;
10762 10439
10763 10440 ASSERT(hmeblkp->hblk_hmecnt == 0);
10764 10441 ASSERT(hmeblkp->hblk_vcnt == 0);
10765 10442 ASSERT(hmeblkp->hblk_nextpa == va_to_pa((caddr_t)hmeblkp));
10766 10443
10767 10444 /*
10768 10445 * If the current thread is mapping into kernel space,
10769 10446 * let it succede even if freehblkcnt is max
10770 10447 * so that it will avoid freeing it to kmem.
10771 10448 * This will prevent stack overflow due to
10772 10449 * possible recursion since kmem_cache_free()
10773 10450 * might require creation of a slab which
10774 10451 * in turn needs an hmeblk to map that slab;
10775 10452 * let's break this vicious chain at the first
10776 10453 * opportunity.
10777 10454 */
10778 10455 if (freehblkcnt < HBLK_RESERVE_CNT || critical) {
10779 10456 mutex_enter(&freehblkp_lock);
10780 10457 if (freehblkcnt < HBLK_RESERVE_CNT || critical) {
10781 10458 SFMMU_STAT(sf_put_free_success);
10782 10459 freehblkcnt++;
10783 10460 hmeblkp->hblk_next = freehblkp;
10784 10461 freehblkp = hmeblkp;
10785 10462 mutex_exit(&freehblkp_lock);
10786 10463 return (1);
10787 10464 }
10788 10465 mutex_exit(&freehblkp_lock);
10789 10466 }
10790 10467
10791 10468 /*
10792 10469 * Bring down freehblkcnt to HBLK_RESERVE_CNT. We are here
10793 10470 * only if freehblkcnt is at least HBLK_RESERVE_CNT *and*
10794 10471 * we are not in the process of mapping into kernel space.
10795 10472 */
10796 10473 ASSERT(!critical);
10797 10474 while (freehblkcnt > HBLK_RESERVE_CNT) {
10798 10475 mutex_enter(&freehblkp_lock);
10799 10476 if (freehblkcnt > HBLK_RESERVE_CNT) {
10800 10477 freehblkcnt--;
10801 10478 hblkp = freehblkp;
10802 10479 freehblkp = hblkp->hblk_next;
10803 10480 mutex_exit(&freehblkp_lock);
10804 10481 ASSERT(get_hblk_cache(hblkp) == sfmmu8_cache);
10805 10482 kmem_cache_free(sfmmu8_cache, hblkp);
10806 10483 continue;
10807 10484 }
10808 10485 mutex_exit(&freehblkp_lock);
10809 10486 }
10810 10487 SFMMU_STAT(sf_put_free_fail);
10811 10488 return (0);
10812 10489 }
10813 10490
10814 10491 static void
10815 10492 sfmmu_hblk_swap(struct hme_blk *new)
10816 10493 {
10817 10494 struct hme_blk *old, *hblkp, *prev;
10818 10495 uint64_t newpa;
10819 10496 caddr_t base, vaddr, endaddr;
10820 10497 struct hmehash_bucket *hmebp;
10821 10498 struct sf_hment *osfhme, *nsfhme;
10822 10499 page_t *pp;
10823 10500 kmutex_t *pml;
10824 10501 tte_t tte;
10825 10502 struct hme_blk *list = NULL;
10826 10503
10827 10504 #ifdef DEBUG
10828 10505 hmeblk_tag hblktag;
10829 10506 struct hme_blk *found;
10830 10507 #endif
10831 10508 old = HBLK_RESERVE;
10832 10509 ASSERT(!old->hblk_shared);
10833 10510
10834 10511 /*
10835 10512 * save pa before bcopy clobbers it
10836 10513 */
10837 10514 newpa = new->hblk_nextpa;
10838 10515
10839 10516 base = (caddr_t)get_hblk_base(old);
10840 10517 endaddr = base + get_hblk_span(old);
10841 10518
10842 10519 /*
10843 10520 * acquire hash bucket lock.
10844 10521 */
10845 10522 hmebp = sfmmu_tteload_acquire_hashbucket(ksfmmup, base, TTE8K,
10846 10523 SFMMU_INVALID_SHMERID);
10847 10524
10848 10525 /*
10849 10526 * copy contents from old to new
10850 10527 */
10851 10528 bcopy((void *)old, (void *)new, HME8BLK_SZ);
10852 10529
10853 10530 /*
10854 10531 * add new to hash chain
10855 10532 */
10856 10533 sfmmu_hblk_hash_add(hmebp, new, newpa);
10857 10534
10858 10535 /*
10859 10536 * search hash chain for hblk_reserve; this needs to be performed
10860 10537 * after adding new, otherwise prev won't correspond to the hblk which
10861 10538 * is prior to old in hash chain when we call sfmmu_hblk_hash_rm to
10862 10539 * remove old later.
10863 10540 */
10864 10541 for (prev = NULL,
10865 10542 hblkp = hmebp->hmeblkp; hblkp != NULL && hblkp != old;
10866 10543 prev = hblkp, hblkp = hblkp->hblk_next)
10867 10544 ;
10868 10545
10869 10546 if (hblkp != old)
10870 10547 panic("sfmmu_hblk_swap: hblk_reserve not found");
10871 10548
10872 10549 /*
10873 10550 * p_mapping list is still pointing to hments in hblk_reserve;
10874 10551 * fix up p_mapping list so that they point to hments in new.
10875 10552 *
10876 10553 * Since all these mappings are created by hblk_reserve_thread
10877 10554 * on the way and it's using at least one of the buffers from each of
10878 10555 * the newly minted slabs, there is no danger of any of these
10879 10556 * mappings getting unloaded by another thread.
10880 10557 *
10881 10558 * tsbmiss could only modify ref/mod bits of hments in old/new.
10882 10559 * Since all of these hments hold mappings established by segkmem
10883 10560 * and mappings in segkmem are setup with HAT_NOSYNC, ref/mod bits
10884 10561 * have no meaning for the mappings in hblk_reserve. hments in
10885 10562 * old and new are identical except for ref/mod bits.
10886 10563 */
10887 10564 for (vaddr = base; vaddr < endaddr; vaddr += TTEBYTES(TTE8K)) {
10888 10565
10889 10566 HBLKTOHME(osfhme, old, vaddr);
10890 10567 sfmmu_copytte(&osfhme->hme_tte, &tte);
10891 10568
10892 10569 if (TTE_IS_VALID(&tte)) {
10893 10570 if ((pp = osfhme->hme_page) == NULL)
10894 10571 panic("sfmmu_hblk_swap: page not mapped");
10895 10572
10896 10573 pml = sfmmu_mlist_enter(pp);
10897 10574
10898 10575 if (pp != osfhme->hme_page)
10899 10576 panic("sfmmu_hblk_swap: mapping changed");
10900 10577
10901 10578 HBLKTOHME(nsfhme, new, vaddr);
10902 10579
10903 10580 HME_ADD(nsfhme, pp);
10904 10581 HME_SUB(osfhme, pp);
10905 10582
10906 10583 sfmmu_mlist_exit(pml);
10907 10584 }
10908 10585 }
10909 10586
10910 10587 /*
10911 10588 * remove old from hash chain
10912 10589 */
10913 10590 sfmmu_hblk_hash_rm(hmebp, old, prev, &list, 1);
10914 10591
10915 10592 #ifdef DEBUG
10916 10593
10917 10594 hblktag.htag_id = ksfmmup;
10918 10595 hblktag.htag_rid = SFMMU_INVALID_SHMERID;
10919 10596 hblktag.htag_bspage = HME_HASH_BSPAGE(base, HME_HASH_SHIFT(TTE8K));
10920 10597 hblktag.htag_rehash = HME_HASH_REHASH(TTE8K);
10921 10598 HME_HASH_FAST_SEARCH(hmebp, hblktag, found);
10922 10599
10923 10600 if (found != new)
10924 10601 panic("sfmmu_hblk_swap: new hblk not found");
10925 10602 #endif
10926 10603
10927 10604 SFMMU_HASH_UNLOCK(hmebp);
10928 10605
10929 10606 /*
10930 10607 * Reset hblk_reserve
10931 10608 */
10932 10609 bzero((void *)old, HME8BLK_SZ);
10933 10610 old->hblk_nextpa = va_to_pa((caddr_t)old);
10934 10611 }
10935 10612
10936 10613 /*
10937 10614 * Grab the mlist mutex for both pages passed in.
10938 10615 *
10939 10616 * low and high will be returned as pointers to the mutexes for these pages.
10940 10617 * low refers to the mutex residing in the lower bin of the mlist hash, while
10941 10618 * high refers to the mutex residing in the higher bin of the mlist hash. This
10942 10619 * is due to the locking order restrictions on the same thread grabbing
10943 10620 * multiple mlist mutexes. The low lock must be acquired before the high lock.
10944 10621 *
10945 10622 * If both pages hash to the same mutex, only grab that single mutex, and
10946 10623 * high will be returned as NULL
10947 10624 * If the pages hash to different bins in the hash, grab the lower addressed
10948 10625 * lock first and then the higher addressed lock in order to follow the locking
10949 10626 * rules involved with the same thread grabbing multiple mlist mutexes.
10950 10627 * low and high will both have non-NULL values.
10951 10628 */
10952 10629 static void
10953 10630 sfmmu_mlist_reloc_enter(struct page *targ, struct page *repl,
10954 10631 kmutex_t **low, kmutex_t **high)
10955 10632 {
10956 10633 kmutex_t *mml_targ, *mml_repl;
10957 10634
10958 10635 /*
10959 10636 * no need to do the dance around szc as in sfmmu_mlist_enter()
10960 10637 * because this routine is only called by hat_page_relocate() and all
10961 10638 * targ and repl pages are already locked EXCL so szc can't change.
10962 10639 */
10963 10640
10964 10641 mml_targ = MLIST_HASH(PP_PAGEROOT(targ));
10965 10642 mml_repl = MLIST_HASH(PP_PAGEROOT(repl));
10966 10643
10967 10644 if (mml_targ == mml_repl) {
10968 10645 *low = mml_targ;
10969 10646 *high = NULL;
10970 10647 } else {
10971 10648 if (mml_targ < mml_repl) {
10972 10649 *low = mml_targ;
10973 10650 *high = mml_repl;
10974 10651 } else {
10975 10652 *low = mml_repl;
10976 10653 *high = mml_targ;
10977 10654 }
10978 10655 }
10979 10656
10980 10657 mutex_enter(*low);
10981 10658 if (*high)
10982 10659 mutex_enter(*high);
10983 10660 }
10984 10661
10985 10662 static void
10986 10663 sfmmu_mlist_reloc_exit(kmutex_t *low, kmutex_t *high)
10987 10664 {
10988 10665 if (high)
10989 10666 mutex_exit(high);
10990 10667 mutex_exit(low);
10991 10668 }
10992 10669
10993 10670 static hatlock_t *
10994 10671 sfmmu_hat_enter(sfmmu_t *sfmmup)
10995 10672 {
10996 10673 hatlock_t *hatlockp;
10997 10674
10998 10675 if (sfmmup != ksfmmup) {
10999 10676 hatlockp = TSB_HASH(sfmmup);
11000 10677 mutex_enter(HATLOCK_MUTEXP(hatlockp));
11001 10678 return (hatlockp);
11002 10679 }
11003 10680 return (NULL);
11004 10681 }
11005 10682
11006 10683 static hatlock_t *
11007 10684 sfmmu_hat_tryenter(sfmmu_t *sfmmup)
11008 10685 {
11009 10686 hatlock_t *hatlockp;
11010 10687
11011 10688 if (sfmmup != ksfmmup) {
11012 10689 hatlockp = TSB_HASH(sfmmup);
11013 10690 if (mutex_tryenter(HATLOCK_MUTEXP(hatlockp)) == 0)
11014 10691 return (NULL);
11015 10692 return (hatlockp);
11016 10693 }
11017 10694 return (NULL);
11018 10695 }
11019 10696
11020 10697 static void
11021 10698 sfmmu_hat_exit(hatlock_t *hatlockp)
11022 10699 {
11023 10700 if (hatlockp != NULL)
11024 10701 mutex_exit(HATLOCK_MUTEXP(hatlockp));
11025 10702 }
11026 10703
11027 10704 static void
11028 10705 sfmmu_hat_lock_all(void)
11029 10706 {
11030 10707 int i;
11031 10708 for (i = 0; i < SFMMU_NUM_LOCK; i++)
11032 10709 mutex_enter(HATLOCK_MUTEXP(&hat_lock[i]));
11033 10710 }
11034 10711
11035 10712 static void
11036 10713 sfmmu_hat_unlock_all(void)
11037 10714 {
11038 10715 int i;
11039 10716 for (i = SFMMU_NUM_LOCK - 1; i >= 0; i--)
11040 10717 mutex_exit(HATLOCK_MUTEXP(&hat_lock[i]));
11041 10718 }
11042 10719
11043 10720 int
11044 10721 sfmmu_hat_lock_held(sfmmu_t *sfmmup)
11045 10722 {
11046 10723 ASSERT(sfmmup != ksfmmup);
11047 10724 return (MUTEX_HELD(HATLOCK_MUTEXP(TSB_HASH(sfmmup))));
11048 10725 }
11049 10726
11050 10727 /*
11051 10728 * Locking primitives to provide consistency between ISM unmap
11052 10729 * and other operations. Since ISM unmap can take a long time, we
11053 10730 * use HAT_ISMBUSY flag (protected by the hatlock) to avoid creating
11054 10731 * contention on the hatlock buckets while ISM segments are being
11055 10732 * unmapped. The tradeoff is that the flags don't prevent priority
11056 10733 * inversion from occurring, so we must request kernel priority in
11057 10734 * case we have to sleep to keep from getting buried while holding
11058 10735 * the HAT_ISMBUSY flag set, which in turn could block other kernel
11059 10736 * threads from running (for example, in sfmmu_uvatopfn()).
11060 10737 */
11061 10738 static void
11062 10739 sfmmu_ismhat_enter(sfmmu_t *sfmmup, int hatlock_held)
11063 10740 {
11064 10741 hatlock_t *hatlockp;
11065 10742
11066 10743 THREAD_KPRI_REQUEST();
11067 10744 if (!hatlock_held)
11068 10745 hatlockp = sfmmu_hat_enter(sfmmup);
11069 10746 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY))
11070 10747 cv_wait(&sfmmup->sfmmu_tsb_cv, HATLOCK_MUTEXP(hatlockp));
11071 10748 SFMMU_FLAGS_SET(sfmmup, HAT_ISMBUSY);
11072 10749 if (!hatlock_held)
11073 10750 sfmmu_hat_exit(hatlockp);
11074 10751 }
11075 10752
11076 10753 static void
11077 10754 sfmmu_ismhat_exit(sfmmu_t *sfmmup, int hatlock_held)
11078 10755 {
11079 10756 hatlock_t *hatlockp;
11080 10757
11081 10758 if (!hatlock_held)
11082 10759 hatlockp = sfmmu_hat_enter(sfmmup);
11083 10760 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY));
11084 10761 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ISMBUSY);
11085 10762 cv_broadcast(&sfmmup->sfmmu_tsb_cv);
11086 10763 if (!hatlock_held)
11087 10764 sfmmu_hat_exit(hatlockp);
11088 10765 THREAD_KPRI_RELEASE();
11089 10766 }
11090 10767
11091 10768 /*
11092 10769 *
11093 10770 * Algorithm:
11094 10771 *
11095 10772 * (1) if segkmem is not ready, allocate hblk from an array of pre-alloc'ed
11096 10773 * hblks.
11097 10774 *
11098 10775 * (2) if we are allocating an hblk for mapping a slab in sfmmu_cache,
11099 10776 *
11100 10777 * (a) try to return an hblk from reserve pool of free hblks;
11101 10778 * (b) if the reserve pool is empty, acquire hblk_reserve_lock
11102 10779 * and return hblk_reserve.
11103 10780 *
11104 10781 * (3) call kmem_cache_alloc() to allocate hblk;
11105 10782 *
11106 10783 * (a) if hblk_reserve_lock is held by the current thread,
11107 10784 * atomically replace hblk_reserve by the hblk that is
11108 10785 * returned by kmem_cache_alloc; release hblk_reserve_lock
11109 10786 * and call kmem_cache_alloc() again.
11110 10787 * (b) if reserve pool is not full, add the hblk that is
11111 10788 * returned by kmem_cache_alloc to reserve pool and
11112 10789 * call kmem_cache_alloc again.
11113 10790 *
11114 10791 */
11115 10792 static struct hme_blk *
11116 10793 sfmmu_hblk_alloc(sfmmu_t *sfmmup, caddr_t vaddr,
11117 10794 struct hmehash_bucket *hmebp, uint_t size, hmeblk_tag hblktag,
11118 10795 uint_t flags, uint_t rid)
11119 10796 {
11120 10797 struct hme_blk *hmeblkp = NULL;
11121 10798 struct hme_blk *newhblkp;
11122 10799 struct hme_blk *shw_hblkp = NULL;
11123 10800 struct kmem_cache *sfmmu_cache = NULL;
11124 10801 uint64_t hblkpa;
11125 10802 ulong_t index;
11126 10803 uint_t owner; /* set to 1 if using hblk_reserve */
11127 10804 uint_t forcefree;
11128 10805 int sleep;
11129 10806 sf_srd_t *srdp;
11130 10807 sf_region_t *rgnp;
11131 10808
11132 10809 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
11133 10810 ASSERT(hblktag.htag_rid == rid);
11134 10811 SFMMU_VALIDATE_HMERID(sfmmup, rid, vaddr, TTEBYTES(size));
11135 10812 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) ||
11136 10813 IS_P2ALIGNED(vaddr, TTEBYTES(size)));
11137 10814
11138 10815 /*
11139 10816 * If segkmem is not created yet, allocate from static hmeblks
11140 10817 * created at the end of startup_modules(). See the block comment
11141 10818 * in startup_modules() describing how we estimate the number of
11142 10819 * static hmeblks that will be needed during re-map.
11143 10820 */
11144 10821 if (!hblk_alloc_dynamic) {
11145 10822
11146 10823 ASSERT(!SFMMU_IS_SHMERID_VALID(rid));
11147 10824
11148 10825 if (size == TTE8K) {
11149 10826 index = nucleus_hblk8.index;
11150 10827 if (index >= nucleus_hblk8.len) {
11151 10828 /*
11152 10829 * If we panic here, see startup_modules() to
11153 10830 * make sure that we are calculating the
11154 10831 * number of hblk8's that we need correctly.
11155 10832 */
11156 10833 prom_panic("no nucleus hblk8 to allocate");
11157 10834 }
11158 10835 hmeblkp =
11159 10836 (struct hme_blk *)&nucleus_hblk8.list[index];
11160 10837 nucleus_hblk8.index++;
11161 10838 SFMMU_STAT(sf_hblk8_nalloc);
11162 10839 } else {
11163 10840 index = nucleus_hblk1.index;
11164 10841 if (nucleus_hblk1.index >= nucleus_hblk1.len) {
11165 10842 /*
11166 10843 * If we panic here, see startup_modules().
11167 10844 * Most likely you need to update the
11168 10845 * calculation of the number of hblk1 elements
11169 10846 * that the kernel needs to boot.
11170 10847 */
11171 10848 prom_panic("no nucleus hblk1 to allocate");
11172 10849 }
11173 10850 hmeblkp =
11174 10851 (struct hme_blk *)&nucleus_hblk1.list[index];
11175 10852 nucleus_hblk1.index++;
11176 10853 SFMMU_STAT(sf_hblk1_nalloc);
11177 10854 }
11178 10855
11179 10856 goto hblk_init;
11180 10857 }
11181 10858
11182 10859 SFMMU_HASH_UNLOCK(hmebp);
11183 10860
11184 10861 if (sfmmup != KHATID && !SFMMU_IS_SHMERID_VALID(rid)) {
11185 10862 if (mmu_page_sizes == max_mmu_page_sizes) {
11186 10863 if (size < TTE256M)
11187 10864 shw_hblkp = sfmmu_shadow_hcreate(sfmmup, vaddr,
11188 10865 size, flags);
11189 10866 } else {
11190 10867 if (size < TTE4M)
11191 10868 shw_hblkp = sfmmu_shadow_hcreate(sfmmup, vaddr,
11192 10869 size, flags);
11193 10870 }
11194 10871 } else if (SFMMU_IS_SHMERID_VALID(rid)) {
11195 10872 /*
11196 10873 * Shared hmes use per region bitmaps in rgn_hmeflag
11197 10874 * rather than shadow hmeblks to keep track of the
11198 10875 * mapping sizes which have been allocated for the region.
11199 10876 * Here we cleanup old invalid hmeblks with this rid,
11200 10877 * which may be left around by pageunload().
11201 10878 */
11202 10879 int ttesz;
11203 10880 caddr_t va;
11204 10881 caddr_t eva = vaddr + TTEBYTES(size);
11205 10882
11206 10883 ASSERT(sfmmup != KHATID);
11207 10884
11208 10885 srdp = sfmmup->sfmmu_srdp;
11209 10886 ASSERT(srdp != NULL && srdp->srd_refcnt != 0);
11210 10887 rgnp = srdp->srd_hmergnp[rid];
11211 10888 ASSERT(rgnp != NULL && rgnp->rgn_id == rid);
11212 10889 ASSERT(rgnp->rgn_refcnt != 0);
11213 10890 ASSERT(size <= rgnp->rgn_pgszc);
11214 10891
11215 10892 ttesz = HBLK_MIN_TTESZ;
11216 10893 do {
11217 10894 if (!(rgnp->rgn_hmeflags & (0x1 << ttesz))) {
11218 10895 continue;
11219 10896 }
11220 10897
11221 10898 if (ttesz > size && ttesz != HBLK_MIN_TTESZ) {
11222 10899 sfmmu_cleanup_rhblk(srdp, vaddr, rid, ttesz);
11223 10900 } else if (ttesz < size) {
11224 10901 for (va = vaddr; va < eva;
11225 10902 va += TTEBYTES(ttesz)) {
11226 10903 sfmmu_cleanup_rhblk(srdp, va, rid,
11227 10904 ttesz);
11228 10905 }
11229 10906 }
11230 10907 } while (++ttesz <= rgnp->rgn_pgszc);
11231 10908 }
11232 10909
11233 10910 fill_hblk:
11234 10911 owner = (hblk_reserve_thread == curthread) ? 1 : 0;
11235 10912
11236 10913 if (owner && size == TTE8K) {
11237 10914
11238 10915 ASSERT(!SFMMU_IS_SHMERID_VALID(rid));
11239 10916 /*
11240 10917 * We are really in a tight spot. We already own
11241 10918 * hblk_reserve and we need another hblk. In anticipation
11242 10919 * of this kind of scenario, we specifically set aside
11243 10920 * HBLK_RESERVE_MIN number of hblks to be used exclusively
11244 10921 * by owner of hblk_reserve.
11245 10922 */
11246 10923 SFMMU_STAT(sf_hblk_recurse_cnt);
11247 10924
11248 10925 if (!sfmmu_get_free_hblk(&hmeblkp, 1))
11249 10926 panic("sfmmu_hblk_alloc: reserve list is empty");
11250 10927
11251 10928 goto hblk_verify;
11252 10929 }
11253 10930
11254 10931 ASSERT(!owner);
11255 10932
11256 10933 if ((flags & HAT_NO_KALLOC) == 0) {
11257 10934
11258 10935 sfmmu_cache = ((size == TTE8K) ? sfmmu8_cache : sfmmu1_cache);
11259 10936 sleep = ((sfmmup == KHATID) ? KM_NOSLEEP : KM_SLEEP);
11260 10937
11261 10938 if ((hmeblkp = kmem_cache_alloc(sfmmu_cache, sleep)) == NULL) {
11262 10939 hmeblkp = sfmmu_hblk_steal(size);
11263 10940 } else {
11264 10941 /*
11265 10942 * if we are the owner of hblk_reserve,
11266 10943 * swap hblk_reserve with hmeblkp and
11267 10944 * start a fresh life. Hope things go
11268 10945 * better this time.
11269 10946 */
11270 10947 if (hblk_reserve_thread == curthread) {
11271 10948 ASSERT(sfmmu_cache == sfmmu8_cache);
11272 10949 sfmmu_hblk_swap(hmeblkp);
11273 10950 hblk_reserve_thread = NULL;
11274 10951 mutex_exit(&hblk_reserve_lock);
11275 10952 goto fill_hblk;
11276 10953 }
11277 10954 /*
11278 10955 * let's donate this hblk to our reserve list if
11279 10956 * we are not mapping kernel range
11280 10957 */
11281 10958 if (size == TTE8K && sfmmup != KHATID) {
11282 10959 if (sfmmu_put_free_hblk(hmeblkp, 0))
11283 10960 goto fill_hblk;
11284 10961 }
11285 10962 }
11286 10963 } else {
11287 10964 /*
11288 10965 * We are here to map the slab in sfmmu8_cache; let's
11289 10966 * check if we could tap our reserve list; if successful,
11290 10967 * this will avoid the pain of going thru sfmmu_hblk_swap
11291 10968 */
11292 10969 SFMMU_STAT(sf_hblk_slab_cnt);
11293 10970 if (!sfmmu_get_free_hblk(&hmeblkp, 0)) {
11294 10971 /*
11295 10972 * let's start hblk_reserve dance
11296 10973 */
11297 10974 SFMMU_STAT(sf_hblk_reserve_cnt);
11298 10975 owner = 1;
11299 10976 mutex_enter(&hblk_reserve_lock);
11300 10977 hmeblkp = HBLK_RESERVE;
11301 10978 hblk_reserve_thread = curthread;
11302 10979 }
11303 10980 }
11304 10981
11305 10982 hblk_verify:
11306 10983 ASSERT(hmeblkp != NULL);
11307 10984 set_hblk_sz(hmeblkp, size);
11308 10985 ASSERT(hmeblkp->hblk_nextpa == va_to_pa((caddr_t)hmeblkp));
11309 10986 SFMMU_HASH_LOCK(hmebp);
11310 10987 HME_HASH_FAST_SEARCH(hmebp, hblktag, newhblkp);
11311 10988 if (newhblkp != NULL) {
11312 10989 SFMMU_HASH_UNLOCK(hmebp);
11313 10990 if (hmeblkp != HBLK_RESERVE) {
11314 10991 /*
11315 10992 * This is really tricky!
11316 10993 *
11317 10994 * vmem_alloc(vmem_seg_arena)
11318 10995 * vmem_alloc(vmem_internal_arena)
11319 10996 * segkmem_alloc(heap_arena)
11320 10997 * vmem_alloc(heap_arena)
11321 10998 * page_create()
11322 10999 * hat_memload()
11323 11000 * kmem_cache_free()
11324 11001 * kmem_cache_alloc()
11325 11002 * kmem_slab_create()
11326 11003 * vmem_alloc(kmem_internal_arena)
11327 11004 * segkmem_alloc(heap_arena)
11328 11005 * vmem_alloc(heap_arena)
11329 11006 * page_create()
11330 11007 * hat_memload()
11331 11008 * kmem_cache_free()
11332 11009 * ...
11333 11010 *
11334 11011 * Thus, hat_memload() could call kmem_cache_free
11335 11012 * for enough number of times that we could easily
11336 11013 * hit the bottom of the stack or run out of reserve
11337 11014 * list of vmem_seg structs. So, we must donate
11338 11015 * this hblk to reserve list if it's allocated
11339 11016 * from sfmmu8_cache *and* mapping kernel range.
11340 11017 * We don't need to worry about freeing hmeblk1's
11341 11018 * to kmem since they don't map any kmem slabs.
11342 11019 *
11343 11020 * Note: When segkmem supports largepages, we must
11344 11021 * free hmeblk1's to reserve list as well.
11345 11022 */
11346 11023 forcefree = (sfmmup == KHATID) ? 1 : 0;
11347 11024 if (size == TTE8K &&
11348 11025 sfmmu_put_free_hblk(hmeblkp, forcefree)) {
11349 11026 goto re_verify;
11350 11027 }
11351 11028 ASSERT(sfmmup != KHATID);
11352 11029 kmem_cache_free(get_hblk_cache(hmeblkp), hmeblkp);
11353 11030 } else {
11354 11031 /*
11355 11032 * Hey! we don't need hblk_reserve any more.
11356 11033 */
11357 11034 ASSERT(owner);
11358 11035 hblk_reserve_thread = NULL;
11359 11036 mutex_exit(&hblk_reserve_lock);
11360 11037 owner = 0;
11361 11038 }
11362 11039 re_verify:
11363 11040 /*
11364 11041 * let's check if the goodies are still present
11365 11042 */
11366 11043 SFMMU_HASH_LOCK(hmebp);
11367 11044 HME_HASH_FAST_SEARCH(hmebp, hblktag, newhblkp);
11368 11045 if (newhblkp != NULL) {
11369 11046 /*
11370 11047 * return newhblkp if it's not hblk_reserve;
11371 11048 * if newhblkp is hblk_reserve, return it
11372 11049 * _only if_ we are the owner of hblk_reserve.
11373 11050 */
11374 11051 if (newhblkp != HBLK_RESERVE || owner) {
11375 11052 ASSERT(!SFMMU_IS_SHMERID_VALID(rid) ||
11376 11053 newhblkp->hblk_shared);
11377 11054 ASSERT(SFMMU_IS_SHMERID_VALID(rid) ||
11378 11055 !newhblkp->hblk_shared);
11379 11056 return (newhblkp);
11380 11057 } else {
11381 11058 /*
11382 11059 * we just hit hblk_reserve in the hash and
11383 11060 * we are not the owner of that;
11384 11061 *
11385 11062 * block until hblk_reserve_thread completes
11386 11063 * swapping hblk_reserve and try the dance
11387 11064 * once again.
11388 11065 */
11389 11066 SFMMU_HASH_UNLOCK(hmebp);
11390 11067 mutex_enter(&hblk_reserve_lock);
11391 11068 mutex_exit(&hblk_reserve_lock);
11392 11069 SFMMU_STAT(sf_hblk_reserve_hit);
11393 11070 goto fill_hblk;
11394 11071 }
11395 11072 } else {
11396 11073 /*
11397 11074 * it's no more! try the dance once again.
11398 11075 */
11399 11076 SFMMU_HASH_UNLOCK(hmebp);
11400 11077 goto fill_hblk;
11401 11078 }
11402 11079 }
11403 11080
11404 11081 hblk_init:
11405 11082 if (SFMMU_IS_SHMERID_VALID(rid)) {
11406 11083 uint16_t tteflag = 0x1 <<
11407 11084 ((size < HBLK_MIN_TTESZ) ? HBLK_MIN_TTESZ : size);
11408 11085
11409 11086 if (!(rgnp->rgn_hmeflags & tteflag)) {
11410 11087 atomic_or_16(&rgnp->rgn_hmeflags, tteflag);
11411 11088 }
11412 11089 hmeblkp->hblk_shared = 1;
11413 11090 } else {
11414 11091 hmeblkp->hblk_shared = 0;
11415 11092 }
11416 11093 set_hblk_sz(hmeblkp, size);
11417 11094 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
11418 11095 hmeblkp->hblk_next = (struct hme_blk *)NULL;
11419 11096 hmeblkp->hblk_tag = hblktag;
11420 11097 hmeblkp->hblk_shadow = shw_hblkp;
11421 11098 hblkpa = hmeblkp->hblk_nextpa;
11422 11099 hmeblkp->hblk_nextpa = HMEBLK_ENDPA;
11423 11100
11424 11101 ASSERT(get_hblk_ttesz(hmeblkp) == size);
11425 11102 ASSERT(get_hblk_span(hmeblkp) == HMEBLK_SPAN(size));
11426 11103 ASSERT(hmeblkp->hblk_hmecnt == 0);
11427 11104 ASSERT(hmeblkp->hblk_vcnt == 0);
11428 11105 ASSERT(hmeblkp->hblk_lckcnt == 0);
11429 11106 ASSERT(hblkpa == va_to_pa((caddr_t)hmeblkp));
11430 11107 sfmmu_hblk_hash_add(hmebp, hmeblkp, hblkpa);
11431 11108 return (hmeblkp);
11432 11109 }
11433 11110
11434 11111 /*
11435 11112 * This function cleans up the hme_blk and returns it to the free list.
11436 11113 */
11437 11114 /* ARGSUSED */
11438 11115 static void
11439 11116 sfmmu_hblk_free(struct hme_blk **listp)
11440 11117 {
11441 11118 struct hme_blk *hmeblkp, *next_hmeblkp;
11442 11119 int size;
11443 11120 uint_t critical;
11444 11121 uint64_t hblkpa;
11445 11122
11446 11123 ASSERT(*listp != NULL);
11447 11124
11448 11125 hmeblkp = *listp;
11449 11126 while (hmeblkp != NULL) {
11450 11127 next_hmeblkp = hmeblkp->hblk_next;
11451 11128 ASSERT(!hmeblkp->hblk_hmecnt);
11452 11129 ASSERT(!hmeblkp->hblk_vcnt);
11453 11130 ASSERT(!hmeblkp->hblk_lckcnt);
11454 11131 ASSERT(hmeblkp != (struct hme_blk *)hblk_reserve);
11455 11132 ASSERT(hmeblkp->hblk_shared == 0);
11456 11133 ASSERT(hmeblkp->hblk_shw_bit == 0);
11457 11134 ASSERT(hmeblkp->hblk_shadow == NULL);
11458 11135
11459 11136 hblkpa = va_to_pa((caddr_t)hmeblkp);
11460 11137 ASSERT(hblkpa != (uint64_t)-1);
11461 11138 critical = (hblktosfmmu(hmeblkp) == KHATID) ? 1 : 0;
11462 11139
11463 11140 size = get_hblk_ttesz(hmeblkp);
11464 11141 hmeblkp->hblk_next = NULL;
11465 11142 hmeblkp->hblk_nextpa = hblkpa;
11466 11143
11467 11144 if (hmeblkp->hblk_nuc_bit == 0) {
11468 11145
11469 11146 if (size != TTE8K ||
11470 11147 !sfmmu_put_free_hblk(hmeblkp, critical))
11471 11148 kmem_cache_free(get_hblk_cache(hmeblkp),
11472 11149 hmeblkp);
11473 11150 }
11474 11151 hmeblkp = next_hmeblkp;
11475 11152 }
11476 11153 }
11477 11154
11478 11155 #define BUCKETS_TO_SEARCH_BEFORE_UNLOAD 30
11479 11156 #define SFMMU_HBLK_STEAL_THRESHOLD 5
11480 11157
11481 11158 static uint_t sfmmu_hblk_steal_twice;
11482 11159 static uint_t sfmmu_hblk_steal_count, sfmmu_hblk_steal_unload_count;
11483 11160
11484 11161 /*
11485 11162 * Steal a hmeblk from user or kernel hme hash lists.
11486 11163 * For 8K tte grab one from reserve pool (freehblkp) before proceeding to
11487 11164 * steal and if we fail to steal after SFMMU_HBLK_STEAL_THRESHOLD attempts
11488 11165 * tap into critical reserve of freehblkp.
11489 11166 * Note: We remain looping in this routine until we find one.
11490 11167 */
11491 11168 static struct hme_blk *
11492 11169 sfmmu_hblk_steal(int size)
11493 11170 {
11494 11171 static struct hmehash_bucket *uhmehash_steal_hand = NULL;
11495 11172 struct hmehash_bucket *hmebp;
11496 11173 struct hme_blk *hmeblkp = NULL, *pr_hblk;
11497 11174 uint64_t hblkpa;
11498 11175 int i;
11499 11176 uint_t loop_cnt = 0, critical;
11500 11177
11501 11178 for (;;) {
11502 11179 /* Check cpu hblk pending queues */
11503 11180 if ((hmeblkp = sfmmu_check_pending_hblks(size)) != NULL) {
11504 11181 hmeblkp->hblk_nextpa = va_to_pa((caddr_t)hmeblkp);
11505 11182 ASSERT(hmeblkp->hblk_hmecnt == 0);
11506 11183 ASSERT(hmeblkp->hblk_vcnt == 0);
11507 11184 return (hmeblkp);
11508 11185 }
11509 11186
11510 11187 if (size == TTE8K) {
11511 11188 critical =
11512 11189 (++loop_cnt > SFMMU_HBLK_STEAL_THRESHOLD) ? 1 : 0;
11513 11190 if (sfmmu_get_free_hblk(&hmeblkp, critical))
11514 11191 return (hmeblkp);
11515 11192 }
11516 11193
11517 11194 hmebp = (uhmehash_steal_hand == NULL) ? uhme_hash :
11518 11195 uhmehash_steal_hand;
11519 11196 ASSERT(hmebp >= uhme_hash && hmebp <= &uhme_hash[UHMEHASH_SZ]);
11520 11197
11521 11198 for (i = 0; hmeblkp == NULL && i <= UHMEHASH_SZ +
11522 11199 BUCKETS_TO_SEARCH_BEFORE_UNLOAD; i++) {
11523 11200 SFMMU_HASH_LOCK(hmebp);
11524 11201 hmeblkp = hmebp->hmeblkp;
11525 11202 hblkpa = hmebp->hmeh_nextpa;
11526 11203 pr_hblk = NULL;
11527 11204 while (hmeblkp) {
11528 11205 /*
11529 11206 * check if it is a hmeblk that is not locked
11530 11207 * and not shared. skip shadow hmeblks with
11531 11208 * shadow_mask set i.e valid count non zero.
11532 11209 */
11533 11210 if ((get_hblk_ttesz(hmeblkp) == size) &&
11534 11211 (hmeblkp->hblk_shw_bit == 0 ||
11535 11212 hmeblkp->hblk_vcnt == 0) &&
11536 11213 (hmeblkp->hblk_lckcnt == 0)) {
11537 11214 /*
11538 11215 * there is a high probability that we
11539 11216 * will find a free one. search some
11540 11217 * buckets for a free hmeblk initially
11541 11218 * before unloading a valid hmeblk.
11542 11219 */
11543 11220 if ((hmeblkp->hblk_vcnt == 0 &&
11544 11221 hmeblkp->hblk_hmecnt == 0) || (i >=
11545 11222 BUCKETS_TO_SEARCH_BEFORE_UNLOAD)) {
11546 11223 if (sfmmu_steal_this_hblk(hmebp,
11547 11224 hmeblkp, hblkpa, pr_hblk)) {
11548 11225 /*
11549 11226 * Hblk is unloaded
11550 11227 * successfully
11551 11228 */
11552 11229 break;
11553 11230 }
11554 11231 }
11555 11232 }
11556 11233 pr_hblk = hmeblkp;
11557 11234 hblkpa = hmeblkp->hblk_nextpa;
11558 11235 hmeblkp = hmeblkp->hblk_next;
11559 11236 }
11560 11237
11561 11238 SFMMU_HASH_UNLOCK(hmebp);
11562 11239 if (hmebp++ == &uhme_hash[UHMEHASH_SZ])
11563 11240 hmebp = uhme_hash;
11564 11241 }
11565 11242 uhmehash_steal_hand = hmebp;
11566 11243
11567 11244 if (hmeblkp != NULL)
11568 11245 break;
11569 11246
11570 11247 /*
11571 11248 * in the worst case, look for a free one in the kernel
11572 11249 * hash table.
11573 11250 */
11574 11251 for (i = 0, hmebp = khme_hash; i <= KHMEHASH_SZ; i++) {
11575 11252 SFMMU_HASH_LOCK(hmebp);
11576 11253 hmeblkp = hmebp->hmeblkp;
11577 11254 hblkpa = hmebp->hmeh_nextpa;
11578 11255 pr_hblk = NULL;
11579 11256 while (hmeblkp) {
11580 11257 /*
11581 11258 * check if it is free hmeblk
11582 11259 */
11583 11260 if ((get_hblk_ttesz(hmeblkp) == size) &&
11584 11261 (hmeblkp->hblk_lckcnt == 0) &&
11585 11262 (hmeblkp->hblk_vcnt == 0) &&
11586 11263 (hmeblkp->hblk_hmecnt == 0)) {
11587 11264 if (sfmmu_steal_this_hblk(hmebp,
11588 11265 hmeblkp, hblkpa, pr_hblk)) {
11589 11266 break;
11590 11267 } else {
11591 11268 /*
11592 11269 * Cannot fail since we have
11593 11270 * hash lock.
11594 11271 */
11595 11272 panic("fail to steal?");
11596 11273 }
11597 11274 }
11598 11275
11599 11276 pr_hblk = hmeblkp;
11600 11277 hblkpa = hmeblkp->hblk_nextpa;
11601 11278 hmeblkp = hmeblkp->hblk_next;
11602 11279 }
11603 11280
11604 11281 SFMMU_HASH_UNLOCK(hmebp);
11605 11282 if (hmebp++ == &khme_hash[KHMEHASH_SZ])
11606 11283 hmebp = khme_hash;
11607 11284 }
11608 11285
11609 11286 if (hmeblkp != NULL)
11610 11287 break;
11611 11288 sfmmu_hblk_steal_twice++;
11612 11289 }
11613 11290 return (hmeblkp);
11614 11291 }
11615 11292
11616 11293 /*
11617 11294 * This routine does real work to prepare a hblk to be "stolen" by
11618 11295 * unloading the mappings, updating shadow counts ....
11619 11296 * It returns 1 if the block is ready to be reused (stolen), or 0
11620 11297 * means the block cannot be stolen yet- pageunload is still working
11621 11298 * on this hblk.
11622 11299 */
11623 11300 static int
11624 11301 sfmmu_steal_this_hblk(struct hmehash_bucket *hmebp, struct hme_blk *hmeblkp,
11625 11302 uint64_t hblkpa, struct hme_blk *pr_hblk)
11626 11303 {
11627 11304 int shw_size, vshift;
11628 11305 struct hme_blk *shw_hblkp;
11629 11306 caddr_t vaddr;
11630 11307 uint_t shw_mask, newshw_mask;
11631 11308 struct hme_blk *list = NULL;
11632 11309
11633 11310 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
11634 11311
11635 11312 /*
11636 11313 * check if the hmeblk is free, unload if necessary
11637 11314 */
11638 11315 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) {
11639 11316 sfmmu_t *sfmmup;
11640 11317 demap_range_t dmr;
11641 11318
11642 11319 sfmmup = hblktosfmmu(hmeblkp);
11643 11320 if (hmeblkp->hblk_shared || sfmmup->sfmmu_ismhat) {
11644 11321 return (0);
11645 11322 }
11646 11323 DEMAP_RANGE_INIT(sfmmup, &dmr);
11647 11324 (void) sfmmu_hblk_unload(sfmmup, hmeblkp,
11648 11325 (caddr_t)get_hblk_base(hmeblkp),
11649 11326 get_hblk_endaddr(hmeblkp), &dmr, HAT_UNLOAD);
11650 11327 DEMAP_RANGE_FLUSH(&dmr);
11651 11328 if (hmeblkp->hblk_vcnt || hmeblkp->hblk_hmecnt) {
11652 11329 /*
11653 11330 * Pageunload is working on the same hblk.
11654 11331 */
11655 11332 return (0);
11656 11333 }
11657 11334
11658 11335 sfmmu_hblk_steal_unload_count++;
11659 11336 }
11660 11337
11661 11338 ASSERT(hmeblkp->hblk_lckcnt == 0);
11662 11339 ASSERT(hmeblkp->hblk_vcnt == 0 && hmeblkp->hblk_hmecnt == 0);
11663 11340
11664 11341 sfmmu_hblk_hash_rm(hmebp, hmeblkp, pr_hblk, &list, 1);
11665 11342 hmeblkp->hblk_nextpa = hblkpa;
11666 11343
11667 11344 shw_hblkp = hmeblkp->hblk_shadow;
11668 11345 if (shw_hblkp) {
11669 11346 ASSERT(!hmeblkp->hblk_shared);
11670 11347 shw_size = get_hblk_ttesz(shw_hblkp);
11671 11348 vaddr = (caddr_t)get_hblk_base(hmeblkp);
11672 11349 vshift = vaddr_to_vshift(shw_hblkp->hblk_tag, vaddr, shw_size);
11673 11350 ASSERT(vshift < 8);
11674 11351 /*
11675 11352 * Atomically clear shadow mask bit
11676 11353 */
11677 11354 do {
11678 11355 shw_mask = shw_hblkp->hblk_shw_mask;
11679 11356 ASSERT(shw_mask & (1 << vshift));
11680 11357 newshw_mask = shw_mask & ~(1 << vshift);
11681 11358 newshw_mask = atomic_cas_32(&shw_hblkp->hblk_shw_mask,
11682 11359 shw_mask, newshw_mask);
11683 11360 } while (newshw_mask != shw_mask);
11684 11361 hmeblkp->hblk_shadow = NULL;
11685 11362 }
11686 11363
11687 11364 /*
11688 11365 * remove shadow bit if we are stealing an unused shadow hmeblk.
11689 11366 * sfmmu_hblk_alloc needs it that way, will set shadow bit later if
11690 11367 * we are indeed allocating a shadow hmeblk.
11691 11368 */
11692 11369 hmeblkp->hblk_shw_bit = 0;
11693 11370
11694 11371 if (hmeblkp->hblk_shared) {
11695 11372 sf_srd_t *srdp;
11696 11373 sf_region_t *rgnp;
11697 11374 uint_t rid;
11698 11375
11699 11376 srdp = hblktosrd(hmeblkp);
11700 11377 ASSERT(srdp != NULL && srdp->srd_refcnt != 0);
11701 11378 rid = hmeblkp->hblk_tag.htag_rid;
11702 11379 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
11703 11380 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
11704 11381 rgnp = srdp->srd_hmergnp[rid];
11705 11382 ASSERT(rgnp != NULL);
11706 11383 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid);
11707 11384 hmeblkp->hblk_shared = 0;
11708 11385 }
11709 11386
11710 11387 sfmmu_hblk_steal_count++;
11711 11388 SFMMU_STAT(sf_steal_count);
11712 11389
11713 11390 return (1);
11714 11391 }
11715 11392
11716 11393 struct hme_blk *
11717 11394 sfmmu_hmetohblk(struct sf_hment *sfhme)
11718 11395 {
11719 11396 struct hme_blk *hmeblkp;
11720 11397 struct sf_hment *sfhme0;
11721 11398 struct hme_blk *hblk_dummy = 0;
11722 11399
11723 11400 /*
11724 11401 * No dummy sf_hments, please.
11725 11402 */
11726 11403 ASSERT(sfhme->hme_tte.ll != 0);
11727 11404
11728 11405 sfhme0 = sfhme - sfhme->hme_tte.tte_hmenum;
11729 11406 hmeblkp = (struct hme_blk *)((uintptr_t)sfhme0 -
11730 11407 (uintptr_t)&hblk_dummy->hblk_hme[0]);
11731 11408
11732 11409 return (hmeblkp);
11733 11410 }
11734 11411
11735 11412 /*
11736 11413 * On swapin, get appropriately sized TSB(s) and clear the HAT_SWAPPED flag.
11737 11414 * If we can't get appropriately sized TSB(s), try for 8K TSB(s) using
11738 11415 * KM_SLEEP allocation.
11739 11416 *
11740 11417 * Return 0 on success, -1 otherwise.
11741 11418 */
11742 11419 static void
11743 11420 sfmmu_tsb_swapin(sfmmu_t *sfmmup, hatlock_t *hatlockp)
11744 11421 {
11745 11422 struct tsb_info *tsbinfop, *next;
11746 11423 tsb_replace_rc_t rc;
11747 11424 boolean_t gotfirst = B_FALSE;
11748 11425
11749 11426 ASSERT(sfmmup != ksfmmup);
11750 11427 ASSERT(sfmmu_hat_lock_held(sfmmup));
11751 11428
11752 11429 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPIN)) {
11753 11430 cv_wait(&sfmmup->sfmmu_tsb_cv, HATLOCK_MUTEXP(hatlockp));
11754 11431 }
11755 11432
11756 11433 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) {
11757 11434 SFMMU_FLAGS_SET(sfmmup, HAT_SWAPIN);
11758 11435 } else {
11759 11436 return;
11760 11437 }
11761 11438
11762 11439 ASSERT(sfmmup->sfmmu_tsb != NULL);
11763 11440
11764 11441 /*
11765 11442 * Loop over all tsbinfo's replacing them with ones that actually have
11766 11443 * a TSB. If any of the replacements ever fail, bail out of the loop.
11767 11444 */
11768 11445 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL; tsbinfop = next) {
11769 11446 ASSERT(tsbinfop->tsb_flags & TSB_SWAPPED);
11770 11447 next = tsbinfop->tsb_next;
11771 11448 rc = sfmmu_replace_tsb(sfmmup, tsbinfop, tsbinfop->tsb_szc,
11772 11449 hatlockp, TSB_SWAPIN);
11773 11450 if (rc != TSB_SUCCESS) {
11774 11451 break;
11775 11452 }
11776 11453 gotfirst = B_TRUE;
11777 11454 }
11778 11455
11779 11456 switch (rc) {
11780 11457 case TSB_SUCCESS:
11781 11458 SFMMU_FLAGS_CLEAR(sfmmup, HAT_SWAPPED|HAT_SWAPIN);
11782 11459 cv_broadcast(&sfmmup->sfmmu_tsb_cv);
11783 11460 return;
11784 11461 case TSB_LOSTRACE:
11785 11462 break;
11786 11463 case TSB_ALLOCFAIL:
11787 11464 break;
11788 11465 default:
11789 11466 panic("sfmmu_replace_tsb returned unrecognized failure code "
11790 11467 "%d", rc);
11791 11468 }
11792 11469
11793 11470 /*
11794 11471 * In this case, we failed to get one of our TSBs. If we failed to
11795 11472 * get the first TSB, get one of minimum size (8KB). Walk the list
11796 11473 * and throw away the tsbinfos, starting where the allocation failed;
11797 11474 * we can get by with just one TSB as long as we don't leave the
11798 11475 * SWAPPED tsbinfo structures lying around.
11799 11476 */
11800 11477 tsbinfop = sfmmup->sfmmu_tsb;
11801 11478 next = tsbinfop->tsb_next;
11802 11479 tsbinfop->tsb_next = NULL;
11803 11480
11804 11481 sfmmu_hat_exit(hatlockp);
11805 11482 for (tsbinfop = next; tsbinfop != NULL; tsbinfop = next) {
11806 11483 next = tsbinfop->tsb_next;
11807 11484 sfmmu_tsbinfo_free(tsbinfop);
11808 11485 }
11809 11486 hatlockp = sfmmu_hat_enter(sfmmup);
11810 11487
11811 11488 /*
11812 11489 * If we don't have any TSBs, get a single 8K TSB for 8K, 64K and 512K
11813 11490 * pages.
11814 11491 */
11815 11492 if (!gotfirst) {
11816 11493 tsbinfop = sfmmup->sfmmu_tsb;
11817 11494 rc = sfmmu_replace_tsb(sfmmup, tsbinfop, TSB_MIN_SZCODE,
11818 11495 hatlockp, TSB_SWAPIN | TSB_FORCEALLOC);
11819 11496 ASSERT(rc == TSB_SUCCESS);
11820 11497 }
11821 11498
11822 11499 SFMMU_FLAGS_CLEAR(sfmmup, HAT_SWAPPED|HAT_SWAPIN);
11823 11500 cv_broadcast(&sfmmup->sfmmu_tsb_cv);
11824 11501 }
11825 11502
11826 11503 static int
11827 11504 sfmmu_is_rgnva(sf_srd_t *srdp, caddr_t addr, ulong_t w, ulong_t bmw)
11828 11505 {
11829 11506 ulong_t bix = 0;
11830 11507 uint_t rid;
11831 11508 sf_region_t *rgnp;
11832 11509
11833 11510 ASSERT(srdp != NULL);
11834 11511 ASSERT(srdp->srd_refcnt != 0);
11835 11512
11836 11513 w <<= BT_ULSHIFT;
11837 11514 while (bmw) {
11838 11515 if (!(bmw & 0x1)) {
11839 11516 bix++;
11840 11517 bmw >>= 1;
11841 11518 continue;
11842 11519 }
11843 11520 rid = w | bix;
11844 11521 rgnp = srdp->srd_hmergnp[rid];
11845 11522 ASSERT(rgnp->rgn_refcnt > 0);
11846 11523 ASSERT(rgnp->rgn_id == rid);
11847 11524 if (addr < rgnp->rgn_saddr ||
11848 11525 addr >= (rgnp->rgn_saddr + rgnp->rgn_size)) {
11849 11526 bix++;
11850 11527 bmw >>= 1;
11851 11528 } else {
11852 11529 return (1);
11853 11530 }
11854 11531 }
11855 11532 return (0);
11856 11533 }
11857 11534
11858 11535 /*
11859 11536 * Handle exceptions for low level tsb_handler.
11860 11537 *
11861 11538 * There are many scenarios that could land us here:
11862 11539 *
11863 11540 * If the context is invalid we land here. The context can be invalid
11864 11541 * for 3 reasons: 1) we couldn't allocate a new context and now need to
11865 11542 * perform a wrap around operation in order to allocate a new context.
11866 11543 * 2) Context was invalidated to change pagesize programming 3) ISMs or
11867 11544 * TSBs configuration is changeing for this process and we are forced into
11868 11545 * here to do a syncronization operation. If the context is valid we can
11869 11546 * be here from window trap hanlder. In this case just call trap to handle
11870 11547 * the fault.
11871 11548 *
11872 11549 * Note that the process will run in INVALID_CONTEXT before
11873 11550 * faulting into here and subsequently loading the MMU registers
11874 11551 * (including the TSB base register) associated with this process.
11875 11552 * For this reason, the trap handlers must all test for
11876 11553 * INVALID_CONTEXT before attempting to access any registers other
11877 11554 * than the context registers.
11878 11555 */
11879 11556 void
11880 11557 sfmmu_tsbmiss_exception(struct regs *rp, uintptr_t tagaccess, uint_t traptype)
11881 11558 {
11882 11559 sfmmu_t *sfmmup, *shsfmmup;
11883 11560 uint_t ctxtype;
11884 11561 klwp_id_t lwp;
11885 11562 char lwp_save_state;
11886 11563 hatlock_t *hatlockp, *shatlockp;
11887 11564 struct tsb_info *tsbinfop;
11888 11565 struct tsbmiss *tsbmp;
11889 11566 sf_scd_t *scdp;
11890 11567
11891 11568 SFMMU_STAT(sf_tsb_exceptions);
11892 11569 SFMMU_MMU_STAT(mmu_tsb_exceptions);
11893 11570 sfmmup = astosfmmu(curthread->t_procp->p_as);
11894 11571 /*
11895 11572 * note that in sun4u, tagacces register contains ctxnum
11896 11573 * while sun4v passes ctxtype in the tagaccess register.
11897 11574 */
11898 11575 ctxtype = tagaccess & TAGACC_CTX_MASK;
11899 11576
11900 11577 ASSERT(sfmmup != ksfmmup && ctxtype != KCONTEXT);
11901 11578 ASSERT(sfmmup->sfmmu_ismhat == 0);
11902 11579 ASSERT(!SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED) ||
11903 11580 ctxtype == INVALID_CONTEXT);
11904 11581
11905 11582 if (ctxtype != INVALID_CONTEXT && traptype != T_DATA_PROT) {
11906 11583 /*
11907 11584 * We may land here because shme bitmap and pagesize
11908 11585 * flags are updated lazily in tsbmiss area on other cpus.
11909 11586 * If we detect here that tsbmiss area is out of sync with
11910 11587 * sfmmu update it and retry the trapped instruction.
11911 11588 * Otherwise call trap().
11912 11589 */
11913 11590 int ret = 0;
11914 11591 uchar_t tteflag_mask = (1 << TTE64K) | (1 << TTE8K);
11915 11592 caddr_t addr = (caddr_t)(tagaccess & TAGACC_VADDR_MASK);
11916 11593
11917 11594 /*
11918 11595 * Must set lwp state to LWP_SYS before
11919 11596 * trying to acquire any adaptive lock
11920 11597 */
11921 11598 lwp = ttolwp(curthread);
11922 11599 ASSERT(lwp);
11923 11600 lwp_save_state = lwp->lwp_state;
11924 11601 lwp->lwp_state = LWP_SYS;
11925 11602
11926 11603 hatlockp = sfmmu_hat_enter(sfmmup);
11927 11604 kpreempt_disable();
11928 11605 tsbmp = &tsbmiss_area[CPU->cpu_id];
11929 11606 ASSERT(sfmmup == tsbmp->usfmmup);
11930 11607 if (((tsbmp->uhat_tteflags ^ sfmmup->sfmmu_tteflags) &
11931 11608 ~tteflag_mask) ||
11932 11609 ((tsbmp->uhat_rtteflags ^ sfmmup->sfmmu_rtteflags) &
11933 11610 ~tteflag_mask)) {
11934 11611 tsbmp->uhat_tteflags = sfmmup->sfmmu_tteflags;
11935 11612 tsbmp->uhat_rtteflags = sfmmup->sfmmu_rtteflags;
11936 11613 ret = 1;
11937 11614 }
11938 11615 if (sfmmup->sfmmu_srdp != NULL) {
11939 11616 ulong_t *sm = sfmmup->sfmmu_hmeregion_map.bitmap;
11940 11617 ulong_t *tm = tsbmp->shmermap;
11941 11618 ulong_t i;
11942 11619 for (i = 0; i < SFMMU_HMERGNMAP_WORDS; i++) {
11943 11620 ulong_t d = tm[i] ^ sm[i];
11944 11621 if (d) {
11945 11622 if (d & sm[i]) {
11946 11623 if (!ret && sfmmu_is_rgnva(
11947 11624 sfmmup->sfmmu_srdp,
11948 11625 addr, i, d & sm[i])) {
11949 11626 ret = 1;
11950 11627 }
11951 11628 }
11952 11629 tm[i] = sm[i];
11953 11630 }
11954 11631 }
11955 11632 }
11956 11633 kpreempt_enable();
11957 11634 sfmmu_hat_exit(hatlockp);
11958 11635 lwp->lwp_state = lwp_save_state;
11959 11636 if (ret) {
11960 11637 return;
11961 11638 }
11962 11639 } else if (ctxtype == INVALID_CONTEXT) {
11963 11640 /*
11964 11641 * First, make sure we come out of here with a valid ctx,
11965 11642 * since if we don't get one we'll simply loop on the
11966 11643 * faulting instruction.
11967 11644 *
11968 11645 * If the ISM mappings are changing, the TSB is relocated,
11969 11646 * the process is swapped, the process is joining SCD or
11970 11647 * leaving SCD or shared regions we serialize behind the
11971 11648 * controlling thread with hat lock, sfmmu_flags and
11972 11649 * sfmmu_tsb_cv condition variable.
11973 11650 */
11974 11651
11975 11652 /*
11976 11653 * Must set lwp state to LWP_SYS before
11977 11654 * trying to acquire any adaptive lock
11978 11655 */
11979 11656 lwp = ttolwp(curthread);
11980 11657 ASSERT(lwp);
11981 11658 lwp_save_state = lwp->lwp_state;
11982 11659 lwp->lwp_state = LWP_SYS;
11983 11660
11984 11661 hatlockp = sfmmu_hat_enter(sfmmup);
11985 11662 retry:
11986 11663 if ((scdp = sfmmup->sfmmu_scdp) != NULL) {
11987 11664 shsfmmup = scdp->scd_sfmmup;
11988 11665 ASSERT(shsfmmup != NULL);
11989 11666
11990 11667 for (tsbinfop = shsfmmup->sfmmu_tsb; tsbinfop != NULL;
11991 11668 tsbinfop = tsbinfop->tsb_next) {
11992 11669 if (tsbinfop->tsb_flags & TSB_RELOC_FLAG) {
11993 11670 /* drop the private hat lock */
11994 11671 sfmmu_hat_exit(hatlockp);
11995 11672 /* acquire the shared hat lock */
11996 11673 shatlockp = sfmmu_hat_enter(shsfmmup);
11997 11674 /*
11998 11675 * recheck to see if anything changed
11999 11676 * after we drop the private hat lock.
12000 11677 */
12001 11678 if (sfmmup->sfmmu_scdp == scdp &&
12002 11679 shsfmmup == scdp->scd_sfmmup) {
12003 11680 sfmmu_tsb_chk_reloc(shsfmmup,
12004 11681 shatlockp);
12005 11682 }
12006 11683 sfmmu_hat_exit(shatlockp);
12007 11684 hatlockp = sfmmu_hat_enter(sfmmup);
12008 11685 goto retry;
12009 11686 }
12010 11687 }
12011 11688 }
12012 11689
12013 11690 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL;
12014 11691 tsbinfop = tsbinfop->tsb_next) {
12015 11692 if (tsbinfop->tsb_flags & TSB_RELOC_FLAG) {
12016 11693 cv_wait(&sfmmup->sfmmu_tsb_cv,
12017 11694 HATLOCK_MUTEXP(hatlockp));
12018 11695 goto retry;
12019 11696 }
12020 11697 }
12021 11698
12022 11699 /*
12023 11700 * Wait for ISM maps to be updated.
12024 11701 */
12025 11702 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)) {
12026 11703 cv_wait(&sfmmup->sfmmu_tsb_cv,
12027 11704 HATLOCK_MUTEXP(hatlockp));
12028 11705 goto retry;
12029 11706 }
12030 11707
12031 11708 /* Is this process joining an SCD? */
12032 11709 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)) {
12033 11710 /*
12034 11711 * Flush private TSB and setup shared TSB.
12035 11712 * sfmmu_finish_join_scd() does not drop the
12036 11713 * hat lock.
12037 11714 */
12038 11715 sfmmu_finish_join_scd(sfmmup);
12039 11716 SFMMU_FLAGS_CLEAR(sfmmup, HAT_JOIN_SCD);
12040 11717 }
12041 11718
12042 11719 /*
12043 11720 * If we're swapping in, get TSB(s). Note that we must do
12044 11721 * this before we get a ctx or load the MMU state. Once
12045 11722 * we swap in we have to recheck to make sure the TSB(s) and
12046 11723 * ISM mappings didn't change while we slept.
12047 11724 */
12048 11725 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_SWAPPED)) {
12049 11726 sfmmu_tsb_swapin(sfmmup, hatlockp);
12050 11727 goto retry;
12051 11728 }
12052 11729
12053 11730 sfmmu_get_ctx(sfmmup);
12054 11731
12055 11732 sfmmu_hat_exit(hatlockp);
12056 11733 /*
12057 11734 * Must restore lwp_state if not calling
12058 11735 * trap() for further processing. Restore
12059 11736 * it anyway.
12060 11737 */
12061 11738 lwp->lwp_state = lwp_save_state;
12062 11739 return;
12063 11740 }
12064 11741 trap(rp, (caddr_t)tagaccess, traptype, 0);
12065 11742 }
12066 11743
12067 11744 static void
12068 11745 sfmmu_tsb_chk_reloc(sfmmu_t *sfmmup, hatlock_t *hatlockp)
12069 11746 {
12070 11747 struct tsb_info *tp;
12071 11748
12072 11749 ASSERT(sfmmu_hat_lock_held(sfmmup));
12073 11750
12074 11751 for (tp = sfmmup->sfmmu_tsb; tp != NULL; tp = tp->tsb_next) {
12075 11752 if (tp->tsb_flags & TSB_RELOC_FLAG) {
12076 11753 cv_wait(&sfmmup->sfmmu_tsb_cv,
12077 11754 HATLOCK_MUTEXP(hatlockp));
12078 11755 break;
12079 11756 }
12080 11757 }
12081 11758 }
12082 11759
12083 11760 /*
12084 11761 * sfmmu_vatopfn_suspended is called from GET_TTE when TL=0 and
12085 11762 * TTE_SUSPENDED bit set in tte we block on aquiring a page lock
12086 11763 * rather than spinning to avoid send mondo timeouts with
12087 11764 * interrupts enabled. When the lock is acquired it is immediately
12088 11765 * released and we return back to sfmmu_vatopfn just after
12089 11766 * the GET_TTE call.
12090 11767 */
12091 11768 void
12092 11769 sfmmu_vatopfn_suspended(caddr_t vaddr, sfmmu_t *sfmmu, tte_t *ttep)
12093 11770 {
12094 11771 struct page **pp;
12095 11772
12096 11773 (void) as_pagelock(sfmmu->sfmmu_as, &pp, vaddr, TTE_CSZ(ttep), S_WRITE);
12097 11774 as_pageunlock(sfmmu->sfmmu_as, pp, vaddr, TTE_CSZ(ttep), S_WRITE);
12098 11775 }
12099 11776
12100 11777 /*
12101 11778 * sfmmu_tsbmiss_suspended is called from GET_TTE when TL>0 and
12102 11779 * TTE_SUSPENDED bit set in tte. We do this so that we can handle
12103 11780 * cross traps which cannot be handled while spinning in the
12104 11781 * trap handlers. Simply enter and exit the kpr_suspendlock spin
12105 11782 * mutex, which is held by the holder of the suspend bit, and then
12106 11783 * retry the trapped instruction after unwinding.
12107 11784 */
12108 11785 /*ARGSUSED*/
12109 11786 void
12110 11787 sfmmu_tsbmiss_suspended(struct regs *rp, uintptr_t tagacc, uint_t traptype)
12111 11788 {
12112 11789 ASSERT(curthread != kreloc_thread);
12113 11790 mutex_enter(&kpr_suspendlock);
12114 11791 mutex_exit(&kpr_suspendlock);
12115 11792 }
12116 11793
12117 11794 /*
12118 11795 * This routine could be optimized to reduce the number of xcalls by flushing
12119 11796 * the entire TLBs if region reference count is above some threshold but the
12120 11797 * tradeoff will depend on the size of the TLB. So for now flush the specific
12121 11798 * page a context at a time.
12122 11799 *
12123 11800 * If uselocks is 0 then it's called after all cpus were captured and all the
12124 11801 * hat locks were taken. In this case don't take the region lock by relying on
12125 11802 * the order of list region update operations in hat_join_region(),
12126 11803 * hat_leave_region() and hat_dup_region(). The ordering in those routines
12127 11804 * guarantees that list is always forward walkable and reaches active sfmmus
12128 11805 * regardless of where xc_attention() captures a cpu.
12129 11806 */
12130 11807 cpuset_t
12131 11808 sfmmu_rgntlb_demap(caddr_t addr, sf_region_t *rgnp,
12132 11809 struct hme_blk *hmeblkp, int uselocks)
12133 11810 {
12134 11811 sfmmu_t *sfmmup;
12135 11812 cpuset_t cpuset;
12136 11813 cpuset_t rcpuset;
12137 11814 hatlock_t *hatlockp;
12138 11815 uint_t rid = rgnp->rgn_id;
12139 11816 sf_rgn_link_t *rlink;
12140 11817 sf_scd_t *scdp;
12141 11818
12142 11819 ASSERT(hmeblkp->hblk_shared);
12143 11820 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
12144 11821 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
12145 11822
12146 11823 CPUSET_ZERO(rcpuset);
12147 11824 if (uselocks) {
12148 11825 mutex_enter(&rgnp->rgn_mutex);
12149 11826 }
12150 11827 sfmmup = rgnp->rgn_sfmmu_head;
12151 11828 while (sfmmup != NULL) {
12152 11829 if (uselocks) {
12153 11830 hatlockp = sfmmu_hat_enter(sfmmup);
12154 11831 }
12155 11832
12156 11833 /*
12157 11834 * When an SCD is created the SCD hat is linked on the sfmmu
12158 11835 * region lists for each hme region which is part of the
12159 11836 * SCD. If we find an SCD hat, when walking these lists,
12160 11837 * then we flush the shared TSBs, if we find a private hat,
12161 11838 * which is part of an SCD, but where the region
12162 11839 * is not part of the SCD then we flush the private TSBs.
12163 11840 */
12164 11841 if (!sfmmup->sfmmu_scdhat && sfmmup->sfmmu_scdp != NULL &&
12165 11842 !SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)) {
12166 11843 scdp = sfmmup->sfmmu_scdp;
12167 11844 if (SF_RGNMAP_TEST(scdp->scd_hmeregion_map, rid)) {
12168 11845 if (uselocks) {
12169 11846 sfmmu_hat_exit(hatlockp);
12170 11847 }
12171 11848 goto next;
12172 11849 }
12173 11850 }
12174 11851
12175 11852 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0);
12176 11853
12177 11854 kpreempt_disable();
12178 11855 cpuset = sfmmup->sfmmu_cpusran;
12179 11856 CPUSET_AND(cpuset, cpu_ready_set);
12180 11857 CPUSET_DEL(cpuset, CPU->cpu_id);
12181 11858 SFMMU_XCALL_STATS(sfmmup);
12182 11859 xt_some(cpuset, vtag_flushpage_tl1,
12183 11860 (uint64_t)addr, (uint64_t)sfmmup);
12184 11861 vtag_flushpage(addr, (uint64_t)sfmmup);
12185 11862 if (uselocks) {
12186 11863 sfmmu_hat_exit(hatlockp);
12187 11864 }
12188 11865 kpreempt_enable();
12189 11866 CPUSET_OR(rcpuset, cpuset);
12190 11867
12191 11868 next:
12192 11869 /* LINTED: constant in conditional context */
12193 11870 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 0, 0);
12194 11871 ASSERT(rlink != NULL);
12195 11872 sfmmup = rlink->next;
12196 11873 }
12197 11874 if (uselocks) {
12198 11875 mutex_exit(&rgnp->rgn_mutex);
12199 11876 }
12200 11877 return (rcpuset);
12201 11878 }
12202 11879
12203 11880 /*
12204 11881 * This routine takes an sfmmu pointer and the va for an adddress in an
12205 11882 * ISM region as input and returns the corresponding region id in ism_rid.
12206 11883 * The return value of 1 indicates that a region has been found and ism_rid
12207 11884 * is valid, otherwise 0 is returned.
12208 11885 */
12209 11886 static int
12210 11887 find_ism_rid(sfmmu_t *sfmmup, sfmmu_t *ism_sfmmup, caddr_t va, uint_t *ism_rid)
12211 11888 {
12212 11889 ism_blk_t *ism_blkp;
12213 11890 int i;
12214 11891 ism_map_t *ism_map;
12215 11892 #ifdef DEBUG
12216 11893 struct hat *ism_hatid;
12217 11894 #endif
12218 11895 ASSERT(sfmmu_hat_lock_held(sfmmup));
12219 11896
12220 11897 ism_blkp = sfmmup->sfmmu_iblk;
12221 11898 while (ism_blkp != NULL) {
12222 11899 ism_map = ism_blkp->iblk_maps;
12223 11900 for (i = 0; i < ISM_MAP_SLOTS && ism_map[i].imap_ismhat; i++) {
12224 11901 if ((va >= ism_start(ism_map[i])) &&
12225 11902 (va < ism_end(ism_map[i]))) {
12226 11903
12227 11904 *ism_rid = ism_map[i].imap_rid;
12228 11905 #ifdef DEBUG
12229 11906 ism_hatid = ism_map[i].imap_ismhat;
12230 11907 ASSERT(ism_hatid == ism_sfmmup);
12231 11908 ASSERT(ism_hatid->sfmmu_ismhat);
12232 11909 #endif
12233 11910 return (1);
12234 11911 }
12235 11912 }
12236 11913 ism_blkp = ism_blkp->iblk_next;
12237 11914 }
12238 11915 return (0);
12239 11916 }
12240 11917
12241 11918 /*
12242 11919 * Special routine to flush out ism mappings- TSBs, TLBs and D-caches.
12243 11920 * This routine may be called with all cpu's captured. Therefore, the
12244 11921 * caller is responsible for holding all locks and disabling kernel
12245 11922 * preemption.
12246 11923 */
12247 11924 /* ARGSUSED */
12248 11925 static void
12249 11926 sfmmu_ismtlbcache_demap(caddr_t addr, sfmmu_t *ism_sfmmup,
12250 11927 struct hme_blk *hmeblkp, pfn_t pfnum, int cache_flush_flag)
12251 11928 {
12252 11929 cpuset_t cpuset;
12253 11930 caddr_t va;
12254 11931 ism_ment_t *ment;
12255 11932 sfmmu_t *sfmmup;
12256 11933 #ifdef VAC
12257 11934 int vcolor;
12258 11935 #endif
12259 11936
12260 11937 sf_scd_t *scdp;
12261 11938 uint_t ism_rid;
12262 11939
12263 11940 ASSERT(!hmeblkp->hblk_shared);
12264 11941 /*
12265 11942 * Walk the ism_hat's mapping list and flush the page
12266 11943 * from every hat sharing this ism_hat. This routine
12267 11944 * may be called while all cpu's have been captured.
12268 11945 * Therefore we can't attempt to grab any locks. For now
12269 11946 * this means we will protect the ism mapping list under
12270 11947 * a single lock which will be grabbed by the caller.
12271 11948 * If hat_share/unshare scalibility becomes a performance
12272 11949 * problem then we may need to re-think ism mapping list locking.
12273 11950 */
12274 11951 ASSERT(ism_sfmmup->sfmmu_ismhat);
12275 11952 ASSERT(MUTEX_HELD(&ism_mlist_lock));
12276 11953 addr = addr - ISMID_STARTADDR;
12277 11954
12278 11955 for (ment = ism_sfmmup->sfmmu_iment; ment; ment = ment->iment_next) {
12279 11956
12280 11957 sfmmup = ment->iment_hat;
12281 11958
12282 11959 va = ment->iment_base_va;
12283 11960 va = (caddr_t)((uintptr_t)va + (uintptr_t)addr);
12284 11961
12285 11962 /*
12286 11963 * When an SCD is created the SCD hat is linked on the ism
12287 11964 * mapping lists for each ISM segment which is part of the
12288 11965 * SCD. If we find an SCD hat, when walking these lists,
12289 11966 * then we flush the shared TSBs, if we find a private hat,
12290 11967 * which is part of an SCD, but where the region
12291 11968 * corresponding to this va is not part of the SCD then we
12292 11969 * flush the private TSBs.
12293 11970 */
12294 11971 if (!sfmmup->sfmmu_scdhat && sfmmup->sfmmu_scdp != NULL &&
12295 11972 !SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD) &&
12296 11973 !SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY)) {
12297 11974 if (!find_ism_rid(sfmmup, ism_sfmmup, va,
12298 11975 &ism_rid)) {
12299 11976 cmn_err(CE_PANIC,
12300 11977 "can't find matching ISM rid!");
12301 11978 }
12302 11979
12303 11980 scdp = sfmmup->sfmmu_scdp;
12304 11981 if (SFMMU_IS_ISMRID_VALID(ism_rid) &&
12305 11982 SF_RGNMAP_TEST(scdp->scd_ismregion_map,
12306 11983 ism_rid)) {
12307 11984 continue;
12308 11985 }
12309 11986 }
12310 11987 SFMMU_UNLOAD_TSB(va, sfmmup, hmeblkp, 1);
12311 11988
12312 11989 cpuset = sfmmup->sfmmu_cpusran;
12313 11990 CPUSET_AND(cpuset, cpu_ready_set);
12314 11991 CPUSET_DEL(cpuset, CPU->cpu_id);
12315 11992 SFMMU_XCALL_STATS(sfmmup);
12316 11993 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)va,
12317 11994 (uint64_t)sfmmup);
12318 11995 vtag_flushpage(va, (uint64_t)sfmmup);
12319 11996
12320 11997 #ifdef VAC
12321 11998 /*
12322 11999 * Flush D$
12323 12000 * When flushing D$ we must flush all
12324 12001 * cpu's. See sfmmu_cache_flush().
12325 12002 */
12326 12003 if (cache_flush_flag == CACHE_FLUSH) {
12327 12004 cpuset = cpu_ready_set;
12328 12005 CPUSET_DEL(cpuset, CPU->cpu_id);
12329 12006
12330 12007 SFMMU_XCALL_STATS(sfmmup);
12331 12008 vcolor = addr_to_vcolor(va);
12332 12009 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor);
12333 12010 vac_flushpage(pfnum, vcolor);
12334 12011 }
12335 12012 #endif /* VAC */
12336 12013 }
12337 12014 }
12338 12015
12339 12016 /*
12340 12017 * Demaps the TSB, CPU caches, and flushes all TLBs on all CPUs of
12341 12018 * a particular virtual address and ctx. If noflush is set we do not
12342 12019 * flush the TLB/TSB. This function may or may not be called with the
12343 12020 * HAT lock held.
12344 12021 */
12345 12022 static void
12346 12023 sfmmu_tlbcache_demap(caddr_t addr, sfmmu_t *sfmmup, struct hme_blk *hmeblkp,
12347 12024 pfn_t pfnum, int tlb_noflush, int cpu_flag, int cache_flush_flag,
12348 12025 int hat_lock_held)
12349 12026 {
12350 12027 #ifdef VAC
12351 12028 int vcolor;
12352 12029 #endif
12353 12030 cpuset_t cpuset;
12354 12031 hatlock_t *hatlockp;
12355 12032
12356 12033 ASSERT(!hmeblkp->hblk_shared);
12357 12034
12358 12035 #if defined(lint) && !defined(VAC)
12359 12036 pfnum = pfnum;
12360 12037 cpu_flag = cpu_flag;
12361 12038 cache_flush_flag = cache_flush_flag;
12362 12039 #endif
12363 12040
12364 12041 /*
12365 12042 * There is no longer a need to protect against ctx being
12366 12043 * stolen here since we don't store the ctx in the TSB anymore.
12367 12044 */
12368 12045 #ifdef VAC
12369 12046 vcolor = addr_to_vcolor(addr);
12370 12047 #endif
12371 12048
12372 12049 /*
12373 12050 * We must hold the hat lock during the flush of TLB,
12374 12051 * to avoid a race with sfmmu_invalidate_ctx(), where
12375 12052 * sfmmu_cnum on a MMU could be set to INVALID_CONTEXT,
12376 12053 * causing TLB demap routine to skip flush on that MMU.
12377 12054 * If the context on a MMU has already been set to
12378 12055 * INVALID_CONTEXT, we just get an extra flush on
12379 12056 * that MMU.
12380 12057 */
12381 12058 if (!hat_lock_held && !tlb_noflush)
12382 12059 hatlockp = sfmmu_hat_enter(sfmmup);
12383 12060
12384 12061 kpreempt_disable();
12385 12062 if (!tlb_noflush) {
12386 12063 /*
12387 12064 * Flush the TSB and TLB.
12388 12065 */
12389 12066 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0);
12390 12067
12391 12068 cpuset = sfmmup->sfmmu_cpusran;
12392 12069 CPUSET_AND(cpuset, cpu_ready_set);
12393 12070 CPUSET_DEL(cpuset, CPU->cpu_id);
12394 12071
12395 12072 SFMMU_XCALL_STATS(sfmmup);
12396 12073
12397 12074 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr,
12398 12075 (uint64_t)sfmmup);
12399 12076
12400 12077 vtag_flushpage(addr, (uint64_t)sfmmup);
12401 12078 }
12402 12079
12403 12080 if (!hat_lock_held && !tlb_noflush)
12404 12081 sfmmu_hat_exit(hatlockp);
12405 12082
12406 12083 #ifdef VAC
12407 12084 /*
12408 12085 * Flush the D$
12409 12086 *
12410 12087 * Even if the ctx is stolen, we need to flush the
12411 12088 * cache. Our ctx stealer only flushes the TLBs.
12412 12089 */
12413 12090 if (cache_flush_flag == CACHE_FLUSH) {
12414 12091 if (cpu_flag & FLUSH_ALL_CPUS) {
12415 12092 cpuset = cpu_ready_set;
12416 12093 } else {
12417 12094 cpuset = sfmmup->sfmmu_cpusran;
12418 12095 CPUSET_AND(cpuset, cpu_ready_set);
12419 12096 }
12420 12097 CPUSET_DEL(cpuset, CPU->cpu_id);
12421 12098 SFMMU_XCALL_STATS(sfmmup);
12422 12099 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor);
12423 12100 vac_flushpage(pfnum, vcolor);
12424 12101 }
12425 12102 #endif /* VAC */
12426 12103 kpreempt_enable();
12427 12104 }
12428 12105
12429 12106 /*
12430 12107 * Demaps the TSB and flushes all TLBs on all cpus for a particular virtual
12431 12108 * address and ctx. If noflush is set we do not currently do anything.
12432 12109 * This function may or may not be called with the HAT lock held.
12433 12110 */
12434 12111 static void
12435 12112 sfmmu_tlb_demap(caddr_t addr, sfmmu_t *sfmmup, struct hme_blk *hmeblkp,
12436 12113 int tlb_noflush, int hat_lock_held)
12437 12114 {
12438 12115 cpuset_t cpuset;
12439 12116 hatlock_t *hatlockp;
12440 12117
12441 12118 ASSERT(!hmeblkp->hblk_shared);
12442 12119
12443 12120 /*
12444 12121 * If the process is exiting we have nothing to do.
12445 12122 */
12446 12123 if (tlb_noflush)
12447 12124 return;
12448 12125
12449 12126 /*
12450 12127 * Flush TSB.
12451 12128 */
12452 12129 if (!hat_lock_held)
12453 12130 hatlockp = sfmmu_hat_enter(sfmmup);
12454 12131 SFMMU_UNLOAD_TSB(addr, sfmmup, hmeblkp, 0);
12455 12132
12456 12133 kpreempt_disable();
12457 12134
12458 12135 cpuset = sfmmup->sfmmu_cpusran;
12459 12136 CPUSET_AND(cpuset, cpu_ready_set);
12460 12137 CPUSET_DEL(cpuset, CPU->cpu_id);
12461 12138
12462 12139 SFMMU_XCALL_STATS(sfmmup);
12463 12140 xt_some(cpuset, vtag_flushpage_tl1, (uint64_t)addr, (uint64_t)sfmmup);
12464 12141
12465 12142 vtag_flushpage(addr, (uint64_t)sfmmup);
12466 12143
12467 12144 if (!hat_lock_held)
12468 12145 sfmmu_hat_exit(hatlockp);
12469 12146
12470 12147 kpreempt_enable();
12471 12148
12472 12149 }
12473 12150
12474 12151 /*
12475 12152 * Special case of sfmmu_tlb_demap for MMU_PAGESIZE hblks. Use the xcall
12476 12153 * call handler that can flush a range of pages to save on xcalls.
12477 12154 */
12478 12155 static int sfmmu_xcall_save;
12479 12156
12480 12157 /*
12481 12158 * this routine is never used for demaping addresses backed by SRD hmeblks.
12482 12159 */
12483 12160 static void
12484 12161 sfmmu_tlb_range_demap(demap_range_t *dmrp)
12485 12162 {
12486 12163 sfmmu_t *sfmmup = dmrp->dmr_sfmmup;
12487 12164 hatlock_t *hatlockp;
12488 12165 cpuset_t cpuset;
12489 12166 uint64_t sfmmu_pgcnt;
12490 12167 pgcnt_t pgcnt = 0;
12491 12168 int pgunload = 0;
12492 12169 int dirtypg = 0;
12493 12170 caddr_t addr = dmrp->dmr_addr;
12494 12171 caddr_t eaddr;
12495 12172 uint64_t bitvec = dmrp->dmr_bitvec;
12496 12173
12497 12174 ASSERT(bitvec & 1);
12498 12175
12499 12176 /*
12500 12177 * Flush TSB and calculate number of pages to flush.
12501 12178 */
12502 12179 while (bitvec != 0) {
12503 12180 dirtypg = 0;
12504 12181 /*
12505 12182 * Find the first page to flush and then count how many
12506 12183 * pages there are after it that also need to be flushed.
12507 12184 * This way the number of TSB flushes is minimized.
12508 12185 */
12509 12186 while ((bitvec & 1) == 0) {
12510 12187 pgcnt++;
12511 12188 addr += MMU_PAGESIZE;
12512 12189 bitvec >>= 1;
12513 12190 }
12514 12191 while (bitvec & 1) {
12515 12192 dirtypg++;
12516 12193 bitvec >>= 1;
12517 12194 }
12518 12195 eaddr = addr + ptob(dirtypg);
12519 12196 hatlockp = sfmmu_hat_enter(sfmmup);
12520 12197 sfmmu_unload_tsb_range(sfmmup, addr, eaddr, TTE8K);
12521 12198 sfmmu_hat_exit(hatlockp);
12522 12199 pgunload += dirtypg;
12523 12200 addr = eaddr;
12524 12201 pgcnt += dirtypg;
12525 12202 }
12526 12203
12527 12204 ASSERT((pgcnt<<MMU_PAGESHIFT) <= dmrp->dmr_endaddr - dmrp->dmr_addr);
12528 12205 if (sfmmup->sfmmu_free == 0) {
12529 12206 addr = dmrp->dmr_addr;
12530 12207 bitvec = dmrp->dmr_bitvec;
12531 12208
12532 12209 /*
12533 12210 * make sure it has SFMMU_PGCNT_SHIFT bits only,
12534 12211 * as it will be used to pack argument for xt_some
12535 12212 */
12536 12213 ASSERT((pgcnt > 0) &&
12537 12214 (pgcnt <= (1 << SFMMU_PGCNT_SHIFT)));
12538 12215
12539 12216 /*
12540 12217 * Encode pgcnt as (pgcnt -1 ), and pass (pgcnt - 1) in
12541 12218 * the low 6 bits of sfmmup. This is doable since pgcnt
12542 12219 * always >= 1.
12543 12220 */
12544 12221 ASSERT(!((uint64_t)sfmmup & SFMMU_PGCNT_MASK));
12545 12222 sfmmu_pgcnt = (uint64_t)sfmmup |
12546 12223 ((pgcnt - 1) & SFMMU_PGCNT_MASK);
12547 12224
12548 12225 /*
12549 12226 * We must hold the hat lock during the flush of TLB,
12550 12227 * to avoid a race with sfmmu_invalidate_ctx(), where
12551 12228 * sfmmu_cnum on a MMU could be set to INVALID_CONTEXT,
12552 12229 * causing TLB demap routine to skip flush on that MMU.
12553 12230 * If the context on a MMU has already been set to
12554 12231 * INVALID_CONTEXT, we just get an extra flush on
12555 12232 * that MMU.
12556 12233 */
12557 12234 hatlockp = sfmmu_hat_enter(sfmmup);
12558 12235 kpreempt_disable();
12559 12236
12560 12237 cpuset = sfmmup->sfmmu_cpusran;
12561 12238 CPUSET_AND(cpuset, cpu_ready_set);
12562 12239 CPUSET_DEL(cpuset, CPU->cpu_id);
12563 12240
12564 12241 SFMMU_XCALL_STATS(sfmmup);
12565 12242 xt_some(cpuset, vtag_flush_pgcnt_tl1, (uint64_t)addr,
12566 12243 sfmmu_pgcnt);
12567 12244
12568 12245 for (; bitvec != 0; bitvec >>= 1) {
12569 12246 if (bitvec & 1)
12570 12247 vtag_flushpage(addr, (uint64_t)sfmmup);
12571 12248 addr += MMU_PAGESIZE;
12572 12249 }
12573 12250 kpreempt_enable();
12574 12251 sfmmu_hat_exit(hatlockp);
12575 12252
12576 12253 sfmmu_xcall_save += (pgunload-1);
12577 12254 }
12578 12255 dmrp->dmr_bitvec = 0;
12579 12256 }
12580 12257
12581 12258 /*
12582 12259 * In cases where we need to synchronize with TLB/TSB miss trap
12583 12260 * handlers, _and_ need to flush the TLB, it's a lot easier to
12584 12261 * throw away the context from the process than to do a
12585 12262 * special song and dance to keep things consistent for the
12586 12263 * handlers.
12587 12264 *
12588 12265 * Since the process suddenly ends up without a context and our caller
12589 12266 * holds the hat lock, threads that fault after this function is called
12590 12267 * will pile up on the lock. We can then do whatever we need to
12591 12268 * atomically from the context of the caller. The first blocked thread
12592 12269 * to resume executing will get the process a new context, and the
12593 12270 * process will resume executing.
12594 12271 *
12595 12272 * One added advantage of this approach is that on MMUs that
12596 12273 * support a "flush all" operation, we will delay the flush until
12597 12274 * cnum wrap-around, and then flush the TLB one time. This
12598 12275 * is rather rare, so it's a lot less expensive than making 8000
12599 12276 * x-calls to flush the TLB 8000 times.
12600 12277 *
12601 12278 * A per-process (PP) lock is used to synchronize ctx allocations in
12602 12279 * resume() and ctx invalidations here.
12603 12280 */
12604 12281 static void
12605 12282 sfmmu_invalidate_ctx(sfmmu_t *sfmmup)
12606 12283 {
12607 12284 cpuset_t cpuset;
12608 12285 int cnum, currcnum;
12609 12286 mmu_ctx_t *mmu_ctxp;
12610 12287 int i;
12611 12288 uint_t pstate_save;
12612 12289
12613 12290 SFMMU_STAT(sf_ctx_inv);
12614 12291
12615 12292 ASSERT(sfmmu_hat_lock_held(sfmmup));
12616 12293 ASSERT(sfmmup != ksfmmup);
12617 12294
12618 12295 kpreempt_disable();
12619 12296
12620 12297 mmu_ctxp = CPU_MMU_CTXP(CPU);
12621 12298 ASSERT(mmu_ctxp);
12622 12299 ASSERT(mmu_ctxp->mmu_idx < max_mmu_ctxdoms);
12623 12300 ASSERT(mmu_ctxp == mmu_ctxs_tbl[mmu_ctxp->mmu_idx]);
12624 12301
12625 12302 currcnum = sfmmup->sfmmu_ctxs[mmu_ctxp->mmu_idx].cnum;
12626 12303
12627 12304 pstate_save = sfmmu_disable_intrs();
12628 12305
12629 12306 lock_set(&sfmmup->sfmmu_ctx_lock); /* acquire PP lock */
12630 12307 /* set HAT cnum invalid across all context domains. */
12631 12308 for (i = 0; i < max_mmu_ctxdoms; i++) {
12632 12309
12633 12310 cnum = sfmmup->sfmmu_ctxs[i].cnum;
12634 12311 if (cnum == INVALID_CONTEXT) {
12635 12312 continue;
12636 12313 }
12637 12314
12638 12315 sfmmup->sfmmu_ctxs[i].cnum = INVALID_CONTEXT;
12639 12316 }
12640 12317 membar_enter(); /* make sure globally visible to all CPUs */
12641 12318 lock_clear(&sfmmup->sfmmu_ctx_lock); /* release PP lock */
12642 12319
12643 12320 sfmmu_enable_intrs(pstate_save);
12644 12321
12645 12322 cpuset = sfmmup->sfmmu_cpusran;
12646 12323 CPUSET_DEL(cpuset, CPU->cpu_id);
12647 12324 CPUSET_AND(cpuset, cpu_ready_set);
12648 12325 if (!CPUSET_ISNULL(cpuset)) {
12649 12326 SFMMU_XCALL_STATS(sfmmup);
12650 12327 xt_some(cpuset, sfmmu_raise_tsb_exception,
12651 12328 (uint64_t)sfmmup, INVALID_CONTEXT);
12652 12329 xt_sync(cpuset);
12653 12330 SFMMU_STAT(sf_tsb_raise_exception);
12654 12331 SFMMU_MMU_STAT(mmu_tsb_raise_exception);
12655 12332 }
12656 12333
12657 12334 /*
12658 12335 * If the hat to-be-invalidated is the same as the current
12659 12336 * process on local CPU we need to invalidate
12660 12337 * this CPU context as well.
12661 12338 */
12662 12339 if ((sfmmu_getctx_sec() == currcnum) &&
12663 12340 (currcnum != INVALID_CONTEXT)) {
12664 12341 /* sets shared context to INVALID too */
12665 12342 sfmmu_setctx_sec(INVALID_CONTEXT);
12666 12343 sfmmu_clear_utsbinfo();
12667 12344 }
12668 12345
12669 12346 SFMMU_FLAGS_SET(sfmmup, HAT_ALLCTX_INVALID);
12670 12347
12671 12348 kpreempt_enable();
12672 12349
12673 12350 /*
12674 12351 * we hold the hat lock, so nobody should allocate a context
12675 12352 * for us yet
12676 12353 */
12677 12354 ASSERT(sfmmup->sfmmu_ctxs[mmu_ctxp->mmu_idx].cnum == INVALID_CONTEXT);
12678 12355 }
12679 12356
12680 12357 #ifdef VAC
12681 12358 /*
12682 12359 * We need to flush the cache in all cpus. It is possible that
12683 12360 * a process referenced a page as cacheable but has sinced exited
12684 12361 * and cleared the mapping list. We still to flush it but have no
12685 12362 * state so all cpus is the only alternative.
12686 12363 */
12687 12364 void
12688 12365 sfmmu_cache_flush(pfn_t pfnum, int vcolor)
12689 12366 {
12690 12367 cpuset_t cpuset;
12691 12368
12692 12369 kpreempt_disable();
12693 12370 cpuset = cpu_ready_set;
12694 12371 CPUSET_DEL(cpuset, CPU->cpu_id);
12695 12372 SFMMU_XCALL_STATS(NULL); /* account to any ctx */
12696 12373 xt_some(cpuset, vac_flushpage_tl1, pfnum, vcolor);
12697 12374 xt_sync(cpuset);
12698 12375 vac_flushpage(pfnum, vcolor);
12699 12376 kpreempt_enable();
12700 12377 }
12701 12378
12702 12379 void
12703 12380 sfmmu_cache_flushcolor(int vcolor, pfn_t pfnum)
12704 12381 {
12705 12382 cpuset_t cpuset;
12706 12383
12707 12384 ASSERT(vcolor >= 0);
12708 12385
12709 12386 kpreempt_disable();
12710 12387 cpuset = cpu_ready_set;
12711 12388 CPUSET_DEL(cpuset, CPU->cpu_id);
12712 12389 SFMMU_XCALL_STATS(NULL); /* account to any ctx */
12713 12390 xt_some(cpuset, vac_flushcolor_tl1, vcolor, pfnum);
12714 12391 xt_sync(cpuset);
12715 12392 vac_flushcolor(vcolor, pfnum);
12716 12393 kpreempt_enable();
12717 12394 }
12718 12395 #endif /* VAC */
12719 12396
12720 12397 /*
12721 12398 * We need to prevent processes from accessing the TSB using a cached physical
12722 12399 * address. It's alright if they try to access the TSB via virtual address
12723 12400 * since they will just fault on that virtual address once the mapping has
12724 12401 * been suspended.
12725 12402 */
12726 12403 #pragma weak sendmondo_in_recover
12727 12404
12728 12405 /* ARGSUSED */
12729 12406 static int
12730 12407 sfmmu_tsb_pre_relocator(caddr_t va, uint_t tsbsz, uint_t flags, void *tsbinfo)
12731 12408 {
12732 12409 struct tsb_info *tsbinfop = (struct tsb_info *)tsbinfo;
12733 12410 sfmmu_t *sfmmup = tsbinfop->tsb_sfmmu;
12734 12411 hatlock_t *hatlockp;
12735 12412 sf_scd_t *scdp;
12736 12413
12737 12414 if (flags != HAT_PRESUSPEND)
12738 12415 return (0);
12739 12416
12740 12417 /*
12741 12418 * If tsb is a shared TSB with TSB_SHAREDCTX set, sfmmup must
12742 12419 * be a shared hat, then set SCD's tsbinfo's flag.
12743 12420 * If tsb is not shared, sfmmup is a private hat, then set
12744 12421 * its private tsbinfo's flag.
12745 12422 */
12746 12423 hatlockp = sfmmu_hat_enter(sfmmup);
12747 12424 tsbinfop->tsb_flags |= TSB_RELOC_FLAG;
12748 12425
12749 12426 if (!(tsbinfop->tsb_flags & TSB_SHAREDCTX)) {
12750 12427 sfmmu_tsb_inv_ctx(sfmmup);
12751 12428 sfmmu_hat_exit(hatlockp);
12752 12429 } else {
12753 12430 /* release lock on the shared hat */
12754 12431 sfmmu_hat_exit(hatlockp);
12755 12432 /* sfmmup is a shared hat */
12756 12433 ASSERT(sfmmup->sfmmu_scdhat);
12757 12434 scdp = sfmmup->sfmmu_scdp;
12758 12435 ASSERT(scdp != NULL);
12759 12436 /* get private hat from the scd list */
12760 12437 mutex_enter(&scdp->scd_mutex);
12761 12438 sfmmup = scdp->scd_sf_list;
12762 12439 while (sfmmup != NULL) {
12763 12440 hatlockp = sfmmu_hat_enter(sfmmup);
12764 12441 /*
12765 12442 * We do not call sfmmu_tsb_inv_ctx here because
12766 12443 * sendmondo_in_recover check is only needed for
12767 12444 * sun4u.
12768 12445 */
12769 12446 sfmmu_invalidate_ctx(sfmmup);
12770 12447 sfmmu_hat_exit(hatlockp);
12771 12448 sfmmup = sfmmup->sfmmu_scd_link.next;
12772 12449
12773 12450 }
12774 12451 mutex_exit(&scdp->scd_mutex);
12775 12452 }
12776 12453 return (0);
12777 12454 }
12778 12455
12779 12456 static void
12780 12457 sfmmu_tsb_inv_ctx(sfmmu_t *sfmmup)
12781 12458 {
12782 12459 extern uint32_t sendmondo_in_recover;
12783 12460
12784 12461 ASSERT(sfmmu_hat_lock_held(sfmmup));
12785 12462
12786 12463 /*
12787 12464 * For Cheetah+ Erratum 25:
12788 12465 * Wait for any active recovery to finish. We can't risk
12789 12466 * relocating the TSB of the thread running mondo_recover_proc()
12790 12467 * since, if we did that, we would deadlock. The scenario we are
12791 12468 * trying to avoid is as follows:
12792 12469 *
12793 12470 * THIS CPU RECOVER CPU
12794 12471 * -------- -----------
12795 12472 * Begins recovery, walking through TSB
12796 12473 * hat_pagesuspend() TSB TTE
12797 12474 * TLB miss on TSB TTE, spins at TL1
12798 12475 * xt_sync()
12799 12476 * send_mondo_timeout()
12800 12477 * mondo_recover_proc()
12801 12478 * ((deadlocked))
12802 12479 *
12803 12480 * The second half of the workaround is that mondo_recover_proc()
12804 12481 * checks to see if the tsb_info has the RELOC flag set, and if it
12805 12482 * does, it skips over that TSB without ever touching tsbinfop->tsb_va
12806 12483 * and hence avoiding the TLB miss that could result in a deadlock.
12807 12484 */
12808 12485 if (&sendmondo_in_recover) {
12809 12486 membar_enter(); /* make sure RELOC flag visible */
12810 12487 while (sendmondo_in_recover) {
12811 12488 drv_usecwait(1);
12812 12489 membar_consumer();
12813 12490 }
12814 12491 }
12815 12492
12816 12493 sfmmu_invalidate_ctx(sfmmup);
12817 12494 }
12818 12495
12819 12496 /* ARGSUSED */
12820 12497 static int
12821 12498 sfmmu_tsb_post_relocator(caddr_t va, uint_t tsbsz, uint_t flags,
12822 12499 void *tsbinfo, pfn_t newpfn)
12823 12500 {
12824 12501 hatlock_t *hatlockp;
12825 12502 struct tsb_info *tsbinfop = (struct tsb_info *)tsbinfo;
12826 12503 sfmmu_t *sfmmup = tsbinfop->tsb_sfmmu;
12827 12504
12828 12505 if (flags != HAT_POSTUNSUSPEND)
12829 12506 return (0);
12830 12507
12831 12508 hatlockp = sfmmu_hat_enter(sfmmup);
12832 12509
12833 12510 SFMMU_STAT(sf_tsb_reloc);
12834 12511
12835 12512 /*
12836 12513 * The process may have swapped out while we were relocating one
12837 12514 * of its TSBs. If so, don't bother doing the setup since the
12838 12515 * process can't be using the memory anymore.
12839 12516 */
12840 12517 if ((tsbinfop->tsb_flags & TSB_SWAPPED) == 0) {
12841 12518 ASSERT(va == tsbinfop->tsb_va);
12842 12519 sfmmu_tsbinfo_setup_phys(tsbinfop, newpfn);
12843 12520
12844 12521 if (tsbinfop->tsb_flags & TSB_FLUSH_NEEDED) {
12845 12522 sfmmu_inv_tsb(tsbinfop->tsb_va,
12846 12523 TSB_BYTES(tsbinfop->tsb_szc));
12847 12524 tsbinfop->tsb_flags &= ~TSB_FLUSH_NEEDED;
12848 12525 }
12849 12526 }
12850 12527
12851 12528 membar_exit();
12852 12529 tsbinfop->tsb_flags &= ~TSB_RELOC_FLAG;
12853 12530 cv_broadcast(&sfmmup->sfmmu_tsb_cv);
12854 12531
12855 12532 sfmmu_hat_exit(hatlockp);
12856 12533
12857 12534 return (0);
12858 12535 }
12859 12536
12860 12537 /*
12861 12538 * Allocate and initialize a tsb_info structure. Note that we may or may not
12862 12539 * allocate a TSB here, depending on the flags passed in.
12863 12540 */
12864 12541 static int
12865 12542 sfmmu_tsbinfo_alloc(struct tsb_info **tsbinfopp, int tsb_szc, int tte_sz_mask,
12866 12543 uint_t flags, sfmmu_t *sfmmup)
12867 12544 {
12868 12545 int err;
12869 12546
12870 12547 *tsbinfopp = (struct tsb_info *)kmem_cache_alloc(
12871 12548 sfmmu_tsbinfo_cache, KM_SLEEP);
12872 12549
12873 12550 if ((err = sfmmu_init_tsbinfo(*tsbinfopp, tte_sz_mask,
12874 12551 tsb_szc, flags, sfmmup)) != 0) {
12875 12552 kmem_cache_free(sfmmu_tsbinfo_cache, *tsbinfopp);
12876 12553 SFMMU_STAT(sf_tsb_allocfail);
12877 12554 *tsbinfopp = NULL;
12878 12555 return (err);
12879 12556 }
12880 12557 SFMMU_STAT(sf_tsb_alloc);
12881 12558
12882 12559 /*
12883 12560 * Bump the TSB size counters for this TSB size.
12884 12561 */
12885 12562 (*(((int *)&sfmmu_tsbsize_stat) + tsb_szc))++;
12886 12563 return (0);
12887 12564 }
12888 12565
12889 12566 static void
12890 12567 sfmmu_tsb_free(struct tsb_info *tsbinfo)
12891 12568 {
12892 12569 caddr_t tsbva = tsbinfo->tsb_va;
12893 12570 uint_t tsb_size = TSB_BYTES(tsbinfo->tsb_szc);
12894 12571 struct kmem_cache *kmem_cachep = tsbinfo->tsb_cache;
12895 12572 vmem_t *vmp = tsbinfo->tsb_vmp;
12896 12573
12897 12574 /*
12898 12575 * If we allocated this TSB from relocatable kernel memory, then we
12899 12576 * need to uninstall the callback handler.
12900 12577 */
12901 12578 if (tsbinfo->tsb_cache != sfmmu_tsb8k_cache) {
12902 12579 uintptr_t slab_mask;
12903 12580 caddr_t slab_vaddr;
12904 12581 page_t **ppl;
12905 12582 int ret;
12906 12583
12907 12584 ASSERT(tsb_size <= MMU_PAGESIZE4M || use_bigtsb_arena);
12908 12585 if (tsb_size > MMU_PAGESIZE4M)
12909 12586 slab_mask = ~((uintptr_t)bigtsb_slab_mask) << PAGESHIFT;
12910 12587 else
12911 12588 slab_mask = ~((uintptr_t)tsb_slab_mask) << PAGESHIFT;
12912 12589 slab_vaddr = (caddr_t)((uintptr_t)tsbva & slab_mask);
12913 12590
12914 12591 ret = as_pagelock(&kas, &ppl, slab_vaddr, PAGESIZE, S_WRITE);
12915 12592 ASSERT(ret == 0);
12916 12593 hat_delete_callback(tsbva, (uint_t)tsb_size, (void *)tsbinfo,
12917 12594 0, NULL);
12918 12595 as_pageunlock(&kas, ppl, slab_vaddr, PAGESIZE, S_WRITE);
12919 12596 }
12920 12597
12921 12598 if (kmem_cachep != NULL) {
12922 12599 kmem_cache_free(kmem_cachep, tsbva);
12923 12600 } else {
12924 12601 vmem_xfree(vmp, (void *)tsbva, tsb_size);
12925 12602 }
12926 12603 tsbinfo->tsb_va = (caddr_t)0xbad00bad;
12927 12604 atomic_add_64(&tsb_alloc_bytes, -(int64_t)tsb_size);
12928 12605 }
12929 12606
12930 12607 static void
12931 12608 sfmmu_tsbinfo_free(struct tsb_info *tsbinfo)
12932 12609 {
12933 12610 if ((tsbinfo->tsb_flags & TSB_SWAPPED) == 0) {
12934 12611 sfmmu_tsb_free(tsbinfo);
12935 12612 }
12936 12613 kmem_cache_free(sfmmu_tsbinfo_cache, tsbinfo);
12937 12614
12938 12615 }
12939 12616
12940 12617 /*
12941 12618 * Setup all the references to physical memory for this tsbinfo.
12942 12619 * The underlying page(s) must be locked.
12943 12620 */
12944 12621 static void
12945 12622 sfmmu_tsbinfo_setup_phys(struct tsb_info *tsbinfo, pfn_t pfn)
12946 12623 {
12947 12624 ASSERT(pfn != PFN_INVALID);
12948 12625 ASSERT(pfn == va_to_pfn(tsbinfo->tsb_va));
12949 12626
12950 12627 #ifndef sun4v
12951 12628 if (tsbinfo->tsb_szc == 0) {
12952 12629 sfmmu_memtte(&tsbinfo->tsb_tte, pfn,
12953 12630 PROT_WRITE|PROT_READ, TTE8K);
12954 12631 } else {
12955 12632 /*
12956 12633 * Round down PA and use a large mapping; the handlers will
12957 12634 * compute the TSB pointer at the correct offset into the
12958 12635 * big virtual page. NOTE: this assumes all TSBs larger
12959 12636 * than 8K must come from physically contiguous slabs of
12960 12637 * size tsb_slab_size.
12961 12638 */
12962 12639 sfmmu_memtte(&tsbinfo->tsb_tte, pfn & ~tsb_slab_mask,
12963 12640 PROT_WRITE|PROT_READ, tsb_slab_ttesz);
12964 12641 }
12965 12642 tsbinfo->tsb_pa = ptob(pfn);
12966 12643
12967 12644 TTE_SET_LOCKED(&tsbinfo->tsb_tte); /* lock the tte into dtlb */
12968 12645 TTE_SET_MOD(&tsbinfo->tsb_tte); /* enable writes */
12969 12646
12970 12647 ASSERT(TTE_IS_PRIVILEGED(&tsbinfo->tsb_tte));
12971 12648 ASSERT(TTE_IS_LOCKED(&tsbinfo->tsb_tte));
12972 12649 #else /* sun4v */
12973 12650 tsbinfo->tsb_pa = ptob(pfn);
12974 12651 #endif /* sun4v */
12975 12652 }
12976 12653
12977 12654
12978 12655 /*
12979 12656 * Returns zero on success, ENOMEM if over the high water mark,
12980 12657 * or EAGAIN if the caller needs to retry with a smaller TSB
12981 12658 * size (or specify TSB_FORCEALLOC if the allocation can't fail).
12982 12659 *
12983 12660 * This call cannot fail to allocate a TSB if TSB_FORCEALLOC
12984 12661 * is specified and the TSB requested is PAGESIZE, though it
12985 12662 * may sleep waiting for memory if sufficient memory is not
12986 12663 * available.
12987 12664 */
12988 12665 static int
12989 12666 sfmmu_init_tsbinfo(struct tsb_info *tsbinfo, int tteszmask,
12990 12667 int tsbcode, uint_t flags, sfmmu_t *sfmmup)
12991 12668 {
12992 12669 caddr_t vaddr = NULL;
12993 12670 caddr_t slab_vaddr;
12994 12671 uintptr_t slab_mask;
12995 12672 int tsbbytes = TSB_BYTES(tsbcode);
12996 12673 int lowmem = 0;
12997 12674 struct kmem_cache *kmem_cachep = NULL;
12998 12675 vmem_t *vmp = NULL;
12999 12676 lgrp_id_t lgrpid = LGRP_NONE;
13000 12677 pfn_t pfn;
13001 12678 uint_t cbflags = HAC_SLEEP;
13002 12679 page_t **pplist;
13003 12680 int ret;
13004 12681
13005 12682 ASSERT(tsbbytes <= MMU_PAGESIZE4M || use_bigtsb_arena);
13006 12683 if (tsbbytes > MMU_PAGESIZE4M)
13007 12684 slab_mask = ~((uintptr_t)bigtsb_slab_mask) << PAGESHIFT;
13008 12685 else
13009 12686 slab_mask = ~((uintptr_t)tsb_slab_mask) << PAGESHIFT;
13010 12687
13011 12688 if (flags & (TSB_FORCEALLOC | TSB_SWAPIN | TSB_GROW | TSB_SHRINK))
13012 12689 flags |= TSB_ALLOC;
13013 12690
13014 12691 ASSERT((flags & TSB_FORCEALLOC) == 0 || tsbcode == TSB_MIN_SZCODE);
13015 12692
13016 12693 tsbinfo->tsb_sfmmu = sfmmup;
13017 12694
13018 12695 /*
13019 12696 * If not allocating a TSB, set up the tsbinfo, set TSB_SWAPPED, and
13020 12697 * return.
13021 12698 */
13022 12699 if ((flags & TSB_ALLOC) == 0) {
13023 12700 tsbinfo->tsb_szc = tsbcode;
13024 12701 tsbinfo->tsb_ttesz_mask = tteszmask;
13025 12702 tsbinfo->tsb_va = (caddr_t)0xbadbadbeef;
13026 12703 tsbinfo->tsb_pa = -1;
13027 12704 tsbinfo->tsb_tte.ll = 0;
13028 12705 tsbinfo->tsb_next = NULL;
13029 12706 tsbinfo->tsb_flags = TSB_SWAPPED;
13030 12707 tsbinfo->tsb_cache = NULL;
13031 12708 tsbinfo->tsb_vmp = NULL;
13032 12709 return (0);
13033 12710 }
13034 12711
13035 12712 #ifdef DEBUG
13036 12713 /*
13037 12714 * For debugging:
13038 12715 * Randomly force allocation failures every tsb_alloc_mtbf
13039 12716 * tries if TSB_FORCEALLOC is not specified. This will
13040 12717 * return ENOMEM if tsb_alloc_mtbf is odd, or EAGAIN if
13041 12718 * it is even, to allow testing of both failure paths...
13042 12719 */
13043 12720 if (tsb_alloc_mtbf && ((flags & TSB_FORCEALLOC) == 0) &&
13044 12721 (tsb_alloc_count++ == tsb_alloc_mtbf)) {
13045 12722 tsb_alloc_count = 0;
13046 12723 tsb_alloc_fail_mtbf++;
13047 12724 return ((tsb_alloc_mtbf & 1)? ENOMEM : EAGAIN);
13048 12725 }
13049 12726 #endif /* DEBUG */
13050 12727
13051 12728 /*
13052 12729 * Enforce high water mark if we are not doing a forced allocation
13053 12730 * and are not shrinking a process' TSB.
13054 12731 */
13055 12732 if ((flags & TSB_SHRINK) == 0 &&
13056 12733 (tsbbytes + tsb_alloc_bytes) > tsb_alloc_hiwater) {
13057 12734 if ((flags & TSB_FORCEALLOC) == 0)
13058 12735 return (ENOMEM);
13059 12736 lowmem = 1;
13060 12737 }
13061 12738
13062 12739 /*
13063 12740 * Allocate from the correct location based upon the size of the TSB
13064 12741 * compared to the base page size, and what memory conditions dictate.
13065 12742 * Note we always do nonblocking allocations from the TSB arena since
13066 12743 * we don't want memory fragmentation to cause processes to block
13067 12744 * indefinitely waiting for memory; until the kernel algorithms that
13068 12745 * coalesce large pages are improved this is our best option.
13069 12746 *
13070 12747 * Algorithm:
13071 12748 * If allocating a "large" TSB (>8K), allocate from the
13072 12749 * appropriate kmem_tsb_default_arena vmem arena
13073 12750 * else if low on memory or the TSB_FORCEALLOC flag is set or
13074 12751 * tsb_forceheap is set
13075 12752 * Allocate from kernel heap via sfmmu_tsb8k_cache with
13076 12753 * KM_SLEEP (never fails)
13077 12754 * else
13078 12755 * Allocate from appropriate sfmmu_tsb_cache with
13079 12756 * KM_NOSLEEP
13080 12757 * endif
13081 12758 */
13082 12759 if (tsb_lgrp_affinity)
13083 12760 lgrpid = lgrp_home_id(curthread);
13084 12761 if (lgrpid == LGRP_NONE)
13085 12762 lgrpid = 0; /* use lgrp of boot CPU */
13086 12763
13087 12764 if (tsbbytes > MMU_PAGESIZE) {
13088 12765 if (tsbbytes > MMU_PAGESIZE4M) {
13089 12766 vmp = kmem_bigtsb_default_arena[lgrpid];
13090 12767 vaddr = (caddr_t)vmem_xalloc(vmp, tsbbytes, tsbbytes,
13091 12768 0, 0, NULL, NULL, VM_NOSLEEP);
13092 12769 } else {
13093 12770 vmp = kmem_tsb_default_arena[lgrpid];
13094 12771 vaddr = (caddr_t)vmem_xalloc(vmp, tsbbytes, tsbbytes,
13095 12772 0, 0, NULL, NULL, VM_NOSLEEP);
13096 12773 }
13097 12774 #ifdef DEBUG
13098 12775 } else if (lowmem || (flags & TSB_FORCEALLOC) || tsb_forceheap) {
13099 12776 #else /* !DEBUG */
13100 12777 } else if (lowmem || (flags & TSB_FORCEALLOC)) {
13101 12778 #endif /* DEBUG */
13102 12779 kmem_cachep = sfmmu_tsb8k_cache;
13103 12780 vaddr = (caddr_t)kmem_cache_alloc(kmem_cachep, KM_SLEEP);
13104 12781 ASSERT(vaddr != NULL);
13105 12782 } else {
13106 12783 kmem_cachep = sfmmu_tsb_cache[lgrpid];
13107 12784 vaddr = (caddr_t)kmem_cache_alloc(kmem_cachep, KM_NOSLEEP);
13108 12785 }
13109 12786
13110 12787 tsbinfo->tsb_cache = kmem_cachep;
13111 12788 tsbinfo->tsb_vmp = vmp;
13112 12789
13113 12790 if (vaddr == NULL) {
13114 12791 return (EAGAIN);
13115 12792 }
13116 12793
13117 12794 atomic_add_64(&tsb_alloc_bytes, (int64_t)tsbbytes);
13118 12795 kmem_cachep = tsbinfo->tsb_cache;
13119 12796
13120 12797 /*
13121 12798 * If we are allocating from outside the cage, then we need to
13122 12799 * register a relocation callback handler. Note that for now
13123 12800 * since pseudo mappings always hang off of the slab's root page,
13124 12801 * we need only lock the first 8K of the TSB slab. This is a bit
13125 12802 * hacky but it is good for performance.
13126 12803 */
13127 12804 if (kmem_cachep != sfmmu_tsb8k_cache) {
13128 12805 slab_vaddr = (caddr_t)((uintptr_t)vaddr & slab_mask);
13129 12806 ret = as_pagelock(&kas, &pplist, slab_vaddr, PAGESIZE, S_WRITE);
13130 12807 ASSERT(ret == 0);
13131 12808 ret = hat_add_callback(sfmmu_tsb_cb_id, vaddr, (uint_t)tsbbytes,
13132 12809 cbflags, (void *)tsbinfo, &pfn, NULL);
13133 12810
13134 12811 /*
13135 12812 * Need to free up resources if we could not successfully
13136 12813 * add the callback function and return an error condition.
13137 12814 */
13138 12815 if (ret != 0) {
13139 12816 if (kmem_cachep) {
13140 12817 kmem_cache_free(kmem_cachep, vaddr);
13141 12818 } else {
13142 12819 vmem_xfree(vmp, (void *)vaddr, tsbbytes);
13143 12820 }
13144 12821 as_pageunlock(&kas, pplist, slab_vaddr, PAGESIZE,
13145 12822 S_WRITE);
13146 12823 return (EAGAIN);
13147 12824 }
13148 12825 } else {
13149 12826 /*
13150 12827 * Since allocation of 8K TSBs from heap is rare and occurs
13151 12828 * during memory pressure we allocate them from permanent
13152 12829 * memory rather than using callbacks to get the PFN.
13153 12830 */
13154 12831 pfn = hat_getpfnum(kas.a_hat, vaddr);
13155 12832 }
13156 12833
13157 12834 tsbinfo->tsb_va = vaddr;
13158 12835 tsbinfo->tsb_szc = tsbcode;
13159 12836 tsbinfo->tsb_ttesz_mask = tteszmask;
13160 12837 tsbinfo->tsb_next = NULL;
13161 12838 tsbinfo->tsb_flags = 0;
13162 12839
13163 12840 sfmmu_tsbinfo_setup_phys(tsbinfo, pfn);
13164 12841
13165 12842 sfmmu_inv_tsb(vaddr, tsbbytes);
13166 12843
13167 12844 if (kmem_cachep != sfmmu_tsb8k_cache) {
13168 12845 as_pageunlock(&kas, pplist, slab_vaddr, PAGESIZE, S_WRITE);
13169 12846 }
13170 12847
13171 12848 return (0);
13172 12849 }
13173 12850
13174 12851 /*
13175 12852 * Initialize per cpu tsb and per cpu tsbmiss_area
13176 12853 */
13177 12854 void
13178 12855 sfmmu_init_tsbs(void)
13179 12856 {
13180 12857 int i;
13181 12858 struct tsbmiss *tsbmissp;
13182 12859 struct kpmtsbm *kpmtsbmp;
13183 12860 #ifndef sun4v
13184 12861 extern int dcache_line_mask;
13185 12862 #endif /* sun4v */
13186 12863 extern uint_t vac_colors;
13187 12864
13188 12865 /*
13189 12866 * Init. tsb miss area.
13190 12867 */
13191 12868 tsbmissp = tsbmiss_area;
13192 12869
13193 12870 for (i = 0; i < NCPU; tsbmissp++, i++) {
13194 12871 /*
13195 12872 * initialize the tsbmiss area.
13196 12873 * Do this for all possible CPUs as some may be added
13197 12874 * while the system is running. There is no cost to this.
13198 12875 */
13199 12876 tsbmissp->ksfmmup = ksfmmup;
13200 12877 #ifndef sun4v
13201 12878 tsbmissp->dcache_line_mask = (uint16_t)dcache_line_mask;
13202 12879 #endif /* sun4v */
13203 12880 tsbmissp->khashstart =
13204 12881 (struct hmehash_bucket *)va_to_pa((caddr_t)khme_hash);
13205 12882 tsbmissp->uhashstart =
13206 12883 (struct hmehash_bucket *)va_to_pa((caddr_t)uhme_hash);
13207 12884 tsbmissp->khashsz = khmehash_num;
13208 12885 tsbmissp->uhashsz = uhmehash_num;
13209 12886 }
13210 12887
13211 12888 sfmmu_tsb_cb_id = hat_register_callback('T'<<16 | 'S' << 8 | 'B',
13212 12889 sfmmu_tsb_pre_relocator, sfmmu_tsb_post_relocator, NULL, 0);
13213 12890
13214 12891 if (kpm_enable == 0)
13215 12892 return;
13216 12893
13217 12894 /* -- Begin KPM specific init -- */
13218 12895
13219 12896 if (kpm_smallpages) {
13220 12897 /*
13221 12898 * If we're using base pagesize pages for seg_kpm
13222 12899 * mappings, we use the kernel TSB since we can't afford
13223 12900 * to allocate a second huge TSB for these mappings.
13224 12901 */
13225 12902 kpm_tsbbase = ktsb_phys? ktsb_pbase : (uint64_t)ktsb_base;
13226 12903 kpm_tsbsz = ktsb_szcode;
13227 12904 kpmsm_tsbbase = kpm_tsbbase;
13228 12905 kpmsm_tsbsz = kpm_tsbsz;
13229 12906 } else {
13230 12907 /*
13231 12908 * In VAC conflict case, just put the entries in the
13232 12909 * kernel 8K indexed TSB for now so we can find them.
13233 12910 * This could really be changed in the future if we feel
13234 12911 * the need...
13235 12912 */
13236 12913 kpmsm_tsbbase = ktsb_phys? ktsb_pbase : (uint64_t)ktsb_base;
13237 12914 kpmsm_tsbsz = ktsb_szcode;
13238 12915 kpm_tsbbase = ktsb_phys? ktsb4m_pbase : (uint64_t)ktsb4m_base;
13239 12916 kpm_tsbsz = ktsb4m_szcode;
13240 12917 }
13241 12918
13242 12919 kpmtsbmp = kpmtsbm_area;
13243 12920 for (i = 0; i < NCPU; kpmtsbmp++, i++) {
13244 12921 /*
13245 12922 * Initialize the kpmtsbm area.
13246 12923 * Do this for all possible CPUs as some may be added
13247 12924 * while the system is running. There is no cost to this.
13248 12925 */
13249 12926 kpmtsbmp->vbase = kpm_vbase;
13250 12927 kpmtsbmp->vend = kpm_vbase + kpm_size * vac_colors;
13251 12928 kpmtsbmp->sz_shift = kpm_size_shift;
13252 12929 kpmtsbmp->kpmp_shift = kpmp_shift;
13253 12930 kpmtsbmp->kpmp2pshft = (uchar_t)kpmp2pshft;
13254 12931 if (kpm_smallpages == 0) {
13255 12932 kpmtsbmp->kpmp_table_sz = kpmp_table_sz;
13256 12933 kpmtsbmp->kpmp_tablepa = va_to_pa(kpmp_table);
13257 12934 } else {
13258 12935 kpmtsbmp->kpmp_table_sz = kpmp_stable_sz;
13259 12936 kpmtsbmp->kpmp_tablepa = va_to_pa(kpmp_stable);
13260 12937 }
13261 12938 kpmtsbmp->msegphashpa = va_to_pa(memseg_phash);
13262 12939 kpmtsbmp->flags = KPMTSBM_ENABLE_FLAG;
13263 12940 #ifdef DEBUG
13264 12941 kpmtsbmp->flags |= (kpm_tsbmtl) ? KPMTSBM_TLTSBM_FLAG : 0;
13265 12942 #endif /* DEBUG */
13266 12943 if (ktsb_phys)
13267 12944 kpmtsbmp->flags |= KPMTSBM_TSBPHYS_FLAG;
13268 12945 }
13269 12946
13270 12947 /* -- End KPM specific init -- */
13271 12948 }
13272 12949
13273 12950 /* Avoid using sfmmu_tsbinfo_alloc() to avoid kmem_alloc - no real reason */
13274 12951 struct tsb_info ktsb_info[2];
13275 12952
13276 12953 /*
13277 12954 * Called from hat_kern_setup() to setup the tsb_info for ksfmmup.
13278 12955 */
13279 12956 void
13280 12957 sfmmu_init_ktsbinfo()
13281 12958 {
13282 12959 ASSERT(ksfmmup != NULL);
13283 12960 ASSERT(ksfmmup->sfmmu_tsb == NULL);
13284 12961 /*
13285 12962 * Allocate tsbinfos for kernel and copy in data
13286 12963 * to make debug easier and sun4v setup easier.
13287 12964 */
13288 12965 ktsb_info[0].tsb_sfmmu = ksfmmup;
13289 12966 ktsb_info[0].tsb_szc = ktsb_szcode;
13290 12967 ktsb_info[0].tsb_ttesz_mask = TSB8K|TSB64K|TSB512K;
13291 12968 ktsb_info[0].tsb_va = ktsb_base;
13292 12969 ktsb_info[0].tsb_pa = ktsb_pbase;
13293 12970 ktsb_info[0].tsb_flags = 0;
13294 12971 ktsb_info[0].tsb_tte.ll = 0;
13295 12972 ktsb_info[0].tsb_cache = NULL;
13296 12973
13297 12974 ktsb_info[1].tsb_sfmmu = ksfmmup;
13298 12975 ktsb_info[1].tsb_szc = ktsb4m_szcode;
13299 12976 ktsb_info[1].tsb_ttesz_mask = TSB4M;
13300 12977 ktsb_info[1].tsb_va = ktsb4m_base;
13301 12978 ktsb_info[1].tsb_pa = ktsb4m_pbase;
13302 12979 ktsb_info[1].tsb_flags = 0;
13303 12980 ktsb_info[1].tsb_tte.ll = 0;
13304 12981 ktsb_info[1].tsb_cache = NULL;
13305 12982
13306 12983 /* Link them into ksfmmup. */
13307 12984 ktsb_info[0].tsb_next = &ktsb_info[1];
13308 12985 ktsb_info[1].tsb_next = NULL;
13309 12986 ksfmmup->sfmmu_tsb = &ktsb_info[0];
13310 12987
13311 12988 sfmmu_setup_tsbinfo(ksfmmup);
13312 12989 }
13313 12990
13314 12991 /*
13315 12992 * Cache the last value returned from va_to_pa(). If the VA specified
13316 12993 * in the current call to cached_va_to_pa() maps to the same Page (as the
13317 12994 * previous call to cached_va_to_pa()), then compute the PA using
13318 12995 * cached info, else call va_to_pa().
13319 12996 *
13320 12997 * Note: this function is neither MT-safe nor consistent in the presence
13321 12998 * of multiple, interleaved threads. This function was created to enable
13322 12999 * an optimization used during boot (at a point when there's only one thread
13323 13000 * executing on the "boot CPU", and before startup_vm() has been called).
13324 13001 */
13325 13002 static uint64_t
13326 13003 cached_va_to_pa(void *vaddr)
13327 13004 {
13328 13005 static uint64_t prev_vaddr_base = 0;
13329 13006 static uint64_t prev_pfn = 0;
13330 13007
13331 13008 if ((((uint64_t)vaddr) & MMU_PAGEMASK) == prev_vaddr_base) {
13332 13009 return (prev_pfn | ((uint64_t)vaddr & MMU_PAGEOFFSET));
13333 13010 } else {
13334 13011 uint64_t pa = va_to_pa(vaddr);
13335 13012
13336 13013 if (pa != ((uint64_t)-1)) {
13337 13014 /*
13338 13015 * Computed physical address is valid. Cache its
13339 13016 * related info for the next cached_va_to_pa() call.
13340 13017 */
13341 13018 prev_pfn = pa & MMU_PAGEMASK;
13342 13019 prev_vaddr_base = ((uint64_t)vaddr) & MMU_PAGEMASK;
13343 13020 }
13344 13021
13345 13022 return (pa);
13346 13023 }
13347 13024 }
13348 13025
13349 13026 /*
13350 13027 * Carve up our nucleus hblk region. We may allocate more hblks than
13351 13028 * asked due to rounding errors but we are guaranteed to have at least
13352 13029 * enough space to allocate the requested number of hblk8's and hblk1's.
13353 13030 */
13354 13031 void
13355 13032 sfmmu_init_nucleus_hblks(caddr_t addr, size_t size, int nhblk8, int nhblk1)
13356 13033 {
13357 13034 struct hme_blk *hmeblkp;
13358 13035 size_t hme8blk_sz, hme1blk_sz;
13359 13036 size_t i;
13360 13037 size_t hblk8_bound;
13361 13038 ulong_t j = 0, k = 0;
13362 13039
13363 13040 ASSERT(addr != NULL && size != 0);
13364 13041
13365 13042 /* Need to use proper structure alignment */
13366 13043 hme8blk_sz = roundup(HME8BLK_SZ, sizeof (int64_t));
13367 13044 hme1blk_sz = roundup(HME1BLK_SZ, sizeof (int64_t));
13368 13045
13369 13046 nucleus_hblk8.list = (void *)addr;
13370 13047 nucleus_hblk8.index = 0;
13371 13048
13372 13049 /*
13373 13050 * Use as much memory as possible for hblk8's since we
13374 13051 * expect all bop_alloc'ed memory to be allocated in 8k chunks.
13375 13052 * We need to hold back enough space for the hblk1's which
13376 13053 * we'll allocate next.
13377 13054 */
13378 13055 hblk8_bound = size - (nhblk1 * hme1blk_sz) - hme8blk_sz;
13379 13056 for (i = 0; i <= hblk8_bound; i += hme8blk_sz, j++) {
13380 13057 hmeblkp = (struct hme_blk *)addr;
13381 13058 addr += hme8blk_sz;
13382 13059 hmeblkp->hblk_nuc_bit = 1;
13383 13060 hmeblkp->hblk_nextpa = cached_va_to_pa((caddr_t)hmeblkp);
13384 13061 }
13385 13062 nucleus_hblk8.len = j;
13386 13063 ASSERT(j >= nhblk8);
13387 13064 SFMMU_STAT_ADD(sf_hblk8_ncreate, j);
13388 13065
13389 13066 nucleus_hblk1.list = (void *)addr;
13390 13067 nucleus_hblk1.index = 0;
13391 13068 for (; i <= (size - hme1blk_sz); i += hme1blk_sz, k++) {
13392 13069 hmeblkp = (struct hme_blk *)addr;
13393 13070 addr += hme1blk_sz;
13394 13071 hmeblkp->hblk_nuc_bit = 1;
13395 13072 hmeblkp->hblk_nextpa = cached_va_to_pa((caddr_t)hmeblkp);
13396 13073 }
13397 13074 ASSERT(k >= nhblk1);
13398 13075 nucleus_hblk1.len = k;
13399 13076 SFMMU_STAT_ADD(sf_hblk1_ncreate, k);
13400 13077 }
↓ open down ↓ |
3391 lines elided |
↑ open up ↑ |
13401 13078
13402 13079 /*
13403 13080 * This function is currently not supported on this platform. For what
13404 13081 * it's supposed to do, see hat.c and hat_srmmu.c
13405 13082 */
13406 13083 /* ARGSUSED */
13407 13084 faultcode_t
13408 13085 hat_softlock(struct hat *hat, caddr_t addr, size_t *lenp, page_t **ppp,
13409 13086 uint_t flags)
13410 13087 {
13411 - ASSERT(hat->sfmmu_xhat_provider == NULL);
13412 13088 return (FC_NOSUPPORT);
13413 13089 }
13414 13090
13415 13091 /*
13416 13092 * Searchs the mapping list of the page for a mapping of the same size. If not
13417 13093 * found the corresponding bit is cleared in the p_index field. When large
13418 13094 * pages are more prevalent in the system, we can maintain the mapping list
13419 13095 * in order and we don't have to traverse the list each time. Just check the
13420 13096 * next and prev entries, and if both are of different size, we clear the bit.
13421 13097 */
13422 13098 static void
13423 13099 sfmmu_rm_large_mappings(page_t *pp, int ttesz)
13424 13100 {
13425 13101 struct sf_hment *sfhmep;
13426 13102 struct hme_blk *hmeblkp;
13427 13103 int index;
13428 13104 pgcnt_t npgs;
13429 13105
13430 13106 ASSERT(ttesz > TTE8K);
13431 13107
13432 13108 ASSERT(sfmmu_mlist_held(pp));
13433 13109
13434 13110 ASSERT(PP_ISMAPPED_LARGE(pp));
13435 13111
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
13436 13112 /*
13437 13113 * Traverse mapping list looking for another mapping of same size.
13438 13114 * since we only want to clear index field if all mappings of
13439 13115 * that size are gone.
13440 13116 */
13441 13117
13442 13118 for (sfhmep = pp->p_mapping; sfhmep; sfhmep = sfhmep->hme_next) {
13443 13119 if (IS_PAHME(sfhmep))
13444 13120 continue;
13445 13121 hmeblkp = sfmmu_hmetohblk(sfhmep);
13446 - if (hmeblkp->hblk_xhat_bit)
13447 - continue;
13448 13122 if (hme_size(sfhmep) == ttesz) {
13449 13123 /*
13450 13124 * another mapping of the same size. don't clear index.
13451 13125 */
13452 13126 return;
13453 13127 }
13454 13128 }
13455 13129
13456 13130 /*
13457 13131 * Clear the p_index bit for large page.
13458 13132 */
13459 13133 index = PAGESZ_TO_INDEX(ttesz);
13460 13134 npgs = TTEPAGES(ttesz);
13461 13135 while (npgs-- > 0) {
13462 13136 ASSERT(pp->p_index & index);
13463 13137 pp->p_index &= ~index;
13464 13138 pp = PP_PAGENEXT(pp);
13465 13139 }
13466 13140 }
13467 13141
13468 13142 /*
13469 13143 * return supported features
13470 13144 */
13471 13145 /* ARGSUSED */
13472 13146 int
13473 13147 hat_supported(enum hat_features feature, void *arg)
13474 13148 {
13475 13149 switch (feature) {
13476 13150 case HAT_SHARED_PT:
13477 13151 case HAT_DYNAMIC_ISM_UNMAP:
13478 13152 case HAT_VMODSORT:
13479 13153 return (1);
13480 13154 case HAT_SHARED_REGIONS:
13481 13155 if (shctx_on)
13482 13156 return (1);
13483 13157 else
13484 13158 return (0);
13485 13159 default:
13486 13160 return (0);
13487 13161 }
13488 13162 }
13489 13163
13490 13164 void
13491 13165 hat_enter(struct hat *hat)
13492 13166 {
13493 13167 hatlock_t *hatlockp;
13494 13168
13495 13169 if (hat != ksfmmup) {
13496 13170 hatlockp = TSB_HASH(hat);
13497 13171 mutex_enter(HATLOCK_MUTEXP(hatlockp));
13498 13172 }
13499 13173 }
13500 13174
13501 13175 void
13502 13176 hat_exit(struct hat *hat)
13503 13177 {
13504 13178 hatlock_t *hatlockp;
13505 13179
13506 13180 if (hat != ksfmmup) {
13507 13181 hatlockp = TSB_HASH(hat);
13508 13182 mutex_exit(HATLOCK_MUTEXP(hatlockp));
13509 13183 }
13510 13184 }
13511 13185
13512 13186 /*ARGSUSED*/
13513 13187 void
13514 13188 hat_reserve(struct as *as, caddr_t addr, size_t len)
13515 13189 {
13516 13190 }
13517 13191
13518 13192 static void
13519 13193 hat_kstat_init(void)
13520 13194 {
13521 13195 kstat_t *ksp;
13522 13196
13523 13197 ksp = kstat_create("unix", 0, "sfmmu_global_stat", "hat",
13524 13198 KSTAT_TYPE_RAW, sizeof (struct sfmmu_global_stat),
13525 13199 KSTAT_FLAG_VIRTUAL);
13526 13200 if (ksp) {
13527 13201 ksp->ks_data = (void *) &sfmmu_global_stat;
13528 13202 kstat_install(ksp);
13529 13203 }
13530 13204 ksp = kstat_create("unix", 0, "sfmmu_tsbsize_stat", "hat",
13531 13205 KSTAT_TYPE_RAW, sizeof (struct sfmmu_tsbsize_stat),
13532 13206 KSTAT_FLAG_VIRTUAL);
13533 13207 if (ksp) {
13534 13208 ksp->ks_data = (void *) &sfmmu_tsbsize_stat;
13535 13209 kstat_install(ksp);
13536 13210 }
13537 13211 ksp = kstat_create("unix", 0, "sfmmu_percpu_stat", "hat",
13538 13212 KSTAT_TYPE_RAW, sizeof (struct sfmmu_percpu_stat) * NCPU,
13539 13213 KSTAT_FLAG_WRITABLE);
13540 13214 if (ksp) {
13541 13215 ksp->ks_update = sfmmu_kstat_percpu_update;
13542 13216 kstat_install(ksp);
13543 13217 }
13544 13218 }
13545 13219
13546 13220 /* ARGSUSED */
13547 13221 static int
13548 13222 sfmmu_kstat_percpu_update(kstat_t *ksp, int rw)
13549 13223 {
13550 13224 struct sfmmu_percpu_stat *cpu_kstat = ksp->ks_data;
13551 13225 struct tsbmiss *tsbm = tsbmiss_area;
13552 13226 struct kpmtsbm *kpmtsbm = kpmtsbm_area;
13553 13227 int i;
13554 13228
13555 13229 ASSERT(cpu_kstat);
13556 13230 if (rw == KSTAT_READ) {
13557 13231 for (i = 0; i < NCPU; cpu_kstat++, tsbm++, kpmtsbm++, i++) {
13558 13232 cpu_kstat->sf_itlb_misses = 0;
13559 13233 cpu_kstat->sf_dtlb_misses = 0;
13560 13234 cpu_kstat->sf_utsb_misses = tsbm->utsb_misses -
13561 13235 tsbm->uprot_traps;
13562 13236 cpu_kstat->sf_ktsb_misses = tsbm->ktsb_misses +
13563 13237 kpmtsbm->kpm_tsb_misses - tsbm->kprot_traps;
13564 13238 cpu_kstat->sf_tsb_hits = 0;
13565 13239 cpu_kstat->sf_umod_faults = tsbm->uprot_traps;
13566 13240 cpu_kstat->sf_kmod_faults = tsbm->kprot_traps;
13567 13241 }
13568 13242 } else {
13569 13243 /* KSTAT_WRITE is used to clear stats */
13570 13244 for (i = 0; i < NCPU; tsbm++, kpmtsbm++, i++) {
13571 13245 tsbm->utsb_misses = 0;
13572 13246 tsbm->ktsb_misses = 0;
13573 13247 tsbm->uprot_traps = 0;
13574 13248 tsbm->kprot_traps = 0;
13575 13249 kpmtsbm->kpm_dtlb_misses = 0;
13576 13250 kpmtsbm->kpm_tsb_misses = 0;
13577 13251 }
13578 13252 }
13579 13253 return (0);
13580 13254 }
13581 13255
13582 13256 #ifdef DEBUG
13583 13257
13584 13258 tte_t *gorig[NCPU], *gcur[NCPU], *gnew[NCPU];
13585 13259
13586 13260 /*
13587 13261 * A tte checker. *orig_old is the value we read before cas.
13588 13262 * *cur is the value returned by cas.
13589 13263 * *new is the desired value when we do the cas.
13590 13264 *
13591 13265 * *hmeblkp is currently unused.
13592 13266 */
13593 13267
13594 13268 /* ARGSUSED */
13595 13269 void
13596 13270 chk_tte(tte_t *orig_old, tte_t *cur, tte_t *new, struct hme_blk *hmeblkp)
13597 13271 {
13598 13272 pfn_t i, j, k;
13599 13273 int cpuid = CPU->cpu_id;
13600 13274
13601 13275 gorig[cpuid] = orig_old;
13602 13276 gcur[cpuid] = cur;
13603 13277 gnew[cpuid] = new;
13604 13278
13605 13279 #ifdef lint
13606 13280 hmeblkp = hmeblkp;
13607 13281 #endif
13608 13282
13609 13283 if (TTE_IS_VALID(orig_old)) {
13610 13284 if (TTE_IS_VALID(cur)) {
13611 13285 i = TTE_TO_TTEPFN(orig_old);
13612 13286 j = TTE_TO_TTEPFN(cur);
13613 13287 k = TTE_TO_TTEPFN(new);
13614 13288 if (i != j) {
13615 13289 /* remap error? */
13616 13290 panic("chk_tte: bad pfn, 0x%lx, 0x%lx", i, j);
13617 13291 }
13618 13292
13619 13293 if (i != k) {
13620 13294 /* remap error? */
13621 13295 panic("chk_tte: bad pfn2, 0x%lx, 0x%lx", i, k);
13622 13296 }
13623 13297 } else {
13624 13298 if (TTE_IS_VALID(new)) {
13625 13299 panic("chk_tte: invalid cur? ");
13626 13300 }
13627 13301
13628 13302 i = TTE_TO_TTEPFN(orig_old);
13629 13303 k = TTE_TO_TTEPFN(new);
13630 13304 if (i != k) {
13631 13305 panic("chk_tte: bad pfn3, 0x%lx, 0x%lx", i, k);
13632 13306 }
13633 13307 }
13634 13308 } else {
13635 13309 if (TTE_IS_VALID(cur)) {
13636 13310 j = TTE_TO_TTEPFN(cur);
13637 13311 if (TTE_IS_VALID(new)) {
13638 13312 k = TTE_TO_TTEPFN(new);
13639 13313 if (j != k) {
13640 13314 panic("chk_tte: bad pfn4, 0x%lx, 0x%lx",
13641 13315 j, k);
13642 13316 }
13643 13317 } else {
13644 13318 panic("chk_tte: why here?");
13645 13319 }
13646 13320 } else {
13647 13321 if (!TTE_IS_VALID(new)) {
13648 13322 panic("chk_tte: why here2 ?");
13649 13323 }
13650 13324 }
13651 13325 }
13652 13326 }
13653 13327
13654 13328 #endif /* DEBUG */
13655 13329
13656 13330 extern void prefetch_tsbe_read(struct tsbe *);
13657 13331 extern void prefetch_tsbe_write(struct tsbe *);
13658 13332
13659 13333
13660 13334 /*
13661 13335 * We want to prefetch 7 cache lines ahead for our read prefetch. This gives
13662 13336 * us optimal performance on Cheetah+. You can only have 8 outstanding
13663 13337 * prefetches at any one time, so we opted for 7 read prefetches and 1 write
13664 13338 * prefetch to make the most utilization of the prefetch capability.
13665 13339 */
13666 13340 #define TSBE_PREFETCH_STRIDE (7)
13667 13341
13668 13342 void
13669 13343 sfmmu_copy_tsb(struct tsb_info *old_tsbinfo, struct tsb_info *new_tsbinfo)
13670 13344 {
13671 13345 int old_bytes = TSB_BYTES(old_tsbinfo->tsb_szc);
13672 13346 int new_bytes = TSB_BYTES(new_tsbinfo->tsb_szc);
13673 13347 int old_entries = TSB_ENTRIES(old_tsbinfo->tsb_szc);
13674 13348 int new_entries = TSB_ENTRIES(new_tsbinfo->tsb_szc);
13675 13349 struct tsbe *old;
13676 13350 struct tsbe *new;
13677 13351 struct tsbe *new_base = (struct tsbe *)new_tsbinfo->tsb_va;
13678 13352 uint64_t va;
13679 13353 int new_offset;
13680 13354 int i;
13681 13355 int vpshift;
13682 13356 int last_prefetch;
13683 13357
13684 13358 if (old_bytes == new_bytes) {
13685 13359 bcopy(old_tsbinfo->tsb_va, new_tsbinfo->tsb_va, new_bytes);
13686 13360 } else {
13687 13361
13688 13362 /*
13689 13363 * A TSBE is 16 bytes which means there are four TSBE's per
13690 13364 * P$ line (64 bytes), thus every 4 TSBE's we prefetch.
13691 13365 */
13692 13366 old = (struct tsbe *)old_tsbinfo->tsb_va;
13693 13367 last_prefetch = old_entries - (4*(TSBE_PREFETCH_STRIDE+1));
13694 13368 for (i = 0; i < old_entries; i++, old++) {
13695 13369 if (((i & (4-1)) == 0) && (i < last_prefetch))
13696 13370 prefetch_tsbe_read(old);
13697 13371 if (!old->tte_tag.tag_invalid) {
13698 13372 /*
13699 13373 * We have a valid TTE to remap. Check the
13700 13374 * size. We won't remap 64K or 512K TTEs
13701 13375 * because they span more than one TSB entry
13702 13376 * and are indexed using an 8K virt. page.
13703 13377 * Ditto for 32M and 256M TTEs.
13704 13378 */
13705 13379 if (TTE_CSZ(&old->tte_data) == TTE64K ||
13706 13380 TTE_CSZ(&old->tte_data) == TTE512K)
13707 13381 continue;
13708 13382 if (mmu_page_sizes == max_mmu_page_sizes) {
13709 13383 if (TTE_CSZ(&old->tte_data) == TTE32M ||
13710 13384 TTE_CSZ(&old->tte_data) == TTE256M)
13711 13385 continue;
13712 13386 }
13713 13387
13714 13388 /* clear the lower 22 bits of the va */
13715 13389 va = *(uint64_t *)old << 22;
13716 13390 /* turn va into a virtual pfn */
13717 13391 va >>= 22 - TSB_START_SIZE;
13718 13392 /*
13719 13393 * or in bits from the offset in the tsb
13720 13394 * to get the real virtual pfn. These
13721 13395 * correspond to bits [21:13] in the va
13722 13396 */
13723 13397 vpshift =
13724 13398 TTE_BSZS_SHIFT(TTE_CSZ(&old->tte_data)) &
13725 13399 0x1ff;
13726 13400 va |= (i << vpshift);
13727 13401 va >>= vpshift;
13728 13402 new_offset = va & (new_entries - 1);
13729 13403 new = new_base + new_offset;
13730 13404 prefetch_tsbe_write(new);
13731 13405 *new = *old;
13732 13406 }
13733 13407 }
13734 13408 }
13735 13409 }
13736 13410
13737 13411 /*
13738 13412 * unused in sfmmu
13739 13413 */
13740 13414 void
13741 13415 hat_dump(void)
13742 13416 {
13743 13417 }
13744 13418
13745 13419 /*
13746 13420 * Called when a thread is exiting and we have switched to the kernel address
13747 13421 * space. Perform the same VM initialization resume() uses when switching
13748 13422 * processes.
13749 13423 *
13750 13424 * Note that sfmmu_load_mmustate() is currently a no-op for kernel threads, but
13751 13425 * we call it anyway in case the semantics change in the future.
13752 13426 */
13753 13427 /*ARGSUSED*/
13754 13428 void
13755 13429 hat_thread_exit(kthread_t *thd)
13756 13430 {
13757 13431 uint_t pgsz_cnum;
13758 13432 uint_t pstate_save;
13759 13433
13760 13434 ASSERT(thd->t_procp->p_as == &kas);
13761 13435
13762 13436 pgsz_cnum = KCONTEXT;
13763 13437 #ifdef sun4u
13764 13438 pgsz_cnum |= (ksfmmup->sfmmu_cext << CTXREG_EXT_SHIFT);
13765 13439 #endif
13766 13440
13767 13441 /*
13768 13442 * Note that sfmmu_load_mmustate() is currently a no-op for
13769 13443 * kernel threads. We need to disable interrupts here,
13770 13444 * simply because otherwise sfmmu_load_mmustate() would panic
13771 13445 * if the caller does not disable interrupts.
13772 13446 */
13773 13447 pstate_save = sfmmu_disable_intrs();
13774 13448
13775 13449 /* Compatibility Note: hw takes care of MMU_SCONTEXT1 */
13776 13450 sfmmu_setctx_sec(pgsz_cnum);
13777 13451 sfmmu_load_mmustate(ksfmmup);
13778 13452 sfmmu_enable_intrs(pstate_save);
13779 13453 }
13780 13454
13781 13455
13782 13456 /*
13783 13457 * SRD support
13784 13458 */
13785 13459 #define SRD_HASH_FUNCTION(vp) (((((uintptr_t)(vp)) >> 4) ^ \
13786 13460 (((uintptr_t)(vp)) >> 11)) & \
13787 13461 srd_hashmask)
13788 13462
13789 13463 /*
13790 13464 * Attach the process to the srd struct associated with the exec vnode
13791 13465 * from which the process is started.
13792 13466 */
13793 13467 void
13794 13468 hat_join_srd(struct hat *sfmmup, vnode_t *evp)
13795 13469 {
13796 13470 uint_t hash = SRD_HASH_FUNCTION(evp);
13797 13471 sf_srd_t *srdp;
13798 13472 sf_srd_t *newsrdp;
13799 13473
13800 13474 ASSERT(sfmmup != ksfmmup);
13801 13475 ASSERT(sfmmup->sfmmu_srdp == NULL);
13802 13476
13803 13477 if (!shctx_on) {
13804 13478 return;
13805 13479 }
13806 13480
13807 13481 VN_HOLD(evp);
13808 13482
13809 13483 if (srd_buckets[hash].srdb_srdp != NULL) {
13810 13484 mutex_enter(&srd_buckets[hash].srdb_lock);
13811 13485 for (srdp = srd_buckets[hash].srdb_srdp; srdp != NULL;
13812 13486 srdp = srdp->srd_hash) {
13813 13487 if (srdp->srd_evp == evp) {
13814 13488 ASSERT(srdp->srd_refcnt >= 0);
13815 13489 sfmmup->sfmmu_srdp = srdp;
13816 13490 atomic_inc_32(
13817 13491 (volatile uint_t *)&srdp->srd_refcnt);
13818 13492 mutex_exit(&srd_buckets[hash].srdb_lock);
13819 13493 return;
13820 13494 }
13821 13495 }
13822 13496 mutex_exit(&srd_buckets[hash].srdb_lock);
13823 13497 }
13824 13498 newsrdp = kmem_cache_alloc(srd_cache, KM_SLEEP);
13825 13499 ASSERT(newsrdp->srd_next_ismrid == 0 && newsrdp->srd_next_hmerid == 0);
13826 13500
13827 13501 newsrdp->srd_evp = evp;
13828 13502 newsrdp->srd_refcnt = 1;
13829 13503 newsrdp->srd_hmergnfree = NULL;
13830 13504 newsrdp->srd_ismrgnfree = NULL;
13831 13505
13832 13506 mutex_enter(&srd_buckets[hash].srdb_lock);
13833 13507 for (srdp = srd_buckets[hash].srdb_srdp; srdp != NULL;
13834 13508 srdp = srdp->srd_hash) {
13835 13509 if (srdp->srd_evp == evp) {
13836 13510 ASSERT(srdp->srd_refcnt >= 0);
13837 13511 sfmmup->sfmmu_srdp = srdp;
13838 13512 atomic_inc_32((volatile uint_t *)&srdp->srd_refcnt);
13839 13513 mutex_exit(&srd_buckets[hash].srdb_lock);
13840 13514 kmem_cache_free(srd_cache, newsrdp);
13841 13515 return;
13842 13516 }
13843 13517 }
13844 13518 newsrdp->srd_hash = srd_buckets[hash].srdb_srdp;
13845 13519 srd_buckets[hash].srdb_srdp = newsrdp;
13846 13520 sfmmup->sfmmu_srdp = newsrdp;
13847 13521
13848 13522 mutex_exit(&srd_buckets[hash].srdb_lock);
13849 13523
13850 13524 }
13851 13525
13852 13526 static void
13853 13527 sfmmu_leave_srd(sfmmu_t *sfmmup)
13854 13528 {
13855 13529 vnode_t *evp;
13856 13530 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
13857 13531 uint_t hash;
13858 13532 sf_srd_t **prev_srdpp;
13859 13533 sf_region_t *rgnp;
13860 13534 sf_region_t *nrgnp;
13861 13535 #ifdef DEBUG
13862 13536 int rgns = 0;
13863 13537 #endif
13864 13538 int i;
13865 13539
13866 13540 ASSERT(sfmmup != ksfmmup);
13867 13541 ASSERT(srdp != NULL);
13868 13542 ASSERT(srdp->srd_refcnt > 0);
13869 13543 ASSERT(sfmmup->sfmmu_scdp == NULL);
13870 13544 ASSERT(sfmmup->sfmmu_free == 1);
13871 13545
13872 13546 sfmmup->sfmmu_srdp = NULL;
13873 13547 evp = srdp->srd_evp;
13874 13548 ASSERT(evp != NULL);
13875 13549 if (atomic_dec_32_nv((volatile uint_t *)&srdp->srd_refcnt)) {
13876 13550 VN_RELE(evp);
13877 13551 return;
13878 13552 }
13879 13553
13880 13554 hash = SRD_HASH_FUNCTION(evp);
13881 13555 mutex_enter(&srd_buckets[hash].srdb_lock);
13882 13556 for (prev_srdpp = &srd_buckets[hash].srdb_srdp;
13883 13557 (srdp = *prev_srdpp) != NULL; prev_srdpp = &srdp->srd_hash) {
13884 13558 if (srdp->srd_evp == evp) {
13885 13559 break;
13886 13560 }
13887 13561 }
13888 13562 if (srdp == NULL || srdp->srd_refcnt) {
13889 13563 mutex_exit(&srd_buckets[hash].srdb_lock);
13890 13564 VN_RELE(evp);
13891 13565 return;
13892 13566 }
13893 13567 *prev_srdpp = srdp->srd_hash;
13894 13568 mutex_exit(&srd_buckets[hash].srdb_lock);
13895 13569
13896 13570 ASSERT(srdp->srd_refcnt == 0);
13897 13571 VN_RELE(evp);
13898 13572
13899 13573 #ifdef DEBUG
13900 13574 for (i = 0; i < SFMMU_MAX_REGION_BUCKETS; i++) {
13901 13575 ASSERT(srdp->srd_rgnhash[i] == NULL);
13902 13576 }
13903 13577 #endif /* DEBUG */
13904 13578
13905 13579 /* free each hme regions in the srd */
13906 13580 for (rgnp = srdp->srd_hmergnfree; rgnp != NULL; rgnp = nrgnp) {
13907 13581 nrgnp = rgnp->rgn_next;
13908 13582 ASSERT(rgnp->rgn_id < srdp->srd_next_hmerid);
13909 13583 ASSERT(rgnp->rgn_refcnt == 0);
13910 13584 ASSERT(rgnp->rgn_sfmmu_head == NULL);
13911 13585 ASSERT(rgnp->rgn_flags & SFMMU_REGION_FREE);
13912 13586 ASSERT(rgnp->rgn_hmeflags == 0);
13913 13587 ASSERT(srdp->srd_hmergnp[rgnp->rgn_id] == rgnp);
13914 13588 #ifdef DEBUG
13915 13589 for (i = 0; i < MMU_PAGE_SIZES; i++) {
13916 13590 ASSERT(rgnp->rgn_ttecnt[i] == 0);
13917 13591 }
13918 13592 rgns++;
13919 13593 #endif /* DEBUG */
13920 13594 kmem_cache_free(region_cache, rgnp);
13921 13595 }
13922 13596 ASSERT(rgns == srdp->srd_next_hmerid);
13923 13597
13924 13598 #ifdef DEBUG
13925 13599 rgns = 0;
13926 13600 #endif
13927 13601 /* free each ism rgns in the srd */
13928 13602 for (rgnp = srdp->srd_ismrgnfree; rgnp != NULL; rgnp = nrgnp) {
13929 13603 nrgnp = rgnp->rgn_next;
13930 13604 ASSERT(rgnp->rgn_id < srdp->srd_next_ismrid);
13931 13605 ASSERT(rgnp->rgn_refcnt == 0);
13932 13606 ASSERT(rgnp->rgn_sfmmu_head == NULL);
13933 13607 ASSERT(rgnp->rgn_flags & SFMMU_REGION_FREE);
13934 13608 ASSERT(srdp->srd_ismrgnp[rgnp->rgn_id] == rgnp);
13935 13609 #ifdef DEBUG
13936 13610 for (i = 0; i < MMU_PAGE_SIZES; i++) {
13937 13611 ASSERT(rgnp->rgn_ttecnt[i] == 0);
13938 13612 }
13939 13613 rgns++;
13940 13614 #endif /* DEBUG */
13941 13615 kmem_cache_free(region_cache, rgnp);
13942 13616 }
13943 13617 ASSERT(rgns == srdp->srd_next_ismrid);
13944 13618 ASSERT(srdp->srd_ismbusyrgns == 0);
13945 13619 ASSERT(srdp->srd_hmebusyrgns == 0);
13946 13620
13947 13621 srdp->srd_next_ismrid = 0;
13948 13622 srdp->srd_next_hmerid = 0;
13949 13623
13950 13624 bzero((void *)srdp->srd_ismrgnp,
13951 13625 sizeof (sf_region_t *) * SFMMU_MAX_ISM_REGIONS);
13952 13626 bzero((void *)srdp->srd_hmergnp,
13953 13627 sizeof (sf_region_t *) * SFMMU_MAX_HME_REGIONS);
13954 13628
13955 13629 ASSERT(srdp->srd_scdp == NULL);
13956 13630 kmem_cache_free(srd_cache, srdp);
13957 13631 }
13958 13632
13959 13633 /* ARGSUSED */
13960 13634 static int
13961 13635 sfmmu_srdcache_constructor(void *buf, void *cdrarg, int kmflags)
13962 13636 {
13963 13637 sf_srd_t *srdp = (sf_srd_t *)buf;
13964 13638 bzero(buf, sizeof (*srdp));
13965 13639
13966 13640 mutex_init(&srdp->srd_mutex, NULL, MUTEX_DEFAULT, NULL);
13967 13641 mutex_init(&srdp->srd_scd_mutex, NULL, MUTEX_DEFAULT, NULL);
13968 13642 return (0);
13969 13643 }
13970 13644
13971 13645 /* ARGSUSED */
13972 13646 static void
13973 13647 sfmmu_srdcache_destructor(void *buf, void *cdrarg)
13974 13648 {
13975 13649 sf_srd_t *srdp = (sf_srd_t *)buf;
13976 13650
13977 13651 mutex_destroy(&srdp->srd_mutex);
13978 13652 mutex_destroy(&srdp->srd_scd_mutex);
13979 13653 }
13980 13654
13981 13655 /*
13982 13656 * The caller makes sure hat_join_region()/hat_leave_region() can't be called
13983 13657 * at the same time for the same process and address range. This is ensured by
13984 13658 * the fact that address space is locked as writer when a process joins the
13985 13659 * regions. Therefore there's no need to hold an srd lock during the entire
13986 13660 * execution of hat_join_region()/hat_leave_region().
13987 13661 */
13988 13662
13989 13663 #define RGN_HASH_FUNCTION(obj) (((((uintptr_t)(obj)) >> 4) ^ \
13990 13664 (((uintptr_t)(obj)) >> 11)) & \
13991 13665 srd_rgn_hashmask)
13992 13666 /*
13993 13667 * This routine implements the shared context functionality required when
13994 13668 * attaching a segment to an address space. It must be called from
13995 13669 * hat_share() for D(ISM) segments and from segvn_create() for segments
13996 13670 * with the MAP_PRIVATE and MAP_TEXT flags set. It returns a region_cookie
13997 13671 * which is saved in the private segment data for hme segments and
13998 13672 * the ism_map structure for ism segments.
13999 13673 */
14000 13674 hat_region_cookie_t
14001 13675 hat_join_region(struct hat *sfmmup,
14002 13676 caddr_t r_saddr,
14003 13677 size_t r_size,
14004 13678 void *r_obj,
14005 13679 u_offset_t r_objoff,
14006 13680 uchar_t r_perm,
14007 13681 uchar_t r_pgszc,
14008 13682 hat_rgn_cb_func_t r_cb_function,
14009 13683 uint_t flags)
14010 13684 {
14011 13685 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
14012 13686 uint_t rhash;
14013 13687 uint_t rid;
14014 13688 hatlock_t *hatlockp;
14015 13689 sf_region_t *rgnp;
14016 13690 sf_region_t *new_rgnp = NULL;
14017 13691 int i;
14018 13692 uint16_t *nextidp;
14019 13693 sf_region_t **freelistp;
14020 13694 int maxids;
14021 13695 sf_region_t **rarrp;
↓ open down ↓ |
564 lines elided |
↑ open up ↑ |
14022 13696 uint16_t *busyrgnsp;
14023 13697 ulong_t rttecnt;
14024 13698 uchar_t tteflag;
14025 13699 uchar_t r_type = flags & HAT_REGION_TYPE_MASK;
14026 13700 int text = (r_type == HAT_REGION_TEXT);
14027 13701
14028 13702 if (srdp == NULL || r_size == 0) {
14029 13703 return (HAT_INVALID_REGION_COOKIE);
14030 13704 }
14031 13705
14032 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
14033 13706 ASSERT(sfmmup != ksfmmup);
14034 13707 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
14035 13708 ASSERT(srdp->srd_refcnt > 0);
14036 13709 ASSERT(!(flags & ~HAT_REGION_TYPE_MASK));
14037 13710 ASSERT(flags == HAT_REGION_TEXT || flags == HAT_REGION_ISM);
14038 13711 ASSERT(r_pgszc < mmu_page_sizes);
14039 13712 if (!IS_P2ALIGNED(r_saddr, TTEBYTES(r_pgszc)) ||
14040 13713 !IS_P2ALIGNED(r_size, TTEBYTES(r_pgszc))) {
14041 13714 panic("hat_join_region: region addr or size is not aligned\n");
14042 13715 }
14043 13716
14044 13717
14045 13718 r_type = (r_type == HAT_REGION_ISM) ? SFMMU_REGION_ISM :
14046 13719 SFMMU_REGION_HME;
14047 13720 /*
14048 13721 * Currently only support shared hmes for the read only main text
14049 13722 * region.
14050 13723 */
14051 13724 if (r_type == SFMMU_REGION_HME && ((r_obj != srdp->srd_evp) ||
14052 13725 (r_perm & PROT_WRITE))) {
14053 13726 return (HAT_INVALID_REGION_COOKIE);
14054 13727 }
14055 13728
14056 13729 rhash = RGN_HASH_FUNCTION(r_obj);
14057 13730
14058 13731 if (r_type == SFMMU_REGION_ISM) {
14059 13732 nextidp = &srdp->srd_next_ismrid;
14060 13733 freelistp = &srdp->srd_ismrgnfree;
14061 13734 maxids = SFMMU_MAX_ISM_REGIONS;
14062 13735 rarrp = srdp->srd_ismrgnp;
14063 13736 busyrgnsp = &srdp->srd_ismbusyrgns;
14064 13737 } else {
14065 13738 nextidp = &srdp->srd_next_hmerid;
14066 13739 freelistp = &srdp->srd_hmergnfree;
14067 13740 maxids = SFMMU_MAX_HME_REGIONS;
14068 13741 rarrp = srdp->srd_hmergnp;
14069 13742 busyrgnsp = &srdp->srd_hmebusyrgns;
14070 13743 }
14071 13744
14072 13745 mutex_enter(&srdp->srd_mutex);
14073 13746
14074 13747 for (rgnp = srdp->srd_rgnhash[rhash]; rgnp != NULL;
14075 13748 rgnp = rgnp->rgn_hash) {
14076 13749 if (rgnp->rgn_saddr == r_saddr && rgnp->rgn_size == r_size &&
14077 13750 rgnp->rgn_obj == r_obj && rgnp->rgn_objoff == r_objoff &&
14078 13751 rgnp->rgn_perm == r_perm && rgnp->rgn_pgszc == r_pgszc) {
14079 13752 break;
14080 13753 }
14081 13754 }
14082 13755
14083 13756 rfound:
14084 13757 if (rgnp != NULL) {
14085 13758 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type);
14086 13759 ASSERT(rgnp->rgn_cb_function == r_cb_function);
14087 13760 ASSERT(rgnp->rgn_refcnt >= 0);
14088 13761 rid = rgnp->rgn_id;
14089 13762 ASSERT(rid < maxids);
14090 13763 ASSERT(rarrp[rid] == rgnp);
14091 13764 ASSERT(rid < *nextidp);
14092 13765 atomic_inc_32((volatile uint_t *)&rgnp->rgn_refcnt);
14093 13766 mutex_exit(&srdp->srd_mutex);
14094 13767 if (new_rgnp != NULL) {
14095 13768 kmem_cache_free(region_cache, new_rgnp);
14096 13769 }
14097 13770 if (r_type == SFMMU_REGION_HME) {
14098 13771 int myjoin =
14099 13772 (sfmmup == astosfmmu(curthread->t_procp->p_as));
14100 13773
14101 13774 sfmmu_link_to_hmeregion(sfmmup, rgnp);
14102 13775 /*
14103 13776 * bitmap should be updated after linking sfmmu on
14104 13777 * region list so that pageunload() doesn't skip
14105 13778 * TSB/TLB flush. As soon as bitmap is updated another
14106 13779 * thread in this process can already start accessing
14107 13780 * this region.
14108 13781 */
14109 13782 /*
14110 13783 * Normally ttecnt accounting is done as part of
14111 13784 * pagefault handling. But a process may not take any
14112 13785 * pagefaults on shared hmeblks created by some other
14113 13786 * process. To compensate for this assume that the
14114 13787 * entire region will end up faulted in using
14115 13788 * the region's pagesize.
14116 13789 *
14117 13790 */
14118 13791 if (r_pgszc > TTE8K) {
14119 13792 tteflag = 1 << r_pgszc;
14120 13793 if (disable_large_pages & tteflag) {
14121 13794 tteflag = 0;
14122 13795 }
14123 13796 } else {
14124 13797 tteflag = 0;
14125 13798 }
14126 13799 if (tteflag && !(sfmmup->sfmmu_rtteflags & tteflag)) {
14127 13800 hatlockp = sfmmu_hat_enter(sfmmup);
14128 13801 sfmmup->sfmmu_rtteflags |= tteflag;
14129 13802 sfmmu_hat_exit(hatlockp);
14130 13803 }
14131 13804 hatlockp = sfmmu_hat_enter(sfmmup);
14132 13805
14133 13806 /*
14134 13807 * Preallocate 1/4 of ttecnt's in 8K TSB for >= 4M
14135 13808 * region to allow for large page allocation failure.
14136 13809 */
14137 13810 if (r_pgszc >= TTE4M) {
14138 13811 sfmmup->sfmmu_tsb0_4minflcnt +=
14139 13812 r_size >> (TTE_PAGE_SHIFT(TTE8K) + 2);
14140 13813 }
14141 13814
14142 13815 /* update sfmmu_ttecnt with the shme rgn ttecnt */
14143 13816 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc);
14144 13817 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc],
14145 13818 rttecnt);
14146 13819
14147 13820 if (text && r_pgszc >= TTE4M &&
14148 13821 (tteflag || ((disable_large_pages >> TTE4M) &
14149 13822 ((1 << (r_pgszc - TTE4M + 1)) - 1))) &&
14150 13823 !SFMMU_FLAGS_ISSET(sfmmup, HAT_4MTEXT_FLAG)) {
14151 13824 SFMMU_FLAGS_SET(sfmmup, HAT_4MTEXT_FLAG);
14152 13825 }
14153 13826
14154 13827 sfmmu_hat_exit(hatlockp);
14155 13828 /*
14156 13829 * On Panther we need to make sure TLB is programmed
14157 13830 * to accept 32M/256M pages. Call
14158 13831 * sfmmu_check_page_sizes() now to make sure TLB is
14159 13832 * setup before making hmeregions visible to other
14160 13833 * threads.
14161 13834 */
14162 13835 sfmmu_check_page_sizes(sfmmup, 1);
14163 13836 hatlockp = sfmmu_hat_enter(sfmmup);
14164 13837 SF_RGNMAP_ADD(sfmmup->sfmmu_hmeregion_map, rid);
14165 13838
14166 13839 /*
14167 13840 * if context is invalid tsb miss exception code will
14168 13841 * call sfmmu_check_page_sizes() and update tsbmiss
14169 13842 * area later.
14170 13843 */
14171 13844 kpreempt_disable();
14172 13845 if (myjoin &&
14173 13846 (sfmmup->sfmmu_ctxs[CPU_MMU_IDX(CPU)].cnum
14174 13847 != INVALID_CONTEXT)) {
14175 13848 struct tsbmiss *tsbmp;
14176 13849
14177 13850 tsbmp = &tsbmiss_area[CPU->cpu_id];
14178 13851 ASSERT(sfmmup == tsbmp->usfmmup);
14179 13852 BT_SET(tsbmp->shmermap, rid);
14180 13853 if (r_pgszc > TTE64K) {
14181 13854 tsbmp->uhat_rtteflags |= tteflag;
14182 13855 }
14183 13856
14184 13857 }
14185 13858 kpreempt_enable();
14186 13859
14187 13860 sfmmu_hat_exit(hatlockp);
14188 13861 ASSERT((hat_region_cookie_t)((uint64_t)rid) !=
14189 13862 HAT_INVALID_REGION_COOKIE);
14190 13863 } else {
14191 13864 hatlockp = sfmmu_hat_enter(sfmmup);
14192 13865 SF_RGNMAP_ADD(sfmmup->sfmmu_ismregion_map, rid);
14193 13866 sfmmu_hat_exit(hatlockp);
14194 13867 }
14195 13868 ASSERT(rid < maxids);
14196 13869
14197 13870 if (r_type == SFMMU_REGION_ISM) {
14198 13871 sfmmu_find_scd(sfmmup);
14199 13872 }
14200 13873 return ((hat_region_cookie_t)((uint64_t)rid));
14201 13874 }
14202 13875
14203 13876 ASSERT(new_rgnp == NULL);
14204 13877
14205 13878 if (*busyrgnsp >= maxids) {
14206 13879 mutex_exit(&srdp->srd_mutex);
14207 13880 return (HAT_INVALID_REGION_COOKIE);
14208 13881 }
14209 13882
14210 13883 ASSERT(MUTEX_HELD(&srdp->srd_mutex));
14211 13884 if (*freelistp != NULL) {
14212 13885 rgnp = *freelistp;
14213 13886 *freelistp = rgnp->rgn_next;
14214 13887 ASSERT(rgnp->rgn_id < *nextidp);
14215 13888 ASSERT(rgnp->rgn_id < maxids);
14216 13889 ASSERT(rgnp->rgn_flags & SFMMU_REGION_FREE);
14217 13890 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK)
14218 13891 == r_type);
14219 13892 ASSERT(rarrp[rgnp->rgn_id] == rgnp);
14220 13893 ASSERT(rgnp->rgn_hmeflags == 0);
14221 13894 } else {
14222 13895 /*
14223 13896 * release local locks before memory allocation.
14224 13897 */
14225 13898 mutex_exit(&srdp->srd_mutex);
14226 13899
14227 13900 new_rgnp = kmem_cache_alloc(region_cache, KM_SLEEP);
14228 13901
14229 13902 mutex_enter(&srdp->srd_mutex);
14230 13903 for (rgnp = srdp->srd_rgnhash[rhash]; rgnp != NULL;
14231 13904 rgnp = rgnp->rgn_hash) {
14232 13905 if (rgnp->rgn_saddr == r_saddr &&
14233 13906 rgnp->rgn_size == r_size &&
14234 13907 rgnp->rgn_obj == r_obj &&
14235 13908 rgnp->rgn_objoff == r_objoff &&
14236 13909 rgnp->rgn_perm == r_perm &&
14237 13910 rgnp->rgn_pgszc == r_pgszc) {
14238 13911 break;
14239 13912 }
14240 13913 }
14241 13914 if (rgnp != NULL) {
14242 13915 goto rfound;
14243 13916 }
14244 13917
14245 13918 if (*nextidp >= maxids) {
14246 13919 mutex_exit(&srdp->srd_mutex);
14247 13920 goto fail;
14248 13921 }
14249 13922 rgnp = new_rgnp;
14250 13923 new_rgnp = NULL;
14251 13924 rgnp->rgn_id = (*nextidp)++;
14252 13925 ASSERT(rgnp->rgn_id < maxids);
14253 13926 ASSERT(rarrp[rgnp->rgn_id] == NULL);
14254 13927 rarrp[rgnp->rgn_id] = rgnp;
14255 13928 }
14256 13929
14257 13930 ASSERT(rgnp->rgn_sfmmu_head == NULL);
14258 13931 ASSERT(rgnp->rgn_hmeflags == 0);
14259 13932 #ifdef DEBUG
14260 13933 for (i = 0; i < MMU_PAGE_SIZES; i++) {
14261 13934 ASSERT(rgnp->rgn_ttecnt[i] == 0);
14262 13935 }
14263 13936 #endif
14264 13937 rgnp->rgn_saddr = r_saddr;
14265 13938 rgnp->rgn_size = r_size;
14266 13939 rgnp->rgn_obj = r_obj;
14267 13940 rgnp->rgn_objoff = r_objoff;
14268 13941 rgnp->rgn_perm = r_perm;
14269 13942 rgnp->rgn_pgszc = r_pgszc;
14270 13943 rgnp->rgn_flags = r_type;
14271 13944 rgnp->rgn_refcnt = 0;
14272 13945 rgnp->rgn_cb_function = r_cb_function;
14273 13946 rgnp->rgn_hash = srdp->srd_rgnhash[rhash];
14274 13947 srdp->srd_rgnhash[rhash] = rgnp;
14275 13948 (*busyrgnsp)++;
14276 13949 ASSERT(*busyrgnsp <= maxids);
14277 13950 goto rfound;
14278 13951
14279 13952 fail:
14280 13953 ASSERT(new_rgnp != NULL);
14281 13954 kmem_cache_free(region_cache, new_rgnp);
14282 13955 return (HAT_INVALID_REGION_COOKIE);
14283 13956 }
14284 13957
14285 13958 /*
14286 13959 * This function implements the shared context functionality required
14287 13960 * when detaching a segment from an address space. It must be called
14288 13961 * from hat_unshare() for all D(ISM) segments and from segvn_unmap(),
14289 13962 * for segments with a valid region_cookie.
14290 13963 * It will also be called from all seg_vn routines which change a
14291 13964 * segment's attributes such as segvn_setprot(), segvn_setpagesize(),
14292 13965 * segvn_clrszc() & segvn_advise(), as well as in the case of COW fault
14293 13966 * from segvn_fault().
14294 13967 */
14295 13968 void
14296 13969 hat_leave_region(struct hat *sfmmup, hat_region_cookie_t rcookie, uint_t flags)
14297 13970 {
14298 13971 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
14299 13972 sf_scd_t *scdp;
14300 13973 uint_t rhash;
14301 13974 uint_t rid = (uint_t)((uint64_t)rcookie);
14302 13975 hatlock_t *hatlockp = NULL;
14303 13976 sf_region_t *rgnp;
14304 13977 sf_region_t **prev_rgnpp;
14305 13978 sf_region_t *cur_rgnp;
14306 13979 void *r_obj;
14307 13980 int i;
14308 13981 caddr_t r_saddr;
14309 13982 caddr_t r_eaddr;
14310 13983 size_t r_size;
14311 13984 uchar_t r_pgszc;
14312 13985 uchar_t r_type = flags & HAT_REGION_TYPE_MASK;
14313 13986
14314 13987 ASSERT(sfmmup != ksfmmup);
14315 13988 ASSERT(srdp != NULL);
14316 13989 ASSERT(srdp->srd_refcnt > 0);
14317 13990 ASSERT(!(flags & ~HAT_REGION_TYPE_MASK));
14318 13991 ASSERT(flags == HAT_REGION_TEXT || flags == HAT_REGION_ISM);
14319 13992 ASSERT(!sfmmup->sfmmu_free || sfmmup->sfmmu_scdp == NULL);
14320 13993
14321 13994 r_type = (r_type == HAT_REGION_ISM) ? SFMMU_REGION_ISM :
14322 13995 SFMMU_REGION_HME;
14323 13996
14324 13997 if (r_type == SFMMU_REGION_ISM) {
14325 13998 ASSERT(SFMMU_IS_ISMRID_VALID(rid));
14326 13999 ASSERT(rid < SFMMU_MAX_ISM_REGIONS);
14327 14000 rgnp = srdp->srd_ismrgnp[rid];
↓ open down ↓ |
285 lines elided |
↑ open up ↑ |
14328 14001 } else {
14329 14002 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
14330 14003 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
14331 14004 rgnp = srdp->srd_hmergnp[rid];
14332 14005 }
14333 14006 ASSERT(rgnp != NULL);
14334 14007 ASSERT(rgnp->rgn_id == rid);
14335 14008 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type);
14336 14009 ASSERT(!(rgnp->rgn_flags & SFMMU_REGION_FREE));
14337 14010 ASSERT(AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
14338 -
14339 - ASSERT(sfmmup->sfmmu_xhat_provider == NULL);
14340 - if (r_type == SFMMU_REGION_HME && sfmmup->sfmmu_as->a_xhat != NULL) {
14341 - xhat_unload_callback_all(sfmmup->sfmmu_as, rgnp->rgn_saddr,
14342 - rgnp->rgn_size, 0, NULL);
14343 - }
14344 14011
14345 14012 if (sfmmup->sfmmu_free) {
14346 14013 ulong_t rttecnt;
14347 14014 r_pgszc = rgnp->rgn_pgszc;
14348 14015 r_size = rgnp->rgn_size;
14349 14016
14350 14017 ASSERT(sfmmup->sfmmu_scdp == NULL);
14351 14018 if (r_type == SFMMU_REGION_ISM) {
14352 14019 SF_RGNMAP_DEL(sfmmup->sfmmu_ismregion_map, rid);
14353 14020 } else {
14354 14021 /* update shme rgns ttecnt in sfmmu_ttecnt */
14355 14022 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc);
14356 14023 ASSERT(sfmmup->sfmmu_ttecnt[r_pgszc] >= rttecnt);
14357 14024
14358 14025 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc],
14359 14026 -rttecnt);
14360 14027
14361 14028 SF_RGNMAP_DEL(sfmmup->sfmmu_hmeregion_map, rid);
14362 14029 }
14363 14030 } else if (r_type == SFMMU_REGION_ISM) {
14364 14031 hatlockp = sfmmu_hat_enter(sfmmup);
14365 14032 ASSERT(rid < srdp->srd_next_ismrid);
14366 14033 SF_RGNMAP_DEL(sfmmup->sfmmu_ismregion_map, rid);
14367 14034 scdp = sfmmup->sfmmu_scdp;
14368 14035 if (scdp != NULL &&
14369 14036 SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid)) {
14370 14037 sfmmu_leave_scd(sfmmup, r_type);
14371 14038 ASSERT(sfmmu_hat_lock_held(sfmmup));
14372 14039 }
14373 14040 sfmmu_hat_exit(hatlockp);
14374 14041 } else {
14375 14042 ulong_t rttecnt;
14376 14043 r_pgszc = rgnp->rgn_pgszc;
14377 14044 r_saddr = rgnp->rgn_saddr;
14378 14045 r_size = rgnp->rgn_size;
14379 14046 r_eaddr = r_saddr + r_size;
14380 14047
14381 14048 ASSERT(r_type == SFMMU_REGION_HME);
14382 14049 hatlockp = sfmmu_hat_enter(sfmmup);
14383 14050 ASSERT(rid < srdp->srd_next_hmerid);
14384 14051 SF_RGNMAP_DEL(sfmmup->sfmmu_hmeregion_map, rid);
14385 14052
14386 14053 /*
14387 14054 * If region is part of an SCD call sfmmu_leave_scd().
14388 14055 * Otherwise if process is not exiting and has valid context
14389 14056 * just drop the context on the floor to lose stale TLB
14390 14057 * entries and force the update of tsb miss area to reflect
14391 14058 * the new region map. After that clean our TSB entries.
14392 14059 */
14393 14060 scdp = sfmmup->sfmmu_scdp;
14394 14061 if (scdp != NULL &&
14395 14062 SF_RGNMAP_TEST(scdp->scd_hmeregion_map, rid)) {
14396 14063 sfmmu_leave_scd(sfmmup, r_type);
14397 14064 ASSERT(sfmmu_hat_lock_held(sfmmup));
14398 14065 }
14399 14066 sfmmu_invalidate_ctx(sfmmup);
14400 14067
14401 14068 i = TTE8K;
14402 14069 while (i < mmu_page_sizes) {
14403 14070 if (rgnp->rgn_ttecnt[i] != 0) {
14404 14071 sfmmu_unload_tsb_range(sfmmup, r_saddr,
14405 14072 r_eaddr, i);
14406 14073 if (i < TTE4M) {
14407 14074 i = TTE4M;
14408 14075 continue;
14409 14076 } else {
14410 14077 break;
14411 14078 }
14412 14079 }
14413 14080 i++;
14414 14081 }
14415 14082 /* Remove the preallocated 1/4 8k ttecnt for 4M regions. */
14416 14083 if (r_pgszc >= TTE4M) {
14417 14084 rttecnt = r_size >> (TTE_PAGE_SHIFT(TTE8K) + 2);
14418 14085 ASSERT(sfmmup->sfmmu_tsb0_4minflcnt >=
14419 14086 rttecnt);
14420 14087 sfmmup->sfmmu_tsb0_4minflcnt -= rttecnt;
14421 14088 }
14422 14089
14423 14090 /* update shme rgns ttecnt in sfmmu_ttecnt */
14424 14091 rttecnt = r_size >> TTE_PAGE_SHIFT(r_pgszc);
14425 14092 ASSERT(sfmmup->sfmmu_ttecnt[r_pgszc] >= rttecnt);
14426 14093 atomic_add_long(&sfmmup->sfmmu_ttecnt[r_pgszc], -rttecnt);
14427 14094
14428 14095 sfmmu_hat_exit(hatlockp);
14429 14096 if (scdp != NULL && sfmmup->sfmmu_scdp == NULL) {
14430 14097 /* sfmmup left the scd, grow private tsb */
14431 14098 sfmmu_check_page_sizes(sfmmup, 1);
14432 14099 } else {
14433 14100 sfmmu_check_page_sizes(sfmmup, 0);
14434 14101 }
14435 14102 }
14436 14103
14437 14104 if (r_type == SFMMU_REGION_HME) {
14438 14105 sfmmu_unlink_from_hmeregion(sfmmup, rgnp);
14439 14106 }
14440 14107
14441 14108 r_obj = rgnp->rgn_obj;
14442 14109 if (atomic_dec_32_nv((volatile uint_t *)&rgnp->rgn_refcnt)) {
14443 14110 return;
14444 14111 }
14445 14112
14446 14113 /*
14447 14114 * looks like nobody uses this region anymore. Free it.
14448 14115 */
14449 14116 rhash = RGN_HASH_FUNCTION(r_obj);
14450 14117 mutex_enter(&srdp->srd_mutex);
14451 14118 for (prev_rgnpp = &srdp->srd_rgnhash[rhash];
14452 14119 (cur_rgnp = *prev_rgnpp) != NULL;
14453 14120 prev_rgnpp = &cur_rgnp->rgn_hash) {
14454 14121 if (cur_rgnp == rgnp && cur_rgnp->rgn_refcnt == 0) {
14455 14122 break;
14456 14123 }
14457 14124 }
14458 14125
14459 14126 if (cur_rgnp == NULL) {
14460 14127 mutex_exit(&srdp->srd_mutex);
14461 14128 return;
14462 14129 }
14463 14130
14464 14131 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == r_type);
14465 14132 *prev_rgnpp = rgnp->rgn_hash;
14466 14133 if (r_type == SFMMU_REGION_ISM) {
14467 14134 rgnp->rgn_flags |= SFMMU_REGION_FREE;
14468 14135 ASSERT(rid < srdp->srd_next_ismrid);
14469 14136 rgnp->rgn_next = srdp->srd_ismrgnfree;
14470 14137 srdp->srd_ismrgnfree = rgnp;
14471 14138 ASSERT(srdp->srd_ismbusyrgns > 0);
14472 14139 srdp->srd_ismbusyrgns--;
14473 14140 mutex_exit(&srdp->srd_mutex);
14474 14141 return;
14475 14142 }
14476 14143 mutex_exit(&srdp->srd_mutex);
14477 14144
14478 14145 /*
14479 14146 * Destroy region's hmeblks.
14480 14147 */
14481 14148 sfmmu_unload_hmeregion(srdp, rgnp);
14482 14149
14483 14150 rgnp->rgn_hmeflags = 0;
14484 14151
14485 14152 ASSERT(rgnp->rgn_sfmmu_head == NULL);
14486 14153 ASSERT(rgnp->rgn_id == rid);
14487 14154 for (i = 0; i < MMU_PAGE_SIZES; i++) {
14488 14155 rgnp->rgn_ttecnt[i] = 0;
14489 14156 }
14490 14157 rgnp->rgn_flags |= SFMMU_REGION_FREE;
14491 14158 mutex_enter(&srdp->srd_mutex);
14492 14159 ASSERT(rid < srdp->srd_next_hmerid);
14493 14160 rgnp->rgn_next = srdp->srd_hmergnfree;
14494 14161 srdp->srd_hmergnfree = rgnp;
14495 14162 ASSERT(srdp->srd_hmebusyrgns > 0);
14496 14163 srdp->srd_hmebusyrgns--;
14497 14164 mutex_exit(&srdp->srd_mutex);
14498 14165 }
14499 14166
14500 14167 /*
14501 14168 * For now only called for hmeblk regions and not for ISM regions.
14502 14169 */
14503 14170 void
14504 14171 hat_dup_region(struct hat *sfmmup, hat_region_cookie_t rcookie)
14505 14172 {
14506 14173 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
14507 14174 uint_t rid = (uint_t)((uint64_t)rcookie);
14508 14175 sf_region_t *rgnp;
14509 14176 sf_rgn_link_t *rlink;
14510 14177 sf_rgn_link_t *hrlink;
14511 14178 ulong_t rttecnt;
14512 14179
14513 14180 ASSERT(sfmmup != ksfmmup);
14514 14181 ASSERT(srdp != NULL);
14515 14182 ASSERT(srdp->srd_refcnt > 0);
14516 14183
14517 14184 ASSERT(rid < srdp->srd_next_hmerid);
14518 14185 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
14519 14186 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
14520 14187
14521 14188 rgnp = srdp->srd_hmergnp[rid];
14522 14189 ASSERT(rgnp->rgn_refcnt > 0);
14523 14190 ASSERT(rgnp->rgn_id == rid);
14524 14191 ASSERT((rgnp->rgn_flags & SFMMU_REGION_TYPE_MASK) == SFMMU_REGION_HME);
14525 14192 ASSERT(!(rgnp->rgn_flags & SFMMU_REGION_FREE));
14526 14193
14527 14194 atomic_inc_32((volatile uint_t *)&rgnp->rgn_refcnt);
14528 14195
14529 14196 /* LINTED: constant in conditional context */
14530 14197 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 1, 0);
14531 14198 ASSERT(rlink != NULL);
14532 14199 mutex_enter(&rgnp->rgn_mutex);
14533 14200 ASSERT(rgnp->rgn_sfmmu_head != NULL);
14534 14201 /* LINTED: constant in conditional context */
14535 14202 SFMMU_HMERID2RLINKP(rgnp->rgn_sfmmu_head, rid, hrlink, 0, 0);
14536 14203 ASSERT(hrlink != NULL);
14537 14204 ASSERT(hrlink->prev == NULL);
14538 14205 rlink->next = rgnp->rgn_sfmmu_head;
14539 14206 rlink->prev = NULL;
14540 14207 hrlink->prev = sfmmup;
14541 14208 /*
14542 14209 * make sure rlink's next field is correct
14543 14210 * before making this link visible.
14544 14211 */
14545 14212 membar_stst();
14546 14213 rgnp->rgn_sfmmu_head = sfmmup;
14547 14214 mutex_exit(&rgnp->rgn_mutex);
14548 14215
14549 14216 /* update sfmmu_ttecnt with the shme rgn ttecnt */
14550 14217 rttecnt = rgnp->rgn_size >> TTE_PAGE_SHIFT(rgnp->rgn_pgszc);
14551 14218 atomic_add_long(&sfmmup->sfmmu_ttecnt[rgnp->rgn_pgszc], rttecnt);
14552 14219 /* update tsb0 inflation count */
14553 14220 if (rgnp->rgn_pgszc >= TTE4M) {
14554 14221 sfmmup->sfmmu_tsb0_4minflcnt +=
14555 14222 rgnp->rgn_size >> (TTE_PAGE_SHIFT(TTE8K) + 2);
14556 14223 }
14557 14224 /*
14558 14225 * Update regionid bitmask without hat lock since no other thread
14559 14226 * can update this region bitmask right now.
14560 14227 */
14561 14228 SF_RGNMAP_ADD(sfmmup->sfmmu_hmeregion_map, rid);
14562 14229 }
14563 14230
14564 14231 /* ARGSUSED */
14565 14232 static int
14566 14233 sfmmu_rgncache_constructor(void *buf, void *cdrarg, int kmflags)
14567 14234 {
14568 14235 sf_region_t *rgnp = (sf_region_t *)buf;
14569 14236 bzero(buf, sizeof (*rgnp));
14570 14237
14571 14238 mutex_init(&rgnp->rgn_mutex, NULL, MUTEX_DEFAULT, NULL);
14572 14239
14573 14240 return (0);
14574 14241 }
14575 14242
14576 14243 /* ARGSUSED */
14577 14244 static void
14578 14245 sfmmu_rgncache_destructor(void *buf, void *cdrarg)
14579 14246 {
14580 14247 sf_region_t *rgnp = (sf_region_t *)buf;
14581 14248 mutex_destroy(&rgnp->rgn_mutex);
14582 14249 }
14583 14250
14584 14251 static int
14585 14252 sfrgnmap_isnull(sf_region_map_t *map)
14586 14253 {
14587 14254 int i;
14588 14255
14589 14256 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) {
14590 14257 if (map->bitmap[i] != 0) {
14591 14258 return (0);
14592 14259 }
14593 14260 }
14594 14261 return (1);
14595 14262 }
14596 14263
14597 14264 static int
14598 14265 sfhmergnmap_isnull(sf_hmeregion_map_t *map)
14599 14266 {
14600 14267 int i;
14601 14268
14602 14269 for (i = 0; i < SFMMU_HMERGNMAP_WORDS; i++) {
14603 14270 if (map->bitmap[i] != 0) {
14604 14271 return (0);
14605 14272 }
14606 14273 }
14607 14274 return (1);
14608 14275 }
14609 14276
14610 14277 #ifdef DEBUG
14611 14278 static void
14612 14279 check_scd_sfmmu_list(sfmmu_t **headp, sfmmu_t *sfmmup, int onlist)
14613 14280 {
14614 14281 sfmmu_t *sp;
14615 14282 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
14616 14283
14617 14284 for (sp = *headp; sp != NULL; sp = sp->sfmmu_scd_link.next) {
14618 14285 ASSERT(srdp == sp->sfmmu_srdp);
14619 14286 if (sp == sfmmup) {
14620 14287 if (onlist) {
14621 14288 return;
14622 14289 } else {
14623 14290 panic("shctx: sfmmu 0x%p found on scd"
14624 14291 "list 0x%p", (void *)sfmmup,
14625 14292 (void *)*headp);
14626 14293 }
14627 14294 }
14628 14295 }
14629 14296 if (onlist) {
14630 14297 panic("shctx: sfmmu 0x%p not found on scd list 0x%p",
14631 14298 (void *)sfmmup, (void *)*headp);
14632 14299 } else {
14633 14300 return;
14634 14301 }
14635 14302 }
14636 14303 #else /* DEBUG */
14637 14304 #define check_scd_sfmmu_list(headp, sfmmup, onlist)
14638 14305 #endif /* DEBUG */
14639 14306
14640 14307 /*
14641 14308 * Removes an sfmmu from the SCD sfmmu list.
14642 14309 */
14643 14310 static void
14644 14311 sfmmu_from_scd_list(sfmmu_t **headp, sfmmu_t *sfmmup)
14645 14312 {
14646 14313 ASSERT(sfmmup->sfmmu_srdp != NULL);
14647 14314 check_scd_sfmmu_list(headp, sfmmup, 1);
14648 14315 if (sfmmup->sfmmu_scd_link.prev != NULL) {
14649 14316 ASSERT(*headp != sfmmup);
14650 14317 sfmmup->sfmmu_scd_link.prev->sfmmu_scd_link.next =
14651 14318 sfmmup->sfmmu_scd_link.next;
14652 14319 } else {
14653 14320 ASSERT(*headp == sfmmup);
14654 14321 *headp = sfmmup->sfmmu_scd_link.next;
14655 14322 }
14656 14323 if (sfmmup->sfmmu_scd_link.next != NULL) {
14657 14324 sfmmup->sfmmu_scd_link.next->sfmmu_scd_link.prev =
14658 14325 sfmmup->sfmmu_scd_link.prev;
14659 14326 }
14660 14327 }
14661 14328
14662 14329
14663 14330 /*
14664 14331 * Adds an sfmmu to the start of the queue.
14665 14332 */
14666 14333 static void
14667 14334 sfmmu_to_scd_list(sfmmu_t **headp, sfmmu_t *sfmmup)
14668 14335 {
14669 14336 check_scd_sfmmu_list(headp, sfmmup, 0);
14670 14337 sfmmup->sfmmu_scd_link.prev = NULL;
14671 14338 sfmmup->sfmmu_scd_link.next = *headp;
14672 14339 if (*headp != NULL)
14673 14340 (*headp)->sfmmu_scd_link.prev = sfmmup;
14674 14341 *headp = sfmmup;
14675 14342 }
14676 14343
14677 14344 /*
14678 14345 * Remove an scd from the start of the queue.
14679 14346 */
14680 14347 static void
14681 14348 sfmmu_remove_scd(sf_scd_t **headp, sf_scd_t *scdp)
14682 14349 {
14683 14350 if (scdp->scd_prev != NULL) {
14684 14351 ASSERT(*headp != scdp);
14685 14352 scdp->scd_prev->scd_next = scdp->scd_next;
14686 14353 } else {
14687 14354 ASSERT(*headp == scdp);
14688 14355 *headp = scdp->scd_next;
14689 14356 }
14690 14357
14691 14358 if (scdp->scd_next != NULL) {
14692 14359 scdp->scd_next->scd_prev = scdp->scd_prev;
14693 14360 }
14694 14361 }
14695 14362
14696 14363 /*
14697 14364 * Add an scd to the start of the queue.
14698 14365 */
14699 14366 static void
14700 14367 sfmmu_add_scd(sf_scd_t **headp, sf_scd_t *scdp)
14701 14368 {
14702 14369 scdp->scd_prev = NULL;
14703 14370 scdp->scd_next = *headp;
14704 14371 if (*headp != NULL) {
14705 14372 (*headp)->scd_prev = scdp;
14706 14373 }
14707 14374 *headp = scdp;
14708 14375 }
14709 14376
14710 14377 static int
14711 14378 sfmmu_alloc_scd_tsbs(sf_srd_t *srdp, sf_scd_t *scdp)
14712 14379 {
14713 14380 uint_t rid;
14714 14381 uint_t i;
14715 14382 uint_t j;
14716 14383 ulong_t w;
14717 14384 sf_region_t *rgnp;
14718 14385 ulong_t tte8k_cnt = 0;
14719 14386 ulong_t tte4m_cnt = 0;
14720 14387 uint_t tsb_szc;
14721 14388 sfmmu_t *scsfmmup = scdp->scd_sfmmup;
14722 14389 sfmmu_t *ism_hatid;
14723 14390 struct tsb_info *newtsb;
14724 14391 int szc;
14725 14392
14726 14393 ASSERT(srdp != NULL);
14727 14394
14728 14395 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) {
14729 14396 if ((w = scdp->scd_region_map.bitmap[i]) == 0) {
14730 14397 continue;
14731 14398 }
14732 14399 j = 0;
14733 14400 while (w) {
14734 14401 if (!(w & 0x1)) {
14735 14402 j++;
14736 14403 w >>= 1;
14737 14404 continue;
14738 14405 }
14739 14406 rid = (i << BT_ULSHIFT) | j;
14740 14407 j++;
14741 14408 w >>= 1;
14742 14409
14743 14410 if (rid < SFMMU_MAX_HME_REGIONS) {
14744 14411 rgnp = srdp->srd_hmergnp[rid];
14745 14412 ASSERT(rgnp->rgn_id == rid);
14746 14413 ASSERT(rgnp->rgn_refcnt > 0);
14747 14414
14748 14415 if (rgnp->rgn_pgszc < TTE4M) {
14749 14416 tte8k_cnt += rgnp->rgn_size >>
14750 14417 TTE_PAGE_SHIFT(TTE8K);
14751 14418 } else {
14752 14419 ASSERT(rgnp->rgn_pgszc >= TTE4M);
14753 14420 tte4m_cnt += rgnp->rgn_size >>
14754 14421 TTE_PAGE_SHIFT(TTE4M);
14755 14422 /*
14756 14423 * Inflate SCD tsb0 by preallocating
14757 14424 * 1/4 8k ttecnt for 4M regions to
14758 14425 * allow for lgpg alloc failure.
14759 14426 */
14760 14427 tte8k_cnt += rgnp->rgn_size >>
14761 14428 (TTE_PAGE_SHIFT(TTE8K) + 2);
14762 14429 }
14763 14430 } else {
14764 14431 rid -= SFMMU_MAX_HME_REGIONS;
14765 14432 rgnp = srdp->srd_ismrgnp[rid];
14766 14433 ASSERT(rgnp->rgn_id == rid);
14767 14434 ASSERT(rgnp->rgn_refcnt > 0);
14768 14435
14769 14436 ism_hatid = (sfmmu_t *)rgnp->rgn_obj;
14770 14437 ASSERT(ism_hatid->sfmmu_ismhat);
14771 14438
14772 14439 for (szc = 0; szc < TTE4M; szc++) {
14773 14440 tte8k_cnt +=
14774 14441 ism_hatid->sfmmu_ttecnt[szc] <<
14775 14442 TTE_BSZS_SHIFT(szc);
14776 14443 }
14777 14444
14778 14445 ASSERT(rgnp->rgn_pgszc >= TTE4M);
14779 14446 if (rgnp->rgn_pgszc >= TTE4M) {
14780 14447 tte4m_cnt += rgnp->rgn_size >>
14781 14448 TTE_PAGE_SHIFT(TTE4M);
14782 14449 }
14783 14450 }
14784 14451 }
14785 14452 }
14786 14453
14787 14454 tsb_szc = SELECT_TSB_SIZECODE(tte8k_cnt);
14788 14455
14789 14456 /* Allocate both the SCD TSBs here. */
14790 14457 if (sfmmu_tsbinfo_alloc(&scsfmmup->sfmmu_tsb,
14791 14458 tsb_szc, TSB8K|TSB64K|TSB512K, TSB_ALLOC, scsfmmup) &&
14792 14459 (tsb_szc <= TSB_4M_SZCODE ||
14793 14460 sfmmu_tsbinfo_alloc(&scsfmmup->sfmmu_tsb,
14794 14461 TSB_4M_SZCODE, TSB8K|TSB64K|TSB512K,
14795 14462 TSB_ALLOC, scsfmmup))) {
14796 14463
14797 14464 SFMMU_STAT(sf_scd_1sttsb_allocfail);
14798 14465 return (TSB_ALLOCFAIL);
14799 14466 } else {
14800 14467 scsfmmup->sfmmu_tsb->tsb_flags |= TSB_SHAREDCTX;
14801 14468
14802 14469 if (tte4m_cnt) {
14803 14470 tsb_szc = SELECT_TSB_SIZECODE(tte4m_cnt);
14804 14471 if (sfmmu_tsbinfo_alloc(&newtsb, tsb_szc,
14805 14472 TSB4M|TSB32M|TSB256M, TSB_ALLOC, scsfmmup) &&
14806 14473 (tsb_szc <= TSB_4M_SZCODE ||
14807 14474 sfmmu_tsbinfo_alloc(&newtsb, TSB_4M_SZCODE,
14808 14475 TSB4M|TSB32M|TSB256M,
14809 14476 TSB_ALLOC, scsfmmup))) {
14810 14477 /*
14811 14478 * If we fail to allocate the 2nd shared tsb,
14812 14479 * just free the 1st tsb, return failure.
14813 14480 */
14814 14481 sfmmu_tsbinfo_free(scsfmmup->sfmmu_tsb);
14815 14482 SFMMU_STAT(sf_scd_2ndtsb_allocfail);
14816 14483 return (TSB_ALLOCFAIL);
14817 14484 } else {
14818 14485 ASSERT(scsfmmup->sfmmu_tsb->tsb_next == NULL);
14819 14486 newtsb->tsb_flags |= TSB_SHAREDCTX;
14820 14487 scsfmmup->sfmmu_tsb->tsb_next = newtsb;
14821 14488 SFMMU_STAT(sf_scd_2ndtsb_alloc);
14822 14489 }
14823 14490 }
14824 14491 SFMMU_STAT(sf_scd_1sttsb_alloc);
14825 14492 }
14826 14493 return (TSB_SUCCESS);
14827 14494 }
14828 14495
14829 14496 static void
14830 14497 sfmmu_free_scd_tsbs(sfmmu_t *scd_sfmmu)
14831 14498 {
14832 14499 while (scd_sfmmu->sfmmu_tsb != NULL) {
14833 14500 struct tsb_info *next = scd_sfmmu->sfmmu_tsb->tsb_next;
14834 14501 sfmmu_tsbinfo_free(scd_sfmmu->sfmmu_tsb);
14835 14502 scd_sfmmu->sfmmu_tsb = next;
14836 14503 }
14837 14504 }
14838 14505
14839 14506 /*
14840 14507 * Link the sfmmu onto the hme region list.
14841 14508 */
14842 14509 void
14843 14510 sfmmu_link_to_hmeregion(sfmmu_t *sfmmup, sf_region_t *rgnp)
14844 14511 {
14845 14512 uint_t rid;
14846 14513 sf_rgn_link_t *rlink;
14847 14514 sfmmu_t *head;
14848 14515 sf_rgn_link_t *hrlink;
14849 14516
14850 14517 rid = rgnp->rgn_id;
14851 14518 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
14852 14519
14853 14520 /* LINTED: constant in conditional context */
14854 14521 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 1, 1);
14855 14522 ASSERT(rlink != NULL);
14856 14523 mutex_enter(&rgnp->rgn_mutex);
14857 14524 if ((head = rgnp->rgn_sfmmu_head) == NULL) {
14858 14525 rlink->next = NULL;
14859 14526 rlink->prev = NULL;
14860 14527 /*
14861 14528 * make sure rlink's next field is NULL
14862 14529 * before making this link visible.
14863 14530 */
14864 14531 membar_stst();
14865 14532 rgnp->rgn_sfmmu_head = sfmmup;
14866 14533 } else {
14867 14534 /* LINTED: constant in conditional context */
14868 14535 SFMMU_HMERID2RLINKP(head, rid, hrlink, 0, 0);
14869 14536 ASSERT(hrlink != NULL);
14870 14537 ASSERT(hrlink->prev == NULL);
14871 14538 rlink->next = head;
14872 14539 rlink->prev = NULL;
14873 14540 hrlink->prev = sfmmup;
14874 14541 /*
14875 14542 * make sure rlink's next field is correct
14876 14543 * before making this link visible.
14877 14544 */
14878 14545 membar_stst();
14879 14546 rgnp->rgn_sfmmu_head = sfmmup;
14880 14547 }
14881 14548 mutex_exit(&rgnp->rgn_mutex);
14882 14549 }
14883 14550
14884 14551 /*
14885 14552 * Unlink the sfmmu from the hme region list.
14886 14553 */
14887 14554 void
14888 14555 sfmmu_unlink_from_hmeregion(sfmmu_t *sfmmup, sf_region_t *rgnp)
14889 14556 {
14890 14557 uint_t rid;
14891 14558 sf_rgn_link_t *rlink;
14892 14559
14893 14560 rid = rgnp->rgn_id;
14894 14561 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
14895 14562
14896 14563 /* LINTED: constant in conditional context */
14897 14564 SFMMU_HMERID2RLINKP(sfmmup, rid, rlink, 0, 0);
14898 14565 ASSERT(rlink != NULL);
14899 14566 mutex_enter(&rgnp->rgn_mutex);
14900 14567 if (rgnp->rgn_sfmmu_head == sfmmup) {
14901 14568 sfmmu_t *next = rlink->next;
14902 14569 rgnp->rgn_sfmmu_head = next;
14903 14570 /*
14904 14571 * if we are stopped by xc_attention() after this
14905 14572 * point the forward link walking in
14906 14573 * sfmmu_rgntlb_demap() will work correctly since the
14907 14574 * head correctly points to the next element.
14908 14575 */
14909 14576 membar_stst();
14910 14577 rlink->next = NULL;
14911 14578 ASSERT(rlink->prev == NULL);
14912 14579 if (next != NULL) {
14913 14580 sf_rgn_link_t *nrlink;
14914 14581 /* LINTED: constant in conditional context */
14915 14582 SFMMU_HMERID2RLINKP(next, rid, nrlink, 0, 0);
14916 14583 ASSERT(nrlink != NULL);
14917 14584 ASSERT(nrlink->prev == sfmmup);
14918 14585 nrlink->prev = NULL;
14919 14586 }
14920 14587 } else {
14921 14588 sfmmu_t *next = rlink->next;
14922 14589 sfmmu_t *prev = rlink->prev;
14923 14590 sf_rgn_link_t *prlink;
14924 14591
14925 14592 ASSERT(prev != NULL);
14926 14593 /* LINTED: constant in conditional context */
14927 14594 SFMMU_HMERID2RLINKP(prev, rid, prlink, 0, 0);
14928 14595 ASSERT(prlink != NULL);
14929 14596 ASSERT(prlink->next == sfmmup);
14930 14597 prlink->next = next;
14931 14598 /*
14932 14599 * if we are stopped by xc_attention()
14933 14600 * after this point the forward link walking
14934 14601 * will work correctly since the prev element
14935 14602 * correctly points to the next element.
14936 14603 */
14937 14604 membar_stst();
14938 14605 rlink->next = NULL;
14939 14606 rlink->prev = NULL;
14940 14607 if (next != NULL) {
14941 14608 sf_rgn_link_t *nrlink;
14942 14609 /* LINTED: constant in conditional context */
14943 14610 SFMMU_HMERID2RLINKP(next, rid, nrlink, 0, 0);
14944 14611 ASSERT(nrlink != NULL);
14945 14612 ASSERT(nrlink->prev == sfmmup);
14946 14613 nrlink->prev = prev;
14947 14614 }
14948 14615 }
14949 14616 mutex_exit(&rgnp->rgn_mutex);
14950 14617 }
14951 14618
14952 14619 /*
14953 14620 * Link scd sfmmu onto ism or hme region list for each region in the
14954 14621 * scd region map.
14955 14622 */
14956 14623 void
14957 14624 sfmmu_link_scd_to_regions(sf_srd_t *srdp, sf_scd_t *scdp)
14958 14625 {
14959 14626 uint_t rid;
14960 14627 uint_t i;
14961 14628 uint_t j;
14962 14629 ulong_t w;
14963 14630 sf_region_t *rgnp;
14964 14631 sfmmu_t *scsfmmup;
14965 14632
14966 14633 scsfmmup = scdp->scd_sfmmup;
14967 14634 ASSERT(scsfmmup->sfmmu_scdhat);
14968 14635 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) {
14969 14636 if ((w = scdp->scd_region_map.bitmap[i]) == 0) {
14970 14637 continue;
14971 14638 }
14972 14639 j = 0;
14973 14640 while (w) {
14974 14641 if (!(w & 0x1)) {
14975 14642 j++;
14976 14643 w >>= 1;
14977 14644 continue;
14978 14645 }
14979 14646 rid = (i << BT_ULSHIFT) | j;
14980 14647 j++;
14981 14648 w >>= 1;
14982 14649
14983 14650 if (rid < SFMMU_MAX_HME_REGIONS) {
14984 14651 rgnp = srdp->srd_hmergnp[rid];
14985 14652 ASSERT(rgnp->rgn_id == rid);
14986 14653 ASSERT(rgnp->rgn_refcnt > 0);
14987 14654 sfmmu_link_to_hmeregion(scsfmmup, rgnp);
14988 14655 } else {
14989 14656 sfmmu_t *ism_hatid = NULL;
14990 14657 ism_ment_t *ism_ment;
14991 14658 rid -= SFMMU_MAX_HME_REGIONS;
14992 14659 rgnp = srdp->srd_ismrgnp[rid];
14993 14660 ASSERT(rgnp->rgn_id == rid);
14994 14661 ASSERT(rgnp->rgn_refcnt > 0);
14995 14662
14996 14663 ism_hatid = (sfmmu_t *)rgnp->rgn_obj;
14997 14664 ASSERT(ism_hatid->sfmmu_ismhat);
14998 14665 ism_ment = &scdp->scd_ism_links[rid];
14999 14666 ism_ment->iment_hat = scsfmmup;
15000 14667 ism_ment->iment_base_va = rgnp->rgn_saddr;
15001 14668 mutex_enter(&ism_mlist_lock);
15002 14669 iment_add(ism_ment, ism_hatid);
15003 14670 mutex_exit(&ism_mlist_lock);
15004 14671
15005 14672 }
15006 14673 }
15007 14674 }
15008 14675 }
15009 14676 /*
15010 14677 * Unlink scd sfmmu from ism or hme region list for each region in the
15011 14678 * scd region map.
15012 14679 */
15013 14680 void
15014 14681 sfmmu_unlink_scd_from_regions(sf_srd_t *srdp, sf_scd_t *scdp)
15015 14682 {
15016 14683 uint_t rid;
15017 14684 uint_t i;
15018 14685 uint_t j;
15019 14686 ulong_t w;
15020 14687 sf_region_t *rgnp;
15021 14688 sfmmu_t *scsfmmup;
15022 14689
15023 14690 scsfmmup = scdp->scd_sfmmup;
15024 14691 for (i = 0; i < SFMMU_RGNMAP_WORDS; i++) {
15025 14692 if ((w = scdp->scd_region_map.bitmap[i]) == 0) {
15026 14693 continue;
15027 14694 }
15028 14695 j = 0;
15029 14696 while (w) {
15030 14697 if (!(w & 0x1)) {
15031 14698 j++;
15032 14699 w >>= 1;
15033 14700 continue;
15034 14701 }
15035 14702 rid = (i << BT_ULSHIFT) | j;
15036 14703 j++;
15037 14704 w >>= 1;
15038 14705
15039 14706 if (rid < SFMMU_MAX_HME_REGIONS) {
15040 14707 rgnp = srdp->srd_hmergnp[rid];
15041 14708 ASSERT(rgnp->rgn_id == rid);
15042 14709 ASSERT(rgnp->rgn_refcnt > 0);
15043 14710 sfmmu_unlink_from_hmeregion(scsfmmup,
15044 14711 rgnp);
15045 14712
15046 14713 } else {
15047 14714 sfmmu_t *ism_hatid = NULL;
15048 14715 ism_ment_t *ism_ment;
15049 14716 rid -= SFMMU_MAX_HME_REGIONS;
15050 14717 rgnp = srdp->srd_ismrgnp[rid];
15051 14718 ASSERT(rgnp->rgn_id == rid);
15052 14719 ASSERT(rgnp->rgn_refcnt > 0);
15053 14720
15054 14721 ism_hatid = (sfmmu_t *)rgnp->rgn_obj;
15055 14722 ASSERT(ism_hatid->sfmmu_ismhat);
15056 14723 ism_ment = &scdp->scd_ism_links[rid];
15057 14724 ASSERT(ism_ment->iment_hat == scdp->scd_sfmmup);
15058 14725 ASSERT(ism_ment->iment_base_va ==
15059 14726 rgnp->rgn_saddr);
15060 14727 mutex_enter(&ism_mlist_lock);
15061 14728 iment_sub(ism_ment, ism_hatid);
15062 14729 mutex_exit(&ism_mlist_lock);
15063 14730
15064 14731 }
15065 14732 }
15066 14733 }
15067 14734 }
15068 14735 /*
15069 14736 * Allocates and initialises a new SCD structure, this is called with
15070 14737 * the srd_scd_mutex held and returns with the reference count
15071 14738 * initialised to 1.
15072 14739 */
15073 14740 static sf_scd_t *
15074 14741 sfmmu_alloc_scd(sf_srd_t *srdp, sf_region_map_t *new_map)
15075 14742 {
15076 14743 sf_scd_t *new_scdp;
15077 14744 sfmmu_t *scsfmmup;
15078 14745 int i;
15079 14746
15080 14747 ASSERT(MUTEX_HELD(&srdp->srd_scd_mutex));
15081 14748 new_scdp = kmem_cache_alloc(scd_cache, KM_SLEEP);
15082 14749
15083 14750 scsfmmup = kmem_cache_alloc(sfmmuid_cache, KM_SLEEP);
15084 14751 new_scdp->scd_sfmmup = scsfmmup;
15085 14752 scsfmmup->sfmmu_srdp = srdp;
15086 14753 scsfmmup->sfmmu_scdp = new_scdp;
15087 14754 scsfmmup->sfmmu_tsb0_4minflcnt = 0;
15088 14755 scsfmmup->sfmmu_scdhat = 1;
15089 14756 CPUSET_ALL(scsfmmup->sfmmu_cpusran);
15090 14757 bzero(scsfmmup->sfmmu_hmeregion_links, SFMMU_L1_HMERLINKS_SIZE);
15091 14758
15092 14759 ASSERT(max_mmu_ctxdoms > 0);
15093 14760 for (i = 0; i < max_mmu_ctxdoms; i++) {
15094 14761 scsfmmup->sfmmu_ctxs[i].cnum = INVALID_CONTEXT;
15095 14762 scsfmmup->sfmmu_ctxs[i].gnum = 0;
15096 14763 }
15097 14764
15098 14765 for (i = 0; i < MMU_PAGE_SIZES; i++) {
15099 14766 new_scdp->scd_rttecnt[i] = 0;
15100 14767 }
15101 14768
15102 14769 new_scdp->scd_region_map = *new_map;
15103 14770 new_scdp->scd_refcnt = 1;
15104 14771 if (sfmmu_alloc_scd_tsbs(srdp, new_scdp) != TSB_SUCCESS) {
15105 14772 kmem_cache_free(scd_cache, new_scdp);
15106 14773 kmem_cache_free(sfmmuid_cache, scsfmmup);
15107 14774 return (NULL);
15108 14775 }
15109 14776 if (&mmu_init_scd) {
15110 14777 mmu_init_scd(new_scdp);
15111 14778 }
15112 14779 return (new_scdp);
15113 14780 }
15114 14781
15115 14782 /*
15116 14783 * The first phase of a process joining an SCD. The hat structure is
15117 14784 * linked to the SCD queue and then the HAT_JOIN_SCD sfmmu flag is set
15118 14785 * and a cross-call with context invalidation is used to cause the
15119 14786 * remaining work to be carried out in the sfmmu_tsbmiss_exception()
15120 14787 * routine.
15121 14788 */
15122 14789 static void
15123 14790 sfmmu_join_scd(sf_scd_t *scdp, sfmmu_t *sfmmup)
15124 14791 {
15125 14792 hatlock_t *hatlockp;
15126 14793 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
15127 14794 int i;
15128 14795 sf_scd_t *old_scdp;
15129 14796
15130 14797 ASSERT(srdp != NULL);
15131 14798 ASSERT(scdp != NULL);
15132 14799 ASSERT(scdp->scd_refcnt > 0);
15133 14800 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
15134 14801
15135 14802 if ((old_scdp = sfmmup->sfmmu_scdp) != NULL) {
15136 14803 ASSERT(old_scdp != scdp);
15137 14804
15138 14805 mutex_enter(&old_scdp->scd_mutex);
15139 14806 sfmmu_from_scd_list(&old_scdp->scd_sf_list, sfmmup);
15140 14807 mutex_exit(&old_scdp->scd_mutex);
15141 14808 /*
15142 14809 * sfmmup leaves the old scd. Update sfmmu_ttecnt to
15143 14810 * include the shme rgn ttecnt for rgns that
15144 14811 * were in the old SCD
15145 14812 */
15146 14813 for (i = 0; i < mmu_page_sizes; i++) {
15147 14814 ASSERT(sfmmup->sfmmu_scdrttecnt[i] ==
15148 14815 old_scdp->scd_rttecnt[i]);
15149 14816 atomic_add_long(&sfmmup->sfmmu_ttecnt[i],
15150 14817 sfmmup->sfmmu_scdrttecnt[i]);
15151 14818 }
15152 14819 }
15153 14820
15154 14821 /*
15155 14822 * Move sfmmu to the scd lists.
15156 14823 */
15157 14824 mutex_enter(&scdp->scd_mutex);
15158 14825 sfmmu_to_scd_list(&scdp->scd_sf_list, sfmmup);
15159 14826 mutex_exit(&scdp->scd_mutex);
15160 14827 SF_SCD_INCR_REF(scdp);
15161 14828
15162 14829 hatlockp = sfmmu_hat_enter(sfmmup);
15163 14830 /*
15164 14831 * For a multi-thread process, we must stop
15165 14832 * all the other threads before joining the scd.
15166 14833 */
15167 14834
15168 14835 SFMMU_FLAGS_SET(sfmmup, HAT_JOIN_SCD);
15169 14836
15170 14837 sfmmu_invalidate_ctx(sfmmup);
15171 14838 sfmmup->sfmmu_scdp = scdp;
15172 14839
15173 14840 /*
15174 14841 * Copy scd_rttecnt into sfmmup's sfmmu_scdrttecnt, and update
15175 14842 * sfmmu_ttecnt to not include the rgn ttecnt just joined in SCD.
15176 14843 */
15177 14844 for (i = 0; i < mmu_page_sizes; i++) {
15178 14845 sfmmup->sfmmu_scdrttecnt[i] = scdp->scd_rttecnt[i];
15179 14846 ASSERT(sfmmup->sfmmu_ttecnt[i] >= scdp->scd_rttecnt[i]);
15180 14847 atomic_add_long(&sfmmup->sfmmu_ttecnt[i],
15181 14848 -sfmmup->sfmmu_scdrttecnt[i]);
15182 14849 }
15183 14850 /* update tsb0 inflation count */
15184 14851 if (old_scdp != NULL) {
15185 14852 sfmmup->sfmmu_tsb0_4minflcnt +=
15186 14853 old_scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt;
15187 14854 }
15188 14855 ASSERT(sfmmup->sfmmu_tsb0_4minflcnt >=
15189 14856 scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt);
15190 14857 sfmmup->sfmmu_tsb0_4minflcnt -= scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt;
15191 14858
15192 14859 sfmmu_hat_exit(hatlockp);
15193 14860
15194 14861 if (old_scdp != NULL) {
15195 14862 SF_SCD_DECR_REF(srdp, old_scdp);
15196 14863 }
15197 14864
15198 14865 }
15199 14866
15200 14867 /*
15201 14868 * This routine is called by a process to become part of an SCD. It is called
15202 14869 * from sfmmu_tsbmiss_exception() once most of the initial work has been
15203 14870 * done by sfmmu_join_scd(). This routine must not drop the hat lock.
15204 14871 */
15205 14872 static void
15206 14873 sfmmu_finish_join_scd(sfmmu_t *sfmmup)
15207 14874 {
15208 14875 struct tsb_info *tsbinfop;
15209 14876
15210 14877 ASSERT(sfmmu_hat_lock_held(sfmmup));
15211 14878 ASSERT(sfmmup->sfmmu_scdp != NULL);
15212 14879 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD));
15213 14880 ASSERT(!SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY));
15214 14881 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ALLCTX_INVALID));
15215 14882
15216 14883 for (tsbinfop = sfmmup->sfmmu_tsb; tsbinfop != NULL;
15217 14884 tsbinfop = tsbinfop->tsb_next) {
15218 14885 if (tsbinfop->tsb_flags & TSB_SWAPPED) {
15219 14886 continue;
15220 14887 }
15221 14888 ASSERT(!(tsbinfop->tsb_flags & TSB_RELOC_FLAG));
15222 14889
15223 14890 sfmmu_inv_tsb(tsbinfop->tsb_va,
15224 14891 TSB_BYTES(tsbinfop->tsb_szc));
15225 14892 }
15226 14893
15227 14894 /* Set HAT_CTX1_FLAG for all SCD ISMs */
15228 14895 sfmmu_ism_hatflags(sfmmup, 1);
15229 14896
15230 14897 SFMMU_STAT(sf_join_scd);
15231 14898 }
15232 14899
15233 14900 /*
15234 14901 * This routine is called in order to check if there is an SCD which matches
15235 14902 * the process's region map if not then a new SCD may be created.
15236 14903 */
15237 14904 static void
15238 14905 sfmmu_find_scd(sfmmu_t *sfmmup)
15239 14906 {
15240 14907 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
15241 14908 sf_scd_t *scdp, *new_scdp;
15242 14909 int ret;
15243 14910
15244 14911 ASSERT(srdp != NULL);
15245 14912 ASSERT(AS_WRITE_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
15246 14913
15247 14914 mutex_enter(&srdp->srd_scd_mutex);
15248 14915 for (scdp = srdp->srd_scdp; scdp != NULL;
15249 14916 scdp = scdp->scd_next) {
15250 14917 SF_RGNMAP_EQUAL(&scdp->scd_region_map,
15251 14918 &sfmmup->sfmmu_region_map, ret);
15252 14919 if (ret == 1) {
15253 14920 SF_SCD_INCR_REF(scdp);
15254 14921 mutex_exit(&srdp->srd_scd_mutex);
15255 14922 sfmmu_join_scd(scdp, sfmmup);
15256 14923 ASSERT(scdp->scd_refcnt >= 2);
15257 14924 atomic_dec_32((volatile uint32_t *)&scdp->scd_refcnt);
15258 14925 return;
15259 14926 } else {
15260 14927 /*
15261 14928 * If the sfmmu region map is a subset of the scd
15262 14929 * region map, then the assumption is that this process
15263 14930 * will continue attaching to ISM segments until the
15264 14931 * region maps are equal.
15265 14932 */
15266 14933 SF_RGNMAP_IS_SUBSET(&scdp->scd_region_map,
15267 14934 &sfmmup->sfmmu_region_map, ret);
15268 14935 if (ret == 1) {
15269 14936 mutex_exit(&srdp->srd_scd_mutex);
15270 14937 return;
15271 14938 }
15272 14939 }
15273 14940 }
15274 14941
15275 14942 ASSERT(scdp == NULL);
15276 14943 /*
15277 14944 * No matching SCD has been found, create a new one.
15278 14945 */
15279 14946 if ((new_scdp = sfmmu_alloc_scd(srdp, &sfmmup->sfmmu_region_map)) ==
15280 14947 NULL) {
15281 14948 mutex_exit(&srdp->srd_scd_mutex);
15282 14949 return;
15283 14950 }
15284 14951
15285 14952 /*
15286 14953 * sfmmu_alloc_scd() returns with a ref count of 1 on the scd.
15287 14954 */
15288 14955
15289 14956 /* Set scd_rttecnt for shme rgns in SCD */
15290 14957 sfmmu_set_scd_rttecnt(srdp, new_scdp);
15291 14958
15292 14959 /*
15293 14960 * Link scd onto srd_scdp list and scd sfmmu onto region/iment lists.
15294 14961 */
15295 14962 sfmmu_link_scd_to_regions(srdp, new_scdp);
15296 14963 sfmmu_add_scd(&srdp->srd_scdp, new_scdp);
15297 14964 SFMMU_STAT_ADD(sf_create_scd, 1);
15298 14965
15299 14966 mutex_exit(&srdp->srd_scd_mutex);
15300 14967 sfmmu_join_scd(new_scdp, sfmmup);
15301 14968 ASSERT(new_scdp->scd_refcnt >= 2);
15302 14969 atomic_dec_32((volatile uint32_t *)&new_scdp->scd_refcnt);
15303 14970 }
15304 14971
15305 14972 /*
15306 14973 * This routine is called by a process to remove itself from an SCD. It is
15307 14974 * either called when the processes has detached from a segment or from
15308 14975 * hat_free_start() as a result of calling exit.
15309 14976 */
15310 14977 static void
15311 14978 sfmmu_leave_scd(sfmmu_t *sfmmup, uchar_t r_type)
15312 14979 {
15313 14980 sf_scd_t *scdp = sfmmup->sfmmu_scdp;
15314 14981 sf_srd_t *srdp = sfmmup->sfmmu_srdp;
15315 14982 hatlock_t *hatlockp = TSB_HASH(sfmmup);
15316 14983 int i;
15317 14984
15318 14985 ASSERT(scdp != NULL);
15319 14986 ASSERT(srdp != NULL);
15320 14987
15321 14988 if (sfmmup->sfmmu_free) {
15322 14989 /*
15323 14990 * If the process is part of an SCD the sfmmu is unlinked
15324 14991 * from scd_sf_list.
15325 14992 */
15326 14993 mutex_enter(&scdp->scd_mutex);
15327 14994 sfmmu_from_scd_list(&scdp->scd_sf_list, sfmmup);
15328 14995 mutex_exit(&scdp->scd_mutex);
15329 14996 /*
15330 14997 * Update sfmmu_ttecnt to include the rgn ttecnt for rgns that
15331 14998 * are about to leave the SCD
15332 14999 */
15333 15000 for (i = 0; i < mmu_page_sizes; i++) {
15334 15001 ASSERT(sfmmup->sfmmu_scdrttecnt[i] ==
15335 15002 scdp->scd_rttecnt[i]);
15336 15003 atomic_add_long(&sfmmup->sfmmu_ttecnt[i],
15337 15004 sfmmup->sfmmu_scdrttecnt[i]);
15338 15005 sfmmup->sfmmu_scdrttecnt[i] = 0;
15339 15006 }
15340 15007 sfmmup->sfmmu_scdp = NULL;
15341 15008
15342 15009 SF_SCD_DECR_REF(srdp, scdp);
15343 15010 return;
15344 15011 }
15345 15012
15346 15013 ASSERT(r_type != SFMMU_REGION_ISM ||
15347 15014 SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY));
15348 15015 ASSERT(scdp->scd_refcnt);
15349 15016 ASSERT(!sfmmup->sfmmu_free);
15350 15017 ASSERT(sfmmu_hat_lock_held(sfmmup));
15351 15018 ASSERT(AS_LOCK_HELD(sfmmup->sfmmu_as, &sfmmup->sfmmu_as->a_lock));
15352 15019
15353 15020 /*
15354 15021 * Wait for ISM maps to be updated.
15355 15022 */
15356 15023 if (r_type != SFMMU_REGION_ISM) {
15357 15024 while (SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY) &&
15358 15025 sfmmup->sfmmu_scdp != NULL) {
15359 15026 cv_wait(&sfmmup->sfmmu_tsb_cv,
15360 15027 HATLOCK_MUTEXP(hatlockp));
15361 15028 }
15362 15029
15363 15030 if (sfmmup->sfmmu_scdp == NULL) {
15364 15031 sfmmu_hat_exit(hatlockp);
15365 15032 return;
15366 15033 }
15367 15034 SFMMU_FLAGS_SET(sfmmup, HAT_ISMBUSY);
15368 15035 }
15369 15036
15370 15037 if (SFMMU_FLAGS_ISSET(sfmmup, HAT_JOIN_SCD)) {
15371 15038 SFMMU_FLAGS_CLEAR(sfmmup, HAT_JOIN_SCD);
15372 15039 /*
15373 15040 * Since HAT_JOIN_SCD was set our context
15374 15041 * is still invalid.
15375 15042 */
15376 15043 } else {
15377 15044 /*
15378 15045 * For a multi-thread process, we must stop
15379 15046 * all the other threads before leaving the scd.
15380 15047 */
15381 15048
15382 15049 sfmmu_invalidate_ctx(sfmmup);
15383 15050 }
15384 15051
15385 15052 /* Clear all the rid's for ISM, delete flags, etc */
15386 15053 ASSERT(SFMMU_FLAGS_ISSET(sfmmup, HAT_ISMBUSY));
15387 15054 sfmmu_ism_hatflags(sfmmup, 0);
15388 15055
15389 15056 /*
15390 15057 * Update sfmmu_ttecnt to include the rgn ttecnt for rgns that
15391 15058 * are in SCD before this sfmmup leaves the SCD.
15392 15059 */
15393 15060 for (i = 0; i < mmu_page_sizes; i++) {
15394 15061 ASSERT(sfmmup->sfmmu_scdrttecnt[i] ==
15395 15062 scdp->scd_rttecnt[i]);
15396 15063 atomic_add_long(&sfmmup->sfmmu_ttecnt[i],
15397 15064 sfmmup->sfmmu_scdrttecnt[i]);
15398 15065 sfmmup->sfmmu_scdrttecnt[i] = 0;
15399 15066 /* update ismttecnt to include SCD ism before hat leaves SCD */
15400 15067 sfmmup->sfmmu_ismttecnt[i] += sfmmup->sfmmu_scdismttecnt[i];
15401 15068 sfmmup->sfmmu_scdismttecnt[i] = 0;
15402 15069 }
15403 15070 /* update tsb0 inflation count */
15404 15071 sfmmup->sfmmu_tsb0_4minflcnt += scdp->scd_sfmmup->sfmmu_tsb0_4minflcnt;
15405 15072
15406 15073 if (r_type != SFMMU_REGION_ISM) {
15407 15074 SFMMU_FLAGS_CLEAR(sfmmup, HAT_ISMBUSY);
15408 15075 }
15409 15076 sfmmup->sfmmu_scdp = NULL;
15410 15077
15411 15078 sfmmu_hat_exit(hatlockp);
15412 15079
15413 15080 /*
15414 15081 * Unlink sfmmu from scd_sf_list this can be done without holding
15415 15082 * the hat lock as we hold the sfmmu_as lock which prevents
15416 15083 * hat_join_region from adding this thread to the scd again. Other
15417 15084 * threads check if sfmmu_scdp is NULL under hat lock and if it's NULL
15418 15085 * they won't get here, since sfmmu_leave_scd() clears sfmmu_scdp
15419 15086 * while holding the hat lock.
15420 15087 */
15421 15088 mutex_enter(&scdp->scd_mutex);
15422 15089 sfmmu_from_scd_list(&scdp->scd_sf_list, sfmmup);
15423 15090 mutex_exit(&scdp->scd_mutex);
15424 15091 SFMMU_STAT(sf_leave_scd);
15425 15092
15426 15093 SF_SCD_DECR_REF(srdp, scdp);
15427 15094 hatlockp = sfmmu_hat_enter(sfmmup);
15428 15095
15429 15096 }
15430 15097
15431 15098 /*
15432 15099 * Unlink and free up an SCD structure with a reference count of 0.
15433 15100 */
15434 15101 static void
15435 15102 sfmmu_destroy_scd(sf_srd_t *srdp, sf_scd_t *scdp, sf_region_map_t *scd_rmap)
15436 15103 {
15437 15104 sfmmu_t *scsfmmup;
15438 15105 sf_scd_t *sp;
15439 15106 hatlock_t *shatlockp;
15440 15107 int i, ret;
15441 15108
15442 15109 mutex_enter(&srdp->srd_scd_mutex);
15443 15110 for (sp = srdp->srd_scdp; sp != NULL; sp = sp->scd_next) {
15444 15111 if (sp == scdp)
15445 15112 break;
15446 15113 }
15447 15114 if (sp == NULL || sp->scd_refcnt) {
15448 15115 mutex_exit(&srdp->srd_scd_mutex);
15449 15116 return;
15450 15117 }
15451 15118
15452 15119 /*
15453 15120 * It is possible that the scd has been freed and reallocated with a
15454 15121 * different region map while we've been waiting for the srd_scd_mutex.
15455 15122 */
15456 15123 SF_RGNMAP_EQUAL(scd_rmap, &sp->scd_region_map, ret);
15457 15124 if (ret != 1) {
15458 15125 mutex_exit(&srdp->srd_scd_mutex);
15459 15126 return;
15460 15127 }
15461 15128
15462 15129 ASSERT(scdp->scd_sf_list == NULL);
15463 15130 /*
15464 15131 * Unlink scd from srd_scdp list.
15465 15132 */
15466 15133 sfmmu_remove_scd(&srdp->srd_scdp, scdp);
15467 15134 mutex_exit(&srdp->srd_scd_mutex);
15468 15135
15469 15136 sfmmu_unlink_scd_from_regions(srdp, scdp);
15470 15137
15471 15138 /* Clear shared context tsb and release ctx */
15472 15139 scsfmmup = scdp->scd_sfmmup;
15473 15140
15474 15141 /*
15475 15142 * create a barrier so that scd will not be destroyed
15476 15143 * if other thread still holds the same shared hat lock.
15477 15144 * E.g., sfmmu_tsbmiss_exception() needs to acquire the
15478 15145 * shared hat lock before checking the shared tsb reloc flag.
15479 15146 */
15480 15147 shatlockp = sfmmu_hat_enter(scsfmmup);
15481 15148 sfmmu_hat_exit(shatlockp);
15482 15149
15483 15150 sfmmu_free_scd_tsbs(scsfmmup);
15484 15151
15485 15152 for (i = 0; i < SFMMU_L1_HMERLINKS; i++) {
15486 15153 if (scsfmmup->sfmmu_hmeregion_links[i] != NULL) {
15487 15154 kmem_free(scsfmmup->sfmmu_hmeregion_links[i],
15488 15155 SFMMU_L2_HMERLINKS_SIZE);
15489 15156 scsfmmup->sfmmu_hmeregion_links[i] = NULL;
15490 15157 }
15491 15158 }
15492 15159 kmem_cache_free(sfmmuid_cache, scsfmmup);
15493 15160 kmem_cache_free(scd_cache, scdp);
15494 15161 SFMMU_STAT(sf_destroy_scd);
15495 15162 }
15496 15163
15497 15164 /*
15498 15165 * Modifies the HAT_CTX1_FLAG for each of the ISM segments which correspond to
15499 15166 * bits which are set in the ism_region_map parameter. This flag indicates to
15500 15167 * the tsbmiss handler that mapping for these segments should be loaded using
15501 15168 * the shared context.
15502 15169 */
15503 15170 static void
15504 15171 sfmmu_ism_hatflags(sfmmu_t *sfmmup, int addflag)
15505 15172 {
15506 15173 sf_scd_t *scdp = sfmmup->sfmmu_scdp;
15507 15174 ism_blk_t *ism_blkp;
15508 15175 ism_map_t *ism_map;
15509 15176 int i, rid;
15510 15177
15511 15178 ASSERT(sfmmup->sfmmu_iblk != NULL);
15512 15179 ASSERT(scdp != NULL);
15513 15180 /*
15514 15181 * Note that the caller either set HAT_ISMBUSY flag or checked
15515 15182 * under hat lock that HAT_ISMBUSY was not set by another thread.
15516 15183 */
15517 15184 ASSERT(sfmmu_hat_lock_held(sfmmup));
15518 15185
15519 15186 ism_blkp = sfmmup->sfmmu_iblk;
15520 15187 while (ism_blkp != NULL) {
15521 15188 ism_map = ism_blkp->iblk_maps;
15522 15189 for (i = 0; ism_map[i].imap_ismhat && i < ISM_MAP_SLOTS; i++) {
15523 15190 rid = ism_map[i].imap_rid;
15524 15191 if (rid == SFMMU_INVALID_ISMRID) {
15525 15192 continue;
15526 15193 }
15527 15194 ASSERT(rid >= 0 && rid < SFMMU_MAX_ISM_REGIONS);
15528 15195 if (SF_RGNMAP_TEST(scdp->scd_ismregion_map, rid) &&
15529 15196 addflag) {
15530 15197 ism_map[i].imap_hatflags |=
15531 15198 HAT_CTX1_FLAG;
15532 15199 } else {
15533 15200 ism_map[i].imap_hatflags &=
15534 15201 ~HAT_CTX1_FLAG;
15535 15202 }
15536 15203 }
15537 15204 ism_blkp = ism_blkp->iblk_next;
15538 15205 }
15539 15206 }
15540 15207
15541 15208 static int
15542 15209 sfmmu_srd_lock_held(sf_srd_t *srdp)
15543 15210 {
15544 15211 return (MUTEX_HELD(&srdp->srd_mutex));
15545 15212 }
15546 15213
15547 15214 /* ARGSUSED */
15548 15215 static int
15549 15216 sfmmu_scdcache_constructor(void *buf, void *cdrarg, int kmflags)
15550 15217 {
15551 15218 sf_scd_t *scdp = (sf_scd_t *)buf;
15552 15219
15553 15220 bzero(buf, sizeof (sf_scd_t));
15554 15221 mutex_init(&scdp->scd_mutex, NULL, MUTEX_DEFAULT, NULL);
15555 15222 return (0);
15556 15223 }
15557 15224
15558 15225 /* ARGSUSED */
15559 15226 static void
15560 15227 sfmmu_scdcache_destructor(void *buf, void *cdrarg)
15561 15228 {
15562 15229 sf_scd_t *scdp = (sf_scd_t *)buf;
15563 15230
15564 15231 mutex_destroy(&scdp->scd_mutex);
15565 15232 }
15566 15233
15567 15234 /*
15568 15235 * The listp parameter is a pointer to a list of hmeblks which are partially
15569 15236 * freed as result of calling sfmmu_hblk_hash_rm(), the last phase of the
15570 15237 * freeing process is to cross-call all cpus to ensure that there are no
15571 15238 * remaining cached references.
15572 15239 *
15573 15240 * If the local generation number is less than the global then we can free
15574 15241 * hmeblks which are already on the pending queue as another cpu has completed
15575 15242 * the cross-call.
15576 15243 *
15577 15244 * We cross-call to make sure that there are no threads on other cpus accessing
15578 15245 * these hmblks and then complete the process of freeing them under the
15579 15246 * following conditions:
15580 15247 * The total number of pending hmeblks is greater than the threshold
15581 15248 * The reserve list has fewer than HBLK_RESERVE_CNT hmeblks
15582 15249 * It is at least 1 second since the last time we cross-called
15583 15250 *
15584 15251 * Otherwise, we add the hmeblks to the per-cpu pending queue.
15585 15252 */
15586 15253 static void
15587 15254 sfmmu_hblks_list_purge(struct hme_blk **listp, int dontfree)
15588 15255 {
15589 15256 struct hme_blk *hblkp, *pr_hblkp = NULL;
15590 15257 int count = 0;
15591 15258 cpuset_t cpuset = cpu_ready_set;
15592 15259 cpu_hme_pend_t *cpuhp;
15593 15260 timestruc_t now;
15594 15261 int one_second_expired = 0;
15595 15262
15596 15263 gethrestime_lasttick(&now);
15597 15264
15598 15265 for (hblkp = *listp; hblkp != NULL; hblkp = hblkp->hblk_next) {
15599 15266 ASSERT(hblkp->hblk_shw_bit == 0);
15600 15267 ASSERT(hblkp->hblk_shared == 0);
15601 15268 count++;
15602 15269 pr_hblkp = hblkp;
15603 15270 }
15604 15271
15605 15272 cpuhp = &cpu_hme_pend[CPU->cpu_seqid];
15606 15273 mutex_enter(&cpuhp->chp_mutex);
15607 15274
15608 15275 if ((cpuhp->chp_count + count) == 0) {
15609 15276 mutex_exit(&cpuhp->chp_mutex);
15610 15277 return;
15611 15278 }
15612 15279
15613 15280 if ((now.tv_sec - cpuhp->chp_timestamp) > 1) {
15614 15281 one_second_expired = 1;
15615 15282 }
15616 15283
15617 15284 if (!dontfree && (freehblkcnt < HBLK_RESERVE_CNT ||
15618 15285 (cpuhp->chp_count + count) > cpu_hme_pend_thresh ||
15619 15286 one_second_expired)) {
15620 15287 /* Append global list to local */
15621 15288 if (pr_hblkp == NULL) {
15622 15289 *listp = cpuhp->chp_listp;
15623 15290 } else {
15624 15291 pr_hblkp->hblk_next = cpuhp->chp_listp;
15625 15292 }
15626 15293 cpuhp->chp_listp = NULL;
15627 15294 cpuhp->chp_count = 0;
15628 15295 cpuhp->chp_timestamp = now.tv_sec;
15629 15296 mutex_exit(&cpuhp->chp_mutex);
15630 15297
15631 15298 kpreempt_disable();
15632 15299 CPUSET_DEL(cpuset, CPU->cpu_id);
15633 15300 xt_sync(cpuset);
15634 15301 xt_sync(cpuset);
15635 15302 kpreempt_enable();
15636 15303
15637 15304 /*
15638 15305 * At this stage we know that no trap handlers on other
15639 15306 * cpus can have references to hmeblks on the list.
15640 15307 */
15641 15308 sfmmu_hblk_free(listp);
15642 15309 } else if (*listp != NULL) {
15643 15310 pr_hblkp->hblk_next = cpuhp->chp_listp;
15644 15311 cpuhp->chp_listp = *listp;
15645 15312 cpuhp->chp_count += count;
15646 15313 *listp = NULL;
15647 15314 mutex_exit(&cpuhp->chp_mutex);
15648 15315 } else {
15649 15316 mutex_exit(&cpuhp->chp_mutex);
15650 15317 }
15651 15318 }
15652 15319
15653 15320 /*
15654 15321 * Add an hmeblk to the the hash list.
15655 15322 */
15656 15323 void
15657 15324 sfmmu_hblk_hash_add(struct hmehash_bucket *hmebp, struct hme_blk *hmeblkp,
15658 15325 uint64_t hblkpa)
15659 15326 {
15660 15327 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
15661 15328 #ifdef DEBUG
15662 15329 if (hmebp->hmeblkp == NULL) {
15663 15330 ASSERT(hmebp->hmeh_nextpa == HMEBLK_ENDPA);
15664 15331 }
15665 15332 #endif /* DEBUG */
15666 15333
15667 15334 hmeblkp->hblk_nextpa = hmebp->hmeh_nextpa;
15668 15335 /*
15669 15336 * Since the TSB miss handler now does not lock the hash chain before
15670 15337 * walking it, make sure that the hmeblks nextpa is globally visible
15671 15338 * before we make the hmeblk globally visible by updating the chain root
15672 15339 * pointer in the hash bucket.
15673 15340 */
15674 15341 membar_producer();
15675 15342 hmebp->hmeh_nextpa = hblkpa;
15676 15343 hmeblkp->hblk_next = hmebp->hmeblkp;
15677 15344 hmebp->hmeblkp = hmeblkp;
15678 15345
15679 15346 }
15680 15347
15681 15348 /*
15682 15349 * This function is the first part of a 2 part process to remove an hmeblk
15683 15350 * from the hash chain. In this phase we unlink the hmeblk from the hash chain
15684 15351 * but leave the next physical pointer unchanged. The hmeblk is then linked onto
15685 15352 * a per-cpu pending list using the virtual address pointer.
15686 15353 *
15687 15354 * TSB miss trap handlers that start after this phase will no longer see
15688 15355 * this hmeblk. TSB miss handlers that still cache this hmeblk in a register
15689 15356 * can still use it for further chain traversal because we haven't yet modifed
15690 15357 * the next physical pointer or freed it.
15691 15358 *
15692 15359 * In the second phase of hmeblk removal we'll issue a barrier xcall before
15693 15360 * we reuse or free this hmeblk. This will make sure all lingering references to
15694 15361 * the hmeblk after first phase disappear before we finally reclaim it.
15695 15362 * This scheme eliminates the need for TSB miss handlers to lock hmeblk chains
15696 15363 * during their traversal.
15697 15364 *
15698 15365 * The hmehash_mutex must be held when calling this function.
15699 15366 *
15700 15367 * Input:
15701 15368 * hmebp - hme hash bucket pointer
15702 15369 * hmeblkp - address of hmeblk to be removed
15703 15370 * pr_hblk - virtual address of previous hmeblkp
15704 15371 * listp - pointer to list of hmeblks linked by virtual address
15705 15372 * free_now flag - indicates that a complete removal from the hash chains
15706 15373 * is necessary.
15707 15374 *
15708 15375 * It is inefficient to use the free_now flag as a cross-call is required to
15709 15376 * remove a single hmeblk from the hash chain but is necessary when hmeblks are
15710 15377 * in short supply.
15711 15378 */
15712 15379 void
15713 15380 sfmmu_hblk_hash_rm(struct hmehash_bucket *hmebp, struct hme_blk *hmeblkp,
15714 15381 struct hme_blk *pr_hblk, struct hme_blk **listp,
15715 15382 int free_now)
15716 15383 {
15717 15384 int shw_size, vshift;
15718 15385 struct hme_blk *shw_hblkp;
15719 15386 uint_t shw_mask, newshw_mask;
15720 15387 caddr_t vaddr;
15721 15388 int size;
15722 15389 cpuset_t cpuset = cpu_ready_set;
15723 15390
15724 15391 ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));
15725 15392
15726 15393 if (hmebp->hmeblkp == hmeblkp) {
15727 15394 hmebp->hmeh_nextpa = hmeblkp->hblk_nextpa;
15728 15395 hmebp->hmeblkp = hmeblkp->hblk_next;
15729 15396 } else {
15730 15397 pr_hblk->hblk_nextpa = hmeblkp->hblk_nextpa;
15731 15398 pr_hblk->hblk_next = hmeblkp->hblk_next;
15732 15399 }
15733 15400
15734 15401 size = get_hblk_ttesz(hmeblkp);
15735 15402 shw_hblkp = hmeblkp->hblk_shadow;
15736 15403 if (shw_hblkp) {
15737 15404 ASSERT(hblktosfmmu(hmeblkp) != KHATID);
15738 15405 ASSERT(!hmeblkp->hblk_shared);
15739 15406 #ifdef DEBUG
15740 15407 if (mmu_page_sizes == max_mmu_page_sizes) {
15741 15408 ASSERT(size < TTE256M);
15742 15409 } else {
15743 15410 ASSERT(size < TTE4M);
15744 15411 }
15745 15412 #endif /* DEBUG */
15746 15413
15747 15414 shw_size = get_hblk_ttesz(shw_hblkp);
15748 15415 vaddr = (caddr_t)get_hblk_base(hmeblkp);
15749 15416 vshift = vaddr_to_vshift(shw_hblkp->hblk_tag, vaddr, shw_size);
15750 15417 ASSERT(vshift < 8);
15751 15418 /*
15752 15419 * Atomically clear shadow mask bit
15753 15420 */
15754 15421 do {
15755 15422 shw_mask = shw_hblkp->hblk_shw_mask;
15756 15423 ASSERT(shw_mask & (1 << vshift));
15757 15424 newshw_mask = shw_mask & ~(1 << vshift);
15758 15425 newshw_mask = atomic_cas_32(&shw_hblkp->hblk_shw_mask,
15759 15426 shw_mask, newshw_mask);
15760 15427 } while (newshw_mask != shw_mask);
15761 15428 hmeblkp->hblk_shadow = NULL;
15762 15429 }
15763 15430 hmeblkp->hblk_shw_bit = 0;
15764 15431
15765 15432 if (hmeblkp->hblk_shared) {
15766 15433 #ifdef DEBUG
15767 15434 sf_srd_t *srdp;
15768 15435 sf_region_t *rgnp;
15769 15436 uint_t rid;
15770 15437
15771 15438 srdp = hblktosrd(hmeblkp);
15772 15439 ASSERT(srdp != NULL && srdp->srd_refcnt != 0);
15773 15440 rid = hmeblkp->hblk_tag.htag_rid;
15774 15441 ASSERT(SFMMU_IS_SHMERID_VALID(rid));
15775 15442 ASSERT(rid < SFMMU_MAX_HME_REGIONS);
15776 15443 rgnp = srdp->srd_hmergnp[rid];
15777 15444 ASSERT(rgnp != NULL);
15778 15445 SFMMU_VALIDATE_SHAREDHBLK(hmeblkp, srdp, rgnp, rid);
15779 15446 #endif /* DEBUG */
15780 15447 hmeblkp->hblk_shared = 0;
15781 15448 }
15782 15449 if (free_now) {
15783 15450 kpreempt_disable();
15784 15451 CPUSET_DEL(cpuset, CPU->cpu_id);
15785 15452 xt_sync(cpuset);
15786 15453 xt_sync(cpuset);
15787 15454 kpreempt_enable();
15788 15455
15789 15456 hmeblkp->hblk_nextpa = HMEBLK_ENDPA;
15790 15457 hmeblkp->hblk_next = NULL;
15791 15458 } else {
15792 15459 /* Append hmeblkp to listp for processing later. */
15793 15460 hmeblkp->hblk_next = *listp;
15794 15461 *listp = hmeblkp;
15795 15462 }
15796 15463 }
15797 15464
15798 15465 /*
15799 15466 * This routine is called when memory is in short supply and returns a free
15800 15467 * hmeblk of the requested size from the cpu pending lists.
15801 15468 */
15802 15469 static struct hme_blk *
15803 15470 sfmmu_check_pending_hblks(int size)
15804 15471 {
15805 15472 int i;
15806 15473 struct hme_blk *hmeblkp = NULL, *last_hmeblkp;
15807 15474 int found_hmeblk;
15808 15475 cpuset_t cpuset = cpu_ready_set;
15809 15476 cpu_hme_pend_t *cpuhp;
15810 15477
15811 15478 /* Flush cpu hblk pending queues */
15812 15479 for (i = 0; i < NCPU; i++) {
15813 15480 cpuhp = &cpu_hme_pend[i];
15814 15481 if (cpuhp->chp_listp != NULL) {
15815 15482 mutex_enter(&cpuhp->chp_mutex);
15816 15483 if (cpuhp->chp_listp == NULL) {
15817 15484 mutex_exit(&cpuhp->chp_mutex);
15818 15485 continue;
15819 15486 }
15820 15487 found_hmeblk = 0;
15821 15488 last_hmeblkp = NULL;
15822 15489 for (hmeblkp = cpuhp->chp_listp; hmeblkp != NULL;
15823 15490 hmeblkp = hmeblkp->hblk_next) {
15824 15491 if (get_hblk_ttesz(hmeblkp) == size) {
15825 15492 if (last_hmeblkp == NULL) {
15826 15493 cpuhp->chp_listp =
15827 15494 hmeblkp->hblk_next;
15828 15495 } else {
15829 15496 last_hmeblkp->hblk_next =
15830 15497 hmeblkp->hblk_next;
15831 15498 }
15832 15499 ASSERT(cpuhp->chp_count > 0);
15833 15500 cpuhp->chp_count--;
15834 15501 found_hmeblk = 1;
15835 15502 break;
15836 15503 } else {
15837 15504 last_hmeblkp = hmeblkp;
15838 15505 }
15839 15506 }
15840 15507 mutex_exit(&cpuhp->chp_mutex);
15841 15508
15842 15509 if (found_hmeblk) {
15843 15510 kpreempt_disable();
15844 15511 CPUSET_DEL(cpuset, CPU->cpu_id);
15845 15512 xt_sync(cpuset);
15846 15513 xt_sync(cpuset);
15847 15514 kpreempt_enable();
15848 15515 return (hmeblkp);
15849 15516 }
15850 15517 }
15851 15518 }
15852 15519 return (NULL);
15853 15520 }
↓ open down ↓ |
1500 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX