00001 /* 00002 * 00003 * Copyright (c) 1997 Charles D. Cranor and Washington University. 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 3. All advertising materials mentioning features or use of this software 00015 * must display the following acknowledgment: 00016 * This product includes software developed by Charles D. Cranor and 00017 * Washington University. 00018 * 4. The name of the author may not be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00022 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00024 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00025 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00026 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00027 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00028 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00029 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00030 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 #ifndef __MACHDEP_PTE_H__ 00033 #define __MACHDEP_PTE_H__ 00034 /* 00035 * pte.h rewritten by chuck based on the jolitz version, plus random 00036 * info on the pentium and other processors found on the net. the 00037 * goal of this rewrite is to provide enough documentation on the MMU 00038 * hardware that the reader will be able to understand it without having 00039 * to refer to a hardware manual. 00040 */ 00041 00042 /* 00043 * i386 MMU hardware structure: 00044 * 00045 * the i386 MMU is a two-level MMU which maps 4GB of virtual memory. 00046 * the pagesize is 4K (4096 [0x1000] bytes), although newer pentium 00047 * processors can support a 4MB pagesize as well. 00048 * 00049 * the first level table (segment table?) is called a "page directory" 00050 * and it contains 1024 page directory entries (PDEs). each PDE is 00051 * 4 bytes (an int), so a PD fits in a single 4K page. this page is 00052 * the page directory page (PDP). each PDE in a PDP maps 4MB of space 00053 * (1024 * 4MB = 4GB). a PDE contains the physical address of the 00054 * second level table: the page table. or, if 4MB pages are being used, 00055 * then the PDE contains the PA of the 4MB page being mapped. 00056 * 00057 * a page table consists of 1024 page table entries (PTEs). each PTE is 00058 * 4 bytes (an int), so a page table also fits in a single 4K page. a 00059 * 4K page being used as a page table is called a page table page (PTP). 00060 * each PTE in a PTP maps one 4K page (1024 * 4K = 4MB). a PTE contains 00061 * the physical address of the page it maps and some flag bits (described 00062 * below). 00063 * 00064 * the processor has a special register, "cr3", which points to the 00065 * the PDP which is currently controlling the mappings of the virtual 00066 * address space. 00067 * 00068 * the following picture shows the translation process for a 4K page: 00069 * 00070 * %cr3 register [PA of PDP] 00071 * | 00072 * | 00073 * | bits <31-22> of VA bits <21-12> of VA bits <11-0> 00074 * | index the PDP (0 - 1023) index the PTP are the page offset 00075 * | | | | 00076 * | v | | 00077 * +--->+----------+ | | 00078 * | PD Page | PA of v | 00079 * | |---PTP-------->+------------+ | 00080 * | 1024 PDE | | page table |--PTE--+ | 00081 * | entries | | (aka PTP) | | | 00082 * +----------+ | 1024 PTE | | | 00083 * | entries | | | 00084 * +------------+ | | 00085 * | | 00086 * bits <31-12> bits <11-0> 00087 * p h y s i c a l a d d r 00088 * 00089 * the i386 caches PTEs in a TLB. it is important to flush out old 00090 * TLB mappings when making a change to a mappings. writing to the 00091 * %cr3 will flush the entire TLB. newer processors also have an 00092 * instruction that will invalidate the mapping of a single page (which 00093 * is useful if you are changing a single mappings because it preserves 00094 * all the cached TLB entries). 00095 * 00096 * as shows, bits 31-12 of the PTE contain PA of the page being mapped. 00097 * the rest of the PTE is defined as follows: 00098 * bit# name use 00099 * 11 n/a available for OS use, hardware ignores it 00100 * 10 n/a available for OS use, hardware ignores it 00101 * 9 n/a available for OS use, hardware ignores it 00102 * 8 G global bit (see discussion below) 00103 * 7 PS page size [for PDEs] (0=4k, 1=4M <if supported>) 00104 * 6 D dirty (modified) page 00105 * 5 A accessed (referenced) page 00106 * 4 PCD cache disable 00107 * 3 PWT prevent write through (cache) 00108 * 2 U/S user/supervisor bit (0=supervisor only, 1=both u&s) 00109 * 1 R/W read/write bit (0=read only, 1=read-write) 00110 * 0 P present (valid) 00111 * 00112 * notes: 00113 * - on the i386 the R/W bit is ignored if processor is in supervisor 00114 * state (bug!) 00115 * - PS is only supported on newer processors 00116 * - PTEs with the G bit are global in the sense that they are not 00117 * flushed from the TLB when %cr3 is written (to flush, use the 00118 * "flush single page" instruction). this is only supported on 00119 * newer processors. this bit can be used to keep the kernel's 00120 * TLB entries around while context switching. since the kernel 00121 * is mapped into all processes at the same place it does not make 00122 * sense to flush these entries when switching from one process' 00123 * pmap to another. 00124 */ 00125 00126 #if !defined(__LOCORE__) 00127 00128 /* 00129 * here we define the data types for PDEs and PTEs 00130 */ 00131 00132 typedef u_int32_t pd_entry_t; /* PDE */ 00133 typedef u_int32_t pt_entry_t; /* PTE */ 00134 00135 #endif 00136 00137 /* 00138 * now we define various for playing with virtual addresses 00139 */ 00140 00141 #define PDSHIFT 22 /* offset of PD index in VA */ 00142 #define PTSHIFT 12 /* offset of PT index in VA */ 00143 #define NBPD (1 << PDSHIFT) /* # bytes mapped by PD (4MB) */ 00144 #define PDOFSET (NBPD-1) /* mask for non-PD part of VA */ 00145 00146 #define PGSHIFT 12 /* LOG2(NBPG) */ 00147 #define NBPG (1 << PGSHIFT) /* bytes/page */ 00148 #define PGOFSET (NBPG-1) /* byte offset into page */ 00149 00150 #if 0 /* not used? */ 00151 #define NPTEPD (NBPD / NBPG) /* # of PTEs in a PD */ 00152 #else 00153 #define PTES_PER_PTP (NBPD / NBPG) /* # of PTEs in a PTP */ 00154 #endif 00155 #define PD_MASK 0xffc00000 /* page directory address bits */ 00156 #define PT_MASK 0x003ff000 /* page table address bits */ 00157 00158 #define ROUND_PAGE(x) ((((unsigned)(x)) + PGOFSET) & ~PGOFSET) 00159 #define TRUNC_PAGE(x) ((unsigned)(x) & ~PGOFSET) 00160 00161 /* 00162 * here we define the bits of the PDE/PTE, as described above: 00163 * 00164 * XXXCDC: need to rename these (PG_u == ugly). 00165 */ 00166 00167 #define PG_V 0x00000001 /* valid entry */ 00168 #define PG_RO 0x00000000 /* read-only page */ 00169 #define PG_RW 0x00000002 /* read-write page */ 00170 #define PG_u 0x00000004 /* user accessible page */ 00171 #define PG_PROT 0x00000006 /* all protection bits */ 00172 #define PG_N 0x00000018 /* non-cacheable */ 00173 #define PG_U 0x00000020 /* has been used */ 00174 #define PG_M 0x00000040 /* has been modified */ 00175 #define PG_PS 0x00000080 /* 4MB page size */ 00176 #define PG_G 0x00000100 /* global, don't TLB flush */ 00177 #define PG_AVAIL1 0x00000200 /* ignored by hardware */ 00178 #define PG_AVAIL2 0x00000400 /* ignored by hardware */ 00179 #define PG_AVAIL3 0x00000800 /* ignored by hardware */ 00180 #define PG_FRAME 0xfffff000 /* page frame mask */ 00181 00182 /* 00183 * various short-hand protection codes 00184 */ 00185 00186 #define PG_KR 0x00000000 /* kernel read-only */ 00187 #define PG_KW 0x00000002 /* kernel read-write */ 00188 00189 /* 00190 * page protection exception bits 00191 */ 00192 00193 #define PGEX_P 0x01 /* protection violation (vs. no mapping) */ 00194 #define PGEX_W 0x02 /* exception during a write cycle */ 00195 #define PGEX_U 0x04 /* exception while in user mode (upl) */ 00196 00197 #endif