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