xref: /netbsd-src/sys/arch/xen/x86/xen_pmap.c (revision 2e2322c9c07009df921d11b1268f8506affbb8ba)
1 /*	$NetBSD: xen_pmap.c,v 1.24 2016/12/13 10:54:27 kamil Exp $	*/
2 
3 /*
4  * Copyright (c) 2007 Manuel Bouyer.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 /*
29  * Copyright (c) 2006 Mathieu Ropert <mro@adviseo.fr>
30  *
31  * Permission to use, copy, modify, and distribute this software for any
32  * purpose with or without fee is hereby granted, provided that the above
33  * copyright notice and this permission notice appear in all copies.
34  *
35  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
36  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
38  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
39  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
40  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
41  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
42  */
43 
44 /*
45  * Copyright (c) 1997 Charles D. Cranor and Washington University.
46  * All rights reserved.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  */
68 
69 /*
70  * Copyright 2001 (c) Wasabi Systems, Inc.
71  * All rights reserved.
72  *
73  * Written by Frank van der Linden for Wasabi Systems, Inc.
74  *
75  * Redistribution and use in source and binary forms, with or without
76  * modification, are permitted provided that the following conditions
77  * are met:
78  * 1. Redistributions of source code must retain the above copyright
79  *    notice, this list of conditions and the following disclaimer.
80  * 2. Redistributions in binary form must reproduce the above copyright
81  *    notice, this list of conditions and the following disclaimer in the
82  *    documentation and/or other materials provided with the distribution.
83  * 3. All advertising materials mentioning features or use of this software
84  *    must display the following acknowledgement:
85  *      This product includes software developed for the NetBSD Project by
86  *      Wasabi Systems, Inc.
87  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
88  *    or promote products derived from this software without specific prior
89  *    written permission.
90  *
91  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
92  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
93  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
95  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
96  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
97  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
98  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
99  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
100  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
101  * POSSIBILITY OF SUCH DAMAGE.
102  */
103 
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: xen_pmap.c,v 1.24 2016/12/13 10:54:27 kamil Exp $");
106 
107 #include "opt_user_ldt.h"
108 #include "opt_lockdebug.h"
109 #include "opt_multiprocessor.h"
110 #include "opt_xen.h"
111 
112 #include <sys/param.h>
113 #include <sys/systm.h>
114 #include <sys/proc.h>
115 #include <sys/pool.h>
116 #include <sys/kernel.h>
117 #include <sys/atomic.h>
118 #include <sys/cpu.h>
119 #include <sys/intr.h>
120 
121 #include <uvm/uvm.h>
122 
123 #include <dev/isa/isareg.h>
124 
125 #include <machine/specialreg.h>
126 #include <machine/gdt.h>
127 #include <machine/isa_machdep.h>
128 #include <machine/cpuvar.h>
129 
130 #include <x86/pmap.h>
131 #include <x86/pmap_pv.h>
132 
133 #include <x86/i82489reg.h>
134 #include <x86/i82489var.h>
135 
136 #include <xen/xen-public/xen.h>
137 #include <xen/hypervisor.h>
138 #include <xen/xenpmap.h>
139 
140 #define COUNT(x)	/* nothing */
141 
142 extern pd_entry_t * const normal_pdes[];
143 
144 extern paddr_t pmap_pa_start; /* PA of first physical page for this domain */
145 extern paddr_t pmap_pa_end;   /* PA of last physical page for this domain */
146 
147 int
148 pmap_enter(struct pmap *pmap, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
149 {
150         paddr_t ma;
151 
152 	if (__predict_false(pa < pmap_pa_start || pmap_pa_end <= pa)) {
153 		ma = pa; /* XXX hack */
154 	} else {
155 		ma = xpmap_ptom(pa);
156 	}
157 
158 	return pmap_enter_ma(pmap, va, ma, pa, prot, flags, DOMID_SELF);
159 }
160 
161 /*
162  * pmap_kenter_ma: enter a kernel mapping without R/M (pv_entry) tracking
163  *
164  * => no need to lock anything, assume va is already allocated
165  * => should be faster than normal pmap enter function
166  * => we expect a MACHINE address
167  */
168 
169 void
170 pmap_kenter_ma(vaddr_t va, paddr_t ma, vm_prot_t prot, u_int flags)
171 {
172 	pt_entry_t *pte, opte, npte;
173 
174 	if (va < VM_MIN_KERNEL_ADDRESS)
175 		pte = vtopte(va);
176 	else
177 		pte = kvtopte(va);
178 
179 	npte = ma | ((prot & VM_PROT_WRITE) ? PG_RW : PG_RO) |
180 	     PG_V | PG_k;
181 	if (flags & PMAP_NOCACHE)
182 		npte |= PG_N;
183 
184 	if ((cpu_feature[2] & CPUID_NOX) && !(prot & VM_PROT_EXECUTE))
185 		npte |= PG_NX;
186 
187 	opte = pmap_pte_testset (pte, npte); /* zap! */
188 
189 	if (pmap_valid_entry(opte)) {
190 #if defined(MULTIPROCESSOR)
191 		kpreempt_disable();
192 		pmap_tlb_shootdown(pmap_kernel(), va, opte, TLBSHOOT_KENTER);
193 		kpreempt_enable();
194 #else
195 		/* Don't bother deferring in the single CPU case. */
196 		pmap_update_pg(va);
197 #endif
198 	}
199 }
200 
201 /*
202  * pmap_extract_ma: extract a MA for the given VA
203  */
204 
205 bool
206 pmap_extract_ma(struct pmap *pmap, vaddr_t va, paddr_t *pap)
207 {
208 	pt_entry_t *ptes, pte;
209 	pd_entry_t pde;
210 	pd_entry_t * const *pdes;
211 	struct pmap *pmap2;
212 
213 	kpreempt_disable();
214 	pmap_map_ptes(pmap, &pmap2, &ptes, &pdes);
215 	if (!pmap_pdes_valid(va, pdes, &pde)) {
216 		pmap_unmap_ptes(pmap, pmap2);
217 		kpreempt_enable();
218 		return false;
219 	}
220 
221 	pte = ptes[pl1_i(va)];
222 	pmap_unmap_ptes(pmap, pmap2);
223 	kpreempt_enable();
224 
225 	if (__predict_true((pte & PG_V) != 0)) {
226 		if (pap != NULL)
227 			*pap = (pte & PG_FRAME) | (va & (NBPD_L1 - 1));
228 		return true;
229 	}
230 
231 	return false;
232 }
233 
234 /*
235  * Xen pmap's handlers for save/restore
236  */
237 void
238 pmap_xen_suspend(void)
239 {
240 	pmap_unmap_recursive_entries();
241 
242 	xpq_flush_queue();
243 }
244 
245 void
246 pmap_xen_resume(void)
247 {
248 	pmap_map_recursive_entries();
249 
250 	xpq_flush_queue();
251 }
252 
253 /*
254  * NetBSD uses L2 shadow pages to support PAE with Xen. However, Xen does not
255  * handle them correctly during save/restore, leading to incorrect page
256  * tracking and pinning during restore.
257  * For save/restore to succeed, two functions are introduced:
258  * - pmap_map_recursive_entries(), used by resume code to set the recursive
259  *   mapping entries to their correct value
260  * - pmap_unmap_recursive_entries(), used by suspend code to clear all
261  *   PDIR_SLOT_PTE entries
262  */
263 void
264 pmap_map_recursive_entries(void)
265 {
266 	int i;
267 	struct pmap *pm;
268 
269 	mutex_enter(&pmaps_lock);
270 	LIST_FOREACH(pm, &pmaps, pm_list) {
271 		for (i = 0; i < PDP_SIZE; i++) {
272 			xpq_queue_pte_update(
273 			    xpmap_ptom(pmap_pdirpa(pm, PDIR_SLOT_PTE + i)),
274 			    xpmap_ptom((pm)->pm_pdirpa[i]) | PG_V);
275 		}
276 	}
277 	mutex_exit(&pmaps_lock);
278 
279 	for (i = 0; i < PDP_SIZE; i++) {
280 		xpq_queue_pte_update(
281 		    xpmap_ptom(pmap_pdirpa(pmap_kernel(), PDIR_SLOT_PTE + i)),
282 		    xpmap_ptom(pmap_kernel()->pm_pdirpa[i]) | PG_V);
283 	}
284 }
285 
286 /*
287  * Unmap recursive entries found in pmaps. Required during Xen
288  * save/restore operations, as Xen does not handle recursive mappings
289  * properly.
290  */
291 void
292 pmap_unmap_recursive_entries(void)
293 {
294 	int i;
295 	struct pmap *pm;
296 
297 	/*
298 	 * Invalidate pmap_pdp_cache as it contains L2-pinned objects with
299 	 * recursive entries.
300 	 * XXX jym@ : find a way to drain per-CPU caches to. pool_cache_inv
301 	 * does not do that.
302 	 */
303 	pool_cache_invalidate(&pmap_pdp_cache);
304 
305 	mutex_enter(&pmaps_lock);
306 	LIST_FOREACH(pm, &pmaps, pm_list) {
307 		for (i = 0; i < PDP_SIZE; i++) {
308 			xpq_queue_pte_update(
309 			    xpmap_ptom(pmap_pdirpa(pm, PDIR_SLOT_PTE + i)), 0);
310 		}
311 	}
312 	mutex_exit(&pmaps_lock);
313 
314 	/* do it for pmap_kernel() too! */
315 	for (i = 0; i < PDP_SIZE; i++) {
316 		xpq_queue_pte_update(
317 		    xpmap_ptom(pmap_pdirpa(pmap_kernel(), PDIR_SLOT_PTE + i)),
318 		    0);
319 	}
320 }
321 
322 #if defined(PAE) || defined(__x86_64__)
323 
324 static __inline void
325 pmap_kpm_setpte(struct cpu_info *ci, struct pmap *pmap, int index)
326 {
327 	KASSERT(mutex_owned(pmap->pm_lock));
328 	KASSERT(mutex_owned(&ci->ci_kpm_mtx));
329 	if (pmap == pmap_kernel()) {
330 		KASSERT(index >= PDIR_SLOT_KERN);
331 	}
332 #ifdef PAE
333 	xpq_queue_pte_update(
334 		xpmap_ptetomach(&ci->ci_kpm_pdir[l2tol2(index)]),
335 		pmap->pm_pdir[index]);
336 #elif defined(__x86_64__)
337 	xpq_queue_pte_update(
338 		xpmap_ptetomach(&ci->ci_kpm_pdir[index]),
339 		pmap->pm_pdir[index]);
340 #endif /* PAE */
341 	xpq_flush_queue();
342 }
343 
344 /*
345  * Synchronise shadow pdir with the pmap on all cpus on which it is
346  * loaded.
347  */
348 void
349 xen_kpm_sync(struct pmap *pmap, int index)
350 {
351 	CPU_INFO_ITERATOR cii;
352 	struct cpu_info *ci;
353 
354 	KASSERT(pmap != NULL);
355 	KASSERT(kpreempt_disabled());
356 
357 	pmap_pte_flush();
358 
359 	for (CPU_INFO_FOREACH(cii, ci)) {
360 		if (ci == NULL) {
361 			continue;
362 		}
363 		cpuid_t cid = cpu_index(ci);
364 		if (pmap != pmap_kernel() &&
365 		    !kcpuset_isset(pmap->pm_xen_ptp_cpus, cid))
366 			continue;
367 
368 		/* take the lock and check again */
369 		mutex_enter(&ci->ci_kpm_mtx);
370 		if (pmap == pmap_kernel() ||
371 		    kcpuset_isset(pmap->pm_xen_ptp_cpus, cid)) {
372 			pmap_kpm_setpte(ci, pmap, index);
373 		}
374 		mutex_exit(&ci->ci_kpm_mtx);
375 	}
376 }
377 
378 #endif /* PAE || __x86_64__ */
379