Home | History | Annotate | Line # | Download | only in sun68k
      1 /*	$NetBSD: pmap_sunmmu.c,v 1.8 2026/11/15 15:16:25 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Adam Glass, Gordon W. Ross, and Matthew Fredette.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Unified pmap module for 68010 (Sun 2) and 68020 (Sun 3) systems with
     34  * the Sun MMU.  Origins:
     35  *
     36  *	sys/arch/sun3/sun3/pmap.c 1.189
     37  *	sys/arch/sun2/sun2/pmap.c 1.62
     38  *
     39  * ...smooshed together by thorpej, July 2026.
     40  *
     41  * Some notes:
     42  *
     43  * The Sun MMU has a 2-level translation scheme; the level-1 map is called
     44  * the Segment Map and the level-2 map is called the Page Map.  The Segment
     45  * Map is broken up into 8 contexts, and each context provides the root for
     46  * an address space.
     47  *
     48  * The Sun 2 MMU has separate user vs. kernel context registers.  This
     49  * pmap statically assigns context 0 to the kernel.
     50  *
     51  * The Sun 3 MMU, on the other hand, has only a single context register,
     52  * and thus the kernel must be mapped into all contexts.
     53  *
     54  * In both cases, context 0 is also assigned as the "empty context", devoid
     55  * of user-accessible mappings.  This is the context that pmaps are assigned
     56  * when they are brand new or when they have been evicted from the MMU.
     57  *
     58  * The context itself selects a window in the Segment Map.  The Segment Map
     59  * is comprised of single byte entries that index a Page Map Entry Group
     60  * (PMEG).  Each PMEG is a group of 16 Page Map entries.
     61  *
     62  * Sun 2:
     63  *	512 Segment Map entries *
     64  *	16 entries per PMEG *
     65  *	2K page size =			(32KB segments)
     66  *	16MB virtual address space per context (i.e. the 68010 limit)
     67  *	(kernel gets its own 16MB context)
     68  *
     69  * Sun 3:
     70  *	2048 Segment Map entries *
     71  *	16 entries per PMEG *
     72  *	8K page size =			(128KB segments)
     73  *	256MB virtual address space per context
     74  *	(kernel lives in the upper 32MB of each context)
     75  *
     76  * As you might guess, both contexts and PMEGs are in short supply and
     77  * heavy demand.  PMEGs allocated to the kernel are "static" in the sense
     78  * that they can't be stolen from it.  PMEGs allocated to a particular
     79  * segment of a pmap's virtual space will by fought over by the other
     80  * pmaps.
     81  *
     82  * The same is true for contexts.  With one context set aside as the
     83  * "empty" contexts, there are 7 available for user processes.  If you
     84  * have more than 7 user processes running at any given time, then some
     85  * of those processes are getting evicted from the MMU entirely.
     86  *
     87  * Ultimately, the resources must be shared.
     88  *
     89  * On the Sun 3, we also have to worry about cache management.
     90  * - All Sun 3 cache implementations are write-back.
     91  * - Cache flushes must be performed *before* removing translations
     92  *   because the cache is virtually-addressed.
     93  *
     94  * Other desired attributes of this implementation:
     95  * - PMEGs are aren't needed by a pmap remain in the MMU and
     96  *   are lazily-reclaimed.
     97  * - Quick context switches between pmaps
     98  * - Something not applicable on a given platform are optimized
     99  *   out a compile-time.
    100  */
    101 
    102 /*
    103  * [Historical notes here; it's unclear if Project1 is necessary,
    104  * since it that idea predates NetBSD's kthread(9) interface.]
    105  *
    106  * Project1:  Use a "null" context for processes that have not
    107  * touched any user-space address recently.  This is efficient
    108  * for things that stay in the kernel for a while, waking up
    109  * to handle some I/O then going back to sleep (i.e. nfsd).
    110  * If and when such a process returns to user-mode, it will
    111  * fault and be given a real context at that time.
    112  *
    113  * This also lets context switch be fast, because all we need
    114  * to do there for the MMU is slam the context register.
    115  *
    116  * Project2:  Use a private pool of PV elements.  This pool can be
    117  * fixed size because the total mapped virtual space supported by
    118  * the MMU H/W (and this pmap) is fixed for all time.
    119  */
    120 
    121 #include <sys/cdefs.h>
    122 __KERNEL_RCSID(0, "$NetBSD: pmap_sunmmu.c,v 1.8 2026/11/15 15:16:25 thorpej Exp $");
    123 
    124 #include "opt_ddb.h"
    125 #include "opt_kgdb.h"
    126 #include "opt_pmap_debug.h"
    127 
    128 #include <sys/param.h>
    129 #include <sys/systm.h>
    130 #include <sys/proc.h>
    131 #include <sys/kmem.h>
    132 #include <sys/pool.h>
    133 #include <sys/queue.h>
    134 #include <sys/kcore.h>
    135 #include <sys/atomic.h>
    136 
    137 #include <uvm/uvm.h>
    138 
    139 #include <machine/cpu.h>
    140 #include <machine/dvma.h>
    141 #include <machine/fcode.h>
    142 #include <machine/idprom.h>
    143 #include <machine/kcore.h>
    144 #include <machine/promlib.h>
    145 #include <machine/pmap.h>
    146 #include <machine/pte.h>
    147 #include <machine/vmparam.h>
    148 #include <m68k/cacheops.h>
    149 #include <m68k/seglist.h>
    150 
    151 #ifdef DDB
    152 #include <ddb/db_output.h>
    153 #else
    154 #define db_printf printf
    155 #endif
    156 
    157 #ifdef __mc68010__
    158 #define	IS_SUN2		1
    159 #define	IS_SUN3		0
    160 #else
    161 #define	IS_SUN2		0
    162 #define	IS_SUN3		1
    163 #endif /* __mc68010__ */
    164 
    165 #if IS_SUN2
    166 
    167 #include <sun2/sun2/control.h>
    168 #include <sun2/sun2/machdep.h>
    169 
    170 #define	sun_round_seg(va)	sun2_round_seg(va)
    171 #define	sun_trunc_seg(va)	sun2_trunc_seg(va)
    172 
    173 /* Type bits in a "pseudo" physical address. (XXX: pmap.h?) */
    174 #define PMAP_TYPE	PMAP_MBIO
    175 
    176 /* User segments are all of them. */
    177 #define	NUSEG		(NSEGMAP)
    178 
    179 /*
    180  * Only "main memory" pages are registered in the pv_lists.
    181  * This macro is used to determine if a given pte refers to
    182  * "main memory" or not.  One slight hack here deserves more
    183  * explanation:  On the Sun-2, the bwtwo and zs1 appear
    184  * as PG_OBMEM devices at 0x00700000 and 0x00780000,
    185  * respectively.  We do not want to consider these as
    186  * "main memory" so the macro below treats obmem addresses
    187  * >= 0x00700000 as device addresses.  NB: this means for now,
    188  * you can't have a headless Sun-2 with 8MB of main memory.
    189  */
    190 #define	IS_MAIN_MEM(pte) (((pte) & PG_TYPE) == 0 && PG_PA(pte) < 0x00700000)
    191 
    192 /* Does this (pseudo) PA represent device space? */
    193 #define PA_IS_DEV(pa) (((pa) & PMAP_TYPE) != 0 || (pa) >= 0x00700000)
    194 
    195 /*
    196  * No virtually-address cache aliases to worry about on Sun-2.
    197  */
    198 #define	BADALIAS(a1, a2)	(0)
    199 
    200 #else /* IS_SUN3 */
    201 
    202 #include <sun3/sun3/control.h>
    203 #include <sun3/sun3/machdep.h>
    204 #include <sun3/sun3/obmem.h>
    205 
    206 #define	sun_round_seg(va)	sun3_round_seg(va)
    207 #define	sun_trunc_seg(va)	sun3_trunc_seg(va)
    208 
    209 /* Type bits in a "pseudo" physical address. (XXX: pmap.h?) */
    210 #define PMAP_TYPE	PMAP_VME32
    211 
    212 /* User segments from 0 to KERNBASE */
    213 #define	NUSEG		(KERNBASE3 / NBSG)
    214 /* The remainder are kernel segments. */
    215 #define	NKSEG		(NSEGMAP - NUSEG)
    216 
    217 /*
    218  * Only "main memory" pages are registered in the pv_lists.
    219  * This macro is used to determine if a given pte refers to
    220  * "main memory" or not.  One slight hack here deserves more
    221  * explanation:  The Sun frame buffers all appear as PG_OBMEM
    222  * devices but way up near the end of the address space.
    223  * We do not want to consider these as "main memory" so the
    224  * macro below treats the high bits of the PFN as type bits.
    225  *
    226  * Note that on the 3/60 only 16 bits of PFN are stored in the
    227  * MMU and the top 3 bits read back as zero.  This means a
    228  * translation entered into the mmu for physical address
    229  * 0xFF000000 will look like 0x1F000000 after one reads back
    230  * the pte and converts the PFN to a physical address.
    231  */
    232 #define MEM_BITS	(PG_TYPE | PA_PGNUM(0xF8000000))
    233 #define	IS_MAIN_MEM(pte) (((pte) & MEM_BITS) == 0)
    234 
    235 /* Does this (pseudo) PA represent device space? */
    236 #define PA_DEV_MASK   (0xF8000000 | PMAP_TYPE)
    237 #define PA_IS_DEV(pa) ((pa) & PA_DEV_MASK)
    238 
    239 /*
    240  * Is there a Virtually Addressed Cache (VAC) alias problem
    241  * if one page is mapped at both a1 and a2?
    242  */
    243 #define	BADALIAS(a1, a2)	(((int)(a1) ^ (int)(a2)) & SEGOFSET)
    244 
    245 #endif /* IS_SUN2 */
    246 
    247 /* Verify this correspondence between definitions. */
    248 #if	(PMAP_OBIO << PG_MOD_SHIFT) != PGT_OBIO
    249 #error	"PMAP_XXX definitions don't match pte.h!"
    250 #endif
    251 
    252 /*
    253  * Local convenience macros
    254  */
    255 
    256 #define DVMA_MAP_END	(DVMA_MAP_BASE + DVMA_MAP_AVAIL)
    257 #define VA_SEGNUM(x)	((u_int)(x) >> SEGSHIFT)
    258 
    259 /*
    260  * Debugging support.
    261  */
    262 #define	PMD_ENTER	1
    263 #define	PMD_LINK	2
    264 #define	PMD_PROTECT	4
    265 #define	PMD_SWITCH	8
    266 #define PMD_COW		0x10
    267 #define PMD_MODBIT	0x20
    268 #define PMD_REFBIT	0x40
    269 #define PMD_WIRING	0x80
    270 #define PMD_CONTEXT	0x100
    271 #define PMD_CREATE	0x200
    272 #define PMD_SEGMAP	0x400
    273 #define PMD_SETPTE	0x800
    274 #define PMD_FAULT	0x1000
    275 #define PMD_KMAP	0x2000
    276 
    277 #define	PMD_REMOVE	PMD_ENTER
    278 #define	PMD_UNLINK	PMD_LINK
    279 
    280 #ifdef	PMAP_DEBUG
    281 int pmap_debug = 0;
    282 int pmap_db_watchva = -1;
    283 int pmap_db_watchpmeg = -1;
    284 #endif	/* PMAP_DEBUG */
    285 
    286 /*
    287  * Miscellaneous variables.
    288  *
    289  * For simplicity, this interface retains the variables
    290  * that were used in the old interface (without NONCONTIG).
    291  */
    292 vaddr_t virtual_avail, virtual_end;
    293 paddr_t avail_start, avail_end;
    294 
    295 #if IS_SUN3
    296 /* used to skip the Sun3/50 video RAM */
    297 static vaddr_t hole_start, hole_size;
    298 #endif
    299 
    300 /* This is where we map a PMEG without a context. */
    301 static vaddr_t temp_seg_va;
    302 #ifdef DIAGNOSTIC
    303 static int temp_seg_inuse;
    304 #endif
    305 
    306 int tmp_vpages_inuse;
    307 
    308 static int pmap_version = 1;
    309 static struct pmap kernel_pmap_store;
    310 struct pmap *const kernel_pmap_ptr = &kernel_pmap_store;
    311 #define kernel_pmap (kernel_pmap_ptr)
    312 static u_char kernel_segmap[NSEGMAP];
    313 
    314 /* memory pool for pmap structures */
    315 struct pool	pmap_pmap_pool;
    316 
    317 /* statistics... */
    318 struct pmap_stats {
    319 	int	ps_enter_firstpv;	/* pv heads entered */
    320 	int	ps_enter_secondpv;	/* pv nonheads entered */
    321 	int	ps_unlink_pvfirst;	/* of pv_unlinks on head */
    322 	int	ps_unlink_pvsearch;	/* of pv_unlink searches */
    323 	int	ps_pmeg_faultin;	/* pmegs reloaded */
    324 	int	ps_changeprots;		/* of calls to changeprot */
    325 	int	ps_changewire;		/* useless wiring changes */
    326 	int	ps_npg_prot_all;	/* of active pages protected */
    327 	int	ps_npg_prot_actual;	/* pages actually affected */
    328 	int	ps_vac_uncached;	/* non-cached due to bad alias */
    329 	int	ps_vac_recached;	/* re-cached when bad alias gone */
    330 } pmap_stats;
    331 
    332 #ifdef	PMAP_DEBUG
    333 #define	CHECK_SPL() do { \
    334 	if ((getsr() & PSL_IPL) < PSL_IPL4) \
    335 		panic("pmap: bad spl, line %d", __LINE__); \
    336 } while (0)
    337 #else	/* PMAP_DEBUG */
    338 #define	CHECK_SPL() (void)0
    339 #endif	/* PMAP_DEBUG */
    340 
    341 
    342 /*
    343  * PV support.
    344  * (i.e. Find all virtual mappings of a physical page.)
    345  */
    346 
    347 int pv_initialized = 0;
    348 
    349 /* One of these for each mapped virtual page. */
    350 struct pv_entry {
    351 	struct pv_entry *pv_next;
    352 	pmap_t	       pv_pmap;
    353 	vaddr_t        pv_va;
    354 };
    355 typedef struct pv_entry *pv_entry_t;
    356 
    357 /* Table of PV list heads (per physical page). */
    358 static struct pv_entry **pv_head_tbl;
    359 
    360 /* Free list of PV entries. */
    361 static struct pv_entry *pv_free_list;
    362 
    363 /* Table of flags (per physical page). */
    364 static u_char *pv_flags_tbl;
    365 
    366 /* These are as in the MMU but shifted by PV_SHIFT. */
    367 #if IS_SUN2
    368 #define	PV_SHIFT	20
    369 #else
    370 #define	PV_SHIFT	24
    371 #endif
    372 #define	PV_NC		(PG_NC >> PV_SHIFT)
    373 #define	PV_REF		(PG_REF >> PV_SHIFT)
    374 #define	PV_MOD		(PG_MOD >> PV_SHIFT)
    375 
    376 /*
    377  * context structures, and queues
    378  */
    379 
    380 struct context_state {
    381 	TAILQ_ENTRY(context_state) context_link;
    382 	int            context_num;
    383 	struct pmap   *context_upmap;
    384 };
    385 typedef struct context_state *context_t;
    386 
    387 #define INVALID_CONTEXT -1	/* impossible value */
    388 #define EMPTY_CONTEXT 0
    389 #define FIRST_CONTEXT 1
    390 #if IS_SUN2
    391 #define KERNEL_CONTEXT 0
    392 #define	kernel_context_enter()	int saved_ctx = get_context()
    393 #define	kernel_context_exit()	set_context(saved_ctx)
    394 #define	set_kernel_segmap(v, s)	set_segmap((v), (s))
    395 #define	set_kernel_context()	set_context(KERNEL_CONTEXT)
    396 #define	has_context(pmap)	(((pmap)->pm_ctxnum != EMPTY_CONTEXT) == ((pmap) != kernel_pmap))
    397 #else /* IS_SUN3 */
    398 #define	kernel_context_enter()	__nothing
    399 #define	kernel_context_exit()	__nothing
    400 #define	set_kernel_segmap(v, s)	set_segmap_allctx((v), (s))
    401 #define	set_kernel_context()	__nothing
    402 #define	has_context(pmap)	((pmap)->pm_ctxnum != EMPTY_CONTEXT)
    403 #endif /* IS_SUN2 */
    404 
    405 TAILQ_HEAD(context_tailq, context_state)
    406 	context_free_queue, context_active_queue;
    407 
    408 static struct context_state context_array[NCONTEXT];
    409 
    410 
    411 /*
    412  * PMEG structures, queues, and macros
    413  */
    414 #define PMEGQ_FREE     0
    415 #define PMEGQ_INACTIVE 1
    416 #define PMEGQ_ACTIVE   2
    417 #define PMEGQ_KERNEL   3
    418 #define PMEGQ_NONE     4
    419 
    420 struct pmeg_state {
    421 	TAILQ_ENTRY(pmeg_state) pmeg_link;
    422 	int            pmeg_index;
    423 	pmap_t         pmeg_owner;
    424 	int            pmeg_version;
    425 	vaddr_t        pmeg_va;
    426 	int            pmeg_wired;
    427 	int            pmeg_reserved;
    428 	int            pmeg_vpages;
    429 	int            pmeg_qstate;
    430 };
    431 
    432 typedef struct pmeg_state *pmeg_t;
    433 
    434 #define PMEG_INVAL (NPMEG-1)
    435 #define PMEG_NULL (pmeg_t) NULL
    436 
    437 /* XXX - Replace pmeg_kernel_queue with pmeg_wired_queue ? */
    438 TAILQ_HEAD(pmeg_tailq, pmeg_state)
    439 	pmeg_free_queue, pmeg_inactive_queue,
    440 	pmeg_active_queue, pmeg_kernel_queue;
    441 
    442 static struct pmeg_state pmeg_array[NPMEG];
    443 
    444 
    445 /*
    446  * prototypes
    447  */
    448 static int get_pte_pmeg(int, int);
    449 static void set_pte_pmeg(int, int, int);
    450 
    451 static void context_allocate(pmap_t);
    452 static void context_free(pmap_t);
    453 static void context_init(void);
    454 
    455 static void pmeg_init(void);
    456 static void pmeg_reserve(int);
    457 
    458 static pmeg_t pmeg_allocate(pmap_t, vaddr_t);
    459 static void pmeg_mon_init(vaddr_t, vaddr_t, int);
    460 static void pmeg_release(pmeg_t);
    461 static void pmeg_free(pmeg_t);
    462 static pmeg_t pmeg_cache(pmap_t, vaddr_t);
    463 static void pmeg_set_wiring(pmeg_t, vaddr_t, int);
    464 
    465 static int  pv_link  (pmap_t, int, vaddr_t);
    466 static void pv_unlink(pmap_t, int, vaddr_t);
    467 static void pv_remove_all(paddr_t);
    468 static void pv_changepte(paddr_t, int, int);
    469 static u_int pv_syncflags(pv_entry_t);
    470 static void pv_init(void);
    471 
    472 static void pmeg_clean(pmeg_t);
    473 static void pmeg_clean_free(void);
    474 
    475 static void pmap_common_init(pmap_t);
    476 static void pmap_kernel_init(pmap_t);
    477 static void pmap_user_init(pmap_t);
    478 
    479 static void pmap_enter_kernel(vaddr_t, int, bool);
    480 static void pmap_enter_user(pmap_t, vaddr_t, int, bool);
    481 
    482 static void pmap_protect1(pmap_t, vaddr_t, vaddr_t);
    483 static void pmap_protect_mmu(pmap_t, vaddr_t, vaddr_t);
    484 static void pmap_protect_noctx(pmap_t, vaddr_t, vaddr_t);
    485 
    486 static void pmap_remove1(pmap_t, vaddr_t, vaddr_t);
    487 static void pmap_remove_mmu(pmap_t, vaddr_t, vaddr_t);
    488 static void pmap_remove_noctx(pmap_t, vaddr_t, vaddr_t);
    489 
    490 static int  pmap_fault_reload(struct pmap *, vaddr_t, int);
    491 
    492 #ifdef	PMAP_DEBUG
    493 void pmap_print(pmap_t);
    494 void pv_print(paddr_t);
    495 void pmeg_print(pmeg_t);
    496 static void pmeg_verify_empty(vaddr_t);
    497 #endif	/* PMAP_DEBUG */
    498 void pmap_pinit(pmap_t);
    499 void pmap_release(pmap_t);
    500 
    501 /*
    502  * Various in-line helper functions.
    503  */
    504 
    505 static inline pmap_t
    506 current_pmap(void)
    507 {
    508 	struct vmspace *vm;
    509 	struct vm_map *map;
    510 	pmap_t	pmap;
    511 
    512 	vm = curproc->p_vmspace;
    513 	map = &vm->vm_map;
    514 	pmap = vm_map_pmap(map);
    515 
    516 	return (pmap);
    517 }
    518 
    519 static inline struct pv_entry **
    520 pa_to_pvhead(paddr_t pa)
    521 {
    522 	int idx;
    523 
    524 	idx = PA_PGNUM(pa);
    525 #ifdef	DIAGNOSTIC
    526 	if (PA_IS_DEV(pa) || (idx >= physmem))
    527 		panic("pmap:pa_to_pvhead: bad pa=0x%lx", pa);
    528 #endif
    529 	return (&pv_head_tbl[idx]);
    530 }
    531 
    532 static inline u_char *
    533 pa_to_pvflags(paddr_t pa)
    534 {
    535 	int idx;
    536 
    537 	idx = PA_PGNUM(pa);
    538 #ifdef	DIAGNOSTIC
    539 	if (PA_IS_DEV(pa) || (idx >= physmem))
    540 		panic("pmap:pa_to_pvflags: bad pa=0x%lx", pa);
    541 #endif
    542 	return (&pv_flags_tbl[idx]);
    543 }
    544 
    545 /*
    546  * Save the MOD bit from the given PTE using its PA
    547  */
    548 static inline void
    549 save_modref_bits(int pte)
    550 {
    551 	u_char *pv_flags;
    552 
    553 	pv_flags = pa_to_pvflags(PG_PA(pte));
    554 	*pv_flags |= ((pte & PG_MODREF) >> PV_SHIFT);
    555 }
    556 
    557 static inline pmeg_t
    558 pmeg_p(int sme)
    559 {
    560 #ifdef	DIAGNOSTIC
    561 	if (sme < 0 || sme >= SEGINV)
    562 		panic("pmeg_p: bad sme");
    563 #endif
    564 	return &pmeg_array[sme];
    565 }
    566 
    567 #define is_pmeg_wired(pmegp) (pmegp->pmeg_wired != 0)
    568 
    569 static void
    570 pmeg_set_wiring(pmeg_t pmegp, vaddr_t va, int flag)
    571 {
    572 	int idx, mask;
    573 
    574 	idx = VA_PTE_NUM(va);
    575 	mask = 1 << idx;
    576 
    577 	if (flag)
    578 		pmegp->pmeg_wired |= mask;
    579 	else
    580 		pmegp->pmeg_wired &= ~mask;
    581 }
    582 
    583 /****************************************************************
    584  * Context management functions.
    585  */
    586 
    587 /* part of pmap_bootstrap */
    588 static void
    589 context_init(void)
    590 {
    591 	int i;
    592 
    593 	TAILQ_INIT(&context_free_queue);
    594 	TAILQ_INIT(&context_active_queue);
    595 
    596 	/* Leave EMPTY_CONTEXT out of the free list. */
    597 	context_array[0].context_upmap = kernel_pmap;
    598 
    599 	for (i = 1; i < NCONTEXT; i++) {
    600 		context_array[i].context_num = i;
    601 		context_array[i].context_upmap = NULL;
    602 		TAILQ_INSERT_TAIL(&context_free_queue, &context_array[i],
    603 				  context_link);
    604 #ifdef	PMAP_DEBUG
    605 		if (pmap_debug & PMD_CONTEXT)
    606 			printf("context_init: sizeof(context_array[0])=%d\n",
    607 			       sizeof(context_array[0]));
    608 #endif
    609 	}
    610 }
    611 
    612 /* Get us a context (steal one if necessary). */
    613 static void
    614 context_allocate(pmap_t pmap)
    615 {
    616 	context_t context;
    617 
    618 	CHECK_SPL();
    619 
    620 #ifdef	DIAGNOSTIC
    621 	if (pmap == kernel_pmap)
    622 		panic("context_allocate: kernel_pmap");
    623 	if (has_context(pmap))
    624 		panic("pmap: pmap already has context allocated to it");
    625 #endif
    626 
    627 	context = TAILQ_FIRST(&context_free_queue);
    628 	if (context == NULL) {
    629 		/* Steal the head of the active queue. */
    630 		context = TAILQ_FIRST(&context_active_queue);
    631 		if (context == NULL)
    632 			panic("pmap: no contexts left?");
    633 #ifdef	PMAP_DEBUG
    634 		if (pmap_debug & PMD_CONTEXT)
    635 			printf("context_allocate: steal ctx %d from pmap %p\n",
    636 			       context->context_num, context->context_upmap);
    637 #endif
    638 		context_free(context->context_upmap);
    639 		context = TAILQ_FIRST(&context_free_queue);
    640 	}
    641 	TAILQ_REMOVE(&context_free_queue, context, context_link);
    642 
    643 #ifdef DIAGNOSTIC
    644 	if (context->context_upmap != NULL)
    645 		panic("pmap: context in use???");
    646 #endif
    647 
    648 	context->context_upmap = pmap;
    649 	pmap->pm_ctxnum = context->context_num;
    650 
    651 	TAILQ_INSERT_TAIL(&context_active_queue, context, context_link);
    652 
    653 	/*
    654 	 * We could reload the MMU here, but that would
    655 	 * artificially move PMEGs from the inactive queue
    656 	 * to the active queue, so do lazy reloading.
    657 	 * XXX - Need to reload wired pmegs though...
    658 	 * XXX: Verify the context it is empty?
    659 	 */
    660 }
    661 
    662 /*
    663  * Unload the context and put it on the free queue.
    664  */
    665 static void
    666 context_free(pmap_t pmap)
    667 {
    668 	int saved_ctxnum, ctxnum;
    669 	int i, sme;
    670 	context_t contextp;
    671 	vaddr_t va;
    672 
    673 	CHECK_SPL();
    674 
    675 	ctxnum = pmap->pm_ctxnum;
    676 	if (ctxnum < FIRST_CONTEXT || ctxnum >= NCONTEXT)
    677 		panic("pmap: context_free ctxnum");
    678 	contextp = &context_array[ctxnum];
    679 
    680 	/* Temporary context change. */
    681 	saved_ctxnum = get_context();
    682 	set_context(ctxnum);
    683 
    684 	/* Before unloading translations, flush cache. */
    685 #ifdef	HAVECACHE
    686 	if (cache_size)
    687 		cache_flush_context();
    688 #endif
    689 
    690 	/* Unload MMU (but keep in SW segmap). */
    691 	for (i = 0, va = 0; i < NUSEG; i++, va += NBSG) {
    692 
    693 #if !defined(PMAP_DEBUG)
    694 		/* Short-cut using the S/W segmap (if !debug). */
    695 		if (pmap->pm_segmap[i] == SEGINV)
    696 			continue;
    697 #endif
    698 
    699 		/* Check the H/W segmap. */
    700 		sme = get_segmap(va);
    701 		if (sme == SEGINV)
    702 			continue;
    703 
    704 		/* Found valid PMEG in the segmap. */
    705 #ifdef	PMAP_DEBUG
    706 		if (pmap_debug & PMD_SEGMAP)
    707 			printf("pmap: set_segmap ctx=%d v=0x%lx old=0x%x "
    708 			       "new=ff (cf)\n", ctxnum, va, sme);
    709 #endif
    710 #ifdef	DIAGNOSTIC
    711 		if (sme != pmap->pm_segmap[i])
    712 			panic("context_free: unknown sme at va=0x%lx", va);
    713 #endif
    714 		/* Did cache flush above (whole context). */
    715 		set_segmap(va, SEGINV);
    716 		/* In this case, do not clear pm_segmap. */
    717 		/* XXX: Maybe inline this call? */
    718 		pmeg_release(pmeg_p(sme));
    719 	}
    720 
    721 	/* Restore previous context. */
    722 	set_context(saved_ctxnum);
    723 
    724 	/* Dequeue, update, requeue. */
    725 	TAILQ_REMOVE(&context_active_queue, contextp, context_link);
    726 	pmap->pm_ctxnum = EMPTY_CONTEXT;
    727 	contextp->context_upmap = NULL;
    728 	TAILQ_INSERT_TAIL(&context_free_queue, contextp, context_link);
    729 }
    730 
    731 
    732 /****************************************************************
    733  * PMEG management functions.
    734  */
    735 
    736 static void
    737 pmeg_init(void)
    738 {
    739 	int x;
    740 
    741 	/* clear pmeg array, put it all on the free pmeq queue */
    742 
    743 	TAILQ_INIT(&pmeg_free_queue);
    744 	TAILQ_INIT(&pmeg_inactive_queue);
    745 	TAILQ_INIT(&pmeg_active_queue);
    746 	TAILQ_INIT(&pmeg_kernel_queue);
    747 
    748 	memset(pmeg_array, 0, NPMEG*sizeof(struct pmeg_state));
    749 	for (x = 0; x < NPMEG; x++) {
    750 		TAILQ_INSERT_TAIL(&pmeg_free_queue, &pmeg_array[x], pmeg_link);
    751 		pmeg_array[x].pmeg_qstate = PMEGQ_FREE;
    752 		pmeg_array[x].pmeg_index = x;
    753 	}
    754 
    755 	/* The last pmeg is not usable. */
    756 	pmeg_reserve(SEGINV);
    757 }
    758 
    759 /*
    760  * Reserve a pmeg (forever) for use by PROM, etc.
    761  * Contents are left as-is.  Called very early...
    762  */
    763 void
    764 pmeg_reserve(int sme)
    765 {
    766 	pmeg_t pmegp;
    767 
    768 	/* Can not use pmeg_p() because it fails on SEGINV. */
    769 	pmegp = &pmeg_array[sme];
    770 
    771 	if (pmegp->pmeg_reserved) {
    772 		prom_printf("pmeg_reserve: already reserved\n");
    773 		prom_abort();
    774 	}
    775 	if (pmegp->pmeg_owner) {
    776 		prom_printf("pmeg_reserve: already owned\n");
    777 		prom_abort();
    778 	}
    779 
    780 	/* Owned by kernel, but not really usable... */
    781 	pmegp->pmeg_owner = kernel_pmap;
    782 	pmegp->pmeg_reserved++;	/* keep count, just in case */
    783 	TAILQ_REMOVE(&pmeg_free_queue, pmegp, pmeg_link);
    784 	pmegp->pmeg_qstate = PMEGQ_NONE;
    785 }
    786 
    787 /*
    788  * Examine PMEGs used by the monitor, and either
    789  * reserve them (keep=1) or clear them (keep=0)
    790  */
    791 static void
    792 pmeg_mon_init(vaddr_t sva, vaddr_t eva, int keep)
    793 {
    794 	vaddr_t pgva, endseg;
    795 	int pte, valid;
    796 	unsigned char sme;
    797 
    798 #ifdef	PMAP_DEBUG
    799 	if (pmap_debug & PMD_SEGMAP)
    800 		prom_printf("pmeg_mon_init(0x%x, 0x%x, %d)\n",
    801 			    sva, eva, keep);
    802 #endif
    803 
    804 	sva &= ~(NBSG - 1);
    805 
    806 	while (sva < eva) {
    807 		sme = get_segmap(sva);
    808 		if (sme != SEGINV) {
    809 			valid = 0;
    810 			endseg = sva + NBSG;
    811 			for (pgva = sva; pgva < endseg; pgva += PAGE_SIZE) {
    812 				pte = get_pte(pgva);
    813 				if (pte & PG_VALID) {
    814 					valid++;
    815 				}
    816 			}
    817 #ifdef	PMAP_DEBUG
    818 			if (pmap_debug & PMD_SEGMAP)
    819 				prom_printf(" sva=0x%x seg=0x%x valid=%d\n",
    820 					    sva, sme, valid);
    821 #endif
    822 			if (keep && valid)
    823 				pmeg_reserve(sme);
    824 			else
    825 				set_segmap(sva, SEGINV);
    826 		}
    827 		sva += NBSG;
    828 	}
    829 }
    830 
    831 /*
    832  * This is used only during pmap_bootstrap, so we can
    833  * get away with borrowing a user slot in the segmap.
    834  * But we use temp_seg_va because Sun-2 can't do that.
    835  */
    836 static void
    837 pmeg_clean(pmeg_t pmegp)
    838 {
    839 	int sme;
    840 	vaddr_t va;
    841 
    842 	sme = get_segmap(temp_seg_va);
    843 	if (sme != SEGINV)
    844 		panic("pmeg_clean");
    845 
    846 	sme = pmegp->pmeg_index;
    847 	set_segmap(temp_seg_va, sme);
    848 
    849 	for (va = 0; va < NBSG; va += PAGE_SIZE)
    850 		set_pte(temp_seg_va + va, PG_INVAL);
    851 
    852 	set_segmap(temp_seg_va, SEGINV);
    853 }
    854 
    855 /*
    856  * This routine makes sure that pmegs on the pmeg_free_queue contain
    857  * no valid ptes.  It pulls things off the queue, cleans them, and
    858  * puts them at the end.  The ending condition is finding the first
    859  * queue element at the head of the queue again.
    860  */
    861 static void
    862 pmeg_clean_free(void)
    863 {
    864 	pmeg_t pmegp, pmegp_first;
    865 
    866 	pmegp = TAILQ_FIRST(&pmeg_free_queue);
    867 	if (pmegp == NULL)
    868 		panic("pmap: no free pmegs available to clean");
    869 
    870 	pmegp_first = NULL;
    871 
    872 	for (;;) {
    873 		pmegp = TAILQ_FIRST(&pmeg_free_queue);
    874 		TAILQ_REMOVE(&pmeg_free_queue, pmegp, pmeg_link);
    875 
    876 		pmegp->pmeg_qstate = PMEGQ_NONE;
    877 		pmeg_clean(pmegp);
    878 		pmegp->pmeg_qstate = PMEGQ_FREE;
    879 
    880 		TAILQ_INSERT_TAIL(&pmeg_free_queue, pmegp, pmeg_link);
    881 
    882 		if (pmegp == pmegp_first)
    883 			break;
    884 		if (pmegp_first == NULL)
    885 			pmegp_first = pmegp;
    886 	}
    887 }
    888 
    889 /*
    890  * Allocate a PMEG by whatever means necessary.
    891  * (May invalidate some mappings!)
    892  */
    893 static pmeg_t
    894 pmeg_allocate(pmap_t pmap, vaddr_t va)
    895 {
    896 	pmeg_t pmegp;
    897 
    898 	CHECK_SPL();
    899 
    900 #ifdef	DIAGNOSTIC
    901 	if (va & SEGOFSET) {
    902 		panic("pmap:pmeg_allocate: va=0x%lx", va);
    903 	}
    904 #endif
    905 
    906 	/* Get one onto the free list if necessary. */
    907 	pmegp = TAILQ_FIRST(&pmeg_free_queue);
    908 	if (!pmegp) {
    909 		/* Try inactive queue... */
    910 		pmegp = TAILQ_FIRST(&pmeg_inactive_queue);
    911 		if (!pmegp) {
    912 			/* Try active queue... */
    913 			pmegp = TAILQ_FIRST(&pmeg_active_queue);
    914 		}
    915 		if (!pmegp) {
    916 			panic("pmeg_allocate: failed");
    917 		}
    918 
    919 		/*
    920 		 * Remove mappings to free-up a pmeg
    921 		 * (so it will go onto the free list).
    922 		 * XXX - Skip this one if it is wired?
    923 		 */
    924 		pmap_remove1(pmegp->pmeg_owner,
    925 			     pmegp->pmeg_va,
    926 			     pmegp->pmeg_va + NBSG);
    927 	}
    928 
    929 	/* OK, free list has something for us to take. */
    930 	pmegp = TAILQ_FIRST(&pmeg_free_queue);
    931 #ifdef	DIAGNOSTIC
    932 	if (pmegp == NULL)
    933 		panic("pmeg_allocagte: still none free?");
    934 	if ((pmegp->pmeg_qstate != PMEGQ_FREE) ||
    935 	    (pmegp->pmeg_index == SEGINV) ||
    936 	    (pmegp->pmeg_vpages))
    937 		panic("pmeg_allocate: bad pmegp=%p", pmegp);
    938 #endif
    939 #ifdef	PMAP_DEBUG
    940 	if (pmegp->pmeg_index == pmap_db_watchpmeg) {
    941 		db_printf("pmeg_allocate: watch pmegp=%p\n", pmegp);
    942 		Debugger();
    943 	}
    944 #endif
    945 
    946 	TAILQ_REMOVE(&pmeg_free_queue, pmegp, pmeg_link);
    947 
    948 	/* Reassign this PMEG for the caller. */
    949 	pmegp->pmeg_owner = pmap;
    950 	pmegp->pmeg_version = pmap->pm_version;
    951 	pmegp->pmeg_va = va;
    952 	pmegp->pmeg_wired = 0;
    953 	pmegp->pmeg_reserved  = 0;
    954 	pmegp->pmeg_vpages  = 0;
    955 	if (pmap == kernel_pmap) {
    956 		TAILQ_INSERT_TAIL(&pmeg_kernel_queue, pmegp, pmeg_link);
    957 		pmegp->pmeg_qstate = PMEGQ_KERNEL;
    958 	} else {
    959 		TAILQ_INSERT_TAIL(&pmeg_active_queue, pmegp, pmeg_link);
    960 		pmegp->pmeg_qstate = PMEGQ_ACTIVE;
    961 	}
    962 	/* Caller will verify that it's empty (if debugging). */
    963 	return pmegp;
    964 }
    965 
    966 /*
    967  * Put pmeg on the inactive queue, leaving its contents intact.
    968  * This happens when we lose our context.  We may reclaim
    969  * this pmeg later if it is still in the inactive queue.
    970  */
    971 static void
    972 pmeg_release(pmeg_t pmegp)
    973 {
    974 
    975 	CHECK_SPL();
    976 
    977 #ifdef	DIAGNOSTIC
    978 	if ((pmegp->pmeg_owner == kernel_pmap) ||
    979 	    (pmegp->pmeg_qstate != PMEGQ_ACTIVE))
    980 		panic("pmeg_release: bad pmeg=%p", pmegp);
    981 #endif
    982 
    983 	TAILQ_REMOVE(&pmeg_active_queue, pmegp, pmeg_link);
    984 	pmegp->pmeg_qstate = PMEGQ_INACTIVE;
    985 	TAILQ_INSERT_TAIL(&pmeg_inactive_queue, pmegp, pmeg_link);
    986 }
    987 
    988 /*
    989  * Move the pmeg to the free queue from wherever it is.
    990  * The pmeg will be clean.  It might be in kernel_pmap.
    991  */
    992 static void
    993 pmeg_free(pmeg_t pmegp)
    994 {
    995 
    996 	CHECK_SPL();
    997 
    998 #ifdef	DIAGNOSTIC
    999 	/* Caller should verify that it's empty. */
   1000 	if (pmegp->pmeg_vpages != 0)
   1001 		panic("pmeg_free: vpages");
   1002 #endif
   1003 
   1004 	switch (pmegp->pmeg_qstate) {
   1005 	case PMEGQ_ACTIVE:
   1006 		TAILQ_REMOVE(&pmeg_active_queue, pmegp, pmeg_link);
   1007 		break;
   1008 	case PMEGQ_INACTIVE:
   1009 		TAILQ_REMOVE(&pmeg_inactive_queue, pmegp, pmeg_link);
   1010 		break;
   1011 	case PMEGQ_KERNEL:
   1012 		TAILQ_REMOVE(&pmeg_kernel_queue, pmegp, pmeg_link);
   1013 		break;
   1014 	default:
   1015 		panic("pmeg_free: releasing bad pmeg");
   1016 		break;
   1017 	}
   1018 
   1019 #ifdef	PMAP_DEBUG
   1020 	if (pmegp->pmeg_index == pmap_db_watchpmeg) {
   1021 		db_printf("pmeg_free: watch pmeg 0x%x\n",
   1022 			  pmegp->pmeg_index);
   1023 		Debugger();
   1024 	}
   1025 #endif
   1026 
   1027 	pmegp->pmeg_owner = NULL;
   1028 	pmegp->pmeg_qstate = PMEGQ_FREE;
   1029 	TAILQ_INSERT_TAIL(&pmeg_free_queue, pmegp, pmeg_link);
   1030 }
   1031 
   1032 /*
   1033  * Find a PMEG that was put on the inactive queue when we
   1034  * had our context stolen.  If found, move to active queue.
   1035  */
   1036 static pmeg_t
   1037 pmeg_cache(pmap_t pmap, vaddr_t va)
   1038 {
   1039 	int sme, segnum;
   1040 	pmeg_t pmegp;
   1041 
   1042 	CHECK_SPL();
   1043 
   1044 #ifdef	DIAGNOSTIC
   1045 	if (pmap == kernel_pmap)
   1046 		panic("pmeg_cache: kernel_pmap");
   1047 	if (va & SEGOFSET) {
   1048 		panic("pmap:pmeg_cache: va=0x%lx", va);
   1049 	}
   1050 #endif
   1051 
   1052 	if (pmap->pm_segmap == NULL)
   1053 		return PMEG_NULL;
   1054 
   1055 	segnum = VA_SEGNUM(va);
   1056 	if (segnum > NUSEG)		/* out of range */
   1057 		return PMEG_NULL;
   1058 
   1059 	sme = pmap->pm_segmap[segnum];
   1060 	if (sme == SEGINV)	/* nothing cached */
   1061 		return PMEG_NULL;
   1062 
   1063 	pmegp = pmeg_p(sme);
   1064 
   1065 #ifdef	PMAP_DEBUG
   1066 	if (pmegp->pmeg_index == pmap_db_watchpmeg) {
   1067 		db_printf("pmeg_cache: watch pmeg 0x%x\n", pmegp->pmeg_index);
   1068 		Debugger();
   1069 	}
   1070 #endif
   1071 
   1072 	/*
   1073 	 * Our segmap named a PMEG.  If it is no longer ours,
   1074 	 * invalidate that entry in our segmap and return NULL.
   1075 	 */
   1076 	if ((pmegp->pmeg_owner != pmap) ||
   1077 	    (pmegp->pmeg_version != pmap->pm_version) ||
   1078 	    (pmegp->pmeg_va != va))
   1079 	{
   1080 #ifdef	PMAP_DEBUG
   1081 		db_printf("pmap:pmeg_cache: invalid pmeg: sme=0x%x\n", sme);
   1082 		pmeg_print(pmegp);
   1083 		Debugger();
   1084 #endif
   1085 		pmap->pm_segmap[segnum] = SEGINV;
   1086 		return PMEG_NULL; /* cache lookup failed */
   1087 	}
   1088 
   1089 #ifdef	DIAGNOSTIC
   1090 	/* Make sure it is on the inactive queue. */
   1091 	if (pmegp->pmeg_qstate != PMEGQ_INACTIVE)
   1092 		panic("pmeg_cache: pmeg was taken: %p", pmegp);
   1093 #endif
   1094 
   1095 	TAILQ_REMOVE(&pmeg_inactive_queue, pmegp, pmeg_link);
   1096 	pmegp->pmeg_qstate = PMEGQ_ACTIVE;
   1097 	TAILQ_INSERT_TAIL(&pmeg_active_queue, pmegp, pmeg_link);
   1098 
   1099 	return pmegp;
   1100 }
   1101 
   1102 #ifdef	PMAP_DEBUG
   1103 static void
   1104 pmeg_verify_empty(vaddr_t va)
   1105 {
   1106 	vaddr_t eva;
   1107 	int pte;
   1108 
   1109 	for (eva = va + NBSG;  va < eva; va += PAGE_SIZE) {
   1110 		pte = get_pte(va);
   1111 		if (pte & PG_VALID)
   1112 			panic("pmeg_verify_empty");
   1113 	}
   1114 }
   1115 #endif	/* PMAP_DEBUG */
   1116 
   1117 
   1118 /****************************************************************
   1119  * Physical-to-virtual lookup support
   1120  *
   1121  * Need memory for the pv_alloc/pv_free list heads
   1122  * and elements.  We know how many to allocate since
   1123  * there is one list head for each physical page, and
   1124  * at most one element for each PMEG slot.
   1125  */
   1126 static void
   1127 pv_init(void)
   1128 {
   1129 	int npp, nvp, sz;
   1130 	pv_entry_t pv;
   1131 	char *p;
   1132 
   1133 	/* total allocation size */
   1134 	sz = 0;
   1135 
   1136 	/*
   1137 	 * Data for each physical page.
   1138 	 * Each "mod/ref" flag is a char.
   1139 	 * Each PV head is a pointer.
   1140 	 * Note physmem is in pages.
   1141 	 */
   1142 	npp = ALIGN(physmem);
   1143 	sz += (npp * sizeof(*pv_flags_tbl));
   1144 	sz += (npp * sizeof(*pv_head_tbl));
   1145 
   1146 	/*
   1147 	 * Data for each virtual page (all PMEGs).
   1148 	 * One pv_entry for each page frame.
   1149 	 */
   1150 	nvp = NPMEG * NPAGSEG;
   1151 	sz += (nvp * sizeof(*pv_free_list));
   1152 
   1153 	/* Now allocate the whole thing. */
   1154 	sz = m68k_round_page(sz);
   1155 	p = (char *)uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED);
   1156 	if (p == NULL)
   1157 		panic("pmap:pv_init: alloc failed");
   1158 	memset(p, 0, sz);
   1159 
   1160 	/* Now divide up the space. */
   1161 	pv_flags_tbl = (void *) p;
   1162 	p += (npp * sizeof(*pv_flags_tbl));
   1163 	pv_head_tbl = (void*) p;
   1164 	p += (npp * sizeof(*pv_head_tbl));
   1165 	pv_free_list = (void *)p;
   1166 	p += (nvp * sizeof(*pv_free_list));
   1167 
   1168 	/* Finally, make pv_free_list into a list. */
   1169 	for (pv = pv_free_list; (char *)pv < p; pv++)
   1170 		pv->pv_next = &pv[1];
   1171 	pv[-1].pv_next = 0;
   1172 
   1173 	pv_initialized++;
   1174 }
   1175 
   1176 /*
   1177  * Set or clear bits in all PTEs mapping a page.
   1178  * Also does syncflags work while we are there...
   1179  */
   1180 static void
   1181 pv_changepte(paddr_t pa, int set_bits, int clear_bits)
   1182 {
   1183 	pv_entry_t *head, pv;
   1184 	u_char *pv_flags;
   1185 	pmap_t pmap;
   1186 	vaddr_t va;
   1187 	int pte, sme;
   1188 	int saved_ctx;
   1189 	bool in_ctx;
   1190 	u_int flags;
   1191 
   1192 	pv_flags = pa_to_pvflags(pa);
   1193 	head     = pa_to_pvhead(pa);
   1194 
   1195 	/* If no mappings, no work to do. */
   1196 	if (*head == NULL)
   1197 		return;
   1198 
   1199 #ifdef	DIAGNOSTIC
   1200 	/* This function should only clear these bits: */
   1201 	if (clear_bits & ~(PG_WRITE | PG_NC | PG_REF | PG_MOD))
   1202 		panic("pv_changepte: clear=0x%x", clear_bits);
   1203 #endif
   1204 
   1205 	flags = 0;
   1206 	saved_ctx = get_context();
   1207 	for (pv = *head; pv != NULL; pv = pv->pv_next) {
   1208 		pmap = pv->pv_pmap;
   1209 		va = pv->pv_va;
   1210 
   1211 #ifdef	DIAGNOSTIC
   1212 		if (pmap->pm_segmap == NULL)
   1213 			panic("pv_changepte: null segmap");
   1214 #endif
   1215 
   1216 		/* Is the PTE currently accessible in some context? */
   1217 		in_ctx = false;
   1218 		sme = SEGINV;	/* kill warning */
   1219 		if (pmap == kernel_pmap) {
   1220 			set_kernel_context();
   1221 			in_ctx = true;
   1222 		}
   1223 		else if (has_context(pmap)) {
   1224 			/* PMEG may be inactive. */
   1225 			set_context(pmap->pm_ctxnum);
   1226 			sme = get_segmap(va);
   1227 			if (sme != SEGINV)
   1228 				in_ctx = true;
   1229 		}
   1230 
   1231 		if (in_ctx == true) {
   1232 			/*
   1233 			 * The PTE is in the current context.
   1234 			 * Make sure PTE is up-to-date with VAC.
   1235 			 */
   1236 #ifdef	HAVECACHE
   1237 			if (cache_size)
   1238 				cache_flush_page(va);
   1239 #endif
   1240 			pte = get_pte(va);
   1241 		} else {
   1242 
   1243 			/*
   1244 			 * The PTE is not in any context.
   1245 			 */
   1246 
   1247 			sme = pmap->pm_segmap[VA_SEGNUM(va)];
   1248 #ifdef	DIAGNOSTIC
   1249 			if (sme == SEGINV)
   1250 				panic("pv_changepte: SEGINV");
   1251 #endif
   1252 			pte = get_pte_pmeg(sme, VA_PTE_NUM(va));
   1253 		}
   1254 
   1255 #ifdef	DIAGNOSTIC
   1256 		/* PV entries point only to valid mappings. */
   1257 		if ((pte & PG_VALID) == 0)
   1258 			panic("pv_changepte: not PG_VALID at va=0x%lx", va);
   1259 #endif
   1260 		/* Get these while it's easy. */
   1261 		if (pte & PG_MODREF) {
   1262 			flags |= (pte & PG_MODREF);
   1263 			pte &= ~PG_MODREF;
   1264 		}
   1265 
   1266 		/* Finally, set and clear some bits. */
   1267 		pte |= set_bits;
   1268 		pte &= ~clear_bits;
   1269 
   1270 		if (in_ctx == true) {
   1271 			/* Did cache flush above. */
   1272 			set_pte(va, pte);
   1273 		} else {
   1274 			set_pte_pmeg(sme, VA_PTE_NUM(va), pte);
   1275 		}
   1276 	}
   1277 	set_context(saved_ctx);
   1278 
   1279 	*pv_flags |= (flags >> PV_SHIFT);
   1280 }
   1281 
   1282 /*
   1283  * Return ref and mod bits from pvlist,
   1284  * and turns off same in hardware PTEs.
   1285  */
   1286 static u_int
   1287 pv_syncflags(pv_entry_t pv)
   1288 {
   1289 	pmap_t pmap;
   1290 	vaddr_t va;
   1291 	int pte, sme;
   1292 	int saved_ctx;
   1293 	bool in_ctx;
   1294 	u_int flags;
   1295 
   1296 	/* If no mappings, no work to do. */
   1297 	if (pv == NULL)
   1298 		return (0);
   1299 
   1300 	flags = 0;
   1301 	saved_ctx = get_context();
   1302 	for (; pv != NULL; pv = pv->pv_next) {
   1303 		pmap = pv->pv_pmap;
   1304 		va = pv->pv_va;
   1305 		sme = SEGINV;
   1306 
   1307 #ifdef	DIAGNOSTIC
   1308 		/*
   1309 		 * Only the head may have a null pmap, and
   1310 		 * we checked for that above.
   1311 		 */
   1312 		if (pmap->pm_segmap == NULL)
   1313 			panic("pv_syncflags: null segmap");
   1314 #endif
   1315 
   1316 		/* Is the PTE currently accessible in some context? */
   1317 		in_ctx = false;
   1318 		if (pmap == kernel_pmap) {
   1319 			set_kernel_context();
   1320 			in_ctx = true;
   1321 		}
   1322 		else if (has_context(pmap)) {
   1323 			/* PMEG may be inactive. */
   1324 			set_context(pmap->pm_ctxnum);
   1325 			sme = get_segmap(va);
   1326 			if (sme != SEGINV)
   1327 				in_ctx = true;
   1328 		}
   1329 
   1330 		if (in_ctx == true) {
   1331 
   1332 			/*
   1333 			 * The PTE is in the current context.
   1334 			 * Make sure PTE is up-to-date with VAC.
   1335 			 */
   1336 
   1337 #ifdef	HAVECACHE
   1338 			if (cache_size)
   1339 				cache_flush_page(va);
   1340 #endif
   1341 			pte = get_pte(va);
   1342 		} else {
   1343 
   1344 			/*
   1345 			 * The PTE is not in any context.
   1346 			 */
   1347 
   1348 			sme = pmap->pm_segmap[VA_SEGNUM(va)];
   1349 #ifdef	DIAGNOSTIC
   1350 			if (sme == SEGINV)
   1351 				panic("pv_syncflags: SEGINV");
   1352 #endif
   1353 			pte = get_pte_pmeg(sme, VA_PTE_NUM(va));
   1354 		}
   1355 
   1356 #ifdef	DIAGNOSTIC
   1357 		/* PV entries point only to valid mappings. */
   1358 		if ((pte & PG_VALID) == 0)
   1359 			panic("pv_syncflags: not PG_VALID at va=0x%lx", va);
   1360 #endif
   1361 		/* OK, do what we came here for... */
   1362 		if (pte & PG_MODREF) {
   1363 			flags |= (pte & PG_MODREF);
   1364 			pte &= ~PG_MODREF;
   1365 		}
   1366 
   1367 		if (in_ctx == true) {
   1368 			/* Did cache flush above. */
   1369 			set_pte(va, pte);
   1370 		} else {
   1371 			set_pte_pmeg(sme, VA_PTE_NUM(va), pte);
   1372 		}
   1373 	}
   1374 	set_context(saved_ctx);
   1375 
   1376 	return (flags >> PV_SHIFT);
   1377 }
   1378 
   1379 /* Remove all mappings for the physical page. */
   1380 static void
   1381 pv_remove_all(paddr_t pa)
   1382 {
   1383 	pv_entry_t *head, pv;
   1384 	pmap_t pmap;
   1385 	vaddr_t va;
   1386 
   1387 	CHECK_SPL();
   1388 
   1389 #ifdef PMAP_DEBUG
   1390 	if (pmap_debug & PMD_REMOVE)
   1391 		printf("pv_remove_all(0x%lx)\n", pa);
   1392 #endif
   1393 
   1394 	head = pa_to_pvhead(pa);
   1395 	while ((pv = *head) != NULL) {
   1396 		pmap = pv->pv_pmap;
   1397 		va   = pv->pv_va;
   1398 		pmap_remove1(pmap, va, va + PAGE_SIZE);
   1399 #ifdef PMAP_DEBUG
   1400 		/* Make sure it went away. */
   1401 		if (pv == *head) {
   1402 			db_printf("pv_remove_all: "
   1403 				  "head unchanged for pa=0x%lx\n", pa);
   1404 			Debugger();
   1405 		}
   1406 #endif
   1407 	}
   1408 }
   1409 
   1410 /*
   1411  * The pmap system is asked to lookup all mappings that point to a
   1412  * given physical memory address.  This function adds a new element
   1413  * to the list of mappings maintained for the given physical address.
   1414  * Returns PV_NC if the (new) pvlist says that the address cannot
   1415  * be cached.
   1416  */
   1417 static int
   1418 pv_link(pmap_t pmap, int pte, vaddr_t va)
   1419 {
   1420 	paddr_t pa;
   1421 	pv_entry_t *head, pv;
   1422 	u_char *pv_flags;
   1423 
   1424 	if (!pv_initialized)
   1425 		return 0;
   1426 
   1427 	CHECK_SPL();
   1428 
   1429 	pa = PG_PA(pte);
   1430 
   1431 #ifdef PMAP_DEBUG
   1432 	if ((pmap_debug & PMD_LINK) || (va == pmap_db_watchva)) {
   1433 		printf("pv_link(%p, 0x%x, 0x%lx)\n", pmap, pte, va);
   1434 		/* pv_print(pa); */
   1435 	}
   1436 #endif
   1437 
   1438 	pv_flags = pa_to_pvflags(pa);
   1439 	head     = pa_to_pvhead(pa);
   1440 
   1441 #ifdef	DIAGNOSTIC
   1442 	/* See if this mapping is already in the list. */
   1443 	for (pv = *head; pv != NULL; pv = pv->pv_next) {
   1444 		if ((pv->pv_pmap == pmap) && (pv->pv_va == va))
   1445 			panic("pv_link: duplicate entry for PA=0x%lx", pa);
   1446 	}
   1447 #endif
   1448 
   1449 	int flags = (pte & (PG_NC | PG_MODREF)) >> PV_SHIFT;
   1450 	*pv_flags |= flags;
   1451 
   1452 #ifdef HAVECACHE
   1453 	/*
   1454 	 * Does this new mapping cause VAC alias problems?
   1455 	 */
   1456 	if ((*pv_flags & PV_NC) == 0) {
   1457 		for (pv = *head; pv != NULL; pv = pv->pv_next) {
   1458 			if (BADALIAS(va, pv->pv_va)) {
   1459 				*pv_flags |= PV_NC;
   1460 				pv_changepte(pa, PG_NC, 0);
   1461 				pmap_stats.ps_vac_uncached++;
   1462 				break;
   1463 			}
   1464 		}
   1465 	}
   1466 #endif
   1467 
   1468 	/* Allocate a PV element (pv_alloc()). */
   1469 	pv = pv_free_list;
   1470 	if (pv == NULL)
   1471 		panic("pv_link: pv_alloc");
   1472 	pv_free_list = pv->pv_next;
   1473 	pv->pv_next = 0;
   1474 
   1475 	/* Insert new entry at the head. */
   1476 	pv->pv_pmap = pmap;
   1477 	pv->pv_va   = va;
   1478 	pv->pv_next = *head;
   1479 	*head = pv;
   1480 
   1481 	return (*pv_flags & PV_NC);
   1482 }
   1483 
   1484 /*
   1485  * pv_unlink is a helper function for pmap_remove.
   1486  * It removes the appropriate (pmap, pa, va) entry.
   1487  *
   1488  * Once the entry is removed, if the pv_table head has the cache
   1489  * inhibit bit set, see if we can turn that off; if so, walk the
   1490  * pvlist and turn off PG_NC in each PTE.  (The pvlist is by
   1491  * definition nonempty, since it must have at least two elements
   1492  * in it to have PV_NC set, and we only remove one here.)
   1493  */
   1494 static void
   1495 pv_unlink(pmap_t pmap, int pte, vaddr_t va)
   1496 {
   1497 	paddr_t pa;
   1498 	pv_entry_t *head, *ppv, pv;
   1499 	u_char *pv_flags;
   1500 
   1501 	CHECK_SPL();
   1502 
   1503 	pa = PG_PA(pte);
   1504 #ifdef PMAP_DEBUG
   1505 	if ((pmap_debug & PMD_LINK) || (va == pmap_db_watchva)) {
   1506 		printf("pv_unlink(%p, 0x%x, 0x%lx)\n", pmap, pte, va);
   1507 		/* pv_print(pa); */
   1508 	}
   1509 #endif
   1510 
   1511 	pv_flags = pa_to_pvflags(pa);
   1512 	head     = pa_to_pvhead(pa);
   1513 
   1514 	/*
   1515 	 * Find the entry.
   1516 	 */
   1517 	ppv = head;
   1518 	pv = *ppv;
   1519 	while (pv) {
   1520 		if ((pv->pv_pmap == pmap) && (pv->pv_va == va))
   1521 			goto found;
   1522 		ppv = &pv->pv_next;
   1523 		pv  =  pv->pv_next;
   1524 	}
   1525 #ifdef PMAP_DEBUG
   1526 	db_printf("pv_unlink: not found (pa=0x%lx,va=0x%lx)\n", pa, va);
   1527 	Debugger();
   1528 #endif
   1529 	return;
   1530 
   1531   found:
   1532 	/* Unlink this entry from the list and clear it. */
   1533 	*ppv = pv->pv_next;
   1534 	pv->pv_pmap = NULL;
   1535 	pv->pv_va   = 0;
   1536 
   1537 	/* Insert it on the head of the free list. (pv_free()) */
   1538 	pv->pv_next = pv_free_list;
   1539 	pv_free_list = pv;
   1540 	pv = NULL;
   1541 
   1542 	/* Do any non-cached mappings remain? */
   1543 	if ((*pv_flags & PV_NC) == 0)
   1544 		return;
   1545 	if ((pv = *head) == NULL)
   1546 		return;
   1547 
   1548 	/*
   1549 	 * Have non-cached mappings.  See if we can fix that now.
   1550 	 */
   1551 	va = pv->pv_va;
   1552 	for (pv = pv->pv_next; pv != NULL; pv = pv->pv_next) {
   1553 		/* If there is a DVMA mapping, leave it NC. */
   1554 		if (va >= DVMA_MAP_BASE)
   1555 			return;
   1556 		/* If there are VAC alias problems, leave NC. */
   1557 		if (BADALIAS(va, pv->pv_va))
   1558 			return;
   1559 	}
   1560 	/* OK, there are no "problem" mappings. */
   1561 	*pv_flags &= ~PV_NC;
   1562 	pv_changepte(pa, 0, PG_NC);
   1563 	pmap_stats.ps_vac_recached++;
   1564 }
   1565 
   1566 
   1567 /****************************************************************
   1568  * Bootstrap and Initialization, etc.
   1569  */
   1570 
   1571 void
   1572 pmap_common_init(pmap_t pmap)
   1573 {
   1574 	memset(pmap, 0, sizeof(struct pmap));
   1575 	pmap->pm_refcount = 1;
   1576 	pmap->pm_version = pmap_version++;
   1577 	pmap->pm_ctxnum = EMPTY_CONTEXT;
   1578 }
   1579 
   1580 /*
   1581  * Prepare the kernel for VM operations.
   1582  * This is called by locore2.c:_vm_init()
   1583  * after the "start/end" globals are set.
   1584  * This function must NOT leave context zero.
   1585  */
   1586 paddr_t __attribute__((no_instrument_function))
   1587 pmap_bootstrap(vaddr_t nextva)
   1588 {
   1589 	vaddr_t va, eva;
   1590 	int i, pte, sme;
   1591 	extern char etext[];
   1592 	extern void *msgbufaddr;
   1593 	paddr_t nextpa;
   1594 
   1595 	nextva = m68k_round_page(nextva);
   1596 
   1597 	/* Steal some special-purpose, already mapped pages? */
   1598 
   1599 	/*
   1600 	 * Determine the range of kernel virtual space available.
   1601 	 * It is segment-aligned to simplify PMEG management.
   1602 	 */
   1603 	virtual_avail = sun_round_seg(nextva);
   1604 	virtual_end = VM_MAX_KERNEL_ADDRESS;
   1605 
   1606 	/*
   1607 	 * Determine the range of physical memory available.
   1608 	 */
   1609 	phys_seg_list[0].ps_start = 0;
   1610 
   1611 #if IS_SUN2
   1612 	nextpa = nextva;
   1613 
   1614 	phys_seg_list[0].ps_end = m68k_trunc_page(prom_memsize());
   1615 #else
   1616 	/* Physical memory at zero was remapped to KERNBASE. */
   1617 	nextpa = nextva - KERNBASE3;
   1618 
   1619 	struct sunromvec *rvec = romVectorPtr;
   1620 	if (rvec->romvecVersion < 1) {
   1621 		prom_printf("Warning: ancient PROM version=%d\n",
   1622 			   rvec->romvecVersion);
   1623 		/* Guess that PROM version 0.X used two pages. */
   1624 		phys_seg_list[0].ps_end = *rvec->memorySize - (2*PAGE_SIZE);
   1625 	} else {
   1626 		/* PROM version 1 or later. */
   1627 		phys_seg_list[0].ps_end = *rvec->memoryAvail;
   1628 	}
   1629 #endif /* IS_SUN2 */
   1630 
   1631 	/*
   1632 	 * Report the actual amount of physical memory,
   1633 	 * even though the PROM takes a few pages.
   1634 	 */
   1635 	physmem = (btoc(phys_seg_list[0].ps_end) + 0xF) & ~0xF;
   1636 
   1637 #if IS_SUN3
   1638 	/*
   1639 	 * On the Sun3/50, the video frame buffer is located at
   1640 	 * physical address 1MB so we must step over it.
   1641 	 */
   1642 	if (cpu_machine_id == ID_SUN3_50) {
   1643 		hole_start = m68k_trunc_page(OBMEM_BW50_ADDR);
   1644 		hole_size  = m68k_round_page(OBMEM_BW2_SIZE);
   1645 		if (nextpa > hole_start) {
   1646 			prom_printf("kernel too large for Sun3/50\n");
   1647 			prom_abort();
   1648 		}
   1649 		phys_seg_list[1].ps_start = hole_start + hole_size;
   1650 		phys_seg_list[1].ps_end = phys_seg_list[0].ps_end;
   1651 		phys_seg_list[0].ps_end = hole_start;
   1652 	}
   1653 #endif /* IS_SUN3 */
   1654 
   1655 	/*
   1656 	 * Done allocating PAGES of virtual space, so
   1657 	 * clean out the rest of the last used segment.
   1658 	 */
   1659 	for (va = nextva; va < virtual_avail; va += PAGE_SIZE)
   1660 		set_pte(va, PG_INVAL);
   1661 
   1662 	/*
   1663 	 * Now that we are done stealing physical pages, etc.
   1664 	 * figure out which PMEGs are used by those mappings
   1665 	 * and either reserve them or clear them out.
   1666 	 * -- but first, init PMEG management.
   1667 	 * This puts all PMEGs in the free list.
   1668 	 * We will allocate the in-use ones.
   1669 	 */
   1670 	pmeg_init();
   1671 
   1672 #if IS_SUN2
   1673 	va = KERNBASE;
   1674 #else
   1675 	/*
   1676 	 * Unmap user virtual segments.
   1677 	 * VA range: [0 .. KERNBASE]
   1678 	 */
   1679 	for (va = 0; va < KERNBASE3; va += NBSG)
   1680 		set_segmap(va, SEGINV);
   1681 #endif /* IS_SUN2 */
   1682 
   1683 	/*
   1684 	 * Reserve PMEGS for kernel text/data/bss
   1685 	 * and the misc pages taken above.
   1686 	 * VA range: [KERNBASE .. virtual_avail]
   1687 	 */
   1688 	for ( ; va < virtual_avail; va += NBSG) {
   1689 		sme = get_segmap(va);
   1690 		if (sme == SEGINV) {
   1691 			prom_printf("kernel text/data/bss not mapped\n");
   1692 			prom_abort();
   1693 		}
   1694 		pmeg_reserve(sme);
   1695 	}
   1696 
   1697 	/*
   1698 	 * Unmap kernel virtual space.  Make sure to leave no valid
   1699 	 * segmap entries in the MMU unless pmeg_array records them.
   1700 	 * VA range: [vseg_avail .. virtual_end]
   1701 	 */
   1702 	for ( ; va < virtual_end; va += NBSG)
   1703 		set_segmap(va, SEGINV);
   1704 
   1705 #if IS_SUN2
   1706 	/*
   1707 	 * Reserve PMEGs used by the PROM monitor (device mappings).
   1708 	 * Free up any pmegs in this range which have no mappings.
   1709 	 * VA range: [0x00E00000 .. 0x00F00000]
   1710 	 */
   1711 	pmeg_mon_init(SUN2_MONSTART, SUN2_MONEND, true);
   1712 
   1713 	/*
   1714 	 * Unmap any pmegs left in DVMA space by the PROM.
   1715 	 * DO NOT kill the last one! (owned by the PROM!)
   1716 	 * VA range: [0x00F00000 .. 0x00FE0000]
   1717 	 */
   1718 	pmeg_mon_init(SUN2_MONEND, SUN2_MONEND + DVMA_MAP_SIZE, false);
   1719 #else /* IS_SUN3 */
   1720 	/*
   1721 	 * Reserve PMEGs used by the PROM monitor (device mappings).
   1722 	 * Free up any pmegs in this range which have no mappings.
   1723 	 * VA range: [0x0FE00000 .. 0x0FF00000]
   1724 	 */
   1725 	pmeg_mon_init(SUN3_MONSTART, SUN3_MONEND, true);
   1726 
   1727 	/*
   1728 	 * Unmap any pmegs left in DVMA space by the PROM.
   1729 	 * DO NOT kill the last one! (owned by the PROM!)
   1730 	 * VA range: [0x0FF00000 .. 0x0FFE0000]
   1731 	 */
   1732 	pmeg_mon_init(SUN3_MONEND, SUN3_MONSHORTSEG, false);
   1733 
   1734 	/*
   1735 	 * MONSHORTSEG contains MONSHORTPAGE which is a data page
   1736 	 * allocated by the PROM monitor.  Reserve the segment,
   1737 	 * but clear out all but the last PTE inside it.
   1738 	 * Note we use this for tmp_vpages.
   1739 	 */
   1740 	cpu_info_primary.ci_tmpva_src = SUN3_MONSHORTSEG;
   1741 	cpu_info_primary.ci_tmpva_dst = SUN3_MONSHORTSEG + PAGE_SIZE;
   1742 	va  = SUN3_MONSHORTSEG;
   1743 	eva = SUN3_MONSHORTPAGE;
   1744 	sme = get_segmap(va);
   1745 	pmeg_reserve(sme);
   1746 	for ( ; va < eva; va += PAGE_SIZE)
   1747 		set_pte(va, PG_INVAL);
   1748 #endif /* IS_SUN2 */
   1749 
   1750 	/*
   1751 	 * Done reserving PMEGs and/or clearing out mappings.
   1752 	 *
   1753 	 * Now verify the mapping protections and such for the
   1754 	 * important parts of the address space (in VA order).
   1755 	 * Note that the Sun PROM usually leaves the memory
   1756 	 * mapped with everything non-cached...
   1757 	 */
   1758 
   1759 #if IS_SUN2
   1760 	/*
   1761 	 * On a Sun2, the boot loader loads the kernel exactly where
   1762 	 * it is linked, at physical/virtual 0x6000 (KERNBASE).  This
   1763 	 * means there are twelve physical/virtual pages before the
   1764 	 * kernel text begins.
   1765 	 */
   1766 	va = 0;
   1767 
   1768 	/*
   1769 	 * Physical/virtual pages zero through three are used by the
   1770 	 * PROM.  prom_init has already saved the PTEs, but we don't
   1771 	 * want to unmap the pages until we've installed our own
   1772 	 * vector table - just in case something happens before then
   1773 	 * and we drop into the PROM.
   1774 	 */
   1775 	eva = va + PAGE_SIZE * 4;
   1776 	va = eva;
   1777 
   1778 	/*
   1779 	 * We use pages four through seven for the msgbuf.
   1780 	 */
   1781 	eva = va + PAGE_SIZE * 4;
   1782 	msgbufaddr = (void *)va;
   1783 	for (; va < eva; va += PAGE_SIZE) {
   1784 		pte = get_pte(va);
   1785 		pte |= (PG_SYSTEM | PG_WRITE | PG_NC);
   1786 		set_pte(va, pte);
   1787 	}
   1788 
   1789 	/*
   1790 	 * On the Sun3, two of the three dead pages in SUN3_MONSHORTSEG
   1791 	 * are used for tmp_vpages.  The Sun2 doesn't have this
   1792 	 * short-segment concept, so we reserve virtual pages eight
   1793 	 * and nine for this.
   1794 	 */
   1795 	cpu_info_primary.ci_tmpva_src = va;
   1796 	set_pte(va, PG_INVAL);
   1797 	va += PAGE_SIZE;
   1798 	cpu_info_primary.ci_tmpva_dst = va;
   1799 	set_pte(va, PG_INVAL);
   1800 	va += PAGE_SIZE;
   1801 
   1802 	/*
   1803 	 * Pages ten and eleven remain for the temporary kernel stack,
   1804 	 * which is set up by locore.s.  Hopefully this is enough space.
   1805 	 */
   1806 	eva = va + PAGE_SIZE * 2;
   1807 	for (; va < eva ; va += PAGE_SIZE) {
   1808 		pte = get_pte(va);
   1809 		pte &= ~(PG_NC);
   1810 		pte |= (PG_SYSTEM | PG_WRITE);
   1811 		set_pte(va, pte);
   1812 	}
   1813 #else /* IS_SUN3 */
   1814 	/*
   1815 	 * Map the message buffer page at a constant location
   1816 	 * (physical address zero) so its contents will be
   1817 	 * preserved through a reboot.
   1818 	 */
   1819 	va = KERNBASE3;
   1820 	msgbufaddr = (void *)va;
   1821 	pte = get_pte(va);
   1822 	pte |= (PG_SYSTEM | PG_WRITE | PG_NC);
   1823 	set_pte(va, pte);
   1824 	va += PAGE_SIZE;
   1825 
   1826 	/* Next is the tmpstack page. */
   1827 	pte = get_pte(va);
   1828 	pte &= ~(PG_NC);
   1829 	pte |= (PG_SYSTEM | PG_WRITE);
   1830 	set_pte(va, pte);
   1831 	va += PAGE_SIZE;
   1832 #endif /* IS_SUN2 */
   1833 
   1834 	/*
   1835 	 * Next is the kernel text.
   1836 	 *
   1837 	 * Verify protection bits on kernel text/data/bss
   1838 	 * All of kernel text, data, and bss are cached.
   1839 	 * Text is read-only (except in db_write_ktext).
   1840 	 */
   1841 	eva = m68k_trunc_page(etext);
   1842 	while (va < eva) {
   1843 		pte = get_pte(va);
   1844 		if ((pte & (PG_VALID|PG_TYPE)) != PG_VALID) {
   1845 			prom_printf("invalid page at 0x%x\n", va);
   1846 		}
   1847 		pte &= ~(PG_WRITE|PG_NC);
   1848 		/* Kernel text is read-only */
   1849 		pte |= (PG_SYSTEM);
   1850 		set_pte(va, pte);
   1851 		va += PAGE_SIZE;
   1852 	}
   1853 	/* data, bss, etc. */
   1854 	while (va < nextva) {
   1855 		pte = get_pte(va);
   1856 		if ((pte & (PG_VALID|PG_TYPE)) != PG_VALID) {
   1857 			prom_printf("invalid page at 0x%x\n", va);
   1858 		}
   1859 		pte &= ~(PG_NC);
   1860 		pte |= (PG_SYSTEM | PG_WRITE);
   1861 		set_pte(va, pte);
   1862 		va += PAGE_SIZE;
   1863 	}
   1864 
   1865 #ifdef DIAGNOSTIC
   1866 	/*
   1867 	 * Sanity checks for context initialization below...
   1868 	 */
   1869 #if IS_SUN3
   1870 	/* Note: PROM setcxsegmap function needs sfc=dfc=FC_CONTROL */
   1871 	if ((getsfc() != FC_CONTROL) || (getdfc() != FC_CONTROL)) {
   1872 		prom_printf("pmap_bootstrap: bad dfc or sfc\n");
   1873 		prom_abort();
   1874 	}
   1875 #endif
   1876 	/* Near the beginning of locore.s we set context zero. */
   1877 	if (get_context() != 0) {
   1878 		prom_printf("pmap_bootstrap: not in context zero?\n");
   1879 		prom_abort();
   1880 	}
   1881 #endif /* DIAGNOSTIC */
   1882 
   1883 #if IS_SUN2
   1884 	/*
   1885 	 * Initialize all of the other contexts.
   1886 	 */
   1887 	for (va = 0; va < (vaddr_t) (NBSG * NSEGMAP); va += NBSG) {
   1888 		for (i = 1; i < NCONTEXT; i++) {
   1889 			set_context(i);
   1890 			set_segmap(va, SEGINV);
   1891 		}
   1892 	}
   1893 	set_context(KERNEL_CONTEXT);
   1894 #else /* IS_SUN3 */
   1895 	/*
   1896 	 * Duplicate all mappings in the current context into
   1897 	 * every other context.  We have to let the PROM do the
   1898 	 * actual segmap manipulation because we can only switch
   1899 	 * the MMU context after we are sure that the kernel is
   1900 	 * identically mapped in all contexts.  The PROM can do
   1901 	 * the job using hardware-dependent tricks...
   1902 	 */
   1903 	for (va = 0; va < (vaddr_t) (NBSG * NSEGMAP); va += NBSG) {
   1904 		/* Read the segmap entry from context zero... */
   1905 		sme = get_segmap(va);
   1906 		/* ... then copy it into all other contexts. */
   1907 		for (i = 1; i < NCONTEXT; i++) {
   1908 			(*rvec->setcxsegmap)(i, va, sme);
   1909 		}
   1910 	}
   1911 #endif /* IS_SUN2 */
   1912 
   1913 	/*
   1914 	 * Reserve a segment for the kernel to use to access a pmeg
   1915 	 * that is not currently mapped into any context/segmap.
   1916 	 * The kernel temporarily maps such a pmeg into this segment.
   1917 	 *
   1918 	 * XXX SUN3: Now that context zero is reserved as kernel-only,
   1919 	 * we could borrow context zero for these temporary uses.
   1920 	 */
   1921 	temp_seg_va = virtual_avail;
   1922 	virtual_avail += NBSG;
   1923 #ifdef	DIAGNOSTIC
   1924 	if (temp_seg_va & SEGOFSET) {
   1925 		prom_printf("pmap_bootstrap: temp_seg_va\n");
   1926 		prom_abort();
   1927 	}
   1928 #endif
   1929 
   1930 	uvmexp.pagesize = PAGE_SIZE;
   1931 	uvm_md_init();
   1932 
   1933 	/* after setting up some structures */
   1934 
   1935 	pmap_common_init(kernel_pmap);
   1936 	pmap_kernel_init(kernel_pmap);
   1937 
   1938 	context_init();
   1939 
   1940 	pmeg_clean_free();
   1941 
   1942 	return nextpa;
   1943 }
   1944 
   1945 /*
   1946  * Give the kernel pmap a segmap, just so there are not
   1947  * so many special cases required.  Maybe faster too,
   1948  * because this lets pmap_remove() and pmap_protect()
   1949  * use a S/W copy of the segmap to avoid function calls.
   1950  */
   1951 void
   1952 pmap_kernel_init(pmap_t pmap)
   1953 {
   1954 	vaddr_t va;
   1955 	int i, sme;
   1956 
   1957 	for (i=0, va=0; i < NSEGMAP; i++, va+=NBSG) {
   1958 		sme = get_segmap(va);
   1959 		kernel_segmap[i] = sme;
   1960 	}
   1961 	pmap->pm_segmap = kernel_segmap;
   1962 }
   1963 
   1964 
   1965 /****************************************************************
   1966  * PMAP interface functions.
   1967  */
   1968 
   1969 /*
   1970  * Support functions for vm_page_bootstrap().
   1971  */
   1972 
   1973 /*
   1974  * How much virtual space does this kernel have?
   1975  * (After mapping kernel text, data, etc.)
   1976  */
   1977 void
   1978 pmap_virtual_space(vaddr_t *v_start, vaddr_t *v_end)
   1979 {
   1980 	*v_start = virtual_avail;
   1981 	*v_end   = virtual_end;
   1982 }
   1983 
   1984 /*
   1985  *	Initialize the pmap module.
   1986  *	Called by vm_init, to initialize any structures that the pmap
   1987  *	system needs to map virtual memory.
   1988  */
   1989 void
   1990 pmap_init(void)
   1991 {
   1992 	pv_init();
   1993 
   1994 	/* Initialize the pmap pool. */
   1995 	pool_init(&pmap_pmap_pool, sizeof(struct pmap), 0, 0, 0, "pmappl",
   1996 		  &pool_allocator_nointr, IPL_NONE);
   1997 }
   1998 
   1999 /*
   2000  * Map a range of kernel virtual address space.
   2001  * This might be used for device mappings, or to
   2002  * record the mapping for kernel text/data/bss.
   2003  * Return VA following the mapped range.
   2004  */
   2005 vaddr_t
   2006 pmap_map(vaddr_t va, paddr_t pa, paddr_t endpa, int prot)
   2007 {
   2008 	int sz;
   2009 
   2010 	sz = endpa - pa;
   2011 	do {
   2012 		pmap_enter(kernel_pmap, va, pa, prot, 0);
   2013 		va += PAGE_SIZE;
   2014 		pa += PAGE_SIZE;
   2015 		sz -= PAGE_SIZE;
   2016 	} while (sz > 0);
   2017 	pmap_update(kernel_pmap);
   2018 	return(va);
   2019 }
   2020 
   2021 void
   2022 pmap_user_init(pmap_t pmap)
   2023 {
   2024 	int i;
   2025 	pmap->pm_segmap = kmem_alloc(sizeof(char)*NUSEG, KM_SLEEP);
   2026 	for (i = 0; i < NUSEG; i++) {
   2027 		pmap->pm_segmap[i] = SEGINV;
   2028 	}
   2029 }
   2030 
   2031 /*
   2032  *	Create and return a physical map.
   2033  *
   2034  *	If the size specified for the map
   2035  *	is zero, the map is an actual physical
   2036  *	map, and may be referenced by the
   2037  *	hardware.
   2038  *
   2039  *	If the size specified is non-zero,
   2040  *	the map will be used in software only, and
   2041  *	is bounded by that size.
   2042  */
   2043 pmap_t
   2044 pmap_create(void)
   2045 {
   2046 	pmap_t pmap;
   2047 
   2048 	pmap = pool_get(&pmap_pmap_pool, PR_WAITOK);
   2049 	pmap_pinit(pmap);
   2050 	return pmap;
   2051 }
   2052 
   2053 /*
   2054  * Release any resources held by the given physical map.
   2055  * Called when a pmap initialized by pmap_pinit is being released.
   2056  * Should only be called if the map contains no valid mappings.
   2057  */
   2058 void
   2059 pmap_release(struct pmap *pmap)
   2060 {
   2061 	int s;
   2062 
   2063 	s = splvm();
   2064 
   2065 	if (pmap == kernel_pmap)
   2066 		panic("pmap_release: kernel_pmap!");
   2067 
   2068 	if (has_context(pmap)) {
   2069 #ifdef	PMAP_DEBUG
   2070 		if (pmap_debug & PMD_CONTEXT)
   2071 			printf("pmap_release(%p): free ctx %d\n",
   2072 			       pmap, pmap->pm_ctxnum);
   2073 #endif
   2074 		context_free(pmap);
   2075 	}
   2076 	kmem_free(pmap->pm_segmap, sizeof(char)*NUSEG);
   2077 	pmap->pm_segmap = NULL;
   2078 
   2079 	splx(s);
   2080 }
   2081 
   2082 
   2083 /*
   2084  *	Retire the given physical map from service.
   2085  *	Should only be called if the map contains
   2086  *	no valid mappings.
   2087  */
   2088 void
   2089 pmap_destroy(pmap_t pmap)
   2090 {
   2091 	int count;
   2092 
   2093 #ifdef PMAP_DEBUG
   2094 	if (pmap_debug & PMD_CREATE)
   2095 		printf("pmap_destroy(%p)\n", pmap);
   2096 #endif
   2097 	if (pmap == kernel_pmap)
   2098 		panic("pmap_destroy: kernel_pmap!");
   2099 	count = atomic_dec_uint_nv(&pmap->pm_refcount);
   2100 	if (count == 0) {
   2101 		pmap_release(pmap);
   2102 		pool_put(&pmap_pmap_pool, pmap);
   2103 	}
   2104 }
   2105 
   2106 /*
   2107  *	Add a reference to the specified pmap.
   2108  */
   2109 void
   2110 pmap_reference(pmap_t pmap)
   2111 {
   2112 
   2113 	atomic_inc_uint(&pmap->pm_refcount);
   2114 }
   2115 
   2116 
   2117 /*
   2118  *	Insert the given physical page (p) at
   2119  *	the specified virtual address (v) in the
   2120  *	target physical map with the protection requested.
   2121  *
   2122  *	The physical address is page aligned, but may have some
   2123  *	low bits set indicating an OBIO or VME bus page, or just
   2124  *	that the non-cache bit should be set (i.e PMAP_NC).
   2125  *
   2126  *	If specified, the page will be wired down, meaning
   2127  *	that the related pte can not be reclaimed.
   2128  *
   2129  *	NB:  This is the only routine which MAY NOT lazy-evaluate
   2130  *	or lose information.  That is, this routine must actually
   2131  *	insert this page into the given map NOW.
   2132  */
   2133 int
   2134 pmap_enter(pmap_t pmap, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
   2135 {
   2136 	int new_pte, s;
   2137 	bool wired = (flags & PMAP_WIRED) != 0;
   2138 
   2139 #ifdef	PMAP_DEBUG
   2140 	if ((pmap_debug & PMD_ENTER) ||
   2141 	    (va == pmap_db_watchva))
   2142 		printf("pmap_enter(%p, 0x%lx, 0x%lx, 0x%x, 0x%x)\n",
   2143 		       pmap, va, pa, prot, wired);
   2144 #endif
   2145 
   2146 	/* Get page-type bits from low part of the PA... */
   2147 	new_pte = (pa & PMAP_SPEC) << PG_MOD_SHIFT;
   2148 
   2149 	/* ...now the valid and writable bits... */
   2150 	new_pte |= PG_VALID;
   2151 	if (prot & VM_PROT_WRITE)
   2152 		new_pte |= PG_WRITE;
   2153 	if (flags & VM_PROT_ALL) {
   2154 		new_pte |= PG_REF;
   2155 		if (flags & VM_PROT_WRITE) {
   2156 			new_pte |= PG_MOD;
   2157 		}
   2158 	}
   2159 
   2160 	/* ...and finally the page-frame number. */
   2161 	new_pte |= PA_PGNUM(pa);
   2162 
   2163 	/*
   2164 	 * treatment varies significantly:
   2165 	 *  kernel ptes are always in the mmu
   2166 	 *  user ptes may not necessarily? be in the mmu.  pmap may not
   2167 	 *   be in the mmu either.
   2168 	 */
   2169 	s = splvm();
   2170 	if (pmap == kernel_pmap) {
   2171 		new_pte |= PG_SYSTEM;
   2172 		pmap_enter_kernel(va, new_pte, wired);
   2173 	} else {
   2174 		pmap_enter_user(pmap, va, new_pte, wired);
   2175 	}
   2176 	splx(s);
   2177 	return 0;
   2178 }
   2179 
   2180 static void
   2181 pmap_enter_kernel(vaddr_t pgva, int new_pte, bool wired)
   2182 {
   2183 	pmap_t pmap = kernel_pmap;
   2184 	pmeg_t pmegp;
   2185 	int do_pv, old_pte, sme;
   2186 	vaddr_t segva;
   2187 
   2188 	/*
   2189 	 * Kernel mappings are kept in hardware only.  On Sun-2, it's
   2190 	 * in context 0, on Sun-3, it's in all contexts.  The only
   2191 	 * thing we have to handle here is possibly allocating additional
   2192 	 * PMEGs and inserting them into the appropriate contexts.
   2193 	 */
   2194 	kernel_context_enter();
   2195 
   2196 	/*
   2197 	 * In detail:
   2198 	 *
   2199 	 * (a) lock pmap
   2200 	 * (b) Is the VA in a already mapped segment, if so
   2201 	 *	 look to see if that VA address is "valid".  If it is, then
   2202 	 *	 action is a change to an existing pte
   2203 	 * (c) if not mapped segment, need to allocate pmeg
   2204 	 * (d) if adding pte entry or changing physaddr of existing one,
   2205 	 *		use pv_stuff, for change, pmap_remove() possibly.
   2206 	 * (e) change/add pte
   2207 	 */
   2208 
   2209 #ifdef	DIAGNOSTIC
   2210 	if ((pgva < virtual_avail) || (pgva >= DVMA_MAP_END))
   2211 		panic("pmap_enter_kernel: bad va=0x%lx", pgva);
   2212 	if ((new_pte & (PG_VALID | PG_SYSTEM)) != (PG_VALID | PG_SYSTEM))
   2213 		panic("pmap_enter_kernel: bad pte");
   2214 #endif
   2215 
   2216 	if (pgva >= DVMA_MAP_BASE) {
   2217 		/* This is DVMA space.  Always want it non-cached. */
   2218 		new_pte |= PG_NC;
   2219 	}
   2220 
   2221 	segva = sun_trunc_seg(pgva);
   2222 	do_pv = true;
   2223 
   2224 	/* Do we have a PMEG? */
   2225 	sme = get_segmap(segva);
   2226 	if (sme != SEGINV) {
   2227 		/* Found a PMEG in the segmap.  Cool. */
   2228 		pmegp = pmeg_p(sme);
   2229 #ifdef	DIAGNOSTIC
   2230 		/* Make sure it is the right PMEG. */
   2231 		if (sme != pmap->pm_segmap[VA_SEGNUM(segva)])
   2232 			panic("pmap_enter_kernel: wrong sme at VA=0x%lx",
   2233 			      segva);
   2234 		/* Make sure it is ours. */
   2235 		if (pmegp->pmeg_owner != pmap)
   2236 			panic("pmap_enter_kernel: MMU has bad pmeg 0x%x", sme);
   2237 #endif
   2238 	} else {
   2239 		/* No PMEG in the segmap.  Have to allocate one. */
   2240 		pmegp = pmeg_allocate(pmap, segva);
   2241 		sme = pmegp->pmeg_index;
   2242 		pmap->pm_segmap[VA_SEGNUM(segva)] = sme;
   2243 		set_kernel_segmap(segva, sme);
   2244 #ifdef	PMAP_DEBUG
   2245 		pmeg_verify_empty(segva);
   2246 		if (pmap_debug & PMD_SEGMAP) {
   2247 			printf("pmap: set_segmap pmap=%p va=0x%lx sme=0x%x "
   2248 			       "(ek)\n", pmap, segva, sme);
   2249 		}
   2250 #endif
   2251 		/* There are no existing mappings to deal with. */
   2252 		old_pte = 0;
   2253 		goto add_pte;
   2254 	}
   2255 
   2256 	/*
   2257 	 * We have a PMEG.  Is the VA already mapped to somewhere?
   2258 	 *	(a) if so, is it same pa? (really a protection change)
   2259 	 *	(b) if not same pa, then we have to unlink from old pa
   2260 	 */
   2261 	old_pte = get_pte(pgva);
   2262 	if ((old_pte & PG_VALID) == 0)
   2263 		goto add_pte;
   2264 
   2265 	/* Have valid translation.  Flush cache before changing it. */
   2266 #ifdef	HAVECACHE
   2267 	if (cache_size) {
   2268 		cache_flush_page(pgva);
   2269 		/* Get fresh mod/ref bits from write-back. */
   2270 		old_pte = get_pte(pgva);
   2271 	}
   2272 #endif
   2273 
   2274 	/* XXX - removing valid page here, way lame... -glass */
   2275 	pmegp->pmeg_vpages--;
   2276 
   2277 	if (!IS_MAIN_MEM(old_pte)) {
   2278 		/* Was not main memory, so no pv_entry for it. */
   2279 		goto add_pte;
   2280 	}
   2281 
   2282 	/* Old mapping was main memory.  Save mod/ref bits. */
   2283 	save_modref_bits(old_pte);
   2284 
   2285 	/*
   2286 	 * If not changing the type or pfnum then re-use pv_entry.
   2287 	 * Note we get here only with old_pte having PGT_OBMEM.
   2288 	 */
   2289 	if ((old_pte & (PG_TYPE|PG_FRAME)) == (new_pte & (PG_TYPE|PG_FRAME))) {
   2290 		do_pv = false;		/* re-use pv_entry */
   2291 		new_pte |= (old_pte & PG_NC);
   2292 		goto add_pte;
   2293 	}
   2294 
   2295 	/* OK, different type or PA, have to kill old pv_entry. */
   2296 	pv_unlink(pmap, old_pte, pgva);
   2297 
   2298 add_pte:	/* can be destructive */
   2299 	pmeg_set_wiring(pmegp, pgva, wired);
   2300 
   2301 	/* Anything but MAIN_MEM is mapped non-cached. */
   2302 	if (!IS_MAIN_MEM(new_pte)) {
   2303 		new_pte |= PG_NC;
   2304 		do_pv = false;
   2305 	}
   2306 	if (do_pv == true) {
   2307 		if (pv_link(pmap, new_pte, pgva) & PV_NC)
   2308 			new_pte |= PG_NC;
   2309 	}
   2310 #ifdef	PMAP_DEBUG
   2311 	if ((pmap_debug & PMD_SETPTE) || (pgva == pmap_db_watchva)) {
   2312 		printf("pmap: set_pte pmap=%p va=0x%lx old=0x%x new=0x%x "
   2313 		       "(ek)\n", pmap, pgva, old_pte, new_pte);
   2314 	}
   2315 #endif
   2316 	/* cache flush done above */
   2317 	set_pte(pgva, new_pte);
   2318 	kernel_context_exit();
   2319 	pmegp->pmeg_vpages++;
   2320 }
   2321 
   2322 
   2323 static void
   2324 pmap_enter_user(pmap_t pmap, vaddr_t pgva, int new_pte, bool wired)
   2325 {
   2326 	int do_pv, old_pte, sme;
   2327 	vaddr_t segva;
   2328 	pmeg_t pmegp;
   2329 
   2330 #ifdef	DIAGNOSTIC
   2331 	if (pgva >= VM_MAXUSER_ADDRESS)
   2332 		panic("pmap_enter_user: bad va=0x%lx", pgva);
   2333 	if ((new_pte & (PG_VALID | PG_SYSTEM)) != PG_VALID)
   2334 		panic("pmap_enter_user: bad pte");
   2335 #endif
   2336 #ifdef	PMAP_DEBUG
   2337 	/*
   2338 	 * Some user pages are wired here, and a later
   2339 	 * call to pmap_unwire() will unwire them.
   2340 	 * XXX - Need a separate list for wired user pmegs
   2341 	 * so they can not be stolen from the active list.
   2342 	 * XXX - Note: vm_fault.c assumes pmap_extract will
   2343 	 * work on wired mappings, so must preserve them...
   2344 	 * XXX: Maybe keep a list of wired PMEGs?
   2345 	 */
   2346 	if (wired && (pmap_debug & PMD_WIRING)) {
   2347 		db_printf("pmap_enter_user: attempt to wire user page, "
   2348 			  "ignored\n");
   2349 		Debugger();
   2350 	}
   2351 #endif
   2352 
   2353 	/* Validate this assumption. */
   2354 	if (pmap != current_pmap()) {
   2355 #ifdef	PMAP_DEBUG
   2356 		/* Apparently, this never happens. */
   2357 		db_printf("pmap_enter_user: not curlwp\n");
   2358 		Debugger();
   2359 #endif
   2360 		/* Just throw it out (fault it in later). */
   2361 		/* XXX: But must remember it if wired... */
   2362 		return;
   2363 	}
   2364 
   2365 	segva = sun_trunc_seg(pgva);
   2366 	do_pv = true;
   2367 
   2368 	/*
   2369 	 * If this pmap was sharing the "empty" context,
   2370 	 * allocate a real context for its exclusive use.
   2371 	 */
   2372 	if (!has_context(pmap)) {
   2373 		context_allocate(pmap);
   2374 #ifdef PMAP_DEBUG
   2375 		if (pmap_debug & PMD_CONTEXT)
   2376 			printf("pmap_enter(%p) got context %d\n",
   2377 			       pmap, pmap->pm_ctxnum);
   2378 #endif
   2379 		set_context(pmap->pm_ctxnum);
   2380 	} else {
   2381 #ifdef	PMAP_DEBUG
   2382 		/* Make sure context is correct. */
   2383 		if (pmap->pm_ctxnum != get_context()) {
   2384 			db_printf("pmap_enter_user: wrong context\n");
   2385 			Debugger();
   2386 			/* XXX: OK to proceed? */
   2387 			set_context(pmap->pm_ctxnum);
   2388 		}
   2389 #endif
   2390 	}
   2391 
   2392 	/*
   2393 	 * We have a context.  Do we have a PMEG?
   2394 	 */
   2395 	sme = get_segmap(segva);
   2396 	if (sme != SEGINV) {
   2397 		/* Found a PMEG in the segmap.  Cool. */
   2398 		pmegp = pmeg_p(sme);
   2399 #ifdef	DIAGNOSTIC
   2400 		/* Make sure it is the right PMEG. */
   2401 		if (sme != pmap->pm_segmap[VA_SEGNUM(segva)])
   2402 			panic("pmap_enter_user: wrong sme at VA=0x%lx", segva);
   2403 		/* Make sure it is ours. */
   2404 		if (pmegp->pmeg_owner != pmap)
   2405 			panic("pmap_enter_user: MMU has bad pmeg 0x%x", sme);
   2406 #endif
   2407 	} else {
   2408 		/* Not in the segmap.  Try the S/W cache. */
   2409 		pmegp = pmeg_cache(pmap, segva);
   2410 		if (pmegp) {
   2411 			/* Found PMEG in cache.  Just reload it. */
   2412 			sme = pmegp->pmeg_index;
   2413 			set_segmap(segva, sme);
   2414 		} else {
   2415 			/* PMEG not in cache, so allocate one. */
   2416 			pmegp = pmeg_allocate(pmap, segva);
   2417 			sme = pmegp->pmeg_index;
   2418 			pmap->pm_segmap[VA_SEGNUM(segva)] = sme;
   2419 			set_segmap(segva, sme);
   2420 #ifdef	PMAP_DEBUG
   2421 			pmeg_verify_empty(segva);
   2422 #endif
   2423 		}
   2424 #ifdef	PMAP_DEBUG
   2425 		if (pmap_debug & PMD_SEGMAP) {
   2426 			printf("pmap: set_segmap pmap=%p va=0x%lx sme=0x%x "
   2427 			       "(eu)\n", pmap, segva, sme);
   2428 		}
   2429 #endif
   2430 	}
   2431 
   2432 	/*
   2433 	 * We have a PMEG.  Is the VA already mapped to somewhere?
   2434 	 *	(a) if so, is it same pa? (really a protection change)
   2435 	 *	(b) if not same pa, then we have to unlink from old pa
   2436 	 */
   2437 	old_pte = get_pte(pgva);
   2438 	if ((old_pte & PG_VALID) == 0)
   2439 		goto add_pte;
   2440 
   2441 	/* Have valid translation.  Flush cache before changing it. */
   2442 #ifdef	HAVECACHE
   2443 	if (cache_size) {
   2444 		cache_flush_page(pgva);
   2445 		/* Get fresh mod/ref bits from write-back. */
   2446 		old_pte = get_pte(pgva);
   2447 	}
   2448 #endif
   2449 
   2450 	/* XXX - removing valid page here, way lame... -glass */
   2451 	pmegp->pmeg_vpages--;
   2452 
   2453 	if (!IS_MAIN_MEM(old_pte)) {
   2454 		/* Was not main memory, so no pv_entry for it. */
   2455 		goto add_pte;
   2456 	}
   2457 
   2458 	/* Old mapping was main memory.  Save mod/ref bits. */
   2459 	save_modref_bits(old_pte);
   2460 
   2461 	/*
   2462 	 * If not changing the type or pfnum then re-use pv_entry.
   2463 	 * Note we get here only with old_pte having PGT_OBMEM.
   2464 	 */
   2465 	if ((old_pte & (PG_TYPE|PG_FRAME)) == (new_pte & (PG_TYPE|PG_FRAME))) {
   2466 		do_pv = false;		/* re-use pv_entry */
   2467 		new_pte |= (old_pte & PG_NC);
   2468 		goto add_pte;
   2469 	}
   2470 
   2471 	/* OK, different type or PA, have to kill old pv_entry. */
   2472 	pv_unlink(pmap, old_pte, pgva);
   2473 
   2474   add_pte:
   2475 	/* XXX - Wiring changes on user pmaps? */
   2476 	/* pmeg_set_wiring(pmegp, pgva, wired); */
   2477 
   2478 	/* Anything but MAIN_MEM is mapped non-cached. */
   2479 	if (!IS_MAIN_MEM(new_pte)) {
   2480 		new_pte |= PG_NC;
   2481 		do_pv = false;
   2482 	}
   2483 	if (do_pv == true) {
   2484 		if (pv_link(pmap, new_pte, pgva) & PV_NC)
   2485 			new_pte |= PG_NC;
   2486 	}
   2487 #ifdef	PMAP_DEBUG
   2488 	if ((pmap_debug & PMD_SETPTE) || (pgva == pmap_db_watchva)) {
   2489 		printf("pmap: set_pte pmap=%p va=0x%lx old=0x%x new=0x%x "
   2490 		       "(eu)\n", pmap, pgva, old_pte, new_pte);
   2491 	}
   2492 #endif
   2493 	/* cache flush done above */
   2494 	set_pte(pgva, new_pte);
   2495 	pmegp->pmeg_vpages++;
   2496 }
   2497 
   2498 void
   2499 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
   2500 {
   2501 	int new_pte, s;
   2502 	pmap_t pmap = kernel_pmap;
   2503 	pmeg_t pmegp;
   2504 	int sme;
   2505 	vaddr_t segva;
   2506 
   2507 #ifdef	PMAP_DEBUG
   2508 	if ((pmap_debug & PMD_ENTER) ||
   2509 	    (va == pmap_db_watchva))
   2510 		printf("pmap_kenter_pa(0x%lx, 0x%lx, 0x%x)\n",
   2511 		       va, pa, prot);
   2512 #endif
   2513 
   2514 	/* Get page-type bits from low part of the PA... */
   2515 	new_pte = (pa & PMAP_SPEC) << PG_MOD_SHIFT;
   2516 
   2517 	/* ...now the valid and writable bits... */
   2518 	new_pte |= PG_SYSTEM|PG_VALID;
   2519 	if (prot & VM_PROT_WRITE)
   2520 		new_pte |= PG_WRITE;
   2521 
   2522 	/* ...and finally the page-frame number. */
   2523 	new_pte |= PA_PGNUM(pa);
   2524 
   2525 	/*
   2526 	 * Kernel mappings are kept in hardware only.  On Sun-2, it's
   2527 	 * in context 0, on Sun-3, it's in all contexts.  The only
   2528 	 * thing we have to handle here is possibly allocating additional
   2529 	 * PMEGs and inserting them into the appropriate contexts.
   2530 	 */
   2531 	kernel_context_enter();
   2532 
   2533 	/*
   2534 	 * In detail:
   2535 	 *
   2536 	 * (a) lock pmap
   2537 	 * (b) Is the VA in a already mapped segment, if so
   2538 	 *	 look to see if that VA address is "valid".  If it is, then
   2539 	 *	 action is a change to an existing pte
   2540 	 * (c) if not mapped segment, need to allocate pmeg
   2541 	 * (d) change/add pte
   2542 	 */
   2543 
   2544 #ifdef	DIAGNOSTIC
   2545 	if ((va < virtual_avail) || (va >= DVMA_MAP_END))
   2546 		panic("pmap_kenter_pa: bad va=0x%lx", va);
   2547 #endif
   2548 
   2549 	if (va >= DVMA_MAP_BASE) {
   2550 		/* This is DVMA space.  Always want it non-cached. */
   2551 		new_pte |= PG_NC;
   2552 	}
   2553 
   2554 	segva = sun_trunc_seg(va);
   2555 
   2556 	s = splvm();
   2557 
   2558 	/* Do we have a PMEG? */
   2559 	sme = get_segmap(segva);
   2560 	if (sme != SEGINV) {
   2561 		KASSERT((get_pte(va) & PG_VALID) == 0);
   2562 
   2563 		/* Found a PMEG in the segmap.  Cool. */
   2564 		pmegp = pmeg_p(sme);
   2565 #ifdef	DIAGNOSTIC
   2566 		/* Make sure it is the right PMEG. */
   2567 		if (sme != pmap->pm_segmap[VA_SEGNUM(segva)])
   2568 			panic("pmap_kenter_pa: wrong sme at VA=0x%lx", segva);
   2569 		/* Make sure it is ours. */
   2570 		if (pmegp->pmeg_owner != pmap)
   2571 			panic("pmap_kenter_pa: MMU has bad pmeg 0x%x", sme);
   2572 #endif
   2573 	} else {
   2574 
   2575 		/* No PMEG in the segmap.  Have to allocate one. */
   2576 		pmegp = pmeg_allocate(pmap, segva);
   2577 		sme = pmegp->pmeg_index;
   2578 		pmap->pm_segmap[VA_SEGNUM(segva)] = sme;
   2579 		set_kernel_segmap(segva, sme);
   2580 #ifdef	PMAP_DEBUG
   2581 		pmeg_verify_empty(segva);
   2582 		if (pmap_debug & PMD_SEGMAP) {
   2583 			printf("pmap: set_segmap pmap=%p va=0x%lx sme=0x%x "
   2584 			       "(ek)\n", pmap, segva, sme);
   2585 		}
   2586 #endif
   2587 	}
   2588 
   2589 	pmeg_set_wiring(pmegp, va, true);
   2590 
   2591 	/* Anything but MAIN_MEM is mapped non-cached. */
   2592 	if (!IS_MAIN_MEM(new_pte)) {
   2593 		new_pte |= PG_NC;
   2594 	}
   2595 #ifdef	PMAP_DEBUG
   2596 	if ((pmap_debug & PMD_SETPTE) || (va == pmap_db_watchva)) {
   2597 		printf("pmap: set_pte pmap=%p va=0x%lx new=0x%x "
   2598 		       "(ek)\n", pmap, va, new_pte);
   2599 	}
   2600 #endif
   2601 	/* cache flush done above */
   2602 	set_pte(va, new_pte);
   2603 	kernel_context_exit();
   2604 	pmegp->pmeg_vpages++;
   2605 	splx(s);
   2606 }
   2607 
   2608 void
   2609 pmap_kremove(vaddr_t va, vsize_t len)
   2610 {
   2611 	pmap_t pmap = kernel_pmap;
   2612 	vaddr_t eva, neva, pgva, segva, segnum;
   2613 	int pte, sme;
   2614 	pmeg_t pmegp;
   2615 #ifdef HAVECACHE
   2616 	int flush_by_page = 0;
   2617 #endif
   2618 	int s;
   2619 
   2620 	s = splvm();
   2621 	kernel_context_enter();
   2622 	segnum = VA_SEGNUM(va);
   2623 	for (eva = va + len; va < eva; va = neva, segnum++) {
   2624 		neva = sun_trunc_seg(va) + NBSG;
   2625 		if (neva > eva) {
   2626 			neva = eva;
   2627 		}
   2628 		if (pmap->pm_segmap[segnum] == SEGINV) {
   2629 			continue;
   2630 		}
   2631 
   2632 		segva = sun_trunc_seg(va);
   2633 		sme = get_segmap(segva);
   2634 		pmegp = pmeg_p(sme);
   2635 
   2636 #ifdef	HAVECACHE
   2637 		if (cache_size) {
   2638 
   2639 			/*
   2640 			 * If the range to be removed is larger than the cache,
   2641 			 * it will be cheaper to flush this segment entirely.
   2642 			 */
   2643 
   2644 			if (cache_size < (eva - va)) {
   2645 				/* cheaper to flush whole segment */
   2646 				cache_flush_segment(segva);
   2647 			} else {
   2648 				flush_by_page = 1;
   2649 			}
   2650 		}
   2651 #endif
   2652 
   2653 		/* Invalidate the PTEs in the given range. */
   2654 		for (pgva = va; pgva < neva; pgva += PAGE_SIZE) {
   2655 			pte = get_pte(pgva);
   2656 			if (pte & PG_VALID) {
   2657 #ifdef	HAVECACHE
   2658 				if (flush_by_page) {
   2659 					cache_flush_page(pgva);
   2660 					/* Get fresh mod/ref bits
   2661 					   from write-back. */
   2662 					pte = get_pte(pgva);
   2663 				}
   2664 #endif
   2665 #ifdef	PMAP_DEBUG
   2666 				if ((pmap_debug & PMD_SETPTE) ||
   2667 				    (pgva == pmap_db_watchva)) {
   2668 					printf("pmap: set_pte pmap=%p va=0x%lx"
   2669 					       " old=0x%x new=0x%x (rrmmu)\n",
   2670 					       pmap, pgva, pte, PG_INVAL);
   2671 				}
   2672 #endif
   2673 				set_pte(pgva, PG_INVAL);
   2674 				KASSERT(pmegp->pmeg_vpages > 0);
   2675 				pmegp->pmeg_vpages--;
   2676 			}
   2677 		}
   2678 		KASSERT(pmegp->pmeg_vpages >= 0);
   2679 		if (pmegp->pmeg_vpages == 0) {
   2680 			/* We are done with this pmeg. */
   2681 #ifdef	PMAP_DEBUG
   2682 			if (is_pmeg_wired(pmegp)) {
   2683 				if (pmap_debug & PMD_WIRING) {
   2684 					db_printf("pmap: removing wired "
   2685 						  "pmeg: %p\n", pmegp);
   2686 					Debugger();
   2687 				}
   2688 			}
   2689 			if (pmap_debug & PMD_SEGMAP) {
   2690 				printf("pmap: set_segmap ctx=%d v=0x%lx "
   2691 				       "old=0x%x new=ff (rm)\n",
   2692 				       pmap->pm_ctxnum, segva,
   2693 				       pmegp->pmeg_index);
   2694 			}
   2695 			pmeg_verify_empty(segva);
   2696 #endif
   2697 
   2698 			/* Remove it from the MMU. */
   2699 			set_kernel_segmap(segva, SEGINV);
   2700 			pmap->pm_segmap[VA_SEGNUM(segva)] = SEGINV;
   2701 
   2702 			/* Now, put it on the free list. */
   2703 			pmeg_free(pmegp);
   2704 		}
   2705 	}
   2706 	kernel_context_exit();
   2707 	splx(s);
   2708 }
   2709 
   2710 
   2711 /*
   2712  * The trap handler calls this so we can try to resolve
   2713  * user-level faults by reloading a PMEG.
   2714  * If that does not prodce a valid mapping,
   2715  * call vm_fault as usual.
   2716  *
   2717  * XXX: Merge this with the next function?
   2718  */
   2719 int
   2720 pmap_fault(struct vm_map *map, vaddr_t va, vm_prot_t ftype)
   2721 {
   2722 	pmap_t pmap;
   2723 	int rv;
   2724 
   2725 	pmap = vm_map_pmap(map);
   2726 	if (map == kernel_map) {
   2727 		/* Do not allow faults below the "managed" space. */
   2728 		if (va < virtual_avail) {
   2729 			/*
   2730 			 * Most pages below virtual_avail are read-only,
   2731 			 * so I will assume it is a protection failure.
   2732 			 */
   2733 			return EACCES;
   2734 		}
   2735 	} else {
   2736 		/* User map.  Try reload shortcut. */
   2737 		if (pmap_fault_reload(pmap, va, ftype))
   2738 			return 0;
   2739 	}
   2740 	rv = uvm_fault(map, va, ftype);
   2741 
   2742 #ifdef	PMAP_DEBUG
   2743 	if (pmap_debug & PMD_FAULT) {
   2744 		printf("pmap_fault(%p, 0x%lx, 0x%x) -> 0x%x\n",
   2745 		       map, va, ftype, rv);
   2746 	}
   2747 #endif
   2748 
   2749 	return (rv);
   2750 }
   2751 
   2752 /*
   2753  * This is a shortcut used by the trap handler to
   2754  * reload PMEGs into a user segmap without calling
   2755  * the actual VM fault handler.  Returns true if:
   2756  *	the PMEG was reloaded, and
   2757  *	it has a valid PTE at va.
   2758  * Otherwise return zero and let VM code handle it.
   2759  */
   2760 int
   2761 pmap_fault_reload(pmap_t pmap, vaddr_t pgva, vm_prot_t ftype)
   2762 {
   2763 	int rv, s, pte, chkpte, sme;
   2764 	vaddr_t segva;
   2765 	pmeg_t pmegp;
   2766 
   2767 	if (pgva >= VM_MAXUSER_ADDRESS)
   2768 		return (0);
   2769 	if (pmap->pm_segmap == NULL) {
   2770 #ifdef	PMAP_DEBUG
   2771 		db_printf("pmap_fault_reload: null segmap\n");
   2772 		Debugger();
   2773 #endif
   2774 		return (0);
   2775 	}
   2776 
   2777 	/* Short-cut using the S/W segmap. */
   2778 	if (pmap->pm_segmap[VA_SEGNUM(pgva)] == SEGINV)
   2779 		return (0);
   2780 
   2781 	segva = sun_trunc_seg(pgva);
   2782 	chkpte = PG_VALID;
   2783 	if (ftype & VM_PROT_WRITE)
   2784 		chkpte |= PG_WRITE;
   2785 	rv = 0;
   2786 
   2787 	s = splvm();
   2788 
   2789 	/*
   2790 	 * Given that we faulted on a user-space address, we will
   2791 	 * probably need a context.  Get a context now so we can
   2792 	 * try to resolve the fault with a segmap reload.
   2793 	 */
   2794 	if (!has_context(pmap)) {
   2795 		context_allocate(pmap);
   2796 #ifdef PMAP_DEBUG
   2797 		if (pmap_debug & PMD_CONTEXT)
   2798 			printf("pmap_fault(%p) got context %d\n",
   2799 			       pmap, pmap->pm_ctxnum);
   2800 #endif
   2801 		set_context(pmap->pm_ctxnum);
   2802 	} else {
   2803 #ifdef	PMAP_DEBUG
   2804 		/* Make sure context is correct. */
   2805 		if (pmap->pm_ctxnum != get_context()) {
   2806 			db_printf("pmap_fault_reload: wrong context\n");
   2807 			Debugger();
   2808 			/* XXX: OK to proceed? */
   2809 			set_context(pmap->pm_ctxnum);
   2810 		}
   2811 #endif
   2812 	}
   2813 
   2814 	sme = get_segmap(segva);
   2815 	if (sme == SEGINV) {
   2816 		/* See if there is something to reload. */
   2817 		pmegp = pmeg_cache(pmap, segva);
   2818 		if (pmegp) {
   2819 			/* Found one!  OK, reload it. */
   2820 			pmap_stats.ps_pmeg_faultin++;
   2821 			sme = pmegp->pmeg_index;
   2822 			set_segmap(segva, sme);
   2823 			pte = get_pte(pgva);
   2824 			if (pte & chkpte)
   2825 				rv = 1;
   2826 		}
   2827 	}
   2828 
   2829 	splx(s);
   2830 	return (rv);
   2831 }
   2832 
   2833 
   2834 /*
   2835  * Clear the modify bit for the given physical page.
   2836  */
   2837 bool
   2838 pmap_clear_modify(struct vm_page *pg)
   2839 {
   2840 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   2841 	pv_entry_t *head;
   2842 	u_char *pv_flags;
   2843 	int s;
   2844 	bool rv;
   2845 
   2846 	pv_flags = pa_to_pvflags(pa);
   2847 	head     = pa_to_pvhead(pa);
   2848 
   2849 	s = splvm();
   2850 	*pv_flags |= pv_syncflags(*head);
   2851 	rv = *pv_flags & PV_MOD;
   2852 	*pv_flags &= ~PV_MOD;
   2853 	splx(s);
   2854 	return rv;
   2855 }
   2856 
   2857 /*
   2858  * Tell whether the given physical page has been modified.
   2859  */
   2860 bool
   2861 pmap_is_modified(struct vm_page *pg)
   2862 {
   2863 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   2864 	pv_entry_t *head;
   2865 	u_char *pv_flags;
   2866 	int s;
   2867 	bool rv;
   2868 
   2869 	pv_flags = pa_to_pvflags(pa);
   2870 	head     = pa_to_pvhead(pa);
   2871 
   2872 	s = splvm();
   2873 	if ((*pv_flags & PV_MOD) == 0)
   2874 		*pv_flags |= pv_syncflags(*head);
   2875 	rv = (*pv_flags & PV_MOD);
   2876 	splx(s);
   2877 	return (rv);
   2878 }
   2879 
   2880 /*
   2881  * Clear the reference bit for the given physical page.
   2882  * It's OK to just remove mappings if that's easier.
   2883  */
   2884 bool
   2885 pmap_clear_reference(struct vm_page *pg)
   2886 {
   2887 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   2888 	pv_entry_t *head;
   2889 	u_char *pv_flags;
   2890 	int s;
   2891 	bool rv;
   2892 
   2893 	pv_flags = pa_to_pvflags(pa);
   2894 	head     = pa_to_pvhead(pa);
   2895 
   2896 	s = splvm();
   2897 	*pv_flags |= pv_syncflags(*head);
   2898 	rv = *pv_flags & PV_REF;
   2899 	*pv_flags &= ~PV_REF;
   2900 	splx(s);
   2901 	return rv;
   2902 }
   2903 
   2904 /*
   2905  * Tell whether the given physical page has been referenced.
   2906  * It's OK to just return false if page is not mapped.
   2907  */
   2908 bool
   2909 pmap_is_referenced(struct vm_page *pg)
   2910 {
   2911 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   2912 	pv_entry_t *head;
   2913 	u_char *pv_flags;
   2914 	int s;
   2915 	bool rv;
   2916 
   2917 	pv_flags = pa_to_pvflags(pa);
   2918 	head     = pa_to_pvhead(pa);
   2919 
   2920 	s = splvm();
   2921 	if ((*pv_flags & PV_REF) == 0)
   2922 		*pv_flags |= pv_syncflags(*head);
   2923 	rv = (*pv_flags & PV_REF);
   2924 	splx(s);
   2925 	return (rv);
   2926 }
   2927 
   2928 
   2929 static inline void
   2930 _pmap_switch(pmap_t pmap)
   2931 {
   2932 #if IS_SUN2
   2933 	/*
   2934 	 * Since we maintain completely separate user and kernel address
   2935 	 * spaces, whenever we switch to a process, we need to make sure
   2936 	 * that it has a context allocated.
   2937 	 */
   2938 	if (!has_context(pmap)) {
   2939 		context_allocate(pmap);
   2940 #ifdef PMAP_DEBUG
   2941 		if (pmap_debug & PMD_CONTEXT)
   2942 			printf("_pmap_switch(%p) got context %d\n",
   2943 			       pmap, pmap->pm_ctxnum);
   2944 #endif
   2945 	}
   2946 #endif /* IS_SUN2 */
   2947 	set_context(pmap->pm_ctxnum);
   2948 	ICIA();
   2949 }
   2950 
   2951 /*
   2952  * Exported version of pmap_activate().  This is called from the
   2953  * machine-independent VM code when a process is given a new pmap.
   2954  * If (p == curlwp) do like cpu_switch would do; otherwise just
   2955  * take this as notification that the process has a new pmap.
   2956  */
   2957 void
   2958 pmap_activate(struct lwp *l)
   2959 {
   2960 	pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
   2961 
   2962 	if (l->l_proc == curproc) {
   2963 		_pmap_switch(pmap);
   2964 	}
   2965 }
   2966 
   2967 /*
   2968  * Deactivate the address space of the specified process.
   2969  */
   2970 void
   2971 pmap_deactivate(struct lwp *l)
   2972 {
   2973 	/* Nothing to do. */
   2974 }
   2975 
   2976 /*
   2977  *	Routine:	pmap_unwire
   2978  *	Function:	Clear the wired attribute for a map/virtual-address
   2979  *			pair.
   2980  *	In/out conditions:
   2981  *			The mapping must already exist in the pmap.
   2982  */
   2983 void
   2984 pmap_unwire(pmap_t pmap, vaddr_t va)
   2985 {
   2986 	int s, sme;
   2987 	int wiremask, ptenum;
   2988 	pmeg_t pmegp;
   2989 
   2990 #ifdef PMAP_DEBUG
   2991 	if (pmap_debug & PMD_WIRING)
   2992 		printf("pmap_unwire(pmap=%p, va=0x%lx)\n",
   2993 		       pmap, va);
   2994 #endif
   2995 	/*
   2996 	 * We are asked to unwire pages that were wired when
   2997 	 * pmap_enter() was called and we ignored wiring.
   2998 	 * (VM code appears to wire a stack page during fork.)
   2999 	 */
   3000 	if (pmap != kernel_pmap) {
   3001 #ifdef PMAP_DEBUG
   3002 		if (pmap_debug & PMD_WIRING) {
   3003 			db_printf("  (user pmap -- ignored)\n");
   3004 			Debugger();
   3005 		}
   3006 #endif
   3007 		return;
   3008 	}
   3009 
   3010 	ptenum = VA_PTE_NUM(va);
   3011 	wiremask = 1 << ptenum;
   3012 
   3013 	s = splvm();
   3014 	kernel_context_enter();
   3015 	sme = get_segmap(va);
   3016 	kernel_context_exit();
   3017 	pmegp = pmeg_p(sme);
   3018 	pmegp->pmeg_wired &= ~wiremask;
   3019 	splx(s);
   3020 }
   3021 
   3022 /*
   3023  *	Copy the range specified by src_addr/len
   3024  *	from the source map to the range dst_addr/len
   3025  *	in the destination map.
   3026  *
   3027  *	This routine is only advisory and need not do anything.
   3028  */
   3029 void
   3030 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vaddr_t dst_addr, vsize_t len,
   3031 	  vaddr_t src_addr)
   3032 {
   3033 }
   3034 
   3035 /*
   3036  *	Routine:	pmap_extract
   3037  *	Function:
   3038  *		Extract the physical page address associated
   3039  *		with the given map/virtual_address pair.
   3040  *	Returns zero if VA not valid.
   3041  */
   3042 bool
   3043 pmap_extract(pmap_t pmap, vaddr_t va, paddr_t *pap)
   3044 {
   3045 	int s, sme, segnum, ptenum, pte;
   3046 	paddr_t pa;
   3047 
   3048 	pte = 0;
   3049 	s = splvm();
   3050 	if (pmap == kernel_pmap) {
   3051 		kernel_context_enter();
   3052 		sme = get_segmap(va);
   3053 		if (sme != SEGINV)
   3054 			pte = get_pte(va);
   3055 		kernel_context_exit();
   3056 	} else {
   3057 		/* This is rare, so do it the easy way. */
   3058 		segnum = VA_SEGNUM(va);
   3059 		sme = pmap->pm_segmap[segnum];
   3060 		if (sme != SEGINV) {
   3061 			ptenum = VA_PTE_NUM(va);
   3062 			pte = get_pte_pmeg(sme, ptenum);
   3063 		}
   3064 	}
   3065 	splx(s);
   3066 
   3067 	if ((pte & PG_VALID) == 0) {
   3068 #ifdef PMAP_DEBUG
   3069 		db_printf("pmap_extract: invalid va=0x%lx\n", va);
   3070 		Debugger();
   3071 #endif
   3072 		return (false);
   3073 	}
   3074 	pa = PG_PA(pte);
   3075 #ifdef	DIAGNOSTIC
   3076 	if (pte & PG_TYPE) {
   3077 		panic("pmap_extract: not main mem, va=0x%lx", va);
   3078 	}
   3079 #endif
   3080 	if (pap != NULL)
   3081 		*pap = pa;
   3082 	return (true);
   3083 }
   3084 
   3085 /*
   3086  *	pmap_phys_address:
   3087  *
   3088  *	Return the physical address corresponding to the specified
   3089  *	cookie.  Used by the device pager to decode a device driver's
   3090  *	mmap entry point return value.
   3091  */
   3092 paddr_t
   3093 pmap_phys_address(paddr_t addr)
   3094 {
   3095 	return addr;
   3096 }
   3097 
   3098 /*
   3099  *	  pmap_page_protect:
   3100  *
   3101  *	  Lower the permission for all mappings to a given page.
   3102  */
   3103 void
   3104 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
   3105 {
   3106 	paddr_t pa = VM_PAGE_TO_PHYS(pg);
   3107 	int s;
   3108 
   3109 	s = splvm();
   3110 #ifdef PMAP_DEBUG
   3111 	if (pmap_debug & PMD_PROTECT)
   3112 		printf("pmap_page_protect(0x%lx, 0x%x)\n", pa, prot);
   3113 #endif
   3114 	switch (prot) {
   3115 	case VM_PROT_ALL:
   3116 		break;
   3117 	case VM_PROT_READ:
   3118 	case VM_PROT_READ|VM_PROT_EXECUTE:
   3119 		pv_changepte(pa, 0, PG_WRITE);
   3120 		break;
   3121 	default:
   3122 		/* remove mapping for all pmaps that have it */
   3123 		pv_remove_all(pa);
   3124 		break;
   3125 	}
   3126 	splx(s);
   3127 }
   3128 
   3129 /*
   3130  * Initialize a preallocated and zeroed pmap structure,
   3131  * such as one in a vmspace structure.
   3132  */
   3133 void
   3134 pmap_pinit(pmap_t pmap)
   3135 {
   3136 	pmap_common_init(pmap);
   3137 	pmap_user_init(pmap);
   3138 }
   3139 
   3140 /*
   3141  *	Reduce the permissions on the specified
   3142  *	range of this map as requested.
   3143  *	(Make pages read-only.)
   3144  */
   3145 void
   3146 pmap_protect(pmap_t pmap, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
   3147 {
   3148 	vaddr_t va, neva;
   3149 	int segnum;
   3150 
   3151 	/* If leaving writable, nothing to do. */
   3152 	if (prot & VM_PROT_WRITE)
   3153 		return;
   3154 
   3155 	/* If removing all permissions, just unmap. */
   3156 	if ((prot & VM_PROT_READ) == 0) {
   3157 		pmap_remove(pmap, sva, eva);
   3158 		return;
   3159 	}
   3160 
   3161 #ifdef	PMAP_DEBUG
   3162 	if ((pmap_debug & PMD_PROTECT) ||
   3163 	    ((sva <= pmap_db_watchva && eva > pmap_db_watchva)))
   3164 		printf("pmap_protect(%p, 0x%lx, 0x%lx)\n", pmap, sva, eva);
   3165 #endif
   3166 
   3167 	KASSERT((pmap == kernel_pmap) ?
   3168 		sva >= virtual_avail && eva < DVMA_MAP_END :
   3169 		eva <= VM_MAXUSER_ADDRESS);
   3170 	va = sva;
   3171 	segnum = VA_SEGNUM(va);
   3172 	while (va < eva) {
   3173 		neva = sun_trunc_seg(va) + NBSG;
   3174 		if (neva > eva)
   3175 			neva = eva;
   3176 		if (pmap->pm_segmap[segnum] != SEGINV)
   3177 			pmap_protect1(pmap, va, neva);
   3178 		va = neva;
   3179 		segnum++;
   3180 	}
   3181 }
   3182 
   3183 /*
   3184  * Remove write permissions in given range.
   3185  * (guaranteed to be within one segment)
   3186  * similar to pmap_remove1()
   3187  */
   3188 void
   3189 pmap_protect1(pmap_t pmap, vaddr_t sva, vaddr_t eva)
   3190 {
   3191 	int old_ctx, s, sme;
   3192 	bool in_ctx;
   3193 
   3194 	s = splvm();
   3195 
   3196 #ifdef	DIAGNOSTIC
   3197 	if (sun_trunc_seg(sva) != sun_trunc_seg(eva-1))
   3198 		panic("pmap_protect1: bad range!");
   3199 #endif
   3200 
   3201 	if (pmap == kernel_pmap) {
   3202 		kernel_context_enter();
   3203 		sme = get_segmap(sva);
   3204 		if (sme != SEGINV)
   3205 			pmap_protect_mmu(pmap, sva, eva);
   3206 		kernel_context_exit();
   3207 		goto out;
   3208 	}
   3209 	/* It is a user pmap. */
   3210 
   3211 	/* There is a PMEG, but maybe not active. */
   3212 	old_ctx = INVALID_CONTEXT;
   3213 	in_ctx = false;
   3214 	if (has_context(pmap)) {
   3215 		/* Temporary context change. */
   3216 		old_ctx = get_context();
   3217 		set_context(pmap->pm_ctxnum);
   3218 		sme = get_segmap(sva);
   3219 		if (sme != SEGINV)
   3220 			in_ctx = true;
   3221 	}
   3222 
   3223 	if (in_ctx == true)
   3224 		pmap_protect_mmu(pmap, sva, eva);
   3225 	else
   3226 		pmap_protect_noctx(pmap, sva, eva);
   3227 
   3228 	if (old_ctx != INVALID_CONTEXT) {
   3229 		/* Restore previous context. */
   3230 		set_context(old_ctx);
   3231 	}
   3232 
   3233 out:
   3234 	splx(s);
   3235 }
   3236 
   3237 /*
   3238  * Remove write permissions, all in one PMEG,
   3239  * where that PMEG is currently in the MMU.
   3240  * The current context is already correct.
   3241  */
   3242 void
   3243 pmap_protect_mmu(pmap_t pmap, vaddr_t sva, vaddr_t eva)
   3244 {
   3245 	vaddr_t pgva;
   3246 	int pte;
   3247 #if defined(HAVECACHE)
   3248 	int flush_by_page = 0;
   3249 #endif
   3250 #if defined(HAVECACHE) || defined(DIAGNOSTIC)
   3251 	vaddr_t segva = sun_trunc_seg(sva);
   3252 #endif
   3253 
   3254 	CHECK_SPL();
   3255 
   3256 #ifdef	DIAGNOSTIC
   3257 	if (
   3258 #if IS_SUN3
   3259 	    pmap != kernel_pmap &&
   3260 #endif
   3261 	    pmap->pm_ctxnum != get_context()) {
   3262 		panic("pmap_protect_mmu: wrong context");
   3263 	}
   3264 
   3265 	int sme = get_segmap(segva);
   3266 	/* Make sure it is valid and known. */
   3267 	if (sme == SEGINV)
   3268 		panic("pmap_protect_mmu: SEGINV");
   3269 	if (pmap->pm_segmap && (pmap->pm_segmap[VA_SEGNUM(segva)] != sme))
   3270 		panic("pmap_protect_mmu: incorrect sme, va=0x%lx", segva);
   3271 
   3272 	/* have pmeg, will travel */
   3273 	pmeg_t pmegp = pmeg_p(sme);
   3274 	/* Make sure we own the pmeg, right va, etc. */
   3275 	if ((pmegp->pmeg_va != segva) ||
   3276 	    (pmegp->pmeg_owner != pmap) ||
   3277 	    (pmegp->pmeg_version != pmap->pm_version))
   3278 	{
   3279 		panic("pmap_protect_mmu: bad pmeg=%p", pmegp);
   3280 	}
   3281 	if (pmegp->pmeg_vpages < 0)
   3282 		panic("pmap_protect_mmu: npages corrupted");
   3283 	if (pmegp->pmeg_vpages == 0)
   3284 		panic("pmap_protect_mmu: no valid pages?");
   3285 #endif /* DIAGNOSTIC */
   3286 
   3287 #ifdef	HAVECACHE
   3288 	if (cache_size) {
   3289 		/*
   3290 		 * If the range to be removed is larger than the cache,
   3291 		 * it will be cheaper to flush this segment entirely.
   3292 		 */
   3293 		if (cache_size < (eva - sva)) {
   3294 			/* cheaper to flush whole segment */
   3295 			cache_flush_segment(segva);
   3296 		} else {
   3297 			flush_by_page = 1;
   3298 		}
   3299 	}
   3300 #endif
   3301 
   3302 	/* Remove write permission in the given range. */
   3303 	for (pgva = sva; pgva < eva; pgva += PAGE_SIZE) {
   3304 		pte = get_pte(pgva);
   3305 		if (pte & PG_VALID) {
   3306 #ifdef	HAVECACHE
   3307 			if (flush_by_page) {
   3308 				cache_flush_page(pgva);
   3309 				/* Get fresh mod/ref bits from write-back. */
   3310 				pte = get_pte(pgva);
   3311 			}
   3312 #endif
   3313 			if (IS_MAIN_MEM(pte)) {
   3314 				save_modref_bits(pte);
   3315 			}
   3316 			pte &= ~(PG_WRITE | PG_MODREF);
   3317 			set_pte(pgva, pte);
   3318 		}
   3319 	}
   3320 }
   3321 
   3322 /*
   3323  * Remove write permissions, all in one PMEG,
   3324  * where it is not currently in any context.
   3325  */
   3326 void
   3327 pmap_protect_noctx(pmap_t pmap, vaddr_t sva, vaddr_t eva)
   3328 {
   3329 	int old_ctx, pte, sme, segnum;
   3330 	vaddr_t pgva, segva;
   3331 
   3332 #ifdef	DIAGNOSTIC
   3333 	/* Kernel always in a context. */
   3334 	if (pmap == kernel_pmap)
   3335 		panic("pmap_protect_noctx: kernel_pmap");
   3336 	if (pmap->pm_segmap == NULL)
   3337 		panic("pmap_protect_noctx: null segmap");
   3338 #endif
   3339 
   3340 	segva = sun_trunc_seg(sva);
   3341 	segnum = VA_SEGNUM(segva);
   3342 	sme = pmap->pm_segmap[segnum];
   3343 	if (sme == SEGINV)
   3344 		return;
   3345 
   3346 	/*
   3347 	 * Borrow the EMPTY_CONTEXT so we can access the PMEG
   3348 	 * at its normal virtual address.
   3349 	 *
   3350 	 * (On Sun-2, we have to use the temporary segment.)
   3351 	 */
   3352 	old_ctx = get_context();
   3353 #if IS_SUN2
   3354 	set_context(KERNEL_CONTEXT);
   3355 	set_segmap(temp_seg_va, sme);
   3356 #ifdef DIAGNOSTIC
   3357 	if (temp_seg_inuse)
   3358 		panic("pmap_protect_noctx: temp_seg_inuse");
   3359 	temp_seg_inuse++;
   3360 #endif
   3361 	sva += (temp_seg_va - segva);
   3362 	eva += (temp_seg_va - segva);
   3363 #else /* IS_SUN3 */
   3364 	set_context(EMPTY_CONTEXT);
   3365 	set_segmap(segva, sme);
   3366 #endif /* IS_SUN2 */
   3367 
   3368 	/* Remove write permission in the given range. */
   3369 	for (pgva = sva; pgva < eva; pgva += PAGE_SIZE) {
   3370 		pte = get_pte(pgva);
   3371 		if (pte & PG_VALID) {
   3372 			/* No cache flush needed. */
   3373 			if (IS_MAIN_MEM(pte)) {
   3374 				save_modref_bits(pte);
   3375 			}
   3376 			pte &= ~(PG_WRITE | PG_MODREF);
   3377 			set_pte(pgva, pte);
   3378 		}
   3379 	}
   3380 
   3381 	/*
   3382 	 * Make the EMPTY_CONTEXT really empty again, and
   3383 	 * restore the previous context.
   3384 	 *
   3385 	 * (On Sun-2, release the temporary segment.)
   3386 	 */
   3387 #if IS_SUN2
   3388 	set_segmap(temp_seg_va, SEGINV);
   3389 #ifdef DIAGNOSTIC
   3390 	temp_seg_inuse--;
   3391 #endif
   3392 #else /* IS_SUN3 */
   3393 	set_segmap(segva, SEGINV);
   3394 #endif /* IS_SUN2 */
   3395 	set_context(old_ctx);
   3396 }
   3397 
   3398 
   3399 /*
   3400  *	Remove the given range of addresses from the specified map.
   3401  *
   3402  *	It is assumed that the start and end are properly
   3403  *	rounded to the page size.
   3404  */
   3405 void
   3406 pmap_remove(pmap_t pmap, vaddr_t sva, vaddr_t eva)
   3407 {
   3408 	vaddr_t va, neva;
   3409 	int segnum;
   3410 
   3411 #ifdef	PMAP_DEBUG
   3412 	if ((pmap_debug & PMD_REMOVE) ||
   3413 	    ((sva <= pmap_db_watchva && eva > pmap_db_watchva)))
   3414 		printf("pmap_remove(%p, 0x%lx, 0x%lx)\n", pmap, sva, eva);
   3415 #endif
   3416 
   3417 
   3418 	KASSERT((pmap == kernel_pmap) ?
   3419 		sva >= virtual_avail && eva < DVMA_MAP_END :
   3420 		eva <= VM_MAXUSER_ADDRESS);
   3421 	va = sva;
   3422 	segnum = VA_SEGNUM(va);
   3423 	while (va < eva) {
   3424 		neva = sun_trunc_seg(va) + NBSG;
   3425 		if (neva > eva)
   3426 			neva = eva;
   3427 		if (pmap->pm_segmap[segnum] != SEGINV)
   3428 			pmap_remove1(pmap, va, neva);
   3429 		va = neva;
   3430 		segnum++;
   3431 	}
   3432 }
   3433 
   3434 /*
   3435  * Remove user mappings, all within one segment
   3436  */
   3437 void
   3438 pmap_remove1(pmap_t pmap, vaddr_t sva, vaddr_t eva)
   3439 {
   3440 	int old_ctx, s, sme;
   3441 	bool in_ctx;
   3442 
   3443 	s = splvm();
   3444 
   3445 #ifdef	DIAGNOSTIC
   3446 	if (sun_trunc_seg(sva) != sun_trunc_seg(eva-1))
   3447 		panic("pmap_remove1: bad range!");
   3448 #endif
   3449 
   3450 	if (pmap == kernel_pmap) {
   3451 		kernel_context_enter();
   3452 		sme = get_segmap(sva);
   3453 		if (sme != SEGINV)
   3454 			pmap_remove_mmu(pmap, sva, eva);
   3455 		kernel_context_exit();
   3456 		goto out;
   3457 	}
   3458 	/* It is a user pmap. */
   3459 
   3460 	/* There is a PMEG, but maybe not active. */
   3461 	old_ctx = INVALID_CONTEXT;
   3462 	in_ctx = false;
   3463 	if (has_context(pmap)) {
   3464 		/* Temporary context change. */
   3465 		old_ctx = get_context();
   3466 		set_context(pmap->pm_ctxnum);
   3467 		sme = get_segmap(sva);
   3468 		if (sme != SEGINV)
   3469 			in_ctx = true;
   3470 	}
   3471 
   3472 	if (in_ctx == true)
   3473 		pmap_remove_mmu(pmap, sva, eva);
   3474 	else
   3475 		pmap_remove_noctx(pmap, sva, eva);
   3476 
   3477 	if (old_ctx != INVALID_CONTEXT) {
   3478 		/* Restore previous context. */
   3479 		set_context(old_ctx);
   3480 	}
   3481 
   3482 out:
   3483 	splx(s);
   3484 }
   3485 
   3486 /*
   3487  * Remove some mappings, all in one PMEG,
   3488  * where that PMEG is currently in the MMU.
   3489  * The current context is already correct.
   3490  * If no PTEs remain valid in the PMEG, free it.
   3491  */
   3492 void
   3493 pmap_remove_mmu(pmap_t pmap, vaddr_t sva, vaddr_t eva)
   3494 {
   3495 	pmeg_t pmegp;
   3496 	vaddr_t pgva, segva;
   3497 	int pte, sme;
   3498 #ifdef HAVECACHE
   3499 	int flush_by_page = 0;
   3500 #endif
   3501 
   3502 	CHECK_SPL();
   3503 
   3504 #ifdef	DIAGNOSTIC
   3505 	if (
   3506 #if IS_SUN3
   3507 	    pmap != kernel_pmap &&
   3508 #endif
   3509 	    pmap->pm_ctxnum != get_context()) {
   3510 		panic("pmap_remove_mmu: wrong context");
   3511 	}
   3512 #endif
   3513 
   3514 	segva = sun_trunc_seg(sva);
   3515 	sme = get_segmap(segva);
   3516 
   3517 #ifdef	DIAGNOSTIC
   3518 	/* Make sure it is valid and known. */
   3519 	if (sme == SEGINV)
   3520 		panic("pmap_remove_mmu: SEGINV");
   3521 	if (pmap->pm_segmap && (pmap->pm_segmap[VA_SEGNUM(segva)] != sme))
   3522 		panic("pmap_remove_mmu: incorrect sme, va=0x%lx", segva);
   3523 #endif
   3524 
   3525 	pmegp = pmeg_p(sme);
   3526 	/* have pmeg, will travel */
   3527 
   3528 #ifdef	DIAGNOSTIC
   3529 	/* Make sure we own the pmeg, right va, etc. */
   3530 	if ((pmegp->pmeg_va != segva) ||
   3531 	    (pmegp->pmeg_owner != pmap) ||
   3532 	    (pmegp->pmeg_version != pmap->pm_version))
   3533 	{
   3534 		panic("pmap_remove_mmu: bad pmeg=%p", pmegp);
   3535 	}
   3536 	if (pmegp->pmeg_vpages < 0)
   3537 		panic("pmap_remove_mmu: npages corrupted");
   3538 	if (pmegp->pmeg_vpages == 0)
   3539 		panic("pmap_remove_mmu: no valid pages?");
   3540 #endif
   3541 
   3542 #ifdef	HAVECACHE
   3543 	if (cache_size) {
   3544 		/*
   3545 		 * If the range to be removed is larger than the cache,
   3546 		 * it will be cheaper to flush this segment entirely.
   3547 		 */
   3548 		if (cache_size < (eva - sva)) {
   3549 			/* cheaper to flush whole segment */
   3550 			cache_flush_segment(segva);
   3551 		} else {
   3552 			flush_by_page = 1;
   3553 		}
   3554 	}
   3555 #endif
   3556 
   3557 	/* Invalidate the PTEs in the given range. */
   3558 	for (pgva = sva; pgva < eva; pgva += PAGE_SIZE) {
   3559 		pte = get_pte(pgva);
   3560 		if (pte & PG_VALID) {
   3561 #ifdef	HAVECACHE
   3562 			if (flush_by_page) {
   3563 				cache_flush_page(pgva);
   3564 				/* Get fresh mod/ref bits from write-back. */
   3565 				pte = get_pte(pgva);
   3566 			}
   3567 #endif
   3568 			if (IS_MAIN_MEM(pte)) {
   3569 				save_modref_bits(pte);
   3570 				pv_unlink(pmap, pte, pgva);
   3571 			}
   3572 #ifdef	PMAP_DEBUG
   3573 			if ((pmap_debug & PMD_SETPTE) ||
   3574 			    (pgva == pmap_db_watchva)) {
   3575 				printf("pmap: set_pte pmap=%p va=0x%lx"
   3576 				       " old=0x%x new=0x%x (rrmmu)\n",
   3577 				       pmap, pgva, pte, PG_INVAL);
   3578 			}
   3579 #endif
   3580 			set_pte(pgva, PG_INVAL);
   3581 			KASSERT(pmegp->pmeg_vpages > 0);
   3582 			pmegp->pmeg_vpages--;
   3583 		}
   3584 	}
   3585 
   3586 	KASSERT(pmegp->pmeg_vpages >= 0);
   3587 	if (pmegp->pmeg_vpages == 0) {
   3588 		/* We are done with this pmeg. */
   3589 #ifdef	PMAP_DEBUG
   3590 		if (is_pmeg_wired(pmegp)) {
   3591 			if (pmap_debug & PMD_WIRING) {
   3592 				db_printf("pmap: removing wired pmeg: %p\n",
   3593 					  pmegp);
   3594 				Debugger();
   3595 			}
   3596 		}
   3597 
   3598 		if (pmap_debug & PMD_SEGMAP) {
   3599 			printf("pmap: set_segmap ctx=%d v=0x%lx old=0x%x "
   3600 			       "new=ff (rm)\n",
   3601 			       pmap->pm_ctxnum, segva, pmegp->pmeg_index);
   3602 		}
   3603 		pmeg_verify_empty(segva);
   3604 #endif /* PMAP_DEBUG */
   3605 
   3606 		/* Remove it from the MMU. */
   3607 		if (kernel_pmap == pmap) {
   3608 			/* Did cache flush above. */
   3609 			set_kernel_segmap(segva, SEGINV);
   3610 		} else {
   3611 			/* Did cache flush above. */
   3612 			set_segmap(segva, SEGINV);
   3613 		}
   3614 		pmap->pm_segmap[VA_SEGNUM(segva)] = SEGINV;
   3615 		/* Now, put it on the free list. */
   3616 		pmeg_free(pmegp);
   3617 	}
   3618 }
   3619 
   3620 /*
   3621  * Remove some mappings, all in one PMEG,
   3622  * where it is not currently in any context.
   3623  */
   3624 void
   3625 pmap_remove_noctx(pmap_t pmap, vaddr_t sva, vaddr_t eva)
   3626 {
   3627 	pmeg_t pmegp;
   3628 	int old_ctx, pte, sme, segnum;
   3629 	vaddr_t pgva, segva;
   3630 
   3631 	CHECK_SPL();
   3632 
   3633 #ifdef	DIAGNOSTIC
   3634 	/* Kernel always in a context. */
   3635 	if (pmap == kernel_pmap)
   3636 		panic("pmap_remove_noctx: kernel_pmap");
   3637 	if (pmap->pm_segmap == NULL)
   3638 		panic("pmap_remove_noctx: null segmap");
   3639 #endif
   3640 
   3641 	segva = sun_trunc_seg(sva);
   3642 	segnum = VA_SEGNUM(segva);
   3643 	sme = pmap->pm_segmap[segnum];
   3644 	if (sme == SEGINV)
   3645 		return;
   3646 	pmegp = pmeg_p(sme);
   3647 
   3648 	/*
   3649 	 * Borrow the EMPTY_CONTEXT so we can access the PMEG
   3650 	 * at its normal virtual address.
   3651 	 *
   3652 	 * (On Sun-2, use the temporary segment.)
   3653 	 */
   3654 	old_ctx = get_context();
   3655 #if IS_SUN2
   3656 	set_context(KERNEL_CONTEXT);
   3657 #ifdef DIAGNOSTIC
   3658 	if (temp_seg_inuse)
   3659 		panic("pmap_remove_noctx: temp_seg_inuse");
   3660 	temp_seg_inuse++;
   3661 #endif
   3662 	set_segmap(temp_seg_va, sme);
   3663 	sva += (temp_seg_va - segva);
   3664 	eva += (temp_seg_va - segva);
   3665 #else /* IS_SUN3 */
   3666 	set_context(EMPTY_CONTEXT);
   3667 	set_segmap(segva, sme);
   3668 #endif /* IS_SUN2 */
   3669 
   3670 	/* Invalidate the PTEs in the given range. */
   3671 	for (pgva = sva; pgva < eva; pgva += PAGE_SIZE) {
   3672 		pte = get_pte(pgva);
   3673 		if (pte & PG_VALID) {
   3674 			/* No cache flush needed. */
   3675 			if (IS_MAIN_MEM(pte)) {
   3676 				save_modref_bits(pte);
   3677 				pv_unlink(pmap, pte,
   3678 #if IS_SUN2
   3679 					  pgva - (temp_seg_va - segva)
   3680 #else
   3681 					  pgva
   3682 #endif
   3683 				);
   3684 			}
   3685 #ifdef	PMAP_DEBUG
   3686 			if ((pmap_debug & PMD_SETPTE) ||
   3687 			    (pgva == pmap_db_watchva)) {
   3688 				printf("pmap: set_pte pmap=%p va=0x%lx"
   3689 				       " old=0x%x new=0x%x (rrncx)\n",
   3690 				       pmap, pgva, pte, PG_INVAL);
   3691 			}
   3692 #endif
   3693 			set_pte(pgva, PG_INVAL);
   3694 			KASSERT(pmegp->pmeg_vpages > 0);
   3695 			pmegp->pmeg_vpages--;
   3696 		}
   3697 	}
   3698 
   3699 	/*
   3700 	 * Make the EMPTY_CONTEXT really empty again, and
   3701 	 * restore the previous context.
   3702 	 *
   3703 	 * (On Sun-2, release the temporary segment.)
   3704 	 */
   3705 #if IS_SUN2
   3706 	set_segmap(temp_seg_va, SEGINV);
   3707 #ifdef DIAGNOSTIC
   3708 	temp_seg_inuse--;
   3709 #endif
   3710 #else /* IS_SUN3 */
   3711 	set_segmap(segva, SEGINV);
   3712 #endif /* IS_SUN2 */
   3713 	set_context(old_ctx);
   3714 
   3715 	KASSERT(pmegp->pmeg_vpages >= 0);
   3716 	if (pmegp->pmeg_vpages == 0) {
   3717 		/* We are done with this pmeg. */
   3718 		if (is_pmeg_wired(pmegp)) {
   3719 #ifdef	PMAP_DEBUG
   3720 			if (pmap_debug & PMD_WIRING) {
   3721 				db_printf("pmap: removing wired pmeg: %p\n",
   3722 					  pmegp);
   3723 				Debugger();
   3724 			}
   3725 #endif	/* PMAP_DEBUG */
   3726 		}
   3727 
   3728 		pmap->pm_segmap[segnum] = SEGINV;
   3729 		pmeg_free(pmegp);
   3730 	}
   3731 }
   3732 
   3733 
   3734 /*
   3735  * Count resident pages in this pmap.
   3736  * See: kern_sysctl.c:pmap_resident_count
   3737  */
   3738 segsz_t
   3739 pmap_resident_pages(pmap_t pmap)
   3740 {
   3741 	int i, sme, pages;
   3742 	pmeg_t pmeg;
   3743 
   3744 	if (pmap->pm_segmap == 0)
   3745 		return (0);
   3746 
   3747 	pages = 0;
   3748 	for (i = 0; i < NUSEG; i++) {
   3749 		sme = pmap->pm_segmap[i];
   3750 		if (sme != SEGINV) {
   3751 			pmeg = pmeg_p(sme);
   3752 			pages += pmeg->pmeg_vpages;
   3753 		}
   3754 	}
   3755 	return (pages);
   3756 }
   3757 
   3758 /*
   3759  * Count wired pages in this pmap.
   3760  * See vm_mmap.c:pmap_wired_count
   3761  */
   3762 segsz_t
   3763 pmap_wired_pages(pmap_t pmap)
   3764 {
   3765 	int i, mask, sme, pages;
   3766 	pmeg_t pmeg;
   3767 
   3768 	if (pmap->pm_segmap == 0)
   3769 		return (0);
   3770 
   3771 	pages = 0;
   3772 	for (i = 0; i < NUSEG; i++) {
   3773 		sme = pmap->pm_segmap[i];
   3774 		if (sme != SEGINV) {
   3775 			pmeg = pmeg_p(sme);
   3776 			mask = 0x8000;
   3777 			do {
   3778 				if (pmeg->pmeg_wired & mask)
   3779 					pages++;
   3780 				mask = (mask >> 1);
   3781 			} while (mask);
   3782 		}
   3783 	}
   3784 	return (pages);
   3785 }
   3786 
   3787 
   3788 /*
   3789  *	pmap_copy_page copies the specified (machine independent)
   3790  *	page by mapping the page into virtual memory and using
   3791  *	bcopy to copy the page, one machine dependent page at a
   3792  *	time.
   3793  */
   3794 void
   3795 pmap_copy_page(paddr_t src, paddr_t dst)
   3796 {
   3797 	int pte;
   3798 	int s;
   3799 
   3800 	s = splvm();
   3801 
   3802 #ifdef	PMAP_DEBUG
   3803 	if (pmap_debug & PMD_COW)
   3804 		printf("pmap_copy_page: 0x%lx -> 0x%lx\n", src, dst);
   3805 #endif
   3806 
   3807 #ifdef DIAGNOSTIC
   3808 	if (tmp_vpages_inuse)
   3809 		panic("pmap_copy_page: vpages inuse");
   3810 	tmp_vpages_inuse++;
   3811 #endif
   3812 
   3813 	vaddr_t srcva = curcpu()->ci_tmpva_src;
   3814 	vaddr_t dstva = curcpu()->ci_tmpva_dst;
   3815 
   3816 	kernel_context_enter();
   3817 
   3818 	/* PG_PERM is short for (PG_VALID|PG_WRITE|PG_SYSTEM|PG_NC) */
   3819 	/* All mappings to vmp_vpages are non-cached, so no flush. */
   3820 	pte = PG_PERM | PA_PGNUM(src);
   3821 	set_pte(srcva, pte);
   3822 	pte = PG_PERM | PA_PGNUM(dst);
   3823 	set_pte(dstva, pte);
   3824 	copypage((char *) srcva, (char *) dstva);
   3825 	set_pte(srcva, PG_INVAL);
   3826 	set_pte(dstva, PG_INVAL);
   3827 
   3828 	kernel_context_exit();
   3829 
   3830 #ifdef DIAGNOSTIC
   3831 	tmp_vpages_inuse--;
   3832 #endif
   3833 
   3834 	splx(s);
   3835 }
   3836 
   3837 /*
   3838  *	pmap_zero_page zeros the specified (machine independent)
   3839  *	page by mapping the page into virtual memory and using
   3840  *	bzero to clear its contents, one machine dependent page
   3841  *	at a time.
   3842  */
   3843 void
   3844 pmap_zero_page(paddr_t pa)
   3845 {
   3846 	int pte;
   3847 	int s;
   3848 
   3849 	s = splvm();
   3850 
   3851 #ifdef	PMAP_DEBUG
   3852 	if (pmap_debug & PMD_COW)
   3853 		printf("pmap_zero_page: 0x%lx\n", pa);
   3854 #endif
   3855 
   3856 #ifdef DIAGNOSTIC
   3857 	if (tmp_vpages_inuse)
   3858 		panic("pmap_zero_page: vpages inuse");
   3859 	tmp_vpages_inuse++;
   3860 #endif
   3861 
   3862 	vaddr_t dstva = curcpu()->ci_tmpva_dst;
   3863 
   3864 	kernel_context_enter();
   3865 
   3866 	/* PG_PERM is short for (PG_VALID|PG_WRITE|PG_SYSTEM|PG_NC) */
   3867 	/* All mappings to vmp_vpages are non-cached, so no flush. */
   3868 	pte = PG_PERM | PA_PGNUM(pa);
   3869 	set_pte(dstva, pte);
   3870 	zeropage((char *) dstva);
   3871 	set_pte(dstva, PG_INVAL);
   3872 
   3873 	kernel_context_exit();
   3874 
   3875 #ifdef DIAGNOSTIC
   3876 	tmp_vpages_inuse--;
   3877 #endif
   3878 
   3879 	splx(s);
   3880 }
   3881 
   3882 /*
   3883  * Find first virtual address >= *va that is
   3884  * least likely to cause cache aliases.
   3885  * (This will just seg-align mappings.)
   3886  */
   3887 void
   3888 pmap_prefer(vaddr_t fo, vaddr_t *va, int td)
   3889 {
   3890 	long d;
   3891 
   3892 	d = fo - *va;
   3893 	d &= SEGOFSET;
   3894 	if (d == 0) {
   3895 		return;
   3896 	}
   3897 	if (td) {
   3898 		*va -= SEGOFSET + 1;
   3899 	}
   3900 	*va += d;
   3901 }
   3902 
   3903 /*
   3904  * Fill in the sun-specific part of the kernel core header
   3905  * for dumpsys().  (See machdep.c for the rest.)
   3906  */
   3907 void
   3908 pmap_kcore_hdr(
   3909 #if IS_SUN2
   3910     struct sun2_kcore_hdr *sh
   3911 #else
   3912     struct sun3_kcore_hdr *sh
   3913 #endif
   3914 )
   3915 {
   3916 	vaddr_t va;
   3917 	u_char *cp, *ep;
   3918 
   3919 	sh->segshift = SEGSHIFT;
   3920 	sh->pg_frame = PG_FRAME;
   3921 	sh->pg_valid = PG_VALID;
   3922 
   3923 	kernel_context_enter();
   3924 
   3925 	/* Copy the kernel segmap (256 bytes). */
   3926 #if IS_SUN2
   3927 	va = KERNBASE;
   3928 #else
   3929 	va = KERNBASE3;
   3930 #endif
   3931 	cp = sh->ksegmap;
   3932 	ep = cp + sizeof(sh->ksegmap);
   3933 	do {
   3934 		*cp = get_segmap(va);
   3935 		va += NBSG;
   3936 		cp++;
   3937 	} while (cp < ep);
   3938 
   3939 	kernel_context_exit();
   3940 }
   3941 
   3942 /*
   3943  * Copy the pagemap RAM into the passed buffer (one page)
   3944  * starting at OFF in the pagemap RAM.
   3945  */
   3946 void
   3947 pmap_get_pagemap(int *pt, int off)
   3948 {
   3949 	vaddr_t va, va_end;
   3950 	int sme, sme_end;	/* SegMap Entry numbers */
   3951 
   3952 #if IS_SUN2
   3953 	sme = (off / (NPAGSEG * sizeof(*pt)));		/* PMEG to start on */
   3954 	sme_end =
   3955 	    sme + (PAGE_SIZE / (NPAGSEG * sizeof(*pt)));/* where to stop */
   3956 #else
   3957 	sme = (off >> 6);				/* PMEG to start on */
   3958 	sme_end = sme + 128;				/* where to stop */
   3959 #endif /* IS_SUN2 */
   3960 	va_end = temp_seg_va + NBSG;
   3961 
   3962 	kernel_context_enter();
   3963 	do {
   3964 		set_segmap(temp_seg_va, sme);
   3965 		va = temp_seg_va;
   3966 		do {
   3967 			*pt++ = get_pte(va);
   3968 			va += PAGE_SIZE;
   3969 		} while (va < va_end);
   3970 		sme++;
   3971 	} while (sme < sme_end);
   3972 	set_segmap(temp_seg_va, SEGINV);
   3973 	kernel_context_exit();
   3974 }
   3975 
   3976 
   3977 /*
   3978  * Helper functions for changing unloaded PMEGs
   3979  */
   3980 
   3981 #ifdef DIAGNOSTIC
   3982 static int temp_seg_inuse;
   3983 #endif
   3984 
   3985 static int
   3986 get_pte_pmeg(int pmeg_num, int page_num)
   3987 {
   3988 	vaddr_t va;
   3989 	int pte;
   3990 
   3991 	CHECK_SPL();
   3992 #ifdef DIAGNOSTIC
   3993 	if (temp_seg_inuse)
   3994 		panic("get_pte_pmeg: temp_seg_inuse");
   3995 	temp_seg_inuse++;
   3996 #endif
   3997 
   3998 	kernel_context_enter();
   3999 
   4000 	va = temp_seg_va;
   4001 	set_segmap(temp_seg_va, pmeg_num);
   4002 	va += PAGE_SIZE*page_num;
   4003 	pte = get_pte(va);
   4004 	set_segmap(temp_seg_va, SEGINV);
   4005 
   4006 	kernel_context_exit();
   4007 
   4008 #ifdef DIAGNOSTIC
   4009 	temp_seg_inuse--;
   4010 #endif
   4011 	return pte;
   4012 }
   4013 
   4014 static void
   4015 set_pte_pmeg(int pmeg_num, int page_num, int pte)
   4016 {
   4017 	vaddr_t va;
   4018 
   4019 	CHECK_SPL();
   4020 #ifdef DIAGNOSTIC
   4021 	if (temp_seg_inuse)
   4022 		panic("set_pte_pmeg: temp_seg_inuse");
   4023 	temp_seg_inuse++;
   4024 #endif
   4025 
   4026 	kernel_context_enter();
   4027 
   4028 	/* We never access data in temp_seg_va so no need to flush. */
   4029 	va = temp_seg_va;
   4030 	set_segmap(temp_seg_va, pmeg_num);
   4031 	va += PAGE_SIZE*page_num;
   4032 	set_pte(va, pte);
   4033 	set_segmap(temp_seg_va, SEGINV);
   4034 
   4035 	kernel_context_exit();
   4036 
   4037 #ifdef DIAGNOSTIC
   4038 	temp_seg_inuse--;
   4039 #endif
   4040 }
   4041 
   4042 /*
   4043  *	Routine:        pmap_procwr
   4044  *
   4045  *	Function:
   4046  *		Synchronize caches corresponding to [addr, addr+len) in p.
   4047  */
   4048 void
   4049 pmap_procwr(struct proc *p, vaddr_t va, size_t len)
   4050 {
   4051 #if IS_SUN3
   4052 	(void)cachectl1(0x80000004, va, len, p);
   4053 #endif
   4054 }
   4055 
   4056 #if defined(DDB) || defined(KGDB) || defined(M68K_FPCOPROC)
   4057 /*
   4058  *	Routine:	pmap_write_text_enter
   4059  *
   4060  *	Function:
   4061  *		Temporarily map a page of kernel text read-write for the
   4062  *		kernel debugger.
   4063  */
   4064 bool
   4065 pmap_write_text_enter(vaddr_t pgva, struct pmap_write_text_context *ctx)
   4066 {
   4067 	int oldpte, tmppte;
   4068 
   4069 #ifdef HAVECACHE
   4070 	if (cache_size) {
   4071 		cache_flush_page(pgva);
   4072 	}
   4073 #endif
   4074 
   4075 	kernel_context_enter();
   4076 
   4077 	oldpte = get_pte(pgva);
   4078 	if ((oldpte & PG_VALID) == 0) {
   4079 		return false;
   4080 	}
   4081 
   4082 	/* Make the pte writable and non-cached. */
   4083 	tmppte = oldpte;
   4084 	tmppte |= (PG_WRITE | PG_NC);
   4085 	set_pte(pgva, tmppte);
   4086 
   4087 	kernel_context_exit();
   4088 
   4089 	ctx->pgva = pgva;
   4090 	ctx->opte = oldpte;
   4091 
   4092 	return true;
   4093 }
   4094 
   4095 /*
   4096  *	Routine:	pmap_write_text_exit
   4097  *
   4098  *	Function:
   4099  *		Undo the effects of pmap_write_text_enter().
   4100  */
   4101 void
   4102 pmap_write_text_exit(struct pmap_write_text_context *ctx)
   4103 {
   4104 	kernel_context_enter();
   4105 	set_pte(ctx->pgva, ctx->opte);
   4106 	kernel_context_exit();
   4107 }
   4108 #endif /* DDB || KGDB || M68K_FPCOPROC */
   4109 
   4110 #ifdef	PMAP_DEBUG
   4111 /* Things to call from the debugger. */
   4112 
   4113 void
   4114 pmap_print(pmap_t pmap)
   4115 {
   4116 	db_printf(" pm_ctxnum=%d\n", pmap->pm_ctxnum);
   4117 	db_printf(" pm_version=0x%x\n", pmap->pm_version);
   4118 	db_printf(" pm_segmap=%p\n", pmap->pm_segmap);
   4119 }
   4120 
   4121 void
   4122 pmeg_print(pmeg_t pmegp)
   4123 {
   4124 	db_printf("link_next=%p  link_prev=%p\n",
   4125 		  TAILQ_NEXT(pmegp, pmeg_link),
   4126 		  TAILQ_PREV(pmegp, pmeg_tailq, pmeg_link));
   4127 	db_printf("index=0x%x owner=%p own_vers=0x%x\n",
   4128 		  pmegp->pmeg_index, pmegp->pmeg_owner, pmegp->pmeg_version);
   4129 	db_printf("va=0x%lx wired=0x%x reserved=0x%x vpgs=0x%x qstate=0x%x\n",
   4130 		  pmegp->pmeg_va, pmegp->pmeg_wired,
   4131 		  pmegp->pmeg_reserved, pmegp->pmeg_vpages,
   4132 		  pmegp->pmeg_qstate);
   4133 }
   4134 
   4135 void
   4136 pv_print(paddr_t pa)
   4137 {
   4138 	pv_entry_t pv;
   4139 	int idx;
   4140 
   4141 	idx = PA_PGNUM(pa);
   4142 	if (idx >= physmem) {
   4143 		db_printf("bad address\n");
   4144 		return;
   4145 	}
   4146 	db_printf("pa=0x%lx, flags=0x%x\n",
   4147 		  pa, pv_flags_tbl[idx]);
   4148 
   4149 	pv = pv_head_tbl[idx];
   4150 	while (pv) {
   4151 		db_printf(" pv_entry %p pmap %p va 0x%lx next %p\n",
   4152 			  pv, pv->pv_pmap, pv->pv_va, pv->pv_next);
   4153 		pv = pv->pv_next;
   4154 	}
   4155 }
   4156 #endif	/* PMAP_DEBUG */
   4157