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