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