10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51747Sjosephb * Common Development and Distribution License (the "License"). 61747Sjosephb * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 213446Smrj 220Sstevel@tonic-gate /* 233446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/sysmacros.h> 310Sstevel@tonic-gate #include <sys/kmem.h> 320Sstevel@tonic-gate #include <sys/atomic.h> 330Sstevel@tonic-gate #include <sys/bitmap.h> 340Sstevel@tonic-gate #include <sys/machparam.h> 350Sstevel@tonic-gate #include <sys/machsystm.h> 360Sstevel@tonic-gate #include <sys/mman.h> 370Sstevel@tonic-gate #include <sys/systm.h> 380Sstevel@tonic-gate #include <sys/cpuvar.h> 390Sstevel@tonic-gate #include <sys/thread.h> 400Sstevel@tonic-gate #include <sys/proc.h> 410Sstevel@tonic-gate #include <sys/cpu.h> 420Sstevel@tonic-gate #include <sys/kmem.h> 430Sstevel@tonic-gate #include <sys/disp.h> 440Sstevel@tonic-gate #include <sys/vmem.h> 450Sstevel@tonic-gate #include <sys/vmsystm.h> 460Sstevel@tonic-gate #include <sys/promif.h> 470Sstevel@tonic-gate #include <sys/var.h> 480Sstevel@tonic-gate #include <sys/x86_archext.h> 493446Smrj #include <sys/archsystm.h> 500Sstevel@tonic-gate #include <sys/bootconf.h> 510Sstevel@tonic-gate #include <sys/dumphdr.h> 520Sstevel@tonic-gate #include <vm/seg_kmem.h> 530Sstevel@tonic-gate #include <vm/seg_kpm.h> 540Sstevel@tonic-gate #include <vm/hat.h> 550Sstevel@tonic-gate #include <vm/hat_i86.h> 560Sstevel@tonic-gate #include <sys/cmn_err.h> 575084Sjohnlev #include <sys/panic.h> 585084Sjohnlev 595084Sjohnlev #ifdef __xpv 605084Sjohnlev #include <sys/hypervisor.h> 615084Sjohnlev #include <sys/xpv_panic.h> 625084Sjohnlev #endif 630Sstevel@tonic-gate 643446Smrj #include <sys/bootinfo.h> 653446Smrj #include <vm/kboot_mmu.h> 663446Smrj 673446Smrj static void x86pte_zero(htable_t *dest, uint_t entry, uint_t count); 683446Smrj 690Sstevel@tonic-gate kmem_cache_t *htable_cache; 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* 720Sstevel@tonic-gate * The variable htable_reserve_amount, rather than HTABLE_RESERVE_AMOUNT, 730Sstevel@tonic-gate * is used in order to facilitate testing of the htable_steal() code. 740Sstevel@tonic-gate * By resetting htable_reserve_amount to a lower value, we can force 750Sstevel@tonic-gate * stealing to occur. The reserve amount is a guess to get us through boot. 760Sstevel@tonic-gate */ 770Sstevel@tonic-gate #define HTABLE_RESERVE_AMOUNT (200) 780Sstevel@tonic-gate uint_t htable_reserve_amount = HTABLE_RESERVE_AMOUNT; 790Sstevel@tonic-gate kmutex_t htable_reserve_mutex; 800Sstevel@tonic-gate uint_t htable_reserve_cnt; 810Sstevel@tonic-gate htable_t *htable_reserve_pool; 820Sstevel@tonic-gate 830Sstevel@tonic-gate /* 841747Sjosephb * Used to hand test htable_steal(). 850Sstevel@tonic-gate */ 861747Sjosephb #ifdef DEBUG 871747Sjosephb ulong_t force_steal = 0; 881747Sjosephb ulong_t ptable_cnt = 0; 891747Sjosephb #endif 901747Sjosephb 911747Sjosephb /* 921747Sjosephb * This variable is so that we can tune this via /etc/system 931747Sjosephb * Any value works, but a power of two <= mmu.ptes_per_table is best. 941747Sjosephb */ 951747Sjosephb uint_t htable_steal_passes = 8; 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* 980Sstevel@tonic-gate * mutex stuff for access to htable hash 990Sstevel@tonic-gate */ 1000Sstevel@tonic-gate #define NUM_HTABLE_MUTEX 128 1010Sstevel@tonic-gate kmutex_t htable_mutex[NUM_HTABLE_MUTEX]; 1020Sstevel@tonic-gate #define HTABLE_MUTEX_HASH(h) ((h) & (NUM_HTABLE_MUTEX - 1)) 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate #define HTABLE_ENTER(h) mutex_enter(&htable_mutex[HTABLE_MUTEX_HASH(h)]); 1050Sstevel@tonic-gate #define HTABLE_EXIT(h) mutex_exit(&htable_mutex[HTABLE_MUTEX_HASH(h)]); 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate /* 1080Sstevel@tonic-gate * forward declarations 1090Sstevel@tonic-gate */ 1100Sstevel@tonic-gate static void link_ptp(htable_t *higher, htable_t *new, uintptr_t vaddr); 1110Sstevel@tonic-gate static void unlink_ptp(htable_t *higher, htable_t *old, uintptr_t vaddr); 1120Sstevel@tonic-gate static void htable_free(htable_t *ht); 1133446Smrj static x86pte_t *x86pte_access_pagetable(htable_t *ht, uint_t index); 1140Sstevel@tonic-gate static void x86pte_release_pagetable(htable_t *ht); 1150Sstevel@tonic-gate static x86pte_t x86pte_cas(htable_t *ht, uint_t entry, x86pte_t old, 1160Sstevel@tonic-gate x86pte_t new); 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* 1190Sstevel@tonic-gate * A counter to track if we are stealing or reaping htables. When non-zero 1200Sstevel@tonic-gate * htable_free() will directly free htables (either to the reserve or kmem) 1210Sstevel@tonic-gate * instead of putting them in a hat's htable cache. 1220Sstevel@tonic-gate */ 1230Sstevel@tonic-gate uint32_t htable_dont_cache = 0; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate /* 1260Sstevel@tonic-gate * Track the number of active pagetables, so we can know how many to reap 1270Sstevel@tonic-gate */ 1280Sstevel@tonic-gate static uint32_t active_ptables = 0; 1290Sstevel@tonic-gate 1305084Sjohnlev #ifdef __xpv 1315084Sjohnlev /* 1325084Sjohnlev * Deal with hypervisor complications. 1335084Sjohnlev */ 1345084Sjohnlev void 1355084Sjohnlev xen_flush_va(caddr_t va) 1365084Sjohnlev { 1375084Sjohnlev struct mmuext_op t; 1385084Sjohnlev uint_t count; 1395084Sjohnlev 1405084Sjohnlev if (IN_XPV_PANIC()) { 1415084Sjohnlev mmu_tlbflush_entry((caddr_t)va); 1425084Sjohnlev } else { 1435084Sjohnlev t.cmd = MMUEXT_INVLPG_LOCAL; 1445084Sjohnlev t.arg1.linear_addr = (uintptr_t)va; 1455084Sjohnlev if (HYPERVISOR_mmuext_op(&t, 1, &count, DOMID_SELF) < 0) 1465084Sjohnlev panic("HYPERVISOR_mmuext_op() failed"); 1475084Sjohnlev ASSERT(count == 1); 1485084Sjohnlev } 1495084Sjohnlev } 1505084Sjohnlev 1515084Sjohnlev void 1525084Sjohnlev xen_gflush_va(caddr_t va, cpuset_t cpus) 1535084Sjohnlev { 1545084Sjohnlev struct mmuext_op t; 1555084Sjohnlev uint_t count; 1565084Sjohnlev 1575084Sjohnlev if (IN_XPV_PANIC()) { 1585084Sjohnlev mmu_tlbflush_entry((caddr_t)va); 1595084Sjohnlev return; 1605084Sjohnlev } 1615084Sjohnlev 1625084Sjohnlev t.cmd = MMUEXT_INVLPG_MULTI; 1635084Sjohnlev t.arg1.linear_addr = (uintptr_t)va; 1645084Sjohnlev /*LINTED: constant in conditional context*/ 1655084Sjohnlev set_xen_guest_handle(t.arg2.vcpumask, &cpus); 1665084Sjohnlev if (HYPERVISOR_mmuext_op(&t, 1, &count, DOMID_SELF) < 0) 1675084Sjohnlev panic("HYPERVISOR_mmuext_op() failed"); 1685084Sjohnlev ASSERT(count == 1); 1695084Sjohnlev } 1705084Sjohnlev 1715084Sjohnlev void 1725084Sjohnlev xen_flush_tlb() 1735084Sjohnlev { 1745084Sjohnlev struct mmuext_op t; 1755084Sjohnlev uint_t count; 1765084Sjohnlev 1775084Sjohnlev if (IN_XPV_PANIC()) { 1785084Sjohnlev xpv_panic_reload_cr3(); 1795084Sjohnlev } else { 1805084Sjohnlev t.cmd = MMUEXT_TLB_FLUSH_LOCAL; 1815084Sjohnlev if (HYPERVISOR_mmuext_op(&t, 1, &count, DOMID_SELF) < 0) 1825084Sjohnlev panic("HYPERVISOR_mmuext_op() failed"); 1835084Sjohnlev ASSERT(count == 1); 1845084Sjohnlev } 1855084Sjohnlev } 1865084Sjohnlev 1875084Sjohnlev void 1885084Sjohnlev xen_gflush_tlb(cpuset_t cpus) 1895084Sjohnlev { 1905084Sjohnlev struct mmuext_op t; 1915084Sjohnlev uint_t count; 1925084Sjohnlev 1935084Sjohnlev ASSERT(!IN_XPV_PANIC()); 1945084Sjohnlev t.cmd = MMUEXT_TLB_FLUSH_MULTI; 1955084Sjohnlev /*LINTED: constant in conditional context*/ 1965084Sjohnlev set_xen_guest_handle(t.arg2.vcpumask, &cpus); 1975084Sjohnlev if (HYPERVISOR_mmuext_op(&t, 1, &count, DOMID_SELF) < 0) 1985084Sjohnlev panic("HYPERVISOR_mmuext_op() failed"); 1995084Sjohnlev ASSERT(count == 1); 2005084Sjohnlev } 2015084Sjohnlev 2025084Sjohnlev /* 2035084Sjohnlev * Install/Adjust a kpm mapping under the hypervisor. 2045084Sjohnlev * Value of "how" should be: 2055084Sjohnlev * PT_WRITABLE | PT_VALID - regular kpm mapping 2065084Sjohnlev * PT_VALID - make mapping read-only 2075084Sjohnlev * 0 - remove mapping 2085084Sjohnlev * 2095084Sjohnlev * returns 0 on success. non-zero for failure. 2105084Sjohnlev */ 2115084Sjohnlev int 2125084Sjohnlev xen_kpm_page(pfn_t pfn, uint_t how) 2135084Sjohnlev { 2145084Sjohnlev paddr_t pa = mmu_ptob((paddr_t)pfn); 2155084Sjohnlev x86pte_t pte = PT_NOCONSIST | PT_REF | PT_MOD; 2165084Sjohnlev 2175084Sjohnlev if (kpm_vbase == NULL) 2185084Sjohnlev return (0); 2195084Sjohnlev 2205084Sjohnlev if (how) 2215084Sjohnlev pte |= pa_to_ma(pa) | how; 2225084Sjohnlev else 2235084Sjohnlev pte = 0; 2245084Sjohnlev return (HYPERVISOR_update_va_mapping((uintptr_t)kpm_vbase + pa, 2255084Sjohnlev pte, UVMF_INVLPG | UVMF_ALL)); 2265084Sjohnlev } 2275084Sjohnlev 2285084Sjohnlev void 2295084Sjohnlev xen_pin(pfn_t pfn, level_t lvl) 2305084Sjohnlev { 2315084Sjohnlev struct mmuext_op t; 2325084Sjohnlev uint_t count; 2335084Sjohnlev 2345084Sjohnlev t.cmd = MMUEXT_PIN_L1_TABLE + lvl; 2355084Sjohnlev t.arg1.mfn = pfn_to_mfn(pfn); 2365084Sjohnlev if (HYPERVISOR_mmuext_op(&t, 1, &count, DOMID_SELF) < 0) 2375084Sjohnlev panic("HYPERVISOR_mmuext_op() failed"); 2385084Sjohnlev ASSERT(count == 1); 2395084Sjohnlev } 2405084Sjohnlev 2415084Sjohnlev void 2425084Sjohnlev xen_unpin(pfn_t pfn) 2435084Sjohnlev { 2445084Sjohnlev struct mmuext_op t; 2455084Sjohnlev uint_t count; 2465084Sjohnlev 2475084Sjohnlev t.cmd = MMUEXT_UNPIN_TABLE; 2485084Sjohnlev t.arg1.mfn = pfn_to_mfn(pfn); 2495084Sjohnlev if (HYPERVISOR_mmuext_op(&t, 1, &count, DOMID_SELF) < 0) 2505084Sjohnlev panic("HYPERVISOR_mmuext_op() failed"); 2515084Sjohnlev ASSERT(count == 1); 2525084Sjohnlev } 2535084Sjohnlev 2545084Sjohnlev static void 2555084Sjohnlev xen_map(uint64_t pte, caddr_t va) 2565084Sjohnlev { 2575084Sjohnlev if (HYPERVISOR_update_va_mapping((uintptr_t)va, pte, 2585084Sjohnlev UVMF_INVLPG | UVMF_LOCAL)) 2595084Sjohnlev panic("HYPERVISOR_update_va_mapping() failed"); 2605084Sjohnlev } 2615084Sjohnlev #endif /* __xpv */ 2625084Sjohnlev 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * Allocate a memory page for a hardware page table. 2650Sstevel@tonic-gate * 2663446Smrj * A wrapper around page_get_physical(), with some extra checks. 2670Sstevel@tonic-gate */ 2683446Smrj static pfn_t 2693446Smrj ptable_alloc(uintptr_t seed) 2700Sstevel@tonic-gate { 2710Sstevel@tonic-gate pfn_t pfn; 2720Sstevel@tonic-gate page_t *pp; 2730Sstevel@tonic-gate 2743446Smrj pfn = PFN_INVALID; 2750Sstevel@tonic-gate atomic_add_32(&active_ptables, 1); 2760Sstevel@tonic-gate 2773446Smrj /* 2783446Smrj * The first check is to see if there is memory in the system. If we 2793446Smrj * drop to throttlefree, then fail the ptable_alloc() and let the 2803446Smrj * stealing code kick in. Note that we have to do this test here, 2813446Smrj * since the test in page_create_throttle() would let the NOSLEEP 2823446Smrj * allocation go through and deplete the page reserves. 2833446Smrj * 2843446Smrj * The !NOMEMWAIT() lets pageout, fsflush, etc. skip this check. 2853446Smrj */ 2863446Smrj if (!NOMEMWAIT() && freemem <= throttlefree + 1) 2873446Smrj return (PFN_INVALID); 2880Sstevel@tonic-gate 2891747Sjosephb #ifdef DEBUG 2903446Smrj /* 2913446Smrj * This code makes htable_steal() easier to test. By setting 2923446Smrj * force_steal we force pagetable allocations to fall 2933446Smrj * into the stealing code. Roughly 1 in ever "force_steal" 2943446Smrj * page table allocations will fail. 2953446Smrj */ 2963446Smrj if (proc_pageout != NULL && force_steal > 1 && 2973446Smrj ++ptable_cnt > force_steal) { 2983446Smrj ptable_cnt = 0; 2993446Smrj return (PFN_INVALID); 3003446Smrj } 3011747Sjosephb #endif /* DEBUG */ 3021747Sjosephb 3033446Smrj pp = page_get_physical(seed); 3043446Smrj if (pp == NULL) 3053446Smrj return (PFN_INVALID); 3063446Smrj pfn = pp->p_pagenum; 3070Sstevel@tonic-gate page_downgrade(pp); 3080Sstevel@tonic-gate ASSERT(PAGE_SHARED(pp)); 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate if (pfn == PFN_INVALID) 3110Sstevel@tonic-gate panic("ptable_alloc(): Invalid PFN!!"); 3121747Sjosephb HATSTAT_INC(hs_ptable_allocs); 3133446Smrj return (pfn); 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate /* 3170Sstevel@tonic-gate * Free an htable's associated page table page. See the comments 3180Sstevel@tonic-gate * for ptable_alloc(). 3190Sstevel@tonic-gate */ 3200Sstevel@tonic-gate static void 3213446Smrj ptable_free(pfn_t pfn) 3220Sstevel@tonic-gate { 3233446Smrj page_t *pp = page_numtopp_nolock(pfn); 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * need to destroy the page used for the pagetable 3270Sstevel@tonic-gate */ 3280Sstevel@tonic-gate ASSERT(pfn != PFN_INVALID); 3290Sstevel@tonic-gate HATSTAT_INC(hs_ptable_frees); 3300Sstevel@tonic-gate atomic_add_32(&active_ptables, -1); 3310Sstevel@tonic-gate if (pp == NULL) 3320Sstevel@tonic-gate panic("ptable_free(): no page for pfn!"); 3330Sstevel@tonic-gate ASSERT(PAGE_SHARED(pp)); 3340Sstevel@tonic-gate ASSERT(pfn == pp->p_pagenum); 3355084Sjohnlev ASSERT(!IN_XPV_PANIC()); 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* 3380Sstevel@tonic-gate * Get an exclusive lock, might have to wait for a kmem reader. 3390Sstevel@tonic-gate */ 3400Sstevel@tonic-gate if (!page_tryupgrade(pp)) { 3410Sstevel@tonic-gate page_unlock(pp); 3420Sstevel@tonic-gate /* 3430Sstevel@tonic-gate * RFE: we could change this to not loop forever 3440Sstevel@tonic-gate * George Cameron had some idea on how to do that. 3450Sstevel@tonic-gate * For now looping works - it's just like sfmmu. 3460Sstevel@tonic-gate */ 3470Sstevel@tonic-gate while (!page_lock(pp, SE_EXCL, (kmutex_t *)NULL, P_RECLAIM)) 3480Sstevel@tonic-gate continue; 3490Sstevel@tonic-gate } 3505084Sjohnlev #ifdef __xpv 3515084Sjohnlev if (kpm_vbase && xen_kpm_page(pfn, PT_VALID | PT_WRITABLE) < 0) 3525084Sjohnlev panic("failure making kpm r/w pfn=0x%lx", pfn); 3535084Sjohnlev #endif 3540Sstevel@tonic-gate page_free(pp, 1); 3550Sstevel@tonic-gate page_unresv(1); 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* 3590Sstevel@tonic-gate * Put one htable on the reserve list. 3600Sstevel@tonic-gate */ 3610Sstevel@tonic-gate static void 3620Sstevel@tonic-gate htable_put_reserve(htable_t *ht) 3630Sstevel@tonic-gate { 3640Sstevel@tonic-gate ht->ht_hat = NULL; /* no longer tied to a hat */ 3650Sstevel@tonic-gate ASSERT(ht->ht_pfn == PFN_INVALID); 3660Sstevel@tonic-gate HATSTAT_INC(hs_htable_rputs); 3670Sstevel@tonic-gate mutex_enter(&htable_reserve_mutex); 3680Sstevel@tonic-gate ht->ht_next = htable_reserve_pool; 3690Sstevel@tonic-gate htable_reserve_pool = ht; 3700Sstevel@tonic-gate ++htable_reserve_cnt; 3710Sstevel@tonic-gate mutex_exit(&htable_reserve_mutex); 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate /* 3750Sstevel@tonic-gate * Take one htable from the reserve. 3760Sstevel@tonic-gate */ 3770Sstevel@tonic-gate static htable_t * 3780Sstevel@tonic-gate htable_get_reserve(void) 3790Sstevel@tonic-gate { 3800Sstevel@tonic-gate htable_t *ht = NULL; 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate mutex_enter(&htable_reserve_mutex); 3830Sstevel@tonic-gate if (htable_reserve_cnt != 0) { 3840Sstevel@tonic-gate ht = htable_reserve_pool; 3850Sstevel@tonic-gate ASSERT(ht != NULL); 3860Sstevel@tonic-gate ASSERT(ht->ht_pfn == PFN_INVALID); 3870Sstevel@tonic-gate htable_reserve_pool = ht->ht_next; 3880Sstevel@tonic-gate --htable_reserve_cnt; 3890Sstevel@tonic-gate HATSTAT_INC(hs_htable_rgets); 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate mutex_exit(&htable_reserve_mutex); 3920Sstevel@tonic-gate return (ht); 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate /* 3963446Smrj * Allocate initial htables and put them on the reserve list 3970Sstevel@tonic-gate */ 3980Sstevel@tonic-gate void 3990Sstevel@tonic-gate htable_initial_reserve(uint_t count) 4000Sstevel@tonic-gate { 4010Sstevel@tonic-gate htable_t *ht; 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate count += HTABLE_RESERVE_AMOUNT; 4040Sstevel@tonic-gate while (count > 0) { 4050Sstevel@tonic-gate ht = kmem_cache_alloc(htable_cache, KM_NOSLEEP); 4060Sstevel@tonic-gate ASSERT(ht != NULL); 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate ASSERT(use_boot_reserve); 4093446Smrj ht->ht_pfn = PFN_INVALID; 4103446Smrj htable_put_reserve(ht); 4110Sstevel@tonic-gate --count; 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate /* 4160Sstevel@tonic-gate * Readjust the reserves after a thread finishes using them. 4170Sstevel@tonic-gate */ 4180Sstevel@tonic-gate void 4190Sstevel@tonic-gate htable_adjust_reserve() 4200Sstevel@tonic-gate { 4210Sstevel@tonic-gate htable_t *ht; 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate /* 4240Sstevel@tonic-gate * Free any excess htables in the reserve list 4250Sstevel@tonic-gate */ 4264004Sjosephb while (htable_reserve_cnt > htable_reserve_amount && 4274004Sjosephb !USE_HAT_RESERVES()) { 4280Sstevel@tonic-gate ht = htable_get_reserve(); 4290Sstevel@tonic-gate if (ht == NULL) 4300Sstevel@tonic-gate return; 4310Sstevel@tonic-gate ASSERT(ht->ht_pfn == PFN_INVALID); 4320Sstevel@tonic-gate kmem_cache_free(htable_cache, ht); 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate /* 4380Sstevel@tonic-gate * This routine steals htables from user processes for htable_alloc() or 4390Sstevel@tonic-gate * for htable_reap(). 4400Sstevel@tonic-gate */ 4410Sstevel@tonic-gate static htable_t * 4420Sstevel@tonic-gate htable_steal(uint_t cnt) 4430Sstevel@tonic-gate { 4440Sstevel@tonic-gate hat_t *hat = kas.a_hat; /* list starts with khat */ 4450Sstevel@tonic-gate htable_t *list = NULL; 4460Sstevel@tonic-gate htable_t *ht; 4470Sstevel@tonic-gate htable_t *higher; 4480Sstevel@tonic-gate uint_t h; 4491747Sjosephb uint_t h_start; 4501747Sjosephb static uint_t h_seed = 0; 4510Sstevel@tonic-gate uint_t e; 4520Sstevel@tonic-gate uintptr_t va; 4530Sstevel@tonic-gate x86pte_t pte; 4540Sstevel@tonic-gate uint_t stolen = 0; 4550Sstevel@tonic-gate uint_t pass; 4561747Sjosephb uint_t threshold; 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate /* 4590Sstevel@tonic-gate * Limit htable_steal_passes to something reasonable 4600Sstevel@tonic-gate */ 4610Sstevel@tonic-gate if (htable_steal_passes == 0) 4620Sstevel@tonic-gate htable_steal_passes = 1; 4630Sstevel@tonic-gate if (htable_steal_passes > mmu.ptes_per_table) 4640Sstevel@tonic-gate htable_steal_passes = mmu.ptes_per_table; 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate /* 4671747Sjosephb * Loop through all user hats. The 1st pass takes cached htables that 4680Sstevel@tonic-gate * aren't in use. The later passes steal by removing mappings, too. 4690Sstevel@tonic-gate */ 4700Sstevel@tonic-gate atomic_add_32(&htable_dont_cache, 1); 4711747Sjosephb for (pass = 0; pass <= htable_steal_passes && stolen < cnt; ++pass) { 4721747Sjosephb threshold = pass * mmu.ptes_per_table / htable_steal_passes; 4731747Sjosephb hat = kas.a_hat; 4740Sstevel@tonic-gate for (;;) { 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate /* 4771747Sjosephb * Clear the victim flag and move to next hat 4780Sstevel@tonic-gate */ 4790Sstevel@tonic-gate mutex_enter(&hat_list_lock); 4801747Sjosephb if (hat != kas.a_hat) { 4811747Sjosephb hat->hat_flags &= ~HAT_VICTIM; 4821747Sjosephb cv_broadcast(&hat_list_cv); 4831747Sjosephb } 4841747Sjosephb hat = hat->hat_next; 4851747Sjosephb 4861747Sjosephb /* 4871747Sjosephb * Skip any hat that is already being stolen from. 4881747Sjosephb * 4891747Sjosephb * We skip SHARED hats, as these are dummy 4901747Sjosephb * hats that host ISM shared page tables. 4911747Sjosephb * 4921747Sjosephb * We also skip if HAT_FREEING because hat_pte_unmap() 4931747Sjosephb * won't zero out the PTE's. That would lead to hitting 4941747Sjosephb * stale PTEs either here or under hat_unload() when we 4951747Sjosephb * steal and unload the same page table in competing 4961747Sjosephb * threads. 4971747Sjosephb */ 4981747Sjosephb while (hat != NULL && 4991747Sjosephb (hat->hat_flags & 5001747Sjosephb (HAT_VICTIM | HAT_SHARED | HAT_FREEING)) != 0) 5011747Sjosephb hat = hat->hat_next; 5021747Sjosephb 5031747Sjosephb if (hat == NULL) { 5040Sstevel@tonic-gate mutex_exit(&hat_list_lock); 5050Sstevel@tonic-gate break; 5060Sstevel@tonic-gate } 5071747Sjosephb 5081747Sjosephb /* 5091747Sjosephb * Are we finished? 5101747Sjosephb */ 5111747Sjosephb if (stolen == cnt) { 5121747Sjosephb /* 5131747Sjosephb * Try to spread the pain of stealing, 5141747Sjosephb * move victim HAT to the end of the HAT list. 5151747Sjosephb */ 5161747Sjosephb if (pass >= 1 && cnt == 1 && 5171747Sjosephb kas.a_hat->hat_prev != hat) { 5181747Sjosephb 5191747Sjosephb /* unlink victim hat */ 5201747Sjosephb if (hat->hat_prev) 5211747Sjosephb hat->hat_prev->hat_next = 5221747Sjosephb hat->hat_next; 5231747Sjosephb else 5241747Sjosephb kas.a_hat->hat_next = 5251747Sjosephb hat->hat_next; 5261747Sjosephb if (hat->hat_next) 5271747Sjosephb hat->hat_next->hat_prev = 5281747Sjosephb hat->hat_prev; 5291747Sjosephb else 5301747Sjosephb kas.a_hat->hat_prev = 5311747Sjosephb hat->hat_prev; 5321747Sjosephb 5331747Sjosephb 5341747Sjosephb /* relink at end of hat list */ 5351747Sjosephb hat->hat_next = NULL; 5361747Sjosephb hat->hat_prev = kas.a_hat->hat_prev; 5371747Sjosephb if (hat->hat_prev) 5381747Sjosephb hat->hat_prev->hat_next = hat; 5391747Sjosephb else 5401747Sjosephb kas.a_hat->hat_next = hat; 5411747Sjosephb kas.a_hat->hat_prev = hat; 5421747Sjosephb 5431747Sjosephb } 5441747Sjosephb 5451747Sjosephb mutex_exit(&hat_list_lock); 5461747Sjosephb break; 5471747Sjosephb } 5481747Sjosephb 5491747Sjosephb /* 5501747Sjosephb * Mark the HAT as a stealing victim. 5511747Sjosephb */ 5520Sstevel@tonic-gate hat->hat_flags |= HAT_VICTIM; 5530Sstevel@tonic-gate mutex_exit(&hat_list_lock); 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate /* 5560Sstevel@tonic-gate * Take any htables from the hat's cached "free" list. 5570Sstevel@tonic-gate */ 5580Sstevel@tonic-gate hat_enter(hat); 5590Sstevel@tonic-gate while ((ht = hat->hat_ht_cached) != NULL && 5600Sstevel@tonic-gate stolen < cnt) { 5610Sstevel@tonic-gate hat->hat_ht_cached = ht->ht_next; 5620Sstevel@tonic-gate ht->ht_next = list; 5630Sstevel@tonic-gate list = ht; 5640Sstevel@tonic-gate ++stolen; 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate hat_exit(hat); 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate /* 5690Sstevel@tonic-gate * Don't steal on first pass. 5700Sstevel@tonic-gate */ 5711747Sjosephb if (pass == 0 || stolen == cnt) 5720Sstevel@tonic-gate continue; 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate /* 5751747Sjosephb * Search the active htables for one to steal. 5761747Sjosephb * Start at a different hash bucket every time to 5771747Sjosephb * help spread the pain of stealing. 5780Sstevel@tonic-gate */ 5791747Sjosephb h = h_start = h_seed++ % hat->hat_num_hash; 5801747Sjosephb do { 5810Sstevel@tonic-gate higher = NULL; 5820Sstevel@tonic-gate HTABLE_ENTER(h); 5830Sstevel@tonic-gate for (ht = hat->hat_ht_hash[h]; ht; 5840Sstevel@tonic-gate ht = ht->ht_next) { 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate /* 5870Sstevel@tonic-gate * Can we rule out reaping? 5880Sstevel@tonic-gate */ 5890Sstevel@tonic-gate if (ht->ht_busy != 0 || 5900Sstevel@tonic-gate (ht->ht_flags & HTABLE_SHARED_PFN)|| 5911747Sjosephb ht->ht_level > 0 || 5921747Sjosephb ht->ht_valid_cnt > threshold || 5930Sstevel@tonic-gate ht->ht_lock_cnt != 0) 5940Sstevel@tonic-gate continue; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate /* 5970Sstevel@tonic-gate * Increment busy so the htable can't 5980Sstevel@tonic-gate * disappear. We drop the htable mutex 5990Sstevel@tonic-gate * to avoid deadlocks with 6000Sstevel@tonic-gate * hat_pageunload() and the hment mutex 6010Sstevel@tonic-gate * while we call hat_pte_unmap() 6020Sstevel@tonic-gate */ 6030Sstevel@tonic-gate ++ht->ht_busy; 6040Sstevel@tonic-gate HTABLE_EXIT(h); 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate /* 6070Sstevel@tonic-gate * Try stealing. 6080Sstevel@tonic-gate * - unload and invalidate all PTEs 6090Sstevel@tonic-gate */ 6100Sstevel@tonic-gate for (e = 0, va = ht->ht_vaddr; 6113446Smrj e < HTABLE_NUM_PTES(ht) && 6120Sstevel@tonic-gate ht->ht_valid_cnt > 0 && 6130Sstevel@tonic-gate ht->ht_busy == 1 && 6140Sstevel@tonic-gate ht->ht_lock_cnt == 0; 6150Sstevel@tonic-gate ++e, va += MMU_PAGESIZE) { 6160Sstevel@tonic-gate pte = x86pte_get(ht, e); 6170Sstevel@tonic-gate if (!PTE_ISVALID(pte)) 6180Sstevel@tonic-gate continue; 6190Sstevel@tonic-gate hat_pte_unmap(ht, e, 6200Sstevel@tonic-gate HAT_UNLOAD, pte, NULL); 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate /* 6240Sstevel@tonic-gate * Reacquire htable lock. If we didn't 6250Sstevel@tonic-gate * remove all mappings in the table, 6260Sstevel@tonic-gate * or another thread added a new mapping 6270Sstevel@tonic-gate * behind us, give up on this table. 6280Sstevel@tonic-gate */ 6290Sstevel@tonic-gate HTABLE_ENTER(h); 6300Sstevel@tonic-gate if (ht->ht_busy != 1 || 6310Sstevel@tonic-gate ht->ht_valid_cnt != 0 || 6320Sstevel@tonic-gate ht->ht_lock_cnt != 0) { 6330Sstevel@tonic-gate --ht->ht_busy; 6340Sstevel@tonic-gate continue; 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate /* 6380Sstevel@tonic-gate * Steal it and unlink the page table. 6390Sstevel@tonic-gate */ 6400Sstevel@tonic-gate higher = ht->ht_parent; 6410Sstevel@tonic-gate unlink_ptp(higher, ht, ht->ht_vaddr); 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate /* 6440Sstevel@tonic-gate * remove from the hash list 6450Sstevel@tonic-gate */ 6460Sstevel@tonic-gate if (ht->ht_next) 6470Sstevel@tonic-gate ht->ht_next->ht_prev = 6480Sstevel@tonic-gate ht->ht_prev; 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate if (ht->ht_prev) { 6510Sstevel@tonic-gate ht->ht_prev->ht_next = 6520Sstevel@tonic-gate ht->ht_next; 6530Sstevel@tonic-gate } else { 6540Sstevel@tonic-gate ASSERT(hat->hat_ht_hash[h] == 6550Sstevel@tonic-gate ht); 6560Sstevel@tonic-gate hat->hat_ht_hash[h] = 6570Sstevel@tonic-gate ht->ht_next; 6580Sstevel@tonic-gate } 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate /* 6610Sstevel@tonic-gate * Break to outer loop to release the 6623446Smrj * higher (ht_parent) pagetable. This 6630Sstevel@tonic-gate * spreads out the pain caused by 6640Sstevel@tonic-gate * pagefaults. 6650Sstevel@tonic-gate */ 6660Sstevel@tonic-gate ht->ht_next = list; 6670Sstevel@tonic-gate list = ht; 6680Sstevel@tonic-gate ++stolen; 6690Sstevel@tonic-gate break; 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate HTABLE_EXIT(h); 6720Sstevel@tonic-gate if (higher != NULL) 6730Sstevel@tonic-gate htable_release(higher); 6741747Sjosephb if (++h == hat->hat_num_hash) 6751747Sjosephb h = 0; 6761747Sjosephb } while (stolen < cnt && h != h_start); 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate atomic_add_32(&htable_dont_cache, -1); 6800Sstevel@tonic-gate return (list); 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate /* 6850Sstevel@tonic-gate * This is invoked from kmem when the system is low on memory. We try 6860Sstevel@tonic-gate * to free hments, htables, and ptables to improve the memory situation. 6870Sstevel@tonic-gate */ 6880Sstevel@tonic-gate /*ARGSUSED*/ 6890Sstevel@tonic-gate static void 6900Sstevel@tonic-gate htable_reap(void *handle) 6910Sstevel@tonic-gate { 6920Sstevel@tonic-gate uint_t reap_cnt; 6930Sstevel@tonic-gate htable_t *list; 6940Sstevel@tonic-gate htable_t *ht; 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate HATSTAT_INC(hs_reap_attempts); 6970Sstevel@tonic-gate if (!can_steal_post_boot) 6980Sstevel@tonic-gate return; 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate /* 7010Sstevel@tonic-gate * Try to reap 5% of the page tables bounded by a maximum of 7020Sstevel@tonic-gate * 5% of physmem and a minimum of 10. 7030Sstevel@tonic-gate */ 7040Sstevel@tonic-gate reap_cnt = MIN(MAX(physmem / 20, active_ptables / 20), 10); 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate /* 7070Sstevel@tonic-gate * Let htable_steal() do the work, we just call htable_free() 7080Sstevel@tonic-gate */ 7095084Sjohnlev XPV_DISALLOW_MIGRATE(); 7100Sstevel@tonic-gate list = htable_steal(reap_cnt); 7115084Sjohnlev XPV_ALLOW_MIGRATE(); 7120Sstevel@tonic-gate while ((ht = list) != NULL) { 7130Sstevel@tonic-gate list = ht->ht_next; 7140Sstevel@tonic-gate HATSTAT_INC(hs_reaped); 7150Sstevel@tonic-gate htable_free(ht); 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate /* 7190Sstevel@tonic-gate * Free up excess reserves 7200Sstevel@tonic-gate */ 7210Sstevel@tonic-gate htable_adjust_reserve(); 7220Sstevel@tonic-gate hment_adjust_reserve(); 7230Sstevel@tonic-gate } 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate /* 7263446Smrj * Allocate an htable, stealing one or using the reserve if necessary 7270Sstevel@tonic-gate */ 7280Sstevel@tonic-gate static htable_t * 7290Sstevel@tonic-gate htable_alloc( 7300Sstevel@tonic-gate hat_t *hat, 7310Sstevel@tonic-gate uintptr_t vaddr, 7320Sstevel@tonic-gate level_t level, 7330Sstevel@tonic-gate htable_t *shared) 7340Sstevel@tonic-gate { 7350Sstevel@tonic-gate htable_t *ht = NULL; 7360Sstevel@tonic-gate uint_t is_vlp; 7370Sstevel@tonic-gate uint_t is_bare = 0; 7380Sstevel@tonic-gate uint_t need_to_zero = 1; 7390Sstevel@tonic-gate int kmflags = (can_steal_post_boot ? KM_NOSLEEP : KM_SLEEP); 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate if (level < 0 || level > TOP_LEVEL(hat)) 7420Sstevel@tonic-gate panic("htable_alloc(): level %d out of range\n", level); 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate is_vlp = (hat->hat_flags & HAT_VLP) && level == VLP_LEVEL; 7450Sstevel@tonic-gate if (is_vlp || shared != NULL) 7460Sstevel@tonic-gate is_bare = 1; 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate /* 7490Sstevel@tonic-gate * First reuse a cached htable from the hat_ht_cached field, this 7503446Smrj * avoids unnecessary trips through kmem/page allocators. 7510Sstevel@tonic-gate */ 7520Sstevel@tonic-gate if (hat->hat_ht_cached != NULL && !is_bare) { 7530Sstevel@tonic-gate hat_enter(hat); 7540Sstevel@tonic-gate ht = hat->hat_ht_cached; 7550Sstevel@tonic-gate if (ht != NULL) { 7560Sstevel@tonic-gate hat->hat_ht_cached = ht->ht_next; 7570Sstevel@tonic-gate need_to_zero = 0; 7580Sstevel@tonic-gate /* XX64 ASSERT() they're all zero somehow */ 7590Sstevel@tonic-gate ASSERT(ht->ht_pfn != PFN_INVALID); 7600Sstevel@tonic-gate } 7610Sstevel@tonic-gate hat_exit(hat); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate if (ht == NULL) { 7650Sstevel@tonic-gate /* 7663543Sjosephb * Allocate an htable, possibly refilling the reserves. 7670Sstevel@tonic-gate */ 7683543Sjosephb if (USE_HAT_RESERVES()) { 7690Sstevel@tonic-gate ht = htable_get_reserve(); 7700Sstevel@tonic-gate } else { 7710Sstevel@tonic-gate /* 7720Sstevel@tonic-gate * Donate successful htable allocations to the reserve. 7730Sstevel@tonic-gate */ 7740Sstevel@tonic-gate for (;;) { 7750Sstevel@tonic-gate ht = kmem_cache_alloc(htable_cache, kmflags); 7760Sstevel@tonic-gate if (ht == NULL) 7770Sstevel@tonic-gate break; 7780Sstevel@tonic-gate ht->ht_pfn = PFN_INVALID; 7793543Sjosephb if (USE_HAT_RESERVES() || 7800Sstevel@tonic-gate htable_reserve_cnt >= htable_reserve_amount) 7810Sstevel@tonic-gate break; 7820Sstevel@tonic-gate htable_put_reserve(ht); 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate 7860Sstevel@tonic-gate /* 7870Sstevel@tonic-gate * allocate a page for the hardware page table if needed 7880Sstevel@tonic-gate */ 7890Sstevel@tonic-gate if (ht != NULL && !is_bare) { 7901747Sjosephb ht->ht_hat = hat; 7913446Smrj ht->ht_pfn = ptable_alloc((uintptr_t)ht); 7920Sstevel@tonic-gate if (ht->ht_pfn == PFN_INVALID) { 7933543Sjosephb if (USE_HAT_RESERVES()) 7943543Sjosephb htable_put_reserve(ht); 7953543Sjosephb else 7963543Sjosephb kmem_cache_free(htable_cache, ht); 7970Sstevel@tonic-gate ht = NULL; 7980Sstevel@tonic-gate } 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate } 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate /* 8031747Sjosephb * If allocations failed, kick off a kmem_reap() and resort to 8041747Sjosephb * htable steal(). We may spin here if the system is very low on 8051747Sjosephb * memory. If the kernel itself has consumed all memory and kmem_reap() 8061747Sjosephb * can't free up anything, then we'll really get stuck here. 8071747Sjosephb * That should only happen in a system where the administrator has 8081747Sjosephb * misconfigured VM parameters via /etc/system. 8090Sstevel@tonic-gate */ 8101747Sjosephb while (ht == NULL && can_steal_post_boot) { 8111747Sjosephb kmem_reap(); 8120Sstevel@tonic-gate ht = htable_steal(1); 8130Sstevel@tonic-gate HATSTAT_INC(hs_steals); 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate /* 8161747Sjosephb * If we stole for a bare htable, release the pagetable page. 8170Sstevel@tonic-gate */ 8183446Smrj if (ht != NULL) { 8193446Smrj if (is_bare) { 8203446Smrj ptable_free(ht->ht_pfn); 8213446Smrj ht->ht_pfn = PFN_INVALID; 8225084Sjohnlev #if defined(__xpv) && defined(__amd64) 8235084Sjohnlev /* 8245084Sjohnlev * make stolen page table writable again in kpm 8255084Sjohnlev */ 8265084Sjohnlev } else if (kpm_vbase && xen_kpm_page(ht->ht_pfn, 8275084Sjohnlev PT_VALID | PT_WRITABLE) < 0) { 8285084Sjohnlev panic("failure making kpm r/w pfn=0x%lx", 8295084Sjohnlev ht->ht_pfn); 8305084Sjohnlev #endif 8313446Smrj } 8323446Smrj } 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate /* 8361747Sjosephb * All attempts to allocate or steal failed. This should only happen 8371747Sjosephb * if we run out of memory during boot, due perhaps to a huge 8381747Sjosephb * boot_archive. At this point there's no way to continue. 8390Sstevel@tonic-gate */ 8400Sstevel@tonic-gate if (ht == NULL) 8410Sstevel@tonic-gate panic("htable_alloc(): couldn't steal\n"); 8420Sstevel@tonic-gate 8435084Sjohnlev #if defined(__amd64) && defined(__xpv) 8445084Sjohnlev /* 8455084Sjohnlev * Under the 64-bit hypervisor, we have 2 top level page tables. 8465084Sjohnlev * If this allocation fails, we'll resort to stealing. 8475084Sjohnlev * We use the stolen page indirectly, by freeing the 8485084Sjohnlev * stolen htable first. 8495084Sjohnlev */ 8505084Sjohnlev if (level == mmu.max_level) { 8515084Sjohnlev for (;;) { 8525084Sjohnlev htable_t *stolen; 8535084Sjohnlev 8545084Sjohnlev hat->hat_user_ptable = ptable_alloc((uintptr_t)ht + 1); 8555084Sjohnlev if (hat->hat_user_ptable != PFN_INVALID) 8565084Sjohnlev break; 8575084Sjohnlev stolen = htable_steal(1); 8585084Sjohnlev if (stolen == NULL) 8595084Sjohnlev panic("2nd steal ptable failed\n"); 8605084Sjohnlev htable_free(stolen); 8615084Sjohnlev } 8625084Sjohnlev block_zero_no_xmm(kpm_vbase + pfn_to_pa(hat->hat_user_ptable), 8635084Sjohnlev MMU_PAGESIZE); 8645084Sjohnlev } 8655084Sjohnlev #endif 8665084Sjohnlev 8670Sstevel@tonic-gate /* 8680Sstevel@tonic-gate * Shared page tables have all entries locked and entries may not 8690Sstevel@tonic-gate * be added or deleted. 8700Sstevel@tonic-gate */ 8710Sstevel@tonic-gate ht->ht_flags = 0; 8720Sstevel@tonic-gate if (shared != NULL) { 8730Sstevel@tonic-gate ASSERT(shared->ht_valid_cnt > 0); 8740Sstevel@tonic-gate ht->ht_flags |= HTABLE_SHARED_PFN; 8750Sstevel@tonic-gate ht->ht_pfn = shared->ht_pfn; 8760Sstevel@tonic-gate ht->ht_lock_cnt = 0; 8770Sstevel@tonic-gate ht->ht_valid_cnt = 0; /* updated in hat_share() */ 8780Sstevel@tonic-gate ht->ht_shares = shared; 8790Sstevel@tonic-gate need_to_zero = 0; 8800Sstevel@tonic-gate } else { 8810Sstevel@tonic-gate ht->ht_shares = NULL; 8820Sstevel@tonic-gate ht->ht_lock_cnt = 0; 8830Sstevel@tonic-gate ht->ht_valid_cnt = 0; 8840Sstevel@tonic-gate } 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate /* 8870Sstevel@tonic-gate * setup flags, etc. for VLP htables 8880Sstevel@tonic-gate */ 8890Sstevel@tonic-gate if (is_vlp) { 8900Sstevel@tonic-gate ht->ht_flags |= HTABLE_VLP; 8910Sstevel@tonic-gate ASSERT(ht->ht_pfn == PFN_INVALID); 8920Sstevel@tonic-gate need_to_zero = 0; 8930Sstevel@tonic-gate } 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate /* 8960Sstevel@tonic-gate * fill in the htable 8970Sstevel@tonic-gate */ 8980Sstevel@tonic-gate ht->ht_hat = hat; 8990Sstevel@tonic-gate ht->ht_parent = NULL; 9000Sstevel@tonic-gate ht->ht_vaddr = vaddr; 9010Sstevel@tonic-gate ht->ht_level = level; 9020Sstevel@tonic-gate ht->ht_busy = 1; 9030Sstevel@tonic-gate ht->ht_next = NULL; 9040Sstevel@tonic-gate ht->ht_prev = NULL; 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate /* 9070Sstevel@tonic-gate * Zero out any freshly allocated page table 9080Sstevel@tonic-gate */ 9090Sstevel@tonic-gate if (need_to_zero) 9100Sstevel@tonic-gate x86pte_zero(ht, 0, mmu.ptes_per_table); 9113446Smrj 9125084Sjohnlev #if defined(__amd64) && defined(__xpv) 9135084Sjohnlev if (!is_bare && kpm_vbase) { 9145084Sjohnlev (void) xen_kpm_page(ht->ht_pfn, PT_VALID); 9155084Sjohnlev if (level == mmu.max_level) 9165084Sjohnlev (void) xen_kpm_page(hat->hat_user_ptable, PT_VALID); 9175084Sjohnlev } 9185084Sjohnlev #endif 9195084Sjohnlev 9200Sstevel@tonic-gate return (ht); 9210Sstevel@tonic-gate } 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate /* 9240Sstevel@tonic-gate * Free up an htable, either to a hat's cached list, the reserves or 9250Sstevel@tonic-gate * back to kmem. 9260Sstevel@tonic-gate */ 9270Sstevel@tonic-gate static void 9280Sstevel@tonic-gate htable_free(htable_t *ht) 9290Sstevel@tonic-gate { 9300Sstevel@tonic-gate hat_t *hat = ht->ht_hat; 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate /* 9330Sstevel@tonic-gate * If the process isn't exiting, cache the free htable in the hat 9345084Sjohnlev * structure. We always do this for the boot time reserve. We don't 9350Sstevel@tonic-gate * do this if the hat is exiting or we are stealing/reaping htables. 9360Sstevel@tonic-gate */ 9370Sstevel@tonic-gate if (hat != NULL && 9380Sstevel@tonic-gate !(ht->ht_flags & HTABLE_SHARED_PFN) && 9390Sstevel@tonic-gate (use_boot_reserve || 9400Sstevel@tonic-gate (!(hat->hat_flags & HAT_FREEING) && !htable_dont_cache))) { 9410Sstevel@tonic-gate ASSERT((ht->ht_flags & HTABLE_VLP) == 0); 9420Sstevel@tonic-gate ASSERT(ht->ht_pfn != PFN_INVALID); 9430Sstevel@tonic-gate hat_enter(hat); 9440Sstevel@tonic-gate ht->ht_next = hat->hat_ht_cached; 9450Sstevel@tonic-gate hat->hat_ht_cached = ht; 9460Sstevel@tonic-gate hat_exit(hat); 9470Sstevel@tonic-gate return; 9480Sstevel@tonic-gate } 9490Sstevel@tonic-gate 9500Sstevel@tonic-gate /* 9510Sstevel@tonic-gate * If we have a hardware page table, free it. 9523446Smrj * We don't free page tables that are accessed by sharing. 9530Sstevel@tonic-gate */ 9540Sstevel@tonic-gate if (ht->ht_flags & HTABLE_SHARED_PFN) { 9550Sstevel@tonic-gate ASSERT(ht->ht_pfn != PFN_INVALID); 9560Sstevel@tonic-gate } else if (!(ht->ht_flags & HTABLE_VLP)) { 9573446Smrj ptable_free(ht->ht_pfn); 9585084Sjohnlev #if defined(__amd64) && defined(__xpv) 9595084Sjohnlev if (ht->ht_level == mmu.max_level) { 9605084Sjohnlev ptable_free(hat->hat_user_ptable); 9615084Sjohnlev hat->hat_user_ptable = PFN_INVALID; 9625084Sjohnlev } 9635084Sjohnlev #endif 9640Sstevel@tonic-gate } 9653446Smrj ht->ht_pfn = PFN_INVALID; 9660Sstevel@tonic-gate 9670Sstevel@tonic-gate /* 9685084Sjohnlev * Free it or put into reserves. 9690Sstevel@tonic-gate */ 9704004Sjosephb if (USE_HAT_RESERVES() || htable_reserve_cnt < htable_reserve_amount) { 9710Sstevel@tonic-gate htable_put_reserve(ht); 9724004Sjosephb } else { 9730Sstevel@tonic-gate kmem_cache_free(htable_cache, ht); 9744004Sjosephb htable_adjust_reserve(); 9754004Sjosephb } 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate /* 9800Sstevel@tonic-gate * This is called when a hat is being destroyed or swapped out. We reap all 9810Sstevel@tonic-gate * the remaining htables in the hat cache. If destroying all left over 9820Sstevel@tonic-gate * htables are also destroyed. 9830Sstevel@tonic-gate * 9840Sstevel@tonic-gate * We also don't need to invalidate any of the PTPs nor do any demapping. 9850Sstevel@tonic-gate */ 9860Sstevel@tonic-gate void 9870Sstevel@tonic-gate htable_purge_hat(hat_t *hat) 9880Sstevel@tonic-gate { 9890Sstevel@tonic-gate htable_t *ht; 9900Sstevel@tonic-gate int h; 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate /* 9930Sstevel@tonic-gate * Purge the htable cache if just reaping. 9940Sstevel@tonic-gate */ 9950Sstevel@tonic-gate if (!(hat->hat_flags & HAT_FREEING)) { 9960Sstevel@tonic-gate atomic_add_32(&htable_dont_cache, 1); 9970Sstevel@tonic-gate for (;;) { 9980Sstevel@tonic-gate hat_enter(hat); 9990Sstevel@tonic-gate ht = hat->hat_ht_cached; 10000Sstevel@tonic-gate if (ht == NULL) { 10010Sstevel@tonic-gate hat_exit(hat); 10020Sstevel@tonic-gate break; 10030Sstevel@tonic-gate } 10040Sstevel@tonic-gate hat->hat_ht_cached = ht->ht_next; 10050Sstevel@tonic-gate hat_exit(hat); 10060Sstevel@tonic-gate htable_free(ht); 10070Sstevel@tonic-gate } 10080Sstevel@tonic-gate atomic_add_32(&htable_dont_cache, -1); 10090Sstevel@tonic-gate return; 10100Sstevel@tonic-gate } 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate /* 10130Sstevel@tonic-gate * if freeing, no locking is needed 10140Sstevel@tonic-gate */ 10150Sstevel@tonic-gate while ((ht = hat->hat_ht_cached) != NULL) { 10160Sstevel@tonic-gate hat->hat_ht_cached = ht->ht_next; 10170Sstevel@tonic-gate htable_free(ht); 10180Sstevel@tonic-gate } 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate /* 10210Sstevel@tonic-gate * walk thru the htable hash table and free all the htables in it. 10220Sstevel@tonic-gate */ 10230Sstevel@tonic-gate for (h = 0; h < hat->hat_num_hash; ++h) { 10240Sstevel@tonic-gate while ((ht = hat->hat_ht_hash[h]) != NULL) { 10250Sstevel@tonic-gate if (ht->ht_next) 10260Sstevel@tonic-gate ht->ht_next->ht_prev = ht->ht_prev; 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate if (ht->ht_prev) { 10290Sstevel@tonic-gate ht->ht_prev->ht_next = ht->ht_next; 10300Sstevel@tonic-gate } else { 10310Sstevel@tonic-gate ASSERT(hat->hat_ht_hash[h] == ht); 10320Sstevel@tonic-gate hat->hat_ht_hash[h] = ht->ht_next; 10330Sstevel@tonic-gate } 10340Sstevel@tonic-gate htable_free(ht); 10350Sstevel@tonic-gate } 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate /* 10400Sstevel@tonic-gate * Unlink an entry for a table at vaddr and level out of the existing table 10410Sstevel@tonic-gate * one level higher. We are always holding the HASH_ENTER() when doing this. 10420Sstevel@tonic-gate */ 10430Sstevel@tonic-gate static void 10440Sstevel@tonic-gate unlink_ptp(htable_t *higher, htable_t *old, uintptr_t vaddr) 10450Sstevel@tonic-gate { 10460Sstevel@tonic-gate uint_t entry = htable_va2entry(vaddr, higher); 10470Sstevel@tonic-gate x86pte_t expect = MAKEPTP(old->ht_pfn, old->ht_level); 10480Sstevel@tonic-gate x86pte_t found; 10494169Sjosephb hat_t *hat = old->ht_hat; 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate ASSERT(higher->ht_busy > 0); 10520Sstevel@tonic-gate ASSERT(higher->ht_valid_cnt > 0); 10530Sstevel@tonic-gate ASSERT(old->ht_valid_cnt == 0); 10540Sstevel@tonic-gate found = x86pte_cas(higher, entry, expect, 0); 10555084Sjohnlev #ifdef __xpv 10565084Sjohnlev /* 10575084Sjohnlev * This is weird, but Xen apparently automatically unlinks empty 10585084Sjohnlev * pagetables from the upper page table. So allow PTP to be 0 already. 10595084Sjohnlev */ 10605084Sjohnlev if (found != expect && found != 0) 10615084Sjohnlev #else 10620Sstevel@tonic-gate if (found != expect) 10635084Sjohnlev #endif 10640Sstevel@tonic-gate panic("Bad PTP found=" FMT_PTE ", expected=" FMT_PTE, 10650Sstevel@tonic-gate found, expect); 10664169Sjosephb 10674169Sjosephb /* 10684654Sjosephb * When a top level VLP page table entry changes, we must issue 10694654Sjosephb * a reload of cr3 on all processors. 10704654Sjosephb * 10714654Sjosephb * If we don't need do do that, then we still have to INVLPG against 10724654Sjosephb * an address covered by the inner page table, as the latest processors 10734654Sjosephb * have TLB-like caches for non-leaf page table entries. 10744169Sjosephb */ 10754169Sjosephb if (!(hat->hat_flags & HAT_FREEING)) { 10764654Sjosephb hat_tlb_inval(hat, (higher->ht_flags & HTABLE_VLP) ? 10774654Sjosephb DEMAP_ALL_ADDR : old->ht_vaddr); 10784169Sjosephb } 10794169Sjosephb 10800Sstevel@tonic-gate HTABLE_DEC(higher->ht_valid_cnt); 10810Sstevel@tonic-gate } 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate /* 10840Sstevel@tonic-gate * Link an entry for a new table at vaddr and level into the existing table 10850Sstevel@tonic-gate * one level higher. We are always holding the HASH_ENTER() when doing this. 10860Sstevel@tonic-gate */ 10870Sstevel@tonic-gate static void 10880Sstevel@tonic-gate link_ptp(htable_t *higher, htable_t *new, uintptr_t vaddr) 10890Sstevel@tonic-gate { 10900Sstevel@tonic-gate uint_t entry = htable_va2entry(vaddr, higher); 10910Sstevel@tonic-gate x86pte_t newptp = MAKEPTP(new->ht_pfn, new->ht_level); 10920Sstevel@tonic-gate x86pte_t found; 10930Sstevel@tonic-gate 10940Sstevel@tonic-gate ASSERT(higher->ht_busy > 0); 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate ASSERT(new->ht_level != mmu.max_level); 10970Sstevel@tonic-gate 10980Sstevel@tonic-gate HTABLE_INC(higher->ht_valid_cnt); 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate found = x86pte_cas(higher, entry, 0, newptp); 11011251Skchow if ((found & ~PT_REF) != 0) 11020Sstevel@tonic-gate panic("HAT: ptp not 0, found=" FMT_PTE, found); 11034169Sjosephb 11044169Sjosephb /* 11054169Sjosephb * When any top level VLP page table entry changes, we must issue 11064169Sjosephb * a reload of cr3 on all processors using it. 11074269Sjosephb * We also need to do this for the kernel hat on PAE 32 bit kernel. 11084169Sjosephb */ 11094269Sjosephb if ( 11104269Sjosephb #ifdef __i386 11114269Sjosephb (higher->ht_hat == kas.a_hat && higher->ht_level == VLP_LEVEL) || 11124269Sjosephb #endif 11134269Sjosephb (higher->ht_flags & HTABLE_VLP)) 11144169Sjosephb hat_tlb_inval(higher->ht_hat, DEMAP_ALL_ADDR); 11150Sstevel@tonic-gate } 11160Sstevel@tonic-gate 11170Sstevel@tonic-gate /* 11183446Smrj * Release of hold on an htable. If this is the last use and the pagetable 11193446Smrj * is empty we may want to free it, then recursively look at the pagetable 11203446Smrj * above it. The recursion is handled by the outer while() loop. 11215084Sjohnlev * 11225084Sjohnlev * On the metal, during process exit, we don't bother unlinking the tables from 11235084Sjohnlev * upper level pagetables. They are instead handled in bulk by hat_free_end(). 11245084Sjohnlev * We can't do this on the hypervisor as we need the page table to be 11255084Sjohnlev * implicitly unpinnned before it goes to the free page lists. This can't 11265084Sjohnlev * happen unless we fully unlink it from the page table hierarchy. 11270Sstevel@tonic-gate */ 11280Sstevel@tonic-gate void 11290Sstevel@tonic-gate htable_release(htable_t *ht) 11300Sstevel@tonic-gate { 11310Sstevel@tonic-gate uint_t hashval; 11320Sstevel@tonic-gate htable_t *shared; 11330Sstevel@tonic-gate htable_t *higher; 11340Sstevel@tonic-gate hat_t *hat; 11350Sstevel@tonic-gate uintptr_t va; 11360Sstevel@tonic-gate level_t level; 11370Sstevel@tonic-gate 11380Sstevel@tonic-gate while (ht != NULL) { 11390Sstevel@tonic-gate shared = NULL; 11400Sstevel@tonic-gate for (;;) { 11410Sstevel@tonic-gate hat = ht->ht_hat; 11420Sstevel@tonic-gate va = ht->ht_vaddr; 11430Sstevel@tonic-gate level = ht->ht_level; 11440Sstevel@tonic-gate hashval = HTABLE_HASH(hat, va, level); 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate /* 11470Sstevel@tonic-gate * The common case is that this isn't the last use of 11480Sstevel@tonic-gate * an htable so we don't want to free the htable. 11490Sstevel@tonic-gate */ 11500Sstevel@tonic-gate HTABLE_ENTER(hashval); 11510Sstevel@tonic-gate ASSERT(ht->ht_valid_cnt >= 0); 11520Sstevel@tonic-gate ASSERT(ht->ht_busy > 0); 11530Sstevel@tonic-gate if (ht->ht_valid_cnt > 0) 11540Sstevel@tonic-gate break; 11550Sstevel@tonic-gate if (ht->ht_busy > 1) 11560Sstevel@tonic-gate break; 1157*5224Smec ASSERT(ht->ht_lock_cnt == 0); 11580Sstevel@tonic-gate 11595084Sjohnlev #if !defined(__xpv) 11600Sstevel@tonic-gate /* 11610Sstevel@tonic-gate * we always release empty shared htables 11620Sstevel@tonic-gate */ 11630Sstevel@tonic-gate if (!(ht->ht_flags & HTABLE_SHARED_PFN)) { 11640Sstevel@tonic-gate 11650Sstevel@tonic-gate /* 11660Sstevel@tonic-gate * don't release if in address space tear down 11670Sstevel@tonic-gate */ 11680Sstevel@tonic-gate if (hat->hat_flags & HAT_FREEING) 11690Sstevel@tonic-gate break; 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate /* 11720Sstevel@tonic-gate * At and above max_page_level, free if it's for 11730Sstevel@tonic-gate * a boot-time kernel mapping below kernelbase. 11740Sstevel@tonic-gate */ 11750Sstevel@tonic-gate if (level >= mmu.max_page_level && 11760Sstevel@tonic-gate (hat != kas.a_hat || va >= kernelbase)) 11770Sstevel@tonic-gate break; 11780Sstevel@tonic-gate } 11795084Sjohnlev #endif /* __xpv */ 11800Sstevel@tonic-gate 11810Sstevel@tonic-gate /* 11823446Smrj * Remember if we destroy an htable that shares its PFN 11833446Smrj * from elsewhere. 11840Sstevel@tonic-gate */ 11850Sstevel@tonic-gate if (ht->ht_flags & HTABLE_SHARED_PFN) { 11860Sstevel@tonic-gate ASSERT(shared == NULL); 11870Sstevel@tonic-gate shared = ht->ht_shares; 11880Sstevel@tonic-gate HATSTAT_INC(hs_htable_unshared); 11890Sstevel@tonic-gate } 11900Sstevel@tonic-gate 11910Sstevel@tonic-gate /* 11920Sstevel@tonic-gate * Handle release of a table and freeing the htable_t. 11930Sstevel@tonic-gate * Unlink it from the table higher (ie. ht_parent). 11940Sstevel@tonic-gate */ 11950Sstevel@tonic-gate ASSERT(ht->ht_lock_cnt == 0); 11960Sstevel@tonic-gate higher = ht->ht_parent; 11970Sstevel@tonic-gate ASSERT(higher != NULL); 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate /* 12000Sstevel@tonic-gate * Unlink the pagetable. 12010Sstevel@tonic-gate */ 12020Sstevel@tonic-gate unlink_ptp(higher, ht, va); 12030Sstevel@tonic-gate 12040Sstevel@tonic-gate /* 12050Sstevel@tonic-gate * remove this htable from its hash list 12060Sstevel@tonic-gate */ 12070Sstevel@tonic-gate if (ht->ht_next) 12080Sstevel@tonic-gate ht->ht_next->ht_prev = ht->ht_prev; 12090Sstevel@tonic-gate 12100Sstevel@tonic-gate if (ht->ht_prev) { 12110Sstevel@tonic-gate ht->ht_prev->ht_next = ht->ht_next; 12120Sstevel@tonic-gate } else { 12130Sstevel@tonic-gate ASSERT(hat->hat_ht_hash[hashval] == ht); 12140Sstevel@tonic-gate hat->hat_ht_hash[hashval] = ht->ht_next; 12150Sstevel@tonic-gate } 12160Sstevel@tonic-gate HTABLE_EXIT(hashval); 12170Sstevel@tonic-gate htable_free(ht); 12180Sstevel@tonic-gate ht = higher; 12190Sstevel@tonic-gate } 12200Sstevel@tonic-gate 12210Sstevel@tonic-gate ASSERT(ht->ht_busy >= 1); 12220Sstevel@tonic-gate --ht->ht_busy; 12230Sstevel@tonic-gate HTABLE_EXIT(hashval); 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate /* 12260Sstevel@tonic-gate * If we released a shared htable, do a release on the htable 12270Sstevel@tonic-gate * from which it shared 12280Sstevel@tonic-gate */ 12290Sstevel@tonic-gate ht = shared; 12300Sstevel@tonic-gate } 12310Sstevel@tonic-gate } 12320Sstevel@tonic-gate 12330Sstevel@tonic-gate /* 12340Sstevel@tonic-gate * Find the htable for the pagetable at the given level for the given address. 12350Sstevel@tonic-gate * If found acquires a hold that eventually needs to be htable_release()d 12360Sstevel@tonic-gate */ 12370Sstevel@tonic-gate htable_t * 12380Sstevel@tonic-gate htable_lookup(hat_t *hat, uintptr_t vaddr, level_t level) 12390Sstevel@tonic-gate { 12400Sstevel@tonic-gate uintptr_t base; 12410Sstevel@tonic-gate uint_t hashval; 12420Sstevel@tonic-gate htable_t *ht = NULL; 12430Sstevel@tonic-gate 12440Sstevel@tonic-gate ASSERT(level >= 0); 12450Sstevel@tonic-gate ASSERT(level <= TOP_LEVEL(hat)); 12460Sstevel@tonic-gate 12474654Sjosephb if (level == TOP_LEVEL(hat)) { 12484654Sjosephb #if defined(__amd64) 12494654Sjosephb /* 12504654Sjosephb * 32 bit address spaces on 64 bit kernels need to check 12514654Sjosephb * for overflow of the 32 bit address space 12524654Sjosephb */ 12534654Sjosephb if ((hat->hat_flags & HAT_VLP) && vaddr >= ((uint64_t)1 << 32)) 12544654Sjosephb return (NULL); 12554654Sjosephb #endif 12560Sstevel@tonic-gate base = 0; 12574654Sjosephb } else { 12580Sstevel@tonic-gate base = vaddr & LEVEL_MASK(level + 1); 12594654Sjosephb } 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate hashval = HTABLE_HASH(hat, base, level); 12620Sstevel@tonic-gate HTABLE_ENTER(hashval); 12630Sstevel@tonic-gate for (ht = hat->hat_ht_hash[hashval]; ht; ht = ht->ht_next) { 12640Sstevel@tonic-gate if (ht->ht_hat == hat && 12650Sstevel@tonic-gate ht->ht_vaddr == base && 12660Sstevel@tonic-gate ht->ht_level == level) 12670Sstevel@tonic-gate break; 12680Sstevel@tonic-gate } 12690Sstevel@tonic-gate if (ht) 12700Sstevel@tonic-gate ++ht->ht_busy; 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate HTABLE_EXIT(hashval); 12730Sstevel@tonic-gate return (ht); 12740Sstevel@tonic-gate } 12750Sstevel@tonic-gate 12760Sstevel@tonic-gate /* 12770Sstevel@tonic-gate * Acquires a hold on a known htable (from a locked hment entry). 12780Sstevel@tonic-gate */ 12790Sstevel@tonic-gate void 12800Sstevel@tonic-gate htable_acquire(htable_t *ht) 12810Sstevel@tonic-gate { 12820Sstevel@tonic-gate hat_t *hat = ht->ht_hat; 12830Sstevel@tonic-gate level_t level = ht->ht_level; 12840Sstevel@tonic-gate uintptr_t base = ht->ht_vaddr; 12850Sstevel@tonic-gate uint_t hashval = HTABLE_HASH(hat, base, level); 12860Sstevel@tonic-gate 12870Sstevel@tonic-gate HTABLE_ENTER(hashval); 12880Sstevel@tonic-gate #ifdef DEBUG 12890Sstevel@tonic-gate /* 12900Sstevel@tonic-gate * make sure the htable is there 12910Sstevel@tonic-gate */ 12920Sstevel@tonic-gate { 12930Sstevel@tonic-gate htable_t *h; 12940Sstevel@tonic-gate 12950Sstevel@tonic-gate for (h = hat->hat_ht_hash[hashval]; 12960Sstevel@tonic-gate h && h != ht; 12970Sstevel@tonic-gate h = h->ht_next) 12980Sstevel@tonic-gate ; 12990Sstevel@tonic-gate ASSERT(h == ht); 13000Sstevel@tonic-gate } 13010Sstevel@tonic-gate #endif /* DEBUG */ 13020Sstevel@tonic-gate ++ht->ht_busy; 13030Sstevel@tonic-gate HTABLE_EXIT(hashval); 13040Sstevel@tonic-gate } 13050Sstevel@tonic-gate 13060Sstevel@tonic-gate /* 13070Sstevel@tonic-gate * Find the htable for the pagetable at the given level for the given address. 13080Sstevel@tonic-gate * If found acquires a hold that eventually needs to be htable_release()d 13090Sstevel@tonic-gate * If not found the table is created. 13100Sstevel@tonic-gate * 13110Sstevel@tonic-gate * Since we can't hold a hash table mutex during allocation, we have to 13120Sstevel@tonic-gate * drop it and redo the search on a create. Then we may have to free the newly 13130Sstevel@tonic-gate * allocated htable if another thread raced in and created it ahead of us. 13140Sstevel@tonic-gate */ 13150Sstevel@tonic-gate htable_t * 13160Sstevel@tonic-gate htable_create( 13170Sstevel@tonic-gate hat_t *hat, 13180Sstevel@tonic-gate uintptr_t vaddr, 13190Sstevel@tonic-gate level_t level, 13200Sstevel@tonic-gate htable_t *shared) 13210Sstevel@tonic-gate { 13220Sstevel@tonic-gate uint_t h; 13230Sstevel@tonic-gate level_t l; 13240Sstevel@tonic-gate uintptr_t base; 13250Sstevel@tonic-gate htable_t *ht; 13260Sstevel@tonic-gate htable_t *higher = NULL; 13270Sstevel@tonic-gate htable_t *new = NULL; 13280Sstevel@tonic-gate 13290Sstevel@tonic-gate if (level < 0 || level > TOP_LEVEL(hat)) 13300Sstevel@tonic-gate panic("htable_create(): level %d out of range\n", level); 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate /* 13330Sstevel@tonic-gate * Create the page tables in top down order. 13340Sstevel@tonic-gate */ 13350Sstevel@tonic-gate for (l = TOP_LEVEL(hat); l >= level; --l) { 13360Sstevel@tonic-gate new = NULL; 13370Sstevel@tonic-gate if (l == TOP_LEVEL(hat)) 13380Sstevel@tonic-gate base = 0; 13390Sstevel@tonic-gate else 13400Sstevel@tonic-gate base = vaddr & LEVEL_MASK(l + 1); 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate h = HTABLE_HASH(hat, base, l); 13430Sstevel@tonic-gate try_again: 13440Sstevel@tonic-gate /* 13450Sstevel@tonic-gate * look up the htable at this level 13460Sstevel@tonic-gate */ 13470Sstevel@tonic-gate HTABLE_ENTER(h); 13480Sstevel@tonic-gate if (l == TOP_LEVEL(hat)) { 13490Sstevel@tonic-gate ht = hat->hat_htable; 13500Sstevel@tonic-gate } else { 13510Sstevel@tonic-gate for (ht = hat->hat_ht_hash[h]; ht; ht = ht->ht_next) { 13520Sstevel@tonic-gate ASSERT(ht->ht_hat == hat); 13530Sstevel@tonic-gate if (ht->ht_vaddr == base && 13540Sstevel@tonic-gate ht->ht_level == l) 13550Sstevel@tonic-gate break; 13560Sstevel@tonic-gate } 13570Sstevel@tonic-gate } 13580Sstevel@tonic-gate 13590Sstevel@tonic-gate /* 13600Sstevel@tonic-gate * if we found the htable, increment its busy cnt 13610Sstevel@tonic-gate * and if we had allocated a new htable, free it. 13620Sstevel@tonic-gate */ 13630Sstevel@tonic-gate if (ht != NULL) { 13640Sstevel@tonic-gate /* 13650Sstevel@tonic-gate * If we find a pre-existing shared table, it must 13660Sstevel@tonic-gate * share from the same place. 13670Sstevel@tonic-gate */ 13680Sstevel@tonic-gate if (l == level && shared && ht->ht_shares && 13690Sstevel@tonic-gate ht->ht_shares != shared) { 13700Sstevel@tonic-gate panic("htable shared from wrong place " 13710Sstevel@tonic-gate "found htable=%p shared=%p", ht, shared); 13720Sstevel@tonic-gate } 13730Sstevel@tonic-gate ++ht->ht_busy; 13740Sstevel@tonic-gate HTABLE_EXIT(h); 13750Sstevel@tonic-gate if (new) 13760Sstevel@tonic-gate htable_free(new); 13770Sstevel@tonic-gate if (higher != NULL) 13780Sstevel@tonic-gate htable_release(higher); 13790Sstevel@tonic-gate higher = ht; 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate /* 13820Sstevel@tonic-gate * if we didn't find it on the first search 13830Sstevel@tonic-gate * allocate a new one and search again 13840Sstevel@tonic-gate */ 13850Sstevel@tonic-gate } else if (new == NULL) { 13860Sstevel@tonic-gate HTABLE_EXIT(h); 13870Sstevel@tonic-gate new = htable_alloc(hat, base, l, 13880Sstevel@tonic-gate l == level ? shared : NULL); 13890Sstevel@tonic-gate goto try_again; 13900Sstevel@tonic-gate 13910Sstevel@tonic-gate /* 13920Sstevel@tonic-gate * 2nd search and still not there, use "new" table 13930Sstevel@tonic-gate * Link new table into higher, when not at top level. 13940Sstevel@tonic-gate */ 13950Sstevel@tonic-gate } else { 13960Sstevel@tonic-gate ht = new; 13970Sstevel@tonic-gate if (higher != NULL) { 13980Sstevel@tonic-gate link_ptp(higher, ht, base); 13990Sstevel@tonic-gate ht->ht_parent = higher; 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate ht->ht_next = hat->hat_ht_hash[h]; 14020Sstevel@tonic-gate ASSERT(ht->ht_prev == NULL); 14030Sstevel@tonic-gate if (hat->hat_ht_hash[h]) 14040Sstevel@tonic-gate hat->hat_ht_hash[h]->ht_prev = ht; 14050Sstevel@tonic-gate hat->hat_ht_hash[h] = ht; 14060Sstevel@tonic-gate HTABLE_EXIT(h); 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate /* 14090Sstevel@tonic-gate * Note we don't do htable_release(higher). 14100Sstevel@tonic-gate * That happens recursively when "new" is removed by 14110Sstevel@tonic-gate * htable_release() or htable_steal(). 14120Sstevel@tonic-gate */ 14130Sstevel@tonic-gate higher = ht; 14140Sstevel@tonic-gate 14150Sstevel@tonic-gate /* 14160Sstevel@tonic-gate * If we just created a new shared page table we 14170Sstevel@tonic-gate * increment the shared htable's busy count, so that 14180Sstevel@tonic-gate * it can't be the victim of a steal even if it's empty. 14190Sstevel@tonic-gate */ 14200Sstevel@tonic-gate if (l == level && shared) { 14210Sstevel@tonic-gate (void) htable_lookup(shared->ht_hat, 14220Sstevel@tonic-gate shared->ht_vaddr, shared->ht_level); 14230Sstevel@tonic-gate HATSTAT_INC(hs_htable_shared); 14240Sstevel@tonic-gate } 14250Sstevel@tonic-gate } 14260Sstevel@tonic-gate } 14270Sstevel@tonic-gate 14280Sstevel@tonic-gate return (ht); 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate /* 14325084Sjohnlev * Inherit initial pagetables from the boot program. On the 64-bit 14335084Sjohnlev * hypervisor we also temporarily mark the p_index field of page table 14345084Sjohnlev * pages, so we know not to try making them writable in seg_kpm. 14353446Smrj */ 14363446Smrj void 14373446Smrj htable_attach( 14383446Smrj hat_t *hat, 14393446Smrj uintptr_t base, 14403446Smrj level_t level, 14413446Smrj htable_t *parent, 14423446Smrj pfn_t pfn) 14433446Smrj { 14443446Smrj htable_t *ht; 14453446Smrj uint_t h; 14463446Smrj uint_t i; 14473446Smrj x86pte_t pte; 14483446Smrj x86pte_t *ptep; 14493446Smrj page_t *pp; 14503446Smrj extern page_t *boot_claim_page(pfn_t); 14513446Smrj 14523446Smrj ht = htable_get_reserve(); 14533446Smrj if (level == mmu.max_level) 14543446Smrj kas.a_hat->hat_htable = ht; 14553446Smrj ht->ht_hat = hat; 14563446Smrj ht->ht_parent = parent; 14573446Smrj ht->ht_vaddr = base; 14583446Smrj ht->ht_level = level; 14593446Smrj ht->ht_busy = 1; 14603446Smrj ht->ht_next = NULL; 14613446Smrj ht->ht_prev = NULL; 14623446Smrj ht->ht_flags = 0; 14633446Smrj ht->ht_pfn = pfn; 14643446Smrj ht->ht_lock_cnt = 0; 14653446Smrj ht->ht_valid_cnt = 0; 14663446Smrj if (parent != NULL) 14673446Smrj ++parent->ht_busy; 14683446Smrj 14693446Smrj h = HTABLE_HASH(hat, base, level); 14703446Smrj HTABLE_ENTER(h); 14713446Smrj ht->ht_next = hat->hat_ht_hash[h]; 14723446Smrj ASSERT(ht->ht_prev == NULL); 14733446Smrj if (hat->hat_ht_hash[h]) 14743446Smrj hat->hat_ht_hash[h]->ht_prev = ht; 14753446Smrj hat->hat_ht_hash[h] = ht; 14763446Smrj HTABLE_EXIT(h); 14773446Smrj 14783446Smrj /* 14793446Smrj * make sure the page table physical page is not FREE 14803446Smrj */ 14813446Smrj if (page_resv(1, KM_NOSLEEP) == 0) 14823446Smrj panic("page_resv() failed in ptable alloc"); 14833446Smrj 14843446Smrj pp = boot_claim_page(pfn); 14853446Smrj ASSERT(pp != NULL); 14863446Smrj page_downgrade(pp); 14875084Sjohnlev #if defined(__xpv) && defined(__amd64) 14883446Smrj /* 14893446Smrj * Record in the page_t that is a pagetable for segkpm setup. 14903446Smrj */ 14913446Smrj if (kpm_vbase) 14923446Smrj pp->p_index = 1; 14935084Sjohnlev #endif 14943446Smrj 14953446Smrj /* 14963446Smrj * Count valid mappings and recursively attach lower level pagetables. 14973446Smrj */ 14983446Smrj ptep = kbm_remap_window(pfn_to_pa(pfn), 0); 14993446Smrj for (i = 0; i < HTABLE_NUM_PTES(ht); ++i) { 15003446Smrj if (mmu.pae_hat) 15013446Smrj pte = ptep[i]; 15023446Smrj else 15033446Smrj pte = ((x86pte32_t *)ptep)[i]; 15043446Smrj if (!IN_HYPERVISOR_VA(base) && PTE_ISVALID(pte)) { 15053446Smrj ++ht->ht_valid_cnt; 15063446Smrj if (!PTE_ISPAGE(pte, level)) { 15073446Smrj htable_attach(hat, base, level - 1, 15083446Smrj ht, PTE2PFN(pte, level)); 15093446Smrj ptep = kbm_remap_window(pfn_to_pa(pfn), 0); 15103446Smrj } 15113446Smrj } 15123446Smrj base += LEVEL_SIZE(level); 15133446Smrj if (base == mmu.hole_start) 15143446Smrj base = (mmu.hole_end + MMU_PAGEOFFSET) & MMU_PAGEMASK; 15153446Smrj } 15163446Smrj 15173446Smrj /* 15183446Smrj * As long as all the mappings we had were below kernel base 15193446Smrj * we can release the htable. 15203446Smrj */ 15213446Smrj if (base < kernelbase) 15223446Smrj htable_release(ht); 15233446Smrj } 15243446Smrj 15253446Smrj /* 15260Sstevel@tonic-gate * Walk through a given htable looking for the first valid entry. This 15270Sstevel@tonic-gate * routine takes both a starting and ending address. The starting address 15280Sstevel@tonic-gate * is required to be within the htable provided by the caller, but there is 15290Sstevel@tonic-gate * no such restriction on the ending address. 15300Sstevel@tonic-gate * 15310Sstevel@tonic-gate * If the routine finds a valid entry in the htable (at or beyond the 15320Sstevel@tonic-gate * starting address), the PTE (and its address) will be returned. 15330Sstevel@tonic-gate * This PTE may correspond to either a page or a pagetable - it is the 15340Sstevel@tonic-gate * caller's responsibility to determine which. If no valid entry is 15350Sstevel@tonic-gate * found, 0 (and invalid PTE) and the next unexamined address will be 15360Sstevel@tonic-gate * returned. 15370Sstevel@tonic-gate * 15380Sstevel@tonic-gate * The loop has been carefully coded for optimization. 15390Sstevel@tonic-gate */ 15400Sstevel@tonic-gate static x86pte_t 15410Sstevel@tonic-gate htable_scan(htable_t *ht, uintptr_t *vap, uintptr_t eaddr) 15420Sstevel@tonic-gate { 15430Sstevel@tonic-gate uint_t e; 15440Sstevel@tonic-gate x86pte_t found_pte = (x86pte_t)0; 15453446Smrj caddr_t pte_ptr; 15463446Smrj caddr_t end_pte_ptr; 15470Sstevel@tonic-gate int l = ht->ht_level; 15480Sstevel@tonic-gate uintptr_t va = *vap & LEVEL_MASK(l); 15490Sstevel@tonic-gate size_t pgsize = LEVEL_SIZE(l); 15500Sstevel@tonic-gate 15510Sstevel@tonic-gate ASSERT(va >= ht->ht_vaddr); 15520Sstevel@tonic-gate ASSERT(va <= HTABLE_LAST_PAGE(ht)); 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate /* 15550Sstevel@tonic-gate * Compute the starting index and ending virtual address 15560Sstevel@tonic-gate */ 15570Sstevel@tonic-gate e = htable_va2entry(va, ht); 15580Sstevel@tonic-gate 15590Sstevel@tonic-gate /* 15600Sstevel@tonic-gate * The following page table scan code knows that the valid 15610Sstevel@tonic-gate * bit of a PTE is in the lowest byte AND that x86 is little endian!! 15620Sstevel@tonic-gate */ 15633446Smrj pte_ptr = (caddr_t)x86pte_access_pagetable(ht, 0); 15643446Smrj end_pte_ptr = (caddr_t)PT_INDEX_PTR(pte_ptr, HTABLE_NUM_PTES(ht)); 15653446Smrj pte_ptr = (caddr_t)PT_INDEX_PTR((x86pte_t *)pte_ptr, e); 15662687Skchow while (!PTE_ISVALID(*pte_ptr)) { 15670Sstevel@tonic-gate va += pgsize; 15680Sstevel@tonic-gate if (va >= eaddr) 15690Sstevel@tonic-gate break; 15700Sstevel@tonic-gate pte_ptr += mmu.pte_size; 15710Sstevel@tonic-gate ASSERT(pte_ptr <= end_pte_ptr); 15720Sstevel@tonic-gate if (pte_ptr == end_pte_ptr) 15730Sstevel@tonic-gate break; 15740Sstevel@tonic-gate } 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate /* 15770Sstevel@tonic-gate * if we found a valid PTE, load the entire PTE 15780Sstevel@tonic-gate */ 15793446Smrj if (va < eaddr && pte_ptr != end_pte_ptr) 15803446Smrj found_pte = GET_PTE((x86pte_t *)pte_ptr); 15810Sstevel@tonic-gate x86pte_release_pagetable(ht); 15820Sstevel@tonic-gate 15830Sstevel@tonic-gate #if defined(__amd64) 15840Sstevel@tonic-gate /* 15850Sstevel@tonic-gate * deal with VA hole on amd64 15860Sstevel@tonic-gate */ 15870Sstevel@tonic-gate if (l == mmu.max_level && va >= mmu.hole_start && va <= mmu.hole_end) 15880Sstevel@tonic-gate va = mmu.hole_end + va - mmu.hole_start; 15890Sstevel@tonic-gate #endif /* __amd64 */ 15900Sstevel@tonic-gate 15910Sstevel@tonic-gate *vap = va; 15920Sstevel@tonic-gate return (found_pte); 15930Sstevel@tonic-gate } 15940Sstevel@tonic-gate 15950Sstevel@tonic-gate /* 15960Sstevel@tonic-gate * Find the address and htable for the first populated translation at or 15970Sstevel@tonic-gate * above the given virtual address. The caller may also specify an upper 15980Sstevel@tonic-gate * limit to the address range to search. Uses level information to quickly 15990Sstevel@tonic-gate * skip unpopulated sections of virtual address spaces. 16000Sstevel@tonic-gate * 16010Sstevel@tonic-gate * If not found returns NULL. When found, returns the htable and virt addr 16020Sstevel@tonic-gate * and has a hold on the htable. 16030Sstevel@tonic-gate */ 16040Sstevel@tonic-gate x86pte_t 16050Sstevel@tonic-gate htable_walk( 16060Sstevel@tonic-gate struct hat *hat, 16070Sstevel@tonic-gate htable_t **htp, 16080Sstevel@tonic-gate uintptr_t *vaddr, 16090Sstevel@tonic-gate uintptr_t eaddr) 16100Sstevel@tonic-gate { 16110Sstevel@tonic-gate uintptr_t va = *vaddr; 16120Sstevel@tonic-gate htable_t *ht; 16130Sstevel@tonic-gate htable_t *prev = *htp; 16140Sstevel@tonic-gate level_t l; 16150Sstevel@tonic-gate level_t max_mapped_level; 16160Sstevel@tonic-gate x86pte_t pte; 16170Sstevel@tonic-gate 16180Sstevel@tonic-gate ASSERT(eaddr > va); 16190Sstevel@tonic-gate 16200Sstevel@tonic-gate /* 16210Sstevel@tonic-gate * If this is a user address, then we know we need not look beyond 16220Sstevel@tonic-gate * kernelbase. 16230Sstevel@tonic-gate */ 16240Sstevel@tonic-gate ASSERT(hat == kas.a_hat || eaddr <= kernelbase || 16250Sstevel@tonic-gate eaddr == HTABLE_WALK_TO_END); 16260Sstevel@tonic-gate if (hat != kas.a_hat && eaddr == HTABLE_WALK_TO_END) 16270Sstevel@tonic-gate eaddr = kernelbase; 16280Sstevel@tonic-gate 16290Sstevel@tonic-gate /* 16300Sstevel@tonic-gate * If we're coming in with a previous page table, search it first 16310Sstevel@tonic-gate * without doing an htable_lookup(), this should be frequent. 16320Sstevel@tonic-gate */ 16330Sstevel@tonic-gate if (prev) { 16340Sstevel@tonic-gate ASSERT(prev->ht_busy > 0); 16350Sstevel@tonic-gate ASSERT(prev->ht_vaddr <= va); 16360Sstevel@tonic-gate l = prev->ht_level; 16370Sstevel@tonic-gate if (va <= HTABLE_LAST_PAGE(prev)) { 16380Sstevel@tonic-gate pte = htable_scan(prev, &va, eaddr); 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate if (PTE_ISPAGE(pte, l)) { 16410Sstevel@tonic-gate *vaddr = va; 16420Sstevel@tonic-gate *htp = prev; 16430Sstevel@tonic-gate return (pte); 16440Sstevel@tonic-gate } 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate /* 16480Sstevel@tonic-gate * We found nothing in the htable provided by the caller, 16490Sstevel@tonic-gate * so fall through and do the full search 16500Sstevel@tonic-gate */ 16510Sstevel@tonic-gate htable_release(prev); 16520Sstevel@tonic-gate } 16530Sstevel@tonic-gate 16540Sstevel@tonic-gate /* 16550Sstevel@tonic-gate * Find the level of the largest pagesize used by this HAT. 16560Sstevel@tonic-gate */ 16574654Sjosephb if (hat->hat_ism_pgcnt > 0) { 16584654Sjosephb max_mapped_level = mmu.max_page_level; 16594654Sjosephb } else { 16604654Sjosephb max_mapped_level = 0; 16614654Sjosephb for (l = 1; l <= mmu.max_page_level; ++l) 16624654Sjosephb if (hat->hat_pages_mapped[l] != 0) 16634654Sjosephb max_mapped_level = l; 16644654Sjosephb } 16650Sstevel@tonic-gate 16660Sstevel@tonic-gate while (va < eaddr && va >= *vaddr) { 16670Sstevel@tonic-gate ASSERT(!IN_VA_HOLE(va)); 16680Sstevel@tonic-gate 16690Sstevel@tonic-gate /* 16700Sstevel@tonic-gate * Find lowest table with any entry for given address. 16710Sstevel@tonic-gate */ 16720Sstevel@tonic-gate for (l = 0; l <= TOP_LEVEL(hat); ++l) { 16730Sstevel@tonic-gate ht = htable_lookup(hat, va, l); 16740Sstevel@tonic-gate if (ht != NULL) { 16750Sstevel@tonic-gate pte = htable_scan(ht, &va, eaddr); 16760Sstevel@tonic-gate if (PTE_ISPAGE(pte, l)) { 16770Sstevel@tonic-gate *vaddr = va; 16780Sstevel@tonic-gate *htp = ht; 16790Sstevel@tonic-gate return (pte); 16800Sstevel@tonic-gate } 16810Sstevel@tonic-gate htable_release(ht); 16820Sstevel@tonic-gate break; 16830Sstevel@tonic-gate } 16840Sstevel@tonic-gate 16850Sstevel@tonic-gate /* 16864654Sjosephb * No htable at this level for the address. If there 16874654Sjosephb * is no larger page size that could cover it, we can 16884654Sjosephb * skip right to the start of the next page table. 16894575Sdm120769 */ 16904575Sdm120769 ASSERT(l < TOP_LEVEL(hat)); 16914575Sdm120769 if (l >= max_mapped_level) { 16920Sstevel@tonic-gate va = NEXT_ENTRY_VA(va, l + 1); 16934654Sjosephb if (va >= eaddr) 16944654Sjosephb break; 16954575Sdm120769 } 16960Sstevel@tonic-gate } 16970Sstevel@tonic-gate } 16980Sstevel@tonic-gate 16990Sstevel@tonic-gate *vaddr = 0; 17000Sstevel@tonic-gate *htp = NULL; 17010Sstevel@tonic-gate return (0); 17020Sstevel@tonic-gate } 17030Sstevel@tonic-gate 17040Sstevel@tonic-gate /* 17050Sstevel@tonic-gate * Find the htable and page table entry index of the given virtual address 17060Sstevel@tonic-gate * with pagesize at or below given level. 17070Sstevel@tonic-gate * If not found returns NULL. When found, returns the htable, sets 17080Sstevel@tonic-gate * entry, and has a hold on the htable. 17090Sstevel@tonic-gate */ 17100Sstevel@tonic-gate htable_t * 17110Sstevel@tonic-gate htable_getpte( 17120Sstevel@tonic-gate struct hat *hat, 17130Sstevel@tonic-gate uintptr_t vaddr, 17140Sstevel@tonic-gate uint_t *entry, 17150Sstevel@tonic-gate x86pte_t *pte, 17160Sstevel@tonic-gate level_t level) 17170Sstevel@tonic-gate { 17180Sstevel@tonic-gate htable_t *ht; 17190Sstevel@tonic-gate level_t l; 17200Sstevel@tonic-gate uint_t e; 17210Sstevel@tonic-gate 17220Sstevel@tonic-gate ASSERT(level <= mmu.max_page_level); 17230Sstevel@tonic-gate 17240Sstevel@tonic-gate for (l = 0; l <= level; ++l) { 17250Sstevel@tonic-gate ht = htable_lookup(hat, vaddr, l); 17260Sstevel@tonic-gate if (ht == NULL) 17270Sstevel@tonic-gate continue; 17280Sstevel@tonic-gate e = htable_va2entry(vaddr, ht); 17290Sstevel@tonic-gate if (entry != NULL) 17300Sstevel@tonic-gate *entry = e; 17310Sstevel@tonic-gate if (pte != NULL) 17320Sstevel@tonic-gate *pte = x86pte_get(ht, e); 17330Sstevel@tonic-gate return (ht); 17340Sstevel@tonic-gate } 17350Sstevel@tonic-gate return (NULL); 17360Sstevel@tonic-gate } 17370Sstevel@tonic-gate 17380Sstevel@tonic-gate /* 17390Sstevel@tonic-gate * Find the htable and page table entry index of the given virtual address. 17400Sstevel@tonic-gate * There must be a valid page mapped at the given address. 17410Sstevel@tonic-gate * If not found returns NULL. When found, returns the htable, sets 17420Sstevel@tonic-gate * entry, and has a hold on the htable. 17430Sstevel@tonic-gate */ 17440Sstevel@tonic-gate htable_t * 17450Sstevel@tonic-gate htable_getpage(struct hat *hat, uintptr_t vaddr, uint_t *entry) 17460Sstevel@tonic-gate { 17470Sstevel@tonic-gate htable_t *ht; 17480Sstevel@tonic-gate uint_t e; 17490Sstevel@tonic-gate x86pte_t pte; 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate ht = htable_getpte(hat, vaddr, &e, &pte, mmu.max_page_level); 17520Sstevel@tonic-gate if (ht == NULL) 17530Sstevel@tonic-gate return (NULL); 17540Sstevel@tonic-gate 17550Sstevel@tonic-gate if (entry) 17560Sstevel@tonic-gate *entry = e; 17570Sstevel@tonic-gate 17580Sstevel@tonic-gate if (PTE_ISPAGE(pte, ht->ht_level)) 17590Sstevel@tonic-gate return (ht); 17600Sstevel@tonic-gate htable_release(ht); 17610Sstevel@tonic-gate return (NULL); 17620Sstevel@tonic-gate } 17630Sstevel@tonic-gate 17640Sstevel@tonic-gate 17650Sstevel@tonic-gate void 17660Sstevel@tonic-gate htable_init() 17670Sstevel@tonic-gate { 17680Sstevel@tonic-gate /* 17690Sstevel@tonic-gate * To save on kernel VA usage, we avoid debug information in 32 bit 17700Sstevel@tonic-gate * kernels. 17710Sstevel@tonic-gate */ 17720Sstevel@tonic-gate #if defined(__amd64) 17730Sstevel@tonic-gate int kmem_flags = KMC_NOHASH; 17740Sstevel@tonic-gate #elif defined(__i386) 17750Sstevel@tonic-gate int kmem_flags = KMC_NOHASH | KMC_NODEBUG; 17760Sstevel@tonic-gate #endif 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate /* 17790Sstevel@tonic-gate * initialize kmem caches 17800Sstevel@tonic-gate */ 17810Sstevel@tonic-gate htable_cache = kmem_cache_create("htable_t", 17820Sstevel@tonic-gate sizeof (htable_t), 0, NULL, NULL, 17830Sstevel@tonic-gate htable_reap, NULL, hat_memload_arena, kmem_flags); 17840Sstevel@tonic-gate } 17850Sstevel@tonic-gate 17860Sstevel@tonic-gate /* 17870Sstevel@tonic-gate * get the pte index for the virtual address in the given htable's pagetable 17880Sstevel@tonic-gate */ 17890Sstevel@tonic-gate uint_t 17900Sstevel@tonic-gate htable_va2entry(uintptr_t va, htable_t *ht) 17910Sstevel@tonic-gate { 17920Sstevel@tonic-gate level_t l = ht->ht_level; 17930Sstevel@tonic-gate 17940Sstevel@tonic-gate ASSERT(va >= ht->ht_vaddr); 17950Sstevel@tonic-gate ASSERT(va <= HTABLE_LAST_PAGE(ht)); 17963446Smrj return ((va >> LEVEL_SHIFT(l)) & (HTABLE_NUM_PTES(ht) - 1)); 17970Sstevel@tonic-gate } 17980Sstevel@tonic-gate 17990Sstevel@tonic-gate /* 18000Sstevel@tonic-gate * Given an htable and the index of a pte in it, return the virtual address 18010Sstevel@tonic-gate * of the page. 18020Sstevel@tonic-gate */ 18030Sstevel@tonic-gate uintptr_t 18040Sstevel@tonic-gate htable_e2va(htable_t *ht, uint_t entry) 18050Sstevel@tonic-gate { 18060Sstevel@tonic-gate level_t l = ht->ht_level; 18070Sstevel@tonic-gate uintptr_t va; 18080Sstevel@tonic-gate 18093446Smrj ASSERT(entry < HTABLE_NUM_PTES(ht)); 18100Sstevel@tonic-gate va = ht->ht_vaddr + ((uintptr_t)entry << LEVEL_SHIFT(l)); 18110Sstevel@tonic-gate 18120Sstevel@tonic-gate /* 18130Sstevel@tonic-gate * Need to skip over any VA hole in top level table 18140Sstevel@tonic-gate */ 18150Sstevel@tonic-gate #if defined(__amd64) 18160Sstevel@tonic-gate if (ht->ht_level == mmu.max_level && va >= mmu.hole_start) 18170Sstevel@tonic-gate va += ((mmu.hole_end - mmu.hole_start) + 1); 18180Sstevel@tonic-gate #endif 18190Sstevel@tonic-gate 18200Sstevel@tonic-gate return (va); 18210Sstevel@tonic-gate } 18220Sstevel@tonic-gate 18230Sstevel@tonic-gate /* 18240Sstevel@tonic-gate * The code uses compare and swap instructions to read/write PTE's to 18250Sstevel@tonic-gate * avoid atomicity problems, since PTEs can be 8 bytes on 32 bit systems. 18260Sstevel@tonic-gate * will naturally be atomic. 18270Sstevel@tonic-gate * 18280Sstevel@tonic-gate * The combination of using kpreempt_disable()/_enable() and the hci_mutex 18290Sstevel@tonic-gate * are used to ensure that an interrupt won't overwrite a temporary mapping 18300Sstevel@tonic-gate * while it's in use. If an interrupt thread tries to access a PTE, it will 18310Sstevel@tonic-gate * yield briefly back to the pinned thread which holds the cpu's hci_mutex. 18320Sstevel@tonic-gate */ 18330Sstevel@tonic-gate void 18343446Smrj x86pte_cpu_init(cpu_t *cpu) 18350Sstevel@tonic-gate { 18360Sstevel@tonic-gate struct hat_cpu_info *hci; 18370Sstevel@tonic-gate 18383446Smrj hci = kmem_zalloc(sizeof (*hci), KM_SLEEP); 18390Sstevel@tonic-gate mutex_init(&hci->hci_mutex, NULL, MUTEX_DEFAULT, NULL); 18400Sstevel@tonic-gate cpu->cpu_hat_info = hci; 18410Sstevel@tonic-gate } 18420Sstevel@tonic-gate 18433446Smrj void 18443446Smrj x86pte_cpu_fini(cpu_t *cpu) 18453446Smrj { 18463446Smrj struct hat_cpu_info *hci = cpu->cpu_hat_info; 18473446Smrj 18483446Smrj kmem_free(hci, sizeof (*hci)); 18493446Smrj cpu->cpu_hat_info = NULL; 18503446Smrj } 18513446Smrj 18523446Smrj #ifdef __i386 18530Sstevel@tonic-gate /* 18543446Smrj * On 32 bit kernels, loading a 64 bit PTE is a little tricky 18550Sstevel@tonic-gate */ 18563446Smrj x86pte_t 18573446Smrj get_pte64(x86pte_t *ptr) 18583446Smrj { 18593446Smrj volatile uint32_t *p = (uint32_t *)ptr; 18603446Smrj x86pte_t t; 18613446Smrj 18623446Smrj ASSERT(mmu.pae_hat != 0); 18633446Smrj for (;;) { 18643446Smrj t = p[0]; 18653446Smrj t |= (uint64_t)p[1] << 32; 18663446Smrj if ((t & 0xffffffff) == p[0]) 18673446Smrj return (t); 18683446Smrj } 18690Sstevel@tonic-gate } 18703446Smrj #endif /* __i386 */ 18710Sstevel@tonic-gate 18720Sstevel@tonic-gate /* 18730Sstevel@tonic-gate * Disable preemption and establish a mapping to the pagetable with the 18740Sstevel@tonic-gate * given pfn. This is optimized for there case where it's the same 18750Sstevel@tonic-gate * pfn as we last used referenced from this CPU. 18760Sstevel@tonic-gate */ 18770Sstevel@tonic-gate static x86pte_t * 18783446Smrj x86pte_access_pagetable(htable_t *ht, uint_t index) 18790Sstevel@tonic-gate { 18800Sstevel@tonic-gate /* 18810Sstevel@tonic-gate * VLP pagetables are contained in the hat_t 18820Sstevel@tonic-gate */ 18830Sstevel@tonic-gate if (ht->ht_flags & HTABLE_VLP) 18843446Smrj return (PT_INDEX_PTR(ht->ht_hat->hat_vlp_ptes, index)); 18853446Smrj return (x86pte_mapin(ht->ht_pfn, index, ht)); 18863446Smrj } 18870Sstevel@tonic-gate 18883446Smrj /* 18893446Smrj * map the given pfn into the page table window. 18903446Smrj */ 18913446Smrj /*ARGSUSED*/ 18923446Smrj x86pte_t * 18933446Smrj x86pte_mapin(pfn_t pfn, uint_t index, htable_t *ht) 18943446Smrj { 18953446Smrj x86pte_t *pteptr; 18965217Sjosephb x86pte_t pte = 0; 18973446Smrj x86pte_t newpte; 18983446Smrj int x; 18993446Smrj 19000Sstevel@tonic-gate ASSERT(pfn != PFN_INVALID); 19010Sstevel@tonic-gate 19020Sstevel@tonic-gate if (!khat_running) { 19033446Smrj caddr_t va = kbm_remap_window(pfn_to_pa(pfn), 1); 19043446Smrj return (PT_INDEX_PTR(va, index)); 19050Sstevel@tonic-gate } 19060Sstevel@tonic-gate 19070Sstevel@tonic-gate /* 19083446Smrj * If kpm is available, use it. 19093446Smrj */ 19103446Smrj if (kpm_vbase) 19113446Smrj return (PT_INDEX_PTR(hat_kpm_pfn2va(pfn), index)); 19123446Smrj 19133446Smrj /* 19143446Smrj * Disable preemption and grab the CPU's hci_mutex 19150Sstevel@tonic-gate */ 19160Sstevel@tonic-gate kpreempt_disable(); 19173446Smrj ASSERT(CPU->cpu_hat_info != NULL); 19183446Smrj mutex_enter(&CPU->cpu_hat_info->hci_mutex); 19193446Smrj x = PWIN_TABLE(CPU->cpu_id); 19203446Smrj pteptr = (x86pte_t *)PWIN_PTE_VA(x); 19215217Sjosephb #ifndef __xpv 19223446Smrj if (mmu.pae_hat) 19233446Smrj pte = *pteptr; 19243446Smrj else 19253446Smrj pte = *(x86pte32_t *)pteptr; 19265217Sjosephb #endif 19273446Smrj 19283446Smrj newpte = MAKEPTE(pfn, 0) | mmu.pt_global | mmu.pt_nx; 19295084Sjohnlev 19305084Sjohnlev /* 19315084Sjohnlev * For hardware we can use a writable mapping. 19325084Sjohnlev */ 19335084Sjohnlev #ifdef __xpv 19345084Sjohnlev if (IN_XPV_PANIC()) 19355084Sjohnlev #endif 19365084Sjohnlev newpte |= PT_WRITABLE; 19373446Smrj 19383446Smrj if (!PTE_EQUIV(newpte, pte)) { 19395084Sjohnlev 19405084Sjohnlev #ifdef __xpv 19415084Sjohnlev if (!IN_XPV_PANIC()) { 19425084Sjohnlev xen_map(newpte, PWIN_VA(x)); 19435084Sjohnlev } else 19445084Sjohnlev #endif 19455084Sjohnlev { 19465084Sjohnlev XPV_ALLOW_PAGETABLE_UPDATES(); 19475084Sjohnlev if (mmu.pae_hat) 19485084Sjohnlev *pteptr = newpte; 19495084Sjohnlev else 19505084Sjohnlev *(x86pte32_t *)pteptr = newpte; 19515084Sjohnlev XPV_DISALLOW_PAGETABLE_UPDATES(); 19525084Sjohnlev mmu_tlbflush_entry((caddr_t)(PWIN_VA(x))); 19535084Sjohnlev } 19540Sstevel@tonic-gate } 19553446Smrj return (PT_INDEX_PTR(PWIN_VA(x), index)); 19560Sstevel@tonic-gate } 19570Sstevel@tonic-gate 19580Sstevel@tonic-gate /* 19590Sstevel@tonic-gate * Release access to a page table. 19600Sstevel@tonic-gate */ 19610Sstevel@tonic-gate static void 19620Sstevel@tonic-gate x86pte_release_pagetable(htable_t *ht) 19630Sstevel@tonic-gate { 19640Sstevel@tonic-gate /* 19650Sstevel@tonic-gate * nothing to do for VLP htables 19660Sstevel@tonic-gate */ 19670Sstevel@tonic-gate if (ht->ht_flags & HTABLE_VLP) 19680Sstevel@tonic-gate return; 19690Sstevel@tonic-gate 19703446Smrj x86pte_mapout(); 19713446Smrj } 19723446Smrj 19733446Smrj void 19743446Smrj x86pte_mapout(void) 19753446Smrj { 19765084Sjohnlev if (kpm_vbase != NULL || !khat_running) 19770Sstevel@tonic-gate return; 19780Sstevel@tonic-gate 19790Sstevel@tonic-gate /* 19803446Smrj * Drop the CPU's hci_mutex and restore preemption. 19810Sstevel@tonic-gate */ 19825217Sjosephb #ifdef __xpv 19835217Sjosephb if (!IN_XPV_PANIC()) { 19845217Sjosephb uintptr_t va; 19855217Sjosephb 19865217Sjosephb /* 19875217Sjosephb * We need to always clear the mapping in case a page 19885217Sjosephb * that was once a page table page is ballooned out. 19895217Sjosephb */ 19905217Sjosephb va = (uintptr_t)PWIN_VA(PWIN_TABLE(CPU->cpu_id)); 19915217Sjosephb (void) HYPERVISOR_update_va_mapping(va, 0, 19925217Sjosephb UVMF_INVLPG | UVMF_LOCAL); 19935217Sjosephb } 19945217Sjosephb #endif 19953446Smrj mutex_exit(&CPU->cpu_hat_info->hci_mutex); 19960Sstevel@tonic-gate kpreempt_enable(); 19970Sstevel@tonic-gate } 19980Sstevel@tonic-gate 19990Sstevel@tonic-gate /* 20000Sstevel@tonic-gate * Atomic retrieval of a pagetable entry 20010Sstevel@tonic-gate */ 20020Sstevel@tonic-gate x86pte_t 20030Sstevel@tonic-gate x86pte_get(htable_t *ht, uint_t entry) 20040Sstevel@tonic-gate { 20050Sstevel@tonic-gate x86pte_t pte; 200647Sjosephb x86pte_t *ptep; 20070Sstevel@tonic-gate 20080Sstevel@tonic-gate /* 200947Sjosephb * Be careful that loading PAE entries in 32 bit kernel is atomic. 20100Sstevel@tonic-gate */ 20113446Smrj ASSERT(entry < mmu.ptes_per_table); 20123446Smrj ptep = x86pte_access_pagetable(ht, entry); 20133446Smrj pte = GET_PTE(ptep); 20140Sstevel@tonic-gate x86pte_release_pagetable(ht); 20150Sstevel@tonic-gate return (pte); 20160Sstevel@tonic-gate } 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate /* 20190Sstevel@tonic-gate * Atomic unconditional set of a page table entry, it returns the previous 20203446Smrj * value. For pre-existing mappings if the PFN changes, then we don't care 20213446Smrj * about the old pte's REF / MOD bits. If the PFN remains the same, we leave 20223446Smrj * the MOD/REF bits unchanged. 20233446Smrj * 20243446Smrj * If asked to overwrite a link to a lower page table with a large page 20253446Smrj * mapping, this routine returns the special value of LPAGE_ERROR. This 20263446Smrj * allows the upper HAT layers to retry with a smaller mapping size. 20270Sstevel@tonic-gate */ 20280Sstevel@tonic-gate x86pte_t 20290Sstevel@tonic-gate x86pte_set(htable_t *ht, uint_t entry, x86pte_t new, void *ptr) 20300Sstevel@tonic-gate { 20310Sstevel@tonic-gate x86pte_t old; 20323446Smrj x86pte_t prev; 20330Sstevel@tonic-gate x86pte_t *ptep; 20343446Smrj level_t l = ht->ht_level; 20353446Smrj x86pte_t pfn_mask = (l != 0) ? PT_PADDR_LGPG : PT_PADDR; 20363446Smrj x86pte_t n; 20373446Smrj uintptr_t addr = htable_e2va(ht, entry); 20383446Smrj hat_t *hat = ht->ht_hat; 20390Sstevel@tonic-gate 20403446Smrj ASSERT(new != 0); /* don't use to invalidate a PTE, see x86pte_update */ 20410Sstevel@tonic-gate ASSERT(!(ht->ht_flags & HTABLE_SHARED_PFN)); 20423446Smrj if (ptr == NULL) 20433446Smrj ptep = x86pte_access_pagetable(ht, entry); 20443446Smrj else 20450Sstevel@tonic-gate ptep = ptr; 20463446Smrj 20473446Smrj /* 20483446Smrj * Install the new PTE. If remapping the same PFN, then 20493446Smrj * copy existing REF/MOD bits to new mapping. 20503446Smrj */ 20513446Smrj do { 20523446Smrj prev = GET_PTE(ptep); 20533446Smrj n = new; 20543446Smrj if (PTE_ISVALID(n) && (prev & pfn_mask) == (new & pfn_mask)) 20553446Smrj n |= prev & (PT_REF | PT_MOD); 20560Sstevel@tonic-gate 20573446Smrj /* 20583446Smrj * Another thread may have installed this mapping already, 20593446Smrj * flush the local TLB and be done. 20603446Smrj */ 20613446Smrj if (prev == n) { 20623446Smrj old = new; 20635084Sjohnlev #ifdef __xpv 20645084Sjohnlev if (!IN_XPV_PANIC()) 20655084Sjohnlev xen_flush_va((caddr_t)addr); 20665084Sjohnlev else 20675084Sjohnlev #endif 20685084Sjohnlev mmu_tlbflush_entry((caddr_t)addr); 20693446Smrj goto done; 20700Sstevel@tonic-gate } 20713446Smrj 20723446Smrj /* 20733446Smrj * Detect if we have a collision of installing a large 20743446Smrj * page mapping where there already is a lower page table. 20753446Smrj */ 20763543Sjosephb if (l > 0 && (prev & PT_VALID) && !(prev & PT_PAGESIZE)) { 20773543Sjosephb old = LPAGE_ERROR; 20783543Sjosephb goto done; 20793543Sjosephb } 20803446Smrj 20815084Sjohnlev XPV_ALLOW_PAGETABLE_UPDATES(); 20823446Smrj old = CAS_PTE(ptep, prev, n); 20835084Sjohnlev XPV_DISALLOW_PAGETABLE_UPDATES(); 20843446Smrj } while (old != prev); 20853446Smrj 20863446Smrj /* 20873446Smrj * Do a TLB demap if needed, ie. the old pte was valid. 20883446Smrj * 20893446Smrj * Note that a stale TLB writeback to the PTE here either can't happen 20903446Smrj * or doesn't matter. The PFN can only change for NOSYNC|NOCONSIST 20913446Smrj * mappings, but they were created with REF and MOD already set, so 20923446Smrj * no stale writeback will happen. 20933446Smrj * 20943446Smrj * Segmap is the only place where remaps happen on the same pfn and for 20953446Smrj * that we want to preserve the stale REF/MOD bits. 20963446Smrj */ 20973446Smrj if (old & PT_REF) 20983446Smrj hat_tlb_inval(hat, addr); 20993446Smrj 21003446Smrj done: 21010Sstevel@tonic-gate if (ptr == NULL) 21020Sstevel@tonic-gate x86pte_release_pagetable(ht); 21030Sstevel@tonic-gate return (old); 21040Sstevel@tonic-gate } 21050Sstevel@tonic-gate 21060Sstevel@tonic-gate /* 21073446Smrj * Atomic compare and swap of a page table entry. No TLB invalidates are done. 21083446Smrj * This is used for links between pagetables of different levels. 21093446Smrj * Note we always create these links with dirty/access set, so they should 21103446Smrj * never change. 21110Sstevel@tonic-gate */ 21123446Smrj x86pte_t 21130Sstevel@tonic-gate x86pte_cas(htable_t *ht, uint_t entry, x86pte_t old, x86pte_t new) 21140Sstevel@tonic-gate { 21150Sstevel@tonic-gate x86pte_t pte; 21160Sstevel@tonic-gate x86pte_t *ptep; 21175084Sjohnlev #ifdef __xpv 21185084Sjohnlev /* 21195084Sjohnlev * We can't use writable pagetables for upper level tables, so fake it. 21205084Sjohnlev */ 21215084Sjohnlev mmu_update_t t[2]; 21225084Sjohnlev int cnt = 1; 21235084Sjohnlev int count; 21245084Sjohnlev maddr_t ma; 21250Sstevel@tonic-gate 21265084Sjohnlev if (!IN_XPV_PANIC()) { 21275084Sjohnlev ASSERT(!(ht->ht_flags & HTABLE_VLP)); /* no VLP yet */ 21285084Sjohnlev ma = pa_to_ma(PT_INDEX_PHYSADDR(pfn_to_pa(ht->ht_pfn), entry)); 21295084Sjohnlev t[0].ptr = ma | MMU_NORMAL_PT_UPDATE; 21305084Sjohnlev t[0].val = new; 21315084Sjohnlev 21325084Sjohnlev #if defined(__amd64) 21335084Sjohnlev /* 21345084Sjohnlev * On the 64-bit hypervisor we need to maintain the user mode 21355084Sjohnlev * top page table too. 21365084Sjohnlev */ 21375084Sjohnlev if (ht->ht_level == mmu.max_level && ht->ht_hat != kas.a_hat) { 21385084Sjohnlev ma = pa_to_ma(PT_INDEX_PHYSADDR(pfn_to_pa( 21395084Sjohnlev ht->ht_hat->hat_user_ptable), entry)); 21405084Sjohnlev t[1].ptr = ma | MMU_NORMAL_PT_UPDATE; 21415084Sjohnlev t[1].val = new; 21425084Sjohnlev ++cnt; 21435084Sjohnlev } 21445084Sjohnlev #endif /* __amd64 */ 21455084Sjohnlev 21465084Sjohnlev if (HYPERVISOR_mmu_update(t, cnt, &count, DOMID_SELF)) 21475084Sjohnlev panic("HYPERVISOR_mmu_update() failed"); 21485084Sjohnlev ASSERT(count == cnt); 21495084Sjohnlev return (old); 21505084Sjohnlev } 21515084Sjohnlev #endif 21523446Smrj ptep = x86pte_access_pagetable(ht, entry); 21535084Sjohnlev XPV_ALLOW_PAGETABLE_UPDATES(); 21543446Smrj pte = CAS_PTE(ptep, old, new); 21555084Sjohnlev XPV_DISALLOW_PAGETABLE_UPDATES(); 21560Sstevel@tonic-gate x86pte_release_pagetable(ht); 21570Sstevel@tonic-gate return (pte); 21580Sstevel@tonic-gate } 21590Sstevel@tonic-gate 21600Sstevel@tonic-gate /* 21613446Smrj * Invalidate a page table entry as long as it currently maps something that 21623446Smrj * matches the value determined by expect. 21633446Smrj * 21643446Smrj * Also invalidates any TLB entries and returns the previous value of the PTE. 21650Sstevel@tonic-gate */ 21663446Smrj x86pte_t 21673446Smrj x86pte_inval( 21683446Smrj htable_t *ht, 21693446Smrj uint_t entry, 21703446Smrj x86pte_t expect, 21713446Smrj x86pte_t *pte_ptr) 21720Sstevel@tonic-gate { 21733446Smrj x86pte_t *ptep; 21744191Sjosephb x86pte_t oldpte; 21754191Sjosephb x86pte_t found; 21760Sstevel@tonic-gate 21773446Smrj ASSERT(!(ht->ht_flags & HTABLE_SHARED_PFN)); 21783446Smrj ASSERT(ht->ht_level != VLP_LEVEL); 21793543Sjosephb 21803446Smrj if (pte_ptr != NULL) 21813446Smrj ptep = pte_ptr; 21823446Smrj else 21833446Smrj ptep = x86pte_access_pagetable(ht, entry); 21840Sstevel@tonic-gate 21855084Sjohnlev #if defined(__xpv) 21865084Sjohnlev /* 21875084Sjohnlev * If exit()ing just use HYPERVISOR_mmu_update(), as we can't be racing 21885084Sjohnlev * with anything else. 21895084Sjohnlev */ 21905084Sjohnlev if ((ht->ht_hat->hat_flags & HAT_FREEING) && !IN_XPV_PANIC()) { 21915084Sjohnlev int count; 21925084Sjohnlev mmu_update_t t[1]; 21935084Sjohnlev maddr_t ma; 21945084Sjohnlev 21955084Sjohnlev oldpte = GET_PTE(ptep); 21965084Sjohnlev if (expect != 0 && (oldpte & PT_PADDR) != (expect & PT_PADDR)) 21975084Sjohnlev goto done; 21985084Sjohnlev ma = pa_to_ma(PT_INDEX_PHYSADDR(pfn_to_pa(ht->ht_pfn), entry)); 21995084Sjohnlev t[0].ptr = ma | MMU_NORMAL_PT_UPDATE; 22005084Sjohnlev t[0].val = 0; 22015084Sjohnlev if (HYPERVISOR_mmu_update(t, 1, &count, DOMID_SELF)) 22025084Sjohnlev panic("HYPERVISOR_mmu_update() failed"); 22035084Sjohnlev ASSERT(count == 1); 22045084Sjohnlev goto done; 22055084Sjohnlev } 22065084Sjohnlev #endif /* __xpv */ 22075084Sjohnlev 22080Sstevel@tonic-gate /* 22093543Sjosephb * Note that the loop is needed to handle changes due to h/w updating 22103543Sjosephb * of PT_MOD/PT_REF. 22110Sstevel@tonic-gate */ 22123446Smrj do { 22134191Sjosephb oldpte = GET_PTE(ptep); 22144191Sjosephb if (expect != 0 && (oldpte & PT_PADDR) != (expect & PT_PADDR)) 22154191Sjosephb goto done; 22165084Sjohnlev XPV_ALLOW_PAGETABLE_UPDATES(); 22174191Sjosephb found = CAS_PTE(ptep, oldpte, 0); 22185084Sjohnlev XPV_DISALLOW_PAGETABLE_UPDATES(); 22194191Sjosephb } while (found != oldpte); 22204191Sjosephb if (oldpte & (PT_REF | PT_MOD)) 22214191Sjosephb hat_tlb_inval(ht->ht_hat, htable_e2va(ht, entry)); 22220Sstevel@tonic-gate 22234191Sjosephb done: 22243446Smrj if (pte_ptr == NULL) 22253446Smrj x86pte_release_pagetable(ht); 22264191Sjosephb return (oldpte); 22270Sstevel@tonic-gate } 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate /* 22303446Smrj * Change a page table entry af it currently matches the value in expect. 22310Sstevel@tonic-gate */ 22320Sstevel@tonic-gate x86pte_t 22333446Smrj x86pte_update( 22343446Smrj htable_t *ht, 22353446Smrj uint_t entry, 22363446Smrj x86pte_t expect, 22373446Smrj x86pte_t new) 22380Sstevel@tonic-gate { 22390Sstevel@tonic-gate x86pte_t *ptep; 22403446Smrj x86pte_t found; 22410Sstevel@tonic-gate 22423446Smrj ASSERT(new != 0); 22433446Smrj ASSERT(!(ht->ht_flags & HTABLE_SHARED_PFN)); 22443446Smrj ASSERT(ht->ht_level != VLP_LEVEL); 22450Sstevel@tonic-gate 22463446Smrj ptep = x86pte_access_pagetable(ht, entry); 22475084Sjohnlev XPV_ALLOW_PAGETABLE_UPDATES(); 22483446Smrj found = CAS_PTE(ptep, expect, new); 22495084Sjohnlev XPV_DISALLOW_PAGETABLE_UPDATES(); 22503446Smrj if (found == expect) { 22513446Smrj hat_tlb_inval(ht->ht_hat, htable_e2va(ht, entry)); 22520Sstevel@tonic-gate 22533446Smrj /* 22543446Smrj * When removing write permission *and* clearing the 22553446Smrj * MOD bit, check if a write happened via a stale 22563446Smrj * TLB entry before the TLB shootdown finished. 22573446Smrj * 22583446Smrj * If it did happen, simply re-enable write permission and 22593446Smrj * act like the original CAS failed. 22603446Smrj */ 22613446Smrj if ((expect & (PT_WRITABLE | PT_MOD)) == PT_WRITABLE && 22623446Smrj (new & (PT_WRITABLE | PT_MOD)) == 0 && 22633446Smrj (GET_PTE(ptep) & PT_MOD) != 0) { 22643446Smrj do { 22653446Smrj found = GET_PTE(ptep); 22665084Sjohnlev XPV_ALLOW_PAGETABLE_UPDATES(); 22673446Smrj found = 22683446Smrj CAS_PTE(ptep, found, found | PT_WRITABLE); 22695084Sjohnlev XPV_DISALLOW_PAGETABLE_UPDATES(); 22703446Smrj } while ((found & PT_WRITABLE) == 0); 22713446Smrj } 22723446Smrj } 22730Sstevel@tonic-gate x86pte_release_pagetable(ht); 22743446Smrj return (found); 22750Sstevel@tonic-gate } 22760Sstevel@tonic-gate 22775084Sjohnlev #ifndef __xpv 22780Sstevel@tonic-gate /* 22790Sstevel@tonic-gate * Copy page tables - this is just a little more complicated than the 22800Sstevel@tonic-gate * previous routines. Note that it's also not atomic! It also is never 22810Sstevel@tonic-gate * used for VLP pagetables. 22820Sstevel@tonic-gate */ 22830Sstevel@tonic-gate void 22840Sstevel@tonic-gate x86pte_copy(htable_t *src, htable_t *dest, uint_t entry, uint_t count) 22850Sstevel@tonic-gate { 22860Sstevel@tonic-gate caddr_t src_va; 22870Sstevel@tonic-gate caddr_t dst_va; 22880Sstevel@tonic-gate size_t size; 22893446Smrj x86pte_t *pteptr; 22903446Smrj x86pte_t pte; 22910Sstevel@tonic-gate 22920Sstevel@tonic-gate ASSERT(khat_running); 22930Sstevel@tonic-gate ASSERT(!(dest->ht_flags & HTABLE_VLP)); 22940Sstevel@tonic-gate ASSERT(!(src->ht_flags & HTABLE_VLP)); 22950Sstevel@tonic-gate ASSERT(!(src->ht_flags & HTABLE_SHARED_PFN)); 22960Sstevel@tonic-gate ASSERT(!(dest->ht_flags & HTABLE_SHARED_PFN)); 22970Sstevel@tonic-gate 22980Sstevel@tonic-gate /* 22993446Smrj * Acquire access to the CPU pagetable windows for the dest and source. 23000Sstevel@tonic-gate */ 23013446Smrj dst_va = (caddr_t)x86pte_access_pagetable(dest, entry); 23023446Smrj if (kpm_vbase) { 23033446Smrj src_va = (caddr_t) 23043446Smrj PT_INDEX_PTR(hat_kpm_pfn2va(src->ht_pfn), entry); 23050Sstevel@tonic-gate } else { 23063446Smrj uint_t x = PWIN_SRC(CPU->cpu_id); 23070Sstevel@tonic-gate 23080Sstevel@tonic-gate /* 23090Sstevel@tonic-gate * Finish defining the src pagetable mapping 23100Sstevel@tonic-gate */ 23113446Smrj src_va = (caddr_t)PT_INDEX_PTR(PWIN_VA(x), entry); 23123446Smrj pte = MAKEPTE(src->ht_pfn, 0) | mmu.pt_global | mmu.pt_nx; 23133446Smrj pteptr = (x86pte_t *)PWIN_PTE_VA(x); 23143446Smrj if (mmu.pae_hat) 23153446Smrj *pteptr = pte; 23163446Smrj else 23173446Smrj *(x86pte32_t *)pteptr = pte; 23183446Smrj mmu_tlbflush_entry((caddr_t)(PWIN_VA(x))); 23190Sstevel@tonic-gate } 23200Sstevel@tonic-gate 23210Sstevel@tonic-gate /* 23220Sstevel@tonic-gate * now do the copy 23230Sstevel@tonic-gate */ 23240Sstevel@tonic-gate size = count << mmu.pte_size_shift; 23250Sstevel@tonic-gate bcopy(src_va, dst_va, size); 23260Sstevel@tonic-gate 23270Sstevel@tonic-gate x86pte_release_pagetable(dest); 23280Sstevel@tonic-gate } 23290Sstevel@tonic-gate 23305084Sjohnlev #else /* __xpv */ 23315084Sjohnlev 23325084Sjohnlev /* 23335084Sjohnlev * The hypervisor only supports writable pagetables at level 0, so we have 23345084Sjohnlev * to install these 1 by 1 the slow way. 23355084Sjohnlev */ 23365084Sjohnlev void 23375084Sjohnlev x86pte_copy(htable_t *src, htable_t *dest, uint_t entry, uint_t count) 23385084Sjohnlev { 23395084Sjohnlev caddr_t src_va; 23405084Sjohnlev x86pte_t pte; 23415084Sjohnlev 23425084Sjohnlev ASSERT(!IN_XPV_PANIC()); 23435084Sjohnlev src_va = (caddr_t)x86pte_access_pagetable(src, entry); 23445084Sjohnlev while (count) { 23455084Sjohnlev if (mmu.pae_hat) 23465084Sjohnlev pte = *(x86pte_t *)src_va; 23475084Sjohnlev else 23485084Sjohnlev pte = *(x86pte32_t *)src_va; 23495084Sjohnlev if (pte != 0) { 23505084Sjohnlev set_pteval(pfn_to_pa(dest->ht_pfn), entry, 23515084Sjohnlev dest->ht_level, pte); 23525084Sjohnlev #ifdef __amd64 23535084Sjohnlev if (dest->ht_level == mmu.max_level && 23545084Sjohnlev htable_e2va(dest, entry) < HYPERVISOR_VIRT_END) 23555084Sjohnlev set_pteval( 23565084Sjohnlev pfn_to_pa(dest->ht_hat->hat_user_ptable), 23575084Sjohnlev entry, dest->ht_level, pte); 23585084Sjohnlev #endif 23595084Sjohnlev } 23605084Sjohnlev --count; 23615084Sjohnlev ++entry; 23625084Sjohnlev src_va += mmu.pte_size; 23635084Sjohnlev } 23645084Sjohnlev x86pte_release_pagetable(src); 23655084Sjohnlev } 23665084Sjohnlev #endif /* __xpv */ 23675084Sjohnlev 23680Sstevel@tonic-gate /* 23690Sstevel@tonic-gate * Zero page table entries - Note this doesn't use atomic stores! 23700Sstevel@tonic-gate */ 23713446Smrj static void 23720Sstevel@tonic-gate x86pte_zero(htable_t *dest, uint_t entry, uint_t count) 23730Sstevel@tonic-gate { 23740Sstevel@tonic-gate caddr_t dst_va; 23750Sstevel@tonic-gate size_t size; 23765084Sjohnlev #ifdef __xpv 23775084Sjohnlev int x; 23785084Sjohnlev x86pte_t newpte; 23795084Sjohnlev #endif 23800Sstevel@tonic-gate 23810Sstevel@tonic-gate /* 23820Sstevel@tonic-gate * Map in the page table to be zeroed. 23830Sstevel@tonic-gate */ 23840Sstevel@tonic-gate ASSERT(!(dest->ht_flags & HTABLE_SHARED_PFN)); 23850Sstevel@tonic-gate ASSERT(!(dest->ht_flags & HTABLE_VLP)); 23863446Smrj 23875084Sjohnlev /* 23885084Sjohnlev * On the hypervisor we don't use x86pte_access_pagetable() since 23895084Sjohnlev * in this case the page is not pinned yet. 23905084Sjohnlev */ 23915084Sjohnlev #ifdef __xpv 23925084Sjohnlev if (kpm_vbase == NULL) { 23935084Sjohnlev kpreempt_disable(); 23945084Sjohnlev ASSERT(CPU->cpu_hat_info != NULL); 23955084Sjohnlev mutex_enter(&CPU->cpu_hat_info->hci_mutex); 23965084Sjohnlev x = PWIN_TABLE(CPU->cpu_id); 23975084Sjohnlev newpte = MAKEPTE(dest->ht_pfn, 0) | PT_WRITABLE; 23985084Sjohnlev xen_map(newpte, PWIN_VA(x)); 23995084Sjohnlev dst_va = (caddr_t)PT_INDEX_PTR(PWIN_VA(x), entry); 24005084Sjohnlev } else 24015084Sjohnlev #endif 24025084Sjohnlev dst_va = (caddr_t)x86pte_access_pagetable(dest, entry); 24033446Smrj 24040Sstevel@tonic-gate size = count << mmu.pte_size_shift; 24053446Smrj ASSERT(size > BLOCKZEROALIGN); 24063446Smrj #ifdef __i386 24073446Smrj if ((x86_feature & X86_SSE2) == 0) 24080Sstevel@tonic-gate bzero(dst_va, size); 24093446Smrj else 24103446Smrj #endif 24113446Smrj block_zero_no_xmm(dst_va, size); 24123446Smrj 24135084Sjohnlev #ifdef __xpv 24145084Sjohnlev if (kpm_vbase == NULL) { 24155084Sjohnlev xen_map(0, PWIN_VA(x)); 24165084Sjohnlev mutex_exit(&CPU->cpu_hat_info->hci_mutex); 24175084Sjohnlev kpreempt_enable(); 24185084Sjohnlev } else 24195084Sjohnlev #endif 24205084Sjohnlev x86pte_release_pagetable(dest); 24210Sstevel@tonic-gate } 24220Sstevel@tonic-gate 24230Sstevel@tonic-gate /* 24240Sstevel@tonic-gate * Called to ensure that all pagetables are in the system dump 24250Sstevel@tonic-gate */ 24260Sstevel@tonic-gate void 24270Sstevel@tonic-gate hat_dump(void) 24280Sstevel@tonic-gate { 24290Sstevel@tonic-gate hat_t *hat; 24300Sstevel@tonic-gate uint_t h; 24310Sstevel@tonic-gate htable_t *ht; 24320Sstevel@tonic-gate 24330Sstevel@tonic-gate /* 24341747Sjosephb * Dump all page tables 24350Sstevel@tonic-gate */ 24361747Sjosephb for (hat = kas.a_hat; hat != NULL; hat = hat->hat_next) { 24370Sstevel@tonic-gate for (h = 0; h < hat->hat_num_hash; ++h) { 24380Sstevel@tonic-gate for (ht = hat->hat_ht_hash[h]; ht; ht = ht->ht_next) { 24391747Sjosephb if ((ht->ht_flags & HTABLE_VLP) == 0) 24400Sstevel@tonic-gate dump_page(ht->ht_pfn); 24410Sstevel@tonic-gate } 24420Sstevel@tonic-gate } 24430Sstevel@tonic-gate } 24440Sstevel@tonic-gate } 2445