Home | History | Annotate | Line # | Download | only in arm32
pmap.c revision 1.164.12.3
      1  1.164.12.3      matt /*	$NetBSD: pmap.c,v 1.164.12.3 2007/11/06 23:15:00 matt Exp $	*/
      2        1.12     chris 
      3        1.12     chris /*
      4       1.134   thorpej  * Copyright 2003 Wasabi Systems, Inc.
      5       1.134   thorpej  * All rights reserved.
      6       1.134   thorpej  *
      7       1.134   thorpej  * Written by Steve C. Woodford for Wasabi Systems, Inc.
      8       1.134   thorpej  *
      9       1.134   thorpej  * Redistribution and use in source and binary forms, with or without
     10       1.134   thorpej  * modification, are permitted provided that the following conditions
     11       1.134   thorpej  * are met:
     12       1.134   thorpej  * 1. Redistributions of source code must retain the above copyright
     13       1.134   thorpej  *    notice, this list of conditions and the following disclaimer.
     14       1.134   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.134   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16       1.134   thorpej  *    documentation and/or other materials provided with the distribution.
     17       1.134   thorpej  * 3. All advertising materials mentioning features or use of this software
     18       1.134   thorpej  *    must display the following acknowledgement:
     19       1.134   thorpej  *      This product includes software developed for the NetBSD Project by
     20       1.134   thorpej  *      Wasabi Systems, Inc.
     21       1.134   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22       1.134   thorpej  *    or promote products derived from this software without specific prior
     23       1.134   thorpej  *    written permission.
     24       1.134   thorpej  *
     25       1.134   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26       1.134   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27       1.134   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28       1.134   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29       1.134   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30       1.134   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31       1.134   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32       1.134   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33       1.134   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34       1.134   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35       1.134   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36       1.134   thorpej  */
     37       1.134   thorpej 
     38       1.134   thorpej /*
     39       1.134   thorpej  * Copyright (c) 2002-2003 Wasabi Systems, Inc.
     40        1.12     chris  * Copyright (c) 2001 Richard Earnshaw
     41       1.119     chris  * Copyright (c) 2001-2002 Christopher Gilbert
     42        1.12     chris  * All rights reserved.
     43        1.12     chris  *
     44        1.12     chris  * 1. Redistributions of source code must retain the above copyright
     45        1.12     chris  *    notice, this list of conditions and the following disclaimer.
     46        1.12     chris  * 2. Redistributions in binary form must reproduce the above copyright
     47        1.12     chris  *    notice, this list of conditions and the following disclaimer in the
     48        1.12     chris  *    documentation and/or other materials provided with the distribution.
     49        1.12     chris  * 3. The name of the company nor the name of the author may be used to
     50        1.12     chris  *    endorse or promote products derived from this software without specific
     51        1.12     chris  *    prior written permission.
     52        1.12     chris  *
     53        1.12     chris  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     54        1.12     chris  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     55        1.12     chris  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     56        1.12     chris  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     57        1.12     chris  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     58        1.12     chris  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     59        1.12     chris  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60        1.12     chris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61        1.12     chris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62        1.12     chris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63        1.12     chris  * SUCH DAMAGE.
     64        1.12     chris  */
     65         1.1      matt 
     66         1.1      matt /*-
     67         1.1      matt  * Copyright (c) 1999 The NetBSD Foundation, Inc.
     68         1.1      matt  * All rights reserved.
     69         1.1      matt  *
     70         1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
     71         1.1      matt  * by Charles M. Hannum.
     72         1.1      matt  *
     73         1.1      matt  * Redistribution and use in source and binary forms, with or without
     74         1.1      matt  * modification, are permitted provided that the following conditions
     75         1.1      matt  * are met:
     76         1.1      matt  * 1. Redistributions of source code must retain the above copyright
     77         1.1      matt  *    notice, this list of conditions and the following disclaimer.
     78         1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     79         1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     80         1.1      matt  *    documentation and/or other materials provided with the distribution.
     81         1.1      matt  * 3. All advertising materials mentioning features or use of this software
     82         1.1      matt  *    must display the following acknowledgement:
     83         1.1      matt  *        This product includes software developed by the NetBSD
     84         1.1      matt  *        Foundation, Inc. and its contributors.
     85         1.1      matt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     86         1.1      matt  *    contributors may be used to endorse or promote products derived
     87         1.1      matt  *    from this software without specific prior written permission.
     88         1.1      matt  *
     89         1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     90         1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     91         1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     92         1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     93         1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     94         1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     95         1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     96         1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     97         1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     98         1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     99         1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
    100         1.1      matt  */
    101         1.1      matt 
    102         1.1      matt /*
    103         1.1      matt  * Copyright (c) 1994-1998 Mark Brinicombe.
    104         1.1      matt  * Copyright (c) 1994 Brini.
    105         1.1      matt  * All rights reserved.
    106         1.1      matt  *
    107         1.1      matt  * This code is derived from software written for Brini by Mark Brinicombe
    108         1.1      matt  *
    109         1.1      matt  * Redistribution and use in source and binary forms, with or without
    110         1.1      matt  * modification, are permitted provided that the following conditions
    111         1.1      matt  * are met:
    112         1.1      matt  * 1. Redistributions of source code must retain the above copyright
    113         1.1      matt  *    notice, this list of conditions and the following disclaimer.
    114         1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
    115         1.1      matt  *    notice, this list of conditions and the following disclaimer in the
    116         1.1      matt  *    documentation and/or other materials provided with the distribution.
    117         1.1      matt  * 3. All advertising materials mentioning features or use of this software
    118         1.1      matt  *    must display the following acknowledgement:
    119         1.1      matt  *	This product includes software developed by Mark Brinicombe.
    120         1.1      matt  * 4. The name of the author may not be used to endorse or promote products
    121         1.1      matt  *    derived from this software without specific prior written permission.
    122         1.1      matt  *
    123         1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    124         1.1      matt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    125         1.1      matt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    126         1.1      matt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    127         1.1      matt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    128         1.1      matt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    129         1.1      matt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    130         1.1      matt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    131         1.1      matt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    132         1.1      matt  *
    133         1.1      matt  * RiscBSD kernel project
    134         1.1      matt  *
    135         1.1      matt  * pmap.c
    136         1.1      matt  *
    137         1.1      matt  * Machine dependant vm stuff
    138         1.1      matt  *
    139         1.1      matt  * Created      : 20/09/94
    140         1.1      matt  */
    141         1.1      matt 
    142         1.1      matt /*
    143  1.164.12.2      matt  * armv6 and VIPT cache support by 3am Software Foundry,
    144  1.164.12.2      matt  * Copyright (c) 2007 Danger Inc
    145  1.164.12.2      matt  */
    146  1.164.12.2      matt 
    147  1.164.12.2      matt /*
    148         1.1      matt  * Performance improvements, UVM changes, overhauls and part-rewrites
    149         1.1      matt  * were contributed by Neil A. Carson <neil (at) causality.com>.
    150         1.1      matt  */
    151         1.1      matt 
    152         1.1      matt /*
    153       1.134   thorpej  * Overhauled again to speedup the pmap, use MMU Domains so that L1 tables
    154       1.134   thorpej  * can be shared, and re-work the KVM layout, by Steve Woodford of Wasabi
    155       1.134   thorpej  * Systems, Inc.
    156       1.134   thorpej  *
    157       1.134   thorpej  * There are still a few things outstanding at this time:
    158       1.134   thorpej  *
    159       1.134   thorpej  *   - There are some unresolved issues for MP systems:
    160       1.134   thorpej  *
    161       1.134   thorpej  *     o The L1 metadata needs a lock, or more specifically, some places
    162       1.134   thorpej  *       need to acquire an exclusive lock when modifying L1 translation
    163       1.134   thorpej  *       table entries.
    164       1.134   thorpej  *
    165       1.134   thorpej  *     o When one cpu modifies an L1 entry, and that L1 table is also
    166       1.134   thorpej  *       being used by another cpu, then the latter will need to be told
    167       1.134   thorpej  *       that a tlb invalidation may be necessary. (But only if the old
    168       1.134   thorpej  *       domain number in the L1 entry being over-written is currently
    169       1.134   thorpej  *       the active domain on that cpu). I guess there are lots more tlb
    170       1.134   thorpej  *       shootdown issues too...
    171       1.134   thorpej  *
    172       1.134   thorpej  *     o If the vector_page is at 0x00000000 instead of 0xffff0000, then
    173       1.134   thorpej  *       MP systems will lose big-time because of the MMU domain hack.
    174       1.134   thorpej  *       The only way this can be solved (apart from moving the vector
    175       1.134   thorpej  *       page to 0xffff0000) is to reserve the first 1MB of user address
    176       1.134   thorpej  *       space for kernel use only. This would require re-linking all
    177       1.134   thorpej  *       applications so that the text section starts above this 1MB
    178       1.134   thorpej  *       boundary.
    179       1.134   thorpej  *
    180       1.134   thorpej  *     o Tracking which VM space is resident in the cache/tlb has not yet
    181       1.134   thorpej  *       been implemented for MP systems.
    182       1.134   thorpej  *
    183       1.134   thorpej  *     o Finally, there is a pathological condition where two cpus running
    184       1.134   thorpej  *       two separate processes (not lwps) which happen to share an L1
    185       1.134   thorpej  *       can get into a fight over one or more L1 entries. This will result
    186       1.134   thorpej  *       in a significant slow-down if both processes are in tight loops.
    187         1.1      matt  */
    188         1.1      matt 
    189         1.1      matt /*
    190         1.1      matt  * Special compilation symbols
    191         1.1      matt  * PMAP_DEBUG		- Build in pmap_debug_level code
    192         1.1      matt  */
    193       1.134   thorpej 
    194         1.1      matt /* Include header files */
    195         1.1      matt 
    196       1.134   thorpej #include "opt_cpuoptions.h"
    197         1.1      matt #include "opt_pmap_debug.h"
    198         1.1      matt #include "opt_ddb.h"
    199       1.137    martin #include "opt_lockdebug.h"
    200       1.137    martin #include "opt_multiprocessor.h"
    201         1.1      matt 
    202         1.1      matt #include <sys/types.h>
    203         1.1      matt #include <sys/param.h>
    204         1.1      matt #include <sys/kernel.h>
    205         1.1      matt #include <sys/systm.h>
    206         1.1      matt #include <sys/proc.h>
    207         1.1      matt #include <sys/malloc.h>
    208         1.1      matt #include <sys/user.h>
    209        1.10     chris #include <sys/pool.h>
    210        1.16     chris #include <sys/cdefs.h>
    211        1.16     chris 
    212         1.1      matt #include <uvm/uvm.h>
    213         1.1      matt 
    214         1.1      matt #include <machine/bus.h>
    215         1.1      matt #include <machine/pmap.h>
    216         1.1      matt #include <machine/pcb.h>
    217         1.1      matt #include <machine/param.h>
    218        1.32   thorpej #include <arm/arm32/katelib.h>
    219        1.16     chris 
    220  1.164.12.3      matt __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.164.12.3 2007/11/06 23:15:00 matt Exp $");
    221       1.116  jdolecek 
    222         1.1      matt #ifdef PMAP_DEBUG
    223       1.140      matt 
    224       1.140      matt /* XXX need to get rid of all refs to this */
    225       1.134   thorpej int pmap_debug_level = 0;
    226        1.17     chris 
    227        1.17     chris /*
    228        1.17     chris  * for switching to potentially finer grained debugging
    229        1.17     chris  */
    230        1.17     chris #define	PDB_FOLLOW	0x0001
    231        1.17     chris #define	PDB_INIT	0x0002
    232        1.17     chris #define	PDB_ENTER	0x0004
    233        1.17     chris #define	PDB_REMOVE	0x0008
    234        1.17     chris #define	PDB_CREATE	0x0010
    235        1.17     chris #define	PDB_PTPAGE	0x0020
    236        1.48     chris #define	PDB_GROWKERN	0x0040
    237        1.17     chris #define	PDB_BITS	0x0080
    238        1.17     chris #define	PDB_COLLECT	0x0100
    239        1.17     chris #define	PDB_PROTECT	0x0200
    240        1.48     chris #define	PDB_MAP_L1	0x0400
    241        1.17     chris #define	PDB_BOOTSTRAP	0x1000
    242        1.17     chris #define	PDB_PARANOIA	0x2000
    243        1.17     chris #define	PDB_WIRING	0x4000
    244        1.17     chris #define	PDB_PVDUMP	0x8000
    245       1.134   thorpej #define	PDB_VAC		0x10000
    246       1.134   thorpej #define	PDB_KENTER	0x20000
    247       1.134   thorpej #define	PDB_KREMOVE	0x40000
    248  1.164.12.2      matt #define	PDB_EXEC	0x80000
    249        1.17     chris 
    250       1.134   thorpej int debugmap = 1;
    251       1.134   thorpej int pmapdebug = 0;
    252        1.17     chris #define	NPDEBUG(_lev_,_stat_) \
    253        1.17     chris 	if (pmapdebug & (_lev_)) \
    254        1.17     chris         	((_stat_))
    255        1.17     chris 
    256         1.1      matt #else	/* PMAP_DEBUG */
    257        1.48     chris #define NPDEBUG(_lev_,_stat_) /* Nothing */
    258         1.1      matt #endif	/* PMAP_DEBUG */
    259         1.1      matt 
    260       1.134   thorpej /*
    261       1.134   thorpej  * pmap_kernel() points here
    262       1.134   thorpej  */
    263         1.1      matt struct pmap     kernel_pmap_store;
    264         1.1      matt 
    265        1.10     chris /*
    266       1.134   thorpej  * Which pmap is currently 'live' in the cache
    267       1.134   thorpej  *
    268       1.134   thorpej  * XXXSCW: Fix for SMP ...
    269        1.48     chris  */
    270  1.164.12.3      matt static pmap_t pmap_recent_user;
    271        1.48     chris 
    272       1.134   thorpej /*
    273       1.134   thorpej  * Pool and cache that pmap structures are allocated from.
    274       1.134   thorpej  * We use a cache to avoid clearing the pm_l2[] array (1KB)
    275       1.134   thorpej  * in pmap_create().
    276       1.134   thorpej  */
    277       1.134   thorpej static struct pool pmap_pmap_pool;
    278       1.134   thorpej static struct pool_cache pmap_pmap_cache;
    279       1.134   thorpej static LIST_HEAD(, pmap) pmap_pmaps;
    280        1.48     chris 
    281        1.48     chris /*
    282       1.134   thorpej  * Pool of PV structures
    283        1.10     chris  */
    284       1.134   thorpej static struct pool pmap_pv_pool;
    285       1.134   thorpej static void *pmap_bootstrap_pv_page_alloc(struct pool *, int);
    286       1.134   thorpej static void pmap_bootstrap_pv_page_free(struct pool *, void *);
    287       1.134   thorpej static struct pool_allocator pmap_bootstrap_pv_allocator = {
    288       1.134   thorpej 	pmap_bootstrap_pv_page_alloc, pmap_bootstrap_pv_page_free
    289       1.134   thorpej };
    290        1.10     chris 
    291       1.134   thorpej /*
    292       1.134   thorpej  * Pool and cache of l2_dtable structures.
    293       1.134   thorpej  * We use a cache to avoid clearing the structures when they're
    294       1.134   thorpej  * allocated. (196 bytes)
    295       1.134   thorpej  */
    296       1.134   thorpej static struct pool pmap_l2dtable_pool;
    297       1.134   thorpej static struct pool_cache pmap_l2dtable_cache;
    298       1.134   thorpej static vaddr_t pmap_kernel_l2dtable_kva;
    299        1.10     chris 
    300       1.111   thorpej /*
    301       1.134   thorpej  * Pool and cache of L2 page descriptors.
    302       1.134   thorpej  * We use a cache to avoid clearing the descriptor table
    303       1.134   thorpej  * when they're allocated. (1KB)
    304       1.111   thorpej  */
    305       1.134   thorpej static struct pool pmap_l2ptp_pool;
    306       1.134   thorpej static struct pool_cache pmap_l2ptp_cache;
    307       1.134   thorpej static vaddr_t pmap_kernel_l2ptp_kva;
    308       1.134   thorpej static paddr_t pmap_kernel_l2ptp_phys;
    309       1.111   thorpej 
    310  1.164.12.2      matt #ifdef PMAPCOUNT
    311  1.164.12.2      matt #define	PMAP_EVCNT_INITIALIZER(name) \
    312  1.164.12.2      matt 	EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap", name)
    313  1.164.12.2      matt 
    314  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
    315  1.164.12.2      matt static struct evcnt pmap_ev_vac_color_new =
    316  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("new page color");
    317  1.164.12.2      matt static struct evcnt pmap_ev_vac_color_reuse =
    318  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("ok first page color");
    319  1.164.12.2      matt static struct evcnt pmap_ev_vac_color_ok =
    320  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("ok page color");
    321  1.164.12.2      matt static struct evcnt pmap_ev_vac_color_change =
    322  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("change page color");
    323  1.164.12.2      matt static struct evcnt pmap_ev_vac_color_erase =
    324  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("erase page color");
    325  1.164.12.2      matt static struct evcnt pmap_ev_vac_color_none =
    326  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("no page color");
    327  1.164.12.2      matt static struct evcnt pmap_ev_vac_color_restore =
    328  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("restore page color");
    329  1.164.12.2      matt 
    330  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_vac_color_new);
    331  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_vac_color_reuse);
    332  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_vac_color_ok);
    333  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_vac_color_change);
    334  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_vac_color_erase);
    335  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_vac_color_none);
    336  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_vac_color_restore);
    337  1.164.12.2      matt #endif
    338  1.164.12.2      matt 
    339  1.164.12.2      matt static struct evcnt pmap_ev_mappings =
    340  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("pages mapped");
    341  1.164.12.2      matt static struct evcnt pmap_ev_unmappings =
    342  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("pages unmapped");
    343  1.164.12.2      matt static struct evcnt pmap_ev_remappings =
    344  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("pages remapped");
    345  1.164.12.2      matt 
    346  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_mappings);
    347  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_unmappings);
    348  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_remappings);
    349  1.164.12.2      matt 
    350  1.164.12.2      matt static struct evcnt pmap_ev_kernel_mappings =
    351  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("kernel pages mapped");
    352  1.164.12.2      matt static struct evcnt pmap_ev_kernel_unmappings =
    353  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("kernel pages unmapped");
    354  1.164.12.2      matt static struct evcnt pmap_ev_kernel_remappings =
    355  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("kernel pages remapped");
    356  1.164.12.2      matt 
    357  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_kernel_mappings);
    358  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_kernel_unmappings);
    359  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_kernel_remappings);
    360  1.164.12.2      matt 
    361  1.164.12.2      matt static struct evcnt pmap_ev_kenter_mappings =
    362  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("kenter pages mapped");
    363  1.164.12.2      matt static struct evcnt pmap_ev_kenter_unmappings =
    364  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("kenter pages unmapped");
    365  1.164.12.2      matt static struct evcnt pmap_ev_kenter_remappings =
    366  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("kenter pages remapped");
    367  1.164.12.2      matt static struct evcnt pmap_ev_pt_mappings =
    368  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("page table pages mapped");
    369  1.164.12.2      matt 
    370  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_kenter_mappings);
    371  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_kenter_unmappings);
    372  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_kenter_remappings);
    373  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_pt_mappings);
    374  1.164.12.2      matt 
    375  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
    376  1.164.12.2      matt static struct evcnt pmap_ev_exec_mappings =
    377  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages mapped");
    378  1.164.12.2      matt static struct evcnt pmap_ev_exec_cached =
    379  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages cached");
    380  1.164.12.2      matt 
    381  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_mappings);
    382  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_cached);
    383  1.164.12.2      matt 
    384  1.164.12.2      matt static struct evcnt pmap_ev_exec_synced =
    385  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages synced");
    386  1.164.12.2      matt static struct evcnt pmap_ev_exec_synced_map =
    387  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages synced (MP)");
    388  1.164.12.2      matt static struct evcnt pmap_ev_exec_synced_unmap =
    389  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages synced (UM)");
    390  1.164.12.2      matt static struct evcnt pmap_ev_exec_synced_remap =
    391  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages synced (RM)");
    392  1.164.12.2      matt static struct evcnt pmap_ev_exec_synced_clearbit =
    393  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages synced (DG)");
    394  1.164.12.2      matt static struct evcnt pmap_ev_exec_synced_kremove =
    395  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages synced (KU)");
    396  1.164.12.2      matt 
    397  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_synced);
    398  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_map);
    399  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_unmap);
    400  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_remap);
    401  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_clearbit);
    402  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_synced_kremove);
    403  1.164.12.2      matt 
    404  1.164.12.2      matt static struct evcnt pmap_ev_exec_discarded_unmap =
    405  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages discarded (UM)");
    406  1.164.12.2      matt static struct evcnt pmap_ev_exec_discarded_zero =
    407  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages discarded (ZP)");
    408  1.164.12.2      matt static struct evcnt pmap_ev_exec_discarded_copy =
    409  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages discarded (CP)");
    410  1.164.12.2      matt static struct evcnt pmap_ev_exec_discarded_page_protect =
    411  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages discarded (PP)");
    412  1.164.12.2      matt static struct evcnt pmap_ev_exec_discarded_clearbit =
    413  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages discarded (DG)");
    414  1.164.12.2      matt static struct evcnt pmap_ev_exec_discarded_kremove =
    415  1.164.12.2      matt    PMAP_EVCNT_INITIALIZER("exec pages discarded (KU)");
    416  1.164.12.2      matt 
    417  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_unmap);
    418  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_zero);
    419  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_copy);
    420  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_page_protect);
    421  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_clearbit);
    422  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_exec_discarded_kremove);
    423  1.164.12.2      matt #endif /* PMAP_CACHE_VIPT */
    424  1.164.12.2      matt 
    425  1.164.12.2      matt static struct evcnt pmap_ev_updates = PMAP_EVCNT_INITIALIZER("updates");
    426  1.164.12.2      matt static struct evcnt pmap_ev_collects = PMAP_EVCNT_INITIALIZER("collects");
    427  1.164.12.2      matt static struct evcnt pmap_ev_activations = PMAP_EVCNT_INITIALIZER("activations");
    428  1.164.12.2      matt 
    429  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_updates);
    430  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_collects);
    431  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_ev_activations);
    432  1.164.12.2      matt 
    433  1.164.12.2      matt #define	PMAPCOUNT(x)	((void)(pmap_ev_##x.ev_count++))
    434  1.164.12.2      matt #else
    435  1.164.12.2      matt #define	PMAPCOUNT(x)	((void)0)
    436  1.164.12.2      matt #endif
    437  1.164.12.2      matt 
    438       1.134   thorpej /*
    439       1.134   thorpej  * pmap copy/zero page, and mem(5) hook point
    440       1.134   thorpej  */
    441        1.54   thorpej static pt_entry_t *csrc_pte, *cdst_pte;
    442        1.54   thorpej static vaddr_t csrcp, cdstp;
    443         1.1      matt char *memhook;
    444       1.161  christos extern void *msgbufaddr;
    445         1.1      matt 
    446        1.17     chris /*
    447       1.134   thorpej  * Flag to indicate if pmap_init() has done its thing
    448       1.134   thorpej  */
    449       1.159   thorpej bool pmap_initialized;
    450       1.134   thorpej 
    451       1.134   thorpej /*
    452       1.134   thorpej  * Misc. locking data structures
    453        1.17     chris  */
    454         1.1      matt 
    455  1.164.12.3      matt #if 0 /* defined(MULTIPROCESSOR) || defined(LOCKDEBUG) */
    456        1.17     chris static struct lock pmap_main_lock;
    457       1.134   thorpej 
    458        1.17     chris #define PMAP_MAP_TO_HEAD_LOCK() \
    459        1.17     chris      (void) spinlockmgr(&pmap_main_lock, LK_SHARED, NULL)
    460        1.17     chris #define PMAP_MAP_TO_HEAD_UNLOCK() \
    461        1.17     chris      (void) spinlockmgr(&pmap_main_lock, LK_RELEASE, NULL)
    462        1.17     chris #define PMAP_HEAD_TO_MAP_LOCK() \
    463        1.17     chris      (void) spinlockmgr(&pmap_main_lock, LK_EXCLUSIVE, NULL)
    464        1.17     chris #define PMAP_HEAD_TO_MAP_UNLOCK() \
    465       1.134   thorpej      spinlockmgr(&pmap_main_lock, LK_RELEASE, (void *) 0)
    466        1.17     chris #else
    467       1.134   thorpej #define PMAP_MAP_TO_HEAD_LOCK()		/* null */
    468       1.134   thorpej #define PMAP_MAP_TO_HEAD_UNLOCK()	/* null */
    469       1.134   thorpej #define PMAP_HEAD_TO_MAP_LOCK()		/* null */
    470       1.134   thorpej #define PMAP_HEAD_TO_MAP_UNLOCK()	/* null */
    471       1.134   thorpej #endif
    472       1.134   thorpej 
    473       1.134   thorpej #define	pmap_acquire_pmap_lock(pm)			\
    474       1.134   thorpej 	do {						\
    475       1.134   thorpej 		if ((pm) != pmap_kernel())		\
    476       1.134   thorpej 			simple_lock(&(pm)->pm_lock);	\
    477       1.134   thorpej 	} while (/*CONSTCOND*/0)
    478       1.134   thorpej 
    479       1.134   thorpej #define	pmap_release_pmap_lock(pm)			\
    480       1.134   thorpej 	do {						\
    481       1.134   thorpej 		if ((pm) != pmap_kernel())		\
    482       1.134   thorpej 			simple_unlock(&(pm)->pm_lock);	\
    483       1.134   thorpej 	} while (/*CONSTCOND*/0)
    484         1.1      matt 
    485        1.33     chris 
    486        1.69   thorpej /*
    487       1.134   thorpej  * Metadata for L1 translation tables.
    488        1.69   thorpej  */
    489       1.134   thorpej struct l1_ttable {
    490       1.134   thorpej 	/* Entry on the L1 Table list */
    491       1.134   thorpej 	SLIST_ENTRY(l1_ttable) l1_link;
    492         1.1      matt 
    493       1.134   thorpej 	/* Entry on the L1 Least Recently Used list */
    494       1.134   thorpej 	TAILQ_ENTRY(l1_ttable) l1_lru;
    495         1.1      matt 
    496       1.134   thorpej 	/* Track how many domains are allocated from this L1 */
    497       1.134   thorpej 	volatile u_int l1_domain_use_count;
    498         1.1      matt 
    499       1.134   thorpej 	/*
    500       1.134   thorpej 	 * A free-list of domain numbers for this L1.
    501       1.134   thorpej 	 * We avoid using ffs() and a bitmap to track domains since ffs()
    502       1.134   thorpej 	 * is slow on ARM.
    503       1.134   thorpej 	 */
    504       1.134   thorpej 	u_int8_t l1_domain_first;
    505       1.134   thorpej 	u_int8_t l1_domain_free[PMAP_DOMAINS];
    506         1.1      matt 
    507       1.134   thorpej 	/* Physical address of this L1 page table */
    508       1.134   thorpej 	paddr_t l1_physaddr;
    509         1.1      matt 
    510       1.134   thorpej 	/* KVA of this L1 page table */
    511       1.134   thorpej 	pd_entry_t *l1_kva;
    512       1.134   thorpej };
    513         1.1      matt 
    514       1.134   thorpej /*
    515       1.134   thorpej  * Convert a virtual address into its L1 table index. That is, the
    516       1.134   thorpej  * index used to locate the L2 descriptor table pointer in an L1 table.
    517       1.134   thorpej  * This is basically used to index l1->l1_kva[].
    518       1.134   thorpej  *
    519       1.134   thorpej  * Each L2 descriptor table represents 1MB of VA space.
    520       1.134   thorpej  */
    521       1.134   thorpej #define	L1_IDX(va)		(((vaddr_t)(va)) >> L1_S_SHIFT)
    522        1.11     chris 
    523        1.17     chris /*
    524       1.134   thorpej  * L1 Page Tables are tracked using a Least Recently Used list.
    525       1.134   thorpej  *  - New L1s are allocated from the HEAD.
    526       1.134   thorpej  *  - Freed L1s are added to the TAIl.
    527       1.134   thorpej  *  - Recently accessed L1s (where an 'access' is some change to one of
    528       1.134   thorpej  *    the userland pmaps which owns this L1) are moved to the TAIL.
    529        1.17     chris  */
    530       1.134   thorpej static TAILQ_HEAD(, l1_ttable) l1_lru_list;
    531       1.134   thorpej static struct simplelock l1_lru_lock;
    532        1.17     chris 
    533       1.134   thorpej /*
    534       1.134   thorpej  * A list of all L1 tables
    535       1.134   thorpej  */
    536       1.134   thorpej static SLIST_HEAD(, l1_ttable) l1_list;
    537        1.17     chris 
    538        1.17     chris /*
    539       1.134   thorpej  * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots.
    540       1.134   thorpej  *
    541       1.134   thorpej  * This is normally 16MB worth L2 page descriptors for any given pmap.
    542       1.134   thorpej  * Reference counts are maintained for L2 descriptors so they can be
    543       1.134   thorpej  * freed when empty.
    544        1.17     chris  */
    545       1.134   thorpej struct l2_dtable {
    546       1.134   thorpej 	/* The number of L2 page descriptors allocated to this l2_dtable */
    547       1.134   thorpej 	u_int l2_occupancy;
    548        1.17     chris 
    549       1.134   thorpej 	/* List of L2 page descriptors */
    550       1.134   thorpej 	struct l2_bucket {
    551       1.134   thorpej 		pt_entry_t *l2b_kva;	/* KVA of L2 Descriptor Table */
    552       1.134   thorpej 		paddr_t l2b_phys;	/* Physical address of same */
    553       1.134   thorpej 		u_short l2b_l1idx;	/* This L2 table's L1 index */
    554       1.134   thorpej 		u_short l2b_occupancy;	/* How many active descriptors */
    555       1.134   thorpej 	} l2_bucket[L2_BUCKET_SIZE];
    556        1.17     chris };
    557        1.17     chris 
    558        1.17     chris /*
    559       1.134   thorpej  * Given an L1 table index, calculate the corresponding l2_dtable index
    560       1.134   thorpej  * and bucket index within the l2_dtable.
    561        1.17     chris  */
    562       1.134   thorpej #define	L2_IDX(l1idx)		(((l1idx) >> L2_BUCKET_LOG2) & \
    563       1.134   thorpej 				 (L2_SIZE - 1))
    564       1.134   thorpej #define	L2_BUCKET(l1idx)	((l1idx) & (L2_BUCKET_SIZE - 1))
    565        1.17     chris 
    566       1.134   thorpej /*
    567       1.134   thorpej  * Given a virtual address, this macro returns the
    568       1.134   thorpej  * virtual address required to drop into the next L2 bucket.
    569       1.134   thorpej  */
    570       1.134   thorpej #define	L2_NEXT_BUCKET(va)	(((va) & L1_S_FRAME) + L1_S_SIZE)
    571        1.17     chris 
    572        1.17     chris /*
    573       1.134   thorpej  * L2 allocation.
    574        1.17     chris  */
    575       1.134   thorpej #define	pmap_alloc_l2_dtable()		\
    576       1.134   thorpej 	    pool_cache_get(&pmap_l2dtable_cache, PR_NOWAIT)
    577       1.134   thorpej #define	pmap_free_l2_dtable(l2)		\
    578       1.134   thorpej 	    pool_cache_put(&pmap_l2dtable_cache, (l2))
    579       1.134   thorpej #define pmap_alloc_l2_ptp(pap)		\
    580       1.134   thorpej 	    ((pt_entry_t *)pool_cache_get_paddr(&pmap_l2ptp_cache,\
    581       1.134   thorpej 	    PR_NOWAIT, (pap)))
    582         1.1      matt 
    583         1.1      matt /*
    584       1.134   thorpej  * We try to map the page tables write-through, if possible.  However, not
    585       1.134   thorpej  * all CPUs have a write-through cache mode, so on those we have to sync
    586       1.134   thorpej  * the cache when we frob page tables.
    587       1.113   thorpej  *
    588       1.134   thorpej  * We try to evaluate this at compile time, if possible.  However, it's
    589       1.134   thorpej  * not always possible to do that, hence this run-time var.
    590       1.134   thorpej  */
    591       1.134   thorpej int	pmap_needs_pte_sync;
    592       1.113   thorpej 
    593       1.113   thorpej /*
    594       1.134   thorpej  * Real definition of pv_entry.
    595       1.113   thorpej  */
    596       1.134   thorpej struct pv_entry {
    597       1.134   thorpej 	struct pv_entry *pv_next;       /* next pv_entry */
    598       1.134   thorpej 	pmap_t		pv_pmap;        /* pmap where mapping lies */
    599       1.134   thorpej 	vaddr_t		pv_va;          /* virtual address for mapping */
    600       1.134   thorpej 	u_int		pv_flags;       /* flags */
    601       1.134   thorpej };
    602       1.113   thorpej 
    603       1.113   thorpej /*
    604       1.134   thorpej  * Macro to determine if a mapping might be resident in the
    605       1.134   thorpej  * instruction cache and/or TLB
    606        1.17     chris  */
    607       1.134   thorpej #define	PV_BEEN_EXECD(f)  (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC))
    608  1.164.12.2      matt #define	PV_IS_EXEC_P(f)   (((f) & PVF_EXEC) != 0)
    609        1.17     chris 
    610        1.17     chris /*
    611       1.134   thorpej  * Macro to determine if a mapping might be resident in the
    612       1.134   thorpej  * data cache and/or TLB
    613         1.1      matt  */
    614       1.134   thorpej #define	PV_BEEN_REFD(f)   (((f) & PVF_REF) != 0)
    615         1.1      matt 
    616         1.1      matt /*
    617       1.134   thorpej  * Local prototypes
    618         1.1      matt  */
    619       1.134   thorpej static int		pmap_set_pt_cache_mode(pd_entry_t *, vaddr_t);
    620       1.134   thorpej static void		pmap_alloc_specials(vaddr_t *, int, vaddr_t *,
    621       1.134   thorpej 			    pt_entry_t **);
    622       1.159   thorpej static bool		pmap_is_current(pmap_t);
    623       1.159   thorpej static bool		pmap_is_cached(pmap_t);
    624       1.134   thorpej static void		pmap_enter_pv(struct vm_page *, struct pv_entry *,
    625       1.134   thorpej 			    pmap_t, vaddr_t, u_int);
    626       1.134   thorpej static struct pv_entry *pmap_find_pv(struct vm_page *, pmap_t, vaddr_t);
    627       1.156       scw static struct pv_entry *pmap_remove_pv(struct vm_page *, pmap_t, vaddr_t, int);
    628       1.134   thorpej static u_int		pmap_modify_pv(struct vm_page *, pmap_t, vaddr_t,
    629       1.134   thorpej 			    u_int, u_int);
    630        1.17     chris 
    631       1.134   thorpej static void		pmap_pinit(pmap_t);
    632       1.134   thorpej static int		pmap_pmap_ctor(void *, void *, int);
    633        1.17     chris 
    634       1.134   thorpej static void		pmap_alloc_l1(pmap_t);
    635       1.134   thorpej static void		pmap_free_l1(pmap_t);
    636       1.134   thorpej static void		pmap_use_l1(pmap_t);
    637        1.17     chris 
    638       1.134   thorpej static struct l2_bucket *pmap_get_l2_bucket(pmap_t, vaddr_t);
    639       1.134   thorpej static struct l2_bucket *pmap_alloc_l2_bucket(pmap_t, vaddr_t);
    640       1.134   thorpej static void		pmap_free_l2_bucket(pmap_t, struct l2_bucket *, u_int);
    641       1.134   thorpej static int		pmap_l2ptp_ctor(void *, void *, int);
    642       1.134   thorpej static int		pmap_l2dtable_ctor(void *, void *, int);
    643        1.51     chris 
    644       1.134   thorpej static void		pmap_vac_me_harder(struct vm_page *, pmap_t, vaddr_t);
    645  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
    646       1.134   thorpej static void		pmap_vac_me_kpmap(struct vm_page *, pmap_t, vaddr_t);
    647       1.134   thorpej static void		pmap_vac_me_user(struct vm_page *, pmap_t, vaddr_t);
    648  1.164.12.2      matt #endif
    649        1.17     chris 
    650       1.134   thorpej static void		pmap_clearbit(struct vm_page *, u_int);
    651  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
    652       1.159   thorpej static int		pmap_clean_page(struct pv_entry *, bool);
    653  1.164.12.2      matt #endif
    654  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
    655  1.164.12.2      matt static void		pmap_syncicache_page(struct vm_page *);
    656  1.164.12.2      matt static void		pmap_flush_page(struct vm_page *);
    657  1.164.12.2      matt #endif
    658       1.134   thorpej static void		pmap_page_remove(struct vm_page *);
    659        1.17     chris 
    660       1.134   thorpej static void		pmap_init_l1(struct l1_ttable *, pd_entry_t *);
    661       1.134   thorpej static vaddr_t		kernel_pt_lookup(paddr_t);
    662        1.17     chris 
    663  1.164.12.3      matt void pmap_switch(struct lwp *, struct lwp *);
    664  1.164.12.3      matt 
    665        1.17     chris 
    666        1.17     chris /*
    667       1.134   thorpej  * External function prototypes
    668        1.17     chris  */
    669       1.134   thorpej extern void bzero_page(vaddr_t);
    670       1.134   thorpej extern void bcopy_page(vaddr_t, vaddr_t);
    671        1.17     chris 
    672       1.134   thorpej /*
    673       1.134   thorpej  * Misc variables
    674       1.134   thorpej  */
    675       1.134   thorpej vaddr_t virtual_avail;
    676       1.134   thorpej vaddr_t virtual_end;
    677       1.134   thorpej vaddr_t pmap_curmaxkvaddr;
    678        1.17     chris 
    679       1.134   thorpej vaddr_t avail_start;
    680       1.134   thorpej vaddr_t avail_end;
    681        1.17     chris 
    682  1.164.12.2      matt pv_addrqh_t pmap_boot_freeq = SLIST_HEAD_INITIALIZER(&pmap_boot_freeq);
    683  1.164.12.2      matt pv_addr_t kernelpages;
    684  1.164.12.2      matt pv_addr_t kernel_l1pt;
    685  1.164.12.2      matt pv_addr_t systempage;
    686        1.17     chris 
    687       1.134   thorpej /* Function to set the debug level of the pmap code */
    688        1.17     chris 
    689       1.134   thorpej #ifdef PMAP_DEBUG
    690       1.134   thorpej void
    691       1.134   thorpej pmap_debug(int level)
    692       1.134   thorpej {
    693       1.134   thorpej 	pmap_debug_level = level;
    694       1.134   thorpej 	printf("pmap_debug: level=%d\n", pmap_debug_level);
    695         1.1      matt }
    696       1.134   thorpej #endif	/* PMAP_DEBUG */
    697         1.1      matt 
    698         1.1      matt /*
    699       1.134   thorpej  * A bunch of routines to conditionally flush the caches/TLB depending
    700       1.134   thorpej  * on whether the specified pmap actually needs to be flushed at any
    701       1.134   thorpej  * given time.
    702         1.1      matt  */
    703       1.157     perry static inline void
    704       1.134   thorpej pmap_tlb_flushID_SE(pmap_t pm, vaddr_t va)
    705       1.134   thorpej {
    706       1.134   thorpej 
    707       1.134   thorpej 	if (pm->pm_cstate.cs_tlb_id)
    708       1.134   thorpej 		cpu_tlb_flushID_SE(va);
    709       1.134   thorpej }
    710         1.1      matt 
    711       1.157     perry static inline void
    712       1.134   thorpej pmap_tlb_flushD_SE(pmap_t pm, vaddr_t va)
    713         1.1      matt {
    714         1.1      matt 
    715       1.134   thorpej 	if (pm->pm_cstate.cs_tlb_d)
    716       1.134   thorpej 		cpu_tlb_flushD_SE(va);
    717         1.1      matt }
    718         1.1      matt 
    719       1.157     perry static inline void
    720       1.134   thorpej pmap_tlb_flushID(pmap_t pm)
    721         1.1      matt {
    722         1.1      matt 
    723       1.134   thorpej 	if (pm->pm_cstate.cs_tlb_id) {
    724       1.134   thorpej 		cpu_tlb_flushID();
    725       1.134   thorpej 		pm->pm_cstate.cs_tlb = 0;
    726         1.1      matt 	}
    727       1.134   thorpej }
    728         1.1      matt 
    729       1.157     perry static inline void
    730       1.134   thorpej pmap_tlb_flushD(pmap_t pm)
    731       1.134   thorpej {
    732         1.1      matt 
    733       1.134   thorpej 	if (pm->pm_cstate.cs_tlb_d) {
    734       1.134   thorpej 		cpu_tlb_flushD();
    735       1.134   thorpej 		pm->pm_cstate.cs_tlb_d = 0;
    736         1.1      matt 	}
    737         1.1      matt }
    738         1.1      matt 
    739  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
    740       1.157     perry static inline void
    741       1.134   thorpej pmap_idcache_wbinv_range(pmap_t pm, vaddr_t va, vsize_t len)
    742         1.1      matt {
    743  1.164.12.2      matt 	if (pm->pm_cstate.cs_cache_id) {
    744       1.134   thorpej 		cpu_idcache_wbinv_range(va, len);
    745  1.164.12.2      matt 	}
    746        1.17     chris }
    747         1.1      matt 
    748       1.157     perry static inline void
    749       1.134   thorpej pmap_dcache_wb_range(pmap_t pm, vaddr_t va, vsize_t len,
    750       1.159   thorpej     bool do_inv, bool rd_only)
    751        1.17     chris {
    752         1.1      matt 
    753       1.134   thorpej 	if (pm->pm_cstate.cs_cache_d) {
    754       1.134   thorpej 		if (do_inv) {
    755       1.134   thorpej 			if (rd_only)
    756       1.134   thorpej 				cpu_dcache_inv_range(va, len);
    757       1.134   thorpej 			else
    758       1.134   thorpej 				cpu_dcache_wbinv_range(va, len);
    759       1.134   thorpej 		} else
    760       1.134   thorpej 		if (!rd_only)
    761       1.134   thorpej 			cpu_dcache_wb_range(va, len);
    762         1.1      matt 	}
    763       1.134   thorpej }
    764         1.1      matt 
    765       1.157     perry static inline void
    766       1.134   thorpej pmap_idcache_wbinv_all(pmap_t pm)
    767       1.134   thorpej {
    768       1.134   thorpej 	if (pm->pm_cstate.cs_cache_id) {
    769       1.134   thorpej 		cpu_idcache_wbinv_all();
    770       1.134   thorpej 		pm->pm_cstate.cs_cache = 0;
    771       1.134   thorpej 	}
    772         1.1      matt }
    773         1.1      matt 
    774       1.157     perry static inline void
    775       1.134   thorpej pmap_dcache_wbinv_all(pmap_t pm)
    776       1.134   thorpej {
    777       1.134   thorpej 	if (pm->pm_cstate.cs_cache_d) {
    778       1.134   thorpej 		cpu_dcache_wbinv_all();
    779       1.134   thorpej 		pm->pm_cstate.cs_cache_d = 0;
    780       1.134   thorpej 	}
    781       1.134   thorpej }
    782  1.164.12.2      matt #endif /* PMAP_CACHE_VIVT */
    783         1.1      matt 
    784       1.159   thorpej static inline bool
    785       1.134   thorpej pmap_is_current(pmap_t pm)
    786         1.1      matt {
    787        1.17     chris 
    788       1.134   thorpej 	if (pm == pmap_kernel() ||
    789       1.134   thorpej 	    (curproc && curproc->p_vmspace->vm_map.pmap == pm))
    790  1.164.12.2      matt 		return true;
    791         1.1      matt 
    792  1.164.12.2      matt 	return false;
    793       1.134   thorpej }
    794         1.1      matt 
    795       1.159   thorpej static inline bool
    796       1.134   thorpej pmap_is_cached(pmap_t pm)
    797       1.134   thorpej {
    798        1.17     chris 
    799  1.164.12.3      matt 	if (pm == pmap_kernel() || pmap_recent_user == NULL ||
    800  1.164.12.3      matt 	    pmap_recent_user == pm)
    801  1.164.12.3      matt 		return (true);
    802        1.17     chris 
    803  1.164.12.2      matt 	return false;
    804       1.134   thorpej }
    805         1.1      matt 
    806       1.134   thorpej /*
    807       1.134   thorpej  * PTE_SYNC_CURRENT:
    808       1.134   thorpej  *
    809       1.134   thorpej  *     Make sure the pte is written out to RAM.
    810       1.134   thorpej  *     We need to do this for one of two cases:
    811       1.134   thorpej  *       - We're dealing with the kernel pmap
    812       1.134   thorpej  *       - There is no pmap active in the cache/tlb.
    813       1.134   thorpej  *       - The specified pmap is 'active' in the cache/tlb.
    814       1.134   thorpej  */
    815       1.134   thorpej #ifdef PMAP_INCLUDE_PTE_SYNC
    816       1.134   thorpej #define	PTE_SYNC_CURRENT(pm, ptep)	\
    817       1.134   thorpej do {					\
    818       1.134   thorpej 	if (PMAP_NEEDS_PTE_SYNC && 	\
    819       1.134   thorpej 	    pmap_is_cached(pm))		\
    820       1.134   thorpej 		PTE_SYNC(ptep);		\
    821       1.134   thorpej } while (/*CONSTCOND*/0)
    822       1.134   thorpej #else
    823       1.134   thorpej #define	PTE_SYNC_CURRENT(pm, ptep)	/* nothing */
    824       1.134   thorpej #endif
    825         1.1      matt 
    826         1.1      matt /*
    827        1.17     chris  * main pv_entry manipulation functions:
    828        1.49   thorpej  *   pmap_enter_pv: enter a mapping onto a vm_page list
    829        1.49   thorpej  *   pmap_remove_pv: remove a mappiing from a vm_page list
    830        1.17     chris  *
    831        1.17     chris  * NOTE: pmap_enter_pv expects to lock the pvh itself
    832        1.17     chris  *       pmap_remove_pv expects te caller to lock the pvh before calling
    833        1.17     chris  */
    834        1.17     chris 
    835        1.17     chris /*
    836        1.49   thorpej  * pmap_enter_pv: enter a mapping onto a vm_page lst
    837        1.17     chris  *
    838        1.17     chris  * => caller should hold the proper lock on pmap_main_lock
    839        1.17     chris  * => caller should have pmap locked
    840        1.49   thorpej  * => we will gain the lock on the vm_page and allocate the new pv_entry
    841        1.17     chris  * => caller should adjust ptp's wire_count before calling
    842        1.17     chris  * => caller should not adjust pmap's wire_count
    843        1.17     chris  */
    844       1.134   thorpej static void
    845       1.134   thorpej pmap_enter_pv(struct vm_page *pg, struct pv_entry *pve, pmap_t pm,
    846       1.134   thorpej     vaddr_t va, u_int flags)
    847       1.134   thorpej {
    848        1.17     chris 
    849       1.134   thorpej 	NPDEBUG(PDB_PVDUMP,
    850       1.134   thorpej 	    printf("pmap_enter_pv: pm %p, pg %p, flags 0x%x\n", pm, pg, flags));
    851       1.134   thorpej 
    852       1.134   thorpej 	pve->pv_pmap = pm;
    853        1.17     chris 	pve->pv_va = va;
    854        1.17     chris 	pve->pv_flags = flags;
    855       1.134   thorpej 
    856        1.49   thorpej 	simple_lock(&pg->mdpage.pvh_slock);	/* lock vm_page */
    857        1.49   thorpej 	pve->pv_next = pg->mdpage.pvh_list;	/* add to ... */
    858        1.49   thorpej 	pg->mdpage.pvh_list = pve;		/* ... locked list */
    859       1.134   thorpej 	pg->mdpage.pvh_attrs |= flags & (PVF_REF | PVF_MOD);
    860       1.134   thorpej 	if (pm == pmap_kernel()) {
    861  1.164.12.2      matt 		PMAPCOUNT(kernel_mappings);
    862       1.134   thorpej 		if (flags & PVF_WRITE)
    863       1.134   thorpej 			pg->mdpage.krw_mappings++;
    864       1.134   thorpej 		else
    865       1.134   thorpej 			pg->mdpage.kro_mappings++;
    866       1.134   thorpej 	} else
    867       1.134   thorpej 	if (flags & PVF_WRITE)
    868       1.134   thorpej 		pg->mdpage.urw_mappings++;
    869       1.134   thorpej 	else
    870       1.134   thorpej 		pg->mdpage.uro_mappings++;
    871  1.164.12.2      matt 
    872  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
    873  1.164.12.2      matt 	/*
    874  1.164.12.2      matt 	 * If this is an exec mapping and its the first exec mapping
    875  1.164.12.2      matt 	 * for this page, make sure to sync the I-cache.
    876  1.164.12.2      matt 	 */
    877  1.164.12.2      matt 	if (PV_IS_EXEC_P(flags)) {
    878  1.164.12.2      matt 		if (!PV_IS_EXEC_P(pg->mdpage.pvh_attrs)) {
    879  1.164.12.2      matt 			pmap_syncicache_page(pg);
    880  1.164.12.2      matt 			PMAPCOUNT(exec_synced_map);
    881  1.164.12.2      matt 		}
    882  1.164.12.2      matt 		PMAPCOUNT(exec_mappings);
    883  1.164.12.2      matt 	}
    884  1.164.12.2      matt #endif
    885  1.164.12.2      matt 
    886  1.164.12.2      matt 	PMAPCOUNT(mappings);
    887        1.49   thorpej 	simple_unlock(&pg->mdpage.pvh_slock);	/* unlock, done! */
    888       1.134   thorpej 
    889        1.78   thorpej 	if (pve->pv_flags & PVF_WIRED)
    890       1.134   thorpej 		++pm->pm_stats.wired_count;
    891        1.17     chris }
    892        1.17     chris 
    893        1.17     chris /*
    894       1.134   thorpej  *
    895       1.134   thorpej  * pmap_find_pv: Find a pv entry
    896       1.134   thorpej  *
    897       1.134   thorpej  * => caller should hold lock on vm_page
    898       1.134   thorpej  */
    899       1.157     perry static inline struct pv_entry *
    900       1.134   thorpej pmap_find_pv(struct vm_page *pg, pmap_t pm, vaddr_t va)
    901       1.134   thorpej {
    902       1.134   thorpej 	struct pv_entry *pv;
    903       1.134   thorpej 
    904       1.134   thorpej 	for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
    905       1.134   thorpej 		if (pm == pv->pv_pmap && va == pv->pv_va)
    906       1.134   thorpej 			break;
    907       1.134   thorpej 	}
    908       1.134   thorpej 
    909       1.134   thorpej 	return (pv);
    910       1.134   thorpej }
    911       1.134   thorpej 
    912       1.134   thorpej /*
    913       1.134   thorpej  * pmap_remove_pv: try to remove a mapping from a pv_list
    914        1.17     chris  *
    915        1.17     chris  * => caller should hold proper lock on pmap_main_lock
    916        1.17     chris  * => pmap should be locked
    917        1.49   thorpej  * => caller should hold lock on vm_page [so that attrs can be adjusted]
    918        1.17     chris  * => caller should adjust ptp's wire_count and free PTP if needed
    919        1.17     chris  * => caller should NOT adjust pmap's wire_count
    920        1.17     chris  * => we return the removed pve
    921        1.17     chris  */
    922       1.134   thorpej static struct pv_entry *
    923       1.156       scw pmap_remove_pv(struct vm_page *pg, pmap_t pm, vaddr_t va, int skip_wired)
    924        1.17     chris {
    925        1.17     chris 	struct pv_entry *pve, **prevptr;
    926        1.17     chris 
    927       1.134   thorpej 	NPDEBUG(PDB_PVDUMP,
    928       1.134   thorpej 	    printf("pmap_remove_pv: pm %p, pg %p, va 0x%08lx\n", pm, pg, va));
    929       1.134   thorpej 
    930        1.49   thorpej 	prevptr = &pg->mdpage.pvh_list;		/* previous pv_entry pointer */
    931        1.17     chris 	pve = *prevptr;
    932       1.134   thorpej 
    933        1.17     chris 	while (pve) {
    934       1.134   thorpej 		if (pve->pv_pmap == pm && pve->pv_va == va) {	/* match? */
    935       1.156       scw 			NPDEBUG(PDB_PVDUMP, printf("pmap_remove_pv: pm %p, pg "
    936       1.156       scw 			    "%p, flags 0x%x\n", pm, pg, pve->pv_flags));
    937       1.156       scw 			if (pve->pv_flags & PVF_WIRED) {
    938       1.156       scw 				if (skip_wired)
    939       1.156       scw 					return (NULL);
    940       1.156       scw 				--pm->pm_stats.wired_count;
    941       1.156       scw 			}
    942        1.17     chris 			*prevptr = pve->pv_next;		/* remove it! */
    943       1.134   thorpej 			if (pm == pmap_kernel()) {
    944  1.164.12.2      matt 				PMAPCOUNT(kernel_unmappings);
    945       1.134   thorpej 				if (pve->pv_flags & PVF_WRITE)
    946       1.134   thorpej 					pg->mdpage.krw_mappings--;
    947       1.134   thorpej 				else
    948       1.134   thorpej 					pg->mdpage.kro_mappings--;
    949       1.134   thorpej 			} else
    950       1.134   thorpej 			if (pve->pv_flags & PVF_WRITE)
    951       1.134   thorpej 				pg->mdpage.urw_mappings--;
    952       1.134   thorpej 			else
    953       1.134   thorpej 				pg->mdpage.uro_mappings--;
    954  1.164.12.2      matt 
    955  1.164.12.2      matt 			PMAPCOUNT(unmappings);
    956  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
    957  1.164.12.2      matt 			if (!(pve->pv_flags & PVF_WRITE))
    958  1.164.12.2      matt 				break;
    959  1.164.12.2      matt 			/*
    960  1.164.12.2      matt 			 * If this page has had an exec mapping, then if
    961  1.164.12.2      matt 			 * this was the last mapping, discard the contents,
    962  1.164.12.2      matt 			 * otherwise sync the i-cache for this page.
    963  1.164.12.2      matt 			 */
    964  1.164.12.2      matt 			if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs)) {
    965  1.164.12.2      matt 				if (pg->mdpage.pvh_list == NULL) {
    966  1.164.12.2      matt 					pg->mdpage.pvh_attrs &= ~PVF_EXEC;
    967  1.164.12.2      matt 					PMAPCOUNT(exec_discarded_unmap);
    968  1.164.12.2      matt 				} else {
    969  1.164.12.2      matt 					pmap_syncicache_page(pg);
    970  1.164.12.2      matt 					PMAPCOUNT(exec_synced_unmap);
    971  1.164.12.2      matt 				}
    972  1.164.12.2      matt 			}
    973  1.164.12.2      matt #endif /* PMAP_CACHE_VIPT */
    974        1.17     chris 			break;
    975        1.17     chris 		}
    976        1.17     chris 		prevptr = &pve->pv_next;		/* previous pointer */
    977        1.17     chris 		pve = pve->pv_next;			/* advance */
    978        1.17     chris 	}
    979       1.134   thorpej 
    980        1.17     chris 	return(pve);				/* return removed pve */
    981        1.17     chris }
    982        1.17     chris 
    983        1.17     chris /*
    984        1.17     chris  *
    985        1.17     chris  * pmap_modify_pv: Update pv flags
    986        1.17     chris  *
    987        1.49   thorpej  * => caller should hold lock on vm_page [so that attrs can be adjusted]
    988        1.17     chris  * => caller should NOT adjust pmap's wire_count
    989        1.29  rearnsha  * => caller must call pmap_vac_me_harder() if writable status of a page
    990        1.29  rearnsha  *    may have changed.
    991        1.17     chris  * => we return the old flags
    992        1.17     chris  *
    993         1.1      matt  * Modify a physical-virtual mapping in the pv table
    994         1.1      matt  */
    995       1.134   thorpej static u_int
    996       1.134   thorpej pmap_modify_pv(struct vm_page *pg, pmap_t pm, vaddr_t va,
    997       1.134   thorpej     u_int clr_mask, u_int set_mask)
    998         1.1      matt {
    999         1.1      matt 	struct pv_entry *npv;
   1000         1.1      matt 	u_int flags, oflags;
   1001         1.1      matt 
   1002       1.134   thorpej 	if ((npv = pmap_find_pv(pg, pm, va)) == NULL)
   1003       1.134   thorpej 		return (0);
   1004       1.134   thorpej 
   1005       1.134   thorpej 	NPDEBUG(PDB_PVDUMP,
   1006       1.134   thorpej 	    printf("pmap_modify_pv: pm %p, pg %p, clr 0x%x, set 0x%x, flags 0x%x\n", pm, pg, clr_mask, set_mask, npv->pv_flags));
   1007       1.134   thorpej 
   1008         1.1      matt 	/*
   1009         1.1      matt 	 * There is at least one VA mapping this page.
   1010         1.1      matt 	 */
   1011         1.1      matt 
   1012       1.134   thorpej 	if (clr_mask & (PVF_REF | PVF_MOD))
   1013       1.134   thorpej 		pg->mdpage.pvh_attrs |= set_mask & (PVF_REF | PVF_MOD);
   1014       1.134   thorpej 
   1015       1.134   thorpej 	oflags = npv->pv_flags;
   1016       1.134   thorpej 	npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask;
   1017       1.134   thorpej 
   1018       1.134   thorpej 	if ((flags ^ oflags) & PVF_WIRED) {
   1019       1.134   thorpej 		if (flags & PVF_WIRED)
   1020       1.134   thorpej 			++pm->pm_stats.wired_count;
   1021       1.134   thorpej 		else
   1022       1.134   thorpej 			--pm->pm_stats.wired_count;
   1023       1.134   thorpej 	}
   1024       1.134   thorpej 
   1025       1.134   thorpej 	if ((flags ^ oflags) & PVF_WRITE) {
   1026       1.134   thorpej 		if (pm == pmap_kernel()) {
   1027       1.134   thorpej 			if (flags & PVF_WRITE) {
   1028       1.134   thorpej 				pg->mdpage.krw_mappings++;
   1029       1.134   thorpej 				pg->mdpage.kro_mappings--;
   1030       1.134   thorpej 			} else {
   1031       1.134   thorpej 				pg->mdpage.kro_mappings++;
   1032       1.134   thorpej 				pg->mdpage.krw_mappings--;
   1033         1.1      matt 			}
   1034       1.134   thorpej 		} else
   1035       1.134   thorpej 		if (flags & PVF_WRITE) {
   1036       1.134   thorpej 			pg->mdpage.urw_mappings++;
   1037       1.134   thorpej 			pg->mdpage.uro_mappings--;
   1038       1.134   thorpej 		} else {
   1039       1.134   thorpej 			pg->mdpage.uro_mappings++;
   1040       1.134   thorpej 			pg->mdpage.urw_mappings--;
   1041         1.1      matt 		}
   1042         1.1      matt 	}
   1043  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   1044  1.164.12.2      matt 	/*
   1045  1.164.12.2      matt 	 * We have two cases here: the first is from enter_pv (new exec
   1046  1.164.12.2      matt 	 * page), the second is a combined pmap_remove_pv/pmap_enter_pv.
   1047  1.164.12.2      matt 	 * Since in latter, pmap_enter_pv won't do anything, we just have
   1048  1.164.12.2      matt 	 * to do what pmap_remove_pv would do.
   1049  1.164.12.2      matt 	 */
   1050  1.164.12.2      matt 	if ((PV_IS_EXEC_P(flags) && !PV_IS_EXEC_P(pg->mdpage.pvh_attrs))
   1051  1.164.12.2      matt 	    || (PV_IS_EXEC_P(pg->mdpage.pvh_attrs)
   1052  1.164.12.2      matt 		|| (!(flags & PVF_WRITE) && (oflags & PVF_WRITE)))) {
   1053  1.164.12.2      matt 		pmap_syncicache_page(pg);
   1054  1.164.12.2      matt 		PMAPCOUNT(exec_synced_remap);
   1055  1.164.12.2      matt 	}
   1056  1.164.12.2      matt #endif
   1057  1.164.12.2      matt 
   1058  1.164.12.2      matt 	PMAPCOUNT(remappings);
   1059       1.134   thorpej 
   1060       1.134   thorpej 	return (oflags);
   1061         1.1      matt }
   1062         1.1      matt 
   1063       1.134   thorpej /*
   1064       1.134   thorpej  * Allocate an L1 translation table for the specified pmap.
   1065       1.134   thorpej  * This is called at pmap creation time.
   1066       1.134   thorpej  */
   1067       1.134   thorpej static void
   1068       1.134   thorpej pmap_alloc_l1(pmap_t pm)
   1069         1.1      matt {
   1070       1.134   thorpej 	struct l1_ttable *l1;
   1071       1.134   thorpej 	u_int8_t domain;
   1072       1.134   thorpej 
   1073       1.134   thorpej 	/*
   1074       1.134   thorpej 	 * Remove the L1 at the head of the LRU list
   1075       1.134   thorpej 	 */
   1076       1.134   thorpej 	simple_lock(&l1_lru_lock);
   1077       1.134   thorpej 	l1 = TAILQ_FIRST(&l1_lru_list);
   1078       1.134   thorpej 	KDASSERT(l1 != NULL);
   1079       1.134   thorpej 	TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
   1080         1.1      matt 
   1081       1.134   thorpej 	/*
   1082       1.134   thorpej 	 * Pick the first available domain number, and update
   1083       1.134   thorpej 	 * the link to the next number.
   1084       1.134   thorpej 	 */
   1085       1.134   thorpej 	domain = l1->l1_domain_first;
   1086       1.134   thorpej 	l1->l1_domain_first = l1->l1_domain_free[domain];
   1087       1.115   thorpej 
   1088       1.134   thorpej 	/*
   1089       1.134   thorpej 	 * If there are still free domain numbers in this L1,
   1090       1.134   thorpej 	 * put it back on the TAIL of the LRU list.
   1091       1.134   thorpej 	 */
   1092       1.134   thorpej 	if (++l1->l1_domain_use_count < PMAP_DOMAINS)
   1093       1.134   thorpej 		TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
   1094         1.1      matt 
   1095       1.134   thorpej 	simple_unlock(&l1_lru_lock);
   1096         1.1      matt 
   1097       1.134   thorpej 	/*
   1098       1.134   thorpej 	 * Fix up the relevant bits in the pmap structure
   1099       1.134   thorpej 	 */
   1100       1.134   thorpej 	pm->pm_l1 = l1;
   1101       1.134   thorpej 	pm->pm_domain = domain;
   1102         1.1      matt }
   1103         1.1      matt 
   1104         1.1      matt /*
   1105       1.134   thorpej  * Free an L1 translation table.
   1106       1.134   thorpej  * This is called at pmap destruction time.
   1107         1.1      matt  */
   1108       1.134   thorpej static void
   1109       1.134   thorpej pmap_free_l1(pmap_t pm)
   1110         1.1      matt {
   1111       1.134   thorpej 	struct l1_ttable *l1 = pm->pm_l1;
   1112         1.1      matt 
   1113       1.134   thorpej 	simple_lock(&l1_lru_lock);
   1114         1.1      matt 
   1115       1.134   thorpej 	/*
   1116       1.134   thorpej 	 * If this L1 is currently on the LRU list, remove it.
   1117       1.134   thorpej 	 */
   1118       1.134   thorpej 	if (l1->l1_domain_use_count < PMAP_DOMAINS)
   1119       1.134   thorpej 		TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
   1120         1.1      matt 
   1121         1.1      matt 	/*
   1122       1.134   thorpej 	 * Free up the domain number which was allocated to the pmap
   1123         1.1      matt 	 */
   1124       1.134   thorpej 	l1->l1_domain_free[pm->pm_domain] = l1->l1_domain_first;
   1125       1.134   thorpej 	l1->l1_domain_first = pm->pm_domain;
   1126       1.134   thorpej 	l1->l1_domain_use_count--;
   1127         1.1      matt 
   1128       1.134   thorpej 	/*
   1129       1.134   thorpej 	 * The L1 now must have at least 1 free domain, so add
   1130       1.134   thorpej 	 * it back to the LRU list. If the use count is zero,
   1131       1.134   thorpej 	 * put it at the head of the list, otherwise it goes
   1132       1.134   thorpej 	 * to the tail.
   1133       1.134   thorpej 	 */
   1134       1.134   thorpej 	if (l1->l1_domain_use_count == 0)
   1135       1.134   thorpej 		TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru);
   1136       1.134   thorpej 	else
   1137       1.134   thorpej 		TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
   1138        1.54   thorpej 
   1139       1.134   thorpej 	simple_unlock(&l1_lru_lock);
   1140       1.134   thorpej }
   1141        1.54   thorpej 
   1142       1.157     perry static inline void
   1143       1.134   thorpej pmap_use_l1(pmap_t pm)
   1144       1.134   thorpej {
   1145       1.134   thorpej 	struct l1_ttable *l1;
   1146        1.54   thorpej 
   1147       1.134   thorpej 	/*
   1148       1.134   thorpej 	 * Do nothing if we're in interrupt context.
   1149       1.134   thorpej 	 * Access to an L1 by the kernel pmap must not affect
   1150       1.134   thorpej 	 * the LRU list.
   1151       1.134   thorpej 	 */
   1152  1.164.12.1      matt 	if (curcpu()->ci_intr_depth || pm == pmap_kernel())
   1153       1.134   thorpej 		return;
   1154        1.54   thorpej 
   1155       1.134   thorpej 	l1 = pm->pm_l1;
   1156         1.1      matt 
   1157        1.17     chris 	/*
   1158       1.134   thorpej 	 * If the L1 is not currently on the LRU list, just return
   1159        1.17     chris 	 */
   1160       1.134   thorpej 	if (l1->l1_domain_use_count == PMAP_DOMAINS)
   1161       1.134   thorpej 		return;
   1162       1.134   thorpej 
   1163       1.134   thorpej 	simple_lock(&l1_lru_lock);
   1164         1.1      matt 
   1165        1.10     chris 	/*
   1166       1.134   thorpej 	 * Check the use count again, now that we've acquired the lock
   1167        1.10     chris 	 */
   1168       1.134   thorpej 	if (l1->l1_domain_use_count == PMAP_DOMAINS) {
   1169       1.134   thorpej 		simple_unlock(&l1_lru_lock);
   1170       1.134   thorpej 		return;
   1171       1.134   thorpej 	}
   1172       1.111   thorpej 
   1173       1.111   thorpej 	/*
   1174       1.134   thorpej 	 * Move the L1 to the back of the LRU list
   1175       1.111   thorpej 	 */
   1176       1.134   thorpej 	TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
   1177       1.134   thorpej 	TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
   1178       1.111   thorpej 
   1179       1.134   thorpej 	simple_unlock(&l1_lru_lock);
   1180         1.1      matt }
   1181         1.1      matt 
   1182         1.1      matt /*
   1183       1.134   thorpej  * void pmap_free_l2_ptp(pt_entry_t *, paddr_t *)
   1184         1.1      matt  *
   1185       1.134   thorpej  * Free an L2 descriptor table.
   1186         1.1      matt  */
   1187       1.157     perry static inline void
   1188       1.134   thorpej #ifndef PMAP_INCLUDE_PTE_SYNC
   1189       1.134   thorpej pmap_free_l2_ptp(pt_entry_t *l2, paddr_t pa)
   1190       1.134   thorpej #else
   1191       1.159   thorpej pmap_free_l2_ptp(bool need_sync, pt_entry_t *l2, paddr_t pa)
   1192       1.134   thorpej #endif
   1193         1.1      matt {
   1194       1.134   thorpej #ifdef PMAP_INCLUDE_PTE_SYNC
   1195  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   1196         1.1      matt 	/*
   1197       1.134   thorpej 	 * Note: With a write-back cache, we may need to sync this
   1198       1.134   thorpej 	 * L2 table before re-using it.
   1199       1.134   thorpej 	 * This is because it may have belonged to a non-current
   1200       1.134   thorpej 	 * pmap, in which case the cache syncs would have been
   1201  1.164.12.2      matt 	 * skipped for the pages that were being unmapped. If the
   1202       1.134   thorpej 	 * L2 table were then to be immediately re-allocated to
   1203       1.134   thorpej 	 * the *current* pmap, it may well contain stale mappings
   1204       1.134   thorpej 	 * which have not yet been cleared by a cache write-back
   1205       1.134   thorpej 	 * and so would still be visible to the mmu.
   1206         1.1      matt 	 */
   1207       1.134   thorpej 	if (need_sync)
   1208       1.134   thorpej 		PTE_SYNC_RANGE(l2, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
   1209  1.164.12.2      matt #endif /* PMAP_CACHE_VIVT */
   1210  1.164.12.2      matt #endif /* PMAP_INCLUDE_PTE_SYNC */
   1211       1.134   thorpej 	pool_cache_put_paddr(&pmap_l2ptp_cache, (void *)l2, pa);
   1212         1.1      matt }
   1213         1.1      matt 
   1214         1.1      matt /*
   1215       1.134   thorpej  * Returns a pointer to the L2 bucket associated with the specified pmap
   1216       1.134   thorpej  * and VA, or NULL if no L2 bucket exists for the address.
   1217         1.1      matt  */
   1218       1.157     perry static inline struct l2_bucket *
   1219       1.134   thorpej pmap_get_l2_bucket(pmap_t pm, vaddr_t va)
   1220       1.134   thorpej {
   1221       1.134   thorpej 	struct l2_dtable *l2;
   1222       1.134   thorpej 	struct l2_bucket *l2b;
   1223       1.134   thorpej 	u_short l1idx;
   1224         1.1      matt 
   1225       1.134   thorpej 	l1idx = L1_IDX(va);
   1226         1.1      matt 
   1227       1.134   thorpej 	if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL ||
   1228       1.134   thorpej 	    (l2b = &l2->l2_bucket[L2_BUCKET(l1idx)])->l2b_kva == NULL)
   1229       1.134   thorpej 		return (NULL);
   1230         1.1      matt 
   1231       1.134   thorpej 	return (l2b);
   1232         1.1      matt }
   1233         1.1      matt 
   1234         1.1      matt /*
   1235       1.134   thorpej  * Returns a pointer to the L2 bucket associated with the specified pmap
   1236       1.134   thorpej  * and VA.
   1237         1.1      matt  *
   1238       1.134   thorpej  * If no L2 bucket exists, perform the necessary allocations to put an L2
   1239       1.134   thorpej  * bucket/page table in place.
   1240         1.1      matt  *
   1241       1.134   thorpej  * Note that if a new L2 bucket/page was allocated, the caller *must*
   1242       1.134   thorpej  * increment the bucket occupancy counter appropriately *before*
   1243       1.134   thorpej  * releasing the pmap's lock to ensure no other thread or cpu deallocates
   1244       1.134   thorpej  * the bucket/page in the meantime.
   1245         1.1      matt  */
   1246       1.134   thorpej static struct l2_bucket *
   1247       1.134   thorpej pmap_alloc_l2_bucket(pmap_t pm, vaddr_t va)
   1248       1.134   thorpej {
   1249       1.134   thorpej 	struct l2_dtable *l2;
   1250       1.134   thorpej 	struct l2_bucket *l2b;
   1251       1.134   thorpej 	u_short l1idx;
   1252       1.134   thorpej 
   1253       1.134   thorpej 	l1idx = L1_IDX(va);
   1254       1.134   thorpej 
   1255       1.134   thorpej 	if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
   1256       1.134   thorpej 		/*
   1257       1.134   thorpej 		 * No mapping at this address, as there is
   1258       1.134   thorpej 		 * no entry in the L1 table.
   1259       1.134   thorpej 		 * Need to allocate a new l2_dtable.
   1260       1.134   thorpej 		 */
   1261       1.134   thorpej 		if ((l2 = pmap_alloc_l2_dtable()) == NULL)
   1262       1.134   thorpej 			return (NULL);
   1263       1.134   thorpej 
   1264       1.134   thorpej 		/*
   1265       1.134   thorpej 		 * Link it into the parent pmap
   1266       1.134   thorpej 		 */
   1267       1.134   thorpej 		pm->pm_l2[L2_IDX(l1idx)] = l2;
   1268       1.134   thorpej 	}
   1269         1.1      matt 
   1270       1.134   thorpej 	l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   1271         1.1      matt 
   1272        1.10     chris 	/*
   1273       1.134   thorpej 	 * Fetch pointer to the L2 page table associated with the address.
   1274        1.10     chris 	 */
   1275       1.134   thorpej 	if (l2b->l2b_kva == NULL) {
   1276       1.134   thorpej 		pt_entry_t *ptep;
   1277       1.134   thorpej 
   1278       1.134   thorpej 		/*
   1279       1.134   thorpej 		 * No L2 page table has been allocated. Chances are, this
   1280       1.134   thorpej 		 * is because we just allocated the l2_dtable, above.
   1281       1.134   thorpej 		 */
   1282       1.134   thorpej 		if ((ptep = pmap_alloc_l2_ptp(&l2b->l2b_phys)) == NULL) {
   1283       1.134   thorpej 			/*
   1284       1.134   thorpej 			 * Oops, no more L2 page tables available at this
   1285       1.134   thorpej 			 * time. We may need to deallocate the l2_dtable
   1286       1.134   thorpej 			 * if we allocated a new one above.
   1287       1.134   thorpej 			 */
   1288       1.134   thorpej 			if (l2->l2_occupancy == 0) {
   1289       1.134   thorpej 				pm->pm_l2[L2_IDX(l1idx)] = NULL;
   1290       1.134   thorpej 				pmap_free_l2_dtable(l2);
   1291       1.134   thorpej 			}
   1292       1.134   thorpej 			return (NULL);
   1293       1.134   thorpej 		}
   1294         1.1      matt 
   1295       1.134   thorpej 		l2->l2_occupancy++;
   1296       1.134   thorpej 		l2b->l2b_kva = ptep;
   1297       1.134   thorpej 		l2b->l2b_l1idx = l1idx;
   1298       1.134   thorpej 	}
   1299        1.16     chris 
   1300       1.134   thorpej 	return (l2b);
   1301         1.1      matt }
   1302         1.1      matt 
   1303         1.1      matt /*
   1304       1.134   thorpej  * One or more mappings in the specified L2 descriptor table have just been
   1305       1.134   thorpej  * invalidated.
   1306         1.1      matt  *
   1307       1.134   thorpej  * Garbage collect the metadata and descriptor table itself if necessary.
   1308         1.1      matt  *
   1309       1.134   thorpej  * The pmap lock must be acquired when this is called (not necessary
   1310       1.134   thorpej  * for the kernel pmap).
   1311         1.1      matt  */
   1312       1.134   thorpej static void
   1313       1.134   thorpej pmap_free_l2_bucket(pmap_t pm, struct l2_bucket *l2b, u_int count)
   1314         1.1      matt {
   1315       1.134   thorpej 	struct l2_dtable *l2;
   1316       1.134   thorpej 	pd_entry_t *pl1pd, l1pd;
   1317       1.134   thorpej 	pt_entry_t *ptep;
   1318       1.134   thorpej 	u_short l1idx;
   1319         1.1      matt 
   1320       1.134   thorpej 	KDASSERT(count <= l2b->l2b_occupancy);
   1321         1.1      matt 
   1322       1.134   thorpej 	/*
   1323       1.134   thorpej 	 * Update the bucket's reference count according to how many
   1324       1.134   thorpej 	 * PTEs the caller has just invalidated.
   1325       1.134   thorpej 	 */
   1326       1.134   thorpej 	l2b->l2b_occupancy -= count;
   1327         1.1      matt 
   1328         1.1      matt 	/*
   1329       1.134   thorpej 	 * Note:
   1330       1.134   thorpej 	 *
   1331       1.134   thorpej 	 * Level 2 page tables allocated to the kernel pmap are never freed
   1332       1.134   thorpej 	 * as that would require checking all Level 1 page tables and
   1333       1.134   thorpej 	 * removing any references to the Level 2 page table. See also the
   1334       1.134   thorpej 	 * comment elsewhere about never freeing bootstrap L2 descriptors.
   1335       1.134   thorpej 	 *
   1336       1.134   thorpej 	 * We make do with just invalidating the mapping in the L2 table.
   1337       1.134   thorpej 	 *
   1338       1.134   thorpej 	 * This isn't really a big deal in practice and, in fact, leads
   1339       1.134   thorpej 	 * to a performance win over time as we don't need to continually
   1340       1.134   thorpej 	 * alloc/free.
   1341         1.1      matt 	 */
   1342       1.134   thorpej 	if (l2b->l2b_occupancy > 0 || pm == pmap_kernel())
   1343       1.134   thorpej 		return;
   1344         1.1      matt 
   1345       1.134   thorpej 	/*
   1346       1.134   thorpej 	 * There are no more valid mappings in this level 2 page table.
   1347       1.134   thorpej 	 * Go ahead and NULL-out the pointer in the bucket, then
   1348       1.134   thorpej 	 * free the page table.
   1349       1.134   thorpej 	 */
   1350       1.134   thorpej 	l1idx = l2b->l2b_l1idx;
   1351       1.134   thorpej 	ptep = l2b->l2b_kva;
   1352       1.134   thorpej 	l2b->l2b_kva = NULL;
   1353         1.1      matt 
   1354       1.134   thorpej 	pl1pd = &pm->pm_l1->l1_kva[l1idx];
   1355         1.1      matt 
   1356       1.134   thorpej 	/*
   1357       1.134   thorpej 	 * If the L1 slot matches the pmap's domain
   1358       1.134   thorpej 	 * number, then invalidate it.
   1359       1.134   thorpej 	 */
   1360       1.134   thorpej 	l1pd = *pl1pd & (L1_TYPE_MASK | L1_C_DOM_MASK);
   1361       1.134   thorpej 	if (l1pd == (L1_C_DOM(pm->pm_domain) | L1_TYPE_C)) {
   1362       1.134   thorpej 		*pl1pd = 0;
   1363       1.134   thorpej 		PTE_SYNC(pl1pd);
   1364         1.1      matt 	}
   1365         1.1      matt 
   1366       1.134   thorpej 	/*
   1367       1.134   thorpej 	 * Release the L2 descriptor table back to the pool cache.
   1368       1.134   thorpej 	 */
   1369       1.134   thorpej #ifndef PMAP_INCLUDE_PTE_SYNC
   1370       1.134   thorpej 	pmap_free_l2_ptp(ptep, l2b->l2b_phys);
   1371       1.134   thorpej #else
   1372       1.134   thorpej 	pmap_free_l2_ptp(!pmap_is_cached(pm), ptep, l2b->l2b_phys);
   1373       1.134   thorpej #endif
   1374       1.134   thorpej 
   1375       1.134   thorpej 	/*
   1376       1.134   thorpej 	 * Update the reference count in the associated l2_dtable
   1377       1.134   thorpej 	 */
   1378       1.134   thorpej 	l2 = pm->pm_l2[L2_IDX(l1idx)];
   1379       1.134   thorpej 	if (--l2->l2_occupancy > 0)
   1380       1.134   thorpej 		return;
   1381         1.1      matt 
   1382       1.134   thorpej 	/*
   1383       1.134   thorpej 	 * There are no more valid mappings in any of the Level 1
   1384       1.134   thorpej 	 * slots managed by this l2_dtable. Go ahead and NULL-out
   1385       1.134   thorpej 	 * the pointer in the parent pmap and free the l2_dtable.
   1386       1.134   thorpej 	 */
   1387       1.134   thorpej 	pm->pm_l2[L2_IDX(l1idx)] = NULL;
   1388       1.134   thorpej 	pmap_free_l2_dtable(l2);
   1389         1.1      matt }
   1390         1.1      matt 
   1391         1.1      matt /*
   1392       1.134   thorpej  * Pool cache constructors for L2 descriptor tables, metadata and pmap
   1393       1.134   thorpej  * structures.
   1394         1.1      matt  */
   1395       1.134   thorpej static int
   1396       1.134   thorpej pmap_l2ptp_ctor(void *arg, void *v, int flags)
   1397         1.1      matt {
   1398       1.134   thorpej #ifndef PMAP_INCLUDE_PTE_SYNC
   1399       1.134   thorpej 	struct l2_bucket *l2b;
   1400       1.134   thorpej 	pt_entry_t *ptep, pte;
   1401       1.134   thorpej 	vaddr_t va = (vaddr_t)v & ~PGOFSET;
   1402       1.134   thorpej 
   1403       1.134   thorpej 	/*
   1404       1.134   thorpej 	 * The mappings for these page tables were initially made using
   1405       1.134   thorpej 	 * pmap_kenter_pa() by the pool subsystem. Therefore, the cache-
   1406       1.134   thorpej 	 * mode will not be right for page table mappings. To avoid
   1407       1.134   thorpej 	 * polluting the pmap_kenter_pa() code with a special case for
   1408       1.134   thorpej 	 * page tables, we simply fix up the cache-mode here if it's not
   1409       1.134   thorpej 	 * correct.
   1410       1.134   thorpej 	 */
   1411       1.134   thorpej 	l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   1412       1.134   thorpej 	KDASSERT(l2b != NULL);
   1413       1.134   thorpej 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   1414       1.134   thorpej 	pte = *ptep;
   1415         1.1      matt 
   1416       1.134   thorpej 	if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
   1417       1.134   thorpej 		/*
   1418       1.134   thorpej 		 * Page tables must have the cache-mode set to Write-Thru.
   1419       1.134   thorpej 		 */
   1420       1.134   thorpej 		*ptep = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
   1421       1.134   thorpej 		PTE_SYNC(ptep);
   1422       1.134   thorpej 		cpu_tlb_flushD_SE(va);
   1423       1.134   thorpej 		cpu_cpwait();
   1424       1.134   thorpej 	}
   1425       1.134   thorpej #endif
   1426         1.1      matt 
   1427       1.134   thorpej 	memset(v, 0, L2_TABLE_SIZE_REAL);
   1428       1.134   thorpej 	PTE_SYNC_RANGE(v, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
   1429       1.134   thorpej 	return (0);
   1430         1.1      matt }
   1431         1.1      matt 
   1432       1.134   thorpej static int
   1433       1.134   thorpej pmap_l2dtable_ctor(void *arg, void *v, int flags)
   1434        1.93   thorpej {
   1435        1.93   thorpej 
   1436       1.134   thorpej 	memset(v, 0, sizeof(struct l2_dtable));
   1437       1.134   thorpej 	return (0);
   1438       1.134   thorpej }
   1439        1.93   thorpej 
   1440       1.134   thorpej static int
   1441       1.134   thorpej pmap_pmap_ctor(void *arg, void *v, int flags)
   1442       1.134   thorpej {
   1443        1.93   thorpej 
   1444       1.134   thorpej 	memset(v, 0, sizeof(struct pmap));
   1445       1.134   thorpej 	return (0);
   1446        1.93   thorpej }
   1447        1.93   thorpej 
   1448  1.164.12.3      matt static void
   1449  1.164.12.3      matt pmap_pinit(pmap_t pm)
   1450  1.164.12.3      matt {
   1451  1.164.12.3      matt 	struct l2_bucket *l2b;
   1452  1.164.12.3      matt 
   1453  1.164.12.3      matt 	if (vector_page < KERNEL_BASE) {
   1454  1.164.12.3      matt 		/*
   1455  1.164.12.3      matt 		 * Map the vector page.
   1456  1.164.12.3      matt 		 */
   1457  1.164.12.3      matt 		pmap_enter(pm, vector_page, systempage.pv_pa,
   1458  1.164.12.3      matt 		    VM_PROT_READ, VM_PROT_READ | PMAP_WIRED);
   1459  1.164.12.3      matt 		pmap_update(pm);
   1460  1.164.12.3      matt 
   1461  1.164.12.3      matt 		pm->pm_pl1vec = &pm->pm_l1->l1_kva[L1_IDX(vector_page)];
   1462  1.164.12.3      matt 		l2b = pmap_get_l2_bucket(pm, vector_page);
   1463  1.164.12.3      matt 		pm->pm_l1vec = l2b->l2b_phys | L1_C_PROTO |
   1464  1.164.12.3      matt 		    L1_C_DOM(pm->pm_domain);
   1465  1.164.12.3      matt 	} else
   1466  1.164.12.3      matt 		pm->pm_pl1vec = NULL;
   1467  1.164.12.3      matt }
   1468  1.164.12.3      matt 
   1469  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   1470        1.93   thorpej /*
   1471       1.134   thorpej  * Since we have a virtually indexed cache, we may need to inhibit caching if
   1472       1.134   thorpej  * there is more than one mapping and at least one of them is writable.
   1473       1.134   thorpej  * Since we purge the cache on every context switch, we only need to check for
   1474       1.134   thorpej  * other mappings within the same pmap, or kernel_pmap.
   1475       1.134   thorpej  * This function is also called when a page is unmapped, to possibly reenable
   1476       1.134   thorpej  * caching on any remaining mappings.
   1477       1.134   thorpej  *
   1478       1.134   thorpej  * The code implements the following logic, where:
   1479       1.134   thorpej  *
   1480       1.134   thorpej  * KW = # of kernel read/write pages
   1481       1.134   thorpej  * KR = # of kernel read only pages
   1482       1.134   thorpej  * UW = # of user read/write pages
   1483       1.134   thorpej  * UR = # of user read only pages
   1484       1.134   thorpej  *
   1485       1.134   thorpej  * KC = kernel mapping is cacheable
   1486       1.134   thorpej  * UC = user mapping is cacheable
   1487        1.93   thorpej  *
   1488       1.134   thorpej  *               KW=0,KR=0  KW=0,KR>0  KW=1,KR=0  KW>1,KR>=0
   1489       1.134   thorpej  *             +---------------------------------------------
   1490       1.134   thorpej  * UW=0,UR=0   | ---        KC=1       KC=1       KC=0
   1491       1.134   thorpej  * UW=0,UR>0   | UC=1       KC=1,UC=1  KC=0,UC=0  KC=0,UC=0
   1492       1.134   thorpej  * UW=1,UR=0   | UC=1       KC=0,UC=0  KC=0,UC=0  KC=0,UC=0
   1493       1.134   thorpej  * UW>1,UR>=0  | UC=0       KC=0,UC=0  KC=0,UC=0  KC=0,UC=0
   1494        1.93   thorpej  */
   1495       1.111   thorpej 
   1496       1.134   thorpej static const int pmap_vac_flags[4][4] = {
   1497       1.134   thorpej 	{-1,		0,		0,		PVF_KNC},
   1498       1.134   thorpej 	{0,		0,		PVF_NC,		PVF_NC},
   1499       1.134   thorpej 	{0,		PVF_NC,		PVF_NC,		PVF_NC},
   1500       1.134   thorpej 	{PVF_UNC,	PVF_NC,		PVF_NC,		PVF_NC}
   1501       1.134   thorpej };
   1502        1.93   thorpej 
   1503       1.157     perry static inline int
   1504       1.134   thorpej pmap_get_vac_flags(const struct vm_page *pg)
   1505       1.134   thorpej {
   1506       1.134   thorpej 	int kidx, uidx;
   1507        1.93   thorpej 
   1508       1.134   thorpej 	kidx = 0;
   1509       1.134   thorpej 	if (pg->mdpage.kro_mappings || pg->mdpage.krw_mappings > 1)
   1510       1.134   thorpej 		kidx |= 1;
   1511       1.134   thorpej 	if (pg->mdpage.krw_mappings)
   1512       1.134   thorpej 		kidx |= 2;
   1513       1.134   thorpej 
   1514       1.134   thorpej 	uidx = 0;
   1515       1.134   thorpej 	if (pg->mdpage.uro_mappings || pg->mdpage.urw_mappings > 1)
   1516       1.134   thorpej 		uidx |= 1;
   1517       1.134   thorpej 	if (pg->mdpage.urw_mappings)
   1518       1.134   thorpej 		uidx |= 2;
   1519       1.111   thorpej 
   1520       1.134   thorpej 	return (pmap_vac_flags[uidx][kidx]);
   1521       1.111   thorpej }
   1522       1.111   thorpej 
   1523       1.157     perry static inline void
   1524       1.134   thorpej pmap_vac_me_harder(struct vm_page *pg, pmap_t pm, vaddr_t va)
   1525       1.111   thorpej {
   1526       1.134   thorpej 	int nattr;
   1527       1.134   thorpej 
   1528       1.134   thorpej 	nattr = pmap_get_vac_flags(pg);
   1529       1.111   thorpej 
   1530       1.134   thorpej 	if (nattr < 0) {
   1531       1.134   thorpej 		pg->mdpage.pvh_attrs &= ~PVF_NC;
   1532       1.134   thorpej 		return;
   1533       1.134   thorpej 	}
   1534        1.93   thorpej 
   1535       1.134   thorpej 	if (nattr == 0 && (pg->mdpage.pvh_attrs & PVF_NC) == 0)
   1536       1.134   thorpej 		return;
   1537       1.111   thorpej 
   1538       1.134   thorpej 	if (pm == pmap_kernel())
   1539       1.134   thorpej 		pmap_vac_me_kpmap(pg, pm, va);
   1540       1.134   thorpej 	else
   1541       1.134   thorpej 		pmap_vac_me_user(pg, pm, va);
   1542       1.134   thorpej 
   1543       1.134   thorpej 	pg->mdpage.pvh_attrs = (pg->mdpage.pvh_attrs & ~PVF_NC) | nattr;
   1544        1.93   thorpej }
   1545        1.93   thorpej 
   1546       1.134   thorpej static void
   1547       1.134   thorpej pmap_vac_me_kpmap(struct vm_page *pg, pmap_t pm, vaddr_t va)
   1548         1.1      matt {
   1549       1.134   thorpej 	u_int u_cacheable, u_entries;
   1550       1.134   thorpej 	struct pv_entry *pv;
   1551       1.134   thorpej 	pmap_t last_pmap = pm;
   1552       1.134   thorpej 
   1553       1.134   thorpej 	/*
   1554       1.134   thorpej 	 * Pass one, see if there are both kernel and user pmaps for
   1555       1.134   thorpej 	 * this page.  Calculate whether there are user-writable or
   1556       1.134   thorpej 	 * kernel-writable pages.
   1557       1.134   thorpej 	 */
   1558       1.134   thorpej 	u_cacheable = 0;
   1559       1.134   thorpej 	for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   1560       1.134   thorpej 		if (pv->pv_pmap != pm && (pv->pv_flags & PVF_NC) == 0)
   1561       1.134   thorpej 			u_cacheable++;
   1562         1.1      matt 	}
   1563         1.1      matt 
   1564       1.134   thorpej 	u_entries = pg->mdpage.urw_mappings + pg->mdpage.uro_mappings;
   1565         1.1      matt 
   1566       1.134   thorpej 	/*
   1567       1.134   thorpej 	 * We know we have just been updating a kernel entry, so if
   1568       1.134   thorpej 	 * all user pages are already cacheable, then there is nothing
   1569       1.134   thorpej 	 * further to do.
   1570       1.134   thorpej 	 */
   1571       1.134   thorpej 	if (pg->mdpage.k_mappings == 0 && u_cacheable == u_entries)
   1572       1.134   thorpej 		return;
   1573         1.1      matt 
   1574       1.134   thorpej 	if (u_entries) {
   1575       1.134   thorpej 		/*
   1576       1.134   thorpej 		 * Scan over the list again, for each entry, if it
   1577       1.134   thorpej 		 * might not be set correctly, call pmap_vac_me_user
   1578       1.134   thorpej 		 * to recalculate the settings.
   1579       1.134   thorpej 		 */
   1580       1.134   thorpej 		for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   1581       1.134   thorpej 			/*
   1582       1.134   thorpej 			 * We know kernel mappings will get set
   1583       1.134   thorpej 			 * correctly in other calls.  We also know
   1584       1.134   thorpej 			 * that if the pmap is the same as last_pmap
   1585       1.134   thorpej 			 * then we've just handled this entry.
   1586       1.134   thorpej 			 */
   1587       1.134   thorpej 			if (pv->pv_pmap == pm || pv->pv_pmap == last_pmap)
   1588       1.134   thorpej 				continue;
   1589         1.1      matt 
   1590       1.134   thorpej 			/*
   1591       1.134   thorpej 			 * If there are kernel entries and this page
   1592       1.134   thorpej 			 * is writable but non-cacheable, then we can
   1593       1.134   thorpej 			 * skip this entry also.
   1594       1.134   thorpej 			 */
   1595       1.134   thorpej 			if (pg->mdpage.k_mappings &&
   1596       1.134   thorpej 			    (pv->pv_flags & (PVF_NC | PVF_WRITE)) ==
   1597       1.134   thorpej 			    (PVF_NC | PVF_WRITE))
   1598       1.134   thorpej 				continue;
   1599       1.111   thorpej 
   1600       1.134   thorpej 			/*
   1601       1.134   thorpej 			 * Similarly if there are no kernel-writable
   1602       1.134   thorpej 			 * entries and the page is already
   1603       1.134   thorpej 			 * read-only/cacheable.
   1604       1.134   thorpej 			 */
   1605       1.134   thorpej 			if (pg->mdpage.krw_mappings == 0 &&
   1606       1.134   thorpej 			    (pv->pv_flags & (PVF_NC | PVF_WRITE)) == 0)
   1607       1.134   thorpej 				continue;
   1608         1.5    toshii 
   1609       1.134   thorpej 			/*
   1610       1.134   thorpej 			 * For some of the remaining cases, we know
   1611       1.134   thorpej 			 * that we must recalculate, but for others we
   1612       1.134   thorpej 			 * can't tell if they are correct or not, so
   1613       1.134   thorpej 			 * we recalculate anyway.
   1614       1.134   thorpej 			 */
   1615       1.134   thorpej 			pmap_vac_me_user(pg, (last_pmap = pv->pv_pmap), 0);
   1616       1.134   thorpej 		}
   1617        1.48     chris 
   1618       1.134   thorpej 		if (pg->mdpage.k_mappings == 0)
   1619       1.134   thorpej 			return;
   1620       1.111   thorpej 	}
   1621       1.111   thorpej 
   1622       1.134   thorpej 	pmap_vac_me_user(pg, pm, va);
   1623       1.134   thorpej }
   1624       1.111   thorpej 
   1625       1.134   thorpej static void
   1626       1.134   thorpej pmap_vac_me_user(struct vm_page *pg, pmap_t pm, vaddr_t va)
   1627       1.134   thorpej {
   1628       1.134   thorpej 	pmap_t kpmap = pmap_kernel();
   1629       1.134   thorpej 	struct pv_entry *pv, *npv;
   1630       1.134   thorpej 	struct l2_bucket *l2b;
   1631       1.134   thorpej 	pt_entry_t *ptep, pte;
   1632       1.134   thorpej 	u_int entries = 0;
   1633       1.134   thorpej 	u_int writable = 0;
   1634       1.134   thorpej 	u_int cacheable_entries = 0;
   1635       1.134   thorpej 	u_int kern_cacheable = 0;
   1636       1.134   thorpej 	u_int other_writable = 0;
   1637        1.48     chris 
   1638       1.134   thorpej 	/*
   1639       1.134   thorpej 	 * Count mappings and writable mappings in this pmap.
   1640       1.134   thorpej 	 * Include kernel mappings as part of our own.
   1641       1.134   thorpej 	 * Keep a pointer to the first one.
   1642       1.134   thorpej 	 */
   1643       1.134   thorpej 	for (pv = npv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   1644       1.134   thorpej 		/* Count mappings in the same pmap */
   1645       1.134   thorpej 		if (pm == pv->pv_pmap || kpmap == pv->pv_pmap) {
   1646       1.134   thorpej 			if (entries++ == 0)
   1647       1.134   thorpej 				npv = pv;
   1648         1.1      matt 
   1649       1.134   thorpej 			/* Cacheable mappings */
   1650       1.134   thorpej 			if ((pv->pv_flags & PVF_NC) == 0) {
   1651       1.134   thorpej 				cacheable_entries++;
   1652       1.134   thorpej 				if (kpmap == pv->pv_pmap)
   1653       1.134   thorpej 					kern_cacheable++;
   1654       1.134   thorpej 			}
   1655       1.110   thorpej 
   1656       1.134   thorpej 			/* Writable mappings */
   1657       1.134   thorpej 			if (pv->pv_flags & PVF_WRITE)
   1658       1.134   thorpej 				++writable;
   1659       1.134   thorpej 		} else
   1660       1.134   thorpej 		if (pv->pv_flags & PVF_WRITE)
   1661       1.134   thorpej 			other_writable = 1;
   1662       1.134   thorpej 	}
   1663         1.1      matt 
   1664       1.134   thorpej 	/*
   1665       1.134   thorpej 	 * Enable or disable caching as necessary.
   1666       1.134   thorpej 	 * Note: the first entry might be part of the kernel pmap,
   1667       1.134   thorpej 	 * so we can't assume this is indicative of the state of the
   1668       1.134   thorpej 	 * other (maybe non-kpmap) entries.
   1669       1.134   thorpej 	 */
   1670       1.134   thorpej 	if ((entries > 1 && writable) ||
   1671       1.134   thorpej 	    (entries > 0 && pm == kpmap && other_writable)) {
   1672       1.134   thorpej 		if (cacheable_entries == 0)
   1673       1.134   thorpej 			return;
   1674         1.1      matt 
   1675       1.134   thorpej 		for (pv = npv; pv; pv = pv->pv_next) {
   1676       1.134   thorpej 			if ((pm != pv->pv_pmap && kpmap != pv->pv_pmap) ||
   1677       1.134   thorpej 			    (pv->pv_flags & PVF_NC))
   1678       1.134   thorpej 				continue;
   1679         1.1      matt 
   1680       1.134   thorpej 			pv->pv_flags |= PVF_NC;
   1681        1.26  rearnsha 
   1682       1.134   thorpej 			l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
   1683       1.134   thorpej 			ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
   1684       1.134   thorpej 			pte = *ptep & ~L2_S_CACHE_MASK;
   1685       1.134   thorpej 
   1686       1.134   thorpej 			if ((va != pv->pv_va || pm != pv->pv_pmap) &&
   1687       1.134   thorpej 			    l2pte_valid(pte)) {
   1688       1.134   thorpej 				if (PV_BEEN_EXECD(pv->pv_flags)) {
   1689  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   1690       1.134   thorpej 					pmap_idcache_wbinv_range(pv->pv_pmap,
   1691       1.134   thorpej 					    pv->pv_va, PAGE_SIZE);
   1692  1.164.12.2      matt #endif
   1693       1.134   thorpej 					pmap_tlb_flushID_SE(pv->pv_pmap,
   1694       1.134   thorpej 					    pv->pv_va);
   1695       1.134   thorpej 				} else
   1696       1.134   thorpej 				if (PV_BEEN_REFD(pv->pv_flags)) {
   1697  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   1698       1.134   thorpej 					pmap_dcache_wb_range(pv->pv_pmap,
   1699       1.160   thorpej 					    pv->pv_va, PAGE_SIZE, true,
   1700       1.134   thorpej 					    (pv->pv_flags & PVF_WRITE) == 0);
   1701  1.164.12.2      matt #endif
   1702       1.134   thorpej 					pmap_tlb_flushD_SE(pv->pv_pmap,
   1703       1.134   thorpej 					    pv->pv_va);
   1704       1.134   thorpej 				}
   1705       1.134   thorpej 			}
   1706         1.1      matt 
   1707       1.134   thorpej 			*ptep = pte;
   1708       1.134   thorpej 			PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
   1709       1.134   thorpej 		}
   1710       1.134   thorpej 		cpu_cpwait();
   1711       1.134   thorpej 	} else
   1712       1.134   thorpej 	if (entries > cacheable_entries) {
   1713         1.1      matt 		/*
   1714       1.134   thorpej 		 * Turn cacheing back on for some pages.  If it is a kernel
   1715       1.134   thorpej 		 * page, only do so if there are no other writable pages.
   1716         1.1      matt 		 */
   1717       1.134   thorpej 		for (pv = npv; pv; pv = pv->pv_next) {
   1718       1.134   thorpej 			if (!(pv->pv_flags & PVF_NC) || (pm != pv->pv_pmap &&
   1719       1.134   thorpej 			    (kpmap != pv->pv_pmap || other_writable)))
   1720       1.134   thorpej 				continue;
   1721       1.134   thorpej 
   1722       1.134   thorpej 			pv->pv_flags &= ~PVF_NC;
   1723         1.1      matt 
   1724       1.134   thorpej 			l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
   1725       1.134   thorpej 			ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
   1726       1.134   thorpej 			pte = (*ptep & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode;
   1727       1.134   thorpej 
   1728       1.134   thorpej 			if (l2pte_valid(pte)) {
   1729       1.134   thorpej 				if (PV_BEEN_EXECD(pv->pv_flags)) {
   1730       1.134   thorpej 					pmap_tlb_flushID_SE(pv->pv_pmap,
   1731       1.134   thorpej 					    pv->pv_va);
   1732       1.134   thorpej 				} else
   1733       1.134   thorpej 				if (PV_BEEN_REFD(pv->pv_flags)) {
   1734       1.134   thorpej 					pmap_tlb_flushD_SE(pv->pv_pmap,
   1735       1.134   thorpej 					    pv->pv_va);
   1736       1.134   thorpej 				}
   1737       1.134   thorpej 			}
   1738         1.1      matt 
   1739       1.134   thorpej 			*ptep = pte;
   1740       1.134   thorpej 			PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
   1741       1.134   thorpej 		}
   1742       1.111   thorpej 	}
   1743         1.1      matt }
   1744  1.164.12.2      matt #endif
   1745  1.164.12.2      matt 
   1746  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   1747  1.164.12.2      matt /*
   1748  1.164.12.2      matt  * For virtually indexed / physically tagged caches, what we have to worry
   1749  1.164.12.2      matt  * about is illegal cache aliases.  To prevent this, we must ensure that
   1750  1.164.12.2      matt  * virtual addresses that map the physical page use the same bits for those
   1751  1.164.12.2      matt  * bits masked by "arm_cache_prefer_mask" (bits 12+).  If there is a conflict,
   1752  1.164.12.2      matt  * all mappings of the page must be non-cached.
   1753  1.164.12.2      matt  */
   1754  1.164.12.2      matt #if 0
   1755  1.164.12.2      matt static inline vaddr_t
   1756  1.164.12.2      matt pmap_check_sets(paddr_t pa)
   1757  1.164.12.2      matt {
   1758  1.164.12.2      matt 	extern int arm_dcache_l2_nsets;
   1759  1.164.12.2      matt 	int set, way;
   1760  1.164.12.2      matt 	vaddr_t mask = 0;
   1761  1.164.12.2      matt 	int v;
   1762  1.164.12.2      matt 	pa |= 1;
   1763  1.164.12.2      matt 	for (set = 0; set < (1 << arm_dcache_l2_nsets); set++) {
   1764  1.164.12.2      matt 		for (way = 0; way < 4; way++) {
   1765  1.164.12.2      matt 			v = (way << 30) | (set << 5);
   1766  1.164.12.2      matt 			asm("mcr	p15, 3, %0, c15, c2, 0" :: "r"(v));
   1767  1.164.12.2      matt 			asm("mrc	p15, 3, %0, c15, c0, 0" : "=r"(v));
   1768  1.164.12.2      matt 
   1769  1.164.12.2      matt 			if ((v & (1 | ~(PAGE_SIZE-1))) == pa) {
   1770  1.164.12.2      matt 				mask |= 1 << (set >> 7);
   1771  1.164.12.2      matt 			}
   1772  1.164.12.2      matt 		}
   1773  1.164.12.2      matt 	}
   1774  1.164.12.2      matt 	return mask;
   1775  1.164.12.2      matt }
   1776  1.164.12.2      matt #endif
   1777  1.164.12.2      matt static void
   1778  1.164.12.2      matt pmap_vac_me_harder(struct vm_page *pg, pmap_t pm, vaddr_t va)
   1779  1.164.12.2      matt {
   1780  1.164.12.2      matt 	struct pv_entry *pv, pv0;
   1781  1.164.12.2      matt 	vaddr_t tst_mask;
   1782  1.164.12.2      matt 	bool bad_alias;
   1783  1.164.12.2      matt 	struct l2_bucket *l2b;
   1784  1.164.12.2      matt 	pt_entry_t *ptep, pte, opte;
   1785  1.164.12.2      matt 
   1786  1.164.12.2      matt 	/* do we need to do anything? */
   1787  1.164.12.2      matt 	if (arm_cache_prefer_mask == 0)
   1788  1.164.12.2      matt 		return;
   1789  1.164.12.2      matt 
   1790  1.164.12.2      matt 	NPDEBUG(PDB_VAC, printf("pmap_vac_me_harder: pg=%p, pmap=%p va=%08lx\n",
   1791  1.164.12.2      matt 	    pg, pm, va));
   1792  1.164.12.2      matt 
   1793  1.164.12.2      matt #define popc4(x) \
   1794  1.164.12.2      matt 	(((0x94 >> ((x & 3) << 1)) & 3) + ((0x94 >> ((x & 12) >> 1)) & 3))
   1795  1.164.12.2      matt #if 0
   1796  1.164.12.2      matt 	tst_mask = pmap_check_sets(pg->phys_addr);
   1797  1.164.12.2      matt 	KASSERT(popc4(tst_mask) < 2);
   1798  1.164.12.2      matt #endif
   1799  1.164.12.2      matt 
   1800  1.164.12.2      matt 	KASSERT(!va || pm || (pg->mdpage.pvh_attrs & PVF_KENTRY));
   1801  1.164.12.2      matt 
   1802  1.164.12.2      matt 	/* Already a conflict? */
   1803  1.164.12.2      matt 	if (__predict_false(pg->mdpage.pvh_attrs & PVF_NC)) {
   1804  1.164.12.2      matt 		/* just an add, things are already non-cached */
   1805  1.164.12.2      matt 		bad_alias = false;
   1806  1.164.12.2      matt 		if (va) {
   1807  1.164.12.2      matt 			PMAPCOUNT(vac_color_none);
   1808  1.164.12.2      matt 			bad_alias = true;
   1809  1.164.12.2      matt 			goto fixup;
   1810  1.164.12.2      matt 		}
   1811  1.164.12.2      matt 		pv = pg->mdpage.pvh_list;
   1812  1.164.12.2      matt 		/* the list can't be empty because it would be cachable */
   1813  1.164.12.2      matt 		if (pg->mdpage.pvh_attrs & PVF_KENTRY) {
   1814  1.164.12.2      matt 			tst_mask = pg->mdpage.pvh_attrs;
   1815  1.164.12.2      matt 		} else {
   1816  1.164.12.2      matt 			KASSERT(pv);
   1817  1.164.12.2      matt 			tst_mask = pv->pv_va;
   1818  1.164.12.2      matt 			pv = pv->pv_next;
   1819  1.164.12.2      matt 		}
   1820  1.164.12.2      matt 		tst_mask &= arm_cache_prefer_mask;
   1821  1.164.12.2      matt 		for (; pv && !bad_alias; pv = pv->pv_next) {
   1822  1.164.12.2      matt 			/* if there's a bad alias, stop checking. */
   1823  1.164.12.2      matt 			if (tst_mask != (pv->pv_va & arm_cache_prefer_mask))
   1824  1.164.12.2      matt 				bad_alias = true;
   1825  1.164.12.2      matt 		}
   1826  1.164.12.2      matt 		/* If no conflicting colors, set everything back to cached */
   1827  1.164.12.2      matt 		if (!bad_alias) {
   1828  1.164.12.2      matt 			PMAPCOUNT(vac_color_restore);
   1829  1.164.12.2      matt 			pg->mdpage.pvh_attrs |= PVF_COLORED;
   1830  1.164.12.2      matt 			if (!(pg->mdpage.pvh_attrs & PVF_KENTRY)) {
   1831  1.164.12.2      matt 				pg->mdpage.pvh_attrs &= PAGE_SIZE - 1;
   1832  1.164.12.2      matt 				pg->mdpage.pvh_attrs |= tst_mask;
   1833  1.164.12.2      matt 			}
   1834  1.164.12.2      matt 			pg->mdpage.pvh_attrs &= ~PVF_NC;
   1835  1.164.12.2      matt 		} else {
   1836  1.164.12.2      matt 			KASSERT(pg->mdpage.pvh_list != NULL);
   1837  1.164.12.2      matt 			KASSERT((pg->mdpage.pvh_attrs & PVF_KENTRY)
   1838  1.164.12.2      matt 			     || pg->mdpage.pvh_list->pv_next != NULL);
   1839  1.164.12.2      matt 		}
   1840  1.164.12.2      matt 	} else if (!va) {
   1841  1.164.12.2      matt 		KASSERT(pmap_is_page_colored_p(pg));
   1842  1.164.12.2      matt 		if (pm == NULL)
   1843  1.164.12.2      matt 			pg->mdpage.pvh_attrs &=
   1844  1.164.12.2      matt 			    (PAGE_SIZE - 1) | arm_cache_prefer_mask;
   1845  1.164.12.2      matt 		return;
   1846  1.164.12.2      matt 	} else if (!pmap_is_page_colored_p(pg)) {
   1847  1.164.12.2      matt 		/* not colored so we just use its color */
   1848  1.164.12.2      matt 		PMAPCOUNT(vac_color_new);
   1849  1.164.12.2      matt 		pg->mdpage.pvh_attrs &= PAGE_SIZE - 1;
   1850  1.164.12.2      matt 		if (pm == NULL)
   1851  1.164.12.2      matt 			pg->mdpage.pvh_attrs |= PVF_COLORED | va;
   1852  1.164.12.2      matt 		else
   1853  1.164.12.2      matt 			pg->mdpage.pvh_attrs |= PVF_COLORED
   1854  1.164.12.2      matt 			    | (va & arm_cache_prefer_mask);
   1855  1.164.12.2      matt 		return;
   1856  1.164.12.2      matt 	} else if (!((pg->mdpage.pvh_attrs ^ va) & arm_cache_prefer_mask)) {
   1857  1.164.12.2      matt 		if (pm == NULL) {
   1858  1.164.12.2      matt 			pg->mdpage.pvh_attrs &= PAGE_SIZE - 1;
   1859  1.164.12.2      matt 			pg->mdpage.pvh_attrs |= va;
   1860  1.164.12.2      matt 		}
   1861  1.164.12.2      matt 		if (pg->mdpage.pvh_list)
   1862  1.164.12.2      matt 			PMAPCOUNT(vac_color_reuse);
   1863  1.164.12.2      matt 		else
   1864  1.164.12.2      matt 			PMAPCOUNT(vac_color_ok);
   1865  1.164.12.2      matt 		/* matching color, just return */
   1866  1.164.12.2      matt 		return;
   1867  1.164.12.2      matt 	} else {
   1868  1.164.12.2      matt 		/* color conflict.  evict from cache. */
   1869  1.164.12.2      matt 		pmap_flush_page(pg);
   1870  1.164.12.2      matt 
   1871  1.164.12.2      matt 		/* the list can't be empty because this was a enter/modify */
   1872  1.164.12.2      matt 		pv = pg->mdpage.pvh_list;
   1873  1.164.12.2      matt 		KASSERT((pg->mdpage.pvh_attrs & PVF_KENTRY) || pv);
   1874  1.164.12.2      matt 
   1875  1.164.12.2      matt 		/*
   1876  1.164.12.2      matt 		 * If there's only one mapped page, change color to the
   1877  1.164.12.2      matt 		 * page's new color and return.
   1878  1.164.12.2      matt 		 */
   1879  1.164.12.2      matt 		if (((pg->mdpage.pvh_attrs & PVF_KENTRY)
   1880  1.164.12.2      matt 		    ? pv : pv->pv_next) == NULL) {
   1881  1.164.12.2      matt 			PMAPCOUNT(vac_color_change);
   1882  1.164.12.2      matt 			pg->mdpage.pvh_attrs &= PAGE_SIZE - 1;
   1883  1.164.12.2      matt 			if (pm == NULL)
   1884  1.164.12.2      matt 				pg->mdpage.pvh_attrs |= va;
   1885  1.164.12.2      matt 			else
   1886  1.164.12.2      matt 				pg->mdpage.pvh_attrs |=
   1887  1.164.12.2      matt 				    (va & arm_cache_prefer_mask);
   1888  1.164.12.2      matt 			return;
   1889  1.164.12.2      matt 		}
   1890  1.164.12.2      matt 		bad_alias = true;
   1891  1.164.12.2      matt 		pg->mdpage.pvh_attrs &= ~PVF_COLORED;
   1892  1.164.12.2      matt 		pg->mdpage.pvh_attrs |= PVF_NC;
   1893  1.164.12.2      matt 		PMAPCOUNT(vac_color_erase);
   1894  1.164.12.2      matt 	}
   1895  1.164.12.2      matt 
   1896  1.164.12.2      matt   fixup:
   1897  1.164.12.2      matt 	/*
   1898  1.164.12.2      matt 	 * If the pmap is NULL, then we got called from pmap_kenter_pa
   1899  1.164.12.2      matt 	 * and we must save the kenter'ed va.  And this changes the
   1900  1.164.12.2      matt 	 * color to match the kenter'ed page.  if this is a remove clear
   1901  1.164.12.2      matt 	 * saved va bits which retaining the color bits.
   1902  1.164.12.2      matt 	 */
   1903  1.164.12.2      matt 	if (pm == NULL) {
   1904  1.164.12.2      matt 		if (va) {
   1905  1.164.12.2      matt 			pg->mdpage.pvh_attrs &= (PAGE_SIZE - 1);
   1906  1.164.12.2      matt 			pg->mdpage.pvh_attrs |= va;
   1907  1.164.12.2      matt 		} else {
   1908  1.164.12.2      matt 			pg->mdpage.pvh_attrs &=
   1909  1.164.12.2      matt 			    ((PAGE_SIZE - 1) | arm_cache_prefer_mask);
   1910  1.164.12.2      matt 		}
   1911  1.164.12.2      matt 	}
   1912  1.164.12.2      matt 
   1913  1.164.12.2      matt 	pv = pg->mdpage.pvh_list;
   1914  1.164.12.2      matt 
   1915  1.164.12.2      matt 	/*
   1916  1.164.12.2      matt 	 * If this page has an kenter'ed mapping, fake up a pv entry.
   1917  1.164.12.2      matt 	 */
   1918  1.164.12.2      matt 	if (__predict_false(pg->mdpage.pvh_attrs & PVF_KENTRY)) {
   1919  1.164.12.2      matt 		pv0.pv_pmap = pmap_kernel();
   1920  1.164.12.2      matt 		pv0.pv_va = pg->mdpage.pvh_attrs & ~(PAGE_SIZE - 1);
   1921  1.164.12.2      matt 		pv0.pv_next = pv;
   1922  1.164.12.2      matt 		pv0.pv_flags = PVF_REF;
   1923  1.164.12.2      matt 		pv = &pv0;
   1924  1.164.12.2      matt 	}
   1925  1.164.12.2      matt 
   1926  1.164.12.2      matt 	/*
   1927  1.164.12.2      matt 	 * Turn cacheing on/off for all pages.
   1928  1.164.12.2      matt 	 */
   1929  1.164.12.2      matt 	for (; pv; pv = pv->pv_next) {
   1930  1.164.12.2      matt 		l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
   1931  1.164.12.2      matt 		ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
   1932  1.164.12.2      matt 		opte = *ptep;
   1933  1.164.12.2      matt 		pte = opte & ~L2_S_CACHE_MASK;
   1934  1.164.12.2      matt 		if (bad_alias) {
   1935  1.164.12.2      matt 			pv->pv_flags |= PVF_NC;
   1936  1.164.12.2      matt 		} else {
   1937  1.164.12.2      matt 			pv->pv_flags &= ~PVF_NC;
   1938  1.164.12.2      matt 			pte |= pte_l2_s_cache_mode;
   1939  1.164.12.2      matt 		}
   1940  1.164.12.2      matt 		if (opte == pte)	/* only update is there's a change */
   1941  1.164.12.2      matt 			continue;
   1942  1.164.12.2      matt 
   1943  1.164.12.2      matt 		if (l2pte_valid(pte)) {
   1944  1.164.12.2      matt 			if (PV_BEEN_EXECD(pv->pv_flags)) {
   1945  1.164.12.2      matt 				pmap_tlb_flushID_SE(pv->pv_pmap, pv->pv_va);
   1946  1.164.12.2      matt 			} else if (PV_BEEN_REFD(pv->pv_flags)) {
   1947  1.164.12.2      matt 				pmap_tlb_flushD_SE(pv->pv_pmap, pv->pv_va);
   1948  1.164.12.2      matt 			}
   1949  1.164.12.2      matt 		}
   1950  1.164.12.2      matt 
   1951  1.164.12.2      matt 		*ptep = pte;
   1952  1.164.12.2      matt 		PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
   1953  1.164.12.2      matt 	}
   1954  1.164.12.2      matt }
   1955  1.164.12.2      matt #endif	/* PMAP_CACHE_VIPT */
   1956  1.164.12.2      matt 
   1957         1.1      matt 
   1958         1.1      matt /*
   1959       1.134   thorpej  * Modify pte bits for all ptes corresponding to the given physical address.
   1960       1.134   thorpej  * We use `maskbits' rather than `clearbits' because we're always passing
   1961       1.134   thorpej  * constants and the latter would require an extra inversion at run-time.
   1962         1.1      matt  */
   1963       1.134   thorpej static void
   1964       1.134   thorpej pmap_clearbit(struct vm_page *pg, u_int maskbits)
   1965         1.1      matt {
   1966       1.134   thorpej 	struct l2_bucket *l2b;
   1967       1.134   thorpej 	struct pv_entry *pv;
   1968       1.134   thorpej 	pt_entry_t *ptep, npte, opte;
   1969       1.134   thorpej 	pmap_t pm;
   1970       1.134   thorpej 	vaddr_t va;
   1971       1.134   thorpej 	u_int oflags;
   1972  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   1973  1.164.12.2      matt 	const bool want_syncicache = PV_IS_EXEC_P(pg->mdpage.pvh_attrs);
   1974  1.164.12.2      matt 	bool need_syncicache = false;
   1975  1.164.12.2      matt 	bool did_syncicache = false;
   1976  1.164.12.2      matt #endif
   1977         1.1      matt 
   1978       1.134   thorpej 	NPDEBUG(PDB_BITS,
   1979       1.134   thorpej 	    printf("pmap_clearbit: pg %p (0x%08lx) mask 0x%x\n",
   1980       1.155      yamt 	    pg, VM_PAGE_TO_PHYS(pg), maskbits));
   1981         1.1      matt 
   1982       1.134   thorpej 	PMAP_HEAD_TO_MAP_LOCK();
   1983       1.134   thorpej 	simple_lock(&pg->mdpage.pvh_slock);
   1984        1.17     chris 
   1985  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   1986  1.164.12.2      matt 	/*
   1987  1.164.12.2      matt 	 * If we might want to sync the I-cache and we've modified it,
   1988  1.164.12.2      matt 	 * then we know we definitely need to sync or discard it.
   1989  1.164.12.2      matt 	 */
   1990  1.164.12.2      matt 	if (want_syncicache)
   1991  1.164.12.2      matt 		need_syncicache = pg->mdpage.pvh_attrs & PVF_MOD;
   1992  1.164.12.2      matt #endif
   1993        1.17     chris 	/*
   1994       1.134   thorpej 	 * Clear saved attributes (modify, reference)
   1995        1.17     chris 	 */
   1996       1.134   thorpej 	pg->mdpage.pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF));
   1997       1.134   thorpej 
   1998       1.134   thorpej 	if (pg->mdpage.pvh_list == NULL) {
   1999  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   2000  1.164.12.2      matt 		if (need_syncicache) {
   2001  1.164.12.2      matt 			/*
   2002  1.164.12.2      matt 			 * No one has it mapped, so just discard it.  The next
   2003  1.164.12.2      matt 			 * exec remapping will cause it to be synced.
   2004  1.164.12.2      matt 			 */
   2005  1.164.12.2      matt 			pg->mdpage.pvh_attrs &= ~PVF_EXEC;
   2006  1.164.12.2      matt 			PMAPCOUNT(exec_discarded_clearbit);
   2007  1.164.12.2      matt 		}
   2008  1.164.12.2      matt #endif
   2009       1.134   thorpej 		simple_unlock(&pg->mdpage.pvh_slock);
   2010       1.134   thorpej 		PMAP_HEAD_TO_MAP_UNLOCK();
   2011        1.17     chris 		return;
   2012         1.1      matt 	}
   2013         1.1      matt 
   2014        1.17     chris 	/*
   2015       1.134   thorpej 	 * Loop over all current mappings setting/clearing as appropos
   2016        1.17     chris 	 */
   2017       1.134   thorpej 	for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   2018       1.134   thorpej 		va = pv->pv_va;
   2019       1.134   thorpej 		pm = pv->pv_pmap;
   2020       1.134   thorpej 		oflags = pv->pv_flags;
   2021       1.134   thorpej 		pv->pv_flags &= ~maskbits;
   2022        1.48     chris 
   2023       1.134   thorpej 		pmap_acquire_pmap_lock(pm);
   2024        1.48     chris 
   2025       1.134   thorpej 		l2b = pmap_get_l2_bucket(pm, va);
   2026       1.134   thorpej 		KDASSERT(l2b != NULL);
   2027         1.1      matt 
   2028       1.134   thorpej 		ptep = &l2b->l2b_kva[l2pte_index(va)];
   2029       1.134   thorpej 		npte = opte = *ptep;
   2030       1.114   thorpej 
   2031       1.134   thorpej 		NPDEBUG(PDB_BITS,
   2032       1.134   thorpej 		    printf(
   2033       1.134   thorpej 		    "pmap_clearbit: pv %p, pm %p, va 0x%08lx, flag 0x%x\n",
   2034       1.134   thorpej 		    pv, pv->pv_pmap, pv->pv_va, oflags));
   2035       1.114   thorpej 
   2036       1.134   thorpej 		if (maskbits & (PVF_WRITE|PVF_MOD)) {
   2037  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   2038       1.134   thorpej 			if ((pv->pv_flags & PVF_NC)) {
   2039       1.134   thorpej 				/*
   2040       1.134   thorpej 				 * Entry is not cacheable:
   2041       1.134   thorpej 				 *
   2042       1.134   thorpej 				 * Don't turn caching on again if this is a
   2043       1.134   thorpej 				 * modified emulation. This would be
   2044       1.134   thorpej 				 * inconsitent with the settings created by
   2045       1.134   thorpej 				 * pmap_vac_me_harder(). Otherwise, it's safe
   2046       1.134   thorpej 				 * to re-enable cacheing.
   2047       1.134   thorpej 				 *
   2048       1.134   thorpej 				 * There's no need to call pmap_vac_me_harder()
   2049       1.134   thorpej 				 * here: all pages are losing their write
   2050       1.134   thorpej 				 * permission.
   2051       1.134   thorpej 				 */
   2052       1.134   thorpej 				if (maskbits & PVF_WRITE) {
   2053       1.134   thorpej 					npte |= pte_l2_s_cache_mode;
   2054       1.134   thorpej 					pv->pv_flags &= ~PVF_NC;
   2055       1.134   thorpej 				}
   2056       1.134   thorpej 			} else
   2057       1.134   thorpej 			if (opte & L2_S_PROT_W) {
   2058       1.134   thorpej 				/*
   2059       1.134   thorpej 				 * Entry is writable/cacheable: check if pmap
   2060       1.134   thorpej 				 * is current if it is flush it, otherwise it
   2061       1.134   thorpej 				 * won't be in the cache
   2062       1.134   thorpej 				 */
   2063       1.134   thorpej 				if (PV_BEEN_EXECD(oflags))
   2064       1.134   thorpej 					pmap_idcache_wbinv_range(pm, pv->pv_va,
   2065       1.134   thorpej 					    PAGE_SIZE);
   2066       1.134   thorpej 				else
   2067       1.134   thorpej 				if (PV_BEEN_REFD(oflags))
   2068       1.134   thorpej 					pmap_dcache_wb_range(pm, pv->pv_va,
   2069       1.134   thorpej 					    PAGE_SIZE,
   2070  1.164.12.2      matt 					    (maskbits & PVF_REF) != 0, false);
   2071       1.134   thorpej 			}
   2072  1.164.12.2      matt #endif
   2073       1.111   thorpej 
   2074       1.134   thorpej 			/* make the pte read only */
   2075       1.134   thorpej 			npte &= ~L2_S_PROT_W;
   2076       1.111   thorpej 
   2077  1.164.12.2      matt 			if (maskbits & oflags & PVF_WRITE) {
   2078       1.134   thorpej 				/*
   2079       1.134   thorpej 				 * Keep alias accounting up to date
   2080       1.134   thorpej 				 */
   2081       1.134   thorpej 				if (pv->pv_pmap == pmap_kernel()) {
   2082  1.164.12.2      matt 					pg->mdpage.krw_mappings--;
   2083  1.164.12.2      matt 					pg->mdpage.kro_mappings++;
   2084  1.164.12.2      matt 				} else {
   2085       1.134   thorpej 					pg->mdpage.urw_mappings--;
   2086       1.134   thorpej 					pg->mdpage.uro_mappings++;
   2087       1.134   thorpej 				}
   2088  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   2089  1.164.12.2      matt 				if (want_syncicache)
   2090  1.164.12.2      matt 					need_syncicache = true;
   2091  1.164.12.2      matt #endif
   2092       1.134   thorpej 			}
   2093       1.134   thorpej 		}
   2094         1.1      matt 
   2095       1.134   thorpej 		if (maskbits & PVF_REF) {
   2096  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   2097       1.134   thorpej 			if ((pv->pv_flags & PVF_NC) == 0 &&
   2098  1.164.12.2      matt 			    (maskbits & (PVF_WRITE|PVF_MOD)) == 0 &&
   2099  1.164.12.2      matt 			    l2pte_valid(npte)) {
   2100       1.134   thorpej 				/*
   2101       1.134   thorpej 				 * Check npte here; we may have already
   2102       1.134   thorpej 				 * done the wbinv above, and the validity
   2103       1.134   thorpej 				 * of the PTE is the same for opte and
   2104       1.134   thorpej 				 * npte.
   2105       1.134   thorpej 				 */
   2106  1.164.12.2      matt 				/* XXXJRT need idcache_inv_range */
   2107  1.164.12.2      matt 				if (PV_BEEN_EXECD(oflags))
   2108  1.164.12.2      matt 					pmap_idcache_wbinv_range(pm,
   2109  1.164.12.2      matt 					    pv->pv_va, PAGE_SIZE);
   2110  1.164.12.2      matt 				else
   2111  1.164.12.2      matt 				if (PV_BEEN_REFD(oflags))
   2112  1.164.12.2      matt 					pmap_dcache_wb_range(pm,
   2113  1.164.12.2      matt 					    pv->pv_va, PAGE_SIZE,
   2114  1.164.12.2      matt 					    true, true);
   2115       1.134   thorpej 			}
   2116  1.164.12.2      matt #endif
   2117         1.1      matt 
   2118       1.134   thorpej 			/*
   2119       1.134   thorpej 			 * Make the PTE invalid so that we will take a
   2120       1.134   thorpej 			 * page fault the next time the mapping is
   2121       1.134   thorpej 			 * referenced.
   2122       1.134   thorpej 			 */
   2123       1.134   thorpej 			npte &= ~L2_TYPE_MASK;
   2124       1.134   thorpej 			npte |= L2_TYPE_INV;
   2125       1.134   thorpej 		}
   2126         1.1      matt 
   2127       1.134   thorpej 		if (npte != opte) {
   2128       1.134   thorpej 			*ptep = npte;
   2129       1.134   thorpej 			PTE_SYNC(ptep);
   2130       1.134   thorpej 			/* Flush the TLB entry if a current pmap. */
   2131       1.134   thorpej 			if (PV_BEEN_EXECD(oflags))
   2132       1.134   thorpej 				pmap_tlb_flushID_SE(pm, pv->pv_va);
   2133       1.134   thorpej 			else
   2134       1.134   thorpej 			if (PV_BEEN_REFD(oflags))
   2135       1.134   thorpej 				pmap_tlb_flushD_SE(pm, pv->pv_va);
   2136       1.134   thorpej 		}
   2137         1.1      matt 
   2138       1.134   thorpej 		pmap_release_pmap_lock(pm);
   2139       1.133   thorpej 
   2140       1.134   thorpej 		NPDEBUG(PDB_BITS,
   2141       1.134   thorpej 		    printf("pmap_clearbit: pm %p va 0x%lx opte 0x%08x npte 0x%08x\n",
   2142       1.134   thorpej 		    pm, va, opte, npte));
   2143       1.134   thorpej 	}
   2144       1.133   thorpej 
   2145  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   2146  1.164.12.2      matt 	/*
   2147  1.164.12.2      matt 	 * If we need to sync the I-cache and we haven't done it yet, do it.
   2148  1.164.12.2      matt 	 */
   2149  1.164.12.2      matt 	if (need_syncicache && !did_syncicache) {
   2150  1.164.12.2      matt 		pmap_syncicache_page(pg);
   2151  1.164.12.2      matt 		PMAPCOUNT(exec_synced_clearbit);
   2152  1.164.12.2      matt 	}
   2153  1.164.12.2      matt #endif
   2154  1.164.12.2      matt 
   2155       1.134   thorpej 	simple_unlock(&pg->mdpage.pvh_slock);
   2156       1.134   thorpej 	PMAP_HEAD_TO_MAP_UNLOCK();
   2157         1.1      matt }
   2158         1.1      matt 
   2159         1.1      matt /*
   2160       1.134   thorpej  * pmap_clean_page()
   2161       1.134   thorpej  *
   2162       1.134   thorpej  * This is a local function used to work out the best strategy to clean
   2163       1.134   thorpej  * a single page referenced by its entry in the PV table. It's used by
   2164       1.134   thorpej  * pmap_copy_page, pmap_zero page and maybe some others later on.
   2165       1.134   thorpej  *
   2166       1.134   thorpej  * Its policy is effectively:
   2167       1.134   thorpej  *  o If there are no mappings, we don't bother doing anything with the cache.
   2168       1.134   thorpej  *  o If there is one mapping, we clean just that page.
   2169       1.134   thorpej  *  o If there are multiple mappings, we clean the entire cache.
   2170       1.134   thorpej  *
   2171       1.134   thorpej  * So that some functions can be further optimised, it returns 0 if it didn't
   2172       1.134   thorpej  * clean the entire cache, or 1 if it did.
   2173       1.134   thorpej  *
   2174       1.134   thorpej  * XXX One bug in this routine is that if the pv_entry has a single page
   2175       1.134   thorpej  * mapped at 0x00000000 a whole cache clean will be performed rather than
   2176       1.134   thorpej  * just the 1 page. Since this should not occur in everyday use and if it does
   2177       1.134   thorpej  * it will just result in not the most efficient clean for the page.
   2178         1.1      matt  */
   2179  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   2180       1.134   thorpej static int
   2181       1.159   thorpej pmap_clean_page(struct pv_entry *pv, bool is_src)
   2182         1.1      matt {
   2183       1.134   thorpej 	pmap_t pm, pm_to_clean = NULL;
   2184       1.134   thorpej 	struct pv_entry *npv;
   2185       1.134   thorpej 	u_int cache_needs_cleaning = 0;
   2186       1.134   thorpej 	u_int flags = 0;
   2187       1.134   thorpej 	vaddr_t page_to_clean = 0;
   2188         1.1      matt 
   2189       1.134   thorpej 	if (pv == NULL) {
   2190       1.134   thorpej 		/* nothing mapped in so nothing to flush */
   2191        1.17     chris 		return (0);
   2192       1.108   thorpej 	}
   2193        1.17     chris 
   2194       1.108   thorpej 	/*
   2195       1.134   thorpej 	 * Since we flush the cache each time we change to a different
   2196       1.134   thorpej 	 * user vmspace, we only need to flush the page if it is in the
   2197       1.134   thorpej 	 * current pmap.
   2198        1.17     chris 	 */
   2199        1.17     chris 	if (curproc)
   2200       1.134   thorpej 		pm = curproc->p_vmspace->vm_map.pmap;
   2201        1.17     chris 	else
   2202       1.134   thorpej 		pm = pmap_kernel();
   2203        1.17     chris 
   2204        1.17     chris 	for (npv = pv; npv; npv = npv->pv_next) {
   2205       1.134   thorpej 		if (npv->pv_pmap == pmap_kernel() || npv->pv_pmap == pm) {
   2206       1.134   thorpej 			flags |= npv->pv_flags;
   2207       1.108   thorpej 			/*
   2208       1.108   thorpej 			 * The page is mapped non-cacheable in
   2209        1.17     chris 			 * this map.  No need to flush the cache.
   2210        1.17     chris 			 */
   2211        1.78   thorpej 			if (npv->pv_flags & PVF_NC) {
   2212        1.17     chris #ifdef DIAGNOSTIC
   2213        1.17     chris 				if (cache_needs_cleaning)
   2214        1.17     chris 					panic("pmap_clean_page: "
   2215       1.108   thorpej 					    "cache inconsistency");
   2216        1.17     chris #endif
   2217        1.17     chris 				break;
   2218       1.108   thorpej 			} else if (is_src && (npv->pv_flags & PVF_WRITE) == 0)
   2219        1.17     chris 				continue;
   2220       1.108   thorpej 			if (cache_needs_cleaning) {
   2221        1.17     chris 				page_to_clean = 0;
   2222        1.17     chris 				break;
   2223       1.134   thorpej 			} else {
   2224        1.17     chris 				page_to_clean = npv->pv_va;
   2225       1.134   thorpej 				pm_to_clean = npv->pv_pmap;
   2226       1.134   thorpej 			}
   2227       1.134   thorpej 			cache_needs_cleaning = 1;
   2228        1.17     chris 		}
   2229         1.1      matt 	}
   2230         1.1      matt 
   2231       1.108   thorpej 	if (page_to_clean) {
   2232       1.134   thorpej 		if (PV_BEEN_EXECD(flags))
   2233       1.134   thorpej 			pmap_idcache_wbinv_range(pm_to_clean, page_to_clean,
   2234       1.134   thorpej 			    PAGE_SIZE);
   2235       1.134   thorpej 		else
   2236       1.134   thorpej 			pmap_dcache_wb_range(pm_to_clean, page_to_clean,
   2237       1.134   thorpej 			    PAGE_SIZE, !is_src, (flags & PVF_WRITE) == 0);
   2238       1.108   thorpej 	} else if (cache_needs_cleaning) {
   2239       1.134   thorpej 		if (PV_BEEN_EXECD(flags))
   2240       1.134   thorpej 			pmap_idcache_wbinv_all(pm);
   2241       1.134   thorpej 		else
   2242       1.134   thorpej 			pmap_dcache_wbinv_all(pm);
   2243         1.1      matt 		return (1);
   2244         1.1      matt 	}
   2245         1.1      matt 	return (0);
   2246         1.1      matt }
   2247  1.164.12.2      matt #endif
   2248  1.164.12.2      matt 
   2249  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   2250  1.164.12.2      matt /*
   2251  1.164.12.2      matt  * Sync a page with the I-cache.  Since this is a VIPT, we must pick the
   2252  1.164.12.2      matt  * right cache alias to make sure we flush the right stuff.
   2253  1.164.12.2      matt  */
   2254  1.164.12.2      matt void
   2255  1.164.12.2      matt pmap_syncicache_page(struct vm_page *pg)
   2256  1.164.12.2      matt {
   2257  1.164.12.2      matt 	const vsize_t va_offset = pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
   2258  1.164.12.2      matt 	pt_entry_t * const ptep = &cdst_pte[va_offset >> PGSHIFT];
   2259  1.164.12.2      matt 
   2260  1.164.12.2      matt 	NPDEBUG(PDB_EXEC, printf("pmap_syncicache_page: pg=%p (attrs=%#x)\n",
   2261  1.164.12.2      matt 	    pg, pg->mdpage.pvh_attrs));
   2262  1.164.12.2      matt 	/*
   2263  1.164.12.2      matt 	 * No need to clean the page if it's non-cached.
   2264  1.164.12.2      matt 	 */
   2265  1.164.12.2      matt 	if (pg->mdpage.pvh_attrs & PVF_NC)
   2266  1.164.12.2      matt 		return;
   2267  1.164.12.2      matt 	KASSERT(pg->mdpage.pvh_attrs & PVF_COLORED);
   2268  1.164.12.2      matt 
   2269  1.164.12.2      matt 	pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
   2270  1.164.12.2      matt 	/*
   2271  1.164.12.2      matt 	 * Set up a PTE with the right coloring to flush existing cache lines.
   2272  1.164.12.2      matt 	 */
   2273  1.164.12.2      matt 	*ptep = L2_S_PROTO |
   2274  1.164.12.2      matt 	    VM_PAGE_TO_PHYS(pg)
   2275  1.164.12.2      matt 	    | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE)
   2276  1.164.12.2      matt 	    | pte_l2_s_cache_mode;
   2277  1.164.12.2      matt 	PTE_SYNC(ptep);
   2278  1.164.12.2      matt 
   2279  1.164.12.2      matt 	/*
   2280  1.164.12.2      matt 	 * Flush it.
   2281  1.164.12.2      matt 	 */
   2282  1.164.12.2      matt 	cpu_icache_sync_range(cdstp + va_offset, PAGE_SIZE);
   2283  1.164.12.2      matt 	/*
   2284  1.164.12.2      matt 	 * Unmap the page.
   2285  1.164.12.2      matt 	 */
   2286  1.164.12.2      matt 	*ptep = 0;
   2287  1.164.12.2      matt 	PTE_SYNC(ptep);
   2288  1.164.12.2      matt 	pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
   2289  1.164.12.2      matt 
   2290  1.164.12.2      matt 	pg->mdpage.pvh_attrs |= PVF_EXEC;
   2291  1.164.12.2      matt 	PMAPCOUNT(exec_synced);
   2292  1.164.12.2      matt }
   2293  1.164.12.2      matt 
   2294  1.164.12.2      matt void
   2295  1.164.12.2      matt pmap_flush_page(struct vm_page *pg)
   2296  1.164.12.2      matt {
   2297  1.164.12.2      matt 	const vsize_t va_offset = pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
   2298  1.164.12.2      matt 	const size_t pte_offset = va_offset >> PGSHIFT;
   2299  1.164.12.2      matt 	pt_entry_t * const ptep = &cdst_pte[pte_offset];
   2300  1.164.12.2      matt #if 0
   2301  1.164.12.2      matt 	vaddr_t mask;
   2302  1.164.12.2      matt #endif
   2303  1.164.12.2      matt 
   2304  1.164.12.2      matt 	KASSERT(!(pg->mdpage.pvh_attrs & PVF_NC));
   2305  1.164.12.2      matt #if 0
   2306  1.164.12.2      matt 	mask = pmap_check_sets(pg->phys_addr);
   2307  1.164.12.2      matt 	KASSERT(popc4(mask) < 2);
   2308  1.164.12.2      matt #endif
   2309  1.164.12.2      matt 
   2310  1.164.12.2      matt 	NPDEBUG(PDB_VAC, printf("pmap_flush_page: pg=%p (attrs=%#x)\n",
   2311  1.164.12.2      matt 	    pg, pg->mdpage.pvh_attrs));
   2312  1.164.12.2      matt 	pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
   2313  1.164.12.2      matt 	/*
   2314  1.164.12.2      matt 	 * Set up a PTE with the right coloring to flush existing cache entries.
   2315  1.164.12.2      matt 	 */
   2316  1.164.12.2      matt 	*ptep = L2_S_PROTO
   2317  1.164.12.2      matt 	    | VM_PAGE_TO_PHYS(pg)
   2318  1.164.12.2      matt 	    | L2_S_PROT(PTE_KERNEL, VM_PROT_READ|VM_PROT_WRITE)
   2319  1.164.12.2      matt 	    | pte_l2_s_cache_mode;
   2320  1.164.12.2      matt 	PTE_SYNC(ptep);
   2321  1.164.12.2      matt 
   2322  1.164.12.2      matt 	/*
   2323  1.164.12.2      matt 	 * Flush it.
   2324  1.164.12.2      matt 	 */
   2325  1.164.12.2      matt 	cpu_idcache_wbinv_range(cdstp + va_offset, PAGE_SIZE);
   2326  1.164.12.2      matt 
   2327  1.164.12.2      matt 	/*
   2328  1.164.12.2      matt 	 * Unmap the page.
   2329  1.164.12.2      matt 	 */
   2330  1.164.12.2      matt 	*ptep = 0;
   2331  1.164.12.2      matt 	PTE_SYNC(ptep);
   2332  1.164.12.2      matt 	pmap_tlb_flushID_SE(pmap_kernel(), cdstp + va_offset);
   2333  1.164.12.2      matt #if 0
   2334  1.164.12.2      matt 	mask = pmap_check_sets(pg->phys_addr);
   2335  1.164.12.2      matt 	KASSERT(mask == 0);
   2336  1.164.12.2      matt #endif
   2337  1.164.12.2      matt }
   2338  1.164.12.2      matt #endif /* PMAP_CACHE_VIPT */
   2339         1.1      matt 
   2340         1.1      matt /*
   2341       1.134   thorpej  * Routine:	pmap_page_remove
   2342       1.134   thorpej  * Function:
   2343       1.134   thorpej  *		Removes this physical page from
   2344       1.134   thorpej  *		all physical maps in which it resides.
   2345       1.134   thorpej  *		Reflects back modify bits to the pager.
   2346         1.1      matt  */
   2347       1.134   thorpej static void
   2348       1.134   thorpej pmap_page_remove(struct vm_page *pg)
   2349         1.1      matt {
   2350       1.134   thorpej 	struct l2_bucket *l2b;
   2351       1.134   thorpej 	struct pv_entry *pv, *npv;
   2352       1.134   thorpej 	pmap_t pm, curpm;
   2353       1.134   thorpej 	pt_entry_t *ptep, pte;
   2354       1.159   thorpej 	bool flush;
   2355       1.134   thorpej 	u_int flags;
   2356       1.134   thorpej 
   2357       1.134   thorpej 	NPDEBUG(PDB_FOLLOW,
   2358       1.155      yamt 	    printf("pmap_page_remove: pg %p (0x%08lx)\n", pg,
   2359       1.155      yamt 	    VM_PAGE_TO_PHYS(pg)));
   2360        1.71   thorpej 
   2361       1.134   thorpej 	PMAP_HEAD_TO_MAP_LOCK();
   2362       1.134   thorpej 	simple_lock(&pg->mdpage.pvh_slock);
   2363         1.1      matt 
   2364       1.134   thorpej 	pv = pg->mdpage.pvh_list;
   2365       1.134   thorpej 	if (pv == NULL) {
   2366  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   2367  1.164.12.2      matt 		/*
   2368  1.164.12.2      matt 		 * We *know* the page contents are about to be replaced.
   2369  1.164.12.2      matt 		 * Discard the exec contents
   2370  1.164.12.2      matt 		 */
   2371  1.164.12.2      matt 		if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs))
   2372  1.164.12.2      matt 			PMAPCOUNT(exec_discarded_page_protect);
   2373  1.164.12.2      matt 		pg->mdpage.pvh_attrs &= ~PVF_EXEC;
   2374  1.164.12.2      matt #endif
   2375       1.134   thorpej 		simple_unlock(&pg->mdpage.pvh_slock);
   2376       1.134   thorpej 		PMAP_HEAD_TO_MAP_UNLOCK();
   2377       1.134   thorpej 		return;
   2378       1.134   thorpej 	}
   2379  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   2380  1.164.12.2      matt 	KASSERT(pmap_is_page_colored_p(pg));
   2381  1.164.12.2      matt #endif
   2382        1.79   thorpej 
   2383         1.1      matt 	/*
   2384       1.134   thorpej 	 * Clear alias counts
   2385         1.1      matt 	 */
   2386       1.134   thorpej 	pg->mdpage.k_mappings = 0;
   2387       1.134   thorpej 	pg->mdpage.urw_mappings = pg->mdpage.uro_mappings = 0;
   2388       1.134   thorpej 
   2389       1.160   thorpej 	flush = false;
   2390       1.134   thorpej 	flags = 0;
   2391       1.134   thorpej 	if (curproc)
   2392       1.134   thorpej 		curpm = curproc->p_vmspace->vm_map.pmap;
   2393       1.134   thorpej 	else
   2394       1.134   thorpej 		curpm = pmap_kernel();
   2395       1.134   thorpej 
   2396  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   2397       1.160   thorpej 	pmap_clean_page(pv, false);
   2398  1.164.12.2      matt #endif
   2399       1.134   thorpej 
   2400       1.134   thorpej 	while (pv) {
   2401       1.134   thorpej 		pm = pv->pv_pmap;
   2402       1.160   thorpej 		if (flush == false && (pm == curpm || pm == pmap_kernel()))
   2403       1.160   thorpej 			flush = true;
   2404       1.134   thorpej 
   2405  1.164.12.2      matt 		if (pm == pmap_kernel())
   2406  1.164.12.2      matt 			PMAPCOUNT(kernel_unmappings);
   2407  1.164.12.2      matt 		PMAPCOUNT(unmappings);
   2408  1.164.12.2      matt 
   2409       1.134   thorpej 		pmap_acquire_pmap_lock(pm);
   2410       1.134   thorpej 
   2411       1.134   thorpej 		l2b = pmap_get_l2_bucket(pm, pv->pv_va);
   2412       1.134   thorpej 		KDASSERT(l2b != NULL);
   2413       1.134   thorpej 
   2414       1.134   thorpej 		ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
   2415       1.134   thorpej 		pte = *ptep;
   2416       1.134   thorpej 
   2417       1.134   thorpej 		/*
   2418       1.134   thorpej 		 * Update statistics
   2419       1.134   thorpej 		 */
   2420       1.134   thorpej 		--pm->pm_stats.resident_count;
   2421       1.134   thorpej 
   2422       1.134   thorpej 		/* Wired bit */
   2423       1.134   thorpej 		if (pv->pv_flags & PVF_WIRED)
   2424       1.134   thorpej 			--pm->pm_stats.wired_count;
   2425        1.88   thorpej 
   2426       1.134   thorpej 		flags |= pv->pv_flags;
   2427        1.88   thorpej 
   2428       1.134   thorpej 		/*
   2429       1.134   thorpej 		 * Invalidate the PTEs.
   2430       1.134   thorpej 		 */
   2431       1.134   thorpej 		*ptep = 0;
   2432       1.134   thorpej 		PTE_SYNC_CURRENT(pm, ptep);
   2433       1.134   thorpej 		pmap_free_l2_bucket(pm, l2b, 1);
   2434        1.88   thorpej 
   2435       1.134   thorpej 		npv = pv->pv_next;
   2436       1.134   thorpej 		pool_put(&pmap_pv_pool, pv);
   2437       1.134   thorpej 		pv = npv;
   2438  1.164.12.2      matt 		if (pv == NULL) {
   2439  1.164.12.2      matt 			pg->mdpage.pvh_list = NULL;
   2440  1.164.12.2      matt 			if (pg->mdpage.pvh_attrs & PVF_KENTRY)
   2441  1.164.12.2      matt 				pmap_vac_me_harder(pg, pm, 0);
   2442  1.164.12.2      matt 		}
   2443       1.134   thorpej 		pmap_release_pmap_lock(pm);
   2444       1.134   thorpej 	}
   2445  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   2446  1.164.12.2      matt 	/*
   2447  1.164.12.2      matt 	 * Since there are now no mappings, there isn't reason to mark it
   2448  1.164.12.2      matt 	 * as uncached.  Its EXEC cache is also gone.
   2449  1.164.12.2      matt 	 */
   2450  1.164.12.2      matt 	if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs))
   2451  1.164.12.2      matt 		PMAPCOUNT(exec_discarded_page_protect);
   2452  1.164.12.2      matt 	pg->mdpage.pvh_attrs &= ~(PVF_NC|PVF_EXEC);
   2453  1.164.12.2      matt #endif
   2454  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   2455       1.134   thorpej 	pg->mdpage.pvh_list = NULL;
   2456  1.164.12.2      matt #endif
   2457       1.134   thorpej 	simple_unlock(&pg->mdpage.pvh_slock);
   2458       1.134   thorpej 	PMAP_HEAD_TO_MAP_UNLOCK();
   2459        1.88   thorpej 
   2460       1.134   thorpej 	if (flush) {
   2461       1.152       scw 		/*
   2462       1.152       scw 		 * Note: We can't use pmap_tlb_flush{I,}D() here since that
   2463       1.152       scw 		 * would need a subsequent call to pmap_update() to ensure
   2464       1.152       scw 		 * curpm->pm_cstate.cs_all is reset. Our callers are not
   2465       1.152       scw 		 * required to do that (see pmap(9)), so we can't modify
   2466       1.152       scw 		 * the current pmap's state.
   2467       1.152       scw 		 */
   2468       1.134   thorpej 		if (PV_BEEN_EXECD(flags))
   2469       1.152       scw 			cpu_tlb_flushID();
   2470       1.134   thorpej 		else
   2471       1.152       scw 			cpu_tlb_flushD();
   2472       1.134   thorpej 	}
   2473        1.88   thorpej 	cpu_cpwait();
   2474        1.88   thorpej }
   2475         1.1      matt 
   2476       1.134   thorpej /*
   2477       1.134   thorpej  * pmap_t pmap_create(void)
   2478       1.134   thorpej  *
   2479       1.134   thorpej  *      Create a new pmap structure from scratch.
   2480        1.17     chris  */
   2481       1.134   thorpej pmap_t
   2482       1.134   thorpej pmap_create(void)
   2483        1.17     chris {
   2484       1.134   thorpej 	pmap_t pm;
   2485       1.134   thorpej 
   2486       1.134   thorpej 	pm = pool_cache_get(&pmap_pmap_cache, PR_WAITOK);
   2487        1.79   thorpej 
   2488       1.134   thorpej 	simple_lock_init(&pm->pm_lock);
   2489       1.134   thorpej 	pm->pm_obj.pgops = NULL;	/* currently not a mappable object */
   2490       1.134   thorpej 	TAILQ_INIT(&pm->pm_obj.memq);
   2491       1.134   thorpej 	pm->pm_obj.uo_npages = 0;
   2492       1.134   thorpej 	pm->pm_obj.uo_refs = 1;
   2493       1.134   thorpej 	pm->pm_stats.wired_count = 0;
   2494       1.134   thorpej 	pm->pm_stats.resident_count = 1;
   2495       1.134   thorpej 	pm->pm_cstate.cs_all = 0;
   2496       1.134   thorpej 	pmap_alloc_l1(pm);
   2497        1.79   thorpej 
   2498        1.17     chris 	/*
   2499       1.134   thorpej 	 * Note: The pool cache ensures that the pm_l2[] array is already
   2500       1.134   thorpej 	 * initialised to zero.
   2501        1.17     chris 	 */
   2502        1.32   thorpej 
   2503       1.134   thorpej 	pmap_pinit(pm);
   2504       1.134   thorpej 
   2505       1.134   thorpej 	LIST_INSERT_HEAD(&pmap_pmaps, pm, pm_list);
   2506        1.17     chris 
   2507       1.134   thorpej 	return (pm);
   2508        1.17     chris }
   2509       1.134   thorpej 
   2510         1.1      matt /*
   2511       1.134   thorpej  * void pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot,
   2512       1.134   thorpej  *     int flags)
   2513       1.134   thorpej  *
   2514       1.134   thorpej  *      Insert the given physical page (p) at
   2515       1.134   thorpej  *      the specified virtual address (v) in the
   2516       1.134   thorpej  *      target physical map with the protection requested.
   2517         1.1      matt  *
   2518       1.134   thorpej  *      NB:  This is the only routine which MAY NOT lazy-evaluate
   2519       1.134   thorpej  *      or lose information.  That is, this routine must actually
   2520       1.134   thorpej  *      insert this page into the given map NOW.
   2521         1.1      matt  */
   2522       1.134   thorpej int
   2523       1.134   thorpej pmap_enter(pmap_t pm, vaddr_t va, paddr_t pa, vm_prot_t prot, int flags)
   2524         1.1      matt {
   2525       1.134   thorpej 	struct l2_bucket *l2b;
   2526       1.134   thorpej 	struct vm_page *pg, *opg;
   2527       1.134   thorpej 	struct pv_entry *pve;
   2528       1.134   thorpej 	pt_entry_t *ptep, npte, opte;
   2529       1.134   thorpej 	u_int nflags;
   2530       1.134   thorpej 	u_int oflags;
   2531        1.71   thorpej 
   2532       1.134   thorpej 	NPDEBUG(PDB_ENTER, printf("pmap_enter: pm %p va 0x%lx pa 0x%lx prot %x flag %x\n", pm, va, pa, prot, flags));
   2533        1.71   thorpej 
   2534       1.134   thorpej 	KDASSERT((flags & PMAP_WIRED) == 0 || (flags & VM_PROT_ALL) != 0);
   2535       1.134   thorpej 	KDASSERT(((va | pa) & PGOFSET) == 0);
   2536        1.79   thorpej 
   2537        1.71   thorpej 	/*
   2538       1.134   thorpej 	 * Get a pointer to the page.  Later on in this function, we
   2539       1.134   thorpej 	 * test for a managed page by checking pg != NULL.
   2540        1.71   thorpej 	 */
   2541       1.134   thorpej 	pg = pmap_initialized ? PHYS_TO_VM_PAGE(pa) : NULL;
   2542       1.134   thorpej 
   2543       1.134   thorpej 	nflags = 0;
   2544       1.134   thorpej 	if (prot & VM_PROT_WRITE)
   2545       1.134   thorpej 		nflags |= PVF_WRITE;
   2546       1.134   thorpej 	if (prot & VM_PROT_EXECUTE)
   2547       1.134   thorpej 		nflags |= PVF_EXEC;
   2548       1.134   thorpej 	if (flags & PMAP_WIRED)
   2549       1.134   thorpej 		nflags |= PVF_WIRED;
   2550       1.134   thorpej 
   2551       1.134   thorpej 	PMAP_MAP_TO_HEAD_LOCK();
   2552       1.134   thorpej 	pmap_acquire_pmap_lock(pm);
   2553         1.1      matt 
   2554         1.1      matt 	/*
   2555       1.134   thorpej 	 * Fetch the L2 bucket which maps this page, allocating one if
   2556       1.134   thorpej 	 * necessary for user pmaps.
   2557         1.1      matt 	 */
   2558       1.134   thorpej 	if (pm == pmap_kernel())
   2559       1.134   thorpej 		l2b = pmap_get_l2_bucket(pm, va);
   2560       1.134   thorpej 	else
   2561       1.134   thorpej 		l2b = pmap_alloc_l2_bucket(pm, va);
   2562       1.134   thorpej 	if (l2b == NULL) {
   2563       1.134   thorpej 		if (flags & PMAP_CANFAIL) {
   2564       1.134   thorpej 			pmap_release_pmap_lock(pm);
   2565       1.134   thorpej 			PMAP_MAP_TO_HEAD_UNLOCK();
   2566       1.134   thorpej 			return (ENOMEM);
   2567       1.134   thorpej 		}
   2568       1.134   thorpej 		panic("pmap_enter: failed to allocate L2 bucket");
   2569       1.134   thorpej 	}
   2570       1.134   thorpej 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   2571       1.134   thorpej 	opte = *ptep;
   2572       1.134   thorpej 	npte = pa;
   2573       1.134   thorpej 	oflags = 0;
   2574        1.88   thorpej 
   2575       1.134   thorpej 	if (opte) {
   2576       1.134   thorpej 		/*
   2577       1.134   thorpej 		 * There is already a mapping at this address.
   2578       1.134   thorpej 		 * If the physical address is different, lookup the
   2579       1.134   thorpej 		 * vm_page.
   2580       1.134   thorpej 		 */
   2581       1.134   thorpej 		if (l2pte_pa(opte) != pa)
   2582       1.134   thorpej 			opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
   2583       1.134   thorpej 		else
   2584       1.134   thorpej 			opg = pg;
   2585       1.134   thorpej 	} else
   2586       1.134   thorpej 		opg = NULL;
   2587        1.88   thorpej 
   2588       1.134   thorpej 	if (pg) {
   2589       1.134   thorpej 		/*
   2590       1.134   thorpej 		 * This is to be a managed mapping.
   2591       1.134   thorpej 		 */
   2592       1.134   thorpej 		if ((flags & VM_PROT_ALL) ||
   2593       1.134   thorpej 		    (pg->mdpage.pvh_attrs & PVF_REF)) {
   2594       1.134   thorpej 			/*
   2595       1.134   thorpej 			 * - The access type indicates that we don't need
   2596       1.134   thorpej 			 *   to do referenced emulation.
   2597       1.134   thorpej 			 * OR
   2598       1.134   thorpej 			 * - The physical page has already been referenced
   2599       1.134   thorpej 			 *   so no need to re-do referenced emulation here.
   2600       1.134   thorpej 			 */
   2601       1.134   thorpej 			npte |= L2_S_PROTO;
   2602        1.88   thorpej 
   2603       1.134   thorpej 			nflags |= PVF_REF;
   2604        1.88   thorpej 
   2605       1.134   thorpej 			if ((prot & VM_PROT_WRITE) != 0 &&
   2606       1.134   thorpej 			    ((flags & VM_PROT_WRITE) != 0 ||
   2607       1.134   thorpej 			     (pg->mdpage.pvh_attrs & PVF_MOD) != 0)) {
   2608       1.134   thorpej 				/*
   2609       1.134   thorpej 				 * This is a writable mapping, and the
   2610       1.134   thorpej 				 * page's mod state indicates it has
   2611       1.134   thorpej 				 * already been modified. Make it
   2612       1.134   thorpej 				 * writable from the outset.
   2613       1.134   thorpej 				 */
   2614       1.134   thorpej 				npte |= L2_S_PROT_W;
   2615       1.134   thorpej 				nflags |= PVF_MOD;
   2616       1.134   thorpej 			}
   2617       1.134   thorpej 		} else {
   2618       1.134   thorpej 			/*
   2619       1.134   thorpej 			 * Need to do page referenced emulation.
   2620       1.134   thorpej 			 */
   2621       1.134   thorpej 			npte |= L2_TYPE_INV;
   2622       1.134   thorpej 		}
   2623        1.88   thorpej 
   2624       1.134   thorpej 		npte |= pte_l2_s_cache_mode;
   2625         1.1      matt 
   2626       1.134   thorpej 		if (pg == opg) {
   2627       1.134   thorpej 			/*
   2628       1.134   thorpej 			 * We're changing the attrs of an existing mapping.
   2629       1.134   thorpej 			 */
   2630       1.134   thorpej 			simple_lock(&pg->mdpage.pvh_slock);
   2631       1.134   thorpej 			oflags = pmap_modify_pv(pg, pm, va,
   2632       1.134   thorpej 			    PVF_WRITE | PVF_EXEC | PVF_WIRED |
   2633       1.134   thorpej 			    PVF_MOD | PVF_REF, nflags);
   2634       1.134   thorpej 			simple_unlock(&pg->mdpage.pvh_slock);
   2635         1.1      matt 
   2636  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   2637       1.134   thorpej 			/*
   2638       1.134   thorpej 			 * We may need to flush the cache if we're
   2639       1.134   thorpej 			 * doing rw-ro...
   2640       1.134   thorpej 			 */
   2641       1.134   thorpej 			if (pm->pm_cstate.cs_cache_d &&
   2642       1.134   thorpej 			    (oflags & PVF_NC) == 0 &&
   2643       1.134   thorpej 			    (opte & L2_S_PROT_W) != 0 &&
   2644       1.134   thorpej 			    (prot & VM_PROT_WRITE) == 0)
   2645       1.134   thorpej 				cpu_dcache_wb_range(va, PAGE_SIZE);
   2646  1.164.12.2      matt #endif
   2647       1.134   thorpej 		} else {
   2648       1.134   thorpej 			/*
   2649       1.134   thorpej 			 * New mapping, or changing the backing page
   2650       1.134   thorpej 			 * of an existing mapping.
   2651       1.134   thorpej 			 */
   2652       1.134   thorpej 			if (opg) {
   2653       1.134   thorpej 				/*
   2654       1.134   thorpej 				 * Replacing an existing mapping with a new one.
   2655       1.134   thorpej 				 * It is part of our managed memory so we
   2656       1.134   thorpej 				 * must remove it from the PV list
   2657       1.134   thorpej 				 */
   2658       1.134   thorpej 				simple_lock(&opg->mdpage.pvh_slock);
   2659       1.156       scw 				pve = pmap_remove_pv(opg, pm, va, 0);
   2660       1.134   thorpej 				pmap_vac_me_harder(opg, pm, 0);
   2661       1.134   thorpej 				simple_unlock(&opg->mdpage.pvh_slock);
   2662       1.134   thorpej 				oflags = pve->pv_flags;
   2663         1.1      matt 
   2664  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   2665       1.134   thorpej 				/*
   2666       1.134   thorpej 				 * If the old mapping was valid (ref/mod
   2667       1.134   thorpej 				 * emulation creates 'invalid' mappings
   2668       1.134   thorpej 				 * initially) then make sure to frob
   2669       1.134   thorpej 				 * the cache.
   2670       1.134   thorpej 				 */
   2671       1.134   thorpej 				if ((oflags & PVF_NC) == 0 &&
   2672       1.134   thorpej 				    l2pte_valid(opte)) {
   2673       1.134   thorpej 					if (PV_BEEN_EXECD(oflags)) {
   2674       1.134   thorpej 						pmap_idcache_wbinv_range(pm, va,
   2675       1.134   thorpej 						    PAGE_SIZE);
   2676       1.134   thorpej 					} else
   2677       1.134   thorpej 					if (PV_BEEN_REFD(oflags)) {
   2678       1.134   thorpej 						pmap_dcache_wb_range(pm, va,
   2679       1.160   thorpej 						    PAGE_SIZE, true,
   2680       1.134   thorpej 						    (oflags & PVF_WRITE) == 0);
   2681       1.134   thorpej 					}
   2682       1.134   thorpej 				}
   2683  1.164.12.2      matt #endif
   2684       1.134   thorpej 			} else
   2685       1.134   thorpej 			if ((pve = pool_get(&pmap_pv_pool, PR_NOWAIT)) == NULL){
   2686       1.134   thorpej 				if ((flags & PMAP_CANFAIL) == 0)
   2687       1.134   thorpej 					panic("pmap_enter: no pv entries");
   2688       1.134   thorpej 
   2689       1.134   thorpej 				if (pm != pmap_kernel())
   2690       1.134   thorpej 					pmap_free_l2_bucket(pm, l2b, 0);
   2691       1.134   thorpej 				pmap_release_pmap_lock(pm);
   2692       1.134   thorpej 				PMAP_MAP_TO_HEAD_UNLOCK();
   2693       1.134   thorpej 				NPDEBUG(PDB_ENTER,
   2694       1.134   thorpej 				    printf("pmap_enter: ENOMEM\n"));
   2695       1.134   thorpej 				return (ENOMEM);
   2696       1.134   thorpej 			}
   2697        1.25  rearnsha 
   2698       1.134   thorpej 			pmap_enter_pv(pg, pve, pm, va, nflags);
   2699        1.25  rearnsha 		}
   2700       1.134   thorpej 	} else {
   2701       1.134   thorpej 		/*
   2702       1.134   thorpej 		 * We're mapping an unmanaged page.
   2703       1.134   thorpej 		 * These are always readable, and possibly writable, from
   2704       1.134   thorpej 		 * the get go as we don't need to track ref/mod status.
   2705       1.134   thorpej 		 */
   2706       1.134   thorpej 		npte |= L2_S_PROTO;
   2707       1.134   thorpej 		if (prot & VM_PROT_WRITE)
   2708       1.134   thorpej 			npte |= L2_S_PROT_W;
   2709        1.25  rearnsha 
   2710       1.134   thorpej 		/*
   2711       1.134   thorpej 		 * Make sure the vector table is mapped cacheable
   2712       1.134   thorpej 		 */
   2713       1.134   thorpej 		if (pm != pmap_kernel() && va == vector_page)
   2714       1.134   thorpej 			npte |= pte_l2_s_cache_mode;
   2715        1.25  rearnsha 
   2716       1.134   thorpej 		if (opg) {
   2717       1.134   thorpej 			/*
   2718       1.134   thorpej 			 * Looks like there's an existing 'managed' mapping
   2719       1.134   thorpej 			 * at this address.
   2720        1.25  rearnsha 			 */
   2721       1.134   thorpej 			simple_lock(&opg->mdpage.pvh_slock);
   2722       1.156       scw 			pve = pmap_remove_pv(opg, pm, va, 0);
   2723       1.134   thorpej 			pmap_vac_me_harder(opg, pm, 0);
   2724       1.134   thorpej 			simple_unlock(&opg->mdpage.pvh_slock);
   2725       1.134   thorpej 			oflags = pve->pv_flags;
   2726       1.134   thorpej 
   2727  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   2728       1.134   thorpej 			if ((oflags & PVF_NC) == 0 && l2pte_valid(opte)) {
   2729       1.134   thorpej 				if (PV_BEEN_EXECD(oflags))
   2730       1.134   thorpej 					pmap_idcache_wbinv_range(pm, va,
   2731       1.134   thorpej 					    PAGE_SIZE);
   2732       1.134   thorpej 				else
   2733       1.134   thorpej 				if (PV_BEEN_REFD(oflags))
   2734       1.134   thorpej 					pmap_dcache_wb_range(pm, va, PAGE_SIZE,
   2735       1.160   thorpej 					    true, (oflags & PVF_WRITE) == 0);
   2736       1.134   thorpej 			}
   2737  1.164.12.2      matt #endif
   2738       1.134   thorpej 			pool_put(&pmap_pv_pool, pve);
   2739        1.25  rearnsha 		}
   2740        1.25  rearnsha 	}
   2741        1.25  rearnsha 
   2742       1.134   thorpej 	/*
   2743       1.134   thorpej 	 * Make sure userland mappings get the right permissions
   2744       1.134   thorpej 	 */
   2745       1.134   thorpej 	if (pm != pmap_kernel() && va != vector_page)
   2746       1.134   thorpej 		npte |= L2_S_PROT_U;
   2747        1.25  rearnsha 
   2748       1.134   thorpej 	/*
   2749       1.134   thorpej 	 * Keep the stats up to date
   2750       1.134   thorpej 	 */
   2751       1.134   thorpej 	if (opte == 0) {
   2752       1.134   thorpej 		l2b->l2b_occupancy++;
   2753       1.134   thorpej 		pm->pm_stats.resident_count++;
   2754       1.134   thorpej 	}
   2755         1.1      matt 
   2756       1.134   thorpej 	NPDEBUG(PDB_ENTER,
   2757       1.134   thorpej 	    printf("pmap_enter: opte 0x%08x npte 0x%08x\n", opte, npte));
   2758         1.1      matt 
   2759         1.1      matt 	/*
   2760       1.134   thorpej 	 * If this is just a wiring change, the two PTEs will be
   2761       1.134   thorpej 	 * identical, so there's no need to update the page table.
   2762         1.1      matt 	 */
   2763       1.134   thorpej 	if (npte != opte) {
   2764       1.159   thorpej 		bool is_cached = pmap_is_cached(pm);
   2765         1.1      matt 
   2766       1.134   thorpej 		*ptep = npte;
   2767       1.134   thorpej 		if (is_cached) {
   2768       1.134   thorpej 			/*
   2769       1.134   thorpej 			 * We only need to frob the cache/tlb if this pmap
   2770       1.134   thorpej 			 * is current
   2771       1.134   thorpej 			 */
   2772       1.134   thorpej 			PTE_SYNC(ptep);
   2773       1.134   thorpej 			if (va != vector_page && l2pte_valid(npte)) {
   2774        1.25  rearnsha 				/*
   2775       1.134   thorpej 				 * This mapping is likely to be accessed as
   2776       1.134   thorpej 				 * soon as we return to userland. Fix up the
   2777       1.134   thorpej 				 * L1 entry to avoid taking another
   2778       1.134   thorpej 				 * page/domain fault.
   2779        1.25  rearnsha 				 */
   2780       1.134   thorpej 				pd_entry_t *pl1pd, l1pd;
   2781       1.134   thorpej 
   2782       1.134   thorpej 				pl1pd = &pm->pm_l1->l1_kva[L1_IDX(va)];
   2783       1.134   thorpej 				l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) |
   2784       1.134   thorpej 				    L1_C_PROTO;
   2785       1.134   thorpej 				if (*pl1pd != l1pd) {
   2786       1.134   thorpej 					*pl1pd = l1pd;
   2787       1.134   thorpej 					PTE_SYNC(pl1pd);
   2788        1.12     chris 				}
   2789         1.1      matt 			}
   2790         1.1      matt 		}
   2791       1.134   thorpej 
   2792       1.134   thorpej 		if (PV_BEEN_EXECD(oflags))
   2793       1.134   thorpej 			pmap_tlb_flushID_SE(pm, va);
   2794       1.134   thorpej 		else
   2795       1.134   thorpej 		if (PV_BEEN_REFD(oflags))
   2796       1.134   thorpej 			pmap_tlb_flushD_SE(pm, va);
   2797       1.134   thorpej 
   2798       1.134   thorpej 		NPDEBUG(PDB_ENTER,
   2799       1.134   thorpej 		    printf("pmap_enter: is_cached %d cs 0x%08x\n",
   2800       1.134   thorpej 		    is_cached, pm->pm_cstate.cs_all));
   2801       1.134   thorpej 
   2802       1.134   thorpej 		if (pg != NULL) {
   2803       1.134   thorpej 			simple_lock(&pg->mdpage.pvh_slock);
   2804       1.134   thorpej 			pmap_vac_me_harder(pg, pm, va);
   2805       1.134   thorpej 			simple_unlock(&pg->mdpage.pvh_slock);
   2806         1.1      matt 		}
   2807         1.1      matt 	}
   2808       1.134   thorpej 
   2809       1.134   thorpej 	pmap_release_pmap_lock(pm);
   2810       1.134   thorpej 	PMAP_MAP_TO_HEAD_UNLOCK();
   2811       1.134   thorpej 
   2812       1.134   thorpej 	return (0);
   2813         1.1      matt }
   2814         1.1      matt 
   2815         1.1      matt /*
   2816         1.1      matt  * pmap_remove()
   2817         1.1      matt  *
   2818         1.1      matt  * pmap_remove is responsible for nuking a number of mappings for a range
   2819         1.1      matt  * of virtual address space in the current pmap. To do this efficiently
   2820         1.1      matt  * is interesting, because in a number of cases a wide virtual address
   2821         1.1      matt  * range may be supplied that contains few actual mappings. So, the
   2822         1.1      matt  * optimisations are:
   2823       1.134   thorpej  *  1. Skip over hunks of address space for which no L1 or L2 entry exists.
   2824         1.1      matt  *  2. Build up a list of pages we've hit, up to a maximum, so we can
   2825         1.1      matt  *     maybe do just a partial cache clean. This path of execution is
   2826         1.1      matt  *     complicated by the fact that the cache must be flushed _before_
   2827         1.1      matt  *     the PTE is nuked, being a VAC :-)
   2828       1.134   thorpej  *  3. If we're called after UVM calls pmap_remove_all(), we can defer
   2829       1.134   thorpej  *     all invalidations until pmap_update(), since pmap_remove_all() has
   2830       1.134   thorpej  *     already flushed the cache.
   2831       1.134   thorpej  *  4. Maybe later fast-case a single page, but I don't think this is
   2832         1.1      matt  *     going to make _that_ much difference overall.
   2833         1.1      matt  */
   2834         1.1      matt 
   2835       1.134   thorpej #define	PMAP_REMOVE_CLEAN_LIST_SIZE	3
   2836         1.1      matt 
   2837         1.1      matt void
   2838       1.156       scw pmap_do_remove(pmap_t pm, vaddr_t sva, vaddr_t eva, int skip_wired)
   2839         1.1      matt {
   2840       1.134   thorpej 	struct l2_bucket *l2b;
   2841       1.134   thorpej 	vaddr_t next_bucket;
   2842       1.134   thorpej 	pt_entry_t *ptep;
   2843       1.134   thorpej 	u_int cleanlist_idx, total, cnt;
   2844       1.134   thorpej 	struct {
   2845         1.1      matt 		vaddr_t va;
   2846  1.164.12.2      matt 		pt_entry_t *ptep;
   2847         1.1      matt 	} cleanlist[PMAP_REMOVE_CLEAN_LIST_SIZE];
   2848       1.134   thorpej 	u_int mappings, is_exec, is_refd;
   2849         1.1      matt 
   2850       1.156       scw 	NPDEBUG(PDB_REMOVE, printf("pmap_do_remove: pmap=%p sva=%08lx "
   2851       1.156       scw 	    "eva=%08lx\n", pm, sva, eva));
   2852         1.1      matt 
   2853        1.17     chris 	/*
   2854       1.134   thorpej 	 * we lock in the pmap => pv_head direction
   2855        1.17     chris 	 */
   2856        1.17     chris 	PMAP_MAP_TO_HEAD_LOCK();
   2857       1.134   thorpej 	pmap_acquire_pmap_lock(pm);
   2858       1.134   thorpej 
   2859       1.134   thorpej 	if (pm->pm_remove_all || !pmap_is_cached(pm)) {
   2860       1.134   thorpej 		cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
   2861       1.134   thorpej 		if (pm->pm_cstate.cs_tlb == 0)
   2862       1.160   thorpej 			pm->pm_remove_all = true;
   2863       1.134   thorpej 	} else
   2864       1.134   thorpej 		cleanlist_idx = 0;
   2865       1.134   thorpej 
   2866       1.134   thorpej 	total = 0;
   2867       1.134   thorpej 
   2868         1.1      matt 	while (sva < eva) {
   2869       1.134   thorpej 		/*
   2870       1.134   thorpej 		 * Do one L2 bucket's worth at a time.
   2871       1.134   thorpej 		 */
   2872       1.134   thorpej 		next_bucket = L2_NEXT_BUCKET(sva);
   2873       1.134   thorpej 		if (next_bucket > eva)
   2874       1.134   thorpej 			next_bucket = eva;
   2875       1.134   thorpej 
   2876       1.134   thorpej 		l2b = pmap_get_l2_bucket(pm, sva);
   2877       1.134   thorpej 		if (l2b == NULL) {
   2878       1.134   thorpej 			sva = next_bucket;
   2879       1.134   thorpej 			continue;
   2880       1.134   thorpej 		}
   2881       1.134   thorpej 
   2882       1.134   thorpej 		ptep = &l2b->l2b_kva[l2pte_index(sva)];
   2883       1.134   thorpej 
   2884       1.156       scw 		for (mappings = 0; sva < next_bucket; sva += PAGE_SIZE, ptep++){
   2885       1.134   thorpej 			struct vm_page *pg;
   2886       1.134   thorpej 			pt_entry_t pte;
   2887       1.134   thorpej 			paddr_t pa;
   2888       1.134   thorpej 
   2889       1.134   thorpej 			pte = *ptep;
   2890         1.1      matt 
   2891       1.134   thorpej 			if (pte == 0) {
   2892       1.156       scw 				/* Nothing here, move along */
   2893         1.1      matt 				continue;
   2894         1.1      matt 			}
   2895         1.1      matt 
   2896       1.134   thorpej 			pa = l2pte_pa(pte);
   2897       1.134   thorpej 			is_exec = 0;
   2898       1.134   thorpej 			is_refd = 1;
   2899         1.1      matt 
   2900         1.1      matt 			/*
   2901       1.134   thorpej 			 * Update flags. In a number of circumstances,
   2902       1.134   thorpej 			 * we could cluster a lot of these and do a
   2903       1.134   thorpej 			 * number of sequential pages in one go.
   2904         1.1      matt 			 */
   2905       1.134   thorpej 			if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
   2906       1.134   thorpej 				struct pv_entry *pve;
   2907       1.134   thorpej 				simple_lock(&pg->mdpage.pvh_slock);
   2908       1.156       scw 				pve = pmap_remove_pv(pg, pm, sva, skip_wired);
   2909       1.134   thorpej 				pmap_vac_me_harder(pg, pm, 0);
   2910       1.134   thorpej 				simple_unlock(&pg->mdpage.pvh_slock);
   2911       1.134   thorpej 				if (pve != NULL) {
   2912       1.160   thorpej 					if (pm->pm_remove_all == false) {
   2913       1.134   thorpej 						is_exec =
   2914       1.134   thorpej 						   PV_BEEN_EXECD(pve->pv_flags);
   2915       1.134   thorpej 						is_refd =
   2916       1.134   thorpej 						   PV_BEEN_REFD(pve->pv_flags);
   2917       1.134   thorpej 					}
   2918       1.134   thorpej 					pool_put(&pmap_pv_pool, pve);
   2919       1.156       scw 				} else
   2920       1.156       scw 				if (skip_wired) {
   2921       1.156       scw 					/* The mapping is wired. Skip it */
   2922       1.156       scw 					continue;
   2923       1.134   thorpej 				}
   2924       1.156       scw 			} else
   2925       1.156       scw 			if (skip_wired) {
   2926       1.156       scw 				/* Unmanaged pages are always wired. */
   2927       1.156       scw 				continue;
   2928       1.134   thorpej 			}
   2929       1.134   thorpej 
   2930       1.156       scw 			mappings++;
   2931       1.156       scw 
   2932       1.134   thorpej 			if (!l2pte_valid(pte)) {
   2933       1.156       scw 				/*
   2934       1.156       scw 				 * Ref/Mod emulation is still active for this
   2935       1.156       scw 				 * mapping, therefore it is has not yet been
   2936       1.156       scw 				 * accessed. No need to frob the cache/tlb.
   2937       1.156       scw 				 */
   2938       1.134   thorpej 				*ptep = 0;
   2939       1.134   thorpej 				PTE_SYNC_CURRENT(pm, ptep);
   2940       1.134   thorpej 				continue;
   2941       1.134   thorpej 			}
   2942         1.1      matt 
   2943         1.1      matt 			if (cleanlist_idx < PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2944         1.1      matt 				/* Add to the clean list. */
   2945  1.164.12.2      matt 				cleanlist[cleanlist_idx].ptep = ptep;
   2946       1.134   thorpej 				cleanlist[cleanlist_idx].va =
   2947       1.134   thorpej 				    sva | (is_exec & 1);
   2948         1.1      matt 				cleanlist_idx++;
   2949       1.134   thorpej 			} else
   2950       1.134   thorpej 			if (cleanlist_idx == PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2951         1.1      matt 				/* Nuke everything if needed. */
   2952  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   2953       1.134   thorpej 				pmap_idcache_wbinv_all(pm);
   2954  1.164.12.2      matt #endif
   2955       1.134   thorpej 				pmap_tlb_flushID(pm);
   2956         1.1      matt 
   2957         1.1      matt 				/*
   2958         1.1      matt 				 * Roll back the previous PTE list,
   2959         1.1      matt 				 * and zero out the current PTE.
   2960         1.1      matt 				 */
   2961       1.113   thorpej 				for (cnt = 0;
   2962       1.134   thorpej 				     cnt < PMAP_REMOVE_CLEAN_LIST_SIZE; cnt++) {
   2963  1.164.12.2      matt 					*cleanlist[cnt].ptep = 0;
   2964         1.1      matt 				}
   2965       1.134   thorpej 				*ptep = 0;
   2966       1.134   thorpej 				PTE_SYNC(ptep);
   2967         1.1      matt 				cleanlist_idx++;
   2968       1.160   thorpej 				pm->pm_remove_all = true;
   2969         1.1      matt 			} else {
   2970       1.134   thorpej 				*ptep = 0;
   2971       1.134   thorpej 				PTE_SYNC(ptep);
   2972       1.160   thorpej 				if (pm->pm_remove_all == false) {
   2973       1.134   thorpej 					if (is_exec)
   2974       1.134   thorpej 						pmap_tlb_flushID_SE(pm, sva);
   2975       1.134   thorpej 					else
   2976       1.134   thorpej 					if (is_refd)
   2977       1.134   thorpej 						pmap_tlb_flushD_SE(pm, sva);
   2978       1.134   thorpej 				}
   2979       1.134   thorpej 			}
   2980       1.134   thorpej 		}
   2981       1.134   thorpej 
   2982       1.134   thorpej 		/*
   2983       1.134   thorpej 		 * Deal with any left overs
   2984       1.134   thorpej 		 */
   2985       1.134   thorpej 		if (cleanlist_idx <= PMAP_REMOVE_CLEAN_LIST_SIZE) {
   2986       1.134   thorpej 			total += cleanlist_idx;
   2987       1.134   thorpej 			for (cnt = 0; cnt < cleanlist_idx; cnt++) {
   2988       1.134   thorpej 				if (pm->pm_cstate.cs_all != 0) {
   2989       1.134   thorpej 					vaddr_t clva = cleanlist[cnt].va & ~1;
   2990       1.134   thorpej 					if (cleanlist[cnt].va & 1) {
   2991  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   2992       1.134   thorpej 						pmap_idcache_wbinv_range(pm,
   2993       1.134   thorpej 						    clva, PAGE_SIZE);
   2994  1.164.12.2      matt #endif
   2995       1.134   thorpej 						pmap_tlb_flushID_SE(pm, clva);
   2996       1.134   thorpej 					} else {
   2997  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   2998       1.134   thorpej 						pmap_dcache_wb_range(pm,
   2999       1.160   thorpej 						    clva, PAGE_SIZE, true,
   3000       1.160   thorpej 						    false);
   3001  1.164.12.2      matt #endif
   3002       1.134   thorpej 						pmap_tlb_flushD_SE(pm, clva);
   3003       1.134   thorpej 					}
   3004       1.134   thorpej 				}
   3005  1.164.12.2      matt 				*cleanlist[cnt].ptep = 0;
   3006  1.164.12.2      matt 				PTE_SYNC_CURRENT(pm, cleanlist[cnt].ptep);
   3007         1.1      matt 			}
   3008         1.1      matt 
   3009         1.1      matt 			/*
   3010       1.134   thorpej 			 * If it looks like we're removing a whole bunch
   3011       1.134   thorpej 			 * of mappings, it's faster to just write-back
   3012       1.134   thorpej 			 * the whole cache now and defer TLB flushes until
   3013       1.134   thorpej 			 * pmap_update() is called.
   3014         1.1      matt 			 */
   3015       1.134   thorpej 			if (total <= PMAP_REMOVE_CLEAN_LIST_SIZE)
   3016       1.134   thorpej 				cleanlist_idx = 0;
   3017       1.134   thorpej 			else {
   3018       1.134   thorpej 				cleanlist_idx = PMAP_REMOVE_CLEAN_LIST_SIZE + 1;
   3019  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   3020       1.134   thorpej 				pmap_idcache_wbinv_all(pm);
   3021  1.164.12.2      matt #endif
   3022       1.160   thorpej 				pm->pm_remove_all = true;
   3023       1.134   thorpej 			}
   3024       1.134   thorpej 		}
   3025       1.134   thorpej 
   3026       1.134   thorpej 		pmap_free_l2_bucket(pm, l2b, mappings);
   3027       1.156       scw 		pm->pm_stats.resident_count -= mappings;
   3028       1.134   thorpej 	}
   3029       1.134   thorpej 
   3030       1.134   thorpej 	pmap_release_pmap_lock(pm);
   3031       1.134   thorpej 	PMAP_MAP_TO_HEAD_UNLOCK();
   3032       1.134   thorpej }
   3033       1.134   thorpej 
   3034       1.134   thorpej /*
   3035       1.134   thorpej  * pmap_kenter_pa: enter an unmanaged, wired kernel mapping
   3036       1.134   thorpej  *
   3037       1.134   thorpej  * We assume there is already sufficient KVM space available
   3038       1.134   thorpej  * to do this, as we can't allocate L2 descriptor tables/metadata
   3039       1.134   thorpej  * from here.
   3040       1.134   thorpej  */
   3041       1.134   thorpej void
   3042       1.134   thorpej pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot)
   3043       1.134   thorpej {
   3044       1.134   thorpej 	struct l2_bucket *l2b;
   3045       1.134   thorpej 	pt_entry_t *ptep, opte;
   3046  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   3047  1.164.12.2      matt 	struct vm_page *pg = PHYS_TO_VM_PAGE(pa);
   3048  1.164.12.2      matt 	struct vm_page *opg;
   3049  1.164.12.2      matt #endif
   3050  1.164.12.2      matt 
   3051       1.134   thorpej 
   3052       1.134   thorpej 	NPDEBUG(PDB_KENTER,
   3053       1.134   thorpej 	    printf("pmap_kenter_pa: va 0x%08lx, pa 0x%08lx, prot 0x%x\n",
   3054       1.134   thorpej 	    va, pa, prot));
   3055       1.134   thorpej 
   3056       1.134   thorpej 	l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   3057       1.134   thorpej 	KDASSERT(l2b != NULL);
   3058       1.134   thorpej 
   3059       1.134   thorpej 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   3060       1.134   thorpej 	opte = *ptep;
   3061       1.134   thorpej 
   3062  1.164.12.2      matt 	if (opte == 0) {
   3063  1.164.12.2      matt 		PMAPCOUNT(kenter_mappings);
   3064       1.134   thorpej 		l2b->l2b_occupancy++;
   3065  1.164.12.2      matt 	} else {
   3066  1.164.12.2      matt 		PMAPCOUNT(kenter_remappings);
   3067  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   3068  1.164.12.2      matt 		opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
   3069  1.164.12.2      matt 		if (opg) {
   3070  1.164.12.2      matt 			KASSERT(opg != pg);
   3071  1.164.12.2      matt 			simple_lock(&opg->mdpage.pvh_slock);
   3072  1.164.12.2      matt 			KASSERT(opg->mdpage.pvh_attrs & PVF_KENTRY);
   3073  1.164.12.2      matt 			if (PV_IS_EXEC_P(opg->mdpage.pvh_attrs)
   3074  1.164.12.2      matt 			    && !(opg->mdpage.pvh_attrs & PVF_NC)) {
   3075  1.164.12.2      matt 				if (opg->mdpage.pvh_list == NULL) {
   3076  1.164.12.2      matt 					opg->mdpage.pvh_attrs &= ~PVF_EXEC;
   3077  1.164.12.2      matt 					PMAPCOUNT(exec_discarded_kremove);
   3078  1.164.12.2      matt 				} else {
   3079  1.164.12.2      matt 					pmap_syncicache_page(opg);
   3080  1.164.12.2      matt 					PMAPCOUNT(exec_synced_kremove);
   3081  1.164.12.2      matt 				}
   3082  1.164.12.2      matt 			}
   3083  1.164.12.2      matt 			KASSERT(opg->mdpage.pvh_attrs | (PVF_COLORED|PVF_NC));
   3084  1.164.12.2      matt 			opg->mdpage.pvh_attrs &= ~PVF_KENTRY;
   3085  1.164.12.2      matt 			pmap_vac_me_harder(opg, NULL, 0);
   3086  1.164.12.2      matt 			simple_unlock(&opg->mdpage.pvh_slock);
   3087  1.164.12.2      matt 		}
   3088  1.164.12.2      matt #endif
   3089  1.164.12.2      matt 		if (l2pte_valid(opte)) {
   3090  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   3091  1.164.12.2      matt 			cpu_dcache_wbinv_range(va, PAGE_SIZE);
   3092  1.164.12.2      matt #endif
   3093  1.164.12.2      matt 			cpu_tlb_flushD_SE(va);
   3094  1.164.12.2      matt 			cpu_cpwait();
   3095  1.164.12.2      matt 		}
   3096  1.164.12.2      matt 	}
   3097       1.134   thorpej 
   3098       1.134   thorpej 	*ptep = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) |
   3099       1.134   thorpej 	    pte_l2_s_cache_mode;
   3100       1.134   thorpej 	PTE_SYNC(ptep);
   3101  1.164.12.2      matt 
   3102  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   3103  1.164.12.2      matt 	if (pg) {
   3104  1.164.12.2      matt 		simple_lock(&pg->mdpage.pvh_slock);
   3105  1.164.12.2      matt 		KASSERT((pg->mdpage.pvh_attrs & PVF_KENTRY) == 0);
   3106  1.164.12.2      matt 		pg->mdpage.pvh_attrs |= PVF_KENTRY;
   3107  1.164.12.2      matt 		pmap_vac_me_harder(pg, NULL, va);
   3108  1.164.12.2      matt 		simple_unlock(&pg->mdpage.pvh_slock);
   3109  1.164.12.2      matt 	}
   3110  1.164.12.2      matt #endif
   3111       1.134   thorpej }
   3112       1.134   thorpej 
   3113       1.134   thorpej void
   3114       1.134   thorpej pmap_kremove(vaddr_t va, vsize_t len)
   3115       1.134   thorpej {
   3116       1.134   thorpej 	struct l2_bucket *l2b;
   3117       1.134   thorpej 	pt_entry_t *ptep, *sptep, opte;
   3118       1.134   thorpej 	vaddr_t next_bucket, eva;
   3119       1.134   thorpej 	u_int mappings;
   3120  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   3121  1.164.12.2      matt 	struct vm_page *opg;
   3122  1.164.12.2      matt #endif
   3123  1.164.12.2      matt 
   3124  1.164.12.2      matt 	PMAPCOUNT(kenter_unmappings);
   3125       1.134   thorpej 
   3126       1.134   thorpej 	NPDEBUG(PDB_KREMOVE, printf("pmap_kremove: va 0x%08lx, len 0x%08lx\n",
   3127       1.134   thorpej 	    va, len));
   3128       1.134   thorpej 
   3129       1.134   thorpej 	eva = va + len;
   3130       1.134   thorpej 
   3131       1.134   thorpej 	while (va < eva) {
   3132       1.134   thorpej 		next_bucket = L2_NEXT_BUCKET(va);
   3133       1.134   thorpej 		if (next_bucket > eva)
   3134       1.134   thorpej 			next_bucket = eva;
   3135       1.134   thorpej 
   3136       1.134   thorpej 		l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   3137       1.134   thorpej 		KDASSERT(l2b != NULL);
   3138       1.134   thorpej 
   3139       1.134   thorpej 		sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
   3140       1.134   thorpej 		mappings = 0;
   3141       1.134   thorpej 
   3142       1.134   thorpej 		while (va < next_bucket) {
   3143       1.134   thorpej 			opte = *ptep;
   3144  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   3145  1.164.12.2      matt 			opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
   3146  1.164.12.2      matt 			if (opg) {
   3147  1.164.12.2      matt 				simple_lock(&opg->mdpage.pvh_slock);
   3148  1.164.12.2      matt 				KASSERT(opg->mdpage.pvh_attrs & PVF_KENTRY);
   3149  1.164.12.2      matt 				if (PV_IS_EXEC_P(opg->mdpage.pvh_attrs)
   3150  1.164.12.2      matt 				    && !(opg->mdpage.pvh_attrs & PVF_NC)) {
   3151  1.164.12.2      matt 					if (opg->mdpage.pvh_list == NULL) {
   3152  1.164.12.2      matt 						opg->mdpage.pvh_attrs &=
   3153  1.164.12.2      matt 						    ~PVF_EXEC;
   3154  1.164.12.2      matt 						PMAPCOUNT(exec_discarded_kremove);
   3155  1.164.12.2      matt 					} else {
   3156  1.164.12.2      matt 						pmap_syncicache_page(opg);
   3157  1.164.12.2      matt 						PMAPCOUNT(exec_synced_kremove);
   3158  1.164.12.2      matt 					}
   3159  1.164.12.2      matt 				}
   3160  1.164.12.2      matt 				KASSERT(opg->mdpage.pvh_attrs | (PVF_COLORED|PVF_NC));
   3161  1.164.12.2      matt 				opg->mdpage.pvh_attrs &= ~PVF_KENTRY;
   3162  1.164.12.2      matt 				pmap_vac_me_harder(opg, NULL, 0);
   3163  1.164.12.2      matt 				simple_unlock(&opg->mdpage.pvh_slock);
   3164  1.164.12.2      matt 			}
   3165  1.164.12.2      matt #endif
   3166       1.134   thorpej 			if (l2pte_valid(opte)) {
   3167  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   3168       1.134   thorpej 				cpu_dcache_wbinv_range(va, PAGE_SIZE);
   3169  1.164.12.2      matt #endif
   3170       1.134   thorpej 				cpu_tlb_flushD_SE(va);
   3171       1.134   thorpej 			}
   3172       1.134   thorpej 			if (opte) {
   3173       1.134   thorpej 				*ptep = 0;
   3174       1.134   thorpej 				mappings++;
   3175       1.134   thorpej 			}
   3176       1.134   thorpej 			va += PAGE_SIZE;
   3177       1.134   thorpej 			ptep++;
   3178       1.134   thorpej 		}
   3179       1.134   thorpej 		KDASSERT(mappings <= l2b->l2b_occupancy);
   3180       1.134   thorpej 		l2b->l2b_occupancy -= mappings;
   3181       1.134   thorpej 		PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
   3182       1.134   thorpej 	}
   3183       1.134   thorpej 	cpu_cpwait();
   3184       1.134   thorpej }
   3185       1.134   thorpej 
   3186       1.159   thorpej bool
   3187       1.134   thorpej pmap_extract(pmap_t pm, vaddr_t va, paddr_t *pap)
   3188       1.134   thorpej {
   3189       1.134   thorpej 	struct l2_dtable *l2;
   3190       1.134   thorpej 	pd_entry_t *pl1pd, l1pd;
   3191       1.134   thorpej 	pt_entry_t *ptep, pte;
   3192       1.134   thorpej 	paddr_t pa;
   3193       1.134   thorpej 	u_int l1idx;
   3194       1.134   thorpej 
   3195       1.134   thorpej 	pmap_acquire_pmap_lock(pm);
   3196       1.134   thorpej 
   3197       1.134   thorpej 	l1idx = L1_IDX(va);
   3198       1.134   thorpej 	pl1pd = &pm->pm_l1->l1_kva[l1idx];
   3199       1.134   thorpej 	l1pd = *pl1pd;
   3200       1.134   thorpej 
   3201       1.134   thorpej 	if (l1pte_section_p(l1pd)) {
   3202       1.134   thorpej 		/*
   3203       1.134   thorpej 		 * These should only happen for pmap_kernel()
   3204       1.134   thorpej 		 */
   3205       1.134   thorpej 		KDASSERT(pm == pmap_kernel());
   3206       1.134   thorpej 		pmap_release_pmap_lock(pm);
   3207       1.134   thorpej 		pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
   3208       1.134   thorpej 	} else {
   3209       1.134   thorpej 		/*
   3210       1.134   thorpej 		 * Note that we can't rely on the validity of the L1
   3211       1.134   thorpej 		 * descriptor as an indication that a mapping exists.
   3212       1.134   thorpej 		 * We have to look it up in the L2 dtable.
   3213       1.134   thorpej 		 */
   3214       1.134   thorpej 		l2 = pm->pm_l2[L2_IDX(l1idx)];
   3215       1.134   thorpej 
   3216       1.134   thorpej 		if (l2 == NULL ||
   3217       1.134   thorpej 		    (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
   3218       1.134   thorpej 			pmap_release_pmap_lock(pm);
   3219  1.164.12.2      matt 			return false;
   3220       1.134   thorpej 		}
   3221       1.134   thorpej 
   3222       1.134   thorpej 		ptep = &ptep[l2pte_index(va)];
   3223       1.134   thorpej 		pte = *ptep;
   3224       1.134   thorpej 		pmap_release_pmap_lock(pm);
   3225       1.134   thorpej 
   3226       1.134   thorpej 		if (pte == 0)
   3227  1.164.12.2      matt 			return false;
   3228       1.134   thorpej 
   3229       1.134   thorpej 		switch (pte & L2_TYPE_MASK) {
   3230       1.134   thorpej 		case L2_TYPE_L:
   3231       1.134   thorpej 			pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
   3232       1.134   thorpej 			break;
   3233       1.134   thorpej 
   3234       1.134   thorpej 		default:
   3235       1.134   thorpej 			pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
   3236       1.134   thorpej 			break;
   3237       1.134   thorpej 		}
   3238       1.134   thorpej 	}
   3239       1.134   thorpej 
   3240       1.134   thorpej 	if (pap != NULL)
   3241       1.134   thorpej 		*pap = pa;
   3242       1.134   thorpej 
   3243  1.164.12.2      matt 	return true;
   3244       1.134   thorpej }
   3245       1.134   thorpej 
   3246       1.134   thorpej void
   3247       1.134   thorpej pmap_protect(pmap_t pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
   3248       1.134   thorpej {
   3249       1.134   thorpej 	struct l2_bucket *l2b;
   3250       1.134   thorpej 	pt_entry_t *ptep, pte;
   3251       1.134   thorpej 	vaddr_t next_bucket;
   3252       1.134   thorpej 	u_int flags;
   3253  1.164.12.2      matt 	u_int clr_mask;
   3254       1.134   thorpej 	int flush;
   3255       1.134   thorpej 
   3256       1.134   thorpej 	NPDEBUG(PDB_PROTECT,
   3257       1.134   thorpej 	    printf("pmap_protect: pm %p sva 0x%lx eva 0x%lx prot 0x%x\n",
   3258       1.134   thorpej 	    pm, sva, eva, prot));
   3259       1.134   thorpej 
   3260       1.134   thorpej 	if ((prot & VM_PROT_READ) == 0) {
   3261       1.134   thorpej 		pmap_remove(pm, sva, eva);
   3262       1.134   thorpej 		return;
   3263       1.134   thorpej 	}
   3264       1.134   thorpej 
   3265       1.134   thorpej 	if (prot & VM_PROT_WRITE) {
   3266       1.134   thorpej 		/*
   3267       1.134   thorpej 		 * If this is a read->write transition, just ignore it and let
   3268       1.134   thorpej 		 * uvm_fault() take care of it later.
   3269       1.134   thorpej 		 */
   3270       1.134   thorpej 		return;
   3271       1.134   thorpej 	}
   3272       1.134   thorpej 
   3273       1.134   thorpej 	PMAP_MAP_TO_HEAD_LOCK();
   3274       1.134   thorpej 	pmap_acquire_pmap_lock(pm);
   3275       1.134   thorpej 
   3276       1.134   thorpej 	flush = ((eva - sva) >= (PAGE_SIZE * 4)) ? 0 : -1;
   3277       1.134   thorpej 	flags = 0;
   3278  1.164.12.2      matt 	clr_mask = PVF_WRITE | ((prot & VM_PROT_EXECUTE) ? 0 : PVF_EXEC);
   3279       1.134   thorpej 
   3280       1.134   thorpej 	while (sva < eva) {
   3281       1.134   thorpej 		next_bucket = L2_NEXT_BUCKET(sva);
   3282       1.134   thorpej 		if (next_bucket > eva)
   3283       1.134   thorpej 			next_bucket = eva;
   3284       1.134   thorpej 
   3285       1.134   thorpej 		l2b = pmap_get_l2_bucket(pm, sva);
   3286       1.134   thorpej 		if (l2b == NULL) {
   3287       1.134   thorpej 			sva = next_bucket;
   3288       1.134   thorpej 			continue;
   3289       1.134   thorpej 		}
   3290       1.134   thorpej 
   3291       1.134   thorpej 		ptep = &l2b->l2b_kva[l2pte_index(sva)];
   3292       1.134   thorpej 
   3293       1.134   thorpej 		while (sva < next_bucket) {
   3294  1.164.12.2      matt 			pte = *ptep;
   3295  1.164.12.2      matt 			if (l2pte_valid(pte) != 0 && (pte & L2_S_PROT_W) != 0) {
   3296       1.134   thorpej 				struct vm_page *pg;
   3297       1.134   thorpej 				u_int f;
   3298       1.134   thorpej 
   3299  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   3300  1.164.12.2      matt 				/*
   3301  1.164.12.2      matt 				 * OK, at this point, we know we're doing
   3302  1.164.12.2      matt 				 * write-protect operation.  If the pmap is
   3303  1.164.12.2      matt 				 * active, write-back the page.
   3304  1.164.12.2      matt 				 */
   3305  1.164.12.2      matt 				pmap_dcache_wb_range(pm, sva, PAGE_SIZE,
   3306  1.164.12.2      matt 				    false, false);
   3307  1.164.12.2      matt #endif
   3308  1.164.12.2      matt 
   3309       1.134   thorpej 				pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
   3310       1.134   thorpej 				pte &= ~L2_S_PROT_W;
   3311       1.134   thorpej 				*ptep = pte;
   3312       1.134   thorpej 				PTE_SYNC(ptep);
   3313       1.134   thorpej 
   3314       1.134   thorpej 				if (pg != NULL) {
   3315       1.134   thorpej 					simple_lock(&pg->mdpage.pvh_slock);
   3316       1.134   thorpej 					f = pmap_modify_pv(pg, pm, sva,
   3317  1.164.12.2      matt 					    clr_mask, 0);
   3318       1.134   thorpej 					pmap_vac_me_harder(pg, pm, sva);
   3319       1.134   thorpej 					simple_unlock(&pg->mdpage.pvh_slock);
   3320       1.134   thorpej 				} else
   3321       1.134   thorpej 					f = PVF_REF | PVF_EXEC;
   3322       1.134   thorpej 
   3323       1.134   thorpej 				if (flush >= 0) {
   3324       1.134   thorpej 					flush++;
   3325       1.134   thorpej 					flags |= f;
   3326       1.134   thorpej 				} else
   3327       1.134   thorpej 				if (PV_BEEN_EXECD(f))
   3328       1.134   thorpej 					pmap_tlb_flushID_SE(pm, sva);
   3329       1.134   thorpej 				else
   3330       1.134   thorpej 				if (PV_BEEN_REFD(f))
   3331       1.134   thorpej 					pmap_tlb_flushD_SE(pm, sva);
   3332         1.1      matt 			}
   3333       1.134   thorpej 
   3334       1.134   thorpej 			sva += PAGE_SIZE;
   3335       1.134   thorpej 			ptep++;
   3336       1.134   thorpej 		}
   3337         1.1      matt 	}
   3338         1.1      matt 
   3339       1.134   thorpej 	pmap_release_pmap_lock(pm);
   3340       1.134   thorpej 	PMAP_MAP_TO_HEAD_UNLOCK();
   3341       1.134   thorpej 
   3342       1.134   thorpej 	if (flush) {
   3343       1.134   thorpej 		if (PV_BEEN_EXECD(flags))
   3344       1.134   thorpej 			pmap_tlb_flushID(pm);
   3345       1.134   thorpej 		else
   3346       1.134   thorpej 		if (PV_BEEN_REFD(flags))
   3347       1.134   thorpej 			pmap_tlb_flushD(pm);
   3348       1.134   thorpej 	}
   3349       1.134   thorpej }
   3350       1.134   thorpej 
   3351       1.134   thorpej void
   3352       1.134   thorpej pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
   3353       1.134   thorpej {
   3354       1.134   thorpej 
   3355       1.134   thorpej 	NPDEBUG(PDB_PROTECT,
   3356       1.134   thorpej 	    printf("pmap_page_protect: pg %p (0x%08lx), prot 0x%x\n",
   3357       1.155      yamt 	    pg, VM_PAGE_TO_PHYS(pg), prot));
   3358       1.134   thorpej 
   3359       1.134   thorpej 	switch(prot) {
   3360       1.134   thorpej 		return;
   3361  1.164.12.2      matt 	case VM_PROT_READ|VM_PROT_WRITE:
   3362  1.164.12.2      matt #if defined(PMAP_CHECK_VIPT) && defined(PMAP_APX)
   3363  1.164.12.2      matt 		pmap_clearbit(pg, PVF_EXEC);
   3364  1.164.12.2      matt 		break;
   3365  1.164.12.2      matt #endif
   3366  1.164.12.2      matt 	case VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE:
   3367  1.164.12.2      matt 		break;
   3368       1.134   thorpej 
   3369       1.134   thorpej 	case VM_PROT_READ:
   3370  1.164.12.2      matt #if defined(PMAP_CHECK_VIPT) && defined(PMAP_APX)
   3371  1.164.12.2      matt 		pmap_clearbit(pg, PVF_WRITE|PVF_EXEC);
   3372  1.164.12.2      matt 		break;
   3373  1.164.12.2      matt #endif
   3374       1.134   thorpej 	case VM_PROT_READ|VM_PROT_EXECUTE:
   3375       1.134   thorpej 		pmap_clearbit(pg, PVF_WRITE);
   3376       1.134   thorpej 		break;
   3377       1.134   thorpej 
   3378       1.134   thorpej 	default:
   3379       1.134   thorpej 		pmap_page_remove(pg);
   3380       1.134   thorpej 		break;
   3381       1.134   thorpej 	}
   3382       1.134   thorpej }
   3383       1.134   thorpej 
   3384       1.134   thorpej /*
   3385       1.134   thorpej  * pmap_clear_modify:
   3386       1.134   thorpej  *
   3387       1.134   thorpej  *	Clear the "modified" attribute for a page.
   3388       1.134   thorpej  */
   3389       1.159   thorpej bool
   3390       1.134   thorpej pmap_clear_modify(struct vm_page *pg)
   3391       1.134   thorpej {
   3392       1.159   thorpej 	bool rv;
   3393       1.134   thorpej 
   3394       1.134   thorpej 	if (pg->mdpage.pvh_attrs & PVF_MOD) {
   3395       1.160   thorpej 		rv = true;
   3396       1.134   thorpej 		pmap_clearbit(pg, PVF_MOD);
   3397       1.134   thorpej 	} else
   3398       1.160   thorpej 		rv = false;
   3399       1.134   thorpej 
   3400       1.134   thorpej 	return (rv);
   3401       1.134   thorpej }
   3402       1.134   thorpej 
   3403       1.134   thorpej /*
   3404       1.134   thorpej  * pmap_clear_reference:
   3405       1.134   thorpej  *
   3406       1.134   thorpej  *	Clear the "referenced" attribute for a page.
   3407       1.134   thorpej  */
   3408       1.159   thorpej bool
   3409       1.134   thorpej pmap_clear_reference(struct vm_page *pg)
   3410       1.134   thorpej {
   3411       1.159   thorpej 	bool rv;
   3412       1.134   thorpej 
   3413       1.134   thorpej 	if (pg->mdpage.pvh_attrs & PVF_REF) {
   3414       1.160   thorpej 		rv = true;
   3415       1.134   thorpej 		pmap_clearbit(pg, PVF_REF);
   3416       1.134   thorpej 	} else
   3417       1.160   thorpej 		rv = false;
   3418       1.134   thorpej 
   3419       1.134   thorpej 	return (rv);
   3420       1.134   thorpej }
   3421       1.134   thorpej 
   3422       1.134   thorpej /*
   3423       1.134   thorpej  * pmap_is_modified:
   3424       1.134   thorpej  *
   3425       1.134   thorpej  *	Test if a page has the "modified" attribute.
   3426       1.134   thorpej  */
   3427       1.134   thorpej /* See <arm/arm32/pmap.h> */
   3428       1.134   thorpej 
   3429       1.134   thorpej /*
   3430       1.134   thorpej  * pmap_is_referenced:
   3431       1.134   thorpej  *
   3432       1.134   thorpej  *	Test if a page has the "referenced" attribute.
   3433       1.134   thorpej  */
   3434       1.134   thorpej /* See <arm/arm32/pmap.h> */
   3435       1.134   thorpej 
   3436       1.134   thorpej int
   3437       1.134   thorpej pmap_fault_fixup(pmap_t pm, vaddr_t va, vm_prot_t ftype, int user)
   3438       1.134   thorpej {
   3439       1.134   thorpej 	struct l2_dtable *l2;
   3440       1.134   thorpej 	struct l2_bucket *l2b;
   3441       1.134   thorpej 	pd_entry_t *pl1pd, l1pd;
   3442       1.134   thorpej 	pt_entry_t *ptep, pte;
   3443       1.134   thorpej 	paddr_t pa;
   3444       1.134   thorpej 	u_int l1idx;
   3445       1.134   thorpej 	int rv = 0;
   3446       1.134   thorpej 
   3447       1.134   thorpej 	PMAP_MAP_TO_HEAD_LOCK();
   3448       1.134   thorpej 	pmap_acquire_pmap_lock(pm);
   3449       1.134   thorpej 
   3450       1.134   thorpej 	l1idx = L1_IDX(va);
   3451       1.134   thorpej 
   3452       1.134   thorpej 	/*
   3453       1.134   thorpej 	 * If there is no l2_dtable for this address, then the process
   3454       1.134   thorpej 	 * has no business accessing it.
   3455       1.134   thorpej 	 *
   3456       1.134   thorpej 	 * Note: This will catch userland processes trying to access
   3457       1.134   thorpej 	 * kernel addresses.
   3458       1.134   thorpej 	 */
   3459       1.134   thorpej 	l2 = pm->pm_l2[L2_IDX(l1idx)];
   3460       1.134   thorpej 	if (l2 == NULL)
   3461       1.134   thorpej 		goto out;
   3462       1.134   thorpej 
   3463         1.1      matt 	/*
   3464       1.134   thorpej 	 * Likewise if there is no L2 descriptor table
   3465         1.1      matt 	 */
   3466       1.134   thorpej 	l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   3467       1.134   thorpej 	if (l2b->l2b_kva == NULL)
   3468       1.134   thorpej 		goto out;
   3469       1.134   thorpej 
   3470       1.134   thorpej 	/*
   3471       1.134   thorpej 	 * Check the PTE itself.
   3472       1.134   thorpej 	 */
   3473       1.134   thorpej 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   3474       1.134   thorpej 	pte = *ptep;
   3475       1.134   thorpej 	if (pte == 0)
   3476       1.134   thorpej 		goto out;
   3477       1.134   thorpej 
   3478       1.134   thorpej 	/*
   3479       1.134   thorpej 	 * Catch a userland access to the vector page mapped at 0x0
   3480       1.134   thorpej 	 */
   3481       1.134   thorpej 	if (user && (pte & L2_S_PROT_U) == 0)
   3482       1.134   thorpej 		goto out;
   3483       1.134   thorpej 
   3484       1.134   thorpej 	pa = l2pte_pa(pte);
   3485       1.134   thorpej 
   3486       1.134   thorpej 	if ((ftype & VM_PROT_WRITE) && (pte & L2_S_PROT_W) == 0) {
   3487       1.134   thorpej 		/*
   3488       1.134   thorpej 		 * This looks like a good candidate for "page modified"
   3489       1.134   thorpej 		 * emulation...
   3490       1.134   thorpej 		 */
   3491       1.134   thorpej 		struct pv_entry *pv;
   3492       1.134   thorpej 		struct vm_page *pg;
   3493       1.134   thorpej 
   3494       1.134   thorpej 		/* Extract the physical address of the page */
   3495       1.134   thorpej 		if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
   3496       1.134   thorpej 			goto out;
   3497       1.134   thorpej 
   3498       1.134   thorpej 		/* Get the current flags for this page. */
   3499       1.134   thorpej 		simple_lock(&pg->mdpage.pvh_slock);
   3500       1.134   thorpej 
   3501       1.134   thorpej 		pv = pmap_find_pv(pg, pm, va);
   3502       1.134   thorpej 		if (pv == NULL) {
   3503       1.134   thorpej 	    		simple_unlock(&pg->mdpage.pvh_slock);
   3504       1.134   thorpej 			goto out;
   3505       1.134   thorpej 		}
   3506       1.134   thorpej 
   3507       1.134   thorpej 		/*
   3508       1.134   thorpej 		 * Do the flags say this page is writable? If not then it
   3509       1.134   thorpej 		 * is a genuine write fault. If yes then the write fault is
   3510       1.134   thorpej 		 * our fault as we did not reflect the write access in the
   3511       1.134   thorpej 		 * PTE. Now we know a write has occurred we can correct this
   3512       1.134   thorpej 		 * and also set the modified bit
   3513       1.134   thorpej 		 */
   3514       1.134   thorpej 		if ((pv->pv_flags & PVF_WRITE) == 0) {
   3515       1.134   thorpej 		    	simple_unlock(&pg->mdpage.pvh_slock);
   3516       1.134   thorpej 			goto out;
   3517       1.134   thorpej 		}
   3518       1.134   thorpej 
   3519       1.134   thorpej 		NPDEBUG(PDB_FOLLOW,
   3520       1.134   thorpej 		    printf("pmap_fault_fixup: mod emul. pm %p, va 0x%08lx, pa 0x%08lx\n",
   3521       1.155      yamt 		    pm, va, VM_PAGE_TO_PHYS(pg)));
   3522       1.134   thorpej 
   3523       1.134   thorpej 		pg->mdpage.pvh_attrs |= PVF_REF | PVF_MOD;
   3524       1.134   thorpej 		pv->pv_flags |= PVF_REF | PVF_MOD;
   3525       1.134   thorpej 		simple_unlock(&pg->mdpage.pvh_slock);
   3526       1.134   thorpej 
   3527       1.134   thorpej 		/*
   3528       1.134   thorpej 		 * Re-enable write permissions for the page.  No need to call
   3529       1.134   thorpej 		 * pmap_vac_me_harder(), since this is just a
   3530       1.134   thorpej 		 * modified-emulation fault, and the PVF_WRITE bit isn't
   3531       1.134   thorpej 		 * changing. We've already set the cacheable bits based on
   3532       1.134   thorpej 		 * the assumption that we can write to this page.
   3533       1.134   thorpej 		 */
   3534       1.134   thorpej 		*ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO | L2_S_PROT_W;
   3535       1.134   thorpej 		PTE_SYNC(ptep);
   3536       1.134   thorpej 		rv = 1;
   3537       1.134   thorpej 	} else
   3538       1.134   thorpej 	if ((pte & L2_TYPE_MASK) == L2_TYPE_INV) {
   3539       1.134   thorpej 		/*
   3540       1.134   thorpej 		 * This looks like a good candidate for "page referenced"
   3541       1.134   thorpej 		 * emulation.
   3542       1.134   thorpej 		 */
   3543       1.134   thorpej 		struct pv_entry *pv;
   3544       1.134   thorpej 		struct vm_page *pg;
   3545       1.134   thorpej 
   3546       1.134   thorpej 		/* Extract the physical address of the page */
   3547       1.134   thorpej 		if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
   3548       1.134   thorpej 			goto out;
   3549       1.134   thorpej 
   3550       1.134   thorpej 		/* Get the current flags for this page. */
   3551       1.134   thorpej 		simple_lock(&pg->mdpage.pvh_slock);
   3552       1.134   thorpej 
   3553       1.134   thorpej 		pv = pmap_find_pv(pg, pm, va);
   3554       1.134   thorpej 		if (pv == NULL) {
   3555       1.134   thorpej 	    		simple_unlock(&pg->mdpage.pvh_slock);
   3556       1.134   thorpej 			goto out;
   3557       1.134   thorpej 		}
   3558       1.134   thorpej 
   3559       1.134   thorpej 		pg->mdpage.pvh_attrs |= PVF_REF;
   3560       1.134   thorpej 		pv->pv_flags |= PVF_REF;
   3561       1.134   thorpej 		simple_unlock(&pg->mdpage.pvh_slock);
   3562         1.1      matt 
   3563       1.134   thorpej 		NPDEBUG(PDB_FOLLOW,
   3564       1.134   thorpej 		    printf("pmap_fault_fixup: ref emul. pm %p, va 0x%08lx, pa 0x%08lx\n",
   3565       1.155      yamt 		    pm, va, VM_PAGE_TO_PHYS(pg)));
   3566       1.134   thorpej 
   3567       1.134   thorpej 		*ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO;
   3568       1.134   thorpej 		PTE_SYNC(ptep);
   3569       1.134   thorpej 		rv = 1;
   3570       1.134   thorpej 	}
   3571       1.134   thorpej 
   3572       1.134   thorpej 	/*
   3573       1.134   thorpej 	 * We know there is a valid mapping here, so simply
   3574       1.134   thorpej 	 * fix up the L1 if necessary.
   3575       1.134   thorpej 	 */
   3576       1.134   thorpej 	pl1pd = &pm->pm_l1->l1_kva[l1idx];
   3577       1.134   thorpej 	l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) | L1_C_PROTO;
   3578       1.134   thorpej 	if (*pl1pd != l1pd) {
   3579       1.134   thorpej 		*pl1pd = l1pd;
   3580       1.134   thorpej 		PTE_SYNC(pl1pd);
   3581       1.134   thorpej 		rv = 1;
   3582       1.134   thorpej 	}
   3583       1.134   thorpej 
   3584       1.134   thorpej #ifdef CPU_SA110
   3585       1.134   thorpej 	/*
   3586       1.134   thorpej 	 * There are bugs in the rev K SA110.  This is a check for one
   3587       1.134   thorpej 	 * of them.
   3588       1.134   thorpej 	 */
   3589       1.134   thorpej 	if (rv == 0 && curcpu()->ci_arm_cputype == CPU_ID_SA110 &&
   3590       1.134   thorpej 	    curcpu()->ci_arm_cpurev < 3) {
   3591       1.134   thorpej 		/* Always current pmap */
   3592       1.134   thorpej 		if (l2pte_valid(pte)) {
   3593       1.134   thorpej 			extern int kernel_debug;
   3594       1.134   thorpej 			if (kernel_debug & 1) {
   3595       1.134   thorpej 				struct proc *p = curlwp->l_proc;
   3596       1.134   thorpej 				printf("prefetch_abort: page is already "
   3597       1.134   thorpej 				    "mapped - pte=%p *pte=%08x\n", ptep, pte);
   3598       1.134   thorpej 				printf("prefetch_abort: pc=%08lx proc=%p "
   3599       1.134   thorpej 				    "process=%s\n", va, p, p->p_comm);
   3600       1.134   thorpej 				printf("prefetch_abort: far=%08x fs=%x\n",
   3601       1.134   thorpej 				    cpu_faultaddress(), cpu_faultstatus());
   3602       1.113   thorpej 			}
   3603       1.134   thorpej #ifdef DDB
   3604       1.134   thorpej 			if (kernel_debug & 2)
   3605       1.134   thorpej 				Debugger();
   3606       1.134   thorpej #endif
   3607       1.134   thorpej 			rv = 1;
   3608         1.1      matt 		}
   3609         1.1      matt 	}
   3610       1.134   thorpej #endif /* CPU_SA110 */
   3611       1.104   thorpej 
   3612       1.134   thorpej #ifdef DEBUG
   3613       1.134   thorpej 	/*
   3614       1.134   thorpej 	 * If 'rv == 0' at this point, it generally indicates that there is a
   3615       1.134   thorpej 	 * stale TLB entry for the faulting address. This happens when two or
   3616       1.134   thorpej 	 * more processes are sharing an L1. Since we don't flush the TLB on
   3617       1.134   thorpej 	 * a context switch between such processes, we can take domain faults
   3618       1.134   thorpej 	 * for mappings which exist at the same VA in both processes. EVEN IF
   3619       1.134   thorpej 	 * WE'VE RECENTLY FIXED UP THE CORRESPONDING L1 in pmap_enter(), for
   3620       1.134   thorpej 	 * example.
   3621       1.134   thorpej 	 *
   3622       1.134   thorpej 	 * This is extremely likely to happen if pmap_enter() updated the L1
   3623       1.134   thorpej 	 * entry for a recently entered mapping. In this case, the TLB is
   3624       1.134   thorpej 	 * flushed for the new mapping, but there may still be TLB entries for
   3625       1.134   thorpej 	 * other mappings belonging to other processes in the 1MB range
   3626       1.134   thorpej 	 * covered by the L1 entry.
   3627       1.134   thorpej 	 *
   3628       1.134   thorpej 	 * Since 'rv == 0', we know that the L1 already contains the correct
   3629       1.134   thorpej 	 * value, so the fault must be due to a stale TLB entry.
   3630       1.134   thorpej 	 *
   3631       1.134   thorpej 	 * Since we always need to flush the TLB anyway in the case where we
   3632       1.134   thorpej 	 * fixed up the L1, or frobbed the L2 PTE, we effectively deal with
   3633       1.134   thorpej 	 * stale TLB entries dynamically.
   3634       1.134   thorpej 	 *
   3635       1.134   thorpej 	 * However, the above condition can ONLY happen if the current L1 is
   3636       1.134   thorpej 	 * being shared. If it happens when the L1 is unshared, it indicates
   3637       1.134   thorpej 	 * that other parts of the pmap are not doing their job WRT managing
   3638       1.134   thorpej 	 * the TLB.
   3639       1.134   thorpej 	 */
   3640       1.134   thorpej 	if (rv == 0 && pm->pm_l1->l1_domain_use_count == 1) {
   3641       1.134   thorpej 		extern int last_fault_code;
   3642       1.134   thorpej 		printf("fixup: pm %p, va 0x%lx, ftype %d - nothing to do!\n",
   3643       1.134   thorpej 		    pm, va, ftype);
   3644       1.134   thorpej 		printf("fixup: l2 %p, l2b %p, ptep %p, pl1pd %p\n",
   3645       1.134   thorpej 		    l2, l2b, ptep, pl1pd);
   3646       1.134   thorpej 		printf("fixup: pte 0x%x, l1pd 0x%x, last code 0x%x\n",
   3647       1.134   thorpej 		    pte, l1pd, last_fault_code);
   3648       1.134   thorpej #ifdef DDB
   3649       1.134   thorpej 		Debugger();
   3650       1.134   thorpej #endif
   3651       1.134   thorpej 	}
   3652       1.134   thorpej #endif
   3653       1.134   thorpej 
   3654       1.134   thorpej 	cpu_tlb_flushID_SE(va);
   3655       1.134   thorpej 	cpu_cpwait();
   3656       1.134   thorpej 
   3657       1.134   thorpej 	rv = 1;
   3658       1.104   thorpej 
   3659       1.134   thorpej out:
   3660       1.134   thorpej 	pmap_release_pmap_lock(pm);
   3661        1.17     chris 	PMAP_MAP_TO_HEAD_UNLOCK();
   3662       1.134   thorpej 
   3663       1.134   thorpej 	return (rv);
   3664       1.134   thorpej }
   3665       1.134   thorpej 
   3666       1.134   thorpej /*
   3667       1.134   thorpej  * pmap_collect: free resources held by a pmap
   3668       1.134   thorpej  *
   3669       1.134   thorpej  * => optional function.
   3670       1.134   thorpej  * => called when a process is swapped out to free memory.
   3671       1.134   thorpej  */
   3672       1.134   thorpej void
   3673       1.134   thorpej pmap_collect(pmap_t pm)
   3674       1.134   thorpej {
   3675       1.156       scw 
   3676  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   3677       1.156       scw 	pmap_idcache_wbinv_all(pm);
   3678  1.164.12.2      matt #endif
   3679       1.160   thorpej 	pm->pm_remove_all = true;
   3680       1.156       scw 	pmap_do_remove(pm, VM_MIN_ADDRESS, VM_MAX_ADDRESS, 1);
   3681       1.156       scw 	pmap_update(pm);
   3682  1.164.12.2      matt 	PMAPCOUNT(collects);
   3683         1.1      matt }
   3684         1.1      matt 
   3685         1.1      matt /*
   3686       1.134   thorpej  * Routine:	pmap_procwr
   3687       1.134   thorpej  *
   3688         1.1      matt  * Function:
   3689       1.134   thorpej  *	Synchronize caches corresponding to [addr, addr+len) in p.
   3690       1.134   thorpej  *
   3691       1.134   thorpej  */
   3692       1.134   thorpej void
   3693       1.134   thorpej pmap_procwr(struct proc *p, vaddr_t va, int len)
   3694       1.134   thorpej {
   3695       1.134   thorpej 	/* We only need to do anything if it is the current process. */
   3696       1.134   thorpej 	if (p == curproc)
   3697       1.134   thorpej 		cpu_icache_sync_range(va, len);
   3698       1.134   thorpej }
   3699       1.134   thorpej 
   3700       1.134   thorpej /*
   3701       1.134   thorpej  * Routine:	pmap_unwire
   3702       1.134   thorpej  * Function:	Clear the wired attribute for a map/virtual-address pair.
   3703       1.134   thorpej  *
   3704       1.134   thorpej  * In/out conditions:
   3705       1.134   thorpej  *		The mapping must already exist in the pmap.
   3706         1.1      matt  */
   3707       1.134   thorpej void
   3708       1.134   thorpej pmap_unwire(pmap_t pm, vaddr_t va)
   3709       1.134   thorpej {
   3710       1.134   thorpej 	struct l2_bucket *l2b;
   3711       1.134   thorpej 	pt_entry_t *ptep, pte;
   3712       1.134   thorpej 	struct vm_page *pg;
   3713       1.134   thorpej 	paddr_t pa;
   3714       1.134   thorpej 
   3715       1.134   thorpej 	NPDEBUG(PDB_WIRING, printf("pmap_unwire: pm %p, va 0x%08lx\n", pm, va));
   3716       1.134   thorpej 
   3717       1.134   thorpej 	PMAP_MAP_TO_HEAD_LOCK();
   3718       1.134   thorpej 	pmap_acquire_pmap_lock(pm);
   3719       1.134   thorpej 
   3720       1.134   thorpej 	l2b = pmap_get_l2_bucket(pm, va);
   3721       1.134   thorpej 	KDASSERT(l2b != NULL);
   3722       1.134   thorpej 
   3723       1.134   thorpej 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   3724       1.134   thorpej 	pte = *ptep;
   3725       1.134   thorpej 
   3726       1.134   thorpej 	/* Extract the physical address of the page */
   3727       1.134   thorpej 	pa = l2pte_pa(pte);
   3728         1.1      matt 
   3729       1.134   thorpej 	if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
   3730       1.134   thorpej 		/* Update the wired bit in the pv entry for this page. */
   3731       1.134   thorpej 		simple_lock(&pg->mdpage.pvh_slock);
   3732       1.134   thorpej 		(void) pmap_modify_pv(pg, pm, va, PVF_WIRED, 0);
   3733       1.134   thorpej 		simple_unlock(&pg->mdpage.pvh_slock);
   3734       1.134   thorpej 	}
   3735       1.134   thorpej 
   3736       1.134   thorpej 	pmap_release_pmap_lock(pm);
   3737       1.134   thorpej 	PMAP_MAP_TO_HEAD_UNLOCK();
   3738       1.134   thorpej }
   3739       1.134   thorpej 
   3740       1.134   thorpej void
   3741  1.164.12.3      matt pmap_switch(struct lwp *olwp, struct lwp *nlwp)
   3742         1.1      matt {
   3743  1.164.12.3      matt 	extern int block_userspace_access;
   3744  1.164.12.3      matt 	pmap_t opm, npm, rpm;
   3745  1.164.12.3      matt 	uint32_t odacr, ndacr;
   3746  1.164.12.3      matt 	int oldirqstate;
   3747       1.134   thorpej 
   3748  1.164.12.3      matt 	npm = nlwp->l_proc->p_vmspace->vm_map.pmap;
   3749  1.164.12.3      matt 	ndacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
   3750  1.164.12.3      matt 	    (DOMAIN_CLIENT << (npm->pm_domain * 2));
   3751       1.134   thorpej 
   3752  1.164.12.3      matt 	/*
   3753  1.164.12.3      matt 	 * If TTB and DACR are unchanged, short-circuit all the
   3754  1.164.12.3      matt 	 * TLB/cache management stuff.
   3755  1.164.12.3      matt 	 */
   3756  1.164.12.3      matt 	if (olwp != NULL) {
   3757  1.164.12.3      matt 		opm = olwp->l_proc->p_vmspace->vm_map.pmap;
   3758  1.164.12.3      matt 		odacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
   3759  1.164.12.3      matt 		    (DOMAIN_CLIENT << (opm->pm_domain * 2));
   3760       1.134   thorpej 
   3761  1.164.12.3      matt 		if (opm->pm_l1 == npm->pm_l1 && odacr == ndacr)
   3762  1.164.12.3      matt 			goto all_done;
   3763  1.164.12.3      matt 	} else
   3764  1.164.12.3      matt 		opm = NULL;
   3765  1.164.12.2      matt 
   3766  1.164.12.3      matt 	PMAPCOUNT(activations);
   3767  1.164.12.3      matt 	block_userspace_access = 1;
   3768       1.134   thorpej 
   3769  1.164.12.3      matt 	/*
   3770  1.164.12.3      matt 	 * If switching to a user vmspace which is different to the
   3771  1.164.12.3      matt 	 * most recent one, and the most recent one is potentially
   3772  1.164.12.3      matt 	 * live in the cache, we must write-back and invalidate the
   3773  1.164.12.3      matt 	 * entire cache.
   3774  1.164.12.3      matt 	 */
   3775  1.164.12.3      matt 	rpm = pmap_recent_user;
   3776  1.164.12.3      matt 	if (npm != pmap_kernel() && rpm && npm != rpm &&
   3777  1.164.12.3      matt 	    rpm->pm_cstate.cs_cache) {
   3778  1.164.12.3      matt 		rpm->pm_cstate.cs_cache = 0;
   3779  1.164.12.3      matt #ifdef PMAP_CACHE_VIVT
   3780  1.164.12.3      matt 		cpu_idcache_wbinv_all();
   3781  1.164.12.3      matt #endif
   3782  1.164.12.3      matt 	}
   3783       1.134   thorpej 
   3784  1.164.12.3      matt 	/* No interrupts while we frob the TTB/DACR */
   3785  1.164.12.3      matt 	oldirqstate = disable_interrupts(I32_bit | F32_bit);
   3786         1.1      matt 
   3787  1.164.12.3      matt 	/*
   3788  1.164.12.3      matt 	 * For ARM_VECTORS_LOW, we MUST, I repeat, MUST fix up the L1
   3789  1.164.12.3      matt 	 * entry corresponding to 'vector_page' in the incoming L1 table
   3790  1.164.12.3      matt 	 * before switching to it otherwise subsequent interrupts/exceptions
   3791  1.164.12.3      matt 	 * (including domain faults!) will jump into hyperspace.
   3792  1.164.12.3      matt 	 */
   3793  1.164.12.3      matt 	if (npm->pm_pl1vec != NULL) {
   3794  1.164.12.3      matt 		cpu_tlb_flushID_SE((u_int)vector_page);
   3795  1.164.12.3      matt 		cpu_cpwait();
   3796  1.164.12.3      matt 		*npm->pm_pl1vec = npm->pm_l1vec;
   3797  1.164.12.3      matt 		PTE_SYNC(npm->pm_pl1vec);
   3798  1.164.12.3      matt 	}
   3799         1.1      matt 
   3800  1.164.12.3      matt 	cpu_domains(ndacr);
   3801         1.1      matt 
   3802  1.164.12.3      matt 	if (npm == pmap_kernel() || npm == rpm) {
   3803       1.134   thorpej 		/*
   3804  1.164.12.3      matt 		 * Switching to a kernel thread, or back to the
   3805  1.164.12.3      matt 		 * same user vmspace as before... Simply update
   3806  1.164.12.3      matt 		 * the TTB (no TLB flush required)
   3807       1.134   thorpej 		 */
   3808  1.164.12.3      matt 		__asm volatile("mcr p15, 0, %0, c2, c0, 0" ::
   3809  1.164.12.3      matt 		    "r"(npm->pm_l1->l1_physaddr));
   3810  1.164.12.3      matt 		cpu_cpwait();
   3811  1.164.12.3      matt 	} else {
   3812  1.164.12.3      matt 		/*
   3813  1.164.12.3      matt 		 * Otherwise, update TTB and flush TLB
   3814  1.164.12.3      matt 		 */
   3815  1.164.12.3      matt 		cpu_context_switch(npm->pm_l1->l1_physaddr);
   3816  1.164.12.3      matt 		if (rpm != NULL)
   3817  1.164.12.3      matt 			rpm->pm_cstate.cs_tlb = 0;
   3818  1.164.12.3      matt 	}
   3819         1.1      matt 
   3820  1.164.12.3      matt 	restore_interrupts(oldirqstate);
   3821         1.1      matt 
   3822  1.164.12.3      matt 	block_userspace_access = 0;
   3823         1.1      matt 
   3824  1.164.12.3      matt  all_done:
   3825  1.164.12.3      matt 	/*
   3826  1.164.12.3      matt 	 * The new pmap is resident. Make sure it's marked
   3827  1.164.12.3      matt 	 * as resident in the cache/TLB.
   3828  1.164.12.3      matt 	 */
   3829  1.164.12.3      matt 	npm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   3830  1.164.12.3      matt 	if (npm != pmap_kernel())
   3831  1.164.12.3      matt 		pmap_recent_user = npm;
   3832         1.1      matt 
   3833  1.164.12.3      matt 	/* The old pmap is not longer active */
   3834  1.164.12.3      matt 	if (opm != NULL)
   3835  1.164.12.3      matt 		opm->pm_activated = false;
   3836  1.164.12.3      matt 
   3837  1.164.12.3      matt 	/* But the new one is */
   3838  1.164.12.3      matt 	npm->pm_activated = true;
   3839  1.164.12.3      matt }
   3840  1.164.12.3      matt 
   3841  1.164.12.3      matt void
   3842  1.164.12.3      matt pmap_activate(struct lwp *l)
   3843  1.164.12.3      matt {
   3844  1.164.12.3      matt 
   3845  1.164.12.3      matt 	if (l == curlwp &&
   3846  1.164.12.3      matt 	    l->l_proc->p_vmspace->vm_map.pmap->pm_activated == false)
   3847  1.164.12.3      matt 		pmap_switch(NULL, l);
   3848       1.134   thorpej }
   3849         1.1      matt 
   3850       1.134   thorpej void
   3851       1.134   thorpej pmap_deactivate(struct lwp *l)
   3852       1.134   thorpej {
   3853  1.164.12.3      matt 
   3854  1.164.12.3      matt 	l->l_proc->p_vmspace->vm_map.pmap->pm_activated = false;
   3855         1.1      matt }
   3856         1.1      matt 
   3857         1.1      matt void
   3858       1.134   thorpej pmap_update(pmap_t pm)
   3859         1.1      matt {
   3860         1.1      matt 
   3861       1.134   thorpej 	if (pm->pm_remove_all) {
   3862       1.134   thorpej 		/*
   3863       1.134   thorpej 		 * Finish up the pmap_remove_all() optimisation by flushing
   3864       1.134   thorpej 		 * the TLB.
   3865       1.134   thorpej 		 */
   3866       1.134   thorpej 		pmap_tlb_flushID(pm);
   3867       1.160   thorpej 		pm->pm_remove_all = false;
   3868       1.134   thorpej 	}
   3869         1.1      matt 
   3870       1.134   thorpej 	if (pmap_is_current(pm)) {
   3871       1.107   thorpej 		/*
   3872       1.134   thorpej 		 * If we're dealing with a current userland pmap, move its L1
   3873       1.134   thorpej 		 * to the end of the LRU.
   3874       1.107   thorpej 		 */
   3875       1.134   thorpej 		if (pm != pmap_kernel())
   3876       1.134   thorpej 			pmap_use_l1(pm);
   3877       1.134   thorpej 
   3878         1.1      matt 		/*
   3879       1.134   thorpej 		 * We can assume we're done with frobbing the cache/tlb for
   3880       1.134   thorpej 		 * now. Make sure any future pmap ops don't skip cache/tlb
   3881       1.134   thorpej 		 * flushes.
   3882         1.1      matt 		 */
   3883       1.134   thorpej 		pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   3884         1.1      matt 	}
   3885         1.1      matt 
   3886  1.164.12.2      matt 	PMAPCOUNT(updates);
   3887  1.164.12.2      matt 
   3888        1.96   thorpej 	/*
   3889       1.134   thorpej 	 * make sure TLB/cache operations have completed.
   3890        1.96   thorpej 	 */
   3891       1.134   thorpej 	cpu_cpwait();
   3892       1.134   thorpej }
   3893       1.134   thorpej 
   3894       1.134   thorpej void
   3895       1.134   thorpej pmap_remove_all(pmap_t pm)
   3896       1.134   thorpej {
   3897        1.96   thorpej 
   3898         1.1      matt 	/*
   3899       1.134   thorpej 	 * The vmspace described by this pmap is about to be torn down.
   3900       1.134   thorpej 	 * Until pmap_update() is called, UVM will only make calls
   3901       1.134   thorpej 	 * to pmap_remove(). We can make life much simpler by flushing
   3902       1.134   thorpej 	 * the cache now, and deferring TLB invalidation to pmap_update().
   3903         1.1      matt 	 */
   3904  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   3905       1.134   thorpej 	pmap_idcache_wbinv_all(pm);
   3906  1.164.12.2      matt #endif
   3907       1.160   thorpej 	pm->pm_remove_all = true;
   3908         1.1      matt }
   3909         1.1      matt 
   3910         1.1      matt /*
   3911       1.134   thorpej  * Retire the given physical map from service.
   3912       1.134   thorpej  * Should only be called if the map contains no valid mappings.
   3913         1.1      matt  */
   3914       1.134   thorpej void
   3915       1.134   thorpej pmap_destroy(pmap_t pm)
   3916         1.1      matt {
   3917       1.134   thorpej 	u_int count;
   3918         1.1      matt 
   3919       1.134   thorpej 	if (pm == NULL)
   3920       1.134   thorpej 		return;
   3921         1.1      matt 
   3922       1.134   thorpej 	if (pm->pm_remove_all) {
   3923       1.134   thorpej 		pmap_tlb_flushID(pm);
   3924       1.160   thorpej 		pm->pm_remove_all = false;
   3925         1.1      matt 	}
   3926        1.79   thorpej 
   3927        1.49   thorpej 	/*
   3928       1.134   thorpej 	 * Drop reference count
   3929        1.49   thorpej 	 */
   3930       1.134   thorpej 	simple_lock(&pm->pm_lock);
   3931       1.134   thorpej 	count = --pm->pm_obj.uo_refs;
   3932       1.134   thorpej 	simple_unlock(&pm->pm_lock);
   3933       1.134   thorpej 	if (count > 0) {
   3934       1.134   thorpej 		if (pmap_is_current(pm)) {
   3935       1.134   thorpej 			if (pm != pmap_kernel())
   3936       1.134   thorpej 				pmap_use_l1(pm);
   3937       1.134   thorpej 			pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   3938       1.134   thorpej 		}
   3939       1.134   thorpej 		return;
   3940       1.134   thorpej 	}
   3941        1.66   thorpej 
   3942         1.1      matt 	/*
   3943       1.134   thorpej 	 * reference count is zero, free pmap resources and then free pmap.
   3944         1.1      matt 	 */
   3945       1.134   thorpej 
   3946       1.134   thorpej 	if (vector_page < KERNEL_BASE) {
   3947  1.164.12.3      matt 		KDASSERT(!pmap_is_current(pm));
   3948       1.147       scw 
   3949       1.134   thorpej 		/* Remove the vector page mapping */
   3950       1.134   thorpej 		pmap_remove(pm, vector_page, vector_page + PAGE_SIZE);
   3951       1.134   thorpej 		pmap_update(pm);
   3952         1.1      matt 	}
   3953         1.1      matt 
   3954       1.134   thorpej 	LIST_REMOVE(pm, pm_list);
   3955       1.134   thorpej 
   3956       1.134   thorpej 	pmap_free_l1(pm);
   3957       1.134   thorpej 
   3958  1.164.12.3      matt 	if (pmap_recent_user == pm)
   3959  1.164.12.3      matt 		pmap_recent_user = NULL;
   3960  1.164.12.3      matt 
   3961       1.134   thorpej 	/* return the pmap to the pool */
   3962       1.134   thorpej 	pool_cache_put(&pmap_pmap_cache, pm);
   3963       1.134   thorpej }
   3964       1.134   thorpej 
   3965       1.134   thorpej 
   3966       1.134   thorpej /*
   3967       1.134   thorpej  * void pmap_reference(pmap_t pm)
   3968       1.134   thorpej  *
   3969       1.134   thorpej  * Add a reference to the specified pmap.
   3970       1.134   thorpej  */
   3971       1.134   thorpej void
   3972       1.134   thorpej pmap_reference(pmap_t pm)
   3973       1.134   thorpej {
   3974         1.1      matt 
   3975       1.134   thorpej 	if (pm == NULL)
   3976       1.134   thorpej 		return;
   3977         1.1      matt 
   3978       1.134   thorpej 	pmap_use_l1(pm);
   3979       1.104   thorpej 
   3980       1.134   thorpej 	simple_lock(&pm->pm_lock);
   3981       1.134   thorpej 	pm->pm_obj.uo_refs++;
   3982       1.134   thorpej 	simple_unlock(&pm->pm_lock);
   3983       1.134   thorpej }
   3984        1.49   thorpej 
   3985  1.164.12.2      matt #if ARM_MMU_V6 > 0
   3986  1.164.12.2      matt 
   3987  1.164.12.2      matt static struct evcnt pmap_prefer_nochange_ev =
   3988  1.164.12.2      matt     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "nochange");
   3989  1.164.12.2      matt static struct evcnt pmap_prefer_change_ev =
   3990  1.164.12.2      matt     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pmap prefer", "change");
   3991  1.164.12.2      matt 
   3992  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_prefer_change_ev);
   3993  1.164.12.2      matt EVCNT_ATTACH_STATIC(pmap_prefer_nochange_ev);
   3994  1.164.12.2      matt 
   3995  1.164.12.2      matt void
   3996  1.164.12.2      matt pmap_prefer(vaddr_t hint, vaddr_t *vap, int td)
   3997  1.164.12.2      matt {
   3998  1.164.12.2      matt 	vsize_t mask = arm_cache_prefer_mask | (PAGE_SIZE - 1);
   3999  1.164.12.2      matt 	vaddr_t va = *vap;
   4000  1.164.12.2      matt 	vaddr_t diff = (hint - va) & mask;
   4001  1.164.12.2      matt 	if (diff == 0) {
   4002  1.164.12.2      matt 		pmap_prefer_nochange_ev.ev_count++;
   4003  1.164.12.2      matt 	} else {
   4004  1.164.12.2      matt 		pmap_prefer_change_ev.ev_count++;
   4005  1.164.12.2      matt 		if (__predict_false(td))
   4006  1.164.12.2      matt 			va -= mask + 1;
   4007  1.164.12.2      matt 		*vap = va + diff;
   4008  1.164.12.2      matt 	}
   4009  1.164.12.2      matt }
   4010  1.164.12.2      matt #endif /* ARM_MMU_V6 */
   4011  1.164.12.2      matt 
   4012       1.134   thorpej /*
   4013       1.134   thorpej  * pmap_zero_page()
   4014       1.134   thorpej  *
   4015       1.134   thorpej  * Zero a given physical page by mapping it at a page hook point.
   4016       1.134   thorpej  * In doing the zero page op, the page we zero is mapped cachable, as with
   4017       1.134   thorpej  * StrongARM accesses to non-cached pages are non-burst making writing
   4018       1.134   thorpej  * _any_ bulk data very slow.
   4019       1.134   thorpej  */
   4020  1.164.12.2      matt #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0
   4021       1.134   thorpej void
   4022       1.134   thorpej pmap_zero_page_generic(paddr_t phys)
   4023       1.134   thorpej {
   4024  1.164.12.2      matt #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
   4025       1.134   thorpej 	struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
   4026  1.164.12.2      matt #endif
   4027  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   4028  1.164.12.2      matt 	/* Choose the last page color it had, if any */
   4029  1.164.12.2      matt 	const vsize_t va_offset = pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
   4030  1.164.12.2      matt #else
   4031  1.164.12.2      matt 	const vsize_t va_offset = 0;
   4032  1.164.12.2      matt #endif
   4033  1.164.12.2      matt 	pt_entry_t * const ptep = &cdst_pte[va_offset >> PGSHIFT];
   4034         1.1      matt 
   4035  1.164.12.2      matt #ifdef DEBUG
   4036       1.134   thorpej 	if (pg->mdpage.pvh_list != NULL)
   4037       1.134   thorpej 		panic("pmap_zero_page: page has mappings");
   4038       1.134   thorpej #endif
   4039         1.1      matt 
   4040       1.134   thorpej 	KDASSERT((phys & PGOFSET) == 0);
   4041       1.120     chris 
   4042       1.134   thorpej 	/*
   4043       1.134   thorpej 	 * Hook in the page, zero it, and purge the cache for that
   4044       1.134   thorpej 	 * zeroed page. Invalidate the TLB as needed.
   4045       1.134   thorpej 	 */
   4046  1.164.12.2      matt 	*ptep = L2_S_PROTO | phys |
   4047       1.134   thorpej 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   4048  1.164.12.2      matt 	PTE_SYNC(ptep);
   4049  1.164.12.2      matt 	cpu_tlb_flushD_SE(cdstp + va_offset);
   4050       1.134   thorpej 	cpu_cpwait();
   4051  1.164.12.2      matt 	bzero_page(cdstp + va_offset);
   4052  1.164.12.2      matt 	/*
   4053  1.164.12.2      matt 	 * Unmap the page.
   4054  1.164.12.2      matt 	 */
   4055  1.164.12.2      matt 	*ptep = 0;
   4056  1.164.12.2      matt 	PTE_SYNC(ptep);
   4057  1.164.12.2      matt 	cpu_tlb_flushD_SE(cdstp + va_offset);
   4058  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   4059  1.164.12.2      matt 	cpu_dcache_wbinv_range(cdstp + va_offset, PAGE_SIZE);
   4060  1.164.12.2      matt #endif
   4061  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   4062  1.164.12.2      matt 	/*
   4063  1.164.12.2      matt 	 * This page is now cache resident so it now has a page color.
   4064  1.164.12.2      matt 	 * Any contents have been obliterated so clear the EXEC flag.
   4065  1.164.12.2      matt 	 */
   4066  1.164.12.2      matt 	if (!pmap_is_page_colored_p(pg)) {
   4067  1.164.12.2      matt 		PMAPCOUNT(vac_color_new);
   4068  1.164.12.2      matt 		pg->mdpage.pvh_attrs |= PVF_COLORED;
   4069  1.164.12.2      matt 	}
   4070  1.164.12.2      matt 	if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs)) {
   4071  1.164.12.2      matt 		pg->mdpage.pvh_attrs &= ~PVF_EXEC;
   4072  1.164.12.2      matt 		PMAPCOUNT(exec_discarded_zero);
   4073  1.164.12.2      matt 	}
   4074  1.164.12.2      matt #endif
   4075       1.134   thorpej }
   4076  1.164.12.2      matt #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
   4077         1.1      matt 
   4078       1.134   thorpej #if ARM_MMU_XSCALE == 1
   4079       1.134   thorpej void
   4080       1.134   thorpej pmap_zero_page_xscale(paddr_t phys)
   4081       1.134   thorpej {
   4082       1.134   thorpej #ifdef DEBUG
   4083       1.134   thorpej 	struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
   4084         1.1      matt 
   4085       1.134   thorpej 	if (pg->mdpage.pvh_list != NULL)
   4086       1.134   thorpej 		panic("pmap_zero_page: page has mappings");
   4087       1.134   thorpej #endif
   4088         1.1      matt 
   4089       1.134   thorpej 	KDASSERT((phys & PGOFSET) == 0);
   4090         1.1      matt 
   4091       1.134   thorpej 	/*
   4092       1.134   thorpej 	 * Hook in the page, zero it, and purge the cache for that
   4093       1.134   thorpej 	 * zeroed page. Invalidate the TLB as needed.
   4094       1.134   thorpej 	 */
   4095       1.134   thorpej 	*cdst_pte = L2_S_PROTO | phys |
   4096       1.134   thorpej 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
   4097       1.134   thorpej 	    L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);	/* mini-data */
   4098       1.134   thorpej 	PTE_SYNC(cdst_pte);
   4099       1.134   thorpej 	cpu_tlb_flushD_SE(cdstp);
   4100       1.134   thorpej 	cpu_cpwait();
   4101       1.134   thorpej 	bzero_page(cdstp);
   4102       1.134   thorpej 	xscale_cache_clean_minidata();
   4103       1.134   thorpej }
   4104       1.134   thorpej #endif /* ARM_MMU_XSCALE == 1 */
   4105         1.1      matt 
   4106       1.134   thorpej /* pmap_pageidlezero()
   4107       1.134   thorpej  *
   4108       1.134   thorpej  * The same as above, except that we assume that the page is not
   4109       1.134   thorpej  * mapped.  This means we never have to flush the cache first.  Called
   4110       1.134   thorpej  * from the idle loop.
   4111       1.134   thorpej  */
   4112       1.159   thorpej bool
   4113       1.134   thorpej pmap_pageidlezero(paddr_t phys)
   4114       1.134   thorpej {
   4115       1.134   thorpej 	unsigned int i;
   4116       1.134   thorpej 	int *ptr;
   4117       1.160   thorpej 	bool rv = true;
   4118  1.164.12.2      matt #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
   4119  1.164.12.2      matt 	struct vm_page * const pg = PHYS_TO_VM_PAGE(phys);
   4120  1.164.12.2      matt #endif
   4121  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   4122  1.164.12.2      matt 	/* Choose the last page color it had, if any */
   4123  1.164.12.2      matt 	const vsize_t va_offset = pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
   4124  1.164.12.2      matt #else
   4125  1.164.12.2      matt 	const vsize_t va_offset = 0;
   4126  1.164.12.2      matt #endif
   4127  1.164.12.2      matt 	pt_entry_t * const ptep = &csrc_pte[va_offset >> PGSHIFT];
   4128  1.164.12.2      matt 
   4129  1.164.12.2      matt 
   4130       1.134   thorpej #ifdef DEBUG
   4131       1.134   thorpej 	if (pg->mdpage.pvh_list != NULL)
   4132       1.134   thorpej 		panic("pmap_pageidlezero: page has mappings");
   4133         1.1      matt #endif
   4134         1.1      matt 
   4135       1.134   thorpej 	KDASSERT((phys & PGOFSET) == 0);
   4136       1.134   thorpej 
   4137       1.109   thorpej 	/*
   4138       1.134   thorpej 	 * Hook in the page, zero it, and purge the cache for that
   4139       1.134   thorpej 	 * zeroed page. Invalidate the TLB as needed.
   4140       1.109   thorpej 	 */
   4141  1.164.12.2      matt 	*ptep = L2_S_PROTO | phys |
   4142       1.134   thorpej 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   4143  1.164.12.2      matt 	PTE_SYNC(ptep);
   4144  1.164.12.2      matt 	cpu_tlb_flushD_SE(cdstp + va_offset);
   4145       1.134   thorpej 	cpu_cpwait();
   4146         1.1      matt 
   4147  1.164.12.2      matt 	for (i = 0, ptr = (int *)(cdstp + va_offset);
   4148       1.134   thorpej 			i < (PAGE_SIZE / sizeof(int)); i++) {
   4149  1.164.12.2      matt 		if (sched_curcpu_runnable_p() != 0) {
   4150       1.134   thorpej 			/*
   4151       1.134   thorpej 			 * A process has become ready.  Abort now,
   4152       1.134   thorpej 			 * so we don't keep it waiting while we
   4153       1.134   thorpej 			 * do slow memory access to finish this
   4154       1.134   thorpej 			 * page.
   4155       1.134   thorpej 			 */
   4156       1.160   thorpej 			rv = false;
   4157       1.134   thorpej 			break;
   4158       1.134   thorpej 		}
   4159       1.134   thorpej 		*ptr++ = 0;
   4160        1.11     chris 	}
   4161         1.1      matt 
   4162  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   4163       1.134   thorpej 	if (rv)
   4164       1.134   thorpej 		/*
   4165       1.134   thorpej 		 * if we aborted we'll rezero this page again later so don't
   4166       1.134   thorpej 		 * purge it unless we finished it
   4167       1.134   thorpej 		 */
   4168       1.134   thorpej 		cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
   4169  1.164.12.2      matt #elif defined(PMAP_CACHE_VIPT)
   4170  1.164.12.2      matt 	/*
   4171  1.164.12.2      matt 	 * This page is now cache resident so it now has a page color.
   4172  1.164.12.2      matt 	 * Any contents have been obliterated so clear the EXEC flag.
   4173  1.164.12.2      matt 	 */
   4174  1.164.12.2      matt 	if (!pmap_is_page_colored_p(pg)) {
   4175  1.164.12.2      matt 		PMAPCOUNT(vac_color_new);
   4176  1.164.12.2      matt 		pg->mdpage.pvh_attrs |= PVF_COLORED;
   4177  1.164.12.2      matt 	}
   4178  1.164.12.2      matt 	if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs)) {
   4179  1.164.12.2      matt 		pg->mdpage.pvh_attrs &= ~PVF_EXEC;
   4180  1.164.12.2      matt 		PMAPCOUNT(exec_discarded_zero);
   4181  1.164.12.2      matt 	}
   4182  1.164.12.2      matt #endif
   4183  1.164.12.2      matt 	/*
   4184  1.164.12.2      matt 	 * Unmap the page.
   4185  1.164.12.2      matt 	 */
   4186  1.164.12.2      matt 	*ptep = 0;
   4187  1.164.12.2      matt 	PTE_SYNC(ptep);
   4188  1.164.12.2      matt 	cpu_tlb_flushD_SE(cdstp + va_offset);
   4189         1.1      matt 
   4190       1.134   thorpej 	return (rv);
   4191         1.1      matt }
   4192       1.134   thorpej 
   4193        1.48     chris /*
   4194       1.134   thorpej  * pmap_copy_page()
   4195        1.48     chris  *
   4196       1.134   thorpej  * Copy one physical page into another, by mapping the pages into
   4197       1.134   thorpej  * hook points. The same comment regarding cachability as in
   4198       1.134   thorpej  * pmap_zero_page also applies here.
   4199        1.48     chris  */
   4200  1.164.12.2      matt #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0
   4201         1.1      matt void
   4202       1.134   thorpej pmap_copy_page_generic(paddr_t src, paddr_t dst)
   4203         1.1      matt {
   4204  1.164.12.2      matt 	struct vm_page * const src_pg = PHYS_TO_VM_PAGE(src);
   4205  1.164.12.2      matt #if defined(PMAP_CACHE_VIPT) || defined(DEBUG)
   4206  1.164.12.2      matt 	struct vm_page * const dst_pg = PHYS_TO_VM_PAGE(dst);
   4207  1.164.12.2      matt #endif
   4208  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   4209  1.164.12.2      matt 	const vsize_t src_va_offset = src_pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
   4210  1.164.12.2      matt 	const vsize_t dst_va_offset = dst_pg->mdpage.pvh_attrs & arm_cache_prefer_mask;
   4211  1.164.12.2      matt #else
   4212  1.164.12.2      matt 	const vsize_t src_va_offset = 0;
   4213  1.164.12.2      matt 	const vsize_t dst_va_offset = 0;
   4214  1.164.12.2      matt #endif
   4215  1.164.12.2      matt 	pt_entry_t * const src_ptep = &csrc_pte[src_va_offset >> PGSHIFT];
   4216  1.164.12.2      matt 	pt_entry_t * const dst_ptep = &cdst_pte[dst_va_offset >> PGSHIFT];
   4217       1.105   thorpej 
   4218  1.164.12.2      matt #ifdef DEBUG
   4219       1.134   thorpej 	if (dst_pg->mdpage.pvh_list != NULL)
   4220       1.134   thorpej 		panic("pmap_copy_page: dst page has mappings");
   4221       1.134   thorpej #endif
   4222        1.83   thorpej 
   4223  1.164.12.2      matt 	KASSERT(src_pg->mdpage.pvh_attrs & (PVF_COLORED|PVF_NC));
   4224       1.134   thorpej 	KDASSERT((src & PGOFSET) == 0);
   4225       1.134   thorpej 	KDASSERT((dst & PGOFSET) == 0);
   4226       1.105   thorpej 
   4227       1.134   thorpej 	/*
   4228       1.134   thorpej 	 * Clean the source page.  Hold the source page's lock for
   4229       1.134   thorpej 	 * the duration of the copy so that no other mappings can
   4230       1.134   thorpej 	 * be created while we have a potentially aliased mapping.
   4231       1.134   thorpej 	 */
   4232       1.134   thorpej 	simple_lock(&src_pg->mdpage.pvh_slock);
   4233  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   4234       1.160   thorpej 	(void) pmap_clean_page(src_pg->mdpage.pvh_list, true);
   4235  1.164.12.2      matt #endif
   4236       1.105   thorpej 
   4237       1.134   thorpej 	/*
   4238       1.134   thorpej 	 * Map the pages into the page hook points, copy them, and purge
   4239       1.134   thorpej 	 * the cache for the appropriate page. Invalidate the TLB
   4240       1.134   thorpej 	 * as required.
   4241       1.134   thorpej 	 */
   4242  1.164.12.2      matt 	*src_ptep = L2_S_PROTO
   4243  1.164.12.2      matt 	    | src
   4244  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   4245  1.164.12.2      matt 	    | ((src_pg->mdpage.pvh_attrs & PVF_NC) ? 0 : pte_l2_s_cache_mode)
   4246  1.164.12.2      matt #endif
   4247  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   4248  1.164.12.2      matt 	    | pte_l2_s_cache_mode
   4249  1.164.12.2      matt #endif
   4250  1.164.12.2      matt 	    | L2_S_PROT(PTE_KERNEL, VM_PROT_READ);
   4251  1.164.12.2      matt 	*dst_ptep = L2_S_PROTO | dst |
   4252       1.134   thorpej 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
   4253  1.164.12.2      matt 	PTE_SYNC(src_ptep);
   4254  1.164.12.2      matt 	PTE_SYNC(dst_ptep);
   4255  1.164.12.2      matt 	cpu_tlb_flushD_SE(csrcp + src_va_offset);
   4256  1.164.12.2      matt 	cpu_tlb_flushD_SE(cdstp + dst_va_offset);
   4257       1.134   thorpej 	cpu_cpwait();
   4258  1.164.12.2      matt 	bcopy_page(csrcp + src_va_offset, cdstp + dst_va_offset);
   4259  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   4260  1.164.12.2      matt 	cpu_dcache_inv_range(csrcp + src_va_offset, PAGE_SIZE);
   4261  1.164.12.2      matt #endif
   4262       1.134   thorpej 	simple_unlock(&src_pg->mdpage.pvh_slock); /* cache is safe again */
   4263  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   4264  1.164.12.2      matt 	cpu_dcache_wbinv_range(cdstp + dst_va_offset, PAGE_SIZE);
   4265  1.164.12.2      matt #endif
   4266  1.164.12.2      matt 	/*
   4267  1.164.12.2      matt 	 * Unmap the pages.
   4268  1.164.12.2      matt 	 */
   4269  1.164.12.2      matt 	*src_ptep = 0;
   4270  1.164.12.2      matt 	*dst_ptep = 0;
   4271  1.164.12.2      matt 	PTE_SYNC(src_ptep);
   4272  1.164.12.2      matt 	PTE_SYNC(dst_ptep);
   4273  1.164.12.2      matt 	cpu_tlb_flushD_SE(csrcp + src_va_offset);
   4274  1.164.12.2      matt 	cpu_tlb_flushD_SE(cdstp + dst_va_offset);
   4275  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   4276  1.164.12.2      matt 	/*
   4277  1.164.12.2      matt 	 * Now that the destination page is in the cache, mark it as colored.
   4278  1.164.12.2      matt 	 * If this was an exec page, discard it.
   4279  1.164.12.2      matt 	 */
   4280  1.164.12.2      matt 	if (!pmap_is_page_colored_p(dst_pg)) {
   4281  1.164.12.2      matt 		PMAPCOUNT(vac_color_new);
   4282  1.164.12.2      matt 		dst_pg->mdpage.pvh_attrs |= PVF_COLORED;
   4283  1.164.12.2      matt 	}
   4284  1.164.12.2      matt 	if (PV_IS_EXEC_P(dst_pg->mdpage.pvh_attrs)) {
   4285  1.164.12.2      matt 		dst_pg->mdpage.pvh_attrs &= ~PVF_EXEC;
   4286  1.164.12.2      matt 		PMAPCOUNT(exec_discarded_copy);
   4287  1.164.12.2      matt 	}
   4288  1.164.12.2      matt #endif
   4289         1.1      matt }
   4290  1.164.12.2      matt #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
   4291         1.1      matt 
   4292       1.134   thorpej #if ARM_MMU_XSCALE == 1
   4293         1.1      matt void
   4294       1.134   thorpej pmap_copy_page_xscale(paddr_t src, paddr_t dst)
   4295         1.1      matt {
   4296       1.134   thorpej 	struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
   4297       1.134   thorpej #ifdef DEBUG
   4298       1.134   thorpej 	struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
   4299        1.14       chs 
   4300       1.134   thorpej 	if (dst_pg->mdpage.pvh_list != NULL)
   4301       1.134   thorpej 		panic("pmap_copy_page: dst page has mappings");
   4302       1.134   thorpej #endif
   4303        1.13     chris 
   4304       1.134   thorpej 	KDASSERT((src & PGOFSET) == 0);
   4305       1.134   thorpej 	KDASSERT((dst & PGOFSET) == 0);
   4306        1.14       chs 
   4307       1.134   thorpej 	/*
   4308       1.134   thorpej 	 * Clean the source page.  Hold the source page's lock for
   4309       1.134   thorpej 	 * the duration of the copy so that no other mappings can
   4310       1.134   thorpej 	 * be created while we have a potentially aliased mapping.
   4311       1.134   thorpej 	 */
   4312       1.134   thorpej 	simple_lock(&src_pg->mdpage.pvh_slock);
   4313  1.164.12.2      matt #ifdef PMAP_CACHE_VIVT
   4314       1.160   thorpej 	(void) pmap_clean_page(src_pg->mdpage.pvh_list, true);
   4315  1.164.12.2      matt #endif
   4316       1.105   thorpej 
   4317       1.134   thorpej 	/*
   4318       1.134   thorpej 	 * Map the pages into the page hook points, copy them, and purge
   4319       1.134   thorpej 	 * the cache for the appropriate page. Invalidate the TLB
   4320       1.134   thorpej 	 * as required.
   4321       1.134   thorpej 	 */
   4322       1.134   thorpej 	*csrc_pte = L2_S_PROTO | src |
   4323       1.134   thorpej 	    L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
   4324       1.134   thorpej 	    L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);	/* mini-data */
   4325       1.134   thorpej 	PTE_SYNC(csrc_pte);
   4326       1.134   thorpej 	*cdst_pte = L2_S_PROTO | dst |
   4327       1.134   thorpej 	    L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
   4328       1.134   thorpej 	    L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);	/* mini-data */
   4329       1.134   thorpej 	PTE_SYNC(cdst_pte);
   4330       1.134   thorpej 	cpu_tlb_flushD_SE(csrcp);
   4331       1.134   thorpej 	cpu_tlb_flushD_SE(cdstp);
   4332       1.134   thorpej 	cpu_cpwait();
   4333       1.134   thorpej 	bcopy_page(csrcp, cdstp);
   4334       1.134   thorpej 	simple_unlock(&src_pg->mdpage.pvh_slock); /* cache is safe again */
   4335       1.134   thorpej 	xscale_cache_clean_minidata();
   4336         1.1      matt }
   4337       1.134   thorpej #endif /* ARM_MMU_XSCALE == 1 */
   4338         1.1      matt 
   4339         1.1      matt /*
   4340       1.134   thorpej  * void pmap_virtual_space(vaddr_t *start, vaddr_t *end)
   4341         1.1      matt  *
   4342       1.134   thorpej  * Return the start and end addresses of the kernel's virtual space.
   4343       1.134   thorpej  * These values are setup in pmap_bootstrap and are updated as pages
   4344       1.134   thorpej  * are allocated.
   4345         1.1      matt  */
   4346         1.1      matt void
   4347       1.134   thorpej pmap_virtual_space(vaddr_t *start, vaddr_t *end)
   4348         1.1      matt {
   4349       1.134   thorpej 	*start = virtual_avail;
   4350       1.134   thorpej 	*end = virtual_end;
   4351         1.1      matt }
   4352         1.1      matt 
   4353         1.1      matt /*
   4354       1.134   thorpej  * Helper function for pmap_grow_l2_bucket()
   4355         1.1      matt  */
   4356       1.157     perry static inline int
   4357       1.134   thorpej pmap_grow_map(vaddr_t va, pt_entry_t cache_mode, paddr_t *pap)
   4358         1.1      matt {
   4359       1.134   thorpej 	struct l2_bucket *l2b;
   4360       1.134   thorpej 	pt_entry_t *ptep;
   4361         1.2      matt 	paddr_t pa;
   4362         1.1      matt 
   4363       1.160   thorpej 	if (uvm.page_init_done == false) {
   4364  1.164.12.2      matt #ifdef PMAP_STEAL_MEMORY
   4365  1.164.12.2      matt 		pv_addr_t pv;
   4366  1.164.12.2      matt 		pmap_boot_pagealloc(PAGE_SIZE,
   4367  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   4368  1.164.12.2      matt 		    arm_cache_prefer_mask,
   4369  1.164.12.2      matt 		    va & arm_cache_prefer_mask,
   4370  1.164.12.2      matt #else
   4371  1.164.12.2      matt 		    0, 0,
   4372  1.164.12.2      matt #endif
   4373  1.164.12.2      matt 		    &pv);
   4374  1.164.12.2      matt 		pa = pv.pv_pa;
   4375  1.164.12.2      matt #else
   4376       1.160   thorpej 		if (uvm_page_physget(&pa) == false)
   4377       1.134   thorpej 			return (1);
   4378  1.164.12.2      matt #endif	/* PMAP_STEAL_MEMORY */
   4379       1.134   thorpej 	} else {
   4380       1.134   thorpej 		struct vm_page *pg;
   4381       1.134   thorpej 		pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
   4382       1.134   thorpej 		if (pg == NULL)
   4383       1.134   thorpej 			return (1);
   4384       1.134   thorpej 		pa = VM_PAGE_TO_PHYS(pg);
   4385  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   4386  1.164.12.2      matt 		/*
   4387  1.164.12.2      matt 		 * This new page must not have any mappings.  However, it might
   4388  1.164.12.2      matt 		 * have previously used and therefore present in the cache.  If
   4389  1.164.12.2      matt 		 * it doesn't have the desired color, we have to flush it from
   4390  1.164.12.2      matt 		 * the cache.  And while we are at it, make sure to clear its
   4391  1.164.12.2      matt 		 * EXEC status.
   4392  1.164.12.2      matt 		 */
   4393  1.164.12.2      matt 		KASSERT(!(pg->mdpage.pvh_attrs & PVF_KENTRY));
   4394  1.164.12.2      matt 		KASSERT(pg->mdpage.pvh_list == NULL);
   4395  1.164.12.2      matt 		if (pmap_is_page_colored_p(pg)) {
   4396  1.164.12.2      matt 			if ((va ^ pg->mdpage.pvh_attrs) & arm_cache_prefer_mask) {
   4397  1.164.12.2      matt 				pmap_flush_page(pg);
   4398  1.164.12.2      matt 				PMAPCOUNT(vac_color_change);
   4399  1.164.12.2      matt 			} else {
   4400  1.164.12.2      matt 				PMAPCOUNT(vac_color_reuse);
   4401  1.164.12.2      matt 			}
   4402  1.164.12.2      matt 		} else {
   4403  1.164.12.2      matt 			PMAPCOUNT(vac_color_new);
   4404  1.164.12.2      matt 		}
   4405  1.164.12.2      matt 		if (PV_IS_EXEC_P(pg->mdpage.pvh_attrs))
   4406  1.164.12.2      matt 			PMAPCOUNT(exec_discarded_kremove);
   4407  1.164.12.2      matt 		/*
   4408  1.164.12.2      matt 		 * We'll pretend this page was entered by pmap_kenter_pa
   4409  1.164.12.2      matt 		 */
   4410  1.164.12.2      matt 		pg->mdpage.pvh_attrs &= (PAGE_SIZE - 1) & ~PVF_EXEC;
   4411  1.164.12.2      matt 		pg->mdpage.pvh_attrs |= va | PVF_KENTRY | PVF_COLORED | PVF_REF | PVF_MOD;
   4412  1.164.12.2      matt #endif
   4413       1.134   thorpej 	}
   4414         1.1      matt 
   4415       1.134   thorpej 	if (pap)
   4416       1.134   thorpej 		*pap = pa;
   4417         1.1      matt 
   4418  1.164.12.2      matt 	PMAPCOUNT(pt_mappings);
   4419       1.134   thorpej 	l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   4420       1.134   thorpej 	KDASSERT(l2b != NULL);
   4421         1.1      matt 
   4422       1.134   thorpej 	ptep = &l2b->l2b_kva[l2pte_index(va)];
   4423       1.134   thorpej 	*ptep = L2_S_PROTO | pa | cache_mode |
   4424       1.134   thorpej 	    L2_S_PROT(PTE_KERNEL, VM_PROT_READ | VM_PROT_WRITE);
   4425       1.134   thorpej 	PTE_SYNC(ptep);
   4426       1.134   thorpej 	memset((void *)va, 0, PAGE_SIZE);
   4427       1.134   thorpej 	return (0);
   4428         1.1      matt }
   4429         1.1      matt 
   4430         1.1      matt /*
   4431       1.134   thorpej  * This is the same as pmap_alloc_l2_bucket(), except that it is only
   4432       1.134   thorpej  * used by pmap_growkernel().
   4433         1.1      matt  */
   4434       1.157     perry static inline struct l2_bucket *
   4435       1.134   thorpej pmap_grow_l2_bucket(pmap_t pm, vaddr_t va)
   4436         1.1      matt {
   4437       1.134   thorpej 	struct l2_dtable *l2;
   4438       1.134   thorpej 	struct l2_bucket *l2b;
   4439       1.134   thorpej 	u_short l1idx;
   4440       1.134   thorpej 	vaddr_t nva;
   4441       1.134   thorpej 
   4442       1.134   thorpej 	l1idx = L1_IDX(va);
   4443       1.134   thorpej 
   4444       1.134   thorpej 	if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
   4445       1.134   thorpej 		/*
   4446       1.134   thorpej 		 * No mapping at this address, as there is
   4447       1.134   thorpej 		 * no entry in the L1 table.
   4448       1.134   thorpej 		 * Need to allocate a new l2_dtable.
   4449       1.134   thorpej 		 */
   4450       1.134   thorpej 		nva = pmap_kernel_l2dtable_kva;
   4451       1.134   thorpej 		if ((nva & PGOFSET) == 0) {
   4452       1.134   thorpej 			/*
   4453       1.134   thorpej 			 * Need to allocate a backing page
   4454       1.134   thorpej 			 */
   4455       1.134   thorpej 			if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
   4456       1.134   thorpej 				return (NULL);
   4457       1.134   thorpej 		}
   4458         1.1      matt 
   4459       1.134   thorpej 		l2 = (struct l2_dtable *)nva;
   4460       1.134   thorpej 		nva += sizeof(struct l2_dtable);
   4461        1.82   thorpej 
   4462       1.134   thorpej 		if ((nva & PGOFSET) < (pmap_kernel_l2dtable_kva & PGOFSET)) {
   4463       1.134   thorpej 			/*
   4464       1.134   thorpej 			 * The new l2_dtable straddles a page boundary.
   4465       1.134   thorpej 			 * Map in another page to cover it.
   4466       1.134   thorpej 			 */
   4467       1.134   thorpej 			if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
   4468       1.134   thorpej 				return (NULL);
   4469       1.134   thorpej 		}
   4470         1.1      matt 
   4471       1.134   thorpej 		pmap_kernel_l2dtable_kva = nva;
   4472         1.1      matt 
   4473       1.134   thorpej 		/*
   4474       1.134   thorpej 		 * Link it into the parent pmap
   4475       1.134   thorpej 		 */
   4476       1.134   thorpej 		pm->pm_l2[L2_IDX(l1idx)] = l2;
   4477        1.82   thorpej 	}
   4478        1.75   reinoud 
   4479       1.134   thorpej 	l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   4480       1.134   thorpej 
   4481       1.134   thorpej 	/*
   4482       1.134   thorpej 	 * Fetch pointer to the L2 page table associated with the address.
   4483       1.134   thorpej 	 */
   4484       1.134   thorpej 	if (l2b->l2b_kva == NULL) {
   4485       1.134   thorpej 		pt_entry_t *ptep;
   4486       1.134   thorpej 
   4487       1.134   thorpej 		/*
   4488       1.134   thorpej 		 * No L2 page table has been allocated. Chances are, this
   4489       1.134   thorpej 		 * is because we just allocated the l2_dtable, above.
   4490       1.134   thorpej 		 */
   4491       1.134   thorpej 		nva = pmap_kernel_l2ptp_kva;
   4492       1.134   thorpej 		ptep = (pt_entry_t *)nva;
   4493       1.134   thorpej 		if ((nva & PGOFSET) == 0) {
   4494       1.134   thorpej 			/*
   4495       1.134   thorpej 			 * Need to allocate a backing page
   4496       1.134   thorpej 			 */
   4497       1.134   thorpej 			if (pmap_grow_map(nva, pte_l2_s_cache_mode_pt,
   4498       1.134   thorpej 			    &pmap_kernel_l2ptp_phys))
   4499       1.134   thorpej 				return (NULL);
   4500       1.134   thorpej 			PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
   4501       1.134   thorpej 		}
   4502       1.134   thorpej 
   4503       1.134   thorpej 		l2->l2_occupancy++;
   4504       1.134   thorpej 		l2b->l2b_kva = ptep;
   4505       1.134   thorpej 		l2b->l2b_l1idx = l1idx;
   4506       1.134   thorpej 		l2b->l2b_phys = pmap_kernel_l2ptp_phys;
   4507       1.134   thorpej 
   4508       1.134   thorpej 		pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
   4509       1.134   thorpej 		pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
   4510        1.82   thorpej 	}
   4511         1.1      matt 
   4512       1.134   thorpej 	return (l2b);
   4513       1.134   thorpej }
   4514       1.134   thorpej 
   4515       1.134   thorpej vaddr_t
   4516       1.134   thorpej pmap_growkernel(vaddr_t maxkvaddr)
   4517       1.134   thorpej {
   4518       1.134   thorpej 	pmap_t kpm = pmap_kernel();
   4519       1.134   thorpej 	struct l1_ttable *l1;
   4520       1.134   thorpej 	struct l2_bucket *l2b;
   4521       1.134   thorpej 	pd_entry_t *pl1pd;
   4522       1.134   thorpej 	int s;
   4523       1.134   thorpej 
   4524       1.134   thorpej 	if (maxkvaddr <= pmap_curmaxkvaddr)
   4525       1.134   thorpej 		goto out;		/* we are OK */
   4526         1.1      matt 
   4527       1.134   thorpej 	NPDEBUG(PDB_GROWKERN,
   4528       1.134   thorpej 	    printf("pmap_growkernel: growing kernel from 0x%lx to 0x%lx\n",
   4529       1.134   thorpej 	    pmap_curmaxkvaddr, maxkvaddr));
   4530         1.1      matt 
   4531       1.134   thorpej 	KDASSERT(maxkvaddr <= virtual_end);
   4532        1.34   thorpej 
   4533       1.134   thorpej 	/*
   4534       1.134   thorpej 	 * whoops!   we need to add kernel PTPs
   4535       1.134   thorpej 	 */
   4536         1.1      matt 
   4537       1.134   thorpej 	s = splhigh();	/* to be safe */
   4538       1.134   thorpej 	simple_lock(&kpm->pm_lock);
   4539         1.1      matt 
   4540       1.134   thorpej 	/* Map 1MB at a time */
   4541       1.134   thorpej 	for (; pmap_curmaxkvaddr < maxkvaddr; pmap_curmaxkvaddr += L1_S_SIZE) {
   4542         1.1      matt 
   4543       1.134   thorpej 		l2b = pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
   4544       1.134   thorpej 		KDASSERT(l2b != NULL);
   4545         1.1      matt 
   4546       1.134   thorpej 		/* Distribute new L1 entry to all other L1s */
   4547       1.134   thorpej 		SLIST_FOREACH(l1, &l1_list, l1_link) {
   4548       1.134   thorpej 			pl1pd = &l1->l1_kva[L1_IDX(pmap_curmaxkvaddr)];
   4549       1.134   thorpej 			*pl1pd = l2b->l2b_phys | L1_C_DOM(PMAP_DOMAIN_KERNEL) |
   4550       1.134   thorpej 			    L1_C_PROTO;
   4551       1.134   thorpej 			PTE_SYNC(pl1pd);
   4552       1.134   thorpej 		}
   4553         1.1      matt 	}
   4554         1.1      matt 
   4555       1.134   thorpej 	/*
   4556       1.134   thorpej 	 * flush out the cache, expensive but growkernel will happen so
   4557       1.134   thorpej 	 * rarely
   4558       1.134   thorpej 	 */
   4559       1.134   thorpej 	cpu_dcache_wbinv_all();
   4560       1.134   thorpej 	cpu_tlb_flushD();
   4561       1.134   thorpej 	cpu_cpwait();
   4562       1.134   thorpej 
   4563       1.134   thorpej 	simple_unlock(&kpm->pm_lock);
   4564       1.134   thorpej 	splx(s);
   4565         1.1      matt 
   4566       1.134   thorpej out:
   4567       1.134   thorpej 	return (pmap_curmaxkvaddr);
   4568         1.1      matt }
   4569         1.1      matt 
   4570       1.134   thorpej /************************ Utility routines ****************************/
   4571         1.1      matt 
   4572       1.134   thorpej /*
   4573       1.134   thorpej  * vector_page_setprot:
   4574       1.134   thorpej  *
   4575       1.134   thorpej  *	Manipulate the protection of the vector page.
   4576       1.134   thorpej  */
   4577       1.134   thorpej void
   4578       1.134   thorpej vector_page_setprot(int prot)
   4579        1.11     chris {
   4580       1.134   thorpej 	struct l2_bucket *l2b;
   4581       1.134   thorpej 	pt_entry_t *ptep;
   4582       1.134   thorpej 
   4583       1.134   thorpej 	l2b = pmap_get_l2_bucket(pmap_kernel(), vector_page);
   4584       1.134   thorpej 	KDASSERT(l2b != NULL);
   4585        1.17     chris 
   4586       1.134   thorpej 	ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
   4587        1.72   thorpej 
   4588       1.134   thorpej 	*ptep = (*ptep & ~L1_S_PROT_MASK) | L2_S_PROT(PTE_KERNEL, prot);
   4589       1.134   thorpej 	PTE_SYNC(ptep);
   4590       1.134   thorpej 	cpu_tlb_flushD_SE(vector_page);
   4591        1.32   thorpej 	cpu_cpwait();
   4592        1.17     chris }
   4593        1.17     chris 
   4594        1.17     chris /*
   4595       1.134   thorpej  * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
   4596       1.160   thorpej  * Returns true if the mapping exists, else false.
   4597       1.134   thorpej  *
   4598       1.134   thorpej  * NOTE: This function is only used by a couple of arm-specific modules.
   4599       1.134   thorpej  * It is not safe to take any pmap locks here, since we could be right
   4600       1.134   thorpej  * in the middle of debugging the pmap anyway...
   4601       1.134   thorpej  *
   4602       1.160   thorpej  * It is possible for this routine to return false even though a valid
   4603       1.134   thorpej  * mapping does exist. This is because we don't lock, so the metadata
   4604       1.134   thorpej  * state may be inconsistent.
   4605       1.134   thorpej  *
   4606       1.134   thorpej  * NOTE: We can return a NULL *ptp in the case where the L1 pde is
   4607       1.134   thorpej  * a "section" mapping.
   4608         1.1      matt  */
   4609       1.159   thorpej bool
   4610       1.134   thorpej pmap_get_pde_pte(pmap_t pm, vaddr_t va, pd_entry_t **pdp, pt_entry_t **ptp)
   4611         1.1      matt {
   4612       1.134   thorpej 	struct l2_dtable *l2;
   4613       1.134   thorpej 	pd_entry_t *pl1pd, l1pd;
   4614       1.134   thorpej 	pt_entry_t *ptep;
   4615       1.134   thorpej 	u_short l1idx;
   4616       1.134   thorpej 
   4617       1.134   thorpej 	if (pm->pm_l1 == NULL)
   4618  1.164.12.2      matt 		return false;
   4619       1.134   thorpej 
   4620       1.134   thorpej 	l1idx = L1_IDX(va);
   4621       1.134   thorpej 	*pdp = pl1pd = &pm->pm_l1->l1_kva[l1idx];
   4622       1.134   thorpej 	l1pd = *pl1pd;
   4623         1.1      matt 
   4624       1.134   thorpej 	if (l1pte_section_p(l1pd)) {
   4625       1.134   thorpej 		*ptp = NULL;
   4626  1.164.12.2      matt 		return true;
   4627         1.1      matt 	}
   4628         1.1      matt 
   4629       1.134   thorpej 	if (pm->pm_l2 == NULL)
   4630  1.164.12.2      matt 		return false;
   4631        1.21     chris 
   4632       1.134   thorpej 	l2 = pm->pm_l2[L2_IDX(l1idx)];
   4633       1.104   thorpej 
   4634       1.134   thorpej 	if (l2 == NULL ||
   4635       1.134   thorpej 	    (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
   4636  1.164.12.2      matt 		return false;
   4637        1.29  rearnsha 	}
   4638        1.21     chris 
   4639       1.134   thorpej 	*ptp = &ptep[l2pte_index(va)];
   4640  1.164.12.2      matt 	return true;
   4641         1.1      matt }
   4642         1.1      matt 
   4643       1.159   thorpej bool
   4644       1.134   thorpej pmap_get_pde(pmap_t pm, vaddr_t va, pd_entry_t **pdp)
   4645         1.1      matt {
   4646       1.134   thorpej 	u_short l1idx;
   4647         1.1      matt 
   4648       1.134   thorpej 	if (pm->pm_l1 == NULL)
   4649  1.164.12.2      matt 		return false;
   4650        1.50   thorpej 
   4651       1.134   thorpej 	l1idx = L1_IDX(va);
   4652       1.134   thorpej 	*pdp = &pm->pm_l1->l1_kva[l1idx];
   4653        1.50   thorpej 
   4654  1.164.12.2      matt 	return true;
   4655         1.1      matt }
   4656         1.1      matt 
   4657       1.134   thorpej /************************ Bootstrapping routines ****************************/
   4658       1.134   thorpej 
   4659       1.134   thorpej static void
   4660       1.134   thorpej pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
   4661         1.1      matt {
   4662       1.134   thorpej 	int i;
   4663       1.134   thorpej 
   4664       1.134   thorpej 	l1->l1_kva = l1pt;
   4665       1.134   thorpej 	l1->l1_domain_use_count = 0;
   4666       1.134   thorpej 	l1->l1_domain_first = 0;
   4667       1.134   thorpej 
   4668       1.134   thorpej 	for (i = 0; i < PMAP_DOMAINS; i++)
   4669       1.134   thorpej 		l1->l1_domain_free[i] = i + 1;
   4670         1.1      matt 
   4671       1.134   thorpej 	/*
   4672       1.134   thorpej 	 * Copy the kernel's L1 entries to each new L1.
   4673       1.134   thorpej 	 */
   4674       1.134   thorpej 	if (pmap_initialized)
   4675       1.134   thorpej 		memcpy(l1pt, pmap_kernel()->pm_l1->l1_kva, L1_TABLE_SIZE);
   4676        1.50   thorpej 
   4677       1.134   thorpej 	if (pmap_extract(pmap_kernel(), (vaddr_t)l1pt,
   4678       1.160   thorpej 	    &l1->l1_physaddr) == false)
   4679       1.134   thorpej 		panic("pmap_init_l1: can't get PA of L1 at %p", l1pt);
   4680        1.50   thorpej 
   4681       1.134   thorpej 	SLIST_INSERT_HEAD(&l1_list, l1, l1_link);
   4682       1.134   thorpej 	TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
   4683         1.1      matt }
   4684         1.1      matt 
   4685        1.50   thorpej /*
   4686       1.134   thorpej  * pmap_bootstrap() is called from the board-specific initarm() routine
   4687       1.134   thorpej  * once the kernel L1/L2 descriptors tables have been set up.
   4688       1.134   thorpej  *
   4689       1.134   thorpej  * This is a somewhat convoluted process since pmap bootstrap is, effectively,
   4690       1.134   thorpej  * spread over a number of disparate files/functions.
   4691        1.50   thorpej  *
   4692       1.134   thorpej  * We are passed the following parameters
   4693       1.134   thorpej  *  - kernel_l1pt
   4694       1.134   thorpej  *    This is a pointer to the base of the kernel's L1 translation table.
   4695       1.134   thorpej  *  - vstart
   4696       1.134   thorpej  *    1MB-aligned start of managed kernel virtual memory.
   4697       1.134   thorpej  *  - vend
   4698       1.134   thorpej  *    1MB-aligned end of managed kernel virtual memory.
   4699        1.50   thorpej  *
   4700       1.134   thorpej  * We use the first parameter to build the metadata (struct l1_ttable and
   4701       1.134   thorpej  * struct l2_dtable) necessary to track kernel mappings.
   4702        1.50   thorpej  */
   4703       1.134   thorpej #define	PMAP_STATIC_L2_SIZE 16
   4704       1.134   thorpej void
   4705  1.164.12.2      matt pmap_bootstrap(vaddr_t vstart, vaddr_t vend)
   4706         1.1      matt {
   4707       1.134   thorpej 	static struct l1_ttable static_l1;
   4708       1.134   thorpej 	static struct l2_dtable static_l2[PMAP_STATIC_L2_SIZE];
   4709       1.134   thorpej 	struct l1_ttable *l1 = &static_l1;
   4710       1.134   thorpej 	struct l2_dtable *l2;
   4711       1.134   thorpej 	struct l2_bucket *l2b;
   4712  1.164.12.2      matt 	pd_entry_t *l1pt = (pd_entry_t *) kernel_l1pt.pv_va;
   4713       1.134   thorpej 	pmap_t pm = pmap_kernel();
   4714       1.134   thorpej 	pd_entry_t pde;
   4715       1.134   thorpej 	pt_entry_t *ptep;
   4716         1.2      matt 	paddr_t pa;
   4717       1.134   thorpej 	vaddr_t va;
   4718       1.134   thorpej 	vsize_t size;
   4719  1.164.12.2      matt 	int nptes, l1idx, l2idx, l2next = 0;
   4720       1.134   thorpej 
   4721       1.134   thorpej 	/*
   4722       1.134   thorpej 	 * Initialise the kernel pmap object
   4723       1.134   thorpej 	 */
   4724       1.134   thorpej 	pm->pm_l1 = l1;
   4725       1.134   thorpej 	pm->pm_domain = PMAP_DOMAIN_KERNEL;
   4726  1.164.12.3      matt 	pm->pm_activated = true;
   4727       1.134   thorpej 	pm->pm_cstate.cs_all = PMAP_CACHE_STATE_ALL;
   4728       1.134   thorpej 	simple_lock_init(&pm->pm_lock);
   4729       1.134   thorpej 	pm->pm_obj.pgops = NULL;
   4730       1.134   thorpej 	TAILQ_INIT(&pm->pm_obj.memq);
   4731       1.134   thorpej 	pm->pm_obj.uo_npages = 0;
   4732       1.134   thorpej 	pm->pm_obj.uo_refs = 1;
   4733       1.134   thorpej 
   4734       1.134   thorpej 	/*
   4735       1.134   thorpej 	 * Scan the L1 translation table created by initarm() and create
   4736       1.134   thorpej 	 * the required metadata for all valid mappings found in it.
   4737       1.134   thorpej 	 */
   4738       1.134   thorpej 	for (l1idx = 0; l1idx < (L1_TABLE_SIZE / sizeof(pd_entry_t)); l1idx++) {
   4739  1.164.12.2      matt 		pde = l1pt[l1idx];
   4740       1.134   thorpej 
   4741       1.134   thorpej 		/*
   4742       1.134   thorpej 		 * We're only interested in Coarse mappings.
   4743       1.134   thorpej 		 * pmap_extract() can deal with section mappings without
   4744       1.134   thorpej 		 * recourse to checking L2 metadata.
   4745       1.134   thorpej 		 */
   4746       1.134   thorpej 		if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
   4747       1.134   thorpej 			continue;
   4748       1.134   thorpej 
   4749       1.134   thorpej 		/*
   4750       1.134   thorpej 		 * Lookup the KVA of this L2 descriptor table
   4751       1.134   thorpej 		 */
   4752       1.134   thorpej 		pa = (paddr_t)(pde & L1_C_ADDR_MASK);
   4753       1.134   thorpej 		ptep = (pt_entry_t *)kernel_pt_lookup(pa);
   4754       1.134   thorpej 		if (ptep == NULL) {
   4755       1.134   thorpej 			panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
   4756       1.134   thorpej 			    (u_int)l1idx << L1_S_SHIFT, pa);
   4757       1.134   thorpej 		}
   4758       1.134   thorpej 
   4759       1.134   thorpej 		/*
   4760       1.134   thorpej 		 * Fetch the associated L2 metadata structure.
   4761       1.134   thorpej 		 * Allocate a new one if necessary.
   4762       1.134   thorpej 		 */
   4763       1.134   thorpej 		if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
   4764       1.134   thorpej 			if (l2next == PMAP_STATIC_L2_SIZE)
   4765       1.134   thorpej 				panic("pmap_bootstrap: out of static L2s");
   4766       1.134   thorpej 			pm->pm_l2[L2_IDX(l1idx)] = l2 = &static_l2[l2next++];
   4767       1.134   thorpej 		}
   4768       1.134   thorpej 
   4769       1.134   thorpej 		/*
   4770       1.134   thorpej 		 * One more L1 slot tracked...
   4771       1.134   thorpej 		 */
   4772       1.134   thorpej 		l2->l2_occupancy++;
   4773       1.134   thorpej 
   4774       1.134   thorpej 		/*
   4775       1.134   thorpej 		 * Fill in the details of the L2 descriptor in the
   4776       1.134   thorpej 		 * appropriate bucket.
   4777       1.134   thorpej 		 */
   4778       1.134   thorpej 		l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
   4779       1.134   thorpej 		l2b->l2b_kva = ptep;
   4780       1.134   thorpej 		l2b->l2b_phys = pa;
   4781       1.134   thorpej 		l2b->l2b_l1idx = l1idx;
   4782         1.1      matt 
   4783       1.134   thorpej 		/*
   4784       1.134   thorpej 		 * Establish an initial occupancy count for this descriptor
   4785       1.134   thorpej 		 */
   4786       1.134   thorpej 		for (l2idx = 0;
   4787       1.134   thorpej 		    l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
   4788       1.134   thorpej 		    l2idx++) {
   4789       1.134   thorpej 			if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
   4790       1.134   thorpej 				l2b->l2b_occupancy++;
   4791       1.134   thorpej 			}
   4792       1.134   thorpej 		}
   4793         1.1      matt 
   4794       1.134   thorpej 		/*
   4795       1.134   thorpej 		 * Make sure the descriptor itself has the correct cache mode.
   4796       1.146  jdolecek 		 * If not, fix it, but whine about the problem. Port-meisters
   4797       1.134   thorpej 		 * should consider this a clue to fix up their initarm()
   4798       1.134   thorpej 		 * function. :)
   4799       1.134   thorpej 		 */
   4800  1.164.12.2      matt 		if (pmap_set_pt_cache_mode(l1pt, (vaddr_t)ptep)) {
   4801       1.134   thorpej 			printf("pmap_bootstrap: WARNING! wrong cache mode for "
   4802       1.134   thorpej 			    "L2 pte @ %p\n", ptep);
   4803       1.134   thorpej 		}
   4804       1.134   thorpej 	}
   4805        1.61   thorpej 
   4806       1.134   thorpej 	/*
   4807       1.134   thorpej 	 * Ensure the primary (kernel) L1 has the correct cache mode for
   4808       1.134   thorpej 	 * a page table. Bitch if it is not correctly set.
   4809       1.134   thorpej 	 */
   4810  1.164.12.2      matt 	for (va = (vaddr_t)l1pt;
   4811  1.164.12.2      matt 	    va < ((vaddr_t)l1pt + L1_TABLE_SIZE); va += PAGE_SIZE) {
   4812  1.164.12.2      matt 		if (pmap_set_pt_cache_mode(l1pt, va))
   4813       1.134   thorpej 			printf("pmap_bootstrap: WARNING! wrong cache mode for "
   4814       1.134   thorpej 			    "primary L1 @ 0x%lx\n", va);
   4815         1.1      matt 	}
   4816         1.1      matt 
   4817       1.134   thorpej 	cpu_dcache_wbinv_all();
   4818       1.134   thorpej 	cpu_tlb_flushID();
   4819       1.134   thorpej 	cpu_cpwait();
   4820         1.1      matt 
   4821       1.113   thorpej 	/*
   4822       1.134   thorpej 	 * now we allocate the "special" VAs which are used for tmp mappings
   4823       1.134   thorpej 	 * by the pmap (and other modules).  we allocate the VAs by advancing
   4824       1.134   thorpej 	 * virtual_avail (note that there are no pages mapped at these VAs).
   4825       1.134   thorpej 	 *
   4826       1.134   thorpej 	 * Managed KVM space start from wherever initarm() tells us.
   4827       1.113   thorpej 	 */
   4828       1.134   thorpej 	virtual_avail = vstart;
   4829       1.134   thorpej 	virtual_end = vend;
   4830       1.113   thorpej 
   4831  1.164.12.2      matt #ifdef PMAP_CACHE_VIPT
   4832  1.164.12.2      matt 	/*
   4833  1.164.12.2      matt 	 * If we have a VIPT cache, we need one page/pte per possible alias
   4834  1.164.12.2      matt 	 * page so we won't violate cache aliasing rules.
   4835  1.164.12.2      matt 	 */
   4836  1.164.12.2      matt 	virtual_avail = (virtual_avail + arm_cache_prefer_mask) & ~arm_cache_prefer_mask;
   4837  1.164.12.2      matt 	nptes = (arm_cache_prefer_mask >> PGSHIFT) + 1;
   4838  1.164.12.2      matt #else
   4839  1.164.12.2      matt 	nptes = 1;
   4840  1.164.12.2      matt #endif
   4841  1.164.12.2      matt 	pmap_alloc_specials(&virtual_avail, nptes, &csrcp, &csrc_pte);
   4842  1.164.12.2      matt 	pmap_set_pt_cache_mode(l1pt, (vaddr_t)csrc_pte);
   4843  1.164.12.2      matt 	pmap_alloc_specials(&virtual_avail, nptes, &cdstp, &cdst_pte);
   4844  1.164.12.2      matt 	pmap_set_pt_cache_mode(l1pt, (vaddr_t)cdst_pte);
   4845       1.139      matt 	pmap_alloc_specials(&virtual_avail, 1, (void *)&memhook, NULL);
   4846       1.134   thorpej 	pmap_alloc_specials(&virtual_avail, round_page(MSGBUFSIZE) / PAGE_SIZE,
   4847       1.139      matt 	    (void *)&msgbufaddr, NULL);
   4848       1.134   thorpej 
   4849       1.134   thorpej 	/*
   4850       1.134   thorpej 	 * Allocate a range of kernel virtual address space to be used
   4851       1.134   thorpej 	 * for L2 descriptor tables and metadata allocation in
   4852       1.134   thorpej 	 * pmap_growkernel().
   4853       1.134   thorpej 	 */
   4854       1.134   thorpej 	size = ((virtual_end - pmap_curmaxkvaddr) + L1_S_OFFSET) / L1_S_SIZE;
   4855       1.134   thorpej 	pmap_alloc_specials(&virtual_avail,
   4856       1.134   thorpej 	    round_page(size * L2_TABLE_SIZE_REAL) / PAGE_SIZE,
   4857       1.134   thorpej 	    &pmap_kernel_l2ptp_kva, NULL);
   4858         1.1      matt 
   4859       1.134   thorpej 	size = (size + (L2_BUCKET_SIZE - 1)) / L2_BUCKET_SIZE;
   4860       1.134   thorpej 	pmap_alloc_specials(&virtual_avail,
   4861       1.134   thorpej 	    round_page(size * sizeof(struct l2_dtable)) / PAGE_SIZE,
   4862       1.134   thorpej 	    &pmap_kernel_l2dtable_kva, NULL);
   4863         1.1      matt 
   4864       1.134   thorpej 	/*
   4865       1.134   thorpej 	 * init the static-global locks and global pmap list.
   4866       1.134   thorpej 	 */
   4867  1.164.12.3      matt 	/* spinlockinit(&pmap_main_lock, "pmaplk", 0); */
   4868         1.1      matt 
   4869       1.134   thorpej 	/*
   4870       1.134   thorpej 	 * We can now initialise the first L1's metadata.
   4871       1.134   thorpej 	 */
   4872       1.134   thorpej 	SLIST_INIT(&l1_list);
   4873       1.134   thorpej 	TAILQ_INIT(&l1_lru_list);
   4874       1.134   thorpej 	simple_lock_init(&l1_lru_lock);
   4875  1.164.12.2      matt 	pmap_init_l1(l1, l1pt);
   4876         1.1      matt 
   4877  1.164.12.3      matt 	/* Set up vector page L1 details, if necessary */
   4878  1.164.12.3      matt 	if (vector_page < KERNEL_BASE) {
   4879  1.164.12.3      matt 		pm->pm_pl1vec = &pm->pm_l1->l1_kva[L1_IDX(vector_page)];
   4880  1.164.12.3      matt 		l2b = pmap_get_l2_bucket(pm, vector_page);
   4881  1.164.12.3      matt 		pm->pm_l1vec = l2b->l2b_phys | L1_C_PROTO |
   4882  1.164.12.3      matt 		    L1_C_DOM(pm->pm_domain);
   4883  1.164.12.3      matt 	} else
   4884  1.164.12.3      matt 		pm->pm_pl1vec = NULL;
   4885  1.164.12.3      matt 
   4886         1.1      matt 	/*
   4887       1.134   thorpej 	 * Initialize the pmap pool and cache
   4888         1.1      matt 	 */
   4889       1.134   thorpej 	pool_init(&pmap_pmap_pool, sizeof(struct pmap), 0, 0, 0, "pmappl",
   4890       1.162        ad 	    &pool_allocator_nointr, IPL_NONE);
   4891       1.134   thorpej 	pool_cache_init(&pmap_pmap_cache, &pmap_pmap_pool,
   4892       1.134   thorpej 	    pmap_pmap_ctor, NULL, NULL);
   4893       1.134   thorpej 	LIST_INIT(&pmap_pmaps);
   4894       1.134   thorpej 	LIST_INSERT_HEAD(&pmap_pmaps, pm, pm_list);
   4895         1.1      matt 
   4896       1.134   thorpej 	/*
   4897       1.134   thorpej 	 * Initialize the pv pool.
   4898       1.134   thorpej 	 */
   4899       1.134   thorpej 	pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvepl",
   4900       1.162        ad 	    &pmap_bootstrap_pv_allocator, IPL_NONE);
   4901        1.29  rearnsha 
   4902       1.134   thorpej 	/*
   4903       1.134   thorpej 	 * Initialize the L2 dtable pool and cache.
   4904       1.134   thorpej 	 */
   4905       1.134   thorpej 	pool_init(&pmap_l2dtable_pool, sizeof(struct l2_dtable), 0, 0, 0,
   4906       1.162        ad 	    "l2dtblpl", NULL, IPL_NONE);
   4907       1.134   thorpej 	pool_cache_init(&pmap_l2dtable_cache, &pmap_l2dtable_pool,
   4908       1.134   thorpej 	    pmap_l2dtable_ctor, NULL, NULL);
   4909         1.1      matt 
   4910       1.134   thorpej 	/*
   4911       1.134   thorpej 	 * Initialise the L2 descriptor table pool and cache
   4912       1.134   thorpej 	 */
   4913       1.134   thorpej 	pool_init(&pmap_l2ptp_pool, L2_TABLE_SIZE_REAL, 0, L2_TABLE_SIZE_REAL,
   4914       1.162        ad 	    0, "l2ptppl", NULL, IPL_NONE);
   4915       1.134   thorpej 	pool_cache_init(&pmap_l2ptp_cache, &pmap_l2ptp_pool,
   4916       1.134   thorpej 	    pmap_l2ptp_ctor, NULL, NULL);
   4917        1.61   thorpej 
   4918       1.134   thorpej 	cpu_dcache_wbinv_all();
   4919         1.1      matt }
   4920         1.1      matt 
   4921       1.134   thorpej static int
   4922       1.134   thorpej pmap_set_pt_cache_mode(pd_entry_t *kl1, vaddr_t va)
   4923         1.1      matt {
   4924       1.134   thorpej 	pd_entry_t *pdep, pde;
   4925       1.134   thorpej 	pt_entry_t *ptep, pte;
   4926       1.134   thorpej 	vaddr_t pa;
   4927       1.134   thorpej 	int rv = 0;
   4928       1.134   thorpej 
   4929       1.134   thorpej 	/*
   4930       1.134   thorpej 	 * Make sure the descriptor itself has the correct cache mode
   4931       1.134   thorpej 	 */
   4932       1.134   thorpej 	pdep = &kl1[L1_IDX(va)];
   4933       1.134   thorpej 	pde = *pdep;
   4934       1.134   thorpej 
   4935       1.134   thorpej 	if (l1pte_section_p(pde)) {
   4936       1.134   thorpej 		if ((pde & L1_S_CACHE_MASK) != pte_l1_s_cache_mode_pt) {
   4937       1.134   thorpej 			*pdep = (pde & ~L1_S_CACHE_MASK) |
   4938       1.134   thorpej 			    pte_l1_s_cache_mode_pt;
   4939       1.134   thorpej 			PTE_SYNC(pdep);
   4940       1.134   thorpej 			cpu_dcache_wbinv_range((vaddr_t)pdep, sizeof(*pdep));
   4941       1.134   thorpej 			rv = 1;
   4942       1.134   thorpej 		}
   4943       1.134   thorpej 	} else {
   4944       1.134   thorpej 		pa = (paddr_t)(pde & L1_C_ADDR_MASK);
   4945       1.134   thorpej 		ptep = (pt_entry_t *)kernel_pt_lookup(pa);
   4946       1.134   thorpej 		if (ptep == NULL)
   4947       1.134   thorpej 			panic("pmap_bootstrap: No L2 for L2 @ va %p\n", ptep);
   4948       1.134   thorpej 
   4949       1.134   thorpej 		ptep = &ptep[l2pte_index(va)];
   4950       1.134   thorpej 		pte = *ptep;
   4951       1.134   thorpej 		if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
   4952       1.134   thorpej 			*ptep = (pte & ~L2_S_CACHE_MASK) |
   4953       1.134   thorpej 			    pte_l2_s_cache_mode_pt;
   4954       1.134   thorpej 			PTE_SYNC(ptep);
   4955       1.134   thorpej 			cpu_dcache_wbinv_range((vaddr_t)ptep, sizeof(*ptep));
   4956       1.134   thorpej 			rv = 1;
   4957       1.134   thorpej 		}
   4958       1.134   thorpej 	}
   4959       1.134   thorpej 
   4960       1.134   thorpej 	return (rv);
   4961       1.134   thorpej }
   4962         1.1      matt 
   4963       1.134   thorpej static void
   4964       1.134   thorpej pmap_alloc_specials(vaddr_t *availp, int pages, vaddr_t *vap, pt_entry_t **ptep)
   4965       1.134   thorpej {
   4966       1.134   thorpej 	vaddr_t va = *availp;
   4967       1.134   thorpej 	struct l2_bucket *l2b;
   4968         1.1      matt 
   4969       1.134   thorpej 	if (ptep) {
   4970       1.134   thorpej 		l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   4971       1.134   thorpej 		if (l2b == NULL)
   4972       1.134   thorpej 			panic("pmap_alloc_specials: no l2b for 0x%lx", va);
   4973        1.62   thorpej 
   4974       1.134   thorpej 		if (ptep)
   4975       1.134   thorpej 			*ptep = &l2b->l2b_kva[l2pte_index(va)];
   4976         1.1      matt 	}
   4977         1.1      matt 
   4978       1.134   thorpej 	*vap = va;
   4979       1.134   thorpej 	*availp = va + (PAGE_SIZE * pages);
   4980       1.134   thorpej }
   4981       1.134   thorpej 
   4982       1.134   thorpej void
   4983       1.134   thorpej pmap_init(void)
   4984       1.134   thorpej {
   4985       1.134   thorpej 	extern int physmem;
   4986         1.1      matt 
   4987       1.113   thorpej 	/*
   4988       1.134   thorpej 	 * Set the available memory vars - These do not map to real memory
   4989       1.134   thorpej 	 * addresses and cannot as the physical memory is fragmented.
   4990       1.134   thorpej 	 * They are used by ps for %mem calculations.
   4991       1.134   thorpej 	 * One could argue whether this should be the entire memory or just
   4992       1.134   thorpej 	 * the memory that is useable in a user process.
   4993       1.113   thorpej 	 */
   4994       1.134   thorpej 	avail_start = 0;
   4995       1.134   thorpej 	avail_end = physmem * PAGE_SIZE;
   4996        1.63   thorpej 
   4997         1.1      matt 	/*
   4998       1.134   thorpej 	 * Now we need to free enough pv_entry structures to allow us to get
   4999       1.134   thorpej 	 * the kmem_map/kmem_object allocated and inited (done after this
   5000       1.134   thorpej 	 * function is finished).  to do this we allocate one bootstrap page out
   5001       1.134   thorpej 	 * of kernel_map and use it to provide an initial pool of pv_entry
   5002       1.134   thorpej 	 * structures.   we never free this page.
   5003         1.1      matt 	 */
   5004       1.134   thorpej 	pool_setlowat(&pmap_pv_pool,
   5005       1.134   thorpej 	    (PAGE_SIZE / sizeof(struct pv_entry)) * 2);
   5006        1.62   thorpej 
   5007       1.160   thorpej 	pmap_initialized = true;
   5008         1.1      matt }
   5009        1.17     chris 
   5010       1.134   thorpej static vaddr_t last_bootstrap_page = 0;
   5011       1.134   thorpej static void *free_bootstrap_pages = NULL;
   5012         1.1      matt 
   5013       1.134   thorpej static void *
   5014       1.134   thorpej pmap_bootstrap_pv_page_alloc(struct pool *pp, int flags)
   5015         1.1      matt {
   5016       1.134   thorpej 	extern void *pool_page_alloc(struct pool *, int);
   5017       1.134   thorpej 	vaddr_t new_page;
   5018       1.134   thorpej 	void *rv;
   5019       1.134   thorpej 
   5020       1.134   thorpej 	if (pmap_initialized)
   5021       1.134   thorpej 		return (pool_page_alloc(pp, flags));
   5022       1.134   thorpej 
   5023       1.134   thorpej 	if (free_bootstrap_pages) {
   5024       1.134   thorpej 		rv = free_bootstrap_pages;
   5025       1.134   thorpej 		free_bootstrap_pages = *((void **)rv);
   5026       1.134   thorpej 		return (rv);
   5027       1.134   thorpej 	}
   5028       1.134   thorpej 
   5029       1.151      yamt 	new_page = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
   5030       1.151      yamt 	    UVM_KMF_WIRED | ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT));
   5031         1.1      matt 
   5032       1.134   thorpej 	KASSERT(new_page > last_bootstrap_page);
   5033       1.134   thorpej 	last_bootstrap_page = new_page;
   5034       1.134   thorpej 	return ((void *)new_page);
   5035        1.17     chris }
   5036        1.17     chris 
   5037       1.134   thorpej static void
   5038       1.134   thorpej pmap_bootstrap_pv_page_free(struct pool *pp, void *v)
   5039        1.17     chris {
   5040       1.134   thorpej 	extern void pool_page_free(struct pool *, void *);
   5041        1.17     chris 
   5042       1.150      joff 	if ((vaddr_t)v <= last_bootstrap_page) {
   5043       1.150      joff 		*((void **)v) = free_bootstrap_pages;
   5044       1.150      joff 		free_bootstrap_pages = v;
   5045       1.134   thorpej 		return;
   5046       1.134   thorpej 	}
   5047       1.114   thorpej 
   5048       1.150      joff 	if (pmap_initialized) {
   5049       1.150      joff 		pool_page_free(pp, v);
   5050       1.134   thorpej 		return;
   5051        1.57   thorpej 	}
   5052        1.17     chris }
   5053        1.17     chris 
   5054        1.17     chris /*
   5055       1.134   thorpej  * pmap_postinit()
   5056        1.17     chris  *
   5057       1.134   thorpej  * This routine is called after the vm and kmem subsystems have been
   5058       1.134   thorpej  * initialised. This allows the pmap code to perform any initialisation
   5059       1.134   thorpej  * that can only be done one the memory allocation is in place.
   5060        1.17     chris  */
   5061       1.134   thorpej void
   5062       1.134   thorpej pmap_postinit(void)
   5063        1.17     chris {
   5064       1.134   thorpej 	extern paddr_t physical_start, physical_end;
   5065       1.134   thorpej 	struct l2_bucket *l2b;
   5066       1.134   thorpej 	struct l1_ttable *l1;
   5067       1.134   thorpej 	struct pglist plist;
   5068       1.134   thorpej 	struct vm_page *m;
   5069       1.134   thorpej 	pd_entry_t *pl1pt;
   5070       1.134   thorpej 	pt_entry_t *ptep, pte;
   5071       1.134   thorpej 	vaddr_t va, eva;
   5072       1.134   thorpej 	u_int loop, needed;
   5073       1.134   thorpej 	int error;
   5074       1.114   thorpej 
   5075       1.134   thorpej 	pool_setlowat(&pmap_l2ptp_pool,
   5076       1.134   thorpej 	    (PAGE_SIZE / L2_TABLE_SIZE_REAL) * 4);
   5077       1.134   thorpej 	pool_setlowat(&pmap_l2dtable_pool,
   5078       1.134   thorpej 	    (PAGE_SIZE / sizeof(struct l2_dtable)) * 2);
   5079        1.17     chris 
   5080       1.134   thorpej 	needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
   5081       1.134   thorpej 	needed -= 1;
   5082        1.48     chris 
   5083       1.134   thorpej 	l1 = malloc(sizeof(*l1) * needed, M_VMPMAP, M_WAITOK);
   5084        1.48     chris 
   5085       1.134   thorpej 	for (loop = 0; loop < needed; loop++, l1++) {
   5086       1.134   thorpej 		/* Allocate a L1 page table */
   5087       1.151      yamt 		va = uvm_km_alloc(kernel_map, L1_TABLE_SIZE, 0, UVM_KMF_VAONLY);
   5088       1.134   thorpej 		if (va == 0)
   5089       1.134   thorpej 			panic("Cannot allocate L1 KVM");
   5090       1.134   thorpej 
   5091       1.134   thorpej 		error = uvm_pglistalloc(L1_TABLE_SIZE, physical_start,
   5092       1.134   thorpej 		    physical_end, L1_TABLE_SIZE, 0, &plist, 1, M_WAITOK);
   5093       1.134   thorpej 		if (error)
   5094       1.134   thorpej 			panic("Cannot allocate L1 physical pages");
   5095       1.134   thorpej 
   5096       1.134   thorpej 		m = TAILQ_FIRST(&plist);
   5097       1.134   thorpej 		eva = va + L1_TABLE_SIZE;
   5098       1.134   thorpej 		pl1pt = (pd_entry_t *)va;
   5099        1.48     chris 
   5100       1.134   thorpej 		while (m && va < eva) {
   5101       1.134   thorpej 			paddr_t pa = VM_PAGE_TO_PHYS(m);
   5102        1.48     chris 
   5103       1.134   thorpej 			pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE);
   5104        1.48     chris 
   5105        1.48     chris 			/*
   5106       1.134   thorpej 			 * Make sure the L1 descriptor table is mapped
   5107       1.134   thorpej 			 * with the cache-mode set to write-through.
   5108        1.48     chris 			 */
   5109       1.134   thorpej 			l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   5110       1.134   thorpej 			ptep = &l2b->l2b_kva[l2pte_index(va)];
   5111       1.134   thorpej 			pte = *ptep;
   5112       1.134   thorpej 			pte = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
   5113       1.134   thorpej 			*ptep = pte;
   5114       1.134   thorpej 			PTE_SYNC(ptep);
   5115       1.134   thorpej 			cpu_tlb_flushD_SE(va);
   5116        1.48     chris 
   5117       1.134   thorpej 			va += PAGE_SIZE;
   5118       1.149     chris 			m = TAILQ_NEXT(m, pageq);
   5119        1.48     chris 		}
   5120        1.48     chris 
   5121       1.134   thorpej #ifdef DIAGNOSTIC
   5122       1.134   thorpej 		if (m)
   5123       1.134   thorpej 			panic("pmap_alloc_l1pt: pglist not empty");
   5124       1.134   thorpej #endif	/* DIAGNOSTIC */
   5125        1.48     chris 
   5126       1.134   thorpej 		pmap_init_l1(l1, pl1pt);
   5127        1.48     chris 	}
   5128        1.48     chris 
   5129       1.134   thorpej #ifdef DEBUG
   5130       1.134   thorpej 	printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
   5131       1.134   thorpej 	    needed);
   5132       1.134   thorpej #endif
   5133        1.48     chris }
   5134        1.48     chris 
   5135        1.76   thorpej /*
   5136       1.134   thorpej  * Note that the following routines are used by board-specific initialisation
   5137       1.134   thorpej  * code to configure the initial kernel page tables.
   5138       1.134   thorpej  *
   5139       1.134   thorpej  * If ARM32_NEW_VM_LAYOUT is *not* defined, they operate on the assumption that
   5140       1.134   thorpej  * L2 page-table pages are 4KB in size and use 4 L1 slots. This mimics the
   5141       1.134   thorpej  * behaviour of the old pmap, and provides an easy migration path for
   5142       1.134   thorpej  * initial bring-up of the new pmap on existing ports. Fortunately,
   5143       1.134   thorpej  * pmap_bootstrap() compensates for this hackery. This is only a stop-gap and
   5144       1.134   thorpej  * will be deprecated.
   5145        1.76   thorpej  *
   5146       1.134   thorpej  * If ARM32_NEW_VM_LAYOUT *is* defined, these functions deal with 1KB L2 page
   5147       1.134   thorpej  * tables.
   5148        1.76   thorpej  */
   5149        1.40   thorpej 
   5150        1.40   thorpej /*
   5151        1.46   thorpej  * This list exists for the benefit of pmap_map_chunk().  It keeps track
   5152        1.46   thorpej  * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
   5153        1.46   thorpej  * find them as necessary.
   5154        1.46   thorpej  *
   5155       1.134   thorpej  * Note that the data on this list MUST remain valid after initarm() returns,
   5156       1.134   thorpej  * as pmap_bootstrap() uses it to contruct L2 table metadata.
   5157        1.46   thorpej  */
   5158        1.46   thorpej SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
   5159        1.46   thorpej 
   5160        1.46   thorpej static vaddr_t
   5161        1.46   thorpej kernel_pt_lookup(paddr_t pa)
   5162        1.46   thorpej {
   5163        1.46   thorpej 	pv_addr_t *pv;
   5164        1.46   thorpej 
   5165        1.46   thorpej 	SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
   5166       1.134   thorpej #ifndef ARM32_NEW_VM_LAYOUT
   5167       1.134   thorpej 		if (pv->pv_pa == (pa & ~PGOFSET))
   5168       1.134   thorpej 			return (pv->pv_va | (pa & PGOFSET));
   5169       1.134   thorpej #else
   5170        1.46   thorpej 		if (pv->pv_pa == pa)
   5171        1.46   thorpej 			return (pv->pv_va);
   5172       1.134   thorpej #endif
   5173        1.46   thorpej 	}
   5174        1.46   thorpej 	return (0);
   5175        1.46   thorpej }
   5176        1.46   thorpej 
   5177        1.46   thorpej /*
   5178        1.40   thorpej  * pmap_map_section:
   5179        1.40   thorpej  *
   5180        1.40   thorpej  *	Create a single section mapping.
   5181        1.40   thorpej  */
   5182        1.40   thorpej void
   5183        1.40   thorpej pmap_map_section(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
   5184        1.40   thorpej {
   5185        1.40   thorpej 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   5186       1.134   thorpej 	pd_entry_t fl;
   5187        1.40   thorpej 
   5188        1.81   thorpej 	KASSERT(((va | pa) & L1_S_OFFSET) == 0);
   5189        1.40   thorpej 
   5190       1.134   thorpej 	switch (cache) {
   5191       1.134   thorpej 	case PTE_NOCACHE:
   5192       1.134   thorpej 	default:
   5193       1.134   thorpej 		fl = 0;
   5194       1.134   thorpej 		break;
   5195       1.134   thorpej 
   5196       1.134   thorpej 	case PTE_CACHE:
   5197       1.134   thorpej 		fl = pte_l1_s_cache_mode;
   5198       1.134   thorpej 		break;
   5199       1.134   thorpej 
   5200       1.134   thorpej 	case PTE_PAGETABLE:
   5201       1.134   thorpej 		fl = pte_l1_s_cache_mode_pt;
   5202       1.134   thorpej 		break;
   5203       1.134   thorpej 	}
   5204       1.134   thorpej 
   5205        1.83   thorpej 	pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
   5206       1.134   thorpej 	    L1_S_PROT(PTE_KERNEL, prot) | fl | L1_S_DOM(PMAP_DOMAIN_KERNEL);
   5207       1.134   thorpej 	PTE_SYNC(&pde[va >> L1_S_SHIFT]);
   5208        1.41   thorpej }
   5209        1.41   thorpej 
   5210        1.41   thorpej /*
   5211        1.41   thorpej  * pmap_map_entry:
   5212        1.41   thorpej  *
   5213        1.41   thorpej  *	Create a single page mapping.
   5214        1.41   thorpej  */
   5215        1.41   thorpej void
   5216        1.47   thorpej pmap_map_entry(vaddr_t l1pt, vaddr_t va, paddr_t pa, int prot, int cache)
   5217        1.41   thorpej {
   5218        1.47   thorpej 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   5219       1.134   thorpej 	pt_entry_t fl;
   5220        1.47   thorpej 	pt_entry_t *pte;
   5221        1.41   thorpej 
   5222        1.41   thorpej 	KASSERT(((va | pa) & PGOFSET) == 0);
   5223        1.41   thorpej 
   5224       1.134   thorpej 	switch (cache) {
   5225       1.134   thorpej 	case PTE_NOCACHE:
   5226       1.134   thorpej 	default:
   5227       1.134   thorpej 		fl = 0;
   5228       1.134   thorpej 		break;
   5229       1.134   thorpej 
   5230       1.134   thorpej 	case PTE_CACHE:
   5231       1.134   thorpej 		fl = pte_l2_s_cache_mode;
   5232       1.134   thorpej 		break;
   5233       1.134   thorpej 
   5234       1.134   thorpej 	case PTE_PAGETABLE:
   5235       1.134   thorpej 		fl = pte_l2_s_cache_mode_pt;
   5236       1.134   thorpej 		break;
   5237       1.134   thorpej 	}
   5238       1.134   thorpej 
   5239        1.81   thorpej 	if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
   5240        1.47   thorpej 		panic("pmap_map_entry: no L2 table for VA 0x%08lx", va);
   5241        1.47   thorpej 
   5242       1.134   thorpej #ifndef ARM32_NEW_VM_LAYOUT
   5243        1.47   thorpej 	pte = (pt_entry_t *)
   5244        1.81   thorpej 	    kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
   5245       1.134   thorpej #else
   5246       1.134   thorpej 	pte = (pt_entry_t *) kernel_pt_lookup(pde[L1_IDX(va)] & L1_C_ADDR_MASK);
   5247       1.134   thorpej #endif
   5248        1.47   thorpej 	if (pte == NULL)
   5249        1.47   thorpej 		panic("pmap_map_entry: can't find L2 table for VA 0x%08lx", va);
   5250        1.47   thorpej 
   5251  1.164.12.2      matt 	fl |= L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot);
   5252       1.134   thorpej #ifndef ARM32_NEW_VM_LAYOUT
   5253  1.164.12.2      matt 	pte += (va >> PGSHIFT) & 0x3ff;
   5254       1.134   thorpej #else
   5255  1.164.12.2      matt 	pte += l2pte_index(va);
   5256       1.134   thorpej 	    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | fl;
   5257       1.134   thorpej #endif
   5258  1.164.12.2      matt 	*pte = fl;
   5259  1.164.12.2      matt 	PTE_SYNC(pte);
   5260        1.42   thorpej }
   5261        1.42   thorpej 
   5262        1.42   thorpej /*
   5263        1.42   thorpej  * pmap_link_l2pt:
   5264        1.42   thorpej  *
   5265       1.134   thorpej  *	Link the L2 page table specified by "l2pv" into the L1
   5266        1.42   thorpej  *	page table at the slot for "va".
   5267        1.42   thorpej  */
   5268        1.42   thorpej void
   5269        1.46   thorpej pmap_link_l2pt(vaddr_t l1pt, vaddr_t va, pv_addr_t *l2pv)
   5270        1.42   thorpej {
   5271       1.134   thorpej 	pd_entry_t *pde = (pd_entry_t *) l1pt, proto;
   5272        1.81   thorpej 	u_int slot = va >> L1_S_SHIFT;
   5273        1.42   thorpej 
   5274       1.134   thorpej #ifndef ARM32_NEW_VM_LAYOUT
   5275       1.134   thorpej 	KASSERT((va & ((L1_S_SIZE * 4) - 1)) == 0);
   5276        1.46   thorpej 	KASSERT((l2pv->pv_pa & PGOFSET) == 0);
   5277       1.134   thorpej #endif
   5278        1.46   thorpej 
   5279       1.134   thorpej 	proto = L1_S_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO;
   5280       1.134   thorpej 
   5281       1.134   thorpej 	pde[slot + 0] = proto | (l2pv->pv_pa + 0x000);
   5282       1.134   thorpej #ifdef ARM32_NEW_VM_LAYOUT
   5283       1.134   thorpej 	PTE_SYNC(&pde[slot]);
   5284       1.134   thorpej #else
   5285       1.134   thorpej 	pde[slot + 1] = proto | (l2pv->pv_pa + 0x400);
   5286       1.134   thorpej 	pde[slot + 2] = proto | (l2pv->pv_pa + 0x800);
   5287       1.134   thorpej 	pde[slot + 3] = proto | (l2pv->pv_pa + 0xc00);
   5288       1.134   thorpej 	PTE_SYNC_RANGE(&pde[slot + 0], 4);
   5289       1.134   thorpej #endif
   5290        1.42   thorpej 
   5291        1.46   thorpej 	SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
   5292        1.43   thorpej }
   5293        1.43   thorpej 
   5294        1.43   thorpej /*
   5295        1.43   thorpej  * pmap_map_chunk:
   5296        1.43   thorpej  *
   5297        1.43   thorpej  *	Map a chunk of memory using the most efficient mappings
   5298        1.43   thorpej  *	possible (section, large page, small page) into the
   5299        1.43   thorpej  *	provided L1 and L2 tables at the specified virtual address.
   5300        1.43   thorpej  */
   5301        1.43   thorpej vsize_t
   5302        1.46   thorpej pmap_map_chunk(vaddr_t l1pt, vaddr_t va, paddr_t pa, vsize_t size,
   5303        1.46   thorpej     int prot, int cache)
   5304        1.43   thorpej {
   5305        1.43   thorpej 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   5306       1.134   thorpej 	pt_entry_t *pte, f1, f2s, f2l;
   5307        1.43   thorpej 	vsize_t resid;
   5308       1.134   thorpej 	int i;
   5309        1.43   thorpej 
   5310       1.130   thorpej 	resid = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
   5311        1.43   thorpej 
   5312        1.44   thorpej 	if (l1pt == 0)
   5313        1.44   thorpej 		panic("pmap_map_chunk: no L1 table provided");
   5314        1.44   thorpej 
   5315        1.43   thorpej #ifdef VERBOSE_INIT_ARM
   5316        1.43   thorpej 	printf("pmap_map_chunk: pa=0x%lx va=0x%lx size=0x%lx resid=0x%lx "
   5317        1.43   thorpej 	    "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
   5318        1.43   thorpej #endif
   5319        1.43   thorpej 
   5320       1.134   thorpej 	switch (cache) {
   5321       1.134   thorpej 	case PTE_NOCACHE:
   5322       1.134   thorpej 	default:
   5323       1.134   thorpej 		f1 = 0;
   5324       1.134   thorpej 		f2l = 0;
   5325       1.134   thorpej 		f2s = 0;
   5326       1.134   thorpej 		break;
   5327       1.134   thorpej 
   5328       1.134   thorpej 	case PTE_CACHE:
   5329       1.134   thorpej 		f1 = pte_l1_s_cache_mode;
   5330       1.134   thorpej 		f2l = pte_l2_l_cache_mode;
   5331       1.134   thorpej 		f2s = pte_l2_s_cache_mode;
   5332       1.134   thorpej 		break;
   5333       1.134   thorpej 
   5334       1.134   thorpej 	case PTE_PAGETABLE:
   5335       1.134   thorpej 		f1 = pte_l1_s_cache_mode_pt;
   5336       1.134   thorpej 		f2l = pte_l2_l_cache_mode_pt;
   5337       1.134   thorpej 		f2s = pte_l2_s_cache_mode_pt;
   5338       1.134   thorpej 		break;
   5339       1.134   thorpej 	}
   5340       1.134   thorpej 
   5341        1.43   thorpej 	size = resid;
   5342        1.43   thorpej 
   5343        1.43   thorpej 	while (resid > 0) {
   5344        1.43   thorpej 		/* See if we can use a section mapping. */
   5345       1.134   thorpej 		if (L1_S_MAPPABLE_P(va, pa, resid)) {
   5346        1.43   thorpej #ifdef VERBOSE_INIT_ARM
   5347        1.43   thorpej 			printf("S");
   5348        1.43   thorpej #endif
   5349        1.83   thorpej 			pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
   5350       1.134   thorpej 			    L1_S_PROT(PTE_KERNEL, prot) | f1 |
   5351       1.134   thorpej 			    L1_S_DOM(PMAP_DOMAIN_KERNEL);
   5352       1.134   thorpej 			PTE_SYNC(&pde[va >> L1_S_SHIFT]);
   5353        1.81   thorpej 			va += L1_S_SIZE;
   5354        1.81   thorpej 			pa += L1_S_SIZE;
   5355        1.81   thorpej 			resid -= L1_S_SIZE;
   5356        1.43   thorpej 			continue;
   5357        1.43   thorpej 		}
   5358        1.45   thorpej 
   5359        1.45   thorpej 		/*
   5360        1.45   thorpej 		 * Ok, we're going to use an L2 table.  Make sure
   5361        1.45   thorpej 		 * one is actually in the corresponding L1 slot
   5362        1.45   thorpej 		 * for the current VA.
   5363        1.45   thorpej 		 */
   5364        1.81   thorpej 		if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
   5365        1.46   thorpej 			panic("pmap_map_chunk: no L2 table for VA 0x%08lx", va);
   5366        1.46   thorpej 
   5367       1.134   thorpej #ifndef ARM32_NEW_VM_LAYOUT
   5368        1.46   thorpej 		pte = (pt_entry_t *)
   5369        1.81   thorpej 		    kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
   5370       1.134   thorpej #else
   5371       1.134   thorpej 		pte = (pt_entry_t *) kernel_pt_lookup(
   5372       1.134   thorpej 		    pde[L1_IDX(va)] & L1_C_ADDR_MASK);
   5373       1.134   thorpej #endif
   5374        1.46   thorpej 		if (pte == NULL)
   5375        1.46   thorpej 			panic("pmap_map_chunk: can't find L2 table for VA"
   5376        1.46   thorpej 			    "0x%08lx", va);
   5377        1.43   thorpej 
   5378        1.43   thorpej 		/* See if we can use a L2 large page mapping. */
   5379       1.134   thorpej 		if (L2_L_MAPPABLE_P(va, pa, resid)) {
   5380        1.43   thorpej #ifdef VERBOSE_INIT_ARM
   5381        1.43   thorpej 			printf("L");
   5382        1.43   thorpej #endif
   5383        1.43   thorpej 			for (i = 0; i < 16; i++) {
   5384       1.134   thorpej #ifndef ARM32_NEW_VM_LAYOUT
   5385        1.43   thorpej 				pte[((va >> PGSHIFT) & 0x3f0) + i] =
   5386        1.83   thorpej 				    L2_L_PROTO | pa |
   5387       1.134   thorpej 				    L2_L_PROT(PTE_KERNEL, prot) | f2l;
   5388       1.134   thorpej 				PTE_SYNC(&pte[((va >> PGSHIFT) & 0x3f0) + i]);
   5389       1.134   thorpej #else
   5390       1.134   thorpej 				pte[l2pte_index(va) + i] =
   5391       1.134   thorpej 				    L2_L_PROTO | pa |
   5392       1.134   thorpej 				    L2_L_PROT(PTE_KERNEL, prot) | f2l;
   5393       1.134   thorpej 				PTE_SYNC(&pte[l2pte_index(va) + i]);
   5394       1.134   thorpej #endif
   5395        1.43   thorpej 			}
   5396        1.81   thorpej 			va += L2_L_SIZE;
   5397        1.81   thorpej 			pa += L2_L_SIZE;
   5398        1.81   thorpej 			resid -= L2_L_SIZE;
   5399        1.43   thorpej 			continue;
   5400        1.43   thorpej 		}
   5401        1.43   thorpej 
   5402        1.43   thorpej 		/* Use a small page mapping. */
   5403        1.43   thorpej #ifdef VERBOSE_INIT_ARM
   5404        1.43   thorpej 		printf("P");
   5405        1.43   thorpej #endif
   5406       1.134   thorpej #ifndef ARM32_NEW_VM_LAYOUT
   5407       1.134   thorpej 		pte[(va >> PGSHIFT) & 0x3ff] =
   5408       1.134   thorpej 		    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
   5409       1.134   thorpej 		PTE_SYNC(&pte[(va >> PGSHIFT) & 0x3ff]);
   5410       1.134   thorpej #else
   5411       1.134   thorpej 		pte[l2pte_index(va)] =
   5412       1.134   thorpej 		    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
   5413       1.134   thorpej 		PTE_SYNC(&pte[l2pte_index(va)]);
   5414       1.134   thorpej #endif
   5415       1.130   thorpej 		va += PAGE_SIZE;
   5416       1.130   thorpej 		pa += PAGE_SIZE;
   5417       1.130   thorpej 		resid -= PAGE_SIZE;
   5418        1.43   thorpej 	}
   5419        1.43   thorpej #ifdef VERBOSE_INIT_ARM
   5420        1.43   thorpej 	printf("\n");
   5421        1.43   thorpej #endif
   5422        1.43   thorpej 	return (size);
   5423       1.135   thorpej }
   5424       1.135   thorpej 
   5425       1.135   thorpej /********************** Static device map routines ***************************/
   5426       1.135   thorpej 
   5427       1.135   thorpej static const struct pmap_devmap *pmap_devmap_table;
   5428       1.135   thorpej 
   5429       1.135   thorpej /*
   5430       1.136   thorpej  * Register the devmap table.  This is provided in case early console
   5431       1.136   thorpej  * initialization needs to register mappings created by bootstrap code
   5432       1.136   thorpej  * before pmap_devmap_bootstrap() is called.
   5433       1.136   thorpej  */
   5434       1.136   thorpej void
   5435       1.136   thorpej pmap_devmap_register(const struct pmap_devmap *table)
   5436       1.136   thorpej {
   5437       1.136   thorpej 
   5438       1.136   thorpej 	pmap_devmap_table = table;
   5439       1.136   thorpej }
   5440       1.136   thorpej 
   5441       1.136   thorpej /*
   5442       1.135   thorpej  * Map all of the static regions in the devmap table, and remember
   5443       1.135   thorpej  * the devmap table so other parts of the kernel can look up entries
   5444       1.135   thorpej  * later.
   5445       1.135   thorpej  */
   5446       1.135   thorpej void
   5447       1.135   thorpej pmap_devmap_bootstrap(vaddr_t l1pt, const struct pmap_devmap *table)
   5448       1.135   thorpej {
   5449       1.135   thorpej 	int i;
   5450       1.135   thorpej 
   5451       1.135   thorpej 	pmap_devmap_table = table;
   5452       1.135   thorpej 
   5453       1.135   thorpej 	for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
   5454       1.135   thorpej #ifdef VERBOSE_INIT_ARM
   5455       1.135   thorpej 		printf("devmap: %08lx -> %08lx @ %08lx\n",
   5456       1.135   thorpej 		    pmap_devmap_table[i].pd_pa,
   5457       1.135   thorpej 		    pmap_devmap_table[i].pd_pa +
   5458       1.135   thorpej 			pmap_devmap_table[i].pd_size - 1,
   5459       1.135   thorpej 		    pmap_devmap_table[i].pd_va);
   5460       1.135   thorpej #endif
   5461       1.135   thorpej 		pmap_map_chunk(l1pt, pmap_devmap_table[i].pd_va,
   5462       1.135   thorpej 		    pmap_devmap_table[i].pd_pa,
   5463       1.135   thorpej 		    pmap_devmap_table[i].pd_size,
   5464       1.135   thorpej 		    pmap_devmap_table[i].pd_prot,
   5465       1.135   thorpej 		    pmap_devmap_table[i].pd_cache);
   5466       1.135   thorpej 	}
   5467       1.135   thorpej }
   5468       1.135   thorpej 
   5469       1.135   thorpej const struct pmap_devmap *
   5470       1.135   thorpej pmap_devmap_find_pa(paddr_t pa, psize_t size)
   5471       1.135   thorpej {
   5472       1.153       scw 	uint64_t endpa;
   5473       1.135   thorpej 	int i;
   5474       1.135   thorpej 
   5475       1.135   thorpej 	if (pmap_devmap_table == NULL)
   5476       1.135   thorpej 		return (NULL);
   5477       1.135   thorpej 
   5478       1.158  christos 	endpa = (uint64_t)pa + (uint64_t)(size - 1);
   5479       1.153       scw 
   5480       1.135   thorpej 	for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
   5481       1.135   thorpej 		if (pa >= pmap_devmap_table[i].pd_pa &&
   5482       1.153       scw 		    endpa <= (uint64_t)pmap_devmap_table[i].pd_pa +
   5483       1.158  christos 			     (uint64_t)(pmap_devmap_table[i].pd_size - 1))
   5484       1.135   thorpej 			return (&pmap_devmap_table[i]);
   5485       1.135   thorpej 	}
   5486       1.135   thorpej 
   5487       1.135   thorpej 	return (NULL);
   5488       1.135   thorpej }
   5489       1.135   thorpej 
   5490       1.135   thorpej const struct pmap_devmap *
   5491       1.135   thorpej pmap_devmap_find_va(vaddr_t va, vsize_t size)
   5492       1.135   thorpej {
   5493       1.135   thorpej 	int i;
   5494       1.135   thorpej 
   5495       1.135   thorpej 	if (pmap_devmap_table == NULL)
   5496       1.135   thorpej 		return (NULL);
   5497       1.135   thorpej 
   5498       1.135   thorpej 	for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
   5499       1.135   thorpej 		if (va >= pmap_devmap_table[i].pd_va &&
   5500       1.158  christos 		    va + size - 1 <= pmap_devmap_table[i].pd_va +
   5501       1.158  christos 				     pmap_devmap_table[i].pd_size - 1)
   5502       1.135   thorpej 			return (&pmap_devmap_table[i]);
   5503       1.135   thorpej 	}
   5504       1.135   thorpej 
   5505       1.135   thorpej 	return (NULL);
   5506        1.40   thorpej }
   5507        1.85   thorpej 
   5508        1.85   thorpej /********************** PTE initialization routines **************************/
   5509        1.85   thorpej 
   5510        1.85   thorpej /*
   5511        1.85   thorpej  * These routines are called when the CPU type is identified to set up
   5512        1.85   thorpej  * the PTE prototypes, cache modes, etc.
   5513        1.85   thorpej  *
   5514        1.85   thorpej  * The variables are always here, just in case LKMs need to reference
   5515        1.85   thorpej  * them (though, they shouldn't).
   5516        1.85   thorpej  */
   5517        1.85   thorpej 
   5518        1.86   thorpej pt_entry_t	pte_l1_s_cache_mode;
   5519       1.134   thorpej pt_entry_t	pte_l1_s_cache_mode_pt;
   5520        1.86   thorpej pt_entry_t	pte_l1_s_cache_mask;
   5521        1.86   thorpej 
   5522        1.86   thorpej pt_entry_t	pte_l2_l_cache_mode;
   5523       1.134   thorpej pt_entry_t	pte_l2_l_cache_mode_pt;
   5524        1.86   thorpej pt_entry_t	pte_l2_l_cache_mask;
   5525        1.86   thorpej 
   5526        1.86   thorpej pt_entry_t	pte_l2_s_cache_mode;
   5527       1.134   thorpej pt_entry_t	pte_l2_s_cache_mode_pt;
   5528        1.86   thorpej pt_entry_t	pte_l2_s_cache_mask;
   5529        1.85   thorpej 
   5530        1.85   thorpej pt_entry_t	pte_l2_s_prot_u;
   5531        1.85   thorpej pt_entry_t	pte_l2_s_prot_w;
   5532        1.85   thorpej pt_entry_t	pte_l2_s_prot_mask;
   5533        1.85   thorpej 
   5534        1.85   thorpej pt_entry_t	pte_l1_s_proto;
   5535        1.85   thorpej pt_entry_t	pte_l1_c_proto;
   5536        1.85   thorpej pt_entry_t	pte_l2_s_proto;
   5537        1.85   thorpej 
   5538        1.88   thorpej void		(*pmap_copy_page_func)(paddr_t, paddr_t);
   5539        1.88   thorpej void		(*pmap_zero_page_func)(paddr_t);
   5540        1.88   thorpej 
   5541  1.164.12.2      matt #if (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0
   5542        1.85   thorpej void
   5543        1.85   thorpej pmap_pte_init_generic(void)
   5544        1.85   thorpej {
   5545        1.85   thorpej 
   5546        1.86   thorpej 	pte_l1_s_cache_mode = L1_S_B|L1_S_C;
   5547        1.86   thorpej 	pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
   5548        1.86   thorpej 
   5549        1.86   thorpej 	pte_l2_l_cache_mode = L2_B|L2_C;
   5550        1.86   thorpej 	pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
   5551        1.86   thorpej 
   5552        1.86   thorpej 	pte_l2_s_cache_mode = L2_B|L2_C;
   5553        1.86   thorpej 	pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
   5554        1.85   thorpej 
   5555       1.134   thorpej 	/*
   5556       1.134   thorpej 	 * If we have a write-through cache, set B and C.  If
   5557       1.134   thorpej 	 * we have a write-back cache, then we assume setting
   5558       1.134   thorpej 	 * only C will make those pages write-through.
   5559       1.134   thorpej 	 */
   5560       1.134   thorpej 	if (cpufuncs.cf_dcache_wb_range == (void *) cpufunc_nullop) {
   5561       1.134   thorpej 		pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
   5562       1.134   thorpej 		pte_l2_l_cache_mode_pt = L2_B|L2_C;
   5563       1.134   thorpej 		pte_l2_s_cache_mode_pt = L2_B|L2_C;
   5564       1.134   thorpej 	} else {
   5565  1.164.12.2      matt #if ARM_MMU_V6 > 1
   5566  1.164.12.2      matt 		pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C; /* arm116 errata 399234 */
   5567  1.164.12.2      matt 		pte_l2_l_cache_mode_pt = L2_B|L2_C; /* arm116 errata 399234 */
   5568  1.164.12.2      matt 		pte_l2_s_cache_mode_pt = L2_B|L2_C; /* arm116 errata 399234 */
   5569  1.164.12.2      matt #else
   5570       1.134   thorpej 		pte_l1_s_cache_mode_pt = L1_S_C;
   5571       1.134   thorpej 		pte_l2_l_cache_mode_pt = L2_C;
   5572       1.134   thorpej 		pte_l2_s_cache_mode_pt = L2_C;
   5573  1.164.12.2      matt #endif
   5574       1.134   thorpej 	}
   5575       1.134   thorpej 
   5576        1.85   thorpej 	pte_l2_s_prot_u = L2_S_PROT_U_generic;
   5577        1.85   thorpej 	pte_l2_s_prot_w = L2_S_PROT_W_generic;
   5578        1.85   thorpej 	pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
   5579        1.85   thorpej 
   5580        1.85   thorpej 	pte_l1_s_proto = L1_S_PROTO_generic;
   5581        1.85   thorpej 	pte_l1_c_proto = L1_C_PROTO_generic;
   5582        1.85   thorpej 	pte_l2_s_proto = L2_S_PROTO_generic;
   5583        1.88   thorpej 
   5584        1.88   thorpej 	pmap_copy_page_func = pmap_copy_page_generic;
   5585        1.88   thorpej 	pmap_zero_page_func = pmap_zero_page_generic;
   5586        1.85   thorpej }
   5587        1.85   thorpej 
   5588       1.131   thorpej #if defined(CPU_ARM8)
   5589       1.131   thorpej void
   5590       1.131   thorpej pmap_pte_init_arm8(void)
   5591       1.131   thorpej {
   5592       1.131   thorpej 
   5593       1.134   thorpej 	/*
   5594       1.134   thorpej 	 * ARM8 is compatible with generic, but we need to use
   5595       1.134   thorpej 	 * the page tables uncached.
   5596       1.134   thorpej 	 */
   5597       1.131   thorpej 	pmap_pte_init_generic();
   5598       1.134   thorpej 
   5599       1.134   thorpej 	pte_l1_s_cache_mode_pt = 0;
   5600       1.134   thorpej 	pte_l2_l_cache_mode_pt = 0;
   5601       1.134   thorpej 	pte_l2_s_cache_mode_pt = 0;
   5602       1.131   thorpej }
   5603       1.131   thorpej #endif /* CPU_ARM8 */
   5604       1.131   thorpej 
   5605       1.148       bsh #if defined(CPU_ARM9) && defined(ARM9_CACHE_WRITE_THROUGH)
   5606        1.85   thorpej void
   5607        1.85   thorpej pmap_pte_init_arm9(void)
   5608        1.85   thorpej {
   5609        1.85   thorpej 
   5610        1.85   thorpej 	/*
   5611        1.85   thorpej 	 * ARM9 is compatible with generic, but we want to use
   5612        1.85   thorpej 	 * write-through caching for now.
   5613        1.85   thorpej 	 */
   5614        1.85   thorpej 	pmap_pte_init_generic();
   5615        1.86   thorpej 
   5616        1.86   thorpej 	pte_l1_s_cache_mode = L1_S_C;
   5617        1.86   thorpej 	pte_l2_l_cache_mode = L2_C;
   5618        1.86   thorpej 	pte_l2_s_cache_mode = L2_C;
   5619       1.134   thorpej 
   5620       1.134   thorpej 	pte_l1_s_cache_mode_pt = L1_S_C;
   5621       1.134   thorpej 	pte_l2_l_cache_mode_pt = L2_C;
   5622       1.134   thorpej 	pte_l2_s_cache_mode_pt = L2_C;
   5623        1.85   thorpej }
   5624        1.85   thorpej #endif /* CPU_ARM9 */
   5625  1.164.12.2      matt #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1 + ARM_MMU_V6) != 0 */
   5626       1.138  rearnsha 
   5627       1.138  rearnsha #if defined(CPU_ARM10)
   5628       1.138  rearnsha void
   5629       1.138  rearnsha pmap_pte_init_arm10(void)
   5630       1.138  rearnsha {
   5631       1.138  rearnsha 
   5632       1.138  rearnsha 	/*
   5633       1.138  rearnsha 	 * ARM10 is compatible with generic, but we want to use
   5634       1.138  rearnsha 	 * write-through caching for now.
   5635       1.138  rearnsha 	 */
   5636       1.138  rearnsha 	pmap_pte_init_generic();
   5637       1.138  rearnsha 
   5638       1.138  rearnsha 	pte_l1_s_cache_mode = L1_S_B | L1_S_C;
   5639       1.138  rearnsha 	pte_l2_l_cache_mode = L2_B | L2_C;
   5640       1.138  rearnsha 	pte_l2_s_cache_mode = L2_B | L2_C;
   5641       1.138  rearnsha 
   5642       1.138  rearnsha 	pte_l1_s_cache_mode_pt = L1_S_C;
   5643       1.138  rearnsha 	pte_l2_l_cache_mode_pt = L2_C;
   5644       1.138  rearnsha 	pte_l2_s_cache_mode_pt = L2_C;
   5645       1.138  rearnsha 
   5646       1.138  rearnsha }
   5647       1.138  rearnsha #endif /* CPU_ARM10 */
   5648       1.131   thorpej 
   5649       1.131   thorpej #if ARM_MMU_SA1 == 1
   5650       1.131   thorpej void
   5651       1.131   thorpej pmap_pte_init_sa1(void)
   5652       1.131   thorpej {
   5653       1.131   thorpej 
   5654       1.134   thorpej 	/*
   5655       1.134   thorpej 	 * The StrongARM SA-1 cache does not have a write-through
   5656       1.134   thorpej 	 * mode.  So, do the generic initialization, then reset
   5657       1.134   thorpej 	 * the page table cache mode to B=1,C=1, and note that
   5658       1.134   thorpej 	 * the PTEs need to be sync'd.
   5659       1.134   thorpej 	 */
   5660       1.131   thorpej 	pmap_pte_init_generic();
   5661       1.134   thorpej 
   5662       1.134   thorpej 	pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
   5663       1.134   thorpej 	pte_l2_l_cache_mode_pt = L2_B|L2_C;
   5664       1.134   thorpej 	pte_l2_s_cache_mode_pt = L2_B|L2_C;
   5665       1.134   thorpej 
   5666       1.134   thorpej 	pmap_needs_pte_sync = 1;
   5667       1.131   thorpej }
   5668       1.134   thorpej #endif /* ARM_MMU_SA1 == 1*/
   5669        1.85   thorpej 
   5670        1.85   thorpej #if ARM_MMU_XSCALE == 1
   5671       1.141       scw #if (ARM_NMMUS > 1)
   5672       1.141       scw static u_int xscale_use_minidata;
   5673       1.141       scw #endif
   5674       1.141       scw 
   5675        1.85   thorpej void
   5676        1.85   thorpej pmap_pte_init_xscale(void)
   5677        1.85   thorpej {
   5678        1.96   thorpej 	uint32_t auxctl;
   5679       1.134   thorpej 	int write_through = 0;
   5680        1.85   thorpej 
   5681        1.96   thorpej 	pte_l1_s_cache_mode = L1_S_B|L1_S_C;
   5682        1.86   thorpej 	pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
   5683        1.86   thorpej 
   5684        1.96   thorpej 	pte_l2_l_cache_mode = L2_B|L2_C;
   5685        1.86   thorpej 	pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
   5686        1.86   thorpej 
   5687        1.96   thorpej 	pte_l2_s_cache_mode = L2_B|L2_C;
   5688        1.86   thorpej 	pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
   5689       1.106   thorpej 
   5690       1.134   thorpej 	pte_l1_s_cache_mode_pt = L1_S_C;
   5691       1.134   thorpej 	pte_l2_l_cache_mode_pt = L2_C;
   5692       1.134   thorpej 	pte_l2_s_cache_mode_pt = L2_C;
   5693       1.134   thorpej 
   5694       1.106   thorpej #ifdef XSCALE_CACHE_READ_WRITE_ALLOCATE
   5695       1.106   thorpej 	/*
   5696       1.106   thorpej 	 * The XScale core has an enhanced mode where writes that
   5697       1.106   thorpej 	 * miss the cache cause a cache line to be allocated.  This
   5698       1.106   thorpej 	 * is significantly faster than the traditional, write-through
   5699       1.106   thorpej 	 * behavior of this case.
   5700       1.106   thorpej 	 */
   5701       1.106   thorpej 	pte_l1_s_cache_mode |= L1_S_XSCALE_TEX(TEX_XSCALE_X);
   5702       1.106   thorpej 	pte_l2_l_cache_mode |= L2_XSCALE_L_TEX(TEX_XSCALE_X);
   5703       1.106   thorpej 	pte_l2_s_cache_mode |= L2_XSCALE_T_TEX(TEX_XSCALE_X);
   5704       1.106   thorpej #endif /* XSCALE_CACHE_READ_WRITE_ALLOCATE */
   5705        1.85   thorpej 
   5706        1.95   thorpej #ifdef XSCALE_CACHE_WRITE_THROUGH
   5707        1.95   thorpej 	/*
   5708        1.95   thorpej 	 * Some versions of the XScale core have various bugs in
   5709        1.95   thorpej 	 * their cache units, the work-around for which is to run
   5710        1.95   thorpej 	 * the cache in write-through mode.  Unfortunately, this
   5711        1.95   thorpej 	 * has a major (negative) impact on performance.  So, we
   5712        1.95   thorpej 	 * go ahead and run fast-and-loose, in the hopes that we
   5713        1.95   thorpej 	 * don't line up the planets in a way that will trip the
   5714        1.95   thorpej 	 * bugs.
   5715        1.95   thorpej 	 *
   5716        1.95   thorpej 	 * However, we give you the option to be slow-but-correct.
   5717        1.95   thorpej 	 */
   5718       1.129       bsh 	write_through = 1;
   5719       1.129       bsh #elif defined(XSCALE_CACHE_WRITE_BACK)
   5720       1.134   thorpej 	/* force write back cache mode */
   5721       1.129       bsh 	write_through = 0;
   5722       1.154       bsh #elif defined(CPU_XSCALE_PXA250) || defined(CPU_XSCALE_PXA270)
   5723       1.129       bsh 	/*
   5724       1.129       bsh 	 * Intel PXA2[15]0 processors are known to have a bug in
   5725       1.129       bsh 	 * write-back cache on revision 4 and earlier (stepping
   5726       1.129       bsh 	 * A[01] and B[012]).  Fixed for C0 and later.
   5727       1.129       bsh 	 */
   5728       1.129       bsh 	{
   5729       1.134   thorpej 		uint32_t id, type;
   5730       1.129       bsh 
   5731       1.129       bsh 		id = cpufunc_id();
   5732       1.129       bsh 		type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
   5733       1.129       bsh 
   5734       1.129       bsh 		if (type == CPU_ID_PXA250 || type == CPU_ID_PXA210) {
   5735       1.129       bsh 			if ((id & CPU_ID_REVISION_MASK) < 5) {
   5736       1.129       bsh 				/* write through for stepping A0-1 and B0-2 */
   5737       1.129       bsh 				write_through = 1;
   5738       1.129       bsh 			}
   5739       1.129       bsh 		}
   5740       1.129       bsh 	}
   5741        1.95   thorpej #endif /* XSCALE_CACHE_WRITE_THROUGH */
   5742       1.129       bsh 
   5743       1.129       bsh 	if (write_through) {
   5744       1.129       bsh 		pte_l1_s_cache_mode = L1_S_C;
   5745       1.129       bsh 		pte_l2_l_cache_mode = L2_C;
   5746       1.129       bsh 		pte_l2_s_cache_mode = L2_C;
   5747       1.129       bsh 	}
   5748        1.95   thorpej 
   5749       1.141       scw #if (ARM_NMMUS > 1)
   5750       1.141       scw 	xscale_use_minidata = 1;
   5751       1.141       scw #endif
   5752       1.141       scw 
   5753        1.85   thorpej 	pte_l2_s_prot_u = L2_S_PROT_U_xscale;
   5754        1.85   thorpej 	pte_l2_s_prot_w = L2_S_PROT_W_xscale;
   5755        1.85   thorpej 	pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
   5756        1.85   thorpej 
   5757        1.85   thorpej 	pte_l1_s_proto = L1_S_PROTO_xscale;
   5758        1.85   thorpej 	pte_l1_c_proto = L1_C_PROTO_xscale;
   5759        1.85   thorpej 	pte_l2_s_proto = L2_S_PROTO_xscale;
   5760        1.88   thorpej 
   5761        1.88   thorpej 	pmap_copy_page_func = pmap_copy_page_xscale;
   5762        1.88   thorpej 	pmap_zero_page_func = pmap_zero_page_xscale;
   5763        1.96   thorpej 
   5764        1.96   thorpej 	/*
   5765        1.96   thorpej 	 * Disable ECC protection of page table access, for now.
   5766        1.96   thorpej 	 */
   5767       1.157     perry 	__asm volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
   5768        1.96   thorpej 	auxctl &= ~XSCALE_AUXCTL_P;
   5769       1.157     perry 	__asm volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
   5770        1.85   thorpej }
   5771        1.87   thorpej 
   5772        1.87   thorpej /*
   5773        1.87   thorpej  * xscale_setup_minidata:
   5774        1.87   thorpej  *
   5775        1.87   thorpej  *	Set up the mini-data cache clean area.  We require the
   5776        1.87   thorpej  *	caller to allocate the right amount of physically and
   5777        1.87   thorpej  *	virtually contiguous space.
   5778        1.87   thorpej  */
   5779        1.87   thorpej void
   5780        1.87   thorpej xscale_setup_minidata(vaddr_t l1pt, vaddr_t va, paddr_t pa)
   5781        1.87   thorpej {
   5782        1.87   thorpej 	extern vaddr_t xscale_minidata_clean_addr;
   5783        1.87   thorpej 	extern vsize_t xscale_minidata_clean_size; /* already initialized */
   5784        1.87   thorpej 	pd_entry_t *pde = (pd_entry_t *) l1pt;
   5785        1.87   thorpej 	pt_entry_t *pte;
   5786        1.87   thorpej 	vsize_t size;
   5787        1.96   thorpej 	uint32_t auxctl;
   5788        1.87   thorpej 
   5789        1.87   thorpej 	xscale_minidata_clean_addr = va;
   5790        1.87   thorpej 
   5791        1.87   thorpej 	/* Round it to page size. */
   5792        1.87   thorpej 	size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
   5793        1.87   thorpej 
   5794        1.87   thorpej 	for (; size != 0;
   5795        1.87   thorpej 	     va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
   5796       1.134   thorpej #ifndef ARM32_NEW_VM_LAYOUT
   5797        1.87   thorpej 		pte = (pt_entry_t *)
   5798        1.87   thorpej 		    kernel_pt_lookup(pde[va >> L1_S_SHIFT] & L2_S_FRAME);
   5799       1.134   thorpej #else
   5800       1.134   thorpej 		pte = (pt_entry_t *) kernel_pt_lookup(
   5801       1.134   thorpej 		    pde[L1_IDX(va)] & L1_C_ADDR_MASK);
   5802       1.134   thorpej #endif
   5803        1.87   thorpej 		if (pte == NULL)
   5804        1.87   thorpej 			panic("xscale_setup_minidata: can't find L2 table for "
   5805        1.87   thorpej 			    "VA 0x%08lx", va);
   5806       1.134   thorpej #ifndef ARM32_NEW_VM_LAYOUT
   5807       1.134   thorpej 		pte[(va >> PGSHIFT) & 0x3ff] =
   5808       1.134   thorpej #else
   5809       1.134   thorpej 		pte[l2pte_index(va)] =
   5810       1.134   thorpej #endif
   5811       1.134   thorpej 		    L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
   5812        1.87   thorpej 		    L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);
   5813        1.87   thorpej 	}
   5814        1.96   thorpej 
   5815        1.96   thorpej 	/*
   5816        1.96   thorpej 	 * Configure the mini-data cache for write-back with
   5817        1.96   thorpej 	 * read/write-allocate.
   5818        1.96   thorpej 	 *
   5819        1.96   thorpej 	 * NOTE: In order to reconfigure the mini-data cache, we must
   5820        1.96   thorpej 	 * make sure it contains no valid data!  In order to do that,
   5821        1.96   thorpej 	 * we must issue a global data cache invalidate command!
   5822        1.96   thorpej 	 *
   5823        1.96   thorpej 	 * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
   5824        1.96   thorpej 	 * THIS IS VERY IMPORTANT!
   5825        1.96   thorpej 	 */
   5826       1.134   thorpej 
   5827        1.96   thorpej 	/* Invalidate data and mini-data. */
   5828       1.157     perry 	__asm volatile("mcr p15, 0, %0, c7, c6, 0" : : "r" (0));
   5829       1.157     perry 	__asm volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
   5830        1.96   thorpej 	auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
   5831       1.157     perry 	__asm volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
   5832        1.87   thorpej }
   5833       1.141       scw 
   5834       1.141       scw /*
   5835       1.141       scw  * Change the PTEs for the specified kernel mappings such that they
   5836       1.141       scw  * will use the mini data cache instead of the main data cache.
   5837       1.141       scw  */
   5838       1.141       scw void
   5839       1.141       scw pmap_uarea(vaddr_t va)
   5840       1.141       scw {
   5841       1.141       scw 	struct l2_bucket *l2b;
   5842       1.141       scw 	pt_entry_t *ptep, *sptep, pte;
   5843       1.141       scw 	vaddr_t next_bucket, eva;
   5844       1.141       scw 
   5845       1.141       scw #if (ARM_NMMUS > 1)
   5846       1.141       scw 	if (xscale_use_minidata == 0)
   5847       1.141       scw 		return;
   5848       1.141       scw #endif
   5849       1.141       scw 
   5850       1.141       scw 	eva = va + USPACE;
   5851       1.141       scw 
   5852       1.141       scw 	while (va < eva) {
   5853       1.141       scw 		next_bucket = L2_NEXT_BUCKET(va);
   5854       1.141       scw 		if (next_bucket > eva)
   5855       1.141       scw 			next_bucket = eva;
   5856       1.141       scw 
   5857       1.141       scw 		l2b = pmap_get_l2_bucket(pmap_kernel(), va);
   5858       1.141       scw 		KDASSERT(l2b != NULL);
   5859       1.141       scw 
   5860       1.141       scw 		sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
   5861       1.141       scw 
   5862       1.141       scw 		while (va < next_bucket) {
   5863       1.141       scw 			pte = *ptep;
   5864       1.141       scw 			if (!l2pte_minidata(pte)) {
   5865       1.141       scw 				cpu_dcache_wbinv_range(va, PAGE_SIZE);
   5866       1.141       scw 				cpu_tlb_flushD_SE(va);
   5867       1.141       scw 				*ptep = pte & ~L2_B;
   5868       1.141       scw 			}
   5869       1.141       scw 			ptep++;
   5870       1.141       scw 			va += PAGE_SIZE;
   5871       1.141       scw 		}
   5872       1.141       scw 		PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
   5873       1.141       scw 	}
   5874       1.141       scw 	cpu_cpwait();
   5875       1.141       scw }
   5876        1.85   thorpej #endif /* ARM_MMU_XSCALE == 1 */
   5877       1.134   thorpej 
   5878       1.134   thorpej #if defined(DDB)
   5879       1.134   thorpej /*
   5880       1.134   thorpej  * A couple of ddb-callable functions for dumping pmaps
   5881       1.134   thorpej  */
   5882       1.134   thorpej void pmap_dump_all(void);
   5883       1.134   thorpej void pmap_dump(pmap_t);
   5884       1.134   thorpej 
   5885       1.134   thorpej void
   5886       1.134   thorpej pmap_dump_all(void)
   5887       1.134   thorpej {
   5888       1.134   thorpej 	pmap_t pm;
   5889       1.134   thorpej 
   5890       1.134   thorpej 	LIST_FOREACH(pm, &pmap_pmaps, pm_list) {
   5891       1.134   thorpej 		if (pm == pmap_kernel())
   5892       1.134   thorpej 			continue;
   5893       1.134   thorpej 		pmap_dump(pm);
   5894       1.134   thorpej 		printf("\n");
   5895       1.134   thorpej 	}
   5896       1.134   thorpej }
   5897       1.134   thorpej 
   5898       1.134   thorpej static pt_entry_t ncptes[64];
   5899       1.134   thorpej static void pmap_dump_ncpg(pmap_t);
   5900       1.134   thorpej 
   5901       1.134   thorpej void
   5902       1.134   thorpej pmap_dump(pmap_t pm)
   5903       1.134   thorpej {
   5904       1.134   thorpej 	struct l2_dtable *l2;
   5905       1.134   thorpej 	struct l2_bucket *l2b;
   5906       1.134   thorpej 	pt_entry_t *ptep, pte;
   5907       1.134   thorpej 	vaddr_t l2_va, l2b_va, va;
   5908       1.134   thorpej 	int i, j, k, occ, rows = 0;
   5909       1.134   thorpej 
   5910       1.134   thorpej 	if (pm == pmap_kernel())
   5911       1.134   thorpej 		printf("pmap_kernel (%p): ", pm);
   5912       1.134   thorpej 	else
   5913       1.134   thorpej 		printf("user pmap (%p): ", pm);
   5914       1.134   thorpej 
   5915       1.134   thorpej 	printf("domain %d, l1 at %p\n", pm->pm_domain, pm->pm_l1->l1_kva);
   5916       1.134   thorpej 
   5917       1.134   thorpej 	l2_va = 0;
   5918       1.134   thorpej 	for (i = 0; i < L2_SIZE; i++, l2_va += 0x01000000) {
   5919       1.134   thorpej 		l2 = pm->pm_l2[i];
   5920       1.134   thorpej 
   5921       1.134   thorpej 		if (l2 == NULL || l2->l2_occupancy == 0)
   5922       1.134   thorpej 			continue;
   5923       1.134   thorpej 
   5924       1.134   thorpej 		l2b_va = l2_va;
   5925       1.134   thorpej 		for (j = 0; j < L2_BUCKET_SIZE; j++, l2b_va += 0x00100000) {
   5926       1.134   thorpej 			l2b = &l2->l2_bucket[j];
   5927       1.134   thorpej 
   5928       1.134   thorpej 			if (l2b->l2b_occupancy == 0 || l2b->l2b_kva == NULL)
   5929       1.134   thorpej 				continue;
   5930       1.134   thorpej 
   5931       1.134   thorpej 			ptep = l2b->l2b_kva;
   5932       1.134   thorpej 
   5933       1.134   thorpej 			for (k = 0; k < 256 && ptep[k] == 0; k++)
   5934       1.134   thorpej 				;
   5935       1.134   thorpej 
   5936       1.134   thorpej 			k &= ~63;
   5937       1.134   thorpej 			occ = l2b->l2b_occupancy;
   5938       1.134   thorpej 			va = l2b_va + (k * 4096);
   5939       1.134   thorpej 			for (; k < 256; k++, va += 0x1000) {
   5940       1.142     chris 				char ch = ' ';
   5941       1.134   thorpej 				if ((k % 64) == 0) {
   5942       1.134   thorpej 					if ((rows % 8) == 0) {
   5943       1.134   thorpej 						printf(
   5944       1.134   thorpej "          |0000   |8000   |10000  |18000  |20000  |28000  |30000  |38000\n");
   5945       1.134   thorpej 					}
   5946       1.134   thorpej 					printf("%08lx: ", va);
   5947       1.134   thorpej 				}
   5948       1.134   thorpej 
   5949       1.134   thorpej 				ncptes[k & 63] = 0;
   5950       1.134   thorpej 				pte = ptep[k];
   5951       1.134   thorpej 				if (pte == 0) {
   5952       1.134   thorpej 					ch = '.';
   5953       1.134   thorpej 				} else {
   5954       1.134   thorpej 					occ--;
   5955       1.134   thorpej 					switch (pte & 0x0c) {
   5956       1.134   thorpej 					case 0x00:
   5957       1.134   thorpej 						ch = 'D'; /* No cache No buff */
   5958       1.134   thorpej 						break;
   5959       1.134   thorpej 					case 0x04:
   5960       1.134   thorpej 						ch = 'B'; /* No cache buff */
   5961       1.134   thorpej 						break;
   5962       1.134   thorpej 					case 0x08:
   5963       1.141       scw 						if (pte & 0x40)
   5964       1.141       scw 							ch = 'm';
   5965       1.141       scw 						else
   5966       1.141       scw 						   ch = 'C'; /* Cache No buff */
   5967       1.134   thorpej 						break;
   5968       1.134   thorpej 					case 0x0c:
   5969       1.134   thorpej 						ch = 'F'; /* Cache Buff */
   5970       1.134   thorpej 						break;
   5971       1.134   thorpej 					}
   5972       1.134   thorpej 
   5973       1.134   thorpej 					if ((pte & L2_S_PROT_U) == L2_S_PROT_U)
   5974       1.134   thorpej 						ch += 0x20;
   5975       1.134   thorpej 
   5976       1.134   thorpej 					if ((pte & 0xc) == 0)
   5977       1.134   thorpej 						ncptes[k & 63] = pte;
   5978       1.134   thorpej 				}
   5979       1.134   thorpej 
   5980       1.134   thorpej 				if ((k % 64) == 63) {
   5981       1.134   thorpej 					rows++;
   5982       1.134   thorpej 					printf("%c\n", ch);
   5983       1.134   thorpej 					pmap_dump_ncpg(pm);
   5984       1.134   thorpej 					if (occ == 0)
   5985       1.134   thorpej 						break;
   5986       1.134   thorpej 				} else
   5987       1.134   thorpej 					printf("%c", ch);
   5988       1.134   thorpej 			}
   5989       1.134   thorpej 		}
   5990       1.134   thorpej 	}
   5991       1.134   thorpej }
   5992       1.134   thorpej 
   5993       1.134   thorpej static void
   5994       1.134   thorpej pmap_dump_ncpg(pmap_t pm)
   5995       1.134   thorpej {
   5996       1.134   thorpej 	struct vm_page *pg;
   5997       1.134   thorpej 	struct pv_entry *pv;
   5998       1.134   thorpej 	int i;
   5999       1.134   thorpej 
   6000       1.134   thorpej 	for (i = 0; i < 63; i++) {
   6001       1.134   thorpej 		if (ncptes[i] == 0)
   6002       1.134   thorpej 			continue;
   6003       1.134   thorpej 
   6004       1.134   thorpej 		pg = PHYS_TO_VM_PAGE(l2pte_pa(ncptes[i]));
   6005       1.134   thorpej 		if (pg == NULL)
   6006       1.134   thorpej 			continue;
   6007       1.134   thorpej 
   6008       1.134   thorpej 		printf(" pa 0x%08lx: krw %d kro %d urw %d uro %d\n",
   6009       1.155      yamt 		    VM_PAGE_TO_PHYS(pg),
   6010       1.134   thorpej 		    pg->mdpage.krw_mappings, pg->mdpage.kro_mappings,
   6011       1.134   thorpej 		    pg->mdpage.urw_mappings, pg->mdpage.uro_mappings);
   6012       1.134   thorpej 
   6013       1.134   thorpej 		for (pv = pg->mdpage.pvh_list; pv; pv = pv->pv_next) {
   6014       1.134   thorpej 			printf("   %c va 0x%08lx, flags 0x%x\n",
   6015       1.134   thorpej 			    (pm == pv->pv_pmap) ? '*' : ' ',
   6016       1.134   thorpej 			    pv->pv_va, pv->pv_flags);
   6017       1.134   thorpej 		}
   6018       1.134   thorpej 	}
   6019       1.134   thorpej }
   6020       1.134   thorpej #endif
   6021  1.164.12.2      matt 
   6022  1.164.12.2      matt #ifdef PMAP_STEAL_MEMORY
   6023  1.164.12.2      matt void
   6024  1.164.12.2      matt pmap_boot_pageadd(pv_addr_t *newpv)
   6025  1.164.12.2      matt {
   6026  1.164.12.2      matt 	pv_addr_t *pv, *npv;
   6027  1.164.12.2      matt 
   6028  1.164.12.2      matt 	if ((pv = SLIST_FIRST(&pmap_boot_freeq)) != NULL) {
   6029  1.164.12.2      matt 		if (newpv->pv_pa < pv->pv_va) {
   6030  1.164.12.2      matt 			KASSERT(newpv->pv_pa + newpv->pv_size <= pv->pv_pa);
   6031  1.164.12.2      matt 			if (newpv->pv_pa + newpv->pv_size == pv->pv_pa) {
   6032  1.164.12.2      matt 				newpv->pv_size += pv->pv_size;
   6033  1.164.12.2      matt 				SLIST_REMOVE_HEAD(&pmap_boot_freeq, pv_list);
   6034  1.164.12.2      matt 			}
   6035  1.164.12.2      matt 			pv = NULL;
   6036  1.164.12.2      matt 		} else {
   6037  1.164.12.2      matt 			for (; (npv = SLIST_NEXT(pv, pv_list)) != NULL;
   6038  1.164.12.2      matt 			     pv = npv) {
   6039  1.164.12.2      matt 				KASSERT(pv->pv_pa + pv->pv_size < npv->pv_pa);
   6040  1.164.12.2      matt 				KASSERT(pv->pv_pa < newpv->pv_pa);
   6041  1.164.12.2      matt 				if (newpv->pv_pa > npv->pv_pa)
   6042  1.164.12.2      matt 					continue;
   6043  1.164.12.2      matt 				if (pv->pv_pa + pv->pv_size == newpv->pv_pa) {
   6044  1.164.12.2      matt 					pv->pv_size += newpv->pv_size;
   6045  1.164.12.2      matt 					return;
   6046  1.164.12.2      matt 				}
   6047  1.164.12.2      matt 				if (newpv->pv_pa + newpv->pv_size < npv->pv_pa)
   6048  1.164.12.2      matt 					break;
   6049  1.164.12.2      matt 				newpv->pv_size += npv->pv_size;
   6050  1.164.12.2      matt 				SLIST_INSERT_AFTER(pv, newpv, pv_list);
   6051  1.164.12.2      matt 				SLIST_REMOVE_AFTER(newpv, pv_list);
   6052  1.164.12.2      matt 				return;
   6053  1.164.12.2      matt 			}
   6054  1.164.12.2      matt 		}
   6055  1.164.12.2      matt 	}
   6056  1.164.12.2      matt 
   6057  1.164.12.2      matt 	if (pv) {
   6058  1.164.12.2      matt 		SLIST_INSERT_AFTER(pv, newpv, pv_list);
   6059  1.164.12.2      matt 	} else {
   6060  1.164.12.2      matt 		SLIST_INSERT_HEAD(&pmap_boot_freeq, newpv, pv_list);
   6061  1.164.12.2      matt 	}
   6062  1.164.12.2      matt }
   6063  1.164.12.2      matt 
   6064  1.164.12.2      matt void
   6065  1.164.12.2      matt pmap_boot_pagealloc(psize_t amount, psize_t mask, psize_t match,
   6066  1.164.12.2      matt 	pv_addr_t *rpv)
   6067  1.164.12.2      matt {
   6068  1.164.12.2      matt 	pv_addr_t *pv, **pvp;
   6069  1.164.12.2      matt 	struct vm_physseg *ps;
   6070  1.164.12.2      matt 	size_t i;
   6071  1.164.12.2      matt 
   6072  1.164.12.2      matt 	KASSERT(amount & PGOFSET);
   6073  1.164.12.2      matt 	KASSERT((mask & PGOFSET) == 0);
   6074  1.164.12.2      matt 	KASSERT((match & PGOFSET) == 0);
   6075  1.164.12.2      matt 	KASSERT(amount != 0);
   6076  1.164.12.2      matt 
   6077  1.164.12.2      matt 	for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
   6078  1.164.12.2      matt 	     (pv = *pvp) != NULL;
   6079  1.164.12.2      matt 	     pvp = &SLIST_NEXT(pv, pv_list)) {
   6080  1.164.12.2      matt 		pv_addr_t *newpv;
   6081  1.164.12.2      matt 		psize_t off;
   6082  1.164.12.2      matt 		/*
   6083  1.164.12.2      matt 		 * If this entry is too small to satify the request...
   6084  1.164.12.2      matt 		 */
   6085  1.164.12.2      matt 		KASSERT(pv->pv_size > 0);
   6086  1.164.12.2      matt 		if (pv->pv_size < amount)
   6087  1.164.12.2      matt 			continue;
   6088  1.164.12.2      matt 
   6089  1.164.12.2      matt 		for (off = 0; off <= mask; off += PAGE_SIZE) {
   6090  1.164.12.2      matt 			if (((pv->pv_pa + off) & mask) == match
   6091  1.164.12.2      matt 			    && off + amount <= pv->pv_size)
   6092  1.164.12.2      matt 				break;
   6093  1.164.12.2      matt 		}
   6094  1.164.12.2      matt 		if (off > mask)
   6095  1.164.12.2      matt 			continue;
   6096  1.164.12.2      matt 
   6097  1.164.12.2      matt 		rpv->pv_va = pv->pv_va + off;
   6098  1.164.12.2      matt 		rpv->pv_pa = pv->pv_pa + off;
   6099  1.164.12.2      matt 		rpv->pv_size = amount;
   6100  1.164.12.2      matt 		pv->pv_size -= amount;
   6101  1.164.12.2      matt 		if (pv->pv_size == 0) {
   6102  1.164.12.2      matt 			KASSERT(off == 0);
   6103  1.164.12.2      matt 			KASSERT((vaddr_t) pv == rpv->pv_va);
   6104  1.164.12.2      matt 			*pvp = SLIST_NEXT(pv, pv_list);
   6105  1.164.12.2      matt 		} else if (off == 0) {
   6106  1.164.12.2      matt 			KASSERT((vaddr_t) pv == rpv->pv_va);
   6107  1.164.12.2      matt 			newpv = (pv_addr_t *) (rpv->pv_va + amount);
   6108  1.164.12.2      matt 			*newpv = *pv;
   6109  1.164.12.2      matt 			newpv->pv_pa += amount;
   6110  1.164.12.2      matt 			newpv->pv_va += amount;
   6111  1.164.12.2      matt 			*pvp = newpv;
   6112  1.164.12.2      matt 		} else if (off < pv->pv_size) {
   6113  1.164.12.2      matt 			newpv = (pv_addr_t *) (rpv->pv_va + amount);
   6114  1.164.12.2      matt 			*newpv = *pv;
   6115  1.164.12.2      matt 			newpv->pv_size -= off;
   6116  1.164.12.2      matt 			newpv->pv_pa += off + amount;
   6117  1.164.12.2      matt 			newpv->pv_va += off + amount;
   6118  1.164.12.2      matt 
   6119  1.164.12.2      matt 			SLIST_NEXT(pv, pv_list) = newpv;
   6120  1.164.12.2      matt 			pv->pv_size = off;
   6121  1.164.12.2      matt 		} else {
   6122  1.164.12.2      matt 			KASSERT((vaddr_t) pv != rpv->pv_va);
   6123  1.164.12.2      matt 		}
   6124  1.164.12.2      matt 		memset((void *)rpv->pv_va, 0, amount);
   6125  1.164.12.2      matt 		return;
   6126  1.164.12.2      matt 	}
   6127  1.164.12.2      matt 
   6128  1.164.12.2      matt 	if (vm_nphysseg == 0)
   6129  1.164.12.2      matt 		panic("pmap_boot_pagealloc: couldn't allocate memory");
   6130  1.164.12.2      matt 
   6131  1.164.12.2      matt 	for (pvp = &SLIST_FIRST(&pmap_boot_freeq);
   6132  1.164.12.2      matt 	     (pv = *pvp) != NULL;
   6133  1.164.12.2      matt 	     pvp = &SLIST_NEXT(pv, pv_list)) {
   6134  1.164.12.2      matt 		if (SLIST_NEXT(pv, pv_list) == NULL)
   6135  1.164.12.2      matt 			break;
   6136  1.164.12.2      matt 	}
   6137  1.164.12.2      matt 	KASSERT(mask == 0);
   6138  1.164.12.2      matt 	for (ps = vm_physmem, i = 0; i < vm_nphysseg; ps++, i++) {
   6139  1.164.12.2      matt 		if (ps->avail_start == atop(pv->pv_pa + pv->pv_size)
   6140  1.164.12.2      matt 		    && pv->pv_va + pv->pv_size <= ptoa(ps->avail_end)) {
   6141  1.164.12.2      matt 			rpv->pv_va = pv->pv_va;
   6142  1.164.12.2      matt 			rpv->pv_pa = pv->pv_pa;
   6143  1.164.12.2      matt 			rpv->pv_size = amount;
   6144  1.164.12.2      matt 			*pvp = NULL;
   6145  1.164.12.2      matt 			pmap_map_chunk(kernel_l1pt.pv_va,
   6146  1.164.12.2      matt 			     ptoa(ps->avail_start) + (pv->pv_va - pv->pv_pa),
   6147  1.164.12.2      matt 			     ptoa(ps->avail_start),
   6148  1.164.12.2      matt 			     amount - pv->pv_size,
   6149  1.164.12.2      matt 			     VM_PROT_READ|VM_PROT_WRITE,
   6150  1.164.12.2      matt 			     PTE_CACHE);
   6151  1.164.12.2      matt 			ps->avail_start += atop(amount - pv->pv_size);
   6152  1.164.12.2      matt 			/*
   6153  1.164.12.2      matt 			 * If we consumed the entire physseg, remove it.
   6154  1.164.12.2      matt 			 */
   6155  1.164.12.2      matt 			if (ps->avail_start == ps->avail_end) {
   6156  1.164.12.2      matt 				for (--vm_nphysseg; i < vm_nphysseg; i++, ps++)
   6157  1.164.12.2      matt 					ps[0] = ps[1];
   6158  1.164.12.2      matt 			}
   6159  1.164.12.2      matt 			memset((void *)rpv->pv_va, 0, rpv->pv_size);
   6160  1.164.12.2      matt 			return;
   6161  1.164.12.2      matt 		}
   6162  1.164.12.2      matt 	}
   6163  1.164.12.2      matt 
   6164  1.164.12.2      matt 	panic("pmap_boot_pagealloc: couldn't allocate memory");
   6165  1.164.12.2      matt }
   6166  1.164.12.2      matt 
   6167  1.164.12.2      matt vaddr_t
   6168  1.164.12.2      matt pmap_steal_memory(vsize_t size, vaddr_t *vstartp, vaddr_t *vendp)
   6169  1.164.12.2      matt {
   6170  1.164.12.2      matt 	pv_addr_t pv;
   6171  1.164.12.2      matt 
   6172  1.164.12.2      matt 	pmap_boot_pagealloc(size, 0, 0, &pv);
   6173  1.164.12.2      matt 
   6174  1.164.12.2      matt 	return pv.pv_va;
   6175  1.164.12.2      matt }
   6176  1.164.12.2      matt #endif /* PMAP_STEAL_MEMORY */
   6177