17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*a925c1ccSsudheer * Common Development and Distribution License (the "License"). 6*a925c1ccSsudheer * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*a925c1ccSsudheer * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 307c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 317c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 327c478bd9Sstevel@tonic-gate #include <sys/bitmap.h> 337c478bd9Sstevel@tonic-gate #include <sys/systm.h> 347c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h> 357c478bd9Sstevel@tonic-gate #include <vm/hat.h> 367c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h> 377c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h> 387c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * When pages are shared by more than one mapping, a list of these 437c478bd9Sstevel@tonic-gate * structs hangs off of the page_t connected by the hm_next and hm_prev 447c478bd9Sstevel@tonic-gate * fields. Every hment is also indexed by a system-wide hash table, using 457c478bd9Sstevel@tonic-gate * hm_hashnext to connect it to the chain of hments in a single hash 467c478bd9Sstevel@tonic-gate * bucket. 477c478bd9Sstevel@tonic-gate */ 487c478bd9Sstevel@tonic-gate struct hment { 497c478bd9Sstevel@tonic-gate struct hment *hm_hashnext; /* next mapping on hash chain */ 507c478bd9Sstevel@tonic-gate struct hment *hm_next; /* next mapping of same page */ 517c478bd9Sstevel@tonic-gate struct hment *hm_prev; /* previous mapping of same page */ 527c478bd9Sstevel@tonic-gate htable_t *hm_htable; /* corresponding htable_t */ 53*a925c1ccSsudheer pfn_t hm_pfn; /* mapping page frame number */ 547c478bd9Sstevel@tonic-gate uint16_t hm_entry; /* index of pte in htable */ 557c478bd9Sstevel@tonic-gate uint16_t hm_pad; /* explicitly expose compiler padding */ 567c478bd9Sstevel@tonic-gate #ifdef __amd64 577c478bd9Sstevel@tonic-gate uint32_t hm_pad2; /* explicitly expose compiler padding */ 587c478bd9Sstevel@tonic-gate #endif 597c478bd9Sstevel@tonic-gate }; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate /* 627c478bd9Sstevel@tonic-gate * Value returned by hment_walk() when dealing with a single mapping 637c478bd9Sstevel@tonic-gate * embedded in the page_t. 647c478bd9Sstevel@tonic-gate */ 657c478bd9Sstevel@tonic-gate #define HMENT_EMBEDDED ((hment_t *)(uintptr_t)1) 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate kmem_cache_t *hment_cache; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate /* 707c478bd9Sstevel@tonic-gate * The hment reserve is similar to the htable reserve, with the following 717c478bd9Sstevel@tonic-gate * exception. Hment's are never needed for HAT kmem allocs. 727c478bd9Sstevel@tonic-gate * 737c478bd9Sstevel@tonic-gate * The hment_reserve_amount variable is used, so that you can change it's 747c478bd9Sstevel@tonic-gate * value to zero via a kernel debugger to force stealing to get tested. 757c478bd9Sstevel@tonic-gate */ 767c478bd9Sstevel@tonic-gate #define HMENT_RESERVE_AMOUNT (200) /* currently a guess at right value. */ 777c478bd9Sstevel@tonic-gate uint_t hment_reserve_amount = HMENT_RESERVE_AMOUNT; 787c478bd9Sstevel@tonic-gate kmutex_t hment_reserve_mutex; 797c478bd9Sstevel@tonic-gate uint_t hment_reserve_count; 807c478bd9Sstevel@tonic-gate hment_t *hment_reserve_pool; 817c478bd9Sstevel@tonic-gate extern kthread_t *hat_reserves_thread; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * Possible performance RFE: we might need to make this dynamic, perhaps 857c478bd9Sstevel@tonic-gate * based on the number of pages in the system. 867c478bd9Sstevel@tonic-gate */ 877c478bd9Sstevel@tonic-gate #define HMENT_HASH_SIZE (64 * 1024) 887c478bd9Sstevel@tonic-gate static uint_t hment_hash_entries = HMENT_HASH_SIZE; 897c478bd9Sstevel@tonic-gate static hment_t **hment_hash; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* 927c478bd9Sstevel@tonic-gate * Lots of highly shared pages will have the same value for "entry" (consider 937c478bd9Sstevel@tonic-gate * the starting address of "xterm" or "sh"). So we'll distinguish them by 947c478bd9Sstevel@tonic-gate * adding the pfn of the page table into both the high bits. 957c478bd9Sstevel@tonic-gate * The shift by 9 corresponds to the range of values for entry (0..511). 967c478bd9Sstevel@tonic-gate */ 977c478bd9Sstevel@tonic-gate #define HMENT_HASH(pfn, entry) (uint32_t) \ 987c478bd9Sstevel@tonic-gate ((((pfn) << 9) + entry + pfn) & (hment_hash_entries - 1)) 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate * "mlist_lock" is a hashed mutex lock for protecting per-page mapping 1027c478bd9Sstevel@tonic-gate * lists and "hash_lock" is a similar lock protecting the hment hash 1037c478bd9Sstevel@tonic-gate * table. The hashed approach is taken to avoid the spatial overhead of 1047c478bd9Sstevel@tonic-gate * maintaining a separate lock for each page, while still achieving better 1057c478bd9Sstevel@tonic-gate * scalability than a single lock would allow. 1067c478bd9Sstevel@tonic-gate */ 1077c478bd9Sstevel@tonic-gate #define MLIST_NUM_LOCK 256 /* must be power of two */ 1087c478bd9Sstevel@tonic-gate static kmutex_t mlist_lock[MLIST_NUM_LOCK]; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate /* 1117c478bd9Sstevel@tonic-gate * the shift by 9 is so that all large pages don't use the same hash bucket 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate #define MLIST_MUTEX(pp) \ 1147c478bd9Sstevel@tonic-gate &mlist_lock[((pp)->p_pagenum + ((pp)->p_pagenum >> 9)) & \ 1157c478bd9Sstevel@tonic-gate (MLIST_NUM_LOCK - 1)] 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate #define HASH_NUM_LOCK 256 /* must be power of two */ 1187c478bd9Sstevel@tonic-gate static kmutex_t hash_lock[HASH_NUM_LOCK]; 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate #define HASH_MUTEX(idx) &hash_lock[(idx) & (HASH_NUM_LOCK-1)] 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate static hment_t *hment_steal(void); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate * put one hment onto the reserves list 1267c478bd9Sstevel@tonic-gate */ 1277c478bd9Sstevel@tonic-gate static void 1287c478bd9Sstevel@tonic-gate hment_put_reserve(hment_t *hm) 1297c478bd9Sstevel@tonic-gate { 1307c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_put_reserve); 1317c478bd9Sstevel@tonic-gate mutex_enter(&hment_reserve_mutex); 1327c478bd9Sstevel@tonic-gate hm->hm_next = hment_reserve_pool; 1337c478bd9Sstevel@tonic-gate hment_reserve_pool = hm; 1347c478bd9Sstevel@tonic-gate ++hment_reserve_count; 1357c478bd9Sstevel@tonic-gate mutex_exit(&hment_reserve_mutex); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate /* 1397c478bd9Sstevel@tonic-gate * Take one hment from the reserve. 1407c478bd9Sstevel@tonic-gate */ 1417c478bd9Sstevel@tonic-gate static hment_t * 1427c478bd9Sstevel@tonic-gate hment_get_reserve(void) 1437c478bd9Sstevel@tonic-gate { 1447c478bd9Sstevel@tonic-gate hment_t *hm = NULL; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate /* 1477c478bd9Sstevel@tonic-gate * We rely on a "donation system" to refill the hment reserve 1487c478bd9Sstevel@tonic-gate * list, which only takes place when we are allocating hments for 1497c478bd9Sstevel@tonic-gate * user mappings. It is theoretically possible that an incredibly 1507c478bd9Sstevel@tonic-gate * long string of kernel hment_alloc()s with no intervening user 1517c478bd9Sstevel@tonic-gate * hment_alloc()s could exhaust that pool. 1527c478bd9Sstevel@tonic-gate */ 1537c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_get_reserve); 1547c478bd9Sstevel@tonic-gate mutex_enter(&hment_reserve_mutex); 1557c478bd9Sstevel@tonic-gate if (hment_reserve_count != 0) { 1567c478bd9Sstevel@tonic-gate hm = hment_reserve_pool; 1577c478bd9Sstevel@tonic-gate hment_reserve_pool = hm->hm_next; 1587c478bd9Sstevel@tonic-gate --hment_reserve_count; 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate mutex_exit(&hment_reserve_mutex); 1617c478bd9Sstevel@tonic-gate return (hm); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate * Allocate an hment 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate static hment_t * 1687c478bd9Sstevel@tonic-gate hment_alloc() 1697c478bd9Sstevel@tonic-gate { 1707c478bd9Sstevel@tonic-gate int km_flag = can_steal_post_boot ? KM_NOSLEEP : KM_SLEEP; 1717c478bd9Sstevel@tonic-gate hment_t *hm = NULL; 1727c478bd9Sstevel@tonic-gate int use_reserves = (use_boot_reserve || 1737c478bd9Sstevel@tonic-gate curthread == hat_reserves_thread || panicstr != NULL); 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* 1767c478bd9Sstevel@tonic-gate * If we aren't using the reserves, try using kmem to get an hment. 1777c478bd9Sstevel@tonic-gate * Donate any successful allocations to reserves if low. 1787c478bd9Sstevel@tonic-gate * 1797c478bd9Sstevel@tonic-gate * If we're in panic, resort to using the reserves. 1807c478bd9Sstevel@tonic-gate */ 1817c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_alloc); 1827c478bd9Sstevel@tonic-gate if (!use_reserves) { 1837c478bd9Sstevel@tonic-gate for (;;) { 1847c478bd9Sstevel@tonic-gate hm = kmem_cache_alloc(hment_cache, km_flag); 1857c478bd9Sstevel@tonic-gate if (hment_reserve_count >= hment_reserve_amount || 1867c478bd9Sstevel@tonic-gate hm == NULL || panicstr != NULL || 1877c478bd9Sstevel@tonic-gate curthread == hat_reserves_thread) 1887c478bd9Sstevel@tonic-gate break; 1897c478bd9Sstevel@tonic-gate hment_put_reserve(hm); 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate /* 1947c478bd9Sstevel@tonic-gate * If allocation failed, we need to tap the reserves or steal 1957c478bd9Sstevel@tonic-gate */ 1967c478bd9Sstevel@tonic-gate if (hm == NULL) { 1977c478bd9Sstevel@tonic-gate if (use_reserves) 1987c478bd9Sstevel@tonic-gate hm = hment_get_reserve(); 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * If we still haven't gotten an hment, attempt to steal one by 2027c478bd9Sstevel@tonic-gate * victimizing a mapping in a user htable. 2037c478bd9Sstevel@tonic-gate */ 2047c478bd9Sstevel@tonic-gate if (hm == NULL && can_steal_post_boot) 2057c478bd9Sstevel@tonic-gate hm = hment_steal(); 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate /* 2087c478bd9Sstevel@tonic-gate * we're in dire straights, try the reserve 2097c478bd9Sstevel@tonic-gate */ 2107c478bd9Sstevel@tonic-gate if (hm == NULL) 2117c478bd9Sstevel@tonic-gate hm = hment_get_reserve(); 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * still no hment is a serious problem. 2157c478bd9Sstevel@tonic-gate */ 2167c478bd9Sstevel@tonic-gate if (hm == NULL) 2177c478bd9Sstevel@tonic-gate panic("hment_alloc(): no reserve, couldn't steal"); 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate hm->hm_entry = 0; 2227c478bd9Sstevel@tonic-gate hm->hm_htable = NULL; 2237c478bd9Sstevel@tonic-gate hm->hm_hashnext = NULL; 2247c478bd9Sstevel@tonic-gate hm->hm_next = NULL; 2257c478bd9Sstevel@tonic-gate hm->hm_prev = NULL; 226*a925c1ccSsudheer hm->hm_pfn = PFN_INVALID; 2277c478bd9Sstevel@tonic-gate return (hm); 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate /* 2317c478bd9Sstevel@tonic-gate * Free an hment, possibly to the reserves list when called from the 2327c478bd9Sstevel@tonic-gate * thread using the reserves. For example, when freeing an hment during an 2337c478bd9Sstevel@tonic-gate * htable_steal(), we can't recurse into the kmem allocator, so we just 2347c478bd9Sstevel@tonic-gate * push the hment onto the reserve list. 2357c478bd9Sstevel@tonic-gate */ 2367c478bd9Sstevel@tonic-gate void 2377c478bd9Sstevel@tonic-gate hment_free(hment_t *hm) 2387c478bd9Sstevel@tonic-gate { 2397c478bd9Sstevel@tonic-gate #ifdef DEBUG 2407c478bd9Sstevel@tonic-gate /* 2417c478bd9Sstevel@tonic-gate * zero out all fields to try and force any race conditions to segfault 2427c478bd9Sstevel@tonic-gate */ 2437c478bd9Sstevel@tonic-gate bzero(hm, sizeof (*hm)); 2447c478bd9Sstevel@tonic-gate #endif 2457c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_free); 2467c478bd9Sstevel@tonic-gate if (curthread == hat_reserves_thread || 2477c478bd9Sstevel@tonic-gate hment_reserve_count < hment_reserve_amount) 2487c478bd9Sstevel@tonic-gate hment_put_reserve(hm); 2497c478bd9Sstevel@tonic-gate else 2507c478bd9Sstevel@tonic-gate kmem_cache_free(hment_cache, hm); 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate int 2547c478bd9Sstevel@tonic-gate x86_hm_held(page_t *pp) 2557c478bd9Sstevel@tonic-gate { 2567c478bd9Sstevel@tonic-gate ASSERT(pp != NULL); 2577c478bd9Sstevel@tonic-gate return (MUTEX_HELD(MLIST_MUTEX(pp))); 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate void 2617c478bd9Sstevel@tonic-gate x86_hm_enter(page_t *pp) 2627c478bd9Sstevel@tonic-gate { 2637c478bd9Sstevel@tonic-gate ASSERT(pp != NULL); 2647c478bd9Sstevel@tonic-gate mutex_enter(MLIST_MUTEX(pp)); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate void 2687c478bd9Sstevel@tonic-gate x86_hm_exit(page_t *pp) 2697c478bd9Sstevel@tonic-gate { 2707c478bd9Sstevel@tonic-gate ASSERT(pp != NULL); 2717c478bd9Sstevel@tonic-gate mutex_exit(MLIST_MUTEX(pp)); 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate /* 2757c478bd9Sstevel@tonic-gate * Internal routine to add a full hment to a page_t mapping list 2767c478bd9Sstevel@tonic-gate */ 2777c478bd9Sstevel@tonic-gate static void 2787c478bd9Sstevel@tonic-gate hment_insert(hment_t *hm, page_t *pp) 2797c478bd9Sstevel@tonic-gate { 2807c478bd9Sstevel@tonic-gate uint_t idx; 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 2837c478bd9Sstevel@tonic-gate ASSERT(!pp->p_embed); 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate /* 2867c478bd9Sstevel@tonic-gate * Add the hment to the page's mapping list. 2877c478bd9Sstevel@tonic-gate */ 2887c478bd9Sstevel@tonic-gate ++pp->p_share; 2897c478bd9Sstevel@tonic-gate hm->hm_next = pp->p_mapping; 2907c478bd9Sstevel@tonic-gate if (pp->p_mapping != NULL) 2917c478bd9Sstevel@tonic-gate ((hment_t *)pp->p_mapping)->hm_prev = hm; 2927c478bd9Sstevel@tonic-gate pp->p_mapping = hm; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate /* 2957c478bd9Sstevel@tonic-gate * Add the hment to the system-wide hash table. 2967c478bd9Sstevel@tonic-gate */ 2977c478bd9Sstevel@tonic-gate idx = HMENT_HASH(hm->hm_htable->ht_pfn, hm->hm_entry); 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate mutex_enter(HASH_MUTEX(idx)); 3007c478bd9Sstevel@tonic-gate hm->hm_hashnext = hment_hash[idx]; 3017c478bd9Sstevel@tonic-gate hment_hash[idx] = hm; 3027c478bd9Sstevel@tonic-gate mutex_exit(HASH_MUTEX(idx)); 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate /* 3067c478bd9Sstevel@tonic-gate * Prepare a mapping list entry to the given page. 3077c478bd9Sstevel@tonic-gate * 3087c478bd9Sstevel@tonic-gate * There are 4 different situations to deal with: 3097c478bd9Sstevel@tonic-gate * 3107c478bd9Sstevel@tonic-gate * - Adding the first mapping to a page_t as an embedded hment 3117c478bd9Sstevel@tonic-gate * - Refaulting on an existing embedded mapping 3127c478bd9Sstevel@tonic-gate * - Upgrading an embedded mapping when adding a 2nd mapping 3137c478bd9Sstevel@tonic-gate * - Adding another mapping to a page_t that already has multiple mappings 3147c478bd9Sstevel@tonic-gate * note we don't optimized for the refaulting case here. 3157c478bd9Sstevel@tonic-gate * 3167c478bd9Sstevel@tonic-gate * Due to competition with other threads that may be mapping/unmapping the 3177c478bd9Sstevel@tonic-gate * same page and the need to drop all locks while allocating hments, any or 3187c478bd9Sstevel@tonic-gate * all of the 3 situations can occur (and in almost any order) in any given 3197c478bd9Sstevel@tonic-gate * call. Isn't this fun! 3207c478bd9Sstevel@tonic-gate */ 3217c478bd9Sstevel@tonic-gate hment_t * 3227c478bd9Sstevel@tonic-gate hment_prepare(htable_t *htable, uint_t entry, page_t *pp) 3237c478bd9Sstevel@tonic-gate { 3247c478bd9Sstevel@tonic-gate hment_t *hm = NULL; 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate for (;;) { 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate /* 3317c478bd9Sstevel@tonic-gate * The most common case is establishing the first mapping to a 3327c478bd9Sstevel@tonic-gate * page, so check that first. This doesn't need any allocated 3337c478bd9Sstevel@tonic-gate * hment. 3347c478bd9Sstevel@tonic-gate */ 3357c478bd9Sstevel@tonic-gate if (pp->p_mapping == NULL) { 3367c478bd9Sstevel@tonic-gate ASSERT(!pp->p_embed); 3377c478bd9Sstevel@tonic-gate ASSERT(pp->p_share == 0); 3387c478bd9Sstevel@tonic-gate if (hm == NULL) 3397c478bd9Sstevel@tonic-gate break; 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate /* 3427c478bd9Sstevel@tonic-gate * we had an hment already, so free it and retry 3437c478bd9Sstevel@tonic-gate */ 3447c478bd9Sstevel@tonic-gate goto free_and_continue; 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate /* 3487c478bd9Sstevel@tonic-gate * If there is an embedded mapping, we may need to 3497c478bd9Sstevel@tonic-gate * convert it to an hment. 3507c478bd9Sstevel@tonic-gate */ 3517c478bd9Sstevel@tonic-gate if (pp->p_embed) { 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate /* should point to htable */ 3547c478bd9Sstevel@tonic-gate ASSERT(pp->p_mapping != NULL); 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate /* 3577c478bd9Sstevel@tonic-gate * If we are faulting on a pre-existing mapping 3587c478bd9Sstevel@tonic-gate * there is no need to promote/allocate a new hment. 3597c478bd9Sstevel@tonic-gate * This happens a lot due to segmap. 3607c478bd9Sstevel@tonic-gate */ 3617c478bd9Sstevel@tonic-gate if (pp->p_mapping == htable && pp->p_mlentry == entry) { 3627c478bd9Sstevel@tonic-gate if (hm == NULL) 3637c478bd9Sstevel@tonic-gate break; 3647c478bd9Sstevel@tonic-gate goto free_and_continue; 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate /* 3687c478bd9Sstevel@tonic-gate * If we have an hment allocated, use it to promote the 3697c478bd9Sstevel@tonic-gate * existing embedded mapping. 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate if (hm != NULL) { 3727c478bd9Sstevel@tonic-gate hm->hm_htable = pp->p_mapping; 3737c478bd9Sstevel@tonic-gate hm->hm_entry = pp->p_mlentry; 374*a925c1ccSsudheer hm->hm_pfn = pp->p_pagenum; 3757c478bd9Sstevel@tonic-gate pp->p_mapping = NULL; 3767c478bd9Sstevel@tonic-gate pp->p_share = 0; 3777c478bd9Sstevel@tonic-gate pp->p_embed = 0; 3787c478bd9Sstevel@tonic-gate hment_insert(hm, pp); 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate /* 3827c478bd9Sstevel@tonic-gate * We either didn't have an hment allocated or we just 3837c478bd9Sstevel@tonic-gate * used it for the embedded mapping. In either case, 3847c478bd9Sstevel@tonic-gate * allocate another hment and restart. 3857c478bd9Sstevel@tonic-gate */ 3867c478bd9Sstevel@tonic-gate goto allocate_and_continue; 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate /* 3907c478bd9Sstevel@tonic-gate * Last possibility is that we're adding an hment to a list 3917c478bd9Sstevel@tonic-gate * of hments. 3927c478bd9Sstevel@tonic-gate */ 3937c478bd9Sstevel@tonic-gate if (hm != NULL) 3947c478bd9Sstevel@tonic-gate break; 3957c478bd9Sstevel@tonic-gate allocate_and_continue: 3967c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 3977c478bd9Sstevel@tonic-gate hm = hment_alloc(); 3987c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 3997c478bd9Sstevel@tonic-gate continue; 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate free_and_continue: 4027c478bd9Sstevel@tonic-gate /* 4037c478bd9Sstevel@tonic-gate * we allocated an hment already, free it and retry 4047c478bd9Sstevel@tonic-gate */ 4057c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 4067c478bd9Sstevel@tonic-gate hment_free(hm); 4077c478bd9Sstevel@tonic-gate hm = NULL; 4087c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 4097c478bd9Sstevel@tonic-gate } 4107c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 4117c478bd9Sstevel@tonic-gate return (hm); 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate /* 4157c478bd9Sstevel@tonic-gate * Record a mapping list entry for the htable/entry to the given page. 4167c478bd9Sstevel@tonic-gate * 4177c478bd9Sstevel@tonic-gate * hment_prepare() should have properly set up the situation. 4187c478bd9Sstevel@tonic-gate */ 4197c478bd9Sstevel@tonic-gate void 4207c478bd9Sstevel@tonic-gate hment_assign(htable_t *htable, uint_t entry, page_t *pp, hment_t *hm) 4217c478bd9Sstevel@tonic-gate { 4227c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate /* 4257c478bd9Sstevel@tonic-gate * The most common case is establishing the first mapping to a 4267c478bd9Sstevel@tonic-gate * page, so check that first. This doesn't need any allocated 4277c478bd9Sstevel@tonic-gate * hment. 4287c478bd9Sstevel@tonic-gate */ 4297c478bd9Sstevel@tonic-gate if (pp->p_mapping == NULL) { 4307c478bd9Sstevel@tonic-gate ASSERT(hm == NULL); 4317c478bd9Sstevel@tonic-gate ASSERT(!pp->p_embed); 4327c478bd9Sstevel@tonic-gate ASSERT(pp->p_share == 0); 4337c478bd9Sstevel@tonic-gate pp->p_embed = 1; 4347c478bd9Sstevel@tonic-gate pp->p_mapping = htable; 4357c478bd9Sstevel@tonic-gate pp->p_mlentry = entry; 4367c478bd9Sstevel@tonic-gate return; 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate /* 4407c478bd9Sstevel@tonic-gate * We should never get here with a pre-existing embedded maping 4417c478bd9Sstevel@tonic-gate */ 4427c478bd9Sstevel@tonic-gate ASSERT(!pp->p_embed); 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate /* 4457c478bd9Sstevel@tonic-gate * add the new hment to the mapping list 4467c478bd9Sstevel@tonic-gate */ 4477c478bd9Sstevel@tonic-gate ASSERT(hm != NULL); 4487c478bd9Sstevel@tonic-gate hm->hm_htable = htable; 4497c478bd9Sstevel@tonic-gate hm->hm_entry = entry; 450*a925c1ccSsudheer hm->hm_pfn = pp->p_pagenum; 4517c478bd9Sstevel@tonic-gate hment_insert(hm, pp); 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate /* 4557c478bd9Sstevel@tonic-gate * Walk through the mappings for a page. 4567c478bd9Sstevel@tonic-gate * 4577c478bd9Sstevel@tonic-gate * must already have done an x86_hm_enter() 4587c478bd9Sstevel@tonic-gate */ 4597c478bd9Sstevel@tonic-gate hment_t * 4607c478bd9Sstevel@tonic-gate hment_walk(page_t *pp, htable_t **ht, uint_t *entry, hment_t *prev) 4617c478bd9Sstevel@tonic-gate { 4627c478bd9Sstevel@tonic-gate hment_t *hm; 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate if (pp->p_embed) { 4677c478bd9Sstevel@tonic-gate if (prev == NULL) { 4687c478bd9Sstevel@tonic-gate *ht = (htable_t *)pp->p_mapping; 4697c478bd9Sstevel@tonic-gate *entry = pp->p_mlentry; 4707c478bd9Sstevel@tonic-gate hm = HMENT_EMBEDDED; 4717c478bd9Sstevel@tonic-gate } else { 4727c478bd9Sstevel@tonic-gate ASSERT(prev == HMENT_EMBEDDED); 4737c478bd9Sstevel@tonic-gate hm = NULL; 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate } else { 4767c478bd9Sstevel@tonic-gate if (prev == NULL) { 4777c478bd9Sstevel@tonic-gate ASSERT(prev != HMENT_EMBEDDED); 4787c478bd9Sstevel@tonic-gate hm = (hment_t *)pp->p_mapping; 4797c478bd9Sstevel@tonic-gate } else { 4807c478bd9Sstevel@tonic-gate hm = prev->hm_next; 4817c478bd9Sstevel@tonic-gate } 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate if (hm != NULL) { 4847c478bd9Sstevel@tonic-gate *ht = hm->hm_htable; 4857c478bd9Sstevel@tonic-gate *entry = hm->hm_entry; 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate } 4887c478bd9Sstevel@tonic-gate return (hm); 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate /* 4927c478bd9Sstevel@tonic-gate * Remove a mapping to a page from its mapping list. Must have 4937c478bd9Sstevel@tonic-gate * the corresponding mapping list locked. 4947c478bd9Sstevel@tonic-gate * Finds the mapping list entry with the given pte_t and 4957c478bd9Sstevel@tonic-gate * unlinks it from the mapping list. 4967c478bd9Sstevel@tonic-gate */ 4977c478bd9Sstevel@tonic-gate hment_t * 4987c478bd9Sstevel@tonic-gate hment_remove(page_t *pp, htable_t *ht, uint_t entry) 4997c478bd9Sstevel@tonic-gate { 5007c478bd9Sstevel@tonic-gate hment_t *prev = NULL; 5017c478bd9Sstevel@tonic-gate hment_t *hm; 5027c478bd9Sstevel@tonic-gate uint_t idx; 503*a925c1ccSsudheer pfn_t pfn; 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate /* 5087c478bd9Sstevel@tonic-gate * Check if we have only one mapping embedded in the page_t. 5097c478bd9Sstevel@tonic-gate */ 5107c478bd9Sstevel@tonic-gate if (pp->p_embed) { 5117c478bd9Sstevel@tonic-gate ASSERT(ht == (htable_t *)pp->p_mapping); 5127c478bd9Sstevel@tonic-gate ASSERT(entry == pp->p_mlentry); 5137c478bd9Sstevel@tonic-gate ASSERT(pp->p_share == 0); 5147c478bd9Sstevel@tonic-gate pp->p_mapping = NULL; 5157c478bd9Sstevel@tonic-gate pp->p_mlentry = 0; 5167c478bd9Sstevel@tonic-gate pp->p_embed = 0; 5177c478bd9Sstevel@tonic-gate return (NULL); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate /* 5217c478bd9Sstevel@tonic-gate * Otherwise it must be in the list of hments. 5227c478bd9Sstevel@tonic-gate * Find the hment in the system-wide hash table and remove it. 5237c478bd9Sstevel@tonic-gate */ 5247c478bd9Sstevel@tonic-gate ASSERT(pp->p_share != 0); 525*a925c1ccSsudheer pfn = pp->p_pagenum; 5267c478bd9Sstevel@tonic-gate idx = HMENT_HASH(ht->ht_pfn, entry); 5277c478bd9Sstevel@tonic-gate mutex_enter(HASH_MUTEX(idx)); 5287c478bd9Sstevel@tonic-gate hm = hment_hash[idx]; 529*a925c1ccSsudheer while (hm && (hm->hm_htable != ht || hm->hm_entry != entry || 530*a925c1ccSsudheer hm->hm_pfn != pfn)) { 5317c478bd9Sstevel@tonic-gate prev = hm; 5327c478bd9Sstevel@tonic-gate hm = hm->hm_hashnext; 5337c478bd9Sstevel@tonic-gate } 534aa2ed9e5Sjosephb if (hm == NULL) { 535aa2ed9e5Sjosephb panic("hment_remove() missing in hash table pp=%lx, ht=%lx," 536aa2ed9e5Sjosephb "entry=0x%x hash index=0x%x", (uintptr_t)pp, (uintptr_t)ht, 537aa2ed9e5Sjosephb entry, idx); 538aa2ed9e5Sjosephb } 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate if (prev) 5417c478bd9Sstevel@tonic-gate prev->hm_hashnext = hm->hm_hashnext; 5427c478bd9Sstevel@tonic-gate else 5437c478bd9Sstevel@tonic-gate hment_hash[idx] = hm->hm_hashnext; 5447c478bd9Sstevel@tonic-gate mutex_exit(HASH_MUTEX(idx)); 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate /* 5477c478bd9Sstevel@tonic-gate * Remove the hment from the page's mapping list 5487c478bd9Sstevel@tonic-gate */ 5497c478bd9Sstevel@tonic-gate if (hm->hm_next) 5507c478bd9Sstevel@tonic-gate hm->hm_next->hm_prev = hm->hm_prev; 5517c478bd9Sstevel@tonic-gate if (hm->hm_prev) 5527c478bd9Sstevel@tonic-gate hm->hm_prev->hm_next = hm->hm_next; 5537c478bd9Sstevel@tonic-gate else 5547c478bd9Sstevel@tonic-gate pp->p_mapping = hm->hm_next; 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate --pp->p_share; 5577c478bd9Sstevel@tonic-gate hm->hm_hashnext = NULL; 5587c478bd9Sstevel@tonic-gate hm->hm_next = NULL; 5597c478bd9Sstevel@tonic-gate hm->hm_prev = NULL; 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate return (hm); 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate /* 5657c478bd9Sstevel@tonic-gate * Put initial hment's in the reserve pool. 5667c478bd9Sstevel@tonic-gate */ 5677c478bd9Sstevel@tonic-gate void 5687c478bd9Sstevel@tonic-gate hment_reserve(uint_t count) 5697c478bd9Sstevel@tonic-gate { 5707c478bd9Sstevel@tonic-gate hment_t *hm; 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate count += hment_reserve_amount; 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate while (hment_reserve_count < count) { 5757c478bd9Sstevel@tonic-gate hm = kmem_cache_alloc(hment_cache, KM_NOSLEEP); 5767c478bd9Sstevel@tonic-gate if (hm == NULL) 5777c478bd9Sstevel@tonic-gate return; 5787c478bd9Sstevel@tonic-gate hment_put_reserve(hm); 5797c478bd9Sstevel@tonic-gate } 5807c478bd9Sstevel@tonic-gate } 5817c478bd9Sstevel@tonic-gate 5827c478bd9Sstevel@tonic-gate /* 5837c478bd9Sstevel@tonic-gate * Readjust the hment reserves after they may have been used. 5847c478bd9Sstevel@tonic-gate */ 5857c478bd9Sstevel@tonic-gate void 5867c478bd9Sstevel@tonic-gate hment_adjust_reserve() 5877c478bd9Sstevel@tonic-gate { 5887c478bd9Sstevel@tonic-gate hment_t *hm; 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate /* 5917c478bd9Sstevel@tonic-gate * Free up any excess reserves 5927c478bd9Sstevel@tonic-gate */ 5937c478bd9Sstevel@tonic-gate while (hment_reserve_count > hment_reserve_amount) { 5947c478bd9Sstevel@tonic-gate ASSERT(curthread != hat_reserves_thread); 5957c478bd9Sstevel@tonic-gate hm = hment_get_reserve(); 5967c478bd9Sstevel@tonic-gate if (hm == NULL) 5977c478bd9Sstevel@tonic-gate return; 5987c478bd9Sstevel@tonic-gate hment_free(hm); 5997c478bd9Sstevel@tonic-gate } 6007c478bd9Sstevel@tonic-gate } 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate /* 6037c478bd9Sstevel@tonic-gate * initialize hment data structures 6047c478bd9Sstevel@tonic-gate */ 6057c478bd9Sstevel@tonic-gate void 6067c478bd9Sstevel@tonic-gate hment_init(void) 6077c478bd9Sstevel@tonic-gate { 6087c478bd9Sstevel@tonic-gate int i; 6097c478bd9Sstevel@tonic-gate int flags = KMC_NOHASH | KMC_NODEBUG; 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate /* 6127c478bd9Sstevel@tonic-gate * Initialize kmem caches. On 32 bit kernel's we shut off 6137c478bd9Sstevel@tonic-gate * debug information to save on precious kernel VA usage. 6147c478bd9Sstevel@tonic-gate */ 6157c478bd9Sstevel@tonic-gate hment_cache = kmem_cache_create("hment_t", 6167c478bd9Sstevel@tonic-gate sizeof (hment_t), 0, NULL, NULL, NULL, 6177c478bd9Sstevel@tonic-gate NULL, hat_memload_arena, flags); 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate hment_hash = kmem_zalloc(hment_hash_entries * sizeof (hment_t *), 6207c478bd9Sstevel@tonic-gate KM_SLEEP); 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate for (i = 0; i < MLIST_NUM_LOCK; i++) 6237c478bd9Sstevel@tonic-gate mutex_init(&mlist_lock[i], NULL, MUTEX_DEFAULT, NULL); 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate for (i = 0; i < HASH_NUM_LOCK; i++) 6267c478bd9Sstevel@tonic-gate mutex_init(&hash_lock[i], NULL, MUTEX_DEFAULT, NULL); 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate /* 6327c478bd9Sstevel@tonic-gate * return the number of mappings to a page 6337c478bd9Sstevel@tonic-gate * 6347c478bd9Sstevel@tonic-gate * Note there is no ASSERT() that the MUTEX is held for this. 6357c478bd9Sstevel@tonic-gate * Hence the return value might be inaccurate if this is called without 6367c478bd9Sstevel@tonic-gate * doing an x86_hm_enter(). 6377c478bd9Sstevel@tonic-gate */ 6387c478bd9Sstevel@tonic-gate uint_t 6397c478bd9Sstevel@tonic-gate hment_mapcnt(page_t *pp) 6407c478bd9Sstevel@tonic-gate { 6417c478bd9Sstevel@tonic-gate uint_t cnt; 6427c478bd9Sstevel@tonic-gate uint_t szc; 6437c478bd9Sstevel@tonic-gate page_t *larger; 6447c478bd9Sstevel@tonic-gate hment_t *hm; 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 6477c478bd9Sstevel@tonic-gate if (pp->p_mapping == NULL) 6487c478bd9Sstevel@tonic-gate cnt = 0; 6497c478bd9Sstevel@tonic-gate else if (pp->p_embed) 6507c478bd9Sstevel@tonic-gate cnt = 1; 6517c478bd9Sstevel@tonic-gate else 6527c478bd9Sstevel@tonic-gate cnt = pp->p_share; 6537c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate /* 6567c478bd9Sstevel@tonic-gate * walk through all larger mapping sizes counting mappings 6577c478bd9Sstevel@tonic-gate */ 6587c478bd9Sstevel@tonic-gate for (szc = 1; szc <= pp->p_szc; ++szc) { 6597c478bd9Sstevel@tonic-gate larger = PP_GROUPLEADER(pp, szc); 6607c478bd9Sstevel@tonic-gate if (larger == pp) /* don't double count large mappings */ 6617c478bd9Sstevel@tonic-gate continue; 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate x86_hm_enter(larger); 6647c478bd9Sstevel@tonic-gate if (larger->p_mapping != NULL) { 6657c478bd9Sstevel@tonic-gate if (larger->p_embed && 6667c478bd9Sstevel@tonic-gate ((htable_t *)larger->p_mapping)->ht_level == szc) { 6677c478bd9Sstevel@tonic-gate ++cnt; 6687c478bd9Sstevel@tonic-gate } else if (!larger->p_embed) { 6697c478bd9Sstevel@tonic-gate for (hm = larger->p_mapping; hm; 6707c478bd9Sstevel@tonic-gate hm = hm->hm_next) { 6717c478bd9Sstevel@tonic-gate if (hm->hm_htable->ht_level == szc) 6727c478bd9Sstevel@tonic-gate ++cnt; 6737c478bd9Sstevel@tonic-gate } 6747c478bd9Sstevel@tonic-gate } 6757c478bd9Sstevel@tonic-gate } 6767c478bd9Sstevel@tonic-gate x86_hm_exit(larger); 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate return (cnt); 6797c478bd9Sstevel@tonic-gate } 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate /* 6827c478bd9Sstevel@tonic-gate * We need to steal an hment. Walk through all the page_t's until we 6837c478bd9Sstevel@tonic-gate * find one that has multiple mappings. Unload one of the mappings 6847c478bd9Sstevel@tonic-gate * and reclaim that hment. Note that we'll save/restart the starting 6857c478bd9Sstevel@tonic-gate * page to try and spread the pain. 6867c478bd9Sstevel@tonic-gate */ 6877c478bd9Sstevel@tonic-gate static page_t *last_page = NULL; 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate static hment_t * 6907c478bd9Sstevel@tonic-gate hment_steal(void) 6917c478bd9Sstevel@tonic-gate { 6927c478bd9Sstevel@tonic-gate page_t *last = last_page; 6937c478bd9Sstevel@tonic-gate page_t *pp = last; 6947c478bd9Sstevel@tonic-gate hment_t *hm = NULL; 6957c478bd9Sstevel@tonic-gate hment_t *hm2; 6967c478bd9Sstevel@tonic-gate htable_t *ht; 6977c478bd9Sstevel@tonic-gate uint_t found_one = 0; 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_steals); 7007c478bd9Sstevel@tonic-gate if (pp == NULL) 7017c478bd9Sstevel@tonic-gate last = pp = page_first(); 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate while (!found_one) { 7047c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_steal_exam); 7057c478bd9Sstevel@tonic-gate pp = page_next(pp); 7067c478bd9Sstevel@tonic-gate if (pp == NULL) 7077c478bd9Sstevel@tonic-gate pp = page_first(); 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate /* 7107c478bd9Sstevel@tonic-gate * The loop and function exit here if nothing found to steal. 7117c478bd9Sstevel@tonic-gate */ 7127c478bd9Sstevel@tonic-gate if (pp == last) 7137c478bd9Sstevel@tonic-gate return (NULL); 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate /* 7167c478bd9Sstevel@tonic-gate * Only lock the page_t if it has hments. 7177c478bd9Sstevel@tonic-gate */ 7187c478bd9Sstevel@tonic-gate if (pp->p_mapping == NULL || pp->p_embed) 7197c478bd9Sstevel@tonic-gate continue; 7207c478bd9Sstevel@tonic-gate 7217c478bd9Sstevel@tonic-gate /* 7227c478bd9Sstevel@tonic-gate * Search the mapping list for a usable mapping. 7237c478bd9Sstevel@tonic-gate */ 7247c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 7257c478bd9Sstevel@tonic-gate if (!pp->p_embed) { 7267c478bd9Sstevel@tonic-gate for (hm = pp->p_mapping; hm; hm = hm->hm_next) { 7277c478bd9Sstevel@tonic-gate ht = hm->hm_htable; 7287c478bd9Sstevel@tonic-gate if (ht->ht_hat != kas.a_hat && 7297c478bd9Sstevel@tonic-gate ht->ht_busy == 0 && 7307c478bd9Sstevel@tonic-gate ht->ht_lock_cnt == 0) { 7317c478bd9Sstevel@tonic-gate found_one = 1; 7327c478bd9Sstevel@tonic-gate break; 7337c478bd9Sstevel@tonic-gate } 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate } 7367c478bd9Sstevel@tonic-gate if (!found_one) 7377c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 7387c478bd9Sstevel@tonic-gate } 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate /* 7417c478bd9Sstevel@tonic-gate * Steal the mapping we found. Note that hati_page_unmap() will 7427c478bd9Sstevel@tonic-gate * do the x86_hm_exit(). 7437c478bd9Sstevel@tonic-gate */ 7447c478bd9Sstevel@tonic-gate hm2 = hati_page_unmap(pp, ht, hm->hm_entry); 7457c478bd9Sstevel@tonic-gate ASSERT(hm2 == hm); 7467c478bd9Sstevel@tonic-gate last_page = pp; 7477c478bd9Sstevel@tonic-gate return (hm); 7487c478bd9Sstevel@tonic-gate } 749