pte.h revision 1.8 1 1.8 pk /* $NetBSD: pte.h,v 1.8 1995/04/13 13:48:46 pk Exp $ */
2 1.7 deraadt
3 1.1 deraadt /*
4 1.1 deraadt * Copyright (c) 1992, 1993
5 1.1 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 deraadt *
7 1.1 deraadt * This software was developed by the Computer Systems Engineering group
8 1.1 deraadt * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 deraadt * contributed to Berkeley.
10 1.1 deraadt *
11 1.1 deraadt * All advertising materials mentioning features or use of this software
12 1.1 deraadt * must display the following acknowledgement:
13 1.1 deraadt * This product includes software developed by the University of
14 1.1 deraadt * California, Lawrence Berkeley Laboratory.
15 1.1 deraadt *
16 1.1 deraadt * Redistribution and use in source and binary forms, with or without
17 1.1 deraadt * modification, are permitted provided that the following conditions
18 1.1 deraadt * are met:
19 1.1 deraadt * 1. Redistributions of source code must retain the above copyright
20 1.1 deraadt * notice, this list of conditions and the following disclaimer.
21 1.1 deraadt * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 deraadt * notice, this list of conditions and the following disclaimer in the
23 1.1 deraadt * documentation and/or other materials provided with the distribution.
24 1.1 deraadt * 3. All advertising materials mentioning features or use of this software
25 1.1 deraadt * must display the following acknowledgement:
26 1.1 deraadt * This product includes software developed by the University of
27 1.1 deraadt * California, Berkeley and its contributors.
28 1.1 deraadt * 4. Neither the name of the University nor the names of its contributors
29 1.1 deraadt * may be used to endorse or promote products derived from this software
30 1.1 deraadt * without specific prior written permission.
31 1.1 deraadt *
32 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 1.1 deraadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 1.1 deraadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 1.1 deraadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 1.1 deraadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1 deraadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1 deraadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1 deraadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 1.1 deraadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 1.1 deraadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 1.1 deraadt * SUCH DAMAGE.
43 1.1 deraadt *
44 1.1 deraadt * @(#)pte.h 8.1 (Berkeley) 6/11/93
45 1.1 deraadt */
46 1.1 deraadt
47 1.1 deraadt /*
48 1.1 deraadt * Sun-4 (sort of) and 4c (SparcStation) Page Table Entries
49 1.1 deraadt * (Sun call them `Page Map Entries').
50 1.1 deraadt */
51 1.1 deraadt
52 1.1 deraadt #ifndef LOCORE
53 1.1 deraadt /*
54 1.1 deraadt * Segment maps contain `pmeg' (Page Map Entry Group) numbers.
55 1.1 deraadt * A PMEG is simply an index that names a group of 32 (sun4) or
56 1.1 deraadt * 64 (sun4c) PTEs.
57 1.1 deraadt */
58 1.3 deraadt typedef u_short pmeg_t; /* 9 bits needed per Sun-4 segmap entry */
59 1.8 pk /*
60 1.8 pk * Region maps contain `smeg' (Segment Entry Group) numbers.
61 1.8 pk * An SMEG is simply an index that names a group of 64 PMEGs
62 1.8 pk */
63 1.8 pk typedef u_char smeg_t; /* 6 bits needed per Sun-4 regmap entry */
64 1.1 deraadt #endif
65 1.1 deraadt
66 1.1 deraadt /*
67 1.1 deraadt * Address translation works as follows:
68 1.1 deraadt *
69 1.8 pk * (for sun4c and 2-level sun4)
70 1.1 deraadt * 1. test va<31:29> -- these must be 000 or 111 (or you get a fault)
71 1.1 deraadt * 2. concatenate context_reg<2:0> and va<29:18> to get a 15 bit number;
72 1.8 pk * use this to index the segment maps, yielding a 7 or 9 bit value.
73 1.8 pk * (for 3-level sun4)
74 1.8 pk * 1. concatenate context_reg<3:0> and va<31:24> to get a 8 bit number;
75 1.8 pk * use this to index the region maps, yielding a 10 bit value.
76 1.8 pk * 2. take the value from (1) above and concatenate va<17:12> to
77 1.8 pk * get a `segment map entry' index. This gives a 9 bit value.
78 1.1 deraadt * (for sun4c)
79 1.1 deraadt * 3. take the value from (2) above and concatenate va<17:12> to
80 1.1 deraadt * get a `page map entry' index. This gives a 32-bit PTE.
81 1.1 deraadt * (for sun4)
82 1.8 pk * 3. take the value from (2 or 3) above and concatenate va<17:13> to
83 1.1 deraadt * get a `page map entry' index. This gives a 32-bit PTE.
84 1.1 deraadt *
85 1.1 deraadt * In other words:
86 1.1 deraadt *
87 1.8 pk * struct sun4_3_levelmmu_virtual_addr {
88 1.8 pk * u_int va_reg:8, (virtual region)
89 1.8 pk * va_seg:6, (virtual segment)
90 1.8 pk * va_pg:5, (virtual page within segment)
91 1.8 pk * va_off:13; (offset within page)
92 1.8 pk * };
93 1.1 deraadt * struct sun4_virtual_addr {
94 1.1 deraadt * u_int :2, (required to be the same as bit 29)
95 1.1 deraadt * va_seg:12, (virtual segment)
96 1.1 deraadt * va_pg:5, (virtual page within segment)
97 1.1 deraadt * va_off:13; (offset within page)
98 1.1 deraadt * };
99 1.1 deraadt * struct sun4c_virtual_addr {
100 1.1 deraadt * u_int :2, (required to be the same as bit 29)
101 1.1 deraadt * va_seg:12, (virtual segment)
102 1.1 deraadt * va_pg:6, (virtual page within segment)
103 1.1 deraadt * va_off:12; (offset within page)
104 1.1 deraadt * };
105 1.1 deraadt *
106 1.1 deraadt * Then, given any `va':
107 1.1 deraadt *
108 1.8 pk * extern smeg_t regmap[16][1<<8]; (3-level MMU only)
109 1.1 deraadt * extern pmeg_t segmap[8][1<<12]; ([16][1<<12] for sun4)
110 1.1 deraadt * extern int ptetable[128][1<<6]; ([512][1<<5] for sun4)
111 1.1 deraadt *
112 1.1 deraadt * (the above being in the hardware, accessed as Alternate Address Spaces)
113 1.1 deraadt *
114 1.8 pk * if (mmu_3l)
115 1.8 pk * physreg = regmap[curr_ctx][va.va_reg];
116 1.8 pk * physseg = segmap[physreg][va.va_seg];
117 1.8 pk * else
118 1.8 pk * physseg = segmap[curr_ctx][va.va_seg];
119 1.1 deraadt * pte = ptetable[physseg][va.va_pg];
120 1.1 deraadt * if (!(pte & PG_V)) TRAP();
121 1.1 deraadt * if (writing && !pte.pg_w) TRAP();
122 1.1 deraadt * if (usermode && pte.pg_s) TRAP();
123 1.1 deraadt * if (pte & PG_NC) DO_NOT_USE_CACHE_FOR_THIS_ACCESS();
124 1.1 deraadt * pte |= PG_U; (mark used/accessed)
125 1.1 deraadt * if (writing) pte |= PG_M; (mark modified)
126 1.1 deraadt * ptetable[physseg][va.va_pg] = pte;
127 1.1 deraadt * physadr = ((pte & PG_PFNUM) << PGSHIFT) | va.va_off;
128 1.1 deraadt */
129 1.1 deraadt
130 1.8 pk #if defined(MMU_3L) && !defined(SUN4)
131 1.8 pk #error "configuration error"
132 1.8 pk #endif
133 1.8 pk
134 1.8 pk #if defined(MMU_3L)
135 1.8 pk extern int mmu_3l;
136 1.8 pk #endif
137 1.8 pk
138 1.8 pk #define NBPRG (1 << 24) /* bytes per segment */
139 1.8 pk #define RGSHIFT 24 /* log2(NBPSG) */
140 1.8 pk #define RGOFSET (NBPRG - 1) /* mask for segment offset */
141 1.8 pk #define NSEGRG (NBPRG / NBPSG) /* segments per region */
142 1.8 pk
143 1.1 deraadt #define NBPSG (1 << 18) /* bytes per segment */
144 1.1 deraadt #define SGSHIFT 18 /* log2(NBPSG) */
145 1.1 deraadt #define SGOFSET (NBPSG - 1) /* mask for segment offset */
146 1.1 deraadt
147 1.1 deraadt /* number of PTEs that map one segment (not number that fit in one segment!) */
148 1.1 deraadt #if defined(SUN4) && defined(SUN4C)
149 1.2 deraadt extern int nptesg;
150 1.4 deraadt #define NPTESG nptesg /* (which someone will have to initialize) */
151 1.1 deraadt #else
152 1.1 deraadt #define NPTESG (NBPSG / NBPG)
153 1.1 deraadt #endif
154 1.1 deraadt
155 1.8 pk /* virtual address to virtual region number */
156 1.8 pk #define VA_VREG(va) (((unsigned int)(va) >> RGSHIFT) & 255)
157 1.8 pk
158 1.1 deraadt /* virtual address to virtual segment number */
159 1.8 pk #define VA_VSEG(va) (((unsigned int)(va) >> SGSHIFT) & 63)
160 1.1 deraadt
161 1.1 deraadt /* virtual address to virtual page number, for Sun-4 and Sun-4c */
162 1.1 deraadt #define VA_SUN4_VPG(va) (((int)(va) >> 13) & 31)
163 1.1 deraadt #define VA_SUN4C_VPG(va) (((int)(va) >> 12) & 63)
164 1.1 deraadt
165 1.8 pk /* truncate virtual address to region base */
166 1.8 pk #define VA_ROUNDDOWNTOREG(va) ((int)(va) & ~RGOFSET)
167 1.8 pk
168 1.1 deraadt /* truncate virtual address to segment base */
169 1.1 deraadt #define VA_ROUNDDOWNTOSEG(va) ((int)(va) & ~SGOFSET)
170 1.1 deraadt
171 1.8 pk /* virtual segment to virtual address (must sign extend on holy MMUs!) */
172 1.8 pk #if defined(MMU_3L)
173 1.8 pk #define VRTOVA(vr) (mmu_3l \
174 1.8 pk ? ((int)(vr) << RGSHIFT) \
175 1.8 pk : (((int)(vr) << (RGSHIFT+2)) >> 2))
176 1.8 pk #define VSTOVA(vr,vs) (mmu_3l \
177 1.8 pk ? (((int)vr << RGSHIFT) + ((int)vs << SGSHIFT)) \
178 1.8 pk : ((((int)vr << (RGSHIFT+2)) >> 2) + ((int)vs << SGSHIFT)))
179 1.8 pk #else
180 1.8 pk #define VRTOVA(vr) (((int)vr << (RGSHIFT+2)) >> 2)
181 1.8 pk #define VSTOVA(vr,vs) ((((int)vr << (RGSHIFT+2)) >> 2) + ((int)vs << SGSHIFT))
182 1.8 pk #endif
183 1.1 deraadt
184 1.4 deraadt #if defined(SUN4) && defined(SUN4C)
185 1.4 deraadt #define VA_VPG(va) (cputyp==CPU_SUN4C ? VA_SUN4C_VPG(va) : VA_SUN4_VPG(va))
186 1.4 deraadt #endif
187 1.4 deraadt #if defined(SUN4C) && !defined(SUN4)
188 1.4 deraadt #define VA_VPG(va) VA_SUN4C_VPG(va)
189 1.1 deraadt #endif
190 1.4 deraadt #if !defined(SUN4C) && defined(SUN4)
191 1.4 deraadt #define VA_VPG(va) VA_SUN4_VPG(va)
192 1.1 deraadt #endif
193 1.1 deraadt
194 1.1 deraadt /* there is no `struct pte'; we just use `int' */
195 1.1 deraadt #define PG_V 0x80000000
196 1.1 deraadt #define PG_PROT 0x60000000 /* both protection bits */
197 1.1 deraadt #define PG_W 0x40000000 /* allowed to write */
198 1.1 deraadt #define PG_S 0x20000000 /* supervisor only */
199 1.1 deraadt #define PG_NC 0x10000000 /* non-cacheable */
200 1.1 deraadt #define PG_TYPE 0x0c000000 /* both type bits */
201 1.1 deraadt
202 1.1 deraadt #define PG_OBMEM 0x00000000 /* on board memory */
203 1.1 deraadt #define PG_OBIO 0x04000000 /* on board I/O (incl. Sbus on 4c) */
204 1.1 deraadt #ifdef SUN4
205 1.1 deraadt #define PG_VME16 0x08000000 /* 16-bit-data VME space */
206 1.1 deraadt #define PG_VME32 0x0c000000 /* 32-bit-data VME space */
207 1.1 deraadt #endif
208 1.1 deraadt
209 1.1 deraadt #define PG_U 0x02000000
210 1.1 deraadt #define PG_M 0x01000000
211 1.1 deraadt #define PG_MBZ 0x00f80000 /* unused; must be zero (oh really?) */
212 1.1 deraadt #define PG_PFNUM 0x0007ffff /* n.b.: only 16 bits on sun4c */
213 1.1 deraadt
214 1.1 deraadt #define PG_TNC_SHIFT 26 /* shift to get PG_TYPE + PG_NC */
215 1.1 deraadt #define PG_M_SHIFT 24 /* shift to get PG_M, PG_U */
216 1.1 deraadt
217 1.1 deraadt /*efine PG_NOACC 0 ** XXX */
218 1.1 deraadt #define PG_KR 0x20000000
219 1.1 deraadt #define PG_KW 0x60000000
220 1.1 deraadt #define PG_URKR 0
221 1.1 deraadt #define PG_UW 0x40000000
222 1.1 deraadt
223 1.1 deraadt #ifdef KGDB
224 1.1 deraadt /* but we will define one for gdb anyway */
225 1.1 deraadt struct pte {
226 1.1 deraadt u_int pg_v:1,
227 1.1 deraadt pg_w:1,
228 1.1 deraadt pg_s:1,
229 1.1 deraadt pg_nc:1;
230 1.1 deraadt enum pgtype { pg_obmem, pg_obio, pg_vme16, pg_vme32 } pg_type:2;
231 1.1 deraadt u_int pg_u:1,
232 1.1 deraadt pg_m:1,
233 1.1 deraadt pg_mbz:5,
234 1.1 deraadt pg_pfnum:19;
235 1.1 deraadt };
236 1.1 deraadt #endif
237 1.1 deraadt
238 1.1 deraadt /*
239 1.1 deraadt * These are needed in the register window code
240 1.1 deraadt * to check the validity of (ostensible) user stack PTEs.
241 1.1 deraadt */
242 1.1 deraadt #define PG_VSHIFT 30 /* (va>>vshift)==0 or -1 => valid */
243 1.1 deraadt /* XXX fix this name, it is a va shift not a pte bit shift! */
244 1.1 deraadt
245 1.1 deraadt #define PG_PROTSHIFT 29
246 1.1 deraadt #define PG_PROTUWRITE 6 /* PG_V,PG_W,!PG_S */
247 1.1 deraadt #define PG_PROTUREAD 4 /* PG_V,!PG_W,!PG_S */
248 1.1 deraadt
249 1.1 deraadt /* static __inline int PG_VALID(void *va) {
250 1.1 deraadt register int t = va; t >>= PG_VSHIFT; return (t == 0 || t == -1);
251 1.1 deraadt } */
252