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