pmap.c revision 1.3.4.3 1 1.3.4.3 bouyer /* $NetBSD: pmap.c,v 1.3.4.3 2001/03/27 15:30:18 bouyer Exp $ */
2 1.3.4.2 bouyer
3 1.3.4.2 bouyer /*-
4 1.3.4.2 bouyer * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.3.4.2 bouyer * All rights reserved.
6 1.3.4.2 bouyer *
7 1.3.4.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.3.4.2 bouyer * by Charles M. Hannum.
9 1.3.4.2 bouyer *
10 1.3.4.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.3.4.2 bouyer * modification, are permitted provided that the following conditions
12 1.3.4.2 bouyer * are met:
13 1.3.4.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.3.4.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.3.4.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.4.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.3.4.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.3.4.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.3.4.2 bouyer * must display the following acknowledgement:
20 1.3.4.2 bouyer * This product includes software developed by the NetBSD
21 1.3.4.2 bouyer * Foundation, Inc. and its contributors.
22 1.3.4.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.4.2 bouyer * contributors may be used to endorse or promote products derived
24 1.3.4.2 bouyer * from this software without specific prior written permission.
25 1.3.4.2 bouyer *
26 1.3.4.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.4.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.4.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.4.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.4.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.4.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.4.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.4.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.4.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.4.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.4.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.3.4.2 bouyer */
38 1.3.4.2 bouyer
39 1.3.4.2 bouyer /*
40 1.3.4.2 bouyer * Copyright (c) 1994-1998 Mark Brinicombe.
41 1.3.4.2 bouyer * Copyright (c) 1994 Brini.
42 1.3.4.2 bouyer * All rights reserved.
43 1.3.4.2 bouyer *
44 1.3.4.2 bouyer * This code is derived from software written for Brini by Mark Brinicombe
45 1.3.4.2 bouyer *
46 1.3.4.2 bouyer * Redistribution and use in source and binary forms, with or without
47 1.3.4.2 bouyer * modification, are permitted provided that the following conditions
48 1.3.4.2 bouyer * are met:
49 1.3.4.2 bouyer * 1. Redistributions of source code must retain the above copyright
50 1.3.4.2 bouyer * notice, this list of conditions and the following disclaimer.
51 1.3.4.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
52 1.3.4.2 bouyer * notice, this list of conditions and the following disclaimer in the
53 1.3.4.2 bouyer * documentation and/or other materials provided with the distribution.
54 1.3.4.2 bouyer * 3. All advertising materials mentioning features or use of this software
55 1.3.4.2 bouyer * must display the following acknowledgement:
56 1.3.4.2 bouyer * This product includes software developed by Mark Brinicombe.
57 1.3.4.2 bouyer * 4. The name of the author may not be used to endorse or promote products
58 1.3.4.2 bouyer * derived from this software without specific prior written permission.
59 1.3.4.2 bouyer *
60 1.3.4.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
61 1.3.4.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
62 1.3.4.2 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63 1.3.4.2 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
64 1.3.4.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
65 1.3.4.2 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66 1.3.4.2 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67 1.3.4.2 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 1.3.4.2 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
69 1.3.4.2 bouyer *
70 1.3.4.2 bouyer * RiscBSD kernel project
71 1.3.4.2 bouyer *
72 1.3.4.2 bouyer * pmap.c
73 1.3.4.2 bouyer *
74 1.3.4.2 bouyer * Machine dependant vm stuff
75 1.3.4.2 bouyer *
76 1.3.4.2 bouyer * Created : 20/09/94
77 1.3.4.2 bouyer */
78 1.3.4.2 bouyer
79 1.3.4.2 bouyer /*
80 1.3.4.2 bouyer * Performance improvements, UVM changes, overhauls and part-rewrites
81 1.3.4.2 bouyer * were contributed by Neil A. Carson <neil (at) causality.com>.
82 1.3.4.2 bouyer */
83 1.3.4.2 bouyer
84 1.3.4.2 bouyer /*
85 1.3.4.2 bouyer * The dram block info is currently referenced from the bootconfig.
86 1.3.4.2 bouyer * This should be placed in a separate structure.
87 1.3.4.2 bouyer */
88 1.3.4.2 bouyer
89 1.3.4.2 bouyer /*
90 1.3.4.2 bouyer * Special compilation symbols
91 1.3.4.2 bouyer * PMAP_DEBUG - Build in pmap_debug_level code
92 1.3.4.2 bouyer */
93 1.3.4.2 bouyer
94 1.3.4.2 bouyer /* Include header files */
95 1.3.4.2 bouyer
96 1.3.4.2 bouyer #include "opt_pmap_debug.h"
97 1.3.4.2 bouyer #include "opt_ddb.h"
98 1.3.4.2 bouyer
99 1.3.4.2 bouyer #include <sys/types.h>
100 1.3.4.2 bouyer #include <sys/param.h>
101 1.3.4.2 bouyer #include <sys/kernel.h>
102 1.3.4.2 bouyer #include <sys/systm.h>
103 1.3.4.2 bouyer #include <sys/proc.h>
104 1.3.4.2 bouyer #include <sys/malloc.h>
105 1.3.4.2 bouyer #include <sys/user.h>
106 1.3.4.2 bouyer
107 1.3.4.2 bouyer #include <uvm/uvm.h>
108 1.3.4.2 bouyer
109 1.3.4.2 bouyer #include <machine/bootconfig.h>
110 1.3.4.2 bouyer #include <machine/bus.h>
111 1.3.4.2 bouyer #include <machine/pmap.h>
112 1.3.4.2 bouyer #include <machine/pcb.h>
113 1.3.4.2 bouyer #include <machine/param.h>
114 1.3.4.2 bouyer #include <machine/katelib.h>
115 1.3.4.2 bouyer
116 1.3.4.2 bouyer #ifdef PMAP_DEBUG
117 1.3.4.2 bouyer #define PDEBUG(_lev_,_stat_) \
118 1.3.4.2 bouyer if (pmap_debug_level >= (_lev_)) \
119 1.3.4.2 bouyer ((_stat_))
120 1.3.4.2 bouyer int pmap_debug_level = -2;
121 1.3.4.2 bouyer #else /* PMAP_DEBUG */
122 1.3.4.2 bouyer #define PDEBUG(_lev_,_stat_) /* Nothing */
123 1.3.4.2 bouyer #endif /* PMAP_DEBUG */
124 1.3.4.2 bouyer
125 1.3.4.2 bouyer struct pmap kernel_pmap_store;
126 1.3.4.2 bouyer pmap_t kernel_pmap;
127 1.3.4.2 bouyer
128 1.3.4.2 bouyer pagehook_t page_hook0;
129 1.3.4.2 bouyer pagehook_t page_hook1;
130 1.3.4.2 bouyer char *memhook;
131 1.3.4.2 bouyer pt_entry_t msgbufpte;
132 1.3.4.2 bouyer extern caddr_t msgbufaddr;
133 1.3.4.2 bouyer
134 1.3.4.2 bouyer #ifdef DIAGNOSTIC
135 1.3.4.2 bouyer boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
136 1.3.4.2 bouyer #endif
137 1.3.4.2 bouyer
138 1.3.4.2 bouyer TAILQ_HEAD(pv_page_list, pv_page) pv_page_freelist;
139 1.3.4.2 bouyer
140 1.3.4.2 bouyer int pv_nfree = 0;
141 1.3.4.2 bouyer
142 1.3.4.2 bouyer vsize_t npages;
143 1.3.4.2 bouyer
144 1.3.4.2 bouyer extern paddr_t physical_start;
145 1.3.4.2 bouyer extern paddr_t physical_freestart;
146 1.3.4.2 bouyer extern paddr_t physical_end;
147 1.3.4.2 bouyer extern paddr_t physical_freeend;
148 1.3.4.2 bouyer extern unsigned int free_pages;
149 1.3.4.2 bouyer extern int max_processes;
150 1.3.4.2 bouyer
151 1.3.4.2 bouyer vaddr_t virtual_start;
152 1.3.4.2 bouyer vaddr_t virtual_end;
153 1.3.4.2 bouyer
154 1.3.4.2 bouyer vaddr_t avail_start;
155 1.3.4.2 bouyer vaddr_t avail_end;
156 1.3.4.2 bouyer
157 1.3.4.2 bouyer extern pv_addr_t systempage;
158 1.3.4.2 bouyer
159 1.3.4.2 bouyer #define ALLOC_PAGE_HOOK(x, s) \
160 1.3.4.2 bouyer x.va = virtual_start; \
161 1.3.4.2 bouyer x.pte = (pt_entry_t *)pmap_pte(kernel_pmap, virtual_start); \
162 1.3.4.2 bouyer virtual_start += s;
163 1.3.4.2 bouyer
164 1.3.4.2 bouyer /* Variables used by the L1 page table queue code */
165 1.3.4.2 bouyer SIMPLEQ_HEAD(l1pt_queue, l1pt);
166 1.3.4.2 bouyer struct l1pt_queue l1pt_static_queue; /* head of our static l1 queue */
167 1.3.4.2 bouyer int l1pt_static_queue_count; /* items in the static l1 queue */
168 1.3.4.2 bouyer int l1pt_static_create_count; /* static l1 items created */
169 1.3.4.2 bouyer struct l1pt_queue l1pt_queue; /* head of our l1 queue */
170 1.3.4.2 bouyer int l1pt_queue_count; /* items in the l1 queue */
171 1.3.4.2 bouyer int l1pt_create_count; /* stat - L1's create count */
172 1.3.4.2 bouyer int l1pt_reuse_count; /* stat - L1's reused count */
173 1.3.4.2 bouyer
174 1.3.4.2 bouyer /* Local function prototypes (not used outside this file) */
175 1.3.4.2 bouyer pt_entry_t *pmap_pte __P((pmap_t pmap, vaddr_t va));
176 1.3.4.2 bouyer int pmap_page_index __P((paddr_t pa));
177 1.3.4.2 bouyer void map_pagetable __P((vaddr_t pagetable, vaddr_t va,
178 1.3.4.2 bouyer paddr_t pa, unsigned int flags));
179 1.3.4.2 bouyer void pmap_copy_on_write __P((paddr_t pa));
180 1.3.4.2 bouyer void pmap_pinit __P((pmap_t));
181 1.3.4.2 bouyer void pmap_release __P((pmap_t));
182 1.3.4.2 bouyer
183 1.3.4.2 bouyer /* Other function prototypes */
184 1.3.4.2 bouyer extern void bzero_page __P((vaddr_t));
185 1.3.4.2 bouyer extern void bcopy_page __P((vaddr_t, vaddr_t));
186 1.3.4.2 bouyer
187 1.3.4.2 bouyer struct l1pt *pmap_alloc_l1pt __P((void));
188 1.3.4.2 bouyer static __inline void pmap_map_in_l1 __P((pmap_t pmap, vaddr_t va,
189 1.3.4.2 bouyer vaddr_t l2pa));
190 1.3.4.2 bouyer
191 1.3.4.2 bouyer #ifdef MYCROFT_HACK
192 1.3.4.2 bouyer int mycroft_hack = 0;
193 1.3.4.2 bouyer #endif
194 1.3.4.2 bouyer
195 1.3.4.2 bouyer /* Function to set the debug level of the pmap code */
196 1.3.4.2 bouyer
197 1.3.4.2 bouyer #ifdef PMAP_DEBUG
198 1.3.4.2 bouyer void
199 1.3.4.2 bouyer pmap_debug(level)
200 1.3.4.2 bouyer int level;
201 1.3.4.2 bouyer {
202 1.3.4.2 bouyer pmap_debug_level = level;
203 1.3.4.2 bouyer printf("pmap_debug: level=%d\n", pmap_debug_level);
204 1.3.4.2 bouyer }
205 1.3.4.2 bouyer #endif /* PMAP_DEBUG */
206 1.3.4.2 bouyer
207 1.3.4.2 bouyer #include "isadma.h"
208 1.3.4.2 bouyer
209 1.3.4.2 bouyer #if NISADMA > 0
210 1.3.4.2 bouyer /*
211 1.3.4.2 bouyer * Used to protect memory for ISA DMA bounce buffers. If, when loading
212 1.3.4.2 bouyer * pages into the system, memory intersects with any of these ranges,
213 1.3.4.2 bouyer * the intersecting memory will be loaded into a lower-priority free list.
214 1.3.4.2 bouyer */
215 1.3.4.2 bouyer bus_dma_segment_t *pmap_isa_dma_ranges;
216 1.3.4.2 bouyer int pmap_isa_dma_nranges;
217 1.3.4.2 bouyer
218 1.3.4.2 bouyer boolean_t pmap_isa_dma_range_intersect __P((paddr_t, psize_t,
219 1.3.4.2 bouyer paddr_t *, psize_t *));
220 1.3.4.2 bouyer
221 1.3.4.2 bouyer /*
222 1.3.4.2 bouyer * Check if a memory range intersects with an ISA DMA range, and
223 1.3.4.2 bouyer * return the page-rounded intersection if it does. The intersection
224 1.3.4.2 bouyer * will be placed on a lower-priority free list.
225 1.3.4.2 bouyer */
226 1.3.4.2 bouyer boolean_t
227 1.3.4.2 bouyer pmap_isa_dma_range_intersect(pa, size, pap, sizep)
228 1.3.4.2 bouyer paddr_t pa;
229 1.3.4.2 bouyer psize_t size;
230 1.3.4.2 bouyer paddr_t *pap;
231 1.3.4.2 bouyer psize_t *sizep;
232 1.3.4.2 bouyer {
233 1.3.4.2 bouyer bus_dma_segment_t *ds;
234 1.3.4.2 bouyer int i;
235 1.3.4.2 bouyer
236 1.3.4.2 bouyer if (pmap_isa_dma_ranges == NULL)
237 1.3.4.2 bouyer return (FALSE);
238 1.3.4.2 bouyer
239 1.3.4.2 bouyer for (i = 0, ds = pmap_isa_dma_ranges;
240 1.3.4.2 bouyer i < pmap_isa_dma_nranges; i++, ds++) {
241 1.3.4.2 bouyer if (ds->ds_addr <= pa && pa < (ds->ds_addr + ds->ds_len)) {
242 1.3.4.2 bouyer /*
243 1.3.4.2 bouyer * Beginning of region intersects with this range.
244 1.3.4.2 bouyer */
245 1.3.4.2 bouyer *pap = trunc_page(pa);
246 1.3.4.2 bouyer *sizep = round_page(min(pa + size,
247 1.3.4.2 bouyer ds->ds_addr + ds->ds_len) - pa);
248 1.3.4.2 bouyer return (TRUE);
249 1.3.4.2 bouyer }
250 1.3.4.2 bouyer if (pa < ds->ds_addr && ds->ds_addr < (pa + size)) {
251 1.3.4.2 bouyer /*
252 1.3.4.2 bouyer * End of region intersects with this range.
253 1.3.4.2 bouyer */
254 1.3.4.2 bouyer *pap = trunc_page(ds->ds_addr);
255 1.3.4.2 bouyer *sizep = round_page(min((pa + size) - ds->ds_addr,
256 1.3.4.2 bouyer ds->ds_len));
257 1.3.4.2 bouyer return (TRUE);
258 1.3.4.2 bouyer }
259 1.3.4.2 bouyer }
260 1.3.4.2 bouyer
261 1.3.4.2 bouyer /*
262 1.3.4.2 bouyer * No intersection found.
263 1.3.4.2 bouyer */
264 1.3.4.2 bouyer return (FALSE);
265 1.3.4.2 bouyer }
266 1.3.4.2 bouyer #endif /* NISADMA > 0 */
267 1.3.4.2 bouyer
268 1.3.4.2 bouyer /*
269 1.3.4.2 bouyer * Functions for manipluation pv_entry structures. These are used to keep a
270 1.3.4.2 bouyer * record of the mappings of virtual addresses and the associated physical
271 1.3.4.2 bouyer * pages.
272 1.3.4.2 bouyer */
273 1.3.4.2 bouyer
274 1.3.4.2 bouyer /*
275 1.3.4.2 bouyer * Allocate a new pv_entry structure from the freelist. If the list is
276 1.3.4.2 bouyer * empty allocate a new page and fill the freelist.
277 1.3.4.2 bouyer */
278 1.3.4.2 bouyer struct pv_entry *
279 1.3.4.2 bouyer pmap_alloc_pv()
280 1.3.4.2 bouyer {
281 1.3.4.2 bouyer struct pv_page *pvp;
282 1.3.4.2 bouyer struct pv_entry *pv;
283 1.3.4.2 bouyer int i;
284 1.3.4.2 bouyer
285 1.3.4.2 bouyer /*
286 1.3.4.2 bouyer * Do we have any free pv_entry structures left ?
287 1.3.4.2 bouyer * If not allocate a page of them
288 1.3.4.2 bouyer */
289 1.3.4.2 bouyer
290 1.3.4.2 bouyer if (pv_nfree == 0) {
291 1.3.4.2 bouyer /* NOTE: can't lock kernel_map here */
292 1.3.4.2 bouyer MALLOC(pvp, struct pv_page *, NBPG, M_VMPVENT, M_WAITOK);
293 1.3.4.2 bouyer if (pvp == 0)
294 1.3.4.2 bouyer panic("pmap_alloc_pv: kmem_alloc() failed");
295 1.3.4.2 bouyer pvp->pvp_pgi.pgi_freelist = pv = &pvp->pvp_pv[1];
296 1.3.4.2 bouyer for (i = NPVPPG - 2; i; i--, pv++)
297 1.3.4.2 bouyer pv->pv_next = pv + 1;
298 1.3.4.2 bouyer pv->pv_next = 0;
299 1.3.4.2 bouyer pv_nfree += pvp->pvp_pgi.pgi_nfree = NPVPPG - 1;
300 1.3.4.2 bouyer TAILQ_INSERT_HEAD(&pv_page_freelist, pvp, pvp_pgi.pgi_list);
301 1.3.4.2 bouyer pv = &pvp->pvp_pv[0];
302 1.3.4.2 bouyer } else {
303 1.3.4.2 bouyer --pv_nfree;
304 1.3.4.2 bouyer pvp = pv_page_freelist.tqh_first;
305 1.3.4.2 bouyer if (--pvp->pvp_pgi.pgi_nfree == 0) {
306 1.3.4.2 bouyer TAILQ_REMOVE(&pv_page_freelist, pvp, pvp_pgi.pgi_list);
307 1.3.4.2 bouyer }
308 1.3.4.2 bouyer pv = pvp->pvp_pgi.pgi_freelist;
309 1.3.4.2 bouyer #ifdef DIAGNOSTIC
310 1.3.4.2 bouyer if (pv == 0)
311 1.3.4.2 bouyer panic("pmap_alloc_pv: pgi_nfree inconsistent");
312 1.3.4.2 bouyer #endif /* DIAGNOSTIC */
313 1.3.4.2 bouyer pvp->pvp_pgi.pgi_freelist = pv->pv_next;
314 1.3.4.2 bouyer }
315 1.3.4.2 bouyer return pv;
316 1.3.4.2 bouyer }
317 1.3.4.2 bouyer
318 1.3.4.2 bouyer /*
319 1.3.4.2 bouyer * Release a pv_entry structure putting it back on the freelist.
320 1.3.4.2 bouyer */
321 1.3.4.2 bouyer
322 1.3.4.2 bouyer void
323 1.3.4.2 bouyer pmap_free_pv(pv)
324 1.3.4.2 bouyer struct pv_entry *pv;
325 1.3.4.2 bouyer {
326 1.3.4.2 bouyer struct pv_page *pvp;
327 1.3.4.2 bouyer
328 1.3.4.2 bouyer pvp = (struct pv_page *) trunc_page((vaddr_t)pv);
329 1.3.4.2 bouyer switch (++pvp->pvp_pgi.pgi_nfree) {
330 1.3.4.2 bouyer case 1:
331 1.3.4.2 bouyer TAILQ_INSERT_TAIL(&pv_page_freelist, pvp, pvp_pgi.pgi_list);
332 1.3.4.2 bouyer default:
333 1.3.4.2 bouyer pv->pv_next = pvp->pvp_pgi.pgi_freelist;
334 1.3.4.2 bouyer pvp->pvp_pgi.pgi_freelist = pv;
335 1.3.4.2 bouyer ++pv_nfree;
336 1.3.4.2 bouyer break;
337 1.3.4.2 bouyer case NPVPPG:
338 1.3.4.2 bouyer pv_nfree -= NPVPPG - 1;
339 1.3.4.2 bouyer TAILQ_REMOVE(&pv_page_freelist, pvp, pvp_pgi.pgi_list);
340 1.3.4.2 bouyer FREE((vaddr_t)pvp, M_VMPVENT);
341 1.3.4.2 bouyer break;
342 1.3.4.2 bouyer }
343 1.3.4.2 bouyer }
344 1.3.4.2 bouyer
345 1.3.4.2 bouyer #if 0
346 1.3.4.2 bouyer void
347 1.3.4.2 bouyer pmap_collect_pv()
348 1.3.4.2 bouyer {
349 1.3.4.2 bouyer struct pv_page_list pv_page_collectlist;
350 1.3.4.2 bouyer struct pv_page *pvp, *npvp;
351 1.3.4.2 bouyer struct pv_entry *ph, *ppv, *pv, *npv;
352 1.3.4.2 bouyer int s;
353 1.3.4.2 bouyer
354 1.3.4.2 bouyer TAILQ_INIT(&pv_page_collectlist);
355 1.3.4.2 bouyer
356 1.3.4.2 bouyer for (pvp = pv_page_freelist.tqh_first; pvp; pvp = npvp) {
357 1.3.4.2 bouyer if (pv_nfree < NPVPPG)
358 1.3.4.2 bouyer break;
359 1.3.4.2 bouyer npvp = pvp->pvp_pgi.pgi_list.tqe_next;
360 1.3.4.2 bouyer if (pvp->pvp_pgi.pgi_nfree > NPVPPG / 3) {
361 1.3.4.2 bouyer TAILQ_REMOVE(&pv_page_freelist, pvp, pvp_pgi.pgi_list);
362 1.3.4.2 bouyer TAILQ_INSERT_TAIL(&pv_page_collectlist, pvp,
363 1.3.4.2 bouyer pvp_pgi.pgi_list);
364 1.3.4.2 bouyer pv_nfree -= NPVPPG;
365 1.3.4.2 bouyer pvp->pvp_pgi.pgi_nfree = -1;
366 1.3.4.2 bouyer }
367 1.3.4.2 bouyer }
368 1.3.4.2 bouyer
369 1.3.4.2 bouyer if (pv_page_collectlist.tqh_first == 0)
370 1.3.4.2 bouyer return;
371 1.3.4.2 bouyer
372 1.3.4.2 bouyer for (ph = &pv_table[npages - 1]; ph >= &pv_table[0]; ph--) {
373 1.3.4.2 bouyer if (ph->pv_pmap == 0)
374 1.3.4.2 bouyer continue;
375 1.3.4.2 bouyer s = splvm();
376 1.3.4.2 bouyer for (ppv = ph; (pv = ppv->pv_next) != 0; ) {
377 1.3.4.2 bouyer pvp = (struct pv_page *) trunc_page((vaddr_t)pv);
378 1.3.4.2 bouyer if (pvp->pvp_pgi.pgi_nfree == -1) {
379 1.3.4.2 bouyer pvp = pv_page_freelist.tqh_first;
380 1.3.4.2 bouyer if (--pvp->pvp_pgi.pgi_nfree == 0) {
381 1.3.4.2 bouyer TAILQ_REMOVE(&pv_page_freelist,
382 1.3.4.2 bouyer pvp, pvp_pgi.pgi_list);
383 1.3.4.2 bouyer }
384 1.3.4.2 bouyer npv = pvp->pvp_pgi.pgi_freelist;
385 1.3.4.2 bouyer #ifdef DIAGNOSTIC
386 1.3.4.2 bouyer if (npv == 0)
387 1.3.4.2 bouyer panic("pmap_collect_pv: pgi_nfree inconsistent");
388 1.3.4.2 bouyer #endif /* DIAGNOSTIC */
389 1.3.4.2 bouyer pvp->pvp_pgi.pgi_freelist = npv->pv_next;
390 1.3.4.2 bouyer *npv = *pv;
391 1.3.4.2 bouyer ppv->pv_next = npv;
392 1.3.4.2 bouyer ppv = npv;
393 1.3.4.2 bouyer } else
394 1.3.4.2 bouyer ppv = pv;
395 1.3.4.2 bouyer }
396 1.3.4.2 bouyer splx(s);
397 1.3.4.2 bouyer }
398 1.3.4.2 bouyer
399 1.3.4.2 bouyer for (pvp = pv_page_collectlist.tqh_first; pvp; pvp = npvp) {
400 1.3.4.2 bouyer npvp = pvp->pvp_pgi.pgi_list.tqe_next;
401 1.3.4.2 bouyer FREE((vaddr_t)pvp, M_VMPVENT);
402 1.3.4.2 bouyer }
403 1.3.4.2 bouyer }
404 1.3.4.2 bouyer #endif
405 1.3.4.2 bouyer
406 1.3.4.2 bouyer /*
407 1.3.4.2 bouyer * Enter a new physical-virtual mapping into the pv table
408 1.3.4.2 bouyer */
409 1.3.4.2 bouyer
410 1.3.4.2 bouyer /*__inline*/ void
411 1.3.4.2 bouyer pmap_enter_pv(pmap, va, pv, flags)
412 1.3.4.2 bouyer pmap_t pmap;
413 1.3.4.2 bouyer vaddr_t va;
414 1.3.4.2 bouyer struct pv_entry *pv;
415 1.3.4.2 bouyer u_int flags;
416 1.3.4.2 bouyer {
417 1.3.4.2 bouyer struct pv_entry *npv;
418 1.3.4.2 bouyer u_int s;
419 1.3.4.2 bouyer
420 1.3.4.2 bouyer #ifdef DIAGNOSTIC
421 1.3.4.2 bouyer if (!pmap_initialized)
422 1.3.4.2 bouyer panic("pmap_enter_pv: !pmap_initialized");
423 1.3.4.2 bouyer #endif
424 1.3.4.2 bouyer
425 1.3.4.2 bouyer s = splvm();
426 1.3.4.2 bouyer
427 1.3.4.2 bouyer PDEBUG(5, printf("pmap_enter_pv: pv %p: %08lx/%p/%p\n",
428 1.3.4.2 bouyer pv, pv->pv_va, pv->pv_pmap, pv->pv_next));
429 1.3.4.2 bouyer
430 1.3.4.2 bouyer if (pv->pv_pmap == NULL) {
431 1.3.4.2 bouyer /*
432 1.3.4.2 bouyer * No entries yet, use header as the first entry
433 1.3.4.2 bouyer */
434 1.3.4.2 bouyer pv->pv_va = va;
435 1.3.4.2 bouyer pv->pv_pmap = pmap;
436 1.3.4.2 bouyer pv->pv_next = NULL;
437 1.3.4.2 bouyer pv->pv_flags = flags;
438 1.3.4.2 bouyer } else {
439 1.3.4.2 bouyer /*
440 1.3.4.2 bouyer * There is at least one other VA mapping this page.
441 1.3.4.2 bouyer * Place this entry after the header.
442 1.3.4.2 bouyer */
443 1.3.4.2 bouyer #ifdef PMAP_DEBUG
444 1.3.4.2 bouyer for (npv = pv; npv; npv = npv->pv_next)
445 1.3.4.2 bouyer if (pmap == npv->pv_pmap && va == npv->pv_va)
446 1.3.4.2 bouyer panic("pmap_enter_pv: already in pv_tab pv %p: %08lx/%p/%p",
447 1.3.4.2 bouyer pv, pv->pv_va, pv->pv_pmap, pv->pv_next);
448 1.3.4.2 bouyer #endif
449 1.3.4.2 bouyer npv = pmap_alloc_pv();
450 1.3.4.2 bouyer npv->pv_va = va;
451 1.3.4.2 bouyer npv->pv_pmap = pmap;
452 1.3.4.2 bouyer npv->pv_flags = flags;
453 1.3.4.2 bouyer npv->pv_next = pv->pv_next;
454 1.3.4.2 bouyer pv->pv_next = npv;
455 1.3.4.2 bouyer }
456 1.3.4.2 bouyer
457 1.3.4.2 bouyer if (flags & PT_W)
458 1.3.4.2 bouyer ++pmap->pm_stats.wired_count;
459 1.3.4.2 bouyer
460 1.3.4.2 bouyer splx(s);
461 1.3.4.2 bouyer }
462 1.3.4.2 bouyer
463 1.3.4.2 bouyer
464 1.3.4.2 bouyer /*
465 1.3.4.2 bouyer * Remove a physical-virtual mapping from the pv table
466 1.3.4.2 bouyer */
467 1.3.4.2 bouyer
468 1.3.4.2 bouyer /*__inline*/ void
469 1.3.4.2 bouyer pmap_remove_pv(pmap, va, pv)
470 1.3.4.2 bouyer pmap_t pmap;
471 1.3.4.2 bouyer vaddr_t va;
472 1.3.4.2 bouyer struct pv_entry *pv;
473 1.3.4.2 bouyer {
474 1.3.4.2 bouyer struct pv_entry *npv;
475 1.3.4.2 bouyer u_int s;
476 1.3.4.2 bouyer u_int flags = 0;
477 1.3.4.2 bouyer
478 1.3.4.2 bouyer #ifdef DIAGNOSTIC
479 1.3.4.2 bouyer if (!pmap_initialized)
480 1.3.4.2 bouyer panic("pmap_remove_pv: !pmap_initialized");
481 1.3.4.2 bouyer #endif
482 1.3.4.2 bouyer
483 1.3.4.2 bouyer s = splvm();
484 1.3.4.2 bouyer
485 1.3.4.2 bouyer /*
486 1.3.4.2 bouyer * If it is the first entry on the list, it is actually
487 1.3.4.2 bouyer * in the header and we must copy the following entry up
488 1.3.4.2 bouyer * to the header. Otherwise we must search the list for
489 1.3.4.2 bouyer * the entry. In either case we free the now unused entry.
490 1.3.4.2 bouyer */
491 1.3.4.2 bouyer
492 1.3.4.2 bouyer if (pmap == pv->pv_pmap && va == pv->pv_va) {
493 1.3.4.2 bouyer npv = pv->pv_next;
494 1.3.4.2 bouyer if (npv) {
495 1.3.4.2 bouyer *pv = *npv;
496 1.3.4.2 bouyer flags = npv->pv_flags;
497 1.3.4.2 bouyer pmap_free_pv(npv);
498 1.3.4.2 bouyer } else {
499 1.3.4.2 bouyer flags = pv->pv_flags;
500 1.3.4.2 bouyer pv->pv_pmap = NULL;
501 1.3.4.2 bouyer }
502 1.3.4.2 bouyer } else {
503 1.3.4.2 bouyer for (npv = pv->pv_next; npv; pv = npv, npv = npv->pv_next) {
504 1.3.4.2 bouyer if (pmap == npv->pv_pmap && va == npv->pv_va)
505 1.3.4.2 bouyer break;
506 1.3.4.2 bouyer }
507 1.3.4.2 bouyer if (npv) {
508 1.3.4.2 bouyer pv->pv_next = npv->pv_next;
509 1.3.4.2 bouyer flags = npv->pv_flags;
510 1.3.4.2 bouyer pmap_free_pv(npv);
511 1.3.4.2 bouyer } else
512 1.3.4.2 bouyer panic("pmap_remove_pv: lost entry");
513 1.3.4.2 bouyer }
514 1.3.4.2 bouyer
515 1.3.4.2 bouyer if (flags & PT_W)
516 1.3.4.2 bouyer --pmap->pm_stats.wired_count;
517 1.3.4.2 bouyer
518 1.3.4.2 bouyer splx(s);
519 1.3.4.2 bouyer }
520 1.3.4.2 bouyer
521 1.3.4.2 bouyer /*
522 1.3.4.2 bouyer * Modify a physical-virtual mapping in the pv table
523 1.3.4.2 bouyer */
524 1.3.4.2 bouyer
525 1.3.4.2 bouyer /*__inline */ u_int
526 1.3.4.2 bouyer pmap_modify_pv(pmap, va, pv, bic_mask, eor_mask)
527 1.3.4.2 bouyer pmap_t pmap;
528 1.3.4.2 bouyer vaddr_t va;
529 1.3.4.2 bouyer struct pv_entry *pv;
530 1.3.4.2 bouyer u_int bic_mask;
531 1.3.4.2 bouyer u_int eor_mask;
532 1.3.4.2 bouyer {
533 1.3.4.2 bouyer struct pv_entry *npv;
534 1.3.4.2 bouyer u_int s;
535 1.3.4.2 bouyer u_int flags, oflags;
536 1.3.4.2 bouyer
537 1.3.4.2 bouyer PDEBUG(5, printf("pmap_modify_pv(pmap=%p, va=%08lx, pv=%p, bic_mask=%08x, eor_mask=%08x)\n",
538 1.3.4.2 bouyer pmap, va, pv, bic_mask, eor_mask));
539 1.3.4.2 bouyer
540 1.3.4.2 bouyer #ifdef DIAGNOSTIC
541 1.3.4.2 bouyer if (!pmap_initialized)
542 1.3.4.2 bouyer panic("pmap_modify_pv: !pmap_initialized");
543 1.3.4.2 bouyer #endif
544 1.3.4.2 bouyer
545 1.3.4.2 bouyer s = splvm();
546 1.3.4.2 bouyer
547 1.3.4.2 bouyer PDEBUG(5, printf("pmap_modify_pv: pv %p: %08lx/%p/%p/%08x ",
548 1.3.4.2 bouyer pv, pv->pv_va, pv->pv_pmap, pv->pv_next, pv->pv_flags));
549 1.3.4.2 bouyer
550 1.3.4.2 bouyer /*
551 1.3.4.2 bouyer * There is at least one VA mapping this page.
552 1.3.4.2 bouyer */
553 1.3.4.2 bouyer
554 1.3.4.2 bouyer for (npv = pv; npv; npv = npv->pv_next) {
555 1.3.4.2 bouyer if (pmap == npv->pv_pmap && va == npv->pv_va) {
556 1.3.4.2 bouyer oflags = npv->pv_flags;
557 1.3.4.2 bouyer npv->pv_flags = flags =
558 1.3.4.2 bouyer ((oflags & ~bic_mask) ^ eor_mask);
559 1.3.4.2 bouyer if ((flags ^ oflags) & PT_W) {
560 1.3.4.2 bouyer if (flags & PT_W)
561 1.3.4.2 bouyer ++pmap->pm_stats.wired_count;
562 1.3.4.2 bouyer else
563 1.3.4.2 bouyer --pmap->pm_stats.wired_count;
564 1.3.4.2 bouyer }
565 1.3.4.2 bouyer PDEBUG(0, printf("done flags=%08x\n", flags));
566 1.3.4.2 bouyer splx(s);
567 1.3.4.2 bouyer return (oflags);
568 1.3.4.2 bouyer }
569 1.3.4.2 bouyer }
570 1.3.4.2 bouyer
571 1.3.4.2 bouyer PDEBUG(0, printf("done.\n"));
572 1.3.4.2 bouyer splx(s);
573 1.3.4.2 bouyer return (0);
574 1.3.4.2 bouyer }
575 1.3.4.2 bouyer
576 1.3.4.2 bouyer
577 1.3.4.2 bouyer /*
578 1.3.4.2 bouyer * Map the specified level 2 pagetable into the level 1 page table for
579 1.3.4.2 bouyer * the given pmap to cover a chunk of virtual address space starting from the
580 1.3.4.2 bouyer * address specified.
581 1.3.4.2 bouyer */
582 1.3.4.2 bouyer static /*__inline*/ void
583 1.3.4.2 bouyer pmap_map_in_l1(pmap, va, l2pa)
584 1.3.4.2 bouyer pmap_t pmap;
585 1.3.4.2 bouyer vaddr_t va, l2pa;
586 1.3.4.2 bouyer {
587 1.3.4.2 bouyer vaddr_t ptva;
588 1.3.4.2 bouyer
589 1.3.4.2 bouyer /* Calculate the index into the L1 page table. */
590 1.3.4.2 bouyer ptva = (va >> PDSHIFT) & ~3;
591 1.3.4.2 bouyer
592 1.3.4.2 bouyer PDEBUG(0, printf("wiring %08lx in to pd%p pte0x%lx va0x%lx\n", l2pa,
593 1.3.4.2 bouyer pmap->pm_pdir, L1_PTE(l2pa), ptva));
594 1.3.4.2 bouyer
595 1.3.4.2 bouyer /* Map page table into the L1. */
596 1.3.4.2 bouyer pmap->pm_pdir[ptva + 0] = L1_PTE(l2pa + 0x000);
597 1.3.4.2 bouyer pmap->pm_pdir[ptva + 1] = L1_PTE(l2pa + 0x400);
598 1.3.4.2 bouyer pmap->pm_pdir[ptva + 2] = L1_PTE(l2pa + 0x800);
599 1.3.4.2 bouyer pmap->pm_pdir[ptva + 3] = L1_PTE(l2pa + 0xc00);
600 1.3.4.2 bouyer
601 1.3.4.2 bouyer PDEBUG(0, printf("pt self reference %lx in %lx\n",
602 1.3.4.2 bouyer L2_PTE_NC_NB(l2pa, AP_KRW), pmap->pm_vptpt));
603 1.3.4.2 bouyer
604 1.3.4.2 bouyer /* Map the page table into the page table area. */
605 1.3.4.2 bouyer *((pt_entry_t *)(pmap->pm_vptpt + ptva)) = L2_PTE_NC_NB(l2pa, AP_KRW);
606 1.3.4.2 bouyer
607 1.3.4.2 bouyer /* XXX should be a purge */
608 1.3.4.2 bouyer /* cpu_tlb_flushD();*/
609 1.3.4.2 bouyer }
610 1.3.4.2 bouyer
611 1.3.4.2 bouyer #if 0
612 1.3.4.2 bouyer static /*__inline*/ void
613 1.3.4.2 bouyer pmap_unmap_in_l1(pmap, va)
614 1.3.4.2 bouyer pmap_t pmap;
615 1.3.4.2 bouyer vaddr_t va;
616 1.3.4.2 bouyer {
617 1.3.4.2 bouyer vaddr_t ptva;
618 1.3.4.2 bouyer
619 1.3.4.2 bouyer /* Calculate the index into the L1 page table. */
620 1.3.4.2 bouyer ptva = (va >> PDSHIFT) & ~3;
621 1.3.4.2 bouyer
622 1.3.4.2 bouyer /* Unmap page table from the L1. */
623 1.3.4.2 bouyer pmap->pm_pdir[ptva + 0] = 0;
624 1.3.4.2 bouyer pmap->pm_pdir[ptva + 1] = 0;
625 1.3.4.2 bouyer pmap->pm_pdir[ptva + 2] = 0;
626 1.3.4.2 bouyer pmap->pm_pdir[ptva + 3] = 0;
627 1.3.4.2 bouyer
628 1.3.4.2 bouyer /* Unmap the page table from the page table area. */
629 1.3.4.2 bouyer *((pt_entry_t *)(pmap->pm_vptpt + ptva)) = 0;
630 1.3.4.2 bouyer
631 1.3.4.2 bouyer /* XXX should be a purge */
632 1.3.4.2 bouyer /* cpu_tlb_flushD();*/
633 1.3.4.2 bouyer }
634 1.3.4.2 bouyer #endif
635 1.3.4.2 bouyer
636 1.3.4.2 bouyer
637 1.3.4.2 bouyer /*
638 1.3.4.2 bouyer * Used to map a range of physical addresses into kernel
639 1.3.4.2 bouyer * virtual address space.
640 1.3.4.2 bouyer *
641 1.3.4.2 bouyer * For now, VM is already on, we only need to map the
642 1.3.4.2 bouyer * specified memory.
643 1.3.4.2 bouyer */
644 1.3.4.2 bouyer vaddr_t
645 1.3.4.2 bouyer pmap_map(va, spa, epa, prot)
646 1.3.4.2 bouyer vaddr_t va, spa, epa;
647 1.3.4.2 bouyer int prot;
648 1.3.4.2 bouyer {
649 1.3.4.2 bouyer while (spa < epa) {
650 1.3.4.2 bouyer pmap_enter(pmap_kernel(), va, spa, prot, 0);
651 1.3.4.2 bouyer va += NBPG;
652 1.3.4.2 bouyer spa += NBPG;
653 1.3.4.2 bouyer }
654 1.3.4.2 bouyer return(va);
655 1.3.4.2 bouyer }
656 1.3.4.2 bouyer
657 1.3.4.2 bouyer
658 1.3.4.2 bouyer /*
659 1.3.4.2 bouyer * void pmap_bootstrap(pd_entry_t *kernel_l1pt, pv_addr_t kernel_ptpt)
660 1.3.4.2 bouyer *
661 1.3.4.2 bouyer * bootstrap the pmap system. This is called from initarm and allows
662 1.3.4.2 bouyer * the pmap system to initailise any structures it requires.
663 1.3.4.2 bouyer *
664 1.3.4.2 bouyer * Currently this sets up the kernel_pmap that is statically allocated
665 1.3.4.2 bouyer * and also allocated virtual addresses for certain page hooks.
666 1.3.4.2 bouyer * Currently the only one page hook is allocated that is used
667 1.3.4.2 bouyer * to zero physical pages of memory.
668 1.3.4.2 bouyer * It also initialises the start and end address of the kernel data space.
669 1.3.4.2 bouyer */
670 1.3.4.2 bouyer extern paddr_t physical_freestart;
671 1.3.4.2 bouyer extern paddr_t physical_freeend;
672 1.3.4.2 bouyer
673 1.3.4.2 bouyer struct pv_entry *boot_pvent;
674 1.3.4.2 bouyer char *boot_attrs;
675 1.3.4.2 bouyer
676 1.3.4.2 bouyer void
677 1.3.4.2 bouyer pmap_bootstrap(kernel_l1pt, kernel_ptpt)
678 1.3.4.2 bouyer pd_entry_t *kernel_l1pt;
679 1.3.4.2 bouyer pv_addr_t kernel_ptpt;
680 1.3.4.2 bouyer {
681 1.3.4.2 bouyer int loop;
682 1.3.4.2 bouyer paddr_t start, end;
683 1.3.4.2 bouyer #if NISADMA > 0
684 1.3.4.2 bouyer paddr_t istart;
685 1.3.4.2 bouyer psize_t isize;
686 1.3.4.2 bouyer #endif
687 1.3.4.2 bouyer vsize_t size;
688 1.3.4.2 bouyer
689 1.3.4.2 bouyer kernel_pmap = &kernel_pmap_store;
690 1.3.4.2 bouyer
691 1.3.4.2 bouyer kernel_pmap->pm_pdir = kernel_l1pt;
692 1.3.4.2 bouyer kernel_pmap->pm_pptpt = kernel_ptpt.pv_pa;
693 1.3.4.2 bouyer kernel_pmap->pm_vptpt = kernel_ptpt.pv_va;
694 1.3.4.2 bouyer simple_lock_init(&kernel_pmap->pm_lock);
695 1.3.4.2 bouyer kernel_pmap->pm_count = 1;
696 1.3.4.2 bouyer
697 1.3.4.2 bouyer /*
698 1.3.4.2 bouyer * Initialize PAGE_SIZE-dependent variables.
699 1.3.4.2 bouyer */
700 1.3.4.2 bouyer uvm_setpagesize();
701 1.3.4.2 bouyer
702 1.3.4.2 bouyer npages = 0;
703 1.3.4.2 bouyer loop = 0;
704 1.3.4.2 bouyer while (loop < bootconfig.dramblocks) {
705 1.3.4.2 bouyer start = (paddr_t)bootconfig.dram[loop].address;
706 1.3.4.2 bouyer end = start + (bootconfig.dram[loop].pages * NBPG);
707 1.3.4.2 bouyer if (start < physical_freestart)
708 1.3.4.2 bouyer start = physical_freestart;
709 1.3.4.2 bouyer if (end > physical_freeend)
710 1.3.4.2 bouyer end = physical_freeend;
711 1.3.4.2 bouyer #if 0
712 1.3.4.2 bouyer printf("%d: %lx -> %lx\n", loop, start, end - 1);
713 1.3.4.2 bouyer #endif
714 1.3.4.2 bouyer #if NISADMA > 0
715 1.3.4.2 bouyer if (pmap_isa_dma_range_intersect(start, end - start,
716 1.3.4.2 bouyer &istart, &isize)) {
717 1.3.4.2 bouyer /*
718 1.3.4.2 bouyer * Place the pages that intersect with the
719 1.3.4.2 bouyer * ISA DMA range onto the ISA DMA free list.
720 1.3.4.2 bouyer */
721 1.3.4.2 bouyer #if 0
722 1.3.4.2 bouyer printf(" ISADMA 0x%lx -> 0x%lx\n", istart,
723 1.3.4.2 bouyer istart + isize - 1);
724 1.3.4.2 bouyer #endif
725 1.3.4.2 bouyer uvm_page_physload(atop(istart),
726 1.3.4.2 bouyer atop(istart + isize), atop(istart),
727 1.3.4.2 bouyer atop(istart + isize), VM_FREELIST_ISADMA);
728 1.3.4.2 bouyer npages += atop(istart + isize) - atop(istart);
729 1.3.4.2 bouyer
730 1.3.4.2 bouyer /*
731 1.3.4.2 bouyer * Load the pieces that come before
732 1.3.4.2 bouyer * the intersection into the default
733 1.3.4.2 bouyer * free list.
734 1.3.4.2 bouyer */
735 1.3.4.2 bouyer if (start < istart) {
736 1.3.4.2 bouyer #if 0
737 1.3.4.2 bouyer printf(" BEFORE 0x%lx -> 0x%lx\n",
738 1.3.4.2 bouyer start, istart - 1);
739 1.3.4.2 bouyer #endif
740 1.3.4.2 bouyer uvm_page_physload(atop(start),
741 1.3.4.2 bouyer atop(istart), atop(start),
742 1.3.4.2 bouyer atop(istart), VM_FREELIST_DEFAULT);
743 1.3.4.2 bouyer npages += atop(istart) - atop(start);
744 1.3.4.2 bouyer }
745 1.3.4.2 bouyer
746 1.3.4.2 bouyer /*
747 1.3.4.2 bouyer * Load the pieces that come after
748 1.3.4.2 bouyer * the intersection into the default
749 1.3.4.2 bouyer * free list.
750 1.3.4.2 bouyer */
751 1.3.4.2 bouyer if ((istart + isize) < end) {
752 1.3.4.2 bouyer #if 0
753 1.3.4.2 bouyer printf(" AFTER 0x%lx -> 0x%lx\n",
754 1.3.4.2 bouyer (istart + isize), end - 1);
755 1.3.4.2 bouyer #endif
756 1.3.4.2 bouyer uvm_page_physload(atop(istart + isize),
757 1.3.4.2 bouyer atop(end), atop(istart + isize),
758 1.3.4.2 bouyer atop(end), VM_FREELIST_DEFAULT);
759 1.3.4.2 bouyer npages += atop(end) - atop(istart + isize);
760 1.3.4.2 bouyer }
761 1.3.4.2 bouyer } else {
762 1.3.4.2 bouyer uvm_page_physload(atop(start), atop(end),
763 1.3.4.2 bouyer atop(start), atop(end), VM_FREELIST_DEFAULT);
764 1.3.4.2 bouyer npages += atop(end) - atop(start);
765 1.3.4.2 bouyer }
766 1.3.4.2 bouyer #else /* NISADMA > 0 */
767 1.3.4.2 bouyer uvm_page_physload(atop(start), atop(end),
768 1.3.4.2 bouyer atop(start), atop(end), VM_FREELIST_DEFAULT);
769 1.3.4.2 bouyer npages += atop(end) - atop(start);
770 1.3.4.2 bouyer #endif /* NISADMA > 0 */
771 1.3.4.2 bouyer ++loop;
772 1.3.4.2 bouyer }
773 1.3.4.2 bouyer
774 1.3.4.2 bouyer #ifdef MYCROFT_HACK
775 1.3.4.2 bouyer printf("npages = %ld\n", npages);
776 1.3.4.2 bouyer #endif
777 1.3.4.2 bouyer
778 1.3.4.2 bouyer virtual_start = KERNEL_VM_BASE;
779 1.3.4.2 bouyer virtual_end = virtual_start + KERNEL_VM_SIZE - 1;
780 1.3.4.2 bouyer
781 1.3.4.2 bouyer ALLOC_PAGE_HOOK(page_hook0, NBPG);
782 1.3.4.2 bouyer ALLOC_PAGE_HOOK(page_hook1, NBPG);
783 1.3.4.2 bouyer
784 1.3.4.2 bouyer /*
785 1.3.4.2 bouyer * The mem special device needs a virtual hook but we don't
786 1.3.4.2 bouyer * need a pte
787 1.3.4.2 bouyer */
788 1.3.4.2 bouyer memhook = (char *)virtual_start;
789 1.3.4.2 bouyer virtual_start += NBPG;
790 1.3.4.2 bouyer
791 1.3.4.2 bouyer msgbufaddr = (caddr_t)virtual_start;
792 1.3.4.2 bouyer msgbufpte = (pt_entry_t)pmap_pte(kernel_pmap, virtual_start);
793 1.3.4.2 bouyer virtual_start += round_page(MSGBUFSIZE);
794 1.3.4.2 bouyer
795 1.3.4.2 bouyer size = npages * sizeof(struct pv_entry);
796 1.3.4.2 bouyer boot_pvent = (struct pv_entry *)uvm_pageboot_alloc(size);
797 1.3.4.2 bouyer bzero(boot_pvent, size);
798 1.3.4.2 bouyer size = npages * sizeof(char);
799 1.3.4.2 bouyer boot_attrs = (char *)uvm_pageboot_alloc(size);
800 1.3.4.2 bouyer bzero(boot_attrs, size);
801 1.3.4.2 bouyer
802 1.3.4.2 bouyer cpu_cache_cleanD();
803 1.3.4.2 bouyer }
804 1.3.4.2 bouyer
805 1.3.4.2 bouyer /*
806 1.3.4.2 bouyer * void pmap_init(void)
807 1.3.4.2 bouyer *
808 1.3.4.2 bouyer * Initialize the pmap module.
809 1.3.4.2 bouyer * Called by vm_init() in vm/vm_init.c in order to initialise
810 1.3.4.2 bouyer * any structures that the pmap system needs to map virtual memory.
811 1.3.4.2 bouyer */
812 1.3.4.2 bouyer
813 1.3.4.2 bouyer extern int physmem;
814 1.3.4.2 bouyer
815 1.3.4.2 bouyer void
816 1.3.4.2 bouyer pmap_init()
817 1.3.4.2 bouyer {
818 1.3.4.2 bouyer int lcv;
819 1.3.4.2 bouyer
820 1.3.4.2 bouyer #ifdef MYCROFT_HACK
821 1.3.4.2 bouyer printf("physmem = %d\n", physmem);
822 1.3.4.2 bouyer #endif
823 1.3.4.2 bouyer
824 1.3.4.2 bouyer /*
825 1.3.4.2 bouyer * Set the available memory vars - These do not map to real memory
826 1.3.4.2 bouyer * addresses and cannot as the physical memory is fragmented.
827 1.3.4.2 bouyer * They are used by ps for %mem calculations.
828 1.3.4.2 bouyer * One could argue whether this should be the entire memory or just
829 1.3.4.2 bouyer * the memory that is useable in a user process.
830 1.3.4.2 bouyer */
831 1.3.4.2 bouyer avail_start = 0;
832 1.3.4.2 bouyer avail_end = physmem * NBPG;
833 1.3.4.2 bouyer
834 1.3.4.2 bouyer /* Set up pmap info for physsegs. */
835 1.3.4.2 bouyer for (lcv = 0; lcv < vm_nphysseg; lcv++) {
836 1.3.4.2 bouyer vm_physmem[lcv].pmseg.pvent = boot_pvent;
837 1.3.4.2 bouyer boot_pvent += vm_physmem[lcv].end - vm_physmem[lcv].start;
838 1.3.4.2 bouyer vm_physmem[lcv].pmseg.attrs = boot_attrs;
839 1.3.4.2 bouyer boot_attrs += vm_physmem[lcv].end - vm_physmem[lcv].start;
840 1.3.4.2 bouyer }
841 1.3.4.2 bouyer #ifdef MYCROFT_HACK
842 1.3.4.2 bouyer for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
843 1.3.4.2 bouyer printf("physseg[%d] pvent=%p attrs=%p start=%ld end=%ld\n",
844 1.3.4.2 bouyer lcv,
845 1.3.4.2 bouyer vm_physmem[lcv].pmseg.pvent, vm_physmem[lcv].pmseg.attrs,
846 1.3.4.2 bouyer vm_physmem[lcv].start, vm_physmem[lcv].end);
847 1.3.4.2 bouyer }
848 1.3.4.2 bouyer #endif
849 1.3.4.2 bouyer TAILQ_INIT(&pv_page_freelist);
850 1.3.4.2 bouyer
851 1.3.4.2 bouyer #ifdef DIAGNOSTIC
852 1.3.4.2 bouyer /* Now it is safe to enable pv_entry recording. */
853 1.3.4.2 bouyer pmap_initialized = TRUE;
854 1.3.4.2 bouyer #endif
855 1.3.4.2 bouyer
856 1.3.4.2 bouyer /* Initialise our L1 page table queues and counters */
857 1.3.4.2 bouyer SIMPLEQ_INIT(&l1pt_static_queue);
858 1.3.4.2 bouyer l1pt_static_queue_count = 0;
859 1.3.4.2 bouyer l1pt_static_create_count = 0;
860 1.3.4.2 bouyer SIMPLEQ_INIT(&l1pt_queue);
861 1.3.4.2 bouyer l1pt_queue_count = 0;
862 1.3.4.2 bouyer l1pt_create_count = 0;
863 1.3.4.2 bouyer l1pt_reuse_count = 0;
864 1.3.4.2 bouyer }
865 1.3.4.2 bouyer
866 1.3.4.2 bouyer /*
867 1.3.4.2 bouyer * pmap_postinit()
868 1.3.4.2 bouyer *
869 1.3.4.2 bouyer * This routine is called after the vm and kmem subsystems have been
870 1.3.4.2 bouyer * initialised. This allows the pmap code to perform any initialisation
871 1.3.4.2 bouyer * that can only be done one the memory allocation is in place.
872 1.3.4.2 bouyer */
873 1.3.4.2 bouyer
874 1.3.4.2 bouyer void
875 1.3.4.2 bouyer pmap_postinit()
876 1.3.4.2 bouyer {
877 1.3.4.2 bouyer int loop;
878 1.3.4.2 bouyer struct l1pt *pt;
879 1.3.4.2 bouyer
880 1.3.4.2 bouyer #ifdef PMAP_STATIC_L1S
881 1.3.4.2 bouyer for (loop = 0; loop < PMAP_STATIC_L1S; ++loop) {
882 1.3.4.2 bouyer #else /* PMAP_STATIC_L1S */
883 1.3.4.2 bouyer for (loop = 0; loop < max_processes; ++loop) {
884 1.3.4.2 bouyer #endif /* PMAP_STATIC_L1S */
885 1.3.4.2 bouyer /* Allocate a L1 page table */
886 1.3.4.2 bouyer pt = pmap_alloc_l1pt();
887 1.3.4.2 bouyer if (!pt)
888 1.3.4.2 bouyer panic("Cannot allocate static L1 page tables\n");
889 1.3.4.2 bouyer
890 1.3.4.2 bouyer /* Clean it */
891 1.3.4.2 bouyer bzero((void *)pt->pt_va, PD_SIZE);
892 1.3.4.2 bouyer pt->pt_flags |= (PTFLAG_STATIC | PTFLAG_CLEAN);
893 1.3.4.2 bouyer /* Add the page table to the queue */
894 1.3.4.2 bouyer SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pt, pt_queue);
895 1.3.4.2 bouyer ++l1pt_static_queue_count;
896 1.3.4.2 bouyer ++l1pt_static_create_count;
897 1.3.4.2 bouyer }
898 1.3.4.2 bouyer }
899 1.3.4.2 bouyer
900 1.3.4.2 bouyer
901 1.3.4.2 bouyer /*
902 1.3.4.2 bouyer * Create and return a physical map.
903 1.3.4.2 bouyer *
904 1.3.4.2 bouyer * If the size specified for the map is zero, the map is an actual physical
905 1.3.4.2 bouyer * map, and may be referenced by the hardware.
906 1.3.4.2 bouyer *
907 1.3.4.2 bouyer * If the size specified is non-zero, the map will be used in software only,
908 1.3.4.2 bouyer * and is bounded by that size.
909 1.3.4.2 bouyer */
910 1.3.4.2 bouyer
911 1.3.4.2 bouyer pmap_t
912 1.3.4.2 bouyer pmap_create()
913 1.3.4.2 bouyer {
914 1.3.4.2 bouyer pmap_t pmap;
915 1.3.4.2 bouyer
916 1.3.4.2 bouyer /* Allocate memory for pmap structure and zero it */
917 1.3.4.2 bouyer pmap = (pmap_t) malloc(sizeof *pmap, M_VMPMAP, M_WAITOK);
918 1.3.4.2 bouyer bzero(pmap, sizeof(*pmap));
919 1.3.4.2 bouyer
920 1.3.4.2 bouyer /* Now init the machine part of the pmap */
921 1.3.4.2 bouyer pmap_pinit(pmap);
922 1.3.4.2 bouyer return(pmap);
923 1.3.4.2 bouyer }
924 1.3.4.2 bouyer
925 1.3.4.2 bouyer /*
926 1.3.4.2 bouyer * pmap_alloc_l1pt()
927 1.3.4.2 bouyer *
928 1.3.4.2 bouyer * This routine allocates physical and virtual memory for a L1 page table
929 1.3.4.2 bouyer * and wires it.
930 1.3.4.2 bouyer * A l1pt structure is returned to describe the allocated page table.
931 1.3.4.2 bouyer *
932 1.3.4.2 bouyer * This routine is allowed to fail if the required memory cannot be allocated.
933 1.3.4.2 bouyer * In this case NULL is returned.
934 1.3.4.2 bouyer */
935 1.3.4.2 bouyer
936 1.3.4.2 bouyer struct l1pt *
937 1.3.4.2 bouyer pmap_alloc_l1pt(void)
938 1.3.4.2 bouyer {
939 1.3.4.2 bouyer paddr_t pa;
940 1.3.4.2 bouyer vaddr_t va;
941 1.3.4.2 bouyer struct l1pt *pt;
942 1.3.4.2 bouyer int error;
943 1.3.4.2 bouyer vm_page_t m;
944 1.3.4.2 bouyer pt_entry_t *pte;
945 1.3.4.2 bouyer
946 1.3.4.2 bouyer /* Allocate virtual address space for the L1 page table */
947 1.3.4.2 bouyer va = uvm_km_valloc(kernel_map, PD_SIZE);
948 1.3.4.2 bouyer if (va == 0) {
949 1.3.4.2 bouyer #ifdef DIAGNOSTIC
950 1.3.4.2 bouyer printf("pmap: Cannot allocate pageable memory for L1\n");
951 1.3.4.2 bouyer #endif /* DIAGNOSTIC */
952 1.3.4.2 bouyer return(NULL);
953 1.3.4.2 bouyer }
954 1.3.4.2 bouyer
955 1.3.4.2 bouyer /* Allocate memory for the l1pt structure */
956 1.3.4.2 bouyer pt = (struct l1pt *)malloc(sizeof(struct l1pt), M_VMPMAP, M_WAITOK);
957 1.3.4.2 bouyer
958 1.3.4.2 bouyer /*
959 1.3.4.2 bouyer * Allocate pages from the VM system.
960 1.3.4.2 bouyer */
961 1.3.4.2 bouyer TAILQ_INIT(&pt->pt_plist);
962 1.3.4.2 bouyer error = uvm_pglistalloc(PD_SIZE, physical_start, physical_end,
963 1.3.4.2 bouyer PD_SIZE, 0, &pt->pt_plist, 1, M_WAITOK);
964 1.3.4.2 bouyer if (error) {
965 1.3.4.2 bouyer #ifdef DIAGNOSTIC
966 1.3.4.2 bouyer printf("pmap: Cannot allocate physical memory for L1 (%d)\n",
967 1.3.4.2 bouyer error);
968 1.3.4.2 bouyer #endif /* DIAGNOSTIC */
969 1.3.4.2 bouyer /* Release the resources we already have claimed */
970 1.3.4.2 bouyer free(pt, M_VMPMAP);
971 1.3.4.2 bouyer uvm_km_free(kernel_map, va, PD_SIZE);
972 1.3.4.2 bouyer return(NULL);
973 1.3.4.2 bouyer }
974 1.3.4.2 bouyer
975 1.3.4.2 bouyer /* Map our physical pages into our virtual space */
976 1.3.4.2 bouyer pt->pt_va = va;
977 1.3.4.2 bouyer m = pt->pt_plist.tqh_first;
978 1.3.4.2 bouyer while (m && va < (pt->pt_va + PD_SIZE)) {
979 1.3.4.2 bouyer pa = VM_PAGE_TO_PHYS(m);
980 1.3.4.2 bouyer
981 1.3.4.2 bouyer pmap_enter(pmap_kernel(), va, pa,
982 1.3.4.2 bouyer VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
983 1.3.4.2 bouyer
984 1.3.4.2 bouyer /* Revoke cacheability and bufferability */
985 1.3.4.2 bouyer /* XXX should be done better than this */
986 1.3.4.2 bouyer pte = pmap_pte(pmap_kernel(), va);
987 1.3.4.2 bouyer *pte = *pte & ~(PT_C | PT_B);
988 1.3.4.2 bouyer
989 1.3.4.2 bouyer va += NBPG;
990 1.3.4.2 bouyer m = m->pageq.tqe_next;
991 1.3.4.2 bouyer }
992 1.3.4.2 bouyer
993 1.3.4.2 bouyer #ifdef DIAGNOSTIC
994 1.3.4.2 bouyer if (m)
995 1.3.4.2 bouyer panic("pmap_alloc_l1pt: pglist not empty\n");
996 1.3.4.2 bouyer #endif /* DIAGNOSTIC */
997 1.3.4.2 bouyer
998 1.3.4.2 bouyer pt->pt_flags = 0;
999 1.3.4.2 bouyer return(pt);
1000 1.3.4.2 bouyer }
1001 1.3.4.2 bouyer
1002 1.3.4.2 bouyer /*
1003 1.3.4.2 bouyer * Free a L1 page table previously allocated with pmap_alloc_l1pt().
1004 1.3.4.2 bouyer */
1005 1.3.4.2 bouyer void
1006 1.3.4.2 bouyer pmap_free_l1pt(pt)
1007 1.3.4.2 bouyer struct l1pt *pt;
1008 1.3.4.2 bouyer {
1009 1.3.4.2 bouyer /* Separate the physical memory for the virtual space */
1010 1.3.4.2 bouyer pmap_remove(kernel_pmap, pt->pt_va, pt->pt_va + PD_SIZE);
1011 1.3.4.2 bouyer
1012 1.3.4.2 bouyer /* Return the physical memory */
1013 1.3.4.2 bouyer uvm_pglistfree(&pt->pt_plist);
1014 1.3.4.2 bouyer
1015 1.3.4.2 bouyer /* Free the virtual space */
1016 1.3.4.2 bouyer uvm_km_free(kernel_map, pt->pt_va, PD_SIZE);
1017 1.3.4.2 bouyer
1018 1.3.4.2 bouyer /* Free the l1pt structure */
1019 1.3.4.2 bouyer free(pt, M_VMPMAP);
1020 1.3.4.2 bouyer }
1021 1.3.4.2 bouyer
1022 1.3.4.2 bouyer /*
1023 1.3.4.2 bouyer * Allocate a page directory.
1024 1.3.4.2 bouyer * This routine will either allocate a new page directory from the pool
1025 1.3.4.2 bouyer * of L1 page tables currently held by the kernel or it will allocate
1026 1.3.4.2 bouyer * a new one via pmap_alloc_l1pt().
1027 1.3.4.2 bouyer * It will then initialise the l1 page table for use.
1028 1.3.4.2 bouyer */
1029 1.3.4.2 bouyer int
1030 1.3.4.2 bouyer pmap_allocpagedir(pmap)
1031 1.3.4.2 bouyer struct pmap *pmap;
1032 1.3.4.2 bouyer {
1033 1.3.4.2 bouyer paddr_t pa;
1034 1.3.4.2 bouyer struct l1pt *pt;
1035 1.3.4.2 bouyer pt_entry_t *pte;
1036 1.3.4.2 bouyer
1037 1.3.4.2 bouyer PDEBUG(0, printf("pmap_allocpagedir(%p)\n", pmap));
1038 1.3.4.2 bouyer
1039 1.3.4.2 bouyer /* Do we have any spare L1's lying around ? */
1040 1.3.4.2 bouyer if (l1pt_static_queue_count) {
1041 1.3.4.2 bouyer --l1pt_static_queue_count;
1042 1.3.4.2 bouyer pt = l1pt_static_queue.sqh_first;
1043 1.3.4.2 bouyer SIMPLEQ_REMOVE_HEAD(&l1pt_static_queue, pt, pt_queue);
1044 1.3.4.2 bouyer } else if (l1pt_queue_count) {
1045 1.3.4.2 bouyer --l1pt_queue_count;
1046 1.3.4.2 bouyer pt = l1pt_queue.sqh_first;
1047 1.3.4.2 bouyer SIMPLEQ_REMOVE_HEAD(&l1pt_queue, pt, pt_queue);
1048 1.3.4.2 bouyer ++l1pt_reuse_count;
1049 1.3.4.2 bouyer } else {
1050 1.3.4.2 bouyer pt = pmap_alloc_l1pt();
1051 1.3.4.2 bouyer if (!pt)
1052 1.3.4.2 bouyer return(ENOMEM);
1053 1.3.4.2 bouyer ++l1pt_create_count;
1054 1.3.4.2 bouyer }
1055 1.3.4.2 bouyer
1056 1.3.4.2 bouyer /* Store the pointer to the l1 descriptor in the pmap. */
1057 1.3.4.2 bouyer pmap->pm_l1pt = pt;
1058 1.3.4.2 bouyer
1059 1.3.4.2 bouyer /* Get the physical address of the start of the l1 */
1060 1.3.4.2 bouyer pa = VM_PAGE_TO_PHYS(pt->pt_plist.tqh_first);
1061 1.3.4.2 bouyer
1062 1.3.4.2 bouyer /* Store the virtual address of the l1 in the pmap. */
1063 1.3.4.2 bouyer pmap->pm_pdir = (pd_entry_t *)pt->pt_va;
1064 1.3.4.2 bouyer
1065 1.3.4.2 bouyer /* Clean the L1 if it is dirty */
1066 1.3.4.2 bouyer if (!(pt->pt_flags & PTFLAG_CLEAN))
1067 1.3.4.2 bouyer bzero((void *)pmap->pm_pdir, (PD_SIZE - KERNEL_PD_SIZE));
1068 1.3.4.2 bouyer
1069 1.3.4.2 bouyer /* Do we already have the kernel mappings ? */
1070 1.3.4.2 bouyer if (!(pt->pt_flags & PTFLAG_KPT)) {
1071 1.3.4.2 bouyer /* Duplicate the kernel mapping i.e. all mappings 0xf0000000+ */
1072 1.3.4.2 bouyer
1073 1.3.4.2 bouyer bcopy((char *)kernel_pmap->pm_pdir + (PD_SIZE - KERNEL_PD_SIZE),
1074 1.3.4.2 bouyer (char *)pmap->pm_pdir + (PD_SIZE - KERNEL_PD_SIZE),
1075 1.3.4.2 bouyer KERNEL_PD_SIZE);
1076 1.3.4.2 bouyer pt->pt_flags |= PTFLAG_KPT;
1077 1.3.4.2 bouyer }
1078 1.3.4.2 bouyer
1079 1.3.4.2 bouyer /* Allocate a page table to map all the page tables for this pmap */
1080 1.3.4.2 bouyer
1081 1.3.4.2 bouyer #ifdef DIAGNOSTIC
1082 1.3.4.2 bouyer if (pmap->pm_vptpt) {
1083 1.3.4.2 bouyer /* XXX What if we have one already ? */
1084 1.3.4.2 bouyer panic("pmap_allocpagedir: have pt already\n");
1085 1.3.4.2 bouyer }
1086 1.3.4.2 bouyer #endif /* DIAGNOSTIC */
1087 1.3.4.2 bouyer pmap->pm_vptpt = uvm_km_zalloc(kernel_map, NBPG);
1088 1.3.4.2 bouyer (void) pmap_extract(kernel_pmap, pmap->pm_vptpt, &pmap->pm_pptpt);
1089 1.3.4.2 bouyer pmap->pm_pptpt &= PG_FRAME;
1090 1.3.4.2 bouyer /* Revoke cacheability and bufferability */
1091 1.3.4.2 bouyer /* XXX should be done better than this */
1092 1.3.4.2 bouyer pte = pmap_pte(kernel_pmap, pmap->pm_vptpt);
1093 1.3.4.2 bouyer *pte = *pte & ~(PT_C | PT_B);
1094 1.3.4.2 bouyer
1095 1.3.4.2 bouyer /* Wire in this page table */
1096 1.3.4.2 bouyer pmap_map_in_l1(pmap, PROCESS_PAGE_TBLS_BASE, pmap->pm_pptpt);
1097 1.3.4.2 bouyer
1098 1.3.4.2 bouyer pt->pt_flags &= ~PTFLAG_CLEAN; /* L1 is dirty now */
1099 1.3.4.2 bouyer
1100 1.3.4.2 bouyer /*
1101 1.3.4.2 bouyer * Map the kernel page tables for 0xf0000000 +
1102 1.3.4.2 bouyer * into the page table used to map the
1103 1.3.4.2 bouyer * pmap's page tables
1104 1.3.4.2 bouyer */
1105 1.3.4.2 bouyer bcopy((char *)(PROCESS_PAGE_TBLS_BASE
1106 1.3.4.2 bouyer + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2))
1107 1.3.4.2 bouyer + ((PD_SIZE - KERNEL_PD_SIZE) >> 2)),
1108 1.3.4.2 bouyer (char *)pmap->pm_vptpt + ((PD_SIZE - KERNEL_PD_SIZE) >> 2),
1109 1.3.4.2 bouyer (KERNEL_PD_SIZE >> 2));
1110 1.3.4.2 bouyer
1111 1.3.4.2 bouyer pmap->pm_count = 1;
1112 1.3.4.2 bouyer simple_lock_init(&pmap->pm_lock);
1113 1.3.4.2 bouyer
1114 1.3.4.2 bouyer return(0);
1115 1.3.4.2 bouyer }
1116 1.3.4.2 bouyer
1117 1.3.4.2 bouyer
1118 1.3.4.2 bouyer /*
1119 1.3.4.2 bouyer * Initialize a preallocated and zeroed pmap structure,
1120 1.3.4.2 bouyer * such as one in a vmspace structure.
1121 1.3.4.2 bouyer */
1122 1.3.4.2 bouyer
1123 1.3.4.2 bouyer static int pmap_pagedir_ident; /* tsleep() ident */
1124 1.3.4.2 bouyer
1125 1.3.4.2 bouyer void
1126 1.3.4.2 bouyer pmap_pinit(pmap)
1127 1.3.4.2 bouyer struct pmap *pmap;
1128 1.3.4.2 bouyer {
1129 1.3.4.2 bouyer PDEBUG(0, printf("pmap_pinit(%p)\n", pmap));
1130 1.3.4.2 bouyer
1131 1.3.4.2 bouyer /* Keep looping until we succeed in allocating a page directory */
1132 1.3.4.2 bouyer while (pmap_allocpagedir(pmap) != 0) {
1133 1.3.4.2 bouyer /*
1134 1.3.4.2 bouyer * Ok we failed to allocate a suitable block of memory for an
1135 1.3.4.2 bouyer * L1 page table. This means that either:
1136 1.3.4.2 bouyer * 1. 16KB of virtual address space could not be allocated
1137 1.3.4.2 bouyer * 2. 16KB of physically contiguous memory on a 16KB boundary
1138 1.3.4.2 bouyer * could not be allocated.
1139 1.3.4.2 bouyer *
1140 1.3.4.2 bouyer * Since we cannot fail we will sleep for a while and try
1141 1.3.4.2 bouyer * again. Although we will be wakened when another page table
1142 1.3.4.2 bouyer * is freed other memory releasing and swapping may occur
1143 1.3.4.2 bouyer * that will mean we can succeed so we will keep trying
1144 1.3.4.2 bouyer * regularly just in case.
1145 1.3.4.2 bouyer */
1146 1.3.4.2 bouyer
1147 1.3.4.2 bouyer if (tsleep((caddr_t)&pmap_pagedir_ident, PZERO,
1148 1.3.4.2 bouyer "l1ptwait", 1000) == EWOULDBLOCK)
1149 1.3.4.2 bouyer printf("pmap: Cannot allocate L1 page table, sleeping ...\n");
1150 1.3.4.2 bouyer }
1151 1.3.4.2 bouyer
1152 1.3.4.2 bouyer /* Map zero page for the pmap. This will also map the L2 for it */
1153 1.3.4.2 bouyer pmap_enter(pmap, 0x00000000, systempage.pv_pa,
1154 1.3.4.2 bouyer VM_PROT_READ, VM_PROT_READ | PMAP_WIRED);
1155 1.3.4.2 bouyer }
1156 1.3.4.2 bouyer
1157 1.3.4.2 bouyer
1158 1.3.4.2 bouyer void
1159 1.3.4.2 bouyer pmap_freepagedir(pmap)
1160 1.3.4.2 bouyer pmap_t pmap;
1161 1.3.4.2 bouyer {
1162 1.3.4.2 bouyer /* Free the memory used for the page table mapping */
1163 1.3.4.2 bouyer uvm_km_free(kernel_map, (vaddr_t)pmap->pm_vptpt, NBPG);
1164 1.3.4.2 bouyer
1165 1.3.4.2 bouyer /* junk the L1 page table */
1166 1.3.4.2 bouyer if (pmap->pm_l1pt->pt_flags & PTFLAG_STATIC) {
1167 1.3.4.2 bouyer /* Add the page table to the queue */
1168 1.3.4.2 bouyer SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pmap->pm_l1pt, pt_queue);
1169 1.3.4.2 bouyer ++l1pt_static_queue_count;
1170 1.3.4.2 bouyer /* Wake up any sleeping processes waiting for a l1 page table */
1171 1.3.4.2 bouyer wakeup((caddr_t)&pmap_pagedir_ident);
1172 1.3.4.2 bouyer } else if (l1pt_queue_count < 8) {
1173 1.3.4.2 bouyer /* Add the page table to the queue */
1174 1.3.4.2 bouyer SIMPLEQ_INSERT_TAIL(&l1pt_queue, pmap->pm_l1pt, pt_queue);
1175 1.3.4.2 bouyer ++l1pt_queue_count;
1176 1.3.4.2 bouyer /* Wake up any sleeping processes waiting for a l1 page table */
1177 1.3.4.2 bouyer wakeup((caddr_t)&pmap_pagedir_ident);
1178 1.3.4.2 bouyer } else
1179 1.3.4.2 bouyer pmap_free_l1pt(pmap->pm_l1pt);
1180 1.3.4.2 bouyer }
1181 1.3.4.2 bouyer
1182 1.3.4.2 bouyer
1183 1.3.4.2 bouyer /*
1184 1.3.4.2 bouyer * Retire the given physical map from service.
1185 1.3.4.2 bouyer * Should only be called if the map contains no valid mappings.
1186 1.3.4.2 bouyer */
1187 1.3.4.2 bouyer
1188 1.3.4.2 bouyer void
1189 1.3.4.2 bouyer pmap_destroy(pmap)
1190 1.3.4.2 bouyer pmap_t pmap;
1191 1.3.4.2 bouyer {
1192 1.3.4.2 bouyer int count;
1193 1.3.4.2 bouyer
1194 1.3.4.2 bouyer if (pmap == NULL)
1195 1.3.4.2 bouyer return;
1196 1.3.4.2 bouyer
1197 1.3.4.2 bouyer PDEBUG(0, printf("pmap_destroy(%p)\n", pmap));
1198 1.3.4.2 bouyer simple_lock(&pmap->pm_lock);
1199 1.3.4.2 bouyer count = --pmap->pm_count;
1200 1.3.4.2 bouyer simple_unlock(&pmap->pm_lock);
1201 1.3.4.2 bouyer if (count == 0) {
1202 1.3.4.2 bouyer pmap_release(pmap);
1203 1.3.4.2 bouyer free((caddr_t)pmap, M_VMPMAP);
1204 1.3.4.2 bouyer }
1205 1.3.4.2 bouyer }
1206 1.3.4.2 bouyer
1207 1.3.4.2 bouyer
1208 1.3.4.2 bouyer /*
1209 1.3.4.2 bouyer * Release any resources held by the given physical map.
1210 1.3.4.2 bouyer * Called when a pmap initialized by pmap_pinit is being released.
1211 1.3.4.2 bouyer * Should only be called if the map contains no valid mappings.
1212 1.3.4.2 bouyer */
1213 1.3.4.2 bouyer
1214 1.3.4.2 bouyer void
1215 1.3.4.2 bouyer pmap_release(pmap)
1216 1.3.4.2 bouyer pmap_t pmap;
1217 1.3.4.2 bouyer {
1218 1.3.4.2 bouyer struct vm_page *page;
1219 1.3.4.2 bouyer pt_entry_t *pte;
1220 1.3.4.2 bouyer int loop;
1221 1.3.4.2 bouyer
1222 1.3.4.2 bouyer PDEBUG(0, printf("pmap_release(%p)\n", pmap));
1223 1.3.4.2 bouyer
1224 1.3.4.2 bouyer #if 0
1225 1.3.4.2 bouyer if (pmap->pm_count != 1) /* XXX: needs sorting */
1226 1.3.4.2 bouyer panic("pmap_release count %d", pmap->pm_count);
1227 1.3.4.2 bouyer #endif
1228 1.3.4.2 bouyer
1229 1.3.4.2 bouyer /* Remove the zero page mapping */
1230 1.3.4.2 bouyer pmap_remove(pmap, 0x00000000, 0x00000000 + NBPG);
1231 1.3.4.2 bouyer
1232 1.3.4.2 bouyer /*
1233 1.3.4.2 bouyer * Free any page tables still mapped
1234 1.3.4.2 bouyer * This is only temporay until pmap_enter can count the number
1235 1.3.4.2 bouyer * of mappings made in a page table. Then pmap_remove() can
1236 1.3.4.2 bouyer * reduce the count and free the pagetable when the count
1237 1.3.4.2 bouyer * reaches zero.
1238 1.3.4.2 bouyer */
1239 1.3.4.2 bouyer for (loop = 0; loop < (((PD_SIZE - KERNEL_PD_SIZE) >> 4) - 1); ++loop) {
1240 1.3.4.2 bouyer pte = (pt_entry_t *)(pmap->pm_vptpt + loop * 4);
1241 1.3.4.2 bouyer if (*pte != 0) {
1242 1.3.4.2 bouyer PDEBUG(0, printf("%x: pte=%p:%08x\n", loop, pte, *pte));
1243 1.3.4.2 bouyer page = PHYS_TO_VM_PAGE(pmap_pte_pa(pte));
1244 1.3.4.2 bouyer if (page == NULL)
1245 1.3.4.2 bouyer panic("pmap_release: bad address for phys page");
1246 1.3.4.2 bouyer uvm_pagefree(page);
1247 1.3.4.2 bouyer }
1248 1.3.4.2 bouyer }
1249 1.3.4.2 bouyer /* Free the page dir */
1250 1.3.4.2 bouyer pmap_freepagedir(pmap);
1251 1.3.4.2 bouyer }
1252 1.3.4.2 bouyer
1253 1.3.4.2 bouyer
1254 1.3.4.2 bouyer /*
1255 1.3.4.2 bouyer * void pmap_reference(pmap_t pmap)
1256 1.3.4.2 bouyer *
1257 1.3.4.2 bouyer * Add a reference to the specified pmap.
1258 1.3.4.2 bouyer */
1259 1.3.4.2 bouyer
1260 1.3.4.2 bouyer void
1261 1.3.4.2 bouyer pmap_reference(pmap)
1262 1.3.4.2 bouyer pmap_t pmap;
1263 1.3.4.2 bouyer {
1264 1.3.4.2 bouyer if (pmap == NULL)
1265 1.3.4.2 bouyer return;
1266 1.3.4.2 bouyer
1267 1.3.4.2 bouyer simple_lock(&pmap->pm_lock);
1268 1.3.4.2 bouyer pmap->pm_count++;
1269 1.3.4.2 bouyer simple_unlock(&pmap->pm_lock);
1270 1.3.4.2 bouyer }
1271 1.3.4.2 bouyer
1272 1.3.4.2 bouyer /*
1273 1.3.4.2 bouyer * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
1274 1.3.4.2 bouyer *
1275 1.3.4.2 bouyer * Return the start and end addresses of the kernel's virtual space.
1276 1.3.4.2 bouyer * These values are setup in pmap_bootstrap and are updated as pages
1277 1.3.4.2 bouyer * are allocated.
1278 1.3.4.2 bouyer */
1279 1.3.4.2 bouyer
1280 1.3.4.2 bouyer void
1281 1.3.4.2 bouyer pmap_virtual_space(start, end)
1282 1.3.4.2 bouyer vaddr_t *start;
1283 1.3.4.2 bouyer vaddr_t *end;
1284 1.3.4.2 bouyer {
1285 1.3.4.2 bouyer *start = virtual_start;
1286 1.3.4.2 bouyer *end = virtual_end;
1287 1.3.4.2 bouyer }
1288 1.3.4.2 bouyer
1289 1.3.4.2 bouyer
1290 1.3.4.2 bouyer /*
1291 1.3.4.2 bouyer * Activate the address space for the specified process. If the process
1292 1.3.4.2 bouyer * is the current process, load the new MMU context.
1293 1.3.4.2 bouyer */
1294 1.3.4.2 bouyer void
1295 1.3.4.2 bouyer pmap_activate(p)
1296 1.3.4.2 bouyer struct proc *p;
1297 1.3.4.2 bouyer {
1298 1.3.4.2 bouyer pmap_t pmap = p->p_vmspace->vm_map.pmap;
1299 1.3.4.2 bouyer struct pcb *pcb = &p->p_addr->u_pcb;
1300 1.3.4.2 bouyer
1301 1.3.4.2 bouyer (void) pmap_extract(kernel_pmap, (vaddr_t)pmap->pm_pdir,
1302 1.3.4.2 bouyer (paddr_t *)&pcb->pcb_pagedir);
1303 1.3.4.2 bouyer
1304 1.3.4.2 bouyer PDEBUG(0, printf("pmap_activate: p=%p pmap=%p pcb=%p pdir=%p l1=%p\n",
1305 1.3.4.2 bouyer p, pmap, pcb, pmap->pm_pdir, pcb->pcb_pagedir));
1306 1.3.4.2 bouyer
1307 1.3.4.2 bouyer if (p == curproc) {
1308 1.3.4.2 bouyer PDEBUG(0, printf("pmap_activate: setting TTB\n"));
1309 1.3.4.2 bouyer setttb((u_int)pcb->pcb_pagedir);
1310 1.3.4.2 bouyer }
1311 1.3.4.2 bouyer #if 0
1312 1.3.4.2 bouyer pmap->pm_pdchanged = FALSE;
1313 1.3.4.2 bouyer #endif
1314 1.3.4.2 bouyer }
1315 1.3.4.2 bouyer
1316 1.3.4.2 bouyer
1317 1.3.4.2 bouyer /*
1318 1.3.4.2 bouyer * Deactivate the address space of the specified process.
1319 1.3.4.2 bouyer */
1320 1.3.4.2 bouyer void
1321 1.3.4.2 bouyer pmap_deactivate(p)
1322 1.3.4.2 bouyer struct proc *p;
1323 1.3.4.2 bouyer {
1324 1.3.4.2 bouyer }
1325 1.3.4.2 bouyer
1326 1.3.4.2 bouyer
1327 1.3.4.2 bouyer /*
1328 1.3.4.2 bouyer * pmap_clean_page()
1329 1.3.4.2 bouyer *
1330 1.3.4.2 bouyer * This is a local function used to work out the best strategy to clean
1331 1.3.4.2 bouyer * a single page referenced by its entry in the PV table. It's used by
1332 1.3.4.2 bouyer * pmap_copy_page, pmap_zero page and maybe some others later on.
1333 1.3.4.2 bouyer *
1334 1.3.4.2 bouyer * Its policy is effectively:
1335 1.3.4.2 bouyer * o If there are no mappings, we don't bother doing anything with the cache.
1336 1.3.4.2 bouyer * o If there is one mapping, we clean just that page.
1337 1.3.4.2 bouyer * o If there are multiple mappings, we clean the entire cache.
1338 1.3.4.2 bouyer *
1339 1.3.4.2 bouyer * So that some functions can be further optimised, it returns 0 if it didn't
1340 1.3.4.2 bouyer * clean the entire cache, or 1 if it did.
1341 1.3.4.2 bouyer *
1342 1.3.4.2 bouyer * XXX One bug in this routine is that if the pv_entry has a single page
1343 1.3.4.2 bouyer * mapped at 0x00000000 a whole cache clean will be performed rather than
1344 1.3.4.2 bouyer * just the 1 page. Since this should not occur in everyday use and if it does
1345 1.3.4.2 bouyer * it will just result in not the most efficient clean for the page.
1346 1.3.4.2 bouyer */
1347 1.3.4.2 bouyer static int
1348 1.3.4.2 bouyer pmap_clean_page(pv)
1349 1.3.4.2 bouyer struct pv_entry *pv;
1350 1.3.4.2 bouyer {
1351 1.3.4.2 bouyer int s;
1352 1.3.4.2 bouyer int cache_needs_cleaning = 0;
1353 1.3.4.2 bouyer vaddr_t page_to_clean = 0;
1354 1.3.4.2 bouyer
1355 1.3.4.2 bouyer /* Go to splvm() so we get exclusive lock for a mo */
1356 1.3.4.2 bouyer s = splvm();
1357 1.3.4.2 bouyer if (pv->pv_pmap) {
1358 1.3.4.2 bouyer cache_needs_cleaning = 1;
1359 1.3.4.2 bouyer if (!pv->pv_next)
1360 1.3.4.2 bouyer page_to_clean = pv->pv_va;
1361 1.3.4.2 bouyer }
1362 1.3.4.2 bouyer splx(s);
1363 1.3.4.2 bouyer
1364 1.3.4.2 bouyer /* Do cache ops outside the splvm. */
1365 1.3.4.2 bouyer if (page_to_clean)
1366 1.3.4.2 bouyer cpu_cache_purgeID_rng(page_to_clean, NBPG);
1367 1.3.4.2 bouyer else if (cache_needs_cleaning) {
1368 1.3.4.2 bouyer cpu_cache_purgeID();
1369 1.3.4.2 bouyer return (1);
1370 1.3.4.2 bouyer }
1371 1.3.4.2 bouyer return (0);
1372 1.3.4.2 bouyer }
1373 1.3.4.2 bouyer
1374 1.3.4.2 bouyer /*
1375 1.3.4.2 bouyer * pmap_find_pv()
1376 1.3.4.2 bouyer *
1377 1.3.4.2 bouyer * This is a local function that finds a PV entry for a given physical page.
1378 1.3.4.2 bouyer * This is a common op, and this function removes loads of ifdefs in the code.
1379 1.3.4.2 bouyer */
1380 1.3.4.2 bouyer static __inline struct pv_entry *
1381 1.3.4.2 bouyer pmap_find_pv(phys)
1382 1.3.4.2 bouyer paddr_t phys;
1383 1.3.4.2 bouyer {
1384 1.3.4.2 bouyer int bank, off;
1385 1.3.4.2 bouyer struct pv_entry *pv;
1386 1.3.4.2 bouyer
1387 1.3.4.2 bouyer #ifdef DIAGNOSTIC
1388 1.3.4.2 bouyer if (!pmap_initialized)
1389 1.3.4.2 bouyer panic("pmap_find_pv: !pmap_initialized");
1390 1.3.4.2 bouyer #endif
1391 1.3.4.2 bouyer
1392 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(phys), &off)) == -1)
1393 1.3.4.2 bouyer panic("pmap_find_pv: not a real page, phys=%lx\n", phys);
1394 1.3.4.2 bouyer pv = &vm_physmem[bank].pmseg.pvent[off];
1395 1.3.4.2 bouyer return (pv);
1396 1.3.4.2 bouyer }
1397 1.3.4.2 bouyer
1398 1.3.4.2 bouyer /*
1399 1.3.4.2 bouyer * pmap_zero_page()
1400 1.3.4.2 bouyer *
1401 1.3.4.2 bouyer * Zero a given physical page by mapping it at a page hook point.
1402 1.3.4.2 bouyer * In doing the zero page op, the page we zero is mapped cachable, as with
1403 1.3.4.2 bouyer * StrongARM accesses to non-cached pages are non-burst making writing
1404 1.3.4.2 bouyer * _any_ bulk data very slow.
1405 1.3.4.2 bouyer */
1406 1.3.4.2 bouyer void
1407 1.3.4.2 bouyer pmap_zero_page(phys)
1408 1.3.4.2 bouyer paddr_t phys;
1409 1.3.4.2 bouyer {
1410 1.3.4.2 bouyer struct pv_entry *pv;
1411 1.3.4.2 bouyer
1412 1.3.4.2 bouyer /* Get an entry for this page, and clean it it. */
1413 1.3.4.2 bouyer pv = pmap_find_pv(phys);
1414 1.3.4.2 bouyer pmap_clean_page(pv);
1415 1.3.4.2 bouyer
1416 1.3.4.2 bouyer /*
1417 1.3.4.2 bouyer * Hook in the page, zero it, and purge the cache for that
1418 1.3.4.2 bouyer * zeroed page. Invalidate the TLB as needed.
1419 1.3.4.2 bouyer */
1420 1.3.4.2 bouyer *page_hook0.pte = L2_PTE(phys & PG_FRAME, AP_KRW);
1421 1.3.4.2 bouyer cpu_tlb_flushD_SE(page_hook0.va);
1422 1.3.4.2 bouyer bzero_page(page_hook0.va);
1423 1.3.4.2 bouyer cpu_cache_purgeD_rng(page_hook0.va, NBPG);
1424 1.3.4.2 bouyer }
1425 1.3.4.2 bouyer
1426 1.3.4.2 bouyer /*
1427 1.3.4.2 bouyer * pmap_copy_page()
1428 1.3.4.2 bouyer *
1429 1.3.4.2 bouyer * Copy one physical page into another, by mapping the pages into
1430 1.3.4.2 bouyer * hook points. The same comment regarding cachability as in
1431 1.3.4.2 bouyer * pmap_zero_page also applies here.
1432 1.3.4.2 bouyer */
1433 1.3.4.2 bouyer void
1434 1.3.4.2 bouyer pmap_copy_page(src, dest)
1435 1.3.4.2 bouyer paddr_t src;
1436 1.3.4.2 bouyer paddr_t dest;
1437 1.3.4.2 bouyer {
1438 1.3.4.2 bouyer struct pv_entry *src_pv, *dest_pv;
1439 1.3.4.2 bouyer
1440 1.3.4.2 bouyer /* Get PV entries for the pages, and clean them if needed. */
1441 1.3.4.2 bouyer src_pv = pmap_find_pv(src);
1442 1.3.4.2 bouyer dest_pv = pmap_find_pv(dest);
1443 1.3.4.2 bouyer if (!pmap_clean_page(src_pv))
1444 1.3.4.2 bouyer pmap_clean_page(dest_pv);
1445 1.3.4.2 bouyer
1446 1.3.4.2 bouyer /*
1447 1.3.4.2 bouyer * Map the pages into the page hook points, copy them, and purge
1448 1.3.4.2 bouyer * the cache for the appropriate page. Invalidate the TLB
1449 1.3.4.2 bouyer * as required.
1450 1.3.4.2 bouyer */
1451 1.3.4.2 bouyer *page_hook0.pte = L2_PTE(src & PG_FRAME, AP_KRW);
1452 1.3.4.2 bouyer *page_hook1.pte = L2_PTE(dest & PG_FRAME, AP_KRW);
1453 1.3.4.2 bouyer cpu_tlb_flushD_SE(page_hook0.va);
1454 1.3.4.2 bouyer cpu_tlb_flushD_SE(page_hook1.va);
1455 1.3.4.2 bouyer bcopy_page(page_hook0.va, page_hook1.va);
1456 1.3.4.2 bouyer cpu_cache_purgeD_rng(page_hook0.va, NBPG);
1457 1.3.4.2 bouyer cpu_cache_purgeD_rng(page_hook1.va, NBPG);
1458 1.3.4.2 bouyer }
1459 1.3.4.2 bouyer
1460 1.3.4.2 bouyer /*
1461 1.3.4.2 bouyer * int pmap_next_phys_page(paddr_t *addr)
1462 1.3.4.2 bouyer *
1463 1.3.4.2 bouyer * Allocate another physical page returning true or false depending
1464 1.3.4.2 bouyer * on whether a page could be allocated.
1465 1.3.4.2 bouyer */
1466 1.3.4.2 bouyer
1467 1.3.4.2 bouyer paddr_t
1468 1.3.4.2 bouyer pmap_next_phys_page(addr)
1469 1.3.4.2 bouyer paddr_t addr;
1470 1.3.4.2 bouyer
1471 1.3.4.2 bouyer {
1472 1.3.4.2 bouyer int loop;
1473 1.3.4.2 bouyer
1474 1.3.4.2 bouyer if (addr < bootconfig.dram[0].address)
1475 1.3.4.2 bouyer return(bootconfig.dram[0].address);
1476 1.3.4.2 bouyer
1477 1.3.4.2 bouyer loop = 0;
1478 1.3.4.2 bouyer
1479 1.3.4.2 bouyer while (bootconfig.dram[loop].address != 0
1480 1.3.4.2 bouyer && addr > (bootconfig.dram[loop].address + bootconfig.dram[loop].pages * NBPG))
1481 1.3.4.2 bouyer ++loop;
1482 1.3.4.2 bouyer
1483 1.3.4.2 bouyer if (bootconfig.dram[loop].address == 0)
1484 1.3.4.2 bouyer return(0);
1485 1.3.4.2 bouyer
1486 1.3.4.2 bouyer addr += NBPG;
1487 1.3.4.2 bouyer
1488 1.3.4.2 bouyer if (addr >= (bootconfig.dram[loop].address + bootconfig.dram[loop].pages * NBPG)) {
1489 1.3.4.2 bouyer if (bootconfig.dram[loop + 1].address == 0)
1490 1.3.4.2 bouyer return(0);
1491 1.3.4.2 bouyer addr = bootconfig.dram[loop + 1].address;
1492 1.3.4.2 bouyer }
1493 1.3.4.2 bouyer
1494 1.3.4.2 bouyer return(addr);
1495 1.3.4.2 bouyer }
1496 1.3.4.2 bouyer
1497 1.3.4.2 bouyer #if 0
1498 1.3.4.2 bouyer void
1499 1.3.4.2 bouyer pmap_pte_addref(pmap, va)
1500 1.3.4.2 bouyer pmap_t pmap;
1501 1.3.4.2 bouyer vaddr_t va;
1502 1.3.4.2 bouyer {
1503 1.3.4.2 bouyer pd_entry_t *pde;
1504 1.3.4.2 bouyer paddr_t pa;
1505 1.3.4.2 bouyer struct vm_page *m;
1506 1.3.4.2 bouyer
1507 1.3.4.2 bouyer if (pmap == pmap_kernel())
1508 1.3.4.2 bouyer return;
1509 1.3.4.2 bouyer
1510 1.3.4.2 bouyer pde = pmap_pde(pmap, va & ~(3 << PDSHIFT));
1511 1.3.4.2 bouyer pa = pmap_pte_pa(pde);
1512 1.3.4.2 bouyer m = PHYS_TO_VM_PAGE(pa);
1513 1.3.4.2 bouyer ++m->wire_count;
1514 1.3.4.2 bouyer #ifdef MYCROFT_HACK
1515 1.3.4.2 bouyer printf("addref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
1516 1.3.4.2 bouyer pmap, va, pde, pa, m, m->wire_count);
1517 1.3.4.2 bouyer #endif
1518 1.3.4.2 bouyer }
1519 1.3.4.2 bouyer
1520 1.3.4.2 bouyer void
1521 1.3.4.2 bouyer pmap_pte_delref(pmap, va)
1522 1.3.4.2 bouyer pmap_t pmap;
1523 1.3.4.2 bouyer vaddr_t va;
1524 1.3.4.2 bouyer {
1525 1.3.4.2 bouyer pd_entry_t *pde;
1526 1.3.4.2 bouyer paddr_t pa;
1527 1.3.4.2 bouyer struct vm_page *m;
1528 1.3.4.2 bouyer
1529 1.3.4.2 bouyer if (pmap == pmap_kernel())
1530 1.3.4.2 bouyer return;
1531 1.3.4.2 bouyer
1532 1.3.4.2 bouyer pde = pmap_pde(pmap, va & ~(3 << PDSHIFT));
1533 1.3.4.2 bouyer pa = pmap_pte_pa(pde);
1534 1.3.4.2 bouyer m = PHYS_TO_VM_PAGE(pa);
1535 1.3.4.2 bouyer --m->wire_count;
1536 1.3.4.2 bouyer #ifdef MYCROFT_HACK
1537 1.3.4.2 bouyer printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
1538 1.3.4.2 bouyer pmap, va, pde, pa, m, m->wire_count);
1539 1.3.4.2 bouyer #endif
1540 1.3.4.2 bouyer if (m->wire_count == 0) {
1541 1.3.4.2 bouyer #ifdef MYCROFT_HACK
1542 1.3.4.2 bouyer printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p\n",
1543 1.3.4.2 bouyer pmap, va, pde, pa, m);
1544 1.3.4.2 bouyer #endif
1545 1.3.4.2 bouyer pmap_unmap_in_l1(pmap, va);
1546 1.3.4.2 bouyer uvm_pagefree(m);
1547 1.3.4.2 bouyer --pmap->pm_stats.resident_count;
1548 1.3.4.2 bouyer }
1549 1.3.4.2 bouyer }
1550 1.3.4.2 bouyer #else
1551 1.3.4.2 bouyer #define pmap_pte_addref(pmap, va)
1552 1.3.4.2 bouyer #define pmap_pte_delref(pmap, va)
1553 1.3.4.2 bouyer #endif
1554 1.3.4.2 bouyer
1555 1.3.4.2 bouyer /*
1556 1.3.4.2 bouyer * Since we have a virtually indexed cache, we may need to inhibit caching if
1557 1.3.4.2 bouyer * there is more than one mapping and at least one of them is writable.
1558 1.3.4.2 bouyer * Since we purge the cache on every context switch, we only need to check for
1559 1.3.4.2 bouyer * other mappings within the same pmap, or kernel_pmap.
1560 1.3.4.2 bouyer * This function is also called when a page is unmapped, to possibly reenable
1561 1.3.4.2 bouyer * caching on any remaining mappings.
1562 1.3.4.2 bouyer */
1563 1.3.4.2 bouyer void
1564 1.3.4.2 bouyer pmap_vac_me_harder(pmap, pv)
1565 1.3.4.2 bouyer pmap_t pmap;
1566 1.3.4.2 bouyer struct pv_entry *pv;
1567 1.3.4.2 bouyer {
1568 1.3.4.2 bouyer struct pv_entry *npv;
1569 1.3.4.2 bouyer pt_entry_t *pte;
1570 1.3.4.2 bouyer int entries = 0;
1571 1.3.4.2 bouyer int writeable = 0;
1572 1.3.4.2 bouyer
1573 1.3.4.2 bouyer if (pv->pv_pmap == NULL)
1574 1.3.4.2 bouyer return;
1575 1.3.4.2 bouyer
1576 1.3.4.2 bouyer /*
1577 1.3.4.2 bouyer * Count mappings and writable mappings in this pmap.
1578 1.3.4.2 bouyer * Keep a pointer to the first one.
1579 1.3.4.2 bouyer */
1580 1.3.4.2 bouyer for (npv = pv; npv; npv = npv->pv_next) {
1581 1.3.4.2 bouyer /* Count mappings in the same pmap */
1582 1.3.4.2 bouyer if (pmap == npv->pv_pmap) {
1583 1.3.4.2 bouyer if (entries++ == 0)
1584 1.3.4.2 bouyer pv = npv;
1585 1.3.4.2 bouyer /* Writeable mappings */
1586 1.3.4.2 bouyer if (npv->pv_flags & PT_Wr)
1587 1.3.4.2 bouyer ++writeable;
1588 1.3.4.2 bouyer }
1589 1.3.4.2 bouyer }
1590 1.3.4.2 bouyer
1591 1.3.4.2 bouyer /*
1592 1.3.4.2 bouyer * Enable or disable caching as necessary.
1593 1.3.4.2 bouyer * We do a quick check of the first PTE to avoid walking the list if
1594 1.3.4.2 bouyer * we're already in the right state.
1595 1.3.4.2 bouyer */
1596 1.3.4.2 bouyer if (entries > 1 && writeable) {
1597 1.3.4.2 bouyer pte = pmap_pte(pmap, pv->pv_va);
1598 1.3.4.2 bouyer if (~*pte & (PT_C | PT_B))
1599 1.3.4.2 bouyer return;
1600 1.3.4.2 bouyer *pte = *pte & ~(PT_C | PT_B);
1601 1.3.4.2 bouyer for (npv = pv->pv_next; npv; npv = npv->pv_next) {
1602 1.3.4.2 bouyer if (pmap == npv->pv_pmap) {
1603 1.3.4.2 bouyer pte = pmap_pte(pmap, npv->pv_va);
1604 1.3.4.2 bouyer *pte = *pte & ~(PT_C | PT_B);
1605 1.3.4.2 bouyer }
1606 1.3.4.2 bouyer }
1607 1.3.4.2 bouyer } else if (entries > 0) {
1608 1.3.4.2 bouyer pte = pmap_pte(pmap, pv->pv_va);
1609 1.3.4.2 bouyer if (*pte & (PT_C | PT_B))
1610 1.3.4.2 bouyer return;
1611 1.3.4.2 bouyer *pte = *pte | (PT_C | PT_B);
1612 1.3.4.2 bouyer for (npv = pv->pv_next; npv; npv = npv->pv_next) {
1613 1.3.4.2 bouyer if (pmap == npv->pv_pmap) {
1614 1.3.4.2 bouyer pte = pmap_pte(pmap, npv->pv_va);
1615 1.3.4.2 bouyer *pte = *pte | (PT_C | PT_B);
1616 1.3.4.2 bouyer }
1617 1.3.4.2 bouyer }
1618 1.3.4.2 bouyer }
1619 1.3.4.2 bouyer }
1620 1.3.4.2 bouyer
1621 1.3.4.2 bouyer /*
1622 1.3.4.2 bouyer * pmap_remove()
1623 1.3.4.2 bouyer *
1624 1.3.4.2 bouyer * pmap_remove is responsible for nuking a number of mappings for a range
1625 1.3.4.2 bouyer * of virtual address space in the current pmap. To do this efficiently
1626 1.3.4.2 bouyer * is interesting, because in a number of cases a wide virtual address
1627 1.3.4.2 bouyer * range may be supplied that contains few actual mappings. So, the
1628 1.3.4.2 bouyer * optimisations are:
1629 1.3.4.2 bouyer * 1. Try and skip over hunks of address space for which an L1 entry
1630 1.3.4.2 bouyer * does not exist.
1631 1.3.4.2 bouyer * 2. Build up a list of pages we've hit, up to a maximum, so we can
1632 1.3.4.2 bouyer * maybe do just a partial cache clean. This path of execution is
1633 1.3.4.2 bouyer * complicated by the fact that the cache must be flushed _before_
1634 1.3.4.2 bouyer * the PTE is nuked, being a VAC :-)
1635 1.3.4.2 bouyer * 3. Maybe later fast-case a single page, but I don't think this is
1636 1.3.4.2 bouyer * going to make _that_ much difference overall.
1637 1.3.4.2 bouyer */
1638 1.3.4.2 bouyer
1639 1.3.4.2 bouyer #define PMAP_REMOVE_CLEAN_LIST_SIZE 3
1640 1.3.4.2 bouyer
1641 1.3.4.2 bouyer void
1642 1.3.4.2 bouyer pmap_remove(pmap, sva, eva)
1643 1.3.4.2 bouyer pmap_t pmap;
1644 1.3.4.2 bouyer vaddr_t sva;
1645 1.3.4.2 bouyer vaddr_t eva;
1646 1.3.4.2 bouyer {
1647 1.3.4.2 bouyer int cleanlist_idx = 0;
1648 1.3.4.2 bouyer struct pagelist {
1649 1.3.4.2 bouyer vaddr_t va;
1650 1.3.4.2 bouyer pt_entry_t *pte;
1651 1.3.4.2 bouyer } cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
1652 1.3.4.2 bouyer pt_entry_t *pte = 0;
1653 1.3.4.2 bouyer paddr_t pa;
1654 1.3.4.2 bouyer int pmap_active;
1655 1.3.4.2 bouyer struct pv_entry *pv;
1656 1.3.4.2 bouyer
1657 1.3.4.2 bouyer /* Exit quick if there is no pmap */
1658 1.3.4.2 bouyer if (!pmap)
1659 1.3.4.2 bouyer return;
1660 1.3.4.2 bouyer
1661 1.3.4.2 bouyer PDEBUG(0, printf("pmap_remove: pmap=%p sva=%08lx eva=%08lx\n", pmap, sva, eva));
1662 1.3.4.2 bouyer
1663 1.3.4.2 bouyer sva &= PG_FRAME;
1664 1.3.4.2 bouyer eva &= PG_FRAME;
1665 1.3.4.2 bouyer
1666 1.3.4.2 bouyer /* Get a page table pointer */
1667 1.3.4.2 bouyer while (sva < eva) {
1668 1.3.4.2 bouyer pte = pmap_pte(pmap, sva);
1669 1.3.4.2 bouyer if (pte)
1670 1.3.4.2 bouyer break;
1671 1.3.4.2 bouyer sva = (sva & PD_MASK) + NBPD;
1672 1.3.4.2 bouyer }
1673 1.3.4.2 bouyer
1674 1.3.4.2 bouyer /* Note if the pmap is active thus require cache and tlb cleans */
1675 1.3.4.2 bouyer if ((curproc && curproc->p_vmspace->vm_map.pmap == pmap)
1676 1.3.4.2 bouyer || (pmap == kernel_pmap))
1677 1.3.4.2 bouyer pmap_active = 1;
1678 1.3.4.2 bouyer else
1679 1.3.4.2 bouyer pmap_active = 0;
1680 1.3.4.2 bouyer
1681 1.3.4.2 bouyer /* Now loop along */
1682 1.3.4.2 bouyer while (sva < eva) {
1683 1.3.4.2 bouyer /* Check if we can move to the next PDE (l1 chunk) */
1684 1.3.4.2 bouyer if (!(sva & PT_MASK))
1685 1.3.4.2 bouyer if (!pmap_pde_v(pmap_pde(pmap, sva))) {
1686 1.3.4.2 bouyer sva += NBPD;
1687 1.3.4.2 bouyer pte += arm_byte_to_page(NBPD);
1688 1.3.4.2 bouyer continue;
1689 1.3.4.2 bouyer }
1690 1.3.4.2 bouyer
1691 1.3.4.2 bouyer /* We've found a valid PTE, so this page of PTEs has to go. */
1692 1.3.4.2 bouyer if (pmap_pte_v(pte)) {
1693 1.3.4.2 bouyer int bank, off;
1694 1.3.4.2 bouyer
1695 1.3.4.2 bouyer /* Update statistics */
1696 1.3.4.2 bouyer --pmap->pm_stats.resident_count;
1697 1.3.4.2 bouyer
1698 1.3.4.2 bouyer /*
1699 1.3.4.2 bouyer * Add this page to our cache remove list, if we can.
1700 1.3.4.2 bouyer * If, however the cache remove list is totally full,
1701 1.3.4.2 bouyer * then do a complete cache invalidation taking note
1702 1.3.4.2 bouyer * to backtrack the PTE table beforehand, and ignore
1703 1.3.4.2 bouyer * the lists in future because there's no longer any
1704 1.3.4.2 bouyer * point in bothering with them (we've paid the
1705 1.3.4.2 bouyer * penalty, so will carry on unhindered). Otherwise,
1706 1.3.4.2 bouyer * when we fall out, we just clean the list.
1707 1.3.4.2 bouyer */
1708 1.3.4.2 bouyer PDEBUG(10, printf("remove: inv pte at %p(%x) ", pte, *pte));
1709 1.3.4.2 bouyer pa = pmap_pte_pa(pte);
1710 1.3.4.2 bouyer
1711 1.3.4.2 bouyer if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
1712 1.3.4.2 bouyer /* Add to the clean list. */
1713 1.3.4.2 bouyer cleanlist[cleanlist_idx].pte = pte;
1714 1.3.4.2 bouyer cleanlist[cleanlist_idx].va = sva;
1715 1.3.4.2 bouyer cleanlist_idx++;
1716 1.3.4.2 bouyer } else if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
1717 1.3.4.2 bouyer int cnt;
1718 1.3.4.2 bouyer
1719 1.3.4.2 bouyer /* Nuke everything if needed. */
1720 1.3.4.2 bouyer if (pmap_active) {
1721 1.3.4.2 bouyer cpu_cache_purgeID();
1722 1.3.4.2 bouyer cpu_tlb_flushID();
1723 1.3.4.2 bouyer }
1724 1.3.4.2 bouyer
1725 1.3.4.2 bouyer /*
1726 1.3.4.2 bouyer * Roll back the previous PTE list,
1727 1.3.4.2 bouyer * and zero out the current PTE.
1728 1.3.4.2 bouyer */
1729 1.3.4.2 bouyer for (cnt = 0; cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
1730 1.3.4.2 bouyer *cleanlist[cnt].pte = 0;
1731 1.3.4.2 bouyer pmap_pte_delref(pmap, cleanlist[cnt].va);
1732 1.3.4.2 bouyer }
1733 1.3.4.2 bouyer *pte = 0;
1734 1.3.4.2 bouyer pmap_pte_delref(pmap, sva);
1735 1.3.4.2 bouyer cleanlist_idx++;
1736 1.3.4.2 bouyer } else {
1737 1.3.4.2 bouyer /*
1738 1.3.4.2 bouyer * We've already nuked the cache and
1739 1.3.4.2 bouyer * TLB, so just carry on regardless,
1740 1.3.4.2 bouyer * and we won't need to do it again
1741 1.3.4.2 bouyer */
1742 1.3.4.2 bouyer *pte = 0;
1743 1.3.4.2 bouyer pmap_pte_delref(pmap, sva);
1744 1.3.4.2 bouyer }
1745 1.3.4.2 bouyer
1746 1.3.4.2 bouyer /*
1747 1.3.4.2 bouyer * Update flags. In a number of circumstances,
1748 1.3.4.2 bouyer * we could cluster a lot of these and do a
1749 1.3.4.2 bouyer * number of sequential pages in one go.
1750 1.3.4.2 bouyer */
1751 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
1752 1.3.4.2 bouyer pv = &vm_physmem[bank].pmseg.pvent[off];
1753 1.3.4.2 bouyer pmap_remove_pv(pmap, sva, pv);
1754 1.3.4.2 bouyer pmap_vac_me_harder(pmap, pv);
1755 1.3.4.2 bouyer }
1756 1.3.4.2 bouyer }
1757 1.3.4.2 bouyer sva += NBPG;
1758 1.3.4.2 bouyer pte++;
1759 1.3.4.2 bouyer }
1760 1.3.4.2 bouyer
1761 1.3.4.2 bouyer /*
1762 1.3.4.2 bouyer * Now, if we've fallen through down to here, chances are that there
1763 1.3.4.2 bouyer * are less than PMAP_REMOVE_CLEAN_LIST_SIZE mappings left.
1764 1.3.4.2 bouyer */
1765 1.3.4.2 bouyer if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
1766 1.3.4.2 bouyer u_int cnt;
1767 1.3.4.2 bouyer
1768 1.3.4.2 bouyer for (cnt = 0; cnt < cleanlist_idx; cnt++) {
1769 1.3.4.2 bouyer if (pmap_active) {
1770 1.3.4.2 bouyer cpu_cache_purgeID_rng(cleanlist[cnt].va, NBPG);
1771 1.3.4.2 bouyer *cleanlist[cnt].pte = 0;
1772 1.3.4.2 bouyer cpu_tlb_flushID_SE(cleanlist[cnt].va);
1773 1.3.4.2 bouyer } else
1774 1.3.4.2 bouyer *cleanlist[cnt].pte = 0;
1775 1.3.4.2 bouyer pmap_pte_delref(pmap, cleanlist[cnt].va);
1776 1.3.4.2 bouyer }
1777 1.3.4.2 bouyer }
1778 1.3.4.2 bouyer }
1779 1.3.4.2 bouyer
1780 1.3.4.2 bouyer /*
1781 1.3.4.2 bouyer * Routine: pmap_remove_all
1782 1.3.4.2 bouyer * Function:
1783 1.3.4.2 bouyer * Removes this physical page from
1784 1.3.4.2 bouyer * all physical maps in which it resides.
1785 1.3.4.2 bouyer * Reflects back modify bits to the pager.
1786 1.3.4.2 bouyer */
1787 1.3.4.2 bouyer
1788 1.3.4.2 bouyer void
1789 1.3.4.2 bouyer pmap_remove_all(pa)
1790 1.3.4.2 bouyer paddr_t pa;
1791 1.3.4.2 bouyer {
1792 1.3.4.2 bouyer struct pv_entry *ph, *pv, *npv;
1793 1.3.4.2 bouyer pmap_t pmap;
1794 1.3.4.2 bouyer pt_entry_t *pte;
1795 1.3.4.2 bouyer int s;
1796 1.3.4.2 bouyer
1797 1.3.4.2 bouyer PDEBUG(0, printf("pmap_remove_all: pa=%lx ", pa));
1798 1.3.4.2 bouyer
1799 1.3.4.2 bouyer pv = ph = pmap_find_pv(pa);
1800 1.3.4.2 bouyer pmap_clean_page(pv);
1801 1.3.4.2 bouyer
1802 1.3.4.2 bouyer s = splvm();
1803 1.3.4.2 bouyer
1804 1.3.4.2 bouyer if (ph->pv_pmap == NULL) {
1805 1.3.4.2 bouyer PDEBUG(0, printf("free page\n"));
1806 1.3.4.2 bouyer splx(s);
1807 1.3.4.2 bouyer return;
1808 1.3.4.2 bouyer }
1809 1.3.4.2 bouyer
1810 1.3.4.2 bouyer while (pv) {
1811 1.3.4.2 bouyer pmap = pv->pv_pmap;
1812 1.3.4.2 bouyer pte = pmap_pte(pmap, pv->pv_va);
1813 1.3.4.2 bouyer
1814 1.3.4.2 bouyer PDEBUG(0, printf("[%p,%08x,%08lx,%08x] ", pmap, *pte,
1815 1.3.4.2 bouyer pv->pv_va, pv->pv_flags));
1816 1.3.4.2 bouyer #ifdef DEBUG
1817 1.3.4.2 bouyer if (!pte || !pmap_pte_v(pte) || pmap_pte_pa(pte) != pa)
1818 1.3.4.2 bouyer panic("pmap_remove_all: bad mapping");
1819 1.3.4.2 bouyer #endif /* DEBUG */
1820 1.3.4.2 bouyer
1821 1.3.4.2 bouyer /*
1822 1.3.4.2 bouyer * Update statistics
1823 1.3.4.2 bouyer */
1824 1.3.4.2 bouyer --pmap->pm_stats.resident_count;
1825 1.3.4.2 bouyer
1826 1.3.4.2 bouyer /* Wired bit */
1827 1.3.4.2 bouyer if (pv->pv_flags & PT_W)
1828 1.3.4.2 bouyer --pmap->pm_stats.wired_count;
1829 1.3.4.2 bouyer
1830 1.3.4.2 bouyer /*
1831 1.3.4.2 bouyer * Invalidate the PTEs.
1832 1.3.4.2 bouyer * XXX: should cluster them up and invalidate as many
1833 1.3.4.2 bouyer * as possible at once.
1834 1.3.4.2 bouyer */
1835 1.3.4.2 bouyer
1836 1.3.4.2 bouyer #ifdef needednotdone
1837 1.3.4.2 bouyer reduce wiring count on page table pages as references drop
1838 1.3.4.2 bouyer #endif
1839 1.3.4.2 bouyer
1840 1.3.4.2 bouyer *pte = 0;
1841 1.3.4.2 bouyer pmap_pte_delref(pmap, pv->pv_va);
1842 1.3.4.2 bouyer
1843 1.3.4.2 bouyer npv = pv->pv_next;
1844 1.3.4.2 bouyer if (pv == ph)
1845 1.3.4.2 bouyer ph->pv_pmap = NULL;
1846 1.3.4.2 bouyer else
1847 1.3.4.2 bouyer pmap_free_pv(pv);
1848 1.3.4.2 bouyer pv = npv;
1849 1.3.4.2 bouyer }
1850 1.3.4.2 bouyer
1851 1.3.4.2 bouyer splx(s);
1852 1.3.4.2 bouyer
1853 1.3.4.2 bouyer PDEBUG(0, printf("done\n"));
1854 1.3.4.2 bouyer cpu_tlb_flushID();
1855 1.3.4.2 bouyer }
1856 1.3.4.2 bouyer
1857 1.3.4.2 bouyer
1858 1.3.4.2 bouyer /*
1859 1.3.4.2 bouyer * Set the physical protection on the specified range of this map as requested.
1860 1.3.4.2 bouyer */
1861 1.3.4.2 bouyer
1862 1.3.4.2 bouyer void
1863 1.3.4.2 bouyer pmap_protect(pmap, sva, eva, prot)
1864 1.3.4.2 bouyer pmap_t pmap;
1865 1.3.4.2 bouyer vaddr_t sva;
1866 1.3.4.2 bouyer vaddr_t eva;
1867 1.3.4.2 bouyer vm_prot_t prot;
1868 1.3.4.2 bouyer {
1869 1.3.4.2 bouyer pt_entry_t *pte = NULL;
1870 1.3.4.2 bouyer int armprot;
1871 1.3.4.2 bouyer int flush = 0;
1872 1.3.4.2 bouyer paddr_t pa;
1873 1.3.4.2 bouyer int bank, off;
1874 1.3.4.2 bouyer struct pv_entry *pv;
1875 1.3.4.2 bouyer
1876 1.3.4.2 bouyer /*
1877 1.3.4.2 bouyer * Make sure pmap is valid. -dct
1878 1.3.4.2 bouyer */
1879 1.3.4.2 bouyer if (pmap == NULL)
1880 1.3.4.2 bouyer return;
1881 1.3.4.2 bouyer PDEBUG(0, printf("pmap_protect: pmap=%p %08lx->%08lx %x\n",
1882 1.3.4.2 bouyer pmap, sva, eva, prot));
1883 1.3.4.2 bouyer
1884 1.3.4.2 bouyer if (~prot & VM_PROT_READ) {
1885 1.3.4.2 bouyer /* Just remove the mappings. */
1886 1.3.4.2 bouyer pmap_remove(pmap, sva, eva);
1887 1.3.4.2 bouyer return;
1888 1.3.4.2 bouyer }
1889 1.3.4.2 bouyer if (prot & VM_PROT_WRITE) {
1890 1.3.4.2 bouyer /*
1891 1.3.4.2 bouyer * If this is a read->write transition, just ignore it and let
1892 1.3.4.2 bouyer * uvm_fault() take care of it later.
1893 1.3.4.2 bouyer */
1894 1.3.4.2 bouyer return;
1895 1.3.4.2 bouyer }
1896 1.3.4.2 bouyer
1897 1.3.4.2 bouyer sva &= PG_FRAME;
1898 1.3.4.2 bouyer eva &= PG_FRAME;
1899 1.3.4.2 bouyer
1900 1.3.4.2 bouyer /*
1901 1.3.4.2 bouyer * We need to acquire a pointer to a page table page before entering
1902 1.3.4.2 bouyer * the following loop.
1903 1.3.4.2 bouyer */
1904 1.3.4.2 bouyer while (sva < eva) {
1905 1.3.4.2 bouyer pte = pmap_pte(pmap, sva);
1906 1.3.4.2 bouyer if (pte)
1907 1.3.4.2 bouyer break;
1908 1.3.4.2 bouyer sva = (sva & PD_MASK) + NBPD;
1909 1.3.4.2 bouyer }
1910 1.3.4.2 bouyer
1911 1.3.4.2 bouyer while (sva < eva) {
1912 1.3.4.2 bouyer /* only check once in a while */
1913 1.3.4.2 bouyer if ((sva & PT_MASK) == 0) {
1914 1.3.4.2 bouyer if (!pmap_pde_v(pmap_pde(pmap, sva))) {
1915 1.3.4.2 bouyer /* We can race ahead here, to the next pde. */
1916 1.3.4.2 bouyer sva += NBPD;
1917 1.3.4.2 bouyer pte += arm_byte_to_page(NBPD);
1918 1.3.4.2 bouyer continue;
1919 1.3.4.2 bouyer }
1920 1.3.4.2 bouyer }
1921 1.3.4.2 bouyer
1922 1.3.4.2 bouyer if (!pmap_pte_v(pte))
1923 1.3.4.2 bouyer goto next;
1924 1.3.4.2 bouyer
1925 1.3.4.2 bouyer flush = 1;
1926 1.3.4.2 bouyer
1927 1.3.4.2 bouyer armprot = 0;
1928 1.3.4.2 bouyer if (sva < VM_MAXUSER_ADDRESS)
1929 1.3.4.2 bouyer armprot |= PT_AP(AP_U);
1930 1.3.4.2 bouyer else if (sva < VM_MAX_ADDRESS)
1931 1.3.4.2 bouyer armprot |= PT_AP(AP_W); /* XXX Ekk what is this ? */
1932 1.3.4.2 bouyer *pte = (*pte & 0xfffff00f) | armprot;
1933 1.3.4.2 bouyer
1934 1.3.4.2 bouyer pa = pmap_pte_pa(pte);
1935 1.3.4.2 bouyer
1936 1.3.4.2 bouyer /* Get the physical page index */
1937 1.3.4.2 bouyer
1938 1.3.4.2 bouyer /* Clear write flag */
1939 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
1940 1.3.4.2 bouyer pv = &vm_physmem[bank].pmseg.pvent[off];
1941 1.3.4.2 bouyer (void) pmap_modify_pv(pmap, sva, pv, PT_Wr, 0);
1942 1.3.4.2 bouyer pmap_vac_me_harder(pmap, pv);
1943 1.3.4.2 bouyer }
1944 1.3.4.2 bouyer
1945 1.3.4.2 bouyer next:
1946 1.3.4.2 bouyer sva += NBPG;
1947 1.3.4.2 bouyer pte++;
1948 1.3.4.2 bouyer }
1949 1.3.4.2 bouyer
1950 1.3.4.2 bouyer if (flush)
1951 1.3.4.2 bouyer cpu_tlb_flushID();
1952 1.3.4.2 bouyer }
1953 1.3.4.2 bouyer
1954 1.3.4.2 bouyer /*
1955 1.3.4.2 bouyer * void pmap_enter(pmap_t pmap, vaddr_t va, paddr_t pa, vm_prot_t prot,
1956 1.3.4.2 bouyer * int flags)
1957 1.3.4.2 bouyer *
1958 1.3.4.2 bouyer * Insert the given physical page (p) at
1959 1.3.4.2 bouyer * the specified virtual address (v) in the
1960 1.3.4.2 bouyer * target physical map with the protection requested.
1961 1.3.4.2 bouyer *
1962 1.3.4.2 bouyer * If specified, the page will be wired down, meaning
1963 1.3.4.2 bouyer * that the related pte can not be reclaimed.
1964 1.3.4.2 bouyer *
1965 1.3.4.2 bouyer * NB: This is the only routine which MAY NOT lazy-evaluate
1966 1.3.4.2 bouyer * or lose information. That is, this routine must actually
1967 1.3.4.2 bouyer * insert this page into the given map NOW.
1968 1.3.4.2 bouyer */
1969 1.3.4.2 bouyer
1970 1.3.4.2 bouyer int
1971 1.3.4.2 bouyer pmap_enter(pmap, va, pa, prot, flags)
1972 1.3.4.2 bouyer pmap_t pmap;
1973 1.3.4.2 bouyer vaddr_t va;
1974 1.3.4.2 bouyer paddr_t pa;
1975 1.3.4.2 bouyer vm_prot_t prot;
1976 1.3.4.2 bouyer int flags;
1977 1.3.4.2 bouyer {
1978 1.3.4.2 bouyer pt_entry_t *pte;
1979 1.3.4.2 bouyer u_int npte;
1980 1.3.4.2 bouyer int bank, off;
1981 1.3.4.2 bouyer struct pv_entry *pv = NULL;
1982 1.3.4.2 bouyer paddr_t opa;
1983 1.3.4.2 bouyer int nflags;
1984 1.3.4.2 bouyer boolean_t wired = (flags & PMAP_WIRED) != 0;
1985 1.3.4.2 bouyer
1986 1.3.4.2 bouyer PDEBUG(5, printf("pmap_enter: V%08lx P%08lx in pmap %p prot=%08x, wired = %d\n",
1987 1.3.4.2 bouyer va, pa, pmap, prot, wired));
1988 1.3.4.2 bouyer
1989 1.3.4.2 bouyer #ifdef DIAGNOSTIC
1990 1.3.4.2 bouyer /* Valid address ? */
1991 1.3.4.2 bouyer if (va >= (KERNEL_VM_BASE + KERNEL_VM_SIZE))
1992 1.3.4.2 bouyer panic("pmap_enter: too big");
1993 1.3.4.2 bouyer if (pmap != pmap_kernel() && va != 0) {
1994 1.3.4.2 bouyer if (va < VM_MIN_ADDRESS || va >= VM_MAXUSER_ADDRESS)
1995 1.3.4.2 bouyer panic("pmap_enter: kernel page in user map");
1996 1.3.4.2 bouyer } else {
1997 1.3.4.2 bouyer if (va >= VM_MIN_ADDRESS && va < VM_MAXUSER_ADDRESS)
1998 1.3.4.2 bouyer panic("pmap_enter: user page in kernel map");
1999 1.3.4.2 bouyer if (va >= VM_MAXUSER_ADDRESS && va < VM_MAX_ADDRESS)
2000 1.3.4.2 bouyer panic("pmap_enter: entering PT page");
2001 1.3.4.2 bouyer }
2002 1.3.4.2 bouyer #endif
2003 1.3.4.2 bouyer
2004 1.3.4.2 bouyer /*
2005 1.3.4.2 bouyer * Get a pointer to the pte for this virtual address. If the
2006 1.3.4.2 bouyer * pte pointer is NULL then we are missing the L2 page table
2007 1.3.4.2 bouyer * so we need to create one.
2008 1.3.4.2 bouyer */
2009 1.3.4.2 bouyer pte = pmap_pte(pmap, va);
2010 1.3.4.2 bouyer if (!pte) {
2011 1.3.4.2 bouyer paddr_t l2pa;
2012 1.3.4.2 bouyer struct vm_page *m;
2013 1.3.4.2 bouyer
2014 1.3.4.2 bouyer /* Allocate a page table */
2015 1.3.4.2 bouyer for (;;) {
2016 1.3.4.2 bouyer m = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
2017 1.3.4.2 bouyer if (m != NULL)
2018 1.3.4.2 bouyer break;
2019 1.3.4.2 bouyer
2020 1.3.4.2 bouyer /*
2021 1.3.4.2 bouyer * No page available. If we're the kernel
2022 1.3.4.2 bouyer * pmap, we die, since we might not have
2023 1.3.4.2 bouyer * a valid thread context. For user pmaps,
2024 1.3.4.2 bouyer * we assume that we _do_ have a valid thread
2025 1.3.4.2 bouyer * context, so we wait here for the pagedaemon
2026 1.3.4.2 bouyer * to free up some pages.
2027 1.3.4.2 bouyer *
2028 1.3.4.2 bouyer * XXX THE VM CODE IS PROBABLY HOLDING LOCKS
2029 1.3.4.2 bouyer * XXX RIGHT NOW, BUT ONLY ON OUR PARENT VM_MAP
2030 1.3.4.2 bouyer * XXX SO THIS IS PROBABLY SAFE. In any case,
2031 1.3.4.2 bouyer * XXX other pmap modules claim it is safe to
2032 1.3.4.2 bouyer * XXX sleep here if it's a user pmap.
2033 1.3.4.2 bouyer */
2034 1.3.4.2 bouyer if (pmap == pmap_kernel())
2035 1.3.4.2 bouyer panic("pmap_enter: no free pages");
2036 1.3.4.2 bouyer else
2037 1.3.4.2 bouyer uvm_wait("pmap_enter");
2038 1.3.4.2 bouyer }
2039 1.3.4.2 bouyer
2040 1.3.4.2 bouyer /* Wire this page table into the L1. */
2041 1.3.4.2 bouyer l2pa = VM_PAGE_TO_PHYS(m);
2042 1.3.4.2 bouyer pmap_zero_page(l2pa);
2043 1.3.4.2 bouyer pmap_map_in_l1(pmap, va, l2pa);
2044 1.3.4.2 bouyer ++pmap->pm_stats.resident_count;
2045 1.3.4.2 bouyer
2046 1.3.4.2 bouyer pte = pmap_pte(pmap, va);
2047 1.3.4.2 bouyer #ifdef DIAGNOSTIC
2048 1.3.4.2 bouyer if (!pte)
2049 1.3.4.2 bouyer panic("pmap_enter: no pte");
2050 1.3.4.2 bouyer #endif
2051 1.3.4.2 bouyer }
2052 1.3.4.2 bouyer
2053 1.3.4.2 bouyer nflags = 0;
2054 1.3.4.2 bouyer if (prot & VM_PROT_WRITE)
2055 1.3.4.2 bouyer nflags |= PT_Wr;
2056 1.3.4.2 bouyer if (wired)
2057 1.3.4.2 bouyer nflags |= PT_W;
2058 1.3.4.2 bouyer
2059 1.3.4.2 bouyer /* More debugging info */
2060 1.3.4.2 bouyer PDEBUG(5, printf("pmap_enter: pte for V%08lx = V%p (%08x)\n", va, pte,
2061 1.3.4.2 bouyer *pte));
2062 1.3.4.2 bouyer
2063 1.3.4.2 bouyer /* Is the pte valid ? If so then this page is already mapped */
2064 1.3.4.2 bouyer if (pmap_pte_v(pte)) {
2065 1.3.4.2 bouyer /* Get the physical address of the current page mapped */
2066 1.3.4.2 bouyer opa = pmap_pte_pa(pte);
2067 1.3.4.2 bouyer
2068 1.3.4.2 bouyer #ifdef MYCROFT_HACK
2069 1.3.4.2 bouyer printf("pmap_enter: pmap=%p va=%lx pa=%lx opa=%lx\n", pmap, va, pa, opa);
2070 1.3.4.2 bouyer #endif
2071 1.3.4.2 bouyer
2072 1.3.4.2 bouyer /* Are we mapping the same page ? */
2073 1.3.4.2 bouyer if (opa == pa) {
2074 1.3.4.2 bouyer /* All we must be doing is changing the protection */
2075 1.3.4.2 bouyer PDEBUG(0, printf("Case 02 in pmap_enter (V%08lx P%08lx)\n",
2076 1.3.4.2 bouyer va, pa));
2077 1.3.4.2 bouyer
2078 1.3.4.2 bouyer /* Has the wiring changed ? */
2079 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
2080 1.3.4.2 bouyer pv = &vm_physmem[bank].pmseg.pvent[off];
2081 1.3.4.2 bouyer (void) pmap_modify_pv(pmap, va, pv,
2082 1.3.4.2 bouyer PT_Wr | PT_W, nflags);
2083 1.3.4.2 bouyer }
2084 1.3.4.2 bouyer } else {
2085 1.3.4.2 bouyer /* We are replacing the page with a new one. */
2086 1.3.4.2 bouyer cpu_cache_purgeID_rng(va, NBPG);
2087 1.3.4.2 bouyer
2088 1.3.4.2 bouyer PDEBUG(0, printf("Case 03 in pmap_enter (V%08lx P%08lx P%08lx)\n",
2089 1.3.4.2 bouyer va, pa, opa));
2090 1.3.4.2 bouyer
2091 1.3.4.2 bouyer /*
2092 1.3.4.2 bouyer * If it is part of our managed memory then we
2093 1.3.4.2 bouyer * must remove it from the PV list
2094 1.3.4.2 bouyer */
2095 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(opa), &off)) != -1) {
2096 1.3.4.2 bouyer pv = &vm_physmem[bank].pmseg.pvent[off];
2097 1.3.4.2 bouyer pmap_remove_pv(pmap, va, pv);
2098 1.3.4.2 bouyer }
2099 1.3.4.2 bouyer
2100 1.3.4.2 bouyer goto enter;
2101 1.3.4.2 bouyer }
2102 1.3.4.2 bouyer } else {
2103 1.3.4.2 bouyer opa = 0;
2104 1.3.4.2 bouyer pmap_pte_addref(pmap, va);
2105 1.3.4.2 bouyer
2106 1.3.4.2 bouyer /* pte is not valid so we must be hooking in a new page */
2107 1.3.4.2 bouyer ++pmap->pm_stats.resident_count;
2108 1.3.4.2 bouyer
2109 1.3.4.2 bouyer enter:
2110 1.3.4.2 bouyer /*
2111 1.3.4.2 bouyer * Enter on the PV list if part of our managed memory
2112 1.3.4.2 bouyer */
2113 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
2114 1.3.4.2 bouyer pv = &vm_physmem[bank].pmseg.pvent[off];
2115 1.3.4.2 bouyer pmap_enter_pv(pmap, va, pv, nflags);
2116 1.3.4.2 bouyer }
2117 1.3.4.2 bouyer }
2118 1.3.4.2 bouyer
2119 1.3.4.2 bouyer #ifdef MYCROFT_HACK
2120 1.3.4.2 bouyer if (mycroft_hack)
2121 1.3.4.2 bouyer printf("pmap_enter: pmap=%p va=%lx pa=%lx opa=%lx bank=%d off=%d pv=%p\n", pmap, va, pa, opa, bank, off, pv);
2122 1.3.4.2 bouyer #endif
2123 1.3.4.2 bouyer
2124 1.3.4.2 bouyer /* Construct the pte, giving the correct access. */
2125 1.3.4.2 bouyer npte = (pa & PG_FRAME);
2126 1.3.4.2 bouyer
2127 1.3.4.2 bouyer /* VA 0 is magic. */
2128 1.3.4.2 bouyer if (pmap != pmap_kernel() && va != 0)
2129 1.3.4.2 bouyer npte |= PT_AP(AP_U);
2130 1.3.4.2 bouyer
2131 1.3.4.2 bouyer if (bank != -1) {
2132 1.3.4.2 bouyer #ifdef DIAGNOSTIC
2133 1.3.4.2 bouyer if ((flags & VM_PROT_ALL) & ~prot)
2134 1.3.4.2 bouyer panic("pmap_enter: access_type exceeds prot");
2135 1.3.4.2 bouyer #endif
2136 1.3.4.2 bouyer npte |= PT_C | PT_B;
2137 1.3.4.2 bouyer if (flags & VM_PROT_WRITE) {
2138 1.3.4.2 bouyer npte |= L2_SPAGE | PT_AP(AP_W);
2139 1.3.4.2 bouyer vm_physmem[bank].pmseg.attrs[off] |= PT_H | PT_M;
2140 1.3.4.2 bouyer } else if (flags & VM_PROT_ALL) {
2141 1.3.4.2 bouyer npte |= L2_SPAGE;
2142 1.3.4.2 bouyer vm_physmem[bank].pmseg.attrs[off] |= PT_H;
2143 1.3.4.2 bouyer } else
2144 1.3.4.2 bouyer npte |= L2_INVAL;
2145 1.3.4.2 bouyer } else {
2146 1.3.4.2 bouyer if (prot & VM_PROT_WRITE)
2147 1.3.4.2 bouyer npte |= L2_SPAGE | PT_AP(AP_W);
2148 1.3.4.2 bouyer else if (prot & VM_PROT_ALL)
2149 1.3.4.2 bouyer npte |= L2_SPAGE;
2150 1.3.4.2 bouyer else
2151 1.3.4.2 bouyer npte |= L2_INVAL;
2152 1.3.4.2 bouyer }
2153 1.3.4.2 bouyer
2154 1.3.4.2 bouyer #ifdef MYCROFT_HACK
2155 1.3.4.2 bouyer if (mycroft_hack)
2156 1.3.4.2 bouyer printf("pmap_enter: pmap=%p va=%lx pa=%lx prot=%x wired=%d access_type=%x npte=%08x\n", pmap, va, pa, prot, wired, flags & VM_PROT_ALL, npte);
2157 1.3.4.2 bouyer #endif
2158 1.3.4.2 bouyer
2159 1.3.4.2 bouyer *pte = npte;
2160 1.3.4.2 bouyer
2161 1.3.4.2 bouyer if (bank != -1)
2162 1.3.4.2 bouyer pmap_vac_me_harder(pmap, pv);
2163 1.3.4.2 bouyer
2164 1.3.4.2 bouyer /* Better flush the TLB ... */
2165 1.3.4.2 bouyer cpu_tlb_flushID_SE(va);
2166 1.3.4.2 bouyer
2167 1.3.4.2 bouyer PDEBUG(5, printf("pmap_enter: pte = V%p %08x\n", pte, *pte));
2168 1.3.4.2 bouyer
2169 1.3.4.3 bouyer return 0;
2170 1.3.4.2 bouyer }
2171 1.3.4.2 bouyer
2172 1.3.4.2 bouyer void
2173 1.3.4.2 bouyer pmap_kenter_pa(va, pa, prot)
2174 1.3.4.2 bouyer vaddr_t va;
2175 1.3.4.2 bouyer paddr_t pa;
2176 1.3.4.2 bouyer vm_prot_t prot;
2177 1.3.4.2 bouyer {
2178 1.3.4.2 bouyer pmap_enter(pmap_kernel(), va, pa, prot, PMAP_WIRED);
2179 1.3.4.2 bouyer }
2180 1.3.4.2 bouyer
2181 1.3.4.2 bouyer void
2182 1.3.4.2 bouyer pmap_kenter_pgs(va, pgs, npgs)
2183 1.3.4.2 bouyer vaddr_t va;
2184 1.3.4.2 bouyer struct vm_page **pgs;
2185 1.3.4.2 bouyer int npgs;
2186 1.3.4.2 bouyer {
2187 1.3.4.2 bouyer int i;
2188 1.3.4.2 bouyer
2189 1.3.4.2 bouyer for (i = 0; i < npgs; i++, va += PAGE_SIZE) {
2190 1.3.4.2 bouyer pmap_enter(pmap_kernel(), va, VM_PAGE_TO_PHYS(pgs[i]),
2191 1.3.4.2 bouyer VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
2192 1.3.4.2 bouyer }
2193 1.3.4.2 bouyer }
2194 1.3.4.2 bouyer
2195 1.3.4.2 bouyer void
2196 1.3.4.2 bouyer pmap_kremove(va, len)
2197 1.3.4.2 bouyer vaddr_t va;
2198 1.3.4.2 bouyer vsize_t len;
2199 1.3.4.2 bouyer {
2200 1.3.4.2 bouyer for (len >>= PAGE_SHIFT; len > 0; len--, va += PAGE_SIZE) {
2201 1.3.4.2 bouyer pmap_remove(pmap_kernel(), va, va + PAGE_SIZE);
2202 1.3.4.2 bouyer }
2203 1.3.4.2 bouyer }
2204 1.3.4.2 bouyer
2205 1.3.4.2 bouyer /*
2206 1.3.4.2 bouyer * pmap_page_protect:
2207 1.3.4.2 bouyer *
2208 1.3.4.2 bouyer * Lower the permission for all mappings to a given page.
2209 1.3.4.2 bouyer */
2210 1.3.4.2 bouyer
2211 1.3.4.2 bouyer void
2212 1.3.4.2 bouyer pmap_page_protect(pg, prot)
2213 1.3.4.2 bouyer struct vm_page *pg;
2214 1.3.4.2 bouyer vm_prot_t prot;
2215 1.3.4.2 bouyer {
2216 1.3.4.2 bouyer paddr_t pa = VM_PAGE_TO_PHYS(pg);
2217 1.3.4.2 bouyer
2218 1.3.4.2 bouyer PDEBUG(0, printf("pmap_page_protect(pa=%lx, prot=%d)\n", pa, prot));
2219 1.3.4.2 bouyer
2220 1.3.4.2 bouyer switch(prot) {
2221 1.3.4.2 bouyer case VM_PROT_READ:
2222 1.3.4.2 bouyer case VM_PROT_READ|VM_PROT_EXECUTE:
2223 1.3.4.2 bouyer pmap_copy_on_write(pa);
2224 1.3.4.2 bouyer break;
2225 1.3.4.2 bouyer
2226 1.3.4.2 bouyer case VM_PROT_ALL:
2227 1.3.4.2 bouyer break;
2228 1.3.4.2 bouyer
2229 1.3.4.2 bouyer default:
2230 1.3.4.2 bouyer pmap_remove_all(pa);
2231 1.3.4.2 bouyer break;
2232 1.3.4.2 bouyer }
2233 1.3.4.2 bouyer }
2234 1.3.4.2 bouyer
2235 1.3.4.2 bouyer
2236 1.3.4.2 bouyer /*
2237 1.3.4.2 bouyer * Routine: pmap_unwire
2238 1.3.4.2 bouyer * Function: Clear the wired attribute for a map/virtual-address
2239 1.3.4.2 bouyer * pair.
2240 1.3.4.2 bouyer * In/out conditions:
2241 1.3.4.2 bouyer * The mapping must already exist in the pmap.
2242 1.3.4.2 bouyer */
2243 1.3.4.2 bouyer
2244 1.3.4.2 bouyer void
2245 1.3.4.2 bouyer pmap_unwire(pmap, va)
2246 1.3.4.2 bouyer pmap_t pmap;
2247 1.3.4.2 bouyer vaddr_t va;
2248 1.3.4.2 bouyer {
2249 1.3.4.2 bouyer pt_entry_t *pte;
2250 1.3.4.2 bouyer paddr_t pa;
2251 1.3.4.2 bouyer int bank, off;
2252 1.3.4.2 bouyer struct pv_entry *pv;
2253 1.3.4.2 bouyer
2254 1.3.4.2 bouyer /*
2255 1.3.4.2 bouyer * Make sure pmap is valid. -dct
2256 1.3.4.2 bouyer */
2257 1.3.4.2 bouyer if (pmap == NULL)
2258 1.3.4.2 bouyer return;
2259 1.3.4.2 bouyer
2260 1.3.4.2 bouyer /* Get the pte */
2261 1.3.4.2 bouyer pte = pmap_pte(pmap, va);
2262 1.3.4.2 bouyer if (!pte)
2263 1.3.4.2 bouyer return;
2264 1.3.4.2 bouyer
2265 1.3.4.2 bouyer /* Extract the physical address of the page */
2266 1.3.4.2 bouyer pa = pmap_pte_pa(pte);
2267 1.3.4.2 bouyer
2268 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
2269 1.3.4.2 bouyer return;
2270 1.3.4.2 bouyer pv = &vm_physmem[bank].pmseg.pvent[off];
2271 1.3.4.2 bouyer /* Update the wired bit in the pv entry for this page. */
2272 1.3.4.2 bouyer (void) pmap_modify_pv(pmap, va, pv, PT_W, 0);
2273 1.3.4.2 bouyer }
2274 1.3.4.2 bouyer
2275 1.3.4.2 bouyer /*
2276 1.3.4.2 bouyer * pt_entry_t *pmap_pte(pmap_t pmap, vaddr_t va)
2277 1.3.4.2 bouyer *
2278 1.3.4.2 bouyer * Return the pointer to a page table entry corresponding to the supplied
2279 1.3.4.2 bouyer * virtual address.
2280 1.3.4.2 bouyer *
2281 1.3.4.2 bouyer * The page directory is first checked to make sure that a page table
2282 1.3.4.2 bouyer * for the address in question exists and if it does a pointer to the
2283 1.3.4.2 bouyer * entry is returned.
2284 1.3.4.2 bouyer *
2285 1.3.4.2 bouyer * The way this works is that that the kernel page tables are mapped
2286 1.3.4.2 bouyer * into the memory map at ALT_PAGE_TBLS_BASE to ALT_PAGE_TBLS_BASE+4MB.
2287 1.3.4.2 bouyer * This allows page tables to be located quickly.
2288 1.3.4.2 bouyer */
2289 1.3.4.2 bouyer pt_entry_t *
2290 1.3.4.2 bouyer pmap_pte(pmap, va)
2291 1.3.4.2 bouyer pmap_t pmap;
2292 1.3.4.2 bouyer vaddr_t va;
2293 1.3.4.2 bouyer {
2294 1.3.4.2 bouyer pt_entry_t *ptp;
2295 1.3.4.2 bouyer pt_entry_t *result;
2296 1.3.4.2 bouyer
2297 1.3.4.2 bouyer /* The pmap must be valid */
2298 1.3.4.2 bouyer if (!pmap)
2299 1.3.4.2 bouyer return(NULL);
2300 1.3.4.2 bouyer
2301 1.3.4.2 bouyer /* Return the address of the pte */
2302 1.3.4.2 bouyer PDEBUG(10, printf("pmap_pte: pmap=%p va=V%08lx pde = V%p (%08X)\n",
2303 1.3.4.2 bouyer pmap, va, pmap_pde(pmap, va), *(pmap_pde(pmap, va))));
2304 1.3.4.2 bouyer
2305 1.3.4.2 bouyer /* Do we have a valid pde ? If not we don't have a page table */
2306 1.3.4.2 bouyer if (!pmap_pde_v(pmap_pde(pmap, va))) {
2307 1.3.4.2 bouyer PDEBUG(0, printf("pmap_pte: failed - pde = %p\n",
2308 1.3.4.2 bouyer pmap_pde(pmap, va)));
2309 1.3.4.2 bouyer return(NULL);
2310 1.3.4.2 bouyer }
2311 1.3.4.2 bouyer
2312 1.3.4.2 bouyer PDEBUG(10, printf("pmap pagetable = P%08lx current = P%08x\n",
2313 1.3.4.2 bouyer pmap->pm_pptpt, (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
2314 1.3.4.2 bouyer + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
2315 1.3.4.2 bouyer (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)));
2316 1.3.4.2 bouyer
2317 1.3.4.2 bouyer /*
2318 1.3.4.2 bouyer * If the pmap is the kernel pmap or the pmap is the active one
2319 1.3.4.2 bouyer * then we can just return a pointer to entry relative to
2320 1.3.4.2 bouyer * PROCESS_PAGE_TBLS_BASE.
2321 1.3.4.2 bouyer * Otherwise we need to map the page tables to an alternative
2322 1.3.4.2 bouyer * address and reference them there.
2323 1.3.4.2 bouyer */
2324 1.3.4.2 bouyer if (pmap == kernel_pmap || pmap->pm_pptpt
2325 1.3.4.2 bouyer == (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
2326 1.3.4.2 bouyer + ((PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) &
2327 1.3.4.2 bouyer ~3) + (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)) {
2328 1.3.4.2 bouyer ptp = (pt_entry_t *)PROCESS_PAGE_TBLS_BASE;
2329 1.3.4.2 bouyer } else {
2330 1.3.4.2 bouyer struct proc *p = curproc;
2331 1.3.4.2 bouyer
2332 1.3.4.2 bouyer /* If we don't have a valid curproc use proc0 */
2333 1.3.4.2 bouyer /* Perhaps we should just use kernel_pmap instead */
2334 1.3.4.2 bouyer if (p == NULL)
2335 1.3.4.2 bouyer p = &proc0;
2336 1.3.4.2 bouyer #ifdef DIAGNOSTIC
2337 1.3.4.2 bouyer /*
2338 1.3.4.2 bouyer * The pmap should always be valid for the process so
2339 1.3.4.2 bouyer * panic if it is not.
2340 1.3.4.2 bouyer */
2341 1.3.4.2 bouyer if (!p->p_vmspace || !p->p_vmspace->vm_map.pmap) {
2342 1.3.4.2 bouyer printf("pmap_pte: va=%08lx p=%p vm=%p\n",
2343 1.3.4.2 bouyer va, p, p->p_vmspace);
2344 1.3.4.2 bouyer console_debugger();
2345 1.3.4.2 bouyer }
2346 1.3.4.2 bouyer /*
2347 1.3.4.2 bouyer * The pmap for the current process should be mapped. If it
2348 1.3.4.2 bouyer * is not then we have a problem.
2349 1.3.4.2 bouyer */
2350 1.3.4.2 bouyer if (p->p_vmspace->vm_map.pmap->pm_pptpt !=
2351 1.3.4.2 bouyer (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
2352 1.3.4.2 bouyer + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
2353 1.3.4.2 bouyer (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)) {
2354 1.3.4.2 bouyer printf("pmap pagetable = P%08lx current = P%08x ",
2355 1.3.4.2 bouyer pmap->pm_pptpt, (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
2356 1.3.4.2 bouyer + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
2357 1.3.4.2 bouyer (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) &
2358 1.3.4.2 bouyer PG_FRAME));
2359 1.3.4.2 bouyer printf("pptpt=%lx\n", p->p_vmspace->vm_map.pmap->pm_pptpt);
2360 1.3.4.2 bouyer panic("pmap_pte: current and pmap mismatch\n");
2361 1.3.4.2 bouyer }
2362 1.3.4.2 bouyer #endif
2363 1.3.4.2 bouyer
2364 1.3.4.2 bouyer ptp = (pt_entry_t *)ALT_PAGE_TBLS_BASE;
2365 1.3.4.2 bouyer pmap_map_in_l1(p->p_vmspace->vm_map.pmap, ALT_PAGE_TBLS_BASE,
2366 1.3.4.2 bouyer pmap->pm_pptpt);
2367 1.3.4.2 bouyer cpu_tlb_flushD();
2368 1.3.4.2 bouyer }
2369 1.3.4.2 bouyer PDEBUG(10, printf("page tables base = %p offset=%lx\n", ptp,
2370 1.3.4.2 bouyer ((va >> (PGSHIFT-2)) & ~3)));
2371 1.3.4.2 bouyer result = (pt_entry_t *)((char *)ptp + ((va >> (PGSHIFT-2)) & ~3));
2372 1.3.4.2 bouyer return(result);
2373 1.3.4.2 bouyer }
2374 1.3.4.2 bouyer
2375 1.3.4.2 bouyer /*
2376 1.3.4.2 bouyer * Routine: pmap_extract
2377 1.3.4.2 bouyer * Function:
2378 1.3.4.2 bouyer * Extract the physical page address associated
2379 1.3.4.2 bouyer * with the given map/virtual_address pair.
2380 1.3.4.2 bouyer */
2381 1.3.4.2 bouyer boolean_t
2382 1.3.4.2 bouyer pmap_extract(pmap, va, pap)
2383 1.3.4.2 bouyer pmap_t pmap;
2384 1.3.4.2 bouyer vaddr_t va;
2385 1.3.4.2 bouyer paddr_t *pap;
2386 1.3.4.2 bouyer {
2387 1.3.4.2 bouyer pt_entry_t *pte;
2388 1.3.4.2 bouyer paddr_t pa;
2389 1.3.4.2 bouyer
2390 1.3.4.2 bouyer PDEBUG(5, printf("pmap_extract: pmap=%p, va=V%08lx\n", pmap, va));
2391 1.3.4.2 bouyer
2392 1.3.4.2 bouyer /*
2393 1.3.4.2 bouyer * Get the pte for this virtual address. If there is no pte
2394 1.3.4.2 bouyer * then there is no page table etc.
2395 1.3.4.2 bouyer */
2396 1.3.4.2 bouyer
2397 1.3.4.2 bouyer pte = pmap_pte(pmap, va);
2398 1.3.4.2 bouyer if (!pte)
2399 1.3.4.2 bouyer return(FALSE);
2400 1.3.4.2 bouyer
2401 1.3.4.2 bouyer /* Is the pte valid ? If not then no paged is actually mapped here */
2402 1.3.4.2 bouyer if (!pmap_pte_v(pte))
2403 1.3.4.2 bouyer return(FALSE);
2404 1.3.4.2 bouyer
2405 1.3.4.2 bouyer /* Return the physical address depending on the PTE type */
2406 1.3.4.2 bouyer /* XXX What about L1 section mappings ? */
2407 1.3.4.2 bouyer if ((*(pte) & L2_MASK) == L2_LPAGE) {
2408 1.3.4.2 bouyer /* Extract the physical address from the pte */
2409 1.3.4.2 bouyer pa = (*(pte)) & ~(L2_LPAGE_SIZE - 1);
2410 1.3.4.2 bouyer
2411 1.3.4.2 bouyer PDEBUG(5, printf("pmap_extract: LPAGE pa = P%08lx\n",
2412 1.3.4.2 bouyer (pa | (va & (L2_LPAGE_SIZE - 1)))));
2413 1.3.4.2 bouyer
2414 1.3.4.2 bouyer if (pap != NULL)
2415 1.3.4.2 bouyer *pap = pa | (va & (L2_LPAGE_SIZE - 1));
2416 1.3.4.2 bouyer return (TRUE);
2417 1.3.4.2 bouyer } else {
2418 1.3.4.2 bouyer /* Extract the physical address from the pte */
2419 1.3.4.2 bouyer pa = pmap_pte_pa(pte);
2420 1.3.4.2 bouyer
2421 1.3.4.2 bouyer PDEBUG(5, printf("pmap_extract: SPAGE pa = P%08lx\n",
2422 1.3.4.2 bouyer (pa | (va & ~PG_FRAME))));
2423 1.3.4.2 bouyer
2424 1.3.4.2 bouyer if (pap != NULL)
2425 1.3.4.2 bouyer *pap = pa | (va & ~PG_FRAME);
2426 1.3.4.2 bouyer return (TRUE);
2427 1.3.4.2 bouyer }
2428 1.3.4.2 bouyer }
2429 1.3.4.2 bouyer
2430 1.3.4.2 bouyer
2431 1.3.4.2 bouyer /*
2432 1.3.4.2 bouyer * Copy the range specified by src_addr/len from the source map to the
2433 1.3.4.2 bouyer * range dst_addr/len in the destination map.
2434 1.3.4.2 bouyer *
2435 1.3.4.2 bouyer * This routine is only advisory and need not do anything.
2436 1.3.4.2 bouyer */
2437 1.3.4.2 bouyer
2438 1.3.4.2 bouyer void
2439 1.3.4.2 bouyer pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr)
2440 1.3.4.2 bouyer pmap_t dst_pmap;
2441 1.3.4.2 bouyer pmap_t src_pmap;
2442 1.3.4.2 bouyer vaddr_t dst_addr;
2443 1.3.4.2 bouyer vsize_t len;
2444 1.3.4.2 bouyer vaddr_t src_addr;
2445 1.3.4.2 bouyer {
2446 1.3.4.2 bouyer PDEBUG(0, printf("pmap_copy(%p, %p, %lx, %lx, %lx)\n",
2447 1.3.4.2 bouyer dst_pmap, src_pmap, dst_addr, len, src_addr));
2448 1.3.4.2 bouyer }
2449 1.3.4.2 bouyer
2450 1.3.4.2 bouyer #if defined(PMAP_DEBUG)
2451 1.3.4.2 bouyer void
2452 1.3.4.2 bouyer pmap_dump_pvlist(phys, m)
2453 1.3.4.2 bouyer vaddr_t phys;
2454 1.3.4.2 bouyer char *m;
2455 1.3.4.2 bouyer {
2456 1.3.4.2 bouyer struct pv_entry *pv;
2457 1.3.4.2 bouyer int bank, off;
2458 1.3.4.2 bouyer
2459 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(phys), &off)) == -1) {
2460 1.3.4.2 bouyer printf("INVALID PA\n");
2461 1.3.4.2 bouyer return;
2462 1.3.4.2 bouyer }
2463 1.3.4.2 bouyer pv = &vm_physmem[bank].pmseg.pvent[off];
2464 1.3.4.2 bouyer printf("%s %08lx:", m, phys);
2465 1.3.4.2 bouyer if (pv->pv_pmap == NULL) {
2466 1.3.4.2 bouyer printf(" no mappings\n");
2467 1.3.4.2 bouyer return;
2468 1.3.4.2 bouyer }
2469 1.3.4.2 bouyer
2470 1.3.4.2 bouyer for (; pv; pv = pv->pv_next)
2471 1.3.4.2 bouyer printf(" pmap %p va %08lx flags %08x", pv->pv_pmap,
2472 1.3.4.2 bouyer pv->pv_va, pv->pv_flags);
2473 1.3.4.2 bouyer
2474 1.3.4.2 bouyer printf("\n");
2475 1.3.4.2 bouyer }
2476 1.3.4.2 bouyer
2477 1.3.4.2 bouyer #endif /* PMAP_DEBUG */
2478 1.3.4.2 bouyer
2479 1.3.4.2 bouyer boolean_t
2480 1.3.4.2 bouyer pmap_testbit(pa, setbits)
2481 1.3.4.2 bouyer paddr_t pa;
2482 1.3.4.2 bouyer int setbits;
2483 1.3.4.2 bouyer {
2484 1.3.4.2 bouyer int bank, off;
2485 1.3.4.2 bouyer
2486 1.3.4.2 bouyer PDEBUG(1, printf("pmap_testbit: pa=%08lx set=%08x\n", pa, setbits));
2487 1.3.4.2 bouyer
2488 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
2489 1.3.4.2 bouyer return(FALSE);
2490 1.3.4.2 bouyer
2491 1.3.4.2 bouyer /*
2492 1.3.4.2 bouyer * Check saved info only
2493 1.3.4.2 bouyer */
2494 1.3.4.2 bouyer if (vm_physmem[bank].pmseg.attrs[off] & setbits) {
2495 1.3.4.2 bouyer PDEBUG(0, printf("pmap_attributes = %02x\n",
2496 1.3.4.2 bouyer vm_physmem[bank].pmseg.attrs[off]));
2497 1.3.4.2 bouyer return(TRUE);
2498 1.3.4.2 bouyer }
2499 1.3.4.2 bouyer
2500 1.3.4.2 bouyer return(FALSE);
2501 1.3.4.2 bouyer }
2502 1.3.4.2 bouyer
2503 1.3.4.2 bouyer
2504 1.3.4.2 bouyer /*
2505 1.3.4.2 bouyer * Modify pte bits for all ptes corresponding to the given physical address.
2506 1.3.4.2 bouyer * We use `maskbits' rather than `clearbits' because we're always passing
2507 1.3.4.2 bouyer * constants and the latter would require an extra inversion at run-time.
2508 1.3.4.2 bouyer */
2509 1.3.4.2 bouyer
2510 1.3.4.2 bouyer void
2511 1.3.4.2 bouyer pmap_clearbit(pa, maskbits)
2512 1.3.4.2 bouyer paddr_t pa;
2513 1.3.4.2 bouyer int maskbits;
2514 1.3.4.2 bouyer {
2515 1.3.4.2 bouyer struct pv_entry *pv;
2516 1.3.4.2 bouyer pt_entry_t *pte;
2517 1.3.4.2 bouyer vaddr_t va;
2518 1.3.4.2 bouyer int bank, off;
2519 1.3.4.2 bouyer int s;
2520 1.3.4.2 bouyer
2521 1.3.4.2 bouyer PDEBUG(1, printf("pmap_clearbit: pa=%08lx mask=%08x\n",
2522 1.3.4.2 bouyer pa, maskbits));
2523 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
2524 1.3.4.2 bouyer return;
2525 1.3.4.2 bouyer pv = &vm_physmem[bank].pmseg.pvent[off];
2526 1.3.4.2 bouyer s = splvm();
2527 1.3.4.2 bouyer
2528 1.3.4.2 bouyer /*
2529 1.3.4.2 bouyer * Clear saved attributes (modify, reference)
2530 1.3.4.2 bouyer */
2531 1.3.4.2 bouyer vm_physmem[bank].pmseg.attrs[off] &= ~maskbits;
2532 1.3.4.2 bouyer
2533 1.3.4.2 bouyer if (pv->pv_pmap == NULL) {
2534 1.3.4.2 bouyer splx(s);
2535 1.3.4.2 bouyer return;
2536 1.3.4.2 bouyer }
2537 1.3.4.2 bouyer
2538 1.3.4.2 bouyer /*
2539 1.3.4.2 bouyer * Loop over all current mappings setting/clearing as appropos
2540 1.3.4.2 bouyer */
2541 1.3.4.2 bouyer for (; pv; pv = pv->pv_next) {
2542 1.3.4.2 bouyer va = pv->pv_va;
2543 1.3.4.2 bouyer
2544 1.3.4.2 bouyer /*
2545 1.3.4.2 bouyer * XXX don't write protect pager mappings
2546 1.3.4.2 bouyer */
2547 1.3.4.2 bouyer if (va >= uvm.pager_sva && va < uvm.pager_eva) {
2548 1.3.4.2 bouyer printf("pmap_clearbit: bogon alpha\n");
2549 1.3.4.2 bouyer continue;
2550 1.3.4.2 bouyer }
2551 1.3.4.2 bouyer
2552 1.3.4.2 bouyer pv->pv_flags &= ~maskbits;
2553 1.3.4.2 bouyer pte = pmap_pte(pv->pv_pmap, va);
2554 1.3.4.2 bouyer if (maskbits & (PT_Wr|PT_M))
2555 1.3.4.2 bouyer *pte = *pte & ~PT_AP(AP_W);
2556 1.3.4.2 bouyer if (maskbits & PT_H)
2557 1.3.4.2 bouyer *pte = (*pte & ~L2_MASK) | L2_INVAL;
2558 1.3.4.2 bouyer }
2559 1.3.4.2 bouyer cpu_tlb_flushID();
2560 1.3.4.2 bouyer
2561 1.3.4.2 bouyer splx(s);
2562 1.3.4.2 bouyer }
2563 1.3.4.2 bouyer
2564 1.3.4.2 bouyer
2565 1.3.4.2 bouyer boolean_t
2566 1.3.4.2 bouyer pmap_clear_modify(pg)
2567 1.3.4.2 bouyer struct vm_page *pg;
2568 1.3.4.2 bouyer {
2569 1.3.4.2 bouyer paddr_t pa = VM_PAGE_TO_PHYS(pg);
2570 1.3.4.2 bouyer boolean_t rv;
2571 1.3.4.2 bouyer
2572 1.3.4.2 bouyer PDEBUG(0, printf("pmap_clear_modify pa=%08lx\n", pa));
2573 1.3.4.2 bouyer rv = pmap_testbit(pa, PT_M);
2574 1.3.4.2 bouyer pmap_clearbit(pa, PT_M);
2575 1.3.4.2 bouyer return rv;
2576 1.3.4.2 bouyer }
2577 1.3.4.2 bouyer
2578 1.3.4.2 bouyer
2579 1.3.4.2 bouyer boolean_t
2580 1.3.4.2 bouyer pmap_clear_reference(pg)
2581 1.3.4.2 bouyer struct vm_page *pg;
2582 1.3.4.2 bouyer {
2583 1.3.4.2 bouyer paddr_t pa = VM_PAGE_TO_PHYS(pg);
2584 1.3.4.2 bouyer boolean_t rv;
2585 1.3.4.2 bouyer
2586 1.3.4.2 bouyer PDEBUG(0, printf("pmap_clear_reference pa=%08lx\n", pa));
2587 1.3.4.2 bouyer rv = pmap_testbit(pa, PT_H);
2588 1.3.4.2 bouyer pmap_clearbit(pa, PT_H);
2589 1.3.4.2 bouyer return rv;
2590 1.3.4.2 bouyer }
2591 1.3.4.2 bouyer
2592 1.3.4.2 bouyer
2593 1.3.4.2 bouyer void
2594 1.3.4.2 bouyer pmap_copy_on_write(pa)
2595 1.3.4.2 bouyer paddr_t pa;
2596 1.3.4.2 bouyer {
2597 1.3.4.2 bouyer PDEBUG(0, printf("pmap_copy_on_write pa=%08lx\n", pa));
2598 1.3.4.2 bouyer pmap_clearbit(pa, PT_Wr);
2599 1.3.4.2 bouyer }
2600 1.3.4.2 bouyer
2601 1.3.4.2 bouyer
2602 1.3.4.2 bouyer boolean_t
2603 1.3.4.2 bouyer pmap_is_modified(pg)
2604 1.3.4.2 bouyer struct vm_page *pg;
2605 1.3.4.2 bouyer {
2606 1.3.4.2 bouyer paddr_t pa = VM_PAGE_TO_PHYS(pg);
2607 1.3.4.2 bouyer boolean_t result;
2608 1.3.4.2 bouyer
2609 1.3.4.2 bouyer result = pmap_testbit(pa, PT_M);
2610 1.3.4.2 bouyer PDEBUG(0, printf("pmap_is_modified pa=%08lx %x\n", pa, result));
2611 1.3.4.2 bouyer return (result);
2612 1.3.4.2 bouyer }
2613 1.3.4.2 bouyer
2614 1.3.4.2 bouyer
2615 1.3.4.2 bouyer boolean_t
2616 1.3.4.2 bouyer pmap_is_referenced(pg)
2617 1.3.4.2 bouyer struct vm_page *pg;
2618 1.3.4.2 bouyer {
2619 1.3.4.2 bouyer paddr_t pa = VM_PAGE_TO_PHYS(pg);
2620 1.3.4.2 bouyer boolean_t result;
2621 1.3.4.2 bouyer
2622 1.3.4.2 bouyer result = pmap_testbit(pa, PT_H);
2623 1.3.4.2 bouyer PDEBUG(0, printf("pmap_is_referenced pa=%08lx %x\n", pa, result));
2624 1.3.4.2 bouyer return (result);
2625 1.3.4.2 bouyer }
2626 1.3.4.2 bouyer
2627 1.3.4.2 bouyer
2628 1.3.4.2 bouyer int
2629 1.3.4.2 bouyer pmap_modified_emulation(pmap, va)
2630 1.3.4.2 bouyer pmap_t pmap;
2631 1.3.4.2 bouyer vaddr_t va;
2632 1.3.4.2 bouyer {
2633 1.3.4.2 bouyer pt_entry_t *pte;
2634 1.3.4.2 bouyer paddr_t pa;
2635 1.3.4.2 bouyer int bank, off;
2636 1.3.4.2 bouyer struct pv_entry *pv;
2637 1.3.4.2 bouyer u_int flags;
2638 1.3.4.2 bouyer
2639 1.3.4.2 bouyer PDEBUG(2, printf("pmap_modified_emulation\n"));
2640 1.3.4.2 bouyer
2641 1.3.4.2 bouyer /* Get the pte */
2642 1.3.4.2 bouyer pte = pmap_pte(pmap, va);
2643 1.3.4.2 bouyer if (!pte) {
2644 1.3.4.2 bouyer PDEBUG(2, printf("no pte\n"));
2645 1.3.4.2 bouyer return(0);
2646 1.3.4.2 bouyer }
2647 1.3.4.2 bouyer
2648 1.3.4.2 bouyer PDEBUG(1, printf("*pte=%08x\n", *pte));
2649 1.3.4.2 bouyer
2650 1.3.4.2 bouyer /* Check for a zero pte */
2651 1.3.4.2 bouyer if (*pte == 0)
2652 1.3.4.2 bouyer return(0);
2653 1.3.4.2 bouyer
2654 1.3.4.2 bouyer /* This can happen if user code tries to access kernel memory. */
2655 1.3.4.2 bouyer if ((*pte & PT_AP(AP_W)) != 0)
2656 1.3.4.2 bouyer return (0);
2657 1.3.4.2 bouyer
2658 1.3.4.2 bouyer /* Extract the physical address of the page */
2659 1.3.4.2 bouyer pa = pmap_pte_pa(pte);
2660 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
2661 1.3.4.2 bouyer return(0);
2662 1.3.4.2 bouyer
2663 1.3.4.2 bouyer /* Get the current flags for this page. */
2664 1.3.4.2 bouyer pv = &vm_physmem[bank].pmseg.pvent[off];
2665 1.3.4.2 bouyer flags = pmap_modify_pv(pmap, va, pv, 0, 0);
2666 1.3.4.2 bouyer PDEBUG(2, printf("pmap_modified_emulation: flags = %08x\n", flags));
2667 1.3.4.2 bouyer
2668 1.3.4.2 bouyer /*
2669 1.3.4.2 bouyer * Do the flags say this page is writable ? If not then it is a
2670 1.3.4.2 bouyer * genuine write fault. If yes then the write fault is our fault
2671 1.3.4.2 bouyer * as we did not reflect the write access in the PTE. Now we know
2672 1.3.4.2 bouyer * a write has occurred we can correct this and also set the
2673 1.3.4.2 bouyer * modified bit
2674 1.3.4.2 bouyer */
2675 1.3.4.2 bouyer if (~flags & PT_Wr)
2676 1.3.4.2 bouyer return(0);
2677 1.3.4.2 bouyer
2678 1.3.4.2 bouyer PDEBUG(0, printf("pmap_modified_emulation: Got a hit va=%08lx, pte = %p (%08x)\n",
2679 1.3.4.2 bouyer va, pte, *pte));
2680 1.3.4.2 bouyer vm_physmem[bank].pmseg.attrs[off] |= PT_H | PT_M;
2681 1.3.4.2 bouyer *pte = (*pte & ~L2_MASK) | L2_SPAGE | PT_AP(AP_W);
2682 1.3.4.2 bouyer PDEBUG(0, printf("->(%08x)\n", *pte));
2683 1.3.4.2 bouyer
2684 1.3.4.2 bouyer /* Return, indicating the problem has been dealt with */
2685 1.3.4.2 bouyer cpu_tlb_flushID_SE(va);
2686 1.3.4.2 bouyer return(1);
2687 1.3.4.2 bouyer }
2688 1.3.4.2 bouyer
2689 1.3.4.2 bouyer
2690 1.3.4.2 bouyer int
2691 1.3.4.2 bouyer pmap_handled_emulation(pmap, va)
2692 1.3.4.2 bouyer pmap_t pmap;
2693 1.3.4.2 bouyer vaddr_t va;
2694 1.3.4.2 bouyer {
2695 1.3.4.2 bouyer pt_entry_t *pte;
2696 1.3.4.2 bouyer paddr_t pa;
2697 1.3.4.2 bouyer int bank, off;
2698 1.3.4.2 bouyer
2699 1.3.4.2 bouyer PDEBUG(2, printf("pmap_handled_emulation\n"));
2700 1.3.4.2 bouyer
2701 1.3.4.2 bouyer /* Get the pte */
2702 1.3.4.2 bouyer pte = pmap_pte(pmap, va);
2703 1.3.4.2 bouyer if (!pte) {
2704 1.3.4.2 bouyer PDEBUG(2, printf("no pte\n"));
2705 1.3.4.2 bouyer return(0);
2706 1.3.4.2 bouyer }
2707 1.3.4.2 bouyer
2708 1.3.4.2 bouyer PDEBUG(1, printf("*pte=%08x\n", *pte));
2709 1.3.4.2 bouyer
2710 1.3.4.2 bouyer /* Check for a zero pte */
2711 1.3.4.2 bouyer if (*pte == 0)
2712 1.3.4.2 bouyer return(0);
2713 1.3.4.2 bouyer
2714 1.3.4.2 bouyer /* This can happen if user code tries to access kernel memory. */
2715 1.3.4.2 bouyer if ((*pte & L2_MASK) != L2_INVAL)
2716 1.3.4.2 bouyer return (0);
2717 1.3.4.2 bouyer
2718 1.3.4.2 bouyer /* Extract the physical address of the page */
2719 1.3.4.2 bouyer pa = pmap_pte_pa(pte);
2720 1.3.4.2 bouyer if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
2721 1.3.4.2 bouyer return(0);
2722 1.3.4.2 bouyer
2723 1.3.4.2 bouyer /*
2724 1.3.4.2 bouyer * Ok we just enable the pte and mark the attibs as handled
2725 1.3.4.2 bouyer */
2726 1.3.4.2 bouyer PDEBUG(0, printf("pmap_handled_emulation: Got a hit va=%08lx pte = %p (%08x)\n",
2727 1.3.4.2 bouyer va, pte, *pte));
2728 1.3.4.2 bouyer vm_physmem[bank].pmseg.attrs[off] |= PT_H;
2729 1.3.4.2 bouyer *pte = (*pte & ~L2_MASK) | L2_SPAGE;
2730 1.3.4.2 bouyer PDEBUG(0, printf("->(%08x)\n", *pte));
2731 1.3.4.2 bouyer
2732 1.3.4.2 bouyer /* Return, indicating the problem has been dealt with */
2733 1.3.4.2 bouyer cpu_tlb_flushID_SE(va);
2734 1.3.4.2 bouyer return(1);
2735 1.3.4.2 bouyer }
2736 1.3.4.2 bouyer
2737 1.3.4.2 bouyer /*
2738 1.3.4.2 bouyer * pmap_collect: free resources held by a pmap
2739 1.3.4.2 bouyer *
2740 1.3.4.2 bouyer * => optional function.
2741 1.3.4.2 bouyer * => called when a process is swapped out to free memory.
2742 1.3.4.2 bouyer */
2743 1.3.4.2 bouyer
2744 1.3.4.2 bouyer void
2745 1.3.4.2 bouyer pmap_collect(pmap)
2746 1.3.4.2 bouyer pmap_t pmap;
2747 1.3.4.2 bouyer {
2748 1.3.4.2 bouyer }
2749 1.3.4.2 bouyer
2750 1.3.4.2 bouyer /*
2751 1.3.4.2 bouyer * Routine: pmap_procwr
2752 1.3.4.2 bouyer *
2753 1.3.4.2 bouyer * Function:
2754 1.3.4.2 bouyer * Synchronize caches corresponding to [addr, addr+len) in p.
2755 1.3.4.2 bouyer *
2756 1.3.4.2 bouyer */
2757 1.3.4.2 bouyer void
2758 1.3.4.2 bouyer pmap_procwr(p, va, len)
2759 1.3.4.2 bouyer struct proc *p;
2760 1.3.4.2 bouyer vaddr_t va;
2761 1.3.4.2 bouyer int len;
2762 1.3.4.2 bouyer {
2763 1.3.4.2 bouyer /* We only need to do anything if it is the current process. */
2764 1.3.4.2 bouyer if (p == curproc)
2765 1.3.4.2 bouyer cpu_cache_syncI_rng(va, len);
2766 1.3.4.2 bouyer }
2767 1.3.4.2 bouyer
2768 1.3.4.2 bouyer /* End of pmap.c */
2769