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