pmap.c revision 1.22 1 /* $NetBSD: pmap.c,v 1.22 1997/05/20 06:01:19 jeremy Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jeremy Cooper.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * XXX These comments aren't quite accurate. Need to change.
41 * The sun3x uses the MC68851 Memory Management Unit, which is built
42 * into the CPU. The 68851 maps virtual to physical addresses using
43 * a multi-level table lookup, which is stored in the very memory that
44 * it maps. The number of levels of lookup is configurable from one
45 * to four. In this implementation, we use three, named 'A' through 'C'.
46 *
47 * The MMU translates virtual addresses into physical addresses by
48 * traversing these tables in a proccess called a 'table walk'. The most
49 * significant 7 bits of the Virtual Address ('VA') being translated are
50 * used as an index into the level A table, whose base in physical memory
51 * is stored in a special MMU register, the 'CPU Root Pointer' or CRP. The
52 * address found at that index in the A table is used as the base
53 * address for the next table, the B table. The next six bits of the VA are
54 * used as an index into the B table, which in turn gives the base address
55 * of the third and final C table.
56 *
57 * The next six bits of the VA are used as an index into the C table to
58 * locate a Page Table Entry (PTE). The PTE is a physical address in memory
59 * to which the remaining 13 bits of the VA are added, producing the
60 * mapped physical address.
61 *
62 * To map the entire memory space in this manner would require 2114296 bytes
63 * of page tables per process - quite expensive. Instead we will
64 * allocate a fixed but considerably smaller space for the page tables at
65 * the time the VM system is initialized. When the pmap code is asked by
66 * the kernel to map a VA to a PA, it allocates tables as needed from this
67 * pool. When there are no more tables in the pool, tables are stolen
68 * from the oldest mapped entries in the tree. This is only possible
69 * because all memory mappings are stored in the kernel memory map
70 * structures, independent of the pmap structures. A VA which references
71 * one of these invalidated maps will cause a page fault. The kernel
72 * will determine that the page fault was caused by a task using a valid
73 * VA, but for some reason (which does not concern it), that address was
74 * not mapped. It will ask the pmap code to re-map the entry and then
75 * it will resume executing the faulting task.
76 *
77 * In this manner the most efficient use of the page table space is
78 * achieved. Tasks which do not execute often will have their tables
79 * stolen and reused by tasks which execute more frequently. The best
80 * size for the page table pool will probably be determined by
81 * experimentation.
82 *
83 * You read all of the comments so far. Good for you.
84 * Now go play!
85 */
86
87 /*** A Note About the 68851 Address Translation Cache
88 * The MC68851 has a 64 entry cache, called the Address Translation Cache
89 * or 'ATC'. This cache stores the most recently used page descriptors
90 * accessed by the MMU when it does translations. Using a marker called a
91 * 'task alias' the MMU can store the descriptors from 8 different table
92 * spaces concurrently. The task alias is associated with the base
93 * address of the level A table of that address space. When an address
94 * space is currently active (the CRP currently points to its A table)
95 * the only cached descriptors that will be obeyed are ones which have a
96 * matching task alias of the current space associated with them.
97 *
98 * Since the cache is always consulted before any table lookups are done,
99 * it is important that it accurately reflect the state of the MMU tables.
100 * Whenever a change has been made to a table that has been loaded into
101 * the MMU, the code must be sure to flush any cached entries that are
102 * affected by the change. These instances are documented in the code at
103 * various points.
104 */
105 /*** A Note About the Note About the 68851 Address Translation Cache
106 * 4 months into this code I discovered that the sun3x does not have
107 * a MC68851 chip. Instead, it has a version of this MMU that is part of the
108 * the 68030 CPU.
109 * All though it behaves very similarly to the 68851, it only has 1 task
110 * alias and a 22 entry cache. So sadly (or happily), the first paragraph
111 * of the previous note does not apply to the sun3x pmap.
112 */
113
114 #include <sys/param.h>
115 #include <sys/systm.h>
116 #include <sys/proc.h>
117 #include <sys/malloc.h>
118 #include <sys/user.h>
119 #include <sys/queue.h>
120 #include <sys/kcore.h>
121
122 #include <vm/vm.h>
123 #include <vm/vm_kern.h>
124 #include <vm/vm_page.h>
125
126 #include <machine/cpu.h>
127 #include <machine/kcore.h>
128 #include <machine/pmap.h>
129 #include <machine/pte.h>
130 #include <machine/machdep.h>
131 #include <machine/mon.h>
132
133 #include "pmap_pvt.h"
134
135 /* XXX - What headers declare these? */
136 extern struct pcb *curpcb;
137 extern int physmem;
138
139 extern void copypage __P((const void*, void*));
140 extern void zeropage __P((void*));
141
142 /* Defined in locore.s */
143 extern char kernel_text[];
144
145 /* Defined by the linker */
146 extern char etext[], edata[], end[];
147 extern char *esym; /* DDB */
148
149 /*************************** DEBUGGING DEFINITIONS ***********************
150 * Macros, preprocessor defines and variables used in debugging can make *
151 * code hard to read. Anything used exclusively for debugging purposes *
152 * is defined here to avoid having such mess scattered around the file. *
153 *************************************************************************/
154 #ifdef PMAP_DEBUG
155 /*
156 * To aid the debugging process, macros should be expanded into smaller steps
157 * that accomplish the same goal, yet provide convenient places for placing
158 * breakpoints. When this code is compiled with PMAP_DEBUG mode defined, the
159 * 'INLINE' keyword is defined to an empty string. This way, any function
160 * defined to be a 'static INLINE' will become 'outlined' and compiled as
161 * a separate function, which is much easier to debug.
162 */
163 #define INLINE /* nothing */
164
165 /*
166 * It is sometimes convenient to watch the activity of a particular table
167 * in the system. The following variables are used for that purpose.
168 */
169 a_tmgr_t *pmap_watch_atbl = 0;
170 b_tmgr_t *pmap_watch_btbl = 0;
171 c_tmgr_t *pmap_watch_ctbl = 0;
172
173 int pmap_debug = 0;
174 #define DPRINT(args) if (pmap_debug) printf args
175
176 #else /********** Stuff below is defined if NOT debugging **************/
177
178 #define INLINE inline
179 #define DPRINT(args) /* nada */
180
181 #endif /* PMAP_DEBUG */
182 /*********************** END OF DEBUGGING DEFINITIONS ********************/
183
184 /*** Management Structure - Memory Layout
185 * For every MMU table in the sun3x pmap system there must be a way to
186 * manage it; we must know which process is using it, what other tables
187 * depend on it, and whether or not it contains any locked pages. This
188 * is solved by the creation of 'table management' or 'tmgr'
189 * structures. One for each MMU table in the system.
190 *
191 * MAP OF MEMORY USED BY THE PMAP SYSTEM
192 *
193 * towards lower memory
194 * kernAbase -> +-------------------------------------------------------+
195 * | Kernel MMU A level table |
196 * kernBbase -> +-------------------------------------------------------+
197 * | Kernel MMU B level tables |
198 * kernCbase -> +-------------------------------------------------------+
199 * | |
200 * | Kernel MMU C level tables |
201 * | |
202 * mmuCbase -> +-------------------------------------------------------+
203 * | User MMU C level tables |
204 * mmuAbase -> +-------------------------------------------------------+
205 * | |
206 * | User MMU A level tables |
207 * | |
208 * mmuBbase -> +-------------------------------------------------------+
209 * | User MMU B level tables |
210 * tmgrAbase -> +-------------------------------------------------------+
211 * | TMGR A level table structures |
212 * tmgrBbase -> +-------------------------------------------------------+
213 * | TMGR B level table structures |
214 * tmgrCbase -> +-------------------------------------------------------+
215 * | TMGR C level table structures |
216 * pvbase -> +-------------------------------------------------------+
217 * | Physical to Virtual mapping table (list heads) |
218 * pvebase -> +-------------------------------------------------------+
219 * | Physical to Virtual mapping table (list elements) |
220 * | |
221 * +-------------------------------------------------------+
222 * towards higher memory
223 *
224 * For every A table in the MMU A area, there will be a corresponding
225 * a_tmgr structure in the TMGR A area. The same will be true for
226 * the B and C tables. This arrangement will make it easy to find the
227 * controling tmgr structure for any table in the system by use of
228 * (relatively) simple macros.
229 */
230
231 /*
232 * Global variables for storing the base addresses for the areas
233 * labeled above.
234 */
235 static vm_offset_t kernAphys;
236 static mmu_long_dte_t *kernAbase;
237 static mmu_short_dte_t *kernBbase;
238 static mmu_short_pte_t *kernCbase;
239 static mmu_short_pte_t *mmuCbase;
240 static mmu_short_dte_t *mmuBbase;
241 static mmu_long_dte_t *mmuAbase;
242 static a_tmgr_t *Atmgrbase;
243 static b_tmgr_t *Btmgrbase;
244 static c_tmgr_t *Ctmgrbase;
245 static pv_t *pvbase;
246 static pv_elem_t *pvebase;
247 struct pmap kernel_pmap;
248
249 /*
250 * This holds the CRP currently loaded into the MMU.
251 */
252 struct mmu_rootptr kernel_crp;
253
254 /*
255 * Just all around global variables.
256 */
257 static TAILQ_HEAD(a_pool_head_struct, a_tmgr_struct) a_pool;
258 static TAILQ_HEAD(b_pool_head_struct, b_tmgr_struct) b_pool;
259 static TAILQ_HEAD(c_pool_head_struct, c_tmgr_struct) c_pool;
260
261
262 /*
263 * Flags used to mark the safety/availability of certain operations or
264 * resources.
265 */
266 static boolean_t
267 pv_initialized = FALSE, /* PV system has been initialized. */
268 tmp_vpages_inuse = FALSE, /*
269 * Temp. virtual pages are in use.
270 * (see pmap_copy_page, et. al.)
271 */
272 bootstrap_alloc_enabled = FALSE; /* Safe to use pmap_bootstrap_alloc(). */
273
274 /*
275 * XXX: For now, retain the traditional variables that were
276 * used in the old pmap/vm interface (without NONCONTIG).
277 */
278 /* Kernel virtual address space available: */
279 vm_offset_t virtual_avail, virtual_end;
280 /* Physical address space available: */
281 vm_offset_t avail_start, avail_end;
282
283 /* This keep track of the end of the contiguously mapped range. */
284 vm_offset_t virtual_contig_end;
285
286 /* Physical address used by pmap_next_page() */
287 vm_offset_t avail_next;
288
289 /* These are used by pmap_copy_page(), etc. */
290 vm_offset_t tmp_vpages[2];
291
292 /*
293 * The 3/80 is the only member of the sun3x family that has non-contiguous
294 * physical memory. Memory is divided into 4 banks which are physically
295 * locatable on the system board. Although the size of these banks varies
296 * with the size of memory they contain, their base addresses are
297 * permenently fixed. The following structure, which describes these
298 * banks, is initialized by pmap_bootstrap() after it reads from a similar
299 * structure provided by the ROM Monitor.
300 *
301 * For the other machines in the sun3x architecture which do have contiguous
302 * RAM, this list will have only one entry, which will describe the entire
303 * range of available memory.
304 */
305 struct pmap_physmem_struct avail_mem[SUN3X_NPHYS_RAM_SEGS];
306 u_int total_phys_mem;
307
308 /*************************************************************************/
309
310 /*
311 * XXX - Should "tune" these based on statistics.
312 *
313 * My first guess about the relative numbers of these needed is
314 * based on the fact that a "typical" process will have several
315 * pages mapped at low virtual addresses (text, data, bss), then
316 * some mapped shared libraries, and then some stack pages mapped
317 * near the high end of the VA space. Each process can use only
318 * one A table, and most will use only two B tables (maybe three)
319 * and probably about four C tables. Therefore, the first guess
320 * at the relative numbers of these needed is 1:2:4 -gwr
321 *
322 * The number of C tables needed is closely related to the amount
323 * of physical memory available plus a certain amount attributable
324 * to the use of double mappings. With a few simulation statistics
325 * we can find a reasonably good estimation of this unknown value.
326 * Armed with that and the above ratios, we have a good idea of what
327 * is needed at each level. -j
328 *
329 * Note: It is not physical memory memory size, but the total mapped
330 * virtual space required by the combined working sets of all the
331 * currently _runnable_ processes. (Sleeping ones don't count.)
332 * The amount of physical memory should be irrelevant. -gwr
333 */
334 #ifdef FIXED_NTABLES
335 #define NUM_A_TABLES 16
336 #define NUM_B_TABLES 32
337 #define NUM_C_TABLES 64
338 #else
339 unsigned int NUM_A_TABLES, NUM_B_TABLES, NUM_C_TABLES;
340 #endif /* FIXED_NTABLES */
341
342 /*
343 * This determines our total virtual mapping capacity.
344 * Yes, it is a FIXED value so we can pre-allocate.
345 */
346 #define NUM_USER_PTES (NUM_C_TABLES * MMU_C_TBL_SIZE)
347
348 /*
349 * The size of the Kernel Virtual Address Space (KVAS)
350 * for purposes of MMU table allocation is -KERNBASE
351 * (length from KERNBASE to 0xFFFFffff)
352 */
353 #define KVAS_SIZE (-KERNBASE)
354
355 /* Numbers of kernel MMU tables to support KVAS_SIZE. */
356 #define KERN_B_TABLES (KVAS_SIZE >> MMU_TIA_SHIFT)
357 #define KERN_C_TABLES (KVAS_SIZE >> MMU_TIB_SHIFT)
358 #define NUM_KERN_PTES (KVAS_SIZE >> MMU_TIC_SHIFT)
359
360 /*************************** MISCELANEOUS MACROS *************************/
361 #define PMAP_LOCK() ; /* Nothing, for now */
362 #define PMAP_UNLOCK() ; /* same. */
363 #define NULL 0
364
365 static INLINE void * mmu_ptov __P((vm_offset_t pa));
366 static INLINE vm_offset_t mmu_vtop __P((void * va));
367
368 #if 0
369 static INLINE a_tmgr_t * mmuA2tmgr __P((mmu_long_dte_t *));
370 #endif
371 static INLINE b_tmgr_t * mmuB2tmgr __P((mmu_short_dte_t *));
372 static INLINE c_tmgr_t * mmuC2tmgr __P((mmu_short_pte_t *));
373
374 static INLINE pv_t *pa2pv __P((vm_offset_t pa));
375 static INLINE int pteidx __P((mmu_short_pte_t *));
376 static INLINE pmap_t current_pmap __P((void));
377
378 /*
379 * We can always convert between virtual and physical addresses
380 * for anything in the range [KERNBASE ... avail_start] because
381 * that range is GUARANTEED to be mapped linearly.
382 * We rely heavily upon this feature!
383 */
384 static INLINE void *
385 mmu_ptov(pa)
386 vm_offset_t pa;
387 {
388 register vm_offset_t va;
389
390 va = (pa + KERNBASE);
391 #ifdef PMAP_DEBUG
392 if ((va < KERNBASE) || (va >= virtual_contig_end))
393 panic("mmu_ptov");
394 #endif
395 return ((void*)va);
396 }
397 static INLINE vm_offset_t
398 mmu_vtop(vva)
399 void *vva;
400 {
401 register vm_offset_t va;
402
403 va = (vm_offset_t)vva;
404 #ifdef PMAP_DEBUG
405 if ((va < KERNBASE) || (va >= virtual_contig_end))
406 panic("mmu_ptov");
407 #endif
408 return (va - KERNBASE);
409 }
410
411 /*
412 * These macros map MMU tables to their corresponding manager structures.
413 * They are needed quite often because many of the pointers in the pmap
414 * system reference MMU tables and not the structures that control them.
415 * There needs to be a way to find one when given the other and these
416 * macros do so by taking advantage of the memory layout described above.
417 * Here's a quick step through the first macro, mmuA2tmgr():
418 *
419 * 1) find the offset of the given MMU A table from the base of its table
420 * pool (table - mmuAbase).
421 * 2) convert this offset into a table index by dividing it by the
422 * size of one MMU 'A' table. (sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE)
423 * 3) use this index to select the corresponding 'A' table manager
424 * structure from the 'A' table manager pool (Atmgrbase[index]).
425 */
426 /* This function is not currently used. */
427 #if 0
428 static INLINE a_tmgr_t *
429 mmuA2tmgr(mmuAtbl)
430 mmu_long_dte_t *mmuAtbl;
431 {
432 register int idx;
433
434 /* Which table is this in? */
435 idx = (mmuAtbl - mmuAbase) / MMU_A_TBL_SIZE;
436 #ifdef PMAP_DEBUG
437 if ((idx < 0) || (idx >= NUM_A_TABLES))
438 panic("mmuA2tmgr");
439 #endif
440 return (&Atmgrbase[idx]);
441 }
442 #endif /* 0 */
443
444 static INLINE b_tmgr_t *
445 mmuB2tmgr(mmuBtbl)
446 mmu_short_dte_t *mmuBtbl;
447 {
448 register int idx;
449
450 /* Which table is this in? */
451 idx = (mmuBtbl - mmuBbase) / MMU_B_TBL_SIZE;
452 #ifdef PMAP_DEBUG
453 if ((idx < 0) || (idx >= NUM_B_TABLES))
454 panic("mmuB2tmgr");
455 #endif
456 return (&Btmgrbase[idx]);
457 }
458
459 /* mmuC2tmgr INTERNAL
460 **
461 * Given a pte known to belong to a C table, return the address of
462 * that table's management structure.
463 */
464 static INLINE c_tmgr_t *
465 mmuC2tmgr(mmuCtbl)
466 mmu_short_pte_t *mmuCtbl;
467 {
468 register int idx;
469
470 /* Which table is this in? */
471 idx = (mmuCtbl - mmuCbase) / MMU_C_TBL_SIZE;
472 #ifdef PMAP_DEBUG
473 if ((idx < 0) || (idx >= NUM_C_TABLES))
474 panic("mmuC2tmgr");
475 #endif
476 return (&Ctmgrbase[idx]);
477 }
478
479 /* This is now a function call below.
480 * #define pa2pv(pa) \
481 * (&pvbase[(unsigned long)\
482 * _btop(pa)\
483 * ])
484 */
485
486 /* pa2pv INTERNAL
487 **
488 * Return the pv_list_head element which manages the given physical
489 * address.
490 */
491 static INLINE pv_t *
492 pa2pv(pa)
493 vm_offset_t pa;
494 {
495 register struct pmap_physmem_struct *bank;
496 register int idx;
497
498 bank = &avail_mem[0];
499 while (pa >= bank->pmem_end)
500 bank = bank->pmem_next;
501
502 pa -= bank->pmem_start;
503 idx = bank->pmem_pvbase + _btop(pa);
504 #ifdef PMAP_DEBUG
505 if ((idx < 0) || (idx >= physmem))
506 panic("pa2pv");
507 #endif
508 return &pvbase[idx];
509 }
510
511 /* pteidx INTERNAL
512 **
513 * Return the index of the given PTE within the entire fixed table of
514 * PTEs.
515 */
516 static INLINE int
517 pteidx(pte)
518 mmu_short_pte_t *pte;
519 {
520 return (pte - kernCbase);
521 }
522
523 /*
524 * This just offers a place to put some debugging checks,
525 * and reduces the number of places "curproc" appears...
526 */
527 static INLINE pmap_t
528 current_pmap()
529 {
530 struct proc *p;
531 struct vmspace *vm;
532 vm_map_t map;
533 pmap_t pmap;
534
535 p = curproc; /* XXX */
536 if (p == NULL)
537 pmap = &kernel_pmap;
538 else {
539 vm = p->p_vmspace;
540 map = &vm->vm_map;
541 pmap = vm_map_pmap(map);
542 }
543
544 return (pmap);
545 }
546
547
548 /*************************** FUNCTION DEFINITIONS ************************
549 * These appear here merely for the compiler to enforce type checking on *
550 * all function calls. *
551 *************************************************************************/
552
553 /** External functions
554 ** - functions used within this module but written elsewhere.
555 ** both of these functions are in locore.s
556 ** XXX - These functions were later replaced with their more cryptic
557 ** hp300 counterparts. They may be removed now.
558 **/
559 #if 0 /* deprecated mmu */
560 void mmu_seturp __P((vm_offset_t));
561 void mmu_flush __P((int, vm_offset_t));
562 void mmu_flusha __P((void));
563 #endif /* 0 */
564
565 /** Internal functions
566 ** - all functions used only within this module are defined in
567 ** pmap_pvt.h
568 **/
569
570 /** Interface functions
571 ** - functions required by the Mach VM Pmap interface, with MACHINE_CONTIG
572 ** defined.
573 **/
574 #ifdef INCLUDED_IN_PMAP_H
575 void pmap_bootstrap __P((void));
576 void *pmap_bootstrap_alloc __P((int));
577 void pmap_enter __P((pmap_t, vm_offset_t, vm_offset_t, vm_prot_t, boolean_t));
578 pmap_t pmap_create __P((vm_size_t));
579 void pmap_destroy __P((pmap_t));
580 void pmap_reference __P((pmap_t));
581 boolean_t pmap_is_referenced __P((vm_offset_t));
582 boolean_t pmap_is_modified __P((vm_offset_t));
583 void pmap_clear_modify __P((vm_offset_t));
584 vm_offset_t pmap_extract __P((pmap_t, vm_offset_t));
585 void pmap_activate __P((pmap_t));
586 int pmap_page_index __P((vm_offset_t));
587 u_int pmap_free_pages __P((void));
588 #endif /* INCLUDED_IN_PMAP_H */
589
590 /********************************** CODE ********************************
591 * Functions that are called from other parts of the kernel are labeled *
592 * as 'INTERFACE' functions. Functions that are only called from *
593 * within the pmap module are labeled as 'INTERNAL' functions. *
594 * Functions that are internal, but are not (currently) used at all are *
595 * labeled 'INTERNAL_X'. *
596 ************************************************************************/
597
598 /* pmap_bootstrap INTERNAL
599 **
600 * Initializes the pmap system. Called at boot time from _vm_init()
601 * in _startup.c.
602 *
603 * Reminder: having a pmap_bootstrap_alloc() and also having the VM
604 * system implement pmap_steal_memory() is redundant.
605 * Don't release this code without removing one or the other!
606 */
607 void
608 pmap_bootstrap(nextva)
609 vm_offset_t nextva;
610 {
611 struct physmemory *membank;
612 struct pmap_physmem_struct *pmap_membank;
613 vm_offset_t va, pa, eva;
614 int b, c, i, j; /* running table counts */
615 int size;
616
617 /*
618 * This function is called by __bootstrap after it has
619 * determined the type of machine and made the appropriate
620 * patches to the ROM vectors (XXX- I don't quite know what I meant
621 * by that.) It allocates and sets up enough of the pmap system
622 * to manage the kernel's address space.
623 */
624
625 /*
626 * Determine the range of kernel virtual and physical
627 * space available. Note that we ABSOLUTELY DEPEND on
628 * the fact that the first bank of memory (4MB) is
629 * mapped linearly to KERNBASE (which we guaranteed in
630 * the first instructions of locore.s).
631 * That is plenty for our bootstrap work.
632 */
633 virtual_avail = _round_page(nextva);
634 virtual_contig_end = KERNBASE + 0x400000; /* +4MB */
635 virtual_end = VM_MAX_KERNEL_ADDRESS;
636 /* Don't need avail_start til later. */
637
638 /* We may now call pmap_bootstrap_alloc(). */
639 bootstrap_alloc_enabled = TRUE;
640
641 /*
642 * This is a somewhat unwrapped loop to deal with
643 * copying the PROM's 'phsymem' banks into the pmap's
644 * banks. The following is always assumed:
645 * 1. There is always at least one bank of memory.
646 * 2. There is always a last bank of memory, and its
647 * pmem_next member must be set to NULL.
648 * XXX - Use: do { ... } while (membank->next) instead?
649 * XXX - Why copy this stuff at all? -gwr
650 * - It is needed in pa2pv().
651 */
652 membank = romVectorPtr->v_physmemory;
653 pmap_membank = avail_mem;
654 total_phys_mem = 0;
655
656 while (membank->next) {
657 pmap_membank->pmem_start = membank->address;
658 pmap_membank->pmem_end = membank->address + membank->size;
659 total_phys_mem += membank->size;
660 /* This silly syntax arises because pmap_membank
661 * is really a pre-allocated array, but it is put into
662 * use as a linked list.
663 */
664 pmap_membank->pmem_next = pmap_membank + 1;
665 pmap_membank = pmap_membank->pmem_next;
666 membank = membank->next;
667 }
668
669 /*
670 * XXX The last bank of memory should be reduced to exclude the
671 * physical pages needed by the PROM monitor from being used
672 * in the VM system. XXX - See below - Fix!
673 */
674 pmap_membank->pmem_start = membank->address;
675 pmap_membank->pmem_end = membank->address + membank->size;
676 pmap_membank->pmem_next = NULL;
677
678 #if 0 /* XXX - Need to integrate this! */
679 /*
680 * The last few pages of physical memory are "owned" by
681 * the PROM. The total amount of memory we are allowed
682 * to use is given by the romvec pointer. -gwr
683 *
684 * We should dedicate different variables for 'useable'
685 * and 'physically available'. Most users are used to the
686 * kernel reporting the amount of memory 'physically available'
687 * as opposed to 'useable by the kernel' at boot time. -j
688 */
689 total_phys_mem = *romVectorPtr->memoryAvail;
690 #endif /* XXX */
691
692 total_phys_mem += membank->size; /* XXX see above */
693 physmem = btoc(total_phys_mem);
694
695 /*
696 * Avail_end is set to the first byte of physical memory
697 * after the end of the last bank. We use this only to
698 * determine if a physical address is "managed" memory.
699 *
700 * XXX - The setting of avail_end is a temporary ROM saving hack.
701 */
702 avail_end = pmap_membank->pmem_end -
703 (total_phys_mem - *romVectorPtr->memoryAvail);
704 avail_end = _trunc_page(avail_end);
705
706 /*
707 * First allocate enough kernel MMU tables to map all
708 * of kernel virtual space from KERNBASE to 0xFFFFFFFF.
709 * Note: All must be aligned on 256 byte boundaries.
710 * Start with the level-A table (one of those).
711 */
712 size = sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE;
713 kernAbase = pmap_bootstrap_alloc(size);
714 bzero(kernAbase, size);
715
716 /* Now the level-B kernel tables... */
717 size = sizeof(mmu_short_dte_t) * MMU_B_TBL_SIZE * KERN_B_TABLES;
718 kernBbase = pmap_bootstrap_alloc(size);
719 bzero(kernBbase, size);
720
721 /* Now the level-C kernel tables... */
722 size = sizeof(mmu_short_pte_t) * MMU_C_TBL_SIZE * KERN_C_TABLES;
723 kernCbase = pmap_bootstrap_alloc(size);
724 bzero(kernCbase, size);
725 /*
726 * Note: In order for the PV system to work correctly, the kernel
727 * and user-level C tables must be allocated contiguously.
728 * Nothing should be allocated between here and the allocation of
729 * mmuCbase below. XXX: Should do this as one allocation, and
730 * then compute a pointer for mmuCbase instead of this...
731 *
732 * Allocate user MMU tables.
733 * These must be contiguous with the preceeding.
734 */
735
736 #ifndef FIXED_NTABLES
737 /*
738 * The number of user-level C tables that should be allocated is
739 * related to the size of physical memory. In general, there should
740 * be enough tables to map four times the amount of available RAM.
741 * The extra amount is needed because some table space is wasted by
742 * fragmentation.
743 */
744 NUM_C_TABLES = (total_phys_mem * 4) / (MMU_C_TBL_SIZE * MMU_PAGE_SIZE);
745 NUM_B_TABLES = NUM_C_TABLES / 2;
746 NUM_A_TABLES = NUM_B_TABLES / 2;
747 #endif /* !FIXED_NTABLES */
748
749 size = sizeof(mmu_short_pte_t) * MMU_C_TBL_SIZE * NUM_C_TABLES;
750 mmuCbase = pmap_bootstrap_alloc(size);
751
752 size = sizeof(mmu_short_dte_t) * MMU_B_TBL_SIZE * NUM_B_TABLES;
753 mmuBbase = pmap_bootstrap_alloc(size);
754
755 size = sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE * NUM_A_TABLES;
756 mmuAbase = pmap_bootstrap_alloc(size);
757
758 /*
759 * Fill in the never-changing part of the kernel tables.
760 * For simplicity, the kernel's mappings will be editable as a
761 * flat array of page table entries at kernCbase. The
762 * higher level 'A' and 'B' tables must be initialized to point
763 * to this lower one.
764 */
765 b = c = 0;
766
767 /*
768 * Invalidate all mappings below KERNBASE in the A table.
769 * This area has already been zeroed out, but it is good
770 * practice to explicitly show that we are interpreting
771 * it as a list of A table descriptors.
772 */
773 for (i = 0; i < MMU_TIA(KERNBASE); i++) {
774 kernAbase[i].addr.raw = 0;
775 }
776
777 /*
778 * Set up the kernel A and B tables so that they will reference the
779 * correct spots in the contiguous table of PTEs allocated for the
780 * kernel's virtual memory space.
781 */
782 for (i = MMU_TIA(KERNBASE); i < MMU_A_TBL_SIZE; i++) {
783 kernAbase[i].attr.raw =
784 MMU_LONG_DTE_LU | MMU_LONG_DTE_SUPV | MMU_DT_SHORT;
785 kernAbase[i].addr.raw = mmu_vtop(&kernBbase[b]);
786
787 for (j=0; j < MMU_B_TBL_SIZE; j++) {
788 kernBbase[b + j].attr.raw = mmu_vtop(&kernCbase[c])
789 | MMU_DT_SHORT;
790 c += MMU_C_TBL_SIZE;
791 }
792 b += MMU_B_TBL_SIZE;
793 }
794
795 /* XXX - Doing kernel_pmap a little further down. */
796
797 pmap_alloc_usermmu(); /* Allocate user MMU tables. */
798 pmap_alloc_usertmgr(); /* Allocate user MMU table managers.*/
799 pmap_alloc_pv(); /* Allocate physical->virtual map. */
800
801 /*
802 * We are now done with pmap_bootstrap_alloc(). Round up
803 * `virtual_avail' to the nearest page, and set the flag
804 * to prevent use of pmap_bootstrap_alloc() hereafter.
805 */
806 pmap_bootstrap_aalign(NBPG);
807 bootstrap_alloc_enabled = FALSE;
808
809 /*
810 * Now that we are done with pmap_bootstrap_alloc(), we
811 * must save the virtual and physical addresses of the
812 * end of the linearly mapped range, which are stored in
813 * virtual_contig_end and avail_start, respectively.
814 * These variables will never change after this point.
815 */
816 virtual_contig_end = virtual_avail;
817 avail_start = virtual_avail - KERNBASE;
818
819 /*
820 * `avail_next' is a running pointer used by pmap_next_page() to
821 * keep track of the next available physical page to be handed
822 * to the VM system during its initialization, in which it
823 * asks for physical pages, one at a time.
824 */
825 avail_next = avail_start;
826
827 /*
828 * Now allocate some virtual addresses, but not the physical pages
829 * behind them. Note that virtual_avail is already page-aligned.
830 *
831 * tmp_vpages[] is an array of two virtual pages used for temporary
832 * kernel mappings in the pmap module to facilitate various physical
833 * address-oritented operations.
834 */
835 tmp_vpages[0] = virtual_avail;
836 virtual_avail += NBPG;
837 tmp_vpages[1] = virtual_avail;
838 virtual_avail += NBPG;
839
840 /** Initialize the PV system **/
841 pmap_init_pv();
842
843 /*
844 * Fill in the kernel_pmap structure and kernel_crp.
845 */
846 kernAphys = mmu_vtop(kernAbase);
847 kernel_pmap.pm_a_tmgr = NULL;
848 kernel_pmap.pm_a_phys = kernAphys;
849 kernel_pmap.pm_refcount = 1; /* always in use */
850
851 kernel_crp.rp_attr = MMU_LONG_DTE_LU | MMU_DT_LONG;
852 kernel_crp.rp_addr = kernAphys;
853
854 /*
855 * Now pmap_enter_kernel() may be used safely and will be
856 * the main interface used hereafter to modify the kernel's
857 * virtual address space. Note that since we are still running
858 * under the PROM's address table, none of these table modifications
859 * actually take effect until pmap_takeover_mmu() is called.
860 *
861 * Note: Our tables do NOT have the PROM linear mappings!
862 * Only the mappings created here exist in our tables, so
863 * remember to map anything we expect to use.
864 */
865 va = (vm_offset_t) KERNBASE;
866 pa = 0;
867
868 /*
869 * The first page of the kernel virtual address space is the msgbuf
870 * page. The page attributes (data, non-cached) are set here, while
871 * the address is assigned to this global pointer in cpu_startup().
872 * XXX - Make it non-cached?
873 */
874 pmap_enter_kernel(va, pa|PMAP_NC, VM_PROT_ALL);
875 va += NBPG; pa += NBPG;
876
877 /* Next page is used as the temporary stack. */
878 pmap_enter_kernel(va, pa, VM_PROT_ALL);
879 va += NBPG; pa += NBPG;
880
881 /*
882 * Map all of the kernel's text segment as read-only and cacheable.
883 * (Cacheable is implied by default). Unfortunately, the last bytes
884 * of kernel text and the first bytes of kernel data will often be
885 * sharing the same page. Therefore, the last page of kernel text
886 * has to be mapped as read/write, to accomodate the data.
887 */
888 eva = _trunc_page((vm_offset_t)etext);
889 for (; va < eva; va += NBPG, pa += NBPG)
890 pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_EXECUTE);
891
892 /*
893 * Map all of the kernel's data as read/write and cacheable.
894 * This includes: data, BSS, symbols, and everything in the
895 * contiguous memory used by pmap_bootstrap_alloc()
896 */
897 for (; pa < avail_start; va += NBPG, pa += NBPG)
898 pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_WRITE);
899
900 /*
901 * At this point we are almost ready to take over the MMU. But first
902 * we must save the PROM's address space in our map, as we call its
903 * routines and make references to its data later in the kernel.
904 */
905 pmap_bootstrap_copyprom();
906 pmap_takeover_mmu();
907 pmap_bootstrap_setprom();
908
909 /* Notify the VM system of our page size. */
910 PAGE_SIZE = NBPG;
911 vm_set_page_size();
912 }
913
914
915 /* pmap_alloc_usermmu INTERNAL
916 **
917 * Called from pmap_bootstrap() to allocate MMU tables that will
918 * eventually be used for user mappings.
919 */
920 void
921 pmap_alloc_usermmu()
922 {
923 /* XXX: Moved into caller. */
924 }
925
926 /* pmap_alloc_pv INTERNAL
927 **
928 * Called from pmap_bootstrap() to allocate the physical
929 * to virtual mapping list. Each physical page of memory
930 * in the system has a corresponding element in this list.
931 */
932 void
933 pmap_alloc_pv()
934 {
935 int i;
936 unsigned int total_mem;
937
938 /*
939 * Allocate a pv_head structure for every page of physical
940 * memory that will be managed by the system. Since memory on
941 * the 3/80 is non-contiguous, we cannot arrive at a total page
942 * count by subtraction of the lowest available address from the
943 * highest, but rather we have to step through each memory
944 * bank and add the number of pages in each to the total.
945 *
946 * At this time we also initialize the offset of each bank's
947 * starting pv_head within the pv_head list so that the physical
948 * memory state routines (pmap_is_referenced(),
949 * pmap_is_modified(), et al.) can quickly find coresponding
950 * pv_heads in spite of the non-contiguity.
951 */
952 total_mem = 0;
953 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) {
954 avail_mem[i].pmem_pvbase = _btop(total_mem);
955 total_mem += avail_mem[i].pmem_end -
956 avail_mem[i].pmem_start;
957 if (avail_mem[i].pmem_next == NULL)
958 break;
959 }
960 #ifdef PMAP_DEBUG
961 if (total_mem != total_phys_mem)
962 panic("pmap_alloc_pv did not arrive at correct page count");
963 #endif
964
965 pvbase = (pv_t *) pmap_bootstrap_alloc(sizeof(pv_t) *
966 _btop(total_phys_mem));
967 }
968
969 /* pmap_alloc_usertmgr INTERNAL
970 **
971 * Called from pmap_bootstrap() to allocate the structures which
972 * facilitate management of user MMU tables. Each user MMU table
973 * in the system has one such structure associated with it.
974 */
975 void
976 pmap_alloc_usertmgr()
977 {
978 /* Allocate user MMU table managers */
979 /* It would be a lot simpler to just make these BSS, but */
980 /* we may want to change their size at boot time... -j */
981 Atmgrbase = (a_tmgr_t *) pmap_bootstrap_alloc(sizeof(a_tmgr_t)
982 * NUM_A_TABLES);
983 Btmgrbase = (b_tmgr_t *) pmap_bootstrap_alloc(sizeof(b_tmgr_t)
984 * NUM_B_TABLES);
985 Ctmgrbase = (c_tmgr_t *) pmap_bootstrap_alloc(sizeof(c_tmgr_t)
986 * NUM_C_TABLES);
987
988 /*
989 * Allocate PV list elements for the physical to virtual
990 * mapping system.
991 */
992 pvebase = (pv_elem_t *) pmap_bootstrap_alloc(
993 sizeof(pv_elem_t) * (NUM_USER_PTES + NUM_KERN_PTES));
994 }
995
996 /* pmap_bootstrap_copyprom() INTERNAL
997 **
998 * Copy the PROM mappings into our own tables. Note, we
999 * can use physical addresses until __bootstrap returns.
1000 */
1001 void
1002 pmap_bootstrap_copyprom()
1003 {
1004 MachMonRomVector *romp;
1005 int *mon_ctbl;
1006 mmu_short_pte_t *kpte;
1007 int i, len;
1008
1009 romp = romVectorPtr;
1010
1011 /*
1012 * Copy the mappings in MON_KDB_START...MONEND
1013 * Note: mon_ctbl[0] maps MON_KDB_START
1014 */
1015 mon_ctbl = *romp->monptaddr;
1016 i = _btop(MON_KDB_START - KERNBASE);
1017 kpte = &kernCbase[i];
1018 len = _btop(MONEND - MON_KDB_START);
1019
1020 for (i = 0; i < len; i++) {
1021 kpte[i].attr.raw = mon_ctbl[i];
1022 }
1023
1024 /*
1025 * Copy the mappings at MON_DVMA_BASE (to the end).
1026 * Note, in here, mon_ctbl[0] maps MON_DVMA_BASE.
1027 * XXX - This does not appear to be necessary, but
1028 * I'm not sure yet if it is or not. -gwr
1029 */
1030 mon_ctbl = *romp->shadowpteaddr;
1031 i = _btop(MON_DVMA_BASE - KERNBASE);
1032 kpte = &kernCbase[i];
1033 len = _btop(MON_DVMA_SIZE);
1034
1035 for (i = 0; i < len; i++) {
1036 kpte[i].attr.raw = mon_ctbl[i];
1037 }
1038 }
1039
1040 /* pmap_takeover_mmu INTERNAL
1041 **
1042 * Called from pmap_bootstrap() after it has copied enough of the
1043 * PROM mappings into the kernel map so that we can use our own
1044 * MMU table.
1045 */
1046 void
1047 pmap_takeover_mmu()
1048 {
1049
1050 loadcrp(&kernel_crp);
1051 }
1052
1053 /* pmap_bootstrap_setprom() INTERNAL
1054 **
1055 * Set the PROM mappings so it can see kernel space.
1056 * Note that physical addresses are used here, which
1057 * we can get away with because this runs with the
1058 * low 1GB set for transparent translation.
1059 */
1060 void
1061 pmap_bootstrap_setprom()
1062 {
1063 mmu_long_dte_t *mon_dte;
1064 extern struct mmu_rootptr mon_crp;
1065 int i;
1066
1067 mon_dte = (mmu_long_dte_t *) mon_crp.rp_addr;
1068 for (i = MMU_TIA(KERNBASE); i < MMU_TIA(KERN_END); i++) {
1069 mon_dte[i].attr.raw = kernAbase[i].attr.raw;
1070 mon_dte[i].addr.raw = kernAbase[i].addr.raw;
1071 }
1072 }
1073
1074
1075 /* pmap_init INTERFACE
1076 **
1077 * Called at the end of vm_init() to set up the pmap system to go
1078 * into full time operation. All initialization of kernel_pmap
1079 * should be already done by now, so this should just do things
1080 * needed for user-level pmaps to work.
1081 */
1082 void
1083 pmap_init()
1084 {
1085 /** Initialize the manager pools **/
1086 TAILQ_INIT(&a_pool);
1087 TAILQ_INIT(&b_pool);
1088 TAILQ_INIT(&c_pool);
1089
1090 /**************************************************************
1091 * Initialize all tmgr structures and MMU tables they manage. *
1092 **************************************************************/
1093 /** Initialize A tables **/
1094 pmap_init_a_tables();
1095 /** Initialize B tables **/
1096 pmap_init_b_tables();
1097 /** Initialize C tables **/
1098 pmap_init_c_tables();
1099 }
1100
1101 /* pmap_init_a_tables() INTERNAL
1102 **
1103 * Initializes all A managers, their MMU A tables, and inserts
1104 * them into the A manager pool for use by the system.
1105 */
1106 void
1107 pmap_init_a_tables()
1108 {
1109 int i;
1110 a_tmgr_t *a_tbl;
1111
1112 for (i=0; i < NUM_A_TABLES; i++) {
1113 /* Select the next available A manager from the pool */
1114 a_tbl = &Atmgrbase[i];
1115
1116 /*
1117 * Clear its parent entry. Set its wired and valid
1118 * entry count to zero.
1119 */
1120 a_tbl->at_parent = NULL;
1121 a_tbl->at_wcnt = a_tbl->at_ecnt = 0;
1122
1123 /* Assign it the next available MMU A table from the pool */
1124 a_tbl->at_dtbl = &mmuAbase[i * MMU_A_TBL_SIZE];
1125
1126 /*
1127 * Initialize the MMU A table with the table in the `proc0',
1128 * or kernel, mapping. This ensures that every process has
1129 * the kernel mapped in the top part of its address space.
1130 */
1131 bcopy(kernAbase, a_tbl->at_dtbl, MMU_A_TBL_SIZE *
1132 sizeof(mmu_long_dte_t));
1133
1134 /*
1135 * Finally, insert the manager into the A pool,
1136 * making it ready to be used by the system.
1137 */
1138 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
1139 }
1140 }
1141
1142 /* pmap_init_b_tables() INTERNAL
1143 **
1144 * Initializes all B table managers, their MMU B tables, and
1145 * inserts them into the B manager pool for use by the system.
1146 */
1147 void
1148 pmap_init_b_tables()
1149 {
1150 int i,j;
1151 b_tmgr_t *b_tbl;
1152
1153 for (i=0; i < NUM_B_TABLES; i++) {
1154 /* Select the next available B manager from the pool */
1155 b_tbl = &Btmgrbase[i];
1156
1157 b_tbl->bt_parent = NULL; /* clear its parent, */
1158 b_tbl->bt_pidx = 0; /* parent index, */
1159 b_tbl->bt_wcnt = 0; /* wired entry count, */
1160 b_tbl->bt_ecnt = 0; /* valid entry count. */
1161
1162 /* Assign it the next available MMU B table from the pool */
1163 b_tbl->bt_dtbl = &mmuBbase[i * MMU_B_TBL_SIZE];
1164
1165 /* Invalidate every descriptor in the table */
1166 for (j=0; j < MMU_B_TBL_SIZE; j++)
1167 b_tbl->bt_dtbl[j].attr.raw = MMU_DT_INVALID;
1168
1169 /* Insert the manager into the B pool */
1170 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
1171 }
1172 }
1173
1174 /* pmap_init_c_tables() INTERNAL
1175 **
1176 * Initializes all C table managers, their MMU C tables, and
1177 * inserts them into the C manager pool for use by the system.
1178 */
1179 void
1180 pmap_init_c_tables()
1181 {
1182 int i,j;
1183 c_tmgr_t *c_tbl;
1184
1185 for (i=0; i < NUM_C_TABLES; i++) {
1186 /* Select the next available C manager from the pool */
1187 c_tbl = &Ctmgrbase[i];
1188
1189 c_tbl->ct_parent = NULL; /* clear its parent, */
1190 c_tbl->ct_pidx = 0; /* parent index, */
1191 c_tbl->ct_wcnt = 0; /* wired entry count, */
1192 c_tbl->ct_ecnt = 0; /* valid entry count. */
1193
1194 /* Assign it the next available MMU C table from the pool */
1195 c_tbl->ct_dtbl = &mmuCbase[i * MMU_C_TBL_SIZE];
1196
1197 for (j=0; j < MMU_C_TBL_SIZE; j++)
1198 c_tbl->ct_dtbl[j].attr.raw = MMU_DT_INVALID;
1199
1200 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
1201 }
1202 }
1203
1204 /* pmap_init_pv() INTERNAL
1205 **
1206 * Initializes the Physical to Virtual mapping system.
1207 */
1208 void
1209 pmap_init_pv()
1210 {
1211 int i;
1212
1213 /* Initialize every PV head. */
1214 for (i = 0; i < _btop(total_phys_mem); i++) {
1215 pvbase[i].pv_idx = PVE_EOL; /* Indicate no mappings */
1216 pvbase[i].pv_flags = 0; /* Zero out page flags */
1217 }
1218
1219 pv_initialized = TRUE;
1220 }
1221
1222 /* get_a_table INTERNAL
1223 **
1224 * Retrieve and return a level A table for use in a user map.
1225 */
1226 a_tmgr_t *
1227 get_a_table()
1228 {
1229 a_tmgr_t *tbl;
1230 pmap_t pmap;
1231
1232 /* Get the top A table in the pool */
1233 tbl = a_pool.tqh_first;
1234 if (tbl == NULL) {
1235 /*
1236 * XXX - Instead of panicing here and in other get_x_table
1237 * functions, we do have the option of sleeping on the head of
1238 * the table pool. Any function which updates the table pool
1239 * would then issue a wakeup() on the head, thus waking up any
1240 * processes waiting for a table.
1241 *
1242 * Actually, the place to sleep would be when some process
1243 * asks for a "wired" mapping that would run us short of
1244 * mapping resources. This design DEPENDS on always having
1245 * some mapping resources in the pool for stealing, so we
1246 * must make sure we NEVER let the pool become empty. -gwr
1247 */
1248 panic("get_a_table: out of A tables.");
1249 }
1250
1251 TAILQ_REMOVE(&a_pool, tbl, at_link);
1252 /*
1253 * If the table has a non-null parent pointer then it is in use.
1254 * Forcibly abduct it from its parent and clear its entries.
1255 * No re-entrancy worries here. This table would not be in the
1256 * table pool unless it was available for use.
1257 *
1258 * Note that the second argument to free_a_table() is FALSE. This
1259 * indicates that the table should not be relinked into the A table
1260 * pool. That is a job for the function that called us.
1261 */
1262 if (tbl->at_parent) {
1263 pmap = tbl->at_parent;
1264 free_a_table(tbl, FALSE);
1265 pmap->pm_a_tmgr = NULL;
1266 pmap->pm_a_phys = kernAphys;
1267 }
1268 #ifdef NON_REENTRANT
1269 /*
1270 * If the table isn't to be wired down, re-insert it at the
1271 * end of the pool.
1272 */
1273 if (!wired)
1274 /*
1275 * Quandary - XXX
1276 * Would it be better to let the calling function insert this
1277 * table into the queue? By inserting it here, we are allowing
1278 * it to be stolen immediately. The calling function is
1279 * probably not expecting to use a table that it is not
1280 * assured full control of.
1281 * Answer - In the intrest of re-entrancy, it is best to let
1282 * the calling function determine when a table is available
1283 * for use. Therefore this code block is not used.
1284 */
1285 TAILQ_INSERT_TAIL(&a_pool, tbl, at_link);
1286 #endif /* NON_REENTRANT */
1287 return tbl;
1288 }
1289
1290 /* get_b_table INTERNAL
1291 **
1292 * Return a level B table for use.
1293 */
1294 b_tmgr_t *
1295 get_b_table()
1296 {
1297 b_tmgr_t *tbl;
1298
1299 /* See 'get_a_table' for comments. */
1300 tbl = b_pool.tqh_first;
1301 if (tbl == NULL)
1302 panic("get_b_table: out of B tables.");
1303 TAILQ_REMOVE(&b_pool, tbl, bt_link);
1304 if (tbl->bt_parent) {
1305 tbl->bt_parent->at_dtbl[tbl->bt_pidx].attr.raw = MMU_DT_INVALID;
1306 tbl->bt_parent->at_ecnt--;
1307 free_b_table(tbl, FALSE);
1308 }
1309 #ifdef NON_REENTRANT
1310 if (!wired)
1311 /* XXX see quandary in get_b_table */
1312 /* XXX start lock */
1313 TAILQ_INSERT_TAIL(&b_pool, tbl, bt_link);
1314 /* XXX end lock */
1315 #endif /* NON_REENTRANT */
1316 return tbl;
1317 }
1318
1319 /* get_c_table INTERNAL
1320 **
1321 * Return a level C table for use.
1322 */
1323 c_tmgr_t *
1324 get_c_table()
1325 {
1326 c_tmgr_t *tbl;
1327
1328 /* See 'get_a_table' for comments */
1329 tbl = c_pool.tqh_first;
1330 if (tbl == NULL)
1331 panic("get_c_table: out of C tables.");
1332 TAILQ_REMOVE(&c_pool, tbl, ct_link);
1333 if (tbl->ct_parent) {
1334 tbl->ct_parent->bt_dtbl[tbl->ct_pidx].attr.raw = MMU_DT_INVALID;
1335 tbl->ct_parent->bt_ecnt--;
1336 free_c_table(tbl, FALSE);
1337 }
1338 #ifdef NON_REENTRANT
1339 if (!wired)
1340 /* XXX See quandary in get_a_table */
1341 /* XXX start lock */
1342 TAILQ_INSERT_TAIL(&c_pool, tbl, c_link);
1343 /* XXX end lock */
1344 #endif /* NON_REENTRANT */
1345
1346 return tbl;
1347 }
1348
1349 /*
1350 * The following 'free_table' and 'steal_table' functions are called to
1351 * detach tables from their current obligations (parents and children) and
1352 * prepare them for reuse in another mapping.
1353 *
1354 * Free_table is used when the calling function will handle the fate
1355 * of the parent table, such as returning it to the free pool when it has
1356 * no valid entries. Functions that do not want to handle this should
1357 * call steal_table, in which the parent table's descriptors and entry
1358 * count are automatically modified when this table is removed.
1359 */
1360
1361 /* free_a_table INTERNAL
1362 **
1363 * Unmaps the given A table and all child tables from their current
1364 * mappings. Returns the number of pages that were invalidated.
1365 * If 'relink' is true, the function will return the table to the head
1366 * of the available table pool.
1367 *
1368 * Cache note: The MC68851 will automatically flush all
1369 * descriptors derived from a given A table from its
1370 * Automatic Translation Cache (ATC) if we issue a
1371 * 'PFLUSHR' instruction with the base address of the
1372 * table. This function should do, and does so.
1373 * Note note: We are using an MC68030 - there is no
1374 * PFLUSHR.
1375 */
1376 int
1377 free_a_table(a_tbl, relink)
1378 a_tmgr_t *a_tbl;
1379 boolean_t relink;
1380 {
1381 int i, removed_cnt;
1382 mmu_long_dte_t *dte;
1383 mmu_short_dte_t *dtbl;
1384 b_tmgr_t *tmgr;
1385
1386 /*
1387 * Flush the ATC cache of all cached descriptors derived
1388 * from this table.
1389 * Sun3x does not use 68851's cached table feature
1390 * flush_atc_crp(mmu_vtop(a_tbl->dte));
1391 */
1392
1393 /*
1394 * Remove any pending cache flushes that were designated
1395 * for the pmap this A table belongs to.
1396 * a_tbl->parent->atc_flushq[0] = 0;
1397 * Not implemented in sun3x.
1398 */
1399
1400 /*
1401 * All A tables in the system should retain a map for the
1402 * kernel. If the table contains any valid descriptors
1403 * (other than those for the kernel area), invalidate them all,
1404 * stopping short of the kernel's entries.
1405 */
1406 removed_cnt = 0;
1407 if (a_tbl->at_ecnt) {
1408 dte = a_tbl->at_dtbl;
1409 for (i=0; i < MMU_TIA(KERNBASE); i++) {
1410 /*
1411 * If a table entry points to a valid B table, free
1412 * it and its children.
1413 */
1414 if (MMU_VALID_DT(dte[i])) {
1415 /*
1416 * The following block does several things,
1417 * from innermost expression to the
1418 * outermost:
1419 * 1) It extracts the base (cc 1996)
1420 * address of the B table pointed
1421 * to in the A table entry dte[i].
1422 * 2) It converts this base address into
1423 * the virtual address it can be
1424 * accessed with. (all MMU tables point
1425 * to physical addresses.)
1426 * 3) It finds the corresponding manager
1427 * structure which manages this MMU table.
1428 * 4) It frees the manager structure.
1429 * (This frees the MMU table and all
1430 * child tables. See 'free_b_table' for
1431 * details.)
1432 */
1433 dtbl = mmu_ptov(dte[i].addr.raw);
1434 tmgr = mmuB2tmgr(dtbl);
1435 removed_cnt += free_b_table(tmgr, TRUE);
1436 dte[i].attr.raw = MMU_DT_INVALID;
1437 }
1438 }
1439 a_tbl->at_ecnt = 0;
1440 }
1441 if (relink) {
1442 a_tbl->at_parent = NULL;
1443 TAILQ_REMOVE(&a_pool, a_tbl, at_link);
1444 TAILQ_INSERT_HEAD(&a_pool, a_tbl, at_link);
1445 }
1446 return removed_cnt;
1447 }
1448
1449 /* free_b_table INTERNAL
1450 **
1451 * Unmaps the given B table and all its children from their current
1452 * mappings. Returns the number of pages that were invalidated.
1453 * (For comments, see 'free_a_table()').
1454 */
1455 int
1456 free_b_table(b_tbl, relink)
1457 b_tmgr_t *b_tbl;
1458 boolean_t relink;
1459 {
1460 int i, removed_cnt;
1461 mmu_short_dte_t *dte;
1462 mmu_short_pte_t *dtbl;
1463 c_tmgr_t *tmgr;
1464
1465 removed_cnt = 0;
1466 if (b_tbl->bt_ecnt) {
1467 dte = b_tbl->bt_dtbl;
1468 for (i=0; i < MMU_B_TBL_SIZE; i++) {
1469 if (MMU_VALID_DT(dte[i])) {
1470 dtbl = mmu_ptov(MMU_DTE_PA(dte[i]));
1471 tmgr = mmuC2tmgr(dtbl);
1472 removed_cnt += free_c_table(tmgr, TRUE);
1473 dte[i].attr.raw = MMU_DT_INVALID;
1474 }
1475 }
1476 b_tbl->bt_ecnt = 0;
1477 }
1478
1479 if (relink) {
1480 b_tbl->bt_parent = NULL;
1481 TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
1482 TAILQ_INSERT_HEAD(&b_pool, b_tbl, bt_link);
1483 }
1484 return removed_cnt;
1485 }
1486
1487 /* free_c_table INTERNAL
1488 **
1489 * Unmaps the given C table from use and returns it to the pool for
1490 * re-use. Returns the number of pages that were invalidated.
1491 *
1492 * This function preserves any physical page modification information
1493 * contained in the page descriptors within the C table by calling
1494 * 'pmap_remove_pte().'
1495 */
1496 int
1497 free_c_table(c_tbl, relink)
1498 c_tmgr_t *c_tbl;
1499 boolean_t relink;
1500 {
1501 int i, removed_cnt;
1502
1503 removed_cnt = 0;
1504 if (c_tbl->ct_ecnt) {
1505 for (i=0; i < MMU_C_TBL_SIZE; i++) {
1506 if (MMU_VALID_DT(c_tbl->ct_dtbl[i])) {
1507 pmap_remove_pte(&c_tbl->ct_dtbl[i]);
1508 removed_cnt++;
1509 }
1510 }
1511 c_tbl->ct_ecnt = 0;
1512 }
1513
1514 if (relink) {
1515 c_tbl->ct_parent = NULL;
1516 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
1517 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
1518 }
1519 return removed_cnt;
1520 }
1521
1522 #if 0
1523 /* free_c_table_novalid INTERNAL
1524 **
1525 * Frees the given C table manager without checking to see whether
1526 * or not it contains any valid page descriptors as it is assumed
1527 * that it does not.
1528 */
1529 void
1530 free_c_table_novalid(c_tbl)
1531 c_tmgr_t *c_tbl;
1532 {
1533 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
1534 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
1535 c_tbl->ct_parent->bt_dtbl[c_tbl->ct_pidx].attr.raw = MMU_DT_INVALID;
1536 c_tbl->ct_parent->bt_ecnt--;
1537 /*
1538 * XXX - Should call equiv. of 'free_b_table_novalid' here if
1539 * we just removed the last entry of the parent B table.
1540 * But I want to insure that this will not endanger pmap_enter()
1541 * with sudden removal of tables it is working with.
1542 *
1543 * We should probably add another field to each table, indicating
1544 * whether or not it is 'locked', ie. in the process of being
1545 * modified.
1546 */
1547 c_tbl->ct_parent = NULL;
1548 }
1549 #endif
1550
1551 /* pmap_remove_pte INTERNAL
1552 **
1553 * Unmap the given pte and preserve any page modification
1554 * information by transfering it to the pv head of the
1555 * physical page it maps to. This function does not update
1556 * any reference counts because it is assumed that the calling
1557 * function will do so.
1558 */
1559 void
1560 pmap_remove_pte(pte)
1561 mmu_short_pte_t *pte;
1562 {
1563 u_short pv_idx, targ_idx;
1564 int s;
1565 vm_offset_t pa;
1566 pv_t *pv;
1567
1568 pa = MMU_PTE_PA(*pte);
1569 if (is_managed(pa)) {
1570 pv = pa2pv(pa);
1571 targ_idx = pteidx(pte); /* Index of PTE being removed */
1572
1573 /*
1574 * If the PTE being removed is the first (or only) PTE in
1575 * the list of PTEs currently mapped to this page, remove the
1576 * PTE by changing the index found on the PV head. Otherwise
1577 * a linear search through the list will have to be executed
1578 * in order to find the PVE which points to the PTE being
1579 * removed, so that it may be modified to point to its new
1580 * neighbor.
1581 */
1582 s = splimp();
1583 pv_idx = pv->pv_idx; /* Index of first PTE in PV list */
1584 if (pv_idx == targ_idx) {
1585 pv->pv_idx = pvebase[targ_idx].pve_next;
1586 } else {
1587 /*
1588 * Find the PV element which points to the target
1589 * element.
1590 */
1591 while (pvebase[pv_idx].pve_next != targ_idx) {
1592 pv_idx = pvebase[pv_idx].pve_next;
1593 #ifdef DIAGNOSTIC
1594 if (pv_idx == PVE_EOL)
1595 panic("pmap_remove_pte: pv list end!");
1596 #endif
1597 }
1598
1599 /*
1600 * At this point, pv_idx is the index of the PV
1601 * element just before the target element in the list.
1602 * Unlink the target.
1603 */
1604 pvebase[pv_idx].pve_next = pvebase[targ_idx].pve_next;
1605 }
1606 /*
1607 * Save the mod/ref bits of the pte by simply
1608 * ORing the entire pte onto the pv_flags member
1609 * of the pv structure.
1610 * There is no need to use a separate bit pattern
1611 * for usage information on the pv head than that
1612 * which is used on the MMU ptes.
1613 */
1614 pv->pv_flags |= (u_short) pte->attr.raw;
1615 splx(s);
1616 }
1617
1618 pte->attr.raw = MMU_DT_INVALID;
1619 }
1620
1621 /* pmap_stroll INTERNAL
1622 **
1623 * Retrieve the addresses of all table managers involved in the mapping of
1624 * the given virtual address. If the table walk completed sucessfully,
1625 * return TRUE. If it was only partially sucessful, return FALSE.
1626 * The table walk performed by this function is important to many other
1627 * functions in this module.
1628 *
1629 * Note: This function ought to be easier to read.
1630 */
1631 boolean_t
1632 pmap_stroll(pmap, va, a_tbl, b_tbl, c_tbl, pte, a_idx, b_idx, pte_idx)
1633 pmap_t pmap;
1634 vm_offset_t va;
1635 a_tmgr_t **a_tbl;
1636 b_tmgr_t **b_tbl;
1637 c_tmgr_t **c_tbl;
1638 mmu_short_pte_t **pte;
1639 int *a_idx, *b_idx, *pte_idx;
1640 {
1641 mmu_long_dte_t *a_dte; /* A: long descriptor table */
1642 mmu_short_dte_t *b_dte; /* B: short descriptor table */
1643
1644 if (pmap == pmap_kernel())
1645 return FALSE;
1646
1647 /* Does the given pmap have its own A table? */
1648 *a_tbl = pmap->pm_a_tmgr;
1649 if (*a_tbl == NULL)
1650 return FALSE; /* No. Return unknown. */
1651 /* Does the A table have a valid B table
1652 * under the corresponding table entry?
1653 */
1654 *a_idx = MMU_TIA(va);
1655 a_dte = &((*a_tbl)->at_dtbl[*a_idx]);
1656 if (!MMU_VALID_DT(*a_dte))
1657 return FALSE; /* No. Return unknown. */
1658 /* Yes. Extract B table from the A table. */
1659 *b_tbl = mmuB2tmgr(mmu_ptov(a_dte->addr.raw));
1660 /* Does the B table have a valid C table
1661 * under the corresponding table entry?
1662 */
1663 *b_idx = MMU_TIB(va);
1664 b_dte = &((*b_tbl)->bt_dtbl[*b_idx]);
1665 if (!MMU_VALID_DT(*b_dte))
1666 return FALSE; /* No. Return unknown. */
1667 /* Yes. Extract C table from the B table. */
1668 *c_tbl = mmuC2tmgr(mmu_ptov(MMU_DTE_PA(*b_dte)));
1669 *pte_idx = MMU_TIC(va);
1670 *pte = &((*c_tbl)->ct_dtbl[*pte_idx]);
1671
1672 return TRUE;
1673 }
1674
1675 /* pmap_enter INTERFACE
1676 **
1677 * Called by the kernel to map a virtual address
1678 * to a physical address in the given process map.
1679 *
1680 * Note: this function should apply an exclusive lock
1681 * on the pmap system for its duration. (it certainly
1682 * would save my hair!!)
1683 * This function ought to be easier to read.
1684 */
1685 void
1686 pmap_enter(pmap, va, pa, prot, wired)
1687 pmap_t pmap;
1688 vm_offset_t va;
1689 vm_offset_t pa;
1690 vm_prot_t prot;
1691 boolean_t wired;
1692 {
1693 boolean_t insert, managed; /* Marks the need for PV insertion.*/
1694 u_short nidx; /* PV list index */
1695 int s; /* Used for splimp()/splx() */
1696 int flags; /* Mapping flags. eg. Cache inhibit */
1697 u_int a_idx, b_idx, pte_idx; /* table indices */
1698 a_tmgr_t *a_tbl; /* A: long descriptor table manager */
1699 b_tmgr_t *b_tbl; /* B: short descriptor table manager */
1700 c_tmgr_t *c_tbl; /* C: short page table manager */
1701 mmu_long_dte_t *a_dte; /* A: long descriptor table */
1702 mmu_short_dte_t *b_dte; /* B: short descriptor table */
1703 mmu_short_pte_t *c_pte; /* C: short page descriptor table */
1704 pv_t *pv; /* pv list head */
1705 enum {NONE, NEWA, NEWB, NEWC} llevel; /* used at end */
1706
1707 if (pmap == NULL)
1708 return;
1709 if (pmap == pmap_kernel()) {
1710 pmap_enter_kernel(va, pa, prot);
1711 return;
1712 }
1713
1714 flags = (pa & ~MMU_PAGE_MASK);
1715 pa &= MMU_PAGE_MASK;
1716
1717 /*
1718 * Determine if the physical address being mapped is on-board RAM.
1719 * Any other area of the address space is likely to belong to a
1720 * device and hence it would be disasterous to cache its contents.
1721 */
1722 if ((managed = is_managed(pa)) == FALSE)
1723 flags |= PMAP_NC;
1724
1725 /*
1726 * For user mappings we walk along the MMU tables of the given
1727 * pmap, reaching a PTE which describes the virtual page being
1728 * mapped or changed. If any level of the walk ends in an invalid
1729 * entry, a table must be allocated and the entry must be updated
1730 * to point to it.
1731 * There is a bit of confusion as to whether this code must be
1732 * re-entrant. For now we will assume it is. To support
1733 * re-entrancy we must unlink tables from the table pool before
1734 * we assume we may use them. Tables are re-linked into the pool
1735 * when we are finished with them at the end of the function.
1736 * But I don't feel like doing that until we have proof that this
1737 * needs to be re-entrant.
1738 * 'llevel' records which tables need to be relinked.
1739 */
1740 llevel = NONE;
1741
1742 /*
1743 * Step 1 - Retrieve the A table from the pmap. If it has no
1744 * A table, allocate a new one from the available pool.
1745 */
1746
1747 a_tbl = pmap->pm_a_tmgr;
1748 if (a_tbl == NULL) {
1749 /*
1750 * This pmap does not currently have an A table. Allocate
1751 * a new one.
1752 */
1753 a_tbl = get_a_table();
1754 a_tbl->at_parent = pmap;
1755
1756 /*
1757 * Assign this new A table to the pmap, and calculate its
1758 * physical address so that loadcrp() can be used to make
1759 * the table active.
1760 */
1761 pmap->pm_a_tmgr = a_tbl;
1762 pmap->pm_a_phys = mmu_vtop(a_tbl->at_dtbl);
1763
1764 /*
1765 * If the process receiving a new A table is the current
1766 * process, we are responsible for setting the MMU so that
1767 * it becomes the current address space. This only adds
1768 * new mappings, so no need to flush anything.
1769 */
1770 if (pmap == current_pmap()) {
1771 kernel_crp.rp_addr = pmap->pm_a_phys;
1772 loadcrp(&kernel_crp);
1773 }
1774
1775 if (!wired)
1776 llevel = NEWA;
1777 } else {
1778 /*
1779 * Use the A table already allocated for this pmap.
1780 * Unlink it from the A table pool if necessary.
1781 */
1782 if (wired && !a_tbl->at_wcnt)
1783 TAILQ_REMOVE(&a_pool, a_tbl, at_link);
1784 }
1785
1786 /*
1787 * Step 2 - Walk into the B table. If there is no valid B table,
1788 * allocate one.
1789 */
1790
1791 a_idx = MMU_TIA(va); /* Calculate the TIA of the VA. */
1792 a_dte = &a_tbl->at_dtbl[a_idx]; /* Retrieve descriptor from table */
1793 if (MMU_VALID_DT(*a_dte)) { /* Is the descriptor valid? */
1794 /* The descriptor is valid. Use the B table it points to. */
1795 /*************************************
1796 * a_idx *
1797 * v *
1798 * a_tbl -> +-+-+-+-+-+-+-+-+-+-+-+- *
1799 * | | | | | | | | | | | | *
1800 * +-+-+-+-+-+-+-+-+-+-+-+- *
1801 * | *
1802 * \- b_tbl -> +-+- *
1803 * | | *
1804 * +-+- *
1805 *************************************/
1806 b_dte = mmu_ptov(a_dte->addr.raw);
1807 b_tbl = mmuB2tmgr(b_dte);
1808
1809 /*
1810 * If the requested mapping must be wired, but this table
1811 * being used to map it is not, the table must be removed
1812 * from the available pool and its wired entry count
1813 * incremented.
1814 */
1815 if (wired && !b_tbl->bt_wcnt) {
1816 TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
1817 a_tbl->at_wcnt++;
1818 }
1819 } else {
1820 /* The descriptor is invalid. Allocate a new B table. */
1821 b_tbl = get_b_table();
1822
1823 /* Point the parent A table descriptor to this new B table. */
1824 a_dte->addr.raw = mmu_vtop(b_tbl->bt_dtbl);
1825 a_dte->attr.raw = MMU_LONG_DTE_LU | MMU_DT_SHORT;
1826 a_tbl->at_ecnt++; /* Update parent's valid entry count */
1827
1828 /* Create the necessary back references to the parent table */
1829 b_tbl->bt_parent = a_tbl;
1830 b_tbl->bt_pidx = a_idx;
1831
1832 /*
1833 * If this table is to be wired, make sure the parent A table
1834 * wired count is updated to reflect that it has another wired
1835 * entry.
1836 */
1837 if (wired)
1838 a_tbl->at_wcnt++;
1839 else if (llevel == NONE)
1840 llevel = NEWB;
1841 }
1842
1843 /*
1844 * Step 3 - Walk into the C table, if there is no valid C table,
1845 * allocate one.
1846 */
1847
1848 b_idx = MMU_TIB(va); /* Calculate the TIB of the VA */
1849 b_dte = &b_tbl->bt_dtbl[b_idx]; /* Retrieve descriptor from table */
1850 if (MMU_VALID_DT(*b_dte)) { /* Is the descriptor valid? */
1851 /* The descriptor is valid. Use the C table it points to. */
1852 /**************************************
1853 * c_idx *
1854 * | v *
1855 * \- b_tbl -> +-+-+-+-+-+-+-+-+-+-+- *
1856 * | | | | | | | | | | | *
1857 * +-+-+-+-+-+-+-+-+-+-+- *
1858 * | *
1859 * \- c_tbl -> +-+-- *
1860 * | | | *
1861 * +-+-- *
1862 **************************************/
1863 c_pte = mmu_ptov(MMU_PTE_PA(*b_dte));
1864 c_tbl = mmuC2tmgr(c_pte);
1865
1866 /* If mapping is wired and table is not */
1867 if (wired && !c_tbl->ct_wcnt) {
1868 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
1869 b_tbl->bt_wcnt++;
1870 }
1871 } else {
1872 /* The descriptor is invalid. Allocate a new C table. */
1873 c_tbl = get_c_table();
1874
1875 /* Point the parent B table descriptor to this new C table. */
1876 b_dte->attr.raw = mmu_vtop(c_tbl->ct_dtbl);
1877 b_dte->attr.raw |= MMU_DT_SHORT;
1878 b_tbl->bt_ecnt++; /* Update parent's valid entry count */
1879
1880 /* Create the necessary back references to the parent table */
1881 c_tbl->ct_parent = b_tbl;
1882 c_tbl->ct_pidx = b_idx;
1883
1884 /*
1885 * If this table is to be wired, make sure the parent B table
1886 * wired count is updated to reflect that it has another wired
1887 * entry.
1888 */
1889 if (wired)
1890 b_tbl->bt_wcnt++;
1891 else if (llevel == NONE)
1892 llevel = NEWC;
1893 }
1894
1895 /*
1896 * Step 4 - Deposit a page descriptor (PTE) into the appropriate
1897 * slot of the C table, describing the PA to which the VA is mapped.
1898 */
1899
1900 pte_idx = MMU_TIC(va);
1901 c_pte = &c_tbl->ct_dtbl[pte_idx];
1902 if (MMU_VALID_DT(*c_pte)) { /* Is the entry currently valid? */
1903 /*
1904 * The PTE is currently valid. This particular call
1905 * is just a synonym for one (or more) of the following
1906 * operations:
1907 * change protection of a page
1908 * change wiring status of a page
1909 * remove the mapping of a page
1910 *
1911 * XXX - Semi critical: This code should unwire the PTE
1912 * and, possibly, associated parent tables if this is a
1913 * change wiring operation. Currently it does not.
1914 *
1915 * This may be ok if pmap_change_wiring() is the only
1916 * interface used to UNWIRE a page.
1917 */
1918
1919 /* First check if this is a wiring operation. */
1920 if (wired && (c_pte->attr.raw & MMU_SHORT_PTE_WIRED)) {
1921 /*
1922 * The PTE is already wired. To prevent it from being
1923 * counted as a new wiring operation, reset the 'wired'
1924 * variable.
1925 */
1926 wired = FALSE;
1927 }
1928
1929 /* Is the new address the same as the old? */
1930 if (MMU_PTE_PA(*c_pte) == pa) {
1931 /*
1932 * Yes, mark that it does not need to be reinserted
1933 * into the PV list.
1934 */
1935 insert = FALSE;
1936
1937 /*
1938 * Clear all but the modified, referenced and wired
1939 * bits on the PTE.
1940 */
1941 c_pte->attr.raw &= (MMU_SHORT_PTE_M
1942 | MMU_SHORT_PTE_USED | MMU_SHORT_PTE_WIRED);
1943 } else {
1944 /* No, remove the old entry */
1945 pmap_remove_pte(c_pte);
1946 insert = TRUE;
1947 }
1948
1949 /*
1950 * TLB flush is only necessary if modifying current map.
1951 * However, in pmap_enter(), the pmap almost always IS
1952 * the current pmap, so don't even bother to check.
1953 */
1954 TBIS(va);
1955 } else {
1956 /*
1957 * The PTE is invalid. Increment the valid entry count in
1958 * the C table manager to reflect the addition of a new entry.
1959 */
1960 c_tbl->ct_ecnt++;
1961
1962 /* XXX - temporarily make sure the PTE is cleared. */
1963 c_pte->attr.raw = 0;
1964
1965 /* It will also need to be inserted into the PV list. */
1966 insert = TRUE;
1967 }
1968
1969 /*
1970 * If page is changing from unwired to wired status, set an unused bit
1971 * within the PTE to indicate that it is wired. Also increment the
1972 * wired entry count in the C table manager.
1973 */
1974 if (wired) {
1975 c_pte->attr.raw |= MMU_SHORT_PTE_WIRED;
1976 c_tbl->ct_wcnt++;
1977 }
1978
1979 /*
1980 * Map the page, being careful to preserve modify/reference/wired
1981 * bits. At this point it is assumed that the PTE either has no bits
1982 * set, or if there are set bits, they are only modified, reference or
1983 * wired bits. If not, the following statement will cause erratic
1984 * behavior.
1985 */
1986 #ifdef PMAP_DEBUG
1987 if (c_pte->attr.raw & ~(MMU_SHORT_PTE_M |
1988 MMU_SHORT_PTE_USED | MMU_SHORT_PTE_WIRED)) {
1989 printf("pmap_enter: junk left in PTE at %p\n", c_pte);
1990 Debugger();
1991 }
1992 #endif
1993 c_pte->attr.raw |= ((u_long) pa | MMU_DT_PAGE);
1994
1995 /*
1996 * If the mapping should be read-only, set the write protect
1997 * bit in the PTE.
1998 */
1999 if (!(prot & VM_PROT_WRITE))
2000 c_pte->attr.raw |= MMU_SHORT_PTE_WP;
2001
2002 /*
2003 * If the mapping should be cache inhibited (indicated by the flag
2004 * bits found on the lower order of the physical address.)
2005 * mark the PTE as a cache inhibited page.
2006 */
2007 if (flags & PMAP_NC)
2008 c_pte->attr.raw |= MMU_SHORT_PTE_CI;
2009
2010 /*
2011 * If the physical address being mapped is managed by the PV
2012 * system then link the pte into the list of pages mapped to that
2013 * address.
2014 */
2015 if (insert && managed) {
2016 pv = pa2pv(pa);
2017 nidx = pteidx(c_pte);
2018
2019 s = splimp();
2020 pvebase[nidx].pve_next = pv->pv_idx;
2021 pv->pv_idx = nidx;
2022 splx(s);
2023 }
2024
2025 /* Move any allocated tables back into the active pool. */
2026
2027 switch (llevel) {
2028 case NEWA:
2029 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
2030 /* FALLTHROUGH */
2031 case NEWB:
2032 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
2033 /* FALLTHROUGH */
2034 case NEWC:
2035 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
2036 /* FALLTHROUGH */
2037 default:
2038 break;
2039 }
2040 }
2041
2042 /* pmap_enter_kernel INTERNAL
2043 **
2044 * Map the given virtual address to the given physical address within the
2045 * kernel address space. This function exists because the kernel map does
2046 * not do dynamic table allocation. It consists of a contiguous array of ptes
2047 * and can be edited directly without the need to walk through any tables.
2048 *
2049 * XXX: "Danger, Will Robinson!"
2050 * Note that the kernel should never take a fault on any page
2051 * between [ KERNBASE .. virtual_avail ] and this is checked in
2052 * trap.c for kernel-mode MMU faults. This means that mappings
2053 * created in that range must be implicily wired. -gwr
2054 */
2055 void
2056 pmap_enter_kernel(va, pa, prot)
2057 vm_offset_t va;
2058 vm_offset_t pa;
2059 vm_prot_t prot;
2060 {
2061 boolean_t was_valid, insert;
2062 u_short pte_idx, pv_idx;
2063 int s, flags;
2064 mmu_short_pte_t *pte;
2065 pv_t *pv;
2066 vm_offset_t old_pa;
2067
2068 flags = (pa & ~MMU_PAGE_MASK);
2069 pa &= MMU_PAGE_MASK;
2070
2071 /*
2072 * Calculate the index of the PTE being modified.
2073 */
2074 pte_idx = (u_long) _btop(va - KERNBASE);
2075
2076 /* This array is traditionally named "Sysmap" */
2077 pte = &kernCbase[pte_idx];
2078
2079 s = splimp();
2080 if (MMU_VALID_DT(*pte)) {
2081 was_valid = TRUE;
2082 /*
2083 * If the PTE is already mapped to an address and it differs
2084 * from the address requested, unlink it from the PV list.
2085 *
2086 * This only applies to mappings within virtual_avail
2087 * and VM_MAX_KERNEL_ADDRESS. All others are not requests
2088 * from the VM system and should not be part of the PV system.
2089 */
2090 if ((va >= virtual_avail) && (va < VM_MAX_KERNEL_ADDRESS)) {
2091 old_pa = MMU_PTE_PA(*pte);
2092 if (pa != old_pa) {
2093 if (is_managed(old_pa)) {
2094 /* XXX - Make this into a function call? */
2095 pv = pa2pv(old_pa);
2096 pv_idx = pv->pv_idx;
2097 if (pv_idx == pte_idx) {
2098 pv->pv_idx = pvebase[pte_idx].pve_next;
2099 } else {
2100 while (pvebase[pv_idx].pve_next != pte_idx)
2101 pv_idx = pvebase[pv_idx].pve_next;
2102 pvebase[pv_idx].pve_next =
2103 pvebase[pte_idx].pve_next;
2104 }
2105 /* Save modified/reference bits */
2106 pv->pv_flags |= (u_short) pte->attr.raw;
2107 }
2108 if (is_managed(pa))
2109 insert = TRUE;
2110 else
2111 insert = FALSE;
2112 /*
2113 * Clear out any old bits in the PTE.
2114 */
2115 pte->attr.raw = MMU_DT_INVALID;
2116 } else {
2117 /*
2118 * Old PA and new PA are the same. No need to relink
2119 * the mapping within the PV list.
2120 */
2121 insert = FALSE;
2122
2123 /*
2124 * Save any mod/ref bits on the PTE.
2125 */
2126 pte->attr.raw &= (MMU_SHORT_PTE_USED|MMU_SHORT_PTE_M);
2127 }
2128 } else {
2129 /*
2130 * If the VA lies below virtual_avail or beyond
2131 * VM_MAX_KERNEL_ADDRESS, it is not a request by the VM
2132 * system and hence does not need to be linked into the PV
2133 * system.
2134 */
2135 insert = FALSE;
2136 pte->attr.raw = MMU_DT_INVALID;
2137 }
2138 } else {
2139 pte->attr.raw = MMU_DT_INVALID;
2140 was_valid = FALSE;
2141 if ((va >= virtual_avail) && (va < VM_MAX_KERNEL_ADDRESS)) {
2142 if (is_managed(pa))
2143 insert = TRUE;
2144 else
2145 insert = FALSE;
2146 } else
2147 insert = FALSE;
2148 }
2149
2150 /*
2151 * Map the page. Being careful to preserve modified/referenced bits
2152 * on the PTE.
2153 */
2154 pte->attr.raw |= (pa | MMU_DT_PAGE);
2155
2156 if (!(prot & VM_PROT_WRITE)) /* If access should be read-only */
2157 pte->attr.raw |= MMU_SHORT_PTE_WP;
2158 if (flags & PMAP_NC)
2159 pte->attr.raw |= MMU_SHORT_PTE_CI;
2160 if (was_valid)
2161 TBIS(va);
2162
2163 /*
2164 * Insert the PTE into the PV system, if need be.
2165 */
2166 if (insert) {
2167 pv = pa2pv(pa);
2168 pvebase[pte_idx].pve_next = pv->pv_idx;
2169 pv->pv_idx = pte_idx;
2170 }
2171 splx(s);
2172
2173 }
2174
2175 /* pmap_protect INTERFACE
2176 **
2177 * Apply the given protection to the given virtual address range within
2178 * the given map.
2179 *
2180 * It is ok for the protection applied to be stronger than what is
2181 * specified. We use this to our advantage when the given map has no
2182 * mapping for the virtual address. By skipping a page when this
2183 * is discovered, we are effectively applying a protection of VM_PROT_NONE,
2184 * and therefore do not need to map the page just to apply a protection
2185 * code. Only pmap_enter() needs to create new mappings if they do not exist.
2186 *
2187 * XXX - This function could be speeded up by using pmap_stroll() for inital
2188 * setup, and then manual scrolling in the for() loop.
2189 */
2190 void
2191 pmap_protect(pmap, startva, endva, prot)
2192 pmap_t pmap;
2193 vm_offset_t startva, endva;
2194 vm_prot_t prot;
2195 {
2196 boolean_t iscurpmap;
2197 int a_idx, b_idx, c_idx;
2198 a_tmgr_t *a_tbl;
2199 b_tmgr_t *b_tbl;
2200 c_tmgr_t *c_tbl;
2201 mmu_short_pte_t *pte;
2202
2203 if (pmap == NULL)
2204 return;
2205 if (pmap == pmap_kernel()) {
2206 pmap_protect_kernel(startva, endva, prot);
2207 return;
2208 }
2209
2210 /*
2211 * In this particular pmap implementation, there are only three
2212 * types of memory protection: 'all' (read/write/execute),
2213 * 'read-only' (read/execute) and 'none' (no mapping.)
2214 * It is not possible for us to treat 'executable' as a separate
2215 * protection type. Therefore, protection requests that seek to
2216 * remove execute permission while retaining read or write, and those
2217 * that make little sense (write-only for example) are ignored.
2218 */
2219 switch (prot) {
2220 case VM_PROT_NONE:
2221 /*
2222 * A request to apply the protection code of
2223 * 'VM_PROT_NONE' is a synonym for pmap_remove().
2224 */
2225 pmap_remove(pmap, startva, endva);
2226 return;
2227 case VM_PROT_EXECUTE:
2228 case VM_PROT_READ:
2229 case VM_PROT_READ|VM_PROT_EXECUTE:
2230 /* continue */
2231 break;
2232 case VM_PROT_WRITE:
2233 case VM_PROT_WRITE|VM_PROT_READ:
2234 case VM_PROT_WRITE|VM_PROT_EXECUTE:
2235 case VM_PROT_ALL:
2236 /* None of these should happen in a sane system. */
2237 return;
2238 }
2239
2240 /*
2241 * If the pmap has no A table, it has no mappings and therefore
2242 * there is nothing to protect.
2243 */
2244 if ((a_tbl = pmap->pm_a_tmgr) == NULL)
2245 return;
2246
2247 a_idx = MMU_TIA(startva);
2248 b_idx = MMU_TIB(startva);
2249 c_idx = MMU_TIC(startva);
2250 b_tbl = (b_tmgr_t *) c_tbl = NULL;
2251
2252 iscurpmap = (pmap == current_pmap());
2253 while (startva < endva) {
2254 if (b_tbl || MMU_VALID_DT(a_tbl->at_dtbl[a_idx])) {
2255 if (b_tbl == NULL) {
2256 b_tbl = (b_tmgr_t *) a_tbl->at_dtbl[a_idx].addr.raw;
2257 b_tbl = mmu_ptov((vm_offset_t) b_tbl);
2258 b_tbl = mmuB2tmgr((mmu_short_dte_t *) b_tbl);
2259 }
2260 if (c_tbl || MMU_VALID_DT(b_tbl->bt_dtbl[b_idx])) {
2261 if (c_tbl == NULL) {
2262 c_tbl = (c_tmgr_t *) MMU_DTE_PA(b_tbl->bt_dtbl[b_idx]);
2263 c_tbl = mmu_ptov((vm_offset_t) c_tbl);
2264 c_tbl = mmuC2tmgr((mmu_short_pte_t *) c_tbl);
2265 }
2266 if (MMU_VALID_DT(c_tbl->ct_dtbl[c_idx])) {
2267 pte = &c_tbl->ct_dtbl[c_idx];
2268 /* make the mapping read-only */
2269 pte->attr.raw |= MMU_SHORT_PTE_WP;
2270 /*
2271 * If we just modified the current address space,
2272 * flush any translations for the modified page from
2273 * the translation cache and any data from it in the
2274 * data cache.
2275 */
2276 if (iscurpmap)
2277 TBIS(startva);
2278 }
2279 startva += NBPG;
2280
2281 if (++c_idx >= MMU_C_TBL_SIZE) { /* exceeded C table? */
2282 c_tbl = NULL;
2283 c_idx = 0;
2284 if (++b_idx >= MMU_B_TBL_SIZE) { /* exceeded B table? */
2285 b_tbl = NULL;
2286 b_idx = 0;
2287 }
2288 }
2289 } else { /* C table wasn't valid */
2290 c_tbl = NULL;
2291 c_idx = 0;
2292 startva += MMU_TIB_RANGE;
2293 if (++b_idx >= MMU_B_TBL_SIZE) { /* exceeded B table? */
2294 b_tbl = NULL;
2295 b_idx = 0;
2296 }
2297 } /* C table */
2298 } else { /* B table wasn't valid */
2299 b_tbl = NULL;
2300 b_idx = 0;
2301 startva += MMU_TIA_RANGE;
2302 a_idx++;
2303 } /* B table */
2304 }
2305 }
2306
2307 /* pmap_protect_kernel INTERNAL
2308 **
2309 * Apply the given protection code to a kernel address range.
2310 */
2311 void
2312 pmap_protect_kernel(startva, endva, prot)
2313 vm_offset_t startva, endva;
2314 vm_prot_t prot;
2315 {
2316 vm_offset_t va;
2317 mmu_short_pte_t *pte;
2318
2319 pte = &kernCbase[(unsigned long) _btop(startva - KERNBASE)];
2320 for (va = startva; va < endva; va += NBPG, pte++) {
2321 if (MMU_VALID_DT(*pte)) {
2322 switch (prot) {
2323 case VM_PROT_ALL:
2324 break;
2325 case VM_PROT_EXECUTE:
2326 case VM_PROT_READ:
2327 case VM_PROT_READ|VM_PROT_EXECUTE:
2328 pte->attr.raw |= MMU_SHORT_PTE_WP;
2329 break;
2330 case VM_PROT_NONE:
2331 /* this is an alias for 'pmap_remove_kernel' */
2332 pmap_remove_pte(pte);
2333 break;
2334 default:
2335 break;
2336 }
2337 /*
2338 * since this is the kernel, immediately flush any cached
2339 * descriptors for this address.
2340 */
2341 TBIS(va);
2342 }
2343 }
2344 }
2345
2346 /* pmap_change_wiring INTERFACE
2347 **
2348 * Changes the wiring of the specified page.
2349 *
2350 * This function is called from vm_fault.c to unwire
2351 * a mapping. It really should be called 'pmap_unwire'
2352 * because it is never asked to do anything but remove
2353 * wirings.
2354 */
2355 void
2356 pmap_change_wiring(pmap, va, wire)
2357 pmap_t pmap;
2358 vm_offset_t va;
2359 boolean_t wire;
2360 {
2361 int a_idx, b_idx, c_idx;
2362 a_tmgr_t *a_tbl;
2363 b_tmgr_t *b_tbl;
2364 c_tmgr_t *c_tbl;
2365 mmu_short_pte_t *pte;
2366
2367 /* Kernel mappings always remain wired. */
2368 if (pmap == pmap_kernel())
2369 return;
2370
2371 #ifdef PMAP_DEBUG
2372 if (wire == TRUE)
2373 panic("pmap_change_wiring: wire requested.");
2374 #endif
2375
2376 /*
2377 * Walk through the tables. If the walk terminates without
2378 * a valid PTE then the address wasn't wired in the first place.
2379 * Return immediately.
2380 */
2381 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl, &pte, &a_idx,
2382 &b_idx, &c_idx) == FALSE)
2383 return;
2384
2385
2386 /* Is the PTE wired? If not, return. */
2387 if (!(pte->attr.raw & MMU_SHORT_PTE_WIRED))
2388 return;
2389
2390 /* Remove the wiring bit. */
2391 pte->attr.raw &= ~(MMU_SHORT_PTE_WIRED);
2392
2393 /*
2394 * Decrement the wired entry count in the C table.
2395 * If it reaches zero the following things happen:
2396 * 1. The table no longer has any wired entries and is considered
2397 * unwired.
2398 * 2. It is placed on the available queue.
2399 * 3. The parent table's wired entry count is decremented.
2400 * 4. If it reaches zero, this process repeats at step 1 and
2401 * stops at after reaching the A table.
2402 */
2403 if (--c_tbl->ct_wcnt == 0) {
2404 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
2405 if (--b_tbl->bt_wcnt == 0) {
2406 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
2407 if (--a_tbl->at_wcnt == 0) {
2408 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
2409 }
2410 }
2411 }
2412 }
2413
2414 /* pmap_pageable INTERFACE
2415 **
2416 * Make the specified range of addresses within the given pmap,
2417 * 'pageable' or 'not-pageable'. A pageable page must not cause
2418 * any faults when referenced. A non-pageable page may.
2419 *
2420 * This routine is only advisory. The VM system will call pmap_enter()
2421 * to wire or unwire pages that are going to be made pageable before calling
2422 * this function. By the time this routine is called, everything that needs
2423 * to be done has already been done.
2424 */
2425 void
2426 pmap_pageable(pmap, start, end, pageable)
2427 pmap_t pmap;
2428 vm_offset_t start, end;
2429 boolean_t pageable;
2430 {
2431 /* not implemented. */
2432 }
2433
2434 /* pmap_copy INTERFACE
2435 **
2436 * Copy the mappings of a range of addresses in one pmap, into
2437 * the destination address of another.
2438 *
2439 * This routine is advisory. Should we one day decide that MMU tables
2440 * may be shared by more than one pmap, this function should be used to
2441 * link them together. Until that day however, we do nothing.
2442 */
2443 void
2444 pmap_copy(pmap_a, pmap_b, dst, len, src)
2445 pmap_t pmap_a, pmap_b;
2446 vm_offset_t dst;
2447 vm_size_t len;
2448 vm_offset_t src;
2449 {
2450 /* not implemented. */
2451 }
2452
2453 /* pmap_copy_page INTERFACE
2454 **
2455 * Copy the contents of one physical page into another.
2456 *
2457 * This function makes use of two virtual pages allocated in pmap_bootstrap()
2458 * to map the two specified physical pages into the kernel address space. It
2459 * then uses bcopy() to copy one into the other.
2460 *
2461 * Note: We could use the transparent translation registers to make the
2462 * mappings. If we do so, be sure to disable interrupts before using them.
2463 */
2464 void
2465 pmap_copy_page(src, dst)
2466 vm_offset_t src, dst;
2467 {
2468 PMAP_LOCK();
2469 if (tmp_vpages_inuse)
2470 panic("pmap_copy_page: temporary vpages are in use.");
2471 tmp_vpages_inuse++;
2472
2473 /* XXX - Use non-cached mappings to avoid cache polution? */
2474 pmap_enter_kernel(tmp_vpages[0], src, VM_PROT_READ);
2475 pmap_enter_kernel(tmp_vpages[1], dst, VM_PROT_READ|VM_PROT_WRITE);
2476 copypage((char *) tmp_vpages[0], (char *) tmp_vpages[1]);
2477
2478 tmp_vpages_inuse--;
2479 PMAP_UNLOCK();
2480 }
2481
2482 /* pmap_zero_page INTERFACE
2483 **
2484 * Zero the contents of the specified physical page.
2485 *
2486 * Uses one of the virtual pages allocated in pmap_boostrap()
2487 * to map the specified page into the kernel address space. Then uses
2488 * bzero() to zero out the page.
2489 */
2490 void
2491 pmap_zero_page(pa)
2492 vm_offset_t pa;
2493 {
2494 PMAP_LOCK();
2495 if (tmp_vpages_inuse)
2496 panic("pmap_zero_page: temporary vpages are in use.");
2497 tmp_vpages_inuse++;
2498
2499 pmap_enter_kernel(tmp_vpages[0], pa, VM_PROT_READ|VM_PROT_WRITE);
2500 zeropage((char *) tmp_vpages[0]);
2501
2502 tmp_vpages_inuse--;
2503 PMAP_UNLOCK();
2504 }
2505
2506 /* pmap_collect INTERFACE
2507 **
2508 * Called from the VM system when we are about to swap out
2509 * the process using this pmap. This should give up any
2510 * resources held here, including all its MMU tables.
2511 */
2512 void
2513 pmap_collect(pmap)
2514 pmap_t pmap;
2515 {
2516 /* XXX - todo... */
2517 }
2518
2519 /* pmap_create INTERFACE
2520 **
2521 * Create and return a pmap structure.
2522 */
2523 pmap_t
2524 pmap_create(size)
2525 vm_size_t size;
2526 {
2527 pmap_t pmap;
2528
2529 if (size)
2530 return NULL;
2531
2532 pmap = (pmap_t) malloc(sizeof(struct pmap), M_VMPMAP, M_WAITOK);
2533 pmap_pinit(pmap);
2534
2535 return pmap;
2536 }
2537
2538 /* pmap_pinit INTERNAL
2539 **
2540 * Initialize a pmap structure.
2541 */
2542 void
2543 pmap_pinit(pmap)
2544 pmap_t pmap;
2545 {
2546 bzero(pmap, sizeof(struct pmap));
2547 pmap->pm_a_tmgr = NULL;
2548 pmap->pm_a_phys = kernAphys;
2549 }
2550
2551 /* pmap_release INTERFACE
2552 **
2553 * Release any resources held by the given pmap.
2554 *
2555 * This is the reverse analog to pmap_pinit. It does not
2556 * necessarily mean for the pmap structure to be deallocated,
2557 * as in pmap_destroy.
2558 */
2559 void
2560 pmap_release(pmap)
2561 pmap_t pmap;
2562 {
2563 /*
2564 * As long as the pmap contains no mappings,
2565 * which always should be the case whenever
2566 * this function is called, there really should
2567 * be nothing to do.
2568 */
2569 #ifdef PMAP_DEBUG
2570 if (pmap == NULL)
2571 return;
2572 if (pmap == pmap_kernel())
2573 panic("pmap_release: kernel pmap");
2574 #endif
2575 /*
2576 * XXX - If this pmap has an A table, give it back.
2577 * The pmap SHOULD be empty by now, and pmap_remove
2578 * should have already given back the A table...
2579 * However, I see: pmap->pm_a_tmgr->at_ecnt == 1
2580 * at this point, which means some mapping was not
2581 * removed when it should have been. -gwr
2582 */
2583 if (pmap->pm_a_tmgr != NULL) {
2584 /* First make sure we are not using it! */
2585 if (kernel_crp.rp_addr == pmap->pm_a_phys) {
2586 kernel_crp.rp_addr = kernAphys;
2587 loadcrp(&kernel_crp);
2588 }
2589 #ifdef PMAP_DEBUG /* XXX - todo! */
2590 /* XXX - Now complain... */
2591 printf("pmap_release: still have table\n");
2592 Debugger();
2593 #endif
2594 free_a_table(pmap->pm_a_tmgr, TRUE);
2595 pmap->pm_a_tmgr = NULL;
2596 pmap->pm_a_phys = kernAphys;
2597 }
2598 }
2599
2600 /* pmap_reference INTERFACE
2601 **
2602 * Increment the reference count of a pmap.
2603 */
2604 void
2605 pmap_reference(pmap)
2606 pmap_t pmap;
2607 {
2608 if (pmap == NULL)
2609 return;
2610
2611 /* pmap_lock(pmap); */
2612 pmap->pm_refcount++;
2613 /* pmap_unlock(pmap); */
2614 }
2615
2616 /* pmap_dereference INTERNAL
2617 **
2618 * Decrease the reference count on the given pmap
2619 * by one and return the current count.
2620 */
2621 int
2622 pmap_dereference(pmap)
2623 pmap_t pmap;
2624 {
2625 int rtn;
2626
2627 if (pmap == NULL)
2628 return 0;
2629
2630 /* pmap_lock(pmap); */
2631 rtn = --pmap->pm_refcount;
2632 /* pmap_unlock(pmap); */
2633
2634 return rtn;
2635 }
2636
2637 /* pmap_destroy INTERFACE
2638 **
2639 * Decrement a pmap's reference count and delete
2640 * the pmap if it becomes zero. Will be called
2641 * only after all mappings have been removed.
2642 */
2643 void
2644 pmap_destroy(pmap)
2645 pmap_t pmap;
2646 {
2647 if (pmap == NULL)
2648 return;
2649 if (pmap == &kernel_pmap)
2650 panic("pmap_destroy: kernel_pmap!");
2651 if (pmap_dereference(pmap) == 0) {
2652 pmap_release(pmap);
2653 free(pmap, M_VMPMAP);
2654 }
2655 }
2656
2657 /* pmap_is_referenced INTERFACE
2658 **
2659 * Determine if the given physical page has been
2660 * referenced (read from [or written to.])
2661 */
2662 boolean_t
2663 pmap_is_referenced(pa)
2664 vm_offset_t pa;
2665 {
2666 pv_t *pv;
2667 int idx, s;
2668
2669 if (!pv_initialized)
2670 return FALSE;
2671 /* XXX - this may be unecessary. */
2672 if (!is_managed(pa))
2673 return FALSE;
2674
2675 pv = pa2pv(pa);
2676 /*
2677 * Check the flags on the pv head. If they are set,
2678 * return immediately. Otherwise a search must be done.
2679 */
2680 if (pv->pv_flags & PV_FLAGS_USED)
2681 return TRUE;
2682 else {
2683 s = splimp();
2684 /*
2685 * Search through all pv elements pointing
2686 * to this page and query their reference bits
2687 */
2688 for (idx = pv->pv_idx; idx != PVE_EOL; idx =
2689 pvebase[idx].pve_next)
2690 if (MMU_PTE_USED(kernCbase[idx])) {
2691 splx(s);
2692 return TRUE;
2693 }
2694 splx(s);
2695 }
2696
2697 return FALSE;
2698 }
2699
2700 /* pmap_is_modified INTERFACE
2701 **
2702 * Determine if the given physical page has been
2703 * modified (written to.)
2704 */
2705 boolean_t
2706 pmap_is_modified(pa)
2707 vm_offset_t pa;
2708 {
2709 pv_t *pv;
2710 int idx, s;
2711
2712 if (!pv_initialized)
2713 return FALSE;
2714 /* XXX - this may be unecessary. */
2715 if (!is_managed(pa))
2716 return FALSE;
2717
2718 /* see comments in pmap_is_referenced() */
2719 pv = pa2pv(pa);
2720 if (pv->pv_flags & PV_FLAGS_MDFY) {
2721 return TRUE;
2722 } else {
2723 s = splimp();
2724 for (idx = pv->pv_idx; idx != PVE_EOL; idx =
2725 pvebase[idx].pve_next)
2726 if (MMU_PTE_MODIFIED(kernCbase[idx])) {
2727 splx(s);
2728 return TRUE;
2729 }
2730 splx(s);
2731 }
2732
2733 return FALSE;
2734 }
2735
2736 /* pmap_page_protect INTERFACE
2737 **
2738 * Applies the given protection to all mappings to the given
2739 * physical page.
2740 */
2741 void
2742 pmap_page_protect(pa, prot)
2743 vm_offset_t pa;
2744 vm_prot_t prot;
2745 {
2746 pv_t *pv;
2747 int idx, s;
2748 vm_offset_t va;
2749 struct mmu_short_pte_struct *pte;
2750 c_tmgr_t *c_tbl;
2751 pmap_t pmap, curpmap;
2752
2753 if (!is_managed(pa))
2754 return;
2755
2756 curpmap = current_pmap();
2757 pv = pa2pv(pa);
2758 s = splimp();
2759 for (idx = pv->pv_idx; idx != PVE_EOL; idx = pvebase[idx].pve_next) {
2760 pte = &kernCbase[idx];
2761 switch (prot) {
2762 case VM_PROT_ALL:
2763 /* do nothing */
2764 break;
2765 case VM_PROT_EXECUTE:
2766 case VM_PROT_READ:
2767 case VM_PROT_READ|VM_PROT_EXECUTE:
2768 pte->attr.raw |= MMU_SHORT_PTE_WP;
2769
2770 /*
2771 * Determine the virtual address mapped by
2772 * the PTE and flush ATC entries if necessary.
2773 */
2774 va = pmap_get_pteinfo(idx, &pmap, &c_tbl);
2775 if (pmap == curpmap || pmap == pmap_kernel())
2776 TBIS(va);
2777 break;
2778 case VM_PROT_NONE:
2779 /* Save the mod/ref bits. */
2780 pv->pv_flags |= pte->attr.raw;
2781 /* Invalidate the PTE. */
2782 pte->attr.raw = MMU_DT_INVALID;
2783
2784 /*
2785 * Update table counts. And flush ATC entries
2786 * if necessary.
2787 */
2788 va = pmap_get_pteinfo(idx, &pmap, &c_tbl);
2789
2790 /*
2791 * If the PTE belongs to the kernel map,
2792 * be sure to flush the page it maps.
2793 */
2794 if (pmap == pmap_kernel()) {
2795 TBIS(va);
2796 } else {
2797 /*
2798 * The PTE belongs to a user map.
2799 * update the entry count in the C
2800 * table to which it belongs and flush
2801 * the ATC if the mapping belongs to
2802 * the current pmap.
2803 */
2804 c_tbl->ct_ecnt--;
2805 if (pmap == curpmap)
2806 TBIS(va);
2807 }
2808 break;
2809 default:
2810 break;
2811 }
2812 }
2813
2814 /*
2815 * If the protection code indicates that all mappings to the page
2816 * be removed, truncate the PV list to zero entries.
2817 */
2818 if (prot == VM_PROT_NONE)
2819 pv->pv_idx = PVE_EOL;
2820 splx(s);
2821 }
2822
2823 /* pmap_get_pteinfo INTERNAL
2824 **
2825 * Called internally to find the pmap and virtual address within that
2826 * map to which the pte at the given index maps. Also includes the PTE's C
2827 * table manager.
2828 *
2829 * Returns the pmap in the argument provided, and the virtual address
2830 * by return value.
2831 */
2832 vm_offset_t
2833 pmap_get_pteinfo(idx, pmap, tbl)
2834 u_int idx;
2835 pmap_t *pmap;
2836 c_tmgr_t **tbl;
2837 {
2838 a_tmgr_t *a_tbl;
2839 b_tmgr_t *b_tbl;
2840 c_tmgr_t *c_tbl;
2841 vm_offset_t va = 0;
2842
2843 /*
2844 * Determine if the PTE is a kernel PTE or a user PTE.
2845 */
2846 if (idx >= NUM_KERN_PTES) {
2847 /*
2848 * The PTE belongs to a user mapping.
2849 * Find the virtual address by decoding table indices.
2850 * Each successive decode will reveal the address from
2851 * least to most significant bit fashion.
2852 *
2853 * 31 0
2854 * +-------------------------------+
2855 * |AAAAAAABBBBBBCCCCCC............|
2856 * +-------------------------------+
2857 */
2858 /* XXX: c_tbl = mmuC2tmgr(pte); */
2859 /* XXX: Would like an inline for this to validate idx... */
2860 c_tbl = &Ctmgrbase[(idx - NUM_KERN_PTES) / MMU_C_TBL_SIZE];
2861 b_tbl = c_tbl->ct_parent;
2862 a_tbl = b_tbl->bt_parent;
2863 *pmap = a_tbl->at_parent;
2864 *tbl = c_tbl;
2865
2866 /* Start with the 'C' bits, then add B and A... */
2867 va |= ((idx % MMU_C_TBL_SIZE) << MMU_TIC_SHIFT);
2868 va |= (c_tbl->ct_pidx << MMU_TIB_SHIFT);
2869 va |= (b_tbl->bt_pidx << MMU_TIA_SHIFT);
2870 } else {
2871 /*
2872 * The PTE belongs to the kernel map.
2873 */
2874 *pmap = pmap_kernel();
2875
2876 va = _ptob(idx);
2877 va += KERNBASE;
2878 }
2879
2880 return va;
2881 }
2882
2883 /* pmap_clear_modify INTERFACE
2884 **
2885 * Clear the modification bit on the page at the specified
2886 * physical address.
2887 *
2888 */
2889 void
2890 pmap_clear_modify(pa)
2891 vm_offset_t pa;
2892 {
2893 if (!is_managed(pa))
2894 return;
2895 pmap_clear_pv(pa, PV_FLAGS_MDFY);
2896 }
2897
2898 /* pmap_clear_reference INTERFACE
2899 **
2900 * Clear the referenced bit on the page at the specified
2901 * physical address.
2902 */
2903 void
2904 pmap_clear_reference(pa)
2905 vm_offset_t pa;
2906 {
2907 if (!is_managed(pa))
2908 return;
2909 pmap_clear_pv(pa, PV_FLAGS_USED);
2910 }
2911
2912 /* pmap_clear_pv INTERNAL
2913 **
2914 * Clears the specified flag from the specified physical address.
2915 * (Used by pmap_clear_modify() and pmap_clear_reference().)
2916 *
2917 * Flag is one of:
2918 * PV_FLAGS_MDFY - Page modified bit.
2919 * PV_FLAGS_USED - Page used (referenced) bit.
2920 *
2921 * This routine must not only clear the flag on the pv list
2922 * head. It must also clear the bit on every pte in the pv
2923 * list associated with the address.
2924 */
2925 void
2926 pmap_clear_pv(pa, flag)
2927 vm_offset_t pa;
2928 int flag;
2929 {
2930 pv_t *pv;
2931 int idx, s;
2932 vm_offset_t va;
2933 pmap_t pmap;
2934 mmu_short_pte_t *pte;
2935 c_tmgr_t *c_tbl;
2936
2937 pv = pa2pv(pa);
2938
2939 s = splimp();
2940 pv->pv_flags &= ~(flag);
2941 for (idx = pv->pv_idx; idx != PVE_EOL; idx = pvebase[idx].pve_next) {
2942 pte = &kernCbase[idx];
2943 pte->attr.raw &= ~(flag);
2944 /*
2945 * The MC68030 MMU will not set the modified or
2946 * referenced bits on any MMU tables for which it has
2947 * a cached descriptor with its modify bit set. To insure
2948 * that it will modify these bits on the PTE during the next
2949 * time it is written to or read from, we must flush it from
2950 * the ATC.
2951 *
2952 * Ordinarily it is only necessary to flush the descriptor
2953 * if it is used in the current address space. But since I
2954 * am not sure that there will always be a notion of
2955 * 'the current address space' when this function is called,
2956 * I will skip the test and always flush the address. It
2957 * does no harm.
2958 */
2959 va = pmap_get_pteinfo(idx, &pmap, &c_tbl);
2960 TBIS(va);
2961 }
2962 splx(s);
2963 }
2964
2965 /* pmap_extract INTERFACE
2966 **
2967 * Return the physical address mapped by the virtual address
2968 * in the specified pmap or 0 if it is not known.
2969 *
2970 * Note: this function should also apply an exclusive lock
2971 * on the pmap system during its duration.
2972 */
2973 vm_offset_t
2974 pmap_extract(pmap, va)
2975 pmap_t pmap;
2976 vm_offset_t va;
2977 {
2978 int a_idx, b_idx, pte_idx;
2979 a_tmgr_t *a_tbl;
2980 b_tmgr_t *b_tbl;
2981 c_tmgr_t *c_tbl;
2982 mmu_short_pte_t *c_pte;
2983
2984 if (pmap == pmap_kernel())
2985 return pmap_extract_kernel(va);
2986 if (pmap == NULL)
2987 return 0;
2988
2989 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl,
2990 &c_pte, &a_idx, &b_idx, &pte_idx) == FALSE)
2991 return 0;
2992
2993 if (!MMU_VALID_DT(*c_pte))
2994 return 0;
2995
2996 return (MMU_PTE_PA(*c_pte));
2997 }
2998
2999 /* pmap_extract_kernel INTERNAL
3000 **
3001 * Extract a translation from the kernel address space.
3002 */
3003 vm_offset_t
3004 pmap_extract_kernel(va)
3005 vm_offset_t va;
3006 {
3007 mmu_short_pte_t *pte;
3008
3009 pte = &kernCbase[(u_int) _btop(va - KERNBASE)];
3010 return MMU_PTE_PA(*pte);
3011 }
3012
3013 /* pmap_remove_kernel INTERNAL
3014 **
3015 * Remove the mapping of a range of virtual addresses from the kernel map.
3016 * The arguments are already page-aligned.
3017 */
3018 void
3019 pmap_remove_kernel(sva, eva)
3020 vm_offset_t sva;
3021 vm_offset_t eva;
3022 {
3023 int idx, eidx;
3024
3025 #ifdef PMAP_DEBUG
3026 if ((sva & PGOFSET) || (eva & PGOFSET))
3027 panic("pmap_remove_kernel: alignment");
3028 #endif
3029
3030 idx = _btop(sva - KERNBASE);
3031 eidx = _btop(eva - KERNBASE);
3032
3033 while (idx < eidx)
3034 pmap_remove_pte(&kernCbase[idx++]);
3035 /* Always flush the ATC when maniplating the kernel address space. */
3036 TBIAS();
3037 }
3038
3039 /* pmap_remove INTERFACE
3040 **
3041 * Remove the mapping of a range of virtual addresses from the given pmap.
3042 *
3043 * If the range contains any wired entries, this function will probably create
3044 * disaster.
3045 */
3046 void
3047 pmap_remove(pmap, start, end)
3048 pmap_t pmap;
3049 vm_offset_t start;
3050 vm_offset_t end;
3051 {
3052
3053 if (pmap == pmap_kernel()) {
3054 pmap_remove_kernel(start, end);
3055 return;
3056 }
3057
3058 /*
3059 * XXX - Temporary(?) statement to prevent panic caused
3060 * by vm_alloc_with_pager() handing us a software map (ie NULL)
3061 * to remove because it couldn't get backing store.
3062 * (I guess.)
3063 */
3064 if (pmap == NULL)
3065 return;
3066
3067 /*
3068 * If the pmap doesn't have an A table of its own, it has no mappings
3069 * that can be removed.
3070 */
3071 if (pmap->pm_a_tmgr == NULL)
3072 return;
3073
3074 /*
3075 * Remove the specified range from the pmap. If the function
3076 * returns true, the operation removed all the valid mappings
3077 * in the pmap and freed its A table. If this happened to the
3078 * currently loaded pmap, the MMU root pointer must be reloaded
3079 * with the default 'kernel' map.
3080 */
3081 if (pmap_remove_a(pmap->pm_a_tmgr, start, end)) {
3082 if (kernel_crp.rp_addr == pmap->pm_a_phys) {
3083 kernel_crp.rp_addr = kernAphys;
3084 loadcrp(&kernel_crp);
3085 /* will do TLB flush below */
3086 }
3087 pmap->pm_a_tmgr = NULL;
3088 pmap->pm_a_phys = kernAphys;
3089 }
3090
3091 /*
3092 * If we just modified the current address space,
3093 * make sure to flush the MMU cache.
3094 *
3095 * XXX - this could be an unecessarily large flush.
3096 * XXX - Could decide, based on the size of the VA range
3097 * to be removed, whether to flush "by pages" or "all".
3098 */
3099 if (pmap == current_pmap())
3100 TBIAU();
3101 }
3102
3103 /* pmap_remove_a INTERNAL
3104 **
3105 * This is function number one in a set of three that removes a range
3106 * of memory in the most efficient manner by removing the highest possible
3107 * tables from the memory space. This particular function attempts to remove
3108 * as many B tables as it can, delegating the remaining fragmented ranges to
3109 * pmap_remove_b().
3110 *
3111 * If the removal operation results in an empty A table, the function returns
3112 * TRUE.
3113 *
3114 * It's ugly but will do for now.
3115 */
3116 boolean_t
3117 pmap_remove_a(a_tbl, start, end)
3118 a_tmgr_t *a_tbl;
3119 vm_offset_t start;
3120 vm_offset_t end;
3121 {
3122 boolean_t empty;
3123 int idx;
3124 vm_offset_t nstart, nend;
3125 b_tmgr_t *b_tbl;
3126 mmu_long_dte_t *a_dte;
3127 mmu_short_dte_t *b_dte;
3128
3129 /*
3130 * The following code works with what I call a 'granularity
3131 * reduction algorithim'. A range of addresses will always have
3132 * the following properties, which are classified according to
3133 * how the range relates to the size of the current granularity
3134 * - an A table entry:
3135 *
3136 * 1 2 3 4
3137 * -+---+---+---+---+---+---+---+-
3138 * -+---+---+---+---+---+---+---+-
3139 *
3140 * A range will always start on a granularity boundary, illustrated
3141 * by '+' signs in the table above, or it will start at some point
3142 * inbetween a granularity boundary, as illustrated by point 1.
3143 * The first step in removing a range of addresses is to remove the
3144 * range between 1 and 2, the nearest granularity boundary. This
3145 * job is handled by the section of code governed by the
3146 * 'if (start < nstart)' statement.
3147 *
3148 * A range will always encompass zero or more intergral granules,
3149 * illustrated by points 2 and 3. Integral granules are easy to
3150 * remove. The removal of these granules is the second step, and
3151 * is handled by the code block 'if (nstart < nend)'.
3152 *
3153 * Lastly, a range will always end on a granularity boundary,
3154 * ill. by point 3, or it will fall just beyond one, ill. by point
3155 * 4. The last step involves removing this range and is handled by
3156 * the code block 'if (nend < end)'.
3157 */
3158 nstart = MMU_ROUND_UP_A(start);
3159 nend = MMU_ROUND_A(end);
3160
3161 if (start < nstart) {
3162 /*
3163 * This block is executed if the range starts between
3164 * a granularity boundary.
3165 *
3166 * First find the DTE which is responsible for mapping
3167 * the start of the range.
3168 */
3169 idx = MMU_TIA(start);
3170 a_dte = &a_tbl->at_dtbl[idx];
3171
3172 /*
3173 * If the DTE is valid then delegate the removal of the sub
3174 * range to pmap_remove_b(), which can remove addresses at
3175 * a finer granularity.
3176 */
3177 if (MMU_VALID_DT(*a_dte)) {
3178 b_dte = mmu_ptov(a_dte->addr.raw);
3179 b_tbl = mmuB2tmgr(b_dte);
3180
3181 /*
3182 * The sub range to be removed starts at the start
3183 * of the full range we were asked to remove, and ends
3184 * at the greater of:
3185 * 1. The end of the full range, -or-
3186 * 2. The end of the full range, rounded down to the
3187 * nearest granularity boundary.
3188 */
3189 if (end < nstart)
3190 empty = pmap_remove_b(b_tbl, start, end);
3191 else
3192 empty = pmap_remove_b(b_tbl, start, nstart);
3193
3194 /*
3195 * If the removal resulted in an empty B table,
3196 * invalidate the DTE that points to it and decrement
3197 * the valid entry count of the A table.
3198 */
3199 if (empty) {
3200 a_dte->attr.raw = MMU_DT_INVALID;
3201 a_tbl->at_ecnt--;
3202 }
3203 }
3204 /*
3205 * If the DTE is invalid, the address range is already non-
3206 * existant and can simply be skipped.
3207 */
3208 }
3209 if (nstart < nend) {
3210 /*
3211 * This block is executed if the range spans a whole number
3212 * multiple of granules (A table entries.)
3213 *
3214 * First find the DTE which is responsible for mapping
3215 * the start of the first granule involved.
3216 */
3217 idx = MMU_TIA(nstart);
3218 a_dte = &a_tbl->at_dtbl[idx];
3219
3220 /*
3221 * Remove entire sub-granules (B tables) one at a time,
3222 * until reaching the end of the range.
3223 */
3224 for (; nstart < nend; a_dte++, nstart += MMU_TIA_RANGE)
3225 if (MMU_VALID_DT(*a_dte)) {
3226 /*
3227 * Find the B table manager for the
3228 * entry and free it.
3229 */
3230 b_dte = mmu_ptov(a_dte->addr.raw);
3231 b_tbl = mmuB2tmgr(b_dte);
3232 free_b_table(b_tbl, TRUE);
3233
3234 /*
3235 * Invalidate the DTE that points to the
3236 * B table and decrement the valid entry
3237 * count of the A table.
3238 */
3239 a_dte->attr.raw = MMU_DT_INVALID;
3240 a_tbl->at_ecnt--;
3241 }
3242 }
3243 if (nend < end) {
3244 /*
3245 * This block is executed if the range ends beyond a
3246 * granularity boundary.
3247 *
3248 * First find the DTE which is responsible for mapping
3249 * the start of the nearest (rounded down) granularity
3250 * boundary.
3251 */
3252 idx = MMU_TIA(nend);
3253 a_dte = &a_tbl->at_dtbl[idx];
3254
3255 /*
3256 * If the DTE is valid then delegate the removal of the sub
3257 * range to pmap_remove_b(), which can remove addresses at
3258 * a finer granularity.
3259 */
3260 if (MMU_VALID_DT(*a_dte)) {
3261 /*
3262 * Find the B table manager for the entry
3263 * and hand it to pmap_remove_b() along with
3264 * the sub range.
3265 */
3266 b_dte = mmu_ptov(a_dte->addr.raw);
3267 b_tbl = mmuB2tmgr(b_dte);
3268
3269 empty = pmap_remove_b(b_tbl, nend, end);
3270
3271 /*
3272 * If the removal resulted in an empty B table,
3273 * invalidate the DTE that points to it and decrement
3274 * the valid entry count of the A table.
3275 */
3276 if (empty) {
3277 a_dte->attr.raw = MMU_DT_INVALID;
3278 a_tbl->at_ecnt--;
3279 }
3280 }
3281 }
3282
3283 /*
3284 * If there are no more entries in the A table, release it
3285 * back to the available pool and return TRUE.
3286 */
3287 if (a_tbl->at_ecnt == 0) {
3288 a_tbl->at_parent = NULL;
3289 TAILQ_REMOVE(&a_pool, a_tbl, at_link);
3290 TAILQ_INSERT_HEAD(&a_pool, a_tbl, at_link);
3291 empty = TRUE;
3292 } else {
3293 empty = FALSE;
3294 }
3295
3296 return empty;
3297 }
3298
3299 /* pmap_remove_b INTERNAL
3300 **
3301 * Remove a range of addresses from an address space, trying to remove entire
3302 * C tables if possible.
3303 *
3304 * If the operation results in an empty B table, the function returns TRUE.
3305 */
3306 boolean_t
3307 pmap_remove_b(b_tbl, start, end)
3308 b_tmgr_t *b_tbl;
3309 vm_offset_t start;
3310 vm_offset_t end;
3311 {
3312 boolean_t empty;
3313 int idx;
3314 vm_offset_t nstart, nend, rstart;
3315 c_tmgr_t *c_tbl;
3316 mmu_short_dte_t *b_dte;
3317 mmu_short_pte_t *c_dte;
3318
3319
3320 nstart = MMU_ROUND_UP_B(start);
3321 nend = MMU_ROUND_B(end);
3322
3323 if (start < nstart) {
3324 idx = MMU_TIB(start);
3325 b_dte = &b_tbl->bt_dtbl[idx];
3326 if (MMU_VALID_DT(*b_dte)) {
3327 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte));
3328 c_tbl = mmuC2tmgr(c_dte);
3329 if (end < nstart)
3330 empty = pmap_remove_c(c_tbl, start, end);
3331 else
3332 empty = pmap_remove_c(c_tbl, start, nstart);
3333 if (empty) {
3334 b_dte->attr.raw = MMU_DT_INVALID;
3335 b_tbl->bt_ecnt--;
3336 }
3337 }
3338 }
3339 if (nstart < nend) {
3340 idx = MMU_TIB(nstart);
3341 b_dte = &b_tbl->bt_dtbl[idx];
3342 rstart = nstart;
3343 while (rstart < nend) {
3344 if (MMU_VALID_DT(*b_dte)) {
3345 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte));
3346 c_tbl = mmuC2tmgr(c_dte);
3347 free_c_table(c_tbl, TRUE);
3348 b_dte->attr.raw = MMU_DT_INVALID;
3349 b_tbl->bt_ecnt--;
3350 }
3351 b_dte++;
3352 rstart += MMU_TIB_RANGE;
3353 }
3354 }
3355 if (nend < end) {
3356 idx = MMU_TIB(nend);
3357 b_dte = &b_tbl->bt_dtbl[idx];
3358 if (MMU_VALID_DT(*b_dte)) {
3359 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte));
3360 c_tbl = mmuC2tmgr(c_dte);
3361 empty = pmap_remove_c(c_tbl, nend, end);
3362 if (empty) {
3363 b_dte->attr.raw = MMU_DT_INVALID;
3364 b_tbl->bt_ecnt--;
3365 }
3366 }
3367 }
3368
3369 if (b_tbl->bt_ecnt == 0) {
3370 b_tbl->bt_parent = NULL;
3371 TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
3372 TAILQ_INSERT_HEAD(&b_pool, b_tbl, bt_link);
3373 empty = TRUE;
3374 } else {
3375 empty = FALSE;
3376 }
3377
3378 return empty;
3379 }
3380
3381 /* pmap_remove_c INTERNAL
3382 **
3383 * Remove a range of addresses from the given C table.
3384 */
3385 boolean_t
3386 pmap_remove_c(c_tbl, start, end)
3387 c_tmgr_t *c_tbl;
3388 vm_offset_t start;
3389 vm_offset_t end;
3390 {
3391 boolean_t empty;
3392 int idx;
3393 mmu_short_pte_t *c_pte;
3394
3395 idx = MMU_TIC(start);
3396 c_pte = &c_tbl->ct_dtbl[idx];
3397 for (;start < end; start += MMU_PAGE_SIZE, c_pte++) {
3398 if (MMU_VALID_DT(*c_pte)) {
3399 pmap_remove_pte(c_pte);
3400 c_tbl->ct_ecnt--;
3401 }
3402 }
3403
3404 if (c_tbl->ct_ecnt == 0) {
3405 c_tbl->ct_parent = NULL;
3406 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
3407 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
3408 empty = TRUE;
3409 } else {
3410 empty = FALSE;
3411 }
3412
3413 return empty;
3414 }
3415
3416 /* is_managed INTERNAL
3417 **
3418 * Determine if the given physical address is managed by the PV system.
3419 * Note that this logic assumes that no one will ask for the status of
3420 * addresses which lie in-between the memory banks on the 3/80. If they
3421 * do so, it will falsely report that it is managed.
3422 *
3423 * Note: A "managed" address is one that was reported to the VM system as
3424 * a "usable page" during system startup. As such, the VM system expects the
3425 * pmap module to keep an accurate track of the useage of those pages.
3426 * Any page not given to the VM system at startup does not exist (as far as
3427 * the VM system is concerned) and is therefore "unmanaged." Examples are
3428 * those pages which belong to the ROM monitor and the memory allocated before
3429 * the VM system was started.
3430 */
3431 boolean_t
3432 is_managed(pa)
3433 vm_offset_t pa;
3434 {
3435 if (pa >= avail_start && pa < avail_end)
3436 return TRUE;
3437 else
3438 return FALSE;
3439 }
3440
3441 /* pmap_bootstrap_alloc INTERNAL
3442 **
3443 * Used internally for memory allocation at startup when malloc is not
3444 * available. This code will fail once it crosses the first memory
3445 * bank boundary on the 3/80. Hopefully by then however, the VM system
3446 * will be in charge of allocation.
3447 */
3448 void *
3449 pmap_bootstrap_alloc(size)
3450 int size;
3451 {
3452 void *rtn;
3453
3454 #ifdef PMAP_DEBUG
3455 if (bootstrap_alloc_enabled == FALSE) {
3456 mon_printf("pmap_bootstrap_alloc: disabled\n");
3457 sunmon_abort();
3458 }
3459 #endif
3460
3461 rtn = (void *) virtual_avail;
3462 virtual_avail += size;
3463
3464 #ifdef PMAP_DEBUG
3465 if (virtual_avail > virtual_contig_end) {
3466 mon_printf("pmap_bootstrap_alloc: out of mem\n");
3467 sunmon_abort();
3468 }
3469 #endif
3470
3471 return rtn;
3472 }
3473
3474 /* pmap_bootstap_aalign INTERNAL
3475 **
3476 * Used to insure that the next call to pmap_bootstrap_alloc() will
3477 * return a chunk of memory aligned to the specified size.
3478 *
3479 * Note: This function will only support alignment sizes that are powers
3480 * of two.
3481 */
3482 void
3483 pmap_bootstrap_aalign(size)
3484 int size;
3485 {
3486 int off;
3487
3488 off = virtual_avail & (size - 1);
3489 if (off) {
3490 (void) pmap_bootstrap_alloc(size - off);
3491 }
3492 }
3493
3494 /* pmap_pa_exists
3495 **
3496 * Used by the /dev/mem driver to see if a given PA is memory
3497 * that can be mapped. (The PA is not in a hole.)
3498 */
3499 int
3500 pmap_pa_exists(pa)
3501 vm_offset_t pa;
3502 {
3503 register int i;
3504
3505 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) {
3506 if ((pa >= avail_mem[i].pmem_start) &&
3507 (pa < avail_mem[i].pmem_end))
3508 return (1);
3509 if (avail_mem[i].pmem_next == NULL)
3510 break;
3511 }
3512 return (0);
3513 }
3514
3515 /* pmap_activate INTERFACE
3516 **
3517 * This is called by locore.s:cpu_switch when we are switching to a
3518 * new process. This should load the MMU context for the new proc.
3519 * XXX - Later, this should be done directly in locore.s
3520 */
3521 void
3522 pmap_activate(pmap)
3523 pmap_t pmap;
3524 {
3525 u_long rootpa;
3526
3527 /* Only do reload/flush if we have to. */
3528 rootpa = pmap->pm_a_phys;
3529 if (kernel_crp.rp_addr != rootpa) {
3530 DPRINT(("pmap_activate(%p)\n", pmap));
3531 kernel_crp.rp_addr = rootpa;
3532 loadcrp(&kernel_crp);
3533 TBIAU();
3534 }
3535 }
3536
3537
3538 /* pmap_update
3539 **
3540 * Apply any delayed changes scheduled for all pmaps immediately.
3541 *
3542 * No delayed operations are currently done in this pmap.
3543 */
3544 void
3545 pmap_update()
3546 {
3547 /* not implemented. */
3548 }
3549
3550 /*
3551 * Fill in the cpu_kcore header for dumpsys()
3552 * (See machdep.c)
3553 */
3554 void
3555 pmap_set_kcore_hdr(chdr_p)
3556 cpu_kcore_hdr_t *chdr_p;
3557 {
3558 struct sun3x_kcore_hdr *sh = &chdr_p->un._sun3x;
3559 u_long spa, len;
3560 int i;
3561 extern char machine[];
3562
3563 /*
3564 * Fill in dispatch information.
3565 */
3566 strcpy(chdr_p->name, machine);
3567 chdr_p->page_size = NBPG;
3568 chdr_p->kernbase = KERNBASE;
3569
3570 sh->contig_end = virtual_contig_end;
3571 sh->kernCbase = (u_long) kernCbase;
3572 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) {
3573 spa = avail_mem[i].pmem_start;
3574 spa = _trunc_page(spa);
3575 len = avail_mem[i].pmem_end - spa;
3576 len = _round_page(len);
3577 sh->ram_segs[i].start = spa;
3578 sh->ram_segs[i].size = len;
3579 }
3580 }
3581
3582
3583 /* pmap_virtual_space INTERFACE
3584 **
3585 * Return the current available range of virtual addresses in the
3586 * arguuments provided. Only really called once.
3587 */
3588 void
3589 pmap_virtual_space(vstart, vend)
3590 vm_offset_t *vstart, *vend;
3591 {
3592 *vstart = virtual_avail;
3593 *vend = virtual_end;
3594 }
3595
3596 /* pmap_free_pages INTERFACE
3597 **
3598 * Return the number of physical pages still available.
3599 *
3600 * This is probably going to be a mess, but it's only called
3601 * once and it's the only function left that I have to implement!
3602 */
3603 u_int
3604 pmap_free_pages()
3605 {
3606 int i;
3607 u_int left;
3608 vm_offset_t avail;
3609
3610 avail = avail_next;
3611 left = 0;
3612 i = 0;
3613 while (avail >= avail_mem[i].pmem_end) {
3614 if (avail_mem[i].pmem_next == NULL)
3615 return 0;
3616 i++;
3617 }
3618 while (i < SUN3X_NPHYS_RAM_SEGS) {
3619 if (avail < avail_mem[i].pmem_start) {
3620 /* Avail is inside a hole, march it
3621 * up to the next bank.
3622 */
3623 avail = avail_mem[i].pmem_start;
3624 }
3625 left += _btop(avail_mem[i].pmem_end - avail);
3626 if (avail_mem[i].pmem_next == NULL)
3627 break;
3628 i++;
3629 }
3630
3631 return left;
3632 }
3633
3634 /* pmap_page_index INTERFACE
3635 **
3636 * Return the index of the given physical page in a list of useable
3637 * physical pages in the system. Holes in physical memory may be counted
3638 * if so desired. As long as pmap_free_pages() and pmap_page_index()
3639 * agree as to whether holes in memory do or do not count as valid pages,
3640 * it really doesn't matter. However, if you like to save a little
3641 * memory, don't count holes as valid pages. This is even more true when
3642 * the holes are large.
3643 *
3644 * We will not count holes as valid pages. We can generate page indices
3645 * that conform to this by using the memory bank structures initialized
3646 * in pmap_alloc_pv().
3647 */
3648 int
3649 pmap_page_index(pa)
3650 vm_offset_t pa;
3651 {
3652 struct pmap_physmem_struct *bank = avail_mem;
3653
3654 /* Search for the memory bank with this page. */
3655 /* XXX - What if it is not physical memory? */
3656 while (pa > bank->pmem_end)
3657 bank = bank->pmem_next;
3658 pa -= bank->pmem_start;
3659
3660 return (bank->pmem_pvbase + _btop(pa));
3661 }
3662
3663 /* pmap_next_page INTERFACE
3664 **
3665 * Place the physical address of the next available page in the
3666 * argument given. Returns FALSE if there are no more pages left.
3667 *
3668 * This function must jump over any holes in physical memory.
3669 * Once this function is used, any use of pmap_bootstrap_alloc()
3670 * is a sin. Sinners will be punished with erratic behavior.
3671 */
3672 boolean_t
3673 pmap_next_page(pa)
3674 vm_offset_t *pa;
3675 {
3676 static struct pmap_physmem_struct *curbank = avail_mem;
3677
3678 /* XXX - temporary ROM saving hack. */
3679 if (avail_next >= avail_end)
3680 return FALSE;
3681
3682 if (avail_next >= curbank->pmem_end)
3683 if (curbank->pmem_next == NULL)
3684 return FALSE;
3685 else {
3686 curbank = curbank->pmem_next;
3687 avail_next = curbank->pmem_start;
3688 }
3689
3690 *pa = avail_next;
3691 avail_next += NBPG;
3692 return TRUE;
3693 }
3694
3695 /* pmap_count INTERFACE
3696 **
3697 * Return the number of resident (valid) pages in the given pmap.
3698 *
3699 * Note: If this function is handed the kernel map, it will report
3700 * that it has no mappings. Hopefully the VM system won't ask for kernel
3701 * map statistics.
3702 */
3703 segsz_t
3704 pmap_count(pmap, type)
3705 pmap_t pmap;
3706 int type;
3707 {
3708 u_int count;
3709 int a_idx, b_idx;
3710 a_tmgr_t *a_tbl;
3711 b_tmgr_t *b_tbl;
3712 c_tmgr_t *c_tbl;
3713
3714 /*
3715 * If the pmap does not have its own A table manager, it has no
3716 * valid entires.
3717 */
3718 if (pmap->pm_a_tmgr == NULL)
3719 return 0;
3720
3721 a_tbl = pmap->pm_a_tmgr;
3722
3723 count = 0;
3724 for (a_idx = 0; a_idx < MMU_TIA(KERNBASE); a_idx++) {
3725 if (MMU_VALID_DT(a_tbl->at_dtbl[a_idx])) {
3726 b_tbl = mmuB2tmgr(mmu_ptov(a_tbl->at_dtbl[a_idx].addr.raw));
3727 for (b_idx = 0; b_idx < MMU_B_TBL_SIZE; b_idx++) {
3728 if (MMU_VALID_DT(b_tbl->bt_dtbl[b_idx])) {
3729 c_tbl = mmuC2tmgr(
3730 mmu_ptov(MMU_DTE_PA(b_tbl->bt_dtbl[b_idx])));
3731 if (type == 0)
3732 /*
3733 * A resident entry count has been requested.
3734 */
3735 count += c_tbl->ct_ecnt;
3736 else
3737 /*
3738 * A wired entry count has been requested.
3739 */
3740 count += c_tbl->ct_wcnt;
3741 }
3742 }
3743 }
3744 }
3745
3746 return count;
3747 }
3748
3749 /************************ SUN3 COMPATIBILITY ROUTINES ********************
3750 * The following routines are only used by DDB for tricky kernel text *
3751 * text operations in db_memrw.c. They are provided for sun3 *
3752 * compatibility. *
3753 *************************************************************************/
3754 /* get_pte INTERNAL
3755 **
3756 * Return the page descriptor the describes the kernel mapping
3757 * of the given virtual address.
3758 */
3759 extern u_long ptest_addr __P((u_long)); /* XXX: locore.s */
3760 u_long
3761 get_pte(va)
3762 vm_offset_t va;
3763 {
3764 u_long pte_pa;
3765 mmu_short_pte_t *pte;
3766
3767 /* Get the physical address of the PTE */
3768 pte_pa = ptest_addr(va & ~PGOFSET);
3769
3770 /* Convert to a virtual address... */
3771 pte = (mmu_short_pte_t *) (KERNBASE + pte_pa);
3772
3773 /* Make sure it is in our level-C tables... */
3774 if ((pte < kernCbase) ||
3775 (pte >= &mmuCbase[NUM_USER_PTES]))
3776 return 0;
3777
3778 /* ... and just return its contents. */
3779 return (pte->attr.raw);
3780 }
3781
3782
3783 /* set_pte INTERNAL
3784 **
3785 * Set the page descriptor that describes the kernel mapping
3786 * of the given virtual address.
3787 */
3788 void
3789 set_pte(va, pte)
3790 vm_offset_t va;
3791 vm_offset_t pte;
3792 {
3793 u_long idx;
3794
3795 if (va < KERNBASE)
3796 return;
3797
3798 idx = (unsigned long) _btop(va - KERNBASE);
3799 kernCbase[idx].attr.raw = pte;
3800 }
3801
3802 #ifdef PMAP_DEBUG
3803 /************************** DEBUGGING ROUTINES **************************
3804 * The following routines are meant to be an aid to debugging the pmap *
3805 * system. They are callable from the DDB command line and should be *
3806 * prepared to be handed unstable or incomplete states of the system. *
3807 ************************************************************************/
3808
3809 /* pv_list
3810 **
3811 * List all pages found on the pv list for the given physical page.
3812 * To avoid endless loops, the listing will stop at the end of the list
3813 * or after 'n' entries - whichever comes first.
3814 */
3815 void
3816 pv_list(pa, n)
3817 vm_offset_t pa;
3818 int n;
3819 {
3820 int idx;
3821 vm_offset_t va;
3822 pv_t *pv;
3823 c_tmgr_t *c_tbl;
3824 pmap_t pmap;
3825
3826 pv = pa2pv(pa);
3827 idx = pv->pv_idx;
3828
3829 for (;idx != PVE_EOL && n > 0; idx=pvebase[idx].pve_next, n--) {
3830 va = pmap_get_pteinfo(idx, &pmap, &c_tbl);
3831 printf("idx %d, pmap 0x%x, va 0x%x, c_tbl %x\n",
3832 idx, (u_int) pmap, (u_int) va, (u_int) c_tbl);
3833 }
3834 }
3835 #endif /* PMAP_DEBUG */
3836
3837 #ifdef NOT_YET
3838 /* and maybe not ever */
3839 /************************** LOW-LEVEL ROUTINES **************************
3840 * These routines will eventualy be re-written into assembly and placed *
3841 * in locore.s. They are here now as stubs so that the pmap module can *
3842 * be linked as a standalone user program for testing. *
3843 ************************************************************************/
3844 /* flush_atc_crp INTERNAL
3845 **
3846 * Flush all page descriptors derived from the given CPU Root Pointer
3847 * (CRP), or 'A' table as it is known here, from the 68851's automatic
3848 * cache.
3849 */
3850 void
3851 flush_atc_crp(a_tbl)
3852 {
3853 mmu_long_rp_t rp;
3854
3855 /* Create a temporary root table pointer that points to the
3856 * given A table.
3857 */
3858 rp.attr.raw = ~MMU_LONG_RP_LU;
3859 rp.addr.raw = (unsigned int) a_tbl;
3860
3861 mmu_pflushr(&rp);
3862 /* mmu_pflushr:
3863 * movel sp(4)@,a0
3864 * pflushr a0@
3865 * rts
3866 */
3867 }
3868 #endif /* NOT_YET */
3869