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