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