pte.h revision 1.22 1 /* $NetBSD: pte.h,v 1.22 2015/06/27 21:44:06 matt Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright 1996 The Board of Trustees of The Leland Stanford
35 * Junior University. All Rights Reserved.
36 *
37 * Permission to use, copy, modify, and distribute this
38 * software and its documentation for any purpose and without
39 * fee is hereby granted, provided that the above copyright
40 * notice appear in all copies. Stanford University
41 * makes no representations about the suitability of this
42 * software for any purpose. It is provided "as is" without
43 * express or implied warranty.
44 */
45
46 #ifndef __MIPS_PTE_H__
47 #define __MIPS_PTE_H__
48
49 #include <mips/mips1_pte.h>
50 #include <mips/mips3_pte.h>
51
52 #define PG_ASID 0x000000ff /* Address space ID */
53
54 #ifndef _LOCORE
55 #include <mips/cpu.h>
56
57 typedef union pt_entry {
58 uint32_t pt_entry; /* for copying, etc. */
59 #if 0
60 struct mips1_pte pt_mips1_pte; /* for getting to bits by name */
61 struct mips3_pte pt_mips3_pte;
62 #endif
63 } pt_entry_t;
64
65 /*
66 * Macros/inline functions to hide PTE format differences.
67 */
68
69 #define mips_pg_nv_bit() (MIPS1_PG_NV) /* same on mips1 and mips3 */
70
71
72 bool pmap_is_page_ro_p(struct pmap *pmap, vaddr_t, uint32_t);
73
74
75 /* MIPS1-only */
76 #if defined(MIPS1) && !defined(MIPS3_PLUS)
77 #define mips_pg_v(entry) ((entry) & MIPS1_PG_V)
78 #define mips_pg_wired(entry) ((entry) & MIPS1_PG_WIRED)
79
80 #define mips_pg_m_bit() (MIPS1_PG_D)
81 #define mips_pg_rw_bit() (MIPS1_PG_RW) /* no RW bits for mips1 */
82 #define mips_pg_ro_bit() (MIPS1_PG_RO)
83 #define mips_pg_ropage_bit() (MIPS1_PG_RO) /* XXX not MIPS1_PG_ROPAGE? */
84 #define mips_pg_rwpage_bit() (MIPS1_PG_RWPAGE)
85 #define mips_pg_rwncpage_bit() (MIPS1_PG_RWNCPAGE)
86 #define mips_pg_cwpage_bit() (MIPS1_PG_CWPAGE)
87 #define mips_pg_cwncpage_bit() (MIPS1_PG_CWNCPAGE)
88 #define mips_pg_global_bit() (MIPS1_PG_G)
89 #define mips_pg_wired_bit() (MIPS1_PG_WIRED)
90
91 #define PTE_TO_PADDR(pte) MIPS1_PTE_TO_PADDR((pte))
92 #define PAGE_IS_RDONLY(pte, va) MIPS1_PAGE_IS_RDONLY((pte), (va))
93
94 #define mips_tlbpfn_to_paddr(x) mips1_tlbpfn_to_paddr((vaddr_t)(x))
95 #define mips_paddr_to_tlbpfn(x) mips1_paddr_to_tlbpfn((x))
96 #endif /* mips1 */
97
98
99 /* MIPS3 (or greater) only */
100 #if !defined(MIPS1) && defined(MIPS3_PLUS)
101 #define mips_pg_v(entry) ((entry) & MIPS3_PG_V)
102 #define mips_pg_wired(entry) ((entry) & MIPS3_PG_WIRED)
103
104 #define mips_pg_m_bit() (MIPS3_PG_D)
105 #define mips_pg_rw_bit() (MIPS3_PG_D)
106 #define mips_pg_ro_bit() (MIPS3_PG_RO)
107 #define mips_pg_ropage_bit() (MIPS3_PG_ROPAGE)
108 #define mips_pg_rwpage_bit() (MIPS3_PG_RWPAGE)
109 #define mips_pg_rwncpage_bit() (MIPS3_PG_RWNCPAGE)
110 #define mips_pg_cwpage_bit() (MIPS3_PG_CWPAGE)
111 #define mips_pg_cwncpage_bit() (MIPS3_PG_CWNCPAGE)
112 #define mips_pg_global_bit() (MIPS3_PG_G)
113 #define mips_pg_wired_bit() (MIPS3_PG_WIRED)
114
115 #define PTE_TO_PADDR(pte) MIPS3_PTE_TO_PADDR((pte))
116 #define PAGE_IS_RDONLY(pte, va) MIPS3_PAGE_IS_RDONLY((pte), (va))
117
118 #define mips_tlbpfn_to_paddr(x) mips3_tlbpfn_to_paddr((vaddr_t)(x))
119 #define mips_paddr_to_tlbpfn(x) mips3_paddr_to_tlbpfn((x))
120 #endif /* mips3 */
121
122 /* MIPS1 and MIPS3 (or greater) */
123 #if defined(MIPS1) && defined(MIPS3_PLUS)
124
125 static __inline bool
126 mips_pg_v(uint32_t entry),
127 mips_pg_wired(uint32_t entry),
128 PAGE_IS_RDONLY(uint32_t pte, vaddr_t va);
129
130 static __inline uint32_t
131 mips_pg_wired_bit(void) __pure,
132 mips_pg_m_bit(void) __pure,
133 mips_pg_ro_bit(void) __pure,
134 mips_pg_rw_bit(void) __pure,
135 mips_pg_ropage_bit(void) __pure,
136 mips_pg_cwpage_bit(void) __pure,
137 mips_pg_rwpage_bit(void) __pure,
138 mips_pg_global_bit(void) __pure;
139 static __inline paddr_t PTE_TO_PADDR(uint32_t pte) __pure;
140 static __inline bool PAGE_IS_RDONLY(uint32_t pte, vaddr_t va) __pure;
141
142 static __inline paddr_t mips_tlbpfn_to_paddr(uint32_t pfn) __pure;
143 static __inline uint32_t mips_paddr_to_tlbpfn(paddr_t pa) __pure;
144
145
146 static __inline bool
147 mips_pg_v(uint32_t entry)
148 {
149 if (MIPS_HAS_R4K_MMU)
150 return (entry & MIPS3_PG_V) != 0;
151 return (entry & MIPS1_PG_V) != 0;
152 }
153
154 static __inline bool
155 mips_pg_wired(uint32_t entry)
156 {
157 if (MIPS_HAS_R4K_MMU)
158 return (entry & MIPS3_PG_WIRED) != 0;
159 return (entry & MIPS1_PG_WIRED) != 0;
160 }
161
162 static __inline uint32_t
163 mips_pg_m_bit(void)
164 {
165 if (MIPS_HAS_R4K_MMU)
166 return (MIPS3_PG_D);
167 return (MIPS1_PG_D);
168 }
169
170 static __inline unsigned int
171 mips_pg_ro_bit(void)
172 {
173 if (MIPS_HAS_R4K_MMU)
174 return (MIPS3_PG_RO);
175 return (MIPS1_PG_RO);
176 }
177
178 static __inline unsigned int
179 mips_pg_rw_bit(void)
180 {
181 if (MIPS_HAS_R4K_MMU)
182 return (MIPS3_PG_D);
183 return (MIPS1_PG_RW);
184 }
185
186 static __inline unsigned int
187 mips_pg_ropage_bit(void)
188 {
189 if (MIPS_HAS_R4K_MMU)
190 return (MIPS3_PG_ROPAGE);
191 return (MIPS1_PG_RO);
192 }
193
194 static __inline unsigned int
195 mips_pg_rwpage_bit(void)
196 {
197 if (MIPS_HAS_R4K_MMU)
198 return (MIPS3_PG_RWPAGE);
199 return (MIPS1_PG_RWPAGE);
200 }
201
202 static __inline unsigned int
203 mips_pg_cwpage_bit(void)
204 {
205 if (MIPS_HAS_R4K_MMU)
206 return (MIPS3_PG_CWPAGE);
207 return (MIPS1_PG_CWPAGE);
208 }
209
210
211 static __inline unsigned int
212 mips_pg_global_bit(void)
213 {
214 if (MIPS_HAS_R4K_MMU)
215 return (MIPS3_PG_G);
216 return (MIPS1_PG_G);
217 }
218
219 static __inline unsigned int
220 mips_pg_wired_bit(void)
221 {
222 if (MIPS_HAS_R4K_MMU)
223 return (MIPS3_PG_WIRED);
224 return (MIPS1_PG_WIRED);
225 }
226
227 static __inline paddr_t
228 PTE_TO_PADDR(uint32_t pte)
229 {
230 if (MIPS_HAS_R4K_MMU)
231 return (MIPS3_PTE_TO_PADDR(pte));
232 return (MIPS1_PTE_TO_PADDR(pte));
233 }
234
235 static __inline bool
236 PAGE_IS_RDONLY(uint32_t pte, vaddr_t va)
237 {
238 if (MIPS_HAS_R4K_MMU)
239 return (MIPS3_PAGE_IS_RDONLY(pte, va));
240 return (MIPS1_PAGE_IS_RDONLY(pte, va));
241 }
242
243 static __inline paddr_t
244 mips_tlbpfn_to_paddr(uint32_t pfn)
245 {
246 if (MIPS_HAS_R4K_MMU)
247 return (mips3_tlbpfn_to_paddr(pfn));
248 return (mips1_tlbpfn_to_paddr(pfn));
249 }
250
251 static __inline uint32_t
252 mips_paddr_to_tlbpfn(paddr_t pa)
253 {
254 if (MIPS_HAS_R4K_MMU)
255 return (mips3_paddr_to_tlbpfn(pa));
256 return (mips1_paddr_to_tlbpfn(pa));
257 }
258 #endif
259
260 #endif /* ! _LOCORE */
261
262 #if defined(_KERNEL) && !defined(_LOCORE)
263 /*
264 * Kernel virtual address to page table entry and visa versa.
265 */
266 #define kvtopte(va) \
267 (Sysmap + (((vaddr_t)(va) - VM_MIN_KERNEL_ADDRESS) >> PGSHIFT))
268 #define ptetokv(pte) \
269 ((((pt_entry_t *)(pte) - Sysmap) << PGSHIFT) + VM_MIN_KERNEL_ADDRESS)
270
271 extern pt_entry_t *Sysmap; /* kernel pte table */
272 extern u_int Sysmapsize; /* number of pte's in Sysmap */
273
274 static inline bool
275 pte_zero_p(pt_entry_t pte)
276 {
277 return pte.pt_entry == 0;
278 }
279
280 #define PRIxPTE PRIx32
281 static inline uint32_t
282 pte_value(pt_entry_t pte)
283 {
284 return pte.pt_entry;
285 }
286
287 #endif /* defined(_KERNEL) && !defined(_LOCORE) */
288 #endif /* __MIPS_PTE_H__ */
289