ia64/xen-unstable

view xen/arch/x86/mm/shadow/multi.c @ 17906:6563ea38e002

Out-of-sync L1 shadows: always unsync pages on guest writes.

Signed-off-by: Gianluca Guida <gianluca.guida@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jun 20 18:41:50 2008 +0100 (2008-06-20)
parents 7b66b4e9f743
children 1e9df5cb885f
line source
1 /******************************************************************************
2 * arch/x86/mm/shadow/multi.c
3 *
4 * Simple, mostly-synchronous shadow page tables.
5 * Parts of this code are Copyright (c) 2006 by XenSource Inc.
6 * Parts of this code are Copyright (c) 2006 by Michael A Fetterman
7 * Parts based on earlier work by Michael A Fetterman, Ian Pratt et al.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
24 #include <xen/config.h>
25 #include <xen/types.h>
26 #include <xen/mm.h>
27 #include <xen/trace.h>
28 #include <xen/sched.h>
29 #include <xen/perfc.h>
30 #include <xen/domain_page.h>
31 #include <asm/page.h>
32 #include <asm/current.h>
33 #include <asm/shadow.h>
34 #include <asm/flushtlb.h>
35 #include <asm/hvm/hvm.h>
36 #include <asm/hvm/cacheattr.h>
37 #include <asm/mtrr.h>
38 #include "private.h"
39 #include "types.h"
41 /* THINGS TO DO LATER:
42 *
43 * TEARDOWN HEURISTICS
44 * Also: have a heuristic for when to destroy a previous paging-mode's
45 * shadows. When a guest is done with its start-of-day 32-bit tables
46 * and reuses the memory we want to drop those shadows. Start with
47 * shadows in a page in two modes as a hint, but beware of clever tricks
48 * like reusing a pagetable for both PAE and 64-bit during boot...
49 *
50 * PAE LINEAR MAPS
51 * Rework shadow_get_l*e() to have the option of using map_domain_page()
52 * instead of linear maps. Add appropriate unmap_l*e calls in the users.
53 * Then we can test the speed difference made by linear maps. If the
54 * map_domain_page() version is OK on PAE, we could maybe allow a lightweight
55 * l3-and-l2h-only shadow mode for PAE PV guests that would allow them
56 * to share l2h pages again.
57 *
58 * PSE disabled / PSE36
59 * We don't support any modes other than PSE enabled, PSE36 disabled.
60 * Neither of those would be hard to change, but we'd need to be able to
61 * deal with shadows made in one mode and used in another.
62 */
64 #define FETCH_TYPE_PREFETCH 1
65 #define FETCH_TYPE_DEMAND 2
66 #define FETCH_TYPE_WRITE 4
67 typedef enum {
68 ft_prefetch = FETCH_TYPE_PREFETCH,
69 ft_demand_read = FETCH_TYPE_DEMAND,
70 ft_demand_write = FETCH_TYPE_DEMAND | FETCH_TYPE_WRITE,
71 } fetch_type_t;
73 #ifdef DEBUG_TRACE_DUMP
74 static char *fetch_type_names[] = {
75 [ft_prefetch] "prefetch",
76 [ft_demand_read] "demand read",
77 [ft_demand_write] "demand write",
78 };
79 #endif
81 /**************************************************************************/
82 /* Hash table mapping from guest pagetables to shadows
83 *
84 * Normal case: maps the mfn of a guest page to the mfn of its shadow page.
85 * FL1's: maps the *gfn* of the start of a superpage to the mfn of a
86 * shadow L1 which maps its "splinters".
87 */
89 static inline mfn_t
90 get_fl1_shadow_status(struct vcpu *v, gfn_t gfn)
91 /* Look for FL1 shadows in the hash table */
92 {
93 mfn_t smfn = shadow_hash_lookup(v, gfn_x(gfn), SH_type_fl1_shadow);
94 return smfn;
95 }
97 static inline mfn_t
98 get_shadow_status(struct vcpu *v, mfn_t gmfn, u32 shadow_type)
99 /* Look for shadows in the hash table */
100 {
101 mfn_t smfn = shadow_hash_lookup(v, mfn_x(gmfn), shadow_type);
102 perfc_incr(shadow_get_shadow_status);
103 return smfn;
104 }
106 static inline void
107 set_fl1_shadow_status(struct vcpu *v, gfn_t gfn, mfn_t smfn)
108 /* Put an FL1 shadow into the hash table */
109 {
110 SHADOW_PRINTK("gfn=%"SH_PRI_gfn", type=%08x, smfn=%05lx\n",
111 gfn_x(gfn), SH_type_fl1_shadow, mfn_x(smfn));
113 shadow_hash_insert(v, gfn_x(gfn), SH_type_fl1_shadow, smfn);
114 }
116 static inline void
117 set_shadow_status(struct vcpu *v, mfn_t gmfn, u32 shadow_type, mfn_t smfn)
118 /* Put a shadow into the hash table */
119 {
120 struct domain *d = v->domain;
121 int res;
123 SHADOW_PRINTK("d=%d, v=%d, gmfn=%05lx, type=%08x, smfn=%05lx\n",
124 d->domain_id, v->vcpu_id, mfn_x(gmfn),
125 shadow_type, mfn_x(smfn));
127 /* 32-on-64 PV guests don't own their l4 pages so can't get_page them */
128 if ( !is_pv_32on64_vcpu(v) || shadow_type != SH_type_l4_64_shadow )
129 {
130 res = get_page(mfn_to_page(gmfn), d);
131 ASSERT(res == 1);
132 }
134 shadow_hash_insert(v, mfn_x(gmfn), shadow_type, smfn);
135 }
137 static inline void
138 delete_fl1_shadow_status(struct vcpu *v, gfn_t gfn, mfn_t smfn)
139 /* Remove a shadow from the hash table */
140 {
141 SHADOW_PRINTK("gfn=%"SH_PRI_gfn", type=%08x, smfn=%05lx\n",
142 gfn_x(gfn), SH_type_fl1_shadow, mfn_x(smfn));
143 shadow_hash_delete(v, gfn_x(gfn), SH_type_fl1_shadow, smfn);
144 }
146 static inline void
147 delete_shadow_status(struct vcpu *v, mfn_t gmfn, u32 shadow_type, mfn_t smfn)
148 /* Remove a shadow from the hash table */
149 {
150 SHADOW_PRINTK("d=%d, v=%d, gmfn=%05lx, type=%08x, smfn=%05lx\n",
151 v->domain->domain_id, v->vcpu_id,
152 mfn_x(gmfn), shadow_type, mfn_x(smfn));
153 shadow_hash_delete(v, mfn_x(gmfn), shadow_type, smfn);
154 /* 32-on-64 PV guests don't own their l4 pages; see set_shadow_status */
155 if ( !is_pv_32on64_vcpu(v) || shadow_type != SH_type_l4_64_shadow )
156 put_page(mfn_to_page(gmfn));
157 }
159 /**************************************************************************/
160 /* CPU feature support querying */
162 static inline int
163 guest_supports_superpages(struct vcpu *v)
164 {
165 /* The _PAGE_PSE bit must be honoured in HVM guests, whenever
166 * CR4.PSE is set or the guest is in PAE or long mode.
167 * It's also used in the dummy PT for vcpus with CR4.PG cleared. */
168 return (is_hvm_vcpu(v) &&
169 (GUEST_PAGING_LEVELS != 2
170 || !hvm_paging_enabled(v)
171 || (v->arch.hvm_vcpu.guest_cr[4] & X86_CR4_PSE)));
172 }
174 static inline int
175 guest_supports_nx(struct vcpu *v)
176 {
177 if ( GUEST_PAGING_LEVELS == 2 || !cpu_has_nx )
178 return 0;
179 if ( !is_hvm_vcpu(v) )
180 return cpu_has_nx;
181 return hvm_nx_enabled(v);
182 }
185 /**************************************************************************/
186 /* Functions for walking the guest page tables */
188 /* Flags that are needed in a pagetable entry, with the sense of NX inverted */
189 static uint32_t mandatory_flags(struct vcpu *v, uint32_t pfec)
190 {
191 static uint32_t flags[] = {
192 /* I/F - Usr Wr */
193 /* 0 0 0 0 */ _PAGE_PRESENT,
194 /* 0 0 0 1 */ _PAGE_PRESENT|_PAGE_RW,
195 /* 0 0 1 0 */ _PAGE_PRESENT|_PAGE_USER,
196 /* 0 0 1 1 */ _PAGE_PRESENT|_PAGE_RW|_PAGE_USER,
197 /* 0 1 0 0 */ _PAGE_PRESENT,
198 /* 0 1 0 1 */ _PAGE_PRESENT|_PAGE_RW,
199 /* 0 1 1 0 */ _PAGE_PRESENT|_PAGE_USER,
200 /* 0 1 1 1 */ _PAGE_PRESENT|_PAGE_RW|_PAGE_USER,
201 /* 1 0 0 0 */ _PAGE_PRESENT|_PAGE_NX_BIT,
202 /* 1 0 0 1 */ _PAGE_PRESENT|_PAGE_RW|_PAGE_NX_BIT,
203 /* 1 0 1 0 */ _PAGE_PRESENT|_PAGE_USER|_PAGE_NX_BIT,
204 /* 1 0 1 1 */ _PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX_BIT,
205 /* 1 1 0 0 */ _PAGE_PRESENT|_PAGE_NX_BIT,
206 /* 1 1 0 1 */ _PAGE_PRESENT|_PAGE_RW|_PAGE_NX_BIT,
207 /* 1 1 1 0 */ _PAGE_PRESENT|_PAGE_USER|_PAGE_NX_BIT,
208 /* 1 1 1 1 */ _PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX_BIT,
209 };
211 /* Don't demand not-NX if the CPU wouldn't enforce it. */
212 if ( !guest_supports_nx(v) )
213 pfec &= ~PFEC_insn_fetch;
215 /* Don't demand R/W if the CPU wouldn't enforce it. */
216 if ( is_hvm_vcpu(v) && unlikely(!hvm_wp_enabled(v))
217 && !(pfec & PFEC_user_mode) )
218 pfec &= ~PFEC_write_access;
220 return flags[(pfec & 0x1f) >> 1];
221 }
223 /* Modify a guest pagetable entry to set the Accessed and Dirty bits.
224 * Returns non-zero if it actually writes to guest memory. */
225 static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty)
226 {
227 guest_intpte_t old, new;
229 old = *(guest_intpte_t *)walk_p;
230 new = old | _PAGE_ACCESSED | (set_dirty ? _PAGE_DIRTY : 0);
231 if ( old != new )
232 {
233 /* Write the new entry into the walk, and try to write it back
234 * into the guest table as well. If the guest table has changed
235 * under out feet then leave it alone. */
236 *(guest_intpte_t *)walk_p = new;
237 if ( cmpxchg(((guest_intpte_t *)guest_p), old, new) == old )
238 return 1;
239 }
240 return 0;
241 }
243 /* This validation is called with lock held, and after write permission
244 * removal. Then check is atomic and no more inconsistent content can
245 * be observed before lock is released
246 *
247 * Return 1 to indicate success and 0 for inconsistency
248 */
249 static inline uint32_t
250 shadow_check_gwalk(struct vcpu *v, unsigned long va, walk_t *gw)
251 {
252 struct domain *d = v->domain;
253 guest_l1e_t *l1p;
254 guest_l2e_t *l2p;
255 #if GUEST_PAGING_LEVELS >= 4
256 guest_l3e_t *l3p;
257 guest_l4e_t *l4p;
258 #endif
259 int mismatch = 0;
261 ASSERT(shadow_locked_by_me(d));
263 if ( gw->version ==
264 atomic_read(&d->arch.paging.shadow.gtable_dirty_version) )
265 return 1;
267 /* We may consider caching guest page mapping from last
268 * guest table walk. However considering this check happens
269 * relatively less-frequent, and a bit burden here to
270 * remap guest page is better than caching mapping in each
271 * guest table walk.
272 *
273 * Also when inconsistency occurs, simply return to trigger
274 * another fault instead of re-validate new path to make
275 * logic simple.
276 */
277 perfc_incr(shadow_check_gwalk);
278 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
279 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
280 l4p = (guest_l4e_t *)v->arch.paging.shadow.guest_vtable;
281 mismatch |= (gw->l4e.l4 != l4p[guest_l4_table_offset(va)].l4);
282 l3p = sh_map_domain_page(gw->l3mfn);
283 mismatch |= (gw->l3e.l3 != l3p[guest_l3_table_offset(va)].l3);
284 sh_unmap_domain_page(l3p);
285 #else
286 mismatch |= (gw->l3e.l3 !=
287 v->arch.paging.shadow.gl3e[guest_l3_table_offset(va)].l3);
288 #endif
289 l2p = sh_map_domain_page(gw->l2mfn);
290 mismatch |= (gw->l2e.l2 != l2p[guest_l2_table_offset(va)].l2);
291 sh_unmap_domain_page(l2p);
292 #else
293 l2p = (guest_l2e_t *)v->arch.paging.shadow.guest_vtable;
294 mismatch |= (gw->l2e.l2 != l2p[guest_l2_table_offset(va)].l2);
295 #endif
296 if ( !(guest_supports_superpages(v) &&
297 (guest_l2e_get_flags(gw->l2e) & _PAGE_PSE)) )
298 {
299 l1p = sh_map_domain_page(gw->l1mfn);
300 mismatch |= (gw->l1e.l1 != l1p[guest_l1_table_offset(va)].l1);
301 sh_unmap_domain_page(l1p);
302 }
304 return !mismatch;
305 }
307 /* Remove write access permissions from a gwalk_t in a batch, and
308 * return OR-ed result for TLB flush hint and need to rewalk the guest
309 * pages.
310 *
311 * Syncing pages will remove write access to that page; but it may
312 * also give write access to other pages in the path. If we resync any
313 * pages, re-walk from the beginning.
314 */
315 #define GW_RMWR_FLUSHTLB 1
316 #define GW_RMWR_REWALK 2
318 static inline uint32_t
319 gw_remove_write_accesses(struct vcpu *v, unsigned long va, walk_t *gw)
320 {
321 uint32_t rc = 0;
323 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
324 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
325 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
326 if ( mfn_is_out_of_sync(gw->l3mfn) )
327 {
328 sh_resync(v, gw->l3mfn);
329 rc = GW_RMWR_REWALK;
330 }
331 else
332 #endif /* OOS */
333 if ( sh_remove_write_access(v, gw->l3mfn, 3, va) )
334 rc = GW_RMWR_FLUSHTLB;
335 #endif /* GUEST_PAGING_LEVELS >= 4 */
337 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
338 if ( mfn_is_out_of_sync(gw->l2mfn) )
339 {
340 sh_resync(v, gw->l2mfn);
341 rc |= GW_RMWR_REWALK;
342 }
343 else
344 #endif /* OOS */
345 if ( sh_remove_write_access(v, gw->l2mfn, 2, va) )
346 rc |= GW_RMWR_FLUSHTLB;
347 #endif /* GUEST_PAGING_LEVELS >= 3 */
349 if ( !(guest_supports_superpages(v) &&
350 (guest_l2e_get_flags(gw->l2e) & _PAGE_PSE))
351 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
352 && !mfn_is_out_of_sync(gw->l1mfn)
353 #endif /* OOS */
354 && sh_remove_write_access(v, gw->l1mfn, 1, va) )
355 rc |= GW_RMWR_FLUSHTLB;
357 return rc;
358 }
360 /* Walk the guest pagetables, after the manner of a hardware walker.
361 *
362 * Inputs: a vcpu, a virtual address, a walk_t to fill, a
363 * pointer to a pagefault code
364 *
365 * We walk the vcpu's guest pagetables, filling the walk_t with what we
366 * see and adding any Accessed and Dirty bits that are needed in the
367 * guest entries. Using the pagefault code, we check the permissions as
368 * we go. For the purposes of reading pagetables we treat all non-RAM
369 * memory as contining zeroes.
370 *
371 * The walk is done in a lock-free style, with some sanity check postponed
372 * after grabbing shadow lock later. Those delayed checks will make sure
373 * no inconsistent mapping being translated into shadow page table.
374 *
375 * Returns 0 for success, or the set of permission bits that we failed on
376 * if the walk did not complete.
377 * N.B. This is different from the old return code but almost no callers
378 * checked the old return code anyway.
379 */
380 static uint32_t
381 guest_walk_tables(struct vcpu *v, unsigned long va, walk_t *gw, uint32_t pfec)
382 {
383 struct domain *d = v->domain;
384 p2m_type_t p2mt;
385 guest_l1e_t *l1p = NULL;
386 guest_l2e_t *l2p = NULL;
387 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
388 guest_l3e_t *l3p = NULL;
389 guest_l4e_t *l4p;
390 #endif
391 uint32_t gflags, mflags, rc = 0;
392 int pse;
394 perfc_incr(shadow_guest_walk);
395 memset(gw, 0, sizeof(*gw));
396 gw->va = va;
398 gw->version = atomic_read(&d->arch.paging.shadow.gtable_dirty_version);
399 rmb();
401 /* Mandatory bits that must be set in every entry. We invert NX, to
402 * calculate as if there were an "X" bit that allowed access.
403 * We will accumulate, in rc, the set of flags that are missing. */
404 mflags = mandatory_flags(v, pfec);
406 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
407 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
409 /* Get the l4e from the top level table and check its flags*/
410 gw->l4mfn = pagetable_get_mfn(v->arch.guest_table);
411 l4p = ((guest_l4e_t *)v->arch.paging.shadow.guest_vtable);
412 gw->l4e = l4p[guest_l4_table_offset(va)];
413 gflags = guest_l4e_get_flags(gw->l4e) ^ _PAGE_NX_BIT;
414 rc |= ((gflags & mflags) ^ mflags);
415 if ( rc & _PAGE_PRESENT ) goto out;
417 /* Map the l3 table */
418 gw->l3mfn = gfn_to_mfn(d, guest_l4e_get_gfn(gw->l4e), &p2mt);
419 if ( !p2m_is_ram(p2mt) )
420 {
421 rc |= _PAGE_PRESENT;
422 goto out;
423 }
424 ASSERT(mfn_valid(gw->l3mfn));
426 /* Get the l3e and check its flags*/
427 l3p = sh_map_domain_page(gw->l3mfn);
428 gw->l3e = l3p[guest_l3_table_offset(va)];
429 gflags = guest_l3e_get_flags(gw->l3e) ^ _PAGE_NX_BIT;
430 rc |= ((gflags & mflags) ^ mflags);
431 if ( rc & _PAGE_PRESENT )
432 goto out;
434 #else /* PAE only... */
436 /* Get l3e from the cache of the top level table and check its flag */
437 gw->l3e = v->arch.paging.shadow.gl3e[guest_l3_table_offset(va)];
438 if ( !(guest_l3e_get_flags(gw->l3e) & _PAGE_PRESENT) )
439 {
440 rc |= _PAGE_PRESENT;
441 goto out;
442 }
444 #endif /* PAE or 64... */
446 /* Map the l2 table */
447 gw->l2mfn = gfn_to_mfn(d, guest_l3e_get_gfn(gw->l3e), &p2mt);
448 if ( !p2m_is_ram(p2mt) )
449 {
450 rc |= _PAGE_PRESENT;
451 goto out;
452 }
453 ASSERT(mfn_valid(gw->l2mfn));
455 /* Get the l2e */
456 l2p = sh_map_domain_page(gw->l2mfn);
457 gw->l2e = l2p[guest_l2_table_offset(va)];
459 #else /* 32-bit only... */
461 /* Get l2e from the top level table */
462 gw->l2mfn = pagetable_get_mfn(v->arch.guest_table);
463 l2p = ((guest_l2e_t *)v->arch.paging.shadow.guest_vtable);
464 gw->l2e = l2p[guest_l2_table_offset(va)];
466 #endif /* All levels... */
468 gflags = guest_l2e_get_flags(gw->l2e) ^ _PAGE_NX_BIT;
469 rc |= ((gflags & mflags) ^ mflags);
470 if ( rc & _PAGE_PRESENT )
471 goto out;
473 pse = (guest_supports_superpages(v) &&
474 (guest_l2e_get_flags(gw->l2e) & _PAGE_PSE));
476 if ( pse )
477 {
478 /* Special case: this guest VA is in a PSE superpage, so there's
479 * no guest l1e. We make one up so that the propagation code
480 * can generate a shadow l1 table. Start with the gfn of the
481 * first 4k-page of the superpage. */
482 gfn_t start = guest_l2e_get_gfn(gw->l2e);
483 /* Grant full access in the l1e, since all the guest entry's
484 * access controls are enforced in the shadow l2e. */
485 int flags = (_PAGE_PRESENT|_PAGE_USER|_PAGE_RW|
486 _PAGE_ACCESSED|_PAGE_DIRTY);
487 /* PSE level 2 entries use bit 12 for PAT; propagate it to bit 7
488 * of the level 1. */
489 if ( (guest_l2e_get_flags(gw->l2e) & _PAGE_PSE_PAT) )
490 flags |= _PAGE_PAT;
491 /* Copy the cache-control bits to the l1 as well, because we
492 * can't represent PAT in the (non-PSE) shadow l2e. :(
493 * This could cause problems if a guest ever maps an area of
494 * memory with superpages using more than one caching mode. */
495 flags |= guest_l2e_get_flags(gw->l2e) & (_PAGE_PWT|_PAGE_PCD);
496 /* Increment the pfn by the right number of 4k pages.
497 * The ~0x1 is to mask out the PAT bit mentioned above. */
498 start = _gfn((gfn_x(start) & ~0x1) + guest_l1_table_offset(va));
499 gw->l1e = guest_l1e_from_gfn(start, flags);
500 gw->l1mfn = _mfn(INVALID_MFN);
501 }
502 else
503 {
504 /* Not a superpage: carry on and find the l1e. */
505 gw->l1mfn = gfn_to_mfn(d, guest_l2e_get_gfn(gw->l2e), &p2mt);
506 if ( !p2m_is_ram(p2mt) )
507 {
508 rc |= _PAGE_PRESENT;
509 goto out;
510 }
511 ASSERT(mfn_valid(gw->l1mfn));
512 l1p = sh_map_domain_page(gw->l1mfn);
513 gw->l1e = l1p[guest_l1_table_offset(va)];
514 gflags = guest_l1e_get_flags(gw->l1e) ^ _PAGE_NX_BIT;
515 rc |= ((gflags & mflags) ^ mflags);
516 }
518 /* Go back and set accessed and dirty bits only if the walk was a
519 * success. Although the PRMs say higher-level _PAGE_ACCESSED bits
520 * get set whenever a lower-level PT is used, at least some hardware
521 * walkers behave this way. */
522 if ( rc == 0 )
523 {
524 #if GUEST_PAGING_LEVELS == 4 /* 64-bit only... */
525 if ( set_ad_bits(l4p + guest_l4_table_offset(va), &gw->l4e, 0) )
526 paging_mark_dirty(d, mfn_x(gw->l4mfn));
527 if ( set_ad_bits(l3p + guest_l3_table_offset(va), &gw->l3e, 0) )
528 paging_mark_dirty(d, mfn_x(gw->l3mfn));
529 #endif
530 if ( set_ad_bits(l2p + guest_l2_table_offset(va), &gw->l2e,
531 (pse && (pfec & PFEC_write_access))) )
532 paging_mark_dirty(d, mfn_x(gw->l2mfn));
533 if ( !pse )
534 {
535 if ( set_ad_bits(l1p + guest_l1_table_offset(va), &gw->l1e,
536 (pfec & PFEC_write_access)) )
537 paging_mark_dirty(d, mfn_x(gw->l1mfn));
538 }
539 }
541 out:
542 #if GUEST_PAGING_LEVELS == 4
543 if ( l3p ) sh_unmap_domain_page(l3p);
544 #endif
545 #if GUEST_PAGING_LEVELS >= 3
546 if ( l2p ) sh_unmap_domain_page(l2p);
547 #endif
548 if ( l1p ) sh_unmap_domain_page(l1p);
550 return rc;
551 }
553 /* Given a walk_t, translate the gw->va into the guest's notion of the
554 * corresponding frame number. */
555 static inline gfn_t
556 guest_walk_to_gfn(walk_t *gw)
557 {
558 if ( !(guest_l1e_get_flags(gw->l1e) & _PAGE_PRESENT) )
559 return _gfn(INVALID_GFN);
560 return guest_l1e_get_gfn(gw->l1e);
561 }
563 /* Given a walk_t, translate the gw->va into the guest's notion of the
564 * corresponding physical address. */
565 static inline paddr_t
566 guest_walk_to_gpa(walk_t *gw)
567 {
568 if ( !(guest_l1e_get_flags(gw->l1e) & _PAGE_PRESENT) )
569 return 0;
570 return guest_l1e_get_paddr(gw->l1e) + (gw->va & ~PAGE_MASK);
571 }
573 #if 0 /* Keep for debugging */
574 /* Pretty-print the contents of a guest-walk */
575 static inline void print_gw(walk_t *gw)
576 {
577 SHADOW_PRINTK("GUEST WALK TO %#lx:\n", gw->va);
578 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
579 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
580 SHADOW_PRINTK(" l4mfn=%" PRI_mfn "\n", mfn_x(gw->l4mfn));
581 SHADOW_PRINTK(" l4e=%" SH_PRI_gpte "\n", gw->l4e.l4);
582 SHADOW_PRINTK(" l3mfn=%" PRI_mfn "\n", mfn_x(gw->l3mfn));
583 #endif /* PAE or 64... */
584 SHADOW_PRINTK(" l3e=%" SH_PRI_gpte "\n", gw->l3e.l3);
585 #endif /* All levels... */
586 SHADOW_PRINTK(" l2mfn=%" PRI_mfn "\n", mfn_x(gw->l2mfn));
587 SHADOW_PRINTK(" l2e=%" SH_PRI_gpte "\n", gw->l2e.l2);
588 SHADOW_PRINTK(" l1mfn=%" PRI_mfn "\n", mfn_x(gw->l1mfn));
589 SHADOW_PRINTK(" l1e=%" SH_PRI_gpte "\n", gw->l1e.l1);
590 }
591 #endif /* 0 */
593 #if SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES
594 /* Lightweight audit: pass all the shadows associated with this guest walk
595 * through the audit mechanisms */
596 static void sh_audit_gw(struct vcpu *v, walk_t *gw)
597 {
598 mfn_t smfn;
600 if ( !(SHADOW_AUDIT_ENABLE) )
601 return;
603 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
604 if ( mfn_valid(gw->l4mfn)
605 && mfn_valid((smfn = get_shadow_status(v, gw->l4mfn,
606 SH_type_l4_shadow))) )
607 (void) sh_audit_l4_table(v, smfn, _mfn(INVALID_MFN));
608 if ( mfn_valid(gw->l3mfn)
609 && mfn_valid((smfn = get_shadow_status(v, gw->l3mfn,
610 SH_type_l3_shadow))) )
611 (void) sh_audit_l3_table(v, smfn, _mfn(INVALID_MFN));
612 #endif /* PAE or 64... */
613 if ( mfn_valid(gw->l2mfn) )
614 {
615 if ( mfn_valid((smfn = get_shadow_status(v, gw->l2mfn,
616 SH_type_l2_shadow))) )
617 (void) sh_audit_l2_table(v, smfn, _mfn(INVALID_MFN));
618 #if GUEST_PAGING_LEVELS == 3
619 if ( mfn_valid((smfn = get_shadow_status(v, gw->l2mfn,
620 SH_type_l2h_shadow))) )
621 (void) sh_audit_l2_table(v, smfn, _mfn(INVALID_MFN));
622 #endif
623 }
624 if ( mfn_valid(gw->l1mfn)
625 && mfn_valid((smfn = get_shadow_status(v, gw->l1mfn,
626 SH_type_l1_shadow))) )
627 (void) sh_audit_l1_table(v, smfn, _mfn(INVALID_MFN));
628 else if ( (guest_l2e_get_flags(gw->l2e) & _PAGE_PRESENT)
629 && (guest_l2e_get_flags(gw->l2e) & _PAGE_PSE)
630 && mfn_valid(
631 (smfn = get_fl1_shadow_status(v, guest_l2e_get_gfn(gw->l2e)))) )
632 (void) sh_audit_fl1_table(v, smfn, _mfn(INVALID_MFN));
633 }
635 #else
636 #define sh_audit_gw(_v, _gw) do {} while(0)
637 #endif /* audit code */
640 #if (CONFIG_PAGING_LEVELS == GUEST_PAGING_LEVELS)
641 void *
642 sh_guest_map_l1e(struct vcpu *v, unsigned long addr,
643 unsigned long *gl1mfn)
644 {
645 void *pl1e = NULL;
646 walk_t gw;
648 ASSERT(shadow_mode_translate(v->domain));
650 // XXX -- this is expensive, but it's easy to cobble together...
651 // FIXME!
653 if ( guest_walk_tables(v, addr, &gw, PFEC_page_present) == 0
654 && mfn_valid(gw.l1mfn) )
655 {
656 if ( gl1mfn )
657 *gl1mfn = mfn_x(gw.l1mfn);
658 pl1e = map_domain_page(mfn_x(gw.l1mfn)) +
659 (guest_l1_table_offset(addr) * sizeof(guest_l1e_t));
660 }
662 return pl1e;
663 }
665 void
666 sh_guest_get_eff_l1e(struct vcpu *v, unsigned long addr, void *eff_l1e)
667 {
668 walk_t gw;
670 ASSERT(shadow_mode_translate(v->domain));
672 // XXX -- this is expensive, but it's easy to cobble together...
673 // FIXME!
675 (void) guest_walk_tables(v, addr, &gw, PFEC_page_present);
676 *(guest_l1e_t *)eff_l1e = gw.l1e;
677 }
678 #endif /* CONFIG == GUEST (== SHADOW) */
680 /**************************************************************************/
681 /* Functions to compute the correct index into a shadow page, given an
682 * index into the guest page (as returned by guest_get_index()).
683 * This is trivial when the shadow and guest use the same sized PTEs, but
684 * gets more interesting when those sizes are mismatched (e.g. 32-bit guest,
685 * PAE- or 64-bit shadows).
686 *
687 * These functions also increment the shadow mfn, when necessary. When PTE
688 * sizes are mismatched, it takes 2 shadow L1 pages for a single guest L1
689 * page. In this case, we allocate 2 contiguous pages for the shadow L1, and
690 * use simple pointer arithmetic on a pointer to the guest L1e to figure out
691 * which shadow page we really want. Similarly, when PTE sizes are
692 * mismatched, we shadow a guest L2 page with 4 shadow L2 pages. (The easiest
693 * way to see this is: a 32-bit guest L2 page maps 4GB of virtual address
694 * space, while a PAE- or 64-bit shadow L2 page maps 1GB of virtual address
695 * space.)
696 *
697 * For PAE guests, for every 32-bytes of guest L3 page table, we use 64-bytes
698 * of shadow (to store both the shadow, and the info that would normally be
699 * stored in page_info fields). This arrangement allows the shadow and the
700 * "page_info" fields to always be stored in the same page (in fact, in
701 * the same cache line), avoiding an extra call to map_domain_page().
702 */
704 static inline u32
705 guest_index(void *ptr)
706 {
707 return (u32)((unsigned long)ptr & ~PAGE_MASK) / sizeof(guest_l1e_t);
708 }
710 static u32
711 shadow_l1_index(mfn_t *smfn, u32 guest_index)
712 {
713 #if (GUEST_PAGING_LEVELS == 2)
714 *smfn = _mfn(mfn_x(*smfn) +
715 (guest_index / SHADOW_L1_PAGETABLE_ENTRIES));
716 return (guest_index % SHADOW_L1_PAGETABLE_ENTRIES);
717 #else
718 return guest_index;
719 #endif
720 }
722 static u32
723 shadow_l2_index(mfn_t *smfn, u32 guest_index)
724 {
725 #if (GUEST_PAGING_LEVELS == 2)
726 // Because we use 2 shadow l2 entries for each guest entry, the number of
727 // guest entries per shadow page is SHADOW_L2_PAGETABLE_ENTRIES/2
728 //
729 *smfn = _mfn(mfn_x(*smfn) +
730 (guest_index / (SHADOW_L2_PAGETABLE_ENTRIES / 2)));
732 // We multiply by two to get the index of the first of the two entries
733 // used to shadow the specified guest entry.
734 return (guest_index % (SHADOW_L2_PAGETABLE_ENTRIES / 2)) * 2;
735 #else
736 return guest_index;
737 #endif
738 }
740 #if GUEST_PAGING_LEVELS >= 4
742 static u32
743 shadow_l3_index(mfn_t *smfn, u32 guest_index)
744 {
745 return guest_index;
746 }
748 static u32
749 shadow_l4_index(mfn_t *smfn, u32 guest_index)
750 {
751 return guest_index;
752 }
754 #endif // GUEST_PAGING_LEVELS >= 4
757 /**************************************************************************/
758 /* Function which computes shadow entries from their corresponding guest
759 * entries. This is the "heart" of the shadow code. It operates using
760 * level-1 shadow types, but handles all levels of entry.
761 * Don't call it directly, but use the four wrappers below.
762 */
764 static always_inline void
765 _sh_propagate(struct vcpu *v,
766 guest_intpte_t guest_intpte,
767 mfn_t target_mfn,
768 void *shadow_entry_ptr,
769 int level,
770 fetch_type_t ft,
771 p2m_type_t p2mt)
772 {
773 guest_l1e_t guest_entry = { guest_intpte };
774 shadow_l1e_t *sp = shadow_entry_ptr;
775 struct domain *d = v->domain;
776 gfn_t target_gfn = guest_l1e_get_gfn(guest_entry);
777 u32 pass_thru_flags;
778 u32 gflags, sflags;
780 /* We don't shadow PAE l3s */
781 ASSERT(GUEST_PAGING_LEVELS > 3 || level != 3);
783 /* Check there's something for the shadows to map to */
784 if ( !p2m_is_valid(p2mt) )
785 {
786 *sp = shadow_l1e_empty();
787 goto done;
788 }
790 gflags = guest_l1e_get_flags(guest_entry);
792 if ( unlikely(!(gflags & _PAGE_PRESENT)) )
793 {
794 /* If a guest l1 entry is not present, shadow with the magic
795 * guest-not-present entry. */
796 if ( level == 1 )
797 *sp = sh_l1e_gnp();
798 else
799 *sp = shadow_l1e_empty();
800 goto done;
801 }
803 if ( level == 1 && p2mt == p2m_mmio_dm )
804 {
805 /* Guest l1e maps emulated MMIO space */
806 *sp = sh_l1e_mmio(target_gfn, gflags);
807 if ( !d->arch.paging.shadow.has_fast_mmio_entries )
808 d->arch.paging.shadow.has_fast_mmio_entries = 1;
809 goto done;
810 }
812 // Must have a valid target_mfn unless this is a prefetch or an l1
813 // pointing at MMIO space. In the case of a prefetch, an invalid
814 // mfn means that we can not usefully shadow anything, and so we
815 // return early.
816 //
817 if ( !mfn_valid(target_mfn)
818 && !(level == 1 && (!shadow_mode_refcounts(d)
819 || p2mt == p2m_mmio_direct)) )
820 {
821 ASSERT((ft == ft_prefetch));
822 *sp = shadow_l1e_empty();
823 goto done;
824 }
826 // Propagate bits from the guest to the shadow.
827 // Some of these may be overwritten, below.
828 // Since we know the guest's PRESENT bit is set, we also set the shadow's
829 // SHADOW_PRESENT bit.
830 //
831 pass_thru_flags = (_PAGE_ACCESSED | _PAGE_USER |
832 _PAGE_RW | _PAGE_PRESENT);
833 if ( guest_supports_nx(v) )
834 pass_thru_flags |= _PAGE_NX_BIT;
835 if ( !shadow_mode_refcounts(d) && !mfn_valid(target_mfn) )
836 pass_thru_flags |= _PAGE_PAT | _PAGE_PCD | _PAGE_PWT;
837 sflags = gflags & pass_thru_flags;
839 /*
840 * For HVM domains with direct access to MMIO areas, set the correct
841 * caching attributes in the shadows to match what was asked for.
842 */
843 if ( (level == 1) && is_hvm_domain(d) &&
844 !list_empty(&(domain_hvm_iommu(d)->pdev_list)) &&
845 !is_xen_heap_mfn(mfn_x(target_mfn)) )
846 {
847 unsigned int type;
848 if ( hvm_get_mem_pinned_cacheattr(d, gfn_x(target_gfn), &type) )
849 sflags |= pat_type_2_pte_flags(type);
850 else if ( d->arch.hvm_domain.is_in_uc_mode )
851 sflags |= pat_type_2_pte_flags(PAT_TYPE_UNCACHABLE);
852 else
853 sflags |= get_pat_flags(v,
854 gflags,
855 gfn_to_paddr(target_gfn),
856 ((paddr_t)mfn_x(target_mfn)) << PAGE_SHIFT);
857 }
859 // Set the A&D bits for higher level shadows.
860 // Higher level entries do not, strictly speaking, have dirty bits, but
861 // since we use shadow linear tables, each of these entries may, at some
862 // point in time, also serve as a shadow L1 entry.
863 // By setting both the A&D bits in each of these, we eliminate the burden
864 // on the hardware to update these bits on initial accesses.
865 //
866 if ( (level > 1) && !((SHADOW_PAGING_LEVELS == 3) && (level == 3)) )
867 sflags |= _PAGE_ACCESSED | _PAGE_DIRTY;
869 // If the A or D bit has not yet been set in the guest, then we must
870 // prevent the corresponding kind of access.
871 //
872 if ( unlikely(!(gflags & _PAGE_ACCESSED)) )
873 sflags &= ~_PAGE_PRESENT;
875 /* D bits exist in L1es and PSE L2es */
876 if ( unlikely(((level == 1) ||
877 ((level == 2) &&
878 (gflags & _PAGE_PSE) &&
879 guest_supports_superpages(v)))
880 && !(gflags & _PAGE_DIRTY)) )
881 sflags &= ~_PAGE_RW;
883 // shadow_mode_log_dirty support
884 //
885 // Only allow the guest write access to a page a) on a demand fault,
886 // or b) if the page is already marked as dirty.
887 //
888 // (We handle log-dirty entirely inside the shadow code, without using the
889 // p2m_ram_logdirty p2m type: only HAP uses that.)
890 if ( unlikely((level == 1) && shadow_mode_log_dirty(d)) )
891 {
892 if ( mfn_valid(target_mfn) ) {
893 if ( ft & FETCH_TYPE_WRITE )
894 paging_mark_dirty(d, mfn_x(target_mfn));
895 else if ( !sh_mfn_is_dirty(d, target_mfn) )
896 sflags &= ~_PAGE_RW;
897 }
898 }
900 if ( unlikely((level == 1) && d->dirty_vram
901 && d->dirty_vram->last_dirty == -1
902 && gfn_x(target_gfn) >= d->dirty_vram->begin_pfn
903 && gfn_x(target_gfn) < d->dirty_vram->end_pfn) )
904 {
905 if ( ft & FETCH_TYPE_WRITE )
906 d->dirty_vram->last_dirty = NOW();
907 else
908 sflags &= ~_PAGE_RW;
909 }
911 /* Read-only memory */
912 if ( p2mt == p2m_ram_ro )
913 sflags &= ~_PAGE_RW;
915 // protect guest page tables
916 //
917 if ( unlikely((level == 1)
918 && sh_mfn_is_a_page_table(target_mfn)
919 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC )
920 && !mfn_oos_may_write(target_mfn)
921 #endif /* OOS */
922 ) )
923 {
924 if ( shadow_mode_trap_reads(d) )
925 {
926 // if we are trapping both reads & writes, then mark this page
927 // as not present...
928 //
929 sflags &= ~_PAGE_PRESENT;
930 }
931 else
932 {
933 // otherwise, just prevent any writes...
934 //
935 sflags &= ~_PAGE_RW;
936 }
937 }
939 // PV guests in 64-bit mode use two different page tables for user vs
940 // supervisor permissions, making the guest's _PAGE_USER bit irrelevant.
941 // It is always shadowed as present...
942 if ( (GUEST_PAGING_LEVELS == 4) && !is_pv_32on64_domain(d)
943 && !is_hvm_domain(d) )
944 {
945 sflags |= _PAGE_USER;
946 }
948 *sp = shadow_l1e_from_mfn(target_mfn, sflags);
950 done:
951 SHADOW_DEBUG(PROPAGATE,
952 "%s level %u guest %" SH_PRI_gpte " shadow %" SH_PRI_pte "\n",
953 fetch_type_names[ft], level, guest_entry.l1, sp->l1);
954 }
957 /* These four wrappers give us a little bit of type-safety back around
958 * the use of void-* pointers and intpte types in _sh_propagate(), and
959 * allow the compiler to optimize out some level checks. */
961 #if GUEST_PAGING_LEVELS >= 4
962 static void
963 l4e_propagate_from_guest(struct vcpu *v,
964 guest_l4e_t gl4e,
965 mfn_t sl3mfn,
966 shadow_l4e_t *sl4e,
967 fetch_type_t ft)
968 {
969 _sh_propagate(v, gl4e.l4, sl3mfn, sl4e, 4, ft, p2m_ram_rw);
970 }
972 static void
973 l3e_propagate_from_guest(struct vcpu *v,
974 guest_l3e_t gl3e,
975 mfn_t sl2mfn,
976 shadow_l3e_t *sl3e,
977 fetch_type_t ft)
978 {
979 _sh_propagate(v, gl3e.l3, sl2mfn, sl3e, 3, ft, p2m_ram_rw);
980 }
981 #endif // GUEST_PAGING_LEVELS >= 4
983 static void
984 l2e_propagate_from_guest(struct vcpu *v,
985 guest_l2e_t gl2e,
986 mfn_t sl1mfn,
987 shadow_l2e_t *sl2e,
988 fetch_type_t ft)
989 {
990 _sh_propagate(v, gl2e.l2, sl1mfn, sl2e, 2, ft, p2m_ram_rw);
991 }
993 static void
994 l1e_propagate_from_guest(struct vcpu *v,
995 guest_l1e_t gl1e,
996 mfn_t gmfn,
997 shadow_l1e_t *sl1e,
998 fetch_type_t ft,
999 p2m_type_t p2mt)
1001 _sh_propagate(v, gl1e.l1, gmfn, sl1e, 1, ft, p2mt);
1005 /**************************************************************************/
1006 /* These functions update shadow entries (and do bookkeeping on the shadow
1007 * tables they are in). It is intended that they are the only
1008 * functions which ever write (non-zero) data onto a shadow page.
1009 */
1011 static inline void safe_write_entry(void *dst, void *src)
1012 /* Copy one PTE safely when processors might be running on the
1013 * destination pagetable. This does *not* give safety against
1014 * concurrent writes (that's what the shadow lock is for), just
1015 * stops the hardware picking up partially written entries. */
1017 volatile unsigned long *d = dst;
1018 unsigned long *s = src;
1019 ASSERT(!((unsigned long) d & (sizeof (shadow_l1e_t) - 1)));
1020 #if CONFIG_PAGING_LEVELS == 3
1021 /* In PAE mode, pagetable entries are larger
1022 * than machine words, so won't get written atomically. We need to make
1023 * sure any other cpu running on these shadows doesn't see a
1024 * half-written entry. Do this by marking the entry not-present first,
1025 * then writing the high word before the low word. */
1026 BUILD_BUG_ON(sizeof (shadow_l1e_t) != 2 * sizeof (unsigned long));
1027 d[0] = 0;
1028 d[1] = s[1];
1029 d[0] = s[0];
1030 #else
1031 /* In 64-bit, sizeof(pte) == sizeof(ulong) == 1 word,
1032 * which will be an atomic write, since the entry is aligned. */
1033 BUILD_BUG_ON(sizeof (shadow_l1e_t) != sizeof (unsigned long));
1034 *d = *s;
1035 #endif
1039 static inline void
1040 shadow_write_entries(void *d, void *s, int entries, mfn_t mfn)
1041 /* This function does the actual writes to shadow pages.
1042 * It must not be called directly, since it doesn't do the bookkeeping
1043 * that shadow_set_l*e() functions do. */
1045 shadow_l1e_t *dst = d;
1046 shadow_l1e_t *src = s;
1047 void *map = NULL;
1048 int i;
1050 /* Because we mirror access rights at all levels in the shadow, an
1051 * l2 (or higher) entry with the RW bit cleared will leave us with
1052 * no write access through the linear map.
1053 * We detect that by writing to the shadow with copy_to_user() and
1054 * using map_domain_page() to get a writeable mapping if we need to. */
1055 if ( __copy_to_user(d, d, sizeof (unsigned long)) != 0 )
1057 perfc_incr(shadow_linear_map_failed);
1058 map = sh_map_domain_page(mfn);
1059 ASSERT(map != NULL);
1060 dst = map + ((unsigned long)dst & (PAGE_SIZE - 1));
1064 for ( i = 0; i < entries; i++ )
1065 safe_write_entry(dst++, src++);
1067 if ( map != NULL ) sh_unmap_domain_page(map);
1070 static inline int
1071 perms_strictly_increased(u32 old_flags, u32 new_flags)
1072 /* Given the flags of two entries, are the new flags a strict
1073 * increase in rights over the old ones? */
1075 u32 of = old_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX);
1076 u32 nf = new_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX);
1077 /* Flip the NX bit, since it's the only one that decreases rights;
1078 * we calculate as if it were an "X" bit. */
1079 of ^= _PAGE_NX_BIT;
1080 nf ^= _PAGE_NX_BIT;
1081 /* If the changed bits are all set in the new flags, then rights strictly
1082 * increased between old and new. */
1083 return ((of | (of ^ nf)) == nf);
1086 static int inline
1087 shadow_get_page_from_l1e(shadow_l1e_t sl1e, struct domain *d)
1089 int res;
1090 mfn_t mfn;
1091 struct domain *owner;
1093 ASSERT(!sh_l1e_is_magic(sl1e));
1095 if ( !shadow_mode_refcounts(d) )
1096 return 1;
1098 res = get_page_from_l1e(sl1e, d);
1100 // If a privileged domain is attempting to install a map of a page it does
1101 // not own, we let it succeed anyway.
1102 //
1103 if ( unlikely(!res) &&
1104 !shadow_mode_translate(d) &&
1105 mfn_valid(mfn = shadow_l1e_get_mfn(sl1e)) &&
1106 (owner = page_get_owner(mfn_to_page(mfn))) &&
1107 (d != owner) &&
1108 IS_PRIV_FOR(d, owner))
1110 res = get_page_from_l1e(sl1e, owner);
1111 SHADOW_PRINTK("privileged domain %d installs map of mfn %05lx "
1112 "which is owned by domain %d: %s\n",
1113 d->domain_id, mfn_x(mfn), owner->domain_id,
1114 res ? "success" : "failed");
1117 if ( unlikely(!res) )
1119 perfc_incr(shadow_get_page_fail);
1120 SHADOW_PRINTK("failed: l1e=" SH_PRI_pte "\n");
1123 return res;
1126 static void inline
1127 shadow_put_page_from_l1e(shadow_l1e_t sl1e, struct domain *d)
1129 if ( !shadow_mode_refcounts(d) )
1130 return;
1132 put_page_from_l1e(sl1e, d);
1135 #if GUEST_PAGING_LEVELS >= 4
1136 static int shadow_set_l4e(struct vcpu *v,
1137 shadow_l4e_t *sl4e,
1138 shadow_l4e_t new_sl4e,
1139 mfn_t sl4mfn)
1141 int flags = 0, ok;
1142 shadow_l4e_t old_sl4e;
1143 paddr_t paddr;
1144 ASSERT(sl4e != NULL);
1145 old_sl4e = *sl4e;
1147 if ( old_sl4e.l4 == new_sl4e.l4 ) return 0; /* Nothing to do */
1149 paddr = ((((paddr_t)mfn_x(sl4mfn)) << PAGE_SHIFT)
1150 | (((unsigned long)sl4e) & ~PAGE_MASK));
1152 if ( shadow_l4e_get_flags(new_sl4e) & _PAGE_PRESENT )
1154 /* About to install a new reference */
1155 mfn_t sl3mfn = shadow_l4e_get_mfn(new_sl4e);
1156 ok = sh_get_ref(v, sl3mfn, paddr);
1157 /* Are we pinning l3 shadows to handle wierd linux behaviour? */
1158 if ( sh_type_is_pinnable(v, SH_type_l3_64_shadow) )
1159 ok |= sh_pin(v, sl3mfn);
1160 if ( !ok )
1162 domain_crash(v->domain);
1163 return SHADOW_SET_ERROR;
1165 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC )
1166 shadow_resync_all(v, 0);
1167 #endif
1170 /* Write the new entry */
1171 shadow_write_entries(sl4e, &new_sl4e, 1, sl4mfn);
1172 flags |= SHADOW_SET_CHANGED;
1174 if ( shadow_l4e_get_flags(old_sl4e) & _PAGE_PRESENT )
1176 /* We lost a reference to an old mfn. */
1177 mfn_t osl3mfn = shadow_l4e_get_mfn(old_sl4e);
1178 if ( (mfn_x(osl3mfn) != mfn_x(shadow_l4e_get_mfn(new_sl4e)))
1179 || !perms_strictly_increased(shadow_l4e_get_flags(old_sl4e),
1180 shadow_l4e_get_flags(new_sl4e)) )
1182 flags |= SHADOW_SET_FLUSH;
1184 sh_put_ref(v, osl3mfn, paddr);
1186 return flags;
1189 static int shadow_set_l3e(struct vcpu *v,
1190 shadow_l3e_t *sl3e,
1191 shadow_l3e_t new_sl3e,
1192 mfn_t sl3mfn)
1194 int flags = 0;
1195 shadow_l3e_t old_sl3e;
1196 paddr_t paddr;
1197 ASSERT(sl3e != NULL);
1198 old_sl3e = *sl3e;
1200 if ( old_sl3e.l3 == new_sl3e.l3 ) return 0; /* Nothing to do */
1202 paddr = ((((paddr_t)mfn_x(sl3mfn)) << PAGE_SHIFT)
1203 | (((unsigned long)sl3e) & ~PAGE_MASK));
1205 if ( shadow_l3e_get_flags(new_sl3e) & _PAGE_PRESENT )
1207 /* About to install a new reference */
1208 if ( !sh_get_ref(v, shadow_l3e_get_mfn(new_sl3e), paddr) )
1210 domain_crash(v->domain);
1211 return SHADOW_SET_ERROR;
1213 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC )
1214 shadow_resync_all(v, 0);
1215 #endif
1218 /* Write the new entry */
1219 shadow_write_entries(sl3e, &new_sl3e, 1, sl3mfn);
1220 flags |= SHADOW_SET_CHANGED;
1222 if ( shadow_l3e_get_flags(old_sl3e) & _PAGE_PRESENT )
1224 /* We lost a reference to an old mfn. */
1225 mfn_t osl2mfn = shadow_l3e_get_mfn(old_sl3e);
1226 if ( (mfn_x(osl2mfn) != mfn_x(shadow_l3e_get_mfn(new_sl3e))) ||
1227 !perms_strictly_increased(shadow_l3e_get_flags(old_sl3e),
1228 shadow_l3e_get_flags(new_sl3e)) )
1230 flags |= SHADOW_SET_FLUSH;
1232 sh_put_ref(v, osl2mfn, paddr);
1234 return flags;
1236 #endif /* GUEST_PAGING_LEVELS >= 4 */
1238 static int shadow_set_l2e(struct vcpu *v,
1239 shadow_l2e_t *sl2e,
1240 shadow_l2e_t new_sl2e,
1241 mfn_t sl2mfn)
1243 int flags = 0;
1244 shadow_l2e_t old_sl2e;
1245 paddr_t paddr;
1247 #if GUEST_PAGING_LEVELS == 2
1248 /* In 2-on-3 we work with pairs of l2es pointing at two-page
1249 * shadows. Reference counting and up-pointers track from the first
1250 * page of the shadow to the first l2e, so make sure that we're
1251 * working with those:
1252 * Align the pointer down so it's pointing at the first of the pair */
1253 sl2e = (shadow_l2e_t *)((unsigned long)sl2e & ~(sizeof(shadow_l2e_t)));
1254 /* Align the mfn of the shadow entry too */
1255 new_sl2e.l2 &= ~(1<<PAGE_SHIFT);
1256 #endif
1258 ASSERT(sl2e != NULL);
1259 old_sl2e = *sl2e;
1261 if ( old_sl2e.l2 == new_sl2e.l2 ) return 0; /* Nothing to do */
1263 paddr = ((((paddr_t)mfn_x(sl2mfn)) << PAGE_SHIFT)
1264 | (((unsigned long)sl2e) & ~PAGE_MASK));
1266 if ( shadow_l2e_get_flags(new_sl2e) & _PAGE_PRESENT )
1268 mfn_t sl1mfn = shadow_l2e_get_mfn(new_sl2e);
1270 /* About to install a new reference */
1271 if ( !sh_get_ref(v, sl1mfn, paddr) )
1273 domain_crash(v->domain);
1274 return SHADOW_SET_ERROR;
1276 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
1278 struct shadow_page_info *sp = mfn_to_shadow_page(sl1mfn);
1279 mfn_t gl1mfn = _mfn(sp->backpointer);
1281 /* If the shadow is a fl1 then the backpointer contains
1282 the GFN instead of the GMFN, and it's definitely not
1283 OOS. */
1284 if ( (sp->type != SH_type_fl1_shadow) && mfn_valid(gl1mfn)
1285 && mfn_is_out_of_sync(gl1mfn) )
1286 sh_resync(v, gl1mfn);
1288 #endif
1291 /* Write the new entry */
1292 #if GUEST_PAGING_LEVELS == 2
1294 shadow_l2e_t pair[2] = { new_sl2e, new_sl2e };
1295 /* The l1 shadow is two pages long and need to be pointed to by
1296 * two adjacent l1es. The pair have the same flags, but point
1297 * at odd and even MFNs */
1298 ASSERT(!(pair[0].l2 & (1<<PAGE_SHIFT)));
1299 pair[1].l2 |= (1<<PAGE_SHIFT);
1300 shadow_write_entries(sl2e, &pair, 2, sl2mfn);
1302 #else /* normal case */
1303 shadow_write_entries(sl2e, &new_sl2e, 1, sl2mfn);
1304 #endif
1305 flags |= SHADOW_SET_CHANGED;
1307 if ( shadow_l2e_get_flags(old_sl2e) & _PAGE_PRESENT )
1309 /* We lost a reference to an old mfn. */
1310 mfn_t osl1mfn = shadow_l2e_get_mfn(old_sl2e);
1311 if ( (mfn_x(osl1mfn) != mfn_x(shadow_l2e_get_mfn(new_sl2e))) ||
1312 !perms_strictly_increased(shadow_l2e_get_flags(old_sl2e),
1313 shadow_l2e_get_flags(new_sl2e)) )
1315 flags |= SHADOW_SET_FLUSH;
1317 sh_put_ref(v, osl1mfn, paddr);
1319 return flags;
1322 static inline void shadow_vram_get_l1e(shadow_l1e_t new_sl1e,
1323 shadow_l1e_t *sl1e,
1324 mfn_t sl1mfn,
1325 struct domain *d)
1327 mfn_t mfn;
1328 unsigned long gfn;
1330 if ( !d->dirty_vram ) return;
1332 mfn = shadow_l1e_get_mfn(new_sl1e);
1334 if ( !mfn_valid(mfn) ) return; /* m2p for mmio_direct may not exist */
1336 gfn = mfn_to_gfn(d, mfn);
1338 if ( (gfn >= d->dirty_vram->begin_pfn) && (gfn < d->dirty_vram->end_pfn) ) {
1339 unsigned long i = gfn - d->dirty_vram->begin_pfn;
1340 struct page_info *page = mfn_to_page(mfn);
1341 u32 count_info = page->u.inuse.type_info & PGT_count_mask;
1343 if ( count_info == 1 )
1344 /* Initial guest reference, record it */
1345 d->dirty_vram->sl1ma[i] = pfn_to_paddr(mfn_x(sl1mfn))
1346 | ((unsigned long)sl1e & ~PAGE_MASK);
1350 static inline void shadow_vram_put_l1e(shadow_l1e_t old_sl1e,
1351 shadow_l1e_t *sl1e,
1352 mfn_t sl1mfn,
1353 struct domain *d)
1355 mfn_t mfn;
1356 unsigned long gfn;
1358 if ( !d->dirty_vram ) return;
1360 mfn = shadow_l1e_get_mfn(old_sl1e);
1362 if ( !mfn_valid(mfn) ) return;
1364 gfn = mfn_to_gfn(d, mfn);
1366 if ( (gfn >= d->dirty_vram->begin_pfn) && (gfn < d->dirty_vram->end_pfn) ) {
1367 unsigned long i = gfn - d->dirty_vram->begin_pfn;
1368 struct page_info *page = mfn_to_page(mfn);
1369 u32 count_info = page->u.inuse.type_info & PGT_count_mask;
1370 int dirty = 0;
1371 paddr_t sl1ma = pfn_to_paddr(mfn_x(sl1mfn))
1372 | ((unsigned long)sl1e & ~PAGE_MASK);
1374 if ( count_info == 1 ) {
1375 /* Last reference */
1376 if ( d->dirty_vram->sl1ma[i] == INVALID_PADDR ) {
1377 /* We didn't know it was that one, let's say it is dirty */
1378 dirty = 1;
1379 } else {
1380 ASSERT(d->dirty_vram->sl1ma[i] == sl1ma);
1381 d->dirty_vram->sl1ma[i] = INVALID_PADDR;
1382 if ( shadow_l1e_get_flags(old_sl1e) & _PAGE_DIRTY )
1383 dirty = 1;
1385 } else {
1386 /* We had more than one reference, just consider the page dirty. */
1387 dirty = 1;
1388 /* Check that it's not the one we recorded. */
1389 if ( d->dirty_vram->sl1ma[i] == sl1ma ) {
1390 /* Too bad, we remembered the wrong one... */
1391 d->dirty_vram->sl1ma[i] = INVALID_PADDR;
1392 } else {
1393 /* Ok, our recorded sl1e is still pointing to this page, let's
1394 * just hope it will remain. */
1397 if ( dirty ) {
1398 d->dirty_vram->dirty_bitmap[i / 8] |= 1 << (i % 8);
1399 d->dirty_vram->last_dirty = NOW();
1404 static int shadow_set_l1e(struct vcpu *v,
1405 shadow_l1e_t *sl1e,
1406 shadow_l1e_t new_sl1e,
1407 mfn_t sl1mfn)
1409 int flags = 0;
1410 struct domain *d = v->domain;
1411 shadow_l1e_t old_sl1e;
1412 #if SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC
1413 mfn_t new_gmfn = shadow_l1e_get_mfn(new_sl1e);
1414 #endif
1415 ASSERT(sl1e != NULL);
1417 old_sl1e = *sl1e;
1419 if ( old_sl1e.l1 == new_sl1e.l1 ) return 0; /* Nothing to do */
1421 if ( (shadow_l1e_get_flags(new_sl1e) & _PAGE_PRESENT)
1422 && !sh_l1e_is_magic(new_sl1e) )
1424 /* About to install a new reference */
1425 if ( shadow_mode_refcounts(d) ) {
1426 if ( shadow_get_page_from_l1e(new_sl1e, d) == 0 )
1428 /* Doesn't look like a pagetable. */
1429 flags |= SHADOW_SET_ERROR;
1430 new_sl1e = shadow_l1e_empty();
1432 else
1434 shadow_vram_get_l1e(new_sl1e, sl1e, sl1mfn, d);
1435 #if SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC
1436 if ( mfn_valid(new_gmfn) && mfn_oos_may_write(new_gmfn)
1437 && (shadow_l1e_get_flags(new_sl1e) & _PAGE_RW) )
1439 oos_fixup_add(v, new_gmfn, sl1mfn, pgentry_ptr_to_slot(sl1e));
1441 #endif
1447 /* Write the new entry */
1448 shadow_write_entries(sl1e, &new_sl1e, 1, sl1mfn);
1449 flags |= SHADOW_SET_CHANGED;
1451 if ( (shadow_l1e_get_flags(old_sl1e) & _PAGE_PRESENT)
1452 && !sh_l1e_is_magic(old_sl1e) )
1454 /* We lost a reference to an old mfn. */
1455 /* N.B. Unlike higher-level sets, never need an extra flush
1456 * when writing an l1e. Because it points to the same guest frame
1457 * as the guest l1e did, it's the guest's responsibility to
1458 * trigger a flush later. */
1459 if ( shadow_mode_refcounts(d) )
1461 shadow_vram_put_l1e(old_sl1e, sl1e, sl1mfn, d);
1462 shadow_put_page_from_l1e(old_sl1e, d);
1465 return flags;
1469 /**************************************************************************/
1470 /* Macros to walk pagetables. These take the shadow of a pagetable and
1471 * walk every "interesting" entry. That is, they don't touch Xen mappings,
1472 * and for 32-bit l2s shadowed onto PAE or 64-bit, they only touch every
1473 * second entry (since pairs of entries are managed together). For multi-page
1474 * shadows they walk all pages.
1476 * Arguments are an MFN, the variable to point to each entry, a variable
1477 * to indicate that we are done (we will shortcut to the end of the scan
1478 * when _done != 0), a variable to indicate that we should avoid Xen mappings,
1479 * and the code.
1481 * WARNING: These macros have side-effects. They change the values of both
1482 * the pointer and the MFN. */
1484 static inline void increment_ptr_to_guest_entry(void *ptr)
1486 if ( ptr )
1488 guest_l1e_t **entry = ptr;
1489 (*entry)++;
1493 /* All kinds of l1: touch all entries */
1494 #define _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1495 do { \
1496 int _i; \
1497 shadow_l1e_t *_sp = sh_map_domain_page((_sl1mfn)); \
1498 ASSERT(mfn_to_shadow_page(_sl1mfn)->type == SH_type_l1_shadow \
1499 || mfn_to_shadow_page(_sl1mfn)->type == SH_type_fl1_shadow); \
1500 for ( _i = 0; _i < SHADOW_L1_PAGETABLE_ENTRIES; _i++ ) \
1501 { \
1502 (_sl1e) = _sp + _i; \
1503 if ( shadow_l1e_get_flags(*(_sl1e)) & _PAGE_PRESENT ) \
1504 {_code} \
1505 if ( _done ) break; \
1506 increment_ptr_to_guest_entry(_gl1p); \
1507 } \
1508 sh_unmap_domain_page(_sp); \
1509 } while (0)
1511 /* 32-bit l1, on PAE or 64-bit shadows: need to walk both pages of shadow */
1512 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1513 #define SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1514 do { \
1515 int __done = 0; \
1516 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, \
1517 ({ (__done = _done); }), _code); \
1518 _sl1mfn = _mfn(mfn_x(_sl1mfn) + 1); \
1519 if ( !__done ) \
1520 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, \
1521 ({ (__done = _done); }), _code); \
1522 } while (0)
1523 #else /* Everything else; l1 shadows are only one page */
1524 #define SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1525 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code)
1526 #endif
1529 #if GUEST_PAGING_LEVELS == 2
1531 /* 32-bit l2 on PAE/64: four pages, touch every second entry, and avoid Xen */
1532 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \
1533 do { \
1534 int _i, _j, __done = 0; \
1535 int _xen = !shadow_mode_external(_dom); \
1536 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_32_shadow); \
1537 for ( _j = 0; _j < 4 && !__done; _j++ ) \
1538 { \
1539 shadow_l2e_t *_sp = sh_map_domain_page(_sl2mfn); \
1540 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i += 2 ) \
1541 if ( (!(_xen)) \
1542 || ((_j * SHADOW_L2_PAGETABLE_ENTRIES) + _i) \
1543 < (HYPERVISOR_VIRT_START >> SHADOW_L2_PAGETABLE_SHIFT) ) \
1544 { \
1545 (_sl2e) = _sp + _i; \
1546 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1547 {_code} \
1548 if ( (__done = (_done)) ) break; \
1549 increment_ptr_to_guest_entry(_gl2p); \
1550 } \
1551 sh_unmap_domain_page(_sp); \
1552 _sl2mfn = _mfn(mfn_x(_sl2mfn) + 1); \
1553 } \
1554 } while (0)
1556 #elif GUEST_PAGING_LEVELS == 3
1558 /* PAE: if it's an l2h, don't touch Xen mappings */
1559 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \
1560 do { \
1561 int _i; \
1562 int _xen = !shadow_mode_external(_dom); \
1563 shadow_l2e_t *_sp = sh_map_domain_page((_sl2mfn)); \
1564 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_pae_shadow \
1565 || mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2h_pae_shadow);\
1566 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \
1567 if ( (!(_xen)) \
1568 || mfn_to_shadow_page(_sl2mfn)->type != SH_type_l2h_pae_shadow\
1569 || ((_i + (3 * SHADOW_L2_PAGETABLE_ENTRIES)) \
1570 < (HYPERVISOR_VIRT_START >> SHADOW_L2_PAGETABLE_SHIFT)) ) \
1571 { \
1572 (_sl2e) = _sp + _i; \
1573 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1574 {_code} \
1575 if ( _done ) break; \
1576 increment_ptr_to_guest_entry(_gl2p); \
1577 } \
1578 sh_unmap_domain_page(_sp); \
1579 } while (0)
1581 #else
1583 /* 64-bit l2: touch all entries except for PAE compat guests. */
1584 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \
1585 do { \
1586 int _i; \
1587 int _xen = !shadow_mode_external(_dom); \
1588 shadow_l2e_t *_sp = sh_map_domain_page((_sl2mfn)); \
1589 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_64_shadow || \
1590 mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2h_64_shadow); \
1591 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \
1592 { \
1593 if ( (!(_xen)) \
1594 || !is_pv_32on64_domain(_dom) \
1595 || mfn_to_shadow_page(_sl2mfn)->type != SH_type_l2h_64_shadow \
1596 || (_i < COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(_dom)) ) \
1597 { \
1598 (_sl2e) = _sp + _i; \
1599 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1600 {_code} \
1601 if ( _done ) break; \
1602 increment_ptr_to_guest_entry(_gl2p); \
1603 } \
1604 } \
1605 sh_unmap_domain_page(_sp); \
1606 } while (0)
1608 #endif /* different kinds of l2 */
1610 #if GUEST_PAGING_LEVELS == 4
1612 /* 64-bit l3: touch all entries */
1613 #define SHADOW_FOREACH_L3E(_sl3mfn, _sl3e, _gl3p, _done, _code) \
1614 do { \
1615 int _i; \
1616 shadow_l3e_t *_sp = sh_map_domain_page((_sl3mfn)); \
1617 ASSERT(mfn_to_shadow_page(_sl3mfn)->type == SH_type_l3_64_shadow); \
1618 for ( _i = 0; _i < SHADOW_L3_PAGETABLE_ENTRIES; _i++ ) \
1619 { \
1620 (_sl3e) = _sp + _i; \
1621 if ( shadow_l3e_get_flags(*(_sl3e)) & _PAGE_PRESENT ) \
1622 {_code} \
1623 if ( _done ) break; \
1624 increment_ptr_to_guest_entry(_gl3p); \
1625 } \
1626 sh_unmap_domain_page(_sp); \
1627 } while (0)
1629 /* 64-bit l4: avoid Xen mappings */
1630 #define SHADOW_FOREACH_L4E(_sl4mfn, _sl4e, _gl4p, _done, _dom, _code) \
1631 do { \
1632 shadow_l4e_t *_sp = sh_map_domain_page((_sl4mfn)); \
1633 int _xen = !shadow_mode_external(_dom); \
1634 int _i; \
1635 ASSERT(mfn_to_shadow_page(_sl4mfn)->type == SH_type_l4_64_shadow); \
1636 for ( _i = 0; _i < SHADOW_L4_PAGETABLE_ENTRIES; _i++ ) \
1637 { \
1638 if ( (!(_xen)) || is_guest_l4_slot(_dom, _i) ) \
1639 { \
1640 (_sl4e) = _sp + _i; \
1641 if ( shadow_l4e_get_flags(*(_sl4e)) & _PAGE_PRESENT ) \
1642 {_code} \
1643 if ( _done ) break; \
1644 } \
1645 increment_ptr_to_guest_entry(_gl4p); \
1646 } \
1647 sh_unmap_domain_page(_sp); \
1648 } while (0)
1650 #endif
1654 /**************************************************************************/
1655 /* Functions to install Xen mappings and linear mappings in shadow pages */
1657 // XXX -- this function should probably be moved to shadow-common.c, but that
1658 // probably wants to wait until the shadow types have been moved from
1659 // shadow-types.h to shadow-private.h
1660 //
1661 #if CONFIG_PAGING_LEVELS == 4 && GUEST_PAGING_LEVELS == 4
1662 void sh_install_xen_entries_in_l4(struct vcpu *v, mfn_t gl4mfn, mfn_t sl4mfn)
1664 struct domain *d = v->domain;
1665 shadow_l4e_t *sl4e;
1667 sl4e = sh_map_domain_page(sl4mfn);
1668 ASSERT(sl4e != NULL);
1669 ASSERT(sizeof (l4_pgentry_t) == sizeof (shadow_l4e_t));
1671 /* Copy the common Xen mappings from the idle domain */
1672 memcpy(&sl4e[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1673 &idle_pg_table[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1674 ROOT_PAGETABLE_XEN_SLOTS * sizeof(l4_pgentry_t));
1676 /* Install the per-domain mappings for this domain */
1677 sl4e[shadow_l4_table_offset(PERDOMAIN_VIRT_START)] =
1678 shadow_l4e_from_mfn(page_to_mfn(virt_to_page(d->arch.mm_perdomain_l3)),
1679 __PAGE_HYPERVISOR);
1681 /* Shadow linear mapping for 4-level shadows. N.B. for 3-level
1682 * shadows on 64-bit xen, this linear mapping is later replaced by the
1683 * monitor pagetable structure, which is built in make_monitor_table
1684 * and maintained by sh_update_linear_entries. */
1685 sl4e[shadow_l4_table_offset(SH_LINEAR_PT_VIRT_START)] =
1686 shadow_l4e_from_mfn(sl4mfn, __PAGE_HYPERVISOR);
1688 /* Self linear mapping. */
1689 if ( shadow_mode_translate(v->domain) && !shadow_mode_external(v->domain) )
1691 // linear tables may not be used with translated PV guests
1692 sl4e[shadow_l4_table_offset(LINEAR_PT_VIRT_START)] =
1693 shadow_l4e_empty();
1695 else
1697 sl4e[shadow_l4_table_offset(LINEAR_PT_VIRT_START)] =
1698 shadow_l4e_from_mfn(gl4mfn, __PAGE_HYPERVISOR);
1701 if ( shadow_mode_translate(v->domain) )
1703 /* install domain-specific P2M table */
1704 sl4e[shadow_l4_table_offset(RO_MPT_VIRT_START)] =
1705 shadow_l4e_from_mfn(pagetable_get_mfn(d->arch.phys_table),
1706 __PAGE_HYPERVISOR);
1709 sh_unmap_domain_page(sl4e);
1711 #endif
1713 #if CONFIG_PAGING_LEVELS >= 3 && GUEST_PAGING_LEVELS >= 3
1714 // For 3-on-3 PV guests, we need to make sure the xen mappings are in
1715 // place, which means that we need to populate the l2h entry in the l3
1716 // table.
1718 static void sh_install_xen_entries_in_l2h(struct vcpu *v, mfn_t sl2hmfn)
1720 struct domain *d = v->domain;
1721 shadow_l2e_t *sl2e;
1722 #if CONFIG_PAGING_LEVELS == 3
1723 int i;
1724 #else
1726 if ( !is_pv_32on64_vcpu(v) )
1727 return;
1728 #endif
1730 sl2e = sh_map_domain_page(sl2hmfn);
1731 ASSERT(sl2e != NULL);
1732 ASSERT(sizeof (l2_pgentry_t) == sizeof (shadow_l2e_t));
1734 #if CONFIG_PAGING_LEVELS == 3
1736 /* Copy the common Xen mappings from the idle domain */
1737 memcpy(&sl2e[L2_PAGETABLE_FIRST_XEN_SLOT & (L2_PAGETABLE_ENTRIES-1)],
1738 &idle_pg_table_l2[L2_PAGETABLE_FIRST_XEN_SLOT],
1739 L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t));
1741 /* Install the per-domain mappings for this domain */
1742 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
1743 sl2e[shadow_l2_table_offset(PERDOMAIN_VIRT_START) + i] =
1744 shadow_l2e_from_mfn(
1745 page_to_mfn(virt_to_page(d->arch.mm_perdomain_pt) + i),
1746 __PAGE_HYPERVISOR);
1748 /* We don't set up a linear mapping here because we can't until this
1749 * l2h is installed in an l3e. sh_update_linear_entries() handles
1750 * the linear mappings when CR3 (and so the fourth l3e) is loaded.
1751 * We zero them here, just as a safety measure.
1752 */
1753 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
1754 sl2e[shadow_l2_table_offset(LINEAR_PT_VIRT_START) + i] =
1755 shadow_l2e_empty();
1756 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
1757 sl2e[shadow_l2_table_offset(SH_LINEAR_PT_VIRT_START) + i] =
1758 shadow_l2e_empty();
1760 if ( shadow_mode_translate(d) )
1762 /* Install the domain-specific p2m table */
1763 l3_pgentry_t *p2m;
1764 ASSERT(pagetable_get_pfn(d->arch.phys_table) != 0);
1765 p2m = sh_map_domain_page(pagetable_get_mfn(d->arch.phys_table));
1766 for ( i = 0; i < MACHPHYS_MBYTES>>1; i++ )
1768 sl2e[shadow_l2_table_offset(RO_MPT_VIRT_START) + i] =
1769 (l3e_get_flags(p2m[i]) & _PAGE_PRESENT)
1770 ? shadow_l2e_from_mfn(_mfn(l3e_get_pfn(p2m[i])),
1771 __PAGE_HYPERVISOR)
1772 : shadow_l2e_empty();
1774 sh_unmap_domain_page(p2m);
1777 #else
1779 /* Copy the common Xen mappings from the idle domain */
1780 memcpy(
1781 &sl2e[COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(d)],
1782 &compat_idle_pg_table_l2[l2_table_offset(HIRO_COMPAT_MPT_VIRT_START)],
1783 COMPAT_L2_PAGETABLE_XEN_SLOTS(d) * sizeof(*sl2e));
1785 #endif
1787 sh_unmap_domain_page(sl2e);
1789 #endif
1795 /**************************************************************************/
1796 /* Create a shadow of a given guest page.
1797 */
1798 static mfn_t
1799 sh_make_shadow(struct vcpu *v, mfn_t gmfn, u32 shadow_type)
1801 mfn_t smfn = shadow_alloc(v->domain, shadow_type, mfn_x(gmfn));
1802 SHADOW_DEBUG(MAKE_SHADOW, "(%05lx, %u)=>%05lx\n",
1803 mfn_x(gmfn), shadow_type, mfn_x(smfn));
1805 if ( shadow_type != SH_type_l2_32_shadow
1806 && shadow_type != SH_type_l2_pae_shadow
1807 && shadow_type != SH_type_l2h_pae_shadow
1808 && shadow_type != SH_type_l4_64_shadow )
1809 /* Lower-level shadow, not yet linked form a higher level */
1810 mfn_to_shadow_page(smfn)->up = 0;
1812 #if GUEST_PAGING_LEVELS == 4
1813 #if (SHADOW_OPTIMIZATIONS & SHOPT_LINUX_L3_TOPLEVEL)
1814 if ( shadow_type == SH_type_l4_64_shadow &&
1815 unlikely(v->domain->arch.paging.shadow.opt_flags & SHOPT_LINUX_L3_TOPLEVEL) )
1817 /* We're shadowing a new l4, but we've been assuming the guest uses
1818 * only one l4 per vcpu and context switches using an l4 entry.
1819 * Count the number of active l4 shadows. If there are enough
1820 * of them, decide that this isn't an old linux guest, and stop
1821 * pinning l3es. This is not very quick but it doesn't happen
1822 * very often. */
1823 struct list_head *l, *t;
1824 struct shadow_page_info *sp;
1825 struct vcpu *v2;
1826 int l4count = 0, vcpus = 0;
1827 list_for_each(l, &v->domain->arch.paging.shadow.pinned_shadows)
1829 sp = list_entry(l, struct shadow_page_info, list);
1830 if ( sp->type == SH_type_l4_64_shadow )
1831 l4count++;
1833 for_each_vcpu ( v->domain, v2 )
1834 vcpus++;
1835 if ( l4count > 2 * vcpus )
1837 /* Unpin all the pinned l3 tables, and don't pin any more. */
1838 list_for_each_safe(l, t, &v->domain->arch.paging.shadow.pinned_shadows)
1840 sp = list_entry(l, struct shadow_page_info, list);
1841 if ( sp->type == SH_type_l3_64_shadow )
1842 sh_unpin(v, shadow_page_to_mfn(sp));
1844 v->domain->arch.paging.shadow.opt_flags &= ~SHOPT_LINUX_L3_TOPLEVEL;
1847 #endif
1848 #endif
1850 // Create the Xen mappings...
1851 if ( !shadow_mode_external(v->domain) )
1853 switch (shadow_type)
1855 #if CONFIG_PAGING_LEVELS == 4 && GUEST_PAGING_LEVELS == 4
1856 case SH_type_l4_shadow:
1857 sh_install_xen_entries_in_l4(v, gmfn, smfn); break;
1858 #endif
1859 #if CONFIG_PAGING_LEVELS >= 3 && GUEST_PAGING_LEVELS >= 3
1860 case SH_type_l2h_shadow:
1861 sh_install_xen_entries_in_l2h(v, smfn); break;
1862 #endif
1863 default: /* Do nothing */ break;
1867 shadow_promote(v, gmfn, shadow_type);
1868 set_shadow_status(v, gmfn, shadow_type, smfn);
1870 return smfn;
1873 /* Make a splintered superpage shadow */
1874 static mfn_t
1875 make_fl1_shadow(struct vcpu *v, gfn_t gfn)
1877 mfn_t smfn = shadow_alloc(v->domain, SH_type_fl1_shadow,
1878 (unsigned long) gfn_x(gfn));
1880 SHADOW_DEBUG(MAKE_SHADOW, "(%" SH_PRI_gfn ")=>%" PRI_mfn "\n",
1881 gfn_x(gfn), mfn_x(smfn));
1883 set_fl1_shadow_status(v, gfn, smfn);
1884 return smfn;
1888 #if SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS
1889 mfn_t
1890 sh_make_monitor_table(struct vcpu *v)
1892 struct domain *d = v->domain;
1894 ASSERT(pagetable_get_pfn(v->arch.monitor_table) == 0);
1896 /* Guarantee we can get the memory we need */
1897 shadow_prealloc(d, SH_type_monitor_table, CONFIG_PAGING_LEVELS);
1899 #if CONFIG_PAGING_LEVELS == 4
1901 mfn_t m4mfn;
1902 m4mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1903 sh_install_xen_entries_in_l4(v, m4mfn, m4mfn);
1904 /* Remember the level of this table */
1905 mfn_to_page(m4mfn)->shadow_flags = 4;
1906 #if SHADOW_PAGING_LEVELS < 4
1908 mfn_t m3mfn, m2mfn;
1909 l4_pgentry_t *l4e;
1910 l3_pgentry_t *l3e;
1911 /* Install an l3 table and an l2 table that will hold the shadow
1912 * linear map entries. This overrides the linear map entry that
1913 * was installed by sh_install_xen_entries_in_l4. */
1914 l4e = sh_map_domain_page(m4mfn);
1916 m3mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1917 mfn_to_page(m3mfn)->shadow_flags = 3;
1918 l4e[shadow_l4_table_offset(SH_LINEAR_PT_VIRT_START)]
1919 = l4e_from_pfn(mfn_x(m3mfn), __PAGE_HYPERVISOR);
1921 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1922 mfn_to_page(m2mfn)->shadow_flags = 2;
1923 l3e = sh_map_domain_page(m3mfn);
1924 l3e[0] = l3e_from_pfn(mfn_x(m2mfn), __PAGE_HYPERVISOR);
1925 sh_unmap_domain_page(l3e);
1927 if ( is_pv_32on64_vcpu(v) )
1929 /* For 32-on-64 PV guests, we need to map the 32-bit Xen
1930 * area into its usual VAs in the monitor tables */
1931 m3mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1932 mfn_to_page(m3mfn)->shadow_flags = 3;
1933 l4e[0] = l4e_from_pfn(mfn_x(m3mfn), __PAGE_HYPERVISOR);
1935 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1936 mfn_to_page(m2mfn)->shadow_flags = 2;
1937 l3e = sh_map_domain_page(m3mfn);
1938 l3e[3] = l3e_from_pfn(mfn_x(m2mfn), _PAGE_PRESENT);
1939 sh_install_xen_entries_in_l2h(v, m2mfn);
1940 sh_unmap_domain_page(l3e);
1943 sh_unmap_domain_page(l4e);
1945 #endif /* SHADOW_PAGING_LEVELS < 4 */
1946 return m4mfn;
1949 #elif CONFIG_PAGING_LEVELS == 3
1952 mfn_t m3mfn, m2mfn;
1953 l3_pgentry_t *l3e;
1954 l2_pgentry_t *l2e;
1955 int i;
1957 m3mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1958 /* Remember the level of this table */
1959 mfn_to_page(m3mfn)->shadow_flags = 3;
1961 // Install a monitor l2 table in slot 3 of the l3 table.
1962 // This is used for all Xen entries, including linear maps
1963 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1964 mfn_to_page(m2mfn)->shadow_flags = 2;
1965 l3e = sh_map_domain_page(m3mfn);
1966 l3e[3] = l3e_from_pfn(mfn_x(m2mfn), _PAGE_PRESENT);
1967 sh_install_xen_entries_in_l2h(v, m2mfn);
1968 /* Install the monitor's own linear map */
1969 l2e = sh_map_domain_page(m2mfn);
1970 for ( i = 0; i < L3_PAGETABLE_ENTRIES; i++ )
1971 l2e[l2_table_offset(LINEAR_PT_VIRT_START) + i] =
1972 (l3e_get_flags(l3e[i]) & _PAGE_PRESENT)
1973 ? l2e_from_pfn(l3e_get_pfn(l3e[i]), __PAGE_HYPERVISOR)
1974 : l2e_empty();
1975 sh_unmap_domain_page(l2e);
1976 sh_unmap_domain_page(l3e);
1978 SHADOW_PRINTK("new monitor table: %#lx\n", mfn_x(m3mfn));
1979 return m3mfn;
1982 #else
1983 #error this should not happen
1984 #endif /* CONFIG_PAGING_LEVELS */
1986 #endif /* SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS */
1988 /**************************************************************************/
1989 /* These functions also take a virtual address and return the level-N
1990 * shadow table mfn and entry, but they create the shadow pagetables if
1991 * they are needed. The "demand" argument is non-zero when handling
1992 * a demand fault (so we know what to do about accessed bits &c).
1993 * If the necessary tables are not present in the guest, they return NULL. */
1995 /* N.B. The use of GUEST_PAGING_LEVELS here is correct. If the shadow has
1996 * more levels than the guest, the upper levels are always fixed and do not
1997 * reflect any information from the guest, so we do not use these functions
1998 * to access them. */
2000 #if GUEST_PAGING_LEVELS >= 4
2001 static shadow_l4e_t * shadow_get_and_create_l4e(struct vcpu *v,
2002 walk_t *gw,
2003 mfn_t *sl4mfn)
2005 /* There is always a shadow of the top level table. Get it. */
2006 *sl4mfn = pagetable_get_mfn(v->arch.shadow_table[0]);
2007 /* Reading the top level table is always valid. */
2008 return sh_linear_l4_table(v) + shadow_l4_linear_offset(gw->va);
2011 static shadow_l3e_t * shadow_get_and_create_l3e(struct vcpu *v,
2012 walk_t *gw,
2013 mfn_t *sl3mfn,
2014 fetch_type_t ft)
2016 mfn_t sl4mfn;
2017 shadow_l4e_t *sl4e;
2018 if ( !mfn_valid(gw->l3mfn) ) return NULL; /* No guest page. */
2019 /* Get the l4e */
2020 sl4e = shadow_get_and_create_l4e(v, gw, &sl4mfn);
2021 ASSERT(sl4e != NULL);
2022 if ( shadow_l4e_get_flags(*sl4e) & _PAGE_PRESENT )
2024 *sl3mfn = shadow_l4e_get_mfn(*sl4e);
2025 ASSERT(mfn_valid(*sl3mfn));
2027 else
2029 int r;
2030 shadow_l4e_t new_sl4e;
2031 /* No l3 shadow installed: find and install it. */
2032 *sl3mfn = get_shadow_status(v, gw->l3mfn, SH_type_l3_shadow);
2033 if ( !mfn_valid(*sl3mfn) )
2035 /* No l3 shadow of this page exists at all: make one. */
2036 *sl3mfn = sh_make_shadow(v, gw->l3mfn, SH_type_l3_shadow);
2038 /* Install the new sl3 table in the sl4e */
2039 l4e_propagate_from_guest(v, gw->l4e, *sl3mfn, &new_sl4e, ft);
2040 r = shadow_set_l4e(v, sl4e, new_sl4e, sl4mfn);
2041 ASSERT((r & SHADOW_SET_FLUSH) == 0);
2042 if ( r & SHADOW_SET_ERROR )
2043 return NULL;
2045 /* Now follow it down a level. Guaranteed to succeed. */
2046 return sh_linear_l3_table(v) + shadow_l3_linear_offset(gw->va);
2048 #endif /* GUEST_PAGING_LEVELS >= 4 */
2051 static shadow_l2e_t * shadow_get_and_create_l2e(struct vcpu *v,
2052 walk_t *gw,
2053 mfn_t *sl2mfn,
2054 fetch_type_t ft)
2056 #if GUEST_PAGING_LEVELS >= 4 /* 64bit... */
2057 mfn_t sl3mfn = _mfn(INVALID_MFN);
2058 shadow_l3e_t *sl3e;
2059 if ( !mfn_valid(gw->l2mfn) ) return NULL; /* No guest page. */
2060 /* Get the l3e */
2061 sl3e = shadow_get_and_create_l3e(v, gw, &sl3mfn, ft);
2062 if ( sl3e == NULL ) return NULL;
2063 if ( shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT )
2065 *sl2mfn = shadow_l3e_get_mfn(*sl3e);
2066 ASSERT(mfn_valid(*sl2mfn));
2068 else
2070 int r;
2071 shadow_l3e_t new_sl3e;
2072 unsigned int t = SH_type_l2_shadow;
2074 /* Tag compat L2 containing hypervisor (m2p) mappings */
2075 if ( is_pv_32on64_domain(v->domain) &&
2076 guest_l4_table_offset(gw->va) == 0 &&
2077 guest_l3_table_offset(gw->va) == 3 )
2078 t = SH_type_l2h_shadow;
2080 /* No l2 shadow installed: find and install it. */
2081 *sl2mfn = get_shadow_status(v, gw->l2mfn, t);
2082 if ( !mfn_valid(*sl2mfn) )
2084 /* No l2 shadow of this page exists at all: make one. */
2085 *sl2mfn = sh_make_shadow(v, gw->l2mfn, t);
2087 /* Install the new sl2 table in the sl3e */
2088 l3e_propagate_from_guest(v, gw->l3e, *sl2mfn, &new_sl3e, ft);
2089 r = shadow_set_l3e(v, sl3e, new_sl3e, sl3mfn);
2090 ASSERT((r & SHADOW_SET_FLUSH) == 0);
2091 if ( r & SHADOW_SET_ERROR )
2092 return NULL;
2094 /* Now follow it down a level. Guaranteed to succeed. */
2095 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
2096 #elif GUEST_PAGING_LEVELS == 3 /* PAE... */
2097 /* We never demand-shadow PAE l3es: they are only created in
2098 * sh_update_cr3(). Check if the relevant sl3e is present. */
2099 shadow_l3e_t *sl3e = ((shadow_l3e_t *)&v->arch.paging.shadow.l3table)
2100 + shadow_l3_linear_offset(gw->va);
2101 if ( !(shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT) )
2102 return NULL;
2103 *sl2mfn = shadow_l3e_get_mfn(*sl3e);
2104 ASSERT(mfn_valid(*sl2mfn));
2105 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
2106 #else /* 32bit... */
2107 /* There is always a shadow of the top level table. Get it. */
2108 *sl2mfn = pagetable_get_mfn(v->arch.shadow_table[0]);
2109 /* This next line is important: the guest l2 has a 16k
2110 * shadow, we need to return the right mfn of the four. This
2111 * call will set it for us as a side-effect. */
2112 (void) shadow_l2_index(sl2mfn, guest_l2_table_offset(gw->va));
2113 /* Reading the top level table is always valid. */
2114 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
2115 #endif
2119 static shadow_l1e_t * shadow_get_and_create_l1e(struct vcpu *v,
2120 walk_t *gw,
2121 mfn_t *sl1mfn,
2122 fetch_type_t ft)
2124 mfn_t sl2mfn;
2125 shadow_l2e_t *sl2e;
2127 /* Get the l2e */
2128 sl2e = shadow_get_and_create_l2e(v, gw, &sl2mfn, ft);
2129 if ( sl2e == NULL ) return NULL;
2130 /* Install the sl1 in the l2e if it wasn't there or if we need to
2131 * re-do it to fix a PSE dirty bit. */
2132 if ( shadow_l2e_get_flags(*sl2e) & _PAGE_PRESENT
2133 && likely(ft != ft_demand_write
2134 || (shadow_l2e_get_flags(*sl2e) & _PAGE_RW)
2135 || !(guest_l2e_get_flags(gw->l2e) & _PAGE_PSE)) )
2137 *sl1mfn = shadow_l2e_get_mfn(*sl2e);
2138 ASSERT(mfn_valid(*sl1mfn));
2140 else
2142 shadow_l2e_t new_sl2e;
2143 int r, flags = guest_l2e_get_flags(gw->l2e);
2144 /* No l1 shadow installed: find and install it. */
2145 if ( !(flags & _PAGE_PRESENT) )
2146 return NULL; /* No guest page. */
2147 if ( guest_supports_superpages(v) && (flags & _PAGE_PSE) )
2149 /* Splintering a superpage */
2150 gfn_t l2gfn = guest_l2e_get_gfn(gw->l2e);
2151 *sl1mfn = get_fl1_shadow_status(v, l2gfn);
2152 if ( !mfn_valid(*sl1mfn) )
2154 /* No fl1 shadow of this superpage exists at all: make one. */
2155 *sl1mfn = make_fl1_shadow(v, l2gfn);
2158 else
2160 /* Shadowing an actual guest l1 table */
2161 if ( !mfn_valid(gw->l1mfn) ) return NULL; /* No guest page. */
2162 *sl1mfn = get_shadow_status(v, gw->l1mfn, SH_type_l1_shadow);
2163 if ( !mfn_valid(*sl1mfn) )
2165 /* No l1 shadow of this page exists at all: make one. */
2166 *sl1mfn = sh_make_shadow(v, gw->l1mfn, SH_type_l1_shadow);
2169 /* Install the new sl1 table in the sl2e */
2170 l2e_propagate_from_guest(v, gw->l2e, *sl1mfn, &new_sl2e, ft);
2171 r = shadow_set_l2e(v, sl2e, new_sl2e, sl2mfn);
2172 ASSERT((r & SHADOW_SET_FLUSH) == 0);
2173 if ( r & SHADOW_SET_ERROR )
2174 return NULL;
2175 /* This next line is important: in 32-on-PAE and 32-on-64 modes,
2176 * the guest l1 table has an 8k shadow, and we need to return
2177 * the right mfn of the pair. This call will set it for us as a
2178 * side-effect. (In all other cases, it's a no-op and will be
2179 * compiled out.) */
2180 (void) shadow_l1_index(sl1mfn, guest_l1_table_offset(gw->va));
2182 /* Now follow it down a level. Guaranteed to succeed. */
2183 return sh_linear_l1_table(v) + shadow_l1_linear_offset(gw->va);
2188 /**************************************************************************/
2189 /* Destructors for shadow tables:
2190 * Unregister the shadow, decrement refcounts of any entries present in it,
2191 * and release the memory.
2193 * N.B. These destructors do not clear the contents of the shadows.
2194 * This allows us to delay TLB shootdowns until the page is being reused.
2195 * See shadow_alloc() and shadow_free() for how this is handled.
2196 */
2198 #if GUEST_PAGING_LEVELS >= 4
2199 void sh_destroy_l4_shadow(struct vcpu *v, mfn_t smfn)
2201 shadow_l4e_t *sl4e;
2202 u32 t = mfn_to_shadow_page(smfn)->type;
2203 mfn_t gmfn, sl4mfn;
2205 SHADOW_DEBUG(DESTROY_SHADOW,
2206 "%s(%05lx)\n", __func__, mfn_x(smfn));
2207 ASSERT(t == SH_type_l4_shadow);
2209 /* Record that the guest page isn't shadowed any more (in this type) */
2210 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2211 delete_shadow_status(v, gmfn, t, smfn);
2212 shadow_demote(v, gmfn, t);
2213 /* Decrement refcounts of all the old entries */
2214 sl4mfn = smfn;
2215 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, 0, v->domain, {
2216 if ( shadow_l4e_get_flags(*sl4e) & _PAGE_PRESENT )
2218 sh_put_ref(v, shadow_l4e_get_mfn(*sl4e),
2219 (((paddr_t)mfn_x(sl4mfn)) << PAGE_SHIFT)
2220 | ((unsigned long)sl4e & ~PAGE_MASK));
2222 });
2224 /* Put the memory back in the pool */
2225 shadow_free(v->domain, smfn);
2228 void sh_destroy_l3_shadow(struct vcpu *v, mfn_t smfn)
2230 shadow_l3e_t *sl3e;
2231 u32 t = mfn_to_shadow_page(smfn)->type;
2232 mfn_t gmfn, sl3mfn;
2234 SHADOW_DEBUG(DESTROY_SHADOW,
2235 "%s(%05lx)\n", __func__, mfn_x(smfn));
2236 ASSERT(t == SH_type_l3_shadow);
2238 /* Record that the guest page isn't shadowed any more (in this type) */
2239 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2240 delete_shadow_status(v, gmfn, t, smfn);
2241 shadow_demote(v, gmfn, t);
2243 /* Decrement refcounts of all the old entries */
2244 sl3mfn = smfn;
2245 SHADOW_FOREACH_L3E(sl3mfn, sl3e, 0, 0, {
2246 if ( shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT )
2247 sh_put_ref(v, shadow_l3e_get_mfn(*sl3e),
2248 (((paddr_t)mfn_x(sl3mfn)) << PAGE_SHIFT)
2249 | ((unsigned long)sl3e & ~PAGE_MASK));
2250 });
2252 /* Put the memory back in the pool */
2253 shadow_free(v->domain, smfn);
2255 #endif /* GUEST_PAGING_LEVELS >= 4 */
2258 void sh_destroy_l2_shadow(struct vcpu *v, mfn_t smfn)
2260 shadow_l2e_t *sl2e;
2261 u32 t = mfn_to_shadow_page(smfn)->type;
2262 mfn_t gmfn, sl2mfn;
2264 SHADOW_DEBUG(DESTROY_SHADOW,
2265 "%s(%05lx)\n", __func__, mfn_x(smfn));
2267 #if GUEST_PAGING_LEVELS >= 3
2268 ASSERT(t == SH_type_l2_shadow || t == SH_type_l2h_shadow);
2269 #else
2270 ASSERT(t == SH_type_l2_shadow);
2271 #endif
2273 /* Record that the guest page isn't shadowed any more (in this type) */
2274 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2275 delete_shadow_status(v, gmfn, t, smfn);
2276 shadow_demote(v, gmfn, t);
2278 /* Decrement refcounts of all the old entries */
2279 sl2mfn = smfn;
2280 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, v->domain, {
2281 if ( shadow_l2e_get_flags(*sl2e) & _PAGE_PRESENT )
2282 sh_put_ref(v, shadow_l2e_get_mfn(*sl2e),
2283 (((paddr_t)mfn_x(sl2mfn)) << PAGE_SHIFT)
2284 | ((unsigned long)sl2e & ~PAGE_MASK));
2285 });
2287 /* Put the memory back in the pool */
2288 shadow_free(v->domain, smfn);
2291 void sh_destroy_l1_shadow(struct vcpu *v, mfn_t smfn)
2293 struct domain *d = v->domain;
2294 shadow_l1e_t *sl1e;
2295 u32 t = mfn_to_shadow_page(smfn)->type;
2297 SHADOW_DEBUG(DESTROY_SHADOW,
2298 "%s(%05lx)\n", __func__, mfn_x(smfn));
2299 ASSERT(t == SH_type_l1_shadow || t == SH_type_fl1_shadow);
2301 /* Record that the guest page isn't shadowed any more (in this type) */
2302 if ( t == SH_type_fl1_shadow )
2304 gfn_t gfn = _gfn(mfn_to_shadow_page(smfn)->backpointer);
2305 delete_fl1_shadow_status(v, gfn, smfn);
2307 else
2309 mfn_t gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2310 delete_shadow_status(v, gmfn, t, smfn);
2311 shadow_demote(v, gmfn, t);
2314 if ( shadow_mode_refcounts(d) )
2316 /* Decrement refcounts of all the old entries */
2317 mfn_t sl1mfn = smfn;
2318 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, 0, {
2319 if ( (shadow_l1e_get_flags(*sl1e) & _PAGE_PRESENT)
2320 && !sh_l1e_is_magic(*sl1e) ) {
2321 shadow_vram_put_l1e(*sl1e, sl1e, sl1mfn, d);
2322 shadow_put_page_from_l1e(*sl1e, d);
2324 });
2327 /* Put the memory back in the pool */
2328 shadow_free(v->domain, smfn);
2331 #if SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS
2332 void sh_destroy_monitor_table(struct vcpu *v, mfn_t mmfn)
2334 struct domain *d = v->domain;
2335 ASSERT(mfn_to_shadow_page(mmfn)->type == SH_type_monitor_table);
2337 #if (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS != 4)
2339 mfn_t m3mfn;
2340 l4_pgentry_t *l4e = sh_map_domain_page(mmfn);
2341 l3_pgentry_t *l3e;
2342 int linear_slot = shadow_l4_table_offset(SH_LINEAR_PT_VIRT_START);
2344 /* Need to destroy the l3 and l2 monitor pages used
2345 * for the linear map */
2346 ASSERT(l4e_get_flags(l4e[linear_slot]) & _PAGE_PRESENT);
2347 m3mfn = _mfn(l4e_get_pfn(l4e[linear_slot]));
2348 l3e = sh_map_domain_page(m3mfn);
2349 ASSERT(l3e_get_flags(l3e[0]) & _PAGE_PRESENT);
2350 shadow_free(d, _mfn(l3e_get_pfn(l3e[0])));
2351 sh_unmap_domain_page(l3e);
2352 shadow_free(d, m3mfn);
2354 if ( is_pv_32on64_vcpu(v) )
2356 /* Need to destroy the l3 and l2 monitor pages that map the
2357 * Xen VAs at 3GB-4GB */
2358 ASSERT(l4e_get_flags(l4e[0]) & _PAGE_PRESENT);
2359 m3mfn = _mfn(l4e_get_pfn(l4e[0]));
2360 l3e = sh_map_domain_page(m3mfn);
2361 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
2362 shadow_free(d, _mfn(l3e_get_pfn(l3e[3])));
2363 sh_unmap_domain_page(l3e);
2364 shadow_free(d, m3mfn);
2366 sh_unmap_domain_page(l4e);
2368 #elif CONFIG_PAGING_LEVELS == 3
2369 /* Need to destroy the l2 monitor page in slot 4 too */
2371 l3_pgentry_t *l3e = sh_map_domain_page(mmfn);
2372 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
2373 shadow_free(d, _mfn(l3e_get_pfn(l3e[3])));
2374 sh_unmap_domain_page(l3e);
2376 #endif
2378 /* Put the memory back in the pool */
2379 shadow_free(d, mmfn);
2381 #endif
2383 /**************************************************************************/
2384 /* Functions to destroy non-Xen mappings in a pagetable hierarchy.
2385 * These are called from common code when we are running out of shadow
2386 * memory, and unpinning all the top-level shadows hasn't worked.
2388 * This implementation is pretty crude and slow, but we hope that it won't
2389 * be called very often. */
2391 #if GUEST_PAGING_LEVELS == 2
2393 void sh_unhook_32b_mappings(struct vcpu *v, mfn_t sl2mfn)
2395 shadow_l2e_t *sl2e;
2396 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, v->domain, {
2397 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
2398 });
2401 #elif GUEST_PAGING_LEVELS == 3
2403 void sh_unhook_pae_mappings(struct vcpu *v, mfn_t sl2mfn)
2404 /* Walk a PAE l2 shadow, unhooking entries from all the subshadows */
2406 shadow_l2e_t *sl2e;
2407 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, v->domain, {
2408 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
2409 });
2412 #elif GUEST_PAGING_LEVELS == 4
2414 void sh_unhook_64b_mappings(struct vcpu *v, mfn_t sl4mfn)
2416 shadow_l4e_t *sl4e;
2417 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, 0, v->domain, {
2418 (void) shadow_set_l4e(v, sl4e, shadow_l4e_empty(), sl4mfn);
2419 });
2422 #endif
2424 /**************************************************************************/
2425 /* Internal translation functions.
2426 * These functions require a pointer to the shadow entry that will be updated.
2427 */
2429 /* These functions take a new guest entry, translate it to shadow and write
2430 * the shadow entry.
2432 * They return the same bitmaps as the shadow_set_lXe() functions.
2433 */
2435 #if GUEST_PAGING_LEVELS >= 4
2436 static int validate_gl4e(struct vcpu *v, void *new_ge, mfn_t sl4mfn, void *se)
2438 shadow_l4e_t new_sl4e;
2439 guest_l4e_t new_gl4e = *(guest_l4e_t *)new_ge;
2440 shadow_l4e_t *sl4p = se;
2441 mfn_t sl3mfn = _mfn(INVALID_MFN);
2442 struct domain *d = v->domain;
2443 p2m_type_t p2mt;
2444 int result = 0;
2446 perfc_incr(shadow_validate_gl4e_calls);
2448 if ( guest_l4e_get_flags(new_gl4e) & _PAGE_PRESENT )
2450 gfn_t gl3gfn = guest_l4e_get_gfn(new_gl4e);
2451 mfn_t gl3mfn = gfn_to_mfn(d, gl3gfn, &p2mt);
2452 if ( p2m_is_ram(p2mt) )
2453 sl3mfn = get_shadow_status(v, gl3mfn, SH_type_l3_shadow);
2454 else
2455 result |= SHADOW_SET_ERROR;
2457 l4e_propagate_from_guest(v, new_gl4e, sl3mfn, &new_sl4e, ft_prefetch);
2459 // check for updates to xen reserved slots
2460 if ( !shadow_mode_external(d) )
2462 int shadow_index = (((unsigned long)sl4p & ~PAGE_MASK) /
2463 sizeof(shadow_l4e_t));
2464 int reserved_xen_slot = !is_guest_l4_slot(d, shadow_index);
2466 if ( unlikely(reserved_xen_slot) )
2468 // attempt by the guest to write to a xen reserved slot
2469 //
2470 SHADOW_PRINTK("%s out-of-range update "
2471 "sl4mfn=%05lx index=0x%x val=%" SH_PRI_pte "\n",
2472 __func__, mfn_x(sl4mfn), shadow_index, new_sl4e.l4);
2473 if ( shadow_l4e_get_flags(new_sl4e) & _PAGE_PRESENT )
2475 SHADOW_ERROR("out-of-range l4e update\n");
2476 result |= SHADOW_SET_ERROR;
2479 // do not call shadow_set_l4e...
2480 return result;
2484 result |= shadow_set_l4e(v, sl4p, new_sl4e, sl4mfn);
2485 return result;
2489 static int validate_gl3e(struct vcpu *v, void *new_ge, mfn_t sl3mfn, void *se)
2491 shadow_l3e_t new_sl3e;
2492 guest_l3e_t new_gl3e = *(guest_l3e_t *)new_ge;
2493 shadow_l3e_t *sl3p = se;
2494 mfn_t sl2mfn = _mfn(INVALID_MFN);
2495 p2m_type_t p2mt;
2496 int result = 0;
2498 perfc_incr(shadow_validate_gl3e_calls);
2500 if ( guest_l3e_get_flags(new_gl3e) & _PAGE_PRESENT )
2502 gfn_t gl2gfn = guest_l3e_get_gfn(new_gl3e);
2503 mfn_t gl2mfn = gfn_to_mfn(v->domain, gl2gfn, &p2mt);
2504 if ( p2m_is_ram(p2mt) )
2505 sl2mfn = get_shadow_status(v, gl2mfn, SH_type_l2_shadow);
2506 else
2507 result |= SHADOW_SET_ERROR;
2509 l3e_propagate_from_guest(v, new_gl3e, sl2mfn, &new_sl3e, ft_prefetch);
2510 result |= shadow_set_l3e(v, sl3p, new_sl3e, sl3mfn);
2512 return result;
2514 #endif // GUEST_PAGING_LEVELS >= 4
2516 static int validate_gl2e(struct vcpu *v, void *new_ge, mfn_t sl2mfn, void *se)
2518 shadow_l2e_t new_sl2e;
2519 guest_l2e_t new_gl2e = *(guest_l2e_t *)new_ge;
2520 shadow_l2e_t *sl2p = se;
2521 mfn_t sl1mfn = _mfn(INVALID_MFN);
2522 p2m_type_t p2mt;
2523 int result = 0;
2525 perfc_incr(shadow_validate_gl2e_calls);
2527 if ( guest_l2e_get_flags(new_gl2e) & _PAGE_PRESENT )
2529 gfn_t gl1gfn = guest_l2e_get_gfn(new_gl2e);
2530 if ( guest_supports_superpages(v) &&
2531 (guest_l2e_get_flags(new_gl2e) & _PAGE_PSE) )
2533 // superpage -- need to look up the shadow L1 which holds the
2534 // splitters...
2535 sl1mfn = get_fl1_shadow_status(v, gl1gfn);
2536 #if 0
2537 // XXX - it's possible that we want to do some kind of prefetch
2538 // for superpage fl1's here, but this is *not* on the demand path,
2539 // so we'll hold off trying that for now...
2540 //
2541 if ( !mfn_valid(sl1mfn) )
2542 sl1mfn = make_fl1_shadow(v, gl1gfn);
2543 #endif
2545 else
2547 mfn_t gl1mfn = gfn_to_mfn(v->domain, gl1gfn, &p2mt);
2548 if ( p2m_is_ram(p2mt) )
2549 sl1mfn = get_shadow_status(v, gl1mfn, SH_type_l1_shadow);
2550 else
2551 result |= SHADOW_SET_ERROR;
2554 l2e_propagate_from_guest(v, new_gl2e, sl1mfn, &new_sl2e, ft_prefetch);
2556 // check for updates to xen reserved slots in PV guests...
2557 // XXX -- need to revisit this for PV 3-on-4 guests.
2558 //
2559 #if SHADOW_PAGING_LEVELS < 4
2560 #if CONFIG_PAGING_LEVELS == SHADOW_PAGING_LEVELS
2561 if ( !shadow_mode_external(v->domain) )
2563 int shadow_index = (((unsigned long)sl2p & ~PAGE_MASK) /
2564 sizeof(shadow_l2e_t));
2565 int reserved_xen_slot;
2567 #if SHADOW_PAGING_LEVELS == 3
2568 reserved_xen_slot =
2569 ((mfn_to_shadow_page(sl2mfn)->type == SH_type_l2h_pae_shadow) &&
2570 (shadow_index
2571 >= (L2_PAGETABLE_FIRST_XEN_SLOT & (L2_PAGETABLE_ENTRIES-1))));
2572 #else /* SHADOW_PAGING_LEVELS == 2 */
2573 reserved_xen_slot = (shadow_index >= L2_PAGETABLE_FIRST_XEN_SLOT);
2574 #endif
2576 if ( unlikely(reserved_xen_slot) )
2578 // attempt by the guest to write to a xen reserved slot
2579 //
2580 SHADOW_PRINTK("%s out-of-range update "
2581 "sl2mfn=%05lx index=0x%x val=%" SH_PRI_pte "\n",
2582 __func__, mfn_x(sl2mfn), shadow_index, new_sl2e.l2);
2583 if ( shadow_l2e_get_flags(new_sl2e) & _PAGE_PRESENT )
2585 SHADOW_ERROR("out-of-range l2e update\n");
2586 result |= SHADOW_SET_ERROR;
2589 // do not call shadow_set_l2e...
2590 return result;
2593 #endif /* CONFIG_PAGING_LEVELS == SHADOW_PAGING_LEVELS */
2594 #endif /* SHADOW_PAGING_LEVELS < 4 */
2596 result |= shadow_set_l2e(v, sl2p, new_sl2e, sl2mfn);
2598 return result;
2601 static int validate_gl1e(struct vcpu *v, void *new_ge, mfn_t sl1mfn, void *se)
2603 shadow_l1e_t new_sl1e;
2604 guest_l1e_t new_gl1e = *(guest_l1e_t *)new_ge;
2605 shadow_l1e_t *sl1p = se;
2606 gfn_t gfn;
2607 mfn_t gmfn;
2608 p2m_type_t p2mt;
2609 int result = 0;
2610 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
2611 mfn_t gl1mfn;
2612 #endif /* OOS */
2614 perfc_incr(shadow_validate_gl1e_calls);
2616 gfn = guest_l1e_get_gfn(new_gl1e);
2617 gmfn = gfn_to_mfn(v->domain, gfn, &p2mt);
2619 l1e_propagate_from_guest(v, new_gl1e, gmfn, &new_sl1e, ft_prefetch, p2mt);
2620 result |= shadow_set_l1e(v, sl1p, new_sl1e, sl1mfn);
2622 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
2623 gl1mfn = _mfn(mfn_to_shadow_page(sl1mfn)->backpointer);
2624 if ( mfn_valid(gl1mfn)
2625 && mfn_is_out_of_sync(gl1mfn) )
2627 /* Update the OOS snapshot. */
2628 mfn_t snpmfn = oos_snapshot_lookup(v, gl1mfn);
2629 guest_l1e_t *snp;
2631 ASSERT(mfn_valid(snpmfn));
2633 snp = sh_map_domain_page(snpmfn);
2634 snp[guest_index(new_ge)] = new_gl1e;
2635 sh_unmap_domain_page(snp);
2637 #endif /* OOS */
2639 return result;
2642 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
2643 /**************************************************************************/
2644 /* Special validation function for re-syncing out-of-sync shadows.
2645 * Walks the *shadow* page, and for every entry that it finds,
2646 * revalidates the guest entry that corresponds to it.
2647 * N.B. This function is called with the vcpu that unsynced the page,
2648 * *not* the one that is causing it to be resynced. */
2649 void sh_resync_l1(struct vcpu *v, mfn_t gl1mfn, mfn_t snpmfn)
2651 mfn_t sl1mfn;
2652 shadow_l1e_t *sl1p;
2653 guest_l1e_t *gl1p, *gp, *snp;
2654 int rc = 0;
2656 ASSERT(mfn_valid(snpmfn));
2658 sl1mfn = get_shadow_status(v, gl1mfn, SH_type_l1_shadow);
2659 ASSERT(mfn_valid(sl1mfn)); /* Otherwise we would not have been called */
2661 snp = sh_map_domain_page(snpmfn);
2662 gp = sh_map_domain_page(gl1mfn);
2663 gl1p = gp;
2665 SHADOW_FOREACH_L1E(sl1mfn, sl1p, &gl1p, 0, {
2666 guest_l1e_t gl1e = *gl1p;
2667 guest_l1e_t *snpl1p = (guest_l1e_t *)snp + guest_index(gl1p);
2669 if ( memcmp(snpl1p, &gl1e, sizeof(gl1e)) )
2671 gfn_t gfn;
2672 mfn_t gmfn;
2673 p2m_type_t p2mt;
2674 shadow_l1e_t nsl1e;
2676 gfn = guest_l1e_get_gfn(gl1e);
2677 gmfn = gfn_to_mfn(v->domain, gfn, &p2mt);
2678 l1e_propagate_from_guest(v, gl1e, gmfn, &nsl1e, ft_prefetch, p2mt);
2679 rc |= shadow_set_l1e(v, sl1p, nsl1e, sl1mfn);
2681 *snpl1p = gl1e;
2683 });
2685 sh_unmap_domain_page(gp);
2686 sh_unmap_domain_page(snp);
2688 /* Setting shadow L1 entries should never need us to flush the TLB */
2689 ASSERT(!(rc & SHADOW_SET_FLUSH));
2692 /* Figure out whether it's definitely safe not to sync this l1 table.
2693 * That is: if we can tell that it's only used once, and that the
2694 * toplevel shadow responsible is not one of ours.
2695 * N.B. This function is called with the vcpu that required the resync,
2696 * *not* the one that originally unsynced the page, but it is
2697 * called in the *mode* of the vcpu that unsynced it. Clear? Good. */
2698 int sh_safe_not_to_sync(struct vcpu *v, mfn_t gl1mfn)
2700 struct shadow_page_info *sp;
2701 mfn_t smfn;
2703 smfn = get_shadow_status(v, gl1mfn, SH_type_l1_shadow);
2704 ASSERT(mfn_valid(smfn)); /* Otherwise we would not have been called */
2706 /* Up to l2 */
2707 sp = mfn_to_shadow_page(smfn);
2708 if ( sp->count != 1 || !sp->up )
2709 return 0;
2710 smfn = _mfn(sp->up >> PAGE_SHIFT);
2711 ASSERT(mfn_valid(smfn));
2713 #if (SHADOW_PAGING_LEVELS == 4)
2714 /* up to l3 */
2715 sp = mfn_to_shadow_page(smfn);
2716 if ( sp->count != 1 || !sp->up )
2717 return 0;
2718 smfn = _mfn(sp->up >> PAGE_SHIFT);
2719 ASSERT(mfn_valid(smfn));
2721 /* up to l4 */
2722 sp = mfn_to_shadow_page(smfn);
2723 if ( sp->count != 1
2724 || sh_type_is_pinnable(v, SH_type_l3_64_shadow) || !sp->up )
2725 return 0;
2726 smfn = _mfn(sp->up >> PAGE_SHIFT);
2727 ASSERT(mfn_valid(smfn));
2729 #if (GUEST_PAGING_LEVELS == 2)
2730 /* In 2-on-3 shadow mode the up pointer contains the link to the
2731 * shadow page, but the shadow_table contains only the first of the
2732 * four pages that makes the PAE top shadow tables. */
2733 smfn = _mfn(mfn_x(smfn) & ~0x3UL);
2734 #endif
2736 #endif
2738 if ( pagetable_get_pfn(v->arch.shadow_table[0]) == mfn_x(smfn)
2739 #if (SHADOW_PAGING_LEVELS == 3)
2740 || pagetable_get_pfn(v->arch.shadow_table[1]) == mfn_x(smfn)
2741 || pagetable_get_pfn(v->arch.shadow_table[2]) == mfn_x(smfn)
2742 || pagetable_get_pfn(v->arch.shadow_table[3]) == mfn_x(smfn)
2743 #endif
2745 return 0;
2747 /* Only in use in one toplevel shadow, and it's not the one we're
2748 * running on */
2749 return 1;
2751 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC) */
2754 /**************************************************************************/
2755 /* Functions which translate and install the shadows of arbitrary guest
2756 * entries that we have just seen the guest write. */
2759 static inline int
2760 sh_map_and_validate(struct vcpu *v, mfn_t gmfn,
2761 void *new_gp, u32 size, u32 sh_type,
2762 u32 (*shadow_index)(mfn_t *smfn, u32 idx),
2763 int (*validate_ge)(struct vcpu *v, void *ge,
2764 mfn_t smfn, void *se))
2765 /* Generic function for mapping and validating. */
2767 mfn_t smfn, smfn2, map_mfn;
2768 shadow_l1e_t *sl1p;
2769 u32 shadow_idx, guest_idx;
2770 int result = 0;
2772 /* Align address and size to guest entry boundaries */
2773 size += (unsigned long)new_gp & (sizeof (guest_l1e_t) - 1);
2774 new_gp = (void *)((unsigned long)new_gp & ~(sizeof (guest_l1e_t) - 1));
2775 size = (size + sizeof (guest_l1e_t) - 1) & ~(sizeof (guest_l1e_t) - 1);
2776 ASSERT(size + (((unsigned long)new_gp) & ~PAGE_MASK) <= PAGE_SIZE);
2778 /* Map the shadow page */
2779 smfn = get_shadow_status(v, gmfn, sh_type);
2780 ASSERT(mfn_valid(smfn)); /* Otherwise we would not have been called */
2781 guest_idx = guest_index(new_gp);
2782 map_mfn = smfn;
2783 shadow_idx = shadow_index(&map_mfn, guest_idx);
2784 sl1p = sh_map_domain_page(map_mfn);
2786 /* Validate one entry at a time */
2787 while ( size )
2789 smfn2 = smfn;
2790 guest_idx = guest_index(new_gp);
2791 shadow_idx = shadow_index(&smfn2, guest_idx);
2792 if ( mfn_x(smfn2) != mfn_x(map_mfn) )
2794 /* We have moved to another page of the shadow */
2795 map_mfn = smfn2;
2796 sh_unmap_domain_page(sl1p);
2797 sl1p = sh_map_domain_page(map_mfn);
2799 result |= validate_ge(v,
2800 new_gp,
2801 map_mfn,
2802 &sl1p[shadow_idx]);
2803 size -= sizeof(guest_l1e_t);
2804 new_gp += sizeof(guest_l1e_t);
2806 sh_unmap_domain_page(sl1p);
2807 return result;
2811 int
2812 sh_map_and_validate_gl4e(struct vcpu *v, mfn_t gl4mfn,
2813 void *new_gl4p, u32 size)
2815 #if GUEST_PAGING_LEVELS >= 4
2816 return sh_map_and_validate(v, gl4mfn, new_gl4p, size,
2817 SH_type_l4_shadow,
2818 shadow_l4_index,
2819 validate_gl4e);
2820 #else // ! GUEST_PAGING_LEVELS >= 4
2821 SHADOW_ERROR("called in wrong paging mode!\n");
2822 BUG();
2823 return 0;
2824 #endif
2827 int
2828 sh_map_and_validate_gl3e(struct vcpu *v, mfn_t gl3mfn,
2829 void *new_gl3p, u32 size)
2831 #if GUEST_PAGING_LEVELS >= 4
2832 return sh_map_and_validate(v, gl3mfn, new_gl3p, size,
2833 SH_type_l3_shadow,
2834 shadow_l3_index,
2835 validate_gl3e);
2836 #else // ! GUEST_PAGING_LEVELS >= 4
2837 SHADOW_ERROR("called in wrong paging mode!\n");
2838 BUG();
2839 return 0;
2840 #endif
2843 int
2844 sh_map_and_validate_gl2e(struct vcpu *v, mfn_t gl2mfn,
2845 void *new_gl2p, u32 size)
2847 return sh_map_and_validate(v, gl2mfn, new_gl2p, size,
2848 SH_type_l2_shadow,
2849 shadow_l2_index,
2850 validate_gl2e);
2853 int
2854 sh_map_and_validate_gl2he(struct vcpu *v, mfn_t gl2mfn,
2855 void *new_gl2p, u32 size)
2857 #if GUEST_PAGING_LEVELS >= 3
2858 return sh_map_and_validate(v, gl2mfn, new_gl2p, size,
2859 SH_type_l2h_shadow,
2860 shadow_l2_index,
2861 validate_gl2e);
2862 #else /* Non-PAE guests don't have different kinds of l2 table */
2863 SHADOW_ERROR("called in wrong paging mode!\n");
2864 BUG();
2865 return 0;
2866 #endif
2869 int
2870 sh_map_and_validate_gl1e(struct vcpu *v, mfn_t gl1mfn,
2871 void *new_gl1p, u32 size)
2873 return sh_map_and_validate(v, gl1mfn, new_gl1p, size,
2874 SH_type_l1_shadow,
2875 shadow_l1_index,
2876 validate_gl1e);
2880 /**************************************************************************/
2881 /* Optimization: If we see two emulated writes of zeros to the same
2882 * page-table without another kind of page fault in between, we guess
2883 * that this is a batch of changes (for process destruction) and
2884 * unshadow the page so we don't take a pagefault on every entry. This
2885 * should also make finding writeable mappings of pagetables much
2886 * easier. */
2888 /* Look to see if this is the second emulated write in a row to this
2889 * page, and unshadow if it is */
2890 static inline void check_for_early_unshadow(struct vcpu *v, mfn_t gmfn)
2892 #if SHADOW_OPTIMIZATIONS & SHOPT_EARLY_UNSHADOW
2893 if ( v->arch.paging.shadow.last_emulated_mfn_for_unshadow == mfn_x(gmfn)
2894 && sh_mfn_is_a_page_table(gmfn) )
2896 perfc_incr(shadow_early_unshadow);
2897 sh_remove_shadows(v, gmfn, 1, 0 /* Fast, can fail to unshadow */ );
2899 v->arch.paging.shadow.last_emulated_mfn_for_unshadow = mfn_x(gmfn);
2900 #endif
2903 /* Stop counting towards early unshadows, as we've seen a real page fault */
2904 static inline void reset_early_unshadow(struct vcpu *v)
2906 #if SHADOW_OPTIMIZATIONS & SHOPT_EARLY_UNSHADOW
2907 v->arch.paging.shadow.last_emulated_mfn_for_unshadow = INVALID_MFN;
2908 #endif
2913 /**************************************************************************/
2914 /* Optimization: Prefetch multiple L1 entries. This is called after we have
2915 * demand-faulted a shadow l1e in the fault handler, to see if it's
2916 * worth fetching some more.
2917 */
2919 #if SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH
2921 /* XXX magic number */
2922 #define PREFETCH_DISTANCE 32
2924 static void sh_prefetch(struct vcpu *v, walk_t *gw,
2925 shadow_l1e_t *ptr_sl1e, mfn_t sl1mfn)
2927 int i, dist;
2928 gfn_t gfn;
2929 mfn_t gmfn;
2930 guest_l1e_t *gl1p = NULL, gl1e;
2931 shadow_l1e_t sl1e;
2932 u32 gflags;
2933 p2m_type_t p2mt;
2934 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
2935 guest_l1e_t *snpl1p = NULL;
2936 #endif /* OOS */
2939 /* Prefetch no further than the end of the _shadow_ l1 MFN */
2940 dist = (PAGE_SIZE - ((unsigned long)ptr_sl1e & ~PAGE_MASK)) / sizeof sl1e;
2941 /* And no more than a maximum fetches-per-fault */
2942 if ( dist > PREFETCH_DISTANCE )
2943 dist = PREFETCH_DISTANCE;
2945 if ( mfn_valid(gw->l1mfn) )
2947 /* Normal guest page; grab the next guest entry */
2948 gl1p = sh_map_domain_page(gw->l1mfn);
2949 gl1p += guest_l1_table_offset(gw->va);
2951 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
2952 if ( mfn_is_out_of_sync(gw->l1mfn) )
2954 mfn_t snpmfn = oos_snapshot_lookup(v, gw->l1mfn);
2956 ASSERT(mfn_valid(snpmfn));
2957 snpl1p = sh_map_domain_page(snpmfn);
2958 snpl1p += guest_l1_table_offset(gw->va);
2960 #endif /* OOS */
2963 for ( i = 1; i < dist ; i++ )
2965 /* No point in prefetching if there's already a shadow */
2966 if ( ptr_sl1e[i].l1 != 0 )
2967 break;
2969 if ( mfn_valid(gw->l1mfn) )
2971 /* Normal guest page; grab the next guest entry */
2972 gl1e = gl1p[i];
2973 /* Not worth continuing if we hit an entry that will need another
2974 * fault for A/D-bit propagation anyway */
2975 gflags = guest_l1e_get_flags(gl1e);
2976 if ( (gflags & _PAGE_PRESENT)
2977 && (!(gflags & _PAGE_ACCESSED)
2978 || ((gflags & _PAGE_RW) && !(gflags & _PAGE_DIRTY))) )
2979 break;
2981 else
2983 /* Fragmented superpage, unless we've been called wrongly */
2984 ASSERT(guest_l2e_get_flags(gw->l2e) & _PAGE_PSE);
2985 /* Increment the l1e's GFN by the right number of guest pages */
2986 gl1e = guest_l1e_from_gfn(
2987 _gfn(gfn_x(guest_l1e_get_gfn(gw->l1e)) + i),
2988 guest_l1e_get_flags(gw->l1e));
2991 /* Look at the gfn that the l1e is pointing at */
2992 gfn = guest_l1e_get_gfn(gl1e);
2993 gmfn = gfn_to_mfn(v->domain, gfn, &p2mt);
2995 /* Propagate the entry. */
2996 l1e_propagate_from_guest(v, gl1e, gmfn, &sl1e, ft_prefetch, p2mt);
2997 (void) shadow_set_l1e(v, ptr_sl1e + i, sl1e, sl1mfn);
2999 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
3000 if ( snpl1p != NULL )
3001 snpl1p[i] = gl1e;
3002 #endif /* OOS */
3004 if ( gl1p != NULL )
3005 sh_unmap_domain_page(gl1p);
3006 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
3007 if ( snpl1p != NULL )
3008 sh_unmap_domain_page(snpl1p);
3009 #endif /* OOS */
3012 #endif /* SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH */
3015 /**************************************************************************/
3016 /* Entry points into the shadow code */
3018 /* Called from pagefault handler in Xen, and from the HVM trap handlers
3019 * for pagefaults. Returns 1 if this fault was an artefact of the
3020 * shadow code (and the guest should retry) or 0 if it is not (and the
3021 * fault should be handled elsewhere or passed to the guest). */
3023 static int sh_page_fault(struct vcpu *v,
3024 unsigned long va,
3025 struct cpu_user_regs *regs)
3027 struct domain *d = v->domain;
3028 walk_t gw;
3029 gfn_t gfn;
3030 mfn_t gmfn, sl1mfn=_mfn(0);
3031 shadow_l1e_t sl1e, *ptr_sl1e;
3032 paddr_t gpa;
3033 struct sh_emulate_ctxt emul_ctxt;
3034 struct x86_emulate_ops *emul_ops;
3035 int r;
3036 fetch_type_t ft = 0;
3037 p2m_type_t p2mt;
3038 uint32_t rc;
3039 #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION
3040 int fast_emul = 0;
3041 #endif
3043 SHADOW_PRINTK("d:v=%u:%u va=%#lx err=%u, rip=%lx\n",
3044 v->domain->domain_id, v->vcpu_id, va, regs->error_code,
3045 regs->rip);
3047 perfc_incr(shadow_fault);
3049 #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION
3050 /* If faulting frame is successfully emulated in last shadow fault
3051 * it's highly likely to reach same emulation action for this frame.
3052 * Then try to emulate early to avoid lock aquisition.
3053 */
3054 if ( v->arch.paging.last_write_emul_ok
3055 && v->arch.paging.shadow.last_emulated_frame == (va >> PAGE_SHIFT) )
3057 /* check whether error code is 3, or else fall back to normal path
3058 * in case of some validation is required
3059 */
3060 if ( regs->error_code == (PFEC_write_access | PFEC_page_present) )
3062 fast_emul = 1;
3063 gmfn = _mfn(v->arch.paging.shadow.last_emulated_mfn);
3065 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
3066 /* Fall back to the slow path if we're trying to emulate
3067 writes to an out of sync page. */
3068 if ( mfn_valid(gmfn) && mfn_is_out_of_sync(gmfn) )
3070 v->arch.paging.last_write_emul_ok = 0;
3071 goto page_fault_slow_path;
3073 #endif /* OOS */
3075 perfc_incr(shadow_fault_fast_emulate);
3076 goto early_emulation;
3078 else
3079 v->arch.paging.last_write_emul_ok = 0;
3081 #endif
3083 //
3084 // XXX: Need to think about eventually mapping superpages directly in the
3085 // shadow (when possible), as opposed to splintering them into a
3086 // bunch of 4K maps.
3087 //
3089 #if (SHADOW_OPTIMIZATIONS & SHOPT_FAST_FAULT_PATH)
3090 if ( (regs->error_code & PFEC_reserved_bit) )
3092 /* The only reasons for reserved bits to be set in shadow entries
3093 * are the two "magic" shadow_l1e entries. */
3094 if ( likely((__copy_from_user(&sl1e,
3095 (sh_linear_l1_table(v)
3096 + shadow_l1_linear_offset(va)),
3097 sizeof(sl1e)) == 0)
3098 && sh_l1e_is_magic(sl1e)) )
3100 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
3101 /* First, need to check that this isn't an out-of-sync
3102 * shadow l1e. If it is, we fall back to the slow path, which
3103 * will sync it up again. */
3105 shadow_l2e_t sl2e;
3106 mfn_t gl1mfn;
3107 if ( (__copy_from_user(&sl2e,
3108 (sh_linear_l2_table(v)
3109 + shadow_l2_linear_offset(va)),
3110 sizeof(sl2e)) != 0)
3111 || !(shadow_l2e_get_flags(sl2e) & _PAGE_PRESENT)
3112 || !mfn_valid(gl1mfn = _mfn(mfn_to_shadow_page(
3113 shadow_l2e_get_mfn(sl2e))->backpointer))
3114 || unlikely(mfn_is_out_of_sync(gl1mfn)) )
3116 /* Hit the slow path as if there had been no
3117 * shadow entry at all, and let it tidy up */
3118 ASSERT(regs->error_code & PFEC_page_present);
3119 regs->error_code ^= (PFEC_reserved_bit|PFEC_page_present);
3120 goto page_fault_slow_path;
3123 #endif /* SHOPT_OUT_OF_SYNC */
3125 if ( sh_l1e_is_gnp(sl1e) )
3127 /* Not-present in a guest PT: pass to the guest as
3128 * a not-present fault (by flipping two bits). */
3129 ASSERT(regs->error_code & PFEC_page_present);
3130 regs->error_code ^= (PFEC_reserved_bit|PFEC_page_present);
3131 reset_early_unshadow(v);
3132 perfc_incr(shadow_fault_fast_gnp);
3133 SHADOW_PRINTK("fast path not-present\n");
3134 return 0;
3136 else
3138 /* Magic MMIO marker: extract gfn for MMIO address */
3139 ASSERT(sh_l1e_is_mmio(sl1e));
3140 gpa = (((paddr_t)(gfn_x(sh_l1e_mmio_get_gfn(sl1e))))
3141 << PAGE_SHIFT)
3142 | (va & ~PAGE_MASK);
3144 perfc_incr(shadow_fault_fast_mmio);
3145 SHADOW_PRINTK("fast path mmio %#"PRIpaddr"\n", gpa);
3146 reset_early_unshadow(v);
3147 return (handle_mmio_with_translation(va, gpa >> PAGE_SHIFT)
3148 ? EXCRET_fault_fixed : 0);
3150 else
3152 /* This should be exceptionally rare: another vcpu has fixed
3153 * the tables between the fault and our reading the l1e.
3154 * Retry and let the hardware give us the right fault next time. */
3155 perfc_incr(shadow_fault_fast_fail);
3156 SHADOW_PRINTK("fast path false alarm!\n");
3157 return EXCRET_fault_fixed;
3161 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
3162 page_fault_slow_path:
3163 #endif
3164 #endif /* SHOPT_FAST_FAULT_PATH */
3166 /* Detect if this page fault happened while we were already in Xen
3167 * doing a shadow operation. If that happens, the only thing we can
3168 * do is let Xen's normal fault handlers try to fix it. In any case,
3169 * a diagnostic trace of the fault will be more useful than
3170 * a BUG() when we try to take the lock again. */
3171 if ( unlikely(shadow_locked_by_me(d)) )
3173 SHADOW_ERROR("Recursive shadow fault: lock was taken by %s\n",
3174 d->arch.paging.shadow.locker_function);
3175 return 0;
3178 rewalk:
3179 rc = guest_walk_tables(v, va, &gw, regs->error_code);
3181 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
3182 if ( !(rc & _PAGE_PRESENT) )
3183 regs->error_code |= PFEC_page_present;
3184 else if ( regs->error_code & PFEC_page_present )
3186 SHADOW_ERROR("OOS paranoia: Something is wrong in guest TLB"
3187 " flushing. Have fun debugging it.\n");
3188 regs->error_code &= ~PFEC_page_present;
3190 #endif
3192 if ( rc != 0 )
3194 perfc_incr(shadow_fault_bail_real_fault);
3195 SHADOW_PRINTK("not a shadow fault\n");
3196 reset_early_unshadow(v);
3197 return 0;
3200 /* It's possible that the guest has put pagetables in memory that it has
3201 * already used for some special purpose (ioreq pages, or granted pages).
3202 * If that happens we'll have killed the guest already but it's still not
3203 * safe to propagate entries out of the guest PT so get out now. */
3204 if ( unlikely(d->is_shutting_down) )
3206 SHADOW_PRINTK("guest is shutting down\n");
3207 return 0;
3210 /* What kind of access are we dealing with? */
3211 ft = ((regs->error_code & PFEC_write_access)
3212 ? ft_demand_write : ft_demand_read);
3214 /* What mfn is the guest trying to access? */
3215 gfn = guest_l1e_get_gfn(gw.l1e);
3216 gmfn = gfn_to_mfn(d, gfn, &p2mt);
3218 if ( shadow_mode_refcounts(d) &&
3219 (!p2m_is_valid(p2mt) || (!p2m_is_mmio(p2mt) && !mfn_valid(gmfn))) )
3221 perfc_incr(shadow_fault_bail_bad_gfn);
3222 SHADOW_PRINTK("BAD gfn=%"SH_PRI_gfn" gmfn=%"PRI_mfn"\n",
3223 gfn_x(gfn), mfn_x(gmfn));
3224 reset_early_unshadow(v);
3225 return 0;
3228 #if (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB)
3229 /* Remember this successful VA->GFN translation for later. */
3230 vtlb_insert(v, va >> PAGE_SHIFT, gfn_x(gfn),
3231 regs->error_code | PFEC_page_present);
3232 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB) */
3234 shadow_lock(d);
3236 rc = gw_remove_write_accesses(v, va, &gw);
3238 /* First bit set: Removed write access to a page. */
3239 if ( rc & GW_RMWR_FLUSHTLB )
3241 /* Write permission removal is also a hint that other gwalks
3242 * overlapping with this one may be inconsistent
3243 */
3244 perfc_incr(shadow_rm_write_flush_tlb);
3245 atomic_inc(&d->arch.paging.shadow.gtable_dirty_version);
3246 flush_tlb_mask(d->domain_dirty_cpumask);
3249 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
3250 /* Second bit set: Resynced a page. Re-walk needed. */
3251 if ( rc & GW_RMWR_REWALK )
3253 shadow_unlock(d);
3254 goto rewalk;
3256 #endif /* OOS */
3258 if ( !shadow_check_gwalk(v, va, &gw) )
3260 perfc_incr(shadow_inconsistent_gwalk);
3261 shadow_unlock(d);
3262 goto rewalk;
3265 shadow_audit_tables(v);
3266 sh_audit_gw(v, &gw);
3268 /* Make sure there is enough free shadow memory to build a chain of
3269 * shadow tables. (We never allocate a top-level shadow on this path,
3270 * only a 32b l1, pae l1, or 64b l3+2+1. Note that while
3271 * SH_type_l1_shadow isn't correct in the latter case, all page
3272 * tables are the same size there.) */
3273 shadow_prealloc(d,
3274 SH_type_l1_shadow,
3275 GUEST_PAGING_LEVELS < 4 ? 1 : GUEST_PAGING_LEVELS - 1);
3277 /* Acquire the shadow. This must happen before we figure out the rights
3278 * for the shadow entry, since we might promote a page here. */
3279 ptr_sl1e = shadow_get_and_create_l1e(v, &gw, &sl1mfn, ft);
3280 if ( unlikely(ptr_sl1e == NULL) )
3282 /* Couldn't get the sl1e! Since we know the guest entries
3283 * are OK, this can only have been caused by a failed
3284 * shadow_set_l*e(), which will have crashed the guest.
3285 * Get out of the fault handler immediately. */
3286 ASSERT(d->is_shutting_down);
3287 shadow_unlock(d);
3288 return 0;
3291 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
3292 /* Always unsync when writing to L1 page tables. */
3293 if ( sh_mfn_is_a_page_table(gmfn)
3294 && ft == ft_demand_write )
3295 sh_unsync(v, gmfn, va);
3296 #endif /* OOS */
3298 /* Calculate the shadow entry and write it */
3299 l1e_propagate_from_guest(v, gw.l1e, gmfn, &sl1e, ft, p2mt);
3300 r = shadow_set_l1e(v, ptr_sl1e, sl1e, sl1mfn);
3302 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
3303 if ( mfn_valid(gw.l1mfn)
3304 && mfn_is_out_of_sync(gw.l1mfn) )
3306 /* Update the OOS snapshot. */
3307 mfn_t snpmfn = oos_snapshot_lookup(v, gw.l1mfn);
3308 guest_l1e_t *snp;
3310 ASSERT(mfn_valid(snpmfn));
3312 snp = sh_map_domain_page(snpmfn);
3313 snp[guest_l1_table_offset(va)] = gw.l1e;
3314 sh_unmap_domain_page(snp);
3316 #endif /* OOS */
3318 #if SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH
3319 /* Prefetch some more shadow entries */
3320 sh_prefetch(v, &gw, ptr_sl1e, sl1mfn);
3321 #endif
3323 /* Need to emulate accesses to page tables */
3324 if ( sh_mfn_is_a_page_table(gmfn)
3325 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
3326 /* Unless they've been allowed to go out of sync with their shadows */
3327 && !mfn_is_out_of_sync(gmfn)
3328 #endif
3331 if ( ft == ft_demand_write )
3333 perfc_incr(shadow_fault_emulate_write);
3334 goto emulate;
3336 else if ( shadow_mode_trap_reads(d) && ft == ft_demand_read )
3338 perfc_incr(shadow_fault_emulate_read);
3339 goto emulate;
3343 /* Need to hand off device-model MMIO and writes to read-only
3344 * memory to the device model */
3345 if ( p2mt == p2m_mmio_dm
3346 || (p2mt == p2m_ram_ro && ft == ft_demand_write) )
3348 gpa = guest_walk_to_gpa(&gw);
3349 goto mmio;
3352 /* In HVM guests, we force CR0.WP always to be set, so that the
3353 * pagetables are always write-protected. If the guest thinks
3354 * CR0.WP is clear, we must emulate faulting supervisor writes to
3355 * allow the guest to write through read-only PTEs. Emulate if the
3356 * fault was a non-user write to a present page. */
3357 if ( is_hvm_domain(d)
3358 && unlikely(!hvm_wp_enabled(v))
3359 && regs->error_code == (PFEC_write_access|PFEC_page_present) )
3361 perfc_incr(shadow_fault_emulate_wp);
3362 goto emulate;
3365 perfc_incr(shadow_fault_fixed);
3366 d->arch.paging.log_dirty.fault_count++;
3367 reset_early_unshadow(v);
3369 done:
3370 sh_audit_gw(v, &gw);
3371 SHADOW_PRINTK("fixed\n");
3372 shadow_audit_tables(v);
3373 shadow_unlock(d);
3374 return EXCRET_fault_fixed;
3376 emulate:
3377 if ( !shadow_mode_refcounts(d) || !guest_mode(regs) )
3378 goto not_a_shadow_fault;
3380 /*
3381 * We do not emulate user writes. Instead we use them as a hint that the
3382 * page is no longer a page table. This behaviour differs from native, but
3383 * it seems very unlikely that any OS grants user access to page tables.
3384 */
3385 if ( (regs->error_code & PFEC_user_mode) )
3387 SHADOW_PRINTK("user-mode fault to PT, unshadowing mfn %#lx\n",
3388 mfn_x(gmfn));
3389 perfc_incr(shadow_fault_emulate_failed);
3390 sh_remove_shadows(v, gmfn, 0 /* thorough */, 1 /* must succeed */);
3391 goto done;
3394 /*
3395 * We don't need to hold the lock for the whole emulation; we will
3396 * take it again when we write to the pagetables.
3397 */
3398 sh_audit_gw(v, &gw);
3399 shadow_audit_tables(v);
3400 shadow_unlock(d);
3402 #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION
3403 early_emulation:
3404 #endif
3405 if ( is_hvm_domain(d) )
3407 /*
3408 * If we are in the middle of injecting an exception or interrupt then
3409 * we should not emulate: it is not the instruction at %eip that caused
3410 * the fault. Furthermore it is almost certainly the case the handler
3411 * stack is currently considered to be a page table, so we should
3412 * unshadow the faulting page before exiting.
3413 */
3414 if ( unlikely(hvm_event_pending(v)) )
3416 #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION
3417 if ( fast_emul )
3419 perfc_incr(shadow_fault_fast_emulate_fail);
3420 v->arch.paging.last_write_emul_ok = 0;
3422 #endif
3423 gdprintk(XENLOG_DEBUG, "write to pagetable during event "
3424 "injection: cr2=%#lx, mfn=%#lx\n",
3425 va, mfn_x(gmfn));
3426 sh_remove_shadows(v, gmfn, 0 /* thorough */, 1 /* must succeed */);
3427 return EXCRET_fault_fixed;
3431 SHADOW_PRINTK("emulate: eip=%#lx esp=%#lx\n",
3432 (unsigned long)regs->eip, (unsigned long)regs->esp);
3434 emul_ops = shadow_init_emulation(&emul_ctxt, regs);
3436 r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
3438 /*
3439 * NB. We do not unshadow on X86EMUL_EXCEPTION. It's not clear that it
3440 * would be a good unshadow hint. If we *do* decide to unshadow-on-fault
3441 * then it must be 'failable': we cannot require the unshadow to succeed.
3442 */
3443 if ( r == X86EMUL_UNHANDLEABLE )
3445 perfc_incr(shadow_fault_emulate_failed);
3446 #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION
3447 if ( fast_emul )
3449 perfc_incr(shadow_fault_fast_emulate_fail);
3450 v->arch.paging.last_write_emul_ok = 0;
3452 #endif
3453 SHADOW_PRINTK("emulator failure, unshadowing mfn %#lx\n",
3454 mfn_x(gmfn));
3455 /* If this is actually a page table, then we have a bug, and need
3456 * to support more operations in the emulator. More likely,
3457 * though, this is a hint that this page should not be shadowed. */
3458 shadow_remove_all_shadows(v, gmfn);
3461 #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION
3462 /* Record successfully emulated information as heuristics to next
3463 * fault on same frame for acceleration. But be careful to verify
3464 * its attribute still as page table, or else unshadow triggered
3465 * in write emulation normally requires a re-sync with guest page
3466 * table to recover r/w permission. Incorrect record for such case
3467 * will cause unexpected more shadow faults due to propagation is
3468 * skipped.
3469 */
3470 if ( (r == X86EMUL_OKAY) && sh_mfn_is_a_page_table(gmfn) )
3472 if ( !fast_emul )
3474 v->arch.paging.shadow.last_emulated_frame = va >> PAGE_SHIFT;
3475 v->arch.paging.shadow.last_emulated_mfn = mfn_x(gmfn);
3476 v->arch.paging.last_write_emul_ok = 1;
3479 else if ( fast_emul )
3480 v->arch.paging.last_write_emul_ok = 0;
3481 #endif
3483 #if GUEST_PAGING_LEVELS == 3 /* PAE guest */
3484 if ( r == X86EMUL_OKAY ) {
3485 int i;
3486 /* Emulate up to four extra instructions in the hope of catching
3487 * the "second half" of a 64-bit pagetable write. */
3488 for ( i = 0 ; i < 4 ; i++ )
3490 shadow_continue_emulation(&emul_ctxt, regs);
3491 v->arch.paging.last_write_was_pt = 0;
3492 r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
3493 if ( r == X86EMUL_OKAY )
3495 if ( v->arch.paging.last_write_was_pt )
3497 perfc_incr(shadow_em_ex_pt);
3498 break; /* Don't emulate past the other half of the write */
3500 else
3501 perfc_incr(shadow_em_ex_non_pt);
3503 else
3505 perfc_incr(shadow_em_ex_fail);
3506 break; /* Don't emulate again if we failed! */
3510 #endif /* PAE guest */
3512 SHADOW_PRINTK("emulated\n");
3513 return EXCRET_fault_fixed;
3515 mmio:
3516 if ( !guest_mode(regs) )
3517 goto not_a_shadow_fault;
3518 perfc_incr(shadow_fault_mmio);
3519 sh_audit_gw(v, &gw);
3520 SHADOW_PRINTK("mmio %#"PRIpaddr"\n", gpa);
3521 shadow_audit_tables(v);
3522 reset_early_unshadow(v);
3523 shadow_unlock(d);
3524 return (handle_mmio_with_translation(va, gpa >> PAGE_SHIFT)
3525 ? EXCRET_fault_fixed : 0);
3527 not_a_shadow_fault:
3528 sh_audit_gw(v, &gw);
3529 SHADOW_PRINTK("not a shadow fault\n");
3530 shadow_audit_tables(v);
3531 reset_early_unshadow(v);
3532 shadow_unlock(d);
3533 return 0;
3537 static int
3538 sh_invlpg(struct vcpu *v, unsigned long va)
3539 /* Called when the guest requests an invlpg. Returns 1 if the invlpg
3540 * instruction should be issued on the hardware, or 0 if it's safe not
3541 * to do so. */
3543 mfn_t sl1mfn;
3544 shadow_l2e_t sl2e;
3546 perfc_incr(shadow_invlpg);
3548 #if (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB)
3549 /* No longer safe to use cached gva->gfn translations */
3550 vtlb_flush(v);
3551 #endif
3553 #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION
3554 v->arch.paging.last_write_emul_ok = 0;
3555 #endif
3557 /* First check that we can safely read the shadow l2e. SMP/PAE linux can
3558 * run as high as 6% of invlpg calls where we haven't shadowed the l2
3559 * yet. */
3560 #if SHADOW_PAGING_LEVELS == 4
3562 shadow_l3e_t sl3e;
3563 if ( !(shadow_l4e_get_flags(
3564 sh_linear_l4_table(v)[shadow_l4_linear_offset(va)])
3565 & _PAGE_PRESENT) )
3566 return 0;
3567 /* This must still be a copy-from-user because we don't have the
3568 * shadow lock, and the higher-level shadows might disappear
3569 * under our feet. */
3570 if ( __copy_from_user(&sl3e, (sh_linear_l3_table(v)
3571 + shadow_l3_linear_offset(va)),
3572 sizeof (sl3e)) != 0 )
3574 perfc_incr(shadow_invlpg_fault);
3575 return 0;
3577 if ( (!shadow_l3e_get_flags(sl3e) & _PAGE_PRESENT) )
3578 return 0;
3580 #else /* SHADOW_PAGING_LEVELS == 3 */
3581 if ( !(l3e_get_flags(v->arch.paging.shadow.l3table[shadow_l3_linear_offset(va)])
3582 & _PAGE_PRESENT) )
3583 // no need to flush anything if there's no SL2...
3584 return 0;
3585 #endif
3587 /* This must still be a copy-from-user because we don't have the shadow
3588 * lock, and the higher-level shadows might disappear under our feet. */
3589 if ( __copy_from_user(&sl2e,
3590 sh_linear_l2_table(v) + shadow_l2_linear_offset(va),
3591 sizeof (sl2e)) != 0 )
3593 perfc_incr(shadow_invlpg_fault);
3594 return 0;
3597 // If there's nothing shadowed for this particular sl2e, then
3598 // there is no need to do an invlpg, either...
3599 //
3600 if ( !(shadow_l2e_get_flags(sl2e) & _PAGE_PRESENT) )
3601 return 0;
3603 // Check to see if the SL2 is a splintered superpage...
3604 // If so, then we'll need to flush the entire TLB (because that's
3605 // easier than invalidating all of the individual 4K pages).
3606 //
3607 sl1mfn = shadow_l2e_get_mfn(sl2e);
3608 if ( mfn_to_shadow_page(sl1mfn)->type
3609 == SH_type_fl1_shadow )
3611 flush_tlb_local();
3612 return 0;
3615 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
3616 /* Check to see if the SL1 is out of sync. */
3618 mfn_t gl1mfn = _mfn(mfn_to_shadow_page(sl1mfn)->backpointer);
3619 struct page_info *pg = mfn_to_page(gl1mfn);
3620 if ( mfn_valid(gl1mfn)
3621 && page_is_out_of_sync(pg) )
3623 /* The test above may give false positives, since we don't
3624 * hold the shadow lock yet. Check again with the lock held. */
3625 shadow_lock(v->domain);
3627 /* This must still be a copy-from-user because we didn't
3628 * have the shadow lock last time we checked, and the
3629 * higher-level shadows might have disappeared under our
3630 * feet. */
3631 if ( __copy_from_user(&sl2e,
3632 sh_linear_l2_table(v)
3633 + shadow_l2_linear_offset(va),
3634 sizeof (sl2e)) != 0 )
3636 perfc_incr(shadow_invlpg_fault);
3637 shadow_unlock(v->domain);
3638 return 0;
3641 if ( !(shadow_l2e_get_flags(sl2e) & _PAGE_PRESENT) )
3643 shadow_unlock(v->domain);
3644 return 0;
3647 sl1mfn = shadow_l2e_get_mfn(sl2e);
3648 gl1mfn = _mfn(mfn_to_shadow_page(sl1mfn)->backpointer);
3649 pg = mfn_to_page(gl1mfn);
3651 if ( likely(sh_mfn_is_a_page_table(gl1mfn)
3652 && page_is_out_of_sync(pg) ) )
3654 shadow_l1e_t *sl1;
3655 sl1 = sh_linear_l1_table(v) + shadow_l1_linear_offset(va);
3656 /* Remove the shadow entry that maps this VA */
3657 (void) shadow_set_l1e(v, sl1, shadow_l1e_empty(), sl1mfn);
3659 shadow_unlock(v->domain);
3660 /* Need the invlpg, to pick up the disappeareance of the sl1e */
3661 return 1;
3664 #endif
3666 return 1;
3670 static unsigned long
3671 sh_gva_to_gfn(struct vcpu *v, unsigned long va, uint32_t *pfec)
3672 /* Called to translate a guest virtual address to what the *guest*
3673 * pagetables would map it to. */
3675 walk_t gw;
3676 gfn_t gfn;
3678 #if (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB)
3679 /* Check the vTLB cache first */
3680 unsigned long vtlb_gfn = vtlb_lookup(v, va, pfec[0]);
3681 if ( VALID_GFN(vtlb_gfn) )
3682 return vtlb_gfn;
3683 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB) */
3685 if ( guest_walk_tables(v, va, &gw, pfec[0]) != 0 )
3687 if ( !(guest_l1e_get_flags(gw.l1e) & _PAGE_PRESENT) )
3688 pfec[0] &= ~PFEC_page_present;
3689 return INVALID_GFN;
3691 gfn = guest_walk_to_gfn(&gw);
3693 #if (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB)
3694 /* Remember this successful VA->GFN translation for later. */
3695 vtlb_insert(v, va >> PAGE_SHIFT, gfn_x(gfn), pfec[0]);
3696 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB) */
3698 return gfn_x(gfn);
3702 static inline void
3703 sh_update_linear_entries(struct vcpu *v)
3704 /* Sync up all the linear mappings for this vcpu's pagetables */
3706 struct domain *d = v->domain;
3708 /* Linear pagetables in PV guests
3709 * ------------------------------
3711 * Guest linear pagetables, which map the guest pages, are at
3712 * LINEAR_PT_VIRT_START. Shadow linear pagetables, which map the
3713 * shadows, are at SH_LINEAR_PT_VIRT_START. Most of the time these
3714 * are set up at shadow creation time, but (of course!) the PAE case
3715 * is subtler. Normal linear mappings are made by having an entry
3716 * in the top-level table that points to itself (shadow linear) or
3717 * to the guest top-level table (guest linear). For PAE, to set up
3718 * a linear map requires us to copy the four top-level entries into
3719 * level-2 entries. That means that every time we change a PAE l3e,
3720 * we need to reflect the change into the copy.
3722 * Linear pagetables in HVM guests
3723 * -------------------------------
3725 * For HVM guests, the linear pagetables are installed in the monitor
3726 * tables (since we can't put them in the shadow). Shadow linear
3727 * pagetables, which map the shadows, are at SH_LINEAR_PT_VIRT_START,
3728 * and we use the linear pagetable slot at LINEAR_PT_VIRT_START for
3729 * a linear pagetable of the monitor tables themselves. We have
3730 * the same issue of having to re-copy PAE l3 entries whevever we use
3731 * PAE shadows.
3733 * Because HVM guests run on the same monitor tables regardless of the
3734 * shadow tables in use, the linear mapping of the shadow tables has to
3735 * be updated every time v->arch.shadow_table changes.
3736 */
3738 /* Don't try to update the monitor table if it doesn't exist */
3739 if ( shadow_mode_external(d)
3740 && pagetable_get_pfn(v->arch.monitor_table) == 0 )
3741 return;
3743 #if (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS == 4)
3745 /* For PV, one l4e points at the guest l4, one points at the shadow
3746 * l4. No maintenance required.
3747 * For HVM, just need to update the l4e that points to the shadow l4. */
3749 if ( shadow_mode_external(d) )
3751 /* Use the linear map if we can; otherwise make a new mapping */
3752 if ( v == current )
3754 __linear_l4_table[l4_linear_offset(SH_LINEAR_PT_VIRT_START)] =
3755 l4e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3756 __PAGE_HYPERVISOR);
3758 else
3760 l4_pgentry_t *ml4e;
3761 ml4e = sh_map_domain_page(pagetable_get_mfn(v->arch.monitor_table));
3762 ml4e[l4_table_offset(SH_LINEAR_PT_VIRT_START)] =
3763 l4e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3764 __PAGE_HYPERVISOR);
3765 sh_unmap_domain_page(ml4e);
3769 #elif (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS == 3)
3771 /* PV: XXX
3773 * HVM: To give ourselves a linear map of the shadows, we need to
3774 * extend a PAE shadow to 4 levels. We do this by having a monitor
3775 * l3 in slot 0 of the monitor l4 table, and copying the PAE l3
3776 * entries into it. Then, by having the monitor l4e for shadow
3777 * pagetables also point to the monitor l4, we can use it to access
3778 * the shadows.
3779 */
3781 if ( shadow_mode_external(d) )
3783 /* Install copies of the shadow l3es into the monitor l2 table
3784 * that maps SH_LINEAR_PT_VIRT_START. */
3785 shadow_l3e_t *sl3e;
3786 l2_pgentry_t *ml2e;
3787 int i;
3789 /* Use linear mappings if we can; otherwise make new mappings */
3790 if ( v == current )
3791 ml2e = __linear_l2_table
3792 + l2_linear_offset(SH_LINEAR_PT_VIRT_START);
3793 else
3795 mfn_t l3mfn, l2mfn;
3796 l4_pgentry_t *ml4e;
3797 l3_pgentry_t *ml3e;
3798 int linear_slot = shadow_l4_table_offset(SH_LINEAR_PT_VIRT_START);
3799 ml4e = sh_map_domain_page(pagetable_get_mfn(v->arch.monitor_table));
3801 ASSERT(l4e_get_flags(ml4e[linear_slot]) & _PAGE_PRESENT);
3802 l3mfn = _mfn(l4e_get_pfn(ml4e[linear_slot]));
3803 ml3e = sh_map_domain_page(l3mfn);
3804 sh_unmap_domain_page(ml4e);
3806 ASSERT(l3e_get_flags(ml3e[0]) & _PAGE_PRESENT);
3807 l2mfn = _mfn(l3e_get_pfn(ml3e[0]));
3808 ml2e = sh_map_domain_page(l2mfn);
3809 sh_unmap_domain_page(ml3e);
3812 /* Shadow l3 tables are made up by sh_update_cr3 */
3813 sl3e = v->arch.paging.shadow.l3table;
3815 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3817 ml2e[i] =
3818 (shadow_l3e_get_flags(sl3e[i]) & _PAGE_PRESENT)
3819 ? l2e_from_pfn(mfn_x(shadow_l3e_get_mfn(sl3e[i])),
3820 __PAGE_HYPERVISOR)
3821 : l2e_empty();
3824 if ( v != current )
3825 sh_unmap_domain_page(ml2e);
3827 else
3828 domain_crash(d); /* XXX */
3830 #elif CONFIG_PAGING_LEVELS == 3
3832 /* PV: need to copy the guest's l3 entries into the guest-linear-map l2
3833 * entries in the shadow, and the shadow's l3 entries into the
3834 * shadow-linear-map l2 entries in the shadow. This is safe to do
3835 * because Xen does not let guests share high-slot l2 tables between l3s,
3836 * so we know we're not treading on anyone's toes.
3838 * HVM: need to copy the shadow's l3 entries into the
3839 * shadow-linear-map l2 entries in the monitor table. This is safe
3840 * because we have one monitor table for each vcpu. The monitor's
3841 * own l3es don't need to be copied because they never change.
3842 * XXX That might change if we start stuffing things into the rest
3843 * of the monitor's virtual address space.
3844 */
3846 l2_pgentry_t *l2e, new_l2e;
3847 shadow_l3e_t *guest_l3e = NULL, *shadow_l3e;
3848 int i;
3849 int unmap_l2e = 0;
3851 #if GUEST_PAGING_LEVELS == 2
3853 /* Shadow l3 tables were built by sh_update_cr3 */
3854 BUG_ON(!shadow_mode_external(d)); /* PV 2-on-3 is unsupported */
3855 shadow_l3e = (shadow_l3e_t *)&v->arch.paging.shadow.l3table;
3857 #else /* GUEST_PAGING_LEVELS == 3 */
3859 shadow_l3e = (shadow_l3e_t *)&v->arch.paging.shadow.l3table;
3860 guest_l3e = (guest_l3e_t *)&v->arch.paging.shadow.gl3e;
3862 #endif /* GUEST_PAGING_LEVELS */
3864 /* Choose where to write the entries, using linear maps if possible */
3865 if ( shadow_mode_external(d) )
3867 if ( v == current )
3869 /* From the monitor tables, it's safe to use linear maps
3870 * to update monitor l2s */
3871 l2e = __linear_l2_table + (3 * L2_PAGETABLE_ENTRIES);
3873 else
3875 /* Map the monitor table's high l2 */
3876 l3_pgentry_t *l3e;
3877 l3e = sh_map_domain_page(
3878 pagetable_get_mfn(v->arch.monitor_table));
3879 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
3880 l2e = sh_map_domain_page(_mfn(l3e_get_pfn(l3e[3])));
3881 unmap_l2e = 1;
3882 sh_unmap_domain_page(l3e);
3885 else
3887 /* Map the shadow table's high l2 */
3888 ASSERT(shadow_l3e_get_flags(shadow_l3e[3]) & _PAGE_PRESENT);
3889 l2e = sh_map_domain_page(shadow_l3e_get_mfn(shadow_l3e[3]));
3890 unmap_l2e = 1;
3893 /* Write linear mapping of guest (only in PV, and only when
3894 * not translated). */
3895 if ( !shadow_mode_translate(d) )
3897 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3899 new_l2e =
3900 ((shadow_l3e_get_flags(guest_l3e[i]) & _PAGE_PRESENT)
3901 ? l2e_from_pfn(mfn_x(shadow_l3e_get_mfn(guest_l3e[i])),
3902 __PAGE_HYPERVISOR)
3903 : l2e_empty());
3904 safe_write_entry(
3905 &l2e[l2_table_offset(LINEAR_PT_VIRT_START) + i],
3906 &new_l2e);
3910 /* Write linear mapping of shadow. */
3911 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3913 new_l2e = (shadow_l3e_get_flags(shadow_l3e[i]) & _PAGE_PRESENT)
3914 ? l2e_from_pfn(mfn_x(shadow_l3e_get_mfn(shadow_l3e[i])),
3915 __PAGE_HYPERVISOR)
3916 : l2e_empty();
3917 safe_write_entry(
3918 &l2e[l2_table_offset(SH_LINEAR_PT_VIRT_START) + i],
3919 &new_l2e);
3922 if ( unmap_l2e )
3923 sh_unmap_domain_page(l2e);
3926 #else
3927 #error this should not happen
3928 #endif
3930 if ( shadow_mode_external(d) )
3932 /*
3933 * Having modified the linear pagetable mapping, flush local host TLBs.
3934 * This was not needed when vmenter/vmexit always had the side effect
3935 * of flushing host TLBs but, with ASIDs, it is possible to finish
3936 * this CR3 update, vmenter the guest, vmexit due to a page fault,
3937 * without an intervening host TLB flush. Then the page fault code
3938 * could use the linear pagetable to read a top-level shadow page
3939 * table entry. But, without this change, it would fetch the wrong
3940 * value due to a stale TLB.
3941 */
3942 flush_tlb_local();
3947 /* Removes vcpu->arch.paging.shadow.guest_vtable and vcpu->arch.shadow_table[].
3948 * Does all appropriate management/bookkeeping/refcounting/etc...
3949 */
3950 static void
3951 sh_detach_old_tables(struct vcpu *v)
3953 mfn_t smfn;
3954 int i = 0;
3956 ////
3957 //// vcpu->arch.paging.shadow.guest_vtable
3958 ////
3960 #if GUEST_PAGING_LEVELS == 3
3961 /* PAE guests don't have a mapping of the guest top-level table */
3962 ASSERT(v->arch.paging.shadow.guest_vtable == NULL);
3963 #else
3964 if ( v->arch.paging.shadow.guest_vtable )
3966 struct domain *d = v->domain;
3967 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
3968 sh_unmap_domain_page_global(v->arch.paging.shadow.guest_vtable);
3969 v->arch.paging.shadow.guest_vtable = NULL;
3971 #endif
3974 ////
3975 //// vcpu->arch.shadow_table[]
3976 ////
3978 #if GUEST_PAGING_LEVELS == 3
3979 /* PAE guests have four shadow_table entries */
3980 for ( i = 0 ; i < 4 ; i++ )
3981 #endif
3983 smfn = pagetable_get_mfn(v->arch.shadow_table[i]);
3984 if ( mfn_x(smfn) )
3985 sh_put_ref(v, smfn, 0);
3986 v->arch.shadow_table[i] = pagetable_null();
3990 /* Set up the top-level shadow and install it in slot 'slot' of shadow_table */
3991 static void
3992 sh_set_toplevel_shadow(struct vcpu *v,
3993 int slot,
3994 mfn_t gmfn,
3995 unsigned int root_type)
3997 mfn_t smfn;
3998 pagetable_t old_entry, new_entry;
4000 struct domain *d = v->domain;
4002 /* Remember the old contents of this slot */
4003 old_entry = v->arch.shadow_table[slot];
4005 /* Now figure out the new contents: is this a valid guest MFN? */
4006 if ( !mfn_valid(gmfn) )
4008 new_entry = pagetable_null();
4009 goto install_new_entry;
4012 /* Guest mfn is valid: shadow it and install the shadow */
4013 smfn = get_shadow_status(v, gmfn, root_type);
4014 if ( !mfn_valid(smfn) )
4016 /* Make sure there's enough free shadow memory. */
4017 shadow_prealloc(d, root_type, 1);
4018 /* Shadow the page. */
4019 smfn = sh_make_shadow(v, gmfn, root_type);
4021 ASSERT(mfn_valid(smfn));
4023 /* Pin the shadow and put it (back) on the list of pinned shadows */
4024 if ( sh_pin(v, smfn) == 0 )
4026 SHADOW_ERROR("can't pin %#lx as toplevel shadow\n", mfn_x(smfn));
4027 domain_crash(v->domain);
4030 /* Take a ref to this page: it will be released in sh_detach_old_tables()
4031 * or the next call to set_toplevel_shadow() */
4032 if ( !sh_get_ref(v, smfn, 0) )
4034 SHADOW_ERROR("can't install %#lx as toplevel shadow\n", mfn_x(smfn));
4035 domain_crash(v->domain);
4038 new_entry = pagetable_from_mfn(smfn);
4040 install_new_entry:
4041 /* Done. Install it */
4042 SHADOW_PRINTK("%u/%u [%u] gmfn %#"PRI_mfn" smfn %#"PRI_mfn"\n",
4043 GUEST_PAGING_LEVELS, SHADOW_PAGING_LEVELS, slot,
4044 mfn_x(gmfn), mfn_x(pagetable_get_mfn(new_entry)));
4045 v->arch.shadow_table[slot] = new_entry;
4047 /* Decrement the refcount of the old contents of this slot */
4048 if ( !pagetable_is_null(old_entry) ) {
4049 mfn_t old_smfn = pagetable_get_mfn(old_entry);
4050 /* Need to repin the old toplevel shadow if it's been unpinned
4051 * by shadow_prealloc(): in PV mode we're still running on this
4052 * shadow and it's not safe to free it yet. */
4053 if ( !mfn_to_shadow_page(old_smfn)->pinned && !sh_pin(v, old_smfn) )
4055 SHADOW_ERROR("can't re-pin %#lx\n", mfn_x(old_smfn));
4056 domain_crash(v->domain);
4058 sh_put_ref(v, old_smfn, 0);
4063 static void
4064 sh_update_cr3(struct vcpu *v, int do_locking)
4065 /* Updates vcpu->arch.cr3 after the guest has changed CR3.
4066 * Paravirtual guests should set v->arch.guest_table (and guest_table_user,
4067 * if appropriate).
4068 * HVM guests should also make sure hvm_get_guest_cntl_reg(v, 3) works;
4069 * this function will call hvm_update_guest_cr(v, 3) to tell them where the
4070 * shadow tables are.
4071 * If do_locking != 0, assume we are being called from outside the
4072 * shadow code, and must take and release the shadow lock; otherwise
4073 * that is the caller's responsibility.
4074 */
4076 struct domain *d = v->domain;
4077 mfn_t gmfn;
4078 #if GUEST_PAGING_LEVELS == 3
4079 guest_l3e_t *gl3e;
4080 u32 guest_idx=0;
4081 int i;
4082 #endif
4084 /* Don't do anything on an uninitialised vcpu */
4085 if ( !is_hvm_domain(d) && !v->is_initialised )
4087 ASSERT(v->arch.cr3 == 0);
4088 return;
4091 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
4092 /* Need to resync all the shadow entries on a TLB flush. Resync
4093 * current vcpus OOS pages before switching to the new shadow
4094 * tables so that the VA hint is still valid. */
4095 shadow_resync_current_vcpu(v, do_locking);
4096 #endif
4098 if ( do_locking ) shadow_lock(v->domain);
4100 ASSERT(shadow_locked_by_me(v->domain));
4101 ASSERT(v->arch.paging.mode);
4103 ////
4104 //// vcpu->arch.guest_table is already set
4105 ////
4107 #ifndef NDEBUG
4108 /* Double-check that the HVM code has sent us a sane guest_table */
4109 if ( is_hvm_domain(d) )
4111 ASSERT(shadow_mode_external(d));
4112 if ( hvm_paging_enabled(v) )
4113 ASSERT(pagetable_get_pfn(v->arch.guest_table));
4114 else
4115 ASSERT(v->arch.guest_table.pfn
4116 == d->arch.paging.shadow.unpaged_pagetable.pfn);
4118 #endif
4120 SHADOW_PRINTK("d=%u v=%u guest_table=%05lx\n",
4121 d->domain_id, v->vcpu_id,
4122 (unsigned long)pagetable_get_pfn(v->arch.guest_table));
4124 #if GUEST_PAGING_LEVELS == 4
4125 if ( !(v->arch.flags & TF_kernel_mode) && !is_pv_32on64_vcpu(v) )
4126 gmfn = pagetable_get_mfn(v->arch.guest_table_user);
4127 else
4128 #endif
4129 gmfn = pagetable_get_mfn(v->arch.guest_table);
4132 ////
4133 //// vcpu->arch.paging.shadow.guest_vtable
4134 ////
4135 #if GUEST_PAGING_LEVELS == 4
4136 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
4138 if ( v->arch.paging.shadow.guest_vtable )
4139 sh_unmap_domain_page_global(v->arch.paging.shadow.guest_vtable);
4140 v->arch.paging.shadow.guest_vtable = sh_map_domain_page_global(gmfn);
4141 /* PAGING_LEVELS==4 implies 64-bit, which means that
4142 * map_domain_page_global can't fail */
4143 BUG_ON(v->arch.paging.shadow.guest_vtable == NULL);
4145 else
4146 v->arch.paging.shadow.guest_vtable = __linear_l4_table;
4147 #elif GUEST_PAGING_LEVELS == 3
4148 /* On PAE guests we don't use a mapping of the guest's own top-level
4149 * table. We cache the current state of that table and shadow that,
4150 * until the next CR3 write makes us refresh our cache. */
4151 ASSERT(v->arch.paging.shadow.guest_vtable == NULL);
4153 if ( shadow_mode_external(d) )
4154 /* Find where in the page the l3 table is */
4155 guest_idx = guest_index((void *)v->arch.hvm_vcpu.guest_cr[3]);
4156 else
4157 /* PV guest: l3 is at the start of a page */
4158 guest_idx = 0;
4160 // Ignore the low 2 bits of guest_idx -- they are really just
4161 // cache control.
4162 guest_idx &= ~3;
4164 gl3e = ((guest_l3e_t *)sh_map_domain_page(gmfn)) + guest_idx;
4165 for ( i = 0; i < 4 ; i++ )
4166 v->arch.paging.shadow.gl3e[i] = gl3e[i];
4167 sh_unmap_domain_page(gl3e);
4168 #elif GUEST_PAGING_LEVELS == 2
4169 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
4171 if ( v->arch.paging.shadow.guest_vtable )
4172 sh_unmap_domain_page_global(v->arch.paging.shadow.guest_vtable);
4173 v->arch.paging.shadow.guest_vtable = sh_map_domain_page_global(gmfn);
4174 /* Does this really need map_domain_page_global? Handle the
4175 * error properly if so. */
4176 BUG_ON(v->arch.paging.shadow.guest_vtable == NULL); /* XXX */
4178 else
4179 v->arch.paging.shadow.guest_vtable = __linear_l2_table;
4180 #else
4181 #error this should never happen
4182 #endif
4185 ////
4186 //// vcpu->arch.shadow_table[]
4187 ////
4189 /* We revoke write access to the new guest toplevel page(s) before we
4190 * replace the old shadow pagetable(s), so that we can safely use the
4191 * (old) shadow linear maps in the writeable mapping heuristics. */
4192 #if GUEST_PAGING_LEVELS == 2
4193 if ( sh_remove_write_access(v, gmfn, 2, 0) != 0 )
4194 flush_tlb_mask(v->domain->domain_dirty_cpumask);
4195 sh_set_toplevel_shadow(v, 0, gmfn, SH_type_l2_shadow);
4196 #elif GUEST_PAGING_LEVELS == 3
4197 /* PAE guests have four shadow_table entries, based on the
4198 * current values of the guest's four l3es. */
4200 int flush = 0;
4201 gfn_t gl2gfn;
4202 mfn_t gl2mfn;
4203 p2m_type_t p2mt;
4204 guest_l3e_t *gl3e = (guest_l3e_t*)&v->arch.paging.shadow.gl3e;
4205 /* First, make all four entries read-only. */
4206 for ( i = 0; i < 4; i++ )
4208 if ( guest_l3e_get_flags(gl3e[i]) & _PAGE_PRESENT )
4210 gl2gfn = guest_l3e_get_gfn(gl3e[i]);
4211 gl2mfn = gfn_to_mfn(d, gl2gfn, &p2mt);
4212 if ( p2m_is_ram(p2mt) )
4213 flush |= sh_remove_write_access(v, gl2mfn, 2, 0);
4216 if ( flush )
4217 flush_tlb_mask(v->domain->domain_dirty_cpumask);
4218 /* Now install the new shadows. */
4219 for ( i = 0; i < 4; i++ )
4221 if ( guest_l3e_get_flags(gl3e[i]) & _PAGE_PRESENT )
4223 gl2gfn = guest_l3e_get_gfn(gl3e[i]);
4224 gl2mfn = gfn_to_mfn(d, gl2gfn, &p2mt);
4225 if ( p2m_is_ram(p2mt) )
4226 sh_set_toplevel_shadow(v, i, gl2mfn, (i == 3)
4227 ? SH_type_l2h_shadow
4228 : SH_type_l2_shadow);
4229 else
4230 sh_set_toplevel_shadow(v, i, _mfn(INVALID_MFN), 0);
4232 else
4233 sh_set_toplevel_shadow(v, i, _mfn(INVALID_MFN), 0);
4236 #elif GUEST_PAGING_LEVELS == 4
4237 if ( sh_remove_write_access(v, gmfn, 4, 0) != 0 )
4238 flush_tlb_mask(v->domain->domain_dirty_cpumask);
4239 sh_set_toplevel_shadow(v, 0, gmfn, SH_type_l4_shadow);
4240 #else
4241 #error This should never happen
4242 #endif
4245 ///
4246 /// v->arch.paging.shadow.l3table
4247 ///
4248 #if SHADOW_PAGING_LEVELS == 3
4250 mfn_t smfn;
4251 int i;
4252 for ( i = 0; i < 4; i++ )
4254 #if GUEST_PAGING_LEVELS == 2
4255 /* 2-on-3: make a PAE l3 that points at the four-page l2 */
4256 smfn = _mfn(pagetable_get_pfn(v->arch.shadow_table[0]) + i);
4257 #else
4258 /* 3-on-3: make a PAE l3 that points at the four l2 pages */
4259 smfn = pagetable_get_mfn(v->arch.shadow_table[i]);
4260 #endif
4261 v->arch.paging.shadow.l3table[i] =
4262 (mfn_x(smfn) == 0)
4263 ? shadow_l3e_empty()
4264 : shadow_l3e_from_mfn(smfn, _PAGE_PRESENT);
4267 #endif /* SHADOW_PAGING_LEVELS == 3 */
4270 ///
4271 /// v->arch.cr3
4272 ///
4273 if ( shadow_mode_external(d) )
4275 make_cr3(v, pagetable_get_pfn(v->arch.monitor_table));
4277 else // not shadow_mode_external...
4279 /* We don't support PV except guest == shadow == config levels */
4280 BUG_ON(GUEST_PAGING_LEVELS != SHADOW_PAGING_LEVELS);
4281 #if SHADOW_PAGING_LEVELS == 3
4282 /* 2-on-3 or 3-on-3: Use the PAE shadow l3 table we just fabricated.
4283 * Don't use make_cr3 because (a) we know it's below 4GB, and
4284 * (b) it's not necessarily page-aligned, and make_cr3 takes a pfn */
4285 ASSERT(virt_to_maddr(&v->arch.paging.shadow.l3table) <= 0xffffffe0ULL);
4286 v->arch.cr3 = virt_to_maddr(&v->arch.paging.shadow.l3table);
4287 #else
4288 /* 4-on-4: Just use the shadow top-level directly */
4289 make_cr3(v, pagetable_get_pfn(v->arch.shadow_table[0]));
4290 #endif
4294 ///
4295 /// v->arch.hvm_vcpu.hw_cr[3]
4296 ///
4297 if ( shadow_mode_external(d) )
4299 ASSERT(is_hvm_domain(d));
4300 #if SHADOW_PAGING_LEVELS == 3
4301 /* 2-on-3 or 3-on-3: Use the PAE shadow l3 table we just fabricated */
4302 v->arch.hvm_vcpu.hw_cr[3] =
4303 virt_to_maddr(&v->arch.paging.shadow.l3table);
4304 #else
4305 /* 4-on-4: Just use the shadow top-level directly */
4306 v->arch.hvm_vcpu.hw_cr[3] =
4307 pagetable_get_paddr(v->arch.shadow_table[0]);
4308 #endif
4309 hvm_update_guest_cr(v, 3);
4312 /* Fix up the linear pagetable mappings */
4313 sh_update_linear_entries(v);
4315 #if (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB)
4316 /* No longer safe to use cached gva->gfn translations */
4317 vtlb_flush(v);
4318 #endif
4320 #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION
4321 v->arch.paging.last_write_emul_ok = 0;
4322 #endif
4324 /* Release the lock, if we took it (otherwise it's the caller's problem) */
4325 if ( do_locking ) shadow_unlock(v->domain);
4327 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
4328 /* Need to resync all the shadow entries on a TLB flush. We only
4329 * update the shadows, leaving the pages out of sync. Also, we try
4330 * to skip synchronization of shadows not mapped in the new
4331 * tables. */
4332 shadow_sync_other_vcpus(v, do_locking);
4333 #endif
4338 /**************************************************************************/
4339 /* Functions to revoke guest rights */
4341 #if SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC
4342 int sh_rm_write_access_from_sl1p(struct vcpu *v, mfn_t gmfn,
4343 mfn_t smfn, unsigned long off)
4345 int r;
4346 shadow_l1e_t *sl1p, sl1e;
4347 struct shadow_page_info *sp;
4349 ASSERT(mfn_valid(gmfn));
4350 ASSERT(mfn_valid(smfn));
4352 sp = mfn_to_shadow_page(smfn);
4354 if ( sp->mbz != 0 ||
4355 #if GUEST_PAGING_LEVELS == 4
4356 (sp->type != SH_type_l1_64_shadow)
4357 #elif GUEST_PAGING_LEVELS == 3
4358 (sp->type != SH_type_l1_pae_shadow)
4359 #elif GUEST_PAGING_LEVELS == 2
4360 (sp->type != SH_type_l1_32_shadow)
4361 #endif
4363 goto fail;
4365 sl1p = sh_map_domain_page(smfn);
4366 sl1p += off;
4367 sl1e = *sl1p;
4368 if ( ((shadow_l1e_get_flags(sl1e) & (_PAGE_PRESENT|_PAGE_RW))
4369 != (_PAGE_PRESENT|_PAGE_RW))
4370 || (mfn_x(shadow_l1e_get_mfn(sl1e)) != mfn_x(gmfn)) )
4372 sh_unmap_domain_page(sl1p);
4373 goto fail;
4376 /* Found it! Need to remove its write permissions. */
4377 sl1e = shadow_l1e_remove_flags(sl1e, _PAGE_RW);
4378 r = shadow_set_l1e(v, sl1p, sl1e, smfn);
4379 ASSERT( !(r & SHADOW_SET_ERROR) );
4381 sh_unmap_domain_page(sl1p);
4382 perfc_incr(shadow_writeable_h_7);
4383 return 1;
4385 fail:
4386 perfc_incr(shadow_writeable_h_8);
4387 return 0;
4389 #endif /* OOS */
4391 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
4392 static int sh_guess_wrmap(struct vcpu *v, unsigned long vaddr, mfn_t gmfn)
4393 /* Look up this vaddr in the current shadow and see if it's a writeable
4394 * mapping of this gmfn. If so, remove it. Returns 1 if it worked. */
4396 shadow_l1e_t sl1e, *sl1p;
4397 shadow_l2e_t *sl2p;
4398 shadow_l3e_t *sl3p;
4399 #if SHADOW_PAGING_LEVELS >= 4
4400 shadow_l4e_t *sl4p;
4401 #endif
4402 mfn_t sl1mfn;
4403 int r;
4405 /* Carefully look in the shadow linear map for the l1e we expect */
4406 #if SHADOW_PAGING_LEVELS >= 4
4407 sl4p = sh_linear_l4_table(v) + shadow_l4_linear_offset(vaddr);
4408 if ( !(shadow_l4e_get_flags(*sl4p) & _PAGE_PRESENT) )
4409 return 0;
4410 sl3p = sh_linear_l3_table(v) + shadow_l3_linear_offset(vaddr);
4411 if ( !(shadow_l3e_get_flags(*sl3p) & _PAGE_PRESENT) )
4412 return 0;
4413 #else /* SHADOW_PAGING_LEVELS == 3 */
4414 sl3p = ((shadow_l3e_t *) v->arch.paging.shadow.l3table)
4415 + shadow_l3_linear_offset(vaddr);
4416 if ( !(shadow_l3e_get_flags(*sl3p) & _PAGE_PRESENT) )
4417 return 0;
4418 #endif
4419 sl2p = sh_linear_l2_table(v) + shadow_l2_linear_offset(vaddr);
4420 if ( !(shadow_l2e_get_flags(*sl2p) & _PAGE_PRESENT) )
4421 return 0;
4422 sl1p = sh_linear_l1_table(v) + shadow_l1_linear_offset(vaddr);
4423 sl1e = *sl1p;
4424 if ( ((shadow_l1e_get_flags(sl1e) & (_PAGE_PRESENT|_PAGE_RW))
4425 != (_PAGE_PRESENT|_PAGE_RW))
4426 || (mfn_x(shadow_l1e_get_mfn(sl1e)) != mfn_x(gmfn)) )
4427 return 0;
4429 /* Found it! Need to remove its write permissions. */
4430 sl1mfn = shadow_l2e_get_mfn(*sl2p);
4431 sl1e = shadow_l1e_remove_flags(sl1e, _PAGE_RW);
4432 r = shadow_set_l1e(v, sl1p, sl1e, sl1mfn);
4433 ASSERT( !(r & SHADOW_SET_ERROR) );
4434 return 1;
4436 #endif
4438 int sh_rm_write_access_from_l1(struct vcpu *v, mfn_t sl1mfn,
4439 mfn_t readonly_mfn)
4440 /* Excises all writeable mappings to readonly_mfn from this l1 shadow table */
4442 shadow_l1e_t *sl1e;
4443 int done = 0;
4444 int flags;
4445 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
4446 mfn_t base_sl1mfn = sl1mfn; /* Because sl1mfn changes in the foreach */
4447 #endif
4449 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done,
4451 flags = shadow_l1e_get_flags(*sl1e);
4452 if ( (flags & _PAGE_PRESENT)
4453 && (flags & _PAGE_RW)
4454 && (mfn_x(shadow_l1e_get_mfn(*sl1e)) == mfn_x(readonly_mfn)) )
4456 shadow_l1e_t ro_sl1e = shadow_l1e_remove_flags(*sl1e, _PAGE_RW);
4457 (void) shadow_set_l1e(v, sl1e, ro_sl1e, sl1mfn);
4458 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
4459 /* Remember the last shadow that we shot a writeable mapping in */
4460 v->arch.paging.shadow.last_writeable_pte_smfn = mfn_x(base_sl1mfn);
4461 #endif
4462 if ( (mfn_to_page(readonly_mfn)->u.inuse.type_info
4463 & PGT_count_mask) == 0 )
4464 /* This breaks us cleanly out of the FOREACH macro */
4465 done = 1;
4467 });
4468 return done;
4472 int sh_rm_mappings_from_l1(struct vcpu *v, mfn_t sl1mfn, mfn_t target_mfn)
4473 /* Excises all mappings to guest frame from this shadow l1 table */
4475 shadow_l1e_t *sl1e;
4476 int done = 0;
4477 int flags;
4479 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done,
4481 flags = shadow_l1e_get_flags(*sl1e);
4482 if ( (flags & _PAGE_PRESENT)
4483 && (mfn_x(shadow_l1e_get_mfn(*sl1e)) == mfn_x(target_mfn)) )
4485 (void) shadow_set_l1e(v, sl1e, shadow_l1e_empty(), sl1mfn);
4486 if ( (mfn_to_page(target_mfn)->count_info & PGC_count_mask) == 0 )
4487 /* This breaks us cleanly out of the FOREACH macro */
4488 done = 1;
4490 });
4491 return done;
4494 /**************************************************************************/
4495 /* Functions to excise all pointers to shadows from higher-level shadows. */
4497 void sh_clear_shadow_entry(struct vcpu *v, void *ep, mfn_t smfn)
4498 /* Blank out a single shadow entry */
4500 switch ( mfn_to_shadow_page(smfn)->type )
4502 case SH_type_l1_shadow:
4503 (void) shadow_set_l1e(v, ep, shadow_l1e_empty(), smfn); break;
4504 case SH_type_l2_shadow:
4505 #if GUEST_PAGING_LEVELS >= 3
4506 case SH_type_l2h_shadow:
4507 #endif
4508 (void) shadow_set_l2e(v, ep, shadow_l2e_empty(), smfn); break;
4509 #if GUEST_PAGING_LEVELS >= 4
4510 case SH_type_l3_shadow:
4511 (void) shadow_set_l3e(v, ep, shadow_l3e_empty(), smfn); break;
4512 case SH_type_l4_shadow:
4513 (void) shadow_set_l4e(v, ep, shadow_l4e_empty(), smfn); break;
4514 #endif
4515 default: BUG(); /* Called with the wrong kind of shadow. */
4519 int sh_remove_l1_shadow(struct vcpu *v, mfn_t sl2mfn, mfn_t sl1mfn)
4520 /* Remove all mappings of this l1 shadow from this l2 shadow */
4522 shadow_l2e_t *sl2e;
4523 int done = 0;
4524 int flags;
4526 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, done, v->domain,
4528 flags = shadow_l2e_get_flags(*sl2e);
4529 if ( (flags & _PAGE_PRESENT)
4530 && (mfn_x(shadow_l2e_get_mfn(*sl2e)) == mfn_x(sl1mfn)) )
4532 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
4533 if ( mfn_to_shadow_page(sl1mfn)->type == 0 )
4534 /* This breaks us cleanly out of the FOREACH macro */
4535 done = 1;
4537 });
4538 return done;
4541 #if GUEST_PAGING_LEVELS >= 4
4542 int sh_remove_l2_shadow(struct vcpu *v, mfn_t sl3mfn, mfn_t sl2mfn)
4543 /* Remove all mappings of this l2 shadow from this l3 shadow */
4545 shadow_l3e_t *sl3e;
4546 int done = 0;
4547 int flags;
4549 SHADOW_FOREACH_L3E(sl3mfn, sl3e, 0, done,
4551 flags = shadow_l3e_get_flags(*sl3e);
4552 if ( (flags & _PAGE_PRESENT)
4553 && (mfn_x(shadow_l3e_get_mfn(*sl3e)) == mfn_x(sl2mfn)) )
4555 (void) shadow_set_l3e(v, sl3e, shadow_l3e_empty(), sl3mfn);
4556 if ( mfn_to_shadow_page(sl2mfn)->type == 0 )
4557 /* This breaks us cleanly out of the FOREACH macro */
4558 done = 1;
4560 });
4561 return done;
4564 int sh_remove_l3_shadow(struct vcpu *v, mfn_t sl4mfn, mfn_t sl3mfn)
4565 /* Remove all mappings of this l3 shadow from this l4 shadow */
4567 shadow_l4e_t *sl4e;
4568 int done = 0;
4569 int flags;
4571 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, done, v->domain,
4573 flags = shadow_l4e_get_flags(*sl4e);
4574 if ( (flags & _PAGE_PRESENT)
4575 && (mfn_x(shadow_l4e_get_mfn(*sl4e)) == mfn_x(sl3mfn)) )
4577 (void) shadow_set_l4e(v, sl4e, shadow_l4e_empty(), sl4mfn);
4578 if ( mfn_to_shadow_page(sl3mfn)->type == 0 )
4579 /* This breaks us cleanly out of the FOREACH macro */
4580 done = 1;
4582 });
4583 return done;
4585 #endif /* 64bit guest */
4587 /**************************************************************************/
4588 /* Handling HVM guest writes to pagetables */
4590 /* Translate a VA to an MFN, injecting a page-fault if we fail */
4591 #define BAD_GVA_TO_GFN (~0UL)
4592 #define BAD_GFN_TO_MFN (~1UL)
4593 static mfn_t emulate_gva_to_mfn(struct vcpu *v,
4594 unsigned long vaddr,
4595 struct sh_emulate_ctxt *sh_ctxt)
4597 unsigned long gfn;
4598 mfn_t mfn;
4599 p2m_type_t p2mt;
4600 uint32_t pfec = PFEC_page_present | PFEC_write_access;
4602 /* Translate the VA to a GFN */
4603 gfn = sh_gva_to_gfn(v, vaddr, &pfec);
4604 if ( gfn == INVALID_GFN )
4606 if ( is_hvm_vcpu(v) )
4607 hvm_inject_exception(TRAP_page_fault, pfec, vaddr);
4608 else
4609 propagate_page_fault(vaddr, pfec);
4610 return _mfn(BAD_GVA_TO_GFN);
4613 /* Translate the GFN to an MFN */
4614 mfn = gfn_to_mfn(v->domain, _gfn(gfn), &p2mt);
4615 if ( p2m_is_ram(p2mt) )
4617 ASSERT(mfn_valid(mfn));
4618 v->arch.paging.last_write_was_pt = !!sh_mfn_is_a_page_table(mfn);
4619 return mfn;
4622 return _mfn(BAD_GFN_TO_MFN);
4625 /* Check that the user is allowed to perform this write.
4626 * Returns a mapped pointer to write to, or NULL for error. */
4627 #define MAPPING_UNHANDLEABLE ((void *)0)
4628 #define MAPPING_EXCEPTION ((void *)1)
4629 #define emulate_map_dest_failed(rc) ((unsigned long)(rc) <= 1)
4630 static void *emulate_map_dest(struct vcpu *v,
4631 unsigned long vaddr,
4632 u32 bytes,
4633 struct sh_emulate_ctxt *sh_ctxt)
4635 struct segment_register *sreg;
4636 unsigned long offset;
4637 void *map = NULL;
4639 /* We don't emulate user-mode writes to page tables */
4640 sreg = hvm_get_seg_reg(x86_seg_ss, sh_ctxt);
4641 if ( sreg->attr.fields.dpl == 3 )
4642 return MAPPING_UNHANDLEABLE;
4644 sh_ctxt->mfn1 = emulate_gva_to_mfn(v, vaddr, sh_ctxt);
4645 if ( !mfn_valid(sh_ctxt->mfn1) )
4646 return ((mfn_x(sh_ctxt->mfn1) == BAD_GVA_TO_GFN) ?
4647 MAPPING_EXCEPTION : MAPPING_UNHANDLEABLE);
4649 /* Unaligned writes mean probably this isn't a pagetable */
4650 if ( vaddr & (bytes - 1) )
4651 sh_remove_shadows(v, sh_ctxt->mfn1, 0, 0 /* Slow, can fail */ );
4653 if ( likely(((vaddr + bytes - 1) & PAGE_MASK) == (vaddr & PAGE_MASK)) )
4655 /* Whole write fits on a single page */
4656 sh_ctxt->mfn2 = _mfn(INVALID_MFN);
4657 map = sh_map_domain_page(sh_ctxt->mfn1) + (vaddr & ~PAGE_MASK);
4659 else
4661 /* Cross-page emulated writes are only supported for HVM guests;
4662 * PV guests ought to know better */
4663 if ( !is_hvm_vcpu(v) )
4664 return MAPPING_UNHANDLEABLE;
4666 /* This write crosses a page boundary. Translate the second page */
4667 sh_ctxt->mfn2 = emulate_gva_to_mfn(v, (vaddr + bytes - 1) & PAGE_MASK,
4668 sh_ctxt);
4669 if ( !mfn_valid(sh_ctxt->mfn2) )
4670 return ((mfn_x(sh_ctxt->mfn2) == BAD_GVA_TO_GFN) ?
4671 MAPPING_EXCEPTION : MAPPING_UNHANDLEABLE);
4673 /* Cross-page writes mean probably not a pagetable */
4674 sh_remove_shadows(v, sh_ctxt->mfn2, 0, 0 /* Slow, can fail */ );
4676 /* Hack: we map the pages into the vcpu's LDT space, since we
4677 * know that we're not going to need the LDT for HVM guests,
4678 * and only HVM guests are allowed unaligned writes. */
4679 ASSERT(is_hvm_vcpu(v));
4680 map = (void *)LDT_VIRT_START(v);
4681 offset = l1_linear_offset((unsigned long) map);
4682 l1e_write(&__linear_l1_table[offset],
4683 l1e_from_pfn(mfn_x(sh_ctxt->mfn1), __PAGE_HYPERVISOR));
4684 l1e_write(&__linear_l1_table[offset + 1],
4685 l1e_from_pfn(mfn_x(sh_ctxt->mfn2), __PAGE_HYPERVISOR));
4686 flush_tlb_local();
4687 map += (vaddr & ~PAGE_MASK);
4690 #if (SHADOW_OPTIMIZATIONS & SHOPT_SKIP_VERIFY)
4691 /* Remember if the bottom bit was clear, so we can choose not to run
4692 * the change through the verify code if it's still clear afterwards */
4693 sh_ctxt->low_bit_was_clear = map != NULL && !(*(u8 *)map & _PAGE_PRESENT);
4694 #endif
4696 return map;
4699 /* Tidy up after the emulated write: mark pages dirty, verify the new
4700 * contents, and undo the mapping */
4701 static void emulate_unmap_dest(struct vcpu *v,
4702 void *addr,
4703 u32 bytes,
4704 struct sh_emulate_ctxt *sh_ctxt)
4706 u32 b1 = bytes, b2 = 0, shflags;
4708 ASSERT(mfn_valid(sh_ctxt->mfn1));
4710 /* If we are writing lots of PTE-aligned zeros, might want to unshadow */
4711 if ( likely(bytes >= 4)
4712 && (*(u32 *)addr == 0)
4713 && ((unsigned long) addr & ((sizeof (guest_intpte_t)) - 1)) == 0 )
4714 check_for_early_unshadow(v, sh_ctxt->mfn1);
4715 else
4716 reset_early_unshadow(v);
4718 /* We can avoid re-verifying the page contents after the write if:
4719 * - it was no larger than the PTE type of this pagetable;
4720 * - it was aligned to the PTE boundaries; and
4721 * - _PAGE_PRESENT was clear before and after the write. */
4722 shflags = mfn_to_page(sh_ctxt->mfn1)->shadow_flags;
4723 #if (SHADOW_OPTIMIZATIONS & SHOPT_SKIP_VERIFY)
4724 if ( sh_ctxt->low_bit_was_clear
4725 && !(*(u8 *)addr & _PAGE_PRESENT)
4726 && ((!(shflags & SHF_32)
4727 /* Not shadowed 32-bit: aligned 64-bit writes that leave
4728 * the present bit unset are safe to ignore. */
4729 && ((unsigned long)addr & 7) == 0
4730 && bytes <= 8)
4731 ||
4732 (!(shflags & (SHF_PAE|SHF_64))
4733 /* Not shadowed PAE/64-bit: aligned 32-bit writes that
4734 * leave the present bit unset are safe to ignore. */
4735 && ((unsigned long)addr & 3) == 0
4736 && bytes <= 4)) )
4738 /* Writes with this alignment constraint can't possibly cross pages */
4739 ASSERT(!mfn_valid(sh_ctxt->mfn2));
4741 else
4742 #endif /* SHADOW_OPTIMIZATIONS & SHOPT_SKIP_VERIFY */
4744 if ( unlikely(mfn_valid(sh_ctxt->mfn2)) )
4746 /* Validate as two writes, one to each page */
4747 b1 = PAGE_SIZE - (((unsigned long)addr) & ~PAGE_MASK);
4748 b2 = bytes - b1;
4749 ASSERT(b2 < bytes);
4751 if ( likely(b1 > 0) )
4752 sh_validate_guest_pt_write(v, sh_ctxt->mfn1, addr, b1);
4753 if ( unlikely(b2 > 0) )
4754 sh_validate_guest_pt_write(v, sh_ctxt->mfn2, addr + b1, b2);
4757 paging_mark_dirty(v->domain, mfn_x(sh_ctxt->mfn1));
4759 if ( unlikely(mfn_valid(sh_ctxt->mfn2)) )
4761 unsigned long offset;
4762 paging_mark_dirty(v->domain, mfn_x(sh_ctxt->mfn2));
4763 /* Undo the hacky two-frame contiguous map. */
4764 ASSERT(((unsigned long) addr & PAGE_MASK) == LDT_VIRT_START(v));
4765 offset = l1_linear_offset((unsigned long) addr);
4766 l1e_write(&__linear_l1_table[offset], l1e_empty());
4767 l1e_write(&__linear_l1_table[offset + 1], l1e_empty());
4768 flush_tlb_all();
4770 else
4771 sh_unmap_domain_page(addr);
4773 atomic_inc(&v->domain->arch.paging.shadow.gtable_dirty_version);
4776 static int
4777 sh_x86_emulate_write(struct vcpu *v, unsigned long vaddr, void *src,
4778 u32 bytes, struct sh_emulate_ctxt *sh_ctxt)
4780 void *addr;
4782 /* Unaligned writes are only acceptable on HVM */
4783 if ( (vaddr & (bytes - 1)) && !is_hvm_vcpu(v) )
4784 return X86EMUL_UNHANDLEABLE;
4786 addr = emulate_map_dest(v, vaddr, bytes, sh_ctxt);
4787 if ( emulate_map_dest_failed(addr) )
4788 return ((addr == MAPPING_EXCEPTION) ?
4789 X86EMUL_EXCEPTION : X86EMUL_UNHANDLEABLE);
4791 shadow_lock(v->domain);
4792 memcpy(addr, src, bytes);
4794 emulate_unmap_dest(v, addr, bytes, sh_ctxt);
4795 shadow_audit_tables(v);
4796 shadow_unlock(v->domain);
4797 return X86EMUL_OKAY;
4800 static int
4801 sh_x86_emulate_cmpxchg(struct vcpu *v, unsigned long vaddr,
4802 unsigned long old, unsigned long new,
4803 unsigned int bytes, struct sh_emulate_ctxt *sh_ctxt)
4805 void *addr;
4806 unsigned long prev;
4807 int rv = X86EMUL_OKAY;
4809 /* Unaligned writes are only acceptable on HVM */
4810 if ( (vaddr & (bytes - 1)) && !is_hvm_vcpu(v) )
4811 return X86EMUL_UNHANDLEABLE;
4813 addr = emulate_map_dest(v, vaddr, bytes, sh_ctxt);
4814 if ( emulate_map_dest_failed(addr) )
4815 return ((addr == MAPPING_EXCEPTION) ?
4816 X86EMUL_EXCEPTION : X86EMUL_UNHANDLEABLE);
4818 shadow_lock(v->domain);
4819 switch ( bytes )
4821 case 1: prev = cmpxchg(((u8 *)addr), old, new); break;
4822 case 2: prev = cmpxchg(((u16 *)addr), old, new); break;
4823 case 4: prev = cmpxchg(((u32 *)addr), old, new); break;
4824 case 8: prev = cmpxchg(((u64 *)addr), old, new); break;
4825 default:
4826 SHADOW_PRINTK("cmpxchg of size %i is not supported\n", bytes);
4827 prev = ~old;
4830 if ( prev != old )
4831 rv = X86EMUL_CMPXCHG_FAILED;
4833 SHADOW_DEBUG(EMULATE, "va %#lx was %#lx expected %#lx"
4834 " wanted %#lx now %#lx bytes %u\n",
4835 vaddr, prev, old, new, *(unsigned long *)addr, bytes);
4837 emulate_unmap_dest(v, addr, bytes, sh_ctxt);
4838 shadow_audit_tables(v);
4839 shadow_unlock(v->domain);
4840 return rv;
4843 #ifdef __i386__
4844 static int
4845 sh_x86_emulate_cmpxchg8b(struct vcpu *v, unsigned long vaddr,
4846 unsigned long old_lo, unsigned long old_hi,
4847 unsigned long new_lo, unsigned long new_hi,
4848 struct sh_emulate_ctxt *sh_ctxt)
4850 void *addr;
4851 u64 old, new, prev;
4852 int rv = X86EMUL_OKAY;
4854 /* Unaligned writes are only acceptable on HVM */
4855 if ( (vaddr & 7) && !is_hvm_vcpu(v) )
4856 return X86EMUL_UNHANDLEABLE;
4858 addr = emulate_map_dest(v, vaddr, 8, sh_ctxt);
4859 if ( emulate_map_dest_failed(addr) )
4860 return ((addr == MAPPING_EXCEPTION) ?
4861 X86EMUL_EXCEPTION : X86EMUL_UNHANDLEABLE);
4863 old = (((u64) old_hi) << 32) | (u64) old_lo;
4864 new = (((u64) new_hi) << 32) | (u64) new_lo;
4866 shadow_lock(v->domain);
4867 prev = cmpxchg(((u64 *)addr), old, new);
4869 if ( prev != old )
4870 rv = X86EMUL_CMPXCHG_FAILED;
4872 emulate_unmap_dest(v, addr, 8, sh_ctxt);
4873 shadow_audit_tables(v);
4874 shadow_unlock(v->domain);
4875 return rv;
4877 #endif
4879 /**************************************************************************/
4880 /* Audit tools */
4882 #if SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES
4884 #define AUDIT_FAIL(_level, _fmt, _a...) do { \
4885 printk("Shadow %u-on-%u audit failed at level %i, index %i\n" \
4886 "gl" #_level "mfn = %" PRI_mfn \
4887 " sl" #_level "mfn = %" PRI_mfn \
4888 " &gl" #_level "e = %p &sl" #_level "e = %p" \
4889 " gl" #_level "e = %" SH_PRI_gpte \
4890 " sl" #_level "e = %" SH_PRI_pte "\nError: " _fmt "\n", \
4891 GUEST_PAGING_LEVELS, SHADOW_PAGING_LEVELS, \
4892 _level, guest_index(gl ## _level ## e), \
4893 mfn_x(gl ## _level ## mfn), mfn_x(sl ## _level ## mfn), \
4894 gl ## _level ## e, sl ## _level ## e, \
4895 gl ## _level ## e->l ## _level, sl ## _level ## e->l ## _level, \
4896 ##_a); \
4897 BUG(); \
4898 done = 1; \
4899 } while (0)
4901 #define AUDIT_FAIL_MIN(_level, _fmt, _a...) do { \
4902 printk("Shadow %u-on-%u audit failed at level %i\n" \
4903 "gl" #_level "mfn = %" PRI_mfn \
4904 " sl" #_level "mfn = %" PRI_mfn \
4905 " Error: " _fmt "\n", \
4906 GUEST_PAGING_LEVELS, SHADOW_PAGING_LEVELS, \
4907 _level, \
4908 mfn_x(gl ## _level ## mfn), mfn_x(sl ## _level ## mfn), \
4909 ##_a); \
4910 BUG(); \
4911 done = 1; \
4912 } while (0)
4914 static char * sh_audit_flags(struct vcpu *v, int level,
4915 int gflags, int sflags)
4916 /* Common code for auditing flag bits */
4918 if ( (sflags & _PAGE_PRESENT) && !(gflags & _PAGE_PRESENT) )
4919 return "shadow is present but guest is not present";
4920 if ( (sflags & _PAGE_GLOBAL) && !is_hvm_vcpu(v) )
4921 return "global bit set in PV shadow";
4922 if ( level == 2 && (sflags & _PAGE_PSE) )
4923 return "PS bit set in shadow";
4924 #if SHADOW_PAGING_LEVELS == 3
4925 if ( level == 3 ) return NULL; /* All the other bits are blank in PAEl3 */
4926 #endif
4927 if ( (sflags & _PAGE_PRESENT) && !(gflags & _PAGE_ACCESSED) )
4928 return "accessed bit not propagated";
4929 if ( (level == 1 || (level == 2 && (gflags & _PAGE_PSE)))
4930 && ((sflags & _PAGE_RW) && !(gflags & _PAGE_DIRTY)) )
4931 return "dirty bit not propagated";
4932 if ( (sflags & _PAGE_USER) != (gflags & _PAGE_USER) )
4933 return "user/supervisor bit does not match";
4934 if ( (sflags & _PAGE_NX_BIT) != (gflags & _PAGE_NX_BIT) )
4935 return "NX bit does not match";
4936 if ( (sflags & _PAGE_RW) && !(gflags & _PAGE_RW) )
4937 return "shadow grants write access but guest does not";
4938 return NULL;
4941 int sh_audit_l1_table(struct vcpu *v, mfn_t sl1mfn, mfn_t x)
4943 guest_l1e_t *gl1e, *gp;
4944 shadow_l1e_t *sl1e;
4945 mfn_t mfn, gmfn, gl1mfn;
4946 gfn_t gfn;
4947 p2m_type_t p2mt;
4948 char *s;
4949 int done = 0;
4951 /* Follow the backpointer */
4952 gl1mfn = _mfn(mfn_to_shadow_page(sl1mfn)->backpointer);
4954 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
4955 /* Out-of-sync l1 shadows can contain anything: just check the OOS hash */
4956 if ( page_is_out_of_sync(mfn_to_page(gl1mfn)) )
4958 oos_audit_hash_is_present(v->domain, gl1mfn);
4959 return 0;
4961 #endif
4963 gl1e = gp = sh_map_domain_page(gl1mfn);
4964 SHADOW_FOREACH_L1E(sl1mfn, sl1e, &gl1e, done, {
4966 if ( sh_l1e_is_magic(*sl1e) )
4968 #if (SHADOW_OPTIMIZATIONS & SHOPT_FAST_FAULT_PATH)
4969 if ( sh_l1e_is_gnp(*sl1e) )
4971 if ( guest_l1e_get_flags(*gl1e) & _PAGE_PRESENT )
4972 AUDIT_FAIL(1, "shadow is GNP magic but guest is present");
4974 else
4976 ASSERT(sh_l1e_is_mmio(*sl1e));
4977 gfn = sh_l1e_mmio_get_gfn(*sl1e);
4978 if ( gfn_x(gfn) != gfn_x(guest_l1e_get_gfn(*gl1e)) )
4979 AUDIT_FAIL(1, "shadow MMIO gfn is %" SH_PRI_gfn
4980 " but guest gfn is %" SH_PRI_gfn,
4981 gfn_x(gfn),
4982 gfn_x(guest_l1e_get_gfn(*gl1e)));
4984 #endif
4986 else
4988 s = sh_audit_flags(v, 1, guest_l1e_get_flags(*gl1e),
4989 shadow_l1e_get_flags(*sl1e));
4990 if ( s ) AUDIT_FAIL(1, "%s", s);
4992 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4994 gfn = guest_l1e_get_gfn(*gl1e);
4995 mfn = shadow_l1e_get_mfn(*sl1e);
4996 gmfn = gfn_to_mfn(v->domain, gfn, &p2mt);
4997 if ( mfn_x(gmfn) != mfn_x(mfn) )
4998 AUDIT_FAIL(1, "bad translation: gfn %" SH_PRI_gfn
4999 " --> %" PRI_mfn " != mfn %" PRI_mfn,
5000 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
5003 });
5004 sh_unmap_domain_page(gp);
5005 return done;
5008 int sh_audit_fl1_table(struct vcpu *v, mfn_t sl1mfn, mfn_t x)
5010 guest_l1e_t *gl1e, e;
5011 shadow_l1e_t *sl1e;
5012 mfn_t gl1mfn = _mfn(INVALID_MFN);
5013 int f;
5014 int done = 0;
5016 /* fl1 has no useful backpointer: all we can check are flags */
5017 e = guest_l1e_from_gfn(_gfn(0), 0); gl1e = &e; /* Needed for macro */
5018 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done, {
5019 f = shadow_l1e_get_flags(*sl1e);
5020 f &= ~(_PAGE_AVAIL0|_PAGE_AVAIL1|_PAGE_AVAIL2);
5021 if ( !(f == 0
5022 || f == (_PAGE_PRESENT|_PAGE_USER|_PAGE_RW|
5023 _PAGE_ACCESSED|_PAGE_DIRTY)
5024 || f == (_PAGE_PRESENT|_PAGE_USER|_PAGE_ACCESSED|_PAGE_DIRTY)
5025 || sh_l1e_is_magic(*sl1e)) )
5026 AUDIT_FAIL(1, "fl1e has bad flags");
5027 });
5028 return 0;
5031 int sh_audit_l2_table(struct vcpu *v, mfn_t sl2mfn, mfn_t x)
5033 guest_l2e_t *gl2e, *gp;
5034 shadow_l2e_t *sl2e;
5035 mfn_t mfn, gmfn, gl2mfn;
5036 gfn_t gfn;
5037 p2m_type_t p2mt;
5038 char *s;
5039 int done = 0;
5041 /* Follow the backpointer */
5042 gl2mfn = _mfn(mfn_to_shadow_page(sl2mfn)->backpointer);
5044 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
5045 /* Only L1's may be out of sync. */
5046 if ( page_is_out_of_sync(mfn_to_page(gl2mfn)) )
5047 AUDIT_FAIL_MIN(2, "gmfn %lx is out of sync", mfn_x(gl2mfn));
5048 #endif
5050 gl2e = gp = sh_map_domain_page(gl2mfn);
5051 SHADOW_FOREACH_L2E(sl2mfn, sl2e, &gl2e, done, v->domain, {
5053 s = sh_audit_flags(v, 2, guest_l2e_get_flags(*gl2e),
5054 shadow_l2e_get_flags(*sl2e));
5055 if ( s ) AUDIT_FAIL(2, "%s", s);
5057 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
5059 gfn = guest_l2e_get_gfn(*gl2e);
5060 mfn = shadow_l2e_get_mfn(*sl2e);
5061 gmfn = (guest_l2e_get_flags(*gl2e) & _PAGE_PSE)
5062 ? get_fl1_shadow_status(v, gfn)
5063 : get_shadow_status(v, gfn_to_mfn(v->domain, gfn, &p2mt),
5064 SH_type_l1_shadow);
5065 if ( mfn_x(gmfn) != mfn_x(mfn) )
5066 AUDIT_FAIL(2, "bad translation: gfn %" SH_PRI_gfn
5067 " (--> %" PRI_mfn ")"
5068 " --> %" PRI_mfn " != mfn %" PRI_mfn,
5069 gfn_x(gfn),
5070 (guest_l2e_get_flags(*gl2e) & _PAGE_PSE) ? 0
5071 : mfn_x(gfn_to_mfn(v->domain, gfn, &p2mt)),
5072 mfn_x(gmfn), mfn_x(mfn));
5074 });
5075 sh_unmap_domain_page(gp);
5076 return 0;
5079 #if GUEST_PAGING_LEVELS >= 4
5080 int sh_audit_l3_table(struct vcpu *v, mfn_t sl3mfn, mfn_t x)
5082 guest_l3e_t *gl3e, *gp;
5083 shadow_l3e_t *sl3e;
5084 mfn_t mfn, gmfn, gl3mfn;
5085 gfn_t gfn;
5086 p2m_type_t p2mt;
5087 char *s;
5088 int done = 0;
5090 /* Follow the backpointer */
5091 gl3mfn = _mfn(mfn_to_shadow_page(sl3mfn)->backpointer);
5093 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
5094 /* Only L1's may be out of sync. */
5095 if ( page_is_out_of_sync(mfn_to_page(gl3mfn)) )
5096 AUDIT_FAIL_MIN(3, "gmfn %lx is out of sync", mfn_x(gl3mfn));
5097 #endif
5099 gl3e = gp = sh_map_domain_page(gl3mfn);
5100 SHADOW_FOREACH_L3E(sl3mfn, sl3e, &gl3e, done, {
5102 s = sh_audit_flags(v, 3, guest_l3e_get_flags(*gl3e),
5103 shadow_l3e_get_flags(*sl3e));
5104 if ( s ) AUDIT_FAIL(3, "%s", s);
5106 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
5108 gfn = guest_l3e_get_gfn(*gl3e);
5109 mfn = shadow_l3e_get_mfn(*sl3e);
5110 gmfn = get_shadow_status(v, gfn_to_mfn(v->domain, gfn, &p2mt),
5111 ((GUEST_PAGING_LEVELS == 3 ||
5112 is_pv_32on64_vcpu(v))
5113 && !shadow_mode_external(v->domain)
5114 && (guest_index(gl3e) % 4) == 3)
5115 ? SH_type_l2h_shadow
5116 : SH_type_l2_shadow);
5117 if ( mfn_x(gmfn) != mfn_x(mfn) )
5118 AUDIT_FAIL(3, "bad translation: gfn %" SH_PRI_gfn
5119 " --> %" PRI_mfn " != mfn %" PRI_mfn,
5120 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
5122 });
5123 sh_unmap_domain_page(gp);
5124 return 0;
5127 int sh_audit_l4_table(struct vcpu *v, mfn_t sl4mfn, mfn_t x)
5129 guest_l4e_t *gl4e, *gp;
5130 shadow_l4e_t *sl4e;
5131 mfn_t mfn, gmfn, gl4mfn;
5132 gfn_t gfn;
5133 p2m_type_t p2mt;
5134 char *s;
5135 int done = 0;
5137 /* Follow the backpointer */
5138 gl4mfn = _mfn(mfn_to_shadow_page(sl4mfn)->backpointer);
5140 #if (SHADOW_OPTIMIZATIONS & SHOPT_OUT_OF_SYNC)
5141 /* Only L1's may be out of sync. */
5142 if ( page_is_out_of_sync(mfn_to_page(gl4mfn)) )
5143 AUDIT_FAIL_MIN(4, "gmfn %lx is out of sync", mfn_x(gl4mfn));
5144 #endif
5146 gl4e = gp = sh_map_domain_page(gl4mfn);
5147 SHADOW_FOREACH_L4E(sl4mfn, sl4e, &gl4e, done, v->domain,
5149 s = sh_audit_flags(v, 4, guest_l4e_get_flags(*gl4e),
5150 shadow_l4e_get_flags(*sl4e));
5151 if ( s ) AUDIT_FAIL(4, "%s", s);
5153 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
5155 gfn = guest_l4e_get_gfn(*gl4e);
5156 mfn = shadow_l4e_get_mfn(*sl4e);
5157 gmfn = get_shadow_status(v, gfn_to_mfn(v->domain, gfn, &p2mt),
5158 SH_type_l3_shadow);
5159 if ( mfn_x(gmfn) != mfn_x(mfn) )
5160 AUDIT_FAIL(4, "bad translation: gfn %" SH_PRI_gfn
5161 " --> %" PRI_mfn " != mfn %" PRI_mfn,
5162 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
5164 });
5165 sh_unmap_domain_page(gp);
5166 return 0;
5168 #endif /* GUEST_PAGING_LEVELS >= 4 */
5171 #undef AUDIT_FAIL
5173 #endif /* Audit code */
5175 /**************************************************************************/
5176 /* Entry points into this mode of the shadow code.
5177 * This will all be mangled by the preprocessor to uniquify everything. */
5178 struct paging_mode sh_paging_mode = {
5179 .page_fault = sh_page_fault,
5180 .invlpg = sh_invlpg,
5181 .gva_to_gfn = sh_gva_to_gfn,
5182 .update_cr3 = sh_update_cr3,
5183 .update_paging_modes = shadow_update_paging_modes,
5184 .write_p2m_entry = shadow_write_p2m_entry,
5185 .write_guest_entry = shadow_write_guest_entry,
5186 .cmpxchg_guest_entry = shadow_cmpxchg_guest_entry,
5187 .guest_map_l1e = sh_guest_map_l1e,
5188 .guest_get_eff_l1e = sh_guest_get_eff_l1e,
5189 .guest_levels = GUEST_PAGING_LEVELS,
5190 .shadow.detach_old_tables = sh_detach_old_tables,
5191 .shadow.x86_emulate_write = sh_x86_emulate_write,
5192 .shadow.x86_emulate_cmpxchg = sh_x86_emulate_cmpxchg,
5193 #ifdef __i386__
5194 .shadow.x86_emulate_cmpxchg8b = sh_x86_emulate_cmpxchg8b,
5195 #endif
5196 .shadow.make_monitor_table = sh_make_monitor_table,
5197 .shadow.destroy_monitor_table = sh_destroy_monitor_table,
5198 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
5199 .shadow.guess_wrmap = sh_guess_wrmap,
5200 #endif
5201 .shadow.shadow_levels = SHADOW_PAGING_LEVELS,
5202 };
5204 /*
5205 * Local variables:
5206 * mode: C
5207 * c-set-style: "BSD"
5208 * c-basic-offset: 4
5209 * indent-tabs-mode: nil
5210 * End:
5211 */