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