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