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