pte.h revision 1.10 1 /*
2 *
3 * Copyright (c) 1997 Charles D. Cranor and Washington University.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgment:
16 * This product includes software developed by Charles D. Cranor and
17 * Washington University.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * pte.h rewritten by chuck based on the jolitz version, plus random
35 * info on the pentium and other processors found on the net. the
36 * goal of this rewrite is to provide enough documentation on the MMU
37 * hardware that the reader will be able to understand it without having
38 * to refer to a hardware manual.
39 */
40
41 #ifndef _I386_PTE_H_
42 #define _I386_PTE_H_
43
44 /*
45 * i386 MMU hardware structure:
46 *
47 * the i386 MMU is a two-level MMU which maps 4GB of virtual memory.
48 * the pagesize is 4K (4096 [0x1000] bytes), although newer pentium
49 * processors can support a 4MB pagesize as well.
50 *
51 * the first level table (segment table?) is called a "page directory"
52 * and it contains 1024 page directory entries (PDEs). each PDE is
53 * 4 bytes (an int), so a PD fits in a single 4K page. this page is
54 * the page directory page (PDP). each PDE in a PDP maps 4MB of space
55 * (1024 * 4MB = 4GB). a PDE contains the physical address of the
56 * second level table: the page table. or, if 4MB pages are being used,
57 * then the PDE contains the PA of the 4MB page being mapped.
58 *
59 * a page table consists of 1024 page table entries (PTEs). each PTE is
60 * 4 bytes (an int), so a page table also fits in a single 4K page. a
61 * 4K page being used as a page table is called a page table page (PTP).
62 * each PTE in a PTP maps one 4K page (1024 * 4K = 4MB). a PTE contains
63 * the physical address of the page it maps and some flag bits (described
64 * below).
65 *
66 * the processor has a special register, "cr3", which points to the
67 * the PDP which is currently controlling the mappings of the virtual
68 * address space.
69 *
70 * the following picture shows the translation process for a 4K page:
71 *
72 * %cr3 register [PA of PDP]
73 * |
74 * |
75 * | bits <31-22> of VA bits <21-12> of VA bits <11-0>
76 * | index the PDP (0 - 1023) index the PTP are the page offset
77 * | | | |
78 * | v | |
79 * +--->+----------+ | |
80 * | PD Page | PA of v |
81 * | |---PTP-------->+------------+ |
82 * | 1024 PDE | | page table |--PTE--+ |
83 * | entries | | (aka PTP) | | |
84 * +----------+ | 1024 PTE | | |
85 * | entries | | |
86 * +------------+ | |
87 * | |
88 * bits <31-12> bits <11-0>
89 * p h y s i c a l a d d r
90 *
91 * the i386 caches PTEs in a TLB. it is important to flush out old
92 * TLB mappings when making a change to a mappings. writing to the
93 * %cr3 will flush the entire TLB. newer processors also have an
94 * instruction that will invalidate the mapping of a single page (which
95 * is useful if you are changing a single mappings because it preserves
96 * all the cached TLB entries).
97 *
98 * as shows, bits 31-12 of the PTE contain PA of the page being mapped.
99 * the rest of the PTE is defined as follows:
100 * bit# name use
101 * 11 n/a available for OS use, hardware ignores it
102 * 10 n/a available for OS use, hardware ignores it
103 * 9 n/a available for OS use, hardware ignores it
104 * 8 G global bit (see discussion below)
105 * 7 PS page size [for PDEs] (0=4k, 1=4M <if supported>)
106 * 6 D dirty (modified) page
107 * 5 A accessed (referenced) page
108 * 4 PCD cache disable
109 * 3 PWT prevent write through (cache)
110 * 2 U/S user/supervisor bit (0=supervisor only, 1=both u&s)
111 * 1 R/W read/write bit (0=read only, 1=read-write)
112 * 0 P present (valid)
113 *
114 * notes:
115 * - on the i386 the R/W bit is ignored if processor is in supervisor
116 * state (bug!)
117 * - PS is only supported on newer processors
118 * - PTEs with the G bit are global in the sense that they are not
119 * flushed from the TLB when %cr3 is written (to flush, use the
120 * "flush single page" instruction). this is only supported on
121 * newer processors. this bit can be used to keep the kernel's
122 * TLB entries around while context switching. since the kernel
123 * is mapped into all processes at the same place it does not make
124 * sense to flush these entries when switching from one process'
125 * pmap to another.
126 */
127
128 #if defined(_KERNEL) && !defined(_LOCORE)
129
130 /*
131 * here we define the data types for PDEs and PTEs
132 */
133
134 typedef u_int32_t pd_entry_t; /* PDE */
135 typedef u_int32_t pt_entry_t; /* PTE */
136
137 #endif
138
139 /*
140 * now we define various for playing with virtual addresses
141 */
142
143 #define PDSHIFT 22 /* offset of PD index in VA */
144 #define NBPD (1 << PDSHIFT) /* # bytes mapped by PD (4MB) */
145 #define PDOFSET (NBPD-1) /* mask for non-PD part of VA */
146 #if 0 /* not used? */
147 #define NPTEPD (NBPD / NBPG) /* # of PTEs in a PD */
148 #else
149 #define PTES_PER_PTP (NBPD / NBPG) /* # of PTEs in a PTP */
150 #endif
151 #define PD_MASK 0xffc00000 /* page directory address bits */
152 #define PT_MASK 0x003ff000 /* page table address bits */
153
154 /*
155 * here we define the bits of the PDE/PTE, as described above:
156 *
157 * XXXCDC: need to rename these (PG_u == ugly).
158 */
159
160 #define PG_V 0x00000001 /* valid entry */
161 #define PG_RO 0x00000000 /* read-only page */
162 #define PG_RW 0x00000002 /* read-write page */
163 #define PG_u 0x00000004 /* user accessible page */
164 #define PG_PROT 0x00000006 /* all protection bits */
165 #define PG_N 0x00000018 /* non-cacheable */
166 #define PG_U 0x00000020 /* has been used */
167 #define PG_M 0x00000040 /* has been modified */
168 #define PG_PS 0x00000080 /* 4MB page size */
169 #define PG_G 0x00000100 /* global, don't TLB flush */
170 #define PG_AVAIL1 0x00000200 /* ignored by hardware */
171 #define PG_AVAIL2 0x00000400 /* ignored by hardware */
172 #define PG_AVAIL3 0x00000800 /* ignored by hardware */
173 #define PG_FRAME 0xfffff000 /* page frame mask */
174
175 /*
176 * various short-hand protection codes
177 */
178
179 #define PG_KR 0x00000000 /* kernel read-only */
180 #define PG_KW 0x00000002 /* kernel read-write */
181
182 /*
183 * page protection exception bits
184 */
185
186 #define PGEX_P 0x01 /* protection violation (vs. no mapping) */
187 #define PGEX_W 0x02 /* exception during a write cycle */
188 #define PGEX_U 0x04 /* exception while in user mode (upl) */
189
190 #endif /* _I386_PTE_H_ */
191