1 1.4 riastrad /* $NetBSD: pmap_private.h,v 1.4 2022/08/21 09:12:43 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright (c) 1997 Charles D. Cranor and Washington University. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * Redistribution and use in source and binary forms, with or without 8 1.1 riastrad * modification, are permitted provided that the following conditions 9 1.1 riastrad * are met: 10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 11 1.1 riastrad * notice, this list of conditions and the following disclaimer. 12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 14 1.1 riastrad * documentation and/or other materials provided with the distribution. 15 1.1 riastrad * 16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 riastrad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 riastrad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 riastrad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 riastrad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.1 riastrad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 riastrad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 riastrad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 riastrad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.1 riastrad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 riastrad */ 27 1.1 riastrad 28 1.1 riastrad /* 29 1.1 riastrad * Copyright (c) 2001 Wasabi Systems, Inc. 30 1.1 riastrad * All rights reserved. 31 1.1 riastrad * 32 1.1 riastrad * Written by Frank van der Linden for Wasabi Systems, Inc. 33 1.1 riastrad * 34 1.1 riastrad * Redistribution and use in source and binary forms, with or without 35 1.1 riastrad * modification, are permitted provided that the following conditions 36 1.1 riastrad * are met: 37 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 38 1.1 riastrad * notice, this list of conditions and the following disclaimer. 39 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 40 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 41 1.1 riastrad * documentation and/or other materials provided with the distribution. 42 1.1 riastrad * 3. All advertising materials mentioning features or use of this software 43 1.1 riastrad * must display the following acknowledgement: 44 1.1 riastrad * This product includes software developed for the NetBSD Project by 45 1.1 riastrad * Wasabi Systems, Inc. 46 1.1 riastrad * 4. The name of Wasabi Systems, Inc. may not be used to endorse 47 1.1 riastrad * or promote products derived from this software without specific prior 48 1.1 riastrad * written permission. 49 1.1 riastrad * 50 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 51 1.1 riastrad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 52 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 53 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 54 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 55 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 56 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 57 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 58 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 59 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 60 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 61 1.1 riastrad */ 62 1.1 riastrad 63 1.1 riastrad #ifndef _I386_PMAP_H_ 64 1.1 riastrad #define _I386_PMAP_H_ 65 1.1 riastrad 66 1.1 riastrad #if defined(_KERNEL_OPT) 67 1.1 riastrad #include "opt_xen.h" 68 1.1 riastrad #endif 69 1.1 riastrad 70 1.1 riastrad #include <sys/atomic.h> 71 1.1 riastrad 72 1.1 riastrad #include <i386/pte.h> 73 1.1 riastrad #include <i386/vmparam.h> 74 1.1 riastrad #include <machine/segments.h> 75 1.1 riastrad #if defined(_KERNEL) 76 1.1 riastrad #include <machine/cpufunc.h> 77 1.1 riastrad #endif 78 1.1 riastrad 79 1.1 riastrad #include <uvm/uvm_object.h> 80 1.1 riastrad #ifdef XENPV 81 1.1 riastrad #include <xen/xenfunc.h> 82 1.1 riastrad #include <xen/xenpmap.h> 83 1.1 riastrad #endif /* XENPV */ 84 1.1 riastrad 85 1.1 riastrad /* 86 1.1 riastrad * see pte.h for a description of i386 MMU terminology and hardware 87 1.1 riastrad * interface. 88 1.1 riastrad * 89 1.1 riastrad * a pmap describes a processes' 4GB virtual address space. when PAE 90 1.1 riastrad * is not in use, this virtual address space can be broken up into 1024 4MB 91 1.1 riastrad * regions which are described by PDEs in the PDP. the PDEs are defined as 92 1.1 riastrad * follows: 93 1.1 riastrad * 94 1.1 riastrad * (ranges are inclusive -> exclusive, just like vm_map_entry start/end) 95 1.1 riastrad * (the following assumes that KERNBASE is 0xc0000000) 96 1.1 riastrad * 97 1.1 riastrad * PDE#s VA range usage 98 1.1 riastrad * 0->766 0x0 -> 0xbfc00000 user address space 99 1.1 riastrad * 767 0xbfc00000-> recursive mapping of PDP (used for 100 1.1 riastrad * 0xc0000000 linear mapping of PTPs) 101 1.1 riastrad * 768->1023 0xc0000000-> kernel address space (constant 102 1.1 riastrad * 0xffc00000 across all pmap's/processes) 103 1.1 riastrad * <end> 104 1.1 riastrad * 105 1.1 riastrad * 106 1.1 riastrad * note: a recursive PDP mapping provides a way to map all the PTEs for 107 1.1 riastrad * a 4GB address space into a linear chunk of virtual memory. in other 108 1.1 riastrad * words, the PTE for page 0 is the first int mapped into the 4MB recursive 109 1.1 riastrad * area. the PTE for page 1 is the second int. the very last int in the 110 1.1 riastrad * 4MB range is the PTE that maps VA 0xfffff000 (the last page in a 4GB 111 1.1 riastrad * address). 112 1.1 riastrad * 113 1.1 riastrad * all pmap's PD's must have the same values in slots 768->1023 so that 114 1.1 riastrad * the kernel is always mapped in every process. these values are loaded 115 1.1 riastrad * into the PD at pmap creation time. 116 1.1 riastrad * 117 1.1 riastrad * at any one time only one pmap can be active on a processor. this is 118 1.1 riastrad * the pmap whose PDP is pointed to by processor register %cr3. this pmap 119 1.1 riastrad * will have all its PTEs mapped into memory at the recursive mapping 120 1.1 riastrad * point (slot #767 as show above). when the pmap code wants to find the 121 1.1 riastrad * PTE for a virtual address, all it has to do is the following: 122 1.1 riastrad * 123 1.1 riastrad * address of PTE = (767 * 4MB) + (VA / PAGE_SIZE) * sizeof(pt_entry_t) 124 1.1 riastrad * = 0xbfc00000 + (VA / 4096) * 4 125 1.1 riastrad * 126 1.1 riastrad * what happens if the pmap layer is asked to perform an operation 127 1.1 riastrad * on a pmap that is not the one which is currently active? in that 128 1.1 riastrad * case we temporarily load this pmap, perform the operation, and mark 129 1.1 riastrad * the currently active one as pending lazy reload. 130 1.1 riastrad * 131 1.1 riastrad * the following figure shows the effects of the recursive PDP mapping: 132 1.1 riastrad * 133 1.1 riastrad * PDP (%cr3) 134 1.1 riastrad * +----+ 135 1.1 riastrad * | 0| -> PTP#0 that maps VA 0x0 -> 0x400000 136 1.1 riastrad * | | 137 1.1 riastrad * | | 138 1.1 riastrad * | 767| -> points back to PDP (%cr3) mapping VA 0xbfc00000 -> 0xc0000000 139 1.1 riastrad * | 768| -> first kernel PTP (maps 0xc0000000 -> 0xc0400000) 140 1.1 riastrad * | | 141 1.1 riastrad * +----+ 142 1.1 riastrad * 143 1.1 riastrad * note that the PDE#767 VA (0xbfc00000) is defined as "PTE_BASE" 144 1.1 riastrad * 145 1.1 riastrad * starting at VA 0xbfc00000 the current active PDP (%cr3) acts as a 146 1.1 riastrad * PTP: 147 1.1 riastrad * 148 1.1 riastrad * PTP#767 == PDP(%cr3) => maps VA 0xbfc00000 -> 0xc0000000 149 1.1 riastrad * +----+ 150 1.1 riastrad * | 0| -> maps the contents of PTP#0 at VA 0xbfc00000->0xbfc01000 151 1.1 riastrad * | | 152 1.1 riastrad * | | 153 1.1 riastrad * | 767| -> maps contents of PTP#767 (the PDP) at VA 0xbfeff000 154 1.1 riastrad * | 768| -> maps contents of first kernel PTP 155 1.1 riastrad * | | 156 1.1 riastrad * |1023| 157 1.1 riastrad * +----+ 158 1.1 riastrad * 159 1.1 riastrad * note that mapping of the PDP at PTP#767's VA (0xbfeff000) is 160 1.1 riastrad * defined as "PDP_BASE".... within that mapping there are two 161 1.1 riastrad * defines: 162 1.1 riastrad * "PDP_PDE" (0xbfeffbfc) is the VA of the PDE in the PDP 163 1.1 riastrad * which points back to itself. 164 1.1 riastrad * 165 1.1 riastrad * - PAE support - 166 1.1 riastrad * --------------- 167 1.1 riastrad * 168 1.1 riastrad * PAE adds another layer of indirection during address translation, breaking 169 1.1 riastrad * up the translation process in 3 different levels: 170 1.1 riastrad * - L3 page directory, containing 4 * 64-bits addresses (index determined by 171 1.1 riastrad * bits [31:30] from the virtual address). This breaks up the address space 172 1.1 riastrad * in 4 1GB regions. 173 1.1 riastrad * - the PD (L2), containing 512 64-bits addresses, breaking each L3 region 174 1.1 riastrad * in 512 * 2MB regions. 175 1.1 riastrad * - the PT (L1), also containing 512 64-bits addresses (at L1, the size of 176 1.1 riastrad * the pages is still 4K). 177 1.1 riastrad * 178 1.1 riastrad * The kernel virtual space is mapped by the last entry in the L3 page, 179 1.1 riastrad * the first 3 entries mapping the user VA space. 180 1.1 riastrad * 181 1.1 riastrad * Because the L3 has only 4 entries of 1GB each, we can't use recursive 182 1.1 riastrad * mappings at this level for PDP_PDE (this would eat up 2 of the 4GB 183 1.1 riastrad * virtual space). There are also restrictions imposed by Xen on the 184 1.1 riastrad * last entry of the L3 PD (reference count to this page cannot be 185 1.1 riastrad * bigger than 1), which makes it hard to use one L3 page per pmap to 186 1.1 riastrad * switch between pmaps using %cr3. 187 1.1 riastrad * 188 1.1 riastrad * As such, each CPU gets its own L3 page that is always loaded into its %cr3 189 1.1 riastrad * (ci_pae_l3_pd in the associated cpu_info struct). We claim that the VM has 190 1.1 riastrad * only a 2-level PTP (similar to the non-PAE case). L2 PD is now 4 contiguous 191 1.1 riastrad * pages long (corresponding to the 4 entries of the L3), and the different 192 1.1 riastrad * index/slots (like PDP_PDE) are adapted accordingly. 193 1.1 riastrad * 194 1.1 riastrad * Kernel space remains in L3[3], L3[0-2] maps the user VA space. Switching 195 1.1 riastrad * between pmaps consists in modifying the first 3 entries of the CPU's L3 page. 196 1.1 riastrad * 197 1.1 riastrad * PTE_BASE will need 4 entries in the L2 PD pages to map the L2 pages 198 1.1 riastrad * recursively. 199 1.1 riastrad * 200 1.1 riastrad * In addition, for Xen, we can't recursively map L3[3] (Xen wants the ref 201 1.1 riastrad * count on this page to be exactly one), so we use a shadow PD page for 202 1.1 riastrad * the last L2 PD. The shadow page could be static too, but to make pm_pdir[] 203 1.1 riastrad * contiguous we'll allocate/copy one page per pmap. 204 1.1 riastrad */ 205 1.1 riastrad 206 1.1 riastrad /* 207 1.1 riastrad * the following defines give the virtual addresses of various MMU 208 1.1 riastrad * data structures: 209 1.1 riastrad * PTE_BASE: the base VA of the linear PTE mappings 210 1.1 riastrad * PDP_BASE: the base VA of the recursive mapping of the PDP 211 1.1 riastrad * PDP_PDE: the VA of the PDE that points back to the PDP 212 1.1 riastrad */ 213 1.1 riastrad 214 1.1 riastrad #define PTE_BASE ((pt_entry_t *) (PDIR_SLOT_PTE * NBPD_L2)) 215 1.1 riastrad 216 1.1 riastrad #define L1_BASE PTE_BASE 217 1.1 riastrad 218 1.1 riastrad #define L2_BASE ((pd_entry_t *)((char *)L1_BASE + L2_SLOT_PTE * NBPD_L1)) 219 1.1 riastrad 220 1.1 riastrad #define PDP_PDE (L2_BASE + PDIR_SLOT_PTE) 221 1.1 riastrad 222 1.1 riastrad #define PDP_BASE L2_BASE 223 1.1 riastrad 224 1.1 riastrad #define NPDPG (PAGE_SIZE / sizeof (pd_entry_t)) 225 1.1 riastrad 226 1.1 riastrad #define PTP_MASK_INITIALIZER { L1_MASK, L2_MASK } 227 1.1 riastrad #define PTP_FRAME_INITIALIZER { L1_FRAME, L2_FRAME } 228 1.1 riastrad #define PTP_SHIFT_INITIALIZER { L1_SHIFT, L2_SHIFT } 229 1.1 riastrad #define NKPTP_INITIALIZER { NKL1_START_ENTRIES, NKL2_START_ENTRIES } 230 1.1 riastrad #define NKPTPMAX_INITIALIZER { NKL1_MAX_ENTRIES, NKL2_MAX_ENTRIES } 231 1.1 riastrad #define NBPD_INITIALIZER { NBPD_L1, NBPD_L2 } 232 1.1 riastrad #define PDES_INITIALIZER { L2_BASE } 233 1.1 riastrad 234 1.2 riastrad #define PTP_LEVELS 2 235 1.2 riastrad 236 1.1 riastrad /* 237 1.1 riastrad * PTE_AVL usage: we make use of the ignored bits of the PTE 238 1.1 riastrad */ 239 1.1 riastrad #define PTE_WIRED PTE_AVL1 /* Wired Mapping */ 240 1.1 riastrad #define PTE_PVLIST PTE_AVL2 /* Mapping has entry on pvlist */ 241 1.1 riastrad #define PTE_X PTE_AVL3 /* Executable */ 242 1.1 riastrad 243 1.1 riastrad /* XXX To be deleted. */ 244 1.1 riastrad #define PG_W PTE_WIRED 245 1.1 riastrad #define PG_PVLIST PTE_PVLIST 246 1.1 riastrad #define PG_X PTE_X 247 1.1 riastrad 248 1.1 riastrad #define _MACHINE_PMAP_PRIVATE_H_X86 249 1.1 riastrad #include <x86/pmap_private.h> 250 1.1 riastrad #undef _MACHINE_PMAP_PRIVATE_H_X86 251 1.1 riastrad 252 1.1 riastrad #ifndef XENPV 253 1.3 riastrad 254 1.1 riastrad #define pmap_pa2pte(a) (a) 255 1.1 riastrad #define pmap_pte2pa(a) ((a) & PTE_FRAME) 256 1.1 riastrad #define pmap_pte_set(p, n) do { *(p) = (n); } while (0) 257 1.1 riastrad #define pmap_pte_flush() /* nothing */ 258 1.1 riastrad 259 1.1 riastrad #ifdef PAE 260 1.1 riastrad #define pmap_pte_cas(p, o, n) atomic_cas_64((p), (o), (n)) 261 1.1 riastrad #define pmap_pte_testset(p, n) \ 262 1.1 riastrad atomic_swap_64((volatile uint64_t *)p, n) 263 1.1 riastrad #define pmap_pte_setbits(p, b) \ 264 1.1 riastrad atomic_or_64((volatile uint64_t *)p, b) 265 1.1 riastrad #define pmap_pte_clearbits(p, b) \ 266 1.1 riastrad atomic_and_64((volatile uint64_t *)p, ~(b)) 267 1.1 riastrad #else /* PAE */ 268 1.1 riastrad #define pmap_pte_cas(p, o, n) atomic_cas_32((p), (o), (n)) 269 1.1 riastrad #define pmap_pte_testset(p, n) \ 270 1.1 riastrad atomic_swap_ulong((volatile unsigned long *)p, n) 271 1.1 riastrad #define pmap_pte_setbits(p, b) \ 272 1.1 riastrad atomic_or_ulong((volatile unsigned long *)p, b) 273 1.1 riastrad #define pmap_pte_clearbits(p, b) \ 274 1.1 riastrad atomic_and_ulong((volatile unsigned long *)p, ~(b)) 275 1.1 riastrad #endif /* PAE */ 276 1.1 riastrad 277 1.1 riastrad #else /* XENPV */ 278 1.3 riastrad 279 1.1 riastrad extern kmutex_t pte_lock; 280 1.1 riastrad 281 1.1 riastrad static __inline pt_entry_t 282 1.1 riastrad pmap_pa2pte(paddr_t pa) 283 1.1 riastrad { 284 1.1 riastrad return (pt_entry_t)xpmap_ptom_masked(pa); 285 1.1 riastrad } 286 1.1 riastrad 287 1.1 riastrad static __inline paddr_t 288 1.1 riastrad pmap_pte2pa(pt_entry_t pte) 289 1.1 riastrad { 290 1.1 riastrad return xpmap_mtop_masked(pte & PTE_FRAME); 291 1.1 riastrad } 292 1.3 riastrad 293 1.1 riastrad static __inline void 294 1.1 riastrad pmap_pte_set(pt_entry_t *pte, pt_entry_t npte) 295 1.1 riastrad { 296 1.1 riastrad int s = splvm(); 297 1.1 riastrad xpq_queue_pte_update(xpmap_ptetomach(pte), npte); 298 1.1 riastrad splx(s); 299 1.1 riastrad } 300 1.1 riastrad 301 1.1 riastrad static __inline pt_entry_t 302 1.1 riastrad pmap_pte_cas(volatile pt_entry_t *ptep, pt_entry_t o, pt_entry_t n) 303 1.1 riastrad { 304 1.1 riastrad pt_entry_t opte; 305 1.1 riastrad 306 1.1 riastrad mutex_enter(&pte_lock); 307 1.1 riastrad opte = *ptep; 308 1.1 riastrad if (opte == o) { 309 1.1 riastrad xpq_queue_pte_update(xpmap_ptetomach(__UNVOLATILE(ptep)), n); 310 1.1 riastrad xpq_flush_queue(); 311 1.1 riastrad } 312 1.1 riastrad mutex_exit(&pte_lock); 313 1.1 riastrad return opte; 314 1.1 riastrad } 315 1.1 riastrad 316 1.1 riastrad static __inline pt_entry_t 317 1.1 riastrad pmap_pte_testset(volatile pt_entry_t *pte, pt_entry_t npte) 318 1.1 riastrad { 319 1.1 riastrad pt_entry_t opte; 320 1.1 riastrad 321 1.1 riastrad mutex_enter(&pte_lock); 322 1.1 riastrad opte = *pte; 323 1.3 riastrad xpq_queue_pte_update(xpmap_ptetomach(__UNVOLATILE(pte)), npte); 324 1.1 riastrad xpq_flush_queue(); 325 1.1 riastrad mutex_exit(&pte_lock); 326 1.1 riastrad return opte; 327 1.1 riastrad } 328 1.1 riastrad 329 1.1 riastrad static __inline void 330 1.1 riastrad pmap_pte_setbits(volatile pt_entry_t *pte, pt_entry_t bits) 331 1.1 riastrad { 332 1.1 riastrad mutex_enter(&pte_lock); 333 1.1 riastrad xpq_queue_pte_update(xpmap_ptetomach(__UNVOLATILE(pte)), (*pte) | bits); 334 1.1 riastrad xpq_flush_queue(); 335 1.1 riastrad mutex_exit(&pte_lock); 336 1.1 riastrad } 337 1.1 riastrad 338 1.1 riastrad static __inline void 339 1.1 riastrad pmap_pte_clearbits(volatile pt_entry_t *pte, pt_entry_t bits) 340 1.3 riastrad { 341 1.1 riastrad mutex_enter(&pte_lock); 342 1.1 riastrad xpq_queue_pte_update(xpmap_ptetomach(__UNVOLATILE(pte)), 343 1.1 riastrad (*pte) & ~bits); 344 1.1 riastrad xpq_flush_queue(); 345 1.1 riastrad mutex_exit(&pte_lock); 346 1.1 riastrad } 347 1.1 riastrad 348 1.1 riastrad static __inline void 349 1.1 riastrad pmap_pte_flush(void) 350 1.1 riastrad { 351 1.1 riastrad int s = splvm(); 352 1.1 riastrad xpq_flush_queue(); 353 1.1 riastrad splx(s); 354 1.1 riastrad } 355 1.1 riastrad 356 1.1 riastrad #endif 357 1.1 riastrad 358 1.1 riastrad struct vm_map; 359 1.1 riastrad struct trapframe; 360 1.1 riastrad struct pcb; 361 1.1 riastrad 362 1.1 riastrad int pmap_exec_fixup(struct vm_map *, struct trapframe *, struct pcb *); 363 1.1 riastrad 364 1.1 riastrad #endif /* _I386_PMAP_H_ */ 365