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