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