pmap.c revision 1.101 1 /* $NetBSD: pmap.c,v 1.101 2002/07/30 16:16:39 thorpej 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.101 2002/07/30 16:16:39 thorpej 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 }
817
818 /*
819 * pmap_remove_pv: try to remove a mapping from a pv_list
820 *
821 * => caller should hold proper lock on pmap_main_lock
822 * => pmap should be locked
823 * => caller should hold lock on vm_page [so that attrs can be adjusted]
824 * => caller should adjust ptp's wire_count and free PTP if needed
825 * => caller should NOT adjust pmap's wire_count
826 * => we return the removed pve
827 */
828
829 __inline static struct pv_entry *
830 pmap_remove_pv(struct vm_page *pg, struct pmap *pmap, vaddr_t va)
831 {
832 struct pv_entry *pve, **prevptr;
833
834 prevptr = &pg->mdpage.pvh_list; /* previous pv_entry pointer */
835 pve = *prevptr;
836 while (pve) {
837 if (pve->pv_pmap == pmap && pve->pv_va == va) { /* match? */
838 *prevptr = pve->pv_next; /* remove it! */
839 if (pve->pv_flags & PVF_WIRED)
840 --pmap->pm_stats.wired_count;
841 break;
842 }
843 prevptr = &pve->pv_next; /* previous pointer */
844 pve = pve->pv_next; /* advance */
845 }
846 return(pve); /* return removed pve */
847 }
848
849 /*
850 *
851 * pmap_modify_pv: Update pv flags
852 *
853 * => caller should hold lock on vm_page [so that attrs can be adjusted]
854 * => caller should NOT adjust pmap's wire_count
855 * => caller must call pmap_vac_me_harder() if writable status of a page
856 * may have changed.
857 * => we return the old flags
858 *
859 * Modify a physical-virtual mapping in the pv table
860 */
861
862 static /* __inline */ u_int
863 pmap_modify_pv(struct pmap *pmap, vaddr_t va, struct vm_page *pg,
864 u_int bic_mask, u_int eor_mask)
865 {
866 struct pv_entry *npv;
867 u_int flags, oflags;
868
869 /*
870 * There is at least one VA mapping this page.
871 */
872
873 for (npv = pg->mdpage.pvh_list; npv; npv = npv->pv_next) {
874 if (pmap == npv->pv_pmap && va == npv->pv_va) {
875 oflags = npv->pv_flags;
876 npv->pv_flags = flags =
877 ((oflags & ~bic_mask) ^ eor_mask);
878 if ((flags ^ oflags) & PVF_WIRED) {
879 if (flags & PVF_WIRED)
880 ++pmap->pm_stats.wired_count;
881 else
882 --pmap->pm_stats.wired_count;
883 }
884 return (oflags);
885 }
886 }
887 return (0);
888 }
889
890 /*
891 * Map the specified level 2 pagetable into the level 1 page table for
892 * the given pmap to cover a chunk of virtual address space starting from the
893 * address specified.
894 */
895 static __inline void
896 pmap_map_in_l1(struct pmap *pmap, vaddr_t va, paddr_t l2pa, boolean_t selfref)
897 {
898 vaddr_t ptva;
899
900 /* Calculate the index into the L1 page table. */
901 ptva = (va >> L1_S_SHIFT) & ~3;
902
903 /* Map page table into the L1. */
904 pmap->pm_pdir[ptva + 0] = L1_C_PROTO | (l2pa + 0x000);
905 pmap->pm_pdir[ptva + 1] = L1_C_PROTO | (l2pa + 0x400);
906 pmap->pm_pdir[ptva + 2] = L1_C_PROTO | (l2pa + 0x800);
907 pmap->pm_pdir[ptva + 3] = L1_C_PROTO | (l2pa + 0xc00);
908
909 /* Map the page table into the page table area. */
910 if (selfref)
911 *((pt_entry_t *)(pmap->pm_vptpt + ptva)) = L2_S_PROTO | l2pa |
912 L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE);
913 }
914
915 #if 0
916 static __inline void
917 pmap_unmap_in_l1(struct pmap *pmap, vaddr_t va)
918 {
919 vaddr_t ptva;
920
921 /* Calculate the index into the L1 page table. */
922 ptva = (va >> L1_S_SHIFT) & ~3;
923
924 /* Unmap page table from the L1. */
925 pmap->pm_pdir[ptva + 0] = 0;
926 pmap->pm_pdir[ptva + 1] = 0;
927 pmap->pm_pdir[ptva + 2] = 0;
928 pmap->pm_pdir[ptva + 3] = 0;
929
930 /* Unmap the page table from the page table area. */
931 *((pt_entry_t *)(pmap->pm_vptpt + ptva)) = 0;
932 }
933 #endif
934
935 /*
936 * Used to map a range of physical addresses into kernel
937 * virtual address space.
938 *
939 * For now, VM is already on, we only need to map the
940 * specified memory.
941 *
942 * XXX This routine should eventually go away; it's only used
943 * XXX by machine-dependent crash dump code.
944 */
945 vaddr_t
946 pmap_map(vaddr_t va, paddr_t spa, paddr_t epa, vm_prot_t prot)
947 {
948 pt_entry_t *pte;
949
950 while (spa < epa) {
951 pte = vtopte(va);
952
953 *pte = L2_S_PROTO | spa |
954 L2_S_PROT(PTE_KERNEL, prot) | pte_l2_s_cache_mode;
955 cpu_tlb_flushID_SE(va);
956 va += NBPG;
957 spa += NBPG;
958 }
959 pmap_update(pmap_kernel());
960 return(va);
961 }
962
963
964 /*
965 * void pmap_bootstrap(pd_entry_t *kernel_l1pt, pv_addr_t kernel_ptpt)
966 *
967 * bootstrap the pmap system. This is called from initarm and allows
968 * the pmap system to initailise any structures it requires.
969 *
970 * Currently this sets up the kernel_pmap that is statically allocated
971 * and also allocated virtual addresses for certain page hooks.
972 * Currently the only one page hook is allocated that is used
973 * to zero physical pages of memory.
974 * It also initialises the start and end address of the kernel data space.
975 */
976 extern paddr_t physical_freestart;
977 extern paddr_t physical_freeend;
978
979 char *boot_head;
980
981 void
982 pmap_bootstrap(pd_entry_t *kernel_l1pt, pv_addr_t kernel_ptpt)
983 {
984 pt_entry_t *pte;
985 int loop;
986 paddr_t start, end;
987 #if NISADMA > 0
988 paddr_t istart;
989 psize_t isize;
990 #endif
991
992 pmap_kernel()->pm_pdir = kernel_l1pt;
993 pmap_kernel()->pm_pptpt = kernel_ptpt.pv_pa;
994 pmap_kernel()->pm_vptpt = kernel_ptpt.pv_va;
995 simple_lock_init(&pmap_kernel()->pm_lock);
996 pmap_kernel()->pm_obj.pgops = NULL;
997 TAILQ_INIT(&(pmap_kernel()->pm_obj.memq));
998 pmap_kernel()->pm_obj.uo_npages = 0;
999 pmap_kernel()->pm_obj.uo_refs = 1;
1000
1001 loop = 0;
1002 while (loop < bootconfig.dramblocks) {
1003 start = (paddr_t)bootconfig.dram[loop].address;
1004 end = start + (bootconfig.dram[loop].pages * NBPG);
1005 if (start < physical_freestart)
1006 start = physical_freestart;
1007 if (end > physical_freeend)
1008 end = physical_freeend;
1009 #if 0
1010 printf("%d: %lx -> %lx\n", loop, start, end - 1);
1011 #endif
1012 #if NISADMA > 0
1013 if (pmap_isa_dma_range_intersect(start, end - start,
1014 &istart, &isize)) {
1015 /*
1016 * Place the pages that intersect with the
1017 * ISA DMA range onto the ISA DMA free list.
1018 */
1019 #if 0
1020 printf(" ISADMA 0x%lx -> 0x%lx\n", istart,
1021 istart + isize - 1);
1022 #endif
1023 uvm_page_physload(atop(istart),
1024 atop(istart + isize), atop(istart),
1025 atop(istart + isize), VM_FREELIST_ISADMA);
1026
1027 /*
1028 * Load the pieces that come before
1029 * the intersection into the default
1030 * free list.
1031 */
1032 if (start < istart) {
1033 #if 0
1034 printf(" BEFORE 0x%lx -> 0x%lx\n",
1035 start, istart - 1);
1036 #endif
1037 uvm_page_physload(atop(start),
1038 atop(istart), atop(start),
1039 atop(istart), VM_FREELIST_DEFAULT);
1040 }
1041
1042 /*
1043 * Load the pieces that come after
1044 * the intersection into the default
1045 * free list.
1046 */
1047 if ((istart + isize) < end) {
1048 #if 0
1049 printf(" AFTER 0x%lx -> 0x%lx\n",
1050 (istart + isize), end - 1);
1051 #endif
1052 uvm_page_physload(atop(istart + isize),
1053 atop(end), atop(istart + isize),
1054 atop(end), VM_FREELIST_DEFAULT);
1055 }
1056 } else {
1057 uvm_page_physload(atop(start), atop(end),
1058 atop(start), atop(end), VM_FREELIST_DEFAULT);
1059 }
1060 #else /* NISADMA > 0 */
1061 uvm_page_physload(atop(start), atop(end),
1062 atop(start), atop(end), VM_FREELIST_DEFAULT);
1063 #endif /* NISADMA > 0 */
1064 ++loop;
1065 }
1066
1067 virtual_avail = KERNEL_VM_BASE;
1068 virtual_end = KERNEL_VM_BASE + KERNEL_VM_SIZE;
1069
1070 /*
1071 * now we allocate the "special" VAs which are used for tmp mappings
1072 * by the pmap (and other modules). we allocate the VAs by advancing
1073 * virtual_avail (note that there are no pages mapped at these VAs).
1074 * we find the PTE that maps the allocated VA via the linear PTE
1075 * mapping.
1076 */
1077
1078 pte = ((pt_entry_t *) PTE_BASE) + atop(virtual_avail);
1079
1080 csrcp = virtual_avail; csrc_pte = pte;
1081 virtual_avail += PAGE_SIZE; pte++;
1082
1083 cdstp = virtual_avail; cdst_pte = pte;
1084 virtual_avail += PAGE_SIZE; pte++;
1085
1086 memhook = (char *) virtual_avail; /* don't need pte */
1087 virtual_avail += PAGE_SIZE; pte++;
1088
1089 msgbufaddr = (caddr_t) virtual_avail; /* don't need pte */
1090 virtual_avail += round_page(MSGBUFSIZE);
1091 pte += atop(round_page(MSGBUFSIZE));
1092
1093 /*
1094 * init the static-global locks and global lists.
1095 */
1096 spinlockinit(&pmap_main_lock, "pmaplk", 0);
1097 simple_lock_init(&pvalloc_lock);
1098 simple_lock_init(&pmaps_lock);
1099 LIST_INIT(&pmaps);
1100 TAILQ_INIT(&pv_freepages);
1101 TAILQ_INIT(&pv_unusedpgs);
1102
1103 /*
1104 * initialize the pmap pool.
1105 */
1106
1107 pool_init(&pmap_pmap_pool, sizeof(struct pmap), 0, 0, 0, "pmappl",
1108 &pool_allocator_nointr);
1109
1110 cpu_dcache_wbinv_all();
1111 }
1112
1113 /*
1114 * void pmap_init(void)
1115 *
1116 * Initialize the pmap module.
1117 * Called by vm_init() in vm/vm_init.c in order to initialise
1118 * any structures that the pmap system needs to map virtual memory.
1119 */
1120
1121 extern int physmem;
1122
1123 void
1124 pmap_init(void)
1125 {
1126
1127 /*
1128 * Set the available memory vars - These do not map to real memory
1129 * addresses and cannot as the physical memory is fragmented.
1130 * They are used by ps for %mem calculations.
1131 * One could argue whether this should be the entire memory or just
1132 * the memory that is useable in a user process.
1133 */
1134 avail_start = 0;
1135 avail_end = physmem * NBPG;
1136
1137 /*
1138 * now we need to free enough pv_entry structures to allow us to get
1139 * the kmem_map/kmem_object allocated and inited (done after this
1140 * function is finished). to do this we allocate one bootstrap page out
1141 * of kernel_map and use it to provide an initial pool of pv_entry
1142 * structures. we never free this page.
1143 */
1144
1145 pv_initpage = (struct pv_page *) uvm_km_alloc(kernel_map, PAGE_SIZE);
1146 if (pv_initpage == NULL)
1147 panic("pmap_init: pv_initpage");
1148 pv_cachedva = 0; /* a VA we have allocated but not used yet */
1149 pv_nfpvents = 0;
1150 (void) pmap_add_pvpage(pv_initpage, FALSE);
1151
1152 pmap_initialized = TRUE;
1153
1154 /* Initialise our L1 page table queues and counters */
1155 SIMPLEQ_INIT(&l1pt_static_queue);
1156 l1pt_static_queue_count = 0;
1157 l1pt_static_create_count = 0;
1158 SIMPLEQ_INIT(&l1pt_queue);
1159 l1pt_queue_count = 0;
1160 l1pt_create_count = 0;
1161 l1pt_reuse_count = 0;
1162 }
1163
1164 /*
1165 * pmap_postinit()
1166 *
1167 * This routine is called after the vm and kmem subsystems have been
1168 * initialised. This allows the pmap code to perform any initialisation
1169 * that can only be done one the memory allocation is in place.
1170 */
1171
1172 void
1173 pmap_postinit(void)
1174 {
1175 int loop;
1176 struct l1pt *pt;
1177
1178 #ifdef PMAP_STATIC_L1S
1179 for (loop = 0; loop < PMAP_STATIC_L1S; ++loop) {
1180 #else /* PMAP_STATIC_L1S */
1181 for (loop = 0; loop < max_processes; ++loop) {
1182 #endif /* PMAP_STATIC_L1S */
1183 /* Allocate a L1 page table */
1184 pt = pmap_alloc_l1pt();
1185 if (!pt)
1186 panic("Cannot allocate static L1 page tables\n");
1187
1188 /* Clean it */
1189 bzero((void *)pt->pt_va, L1_TABLE_SIZE);
1190 pt->pt_flags |= (PTFLAG_STATIC | PTFLAG_CLEAN);
1191 /* Add the page table to the queue */
1192 SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pt, pt_queue);
1193 ++l1pt_static_queue_count;
1194 ++l1pt_static_create_count;
1195 }
1196 }
1197
1198
1199 /*
1200 * Create and return a physical map.
1201 *
1202 * If the size specified for the map is zero, the map is an actual physical
1203 * map, and may be referenced by the hardware.
1204 *
1205 * If the size specified is non-zero, the map will be used in software only,
1206 * and is bounded by that size.
1207 */
1208
1209 pmap_t
1210 pmap_create(void)
1211 {
1212 struct pmap *pmap;
1213
1214 /*
1215 * Fetch pmap entry from the pool
1216 */
1217
1218 pmap = pool_get(&pmap_pmap_pool, PR_WAITOK);
1219 /* XXX is this really needed! */
1220 memset(pmap, 0, sizeof(*pmap));
1221
1222 simple_lock_init(&pmap->pm_obj.vmobjlock);
1223 pmap->pm_obj.pgops = NULL; /* currently not a mappable object */
1224 TAILQ_INIT(&pmap->pm_obj.memq);
1225 pmap->pm_obj.uo_npages = 0;
1226 pmap->pm_obj.uo_refs = 1;
1227 pmap->pm_stats.wired_count = 0;
1228 pmap->pm_stats.resident_count = 1;
1229 pmap->pm_ptphint = NULL;
1230
1231 /* Now init the machine part of the pmap */
1232 pmap_pinit(pmap);
1233 return(pmap);
1234 }
1235
1236 /*
1237 * pmap_alloc_l1pt()
1238 *
1239 * This routine allocates physical and virtual memory for a L1 page table
1240 * and wires it.
1241 * A l1pt structure is returned to describe the allocated page table.
1242 *
1243 * This routine is allowed to fail if the required memory cannot be allocated.
1244 * In this case NULL is returned.
1245 */
1246
1247 struct l1pt *
1248 pmap_alloc_l1pt(void)
1249 {
1250 paddr_t pa;
1251 vaddr_t va;
1252 struct l1pt *pt;
1253 int error;
1254 struct vm_page *m;
1255 pt_entry_t *pte;
1256
1257 /* Allocate virtual address space for the L1 page table */
1258 va = uvm_km_valloc(kernel_map, L1_TABLE_SIZE);
1259 if (va == 0) {
1260 #ifdef DIAGNOSTIC
1261 PDEBUG(0,
1262 printf("pmap: Cannot allocate pageable memory for L1\n"));
1263 #endif /* DIAGNOSTIC */
1264 return(NULL);
1265 }
1266
1267 /* Allocate memory for the l1pt structure */
1268 pt = (struct l1pt *)malloc(sizeof(struct l1pt), M_VMPMAP, M_WAITOK);
1269
1270 /*
1271 * Allocate pages from the VM system.
1272 */
1273 error = uvm_pglistalloc(L1_TABLE_SIZE, physical_start, physical_end,
1274 L1_TABLE_SIZE, 0, &pt->pt_plist, 1, M_WAITOK);
1275 if (error) {
1276 #ifdef DIAGNOSTIC
1277 PDEBUG(0,
1278 printf("pmap: Cannot allocate physical mem for L1 (%d)\n",
1279 error));
1280 #endif /* DIAGNOSTIC */
1281 /* Release the resources we already have claimed */
1282 free(pt, M_VMPMAP);
1283 uvm_km_free(kernel_map, va, L1_TABLE_SIZE);
1284 return(NULL);
1285 }
1286
1287 /* Map our physical pages into our virtual space */
1288 pt->pt_va = va;
1289 m = TAILQ_FIRST(&pt->pt_plist);
1290 while (m && va < (pt->pt_va + L1_TABLE_SIZE)) {
1291 pa = VM_PAGE_TO_PHYS(m);
1292
1293 pte = vtopte(va);
1294
1295 /*
1296 * Assert that the PTE is invalid. If it's invalid,
1297 * then we are guaranteed that there won't be an entry
1298 * for this VA in the TLB.
1299 */
1300 KDASSERT(pmap_pte_v(pte) == 0);
1301
1302 *pte = L2_S_PROTO | VM_PAGE_TO_PHYS(m) |
1303 L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE);
1304
1305 va += NBPG;
1306 m = m->pageq.tqe_next;
1307 }
1308
1309 #ifdef DIAGNOSTIC
1310 if (m)
1311 panic("pmap_alloc_l1pt: pglist not empty\n");
1312 #endif /* DIAGNOSTIC */
1313
1314 pt->pt_flags = 0;
1315 return(pt);
1316 }
1317
1318 /*
1319 * Free a L1 page table previously allocated with pmap_alloc_l1pt().
1320 */
1321 static void
1322 pmap_free_l1pt(struct l1pt *pt)
1323 {
1324 /* Separate the physical memory for the virtual space */
1325 pmap_kremove(pt->pt_va, L1_TABLE_SIZE);
1326 pmap_update(pmap_kernel());
1327
1328 /* Return the physical memory */
1329 uvm_pglistfree(&pt->pt_plist);
1330
1331 /* Free the virtual space */
1332 uvm_km_free(kernel_map, pt->pt_va, L1_TABLE_SIZE);
1333
1334 /* Free the l1pt structure */
1335 free(pt, M_VMPMAP);
1336 }
1337
1338 /*
1339 * pmap_alloc_ptpt:
1340 *
1341 * Allocate the page table that maps the PTE array.
1342 */
1343 static int
1344 pmap_alloc_ptpt(struct pmap *pmap)
1345 {
1346 struct vm_page *pg;
1347 pt_entry_t *pte;
1348
1349 KASSERT(pmap->pm_vptpt == 0);
1350
1351 pmap->pm_vptpt = uvm_km_valloc(kernel_map, L2_TABLE_SIZE);
1352 if (pmap->pm_vptpt == 0) {
1353 PDEBUG(0,
1354 printf("pmap_alloc_ptpt: no KVA for PTPT\n"));
1355 return (ENOMEM);
1356 }
1357
1358 for (;;) {
1359 pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
1360 if (pg != NULL)
1361 break;
1362 uvm_wait("pmap_ptpt");
1363 }
1364
1365 pmap->pm_pptpt = VM_PAGE_TO_PHYS(pg);
1366
1367 pte = vtopte(pmap->pm_vptpt);
1368
1369 KDASSERT(pmap_pte_v(pte) == 0);
1370
1371 *pte = L2_S_PROTO | pmap->pm_pptpt |
1372 L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE);
1373
1374 return (0);
1375 }
1376
1377 /*
1378 * pmap_free_ptpt:
1379 *
1380 * Free the page table that maps the PTE array.
1381 */
1382 static void
1383 pmap_free_ptpt(struct pmap *pmap)
1384 {
1385
1386 pmap_kremove(pmap->pm_vptpt, L2_TABLE_SIZE);
1387 pmap_update(pmap_kernel());
1388
1389 uvm_pagefree(PHYS_TO_VM_PAGE(pmap->pm_pptpt));
1390
1391 uvm_km_free(kernel_map, pmap->pm_vptpt, L2_TABLE_SIZE);
1392 }
1393
1394 /*
1395 * Allocate a page directory.
1396 * This routine will either allocate a new page directory from the pool
1397 * of L1 page tables currently held by the kernel or it will allocate
1398 * a new one via pmap_alloc_l1pt().
1399 * It will then initialise the l1 page table for use.
1400 */
1401 static int
1402 pmap_allocpagedir(struct pmap *pmap)
1403 {
1404 paddr_t pa;
1405 struct l1pt *pt;
1406 int error;
1407
1408 PDEBUG(0, printf("pmap_allocpagedir(%p)\n", pmap));
1409
1410 /* Do we have any spare L1's lying around ? */
1411 if (l1pt_static_queue_count) {
1412 --l1pt_static_queue_count;
1413 pt = SIMPLEQ_FIRST(&l1pt_static_queue);
1414 SIMPLEQ_REMOVE_HEAD(&l1pt_static_queue, pt_queue);
1415 } else if (l1pt_queue_count) {
1416 --l1pt_queue_count;
1417 pt = SIMPLEQ_FIRST(&l1pt_queue);
1418 SIMPLEQ_REMOVE_HEAD(&l1pt_queue, pt_queue);
1419 ++l1pt_reuse_count;
1420 } else {
1421 pt = pmap_alloc_l1pt();
1422 if (!pt)
1423 return(ENOMEM);
1424 ++l1pt_create_count;
1425 }
1426
1427 /* Store the pointer to the l1 descriptor in the pmap. */
1428 pmap->pm_l1pt = pt;
1429
1430 /* Get the physical address of the start of the l1 */
1431 pa = VM_PAGE_TO_PHYS(TAILQ_FIRST(&pt->pt_plist));
1432
1433 /* Store the virtual address of the l1 in the pmap. */
1434 pmap->pm_pdir = (pd_entry_t *)pt->pt_va;
1435
1436 /* Clean the L1 if it is dirty */
1437 if (!(pt->pt_flags & PTFLAG_CLEAN))
1438 bzero((void *)pmap->pm_pdir, (L1_TABLE_SIZE - KERNEL_PD_SIZE));
1439
1440 /* Allocate a page table to map all the page tables for this pmap */
1441 if ((error = pmap_alloc_ptpt(pmap)) != 0) {
1442 pmap_freepagedir(pmap);
1443 return (error);
1444 }
1445
1446 /* need to lock this all up for growkernel */
1447 simple_lock(&pmaps_lock);
1448
1449 /* Duplicate the kernel mappings. */
1450 bcopy((char *)pmap_kernel()->pm_pdir + (L1_TABLE_SIZE - KERNEL_PD_SIZE),
1451 (char *)pmap->pm_pdir + (L1_TABLE_SIZE - KERNEL_PD_SIZE),
1452 KERNEL_PD_SIZE);
1453
1454 /* Wire in this page table */
1455 pmap_map_in_l1(pmap, PTE_BASE, pmap->pm_pptpt, TRUE);
1456
1457 pt->pt_flags &= ~PTFLAG_CLEAN; /* L1 is dirty now */
1458
1459 /*
1460 * Map the kernel page tables into the new PT map.
1461 */
1462 bcopy((char *)(PTE_BASE
1463 + (PTE_BASE >> (PGSHIFT - 2))
1464 + ((L1_TABLE_SIZE - KERNEL_PD_SIZE) >> 2)),
1465 (char *)pmap->pm_vptpt + ((L1_TABLE_SIZE - KERNEL_PD_SIZE) >> 2),
1466 (KERNEL_PD_SIZE >> 2));
1467
1468 LIST_INSERT_HEAD(&pmaps, pmap, pm_list);
1469 simple_unlock(&pmaps_lock);
1470
1471 return(0);
1472 }
1473
1474
1475 /*
1476 * Initialize a preallocated and zeroed pmap structure,
1477 * such as one in a vmspace structure.
1478 */
1479
1480 void
1481 pmap_pinit(struct pmap *pmap)
1482 {
1483 int backoff = 6;
1484 int retry = 10;
1485
1486 PDEBUG(0, printf("pmap_pinit(%p)\n", pmap));
1487
1488 /* Keep looping until we succeed in allocating a page directory */
1489 while (pmap_allocpagedir(pmap) != 0) {
1490 /*
1491 * Ok we failed to allocate a suitable block of memory for an
1492 * L1 page table. This means that either:
1493 * 1. 16KB of virtual address space could not be allocated
1494 * 2. 16KB of physically contiguous memory on a 16KB boundary
1495 * could not be allocated.
1496 *
1497 * Since we cannot fail we will sleep for a while and try
1498 * again.
1499 *
1500 * Searching for a suitable L1 PT is expensive:
1501 * to avoid hogging the system when memory is really
1502 * scarce, use an exponential back-off so that
1503 * eventually we won't retry more than once every 8
1504 * seconds. This should allow other processes to run
1505 * to completion and free up resources.
1506 */
1507 (void) ltsleep(&lbolt, PVM, "l1ptwait", (hz << 3) >> backoff,
1508 NULL);
1509 if (--retry == 0) {
1510 retry = 10;
1511 if (backoff)
1512 --backoff;
1513 }
1514 }
1515
1516 if (vector_page < KERNEL_BASE) {
1517 /*
1518 * Map the vector page. This will also allocate and map
1519 * an L2 table for it.
1520 */
1521 pmap_enter(pmap, vector_page, systempage.pv_pa,
1522 VM_PROT_READ, VM_PROT_READ | PMAP_WIRED);
1523 pmap_update(pmap);
1524 }
1525 }
1526
1527
1528 void
1529 pmap_freepagedir(struct pmap *pmap)
1530 {
1531 /* Free the memory used for the page table mapping */
1532 if (pmap->pm_vptpt != 0)
1533 pmap_free_ptpt(pmap);
1534
1535 /* junk the L1 page table */
1536 if (pmap->pm_l1pt->pt_flags & PTFLAG_STATIC) {
1537 /* Add the page table to the queue */
1538 SIMPLEQ_INSERT_TAIL(&l1pt_static_queue, pmap->pm_l1pt, pt_queue);
1539 ++l1pt_static_queue_count;
1540 } else if (l1pt_queue_count < 8) {
1541 /* Add the page table to the queue */
1542 SIMPLEQ_INSERT_TAIL(&l1pt_queue, pmap->pm_l1pt, pt_queue);
1543 ++l1pt_queue_count;
1544 } else
1545 pmap_free_l1pt(pmap->pm_l1pt);
1546 }
1547
1548
1549 /*
1550 * Retire the given physical map from service.
1551 * Should only be called if the map contains no valid mappings.
1552 */
1553
1554 void
1555 pmap_destroy(struct pmap *pmap)
1556 {
1557 struct vm_page *page;
1558 int count;
1559
1560 if (pmap == NULL)
1561 return;
1562
1563 PDEBUG(0, printf("pmap_destroy(%p)\n", pmap));
1564
1565 /*
1566 * Drop reference count
1567 */
1568 simple_lock(&pmap->pm_obj.vmobjlock);
1569 count = --pmap->pm_obj.uo_refs;
1570 simple_unlock(&pmap->pm_obj.vmobjlock);
1571 if (count > 0) {
1572 return;
1573 }
1574
1575 /*
1576 * reference count is zero, free pmap resources and then free pmap.
1577 */
1578
1579 /*
1580 * remove it from global list of pmaps
1581 */
1582
1583 simple_lock(&pmaps_lock);
1584 LIST_REMOVE(pmap, pm_list);
1585 simple_unlock(&pmaps_lock);
1586
1587 if (vector_page < KERNEL_BASE) {
1588 /* Remove the vector page mapping */
1589 pmap_remove(pmap, vector_page, vector_page + NBPG);
1590 pmap_update(pmap);
1591 }
1592
1593 /*
1594 * Free any page tables still mapped
1595 * This is only temporay until pmap_enter can count the number
1596 * of mappings made in a page table. Then pmap_remove() can
1597 * reduce the count and free the pagetable when the count
1598 * reaches zero. Note that entries in this list should match the
1599 * contents of the ptpt, however this is faster than walking a 1024
1600 * entries looking for pt's
1601 * taken from i386 pmap.c
1602 */
1603 /*
1604 * vmobjlock must be held while freeing pages
1605 */
1606 simple_lock(&pmap->pm_obj.vmobjlock);
1607 while ((page = TAILQ_FIRST(&pmap->pm_obj.memq)) != NULL) {
1608 KASSERT((page->flags & PG_BUSY) == 0);
1609 page->wire_count = 0;
1610 uvm_pagefree(page);
1611 }
1612 simple_unlock(&pmap->pm_obj.vmobjlock);
1613
1614 /* Free the page dir */
1615 pmap_freepagedir(pmap);
1616
1617 /* return the pmap to the pool */
1618 pool_put(&pmap_pmap_pool, pmap);
1619 }
1620
1621
1622 /*
1623 * void pmap_reference(struct pmap *pmap)
1624 *
1625 * Add a reference to the specified pmap.
1626 */
1627
1628 void
1629 pmap_reference(struct pmap *pmap)
1630 {
1631 if (pmap == NULL)
1632 return;
1633
1634 simple_lock(&pmap->pm_lock);
1635 pmap->pm_obj.uo_refs++;
1636 simple_unlock(&pmap->pm_lock);
1637 }
1638
1639 /*
1640 * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
1641 *
1642 * Return the start and end addresses of the kernel's virtual space.
1643 * These values are setup in pmap_bootstrap and are updated as pages
1644 * are allocated.
1645 */
1646
1647 void
1648 pmap_virtual_space(vaddr_t *start, vaddr_t *end)
1649 {
1650 *start = virtual_avail;
1651 *end = virtual_end;
1652 }
1653
1654 /*
1655 * Activate the address space for the specified process. If the process
1656 * is the current process, load the new MMU context.
1657 */
1658 void
1659 pmap_activate(struct proc *p)
1660 {
1661 struct pmap *pmap = p->p_vmspace->vm_map.pmap;
1662 struct pcb *pcb = &p->p_addr->u_pcb;
1663
1664 (void) pmap_extract(pmap_kernel(), (vaddr_t)pmap->pm_pdir,
1665 (paddr_t *)&pcb->pcb_pagedir);
1666
1667 PDEBUG(0, printf("pmap_activate: p=%p pmap=%p pcb=%p pdir=%p l1=%p\n",
1668 p, pmap, pcb, pmap->pm_pdir, pcb->pcb_pagedir));
1669
1670 if (p == curproc) {
1671 PDEBUG(0, printf("pmap_activate: setting TTB\n"));
1672 setttb((u_int)pcb->pcb_pagedir);
1673 }
1674 }
1675
1676 /*
1677 * Deactivate the address space of the specified process.
1678 */
1679 void
1680 pmap_deactivate(struct proc *p)
1681 {
1682 }
1683
1684 /*
1685 * Perform any deferred pmap operations.
1686 */
1687 void
1688 pmap_update(struct pmap *pmap)
1689 {
1690
1691 /*
1692 * We haven't deferred any pmap operations, but we do need to
1693 * make sure TLB/cache operations have completed.
1694 */
1695 cpu_cpwait();
1696 }
1697
1698 /*
1699 * pmap_clean_page()
1700 *
1701 * This is a local function used to work out the best strategy to clean
1702 * a single page referenced by its entry in the PV table. It's used by
1703 * pmap_copy_page, pmap_zero page and maybe some others later on.
1704 *
1705 * Its policy is effectively:
1706 * o If there are no mappings, we don't bother doing anything with the cache.
1707 * o If there is one mapping, we clean just that page.
1708 * o If there are multiple mappings, we clean the entire cache.
1709 *
1710 * So that some functions can be further optimised, it returns 0 if it didn't
1711 * clean the entire cache, or 1 if it did.
1712 *
1713 * XXX One bug in this routine is that if the pv_entry has a single page
1714 * mapped at 0x00000000 a whole cache clean will be performed rather than
1715 * just the 1 page. Since this should not occur in everyday use and if it does
1716 * it will just result in not the most efficient clean for the page.
1717 */
1718 static int
1719 pmap_clean_page(struct pv_entry *pv, boolean_t is_src)
1720 {
1721 struct pmap *pmap;
1722 struct pv_entry *npv;
1723 int cache_needs_cleaning = 0;
1724 vaddr_t page_to_clean = 0;
1725
1726 if (pv == NULL)
1727 /* nothing mapped in so nothing to flush */
1728 return (0);
1729
1730 /* Since we flush the cache each time we change curproc, we
1731 * only need to flush the page if it is in the current pmap.
1732 */
1733 if (curproc)
1734 pmap = curproc->p_vmspace->vm_map.pmap;
1735 else
1736 pmap = pmap_kernel();
1737
1738 for (npv = pv; npv; npv = npv->pv_next) {
1739 if (npv->pv_pmap == pmap) {
1740 /* The page is mapped non-cacheable in
1741 * this map. No need to flush the cache.
1742 */
1743 if (npv->pv_flags & PVF_NC) {
1744 #ifdef DIAGNOSTIC
1745 if (cache_needs_cleaning)
1746 panic("pmap_clean_page: "
1747 "cache inconsistency");
1748 #endif
1749 break;
1750 }
1751 #if 0
1752 /*
1753 * XXX Can't do this because pmap_protect doesn't
1754 * XXX clean the page when it does a write-protect.
1755 */
1756 else if (is_src && (npv->pv_flags & PVF_WRITE) == 0)
1757 continue;
1758 #endif
1759 if (cache_needs_cleaning){
1760 page_to_clean = 0;
1761 break;
1762 }
1763 else
1764 page_to_clean = npv->pv_va;
1765 cache_needs_cleaning = 1;
1766 }
1767 }
1768
1769 if (page_to_clean)
1770 cpu_idcache_wbinv_range(page_to_clean, NBPG);
1771 else if (cache_needs_cleaning) {
1772 cpu_idcache_wbinv_all();
1773 return (1);
1774 }
1775 return (0);
1776 }
1777
1778 /*
1779 * pmap_zero_page()
1780 *
1781 * Zero a given physical page by mapping it at a page hook point.
1782 * In doing the zero page op, the page we zero is mapped cachable, as with
1783 * StrongARM accesses to non-cached pages are non-burst making writing
1784 * _any_ bulk data very slow.
1785 */
1786 #if ARM_MMU_GENERIC == 1
1787 void
1788 pmap_zero_page_generic(paddr_t phys)
1789 {
1790 #ifdef DEBUG
1791 struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
1792
1793 if (pg->mdpage.pvh_list != NULL)
1794 panic("pmap_zero_page: page has mappings");
1795 #endif
1796
1797 KDASSERT((phys & PGOFSET) == 0);
1798
1799 /*
1800 * Hook in the page, zero it, and purge the cache for that
1801 * zeroed page. Invalidate the TLB as needed.
1802 */
1803 *cdst_pte = L2_S_PROTO | phys |
1804 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
1805 cpu_tlb_flushD_SE(cdstp);
1806 cpu_cpwait();
1807 bzero_page(cdstp);
1808 cpu_dcache_wbinv_range(cdstp, NBPG);
1809 }
1810 #endif /* ARM_MMU_GENERIC == 1 */
1811
1812 #if ARM_MMU_XSCALE == 1
1813 void
1814 pmap_zero_page_xscale(paddr_t phys)
1815 {
1816 #ifdef DEBUG
1817 struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
1818
1819 if (pg->mdpage.pvh_list != NULL)
1820 panic("pmap_zero_page: page has mappings");
1821 #endif
1822
1823 KDASSERT((phys & PGOFSET) == 0);
1824
1825 /*
1826 * Hook in the page, zero it, and purge the cache for that
1827 * zeroed page. Invalidate the TLB as needed.
1828 */
1829 *cdst_pte = L2_S_PROTO | phys |
1830 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
1831 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X); /* mini-data */
1832 cpu_tlb_flushD_SE(cdstp);
1833 cpu_cpwait();
1834 bzero_page(cdstp);
1835 xscale_cache_clean_minidata();
1836 }
1837 #endif /* ARM_MMU_XSCALE == 1 */
1838
1839 /* pmap_pageidlezero()
1840 *
1841 * The same as above, except that we assume that the page is not
1842 * mapped. This means we never have to flush the cache first. Called
1843 * from the idle loop.
1844 */
1845 boolean_t
1846 pmap_pageidlezero(paddr_t phys)
1847 {
1848 int i, *ptr;
1849 boolean_t rv = TRUE;
1850 #ifdef DEBUG
1851 struct vm_page *pg;
1852
1853 pg = PHYS_TO_VM_PAGE(phys);
1854 if (pg->mdpage.pvh_list != NULL)
1855 panic("pmap_pageidlezero: page has mappings");
1856 #endif
1857
1858 KDASSERT((phys & PGOFSET) == 0);
1859
1860 /*
1861 * Hook in the page, zero it, and purge the cache for that
1862 * zeroed page. Invalidate the TLB as needed.
1863 */
1864 *cdst_pte = L2_S_PROTO | phys |
1865 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
1866 cpu_tlb_flushD_SE(cdstp);
1867 cpu_cpwait();
1868
1869 for (i = 0, ptr = (int *)cdstp;
1870 i < (NBPG / sizeof(int)); i++) {
1871 if (sched_whichqs != 0) {
1872 /*
1873 * A process has become ready. Abort now,
1874 * so we don't keep it waiting while we
1875 * do slow memory access to finish this
1876 * page.
1877 */
1878 rv = FALSE;
1879 break;
1880 }
1881 *ptr++ = 0;
1882 }
1883
1884 if (rv)
1885 /*
1886 * if we aborted we'll rezero this page again later so don't
1887 * purge it unless we finished it
1888 */
1889 cpu_dcache_wbinv_range(cdstp, NBPG);
1890 return (rv);
1891 }
1892
1893 /*
1894 * pmap_copy_page()
1895 *
1896 * Copy one physical page into another, by mapping the pages into
1897 * hook points. The same comment regarding cachability as in
1898 * pmap_zero_page also applies here.
1899 */
1900 #if ARM_MMU_GENERIC == 1
1901 void
1902 pmap_copy_page_generic(paddr_t src, paddr_t dst)
1903 {
1904 struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
1905 #ifdef DEBUG
1906 struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
1907
1908 if (dst_pg->mdpage.pvh_list != NULL)
1909 panic("pmap_copy_page: dst page has mappings");
1910 #endif
1911
1912 KDASSERT((src & PGOFSET) == 0);
1913 KDASSERT((dst & PGOFSET) == 0);
1914
1915 /*
1916 * Clean the source page. Hold the source page's lock for
1917 * the duration of the copy so that no other mappings can
1918 * be created while we have a potentially aliased mapping.
1919 */
1920 simple_lock(&src_pg->mdpage.pvh_slock);
1921 (void) pmap_clean_page(src_pg->mdpage.pvh_list, TRUE);
1922
1923 /*
1924 * Map the pages into the page hook points, copy them, and purge
1925 * the cache for the appropriate page. Invalidate the TLB
1926 * as required.
1927 */
1928 *csrc_pte = L2_S_PROTO | src |
1929 L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
1930 *cdst_pte = L2_S_PROTO | dst |
1931 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
1932 cpu_tlb_flushD_SE(csrcp);
1933 cpu_tlb_flushD_SE(cdstp);
1934 cpu_cpwait();
1935 bcopy_page(csrcp, cdstp);
1936 cpu_dcache_inv_range(csrcp, NBPG);
1937 simple_unlock(&src_pg->mdpage.pvh_slock); /* cache is safe again */
1938 cpu_dcache_wbinv_range(cdstp, NBPG);
1939 }
1940 #endif /* ARM_MMU_GENERIC == 1 */
1941
1942 #if ARM_MMU_XSCALE == 1
1943 void
1944 pmap_copy_page_xscale(paddr_t src, paddr_t dst)
1945 {
1946 struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
1947 #ifdef DEBUG
1948 struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
1949
1950 if (dst_pg->mdpage.pvh_list != NULL)
1951 panic("pmap_copy_page: dst page has mappings");
1952 #endif
1953
1954 KDASSERT((src & PGOFSET) == 0);
1955 KDASSERT((dst & PGOFSET) == 0);
1956
1957 /*
1958 * Clean the source page. Hold the source page's lock for
1959 * the duration of the copy so that no other mappings can
1960 * be created while we have a potentially aliased mapping.
1961 */
1962 simple_lock(&src_pg->mdpage.pvh_slock);
1963 (void) pmap_clean_page(src_pg->mdpage.pvh_list, TRUE);
1964
1965 /*
1966 * Map the pages into the page hook points, copy them, and purge
1967 * the cache for the appropriate page. Invalidate the TLB
1968 * as required.
1969 */
1970 *csrc_pte = L2_S_PROTO | src |
1971 L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
1972 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X); /* mini-data */
1973 *cdst_pte = L2_S_PROTO | dst |
1974 L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
1975 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X); /* mini-data */
1976 cpu_tlb_flushD_SE(csrcp);
1977 cpu_tlb_flushD_SE(cdstp);
1978 cpu_cpwait();
1979 bcopy_page(csrcp, cdstp);
1980 simple_unlock(&src_pg->mdpage.pvh_slock); /* cache is safe again */
1981 xscale_cache_clean_minidata();
1982 }
1983 #endif /* ARM_MMU_XSCALE == 1 */
1984
1985 #if 0
1986 void
1987 pmap_pte_addref(struct pmap *pmap, vaddr_t va)
1988 {
1989 pd_entry_t *pde;
1990 paddr_t pa;
1991 struct vm_page *m;
1992
1993 if (pmap == pmap_kernel())
1994 return;
1995
1996 pde = pmap_pde(pmap, va & ~(3 << L1_S_SHIFT));
1997 pa = pmap_pte_pa(pde);
1998 m = PHYS_TO_VM_PAGE(pa);
1999 ++m->wire_count;
2000 #ifdef MYCROFT_HACK
2001 printf("addref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
2002 pmap, va, pde, pa, m, m->wire_count);
2003 #endif
2004 }
2005
2006 void
2007 pmap_pte_delref(struct pmap *pmap, vaddr_t va)
2008 {
2009 pd_entry_t *pde;
2010 paddr_t pa;
2011 struct vm_page *m;
2012
2013 if (pmap == pmap_kernel())
2014 return;
2015
2016 pde = pmap_pde(pmap, va & ~(3 << L1_S_SHIFT));
2017 pa = pmap_pte_pa(pde);
2018 m = PHYS_TO_VM_PAGE(pa);
2019 --m->wire_count;
2020 #ifdef MYCROFT_HACK
2021 printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p wire=%d\n",
2022 pmap, va, pde, pa, m, m->wire_count);
2023 #endif
2024 if (m->wire_count == 0) {
2025 #ifdef MYCROFT_HACK
2026 printf("delref pmap=%p va=%08lx pde=%p pa=%08lx m=%p\n",
2027 pmap, va, pde, pa, m);
2028 #endif
2029 pmap_unmap_in_l1(pmap, va);
2030 uvm_pagefree(m);
2031 --pmap->pm_stats.resident_count;
2032 }
2033 }
2034 #else
2035 #define pmap_pte_addref(pmap, va)
2036 #define pmap_pte_delref(pmap, va)
2037 #endif
2038
2039 /*
2040 * Since we have a virtually indexed cache, we may need to inhibit caching if
2041 * there is more than one mapping and at least one of them is writable.
2042 * Since we purge the cache on every context switch, we only need to check for
2043 * other mappings within the same pmap, or kernel_pmap.
2044 * This function is also called when a page is unmapped, to possibly reenable
2045 * caching on any remaining mappings.
2046 *
2047 * The code implements the following logic, where:
2048 *
2049 * KW = # of kernel read/write pages
2050 * KR = # of kernel read only pages
2051 * UW = # of user read/write pages
2052 * UR = # of user read only pages
2053 * OW = # of user read/write pages in another pmap, then
2054 *
2055 * KC = kernel mapping is cacheable
2056 * UC = user mapping is cacheable
2057 *
2058 * KW=0,KR=0 KW=0,KR>0 KW=1,KR=0 KW>1,KR>=0
2059 * +---------------------------------------------
2060 * UW=0,UR=0,OW=0 | --- KC=1 KC=1 KC=0
2061 * UW=0,UR>0,OW=0 | UC=1 KC=1,UC=1 KC=0,UC=0 KC=0,UC=0
2062 * UW=0,UR>0,OW>0 | UC=1 KC=0,UC=1 KC=0,UC=0 KC=0,UC=0
2063 * UW=1,UR=0,OW=0 | UC=1 KC=0,UC=0 KC=0,UC=0 KC=0,UC=0
2064 * UW>1,UR>=0,OW>=0 | UC=0 KC=0,UC=0 KC=0,UC=0 KC=0,UC=0
2065 *
2066 * Note that the pmap must have it's ptes mapped in, and passed with ptes.
2067 */
2068 __inline static void
2069 pmap_vac_me_harder(struct pmap *pmap, struct vm_page *pg, pt_entry_t *ptes,
2070 boolean_t clear_cache)
2071 {
2072 if (pmap == pmap_kernel())
2073 pmap_vac_me_kpmap(pmap, pg, ptes, clear_cache);
2074 else
2075 pmap_vac_me_user(pmap, pg, ptes, clear_cache);
2076 }
2077
2078 static void
2079 pmap_vac_me_kpmap(struct pmap *pmap, struct vm_page *pg, pt_entry_t *ptes,
2080 boolean_t clear_cache)
2081 {
2082 int user_entries = 0;
2083 int user_writable = 0;
2084 int user_cacheable = 0;
2085 int kernel_entries = 0;
2086 int kernel_writable = 0;
2087 int kernel_cacheable = 0;
2088 struct pv_entry *pv;
2089 struct pmap *last_pmap = pmap;
2090
2091 #ifdef DIAGNOSTIC
2092 if (pmap != pmap_kernel())
2093 panic("pmap_vac_me_kpmap: pmap != pmap_kernel()");
2094 #endif
2095
2096 /*
2097 * Pass one, see if there are both kernel and user pmaps for
2098 * this page. Calculate whether there are user-writable or
2099 * kernel-writable pages.
2100 */
2101 for (pv = pg->mdpage.pvh_list; pv != NULL; pv = pv->pv_next) {
2102 if (pv->pv_pmap != pmap) {
2103 user_entries++;
2104 if (pv->pv_flags & PVF_WRITE)
2105 user_writable++;
2106 if ((pv->pv_flags & PVF_NC) == 0)
2107 user_cacheable++;
2108 } else {
2109 kernel_entries++;
2110 if (pv->pv_flags & PVF_WRITE)
2111 kernel_writable++;
2112 if ((pv->pv_flags & PVF_NC) == 0)
2113 kernel_cacheable++;
2114 }
2115 }
2116
2117 /*
2118 * We know we have just been updating a kernel entry, so if
2119 * all user pages are already cacheable, then there is nothing
2120 * further to do.
2121 */
2122 if (kernel_entries == 0 &&
2123 user_cacheable == user_entries)
2124 return;
2125
2126 if (user_entries) {
2127 /*
2128 * Scan over the list again, for each entry, if it
2129 * might not be set correctly, call pmap_vac_me_user
2130 * to recalculate the settings.
2131 */
2132 for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
2133 /*
2134 * We know kernel mappings will get set
2135 * correctly in other calls. We also know
2136 * that if the pmap is the same as last_pmap
2137 * then we've just handled this entry.
2138 */
2139 if (pv->pv_pmap == pmap || pv->pv_pmap == last_pmap)
2140 continue;
2141 /*
2142 * If there are kernel entries and this page
2143 * is writable but non-cacheable, then we can
2144 * skip this entry also.
2145 */
2146 if (kernel_entries > 0 &&
2147 (pv->pv_flags & (PVF_NC | PVF_WRITE)) ==
2148 (PVF_NC | PVF_WRITE))
2149 continue;
2150 /*
2151 * Similarly if there are no kernel-writable
2152 * entries and the page is already
2153 * read-only/cacheable.
2154 */
2155 if (kernel_writable == 0 &&
2156 (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 0)
2157 continue;
2158 /*
2159 * For some of the remaining cases, we know
2160 * that we must recalculate, but for others we
2161 * can't tell if they are correct or not, so
2162 * we recalculate anyway.
2163 */
2164 pmap_unmap_ptes(last_pmap);
2165 last_pmap = pv->pv_pmap;
2166 ptes = pmap_map_ptes(last_pmap);
2167 pmap_vac_me_user(last_pmap, pg, ptes,
2168 pmap_is_curpmap(last_pmap));
2169 }
2170 /* Restore the pte mapping that was passed to us. */
2171 if (last_pmap != pmap) {
2172 pmap_unmap_ptes(last_pmap);
2173 ptes = pmap_map_ptes(pmap);
2174 }
2175 if (kernel_entries == 0)
2176 return;
2177 }
2178
2179 pmap_vac_me_user(pmap, pg, ptes, clear_cache);
2180 return;
2181 }
2182
2183 static void
2184 pmap_vac_me_user(struct pmap *pmap, struct vm_page *pg, pt_entry_t *ptes,
2185 boolean_t clear_cache)
2186 {
2187 struct pmap *kpmap = pmap_kernel();
2188 struct pv_entry *pv, *npv;
2189 int entries = 0;
2190 int writable = 0;
2191 int cacheable_entries = 0;
2192 int kern_cacheable = 0;
2193 int other_writable = 0;
2194
2195 pv = pg->mdpage.pvh_list;
2196 KASSERT(ptes != NULL);
2197
2198 /*
2199 * Count mappings and writable mappings in this pmap.
2200 * Include kernel mappings as part of our own.
2201 * Keep a pointer to the first one.
2202 */
2203 for (npv = pv; npv; npv = npv->pv_next) {
2204 /* Count mappings in the same pmap */
2205 if (pmap == npv->pv_pmap ||
2206 kpmap == npv->pv_pmap) {
2207 if (entries++ == 0)
2208 pv = npv;
2209 /* Cacheable mappings */
2210 if ((npv->pv_flags & PVF_NC) == 0) {
2211 cacheable_entries++;
2212 if (kpmap == npv->pv_pmap)
2213 kern_cacheable++;
2214 }
2215 /* Writable mappings */
2216 if (npv->pv_flags & PVF_WRITE)
2217 ++writable;
2218 } else if (npv->pv_flags & PVF_WRITE)
2219 other_writable = 1;
2220 }
2221
2222 PDEBUG(3,printf("pmap_vac_me_harder: pmap %p Entries %d, "
2223 "writable %d cacheable %d %s\n", pmap, entries, writable,
2224 cacheable_entries, clear_cache ? "clean" : "no clean"));
2225
2226 /*
2227 * Enable or disable caching as necessary.
2228 * Note: the first entry might be part of the kernel pmap,
2229 * so we can't assume this is indicative of the state of the
2230 * other (maybe non-kpmap) entries.
2231 */
2232 if ((entries > 1 && writable) ||
2233 (entries > 0 && pmap == kpmap && other_writable)) {
2234 if (cacheable_entries == 0)
2235 return;
2236 for (npv = pv; npv; npv = npv->pv_next) {
2237 if ((pmap == npv->pv_pmap
2238 || kpmap == npv->pv_pmap) &&
2239 (npv->pv_flags & PVF_NC) == 0) {
2240 ptes[arm_btop(npv->pv_va)] &= ~L2_S_CACHE_MASK;
2241 npv->pv_flags |= PVF_NC;
2242 /*
2243 * If this page needs flushing from the
2244 * cache, and we aren't going to do it
2245 * below, do it now.
2246 */
2247 if ((cacheable_entries < 4 &&
2248 (clear_cache || npv->pv_pmap == kpmap)) ||
2249 (npv->pv_pmap == kpmap &&
2250 !clear_cache && kern_cacheable < 4)) {
2251 cpu_idcache_wbinv_range(npv->pv_va,
2252 NBPG);
2253 cpu_tlb_flushID_SE(npv->pv_va);
2254 }
2255 }
2256 }
2257 if ((clear_cache && cacheable_entries >= 4) ||
2258 kern_cacheable >= 4) {
2259 cpu_idcache_wbinv_all();
2260 cpu_tlb_flushID();
2261 }
2262 cpu_cpwait();
2263 } else if (entries > 0) {
2264 /*
2265 * Turn cacheing back on for some pages. If it is a kernel
2266 * page, only do so if there are no other writable pages.
2267 */
2268 for (npv = pv; npv; npv = npv->pv_next) {
2269 if ((pmap == npv->pv_pmap ||
2270 (kpmap == npv->pv_pmap && other_writable == 0)) &&
2271 (npv->pv_flags & PVF_NC)) {
2272 ptes[arm_btop(npv->pv_va)] |=
2273 pte_l2_s_cache_mode;
2274 npv->pv_flags &= ~PVF_NC;
2275 }
2276 }
2277 }
2278 }
2279
2280 /*
2281 * pmap_remove()
2282 *
2283 * pmap_remove is responsible for nuking a number of mappings for a range
2284 * of virtual address space in the current pmap. To do this efficiently
2285 * is interesting, because in a number of cases a wide virtual address
2286 * range may be supplied that contains few actual mappings. So, the
2287 * optimisations are:
2288 * 1. Try and skip over hunks of address space for which an L1 entry
2289 * does not exist.
2290 * 2. Build up a list of pages we've hit, up to a maximum, so we can
2291 * maybe do just a partial cache clean. This path of execution is
2292 * complicated by the fact that the cache must be flushed _before_
2293 * the PTE is nuked, being a VAC :-)
2294 * 3. Maybe later fast-case a single page, but I don't think this is
2295 * going to make _that_ much difference overall.
2296 */
2297
2298 #define PMAP_REMOVE_CLEAN_LIST_SIZE 3
2299
2300 void
2301 pmap_remove(struct pmap *pmap, vaddr_t sva, vaddr_t eva)
2302 {
2303 int cleanlist_idx = 0;
2304 struct pagelist {
2305 vaddr_t va;
2306 pt_entry_t *pte;
2307 } cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
2308 pt_entry_t *pte = 0, *ptes;
2309 paddr_t pa;
2310 int pmap_active;
2311 struct vm_page *pg;
2312
2313 /* Exit quick if there is no pmap */
2314 if (!pmap)
2315 return;
2316
2317 PDEBUG(0, printf("pmap_remove: pmap=%p sva=%08lx eva=%08lx\n",
2318 pmap, sva, eva));
2319
2320 /*
2321 * we lock in the pmap => vm_page direction
2322 */
2323 PMAP_MAP_TO_HEAD_LOCK();
2324
2325 ptes = pmap_map_ptes(pmap);
2326 /* Get a page table pointer */
2327 while (sva < eva) {
2328 if (pmap_pde_page(pmap_pde(pmap, sva)))
2329 break;
2330 sva = (sva & L1_S_FRAME) + L1_S_SIZE;
2331 }
2332
2333 pte = &ptes[arm_btop(sva)];
2334 /* Note if the pmap is active thus require cache and tlb cleans */
2335 pmap_active = pmap_is_curpmap(pmap);
2336
2337 /* Now loop along */
2338 while (sva < eva) {
2339 /* Check if we can move to the next PDE (l1 chunk) */
2340 if (!(sva & L2_ADDR_BITS))
2341 if (!pmap_pde_page(pmap_pde(pmap, sva))) {
2342 sva += L1_S_SIZE;
2343 pte += arm_btop(L1_S_SIZE);
2344 continue;
2345 }
2346
2347 /* We've found a valid PTE, so this page of PTEs has to go. */
2348 if (pmap_pte_v(pte)) {
2349 /* Update statistics */
2350 --pmap->pm_stats.resident_count;
2351
2352 /*
2353 * Add this page to our cache remove list, if we can.
2354 * If, however the cache remove list is totally full,
2355 * then do a complete cache invalidation taking note
2356 * to backtrack the PTE table beforehand, and ignore
2357 * the lists in future because there's no longer any
2358 * point in bothering with them (we've paid the
2359 * penalty, so will carry on unhindered). Otherwise,
2360 * when we fall out, we just clean the list.
2361 */
2362 PDEBUG(10, printf("remove: inv pte at %p(%x) ", pte, *pte));
2363 pa = pmap_pte_pa(pte);
2364
2365 if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
2366 /* Add to the clean list. */
2367 cleanlist[cleanlist_idx].pte = pte;
2368 cleanlist[cleanlist_idx].va = sva;
2369 cleanlist_idx++;
2370 } else if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
2371 int cnt;
2372
2373 /* Nuke everything if needed. */
2374 if (pmap_active) {
2375 cpu_idcache_wbinv_all();
2376 cpu_tlb_flushID();
2377 }
2378
2379 /*
2380 * Roll back the previous PTE list,
2381 * and zero out the current PTE.
2382 */
2383 for (cnt = 0; cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
2384 *cleanlist[cnt].pte = 0;
2385 pmap_pte_delref(pmap, cleanlist[cnt].va);
2386 }
2387 *pte = 0;
2388 pmap_pte_delref(pmap, sva);
2389 cleanlist_idx++;
2390 } else {
2391 /*
2392 * We've already nuked the cache and
2393 * TLB, so just carry on regardless,
2394 * and we won't need to do it again
2395 */
2396 *pte = 0;
2397 pmap_pte_delref(pmap, sva);
2398 }
2399
2400 /*
2401 * Update flags. In a number of circumstances,
2402 * we could cluster a lot of these and do a
2403 * number of sequential pages in one go.
2404 */
2405 if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
2406 struct pv_entry *pve;
2407 simple_lock(&pg->mdpage.pvh_slock);
2408 pve = pmap_remove_pv(pg, pmap, sva);
2409 pmap_free_pv(pmap, pve);
2410 pmap_vac_me_harder(pmap, pg, ptes, FALSE);
2411 simple_unlock(&pg->mdpage.pvh_slock);
2412 }
2413 }
2414 sva += NBPG;
2415 pte++;
2416 }
2417
2418 pmap_unmap_ptes(pmap);
2419 /*
2420 * Now, if we've fallen through down to here, chances are that there
2421 * are less than PMAP_REMOVE_CLEAN_LIST_SIZE mappings left.
2422 */
2423 if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
2424 u_int cnt;
2425
2426 for (cnt = 0; cnt < cleanlist_idx; cnt++) {
2427 if (pmap_active) {
2428 cpu_idcache_wbinv_range(cleanlist[cnt].va,
2429 NBPG);
2430 *cleanlist[cnt].pte = 0;
2431 cpu_tlb_flushID_SE(cleanlist[cnt].va);
2432 } else
2433 *cleanlist[cnt].pte = 0;
2434 pmap_pte_delref(pmap, cleanlist[cnt].va);
2435 }
2436 }
2437 PMAP_MAP_TO_HEAD_UNLOCK();
2438 }
2439
2440 /*
2441 * Routine: pmap_remove_all
2442 * Function:
2443 * Removes this physical page from
2444 * all physical maps in which it resides.
2445 * Reflects back modify bits to the pager.
2446 */
2447
2448 static void
2449 pmap_remove_all(struct vm_page *pg)
2450 {
2451 struct pv_entry *pv, *npv;
2452 struct pmap *pmap;
2453 pt_entry_t *pte, *ptes;
2454
2455 PDEBUG(0, printf("pmap_remove_all: pa=%lx ", VM_PAGE_TO_PHYS(pg)));
2456
2457 /* set vm_page => pmap locking */
2458 PMAP_HEAD_TO_MAP_LOCK();
2459
2460 simple_lock(&pg->mdpage.pvh_slock);
2461
2462 pv = pg->mdpage.pvh_list;
2463 if (pv == NULL) {
2464 PDEBUG(0, printf("free page\n"));
2465 simple_unlock(&pg->mdpage.pvh_slock);
2466 PMAP_HEAD_TO_MAP_UNLOCK();
2467 return;
2468 }
2469 pmap_clean_page(pv, FALSE);
2470
2471 while (pv) {
2472 pmap = pv->pv_pmap;
2473 ptes = pmap_map_ptes(pmap);
2474 pte = &ptes[arm_btop(pv->pv_va)];
2475
2476 PDEBUG(0, printf("[%p,%08x,%08lx,%08x] ", pmap, *pte,
2477 pv->pv_va, pv->pv_flags));
2478 #ifdef DEBUG
2479 if (pmap_pde_page(pmap_pde(pmap, pv->pv_va)) == 0 ||
2480 pmap_pte_v(pte) == 0 ||
2481 pmap_pte_pa(pte) != VM_PAGE_TO_PHYS(pg))
2482 panic("pmap_remove_all: bad mapping");
2483 #endif /* DEBUG */
2484
2485 /*
2486 * Update statistics
2487 */
2488 --pmap->pm_stats.resident_count;
2489
2490 /* Wired bit */
2491 if (pv->pv_flags & PVF_WIRED)
2492 --pmap->pm_stats.wired_count;
2493
2494 /*
2495 * Invalidate the PTEs.
2496 * XXX: should cluster them up and invalidate as many
2497 * as possible at once.
2498 */
2499
2500 #ifdef needednotdone
2501 reduce wiring count on page table pages as references drop
2502 #endif
2503
2504 *pte = 0;
2505 pmap_pte_delref(pmap, pv->pv_va);
2506
2507 npv = pv->pv_next;
2508 pmap_free_pv(pmap, pv);
2509 pv = npv;
2510 pmap_unmap_ptes(pmap);
2511 }
2512 pg->mdpage.pvh_list = NULL;
2513 simple_unlock(&pg->mdpage.pvh_slock);
2514 PMAP_HEAD_TO_MAP_UNLOCK();
2515
2516 PDEBUG(0, printf("done\n"));
2517 cpu_tlb_flushID();
2518 cpu_cpwait();
2519 }
2520
2521
2522 /*
2523 * Set the physical protection on the specified range of this map as requested.
2524 */
2525
2526 void
2527 pmap_protect(struct pmap *pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
2528 {
2529 pt_entry_t *pte = NULL, *ptes;
2530 struct vm_page *pg;
2531 int armprot;
2532 int flush = 0;
2533 paddr_t pa;
2534
2535 PDEBUG(0, printf("pmap_protect: pmap=%p %08lx->%08lx %x\n",
2536 pmap, sva, eva, prot));
2537
2538 if (~prot & VM_PROT_READ) {
2539 /* Just remove the mappings. */
2540 pmap_remove(pmap, sva, eva);
2541 /* pmap_update not needed as it should be called by the caller
2542 * of pmap_protect */
2543 return;
2544 }
2545 if (prot & VM_PROT_WRITE) {
2546 /*
2547 * If this is a read->write transition, just ignore it and let
2548 * uvm_fault() take care of it later.
2549 */
2550 return;
2551 }
2552
2553 /* Need to lock map->head */
2554 PMAP_MAP_TO_HEAD_LOCK();
2555
2556 ptes = pmap_map_ptes(pmap);
2557
2558 /*
2559 * OK, at this point, we know we're doing write-protect operation.
2560 * If the pmap is active, write-back the range.
2561 */
2562 if (pmap_is_curpmap(pmap))
2563 cpu_dcache_wb_range(sva, eva - sva);
2564
2565 /*
2566 * We need to acquire a pointer to a page table page before entering
2567 * the following loop.
2568 */
2569 while (sva < eva) {
2570 if (pmap_pde_page(pmap_pde(pmap, sva)))
2571 break;
2572 sva = (sva & L1_S_FRAME) + L1_S_SIZE;
2573 }
2574
2575 pte = &ptes[arm_btop(sva)];
2576
2577 while (sva < eva) {
2578 /* only check once in a while */
2579 if ((sva & L2_ADDR_BITS) == 0) {
2580 if (!pmap_pde_page(pmap_pde(pmap, sva))) {
2581 /* We can race ahead here, to the next pde. */
2582 sva += L1_S_SIZE;
2583 pte += arm_btop(L1_S_SIZE);
2584 continue;
2585 }
2586 }
2587
2588 if (!pmap_pte_v(pte))
2589 goto next;
2590
2591 flush = 1;
2592
2593 armprot = 0;
2594 if (sva < VM_MAXUSER_ADDRESS)
2595 armprot |= L2_S_PROT_U;
2596 else if (sva < VM_MAX_ADDRESS)
2597 armprot |= L2_S_PROT_W; /* XXX Ekk what is this ? */
2598 *pte = (*pte & 0xfffff00f) | armprot;
2599
2600 pa = pmap_pte_pa(pte);
2601
2602 /* Get the physical page index */
2603
2604 /* Clear write flag */
2605 if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
2606 simple_lock(&pg->mdpage.pvh_slock);
2607 (void) pmap_modify_pv(pmap, sva, pg, PVF_WRITE, 0);
2608 pmap_vac_me_harder(pmap, pg, ptes, FALSE);
2609 simple_unlock(&pg->mdpage.pvh_slock);
2610 }
2611
2612 next:
2613 sva += NBPG;
2614 pte++;
2615 }
2616 pmap_unmap_ptes(pmap);
2617 PMAP_MAP_TO_HEAD_UNLOCK();
2618 if (flush)
2619 cpu_tlb_flushID();
2620 }
2621
2622 /*
2623 * void pmap_enter(struct pmap *pmap, vaddr_t va, paddr_t pa, vm_prot_t prot,
2624 * int flags)
2625 *
2626 * Insert the given physical page (p) at
2627 * the specified virtual address (v) in the
2628 * target physical map with the protection requested.
2629 *
2630 * If specified, the page will be wired down, meaning
2631 * that the related pte can not be reclaimed.
2632 *
2633 * NB: This is the only routine which MAY NOT lazy-evaluate
2634 * or lose information. That is, this routine must actually
2635 * insert this page into the given map NOW.
2636 */
2637
2638 int
2639 pmap_enter(struct pmap *pmap, vaddr_t va, paddr_t pa, vm_prot_t prot,
2640 int flags)
2641 {
2642 pt_entry_t *ptes, opte, npte;
2643 paddr_t opa;
2644 boolean_t wired = (flags & PMAP_WIRED) != 0;
2645 struct vm_page *pg;
2646 struct pv_entry *pve;
2647 int error, nflags;
2648
2649 PDEBUG(5, printf("pmap_enter: V%08lx P%08lx in pmap %p prot=%08x, wired = %d\n",
2650 va, pa, pmap, prot, wired));
2651
2652 #ifdef DIAGNOSTIC
2653 /* Valid address ? */
2654 if (va >= (pmap_curmaxkvaddr))
2655 panic("pmap_enter: too big");
2656 if (pmap != pmap_kernel() && va != 0) {
2657 if (va < VM_MIN_ADDRESS || va >= VM_MAXUSER_ADDRESS)
2658 panic("pmap_enter: kernel page in user map");
2659 } else {
2660 if (va >= VM_MIN_ADDRESS && va < VM_MAXUSER_ADDRESS)
2661 panic("pmap_enter: user page in kernel map");
2662 if (va >= VM_MAXUSER_ADDRESS && va < VM_MAX_ADDRESS)
2663 panic("pmap_enter: entering PT page");
2664 }
2665 #endif
2666
2667 KDASSERT(((va | pa) & PGOFSET) == 0);
2668
2669 /*
2670 * Get a pointer to the page. Later on in this function, we
2671 * test for a managed page by checking pg != NULL.
2672 */
2673 pg = pmap_initialized ? PHYS_TO_VM_PAGE(pa) : NULL;
2674
2675 /* get lock */
2676 PMAP_MAP_TO_HEAD_LOCK();
2677
2678 /*
2679 * map the ptes. If there's not already an L2 table for this
2680 * address, allocate one.
2681 */
2682 ptes = pmap_map_ptes(pmap); /* locks pmap */
2683 if (pmap_pde_v(pmap_pde(pmap, va)) == 0) {
2684 struct vm_page *ptp;
2685
2686 /* kernel should be pre-grown */
2687 KASSERT(pmap != pmap_kernel());
2688
2689 /* if failure is allowed then don't try too hard */
2690 ptp = pmap_get_ptp(pmap, va & L1_S_FRAME);
2691 if (ptp == NULL) {
2692 if (flags & PMAP_CANFAIL) {
2693 error = ENOMEM;
2694 goto out;
2695 }
2696 panic("pmap_enter: get ptp failed");
2697 }
2698 }
2699 opte = ptes[arm_btop(va)];
2700
2701 nflags = 0;
2702 if (prot & VM_PROT_WRITE)
2703 nflags |= PVF_WRITE;
2704 if (wired)
2705 nflags |= PVF_WIRED;
2706
2707 /* Is the pte valid ? If so then this page is already mapped */
2708 if (l2pte_valid(opte)) {
2709 /* Get the physical address of the current page mapped */
2710 opa = l2pte_pa(opte);
2711
2712 /* Are we mapping the same page ? */
2713 if (opa == pa) {
2714 /* Has the wiring changed ? */
2715 if (pg != NULL) {
2716 simple_lock(&pg->mdpage.pvh_slock);
2717 (void) pmap_modify_pv(pmap, va, pg,
2718 PVF_WRITE | PVF_WIRED, nflags);
2719 simple_unlock(&pg->mdpage.pvh_slock);
2720 }
2721 } else {
2722 struct vm_page *opg;
2723
2724 /* We are replacing the page with a new one. */
2725 cpu_idcache_wbinv_range(va, NBPG);
2726
2727 /*
2728 * If it is part of our managed memory then we
2729 * must remove it from the PV list
2730 */
2731 if ((opg = PHYS_TO_VM_PAGE(opa)) != NULL) {
2732 simple_lock(&opg->mdpage.pvh_slock);
2733 pve = pmap_remove_pv(opg, pmap, va);
2734 simple_unlock(&opg->mdpage.pvh_slock);
2735 } else {
2736 pve = NULL;
2737 }
2738
2739 goto enter;
2740 }
2741 } else {
2742 opa = 0;
2743 pve = NULL;
2744 pmap_pte_addref(pmap, va);
2745
2746 /* pte is not valid so we must be hooking in a new page */
2747 ++pmap->pm_stats.resident_count;
2748
2749 enter:
2750 /*
2751 * Enter on the PV list if part of our managed memory
2752 */
2753 if (pg != NULL) {
2754 if (pve == NULL) {
2755 pve = pmap_alloc_pv(pmap, ALLOCPV_NEED);
2756 if (pve == NULL) {
2757 if (flags & PMAP_CANFAIL) {
2758 error = ENOMEM;
2759 goto out;
2760 }
2761 panic("pmap_enter: no pv entries "
2762 "available");
2763 }
2764 }
2765 /* enter_pv locks pvh when adding */
2766 pmap_enter_pv(pg, pve, pmap, va, NULL, nflags);
2767 } else {
2768 if (pve != NULL)
2769 pmap_free_pv(pmap, pve);
2770 }
2771 }
2772
2773 /* Construct the pte, giving the correct access. */
2774 npte = pa;
2775
2776 /* VA 0 is magic. */
2777 if (pmap != pmap_kernel() && va != vector_page)
2778 npte |= L2_S_PROT_U;
2779
2780 if (pg != NULL) {
2781 #ifdef DIAGNOSTIC
2782 if ((flags & VM_PROT_ALL) & ~prot)
2783 panic("pmap_enter: access_type exceeds prot");
2784 #endif
2785 npte |= pte_l2_s_cache_mode;
2786 if (flags & VM_PROT_WRITE) {
2787 npte |= L2_S_PROTO | L2_S_PROT_W;
2788 pg->mdpage.pvh_attrs |= PVF_REF | PVF_MOD;
2789 } else if (flags & VM_PROT_ALL) {
2790 npte |= L2_S_PROTO;
2791 pg->mdpage.pvh_attrs |= PVF_REF;
2792 } else
2793 npte |= L2_TYPE_INV;
2794 } else {
2795 if (prot & VM_PROT_WRITE)
2796 npte |= L2_S_PROTO | L2_S_PROT_W;
2797 else if (prot & VM_PROT_ALL)
2798 npte |= L2_S_PROTO;
2799 else
2800 npte |= L2_TYPE_INV;
2801 }
2802
2803 ptes[arm_btop(va)] = npte;
2804
2805 if (pg != NULL) {
2806 simple_lock(&pg->mdpage.pvh_slock);
2807 pmap_vac_me_harder(pmap, pg, ptes, pmap_is_curpmap(pmap));
2808 simple_unlock(&pg->mdpage.pvh_slock);
2809 }
2810
2811 /* Better flush the TLB ... */
2812 cpu_tlb_flushID_SE(va);
2813 error = 0;
2814 out:
2815 pmap_unmap_ptes(pmap); /* unlocks pmap */
2816 PMAP_MAP_TO_HEAD_UNLOCK();
2817
2818 return error;
2819 }
2820
2821 /*
2822 * pmap_kenter_pa: enter a kernel mapping
2823 *
2824 * => no need to lock anything assume va is already allocated
2825 * => should be faster than normal pmap enter function
2826 */
2827 void
2828 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot)
2829 {
2830 pt_entry_t *pte;
2831
2832 pte = vtopte(va);
2833 KASSERT(!pmap_pte_v(pte));
2834
2835 *pte = L2_S_PROTO | pa |
2836 L2_S_PROT(PTE_KERNEL, prot) | pte_l2_s_cache_mode;
2837 }
2838
2839 void
2840 pmap_kremove(vaddr_t va, vsize_t len)
2841 {
2842 pt_entry_t *pte;
2843
2844 for (len >>= PAGE_SHIFT; len > 0; len--, va += PAGE_SIZE) {
2845
2846 /*
2847 * We assume that we will only be called with small
2848 * regions of memory.
2849 */
2850
2851 KASSERT(pmap_pde_page(pmap_pde(pmap_kernel(), va)));
2852 pte = vtopte(va);
2853 cpu_idcache_wbinv_range(va, PAGE_SIZE);
2854 *pte = 0;
2855 cpu_tlb_flushID_SE(va);
2856 }
2857 }
2858
2859 /*
2860 * pmap_page_protect:
2861 *
2862 * Lower the permission for all mappings to a given page.
2863 */
2864
2865 void
2866 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
2867 {
2868
2869 PDEBUG(0, printf("pmap_page_protect(pa=%lx, prot=%d)\n",
2870 VM_PAGE_TO_PHYS(pg), prot));
2871
2872 switch(prot) {
2873 case VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE:
2874 case VM_PROT_READ|VM_PROT_WRITE:
2875 return;
2876
2877 case VM_PROT_READ:
2878 case VM_PROT_READ|VM_PROT_EXECUTE:
2879 pmap_clearbit(pg, PVF_WRITE);
2880 break;
2881
2882 default:
2883 pmap_remove_all(pg);
2884 break;
2885 }
2886 }
2887
2888
2889 /*
2890 * Routine: pmap_unwire
2891 * Function: Clear the wired attribute for a map/virtual-address
2892 * pair.
2893 * In/out conditions:
2894 * The mapping must already exist in the pmap.
2895 */
2896
2897 void
2898 pmap_unwire(struct pmap *pmap, vaddr_t va)
2899 {
2900 pt_entry_t *ptes;
2901 struct vm_page *pg;
2902 paddr_t pa;
2903
2904 PMAP_MAP_TO_HEAD_LOCK();
2905 ptes = pmap_map_ptes(pmap); /* locks pmap */
2906
2907 if (pmap_pde_v(pmap_pde(pmap, va))) {
2908 #ifdef DIAGNOSTIC
2909 if (l2pte_valid(ptes[arm_btop(va)]) == 0)
2910 panic("pmap_unwire: invalid L2 PTE");
2911 #endif
2912 /* Extract the physical address of the page */
2913 pa = l2pte_pa(ptes[arm_btop(va)]);
2914
2915 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
2916 goto out;
2917
2918 /* Update the wired bit in the pv entry for this page. */
2919 simple_lock(&pg->mdpage.pvh_slock);
2920 (void) pmap_modify_pv(pmap, va, pg, PVF_WIRED, 0);
2921 simple_unlock(&pg->mdpage.pvh_slock);
2922 }
2923 #ifdef DIAGNOSTIC
2924 else {
2925 panic("pmap_unwire: invalid L1 PTE");
2926 }
2927 #endif
2928 out:
2929 pmap_unmap_ptes(pmap); /* unlocks pmap */
2930 PMAP_MAP_TO_HEAD_UNLOCK();
2931 }
2932
2933 /*
2934 * Routine: pmap_extract
2935 * Function:
2936 * Extract the physical page address associated
2937 * with the given map/virtual_address pair.
2938 */
2939 boolean_t
2940 pmap_extract(struct pmap *pmap, vaddr_t va, paddr_t *pap)
2941 {
2942 pd_entry_t *pde;
2943 pt_entry_t *pte, *ptes;
2944 paddr_t pa;
2945
2946 PDEBUG(5, printf("pmap_extract: pmap=%p, va=0x%08lx -> ", pmap, va));
2947
2948 ptes = pmap_map_ptes(pmap); /* locks pmap */
2949
2950 pde = pmap_pde(pmap, va);
2951 pte = &ptes[arm_btop(va)];
2952
2953 if (pmap_pde_section(pde)) {
2954 pa = (*pde & L1_S_FRAME) | (va & L1_S_OFFSET);
2955 PDEBUG(5, printf("section pa=0x%08lx\n", pa));
2956 goto out;
2957 } else if (pmap_pde_page(pde) == 0 || pmap_pte_v(pte) == 0) {
2958 PDEBUG(5, printf("no mapping\n"));
2959 goto failed;
2960 }
2961
2962 if ((*pte & L2_TYPE_MASK) == L2_TYPE_L) {
2963 pa = (*pte & L2_L_FRAME) | (va & L2_L_OFFSET);
2964 PDEBUG(5, printf("large page pa=0x%08lx\n", pa));
2965 goto out;
2966 }
2967
2968 pa = (*pte & L2_S_FRAME) | (va & L2_S_OFFSET);
2969 PDEBUG(5, printf("small page pa=0x%08lx\n", pa));
2970
2971 out:
2972 if (pap != NULL)
2973 *pap = pa;
2974
2975 pmap_unmap_ptes(pmap); /* unlocks pmap */
2976 return (TRUE);
2977
2978 failed:
2979 pmap_unmap_ptes(pmap); /* unlocks pmap */
2980 return (FALSE);
2981 }
2982
2983
2984 /*
2985 * pmap_copy:
2986 *
2987 * Copy the range specified by src_addr/len from the source map to the
2988 * range dst_addr/len in the destination map.
2989 *
2990 * This routine is only advisory and need not do anything.
2991 */
2992 /* Call deleted in <arm/arm32/pmap.h> */
2993
2994 #if defined(PMAP_DEBUG)
2995 void
2996 pmap_dump_pvlist(phys, m)
2997 vaddr_t phys;
2998 char *m;
2999 {
3000 struct vm_page *pg;
3001 struct pv_entry *pv;
3002
3003 if ((pg = PHYS_TO_VM_PAGE(phys)) == NULL) {
3004 printf("INVALID PA\n");
3005 return;
3006 }
3007 simple_lock(&pg->mdpage.pvh_slock);
3008 printf("%s %08lx:", m, phys);
3009 if (pg->mdpage.pvh_list == NULL) {
3010 simple_unlock(&pg->mdpage.pvh_slock);
3011 printf(" no mappings\n");
3012 return;
3013 }
3014
3015 for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next)
3016 printf(" pmap %p va %08lx flags %08x", pv->pv_pmap,
3017 pv->pv_va, pv->pv_flags);
3018
3019 printf("\n");
3020 simple_unlock(&pg->mdpage.pvh_slock);
3021 }
3022
3023 #endif /* PMAP_DEBUG */
3024
3025 static pt_entry_t *
3026 pmap_map_ptes(struct pmap *pmap)
3027 {
3028 struct proc *p;
3029
3030 /* the kernel's pmap is always accessible */
3031 if (pmap == pmap_kernel()) {
3032 return (pt_entry_t *)PTE_BASE;
3033 }
3034
3035 if (pmap_is_curpmap(pmap)) {
3036 simple_lock(&pmap->pm_obj.vmobjlock);
3037 return (pt_entry_t *)PTE_BASE;
3038 }
3039
3040 p = curproc;
3041 KDASSERT(p != NULL);
3042
3043 /* need to lock both curpmap and pmap: use ordered locking */
3044 if ((vaddr_t) pmap < (vaddr_t) p->p_vmspace->vm_map.pmap) {
3045 simple_lock(&pmap->pm_obj.vmobjlock);
3046 simple_lock(&p->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
3047 } else {
3048 simple_lock(&p->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
3049 simple_lock(&pmap->pm_obj.vmobjlock);
3050 }
3051
3052 pmap_map_in_l1(p->p_vmspace->vm_map.pmap, APTE_BASE, pmap->pm_pptpt,
3053 FALSE);
3054 cpu_tlb_flushD();
3055 cpu_cpwait();
3056 return (pt_entry_t *)APTE_BASE;
3057 }
3058
3059 /*
3060 * pmap_unmap_ptes: unlock the PTE mapping of "pmap"
3061 */
3062
3063 static void
3064 pmap_unmap_ptes(struct pmap *pmap)
3065 {
3066
3067 if (pmap == pmap_kernel()) {
3068 return;
3069 }
3070 if (pmap_is_curpmap(pmap)) {
3071 simple_unlock(&pmap->pm_obj.vmobjlock);
3072 } else {
3073 KDASSERT(curproc != NULL);
3074 simple_unlock(&pmap->pm_obj.vmobjlock);
3075 simple_unlock(
3076 &curproc->p_vmspace->vm_map.pmap->pm_obj.vmobjlock);
3077 }
3078 }
3079
3080 /*
3081 * Modify pte bits for all ptes corresponding to the given physical address.
3082 * We use `maskbits' rather than `clearbits' because we're always passing
3083 * constants and the latter would require an extra inversion at run-time.
3084 */
3085
3086 static void
3087 pmap_clearbit(struct vm_page *pg, u_int maskbits)
3088 {
3089 struct pv_entry *pv;
3090 pt_entry_t *ptes;
3091 vaddr_t va;
3092 int tlbentry;
3093
3094 PDEBUG(1, printf("pmap_clearbit: pa=%08lx mask=%08x\n",
3095 VM_PAGE_TO_PHYS(pg), maskbits));
3096
3097 tlbentry = 0;
3098
3099 PMAP_HEAD_TO_MAP_LOCK();
3100 simple_lock(&pg->mdpage.pvh_slock);
3101
3102 /*
3103 * Clear saved attributes (modify, reference)
3104 */
3105 pg->mdpage.pvh_attrs &= ~maskbits;
3106
3107 if (pg->mdpage.pvh_list == NULL) {
3108 simple_unlock(&pg->mdpage.pvh_slock);
3109 PMAP_HEAD_TO_MAP_UNLOCK();
3110 return;
3111 }
3112
3113 /*
3114 * Loop over all current mappings setting/clearing as appropos
3115 */
3116 for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
3117 va = pv->pv_va;
3118 pv->pv_flags &= ~maskbits;
3119 ptes = pmap_map_ptes(pv->pv_pmap); /* locks pmap */
3120 KASSERT(pmap_pde_v(pmap_pde(pv->pv_pmap, va)));
3121 if (maskbits & (PVF_WRITE|PVF_MOD)) {
3122 if ((pv->pv_flags & PVF_NC)) {
3123 /*
3124 * Entry is not cacheable: reenable
3125 * the cache, nothing to flush
3126 *
3127 * Don't turn caching on again if this
3128 * is a modified emulation. This
3129 * would be inconsitent with the
3130 * settings created by
3131 * pmap_vac_me_harder().
3132 *
3133 * There's no need to call
3134 * pmap_vac_me_harder() here: all
3135 * pages are loosing their write
3136 * permission.
3137 *
3138 */
3139 if (maskbits & PVF_WRITE) {
3140 ptes[arm_btop(va)] |=
3141 pte_l2_s_cache_mode;
3142 pv->pv_flags &= ~PVF_NC;
3143 }
3144 } else if (pmap_is_curpmap(pv->pv_pmap)) {
3145 /*
3146 * Entry is cacheable: check if pmap is
3147 * current if it is flush it,
3148 * otherwise it won't be in the cache
3149 */
3150 cpu_idcache_wbinv_range(pv->pv_va, NBPG);
3151 }
3152
3153 /* make the pte read only */
3154 ptes[arm_btop(va)] &= ~L2_S_PROT_W;
3155 }
3156
3157 if (maskbits & PVF_REF)
3158 ptes[arm_btop(va)] =
3159 (ptes[arm_btop(va)] & ~L2_TYPE_MASK) | L2_TYPE_INV;
3160
3161 if (pmap_is_curpmap(pv->pv_pmap)) {
3162 /*
3163 * if we had cacheable pte's we'd clean the
3164 * pte out to memory here
3165 *
3166 * flush tlb entry as it's in the current pmap
3167 */
3168 cpu_tlb_flushID_SE(pv->pv_va);
3169 }
3170 pmap_unmap_ptes(pv->pv_pmap); /* unlocks pmap */
3171 }
3172 cpu_cpwait();
3173
3174 simple_unlock(&pg->mdpage.pvh_slock);
3175 PMAP_HEAD_TO_MAP_UNLOCK();
3176 }
3177
3178 /*
3179 * pmap_clear_modify:
3180 *
3181 * Clear the "modified" attribute for a page.
3182 */
3183 boolean_t
3184 pmap_clear_modify(struct vm_page *pg)
3185 {
3186 boolean_t rv;
3187
3188 if (pg->mdpage.pvh_attrs & PVF_MOD) {
3189 rv = TRUE;
3190 pmap_clearbit(pg, PVF_MOD);
3191 } else
3192 rv = FALSE;
3193
3194 PDEBUG(0, printf("pmap_clear_modify pa=%08lx -> %d\n",
3195 VM_PAGE_TO_PHYS(pg), rv));
3196
3197 return (rv);
3198 }
3199
3200 /*
3201 * pmap_clear_reference:
3202 *
3203 * Clear the "referenced" attribute for a page.
3204 */
3205 boolean_t
3206 pmap_clear_reference(struct vm_page *pg)
3207 {
3208 boolean_t rv;
3209
3210 if (pg->mdpage.pvh_attrs & PVF_REF) {
3211 rv = TRUE;
3212 pmap_clearbit(pg, PVF_REF);
3213 } else
3214 rv = FALSE;
3215
3216 PDEBUG(0, printf("pmap_clear_reference pa=%08lx -> %d\n",
3217 VM_PAGE_TO_PHYS(pg), rv));
3218
3219 return (rv);
3220 }
3221
3222 /*
3223 * pmap_is_modified:
3224 *
3225 * Test if a page has the "modified" attribute.
3226 */
3227 /* See <arm/arm32/pmap.h> */
3228
3229 /*
3230 * pmap_is_referenced:
3231 *
3232 * Test if a page has the "referenced" attribute.
3233 */
3234 /* See <arm/arm32/pmap.h> */
3235
3236 int
3237 pmap_modified_emulation(struct pmap *pmap, vaddr_t va)
3238 {
3239 pt_entry_t *ptes;
3240 struct vm_page *pg;
3241 paddr_t pa;
3242 u_int flags;
3243 int rv = 0;
3244
3245 PDEBUG(2, printf("pmap_modified_emulation\n"));
3246
3247 PMAP_MAP_TO_HEAD_LOCK();
3248 ptes = pmap_map_ptes(pmap); /* locks pmap */
3249
3250 if (pmap_pde_v(pmap_pde(pmap, va)) == 0) {
3251 PDEBUG(2, printf("L1 PTE invalid\n"));
3252 goto out;
3253 }
3254
3255 PDEBUG(1, printf("pte=%08x\n", ptes[arm_btop(va)]));
3256
3257 /* Check for a invalid pte */
3258 if (l2pte_valid(ptes[arm_btop(va)]) == 0)
3259 goto out;
3260
3261 /* This can happen if user code tries to access kernel memory. */
3262 if ((ptes[arm_btop(va)] & L2_S_PROT_W) != 0)
3263 goto out;
3264
3265 /* Extract the physical address of the page */
3266 pa = l2pte_pa(ptes[arm_btop(va)]);
3267 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
3268 goto out;
3269
3270 /* Get the current flags for this page. */
3271 simple_lock(&pg->mdpage.pvh_slock);
3272
3273 flags = pmap_modify_pv(pmap, va, pg, 0, 0);
3274 PDEBUG(2, printf("pmap_modified_emulation: flags = %08x\n", flags));
3275
3276 /*
3277 * Do the flags say this page is writable ? If not then it is a
3278 * genuine write fault. If yes then the write fault is our fault
3279 * as we did not reflect the write access in the PTE. Now we know
3280 * a write has occurred we can correct this and also set the
3281 * modified bit
3282 */
3283 if (~flags & PVF_WRITE) {
3284 simple_unlock(&pg->mdpage.pvh_slock);
3285 goto out;
3286 }
3287
3288 PDEBUG(0,
3289 printf("pmap_modified_emulation: Got a hit va=%08lx, pte = %08x\n",
3290 va, ptes[arm_btop(va)]));
3291 pg->mdpage.pvh_attrs |= PVF_REF | PVF_MOD;
3292
3293 /*
3294 * Re-enable write permissions for the page. No need to call
3295 * pmap_vac_me_harder(), since this is just a
3296 * modified-emulation fault, and the PVF_WRITE bit isn't changing.
3297 * We've already set the cacheable bits based on the assumption
3298 * that we can write to this page.
3299 */
3300 ptes[arm_btop(va)] =
3301 (ptes[arm_btop(va)] & ~L2_TYPE_MASK) | L2_S_PROTO | L2_S_PROT_W;
3302 PDEBUG(0, printf("->(%08x)\n", ptes[arm_btop(va)]));
3303
3304 simple_unlock(&pg->mdpage.pvh_slock);
3305
3306 cpu_tlb_flushID_SE(va);
3307 cpu_cpwait();
3308 rv = 1;
3309 out:
3310 pmap_unmap_ptes(pmap); /* unlocks pmap */
3311 PMAP_MAP_TO_HEAD_UNLOCK();
3312 return (rv);
3313 }
3314
3315 int
3316 pmap_handled_emulation(struct pmap *pmap, vaddr_t va)
3317 {
3318 pt_entry_t *ptes;
3319 struct vm_page *pg;
3320 paddr_t pa;
3321 int rv = 0;
3322
3323 PDEBUG(2, printf("pmap_handled_emulation\n"));
3324
3325 PMAP_MAP_TO_HEAD_LOCK();
3326 ptes = pmap_map_ptes(pmap); /* locks pmap */
3327
3328 if (pmap_pde_v(pmap_pde(pmap, va)) == 0) {
3329 PDEBUG(2, printf("L1 PTE invalid\n"));
3330 goto out;
3331 }
3332
3333 PDEBUG(1, printf("pte=%08x\n", ptes[arm_btop(va)]));
3334
3335 /* Check for invalid pte */
3336 if (l2pte_valid(ptes[arm_btop(va)]) == 0)
3337 goto out;
3338
3339 /* This can happen if user code tries to access kernel memory. */
3340 if ((ptes[arm_btop(va)] & L2_TYPE_MASK) != L2_TYPE_INV)
3341 goto out;
3342
3343 /* Extract the physical address of the page */
3344 pa = l2pte_pa(ptes[arm_btop(va)]);
3345 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
3346 goto out;
3347
3348 simple_lock(&pg->mdpage.pvh_slock);
3349
3350 /*
3351 * Ok we just enable the pte and mark the attibs as handled
3352 * XXX Should we traverse the PV list and enable all PTEs?
3353 */
3354 PDEBUG(0,
3355 printf("pmap_handled_emulation: Got a hit va=%08lx pte = %08x\n",
3356 va, ptes[arm_btop(va)]));
3357 pg->mdpage.pvh_attrs |= PVF_REF;
3358
3359 ptes[arm_btop(va)] = (ptes[arm_btop(va)] & ~L2_TYPE_MASK) | L2_S_PROTO;
3360 PDEBUG(0, printf("->(%08x)\n", ptes[arm_btop(va)]));
3361
3362 simple_unlock(&pg->mdpage.pvh_slock);
3363
3364 cpu_tlb_flushID_SE(va);
3365 cpu_cpwait();
3366 rv = 1;
3367 out:
3368 pmap_unmap_ptes(pmap); /* unlocks pmap */
3369 PMAP_MAP_TO_HEAD_UNLOCK();
3370 return (rv);
3371 }
3372
3373 /*
3374 * pmap_collect: free resources held by a pmap
3375 *
3376 * => optional function.
3377 * => called when a process is swapped out to free memory.
3378 */
3379
3380 void
3381 pmap_collect(struct pmap *pmap)
3382 {
3383 }
3384
3385 /*
3386 * Routine: pmap_procwr
3387 *
3388 * Function:
3389 * Synchronize caches corresponding to [addr, addr+len) in p.
3390 *
3391 */
3392 void
3393 pmap_procwr(struct proc *p, vaddr_t va, int len)
3394 {
3395 /* We only need to do anything if it is the current process. */
3396 if (p == curproc)
3397 cpu_icache_sync_range(va, len);
3398 }
3399 /*
3400 * PTP functions
3401 */
3402
3403 /*
3404 * pmap_get_ptp: get a PTP (if there isn't one, allocate a new one)
3405 *
3406 * => pmap should NOT be pmap_kernel()
3407 * => pmap should be locked
3408 */
3409
3410 static struct vm_page *
3411 pmap_get_ptp(struct pmap *pmap, vaddr_t va)
3412 {
3413 struct vm_page *ptp;
3414
3415 if (pmap_pde_page(pmap_pde(pmap, va))) {
3416
3417 /* valid... check hint (saves us a PA->PG lookup) */
3418 if (pmap->pm_ptphint &&
3419 (pmap->pm_pdir[pmap_pdei(va)] & L2_S_FRAME) ==
3420 VM_PAGE_TO_PHYS(pmap->pm_ptphint))
3421 return (pmap->pm_ptphint);
3422 ptp = uvm_pagelookup(&pmap->pm_obj, va);
3423 #ifdef DIAGNOSTIC
3424 if (ptp == NULL)
3425 panic("pmap_get_ptp: unmanaged user PTP");
3426 #endif
3427 pmap->pm_ptphint = ptp;
3428 return(ptp);
3429 }
3430
3431 /* allocate a new PTP (updates ptphint) */
3432 return(pmap_alloc_ptp(pmap, va));
3433 }
3434
3435 /*
3436 * pmap_alloc_ptp: allocate a PTP for a PMAP
3437 *
3438 * => pmap should already be locked by caller
3439 * => we use the ptp's wire_count to count the number of active mappings
3440 * in the PTP (we start it at one to prevent any chance this PTP
3441 * will ever leak onto the active/inactive queues)
3442 */
3443
3444 /*__inline */ static struct vm_page *
3445 pmap_alloc_ptp(struct pmap *pmap, vaddr_t va)
3446 {
3447 struct vm_page *ptp;
3448
3449 ptp = uvm_pagealloc(&pmap->pm_obj, va, NULL,
3450 UVM_PGA_USERESERVE|UVM_PGA_ZERO);
3451 if (ptp == NULL)
3452 return (NULL);
3453
3454 /* got one! */
3455 ptp->flags &= ~PG_BUSY; /* never busy */
3456 ptp->wire_count = 1; /* no mappings yet */
3457 pmap_map_in_l1(pmap, va, VM_PAGE_TO_PHYS(ptp), TRUE);
3458 pmap->pm_stats.resident_count++; /* count PTP as resident */
3459 pmap->pm_ptphint = ptp;
3460 return (ptp);
3461 }
3462
3463 vaddr_t
3464 pmap_growkernel(vaddr_t maxkvaddr)
3465 {
3466 struct pmap *kpm = pmap_kernel(), *pm;
3467 int s;
3468 paddr_t ptaddr;
3469 struct vm_page *ptp;
3470
3471 if (maxkvaddr <= pmap_curmaxkvaddr)
3472 goto out; /* we are OK */
3473 NPDEBUG(PDB_GROWKERN, printf("pmap_growkernel: growing kernel from %lx to %lx\n",
3474 pmap_curmaxkvaddr, maxkvaddr));
3475
3476 /*
3477 * whoops! we need to add kernel PTPs
3478 */
3479
3480 s = splhigh(); /* to be safe */
3481 simple_lock(&kpm->pm_obj.vmobjlock);
3482 /* due to the way the arm pmap works we map 4MB at a time */
3483 for (/*null*/ ; pmap_curmaxkvaddr < maxkvaddr;
3484 pmap_curmaxkvaddr += 4 * L1_S_SIZE) {
3485
3486 if (uvm.page_init_done == FALSE) {
3487
3488 /*
3489 * we're growing the kernel pmap early (from
3490 * uvm_pageboot_alloc()). this case must be
3491 * handled a little differently.
3492 */
3493
3494 if (uvm_page_physget(&ptaddr) == FALSE)
3495 panic("pmap_growkernel: out of memory");
3496 pmap_zero_page(ptaddr);
3497
3498 /* map this page in */
3499 pmap_map_in_l1(kpm, pmap_curmaxkvaddr, ptaddr, TRUE);
3500
3501 /* count PTP as resident */
3502 kpm->pm_stats.resident_count++;
3503 continue;
3504 }
3505
3506 /*
3507 * THIS *MUST* BE CODED SO AS TO WORK IN THE
3508 * pmap_initialized == FALSE CASE! WE MAY BE
3509 * INVOKED WHILE pmap_init() IS RUNNING!
3510 */
3511
3512 if ((ptp = pmap_alloc_ptp(kpm, pmap_curmaxkvaddr)) == NULL)
3513 panic("pmap_growkernel: alloc ptp failed");
3514
3515 /* distribute new kernel PTP to all active pmaps */
3516 simple_lock(&pmaps_lock);
3517 LIST_FOREACH(pm, &pmaps, pm_list) {
3518 pmap_map_in_l1(pm, pmap_curmaxkvaddr,
3519 VM_PAGE_TO_PHYS(ptp), TRUE);
3520 }
3521
3522 simple_unlock(&pmaps_lock);
3523 }
3524
3525 /*
3526 * flush out the cache, expensive but growkernel will happen so
3527 * rarely
3528 */
3529 cpu_tlb_flushD();
3530 cpu_cpwait();
3531
3532 simple_unlock(&kpm->pm_obj.vmobjlock);
3533 splx(s);
3534
3535 out:
3536 return (pmap_curmaxkvaddr);
3537 }
3538
3539 /************************ Utility routines ****************************/
3540
3541 /*
3542 * vector_page_setprot:
3543 *
3544 * Manipulate the protection of the vector page.
3545 */
3546 void
3547 vector_page_setprot(int prot)
3548 {
3549 pt_entry_t *pte;
3550
3551 pte = vtopte(vector_page);
3552
3553 *pte = (*pte & ~L1_S_PROT_MASK) | L2_S_PROT(PTE_KERNEL, prot);
3554 cpu_tlb_flushD_SE(vector_page);
3555 cpu_cpwait();
3556 }
3557
3558 /************************ Bootstrapping routines ****************************/
3559
3560 /*
3561 * This list exists for the benefit of pmap_map_chunk(). It keeps track
3562 * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
3563 * find them as necessary.
3564 *
3565 * Note that the data on this list is not valid after initarm() returns.
3566 */
3567 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
3568
3569 static vaddr_t
3570 kernel_pt_lookup(paddr_t pa)
3571 {
3572 pv_addr_t *pv;
3573
3574 SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
3575 if (pv->pv_pa == pa)
3576 return (pv->pv_va);
3577 }
3578 return (0);
3579 }
3580
3581 /*
3582 * pmap_map_section:
3583 *
3584 * Create a single section mapping.
3585 */
3586 void
3587 pmap_map_section(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
3588 {
3589 pd_entry_t *pde = (pd_entry_t *) l1pt;
3590 pd_entry_t fl = (cache == PTE_CACHE) ? pte_l1_s_cache_mode : 0;
3591
3592 KASSERT(((va | pa) & L1_S_OFFSET) == 0);
3593
3594 pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
3595 L1_S_PROT(PTE_KERNEL, prot) | fl;
3596 }
3597
3598 /*
3599 * pmap_map_entry:
3600 *
3601 * Create a single page mapping.
3602 */
3603 void
3604 pmap_map_entry(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
3605 {
3606 pd_entry_t *pde = (pd_entry_t *) l1pt;
3607 pt_entry_t fl = (cache == PTE_CACHE) ? pte_l2_s_cache_mode : 0;
3608 pt_entry_t *pte;
3609
3610 KASSERT(((va | pa) & PGOFSET) == 0);
3611
3612 if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
3613 panic("pmap_map_entry: no L2 table for VA 0x%08lx", va);
3614
3615 pte = (pt_entry_t *)
3616 kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
3617 if (pte == NULL)
3618 panic("pmap_map_entry: can't find L2 table for VA 0x%08lx", va);
3619
3620 pte[(va >> PGSHIFT) & 0x3ff] = L2_S_PROTO | pa |
3621 L2_S_PROT(PTE_KERNEL, prot) | fl;
3622 }
3623
3624 /*
3625 * pmap_link_l2pt:
3626 *
3627 * Link the L2 page table specified by "pa" into the L1
3628 * page table at the slot for "va".
3629 */
3630 void
3631 pmap_link_l2pt(vaddr_t l1pt, vaddr_t va, pv_addr_t *l2pv)
3632 {
3633 pd_entry_t *pde = (pd_entry_t *) l1pt;
3634 u_int slot = va >> L1_S_SHIFT;
3635
3636 KASSERT((l2pv->pv_pa & PGOFSET) == 0);
3637
3638 pde[slot + 0] = L1_C_PROTO | (l2pv->pv_pa + 0x000);
3639 pde[slot + 1] = L1_C_PROTO | (l2pv->pv_pa + 0x400);
3640 pde[slot + 2] = L1_C_PROTO | (l2pv->pv_pa + 0x800);
3641 pde[slot + 3] = L1_C_PROTO | (l2pv->pv_pa + 0xc00);
3642
3643 SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
3644 }
3645
3646 /*
3647 * pmap_map_chunk:
3648 *
3649 * Map a chunk of memory using the most efficient mappings
3650 * possible (section, large page, small page) into the
3651 * provided L1 and L2 tables at the specified virtual address.
3652 */
3653 vsize_t
3654 pmap_map_chunk(vaddr_t l1pt, vaddr_t va, paddr_t pa, vsize_t size,
3655 int prot, int cache)
3656 {
3657 pd_entry_t *pde = (pd_entry_t *) l1pt;
3658 pt_entry_t *pte, fl;
3659 vsize_t resid;
3660 int i;
3661
3662 resid = (size + (NBPG - 1)) & ~(NBPG - 1);
3663
3664 if (l1pt == 0)
3665 panic("pmap_map_chunk: no L1 table provided");
3666
3667 #ifdef VERBOSE_INIT_ARM
3668 printf("pmap_map_chunk: pa=0x%lx va=0x%lx size=0x%lx resid=0x%lx "
3669 "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
3670 #endif
3671
3672 size = resid;
3673
3674 while (resid > 0) {
3675 /* See if we can use a section mapping. */
3676 if (((pa | va) & L1_S_OFFSET) == 0 &&
3677 resid >= L1_S_SIZE) {
3678 fl = (cache == PTE_CACHE) ? pte_l1_s_cache_mode : 0;
3679 #ifdef VERBOSE_INIT_ARM
3680 printf("S");
3681 #endif
3682 pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
3683 L1_S_PROT(PTE_KERNEL, prot) | fl;
3684 va += L1_S_SIZE;
3685 pa += L1_S_SIZE;
3686 resid -= L1_S_SIZE;
3687 continue;
3688 }
3689
3690 /*
3691 * Ok, we're going to use an L2 table. Make sure
3692 * one is actually in the corresponding L1 slot
3693 * for the current VA.
3694 */
3695 if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
3696 panic("pmap_map_chunk: no L2 table for VA 0x%08lx", va);
3697
3698 pte = (pt_entry_t *)
3699 kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
3700 if (pte == NULL)
3701 panic("pmap_map_chunk: can't find L2 table for VA"
3702 "0x%08lx", va);
3703
3704 /* See if we can use a L2 large page mapping. */
3705 if (((pa | va) & L2_L_OFFSET) == 0 &&
3706 resid >= L2_L_SIZE) {
3707 fl = (cache == PTE_CACHE) ? pte_l2_l_cache_mode : 0;
3708 #ifdef VERBOSE_INIT_ARM
3709 printf("L");
3710 #endif
3711 for (i = 0; i < 16; i++) {
3712 pte[((va >> PGSHIFT) & 0x3f0) + i] =
3713 L2_L_PROTO | pa |
3714 L2_L_PROT(PTE_KERNEL, prot) | fl;
3715 }
3716 va += L2_L_SIZE;
3717 pa += L2_L_SIZE;
3718 resid -= L2_L_SIZE;
3719 continue;
3720 }
3721
3722 /* Use a small page mapping. */
3723 fl = (cache == PTE_CACHE) ? pte_l2_s_cache_mode : 0;
3724 #ifdef VERBOSE_INIT_ARM
3725 printf("P");
3726 #endif
3727 pte[(va >> PGSHIFT) & 0x3ff] = L2_S_PROTO | pa |
3728 L2_S_PROT(PTE_KERNEL, prot) | fl;
3729 va += NBPG;
3730 pa += NBPG;
3731 resid -= NBPG;
3732 }
3733 #ifdef VERBOSE_INIT_ARM
3734 printf("\n");
3735 #endif
3736 return (size);
3737 }
3738
3739 /********************** PTE initialization routines **************************/
3740
3741 /*
3742 * These routines are called when the CPU type is identified to set up
3743 * the PTE prototypes, cache modes, etc.
3744 *
3745 * The variables are always here, just in case LKMs need to reference
3746 * them (though, they shouldn't).
3747 */
3748
3749 pt_entry_t pte_l1_s_cache_mode;
3750 pt_entry_t pte_l1_s_cache_mask;
3751
3752 pt_entry_t pte_l2_l_cache_mode;
3753 pt_entry_t pte_l2_l_cache_mask;
3754
3755 pt_entry_t pte_l2_s_cache_mode;
3756 pt_entry_t pte_l2_s_cache_mask;
3757
3758 pt_entry_t pte_l2_s_prot_u;
3759 pt_entry_t pte_l2_s_prot_w;
3760 pt_entry_t pte_l2_s_prot_mask;
3761
3762 pt_entry_t pte_l1_s_proto;
3763 pt_entry_t pte_l1_c_proto;
3764 pt_entry_t pte_l2_s_proto;
3765
3766 void (*pmap_copy_page_func)(paddr_t, paddr_t);
3767 void (*pmap_zero_page_func)(paddr_t);
3768
3769 #if ARM_MMU_GENERIC == 1
3770 void
3771 pmap_pte_init_generic(void)
3772 {
3773
3774 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
3775 pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
3776
3777 pte_l2_l_cache_mode = L2_B|L2_C;
3778 pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
3779
3780 pte_l2_s_cache_mode = L2_B|L2_C;
3781 pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
3782
3783 pte_l2_s_prot_u = L2_S_PROT_U_generic;
3784 pte_l2_s_prot_w = L2_S_PROT_W_generic;
3785 pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
3786
3787 pte_l1_s_proto = L1_S_PROTO_generic;
3788 pte_l1_c_proto = L1_C_PROTO_generic;
3789 pte_l2_s_proto = L2_S_PROTO_generic;
3790
3791 pmap_copy_page_func = pmap_copy_page_generic;
3792 pmap_zero_page_func = pmap_zero_page_generic;
3793 }
3794
3795 #if defined(CPU_ARM9)
3796 void
3797 pmap_pte_init_arm9(void)
3798 {
3799
3800 /*
3801 * ARM9 is compatible with generic, but we want to use
3802 * write-through caching for now.
3803 */
3804 pmap_pte_init_generic();
3805
3806 pte_l1_s_cache_mode = L1_S_C;
3807 pte_l2_l_cache_mode = L2_C;
3808 pte_l2_s_cache_mode = L2_C;
3809 }
3810 #endif /* CPU_ARM9 */
3811 #endif /* ARM_MMU_GENERIC == 1 */
3812
3813 #if ARM_MMU_XSCALE == 1
3814 void
3815 pmap_pte_init_xscale(void)
3816 {
3817 uint32_t auxctl;
3818
3819 pte_l1_s_cache_mode = L1_S_B|L1_S_C;
3820 pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
3821
3822 pte_l2_l_cache_mode = L2_B|L2_C;
3823 pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
3824
3825 pte_l2_s_cache_mode = L2_B|L2_C;
3826 pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
3827
3828 #ifdef XSCALE_CACHE_WRITE_THROUGH
3829 /*
3830 * Some versions of the XScale core have various bugs in
3831 * their cache units, the work-around for which is to run
3832 * the cache in write-through mode. Unfortunately, this
3833 * has a major (negative) impact on performance. So, we
3834 * go ahead and run fast-and-loose, in the hopes that we
3835 * don't line up the planets in a way that will trip the
3836 * bugs.
3837 *
3838 * However, we give you the option to be slow-but-correct.
3839 */
3840 pte_l1_s_cache_mode = L1_S_C;
3841 pte_l2_l_cache_mode = L2_C;
3842 pte_l2_s_cache_mode = L2_C;
3843 #endif /* XSCALE_CACHE_WRITE_THROUGH */
3844
3845 pte_l2_s_prot_u = L2_S_PROT_U_xscale;
3846 pte_l2_s_prot_w = L2_S_PROT_W_xscale;
3847 pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
3848
3849 pte_l1_s_proto = L1_S_PROTO_xscale;
3850 pte_l1_c_proto = L1_C_PROTO_xscale;
3851 pte_l2_s_proto = L2_S_PROTO_xscale;
3852
3853 pmap_copy_page_func = pmap_copy_page_xscale;
3854 pmap_zero_page_func = pmap_zero_page_xscale;
3855
3856 /*
3857 * Disable ECC protection of page table access, for now.
3858 */
3859 __asm __volatile("mrc p15, 0, %0, c1, c0, 1"
3860 : "=r" (auxctl));
3861 auxctl &= ~XSCALE_AUXCTL_P;
3862 __asm __volatile("mcr p15, 0, %0, c1, c0, 1"
3863 :
3864 : "r" (auxctl));
3865 }
3866
3867 /*
3868 * xscale_setup_minidata:
3869 *
3870 * Set up the mini-data cache clean area. We require the
3871 * caller to allocate the right amount of physically and
3872 * virtually contiguous space.
3873 */
3874 void
3875 xscale_setup_minidata(vaddr_t l1pt, vaddr_t va, paddr_t pa)
3876 {
3877 extern vaddr_t xscale_minidata_clean_addr;
3878 extern vsize_t xscale_minidata_clean_size; /* already initialized */
3879 pd_entry_t *pde = (pd_entry_t *) l1pt;
3880 pt_entry_t *pte;
3881 vsize_t size;
3882 uint32_t auxctl;
3883
3884 xscale_minidata_clean_addr = va;
3885
3886 /* Round it to page size. */
3887 size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
3888
3889 for (; size != 0;
3890 va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
3891 pte = (pt_entry_t *)
3892 kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
3893 if (pte == NULL)
3894 panic("xscale_setup_minidata: can't find L2 table for "
3895 "VA 0x%08lx", va);
3896 pte[(va >> PGSHIFT) & 0x3ff] = L2_S_PROTO | pa |
3897 L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
3898 L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);
3899 }
3900
3901 /*
3902 * Configure the mini-data cache for write-back with
3903 * read/write-allocate.
3904 *
3905 * NOTE: In order to reconfigure the mini-data cache, we must
3906 * make sure it contains no valid data! In order to do that,
3907 * we must issue a global data cache invalidate command!
3908 *
3909 * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
3910 * THIS IS VERY IMPORTANT!
3911 */
3912
3913 /* Invalidate data and mini-data. */
3914 __asm __volatile("mcr p15, 0, %0, c7, c6, 0"
3915 :
3916 : "r" (auxctl));
3917
3918
3919 __asm __volatile("mrc p15, 0, %0, c1, c0, 1"
3920 : "=r" (auxctl));
3921 auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
3922 __asm __volatile("mcr p15, 0, %0, c1, c0, 1"
3923 :
3924 : "r" (auxctl));
3925 }
3926 #endif /* ARM_MMU_XSCALE == 1 */
3927