17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #ifndef _VM_HTABLE_H 287c478bd9Sstevel@tonic-gate #define _VM_HTABLE_H 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #ifdef __cplusplus 337c478bd9Sstevel@tonic-gate extern "C" { 347c478bd9Sstevel@tonic-gate #endif 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #if defined(__GNUC__) && defined(_ASM_INLINES) && defined(_KERNEL) 377c478bd9Sstevel@tonic-gate #include <asm/htable.h> 387c478bd9Sstevel@tonic-gate #endif 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate extern void atomic_andb(uint8_t *addr, uint8_t value); 417c478bd9Sstevel@tonic-gate extern void atomic_orb(uint8_t *addr, uint8_t value); 427c478bd9Sstevel@tonic-gate extern void atomic_inc16(uint16_t *addr); 437c478bd9Sstevel@tonic-gate extern void atomic_dec16(uint16_t *addr); 447c478bd9Sstevel@tonic-gate extern void mmu_tlbflush_entry(caddr_t addr); 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* 477c478bd9Sstevel@tonic-gate * Each hardware page table has an htable_t describing it. 487c478bd9Sstevel@tonic-gate * 497c478bd9Sstevel@tonic-gate * We use a reference counter mechanism to detect when we can free an htable. 507c478bd9Sstevel@tonic-gate * In the implmentation the reference count is split into 2 separate counters: 517c478bd9Sstevel@tonic-gate * 527c478bd9Sstevel@tonic-gate * ht_busy is a traditional reference count of uses of the htable pointer 537c478bd9Sstevel@tonic-gate * 547c478bd9Sstevel@tonic-gate * ht_valid_cnt is a count of how references are implied by valid PTE/PTP 557c478bd9Sstevel@tonic-gate * entries in the pagetable 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * ht_busy is only incremented by htable_lookup() or htable_create() 587c478bd9Sstevel@tonic-gate * while holding the appropriate hash_table mutex. While installing a new 597c478bd9Sstevel@tonic-gate * valid PTE or PTP, in order to increment ht_valid_cnt a thread must have 607c478bd9Sstevel@tonic-gate * done an htable_lookup() or htable_create() but not the htable_release yet. 617c478bd9Sstevel@tonic-gate * 627c478bd9Sstevel@tonic-gate * htable_release(), while holding the mutex, can know that if 637c478bd9Sstevel@tonic-gate * busy == 1 and valid_cnt == 0, the htable can be free'd. 647c478bd9Sstevel@tonic-gate * 657c478bd9Sstevel@tonic-gate * The fields have been ordered to make htable_lookup() fast. Hence, 667c478bd9Sstevel@tonic-gate * ht_hat, ht_vaddr, ht_level and ht_next need to be clustered together. 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate struct htable { 697c478bd9Sstevel@tonic-gate struct htable *ht_next; /* forward link for hash table */ 707c478bd9Sstevel@tonic-gate struct hat *ht_hat; /* hat this mapping comes from */ 717c478bd9Sstevel@tonic-gate uintptr_t ht_vaddr; /* virt addr at start of this table */ 727c478bd9Sstevel@tonic-gate level_t ht_level; /* page table level: 0=4K, 1=2M, ... */ 737c478bd9Sstevel@tonic-gate uint16_t ht_flags; /* see below */ 747c478bd9Sstevel@tonic-gate int16_t ht_busy; /* implements locking protocol */ 757c478bd9Sstevel@tonic-gate uint16_t ht_num_ptes; /* # of PTEs in page table */ 767c478bd9Sstevel@tonic-gate int16_t ht_valid_cnt; /* # of valid entries in this table */ 777c478bd9Sstevel@tonic-gate uint32_t ht_lock_cnt; /* # of locked entries in this table */ 787c478bd9Sstevel@tonic-gate /* never used for kernel hat */ 797c478bd9Sstevel@tonic-gate pfn_t ht_pfn; /* pfn of page of the pagetable */ 807c478bd9Sstevel@tonic-gate struct htable *ht_prev; /* backward link for hash table */ 817c478bd9Sstevel@tonic-gate struct htable *ht_parent; /* htable that points to this htable */ 827c478bd9Sstevel@tonic-gate struct htable *ht_shares; /* for HTABLE_SHARED_PFN only */ 837c478bd9Sstevel@tonic-gate }; 847c478bd9Sstevel@tonic-gate typedef struct htable htable_t; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * Flags values for htable ht_flags field: 887c478bd9Sstevel@tonic-gate * 897c478bd9Sstevel@tonic-gate * HTABLE_VLP - this is the top level htable of a VLP HAT. 907c478bd9Sstevel@tonic-gate * 917c478bd9Sstevel@tonic-gate * HTABLE_SHARED_PFN - this htable had it's PFN assigned from sharing another 927c478bd9Sstevel@tonic-gate * htable. Used by hat_share() for ISM. 937c478bd9Sstevel@tonic-gate */ 947c478bd9Sstevel@tonic-gate #define HTABLE_VLP (0x0001) 957c478bd9Sstevel@tonic-gate #define HTABLE_SHARED_PFN (0x0002) 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * The htable hash table hashing function. The 28 is so that high 997c478bd9Sstevel@tonic-gate * order bits are include in the hash index to skew the wrap 100*7c8868c1Sjosephb * around of addresses. Even though the hash buckets are stored per 101*7c8868c1Sjosephb * hat we include the value of hat pointer in the hash function so 102*7c8868c1Sjosephb * that the secondary hash for the htable mutex winds up begin different in 103*7c8868c1Sjosephb * every address space. 1047c478bd9Sstevel@tonic-gate */ 1057c478bd9Sstevel@tonic-gate #define HTABLE_HASH(hat, va, lvl) \ 106*7c8868c1Sjosephb ((((va) >> LEVEL_SHIFT(1)) + ((va) >> 28) + (lvl) + \ 107*7c8868c1Sjosephb ((uintptr_t)(hat) >> 4)) & ((hat)->hat_num_hash - 1)) 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate /* 1107c478bd9Sstevel@tonic-gate * For 32 bit, access to page table entries is done via the page table's PFN and 1117c478bd9Sstevel@tonic-gate * the index of the PTE. We use a CPU specific mapping (a la ppcopy) to map 1127c478bd9Sstevel@tonic-gate * in page tables on an "as needed" basis. 1137c478bd9Sstevel@tonic-gate * 1147c478bd9Sstevel@tonic-gate * 64 bit kernels will use seg_kpm style mappings and avoid any overhead. 1157c478bd9Sstevel@tonic-gate * 1167c478bd9Sstevel@tonic-gate * The code uses compare and swap instructions to read/write PTE's to 1177c478bd9Sstevel@tonic-gate * avoid atomicity problems, since PTEs can be 8 bytes on 32 bit systems. 1187c478bd9Sstevel@tonic-gate * Again this can be optimized on 64 bit systems, since aligned load/store 1197c478bd9Sstevel@tonic-gate * will naturally be atomic. 1207c478bd9Sstevel@tonic-gate * 1217c478bd9Sstevel@tonic-gate * Each CPU gets a unique hat_cpu_info structure in cpu_hat_info. 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate struct hat_cpu_info { 1247c478bd9Sstevel@tonic-gate pfn_t hci_mapped_pfn; /* pfn of currently mapped page table */ 1257c478bd9Sstevel@tonic-gate x86pte_t *hci_pagetable_va; /* VA to use for mappings */ 1267c478bd9Sstevel@tonic-gate x86pte_t *hci_kernel_pte; /* kernel PTE for cpu_pagetable_va */ 1277c478bd9Sstevel@tonic-gate kmutex_t hci_mutex; /* mutex to ensure sequential usage */ 1287c478bd9Sstevel@tonic-gate #if defined(__amd64) 1297c478bd9Sstevel@tonic-gate pfn_t hci_vlp_pfn; /* pfn of hci_vlp_l3ptes */ 1307c478bd9Sstevel@tonic-gate x86pte_t *hci_vlp_l3ptes; /* VLP Level==3 pagetable (top) */ 1317c478bd9Sstevel@tonic-gate x86pte_t *hci_vlp_l2ptes; /* VLP Level==2 pagetable */ 1327c478bd9Sstevel@tonic-gate #endif /* __amd64 */ 1337c478bd9Sstevel@tonic-gate }; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate /* 1377c478bd9Sstevel@tonic-gate * Compute the last page aligned VA mapped by an htable. 1387c478bd9Sstevel@tonic-gate * 1397c478bd9Sstevel@tonic-gate * Given a va and a level, compute the virtual address of the start of the 1407c478bd9Sstevel@tonic-gate * next page at that level. 1417c478bd9Sstevel@tonic-gate * 1427c478bd9Sstevel@tonic-gate * XX64 - The check for the VA hole needs to be better generalized. 1437c478bd9Sstevel@tonic-gate */ 1447c478bd9Sstevel@tonic-gate #if defined(__amd64) 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate #define HTABLE_LAST_PAGE(ht) \ 1477c478bd9Sstevel@tonic-gate ((ht)->ht_level == mmu.max_level ? ((uintptr_t)0UL - MMU_PAGESIZE) :\ 1487c478bd9Sstevel@tonic-gate ((ht)->ht_vaddr - MMU_PAGESIZE + \ 1497c478bd9Sstevel@tonic-gate ((uintptr_t)((ht)->ht_num_ptes) << LEVEL_SHIFT((ht)->ht_level)))) 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate #define NEXT_ENTRY_VA(va, l) \ 1527c478bd9Sstevel@tonic-gate ((va & LEVEL_MASK(l)) + LEVEL_SIZE(l) == mmu.hole_start ? \ 1537c478bd9Sstevel@tonic-gate mmu.hole_end : (va & LEVEL_MASK(l)) + LEVEL_SIZE(l)) 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate #elif defined(__i386) 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate #define HTABLE_LAST_PAGE(ht) ((ht)->ht_vaddr - MMU_PAGESIZE + \ 1587c478bd9Sstevel@tonic-gate ((uintptr_t)((ht)->ht_num_ptes) << LEVEL_SHIFT((ht)->ht_level))) 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate #define NEXT_ENTRY_VA(va, l) ((va & LEVEL_MASK(l)) + LEVEL_SIZE(l)) 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate #endif 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate #if defined(_KERNEL) 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* 1677c478bd9Sstevel@tonic-gate * initialization function called from hat_init() 1687c478bd9Sstevel@tonic-gate */ 1697c478bd9Sstevel@tonic-gate extern void htable_init(void); 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate /* 1727c478bd9Sstevel@tonic-gate * Functions to lookup, or "lookup and create", the htable corresponding 1737c478bd9Sstevel@tonic-gate * to the virtual address "vaddr" in the "hat" at the given "level" of 1747c478bd9Sstevel@tonic-gate * page tables. htable_lookup() may return NULL if no such entry exists. 1757c478bd9Sstevel@tonic-gate * 1767c478bd9Sstevel@tonic-gate * On return the given htable is marked busy (a shared lock) - this prevents 1777c478bd9Sstevel@tonic-gate * the htable from being stolen or freed) until htable_release() is called. 1787c478bd9Sstevel@tonic-gate * 1797c478bd9Sstevel@tonic-gate * If kalloc_flag is set on an htable_create() we can't call kmem allocation 1807c478bd9Sstevel@tonic-gate * routines for this htable, since it's for the kernel hat itself. 1817c478bd9Sstevel@tonic-gate * 1827c478bd9Sstevel@tonic-gate * htable_acquire() is used when an htable pointer has been extracted from 1837c478bd9Sstevel@tonic-gate * an hment and we need to get a reference to the htable. 1847c478bd9Sstevel@tonic-gate */ 1857c478bd9Sstevel@tonic-gate extern htable_t *htable_lookup(struct hat *hat, uintptr_t vaddr, level_t level); 1867c478bd9Sstevel@tonic-gate extern htable_t *htable_create(struct hat *hat, uintptr_t vaddr, level_t level, 1877c478bd9Sstevel@tonic-gate htable_t *shared); 1887c478bd9Sstevel@tonic-gate extern void htable_acquire(htable_t *); 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate extern void htable_release(htable_t *ht); 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* 1937c478bd9Sstevel@tonic-gate * Code to free all remaining htables for a hat. Called after the hat is no 1947c478bd9Sstevel@tonic-gate * longer in use by any thread. 1957c478bd9Sstevel@tonic-gate */ 1967c478bd9Sstevel@tonic-gate extern void htable_purge_hat(struct hat *hat); 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate /* 1997c478bd9Sstevel@tonic-gate * Find the htable, page table entry index, and PTE of the given virtual 2007c478bd9Sstevel@tonic-gate * address. If not found returns NULL. When found, returns the htable_t *, 2017c478bd9Sstevel@tonic-gate * sets entry, and has a hold on the htable. 2027c478bd9Sstevel@tonic-gate */ 2037c478bd9Sstevel@tonic-gate extern htable_t *htable_getpte(struct hat *, uintptr_t, uint_t *, x86pte_t *, 2047c478bd9Sstevel@tonic-gate level_t); 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* 2077c478bd9Sstevel@tonic-gate * Similar to hat_getpte(), except that this only succeeds if a valid 2087c478bd9Sstevel@tonic-gate * page mapping is present. 2097c478bd9Sstevel@tonic-gate */ 2107c478bd9Sstevel@tonic-gate extern htable_t *htable_getpage(struct hat *hat, uintptr_t va, uint_t *entry); 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate /* 2137c478bd9Sstevel@tonic-gate * Called to allocate initial/additional htables for reserve. 2147c478bd9Sstevel@tonic-gate */ 2157c478bd9Sstevel@tonic-gate extern void htable_initial_reserve(uint_t); 2167c478bd9Sstevel@tonic-gate extern void htable_reserve(uint_t); 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate /* 2197c478bd9Sstevel@tonic-gate * Used to readjust the htable reserve after the reserve list has been used. 2207c478bd9Sstevel@tonic-gate * Also called after boot to release left over boot reserves. 2217c478bd9Sstevel@tonic-gate */ 2227c478bd9Sstevel@tonic-gate extern void htable_adjust_reserve(void); 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate /* 2257c478bd9Sstevel@tonic-gate * Routine to find the next populated htable at or above a given virtual 2267c478bd9Sstevel@tonic-gate * address. Can specify an upper limit, or HTABLE_WALK_TO_END to indicate 2277c478bd9Sstevel@tonic-gate * that it should search the entire address space. Similar to 2287c478bd9Sstevel@tonic-gate * hat_getpte(), but used for walking through address ranges. It can be 2297c478bd9Sstevel@tonic-gate * used like this: 2307c478bd9Sstevel@tonic-gate * 2317c478bd9Sstevel@tonic-gate * va = ... 2327c478bd9Sstevel@tonic-gate * ht = NULL; 2337c478bd9Sstevel@tonic-gate * while (va < end_va) { 2347c478bd9Sstevel@tonic-gate * pte = htable_walk(hat, &ht, &va, end_va); 2357c478bd9Sstevel@tonic-gate * if (!pte) 2367c478bd9Sstevel@tonic-gate * break; 2377c478bd9Sstevel@tonic-gate * 2387c478bd9Sstevel@tonic-gate * ... code to operate on page at va ... 2397c478bd9Sstevel@tonic-gate * 2407c478bd9Sstevel@tonic-gate * va += LEVEL_SIZE(ht->ht_level); 2417c478bd9Sstevel@tonic-gate * } 2427c478bd9Sstevel@tonic-gate * if (ht) 2437c478bd9Sstevel@tonic-gate * htable_release(ht); 2447c478bd9Sstevel@tonic-gate * 2457c478bd9Sstevel@tonic-gate */ 2467c478bd9Sstevel@tonic-gate extern x86pte_t htable_walk(struct hat *hat, htable_t **ht, uintptr_t *va, 2477c478bd9Sstevel@tonic-gate uintptr_t eaddr); 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate #define HTABLE_WALK_TO_END ((uintptr_t)-1) 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* 2527c478bd9Sstevel@tonic-gate * Utilities convert between virtual addresses and page table entry indeces. 2537c478bd9Sstevel@tonic-gate */ 2547c478bd9Sstevel@tonic-gate extern uint_t htable_va2entry(uintptr_t va, htable_t *ht); 2557c478bd9Sstevel@tonic-gate extern uintptr_t htable_e2va(htable_t *ht, uint_t entry); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate /* 2587c478bd9Sstevel@tonic-gate * Interfaces that provide access to page table entries via the htable. 2597c478bd9Sstevel@tonic-gate * 2607c478bd9Sstevel@tonic-gate * Note that all accesses except x86pte_copy() and x86pte_zero() are atomic. 2617c478bd9Sstevel@tonic-gate */ 2627c478bd9Sstevel@tonic-gate extern void x86pte_cpu_init(cpu_t *, void *); 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate extern x86pte_t x86pte_get(htable_t *, uint_t entry); 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate extern x86pte_t x86pte_set(htable_t *, uint_t entry, x86pte_t new, void *); 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate extern x86pte_t x86pte_invalidate_pfn(htable_t *ht, uint_t entry, pfn_t pfn, 2697c478bd9Sstevel@tonic-gate void *pte_ptr); 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate extern x86pte_t x86pte_update(htable_t *ht, uint_t entry, 2727c478bd9Sstevel@tonic-gate x86pte_t old, x86pte_t new); 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate extern void x86pte_copy(htable_t *src, htable_t *dest, uint_t entry, 2757c478bd9Sstevel@tonic-gate uint_t cnt); 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate extern void x86pte_zero(htable_t *ht, uint_t entry, uint_t cnt); 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate /* 2817c478bd9Sstevel@tonic-gate * these are actually inlines for "lock; incw", "lock; decw", etc. instructions. 2827c478bd9Sstevel@tonic-gate */ 2837c478bd9Sstevel@tonic-gate #define HTABLE_INC(x) atomic_inc16((uint16_t *)&x) 2847c478bd9Sstevel@tonic-gate #define HTABLE_DEC(x) atomic_dec16((uint16_t *)&x) 2857c478bd9Sstevel@tonic-gate #define HTABLE_LOCK_INC(ht) atomic_add_32(&(ht)->ht_lock_cnt, 1) 2867c478bd9Sstevel@tonic-gate #define HTABLE_LOCK_DEC(ht) atomic_add_32(&(ht)->ht_lock_cnt, -1) 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate #ifdef __cplusplus 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate #endif 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate #endif /* _VM_HTABLE_H */ 296