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