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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 317c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 327c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 337c478bd9Sstevel@tonic-gate #include <sys/bitmap.h> 347c478bd9Sstevel@tonic-gate #include <sys/systm.h> 357c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h> 367c478bd9Sstevel@tonic-gate #include <vm/hat.h> 377c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h> 387c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h> 397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate /* 437c478bd9Sstevel@tonic-gate * When pages are shared by more than one mapping, a list of these 447c478bd9Sstevel@tonic-gate * structs hangs off of the page_t connected by the hm_next and hm_prev 457c478bd9Sstevel@tonic-gate * fields. Every hment is also indexed by a system-wide hash table, using 467c478bd9Sstevel@tonic-gate * hm_hashnext to connect it to the chain of hments in a single hash 477c478bd9Sstevel@tonic-gate * bucket. 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate struct hment { 507c478bd9Sstevel@tonic-gate struct hment *hm_hashnext; /* next mapping on hash chain */ 517c478bd9Sstevel@tonic-gate struct hment *hm_next; /* next mapping of same page */ 527c478bd9Sstevel@tonic-gate struct hment *hm_prev; /* previous mapping of same page */ 537c478bd9Sstevel@tonic-gate htable_t *hm_htable; /* corresponding htable_t */ 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; 2267c478bd9Sstevel@tonic-gate return (hm); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* 2307c478bd9Sstevel@tonic-gate * Free an hment, possibly to the reserves list when called from the 2317c478bd9Sstevel@tonic-gate * thread using the reserves. For example, when freeing an hment during an 2327c478bd9Sstevel@tonic-gate * htable_steal(), we can't recurse into the kmem allocator, so we just 2337c478bd9Sstevel@tonic-gate * push the hment onto the reserve list. 2347c478bd9Sstevel@tonic-gate */ 2357c478bd9Sstevel@tonic-gate void 2367c478bd9Sstevel@tonic-gate hment_free(hment_t *hm) 2377c478bd9Sstevel@tonic-gate { 2387c478bd9Sstevel@tonic-gate #ifdef DEBUG 2397c478bd9Sstevel@tonic-gate /* 2407c478bd9Sstevel@tonic-gate * zero out all fields to try and force any race conditions to segfault 2417c478bd9Sstevel@tonic-gate */ 2427c478bd9Sstevel@tonic-gate bzero(hm, sizeof (*hm)); 2437c478bd9Sstevel@tonic-gate #endif 2447c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_free); 2457c478bd9Sstevel@tonic-gate if (curthread == hat_reserves_thread || 2467c478bd9Sstevel@tonic-gate hment_reserve_count < hment_reserve_amount) 2477c478bd9Sstevel@tonic-gate hment_put_reserve(hm); 2487c478bd9Sstevel@tonic-gate else 2497c478bd9Sstevel@tonic-gate kmem_cache_free(hment_cache, hm); 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; 3737c478bd9Sstevel@tonic-gate pp->p_mapping = NULL; 3747c478bd9Sstevel@tonic-gate pp->p_share = 0; 3757c478bd9Sstevel@tonic-gate pp->p_embed = 0; 3767c478bd9Sstevel@tonic-gate hment_insert(hm, pp); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate /* 3807c478bd9Sstevel@tonic-gate * We either didn't have an hment allocated or we just 3817c478bd9Sstevel@tonic-gate * used it for the embedded mapping. In either case, 3827c478bd9Sstevel@tonic-gate * allocate another hment and restart. 3837c478bd9Sstevel@tonic-gate */ 3847c478bd9Sstevel@tonic-gate goto allocate_and_continue; 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate /* 3887c478bd9Sstevel@tonic-gate * Last possibility is that we're adding an hment to a list 3897c478bd9Sstevel@tonic-gate * of hments. 3907c478bd9Sstevel@tonic-gate */ 3917c478bd9Sstevel@tonic-gate if (hm != NULL) 3927c478bd9Sstevel@tonic-gate break; 3937c478bd9Sstevel@tonic-gate allocate_and_continue: 3947c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 3957c478bd9Sstevel@tonic-gate hm = hment_alloc(); 3967c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 3977c478bd9Sstevel@tonic-gate continue; 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate free_and_continue: 4007c478bd9Sstevel@tonic-gate /* 4017c478bd9Sstevel@tonic-gate * we allocated an hment already, free it and retry 4027c478bd9Sstevel@tonic-gate */ 4037c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 4047c478bd9Sstevel@tonic-gate hment_free(hm); 4057c478bd9Sstevel@tonic-gate hm = NULL; 4067c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 4097c478bd9Sstevel@tonic-gate return (hm); 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate /* 4137c478bd9Sstevel@tonic-gate * Record a mapping list entry for the htable/entry to the given page. 4147c478bd9Sstevel@tonic-gate * 4157c478bd9Sstevel@tonic-gate * hment_prepare() should have properly set up the situation. 4167c478bd9Sstevel@tonic-gate */ 4177c478bd9Sstevel@tonic-gate void 4187c478bd9Sstevel@tonic-gate hment_assign(htable_t *htable, uint_t entry, page_t *pp, hment_t *hm) 4197c478bd9Sstevel@tonic-gate { 4207c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate /* 4237c478bd9Sstevel@tonic-gate * The most common case is establishing the first mapping to a 4247c478bd9Sstevel@tonic-gate * page, so check that first. This doesn't need any allocated 4257c478bd9Sstevel@tonic-gate * hment. 4267c478bd9Sstevel@tonic-gate */ 4277c478bd9Sstevel@tonic-gate if (pp->p_mapping == NULL) { 4287c478bd9Sstevel@tonic-gate ASSERT(hm == NULL); 4297c478bd9Sstevel@tonic-gate ASSERT(!pp->p_embed); 4307c478bd9Sstevel@tonic-gate ASSERT(pp->p_share == 0); 4317c478bd9Sstevel@tonic-gate pp->p_embed = 1; 4327c478bd9Sstevel@tonic-gate pp->p_mapping = htable; 4337c478bd9Sstevel@tonic-gate pp->p_mlentry = entry; 4347c478bd9Sstevel@tonic-gate return; 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate /* 4387c478bd9Sstevel@tonic-gate * We should never get here with a pre-existing embedded maping 4397c478bd9Sstevel@tonic-gate */ 4407c478bd9Sstevel@tonic-gate ASSERT(!pp->p_embed); 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate /* 4437c478bd9Sstevel@tonic-gate * add the new hment to the mapping list 4447c478bd9Sstevel@tonic-gate */ 4457c478bd9Sstevel@tonic-gate ASSERT(hm != NULL); 4467c478bd9Sstevel@tonic-gate hm->hm_htable = htable; 4477c478bd9Sstevel@tonic-gate hm->hm_entry = entry; 4487c478bd9Sstevel@tonic-gate hment_insert(hm, pp); 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate /* 4527c478bd9Sstevel@tonic-gate * Walk through the mappings for a page. 4537c478bd9Sstevel@tonic-gate * 4547c478bd9Sstevel@tonic-gate * must already have done an x86_hm_enter() 4557c478bd9Sstevel@tonic-gate */ 4567c478bd9Sstevel@tonic-gate hment_t * 4577c478bd9Sstevel@tonic-gate hment_walk(page_t *pp, htable_t **ht, uint_t *entry, hment_t *prev) 4587c478bd9Sstevel@tonic-gate { 4597c478bd9Sstevel@tonic-gate hment_t *hm; 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate if (pp->p_embed) { 4647c478bd9Sstevel@tonic-gate if (prev == NULL) { 4657c478bd9Sstevel@tonic-gate *ht = (htable_t *)pp->p_mapping; 4667c478bd9Sstevel@tonic-gate *entry = pp->p_mlentry; 4677c478bd9Sstevel@tonic-gate hm = HMENT_EMBEDDED; 4687c478bd9Sstevel@tonic-gate } else { 4697c478bd9Sstevel@tonic-gate ASSERT(prev == HMENT_EMBEDDED); 4707c478bd9Sstevel@tonic-gate hm = NULL; 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate } else { 4737c478bd9Sstevel@tonic-gate if (prev == NULL) { 4747c478bd9Sstevel@tonic-gate ASSERT(prev != HMENT_EMBEDDED); 4757c478bd9Sstevel@tonic-gate hm = (hment_t *)pp->p_mapping; 4767c478bd9Sstevel@tonic-gate } else { 4777c478bd9Sstevel@tonic-gate hm = prev->hm_next; 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate if (hm != NULL) { 4817c478bd9Sstevel@tonic-gate *ht = hm->hm_htable; 4827c478bd9Sstevel@tonic-gate *entry = hm->hm_entry; 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate return (hm); 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate /* 4897c478bd9Sstevel@tonic-gate * Remove a mapping to a page from its mapping list. Must have 4907c478bd9Sstevel@tonic-gate * the corresponding mapping list locked. 4917c478bd9Sstevel@tonic-gate * Finds the mapping list entry with the given pte_t and 4927c478bd9Sstevel@tonic-gate * unlinks it from the mapping list. 4937c478bd9Sstevel@tonic-gate */ 4947c478bd9Sstevel@tonic-gate hment_t * 4957c478bd9Sstevel@tonic-gate hment_remove(page_t *pp, htable_t *ht, uint_t entry) 4967c478bd9Sstevel@tonic-gate { 4977c478bd9Sstevel@tonic-gate hment_t *prev = NULL; 4987c478bd9Sstevel@tonic-gate hment_t *hm; 4997c478bd9Sstevel@tonic-gate uint_t idx; 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate ASSERT(x86_hm_held(pp)); 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate /* 5047c478bd9Sstevel@tonic-gate * Check if we have only one mapping embedded in the page_t. 5057c478bd9Sstevel@tonic-gate */ 5067c478bd9Sstevel@tonic-gate if (pp->p_embed) { 5077c478bd9Sstevel@tonic-gate ASSERT(ht == (htable_t *)pp->p_mapping); 5087c478bd9Sstevel@tonic-gate ASSERT(entry == pp->p_mlentry); 5097c478bd9Sstevel@tonic-gate ASSERT(pp->p_share == 0); 5107c478bd9Sstevel@tonic-gate pp->p_mapping = NULL; 5117c478bd9Sstevel@tonic-gate pp->p_mlentry = 0; 5127c478bd9Sstevel@tonic-gate pp->p_embed = 0; 5137c478bd9Sstevel@tonic-gate return (NULL); 5147c478bd9Sstevel@tonic-gate } 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate /* 5177c478bd9Sstevel@tonic-gate * Otherwise it must be in the list of hments. 5187c478bd9Sstevel@tonic-gate * Find the hment in the system-wide hash table and remove it. 5197c478bd9Sstevel@tonic-gate */ 5207c478bd9Sstevel@tonic-gate ASSERT(pp->p_share != 0); 5217c478bd9Sstevel@tonic-gate idx = HMENT_HASH(ht->ht_pfn, entry); 5227c478bd9Sstevel@tonic-gate mutex_enter(HASH_MUTEX(idx)); 5237c478bd9Sstevel@tonic-gate hm = hment_hash[idx]; 5247c478bd9Sstevel@tonic-gate while (hm && (hm->hm_htable != ht || hm->hm_entry != entry)) { 5257c478bd9Sstevel@tonic-gate prev = hm; 5267c478bd9Sstevel@tonic-gate hm = hm->hm_hashnext; 5277c478bd9Sstevel@tonic-gate } 528*aa2ed9e5Sjosephb if (hm == NULL) { 529*aa2ed9e5Sjosephb panic("hment_remove() missing in hash table pp=%lx, ht=%lx," 530*aa2ed9e5Sjosephb "entry=0x%x hash index=0x%x", (uintptr_t)pp, (uintptr_t)ht, 531*aa2ed9e5Sjosephb entry, idx); 532*aa2ed9e5Sjosephb } 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate if (prev) 5357c478bd9Sstevel@tonic-gate prev->hm_hashnext = hm->hm_hashnext; 5367c478bd9Sstevel@tonic-gate else 5377c478bd9Sstevel@tonic-gate hment_hash[idx] = hm->hm_hashnext; 5387c478bd9Sstevel@tonic-gate mutex_exit(HASH_MUTEX(idx)); 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate /* 5417c478bd9Sstevel@tonic-gate * Remove the hment from the page's mapping list 5427c478bd9Sstevel@tonic-gate */ 5437c478bd9Sstevel@tonic-gate if (hm->hm_next) 5447c478bd9Sstevel@tonic-gate hm->hm_next->hm_prev = hm->hm_prev; 5457c478bd9Sstevel@tonic-gate if (hm->hm_prev) 5467c478bd9Sstevel@tonic-gate hm->hm_prev->hm_next = hm->hm_next; 5477c478bd9Sstevel@tonic-gate else 5487c478bd9Sstevel@tonic-gate pp->p_mapping = hm->hm_next; 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate --pp->p_share; 5517c478bd9Sstevel@tonic-gate hm->hm_hashnext = NULL; 5527c478bd9Sstevel@tonic-gate hm->hm_next = NULL; 5537c478bd9Sstevel@tonic-gate hm->hm_prev = NULL; 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate return (hm); 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate /* 5597c478bd9Sstevel@tonic-gate * Put initial hment's in the reserve pool. 5607c478bd9Sstevel@tonic-gate */ 5617c478bd9Sstevel@tonic-gate void 5627c478bd9Sstevel@tonic-gate hment_reserve(uint_t count) 5637c478bd9Sstevel@tonic-gate { 5647c478bd9Sstevel@tonic-gate hment_t *hm; 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate count += hment_reserve_amount; 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate while (hment_reserve_count < count) { 5697c478bd9Sstevel@tonic-gate hm = kmem_cache_alloc(hment_cache, KM_NOSLEEP); 5707c478bd9Sstevel@tonic-gate if (hm == NULL) 5717c478bd9Sstevel@tonic-gate return; 5727c478bd9Sstevel@tonic-gate hment_put_reserve(hm); 5737c478bd9Sstevel@tonic-gate } 5747c478bd9Sstevel@tonic-gate } 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate /* 5777c478bd9Sstevel@tonic-gate * Readjust the hment reserves after they may have been used. 5787c478bd9Sstevel@tonic-gate */ 5797c478bd9Sstevel@tonic-gate void 5807c478bd9Sstevel@tonic-gate hment_adjust_reserve() 5817c478bd9Sstevel@tonic-gate { 5827c478bd9Sstevel@tonic-gate hment_t *hm; 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate /* 5857c478bd9Sstevel@tonic-gate * Free up any excess reserves 5867c478bd9Sstevel@tonic-gate */ 5877c478bd9Sstevel@tonic-gate while (hment_reserve_count > hment_reserve_amount) { 5887c478bd9Sstevel@tonic-gate ASSERT(curthread != hat_reserves_thread); 5897c478bd9Sstevel@tonic-gate hm = hment_get_reserve(); 5907c478bd9Sstevel@tonic-gate if (hm == NULL) 5917c478bd9Sstevel@tonic-gate return; 5927c478bd9Sstevel@tonic-gate hment_free(hm); 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate /* 5977c478bd9Sstevel@tonic-gate * initialize hment data structures 5987c478bd9Sstevel@tonic-gate */ 5997c478bd9Sstevel@tonic-gate void 6007c478bd9Sstevel@tonic-gate hment_init(void) 6017c478bd9Sstevel@tonic-gate { 6027c478bd9Sstevel@tonic-gate int i; 6037c478bd9Sstevel@tonic-gate int flags = KMC_NOHASH | KMC_NODEBUG; 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate /* 6067c478bd9Sstevel@tonic-gate * Initialize kmem caches. On 32 bit kernel's we shut off 6077c478bd9Sstevel@tonic-gate * debug information to save on precious kernel VA usage. 6087c478bd9Sstevel@tonic-gate */ 6097c478bd9Sstevel@tonic-gate hment_cache = kmem_cache_create("hment_t", 6107c478bd9Sstevel@tonic-gate sizeof (hment_t), 0, NULL, NULL, NULL, 6117c478bd9Sstevel@tonic-gate NULL, hat_memload_arena, flags); 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate hment_hash = kmem_zalloc(hment_hash_entries * sizeof (hment_t *), 6147c478bd9Sstevel@tonic-gate KM_SLEEP); 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate for (i = 0; i < MLIST_NUM_LOCK; i++) 6177c478bd9Sstevel@tonic-gate mutex_init(&mlist_lock[i], NULL, MUTEX_DEFAULT, NULL); 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate for (i = 0; i < HASH_NUM_LOCK; i++) 6207c478bd9Sstevel@tonic-gate mutex_init(&hash_lock[i], NULL, MUTEX_DEFAULT, NULL); 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate /* 6267c478bd9Sstevel@tonic-gate * return the number of mappings to a page 6277c478bd9Sstevel@tonic-gate * 6287c478bd9Sstevel@tonic-gate * Note there is no ASSERT() that the MUTEX is held for this. 6297c478bd9Sstevel@tonic-gate * Hence the return value might be inaccurate if this is called without 6307c478bd9Sstevel@tonic-gate * doing an x86_hm_enter(). 6317c478bd9Sstevel@tonic-gate */ 6327c478bd9Sstevel@tonic-gate uint_t 6337c478bd9Sstevel@tonic-gate hment_mapcnt(page_t *pp) 6347c478bd9Sstevel@tonic-gate { 6357c478bd9Sstevel@tonic-gate uint_t cnt; 6367c478bd9Sstevel@tonic-gate uint_t szc; 6377c478bd9Sstevel@tonic-gate page_t *larger; 6387c478bd9Sstevel@tonic-gate hment_t *hm; 6397c478bd9Sstevel@tonic-gate 6407c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 6417c478bd9Sstevel@tonic-gate if (pp->p_mapping == NULL) 6427c478bd9Sstevel@tonic-gate cnt = 0; 6437c478bd9Sstevel@tonic-gate else if (pp->p_embed) 6447c478bd9Sstevel@tonic-gate cnt = 1; 6457c478bd9Sstevel@tonic-gate else 6467c478bd9Sstevel@tonic-gate cnt = pp->p_share; 6477c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 6487c478bd9Sstevel@tonic-gate 6497c478bd9Sstevel@tonic-gate /* 6507c478bd9Sstevel@tonic-gate * walk through all larger mapping sizes counting mappings 6517c478bd9Sstevel@tonic-gate */ 6527c478bd9Sstevel@tonic-gate for (szc = 1; szc <= pp->p_szc; ++szc) { 6537c478bd9Sstevel@tonic-gate larger = PP_GROUPLEADER(pp, szc); 6547c478bd9Sstevel@tonic-gate if (larger == pp) /* don't double count large mappings */ 6557c478bd9Sstevel@tonic-gate continue; 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate x86_hm_enter(larger); 6587c478bd9Sstevel@tonic-gate if (larger->p_mapping != NULL) { 6597c478bd9Sstevel@tonic-gate if (larger->p_embed && 6607c478bd9Sstevel@tonic-gate ((htable_t *)larger->p_mapping)->ht_level == szc) { 6617c478bd9Sstevel@tonic-gate ++cnt; 6627c478bd9Sstevel@tonic-gate } else if (!larger->p_embed) { 6637c478bd9Sstevel@tonic-gate for (hm = larger->p_mapping; hm; 6647c478bd9Sstevel@tonic-gate hm = hm->hm_next) { 6657c478bd9Sstevel@tonic-gate if (hm->hm_htable->ht_level == szc) 6667c478bd9Sstevel@tonic-gate ++cnt; 6677c478bd9Sstevel@tonic-gate } 6687c478bd9Sstevel@tonic-gate } 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate x86_hm_exit(larger); 6717c478bd9Sstevel@tonic-gate } 6727c478bd9Sstevel@tonic-gate return (cnt); 6737c478bd9Sstevel@tonic-gate } 6747c478bd9Sstevel@tonic-gate 6757c478bd9Sstevel@tonic-gate /* 6767c478bd9Sstevel@tonic-gate * We need to steal an hment. Walk through all the page_t's until we 6777c478bd9Sstevel@tonic-gate * find one that has multiple mappings. Unload one of the mappings 6787c478bd9Sstevel@tonic-gate * and reclaim that hment. Note that we'll save/restart the starting 6797c478bd9Sstevel@tonic-gate * page to try and spread the pain. 6807c478bd9Sstevel@tonic-gate */ 6817c478bd9Sstevel@tonic-gate static page_t *last_page = NULL; 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate static hment_t * 6847c478bd9Sstevel@tonic-gate hment_steal(void) 6857c478bd9Sstevel@tonic-gate { 6867c478bd9Sstevel@tonic-gate page_t *last = last_page; 6877c478bd9Sstevel@tonic-gate page_t *pp = last; 6887c478bd9Sstevel@tonic-gate hment_t *hm = NULL; 6897c478bd9Sstevel@tonic-gate hment_t *hm2; 6907c478bd9Sstevel@tonic-gate htable_t *ht; 6917c478bd9Sstevel@tonic-gate uint_t found_one = 0; 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_steals); 6947c478bd9Sstevel@tonic-gate if (pp == NULL) 6957c478bd9Sstevel@tonic-gate last = pp = page_first(); 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate while (!found_one) { 6987c478bd9Sstevel@tonic-gate HATSTAT_INC(hs_hm_steal_exam); 6997c478bd9Sstevel@tonic-gate pp = page_next(pp); 7007c478bd9Sstevel@tonic-gate if (pp == NULL) 7017c478bd9Sstevel@tonic-gate pp = page_first(); 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate /* 7047c478bd9Sstevel@tonic-gate * The loop and function exit here if nothing found to steal. 7057c478bd9Sstevel@tonic-gate */ 7067c478bd9Sstevel@tonic-gate if (pp == last) 7077c478bd9Sstevel@tonic-gate return (NULL); 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate /* 7107c478bd9Sstevel@tonic-gate * Only lock the page_t if it has hments. 7117c478bd9Sstevel@tonic-gate */ 7127c478bd9Sstevel@tonic-gate if (pp->p_mapping == NULL || pp->p_embed) 7137c478bd9Sstevel@tonic-gate continue; 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate /* 7167c478bd9Sstevel@tonic-gate * Search the mapping list for a usable mapping. 7177c478bd9Sstevel@tonic-gate */ 7187c478bd9Sstevel@tonic-gate x86_hm_enter(pp); 7197c478bd9Sstevel@tonic-gate if (!pp->p_embed) { 7207c478bd9Sstevel@tonic-gate for (hm = pp->p_mapping; hm; hm = hm->hm_next) { 7217c478bd9Sstevel@tonic-gate ht = hm->hm_htable; 7227c478bd9Sstevel@tonic-gate if (ht->ht_hat != kas.a_hat && 7237c478bd9Sstevel@tonic-gate ht->ht_busy == 0 && 7247c478bd9Sstevel@tonic-gate ht->ht_lock_cnt == 0) { 7257c478bd9Sstevel@tonic-gate found_one = 1; 7267c478bd9Sstevel@tonic-gate break; 7277c478bd9Sstevel@tonic-gate } 7287c478bd9Sstevel@tonic-gate } 7297c478bd9Sstevel@tonic-gate } 7307c478bd9Sstevel@tonic-gate if (!found_one) 7317c478bd9Sstevel@tonic-gate x86_hm_exit(pp); 7327c478bd9Sstevel@tonic-gate } 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate /* 7357c478bd9Sstevel@tonic-gate * Steal the mapping we found. Note that hati_page_unmap() will 7367c478bd9Sstevel@tonic-gate * do the x86_hm_exit(). 7377c478bd9Sstevel@tonic-gate */ 7387c478bd9Sstevel@tonic-gate hm2 = hati_page_unmap(pp, ht, hm->hm_entry); 7397c478bd9Sstevel@tonic-gate ASSERT(hm2 == hm); 7407c478bd9Sstevel@tonic-gate last_page = pp; 7417c478bd9Sstevel@tonic-gate return (hm); 7427c478bd9Sstevel@tonic-gate } 743