pmap.c revision 1.21 1 /* $NetBSD: pmap.c,v 1.21 2001/09/13 22:45:23 chris Exp $ */
2
3 /*
4 * Copyright (c) 2001 Richard Earnshaw
5 * Copyright (c) 2001 Christopher Gilbert
6 * All rights reserved.
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the company nor the name of the author may be used to
14 * endorse or promote products derived from this software without specific
15 * prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*-
31 * Copyright (c) 1999 The NetBSD Foundation, Inc.
32 * All rights reserved.
33 *
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Charles M. Hannum.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the NetBSD
48 * Foundation, Inc. and its contributors.
49 * 4. Neither the name of The NetBSD Foundation nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
64 */
65
66 /*
67 * Copyright (c) 1994-1998 Mark Brinicombe.
68 * Copyright (c) 1994 Brini.
69 * All rights reserved.
70 *
71 * This code is derived from software written for Brini by Mark Brinicombe
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. All advertising materials mentioning features or use of this software
82 * must display the following acknowledgement:
83 * This product includes software developed by Mark Brinicombe.
84 * 4. The name of the author may not be used to endorse or promote products
85 * derived from this software without specific prior written permission.
86 *
87 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
88 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
89 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
90 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
91 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
92 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
93 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
94 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
95 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
96 *
97 * RiscBSD kernel project
98 *
99 * pmap.c
100 *
101 * Machine dependant vm stuff
102 *
103 * Created : 20/09/94
104 */
105
106 /*
107 * Performance improvements, UVM changes, overhauls and part-rewrites
108 * were contributed by Neil A. Carson <neil (at) causality.com>.
109 */
110
111 /*
112 * The dram block info is currently referenced from the bootconfig.
113 * This should be placed in a separate structure.
114 */
115
116 /*
117 * Special compilation symbols
118 * PMAP_DEBUG - Build in pmap_debug_level code
119 */
120
121 /* Include header files */
122
123 #include "opt_pmap_debug.h"
124 #include "opt_ddb.h"
125
126 #include <sys/types.h>
127 #include <sys/param.h>
128 #include <sys/kernel.h>
129 #include <sys/systm.h>
130 #include <sys/proc.h>
131 #include <sys/malloc.h>
132 #include <sys/user.h>
133 #include <sys/pool.h>
134 #include <sys/cdefs.h>
135
136 #include <uvm/uvm.h>
137
138 #include <machine/bootconfig.h>
139 #include <machine/bus.h>
140 #include <machine/pmap.h>
141 #include <machine/pcb.h>
142 #include <machine/param.h>
143 #include <machine/katelib.h>
144
145 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.21 2001/09/13 22:45:23 chris Exp $");
146 #ifdef PMAP_DEBUG
147 #define PDEBUG(_lev_,_stat_) \
148 if (pmap_debug_level >= (_lev_)) \
149 ((_stat_))
150 int pmap_debug_level = -2;
151
152 /*
153 * for switching to potentially finer grained debugging
154 */
155 #define PDB_FOLLOW 0x0001
156 #define PDB_INIT 0x0002
157 #define PDB_ENTER 0x0004
158 #define PDB_REMOVE 0x0008
159 #define PDB_CREATE 0x0010
160 #define PDB_PTPAGE 0x0020
161 #define PDB_ASN 0x0040
162 #define PDB_BITS 0x0080
163 #define PDB_COLLECT 0x0100
164 #define PDB_PROTECT 0x0200
165 #define PDB_BOOTSTRAP 0x1000
166 #define PDB_PARANOIA 0x2000
167 #define PDB_WIRING 0x4000
168 #define PDB_PVDUMP 0x8000
169
170 int debugmap = 0;
171 int pmapdebug = PDB_PARANOIA | PDB_FOLLOW;
172 #define NPDEBUG(_lev_,_stat_) \
173 if (pmapdebug & (_lev_)) \
174 ((_stat_))
175
176 #else /* PMAP_DEBUG */
177 #define PDEBUG(_lev_,_stat_) /* Nothing */
178 #define PDEBUG(_lev_,_stat_) /* Nothing */
179 #endif /* PMAP_DEBUG */
180
181 struct pmap kernel_pmap_store;
182
183 /*
184 * pool that pmap structures are allocated from
185 */
186
187 struct pool pmap_pmap_pool;
188
189 pagehook_t page_hook0;
190 pagehook_t page_hook1;
191 char *memhook;
192 pt_entry_t msgbufpte;
193 extern caddr_t msgbufaddr;
194
195 boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
196 /*
197 * locking data structures
198 */
199
200 static struct lock pmap_main_lock;
201 static struct simplelock pvalloc_lock;
202 #ifdef LOCKDEBUG
203 #define PMAP_MAP_TO_HEAD_LOCK() \
204 (void) spinlockmgr(&pmap_main_lock, LK_SHARED, NULL)
205 #define PMAP_MAP_TO_HEAD_UNLOCK() \
206 (void) spinlockmgr(&pmap_main_lock, LK_RELEASE, NULL)
207
208 #define PMAP_HEAD_TO_MAP_LOCK() \
209 (void) spinlockmgr(&pmap_main_lock, LK_EXCLUSIVE, NULL)
210 #define PMAP_HEAD_TO_MAP_UNLOCK() \
211 (void) spinlockmgr(&pmap_main_lock, LK_RELEASE, NULL)
212 #else
213 #define PMAP_MAP_TO_HEAD_LOCK() /* nothing */
214 #define PMAP_MAP_TO_HEAD_UNLOCK() /* nothing */
215 #define PMAP_HEAD_TO_MAP_LOCK() /* nothing */
216 #define PMAP_HEAD_TO_MAP_UNLOCK() /* nothing */
217 #endif /* LOCKDEBUG */
218
219 /*
220 * pv_page management structures: locked by pvalloc_lock
221 */
222
223 TAILQ_HEAD(pv_pagelist, pv_page);
224 static struct pv_pagelist pv_freepages; /* list of pv_pages with free entrys */
225 static struct pv_pagelist pv_unusedpgs; /* list of unused pv_pages */
226 static int pv_nfpvents; /* # of free pv entries */
227 static struct pv_page *pv_initpage; /* bootstrap page from kernel_map */
228 static vaddr_t pv_cachedva; /* cached VA for later use */
229
230 #define PVE_LOWAT (PVE_PER_PVPAGE / 2) /* free pv_entry low water mark */
231 #define PVE_HIWAT (PVE_LOWAT + (PVE_PER_PVPAGE * 2))
232 /* high water mark */
233
234 /*
235 * local prototypes
236 */
237
238 static struct pv_entry *pmap_add_pvpage __P((struct pv_page *, boolean_t));
239 static struct pv_entry *pmap_alloc_pv __P((struct pmap *, int)); /* see codes below */
240 #define ALLOCPV_NEED 0 /* need PV now */
241 #define ALLOCPV_TRY 1 /* just try to allocate, don't steal */
242 #define ALLOCPV_NONEED 2 /* don't need PV, just growing cache */
243 static struct pv_entry *pmap_alloc_pvpage __P((struct pmap *, int));
244 static void pmap_enter_pv __P((struct pv_head *,
245 struct pv_entry *, struct pmap *,
246 vaddr_t, struct vm_page *, int));
247 static void pmap_free_pv __P((struct pmap *, struct pv_entry *));
248 static void pmap_free_pvs __P((struct pmap *, struct pv_entry *));
249 static void pmap_free_pv_doit __P((struct pv_entry *));
250 static void pmap_free_pvpage __P((void));
251 static boolean_t pmap_is_curpmap __P((struct pmap *));
252 static struct pv_entry *pmap_remove_pv __P((struct pv_head *, struct pmap *,
253 vaddr_t));
254 #define PMAP_REMOVE_ALL 0 /* remove all mappings */
255 #define PMAP_REMOVE_SKIPWIRED 1 /* skip wired mappings */
256
257 vsize_t npages;
258
259 static struct vm_page *pmap_alloc_ptp __P((struct pmap *, vaddr_t, boolean_t));
260 static struct vm_page *pmap_get_ptp __P((struct pmap *, vaddr_t, boolean_t));
261
262 extern paddr_t physical_start;
263 extern paddr_t physical_freestart;
264 extern paddr_t physical_end;
265 extern paddr_t physical_freeend;
266 extern unsigned int free_pages;
267 extern int max_processes;
268
269 vaddr_t virtual_start;
270 vaddr_t virtual_end;
271
272 vaddr_t avail_start;
273 vaddr_t avail_end;
274
275 extern pv_addr_t systempage;
276
277 #define ALLOC_PAGE_HOOK(x, s) \
278 x.va = virtual_start; \
279 x.pte = (pt_entry_t *)pmap_pte(pmap_kernel(), virtual_start); \
280 virtual_start += s;
281
282 /* Variables used by the L1 page table queue code */
283 SIMPLEQ_HEAD(l1pt_queue, l1pt);
284 struct l1pt_queue l1pt_static_queue; /* head of our static l1 queue */
285 int l1pt_static_queue_count; /* items in the static l1 queue */
286 int l1pt_static_create_count; /* static l1 items created */
287 struct l1pt_queue l1pt_queue; /* head of our l1 queue */
288 int l1pt_queue_count; /* items in the l1 queue */
289 int l1pt_create_count; /* stat - L1's create count */
290 int l1pt_reuse_count; /* stat - L1's reused count */
291
292 /* Local function prototypes (not used outside this file) */
293 pt_entry_t *pmap_pte __P((struct pmap *pmap, vaddr_t va));
294 void map_pagetable __P((vaddr_t pagetable, vaddr_t va,
295 paddr_t pa, unsigned int flags));
296 void pmap_copy_on_write __P((paddr_t pa));
297 void pmap_pinit __P((struct pmap *));
298 void pmap_freepagedir __P((struct pmap *));
299
300 /* Other function prototypes */
301 extern void bzero_page __P((vaddr_t));
302 extern void bcopy_page __P((vaddr_t, vaddr_t));
303
304 struct l1pt *pmap_alloc_l1pt __P((void));
305 static __inline void pmap_map_in_l1 __P((struct pmap *pmap, vaddr_t va,
306 vaddr_t l2pa, boolean_t));
307
308 static pt_entry_t *pmap_map_ptes __P((struct pmap *));
309 static void pmap_unmap_ptes __P((struct pmap *));
310
311 void pmap_vac_me_harder __P((struct pmap *, struct pv_head *,
312 pt_entry_t *, boolean_t));
313
314 /*
315 * real definition of pv_entry.
316 */
317
318 struct pv_entry {
319 struct pv_entry *pv_next; /* next pv_entry */
320 struct pmap *pv_pmap; /* pmap where mapping lies */
321 vaddr_t pv_va; /* virtual address for mapping */
322 int pv_flags; /* flags */
323 struct vm_page *pv_ptp; /* vm_page for the ptp */
324 };
325
326 /*
327 * pv_entrys are dynamically allocated in chunks from a single page.
328 * we keep track of how many pv_entrys are in use for each page and
329 * we can free pv_entry pages if needed. there is one lock for the
330 * entire allocation system.
331 */
332
333 struct pv_page_info {
334 TAILQ_ENTRY(pv_page) pvpi_list;
335 struct pv_entry *pvpi_pvfree;
336 int pvpi_nfree;
337 };
338
339 /*
340 * number of pv_entry's in a pv_page
341 * (note: won't work on systems where NPBG isn't a constant)
342 */
343
344 #define PVE_PER_PVPAGE ((NBPG - sizeof(struct pv_page_info)) / \
345 sizeof(struct pv_entry))
346
347 /*
348 * a pv_page: where pv_entrys are allocated from
349 */
350
351 struct pv_page {
352 struct pv_page_info pvinfo;
353 struct pv_entry pvents[PVE_PER_PVPAGE];
354 };
355
356 #ifdef MYCROFT_HACK
357 int mycroft_hack = 0;
358 #endif
359
360 /* Function to set the debug level of the pmap code */
361
362 #ifdef PMAP_DEBUG
363 void
364 pmap_debug(level)
365 int level;
366 {
367 pmap_debug_level = level;
368 printf("pmap_debug: level=%d\n", pmap_debug_level);
369 }
370 #endif /* PMAP_DEBUG */
371
372 __inline boolean_t
373 pmap_is_curpmap(struct pmap *pmap)
374 {
375 if ((curproc && curproc->p_vmspace->vm_map.pmap == pmap)
376 || (pmap == pmap_kernel()))
377 return (TRUE);
378 return (FALSE);
379 }
380 #include "isadma.h"
381
382 #if NISADMA > 0
383 /*
384 * Used to protect memory for ISA DMA bounce buffers. If, when loading
385 * pages into the system, memory intersects with any of these ranges,
386 * the intersecting memory will be loaded into a lower-priority free list.
387 */
388 bus_dma_segment_t *pmap_isa_dma_ranges;
389 int pmap_isa_dma_nranges;
390
391 boolean_t pmap_isa_dma_range_intersect __P((paddr_t, psize_t,
392 paddr_t *, psize_t *));
393
394 /*
395 * Check if a memory range intersects with an ISA DMA range, and
396 * return the page-rounded intersection if it does. The intersection
397 * will be placed on a lower-priority free list.
398 */
399 boolean_t
400 pmap_isa_dma_range_intersect(pa, size, pap, sizep)
401 paddr_t pa;
402 psize_t size;
403 paddr_t *pap;
404 psize_t *sizep;
405 {
406 bus_dma_segment_t *ds;
407 int i;
408
409 if (pmap_isa_dma_ranges == NULL)
410 return (FALSE);
411
412 for (i = 0, ds = pmap_isa_dma_ranges;
413 i < pmap_isa_dma_nranges; i++, ds++) {
414 if (ds->ds_addr <= pa && pa < (ds->ds_addr + ds->ds_len)) {
415 /*
416 * Beginning of region intersects with this range.
417 */
418 *pap = trunc_page(pa);
419 *sizep = round_page(min(pa + size,
420 ds->ds_addr + ds->ds_len) - pa);
421 return (TRUE);
422 }
423 if (pa < ds->ds_addr && ds->ds_addr < (pa + size)) {
424 /*
425 * End of region intersects with this range.
426 */
427 *pap = trunc_page(ds->ds_addr);
428 *sizep = round_page(min((pa + size) - ds->ds_addr,
429 ds->ds_len));
430 return (TRUE);
431 }
432 }
433
434 /*
435 * No intersection found.
436 */
437 return (FALSE);
438 }
439 #endif /* NISADMA > 0 */
440
441 /*
442 * p v _ e n t r y f u n c t i o n s
443 */
444
445 /*
446 * pv_entry allocation functions:
447 * the main pv_entry allocation functions are:
448 * pmap_alloc_pv: allocate a pv_entry structure
449 * pmap_free_pv: free one pv_entry
450 * pmap_free_pvs: free a list of pv_entrys
451 *
452 * the rest are helper functions
453 */
454
455 /*
456 * pmap_alloc_pv: inline function to allocate a pv_entry structure
457 * => we lock pvalloc_lock
458 * => if we fail, we call out to pmap_alloc_pvpage
459 * => 3 modes:
460 * ALLOCPV_NEED = we really need a pv_entry, even if we have to steal it
461 * ALLOCPV_TRY = we want a pv_entry, but not enough to steal
462 * ALLOCPV_NONEED = we are trying to grow our free list, don't really need
463 * one now
464 *
465 * "try" is for optional functions like pmap_copy().
466 */
467
468 __inline static struct pv_entry *
469 pmap_alloc_pv(pmap, mode)
470 struct pmap *pmap;
471 int mode;
472 {
473 struct pv_page *pvpage;
474 struct pv_entry *pv;
475
476 simple_lock(&pvalloc_lock);
477
478 if (pv_freepages.tqh_first != NULL) {
479 pvpage = pv_freepages.tqh_first;
480 pvpage->pvinfo.pvpi_nfree--;
481 if (pvpage->pvinfo.pvpi_nfree == 0) {
482 /* nothing left in this one? */
483 TAILQ_REMOVE(&pv_freepages, pvpage, pvinfo.pvpi_list);
484 }
485 pv = pvpage->pvinfo.pvpi_pvfree;
486 #ifdef DIAGNOSTIC
487 if (pv == NULL)
488 panic("pmap_alloc_pv: pvpi_nfree off");
489 #endif
490 pvpage->pvinfo.pvpi_pvfree = pv->pv_next;
491 pv_nfpvents--; /* took one from pool */
492 } else {
493 pv = NULL; /* need more of them */
494 }
495
496 /*
497 * if below low water mark or we didn't get a pv_entry we try and
498 * create more pv_entrys ...
499 */
500
501 if (pv_nfpvents < PVE_LOWAT || pv == NULL) {
502 if (pv == NULL)
503 pv = pmap_alloc_pvpage(pmap, (mode == ALLOCPV_TRY) ?
504 mode : ALLOCPV_NEED);
505 else
506 (void) pmap_alloc_pvpage(pmap, ALLOCPV_NONEED);
507 }
508
509 simple_unlock(&pvalloc_lock);
510 return(pv);
511 }
512
513 /*
514 * pmap_alloc_pvpage: maybe allocate a new pvpage
515 *
516 * if need_entry is false: try and allocate a new pv_page
517 * if need_entry is true: try and allocate a new pv_page and return a
518 * new pv_entry from it. if we are unable to allocate a pv_page
519 * we make a last ditch effort to steal a pv_page from some other
520 * mapping. if that fails, we panic...
521 *
522 * => we assume that the caller holds pvalloc_lock
523 */
524
525 static struct pv_entry *
526 pmap_alloc_pvpage(pmap, mode)
527 struct pmap *pmap;
528 int mode;
529 {
530 struct vm_page *pg;
531 struct pv_page *pvpage;
532 struct pv_entry *pv;
533 int s;
534
535 /*
536 * if we need_entry and we've got unused pv_pages, allocate from there
537 */
538
539 if (mode != ALLOCPV_NONEED && pv_unusedpgs.tqh_first != NULL) {
540
541 /* move it to pv_freepages list */
542 pvpage = pv_unusedpgs.tqh_first;
543 TAILQ_REMOVE(&pv_unusedpgs, pvpage, pvinfo.pvpi_list);
544 TAILQ_INSERT_HEAD(&pv_freepages, pvpage, pvinfo.pvpi_list);
545
546 /* allocate a pv_entry */
547 pvpage->pvinfo.pvpi_nfree--; /* can't go to zero */
548 pv = pvpage->pvinfo.pvpi_pvfree;
549 #ifdef DIAGNOSTIC
550 if (pv == NULL)
551 panic("pmap_alloc_pvpage: pvpi_nfree off");
552 #endif
553 pvpage->pvinfo.pvpi_pvfree = pv->pv_next;
554
555 pv_nfpvents--; /* took one from pool */
556 return(pv);
557 }
558
559 /*
560 * see if we've got a cached unmapped VA that we can map a page in.
561 * if not, try to allocate one.
562 */
563
564 s = splvm(); /* must protect kmem_map/kmem_object with splvm! */
565 if (pv_cachedva == 0) {
566 pv_cachedva = uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
567 PAGE_SIZE, UVM_KMF_TRYLOCK|UVM_KMF_VALLOC);
568 if (pv_cachedva == 0) {
569 splx(s);
570 return (NULL);
571 }
572 }
573
574 /*
575 * we have a VA, now let's try and allocate a page in the object
576 * note: we are still holding splvm to protect kmem_object
577 */
578
579 if (!simple_lock_try(&uvmexp.kmem_object->vmobjlock)) {
580 splx(s);
581 return (NULL);
582 }
583
584 pg = uvm_pagealloc(uvmexp.kmem_object, pv_cachedva -
585 vm_map_min(kernel_map),
586 NULL, UVM_PGA_USERESERVE);
587 if (pg)
588 pg->flags &= ~PG_BUSY; /* never busy */
589
590 simple_unlock(&uvmexp.kmem_object->vmobjlock);
591 splx(s);
592 /* splvm now dropped */
593
594 if (pg == NULL)
595 return (NULL);
596
597 /*
598 * add a mapping for our new pv_page and free its entrys (save one!)
599 *
600 * NOTE: If we are allocating a PV page for the kernel pmap, the
601 * pmap is already locked! (...but entering the mapping is safe...)
602 */
603
604 pmap_kenter_pa(pv_cachedva, VM_PAGE_TO_PHYS(pg), VM_PROT_ALL);
605 pmap_update(pmap_kernel());
606 pvpage = (struct pv_page *) pv_cachedva;
607 pv_cachedva = 0;
608 return (pmap_add_pvpage(pvpage, mode != ALLOCPV_NONEED));
609 }
610
611 /*
612 * pmap_add_pvpage: add a pv_page's pv_entrys to the free list
613 *
614 * => caller must hold pvalloc_lock
615 * => if need_entry is true, we allocate and return one pv_entry
616 */
617
618 static struct pv_entry *
619 pmap_add_pvpage(pvp, need_entry)
620 struct pv_page *pvp;
621 boolean_t need_entry;
622 {
623 int tofree, lcv;
624
625 /* do we need to return one? */
626 tofree = (need_entry) ? PVE_PER_PVPAGE - 1 : PVE_PER_PVPAGE;
627
628 pvp->pvinfo.pvpi_pvfree = NULL;
629 pvp->pvinfo.pvpi_nfree = tofree;
630 for (lcv = 0 ; lcv < tofree ; lcv++) {
631 pvp->pvents[lcv].pv_next = pvp->pvinfo.pvpi_pvfree;
632 pvp->pvinfo.pvpi_pvfree = &pvp->pvents[lcv];
633 }
634 if (need_entry)
635 TAILQ_INSERT_TAIL(&pv_freepages, pvp, pvinfo.pvpi_list);
636 else
637 TAILQ_INSERT_TAIL(&pv_unusedpgs, pvp, pvinfo.pvpi_list);
638 pv_nfpvents += tofree;
639 return((need_entry) ? &pvp->pvents[lcv] : NULL);
640 }
641
642 /*
643 * pmap_free_pv_doit: actually free a pv_entry
644 *
645 * => do not call this directly! instead use either
646 * 1. pmap_free_pv ==> free a single pv_entry
647 * 2. pmap_free_pvs => free a list of pv_entrys
648 * => we must be holding pvalloc_lock
649 */
650
651 __inline static void
652 pmap_free_pv_doit(pv)
653 struct pv_entry *pv;
654 {
655 struct pv_page *pvp;
656
657 pvp = (struct pv_page *) arm_trunc_page((vaddr_t)pv);
658 pv_nfpvents++;
659 pvp->pvinfo.pvpi_nfree++;
660
661 /* nfree == 1 => fully allocated page just became partly allocated */
662 if (pvp->pvinfo.pvpi_nfree == 1) {
663 TAILQ_INSERT_HEAD(&pv_freepages, pvp, pvinfo.pvpi_list);
664 }
665
666 /* free it */
667 pv->pv_next = pvp->pvinfo.pvpi_pvfree;
668 pvp->pvinfo.pvpi_pvfree = pv;
669
670 /*
671 * are all pv_page's pv_entry's free? move it to unused queue.
672 */
673
674 if (pvp->pvinfo.pvpi_nfree == PVE_PER_PVPAGE) {
675 TAILQ_REMOVE(&pv_freepages, pvp, pvinfo.pvpi_list);
676 TAILQ_INSERT_HEAD(&pv_unusedpgs, pvp, pvinfo.pvpi_list);
677 }
678 }
679
680 /*
681 * pmap_free_pv: free a single pv_entry
682 *
683 * => we gain the pvalloc_lock
684 */
685
686 __inline static void
687 pmap_free_pv(pmap, pv)
688 struct pmap *pmap;
689 struct pv_entry *pv;
690 {
691 simple_lock(&pvalloc_lock);
692 pmap_free_pv_doit(pv);
693
694 /*
695 * Can't free the PV page if the PV entries were associated with
696 * the kernel pmap; the pmap is already locked.
697 */
698 if (pv_nfpvents > PVE_HIWAT && pv_unusedpgs.tqh_first != NULL &&
699 pmap != pmap_kernel())
700 pmap_free_pvpage();
701
702 simple_unlock(&pvalloc_lock);
703 }
704
705 /*
706 * pmap_free_pvs: free a list of pv_entrys
707 *
708 * => we gain the pvalloc_lock
709 */
710
711 __inline static void
712 pmap_free_pvs(pmap, pvs)
713 struct pmap *pmap;
714 struct pv_entry *pvs;
715 {
716 struct pv_entry *nextpv;
717
718 simple_lock(&pvalloc_lock);
719
720 for ( /* null */ ; pvs != NULL ; pvs = nextpv) {
721 nextpv = pvs->pv_next;
722 pmap_free_pv_doit(pvs);
723 }
724
725 /*
726 * Can't free the PV page if the PV entries were associated with
727 * the kernel pmap; the pmap is already locked.
728 */
729 if (pv_nfpvents > PVE_HIWAT && pv_unusedpgs.tqh_first != NULL &&
730 pmap != pmap_kernel())
731 pmap_free_pvpage();
732
733 simple_unlock(&pvalloc_lock);
734 }
735
736
737 /*
738 * pmap_free_pvpage: try and free an unused pv_page structure
739 *
740 * => assume caller is holding the pvalloc_lock and that
741 * there is a page on the pv_unusedpgs list
742 * => if we can't get a lock on the kmem_map we try again later
743 * => note: analysis of MI kmem_map usage [i.e. malloc/free] shows
744 * that if we can lock the kmem_map then we are not already
745 * holding kmem_object's lock.
746 */
747
748 static void
749 pmap_free_pvpage()
750 {
751 int s;
752 struct vm_map *map;
753 struct vm_map_entry *dead_entries;
754 struct pv_page *pvp;
755
756 s = splvm(); /* protect kmem_map */
757
758 pvp = pv_unusedpgs.tqh_first;
759
760 /*
761 * note: watch out for pv_initpage which is allocated out of
762 * kernel_map rather than kmem_map.
763 */
764 if (pvp == pv_initpage)
765 map = kernel_map;
766 else
767 map = kmem_map;
768
769 if (vm_map_lock_try(map)) {
770
771 /* remove pvp from pv_unusedpgs */
772 TAILQ_REMOVE(&pv_unusedpgs, pvp, pvinfo.pvpi_list);
773
774 /* unmap the page */
775 dead_entries = NULL;
776 uvm_unmap_remove(map, (vaddr_t)pvp, ((vaddr_t)pvp) + PAGE_SIZE,
777 &dead_entries);
778 vm_map_unlock(map);
779
780 if (dead_entries != NULL)
781 uvm_unmap_detach(dead_entries, 0);
782
783 pv_nfpvents -= PVE_PER_PVPAGE; /* update free count */
784 }
785
786 if (pvp == pv_initpage)
787 /* no more initpage, we've freed it */
788 pv_initpage = NULL;
789
790 splx(s);
791 }
792
793 /*
794 * main pv_entry manipulation functions:
795 * pmap_enter_pv: enter a mapping onto a pv_head list
796 * pmap_remove_pv: remove a mappiing from a pv_head list
797 *
798 * NOTE: pmap_enter_pv expects to lock the pvh itself
799 * pmap_remove_pv expects te caller to lock the pvh before calling
800 */
801
802 /*
803 * pmap_enter_pv: enter a mapping onto a pv_head lst
804 *
805 * => caller should hold the proper lock on pmap_main_lock
806 * => caller should have pmap locked
807 * => we will gain the lock on the pv_head and allocate the new pv_entry
808 * => caller should adjust ptp's wire_count before calling
809 * => caller should not adjust pmap's wire_count
810 */
811
812 __inline static void
813 pmap_enter_pv(pvh, pve, pmap, va, ptp, flags)
814 struct pv_head *pvh;
815 struct pv_entry *pve; /* preallocated pve for us to use */
816 struct pmap *pmap;
817 vaddr_t va;
818 struct vm_page *ptp; /* PTP in pmap that maps this VA */
819 int flags;
820 {
821 pve->pv_pmap = pmap;
822 pve->pv_va = va;
823 pve->pv_ptp = ptp; /* NULL for kernel pmap */
824 pve->pv_flags = flags;
825 simple_lock(&pvh->pvh_lock); /* lock pv_head */
826 pve->pv_next = pvh->pvh_list; /* add to ... */
827 pvh->pvh_list = pve; /* ... locked list */
828 simple_unlock(&pvh->pvh_lock); /* unlock, done! */
829 if (pve->pv_flags & PT_W)
830 ++pmap->pm_stats.wired_count;
831 }
832
833 /*
834 * pmap_remove_pv: try to remove a mapping from a pv_list
835 *
836 * => caller should hold proper lock on pmap_main_lock
837 * => pmap should be locked
838 * => caller should hold lock on pv_head [so that attrs can be adjusted]
839 * => caller should adjust ptp's wire_count and free PTP if needed
840 * => caller should NOT adjust pmap's wire_count
841 * => we return the removed pve
842 */
843
844 __inline static struct pv_entry *
845 pmap_remove_pv(pvh, pmap, va)
846 struct pv_head *pvh;
847 struct pmap *pmap;
848 vaddr_t va;
849 {
850 struct pv_entry *pve, **prevptr;
851
852 prevptr = &pvh->pvh_list; /* previous pv_entry pointer */
853 pve = *prevptr;
854 while (pve) {
855 if (pve->pv_pmap == pmap && pve->pv_va == va) { /* match? */
856 *prevptr = pve->pv_next; /* remove it! */
857 if (pve->pv_flags & PT_W)
858 --pmap->pm_stats.wired_count;
859 break;
860 }
861 prevptr = &pve->pv_next; /* previous pointer */
862 pve = pve->pv_next; /* advance */
863 }
864 return(pve); /* return removed pve */
865 }
866
867 /*
868 *
869 * pmap_modify_pv: Update pv flags
870 *
871 * => caller should hold lock on pv_head [so that attrs can be adjusted]
872 * => caller should NOT adjust pmap's wire_count
873 * => we return the old flags
874 *
875 * Modify a physical-virtual mapping in the pv table
876 */
877
878 /*__inline */ u_int
879 pmap_modify_pv(pmap, va, pvh, bic_mask, eor_mask)
880 struct pmap *pmap;
881 vaddr_t va;
882 struct pv_head *pvh;
883 u_int bic_mask;
884 u_int eor_mask;
885 {
886 struct pv_entry *npv;
887 u_int flags, oflags;
888
889 /*
890 * There is at least one VA mapping this page.
891 */
892
893 for (npv = pvh->pvh_list; npv; npv = npv->pv_next) {
894 if (pmap == npv->pv_pmap && va == npv->pv_va) {
895 oflags = npv->pv_flags;
896 npv->pv_flags = flags =
897 ((oflags & ~bic_mask) ^ eor_mask);
898 if ((flags ^ oflags) & PT_W) {
899 if (flags & PT_W)
900 ++pmap->pm_stats.wired_count;
901 else
902 --pmap->pm_stats.wired_count;
903 }
904 return (oflags);
905 }
906 }
907 return (0);
908 }
909
910
911 /*
912 * Map the specified level 2 pagetable into the level 1 page table for
913 * the given pmap to cover a chunk of virtual address space starting from the
914 * address specified.
915 */
916 static /*__inline*/ void
917 pmap_map_in_l1(pmap, va, l2pa, selfref)
918 struct pmap *pmap;
919 vaddr_t va, l2pa;
920 boolean_t selfref;
921 {
922 vaddr_t ptva;
923
924 /* Calculate the index into the L1 page table. */
925 ptva = (va >> PDSHIFT) & ~3;
926
927 PDEBUG(0, printf("wiring %08lx in to pd%p pte0x%lx va0x%lx\n", l2pa,
928 pmap->pm_pdir, L1_PTE(l2pa), ptva));
929
930 /* Map page table into the L1. */
931 pmap->pm_pdir[ptva + 0] = L1_PTE(l2pa + 0x000);
932 pmap->pm_pdir[ptva + 1] = L1_PTE(l2pa + 0x400);
933 pmap->pm_pdir[ptva + 2] = L1_PTE(l2pa + 0x800);
934 pmap->pm_pdir[ptva + 3] = L1_PTE(l2pa + 0xc00);
935
936 PDEBUG(0, printf("pt self reference %lx in %lx\n",
937 L2_PTE_NC_NB(l2pa, AP_KRW), pmap->pm_vptpt));
938
939 /* Map the page table into the page table area. */
940 if (selfref) {
941 *((pt_entry_t *)(pmap->pm_vptpt + ptva)) =
942 L2_PTE_NC_NB(l2pa, AP_KRW);
943 }
944 /* XXX should be a purge */
945 /* cpu_tlb_flushD();*/
946 }
947
948 #if 0
949 static /*__inline*/ void
950 pmap_unmap_in_l1(pmap, va)
951 struct pmap *pmap;
952 vaddr_t va;
953 {
954 vaddr_t ptva;
955
956 /* Calculate the index into the L1 page table. */
957 ptva = (va >> PDSHIFT) & ~3;
958
959 /* Unmap page table from the L1. */
960 pmap->pm_pdir[ptva + 0] = 0;
961 pmap->pm_pdir[ptva + 1] = 0;
962 pmap->pm_pdir[ptva + 2] = 0;
963 pmap->pm_pdir[ptva + 3] = 0;
964
965 /* Unmap the page table from the page table area. */
966 *((pt_entry_t *)(pmap->pm_vptpt + ptva)) = 0;
967
968 /* XXX should be a purge */
969 /* cpu_tlb_flushD();*/
970 }
971 #endif
972
973
974 /*
975 * Used to map a range of physical addresses into kernel
976 * virtual address space.
977 *
978 * For now, VM is already on, we only need to map the
979 * specified memory.
980 */
981 vaddr_t
982 pmap_map(va, spa, epa, prot)
983 vaddr_t va, spa, epa;
984 int prot;
985 {
986 while (spa < epa) {
987 pmap_kenter_pa(va, spa, prot);
988 va += NBPG;
989 spa += NBPG;
990 }
991 pmap_update(pmap_kernel());
992 return(va);
993 }
994
995
996 /*
997 * void pmap_bootstrap(pd_entry_t *kernel_l1pt, pv_addr_t kernel_ptpt)
998 *
999 * bootstrap the pmap system. This is called from initarm and allows
1000 * the pmap system to initailise any structures it requires.
1001 *
1002 * Currently this sets up the kernel_pmap that is statically allocated
1003 * and also allocated virtual addresses for certain page hooks.
1004 * Currently the only one page hook is allocated that is used
1005 * to zero physical pages of memory.
1006 * It also initialises the start and end address of the kernel data space.
1007 */
1008 extern paddr_t physical_freestart;
1009 extern paddr_t physical_freeend;
1010
1011 char *boot_head;
1012
1013 void
1014 pmap_bootstrap(kernel_l1pt, kernel_ptpt)
1015 pd_entry_t *kernel_l1pt;
1016 pv_addr_t kernel_ptpt;
1017 {
1018 int loop;
1019 paddr_t start, end;
1020 #if NISADMA > 0
1021 paddr_t istart;
1022 psize_t isize;
1023 #endif
1024
1025 pmap_kernel()->pm_pdir = kernel_l1pt;
1026 pmap_kernel()->pm_pptpt = kernel_ptpt.pv_pa;
1027 pmap_kernel()->pm_vptpt = kernel_ptpt.pv_va;
1028 simple_lock_init(&pmap_kernel()->pm_lock);
1029 pmap_kernel()->pm_obj.pgops = NULL;
1030 TAILQ_INIT(&(pmap_kernel()->pm_obj.memq));
1031 pmap_kernel()->pm_obj.uo_npages = 0;
1032 pmap_kernel()->pm_obj.uo_refs = 1;
1033
1034 /*
1035 * Initialize PAGE_SIZE-dependent variables.
1036 */
1037 uvm_setpagesize();
1038
1039 npages = 0;
1040 loop = 0;
1041 while (loop < bootconfig.dramblocks) {
1042 start = (paddr_t)bootconfig.dram[loop].address;
1043 end = start + (bootconfig.dram[loop].pages * NBPG);
1044 if (start < physical_freestart)
1045 start = physical_freestart;
1046 if (end > physical_freeend)
1047 end = physical_freeend;
1048 #if 0
1049 printf("%d: %lx -> %lx\n", loop, start, end - 1);
1050 #endif
1051 #if NISADMA > 0
1052 if (pmap_isa_dma_range_intersect(start, end - start,
1053 &istart, &isize)) {
1054 /*
1055 * Place the pages that intersect with the
1056 * ISA DMA range onto the ISA DMA free list.
1057 */
1058 #if 0
1059 printf(" ISADMA 0x%lx -> 0x%lx\n", istart,
1060 istart + isize - 1);
1061 #endif
1062 uvm_page_physload(atop(istart),
1063 atop(istart + isize), atop(istart),
1064 atop(istart + isize), VM_FREELIST_ISADMA);
1065 npages += atop(istart + isize) - atop(istart);
1066
1067 /*
1068 * Load the pieces that come before
1069 * the intersection into the default
1070 * free list.
1071 */
1072 if (start < istart) {
1073 #if 0
1074 printf(" BEFORE 0x%lx -> 0x%lx\n",
1075 start, istart - 1);
1076 #endif
1077 uvm_page_physload(atop(start),
1078 atop(istart), atop(start),
1079 atop(istart), VM_FREELIST_DEFAULT);
1080 npages += atop(istart) - atop(start);
1081 }
1082
1083 /*
1084 * Load the pieces that come after
1085 * the intersection into the default
1086 * free list.
1087 */
1088 if ((istart + isize) < end) {
1089 #if 0
1090 printf(" AFTER 0x%lx -> 0x%lx\n",
1091 (istart + isize), end - 1);
1092 #endif
1093 uvm_page_physload(atop(istart + isize),
1094 atop(end), atop(istart + isize),
1095 atop(end), VM_FREELIST_DEFAULT);
1096 npages += atop(end) - atop(istart + isize);
1097 }
1098 } else {
1099 uvm_page_physload(atop(start), atop(end),
1100 atop(start), atop(end), VM_FREELIST_DEFAULT);
1101 npages += atop(end) - atop(start);
1102 }
1103 #else /* NISADMA > 0 */
1104 uvm_page_physload(atop(start), atop(end),
1105 atop(start), atop(end), VM_FREELIST_DEFAULT);
1106 npages += atop(end) - atop(start);
1107 #endif /* NISADMA > 0 */
1108 ++loop;
1109 }
1110
1111 #ifdef MYCROFT_HACK
1112 printf("npages = %ld\n", npages);
1113 #endif
1114
1115 virtual_start = KERNEL_VM_BASE;
1116 virtual_end = virtual_start + KERNEL_VM_SIZE - 1;
1117
1118 ALLOC_PAGE_HOOK(page_hook0, NBPG);
1119 ALLOC_PAGE_HOOK(page_hook1, NBPG);
1120
1121 /*
1122 * The mem special device needs a virtual hook but we don't
1123 * need a pte
1124 */
1125 memhook = (char *)virtual_start;
1126 virtual_start += NBPG;
1127
1128 msgbufaddr = (caddr_t)virtual_start;
1129 msgbufpte = (pt_entry_t)pmap_pte(pmap_kernel(), virtual_start);
1130 virtual_start += round_page(MSGBUFSIZE);
1131
1132 /*
1133 * init the static-global locks and global lists.
1134 */
1135 spinlockinit(&pmap_main_lock, "pmaplk", 0);
1136 simple_lock_init(&pvalloc_lock);
1137 TAILQ_INIT(&pv_freepages);
1138 TAILQ_INIT(&pv_unusedpgs);
1139
1140 /*
1141 * compute the number of pages we have and then allocate RAM
1142 * for each pages' pv_head and saved attributes.
1143 */
1144 {
1145 int npages, lcv;
1146 vsize_t s;
1147
1148 npages = 0;
1149 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++)
1150 npages += (vm_physmem[lcv].end - vm_physmem[lcv].start);
1151 s = (vsize_t) (sizeof(struct pv_head) * npages +
1152 sizeof(char) * npages);
1153 s = round_page(s); /* round up */
1154 boot_head = (char *)uvm_pageboot_alloc(s);
1155 bzero((char *)boot_head, s);
1156 if (boot_head == 0)
1157 panic("pmap_init: unable to allocate pv_heads");
1158 }
1159
1160 /*
1161 * initialize the pmap pool.
1162 */
1163
1164 pool_init(&pmap_pmap_pool, sizeof(struct pmap), 0, 0, 0, "pmappl",
1165 0, pool_page_alloc_nointr, pool_page_free_nointr, M_VMPMAP);
1166
1167 cpu_cache_cleanD();
1168 }
1169
1170 /*
1171 * void pmap_init(void)
1172 *
1173 * Initialize the pmap module.
1174 * Called by vm_init() in vm/vm_init.c in order to initialise
1175 * any structures that the pmap system needs to map virtual memory.
1176 */
1177
1178 extern int physmem;
1179
1180 void
1181 pmap_init()
1182 {
1183 int lcv, i;
1184
1185 #ifdef MYCROFT_HACK
1186 printf("physmem = %d\n", physmem);
1187 #endif
1188
1189 /*
1190 * Set the available memory vars - These do not map to real memory
1191 * addresses and cannot as the physical memory is fragmented.
1192 * They are used by ps for %mem calculations.
1193 * One could argue whether this should be the entire memory or just
1194 * the memory that is useable in a user process.
1195 */
1196 avail_start = 0;
1197 avail_end = physmem * NBPG;
1198
1199 /* allocate pv_head stuff first */
1200 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
1201 vm_physmem[lcv].pmseg.pvhead = (struct pv_head *)boot_head;
1202 boot_head = (char *)(vaddr_t)(vm_physmem[lcv].pmseg.pvhead +
1203 (vm_physmem[lcv].end - vm_physmem[lcv].start));
1204 for (i = 0;
1205 i < (vm_physmem[lcv].end - vm_physmem[lcv].start); i++) {
1206 simple_lock_init(
1207 &vm_physmem[lcv].pmseg.pvhead[i].pvh_lock);
1208 }
1209 }
1210
1211 /* now allocate attrs */
1212 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
1213 vm_physmem[lcv].pmseg.attrs = (char *) boot_head;
1214 boot_head = (char *)(vaddr_t)(vm_physmem[lcv].pmseg.attrs +
1215 (vm_physmem[lcv].end - vm_physmem[lcv].start));
1216 }
1217
1218 /*
1219 * now we need to free enough pv_entry structures to allow us to get
1220 * the kmem_map/kmem_object allocated and inited (done after this
1221 * function is finished). to do this we allocate one bootstrap page out
1222 * of kernel_map and use it to provide an initial pool of pv_entry
1223 * structures. we never free this page.
1224 */
1225
1226 pv_initpage = (struct pv_page *) uvm_km_alloc(kernel_map, PAGE_SIZE);
1227 if (pv_initpage == NULL)
1228 panic("pmap_init: pv_initpage");
1229 pv_cachedva = 0; /* a VA we have allocated but not used yet */
1230 pv_nfpvents = 0;
1231 (void) pmap_add_pvpage(pv_initpage, FALSE);
1232
1233 #ifdef MYCROFT_HACK
1234 for (lcv = 0 ; lcv < vm_nphysseg ; lcv++) {
1235 printf("physseg[%d] pvent=%p attrs=%p start=%ld end=%ld\n",
1236 lcv,
1237 vm_physmem[lcv].pmseg.pvent, vm_physmem[lcv].pmseg.attrs,
1238 vm_physmem[lcv].start, vm_physmem[lcv].end);
1239 }
1240 #endif
1241 pmap_initialized = TRUE;
1242
1243 /* Initialise our L1 page table queues and counters */
1244 SIMPLEQ_INIT(&l1pt_static_queue);
1245 l1pt_static_queue_count = 0;
1246 l1pt_static_create_count = 0;
1247 SIMPLEQ_INIT(&l1pt_queue);
1248 l1pt_queue_count = 0;
1249 l1pt_create_count = 0;
1250 l1pt_reuse_count = 0;
1251 }
1252
1253 /*
1254 * pmap_postinit()
1255 *
1256 * This routine is called after the vm and kmem subsystems have been
1257 * initialised. This allows the pmap code to perform any initialisation
1258 * that can only be done one the memory allocation is in place.
1259 */
1260
1261 void
1262 pmap_postinit()
1263 {
1264 int loop;
1265 struct l1pt *pt;
1266
1267 #ifdef PMAP_STATIC_L1S
1268 for (loop = 0; loop < PMAP_STATIC_L1S; ++loop) {
1269 #else /* PMAP_STATIC_L1S */
1270 for (loop = 0; loop < max_processes; ++loop) {
1271 #endif /* PMAP_STATIC_L1S */
1272 /* Allocate a L1 page table */
1273 pt = pmap_alloc_l1pt();
1274 if (!pt)
1275 panic("Cannot allocate static L1 page tables\n");
1276
1277 /* Clean it */
1278 bzero((void *)pt->pt_va, PD_SIZE);
1279 pt->pt_flags |= (PTFLAG_STATIC | PTFLAG_CLEAN);
1280 /* Add the page table to the queue */
1281 SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pt, pt_queue);
1282 ++l1pt_static_queue_count;
1283 ++l1pt_static_create_count;
1284 }
1285 }
1286
1287
1288 /*
1289 * Create and return a physical map.
1290 *
1291 * If the size specified for the map is zero, the map is an actual physical
1292 * map, and may be referenced by the hardware.
1293 *
1294 * If the size specified is non-zero, the map will be used in software only,
1295 * and is bounded by that size.
1296 */
1297
1298 pmap_t
1299 pmap_create()
1300 {
1301 struct pmap *pmap;
1302
1303 /*
1304 * Fetch pmap entry from the pool
1305 */
1306
1307 pmap = pool_get(&pmap_pmap_pool, PR_WAITOK);
1308 /* XXX is this really needed! */
1309 memset(pmap, 0, sizeof(*pmap));
1310
1311 simple_lock_init(&pmap->pm_obj.vmobjlock);
1312 pmap->pm_obj.pgops = NULL; /* currently not a mappable object */
1313 TAILQ_INIT(&pmap->pm_obj.memq);
1314 pmap->pm_obj.uo_npages = 0;
1315 pmap->pm_obj.uo_refs = 1;
1316 pmap->pm_stats.wired_count = 0;
1317 pmap->pm_stats.resident_count = 1;
1318
1319 /* Now init the machine part of the pmap */
1320 pmap_pinit(pmap);
1321 return(pmap);
1322 }
1323
1324 /*
1325 * pmap_alloc_l1pt()
1326 *
1327 * This routine allocates physical and virtual memory for a L1 page table
1328 * and wires it.
1329 * A l1pt structure is returned to describe the allocated page table.
1330 *
1331 * This routine is allowed to fail if the required memory cannot be allocated.
1332 * In this case NULL is returned.
1333 */
1334
1335 struct l1pt *
1336 pmap_alloc_l1pt(void)
1337 {
1338 paddr_t pa;
1339 vaddr_t va;
1340 struct l1pt *pt;
1341 int error;
1342 struct vm_page *m;
1343 pt_entry_t *ptes;
1344
1345 /* Allocate virtual address space for the L1 page table */
1346 va = uvm_km_valloc(kernel_map, PD_SIZE);
1347 if (va == 0) {
1348 #ifdef DIAGNOSTIC
1349 printf("pmap: Cannot allocate pageable memory for L1\n");
1350 #endif /* DIAGNOSTIC */
1351 return(NULL);
1352 }
1353
1354 /* Allocate memory for the l1pt structure */
1355 pt = (struct l1pt *)malloc(sizeof(struct l1pt), M_VMPMAP, M_WAITOK);
1356
1357 /*
1358 * Allocate pages from the VM system.
1359 */
1360 TAILQ_INIT(&pt->pt_plist);
1361 error = uvm_pglistalloc(PD_SIZE, physical_start, physical_end,
1362 PD_SIZE, 0, &pt->pt_plist, 1, M_WAITOK);
1363 if (error) {
1364 #ifdef DIAGNOSTIC
1365 printf("pmap: Cannot allocate physical memory for L1 (%d)\n",
1366 error);
1367 #endif /* DIAGNOSTIC */
1368 /* Release the resources we already have claimed */
1369 free(pt, M_VMPMAP);
1370 uvm_km_free(kernel_map, va, PD_SIZE);
1371 return(NULL);
1372 }
1373
1374 /* Map our physical pages into our virtual space */
1375 pt->pt_va = va;
1376 m = pt->pt_plist.tqh_first;
1377 ptes = pmap_map_ptes(pmap_kernel());
1378 while (m && va < (pt->pt_va + PD_SIZE)) {
1379 pa = VM_PAGE_TO_PHYS(m);
1380
1381 pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE);
1382
1383 /* Revoke cacheability and bufferability */
1384 /* XXX should be done better than this */
1385 ptes[arm_byte_to_page(va)] &= ~(PT_C | PT_B);
1386
1387 va += NBPG;
1388 m = m->pageq.tqe_next;
1389 }
1390 pmap_unmap_ptes(pmap_kernel());
1391 pmap_update(pmap_kernel());
1392
1393 #ifdef DIAGNOSTIC
1394 if (m)
1395 panic("pmap_alloc_l1pt: pglist not empty\n");
1396 #endif /* DIAGNOSTIC */
1397
1398 pt->pt_flags = 0;
1399 return(pt);
1400 }
1401
1402 /*
1403 * Free a L1 page table previously allocated with pmap_alloc_l1pt().
1404 */
1405 void
1406 pmap_free_l1pt(pt)
1407 struct l1pt *pt;
1408 {
1409 /* Separate the physical memory for the virtual space */
1410 pmap_kremove(pt->pt_va, PD_SIZE);
1411 pmap_update(pmap_kernel());
1412
1413 /* Return the physical memory */
1414 uvm_pglistfree(&pt->pt_plist);
1415
1416 /* Free the virtual space */
1417 uvm_km_free(kernel_map, pt->pt_va, PD_SIZE);
1418
1419 /* Free the l1pt structure */
1420 free(pt, M_VMPMAP);
1421 }
1422
1423 /*
1424 * Allocate a page directory.
1425 * This routine will either allocate a new page directory from the pool
1426 * of L1 page tables currently held by the kernel or it will allocate
1427 * a new one via pmap_alloc_l1pt().
1428 * It will then initialise the l1 page table for use.
1429 */
1430 int
1431 pmap_allocpagedir(pmap)
1432 struct pmap *pmap;
1433 {
1434 paddr_t pa;
1435 struct l1pt *pt;
1436 pt_entry_t *pte;
1437
1438 PDEBUG(0, printf("pmap_allocpagedir(%p)\n", pmap));
1439
1440 /* Do we have any spare L1's lying around ? */
1441 if (l1pt_static_queue_count) {
1442 --l1pt_static_queue_count;
1443 pt = l1pt_static_queue.sqh_first;
1444 SIMPLEQ_REMOVE_HEAD(&l1pt_static_queue, pt, pt_queue);
1445 } else if (l1pt_queue_count) {
1446 --l1pt_queue_count;
1447 pt = l1pt_queue.sqh_first;
1448 SIMPLEQ_REMOVE_HEAD(&l1pt_queue, pt, pt_queue);
1449 ++l1pt_reuse_count;
1450 } else {
1451 pt = pmap_alloc_l1pt();
1452 if (!pt)
1453 return(ENOMEM);
1454 ++l1pt_create_count;
1455 }
1456
1457 /* Store the pointer to the l1 descriptor in the pmap. */
1458 pmap->pm_l1pt = pt;
1459
1460 /* Get the physical address of the start of the l1 */
1461 pa = VM_PAGE_TO_PHYS(pt->pt_plist.tqh_first);
1462
1463 /* Store the virtual address of the l1 in the pmap. */
1464 pmap->pm_pdir = (pd_entry_t *)pt->pt_va;
1465
1466 /* Clean the L1 if it is dirty */
1467 if (!(pt->pt_flags & PTFLAG_CLEAN))
1468 bzero((void *)pmap->pm_pdir, (PD_SIZE - KERNEL_PD_SIZE));
1469
1470 /* Do we already have the kernel mappings ? */
1471 if (!(pt->pt_flags & PTFLAG_KPT)) {
1472 /* Duplicate the kernel mapping i.e. all mappings 0xf0000000+ */
1473
1474 bcopy((char *)pmap_kernel()->pm_pdir + (PD_SIZE - KERNEL_PD_SIZE),
1475 (char *)pmap->pm_pdir + (PD_SIZE - KERNEL_PD_SIZE),
1476 KERNEL_PD_SIZE);
1477 pt->pt_flags |= PTFLAG_KPT;
1478 }
1479
1480 /* Allocate a page table to map all the page tables for this pmap */
1481
1482 #ifdef DIAGNOSTIC
1483 if (pmap->pm_vptpt) {
1484 /* XXX What if we have one already ? */
1485 panic("pmap_allocpagedir: have pt already\n");
1486 }
1487 #endif /* DIAGNOSTIC */
1488 pmap->pm_vptpt = uvm_km_zalloc(kernel_map, NBPG);
1489 if (pmap->pm_vptpt == 0) {
1490 pmap_freepagedir(pmap);
1491 return(ENOMEM);
1492 }
1493
1494 (void) pmap_extract(pmap_kernel(), pmap->pm_vptpt, &pmap->pm_pptpt);
1495 pmap->pm_pptpt &= PG_FRAME;
1496 /* Revoke cacheability and bufferability */
1497 /* XXX should be done better than this */
1498 pte = pmap_pte(pmap_kernel(), pmap->pm_vptpt);
1499 *pte = *pte & ~(PT_C | PT_B);
1500
1501 /* Wire in this page table */
1502 pmap_map_in_l1(pmap, PROCESS_PAGE_TBLS_BASE, pmap->pm_pptpt, TRUE);
1503
1504 pt->pt_flags &= ~PTFLAG_CLEAN; /* L1 is dirty now */
1505
1506 /*
1507 * Map the kernel page tables for 0xf0000000 +
1508 * into the page table used to map the
1509 * pmap's page tables
1510 */
1511 bcopy((char *)(PROCESS_PAGE_TBLS_BASE
1512 + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2))
1513 + ((PD_SIZE - KERNEL_PD_SIZE) >> 2)),
1514 (char *)pmap->pm_vptpt + ((PD_SIZE - KERNEL_PD_SIZE) >> 2),
1515 (KERNEL_PD_SIZE >> 2));
1516
1517 return(0);
1518 }
1519
1520
1521 /*
1522 * Initialize a preallocated and zeroed pmap structure,
1523 * such as one in a vmspace structure.
1524 */
1525
1526 void
1527 pmap_pinit(pmap)
1528 struct pmap *pmap;
1529 {
1530 PDEBUG(0, printf("pmap_pinit(%p)\n", pmap));
1531
1532 /* Keep looping until we succeed in allocating a page directory */
1533 while (pmap_allocpagedir(pmap) != 0) {
1534 /*
1535 * Ok we failed to allocate a suitable block of memory for an
1536 * L1 page table. This means that either:
1537 * 1. 16KB of virtual address space could not be allocated
1538 * 2. 16KB of physically contiguous memory on a 16KB boundary
1539 * could not be allocated.
1540 *
1541 * Since we cannot fail we will sleep for a while and try
1542 * again.
1543 */
1544 (void) ltsleep(&lbolt, PVM, "l1ptwait", hz >> 3, NULL);
1545 }
1546
1547 /* Map zero page for the pmap. This will also map the L2 for it */
1548 pmap_enter(pmap, 0x00000000, systempage.pv_pa,
1549 VM_PROT_READ, VM_PROT_READ | PMAP_WIRED);
1550 pmap_update(pmap);
1551 }
1552
1553
1554 void
1555 pmap_freepagedir(pmap)
1556 struct pmap *pmap;
1557 {
1558 /* Free the memory used for the page table mapping */
1559 if (pmap->pm_vptpt != 0)
1560 uvm_km_free(kernel_map, (vaddr_t)pmap->pm_vptpt, NBPG);
1561
1562 /* junk the L1 page table */
1563 if (pmap->pm_l1pt->pt_flags & PTFLAG_STATIC) {
1564 /* Add the page table to the queue */
1565 SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pmap->pm_l1pt, pt_queue);
1566 ++l1pt_static_queue_count;
1567 } else if (l1pt_queue_count < 8) {
1568 /* Add the page table to the queue */
1569 SIMPLEQ_INSERT_TAIL(&l1pt_queue, pmap->pm_l1pt, pt_queue);
1570 ++l1pt_queue_count;
1571 } else
1572 pmap_free_l1pt(pmap->pm_l1pt);
1573 }
1574
1575
1576 /*
1577 * Retire the given physical map from service.
1578 * Should only be called if the map contains no valid mappings.
1579 */
1580
1581 void
1582 pmap_destroy(pmap)
1583 struct pmap *pmap;
1584 {
1585 struct vm_page *page;
1586 int count;
1587
1588 if (pmap == NULL)
1589 return;
1590
1591 PDEBUG(0, printf("pmap_destroy(%p)\n", pmap));
1592
1593 /*
1594 * Drop reference count
1595 */
1596 simple_lock(&pmap->pm_obj.vmobjlock);
1597 count = --pmap->pm_obj.uo_refs;
1598 simple_unlock(&pmap->pm_obj.vmobjlock);
1599 if (count > 0) {
1600 return;
1601 }
1602
1603 /*
1604 * reference count is zero, free pmap resources and then free pmap.
1605 */
1606
1607 /* Remove the zero page mapping */
1608 pmap_remove(pmap, 0x00000000, 0x00000000 + NBPG);
1609 pmap_update(pmap);
1610
1611 /*
1612 * Free any page tables still mapped
1613 * This is only temporay until pmap_enter can count the number
1614 * of mappings made in a page table. Then pmap_remove() can
1615 * reduce the count and free the pagetable when the count
1616 * reaches zero. Note that entries in this list should match the
1617 * contents of the ptpt, however this is faster than walking a 1024
1618 * entries looking for pt's
1619 * taken from i386 pmap.c
1620 */
1621 while (pmap->pm_obj.memq.tqh_first != NULL) {
1622 page = pmap->pm_obj.memq.tqh_first;
1623 #ifdef DIAGNOSTIC
1624 if (page->flags & PG_BUSY)
1625 panic("pmap_release: busy page table page");
1626 #endif
1627 /* pmap_page_protect? currently no need for it. */
1628
1629 page->wire_count = 0;
1630 uvm_pagefree(page);
1631 }
1632
1633 /* Free the page dir */
1634 pmap_freepagedir(pmap);
1635
1636 /* return the pmap to the pool */
1637 pool_put(&pmap_pmap_pool, pmap);
1638 }
1639
1640
1641 /*
1642 * void pmap_reference(struct pmap *pmap)
1643 *
1644 * Add a reference to the specified pmap.
1645 */
1646
1647 void
1648 pmap_reference(pmap)
1649 struct pmap *pmap;
1650 {
1651 if (pmap == NULL)
1652 return;
1653
1654 simple_lock(&pmap->pm_lock);
1655 pmap->pm_obj.uo_refs++;
1656 simple_unlock(&pmap->pm_lock);
1657 }
1658
1659 /*
1660 * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
1661 *
1662 * Return the start and end addresses of the kernel's virtual space.
1663 * These values are setup in pmap_bootstrap and are updated as pages
1664 * are allocated.
1665 */
1666
1667 void
1668 pmap_virtual_space(start, end)
1669 vaddr_t *start;
1670 vaddr_t *end;
1671 {
1672 *start = virtual_start;
1673 *end = virtual_end;
1674 }
1675
1676
1677 /*
1678 * Activate the address space for the specified process. If the process
1679 * is the current process, load the new MMU context.
1680 */
1681 void
1682 pmap_activate(p)
1683 struct proc *p;
1684 {
1685 struct pmap *pmap = p->p_vmspace->vm_map.pmap;
1686 struct pcb *pcb = &p->p_addr->u_pcb;
1687
1688 (void) pmap_extract(pmap_kernel(), (vaddr_t)pmap->pm_pdir,
1689 (paddr_t *)&pcb->pcb_pagedir);
1690
1691 PDEBUG(0, printf("pmap_activate: p=%p pmap=%p pcb=%p pdir=%p l1=%p\n",
1692 p, pmap, pcb, pmap->pm_pdir, pcb->pcb_pagedir));
1693
1694 if (p == curproc) {
1695 PDEBUG(0, printf("pmap_activate: setting TTB\n"));
1696 setttb((u_int)pcb->pcb_pagedir);
1697 }
1698 #if 0
1699 pmap->pm_pdchanged = FALSE;
1700 #endif
1701 }
1702
1703
1704 /*
1705 * Deactivate the address space of the specified process.
1706 */
1707 void
1708 pmap_deactivate(p)
1709 struct proc *p;
1710 {
1711 }
1712
1713
1714 /*
1715 * pmap_clean_page()
1716 *
1717 * This is a local function used to work out the best strategy to clean
1718 * a single page referenced by its entry in the PV table. It's used by
1719 * pmap_copy_page, pmap_zero page and maybe some others later on.
1720 *
1721 * Its policy is effectively:
1722 * o If there are no mappings, we don't bother doing anything with the cache.
1723 * o If there is one mapping, we clean just that page.
1724 * o If there are multiple mappings, we clean the entire cache.
1725 *
1726 * So that some functions can be further optimised, it returns 0 if it didn't
1727 * clean the entire cache, or 1 if it did.
1728 *
1729 * XXX One bug in this routine is that if the pv_entry has a single page
1730 * mapped at 0x00000000 a whole cache clean will be performed rather than
1731 * just the 1 page. Since this should not occur in everyday use and if it does
1732 * it will just result in not the most efficient clean for the page.
1733 */
1734 static int
1735 pmap_clean_page(pv, is_src)
1736 struct pv_entry *pv;
1737 boolean_t is_src;
1738 {
1739 struct pmap *pmap;
1740 struct pv_entry *npv;
1741 int cache_needs_cleaning = 0;
1742 vaddr_t page_to_clean = 0;
1743
1744 if (pv == NULL)
1745 /* nothing mapped in so nothing to flush */
1746 return (0);
1747
1748 /* Since we flush the cache each time we change curproc, we
1749 * only need to flush the page if it is in the current pmap.
1750 */
1751 if (curproc)
1752 pmap = curproc->p_vmspace->vm_map.pmap;
1753 else
1754 pmap = pmap_kernel();
1755
1756 for (npv = pv; npv; npv = npv->pv_next) {
1757 if (npv->pv_pmap == pmap) {
1758 /* The page is mapped non-cacheable in
1759 * this map. No need to flush the cache.
1760 */
1761 if (npv->pv_flags & PT_NC) {
1762 #ifdef DIAGNOSTIC
1763 if (cache_needs_cleaning)
1764 panic("pmap_clean_page: "
1765 "cache inconsistency");
1766 #endif
1767 break;
1768 }
1769 #if 0
1770 /* This doesn't work, because pmap_protect
1771 doesn't flush changes on pages that it
1772 has write-protected. */
1773
1774 /* If the page is not writeable and this
1775 is the source, then there is no need
1776 to flush it from the cache. */
1777 else if (is_src && ! (npv->pv_flags & PT_Wr))
1778 continue;
1779 #endif
1780 if (cache_needs_cleaning){
1781 page_to_clean = 0;
1782 break;
1783 }
1784 else
1785 page_to_clean = npv->pv_va;
1786 cache_needs_cleaning = 1;
1787 }
1788 }
1789
1790 if (page_to_clean)
1791 cpu_cache_purgeID_rng(page_to_clean, NBPG);
1792 else if (cache_needs_cleaning) {
1793 cpu_cache_purgeID();
1794 return (1);
1795 }
1796 return (0);
1797 }
1798
1799 /*
1800 * pmap_find_pv()
1801 *
1802 * This is a local function that finds a PV head for a given physical page.
1803 * This is a common op, and this function removes loads of ifdefs in the code.
1804 */
1805 static __inline struct pv_head *
1806 pmap_find_pvh(phys)
1807 paddr_t phys;
1808 {
1809 int bank, off;
1810 struct pv_head *pvh;
1811
1812 if ((bank = vm_physseg_find(atop(phys), &off)) == -1)
1813 panic("pmap_find_pv: not a real page, phys=%lx\n", phys);
1814 pvh = &vm_physmem[bank].pmseg.pvhead[off];
1815 return (pvh);
1816 }
1817
1818 /*
1819 * pmap_zero_page()
1820 *
1821 * Zero a given physical page by mapping it at a page hook point.
1822 * In doing the zero page op, the page we zero is mapped cachable, as with
1823 * StrongARM accesses to non-cached pages are non-burst making writing
1824 * _any_ bulk data very slow.
1825 */
1826 void
1827 pmap_zero_page(phys)
1828 paddr_t phys;
1829 {
1830 struct pv_head *pvh;
1831
1832 /* Get an entry for this page, and clean it it. */
1833 pvh = pmap_find_pvh(phys);
1834 simple_lock(&pvh->pvh_lock);
1835 pmap_clean_page(pvh->pvh_list, FALSE);
1836 simple_unlock(&pvh->pvh_lock);
1837
1838 /*
1839 * Hook in the page, zero it, and purge the cache for that
1840 * zeroed page. Invalidate the TLB as needed.
1841 */
1842 *page_hook0.pte = L2_PTE(phys & PG_FRAME, AP_KRW);
1843 cpu_tlb_flushD_SE(page_hook0.va);
1844 bzero_page(page_hook0.va);
1845 cpu_cache_purgeD_rng(page_hook0.va, NBPG);
1846 }
1847
1848 /* pmap_pageidlezero()
1849 *
1850 * The same as above, except that we assume that the page is not
1851 * mapped. This means we never have to flush the cache first. Called
1852 * from the idle loop.
1853 */
1854 boolean_t
1855 pmap_pageidlezero(phys)
1856 paddr_t phys;
1857 {
1858 int i, *ptr;
1859 boolean_t rv = TRUE;
1860
1861 #ifdef DIAGNOSTIC
1862 struct pv_head *pvh;
1863
1864 pvh = pmap_find_pvh(phys);
1865 if (pvh->pvh_list != NULL)
1866 panic("pmap_pageidlezero: zeroing mapped page\n");
1867 #endif
1868
1869 /*
1870 * Hook in the page, zero it, and purge the cache for that
1871 * zeroed page. Invalidate the TLB as needed.
1872 */
1873 *page_hook0.pte = L2_PTE(phys & PG_FRAME, AP_KRW);
1874 cpu_tlb_flushD_SE(page_hook0.va);
1875
1876 for (i = 0, ptr = (int *)page_hook0.va;
1877 i < (NBPG / sizeof(int)); i++) {
1878 if (sched_whichqs != 0) {
1879 /*
1880 * A process has become ready. Abort now,
1881 * so we don't keep it waiting while we
1882 * do slow memory access to finish this
1883 * page.
1884 */
1885 rv = FALSE;
1886 break;
1887 }
1888 *ptr++ = 0;
1889 }
1890
1891 if (rv)
1892 /*
1893 * if we aborted we'll rezero this page again later so don't
1894 * purge it unless we finished it
1895 */
1896 cpu_cache_purgeD_rng(page_hook0.va, NBPG);
1897 return (rv);
1898 }
1899
1900 /*
1901 * pmap_copy_page()
1902 *
1903 * Copy one physical page into another, by mapping the pages into
1904 * hook points. The same comment regarding cachability as in
1905 * pmap_zero_page also applies here.
1906 */
1907 void
1908 pmap_copy_page(src, dest)
1909 paddr_t src;
1910 paddr_t dest;
1911 {
1912 struct pv_head *src_pvh, *dest_pvh;
1913 boolean_t cleanedcache;
1914
1915 /* Get PV entries for the pages, and clean them if needed. */
1916 src_pvh = pmap_find_pvh(src);
1917
1918 simple_lock(&src_pvh->pvh_lock);
1919 cleanedcache = pmap_clean_page(src_pvh->pvh_list, TRUE);
1920 simple_unlock(&src_pvh->pvh_lock);
1921
1922 if (cleanedcache == 0) {
1923 dest_pvh = pmap_find_pvh(dest);
1924 simple_lock(&dest_pvh->pvh_lock);
1925 pmap_clean_page(dest_pvh->pvh_list, FALSE);
1926 simple_unlock(&dest_pvh->pvh_lock);
1927 }
1928 /*
1929 * Map the pages into the page hook points, copy them, and purge
1930 * the cache for the appropriate page. Invalidate the TLB
1931 * as required.
1932 */
1933 *page_hook0.pte = L2_PTE(src & PG_FRAME, AP_KRW);
1934 *page_hook1.pte = L2_PTE(dest & PG_FRAME, AP_KRW);
1935 cpu_tlb_flushD_SE(page_hook0.va);
1936 cpu_tlb_flushD_SE(page_hook1.va);
1937 bcopy_page(page_hook0.va, page_hook1.va);
1938 cpu_cache_purgeD_rng(page_hook0.va, NBPG);
1939 cpu_cache_purgeD_rng(page_hook1.va, NBPG);
1940 }
1941
1942 /*
1943 * int pmap_next_phys_page(paddr_t *addr)
1944 *
1945 * Allocate another physical page returning true or false depending
1946 * on whether a page could be allocated.
1947 */
1948
1949 paddr_t
1950 pmap_next_phys_page(addr)
1951 paddr_t addr;
1952
1953 {
1954 int loop;
1955
1956 if (addr < bootconfig.dram[0].address)
1957 return(bootconfig.dram[0].address);
1958
1959 loop = 0;
1960
1961 while (bootconfig.dram[loop].address != 0
1962 && addr > (bootconfig.dram[loop].address + bootconfig.dram[loop].pages * NBPG))
1963 ++loop;
1964
1965 if (bootconfig.dram[loop].address == 0)
1966 return(0);
1967
1968 addr += NBPG;
1969
1970 if (addr >= (bootconfig.dram[loop].address + bootconfig.dram[loop].pages * NBPG)) {
1971 if (bootconfig.dram[loop + 1].address == 0)
1972 return(0);
1973 addr = bootconfig.dram[loop + 1].address;
1974 }
1975
1976 return(addr);
1977 }
1978
1979 #if 0
1980 void
1981 pmap_pte_addref(pmap, va)
1982 struct pmap *pmap;
1983 vaddr_t va;
1984 {
1985 pd_entry_t *pde;
1986 paddr_t pa;
1987 struct vm_page *m;
1988
1989 if (pmap == pmap_kernel())
1990 return;
1991
1992 pde = pmap_pde(pmap, va & ~(3 << PDSHIFT));
1993 pa = pmap_pte_pa(pde);
1994 m = PHYS_TO_VM_PAGE(pa);
1995 ++m->wire_count;
1996 #ifdef MYCROFT_HACK
1997 printf("addref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
1998 pmap, va, pde, pa, m, m->wire_count);
1999 #endif
2000 }
2001
2002 void
2003 pmap_pte_delref(pmap, va)
2004 struct pmap *pmap;
2005 vaddr_t va;
2006 {
2007 pd_entry_t *pde;
2008 paddr_t pa;
2009 struct vm_page *m;
2010
2011 if (pmap == pmap_kernel())
2012 return;
2013
2014 pde = pmap_pde(pmap, va & ~(3 << PDSHIFT));
2015 pa = pmap_pte_pa(pde);
2016 m = PHYS_TO_VM_PAGE(pa);
2017 --m->wire_count;
2018 #ifdef MYCROFT_HACK
2019 printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
2020 pmap, va, pde, pa, m, m->wire_count);
2021 #endif
2022 if (m->wire_count == 0) {
2023 #ifdef MYCROFT_HACK
2024 printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p\n",
2025 pmap, va, pde, pa, m);
2026 #endif
2027 pmap_unmap_in_l1(pmap, va);
2028 uvm_pagefree(m);
2029 --pmap->pm_stats.resident_count;
2030 }
2031 }
2032 #else
2033 #define pmap_pte_addref(pmap, va)
2034 #define pmap_pte_delref(pmap, va)
2035 #endif
2036
2037 /*
2038 * Since we have a virtually indexed cache, we may need to inhibit caching if
2039 * there is more than one mapping and at least one of them is writable.
2040 * Since we purge the cache on every context switch, we only need to check for
2041 * other mappings within the same pmap, or kernel_pmap.
2042 * This function is also called when a page is unmapped, to possibly reenable
2043 * caching on any remaining mappings.
2044 *
2045 * Note that the pmap must have it's ptes mapped in, and passed with ptes.
2046 */
2047 void
2048 pmap_vac_me_harder(struct pmap *pmap, struct pv_head *pvh, pt_entry_t *ptes,
2049 boolean_t clear_cache)
2050 {
2051 struct pv_entry *pv, *npv;
2052 pt_entry_t *pte;
2053 int entries = 0;
2054 int writeable = 0;
2055 int cacheable_entries = 0;
2056
2057 pv = pvh->pvh_list;
2058 KASSERT(ptes != NULL);
2059
2060 /*
2061 * Count mappings and writable mappings in this pmap.
2062 * Keep a pointer to the first one.
2063 */
2064 for (npv = pv; npv; npv = npv->pv_next) {
2065 /* Count mappings in the same pmap */
2066 if (pmap == npv->pv_pmap) {
2067 if (entries++ == 0)
2068 pv = npv;
2069 /* Cacheable mappings */
2070 if ((npv->pv_flags & PT_NC) == 0)
2071 cacheable_entries++;
2072 /* Writeable mappings */
2073 if (npv->pv_flags & PT_Wr)
2074 ++writeable;
2075 }
2076 }
2077
2078 PDEBUG(3,printf("pmap_vac_me_harder: pmap %p Entries %d, "
2079 "writeable %d cacheable %d %s\n", pmap, entries, writeable,
2080 cacheable_entries, clear_cache ? "clean" : "no clean"));
2081
2082 /*
2083 * Enable or disable caching as necessary.
2084 * We do a quick check of the first PTE to avoid walking the list if
2085 * we're already in the right state.
2086 */
2087 if (entries > 1 && writeable) {
2088 if (cacheable_entries == 0)
2089 return;
2090 if (pv->pv_flags & PT_NC) {
2091 #ifdef DIAGNOSTIC
2092 /* We have cacheable entries, but the first one
2093 isn't among them. Something is wrong. */
2094 if (cacheable_entries)
2095 panic("pmap_vac_me_harder: "
2096 "cacheable inconsistent");
2097 #endif
2098 return;
2099 }
2100 pte = &ptes[arm_byte_to_page(pv->pv_va)];
2101 *pte &= ~(PT_C | PT_B);
2102 pv->pv_flags |= PT_NC;
2103 if (clear_cache && cacheable_entries < 4) {
2104 cpu_cache_purgeID_rng(pv->pv_va, NBPG);
2105 cpu_tlb_flushID_SE(pv->pv_va);
2106 }
2107 for (npv = pv->pv_next; npv; npv = npv->pv_next) {
2108 if (pmap == npv->pv_pmap &&
2109 (npv->pv_flags & PT_NC) == 0) {
2110 ptes[arm_byte_to_page(npv->pv_va)] &=
2111 ~(PT_C | PT_B);
2112 npv->pv_flags |= PT_NC;
2113 if (clear_cache && cacheable_entries < 4) {
2114 cpu_cache_purgeID_rng(npv->pv_va,
2115 NBPG);
2116 cpu_tlb_flushID_SE(npv->pv_va);
2117 }
2118 }
2119 }
2120 if (clear_cache && cacheable_entries >= 4) {
2121 cpu_cache_purgeID();
2122 cpu_tlb_flushID();
2123 }
2124 } else if (entries > 0) {
2125 if ((pv->pv_flags & PT_NC) == 0)
2126 return;
2127 pte = &ptes[arm_byte_to_page(pv->pv_va)];
2128 *pte |= (PT_C | PT_B);
2129 pv->pv_flags &= ~PT_NC;
2130 for (npv = pv->pv_next; npv; npv = npv->pv_next) {
2131 if (pmap == npv->pv_pmap &&
2132 (npv->pv_flags & PT_NC)) {
2133 ptes[arm_byte_to_page(npv->pv_va)] |=
2134 (PT_C | PT_B);
2135 npv->pv_flags &= ~PT_NC;
2136 }
2137 }
2138 }
2139 }
2140
2141 /*
2142 * pmap_remove()
2143 *
2144 * pmap_remove is responsible for nuking a number of mappings for a range
2145 * of virtual address space in the current pmap. To do this efficiently
2146 * is interesting, because in a number of cases a wide virtual address
2147 * range may be supplied that contains few actual mappings. So, the
2148 * optimisations are:
2149 * 1. Try and skip over hunks of address space for which an L1 entry
2150 * does not exist.
2151 * 2. Build up a list of pages we've hit, up to a maximum, so we can
2152 * maybe do just a partial cache clean. This path of execution is
2153 * complicated by the fact that the cache must be flushed _before_
2154 * the PTE is nuked, being a VAC :-)
2155 * 3. Maybe later fast-case a single page, but I don't think this is
2156 * going to make _that_ much difference overall.
2157 */
2158
2159 #define PMAP_REMOVE_CLEAN_LIST_SIZE 3
2160
2161 void
2162 pmap_remove(pmap, sva, eva)
2163 struct pmap *pmap;
2164 vaddr_t sva;
2165 vaddr_t eva;
2166 {
2167 int cleanlist_idx = 0;
2168 struct pagelist {
2169 vaddr_t va;
2170 pt_entry_t *pte;
2171 } cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
2172 pt_entry_t *pte = 0, *ptes;
2173 paddr_t pa;
2174 int pmap_active;
2175 struct pv_head *pvh;
2176
2177 /* Exit quick if there is no pmap */
2178 if (!pmap)
2179 return;
2180
2181 PDEBUG(0, printf("pmap_remove: pmap=%p sva=%08lx eva=%08lx\n", pmap, sva, eva));
2182
2183 sva &= PG_FRAME;
2184 eva &= PG_FRAME;
2185
2186 /*
2187 * we lock in the pmap => pv_head direction
2188 */
2189 PMAP_MAP_TO_HEAD_LOCK();
2190
2191 ptes = pmap_map_ptes(pmap);
2192 /* Get a page table pointer */
2193 while (sva < eva) {
2194 if (pmap_pde_v(pmap_pde(pmap, sva)))
2195 break;
2196 sva = (sva & PD_MASK) + NBPD;
2197 }
2198
2199 pte = &ptes[arm_byte_to_page(sva)];
2200 /* Note if the pmap is active thus require cache and tlb cleans */
2201 if ((curproc && curproc->p_vmspace->vm_map.pmap == pmap)
2202 || (pmap == pmap_kernel()))
2203 pmap_active = 1;
2204 else
2205 pmap_active = 0;
2206
2207 /* Now loop along */
2208 while (sva < eva) {
2209 /* Check if we can move to the next PDE (l1 chunk) */
2210 if (!(sva & PT_MASK))
2211 if (!pmap_pde_v(pmap_pde(pmap, sva))) {
2212 sva += NBPD;
2213 pte += arm_byte_to_page(NBPD);
2214 continue;
2215 }
2216
2217 /* We've found a valid PTE, so this page of PTEs has to go. */
2218 if (pmap_pte_v(pte)) {
2219 int bank, off;
2220
2221 /* Update statistics */
2222 --pmap->pm_stats.resident_count;
2223
2224 /*
2225 * Add this page to our cache remove list, if we can.
2226 * If, however the cache remove list is totally full,
2227 * then do a complete cache invalidation taking note
2228 * to backtrack the PTE table beforehand, and ignore
2229 * the lists in future because there's no longer any
2230 * point in bothering with them (we've paid the
2231 * penalty, so will carry on unhindered). Otherwise,
2232 * when we fall out, we just clean the list.
2233 */
2234 PDEBUG(10, printf("remove: inv pte at %p(%x) ", pte, *pte));
2235 pa = pmap_pte_pa(pte);
2236
2237 if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
2238 /* Add to the clean list. */
2239 cleanlist[cleanlist_idx].pte = pte;
2240 cleanlist[cleanlist_idx].va = sva;
2241 cleanlist_idx++;
2242 } else if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
2243 int cnt;
2244
2245 /* Nuke everything if needed. */
2246 if (pmap_active) {
2247 cpu_cache_purgeID();
2248 cpu_tlb_flushID();
2249 }
2250
2251 /*
2252 * Roll back the previous PTE list,
2253 * and zero out the current PTE.
2254 */
2255 for (cnt = 0; cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
2256 *cleanlist[cnt].pte = 0;
2257 pmap_pte_delref(pmap, cleanlist[cnt].va);
2258 }
2259 *pte = 0;
2260 pmap_pte_delref(pmap, sva);
2261 cleanlist_idx++;
2262 } else {
2263 /*
2264 * We've already nuked the cache and
2265 * TLB, so just carry on regardless,
2266 * and we won't need to do it again
2267 */
2268 *pte = 0;
2269 pmap_pte_delref(pmap, sva);
2270 }
2271
2272 /*
2273 * Update flags. In a number of circumstances,
2274 * we could cluster a lot of these and do a
2275 * number of sequential pages in one go.
2276 */
2277 if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
2278 struct pv_entry *pve;
2279 pvh = &vm_physmem[bank].pmseg.pvhead[off];
2280 simple_lock(&pvh->pvh_lock);
2281 pve = pmap_remove_pv(pvh, pmap, sva);
2282 pmap_free_pv(pmap, pve);
2283 pmap_vac_me_harder(pmap, pvh, ptes, FALSE);
2284 simple_unlock(&pvh->pvh_lock);
2285 }
2286 }
2287 sva += NBPG;
2288 pte++;
2289 }
2290
2291 pmap_unmap_ptes(pmap);
2292 /*
2293 * Now, if we've fallen through down to here, chances are that there
2294 * are less than PMAP_REMOVE_CLEAN_LIST_SIZE mappings left.
2295 */
2296 if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
2297 u_int cnt;
2298
2299 for (cnt = 0; cnt < cleanlist_idx; cnt++) {
2300 if (pmap_active) {
2301 cpu_cache_purgeID_rng(cleanlist[cnt].va, NBPG);
2302 *cleanlist[cnt].pte = 0;
2303 cpu_tlb_flushID_SE(cleanlist[cnt].va);
2304 } else
2305 *cleanlist[cnt].pte = 0;
2306 pmap_pte_delref(pmap, cleanlist[cnt].va);
2307 }
2308 }
2309 PMAP_MAP_TO_HEAD_UNLOCK();
2310 }
2311
2312 /*
2313 * Routine: pmap_remove_all
2314 * Function:
2315 * Removes this physical page from
2316 * all physical maps in which it resides.
2317 * Reflects back modify bits to the pager.
2318 */
2319
2320 void
2321 pmap_remove_all(pa)
2322 paddr_t pa;
2323 {
2324 struct pv_entry *pv, *npv;
2325 struct pv_head *pvh;
2326 struct pmap *pmap;
2327 pt_entry_t *pte, *ptes;
2328
2329 PDEBUG(0, printf("pmap_remove_all: pa=%lx ", pa));
2330
2331 /* set pv_head => pmap locking */
2332 PMAP_HEAD_TO_MAP_LOCK();
2333
2334 pvh = pmap_find_pvh(pa);
2335 simple_lock(&pvh->pvh_lock);
2336
2337 pv = pvh->pvh_list;
2338 if (pv == NULL)
2339 {
2340 PDEBUG(0, printf("free page\n"));
2341 simple_unlock(&pvh->pvh_lock);
2342 PMAP_HEAD_TO_MAP_UNLOCK();
2343 return;
2344 }
2345 pmap_clean_page(pv, FALSE);
2346
2347 while (pv) {
2348 pmap = pv->pv_pmap;
2349 ptes = pmap_map_ptes(pmap);
2350 pte = &ptes[arm_byte_to_page(pv->pv_va)];
2351
2352 PDEBUG(0, printf("[%p,%08x,%08lx,%08x] ", pmap, *pte,
2353 pv->pv_va, pv->pv_flags));
2354 #ifdef DEBUG
2355 if (!pmap_pde_v(pmap_pde(pmap, va)) || !pmap_pte_v(pte)
2356 || pmap_pte_pa(pte) != pa)
2357 panic("pmap_remove_all: bad mapping");
2358 #endif /* DEBUG */
2359
2360 /*
2361 * Update statistics
2362 */
2363 --pmap->pm_stats.resident_count;
2364
2365 /* Wired bit */
2366 if (pv->pv_flags & PT_W)
2367 --pmap->pm_stats.wired_count;
2368
2369 /*
2370 * Invalidate the PTEs.
2371 * XXX: should cluster them up and invalidate as many
2372 * as possible at once.
2373 */
2374
2375 #ifdef needednotdone
2376 reduce wiring count on page table pages as references drop
2377 #endif
2378
2379 *pte = 0;
2380 pmap_pte_delref(pmap, pv->pv_va);
2381
2382 npv = pv->pv_next;
2383 pmap_free_pv(pmap, pv);
2384 pv = npv;
2385 pmap_unmap_ptes(pmap);
2386 }
2387 pvh->pvh_list = NULL;
2388 simple_unlock(&pvh->pvh_lock);
2389 PMAP_HEAD_TO_MAP_UNLOCK();
2390
2391 PDEBUG(0, printf("done\n"));
2392 cpu_tlb_flushID();
2393 }
2394
2395
2396 /*
2397 * Set the physical protection on the specified range of this map as requested.
2398 */
2399
2400 void
2401 pmap_protect(pmap, sva, eva, prot)
2402 struct pmap *pmap;
2403 vaddr_t sva;
2404 vaddr_t eva;
2405 vm_prot_t prot;
2406 {
2407 pt_entry_t *pte = NULL, *ptes;
2408 int armprot;
2409 int flush = 0;
2410 paddr_t pa;
2411 int bank, off;
2412 struct pv_head *pvh;
2413
2414 PDEBUG(0, printf("pmap_protect: pmap=%p %08lx->%08lx %x\n",
2415 pmap, sva, eva, prot));
2416
2417 if (~prot & VM_PROT_READ) {
2418 /* Just remove the mappings. */
2419 pmap_remove(pmap, sva, eva);
2420 return;
2421 }
2422 if (prot & VM_PROT_WRITE) {
2423 /*
2424 * If this is a read->write transition, just ignore it and let
2425 * uvm_fault() take care of it later.
2426 */
2427 return;
2428 }
2429
2430 sva &= PG_FRAME;
2431 eva &= PG_FRAME;
2432
2433 /* Need to lock map->head */
2434 PMAP_MAP_TO_HEAD_LOCK();
2435
2436 ptes = pmap_map_ptes(pmap);
2437 /*
2438 * We need to acquire a pointer to a page table page before entering
2439 * the following loop.
2440 */
2441 while (sva < eva) {
2442 if (pmap_pde_v(pmap_pde(pmap, sva)))
2443 break;
2444 sva = (sva & PD_MASK) + NBPD;
2445 }
2446
2447 pte = &ptes[arm_byte_to_page(sva)];
2448
2449 while (sva < eva) {
2450 /* only check once in a while */
2451 if ((sva & PT_MASK) == 0) {
2452 if (!pmap_pde_v(pmap_pde(pmap, sva))) {
2453 /* We can race ahead here, to the next pde. */
2454 sva += NBPD;
2455 pte += arm_byte_to_page(NBPD);
2456 continue;
2457 }
2458 }
2459
2460 if (!pmap_pte_v(pte))
2461 goto next;
2462
2463 flush = 1;
2464
2465 armprot = 0;
2466 if (sva < VM_MAXUSER_ADDRESS)
2467 armprot |= PT_AP(AP_U);
2468 else if (sva < VM_MAX_ADDRESS)
2469 armprot |= PT_AP(AP_W); /* XXX Ekk what is this ? */
2470 *pte = (*pte & 0xfffff00f) | armprot;
2471
2472 pa = pmap_pte_pa(pte);
2473
2474 /* Get the physical page index */
2475
2476 /* Clear write flag */
2477 if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
2478 pvh = &vm_physmem[bank].pmseg.pvhead[off];
2479 simple_lock(&pvh->pvh_lock);
2480 (void) pmap_modify_pv(pmap, sva, pvh, PT_Wr, 0);
2481 pmap_vac_me_harder(pmap, pvh, ptes, FALSE);
2482 simple_unlock(&pvh->pvh_lock);
2483 }
2484
2485 next:
2486 sva += NBPG;
2487 pte++;
2488 }
2489 pmap_unmap_ptes(pmap);
2490 PMAP_MAP_TO_HEAD_UNLOCK();
2491 if (flush)
2492 cpu_tlb_flushID();
2493 }
2494
2495 /*
2496 * void pmap_enter(struct pmap *pmap, vaddr_t va, paddr_t pa, vm_prot_t prot,
2497 * int flags)
2498 *
2499 * Insert the given physical page (p) at
2500 * the specified virtual address (v) in the
2501 * target physical map with the protection requested.
2502 *
2503 * If specified, the page will be wired down, meaning
2504 * that the related pte can not be reclaimed.
2505 *
2506 * NB: This is the only routine which MAY NOT lazy-evaluate
2507 * or lose information. That is, this routine must actually
2508 * insert this page into the given map NOW.
2509 */
2510
2511 int
2512 pmap_enter(pmap, va, pa, prot, flags)
2513 struct pmap *pmap;
2514 vaddr_t va;
2515 paddr_t pa;
2516 vm_prot_t prot;
2517 int flags;
2518 {
2519 pt_entry_t *pte, *ptes;
2520 u_int npte;
2521 int bank, off;
2522 paddr_t opa;
2523 int nflags;
2524 boolean_t wired = (flags & PMAP_WIRED) != 0;
2525 struct pv_entry *pve;
2526 struct pv_head *pvh;
2527 int error;
2528
2529 PDEBUG(5, printf("pmap_enter: V%08lx P%08lx in pmap %p prot=%08x, wired = %d\n",
2530 va, pa, pmap, prot, wired));
2531
2532 #ifdef DIAGNOSTIC
2533 /* Valid address ? */
2534 if (va >= (KERNEL_VM_BASE + KERNEL_VM_SIZE))
2535 panic("pmap_enter: too big");
2536 if (pmap != pmap_kernel() && va != 0) {
2537 if (va < VM_MIN_ADDRESS || va >= VM_MAXUSER_ADDRESS)
2538 panic("pmap_enter: kernel page in user map");
2539 } else {
2540 if (va >= VM_MIN_ADDRESS && va < VM_MAXUSER_ADDRESS)
2541 panic("pmap_enter: user page in kernel map");
2542 if (va >= VM_MAXUSER_ADDRESS && va < VM_MAX_ADDRESS)
2543 panic("pmap_enter: entering PT page");
2544 }
2545 #endif
2546 /* get lock */
2547 PMAP_MAP_TO_HEAD_LOCK();
2548 /*
2549 * Get a pointer to the pte for this virtual address. If the
2550 * pte pointer is NULL then we are missing the L2 page table
2551 * so we need to create one.
2552 */
2553 pte = pmap_pte(pmap, va);
2554 if (!pte) {
2555 struct vm_page *ptp;
2556
2557 /* if failure is allowed then don't try too hard */
2558 ptp = pmap_get_ptp(pmap, va, flags & PMAP_CANFAIL);
2559 if (ptp == NULL) {
2560 if (flags & PMAP_CANFAIL) {
2561 error = ENOMEM;
2562 goto out;
2563 }
2564 panic("pmap_enter: get ptp failed");
2565 }
2566
2567 pte = pmap_pte(pmap, va);
2568 #ifdef DIAGNOSTIC
2569 if (!pte)
2570 panic("pmap_enter: no pte");
2571 #endif
2572 }
2573
2574 nflags = 0;
2575 if (prot & VM_PROT_WRITE)
2576 nflags |= PT_Wr;
2577 if (wired)
2578 nflags |= PT_W;
2579
2580 /* More debugging info */
2581 PDEBUG(5, printf("pmap_enter: pte for V%08lx = V%p (%08x)\n", va, pte,
2582 *pte));
2583
2584 /* Is the pte valid ? If so then this page is already mapped */
2585 if (pmap_pte_v(pte)) {
2586 /* Get the physical address of the current page mapped */
2587 opa = pmap_pte_pa(pte);
2588
2589 #ifdef MYCROFT_HACK
2590 printf("pmap_enter: pmap=%p va=%lx pa=%lx opa=%lx\n", pmap, va, pa, opa);
2591 #endif
2592
2593 /* Are we mapping the same page ? */
2594 if (opa == pa) {
2595 /* All we must be doing is changing the protection */
2596 PDEBUG(0, printf("Case 02 in pmap_enter (V%08lx P%08lx)\n",
2597 va, pa));
2598
2599 /* Has the wiring changed ? */
2600 if ((bank = vm_physseg_find(atop(pa), &off)) != -1) {
2601 pvh = &vm_physmem[bank].pmseg.pvhead[off];
2602 simple_lock(&pvh->pvh_lock);
2603 (void) pmap_modify_pv(pmap, va, pvh,
2604 PT_Wr | PT_W, nflags);
2605 simple_unlock(&pvh->pvh_lock);
2606 } else {
2607 pvh = NULL;
2608 }
2609 } else {
2610 /* We are replacing the page with a new one. */
2611 cpu_cache_purgeID_rng(va, NBPG);
2612
2613 PDEBUG(0, printf("Case 03 in pmap_enter (V%08lx P%08lx P%08lx)\n",
2614 va, pa, opa));
2615
2616 /*
2617 * If it is part of our managed memory then we
2618 * must remove it from the PV list
2619 */
2620 if ((bank = vm_physseg_find(atop(opa), &off)) != -1) {
2621 pvh = &vm_physmem[bank].pmseg.pvhead[off];
2622 simple_lock(&pvh->pvh_lock);
2623 pve = pmap_remove_pv(pvh, pmap, va);
2624 simple_unlock(&pvh->pvh_lock);
2625 } else {
2626 pve = NULL;
2627 }
2628
2629 goto enter;
2630 }
2631 } else {
2632 opa = 0;
2633 pve = NULL;
2634 pmap_pte_addref(pmap, va);
2635
2636 /* pte is not valid so we must be hooking in a new page */
2637 ++pmap->pm_stats.resident_count;
2638
2639 enter:
2640 /*
2641 * Enter on the PV list if part of our managed memory
2642 */
2643 bank = vm_physseg_find(atop(pa), &off);
2644
2645 if (pmap_initialized && (bank != -1)) {
2646 pvh = &vm_physmem[bank].pmseg.pvhead[off];
2647 if (pve == NULL) {
2648 pve = pmap_alloc_pv(pmap, ALLOCPV_NEED);
2649 if (pve == NULL) {
2650 if (flags & PMAP_CANFAIL) {
2651 error = ENOMEM;
2652 goto out;
2653 }
2654 panic("pmap_enter: no pv entries available");
2655 }
2656 }
2657 /* enter_pv locks pvh when adding */
2658 pmap_enter_pv(pvh, pve, pmap, va, NULL, nflags);
2659 } else {
2660 pvh = NULL;
2661 if (pve != NULL)
2662 pmap_free_pv(pmap, pve);
2663 }
2664 }
2665
2666 #ifdef MYCROFT_HACK
2667 if (mycroft_hack)
2668 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);
2669 #endif
2670
2671 /* Construct the pte, giving the correct access. */
2672 npte = (pa & PG_FRAME);
2673
2674 /* VA 0 is magic. */
2675 if (pmap != pmap_kernel() && va != 0)
2676 npte |= PT_AP(AP_U);
2677
2678 if (pmap_initialized && bank != -1) {
2679 #ifdef DIAGNOSTIC
2680 if ((flags & VM_PROT_ALL) & ~prot)
2681 panic("pmap_enter: access_type exceeds prot");
2682 #endif
2683 npte |= PT_C | PT_B;
2684 if (flags & VM_PROT_WRITE) {
2685 npte |= L2_SPAGE | PT_AP(AP_W);
2686 vm_physmem[bank].pmseg.attrs[off] |= PT_H | PT_M;
2687 } else if (flags & VM_PROT_ALL) {
2688 npte |= L2_SPAGE;
2689 vm_physmem[bank].pmseg.attrs[off] |= PT_H;
2690 } else
2691 npte |= L2_INVAL;
2692 } else {
2693 if (prot & VM_PROT_WRITE)
2694 npte |= L2_SPAGE | PT_AP(AP_W);
2695 else if (prot & VM_PROT_ALL)
2696 npte |= L2_SPAGE;
2697 else
2698 npte |= L2_INVAL;
2699 }
2700
2701 #ifdef MYCROFT_HACK
2702 if (mycroft_hack)
2703 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);
2704 #endif
2705
2706 *pte = npte;
2707
2708 if (pmap_initialized && bank != -1)
2709 {
2710 boolean_t pmap_active = FALSE;
2711 /* XXX this will change once the whole of pmap_enter uses
2712 * map_ptes
2713 */
2714 ptes = pmap_map_ptes(pmap);
2715 if ((curproc && curproc->p_vmspace->vm_map.pmap == pmap)
2716 || (pmap == pmap_kernel()))
2717 pmap_active = TRUE;
2718 simple_lock(&pvh->pvh_lock);
2719 pmap_vac_me_harder(pmap, pvh, ptes, pmap_active);
2720 simple_unlock(&pvh->pvh_lock);
2721 pmap_unmap_ptes(pmap);
2722 }
2723
2724 /* Better flush the TLB ... */
2725 cpu_tlb_flushID_SE(va);
2726 error = 0;
2727 out:
2728 PMAP_MAP_TO_HEAD_UNLOCK();
2729 PDEBUG(5, printf("pmap_enter: pte = V%p %08x\n", pte, *pte));
2730
2731 return error;
2732 }
2733
2734 void
2735 pmap_kenter_pa(va, pa, prot)
2736 vaddr_t va;
2737 paddr_t pa;
2738 vm_prot_t prot;
2739 {
2740 struct pmap *pmap = pmap_kernel();
2741 pt_entry_t *pte;
2742 struct vm_page *pg;
2743
2744 if (!pmap_pde_v(pmap_pde(pmap, va))) {
2745
2746 /*
2747 * For the kernel pmaps it would be better to ensure
2748 * that they are always present, and to grow the
2749 * kernel as required.
2750 */
2751
2752 /* Allocate a page table */
2753 pg = uvm_pagealloc(&(pmap_kernel()->pm_obj), 0, NULL,
2754 UVM_PGA_USERESERVE | UVM_PGA_ZERO);
2755 if (pg == NULL) {
2756 panic("pmap_kenter_pa: no free pages");
2757 }
2758 pg->flags &= ~PG_BUSY; /* never busy */
2759
2760 /* Wire this page table into the L1. */
2761 pmap_map_in_l1(pmap, va, VM_PAGE_TO_PHYS(pg), TRUE);
2762 }
2763 pte = vtopte(va);
2764 KASSERT(!pmap_pte_v(pte));
2765 *pte = L2_PTE(pa, AP_KRW);
2766 }
2767
2768 void
2769 pmap_kremove(va, len)
2770 vaddr_t va;
2771 vsize_t len;
2772 {
2773 pt_entry_t *pte;
2774
2775 for (len >>= PAGE_SHIFT; len > 0; len--, va += PAGE_SIZE) {
2776
2777 /*
2778 * We assume that we will only be called with small
2779 * regions of memory.
2780 */
2781
2782 KASSERT(pmap_pde_v(pmap_pde(pmap_kernel(), va)));
2783 pte = vtopte(va);
2784 cpu_cache_purgeID_rng(va, PAGE_SIZE);
2785 *pte = 0;
2786 cpu_tlb_flushID_SE(va);
2787 }
2788 }
2789
2790 /*
2791 * pmap_page_protect:
2792 *
2793 * Lower the permission for all mappings to a given page.
2794 */
2795
2796 void
2797 pmap_page_protect(pg, prot)
2798 struct vm_page *pg;
2799 vm_prot_t prot;
2800 {
2801 paddr_t pa = VM_PAGE_TO_PHYS(pg);
2802
2803 PDEBUG(0, printf("pmap_page_protect(pa=%lx, prot=%d)\n", pa, prot));
2804
2805 switch(prot) {
2806 case VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE:
2807 case VM_PROT_READ|VM_PROT_WRITE:
2808 return;
2809
2810 case VM_PROT_READ:
2811 case VM_PROT_READ|VM_PROT_EXECUTE:
2812 pmap_copy_on_write(pa);
2813 break;
2814
2815 default:
2816 pmap_remove_all(pa);
2817 break;
2818 }
2819 }
2820
2821
2822 /*
2823 * Routine: pmap_unwire
2824 * Function: Clear the wired attribute for a map/virtual-address
2825 * pair.
2826 * In/out conditions:
2827 * The mapping must already exist in the pmap.
2828 */
2829
2830 void
2831 pmap_unwire(pmap, va)
2832 struct pmap *pmap;
2833 vaddr_t va;
2834 {
2835 pt_entry_t *pte;
2836 paddr_t pa;
2837 int bank, off;
2838 struct pv_head *pvh;
2839
2840 /*
2841 * Make sure pmap is valid. -dct
2842 */
2843 if (pmap == NULL)
2844 return;
2845
2846 /* Get the pte */
2847 pte = pmap_pte(pmap, va);
2848 if (!pte)
2849 return;
2850
2851 /* Extract the physical address of the page */
2852 pa = pmap_pte_pa(pte);
2853
2854 if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
2855 return;
2856 pvh = &vm_physmem[bank].pmseg.pvhead[off];
2857 simple_lock(&pvh->pvh_lock);
2858 /* Update the wired bit in the pv entry for this page. */
2859 (void) pmap_modify_pv(pmap, va, pvh, PT_W, 0);
2860 simple_unlock(&pvh->pvh_lock);
2861 }
2862
2863 /*
2864 * pt_entry_t *pmap_pte(struct pmap *pmap, vaddr_t va)
2865 *
2866 * Return the pointer to a page table entry corresponding to the supplied
2867 * virtual address.
2868 *
2869 * The page directory is first checked to make sure that a page table
2870 * for the address in question exists and if it does a pointer to the
2871 * entry is returned.
2872 *
2873 * The way this works is that that the kernel page tables are mapped
2874 * into the memory map at ALT_PAGE_TBLS_BASE to ALT_PAGE_TBLS_BASE+4MB.
2875 * This allows page tables to be located quickly.
2876 */
2877 pt_entry_t *
2878 pmap_pte(pmap, va)
2879 struct pmap *pmap;
2880 vaddr_t va;
2881 {
2882 pt_entry_t *ptp;
2883 pt_entry_t *result;
2884
2885 /* The pmap must be valid */
2886 if (!pmap)
2887 return(NULL);
2888
2889 /* Return the address of the pte */
2890 PDEBUG(10, printf("pmap_pte: pmap=%p va=V%08lx pde = V%p (%08X)\n",
2891 pmap, va, pmap_pde(pmap, va), *(pmap_pde(pmap, va))));
2892
2893 /* Do we have a valid pde ? If not we don't have a page table */
2894 if (!pmap_pde_v(pmap_pde(pmap, va))) {
2895 PDEBUG(0, printf("pmap_pte: failed - pde = %p\n",
2896 pmap_pde(pmap, va)));
2897 return(NULL);
2898 }
2899
2900 PDEBUG(10, printf("pmap pagetable = P%08lx current = P%08x\n",
2901 pmap->pm_pptpt, (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
2902 + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
2903 (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)));
2904
2905 /*
2906 * If the pmap is the kernel pmap or the pmap is the active one
2907 * then we can just return a pointer to entry relative to
2908 * PROCESS_PAGE_TBLS_BASE.
2909 * Otherwise we need to map the page tables to an alternative
2910 * address and reference them there.
2911 */
2912 if (pmap == pmap_kernel() || pmap->pm_pptpt
2913 == (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
2914 + ((PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) &
2915 ~3) + (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)) {
2916 ptp = (pt_entry_t *)PROCESS_PAGE_TBLS_BASE;
2917 } else {
2918 struct proc *p = curproc;
2919
2920 /* If we don't have a valid curproc use proc0 */
2921 /* Perhaps we should just use kernel_pmap instead */
2922 if (p == NULL)
2923 p = &proc0;
2924 #ifdef DIAGNOSTIC
2925 /*
2926 * The pmap should always be valid for the process so
2927 * panic if it is not.
2928 */
2929 if (!p->p_vmspace || !p->p_vmspace->vm_map.pmap) {
2930 printf("pmap_pte: va=%08lx p=%p vm=%p\n",
2931 va, p, p->p_vmspace);
2932 console_debugger();
2933 }
2934 /*
2935 * The pmap for the current process should be mapped. If it
2936 * is not then we have a problem.
2937 */
2938 if (p->p_vmspace->vm_map.pmap->pm_pptpt !=
2939 (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
2940 + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
2941 (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) & PG_FRAME)) {
2942 printf("pmap pagetable = P%08lx current = P%08x ",
2943 pmap->pm_pptpt, (*((pt_entry_t *)(PROCESS_PAGE_TBLS_BASE
2944 + (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT - 2)) +
2945 (PROCESS_PAGE_TBLS_BASE >> PDSHIFT))) &
2946 PG_FRAME));
2947 printf("pptpt=%lx\n", p->p_vmspace->vm_map.pmap->pm_pptpt);
2948 panic("pmap_pte: current and pmap mismatch\n");
2949 }
2950 #endif
2951
2952 ptp = (pt_entry_t *)ALT_PAGE_TBLS_BASE;
2953 pmap_map_in_l1(p->p_vmspace->vm_map.pmap, ALT_PAGE_TBLS_BASE,
2954 pmap->pm_pptpt, FALSE);
2955 cpu_tlb_flushD();
2956 }
2957 PDEBUG(10, printf("page tables base = %p offset=%lx\n", ptp,
2958 ((va >> (PGSHIFT-2)) & ~3)));
2959 result = (pt_entry_t *)((char *)ptp + ((va >> (PGSHIFT-2)) & ~3));
2960 return(result);
2961 }
2962
2963 /*
2964 * Routine: pmap_extract
2965 * Function:
2966 * Extract the physical page address associated
2967 * with the given map/virtual_address pair.
2968 */
2969 boolean_t
2970 pmap_extract(pmap, va, pap)
2971 struct pmap *pmap;
2972 vaddr_t va;
2973 paddr_t *pap;
2974 {
2975 pt_entry_t *pte, *ptes;
2976 paddr_t pa;
2977
2978 PDEBUG(5, printf("pmap_extract: pmap=%p, va=V%08lx\n", pmap, va));
2979
2980 /*
2981 * Get the pte for this virtual address.
2982 */
2983 ptes = pmap_map_ptes(pmap);
2984 pte = &ptes[arm_byte_to_page(va)];
2985
2986 /*
2987 * If there is no pte then there is no page table etc.
2988 * Is the pte valid ? If not then no paged is actually mapped here
2989 */
2990 if (!pmap_pde_v(pmap_pde(pmap, va)) || !pmap_pte_v(pte)){
2991 pmap_unmap_ptes(pmap);
2992 return (FALSE);
2993 }
2994
2995 /* Return the physical address depending on the PTE type */
2996 /* XXX What about L1 section mappings ? */
2997 if ((*(pte) & L2_MASK) == L2_LPAGE) {
2998 /* Extract the physical address from the pte */
2999 pa = (*(pte)) & ~(L2_LPAGE_SIZE - 1);
3000
3001 PDEBUG(5, printf("pmap_extract: LPAGE pa = P%08lx\n",
3002 (pa | (va & (L2_LPAGE_SIZE - 1)))));
3003
3004 if (pap != NULL)
3005 *pap = pa | (va & (L2_LPAGE_SIZE - 1));
3006 } else {
3007 /* Extract the physical address from the pte */
3008 pa = pmap_pte_pa(pte);
3009
3010 PDEBUG(5, printf("pmap_extract: SPAGE pa = P%08lx\n",
3011 (pa | (va & ~PG_FRAME))));
3012
3013 if (pap != NULL)
3014 *pap = pa | (va & ~PG_FRAME);
3015 }
3016 pmap_unmap_ptes(pmap);
3017 return (TRUE);
3018 }
3019
3020
3021 /*
3022 * Copy the range specified by src_addr/len from the source map to the
3023 * range dst_addr/len in the destination map.
3024 *
3025 * This routine is only advisory and need not do anything.
3026 */
3027
3028 void
3029 pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr)
3030 struct pmap *dst_pmap;
3031 struct pmap *src_pmap;
3032 vaddr_t dst_addr;
3033 vsize_t len;
3034 vaddr_t src_addr;
3035 {
3036 PDEBUG(0, printf("pmap_copy(%p, %p, %lx, %lx, %lx)\n",
3037 dst_pmap, src_pmap, dst_addr, len, src_addr));
3038 }
3039
3040 #if defined(PMAP_DEBUG)
3041 void
3042 pmap_dump_pvlist(phys, m)
3043 vaddr_t phys;
3044 char *m;
3045 {
3046 struct pv_head *pvh;
3047 struct pv_entry *pv;
3048 int bank, off;
3049
3050 if ((bank = vm_physseg_find(atop(phys), &off)) == -1) {
3051 printf("INVALID PA\n");
3052 return;
3053 }
3054 pvh = &vm_physmem[bank].pmseg.pvhead[off];
3055 simple_lock(&pvh->pvh_lock);
3056 printf("%s %08lx:", m, phys);
3057 if (pvh->pvh_list == NULL) {
3058 printf(" no mappings\n");
3059 return;
3060 }
3061
3062 for (pv = pvh->pvh_list; pv; pv = pv->pv_next)
3063 printf(" pmap %p va %08lx flags %08x", pv->pv_pmap,
3064 pv->pv_va, pv->pv_flags);
3065
3066 printf("\n");
3067 simple_unlock(&pvh->pvh_lock);
3068 }
3069
3070 #endif /* PMAP_DEBUG */
3071
3072 boolean_t
3073 pmap_testbit(pa, setbits)
3074 paddr_t pa;
3075 int setbits;
3076 {
3077 int bank, off;
3078
3079 PDEBUG(1, printf("pmap_testbit: pa=%08lx set=%08x\n", pa, setbits));
3080
3081 if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
3082 return(FALSE);
3083
3084 /*
3085 * Check saved info only
3086 */
3087 if (vm_physmem[bank].pmseg.attrs[off] & setbits) {
3088 PDEBUG(0, printf("pmap_attributes = %02x\n",
3089 vm_physmem[bank].pmseg.attrs[off]));
3090 return(TRUE);
3091 }
3092
3093 return(FALSE);
3094 }
3095
3096 static pt_entry_t *
3097 pmap_map_ptes(struct pmap *pmap)
3098 {
3099 struct proc *p;
3100
3101 /* the kernel's pmap is always accessible */
3102 if (pmap == pmap_kernel()) {
3103 return (pt_entry_t *)PROCESS_PAGE_TBLS_BASE ;
3104 }
3105
3106 if (pmap_is_curpmap(pmap)) {
3107 simple_lock(&pmap->pm_obj.vmobjlock);
3108 return (pt_entry_t *)PROCESS_PAGE_TBLS_BASE;
3109 }
3110
3111 p = curproc;
3112
3113 if (p == NULL)
3114 p = &proc0;
3115
3116 /* need to lock both curpmap and pmap: use ordered locking */
3117 if ((unsigned) pmap < (unsigned) curproc->p_vmspace->vm_map.pmap) {
3118 simple_lock(&pmap->pm_obj.vmobjlock);
3119 simple_lock(&curproc->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
3120 } else {
3121 simple_lock(&curproc->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
3122 simple_lock(&pmap->pm_obj.vmobjlock);
3123 }
3124
3125 pmap_map_in_l1(p->p_vmspace->vm_map.pmap, ALT_PAGE_TBLS_BASE,
3126 pmap->pm_pptpt, FALSE);
3127 cpu_tlb_flushD();
3128 return (pt_entry_t *)ALT_PAGE_TBLS_BASE;
3129 }
3130
3131 /*
3132 * pmap_unmap_ptes: unlock the PTE mapping of "pmap"
3133 */
3134
3135 static void
3136 pmap_unmap_ptes(pmap)
3137 struct pmap *pmap;
3138 {
3139 if (pmap == pmap_kernel()) {
3140 return;
3141 }
3142 if (pmap_is_curpmap(pmap)) {
3143 simple_unlock(&pmap->pm_obj.vmobjlock);
3144 } else {
3145 simple_unlock(&pmap->pm_obj.vmobjlock);
3146 simple_unlock(&curproc->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
3147 }
3148 }
3149
3150 /*
3151 * Modify pte bits for all ptes corresponding to the given physical address.
3152 * We use `maskbits' rather than `clearbits' because we're always passing
3153 * constants and the latter would require an extra inversion at run-time.
3154 */
3155
3156 void
3157 pmap_clearbit(pa, maskbits)
3158 paddr_t pa;
3159 int maskbits;
3160 {
3161 struct pv_entry *pv;
3162 struct pv_head *pvh;
3163 pt_entry_t *pte;
3164 vaddr_t va;
3165 int bank, off, tlbentry;
3166
3167 PDEBUG(1, printf("pmap_clearbit: pa=%08lx mask=%08x\n",
3168 pa, maskbits));
3169
3170 tlbentry = 0;
3171
3172 if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
3173 return;
3174 PMAP_HEAD_TO_MAP_LOCK();
3175 pvh = &vm_physmem[bank].pmseg.pvhead[off];
3176 simple_lock(&pvh->pvh_lock);
3177
3178 /*
3179 * Clear saved attributes (modify, reference)
3180 */
3181 vm_physmem[bank].pmseg.attrs[off] &= ~maskbits;
3182
3183 if (pvh->pvh_list == NULL) {
3184 simple_unlock(&pvh->pvh_lock);
3185 PMAP_HEAD_TO_MAP_UNLOCK();
3186 return;
3187 }
3188
3189 /*
3190 * Loop over all current mappings setting/clearing as appropos
3191 */
3192 for (pv = pvh->pvh_list; pv; pv = pv->pv_next) {
3193 va = pv->pv_va;
3194
3195 /*
3196 * XXX don't write protect pager mappings
3197 */
3198 if (va >= uvm.pager_sva && va < uvm.pager_eva) {
3199 printf("pmap_clearbit: found page VA on pv_list\n");
3200 continue;
3201 }
3202
3203 pv->pv_flags &= ~maskbits;
3204 pte = pmap_pte(pv->pv_pmap, va);
3205 KASSERT(pte != NULL);
3206 if (maskbits & (PT_Wr|PT_M))
3207 {
3208 if ((pv->pv_flags & PT_NC))
3209 {
3210 /*
3211 * entry is not cacheable, so reenable the cache,
3212 * nothing to flush
3213 */
3214 *pte |= (PT_C | PT_B);
3215 pv->pv_flags &= ~PT_NC;
3216 } else {
3217 /*
3218 * entry is cacheable check if pmap is current if it
3219 * is flush it, otherwise it won't be in the cache
3220 */
3221 if (pmap_is_curpmap(pv->pv_pmap))
3222 {
3223 /* entry is in current pmap purge it */
3224 cpu_cache_purgeID_rng(pv->pv_va, NBPG);
3225 }
3226 }
3227
3228 /* make the pte read only */
3229 *pte &= ~PT_AP(AP_W);
3230
3231 if (pmap_is_curpmap(pv->pv_pmap))
3232 /*
3233 * if we had cacheable pte's we'd clean the pte out to
3234 * memory here
3235 */
3236 /*
3237 * flush tlb entry as it's in the current pmap
3238 */
3239 cpu_tlb_flushID_SE(pv->pv_va);
3240
3241 }
3242 if (maskbits & PT_H)
3243 *pte = (*pte & ~L2_MASK) | L2_INVAL;
3244 }
3245 simple_unlock(&pvh->pvh_lock);
3246 PMAP_HEAD_TO_MAP_UNLOCK();
3247 }
3248
3249
3250 boolean_t
3251 pmap_clear_modify(pg)
3252 struct vm_page *pg;
3253 {
3254 paddr_t pa = VM_PAGE_TO_PHYS(pg);
3255 boolean_t rv;
3256
3257 PDEBUG(0, printf("pmap_clear_modify pa=%08lx\n", pa));
3258 rv = pmap_testbit(pa, PT_M);
3259 pmap_clearbit(pa, PT_M);
3260 return rv;
3261 }
3262
3263
3264 boolean_t
3265 pmap_clear_reference(pg)
3266 struct vm_page *pg;
3267 {
3268 paddr_t pa = VM_PAGE_TO_PHYS(pg);
3269 boolean_t rv;
3270
3271 PDEBUG(0, printf("pmap_clear_reference pa=%08lx\n", pa));
3272 rv = pmap_testbit(pa, PT_H);
3273 pmap_clearbit(pa, PT_H);
3274 return rv;
3275 }
3276
3277
3278 void
3279 pmap_copy_on_write(pa)
3280 paddr_t pa;
3281 {
3282 PDEBUG(0, printf("pmap_copy_on_write pa=%08lx\n", pa));
3283 pmap_clearbit(pa, PT_Wr);
3284 }
3285
3286
3287 boolean_t
3288 pmap_is_modified(pg)
3289 struct vm_page *pg;
3290 {
3291 paddr_t pa = VM_PAGE_TO_PHYS(pg);
3292 boolean_t result;
3293
3294 result = pmap_testbit(pa, PT_M);
3295 PDEBUG(1, printf("pmap_is_modified pa=%08lx %x\n", pa, result));
3296 return (result);
3297 }
3298
3299
3300 boolean_t
3301 pmap_is_referenced(pg)
3302 struct vm_page *pg;
3303 {
3304 paddr_t pa = VM_PAGE_TO_PHYS(pg);
3305 boolean_t result;
3306
3307 result = pmap_testbit(pa, PT_H);
3308 PDEBUG(0, printf("pmap_is_referenced pa=%08lx %x\n", pa, result));
3309 return (result);
3310 }
3311
3312
3313 int
3314 pmap_modified_emulation(pmap, va)
3315 struct pmap *pmap;
3316 vaddr_t va;
3317 {
3318 pt_entry_t *pte;
3319 paddr_t pa;
3320 int bank, off;
3321 struct pv_head *pvh;
3322 u_int flags;
3323
3324 PDEBUG(2, printf("pmap_modified_emulation\n"));
3325
3326 /* Get the pte */
3327 pte = pmap_pte(pmap, va);
3328 if (!pte) {
3329 PDEBUG(2, printf("no pte\n"));
3330 return(0);
3331 }
3332
3333 PDEBUG(1, printf("*pte=%08x\n", *pte));
3334
3335 /* Check for a zero pte */
3336 if (*pte == 0)
3337 return(0);
3338
3339 /* This can happen if user code tries to access kernel memory. */
3340 if ((*pte & PT_AP(AP_W)) != 0)
3341 return (0);
3342
3343 /* Extract the physical address of the page */
3344 pa = pmap_pte_pa(pte);
3345 if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
3346 return(0);
3347
3348 PMAP_HEAD_TO_MAP_LOCK();
3349 /* Get the current flags for this page. */
3350 pvh = &vm_physmem[bank].pmseg.pvhead[off];
3351 /* XXX: needed if we hold head->map lock? */
3352 simple_lock(&pvh->pvh_lock);
3353
3354 flags = pmap_modify_pv(pmap, va, pvh, 0, 0);
3355 PDEBUG(2, printf("pmap_modified_emulation: flags = %08x\n", flags));
3356
3357 /*
3358 * Do the flags say this page is writable ? If not then it is a
3359 * genuine write fault. If yes then the write fault is our fault
3360 * as we did not reflect the write access in the PTE. Now we know
3361 * a write has occurred we can correct this and also set the
3362 * modified bit
3363 */
3364 if (~flags & PT_Wr) {
3365 simple_unlock(&pvh->pvh_lock);
3366 PMAP_HEAD_TO_MAP_UNLOCK();
3367 return(0);
3368 }
3369
3370 PDEBUG(0, printf("pmap_modified_emulation: Got a hit va=%08lx, pte = %p (%08x)\n",
3371 va, pte, *pte));
3372 vm_physmem[bank].pmseg.attrs[off] |= PT_H | PT_M;
3373 *pte = (*pte & ~L2_MASK) | L2_SPAGE | PT_AP(AP_W);
3374 PDEBUG(0, printf("->(%08x)\n", *pte));
3375
3376 simple_unlock(&pvh->pvh_lock);
3377 PMAP_HEAD_TO_MAP_UNLOCK();
3378 /* Return, indicating the problem has been dealt with */
3379 cpu_tlb_flushID_SE(va);
3380 return(1);
3381 }
3382
3383
3384 int
3385 pmap_handled_emulation(pmap, va)
3386 struct pmap *pmap;
3387 vaddr_t va;
3388 {
3389 pt_entry_t *pte;
3390 paddr_t pa;
3391 int bank, off;
3392
3393 PDEBUG(2, printf("pmap_handled_emulation\n"));
3394
3395 /* Get the pte */
3396 pte = pmap_pte(pmap, va);
3397 if (!pte) {
3398 PDEBUG(2, printf("no pte\n"));
3399 return(0);
3400 }
3401
3402 PDEBUG(1, printf("*pte=%08x\n", *pte));
3403
3404 /* Check for a zero pte */
3405 if (*pte == 0)
3406 return(0);
3407
3408 /* This can happen if user code tries to access kernel memory. */
3409 if ((*pte & L2_MASK) != L2_INVAL)
3410 return (0);
3411
3412 /* Extract the physical address of the page */
3413 pa = pmap_pte_pa(pte);
3414 if ((bank = vm_physseg_find(atop(pa), &off)) == -1)
3415 return(0);
3416
3417 /*
3418 * Ok we just enable the pte and mark the attibs as handled
3419 */
3420 PDEBUG(0, printf("pmap_handled_emulation: Got a hit va=%08lx pte = %p (%08x)\n",
3421 va, pte, *pte));
3422 vm_physmem[bank].pmseg.attrs[off] |= PT_H;
3423 *pte = (*pte & ~L2_MASK) | L2_SPAGE;
3424 PDEBUG(0, printf("->(%08x)\n", *pte));
3425
3426 /* Return, indicating the problem has been dealt with */
3427 cpu_tlb_flushID_SE(va);
3428 return(1);
3429 }
3430
3431
3432
3433
3434 /*
3435 * pmap_collect: free resources held by a pmap
3436 *
3437 * => optional function.
3438 * => called when a process is swapped out to free memory.
3439 */
3440
3441 void
3442 pmap_collect(pmap)
3443 struct pmap *pmap;
3444 {
3445 }
3446
3447 /*
3448 * Routine: pmap_procwr
3449 *
3450 * Function:
3451 * Synchronize caches corresponding to [addr, addr+len) in p.
3452 *
3453 */
3454 void
3455 pmap_procwr(p, va, len)
3456 struct proc *p;
3457 vaddr_t va;
3458 int len;
3459 {
3460 /* We only need to do anything if it is the current process. */
3461 if (p == curproc)
3462 cpu_cache_syncI_rng(va, len);
3463 }
3464 /*
3465 * PTP functions
3466 */
3467
3468 /*
3469 * pmap_steal_ptp: Steal a PTP from somewhere else.
3470 *
3471 * This is just a placeholder, for now we never steal.
3472 */
3473
3474 static struct vm_page *
3475 pmap_steal_ptp(struct pmap *pmap, vaddr_t va)
3476 {
3477 return (NULL);
3478 }
3479
3480 /*
3481 * pmap_get_ptp: get a PTP (if there isn't one, allocate a new one)
3482 *
3483 * => pmap should NOT be pmap_kernel()
3484 * => pmap should be locked
3485 */
3486
3487 static struct vm_page *
3488 pmap_get_ptp(struct pmap *pmap, vaddr_t va, boolean_t just_try)
3489 {
3490 struct vm_page *ptp;
3491
3492 if (pmap_pde_v(pmap_pde(pmap, va))) {
3493
3494 /* valid... check hint (saves us a PA->PG lookup) */
3495 #if 0
3496 if (pmap->pm_ptphint &&
3497 ((unsigned)pmap_pde(pmap, va) & PG_FRAME) ==
3498 VM_PAGE_TO_PHYS(pmap->pm_ptphint))
3499 return (pmap->pm_ptphint);
3500 #endif
3501 ptp = uvm_pagelookup(&pmap->pm_obj, va);
3502 #ifdef DIAGNOSTIC
3503 if (ptp == NULL)
3504 panic("pmap_get_ptp: unmanaged user PTP");
3505 #endif
3506 // pmap->pm_ptphint = ptp;
3507 return(ptp);
3508 }
3509
3510 /* allocate a new PTP (updates ptphint) */
3511 return(pmap_alloc_ptp(pmap, va, just_try));
3512 }
3513
3514 /*
3515 * pmap_alloc_ptp: allocate a PTP for a PMAP
3516 *
3517 * => pmap should already be locked by caller
3518 * => we use the ptp's wire_count to count the number of active mappings
3519 * in the PTP (we start it at one to prevent any chance this PTP
3520 * will ever leak onto the active/inactive queues)
3521 */
3522
3523 /*__inline */ static struct vm_page *
3524 pmap_alloc_ptp(struct pmap *pmap, vaddr_t va, boolean_t just_try)
3525 {
3526 struct vm_page *ptp;
3527
3528 ptp = uvm_pagealloc(&pmap->pm_obj, va, NULL,
3529 UVM_PGA_USERESERVE|UVM_PGA_ZERO);
3530 if (ptp == NULL) {
3531 if (just_try)
3532 return (NULL);
3533
3534 ptp = pmap_steal_ptp(pmap, va);
3535
3536 if (ptp == NULL)
3537 return (NULL);
3538 /* Stole a page, zero it. */
3539 pmap_zero_page(VM_PAGE_TO_PHYS(ptp));
3540 }
3541
3542 /* got one! */
3543 ptp->flags &= ~PG_BUSY; /* never busy */
3544 ptp->wire_count = 1; /* no mappings yet */
3545 pmap_map_in_l1(pmap, va, VM_PAGE_TO_PHYS(ptp), TRUE);
3546 pmap->pm_stats.resident_count++; /* count PTP as resident */
3547 // pmap->pm_ptphint = ptp;
3548 return (ptp);
3549 }
3550
3551 /* End of pmap.c */
3552