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