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