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