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