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