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