pte.h revision 1.1.2.2 1 1.1.2.2 bouyer /* $NetBSD: pte.h,v 1.1.2.2 2001/04/21 17:54:53 bouyer Exp $ */
2 1.1.2.2 bouyer
3 1.1.2.2 bouyer /*-
4 1.1.2.2 bouyer * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1.2.2 bouyer * All rights reserved.
6 1.1.2.2 bouyer *
7 1.1.2.2 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 bouyer * modification, are permitted provided that the following conditions
9 1.1.2.2 bouyer * are met:
10 1.1.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 bouyer * documentation and/or other materials provided with the distribution.
15 1.1.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
16 1.1.2.2 bouyer * must display the following acknowledgement:
17 1.1.2.2 bouyer * This product includes software developed by the NetBSD
18 1.1.2.2 bouyer * Foundation, Inc. and its contributors.
19 1.1.2.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
20 1.1.2.2 bouyer * contributors may be used to endorse or promote products derived
21 1.1.2.2 bouyer * from this software without specific prior written permission.
22 1.1.2.2 bouyer *
23 1.1.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
34 1.1.2.2 bouyer */
35 1.1.2.2 bouyer
36 1.1.2.2 bouyer #ifndef _MACHINE_PTE_H
37 1.1.2.2 bouyer #define _MACHINE_PTE_H
38 1.1.2.2 bouyer
39 1.1.2.2 bouyer #define NCONTEXT 8
40 1.1.2.2 bouyer #define NPMEG 256
41 1.1.2.2 bouyer #define SEGINV (NPMEG-1)
42 1.1.2.2 bouyer #define NPAGSEG 16
43 1.1.2.2 bouyer #define NSEGMAP 512
44 1.1.2.2 bouyer
45 1.1.2.2 bouyer /*
46 1.1.2.2 bouyer * In our zeal to use the sun3 pmap with as few changes as possible,
47 1.1.2.2 bouyer * we pretend that sun2 page table entries work more like their sun3
48 1.1.2.2 bouyer * counterparts. Namely, we pretend that they simply have PG_WRITE
49 1.1.2.2 bouyer * and PG_SYSTEM bits, and we use get_pte and set_pte to translate
50 1.1.2.2 bouyer * entries between the two styles.
51 1.1.2.2 bouyer *
52 1.1.2.2 bouyer * All known valid protections in a real sun2 PTE are given in
53 1.1.2.2 bouyer * (disabled) defines below, and are displayed as bitmaps here:
54 1.1.2.2 bouyer *
55 1.1.2.2 bouyer * 3 2 2 2 2
56 1.1.2.2 bouyer * 0 9 8 7 6 meaning
57 1.1.2.2 bouyer * -------------------
58 1.1.2.2 bouyer * 1 1 1 0 0 PG_KW => a read/write kernel-only page.
59 1.1.2.2 bouyer * 1 0 1 0 0 PG_KR => a read-only kernel-only page.
60 1.1.2.2 bouyer * 1 1 1 1 1 PG_UW => a read/write kernel/user page.
61 1.1.2.2 bouyer * 1 0 1 1 0 PG_URKR => a read-only kernel/user page.
62 1.1.2.2 bouyer *
63 1.1.2.2 bouyer * The sun3 PTE protections we want to emulate are:
64 1.1.2.2 bouyer *
65 1.1.2.2 bouyer * PG_SYSTEM | PG_WRITE => a read/write kernel-only page.
66 1.1.2.2 bouyer * PG_SYSTEM => a read-only kernel-only page.
67 1.1.2.2 bouyer * PG_WRITE => a read/write kernel/user page.
68 1.1.2.2 bouyer * => a read-only kernel/user page.
69 1.1.2.2 bouyer *
70 1.1.2.2 bouyer * We want to assign values to PG_SYSTEM and PG_WRITE, and
71 1.1.2.2 bouyer * craft get_pte and set_pte to do a translation from and to the real
72 1.1.2.2 bouyer * hardware protections.
73 1.1.2.2 bouyer *
74 1.1.2.2 bouyer * We begin by noting that bits 30 and 28 are set in all known valid
75 1.1.2.2 bouyer * sun2 protections. Since we assume that the kernel can always read
76 1.1.2.2 bouyer * all pages in the system, we might as well call one of them the
77 1.1.2.2 bouyer * "kernel readable" bit, and say that the other is just always on.
78 1.1.2.2 bouyer * We deem bit 30 the "kernel readable" bit. There is some evidence
79 1.1.2.2 bouyer * that bit 28 may mean "not a device" (the PROM makes PTEs for its
80 1.1.2.2 bouyer * device mappings with bit 28 clear), but I'm not sure enough about
81 1.1.2.2 bouyer * this to do anything about it. So, set_pte will always set these
82 1.1.2.2 bouyer * bits when it loads a valid PTE, and get_pte will always clear them
83 1.1.2.2 bouyer * when it unloads a valid PTE.
84 1.1.2.2 bouyer *
85 1.1.2.2 bouyer * Bit 25, which SunOS calles the "fill on demand" bit, also needs
86 1.1.2.2 bouyer * to be set on all valid PTEs. Dunno any more about this bit.
87 1.1.2.2 bouyer *
88 1.1.2.2 bouyer * Next, we see that bit 27 is set for all pages the user can access,
89 1.1.2.2 bouyer * and clear otherwise. This bit has the opposite meaning of the sun3
90 1.1.2.2 bouyer * PG_SYSTEM bit, but that's OK - we will just define PG_SYSTEM to be
91 1.1.2.2 bouyer * bit 27, and set_pte and get_pte will invert it when loading or
92 1.1.2.2 bouyer * unloading a valid PTE.
93 1.1.2.2 bouyer *
94 1.1.2.2 bouyer * Bit 29 is set for all pages the kernel can write to. We define
95 1.1.2.2 bouyer * PG_WRITE to be bit 29. No inverting is done.
96 1.1.2.2 bouyer *
97 1.1.2.2 bouyer * That leaves us to take care of bit 26. This bit, and bit 27, need
98 1.1.2.2 bouyer * to be set for all pages the user can write to. On the sun3, all
99 1.1.2.2 bouyer * user-accessible pages that the kernel can write to, the user can
100 1.1.2.2 bouyer * also write to. We can use this fact to make set_pte set bit 26 iff
101 1.1.2.2 bouyer * the kernel can write to the page (PG_WRITE is set), and the user
102 1.1.2.2 bouyer * can also access the page (bit 27 is set, i.e., PG_SYSTEM was clear
103 1.1.2.2 bouyer * before set_pte inverted it).
104 1.1.2.2 bouyer *
105 1.1.2.2 bouyer * This is what makes set_pte tricky. It begins by clearing bit 26
106 1.1.2.2 bouyer * (this is paranoia, if all is working well, this bit should never be
107 1.1.2.2 bouyer * set in our pseudo-sun3 PTEs). It then flips PG_SYSTEM to become
108 1.1.2.2 bouyer * the user-accessible bit. Lastly, as the tricky part, it sets bits
109 1.1.2.2 bouyer * 30 and 28, *and* sets bit 26 by shifting the expression (pte &
110 1.1.2.2 bouyer * PG_WRITE) right by two to move the resulting "single bit" into the
111 1.1.2.2 bouyer * bit 27 position, ANDing that with bit 27 in the PTE (the
112 1.1.2.2 bouyer * user-accessible bit), shifting that right once more to line up with
113 1.1.2.2 bouyer * the target bit 26 in the PTE, and ORing it in. This will result in
114 1.1.2.2 bouyer * bit 26 being set if the pseudo-sun3 protection was simply PG_WRITE.
115 1.1.2.2 bouyer *
116 1.1.2.2 bouyer * This could be expressed with if .. else.. logic, but the bit
117 1.1.2.2 bouyer * shifts should compile into something that needs no branching.
118 1.1.2.2 bouyer *
119 1.1.2.2 bouyer * get_pte's job is easier. All it has to do is clear the always-set
120 1.1.2.2 bouyer * bits 30, 28, and 25, *and* clear bit 26, and flip PG_SYSTEM. It can
121 1.1.2.2 bouyer * clear bit 26 because the value that was there can always be derived
122 1.1.2.2 bouyer * from the resulting pseudo-sun3 PG_SYSTEM and PG_WRITE combination.
123 1.1.2.2 bouyer *
124 1.1.2.2 bouyer * And that's how we reuse the sun3 pmap.
125 1.1.2.2 bouyer */
126 1.1.2.2 bouyer #define PG_VALID 0x80000000
127 1.1.2.2 bouyer #define PG_WRITE 0x20000000
128 1.1.2.2 bouyer #define PG_NC 0x00000000
129 1.1.2.2 bouyer #define PG_SYSTEM 0x08000000
130 1.1.2.2 bouyer #if 0
131 1.1.2.2 bouyer #define PG_KW 0x70000000
132 1.1.2.2 bouyer #define PG_KR 0x50000000
133 1.1.2.2 bouyer #define PG_UW 0x7C000000
134 1.1.2.2 bouyer #define PG_URKR 0x58000000
135 1.1.2.2 bouyer #endif
136 1.1.2.2 bouyer #define PG_TYPE 0x00C00000
137 1.1.2.2 bouyer #define PG_REF 0x00200000
138 1.1.2.2 bouyer #define PG_MOD 0x00100000
139 1.1.2.2 bouyer
140 1.1.2.2 bouyer #define PG_SPECIAL (PG_VALID|PG_WRITE|PG_SYSTEM|PG_NC|PG_REF|PG_MOD)
141 1.1.2.2 bouyer #define PG_PERM (PG_VALID|PG_WRITE|PG_SYSTEM|PG_NC)
142 1.1.2.2 bouyer #define PG_MODREF (PG_REF|PG_MOD)
143 1.1.2.2 bouyer #define PG_FRAME 0x00000FFF
144 1.1.2.2 bouyer
145 1.1.2.2 bouyer #define PG_MOD_SHIFT 20
146 1.1.2.2 bouyer
147 1.1.2.2 bouyer #define OBMEM 0
148 1.1.2.2 bouyer #define OBIO 1
149 1.1.2.2 bouyer #define MBMEM 2 /* on the 2/120, VME_D16 on the 2/50 */
150 1.1.2.2 bouyer #define VME_D16 2
151 1.1.2.2 bouyer #define MBIO 3 /* on the 2/120, ??? on the 2/50 */
152 1.1.2.2 bouyer #define PG_TYPE_SHIFT 22
153 1.1.2.2 bouyer
154 1.1.2.2 bouyer #define PG_INVAL 0x0
155 1.1.2.2 bouyer
156 1.1.2.2 bouyer #define MAKE_PGTYPE(x) ((x) << PG_TYPE_SHIFT)
157 1.1.2.2 bouyer #define PG_PFNUM(pte) (pte & PG_FRAME)
158 1.1.2.2 bouyer #define PG_PA(pte) (PG_PFNUM(pte) << PGSHIFT)
159 1.1.2.2 bouyer
160 1.1.2.2 bouyer #define PGT_MASK MAKE_PGTYPE(3)
161 1.1.2.2 bouyer #define PGT_OBMEM MAKE_PGTYPE(OBMEM) /* onboard memory */
162 1.1.2.2 bouyer #define PGT_OBIO MAKE_PGTYPE(OBIO) /* onboard I/O */
163 1.1.2.2 bouyer #define PGT_MBMEM MAKE_PGTYPE(MBMEM) /* Multibus memory on the 2/120, VME_D16 on the 2/50 */
164 1.1.2.2 bouyer #define PGT_VME_D16 MAKE_PGTYPE(VME_D16) /* VMEbus 16-bit data */
165 1.1.2.2 bouyer #define PGT_MBIO MAKE_PGTYPE(MBIO) /* Multibus I/O on the 2/120, ??? on the 2/50 */
166 1.1.2.2 bouyer
167 1.1.2.2 bouyer #define VA_SEGNUM(x) ((u_int)(x) >> SEGSHIFT)
168 1.1.2.2 bouyer
169 1.1.2.2 bouyer #define VA_PTE_NUM_SHIFT PGSHIFT
170 1.1.2.2 bouyer #define VA_PTE_NUM_MASK (((1 << SEGSHIFT) - 1) ^ ((1 << PGSHIFT) - 1))
171 1.1.2.2 bouyer #define VA_PTE_NUM(va) ((va & VA_PTE_NUM_MASK) >> VA_PTE_NUM_SHIFT)
172 1.1.2.2 bouyer
173 1.1.2.2 bouyer #define PA_PGNUM(pa) ((unsigned)pa >> PGSHIFT)
174 1.1.2.2 bouyer
175 1.1.2.2 bouyer #if defined(_KERNEL) || defined(_STANDALONE)
176 1.1.2.2 bouyer u_int get_pte __P((vm_offset_t va));
177 1.1.2.2 bouyer void set_pte __P((vm_offset_t va, u_int pte));
178 1.1.2.2 bouyer #endif /* _KERNEL */
179 1.1.2.2 bouyer
180 1.1.2.2 bouyer #endif /* _MACHINE_PTE_H */
181