Home | History | Annotate | Line # | Download | only in sun3x
pmap.c revision 1.1
      1  1.1  gwr /*	$NetBSD: pmap.c,v 1.1 1997/01/14 20:57:08 gwr Exp $	*/
      2  1.1  gwr 
      3  1.1  gwr /*-
      4  1.1  gwr  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  1.1  gwr  * All rights reserved.
      6  1.1  gwr  *
      7  1.1  gwr  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  gwr  * by Jeremy Cooper.
      9  1.1  gwr  *
     10  1.1  gwr  * Redistribution and use in source and binary forms, with or without
     11  1.1  gwr  * modification, are permitted provided that the following conditions
     12  1.1  gwr  * are met:
     13  1.1  gwr  * 1. Redistributions of source code must retain the above copyright
     14  1.1  gwr  *    notice, this list of conditions and the following disclaimer.
     15  1.1  gwr  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  gwr  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  gwr  *    documentation and/or other materials provided with the distribution.
     18  1.1  gwr  * 3. All advertising materials mentioning features or use of this software
     19  1.1  gwr  *    must display the following acknowledgement:
     20  1.1  gwr  *        This product includes software developed by the NetBSD
     21  1.1  gwr  *        Foundation, Inc. and its contributors.
     22  1.1  gwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  gwr  *    contributors may be used to endorse or promote products derived
     24  1.1  gwr  *    from this software without specific prior written permission.
     25  1.1  gwr  *
     26  1.1  gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  gwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  gwr  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  gwr  */
     38  1.1  gwr 
     39  1.1  gwr /*
     40  1.1  gwr  * XXX These comments aren't quite accurate.  Need to change.
     41  1.1  gwr  * The sun3x uses the MC68851 Memory Management Unit, which is built
     42  1.1  gwr  * into the CPU.  The 68851 maps virtual to physical addresses using
     43  1.1  gwr  * a multi-level table lookup, which is stored in the very memory that
     44  1.1  gwr  * it maps.  The number of levels of lookup is configurable from one
     45  1.1  gwr  * to four.  In this implementation, we use three, named 'A' through 'C'.
     46  1.1  gwr  *
     47  1.1  gwr  * The MMU translates virtual addresses into physical addresses by
     48  1.1  gwr  * traversing these tables in a proccess called a 'table walk'.  The most
     49  1.1  gwr  * significant 7 bits of the Virtual Address ('VA') being translated are
     50  1.1  gwr  * used as an index into the level A table, whose base in physical memory
     51  1.1  gwr  * is stored in a special MMU register, the 'CPU Root Pointer' or CRP.  The
     52  1.1  gwr  * address found at that index in the A table is used as the base
     53  1.1  gwr  * address for the next table, the B table.  The next six bits of the VA are
     54  1.1  gwr  * used as an index into the B table, which in turn gives the base address
     55  1.1  gwr  * of the third and final C table.
     56  1.1  gwr  *
     57  1.1  gwr  * The next six bits of the VA are used as an index into the C table to
     58  1.1  gwr  * locate a Page Table Entry (PTE).  The PTE is a physical address in memory
     59  1.1  gwr  * to which the remaining 13 bits of the VA are added, producing the
     60  1.1  gwr  * mapped physical address.
     61  1.1  gwr  *
     62  1.1  gwr  * To map the entire memory space in this manner would require 2114296 bytes
     63  1.1  gwr  * of page tables per process - quite expensive.  Instead we will
     64  1.1  gwr  * allocate a fixed but considerably smaller space for the page tables at
     65  1.1  gwr  * the time the VM system is initialized.  When the pmap code is asked by
     66  1.1  gwr  * the kernel to map a VA to a PA, it allocates tables as needed from this
     67  1.1  gwr  * pool.  When there are no more tables in the pool, tables are stolen
     68  1.1  gwr  * from the oldest mapped entries in the tree.  This is only possible
     69  1.1  gwr  * because all memory mappings are stored in the kernel memory map
     70  1.1  gwr  * structures, independent of the pmap structures.  A VA which references
     71  1.1  gwr  * one of these invalidated maps will cause a page fault.  The kernel
     72  1.1  gwr  * will determine that the page fault was caused by a task using a valid
     73  1.1  gwr  * VA, but for some reason (which does not concern it), that address was
     74  1.1  gwr  * not mapped.  It will ask the pmap code to re-map the entry and then
     75  1.1  gwr  * it will resume executing the faulting task.
     76  1.1  gwr  *
     77  1.1  gwr  * In this manner the most efficient use of the page table space is
     78  1.1  gwr  * achieved.  Tasks which do not execute often will have their tables
     79  1.1  gwr  * stolen and reused by tasks which execute more frequently.  The best
     80  1.1  gwr  * size for the page table pool will probably be determined by
     81  1.1  gwr  * experimentation.
     82  1.1  gwr  *
     83  1.1  gwr  * You read all of the comments so far.  Good for you.
     84  1.1  gwr  * Now go play!
     85  1.1  gwr  */
     86  1.1  gwr 
     87  1.1  gwr /*** A Note About the 68851 Address Translation Cache
     88  1.1  gwr  * The MC68851 has a 64 entry cache, called the Address Translation Cache
     89  1.1  gwr  * or 'ATC'.  This cache stores the most recently used page descriptors
     90  1.1  gwr  * accessed by the MMU when it does translations.  Using a marker called a
     91  1.1  gwr  * 'task alias' the MMU can store the descriptors from 8 different table
     92  1.1  gwr  * spaces concurrently.  The task alias is associated with the base
     93  1.1  gwr  * address of the level A table of that address space.  When an address
     94  1.1  gwr  * space is currently active (the CRP currently points to its A table)
     95  1.1  gwr  * the only cached descriptors that will be obeyed are ones which have a
     96  1.1  gwr  * matching task alias of the current space associated with them.
     97  1.1  gwr  *
     98  1.1  gwr  * Since the cache is always consulted before any table lookups are done,
     99  1.1  gwr  * it is important that it accurately reflect the state of the MMU tables.
    100  1.1  gwr  * Whenever a change has been made to a table that has been loaded into
    101  1.1  gwr  * the MMU, the code must be sure to flush any cached entries that are
    102  1.1  gwr  * affected by the change.  These instances are documented in the code at
    103  1.1  gwr  * various points.
    104  1.1  gwr  */
    105  1.1  gwr /*** A Note About the Note About the 68851 Address Translation Cache
    106  1.1  gwr  * 4 months into this code I discovered that the sun3x does not have
    107  1.1  gwr  * a MC68851 chip. Instead, it has a version of this MMU that is part of the
    108  1.1  gwr  * the 68030 CPU.
    109  1.1  gwr  * All though it behaves very similarly to the 68851, it only has 1 task
    110  1.1  gwr  * alias and a 22 entry cache.  So sadly (or happily), the previous note
    111  1.1  gwr  * does not apply to the sun3x pmap.
    112  1.1  gwr  */
    113  1.1  gwr 
    114  1.1  gwr #include <sys/param.h>
    115  1.1  gwr #include <sys/systm.h>
    116  1.1  gwr #include <sys/proc.h>
    117  1.1  gwr #include <sys/malloc.h>
    118  1.1  gwr #include <sys/user.h>
    119  1.1  gwr #include <sys/queue.h>
    120  1.1  gwr 
    121  1.1  gwr #include <vm/vm.h>
    122  1.1  gwr #include <vm/vm_kern.h>
    123  1.1  gwr #include <vm/vm_page.h>
    124  1.1  gwr 
    125  1.1  gwr #include <machine/cpu.h>
    126  1.1  gwr #include <machine/pmap.h>
    127  1.1  gwr #include <machine/pte.h>
    128  1.1  gwr #include <machine/mon.h>
    129  1.1  gwr 
    130  1.1  gwr #include "machdep.h"
    131  1.1  gwr #include "pmap_pvt.h"
    132  1.1  gwr 
    133  1.1  gwr /* XXX - What headers declare these? */
    134  1.1  gwr extern struct pcb *curpcb;
    135  1.1  gwr extern int physmem;
    136  1.1  gwr 
    137  1.1  gwr /* Defined in locore.s */
    138  1.1  gwr extern char kernel_text[];
    139  1.1  gwr 
    140  1.1  gwr /* Defined by the linker */
    141  1.1  gwr extern char etext[], edata[], end[];
    142  1.1  gwr extern char *esym;	/* DDB */
    143  1.1  gwr 
    144  1.1  gwr /*
    145  1.1  gwr  * I think it might be cleaner to have one of these in each of
    146  1.1  gwr  * the a_tmgr_t structures, but it's late at night... -gwr
    147  1.1  gwr  *
    148  1.1  gwr  */
    149  1.1  gwr struct rootptr {
    150  1.1  gwr 	u_long limit; /* and type */
    151  1.1  gwr 	u_long paddr;
    152  1.1  gwr };
    153  1.1  gwr struct rootptr proc0crp;
    154  1.1  gwr 
    155  1.1  gwr /* This is set by locore.s with the monitor's root ptr. */
    156  1.1  gwr extern struct rootptr mon_crp;
    157  1.1  gwr 
    158  1.1  gwr /*** Management Structure - Memory Layout
    159  1.1  gwr  * For every MMU table in the sun3x pmap system there must be a way to
    160  1.1  gwr  * manage it; we must know which process is using it, what other tables
    161  1.1  gwr  * depend on it, and whether or not it contains any locked pages.  This
    162  1.1  gwr  * is solved by the creation of 'table management'  or 'tmgr'
    163  1.1  gwr  * structures.  One for each MMU table in the system.
    164  1.1  gwr  *
    165  1.1  gwr  *                        MAP OF MEMORY USED BY THE PMAP SYSTEM
    166  1.1  gwr  *
    167  1.1  gwr  *      towards lower memory
    168  1.1  gwr  * kernAbase -> +-------------------------------------------------------+
    169  1.1  gwr  *              | Kernel     MMU A level table                          |
    170  1.1  gwr  * kernBbase -> +-------------------------------------------------------+
    171  1.1  gwr  *              | Kernel     MMU B level tables                         |
    172  1.1  gwr  * kernCbase -> +-------------------------------------------------------+
    173  1.1  gwr  *              |                                                       |
    174  1.1  gwr  *              | Kernel     MMU C level tables                         |
    175  1.1  gwr  *              |                                                       |
    176  1.1  gwr  * mmuAbase  -> +-------------------------------------------------------+
    177  1.1  gwr  *              |                                                       |
    178  1.1  gwr  *              | User       MMU A level tables                         |
    179  1.1  gwr  *              |                                                       |
    180  1.1  gwr  * mmuBbase  -> +-------------------------------------------------------+
    181  1.1  gwr  *              | User       MMU B level tables                         |
    182  1.1  gwr  * mmuCbase  -> +-------------------------------------------------------+
    183  1.1  gwr  *              | User       MMU C level tables                         |
    184  1.1  gwr  * tmgrAbase -> +-------------------------------------------------------+
    185  1.1  gwr  *              |  TMGR A level table structures                        |
    186  1.1  gwr  * tmgrBbase -> +-------------------------------------------------------+
    187  1.1  gwr  *              |  TMGR B level table structures                        |
    188  1.1  gwr  * tmgrCbase -> +-------------------------------------------------------+
    189  1.1  gwr  *              |  TMGR C level table structures                        |
    190  1.1  gwr  * pvbase    -> +-------------------------------------------------------+
    191  1.1  gwr  *              |  Physical to Virtual mapping table (list heads)       |
    192  1.1  gwr  * pvebase   -> +-------------------------------------------------------+
    193  1.1  gwr  *              |  Physical to Virtual mapping table (list elements)    |
    194  1.1  gwr  *              |                                                       |
    195  1.1  gwr  *              +-------------------------------------------------------+
    196  1.1  gwr  *      towards higher memory
    197  1.1  gwr  *
    198  1.1  gwr  * For every A table in the MMU A area, there will be a corresponding
    199  1.1  gwr  * a_tmgr structure in the TMGR A area.  The same will be true for
    200  1.1  gwr  * the B and C tables.  This arrangement will make it easy to find the
    201  1.1  gwr  * controling tmgr structure for any table in the system by use of
    202  1.1  gwr  * (relatively) simple macros.
    203  1.1  gwr  */
    204  1.1  gwr /* Global variables for storing the base addresses for the areas
    205  1.1  gwr  * labeled above.
    206  1.1  gwr  */
    207  1.1  gwr static mmu_long_dte_t	*kernAbase;
    208  1.1  gwr static mmu_short_dte_t	*kernBbase;
    209  1.1  gwr static mmu_short_pte_t	*kernCbase;
    210  1.1  gwr static mmu_long_dte_t	*mmuAbase;
    211  1.1  gwr static mmu_short_dte_t	*mmuBbase;
    212  1.1  gwr static mmu_short_pte_t	*mmuCbase;
    213  1.1  gwr static a_tmgr_t		*Atmgrbase;
    214  1.1  gwr static b_tmgr_t		*Btmgrbase;
    215  1.1  gwr static c_tmgr_t		*Ctmgrbase;
    216  1.1  gwr static pv_t		*pvbase;
    217  1.1  gwr static pv_elem_t	*pvebase;
    218  1.1  gwr 
    219  1.1  gwr /* Just all around global variables.
    220  1.1  gwr  */
    221  1.1  gwr static TAILQ_HEAD(a_pool_head_struct, a_tmgr_struct) a_pool;
    222  1.1  gwr static TAILQ_HEAD(b_pool_head_struct, b_tmgr_struct) b_pool;
    223  1.1  gwr static TAILQ_HEAD(c_pool_head_struct, c_tmgr_struct) c_pool;
    224  1.1  gwr        struct pmap	kernel_pmap;
    225  1.1  gwr static a_tmgr_t		*proc0Atmgr;
    226  1.1  gwr        a_tmgr_t		*curatbl;
    227  1.1  gwr static boolean_t	pv_initialized = 0;
    228  1.1  gwr static vm_offset_t	last_mapped = 0;
    229  1.1  gwr        int		tmp_vpages_inuse = 0;
    230  1.1  gwr 
    231  1.1  gwr /*
    232  1.1  gwr  * XXX:  For now, retain the traditional variables that were
    233  1.1  gwr  * used in the old pmap/vm interface (without NONCONTIG).
    234  1.1  gwr  */
    235  1.1  gwr /* Kernel virtual address space available: */
    236  1.1  gwr vm_offset_t	virtual_avail, virtual_end;
    237  1.1  gwr /* Physical address space available: */
    238  1.1  gwr vm_offset_t	avail_start, avail_end;
    239  1.1  gwr 
    240  1.1  gwr vm_offset_t tmp_vpages[2];
    241  1.1  gwr 
    242  1.1  gwr 
    243  1.1  gwr /* The 3/80 is the only member of the sun3x family that has non-contiguous
    244  1.1  gwr  * physical memory.  Memory is divided into 4 banks which are physically
    245  1.1  gwr  * locatable on the system board.  Although the size of these banks varies
    246  1.1  gwr  * with the size of memory they contain, their base addresses are
    247  1.1  gwr  * permenently fixed.  The following structure, which describes these
    248  1.1  gwr  * banks, is initialized by pmap_bootstrap() after it reads from a similar
    249  1.1  gwr  * structure provided by the ROM Monitor.
    250  1.1  gwr  *
    251  1.1  gwr  * For the other machines in the sun3x architecture which do have contiguous
    252  1.1  gwr  * RAM, this list will have only one entry, which will describe the entire
    253  1.1  gwr  * range of available memory.
    254  1.1  gwr  */
    255  1.1  gwr struct pmap_physmem_struct avail_mem[SUN3X_80_MEM_BANKS];
    256  1.1  gwr u_int total_phys_mem;
    257  1.1  gwr 
    258  1.1  gwr /* These macros map MMU tables to their corresponding manager structures.
    259  1.1  gwr  * They are needed quite often because many of the pointers in the pmap
    260  1.1  gwr  * system reference MMU tables and not the structures that control them.
    261  1.1  gwr  * There needs to be a way to find one when given the other and these
    262  1.1  gwr  * macros do so by taking advantage of the memory layout described above.
    263  1.1  gwr  * Here's a quick step through the first macro, mmuA2tmgr():
    264  1.1  gwr  *
    265  1.1  gwr  * 1) find the offset of the given MMU A table from the base of its table
    266  1.1  gwr  *    pool (table - mmuAbase).
    267  1.1  gwr  * 2) convert this offset into a table index by dividing it by the
    268  1.1  gwr  *    size of one MMU 'A' table. (sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE)
    269  1.1  gwr  * 3) use this index to select the corresponding 'A' table manager
    270  1.1  gwr  *    structure from the 'A' table manager pool (Atmgrbase[index]).
    271  1.1  gwr  */
    272  1.1  gwr #define mmuA2tmgr(table) \
    273  1.1  gwr 	(&Atmgrbase[\
    274  1.1  gwr 		((mmu_long_dte_t *)(table) - mmuAbase)\
    275  1.1  gwr 		/ MMU_A_TBL_SIZE\
    276  1.1  gwr 	])
    277  1.1  gwr #define mmuB2tmgr(table) \
    278  1.1  gwr 	(&Btmgrbase[\
    279  1.1  gwr 		((mmu_short_dte_t *)(table) - mmuBbase)\
    280  1.1  gwr 		/ MMU_B_TBL_SIZE\
    281  1.1  gwr         ])
    282  1.1  gwr #define mmuC2tmgr(table) \
    283  1.1  gwr 	(&Ctmgrbase[\
    284  1.1  gwr 		((mmu_short_pte_t *)(table) - mmuCbase)\
    285  1.1  gwr 		/ MMU_C_TBL_SIZE\
    286  1.1  gwr 	])
    287  1.1  gwr #define pte2pve(pte) \
    288  1.1  gwr 	(&pvebase[\
    289  1.1  gwr 		((mmu_short_pte_t *)(pte) - mmuCbase)\
    290  1.1  gwr 	])
    291  1.1  gwr /* I don't think this is actually used.
    292  1.1  gwr  * #define pte2pv(pte) \
    293  1.1  gwr  *	(pa2pv(\
    294  1.1  gwr  *		(pte)->attr.raw & MMU_SHORT_PTE_BASEADDR\
    295  1.1  gwr  *	))
    296  1.1  gwr  */
    297  1.1  gwr /* This is now a function call
    298  1.1  gwr  * #define pa2pv(pa) \
    299  1.1  gwr  *	(&pvbase[(unsigned long)\
    300  1.1  gwr  *		sun3x_btop(pa)\
    301  1.1  gwr  *	])
    302  1.1  gwr  */
    303  1.1  gwr #define pve2pte(pve) \
    304  1.1  gwr 	(&mmuCbase[(unsigned long)\
    305  1.1  gwr 		(((pv_elem_t *)(pve)) - pvebase)\
    306  1.1  gwr 		/ sizeof(mmu_short_pte_t)\
    307  1.1  gwr 	])
    308  1.1  gwr 
    309  1.1  gwr /*************************** TEMPORARY STATMENTS *************************
    310  1.1  gwr  * These statements will disappear once this code is integrated into the *
    311  1.1  gwr  * system.  They are here only to make the code `stand alone'.           *
    312  1.1  gwr  *************************************************************************/
    313  1.1  gwr #define mmu_ptov(pa) ((unsigned long) KERNBASE + (unsigned long) (pa))
    314  1.1  gwr #define mmu_vtop(va) ((unsigned long) (va) - (unsigned long) KERNBASE)
    315  1.1  gwr #define NULL 0
    316  1.1  gwr 
    317  1.1  gwr #define NUM_A_TABLES	20
    318  1.1  gwr #define NUM_B_TABLES	60
    319  1.1  gwr #define NUM_C_TABLES	60
    320  1.1  gwr 
    321  1.1  gwr /*************************** MISCELANEOUS MACROS *************************/
    322  1.1  gwr #define PMAP_LOCK()	;	/* Nothing, for now */
    323  1.1  gwr #define PMAP_UNLOCK()	;	/* same. */
    324  1.1  gwr /*************************** FUNCTION DEFINITIONS ************************
    325  1.1  gwr  * These appear here merely for the compiler to enforce type checking on *
    326  1.1  gwr  * all function calls.                                                   *
    327  1.1  gwr  *************************************************************************
    328  1.1  gwr  */
    329  1.1  gwr 
    330  1.1  gwr /** External functions
    331  1.1  gwr  ** - functions used within this module but written elsewhere.
    332  1.1  gwr  **   both of these functions are in locore.s
    333  1.1  gwr  */
    334  1.1  gwr void   mmu_seturp __P((vm_offset_t));
    335  1.1  gwr void   mmu_flush __P((int, vm_offset_t));
    336  1.1  gwr void   mmu_flusha __P((void));
    337  1.1  gwr 
    338  1.1  gwr /** Internal functions
    339  1.1  gwr  ** - all functions used only within this module are defined in
    340  1.1  gwr  **   pmap_pvt.h
    341  1.1  gwr  **/
    342  1.1  gwr 
    343  1.1  gwr /** Interface functions
    344  1.1  gwr  ** - functions required by the Mach VM Pmap interface, with MACHINE_CONTIG
    345  1.1  gwr  **   defined.
    346  1.1  gwr  **/
    347  1.1  gwr #ifdef INCLUDED_IN_PMAP_H
    348  1.1  gwr void   pmap_bootstrap __P((void));
    349  1.1  gwr void  *pmap_bootstrap_alloc __P((int));
    350  1.1  gwr void   pmap_enter __P((pmap_t, vm_offset_t, vm_offset_t, vm_prot_t, boolean_t));
    351  1.1  gwr pmap_t pmap_create __P((vm_size_t));
    352  1.1  gwr void   pmap_destroy __P((pmap_t));
    353  1.1  gwr void   pmap_reference __P((pmap_t));
    354  1.1  gwr boolean_t   pmap_is_referenced __P((vm_offset_t));
    355  1.1  gwr boolean_t   pmap_is_modified __P((vm_offset_t));
    356  1.1  gwr void   pmap_clear_modify __P((vm_offset_t));
    357  1.1  gwr vm_offset_t pmap_extract __P((pmap_t, vm_offset_t));
    358  1.1  gwr void   pmap_activate __P((pmap_t, struct pcb *));
    359  1.1  gwr int    pmap_page_index __P((vm_offset_t));
    360  1.1  gwr u_int  pmap_free_pages __P((void));
    361  1.1  gwr #endif /* INCLUDED_IN_PMAP_H */
    362  1.1  gwr 
    363  1.1  gwr /********************************** CODE ********************************
    364  1.1  gwr  * Functions that are called from other parts of the kernel are labeled *
    365  1.1  gwr  * as 'INTERFACE' functions.  Functions that are only called from       *
    366  1.1  gwr  * within the pmap module are labeled as 'INTERNAL' functions.          *
    367  1.1  gwr  * Functions that are internal, but are not (currently) used at all are *
    368  1.1  gwr  * labeled 'INTERNAL_X'.                                                *
    369  1.1  gwr  ************************************************************************/
    370  1.1  gwr 
    371  1.1  gwr /* pmap_bootstrap			INTERNAL
    372  1.1  gwr  **
    373  1.1  gwr  * Initializes the pmap system.  Called at boot time from sun3x_vm_init()
    374  1.1  gwr  * in _startup.c.
    375  1.1  gwr  *
    376  1.1  gwr  * Reminder: having a pmap_bootstrap_alloc() and also having the VM
    377  1.1  gwr  *           system implement pmap_steal_memory() is redundant.
    378  1.1  gwr  *           Don't release this code without removing one or the other!
    379  1.1  gwr  */
    380  1.1  gwr void
    381  1.1  gwr pmap_bootstrap(nextva)
    382  1.1  gwr 	vm_offset_t nextva;
    383  1.1  gwr {
    384  1.1  gwr 	struct physmemory *membank;
    385  1.1  gwr 	struct pmap_physmem_struct *pmap_membank;
    386  1.1  gwr 	vm_offset_t va, pa, eva;
    387  1.1  gwr 	int b, c, i, j;	/* running table counts */
    388  1.1  gwr 	int size;
    389  1.1  gwr 
    390  1.1  gwr 	/*
    391  1.1  gwr 	 * This function is called by __bootstrap after it has
    392  1.1  gwr 	 * determined the type of machine and made the appropriate
    393  1.1  gwr 	 * patches to the ROM vectors (XXX- I don't quite know what I meant
    394  1.1  gwr 	 * by that.)  It allocates and sets up enough of the pmap system
    395  1.1  gwr 	 * to manage the kernel's address space.
    396  1.1  gwr 	 */
    397  1.1  gwr 
    398  1.1  gwr 	/* XXX - Attention: moved stuff. */
    399  1.1  gwr 
    400  1.1  gwr 	/*
    401  1.1  gwr 	 * Determine the range of kernel virtual space available.
    402  1.1  gwr 	 */
    403  1.1  gwr 	virtual_avail = sun3x_round_page(nextva);
    404  1.1  gwr 	virtual_end = VM_MAX_KERNEL_ADDRESS;
    405  1.1  gwr 
    406  1.1  gwr 	/*
    407  1.1  gwr 	 * Determine the range of physical memory available and
    408  1.1  gwr 	 * relay this information to the pmap via the avail_mem[]
    409  1.1  gwr 	 * array of physical memory segment structures.
    410  1.1  gwr 	 *
    411  1.1  gwr 	 * Avail_end is set to the first byte of physical memory
    412  1.1  gwr 	 * outside the last bank.
    413  1.1  gwr 	 */
    414  1.1  gwr 	avail_start = virtual_avail - KERNBASE;
    415  1.1  gwr 
    416  1.1  gwr 	/*
    417  1.1  gwr 	 * This is a somewhat unwrapped loop to deal with
    418  1.1  gwr 	 * copying the PROM's 'phsymem' banks into the pmap's
    419  1.1  gwr 	 * banks.  The following is always assumed:
    420  1.1  gwr 	 * 1. There is always at least one bank of memory.
    421  1.1  gwr 	 * 2. There is always a last bank of memory, and its
    422  1.1  gwr 	 *    pmem_next member must be set to NULL.
    423  1.1  gwr 	 * XXX - Use: do { ... } while (membank->next) instead?
    424  1.1  gwr 	 * XXX - Why copy this stuff at all? -gwr
    425  1.1  gwr 	 */
    426  1.1  gwr 	membank = romVectorPtr->v_physmemory;
    427  1.1  gwr 	pmap_membank = avail_mem;
    428  1.1  gwr 	total_phys_mem = 0;
    429  1.1  gwr 
    430  1.1  gwr 	while (membank->next) {
    431  1.1  gwr 		pmap_membank->pmem_start = membank->address;
    432  1.1  gwr 		pmap_membank->pmem_end = membank->address + membank->size;
    433  1.1  gwr 		total_phys_mem += membank->size;
    434  1.1  gwr 		/* This silly syntax arises because pmap_membank
    435  1.1  gwr 		 * is really a pre-allocated array, but it is put into
    436  1.1  gwr 		 * use as a linked list.
    437  1.1  gwr 		 */
    438  1.1  gwr 		pmap_membank->pmem_next = pmap_membank + 1;
    439  1.1  gwr 		pmap_membank = pmap_membank->pmem_next;
    440  1.1  gwr 		membank = membank->next;
    441  1.1  gwr 	}
    442  1.1  gwr 
    443  1.1  gwr 	/*
    444  1.1  gwr 	 * XXX The last bank of memory should be reduced to exclude the
    445  1.1  gwr 	 * physical pages needed by the PROM monitor from being used
    446  1.1  gwr 	 * in the VM system.  XXX - See below - Fix!
    447  1.1  gwr 	 */
    448  1.1  gwr 	pmap_membank->pmem_start = membank->address;
    449  1.1  gwr 	pmap_membank->pmem_end = membank->address + membank->size;
    450  1.1  gwr 	pmap_membank->pmem_next = NULL;
    451  1.1  gwr 
    452  1.1  gwr #if 0	/* XXX - Need to integrate this! */
    453  1.1  gwr 	/*
    454  1.1  gwr 	 * The last few pages of physical memory are "owned" by
    455  1.1  gwr 	 * the PROM.  The total amount of memory we are allowed
    456  1.1  gwr 	 * to use is given by the romvec pointer. -gwr
    457  1.1  gwr 	 *
    458  1.1  gwr 	 * We should dedicate different variables for 'useable'
    459  1.1  gwr 	 * and 'physically available'.  Most users are used to the
    460  1.1  gwr 	 * kernel reporting the amount of memory 'physically available'
    461  1.1  gwr 	 * as opposed to 'useable by the kernel' at boot time. -j
    462  1.1  gwr 	 */
    463  1.1  gwr 	total_phys_mem = *romVectorPtr->memoryAvail;
    464  1.1  gwr #endif	/* XXX */
    465  1.1  gwr 
    466  1.1  gwr 	total_phys_mem += membank->size;	/* XXX see above */
    467  1.1  gwr 	physmem = btoc(total_phys_mem);
    468  1.1  gwr 	avail_end = pmap_membank->pmem_end;
    469  1.1  gwr 	avail_end = sun3x_trunc_page(avail_end);
    470  1.1  gwr 
    471  1.1  gwr 	/* XXX - End moved stuff. */
    472  1.1  gwr 
    473  1.1  gwr 	/*
    474  1.1  gwr 	 * The first step is to allocate MMU tables.
    475  1.1  gwr 	 * Note: All must be aligned on 256 byte boundaries.
    476  1.1  gwr 	 *
    477  1.1  gwr 	 * Start with the top level, or 'A' table.
    478  1.1  gwr 	 */
    479  1.1  gwr 	kernAbase = (mmu_long_dte_t *) virtual_avail;
    480  1.1  gwr 	size = sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE;
    481  1.1  gwr 	bzero(kernAbase, size);
    482  1.1  gwr 	avail_start += size;
    483  1.1  gwr 	virtual_avail += size;
    484  1.1  gwr 
    485  1.1  gwr 	/* Allocate enough B tables to map from KERNBASE to
    486  1.1  gwr 	 * the end of VM.
    487  1.1  gwr 	 */
    488  1.1  gwr 	kernBbase = (mmu_short_dte_t *) virtual_avail;
    489  1.1  gwr 	size = sizeof(mmu_short_dte_t) *
    490  1.1  gwr 		(MMU_A_TBL_SIZE - MMU_TIA(KERNBASE)) * MMU_B_TBL_SIZE;
    491  1.1  gwr 	bzero(kernBbase, size);
    492  1.1  gwr 	avail_start += size;
    493  1.1  gwr 	virtual_avail += size;
    494  1.1  gwr 
    495  1.1  gwr 	/* Allocate enough C tables. */
    496  1.1  gwr 	kernCbase = (mmu_short_pte_t *) virtual_avail;
    497  1.1  gwr 	size = sizeof (mmu_short_pte_t) *
    498  1.1  gwr 		(MMU_A_TBL_SIZE - MMU_TIA(KERNBASE))
    499  1.1  gwr 		* MMU_B_TBL_SIZE * MMU_C_TBL_SIZE;
    500  1.1  gwr 	bzero(kernCbase, size);
    501  1.1  gwr 	avail_start += size;
    502  1.1  gwr 	virtual_avail += size;
    503  1.1  gwr 
    504  1.1  gwr 	/* For simplicity, the kernel's mappings will be editable as a
    505  1.1  gwr 	 * flat array of page table entries at kernCbase.  The
    506  1.1  gwr 	 * higher level 'A' and 'B' tables must be initialized to point
    507  1.1  gwr 	 * to this lower one.
    508  1.1  gwr 	 */
    509  1.1  gwr 	b = c = 0;
    510  1.1  gwr 
    511  1.1  gwr 	/* Invalidate all mappings below KERNBASE in the A table.
    512  1.1  gwr 	 * This area has already been zeroed out, but it is good
    513  1.1  gwr 	 * practice to explicitly show that we are interpreting
    514  1.1  gwr 	 * it as a list of A table descriptors.
    515  1.1  gwr 	 */
    516  1.1  gwr 	for (i = 0; i < MMU_TIA(KERNBASE); i++) {
    517  1.1  gwr 		kernAbase[i].addr.raw = 0;
    518  1.1  gwr 	}
    519  1.1  gwr 
    520  1.1  gwr 	/* Set up the kernel A and B tables so that they will reference the
    521  1.1  gwr 	 * correct spots in the contiguous table of PTEs allocated for the
    522  1.1  gwr 	 * kernel's virtual memory space.
    523  1.1  gwr 	 */
    524  1.1  gwr 	for (i = MMU_TIA(KERNBASE); i < MMU_A_TBL_SIZE; i++) {
    525  1.1  gwr 		kernAbase[i].attr.raw =
    526  1.1  gwr 			MMU_LONG_DTE_LU | MMU_LONG_DTE_SUPV | MMU_DT_SHORT;
    527  1.1  gwr 		kernAbase[i].addr.raw = (unsigned long) mmu_vtop(&kernBbase[b]);
    528  1.1  gwr 
    529  1.1  gwr 		for (j=0; j < MMU_B_TBL_SIZE; j++) {
    530  1.1  gwr 			kernBbase[b + j].attr.raw =
    531  1.1  gwr 				(unsigned long) mmu_vtop(&kernCbase[c])
    532  1.1  gwr 				| MMU_DT_SHORT;
    533  1.1  gwr 			c += MMU_C_TBL_SIZE;
    534  1.1  gwr 		}
    535  1.1  gwr 		b += MMU_B_TBL_SIZE;
    536  1.1  gwr 	}
    537  1.1  gwr 
    538  1.1  gwr 	/*
    539  1.1  gwr 	 * Now pmap_enter_kernel() may be used safely and will be
    540  1.1  gwr 	 * the main interface used by _startup.c and other various
    541  1.1  gwr 	 * modules to modify kernel mappings.
    542  1.1  gwr 	 *
    543  1.1  gwr 	 * Note: Our tables will NOT have the default linear mappings!
    544  1.1  gwr 	 */
    545  1.1  gwr 	va = (vm_offset_t) KERNBASE;
    546  1.1  gwr 	pa = mmu_vtop(KERNBASE);
    547  1.1  gwr 
    548  1.1  gwr 	/*
    549  1.1  gwr 	 * The first page is the msgbuf page (data, non-cached).
    550  1.1  gwr 	 * Just fixup the mapping here; setup is in cpu_startup().
    551  1.1  gwr 	 * XXX - Make it non-cached?
    552  1.1  gwr 	 */
    553  1.1  gwr 	pmap_enter_kernel(va, pa|PMAP_NC, VM_PROT_ALL);
    554  1.1  gwr 	va += NBPG; pa += NBPG;
    555  1.1  gwr 
    556  1.1  gwr 	/* The tmporary stack page. */
    557  1.1  gwr 	pmap_enter_kernel(va, pa, VM_PROT_ALL);
    558  1.1  gwr 	va += NBPG; pa += NBPG;
    559  1.1  gwr 
    560  1.1  gwr 	/*
    561  1.1  gwr 	 * Map all of the kernel's text segment as read-only and cacheable.
    562  1.1  gwr 	 * (Cacheable is implied by default).  Unfortunately, the last bytes
    563  1.1  gwr 	 * of kernel text and the first bytes of kernel data will often be
    564  1.1  gwr 	 * sharing the same page.  Therefore, the last page of kernel text
    565  1.1  gwr 	 * has to be mapped as read/write, to accomodate the data.
    566  1.1  gwr 	 */
    567  1.1  gwr 	eva = sun3x_trunc_page((vm_offset_t)etext);
    568  1.1  gwr 	for (; va < eva; pa += NBPG, va += NBPG)
    569  1.1  gwr 		pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_EXECUTE);
    570  1.1  gwr 
    571  1.1  gwr 	/* Map all of the kernel's data (including BSS) segment as read/write
    572  1.1  gwr 	 * and cacheable.
    573  1.1  gwr 	 */
    574  1.1  gwr 	for (; va < (vm_offset_t) esym; pa += NBPG, va += NBPG)
    575  1.1  gwr 		pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_WRITE);
    576  1.1  gwr 
    577  1.1  gwr 	/* Map all of the data we have allocated since the start of this
    578  1.1  gwr 	 * function.
    579  1.1  gwr 	 */
    580  1.1  gwr 	for (; va < virtual_avail; va += NBPG, pa += NBPG)
    581  1.1  gwr 		pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_WRITE);
    582  1.1  gwr 
    583  1.1  gwr 	/* Set 'last_mapped' to the address of the last physical page
    584  1.1  gwr 	 * that was mapped in the kernel.  This variable is used by
    585  1.1  gwr 	 * pmap_bootstrap_alloc() to determine when it needs to map
    586  1.1  gwr 	 * a new page.
    587  1.1  gwr 	 *
    588  1.1  gwr 	 * XXX - This can be a lot simpler.  We already know that the
    589  1.1  gwr 	 * first 4MB of memory (at least) is mapped PA=VA-KERNBASE,
    590  1.1  gwr 	 * so we should never need to creat any new mappings. -gwr
    591  1.1  gwr 	 *
    592  1.1  gwr 	 * True, but it only remains so as long as we are using the
    593  1.1  gwr 	 * ROM's CRP.  Unless, of course, we copy these mappings into
    594  1.1  gwr 	 * our table. -j
    595  1.1  gwr 	 */
    596  1.1  gwr 	last_mapped = sun3x_trunc_page(pa - (NBPG - 1));
    597  1.1  gwr 
    598  1.1  gwr 	/* It is now safe to use pmap_bootstrap_alloc(). */
    599  1.1  gwr 
    600  1.1  gwr 	pmap_alloc_usermmu();	/* Allocate user MMU tables.        */
    601  1.1  gwr 	pmap_alloc_usertmgr();	/* Allocate user MMU table managers.*/
    602  1.1  gwr 	pmap_alloc_pv();	/* Allocate physical->virtual map.  */
    603  1.1  gwr 	pmap_alloc_etc();	/* Allocate miscelaneous things.    */
    604  1.1  gwr 
    605  1.1  gwr 	/* Notify the VM system of our page size. */
    606  1.1  gwr 	PAGE_SIZE = NBPG;
    607  1.1  gwr 	vm_set_page_size();
    608  1.1  gwr 
    609  1.1  gwr 	/* XXX - Attention: moved stuff. */
    610  1.1  gwr 
    611  1.1  gwr 	/*
    612  1.1  gwr 	 * XXX - Make sure avail_start is within the low 4M range
    613  1.1  gwr 	 * that the Sun PROM guarantees will be mapped in?
    614  1.1  gwr 	 * Make sure it is below avail_end as well?
    615  1.1  gwr 	 */
    616  1.1  gwr 
    617  1.1  gwr 	/*
    618  1.1  gwr 	 * Now steal some virtual addresses, but
    619  1.1  gwr 	 * not the physical pages behind them.
    620  1.1  gwr 	 */
    621  1.1  gwr 
    622  1.1  gwr 	/*
    623  1.1  gwr 	 * vpages array:  just some virtual addresses for
    624  1.1  gwr 	 * temporary mappings in the pmap module (two pages)
    625  1.1  gwr 	 */
    626  1.1  gwr 	pmap_bootstrap_aalign(NBPG);
    627  1.1  gwr 	tmp_vpages[0] = virtual_avail;
    628  1.1  gwr 	virtual_avail += NBPG;
    629  1.1  gwr 	tmp_vpages[1] = virtual_avail;
    630  1.1  gwr 	virtual_avail += NBPG;
    631  1.1  gwr 
    632  1.1  gwr 	/* XXX - End moved stuff. */
    633  1.1  gwr 
    634  1.1  gwr 	/* It should be noted that none of these mappings take
    635  1.1  gwr 	 * effect until the MMU's root pointer is
    636  1.1  gwr 	 * is changed from the PROM map, to our own.
    637  1.1  gwr 	 */
    638  1.1  gwr 	pmap_bootstrap_copyprom();
    639  1.1  gwr 	pmap_takeover_mmu();
    640  1.1  gwr }
    641  1.1  gwr 
    642  1.1  gwr 
    643  1.1  gwr /* pmap_alloc_usermmu			INTERNAL
    644  1.1  gwr  **
    645  1.1  gwr  * Called from pmap_bootstrap() to allocate MMU tables that will
    646  1.1  gwr  * eventually be used for user mappings.
    647  1.1  gwr  */
    648  1.1  gwr void
    649  1.1  gwr pmap_alloc_usermmu()
    650  1.1  gwr {
    651  1.1  gwr 	/* Allocate user MMU tables.
    652  1.1  gwr 	 * These must be aligned on 256 byte boundaries.
    653  1.1  gwr 	 */
    654  1.1  gwr 	pmap_bootstrap_aalign(256);
    655  1.1  gwr 	mmuAbase = (mmu_long_dte_t *)
    656  1.1  gwr 		pmap_bootstrap_alloc(sizeof(mmu_long_dte_t)
    657  1.1  gwr 		* MMU_A_TBL_SIZE
    658  1.1  gwr 		* NUM_A_TABLES);
    659  1.1  gwr 	mmuBbase = (mmu_short_dte_t *)
    660  1.1  gwr 		pmap_bootstrap_alloc(sizeof(mmu_short_dte_t)
    661  1.1  gwr 		* MMU_B_TBL_SIZE
    662  1.1  gwr 		* NUM_B_TABLES);
    663  1.1  gwr 	mmuCbase = (mmu_short_pte_t *)
    664  1.1  gwr 		pmap_bootstrap_alloc(sizeof(mmu_short_pte_t)
    665  1.1  gwr 		* MMU_C_TBL_SIZE
    666  1.1  gwr 		* NUM_C_TABLES);
    667  1.1  gwr }
    668  1.1  gwr 
    669  1.1  gwr /* pmap_alloc_pv			INTERNAL
    670  1.1  gwr  **
    671  1.1  gwr  * Called from pmap_bootstrap() to allocate the physical
    672  1.1  gwr  * to virtual mapping list.  Each physical page of memory
    673  1.1  gwr  * in the system has a corresponding element in this list.
    674  1.1  gwr  */
    675  1.1  gwr void
    676  1.1  gwr pmap_alloc_pv()
    677  1.1  gwr {
    678  1.1  gwr 	int	i;
    679  1.1  gwr 	unsigned int	total_mem;
    680  1.1  gwr 
    681  1.1  gwr 	/* Allocate a pv_head structure for every page of physical
    682  1.1  gwr 	 * memory that will be managed by the system.  Since memory on
    683  1.1  gwr 	 * the 3/80 is non-contiguous, we cannot arrive at a total page
    684  1.1  gwr 	 * count by subtraction of the lowest available address from the
    685  1.1  gwr 	 * highest, but rather we have to step through each memory
    686  1.1  gwr 	 * bank and add the number of pages in each to the total.
    687  1.1  gwr 	 *
    688  1.1  gwr 	 * At this time we also initialize the offset of each bank's
    689  1.1  gwr 	 * starting pv_head within the pv_head list so that the physical
    690  1.1  gwr 	 * memory state routines (pmap_is_referenced(),
    691  1.1  gwr 	 * pmap_is_modified(), et al.) can quickly find coresponding
    692  1.1  gwr 	 * pv_heads in spite of the non-contiguity.
    693  1.1  gwr 	 */
    694  1.1  gwr 
    695  1.1  gwr 	total_mem = 0;
    696  1.1  gwr 	for (i = 0; i < SUN3X_80_MEM_BANKS; i++) {
    697  1.1  gwr 		avail_mem[i].pmem_pvbase = sun3x_btop(total_mem);
    698  1.1  gwr 		total_mem += avail_mem[i].pmem_end -
    699  1.1  gwr 			avail_mem[i].pmem_start;
    700  1.1  gwr 		if (avail_mem[i].pmem_next == NULL)
    701  1.1  gwr 			break;
    702  1.1  gwr 	}
    703  1.1  gwr #ifdef	PMAP_DEBUG
    704  1.1  gwr 	if (total_mem != total_phys_mem)
    705  1.1  gwr 		panic("pmap_alloc_pv did not arrive at correct page count");
    706  1.1  gwr #endif
    707  1.1  gwr 
    708  1.1  gwr 	pvbase = (pv_t *) pmap_bootstrap_alloc(sizeof(pv_t) *
    709  1.1  gwr 		sun3x_btop(total_phys_mem));
    710  1.1  gwr }
    711  1.1  gwr 
    712  1.1  gwr /* pmap_alloc_usertmgr			INTERNAL
    713  1.1  gwr  **
    714  1.1  gwr  * Called from pmap_bootstrap() to allocate the structures which
    715  1.1  gwr  * facilitate management of user MMU tables.  Each user MMU table
    716  1.1  gwr  * in the system has one such structure associated with it.
    717  1.1  gwr  */
    718  1.1  gwr void
    719  1.1  gwr pmap_alloc_usertmgr()
    720  1.1  gwr {
    721  1.1  gwr 	/* Allocate user MMU table managers */
    722  1.1  gwr 	/* XXX - It would be a lot simpler to just make these BSS. -gwr */
    723  1.1  gwr 	Atmgrbase = (a_tmgr_t *) pmap_bootstrap_alloc(sizeof(a_tmgr_t)
    724  1.1  gwr 		* NUM_A_TABLES);
    725  1.1  gwr 	Btmgrbase = (b_tmgr_t *) pmap_bootstrap_alloc(sizeof(b_tmgr_t)
    726  1.1  gwr 		* NUM_B_TABLES);
    727  1.1  gwr 	Ctmgrbase = (c_tmgr_t *) pmap_bootstrap_alloc(sizeof(c_tmgr_t)
    728  1.1  gwr 		* NUM_C_TABLES);
    729  1.1  gwr 
    730  1.1  gwr 	/* Allocate PV list elements for the physical to virtual
    731  1.1  gwr 	 * mapping system.
    732  1.1  gwr 	 */
    733  1.1  gwr 	pvebase = (pv_elem_t *) pmap_bootstrap_alloc(
    734  1.1  gwr 		sizeof(struct pv_elem_struct)
    735  1.1  gwr 		* MMU_C_TBL_SIZE
    736  1.1  gwr 		* NUM_C_TABLES );
    737  1.1  gwr }
    738  1.1  gwr 
    739  1.1  gwr /* pmap_alloc_etc			INTERNAL
    740  1.1  gwr  **
    741  1.1  gwr  * Called from pmap_bootstrap() to allocate any remaining pieces
    742  1.1  gwr  * that didn't fit neatly into any of the other pmap_alloc
    743  1.1  gwr  * functions.
    744  1.1  gwr  */
    745  1.1  gwr void
    746  1.1  gwr pmap_alloc_etc()
    747  1.1  gwr {
    748  1.1  gwr 	/* Allocate an A table manager for the kernel_pmap */
    749  1.1  gwr 	proc0Atmgr = (a_tmgr_t *) pmap_bootstrap_alloc(sizeof(a_tmgr_t));
    750  1.1  gwr }
    751  1.1  gwr 
    752  1.1  gwr /* pmap_bootstrap_copyprom()			INTERNAL
    753  1.1  gwr  **
    754  1.1  gwr  * Copy the PROM mappings into our own tables.  Note, we
    755  1.1  gwr  * can use physical addresses until __bootstrap returns.
    756  1.1  gwr  */
    757  1.1  gwr void
    758  1.1  gwr pmap_bootstrap_copyprom()
    759  1.1  gwr {
    760  1.1  gwr #if 0	/* XXX - Just history... */
    761  1.1  gwr 	/*
    762  1.1  gwr 	 * XXX - This method makes DVMA difficult, because
    763  1.1  gwr 	 * the PROM only provides PTEs for 1M of DVMA space.
    764  1.1  gwr 	 * That's OK for boot programs, but not a VM system.
    765  1.1  gwr 	 */
    766  1.1  gwr 	int a_idx;
    767  1.1  gwr 	mmu_long_dte_t *mon_atbl;
    768  1.1  gwr 
    769  1.1  gwr 	/*
    770  1.1  gwr 	 * Copy the last entry (PROM monitor and DVMA mappings)
    771  1.1  gwr 	 * so our level-A table will use the PROM level-B table.
    772  1.1  gwr 	 */
    773  1.1  gwr 	mon_atbl = (mmu_long_dte_t *) mon_crp.paddr;
    774  1.1  gwr 	a_idx = MMU_TIA(VM_MAX_KERNEL_ADDRESS);
    775  1.1  gwr 	kernAbase[a_idx].attr.raw = mon_atbl[a_idx].attr.raw;
    776  1.1  gwr 	kernAbase[a_idx].addr.raw = mon_atbl[a_idx].addr.raw;
    777  1.1  gwr 
    778  1.1  gwr #endif
    779  1.1  gwr #if 0	/* XXX - More history... */
    780  1.1  gwr 	/*
    781  1.1  gwr 	 * XXX - This method is OK with our DVMA implementation,
    782  1.1  gwr 	 * but causes pmap_extract() to be ignorant of the PROM
    783  1.1  gwr 	 * mappings.  Maybe that's OK.  If not, we should just
    784  1.1  gwr 	 * copy the PTEs (level-C) from the PROM. -gwr
    785  1.1  gwr 	 */
    786  1.1  gwr 	mmu_long_dte_t *mon_atbl;
    787  1.1  gwr 	mmu_short_dte_t *mon_btbl;
    788  1.1  gwr 	mmu_short_dte_t *our_btbl;
    789  1.1  gwr 	int a_idx, b_idx;
    790  1.1  gwr 
    791  1.1  gwr 	/*
    792  1.1  gwr 	 * Copy parts of the level-B table from the PROM for
    793  1.1  gwr 	 * mappings that the PROM cares about.
    794  1.1  gwr 	 */
    795  1.1  gwr 	mon_atbl = (mmu_long_dte_t *) mon_crp.paddr;
    796  1.1  gwr 	a_idx = MMU_TIA(MON_KDB_START);
    797  1.1  gwr 	mon_btbl = (mmu_short_dte_t *) mon_atbl[a_idx].addr.raw;
    798  1.1  gwr 
    799  1.1  gwr 	/* Temporary use of b_idx to find our level-B table. */
    800  1.1  gwr 	b_idx = MMU_B_TBL_SIZE * (a_idx - MMU_TIA(KERNBASE));
    801  1.1  gwr 	our_btbl = &kernBbase[b_idx];
    802  1.1  gwr 
    803  1.1  gwr 	/*
    804  1.1  gwr 	 * Preserve both the kadb and monitor mappings (2MB).
    805  1.1  gwr 	 * We could have started at MONSTART, but this costs
    806  1.1  gwr 	 * us nothing, and might be useful someday...
    807  1.1  gwr 	 */
    808  1.1  gwr 	for (b_idx = MMU_TIB(MON_KDB_START);
    809  1.1  gwr 		 b_idx < MMU_TIB(MONEND); b_idx++)
    810  1.1  gwr 		our_btbl[b_idx].attr.raw = mon_btbl[b_idx].attr.raw;
    811  1.1  gwr 
    812  1.1  gwr 	/*
    813  1.1  gwr 	 * Preserve the monitor's DVMA map for now (1MB).
    814  1.1  gwr 	 * Later, we might want to kill this mapping so we
    815  1.1  gwr 	 * can have all of the DVMA space (16MB).
    816  1.1  gwr 	 */
    817  1.1  gwr 	for (b_idx = MMU_TIB(MON_DVMA_BASE);
    818  1.1  gwr 		 b_idx < MMU_B_TBL_SIZE; b_idx++)
    819  1.1  gwr 		our_btbl[b_idx].attr.raw = mon_btbl[b_idx].attr.raw;
    820  1.1  gwr #endif
    821  1.1  gwr 	MachMonRomVector *romp;
    822  1.1  gwr 	int *mon_ctbl;
    823  1.1  gwr 	mmu_short_pte_t *kpte;
    824  1.1  gwr 	int i, len;
    825  1.1  gwr 
    826  1.1  gwr 	romp = romVectorPtr;
    827  1.1  gwr 
    828  1.1  gwr 	/*
    829  1.1  gwr 	 * Copy the mappings in MON_KDB_START...MONEND
    830  1.1  gwr 	 * Note: mon_ctbl[0] maps MON_KDB_START
    831  1.1  gwr 	 */
    832  1.1  gwr 	mon_ctbl = *romp->monptaddr;
    833  1.1  gwr 	i = sun3x_btop(MON_KDB_START - KERNBASE);
    834  1.1  gwr 	kpte = &kernCbase[i];
    835  1.1  gwr 	len = sun3x_btop(MONEND - MON_KDB_START);
    836  1.1  gwr 
    837  1.1  gwr 	for (i = 0; i < len; i++) {
    838  1.1  gwr 		kpte[i].attr.raw = mon_ctbl[i];
    839  1.1  gwr 	}
    840  1.1  gwr 
    841  1.1  gwr 	/*
    842  1.1  gwr 	 * Copy the mappings at MON_DVMA_BASE (to the end).
    843  1.1  gwr 	 * Note, in here, mon_ctbl[0] maps MON_DVMA_BASE.
    844  1.1  gwr 	 * XXX - This does not appear to be necessary, but
    845  1.1  gwr 	 * I'm not sure yet if it is or not. -gwr
    846  1.1  gwr 	 */
    847  1.1  gwr 	mon_ctbl = *romp->shadowpteaddr;
    848  1.1  gwr 	i = sun3x_btop(MON_DVMA_BASE - KERNBASE);
    849  1.1  gwr 	kpte = &kernCbase[i];
    850  1.1  gwr 	len = sun3x_btop(MON_DVMA_SIZE);
    851  1.1  gwr 
    852  1.1  gwr 	for (i = 0; i < len; i++) {
    853  1.1  gwr 		kpte[i].attr.raw = mon_ctbl[i];
    854  1.1  gwr 	}
    855  1.1  gwr }
    856  1.1  gwr 
    857  1.1  gwr /* pmap_takeover_mmu			INTERNAL
    858  1.1  gwr  **
    859  1.1  gwr  * Called from pmap_bootstrap() after it has copied enough of the
    860  1.1  gwr  * PROM mappings into the kernel map so that we can use our own
    861  1.1  gwr  * MMU table.
    862  1.1  gwr  */
    863  1.1  gwr void
    864  1.1  gwr pmap_takeover_mmu()
    865  1.1  gwr {
    866  1.1  gwr 	vm_offset_t tbladdr;
    867  1.1  gwr 
    868  1.1  gwr 	tbladdr = mmu_vtop((vm_offset_t) kernAbase);
    869  1.1  gwr 	mon_printf("pmap_takeover_mmu: tbladdr=0x%x\n", tbladdr);
    870  1.1  gwr 
    871  1.1  gwr 	/* Initialize the CPU Root Pointer (CRP) for proc0. */
    872  1.1  gwr 	/* XXX: I'd prefer per-process CRP storage. -gwr */
    873  1.1  gwr 	proc0crp.limit = 0x80000003;	/* limit and type */
    874  1.1  gwr 	proc0crp.paddr = tbladdr;	/* phys. addr. */
    875  1.1  gwr 	curpcb->pcb_mmuctx = (int) &proc0crp;
    876  1.1  gwr 
    877  1.1  gwr 	mon_printf("pmap_takeover_mmu: loadcrp...\n");
    878  1.1  gwr 	loadcrp((vm_offset_t) &proc0crp);
    879  1.1  gwr 	mon_printf("pmap_takeover_mmu: survived!\n");
    880  1.1  gwr }
    881  1.1  gwr 
    882  1.1  gwr /* pmap_init			INTERFACE
    883  1.1  gwr  **
    884  1.1  gwr  * Called at the end of vm_init() to set up the pmap system to go
    885  1.1  gwr  * into full time operation.
    886  1.1  gwr  */
    887  1.1  gwr void
    888  1.1  gwr pmap_init()
    889  1.1  gwr {
    890  1.1  gwr 	/** Initialize the manager pools **/
    891  1.1  gwr 	TAILQ_INIT(&a_pool);
    892  1.1  gwr 	TAILQ_INIT(&b_pool);
    893  1.1  gwr 	TAILQ_INIT(&c_pool);
    894  1.1  gwr 
    895  1.1  gwr 	/** Initialize the PV system **/
    896  1.1  gwr 	pmap_init_pv();
    897  1.1  gwr 
    898  1.1  gwr 	/** Zero out the kernel's pmap **/
    899  1.1  gwr 	bzero(&kernel_pmap, sizeof(struct pmap));
    900  1.1  gwr 
    901  1.1  gwr 	/* Initialize the A table manager that is used in pmaps which
    902  1.1  gwr 	 * do not have an A table of their own.  This table uses the
    903  1.1  gwr 	 * kernel, or 'proc0' level A MMU table, which contains no valid
    904  1.1  gwr 	 * user space mappings.  Any user process that attempts to execute
    905  1.1  gwr 	 * using this A table will fault.  At which point the VM system will
    906  1.1  gwr 	 * call pmap_enter, which will then allocate it an A table of its own
    907  1.1  gwr 	 * from the pool.
    908  1.1  gwr 	 */
    909  1.1  gwr 	proc0Atmgr->at_dtbl = kernAbase;
    910  1.1  gwr 	proc0Atmgr->at_parent = &kernel_pmap;
    911  1.1  gwr 	kernel_pmap.pm_a_tbl = proc0Atmgr;
    912  1.1  gwr 
    913  1.1  gwr 	/**************************************************************
    914  1.1  gwr 	 * Initialize all tmgr structures and MMU tables they manage. *
    915  1.1  gwr 	 **************************************************************/
    916  1.1  gwr 	/** Initialize A tables **/
    917  1.1  gwr 	pmap_init_a_tables();
    918  1.1  gwr 	/** Initialize B tables **/
    919  1.1  gwr 	pmap_init_b_tables();
    920  1.1  gwr 	/** Initialize C tables **/
    921  1.1  gwr 	pmap_init_c_tables();
    922  1.1  gwr }
    923  1.1  gwr 
    924  1.1  gwr /* pmap_init_a_tables()			INTERNAL
    925  1.1  gwr  **
    926  1.1  gwr  * Initializes all A managers, their MMU A tables, and inserts
    927  1.1  gwr  * them into the A manager pool for use by the system.
    928  1.1  gwr  */
    929  1.1  gwr void
    930  1.1  gwr pmap_init_a_tables()
    931  1.1  gwr {
    932  1.1  gwr 	int i;
    933  1.1  gwr 	a_tmgr_t *a_tbl;
    934  1.1  gwr 
    935  1.1  gwr 	for (i=0; i < NUM_A_TABLES; i++) {
    936  1.1  gwr 		/* Select the next available A manager from the pool */
    937  1.1  gwr 		a_tbl = &Atmgrbase[i];
    938  1.1  gwr 
    939  1.1  gwr 		/* Clear its parent entry.  Set its wired and valid
    940  1.1  gwr 		 * entry count to zero.
    941  1.1  gwr 		 */
    942  1.1  gwr 		a_tbl->at_parent = NULL;
    943  1.1  gwr 		a_tbl->at_wcnt = a_tbl->at_ecnt = 0;
    944  1.1  gwr 
    945  1.1  gwr 		/* Assign it the next available MMU A table from the pool */
    946  1.1  gwr 		a_tbl->at_dtbl = &mmuAbase[i * MMU_A_TBL_SIZE];
    947  1.1  gwr 
    948  1.1  gwr 		/* Initialize the MMU A table with the table in the `proc0',
    949  1.1  gwr 		 * or kernel, mapping.  This ensures that every process has
    950  1.1  gwr 		 * the kernel mapped in the top part of its address space.
    951  1.1  gwr 		 */
    952  1.1  gwr 		bcopy(kernAbase, a_tbl->at_dtbl, MMU_A_TBL_SIZE *
    953  1.1  gwr 			sizeof(mmu_long_dte_t));
    954  1.1  gwr 
    955  1.1  gwr 		/* Finally, insert the manager into the A pool,
    956  1.1  gwr 		 * making it ready to be used by the system.
    957  1.1  gwr 		 */
    958  1.1  gwr 		TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
    959  1.1  gwr     }
    960  1.1  gwr }
    961  1.1  gwr 
    962  1.1  gwr /* pmap_init_b_tables()			INTERNAL
    963  1.1  gwr  **
    964  1.1  gwr  * Initializes all B table managers, their MMU B tables, and
    965  1.1  gwr  * inserts them into the B manager pool for use by the system.
    966  1.1  gwr  */
    967  1.1  gwr void
    968  1.1  gwr pmap_init_b_tables()
    969  1.1  gwr {
    970  1.1  gwr 	int i,j;
    971  1.1  gwr 	b_tmgr_t *b_tbl;
    972  1.1  gwr 
    973  1.1  gwr 	for (i=0; i < NUM_B_TABLES; i++) {
    974  1.1  gwr 		/* Select the next available B manager from the pool */
    975  1.1  gwr 		b_tbl = &Btmgrbase[i];
    976  1.1  gwr 
    977  1.1  gwr 		b_tbl->bt_parent = NULL;	/* clear its parent,  */
    978  1.1  gwr 		b_tbl->bt_pidx = 0;		/* parent index,      */
    979  1.1  gwr 		b_tbl->bt_wcnt = 0;		/* wired entry count, */
    980  1.1  gwr 		b_tbl->bt_ecnt = 0;		/* valid entry count. */
    981  1.1  gwr 
    982  1.1  gwr 		/* Assign it the next available MMU B table from the pool */
    983  1.1  gwr 		b_tbl->bt_dtbl = &mmuBbase[i * MMU_B_TBL_SIZE];
    984  1.1  gwr 
    985  1.1  gwr 		/* Invalidate every descriptor in the table */
    986  1.1  gwr 		for (j=0; j < MMU_B_TBL_SIZE; j++)
    987  1.1  gwr 			b_tbl->bt_dtbl[j].attr.raw = MMU_DT_INVALID;
    988  1.1  gwr 
    989  1.1  gwr 		/* Insert the manager into the B pool */
    990  1.1  gwr 		TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
    991  1.1  gwr 	}
    992  1.1  gwr }
    993  1.1  gwr 
    994  1.1  gwr /* pmap_init_c_tables()			INTERNAL
    995  1.1  gwr  **
    996  1.1  gwr  * Initializes all C table managers, their MMU C tables, and
    997  1.1  gwr  * inserts them into the C manager pool for use by the system.
    998  1.1  gwr  */
    999  1.1  gwr void
   1000  1.1  gwr pmap_init_c_tables()
   1001  1.1  gwr {
   1002  1.1  gwr 	int i,j;
   1003  1.1  gwr 	c_tmgr_t *c_tbl;
   1004  1.1  gwr 
   1005  1.1  gwr 	for (i=0; i < NUM_C_TABLES; i++) {
   1006  1.1  gwr 		/* Select the next available C manager from the pool */
   1007  1.1  gwr 		c_tbl = &Ctmgrbase[i];
   1008  1.1  gwr 
   1009  1.1  gwr 		c_tbl->ct_parent = NULL;	/* clear its parent,  */
   1010  1.1  gwr 		c_tbl->ct_pidx = 0;		/* parent index,      */
   1011  1.1  gwr 		c_tbl->ct_wcnt = 0;		/* wired entry count, */
   1012  1.1  gwr 		c_tbl->ct_ecnt = 0;		/* valid entry count. */
   1013  1.1  gwr 
   1014  1.1  gwr 		/* Assign it the next available MMU C table from the pool */
   1015  1.1  gwr 		c_tbl->ct_dtbl = &mmuCbase[i * MMU_C_TBL_SIZE];
   1016  1.1  gwr 
   1017  1.1  gwr 		for (j=0; j < MMU_C_TBL_SIZE; j++)
   1018  1.1  gwr 			c_tbl->ct_dtbl[j].attr.raw = MMU_DT_INVALID;
   1019  1.1  gwr 
   1020  1.1  gwr 		TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
   1021  1.1  gwr 	}
   1022  1.1  gwr }
   1023  1.1  gwr 
   1024  1.1  gwr /* pmap_init_pv()			INTERNAL
   1025  1.1  gwr  **
   1026  1.1  gwr  * Initializes the Physical to Virtual mapping system.
   1027  1.1  gwr  */
   1028  1.1  gwr void
   1029  1.1  gwr pmap_init_pv()
   1030  1.1  gwr {
   1031  1.1  gwr 	bzero(pvbase, sizeof(pv_t) * sun3x_btop(total_phys_mem));
   1032  1.1  gwr 	pv_initialized = TRUE;
   1033  1.1  gwr }
   1034  1.1  gwr 
   1035  1.1  gwr /* get_a_table			INTERNAL
   1036  1.1  gwr  **
   1037  1.1  gwr  * Retrieve and return a level A table for use in a user map.
   1038  1.1  gwr  */
   1039  1.1  gwr a_tmgr_t *
   1040  1.1  gwr get_a_table()
   1041  1.1  gwr {
   1042  1.1  gwr 	a_tmgr_t *tbl;
   1043  1.1  gwr 
   1044  1.1  gwr 	/* Get the top A table in the pool */
   1045  1.1  gwr 	tbl = a_pool.tqh_first;
   1046  1.1  gwr 	if (tbl == NULL)
   1047  1.1  gwr 		panic("get_a_table: out of A tables.");
   1048  1.1  gwr 	TAILQ_REMOVE(&a_pool, tbl, at_link);
   1049  1.1  gwr 	/* If the table has a non-null parent pointer then it is in use.
   1050  1.1  gwr 	 * Forcibly abduct it from its parent and clear its entries.
   1051  1.1  gwr 	 * No re-entrancy worries here.  This table would not be in the
   1052  1.1  gwr 	 * table pool unless it was available for use.
   1053  1.1  gwr 	 */
   1054  1.1  gwr 	if (tbl->at_parent) {
   1055  1.1  gwr 		tbl->at_parent->pm_stats.resident_count -= free_a_table(tbl);
   1056  1.1  gwr 		tbl->at_parent->pm_a_tbl = proc0Atmgr;
   1057  1.1  gwr 	}
   1058  1.1  gwr #ifdef  NON_REENTRANT
   1059  1.1  gwr 	/* If the table isn't to be wired down, re-insert it at the
   1060  1.1  gwr 	 * end of the pool.
   1061  1.1  gwr 	 */
   1062  1.1  gwr 	if (!wired)
   1063  1.1  gwr 		/* Quandary - XXX
   1064  1.1  gwr 		 * Would it be better to let the calling function insert this
   1065  1.1  gwr 		 * table into the queue?  By inserting it here, we are allowing
   1066  1.1  gwr 		 * it to be stolen immediately.  The calling function is
   1067  1.1  gwr 		 * probably not expecting to use a table that it is not
   1068  1.1  gwr 		 * assured full control of.
   1069  1.1  gwr 		 * Answer - In the intrest of re-entrancy, it is best to let
   1070  1.1  gwr 		 * the calling function determine when a table is available
   1071  1.1  gwr 		 * for use.  Therefore this code block is not used.
   1072  1.1  gwr 		 */
   1073  1.1  gwr 		TAILQ_INSERT_TAIL(&a_pool, tbl, at_link);
   1074  1.1  gwr #endif	/* NON_REENTRANT */
   1075  1.1  gwr 	return tbl;
   1076  1.1  gwr }
   1077  1.1  gwr 
   1078  1.1  gwr /* get_b_table			INTERNAL
   1079  1.1  gwr  **
   1080  1.1  gwr  * Return a level B table for use.
   1081  1.1  gwr  */
   1082  1.1  gwr b_tmgr_t *
   1083  1.1  gwr get_b_table()
   1084  1.1  gwr {
   1085  1.1  gwr 	b_tmgr_t *tbl;
   1086  1.1  gwr 
   1087  1.1  gwr 	/* See 'get_a_table' for comments. */
   1088  1.1  gwr 	tbl = b_pool.tqh_first;
   1089  1.1  gwr 	if (tbl == NULL)
   1090  1.1  gwr 		panic("get_b_table: out of B tables.");
   1091  1.1  gwr 	TAILQ_REMOVE(&b_pool, tbl, bt_link);
   1092  1.1  gwr 	if (tbl->bt_parent) {
   1093  1.1  gwr 		tbl->bt_parent->at_dtbl[tbl->bt_pidx].attr.raw = MMU_DT_INVALID;
   1094  1.1  gwr 		tbl->bt_parent->at_ecnt--;
   1095  1.1  gwr 		tbl->bt_parent->at_parent->pm_stats.resident_count -=
   1096  1.1  gwr 		    free_b_table(tbl);
   1097  1.1  gwr 	}
   1098  1.1  gwr #ifdef	NON_REENTRANT
   1099  1.1  gwr 	if (!wired)
   1100  1.1  gwr 		/* XXX see quandary in get_b_table */
   1101  1.1  gwr 		/* XXX start lock */
   1102  1.1  gwr 		TAILQ_INSERT_TAIL(&b_pool, tbl, bt_link);
   1103  1.1  gwr 		/* XXX end lock */
   1104  1.1  gwr #endif	/* NON_REENTRANT */
   1105  1.1  gwr 	return tbl;
   1106  1.1  gwr }
   1107  1.1  gwr 
   1108  1.1  gwr /* get_c_table			INTERNAL
   1109  1.1  gwr  **
   1110  1.1  gwr  * Return a level C table for use.
   1111  1.1  gwr  */
   1112  1.1  gwr c_tmgr_t *
   1113  1.1  gwr get_c_table()
   1114  1.1  gwr {
   1115  1.1  gwr 	c_tmgr_t *tbl;
   1116  1.1  gwr 
   1117  1.1  gwr 	/* See 'get_a_table' for comments */
   1118  1.1  gwr 	tbl = c_pool.tqh_first;
   1119  1.1  gwr 	if (tbl == NULL)
   1120  1.1  gwr 		panic("get_c_table: out of C tables.");
   1121  1.1  gwr 	TAILQ_REMOVE(&c_pool, tbl, ct_link);
   1122  1.1  gwr 	if (tbl->ct_parent) {
   1123  1.1  gwr 		tbl->ct_parent->bt_dtbl[tbl->ct_pidx].attr.raw = MMU_DT_INVALID;
   1124  1.1  gwr 		tbl->ct_parent->bt_ecnt--;
   1125  1.1  gwr 		tbl->ct_parent->bt_parent->at_parent->pm_stats.resident_count
   1126  1.1  gwr 		    -= free_c_table(tbl);
   1127  1.1  gwr 	}
   1128  1.1  gwr #ifdef	NON_REENTRANT
   1129  1.1  gwr 	if (!wired)
   1130  1.1  gwr 		/* XXX See quandary in get_a_table */
   1131  1.1  gwr 		/* XXX start lock */
   1132  1.1  gwr 		TAILQ_INSERT_TAIL(&c_pool, tbl, c_link);
   1133  1.1  gwr 		/* XXX end lock */
   1134  1.1  gwr #endif	/* NON_REENTRANT */
   1135  1.1  gwr 
   1136  1.1  gwr 	return tbl;
   1137  1.1  gwr }
   1138  1.1  gwr 
   1139  1.1  gwr /* The following 'free_table' and 'steal_table' functions are called to
   1140  1.1  gwr  * detach tables from their current obligations (parents and children) and
   1141  1.1  gwr  * prepare them for reuse in another mapping.
   1142  1.1  gwr  *
   1143  1.1  gwr  * Free_table is used when the calling function will handle the fate
   1144  1.1  gwr  * of the parent table, such as returning it to the free pool when it has
   1145  1.1  gwr  * no valid entries.  Functions that do not want to handle this should
   1146  1.1  gwr  * call steal_table, in which the parent table's descriptors and entry
   1147  1.1  gwr  * count are automatically modified when this table is removed.
   1148  1.1  gwr  */
   1149  1.1  gwr 
   1150  1.1  gwr /* free_a_table			INTERNAL
   1151  1.1  gwr  **
   1152  1.1  gwr  * Unmaps the given A table and all child tables from their current
   1153  1.1  gwr  * mappings.  Returns the number of pages that were invalidated.
   1154  1.1  gwr  *
   1155  1.1  gwr  * Cache note: The MC68851 will automatically flush all
   1156  1.1  gwr  * descriptors derived from a given A table from its
   1157  1.1  gwr  * Automatic Translation Cache (ATC) if we issue a
   1158  1.1  gwr  * 'PFLUSHR' instruction with the base address of the
   1159  1.1  gwr  * table.  This function should do, and does so.
   1160  1.1  gwr  * Note note: We are using an MC68030 - there is no
   1161  1.1  gwr  * PFLUSHR.
   1162  1.1  gwr  */
   1163  1.1  gwr int
   1164  1.1  gwr free_a_table(a_tbl)
   1165  1.1  gwr 	a_tmgr_t *a_tbl;
   1166  1.1  gwr {
   1167  1.1  gwr 	int i, removed_cnt;
   1168  1.1  gwr 	mmu_long_dte_t	*dte;
   1169  1.1  gwr 	mmu_short_dte_t *dtbl;
   1170  1.1  gwr 	b_tmgr_t	*tmgr;
   1171  1.1  gwr 
   1172  1.1  gwr 	/* Flush the ATC cache of all cached descriptors derived
   1173  1.1  gwr 	 * from this table.
   1174  1.1  gwr 	 * XXX - Sun3x does not use 68851's cached table feature
   1175  1.1  gwr 	 * flush_atc_crp(mmu_vtop(a_tbl->dte));
   1176  1.1  gwr 	 */
   1177  1.1  gwr 
   1178  1.1  gwr 	/* Remove any pending cache flushes that were designated
   1179  1.1  gwr 	 * for the pmap this A table belongs to.
   1180  1.1  gwr 	 * a_tbl->parent->atc_flushq[0] = 0;
   1181  1.1  gwr 	 * XXX - Not implemented in sun3x.
   1182  1.1  gwr 	 */
   1183  1.1  gwr 
   1184  1.1  gwr 	/* All A tables in the system should retain a map for the
   1185  1.1  gwr 	 * kernel. If the table contains any valid descriptors
   1186  1.1  gwr 	 * (other than those for the kernel area), invalidate them all,
   1187  1.1  gwr 	 * stopping short of the kernel's entries.
   1188  1.1  gwr 	 */
   1189  1.1  gwr 	removed_cnt = 0;
   1190  1.1  gwr 	if (a_tbl->at_ecnt) {
   1191  1.1  gwr 		dte = a_tbl->at_dtbl;
   1192  1.1  gwr 		for (i=0; i < MMU_TIA(KERNBASE); i++)
   1193  1.1  gwr 			/* If a table entry points to a valid B table, free
   1194  1.1  gwr 			 * it and its children.
   1195  1.1  gwr 			 */
   1196  1.1  gwr 			if (MMU_VALID_DT(dte[i])) {
   1197  1.1  gwr 				/* The following block does several things,
   1198  1.1  gwr 				 * from innermost expression to the
   1199  1.1  gwr 				 * outermost:
   1200  1.1  gwr 				 * 1) It extracts the base (cc 1996)
   1201  1.1  gwr 				 *    address of the B table pointed
   1202  1.1  gwr 				 *    to in the A table entry dte[i].
   1203  1.1  gwr 				 * 2) It converts this base address into
   1204  1.1  gwr 				 *    the virtual address it can be
   1205  1.1  gwr 				 *    accessed with. (all MMU tables point
   1206  1.1  gwr 				 *    to physical addresses.)
   1207  1.1  gwr 				 * 3) It finds the corresponding manager
   1208  1.1  gwr 				 *    structure which manages this MMU table.
   1209  1.1  gwr 				 * 4) It frees the manager structure.
   1210  1.1  gwr 				 *    (This frees the MMU table and all
   1211  1.1  gwr 				 *    child tables. See 'free_b_table' for
   1212  1.1  gwr 				 *    details.)
   1213  1.1  gwr 				 */
   1214  1.1  gwr 				dtbl = (mmu_short_dte_t *) MMU_DTE_PA(dte[i]);
   1215  1.1  gwr 				dtbl = (mmu_short_dte_t *) mmu_ptov(dtbl);
   1216  1.1  gwr 				tmgr = mmuB2tmgr(dtbl);
   1217  1.1  gwr 				removed_cnt += free_b_table(tmgr);
   1218  1.1  gwr 			}
   1219  1.1  gwr 	}
   1220  1.1  gwr 	a_tbl->at_ecnt = 0;
   1221  1.1  gwr 	return removed_cnt;
   1222  1.1  gwr }
   1223  1.1  gwr 
   1224  1.1  gwr /* free_b_table			INTERNAL
   1225  1.1  gwr  **
   1226  1.1  gwr  * Unmaps the given B table and all its children from their current
   1227  1.1  gwr  * mappings.  Returns the number of pages that were invalidated.
   1228  1.1  gwr  * (For comments, see 'free_a_table()').
   1229  1.1  gwr  */
   1230  1.1  gwr int
   1231  1.1  gwr free_b_table(b_tbl)
   1232  1.1  gwr 	b_tmgr_t *b_tbl;
   1233  1.1  gwr {
   1234  1.1  gwr 	int i, removed_cnt;
   1235  1.1  gwr 	mmu_short_dte_t *dte;
   1236  1.1  gwr 	mmu_short_pte_t	*dtbl;
   1237  1.1  gwr 	c_tmgr_t	*tmgr;
   1238  1.1  gwr 
   1239  1.1  gwr 	removed_cnt = 0;
   1240  1.1  gwr 	if (b_tbl->bt_ecnt) {
   1241  1.1  gwr 		dte = b_tbl->bt_dtbl;
   1242  1.1  gwr 		for (i=0; i < MMU_B_TBL_SIZE; i++)
   1243  1.1  gwr 			if (MMU_VALID_DT(dte[i])) {
   1244  1.1  gwr 				dtbl = (mmu_short_pte_t *) MMU_DTE_PA(dte[i]);
   1245  1.1  gwr 				dtbl = (mmu_short_pte_t *) mmu_ptov(dtbl);
   1246  1.1  gwr 				tmgr = mmuC2tmgr(dtbl);
   1247  1.1  gwr 				removed_cnt += free_c_table(tmgr);
   1248  1.1  gwr 			}
   1249  1.1  gwr 	}
   1250  1.1  gwr 
   1251  1.1  gwr 	b_tbl->bt_ecnt = 0;
   1252  1.1  gwr 	return removed_cnt;
   1253  1.1  gwr }
   1254  1.1  gwr 
   1255  1.1  gwr /* free_c_table			INTERNAL
   1256  1.1  gwr  **
   1257  1.1  gwr  * Unmaps the given C table from use and returns it to the pool for
   1258  1.1  gwr  * re-use.  Returns the number of pages that were invalidated.
   1259  1.1  gwr  *
   1260  1.1  gwr  * This function preserves any physical page modification information
   1261  1.1  gwr  * contained in the page descriptors within the C table by calling
   1262  1.1  gwr  * 'pmap_remove_pte().'
   1263  1.1  gwr  */
   1264  1.1  gwr int
   1265  1.1  gwr free_c_table(c_tbl)
   1266  1.1  gwr 	c_tmgr_t *c_tbl;
   1267  1.1  gwr {
   1268  1.1  gwr 	int i, removed_cnt;
   1269  1.1  gwr 
   1270  1.1  gwr 	removed_cnt = 0;
   1271  1.1  gwr 	if (c_tbl->ct_ecnt)
   1272  1.1  gwr 		for (i=0; i < MMU_C_TBL_SIZE; i++)
   1273  1.1  gwr 			if (MMU_VALID_DT(c_tbl->ct_dtbl[i])) {
   1274  1.1  gwr 				pmap_remove_pte(&c_tbl->ct_dtbl[i]);
   1275  1.1  gwr 				removed_cnt++;
   1276  1.1  gwr 			}
   1277  1.1  gwr 	c_tbl->ct_ecnt = 0;
   1278  1.1  gwr 	return removed_cnt;
   1279  1.1  gwr }
   1280  1.1  gwr 
   1281  1.1  gwr /* free_c_table_novalid			INTERNAL
   1282  1.1  gwr  **
   1283  1.1  gwr  * Frees the given C table manager without checking to see whether
   1284  1.1  gwr  * or not it contains any valid page descriptors as it is assumed
   1285  1.1  gwr  * that it does not.
   1286  1.1  gwr  */
   1287  1.1  gwr void
   1288  1.1  gwr free_c_table_novalid(c_tbl)
   1289  1.1  gwr 	c_tmgr_t *c_tbl;
   1290  1.1  gwr {
   1291  1.1  gwr 	TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
   1292  1.1  gwr 	TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
   1293  1.1  gwr 	c_tbl->ct_parent->bt_dtbl[c_tbl->ct_pidx].attr.raw = MMU_DT_INVALID;
   1294  1.1  gwr }
   1295  1.1  gwr 
   1296  1.1  gwr /* pmap_remove_pte			INTERNAL
   1297  1.1  gwr  **
   1298  1.1  gwr  * Unmap the given pte and preserve any page modification
   1299  1.1  gwr  * information by transfering it to the pv head of the
   1300  1.1  gwr  * physical page it maps to.  This function does not update
   1301  1.1  gwr  * any reference counts because it is assumed that the calling
   1302  1.1  gwr  * function will do so.  If the calling function does not have the
   1303  1.1  gwr  * ability to do so, the function pmap_dereference_pte() exists
   1304  1.1  gwr  * for this purpose.
   1305  1.1  gwr  */
   1306  1.1  gwr void
   1307  1.1  gwr pmap_remove_pte(pte)
   1308  1.1  gwr 	mmu_short_pte_t *pte;
   1309  1.1  gwr {
   1310  1.1  gwr 	vm_offset_t pa;
   1311  1.1  gwr 	pv_t       *pv;
   1312  1.1  gwr 	pv_elem_t  *pve;
   1313  1.1  gwr 
   1314  1.1  gwr 	pa = MMU_PTE_PA(*pte);
   1315  1.1  gwr 	if (is_managed(pa)) {
   1316  1.1  gwr 		pv = pa2pv(pa);
   1317  1.1  gwr 		/* Save the mod/ref bits of the pte by simply
   1318  1.1  gwr 		 * ORing the entire pte onto the pv_flags member
   1319  1.1  gwr 		 * of the pv structure.
   1320  1.1  gwr 		 * There is no need to use a separate bit pattern
   1321  1.1  gwr 		 * for usage information on the pv head than that
   1322  1.1  gwr 		 * which is used on the MMU ptes.
   1323  1.1  gwr 		 */
   1324  1.1  gwr 		pv->pv_flags |= pte->attr.raw;
   1325  1.1  gwr 
   1326  1.1  gwr 		pve = pte2pve(pte);
   1327  1.1  gwr 		if (pve == pv->pv_head.lh_first)
   1328  1.1  gwr 			pv->pv_head.lh_first = pve->pve_link.le_next;
   1329  1.1  gwr 		LIST_REMOVE(pve, pve_link);
   1330  1.1  gwr 	}
   1331  1.1  gwr 
   1332  1.1  gwr 	pte->attr.raw = MMU_DT_INVALID;
   1333  1.1  gwr }
   1334  1.1  gwr 
   1335  1.1  gwr /* pmap_dereference_pte			INTERNAL
   1336  1.1  gwr  **
   1337  1.1  gwr  * Update the necessary reference counts in any tables and pmaps to
   1338  1.1  gwr  * reflect the removal of the given pte.  Only called when no knowledge of
   1339  1.1  gwr  * the pte's associated pmap is unknown.  This only occurs in the PV call
   1340  1.1  gwr  * 'pmap_page_protect()' with a protection of VM_PROT_NONE, which means
   1341  1.1  gwr  * that all references to a given physical page must be removed.
   1342  1.1  gwr  */
   1343  1.1  gwr void
   1344  1.1  gwr pmap_dereference_pte(pte)
   1345  1.1  gwr 	mmu_short_pte_t *pte;
   1346  1.1  gwr {
   1347  1.1  gwr 	c_tmgr_t *c_tbl;
   1348  1.1  gwr 
   1349  1.1  gwr 	c_tbl = pmap_find_c_tmgr(pte);
   1350  1.1  gwr 	c_tbl->ct_parent->bt_parent->at_parent->pm_stats.resident_count--;
   1351  1.1  gwr 	if (--c_tbl->ct_ecnt == 0)
   1352  1.1  gwr 		free_c_table_novalid(c_tbl);
   1353  1.1  gwr }
   1354  1.1  gwr 
   1355  1.1  gwr /* pmap_stroll			INTERNAL
   1356  1.1  gwr  **
   1357  1.1  gwr  * Retrieve the addresses of all table managers involved in the mapping of
   1358  1.1  gwr  * the given virtual address.  If the table walk completed sucessfully,
   1359  1.1  gwr  * return TRUE.  If it was only partial sucessful, return FALSE.
   1360  1.1  gwr  * The table walk performed by this function is important to many other
   1361  1.1  gwr  * functions in this module.
   1362  1.1  gwr  */
   1363  1.1  gwr boolean_t
   1364  1.1  gwr pmap_stroll(pmap, va, a_tbl, b_tbl, c_tbl, pte, a_idx, b_idx, pte_idx)
   1365  1.1  gwr 	pmap_t pmap;
   1366  1.1  gwr 	vm_offset_t va;
   1367  1.1  gwr 	a_tmgr_t **a_tbl;
   1368  1.1  gwr 	b_tmgr_t **b_tbl;
   1369  1.1  gwr 	c_tmgr_t **c_tbl;
   1370  1.1  gwr 	mmu_short_pte_t **pte;
   1371  1.1  gwr 	int *a_idx, *b_idx, *pte_idx;
   1372  1.1  gwr {
   1373  1.1  gwr 	mmu_long_dte_t *a_dte;   /* A: long descriptor table          */
   1374  1.1  gwr 	mmu_short_dte_t *b_dte;  /* B: short descriptor table         */
   1375  1.1  gwr 
   1376  1.1  gwr 	if (pmap == pmap_kernel())
   1377  1.1  gwr 		return FALSE;
   1378  1.1  gwr 
   1379  1.1  gwr 	/* Does the given pmap have an A table? */
   1380  1.1  gwr 	*a_tbl = pmap->pm_a_tbl;
   1381  1.1  gwr 	if (*a_tbl == NULL)
   1382  1.1  gwr 		return FALSE; /* No.  Return unknown. */
   1383  1.1  gwr 	/* Does the A table have a valid B table
   1384  1.1  gwr 	 * under the corresponding table entry?
   1385  1.1  gwr 	 */
   1386  1.1  gwr 	*a_idx = MMU_TIA(va);
   1387  1.1  gwr 	a_dte = &((*a_tbl)->at_dtbl[*a_idx]);
   1388  1.1  gwr 	if (!MMU_VALID_DT(*a_dte))
   1389  1.1  gwr 		return FALSE; /* No. Return unknown. */
   1390  1.1  gwr 	/* Yes. Extract B table from the A table. */
   1391  1.1  gwr 	*b_tbl = pmap_find_b_tmgr(
   1392  1.1  gwr 		  (mmu_short_dte_t *) mmu_ptov(
   1393  1.1  gwr 		    MMU_DTE_PA(*a_dte)
   1394  1.1  gwr 		  )
   1395  1.1  gwr 		);
   1396  1.1  gwr 	/* Does the B table have a valid C table
   1397  1.1  gwr 	 * under the corresponding table entry?
   1398  1.1  gwr 	 */
   1399  1.1  gwr 	*b_idx = MMU_TIB(va);
   1400  1.1  gwr 	b_dte = &((*b_tbl)->bt_dtbl[*b_idx]);
   1401  1.1  gwr 	if (!MMU_VALID_DT(*b_dte))
   1402  1.1  gwr 		return FALSE; /* No. Return unknown. */
   1403  1.1  gwr 	/* Yes. Extract C table from the B table. */
   1404  1.1  gwr 	*c_tbl = pmap_find_c_tmgr(
   1405  1.1  gwr 		  (mmu_short_pte_t *) mmu_ptov(
   1406  1.1  gwr 		    MMU_DTE_PA(*b_dte)
   1407  1.1  gwr 		  )
   1408  1.1  gwr 		);
   1409  1.1  gwr 	*pte_idx = MMU_TIC(va);
   1410  1.1  gwr 	*pte = &((*c_tbl)->ct_dtbl[*pte_idx]);
   1411  1.1  gwr 
   1412  1.1  gwr 	return	TRUE;
   1413  1.1  gwr }
   1414  1.1  gwr 
   1415  1.1  gwr /* pmap_enter			INTERFACE
   1416  1.1  gwr  **
   1417  1.1  gwr  * Called by the kernel to map a virtual address
   1418  1.1  gwr  * to a physical address in the given process map.
   1419  1.1  gwr  *
   1420  1.1  gwr  * Note: this function should apply an exclusive lock
   1421  1.1  gwr  * on the pmap system for its duration.  (it certainly
   1422  1.1  gwr  * would save my hair!!)
   1423  1.1  gwr  */
   1424  1.1  gwr void
   1425  1.1  gwr pmap_enter(pmap, va, pa, prot, wired)
   1426  1.1  gwr 	pmap_t	pmap;
   1427  1.1  gwr 	vm_offset_t va;
   1428  1.1  gwr 	vm_offset_t pa;
   1429  1.1  gwr 	vm_prot_t prot;
   1430  1.1  gwr 	boolean_t wired;
   1431  1.1  gwr {
   1432  1.1  gwr 	u_int a_idx, b_idx, pte_idx; /* table indexes (fix grammar) */
   1433  1.1  gwr 	a_tmgr_t *a_tbl;         /* A: long descriptor table manager  */
   1434  1.1  gwr 	b_tmgr_t *b_tbl;         /* B: short descriptor table manager */
   1435  1.1  gwr 	c_tmgr_t *c_tbl;         /* C: short page table manager       */
   1436  1.1  gwr 	mmu_long_dte_t *a_dte;   /* A: long descriptor table          */
   1437  1.1  gwr 	mmu_short_dte_t *b_dte;  /* B: short descriptor table         */
   1438  1.1  gwr 	mmu_short_pte_t *c_pte;  /* C: short page descriptor table    */
   1439  1.1  gwr 	pv_t      *pv;           /* pv list head                      */
   1440  1.1  gwr 	pv_elem_t *pve;          /* pv element                        */
   1441  1.1  gwr 	enum {NONE, NEWA, NEWB, NEWC} llevel; /* used at end   */
   1442  1.1  gwr 
   1443  1.1  gwr 	if (pmap == NULL)
   1444  1.1  gwr 		return;
   1445  1.1  gwr 	if (pmap == pmap_kernel()) {
   1446  1.1  gwr 		pmap_enter_kernel(va, pa, prot);
   1447  1.1  gwr 		return;
   1448  1.1  gwr 	}
   1449  1.1  gwr 
   1450  1.1  gwr 	/* For user mappings we walk along the MMU tables of the given
   1451  1.1  gwr 	 * pmap, reaching a PTE which describes the virtual page being
   1452  1.1  gwr 	 * mapped or changed.  If any level of the walk ends in an invalid
   1453  1.1  gwr 	 * entry, a table must be allocated and the entry must be updated
   1454  1.1  gwr 	 * to point to it.
   1455  1.1  gwr 	 * There is a bit of confusion as to whether this code must be
   1456  1.1  gwr 	 * re-entrant.  For now we will assume it is.  To support
   1457  1.1  gwr 	 * re-entrancy we must unlink tables from the table pool before
   1458  1.1  gwr 	 * we assume we may use them.  Tables are re-linked into the pool
   1459  1.1  gwr 	 * when we are finished with them at the end of the function.
   1460  1.1  gwr 	 * But I don't feel like doing that until we have proof that this
   1461  1.1  gwr 	 * needs to be re-entrant.
   1462  1.1  gwr 	 * 'llevel' records which tables need to be relinked.
   1463  1.1  gwr 	 */
   1464  1.1  gwr 	llevel = NONE;
   1465  1.1  gwr 
   1466  1.1  gwr 	/* Step 1 - Retrieve the A table from the pmap.  If it is the default
   1467  1.1  gwr 	 * A table (commonly known as the 'proc0' A table), allocate a new one.
   1468  1.1  gwr 	 */
   1469  1.1  gwr 
   1470  1.1  gwr 	a_tbl = pmap->pm_a_tbl;
   1471  1.1  gwr 	if (a_tbl == proc0Atmgr) {
   1472  1.1  gwr 		pmap->pm_a_tbl = a_tbl = get_a_table();
   1473  1.1  gwr 		if (!wired)
   1474  1.1  gwr 			llevel = NEWA;
   1475  1.1  gwr 	} else {
   1476  1.1  gwr 		/* Use the A table already allocated for this pmap.
   1477  1.1  gwr 		 * Unlink it from the A table pool if necessary.
   1478  1.1  gwr 		 */
   1479  1.1  gwr 		if (wired && !a_tbl->at_wcnt)
   1480  1.1  gwr 			TAILQ_REMOVE(&a_pool, a_tbl, at_link);
   1481  1.1  gwr 	}
   1482  1.1  gwr 
   1483  1.1  gwr 	/* Step 2 - Walk into the B table.  If there is no valid B table,
   1484  1.1  gwr 	 * allocate one.
   1485  1.1  gwr 	 */
   1486  1.1  gwr 
   1487  1.1  gwr 	a_idx = MMU_TIA(va);            /* Calculate the TIA of the VA. */
   1488  1.1  gwr 	a_dte = &a_tbl->at_dtbl[a_idx]; /* Retrieve descriptor from table */
   1489  1.1  gwr 	if (MMU_VALID_DT(*a_dte)) {     /* Is the descriptor valid? */
   1490  1.1  gwr 		/* Yes, it points to a valid B table.  Use it. */
   1491  1.1  gwr 		/*************************************
   1492  1.1  gwr 		 *               a_idx               *
   1493  1.1  gwr 		 *                 v                 *
   1494  1.1  gwr 		 * a_tbl -> +-+-+-+-+-+-+-+-+-+-+-+- *
   1495  1.1  gwr 		 *          | | | | | | | | | | | |  *
   1496  1.1  gwr 		 *          +-+-+-+-+-+-+-+-+-+-+-+- *
   1497  1.1  gwr 		 *                 |                 *
   1498  1.1  gwr 		 *                 \- b_tbl -> +-+-  *
   1499  1.1  gwr 		 *                             | |   *
   1500  1.1  gwr 		 *                             +-+-  *
   1501  1.1  gwr 		 *************************************/
   1502  1.1  gwr 		b_dte = (mmu_short_dte_t *) mmu_ptov(a_dte->addr.raw);
   1503  1.1  gwr 		b_tbl = mmuB2tmgr(b_dte);
   1504  1.1  gwr 		if (wired && !b_tbl->bt_wcnt) {
   1505  1.1  gwr 			/* If mapping is wired and table is not */
   1506  1.1  gwr 			TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
   1507  1.1  gwr 			a_tbl->at_wcnt++; /* Update parent table's wired
   1508  1.1  gwr 			                   * entry count. */
   1509  1.1  gwr 		}
   1510  1.1  gwr 	} else {
   1511  1.1  gwr 		b_tbl = get_b_table(); /* No, need to allocate a new B table */
   1512  1.1  gwr 		/* Point the parent A table descriptor to this new B table. */
   1513  1.1  gwr 		a_dte->addr.raw = (unsigned long) mmu_vtop(b_tbl->bt_dtbl);
   1514  1.1  gwr 		a_dte->attr.attr_struct.dt = MMU_DT_SHORT;
   1515  1.1  gwr 		/* Create the necessary back references to the parent table */
   1516  1.1  gwr 		b_tbl->bt_parent = a_tbl;
   1517  1.1  gwr 		b_tbl->bt_pidx = a_idx;
   1518  1.1  gwr 		/* If this table is to be wired, make sure the parent A table
   1519  1.1  gwr 		 * wired count is updated to reflect that it has another wired
   1520  1.1  gwr 		 * entry.
   1521  1.1  gwr 		 */
   1522  1.1  gwr 		a_tbl->at_ecnt++; /* Update parent's valid entry count */
   1523  1.1  gwr 		if (wired)
   1524  1.1  gwr 			a_tbl->at_wcnt++;
   1525  1.1  gwr 		else if (llevel == NONE)
   1526  1.1  gwr 			llevel = NEWB;
   1527  1.1  gwr 	}
   1528  1.1  gwr 
   1529  1.1  gwr 	/* Step 3 - Walk into the C table, if there is no valid C table,
   1530  1.1  gwr 	 * allocate one.
   1531  1.1  gwr 	 */
   1532  1.1  gwr 
   1533  1.1  gwr 	b_idx = MMU_TIB(va);            /* Calculate the TIB of the VA */
   1534  1.1  gwr 	b_dte = &b_tbl->bt_dtbl[b_idx]; /* Retrieve descriptor from table */
   1535  1.1  gwr 	if (MMU_VALID_DT(*b_dte)) {     /* Is the descriptor valid? */
   1536  1.1  gwr 		/* Yes, it points to a valid C table.  Use it. */
   1537  1.1  gwr 		/**************************************
   1538  1.1  gwr 		 *               c_idx                *
   1539  1.1  gwr 		 * |                v                 *
   1540  1.1  gwr 		 * \- b_tbl -> +-+-+-+-+-+-+-+-+-+-+- *
   1541  1.1  gwr 		 *             | | | | | | | | | | |  *
   1542  1.1  gwr 		 *             +-+-+-+-+-+-+-+-+-+-+- *
   1543  1.1  gwr 		 *                  |                 *
   1544  1.1  gwr 		 *                  \- c_tbl -> +-+-- *
   1545  1.1  gwr 		 *                              | | | *
   1546  1.1  gwr 		 *                              +-+-- *
   1547  1.1  gwr 		 **************************************/
   1548  1.1  gwr 		c_pte = (mmu_short_pte_t *) MMU_PTE_PA(*b_dte);
   1549  1.1  gwr 		c_pte = (mmu_short_pte_t *) mmu_ptov(c_pte);
   1550  1.1  gwr 		c_tbl = mmuC2tmgr(c_pte);
   1551  1.1  gwr 		if (wired && !c_tbl->ct_wcnt) {
   1552  1.1  gwr 			/* If mapping is wired and table is not */
   1553  1.1  gwr 			TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
   1554  1.1  gwr 			b_tbl->bt_wcnt++;
   1555  1.1  gwr 		}
   1556  1.1  gwr 	} else {
   1557  1.1  gwr 		c_tbl = get_c_table(); /* No, need to allocate a new C table */
   1558  1.1  gwr 		/* Point the parent B table descriptor to this new C table. */
   1559  1.1  gwr 		b_dte->attr.raw = (unsigned long) mmu_vtop(c_tbl->ct_dtbl);
   1560  1.1  gwr 		b_dte->attr.attr_struct.dt = MMU_DT_SHORT;
   1561  1.1  gwr 		/* Create the necessary back references to the parent table */
   1562  1.1  gwr 		c_tbl->ct_parent = b_tbl;
   1563  1.1  gwr 		c_tbl->ct_pidx = b_idx;
   1564  1.1  gwr 		/* If this table is to be wired, make sure the parent B table
   1565  1.1  gwr 		 * wired count is updated to reflect that it has another wired
   1566  1.1  gwr 		 * entry.
   1567  1.1  gwr 		 */
   1568  1.1  gwr 		b_tbl->bt_ecnt++; /* Update parent's valid entry count */
   1569  1.1  gwr 		if (wired)
   1570  1.1  gwr 			b_tbl->bt_wcnt++;
   1571  1.1  gwr 		else if (llevel == NONE)
   1572  1.1  gwr 			llevel = NEWC;
   1573  1.1  gwr 	}
   1574  1.1  gwr 
   1575  1.1  gwr 	/* Step 4 - Deposit a page descriptor (PTE) into the appropriate
   1576  1.1  gwr 	 * slot of the C table, describing the PA to which the VA is mapped.
   1577  1.1  gwr 	 */
   1578  1.1  gwr 
   1579  1.1  gwr 	pte_idx = MMU_TIC(va);
   1580  1.1  gwr 	c_pte = &c_tbl->ct_dtbl[pte_idx];
   1581  1.1  gwr 	if (MMU_VALID_DT(*c_pte)) { /* Is the entry currently valid? */
   1582  1.1  gwr 		/* If the PTE is currently valid, then this function call
   1583  1.1  gwr 		 * is just a synonym for one (or more) of the following
   1584  1.1  gwr 		 * operations:
   1585  1.1  gwr 		 *     change protections on a page
   1586  1.1  gwr 		 *     change wiring status of a page
   1587  1.1  gwr 		 *     remove the mapping of a page
   1588  1.1  gwr 		 */
   1589  1.1  gwr 		/* Is the new address the same as the old? */
   1590  1.1  gwr 		if (MMU_PTE_PA(*c_pte) == pa) {
   1591  1.1  gwr 			/* Yes, do nothing. */
   1592  1.1  gwr 		} else {
   1593  1.1  gwr 			/* No, remove the old entry */
   1594  1.1  gwr 			pmap_remove_pte(c_pte);
   1595  1.1  gwr 		}
   1596  1.1  gwr 	} else {
   1597  1.1  gwr 		/* No, update the valid entry count in the C table */
   1598  1.1  gwr 		c_tbl->ct_ecnt++;
   1599  1.1  gwr 		/* and in pmap */
   1600  1.1  gwr 		pmap->pm_stats.resident_count++;
   1601  1.1  gwr         }
   1602  1.1  gwr 	/* Map the page. */
   1603  1.1  gwr 	c_pte->attr.raw = ((unsigned long) pa | MMU_DT_PAGE);
   1604  1.1  gwr 
   1605  1.1  gwr 	if (wired) /* Does the entry need to be wired? */ {
   1606  1.1  gwr 		c_pte->attr.raw |= MMU_SHORT_PTE_WIRED;
   1607  1.1  gwr 	}
   1608  1.1  gwr 
   1609  1.1  gwr         /* If the physical address being mapped is managed by the PV
   1610  1.1  gwr          * system then link the pte into the list of pages mapped to that
   1611  1.1  gwr          * address.
   1612  1.1  gwr          */
   1613  1.1  gwr         if (is_managed(pa)) {
   1614  1.1  gwr             pv = pa2pv(pa);
   1615  1.1  gwr             pve = pte2pve(c_pte);
   1616  1.1  gwr             LIST_INSERT_HEAD(&pv->pv_head, pve, pve_link);
   1617  1.1  gwr         }
   1618  1.1  gwr 
   1619  1.1  gwr 	/* Move any allocated tables back into the active pool. */
   1620  1.1  gwr 
   1621  1.1  gwr 	switch (llevel) {
   1622  1.1  gwr 		case NEWA:
   1623  1.1  gwr 			TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
   1624  1.1  gwr 			/* FALLTHROUGH */
   1625  1.1  gwr 		case NEWB:
   1626  1.1  gwr 			TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
   1627  1.1  gwr 			/* FALLTHROUGH */
   1628  1.1  gwr 		case NEWC:
   1629  1.1  gwr 			TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
   1630  1.1  gwr 			/* FALLTHROUGH */
   1631  1.1  gwr 		default:
   1632  1.1  gwr 			break;
   1633  1.1  gwr 	}
   1634  1.1  gwr }
   1635  1.1  gwr 
   1636  1.1  gwr /* pmap_enter_kernel			INTERNAL
   1637  1.1  gwr  **
   1638  1.1  gwr  * Map the given virtual address to the given physical address within the
   1639  1.1  gwr  * kernel address space.  This function exists because the kernel map does
   1640  1.1  gwr  * not do dynamic table allocation.  It consists of a contiguous array of ptes
   1641  1.1  gwr  * and can be edited directly without the need to walk through any tables.
   1642  1.1  gwr  *
   1643  1.1  gwr  * XXX: "Danger, Will Robinson!"
   1644  1.1  gwr  * Note that the kernel should never take a fault on any page
   1645  1.1  gwr  * between [ KERNBASE .. virtual_avail ] and this is checked in
   1646  1.1  gwr  * trap.c for kernel-mode MMU faults.  This means that mappings
   1647  1.1  gwr  * created in that range must be implicily wired. -gwr
   1648  1.1  gwr  */
   1649  1.1  gwr void
   1650  1.1  gwr pmap_enter_kernel(va, pa, prot)
   1651  1.1  gwr 	vm_offset_t va;
   1652  1.1  gwr 	vm_offset_t pa;
   1653  1.1  gwr 	vm_prot_t   prot;
   1654  1.1  gwr {
   1655  1.1  gwr 	boolean_t was_valid = FALSE;
   1656  1.1  gwr 	mmu_short_pte_t *pte;
   1657  1.1  gwr 
   1658  1.1  gwr 	/* XXX - This array is traditionally named "Sysmap" */
   1659  1.1  gwr 	pte = &kernCbase[(unsigned long) sun3x_btop(va - KERNBASE)];
   1660  1.1  gwr 	if (MMU_VALID_DT(*pte))
   1661  1.1  gwr 		was_valid = TRUE;
   1662  1.1  gwr 
   1663  1.1  gwr 	pte->attr.raw = (pa | MMU_DT_PAGE);
   1664  1.1  gwr 
   1665  1.1  gwr 	if (!(prot & VM_PROT_WRITE)) /* If access should be read-only */
   1666  1.1  gwr 		pte->attr.raw |= MMU_SHORT_PTE_WP;
   1667  1.1  gwr 	if (pa & PMAP_NC)
   1668  1.1  gwr 		pte->attr.raw |= MMU_SHORT_PTE_CI;
   1669  1.1  gwr 	if (was_valid) {
   1670  1.1  gwr 		/* mmu_flusha(FC_SUPERD, va); */
   1671  1.1  gwr 		/* mmu_flusha(); */
   1672  1.1  gwr 		TBIA();
   1673  1.1  gwr 	}
   1674  1.1  gwr 
   1675  1.1  gwr }
   1676  1.1  gwr 
   1677  1.1  gwr /* pmap_protect			INTERFACE
   1678  1.1  gwr  **
   1679  1.1  gwr  * Apply the given protection to the given virtual address within
   1680  1.1  gwr  * the given map.
   1681  1.1  gwr  *
   1682  1.1  gwr  * It is ok for the protection applied to be stronger than what is
   1683  1.1  gwr  * specified.  We use this to our advantage when the given map has no
   1684  1.1  gwr  * mapping for the virtual address.  By returning immediately when this
   1685  1.1  gwr  * is discovered, we are effectively applying a protection of VM_PROT_NONE,
   1686  1.1  gwr  * and therefore do not need to map the page just to apply a protection
   1687  1.1  gwr  * code.  Only pmap_enter() needs to create new mappings if they do not exist.
   1688  1.1  gwr  */
   1689  1.1  gwr void
   1690  1.1  gwr pmap_protect(pmap, va, pa, prot)
   1691  1.1  gwr 	pmap_t pmap;
   1692  1.1  gwr 	vm_offset_t va, pa;
   1693  1.1  gwr 	vm_prot_t prot;
   1694  1.1  gwr {
   1695  1.1  gwr 	int a_idx, b_idx, c_idx;
   1696  1.1  gwr 	a_tmgr_t *a_tbl;
   1697  1.1  gwr 	b_tmgr_t *b_tbl;
   1698  1.1  gwr 	c_tmgr_t *c_tbl;
   1699  1.1  gwr 	mmu_short_pte_t *pte;
   1700  1.1  gwr 
   1701  1.1  gwr 	if (pmap == NULL)
   1702  1.1  gwr 		return;
   1703  1.1  gwr 	if (pmap == pmap_kernel()) {
   1704  1.1  gwr 		pmap_protect_kernel(va, pa, prot);
   1705  1.1  gwr 		return;
   1706  1.1  gwr 	}
   1707  1.1  gwr 
   1708  1.1  gwr 	/* Retrieve the mapping from the given pmap.  If it does
   1709  1.1  gwr 	 * not exist then we need not do anything more.
   1710  1.1  gwr 	 */
   1711  1.1  gwr 	if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl, &pte,
   1712  1.1  gwr 		&a_idx, &b_idx, &c_idx) == FALSE) {
   1713  1.1  gwr 		return;
   1714  1.1  gwr 	}
   1715  1.1  gwr 
   1716  1.1  gwr 	switch (prot) {
   1717  1.1  gwr 		case VM_PROT_ALL:
   1718  1.1  gwr 			/* this should never happen in a sane system */
   1719  1.1  gwr 			break;
   1720  1.1  gwr 		case VM_PROT_READ:
   1721  1.1  gwr 		case VM_PROT_READ|VM_PROT_EXECUTE:
   1722  1.1  gwr 			/* make the mapping read-only */
   1723  1.1  gwr 			pte->attr.raw |= MMU_SHORT_PTE_WP;
   1724  1.1  gwr 			break;
   1725  1.1  gwr 		case VM_PROT_NONE:
   1726  1.1  gwr 			/* this is an alias for 'pmap_remove' */
   1727  1.1  gwr 			pmap_dereference_pte(pte);
   1728  1.1  gwr 			break;
   1729  1.1  gwr 		default:
   1730  1.1  gwr 			break;
   1731  1.1  gwr 	}
   1732  1.1  gwr }
   1733  1.1  gwr 
   1734  1.1  gwr /* pmap_protect_kernel			INTERNAL
   1735  1.1  gwr  **
   1736  1.1  gwr  * Apply the given protection code to a kernel address mapping.
   1737  1.1  gwr  */
   1738  1.1  gwr void
   1739  1.1  gwr pmap_protect_kernel(va, pa, prot)
   1740  1.1  gwr 	vm_offset_t va, pa;
   1741  1.1  gwr 	vm_prot_t prot;
   1742  1.1  gwr {
   1743  1.1  gwr 	mmu_short_pte_t *pte;
   1744  1.1  gwr 
   1745  1.1  gwr 	pte = &kernCbase[(unsigned long) sun3x_btop(va - KERNBASE)];
   1746  1.1  gwr 	if (MMU_VALID_DT(*pte)) {
   1747  1.1  gwr 		switch (prot) {
   1748  1.1  gwr 			case VM_PROT_ALL:
   1749  1.1  gwr 				break;
   1750  1.1  gwr 			case VM_PROT_READ:
   1751  1.1  gwr 			case VM_PROT_READ|VM_PROT_EXECUTE:
   1752  1.1  gwr 				pte->attr.raw |= MMU_SHORT_PTE_WP;
   1753  1.1  gwr 				break;
   1754  1.1  gwr 			case VM_PROT_NONE:
   1755  1.1  gwr 				/* this is an alias for 'pmap_remove_kernel' */
   1756  1.1  gwr 				pte->attr.raw = MMU_DT_INVALID;
   1757  1.1  gwr 				break;
   1758  1.1  gwr 			default:
   1759  1.1  gwr 				break;
   1760  1.1  gwr 		}
   1761  1.1  gwr 	}
   1762  1.1  gwr 	/* since this is the kernel, immediately flush any cached
   1763  1.1  gwr 	 * descriptors for this address.
   1764  1.1  gwr 	 */
   1765  1.1  gwr 	/* mmu_flush(FC_SUPERD, va); */
   1766  1.1  gwr 	TBIS(va);
   1767  1.1  gwr }
   1768  1.1  gwr 
   1769  1.1  gwr /* pmap_change_wiring			INTERFACE
   1770  1.1  gwr  **
   1771  1.1  gwr  * Changes the wiring of the specified page.
   1772  1.1  gwr  *
   1773  1.1  gwr  * This function is called from vm_fault.c to unwire
   1774  1.1  gwr  * a mapping.  It really should be called 'pmap_unwire'
   1775  1.1  gwr  * because it is never asked to do anything but remove
   1776  1.1  gwr  * wirings.
   1777  1.1  gwr  */
   1778  1.1  gwr void
   1779  1.1  gwr pmap_change_wiring(pmap, va, wire)
   1780  1.1  gwr 	pmap_t pmap;
   1781  1.1  gwr 	vm_offset_t va;
   1782  1.1  gwr 	boolean_t wire;
   1783  1.1  gwr {
   1784  1.1  gwr 	int a_idx, b_idx, c_idx;
   1785  1.1  gwr 	a_tmgr_t *a_tbl;
   1786  1.1  gwr 	b_tmgr_t *b_tbl;
   1787  1.1  gwr 	c_tmgr_t *c_tbl;
   1788  1.1  gwr 	mmu_short_pte_t *pte;
   1789  1.1  gwr 
   1790  1.1  gwr 	/* Kernel mappings always remain wired. */
   1791  1.1  gwr 	if (pmap == pmap_kernel())
   1792  1.1  gwr 		return;
   1793  1.1  gwr 
   1794  1.1  gwr #ifdef	PMAP_DEBUG
   1795  1.1  gwr 	if (wire == TRUE)
   1796  1.1  gwr 		panic("pmap_change_wiring: wire requested.");
   1797  1.1  gwr #endif
   1798  1.1  gwr 
   1799  1.1  gwr 	/* Walk through the tables.  If the walk terminates without
   1800  1.1  gwr 	 * a valid PTE then the address wasn't wired in the first place.
   1801  1.1  gwr 	 * Return immediately.
   1802  1.1  gwr 	 */
   1803  1.1  gwr 	if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl, &pte, &a_idx,
   1804  1.1  gwr 		&b_idx, &c_idx) == FALSE)
   1805  1.1  gwr 		return;
   1806  1.1  gwr 
   1807  1.1  gwr 
   1808  1.1  gwr 	/* Is the PTE wired?  If not, return. */
   1809  1.1  gwr 	if (!(pte->attr.raw & MMU_SHORT_PTE_WIRED))
   1810  1.1  gwr 		return;
   1811  1.1  gwr 
   1812  1.1  gwr 	/* Remove the wiring bit. */
   1813  1.1  gwr 	pte->attr.raw &= ~(MMU_SHORT_PTE_WIRED);
   1814  1.1  gwr 
   1815  1.1  gwr 	/* Decrement the wired entry count in the C table.
   1816  1.1  gwr 	 * If it reaches zero the following things happen:
   1817  1.1  gwr 	 * 1. The table no longer has any wired entries and is considered
   1818  1.1  gwr 	 *    unwired.
   1819  1.1  gwr 	 * 2. It is placed on the available queue.
   1820  1.1  gwr 	 * 3. The parent table's wired entry count is decremented.
   1821  1.1  gwr 	 * 4. If it reaches zero, this process repeats at step 1 and
   1822  1.1  gwr 	 *    stops at after reaching the A table.
   1823  1.1  gwr 	 */
   1824  1.1  gwr 	if (c_tbl->ct_wcnt-- == 0) {
   1825  1.1  gwr 		TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
   1826  1.1  gwr 		if (b_tbl->bt_wcnt-- == 0) {
   1827  1.1  gwr 			TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
   1828  1.1  gwr 			if (a_tbl->at_wcnt-- == 0) {
   1829  1.1  gwr 				TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
   1830  1.1  gwr 			}
   1831  1.1  gwr 		}
   1832  1.1  gwr 	}
   1833  1.1  gwr 
   1834  1.1  gwr 	pmap->pm_stats.wired_count--;
   1835  1.1  gwr }
   1836  1.1  gwr 
   1837  1.1  gwr /* pmap_pageable			INTERFACE
   1838  1.1  gwr  **
   1839  1.1  gwr  * Make the specified range of addresses within the given pmap,
   1840  1.1  gwr  * 'pageable' or 'not-pageable'.  A pageable page must not cause
   1841  1.1  gwr  * any faults when referenced.  A non-pageable page may.
   1842  1.1  gwr  *
   1843  1.1  gwr  * This routine is only advisory.  The VM system will call pmap_enter()
   1844  1.1  gwr  * to wire or unwire pages that are going to be made pageable before calling
   1845  1.1  gwr  * this function.  By the time this routine is called, everything that needs
   1846  1.1  gwr  * to be done has already been done.
   1847  1.1  gwr  */
   1848  1.1  gwr void
   1849  1.1  gwr pmap_pageable(pmap, start, end, pageable)
   1850  1.1  gwr 	pmap_t pmap;
   1851  1.1  gwr 	vm_offset_t start, end;
   1852  1.1  gwr 	boolean_t pageable;
   1853  1.1  gwr {
   1854  1.1  gwr 	/* not implemented. */
   1855  1.1  gwr }
   1856  1.1  gwr 
   1857  1.1  gwr /* pmap_copy				INTERFACE
   1858  1.1  gwr  **
   1859  1.1  gwr  * Copy the mappings of a range of addresses in one pmap, into
   1860  1.1  gwr  * the destination address of another.
   1861  1.1  gwr  *
   1862  1.1  gwr  * This routine is advisory.  Should we one day decide that MMU tables
   1863  1.1  gwr  * may be shared by more than one pmap, this function should be used to
   1864  1.1  gwr  * link them together.  Until that day however, we do nothing.
   1865  1.1  gwr  */
   1866  1.1  gwr void
   1867  1.1  gwr pmap_copy(pmap_a, pmap_b, dst, len, src)
   1868  1.1  gwr 	pmap_t pmap_a, pmap_b;
   1869  1.1  gwr 	vm_offset_t dst;
   1870  1.1  gwr 	vm_size_t   len;
   1871  1.1  gwr 	vm_offset_t src;
   1872  1.1  gwr {
   1873  1.1  gwr 	/* not implemented. */
   1874  1.1  gwr }
   1875  1.1  gwr 
   1876  1.1  gwr /* pmap_copy_page			INTERFACE
   1877  1.1  gwr  **
   1878  1.1  gwr  * Copy the contents of one physical page into another.
   1879  1.1  gwr  *
   1880  1.1  gwr  * This function makes use of two virtual pages allocated in sun3x_vm_init()
   1881  1.1  gwr  * (found in _startup.c) to map the two specified physical pages into the
   1882  1.1  gwr  * kernel address space.  It then uses bcopy() to copy one into the other.
   1883  1.1  gwr  */
   1884  1.1  gwr void
   1885  1.1  gwr pmap_copy_page(src, dst)
   1886  1.1  gwr 	vm_offset_t src, dst;
   1887  1.1  gwr {
   1888  1.1  gwr 	PMAP_LOCK();
   1889  1.1  gwr 	if (tmp_vpages_inuse)
   1890  1.1  gwr 		panic("pmap_copy_page: temporary vpages are in use.");
   1891  1.1  gwr 	tmp_vpages_inuse++;
   1892  1.1  gwr 
   1893  1.1  gwr 	pmap_enter_kernel(tmp_vpages[0], src, VM_PROT_READ);
   1894  1.1  gwr 	pmap_enter_kernel(tmp_vpages[1], dst, VM_PROT_READ|VM_PROT_WRITE);
   1895  1.1  gwr 	bcopy((char *) tmp_vpages[1], (char *) tmp_vpages[0], NBPG);
   1896  1.1  gwr 	/* xxx - there's no real need to unmap the mappings is there? */
   1897  1.1  gwr 
   1898  1.1  gwr 	tmp_vpages_inuse--;
   1899  1.1  gwr 	PMAP_UNLOCK();
   1900  1.1  gwr }
   1901  1.1  gwr 
   1902  1.1  gwr /* pmap_zero_page			INTERFACE
   1903  1.1  gwr  **
   1904  1.1  gwr  * Zero the contents of the specified physical page.
   1905  1.1  gwr  *
   1906  1.1  gwr  * Uses one of the virtual pages allocated in sun3x_vm_init() (_startup.c)
   1907  1.1  gwr  * to map the specified page into the kernel address space.  Then uses
   1908  1.1  gwr  * bzero() to zero out the page.
   1909  1.1  gwr  */
   1910  1.1  gwr void
   1911  1.1  gwr pmap_zero_page(pa)
   1912  1.1  gwr 	vm_offset_t pa;
   1913  1.1  gwr {
   1914  1.1  gwr 	PMAP_LOCK();
   1915  1.1  gwr 	if (tmp_vpages_inuse)
   1916  1.1  gwr 		panic("pmap_zero_page: temporary vpages are in use.");
   1917  1.1  gwr 	tmp_vpages_inuse++;
   1918  1.1  gwr 
   1919  1.1  gwr 	pmap_enter_kernel(tmp_vpages[0], pa, VM_PROT_READ|VM_PROT_WRITE);
   1920  1.1  gwr 	bzero((char *) tmp_vpages[0], NBPG);
   1921  1.1  gwr 	/* xxx - there's no real need to unmap the mapping is there? */
   1922  1.1  gwr 
   1923  1.1  gwr 	tmp_vpages_inuse--;
   1924  1.1  gwr 	PMAP_UNLOCK();
   1925  1.1  gwr }
   1926  1.1  gwr 
   1927  1.1  gwr /* pmap_collect			INTERFACE
   1928  1.1  gwr  **
   1929  1.1  gwr  * Called from the VM system to collect unused pages in the given
   1930  1.1  gwr  * pmap.
   1931  1.1  gwr  *
   1932  1.1  gwr  * No one implements it, so I'm not even sure how it is supposed to
   1933  1.1  gwr  * 'collect' anything anyways.  There's nothing to do but do what everyone
   1934  1.1  gwr  * else does..
   1935  1.1  gwr  */
   1936  1.1  gwr void
   1937  1.1  gwr pmap_collect(pmap)
   1938  1.1  gwr 	pmap_t pmap;
   1939  1.1  gwr {
   1940  1.1  gwr 	/* not implemented. */
   1941  1.1  gwr }
   1942  1.1  gwr 
   1943  1.1  gwr /* pmap_create			INTERFACE
   1944  1.1  gwr  **
   1945  1.1  gwr  * Create and return a pmap structure.
   1946  1.1  gwr  */
   1947  1.1  gwr pmap_t
   1948  1.1  gwr pmap_create(size)
   1949  1.1  gwr 	vm_size_t size;
   1950  1.1  gwr {
   1951  1.1  gwr 	pmap_t	pmap;
   1952  1.1  gwr 
   1953  1.1  gwr 	if (size)
   1954  1.1  gwr 		return NULL;
   1955  1.1  gwr 
   1956  1.1  gwr 	pmap = (pmap_t) malloc(sizeof(struct pmap), M_VMPMAP, M_WAITOK);
   1957  1.1  gwr 	pmap_pinit(pmap);
   1958  1.1  gwr 
   1959  1.1  gwr 	return pmap;
   1960  1.1  gwr }
   1961  1.1  gwr 
   1962  1.1  gwr /* pmap_pinit			INTERNAL
   1963  1.1  gwr  **
   1964  1.1  gwr  * Initialize a pmap structure.
   1965  1.1  gwr  */
   1966  1.1  gwr void
   1967  1.1  gwr pmap_pinit(pmap)
   1968  1.1  gwr 	pmap_t pmap;
   1969  1.1  gwr {
   1970  1.1  gwr 	bzero(pmap, sizeof(struct pmap));
   1971  1.1  gwr 	pmap->pm_a_tbl = proc0Atmgr;
   1972  1.1  gwr }
   1973  1.1  gwr 
   1974  1.1  gwr /* pmap_release				INTERFACE
   1975  1.1  gwr  **
   1976  1.1  gwr  * Release any resources held by the given pmap.
   1977  1.1  gwr  *
   1978  1.1  gwr  * This is the reverse analog to pmap_pinit.  It does not
   1979  1.1  gwr  * necessarily mean for the pmap structure to be deallocated,
   1980  1.1  gwr  * as in pmap_destroy.
   1981  1.1  gwr  */
   1982  1.1  gwr void
   1983  1.1  gwr pmap_release(pmap)
   1984  1.1  gwr 	pmap_t pmap;
   1985  1.1  gwr {
   1986  1.1  gwr 	/* As long as the pmap contains no mappings,
   1987  1.1  gwr 	 * which always should be the case whenever
   1988  1.1  gwr 	 * this function is called, there really should
   1989  1.1  gwr 	 * be nothing to do.
   1990  1.1  gwr 	 */
   1991  1.1  gwr #ifdef	PMAP_DEBUG
   1992  1.1  gwr 	if (pmap == NULL)
   1993  1.1  gwr 		return;
   1994  1.1  gwr 	if (pmap == pmap_kernel())
   1995  1.1  gwr 		panic("pmap_release: kernel pmap release requested.");
   1996  1.1  gwr 	if (pmap->pm_a_tbl != proc0Atmgr)
   1997  1.1  gwr 		panic("pmap_release: pmap not empty.");
   1998  1.1  gwr #endif
   1999  1.1  gwr }
   2000  1.1  gwr 
   2001  1.1  gwr /* pmap_reference			INTERFACE
   2002  1.1  gwr  **
   2003  1.1  gwr  * Increment the reference count of a pmap.
   2004  1.1  gwr  */
   2005  1.1  gwr void
   2006  1.1  gwr pmap_reference(pmap)
   2007  1.1  gwr 	pmap_t pmap;
   2008  1.1  gwr {
   2009  1.1  gwr 	if (pmap == NULL)
   2010  1.1  gwr 		return;
   2011  1.1  gwr 
   2012  1.1  gwr 	/* pmap_lock(pmap); */
   2013  1.1  gwr 	pmap->pm_refcount++;
   2014  1.1  gwr 	/* pmap_unlock(pmap); */
   2015  1.1  gwr }
   2016  1.1  gwr 
   2017  1.1  gwr /* pmap_dereference			INTERNAL
   2018  1.1  gwr  **
   2019  1.1  gwr  * Decrease the reference count on the given pmap
   2020  1.1  gwr  * by one and return the current count.
   2021  1.1  gwr  */
   2022  1.1  gwr int
   2023  1.1  gwr pmap_dereference(pmap)
   2024  1.1  gwr 	pmap_t pmap;
   2025  1.1  gwr {
   2026  1.1  gwr 	int rtn;
   2027  1.1  gwr 
   2028  1.1  gwr 	if (pmap == NULL)
   2029  1.1  gwr 		return 0;
   2030  1.1  gwr 
   2031  1.1  gwr 	/* pmap_lock(pmap); */
   2032  1.1  gwr 	rtn = --pmap->pm_refcount;
   2033  1.1  gwr 	/* pmap_unlock(pmap); */
   2034  1.1  gwr 
   2035  1.1  gwr 	return rtn;
   2036  1.1  gwr }
   2037  1.1  gwr 
   2038  1.1  gwr /* pmap_destroy			INTERFACE
   2039  1.1  gwr  **
   2040  1.1  gwr  * Decrement a pmap's reference count and delete
   2041  1.1  gwr  * the pmap if it becomes zero.  Will be called
   2042  1.1  gwr  * only after all mappings have been removed.
   2043  1.1  gwr  */
   2044  1.1  gwr void
   2045  1.1  gwr pmap_destroy(pmap)
   2046  1.1  gwr 	pmap_t pmap;
   2047  1.1  gwr {
   2048  1.1  gwr 	if (pmap == NULL)
   2049  1.1  gwr 		return;
   2050  1.1  gwr 	if (pmap == &kernel_pmap)
   2051  1.1  gwr 		panic("pmap_destroy: kernel_pmap!");
   2052  1.1  gwr 	if (pmap_dereference(pmap) == 0) {
   2053  1.1  gwr 		pmap_release(pmap);
   2054  1.1  gwr 		free(pmap, M_VMPMAP);
   2055  1.1  gwr 	}
   2056  1.1  gwr }
   2057  1.1  gwr 
   2058  1.1  gwr /* pmap_is_referenced			INTERFACE
   2059  1.1  gwr  **
   2060  1.1  gwr  * Determine if the given physical page has been
   2061  1.1  gwr  * referenced (read from [or written to.])
   2062  1.1  gwr  */
   2063  1.1  gwr boolean_t
   2064  1.1  gwr pmap_is_referenced(pa)
   2065  1.1  gwr 	vm_offset_t pa;
   2066  1.1  gwr {
   2067  1.1  gwr 	pv_t      *pv;
   2068  1.1  gwr 	pv_elem_t *pve;
   2069  1.1  gwr 	struct mmu_short_pte_struct *pte;
   2070  1.1  gwr 
   2071  1.1  gwr 	if (!pv_initialized)
   2072  1.1  gwr 		return FALSE;
   2073  1.1  gwr 	if (!is_managed(pa))
   2074  1.1  gwr 		return FALSE;
   2075  1.1  gwr 
   2076  1.1  gwr 	pv = pa2pv(pa);
   2077  1.1  gwr 	/* Check the flags on the pv head.  If they are set,
   2078  1.1  gwr 	 * return immediately.  Otherwise a search must be done.
   2079  1.1  gwr          */
   2080  1.1  gwr 	if (pv->pv_flags & PV_FLAGS_USED)
   2081  1.1  gwr 		return TRUE;
   2082  1.1  gwr 	else
   2083  1.1  gwr 		/* Search through all pv elements pointing
   2084  1.1  gwr 		 * to this page and query their reference bits
   2085  1.1  gwr 		 */
   2086  1.1  gwr 		for (pve = pv->pv_head.lh_first;
   2087  1.1  gwr                      pve != NULL;
   2088  1.1  gwr                      pve = pve->pve_link.le_next) {
   2089  1.1  gwr 			pte = pve2pte(pve);
   2090  1.1  gwr 			if (MMU_PTE_USED(*pte))
   2091  1.1  gwr 				return TRUE;
   2092  1.1  gwr 		}
   2093  1.1  gwr 
   2094  1.1  gwr 	return FALSE;
   2095  1.1  gwr }
   2096  1.1  gwr 
   2097  1.1  gwr /* pmap_is_modified			INTERFACE
   2098  1.1  gwr  **
   2099  1.1  gwr  * Determine if the given physical page has been
   2100  1.1  gwr  * modified (written to.)
   2101  1.1  gwr  */
   2102  1.1  gwr boolean_t
   2103  1.1  gwr pmap_is_modified(pa)
   2104  1.1  gwr 	vm_offset_t pa;
   2105  1.1  gwr {
   2106  1.1  gwr 	pv_t      *pv;
   2107  1.1  gwr 	pv_elem_t *pve;
   2108  1.1  gwr 
   2109  1.1  gwr 	if (!pv_initialized)
   2110  1.1  gwr 		return FALSE;
   2111  1.1  gwr 	if (!is_managed(pa))
   2112  1.1  gwr 		return FALSE;
   2113  1.1  gwr 
   2114  1.1  gwr 	/* see comments in pmap_is_referenced() */
   2115  1.1  gwr 	pv = pa2pv(pa);
   2116  1.1  gwr 	if (pv->pv_flags & PV_FLAGS_MDFY)
   2117  1.1  gwr 		return TRUE;
   2118  1.1  gwr 	else
   2119  1.1  gwr 		for (pve = pv->pv_head.lh_first; pve != NULL;
   2120  1.1  gwr                      pve = pve->pve_link.le_next) {
   2121  1.1  gwr 			struct mmu_short_pte_struct *pte;
   2122  1.1  gwr 			pte = pve2pte(pve);
   2123  1.1  gwr 			if (MMU_PTE_MODIFIED(*pte))
   2124  1.1  gwr 				return TRUE;
   2125  1.1  gwr 		}
   2126  1.1  gwr 	return FALSE;
   2127  1.1  gwr }
   2128  1.1  gwr 
   2129  1.1  gwr /* pmap_page_protect			INTERFACE
   2130  1.1  gwr  **
   2131  1.1  gwr  * Applies the given protection to all mappings to the given
   2132  1.1  gwr  * physical page.
   2133  1.1  gwr  */
   2134  1.1  gwr void
   2135  1.1  gwr pmap_page_protect(pa, prot)
   2136  1.1  gwr 	vm_offset_t pa;
   2137  1.1  gwr 	vm_prot_t prot;
   2138  1.1  gwr {
   2139  1.1  gwr 	pv_t      *pv;
   2140  1.1  gwr 	pv_elem_t *pve;
   2141  1.1  gwr 	struct mmu_short_pte_struct *pte;
   2142  1.1  gwr 
   2143  1.1  gwr 	if (!is_managed(pa))
   2144  1.1  gwr 		return;
   2145  1.1  gwr 
   2146  1.1  gwr 	pv = pa2pv(pa);
   2147  1.1  gwr 	for (pve = pv->pv_head.lh_first; pve != NULL;
   2148  1.1  gwr 		pve = pve->pve_link.le_next) {
   2149  1.1  gwr 		pte = pve2pte(pve);
   2150  1.1  gwr 		switch (prot) {
   2151  1.1  gwr 			case VM_PROT_ALL:
   2152  1.1  gwr 				/* do nothing */
   2153  1.1  gwr 				break;
   2154  1.1  gwr 			case VM_PROT_READ:
   2155  1.1  gwr 			case VM_PROT_READ|VM_PROT_EXECUTE:
   2156  1.1  gwr 				pte->attr.raw |= MMU_SHORT_PTE_WP;
   2157  1.1  gwr 				break;
   2158  1.1  gwr 			case VM_PROT_NONE:
   2159  1.1  gwr 				pmap_dereference_pte(pte);
   2160  1.1  gwr 				break;
   2161  1.1  gwr 			default:
   2162  1.1  gwr 				break;
   2163  1.1  gwr 		}
   2164  1.1  gwr 	}
   2165  1.1  gwr }
   2166  1.1  gwr 
   2167  1.1  gwr /* pmap_who_owns_pte			INTERNAL
   2168  1.1  gwr  **
   2169  1.1  gwr  * Called internally to find which pmap the given pte is
   2170  1.1  gwr  * a member of.
   2171  1.1  gwr  */
   2172  1.1  gwr pmap_t
   2173  1.1  gwr pmap_who_owns_pte(pte)
   2174  1.1  gwr 	mmu_short_pte_t *pte;
   2175  1.1  gwr {
   2176  1.1  gwr 	c_tmgr_t *c_tbl;
   2177  1.1  gwr 
   2178  1.1  gwr 	c_tbl = pmap_find_c_tmgr(pte);
   2179  1.1  gwr 
   2180  1.1  gwr 	return c_tbl->ct_parent->bt_parent->at_parent;
   2181  1.1  gwr }
   2182  1.1  gwr 
   2183  1.1  gwr /* pmap_find_va			INTERNAL_X
   2184  1.1  gwr  **
   2185  1.1  gwr  * Called internally to find the virtual address that the
   2186  1.1  gwr  * given pte maps.
   2187  1.1  gwr  *
   2188  1.1  gwr  * Note: I don't know if this function will ever be used, but I've
   2189  1.1  gwr  * implemented it just in case.
   2190  1.1  gwr  */
   2191  1.1  gwr vm_offset_t
   2192  1.1  gwr pmap_find_va(pte)
   2193  1.1  gwr 	mmu_short_pte_t *pte;
   2194  1.1  gwr {
   2195  1.1  gwr 	a_tmgr_t    *a_tbl;
   2196  1.1  gwr 	b_tmgr_t    *b_tbl;
   2197  1.1  gwr 	c_tmgr_t    *c_tbl;
   2198  1.1  gwr 	vm_offset_t     va = 0;
   2199  1.1  gwr 
   2200  1.1  gwr 	/* Find the virtual address by decoding table indexes.
   2201  1.1  gwr 	 * Each successive decode will reveal the address from
   2202  1.1  gwr 	 * least to most significant bit fashion.
   2203  1.1  gwr 	 *
   2204  1.1  gwr 	 * 31                              0
   2205  1.1  gwr          * +-------------------------------+
   2206  1.1  gwr 	 * |AAAAAAABBBBBBCCCCCCxxxxxxxxxxxx|
   2207  1.1  gwr 	 * +-------------------------------+
   2208  1.1  gwr 	 *
   2209  1.1  gwr 	 * Start with the 'C' bits.
   2210  1.1  gwr 	 */
   2211  1.1  gwr 	va |= (pmap_find_tic(pte) << MMU_TIC_SHIFT);
   2212  1.1  gwr 	c_tbl = pmap_find_c_tmgr(pte);
   2213  1.1  gwr 	b_tbl = c_tbl->ct_parent;
   2214  1.1  gwr 
   2215  1.1  gwr 	/* Add the 'B' bits. */
   2216  1.1  gwr 	va |= (c_tbl->ct_pidx << MMU_TIB_SHIFT);
   2217  1.1  gwr 	a_tbl = b_tbl->bt_parent;
   2218  1.1  gwr 
   2219  1.1  gwr 	/* Add the 'A' bits. */
   2220  1.1  gwr 	va |= (b_tbl->bt_pidx << MMU_TIA_SHIFT);
   2221  1.1  gwr 
   2222  1.1  gwr 	return va;
   2223  1.1  gwr }
   2224  1.1  gwr 
   2225  1.1  gwr /**** These functions should be removed.  Structures have changed, making ****
   2226  1.1  gwr  **** them uneccessary.                                                   ****/
   2227  1.1  gwr 
   2228  1.1  gwr /* pmap_find_tic			INTERNAL
   2229  1.1  gwr  **
   2230  1.1  gwr  * Given the address of a pte, find the TIC (level 'C' table index) for
   2231  1.1  gwr  * the pte within its C table.
   2232  1.1  gwr  */
   2233  1.1  gwr char
   2234  1.1  gwr pmap_find_tic(pte)
   2235  1.1  gwr 	mmu_short_pte_t *pte;
   2236  1.1  gwr {
   2237  1.1  gwr 	return ((mmuCbase - pte) % MMU_C_TBL_SIZE);
   2238  1.1  gwr }
   2239  1.1  gwr 
   2240  1.1  gwr /* pmap_find_tib			INTERNAL
   2241  1.1  gwr  **
   2242  1.1  gwr  * Given the address of dte known to belong to a B table, find the TIB
   2243  1.1  gwr  * (level 'B' table index) for the dte within its table.
   2244  1.1  gwr  */
   2245  1.1  gwr char
   2246  1.1  gwr pmap_find_tib(dte)
   2247  1.1  gwr 	mmu_short_dte_t *dte;
   2248  1.1  gwr {
   2249  1.1  gwr 	return ((mmuBbase - dte) % MMU_B_TBL_SIZE);
   2250  1.1  gwr }
   2251  1.1  gwr 
   2252  1.1  gwr /* pmap_find_tia			INTERNAL
   2253  1.1  gwr  **
   2254  1.1  gwr  * Given the address of a dte known to belong to an A table, find the
   2255  1.1  gwr  * TIA (level 'C' table index) for the dte withing its table.
   2256  1.1  gwr  */
   2257  1.1  gwr char
   2258  1.1  gwr pmap_find_tia(dte)
   2259  1.1  gwr 	mmu_long_dte_t *dte;
   2260  1.1  gwr {
   2261  1.1  gwr 	return ((mmuAbase - dte) % MMU_A_TBL_SIZE);
   2262  1.1  gwr }
   2263  1.1  gwr 
   2264  1.1  gwr /**** This one should stay ****/
   2265  1.1  gwr 
   2266  1.1  gwr /* pmap_find_c_tmgr			INTERNAL
   2267  1.1  gwr  **
   2268  1.1  gwr  * Given a pte known to belong to a C table, return the address of that
   2269  1.1  gwr  * table's management structure.
   2270  1.1  gwr  */
   2271  1.1  gwr c_tmgr_t *
   2272  1.1  gwr pmap_find_c_tmgr(pte)
   2273  1.1  gwr 	mmu_short_pte_t *pte;
   2274  1.1  gwr {
   2275  1.1  gwr 	return &Ctmgrbase[
   2276  1.1  gwr 		((mmuCbase - pte) / sizeof(*pte) / MMU_C_TBL_SIZE)
   2277  1.1  gwr 		];
   2278  1.1  gwr }
   2279  1.1  gwr 
   2280  1.1  gwr /* pmap_find_b_tmgr			INTERNAL
   2281  1.1  gwr  **
   2282  1.1  gwr  * Given a dte known to belong to a B table, return the address of that
   2283  1.1  gwr  * table's management structure.
   2284  1.1  gwr  */
   2285  1.1  gwr b_tmgr_t *
   2286  1.1  gwr pmap_find_b_tmgr(dte)
   2287  1.1  gwr 	mmu_short_dte_t *dte;
   2288  1.1  gwr {
   2289  1.1  gwr 	return &Btmgrbase[
   2290  1.1  gwr 		((mmuBbase - dte) / sizeof(*dte) / MMU_B_TBL_SIZE)
   2291  1.1  gwr 		];
   2292  1.1  gwr }
   2293  1.1  gwr 
   2294  1.1  gwr /* pmap_find_a_tmgr			INTERNAL
   2295  1.1  gwr  **
   2296  1.1  gwr  * Given a dte known to belong to an A table, return the address of that
   2297  1.1  gwr  * table's management structure.
   2298  1.1  gwr  */
   2299  1.1  gwr a_tmgr_t *
   2300  1.1  gwr pmap_find_a_tmgr(dte)
   2301  1.1  gwr 	mmu_long_dte_t *dte;
   2302  1.1  gwr {
   2303  1.1  gwr 	return &Atmgrbase[
   2304  1.1  gwr 		((mmuAbase - dte) / sizeof(*dte) / MMU_A_TBL_SIZE)
   2305  1.1  gwr 		];
   2306  1.1  gwr }
   2307  1.1  gwr 
   2308  1.1  gwr /**** End of functions that should be removed.                          ****
   2309  1.1  gwr  ****                                                                   ****/
   2310  1.1  gwr 
   2311  1.1  gwr /* pmap_clear_modify			INTERFACE
   2312  1.1  gwr  **
   2313  1.1  gwr  * Clear the modification bit on the page at the specified
   2314  1.1  gwr  * physical address.
   2315  1.1  gwr  *
   2316  1.1  gwr  */
   2317  1.1  gwr void
   2318  1.1  gwr pmap_clear_modify(pa)
   2319  1.1  gwr 	vm_offset_t pa;
   2320  1.1  gwr {
   2321  1.1  gwr 	pmap_clear_pv(pa, PV_FLAGS_MDFY);
   2322  1.1  gwr }
   2323  1.1  gwr 
   2324  1.1  gwr /* pmap_clear_reference			INTERFACE
   2325  1.1  gwr  **
   2326  1.1  gwr  * Clear the referenced bit on the page at the specified
   2327  1.1  gwr  * physical address.
   2328  1.1  gwr  */
   2329  1.1  gwr void
   2330  1.1  gwr pmap_clear_reference(pa)
   2331  1.1  gwr 	vm_offset_t pa;
   2332  1.1  gwr {
   2333  1.1  gwr 	pmap_clear_pv(pa, PV_FLAGS_USED);
   2334  1.1  gwr }
   2335  1.1  gwr 
   2336  1.1  gwr /* pmap_clear_pv			INTERNAL
   2337  1.1  gwr  **
   2338  1.1  gwr  * Clears the specified flag from the specified physical address.
   2339  1.1  gwr  * (Used by pmap_clear_modify() and pmap_clear_reference().)
   2340  1.1  gwr  *
   2341  1.1  gwr  * Flag is one of:
   2342  1.1  gwr  *   PV_FLAGS_MDFY - Page modified bit.
   2343  1.1  gwr  *   PV_FLAGS_USED - Page used (referenced) bit.
   2344  1.1  gwr  *
   2345  1.1  gwr  * This routine must not only clear the flag on the pv list
   2346  1.1  gwr  * head.  It must also clear the bit on every pte in the pv
   2347  1.1  gwr  * list associated with the address.
   2348  1.1  gwr  */
   2349  1.1  gwr void
   2350  1.1  gwr pmap_clear_pv(pa, flag)
   2351  1.1  gwr 	vm_offset_t pa;
   2352  1.1  gwr 	int flag;
   2353  1.1  gwr {
   2354  1.1  gwr 	pv_t      *pv;
   2355  1.1  gwr 	pv_elem_t *pve;
   2356  1.1  gwr 	mmu_short_pte_t *pte;
   2357  1.1  gwr 
   2358  1.1  gwr 	pv = pa2pv(pa);
   2359  1.1  gwr 	pv->pv_flags &= ~(flag);
   2360  1.1  gwr 	for (pve = pv->pv_head.lh_first; pve != NULL;
   2361  1.1  gwr              pve = pve->pve_link.le_next) {
   2362  1.1  gwr 		pte = pve2pte(pve);
   2363  1.1  gwr 		pte->attr.raw &= ~(flag);
   2364  1.1  gwr 	}
   2365  1.1  gwr }
   2366  1.1  gwr 
   2367  1.1  gwr /* pmap_extract			INTERFACE
   2368  1.1  gwr  **
   2369  1.1  gwr  * Return the physical address mapped by the virtual address
   2370  1.1  gwr  * in the specified pmap or 0 if it is not known.
   2371  1.1  gwr  *
   2372  1.1  gwr  * Note: this function should also apply an exclusive lock
   2373  1.1  gwr  * on the pmap system during its duration.
   2374  1.1  gwr  */
   2375  1.1  gwr vm_offset_t
   2376  1.1  gwr pmap_extract(pmap, va)
   2377  1.1  gwr 	pmap_t      pmap;
   2378  1.1  gwr 	vm_offset_t va;
   2379  1.1  gwr {
   2380  1.1  gwr 	int a_idx, b_idx, pte_idx;
   2381  1.1  gwr 	a_tmgr_t	*a_tbl;
   2382  1.1  gwr 	b_tmgr_t	*b_tbl;
   2383  1.1  gwr 	c_tmgr_t	*c_tbl;
   2384  1.1  gwr 	mmu_short_pte_t	*c_pte;
   2385  1.1  gwr 
   2386  1.1  gwr 	if (pmap == pmap_kernel())
   2387  1.1  gwr 		return pmap_extract_kernel(va);
   2388  1.1  gwr 	if (pmap == NULL)
   2389  1.1  gwr 		return 0;
   2390  1.1  gwr 
   2391  1.1  gwr 	if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl,
   2392  1.1  gwr 		&c_pte, &a_idx, &b_idx, &pte_idx) == FALSE);
   2393  1.1  gwr 		return 0;
   2394  1.1  gwr 
   2395  1.1  gwr 	if (MMU_VALID_DT(*c_pte))
   2396  1.1  gwr 		return MMU_PTE_PA(*c_pte);
   2397  1.1  gwr 	else
   2398  1.1  gwr 		return 0;
   2399  1.1  gwr }
   2400  1.1  gwr 
   2401  1.1  gwr /* pmap_extract_kernel		INTERNAL
   2402  1.1  gwr  **
   2403  1.1  gwr  * Extract a traslation from the kernel address space.
   2404  1.1  gwr  */
   2405  1.1  gwr vm_offset_t
   2406  1.1  gwr pmap_extract_kernel(va)
   2407  1.1  gwr 	vm_offset_t va;
   2408  1.1  gwr {
   2409  1.1  gwr 	mmu_short_pte_t *pte;
   2410  1.1  gwr 
   2411  1.1  gwr 	pte = &kernCbase[(unsigned long) sun3x_btop(va - KERNBASE)];
   2412  1.1  gwr 	return MMU_PTE_PA(*pte);
   2413  1.1  gwr }
   2414  1.1  gwr 
   2415  1.1  gwr /* pmap_remove_kernel		INTERNAL
   2416  1.1  gwr  **
   2417  1.1  gwr  * Remove the mapping of a range of virtual addresses from the kernel map.
   2418  1.1  gwr  */
   2419  1.1  gwr void
   2420  1.1  gwr pmap_remove_kernel(start, end)
   2421  1.1  gwr 	vm_offset_t start;
   2422  1.1  gwr 	vm_offset_t end;
   2423  1.1  gwr {
   2424  1.1  gwr 	start -= KERNBASE;
   2425  1.1  gwr 	end   -= KERNBASE;
   2426  1.1  gwr 	start = sun3x_round_page(start); /* round down */
   2427  1.1  gwr 	start = sun3x_btop(start);
   2428  1.1  gwr 	end   += MMU_PAGE_SIZE - 1;    /* next round operation will be up */
   2429  1.1  gwr 	end   = sun3x_round_page(end); /* round */
   2430  1.1  gwr 	end   = sun3x_btop(end);
   2431  1.1  gwr 
   2432  1.1  gwr 	while (start < end)
   2433  1.1  gwr 		kernCbase[start++].attr.raw = MMU_DT_INVALID;
   2434  1.1  gwr }
   2435  1.1  gwr 
   2436  1.1  gwr /* pmap_remove			INTERFACE
   2437  1.1  gwr  **
   2438  1.1  gwr  * Remove the mapping of a range of virtual addresses from the given pmap.
   2439  1.1  gwr  */
   2440  1.1  gwr void
   2441  1.1  gwr pmap_remove(pmap, start, end)
   2442  1.1  gwr 	pmap_t pmap;
   2443  1.1  gwr 	vm_offset_t start;
   2444  1.1  gwr 	vm_offset_t end;
   2445  1.1  gwr {
   2446  1.1  gwr 	if (pmap == pmap_kernel()) {
   2447  1.1  gwr 		pmap_remove_kernel(start, end);
   2448  1.1  gwr 		return;
   2449  1.1  gwr 	}
   2450  1.1  gwr 	pmap_remove_a(pmap->pm_a_tbl, start, end);
   2451  1.1  gwr 
   2452  1.1  gwr 	/* If we just modified the current address space,
   2453  1.1  gwr 	 * make sure to flush the MMU cache.
   2454  1.1  gwr 	 */
   2455  1.1  gwr 	if (curatbl == pmap->pm_a_tbl) {
   2456  1.1  gwr 		/* mmu_flusha(); */
   2457  1.1  gwr 		TBIA();
   2458  1.1  gwr 	}
   2459  1.1  gwr }
   2460  1.1  gwr 
   2461  1.1  gwr /* pmap_remove_a			INTERNAL
   2462  1.1  gwr  **
   2463  1.1  gwr  * This is function number one in a set of three that removes a range
   2464  1.1  gwr  * of memory in the most efficient manner by removing the highest possible
   2465  1.1  gwr  * tables from the memory space.  This particular function attempts to remove
   2466  1.1  gwr  * as many B tables as it can, delegating the remaining fragmented ranges to
   2467  1.1  gwr  * pmap_remove_b().
   2468  1.1  gwr  *
   2469  1.1  gwr  * It's ugly but will do for now.
   2470  1.1  gwr  */
   2471  1.1  gwr void
   2472  1.1  gwr pmap_remove_a(a_tbl, start, end)
   2473  1.1  gwr 	a_tmgr_t *a_tbl;
   2474  1.1  gwr 	vm_offset_t start;
   2475  1.1  gwr 	vm_offset_t end;
   2476  1.1  gwr {
   2477  1.1  gwr 	int idx;
   2478  1.1  gwr 	vm_offset_t nstart, nend, rstart;
   2479  1.1  gwr 	b_tmgr_t *b_tbl;
   2480  1.1  gwr 	mmu_long_dte_t  *a_dte;
   2481  1.1  gwr 	mmu_short_dte_t *b_dte;
   2482  1.1  gwr 
   2483  1.1  gwr 
   2484  1.1  gwr 	if (a_tbl == proc0Atmgr) /* If the pmap has no A table, return */
   2485  1.1  gwr 		return;
   2486  1.1  gwr 
   2487  1.1  gwr 	nstart = MMU_ROUND_UP_A(start);
   2488  1.1  gwr 	nend = MMU_ROUND_A(end);
   2489  1.1  gwr 
   2490  1.1  gwr 	if (start < nstart) {
   2491  1.1  gwr 		idx = MMU_TIA(start);
   2492  1.1  gwr 		a_dte = &a_tbl->at_dtbl[idx];
   2493  1.1  gwr 		if (MMU_VALID_DT(*a_dte)) {
   2494  1.1  gwr 			b_dte = (mmu_short_dte_t *) MMU_DTE_PA(*a_dte);
   2495  1.1  gwr 			b_dte = (mmu_short_dte_t *) mmu_ptov(b_dte);
   2496  1.1  gwr 			b_tbl = mmuB2tmgr(b_dte);
   2497  1.1  gwr 			if (end < nstart) {
   2498  1.1  gwr 				pmap_remove_b(b_tbl, start, end);
   2499  1.1  gwr 				return;
   2500  1.1  gwr 			} else {
   2501  1.1  gwr 				pmap_remove_b(b_tbl, start, nstart);
   2502  1.1  gwr 			}
   2503  1.1  gwr 		} else if (end < nstart) {
   2504  1.1  gwr 			return;
   2505  1.1  gwr 		}
   2506  1.1  gwr 	}
   2507  1.1  gwr 	if (nstart < nend) {
   2508  1.1  gwr 		idx = MMU_TIA(nstart);
   2509  1.1  gwr 		a_dte = &a_tbl->at_dtbl[idx];
   2510  1.1  gwr 		rstart = nstart;
   2511  1.1  gwr 		while (rstart < nend) {
   2512  1.1  gwr 			if (MMU_VALID_DT(*a_dte)) {
   2513  1.1  gwr 				b_dte = (mmu_short_dte_t *) MMU_DTE_PA(*a_dte);
   2514  1.1  gwr 				b_dte = (mmu_short_dte_t *) mmu_ptov(b_dte);
   2515  1.1  gwr 				b_tbl = mmuB2tmgr(b_dte);
   2516  1.1  gwr 				a_dte->attr.raw = MMU_DT_INVALID;
   2517  1.1  gwr 				a_tbl->at_ecnt--;
   2518  1.1  gwr 				free_b_table(b_tbl);
   2519  1.1  gwr 				TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
   2520  1.1  gwr 				TAILQ_INSERT_HEAD(&b_pool, b_tbl, bt_link);
   2521  1.1  gwr 			}
   2522  1.1  gwr 			a_dte++;
   2523  1.1  gwr 			rstart += MMU_TIA_RANGE;
   2524  1.1  gwr 		}
   2525  1.1  gwr 	}
   2526  1.1  gwr 	if (nend < end) {
   2527  1.1  gwr 		idx = MMU_TIA(nend);
   2528  1.1  gwr 		a_dte = &a_tbl->at_dtbl[idx];
   2529  1.1  gwr 		if (MMU_VALID_DT(*a_dte)) {
   2530  1.1  gwr 			b_dte = (mmu_short_dte_t *) MMU_DTE_PA(*a_dte);
   2531  1.1  gwr 			b_dte = (mmu_short_dte_t *) mmu_ptov(b_dte);
   2532  1.1  gwr 			b_tbl = mmuB2tmgr(b_dte);
   2533  1.1  gwr 			pmap_remove_b(b_tbl, nend, end);
   2534  1.1  gwr 		}
   2535  1.1  gwr 	}
   2536  1.1  gwr }
   2537  1.1  gwr 
   2538  1.1  gwr /* pmap_remove_b			INTERNAL
   2539  1.1  gwr  **
   2540  1.1  gwr  * Remove a range of addresses from an address space, trying to remove entire
   2541  1.1  gwr  * C tables if possible.
   2542  1.1  gwr  */
   2543  1.1  gwr void
   2544  1.1  gwr pmap_remove_b(b_tbl, start, end)
   2545  1.1  gwr 	b_tmgr_t *b_tbl;
   2546  1.1  gwr 	vm_offset_t start;
   2547  1.1  gwr 	vm_offset_t end;
   2548  1.1  gwr {
   2549  1.1  gwr 	int idx;
   2550  1.1  gwr 	vm_offset_t nstart, nend, rstart;
   2551  1.1  gwr 	c_tmgr_t *c_tbl;
   2552  1.1  gwr 	mmu_short_dte_t  *b_dte;
   2553  1.1  gwr 	mmu_short_pte_t  *c_dte;
   2554  1.1  gwr 
   2555  1.1  gwr 
   2556  1.1  gwr 	nstart = MMU_ROUND_UP_B(start);
   2557  1.1  gwr 	nend = MMU_ROUND_B(end);
   2558  1.1  gwr 
   2559  1.1  gwr 	if (start < nstart) {
   2560  1.1  gwr 		idx = MMU_TIB(start);
   2561  1.1  gwr 		b_dte = &b_tbl->bt_dtbl[idx];
   2562  1.1  gwr 		if (MMU_VALID_DT(*b_dte)) {
   2563  1.1  gwr 			c_dte = (mmu_short_pte_t *) MMU_DTE_PA(*b_dte);
   2564  1.1  gwr 			c_dte = (mmu_short_pte_t *) mmu_ptov(c_dte);
   2565  1.1  gwr 			c_tbl = mmuC2tmgr(c_dte);
   2566  1.1  gwr 			if (end < nstart) {
   2567  1.1  gwr 				pmap_remove_c(c_tbl, start, end);
   2568  1.1  gwr 				return;
   2569  1.1  gwr 			} else {
   2570  1.1  gwr 				pmap_remove_c(c_tbl, start, nstart);
   2571  1.1  gwr 			}
   2572  1.1  gwr 		} else if (end < nstart) {
   2573  1.1  gwr 			return;
   2574  1.1  gwr 		}
   2575  1.1  gwr 	}
   2576  1.1  gwr 	if (nstart < nend) {
   2577  1.1  gwr 		idx = MMU_TIB(nstart);
   2578  1.1  gwr 		b_dte = &b_tbl->bt_dtbl[idx];
   2579  1.1  gwr 		rstart = nstart;
   2580  1.1  gwr 		while (rstart < nend) {
   2581  1.1  gwr 			if (MMU_VALID_DT(*b_dte)) {
   2582  1.1  gwr 				c_dte = (mmu_short_pte_t *) MMU_DTE_PA(*b_dte);
   2583  1.1  gwr 				c_dte = (mmu_short_pte_t *) mmu_ptov(c_dte);
   2584  1.1  gwr 				c_tbl = mmuC2tmgr(c_dte);
   2585  1.1  gwr 				b_dte->attr.raw = MMU_DT_INVALID;
   2586  1.1  gwr 				b_tbl->bt_ecnt--;
   2587  1.1  gwr 				free_c_table(c_tbl);
   2588  1.1  gwr 				TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
   2589  1.1  gwr 				TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
   2590  1.1  gwr 			}
   2591  1.1  gwr 			b_dte++;
   2592  1.1  gwr 			rstart += MMU_TIB_RANGE;
   2593  1.1  gwr 		}
   2594  1.1  gwr 	}
   2595  1.1  gwr 	if (nend < end) {
   2596  1.1  gwr 		idx = MMU_TIB(nend);
   2597  1.1  gwr 		b_dte = &b_tbl->bt_dtbl[idx];
   2598  1.1  gwr 		if (MMU_VALID_DT(*b_dte)) {
   2599  1.1  gwr 			c_dte = (mmu_short_pte_t *) MMU_DTE_PA(*b_dte);
   2600  1.1  gwr 			c_dte = (mmu_short_pte_t *) mmu_ptov(c_dte);
   2601  1.1  gwr 			c_tbl = mmuC2tmgr(c_dte);
   2602  1.1  gwr 			pmap_remove_c(c_tbl, nend, end);
   2603  1.1  gwr 		}
   2604  1.1  gwr 	}
   2605  1.1  gwr }
   2606  1.1  gwr 
   2607  1.1  gwr /* pmap_remove_c			INTERNAL
   2608  1.1  gwr  **
   2609  1.1  gwr  * Remove a range of addresses from the given C table.
   2610  1.1  gwr  */
   2611  1.1  gwr void
   2612  1.1  gwr pmap_remove_c(c_tbl, start, end)
   2613  1.1  gwr 	c_tmgr_t *c_tbl;
   2614  1.1  gwr 	vm_offset_t start;
   2615  1.1  gwr 	vm_offset_t end;
   2616  1.1  gwr {
   2617  1.1  gwr 	int idx;
   2618  1.1  gwr 	mmu_short_pte_t *c_pte;
   2619  1.1  gwr 
   2620  1.1  gwr 	idx = MMU_TIC(start);
   2621  1.1  gwr 	c_pte = &c_tbl->ct_dtbl[idx];
   2622  1.1  gwr 	while (start < end) {
   2623  1.1  gwr 		if (MMU_VALID_DT(*c_pte))
   2624  1.1  gwr 			pmap_remove_pte(c_pte);
   2625  1.1  gwr 		c_tbl->ct_ecnt--;
   2626  1.1  gwr 		start += MMU_PAGE_SIZE;
   2627  1.1  gwr 		c_pte++;
   2628  1.1  gwr 	}
   2629  1.1  gwr }
   2630  1.1  gwr 
   2631  1.1  gwr /* is_managed				INTERNAL
   2632  1.1  gwr  **
   2633  1.1  gwr  * Determine if the given physical address is managed by the PV system.
   2634  1.1  gwr  * Note that this logic assumes that no one will ask for the status of
   2635  1.1  gwr  * addresses which lie in-between the memory banks on the 3/80.  If they
   2636  1.1  gwr  * do so, it will falsely report that it is managed.
   2637  1.1  gwr  */
   2638  1.1  gwr boolean_t
   2639  1.1  gwr is_managed(pa)
   2640  1.1  gwr 	vm_offset_t pa;
   2641  1.1  gwr {
   2642  1.1  gwr 	if (pa >= avail_start && pa < avail_end)
   2643  1.1  gwr 		return TRUE;
   2644  1.1  gwr 	else
   2645  1.1  gwr 		return FALSE;
   2646  1.1  gwr }
   2647  1.1  gwr 
   2648  1.1  gwr /* pa2pv			INTERNAL
   2649  1.1  gwr  **
   2650  1.1  gwr  * Return the pv_list_head element which manages the given physical
   2651  1.1  gwr  * address.
   2652  1.1  gwr  */
   2653  1.1  gwr pv_t *
   2654  1.1  gwr pa2pv(pa)
   2655  1.1  gwr 	vm_offset_t pa;
   2656  1.1  gwr {
   2657  1.1  gwr 	struct pmap_physmem_struct *bank = &avail_mem[0];
   2658  1.1  gwr 
   2659  1.1  gwr 	while (pa >= bank->pmem_end)
   2660  1.1  gwr 		bank = bank->pmem_next;
   2661  1.1  gwr 
   2662  1.1  gwr 	pa -= bank->pmem_start;
   2663  1.1  gwr 	return &pvbase[bank->pmem_pvbase + sun3x_btop(pa)];
   2664  1.1  gwr }
   2665  1.1  gwr 
   2666  1.1  gwr /* pmap_bootstrap_alloc			INTERNAL
   2667  1.1  gwr  **
   2668  1.1  gwr  * Used internally for memory allocation at startup when malloc is not
   2669  1.1  gwr  * available.  This code will fail once it crosses the first memory
   2670  1.1  gwr  * bank boundary on the 3/80.  Hopefully by then however, the VM system
   2671  1.1  gwr  * will be in charge of allocation.
   2672  1.1  gwr  */
   2673  1.1  gwr void *
   2674  1.1  gwr pmap_bootstrap_alloc(size)
   2675  1.1  gwr 	int size;
   2676  1.1  gwr {
   2677  1.1  gwr 	void *rtn;
   2678  1.1  gwr 
   2679  1.1  gwr 	rtn = (void *) virtual_avail;
   2680  1.1  gwr 
   2681  1.1  gwr 	/* While the size is greater than a page, map single pages,
   2682  1.1  gwr 	 * decreasing size until it is less than a page.
   2683  1.1  gwr 	 */
   2684  1.1  gwr 	while (size > NBPG) {
   2685  1.1  gwr 		(void) pmap_bootstrap_alloc(NBPG);
   2686  1.1  gwr 
   2687  1.1  gwr 		/* If the above code is ok, let's keep it.
   2688  1.1  gwr 		 * It looks cooler than:
   2689  1.1  gwr 		 * virtual_avail += NBPG;
   2690  1.1  gwr 		 * avail_start += NBPG;
   2691  1.1  gwr 		 * last_mapped = sun3x_trunc_page(avail_start);
   2692  1.1  gwr 		 * pmap_enter_kernel(last_mapped, last_mapped + KERNBASE,
   2693  1.1  gwr 		 *    VM_PROT_READ|VM_PROT_WRITE);
   2694  1.1  gwr 		 */
   2695  1.1  gwr 
   2696  1.1  gwr 		 size -= NBPG;
   2697  1.1  gwr 	}
   2698  1.1  gwr 	avail_start += size;
   2699  1.1  gwr 	virtual_avail += size;
   2700  1.1  gwr 
   2701  1.1  gwr 	/* did the allocation cross a page boundary? */
   2702  1.1  gwr 	if (last_mapped != sun3x_trunc_page(avail_start)) {
   2703  1.1  gwr 		last_mapped = sun3x_trunc_page(avail_start);
   2704  1.1  gwr 		pmap_enter_kernel(last_mapped + KERNBASE, last_mapped,
   2705  1.1  gwr 		    VM_PROT_READ|VM_PROT_WRITE);
   2706  1.1  gwr 	}
   2707  1.1  gwr 
   2708  1.1  gwr 	return rtn;
   2709  1.1  gwr }
   2710  1.1  gwr 
   2711  1.1  gwr /* pmap_bootstap_aalign			INTERNAL
   2712  1.1  gwr  **
   2713  1.1  gwr  * Used to insure that the next call to pmap_bootstrap_alloc() will return
   2714  1.1  gwr  * a chunk of memory aligned to the specified size.
   2715  1.1  gwr  */
   2716  1.1  gwr void
   2717  1.1  gwr pmap_bootstrap_aalign(size)
   2718  1.1  gwr 	int size;
   2719  1.1  gwr {
   2720  1.1  gwr 	if (((unsigned int) avail_start % size) != 0) {
   2721  1.1  gwr 		(void) pmap_bootstrap_alloc(size -
   2722  1.1  gwr 		    ((unsigned int) (avail_start % size)));
   2723  1.1  gwr 	}
   2724  1.1  gwr }
   2725  1.1  gwr 
   2726  1.1  gwr #if 0
   2727  1.1  gwr /* pmap_activate			INTERFACE
   2728  1.1  gwr  **
   2729  1.1  gwr  * Make the virtual to physical mappings contained in the given
   2730  1.1  gwr  * pmap the current map used by the system.
   2731  1.1  gwr  */
   2732  1.1  gwr void
   2733  1.1  gwr pmap_activate(pmap, pcbp)
   2734  1.1  gwr pmap_t	pmap;
   2735  1.1  gwr struct  pcb *pcbp;
   2736  1.1  gwr {
   2737  1.1  gwr 	vm_offset_t	pa;
   2738  1.1  gwr 	/* Save the A table being loaded in 'curatbl'.
   2739  1.1  gwr 	 * pmap_remove() uses this variable to determine if a given A
   2740  1.1  gwr 	 * table is currently being used as the system map.  If so, it
   2741  1.1  gwr 	 * will issue an MMU cache flush whenever mappings are removed.
   2742  1.1  gwr 	 */
   2743  1.1  gwr 	curatbl = pmap->pm_a_tbl;
   2744  1.1  gwr 	/* call the locore routine to set the user root pointer table */
   2745  1.1  gwr 	pa = mmu_vtop(pmap->pm_a_tbl->at_dtbl);
   2746  1.1  gwr 	mmu_seturp(pa);
   2747  1.1  gwr }
   2748  1.1  gwr #endif
   2749  1.1  gwr 
   2750  1.1  gwr /* pmap_pa_exists
   2751  1.1  gwr  **
   2752  1.1  gwr  * Used by the /dev/mem driver to see if a given PA is memory
   2753  1.1  gwr  * that can be mapped.  (The PA is not in a hole.)
   2754  1.1  gwr  */
   2755  1.1  gwr int
   2756  1.1  gwr pmap_pa_exists(pa)
   2757  1.1  gwr 	vm_offset_t pa;
   2758  1.1  gwr {
   2759  1.1  gwr 	/* XXX - NOTYET */
   2760  1.1  gwr 	return (0);
   2761  1.1  gwr }
   2762  1.1  gwr 
   2763  1.1  gwr 
   2764  1.1  gwr /* pmap_update
   2765  1.1  gwr  **
   2766  1.1  gwr  * Apply any delayed changes scheduled for all pmaps immediately.
   2767  1.1  gwr  *
   2768  1.1  gwr  * No delayed operations are currently done in this pmap.
   2769  1.1  gwr  */
   2770  1.1  gwr void
   2771  1.1  gwr pmap_update()
   2772  1.1  gwr {
   2773  1.1  gwr 	/* not implemented. */
   2774  1.1  gwr }
   2775  1.1  gwr 
   2776  1.1  gwr /* pmap_virtual_space			INTERFACE
   2777  1.1  gwr  **
   2778  1.1  gwr  * Return the current available range of virtual addresses in the
   2779  1.1  gwr  * arguuments provided.  Only really called once.
   2780  1.1  gwr  */
   2781  1.1  gwr void
   2782  1.1  gwr pmap_virtual_space(vstart, vend)
   2783  1.1  gwr 	vm_offset_t *vstart, *vend;
   2784  1.1  gwr {
   2785  1.1  gwr 	*vstart = virtual_avail;
   2786  1.1  gwr 	*vend = virtual_end;
   2787  1.1  gwr }
   2788  1.1  gwr 
   2789  1.1  gwr /* pmap_free_pages			INTERFACE
   2790  1.1  gwr  **
   2791  1.1  gwr  * Return the number of physical pages still available.
   2792  1.1  gwr  *
   2793  1.1  gwr  * This is probably going to be a mess, but it's only called
   2794  1.1  gwr  * once and it's the only function left that I have to implement!
   2795  1.1  gwr  */
   2796  1.1  gwr u_int
   2797  1.1  gwr pmap_free_pages()
   2798  1.1  gwr {
   2799  1.1  gwr 	int i;
   2800  1.1  gwr 	u_int left;
   2801  1.1  gwr 	vm_offset_t avail;
   2802  1.1  gwr 
   2803  1.1  gwr 	avail = sun3x_round_up_page(avail_start);
   2804  1.1  gwr 
   2805  1.1  gwr 	left = 0;
   2806  1.1  gwr 	i = 0;
   2807  1.1  gwr 	while (avail >= avail_mem[i].pmem_end) {
   2808  1.1  gwr 		if (avail_mem[i].pmem_next == NULL)
   2809  1.1  gwr 			return 0;
   2810  1.1  gwr 		i++;
   2811  1.1  gwr 	}
   2812  1.1  gwr 	while (i < SUN3X_80_MEM_BANKS) {
   2813  1.1  gwr 		if (avail < avail_mem[i].pmem_start) {
   2814  1.1  gwr 			/* Avail is inside a hole, march it
   2815  1.1  gwr 			 * up to the next bank.
   2816  1.1  gwr 			 */
   2817  1.1  gwr 			avail = avail_mem[i].pmem_start;
   2818  1.1  gwr 		}
   2819  1.1  gwr 		left += sun3x_btop(avail_mem[i].pmem_end - avail);
   2820  1.1  gwr 		if (avail_mem[i].pmem_next == NULL)
   2821  1.1  gwr 			break;
   2822  1.1  gwr 		i++;
   2823  1.1  gwr 	}
   2824  1.1  gwr 
   2825  1.1  gwr 	return left;
   2826  1.1  gwr }
   2827  1.1  gwr 
   2828  1.1  gwr /* pmap_page_index			INTERFACE
   2829  1.1  gwr  **
   2830  1.1  gwr  * Return the index of the given physical page in a list of useable
   2831  1.1  gwr  * physical pages in the system.  Holes in physical memory may be counted
   2832  1.1  gwr  * if so desired.  As long as pmap_free_pages() and pmap_page_index()
   2833  1.1  gwr  * agree as to whether holes in memory do or do not count as valid pages,
   2834  1.1  gwr  * it really doesn't matter.  However, if you like to save a little
   2835  1.1  gwr  * memory, don't count holes as valid pages.  This is even more true when
   2836  1.1  gwr  * the holes are large.
   2837  1.1  gwr  *
   2838  1.1  gwr  * We will not count holes as valid pages.  We can generate page indexes
   2839  1.1  gwr  * that conform to this by using the memory bank structures initialized
   2840  1.1  gwr  * in pmap_alloc_pv().
   2841  1.1  gwr  */
   2842  1.1  gwr int
   2843  1.1  gwr pmap_page_index(pa)
   2844  1.1  gwr 	vm_offset_t pa;
   2845  1.1  gwr {
   2846  1.1  gwr 	struct pmap_physmem_struct *bank = avail_mem;
   2847  1.1  gwr 
   2848  1.1  gwr 	while (pa > bank->pmem_end)
   2849  1.1  gwr 		bank = bank->pmem_next;
   2850  1.1  gwr 	pa -= bank->pmem_start;
   2851  1.1  gwr 
   2852  1.1  gwr 	return (bank->pmem_pvbase + sun3x_btop(pa));
   2853  1.1  gwr }
   2854  1.1  gwr 
   2855  1.1  gwr /* pmap_next_page			INTERFACE
   2856  1.1  gwr  **
   2857  1.1  gwr  * Place the physical address of the next available page in the
   2858  1.1  gwr  * argument given.  Returns FALSE if there are no more pages left.
   2859  1.1  gwr  *
   2860  1.1  gwr  * This function must jump over any holes in physical memory.
   2861  1.1  gwr  * Once this function is used, any use of pmap_bootstrap_alloc()
   2862  1.1  gwr  * is a sin.  Sinners will be punished with erratic behavior.
   2863  1.1  gwr  */
   2864  1.1  gwr boolean_t
   2865  1.1  gwr pmap_next_page(pa)
   2866  1.1  gwr 	vm_offset_t *pa;
   2867  1.1  gwr {
   2868  1.1  gwr 	static boolean_t initialized = FALSE;
   2869  1.1  gwr 	static struct pmap_physmem_struct *curbank = avail_mem;
   2870  1.1  gwr 
   2871  1.1  gwr 	if (!initialized) {
   2872  1.1  gwr 		pmap_bootstrap_aalign(NBPG);
   2873  1.1  gwr 		initialized = TRUE;
   2874  1.1  gwr 	}
   2875  1.1  gwr 
   2876  1.1  gwr 	if (avail_start >= curbank->pmem_end)
   2877  1.1  gwr 		if (curbank->pmem_next == NULL)
   2878  1.1  gwr 			return FALSE;
   2879  1.1  gwr 		else {
   2880  1.1  gwr 			curbank = curbank->pmem_next;
   2881  1.1  gwr 			avail_start = curbank->pmem_start;
   2882  1.1  gwr 		}
   2883  1.1  gwr 
   2884  1.1  gwr 	*pa = avail_start;
   2885  1.1  gwr 	avail_start += NBPG;
   2886  1.1  gwr 	return TRUE;
   2887  1.1  gwr }
   2888  1.1  gwr 
   2889  1.1  gwr /************************ SUN3 COMPATIBILITY ROUTINES ********************
   2890  1.1  gwr  * The following routines are only used by DDB for tricky kernel text    *
   2891  1.1  gwr  * text operations in db_memrw.c.  They are provided for sun3            *
   2892  1.1  gwr  * compatibility.                                                        *
   2893  1.1  gwr  *************************************************************************/
   2894  1.1  gwr /* get_pte			INTERNAL
   2895  1.1  gwr  **
   2896  1.1  gwr  * Return the page descriptor the describes the kernel mapping
   2897  1.1  gwr  * of the given virtual address.
   2898  1.1  gwr  *
   2899  1.1  gwr  * XXX - It might be nice if this worked outside of the MMU
   2900  1.1  gwr  * structures we manage.  (Could do it with ptest). -gwr
   2901  1.1  gwr  */
   2902  1.1  gwr vm_offset_t
   2903  1.1  gwr get_pte(va)
   2904  1.1  gwr 	vm_offset_t va;
   2905  1.1  gwr {
   2906  1.1  gwr 	u_long idx;
   2907  1.1  gwr 
   2908  1.1  gwr 	idx = (unsigned long) sun3x_btop(mmu_vtop(va));
   2909  1.1  gwr 	return (kernCbase[idx].attr.raw);
   2910  1.1  gwr }
   2911  1.1  gwr 
   2912  1.1  gwr /* set_pte			INTERNAL
   2913  1.1  gwr  **
   2914  1.1  gwr  * Set the page descriptor that describes the kernel mapping
   2915  1.1  gwr  * of the given virtual address.
   2916  1.1  gwr  */
   2917  1.1  gwr void
   2918  1.1  gwr set_pte(va, pte)
   2919  1.1  gwr 	vm_offset_t va;
   2920  1.1  gwr 	vm_offset_t pte;
   2921  1.1  gwr {
   2922  1.1  gwr 	u_long idx;
   2923  1.1  gwr 
   2924  1.1  gwr 	idx = (unsigned long) sun3x_btop(mmu_vtop(va));
   2925  1.1  gwr 	kernCbase[idx].attr.raw = pte;
   2926  1.1  gwr }
   2927  1.1  gwr 
   2928  1.1  gwr #ifdef NOT_YET
   2929  1.1  gwr /* and maybe not ever */
   2930  1.1  gwr /************************** LOW-LEVEL ROUTINES **************************
   2931  1.1  gwr  * These routines will eventualy be re-written into assembly and placed *
   2932  1.1  gwr  * in locore.s.  They are here now as stubs so that the pmap module can *
   2933  1.1  gwr  * be linked as a standalone user program for testing.                  *
   2934  1.1  gwr  ************************************************************************/
   2935  1.1  gwr /* flush_atc_crp			INTERNAL
   2936  1.1  gwr  **
   2937  1.1  gwr  * Flush all page descriptors derived from the given CPU Root Pointer
   2938  1.1  gwr  * (CRP), or 'A' table as it is known here, from the 68851's automatic
   2939  1.1  gwr  * cache.
   2940  1.1  gwr  */
   2941  1.1  gwr void
   2942  1.1  gwr flush_atc_crp(a_tbl)
   2943  1.1  gwr {
   2944  1.1  gwr 	mmu_long_rp_t rp;
   2945  1.1  gwr 
   2946  1.1  gwr 	/* Create a temporary root table pointer that points to the
   2947  1.1  gwr 	 * given A table.
   2948  1.1  gwr 	 */
   2949  1.1  gwr 	rp.attr.raw = ~MMU_LONG_RP_LU;
   2950  1.1  gwr 	rp.addr.raw = (unsigned int) a_tbl;
   2951  1.1  gwr 
   2952  1.1  gwr 	mmu_pflushr(&rp);
   2953  1.1  gwr 	/* mmu_pflushr:
   2954  1.1  gwr 	 * 	movel   sp(4)@,a0
   2955  1.1  gwr 	 * 	pflushr a0@
   2956  1.1  gwr 	 *	rts
   2957  1.1  gwr 	 */
   2958  1.1  gwr }
   2959  1.1  gwr #endif /* NOT_YET */
   2960