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