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