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