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