pmap.h revision 1.11
11.11Sad/* $NetBSD: pmap.h,v 1.11 2007/08/29 23:38:03 ad Exp $ */ 21.1Sfvdl 31.1Sfvdl/* 41.1Sfvdl * 51.1Sfvdl * Copyright (c) 1997 Charles D. Cranor and Washington University. 61.1Sfvdl * All rights reserved. 71.1Sfvdl * 81.1Sfvdl * Redistribution and use in source and binary forms, with or without 91.1Sfvdl * modification, are permitted provided that the following conditions 101.1Sfvdl * are met: 111.1Sfvdl * 1. Redistributions of source code must retain the above copyright 121.1Sfvdl * notice, this list of conditions and the following disclaimer. 131.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright 141.1Sfvdl * notice, this list of conditions and the following disclaimer in the 151.1Sfvdl * documentation and/or other materials provided with the distribution. 161.1Sfvdl * 3. All advertising materials mentioning features or use of this software 171.1Sfvdl * must display the following acknowledgment: 181.1Sfvdl * This product includes software developed by Charles D. Cranor and 191.1Sfvdl * Washington University. 201.1Sfvdl * 4. The name of the author may not be used to endorse or promote products 211.1Sfvdl * derived from this software without specific prior written permission. 221.1Sfvdl * 231.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 241.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 251.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 261.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 271.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 281.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 291.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 301.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 311.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 321.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 331.1Sfvdl */ 341.1Sfvdl 351.1Sfvdl/* 361.1Sfvdl * Copyright (c) 2001 Wasabi Systems, Inc. 371.1Sfvdl * All rights reserved. 381.1Sfvdl * 391.1Sfvdl * Written by Frank van der Linden for Wasabi Systems, Inc. 401.1Sfvdl * 411.1Sfvdl * Redistribution and use in source and binary forms, with or without 421.1Sfvdl * modification, are permitted provided that the following conditions 431.1Sfvdl * are met: 441.1Sfvdl * 1. Redistributions of source code must retain the above copyright 451.1Sfvdl * notice, this list of conditions and the following disclaimer. 461.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright 471.1Sfvdl * notice, this list of conditions and the following disclaimer in the 481.1Sfvdl * documentation and/or other materials provided with the distribution. 491.1Sfvdl * 3. All advertising materials mentioning features or use of this software 501.1Sfvdl * must display the following acknowledgement: 511.1Sfvdl * This product includes software developed for the NetBSD Project by 521.1Sfvdl * Wasabi Systems, Inc. 531.1Sfvdl * 4. The name of Wasabi Systems, Inc. may not be used to endorse 541.1Sfvdl * or promote products derived from this software without specific prior 551.1Sfvdl * written permission. 561.1Sfvdl * 571.1Sfvdl * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 581.1Sfvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 591.1Sfvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 601.1Sfvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 611.1Sfvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 621.1Sfvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 631.1Sfvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 641.1Sfvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 651.1Sfvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 661.1Sfvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 671.1Sfvdl * POSSIBILITY OF SUCH DAMAGE. 681.1Sfvdl */ 691.1Sfvdl 701.1Sfvdl/* 711.1Sfvdl * pmap.h: see pmap.c for the history of this pmap module. 721.1Sfvdl */ 731.1Sfvdl 741.1Sfvdl#ifndef _AMD64_PMAP_H_ 751.1Sfvdl#define _AMD64_PMAP_H_ 761.1Sfvdl 771.1Sfvdl#ifndef _LOCORE 781.1Sfvdl#if defined(_KERNEL_OPT) 791.1Sfvdl#include "opt_largepages.h" 801.1Sfvdl#endif 811.1Sfvdl 821.1Sfvdl#include <machine/cpufunc.h> 831.1Sfvdl#include <machine/pte.h> 841.1Sfvdl#include <machine/segments.h> 851.11Sad#include <machine/atomic.h> 861.11Sad 871.1Sfvdl#include <uvm/uvm_object.h> 881.1Sfvdl#endif 891.1Sfvdl 901.1Sfvdl/* 911.1Sfvdl * The x86_64 pmap module closely resembles the i386 one. It uses 921.1Sfvdl * the same recursive entry scheme, and the same alternate area 931.1Sfvdl * trick for accessing non-current pmaps. See the i386 pmap.h 941.1Sfvdl * for a description. The obvious difference is that 3 extra 951.1Sfvdl * levels of page table need to be dealt with. The level 1 page 961.1Sfvdl * table pages are at: 971.1Sfvdl * 981.1Sfvdl * l1: 0x00007f8000000000 - 0x00007fffffffffff (39 bits, needs PML4 entry) 991.1Sfvdl * 1001.1Sfvdl * The alternate space is at: 1011.1Sfvdl * 1021.1Sfvdl * l1: 0xffffff8000000000 - 0xffffffffffffffff (39 bits, needs PML4 entry) 1031.1Sfvdl * 1041.1Sfvdl * The rest is kept as physical pages in 3 UVM objects, and is 1051.1Sfvdl * temporarily mapped for virtual access when needed. 1061.1Sfvdl * 1071.1Sfvdl * Note that address space is signed, so the layout for 48 bits is: 1081.1Sfvdl * 1091.1Sfvdl * +---------------------------------+ 0xffffffffffffffff 1101.1Sfvdl * | | 1111.1Sfvdl * | alt.L1 table (PTE pages) | 1121.1Sfvdl * | | 1131.1Sfvdl * +---------------------------------+ 0xffffff8000000000 1141.1Sfvdl * ~ ~ 1151.1Sfvdl * | | 1161.1Sfvdl * | Kernel Space | 1171.1Sfvdl * | | 1181.1Sfvdl * | | 1191.4Syamt * +---------------------------------+ 0xffff800000000000 = 0x0000800000000000 1201.1Sfvdl * | | 1211.1Sfvdl * | alt.L1 table (PTE pages) | 1221.1Sfvdl * | | 1231.1Sfvdl * +---------------------------------+ 0x00007f8000000000 1241.1Sfvdl * ~ ~ 1251.1Sfvdl * | | 1261.1Sfvdl * | User Space | 1271.1Sfvdl * | | 1281.1Sfvdl * | | 1291.1Sfvdl * +---------------------------------+ 0x0000000000000000 1301.1Sfvdl * 1311.4Syamt * In other words, there is a 'VA hole' at 0x0000800000000000 - 1321.1Sfvdl * 0xffff800000000000 which will trap, just as on, for example, 1331.1Sfvdl * sparcv9. 1341.1Sfvdl * 1351.1Sfvdl * The unused space can be used if needed, but it adds a little more 1361.1Sfvdl * complexity to the calculations. 1371.1Sfvdl */ 1381.1Sfvdl 1391.1Sfvdl/* 1401.1Sfvdl * The first generation of Hammer processors can use 48 bits of 1411.1Sfvdl * virtual memory, and 40 bits of physical memory. This will be 1421.1Sfvdl * more for later generations. These defines can be changed to 1431.1Sfvdl * variable names containing the # of bits, extracted from an 1441.1Sfvdl * extended cpuid instruction (variables are harder to use during 1451.1Sfvdl * bootstrap, though) 1461.1Sfvdl */ 1471.1Sfvdl#define VIRT_BITS 48 1481.1Sfvdl#define PHYS_BITS 40 1491.1Sfvdl 1501.1Sfvdl/* 1511.1Sfvdl * Mask to get rid of the sign-extended part of addresses. 1521.1Sfvdl */ 1531.1Sfvdl#define VA_SIGN_MASK 0xffff000000000000 1541.1Sfvdl#define VA_SIGN_NEG(va) ((va) | VA_SIGN_MASK) 1551.1Sfvdl/* 1561.1Sfvdl * XXXfvdl this one's not right. 1571.1Sfvdl */ 1581.1Sfvdl#define VA_SIGN_POS(va) ((va) & ~VA_SIGN_MASK) 1591.1Sfvdl 1601.1Sfvdl#define L4_SLOT_PTE 255 1611.1Sfvdl#define L4_SLOT_KERN 256 1621.1Sfvdl#define L4_SLOT_KERNBASE 511 1631.1Sfvdl#define L4_SLOT_APTE 510 1641.1Sfvdl 1651.1Sfvdl#define PDIR_SLOT_KERN L4_SLOT_KERN 1661.1Sfvdl#define PDIR_SLOT_PTE L4_SLOT_PTE 1671.1Sfvdl#define PDIR_SLOT_APTE L4_SLOT_APTE 1681.1Sfvdl 1691.1Sfvdl/* 1701.1Sfvdl * the following defines give the virtual addresses of various MMU 1711.1Sfvdl * data structures: 1721.1Sfvdl * PTE_BASE and APTE_BASE: the base VA of the linear PTE mappings 1731.1Sfvdl * PTD_BASE and APTD_BASE: the base VA of the recursive mapping of the PTD 1741.1Sfvdl * PDP_PDE and APDP_PDE: the VA of the PDE that points back to the PDP/APDP 1751.1Sfvdl * 1761.1Sfvdl */ 1771.1Sfvdl 1781.1Sfvdl#define PTE_BASE ((pt_entry_t *) (L4_SLOT_PTE * NBPD_L4)) 1791.1Sfvdl#define APTE_BASE ((pt_entry_t *) (VA_SIGN_NEG((L4_SLOT_APTE * NBPD_L4)))) 1801.1Sfvdl 1811.1Sfvdl#define L1_BASE PTE_BASE 1821.1Sfvdl#define AL1_BASE APTE_BASE 1831.1Sfvdl 1841.1Sfvdl#define L2_BASE ((pd_entry_t *)((char *)L1_BASE + L4_SLOT_PTE * NBPD_L3)) 1851.1Sfvdl#define L3_BASE ((pd_entry_t *)((char *)L2_BASE + L4_SLOT_PTE * NBPD_L2)) 1861.1Sfvdl#define L4_BASE ((pd_entry_t *)((char *)L3_BASE + L4_SLOT_PTE * NBPD_L1)) 1871.1Sfvdl 1881.1Sfvdl#define AL2_BASE ((pd_entry_t *)((char *)AL1_BASE + L4_SLOT_PTE * NBPD_L3)) 1891.1Sfvdl#define AL3_BASE ((pd_entry_t *)((char *)AL2_BASE + L4_SLOT_PTE * NBPD_L2)) 1901.1Sfvdl#define AL4_BASE ((pd_entry_t *)((char *)AL3_BASE + L4_SLOT_PTE * NBPD_L1)) 1911.1Sfvdl 1921.1Sfvdl#define PDP_PDE (L4_BASE + PDIR_SLOT_PTE) 1931.1Sfvdl#define APDP_PDE (L4_BASE + PDIR_SLOT_APTE) 1941.1Sfvdl 1951.1Sfvdl#define PDP_BASE L4_BASE 1961.1Sfvdl#define APDP_BASE AL4_BASE 1971.1Sfvdl 1981.1Sfvdl#define NKL4_MAX_ENTRIES (unsigned long)1 1991.1Sfvdl#define NKL3_MAX_ENTRIES (unsigned long)(NKL4_MAX_ENTRIES * 512) 2001.1Sfvdl#define NKL2_MAX_ENTRIES (unsigned long)(NKL3_MAX_ENTRIES * 512) 2011.1Sfvdl#define NKL1_MAX_ENTRIES (unsigned long)(NKL2_MAX_ENTRIES * 512) 2021.1Sfvdl 2031.1Sfvdl#define NKL4_KIMG_ENTRIES 1 2041.1Sfvdl#define NKL3_KIMG_ENTRIES 1 2051.1Sfvdl#define NKL2_KIMG_ENTRIES 8 2061.1Sfvdl 2071.1Sfvdl/* 2081.1Sfvdl * Since kva space is below the kernel in its entirety, we start off 2091.1Sfvdl * with zero entries on each level. 2101.1Sfvdl */ 2111.1Sfvdl#define NKL4_START_ENTRIES 0 2121.1Sfvdl#define NKL3_START_ENTRIES 0 2131.1Sfvdl#define NKL2_START_ENTRIES 0 2141.1Sfvdl#define NKL1_START_ENTRIES 0 /* XXX */ 2151.1Sfvdl 2161.1Sfvdl#define NTOPLEVEL_PDES (PAGE_SIZE / (sizeof (pd_entry_t))) 2171.1Sfvdl 2181.1Sfvdl#define KERNSPACE (NKL4_ENTRIES * NBPD_L4) 2191.1Sfvdl 2201.1Sfvdl#define NPDPG (PAGE_SIZE / sizeof (pd_entry_t)) 2211.1Sfvdl 2221.1Sfvdl#define ptei(VA) (((VA_SIGN_POS(VA)) & L1_MASK) >> L1_SHIFT) 2231.1Sfvdl 2241.1Sfvdl/* 2251.1Sfvdl * pl*_pi: index in the ptp page for a pde mapping a VA. 2261.1Sfvdl * (pl*_i below is the index in the virtual array of all pdes per level) 2271.1Sfvdl */ 2281.1Sfvdl#define pl1_pi(VA) (((VA_SIGN_POS(VA)) & L1_MASK) >> L1_SHIFT) 2291.1Sfvdl#define pl2_pi(VA) (((VA_SIGN_POS(VA)) & L2_MASK) >> L2_SHIFT) 2301.1Sfvdl#define pl3_pi(VA) (((VA_SIGN_POS(VA)) & L3_MASK) >> L3_SHIFT) 2311.1Sfvdl#define pl4_pi(VA) (((VA_SIGN_POS(VA)) & L4_MASK) >> L4_SHIFT) 2321.1Sfvdl 2331.1Sfvdl/* 2341.1Sfvdl * pl*_i: generate index into pde/pte arrays in virtual space 2351.1Sfvdl */ 2361.1Sfvdl#define pl1_i(VA) (((VA_SIGN_POS(VA)) & L1_FRAME) >> L1_SHIFT) 2371.1Sfvdl#define pl2_i(VA) (((VA_SIGN_POS(VA)) & L2_FRAME) >> L2_SHIFT) 2381.1Sfvdl#define pl3_i(VA) (((VA_SIGN_POS(VA)) & L3_FRAME) >> L3_SHIFT) 2391.1Sfvdl#define pl4_i(VA) (((VA_SIGN_POS(VA)) & L4_FRAME) >> L4_SHIFT) 2401.1Sfvdl#define pl_i(va, lvl) \ 2411.1Sfvdl (((VA_SIGN_POS(va)) & ptp_masks[(lvl)-1]) >> ptp_shifts[(lvl)-1]) 2421.1Sfvdl 2431.1Sfvdl#define PTP_MASK_INITIALIZER { L1_FRAME, L2_FRAME, L3_FRAME, L4_FRAME } 2441.1Sfvdl#define PTP_SHIFT_INITIALIZER { L1_SHIFT, L2_SHIFT, L3_SHIFT, L4_SHIFT } 2451.1Sfvdl#define NKPTP_INITIALIZER { NKL1_START_ENTRIES, NKL2_START_ENTRIES, \ 2461.1Sfvdl NKL3_START_ENTRIES, NKL4_START_ENTRIES } 2471.1Sfvdl#define NKPTPMAX_INITIALIZER { NKL1_MAX_ENTRIES, NKL2_MAX_ENTRIES, \ 2481.1Sfvdl NKL3_MAX_ENTRIES, NKL4_MAX_ENTRIES } 2491.1Sfvdl#define NBPD_INITIALIZER { NBPD_L1, NBPD_L2, NBPD_L3, NBPD_L4 } 2501.1Sfvdl#define PDES_INITIALIZER { L2_BASE, L3_BASE, L4_BASE } 2511.1Sfvdl#define APDES_INITIALIZER { AL2_BASE, AL3_BASE, AL4_BASE } 2521.1Sfvdl 2531.1Sfvdl/* 2541.1Sfvdl * PTP macros: 2551.1Sfvdl * a PTP's index is the PD index of the PDE that points to it 2561.1Sfvdl * a PTP's offset is the byte-offset in the PTE space that this PTP is at 2571.1Sfvdl * a PTP's VA is the first VA mapped by that PTP 2581.1Sfvdl * 2591.1Sfvdl * note that PAGE_SIZE == number of bytes in a PTP (4096 bytes == 1024 entries) 2601.1Sfvdl * NBPD == number of bytes a PTP can map (4MB) 2611.1Sfvdl */ 2621.1Sfvdl 2631.1Sfvdl#define ptp_va2o(va, lvl) (pl_i(va, (lvl)+1) * PAGE_SIZE) 2641.1Sfvdl 2651.1Sfvdl#define PTP_LEVELS 4 2661.1Sfvdl 2671.1Sfvdl/* 2681.1Sfvdl * PG_AVAIL usage: we make use of the ignored bits of the PTE 2691.1Sfvdl */ 2701.1Sfvdl 2711.1Sfvdl#define PG_W PG_AVAIL1 /* "wired" mapping */ 2721.1Sfvdl#define PG_PVLIST PG_AVAIL2 /* mapping has entry on pvlist */ 2731.1Sfvdl/* PG_AVAIL3 not used */ 2741.1Sfvdl 2751.1Sfvdl/* 2761.1Sfvdl * Number of PTE's per cache line. 8 byte pte, 64-byte cache line 2771.1Sfvdl * Used to avoid false sharing of cache lines. 2781.1Sfvdl */ 2791.1Sfvdl#define NPTECL 8 2801.1Sfvdl 2811.1Sfvdl 2821.1Sfvdl#if defined(_KERNEL) && !defined(_LOCORE) 2831.1Sfvdl/* 2841.1Sfvdl * pmap data structures: see pmap.c for details of locking. 2851.1Sfvdl */ 2861.1Sfvdl 2871.1Sfvdlstruct pmap; 2881.1Sfvdltypedef struct pmap *pmap_t; 2891.1Sfvdl 2901.1Sfvdl/* 2911.1Sfvdl * we maintain a list of all non-kernel pmaps 2921.1Sfvdl */ 2931.1Sfvdl 2941.1SfvdlLIST_HEAD(pmap_head, pmap); /* struct pmap_head: head of a pmap list */ 2951.1Sfvdl 2961.1Sfvdl/* 2971.1Sfvdl * the pmap structure 2981.1Sfvdl * 2991.1Sfvdl * note that the pm_obj contains the simple_lock, the reference count, 3001.1Sfvdl * page list, and number of PTPs within the pmap. 3011.1Sfvdl * 3021.1Sfvdl * pm_lock is the same as the spinlock for vm object 0. Changes to 3031.1Sfvdl * the other objects may only be made if that lock has been taken 3041.1Sfvdl * (the other object locks are only used when uvm_pagealloc is called) 3051.1Sfvdl */ 3061.1Sfvdl 3071.1Sfvdlstruct pmap { 3081.1Sfvdl struct uvm_object pm_obj[PTP_LEVELS-1]; /* objects for lvl >= 1) */ 3091.1Sfvdl#define pm_lock pm_obj[0].vmobjlock 3101.1Sfvdl#define pm_obj_l1 pm_obj[0] 3111.1Sfvdl#define pm_obj_l2 pm_obj[1] 3121.1Sfvdl#define pm_obj_l3 pm_obj[2] 3131.1Sfvdl LIST_ENTRY(pmap) pm_list; /* list (lck by pm_list lock) */ 3141.1Sfvdl pd_entry_t *pm_pdir; /* VA of PD (lck by object lock) */ 3151.1Sfvdl paddr_t pm_pdirpa; /* PA of PD (read-only after create) */ 3161.1Sfvdl struct vm_page *pm_ptphint[PTP_LEVELS-1]; 3171.1Sfvdl /* pointer to a PTP in our pmap */ 3181.1Sfvdl struct pmap_statistics pm_stats; /* pmap stats (lck by object lock) */ 3191.1Sfvdl 3201.1Sfvdl int pm_flags; /* see below */ 3211.1Sfvdl 3221.1Sfvdl union descriptor *pm_ldt; /* user-set LDT */ 3231.1Sfvdl int pm_ldt_len; /* number of LDT entries */ 3241.1Sfvdl int pm_ldt_sel; /* LDT selector */ 3251.1Sfvdl u_int32_t pm_cpus; /* mask of CPUs using pmap */ 3261.1Sfvdl}; 3271.1Sfvdl 3281.1Sfvdl/* pm_flags */ 3291.1Sfvdl#define PMF_USER_LDT 0x01 /* pmap has user-set LDT */ 3301.1Sfvdl 3311.1Sfvdl/* 3321.1Sfvdl * for each managed physical page we maintain a list of <PMAP,VA>'s 3331.1Sfvdl * which it is mapped at. the list is headed by a pv_head structure. 3341.1Sfvdl * there is one pv_head per managed phys page (allocated at boot time). 3351.1Sfvdl * the pv_head structure points to a list of pv_entry structures (each 3361.1Sfvdl * describes one mapping). 3371.1Sfvdl */ 3381.1Sfvdl 3391.2Ssekiyastruct pv_entry { /* locked by its list's pvh_lock */ 3401.2Ssekiya SPLAY_ENTRY(pv_entry) pv_node; /* splay-tree node */ 3411.2Ssekiya struct pmap *pv_pmap; /* the pmap */ 3421.2Ssekiya vaddr_t pv_va; /* the virtual address */ 3431.2Ssekiya struct vm_page *pv_ptp; /* the vm_page of the PTP */ 3441.11Sad struct pmap_cpu *pv_alloc_cpu; /* CPU allocated from */ 3451.2Ssekiya}; 3461.1Sfvdl 3471.1Sfvdl/* 3481.1Sfvdl * pv_entrys are dynamically allocated in chunks from a single page. 3491.1Sfvdl * we keep track of how many pv_entrys are in use for each page and 3501.1Sfvdl * we can free pv_entry pages if needed. there is one lock for the 3511.1Sfvdl * entire allocation system. 3521.1Sfvdl */ 3531.1Sfvdl 3541.1Sfvdlstruct pv_page_info { 3551.1Sfvdl TAILQ_ENTRY(pv_page) pvpi_list; 3561.1Sfvdl struct pv_entry *pvpi_pvfree; 3571.1Sfvdl int pvpi_nfree; 3581.1Sfvdl}; 3591.1Sfvdl 3601.1Sfvdl/* 3611.1Sfvdl * number of pv_entry's in a pv_page 3621.1Sfvdl * (note: won't work on systems where NPBG isn't a constant) 3631.1Sfvdl */ 3641.1Sfvdl 3651.1Sfvdl#define PVE_PER_PVPAGE ((PAGE_SIZE - sizeof(struct pv_page_info)) / \ 3661.1Sfvdl sizeof(struct pv_entry)) 3671.1Sfvdl 3681.1Sfvdl/* 3691.1Sfvdl * a pv_page: where pv_entrys are allocated from 3701.1Sfvdl */ 3711.1Sfvdl 3721.1Sfvdlstruct pv_page { 3731.1Sfvdl struct pv_page_info pvinfo; 3741.1Sfvdl struct pv_entry pvents[PVE_PER_PVPAGE]; 3751.1Sfvdl}; 3761.1Sfvdl 3771.1Sfvdl/* 3781.1Sfvdl * pmap_remove_record: a record of VAs that have been unmapped, used to 3791.1Sfvdl * flush TLB. if we have more than PMAP_RR_MAX then we stop recording. 3801.1Sfvdl */ 3811.1Sfvdl 3821.1Sfvdl#define PMAP_RR_MAX 16 /* max of 16 pages (64K) */ 3831.1Sfvdl 3841.1Sfvdlstruct pmap_remove_record { 3851.1Sfvdl int prr_npages; 3861.1Sfvdl vaddr_t prr_vas[PMAP_RR_MAX]; 3871.1Sfvdl}; 3881.1Sfvdl 3891.1Sfvdl/* 3901.1Sfvdl * global kernel variables 3911.1Sfvdl */ 3921.1Sfvdl 3931.1Sfvdl/* PTDpaddr: is the physical address of the kernel's PDP */ 3941.1Sfvdlextern u_long PTDpaddr; 3951.1Sfvdl 3961.1Sfvdlextern struct pmap kernel_pmap_store; /* kernel pmap */ 3971.1Sfvdlextern int pmap_pg_g; /* do we support PG_G? */ 3981.1Sfvdl 3991.1Sfvdlextern paddr_t ptp_masks[]; 4001.1Sfvdlextern int ptp_shifts[]; 4011.1Sfvdlextern long nkptp[], nbpd[], nkptpmax[]; 4021.1Sfvdl 4031.1Sfvdl/* 4041.1Sfvdl * macros 4051.1Sfvdl */ 4061.1Sfvdl 4071.1Sfvdl#define pmap_kernel() (&kernel_pmap_store) 4081.1Sfvdl#define pmap_resident_count(pmap) ((pmap)->pm_stats.resident_count) 4091.1Sfvdl#define pmap_wired_count(pmap) ((pmap)->pm_stats.wired_count) 4101.1Sfvdl 4111.1Sfvdl#define pmap_clear_modify(pg) pmap_clear_attrs(pg, PG_M) 4121.1Sfvdl#define pmap_clear_reference(pg) pmap_clear_attrs(pg, PG_U) 4131.1Sfvdl#define pmap_copy(DP,SP,D,L,S) 4141.1Sfvdl#define pmap_is_modified(pg) pmap_test_attrs(pg, PG_M) 4151.1Sfvdl#define pmap_is_referenced(pg) pmap_test_attrs(pg, PG_U) 4161.1Sfvdl#define pmap_move(DP,SP,D,L,S) 4171.1Sfvdl#define pmap_phys_address(ppn) ptob(ppn) 4181.1Sfvdl#define pmap_valid_entry(E) ((E) & PG_V) /* is PDE or PTE valid? */ 4191.1Sfvdl 4201.1Sfvdl 4211.1Sfvdl/* 4221.1Sfvdl * prototypes 4231.1Sfvdl */ 4241.1Sfvdl 4251.1Sfvdlvoid pmap_activate __P((struct lwp *)); 4261.1Sfvdlvoid pmap_bootstrap __P((vaddr_t)); 4271.10Sthorpejbool pmap_clear_attrs __P((struct vm_page *, unsigned)); 4281.1Sfvdlvoid pmap_deactivate __P((struct lwp *)); 4291.1Sfvdlstatic void pmap_page_protect __P((struct vm_page *, vm_prot_t)); 4301.1Sfvdlvoid pmap_page_remove __P((struct vm_page *)); 4311.1Sfvdlstatic void pmap_protect __P((struct pmap *, vaddr_t, 4321.1Sfvdl vaddr_t, vm_prot_t)); 4331.1Sfvdlvoid pmap_remove __P((struct pmap *, vaddr_t, vaddr_t)); 4341.10Sthorpejbool pmap_test_attrs __P((struct vm_page *, unsigned)); 4351.1Sfvdlstatic void pmap_update_pg __P((vaddr_t)); 4361.1Sfvdlstatic void pmap_update_2pg __P((vaddr_t,vaddr_t)); 4371.1Sfvdlvoid pmap_write_protect __P((struct pmap *, vaddr_t, 4381.1Sfvdl vaddr_t, vm_prot_t)); 4391.3Sfvdlvoid pmap_changeprot_local(vaddr_t, vm_prot_t); 4401.1Sfvdl 4411.1Sfvdlvaddr_t reserve_dumppages __P((vaddr_t)); /* XXX: not a pmap fn */ 4421.1Sfvdl 4431.11Sadvoid pmap_tlb_shootdown __P((pmap_t, vaddr_t, vaddr_t, pt_entry_t)); 4441.11Sadvoid pmap_tlb_shootwait __P((void)); 4451.1Sfvdlvoid pmap_prealloc_lowmem_ptps __P((void)); 4461.1Sfvdl 4471.1Sfvdl#define PMAP_GROWKERNEL /* turn on pmap_growkernel interface */ 4481.1Sfvdl 4491.1Sfvdl/* 4501.1Sfvdl * Do idle page zero'ing uncached to avoid polluting the cache. 4511.1Sfvdl */ 4521.10Sthorpejbool pmap_pageidlezero __P((paddr_t)); 4531.1Sfvdl#define PMAP_PAGEIDLEZERO(pa) pmap_pageidlezero((pa)) 4541.1Sfvdl 4551.1Sfvdl/* 4561.1Sfvdl * inline functions 4571.1Sfvdl */ 4581.1Sfvdl 4591.9Sperrystatic __inline void 4601.1Sfvdlpmap_remove_all(struct pmap *pmap) 4611.1Sfvdl{ 4621.1Sfvdl /* Nothing. */ 4631.1Sfvdl} 4641.1Sfvdl 4651.1Sfvdl/* 4661.1Sfvdl * pmap_update_pg: flush one page from the TLB (or flush the whole thing 4671.1Sfvdl * if hardware doesn't support one-page flushing) 4681.1Sfvdl */ 4691.1Sfvdl 4701.9Sperry__inline static void 4711.1Sfvdlpmap_update_pg(va) 4721.1Sfvdl vaddr_t va; 4731.1Sfvdl{ 4741.1Sfvdl invlpg(va); 4751.1Sfvdl} 4761.1Sfvdl 4771.1Sfvdl/* 4781.1Sfvdl * pmap_update_2pg: flush two pages from the TLB 4791.1Sfvdl */ 4801.1Sfvdl 4811.9Sperry__inline static void 4821.1Sfvdlpmap_update_2pg(va, vb) 4831.1Sfvdl vaddr_t va, vb; 4841.1Sfvdl{ 4851.1Sfvdl invlpg(va); 4861.1Sfvdl invlpg(vb); 4871.1Sfvdl} 4881.1Sfvdl 4891.1Sfvdl/* 4901.1Sfvdl * pmap_page_protect: change the protection of all recorded mappings 4911.1Sfvdl * of a managed page 4921.1Sfvdl * 4931.1Sfvdl * => this function is a frontend for pmap_page_remove/pmap_clear_attrs 4941.1Sfvdl * => we only have to worry about making the page more protected. 4951.1Sfvdl * unprotecting a page is done on-demand at fault time. 4961.1Sfvdl */ 4971.1Sfvdl 4981.9Sperry__inline static void 4991.1Sfvdlpmap_page_protect(struct vm_page *pg, vm_prot_t prot) 5001.1Sfvdl{ 5011.1Sfvdl if ((prot & VM_PROT_WRITE) == 0) { 5021.1Sfvdl if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) { 5031.1Sfvdl (void) pmap_clear_attrs(pg, PG_RW); 5041.1Sfvdl } else { 5051.1Sfvdl pmap_page_remove(pg); 5061.1Sfvdl } 5071.1Sfvdl } 5081.1Sfvdl} 5091.1Sfvdl 5101.1Sfvdl/* 5111.1Sfvdl * pmap_protect: change the protection of pages in a pmap 5121.1Sfvdl * 5131.1Sfvdl * => this function is a frontend for pmap_remove/pmap_write_protect 5141.1Sfvdl * => we only have to worry about making the page more protected. 5151.1Sfvdl * unprotecting a page is done on-demand at fault time. 5161.1Sfvdl */ 5171.1Sfvdl 5181.9Sperry__inline static void 5191.1Sfvdlpmap_protect(pmap, sva, eva, prot) 5201.1Sfvdl struct pmap *pmap; 5211.1Sfvdl vaddr_t sva, eva; 5221.1Sfvdl vm_prot_t prot; 5231.1Sfvdl{ 5241.1Sfvdl if ((prot & VM_PROT_WRITE) == 0) { 5251.1Sfvdl if (prot & (VM_PROT_READ|VM_PROT_EXECUTE)) { 5261.1Sfvdl pmap_write_protect(pmap, sva, eva, prot); 5271.1Sfvdl } else { 5281.1Sfvdl pmap_remove(pmap, sva, eva); 5291.1Sfvdl } 5301.1Sfvdl } 5311.1Sfvdl} 5321.1Sfvdl 5331.1Sfvdl/* 5341.1Sfvdl * various address inlines 5351.1Sfvdl * 5361.1Sfvdl * vtopte: return a pointer to the PTE mapping a VA, works only for 5371.1Sfvdl * user and PT addresses 5381.1Sfvdl * 5391.1Sfvdl * kvtopte: return a pointer to the PTE mapping a kernel VA 5401.1Sfvdl */ 5411.1Sfvdl 5421.1Sfvdl#include <lib/libkern/libkern.h> 5431.1Sfvdl 5441.9Sperrystatic __inline pt_entry_t * 5451.1Sfvdlvtopte(vaddr_t va) 5461.1Sfvdl{ 5471.1Sfvdl 5481.1Sfvdl KASSERT(va < (L4_SLOT_KERN * NBPD_L4)); 5491.1Sfvdl 5501.1Sfvdl return (PTE_BASE + pl1_i(va)); 5511.1Sfvdl} 5521.1Sfvdl 5531.9Sperrystatic __inline pt_entry_t * 5541.1Sfvdlkvtopte(vaddr_t va) 5551.1Sfvdl{ 5561.1Sfvdl 5571.1Sfvdl KASSERT(va >= (L4_SLOT_KERN * NBPD_L4)); 5581.1Sfvdl 5591.1Sfvdl#ifdef LARGEPAGES 5601.1Sfvdl { 5611.1Sfvdl pd_entry_t *pde; 5621.1Sfvdl 5631.5Syamt pde = L2_BASE + pl2_i(va); 5641.1Sfvdl if (*pde & PG_PS) 5651.1Sfvdl return ((pt_entry_t *)pde); 5661.1Sfvdl } 5671.1Sfvdl#endif 5681.1Sfvdl 5691.1Sfvdl return (PTE_BASE + pl1_i(va)); 5701.1Sfvdl} 5711.1Sfvdl 5721.1Sfvdl#define pmap_pte_set(p, n) x86_atomic_testset_u64(p, n) 5731.11Sad#define pmap_pte_setbits(p, b) x86_atomic_setbits_u64(p, b) 5741.1Sfvdl#define pmap_pte_clearbits(p, b) x86_atomic_clearbits_u64(p, b) 5751.1Sfvdl#define pmap_cpu_has_pg_n() (1) 5761.1Sfvdl#define pmap_cpu_has_invlpg (1) 5771.1Sfvdl 5781.1Sfvdlpaddr_t vtophys __P((vaddr_t)); 5791.1Sfvdlvaddr_t pmap_map __P((vaddr_t, paddr_t, paddr_t, vm_prot_t)); 5801.11Sadvoid pmap_cpu_init_early(struct cpu_info *); 5811.11Sadvoid pmap_cpu_init_late(struct cpu_info *); 5821.11Sadvoid sse2_zero_page(void *); 5831.11Sadvoid sse2_copy_page(void *, void *); 5841.1Sfvdl 5851.1Sfvdl#if 0 /* XXXfvdl was USER_LDT, need to check if that can be supported */ 5861.1Sfvdlvoid pmap_ldt_cleanup __P((struct lwp *)); 5871.1Sfvdl#define PMAP_FORK 5881.1Sfvdl#endif /* USER_LDT */ 5891.1Sfvdl 5901.1Sfvdl/* 5911.1Sfvdl * Hooks for the pool allocator. 5921.1Sfvdl */ 5931.1Sfvdl#define POOL_VTOPHYS(va) vtophys((vaddr_t) (va)) 5941.1Sfvdl 5951.11Sad/* 5961.11Sad * TLB shootdown mailbox. 5971.11Sad */ 5981.11Sad 5991.11Sadstruct pmap_mbox { 6001.11Sad volatile void *mb_pointer; 6011.11Sad volatile uintptr_t mb_addr1; 6021.11Sad volatile uintptr_t mb_addr2; 6031.11Sad volatile uintptr_t mb_head; 6041.11Sad volatile uintptr_t mb_tail; 6051.11Sad volatile uintptr_t mb_global; 6061.11Sad}; 6071.11Sad 6081.1Sfvdl#endif /* _KERNEL && !_LOCORE */ 6091.1Sfvdl#endif /* _AMD64_PMAP_H_ */ 610