]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/pmap.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / arm / arm / pmap.c
1 /* From: $NetBSD: pmap.c,v 1.148 2004/04/03 04:35:48 bsh Exp $ */
2 /*-
3  * Copyright 2004 Olivier Houchard.
4  * Copyright 2003 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Steve C. Woodford for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 /*-
39  * Copyright (c) 2002-2003 Wasabi Systems, Inc.
40  * Copyright (c) 2001 Richard Earnshaw
41  * Copyright (c) 2001-2002 Christopher Gilbert
42  * All rights reserved.
43  *
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. The name of the company nor the name of the author may be used to
50  *    endorse or promote products derived from this software without specific
51  *    prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
54  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
57  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  */
65 /*-
66  * Copyright (c) 1999 The NetBSD Foundation, Inc.
67  * All rights reserved.
68  *
69  * This code is derived from software contributed to The NetBSD Foundation
70  * by Charles M. Hannum.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions
74  * are met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce the above copyright
78  *    notice, this list of conditions and the following disclaimer in the
79  *    documentation and/or other materials provided with the distribution.
80  * 3. All advertising materials mentioning features or use of this software
81  *    must display the following acknowledgement:
82  *        This product includes software developed by the NetBSD
83  *        Foundation, Inc. and its contributors.
84  * 4. Neither the name of The NetBSD Foundation nor the names of its
85  *    contributors may be used to endorse or promote products derived
86  *    from this software without specific prior written permission.
87  *
88  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
89  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
90  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
91  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
92  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
93  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
94  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
95  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
96  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
97  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
98  * POSSIBILITY OF SUCH DAMAGE.
99  */
100
101 /*-
102  * Copyright (c) 1994-1998 Mark Brinicombe.
103  * Copyright (c) 1994 Brini.
104  * All rights reserved.
105  *
106  * This code is derived from software written for Brini by Mark Brinicombe
107  *
108  * Redistribution and use in source and binary forms, with or without
109  * modification, are permitted provided that the following conditions
110  * are met:
111  * 1. Redistributions of source code must retain the above copyright
112  *    notice, this list of conditions and the following disclaimer.
113  * 2. Redistributions in binary form must reproduce the above copyright
114  *    notice, this list of conditions and the following disclaimer in the
115  *    documentation and/or other materials provided with the distribution.
116  * 3. All advertising materials mentioning features or use of this software
117  *    must display the following acknowledgement:
118  *      This product includes software developed by Mark Brinicombe.
119  * 4. The name of the author may not be used to endorse or promote products
120  *    derived from this software without specific prior written permission.
121  *
122  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
123  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
124  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
125  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
126  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
127  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
128  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
129  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
130  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
131  *
132  * RiscBSD kernel project
133  *
134  * pmap.c
135  *
136  * Machine dependant vm stuff
137  *
138  * Created      : 20/09/94
139  */
140
141 /*
142  * Special compilation symbols
143  * PMAP_DEBUG           - Build in pmap_debug_level code
144  */
145 /* Include header files */
146
147 #include "opt_vm.h"
148
149 #include <sys/cdefs.h>
150 __FBSDID("$FreeBSD$");
151 #include <sys/param.h>
152 #include <sys/systm.h>
153 #include <sys/kernel.h>
154 #include <sys/ktr.h>
155 #include <sys/proc.h>
156 #include <sys/malloc.h>
157 #include <sys/msgbuf.h>
158 #include <sys/vmmeter.h>
159 #include <sys/mman.h>
160 #include <sys/smp.h>
161 #include <sys/sched.h>
162
163 #include <vm/vm.h>
164 #include <vm/uma.h>
165 #include <vm/pmap.h>
166 #include <vm/vm_kern.h>
167 #include <vm/vm_object.h>
168 #include <vm/vm_map.h>
169 #include <vm/vm_page.h>
170 #include <vm/vm_pageout.h>
171 #include <vm/vm_extern.h>
172 #include <sys/lock.h>
173 #include <sys/mutex.h>
174 #include <machine/md_var.h>
175 #include <machine/vmparam.h>
176 #include <machine/cpu.h>
177 #include <machine/cpufunc.h>
178 #include <machine/pcb.h>
179
180 #ifdef PMAP_DEBUG
181 #define PDEBUG(_lev_,_stat_) \
182         if (pmap_debug_level >= (_lev_)) \
183                 ((_stat_))
184 #define dprintf printf
185
186 int pmap_debug_level = 0;
187 #define PMAP_INLINE 
188 #else   /* PMAP_DEBUG */
189 #define PDEBUG(_lev_,_stat_) /* Nothing */
190 #define dprintf(x, arg...)
191 #define PMAP_INLINE __inline
192 #endif  /* PMAP_DEBUG */
193
194 extern struct pv_addr systempage;
195 /*
196  * Internal function prototypes
197  */
198 static void pmap_free_pv_entry (pv_entry_t);
199 static pv_entry_t pmap_get_pv_entry(void);
200
201 static void             pmap_enter_locked(pmap_t, vm_offset_t, vm_page_t,
202     vm_prot_t, boolean_t, int);
203 static __inline void    pmap_fix_cache(struct vm_page *, pmap_t,
204     vm_offset_t);
205 static void             pmap_alloc_l1(pmap_t);
206 static void             pmap_free_l1(pmap_t);
207 static void             pmap_use_l1(pmap_t);
208
209 static int              pmap_clearbit(struct vm_page *, u_int);
210
211 static struct l2_bucket *pmap_get_l2_bucket(pmap_t, vm_offset_t);
212 static struct l2_bucket *pmap_alloc_l2_bucket(pmap_t, vm_offset_t);
213 static void             pmap_free_l2_bucket(pmap_t, struct l2_bucket *, u_int);
214 static vm_offset_t      kernel_pt_lookup(vm_paddr_t);
215
216 static MALLOC_DEFINE(M_VMPMAP, "pmap", "PMAP L1");
217
218 vm_offset_t virtual_avail;      /* VA of first avail page (after kernel bss) */
219 vm_offset_t virtual_end;        /* VA of last avail page (end of kernel AS) */
220 vm_offset_t pmap_curmaxkvaddr;
221 vm_paddr_t kernel_l1pa;
222
223 extern void *end;
224 vm_offset_t kernel_vm_end = 0;
225
226 struct pmap kernel_pmap_store;
227 pmap_t kernel_pmap;
228
229 static pt_entry_t *csrc_pte, *cdst_pte;
230 static vm_offset_t csrcp, cdstp;
231 static struct mtx cmtx;
232
233 static void             pmap_init_l1(struct l1_ttable *, pd_entry_t *);
234 /*
235  * These routines are called when the CPU type is identified to set up
236  * the PTE prototypes, cache modes, etc.
237  *
238  * The variables are always here, just in case LKMs need to reference
239  * them (though, they shouldn't).
240  */
241
242 pt_entry_t      pte_l1_s_cache_mode;
243 pt_entry_t      pte_l1_s_cache_mode_pt;
244 pt_entry_t      pte_l1_s_cache_mask;
245
246 pt_entry_t      pte_l2_l_cache_mode;
247 pt_entry_t      pte_l2_l_cache_mode_pt;
248 pt_entry_t      pte_l2_l_cache_mask;
249
250 pt_entry_t      pte_l2_s_cache_mode;
251 pt_entry_t      pte_l2_s_cache_mode_pt;
252 pt_entry_t      pte_l2_s_cache_mask;
253
254 pt_entry_t      pte_l2_s_prot_u;
255 pt_entry_t      pte_l2_s_prot_w;
256 pt_entry_t      pte_l2_s_prot_mask;
257
258 pt_entry_t      pte_l1_s_proto;
259 pt_entry_t      pte_l1_c_proto;
260 pt_entry_t      pte_l2_s_proto;
261
262 void            (*pmap_copy_page_func)(vm_paddr_t, vm_paddr_t);
263 void            (*pmap_zero_page_func)(vm_paddr_t, int, int);
264 /*
265  * Which pmap is currently 'live' in the cache
266  *
267  * XXXSCW: Fix for SMP ...
268  */
269 union pmap_cache_state *pmap_cache_state;
270
271 struct msgbuf *msgbufp = 0;
272
273 extern void bcopy_page(vm_offset_t, vm_offset_t);
274 extern void bzero_page(vm_offset_t);
275
276 extern vm_offset_t alloc_firstaddr;
277
278 char *_tmppt;
279
280 /*
281  * Metadata for L1 translation tables.
282  */
283 struct l1_ttable {
284         /* Entry on the L1 Table list */
285         SLIST_ENTRY(l1_ttable) l1_link;
286
287         /* Entry on the L1 Least Recently Used list */
288         TAILQ_ENTRY(l1_ttable) l1_lru;
289
290         /* Track how many domains are allocated from this L1 */
291         volatile u_int l1_domain_use_count;
292
293         /*
294          * A free-list of domain numbers for this L1.
295          * We avoid using ffs() and a bitmap to track domains since ffs()
296          * is slow on ARM.
297          */
298         u_int8_t l1_domain_first;
299         u_int8_t l1_domain_free[PMAP_DOMAINS];
300
301         /* Physical address of this L1 page table */
302         vm_paddr_t l1_physaddr;
303
304         /* KVA of this L1 page table */
305         pd_entry_t *l1_kva;
306 };
307
308 /*
309  * Convert a virtual address into its L1 table index. That is, the
310  * index used to locate the L2 descriptor table pointer in an L1 table.
311  * This is basically used to index l1->l1_kva[].
312  *
313  * Each L2 descriptor table represents 1MB of VA space.
314  */
315 #define L1_IDX(va)              (((vm_offset_t)(va)) >> L1_S_SHIFT)
316
317 /*
318  * L1 Page Tables are tracked using a Least Recently Used list.
319  *  - New L1s are allocated from the HEAD.
320  *  - Freed L1s are added to the TAIl.
321  *  - Recently accessed L1s (where an 'access' is some change to one of
322  *    the userland pmaps which owns this L1) are moved to the TAIL.
323  */
324 static TAILQ_HEAD(, l1_ttable) l1_lru_list;
325 /*
326  * A list of all L1 tables
327  */
328 static SLIST_HEAD(, l1_ttable) l1_list;
329 static struct mtx l1_lru_lock;
330
331 /*
332  * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots.
333  *
334  * This is normally 16MB worth L2 page descriptors for any given pmap.
335  * Reference counts are maintained for L2 descriptors so they can be
336  * freed when empty.
337  */
338 struct l2_dtable {
339         /* The number of L2 page descriptors allocated to this l2_dtable */
340         u_int l2_occupancy;
341
342         /* List of L2 page descriptors */
343         struct l2_bucket {
344                 pt_entry_t *l2b_kva;    /* KVA of L2 Descriptor Table */
345                 vm_paddr_t l2b_phys;    /* Physical address of same */
346                 u_short l2b_l1idx;      /* This L2 table's L1 index */
347                 u_short l2b_occupancy;  /* How many active descriptors */
348         } l2_bucket[L2_BUCKET_SIZE];
349 };
350
351 /* pmap_kenter_internal flags */
352 #define KENTER_CACHE    0x1
353 #define KENTER_USER     0x2
354
355 /*
356  * Given an L1 table index, calculate the corresponding l2_dtable index
357  * and bucket index within the l2_dtable.
358  */
359 #define L2_IDX(l1idx)           (((l1idx) >> L2_BUCKET_LOG2) & \
360                                  (L2_SIZE - 1))
361 #define L2_BUCKET(l1idx)        ((l1idx) & (L2_BUCKET_SIZE - 1))
362
363 /*
364  * Given a virtual address, this macro returns the
365  * virtual address required to drop into the next L2 bucket.
366  */
367 #define L2_NEXT_BUCKET(va)      (((va) & L1_S_FRAME) + L1_S_SIZE)
368
369 /*
370  * L2 allocation.
371  */
372 #define pmap_alloc_l2_dtable()          \
373                 (void*)uma_zalloc(l2table_zone, M_NOWAIT|M_USE_RESERVE)
374 #define pmap_free_l2_dtable(l2)         \
375                 uma_zfree(l2table_zone, l2)
376
377 /*
378  * We try to map the page tables write-through, if possible.  However, not
379  * all CPUs have a write-through cache mode, so on those we have to sync
380  * the cache when we frob page tables.
381  *
382  * We try to evaluate this at compile time, if possible.  However, it's
383  * not always possible to do that, hence this run-time var.
384  */
385 int     pmap_needs_pte_sync;
386
387 /*
388  * Macro to determine if a mapping might be resident in the
389  * instruction cache and/or TLB
390  */
391 #define PV_BEEN_EXECD(f)  (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC))
392
393 /*
394  * Macro to determine if a mapping might be resident in the
395  * data cache and/or TLB
396  */
397 #define PV_BEEN_REFD(f)   (((f) & PVF_REF) != 0)
398
399 #ifndef PMAP_SHPGPERPROC
400 #define PMAP_SHPGPERPROC 200
401 #endif
402
403 #define pmap_is_current(pm)     ((pm) == pmap_kernel() || \
404             curproc->p_vmspace->vm_map.pmap == (pm))
405 static uma_zone_t pvzone;
406 uma_zone_t l2zone;
407 static uma_zone_t l2table_zone;
408 static vm_offset_t pmap_kernel_l2dtable_kva;
409 static vm_offset_t pmap_kernel_l2ptp_kva;
410 static vm_paddr_t pmap_kernel_l2ptp_phys;
411 static struct vm_object pvzone_obj;
412 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
413
414 /*
415  * This list exists for the benefit of pmap_map_chunk().  It keeps track
416  * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
417  * find them as necessary.
418  *
419  * Note that the data on this list MUST remain valid after initarm() returns,
420  * as pmap_bootstrap() uses it to contruct L2 table metadata.
421  */
422 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
423
424 static void
425 pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
426 {
427         int i;
428
429         l1->l1_kva = l1pt;
430         l1->l1_domain_use_count = 0;
431         l1->l1_domain_first = 0;
432
433         for (i = 0; i < PMAP_DOMAINS; i++)
434                 l1->l1_domain_free[i] = i + 1;
435
436         /*
437          * Copy the kernel's L1 entries to each new L1.
438          */
439         if (l1pt != pmap_kernel()->pm_l1->l1_kva)
440                 memcpy(l1pt, pmap_kernel()->pm_l1->l1_kva, L1_TABLE_SIZE);
441
442         if ((l1->l1_physaddr = pmap_extract(pmap_kernel(), (vm_offset_t)l1pt)) == 0)
443                 panic("pmap_init_l1: can't get PA of L1 at %p", l1pt);
444         SLIST_INSERT_HEAD(&l1_list, l1, l1_link);
445         TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
446 }
447
448 static vm_offset_t
449 kernel_pt_lookup(vm_paddr_t pa)
450 {
451         struct pv_addr *pv;
452
453         SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
454                 if (pv->pv_pa == pa)
455                         return (pv->pv_va);
456         }
457         return (0);
458 }
459
460 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0
461 void
462 pmap_pte_init_generic(void)
463 {
464
465         pte_l1_s_cache_mode = L1_S_B|L1_S_C;
466         pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
467
468         pte_l2_l_cache_mode = L2_B|L2_C;
469         pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
470
471         pte_l2_s_cache_mode = L2_B|L2_C;
472         pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
473
474         /*
475          * If we have a write-through cache, set B and C.  If
476          * we have a write-back cache, then we assume setting
477          * only C will make those pages write-through.
478          */
479         if (cpufuncs.cf_dcache_wb_range == (void *) cpufunc_nullop) {
480                 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
481                 pte_l2_l_cache_mode_pt = L2_B|L2_C;
482                 pte_l2_s_cache_mode_pt = L2_B|L2_C;
483         } else {
484                 pte_l1_s_cache_mode_pt = L1_S_C;
485                 pte_l2_l_cache_mode_pt = L2_C;
486                 pte_l2_s_cache_mode_pt = L2_C;
487         }
488
489         pte_l2_s_prot_u = L2_S_PROT_U_generic;
490         pte_l2_s_prot_w = L2_S_PROT_W_generic;
491         pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
492
493         pte_l1_s_proto = L1_S_PROTO_generic;
494         pte_l1_c_proto = L1_C_PROTO_generic;
495         pte_l2_s_proto = L2_S_PROTO_generic;
496
497         pmap_copy_page_func = pmap_copy_page_generic;
498         pmap_zero_page_func = pmap_zero_page_generic;
499 }
500
501 #if defined(CPU_ARM8)
502 void
503 pmap_pte_init_arm8(void)
504 {
505
506         /*
507          * ARM8 is compatible with generic, but we need to use
508          * the page tables uncached.
509          */
510         pmap_pte_init_generic();
511
512         pte_l1_s_cache_mode_pt = 0;
513         pte_l2_l_cache_mode_pt = 0;
514         pte_l2_s_cache_mode_pt = 0;
515 }
516 #endif /* CPU_ARM8 */
517
518 #if defined(CPU_ARM9) && defined(ARM9_CACHE_WRITE_THROUGH)
519 void
520 pmap_pte_init_arm9(void)
521 {
522
523         /*
524          * ARM9 is compatible with generic, but we want to use
525          * write-through caching for now.
526          */
527         pmap_pte_init_generic();
528
529         pte_l1_s_cache_mode = L1_S_C;
530         pte_l2_l_cache_mode = L2_C;
531         pte_l2_s_cache_mode = L2_C;
532
533         pte_l1_s_cache_mode_pt = L1_S_C;
534         pte_l2_l_cache_mode_pt = L2_C;
535         pte_l2_s_cache_mode_pt = L2_C;
536 }
537 #endif /* CPU_ARM9 */
538 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
539
540 #if defined(CPU_ARM10)
541 void
542 pmap_pte_init_arm10(void)
543 {
544
545         /*
546          * ARM10 is compatible with generic, but we want to use
547          * write-through caching for now.
548          */
549         pmap_pte_init_generic();
550
551         pte_l1_s_cache_mode = L1_S_B | L1_S_C;
552         pte_l2_l_cache_mode = L2_B | L2_C;
553         pte_l2_s_cache_mode = L2_B | L2_C;
554
555         pte_l1_s_cache_mode_pt = L1_S_C;
556         pte_l2_l_cache_mode_pt = L2_C;
557         pte_l2_s_cache_mode_pt = L2_C;
558
559 }
560 #endif /* CPU_ARM10 */
561
562 #if  ARM_MMU_SA1 == 1
563 void
564 pmap_pte_init_sa1(void)
565 {
566
567         /*
568          * The StrongARM SA-1 cache does not have a write-through
569          * mode.  So, do the generic initialization, then reset
570          * the page table cache mode to B=1,C=1, and note that
571          * the PTEs need to be sync'd.
572          */
573         pmap_pte_init_generic();
574
575         pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
576         pte_l2_l_cache_mode_pt = L2_B|L2_C;
577         pte_l2_s_cache_mode_pt = L2_B|L2_C;
578
579         pmap_needs_pte_sync = 1;
580 }
581 #endif /* ARM_MMU_SA1 == 1*/
582
583 #if ARM_MMU_XSCALE == 1
584 #if (ARM_NMMUS > 1) || defined (CPU_XSCALE_CORE3)
585 static u_int xscale_use_minidata;
586 #endif
587
588 void
589 pmap_pte_init_xscale(void)
590 {
591         uint32_t auxctl;
592         int write_through = 0;
593
594         pte_l1_s_cache_mode = L1_S_B|L1_S_C|L1_S_XSCALE_P;
595         pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
596
597         pte_l2_l_cache_mode = L2_B|L2_C;
598         pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
599
600         pte_l2_s_cache_mode = L2_B|L2_C;
601         pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
602
603         pte_l1_s_cache_mode_pt = L1_S_C;
604         pte_l2_l_cache_mode_pt = L2_C;
605         pte_l2_s_cache_mode_pt = L2_C;
606 #ifdef XSCALE_CACHE_READ_WRITE_ALLOCATE
607         /*
608          * The XScale core has an enhanced mode where writes that
609          * miss the cache cause a cache line to be allocated.  This
610          * is significantly faster than the traditional, write-through
611          * behavior of this case.
612          */
613         pte_l1_s_cache_mode |= L1_S_XSCALE_TEX(TEX_XSCALE_X);
614         pte_l2_l_cache_mode |= L2_XSCALE_L_TEX(TEX_XSCALE_X);
615         pte_l2_s_cache_mode |= L2_XSCALE_T_TEX(TEX_XSCALE_X);
616 #endif /* XSCALE_CACHE_READ_WRITE_ALLOCATE */
617 #ifdef XSCALE_CACHE_WRITE_THROUGH
618         /*
619          * Some versions of the XScale core have various bugs in
620          * their cache units, the work-around for which is to run
621          * the cache in write-through mode.  Unfortunately, this
622          * has a major (negative) impact on performance.  So, we
623          * go ahead and run fast-and-loose, in the hopes that we
624          * don't line up the planets in a way that will trip the
625          * bugs.
626          *
627          * However, we give you the option to be slow-but-correct.
628          */
629         write_through = 1;
630 #elif defined(XSCALE_CACHE_WRITE_BACK)
631         /* force write back cache mode */
632         write_through = 0;
633 #elif defined(CPU_XSCALE_PXA2X0)
634         /*
635          * Intel PXA2[15]0 processors are known to have a bug in
636          * write-back cache on revision 4 and earlier (stepping
637          * A[01] and B[012]).  Fixed for C0 and later.
638          */
639         {
640                 uint32_t id, type;
641
642                 id = cpufunc_id();
643                 type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
644
645                 if (type == CPU_ID_PXA250 || type == CPU_ID_PXA210) {
646                         if ((id & CPU_ID_REVISION_MASK) < 5) {
647                                 /* write through for stepping A0-1 and B0-2 */
648                                 write_through = 1;
649                         }
650                 }
651         }
652 #endif /* XSCALE_CACHE_WRITE_THROUGH */
653
654         if (write_through) {
655                 pte_l1_s_cache_mode = L1_S_C;
656                 pte_l2_l_cache_mode = L2_C;
657                 pte_l2_s_cache_mode = L2_C;
658         }
659
660 #if (ARM_NMMUS > 1)
661         xscale_use_minidata = 1;
662 #endif
663
664         pte_l2_s_prot_u = L2_S_PROT_U_xscale;
665         pte_l2_s_prot_w = L2_S_PROT_W_xscale;
666         pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
667
668         pte_l1_s_proto = L1_S_PROTO_xscale;
669         pte_l1_c_proto = L1_C_PROTO_xscale;
670         pte_l2_s_proto = L2_S_PROTO_xscale;
671
672 #ifdef CPU_XSCALE_CORE3
673         pmap_copy_page_func = pmap_copy_page_generic;
674         pmap_zero_page_func = pmap_zero_page_generic;
675         xscale_use_minidata = 0;
676         /* Make sure it is L2-cachable */
677         pte_l1_s_cache_mode |= L1_S_XSCALE_TEX(TEX_XSCALE_T);
678         pte_l1_s_cache_mode_pt = pte_l1_s_cache_mode &~ L1_S_XSCALE_P;
679         pte_l2_l_cache_mode |= L2_XSCALE_L_TEX(TEX_XSCALE_T) ;
680         pte_l2_l_cache_mode_pt = pte_l1_s_cache_mode;
681         pte_l2_s_cache_mode |= L2_XSCALE_T_TEX(TEX_XSCALE_T);
682         pte_l2_s_cache_mode_pt = pte_l2_s_cache_mode;
683
684 #else
685         pmap_copy_page_func = pmap_copy_page_xscale;
686         pmap_zero_page_func = pmap_zero_page_xscale;
687 #endif
688
689         /*
690          * Disable ECC protection of page table access, for now.
691          */
692         __asm __volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
693         auxctl &= ~XSCALE_AUXCTL_P;
694         __asm __volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
695 }
696
697 /*
698  * xscale_setup_minidata:
699  *
700  *      Set up the mini-data cache clean area.  We require the
701  *      caller to allocate the right amount of physically and
702  *      virtually contiguous space.
703  */
704 extern vm_offset_t xscale_minidata_clean_addr;
705 extern vm_size_t xscale_minidata_clean_size; /* already initialized */
706 void
707 xscale_setup_minidata(vm_offset_t l1pt, vm_offset_t va, vm_paddr_t pa)
708 {
709         pd_entry_t *pde = (pd_entry_t *) l1pt;
710         pt_entry_t *pte;
711         vm_size_t size;
712         uint32_t auxctl;
713
714         xscale_minidata_clean_addr = va;
715
716         /* Round it to page size. */
717         size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
718
719         for (; size != 0;
720              va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
721                 pte = (pt_entry_t *) kernel_pt_lookup(
722                     pde[L1_IDX(va)] & L1_C_ADDR_MASK);
723                 if (pte == NULL)
724                         panic("xscale_setup_minidata: can't find L2 table for "
725                             "VA 0x%08x", (u_int32_t) va);
726                 pte[l2pte_index(va)] =
727                     L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
728                     L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);
729         }
730
731         /*
732          * Configure the mini-data cache for write-back with
733          * read/write-allocate.
734          *
735          * NOTE: In order to reconfigure the mini-data cache, we must
736          * make sure it contains no valid data!  In order to do that,
737          * we must issue a global data cache invalidate command!
738          *
739          * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
740          * THIS IS VERY IMPORTANT!
741          */
742
743         /* Invalidate data and mini-data. */
744         __asm __volatile("mcr p15, 0, %0, c7, c6, 0" : : "r" (0));
745         __asm __volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
746         auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
747         __asm __volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
748 }
749 #endif
750
751 /*
752  * Allocate an L1 translation table for the specified pmap.
753  * This is called at pmap creation time.
754  */
755 static void
756 pmap_alloc_l1(pmap_t pm)
757 {
758         struct l1_ttable *l1;
759         u_int8_t domain;
760
761         /*
762          * Remove the L1 at the head of the LRU list
763          */
764         mtx_lock(&l1_lru_lock);
765         l1 = TAILQ_FIRST(&l1_lru_list);
766         TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
767
768         /*
769          * Pick the first available domain number, and update
770          * the link to the next number.
771          */
772         domain = l1->l1_domain_first;
773         l1->l1_domain_first = l1->l1_domain_free[domain];
774
775         /*
776          * If there are still free domain numbers in this L1,
777          * put it back on the TAIL of the LRU list.
778          */
779         if (++l1->l1_domain_use_count < PMAP_DOMAINS)
780                 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
781
782         mtx_unlock(&l1_lru_lock);
783
784         /*
785          * Fix up the relevant bits in the pmap structure
786          */
787         pm->pm_l1 = l1;
788         pm->pm_domain = domain + 1;
789 }
790
791 /*
792  * Free an L1 translation table.
793  * This is called at pmap destruction time.
794  */
795 static void
796 pmap_free_l1(pmap_t pm)
797 {
798         struct l1_ttable *l1 = pm->pm_l1;
799
800         mtx_lock(&l1_lru_lock);
801
802         /*
803          * If this L1 is currently on the LRU list, remove it.
804          */
805         if (l1->l1_domain_use_count < PMAP_DOMAINS)
806                 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
807
808         /*
809          * Free up the domain number which was allocated to the pmap
810          */
811         l1->l1_domain_free[pm->pm_domain - 1] = l1->l1_domain_first;
812         l1->l1_domain_first = pm->pm_domain - 1;
813         l1->l1_domain_use_count--;
814
815         /*
816          * The L1 now must have at least 1 free domain, so add
817          * it back to the LRU list. If the use count is zero,
818          * put it at the head of the list, otherwise it goes
819          * to the tail.
820          */
821         if (l1->l1_domain_use_count == 0) {
822                 TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru);
823         }       else
824                 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
825
826         mtx_unlock(&l1_lru_lock);
827 }
828
829 static PMAP_INLINE void
830 pmap_use_l1(pmap_t pm)
831 {
832         struct l1_ttable *l1;
833
834         /*
835          * Do nothing if we're in interrupt context.
836          * Access to an L1 by the kernel pmap must not affect
837          * the LRU list.
838          */
839         if (pm == pmap_kernel())
840                 return;
841
842         l1 = pm->pm_l1;
843
844         /*
845          * If the L1 is not currently on the LRU list, just return
846          */
847         if (l1->l1_domain_use_count == PMAP_DOMAINS)
848                 return;
849
850         mtx_lock(&l1_lru_lock);
851
852         /*
853          * Check the use count again, now that we've acquired the lock
854          */
855         if (l1->l1_domain_use_count == PMAP_DOMAINS) {
856                 mtx_unlock(&l1_lru_lock);
857                 return;
858         }
859
860         /*
861          * Move the L1 to the back of the LRU list
862          */
863         TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
864         TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
865
866         mtx_unlock(&l1_lru_lock);
867 }
868
869
870 /*
871  * Returns a pointer to the L2 bucket associated with the specified pmap
872  * and VA, or NULL if no L2 bucket exists for the address.
873  */
874 static PMAP_INLINE struct l2_bucket *
875 pmap_get_l2_bucket(pmap_t pm, vm_offset_t va)
876 {
877         struct l2_dtable *l2;
878         struct l2_bucket *l2b;
879         u_short l1idx;
880
881         l1idx = L1_IDX(va);
882
883         if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL ||
884             (l2b = &l2->l2_bucket[L2_BUCKET(l1idx)])->l2b_kva == NULL)
885                 return (NULL);
886
887         return (l2b);
888 }
889
890 /*
891  * Returns a pointer to the L2 bucket associated with the specified pmap
892  * and VA.
893  *
894  * If no L2 bucket exists, perform the necessary allocations to put an L2
895  * bucket/page table in place.
896  *
897  * Note that if a new L2 bucket/page was allocated, the caller *must*
898  * increment the bucket occupancy counter appropriately *before* 
899  * releasing the pmap's lock to ensure no other thread or cpu deallocates
900  * the bucket/page in the meantime.
901  */
902 static struct l2_bucket *
903 pmap_alloc_l2_bucket(pmap_t pm, vm_offset_t va)
904 {
905         struct l2_dtable *l2;
906         struct l2_bucket *l2b;
907         u_short l1idx;
908
909         l1idx = L1_IDX(va);
910
911         PMAP_ASSERT_LOCKED(pm);
912         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
913         if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
914                 /*
915                  * No mapping at this address, as there is
916                  * no entry in the L1 table.
917                  * Need to allocate a new l2_dtable.
918                  */
919 again_l2table:
920                 PMAP_UNLOCK(pm);
921                 vm_page_unlock_queues();
922                 if ((l2 = pmap_alloc_l2_dtable()) == NULL) {
923                         vm_page_lock_queues();
924                         PMAP_LOCK(pm);
925                         return (NULL);
926                 }
927                 vm_page_lock_queues();
928                 PMAP_LOCK(pm);
929                 if (pm->pm_l2[L2_IDX(l1idx)] != NULL) {
930                         PMAP_UNLOCK(pm);
931                         vm_page_unlock_queues();
932                         uma_zfree(l2table_zone, l2);
933                         vm_page_lock_queues();
934                         PMAP_LOCK(pm);
935                         l2 = pm->pm_l2[L2_IDX(l1idx)];
936                         if (l2 == NULL)
937                                 goto again_l2table;
938                         /*
939                          * Someone already allocated the l2_dtable while
940                          * we were doing the same.
941                          */
942                 } else {
943                         bzero(l2, sizeof(*l2));
944                         /*
945                          * Link it into the parent pmap
946                          */
947                         pm->pm_l2[L2_IDX(l1idx)] = l2;
948                 }
949         } 
950
951         l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
952
953         /*
954          * Fetch pointer to the L2 page table associated with the address.
955          */
956         if (l2b->l2b_kva == NULL) {
957                 pt_entry_t *ptep;
958
959                 /*
960                  * No L2 page table has been allocated. Chances are, this
961                  * is because we just allocated the l2_dtable, above.
962                  */
963 again_ptep:
964                 PMAP_UNLOCK(pm);
965                 vm_page_unlock_queues();
966                 ptep = (void*)uma_zalloc(l2zone, M_NOWAIT|M_USE_RESERVE);
967                 vm_page_lock_queues();
968                 PMAP_LOCK(pm);
969                 if (l2b->l2b_kva != 0) {
970                         /* We lost the race. */
971                         PMAP_UNLOCK(pm);
972                         vm_page_unlock_queues();
973                         uma_zfree(l2zone, ptep);
974                         vm_page_lock_queues();
975                         PMAP_LOCK(pm);
976                         if (l2b->l2b_kva == 0)
977                                 goto again_ptep;
978                         return (l2b);
979                 }
980                 l2b->l2b_phys = vtophys(ptep);
981                 if (ptep == NULL) {
982                         /*
983                          * Oops, no more L2 page tables available at this
984                          * time. We may need to deallocate the l2_dtable
985                          * if we allocated a new one above.
986                          */
987                         if (l2->l2_occupancy == 0) {
988                                 pm->pm_l2[L2_IDX(l1idx)] = NULL;
989                                 pmap_free_l2_dtable(l2);
990                         }
991                         return (NULL);
992                 }
993
994                 l2->l2_occupancy++;
995                 l2b->l2b_kva = ptep;
996                 l2b->l2b_l1idx = l1idx;
997         }
998
999         return (l2b);
1000 }
1001
1002 static PMAP_INLINE void
1003 #ifndef PMAP_INCLUDE_PTE_SYNC
1004 pmap_free_l2_ptp(pt_entry_t *l2)
1005 #else
1006 pmap_free_l2_ptp(boolean_t need_sync, pt_entry_t *l2)
1007 #endif
1008 {
1009 #ifdef PMAP_INCLUDE_PTE_SYNC
1010         /*
1011          * Note: With a write-back cache, we may need to sync this
1012          * L2 table before re-using it.
1013          * This is because it may have belonged to a non-current
1014          * pmap, in which case the cache syncs would have been
1015          * skipped when the pages were being unmapped. If the
1016          * L2 table were then to be immediately re-allocated to
1017          * the *current* pmap, it may well contain stale mappings
1018          * which have not yet been cleared by a cache write-back
1019          * and so would still be visible to the mmu.
1020          */
1021         if (need_sync)
1022                 PTE_SYNC_RANGE(l2, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1023 #endif
1024         uma_zfree(l2zone, l2);
1025 }
1026 /*
1027  * One or more mappings in the specified L2 descriptor table have just been
1028  * invalidated.
1029  *
1030  * Garbage collect the metadata and descriptor table itself if necessary.
1031  *
1032  * The pmap lock must be acquired when this is called (not necessary
1033  * for the kernel pmap).
1034  */
1035 static void
1036 pmap_free_l2_bucket(pmap_t pm, struct l2_bucket *l2b, u_int count)
1037 {
1038         struct l2_dtable *l2;
1039         pd_entry_t *pl1pd, l1pd;
1040         pt_entry_t *ptep;
1041         u_short l1idx;
1042
1043
1044         /*
1045          * Update the bucket's reference count according to how many
1046          * PTEs the caller has just invalidated.
1047          */
1048         l2b->l2b_occupancy -= count;
1049
1050         /*
1051          * Note:
1052          *
1053          * Level 2 page tables allocated to the kernel pmap are never freed
1054          * as that would require checking all Level 1 page tables and
1055          * removing any references to the Level 2 page table. See also the
1056          * comment elsewhere about never freeing bootstrap L2 descriptors.
1057          *
1058          * We make do with just invalidating the mapping in the L2 table.
1059          *
1060          * This isn't really a big deal in practice and, in fact, leads
1061          * to a performance win over time as we don't need to continually
1062          * alloc/free.
1063          */
1064         if (l2b->l2b_occupancy > 0 || pm == pmap_kernel())
1065                 return;
1066
1067         /*
1068          * There are no more valid mappings in this level 2 page table.
1069          * Go ahead and NULL-out the pointer in the bucket, then
1070          * free the page table.
1071          */
1072         l1idx = l2b->l2b_l1idx;
1073         ptep = l2b->l2b_kva;
1074         l2b->l2b_kva = NULL;
1075
1076         pl1pd = &pm->pm_l1->l1_kva[l1idx];
1077
1078         /*
1079          * If the L1 slot matches the pmap's domain
1080          * number, then invalidate it.
1081          */
1082         l1pd = *pl1pd & (L1_TYPE_MASK | L1_C_DOM_MASK);
1083         if (l1pd == (L1_C_DOM(pm->pm_domain) | L1_TYPE_C)) {
1084                 *pl1pd = 0;
1085                 PTE_SYNC(pl1pd);
1086         }
1087
1088         /*
1089          * Release the L2 descriptor table back to the pool cache.
1090          */
1091 #ifndef PMAP_INCLUDE_PTE_SYNC
1092         pmap_free_l2_ptp(ptep);
1093 #else
1094         pmap_free_l2_ptp(!pmap_is_current(pm), ptep);
1095 #endif
1096
1097         /*
1098          * Update the reference count in the associated l2_dtable
1099          */
1100         l2 = pm->pm_l2[L2_IDX(l1idx)];
1101         if (--l2->l2_occupancy > 0)
1102                 return;
1103
1104         /*
1105          * There are no more valid mappings in any of the Level 1
1106          * slots managed by this l2_dtable. Go ahead and NULL-out
1107          * the pointer in the parent pmap and free the l2_dtable.
1108          */
1109         pm->pm_l2[L2_IDX(l1idx)] = NULL;
1110         pmap_free_l2_dtable(l2);
1111 }
1112
1113 /*
1114  * Pool cache constructors for L2 descriptor tables, metadata and pmap
1115  * structures.
1116  */
1117 static int
1118 pmap_l2ptp_ctor(void *mem, int size, void *arg, int flags)
1119 {
1120 #ifndef PMAP_INCLUDE_PTE_SYNC
1121         struct l2_bucket *l2b;
1122         pt_entry_t *ptep, pte;
1123 #ifdef ARM_USE_SMALL_ALLOC
1124         pd_entry_t *pde;
1125 #endif
1126         vm_offset_t va = (vm_offset_t)mem & ~PAGE_MASK;
1127
1128         /*
1129          * The mappings for these page tables were initially made using
1130          * pmap_kenter() by the pool subsystem. Therefore, the cache-
1131          * mode will not be right for page table mappings. To avoid
1132          * polluting the pmap_kenter() code with a special case for
1133          * page tables, we simply fix up the cache-mode here if it's not
1134          * correct.
1135          */
1136 #ifdef ARM_USE_SMALL_ALLOC
1137         pde = &kernel_pmap->pm_l1->l1_kva[L1_IDX(va)];
1138         if (!l1pte_section_p(*pde)) {
1139 #endif
1140                 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
1141                 ptep = &l2b->l2b_kva[l2pte_index(va)];
1142                 pte = *ptep;
1143                 
1144                 if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
1145                         /*
1146                          * Page tables must have the cache-mode set to 
1147                          * Write-Thru.
1148                          */
1149                         *ptep = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
1150                         PTE_SYNC(ptep);
1151                         cpu_tlb_flushD_SE(va);
1152                         cpu_cpwait();
1153                 }
1154 #ifdef ARM_USE_SMALL_ALLOC
1155         }
1156 #endif
1157 #endif
1158         memset(mem, 0, L2_TABLE_SIZE_REAL);
1159         PTE_SYNC_RANGE(mem, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1160         return (0);
1161 }
1162
1163 /*
1164  * A bunch of routines to conditionally flush the caches/TLB depending
1165  * on whether the specified pmap actually needs to be flushed at any
1166  * given time.
1167  */
1168 static PMAP_INLINE void
1169 pmap_tlb_flushID_SE(pmap_t pm, vm_offset_t va)
1170 {
1171
1172         if (pmap_is_current(pm))
1173                 cpu_tlb_flushID_SE(va);
1174 }
1175
1176 static PMAP_INLINE void
1177 pmap_tlb_flushD_SE(pmap_t pm, vm_offset_t va)
1178 {
1179
1180         if (pmap_is_current(pm))
1181                 cpu_tlb_flushD_SE(va);
1182 }
1183
1184 static PMAP_INLINE void
1185 pmap_tlb_flushID(pmap_t pm)
1186 {
1187
1188         if (pmap_is_current(pm))
1189                 cpu_tlb_flushID();
1190 }
1191 static PMAP_INLINE void
1192 pmap_tlb_flushD(pmap_t pm)
1193 {
1194
1195         if (pmap_is_current(pm))
1196                 cpu_tlb_flushD();
1197 }
1198
1199 static PMAP_INLINE void
1200 pmap_l2cache_wbinv_range(pmap_t pm, vm_offset_t va, vm_size_t len)
1201 {
1202         vm_size_t rest;
1203         pd_entry_t *pde;
1204         pt_entry_t *ptep;
1205
1206         rest = MIN(PAGE_SIZE - (va & PAGE_MASK), len);
1207
1208         while (len > 0) {
1209                 CTR4(KTR_PMAP, "pmap_l2cache_wbinv_range: pmap %p is_kernel %d "
1210                     "va 0x%08x len 0x%x ", pm, pm == pmap_kernel(), va, rest);
1211                 if (pmap_get_pde_pte(pm, va, &pde, &ptep) && l2pte_valid(*ptep))
1212                     cpu_l2cache_wb_range(va, rest);
1213
1214                 len -= rest;
1215                 va += rest;
1216
1217                 rest = MIN(PAGE_SIZE, len);
1218         }
1219 }
1220
1221 static PMAP_INLINE void
1222 pmap_idcache_wbinv_range(pmap_t pm, vm_offset_t va, vm_size_t len)
1223 {
1224
1225         if (pmap_is_current(pm)) {
1226                 cpu_idcache_wbinv_range(va, len);
1227                 pmap_l2cache_wbinv_range(pm, va, len);
1228         }
1229 }
1230
1231 static PMAP_INLINE void
1232 pmap_l2cache_wb_range(pmap_t pm, vm_offset_t va, vm_size_t len)
1233 {
1234         vm_size_t rest;
1235         pd_entry_t *pde;
1236         pt_entry_t *ptep;
1237
1238         rest = MIN(PAGE_SIZE - (va & PAGE_MASK), len);
1239
1240         while (len > 0) {
1241                 CTR4(KTR_PMAP, "pmap_l2cache_wb_range: pmap %p is_kernel %d "
1242                     "va 0x%08x len 0x%x ", pm, pm == pmap_kernel(), va, rest);
1243                 if (pmap_get_pde_pte(pm, va, &pde, &ptep) && l2pte_valid(*ptep))
1244                     cpu_l2cache_wb_range(va, rest);
1245
1246                 len -= rest;
1247                 va += rest;
1248
1249                 rest = MIN(PAGE_SIZE, len);
1250         }
1251 }
1252
1253 static PMAP_INLINE void
1254 pmap_l2cache_inv_range(pmap_t pm, vm_offset_t va, vm_size_t len)
1255 {
1256         vm_size_t rest;
1257         pd_entry_t *pde;
1258         pt_entry_t *ptep;
1259
1260         rest = MIN(PAGE_SIZE - (va & PAGE_MASK), len);
1261
1262         while (len > 0) {
1263                 CTR4(KTR_PMAP, "pmap_l2cache_wb_range: pmap %p is_kernel %d "
1264                     "va 0x%08x len 0x%x ", pm, pm == pmap_kernel(), va, rest);
1265                 if (pmap_get_pde_pte(pm, va, &pde, &ptep) && l2pte_valid(*ptep)) 
1266                     cpu_l2cache_inv_range(va, rest);
1267
1268                 len -= rest;
1269                 va += rest;
1270
1271                 rest = MIN(PAGE_SIZE, len);
1272         }
1273 }
1274
1275 static PMAP_INLINE void
1276 pmap_dcache_wb_range(pmap_t pm, vm_offset_t va, vm_size_t len, boolean_t do_inv,
1277     boolean_t rd_only)
1278 {
1279         CTR4(KTR_PMAP, "pmap_dcache_wb_range: pmap %p is_kernel %d va 0x%08x "
1280             "len 0x%x ", pm, pm == pmap_kernel(), va, len);
1281         CTR2(KTR_PMAP, " do_inv %d rd_only %d", do_inv, rd_only);
1282
1283         if (pmap_is_current(pm)) {
1284                 if (do_inv) {
1285                         if (rd_only) {
1286                                 cpu_dcache_inv_range(va, len);
1287                                 pmap_l2cache_inv_range(pm, va, len);
1288                         }
1289                         else {
1290                                 cpu_dcache_wbinv_range(va, len);
1291                                 pmap_l2cache_wbinv_range(pm, va, len);
1292                         }
1293                 } else
1294                 if (!rd_only) {
1295                         cpu_dcache_wb_range(va, len);
1296                         pmap_l2cache_wb_range(pm, va, len);
1297                 }
1298         }
1299 }
1300
1301 static PMAP_INLINE void
1302 pmap_idcache_wbinv_all(pmap_t pm)
1303 {
1304
1305         if (pmap_is_current(pm)) {
1306                 cpu_idcache_wbinv_all();
1307                 cpu_l2cache_wbinv_all();
1308         }
1309 }
1310
1311 static PMAP_INLINE void
1312 pmap_dcache_wbinv_all(pmap_t pm)
1313 {
1314
1315         if (pmap_is_current(pm)) {
1316                 cpu_dcache_wbinv_all();
1317                 cpu_l2cache_wbinv_all();
1318         }
1319 }
1320
1321 /*
1322  * PTE_SYNC_CURRENT:
1323  *
1324  *     Make sure the pte is written out to RAM.
1325  *     We need to do this for one of two cases:
1326  *       - We're dealing with the kernel pmap
1327  *       - There is no pmap active in the cache/tlb.
1328  *       - The specified pmap is 'active' in the cache/tlb.
1329  */
1330 #ifdef PMAP_INCLUDE_PTE_SYNC
1331 #define PTE_SYNC_CURRENT(pm, ptep)      \
1332 do {                                    \
1333         if (PMAP_NEEDS_PTE_SYNC &&      \
1334             pmap_is_current(pm))        \
1335                 PTE_SYNC(ptep);         \
1336 } while (/*CONSTCOND*/0)
1337 #else
1338 #define PTE_SYNC_CURRENT(pm, ptep)      /* nothing */
1339 #endif
1340
1341 /*
1342  * cacheable == -1 means we must make the entry uncacheable, 1 means
1343  * cacheable;
1344  */
1345 static __inline void
1346 pmap_set_cache_entry(pv_entry_t pv, pmap_t pm, vm_offset_t va, int cacheable)
1347 {
1348         struct l2_bucket *l2b;
1349         pt_entry_t *ptep, pte;
1350
1351         l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
1352         ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
1353
1354         if (cacheable == 1) {
1355                 pte = (*ptep & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode;
1356                 if (l2pte_valid(pte)) {
1357                         if (PV_BEEN_EXECD(pv->pv_flags)) {
1358                                 pmap_tlb_flushID_SE(pv->pv_pmap, pv->pv_va);
1359                         } else if (PV_BEEN_REFD(pv->pv_flags)) {
1360                                 pmap_tlb_flushD_SE(pv->pv_pmap, pv->pv_va);
1361                         }
1362                 }
1363         } else {
1364                 pte = *ptep &~ L2_S_CACHE_MASK;
1365                 if ((va != pv->pv_va || pm != pv->pv_pmap) &&
1366                             l2pte_valid(pte)) {
1367                         if (PV_BEEN_EXECD(pv->pv_flags)) {
1368                                 pmap_idcache_wbinv_range(pv->pv_pmap,
1369                                             pv->pv_va, PAGE_SIZE);
1370                                 pmap_tlb_flushID_SE(pv->pv_pmap, pv->pv_va);
1371                         } else if (PV_BEEN_REFD(pv->pv_flags)) {
1372                                 pmap_dcache_wb_range(pv->pv_pmap,
1373                                             pv->pv_va, PAGE_SIZE, TRUE,
1374                                             (pv->pv_flags & PVF_WRITE) == 0);
1375                                 pmap_tlb_flushD_SE(pv->pv_pmap,
1376                                             pv->pv_va);
1377                         }
1378                 }
1379         }
1380         *ptep = pte;
1381         PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1382 }
1383
1384 static void
1385 pmap_fix_cache(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1386 {
1387         int pmwc = 0;
1388         int writable = 0, kwritable = 0, uwritable = 0;
1389         int entries = 0, kentries = 0, uentries = 0;
1390         struct pv_entry *pv;
1391
1392         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1393
1394         /* the cache gets written back/invalidated on context switch.
1395          * therefore, if a user page shares an entry in the same page or
1396          * with the kernel map and at least one is writable, then the
1397          * cache entry must be set write-through.
1398          */
1399
1400         TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1401                         /* generate a count of the pv_entry uses */
1402                 if (pv->pv_flags & PVF_WRITE) {
1403                         if (pv->pv_pmap == pmap_kernel())
1404                                 kwritable++;
1405                         else if (pv->pv_pmap == pm)
1406                                 uwritable++;
1407                         writable++;
1408                 }
1409                 if (pv->pv_pmap == pmap_kernel())
1410                         kentries++;
1411                 else {
1412                         if (pv->pv_pmap == pm)
1413                                 uentries++;
1414                         entries++;
1415                 }
1416         }
1417                 /*
1418                  * check if the user duplicate mapping has
1419                  * been removed.
1420                  */
1421         if ((pm != pmap_kernel()) && (((uentries > 1) && uwritable) ||
1422             (uwritable > 1)))
1423                         pmwc = 1;
1424
1425         TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1426                 /* check for user uncachable conditions - order is important */
1427                 if (pm != pmap_kernel() &&
1428                     (pv->pv_pmap == pm || pv->pv_pmap == pmap_kernel())) {
1429
1430                         if ((uentries > 1 && uwritable) || uwritable > 1) {
1431
1432                                 /* user duplicate mapping */
1433                                 if (pv->pv_pmap != pmap_kernel())
1434                                         pv->pv_flags |= PVF_MWC;
1435
1436                                 if (!(pv->pv_flags & PVF_NC)) {
1437                                         pv->pv_flags |= PVF_NC;
1438                                         pmap_set_cache_entry(pv, pm, va, -1);
1439                                 }
1440                                 continue;
1441                         } else  /* no longer a duplicate user */
1442                                 pv->pv_flags &= ~PVF_MWC;
1443                 }
1444
1445                 /*
1446                  * check for kernel uncachable conditions
1447                  * kernel writable or kernel readable with writable user entry
1448                  */
1449                 if ((kwritable && entries) ||
1450                     ((kwritable != writable) && kentries &&
1451                      (pv->pv_pmap == pmap_kernel() ||
1452                       (pv->pv_flags & PVF_WRITE) ||
1453                       (pv->pv_flags & PVF_MWC)))) {
1454
1455                         if (!(pv->pv_flags & PVF_NC)) {
1456                                 pv->pv_flags |= PVF_NC;
1457                                 pmap_set_cache_entry(pv, pm, va, -1);
1458                         }
1459                         continue;
1460                 }
1461
1462                         /* kernel and user are cachable */
1463                 if ((pm == pmap_kernel()) && !(pv->pv_flags & PVF_MWC) &&
1464                     (pv->pv_flags & PVF_NC)) {
1465
1466                         pv->pv_flags &= ~PVF_NC;
1467                         pmap_set_cache_entry(pv, pm, va, 1);
1468                         continue;
1469                 }
1470                         /* user is no longer sharable and writable */
1471                 if (pm != pmap_kernel() && (pv->pv_pmap == pm) &&
1472                     !pmwc && (pv->pv_flags & PVF_NC)) {
1473
1474                         pv->pv_flags &= ~(PVF_NC | PVF_MWC);
1475                         pmap_set_cache_entry(pv, pm, va, 1);
1476                 }
1477         }
1478
1479         if ((kwritable == 0) && (writable == 0)) {
1480                 pg->md.pvh_attrs &= ~PVF_MOD;
1481                 vm_page_flag_clear(pg, PG_WRITEABLE);
1482                 return;
1483         }
1484 }
1485
1486 /*
1487  * Modify pte bits for all ptes corresponding to the given physical address.
1488  * We use `maskbits' rather than `clearbits' because we're always passing
1489  * constants and the latter would require an extra inversion at run-time.
1490  */
1491 static int 
1492 pmap_clearbit(struct vm_page *pg, u_int maskbits)
1493 {
1494         struct l2_bucket *l2b;
1495         struct pv_entry *pv;
1496         pt_entry_t *ptep, npte, opte;
1497         pmap_t pm;
1498         vm_offset_t va;
1499         u_int oflags;
1500         int count = 0;
1501
1502         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1503
1504         if (maskbits & PVF_WRITE)
1505                 maskbits |= PVF_MOD;
1506         /*
1507          * Clear saved attributes (modify, reference)
1508          */
1509         pg->md.pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF));
1510
1511         if (TAILQ_EMPTY(&pg->md.pv_list)) {
1512                 return (0);
1513         }
1514
1515         /*
1516          * Loop over all current mappings setting/clearing as appropos
1517          */
1518         TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1519                 va = pv->pv_va;
1520                 pm = pv->pv_pmap;
1521                 oflags = pv->pv_flags;
1522
1523                 if (!(oflags & maskbits)) {
1524                         if ((maskbits & PVF_WRITE) && (pv->pv_flags & PVF_NC)) {
1525                                 /* It is safe to re-enable cacheing here. */
1526                                 PMAP_LOCK(pm);
1527                                 l2b = pmap_get_l2_bucket(pm, va);
1528                                 ptep = &l2b->l2b_kva[l2pte_index(va)];
1529                                 *ptep |= pte_l2_s_cache_mode;
1530                                 PTE_SYNC(ptep);
1531                                 PMAP_UNLOCK(pm);
1532                                 pv->pv_flags &= ~(PVF_NC | PVF_MWC);
1533                                 
1534                         }
1535                         continue;
1536                 }
1537                 pv->pv_flags &= ~maskbits;
1538
1539                 PMAP_LOCK(pm);
1540
1541                 l2b = pmap_get_l2_bucket(pm, va);
1542
1543                 ptep = &l2b->l2b_kva[l2pte_index(va)];
1544                 npte = opte = *ptep;
1545
1546                 if (maskbits & (PVF_WRITE|PVF_MOD)) {
1547                         if ((pv->pv_flags & PVF_NC)) {
1548                                 /* 
1549                                  * Entry is not cacheable:
1550                                  *
1551                                  * Don't turn caching on again if this is a 
1552                                  * modified emulation. This would be
1553                                  * inconsitent with the settings created by
1554                                  * pmap_fix_cache(). Otherwise, it's safe
1555                                  * to re-enable cacheing.
1556                                  *
1557                                  * There's no need to call pmap_fix_cache()
1558                                  * here: all pages are losing their write
1559                                  * permission.
1560                                  */
1561                                 if (maskbits & PVF_WRITE) {
1562                                         npte |= pte_l2_s_cache_mode;
1563                                         pv->pv_flags &= ~(PVF_NC | PVF_MWC);
1564                                 }
1565                         } else
1566                         if (opte & L2_S_PROT_W) {
1567                                 vm_page_dirty(pg);
1568                                 /* 
1569                                  * Entry is writable/cacheable: check if pmap
1570                                  * is current if it is flush it, otherwise it
1571                                  * won't be in the cache
1572                                  */
1573                                 if (PV_BEEN_EXECD(oflags))
1574                                         pmap_idcache_wbinv_range(pm, pv->pv_va,
1575                                             PAGE_SIZE);
1576                                 else
1577                                 if (PV_BEEN_REFD(oflags))
1578                                         pmap_dcache_wb_range(pm, pv->pv_va,
1579                                             PAGE_SIZE,
1580                                             (maskbits & PVF_REF) ? TRUE : FALSE,
1581                                             FALSE);
1582                         }
1583
1584                         /* make the pte read only */
1585                         npte &= ~L2_S_PROT_W;
1586                 }
1587
1588                 if (maskbits & PVF_REF) {
1589                         if ((pv->pv_flags & PVF_NC) == 0 &&
1590                             (maskbits & (PVF_WRITE|PVF_MOD)) == 0) {
1591                                 /*
1592                                  * Check npte here; we may have already
1593                                  * done the wbinv above, and the validity
1594                                  * of the PTE is the same for opte and
1595                                  * npte.
1596                                  */
1597                                 if (npte & L2_S_PROT_W) {
1598                                         if (PV_BEEN_EXECD(oflags))
1599                                                 pmap_idcache_wbinv_range(pm,
1600                                                     pv->pv_va, PAGE_SIZE);
1601                                         else
1602                                         if (PV_BEEN_REFD(oflags))
1603                                                 pmap_dcache_wb_range(pm,
1604                                                     pv->pv_va, PAGE_SIZE,
1605                                                     TRUE, FALSE);
1606                                 } else
1607                                 if ((npte & L2_TYPE_MASK) != L2_TYPE_INV) {
1608                                         /* XXXJRT need idcache_inv_range */
1609                                         if (PV_BEEN_EXECD(oflags))
1610                                                 pmap_idcache_wbinv_range(pm,
1611                                                     pv->pv_va, PAGE_SIZE);
1612                                         else
1613                                         if (PV_BEEN_REFD(oflags))
1614                                                 pmap_dcache_wb_range(pm,
1615                                                     pv->pv_va, PAGE_SIZE,
1616                                                     TRUE, TRUE);
1617                                 }
1618                         }
1619
1620                         /*
1621                          * Make the PTE invalid so that we will take a
1622                          * page fault the next time the mapping is
1623                          * referenced.
1624                          */
1625                         npte &= ~L2_TYPE_MASK;
1626                         npte |= L2_TYPE_INV;
1627                 }
1628
1629                 if (npte != opte) {
1630                         count++;
1631                         *ptep = npte;
1632                         PTE_SYNC(ptep);
1633                         /* Flush the TLB entry if a current pmap. */
1634                         if (PV_BEEN_EXECD(oflags))
1635                                 pmap_tlb_flushID_SE(pm, pv->pv_va);
1636                         else
1637                         if (PV_BEEN_REFD(oflags))
1638                                 pmap_tlb_flushD_SE(pm, pv->pv_va);
1639                 }
1640
1641                 PMAP_UNLOCK(pm);
1642
1643         }
1644
1645         if (maskbits & PVF_WRITE)
1646                 vm_page_flag_clear(pg, PG_WRITEABLE);
1647         return (count);
1648 }
1649
1650 /*
1651  * main pv_entry manipulation functions:
1652  *   pmap_enter_pv: enter a mapping onto a vm_page list
1653  *   pmap_remove_pv: remove a mappiing from a vm_page list
1654  *
1655  * NOTE: pmap_enter_pv expects to lock the pvh itself
1656  *       pmap_remove_pv expects te caller to lock the pvh before calling
1657  */
1658
1659 /*
1660  * pmap_enter_pv: enter a mapping onto a vm_page lst
1661  *
1662  * => caller should hold the proper lock on pmap_main_lock
1663  * => caller should have pmap locked
1664  * => we will gain the lock on the vm_page and allocate the new pv_entry
1665  * => caller should adjust ptp's wire_count before calling
1666  * => caller should not adjust pmap's wire_count
1667  */
1668 static void
1669 pmap_enter_pv(struct vm_page *pg, struct pv_entry *pve, pmap_t pm,
1670     vm_offset_t va, u_int flags)
1671 {
1672
1673         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1674         PMAP_ASSERT_LOCKED(pm);
1675         pve->pv_pmap = pm;
1676         pve->pv_va = va;
1677         pve->pv_flags = flags;
1678
1679         TAILQ_INSERT_HEAD(&pg->md.pv_list, pve, pv_list);
1680         TAILQ_INSERT_HEAD(&pm->pm_pvlist, pve, pv_plist);
1681         pg->md.pvh_attrs |= flags & (PVF_REF | PVF_MOD);
1682         if (pve->pv_flags & PVF_WIRED)
1683                 ++pm->pm_stats.wired_count;
1684         vm_page_flag_set(pg, PG_REFERENCED);
1685 }
1686
1687 /*
1688  *
1689  * pmap_find_pv: Find a pv entry
1690  *
1691  * => caller should hold lock on vm_page
1692  */
1693 static PMAP_INLINE struct pv_entry *
1694 pmap_find_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1695 {
1696         struct pv_entry *pv;
1697
1698         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1699         TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list)
1700             if (pm == pv->pv_pmap && va == pv->pv_va)
1701                     break;
1702         return (pv);
1703 }
1704
1705 /*
1706  * vector_page_setprot:
1707  *
1708  *      Manipulate the protection of the vector page.
1709  */
1710 void
1711 vector_page_setprot(int prot)
1712 {
1713         struct l2_bucket *l2b;
1714         pt_entry_t *ptep;
1715
1716         l2b = pmap_get_l2_bucket(pmap_kernel(), vector_page);
1717
1718         ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
1719
1720         *ptep = (*ptep & ~L1_S_PROT_MASK) | L2_S_PROT(PTE_KERNEL, prot);
1721         PTE_SYNC(ptep);
1722         cpu_tlb_flushD_SE(vector_page);
1723         cpu_cpwait();
1724 }
1725
1726 /*
1727  * pmap_remove_pv: try to remove a mapping from a pv_list
1728  *
1729  * => caller should hold proper lock on pmap_main_lock
1730  * => pmap should be locked
1731  * => caller should hold lock on vm_page [so that attrs can be adjusted]
1732  * => caller should adjust ptp's wire_count and free PTP if needed
1733  * => caller should NOT adjust pmap's wire_count
1734  * => we return the removed pve
1735  */
1736
1737 static void
1738 pmap_nuke_pv(struct vm_page *pg, pmap_t pm, struct pv_entry *pve)
1739 {
1740
1741         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1742         PMAP_ASSERT_LOCKED(pm);
1743         TAILQ_REMOVE(&pg->md.pv_list, pve, pv_list);
1744         TAILQ_REMOVE(&pm->pm_pvlist, pve, pv_plist);
1745         if (pve->pv_flags & PVF_WIRED)
1746                 --pm->pm_stats.wired_count;
1747         if (pg->md.pvh_attrs & PVF_MOD)
1748                 vm_page_dirty(pg);
1749         if (TAILQ_FIRST(&pg->md.pv_list) == NULL)
1750                 pg->md.pvh_attrs &= ~PVF_REF;
1751         else
1752                 vm_page_flag_set(pg, PG_REFERENCED);
1753         if ((pve->pv_flags & PVF_NC) && ((pm == pmap_kernel()) ||
1754              (pve->pv_flags & PVF_WRITE) || !(pve->pv_flags & PVF_MWC)))
1755                 pmap_fix_cache(pg, pm, 0);
1756         else if (pve->pv_flags & PVF_WRITE) {
1757                 TAILQ_FOREACH(pve, &pg->md.pv_list, pv_list)
1758                     if (pve->pv_flags & PVF_WRITE)
1759                             break;
1760                 if (!pve) {
1761                         pg->md.pvh_attrs &= ~PVF_MOD;
1762                         vm_page_flag_clear(pg, PG_WRITEABLE);
1763                 }
1764         }
1765 }
1766
1767 static struct pv_entry *
1768 pmap_remove_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1769 {
1770         struct pv_entry *pve;
1771
1772         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1773         pve = TAILQ_FIRST(&pg->md.pv_list);
1774
1775         while (pve) {
1776                 if (pve->pv_pmap == pm && pve->pv_va == va) {   /* match? */
1777                         pmap_nuke_pv(pg, pm, pve);
1778                         break;
1779                 }
1780                 pve = TAILQ_NEXT(pve, pv_list);
1781         }
1782
1783         return(pve);                            /* return removed pve */
1784 }
1785 /*
1786  *
1787  * pmap_modify_pv: Update pv flags
1788  *
1789  * => caller should hold lock on vm_page [so that attrs can be adjusted]
1790  * => caller should NOT adjust pmap's wire_count
1791  * => we return the old flags
1792  * 
1793  * Modify a physical-virtual mapping in the pv table
1794  */
1795 static u_int
1796 pmap_modify_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va,
1797     u_int clr_mask, u_int set_mask)
1798 {
1799         struct pv_entry *npv;
1800         u_int flags, oflags;
1801
1802         PMAP_ASSERT_LOCKED(pm);
1803         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1804         if ((npv = pmap_find_pv(pg, pm, va)) == NULL)
1805                 return (0);
1806
1807         /*
1808          * There is at least one VA mapping this page.
1809          */
1810
1811         if (clr_mask & (PVF_REF | PVF_MOD))
1812                 pg->md.pvh_attrs |= set_mask & (PVF_REF | PVF_MOD);
1813
1814         oflags = npv->pv_flags;
1815         npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask;
1816
1817         if ((flags ^ oflags) & PVF_WIRED) {
1818                 if (flags & PVF_WIRED)
1819                         ++pm->pm_stats.wired_count;
1820                 else
1821                         --pm->pm_stats.wired_count;
1822         }
1823
1824         if ((flags ^ oflags) & PVF_WRITE)
1825                 pmap_fix_cache(pg, pm, 0);
1826
1827         return (oflags);
1828 }
1829
1830 /* Function to set the debug level of the pmap code */
1831 #ifdef PMAP_DEBUG
1832 void
1833 pmap_debug(int level)
1834 {
1835         pmap_debug_level = level;
1836         dprintf("pmap_debug: level=%d\n", pmap_debug_level);
1837 }
1838 #endif  /* PMAP_DEBUG */
1839
1840 void
1841 pmap_pinit0(struct pmap *pmap)
1842 {
1843         PDEBUG(1, printf("pmap_pinit0: pmap = %08x\n", (u_int32_t) pmap));
1844
1845         dprintf("pmap_pinit0: pmap = %08x, pm_pdir = %08x\n",
1846                 (u_int32_t) pmap, (u_int32_t) pmap->pm_pdir);
1847         bcopy(kernel_pmap, pmap, sizeof(*pmap));
1848         bzero(&pmap->pm_mtx, sizeof(pmap->pm_mtx));
1849         PMAP_LOCK_INIT(pmap);
1850 }
1851
1852 /*
1853  *      Initialize a vm_page's machine-dependent fields.
1854  */
1855 void
1856 pmap_page_init(vm_page_t m)
1857 {
1858
1859         TAILQ_INIT(&m->md.pv_list);
1860 }
1861
1862 /*
1863  *      Initialize the pmap module.
1864  *      Called by vm_init, to initialize any structures that the pmap
1865  *      system needs to map virtual memory.
1866  */
1867 void
1868 pmap_init(void)
1869 {
1870         int shpgperproc = PMAP_SHPGPERPROC;
1871
1872         PDEBUG(1, printf("pmap_init: phys_start = %08x\n"));
1873
1874         /*
1875          * init the pv free list
1876          */
1877         pvzone = uma_zcreate("PV ENTRY", sizeof (struct pv_entry), NULL, NULL, 
1878             NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1879         /*
1880          * Now it is safe to enable pv_table recording.
1881          */
1882         PDEBUG(1, printf("pmap_init: done!\n"));
1883
1884         TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1885         
1886         pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
1887         pv_entry_high_water = 9 * (pv_entry_max / 10);
1888         l2zone = uma_zcreate("L2 Table", L2_TABLE_SIZE_REAL, pmap_l2ptp_ctor,
1889             NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1890         l2table_zone = uma_zcreate("L2 Table", sizeof(struct l2_dtable),
1891             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
1892             UMA_ZONE_VM | UMA_ZONE_NOFREE);
1893
1894         uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max);
1895
1896 }
1897
1898 int
1899 pmap_fault_fixup(pmap_t pm, vm_offset_t va, vm_prot_t ftype, int user)
1900 {
1901         struct l2_dtable *l2;
1902         struct l2_bucket *l2b;
1903         pd_entry_t *pl1pd, l1pd;
1904         pt_entry_t *ptep, pte;
1905         vm_paddr_t pa;
1906         u_int l1idx;
1907         int rv = 0;
1908
1909         l1idx = L1_IDX(va);
1910         vm_page_lock_queues();
1911         PMAP_LOCK(pm);
1912
1913         /*
1914          * If there is no l2_dtable for this address, then the process
1915          * has no business accessing it.
1916          *
1917          * Note: This will catch userland processes trying to access
1918          * kernel addresses.
1919          */
1920         l2 = pm->pm_l2[L2_IDX(l1idx)];
1921         if (l2 == NULL)
1922                 goto out;
1923
1924         /*
1925          * Likewise if there is no L2 descriptor table
1926          */
1927         l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
1928         if (l2b->l2b_kva == NULL)
1929                 goto out;
1930
1931         /*
1932          * Check the PTE itself.
1933          */
1934         ptep = &l2b->l2b_kva[l2pte_index(va)];
1935         pte = *ptep;
1936         if (pte == 0)
1937                 goto out;
1938
1939         /*
1940          * Catch a userland access to the vector page mapped at 0x0
1941          */
1942         if (user && (pte & L2_S_PROT_U) == 0)
1943                 goto out;
1944         if (va == vector_page)
1945                 goto out;
1946
1947         pa = l2pte_pa(pte);
1948
1949         if ((ftype & VM_PROT_WRITE) && (pte & L2_S_PROT_W) == 0) {
1950                 /*
1951                  * This looks like a good candidate for "page modified"
1952                  * emulation...
1953                  */
1954                 struct pv_entry *pv;
1955                 struct vm_page *pg;
1956
1957                 /* Extract the physical address of the page */
1958                 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL) {
1959                         goto out;
1960                 }
1961                 /* Get the current flags for this page. */
1962
1963                 pv = pmap_find_pv(pg, pm, va);
1964                 if (pv == NULL) {
1965                         goto out;
1966                 }
1967
1968                 /*
1969                  * Do the flags say this page is writable? If not then it
1970                  * is a genuine write fault. If yes then the write fault is
1971                  * our fault as we did not reflect the write access in the
1972                  * PTE. Now we know a write has occurred we can correct this
1973                  * and also set the modified bit
1974                  */
1975                 if ((pv->pv_flags & PVF_WRITE) == 0) {
1976                         goto out;
1977                 }
1978
1979                 pg->md.pvh_attrs |= PVF_REF | PVF_MOD;
1980                 vm_page_dirty(pg);
1981                 pv->pv_flags |= PVF_REF | PVF_MOD;
1982
1983                 /* 
1984                  * Re-enable write permissions for the page.  No need to call
1985                  * pmap_fix_cache(), since this is just a
1986                  * modified-emulation fault, and the PVF_WRITE bit isn't
1987                  * changing. We've already set the cacheable bits based on
1988                  * the assumption that we can write to this page.
1989                  */
1990                 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO | L2_S_PROT_W;
1991                 PTE_SYNC(ptep);
1992                 rv = 1;
1993         } else
1994         if ((pte & L2_TYPE_MASK) == L2_TYPE_INV) {
1995                 /*
1996                  * This looks like a good candidate for "page referenced"
1997                  * emulation.
1998                  */
1999                 struct pv_entry *pv;
2000                 struct vm_page *pg;
2001
2002                 /* Extract the physical address of the page */
2003                 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
2004                         goto out;
2005                 /* Get the current flags for this page. */
2006
2007                 pv = pmap_find_pv(pg, pm, va);
2008                 if (pv == NULL)
2009                         goto out;
2010
2011                 pg->md.pvh_attrs |= PVF_REF;
2012                 pv->pv_flags |= PVF_REF;
2013
2014
2015                 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO;
2016                 PTE_SYNC(ptep);
2017                 rv = 1;
2018         }
2019
2020         /*
2021          * We know there is a valid mapping here, so simply
2022          * fix up the L1 if necessary.
2023          */
2024         pl1pd = &pm->pm_l1->l1_kva[l1idx];
2025         l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) | L1_C_PROTO;
2026         if (*pl1pd != l1pd) {
2027                 *pl1pd = l1pd;
2028                 PTE_SYNC(pl1pd);
2029                 rv = 1;
2030         }
2031
2032 #ifdef CPU_SA110
2033         /*
2034          * There are bugs in the rev K SA110.  This is a check for one
2035          * of them.
2036          */
2037         if (rv == 0 && curcpu()->ci_arm_cputype == CPU_ID_SA110 &&
2038             curcpu()->ci_arm_cpurev < 3) {
2039                 /* Always current pmap */
2040                 if (l2pte_valid(pte)) {
2041                         extern int kernel_debug;
2042                         if (kernel_debug & 1) {
2043                                 struct proc *p = curlwp->l_proc;
2044                                 printf("prefetch_abort: page is already "
2045                                     "mapped - pte=%p *pte=%08x\n", ptep, pte);
2046                                 printf("prefetch_abort: pc=%08lx proc=%p "
2047                                     "process=%s\n", va, p, p->p_comm);
2048                                 printf("prefetch_abort: far=%08x fs=%x\n",
2049                                     cpu_faultaddress(), cpu_faultstatus());
2050                         }
2051 #ifdef DDB
2052                         if (kernel_debug & 2)
2053                                 Debugger();
2054 #endif
2055                         rv = 1;
2056                 }
2057         }
2058 #endif /* CPU_SA110 */
2059
2060 #ifdef DEBUG
2061         /*
2062          * If 'rv == 0' at this point, it generally indicates that there is a
2063          * stale TLB entry for the faulting address. This happens when two or
2064          * more processes are sharing an L1. Since we don't flush the TLB on
2065          * a context switch between such processes, we can take domain faults
2066          * for mappings which exist at the same VA in both processes. EVEN IF
2067          * WE'VE RECENTLY FIXED UP THE CORRESPONDING L1 in pmap_enter(), for
2068          * example.
2069          *
2070          * This is extremely likely to happen if pmap_enter() updated the L1
2071          * entry for a recently entered mapping. In this case, the TLB is
2072          * flushed for the new mapping, but there may still be TLB entries for
2073          * other mappings belonging to other processes in the 1MB range
2074          * covered by the L1 entry.
2075          *
2076          * Since 'rv == 0', we know that the L1 already contains the correct
2077          * value, so the fault must be due to a stale TLB entry.
2078          *
2079          * Since we always need to flush the TLB anyway in the case where we
2080          * fixed up the L1, or frobbed the L2 PTE, we effectively deal with
2081          * stale TLB entries dynamically.
2082          *
2083          * However, the above condition can ONLY happen if the current L1 is
2084          * being shared. If it happens when the L1 is unshared, it indicates
2085          * that other parts of the pmap are not doing their job WRT managing
2086          * the TLB.
2087          */
2088         if (rv == 0 && pm->pm_l1->l1_domain_use_count == 1) {
2089                 extern int last_fault_code;
2090                 printf("fixup: pm %p, va 0x%lx, ftype %d - nothing to do!\n",
2091                     pm, va, ftype);
2092                 printf("fixup: l2 %p, l2b %p, ptep %p, pl1pd %p\n",
2093                     l2, l2b, ptep, pl1pd);
2094                 printf("fixup: pte 0x%x, l1pd 0x%x, last code 0x%x\n",
2095                     pte, l1pd, last_fault_code);
2096 #ifdef DDB
2097                 Debugger();
2098 #endif
2099         }
2100 #endif
2101
2102         cpu_tlb_flushID_SE(va);
2103         cpu_cpwait();
2104
2105         rv = 1;
2106
2107 out:
2108         vm_page_unlock_queues();
2109         PMAP_UNLOCK(pm);
2110         return (rv);
2111 }
2112
2113 void
2114 pmap_postinit(void)
2115 {
2116         struct l2_bucket *l2b;
2117         struct l1_ttable *l1;
2118         pd_entry_t *pl1pt;
2119         pt_entry_t *ptep, pte;
2120         vm_offset_t va, eva;
2121         u_int loop, needed;
2122         
2123         needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
2124         needed -= 1;
2125         l1 = malloc(sizeof(*l1) * needed, M_VMPMAP, M_WAITOK);
2126
2127         for (loop = 0; loop < needed; loop++, l1++) {
2128                 /* Allocate a L1 page table */
2129                 va = (vm_offset_t)contigmalloc(L1_TABLE_SIZE, M_VMPMAP, 0, 0x0,
2130                     0xffffffff, L1_TABLE_SIZE, 0);
2131
2132                 if (va == 0)
2133                         panic("Cannot allocate L1 KVM");
2134
2135                 eva = va + L1_TABLE_SIZE;
2136                 pl1pt = (pd_entry_t *)va;
2137                 
2138                 while (va < eva) {
2139                                 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2140                                 ptep = &l2b->l2b_kva[l2pte_index(va)];
2141                                 pte = *ptep;
2142                                 pte = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
2143                                 *ptep = pte;
2144                                 PTE_SYNC(ptep);
2145                                 cpu_tlb_flushD_SE(va);
2146                                 
2147                                 va += PAGE_SIZE;
2148                 }
2149                 pmap_init_l1(l1, pl1pt);
2150         }
2151
2152
2153 #ifdef DEBUG
2154         printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
2155             needed);
2156 #endif
2157 }
2158
2159 /*
2160  * This is used to stuff certain critical values into the PCB where they
2161  * can be accessed quickly from cpu_switch() et al.
2162  */
2163 void
2164 pmap_set_pcb_pagedir(pmap_t pm, struct pcb *pcb)
2165 {
2166         struct l2_bucket *l2b;
2167
2168         pcb->pcb_pagedir = pm->pm_l1->l1_physaddr;
2169         pcb->pcb_dacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
2170             (DOMAIN_CLIENT << (pm->pm_domain * 2));
2171
2172         if (vector_page < KERNBASE) {
2173                 pcb->pcb_pl1vec = &pm->pm_l1->l1_kva[L1_IDX(vector_page)];
2174                 l2b = pmap_get_l2_bucket(pm, vector_page);
2175                 pcb->pcb_l1vec = l2b->l2b_phys | L1_C_PROTO |
2176                     L1_C_DOM(pm->pm_domain) | L1_C_DOM(PMAP_DOMAIN_KERNEL);
2177         } else
2178                 pcb->pcb_pl1vec = NULL;
2179 }
2180
2181 void
2182 pmap_activate(struct thread *td)
2183 {
2184         pmap_t pm;
2185         struct pcb *pcb;
2186
2187         pm = vmspace_pmap(td->td_proc->p_vmspace);
2188         pcb = td->td_pcb;
2189
2190         critical_enter();
2191         pmap_set_pcb_pagedir(pm, pcb);
2192
2193         if (td == curthread) {
2194                 u_int cur_dacr, cur_ttb;
2195
2196                 __asm __volatile("mrc p15, 0, %0, c2, c0, 0" : "=r"(cur_ttb));
2197                 __asm __volatile("mrc p15, 0, %0, c3, c0, 0" : "=r"(cur_dacr));
2198
2199                 cur_ttb &= ~(L1_TABLE_SIZE - 1);
2200
2201                 if (cur_ttb == (u_int)pcb->pcb_pagedir &&
2202                     cur_dacr == pcb->pcb_dacr) {
2203                         /*
2204                          * No need to switch address spaces.
2205                          */
2206                         critical_exit();
2207                         return;
2208                 }
2209
2210
2211                 /*
2212                  * We MUST, I repeat, MUST fix up the L1 entry corresponding
2213                  * to 'vector_page' in the incoming L1 table before switching
2214                  * to it otherwise subsequent interrupts/exceptions (including
2215                  * domain faults!) will jump into hyperspace.
2216                  */
2217                 if (pcb->pcb_pl1vec) {
2218
2219                         *pcb->pcb_pl1vec = pcb->pcb_l1vec;
2220                         /*
2221                          * Don't need to PTE_SYNC() at this point since
2222                          * cpu_setttb() is about to flush both the cache
2223                          * and the TLB.
2224                          */
2225                 }
2226
2227                 cpu_domains(pcb->pcb_dacr);
2228                 cpu_setttb(pcb->pcb_pagedir);
2229         }
2230         critical_exit();
2231 }
2232
2233 static int
2234 pmap_set_pt_cache_mode(pd_entry_t *kl1, vm_offset_t va)
2235 {
2236         pd_entry_t *pdep, pde;
2237         pt_entry_t *ptep, pte;
2238         vm_offset_t pa;
2239         int rv = 0;
2240
2241         /*
2242          * Make sure the descriptor itself has the correct cache mode
2243          */
2244         pdep = &kl1[L1_IDX(va)];
2245         pde = *pdep;
2246
2247         if (l1pte_section_p(pde)) {
2248                 if ((pde & L1_S_CACHE_MASK) != pte_l1_s_cache_mode_pt) {
2249                         *pdep = (pde & ~L1_S_CACHE_MASK) |
2250                             pte_l1_s_cache_mode_pt;
2251                         PTE_SYNC(pdep);
2252                         cpu_dcache_wbinv_range((vm_offset_t)pdep,
2253                             sizeof(*pdep));
2254                         cpu_l2cache_wbinv_range((vm_offset_t)pdep,
2255                             sizeof(*pdep));
2256                         rv = 1;
2257                 }
2258         } else {
2259                 pa = (vm_paddr_t)(pde & L1_C_ADDR_MASK);
2260                 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
2261                 if (ptep == NULL)
2262                         panic("pmap_bootstrap: No L2 for L2 @ va %p\n", ptep);
2263
2264                 ptep = &ptep[l2pte_index(va)];
2265                 pte = *ptep;
2266                 if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
2267                         *ptep = (pte & ~L2_S_CACHE_MASK) |
2268                             pte_l2_s_cache_mode_pt;
2269                         PTE_SYNC(ptep);
2270                         cpu_dcache_wbinv_range((vm_offset_t)ptep,
2271                             sizeof(*ptep));
2272                         cpu_l2cache_wbinv_range((vm_offset_t)ptep,
2273                             sizeof(*ptep));
2274                         rv = 1;
2275                 }
2276         }
2277
2278         return (rv);
2279 }
2280
2281 static void
2282 pmap_alloc_specials(vm_offset_t *availp, int pages, vm_offset_t *vap, 
2283     pt_entry_t **ptep)
2284 {
2285         vm_offset_t va = *availp;
2286         struct l2_bucket *l2b;
2287
2288         if (ptep) {
2289                 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2290                 if (l2b == NULL)
2291                         panic("pmap_alloc_specials: no l2b for 0x%x", va);
2292
2293                 *ptep = &l2b->l2b_kva[l2pte_index(va)];
2294         }
2295
2296         *vap = va;
2297         *availp = va + (PAGE_SIZE * pages);
2298 }
2299
2300 /*
2301  *      Bootstrap the system enough to run with virtual memory.
2302  *
2303  *      On the arm this is called after mapping has already been enabled
2304  *      and just syncs the pmap module with what has already been done.
2305  *      [We can't call it easily with mapping off since the kernel is not
2306  *      mapped with PA == VA, hence we would have to relocate every address
2307  *      from the linked base (virtual) address "KERNBASE" to the actual
2308  *      (physical) address starting relative to 0]
2309  */
2310 #define PMAP_STATIC_L2_SIZE 16
2311 #ifdef ARM_USE_SMALL_ALLOC
2312 extern struct mtx smallalloc_mtx;
2313 #endif
2314
2315 void
2316 pmap_bootstrap(vm_offset_t firstaddr, vm_offset_t lastaddr, struct pv_addr *l1pt)
2317 {
2318         static struct l1_ttable static_l1;
2319         static struct l2_dtable static_l2[PMAP_STATIC_L2_SIZE];
2320         struct l1_ttable *l1 = &static_l1;
2321         struct l2_dtable *l2;
2322         struct l2_bucket *l2b;
2323         pd_entry_t pde;
2324         pd_entry_t *kernel_l1pt = (pd_entry_t *)l1pt->pv_va;
2325         pt_entry_t *ptep;
2326         vm_paddr_t pa;
2327         vm_offset_t va;
2328         vm_size_t size;
2329         int l1idx, l2idx, l2next = 0;
2330
2331         PDEBUG(1, printf("firstaddr = %08x, loadaddr = %08x\n",
2332             firstaddr, loadaddr));
2333         
2334         virtual_avail = firstaddr;
2335         kernel_pmap = &kernel_pmap_store;
2336         kernel_pmap->pm_l1 = l1;
2337         kernel_l1pa = l1pt->pv_pa;
2338         
2339         /*
2340          * Scan the L1 translation table created by initarm() and create
2341          * the required metadata for all valid mappings found in it.
2342          */
2343         for (l1idx = 0; l1idx < (L1_TABLE_SIZE / sizeof(pd_entry_t)); l1idx++) {
2344                 pde = kernel_l1pt[l1idx];
2345
2346                 /*
2347                  * We're only interested in Coarse mappings.
2348                  * pmap_extract() can deal with section mappings without
2349                  * recourse to checking L2 metadata.
2350                  */
2351                 if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
2352                         continue;
2353
2354                 /*
2355                  * Lookup the KVA of this L2 descriptor table
2356                  */
2357                 pa = (vm_paddr_t)(pde & L1_C_ADDR_MASK);
2358                 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
2359                 
2360                 if (ptep == NULL) {
2361                         panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
2362                             (u_int)l1idx << L1_S_SHIFT, (long unsigned int)pa);
2363                 }
2364
2365                 /*
2366                  * Fetch the associated L2 metadata structure.
2367                  * Allocate a new one if necessary.
2368                  */
2369                 if ((l2 = kernel_pmap->pm_l2[L2_IDX(l1idx)]) == NULL) {
2370                         if (l2next == PMAP_STATIC_L2_SIZE)
2371                                 panic("pmap_bootstrap: out of static L2s");
2372                         kernel_pmap->pm_l2[L2_IDX(l1idx)] = l2 = 
2373                             &static_l2[l2next++];
2374                 }
2375
2376                 /*
2377                  * One more L1 slot tracked...
2378                  */
2379                 l2->l2_occupancy++;
2380
2381                 /*
2382                  * Fill in the details of the L2 descriptor in the
2383                  * appropriate bucket.
2384                  */
2385                 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2386                 l2b->l2b_kva = ptep;
2387                 l2b->l2b_phys = pa;
2388                 l2b->l2b_l1idx = l1idx;
2389
2390                 /*
2391                  * Establish an initial occupancy count for this descriptor
2392                  */
2393                 for (l2idx = 0;
2394                     l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
2395                     l2idx++) {
2396                         if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
2397                                 l2b->l2b_occupancy++;
2398                         }
2399                 }
2400
2401                 /*
2402                  * Make sure the descriptor itself has the correct cache mode.
2403                  * If not, fix it, but whine about the problem. Port-meisters
2404                  * should consider this a clue to fix up their initarm()
2405                  * function. :)
2406                  */
2407                 if (pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)ptep)) {
2408                         printf("pmap_bootstrap: WARNING! wrong cache mode for "
2409                             "L2 pte @ %p\n", ptep);
2410                 }
2411         }
2412
2413         
2414         /*
2415          * Ensure the primary (kernel) L1 has the correct cache mode for
2416          * a page table. Bitch if it is not correctly set.
2417          */
2418         for (va = (vm_offset_t)kernel_l1pt;
2419             va < ((vm_offset_t)kernel_l1pt + L1_TABLE_SIZE); va += PAGE_SIZE) {
2420                 if (pmap_set_pt_cache_mode(kernel_l1pt, va))
2421                         printf("pmap_bootstrap: WARNING! wrong cache mode for "
2422                             "primary L1 @ 0x%x\n", va);
2423         }
2424
2425         cpu_dcache_wbinv_all();
2426         cpu_l2cache_wbinv_all();
2427         cpu_tlb_flushID();
2428         cpu_cpwait();
2429
2430         PMAP_LOCK_INIT(kernel_pmap);
2431         kernel_pmap->pm_active = -1;
2432         kernel_pmap->pm_domain = PMAP_DOMAIN_KERNEL;
2433         TAILQ_INIT(&kernel_pmap->pm_pvlist);
2434         
2435         /*
2436          * Reserve some special page table entries/VA space for temporary
2437          * mapping of pages.
2438          */
2439 #define SYSMAP(c, p, v, n)                                              \
2440     v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
2441     
2442         pmap_alloc_specials(&virtual_avail, 1, &csrcp, &csrc_pte);
2443         pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)csrc_pte);
2444         pmap_alloc_specials(&virtual_avail, 1, &cdstp, &cdst_pte);
2445         pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)cdst_pte);
2446         size = ((lastaddr - pmap_curmaxkvaddr) + L1_S_OFFSET) / L1_S_SIZE;
2447         pmap_alloc_specials(&virtual_avail,
2448             round_page(size * L2_TABLE_SIZE_REAL) / PAGE_SIZE,
2449             &pmap_kernel_l2ptp_kva, NULL);
2450         
2451         size = (size + (L2_BUCKET_SIZE - 1)) / L2_BUCKET_SIZE;
2452         pmap_alloc_specials(&virtual_avail,
2453             round_page(size * sizeof(struct l2_dtable)) / PAGE_SIZE,
2454             &pmap_kernel_l2dtable_kva, NULL);
2455
2456         pmap_alloc_specials(&virtual_avail,
2457             1, (vm_offset_t*)&_tmppt, NULL);
2458         SLIST_INIT(&l1_list);
2459         TAILQ_INIT(&l1_lru_list);
2460         mtx_init(&l1_lru_lock, "l1 list lock", NULL, MTX_DEF);
2461         pmap_init_l1(l1, kernel_l1pt);
2462         cpu_dcache_wbinv_all();
2463         cpu_l2cache_wbinv_all();
2464
2465         virtual_avail = round_page(virtual_avail);
2466         virtual_end = lastaddr;
2467         kernel_vm_end = pmap_curmaxkvaddr;
2468         arm_nocache_startaddr = lastaddr;
2469         mtx_init(&cmtx, "TMP mappings mtx", NULL, MTX_DEF);
2470
2471 #ifdef ARM_USE_SMALL_ALLOC
2472         mtx_init(&smallalloc_mtx, "Small alloc page list", NULL, MTX_DEF);
2473         arm_init_smallalloc();
2474 #endif
2475         pmap_set_pcb_pagedir(kernel_pmap, thread0.td_pcb);
2476 }
2477
2478 /***************************************************
2479  * Pmap allocation/deallocation routines.
2480  ***************************************************/
2481
2482 /*
2483  * Release any resources held by the given physical map.
2484  * Called when a pmap initialized by pmap_pinit is being released.
2485  * Should only be called if the map contains no valid mappings.
2486  */
2487 void
2488 pmap_release(pmap_t pmap)
2489 {
2490         struct pcb *pcb;
2491         
2492         pmap_idcache_wbinv_all(pmap);
2493         cpu_l2cache_wbinv_all();
2494         pmap_tlb_flushID(pmap);
2495         cpu_cpwait();
2496         if (vector_page < KERNBASE) {
2497                 struct pcb *curpcb = PCPU_GET(curpcb);
2498                 pcb = thread0.td_pcb;
2499                 if (pmap_is_current(pmap)) {
2500                         /*
2501                          * Frob the L1 entry corresponding to the vector
2502                          * page so that it contains the kernel pmap's domain
2503                          * number. This will ensure pmap_remove() does not
2504                          * pull the current vector page out from under us.
2505                          */
2506                         critical_enter();
2507                         *pcb->pcb_pl1vec = pcb->pcb_l1vec;
2508                         cpu_domains(pcb->pcb_dacr);
2509                         cpu_setttb(pcb->pcb_pagedir);
2510                         critical_exit();
2511                 }
2512                 pmap_remove(pmap, vector_page, vector_page + PAGE_SIZE);
2513                 /*
2514                  * Make sure cpu_switch(), et al, DTRT. This is safe to do
2515                  * since this process has no remaining mappings of its own.
2516                  */
2517                 curpcb->pcb_pl1vec = pcb->pcb_pl1vec;
2518                 curpcb->pcb_l1vec = pcb->pcb_l1vec;
2519                 curpcb->pcb_dacr = pcb->pcb_dacr;
2520                 curpcb->pcb_pagedir = pcb->pcb_pagedir;
2521
2522         }
2523         pmap_free_l1(pmap);
2524         PMAP_LOCK_DESTROY(pmap);
2525         
2526         dprintf("pmap_release()\n");
2527 }
2528
2529
2530
2531 /*
2532  * Helper function for pmap_grow_l2_bucket()
2533  */
2534 static __inline int
2535 pmap_grow_map(vm_offset_t va, pt_entry_t cache_mode, vm_paddr_t *pap)
2536 {
2537         struct l2_bucket *l2b;
2538         pt_entry_t *ptep;
2539         vm_paddr_t pa;
2540         struct vm_page *pg;
2541         
2542         pg = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
2543         if (pg == NULL)
2544                 return (1);
2545         pa = VM_PAGE_TO_PHYS(pg);
2546
2547         if (pap)
2548                 *pap = pa;
2549
2550         l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2551
2552         ptep = &l2b->l2b_kva[l2pte_index(va)];
2553         *ptep = L2_S_PROTO | pa | cache_mode |
2554             L2_S_PROT(PTE_KERNEL, VM_PROT_READ | VM_PROT_WRITE);
2555         PTE_SYNC(ptep);
2556         return (0);
2557 }
2558
2559 /*
2560  * This is the same as pmap_alloc_l2_bucket(), except that it is only
2561  * used by pmap_growkernel().
2562  */
2563 static __inline struct l2_bucket *
2564 pmap_grow_l2_bucket(pmap_t pm, vm_offset_t va)
2565 {
2566         struct l2_dtable *l2;
2567         struct l2_bucket *l2b;
2568         struct l1_ttable *l1;
2569         pd_entry_t *pl1pd;
2570         u_short l1idx;
2571         vm_offset_t nva;
2572
2573         l1idx = L1_IDX(va);
2574
2575         if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
2576                 /*
2577                  * No mapping at this address, as there is
2578                  * no entry in the L1 table.
2579                  * Need to allocate a new l2_dtable.
2580                  */
2581                 nva = pmap_kernel_l2dtable_kva;
2582                 if ((nva & PAGE_MASK) == 0) {
2583                         /*
2584                          * Need to allocate a backing page
2585                          */
2586                         if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
2587                                 return (NULL);
2588                 }
2589
2590                 l2 = (struct l2_dtable *)nva;
2591                 nva += sizeof(struct l2_dtable);
2592
2593                 if ((nva & PAGE_MASK) < (pmap_kernel_l2dtable_kva & 
2594                     PAGE_MASK)) {
2595                         /*
2596                          * The new l2_dtable straddles a page boundary.
2597                          * Map in another page to cover it.
2598                          */
2599                         if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
2600                                 return (NULL);
2601                 }
2602
2603                 pmap_kernel_l2dtable_kva = nva;
2604
2605                 /*
2606                  * Link it into the parent pmap
2607                  */
2608                 pm->pm_l2[L2_IDX(l1idx)] = l2;
2609                 memset(l2, 0, sizeof(*l2));
2610         }
2611
2612         l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2613
2614         /*
2615          * Fetch pointer to the L2 page table associated with the address.
2616          */
2617         if (l2b->l2b_kva == NULL) {
2618                 pt_entry_t *ptep;
2619
2620                 /*
2621                  * No L2 page table has been allocated. Chances are, this
2622                  * is because we just allocated the l2_dtable, above.
2623                  */
2624                 nva = pmap_kernel_l2ptp_kva;
2625                 ptep = (pt_entry_t *)nva;
2626                 if ((nva & PAGE_MASK) == 0) {
2627                         /*
2628                          * Need to allocate a backing page
2629                          */
2630                         if (pmap_grow_map(nva, pte_l2_s_cache_mode_pt,
2631                             &pmap_kernel_l2ptp_phys))
2632                                 return (NULL);
2633                         PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
2634                 }
2635                 memset(ptep, 0, L2_TABLE_SIZE_REAL);
2636                 l2->l2_occupancy++;
2637                 l2b->l2b_kva = ptep;
2638                 l2b->l2b_l1idx = l1idx;
2639                 l2b->l2b_phys = pmap_kernel_l2ptp_phys;
2640
2641                 pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
2642                 pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
2643         }
2644
2645         /* Distribute new L1 entry to all other L1s */
2646         SLIST_FOREACH(l1, &l1_list, l1_link) {
2647                         pl1pd = &l1->l1_kva[L1_IDX(va)];
2648                         *pl1pd = l2b->l2b_phys | L1_C_DOM(PMAP_DOMAIN_KERNEL) |
2649                             L1_C_PROTO;
2650                         PTE_SYNC(pl1pd);
2651         }
2652
2653         return (l2b);
2654 }
2655
2656
2657 /*
2658  * grow the number of kernel page table entries, if needed
2659  */
2660 void
2661 pmap_growkernel(vm_offset_t addr)
2662 {
2663         pmap_t kpm = pmap_kernel();
2664
2665         if (addr <= pmap_curmaxkvaddr)
2666                 return;         /* we are OK */
2667
2668         /*
2669          * whoops!   we need to add kernel PTPs
2670          */
2671
2672         /* Map 1MB at a time */
2673         for (; pmap_curmaxkvaddr < addr; pmap_curmaxkvaddr += L1_S_SIZE)
2674                 pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
2675
2676         /*
2677          * flush out the cache, expensive but growkernel will happen so
2678          * rarely
2679          */
2680         cpu_dcache_wbinv_all();
2681         cpu_l2cache_wbinv_all();
2682         cpu_tlb_flushD();
2683         cpu_cpwait();
2684         kernel_vm_end = pmap_curmaxkvaddr;
2685 }
2686
2687
2688 /*
2689  * Remove all pages from specified address space
2690  * this aids process exit speeds.  Also, this code
2691  * is special cased for current process only, but
2692  * can have the more generic (and slightly slower)
2693  * mode enabled.  This is much faster than pmap_remove
2694  * in the case of running down an entire address space.
2695  */
2696 void
2697 pmap_remove_pages(pmap_t pmap)
2698 {
2699         struct pv_entry *pv, *npv;
2700         struct l2_bucket *l2b = NULL;
2701         vm_page_t m;
2702         pt_entry_t *pt;
2703         
2704         vm_page_lock_queues();
2705         PMAP_LOCK(pmap);
2706         cpu_idcache_wbinv_all();
2707         cpu_l2cache_wbinv_all();
2708         for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2709                 if (pv->pv_flags & PVF_WIRED) {
2710                         /* The page is wired, cannot remove it now. */
2711                         npv = TAILQ_NEXT(pv, pv_plist);
2712                         continue;
2713                 }
2714                 pmap->pm_stats.resident_count--;
2715                 l2b = pmap_get_l2_bucket(pmap, pv->pv_va);
2716                 KASSERT(l2b != NULL, ("No L2 bucket in pmap_remove_pages"));
2717                 pt = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
2718                 m = PHYS_TO_VM_PAGE(*pt & L2_ADDR_MASK);
2719 #ifdef ARM_USE_SMALL_ALLOC
2720                 KASSERT((vm_offset_t)m >= alloc_firstaddr, ("Trying to access non-existent page va %x pte %x", pv->pv_va, *pt));
2721 #else
2722                 KASSERT((vm_offset_t)m >= KERNBASE, ("Trying to access non-existent page va %x pte %x", pv->pv_va, *pt));
2723 #endif
2724                 *pt = 0;
2725                 PTE_SYNC(pt);
2726                 npv = TAILQ_NEXT(pv, pv_plist);
2727                 pmap_nuke_pv(m, pmap, pv);
2728                 if (TAILQ_EMPTY(&m->md.pv_list))
2729                         vm_page_flag_clear(m, PG_WRITEABLE);
2730                 pmap_free_pv_entry(pv);
2731                 pmap_free_l2_bucket(pmap, l2b, 1);
2732         }
2733         vm_page_unlock_queues();
2734         cpu_tlb_flushID();
2735         cpu_cpwait();
2736         PMAP_UNLOCK(pmap);
2737 }
2738
2739
2740 /***************************************************
2741  * Low level mapping routines.....
2742  ***************************************************/
2743
2744 #ifdef ARM_HAVE_SUPERSECTIONS
2745 /* Map a super section into the KVA. */
2746
2747 void
2748 pmap_kenter_supersection(vm_offset_t va, uint64_t pa, int flags)
2749 {
2750         pd_entry_t pd = L1_S_PROTO | L1_S_SUPERSEC | (pa & L1_SUP_FRAME) |
2751             (((pa >> 32) & 0xf) << 20) | L1_S_PROT(PTE_KERNEL,
2752             VM_PROT_READ|VM_PROT_WRITE) | L1_S_DOM(PMAP_DOMAIN_KERNEL);
2753         struct l1_ttable *l1;   
2754         vm_offset_t va0, va_end;
2755
2756         KASSERT(((va | pa) & L1_SUP_OFFSET) == 0,
2757             ("Not a valid super section mapping"));
2758         if (flags & SECTION_CACHE)
2759                 pd |= pte_l1_s_cache_mode;
2760         else if (flags & SECTION_PT)
2761                 pd |= pte_l1_s_cache_mode_pt;
2762         va0 = va & L1_SUP_FRAME;
2763         va_end = va + L1_SUP_SIZE;
2764         SLIST_FOREACH(l1, &l1_list, l1_link) {
2765                 va = va0;
2766                 for (; va < va_end; va += L1_S_SIZE) {
2767                         l1->l1_kva[L1_IDX(va)] = pd;
2768                         PTE_SYNC(&l1->l1_kva[L1_IDX(va)]);
2769                 }
2770         }
2771 }
2772 #endif
2773
2774 /* Map a section into the KVA. */
2775
2776 void
2777 pmap_kenter_section(vm_offset_t va, vm_offset_t pa, int flags)
2778 {
2779         pd_entry_t pd = L1_S_PROTO | pa | L1_S_PROT(PTE_KERNEL,
2780             VM_PROT_READ|VM_PROT_WRITE) | L1_S_DOM(PMAP_DOMAIN_KERNEL);
2781         struct l1_ttable *l1;
2782
2783         KASSERT(((va | pa) & L1_S_OFFSET) == 0,
2784             ("Not a valid section mapping"));
2785         if (flags & SECTION_CACHE)
2786                 pd |= pte_l1_s_cache_mode;
2787         else if (flags & SECTION_PT)
2788                 pd |= pte_l1_s_cache_mode_pt;
2789         SLIST_FOREACH(l1, &l1_list, l1_link) {
2790                 l1->l1_kva[L1_IDX(va)] = pd;
2791                 PTE_SYNC(&l1->l1_kva[L1_IDX(va)]);
2792         }
2793 }
2794
2795 /*
2796  * add a wired page to the kva
2797  * note that in order for the mapping to take effect -- you
2798  * should do a invltlb after doing the pmap_kenter...
2799  */
2800 static PMAP_INLINE void
2801 pmap_kenter_internal(vm_offset_t va, vm_offset_t pa, int flags)
2802 {
2803         struct l2_bucket *l2b;
2804         pt_entry_t *pte;
2805         pt_entry_t opte;
2806         PDEBUG(1, printf("pmap_kenter: va = %08x, pa = %08x\n",
2807             (uint32_t) va, (uint32_t) pa));
2808
2809
2810         l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2811         if (l2b == NULL)
2812                 l2b = pmap_grow_l2_bucket(pmap_kernel(), va);
2813         KASSERT(l2b != NULL, ("No L2 Bucket"));
2814         pte = &l2b->l2b_kva[l2pte_index(va)];
2815         opte = *pte;
2816         PDEBUG(1, printf("pmap_kenter: pte = %08x, opte = %08x, npte = %08x\n",
2817             (uint32_t) pte, opte, *pte));
2818         if (l2pte_valid(opte)) {
2819                 cpu_dcache_wbinv_range(va, PAGE_SIZE);
2820                 cpu_l2cache_wbinv_range(va, PAGE_SIZE);
2821                 cpu_tlb_flushD_SE(va);
2822                 cpu_cpwait();
2823         } else {
2824                 if (opte == 0)
2825                         l2b->l2b_occupancy++;
2826         }
2827         *pte = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, 
2828             VM_PROT_READ | VM_PROT_WRITE);
2829         if (flags & KENTER_CACHE)
2830                 *pte |= pte_l2_s_cache_mode;
2831         if (flags & KENTER_USER)
2832                 *pte |= L2_S_PROT_U;
2833         PTE_SYNC(pte);
2834 }
2835
2836 void
2837 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
2838 {
2839         pmap_kenter_internal(va, pa, KENTER_CACHE);
2840 }
2841
2842 void
2843 pmap_kenter_nocache(vm_offset_t va, vm_paddr_t pa)
2844 {
2845
2846         pmap_kenter_internal(va, pa, 0);
2847 }
2848
2849 void
2850 pmap_kenter_user(vm_offset_t va, vm_paddr_t pa)
2851 {
2852
2853         pmap_kenter_internal(va, pa, KENTER_CACHE|KENTER_USER);
2854         /*
2855          * Call pmap_fault_fixup now, to make sure we'll have no exception
2856          * at the first use of the new address, or bad things will happen,
2857          * as we use one of these addresses in the exception handlers.
2858          */
2859         pmap_fault_fixup(pmap_kernel(), va, VM_PROT_READ|VM_PROT_WRITE, 1);
2860 }
2861
2862 /*
2863  * remove a page rom the kernel pagetables
2864  */
2865 void
2866 pmap_kremove(vm_offset_t va)
2867 {
2868         struct l2_bucket *l2b;
2869         pt_entry_t *pte, opte;
2870                 
2871         l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2872         if (!l2b)
2873                 return;
2874         KASSERT(l2b != NULL, ("No L2 Bucket"));
2875         pte = &l2b->l2b_kva[l2pte_index(va)];
2876         opte = *pte;
2877         if (l2pte_valid(opte)) {
2878                 cpu_dcache_wbinv_range(va, PAGE_SIZE);
2879                 cpu_l2cache_wbinv_range(va, PAGE_SIZE);
2880                 cpu_tlb_flushD_SE(va);
2881                 cpu_cpwait();
2882                 *pte = 0;
2883         }
2884 }
2885
2886
2887 /*
2888  *      Used to map a range of physical addresses into kernel
2889  *      virtual address space.
2890  *
2891  *      The value passed in '*virt' is a suggested virtual address for
2892  *      the mapping. Architectures which can support a direct-mapped
2893  *      physical to virtual region can return the appropriate address
2894  *      within that region, leaving '*virt' unchanged. Other
2895  *      architectures should map the pages starting at '*virt' and
2896  *      update '*virt' with the first usable address after the mapped
2897  *      region.
2898  */
2899 vm_offset_t
2900 pmap_map(vm_offset_t *virt, vm_offset_t start, vm_offset_t end, int prot)
2901 {
2902 #ifdef ARM_USE_SMALL_ALLOC
2903         return (arm_ptovirt(start));
2904 #else
2905         vm_offset_t sva = *virt;
2906         vm_offset_t va = sva;
2907
2908         PDEBUG(1, printf("pmap_map: virt = %08x, start = %08x, end = %08x, "
2909             "prot = %d\n", (uint32_t) *virt, (uint32_t) start, (uint32_t) end,
2910             prot));
2911             
2912         while (start < end) {
2913                 pmap_kenter(va, start);
2914                 va += PAGE_SIZE;
2915                 start += PAGE_SIZE;
2916         }
2917         *virt = va;
2918         return (sva);
2919 #endif
2920 }
2921
2922 static void
2923 pmap_wb_page(vm_page_t m)
2924 {
2925         struct pv_entry *pv;
2926
2927         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
2928             pmap_dcache_wb_range(pv->pv_pmap, pv->pv_va, PAGE_SIZE, FALSE,
2929                 (pv->pv_flags & PVF_WRITE) == 0);
2930 }
2931
2932 static void
2933 pmap_inv_page(vm_page_t m)
2934 {
2935         struct pv_entry *pv;
2936
2937         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
2938             pmap_dcache_wb_range(pv->pv_pmap, pv->pv_va, PAGE_SIZE, TRUE, TRUE);
2939 }
2940 /*
2941  * Add a list of wired pages to the kva
2942  * this routine is only used for temporary
2943  * kernel mappings that do not need to have
2944  * page modification or references recorded.
2945  * Note that old mappings are simply written
2946  * over.  The page *must* be wired.
2947  */
2948 void
2949 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
2950 {
2951         int i;
2952
2953         for (i = 0; i < count; i++) {
2954                 pmap_wb_page(m[i]);
2955                 pmap_kenter_internal(va, VM_PAGE_TO_PHYS(m[i]), 
2956                     KENTER_CACHE);
2957                 va += PAGE_SIZE;
2958         }
2959 }
2960
2961
2962 /*
2963  * this routine jerks page mappings from the
2964  * kernel -- it is meant only for temporary mappings.
2965  */
2966 void
2967 pmap_qremove(vm_offset_t va, int count)
2968 {
2969         vm_paddr_t pa;
2970         int i;
2971
2972         for (i = 0; i < count; i++) {
2973                 pa = vtophys(va);
2974                 if (pa) {
2975                         pmap_inv_page(PHYS_TO_VM_PAGE(pa));
2976                         pmap_kremove(va);
2977                 }
2978                 va += PAGE_SIZE;
2979         }
2980 }
2981
2982
2983 /*
2984  * pmap_object_init_pt preloads the ptes for a given object
2985  * into the specified pmap.  This eliminates the blast of soft
2986  * faults on process startup and immediately after an mmap.
2987  */
2988 void
2989 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
2990     vm_pindex_t pindex, vm_size_t size)
2991 {
2992
2993         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2994         KASSERT(object->type == OBJT_DEVICE,
2995             ("pmap_object_init_pt: non-device object"));
2996 }
2997
2998
2999 /*
3000  *      pmap_is_prefaultable:
3001  *
3002  *      Return whether or not the specified virtual address is elgible
3003  *      for prefault.
3004  */
3005 boolean_t
3006 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
3007 {
3008         pd_entry_t *pde;
3009         pt_entry_t *pte;
3010
3011         if (!pmap_get_pde_pte(pmap, addr, &pde, &pte))
3012                 return (FALSE);
3013         KASSERT(pte != NULL, ("Valid mapping but no pte ?"));
3014         if (*pte == 0)
3015                 return (TRUE);
3016         return (FALSE);
3017 }
3018
3019 /*
3020  * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
3021  * Returns TRUE if the mapping exists, else FALSE.
3022  *
3023  * NOTE: This function is only used by a couple of arm-specific modules.
3024  * It is not safe to take any pmap locks here, since we could be right
3025  * in the middle of debugging the pmap anyway...
3026  *
3027  * It is possible for this routine to return FALSE even though a valid
3028  * mapping does exist. This is because we don't lock, so the metadata
3029  * state may be inconsistent.
3030  *
3031  * NOTE: We can return a NULL *ptp in the case where the L1 pde is
3032  * a "section" mapping.
3033  */
3034 boolean_t
3035 pmap_get_pde_pte(pmap_t pm, vm_offset_t va, pd_entry_t **pdp, pt_entry_t **ptp)
3036 {
3037         struct l2_dtable *l2;
3038         pd_entry_t *pl1pd, l1pd;
3039         pt_entry_t *ptep;
3040         u_short l1idx;
3041
3042         if (pm->pm_l1 == NULL)
3043                 return (FALSE);
3044
3045         l1idx = L1_IDX(va);
3046         *pdp = pl1pd = &pm->pm_l1->l1_kva[l1idx];
3047         l1pd = *pl1pd;
3048
3049         if (l1pte_section_p(l1pd)) {
3050                 *ptp = NULL;
3051                 return (TRUE);
3052         }
3053
3054         if (pm->pm_l2 == NULL)
3055                 return (FALSE);
3056
3057         l2 = pm->pm_l2[L2_IDX(l1idx)];
3058
3059         if (l2 == NULL ||
3060             (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3061                 return (FALSE);
3062         }
3063
3064         *ptp = &ptep[l2pte_index(va)];
3065         return (TRUE);
3066 }
3067
3068 /*
3069  *      Routine:        pmap_remove_all
3070  *      Function:
3071  *              Removes this physical page from
3072  *              all physical maps in which it resides.
3073  *              Reflects back modify bits to the pager.
3074  *
3075  *      Notes:
3076  *              Original versions of this routine were very
3077  *              inefficient because they iteratively called
3078  *              pmap_remove (slow...)
3079  */
3080 void
3081 pmap_remove_all(vm_page_t m)
3082 {
3083         pv_entry_t pv;
3084         pt_entry_t *ptep, pte;
3085         struct l2_bucket *l2b;
3086         boolean_t flush = FALSE;
3087         pmap_t curpm;
3088         int flags = 0;
3089
3090 #if defined(PMAP_DEBUG)
3091         /*
3092          * XXX This makes pmap_remove_all() illegal for non-managed pages!
3093          */
3094         if (m->flags & PG_FICTITIOUS) {
3095                 panic("pmap_remove_all: illegal for unmanaged page, va: 0x%x", VM_PAGE_TO_PHYS(m));
3096         }
3097 #endif
3098
3099         if (TAILQ_EMPTY(&m->md.pv_list))
3100                 return;
3101         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3102         pmap_remove_write(m);
3103         curpm = vmspace_pmap(curproc->p_vmspace);
3104         while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3105                 if (flush == FALSE && (pv->pv_pmap == curpm ||
3106                     pv->pv_pmap == pmap_kernel()))
3107                         flush = TRUE;
3108                 PMAP_LOCK(pv->pv_pmap);
3109                 l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
3110                 KASSERT(l2b != NULL, ("No l2 bucket"));
3111                 ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
3112                 pte = *ptep;
3113                 *ptep = 0;
3114                 PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
3115                 pmap_free_l2_bucket(pv->pv_pmap, l2b, 1);
3116                 if (pv->pv_flags & PVF_WIRED)
3117                         pv->pv_pmap->pm_stats.wired_count--;
3118                 pv->pv_pmap->pm_stats.resident_count--;
3119                 flags |= pv->pv_flags;
3120                 pmap_nuke_pv(m, pv->pv_pmap, pv);
3121                 PMAP_UNLOCK(pv->pv_pmap);
3122                 pmap_free_pv_entry(pv);
3123         }
3124
3125         if (flush) {
3126                 if (PV_BEEN_EXECD(flags))
3127                         pmap_tlb_flushID(curpm);
3128                 else
3129                         pmap_tlb_flushD(curpm);
3130         }
3131         vm_page_flag_clear(m, PG_WRITEABLE);
3132 }
3133
3134
3135 /*
3136  *      Set the physical protection on the
3137  *      specified range of this map as requested.
3138  */
3139 void
3140 pmap_protect(pmap_t pm, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
3141 {
3142         struct l2_bucket *l2b;
3143         pt_entry_t *ptep, pte;
3144         vm_offset_t next_bucket;
3145         u_int flags;
3146         int flush;
3147
3148         CTR4(KTR_PMAP, "pmap_protect: pmap %p sva 0x%08x eva 0x%08x prot %x",
3149             pm, sva, eva, prot);
3150
3151         if ((prot & VM_PROT_READ) == 0) {
3152                 pmap_remove(pm, sva, eva);
3153                 return;
3154         }
3155
3156         if (prot & VM_PROT_WRITE) {
3157                 /*
3158                  * If this is a read->write transition, just ignore it and let
3159                  * vm_fault() take care of it later.
3160                  */
3161                 return;
3162         }
3163
3164         vm_page_lock_queues();
3165         PMAP_LOCK(pm);
3166
3167         /*
3168          * OK, at this point, we know we're doing write-protect operation.
3169          * If the pmap is active, write-back the range.
3170          */
3171         pmap_dcache_wb_range(pm, sva, eva - sva, FALSE, FALSE);
3172
3173         flush = ((eva - sva) >= (PAGE_SIZE * 4)) ? 0 : -1;
3174         flags = 0;
3175
3176         while (sva < eva) {
3177                 next_bucket = L2_NEXT_BUCKET(sva);
3178                 if (next_bucket > eva)
3179                         next_bucket = eva;
3180
3181                 l2b = pmap_get_l2_bucket(pm, sva);
3182                 if (l2b == NULL) {
3183                         sva = next_bucket;
3184                         continue;
3185                 }
3186
3187                 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3188
3189                 while (sva < next_bucket) {
3190                         if ((pte = *ptep) != 0 && (pte & L2_S_PROT_W) != 0) {
3191                                 struct vm_page *pg;
3192                                 u_int f;
3193
3194                                 pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
3195                                 pte &= ~L2_S_PROT_W;
3196                                 *ptep = pte;
3197                                 PTE_SYNC(ptep);
3198
3199                                 if (pg != NULL) {
3200                                         f = pmap_modify_pv(pg, pm, sva,
3201                                             PVF_WRITE, 0);
3202                                         vm_page_dirty(pg);
3203                                 } else
3204                                         f = PVF_REF | PVF_EXEC;
3205
3206                                 if (flush >= 0) {
3207                                         flush++;
3208                                         flags |= f;
3209                                 } else
3210                                 if (PV_BEEN_EXECD(f))
3211                                         pmap_tlb_flushID_SE(pm, sva);
3212                                 else
3213                                 if (PV_BEEN_REFD(f))
3214                                         pmap_tlb_flushD_SE(pm, sva);
3215                         }
3216
3217                         sva += PAGE_SIZE;
3218                         ptep++;
3219                 }
3220         }
3221
3222
3223         if (flush) {
3224                 if (PV_BEEN_EXECD(flags))
3225                         pmap_tlb_flushID(pm);
3226                 else
3227                 if (PV_BEEN_REFD(flags))
3228                         pmap_tlb_flushD(pm);
3229         }
3230         vm_page_unlock_queues();
3231
3232         PMAP_UNLOCK(pm);
3233 }
3234
3235
3236 /*
3237  *      Insert the given physical page (p) at
3238  *      the specified virtual address (v) in the
3239  *      target physical map with the protection requested.
3240  *
3241  *      If specified, the page will be wired down, meaning
3242  *      that the related pte can not be reclaimed.
3243  *
3244  *      NB:  This is the only routine which MAY NOT lazy-evaluate
3245  *      or lose information.  That is, this routine must actually
3246  *      insert this page into the given map NOW.
3247  */
3248
3249 void
3250 pmap_enter(pmap_t pmap, vm_offset_t va, vm_prot_t access, vm_page_t m,
3251     vm_prot_t prot, boolean_t wired)
3252 {
3253
3254         vm_page_lock_queues();
3255         PMAP_LOCK(pmap);
3256         pmap_enter_locked(pmap, va, m, prot, wired, M_WAITOK);
3257         vm_page_unlock_queues();
3258         PMAP_UNLOCK(pmap);
3259 }
3260
3261 /*
3262  *      The page queues and pmap must be locked.
3263  */
3264 static void
3265 pmap_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3266     boolean_t wired, int flags)
3267 {
3268         struct l2_bucket *l2b = NULL;
3269         struct vm_page *opg;
3270         struct pv_entry *pve = NULL;
3271         pt_entry_t *ptep, npte, opte;
3272         u_int nflags;
3273         u_int oflags;
3274         vm_paddr_t pa;
3275
3276         PMAP_ASSERT_LOCKED(pmap);
3277         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3278         if (va == vector_page) {
3279                 pa = systempage.pv_pa;
3280                 m = NULL;
3281         } else
3282                 pa = VM_PAGE_TO_PHYS(m);
3283         nflags = 0;
3284         if (prot & VM_PROT_WRITE)
3285                 nflags |= PVF_WRITE;
3286         if (prot & VM_PROT_EXECUTE)
3287                 nflags |= PVF_EXEC;
3288         if (wired)
3289                 nflags |= PVF_WIRED;
3290         PDEBUG(1, printf("pmap_enter: pmap = %08x, va = %08x, m = %08x, prot = %x, "
3291             "wired = %x\n", (uint32_t) pmap, va, (uint32_t) m, prot, wired));
3292             
3293         if (pmap == pmap_kernel()) {
3294                 l2b = pmap_get_l2_bucket(pmap, va);
3295                 if (l2b == NULL)
3296                         l2b = pmap_grow_l2_bucket(pmap, va);
3297         } else {
3298 do_l2b_alloc:
3299                 l2b = pmap_alloc_l2_bucket(pmap, va);
3300                 if (l2b == NULL) {
3301                         if (flags & M_WAITOK) {
3302                                 PMAP_UNLOCK(pmap);
3303                                 vm_page_unlock_queues();
3304                                 VM_WAIT;
3305                                 vm_page_lock_queues();
3306                                 PMAP_LOCK(pmap);
3307                                 goto do_l2b_alloc;
3308                         }
3309                         return;
3310                 }
3311         }
3312
3313         ptep = &l2b->l2b_kva[l2pte_index(va)];
3314                     
3315         opte = *ptep;
3316         npte = pa;
3317         oflags = 0;
3318         if (opte) {
3319                 /*
3320                  * There is already a mapping at this address.
3321                  * If the physical address is different, lookup the
3322                  * vm_page.
3323                  */
3324                 if (l2pte_pa(opte) != pa)
3325                         opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3326                 else
3327                         opg = m;
3328         } else
3329                 opg = NULL;
3330
3331         if ((prot & (VM_PROT_ALL)) ||
3332             (!m || m->md.pvh_attrs & PVF_REF)) {
3333                 /*
3334                  * - The access type indicates that we don't need
3335                  *   to do referenced emulation.
3336                  * OR
3337                  * - The physical page has already been referenced
3338                  *   so no need to re-do referenced emulation here.
3339                  */
3340                 npte |= L2_S_PROTO;
3341                 
3342                 nflags |= PVF_REF;
3343                 
3344                 if (m && ((prot & VM_PROT_WRITE) != 0 ||
3345                     (m->md.pvh_attrs & PVF_MOD))) {
3346                         /*
3347                          * This is a writable mapping, and the
3348                          * page's mod state indicates it has
3349                          * already been modified. Make it
3350                          * writable from the outset.
3351                          */
3352                         nflags |= PVF_MOD;
3353                         if (!(m->md.pvh_attrs & PVF_MOD))
3354                                 vm_page_dirty(m);
3355                 }
3356                 if (m && opte)
3357                         vm_page_flag_set(m, PG_REFERENCED);
3358         } else {
3359                 /*
3360                  * Need to do page referenced emulation.
3361                  */
3362                 npte |= L2_TYPE_INV;
3363         }
3364         
3365         if (prot & VM_PROT_WRITE) {
3366                 npte |= L2_S_PROT_W;
3367                 if (m != NULL)
3368                         vm_page_flag_set(m, PG_WRITEABLE);
3369         }
3370         npte |= pte_l2_s_cache_mode;
3371         if (m && m == opg) {
3372                 /*
3373                  * We're changing the attrs of an existing mapping.
3374                  */
3375                 oflags = pmap_modify_pv(m, pmap, va,
3376                     PVF_WRITE | PVF_EXEC | PVF_WIRED |
3377                     PVF_MOD | PVF_REF, nflags);
3378                 
3379                 /*
3380                  * We may need to flush the cache if we're
3381                  * doing rw-ro...
3382                  */
3383                 if (pmap_is_current(pmap) &&
3384                     (oflags & PVF_NC) == 0 &&
3385                     (opte & L2_S_PROT_W) != 0 &&
3386                     (prot & VM_PROT_WRITE) == 0) {
3387                         cpu_dcache_wb_range(va, PAGE_SIZE);
3388                         pmap_l2cache_wb_range(pmap, va, PAGE_SIZE);
3389                 }
3390         } else {
3391                 /*
3392                  * New mapping, or changing the backing page
3393                  * of an existing mapping.
3394                  */
3395                 if (opg) {
3396                         /*
3397                          * Replacing an existing mapping with a new one.
3398                          * It is part of our managed memory so we
3399                          * must remove it from the PV list
3400                          */
3401                         pve = pmap_remove_pv(opg, pmap, va);
3402                         if (m && (m->flags & (PG_UNMANAGED | PG_FICTITIOUS)) &&
3403                             pve)
3404                                 pmap_free_pv_entry(pve);
3405                         else if (!pve && 
3406                             !(m->flags & (PG_UNMANAGED | PG_FICTITIOUS)))
3407                                 pve = pmap_get_pv_entry();
3408                         KASSERT(pve != NULL || m->flags & (PG_UNMANAGED | 
3409                             PG_FICTITIOUS), ("No pv"));
3410                         oflags = pve->pv_flags;
3411                         
3412                         /*
3413                          * If the old mapping was valid (ref/mod
3414                          * emulation creates 'invalid' mappings
3415                          * initially) then make sure to frob
3416                          * the cache.
3417                          */
3418                         if ((oflags & PVF_NC) == 0 &&
3419                             l2pte_valid(opte)) {
3420                                 if (PV_BEEN_EXECD(oflags)) {
3421                                         pmap_idcache_wbinv_range(pmap, va,
3422                                             PAGE_SIZE);
3423                                 } else
3424                                         if (PV_BEEN_REFD(oflags)) {
3425                                                 pmap_dcache_wb_range(pmap, va,
3426                                                     PAGE_SIZE, TRUE,
3427                                                     (oflags & PVF_WRITE) == 0);
3428                                         }
3429                         }
3430                 } else if (m && !(m->flags & (PG_UNMANAGED | PG_FICTITIOUS)))
3431                         if ((pve = pmap_get_pv_entry()) == NULL) {
3432                                 panic("pmap_enter: no pv entries");     
3433                         }
3434                 if (m && !(m->flags & (PG_UNMANAGED | PG_FICTITIOUS))) {
3435                         KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
3436                             ("pmap_enter: managed mapping within the clean submap"));
3437                         pmap_enter_pv(m, pve, pmap, va, nflags);
3438                 }
3439         }
3440         /*
3441          * Make sure userland mappings get the right permissions
3442          */
3443         if (pmap != pmap_kernel() && va != vector_page) {
3444                 npte |= L2_S_PROT_U;
3445         }
3446
3447         /*
3448          * Keep the stats up to date
3449          */
3450         if (opte == 0) {
3451                 l2b->l2b_occupancy++;
3452                 pmap->pm_stats.resident_count++;
3453         } 
3454
3455
3456         /*
3457          * If this is just a wiring change, the two PTEs will be
3458          * identical, so there's no need to update the page table.
3459          */
3460         if (npte != opte) {
3461                 boolean_t is_cached = pmap_is_current(pmap);
3462
3463                 *ptep = npte;
3464                 if (is_cached) {
3465                         /*
3466                          * We only need to frob the cache/tlb if this pmap
3467                          * is current
3468                          */
3469                         PTE_SYNC(ptep);
3470                         if (L1_IDX(va) != L1_IDX(vector_page) && 
3471                             l2pte_valid(npte)) {
3472                                 /*
3473                                  * This mapping is likely to be accessed as
3474                                  * soon as we return to userland. Fix up the
3475                                  * L1 entry to avoid taking another
3476                                  * page/domain fault.
3477                                  */
3478                                 pd_entry_t *pl1pd, l1pd;
3479
3480                                 pl1pd = &pmap->pm_l1->l1_kva[L1_IDX(va)];
3481                                 l1pd = l2b->l2b_phys | L1_C_DOM(pmap->pm_domain) |
3482                                     L1_C_PROTO;
3483                                 if (*pl1pd != l1pd) {
3484                                         *pl1pd = l1pd;
3485                                         PTE_SYNC(pl1pd);
3486                                 }
3487                         }
3488                 }
3489
3490                 if (PV_BEEN_EXECD(oflags))
3491                         pmap_tlb_flushID_SE(pmap, va);
3492                 else if (PV_BEEN_REFD(oflags))
3493                         pmap_tlb_flushD_SE(pmap, va);
3494
3495
3496                 if (m)
3497                         pmap_fix_cache(m, pmap, va);
3498         }
3499 }
3500
3501 /*
3502  * Maps a sequence of resident pages belonging to the same object.
3503  * The sequence begins with the given page m_start.  This page is
3504  * mapped at the given virtual address start.  Each subsequent page is
3505  * mapped at a virtual address that is offset from start by the same
3506  * amount as the page is offset from m_start within the object.  The
3507  * last page in the sequence is the page with the largest offset from
3508  * m_start that can be mapped at a virtual address less than the given
3509  * virtual address end.  Not every virtual page between start and end
3510  * is mapped; only those for which a resident page exists with the
3511  * corresponding offset from m_start are mapped.
3512  */
3513 void
3514 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
3515     vm_page_t m_start, vm_prot_t prot)
3516 {
3517         vm_page_t m;
3518         vm_pindex_t diff, psize;
3519
3520         psize = atop(end - start);
3521         m = m_start;
3522         PMAP_LOCK(pmap);
3523         while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
3524                 pmap_enter_locked(pmap, start + ptoa(diff), m, prot &
3525                     (VM_PROT_READ | VM_PROT_EXECUTE), FALSE, M_NOWAIT);
3526                 m = TAILQ_NEXT(m, listq);
3527         }
3528         PMAP_UNLOCK(pmap);
3529 }
3530
3531 /*
3532  * this code makes some *MAJOR* assumptions:
3533  * 1. Current pmap & pmap exists.
3534  * 2. Not wired.
3535  * 3. Read access.
3536  * 4. No page table pages.
3537  * but is *MUCH* faster than pmap_enter...
3538  */
3539
3540 void
3541 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3542 {
3543
3544         PMAP_LOCK(pmap);
3545         pmap_enter_locked(pmap, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
3546             FALSE, M_NOWAIT);
3547         PMAP_UNLOCK(pmap);
3548 }
3549
3550 /*
3551  *      Routine:        pmap_change_wiring
3552  *      Function:       Change the wiring attribute for a map/virtual-address
3553  *                      pair.
3554  *      In/out conditions:
3555  *                      The mapping must already exist in the pmap.
3556  */
3557 void
3558 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3559 {
3560         struct l2_bucket *l2b;
3561         pt_entry_t *ptep, pte;
3562         vm_page_t pg;
3563
3564         vm_page_lock_queues();
3565         PMAP_LOCK(pmap);
3566         l2b = pmap_get_l2_bucket(pmap, va);
3567         KASSERT(l2b, ("No l2b bucket in pmap_change_wiring"));
3568         ptep = &l2b->l2b_kva[l2pte_index(va)];
3569         pte = *ptep;
3570         pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
3571         if (pg) 
3572                 pmap_modify_pv(pg, pmap, va, PVF_WIRED, wired);
3573         vm_page_unlock_queues();
3574         PMAP_UNLOCK(pmap);
3575 }
3576
3577
3578 /*
3579  *      Copy the range specified by src_addr/len
3580  *      from the source map to the range dst_addr/len
3581  *      in the destination map.
3582  *
3583  *      This routine is only advisory and need not do anything.
3584  */
3585 void
3586 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
3587     vm_size_t len, vm_offset_t src_addr)
3588 {
3589 }
3590
3591
3592 /*
3593  *      Routine:        pmap_extract
3594  *      Function:
3595  *              Extract the physical page address associated
3596  *              with the given map/virtual_address pair.
3597  */
3598 vm_paddr_t
3599 pmap_extract(pmap_t pm, vm_offset_t va)
3600 {
3601         struct l2_dtable *l2;
3602         pd_entry_t l1pd;
3603         pt_entry_t *ptep, pte;
3604         vm_paddr_t pa;
3605         u_int l1idx;
3606         l1idx = L1_IDX(va);
3607
3608         PMAP_LOCK(pm);
3609         l1pd = pm->pm_l1->l1_kva[l1idx];
3610         if (l1pte_section_p(l1pd)) {
3611                 /*
3612                  * These should only happen for pmap_kernel()
3613                  */
3614                 KASSERT(pm == pmap_kernel(), ("huh"));
3615                 /* XXX: what to do about the bits > 32 ? */
3616                 if (l1pd & L1_S_SUPERSEC) 
3617                         pa = (l1pd & L1_SUP_FRAME) | (va & L1_SUP_OFFSET);
3618                 else
3619                         pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3620         } else {
3621                 /*
3622                  * Note that we can't rely on the validity of the L1
3623                  * descriptor as an indication that a mapping exists.
3624                  * We have to look it up in the L2 dtable.
3625                  */
3626                 l2 = pm->pm_l2[L2_IDX(l1idx)];
3627
3628                 if (l2 == NULL ||
3629                     (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3630                         PMAP_UNLOCK(pm);
3631                         return (0);
3632                 }
3633
3634                 ptep = &ptep[l2pte_index(va)];
3635                 pte = *ptep;
3636
3637                 if (pte == 0) {
3638                         PMAP_UNLOCK(pm);
3639                         return (0);
3640                 }
3641
3642                 switch (pte & L2_TYPE_MASK) {
3643                 case L2_TYPE_L:
3644                         pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3645                         break;
3646
3647                 default:
3648                         pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3649                         break;
3650                 }
3651         }
3652
3653         PMAP_UNLOCK(pm);
3654         return (pa);
3655 }
3656
3657 /*
3658  * Atomically extract and hold the physical page with the given
3659  * pmap and virtual address pair if that mapping permits the given
3660  * protection.
3661  *
3662  */
3663 vm_page_t
3664 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
3665 {
3666         struct l2_dtable *l2;
3667         pd_entry_t l1pd;
3668         pt_entry_t *ptep, pte;
3669         vm_paddr_t pa;
3670         vm_page_t m = NULL;
3671         u_int l1idx;
3672         l1idx = L1_IDX(va);
3673
3674         vm_page_lock_queues();
3675         PMAP_LOCK(pmap);
3676         l1pd = pmap->pm_l1->l1_kva[l1idx];
3677         if (l1pte_section_p(l1pd)) {
3678                 /*
3679                  * These should only happen for pmap_kernel()
3680                  */
3681                 KASSERT(pmap == pmap_kernel(), ("huh"));
3682                 /* XXX: what to do about the bits > 32 ? */
3683                 if (l1pd & L1_S_SUPERSEC) 
3684                         pa = (l1pd & L1_SUP_FRAME) | (va & L1_SUP_OFFSET);
3685                 else
3686                         pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3687                 if (l1pd & L1_S_PROT_W || (prot & VM_PROT_WRITE) == 0) {
3688                         m = PHYS_TO_VM_PAGE(pa);
3689                         vm_page_hold(m);
3690                 }
3691                         
3692         } else {
3693                 /*
3694                  * Note that we can't rely on the validity of the L1
3695                  * descriptor as an indication that a mapping exists.
3696                  * We have to look it up in the L2 dtable.
3697                  */
3698                 l2 = pmap->pm_l2[L2_IDX(l1idx)];
3699
3700                 if (l2 == NULL ||
3701                     (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3702                         PMAP_UNLOCK(pmap);
3703                         vm_page_unlock_queues();
3704                         return (NULL);
3705                 }
3706
3707                 ptep = &ptep[l2pte_index(va)];
3708                 pte = *ptep;
3709
3710                 if (pte == 0) {
3711                         PMAP_UNLOCK(pmap);
3712                         vm_page_unlock_queues();
3713                         return (NULL);
3714                 }
3715                 if (pte & L2_S_PROT_W || (prot & VM_PROT_WRITE) == 0) {
3716                         switch (pte & L2_TYPE_MASK) {
3717                         case L2_TYPE_L:
3718                                 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3719                                 break;
3720                                 
3721                         default:
3722                                 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3723                                 break;
3724                         }
3725                         m = PHYS_TO_VM_PAGE(pa);
3726                         vm_page_hold(m);
3727                 }
3728         }
3729
3730         PMAP_UNLOCK(pmap);
3731         vm_page_unlock_queues();
3732         return (m);
3733 }
3734
3735 /*
3736  * Initialize a preallocated and zeroed pmap structure,
3737  * such as one in a vmspace structure.
3738  */
3739
3740 int
3741 pmap_pinit(pmap_t pmap)
3742 {
3743         PDEBUG(1, printf("pmap_pinit: pmap = %08x\n", (uint32_t) pmap));
3744         
3745         PMAP_LOCK_INIT(pmap);
3746         pmap_alloc_l1(pmap);
3747         bzero(pmap->pm_l2, sizeof(pmap->pm_l2));
3748
3749         pmap->pm_active = 0;
3750                 
3751         TAILQ_INIT(&pmap->pm_pvlist);
3752         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
3753         pmap->pm_stats.resident_count = 1;
3754         if (vector_page < KERNBASE) {
3755                 pmap_enter(pmap, vector_page,
3756                     VM_PROT_READ, PHYS_TO_VM_PAGE(systempage.pv_pa),
3757                     VM_PROT_READ, 1);
3758         } 
3759         return (1);
3760 }
3761
3762
3763 /***************************************************
3764  * page management routines.
3765  ***************************************************/
3766
3767
3768 static void
3769 pmap_free_pv_entry(pv_entry_t pv)
3770 {
3771         pv_entry_count--;
3772         uma_zfree(pvzone, pv);
3773 }
3774
3775
3776 /*
3777  * get a new pv_entry, allocating a block from the system
3778  * when needed.
3779  * the memory allocation is performed bypassing the malloc code
3780  * because of the possibility of allocations at interrupt time.
3781  */
3782 static pv_entry_t
3783 pmap_get_pv_entry(void)
3784 {
3785         pv_entry_t ret_value;
3786         
3787         pv_entry_count++;
3788         if (pv_entry_count > pv_entry_high_water)
3789                 pagedaemon_wakeup();
3790         ret_value = uma_zalloc(pvzone, M_NOWAIT);
3791         return ret_value;
3792 }
3793
3794 /*
3795  *      Remove the given range of addresses from the specified map.
3796  *
3797  *      It is assumed that the start and end are properly
3798  *      rounded to the page size.
3799  */
3800 #define PMAP_REMOVE_CLEAN_LIST_SIZE     3
3801 void
3802 pmap_remove(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
3803 {
3804         struct l2_bucket *l2b;
3805         vm_offset_t next_bucket;
3806         pt_entry_t *ptep;
3807         u_int total;
3808         u_int mappings, is_exec, is_refd;
3809         int flushall = 0;
3810
3811
3812         /*
3813          * we lock in the pmap => pv_head direction
3814          */
3815
3816         vm_page_lock_queues();
3817         PMAP_LOCK(pm);
3818         total = 0;
3819         while (sva < eva) {
3820                 /*
3821                  * Do one L2 bucket's worth at a time.
3822                  */
3823                 next_bucket = L2_NEXT_BUCKET(sva);
3824                 if (next_bucket > eva)
3825                         next_bucket = eva;
3826
3827                 l2b = pmap_get_l2_bucket(pm, sva);
3828                 if (l2b == NULL) {
3829                         sva = next_bucket;
3830                         continue;
3831                 }
3832
3833                 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3834                 mappings = 0;
3835
3836                 while (sva < next_bucket) {
3837                         struct vm_page *pg;
3838                         pt_entry_t pte;
3839                         vm_paddr_t pa;
3840
3841                         pte = *ptep;
3842
3843                         if (pte == 0) {
3844                                 /*
3845                                  * Nothing here, move along
3846                                  */
3847                                 sva += PAGE_SIZE;
3848                                 ptep++;
3849                                 continue;
3850                         }
3851
3852                         pm->pm_stats.resident_count--;
3853                         pa = l2pte_pa(pte);
3854                         is_exec = 0;
3855                         is_refd = 1;
3856
3857                         /*
3858                          * Update flags. In a number of circumstances,
3859                          * we could cluster a lot of these and do a
3860                          * number of sequential pages in one go.
3861                          */
3862                         if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
3863                                 struct pv_entry *pve;
3864
3865                                 pve = pmap_remove_pv(pg, pm, sva);
3866                                 if (pve) {
3867                                         is_exec = PV_BEEN_EXECD(pve->pv_flags);
3868                                         is_refd = PV_BEEN_REFD(pve->pv_flags);
3869                                         pmap_free_pv_entry(pve);
3870                                 }
3871                         }
3872
3873                         if (l2pte_valid(pte) && pmap_is_current(pm)) {
3874                                 if (total < PMAP_REMOVE_CLEAN_LIST_SIZE) {
3875                                         total++;
3876                                         if (is_exec) {
3877                                                 cpu_idcache_wbinv_range(sva,
3878                                                     PAGE_SIZE);
3879                                                 cpu_l2cache_wbinv_range(sva,
3880                                                     PAGE_SIZE);
3881                                                 cpu_tlb_flushID_SE(sva);
3882                                         } else if (is_refd) {
3883                                                 cpu_dcache_wbinv_range(sva,
3884                                                     PAGE_SIZE);
3885                                                 cpu_l2cache_wbinv_range(sva,
3886                                                     PAGE_SIZE);
3887                                                 cpu_tlb_flushD_SE(sva);
3888                                         }
3889                                 } else if (total == PMAP_REMOVE_CLEAN_LIST_SIZE) {
3890                                         /* flushall will also only get set for
3891                                          * for a current pmap
3892                                          */
3893                                         cpu_idcache_wbinv_all();
3894                                         cpu_l2cache_wbinv_all();
3895                                         flushall = 1;
3896                                         total++;
3897                                 }
3898                         }
3899                         *ptep = 0;
3900                         PTE_SYNC(ptep);
3901
3902                         sva += PAGE_SIZE;
3903                         ptep++;
3904                         mappings++;
3905                 }
3906
3907                 pmap_free_l2_bucket(pm, l2b, mappings);
3908         }
3909
3910         vm_page_unlock_queues();
3911         if (flushall)
3912                 cpu_tlb_flushID();
3913         PMAP_UNLOCK(pm);
3914 }
3915
3916 /*
3917  * pmap_zero_page()
3918  * 
3919  * Zero a given physical page by mapping it at a page hook point.
3920  * In doing the zero page op, the page we zero is mapped cachable, as with
3921  * StrongARM accesses to non-cached pages are non-burst making writing
3922  * _any_ bulk data very slow.
3923  */
3924 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 || defined(CPU_XSCALE_CORE3)
3925 void
3926 pmap_zero_page_generic(vm_paddr_t phys, int off, int size)
3927 {
3928 #ifdef ARM_USE_SMALL_ALLOC
3929         char *dstpg;
3930 #endif
3931
3932 #ifdef DEBUG
3933         struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
3934
3935         if (pg->md.pvh_list != NULL)
3936                 panic("pmap_zero_page: page has mappings");
3937 #endif
3938
3939         if (_arm_bzero && size >= _min_bzero_size &&
3940             _arm_bzero((void *)(phys + off), size, IS_PHYSICAL) == 0)
3941                 return;
3942
3943 #ifdef ARM_USE_SMALL_ALLOC
3944         dstpg = (char *)arm_ptovirt(phys);
3945         if (off || size != PAGE_SIZE) {
3946                 bzero(dstpg + off, size);
3947                 cpu_dcache_wbinv_range((vm_offset_t)(dstpg + off), size);
3948                 cpu_l2cache_wbinv_range((vm_offset_t)(dstpg + off), size);
3949         } else {
3950                 bzero_page((vm_offset_t)dstpg);
3951                 cpu_dcache_wbinv_range((vm_offset_t)dstpg, PAGE_SIZE);
3952                 cpu_l2cache_wbinv_range((vm_offset_t)dstpg, PAGE_SIZE);
3953         }
3954 #else
3955
3956         mtx_lock(&cmtx);
3957         /*
3958          * Hook in the page, zero it, invalidate the TLB as needed.
3959          *
3960          * Note the temporary zero-page mapping must be a non-cached page in
3961          * ordert to work without corruption when write-allocate is enabled.
3962          */
3963         *cdst_pte = L2_S_PROTO | phys | L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE);
3964         cpu_tlb_flushD_SE(cdstp);
3965         cpu_cpwait();
3966         if (off || size != PAGE_SIZE)
3967                 bzero((void *)(cdstp + off), size);
3968         else
3969                 bzero_page(cdstp);
3970
3971         mtx_unlock(&cmtx);
3972 #endif
3973 }
3974 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
3975
3976 #if ARM_MMU_XSCALE == 1
3977 void
3978 pmap_zero_page_xscale(vm_paddr_t phys, int off, int size)
3979 {
3980 #ifdef ARM_USE_SMALL_ALLOC
3981         char *dstpg;
3982 #endif
3983
3984         if (_arm_bzero && size >= _min_bzero_size &&
3985             _arm_bzero((void *)(phys + off), size, IS_PHYSICAL) == 0)
3986                 return;
3987 #ifdef ARM_USE_SMALL_ALLOC
3988         dstpg = (char *)arm_ptovirt(phys);
3989         if (off || size != PAGE_SIZE) {
3990                 bzero(dstpg + off, size);
3991                 cpu_dcache_wbinv_range((vm_offset_t)(dstpg + off), size);
3992         } else {
3993                 bzero_page((vm_offset_t)dstpg);
3994                 cpu_dcache_wbinv_range((vm_offset_t)dstpg, PAGE_SIZE);
3995         }
3996 #else
3997         mtx_lock(&cmtx);
3998         /*
3999          * Hook in the page, zero it, and purge the cache for that
4000          * zeroed page. Invalidate the TLB as needed.
4001          */
4002         *cdst_pte = L2_S_PROTO | phys |
4003             L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
4004             L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);       /* mini-data */
4005         PTE_SYNC(cdst_pte);
4006         cpu_tlb_flushD_SE(cdstp);
4007         cpu_cpwait();
4008         if (off || size != PAGE_SIZE)
4009                 bzero((void *)(cdstp + off), size);
4010         else
4011                 bzero_page(cdstp);
4012         mtx_unlock(&cmtx);
4013         xscale_cache_clean_minidata();
4014 #endif
4015 }
4016
4017 /*
4018  * Change the PTEs for the specified kernel mappings such that they
4019  * will use the mini data cache instead of the main data cache.
4020  */
4021 void
4022 pmap_use_minicache(vm_offset_t va, vm_size_t size)
4023 {
4024         struct l2_bucket *l2b;
4025         pt_entry_t *ptep, *sptep, pte;
4026         vm_offset_t next_bucket, eva;
4027
4028 #if (ARM_NMMUS > 1) || defined(CPU_XSCALE_CORE3)
4029         if (xscale_use_minidata == 0)
4030                 return;
4031 #endif
4032
4033         eva = va + size;
4034
4035         while (va < eva) {
4036                 next_bucket = L2_NEXT_BUCKET(va);
4037                 if (next_bucket > eva)
4038                         next_bucket = eva;
4039
4040                 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
4041
4042                 sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
4043
4044                 while (va < next_bucket) {
4045                         pte = *ptep;
4046                         if (!l2pte_minidata(pte)) {
4047                                 cpu_dcache_wbinv_range(va, PAGE_SIZE);
4048                                 cpu_tlb_flushD_SE(va);
4049                                 *ptep = pte & ~L2_B;
4050                         }
4051                         ptep++;
4052                         va += PAGE_SIZE;
4053                 }
4054                 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
4055         }
4056         cpu_cpwait();
4057 }
4058 #endif /* ARM_MMU_XSCALE == 1 */
4059
4060 /*
4061  *      pmap_zero_page zeros the specified hardware page by mapping 
4062  *      the page into KVM and using bzero to clear its contents.
4063  */
4064 void
4065 pmap_zero_page(vm_page_t m)
4066 {
4067         pmap_zero_page_func(VM_PAGE_TO_PHYS(m), 0, PAGE_SIZE);
4068 }
4069
4070
4071 /*
4072  *      pmap_zero_page_area zeros the specified hardware page by mapping 
4073  *      the page into KVM and using bzero to clear its contents.
4074  *
4075  *      off and size may not cover an area beyond a single hardware page.
4076  */
4077 void
4078 pmap_zero_page_area(vm_page_t m, int off, int size)
4079 {
4080
4081         pmap_zero_page_func(VM_PAGE_TO_PHYS(m), off, size);
4082 }
4083
4084
4085 /*
4086  *      pmap_zero_page_idle zeros the specified hardware page by mapping 
4087  *      the page into KVM and using bzero to clear its contents.  This
4088  *      is intended to be called from the vm_pagezero process only and
4089  *      outside of Giant.
4090  */
4091 void
4092 pmap_zero_page_idle(vm_page_t m)
4093 {
4094
4095         pmap_zero_page(m);
4096 }
4097
4098 #if 0
4099 /*
4100  * pmap_clean_page()
4101  *
4102  * This is a local function used to work out the best strategy to clean
4103  * a single page referenced by its entry in the PV table. It's used by
4104  * pmap_copy_page, pmap_zero page and maybe some others later on.
4105  *
4106  * Its policy is effectively:
4107  *  o If there are no mappings, we don't bother doing anything with the cache.
4108  *  o If there is one mapping, we clean just that page.
4109  *  o If there are multiple mappings, we clean the entire cache.
4110  *
4111  * So that some functions can be further optimised, it returns 0 if it didn't
4112  * clean the entire cache, or 1 if it did.
4113  *
4114  * XXX One bug in this routine is that if the pv_entry has a single page
4115  * mapped at 0x00000000 a whole cache clean will be performed rather than
4116  * just the 1 page. Since this should not occur in everyday use and if it does
4117  * it will just result in not the most efficient clean for the page.
4118  */
4119 static int
4120 pmap_clean_page(struct pv_entry *pv, boolean_t is_src)
4121 {
4122         pmap_t pm, pm_to_clean = NULL;
4123         struct pv_entry *npv;
4124         u_int cache_needs_cleaning = 0;
4125         u_int flags = 0;
4126         vm_offset_t page_to_clean = 0;
4127
4128         if (pv == NULL) {
4129                 /* nothing mapped in so nothing to flush */
4130                 return (0);
4131         }
4132
4133         /*
4134          * Since we flush the cache each time we change to a different
4135          * user vmspace, we only need to flush the page if it is in the
4136          * current pmap.
4137          */
4138         if (curthread)
4139                 pm = vmspace_pmap(curproc->p_vmspace);
4140         else
4141                 pm = pmap_kernel();
4142
4143         for (npv = pv; npv; npv = TAILQ_NEXT(npv, pv_list)) {
4144                 if (npv->pv_pmap == pmap_kernel() || npv->pv_pmap == pm) {
4145                         flags |= npv->pv_flags;
4146                         /*
4147                          * The page is mapped non-cacheable in 
4148                          * this map.  No need to flush the cache.
4149                          */
4150                         if (npv->pv_flags & PVF_NC) {
4151 #ifdef DIAGNOSTIC
4152                                 if (cache_needs_cleaning)
4153                                         panic("pmap_clean_page: "
4154                                             "cache inconsistency");
4155 #endif
4156                                 break;
4157                         } else if (is_src && (npv->pv_flags & PVF_WRITE) == 0)
4158                                 continue;
4159                         if (cache_needs_cleaning) {
4160                                 page_to_clean = 0;
4161                                 break;
4162                         } else {
4163                                 page_to_clean = npv->pv_va;
4164                                 pm_to_clean = npv->pv_pmap;
4165                         }
4166                         cache_needs_cleaning = 1;
4167                 }
4168         }
4169         if (page_to_clean) {
4170                 if (PV_BEEN_EXECD(flags))
4171                         pmap_idcache_wbinv_range(pm_to_clean, page_to_clean,
4172                             PAGE_SIZE);
4173                 else
4174                         pmap_dcache_wb_range(pm_to_clean, page_to_clean,
4175                             PAGE_SIZE, !is_src, (flags & PVF_WRITE) == 0);
4176         } else if (cache_needs_cleaning) {
4177                 if (PV_BEEN_EXECD(flags))
4178                         pmap_idcache_wbinv_all(pm);
4179                 else
4180                         pmap_dcache_wbinv_all(pm);
4181                 return (1);
4182         }
4183         return (0);
4184 }
4185 #endif
4186
4187 /*
4188  *      pmap_copy_page copies the specified (machine independent)
4189  *      page by mapping the page into virtual memory and using
4190  *      bcopy to copy the page, one machine dependent page at a
4191  *      time.
4192  */
4193
4194 /*
4195  * pmap_copy_page()
4196  *
4197  * Copy one physical page into another, by mapping the pages into
4198  * hook points. The same comment regarding cachability as in
4199  * pmap_zero_page also applies here.
4200  */
4201 #if  (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 || defined (CPU_XSCALE_CORE3)
4202 void
4203 pmap_copy_page_generic(vm_paddr_t src, vm_paddr_t dst)
4204 {
4205 #if 0
4206         struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
4207 #endif
4208 #ifdef DEBUG
4209         struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
4210
4211         if (dst_pg->md.pvh_list != NULL)
4212                 panic("pmap_copy_page: dst page has mappings");
4213 #endif
4214
4215
4216         /*
4217          * Clean the source page.  Hold the source page's lock for
4218          * the duration of the copy so that no other mappings can
4219          * be created while we have a potentially aliased mapping.
4220          */
4221 #if 0
4222         /*
4223          * XXX: Not needed while we call cpu_dcache_wbinv_all() in
4224          * pmap_copy_page().
4225          */
4226         (void) pmap_clean_page(TAILQ_FIRST(&src_pg->md.pv_list), TRUE);
4227 #endif
4228         /*
4229          * Map the pages into the page hook points, copy them, and purge
4230          * the cache for the appropriate page. Invalidate the TLB
4231          * as required.
4232          */
4233         mtx_lock(&cmtx);
4234         *csrc_pte = L2_S_PROTO | src |
4235             L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
4236         PTE_SYNC(csrc_pte);
4237         *cdst_pte = L2_S_PROTO | dst |
4238             L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
4239         PTE_SYNC(cdst_pte);
4240         cpu_tlb_flushD_SE(csrcp);
4241         cpu_tlb_flushD_SE(cdstp);
4242         cpu_cpwait();
4243         bcopy_page(csrcp, cdstp);
4244         mtx_unlock(&cmtx);
4245         cpu_dcache_inv_range(csrcp, PAGE_SIZE);
4246         cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
4247         cpu_l2cache_inv_range(csrcp, PAGE_SIZE);
4248         cpu_l2cache_wbinv_range(cdstp, PAGE_SIZE);
4249 }
4250 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
4251
4252 #if ARM_MMU_XSCALE == 1
4253 void
4254 pmap_copy_page_xscale(vm_paddr_t src, vm_paddr_t dst)
4255 {
4256 #if 0
4257         /* XXX: Only needed for pmap_clean_page(), which is commented out. */
4258         struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
4259 #endif
4260 #ifdef DEBUG
4261         struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
4262
4263         if (dst_pg->md.pvh_list != NULL)
4264                 panic("pmap_copy_page: dst page has mappings");
4265 #endif
4266
4267
4268         /*
4269          * Clean the source page.  Hold the source page's lock for
4270          * the duration of the copy so that no other mappings can
4271          * be created while we have a potentially aliased mapping.
4272          */
4273 #if 0
4274         /*
4275          * XXX: Not needed while we call cpu_dcache_wbinv_all() in
4276          * pmap_copy_page().
4277          */
4278         (void) pmap_clean_page(TAILQ_FIRST(&src_pg->md.pv_list), TRUE);
4279 #endif
4280         /*
4281          * Map the pages into the page hook points, copy them, and purge
4282          * the cache for the appropriate page. Invalidate the TLB
4283          * as required.
4284          */
4285         mtx_lock(&cmtx);
4286         *csrc_pte = L2_S_PROTO | src |
4287             L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
4288             L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);       /* mini-data */
4289         PTE_SYNC(csrc_pte);
4290         *cdst_pte = L2_S_PROTO | dst |
4291             L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
4292             L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);       /* mini-data */
4293         PTE_SYNC(cdst_pte);
4294         cpu_tlb_flushD_SE(csrcp);
4295         cpu_tlb_flushD_SE(cdstp);
4296         cpu_cpwait();
4297         bcopy_page(csrcp, cdstp);
4298         mtx_unlock(&cmtx);
4299         xscale_cache_clean_minidata();
4300 }
4301 #endif /* ARM_MMU_XSCALE == 1 */
4302
4303 void
4304 pmap_copy_page(vm_page_t src, vm_page_t dst)
4305 {
4306 #ifdef ARM_USE_SMALL_ALLOC
4307         vm_offset_t srcpg, dstpg;
4308 #endif
4309
4310         cpu_dcache_wbinv_all();
4311         cpu_l2cache_wbinv_all();
4312         if (_arm_memcpy && PAGE_SIZE >= _min_memcpy_size &&
4313             _arm_memcpy((void *)VM_PAGE_TO_PHYS(dst), 
4314             (void *)VM_PAGE_TO_PHYS(src), PAGE_SIZE, IS_PHYSICAL) == 0)
4315                 return;
4316 #ifdef ARM_USE_SMALL_ALLOC
4317         srcpg = arm_ptovirt(VM_PAGE_TO_PHYS(src));
4318         dstpg = arm_ptovirt(VM_PAGE_TO_PHYS(dst));
4319         bcopy_page(srcpg, dstpg);
4320         cpu_dcache_wbinv_range(dstpg, PAGE_SIZE);
4321         cpu_l2cache_wbinv_range(dstpg, PAGE_SIZE);
4322 #else
4323         pmap_copy_page_func(VM_PAGE_TO_PHYS(src), VM_PAGE_TO_PHYS(dst));
4324 #endif
4325 }
4326
4327
4328
4329
4330 /*
4331  * this routine returns true if a physical page resides
4332  * in the given pmap.
4333  */
4334 boolean_t
4335 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
4336 {
4337         pv_entry_t pv;
4338         int loops = 0;
4339         
4340         if (m->flags & PG_FICTITIOUS)
4341                 return (FALSE);
4342                 
4343         /*
4344          * Not found, check current mappings returning immediately
4345          */
4346         for (pv = TAILQ_FIRST(&m->md.pv_list);
4347             pv;
4348             pv = TAILQ_NEXT(pv, pv_list)) {
4349                 if (pv->pv_pmap == pmap) {
4350                         return (TRUE);
4351                 }
4352                 loops++;
4353                 if (loops >= 16)
4354                         break;
4355         }
4356         return (FALSE);
4357 }
4358
4359 /*
4360  *      pmap_page_wired_mappings:
4361  *
4362  *      Return the number of managed mappings to the given physical page
4363  *      that are wired.
4364  */
4365 int
4366 pmap_page_wired_mappings(vm_page_t m)
4367 {
4368         pv_entry_t pv;
4369         int count;
4370
4371         count = 0;
4372         if ((m->flags & PG_FICTITIOUS) != 0)
4373                 return (count);
4374         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4375         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
4376                 if ((pv->pv_flags & PVF_WIRED) != 0)
4377                         count++;
4378         return (count);
4379 }
4380
4381 /*
4382  *      pmap_ts_referenced:
4383  *
4384  *      Return the count of reference bits for a page, clearing all of them.
4385  */
4386 int
4387 pmap_ts_referenced(vm_page_t m)
4388 {
4389
4390         if (m->flags & PG_FICTITIOUS)
4391                 return (0);
4392         return (pmap_clearbit(m, PVF_REF));
4393 }
4394
4395
4396 boolean_t
4397 pmap_is_modified(vm_page_t m)
4398 {
4399
4400         if (m->md.pvh_attrs & PVF_MOD)
4401                 return (TRUE);
4402         
4403         return(FALSE);
4404 }
4405
4406
4407 /*
4408  *      Clear the modify bits on the specified physical page.
4409  */
4410 void
4411 pmap_clear_modify(vm_page_t m)
4412 {
4413
4414         if (m->md.pvh_attrs & PVF_MOD)
4415                 pmap_clearbit(m, PVF_MOD);
4416 }
4417
4418
4419 /*
4420  *      pmap_clear_reference:
4421  *
4422  *      Clear the reference bit on the specified physical page.
4423  */
4424 void
4425 pmap_clear_reference(vm_page_t m)
4426 {
4427
4428         if (m->md.pvh_attrs & PVF_REF) 
4429                 pmap_clearbit(m, PVF_REF);
4430 }
4431
4432
4433 /*
4434  * Clear the write and modified bits in each of the given page's mappings.
4435  */
4436 void
4437 pmap_remove_write(vm_page_t m)
4438 {
4439
4440         if (m->flags & PG_WRITEABLE)
4441                 pmap_clearbit(m, PVF_WRITE);
4442 }
4443
4444
4445 /*
4446  * perform the pmap work for mincore
4447  */
4448 int
4449 pmap_mincore(pmap_t pmap, vm_offset_t addr)
4450 {
4451         printf("pmap_mincore()\n");
4452         
4453         return (0);
4454 }
4455
4456
4457 /*
4458  *      Increase the starting virtual address of the given mapping if a
4459  *      different alignment might result in more superpage mappings.
4460  */
4461 void
4462 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
4463     vm_offset_t *addr, vm_size_t size)
4464 {
4465 }
4466
4467
4468 /*
4469  * Map a set of physical memory pages into the kernel virtual
4470  * address space. Return a pointer to where it is mapped. This
4471  * routine is intended to be used for mapping device memory,
4472  * NOT real memory.
4473  */
4474 void *
4475 pmap_mapdev(vm_offset_t pa, vm_size_t size)
4476 {
4477         vm_offset_t va, tmpva, offset;
4478         
4479         offset = pa & PAGE_MASK;
4480         size = roundup(size, PAGE_SIZE);
4481         
4482         GIANT_REQUIRED;
4483         
4484         va = kmem_alloc_nofault(kernel_map, size);
4485         if (!va)
4486                 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
4487         for (tmpva = va; size > 0;) {
4488                 pmap_kenter_internal(tmpva, pa, 0);
4489                 size -= PAGE_SIZE;
4490                 tmpva += PAGE_SIZE;
4491                 pa += PAGE_SIZE;
4492         }
4493         
4494         return ((void *)(va + offset));
4495 }
4496
4497 #define BOOTSTRAP_DEBUG
4498
4499 /*
4500  * pmap_map_section:
4501  *
4502  *      Create a single section mapping.
4503  */
4504 void
4505 pmap_map_section(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa,
4506     int prot, int cache)
4507 {
4508         pd_entry_t *pde = (pd_entry_t *) l1pt;
4509         pd_entry_t fl;
4510
4511         KASSERT(((va | pa) & L1_S_OFFSET) == 0, ("ouin2"));
4512
4513         switch (cache) {
4514         case PTE_NOCACHE:
4515         default:
4516                 fl = 0;
4517                 break;
4518
4519         case PTE_CACHE:
4520                 fl = pte_l1_s_cache_mode;
4521                 break;
4522
4523         case PTE_PAGETABLE:
4524                 fl = pte_l1_s_cache_mode_pt;
4525                 break;
4526         }
4527
4528         pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
4529             L1_S_PROT(PTE_KERNEL, prot) | fl | L1_S_DOM(PMAP_DOMAIN_KERNEL);
4530         PTE_SYNC(&pde[va >> L1_S_SHIFT]);
4531
4532 }
4533
4534 /*
4535  * pmap_link_l2pt:
4536  *
4537  *      Link the L2 page table specified by l2pv.pv_pa into the L1
4538  *      page table at the slot for "va".
4539  */
4540 void
4541 pmap_link_l2pt(vm_offset_t l1pt, vm_offset_t va, struct pv_addr *l2pv)
4542 {
4543         pd_entry_t *pde = (pd_entry_t *) l1pt, proto;
4544         u_int slot = va >> L1_S_SHIFT;
4545
4546         proto = L1_S_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO;
4547
4548 #ifdef VERBOSE_INIT_ARM     
4549         printf("pmap_link_l2pt: pa=0x%x va=0x%x\n", l2pv->pv_pa, l2pv->pv_va);
4550 #endif
4551
4552         pde[slot + 0] = proto | (l2pv->pv_pa + 0x000);
4553
4554         PTE_SYNC(&pde[slot]);
4555
4556         SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
4557
4558         
4559 }
4560
4561 /*
4562  * pmap_map_entry
4563  *
4564  *      Create a single page mapping.
4565  */
4566 void
4567 pmap_map_entry(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa, int prot,
4568     int cache)
4569 {
4570         pd_entry_t *pde = (pd_entry_t *) l1pt;
4571         pt_entry_t fl;
4572         pt_entry_t *pte;
4573
4574         KASSERT(((va | pa) & PAGE_MASK) == 0, ("ouin"));
4575
4576         switch (cache) {
4577         case PTE_NOCACHE:
4578         default:
4579                 fl = 0;
4580                 break;
4581
4582         case PTE_CACHE:
4583                 fl = pte_l2_s_cache_mode;
4584                 break;
4585
4586         case PTE_PAGETABLE:
4587                 fl = pte_l2_s_cache_mode_pt;
4588                 break;
4589         }
4590
4591         if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
4592                 panic("pmap_map_entry: no L2 table for VA 0x%08x", va);
4593
4594         pte = (pt_entry_t *) kernel_pt_lookup(pde[L1_IDX(va)] & L1_C_ADDR_MASK);
4595
4596         if (pte == NULL)
4597                 panic("pmap_map_entry: can't find L2 table for VA 0x%08x", va);
4598
4599         pte[l2pte_index(va)] =
4600             L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | fl;
4601         PTE_SYNC(&pte[l2pte_index(va)]);
4602 }
4603
4604 /*
4605  * pmap_map_chunk:
4606  *
4607  *      Map a chunk of memory using the most efficient mappings
4608  *      possible (section. large page, small page) into the
4609  *      provided L1 and L2 tables at the specified virtual address.
4610  */
4611 vm_size_t
4612 pmap_map_chunk(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa,
4613     vm_size_t size, int prot, int cache)
4614 {
4615         pd_entry_t *pde = (pd_entry_t *) l1pt;
4616         pt_entry_t *pte, f1, f2s, f2l;
4617         vm_size_t resid;  
4618         int i;
4619
4620         resid = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
4621
4622         if (l1pt == 0)
4623                 panic("pmap_map_chunk: no L1 table provided");
4624
4625 #ifdef VERBOSE_INIT_ARM     
4626         printf("pmap_map_chunk: pa=0x%x va=0x%x size=0x%x resid=0x%x "
4627             "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
4628 #endif
4629
4630         switch (cache) {
4631         case PTE_NOCACHE:
4632         default:
4633                 f1 = 0;
4634                 f2l = 0;
4635                 f2s = 0;
4636                 break;
4637
4638         case PTE_CACHE:
4639                 f1 = pte_l1_s_cache_mode;
4640                 f2l = pte_l2_l_cache_mode;
4641                 f2s = pte_l2_s_cache_mode;
4642                 break;
4643
4644         case PTE_PAGETABLE:
4645                 f1 = pte_l1_s_cache_mode_pt;
4646                 f2l = pte_l2_l_cache_mode_pt;
4647                 f2s = pte_l2_s_cache_mode_pt;
4648                 break;
4649         }
4650
4651         size = resid;
4652
4653         while (resid > 0) {
4654                 /* See if we can use a section mapping. */
4655                 if (L1_S_MAPPABLE_P(va, pa, resid)) {
4656 #ifdef VERBOSE_INIT_ARM
4657                         printf("S");
4658 #endif
4659                         pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
4660                             L1_S_PROT(PTE_KERNEL, prot) | f1 |
4661                             L1_S_DOM(PMAP_DOMAIN_KERNEL);
4662                         PTE_SYNC(&pde[va >> L1_S_SHIFT]);
4663                         va += L1_S_SIZE;
4664                         pa += L1_S_SIZE;
4665                         resid -= L1_S_SIZE;
4666                         continue;
4667                 }
4668
4669                 /*
4670                  * Ok, we're going to use an L2 table.  Make sure
4671                  * one is actually in the corresponding L1 slot
4672                  * for the current VA.
4673                  */
4674                 if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
4675                         panic("pmap_map_chunk: no L2 table for VA 0x%08x", va);
4676
4677                 pte = (pt_entry_t *) kernel_pt_lookup(
4678                     pde[L1_IDX(va)] & L1_C_ADDR_MASK);
4679                 if (pte == NULL)
4680                         panic("pmap_map_chunk: can't find L2 table for VA"
4681                             "0x%08x", va);
4682                 /* See if we can use a L2 large page mapping. */
4683                 if (L2_L_MAPPABLE_P(va, pa, resid)) {
4684 #ifdef VERBOSE_INIT_ARM
4685                         printf("L");
4686 #endif
4687                         for (i = 0; i < 16; i++) {
4688                                 pte[l2pte_index(va) + i] =
4689                                     L2_L_PROTO | pa |
4690                                     L2_L_PROT(PTE_KERNEL, prot) | f2l;
4691                                 PTE_SYNC(&pte[l2pte_index(va) + i]);
4692                         }
4693                         va += L2_L_SIZE;
4694                         pa += L2_L_SIZE;
4695                         resid -= L2_L_SIZE;
4696                         continue;
4697                 }
4698
4699                 /* Use a small page mapping. */
4700 #ifdef VERBOSE_INIT_ARM
4701                 printf("P");
4702 #endif
4703                 pte[l2pte_index(va)] =
4704                     L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
4705                 PTE_SYNC(&pte[l2pte_index(va)]);
4706                 va += PAGE_SIZE;
4707                 pa += PAGE_SIZE;
4708                 resid -= PAGE_SIZE;
4709         }
4710 #ifdef VERBOSE_INIT_ARM
4711         printf("\n");
4712 #endif
4713         return (size);
4714
4715 }
4716
4717 /********************** Static device map routines ***************************/
4718
4719 static const struct pmap_devmap *pmap_devmap_table;
4720
4721 /*
4722  * Register the devmap table.  This is provided in case early console
4723  * initialization needs to register mappings created by bootstrap code
4724  * before pmap_devmap_bootstrap() is called.
4725  */
4726 void
4727 pmap_devmap_register(const struct pmap_devmap *table)
4728 {
4729
4730         pmap_devmap_table = table;
4731 }
4732
4733 /*
4734  * Map all of the static regions in the devmap table, and remember
4735  * the devmap table so other parts of the kernel can look up entries
4736  * later.
4737  */
4738 void
4739 pmap_devmap_bootstrap(vm_offset_t l1pt, const struct pmap_devmap *table)
4740 {
4741         int i;
4742
4743         pmap_devmap_table = table;
4744
4745         for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
4746 #ifdef VERBOSE_INIT_ARM
4747                 printf("devmap: %08x -> %08x @ %08x\n",
4748                     pmap_devmap_table[i].pd_pa,
4749                     pmap_devmap_table[i].pd_pa +
4750                         pmap_devmap_table[i].pd_size - 1,
4751                     pmap_devmap_table[i].pd_va);
4752 #endif
4753                 pmap_map_chunk(l1pt, pmap_devmap_table[i].pd_va,
4754                     pmap_devmap_table[i].pd_pa,
4755                     pmap_devmap_table[i].pd_size,
4756                     pmap_devmap_table[i].pd_prot,
4757                     pmap_devmap_table[i].pd_cache);
4758         }
4759 }
4760
4761 const struct pmap_devmap *
4762 pmap_devmap_find_pa(vm_paddr_t pa, vm_size_t size)
4763 {
4764         int i;
4765
4766         if (pmap_devmap_table == NULL)
4767                 return (NULL);
4768
4769         for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
4770                 if (pa >= pmap_devmap_table[i].pd_pa &&
4771                     pa + size <= pmap_devmap_table[i].pd_pa +
4772                                  pmap_devmap_table[i].pd_size)
4773                         return (&pmap_devmap_table[i]);
4774         }
4775
4776         return (NULL);
4777 }
4778
4779 const struct pmap_devmap *
4780 pmap_devmap_find_va(vm_offset_t va, vm_size_t size)
4781 {
4782         int i;
4783
4784         if (pmap_devmap_table == NULL)
4785                 return (NULL);
4786
4787         for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
4788                 if (va >= pmap_devmap_table[i].pd_va &&
4789                     va + size <= pmap_devmap_table[i].pd_va +
4790                                  pmap_devmap_table[i].pd_size)
4791                         return (&pmap_devmap_table[i]);
4792         }
4793
4794         return (NULL);
4795 }
4796