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