Home | History | Annotate | Line # | Download | only in x86
xen_pmap.c revision 1.1.2.2
      1  1.1.2.2  rmind /*
      2  1.1.2.2  rmind  * Copyright (c) 2007 Manuel Bouyer.
      3  1.1.2.2  rmind  *
      4  1.1.2.2  rmind  * Redistribution and use in source and binary forms, with or without
      5  1.1.2.2  rmind  * modification, are permitted provided that the following conditions
      6  1.1.2.2  rmind  * are met:
      7  1.1.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
      8  1.1.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
      9  1.1.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     10  1.1.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     11  1.1.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     12  1.1.2.2  rmind  *
     13  1.1.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     14  1.1.2.2  rmind  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     15  1.1.2.2  rmind  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     16  1.1.2.2  rmind  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     17  1.1.2.2  rmind  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     18  1.1.2.2  rmind  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     19  1.1.2.2  rmind  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     20  1.1.2.2  rmind  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     21  1.1.2.2  rmind  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     22  1.1.2.2  rmind  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     23  1.1.2.2  rmind  *
     24  1.1.2.2  rmind  */
     25  1.1.2.2  rmind 
     26  1.1.2.2  rmind /*
     27  1.1.2.2  rmind  * Copyright (c) 2006 Mathieu Ropert <mro (at) adviseo.fr>
     28  1.1.2.2  rmind  *
     29  1.1.2.2  rmind  * Permission to use, copy, modify, and distribute this software for any
     30  1.1.2.2  rmind  * purpose with or without fee is hereby granted, provided that the above
     31  1.1.2.2  rmind  * copyright notice and this permission notice appear in all copies.
     32  1.1.2.2  rmind  *
     33  1.1.2.2  rmind  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     34  1.1.2.2  rmind  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     35  1.1.2.2  rmind  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     36  1.1.2.2  rmind  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     37  1.1.2.2  rmind  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     38  1.1.2.2  rmind  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     39  1.1.2.2  rmind  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     40  1.1.2.2  rmind  */
     41  1.1.2.2  rmind 
     42  1.1.2.2  rmind /*
     43  1.1.2.2  rmind  *
     44  1.1.2.2  rmind  * Copyright (c) 1997 Charles D. Cranor and Washington University.
     45  1.1.2.2  rmind  * All rights reserved.
     46  1.1.2.2  rmind  *
     47  1.1.2.2  rmind  * Redistribution and use in source and binary forms, with or without
     48  1.1.2.2  rmind  * modification, are permitted provided that the following conditions
     49  1.1.2.2  rmind  * are met:
     50  1.1.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
     51  1.1.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
     52  1.1.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     53  1.1.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     54  1.1.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     55  1.1.2.2  rmind  * 3. All advertising materials mentioning features or use of this software
     56  1.1.2.2  rmind  *    must display the following acknowledgement:
     57  1.1.2.2  rmind  *      This product includes software developed by Charles D. Cranor and
     58  1.1.2.2  rmind  *      Washington University.
     59  1.1.2.2  rmind  * 4. The name of the author may not be used to endorse or promote products
     60  1.1.2.2  rmind  *    derived from this software without specific prior written permission.
     61  1.1.2.2  rmind  *
     62  1.1.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     63  1.1.2.2  rmind  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     64  1.1.2.2  rmind  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     65  1.1.2.2  rmind  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     66  1.1.2.2  rmind  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     67  1.1.2.2  rmind  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     68  1.1.2.2  rmind  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     69  1.1.2.2  rmind  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     70  1.1.2.2  rmind  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     71  1.1.2.2  rmind  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     72  1.1.2.2  rmind  */
     73  1.1.2.2  rmind 
     74  1.1.2.2  rmind /*
     75  1.1.2.2  rmind  * Copyright 2001 (c) Wasabi Systems, Inc.
     76  1.1.2.2  rmind  * All rights reserved.
     77  1.1.2.2  rmind  *
     78  1.1.2.2  rmind  * Written by Frank van der Linden for Wasabi Systems, Inc.
     79  1.1.2.2  rmind  *
     80  1.1.2.2  rmind  * Redistribution and use in source and binary forms, with or without
     81  1.1.2.2  rmind  * modification, are permitted provided that the following conditions
     82  1.1.2.2  rmind  * are met:
     83  1.1.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
     84  1.1.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
     85  1.1.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     86  1.1.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     87  1.1.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     88  1.1.2.2  rmind  * 3. All advertising materials mentioning features or use of this software
     89  1.1.2.2  rmind  *    must display the following acknowledgement:
     90  1.1.2.2  rmind  *      This product includes software developed for the NetBSD Project by
     91  1.1.2.2  rmind  *      Wasabi Systems, Inc.
     92  1.1.2.2  rmind  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     93  1.1.2.2  rmind  *    or promote products derived from this software without specific prior
     94  1.1.2.2  rmind  *    written permission.
     95  1.1.2.2  rmind  *
     96  1.1.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     97  1.1.2.2  rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     98  1.1.2.2  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     99  1.1.2.2  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
    100  1.1.2.2  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    101  1.1.2.2  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    102  1.1.2.2  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    103  1.1.2.2  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    104  1.1.2.2  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    105  1.1.2.2  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    106  1.1.2.2  rmind  * POSSIBILITY OF SUCH DAMAGE.
    107  1.1.2.2  rmind  */
    108  1.1.2.2  rmind 
    109  1.1.2.2  rmind #include <sys/cdefs.h>
    110  1.1.2.2  rmind __KERNEL_RCSID(0, "$NetBSD: xen_pmap.c,v 1.1.2.2 2010/05/30 05:17:14 rmind Exp $");
    111  1.1.2.2  rmind 
    112  1.1.2.2  rmind #include "opt_user_ldt.h"
    113  1.1.2.2  rmind #include "opt_lockdebug.h"
    114  1.1.2.2  rmind #include "opt_multiprocessor.h"
    115  1.1.2.2  rmind #include "opt_xen.h"
    116  1.1.2.2  rmind #if !defined(__x86_64__)
    117  1.1.2.2  rmind #include "opt_kstack_dr0.h"
    118  1.1.2.2  rmind #endif /* !defined(__x86_64__) */
    119  1.1.2.2  rmind 
    120  1.1.2.2  rmind #include <sys/param.h>
    121  1.1.2.2  rmind #include <sys/systm.h>
    122  1.1.2.2  rmind #include <sys/proc.h>
    123  1.1.2.2  rmind #include <sys/pool.h>
    124  1.1.2.2  rmind #include <sys/kernel.h>
    125  1.1.2.2  rmind #include <sys/atomic.h>
    126  1.1.2.2  rmind #include <sys/cpu.h>
    127  1.1.2.2  rmind #include <sys/intr.h>
    128  1.1.2.2  rmind #include <sys/xcall.h>
    129  1.1.2.2  rmind 
    130  1.1.2.2  rmind #include <uvm/uvm.h>
    131  1.1.2.2  rmind 
    132  1.1.2.2  rmind #include <dev/isa/isareg.h>
    133  1.1.2.2  rmind 
    134  1.1.2.2  rmind #include <machine/specialreg.h>
    135  1.1.2.2  rmind #include <machine/gdt.h>
    136  1.1.2.2  rmind #include <machine/isa_machdep.h>
    137  1.1.2.2  rmind #include <machine/cpuvar.h>
    138  1.1.2.2  rmind 
    139  1.1.2.2  rmind #include <x86/pmap.h>
    140  1.1.2.2  rmind #include <x86/pmap_pv.h>
    141  1.1.2.2  rmind 
    142  1.1.2.2  rmind #include <x86/i82489reg.h>
    143  1.1.2.2  rmind #include <x86/i82489var.h>
    144  1.1.2.2  rmind 
    145  1.1.2.2  rmind #ifdef XEN
    146  1.1.2.2  rmind #include <xen/xen3-public/xen.h>
    147  1.1.2.2  rmind #include <xen/hypervisor.h>
    148  1.1.2.2  rmind #endif
    149  1.1.2.2  rmind 
    150  1.1.2.2  rmind /* flag to be used for kernel mappings: PG_u on Xen/amd64, 0 otherwise */
    151  1.1.2.2  rmind #if defined(XEN) && defined(__x86_64__)
    152  1.1.2.2  rmind #define PG_k PG_u
    153  1.1.2.2  rmind #else
    154  1.1.2.2  rmind #define PG_k 0
    155  1.1.2.2  rmind #endif
    156  1.1.2.2  rmind 
    157  1.1.2.2  rmind extern paddr_t pmap_pa_start; /* PA of first physical page for this domain */
    158  1.1.2.2  rmind extern paddr_t pmap_pa_end;   /* PA of last physical page for this domain */
    159  1.1.2.2  rmind 
    160  1.1.2.2  rmind int
    161  1.1.2.2  rmind pmap_enter(struct pmap *pmap, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
    162  1.1.2.2  rmind {
    163  1.1.2.2  rmind         paddr_t ma;
    164  1.1.2.2  rmind 
    165  1.1.2.2  rmind 	if (__predict_false(pa < pmap_pa_start || pmap_pa_end <= pa)) {
    166  1.1.2.2  rmind 		ma = pa; /* XXX hack */
    167  1.1.2.2  rmind 	} else {
    168  1.1.2.2  rmind 		ma = xpmap_ptom(pa);
    169  1.1.2.2  rmind 	}
    170  1.1.2.2  rmind 
    171  1.1.2.2  rmind 	return pmap_enter_ma(pmap, va, ma, pa, prot, flags, DOMID_SELF);
    172  1.1.2.2  rmind }
    173  1.1.2.2  rmind 
    174  1.1.2.2  rmind /*
    175  1.1.2.2  rmind  * pmap_kenter_ma: enter a kernel mapping without R/M (pv_entry) tracking
    176  1.1.2.2  rmind  *
    177  1.1.2.2  rmind  * => no need to lock anything, assume va is already allocated
    178  1.1.2.2  rmind  * => should be faster than normal pmap enter function
    179  1.1.2.2  rmind  * => we expect a MACHINE address
    180  1.1.2.2  rmind  */
    181  1.1.2.2  rmind 
    182  1.1.2.2  rmind void
    183  1.1.2.2  rmind pmap_kenter_ma(vaddr_t va, paddr_t ma, vm_prot_t prot, u_int flags)
    184  1.1.2.2  rmind {
    185  1.1.2.2  rmind 	pt_entry_t *pte, opte, npte;
    186  1.1.2.2  rmind 
    187  1.1.2.2  rmind 	if (va < VM_MIN_KERNEL_ADDRESS)
    188  1.1.2.2  rmind 		pte = vtopte(va);
    189  1.1.2.2  rmind 	else
    190  1.1.2.2  rmind 		pte = kvtopte(va);
    191  1.1.2.2  rmind 
    192  1.1.2.2  rmind 	npte = ma | ((prot & VM_PROT_WRITE) ? PG_RW : PG_RO) |
    193  1.1.2.2  rmind 	     PG_V | PG_k;
    194  1.1.2.2  rmind 	if (flags & PMAP_NOCACHE)
    195  1.1.2.2  rmind 		npte |= PG_N;
    196  1.1.2.2  rmind 
    197  1.1.2.2  rmind 	if ((cpu_feature[2] & CPUID_NOX) && !(prot & VM_PROT_EXECUTE))
    198  1.1.2.2  rmind 		npte |= PG_NX;
    199  1.1.2.2  rmind 
    200  1.1.2.2  rmind 	opte = pmap_pte_testset (pte, npte); /* zap! */
    201  1.1.2.2  rmind 
    202  1.1.2.2  rmind 	if (pmap_valid_entry(opte)) {
    203  1.1.2.2  rmind #if defined(MULTIPROCESSOR)
    204  1.1.2.2  rmind 		kpreempt_disable();
    205  1.1.2.2  rmind 		pmap_tlb_shootdown(pmap_kernel(), va, opte, TLBSHOOT_KENTER);
    206  1.1.2.2  rmind 		kpreempt_enable();
    207  1.1.2.2  rmind #else
    208  1.1.2.2  rmind 		/* Don't bother deferring in the single CPU case. */
    209  1.1.2.2  rmind 		pmap_update_pg(va);
    210  1.1.2.2  rmind #endif
    211  1.1.2.2  rmind 	}
    212  1.1.2.2  rmind }
    213  1.1.2.2  rmind 
    214  1.1.2.2  rmind /*
    215  1.1.2.2  rmind  * pmap_extract_ma: extract a MA for the given VA
    216  1.1.2.2  rmind  */
    217  1.1.2.2  rmind 
    218  1.1.2.2  rmind bool
    219  1.1.2.2  rmind pmap_extract_ma(struct pmap *pmap, vaddr_t va, paddr_t *pap)
    220  1.1.2.2  rmind {
    221  1.1.2.2  rmind 	pt_entry_t *ptes, pte;
    222  1.1.2.2  rmind 	pd_entry_t pde;
    223  1.1.2.2  rmind 	pd_entry_t * const *pdes;
    224  1.1.2.2  rmind 	struct pmap *pmap2;
    225  1.1.2.2  rmind 
    226  1.1.2.2  rmind 	kpreempt_disable();
    227  1.1.2.2  rmind 	pmap_map_ptes(pmap, &pmap2, &ptes, &pdes);
    228  1.1.2.2  rmind 	if (!pmap_pdes_valid(va, pdes, &pde)) {
    229  1.1.2.2  rmind 		pmap_unmap_ptes(pmap, pmap2);
    230  1.1.2.2  rmind 		kpreempt_enable();
    231  1.1.2.2  rmind 		return false;
    232  1.1.2.2  rmind 	}
    233  1.1.2.2  rmind 
    234  1.1.2.2  rmind 	pte = ptes[pl1_i(va)];
    235  1.1.2.2  rmind 	pmap_unmap_ptes(pmap, pmap2);
    236  1.1.2.2  rmind 	kpreempt_enable();
    237  1.1.2.2  rmind 
    238  1.1.2.2  rmind 	if (__predict_true((pte & PG_V) != 0)) {
    239  1.1.2.2  rmind 		if (pap != NULL)
    240  1.1.2.2  rmind 			*pap = (pte & PG_FRAME) | (va & (NBPD_L1 - 1));
    241  1.1.2.2  rmind 		return true;
    242  1.1.2.2  rmind 	}
    243  1.1.2.2  rmind 
    244  1.1.2.2  rmind 	return false;
    245  1.1.2.2  rmind }
    246