direct-io.hg

view xen/arch/x86/mm/shadow/multi.c @ 14083:2c3a13993b0d

[HVM] Only treat gfns in known MMIO regions as MMIO
instead of assuming that all non-RAM addresses are MMIO
Signed-off-by: Tim Deegan <Tim.Deegan@xensource.com>
author Tim Deegan <Tim.Deegan@xensource.com>
date Fri Feb 23 14:44:07 2007 +0000 (2007-02-23)
parents e8470a1a01af
children 78c0ae1f77f2
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 "private.h"
37 #include "types.h"
39 /* THINGS TO DO LATER:
40 *
41 * TEARDOWN HEURISTICS
42 * Also: have a heuristic for when to destroy a previous paging-mode's
43 * shadows. When a guest is done with its start-of-day 32-bit tables
44 * and reuses the memory we want to drop those shadows. Start with
45 * shadows in a page in two modes as a hint, but beware of clever tricks
46 * like reusing a pagetable for both PAE and 64-bit during boot...
47 *
48 * PAE LINEAR MAPS
49 * Rework shadow_get_l*e() to have the option of using map_domain_page()
50 * instead of linear maps. Add appropriate unmap_l*e calls in the users.
51 * Then we can test the speed difference made by linear maps. If the
52 * map_domain_page() version is OK on PAE, we could maybe allow a lightweight
53 * l3-and-l2h-only shadow mode for PAE PV guests that would allow them
54 * to share l2h pages again.
55 *
56 * GUEST_WALK_TABLES TLB FLUSH COALESCE
57 * guest_walk_tables can do up to three remote TLB flushes as it walks to
58 * the first l1 of a new pagetable. Should coalesce the flushes to the end,
59 * and if we do flush, re-do the walk. If anything has changed, then
60 * pause all the other vcpus and do the walk *again*.
61 *
62 * WP DISABLED
63 * Consider how to implement having the WP bit of CR0 set to 0.
64 * Since we need to be able to cause write faults to pagetables, this might
65 * end up looking like not having the (guest) pagetables present at all in
66 * HVM guests...
67 *
68 * PSE disabled / PSE36
69 * We don't support any modes other than PSE enabled, PSE36 disabled.
70 * Neither of those would be hard to change, but we'd need to be able to
71 * deal with shadows made in one mode and used in another.
72 */
74 #define FETCH_TYPE_PREFETCH 1
75 #define FETCH_TYPE_DEMAND 2
76 #define FETCH_TYPE_WRITE 4
77 typedef enum {
78 ft_prefetch = FETCH_TYPE_PREFETCH,
79 ft_demand_read = FETCH_TYPE_DEMAND,
80 ft_demand_write = FETCH_TYPE_DEMAND | FETCH_TYPE_WRITE,
81 } fetch_type_t;
83 #ifdef DEBUG_TRACE_DUMP
84 static char *fetch_type_names[] = {
85 [ft_prefetch] "prefetch",
86 [ft_demand_read] "demand read",
87 [ft_demand_write] "demand write",
88 };
89 #endif
91 /**************************************************************************/
92 /* Hash table mapping from guest pagetables to shadows
93 *
94 * Normal case: maps the mfn of a guest page to the mfn of its shadow page.
95 * FL1's: maps the *gfn* of the start of a superpage to the mfn of a
96 * shadow L1 which maps its "splinters".
97 */
99 static inline mfn_t
100 get_fl1_shadow_status(struct vcpu *v, gfn_t gfn)
101 /* Look for FL1 shadows in the hash table */
102 {
103 mfn_t smfn = shadow_hash_lookup(v, gfn_x(gfn), SH_type_fl1_shadow);
105 if ( unlikely(shadow_mode_log_dirty(v->domain) && mfn_valid(smfn)) )
106 {
107 struct shadow_page_info *sp = mfn_to_shadow_page(smfn);
108 if ( !(sp->logdirty) )
109 shadow_convert_to_log_dirty(v, smfn);
110 }
112 return smfn;
113 }
115 static inline mfn_t
116 get_shadow_status(struct vcpu *v, mfn_t gmfn, u32 shadow_type)
117 /* Look for shadows in the hash table */
118 {
119 mfn_t smfn = shadow_hash_lookup(v, mfn_x(gmfn), shadow_type);
120 perfc_incrc(shadow_get_shadow_status);
122 if ( unlikely(shadow_mode_log_dirty(v->domain) && mfn_valid(smfn)) )
123 {
124 struct shadow_page_info *sp = mfn_to_shadow_page(smfn);
125 if ( !(sp->logdirty) )
126 shadow_convert_to_log_dirty(v, smfn);
127 }
129 return smfn;
130 }
132 static inline void
133 set_fl1_shadow_status(struct vcpu *v, gfn_t gfn, mfn_t smfn)
134 /* Put an FL1 shadow into the hash table */
135 {
136 SHADOW_PRINTK("gfn=%"SH_PRI_gfn", type=%08x, smfn=%05lx\n",
137 gfn_x(gfn), SH_type_fl1_shadow, mfn_x(smfn));
139 if ( unlikely(shadow_mode_log_dirty(v->domain)) )
140 // mark this shadow as a log dirty shadow...
141 mfn_to_shadow_page(smfn)->logdirty = 1;
142 else
143 mfn_to_shadow_page(smfn)->logdirty = 0;
145 shadow_hash_insert(v, gfn_x(gfn), SH_type_fl1_shadow, smfn);
146 }
148 static inline void
149 set_shadow_status(struct vcpu *v, mfn_t gmfn, u32 shadow_type, mfn_t smfn)
150 /* Put a shadow into the hash table */
151 {
152 struct domain *d = v->domain;
153 int res;
155 SHADOW_PRINTK("d=%d, v=%d, gmfn=%05lx, type=%08x, smfn=%05lx\n",
156 d->domain_id, v->vcpu_id, mfn_x(gmfn),
157 shadow_type, mfn_x(smfn));
159 if ( unlikely(shadow_mode_log_dirty(d)) )
160 // mark this shadow as a log dirty shadow...
161 mfn_to_shadow_page(smfn)->logdirty = 1;
162 else
163 mfn_to_shadow_page(smfn)->logdirty = 0;
165 #ifdef CONFIG_COMPAT
166 if ( !IS_COMPAT(d) || shadow_type != SH_type_l4_64_shadow )
167 #endif
168 {
169 res = get_page(mfn_to_page(gmfn), d);
170 ASSERT(res == 1);
171 }
173 shadow_hash_insert(v, mfn_x(gmfn), shadow_type, smfn);
174 }
176 static inline void
177 delete_fl1_shadow_status(struct vcpu *v, gfn_t gfn, mfn_t smfn)
178 /* Remove a shadow from the hash table */
179 {
180 SHADOW_PRINTK("gfn=%"SH_PRI_gfn", type=%08x, smfn=%05lx\n",
181 gfn_x(gfn), SH_type_fl1_shadow, mfn_x(smfn));
182 shadow_hash_delete(v, gfn_x(gfn), SH_type_fl1_shadow, smfn);
183 }
185 static inline void
186 delete_shadow_status(struct vcpu *v, mfn_t gmfn, u32 shadow_type, mfn_t smfn)
187 /* Remove a shadow from the hash table */
188 {
189 SHADOW_PRINTK("d=%d, v=%d, gmfn=%05lx, type=%08x, smfn=%05lx\n",
190 v->domain->domain_id, v->vcpu_id,
191 mfn_x(gmfn), shadow_type, mfn_x(smfn));
192 shadow_hash_delete(v, mfn_x(gmfn), shadow_type, smfn);
193 #ifdef CONFIG_COMPAT
194 if ( !IS_COMPAT(v->domain) || shadow_type != SH_type_l4_64_shadow )
195 #endif
196 put_page(mfn_to_page(gmfn));
197 }
199 /**************************************************************************/
200 /* CPU feature support querying */
202 static inline int
203 guest_supports_superpages(struct vcpu *v)
204 {
205 /* The _PAGE_PSE bit must be honoured in HVM guests, whenever
206 * CR4.PSE is set or the guest is in PAE or long mode */
207 return (is_hvm_vcpu(v) && (GUEST_PAGING_LEVELS != 2
208 || (hvm_get_guest_ctrl_reg(v, 4) & X86_CR4_PSE)));
209 }
211 static inline int
212 guest_supports_nx(struct vcpu *v)
213 {
214 if ( !is_hvm_vcpu(v) )
215 return cpu_has_nx;
217 // XXX - fix this!
218 return 1;
219 }
222 /**************************************************************************/
223 /* Functions for walking the guest page tables */
226 /* Walk the guest pagetables, filling the walk_t with what we see.
227 * Takes an uninitialised walk_t. The caller must call unmap_walk()
228 * on the walk_t before discarding it or calling guest_walk_tables again.
229 * If "guest_op" is non-zero, we are serving a genuine guest memory access,
230 * and must (a) be under the shadow lock, and (b) remove write access
231 * from any gueat PT pages we see, as we will be using their contents to
232 * perform shadow updates.
233 * Returns 0 for success or non-zero if the guest pagetables are malformed.
234 * N.B. Finding a not-present entry does not cause a non-zero return code. */
235 static inline int
236 guest_walk_tables(struct vcpu *v, unsigned long va, walk_t *gw, int guest_op)
237 {
238 ASSERT(!guest_op || shadow_locked_by_me(v->domain));
240 perfc_incrc(shadow_guest_walk);
241 memset(gw, 0, sizeof(*gw));
242 gw->va = va;
244 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
245 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
246 /* Get l4e from the top level table */
247 gw->l4mfn = pagetable_get_mfn(v->arch.guest_table);
248 gw->l4e = (guest_l4e_t *)v->arch.paging.shadow.guest_vtable
249 + guest_l4_table_offset(va);
250 /* Walk down to the l3e */
251 if ( !(guest_l4e_get_flags(*gw->l4e) & _PAGE_PRESENT) ) return 0;
252 gw->l3mfn = vcpu_gfn_to_mfn(v, guest_l4e_get_gfn(*gw->l4e));
253 if ( !mfn_valid(gw->l3mfn) ) return 1;
254 /* This mfn is a pagetable: make sure the guest can't write to it. */
255 if ( guest_op && sh_remove_write_access(v, gw->l3mfn, 3, va) != 0 )
256 flush_tlb_mask(v->domain->domain_dirty_cpumask);
257 gw->l3e = ((guest_l3e_t *)sh_map_domain_page(gw->l3mfn))
258 + guest_l3_table_offset(va);
259 #else /* PAE only... */
260 /* Get l3e from the cache of the guest's top level table */
261 gw->l3e = (guest_l3e_t *)&v->arch.paging.shadow.gl3e[guest_l3_table_offset(va)];
262 #endif /* PAE or 64... */
263 /* Walk down to the l2e */
264 if ( !(guest_l3e_get_flags(*gw->l3e) & _PAGE_PRESENT) ) return 0;
265 gw->l2mfn = vcpu_gfn_to_mfn(v, guest_l3e_get_gfn(*gw->l3e));
266 if ( !mfn_valid(gw->l2mfn) ) return 1;
267 /* This mfn is a pagetable: make sure the guest can't write to it. */
268 if ( guest_op && sh_remove_write_access(v, gw->l2mfn, 2, va) != 0 )
269 flush_tlb_mask(v->domain->domain_dirty_cpumask);
270 gw->l2e = ((guest_l2e_t *)sh_map_domain_page(gw->l2mfn))
271 + guest_l2_table_offset(va);
272 #else /* 32-bit only... */
273 /* Get l2e from the top level table */
274 gw->l2mfn = pagetable_get_mfn(v->arch.guest_table);
275 gw->l2e = (guest_l2e_t *)v->arch.paging.shadow.guest_vtable
276 + guest_l2_table_offset(va);
277 #endif /* All levels... */
279 if ( !(guest_l2e_get_flags(*gw->l2e) & _PAGE_PRESENT) ) return 0;
280 if ( guest_supports_superpages(v) &&
281 (guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE) )
282 {
283 /* Special case: this guest VA is in a PSE superpage, so there's
284 * no guest l1e. We make one up so that the propagation code
285 * can generate a shadow l1 table. Start with the gfn of the
286 * first 4k-page of the superpage. */
287 gfn_t start = guest_l2e_get_gfn(*gw->l2e);
288 /* Grant full access in the l1e, since all the guest entry's
289 * access controls are enforced in the shadow l2e. This lets
290 * us reflect l2 changes later without touching the l1s. */
291 int flags = (_PAGE_PRESENT|_PAGE_USER|_PAGE_RW|
292 _PAGE_ACCESSED|_PAGE_DIRTY);
293 /* PSE level 2 entries use bit 12 for PAT; propagate it to bit 7
294 * of the level 1 */
295 if ( (guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE_PAT) )
296 flags |= _PAGE_PAT;
297 /* Increment the pfn by the right number of 4k pages.
298 * The ~0x1 is to mask out the PAT bit mentioned above. */
299 start = _gfn((gfn_x(start) & ~0x1) + guest_l1_table_offset(va));
300 gw->eff_l1e = guest_l1e_from_gfn(start, flags);
301 gw->l1e = NULL;
302 gw->l1mfn = _mfn(INVALID_MFN);
303 }
304 else
305 {
306 /* Not a superpage: carry on and find the l1e. */
307 gw->l1mfn = vcpu_gfn_to_mfn(v, guest_l2e_get_gfn(*gw->l2e));
308 if ( !mfn_valid(gw->l1mfn) ) return 1;
309 /* This mfn is a pagetable: make sure the guest can't write to it. */
310 if ( guest_op
311 && sh_remove_write_access(v, gw->l1mfn, 1, va) != 0 )
312 flush_tlb_mask(v->domain->domain_dirty_cpumask);
313 gw->l1e = ((guest_l1e_t *)sh_map_domain_page(gw->l1mfn))
314 + guest_l1_table_offset(va);
315 gw->eff_l1e = *gw->l1e;
316 }
318 return 0;
319 }
321 /* Given a walk_t, translate the gw->va into the guest's notion of the
322 * corresponding frame number. */
323 static inline gfn_t
324 guest_walk_to_gfn(walk_t *gw)
325 {
326 if ( !(guest_l1e_get_flags(gw->eff_l1e) & _PAGE_PRESENT) )
327 return _gfn(INVALID_GFN);
328 return guest_l1e_get_gfn(gw->eff_l1e);
329 }
331 /* Given a walk_t, translate the gw->va into the guest's notion of the
332 * corresponding physical address. */
333 static inline paddr_t
334 guest_walk_to_gpa(walk_t *gw)
335 {
336 if ( !(guest_l1e_get_flags(gw->eff_l1e) & _PAGE_PRESENT) )
337 return 0;
338 return guest_l1e_get_paddr(gw->eff_l1e) + (gw->va & ~PAGE_MASK);
339 }
342 /* Unmap (and reinitialise) a guest walk.
343 * Call this to dispose of any walk filled in by guest_walk_tables() */
344 static void unmap_walk(struct vcpu *v, walk_t *gw)
345 {
346 #if GUEST_PAGING_LEVELS >= 3
347 #if GUEST_PAGING_LEVELS >= 4
348 if ( gw->l3e != NULL ) sh_unmap_domain_page(gw->l3e);
349 #endif
350 if ( gw->l2e != NULL ) sh_unmap_domain_page(gw->l2e);
351 #endif
352 if ( gw->l1e != NULL ) sh_unmap_domain_page(gw->l1e);
353 #ifdef DEBUG
354 memset(gw, 0, sizeof(*gw));
355 #endif
356 }
359 /* Pretty-print the contents of a guest-walk */
360 static inline void print_gw(walk_t *gw)
361 {
362 SHADOW_PRINTK("GUEST WALK TO %#lx:\n", gw->va);
363 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
364 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
365 SHADOW_PRINTK(" l4mfn=%" PRI_mfn "\n", mfn_x(gw->l4mfn));
366 SHADOW_PRINTK(" l4e=%p\n", gw->l4e);
367 if ( gw->l4e )
368 SHADOW_PRINTK(" *l4e=%" SH_PRI_gpte "\n", gw->l4e->l4);
369 SHADOW_PRINTK(" l3mfn=%" PRI_mfn "\n", mfn_x(gw->l3mfn));
370 #endif /* PAE or 64... */
371 SHADOW_PRINTK(" l3e=%p\n", gw->l3e);
372 if ( gw->l3e )
373 SHADOW_PRINTK(" *l3e=%" SH_PRI_gpte "\n", gw->l3e->l3);
374 #endif /* All levels... */
375 SHADOW_PRINTK(" l2mfn=%" PRI_mfn "\n", mfn_x(gw->l2mfn));
376 SHADOW_PRINTK(" l2e=%p\n", gw->l2e);
377 if ( gw->l2e )
378 SHADOW_PRINTK(" *l2e=%" SH_PRI_gpte "\n", gw->l2e->l2);
379 SHADOW_PRINTK(" l1mfn=%" PRI_mfn "\n", mfn_x(gw->l1mfn));
380 SHADOW_PRINTK(" l1e=%p\n", gw->l1e);
381 if ( gw->l1e )
382 SHADOW_PRINTK(" *l1e=%" SH_PRI_gpte "\n", gw->l1e->l1);
383 SHADOW_PRINTK(" eff_l1e=%" SH_PRI_gpte "\n", gw->eff_l1e.l1);
384 }
387 #if SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES
388 /* Lightweight audit: pass all the shadows associated with this guest walk
389 * through the audit mechanisms */
390 static void sh_audit_gw(struct vcpu *v, walk_t *gw)
391 {
392 mfn_t smfn;
394 if ( !(SHADOW_AUDIT_ENABLE) )
395 return;
397 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
398 if ( mfn_valid(gw->l4mfn)
399 && mfn_valid((smfn = get_shadow_status(v, gw->l4mfn,
400 SH_type_l4_shadow))) )
401 (void) sh_audit_l4_table(v, smfn, _mfn(INVALID_MFN));
402 if ( mfn_valid(gw->l3mfn)
403 && mfn_valid((smfn = get_shadow_status(v, gw->l3mfn,
404 SH_type_l3_shadow))) )
405 (void) sh_audit_l3_table(v, smfn, _mfn(INVALID_MFN));
406 #endif /* PAE or 64... */
407 if ( mfn_valid(gw->l2mfn) )
408 {
409 if ( mfn_valid((smfn = get_shadow_status(v, gw->l2mfn,
410 SH_type_l2_shadow))) )
411 (void) sh_audit_l2_table(v, smfn, _mfn(INVALID_MFN));
412 #if GUEST_PAGING_LEVELS == 3
413 if ( mfn_valid((smfn = get_shadow_status(v, gw->l2mfn,
414 SH_type_l2h_shadow))) )
415 (void) sh_audit_l2_table(v, smfn, _mfn(INVALID_MFN));
416 #endif
417 }
418 if ( mfn_valid(gw->l1mfn)
419 && mfn_valid((smfn = get_shadow_status(v, gw->l1mfn,
420 SH_type_l1_shadow))) )
421 (void) sh_audit_l1_table(v, smfn, _mfn(INVALID_MFN));
422 else if ( gw->l2e
423 && (guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE)
424 && mfn_valid(
425 (smfn = get_fl1_shadow_status(v, guest_l2e_get_gfn(*gw->l2e)))) )
426 (void) sh_audit_fl1_table(v, smfn, _mfn(INVALID_MFN));
427 }
429 #else
430 #define sh_audit_gw(_v, _gw) do {} while(0)
431 #endif /* audit code */
435 /**************************************************************************/
436 /* Function to write to the guest tables, for propagating accessed and
437 * dirty bits from the shadow to the guest.
438 * Takes a guest mfn, a pointer to the guest entry, the level of pagetable,
439 * and an operation type. The guest entry is always passed as an l1e:
440 * since we only ever write flags, that's OK.
441 * Returns the new flag bits of the guest entry. */
443 static u32 guest_set_ad_bits(struct vcpu *v,
444 mfn_t gmfn,
445 guest_l1e_t *ep,
446 unsigned int level,
447 fetch_type_t ft)
448 {
449 u32 flags;
450 int res = 0;
452 ASSERT(ep && !(((unsigned long)ep) & ((sizeof *ep) - 1)));
453 ASSERT(level <= GUEST_PAGING_LEVELS);
454 ASSERT(shadow_locked_by_me(v->domain));
456 flags = guest_l1e_get_flags(*ep);
458 /* Only set A and D bits for guest-initiated accesses */
459 if ( !(ft & FETCH_TYPE_DEMAND) )
460 return flags;
462 ASSERT(mfn_valid(gmfn)
463 && (sh_mfn_is_a_page_table(gmfn)
464 || ((mfn_to_page(gmfn)->u.inuse.type_info & PGT_count_mask)
465 == 0)));
467 /* PAE l3s do not have A and D bits */
468 ASSERT(GUEST_PAGING_LEVELS > 3 || level != 3);
470 /* Need the D bit as well for writes, in L1es and PSE L2es. */
471 if ( ft == ft_demand_write
472 && (level == 1 ||
473 (level == 2 && (flags & _PAGE_PSE) && guest_supports_superpages(v))) )
474 {
475 if ( (flags & (_PAGE_DIRTY | _PAGE_ACCESSED))
476 == (_PAGE_DIRTY | _PAGE_ACCESSED) )
477 return flags; /* Guest already has A and D bits set */
478 flags |= _PAGE_DIRTY | _PAGE_ACCESSED;
479 perfc_incrc(shadow_ad_update);
480 }
481 else
482 {
483 if ( flags & _PAGE_ACCESSED )
484 return flags; /* Guest already has A bit set */
485 flags |= _PAGE_ACCESSED;
486 perfc_incrc(shadow_a_update);
487 }
489 /* Set the bit(s) */
490 sh_mark_dirty(v->domain, gmfn);
491 SHADOW_DEBUG(A_AND_D, "gfn = %" SH_PRI_gfn ", "
492 "old flags = %#x, new flags = %#x\n",
493 gfn_x(guest_l1e_get_gfn(*ep)), guest_l1e_get_flags(*ep),
494 flags);
495 *ep = guest_l1e_from_gfn(guest_l1e_get_gfn(*ep), flags);
497 /* Propagate this change to any other shadows of the page
498 * (only necessary if there is more than one shadow) */
499 if ( mfn_to_page(gmfn)->count_info & PGC_page_table )
500 {
501 u32 shflags = mfn_to_page(gmfn)->shadow_flags & SHF_page_type_mask;
502 /* More than one type bit set in shadow-flags? */
503 if ( shflags & ~(1UL << find_first_set_bit(shflags)) )
504 res = sh_validate_guest_entry(v, gmfn, ep, sizeof (*ep));
505 }
507 /* We should never need to flush the TLB or recopy PAE entries */
508 ASSERT((res == 0) || (res == SHADOW_SET_CHANGED));
510 return flags;
511 }
513 #if (CONFIG_PAGING_LEVELS == GUEST_PAGING_LEVELS) && (CONFIG_PAGING_LEVELS == SHADOW_PAGING_LEVELS)
514 void *
515 sh_guest_map_l1e(struct vcpu *v, unsigned long addr,
516 unsigned long *gl1mfn)
517 {
518 void *pl1e = NULL;
519 walk_t gw;
521 ASSERT(shadow_mode_translate(v->domain));
523 // XXX -- this is expensive, but it's easy to cobble together...
524 // FIXME!
526 shadow_lock(v->domain);
527 guest_walk_tables(v, addr, &gw, 1);
529 if ( gw.l2e &&
530 (guest_l2e_get_flags(*gw.l2e) & _PAGE_PRESENT) &&
531 !(guest_supports_superpages(v) && (guest_l2e_get_flags(*gw.l2e) & _PAGE_PSE)) )
532 {
533 if ( gl1mfn )
534 *gl1mfn = mfn_x(gw.l1mfn);
535 pl1e = map_domain_page(mfn_x(gw.l1mfn)) +
536 (guest_l1_table_offset(addr) * sizeof(guest_l1e_t));
537 }
539 unmap_walk(v, &gw);
540 shadow_unlock(v->domain);
542 return pl1e;
543 }
545 void
546 sh_guest_get_eff_l1e(struct vcpu *v, unsigned long addr, void *eff_l1e)
547 {
548 walk_t gw;
550 ASSERT(shadow_mode_translate(v->domain));
552 // XXX -- this is expensive, but it's easy to cobble together...
553 // FIXME!
555 shadow_lock(v->domain);
556 guest_walk_tables(v, addr, &gw, 1);
557 *(guest_l1e_t *)eff_l1e = gw.eff_l1e;
558 unmap_walk(v, &gw);
559 shadow_unlock(v->domain);
560 }
561 #endif /* CONFIG==SHADOW==GUEST */
563 /**************************************************************************/
564 /* Functions to compute the correct index into a shadow page, given an
565 * index into the guest page (as returned by guest_get_index()).
566 * This is trivial when the shadow and guest use the same sized PTEs, but
567 * gets more interesting when those sizes are mismatched (e.g. 32-bit guest,
568 * PAE- or 64-bit shadows).
569 *
570 * These functions also increment the shadow mfn, when necessary. When PTE
571 * sizes are mismatched, it takes 2 shadow L1 pages for a single guest L1
572 * page. In this case, we allocate 2 contiguous pages for the shadow L1, and
573 * use simple pointer arithmetic on a pointer to the guest L1e to figure out
574 * which shadow page we really want. Similarly, when PTE sizes are
575 * mismatched, we shadow a guest L2 page with 4 shadow L2 pages. (The easiest
576 * way to see this is: a 32-bit guest L2 page maps 4GB of virtual address
577 * space, while a PAE- or 64-bit shadow L2 page maps 1GB of virtual address
578 * space.)
579 *
580 * For PAE guests, for every 32-bytes of guest L3 page table, we use 64-bytes
581 * of shadow (to store both the shadow, and the info that would normally be
582 * stored in page_info fields). This arrangement allows the shadow and the
583 * "page_info" fields to always be stored in the same page (in fact, in
584 * the same cache line), avoiding an extra call to map_domain_page().
585 */
587 static inline u32
588 guest_index(void *ptr)
589 {
590 return (u32)((unsigned long)ptr & ~PAGE_MASK) / sizeof(guest_l1e_t);
591 }
593 static u32
594 shadow_l1_index(mfn_t *smfn, u32 guest_index)
595 {
596 #if (GUEST_PAGING_LEVELS == 2) && (SHADOW_PAGING_LEVELS != 2)
597 *smfn = _mfn(mfn_x(*smfn) +
598 (guest_index / SHADOW_L1_PAGETABLE_ENTRIES));
599 return (guest_index % SHADOW_L1_PAGETABLE_ENTRIES);
600 #else
601 return guest_index;
602 #endif
603 }
605 static u32
606 shadow_l2_index(mfn_t *smfn, u32 guest_index)
607 {
608 #if (GUEST_PAGING_LEVELS == 2) && (SHADOW_PAGING_LEVELS != 2)
609 // Because we use 2 shadow l2 entries for each guest entry, the number of
610 // guest entries per shadow page is SHADOW_L2_PAGETABLE_ENTRIES/2
611 //
612 *smfn = _mfn(mfn_x(*smfn) +
613 (guest_index / (SHADOW_L2_PAGETABLE_ENTRIES / 2)));
615 // We multiple by two to get the index of the first of the two entries
616 // used to shadow the specified guest entry.
617 return (guest_index % (SHADOW_L2_PAGETABLE_ENTRIES / 2)) * 2;
618 #else
619 return guest_index;
620 #endif
621 }
623 #if GUEST_PAGING_LEVELS >= 4
625 static u32
626 shadow_l3_index(mfn_t *smfn, u32 guest_index)
627 {
628 return guest_index;
629 }
631 static u32
632 shadow_l4_index(mfn_t *smfn, u32 guest_index)
633 {
634 return guest_index;
635 }
637 #endif // GUEST_PAGING_LEVELS >= 4
640 /**************************************************************************/
641 /* Function which computes shadow entries from their corresponding guest
642 * entries. This is the "heart" of the shadow code. It operates using
643 * level-1 shadow types, but handles all levels of entry.
644 * Don't call it directly, but use the four wrappers below.
645 */
647 static always_inline void
648 _sh_propagate(struct vcpu *v,
649 void *guest_entry_ptr,
650 mfn_t guest_table_mfn,
651 mfn_t target_mfn,
652 void *shadow_entry_ptr,
653 int level,
654 fetch_type_t ft,
655 int mmio)
656 {
657 guest_l1e_t *gp = guest_entry_ptr;
658 shadow_l1e_t *sp = shadow_entry_ptr;
659 struct domain *d = v->domain;
660 u32 pass_thru_flags;
661 u32 gflags, sflags;
663 /* We don't shadow PAE l3s */
664 ASSERT(GUEST_PAGING_LEVELS > 3 || level != 3);
666 if ( mfn_valid(guest_table_mfn) )
667 /* Handle A and D bit propagation into the guest */
668 gflags = guest_set_ad_bits(v, guest_table_mfn, gp, level, ft);
669 else
670 {
671 /* Must be an fl1e or a prefetch */
672 ASSERT(level==1 || !(ft & FETCH_TYPE_DEMAND));
673 gflags = guest_l1e_get_flags(*gp);
674 }
676 if ( unlikely(!(gflags & _PAGE_PRESENT)) )
677 {
678 /* If a guest l1 entry is not present, shadow with the magic
679 * guest-not-present entry. */
680 if ( level == 1 )
681 *sp = sh_l1e_gnp();
682 else
683 *sp = shadow_l1e_empty();
684 goto done;
685 }
687 if ( level == 1 && mmio )
688 {
689 /* Guest l1e maps MMIO space */
690 *sp = sh_l1e_mmio(guest_l1e_get_gfn(*gp), gflags);
691 goto done;
692 }
694 // Must have a valid target_mfn, unless this is a prefetch. In the
695 // case of a prefetch, an invalid mfn means that we can not usefully
696 // shadow anything, and so we return early.
697 //
698 if ( !mfn_valid(target_mfn) )
699 {
700 ASSERT((ft == ft_prefetch));
701 *sp = shadow_l1e_empty();
702 goto done;
703 }
705 // Propagate bits from the guest to the shadow.
706 // Some of these may be overwritten, below.
707 // Since we know the guest's PRESENT bit is set, we also set the shadow's
708 // SHADOW_PRESENT bit.
709 //
710 pass_thru_flags = (_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_USER |
711 _PAGE_RW | _PAGE_PRESENT);
712 if ( guest_supports_nx(v) )
713 pass_thru_flags |= _PAGE_NX_BIT;
714 sflags = gflags & pass_thru_flags;
716 // Set the A&D bits for higher level shadows.
717 // Higher level entries do not, strictly speaking, have dirty bits, but
718 // since we use shadow linear tables, each of these entries may, at some
719 // point in time, also serve as a shadow L1 entry.
720 // By setting both the A&D bits in each of these, we eliminate the burden
721 // on the hardware to update these bits on initial accesses.
722 //
723 if ( (level > 1) && !((SHADOW_PAGING_LEVELS == 3) && (level == 3)) )
724 sflags |= _PAGE_ACCESSED | _PAGE_DIRTY;
726 // If the A or D bit has not yet been set in the guest, then we must
727 // prevent the corresponding kind of access.
728 //
729 if ( unlikely(!(gflags & _PAGE_ACCESSED)) )
730 sflags &= ~_PAGE_PRESENT;
732 /* D bits exist in L1es and PSE L2es */
733 if ( unlikely(((level == 1) ||
734 ((level == 2) &&
735 (gflags & _PAGE_PSE) &&
736 guest_supports_superpages(v)))
737 && !(gflags & _PAGE_DIRTY)) )
738 sflags &= ~_PAGE_RW;
740 // shadow_mode_log_dirty support
741 //
742 // Only allow the guest write access to a page a) on a demand fault,
743 // or b) if the page is already marked as dirty.
744 //
745 if ( unlikely((level == 1) && shadow_mode_log_dirty(d)) )
746 {
747 if ( ft & FETCH_TYPE_WRITE )
748 sh_mark_dirty(d, target_mfn);
749 else if ( !sh_mfn_is_dirty(d, target_mfn) )
750 sflags &= ~_PAGE_RW;
751 }
753 // protect guest page tables
754 //
755 if ( unlikely((level == 1) && sh_mfn_is_a_page_table(target_mfn)) )
756 {
757 if ( shadow_mode_trap_reads(d) )
758 {
759 // if we are trapping both reads & writes, then mark this page
760 // as not present...
761 //
762 sflags &= ~_PAGE_PRESENT;
763 }
764 else
765 {
766 // otherwise, just prevent any writes...
767 //
768 sflags &= ~_PAGE_RW;
769 }
770 }
772 // PV guests in 64-bit mode use two different page tables for user vs
773 // supervisor permissions, making the guest's _PAGE_USER bit irrelevant.
774 // It is always shadowed as present...
775 if ( (GUEST_PAGING_LEVELS == 4) && !IS_COMPAT(d) && !is_hvm_domain(d) )
776 {
777 sflags |= _PAGE_USER;
778 }
780 *sp = shadow_l1e_from_mfn(target_mfn, sflags);
781 done:
782 SHADOW_DEBUG(PROPAGATE,
783 "%s level %u guest %" SH_PRI_gpte " shadow %" SH_PRI_pte "\n",
784 fetch_type_names[ft], level, gp->l1, sp->l1);
785 }
788 /* These four wrappers give us a little bit of type-safety back around the
789 * use of void-* pointers in _sh_propagate(), and allow the compiler to
790 * optimize out some level checks. */
792 #if GUEST_PAGING_LEVELS >= 4
793 static void
794 l4e_propagate_from_guest(struct vcpu *v,
795 guest_l4e_t *gl4e,
796 mfn_t gl4mfn,
797 mfn_t sl3mfn,
798 shadow_l4e_t *sl4e,
799 fetch_type_t ft)
800 {
801 _sh_propagate(v, gl4e, gl4mfn, sl3mfn, sl4e, 4, ft, 0);
802 }
804 static void
805 l3e_propagate_from_guest(struct vcpu *v,
806 guest_l3e_t *gl3e,
807 mfn_t gl3mfn,
808 mfn_t sl2mfn,
809 shadow_l3e_t *sl3e,
810 fetch_type_t ft)
811 {
812 _sh_propagate(v, gl3e, gl3mfn, sl2mfn, sl3e, 3, ft, 0);
813 }
814 #endif // GUEST_PAGING_LEVELS >= 4
816 static void
817 l2e_propagate_from_guest(struct vcpu *v,
818 guest_l2e_t *gl2e,
819 mfn_t gl2mfn,
820 mfn_t sl1mfn,
821 shadow_l2e_t *sl2e,
822 fetch_type_t ft)
823 {
824 _sh_propagate(v, gl2e, gl2mfn, sl1mfn, sl2e, 2, ft, 0);
825 }
827 static void
828 l1e_propagate_from_guest(struct vcpu *v,
829 guest_l1e_t *gl1e,
830 mfn_t gl1mfn,
831 mfn_t gmfn,
832 shadow_l1e_t *sl1e,
833 fetch_type_t ft,
834 int mmio)
835 {
836 _sh_propagate(v, gl1e, gl1mfn, gmfn, sl1e, 1, ft, mmio);
837 }
840 /**************************************************************************/
841 /* These functions update shadow entries (and do bookkeeping on the shadow
842 * tables they are in). It is intended that they are the only
843 * functions which ever write (non-zero) data onto a shadow page.
844 */
846 static inline void safe_write_entry(void *dst, void *src)
847 /* Copy one PTE safely when processors might be running on the
848 * destination pagetable. This does *not* give safety against
849 * concurrent writes (that's what the shadow lock is for), just
850 * stops the hardware picking up partially written entries. */
851 {
852 volatile unsigned long *d = dst;
853 unsigned long *s = src;
854 ASSERT(!((unsigned long) d & (sizeof (shadow_l1e_t) - 1)));
855 #if CONFIG_PAGING_LEVELS == 3
856 /* In PAE mode, pagetable entries are larger
857 * than machine words, so won't get written atomically. We need to make
858 * sure any other cpu running on these shadows doesn't see a
859 * half-written entry. Do this by marking the entry not-present first,
860 * then writing the high word before the low word. */
861 BUILD_BUG_ON(sizeof (shadow_l1e_t) != 2 * sizeof (unsigned long));
862 d[0] = 0;
863 d[1] = s[1];
864 d[0] = s[0];
865 #else
866 /* In 32-bit and 64-bit, sizeof(pte) == sizeof(ulong) == 1 word,
867 * which will be an atomic write, since the entry is aligned. */
868 BUILD_BUG_ON(sizeof (shadow_l1e_t) != sizeof (unsigned long));
869 *d = *s;
870 #endif
871 }
874 static inline void
875 shadow_write_entries(void *d, void *s, int entries, mfn_t mfn)
876 /* This function does the actual writes to shadow pages.
877 * It must not be called directly, since it doesn't do the bookkeeping
878 * that shadow_set_l*e() functions do. */
879 {
880 shadow_l1e_t *dst = d;
881 shadow_l1e_t *src = s;
882 void *map = NULL;
883 int i;
885 /* Because we mirror access rights at all levels in the shadow, an
886 * l2 (or higher) entry with the RW bit cleared will leave us with
887 * no write access through the linear map.
888 * We detect that by writing to the shadow with copy_to_user() and
889 * using map_domain_page() to get a writeable mapping if we need to. */
890 if ( __copy_to_user(d, d, sizeof (unsigned long)) != 0 )
891 {
892 perfc_incrc(shadow_linear_map_failed);
893 map = sh_map_domain_page(mfn);
894 ASSERT(map != NULL);
895 dst = map + ((unsigned long)dst & (PAGE_SIZE - 1));
896 }
899 for ( i = 0; i < entries; i++ )
900 safe_write_entry(dst++, src++);
902 if ( map != NULL ) sh_unmap_domain_page(map);
903 }
905 static inline int
906 perms_strictly_increased(u32 old_flags, u32 new_flags)
907 /* Given the flags of two entries, are the new flags a strict
908 * increase in rights over the old ones? */
909 {
910 u32 of = old_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX);
911 u32 nf = new_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX);
912 /* Flip the NX bit, since it's the only one that decreases rights;
913 * we calculate as if it were an "X" bit. */
914 of ^= _PAGE_NX_BIT;
915 nf ^= _PAGE_NX_BIT;
916 /* If the changed bits are all set in the new flags, then rights strictly
917 * increased between old and new. */
918 return ((of | (of ^ nf)) == nf);
919 }
921 static int inline
922 shadow_get_page_from_l1e(shadow_l1e_t sl1e, struct domain *d)
923 {
924 int res;
925 mfn_t mfn;
926 struct domain *owner;
928 ASSERT(!sh_l1e_is_magic(sl1e));
930 if ( !shadow_mode_refcounts(d) )
931 return 1;
933 res = get_page_from_l1e(sl1e, d);
935 // If a privileged domain is attempting to install a map of a page it does
936 // not own, we let it succeed anyway.
937 //
938 if ( unlikely(!res) &&
939 IS_PRIV(d) &&
940 !shadow_mode_translate(d) &&
941 mfn_valid(mfn = shadow_l1e_get_mfn(sl1e)) &&
942 (owner = page_get_owner(mfn_to_page(mfn))) &&
943 (d != owner) )
944 {
945 res = get_page_from_l1e(sl1e, owner);
946 SHADOW_PRINTK("privileged domain %d installs map of mfn %05lx "
947 "which is owned by domain %d: %s\n",
948 d->domain_id, mfn_x(mfn), owner->domain_id,
949 res ? "success" : "failed");
950 }
952 if ( unlikely(!res) )
953 {
954 perfc_incrc(shadow_get_page_fail);
955 SHADOW_PRINTK("failed: l1e=" SH_PRI_pte "\n");
956 }
958 return res;
959 }
961 static void inline
962 shadow_put_page_from_l1e(shadow_l1e_t sl1e, struct domain *d)
963 {
964 if ( !shadow_mode_refcounts(d) )
965 return;
967 put_page_from_l1e(sl1e, d);
968 }
970 #if GUEST_PAGING_LEVELS >= 4
971 static int shadow_set_l4e(struct vcpu *v,
972 shadow_l4e_t *sl4e,
973 shadow_l4e_t new_sl4e,
974 mfn_t sl4mfn)
975 {
976 int flags = 0, ok;
977 shadow_l4e_t old_sl4e;
978 paddr_t paddr;
979 ASSERT(sl4e != NULL);
980 old_sl4e = *sl4e;
982 if ( old_sl4e.l4 == new_sl4e.l4 ) return 0; /* Nothing to do */
984 paddr = ((((paddr_t)mfn_x(sl4mfn)) << PAGE_SHIFT)
985 | (((unsigned long)sl4e) & ~PAGE_MASK));
987 if ( shadow_l4e_get_flags(new_sl4e) & _PAGE_PRESENT )
988 {
989 /* About to install a new reference */
990 mfn_t sl3mfn = shadow_l4e_get_mfn(new_sl4e);
991 ok = sh_get_ref(v, sl3mfn, paddr);
992 /* Are we pinning l3 shadows to handle wierd linux behaviour? */
993 if ( sh_type_is_pinnable(v, SH_type_l3_64_shadow) )
994 ok |= sh_pin(v, sl3mfn);
995 if ( !ok )
996 {
997 domain_crash(v->domain);
998 return SHADOW_SET_ERROR;
999 }
1002 /* Write the new entry */
1003 shadow_write_entries(sl4e, &new_sl4e, 1, sl4mfn);
1004 flags |= SHADOW_SET_CHANGED;
1006 if ( shadow_l4e_get_flags(old_sl4e) & _PAGE_PRESENT )
1008 /* We lost a reference to an old mfn. */
1009 mfn_t osl3mfn = shadow_l4e_get_mfn(old_sl4e);
1010 if ( (mfn_x(osl3mfn) != mfn_x(shadow_l4e_get_mfn(new_sl4e)))
1011 || !perms_strictly_increased(shadow_l4e_get_flags(old_sl4e),
1012 shadow_l4e_get_flags(new_sl4e)) )
1014 flags |= SHADOW_SET_FLUSH;
1016 sh_put_ref(v, osl3mfn, paddr);
1018 return flags;
1021 static int shadow_set_l3e(struct vcpu *v,
1022 shadow_l3e_t *sl3e,
1023 shadow_l3e_t new_sl3e,
1024 mfn_t sl3mfn)
1026 int flags = 0;
1027 shadow_l3e_t old_sl3e;
1028 paddr_t paddr;
1029 ASSERT(sl3e != NULL);
1030 old_sl3e = *sl3e;
1032 if ( old_sl3e.l3 == new_sl3e.l3 ) return 0; /* Nothing to do */
1034 paddr = ((((paddr_t)mfn_x(sl3mfn)) << PAGE_SHIFT)
1035 | (((unsigned long)sl3e) & ~PAGE_MASK));
1037 if ( shadow_l3e_get_flags(new_sl3e) & _PAGE_PRESENT )
1038 /* About to install a new reference */
1039 if ( !sh_get_ref(v, shadow_l3e_get_mfn(new_sl3e), paddr) )
1041 domain_crash(v->domain);
1042 return SHADOW_SET_ERROR;
1045 /* Write the new entry */
1046 shadow_write_entries(sl3e, &new_sl3e, 1, sl3mfn);
1047 flags |= SHADOW_SET_CHANGED;
1049 if ( shadow_l3e_get_flags(old_sl3e) & _PAGE_PRESENT )
1051 /* We lost a reference to an old mfn. */
1052 mfn_t osl2mfn = shadow_l3e_get_mfn(old_sl3e);
1053 if ( (mfn_x(osl2mfn) != mfn_x(shadow_l3e_get_mfn(new_sl3e))) ||
1054 !perms_strictly_increased(shadow_l3e_get_flags(old_sl3e),
1055 shadow_l3e_get_flags(new_sl3e)) )
1057 flags |= SHADOW_SET_FLUSH;
1059 sh_put_ref(v, osl2mfn, paddr);
1061 return flags;
1063 #endif /* GUEST_PAGING_LEVELS >= 4 */
1065 static int shadow_set_l2e(struct vcpu *v,
1066 shadow_l2e_t *sl2e,
1067 shadow_l2e_t new_sl2e,
1068 mfn_t sl2mfn)
1070 int flags = 0;
1071 shadow_l2e_t old_sl2e;
1072 paddr_t paddr;
1074 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1075 /* In 2-on-3 we work with pairs of l2es pointing at two-page
1076 * shadows. Reference counting and up-pointers track from the first
1077 * page of the shadow to the first l2e, so make sure that we're
1078 * working with those:
1079 * Align the pointer down so it's pointing at the first of the pair */
1080 sl2e = (shadow_l2e_t *)((unsigned long)sl2e & ~(sizeof(shadow_l2e_t)));
1081 /* Align the mfn of the shadow entry too */
1082 new_sl2e.l2 &= ~(1<<PAGE_SHIFT);
1083 #endif
1085 ASSERT(sl2e != NULL);
1086 old_sl2e = *sl2e;
1088 if ( old_sl2e.l2 == new_sl2e.l2 ) return 0; /* Nothing to do */
1090 paddr = ((((paddr_t)mfn_x(sl2mfn)) << PAGE_SHIFT)
1091 | (((unsigned long)sl2e) & ~PAGE_MASK));
1093 if ( shadow_l2e_get_flags(new_sl2e) & _PAGE_PRESENT )
1094 /* About to install a new reference */
1095 if ( !sh_get_ref(v, shadow_l2e_get_mfn(new_sl2e), paddr) )
1097 domain_crash(v->domain);
1098 return SHADOW_SET_ERROR;
1101 /* Write the new entry */
1102 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1104 shadow_l2e_t pair[2] = { new_sl2e, new_sl2e };
1105 /* The l1 shadow is two pages long and need to be pointed to by
1106 * two adjacent l1es. The pair have the same flags, but point
1107 * at odd and even MFNs */
1108 ASSERT(!(pair[0].l2 & (1<<PAGE_SHIFT)));
1109 pair[1].l2 |= (1<<PAGE_SHIFT);
1110 shadow_write_entries(sl2e, &pair, 2, sl2mfn);
1112 #else /* normal case */
1113 shadow_write_entries(sl2e, &new_sl2e, 1, sl2mfn);
1114 #endif
1115 flags |= SHADOW_SET_CHANGED;
1117 if ( shadow_l2e_get_flags(old_sl2e) & _PAGE_PRESENT )
1119 /* We lost a reference to an old mfn. */
1120 mfn_t osl1mfn = shadow_l2e_get_mfn(old_sl2e);
1121 if ( (mfn_x(osl1mfn) != mfn_x(shadow_l2e_get_mfn(new_sl2e))) ||
1122 !perms_strictly_increased(shadow_l2e_get_flags(old_sl2e),
1123 shadow_l2e_get_flags(new_sl2e)) )
1125 flags |= SHADOW_SET_FLUSH;
1127 sh_put_ref(v, osl1mfn, paddr);
1129 return flags;
1132 static int shadow_set_l1e(struct vcpu *v,
1133 shadow_l1e_t *sl1e,
1134 shadow_l1e_t new_sl1e,
1135 mfn_t sl1mfn)
1137 int flags = 0;
1138 struct domain *d = v->domain;
1139 shadow_l1e_t old_sl1e;
1140 ASSERT(sl1e != NULL);
1142 old_sl1e = *sl1e;
1144 if ( old_sl1e.l1 == new_sl1e.l1 ) return 0; /* Nothing to do */
1146 if ( (shadow_l1e_get_flags(new_sl1e) & _PAGE_PRESENT)
1147 && !sh_l1e_is_magic(new_sl1e) )
1149 /* About to install a new reference */
1150 if ( shadow_mode_refcounts(d) ) {
1151 if ( shadow_get_page_from_l1e(new_sl1e, d) == 0 )
1153 /* Doesn't look like a pagetable. */
1154 flags |= SHADOW_SET_ERROR;
1155 new_sl1e = shadow_l1e_empty();
1160 /* Write the new entry */
1161 shadow_write_entries(sl1e, &new_sl1e, 1, sl1mfn);
1162 flags |= SHADOW_SET_CHANGED;
1164 if ( (shadow_l1e_get_flags(old_sl1e) & _PAGE_PRESENT)
1165 && !sh_l1e_is_magic(old_sl1e) )
1167 /* We lost a reference to an old mfn. */
1168 /* N.B. Unlike higher-level sets, never need an extra flush
1169 * when writing an l1e. Because it points to the same guest frame
1170 * as the guest l1e did, it's the guest's responsibility to
1171 * trigger a flush later. */
1172 if ( shadow_mode_refcounts(d) )
1174 shadow_put_page_from_l1e(old_sl1e, d);
1177 return flags;
1181 /**************************************************************************/
1182 /* Macros to walk pagetables. These take the shadow of a pagetable and
1183 * walk every "interesting" entry. That is, they don't touch Xen mappings,
1184 * and for 32-bit l2s shadowed onto PAE or 64-bit, they only touch every
1185 * second entry (since pairs of entries are managed together). For multi-page
1186 * shadows they walk all pages.
1188 * Arguments are an MFN, the variable to point to each entry, a variable
1189 * to indicate that we are done (we will shortcut to the end of the scan
1190 * when _done != 0), a variable to indicate that we should avoid Xen mappings,
1191 * and the code.
1193 * WARNING: These macros have side-effects. They change the values of both
1194 * the pointer and the MFN. */
1196 static inline void increment_ptr_to_guest_entry(void *ptr)
1198 if ( ptr )
1200 guest_l1e_t **entry = ptr;
1201 (*entry)++;
1205 /* All kinds of l1: touch all entries */
1206 #define _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1207 do { \
1208 int _i; \
1209 shadow_l1e_t *_sp = map_shadow_page((_sl1mfn)); \
1210 ASSERT(mfn_to_shadow_page(_sl1mfn)->type == SH_type_l1_shadow \
1211 || mfn_to_shadow_page(_sl1mfn)->type == SH_type_fl1_shadow); \
1212 for ( _i = 0; _i < SHADOW_L1_PAGETABLE_ENTRIES; _i++ ) \
1213 { \
1214 (_sl1e) = _sp + _i; \
1215 if ( shadow_l1e_get_flags(*(_sl1e)) & _PAGE_PRESENT ) \
1216 {_code} \
1217 if ( _done ) break; \
1218 increment_ptr_to_guest_entry(_gl1p); \
1219 } \
1220 unmap_shadow_page(_sp); \
1221 } while (0)
1223 /* 32-bit l1, on PAE or 64-bit shadows: need to walk both pages of shadow */
1224 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1225 #define SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1226 do { \
1227 int __done = 0; \
1228 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, \
1229 ({ (__done = _done); }), _code); \
1230 _sl1mfn = _mfn(mfn_x(_sl1mfn) + 1); \
1231 if ( !__done ) \
1232 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, \
1233 ({ (__done = _done); }), _code); \
1234 } while (0)
1235 #else /* Everything else; l1 shadows are only one page */
1236 #define SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1237 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code)
1238 #endif
1241 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1243 /* 32-bit l2 on PAE/64: four pages, touch every second entry, and avoid Xen */
1244 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \
1245 do { \
1246 int _i, _j, __done = 0; \
1247 int _xen = !shadow_mode_external(_dom); \
1248 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_32_shadow); \
1249 for ( _j = 0; _j < 4 && !__done; _j++ ) \
1250 { \
1251 shadow_l2e_t *_sp = map_shadow_page(_sl2mfn); \
1252 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i += 2 ) \
1253 if ( (!(_xen)) \
1254 || ((_j * SHADOW_L2_PAGETABLE_ENTRIES) + _i) \
1255 < (HYPERVISOR_VIRT_START >> SHADOW_L2_PAGETABLE_SHIFT) ) \
1256 { \
1257 (_sl2e) = _sp + _i; \
1258 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1259 {_code} \
1260 if ( (__done = (_done)) ) break; \
1261 increment_ptr_to_guest_entry(_gl2p); \
1262 } \
1263 unmap_shadow_page(_sp); \
1264 _sl2mfn = _mfn(mfn_x(_sl2mfn) + 1); \
1265 } \
1266 } while (0)
1268 #elif GUEST_PAGING_LEVELS == 2
1270 /* 32-bit on 32-bit: avoid Xen entries */
1271 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \
1272 do { \
1273 int _i; \
1274 int _xen = !shadow_mode_external(_dom); \
1275 shadow_l2e_t *_sp = map_shadow_page((_sl2mfn)); \
1276 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_32_shadow); \
1277 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \
1278 if ( (!(_xen)) \
1279 || \
1280 (_i < (HYPERVISOR_VIRT_START >> SHADOW_L2_PAGETABLE_SHIFT)) ) \
1281 { \
1282 (_sl2e) = _sp + _i; \
1283 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1284 {_code} \
1285 if ( _done ) break; \
1286 increment_ptr_to_guest_entry(_gl2p); \
1287 } \
1288 unmap_shadow_page(_sp); \
1289 } while (0)
1291 #elif GUEST_PAGING_LEVELS == 3
1293 /* PAE: if it's an l2h, don't touch Xen mappings */
1294 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \
1295 do { \
1296 int _i; \
1297 int _xen = !shadow_mode_external(_dom); \
1298 shadow_l2e_t *_sp = map_shadow_page((_sl2mfn)); \
1299 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_pae_shadow \
1300 || mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2h_pae_shadow);\
1301 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \
1302 if ( (!(_xen)) \
1303 || mfn_to_shadow_page(_sl2mfn)->type != SH_type_l2h_pae_shadow\
1304 || ((_i + (3 * SHADOW_L2_PAGETABLE_ENTRIES)) \
1305 < (HYPERVISOR_VIRT_START >> SHADOW_L2_PAGETABLE_SHIFT)) ) \
1306 { \
1307 (_sl2e) = _sp + _i; \
1308 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1309 {_code} \
1310 if ( _done ) break; \
1311 increment_ptr_to_guest_entry(_gl2p); \
1312 } \
1313 unmap_shadow_page(_sp); \
1314 } while (0)
1316 #else
1318 /* 64-bit l2: touch all entries except for PAE compat guests. */
1319 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \
1320 do { \
1321 int _i; \
1322 int _xen = !shadow_mode_external(_dom); \
1323 shadow_l2e_t *_sp = map_shadow_page((_sl2mfn)); \
1324 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_64_shadow || \
1325 mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2h_64_shadow); \
1326 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \
1327 { \
1328 if ( (!(_xen)) \
1329 || !IS_COMPAT(_dom) \
1330 || mfn_to_shadow_page(_sl2mfn)->type != SH_type_l2h_64_shadow \
1331 || (_i < COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(_dom)) ) \
1332 { \
1333 (_sl2e) = _sp + _i; \
1334 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1335 {_code} \
1336 if ( _done ) break; \
1337 increment_ptr_to_guest_entry(_gl2p); \
1338 } \
1339 } \
1340 unmap_shadow_page(_sp); \
1341 } while (0)
1343 #endif /* different kinds of l2 */
1345 #if GUEST_PAGING_LEVELS == 4
1347 /* 64-bit l3: touch all entries */
1348 #define SHADOW_FOREACH_L3E(_sl3mfn, _sl3e, _gl3p, _done, _code) \
1349 do { \
1350 int _i; \
1351 shadow_l3e_t *_sp = map_shadow_page((_sl3mfn)); \
1352 ASSERT(mfn_to_shadow_page(_sl3mfn)->type == SH_type_l3_64_shadow); \
1353 for ( _i = 0; _i < SHADOW_L3_PAGETABLE_ENTRIES; _i++ ) \
1354 { \
1355 (_sl3e) = _sp + _i; \
1356 if ( shadow_l3e_get_flags(*(_sl3e)) & _PAGE_PRESENT ) \
1357 {_code} \
1358 if ( _done ) break; \
1359 increment_ptr_to_guest_entry(_gl3p); \
1360 } \
1361 unmap_shadow_page(_sp); \
1362 } while (0)
1364 /* 64-bit l4: avoid Xen mappings */
1365 #define SHADOW_FOREACH_L4E(_sl4mfn, _sl4e, _gl4p, _done, _dom, _code) \
1366 do { \
1367 shadow_l4e_t *_sp = map_shadow_page((_sl4mfn)); \
1368 int _xen = !shadow_mode_external(_dom); \
1369 int _i; \
1370 ASSERT(mfn_to_shadow_page(_sl4mfn)->type == SH_type_l4_64_shadow); \
1371 for ( _i = 0; _i < SHADOW_L4_PAGETABLE_ENTRIES; _i++ ) \
1372 { \
1373 if ( (!(_xen)) || is_guest_l4_slot(_dom, _i) ) \
1374 { \
1375 (_sl4e) = _sp + _i; \
1376 if ( shadow_l4e_get_flags(*(_sl4e)) & _PAGE_PRESENT ) \
1377 {_code} \
1378 if ( _done ) break; \
1379 } \
1380 increment_ptr_to_guest_entry(_gl4p); \
1381 } \
1382 unmap_shadow_page(_sp); \
1383 } while (0)
1385 #endif
1389 /**************************************************************************/
1390 /* Functions to install Xen mappings and linear mappings in shadow pages */
1392 // XXX -- this function should probably be moved to shadow-common.c, but that
1393 // probably wants to wait until the shadow types have been moved from
1394 // shadow-types.h to shadow-private.h
1395 //
1396 #if CONFIG_PAGING_LEVELS == 4 && GUEST_PAGING_LEVELS == 4
1397 void sh_install_xen_entries_in_l4(struct vcpu *v, mfn_t gl4mfn, mfn_t sl4mfn)
1399 struct domain *d = v->domain;
1400 shadow_l4e_t *sl4e;
1402 sl4e = sh_map_domain_page(sl4mfn);
1403 ASSERT(sl4e != NULL);
1404 ASSERT(sizeof (l4_pgentry_t) == sizeof (shadow_l4e_t));
1406 /* Copy the common Xen mappings from the idle domain */
1407 memcpy(&sl4e[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1408 &idle_pg_table[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1409 ROOT_PAGETABLE_XEN_SLOTS * sizeof(l4_pgentry_t));
1411 /* Install the per-domain mappings for this domain */
1412 sl4e[shadow_l4_table_offset(PERDOMAIN_VIRT_START)] =
1413 shadow_l4e_from_mfn(page_to_mfn(virt_to_page(d->arch.mm_perdomain_l3)),
1414 __PAGE_HYPERVISOR);
1416 /* Linear mapping */
1417 sl4e[shadow_l4_table_offset(SH_LINEAR_PT_VIRT_START)] =
1418 shadow_l4e_from_mfn(sl4mfn, __PAGE_HYPERVISOR);
1420 if ( shadow_mode_translate(v->domain) && !shadow_mode_external(v->domain) )
1422 // linear tables may not be used with translated PV guests
1423 sl4e[shadow_l4_table_offset(LINEAR_PT_VIRT_START)] =
1424 shadow_l4e_empty();
1426 else
1428 sl4e[shadow_l4_table_offset(LINEAR_PT_VIRT_START)] =
1429 shadow_l4e_from_mfn(gl4mfn, __PAGE_HYPERVISOR);
1432 if ( shadow_mode_translate(v->domain) )
1434 /* install domain-specific P2M table */
1435 sl4e[shadow_l4_table_offset(RO_MPT_VIRT_START)] =
1436 shadow_l4e_from_mfn(pagetable_get_mfn(d->arch.phys_table),
1437 __PAGE_HYPERVISOR);
1440 if ( IS_COMPAT(v->domain) )
1442 /* install compat arg xlat entry */
1443 sl4e[shadow_l4_table_offset(COMPAT_ARG_XLAT_VIRT_BASE)] =
1444 shadow_l4e_from_mfn(
1445 page_to_mfn(virt_to_page(d->arch.mm_arg_xlat_l3)),
1446 __PAGE_HYPERVISOR);
1449 sh_unmap_domain_page(sl4e);
1451 #endif
1453 #if CONFIG_PAGING_LEVELS >= 3 && GUEST_PAGING_LEVELS >= 3
1454 // For 3-on-3 PV guests, we need to make sure the xen mappings are in
1455 // place, which means that we need to populate the l2h entry in the l3
1456 // table.
1458 static void sh_install_xen_entries_in_l2h(struct vcpu *v, mfn_t sl2hmfn)
1460 struct domain *d = v->domain;
1461 shadow_l2e_t *sl2e;
1462 #if CONFIG_PAGING_LEVELS == 3
1463 int i;
1464 #else
1466 if ( !pv_32bit_guest(v) )
1467 return;
1468 #endif
1470 sl2e = sh_map_domain_page(sl2hmfn);
1471 ASSERT(sl2e != NULL);
1472 ASSERT(sizeof (l2_pgentry_t) == sizeof (shadow_l2e_t));
1474 #if CONFIG_PAGING_LEVELS == 3
1476 /* Copy the common Xen mappings from the idle domain */
1477 memcpy(&sl2e[L2_PAGETABLE_FIRST_XEN_SLOT & (L2_PAGETABLE_ENTRIES-1)],
1478 &idle_pg_table_l2[L2_PAGETABLE_FIRST_XEN_SLOT],
1479 L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t));
1481 /* Install the per-domain mappings for this domain */
1482 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
1483 sl2e[shadow_l2_table_offset(PERDOMAIN_VIRT_START) + i] =
1484 shadow_l2e_from_mfn(
1485 page_to_mfn(virt_to_page(d->arch.mm_perdomain_pt) + i),
1486 __PAGE_HYPERVISOR);
1488 /* We don't set up a linear mapping here because we can't until this
1489 * l2h is installed in an l3e. sh_update_linear_entries() handles
1490 * the linear mappings when CR3 (and so the fourth l3e) is loaded.
1491 * We zero them here, just as a safety measure.
1492 */
1493 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
1494 sl2e[shadow_l2_table_offset(LINEAR_PT_VIRT_START) + i] =
1495 shadow_l2e_empty();
1496 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
1497 sl2e[shadow_l2_table_offset(SH_LINEAR_PT_VIRT_START) + i] =
1498 shadow_l2e_empty();
1500 if ( shadow_mode_translate(d) )
1502 /* Install the domain-specific p2m table */
1503 l3_pgentry_t *p2m;
1504 ASSERT(pagetable_get_pfn(d->arch.phys_table) != 0);
1505 p2m = sh_map_domain_page(pagetable_get_mfn(d->arch.phys_table));
1506 for ( i = 0; i < MACHPHYS_MBYTES>>1; i++ )
1508 sl2e[shadow_l2_table_offset(RO_MPT_VIRT_START) + i] =
1509 (l3e_get_flags(p2m[i]) & _PAGE_PRESENT)
1510 ? shadow_l2e_from_mfn(_mfn(l3e_get_pfn(p2m[i])),
1511 __PAGE_HYPERVISOR)
1512 : shadow_l2e_empty();
1514 sh_unmap_domain_page(p2m);
1517 #else
1519 /* Copy the common Xen mappings from the idle domain */
1520 memcpy(
1521 &sl2e[COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(d)],
1522 &compat_idle_pg_table_l2[l2_table_offset(HIRO_COMPAT_MPT_VIRT_START)],
1523 COMPAT_L2_PAGETABLE_XEN_SLOTS(d) * sizeof(*sl2e));
1525 #endif
1527 sh_unmap_domain_page(sl2e);
1529 #endif
1532 #if CONFIG_PAGING_LEVELS == 2 && GUEST_PAGING_LEVELS == 2
1533 void sh_install_xen_entries_in_l2(struct vcpu *v, mfn_t gl2mfn, mfn_t sl2mfn)
1535 struct domain *d = v->domain;
1536 shadow_l2e_t *sl2e;
1537 int i;
1539 sl2e = sh_map_domain_page(sl2mfn);
1540 ASSERT(sl2e != NULL);
1541 ASSERT(sizeof (l2_pgentry_t) == sizeof (shadow_l2e_t));
1543 /* Copy the common Xen mappings from the idle domain */
1544 memcpy(&sl2e[L2_PAGETABLE_FIRST_XEN_SLOT],
1545 &idle_pg_table[L2_PAGETABLE_FIRST_XEN_SLOT],
1546 L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t));
1548 /* Install the per-domain mappings for this domain */
1549 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
1550 sl2e[shadow_l2_table_offset(PERDOMAIN_VIRT_START) + i] =
1551 shadow_l2e_from_mfn(
1552 page_to_mfn(virt_to_page(d->arch.mm_perdomain_pt) + i),
1553 __PAGE_HYPERVISOR);
1555 /* Linear mapping */
1556 sl2e[shadow_l2_table_offset(SH_LINEAR_PT_VIRT_START)] =
1557 shadow_l2e_from_mfn(sl2mfn, __PAGE_HYPERVISOR);
1559 if ( shadow_mode_translate(v->domain) && !shadow_mode_external(v->domain) )
1561 // linear tables may not be used with translated PV guests
1562 sl2e[shadow_l2_table_offset(LINEAR_PT_VIRT_START)] =
1563 shadow_l2e_empty();
1565 else
1567 sl2e[shadow_l2_table_offset(LINEAR_PT_VIRT_START)] =
1568 shadow_l2e_from_mfn(gl2mfn, __PAGE_HYPERVISOR);
1571 if ( shadow_mode_translate(d) )
1573 /* install domain-specific P2M table */
1574 sl2e[shadow_l2_table_offset(RO_MPT_VIRT_START)] =
1575 shadow_l2e_from_mfn(pagetable_get_mfn(d->arch.phys_table),
1576 __PAGE_HYPERVISOR);
1579 sh_unmap_domain_page(sl2e);
1581 #endif
1585 /**************************************************************************/
1586 /* Create a shadow of a given guest page.
1587 */
1588 static mfn_t
1589 sh_make_shadow(struct vcpu *v, mfn_t gmfn, u32 shadow_type)
1591 mfn_t smfn = shadow_alloc(v->domain, shadow_type, mfn_x(gmfn));
1592 SHADOW_DEBUG(MAKE_SHADOW, "(%05lx, %u)=>%05lx\n",
1593 mfn_x(gmfn), shadow_type, mfn_x(smfn));
1595 if ( shadow_type != SH_type_l2_32_shadow
1596 && shadow_type != SH_type_l2_pae_shadow
1597 && shadow_type != SH_type_l2h_pae_shadow
1598 && shadow_type != SH_type_l4_64_shadow )
1599 /* Lower-level shadow, not yet linked form a higher level */
1600 mfn_to_shadow_page(smfn)->up = 0;
1602 #if GUEST_PAGING_LEVELS == 4
1603 #if (SHADOW_OPTIMIZATIONS & SHOPT_LINUX_L3_TOPLEVEL)
1604 if ( shadow_type == SH_type_l4_64_shadow &&
1605 unlikely(v->domain->arch.paging.shadow.opt_flags & SHOPT_LINUX_L3_TOPLEVEL) )
1607 /* We're shadowing a new l4, but we've been assuming the guest uses
1608 * only one l4 per vcpu and context switches using an l4 entry.
1609 * Count the number of active l4 shadows. If there are enough
1610 * of them, decide that this isn't an old linux guest, and stop
1611 * pinning l3es. This is not very quick but it doesn't happen
1612 * very often. */
1613 struct list_head *l, *t;
1614 struct shadow_page_info *sp;
1615 struct vcpu *v2;
1616 int l4count = 0, vcpus = 0;
1617 list_for_each(l, &v->domain->arch.paging.shadow.pinned_shadows)
1619 sp = list_entry(l, struct shadow_page_info, list);
1620 if ( sp->type == SH_type_l4_64_shadow )
1621 l4count++;
1623 for_each_vcpu ( v->domain, v2 )
1624 vcpus++;
1625 if ( l4count > 2 * vcpus )
1627 /* Unpin all the pinned l3 tables, and don't pin any more. */
1628 list_for_each_safe(l, t, &v->domain->arch.paging.shadow.pinned_shadows)
1630 sp = list_entry(l, struct shadow_page_info, list);
1631 if ( sp->type == SH_type_l3_64_shadow )
1632 sh_unpin(v, shadow_page_to_mfn(sp));
1634 v->domain->arch.paging.shadow.opt_flags &= ~SHOPT_LINUX_L3_TOPLEVEL;
1637 #endif
1638 #endif
1640 // Create the Xen mappings...
1641 if ( !shadow_mode_external(v->domain) )
1643 switch (shadow_type)
1645 #if CONFIG_PAGING_LEVELS == 4 && GUEST_PAGING_LEVELS == 4
1646 case SH_type_l4_shadow:
1647 sh_install_xen_entries_in_l4(v, gmfn, smfn); break;
1648 #endif
1649 #if CONFIG_PAGING_LEVELS >= 3 && GUEST_PAGING_LEVELS >= 3
1650 case SH_type_l2h_shadow:
1651 #ifdef CONFIG_COMPAT
1652 ASSERT( IS_COMPAT(v->domain) );
1653 #endif
1654 sh_install_xen_entries_in_l2h(v, smfn); break;
1655 #endif
1656 #if CONFIG_PAGING_LEVELS == 2 && GUEST_PAGING_LEVELS == 2
1657 case SH_type_l2_shadow:
1658 sh_install_xen_entries_in_l2(v, gmfn, smfn); break;
1659 #endif
1660 default: /* Do nothing */ break;
1664 shadow_promote(v, gmfn, shadow_type);
1665 set_shadow_status(v, gmfn, shadow_type, smfn);
1667 return smfn;
1670 /* Make a splintered superpage shadow */
1671 static mfn_t
1672 make_fl1_shadow(struct vcpu *v, gfn_t gfn)
1674 mfn_t smfn = shadow_alloc(v->domain, SH_type_fl1_shadow,
1675 (unsigned long) gfn_x(gfn));
1677 SHADOW_DEBUG(MAKE_SHADOW, "(%" SH_PRI_gfn ")=>%" PRI_mfn "\n",
1678 gfn_x(gfn), mfn_x(smfn));
1680 set_fl1_shadow_status(v, gfn, smfn);
1681 return smfn;
1685 #if SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS
1686 mfn_t
1687 sh_make_monitor_table(struct vcpu *v)
1689 struct domain *d = v->domain;
1691 ASSERT(pagetable_get_pfn(v->arch.monitor_table) == 0);
1693 /* Guarantee we can get the memory we need */
1694 shadow_prealloc(d, SHADOW_MAX_ORDER);
1696 #if CONFIG_PAGING_LEVELS == 4
1698 mfn_t m4mfn;
1699 m4mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1700 sh_install_xen_entries_in_l4(v, m4mfn, m4mfn);
1701 /* Remember the level of this table */
1702 mfn_to_page(m4mfn)->shadow_flags = 4;
1703 #if SHADOW_PAGING_LEVELS < 4
1704 // Install a monitor l3 table in slot 0 of the l4 table.
1705 // This is used for shadow linear maps.
1707 mfn_t m3mfn;
1708 l4_pgentry_t *l4e;
1709 m3mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1710 mfn_to_page(m3mfn)->shadow_flags = 3;
1711 l4e = sh_map_domain_page(m4mfn);
1712 l4e[0] = l4e_from_pfn(mfn_x(m3mfn), __PAGE_HYPERVISOR);
1713 sh_unmap_domain_page(l4e);
1714 if ( pv_32bit_guest(v) )
1716 // Install a monitor l2 table in slot 3 of the l3 table.
1717 // This is used for all Xen entries.
1718 mfn_t m2mfn;
1719 l3_pgentry_t *l3e;
1720 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1721 mfn_to_page(m2mfn)->shadow_flags = 2;
1722 l3e = sh_map_domain_page(m3mfn);
1723 l3e[3] = l3e_from_pfn(mfn_x(m2mfn), _PAGE_PRESENT);
1724 sh_install_xen_entries_in_l2h(v, m2mfn);
1725 sh_unmap_domain_page(l3e);
1728 #endif /* SHADOW_PAGING_LEVELS < 4 */
1729 return m4mfn;
1732 #elif CONFIG_PAGING_LEVELS == 3
1735 mfn_t m3mfn, m2mfn;
1736 l3_pgentry_t *l3e;
1737 l2_pgentry_t *l2e;
1738 int i;
1740 m3mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1741 /* Remember the level of this table */
1742 mfn_to_page(m3mfn)->shadow_flags = 3;
1744 // Install a monitor l2 table in slot 3 of the l3 table.
1745 // This is used for all Xen entries, including linear maps
1746 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1747 mfn_to_page(m2mfn)->shadow_flags = 2;
1748 l3e = sh_map_domain_page(m3mfn);
1749 l3e[3] = l3e_from_pfn(mfn_x(m2mfn), _PAGE_PRESENT);
1750 sh_install_xen_entries_in_l2h(v, m2mfn);
1751 /* Install the monitor's own linear map */
1752 l2e = sh_map_domain_page(m2mfn);
1753 for ( i = 0; i < L3_PAGETABLE_ENTRIES; i++ )
1754 l2e[l2_table_offset(LINEAR_PT_VIRT_START) + i] =
1755 (l3e_get_flags(l3e[i]) & _PAGE_PRESENT)
1756 ? l2e_from_pfn(l3e_get_pfn(l3e[i]), __PAGE_HYPERVISOR)
1757 : l2e_empty();
1758 sh_unmap_domain_page(l2e);
1759 sh_unmap_domain_page(l3e);
1761 SHADOW_PRINTK("new monitor table: %#lx\n", mfn_x(m3mfn));
1762 return m3mfn;
1765 #elif CONFIG_PAGING_LEVELS == 2
1768 mfn_t m2mfn;
1769 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1770 sh_install_xen_entries_in_l2(v, m2mfn, m2mfn);
1771 /* Remember the level of this table */
1772 mfn_to_page(m2mfn)->shadow_flags = 2;
1773 return m2mfn;
1776 #else
1777 #error this should not happen
1778 #endif /* CONFIG_PAGING_LEVELS */
1780 #endif /* SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS */
1782 /**************************************************************************/
1783 /* These functions also take a virtual address and return the level-N
1784 * shadow table mfn and entry, but they create the shadow pagetables if
1785 * they are needed. The "demand" argument is non-zero when handling
1786 * a demand fault (so we know what to do about accessed bits &c).
1787 * If the necessary tables are not present in the guest, they return NULL. */
1789 /* N.B. The use of GUEST_PAGING_LEVELS here is correct. If the shadow has
1790 * more levels than the guest, the upper levels are always fixed and do not
1791 * reflect any information from the guest, so we do not use these functions
1792 * to access them. */
1794 #if GUEST_PAGING_LEVELS >= 4
1795 static shadow_l4e_t * shadow_get_and_create_l4e(struct vcpu *v,
1796 walk_t *gw,
1797 mfn_t *sl4mfn)
1799 /* There is always a shadow of the top level table. Get it. */
1800 *sl4mfn = pagetable_get_mfn(v->arch.shadow_table[0]);
1801 /* Reading the top level table is always valid. */
1802 return sh_linear_l4_table(v) + shadow_l4_linear_offset(gw->va);
1805 static shadow_l3e_t * shadow_get_and_create_l3e(struct vcpu *v,
1806 walk_t *gw,
1807 mfn_t *sl3mfn,
1808 fetch_type_t ft)
1810 mfn_t sl4mfn;
1811 shadow_l4e_t *sl4e;
1812 if ( !mfn_valid(gw->l3mfn) ) return NULL; /* No guest page. */
1813 /* Get the l4e */
1814 sl4e = shadow_get_and_create_l4e(v, gw, &sl4mfn);
1815 ASSERT(sl4e != NULL);
1816 if ( shadow_l4e_get_flags(*sl4e) & _PAGE_PRESENT )
1818 *sl3mfn = shadow_l4e_get_mfn(*sl4e);
1819 ASSERT(mfn_valid(*sl3mfn));
1821 else
1823 int r;
1824 shadow_l4e_t new_sl4e;
1825 /* No l3 shadow installed: find and install it. */
1826 *sl3mfn = get_shadow_status(v, gw->l3mfn, SH_type_l3_shadow);
1827 if ( !mfn_valid(*sl3mfn) )
1829 /* No l3 shadow of this page exists at all: make one. */
1830 *sl3mfn = sh_make_shadow(v, gw->l3mfn, SH_type_l3_shadow);
1832 /* Install the new sl3 table in the sl4e */
1833 l4e_propagate_from_guest(v, gw->l4e, gw->l4mfn,
1834 *sl3mfn, &new_sl4e, ft);
1835 r = shadow_set_l4e(v, sl4e, new_sl4e, sl4mfn);
1836 ASSERT((r & SHADOW_SET_FLUSH) == 0);
1837 if ( r & SHADOW_SET_ERROR )
1838 return NULL;
1840 /* Now follow it down a level. Guaranteed to succeed. */
1841 return sh_linear_l3_table(v) + shadow_l3_linear_offset(gw->va);
1843 #endif /* GUEST_PAGING_LEVELS >= 4 */
1846 static shadow_l2e_t * shadow_get_and_create_l2e(struct vcpu *v,
1847 walk_t *gw,
1848 mfn_t *sl2mfn,
1849 fetch_type_t ft)
1851 #if GUEST_PAGING_LEVELS >= 4 /* 64bit... */
1852 mfn_t sl3mfn = _mfn(INVALID_MFN);
1853 shadow_l3e_t *sl3e;
1854 if ( !mfn_valid(gw->l2mfn) ) return NULL; /* No guest page. */
1855 /* Get the l3e */
1856 sl3e = shadow_get_and_create_l3e(v, gw, &sl3mfn, ft);
1857 if ( sl3e == NULL ) return NULL;
1858 if ( shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT )
1860 *sl2mfn = shadow_l3e_get_mfn(*sl3e);
1861 ASSERT(mfn_valid(*sl2mfn));
1863 else
1865 int r;
1866 shadow_l3e_t new_sl3e;
1867 unsigned int t = SH_type_l2_shadow;
1869 #ifdef CONFIG_COMPAT
1870 /* Tag compat L2 containing hypervisor (m2p) mappings */
1871 if ( IS_COMPAT(v->domain) &&
1872 guest_l4_table_offset(gw->va) == 0 &&
1873 guest_l3_table_offset(gw->va) == 3 )
1874 t = SH_type_l2h_shadow;
1875 #endif
1876 /* No l2 shadow installed: find and install it. */
1877 *sl2mfn = get_shadow_status(v, gw->l2mfn, t);
1878 if ( !mfn_valid(*sl2mfn) )
1880 /* No l2 shadow of this page exists at all: make one. */
1881 *sl2mfn = sh_make_shadow(v, gw->l2mfn, t);
1883 /* Install the new sl2 table in the sl3e */
1884 l3e_propagate_from_guest(v, gw->l3e, gw->l3mfn,
1885 *sl2mfn, &new_sl3e, ft);
1886 r = shadow_set_l3e(v, sl3e, new_sl3e, sl3mfn);
1887 ASSERT((r & SHADOW_SET_FLUSH) == 0);
1888 if ( r & SHADOW_SET_ERROR )
1889 return NULL;
1891 /* Now follow it down a level. Guaranteed to succeed. */
1892 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
1893 #elif GUEST_PAGING_LEVELS == 3 /* PAE... */
1894 /* We never demand-shadow PAE l3es: they are only created in
1895 * sh_update_cr3(). Check if the relevant sl3e is present. */
1896 shadow_l3e_t *sl3e = ((shadow_l3e_t *)&v->arch.paging.shadow.l3table)
1897 + shadow_l3_linear_offset(gw->va);
1898 if ( !(shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT) )
1899 return NULL;
1900 *sl2mfn = shadow_l3e_get_mfn(*sl3e);
1901 ASSERT(mfn_valid(*sl2mfn));
1902 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
1903 #else /* 32bit... */
1904 /* There is always a shadow of the top level table. Get it. */
1905 *sl2mfn = pagetable_get_mfn(v->arch.shadow_table[0]);
1906 /* This next line is important: the guest l2 has a 16k
1907 * shadow, we need to return the right mfn of the four. This
1908 * call will set it for us as a side-effect. */
1909 (void) shadow_l2_index(sl2mfn, guest_index(gw->l2e));
1910 /* Reading the top level table is always valid. */
1911 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
1912 #endif
1916 static shadow_l1e_t * shadow_get_and_create_l1e(struct vcpu *v,
1917 walk_t *gw,
1918 mfn_t *sl1mfn,
1919 fetch_type_t ft)
1921 mfn_t sl2mfn;
1922 shadow_l2e_t *sl2e;
1924 /* Get the l2e */
1925 sl2e = shadow_get_and_create_l2e(v, gw, &sl2mfn, ft);
1926 if ( sl2e == NULL ) return NULL;
1927 /* Install the sl1 in the l2e if it wasn't there or if we need to
1928 * re-do it to fix a PSE dirty bit. */
1929 if ( shadow_l2e_get_flags(*sl2e) & _PAGE_PRESENT
1930 && likely(ft != ft_demand_write
1931 || (guest_l2e_get_flags(*gw->l2e) & _PAGE_DIRTY)
1932 || !(guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE)) )
1934 *sl1mfn = shadow_l2e_get_mfn(*sl2e);
1935 ASSERT(mfn_valid(*sl1mfn));
1937 else
1939 shadow_l2e_t new_sl2e;
1940 int r, flags = guest_l2e_get_flags(*gw->l2e);
1941 /* No l1 shadow installed: find and install it. */
1942 if ( !(flags & _PAGE_PRESENT) )
1943 return NULL; /* No guest page. */
1944 if ( guest_supports_superpages(v) && (flags & _PAGE_PSE) )
1946 /* Splintering a superpage */
1947 gfn_t l2gfn = guest_l2e_get_gfn(*gw->l2e);
1948 *sl1mfn = get_fl1_shadow_status(v, l2gfn);
1949 if ( !mfn_valid(*sl1mfn) )
1951 /* No fl1 shadow of this superpage exists at all: make one. */
1952 *sl1mfn = make_fl1_shadow(v, l2gfn);
1955 else
1957 /* Shadowing an actual guest l1 table */
1958 if ( !mfn_valid(gw->l2mfn) ) return NULL; /* No guest page. */
1959 *sl1mfn = get_shadow_status(v, gw->l1mfn, SH_type_l1_shadow);
1960 if ( !mfn_valid(*sl1mfn) )
1962 /* No l1 shadow of this page exists at all: make one. */
1963 *sl1mfn = sh_make_shadow(v, gw->l1mfn, SH_type_l1_shadow);
1966 /* Install the new sl1 table in the sl2e */
1967 l2e_propagate_from_guest(v, gw->l2e, gw->l2mfn,
1968 *sl1mfn, &new_sl2e, ft);
1969 r = shadow_set_l2e(v, sl2e, new_sl2e, sl2mfn);
1970 ASSERT((r & SHADOW_SET_FLUSH) == 0);
1971 if ( r & SHADOW_SET_ERROR )
1972 return NULL;
1973 /* This next line is important: in 32-on-PAE and 32-on-64 modes,
1974 * the guest l1 table has an 8k shadow, and we need to return
1975 * the right mfn of the pair. This call will set it for us as a
1976 * side-effect. (In all other cases, it's a no-op and will be
1977 * compiled out.) */
1978 (void) shadow_l1_index(sl1mfn, guest_l1_table_offset(gw->va));
1980 /* Now follow it down a level. Guaranteed to succeed. */
1981 return sh_linear_l1_table(v) + shadow_l1_linear_offset(gw->va);
1986 /**************************************************************************/
1987 /* Destructors for shadow tables:
1988 * Unregister the shadow, decrement refcounts of any entries present in it,
1989 * and release the memory.
1991 * N.B. These destructors do not clear the contents of the shadows.
1992 * This allows us to delay TLB shootdowns until the page is being reused.
1993 * See shadow_alloc() and shadow_free() for how this is handled.
1994 */
1996 #if GUEST_PAGING_LEVELS >= 4
1997 void sh_destroy_l4_shadow(struct vcpu *v, mfn_t smfn)
1999 shadow_l4e_t *sl4e;
2000 u32 t = mfn_to_shadow_page(smfn)->type;
2001 mfn_t gmfn, sl4mfn;
2003 SHADOW_DEBUG(DESTROY_SHADOW,
2004 "%s(%05lx)\n", __func__, mfn_x(smfn));
2005 ASSERT(t == SH_type_l4_shadow);
2007 /* Record that the guest page isn't shadowed any more (in this type) */
2008 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2009 delete_shadow_status(v, gmfn, t, smfn);
2010 shadow_demote(v, gmfn, t);
2011 /* Decrement refcounts of all the old entries */
2012 sl4mfn = smfn;
2013 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, 0, v->domain, {
2014 if ( shadow_l4e_get_flags(*sl4e) & _PAGE_PRESENT )
2016 sh_put_ref(v, shadow_l4e_get_mfn(*sl4e),
2017 (((paddr_t)mfn_x(sl4mfn)) << PAGE_SHIFT)
2018 | ((unsigned long)sl4e & ~PAGE_MASK));
2020 });
2022 /* Put the memory back in the pool */
2023 shadow_free(v->domain, smfn);
2026 void sh_destroy_l3_shadow(struct vcpu *v, mfn_t smfn)
2028 shadow_l3e_t *sl3e;
2029 u32 t = mfn_to_shadow_page(smfn)->type;
2030 mfn_t gmfn, sl3mfn;
2032 SHADOW_DEBUG(DESTROY_SHADOW,
2033 "%s(%05lx)\n", __func__, mfn_x(smfn));
2034 ASSERT(t == SH_type_l3_shadow);
2036 /* Record that the guest page isn't shadowed any more (in this type) */
2037 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2038 delete_shadow_status(v, gmfn, t, smfn);
2039 shadow_demote(v, gmfn, t);
2041 /* Decrement refcounts of all the old entries */
2042 sl3mfn = smfn;
2043 SHADOW_FOREACH_L3E(sl3mfn, sl3e, 0, 0, {
2044 if ( shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT )
2045 sh_put_ref(v, shadow_l3e_get_mfn(*sl3e),
2046 (((paddr_t)mfn_x(sl3mfn)) << PAGE_SHIFT)
2047 | ((unsigned long)sl3e & ~PAGE_MASK));
2048 });
2050 /* Put the memory back in the pool */
2051 shadow_free(v->domain, smfn);
2053 #endif /* GUEST_PAGING_LEVELS >= 4 */
2056 void sh_destroy_l2_shadow(struct vcpu *v, mfn_t smfn)
2058 shadow_l2e_t *sl2e;
2059 u32 t = mfn_to_shadow_page(smfn)->type;
2060 mfn_t gmfn, sl2mfn;
2062 SHADOW_DEBUG(DESTROY_SHADOW,
2063 "%s(%05lx)\n", __func__, mfn_x(smfn));
2065 #if GUEST_PAGING_LEVELS >= 3
2066 ASSERT(t == SH_type_l2_shadow || t == SH_type_l2h_shadow);
2067 #else
2068 ASSERT(t == SH_type_l2_shadow);
2069 #endif
2071 /* Record that the guest page isn't shadowed any more (in this type) */
2072 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2073 delete_shadow_status(v, gmfn, t, smfn);
2074 shadow_demote(v, gmfn, t);
2076 /* Decrement refcounts of all the old entries */
2077 sl2mfn = smfn;
2078 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, v->domain, {
2079 if ( shadow_l2e_get_flags(*sl2e) & _PAGE_PRESENT )
2080 sh_put_ref(v, shadow_l2e_get_mfn(*sl2e),
2081 (((paddr_t)mfn_x(sl2mfn)) << PAGE_SHIFT)
2082 | ((unsigned long)sl2e & ~PAGE_MASK));
2083 });
2085 /* Put the memory back in the pool */
2086 shadow_free(v->domain, smfn);
2089 void sh_destroy_l1_shadow(struct vcpu *v, mfn_t smfn)
2091 struct domain *d = v->domain;
2092 shadow_l1e_t *sl1e;
2093 u32 t = mfn_to_shadow_page(smfn)->type;
2095 SHADOW_DEBUG(DESTROY_SHADOW,
2096 "%s(%05lx)\n", __func__, mfn_x(smfn));
2097 ASSERT(t == SH_type_l1_shadow || t == SH_type_fl1_shadow);
2099 /* Record that the guest page isn't shadowed any more (in this type) */
2100 if ( t == SH_type_fl1_shadow )
2102 gfn_t gfn = _gfn(mfn_to_shadow_page(smfn)->backpointer);
2103 delete_fl1_shadow_status(v, gfn, smfn);
2105 else
2107 mfn_t gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2108 delete_shadow_status(v, gmfn, t, smfn);
2109 shadow_demote(v, gmfn, t);
2112 if ( shadow_mode_refcounts(d) )
2114 /* Decrement refcounts of all the old entries */
2115 mfn_t sl1mfn = smfn;
2116 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, 0, {
2117 if ( (shadow_l1e_get_flags(*sl1e) & _PAGE_PRESENT)
2118 && !sh_l1e_is_magic(*sl1e) )
2119 shadow_put_page_from_l1e(*sl1e, d);
2120 });
2123 /* Put the memory back in the pool */
2124 shadow_free(v->domain, smfn);
2127 #if SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS
2128 void sh_destroy_monitor_table(struct vcpu *v, mfn_t mmfn)
2130 struct domain *d = v->domain;
2131 ASSERT(mfn_to_shadow_page(mmfn)->type == SH_type_monitor_table);
2133 #if (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS != 4)
2134 /* Need to destroy the l3 monitor page in slot 0 too */
2136 mfn_t m3mfn;
2137 l4_pgentry_t *l4e = sh_map_domain_page(mmfn);
2138 ASSERT(l4e_get_flags(l4e[0]) & _PAGE_PRESENT);
2139 m3mfn = _mfn(l4e_get_pfn(l4e[0]));
2140 if ( pv_32bit_guest(v) )
2142 /* Need to destroy the l2 monitor page in slot 3 too */
2143 l3_pgentry_t *l3e = sh_map_domain_page(m3mfn);
2144 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
2145 shadow_free(d, _mfn(l3e_get_pfn(l3e[3])));
2146 sh_unmap_domain_page(l3e);
2148 shadow_free(d, m3mfn);
2149 sh_unmap_domain_page(l4e);
2151 #elif CONFIG_PAGING_LEVELS == 3
2152 /* Need to destroy the l2 monitor page in slot 4 too */
2154 l3_pgentry_t *l3e = sh_map_domain_page(mmfn);
2155 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
2156 shadow_free(d, _mfn(l3e_get_pfn(l3e[3])));
2157 sh_unmap_domain_page(l3e);
2159 #endif
2161 /* Put the memory back in the pool */
2162 shadow_free(d, mmfn);
2164 #endif
2166 /**************************************************************************/
2167 /* Functions to destroy non-Xen mappings in a pagetable hierarchy.
2168 * These are called from common code when we are running out of shadow
2169 * memory, and unpinning all the top-level shadows hasn't worked.
2171 * This implementation is pretty crude and slow, but we hope that it won't
2172 * be called very often. */
2174 #if GUEST_PAGING_LEVELS == 2
2176 void sh_unhook_32b_mappings(struct vcpu *v, mfn_t sl2mfn)
2178 shadow_l2e_t *sl2e;
2179 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, v->domain, {
2180 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
2181 });
2184 #elif GUEST_PAGING_LEVELS == 3
2186 void sh_unhook_pae_mappings(struct vcpu *v, mfn_t sl2mfn)
2187 /* Walk a PAE l2 shadow, unhooking entries from all the subshadows */
2189 shadow_l2e_t *sl2e;
2190 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, v->domain, {
2191 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
2192 });
2195 #elif GUEST_PAGING_LEVELS == 4
2197 void sh_unhook_64b_mappings(struct vcpu *v, mfn_t sl4mfn)
2199 shadow_l4e_t *sl4e;
2200 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, 0, v->domain, {
2201 (void) shadow_set_l4e(v, sl4e, shadow_l4e_empty(), sl4mfn);
2202 });
2205 #endif
2207 /**************************************************************************/
2208 /* Internal translation functions.
2209 * These functions require a pointer to the shadow entry that will be updated.
2210 */
2212 /* These functions take a new guest entry, translate it to shadow and write
2213 * the shadow entry.
2215 * They return the same bitmaps as the shadow_set_lXe() functions.
2216 */
2218 #if GUEST_PAGING_LEVELS >= 4
2219 static int validate_gl4e(struct vcpu *v, void *new_ge, mfn_t sl4mfn, void *se)
2221 shadow_l4e_t new_sl4e;
2222 guest_l4e_t *new_gl4e = new_ge;
2223 shadow_l4e_t *sl4p = se;
2224 mfn_t sl3mfn = _mfn(INVALID_MFN);
2225 int result = 0;
2227 perfc_incrc(shadow_validate_gl4e_calls);
2229 if ( guest_l4e_get_flags(*new_gl4e) & _PAGE_PRESENT )
2231 gfn_t gl3gfn = guest_l4e_get_gfn(*new_gl4e);
2232 mfn_t gl3mfn = vcpu_gfn_to_mfn(v, gl3gfn);
2233 if ( mfn_valid(gl3mfn) )
2234 sl3mfn = get_shadow_status(v, gl3mfn, SH_type_l3_shadow);
2235 else
2236 result |= SHADOW_SET_ERROR;
2238 l4e_propagate_from_guest(v, new_gl4e, _mfn(INVALID_MFN),
2239 sl3mfn, &new_sl4e, ft_prefetch);
2241 // check for updates to xen reserved slots
2242 if ( !shadow_mode_external(v->domain) )
2244 int shadow_index = (((unsigned long)sl4p & ~PAGE_MASK) /
2245 sizeof(shadow_l4e_t));
2246 int reserved_xen_slot = !is_guest_l4_slot(v->domain, shadow_index);
2248 if ( unlikely(reserved_xen_slot) )
2250 // attempt by the guest to write to a xen reserved slot
2251 //
2252 SHADOW_PRINTK("%s out-of-range update "
2253 "sl4mfn=%05lx index=0x%x val=%" SH_PRI_pte "\n",
2254 __func__, mfn_x(sl4mfn), shadow_index, new_sl4e.l4);
2255 if ( shadow_l4e_get_flags(new_sl4e) & _PAGE_PRESENT )
2257 SHADOW_ERROR("out-of-range l4e update\n");
2258 result |= SHADOW_SET_ERROR;
2261 // do not call shadow_set_l4e...
2262 return result;
2266 result |= shadow_set_l4e(v, sl4p, new_sl4e, sl4mfn);
2267 return result;
2271 static int validate_gl3e(struct vcpu *v, void *new_ge, mfn_t sl3mfn, void *se)
2273 shadow_l3e_t new_sl3e;
2274 guest_l3e_t *new_gl3e = new_ge;
2275 shadow_l3e_t *sl3p = se;
2276 mfn_t sl2mfn = _mfn(INVALID_MFN);
2277 int result = 0;
2279 perfc_incrc(shadow_validate_gl3e_calls);
2281 if ( guest_l3e_get_flags(*new_gl3e) & _PAGE_PRESENT )
2283 gfn_t gl2gfn = guest_l3e_get_gfn(*new_gl3e);
2284 mfn_t gl2mfn = vcpu_gfn_to_mfn(v, gl2gfn);
2285 if ( mfn_valid(gl2mfn) )
2286 sl2mfn = get_shadow_status(v, gl2mfn, SH_type_l2_shadow);
2287 else
2288 result |= SHADOW_SET_ERROR;
2290 l3e_propagate_from_guest(v, new_gl3e, _mfn(INVALID_MFN),
2291 sl2mfn, &new_sl3e, ft_prefetch);
2292 result |= shadow_set_l3e(v, sl3p, new_sl3e, sl3mfn);
2294 return result;
2296 #endif // GUEST_PAGING_LEVELS >= 4
2298 static int validate_gl2e(struct vcpu *v, void *new_ge, mfn_t sl2mfn, void *se)
2300 shadow_l2e_t new_sl2e;
2301 guest_l2e_t *new_gl2e = new_ge;
2302 shadow_l2e_t *sl2p = se;
2303 mfn_t sl1mfn = _mfn(INVALID_MFN);
2304 int result = 0;
2306 perfc_incrc(shadow_validate_gl2e_calls);
2308 if ( guest_l2e_get_flags(*new_gl2e) & _PAGE_PRESENT )
2310 gfn_t gl1gfn = guest_l2e_get_gfn(*new_gl2e);
2311 if ( guest_supports_superpages(v) &&
2312 (guest_l2e_get_flags(*new_gl2e) & _PAGE_PSE) )
2314 // superpage -- need to look up the shadow L1 which holds the
2315 // splitters...
2316 sl1mfn = get_fl1_shadow_status(v, gl1gfn);
2317 #if 0
2318 // XXX - it's possible that we want to do some kind of prefetch
2319 // for superpage fl1's here, but this is *not* on the demand path,
2320 // so we'll hold off trying that for now...
2321 //
2322 if ( !mfn_valid(sl1mfn) )
2323 sl1mfn = make_fl1_shadow(v, gl1gfn);
2324 #endif
2326 else
2328 mfn_t gl1mfn = vcpu_gfn_to_mfn(v, gl1gfn);
2329 if ( mfn_valid(gl1mfn) )
2330 sl1mfn = get_shadow_status(v, gl1mfn, SH_type_l1_shadow);
2331 else
2332 result |= SHADOW_SET_ERROR;
2335 l2e_propagate_from_guest(v, new_gl2e, _mfn(INVALID_MFN),
2336 sl1mfn, &new_sl2e, ft_prefetch);
2338 // check for updates to xen reserved slots in PV guests...
2339 // XXX -- need to revisit this for PV 3-on-4 guests.
2340 //
2341 #if SHADOW_PAGING_LEVELS < 4
2342 #if CONFIG_PAGING_LEVELS == SHADOW_PAGING_LEVELS
2343 if ( !shadow_mode_external(v->domain) )
2345 int shadow_index = (((unsigned long)sl2p & ~PAGE_MASK) /
2346 sizeof(shadow_l2e_t));
2347 int reserved_xen_slot;
2349 #if SHADOW_PAGING_LEVELS == 3
2350 reserved_xen_slot =
2351 ((mfn_to_shadow_page(sl2mfn)->type == SH_type_l2h_pae_shadow) &&
2352 (shadow_index
2353 >= (L2_PAGETABLE_FIRST_XEN_SLOT & (L2_PAGETABLE_ENTRIES-1))));
2354 #else /* SHADOW_PAGING_LEVELS == 2 */
2355 reserved_xen_slot = (shadow_index >= L2_PAGETABLE_FIRST_XEN_SLOT);
2356 #endif
2358 if ( unlikely(reserved_xen_slot) )
2360 // attempt by the guest to write to a xen reserved slot
2361 //
2362 SHADOW_PRINTK("%s out-of-range update "
2363 "sl2mfn=%05lx index=0x%x val=%" SH_PRI_pte "\n",
2364 __func__, mfn_x(sl2mfn), shadow_index, new_sl2e.l2);
2365 if ( shadow_l2e_get_flags(new_sl2e) & _PAGE_PRESENT )
2367 SHADOW_ERROR("out-of-range l2e update\n");
2368 result |= SHADOW_SET_ERROR;
2371 // do not call shadow_set_l2e...
2372 return result;
2375 #endif /* CONFIG_PAGING_LEVELS == SHADOW_PAGING_LEVELS */
2376 #endif /* SHADOW_PAGING_LEVELS < 4 */
2378 result |= shadow_set_l2e(v, sl2p, new_sl2e, sl2mfn);
2380 return result;
2383 static int validate_gl1e(struct vcpu *v, void *new_ge, mfn_t sl1mfn, void *se)
2385 shadow_l1e_t new_sl1e;
2386 guest_l1e_t *new_gl1e = new_ge;
2387 shadow_l1e_t *sl1p = se;
2388 gfn_t gfn;
2389 mfn_t gmfn;
2390 int result = 0, mmio;
2392 perfc_incrc(shadow_validate_gl1e_calls);
2394 gfn = guest_l1e_get_gfn(*new_gl1e);
2395 gmfn = vcpu_gfn_to_mfn(v, gfn);
2397 mmio = (is_hvm_vcpu(v) && paging_vcpu_mode_translate(v) &&
2398 mmio_space(gfn_to_paddr(gfn)));
2399 l1e_propagate_from_guest(v, new_gl1e, _mfn(INVALID_MFN), gmfn, &new_sl1e,
2400 ft_prefetch, mmio);
2402 result |= shadow_set_l1e(v, sl1p, new_sl1e, sl1mfn);
2403 return result;
2407 /**************************************************************************/
2408 /* Functions which translate and install the shadows of arbitrary guest
2409 * entries that we have just seen the guest write. */
2412 static inline int
2413 sh_map_and_validate(struct vcpu *v, mfn_t gmfn,
2414 void *new_gp, u32 size, u32 sh_type,
2415 u32 (*shadow_index)(mfn_t *smfn, u32 idx),
2416 int (*validate_ge)(struct vcpu *v, void *ge,
2417 mfn_t smfn, void *se))
2418 /* Generic function for mapping and validating. */
2420 mfn_t smfn, smfn2, map_mfn;
2421 shadow_l1e_t *sl1p;
2422 u32 shadow_idx, guest_idx;
2423 int result = 0;
2425 /* Align address and size to guest entry boundaries */
2426 size += (unsigned long)new_gp & (sizeof (guest_l1e_t) - 1);
2427 new_gp = (void *)((unsigned long)new_gp & ~(sizeof (guest_l1e_t) - 1));
2428 size = (size + sizeof (guest_l1e_t) - 1) & ~(sizeof (guest_l1e_t) - 1);
2429 ASSERT(size + (((unsigned long)new_gp) & ~PAGE_MASK) <= PAGE_SIZE);
2431 /* Map the shadow page */
2432 smfn = get_shadow_status(v, gmfn, sh_type);
2433 ASSERT(mfn_valid(smfn)); /* Otherwise we would not have been called */
2434 guest_idx = guest_index(new_gp);
2435 map_mfn = smfn;
2436 shadow_idx = shadow_index(&map_mfn, guest_idx);
2437 sl1p = map_shadow_page(map_mfn);
2439 /* Validate one entry at a time */
2440 while ( size )
2442 smfn2 = smfn;
2443 guest_idx = guest_index(new_gp);
2444 shadow_idx = shadow_index(&smfn2, guest_idx);
2445 if ( mfn_x(smfn2) != mfn_x(map_mfn) )
2447 /* We have moved to another page of the shadow */
2448 map_mfn = smfn2;
2449 unmap_shadow_page(sl1p);
2450 sl1p = map_shadow_page(map_mfn);
2452 result |= validate_ge(v,
2453 new_gp,
2454 map_mfn,
2455 &sl1p[shadow_idx]);
2456 size -= sizeof(guest_l1e_t);
2457 new_gp += sizeof(guest_l1e_t);
2459 unmap_shadow_page(sl1p);
2460 return result;
2464 int
2465 sh_map_and_validate_gl4e(struct vcpu *v, mfn_t gl4mfn,
2466 void *new_gl4p, u32 size)
2468 #if GUEST_PAGING_LEVELS >= 4
2469 return sh_map_and_validate(v, gl4mfn, new_gl4p, size,
2470 SH_type_l4_shadow,
2471 shadow_l4_index,
2472 validate_gl4e);
2473 #else // ! GUEST_PAGING_LEVELS >= 4
2474 SHADOW_PRINTK("called in wrong paging mode!\n");
2475 BUG();
2476 return 0;
2477 #endif
2480 int
2481 sh_map_and_validate_gl3e(struct vcpu *v, mfn_t gl3mfn,
2482 void *new_gl3p, u32 size)
2484 #if GUEST_PAGING_LEVELS >= 4
2485 return sh_map_and_validate(v, gl3mfn, new_gl3p, size,
2486 SH_type_l3_shadow,
2487 shadow_l3_index,
2488 validate_gl3e);
2489 #else // ! GUEST_PAGING_LEVELS >= 4
2490 SHADOW_PRINTK("called in wrong paging mode!\n");
2491 BUG();
2492 return 0;
2493 #endif
2496 int
2497 sh_map_and_validate_gl2e(struct vcpu *v, mfn_t gl2mfn,
2498 void *new_gl2p, u32 size)
2500 return sh_map_and_validate(v, gl2mfn, new_gl2p, size,
2501 SH_type_l2_shadow,
2502 shadow_l2_index,
2503 validate_gl2e);
2506 int
2507 sh_map_and_validate_gl2he(struct vcpu *v, mfn_t gl2mfn,
2508 void *new_gl2p, u32 size)
2510 #if GUEST_PAGING_LEVELS >= 3
2511 return sh_map_and_validate(v, gl2mfn, new_gl2p, size,
2512 SH_type_l2h_shadow,
2513 shadow_l2_index,
2514 validate_gl2e);
2515 #else /* Non-PAE guests don't have different kinds of l2 table */
2516 SHADOW_PRINTK("called in wrong paging mode!\n");
2517 BUG();
2518 return 0;
2519 #endif
2522 int
2523 sh_map_and_validate_gl1e(struct vcpu *v, mfn_t gl1mfn,
2524 void *new_gl1p, u32 size)
2526 return sh_map_and_validate(v, gl1mfn, new_gl1p, size,
2527 SH_type_l1_shadow,
2528 shadow_l1_index,
2529 validate_gl1e);
2533 /**************************************************************************/
2534 /* Optimization: If we see two emulated writes of zeros to the same
2535 * page-table without another kind of page fault in between, we guess
2536 * that this is a batch of changes (for process destruction) and
2537 * unshadow the page so we don't take a pagefault on every entry. This
2538 * should also make finding writeable mappings of pagetables much
2539 * easier. */
2541 /* Look to see if this is the second emulated write in a row to this
2542 * page, and unshadow/unhook if it is */
2543 static inline void check_for_early_unshadow(struct vcpu *v, mfn_t gmfn)
2545 #if SHADOW_OPTIMIZATIONS & SHOPT_EARLY_UNSHADOW
2546 if ( v->arch.paging.shadow.last_emulated_mfn == mfn_x(gmfn) &&
2547 sh_mfn_is_a_page_table(gmfn) )
2549 u32 flags = mfn_to_page(gmfn)->shadow_flags;
2550 if ( !(flags & (SHF_L2_32|SHF_L2_PAE|SHF_L2H_PAE|SHF_L4_64)) )
2552 perfc_incrc(shadow_early_unshadow);
2553 sh_remove_shadows(v, gmfn, 1, 0 /* Fast, can fail to unshadow */ );
2556 v->arch.paging.shadow.last_emulated_mfn = mfn_x(gmfn);
2557 #endif
2560 /* Stop counting towards early unshadows, as we've seen a real page fault */
2561 static inline void reset_early_unshadow(struct vcpu *v)
2563 #if SHADOW_OPTIMIZATIONS & SHOPT_EARLY_UNSHADOW
2564 v->arch.paging.shadow.last_emulated_mfn = INVALID_MFN;
2565 #endif
2570 /**************************************************************************/
2571 /* Optimization: Prefetch multiple L1 entries. This is called after we have
2572 * demand-faulted a shadow l1e in the fault handler, to see if it's
2573 * worth fetching some more.
2574 */
2576 #if SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH
2578 /* XXX magic number */
2579 #define PREFETCH_DISTANCE 32
2581 static void sh_prefetch(struct vcpu *v, walk_t *gw,
2582 shadow_l1e_t *ptr_sl1e, mfn_t sl1mfn)
2584 int i, dist, mmio;
2585 gfn_t gfn;
2586 mfn_t gmfn;
2587 guest_l1e_t gl1e;
2588 shadow_l1e_t sl1e;
2589 u32 gflags;
2591 /* Prefetch no further than the end of the _shadow_ l1 MFN */
2592 dist = (PAGE_SIZE - ((unsigned long)ptr_sl1e & ~PAGE_MASK)) / sizeof sl1e;
2593 /* And no more than a maximum fetches-per-fault */
2594 if ( dist > PREFETCH_DISTANCE )
2595 dist = PREFETCH_DISTANCE;
2597 for ( i = 1; i < dist ; i++ )
2599 /* No point in prefetching if there's already a shadow */
2600 if ( ptr_sl1e[i].l1 != 0 )
2601 break;
2603 if ( gw->l1e )
2605 /* Normal guest page; grab the next guest entry */
2606 gl1e = gw->l1e[i];
2607 /* Not worth continuing if we hit an entry that will need another
2608 * fault for A/D-bit propagation anyway */
2609 gflags = guest_l1e_get_flags(gl1e);
2610 if ( (gflags & _PAGE_PRESENT)
2611 && (!(gflags & _PAGE_ACCESSED)
2612 || ((gflags & _PAGE_RW) && !(gflags & _PAGE_DIRTY))) )
2613 break;
2615 else
2617 /* Fragmented superpage, unless we've been called wrongly */
2618 ASSERT(guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE);
2619 /* Increment the l1e's GFN by the right number of guest pages */
2620 gl1e = guest_l1e_from_gfn(
2621 _gfn(gfn_x(guest_l1e_get_gfn(gw->eff_l1e)) + i),
2622 guest_l1e_get_flags(gw->eff_l1e));
2625 /* Look at the gfn that the l1e is pointing at */
2626 gfn = guest_l1e_get_gfn(gl1e);
2627 gmfn = vcpu_gfn_to_mfn(v, gfn);
2628 mmio = ( is_hvm_vcpu(v)
2629 && paging_vcpu_mode_translate(v)
2630 && mmio_space(gfn_to_paddr(gfn)) );
2632 /* Propagate the entry. Safe to use a pointer to our local
2633 * gl1e, since this is not a demand-fetch so there will be no
2634 * write-back to the guest. */
2635 l1e_propagate_from_guest(v, &gl1e, _mfn(INVALID_MFN),
2636 gmfn, &sl1e, ft_prefetch, mmio);
2637 (void) shadow_set_l1e(v, ptr_sl1e + i, sl1e, sl1mfn);
2641 #endif /* SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH */
2644 /**************************************************************************/
2645 /* Entry points into the shadow code */
2647 /* Called from pagefault handler in Xen, and from the HVM trap handlers
2648 * for pagefaults. Returns 1 if this fault was an artefact of the
2649 * shadow code (and the guest should retry) or 0 if it is not (and the
2650 * fault should be handled elsewhere or passed to the guest). */
2652 static int sh_page_fault(struct vcpu *v,
2653 unsigned long va,
2654 struct cpu_user_regs *regs)
2656 struct domain *d = v->domain;
2657 walk_t gw;
2658 u32 accumulated_gflags;
2659 gfn_t gfn;
2660 mfn_t gmfn, sl1mfn=_mfn(0);
2661 shadow_l1e_t sl1e, *ptr_sl1e;
2662 paddr_t gpa;
2663 struct sh_emulate_ctxt emul_ctxt;
2664 struct x86_emulate_ops *emul_ops;
2665 int r, mmio;
2666 fetch_type_t ft = 0;
2668 SHADOW_PRINTK("d:v=%u:%u va=%#lx err=%u\n",
2669 v->domain->domain_id, v->vcpu_id, va, regs->error_code);
2671 perfc_incrc(shadow_fault);
2672 //
2673 // XXX: Need to think about eventually mapping superpages directly in the
2674 // shadow (when possible), as opposed to splintering them into a
2675 // bunch of 4K maps.
2676 //
2678 #if (SHADOW_OPTIMIZATIONS & SHOPT_FAST_FAULT_PATH) && SHADOW_PAGING_LEVELS > 2
2679 if ( (regs->error_code & PFEC_reserved_bit) )
2681 /* The only reasons for reserved bits to be set in shadow entries
2682 * are the two "magic" shadow_l1e entries. */
2683 if ( likely((__copy_from_user(&sl1e,
2684 (sh_linear_l1_table(v)
2685 + shadow_l1_linear_offset(va)),
2686 sizeof(sl1e)) == 0)
2687 && sh_l1e_is_magic(sl1e)) )
2689 if ( sh_l1e_is_gnp(sl1e) )
2691 if ( likely(!is_hvm_domain(d) ||
2692 paging_vcpu_mode_translate(v)) )
2694 /* Not-present in a guest PT: pass to the guest as
2695 * a not-present fault (by flipping two bits). */
2696 ASSERT(regs->error_code & PFEC_page_present);
2697 regs->error_code ^= (PFEC_reserved_bit|PFEC_page_present);
2698 perfc_incrc(shadow_fault_fast_gnp);
2699 SHADOW_PRINTK("fast path not-present\n");
2700 return 0;
2702 else
2704 /* Not-present in the P2M: MMIO */
2705 gpa = va;
2708 else
2710 /* Magic MMIO marker: extract gfn for MMIO address */
2711 ASSERT(sh_l1e_is_mmio(sl1e));
2712 gpa = (((paddr_t)(gfn_x(sh_l1e_mmio_get_gfn(sl1e))))
2713 << PAGE_SHIFT)
2714 | (va & ~PAGE_MASK);
2716 perfc_incrc(shadow_fault_fast_mmio);
2717 SHADOW_PRINTK("fast path mmio %#"PRIpaddr"\n", gpa);
2718 reset_early_unshadow(v);
2719 handle_mmio(gpa);
2720 return EXCRET_fault_fixed;
2722 else
2724 /* This should be exceptionally rare: another vcpu has fixed
2725 * the tables between the fault and our reading the l1e.
2726 * Retry and let the hardware give us the right fault next time. */
2727 perfc_incrc(shadow_fault_fast_fail);
2728 SHADOW_PRINTK("fast path false alarm!\n");
2729 return EXCRET_fault_fixed;
2732 #endif /* SHOPT_FAST_FAULT_PATH */
2734 /* Detect if this page fault happened while we were already in Xen
2735 * doing a shadow operation. If that happens, the only thing we can
2736 * do is let Xen's normal fault handlers try to fix it. In any case,
2737 * a diagnostic trace of the fault will be more useful than
2738 * a BUG() when we try to take the lock again. */
2739 if ( unlikely(shadow_locked_by_me(d)) )
2741 SHADOW_ERROR("Recursive shadow fault: lock was taken by %s\n",
2742 d->arch.paging.shadow.locker_function);
2743 return 0;
2746 shadow_lock(d);
2748 shadow_audit_tables(v);
2750 if ( guest_walk_tables(v, va, &gw, 1) != 0 )
2752 SHADOW_PRINTK("malformed guest pagetable!");
2753 print_gw(&gw);
2756 sh_audit_gw(v, &gw);
2758 // We do not look at the gw->l1e, as that will not exist for superpages.
2759 // Instead, we use the gw->eff_l1e...
2760 //
2761 // We need not check all the levels of the guest page table entries for
2762 // present vs not-present, as the eff_l1e will always be not present if
2763 // one of the higher level entries is not present.
2764 //
2765 if ( unlikely(!(guest_l1e_get_flags(gw.eff_l1e) & _PAGE_PRESENT)) )
2767 if ( is_hvm_domain(d) && !paging_vcpu_mode_translate(v) )
2769 /* Not present in p2m map, means this is mmio */
2770 gpa = va;
2771 goto mmio;
2774 perfc_incrc(shadow_fault_bail_not_present);
2775 goto not_a_shadow_fault;
2778 // All levels of the guest page table are now known to be present.
2779 accumulated_gflags = accumulate_guest_flags(v, &gw);
2781 // Check for attempts to access supervisor-only pages from user mode,
2782 // i.e. ring 3. Such errors are not caused or dealt with by the shadow
2783 // code.
2784 //
2785 if ( (regs->error_code & PFEC_user_mode) &&
2786 !(accumulated_gflags & _PAGE_USER) )
2788 /* illegal user-mode access to supervisor-only page */
2789 perfc_incrc(shadow_fault_bail_user_supervisor);
2790 goto not_a_shadow_fault;
2793 // Was it a write fault?
2794 ft = ((regs->error_code & PFEC_write_access)
2795 ? ft_demand_write : ft_demand_read);
2796 if ( ft == ft_demand_write )
2798 if ( unlikely(!(accumulated_gflags & _PAGE_RW)) )
2800 perfc_incrc(shadow_fault_bail_ro_mapping);
2801 goto not_a_shadow_fault;
2804 else // must have been either an insn fetch or read fault
2806 // Check for NX bit violations: attempts to execute code that is
2807 // marked "do not execute". Such errors are not caused or dealt with
2808 // by the shadow code.
2809 //
2810 if ( regs->error_code & PFEC_insn_fetch )
2812 if ( accumulated_gflags & _PAGE_NX_BIT )
2814 /* NX prevented this code fetch */
2815 perfc_incrc(shadow_fault_bail_nx);
2816 goto not_a_shadow_fault;
2821 /* What mfn is the guest trying to access? */
2822 gfn = guest_l1e_get_gfn(gw.eff_l1e);
2823 gmfn = vcpu_gfn_to_mfn(v, gfn);
2824 mmio = (is_hvm_domain(d)
2825 && paging_vcpu_mode_translate(v)
2826 && mmio_space(gfn_to_paddr(gfn)));
2828 if ( !mmio && !mfn_valid(gmfn) )
2830 perfc_incrc(shadow_fault_bail_bad_gfn);
2831 SHADOW_PRINTK("BAD gfn=%"SH_PRI_gfn" gmfn=%"PRI_mfn"\n",
2832 gfn_x(gfn), mfn_x(gmfn));
2833 goto not_a_shadow_fault;
2836 /* Make sure there is enough free shadow memory to build a chain of
2837 * shadow tables: one SHADOW_MAX_ORDER chunk will always be enough
2838 * to allocate all we need. (We never allocate a top-level shadow
2839 * on this path, only a 32b l1, pae l2+1 or 64b l3+2+1) */
2840 shadow_prealloc(d, SHADOW_MAX_ORDER);
2842 /* Acquire the shadow. This must happen before we figure out the rights
2843 * for the shadow entry, since we might promote a page here. */
2844 ptr_sl1e = shadow_get_and_create_l1e(v, &gw, &sl1mfn, ft);
2845 if ( unlikely(ptr_sl1e == NULL) )
2847 /* Couldn't get the sl1e! Since we know the guest entries
2848 * are OK, this can only have been caused by a failed
2849 * shadow_set_l*e(), which will have crashed the guest.
2850 * Get out of the fault handler immediately. */
2851 ASSERT(test_bit(_DOMF_dying, &d->domain_flags));
2852 unmap_walk(v, &gw);
2853 shadow_unlock(d);
2854 return 0;
2857 /* Calculate the shadow entry and write it */
2858 l1e_propagate_from_guest(v, (gw.l1e) ? gw.l1e : &gw.eff_l1e, gw.l1mfn,
2859 gmfn, &sl1e, ft, mmio);
2860 r = shadow_set_l1e(v, ptr_sl1e, sl1e, sl1mfn);
2862 #if SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH
2863 /* Prefetch some more shadow entries */
2864 sh_prefetch(v, &gw, ptr_sl1e, sl1mfn);
2865 #endif
2867 /* Need to emulate accesses to page tables */
2868 if ( sh_mfn_is_a_page_table(gmfn) )
2870 if ( ft == ft_demand_write )
2872 perfc_incrc(shadow_fault_emulate_write);
2873 goto emulate;
2875 else if ( shadow_mode_trap_reads(d) && ft == ft_demand_read )
2877 perfc_incrc(shadow_fault_emulate_read);
2878 goto emulate;
2882 if ( mmio )
2884 gpa = guest_walk_to_gpa(&gw);
2885 goto mmio;
2888 perfc_incrc(shadow_fault_fixed);
2889 d->arch.paging.shadow.fault_count++;
2890 reset_early_unshadow(v);
2892 done:
2893 sh_audit_gw(v, &gw);
2894 unmap_walk(v, &gw);
2895 SHADOW_PRINTK("fixed\n");
2896 shadow_audit_tables(v);
2897 shadow_unlock(d);
2898 return EXCRET_fault_fixed;
2900 emulate:
2901 if ( !shadow_mode_refcounts(d) || !guest_mode(regs) )
2902 goto not_a_shadow_fault;
2904 if ( is_hvm_domain(d) )
2906 /*
2907 * If we are in the middle of injecting an exception or interrupt then
2908 * we should not emulate: it is not the instruction at %eip that caused
2909 * the fault. Furthermore it is almost certainly the case the handler
2910 * stack is currently considered to be a page table, so we should
2911 * unshadow the faulting page before exiting.
2912 */
2913 if ( unlikely(hvm_event_injection_faulted(v)) )
2915 gdprintk(XENLOG_DEBUG, "write to pagetable during event "
2916 "injection: cr2=%#lx, mfn=%#lx\n",
2917 va, mfn_x(gmfn));
2918 sh_remove_shadows(v, gmfn, 0 /* thorough */, 1 /* must succeed */);
2919 goto done;
2922 hvm_store_cpu_guest_regs(v, regs, NULL);
2925 SHADOW_PRINTK("emulate: eip=%#lx esp=%#lx\n",
2926 (unsigned long)regs->eip, (unsigned long)regs->esp);
2928 emul_ops = shadow_init_emulation(&emul_ctxt, regs);
2930 /*
2931 * We do not emulate user writes. Instead we use them as a hint that the
2932 * page is no longer a page table. This behaviour differs from native, but
2933 * it seems very unlikely that any OS grants user access to page tables.
2934 */
2935 r = X86EMUL_UNHANDLEABLE;
2936 if ( !(regs->error_code & PFEC_user_mode) )
2937 r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
2939 /*
2940 * NB. We do not unshadow on X86EMUL_EXCEPTION. It's not clear that it
2941 * would be a good unshadow hint. If we *do* decide to unshadow-on-fault
2942 * then it must be 'failable': we cannot require the unshadow to succeed.
2943 */
2944 if ( r == X86EMUL_UNHANDLEABLE )
2946 SHADOW_PRINTK("emulator failure, unshadowing mfn %#lx\n",
2947 mfn_x(gmfn));
2948 perfc_incrc(shadow_fault_emulate_failed);
2949 /* If this is actually a page table, then we have a bug, and need
2950 * to support more operations in the emulator. More likely,
2951 * though, this is a hint that this page should not be shadowed. */
2952 sh_remove_shadows(v, gmfn, 0 /* thorough */, 1 /* must succeed */);
2955 /* Emulator has changed the user registers: write back */
2956 if ( is_hvm_domain(d) )
2957 hvm_load_cpu_guest_regs(v, regs);
2958 goto done;
2960 mmio:
2961 if ( !guest_mode(regs) )
2962 goto not_a_shadow_fault;
2963 perfc_incrc(shadow_fault_mmio);
2964 sh_audit_gw(v, &gw);
2965 unmap_walk(v, &gw);
2966 SHADOW_PRINTK("mmio %#"PRIpaddr"\n", gpa);
2967 shadow_audit_tables(v);
2968 reset_early_unshadow(v);
2969 shadow_unlock(d);
2970 handle_mmio(gpa);
2971 return EXCRET_fault_fixed;
2973 not_a_shadow_fault:
2974 sh_audit_gw(v, &gw);
2975 unmap_walk(v, &gw);
2976 SHADOW_PRINTK("not a shadow fault\n");
2977 shadow_audit_tables(v);
2978 reset_early_unshadow(v);
2979 shadow_unlock(d);
2980 return 0;
2984 static int
2985 sh_invlpg(struct vcpu *v, unsigned long va)
2986 /* Called when the guest requests an invlpg. Returns 1 if the invlpg
2987 * instruction should be issued on the hardware, or 0 if it's safe not
2988 * to do so. */
2990 shadow_l2e_t sl2e;
2992 perfc_incrc(shadow_invlpg);
2994 /* First check that we can safely read the shadow l2e. SMP/PAE linux can
2995 * run as high as 6% of invlpg calls where we haven't shadowed the l2
2996 * yet. */
2997 #if SHADOW_PAGING_LEVELS == 4
2999 shadow_l3e_t sl3e;
3000 if ( !(shadow_l4e_get_flags(
3001 sh_linear_l4_table(v)[shadow_l4_linear_offset(va)])
3002 & _PAGE_PRESENT) )
3003 return 0;
3004 /* This must still be a copy-from-user because we don't have the
3005 * shadow lock, and the higher-level shadows might disappear
3006 * under our feet. */
3007 if ( __copy_from_user(&sl3e, (sh_linear_l3_table(v)
3008 + shadow_l3_linear_offset(va)),
3009 sizeof (sl3e)) != 0 )
3011 perfc_incrc(shadow_invlpg_fault);
3012 return 0;
3014 if ( (!shadow_l3e_get_flags(sl3e) & _PAGE_PRESENT) )
3015 return 0;
3017 #elif SHADOW_PAGING_LEVELS == 3
3018 if ( !(l3e_get_flags(v->arch.paging.shadow.l3table[shadow_l3_linear_offset(va)])
3019 & _PAGE_PRESENT) )
3020 // no need to flush anything if there's no SL2...
3021 return 0;
3022 #endif
3024 /* This must still be a copy-from-user because we don't have the shadow
3025 * lock, and the higher-level shadows might disappear under our feet. */
3026 if ( __copy_from_user(&sl2e,
3027 sh_linear_l2_table(v) + shadow_l2_linear_offset(va),
3028 sizeof (sl2e)) != 0 )
3030 perfc_incrc(shadow_invlpg_fault);
3031 return 0;
3034 // If there's nothing shadowed for this particular sl2e, then
3035 // there is no need to do an invlpg, either...
3036 //
3037 if ( !(shadow_l2e_get_flags(sl2e) & _PAGE_PRESENT) )
3038 return 0;
3040 // Check to see if the SL2 is a splintered superpage...
3041 // If so, then we'll need to flush the entire TLB (because that's
3042 // easier than invalidating all of the individual 4K pages).
3043 //
3044 if ( mfn_to_shadow_page(shadow_l2e_get_mfn(sl2e))->type
3045 == SH_type_fl1_shadow )
3047 local_flush_tlb();
3048 return 0;
3051 return 1;
3054 static unsigned long
3055 sh_gva_to_gfn(struct vcpu *v, unsigned long va)
3056 /* Called to translate a guest virtual address to what the *guest*
3057 * pagetables would map it to. */
3059 walk_t gw;
3060 gfn_t gfn;
3062 guest_walk_tables(v, va, &gw, 0);
3063 gfn = guest_walk_to_gfn(&gw);
3064 unmap_walk(v, &gw);
3066 return gfn_x(gfn);
3070 static inline void
3071 sh_update_linear_entries(struct vcpu *v)
3072 /* Sync up all the linear mappings for this vcpu's pagetables */
3074 struct domain *d = v->domain;
3076 /* Linear pagetables in PV guests
3077 * ------------------------------
3079 * Guest linear pagetables, which map the guest pages, are at
3080 * LINEAR_PT_VIRT_START. Shadow linear pagetables, which map the
3081 * shadows, are at SH_LINEAR_PT_VIRT_START. Most of the time these
3082 * are set up at shadow creation time, but (of course!) the PAE case
3083 * is subtler. Normal linear mappings are made by having an entry
3084 * in the top-level table that points to itself (shadow linear) or
3085 * to the guest top-level table (guest linear). For PAE, to set up
3086 * a linear map requires us to copy the four top-level entries into
3087 * level-2 entries. That means that every time we change a PAE l3e,
3088 * we need to reflect the change into the copy.
3090 * Linear pagetables in HVM guests
3091 * -------------------------------
3093 * For HVM guests, the linear pagetables are installed in the monitor
3094 * tables (since we can't put them in the shadow). Shadow linear
3095 * pagetables, which map the shadows, are at SH_LINEAR_PT_VIRT_START,
3096 * and we use the linear pagetable slot at LINEAR_PT_VIRT_START for
3097 * a linear pagetable of the monitor tables themselves. We have
3098 * the same issue of having to re-copy PAE l3 entries whevever we use
3099 * PAE shadows.
3101 * Because HVM guests run on the same monitor tables regardless of the
3102 * shadow tables in use, the linear mapping of the shadow tables has to
3103 * be updated every time v->arch.shadow_table changes.
3104 */
3106 /* Don't try to update the monitor table if it doesn't exist */
3107 if ( shadow_mode_external(d)
3108 && pagetable_get_pfn(v->arch.monitor_table) == 0 )
3109 return;
3111 #if (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS == 4)
3113 /* For PV, one l4e points at the guest l4, one points at the shadow
3114 * l4. No maintenance required.
3115 * For HVM, just need to update the l4e that points to the shadow l4. */
3117 if ( shadow_mode_external(d) )
3119 /* Use the linear map if we can; otherwise make a new mapping */
3120 if ( v == current )
3122 __linear_l4_table[l4_linear_offset(SH_LINEAR_PT_VIRT_START)] =
3123 l4e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3124 __PAGE_HYPERVISOR);
3126 else
3128 l4_pgentry_t *ml4e;
3129 ml4e = sh_map_domain_page(pagetable_get_mfn(v->arch.monitor_table));
3130 ml4e[l4_table_offset(SH_LINEAR_PT_VIRT_START)] =
3131 l4e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3132 __PAGE_HYPERVISOR);
3133 sh_unmap_domain_page(ml4e);
3137 #elif (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS == 3)
3139 /* PV: XXX
3141 * HVM: To give ourselves a linear map of the shadows, we need to
3142 * extend a PAE shadow to 4 levels. We do this by having a monitor
3143 * l3 in slot 0 of the monitor l4 table, and copying the PAE l3
3144 * entries into it. Then, by having the monitor l4e for shadow
3145 * pagetables also point to the monitor l4, we can use it to access
3146 * the shadows.
3147 */
3149 if ( shadow_mode_external(d) )
3151 /* Install copies of the shadow l3es into the monitor l3 table.
3152 * The monitor l3 table is hooked into slot 0 of the monitor
3153 * l4 table, so we use l3 linear indices 0 to 3 */
3154 shadow_l3e_t *sl3e;
3155 l3_pgentry_t *ml3e;
3156 mfn_t l3mfn;
3157 int i;
3159 /* Use linear mappings if we can; otherwise make new mappings */
3160 if ( v == current )
3162 ml3e = __linear_l3_table;
3163 l3mfn = _mfn(l4e_get_pfn(__linear_l4_table[0]));
3165 else
3167 l4_pgentry_t *ml4e;
3168 ml4e = sh_map_domain_page(pagetable_get_mfn(v->arch.monitor_table));
3169 ASSERT(l4e_get_flags(ml4e[0]) & _PAGE_PRESENT);
3170 l3mfn = _mfn(l4e_get_pfn(ml4e[0]));
3171 ml3e = sh_map_domain_page(l3mfn);
3172 sh_unmap_domain_page(ml4e);
3175 /* Shadow l3 tables are made up by sh_update_cr3 */
3176 sl3e = v->arch.paging.shadow.l3table;
3178 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3180 ml3e[i] =
3181 (shadow_l3e_get_flags(sl3e[i]) & _PAGE_PRESENT)
3182 ? l3e_from_pfn(mfn_x(shadow_l3e_get_mfn(sl3e[i])),
3183 __PAGE_HYPERVISOR)
3184 : l3e_empty();
3187 if ( v != current )
3188 sh_unmap_domain_page(ml3e);
3190 else
3191 domain_crash(d); /* XXX */
3193 #elif CONFIG_PAGING_LEVELS == 3
3195 /* PV: need to copy the guest's l3 entries into the guest-linear-map l2
3196 * entries in the shadow, and the shadow's l3 entries into the
3197 * shadow-linear-map l2 entries in the shadow. This is safe to do
3198 * because Xen does not let guests share high-slot l2 tables between l3s,
3199 * so we know we're not treading on anyone's toes.
3201 * HVM: need to copy the shadow's l3 entries into the
3202 * shadow-linear-map l2 entries in the monitor table. This is safe
3203 * because we have one monitor table for each vcpu. The monitor's
3204 * own l3es don't need to be copied because they never change.
3205 * XXX That might change if we start stuffing things into the rest
3206 * of the monitor's virtual address space.
3207 */
3209 l2_pgentry_t *l2e, new_l2e;
3210 shadow_l3e_t *guest_l3e = NULL, *shadow_l3e;
3211 int i;
3212 int unmap_l2e = 0;
3214 #if GUEST_PAGING_LEVELS == 2
3216 /* Shadow l3 tables were built by sh_update_cr3 */
3217 BUG_ON(!shadow_mode_external(d)); /* PV 2-on-3 is unsupported */
3218 shadow_l3e = (shadow_l3e_t *)&v->arch.paging.shadow.l3table;
3220 #else /* GUEST_PAGING_LEVELS == 3 */
3222 shadow_l3e = (shadow_l3e_t *)&v->arch.paging.shadow.l3table;
3223 guest_l3e = (guest_l3e_t *)&v->arch.paging.shadow.gl3e;
3225 #endif /* GUEST_PAGING_LEVELS */
3227 /* Choose where to write the entries, using linear maps if possible */
3228 if ( shadow_mode_external(d) )
3230 if ( v == current )
3232 /* From the monitor tables, it's safe to use linear maps
3233 * to update monitor l2s */
3234 l2e = __linear_l2_table + (3 * L2_PAGETABLE_ENTRIES);
3236 else
3238 /* Map the monitor table's high l2 */
3239 l3_pgentry_t *l3e;
3240 l3e = sh_map_domain_page(
3241 pagetable_get_mfn(v->arch.monitor_table));
3242 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
3243 l2e = sh_map_domain_page(_mfn(l3e_get_pfn(l3e[3])));
3244 unmap_l2e = 1;
3245 sh_unmap_domain_page(l3e);
3248 else
3250 /* Map the shadow table's high l2 */
3251 ASSERT(shadow_l3e_get_flags(shadow_l3e[3]) & _PAGE_PRESENT);
3252 l2e = sh_map_domain_page(shadow_l3e_get_mfn(shadow_l3e[3]));
3253 unmap_l2e = 1;
3256 /* Write linear mapping of guest (only in PV, and only when
3257 * not translated). */
3258 if ( !shadow_mode_translate(d) )
3260 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3262 new_l2e =
3263 ((shadow_l3e_get_flags(guest_l3e[i]) & _PAGE_PRESENT)
3264 ? l2e_from_pfn(mfn_x(shadow_l3e_get_mfn(guest_l3e[i])),
3265 __PAGE_HYPERVISOR)
3266 : l2e_empty());
3267 safe_write_entry(
3268 &l2e[l2_table_offset(LINEAR_PT_VIRT_START) + i],
3269 &new_l2e);
3273 /* Write linear mapping of shadow. */
3274 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3276 new_l2e = (shadow_l3e_get_flags(shadow_l3e[i]) & _PAGE_PRESENT)
3277 ? l2e_from_pfn(mfn_x(shadow_l3e_get_mfn(shadow_l3e[i])),
3278 __PAGE_HYPERVISOR)
3279 : l2e_empty();
3280 safe_write_entry(
3281 &l2e[l2_table_offset(SH_LINEAR_PT_VIRT_START) + i],
3282 &new_l2e);
3285 if ( unmap_l2e )
3286 sh_unmap_domain_page(l2e);
3289 #elif CONFIG_PAGING_LEVELS == 2
3291 /* For PV, one l2e points at the guest l2, one points at the shadow
3292 * l2. No maintenance required.
3293 * For HVM, just need to update the l2e that points to the shadow l2. */
3295 if ( shadow_mode_external(d) )
3297 /* Use the linear map if we can; otherwise make a new mapping */
3298 if ( v == current )
3300 __linear_l2_table[l2_linear_offset(SH_LINEAR_PT_VIRT_START)] =
3301 l2e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3302 __PAGE_HYPERVISOR);
3304 else
3306 l2_pgentry_t *ml2e;
3307 ml2e = sh_map_domain_page(pagetable_get_mfn(v->arch.monitor_table));
3308 ml2e[l2_table_offset(SH_LINEAR_PT_VIRT_START)] =
3309 l2e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3310 __PAGE_HYPERVISOR);
3311 sh_unmap_domain_page(ml2e);
3315 #else
3316 #error this should not happen
3317 #endif
3321 /* Removes vcpu->arch.paging.shadow.guest_vtable and vcpu->arch.shadow_table[].
3322 * Does all appropriate management/bookkeeping/refcounting/etc...
3323 */
3324 static void
3325 sh_detach_old_tables(struct vcpu *v)
3327 mfn_t smfn;
3328 int i = 0;
3330 ////
3331 //// vcpu->arch.paging.shadow.guest_vtable
3332 ////
3334 #if GUEST_PAGING_LEVELS == 3
3335 /* PAE guests don't have a mapping of the guest top-level table */
3336 ASSERT(v->arch.paging.shadow.guest_vtable == NULL);
3337 #else
3338 if ( v->arch.paging.shadow.guest_vtable )
3340 struct domain *d = v->domain;
3341 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
3342 sh_unmap_domain_page_global(v->arch.paging.shadow.guest_vtable);
3343 v->arch.paging.shadow.guest_vtable = NULL;
3345 #endif
3348 ////
3349 //// vcpu->arch.shadow_table[]
3350 ////
3352 #if GUEST_PAGING_LEVELS == 3
3353 /* PAE guests have four shadow_table entries */
3354 for ( i = 0 ; i < 4 ; i++ )
3355 #endif
3357 smfn = pagetable_get_mfn(v->arch.shadow_table[i]);
3358 if ( mfn_x(smfn) )
3359 sh_put_ref(v, smfn, 0);
3360 v->arch.shadow_table[i] = pagetable_null();
3364 /* Set up the top-level shadow and install it in slot 'slot' of shadow_table */
3365 static void
3366 sh_set_toplevel_shadow(struct vcpu *v,
3367 int slot,
3368 mfn_t gmfn,
3369 unsigned int root_type)
3371 mfn_t smfn;
3372 pagetable_t old_entry, new_entry;
3374 struct domain *d = v->domain;
3376 /* Remember the old contents of this slot */
3377 old_entry = v->arch.shadow_table[slot];
3379 /* Now figure out the new contents: is this a valid guest MFN? */
3380 if ( !mfn_valid(gmfn) )
3382 new_entry = pagetable_null();
3383 goto install_new_entry;
3386 /* Guest mfn is valid: shadow it and install the shadow */
3387 smfn = get_shadow_status(v, gmfn, root_type);
3388 if ( !mfn_valid(smfn) )
3390 /* Make sure there's enough free shadow memory. */
3391 shadow_prealloc(d, SHADOW_MAX_ORDER);
3392 /* Shadow the page. */
3393 smfn = sh_make_shadow(v, gmfn, root_type);
3395 ASSERT(mfn_valid(smfn));
3397 #if SHADOW_OPTIMIZATIONS & SHOPT_EARLY_UNSHADOW
3398 /* Once again OK to unhook entries from this table if we see fork/exit */
3399 #if CONFIG_PAGING_LEVELS == 4
3400 if ( IS_COMPAT(d) )
3401 ASSERT(!sh_mfn_is_a_page_table(gmfn));
3402 else
3403 #endif
3404 ASSERT(sh_mfn_is_a_page_table(gmfn));
3405 mfn_to_page(gmfn)->shadow_flags &= ~SHF_unhooked_mappings;
3406 #endif
3408 /* Pin the shadow and put it (back) on the list of top-level shadows */
3409 if ( sh_pin(v, smfn) == 0 )
3411 SHADOW_ERROR("can't pin %#lx as toplevel shadow\n", mfn_x(smfn));
3412 domain_crash(v->domain);
3415 /* Take a ref to this page: it will be released in sh_detach_old_tables()
3416 * or the next call to set_toplevel_shadow() */
3417 if ( !sh_get_ref(v, smfn, 0) )
3419 SHADOW_ERROR("can't install %#lx as toplevel shadow\n", mfn_x(smfn));
3420 domain_crash(v->domain);
3423 new_entry = pagetable_from_mfn(smfn);
3425 install_new_entry:
3426 /* Done. Install it */
3427 SHADOW_PRINTK("%u/%u [%u] gmfn %#"PRI_mfn" smfn %#"PRI_mfn"\n",
3428 GUEST_PAGING_LEVELS, SHADOW_PAGING_LEVELS, slot,
3429 mfn_x(gmfn), mfn_x(pagetable_get_mfn(new_entry)));
3430 v->arch.shadow_table[slot] = new_entry;
3432 /* Decrement the refcount of the old contents of this slot */
3433 if ( !pagetable_is_null(old_entry) )
3434 sh_put_ref(v, pagetable_get_mfn(old_entry), 0);
3438 static void
3439 sh_update_cr3(struct vcpu *v, int do_locking)
3440 /* Updates vcpu->arch.cr3 after the guest has changed CR3.
3441 * Paravirtual guests should set v->arch.guest_table (and guest_table_user,
3442 * if appropriate).
3443 * HVM guests should also make sure hvm_get_guest_cntl_reg(v, 3) works;
3444 * this function will call hvm_update_guest_cr3() to tell them where the
3445 * shadow tables are.
3446 * If do_locking != 0, assume we are being called from outside the
3447 * shadow code, and must take and release the shadow lock; otherwise
3448 * that is the caller's responsibility.
3449 */
3451 struct domain *d = v->domain;
3452 mfn_t gmfn;
3453 #if GUEST_PAGING_LEVELS == 3
3454 guest_l3e_t *gl3e;
3455 u32 guest_idx=0;
3456 int i;
3457 #endif
3459 /* Don't do anything on an uninitialised vcpu */
3460 if ( !is_hvm_domain(d) && !test_bit(_VCPUF_initialised, &v->vcpu_flags) )
3462 ASSERT(v->arch.cr3 == 0);
3463 return;
3466 if ( do_locking ) shadow_lock(v->domain);
3468 ASSERT(shadow_locked_by_me(v->domain));
3469 ASSERT(v->arch.paging.mode);
3471 ////
3472 //// vcpu->arch.guest_table is already set
3473 ////
3475 #ifndef NDEBUG
3476 /* Double-check that the HVM code has sent us a sane guest_table */
3477 if ( is_hvm_domain(d) )
3479 gfn_t gfn;
3481 ASSERT(shadow_mode_external(d));
3483 // Is paging enabled on this vcpu?
3484 if ( paging_vcpu_mode_translate(v) )
3486 gfn = _gfn(paddr_to_pfn(hvm_get_guest_ctrl_reg(v, 3)));
3487 gmfn = vcpu_gfn_to_mfn(v, gfn);
3488 ASSERT(mfn_valid(gmfn));
3489 ASSERT(pagetable_get_pfn(v->arch.guest_table) == mfn_x(gmfn));
3491 else
3493 /* Paging disabled: guest_table points at (part of) p2m */
3494 #if SHADOW_PAGING_LEVELS != 3 /* in 3-on-4, guest-table is in slot 0 of p2m */
3495 /* For everything else, they sould be the same */
3496 ASSERT(v->arch.guest_table.pfn == d->arch.phys_table.pfn);
3497 #endif
3500 #endif
3502 SHADOW_PRINTK("d=%u v=%u guest_table=%05lx\n",
3503 d->domain_id, v->vcpu_id,
3504 (unsigned long)pagetable_get_pfn(v->arch.guest_table));
3506 #if GUEST_PAGING_LEVELS == 4
3507 if ( !(v->arch.flags & TF_kernel_mode) && !IS_COMPAT(v->domain) )
3508 gmfn = pagetable_get_mfn(v->arch.guest_table_user);
3509 else
3510 #endif
3511 gmfn = pagetable_get_mfn(v->arch.guest_table);
3514 ////
3515 //// vcpu->arch.paging.shadow.guest_vtable
3516 ////
3517 #if GUEST_PAGING_LEVELS == 4
3518 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
3520 if ( v->arch.paging.shadow.guest_vtable )
3521 sh_unmap_domain_page_global(v->arch.paging.shadow.guest_vtable);
3522 v->arch.paging.shadow.guest_vtable = sh_map_domain_page_global(gmfn);
3524 else
3525 v->arch.paging.shadow.guest_vtable = __linear_l4_table;
3526 #elif GUEST_PAGING_LEVELS == 3
3527 /* On PAE guests we don't use a mapping of the guest's own top-level
3528 * table. We cache the current state of that table and shadow that,
3529 * until the next CR3 write makes us refresh our cache. */
3530 ASSERT(v->arch.paging.shadow.guest_vtable == NULL);
3532 if ( shadow_mode_external(d) && paging_vcpu_mode_translate(v) )
3533 /* Paging enabled: find where in the page the l3 table is */
3534 guest_idx = guest_index((void *)hvm_get_guest_ctrl_reg(v, 3));
3535 else
3536 /* Paging disabled or PV: l3 is at the start of a page */
3537 guest_idx = 0;
3539 // Ignore the low 2 bits of guest_idx -- they are really just
3540 // cache control.
3541 guest_idx &= ~3;
3543 gl3e = ((guest_l3e_t *)sh_map_domain_page(gmfn)) + guest_idx;
3544 for ( i = 0; i < 4 ; i++ )
3545 v->arch.paging.shadow.gl3e[i] = gl3e[i];
3546 sh_unmap_domain_page(gl3e);
3547 #elif GUEST_PAGING_LEVELS == 2
3548 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
3550 if ( v->arch.paging.shadow.guest_vtable )
3551 sh_unmap_domain_page_global(v->arch.paging.shadow.guest_vtable);
3552 v->arch.paging.shadow.guest_vtable = sh_map_domain_page_global(gmfn);
3554 else
3555 v->arch.paging.shadow.guest_vtable = __linear_l2_table;
3556 #else
3557 #error this should never happen
3558 #endif
3560 #if 0
3561 printk("%s %s %d gmfn=%05lx shadow.guest_vtable=%p\n",
3562 __func__, __FILE__, __LINE__, gmfn, v->arch.paging.shadow.guest_vtable);
3563 #endif
3565 ////
3566 //// vcpu->arch.shadow_table[]
3567 ////
3569 /* We revoke write access to the new guest toplevel page(s) before we
3570 * replace the old shadow pagetable(s), so that we can safely use the
3571 * (old) shadow linear maps in the writeable mapping heuristics. */
3572 #if GUEST_PAGING_LEVELS == 2
3573 if ( sh_remove_write_access(v, gmfn, 2, 0) != 0 )
3574 flush_tlb_mask(v->domain->domain_dirty_cpumask);
3575 sh_set_toplevel_shadow(v, 0, gmfn, SH_type_l2_shadow);
3576 #elif GUEST_PAGING_LEVELS == 3
3577 /* PAE guests have four shadow_table entries, based on the
3578 * current values of the guest's four l3es. */
3580 int flush = 0;
3581 gfn_t gl2gfn;
3582 mfn_t gl2mfn;
3583 guest_l3e_t *gl3e = (guest_l3e_t*)&v->arch.paging.shadow.gl3e;
3584 /* First, make all four entries read-only. */
3585 for ( i = 0; i < 4; i++ )
3587 if ( guest_l3e_get_flags(gl3e[i]) & _PAGE_PRESENT )
3589 gl2gfn = guest_l3e_get_gfn(gl3e[i]);
3590 gl2mfn = vcpu_gfn_to_mfn(v, gl2gfn);
3591 flush |= sh_remove_write_access(v, gl2mfn, 2, 0);
3594 if ( flush )
3595 flush_tlb_mask(v->domain->domain_dirty_cpumask);
3596 /* Now install the new shadows. */
3597 for ( i = 0; i < 4; i++ )
3599 if ( guest_l3e_get_flags(gl3e[i]) & _PAGE_PRESENT )
3601 gl2gfn = guest_l3e_get_gfn(gl3e[i]);
3602 gl2mfn = vcpu_gfn_to_mfn(v, gl2gfn);
3603 sh_set_toplevel_shadow(v, i, gl2mfn, (i == 3)
3604 ? SH_type_l2h_shadow
3605 : SH_type_l2_shadow);
3607 else
3608 /* The guest is not present: clear out the shadow. */
3609 sh_set_toplevel_shadow(v, i, _mfn(INVALID_MFN), 0);
3612 #elif GUEST_PAGING_LEVELS == 4
3613 if ( sh_remove_write_access(v, gmfn, 4, 0) != 0 )
3614 flush_tlb_mask(v->domain->domain_dirty_cpumask);
3615 sh_set_toplevel_shadow(v, 0, gmfn, SH_type_l4_shadow);
3616 #else
3617 #error This should never happen
3618 #endif
3620 #if (CONFIG_PAGING_LEVELS == 3) && (GUEST_PAGING_LEVELS == 3)
3621 #endif
3623 ///
3624 /// v->arch.paging.shadow.l3table
3625 ///
3626 #if SHADOW_PAGING_LEVELS == 3
3628 mfn_t smfn;
3629 int i;
3630 for ( i = 0; i < 4; i++ )
3632 #if GUEST_PAGING_LEVELS == 2
3633 /* 2-on-3: make a PAE l3 that points at the four-page l2 */
3634 smfn = _mfn(pagetable_get_pfn(v->arch.shadow_table[0]) + i);
3635 #else
3636 /* 3-on-3: make a PAE l3 that points at the four l2 pages */
3637 smfn = pagetable_get_mfn(v->arch.shadow_table[i]);
3638 #endif
3639 v->arch.paging.shadow.l3table[i] =
3640 (mfn_x(smfn) == 0)
3641 ? shadow_l3e_empty()
3642 : shadow_l3e_from_mfn(smfn, _PAGE_PRESENT);
3645 #endif /* SHADOW_PAGING_LEVELS == 3 */
3648 ///
3649 /// v->arch.cr3
3650 ///
3651 if ( shadow_mode_external(d) )
3653 make_cr3(v, pagetable_get_pfn(v->arch.monitor_table));
3655 else // not shadow_mode_external...
3657 /* We don't support PV except guest == shadow == config levels */
3658 BUG_ON(GUEST_PAGING_LEVELS != SHADOW_PAGING_LEVELS);
3659 #if SHADOW_PAGING_LEVELS == 3
3660 /* 2-on-3 or 3-on-3: Use the PAE shadow l3 table we just fabricated.
3661 * Don't use make_cr3 because (a) we know it's below 4GB, and
3662 * (b) it's not necessarily page-aligned, and make_cr3 takes a pfn */
3663 ASSERT(virt_to_maddr(&v->arch.paging.shadow.l3table) <= 0xffffffe0ULL);
3664 v->arch.cr3 = virt_to_maddr(&v->arch.paging.shadow.l3table);
3665 #else
3666 /* 2-on-2 or 4-on-4: Just use the shadow top-level directly */
3667 make_cr3(v, pagetable_get_pfn(v->arch.shadow_table[0]));
3668 #endif
3672 ///
3673 /// v->arch.hvm_vcpu.hw_cr3
3674 ///
3675 if ( shadow_mode_external(d) )
3677 ASSERT(is_hvm_domain(d));
3678 #if SHADOW_PAGING_LEVELS == 3
3679 /* 2-on-3 or 3-on-3: Use the PAE shadow l3 table we just fabricated */
3680 hvm_update_guest_cr3(v, virt_to_maddr(&v->arch.paging.shadow.l3table));
3681 #else
3682 /* 2-on-2 or 4-on-4: Just use the shadow top-level directly */
3683 hvm_update_guest_cr3(v, pagetable_get_paddr(v->arch.shadow_table[0]));
3684 #endif
3687 /* Fix up the linear pagetable mappings */
3688 sh_update_linear_entries(v);
3690 /* Release the lock, if we took it (otherwise it's the caller's problem) */
3691 if ( do_locking ) shadow_unlock(v->domain);
3695 /**************************************************************************/
3696 /* Functions to revoke guest rights */
3698 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
3699 static int sh_guess_wrmap(struct vcpu *v, unsigned long vaddr, mfn_t gmfn)
3700 /* Look up this vaddr in the current shadow and see if it's a writeable
3701 * mapping of this gmfn. If so, remove it. Returns 1 if it worked. */
3703 shadow_l1e_t sl1e, *sl1p;
3704 shadow_l2e_t *sl2p;
3705 #if SHADOW_PAGING_LEVELS >= 3
3706 shadow_l3e_t *sl3p;
3707 #if SHADOW_PAGING_LEVELS >= 4
3708 shadow_l4e_t *sl4p;
3709 #endif
3710 #endif
3711 mfn_t sl1mfn;
3712 int r;
3714 /* Carefully look in the shadow linear map for the l1e we expect */
3715 #if SHADOW_PAGING_LEVELS >= 4
3716 sl4p = sh_linear_l4_table(v) + shadow_l4_linear_offset(vaddr);
3717 if ( !(shadow_l4e_get_flags(*sl4p) & _PAGE_PRESENT) )
3718 return 0;
3719 sl3p = sh_linear_l3_table(v) + shadow_l3_linear_offset(vaddr);
3720 if ( !(shadow_l3e_get_flags(*sl3p) & _PAGE_PRESENT) )
3721 return 0;
3722 #elif SHADOW_PAGING_LEVELS == 3
3723 sl3p = ((shadow_l3e_t *) v->arch.paging.shadow.l3table)
3724 + shadow_l3_linear_offset(vaddr);
3725 if ( !(shadow_l3e_get_flags(*sl3p) & _PAGE_PRESENT) )
3726 return 0;
3727 #endif
3728 sl2p = sh_linear_l2_table(v) + shadow_l2_linear_offset(vaddr);
3729 if ( !(shadow_l2e_get_flags(*sl2p) & _PAGE_PRESENT) )
3730 return 0;
3731 sl1p = sh_linear_l1_table(v) + shadow_l1_linear_offset(vaddr);
3732 sl1e = *sl1p;
3733 if ( ((shadow_l1e_get_flags(sl1e) & (_PAGE_PRESENT|_PAGE_RW))
3734 != (_PAGE_PRESENT|_PAGE_RW))
3735 || (mfn_x(shadow_l1e_get_mfn(sl1e)) != mfn_x(gmfn)) )
3736 return 0;
3738 /* Found it! Need to remove its write permissions. */
3739 sl1mfn = shadow_l2e_get_mfn(*sl2p);
3740 sl1e = shadow_l1e_remove_flags(sl1e, _PAGE_RW);
3741 r = shadow_set_l1e(v, sl1p, sl1e, sl1mfn);
3742 ASSERT( !(r & SHADOW_SET_ERROR) );
3743 return 1;
3745 #endif
3747 int sh_rm_write_access_from_l1(struct vcpu *v, mfn_t sl1mfn,
3748 mfn_t readonly_mfn)
3749 /* Excises all writeable mappings to readonly_mfn from this l1 shadow table */
3751 shadow_l1e_t *sl1e;
3752 int done = 0;
3753 int flags;
3754 mfn_t base_sl1mfn = sl1mfn; /* Because sl1mfn changes in the foreach */
3756 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done,
3758 flags = shadow_l1e_get_flags(*sl1e);
3759 if ( (flags & _PAGE_PRESENT)
3760 && (flags & _PAGE_RW)
3761 && (mfn_x(shadow_l1e_get_mfn(*sl1e)) == mfn_x(readonly_mfn)) )
3763 shadow_l1e_t ro_sl1e = shadow_l1e_remove_flags(*sl1e, _PAGE_RW);
3764 (void) shadow_set_l1e(v, sl1e, ro_sl1e, sl1mfn);
3765 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
3766 /* Remember the last shadow that we shot a writeable mapping in */
3767 v->arch.paging.shadow.last_writeable_pte_smfn = mfn_x(base_sl1mfn);
3768 #endif
3769 if ( (mfn_to_page(readonly_mfn)->u.inuse.type_info
3770 & PGT_count_mask) == 0 )
3771 /* This breaks us cleanly out of the FOREACH macro */
3772 done = 1;
3774 });
3775 return done;
3779 int sh_rm_mappings_from_l1(struct vcpu *v, mfn_t sl1mfn, mfn_t target_mfn)
3780 /* Excises all mappings to guest frame from this shadow l1 table */
3782 shadow_l1e_t *sl1e;
3783 int done = 0;
3784 int flags;
3786 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done,
3788 flags = shadow_l1e_get_flags(*sl1e);
3789 if ( (flags & _PAGE_PRESENT)
3790 && (mfn_x(shadow_l1e_get_mfn(*sl1e)) == mfn_x(target_mfn)) )
3792 (void) shadow_set_l1e(v, sl1e, shadow_l1e_empty(), sl1mfn);
3793 if ( (mfn_to_page(target_mfn)->count_info & PGC_count_mask) == 0 )
3794 /* This breaks us cleanly out of the FOREACH macro */
3795 done = 1;
3797 });
3798 return done;
3801 /**************************************************************************/
3802 /* Functions to excise all pointers to shadows from higher-level shadows. */
3804 void sh_clear_shadow_entry(struct vcpu *v, void *ep, mfn_t smfn)
3805 /* Blank out a single shadow entry */
3807 switch ( mfn_to_shadow_page(smfn)->type )
3809 case SH_type_l1_shadow:
3810 (void) shadow_set_l1e(v, ep, shadow_l1e_empty(), smfn); break;
3811 case SH_type_l2_shadow:
3812 #if GUEST_PAGING_LEVELS >= 3
3813 case SH_type_l2h_shadow:
3814 #endif
3815 (void) shadow_set_l2e(v, ep, shadow_l2e_empty(), smfn); break;
3816 #if GUEST_PAGING_LEVELS >= 4
3817 case SH_type_l3_shadow:
3818 (void) shadow_set_l3e(v, ep, shadow_l3e_empty(), smfn); break;
3819 case SH_type_l4_shadow:
3820 (void) shadow_set_l4e(v, ep, shadow_l4e_empty(), smfn); break;
3821 #endif
3822 default: BUG(); /* Called with the wrong kind of shadow. */
3826 int sh_remove_l1_shadow(struct vcpu *v, mfn_t sl2mfn, mfn_t sl1mfn)
3827 /* Remove all mappings of this l1 shadow from this l2 shadow */
3829 shadow_l2e_t *sl2e;
3830 int done = 0;
3831 int flags;
3833 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, done, v->domain,
3835 flags = shadow_l2e_get_flags(*sl2e);
3836 if ( (flags & _PAGE_PRESENT)
3837 && (mfn_x(shadow_l2e_get_mfn(*sl2e)) == mfn_x(sl1mfn)) )
3839 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
3840 if ( mfn_to_shadow_page(sl1mfn)->type == 0 )
3841 /* This breaks us cleanly out of the FOREACH macro */
3842 done = 1;
3844 });
3845 return done;
3848 #if GUEST_PAGING_LEVELS >= 4
3849 int sh_remove_l2_shadow(struct vcpu *v, mfn_t sl3mfn, mfn_t sl2mfn)
3850 /* Remove all mappings of this l2 shadow from this l3 shadow */
3852 shadow_l3e_t *sl3e;
3853 int done = 0;
3854 int flags;
3856 SHADOW_FOREACH_L3E(sl3mfn, sl3e, 0, done,
3858 flags = shadow_l3e_get_flags(*sl3e);
3859 if ( (flags & _PAGE_PRESENT)
3860 && (mfn_x(shadow_l3e_get_mfn(*sl3e)) == mfn_x(sl2mfn)) )
3862 (void) shadow_set_l3e(v, sl3e, shadow_l3e_empty(), sl3mfn);
3863 if ( mfn_to_shadow_page(sl2mfn)->type == 0 )
3864 /* This breaks us cleanly out of the FOREACH macro */
3865 done = 1;
3867 });
3868 return done;
3871 int sh_remove_l3_shadow(struct vcpu *v, mfn_t sl4mfn, mfn_t sl3mfn)
3872 /* Remove all mappings of this l3 shadow from this l4 shadow */
3874 shadow_l4e_t *sl4e;
3875 int done = 0;
3876 int flags;
3878 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, done, v->domain,
3880 flags = shadow_l4e_get_flags(*sl4e);
3881 if ( (flags & _PAGE_PRESENT)
3882 && (mfn_x(shadow_l4e_get_mfn(*sl4e)) == mfn_x(sl3mfn)) )
3884 (void) shadow_set_l4e(v, sl4e, shadow_l4e_empty(), sl4mfn);
3885 if ( mfn_to_shadow_page(sl3mfn)->type == 0 )
3886 /* This breaks us cleanly out of the FOREACH macro */
3887 done = 1;
3889 });
3890 return done;
3892 #endif /* 64bit guest */
3894 /**************************************************************************/
3895 /* Handling HVM guest writes to pagetables */
3897 /* Check that the user is allowed to perform this write.
3898 * Returns a mapped pointer to write to, and the mfn it's on,
3899 * or NULL for error. */
3900 static inline void * emulate_map_dest(struct vcpu *v,
3901 unsigned long vaddr,
3902 struct sh_emulate_ctxt *sh_ctxt,
3903 mfn_t *mfnp)
3905 walk_t gw;
3906 u32 flags, errcode;
3907 gfn_t gfn;
3908 mfn_t mfn;
3910 guest_walk_tables(v, vaddr, &gw, 1);
3911 flags = accumulate_guest_flags(v, &gw);
3912 gfn = guest_l1e_get_gfn(gw.eff_l1e);
3913 mfn = vcpu_gfn_to_mfn(v, gfn);
3914 sh_audit_gw(v, &gw);
3915 unmap_walk(v, &gw);
3917 if ( !(flags & _PAGE_PRESENT) )
3919 errcode = 0;
3920 goto page_fault;
3923 if ( !(flags & _PAGE_RW) ||
3924 (!(flags & _PAGE_USER) && ring_3(sh_ctxt->ctxt.regs)) )
3926 errcode = PFEC_page_present;
3927 goto page_fault;
3930 if ( !mfn_valid(mfn) )
3931 return NULL;
3933 *mfnp = mfn;
3934 return sh_map_domain_page(mfn) + (vaddr & ~PAGE_MASK);
3936 page_fault:
3937 errcode |= PFEC_write_access;
3938 if ( is_hvm_vcpu(v) )
3939 hvm_inject_exception(TRAP_page_fault, errcode, vaddr);
3940 else
3941 propagate_page_fault(vaddr, errcode);
3942 return NULL;
3945 static int safe_not_to_verify_write(mfn_t gmfn, void *dst, void *src,
3946 int bytes)
3948 #if (SHADOW_OPTIMIZATIONS & SHOPT_SKIP_VERIFY)
3949 struct page_info *pg = mfn_to_page(gmfn);
3950 if ( !(pg->shadow_flags & SHF_32)
3951 && ((unsigned long)dst & 7) == 0 )
3953 /* Not shadowed 32-bit: aligned 64-bit writes that leave the
3954 * present bit unset are safe to ignore. */
3955 if ( (*(u64*)src & _PAGE_PRESENT) == 0
3956 && (*(u64*)dst & _PAGE_PRESENT) == 0 )
3957 return 1;
3959 else if ( !(pg->shadow_flags & (SHF_PAE|SHF_64))
3960 && ((unsigned long)dst & 3) == 0 )
3962 /* Not shadowed PAE/64-bit: aligned 32-bit writes that leave the
3963 * present bit unset are safe to ignore. */
3964 if ( (*(u32*)src & _PAGE_PRESENT) == 0
3965 && (*(u32*)dst & _PAGE_PRESENT) == 0 )
3966 return 1;
3968 #endif
3969 return 0;
3973 int
3974 sh_x86_emulate_write(struct vcpu *v, unsigned long vaddr, void *src,
3975 u32 bytes, struct sh_emulate_ctxt *sh_ctxt)
3977 mfn_t mfn;
3978 void *addr;
3979 int skip;
3981 if ( vaddr & (bytes-1) )
3982 return X86EMUL_UNHANDLEABLE;
3984 ASSERT(shadow_locked_by_me(v->domain));
3985 ASSERT(((vaddr & ~PAGE_MASK) + bytes) <= PAGE_SIZE);
3987 if ( (addr = emulate_map_dest(v, vaddr, sh_ctxt, &mfn)) == NULL )
3988 return X86EMUL_EXCEPTION;
3990 skip = safe_not_to_verify_write(mfn, addr, src, bytes);
3991 memcpy(addr, src, bytes);
3992 if ( !skip ) sh_validate_guest_pt_write(v, mfn, addr, bytes);
3994 /* If we are writing zeros to this page, might want to unshadow */
3995 if ( likely(bytes >= 4) && (*(u32 *)addr == 0) && is_lo_pte(vaddr) )
3996 check_for_early_unshadow(v, mfn);
3998 sh_unmap_domain_page(addr);
3999 shadow_audit_tables(v);
4000 return X86EMUL_OKAY;
4003 int
4004 sh_x86_emulate_cmpxchg(struct vcpu *v, unsigned long vaddr,
4005 unsigned long old, unsigned long new,
4006 unsigned int bytes, struct sh_emulate_ctxt *sh_ctxt)
4008 mfn_t mfn;
4009 void *addr;
4010 unsigned long prev;
4011 int rv = X86EMUL_OKAY, skip;
4013 ASSERT(shadow_locked_by_me(v->domain));
4014 ASSERT(bytes <= sizeof(unsigned long));
4016 if ( vaddr & (bytes-1) )
4017 return X86EMUL_UNHANDLEABLE;
4019 if ( (addr = emulate_map_dest(v, vaddr, sh_ctxt, &mfn)) == NULL )
4020 return X86EMUL_EXCEPTION;
4022 skip = safe_not_to_verify_write(mfn, &new, &old, bytes);
4024 switch ( bytes )
4026 case 1: prev = cmpxchg(((u8 *)addr), old, new); break;
4027 case 2: prev = cmpxchg(((u16 *)addr), old, new); break;
4028 case 4: prev = cmpxchg(((u32 *)addr), old, new); break;
4029 case 8: prev = cmpxchg(((u64 *)addr), old, new); break;
4030 default:
4031 SHADOW_PRINTK("cmpxchg of size %i is not supported\n", bytes);
4032 prev = ~old;
4035 if ( prev == old )
4037 if ( !skip ) sh_validate_guest_pt_write(v, mfn, addr, bytes);
4039 else
4040 rv = X86EMUL_CMPXCHG_FAILED;
4042 SHADOW_DEBUG(EMULATE, "va %#lx was %#lx expected %#lx"
4043 " wanted %#lx now %#lx bytes %u\n",
4044 vaddr, prev, old, new, *(unsigned long *)addr, bytes);
4046 /* If we are writing zeros to this page, might want to unshadow */
4047 if ( likely(bytes >= 4) && (*(u32 *)addr == 0) && is_lo_pte(vaddr) )
4048 check_for_early_unshadow(v, mfn);
4050 sh_unmap_domain_page(addr);
4051 shadow_audit_tables(v);
4052 return rv;
4055 int
4056 sh_x86_emulate_cmpxchg8b(struct vcpu *v, unsigned long vaddr,
4057 unsigned long old_lo, unsigned long old_hi,
4058 unsigned long new_lo, unsigned long new_hi,
4059 struct sh_emulate_ctxt *sh_ctxt)
4061 mfn_t mfn;
4062 void *addr;
4063 u64 old, new, prev;
4064 int rv = X86EMUL_OKAY, skip;
4066 ASSERT(shadow_locked_by_me(v->domain));
4068 if ( vaddr & 7 )
4069 return X86EMUL_UNHANDLEABLE;
4071 if ( (addr = emulate_map_dest(v, vaddr, sh_ctxt, &mfn)) == NULL )
4072 return X86EMUL_EXCEPTION;
4074 old = (((u64) old_hi) << 32) | (u64) old_lo;
4075 new = (((u64) new_hi) << 32) | (u64) new_lo;
4076 skip = safe_not_to_verify_write(mfn, &new, &old, 8);
4077 prev = cmpxchg(((u64 *)addr), old, new);
4079 if ( prev == old )
4081 if ( !skip ) sh_validate_guest_pt_write(v, mfn, addr, 8);
4083 else
4084 rv = X86EMUL_CMPXCHG_FAILED;
4086 /* If we are writing zeros to this page, might want to unshadow */
4087 if ( *(u32 *)addr == 0 )
4088 check_for_early_unshadow(v, mfn);
4090 sh_unmap_domain_page(addr);
4091 shadow_audit_tables(v);
4092 return rv;
4096 /**************************************************************************/
4097 /* Audit tools */
4099 #if SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES
4101 #define AUDIT_FAIL(_level, _fmt, _a...) do { \
4102 printk("Shadow %u-on-%u audit failed at level %i, index %i\n" \
4103 "gl" #_level "mfn = %" PRI_mfn \
4104 " sl" #_level "mfn = %" PRI_mfn \
4105 " &gl" #_level "e = %p &sl" #_level "e = %p" \
4106 " gl" #_level "e = %" SH_PRI_gpte \
4107 " sl" #_level "e = %" SH_PRI_pte "\nError: " _fmt "\n", \
4108 GUEST_PAGING_LEVELS, SHADOW_PAGING_LEVELS, \
4109 _level, guest_index(gl ## _level ## e), \
4110 mfn_x(gl ## _level ## mfn), mfn_x(sl ## _level ## mfn), \
4111 gl ## _level ## e, sl ## _level ## e, \
4112 gl ## _level ## e->l ## _level, sl ## _level ## e->l ## _level, \
4113 ##_a); \
4114 BUG(); \
4115 done = 1; \
4116 } while (0)
4119 static char * sh_audit_flags(struct vcpu *v, int level,
4120 int gflags, int sflags)
4121 /* Common code for auditing flag bits */
4123 if ( (sflags & _PAGE_PRESENT) && !(gflags & _PAGE_PRESENT) )
4124 return "shadow is present but guest is not present";
4125 if ( (sflags & _PAGE_GLOBAL) && !is_hvm_vcpu(v) )
4126 return "global bit set in PV shadow";
4127 if ( level == 2 && (sflags & _PAGE_PSE) )
4128 return "PS bit set in shadow";
4129 #if SHADOW_PAGING_LEVELS == 3
4130 if ( level == 3 ) return NULL; /* All the other bits are blank in PAEl3 */
4131 #endif
4132 if ( (sflags & _PAGE_PRESENT) && !(gflags & _PAGE_ACCESSED) )
4133 return "accessed bit not propagated";
4134 if ( (level == 1 || (level == 2 && (gflags & _PAGE_PSE)))
4135 && ((sflags & _PAGE_RW) && !(gflags & _PAGE_DIRTY)) )
4136 return "dirty bit not propagated";
4137 if ( (sflags & _PAGE_USER) != (gflags & _PAGE_USER) )
4138 return "user/supervisor bit does not match";
4139 if ( (sflags & _PAGE_NX_BIT) != (gflags & _PAGE_NX_BIT) )
4140 return "NX bit does not match";
4141 if ( (sflags & _PAGE_RW) && !(gflags & _PAGE_RW) )
4142 return "shadow grants write access but guest does not";
4143 return NULL;
4146 static inline mfn_t
4147 audit_gfn_to_mfn(struct vcpu *v, gfn_t gfn, mfn_t gmfn)
4148 /* Convert this gfn to an mfn in the manner appropriate for the
4149 * guest pagetable it's used in (gmfn) */
4151 if ( !shadow_mode_translate(v->domain) )
4152 return _mfn(gfn_x(gfn));
4154 if ( (mfn_to_page(gmfn)->u.inuse.type_info & PGT_type_mask)
4155 != PGT_writable_page )
4156 return _mfn(gfn_x(gfn)); /* This is a paging-disabled shadow */
4157 else
4158 return gfn_to_mfn(v->domain, gfn_x(gfn));
4162 int sh_audit_l1_table(struct vcpu *v, mfn_t sl1mfn, mfn_t x)
4164 guest_l1e_t *gl1e, *gp;
4165 shadow_l1e_t *sl1e;
4166 mfn_t mfn, gmfn, gl1mfn;
4167 gfn_t gfn;
4168 char *s;
4169 int done = 0;
4171 /* Follow the backpointer */
4172 gl1mfn = _mfn(mfn_to_shadow_page(sl1mfn)->backpointer);
4173 gl1e = gp = sh_map_domain_page(gl1mfn);
4174 SHADOW_FOREACH_L1E(sl1mfn, sl1e, &gl1e, done, {
4176 if ( sh_l1e_is_magic(*sl1e) )
4178 #if (SHADOW_OPTIMIZATIONS & SHOPT_FAST_FAULT_PATH) && SHADOW_PAGING_LEVELS > 2
4179 if ( sh_l1e_is_gnp(*sl1e) )
4181 if ( guest_l1e_get_flags(*gl1e) & _PAGE_PRESENT )
4182 AUDIT_FAIL(1, "shadow is GNP magic but guest is present");
4184 else
4186 ASSERT(sh_l1e_is_mmio(*sl1e));
4187 gfn = sh_l1e_mmio_get_gfn(*sl1e);
4188 if ( gfn_x(gfn) != gfn_x(guest_l1e_get_gfn(*gl1e)) )
4189 AUDIT_FAIL(1, "shadow MMIO gfn is %" SH_PRI_gfn
4190 " but guest gfn is %" SH_PRI_gfn,
4191 gfn_x(gfn),
4192 gfn_x(guest_l1e_get_gfn(*gl1e)));
4194 #endif
4196 else
4198 s = sh_audit_flags(v, 1, guest_l1e_get_flags(*gl1e),
4199 shadow_l1e_get_flags(*sl1e));
4200 if ( s ) AUDIT_FAIL(1, "%s", s);
4202 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4204 gfn = guest_l1e_get_gfn(*gl1e);
4205 mfn = shadow_l1e_get_mfn(*sl1e);
4206 gmfn = audit_gfn_to_mfn(v, gfn, gl1mfn);
4207 if ( mfn_x(gmfn) != mfn_x(mfn) )
4208 AUDIT_FAIL(1, "bad translation: gfn %" SH_PRI_gfn
4209 " --> %" PRI_mfn " != mfn %" PRI_mfn,
4210 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
4213 });
4214 sh_unmap_domain_page(gp);
4215 return done;
4218 int sh_audit_fl1_table(struct vcpu *v, mfn_t sl1mfn, mfn_t x)
4220 guest_l1e_t *gl1e, e;
4221 shadow_l1e_t *sl1e;
4222 mfn_t gl1mfn = _mfn(INVALID_MFN);
4223 int f;
4224 int done = 0;
4226 /* fl1 has no useful backpointer: all we can check are flags */
4227 e = guest_l1e_from_gfn(_gfn(0), 0); gl1e = &e; /* Needed for macro */
4228 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done, {
4229 f = shadow_l1e_get_flags(*sl1e);
4230 f &= ~(_PAGE_AVAIL0|_PAGE_AVAIL1|_PAGE_AVAIL2);
4231 if ( !(f == 0
4232 || f == (_PAGE_PRESENT|_PAGE_USER|_PAGE_RW|
4233 _PAGE_ACCESSED|_PAGE_DIRTY)
4234 || f == (_PAGE_PRESENT|_PAGE_USER|_PAGE_ACCESSED|_PAGE_DIRTY)
4235 || sh_l1e_is_magic(*sl1e)) )
4236 AUDIT_FAIL(1, "fl1e has bad flags");
4237 });
4238 return 0;
4241 int sh_audit_l2_table(struct vcpu *v, mfn_t sl2mfn, mfn_t x)
4243 guest_l2e_t *gl2e, *gp;
4244 shadow_l2e_t *sl2e;
4245 mfn_t mfn, gmfn, gl2mfn;
4246 gfn_t gfn;
4247 char *s;
4248 int done = 0;
4250 /* Follow the backpointer */
4251 gl2mfn = _mfn(mfn_to_shadow_page(sl2mfn)->backpointer);
4252 gl2e = gp = sh_map_domain_page(gl2mfn);
4253 SHADOW_FOREACH_L2E(sl2mfn, sl2e, &gl2e, done, v->domain, {
4255 s = sh_audit_flags(v, 2, guest_l2e_get_flags(*gl2e),
4256 shadow_l2e_get_flags(*sl2e));
4257 if ( s ) AUDIT_FAIL(2, "%s", s);
4259 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4261 gfn = guest_l2e_get_gfn(*gl2e);
4262 mfn = shadow_l2e_get_mfn(*sl2e);
4263 gmfn = (guest_l2e_get_flags(*gl2e) & _PAGE_PSE)
4264 ? get_fl1_shadow_status(v, gfn)
4265 : get_shadow_status(v, audit_gfn_to_mfn(v, gfn, gl2mfn),
4266 SH_type_l1_shadow);
4267 if ( mfn_x(gmfn) != mfn_x(mfn) )
4268 AUDIT_FAIL(2, "bad translation: gfn %" SH_PRI_gfn
4269 " (--> %" PRI_mfn ")"
4270 " --> %" PRI_mfn " != mfn %" PRI_mfn,
4271 gfn_x(gfn),
4272 (guest_l2e_get_flags(*gl2e) & _PAGE_PSE) ? 0
4273 : mfn_x(audit_gfn_to_mfn(v, gfn, gl2mfn)),
4274 mfn_x(gmfn), mfn_x(mfn));
4276 });
4277 sh_unmap_domain_page(gp);
4278 return 0;
4281 #if GUEST_PAGING_LEVELS >= 4
4282 int sh_audit_l3_table(struct vcpu *v, mfn_t sl3mfn, mfn_t x)
4284 guest_l3e_t *gl3e, *gp;
4285 shadow_l3e_t *sl3e;
4286 mfn_t mfn, gmfn, gl3mfn;
4287 gfn_t gfn;
4288 char *s;
4289 int done = 0;
4291 /* Follow the backpointer */
4292 gl3mfn = _mfn(mfn_to_shadow_page(sl3mfn)->backpointer);
4293 gl3e = gp = sh_map_domain_page(gl3mfn);
4294 SHADOW_FOREACH_L3E(sl3mfn, sl3e, &gl3e, done, {
4296 s = sh_audit_flags(v, 3, guest_l3e_get_flags(*gl3e),
4297 shadow_l3e_get_flags(*sl3e));
4298 if ( s ) AUDIT_FAIL(3, "%s", s);
4300 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4302 gfn = guest_l3e_get_gfn(*gl3e);
4303 mfn = shadow_l3e_get_mfn(*sl3e);
4304 gmfn = get_shadow_status(v, audit_gfn_to_mfn(v, gfn, gl3mfn),
4305 ((GUEST_PAGING_LEVELS == 3 ||
4306 IS_COMPAT(v->domain))
4307 && !shadow_mode_external(v->domain)
4308 && (guest_index(gl3e) % 4) == 3)
4309 ? SH_type_l2h_shadow
4310 : SH_type_l2_shadow);
4311 if ( mfn_x(gmfn) != mfn_x(mfn) )
4312 AUDIT_FAIL(3, "bad translation: gfn %" SH_PRI_gfn
4313 " --> %" PRI_mfn " != mfn %" PRI_mfn,
4314 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
4316 });
4317 sh_unmap_domain_page(gp);
4318 return 0;
4321 int sh_audit_l4_table(struct vcpu *v, mfn_t sl4mfn, mfn_t x)
4323 guest_l4e_t *gl4e, *gp;
4324 shadow_l4e_t *sl4e;
4325 mfn_t mfn, gmfn, gl4mfn;
4326 gfn_t gfn;
4327 char *s;
4328 int done = 0;
4330 /* Follow the backpointer */
4331 gl4mfn = _mfn(mfn_to_shadow_page(sl4mfn)->backpointer);
4332 gl4e = gp = sh_map_domain_page(gl4mfn);
4333 SHADOW_FOREACH_L4E(sl4mfn, sl4e, &gl4e, done, v->domain,
4335 s = sh_audit_flags(v, 4, guest_l4e_get_flags(*gl4e),
4336 shadow_l4e_get_flags(*sl4e));
4337 if ( s ) AUDIT_FAIL(4, "%s", s);
4339 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4341 gfn = guest_l4e_get_gfn(*gl4e);
4342 mfn = shadow_l4e_get_mfn(*sl4e);
4343 gmfn = get_shadow_status(v, audit_gfn_to_mfn(v, gfn, gl4mfn),
4344 SH_type_l3_shadow);
4345 if ( mfn_x(gmfn) != mfn_x(mfn) )
4346 AUDIT_FAIL(4, "bad translation: gfn %" SH_PRI_gfn
4347 " --> %" PRI_mfn " != mfn %" PRI_mfn,
4348 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
4350 });
4351 sh_unmap_domain_page(gp);
4352 return 0;
4354 #endif /* GUEST_PAGING_LEVELS >= 4 */
4357 #undef AUDIT_FAIL
4359 #endif /* Audit code */
4361 /**************************************************************************/
4362 /* Entry points into this mode of the shadow code.
4363 * This will all be mangled by the preprocessor to uniquify everything. */
4364 struct paging_mode sh_paging_mode = {
4365 .page_fault = sh_page_fault,
4366 .invlpg = sh_invlpg,
4367 .gva_to_gfn = sh_gva_to_gfn,
4368 .update_cr3 = sh_update_cr3,
4369 .update_paging_modes = shadow_update_paging_modes,
4370 .write_p2m_entry = shadow_write_p2m_entry,
4371 .write_guest_entry = shadow_write_guest_entry,
4372 .cmpxchg_guest_entry = shadow_cmpxchg_guest_entry,
4373 .guest_map_l1e = sh_guest_map_l1e,
4374 .guest_get_eff_l1e = sh_guest_get_eff_l1e,
4375 .guest_levels = GUEST_PAGING_LEVELS,
4376 .shadow.detach_old_tables = sh_detach_old_tables,
4377 .shadow.x86_emulate_write = sh_x86_emulate_write,
4378 .shadow.x86_emulate_cmpxchg = sh_x86_emulate_cmpxchg,
4379 .shadow.x86_emulate_cmpxchg8b = sh_x86_emulate_cmpxchg8b,
4380 .shadow.make_monitor_table = sh_make_monitor_table,
4381 .shadow.destroy_monitor_table = sh_destroy_monitor_table,
4382 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
4383 .shadow.guess_wrmap = sh_guess_wrmap,
4384 #endif
4385 .shadow.shadow_levels = SHADOW_PAGING_LEVELS,
4386 };
4388 /*
4389 * Local variables:
4390 * mode: C
4391 * c-set-style: "BSD"
4392 * c-basic-offset: 4
4393 * indent-tabs-mode: nil
4394 * End:
4395 */