1*0a6a1f1dSLionel Sambuc /* $NetBSD: pte.h,v 1.27 2011/02/01 20:09:08 chuck Exp $ */ 2*0a6a1f1dSLionel Sambuc 3*0a6a1f1dSLionel Sambuc /* 4*0a6a1f1dSLionel Sambuc * Copyright (c) 2001 Wasabi Systems, Inc. 5*0a6a1f1dSLionel Sambuc * All rights reserved. 6*0a6a1f1dSLionel Sambuc * 7*0a6a1f1dSLionel Sambuc * Written by Frank van der Linden for Wasabi Systems, Inc. 8*0a6a1f1dSLionel Sambuc * 9*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without 10*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions 11*0a6a1f1dSLionel Sambuc * are met: 12*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright 13*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer. 14*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 15*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the 16*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution. 17*0a6a1f1dSLionel Sambuc * 3. All advertising materials mentioning features or use of this software 18*0a6a1f1dSLionel Sambuc * must display the following acknowledgement: 19*0a6a1f1dSLionel Sambuc * This product includes software developed for the NetBSD Project by 20*0a6a1f1dSLionel Sambuc * Wasabi Systems, Inc. 21*0a6a1f1dSLionel Sambuc * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22*0a6a1f1dSLionel Sambuc * or promote products derived from this software without specific prior 23*0a6a1f1dSLionel Sambuc * written permission. 24*0a6a1f1dSLionel Sambuc * 25*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27*0a6a1f1dSLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28*0a6a1f1dSLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29*0a6a1f1dSLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30*0a6a1f1dSLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31*0a6a1f1dSLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32*0a6a1f1dSLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33*0a6a1f1dSLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34*0a6a1f1dSLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35*0a6a1f1dSLionel Sambuc * POSSIBILITY OF SUCH DAMAGE. 36*0a6a1f1dSLionel Sambuc */ 37*0a6a1f1dSLionel Sambuc 38*0a6a1f1dSLionel Sambuc /* 39*0a6a1f1dSLionel Sambuc * Copyright (c) 1997 Charles D. Cranor and Washington University. 40*0a6a1f1dSLionel Sambuc * All rights reserved. 41*0a6a1f1dSLionel Sambuc * 42*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without 43*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions 44*0a6a1f1dSLionel Sambuc * are met: 45*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright 46*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer. 47*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 48*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the 49*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution. 50*0a6a1f1dSLionel Sambuc * 51*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 52*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 53*0a6a1f1dSLionel Sambuc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 54*0a6a1f1dSLionel Sambuc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 55*0a6a1f1dSLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 56*0a6a1f1dSLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 57*0a6a1f1dSLionel Sambuc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 58*0a6a1f1dSLionel Sambuc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 59*0a6a1f1dSLionel Sambuc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 60*0a6a1f1dSLionel Sambuc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61*0a6a1f1dSLionel Sambuc */ 62*0a6a1f1dSLionel Sambuc 63*0a6a1f1dSLionel Sambuc /* 64*0a6a1f1dSLionel Sambuc * pte.h rewritten by chuck based on the jolitz version, plus random 65*0a6a1f1dSLionel Sambuc * info on the pentium and other processors found on the net. the 66*0a6a1f1dSLionel Sambuc * goal of this rewrite is to provide enough documentation on the MMU 67*0a6a1f1dSLionel Sambuc * hardware that the reader will be able to understand it without having 68*0a6a1f1dSLionel Sambuc * to refer to a hardware manual. 69*0a6a1f1dSLionel Sambuc */ 70*0a6a1f1dSLionel Sambuc 71*0a6a1f1dSLionel Sambuc #ifndef _I386_PTE_H_ 72*0a6a1f1dSLionel Sambuc #define _I386_PTE_H_ 73*0a6a1f1dSLionel Sambuc #ifdef _KERNEL_OPT 74*0a6a1f1dSLionel Sambuc #include "opt_xen.h" 75*0a6a1f1dSLionel Sambuc #endif 76*0a6a1f1dSLionel Sambuc 77*0a6a1f1dSLionel Sambuc /* 78*0a6a1f1dSLionel Sambuc * i386 MMU hardware structure (without PAE extension): 79*0a6a1f1dSLionel Sambuc * 80*0a6a1f1dSLionel Sambuc * the i386 MMU is a two-level MMU which maps 4GB of virtual memory. 81*0a6a1f1dSLionel Sambuc * the pagesize is 4K (4096 [0x1000] bytes), although newer pentium 82*0a6a1f1dSLionel Sambuc * processors can support a 4MB pagesize as well. 83*0a6a1f1dSLionel Sambuc * 84*0a6a1f1dSLionel Sambuc * the first level table (segment table?) is called a "page directory" 85*0a6a1f1dSLionel Sambuc * and it contains 1024 page directory entries (PDEs). each PDE is 86*0a6a1f1dSLionel Sambuc * 4 bytes (an int), so a PD fits in a single 4K page. this page is 87*0a6a1f1dSLionel Sambuc * the page directory page (PDP). each PDE in a PDP maps 4MB of space 88*0a6a1f1dSLionel Sambuc * (1024 * 4MB = 4GB). a PDE contains the physical address of the 89*0a6a1f1dSLionel Sambuc * second level table: the page table. or, if 4MB pages are being used, 90*0a6a1f1dSLionel Sambuc * then the PDE contains the PA of the 4MB page being mapped. 91*0a6a1f1dSLionel Sambuc * 92*0a6a1f1dSLionel Sambuc * a page table consists of 1024 page table entries (PTEs). each PTE is 93*0a6a1f1dSLionel Sambuc * 4 bytes (an int), so a page table also fits in a single 4K page. a 94*0a6a1f1dSLionel Sambuc * 4K page being used as a page table is called a page table page (PTP). 95*0a6a1f1dSLionel Sambuc * each PTE in a PTP maps one 4K page (1024 * 4K = 4MB). a PTE contains 96*0a6a1f1dSLionel Sambuc * the physical address of the page it maps and some flag bits (described 97*0a6a1f1dSLionel Sambuc * below). 98*0a6a1f1dSLionel Sambuc * 99*0a6a1f1dSLionel Sambuc * the processor has a special register, "cr3", which points to the 100*0a6a1f1dSLionel Sambuc * the PDP which is currently controlling the mappings of the virtual 101*0a6a1f1dSLionel Sambuc * address space. 102*0a6a1f1dSLionel Sambuc * 103*0a6a1f1dSLionel Sambuc * the following picture shows the translation process for a 4K page: 104*0a6a1f1dSLionel Sambuc * 105*0a6a1f1dSLionel Sambuc * %cr3 register [PA of PDP] 106*0a6a1f1dSLionel Sambuc * | 107*0a6a1f1dSLionel Sambuc * | 108*0a6a1f1dSLionel Sambuc * | bits <31-22> of VA bits <21-12> of VA bits <11-0> 109*0a6a1f1dSLionel Sambuc * | index the PDP (0 - 1023) index the PTP are the page offset 110*0a6a1f1dSLionel Sambuc * | | | | 111*0a6a1f1dSLionel Sambuc * | v | | 112*0a6a1f1dSLionel Sambuc * +--->+----------+ | | 113*0a6a1f1dSLionel Sambuc * | PD Page | PA of v | 114*0a6a1f1dSLionel Sambuc * | |---PTP-------->+------------+ | 115*0a6a1f1dSLionel Sambuc * | 1024 PDE | | page table |--PTE--+ | 116*0a6a1f1dSLionel Sambuc * | entries | | (aka PTP) | | | 117*0a6a1f1dSLionel Sambuc * +----------+ | 1024 PTE | | | 118*0a6a1f1dSLionel Sambuc * | entries | | | 119*0a6a1f1dSLionel Sambuc * +------------+ | | 120*0a6a1f1dSLionel Sambuc * | | 121*0a6a1f1dSLionel Sambuc * bits <31-12> bits <11-0> 122*0a6a1f1dSLionel Sambuc * p h y s i c a l a d d r 123*0a6a1f1dSLionel Sambuc * 124*0a6a1f1dSLionel Sambuc * the i386 caches PTEs in a TLB. it is important to flush out old 125*0a6a1f1dSLionel Sambuc * TLB mappings when making a change to a mappings. writing to the 126*0a6a1f1dSLionel Sambuc * %cr3 will flush the entire TLB. newer processors also have an 127*0a6a1f1dSLionel Sambuc * instruction that will invalidate the mapping of a single page (which 128*0a6a1f1dSLionel Sambuc * is useful if you are changing a single mappings because it preserves 129*0a6a1f1dSLionel Sambuc * all the cached TLB entries). 130*0a6a1f1dSLionel Sambuc * 131*0a6a1f1dSLionel Sambuc * as shows, bits 31-12 of the PTE contain PA of the page being mapped. 132*0a6a1f1dSLionel Sambuc * the rest of the PTE is defined as follows: 133*0a6a1f1dSLionel Sambuc * bit# name use 134*0a6a1f1dSLionel Sambuc * 11 n/a available for OS use, hardware ignores it 135*0a6a1f1dSLionel Sambuc * 10 n/a available for OS use, hardware ignores it 136*0a6a1f1dSLionel Sambuc * 9 n/a available for OS use, hardware ignores it 137*0a6a1f1dSLionel Sambuc * 8 G global bit (see discussion below) 138*0a6a1f1dSLionel Sambuc * 7 PS page size [for PDEs] (0=4k, 1=4M <if supported>) 139*0a6a1f1dSLionel Sambuc * 6 D dirty (modified) page 140*0a6a1f1dSLionel Sambuc * 5 A accessed (referenced) page 141*0a6a1f1dSLionel Sambuc * 4 PCD cache disable 142*0a6a1f1dSLionel Sambuc * 3 PWT prevent write through (cache) 143*0a6a1f1dSLionel Sambuc * 2 U/S user/supervisor bit (0=supervisor only, 1=both u&s) 144*0a6a1f1dSLionel Sambuc * 1 R/W read/write bit (0=read only, 1=read-write) 145*0a6a1f1dSLionel Sambuc * 0 P present (valid) 146*0a6a1f1dSLionel Sambuc * 147*0a6a1f1dSLionel Sambuc * notes: 148*0a6a1f1dSLionel Sambuc * - PS is only supported on newer processors 149*0a6a1f1dSLionel Sambuc * - PTEs with the G bit are global in the sense that they are not 150*0a6a1f1dSLionel Sambuc * flushed from the TLB when %cr3 is written (to flush, use the 151*0a6a1f1dSLionel Sambuc * "flush single page" instruction). this is only supported on 152*0a6a1f1dSLionel Sambuc * newer processors. this bit can be used to keep the kernel's 153*0a6a1f1dSLionel Sambuc * TLB entries around while context switching. since the kernel 154*0a6a1f1dSLionel Sambuc * is mapped into all processes at the same place it does not make 155*0a6a1f1dSLionel Sambuc * sense to flush these entries when switching from one process' 156*0a6a1f1dSLionel Sambuc * pmap to another. 157*0a6a1f1dSLionel Sambuc * 158*0a6a1f1dSLionel Sambuc * The PAE extension extends the size of the PTE to 64 bits (52bits physical 159*0a6a1f1dSLionel Sambuc * address) and is compatible with the amd64 PTE format. The first level 160*0a6a1f1dSLionel Sambuc * maps 2M, the second 1G, so a third level page table is introduced to 161*0a6a1f1dSLionel Sambuc * map the 4GB virtual address space. This PD has only 4 entries. 162*0a6a1f1dSLionel Sambuc * We can't use recursive mapping at level 3 to map the PD pages, as this 163*0a6a1f1dSLionel Sambuc * would eat one GB of address space. In addition, Xen imposes restrictions 164*0a6a1f1dSLionel Sambuc * on the entries we put in the L3 page (for example, the page pointed to by 165*0a6a1f1dSLionel Sambuc * the last slot can't be shared among different L3 pages), which makes 166*0a6a1f1dSLionel Sambuc * handling this L3 page in the same way we do for L2 on i386 (or L4 on amd64) 167*0a6a1f1dSLionel Sambuc * difficult. For most things we'll just pretend to have only 2 levels, 168*0a6a1f1dSLionel Sambuc * with the 2 high bits of the L2 index being in fact the index in the 169*0a6a1f1dSLionel Sambuc * L3. 170*0a6a1f1dSLionel Sambuc */ 171*0a6a1f1dSLionel Sambuc 172*0a6a1f1dSLionel Sambuc #if !defined(_LOCORE) 173*0a6a1f1dSLionel Sambuc 174*0a6a1f1dSLionel Sambuc /* 175*0a6a1f1dSLionel Sambuc * here we define the data types for PDEs and PTEs 176*0a6a1f1dSLionel Sambuc */ 177*0a6a1f1dSLionel Sambuc #ifdef PAE 178*0a6a1f1dSLionel Sambuc typedef uint64_t pd_entry_t; /* PDE */ 179*0a6a1f1dSLionel Sambuc typedef uint64_t pt_entry_t; /* PTE */ 180*0a6a1f1dSLionel Sambuc #else 181*0a6a1f1dSLionel Sambuc typedef uint32_t pd_entry_t; /* PDE */ 182*0a6a1f1dSLionel Sambuc typedef uint32_t pt_entry_t; /* PTE */ 183*0a6a1f1dSLionel Sambuc #endif 184*0a6a1f1dSLionel Sambuc 185*0a6a1f1dSLionel Sambuc #endif 186*0a6a1f1dSLionel Sambuc 187*0a6a1f1dSLionel Sambuc /* 188*0a6a1f1dSLionel Sambuc * now we define various for playing with virtual addresses 189*0a6a1f1dSLionel Sambuc */ 190*0a6a1f1dSLionel Sambuc 191*0a6a1f1dSLionel Sambuc #ifdef PAE 192*0a6a1f1dSLionel Sambuc #define L1_SHIFT 12 193*0a6a1f1dSLionel Sambuc #define L2_SHIFT 21 194*0a6a1f1dSLionel Sambuc #define L3_SHIFT 30 195*0a6a1f1dSLionel Sambuc #define NBPD_L1 (1ULL << L1_SHIFT) /* # bytes mapped by L1 ent (4K) */ 196*0a6a1f1dSLionel Sambuc #define NBPD_L2 (1ULL << L2_SHIFT) /* # bytes mapped by L2 ent (2MB) */ 197*0a6a1f1dSLionel Sambuc #define NBPD_L3 (1ULL << L3_SHIFT) /* # bytes mapped by L3 ent (1GB) */ 198*0a6a1f1dSLionel Sambuc 199*0a6a1f1dSLionel Sambuc #define L3_MASK 0xc0000000 200*0a6a1f1dSLionel Sambuc #define L2_REALMASK 0x3fe00000 201*0a6a1f1dSLionel Sambuc #define L2_MASK (L2_REALMASK | L3_MASK) 202*0a6a1f1dSLionel Sambuc #define L1_MASK 0x001ff000 203*0a6a1f1dSLionel Sambuc 204*0a6a1f1dSLionel Sambuc #define L3_FRAME (L3_MASK) 205*0a6a1f1dSLionel Sambuc #define L2_FRAME (L3_FRAME | L2_MASK) 206*0a6a1f1dSLionel Sambuc #define L1_FRAME (L2_FRAME|L1_MASK) 207*0a6a1f1dSLionel Sambuc 208*0a6a1f1dSLionel Sambuc #define PG_FRAME 0x000ffffffffff000ULL /* page frame mask */ 209*0a6a1f1dSLionel Sambuc #define PG_LGFRAME 0x000fffffffe00000ULL /* large (2MB) page frame mask */ 210*0a6a1f1dSLionel Sambuc 211*0a6a1f1dSLionel Sambuc /* macros to get real L2 and L3 index, from our "extended" L2 index */ 212*0a6a1f1dSLionel Sambuc #define l2tol3(idx) ((idx) >> (L3_SHIFT - L2_SHIFT)) 213*0a6a1f1dSLionel Sambuc #define l2tol2(idx) ((idx) & (L2_REALMASK >> L2_SHIFT)) 214*0a6a1f1dSLionel Sambuc 215*0a6a1f1dSLionel Sambuc #else /* PAE */ 216*0a6a1f1dSLionel Sambuc 217*0a6a1f1dSLionel Sambuc #define L1_SHIFT 12 218*0a6a1f1dSLionel Sambuc #define L2_SHIFT 22 219*0a6a1f1dSLionel Sambuc #define NBPD_L1 (1UL << L1_SHIFT) /* # bytes mapped by L1 ent (4K) */ 220*0a6a1f1dSLionel Sambuc #define NBPD_L2 (1UL << L2_SHIFT) /* # bytes mapped by L2 ent (4MB) */ 221*0a6a1f1dSLionel Sambuc 222*0a6a1f1dSLionel Sambuc #define L2_MASK 0xffc00000 223*0a6a1f1dSLionel Sambuc #define L1_MASK 0x003ff000 224*0a6a1f1dSLionel Sambuc 225*0a6a1f1dSLionel Sambuc #define L2_FRAME (L2_MASK) 226*0a6a1f1dSLionel Sambuc #define L1_FRAME (L2_FRAME|L1_MASK) 227*0a6a1f1dSLionel Sambuc 228*0a6a1f1dSLionel Sambuc #define PG_FRAME 0xfffff000 /* page frame mask */ 229*0a6a1f1dSLionel Sambuc #define PG_LGFRAME 0xffc00000 /* large (4MB) page frame mask */ 230*0a6a1f1dSLionel Sambuc 231*0a6a1f1dSLionel Sambuc #endif /* PAE */ 232*0a6a1f1dSLionel Sambuc /* 233*0a6a1f1dSLionel Sambuc * here we define the bits of the PDE/PTE, as described above: 234*0a6a1f1dSLionel Sambuc * 235*0a6a1f1dSLionel Sambuc * XXXCDC: need to rename these (PG_u == ugly). 236*0a6a1f1dSLionel Sambuc */ 237*0a6a1f1dSLionel Sambuc 238*0a6a1f1dSLionel Sambuc #define PG_V 0x00000001 /* valid entry */ 239*0a6a1f1dSLionel Sambuc #define PG_RO 0x00000000 /* read-only page */ 240*0a6a1f1dSLionel Sambuc #define PG_RW 0x00000002 /* read-write page */ 241*0a6a1f1dSLionel Sambuc #define PG_u 0x00000004 /* user accessible page */ 242*0a6a1f1dSLionel Sambuc #define PG_PROT 0x00000806 /* all protection bits */ 243*0a6a1f1dSLionel Sambuc #define PG_WT 0x00000008 /* write through */ 244*0a6a1f1dSLionel Sambuc #define PG_N 0x00000010 /* non-cacheable */ 245*0a6a1f1dSLionel Sambuc #define PG_U 0x00000020 /* has been used */ 246*0a6a1f1dSLionel Sambuc #define PG_M 0x00000040 /* has been modified */ 247*0a6a1f1dSLionel Sambuc #define PG_PAT 0x00000080 /* PAT (on pte) */ 248*0a6a1f1dSLionel Sambuc #define PG_PS 0x00000080 /* 4MB page size (2MB for PAE) */ 249*0a6a1f1dSLionel Sambuc #define PG_G 0x00000100 /* global, don't TLB flush */ 250*0a6a1f1dSLionel Sambuc #define PG_AVAIL1 0x00000200 /* ignored by hardware */ 251*0a6a1f1dSLionel Sambuc #define PG_AVAIL2 0x00000400 /* ignored by hardware */ 252*0a6a1f1dSLionel Sambuc #define PG_AVAIL3 0x00000800 /* ignored by hardware */ 253*0a6a1f1dSLionel Sambuc #define PG_LGPAT 0x00001000 /* PAT on large pages */ 254*0a6a1f1dSLionel Sambuc 255*0a6a1f1dSLionel Sambuc /* 256*0a6a1f1dSLionel Sambuc * various short-hand protection codes 257*0a6a1f1dSLionel Sambuc */ 258*0a6a1f1dSLionel Sambuc 259*0a6a1f1dSLionel Sambuc #define PG_KR 0x00000000 /* kernel read-only */ 260*0a6a1f1dSLionel Sambuc #define PG_KW 0x00000002 /* kernel read-write */ 261*0a6a1f1dSLionel Sambuc 262*0a6a1f1dSLionel Sambuc #ifdef PAE 263*0a6a1f1dSLionel Sambuc #define PG_NX 0x8000000000000000ULL /* No-execute */ 264*0a6a1f1dSLionel Sambuc #else 265*0a6a1f1dSLionel Sambuc #define PG_NX 0 /* dummy */ 266*0a6a1f1dSLionel Sambuc #endif 267*0a6a1f1dSLionel Sambuc 268*0a6a1f1dSLionel Sambuc #include <x86/pte.h> 269*0a6a1f1dSLionel Sambuc 270*0a6a1f1dSLionel Sambuc #endif /* _I386_PTE_H_ */ 271