ia64/xen-unstable

view xen/arch/x86/mm/shadow/multi.c @ 13866:584ab4fd1ad5

[XEN] Remove bogus assertion in shadow emulation path
Signed-off-by: Tim Deegan <Tim.Deegan@xensource.com>
author Tim Deegan <Tim.Deegan@xensource.com>
date Wed Feb 07 12:58:07 2007 +0000 (2007-02-07)
parents f78cca1e57a2
children 6daa91dc9247
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 res = get_page(mfn_to_page(gmfn), d);
166 ASSERT(res == 1);
168 shadow_hash_insert(v, mfn_x(gmfn), shadow_type, smfn);
169 }
171 static inline void
172 delete_fl1_shadow_status(struct vcpu *v, gfn_t gfn, mfn_t smfn)
173 /* Remove a shadow from the hash table */
174 {
175 SHADOW_PRINTK("gfn=%"SH_PRI_gfn", type=%08x, smfn=%05lx\n",
176 gfn_x(gfn), SH_type_fl1_shadow, mfn_x(smfn));
177 shadow_hash_delete(v, gfn_x(gfn), SH_type_fl1_shadow, smfn);
178 }
180 static inline void
181 delete_shadow_status(struct vcpu *v, mfn_t gmfn, u32 shadow_type, mfn_t smfn)
182 /* Remove a shadow from the hash table */
183 {
184 SHADOW_PRINTK("d=%d, v=%d, gmfn=%05lx, type=%08x, smfn=%05lx\n",
185 v->domain->domain_id, v->vcpu_id,
186 mfn_x(gmfn), shadow_type, mfn_x(smfn));
187 shadow_hash_delete(v, mfn_x(gmfn), shadow_type, smfn);
188 put_page(mfn_to_page(gmfn));
189 }
191 /**************************************************************************/
192 /* CPU feature support querying */
194 static inline int
195 guest_supports_superpages(struct vcpu *v)
196 {
197 /* The _PAGE_PSE bit must be honoured in HVM guests, whenever
198 * CR4.PSE is set or the guest is in PAE or long mode */
199 return (is_hvm_vcpu(v) && (GUEST_PAGING_LEVELS != 2
200 || (hvm_get_guest_ctrl_reg(v, 4) & X86_CR4_PSE)));
201 }
203 static inline int
204 guest_supports_nx(struct vcpu *v)
205 {
206 if ( !is_hvm_vcpu(v) )
207 return cpu_has_nx;
209 // XXX - fix this!
210 return 1;
211 }
214 /**************************************************************************/
215 /* Functions for walking the guest page tables */
218 /* Walk the guest pagetables, filling the walk_t with what we see.
219 * Takes an uninitialised walk_t. The caller must call unmap_walk()
220 * on the walk_t before discarding it or calling guest_walk_tables again.
221 * If "guest_op" is non-zero, we are serving a genuine guest memory access,
222 * and must (a) be under the shadow lock, and (b) remove write access
223 * from any gueat PT pages we see, as we will be using their contents to
224 * perform shadow updates.
225 * Returns 0 for success or non-zero if the guest pagetables are malformed.
226 * N.B. Finding a not-present entry does not cause a non-zero return code. */
227 static inline int
228 guest_walk_tables(struct vcpu *v, unsigned long va, walk_t *gw, int guest_op)
229 {
230 ASSERT(!guest_op || shadow_locked_by_me(v->domain));
232 perfc_incrc(shadow_guest_walk);
233 memset(gw, 0, sizeof(*gw));
234 gw->va = va;
236 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
237 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
238 /* Get l4e from the top level table */
239 gw->l4mfn = pagetable_get_mfn(v->arch.guest_table);
240 gw->l4e = (guest_l4e_t *)v->arch.guest_vtable + guest_l4_table_offset(va);
241 /* Walk down to the l3e */
242 if ( !(guest_l4e_get_flags(*gw->l4e) & _PAGE_PRESENT) ) return 0;
243 gw->l3mfn = vcpu_gfn_to_mfn(v, guest_l4e_get_gfn(*gw->l4e));
244 if ( !mfn_valid(gw->l3mfn) ) return 1;
245 /* This mfn is a pagetable: make sure the guest can't write to it. */
246 if ( guest_op && sh_remove_write_access(v, gw->l3mfn, 3, va) != 0 )
247 flush_tlb_mask(v->domain->domain_dirty_cpumask);
248 gw->l3e = ((guest_l3e_t *)sh_map_domain_page(gw->l3mfn))
249 + guest_l3_table_offset(va);
250 #else /* PAE only... */
251 /* Get l3e from the top level table */
252 gw->l3mfn = pagetable_get_mfn(v->arch.guest_table);
253 gw->l3e = (guest_l3e_t *)v->arch.guest_vtable + guest_l3_table_offset(va);
254 #endif /* PAE or 64... */
255 /* Walk down to the l2e */
256 if ( !(guest_l3e_get_flags(*gw->l3e) & _PAGE_PRESENT) ) return 0;
257 gw->l2mfn = vcpu_gfn_to_mfn(v, guest_l3e_get_gfn(*gw->l3e));
258 if ( !mfn_valid(gw->l2mfn) ) return 1;
259 /* This mfn is a pagetable: make sure the guest can't write to it. */
260 if ( guest_op && sh_remove_write_access(v, gw->l2mfn, 2, va) != 0 )
261 flush_tlb_mask(v->domain->domain_dirty_cpumask);
262 gw->l2e = ((guest_l2e_t *)sh_map_domain_page(gw->l2mfn))
263 + guest_l2_table_offset(va);
264 #else /* 32-bit only... */
265 /* Get l2e from the top level table */
266 gw->l2mfn = pagetable_get_mfn(v->arch.guest_table);
267 gw->l2e = (guest_l2e_t *)v->arch.guest_vtable + guest_l2_table_offset(va);
268 #endif /* All levels... */
270 if ( !(guest_l2e_get_flags(*gw->l2e) & _PAGE_PRESENT) ) return 0;
271 if ( guest_supports_superpages(v) &&
272 (guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE) )
273 {
274 /* Special case: this guest VA is in a PSE superpage, so there's
275 * no guest l1e. We make one up so that the propagation code
276 * can generate a shadow l1 table. Start with the gfn of the
277 * first 4k-page of the superpage. */
278 gfn_t start = guest_l2e_get_gfn(*gw->l2e);
279 /* Grant full access in the l1e, since all the guest entry's
280 * access controls are enforced in the shadow l2e. This lets
281 * us reflect l2 changes later without touching the l1s. */
282 int flags = (_PAGE_PRESENT|_PAGE_USER|_PAGE_RW|
283 _PAGE_ACCESSED|_PAGE_DIRTY);
284 /* PSE level 2 entries use bit 12 for PAT; propagate it to bit 7
285 * of the level 1 */
286 if ( (guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE_PAT) )
287 flags |= _PAGE_PAT;
288 /* Increment the pfn by the right number of 4k pages.
289 * The ~0x1 is to mask out the PAT bit mentioned above. */
290 start = _gfn((gfn_x(start) & ~0x1) + guest_l1_table_offset(va));
291 gw->eff_l1e = guest_l1e_from_gfn(start, flags);
292 gw->l1e = NULL;
293 gw->l1mfn = _mfn(INVALID_MFN);
294 }
295 else
296 {
297 /* Not a superpage: carry on and find the l1e. */
298 gw->l1mfn = vcpu_gfn_to_mfn(v, guest_l2e_get_gfn(*gw->l2e));
299 if ( !mfn_valid(gw->l1mfn) ) return 1;
300 /* This mfn is a pagetable: make sure the guest can't write to it. */
301 if ( guest_op
302 && sh_remove_write_access(v, gw->l1mfn, 1, va) != 0 )
303 flush_tlb_mask(v->domain->domain_dirty_cpumask);
304 gw->l1e = ((guest_l1e_t *)sh_map_domain_page(gw->l1mfn))
305 + guest_l1_table_offset(va);
306 gw->eff_l1e = *gw->l1e;
307 }
309 return 0;
310 }
312 /* Given a walk_t, translate the gw->va into the guest's notion of the
313 * corresponding frame number. */
314 static inline gfn_t
315 guest_walk_to_gfn(walk_t *gw)
316 {
317 if ( !(guest_l1e_get_flags(gw->eff_l1e) & _PAGE_PRESENT) )
318 return _gfn(INVALID_GFN);
319 return guest_l1e_get_gfn(gw->eff_l1e);
320 }
322 /* Given a walk_t, translate the gw->va into the guest's notion of the
323 * corresponding physical address. */
324 static inline paddr_t
325 guest_walk_to_gpa(walk_t *gw)
326 {
327 if ( !(guest_l1e_get_flags(gw->eff_l1e) & _PAGE_PRESENT) )
328 return 0;
329 return guest_l1e_get_paddr(gw->eff_l1e) + (gw->va & ~PAGE_MASK);
330 }
333 /* Unmap (and reinitialise) a guest walk.
334 * Call this to dispose of any walk filled in by guest_walk_tables() */
335 static void unmap_walk(struct vcpu *v, walk_t *gw)
336 {
337 #if GUEST_PAGING_LEVELS >= 3
338 #if GUEST_PAGING_LEVELS >= 4
339 if ( gw->l3e != NULL ) sh_unmap_domain_page(gw->l3e);
340 #endif
341 if ( gw->l2e != NULL ) sh_unmap_domain_page(gw->l2e);
342 #endif
343 if ( gw->l1e != NULL ) sh_unmap_domain_page(gw->l1e);
344 #ifdef DEBUG
345 memset(gw, 0, sizeof(*gw));
346 #endif
347 }
350 /* Pretty-print the contents of a guest-walk */
351 static inline void print_gw(walk_t *gw)
352 {
353 SHADOW_PRINTK("GUEST WALK TO %#lx:\n", gw->va);
354 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
355 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
356 SHADOW_PRINTK(" l4mfn=%" SH_PRI_mfn "\n", mfn_x(gw->l4mfn));
357 SHADOW_PRINTK(" l4e=%p\n", gw->l4e);
358 if ( gw->l4e )
359 SHADOW_PRINTK(" *l4e=%" SH_PRI_gpte "\n", gw->l4e->l4);
360 #endif /* PAE or 64... */
361 SHADOW_PRINTK(" l3mfn=%" SH_PRI_mfn "\n", mfn_x(gw->l3mfn));
362 SHADOW_PRINTK(" l3e=%p\n", gw->l3e);
363 if ( gw->l3e )
364 SHADOW_PRINTK(" *l3e=%" SH_PRI_gpte "\n", gw->l3e->l3);
365 #endif /* All levels... */
366 SHADOW_PRINTK(" l2mfn=%" SH_PRI_mfn "\n", mfn_x(gw->l2mfn));
367 SHADOW_PRINTK(" l2e=%p\n", gw->l2e);
368 if ( gw->l2e )
369 SHADOW_PRINTK(" *l2e=%" SH_PRI_gpte "\n", gw->l2e->l2);
370 SHADOW_PRINTK(" l1mfn=%" SH_PRI_mfn "\n", mfn_x(gw->l1mfn));
371 SHADOW_PRINTK(" l1e=%p\n", gw->l1e);
372 if ( gw->l1e )
373 SHADOW_PRINTK(" *l1e=%" SH_PRI_gpte "\n", gw->l1e->l1);
374 SHADOW_PRINTK(" eff_l1e=%" SH_PRI_gpte "\n", gw->eff_l1e.l1);
375 }
378 #if SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES
379 /* Lightweight audit: pass all the shadows associated with this guest walk
380 * through the audit mechanisms */
381 static void sh_audit_gw(struct vcpu *v, walk_t *gw)
382 {
383 mfn_t smfn;
385 if ( !(SHADOW_AUDIT_ENABLE) )
386 return;
388 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
389 if ( mfn_valid(gw->l4mfn)
390 && mfn_valid((smfn = get_shadow_status(v, gw->l4mfn,
391 SH_type_l4_shadow))) )
392 (void) sh_audit_l4_table(v, smfn, _mfn(INVALID_MFN));
393 if ( mfn_valid(gw->l3mfn)
394 && mfn_valid((smfn = get_shadow_status(v, gw->l3mfn,
395 SH_type_l3_shadow))) )
396 (void) sh_audit_l3_table(v, smfn, _mfn(INVALID_MFN));
397 #endif /* PAE or 64... */
398 if ( mfn_valid(gw->l2mfn) )
399 {
400 if ( mfn_valid((smfn = get_shadow_status(v, gw->l2mfn,
401 SH_type_l2_shadow))) )
402 (void) sh_audit_l2_table(v, smfn, _mfn(INVALID_MFN));
403 #if GUEST_PAGING_LEVELS == 3
404 if ( mfn_valid((smfn = get_shadow_status(v, gw->l2mfn,
405 SH_type_l2h_shadow))) )
406 (void) sh_audit_l2_table(v, smfn, _mfn(INVALID_MFN));
407 #endif
408 }
409 if ( mfn_valid(gw->l1mfn)
410 && mfn_valid((smfn = get_shadow_status(v, gw->l1mfn,
411 SH_type_l1_shadow))) )
412 (void) sh_audit_l1_table(v, smfn, _mfn(INVALID_MFN));
413 else if ( gw->l2e
414 && (guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE)
415 && mfn_valid(
416 (smfn = get_fl1_shadow_status(v, guest_l2e_get_gfn(*gw->l2e)))) )
417 (void) sh_audit_fl1_table(v, smfn, _mfn(INVALID_MFN));
418 }
420 #else
421 #define sh_audit_gw(_v, _gw) do {} while(0)
422 #endif /* audit code */
426 /**************************************************************************/
427 /* Function to write to the guest tables, for propagating accessed and
428 * dirty bits from the shadow to the guest.
429 * Takes a guest mfn, a pointer to the guest entry, the level of pagetable,
430 * and an operation type. The guest entry is always passed as an l1e:
431 * since we only ever write flags, that's OK.
432 * Returns the new flag bits of the guest entry. */
434 static u32 guest_set_ad_bits(struct vcpu *v,
435 mfn_t gmfn,
436 guest_l1e_t *ep,
437 unsigned int level,
438 fetch_type_t ft)
439 {
440 u32 flags;
441 int res = 0;
443 ASSERT(ep && !(((unsigned long)ep) & ((sizeof *ep) - 1)));
444 ASSERT(level <= GUEST_PAGING_LEVELS);
445 ASSERT(shadow_locked_by_me(v->domain));
447 flags = guest_l1e_get_flags(*ep);
449 /* Only set A and D bits for guest-initiated accesses */
450 if ( !(ft & FETCH_TYPE_DEMAND) )
451 return flags;
453 ASSERT(mfn_valid(gmfn)
454 && (sh_mfn_is_a_page_table(gmfn)
455 || ((mfn_to_page(gmfn)->u.inuse.type_info & PGT_count_mask)
456 == 0)));
458 /* PAE l3s do not have A and D bits */
459 ASSERT(GUEST_PAGING_LEVELS > 3 || level != 3);
461 /* Need the D bit as well for writes, in L1es and PSE L2es. */
462 if ( ft == ft_demand_write
463 && (level == 1 ||
464 (level == 2 && (flags & _PAGE_PSE) && guest_supports_superpages(v))) )
465 {
466 if ( (flags & (_PAGE_DIRTY | _PAGE_ACCESSED))
467 == (_PAGE_DIRTY | _PAGE_ACCESSED) )
468 return flags; /* Guest already has A and D bits set */
469 flags |= _PAGE_DIRTY | _PAGE_ACCESSED;
470 perfc_incrc(shadow_ad_update);
471 }
472 else
473 {
474 if ( flags & _PAGE_ACCESSED )
475 return flags; /* Guest already has A bit set */
476 flags |= _PAGE_ACCESSED;
477 perfc_incrc(shadow_a_update);
478 }
480 /* Set the bit(s) */
481 sh_mark_dirty(v->domain, gmfn);
482 SHADOW_DEBUG(A_AND_D, "gfn = %" SH_PRI_gfn ", "
483 "old flags = %#x, new flags = %#x\n",
484 gfn_x(guest_l1e_get_gfn(*ep)), guest_l1e_get_flags(*ep),
485 flags);
486 *ep = guest_l1e_from_gfn(guest_l1e_get_gfn(*ep), flags);
488 /* Propagate this change to any other shadows of the page
489 * (only necessary if there is more than one shadow) */
490 if ( mfn_to_page(gmfn)->count_info & PGC_page_table )
491 {
492 u32 shflags = mfn_to_page(gmfn)->shadow_flags & SHF_page_type_mask;
493 /* More than one type bit set in shadow-flags? */
494 if ( shflags & ~(1UL << find_first_set_bit(shflags)) )
495 res = sh_validate_guest_entry(v, gmfn, ep, sizeof (*ep));
496 }
498 /* We should never need to flush the TLB or recopy PAE entries */
499 ASSERT((res == 0) || (res == SHADOW_SET_CHANGED));
501 return flags;
502 }
504 #if (CONFIG_PAGING_LEVELS == GUEST_PAGING_LEVELS) && (CONFIG_PAGING_LEVELS == SHADOW_PAGING_LEVELS)
505 void *
506 sh_guest_map_l1e(struct vcpu *v, unsigned long addr,
507 unsigned long *gl1mfn)
508 {
509 void *pl1e = NULL;
510 walk_t gw;
512 ASSERT(shadow_mode_translate(v->domain));
514 // XXX -- this is expensive, but it's easy to cobble together...
515 // FIXME!
517 shadow_lock(v->domain);
518 guest_walk_tables(v, addr, &gw, 1);
520 if ( gw.l2e &&
521 (guest_l2e_get_flags(*gw.l2e) & _PAGE_PRESENT) &&
522 !(guest_supports_superpages(v) && (guest_l2e_get_flags(*gw.l2e) & _PAGE_PSE)) )
523 {
524 if ( gl1mfn )
525 *gl1mfn = mfn_x(gw.l1mfn);
526 pl1e = map_domain_page(mfn_x(gw.l1mfn)) +
527 (guest_l1_table_offset(addr) * sizeof(guest_l1e_t));
528 }
530 unmap_walk(v, &gw);
531 shadow_unlock(v->domain);
533 return pl1e;
534 }
536 void
537 sh_guest_get_eff_l1e(struct vcpu *v, unsigned long addr, void *eff_l1e)
538 {
539 walk_t gw;
541 ASSERT(shadow_mode_translate(v->domain));
543 // XXX -- this is expensive, but it's easy to cobble together...
544 // FIXME!
546 shadow_lock(v->domain);
547 guest_walk_tables(v, addr, &gw, 1);
548 *(guest_l1e_t *)eff_l1e = gw.eff_l1e;
549 unmap_walk(v, &gw);
550 shadow_unlock(v->domain);
551 }
552 #endif /* CONFIG==SHADOW==GUEST */
554 /**************************************************************************/
555 /* Functions to compute the correct index into a shadow page, given an
556 * index into the guest page (as returned by guest_get_index()).
557 * This is trivial when the shadow and guest use the same sized PTEs, but
558 * gets more interesting when those sizes are mismatched (e.g. 32-bit guest,
559 * PAE- or 64-bit shadows).
560 *
561 * These functions also increment the shadow mfn, when necessary. When PTE
562 * sizes are mismatched, it takes 2 shadow L1 pages for a single guest L1
563 * page. In this case, we allocate 2 contiguous pages for the shadow L1, and
564 * use simple pointer arithmetic on a pointer to the guest L1e to figure out
565 * which shadow page we really want. Similarly, when PTE sizes are
566 * mismatched, we shadow a guest L2 page with 4 shadow L2 pages. (The easiest
567 * way to see this is: a 32-bit guest L2 page maps 4GB of virtual address
568 * space, while a PAE- or 64-bit shadow L2 page maps 1GB of virtual address
569 * space.)
570 *
571 * For PAE guests, for every 32-bytes of guest L3 page table, we use 64-bytes
572 * of shadow (to store both the shadow, and the info that would normally be
573 * stored in page_info fields). This arrangement allows the shadow and the
574 * "page_info" fields to always be stored in the same page (in fact, in
575 * the same cache line), avoiding an extra call to map_domain_page().
576 */
578 static inline u32
579 guest_index(void *ptr)
580 {
581 return (u32)((unsigned long)ptr & ~PAGE_MASK) / sizeof(guest_l1e_t);
582 }
584 static u32
585 shadow_l1_index(mfn_t *smfn, u32 guest_index)
586 {
587 #if (GUEST_PAGING_LEVELS == 2) && (SHADOW_PAGING_LEVELS != 2)
588 *smfn = _mfn(mfn_x(*smfn) +
589 (guest_index / SHADOW_L1_PAGETABLE_ENTRIES));
590 return (guest_index % SHADOW_L1_PAGETABLE_ENTRIES);
591 #else
592 return guest_index;
593 #endif
594 }
596 static u32
597 shadow_l2_index(mfn_t *smfn, u32 guest_index)
598 {
599 #if (GUEST_PAGING_LEVELS == 2) && (SHADOW_PAGING_LEVELS != 2)
600 // Because we use 2 shadow l2 entries for each guest entry, the number of
601 // guest entries per shadow page is SHADOW_L2_PAGETABLE_ENTRIES/2
602 //
603 *smfn = _mfn(mfn_x(*smfn) +
604 (guest_index / (SHADOW_L2_PAGETABLE_ENTRIES / 2)));
606 // We multiple by two to get the index of the first of the two entries
607 // used to shadow the specified guest entry.
608 return (guest_index % (SHADOW_L2_PAGETABLE_ENTRIES / 2)) * 2;
609 #else
610 return guest_index;
611 #endif
612 }
614 #if GUEST_PAGING_LEVELS >= 4
616 static u32
617 shadow_l3_index(mfn_t *smfn, u32 guest_index)
618 {
619 return guest_index;
620 }
622 static u32
623 shadow_l4_index(mfn_t *smfn, u32 guest_index)
624 {
625 return guest_index;
626 }
628 #endif // GUEST_PAGING_LEVELS >= 4
631 /**************************************************************************/
632 /* Function which computes shadow entries from their corresponding guest
633 * entries. This is the "heart" of the shadow code. It operates using
634 * level-1 shadow types, but handles all levels of entry.
635 * Don't call it directly, but use the four wrappers below.
636 */
638 static always_inline void
639 _sh_propagate(struct vcpu *v,
640 void *guest_entry_ptr,
641 mfn_t guest_table_mfn,
642 mfn_t target_mfn,
643 void *shadow_entry_ptr,
644 int level,
645 fetch_type_t ft,
646 int mmio)
647 {
648 guest_l1e_t *gp = guest_entry_ptr;
649 shadow_l1e_t *sp = shadow_entry_ptr;
650 struct domain *d = v->domain;
651 u32 pass_thru_flags;
652 u32 gflags, sflags;
654 /* We don't shadow PAE l3s */
655 ASSERT(GUEST_PAGING_LEVELS > 3 || level != 3);
657 if ( mfn_valid(guest_table_mfn) )
658 /* Handle A and D bit propagation into the guest */
659 gflags = guest_set_ad_bits(v, guest_table_mfn, gp, level, ft);
660 else
661 {
662 /* Must be an fl1e or a prefetch */
663 ASSERT(level==1 || !(ft & FETCH_TYPE_DEMAND));
664 gflags = guest_l1e_get_flags(*gp);
665 }
667 if ( unlikely(!(gflags & _PAGE_PRESENT)) )
668 {
669 /* If a guest l1 entry is not present, shadow with the magic
670 * guest-not-present entry. */
671 if ( level == 1 )
672 *sp = sh_l1e_gnp();
673 else
674 *sp = shadow_l1e_empty();
675 goto done;
676 }
678 if ( level == 1 && mmio )
679 {
680 /* Guest l1e maps MMIO space */
681 *sp = sh_l1e_mmio(guest_l1e_get_gfn(*gp), gflags);
682 goto done;
683 }
685 // Must have a valid target_mfn, unless this is a prefetch. In the
686 // case of a prefetch, an invalid mfn means that we can not usefully
687 // shadow anything, and so we return early.
688 //
689 if ( !mfn_valid(target_mfn) )
690 {
691 ASSERT((ft == ft_prefetch));
692 *sp = shadow_l1e_empty();
693 goto done;
694 }
696 // Propagate bits from the guest to the shadow.
697 // Some of these may be overwritten, below.
698 // Since we know the guest's PRESENT bit is set, we also set the shadow's
699 // SHADOW_PRESENT bit.
700 //
701 pass_thru_flags = (_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_USER |
702 _PAGE_RW | _PAGE_PRESENT);
703 if ( guest_supports_nx(v) )
704 pass_thru_flags |= _PAGE_NX_BIT;
705 sflags = gflags & pass_thru_flags;
707 // Set the A&D bits for higher level shadows.
708 // Higher level entries do not, strictly speaking, have dirty bits, but
709 // since we use shadow linear tables, each of these entries may, at some
710 // point in time, also serve as a shadow L1 entry.
711 // By setting both the A&D bits in each of these, we eliminate the burden
712 // on the hardware to update these bits on initial accesses.
713 //
714 if ( (level > 1) && !((SHADOW_PAGING_LEVELS == 3) && (level == 3)) )
715 sflags |= _PAGE_ACCESSED | _PAGE_DIRTY;
717 // If the A or D bit has not yet been set in the guest, then we must
718 // prevent the corresponding kind of access.
719 //
720 if ( unlikely(!(gflags & _PAGE_ACCESSED)) )
721 sflags &= ~_PAGE_PRESENT;
723 /* D bits exist in L1es and PSE L2es */
724 if ( unlikely(((level == 1) ||
725 ((level == 2) &&
726 (gflags & _PAGE_PSE) &&
727 guest_supports_superpages(v)))
728 && !(gflags & _PAGE_DIRTY)) )
729 sflags &= ~_PAGE_RW;
731 // shadow_mode_log_dirty support
732 //
733 // Only allow the guest write access to a page a) on a demand fault,
734 // or b) if the page is already marked as dirty.
735 //
736 if ( unlikely((level == 1) && shadow_mode_log_dirty(d)) )
737 {
738 if ( ft & FETCH_TYPE_WRITE )
739 sh_mark_dirty(d, target_mfn);
740 else if ( !sh_mfn_is_dirty(d, target_mfn) )
741 sflags &= ~_PAGE_RW;
742 }
744 // protect guest page tables
745 //
746 if ( unlikely((level == 1) && sh_mfn_is_a_page_table(target_mfn)) )
747 {
748 if ( shadow_mode_trap_reads(d) )
749 {
750 // if we are trapping both reads & writes, then mark this page
751 // as not present...
752 //
753 sflags &= ~_PAGE_PRESENT;
754 }
755 else
756 {
757 // otherwise, just prevent any writes...
758 //
759 sflags &= ~_PAGE_RW;
760 }
761 }
763 // PV guests in 64-bit mode use two different page tables for user vs
764 // supervisor permissions, making the guest's _PAGE_USER bit irrelevant.
765 // It is always shadowed as present...
766 if ( (GUEST_PAGING_LEVELS == 4) && !is_hvm_domain(d) )
767 {
768 sflags |= _PAGE_USER;
769 }
771 *sp = shadow_l1e_from_mfn(target_mfn, sflags);
772 done:
773 SHADOW_DEBUG(PROPAGATE,
774 "%s level %u guest %" SH_PRI_gpte " shadow %" SH_PRI_pte "\n",
775 fetch_type_names[ft], level, gp->l1, sp->l1);
776 }
779 /* These four wrappers give us a little bit of type-safety back around the
780 * use of void-* pointers in _sh_propagate(), and allow the compiler to
781 * optimize out some level checks. */
783 #if GUEST_PAGING_LEVELS >= 4
784 static void
785 l4e_propagate_from_guest(struct vcpu *v,
786 guest_l4e_t *gl4e,
787 mfn_t gl4mfn,
788 mfn_t sl3mfn,
789 shadow_l4e_t *sl4e,
790 fetch_type_t ft)
791 {
792 _sh_propagate(v, gl4e, gl4mfn, sl3mfn, sl4e, 4, ft, 0);
793 }
795 static void
796 l3e_propagate_from_guest(struct vcpu *v,
797 guest_l3e_t *gl3e,
798 mfn_t gl3mfn,
799 mfn_t sl2mfn,
800 shadow_l3e_t *sl3e,
801 fetch_type_t ft)
802 {
803 _sh_propagate(v, gl3e, gl3mfn, sl2mfn, sl3e, 3, ft, 0);
804 }
805 #endif // GUEST_PAGING_LEVELS >= 4
807 static void
808 l2e_propagate_from_guest(struct vcpu *v,
809 guest_l2e_t *gl2e,
810 mfn_t gl2mfn,
811 mfn_t sl1mfn,
812 shadow_l2e_t *sl2e,
813 fetch_type_t ft)
814 {
815 _sh_propagate(v, gl2e, gl2mfn, sl1mfn, sl2e, 2, ft, 0);
816 }
818 static void
819 l1e_propagate_from_guest(struct vcpu *v,
820 guest_l1e_t *gl1e,
821 mfn_t gl1mfn,
822 mfn_t gmfn,
823 shadow_l1e_t *sl1e,
824 fetch_type_t ft,
825 int mmio)
826 {
827 _sh_propagate(v, gl1e, gl1mfn, gmfn, sl1e, 1, ft, mmio);
828 }
831 /**************************************************************************/
832 /* These functions update shadow entries (and do bookkeeping on the shadow
833 * tables they are in). It is intended that they are the only
834 * functions which ever write (non-zero) data onto a shadow page.
835 */
837 static inline void safe_write_entry(void *dst, void *src)
838 /* Copy one PTE safely when processors might be running on the
839 * destination pagetable. This does *not* give safety against
840 * concurrent writes (that's what the shadow lock is for), just
841 * stops the hardware picking up partially written entries. */
842 {
843 volatile unsigned long *d = dst;
844 unsigned long *s = src;
845 ASSERT(!((unsigned long) d & (sizeof (shadow_l1e_t) - 1)));
846 #if CONFIG_PAGING_LEVELS == 3
847 /* In PAE mode, pagetable entries are larger
848 * than machine words, so won't get written atomically. We need to make
849 * sure any other cpu running on these shadows doesn't see a
850 * half-written entry. Do this by marking the entry not-present first,
851 * then writing the high word before the low word. */
852 BUILD_BUG_ON(sizeof (shadow_l1e_t) != 2 * sizeof (unsigned long));
853 d[0] = 0;
854 d[1] = s[1];
855 d[0] = s[0];
856 #else
857 /* In 32-bit and 64-bit, sizeof(pte) == sizeof(ulong) == 1 word,
858 * which will be an atomic write, since the entry is aligned. */
859 BUILD_BUG_ON(sizeof (shadow_l1e_t) != sizeof (unsigned long));
860 *d = *s;
861 #endif
862 }
865 static inline void
866 shadow_write_entries(void *d, void *s, int entries, mfn_t mfn)
867 /* This function does the actual writes to shadow pages.
868 * It must not be called directly, since it doesn't do the bookkeeping
869 * that shadow_set_l*e() functions do. */
870 {
871 shadow_l1e_t *dst = d;
872 shadow_l1e_t *src = s;
873 void *map = NULL;
874 int i;
876 /* Because we mirror access rights at all levels in the shadow, an
877 * l2 (or higher) entry with the RW bit cleared will leave us with
878 * no write access through the linear map.
879 * We detect that by writing to the shadow with copy_to_user() and
880 * using map_domain_page() to get a writeable mapping if we need to. */
881 if ( __copy_to_user(d, d, sizeof (unsigned long)) != 0 )
882 {
883 perfc_incrc(shadow_linear_map_failed);
884 map = sh_map_domain_page(mfn);
885 ASSERT(map != NULL);
886 dst = map + ((unsigned long)dst & (PAGE_SIZE - 1));
887 }
890 for ( i = 0; i < entries; i++ )
891 safe_write_entry(dst++, src++);
893 if ( map != NULL ) sh_unmap_domain_page(map);
894 }
896 static inline int
897 perms_strictly_increased(u32 old_flags, u32 new_flags)
898 /* Given the flags of two entries, are the new flags a strict
899 * increase in rights over the old ones? */
900 {
901 u32 of = old_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX);
902 u32 nf = new_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX);
903 /* Flip the NX bit, since it's the only one that decreases rights;
904 * we calculate as if it were an "X" bit. */
905 of ^= _PAGE_NX_BIT;
906 nf ^= _PAGE_NX_BIT;
907 /* If the changed bits are all set in the new flags, then rights strictly
908 * increased between old and new. */
909 return ((of | (of ^ nf)) == nf);
910 }
912 static int inline
913 shadow_get_page_from_l1e(shadow_l1e_t sl1e, struct domain *d)
914 {
915 int res;
916 mfn_t mfn;
917 struct domain *owner;
919 ASSERT(!sh_l1e_is_magic(sl1e));
921 if ( !shadow_mode_refcounts(d) )
922 return 1;
924 res = get_page_from_l1e(sl1e, d);
926 // If a privileged domain is attempting to install a map of a page it does
927 // not own, we let it succeed anyway.
928 //
929 if ( unlikely(!res) &&
930 IS_PRIV(d) &&
931 !shadow_mode_translate(d) &&
932 mfn_valid(mfn = shadow_l1e_get_mfn(sl1e)) &&
933 (owner = page_get_owner(mfn_to_page(mfn))) &&
934 (d != owner) )
935 {
936 res = get_page_from_l1e(sl1e, owner);
937 SHADOW_PRINTK("privileged domain %d installs map of mfn %05lx "
938 "which is owned by domain %d: %s\n",
939 d->domain_id, mfn_x(mfn), owner->domain_id,
940 res ? "success" : "failed");
941 }
943 if ( unlikely(!res) )
944 {
945 perfc_incrc(shadow_get_page_fail);
946 SHADOW_PRINTK("failed: l1e=" SH_PRI_pte "\n");
947 }
949 return res;
950 }
952 static void inline
953 shadow_put_page_from_l1e(shadow_l1e_t sl1e, struct domain *d)
954 {
955 if ( !shadow_mode_refcounts(d) )
956 return;
958 put_page_from_l1e(sl1e, d);
959 }
961 #if GUEST_PAGING_LEVELS >= 4
962 static int shadow_set_l4e(struct vcpu *v,
963 shadow_l4e_t *sl4e,
964 shadow_l4e_t new_sl4e,
965 mfn_t sl4mfn)
966 {
967 int flags = 0, ok;
968 shadow_l4e_t old_sl4e;
969 paddr_t paddr;
970 ASSERT(sl4e != NULL);
971 old_sl4e = *sl4e;
973 if ( old_sl4e.l4 == new_sl4e.l4 ) return 0; /* Nothing to do */
975 paddr = ((((paddr_t)mfn_x(sl4mfn)) << PAGE_SHIFT)
976 | (((unsigned long)sl4e) & ~PAGE_MASK));
978 if ( shadow_l4e_get_flags(new_sl4e) & _PAGE_PRESENT )
979 {
980 /* About to install a new reference */
981 mfn_t sl3mfn = shadow_l4e_get_mfn(new_sl4e);
982 ok = sh_get_ref(v, sl3mfn, paddr);
983 /* Are we pinning l3 shadows to handle wierd linux behaviour? */
984 if ( sh_type_is_pinnable(v, SH_type_l3_64_shadow) )
985 ok |= sh_pin(v, sl3mfn);
986 if ( !ok )
987 {
988 domain_crash(v->domain);
989 return SHADOW_SET_ERROR;
990 }
991 }
993 /* Write the new entry */
994 shadow_write_entries(sl4e, &new_sl4e, 1, sl4mfn);
995 flags |= SHADOW_SET_CHANGED;
997 if ( shadow_l4e_get_flags(old_sl4e) & _PAGE_PRESENT )
998 {
999 /* We lost a reference to an old mfn. */
1000 mfn_t osl3mfn = shadow_l4e_get_mfn(old_sl4e);
1001 if ( (mfn_x(osl3mfn) != mfn_x(shadow_l4e_get_mfn(new_sl4e)))
1002 || !perms_strictly_increased(shadow_l4e_get_flags(old_sl4e),
1003 shadow_l4e_get_flags(new_sl4e)) )
1005 flags |= SHADOW_SET_FLUSH;
1007 sh_put_ref(v, osl3mfn, paddr);
1009 return flags;
1012 static int shadow_set_l3e(struct vcpu *v,
1013 shadow_l3e_t *sl3e,
1014 shadow_l3e_t new_sl3e,
1015 mfn_t sl3mfn)
1017 int flags = 0;
1018 shadow_l3e_t old_sl3e;
1019 paddr_t paddr;
1020 ASSERT(sl3e != NULL);
1021 old_sl3e = *sl3e;
1023 if ( old_sl3e.l3 == new_sl3e.l3 ) return 0; /* Nothing to do */
1025 paddr = ((((paddr_t)mfn_x(sl3mfn)) << PAGE_SHIFT)
1026 | (((unsigned long)sl3e) & ~PAGE_MASK));
1028 if ( shadow_l3e_get_flags(new_sl3e) & _PAGE_PRESENT )
1029 /* About to install a new reference */
1030 if ( !sh_get_ref(v, shadow_l3e_get_mfn(new_sl3e), paddr) )
1032 domain_crash(v->domain);
1033 return SHADOW_SET_ERROR;
1036 /* Write the new entry */
1037 shadow_write_entries(sl3e, &new_sl3e, 1, sl3mfn);
1038 flags |= SHADOW_SET_CHANGED;
1040 if ( shadow_l3e_get_flags(old_sl3e) & _PAGE_PRESENT )
1042 /* We lost a reference to an old mfn. */
1043 mfn_t osl2mfn = shadow_l3e_get_mfn(old_sl3e);
1044 if ( (mfn_x(osl2mfn) != mfn_x(shadow_l3e_get_mfn(new_sl3e))) ||
1045 !perms_strictly_increased(shadow_l3e_get_flags(old_sl3e),
1046 shadow_l3e_get_flags(new_sl3e)) )
1048 flags |= SHADOW_SET_FLUSH;
1050 sh_put_ref(v, osl2mfn, paddr);
1052 return flags;
1054 #endif /* GUEST_PAGING_LEVELS >= 4 */
1056 static int shadow_set_l2e(struct vcpu *v,
1057 shadow_l2e_t *sl2e,
1058 shadow_l2e_t new_sl2e,
1059 mfn_t sl2mfn)
1061 int flags = 0;
1062 shadow_l2e_t old_sl2e;
1063 paddr_t paddr;
1065 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1066 /* In 2-on-3 we work with pairs of l2es pointing at two-page
1067 * shadows. Reference counting and up-pointers track from the first
1068 * page of the shadow to the first l2e, so make sure that we're
1069 * working with those:
1070 * Align the pointer down so it's pointing at the first of the pair */
1071 sl2e = (shadow_l2e_t *)((unsigned long)sl2e & ~(sizeof(shadow_l2e_t)));
1072 /* Align the mfn of the shadow entry too */
1073 new_sl2e.l2 &= ~(1<<PAGE_SHIFT);
1074 #endif
1076 ASSERT(sl2e != NULL);
1077 old_sl2e = *sl2e;
1079 if ( old_sl2e.l2 == new_sl2e.l2 ) return 0; /* Nothing to do */
1081 paddr = ((((paddr_t)mfn_x(sl2mfn)) << PAGE_SHIFT)
1082 | (((unsigned long)sl2e) & ~PAGE_MASK));
1084 if ( shadow_l2e_get_flags(new_sl2e) & _PAGE_PRESENT )
1085 /* About to install a new reference */
1086 if ( !sh_get_ref(v, shadow_l2e_get_mfn(new_sl2e), paddr) )
1088 domain_crash(v->domain);
1089 return SHADOW_SET_ERROR;
1092 /* Write the new entry */
1093 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1095 shadow_l2e_t pair[2] = { new_sl2e, new_sl2e };
1096 /* The l1 shadow is two pages long and need to be pointed to by
1097 * two adjacent l1es. The pair have the same flags, but point
1098 * at odd and even MFNs */
1099 ASSERT(!(pair[0].l2 & (1<<PAGE_SHIFT)));
1100 pair[1].l2 |= (1<<PAGE_SHIFT);
1101 shadow_write_entries(sl2e, &pair, 2, sl2mfn);
1103 #else /* normal case */
1104 shadow_write_entries(sl2e, &new_sl2e, 1, sl2mfn);
1105 #endif
1106 flags |= SHADOW_SET_CHANGED;
1108 if ( shadow_l2e_get_flags(old_sl2e) & _PAGE_PRESENT )
1110 /* We lost a reference to an old mfn. */
1111 mfn_t osl1mfn = shadow_l2e_get_mfn(old_sl2e);
1112 if ( (mfn_x(osl1mfn) != mfn_x(shadow_l2e_get_mfn(new_sl2e))) ||
1113 !perms_strictly_increased(shadow_l2e_get_flags(old_sl2e),
1114 shadow_l2e_get_flags(new_sl2e)) )
1116 flags |= SHADOW_SET_FLUSH;
1118 sh_put_ref(v, osl1mfn, paddr);
1120 return flags;
1123 static int shadow_set_l1e(struct vcpu *v,
1124 shadow_l1e_t *sl1e,
1125 shadow_l1e_t new_sl1e,
1126 mfn_t sl1mfn)
1128 int flags = 0;
1129 struct domain *d = v->domain;
1130 shadow_l1e_t old_sl1e;
1131 ASSERT(sl1e != NULL);
1133 old_sl1e = *sl1e;
1135 if ( old_sl1e.l1 == new_sl1e.l1 ) return 0; /* Nothing to do */
1137 if ( (shadow_l1e_get_flags(new_sl1e) & _PAGE_PRESENT)
1138 && !sh_l1e_is_magic(new_sl1e) )
1140 /* About to install a new reference */
1141 if ( shadow_mode_refcounts(d) ) {
1142 if ( shadow_get_page_from_l1e(new_sl1e, d) == 0 )
1144 /* Doesn't look like a pagetable. */
1145 flags |= SHADOW_SET_ERROR;
1146 new_sl1e = shadow_l1e_empty();
1151 /* Write the new entry */
1152 shadow_write_entries(sl1e, &new_sl1e, 1, sl1mfn);
1153 flags |= SHADOW_SET_CHANGED;
1155 if ( (shadow_l1e_get_flags(old_sl1e) & _PAGE_PRESENT)
1156 && !sh_l1e_is_magic(old_sl1e) )
1158 /* We lost a reference to an old mfn. */
1159 /* N.B. Unlike higher-level sets, never need an extra flush
1160 * when writing an l1e. Because it points to the same guest frame
1161 * as the guest l1e did, it's the guest's responsibility to
1162 * trigger a flush later. */
1163 if ( shadow_mode_refcounts(d) )
1165 shadow_put_page_from_l1e(old_sl1e, d);
1168 return flags;
1172 /**************************************************************************/
1173 /* Macros to walk pagetables. These take the shadow of a pagetable and
1174 * walk every "interesting" entry. That is, they don't touch Xen mappings,
1175 * and for 32-bit l2s shadowed onto PAE or 64-bit, they only touch every
1176 * second entry (since pairs of entries are managed together). For multi-page
1177 * shadows they walk all pages.
1179 * Arguments are an MFN, the variable to point to each entry, a variable
1180 * to indicate that we are done (we will shortcut to the end of the scan
1181 * when _done != 0), a variable to indicate that we should avoid Xen mappings,
1182 * and the code.
1184 * WARNING: These macros have side-effects. They change the values of both
1185 * the pointer and the MFN. */
1187 static inline void increment_ptr_to_guest_entry(void *ptr)
1189 if ( ptr )
1191 guest_l1e_t **entry = ptr;
1192 (*entry)++;
1196 /* All kinds of l1: touch all entries */
1197 #define _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1198 do { \
1199 int _i; \
1200 shadow_l1e_t *_sp = map_shadow_page((_sl1mfn)); \
1201 ASSERT(mfn_to_shadow_page(_sl1mfn)->type == SH_type_l1_shadow \
1202 || mfn_to_shadow_page(_sl1mfn)->type == SH_type_fl1_shadow); \
1203 for ( _i = 0; _i < SHADOW_L1_PAGETABLE_ENTRIES; _i++ ) \
1204 { \
1205 (_sl1e) = _sp + _i; \
1206 if ( shadow_l1e_get_flags(*(_sl1e)) & _PAGE_PRESENT ) \
1207 {_code} \
1208 if ( _done ) break; \
1209 increment_ptr_to_guest_entry(_gl1p); \
1210 } \
1211 unmap_shadow_page(_sp); \
1212 } while (0)
1214 /* 32-bit l1, on PAE or 64-bit shadows: need to walk both pages of shadow */
1215 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1216 #define SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1217 do { \
1218 int __done = 0; \
1219 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, \
1220 ({ (__done = _done); }), _code); \
1221 _sl1mfn = _mfn(mfn_x(_sl1mfn) + 1); \
1222 if ( !__done ) \
1223 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, \
1224 ({ (__done = _done); }), _code); \
1225 } while (0)
1226 #else /* Everything else; l1 shadows are only one page */
1227 #define SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1228 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code)
1229 #endif
1232 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1234 /* 32-bit l2 on PAE/64: four pages, touch every second entry, and avoid Xen */
1235 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _xen, _code) \
1236 do { \
1237 int _i, _j, __done = 0; \
1238 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_32_shadow); \
1239 for ( _j = 0; _j < 4 && !__done; _j++ ) \
1240 { \
1241 shadow_l2e_t *_sp = map_shadow_page(_sl2mfn); \
1242 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i += 2 ) \
1243 if ( (!(_xen)) \
1244 || ((_j * SHADOW_L2_PAGETABLE_ENTRIES) + _i) \
1245 < (HYPERVISOR_VIRT_START >> SHADOW_L2_PAGETABLE_SHIFT) ) \
1246 { \
1247 (_sl2e) = _sp + _i; \
1248 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1249 {_code} \
1250 if ( (__done = (_done)) ) break; \
1251 increment_ptr_to_guest_entry(_gl2p); \
1252 } \
1253 unmap_shadow_page(_sp); \
1254 _sl2mfn = _mfn(mfn_x(_sl2mfn) + 1); \
1255 } \
1256 } while (0)
1258 #elif GUEST_PAGING_LEVELS == 2
1260 /* 32-bit on 32-bit: avoid Xen entries */
1261 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _xen, _code) \
1262 do { \
1263 int _i; \
1264 shadow_l2e_t *_sp = map_shadow_page((_sl2mfn)); \
1265 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_32_shadow); \
1266 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \
1267 if ( (!(_xen)) \
1268 || \
1269 (_i < (HYPERVISOR_VIRT_START >> SHADOW_L2_PAGETABLE_SHIFT)) ) \
1270 { \
1271 (_sl2e) = _sp + _i; \
1272 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1273 {_code} \
1274 if ( _done ) break; \
1275 increment_ptr_to_guest_entry(_gl2p); \
1276 } \
1277 unmap_shadow_page(_sp); \
1278 } while (0)
1280 #elif GUEST_PAGING_LEVELS == 3
1282 /* PAE: if it's an l2h, don't touch Xen mappings */
1283 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _xen, _code) \
1284 do { \
1285 int _i; \
1286 shadow_l2e_t *_sp = map_shadow_page((_sl2mfn)); \
1287 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_pae_shadow \
1288 || mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2h_pae_shadow);\
1289 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \
1290 if ( (!(_xen)) \
1291 || mfn_to_shadow_page(_sl2mfn)->type != SH_type_l2h_pae_shadow\
1292 || ((_i + (3 * SHADOW_L2_PAGETABLE_ENTRIES)) \
1293 < (HYPERVISOR_VIRT_START >> SHADOW_L2_PAGETABLE_SHIFT)) ) \
1294 { \
1295 (_sl2e) = _sp + _i; \
1296 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1297 {_code} \
1298 if ( _done ) break; \
1299 increment_ptr_to_guest_entry(_gl2p); \
1300 } \
1301 unmap_shadow_page(_sp); \
1302 } while (0)
1304 #else
1306 /* 64-bit l2: touch all entries */
1307 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _xen, _code) \
1308 do { \
1309 int _i; \
1310 shadow_l2e_t *_sp = map_shadow_page((_sl2mfn)); \
1311 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_64_shadow); \
1312 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \
1313 { \
1314 (_sl2e) = _sp + _i; \
1315 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1316 {_code} \
1317 if ( _done ) break; \
1318 increment_ptr_to_guest_entry(_gl2p); \
1319 } \
1320 unmap_shadow_page(_sp); \
1321 } while (0)
1323 #endif /* different kinds of l2 */
1325 #if GUEST_PAGING_LEVELS == 4
1327 /* 64-bit l3: touch all entries */
1328 #define SHADOW_FOREACH_L3E(_sl3mfn, _sl3e, _gl3p, _done, _code) \
1329 do { \
1330 int _i; \
1331 shadow_l3e_t *_sp = map_shadow_page((_sl3mfn)); \
1332 ASSERT(mfn_to_shadow_page(_sl3mfn)->type == SH_type_l3_64_shadow); \
1333 for ( _i = 0; _i < SHADOW_L3_PAGETABLE_ENTRIES; _i++ ) \
1334 { \
1335 (_sl3e) = _sp + _i; \
1336 if ( shadow_l3e_get_flags(*(_sl3e)) & _PAGE_PRESENT ) \
1337 {_code} \
1338 if ( _done ) break; \
1339 increment_ptr_to_guest_entry(_gl3p); \
1340 } \
1341 unmap_shadow_page(_sp); \
1342 } while (0)
1344 /* 64-bit l4: avoid Xen mappings */
1345 #define SHADOW_FOREACH_L4E(_sl4mfn, _sl4e, _gl4p, _done, _xen, _code) \
1346 do { \
1347 int _i; \
1348 shadow_l4e_t *_sp = map_shadow_page((_sl4mfn)); \
1349 ASSERT(mfn_to_shadow_page(_sl4mfn)->type == SH_type_l4_64_shadow); \
1350 for ( _i = 0; _i < SHADOW_L4_PAGETABLE_ENTRIES; _i++ ) \
1351 { \
1352 if ( (!(_xen)) || is_guest_l4_slot(_i) ) \
1353 { \
1354 (_sl4e) = _sp + _i; \
1355 if ( shadow_l4e_get_flags(*(_sl4e)) & _PAGE_PRESENT ) \
1356 {_code} \
1357 if ( _done ) break; \
1358 } \
1359 increment_ptr_to_guest_entry(_gl4p); \
1360 } \
1361 unmap_shadow_page(_sp); \
1362 } while (0)
1364 #endif
1368 /**************************************************************************/
1369 /* Functions to install Xen mappings and linear mappings in shadow pages */
1371 // XXX -- this function should probably be moved to shadow-common.c, but that
1372 // probably wants to wait until the shadow types have been moved from
1373 // shadow-types.h to shadow-private.h
1374 //
1375 #if CONFIG_PAGING_LEVELS == 4 && GUEST_PAGING_LEVELS == 4
1376 void sh_install_xen_entries_in_l4(struct vcpu *v, mfn_t gl4mfn, mfn_t sl4mfn)
1378 struct domain *d = v->domain;
1379 shadow_l4e_t *sl4e;
1381 sl4e = sh_map_domain_page(sl4mfn);
1382 ASSERT(sl4e != NULL);
1383 ASSERT(sizeof (l4_pgentry_t) == sizeof (shadow_l4e_t));
1385 /* Copy the common Xen mappings from the idle domain */
1386 memcpy(&sl4e[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1387 &idle_pg_table[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1388 ROOT_PAGETABLE_XEN_SLOTS * sizeof(l4_pgentry_t));
1390 /* Install the per-domain mappings for this domain */
1391 sl4e[shadow_l4_table_offset(PERDOMAIN_VIRT_START)] =
1392 shadow_l4e_from_mfn(page_to_mfn(virt_to_page(d->arch.mm_perdomain_l3)),
1393 __PAGE_HYPERVISOR);
1395 /* Linear mapping */
1396 sl4e[shadow_l4_table_offset(SH_LINEAR_PT_VIRT_START)] =
1397 shadow_l4e_from_mfn(sl4mfn, __PAGE_HYPERVISOR);
1399 if ( shadow_mode_translate(v->domain) && !shadow_mode_external(v->domain) )
1401 // linear tables may not be used with translated PV guests
1402 sl4e[shadow_l4_table_offset(LINEAR_PT_VIRT_START)] =
1403 shadow_l4e_empty();
1405 else
1407 sl4e[shadow_l4_table_offset(LINEAR_PT_VIRT_START)] =
1408 shadow_l4e_from_mfn(gl4mfn, __PAGE_HYPERVISOR);
1411 if ( shadow_mode_translate(v->domain) )
1413 /* install domain-specific P2M table */
1414 sl4e[shadow_l4_table_offset(RO_MPT_VIRT_START)] =
1415 shadow_l4e_from_mfn(pagetable_get_mfn(d->arch.phys_table),
1416 __PAGE_HYPERVISOR);
1419 sh_unmap_domain_page(sl4e);
1421 #endif
1423 #if (CONFIG_PAGING_LEVELS == 3 || defined(CONFIG_COMPAT)) && GUEST_PAGING_LEVELS == 3
1424 // For 3-on-3 PV guests, we need to make sure the xen mappings are in
1425 // place, which means that we need to populate the l2h entry in the l3
1426 // table.
1428 void sh_install_xen_entries_in_l2h(struct vcpu *v,
1429 mfn_t sl2hmfn)
1431 struct domain *d = v->domain;
1432 shadow_l2e_t *sl2e;
1433 #if CONFIG_PAGING_LEVELS == 3
1434 int i;
1435 #else
1437 if ( !pv_32bit_guest(v) )
1438 return;
1439 #endif
1441 sl2e = sh_map_domain_page(sl2hmfn);
1442 ASSERT(sl2e != NULL);
1443 ASSERT(sizeof (l2_pgentry_t) == sizeof (shadow_l2e_t));
1445 #if CONFIG_PAGING_LEVELS == 3
1447 /* Copy the common Xen mappings from the idle domain */
1448 memcpy(&sl2e[L2_PAGETABLE_FIRST_XEN_SLOT & (L2_PAGETABLE_ENTRIES-1)],
1449 &idle_pg_table_l2[L2_PAGETABLE_FIRST_XEN_SLOT],
1450 L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t));
1452 /* Install the per-domain mappings for this domain */
1453 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
1454 sl2e[shadow_l2_table_offset(PERDOMAIN_VIRT_START) + i] =
1455 shadow_l2e_from_mfn(
1456 page_to_mfn(virt_to_page(d->arch.mm_perdomain_pt) + i),
1457 __PAGE_HYPERVISOR);
1459 /* We don't set up a linear mapping here because we can't until this
1460 * l2h is installed in an l3e. sh_update_linear_entries() handles
1461 * the linear mappings when CR3 (and so the fourth l3e) is loaded.
1462 * We zero them here, just as a safety measure.
1463 */
1464 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
1465 sl2e[shadow_l2_table_offset(LINEAR_PT_VIRT_START) + i] =
1466 shadow_l2e_empty();
1467 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
1468 sl2e[shadow_l2_table_offset(SH_LINEAR_PT_VIRT_START) + i] =
1469 shadow_l2e_empty();
1471 if ( shadow_mode_translate(d) )
1473 /* Install the domain-specific p2m table */
1474 l3_pgentry_t *p2m;
1475 ASSERT(pagetable_get_pfn(d->arch.phys_table) != 0);
1476 p2m = sh_map_domain_page(pagetable_get_mfn(d->arch.phys_table));
1477 for ( i = 0; i < MACHPHYS_MBYTES>>1; i++ )
1479 sl2e[shadow_l2_table_offset(RO_MPT_VIRT_START) + i] =
1480 (l3e_get_flags(p2m[i]) & _PAGE_PRESENT)
1481 ? shadow_l2e_from_mfn(_mfn(l3e_get_pfn(p2m[i])),
1482 __PAGE_HYPERVISOR)
1483 : shadow_l2e_empty();
1485 sh_unmap_domain_page(p2m);
1488 #else
1490 /* Copy the common Xen mappings from the idle domain */
1491 memcpy(&sl2e[COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(d)],
1492 &compat_idle_pg_table_l2[l2_table_offset(HIRO_COMPAT_MPT_VIRT_START)],
1493 COMPAT_L2_PAGETABLE_XEN_SLOTS(d) * sizeof(*sl2e));
1495 #endif
1497 sh_unmap_domain_page(sl2e);
1499 #endif
1502 #if CONFIG_PAGING_LEVELS == 2 && GUEST_PAGING_LEVELS == 2
1503 void sh_install_xen_entries_in_l2(struct vcpu *v, mfn_t gl2mfn, mfn_t sl2mfn)
1505 struct domain *d = v->domain;
1506 shadow_l2e_t *sl2e;
1507 int i;
1509 sl2e = sh_map_domain_page(sl2mfn);
1510 ASSERT(sl2e != NULL);
1511 ASSERT(sizeof (l2_pgentry_t) == sizeof (shadow_l2e_t));
1513 /* Copy the common Xen mappings from the idle domain */
1514 memcpy(&sl2e[L2_PAGETABLE_FIRST_XEN_SLOT],
1515 &idle_pg_table[L2_PAGETABLE_FIRST_XEN_SLOT],
1516 L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t));
1518 /* Install the per-domain mappings for this domain */
1519 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
1520 sl2e[shadow_l2_table_offset(PERDOMAIN_VIRT_START) + i] =
1521 shadow_l2e_from_mfn(
1522 page_to_mfn(virt_to_page(d->arch.mm_perdomain_pt) + i),
1523 __PAGE_HYPERVISOR);
1525 /* Linear mapping */
1526 sl2e[shadow_l2_table_offset(SH_LINEAR_PT_VIRT_START)] =
1527 shadow_l2e_from_mfn(sl2mfn, __PAGE_HYPERVISOR);
1529 if ( shadow_mode_translate(v->domain) && !shadow_mode_external(v->domain) )
1531 // linear tables may not be used with translated PV guests
1532 sl2e[shadow_l2_table_offset(LINEAR_PT_VIRT_START)] =
1533 shadow_l2e_empty();
1535 else
1537 sl2e[shadow_l2_table_offset(LINEAR_PT_VIRT_START)] =
1538 shadow_l2e_from_mfn(gl2mfn, __PAGE_HYPERVISOR);
1541 if ( shadow_mode_translate(d) )
1543 /* install domain-specific P2M table */
1544 sl2e[shadow_l2_table_offset(RO_MPT_VIRT_START)] =
1545 shadow_l2e_from_mfn(pagetable_get_mfn(d->arch.phys_table),
1546 __PAGE_HYPERVISOR);
1549 sh_unmap_domain_page(sl2e);
1551 #endif
1555 /**************************************************************************/
1556 /* Create a shadow of a given guest page.
1557 */
1558 static mfn_t
1559 sh_make_shadow(struct vcpu *v, mfn_t gmfn, u32 shadow_type)
1561 mfn_t smfn = shadow_alloc(v->domain, shadow_type, mfn_x(gmfn));
1562 SHADOW_DEBUG(MAKE_SHADOW, "(%05lx, %u)=>%05lx\n",
1563 mfn_x(gmfn), shadow_type, mfn_x(smfn));
1565 if ( shadow_type != SH_type_l2_32_shadow
1566 && shadow_type != SH_type_l2_pae_shadow
1567 && shadow_type != SH_type_l2h_pae_shadow
1568 && shadow_type != SH_type_l4_64_shadow )
1569 /* Lower-level shadow, not yet linked form a higher level */
1570 mfn_to_shadow_page(smfn)->up = 0;
1572 #if GUEST_PAGING_LEVELS == 4
1573 #if (SHADOW_OPTIMIZATIONS & SHOPT_LINUX_L3_TOPLEVEL)
1574 if ( shadow_type == SH_type_l4_64_shadow &&
1575 unlikely(v->domain->arch.shadow.opt_flags & SHOPT_LINUX_L3_TOPLEVEL) )
1577 /* We're shadowing a new l4, but we've been assuming the guest uses
1578 * only one l4 per vcpu and context switches using an l4 entry.
1579 * Count the number of active l4 shadows. If there are enough
1580 * of them, decide that this isn't an old linux guest, and stop
1581 * pinning l3es. This is not very quick but it doesn't happen
1582 * very often. */
1583 struct list_head *l, *t;
1584 struct shadow_page_info *sp;
1585 struct vcpu *v2;
1586 int l4count = 0, vcpus = 0;
1587 list_for_each(l, &v->domain->arch.shadow.pinned_shadows)
1589 sp = list_entry(l, struct shadow_page_info, list);
1590 if ( sp->type == SH_type_l4_64_shadow )
1591 l4count++;
1593 for_each_vcpu ( v->domain, v2 )
1594 vcpus++;
1595 if ( l4count > 2 * vcpus )
1597 /* Unpin all the pinned l3 tables, and don't pin any more. */
1598 list_for_each_safe(l, t, &v->domain->arch.shadow.pinned_shadows)
1600 sp = list_entry(l, struct shadow_page_info, list);
1601 if ( sp->type == SH_type_l3_64_shadow )
1602 sh_unpin(v, shadow_page_to_mfn(sp));
1604 v->domain->arch.shadow.opt_flags &= ~SHOPT_LINUX_L3_TOPLEVEL;
1607 #endif
1608 #endif
1610 // Create the Xen mappings...
1611 if ( !shadow_mode_external(v->domain) )
1613 switch (shadow_type)
1615 #if CONFIG_PAGING_LEVELS == 4 && GUEST_PAGING_LEVELS == 4
1616 case SH_type_l4_shadow:
1617 sh_install_xen_entries_in_l4(v, gmfn, smfn); break;
1618 #endif
1619 #if CONFIG_PAGING_LEVELS == 3 && GUEST_PAGING_LEVELS == 3
1620 case SH_type_l2h_shadow:
1621 sh_install_xen_entries_in_l2h(v, smfn); break;
1622 #endif
1623 #if CONFIG_PAGING_LEVELS == 2 && GUEST_PAGING_LEVELS == 2
1624 case SH_type_l2_shadow:
1625 sh_install_xen_entries_in_l2(v, gmfn, smfn); break;
1626 #endif
1627 default: /* Do nothing */ break;
1631 shadow_promote(v, gmfn, shadow_type);
1632 set_shadow_status(v, gmfn, shadow_type, smfn);
1634 return smfn;
1637 /* Make a splintered superpage shadow */
1638 static mfn_t
1639 make_fl1_shadow(struct vcpu *v, gfn_t gfn)
1641 mfn_t smfn = shadow_alloc(v->domain, SH_type_fl1_shadow,
1642 (unsigned long) gfn_x(gfn));
1644 SHADOW_DEBUG(MAKE_SHADOW, "(%" SH_PRI_gfn ")=>%" SH_PRI_mfn "\n",
1645 gfn_x(gfn), mfn_x(smfn));
1647 set_fl1_shadow_status(v, gfn, smfn);
1648 return smfn;
1652 #if SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS
1653 mfn_t
1654 sh_make_monitor_table(struct vcpu *v)
1656 struct domain *d = v->domain;
1658 ASSERT(pagetable_get_pfn(v->arch.monitor_table) == 0);
1660 /* Guarantee we can get the memory we need */
1661 shadow_prealloc(d, SHADOW_MAX_ORDER);
1663 #if CONFIG_PAGING_LEVELS == 4
1665 mfn_t m4mfn;
1666 m4mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1667 sh_install_xen_entries_in_l4(v, m4mfn, m4mfn);
1668 /* Remember the level of this table */
1669 mfn_to_page(m4mfn)->shadow_flags = 4;
1670 #if SHADOW_PAGING_LEVELS < 4
1671 // Install a monitor l3 table in slot 0 of the l4 table.
1672 // This is used for shadow linear maps.
1674 mfn_t m3mfn;
1675 l4_pgentry_t *l4e;
1676 m3mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1677 mfn_to_page(m3mfn)->shadow_flags = 3;
1678 l4e = sh_map_domain_page(m4mfn);
1679 l4e[0] = l4e_from_pfn(mfn_x(m3mfn), __PAGE_HYPERVISOR);
1680 sh_unmap_domain_page(l4e);
1681 if ( pv_32bit_guest(v) )
1683 // Install a monitor l2 table in slot 3 of the l3 table.
1684 // This is used for all Xen entries.
1685 mfn_t m2mfn;
1686 l3_pgentry_t *l3e;
1687 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1688 mfn_to_page(m2mfn)->shadow_flags = 2;
1689 l3e = sh_map_domain_page(m3mfn);
1690 l3e[3] = l3e_from_pfn(mfn_x(m2mfn), _PAGE_PRESENT);
1691 sh_install_xen_entries_in_l2h(v, m2mfn);
1692 sh_unmap_domain_page(l3e);
1695 #endif /* SHADOW_PAGING_LEVELS < 4 */
1696 return m4mfn;
1699 #elif CONFIG_PAGING_LEVELS == 3
1702 mfn_t m3mfn, m2mfn;
1703 l3_pgentry_t *l3e;
1704 l2_pgentry_t *l2e;
1705 int i;
1707 m3mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1708 /* Remember the level of this table */
1709 mfn_to_page(m3mfn)->shadow_flags = 3;
1711 // Install a monitor l2 table in slot 3 of the l3 table.
1712 // This is used for all Xen entries, including linear maps
1713 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1714 mfn_to_page(m2mfn)->shadow_flags = 2;
1715 l3e = sh_map_domain_page(m3mfn);
1716 l3e[3] = l3e_from_pfn(mfn_x(m2mfn), _PAGE_PRESENT);
1717 sh_install_xen_entries_in_l2h(v, m2mfn);
1718 /* Install the monitor's own linear map */
1719 l2e = sh_map_domain_page(m2mfn);
1720 for ( i = 0; i < L3_PAGETABLE_ENTRIES; i++ )
1721 l2e[l2_table_offset(LINEAR_PT_VIRT_START) + i] =
1722 (l3e_get_flags(l3e[i]) & _PAGE_PRESENT)
1723 ? l2e_from_pfn(l3e_get_pfn(l3e[i]), __PAGE_HYPERVISOR)
1724 : l2e_empty();
1725 sh_unmap_domain_page(l2e);
1726 sh_unmap_domain_page(l3e);
1728 SHADOW_PRINTK("new monitor table: %#lx\n", mfn_x(m3mfn));
1729 return m3mfn;
1732 #elif CONFIG_PAGING_LEVELS == 2
1735 mfn_t m2mfn;
1736 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1737 sh_install_xen_entries_in_l2(v, m2mfn, m2mfn);
1738 /* Remember the level of this table */
1739 mfn_to_page(m2mfn)->shadow_flags = 2;
1740 return m2mfn;
1743 #else
1744 #error this should not happen
1745 #endif /* CONFIG_PAGING_LEVELS */
1747 #endif /* SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS */
1749 /**************************************************************************/
1750 /* These functions also take a virtual address and return the level-N
1751 * shadow table mfn and entry, but they create the shadow pagetables if
1752 * they are needed. The "demand" argument is non-zero when handling
1753 * a demand fault (so we know what to do about accessed bits &c).
1754 * If the necessary tables are not present in the guest, they return NULL. */
1756 /* N.B. The use of GUEST_PAGING_LEVELS here is correct. If the shadow has
1757 * more levels than the guest, the upper levels are always fixed and do not
1758 * reflect any information from the guest, so we do not use these functions
1759 * to access them. */
1761 #if GUEST_PAGING_LEVELS >= 4
1762 static shadow_l4e_t * shadow_get_and_create_l4e(struct vcpu *v,
1763 walk_t *gw,
1764 mfn_t *sl4mfn)
1766 /* There is always a shadow of the top level table. Get it. */
1767 *sl4mfn = pagetable_get_mfn(v->arch.shadow_table[0]);
1768 /* Reading the top level table is always valid. */
1769 return sh_linear_l4_table(v) + shadow_l4_linear_offset(gw->va);
1772 static shadow_l3e_t * shadow_get_and_create_l3e(struct vcpu *v,
1773 walk_t *gw,
1774 mfn_t *sl3mfn,
1775 fetch_type_t ft)
1777 mfn_t sl4mfn;
1778 shadow_l4e_t *sl4e;
1779 if ( !mfn_valid(gw->l3mfn) ) return NULL; /* No guest page. */
1780 /* Get the l4e */
1781 sl4e = shadow_get_and_create_l4e(v, gw, &sl4mfn);
1782 ASSERT(sl4e != NULL);
1783 if ( shadow_l4e_get_flags(*sl4e) & _PAGE_PRESENT )
1785 *sl3mfn = shadow_l4e_get_mfn(*sl4e);
1786 ASSERT(mfn_valid(*sl3mfn));
1788 else
1790 int r;
1791 shadow_l4e_t new_sl4e;
1792 /* No l3 shadow installed: find and install it. */
1793 *sl3mfn = get_shadow_status(v, gw->l3mfn, SH_type_l3_shadow);
1794 if ( !mfn_valid(*sl3mfn) )
1796 /* No l3 shadow of this page exists at all: make one. */
1797 *sl3mfn = sh_make_shadow(v, gw->l3mfn, SH_type_l3_shadow);
1799 /* Install the new sl3 table in the sl4e */
1800 l4e_propagate_from_guest(v, gw->l4e, gw->l4mfn,
1801 *sl3mfn, &new_sl4e, ft);
1802 r = shadow_set_l4e(v, sl4e, new_sl4e, sl4mfn);
1803 ASSERT((r & SHADOW_SET_FLUSH) == 0);
1804 if ( r & SHADOW_SET_ERROR )
1805 return NULL;
1807 /* Now follow it down a level. Guaranteed to succeed. */
1808 return sh_linear_l3_table(v) + shadow_l3_linear_offset(gw->va);
1810 #endif /* GUEST_PAGING_LEVELS >= 4 */
1813 static shadow_l2e_t * shadow_get_and_create_l2e(struct vcpu *v,
1814 walk_t *gw,
1815 mfn_t *sl2mfn,
1816 fetch_type_t ft)
1818 #if GUEST_PAGING_LEVELS >= 4 /* 64bit... */
1819 mfn_t sl3mfn = _mfn(INVALID_MFN);
1820 shadow_l3e_t *sl3e;
1821 if ( !mfn_valid(gw->l2mfn) ) return NULL; /* No guest page. */
1822 /* Get the l3e */
1823 sl3e = shadow_get_and_create_l3e(v, gw, &sl3mfn, ft);
1824 if ( sl3e == NULL ) return NULL;
1825 if ( shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT )
1827 *sl2mfn = shadow_l3e_get_mfn(*sl3e);
1828 ASSERT(mfn_valid(*sl2mfn));
1830 else
1832 int r;
1833 shadow_l3e_t new_sl3e;
1834 /* No l2 shadow installed: find and install it. */
1835 *sl2mfn = get_shadow_status(v, gw->l2mfn, SH_type_l2_shadow);
1836 if ( !mfn_valid(*sl2mfn) )
1838 /* No l2 shadow of this page exists at all: make one. */
1839 *sl2mfn = sh_make_shadow(v, gw->l2mfn, SH_type_l2_shadow);
1841 /* Install the new sl2 table in the sl3e */
1842 l3e_propagate_from_guest(v, gw->l3e, gw->l3mfn,
1843 *sl2mfn, &new_sl3e, ft);
1844 r = shadow_set_l3e(v, sl3e, new_sl3e, sl3mfn);
1845 ASSERT((r & SHADOW_SET_FLUSH) == 0);
1846 if ( r & SHADOW_SET_ERROR )
1847 return NULL;
1849 /* Now follow it down a level. Guaranteed to succeed. */
1850 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
1851 #elif GUEST_PAGING_LEVELS == 3 /* PAE... */
1852 /* We never demand-shadow PAE l3es: they are only created in
1853 * sh_update_cr3(). Check if the relevant sl3e is present. */
1854 shadow_l3e_t *sl3e = ((shadow_l3e_t *)&v->arch.shadow.l3table)
1855 + shadow_l3_linear_offset(gw->va);
1856 if ( !(shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT) )
1857 return NULL;
1858 *sl2mfn = shadow_l3e_get_mfn(*sl3e);
1859 ASSERT(mfn_valid(*sl2mfn));
1860 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
1861 #else /* 32bit... */
1862 /* There is always a shadow of the top level table. Get it. */
1863 *sl2mfn = pagetable_get_mfn(v->arch.shadow_table[0]);
1864 /* This next line is important: the guest l2 has a 16k
1865 * shadow, we need to return the right mfn of the four. This
1866 * call will set it for us as a side-effect. */
1867 (void) shadow_l2_index(sl2mfn, guest_index(gw->l2e));
1868 /* Reading the top level table is always valid. */
1869 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
1870 #endif
1874 static shadow_l1e_t * shadow_get_and_create_l1e(struct vcpu *v,
1875 walk_t *gw,
1876 mfn_t *sl1mfn,
1877 fetch_type_t ft)
1879 mfn_t sl2mfn;
1880 shadow_l2e_t *sl2e;
1882 /* Get the l2e */
1883 sl2e = shadow_get_and_create_l2e(v, gw, &sl2mfn, ft);
1884 if ( sl2e == NULL ) return NULL;
1885 /* Install the sl1 in the l2e if it wasn't there or if we need to
1886 * re-do it to fix a PSE dirty bit. */
1887 if ( shadow_l2e_get_flags(*sl2e) & _PAGE_PRESENT
1888 && likely(ft != ft_demand_write
1889 || (guest_l2e_get_flags(*gw->l2e) & _PAGE_DIRTY)
1890 || !(guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE)) )
1892 *sl1mfn = shadow_l2e_get_mfn(*sl2e);
1893 ASSERT(mfn_valid(*sl1mfn));
1895 else
1897 shadow_l2e_t new_sl2e;
1898 int r, flags = guest_l2e_get_flags(*gw->l2e);
1899 /* No l1 shadow installed: find and install it. */
1900 if ( !(flags & _PAGE_PRESENT) )
1901 return NULL; /* No guest page. */
1902 if ( guest_supports_superpages(v) && (flags & _PAGE_PSE) )
1904 /* Splintering a superpage */
1905 gfn_t l2gfn = guest_l2e_get_gfn(*gw->l2e);
1906 *sl1mfn = get_fl1_shadow_status(v, l2gfn);
1907 if ( !mfn_valid(*sl1mfn) )
1909 /* No fl1 shadow of this superpage exists at all: make one. */
1910 *sl1mfn = make_fl1_shadow(v, l2gfn);
1913 else
1915 /* Shadowing an actual guest l1 table */
1916 if ( !mfn_valid(gw->l2mfn) ) return NULL; /* No guest page. */
1917 *sl1mfn = get_shadow_status(v, gw->l1mfn, SH_type_l1_shadow);
1918 if ( !mfn_valid(*sl1mfn) )
1920 /* No l1 shadow of this page exists at all: make one. */
1921 *sl1mfn = sh_make_shadow(v, gw->l1mfn, SH_type_l1_shadow);
1924 /* Install the new sl1 table in the sl2e */
1925 l2e_propagate_from_guest(v, gw->l2e, gw->l2mfn,
1926 *sl1mfn, &new_sl2e, ft);
1927 r = shadow_set_l2e(v, sl2e, new_sl2e, sl2mfn);
1928 ASSERT((r & SHADOW_SET_FLUSH) == 0);
1929 if ( r & SHADOW_SET_ERROR )
1930 return NULL;
1931 /* This next line is important: in 32-on-PAE and 32-on-64 modes,
1932 * the guest l1 table has an 8k shadow, and we need to return
1933 * the right mfn of the pair. This call will set it for us as a
1934 * side-effect. (In all other cases, it's a no-op and will be
1935 * compiled out.) */
1936 (void) shadow_l1_index(sl1mfn, guest_l1_table_offset(gw->va));
1938 /* Now follow it down a level. Guaranteed to succeed. */
1939 return sh_linear_l1_table(v) + shadow_l1_linear_offset(gw->va);
1944 /**************************************************************************/
1945 /* Destructors for shadow tables:
1946 * Unregister the shadow, decrement refcounts of any entries present in it,
1947 * and release the memory.
1949 * N.B. These destructors do not clear the contents of the shadows.
1950 * This allows us to delay TLB shootdowns until the page is being reused.
1951 * See shadow_alloc() and shadow_free() for how this is handled.
1952 */
1954 #if GUEST_PAGING_LEVELS >= 4
1955 void sh_destroy_l4_shadow(struct vcpu *v, mfn_t smfn)
1957 shadow_l4e_t *sl4e;
1958 u32 t = mfn_to_shadow_page(smfn)->type;
1959 mfn_t gmfn, sl4mfn;
1960 int xen_mappings;
1962 SHADOW_DEBUG(DESTROY_SHADOW,
1963 "%s(%05lx)\n", __func__, mfn_x(smfn));
1964 ASSERT(t == SH_type_l4_shadow);
1966 /* Record that the guest page isn't shadowed any more (in this type) */
1967 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
1968 delete_shadow_status(v, gmfn, t, smfn);
1969 shadow_demote(v, gmfn, t);
1970 /* Decrement refcounts of all the old entries */
1971 xen_mappings = (!shadow_mode_external(v->domain));
1972 sl4mfn = smfn;
1973 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, 0, xen_mappings, {
1974 if ( shadow_l4e_get_flags(*sl4e) & _PAGE_PRESENT )
1976 sh_put_ref(v, shadow_l4e_get_mfn(*sl4e),
1977 (((paddr_t)mfn_x(sl4mfn)) << PAGE_SHIFT)
1978 | ((unsigned long)sl4e & ~PAGE_MASK));
1980 });
1982 /* Put the memory back in the pool */
1983 shadow_free(v->domain, smfn);
1986 void sh_destroy_l3_shadow(struct vcpu *v, mfn_t smfn)
1988 shadow_l3e_t *sl3e;
1989 u32 t = mfn_to_shadow_page(smfn)->type;
1990 mfn_t gmfn, sl3mfn;
1992 SHADOW_DEBUG(DESTROY_SHADOW,
1993 "%s(%05lx)\n", __func__, mfn_x(smfn));
1994 ASSERT(t == SH_type_l3_shadow);
1996 /* Record that the guest page isn't shadowed any more (in this type) */
1997 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
1998 delete_shadow_status(v, gmfn, t, smfn);
1999 shadow_demote(v, gmfn, t);
2001 /* Decrement refcounts of all the old entries */
2002 sl3mfn = smfn;
2003 SHADOW_FOREACH_L3E(sl3mfn, sl3e, 0, 0, {
2004 if ( shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT )
2005 sh_put_ref(v, shadow_l3e_get_mfn(*sl3e),
2006 (((paddr_t)mfn_x(sl3mfn)) << PAGE_SHIFT)
2007 | ((unsigned long)sl3e & ~PAGE_MASK));
2008 });
2010 /* Put the memory back in the pool */
2011 shadow_free(v->domain, smfn);
2013 #endif /* GUEST_PAGING_LEVELS >= 4 */
2016 void sh_destroy_l2_shadow(struct vcpu *v, mfn_t smfn)
2018 shadow_l2e_t *sl2e;
2019 u32 t = mfn_to_shadow_page(smfn)->type;
2020 mfn_t gmfn, sl2mfn;
2021 int xen_mappings;
2023 SHADOW_DEBUG(DESTROY_SHADOW,
2024 "%s(%05lx)\n", __func__, mfn_x(smfn));
2025 ASSERT(t == SH_type_l2_shadow
2026 || t == SH_type_l2h_pae_shadow);
2028 /* Record that the guest page isn't shadowed any more (in this type) */
2029 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2030 delete_shadow_status(v, gmfn, t, smfn);
2031 shadow_demote(v, gmfn, t);
2033 /* Decrement refcounts of all the old entries */
2034 sl2mfn = smfn;
2035 xen_mappings = (!shadow_mode_external(v->domain) &&
2036 ((GUEST_PAGING_LEVELS == 2) ||
2037 ((GUEST_PAGING_LEVELS == 3) &&
2038 (t == SH_type_l2h_pae_shadow))));
2039 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, xen_mappings, {
2040 if ( shadow_l2e_get_flags(*sl2e) & _PAGE_PRESENT )
2041 sh_put_ref(v, shadow_l2e_get_mfn(*sl2e),
2042 (((paddr_t)mfn_x(sl2mfn)) << PAGE_SHIFT)
2043 | ((unsigned long)sl2e & ~PAGE_MASK));
2044 });
2046 /* Put the memory back in the pool */
2047 shadow_free(v->domain, smfn);
2050 void sh_destroy_l1_shadow(struct vcpu *v, mfn_t smfn)
2052 struct domain *d = v->domain;
2053 shadow_l1e_t *sl1e;
2054 u32 t = mfn_to_shadow_page(smfn)->type;
2056 SHADOW_DEBUG(DESTROY_SHADOW,
2057 "%s(%05lx)\n", __func__, mfn_x(smfn));
2058 ASSERT(t == SH_type_l1_shadow || t == SH_type_fl1_shadow);
2060 /* Record that the guest page isn't shadowed any more (in this type) */
2061 if ( t == SH_type_fl1_shadow )
2063 gfn_t gfn = _gfn(mfn_to_shadow_page(smfn)->backpointer);
2064 delete_fl1_shadow_status(v, gfn, smfn);
2066 else
2068 mfn_t gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2069 delete_shadow_status(v, gmfn, t, smfn);
2070 shadow_demote(v, gmfn, t);
2073 if ( shadow_mode_refcounts(d) )
2075 /* Decrement refcounts of all the old entries */
2076 mfn_t sl1mfn = smfn;
2077 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, 0, {
2078 if ( (shadow_l1e_get_flags(*sl1e) & _PAGE_PRESENT)
2079 && !sh_l1e_is_magic(*sl1e) )
2080 shadow_put_page_from_l1e(*sl1e, d);
2081 });
2084 /* Put the memory back in the pool */
2085 shadow_free(v->domain, smfn);
2088 #if SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS
2089 void sh_destroy_monitor_table(struct vcpu *v, mfn_t mmfn)
2091 struct domain *d = v->domain;
2092 ASSERT(mfn_to_shadow_page(mmfn)->type == SH_type_monitor_table);
2094 #if (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS != 4)
2095 /* Need to destroy the l3 monitor page in slot 0 too */
2097 mfn_t m3mfn;
2098 l4_pgentry_t *l4e = sh_map_domain_page(mmfn);
2099 ASSERT(l4e_get_flags(l4e[0]) & _PAGE_PRESENT);
2100 m3mfn = _mfn(l4e_get_pfn(l4e[0]));
2101 if ( pv_32bit_guest(v) )
2103 /* Need to destroy the l2 monitor page in slot 3 too */
2104 l3_pgentry_t *l3e = sh_map_domain_page(m3mfn);
2105 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
2106 shadow_free(d, _mfn(l3e_get_pfn(l3e[3])));
2107 sh_unmap_domain_page(l3e);
2109 shadow_free(d, m3mfn);
2110 sh_unmap_domain_page(l4e);
2112 #elif CONFIG_PAGING_LEVELS == 3
2113 /* Need to destroy the l2 monitor page in slot 4 too */
2115 l3_pgentry_t *l3e = sh_map_domain_page(mmfn);
2116 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
2117 shadow_free(d, _mfn(l3e_get_pfn(l3e[3])));
2118 sh_unmap_domain_page(l3e);
2120 #endif
2122 /* Put the memory back in the pool */
2123 shadow_free(d, mmfn);
2125 #endif
2127 /**************************************************************************/
2128 /* Functions to destroy non-Xen mappings in a pagetable hierarchy.
2129 * These are called from common code when we are running out of shadow
2130 * memory, and unpinning all the top-level shadows hasn't worked.
2132 * This implementation is pretty crude and slow, but we hope that it won't
2133 * be called very often. */
2135 #if GUEST_PAGING_LEVELS == 2
2137 void sh_unhook_32b_mappings(struct vcpu *v, mfn_t sl2mfn)
2139 shadow_l2e_t *sl2e;
2140 int xen_mappings = !shadow_mode_external(v->domain);
2141 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, xen_mappings, {
2142 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
2143 });
2146 #elif GUEST_PAGING_LEVELS == 3
2148 void sh_unhook_pae_mappings(struct vcpu *v, mfn_t sl2mfn)
2149 /* Walk a PAE l2 shadow, unhooking entries from all the subshadows */
2151 shadow_l2e_t *sl2e;
2152 int xen_mappings = !shadow_mode_external(v->domain);
2153 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, xen_mappings, {
2154 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
2155 });
2158 #elif GUEST_PAGING_LEVELS == 4
2160 void sh_unhook_64b_mappings(struct vcpu *v, mfn_t sl4mfn)
2162 shadow_l4e_t *sl4e;
2163 int xen_mappings = !shadow_mode_external(v->domain);
2164 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, 0, xen_mappings, {
2165 (void) shadow_set_l4e(v, sl4e, shadow_l4e_empty(), sl4mfn);
2166 });
2169 #endif
2171 /**************************************************************************/
2172 /* Internal translation functions.
2173 * These functions require a pointer to the shadow entry that will be updated.
2174 */
2176 /* These functions take a new guest entry, translate it to shadow and write
2177 * the shadow entry.
2179 * They return the same bitmaps as the shadow_set_lXe() functions.
2180 */
2182 #if GUEST_PAGING_LEVELS >= 4
2183 static int validate_gl4e(struct vcpu *v, void *new_ge, mfn_t sl4mfn, void *se)
2185 shadow_l4e_t new_sl4e;
2186 guest_l4e_t *new_gl4e = new_ge;
2187 shadow_l4e_t *sl4p = se;
2188 mfn_t sl3mfn = _mfn(INVALID_MFN);
2189 int result = 0;
2191 perfc_incrc(shadow_validate_gl4e_calls);
2193 if ( guest_l4e_get_flags(*new_gl4e) & _PAGE_PRESENT )
2195 gfn_t gl3gfn = guest_l4e_get_gfn(*new_gl4e);
2196 mfn_t gl3mfn = vcpu_gfn_to_mfn(v, gl3gfn);
2197 if ( mfn_valid(gl3mfn) )
2198 sl3mfn = get_shadow_status(v, gl3mfn, SH_type_l3_shadow);
2199 else
2200 result |= SHADOW_SET_ERROR;
2202 l4e_propagate_from_guest(v, new_gl4e, _mfn(INVALID_MFN),
2203 sl3mfn, &new_sl4e, ft_prefetch);
2205 // check for updates to xen reserved slots
2206 if ( !shadow_mode_external(v->domain) )
2208 int shadow_index = (((unsigned long)sl4p & ~PAGE_MASK) /
2209 sizeof(shadow_l4e_t));
2210 int reserved_xen_slot = !is_guest_l4_slot(shadow_index);
2212 if ( unlikely(reserved_xen_slot) )
2214 // attempt by the guest to write to a xen reserved slot
2215 //
2216 SHADOW_PRINTK("%s out-of-range update "
2217 "sl4mfn=%05lx index=0x%x val=%" SH_PRI_pte "\n",
2218 __func__, mfn_x(sl4mfn), shadow_index, new_sl4e.l4);
2219 if ( shadow_l4e_get_flags(new_sl4e) & _PAGE_PRESENT )
2221 SHADOW_ERROR("out-of-range l4e update\n");
2222 result |= SHADOW_SET_ERROR;
2225 // do not call shadow_set_l4e...
2226 return result;
2230 result |= shadow_set_l4e(v, sl4p, new_sl4e, sl4mfn);
2231 return result;
2235 static int validate_gl3e(struct vcpu *v, void *new_ge, mfn_t sl3mfn, void *se)
2237 shadow_l3e_t new_sl3e;
2238 guest_l3e_t *new_gl3e = new_ge;
2239 shadow_l3e_t *sl3p = se;
2240 mfn_t sl2mfn = _mfn(INVALID_MFN);
2241 int result = 0;
2243 perfc_incrc(shadow_validate_gl3e_calls);
2245 if ( guest_l3e_get_flags(*new_gl3e) & _PAGE_PRESENT )
2247 gfn_t gl2gfn = guest_l3e_get_gfn(*new_gl3e);
2248 mfn_t gl2mfn = vcpu_gfn_to_mfn(v, gl2gfn);
2249 if ( mfn_valid(gl2mfn) )
2250 sl2mfn = get_shadow_status(v, gl2mfn, SH_type_l2_shadow);
2251 else
2252 result |= SHADOW_SET_ERROR;
2254 l3e_propagate_from_guest(v, new_gl3e, _mfn(INVALID_MFN),
2255 sl2mfn, &new_sl3e, ft_prefetch);
2256 result |= shadow_set_l3e(v, sl3p, new_sl3e, sl3mfn);
2258 return result;
2260 #endif // GUEST_PAGING_LEVELS >= 4
2262 static int validate_gl2e(struct vcpu *v, void *new_ge, mfn_t sl2mfn, void *se)
2264 shadow_l2e_t new_sl2e;
2265 guest_l2e_t *new_gl2e = new_ge;
2266 shadow_l2e_t *sl2p = se;
2267 mfn_t sl1mfn = _mfn(INVALID_MFN);
2268 int result = 0;
2270 perfc_incrc(shadow_validate_gl2e_calls);
2272 if ( guest_l2e_get_flags(*new_gl2e) & _PAGE_PRESENT )
2274 gfn_t gl1gfn = guest_l2e_get_gfn(*new_gl2e);
2275 if ( guest_supports_superpages(v) &&
2276 (guest_l2e_get_flags(*new_gl2e) & _PAGE_PSE) )
2278 // superpage -- need to look up the shadow L1 which holds the
2279 // splitters...
2280 sl1mfn = get_fl1_shadow_status(v, gl1gfn);
2281 #if 0
2282 // XXX - it's possible that we want to do some kind of prefetch
2283 // for superpage fl1's here, but this is *not* on the demand path,
2284 // so we'll hold off trying that for now...
2285 //
2286 if ( !mfn_valid(sl1mfn) )
2287 sl1mfn = make_fl1_shadow(v, gl1gfn);
2288 #endif
2290 else
2292 mfn_t gl1mfn = vcpu_gfn_to_mfn(v, gl1gfn);
2293 if ( mfn_valid(gl1mfn) )
2294 sl1mfn = get_shadow_status(v, gl1mfn, SH_type_l1_shadow);
2295 else
2296 result |= SHADOW_SET_ERROR;
2299 l2e_propagate_from_guest(v, new_gl2e, _mfn(INVALID_MFN),
2300 sl1mfn, &new_sl2e, ft_prefetch);
2302 // check for updates to xen reserved slots in PV guests...
2303 // XXX -- need to revisit this for PV 3-on-4 guests.
2304 //
2305 #if SHADOW_PAGING_LEVELS < 4
2306 #if CONFIG_PAGING_LEVELS == SHADOW_PAGING_LEVELS
2307 if ( !shadow_mode_external(v->domain) )
2309 int shadow_index = (((unsigned long)sl2p & ~PAGE_MASK) /
2310 sizeof(shadow_l2e_t));
2311 int reserved_xen_slot;
2313 #if SHADOW_PAGING_LEVELS == 3
2314 reserved_xen_slot =
2315 ((mfn_to_shadow_page(sl2mfn)->type == SH_type_l2h_pae_shadow) &&
2316 (shadow_index
2317 >= (L2_PAGETABLE_FIRST_XEN_SLOT & (L2_PAGETABLE_ENTRIES-1))));
2318 #else /* SHADOW_PAGING_LEVELS == 2 */
2319 reserved_xen_slot = (shadow_index >= L2_PAGETABLE_FIRST_XEN_SLOT);
2320 #endif
2322 if ( unlikely(reserved_xen_slot) )
2324 // attempt by the guest to write to a xen reserved slot
2325 //
2326 SHADOW_PRINTK("%s out-of-range update "
2327 "sl2mfn=%05lx index=0x%x val=%" SH_PRI_pte "\n",
2328 __func__, mfn_x(sl2mfn), shadow_index, new_sl2e.l2);
2329 if ( shadow_l2e_get_flags(new_sl2e) & _PAGE_PRESENT )
2331 SHADOW_ERROR("out-of-range l2e update\n");
2332 result |= SHADOW_SET_ERROR;
2335 // do not call shadow_set_l2e...
2336 return result;
2339 #endif /* CONFIG_PAGING_LEVELS == SHADOW_PAGING_LEVELS */
2340 #endif /* SHADOW_PAGING_LEVELS < 4 */
2342 result |= shadow_set_l2e(v, sl2p, new_sl2e, sl2mfn);
2344 return result;
2347 static int validate_gl1e(struct vcpu *v, void *new_ge, mfn_t sl1mfn, void *se)
2349 shadow_l1e_t new_sl1e;
2350 guest_l1e_t *new_gl1e = new_ge;
2351 shadow_l1e_t *sl1p = se;
2352 gfn_t gfn;
2353 mfn_t gmfn;
2354 int result = 0, mmio;
2356 perfc_incrc(shadow_validate_gl1e_calls);
2358 gfn = guest_l1e_get_gfn(*new_gl1e);
2359 gmfn = vcpu_gfn_to_mfn(v, gfn);
2361 mmio = (is_hvm_vcpu(v) && shadow_vcpu_mode_translate(v) && !mfn_valid(gmfn));
2362 l1e_propagate_from_guest(v, new_gl1e, _mfn(INVALID_MFN), gmfn, &new_sl1e,
2363 ft_prefetch, mmio);
2365 result |= shadow_set_l1e(v, sl1p, new_sl1e, sl1mfn);
2366 return result;
2370 /**************************************************************************/
2371 /* Functions which translate and install the shadows of arbitrary guest
2372 * entries that we have just seen the guest write. */
2375 static inline int
2376 sh_map_and_validate(struct vcpu *v, mfn_t gmfn,
2377 void *new_gp, u32 size, u32 sh_type,
2378 u32 (*shadow_index)(mfn_t *smfn, u32 idx),
2379 int (*validate_ge)(struct vcpu *v, void *ge,
2380 mfn_t smfn, void *se))
2381 /* Generic function for mapping and validating. */
2383 mfn_t smfn, smfn2, map_mfn;
2384 shadow_l1e_t *sl1p;
2385 u32 shadow_idx, guest_idx;
2386 int result = 0;
2388 /* Align address and size to guest entry boundaries */
2389 size += (unsigned long)new_gp & (sizeof (guest_l1e_t) - 1);
2390 new_gp = (void *)((unsigned long)new_gp & ~(sizeof (guest_l1e_t) - 1));
2391 size = (size + sizeof (guest_l1e_t) - 1) & ~(sizeof (guest_l1e_t) - 1);
2392 ASSERT(size + (((unsigned long)new_gp) & ~PAGE_MASK) <= PAGE_SIZE);
2394 /* Map the shadow page */
2395 smfn = get_shadow_status(v, gmfn, sh_type);
2396 ASSERT(mfn_valid(smfn)); /* Otherwise we would not have been called */
2397 guest_idx = guest_index(new_gp);
2398 map_mfn = smfn;
2399 shadow_idx = shadow_index(&map_mfn, guest_idx);
2400 sl1p = map_shadow_page(map_mfn);
2402 /* Validate one entry at a time */
2403 while ( size )
2405 smfn2 = smfn;
2406 guest_idx = guest_index(new_gp);
2407 shadow_idx = shadow_index(&smfn2, guest_idx);
2408 if ( mfn_x(smfn2) != mfn_x(map_mfn) )
2410 /* We have moved to another page of the shadow */
2411 map_mfn = smfn2;
2412 unmap_shadow_page(sl1p);
2413 sl1p = map_shadow_page(map_mfn);
2415 result |= validate_ge(v,
2416 new_gp,
2417 map_mfn,
2418 &sl1p[shadow_idx]);
2419 size -= sizeof(guest_l1e_t);
2420 new_gp += sizeof(guest_l1e_t);
2422 unmap_shadow_page(sl1p);
2423 return result;
2427 int
2428 sh_map_and_validate_gl4e(struct vcpu *v, mfn_t gl4mfn,
2429 void *new_gl4p, u32 size)
2431 #if GUEST_PAGING_LEVELS >= 4
2432 return sh_map_and_validate(v, gl4mfn, new_gl4p, size,
2433 SH_type_l4_shadow,
2434 shadow_l4_index,
2435 validate_gl4e);
2436 #else // ! GUEST_PAGING_LEVELS >= 4
2437 SHADOW_PRINTK("called in wrong paging mode!\n");
2438 BUG();
2439 return 0;
2440 #endif
2443 int
2444 sh_map_and_validate_gl3e(struct vcpu *v, mfn_t gl3mfn,
2445 void *new_gl3p, u32 size)
2447 #if GUEST_PAGING_LEVELS >= 4
2448 return sh_map_and_validate(v, gl3mfn, new_gl3p, size,
2449 SH_type_l3_shadow,
2450 shadow_l3_index,
2451 validate_gl3e);
2452 #else // ! GUEST_PAGING_LEVELS >= 4
2453 SHADOW_PRINTK("called in wrong paging mode!\n");
2454 BUG();
2455 return 0;
2456 #endif
2459 int
2460 sh_map_and_validate_gl2e(struct vcpu *v, mfn_t gl2mfn,
2461 void *new_gl2p, u32 size)
2463 return sh_map_and_validate(v, gl2mfn, new_gl2p, size,
2464 SH_type_l2_shadow,
2465 shadow_l2_index,
2466 validate_gl2e);
2469 int
2470 sh_map_and_validate_gl2he(struct vcpu *v, mfn_t gl2mfn,
2471 void *new_gl2p, u32 size)
2473 #if GUEST_PAGING_LEVELS == 3
2474 return sh_map_and_validate(v, gl2mfn, new_gl2p, size,
2475 SH_type_l2h_shadow,
2476 shadow_l2_index,
2477 validate_gl2e);
2478 #else /* Non-PAE guests don't have different kinds of l2 table */
2479 SHADOW_PRINTK("called in wrong paging mode!\n");
2480 BUG();
2481 return 0;
2482 #endif
2485 int
2486 sh_map_and_validate_gl1e(struct vcpu *v, mfn_t gl1mfn,
2487 void *new_gl1p, u32 size)
2489 return sh_map_and_validate(v, gl1mfn, new_gl1p, size,
2490 SH_type_l1_shadow,
2491 shadow_l1_index,
2492 validate_gl1e);
2496 /**************************************************************************/
2497 /* Optimization: If we see two emulated writes of zeros to the same
2498 * page-table without another kind of page fault in between, we guess
2499 * that this is a batch of changes (for process destruction) and
2500 * unshadow the page so we don't take a pagefault on every entry. This
2501 * should also make finding writeable mappings of pagetables much
2502 * easier. */
2504 /* Look to see if this is the second emulated write in a row to this
2505 * page, and unshadow/unhook if it is */
2506 static inline void check_for_early_unshadow(struct vcpu *v, mfn_t gmfn)
2508 #if SHADOW_OPTIMIZATIONS & SHOPT_EARLY_UNSHADOW
2509 if ( v->arch.shadow.last_emulated_mfn == mfn_x(gmfn) &&
2510 sh_mfn_is_a_page_table(gmfn) )
2512 u32 flags = mfn_to_page(gmfn)->shadow_flags;
2513 if ( !(flags & (SHF_L2_32|SHF_L2_PAE|SHF_L2H_PAE|SHF_L4_64)) )
2515 perfc_incrc(shadow_early_unshadow);
2516 sh_remove_shadows(v, gmfn, 1, 0 /* Fast, can fail to unshadow */ );
2519 v->arch.shadow.last_emulated_mfn = mfn_x(gmfn);
2520 #endif
2523 /* Stop counting towards early unshadows, as we've seen a real page fault */
2524 static inline void reset_early_unshadow(struct vcpu *v)
2526 #if SHADOW_OPTIMIZATIONS & SHOPT_EARLY_UNSHADOW
2527 v->arch.shadow.last_emulated_mfn = INVALID_MFN;
2528 #endif
2533 /**************************************************************************/
2534 /* Optimization: Prefetch multiple L1 entries. This is called after we have
2535 * demand-faulted a shadow l1e in the fault handler, to see if it's
2536 * worth fetching some more.
2537 */
2539 #if SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH
2541 /* XXX magic number */
2542 #define PREFETCH_DISTANCE 32
2544 static void sh_prefetch(struct vcpu *v, walk_t *gw,
2545 shadow_l1e_t *ptr_sl1e, mfn_t sl1mfn)
2547 int i, dist, mmio;
2548 gfn_t gfn;
2549 mfn_t gmfn;
2550 guest_l1e_t gl1e;
2551 shadow_l1e_t sl1e;
2552 u32 gflags;
2554 /* Prefetch no further than the end of the _shadow_ l1 MFN */
2555 dist = (PAGE_SIZE - ((unsigned long)ptr_sl1e & ~PAGE_MASK)) / sizeof sl1e;
2556 /* And no more than a maximum fetches-per-fault */
2557 if ( dist > PREFETCH_DISTANCE )
2558 dist = PREFETCH_DISTANCE;
2560 for ( i = 1; i < dist ; i++ )
2562 /* No point in prefetching if there's already a shadow */
2563 if ( ptr_sl1e[i].l1 != 0 )
2564 break;
2566 if ( gw->l1e )
2568 /* Normal guest page; grab the next guest entry */
2569 gl1e = gw->l1e[i];
2570 /* Not worth continuing if we hit an entry that will need another
2571 * fault for A/D-bit propagation anyway */
2572 gflags = guest_l1e_get_flags(gl1e);
2573 if ( (gflags & _PAGE_PRESENT)
2574 && (!(gflags & _PAGE_ACCESSED)
2575 || ((gflags & _PAGE_RW) && !(gflags & _PAGE_DIRTY))) )
2576 break;
2578 else
2580 /* Fragmented superpage, unless we've been called wrongly */
2581 ASSERT(guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE);
2582 /* Increment the l1e's GFN by the right number of guest pages */
2583 gl1e = guest_l1e_from_gfn(
2584 _gfn(gfn_x(guest_l1e_get_gfn(gw->eff_l1e)) + i),
2585 guest_l1e_get_flags(gw->eff_l1e));
2588 /* Look at the gfn that the l1e is pointing at */
2589 gfn = guest_l1e_get_gfn(gl1e);
2590 gmfn = vcpu_gfn_to_mfn(v, gfn);
2591 mmio = ( is_hvm_vcpu(v)
2592 && shadow_vcpu_mode_translate(v)
2593 && mmio_space(gfn_to_paddr(gfn)) );
2595 /* Propagate the entry. Safe to use a pointer to our local
2596 * gl1e, since this is not a demand-fetch so there will be no
2597 * write-back to the guest. */
2598 l1e_propagate_from_guest(v, &gl1e, _mfn(INVALID_MFN),
2599 gmfn, &sl1e, ft_prefetch, mmio);
2600 (void) shadow_set_l1e(v, ptr_sl1e + i, sl1e, sl1mfn);
2604 #endif /* SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH */
2607 /**************************************************************************/
2608 /* Entry points into the shadow code */
2610 /* Called from pagefault handler in Xen, and from the HVM trap handlers
2611 * for pagefaults. Returns 1 if this fault was an artefact of the
2612 * shadow code (and the guest should retry) or 0 if it is not (and the
2613 * fault should be handled elsewhere or passed to the guest). */
2615 static int sh_page_fault(struct vcpu *v,
2616 unsigned long va,
2617 struct cpu_user_regs *regs)
2619 struct domain *d = v->domain;
2620 walk_t gw;
2621 u32 accumulated_gflags;
2622 gfn_t gfn;
2623 mfn_t gmfn, sl1mfn=_mfn(0);
2624 shadow_l1e_t sl1e, *ptr_sl1e;
2625 paddr_t gpa;
2626 struct sh_emulate_ctxt emul_ctxt;
2627 struct x86_emulate_ops *emul_ops;
2628 int r, mmio;
2629 fetch_type_t ft = 0;
2631 SHADOW_PRINTK("d:v=%u:%u va=%#lx err=%u\n",
2632 v->domain->domain_id, v->vcpu_id, va, regs->error_code);
2634 //
2635 // XXX: Need to think about eventually mapping superpages directly in the
2636 // shadow (when possible), as opposed to splintering them into a
2637 // bunch of 4K maps.
2638 //
2640 #if (SHADOW_OPTIMIZATIONS & SHOPT_FAST_FAULT_PATH) && SHADOW_PAGING_LEVELS > 2
2641 if ( (regs->error_code & PFEC_reserved_bit) )
2643 /* The only reasons for reserved bits to be set in shadow entries
2644 * are the two "magic" shadow_l1e entries. */
2645 if ( likely((__copy_from_user(&sl1e,
2646 (sh_linear_l1_table(v)
2647 + shadow_l1_linear_offset(va)),
2648 sizeof(sl1e)) == 0)
2649 && sh_l1e_is_magic(sl1e)) )
2651 if ( sh_l1e_is_gnp(sl1e) )
2653 if ( likely(!is_hvm_domain(d) ||
2654 shadow_vcpu_mode_translate(v)) )
2656 /* Not-present in a guest PT: pass to the guest as
2657 * a not-present fault (by flipping two bits). */
2658 ASSERT(regs->error_code & PFEC_page_present);
2659 regs->error_code ^= (PFEC_reserved_bit|PFEC_page_present);
2660 perfc_incrc(shadow_fault_fast_gnp);
2661 SHADOW_PRINTK("fast path not-present\n");
2662 return 0;
2664 else
2666 /* Not-present in the P2M: MMIO */
2667 gpa = va;
2670 else
2672 /* Magic MMIO marker: extract gfn for MMIO address */
2673 ASSERT(sh_l1e_is_mmio(sl1e));
2674 gpa = (((paddr_t)(gfn_x(sh_l1e_mmio_get_gfn(sl1e))))
2675 << PAGE_SHIFT)
2676 | (va & ~PAGE_MASK);
2678 perfc_incrc(shadow_fault_fast_mmio);
2679 SHADOW_PRINTK("fast path mmio %#"PRIpaddr"\n", gpa);
2680 reset_early_unshadow(v);
2681 handle_mmio(gpa);
2682 return EXCRET_fault_fixed;
2684 else
2686 /* This should be exceptionally rare: another vcpu has fixed
2687 * the tables between the fault and our reading the l1e.
2688 * Retry and let the hardware give us the right fault next time. */
2689 perfc_incrc(shadow_fault_fast_fail);
2690 SHADOW_PRINTK("fast path false alarm!\n");
2691 return EXCRET_fault_fixed;
2694 #endif /* SHOPT_FAST_FAULT_PATH */
2696 /* Detect if this page fault happened while we were already in Xen
2697 * doing a shadow operation. If that happens, the only thing we can
2698 * do is let Xen's normal fault handlers try to fix it. In any case,
2699 * a diagnostic trace of the fault will be more useful than
2700 * a BUG() when we try to take the lock again. */
2701 if ( unlikely(shadow_locked_by_me(d)) )
2703 SHADOW_ERROR("Recursive shadow fault: lock was taken by %s\n",
2704 d->arch.shadow.locker_function);
2705 return 0;
2708 shadow_lock(d);
2710 shadow_audit_tables(v);
2712 if ( guest_walk_tables(v, va, &gw, 1) != 0 )
2714 SHADOW_PRINTK("malformed guest pagetable!");
2715 print_gw(&gw);
2718 sh_audit_gw(v, &gw);
2720 // We do not look at the gw->l1e, as that will not exist for superpages.
2721 // Instead, we use the gw->eff_l1e...
2722 //
2723 // We need not check all the levels of the guest page table entries for
2724 // present vs not-present, as the eff_l1e will always be not present if
2725 // one of the higher level entries is not present.
2726 //
2727 if ( unlikely(!(guest_l1e_get_flags(gw.eff_l1e) & _PAGE_PRESENT)) )
2729 if ( is_hvm_domain(d) && !shadow_vcpu_mode_translate(v) )
2731 /* Not present in p2m map, means this is mmio */
2732 gpa = va;
2733 goto mmio;
2736 perfc_incrc(shadow_fault_bail_not_present);
2737 goto not_a_shadow_fault;
2740 // All levels of the guest page table are now known to be present.
2741 accumulated_gflags = accumulate_guest_flags(v, &gw);
2743 // Check for attempts to access supervisor-only pages from user mode,
2744 // i.e. ring 3. Such errors are not caused or dealt with by the shadow
2745 // code.
2746 //
2747 if ( (regs->error_code & PFEC_user_mode) &&
2748 !(accumulated_gflags & _PAGE_USER) )
2750 /* illegal user-mode access to supervisor-only page */
2751 perfc_incrc(shadow_fault_bail_user_supervisor);
2752 goto not_a_shadow_fault;
2755 // Was it a write fault?
2756 ft = ((regs->error_code & PFEC_write_access)
2757 ? ft_demand_write : ft_demand_read);
2758 if ( ft == ft_demand_write )
2760 if ( unlikely(!(accumulated_gflags & _PAGE_RW)) )
2762 perfc_incrc(shadow_fault_bail_ro_mapping);
2763 goto not_a_shadow_fault;
2766 else // must have been either an insn fetch or read fault
2768 // Check for NX bit violations: attempts to execute code that is
2769 // marked "do not execute". Such errors are not caused or dealt with
2770 // by the shadow code.
2771 //
2772 if ( regs->error_code & PFEC_insn_fetch )
2774 if ( accumulated_gflags & _PAGE_NX_BIT )
2776 /* NX prevented this code fetch */
2777 perfc_incrc(shadow_fault_bail_nx);
2778 goto not_a_shadow_fault;
2783 /* What mfn is the guest trying to access? */
2784 gfn = guest_l1e_get_gfn(gw.eff_l1e);
2785 gmfn = vcpu_gfn_to_mfn(v, gfn);
2786 mmio = (is_hvm_domain(d)
2787 && shadow_vcpu_mode_translate(v)
2788 && mmio_space(gfn_to_paddr(gfn)));
2790 if ( !mmio && !mfn_valid(gmfn) )
2792 perfc_incrc(shadow_fault_bail_bad_gfn);
2793 SHADOW_PRINTK("BAD gfn=%"SH_PRI_gfn" gmfn=%"SH_PRI_mfn"\n",
2794 gfn_x(gfn), mfn_x(gmfn));
2795 goto not_a_shadow_fault;
2798 /* Make sure there is enough free shadow memory to build a chain of
2799 * shadow tables: one SHADOW_MAX_ORDER chunk will always be enough
2800 * to allocate all we need. (We never allocate a top-level shadow
2801 * on this path, only a 32b l1, pae l2+1 or 64b l3+2+1) */
2802 shadow_prealloc(d, SHADOW_MAX_ORDER);
2804 /* Acquire the shadow. This must happen before we figure out the rights
2805 * for the shadow entry, since we might promote a page here. */
2806 ptr_sl1e = shadow_get_and_create_l1e(v, &gw, &sl1mfn, ft);
2807 if ( unlikely(ptr_sl1e == NULL) )
2809 /* Couldn't get the sl1e! Since we know the guest entries
2810 * are OK, this can only have been caused by a failed
2811 * shadow_set_l*e(), which will have crashed the guest.
2812 * Get out of the fault handler immediately. */
2813 ASSERT(test_bit(_DOMF_dying, &d->domain_flags));
2814 unmap_walk(v, &gw);
2815 shadow_unlock(d);
2816 return 0;
2819 /* Calculate the shadow entry and write it */
2820 l1e_propagate_from_guest(v, (gw.l1e) ? gw.l1e : &gw.eff_l1e, gw.l1mfn,
2821 gmfn, &sl1e, ft, mmio);
2822 r = shadow_set_l1e(v, ptr_sl1e, sl1e, sl1mfn);
2824 #if SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH
2825 /* Prefetch some more shadow entries */
2826 sh_prefetch(v, &gw, ptr_sl1e, sl1mfn);
2827 #endif
2829 /* Need to emulate accesses to page tables */
2830 if ( sh_mfn_is_a_page_table(gmfn) )
2832 if ( ft == ft_demand_write )
2834 perfc_incrc(shadow_fault_emulate_write);
2835 goto emulate;
2837 else if ( shadow_mode_trap_reads(d) && ft == ft_demand_read )
2839 perfc_incrc(shadow_fault_emulate_read);
2840 goto emulate;
2844 if ( mmio )
2846 gpa = guest_walk_to_gpa(&gw);
2847 goto mmio;
2850 perfc_incrc(shadow_fault_fixed);
2851 d->arch.shadow.fault_count++;
2852 reset_early_unshadow(v);
2854 done:
2855 sh_audit_gw(v, &gw);
2856 unmap_walk(v, &gw);
2857 SHADOW_PRINTK("fixed\n");
2858 shadow_audit_tables(v);
2859 shadow_unlock(d);
2860 return EXCRET_fault_fixed;
2862 emulate:
2863 if ( !shadow_mode_refcounts(d) || !guest_mode(regs) )
2864 goto not_a_shadow_fault;
2866 if ( is_hvm_domain(d) )
2867 hvm_store_cpu_guest_regs(v, regs, NULL);
2868 SHADOW_PRINTK("emulate: eip=%#lx\n", (unsigned long)regs->eip);
2870 emul_ops = shadow_init_emulation(&emul_ctxt, regs);
2872 /*
2873 * We do not emulate user writes. Instead we use them as a hint that the
2874 * page is no longer a page table. This behaviour differs from native, but
2875 * it seems very unlikely that any OS grants user access to page tables.
2876 */
2877 if ( (regs->error_code & PFEC_user_mode) ||
2878 x86_emulate(&emul_ctxt.ctxt, emul_ops) )
2880 SHADOW_PRINTK("emulator failure, unshadowing mfn %#lx\n",
2881 mfn_x(gmfn));
2882 perfc_incrc(shadow_fault_emulate_failed);
2883 /* If this is actually a page table, then we have a bug, and need
2884 * to support more operations in the emulator. More likely,
2885 * though, this is a hint that this page should not be shadowed. */
2886 sh_remove_shadows(v, gmfn, 0 /* thorough */, 1 /* must succeed */);
2889 /* Emulator has changed the user registers: write back */
2890 if ( is_hvm_domain(d) )
2891 hvm_load_cpu_guest_regs(v, regs);
2892 goto done;
2894 mmio:
2895 if ( !guest_mode(regs) )
2896 goto not_a_shadow_fault;
2897 perfc_incrc(shadow_fault_mmio);
2898 sh_audit_gw(v, &gw);
2899 unmap_walk(v, &gw);
2900 SHADOW_PRINTK("mmio %#"PRIpaddr"\n", gpa);
2901 shadow_audit_tables(v);
2902 reset_early_unshadow(v);
2903 shadow_unlock(d);
2904 handle_mmio(gpa);
2905 return EXCRET_fault_fixed;
2907 not_a_shadow_fault:
2908 sh_audit_gw(v, &gw);
2909 unmap_walk(v, &gw);
2910 SHADOW_PRINTK("not a shadow fault\n");
2911 shadow_audit_tables(v);
2912 reset_early_unshadow(v);
2913 shadow_unlock(d);
2914 return 0;
2918 static int
2919 sh_invlpg(struct vcpu *v, unsigned long va)
2920 /* Called when the guest requests an invlpg. Returns 1 if the invlpg
2921 * instruction should be issued on the hardware, or 0 if it's safe not
2922 * to do so. */
2924 shadow_l2e_t sl2e;
2926 perfc_incrc(shadow_invlpg);
2928 /* First check that we can safely read the shadow l2e. SMP/PAE linux can
2929 * run as high as 6% of invlpg calls where we haven't shadowed the l2
2930 * yet. */
2931 #if SHADOW_PAGING_LEVELS == 4
2933 shadow_l3e_t sl3e;
2934 if ( !(shadow_l4e_get_flags(
2935 sh_linear_l4_table(v)[shadow_l4_linear_offset(va)])
2936 & _PAGE_PRESENT) )
2937 return 0;
2938 /* This must still be a copy-from-user because we don't have the
2939 * shadow lock, and the higher-level shadows might disappear
2940 * under our feet. */
2941 if ( __copy_from_user(&sl3e, (sh_linear_l3_table(v)
2942 + shadow_l3_linear_offset(va)),
2943 sizeof (sl3e)) != 0 )
2945 perfc_incrc(shadow_invlpg_fault);
2946 return 0;
2948 if ( (!shadow_l3e_get_flags(sl3e) & _PAGE_PRESENT) )
2949 return 0;
2951 #elif SHADOW_PAGING_LEVELS == 3
2952 if ( !(l3e_get_flags(v->arch.shadow.l3table[shadow_l3_linear_offset(va)])
2953 & _PAGE_PRESENT) )
2954 // no need to flush anything if there's no SL2...
2955 return 0;
2956 #endif
2958 /* This must still be a copy-from-user because we don't have the shadow
2959 * lock, and the higher-level shadows might disappear under our feet. */
2960 if ( __copy_from_user(&sl2e,
2961 sh_linear_l2_table(v) + shadow_l2_linear_offset(va),
2962 sizeof (sl2e)) != 0 )
2964 perfc_incrc(shadow_invlpg_fault);
2965 return 0;
2968 // If there's nothing shadowed for this particular sl2e, then
2969 // there is no need to do an invlpg, either...
2970 //
2971 if ( !(shadow_l2e_get_flags(sl2e) & _PAGE_PRESENT) )
2972 return 0;
2974 // Check to see if the SL2 is a splintered superpage...
2975 // If so, then we'll need to flush the entire TLB (because that's
2976 // easier than invalidating all of the individual 4K pages).
2977 //
2978 if ( mfn_to_shadow_page(shadow_l2e_get_mfn(sl2e))->type
2979 == SH_type_fl1_shadow )
2981 local_flush_tlb();
2982 return 0;
2985 return 1;
2988 static unsigned long
2989 sh_gva_to_gfn(struct vcpu *v, unsigned long va)
2990 /* Called to translate a guest virtual address to what the *guest*
2991 * pagetables would map it to. */
2993 walk_t gw;
2994 gfn_t gfn;
2996 guest_walk_tables(v, va, &gw, 0);
2997 gfn = guest_walk_to_gfn(&gw);
2998 unmap_walk(v, &gw);
3000 return gfn_x(gfn);
3004 static paddr_t
3005 sh_gva_to_gpa(struct vcpu *v, unsigned long va)
3006 /* Called to translate a guest virtual address to what the *guest*
3007 * pagetables would map it to. */
3009 unsigned long gfn = sh_gva_to_gfn(v, va);
3010 if ( gfn == INVALID_GFN )
3011 return 0;
3012 else
3013 return (((paddr_t)gfn) << PAGE_SHIFT) + (va & ~PAGE_MASK);
3017 static inline void
3018 sh_update_linear_entries(struct vcpu *v)
3019 /* Sync up all the linear mappings for this vcpu's pagetables */
3021 struct domain *d = v->domain;
3023 /* Linear pagetables in PV guests
3024 * ------------------------------
3026 * Guest linear pagetables, which map the guest pages, are at
3027 * LINEAR_PT_VIRT_START. Shadow linear pagetables, which map the
3028 * shadows, are at SH_LINEAR_PT_VIRT_START. Most of the time these
3029 * are set up at shadow creation time, but (of course!) the PAE case
3030 * is subtler. Normal linear mappings are made by having an entry
3031 * in the top-level table that points to itself (shadow linear) or
3032 * to the guest top-level table (guest linear). For PAE, to set up
3033 * a linear map requires us to copy the four top-level entries into
3034 * level-2 entries. That means that every time we change a PAE l3e,
3035 * we need to reflect the change into the copy.
3037 * Linear pagetables in HVM guests
3038 * -------------------------------
3040 * For HVM guests, the linear pagetables are installed in the monitor
3041 * tables (since we can't put them in the shadow). Shadow linear
3042 * pagetables, which map the shadows, are at SH_LINEAR_PT_VIRT_START,
3043 * and we use the linear pagetable slot at LINEAR_PT_VIRT_START for
3044 * a linear pagetable of the monitor tables themselves. We have
3045 * the same issue of having to re-copy PAE l3 entries whevever we use
3046 * PAE shadows.
3048 * Because HVM guests run on the same monitor tables regardless of the
3049 * shadow tables in use, the linear mapping of the shadow tables has to
3050 * be updated every time v->arch.shadow_table changes.
3051 */
3053 /* Don't try to update the monitor table if it doesn't exist */
3054 if ( shadow_mode_external(d)
3055 && pagetable_get_pfn(v->arch.monitor_table) == 0 )
3056 return;
3058 #if (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS == 4)
3060 /* For PV, one l4e points at the guest l4, one points at the shadow
3061 * l4. No maintenance required.
3062 * For HVM, just need to update the l4e that points to the shadow l4. */
3064 if ( shadow_mode_external(d) )
3066 /* Use the linear map if we can; otherwise make a new mapping */
3067 if ( v == current )
3069 __linear_l4_table[l4_linear_offset(SH_LINEAR_PT_VIRT_START)] =
3070 l4e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3071 __PAGE_HYPERVISOR);
3073 else
3075 l4_pgentry_t *ml4e;
3076 ml4e = sh_map_domain_page(pagetable_get_mfn(v->arch.monitor_table));
3077 ml4e[l4_table_offset(SH_LINEAR_PT_VIRT_START)] =
3078 l4e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3079 __PAGE_HYPERVISOR);
3080 sh_unmap_domain_page(ml4e);
3084 #elif (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS == 3)
3086 /* PV: XXX
3088 * HVM: To give ourselves a linear map of the shadows, we need to
3089 * extend a PAE shadow to 4 levels. We do this by having a monitor
3090 * l3 in slot 0 of the monitor l4 table, and copying the PAE l3
3091 * entries into it. Then, by having the monitor l4e for shadow
3092 * pagetables also point to the monitor l4, we can use it to access
3093 * the shadows.
3094 */
3096 if ( shadow_mode_external(d) )
3098 /* Install copies of the shadow l3es into the monitor l3 table.
3099 * The monitor l3 table is hooked into slot 0 of the monitor
3100 * l4 table, so we use l3 linear indices 0 to 3 */
3101 shadow_l3e_t *sl3e;
3102 l3_pgentry_t *ml3e;
3103 mfn_t l3mfn;
3104 int i;
3106 /* Use linear mappings if we can; otherwise make new mappings */
3107 if ( v == current )
3109 ml3e = __linear_l3_table;
3110 l3mfn = _mfn(l4e_get_pfn(__linear_l4_table[0]));
3112 else
3114 l4_pgentry_t *ml4e;
3115 ml4e = sh_map_domain_page(pagetable_get_mfn(v->arch.monitor_table));
3116 ASSERT(l4e_get_flags(ml4e[0]) & _PAGE_PRESENT);
3117 l3mfn = _mfn(l4e_get_pfn(ml4e[0]));
3118 ml3e = sh_map_domain_page(l3mfn);
3119 sh_unmap_domain_page(ml4e);
3122 /* Shadow l3 tables are made up by sh_update_cr3 */
3123 sl3e = v->arch.shadow.l3table;
3125 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3127 ml3e[i] =
3128 (shadow_l3e_get_flags(sl3e[i]) & _PAGE_PRESENT)
3129 ? l3e_from_pfn(mfn_x(shadow_l3e_get_mfn(sl3e[i])),
3130 __PAGE_HYPERVISOR)
3131 : l3e_empty();
3134 if ( v != current )
3135 sh_unmap_domain_page(ml3e);
3137 else
3138 domain_crash(d); /* XXX */
3140 #elif CONFIG_PAGING_LEVELS == 3
3142 /* PV: need to copy the guest's l3 entries into the guest-linear-map l2
3143 * entries in the shadow, and the shadow's l3 entries into the
3144 * shadow-linear-map l2 entries in the shadow. This is safe to do
3145 * because Xen does not let guests share high-slot l2 tables between l3s,
3146 * so we know we're not treading on anyone's toes.
3148 * HVM: need to copy the shadow's l3 entries into the
3149 * shadow-linear-map l2 entries in the monitor table. This is safe
3150 * because we have one monitor table for each vcpu. The monitor's
3151 * own l3es don't need to be copied because they never change.
3152 * XXX That might change if we start stuffing things into the rest
3153 * of the monitor's virtual address space.
3154 */
3156 l2_pgentry_t *l2e, new_l2e;
3157 shadow_l3e_t *guest_l3e = NULL, *shadow_l3e;
3158 int i;
3159 int unmap_l2e = 0;
3161 #if GUEST_PAGING_LEVELS == 2
3162 /* Shadow l3 tables were built by sh_update_cr3 */
3163 if ( shadow_mode_external(d) )
3164 shadow_l3e = (shadow_l3e_t *)&v->arch.shadow.l3table;
3165 else
3166 BUG(); /* PV 2-on-3 is not supported yet */
3168 #else /* GUEST_PAGING_LEVELS == 3 */
3170 shadow_l3e = (shadow_l3e_t *)&v->arch.shadow.l3table;
3171 /* Always safe to use guest_vtable, because it's globally mapped */
3172 guest_l3e = v->arch.guest_vtable;
3174 #endif /* GUEST_PAGING_LEVELS */
3176 /* Choose where to write the entries, using linear maps if possible */
3177 if ( shadow_mode_external(d) )
3179 if ( v == current )
3181 /* From the monitor tables, it's safe to use linear maps
3182 * to update monitor l2s */
3183 l2e = __linear_l2_table + (3 * L2_PAGETABLE_ENTRIES);
3185 else
3187 /* Map the monitor table's high l2 */
3188 l3_pgentry_t *l3e;
3189 l3e = sh_map_domain_page(
3190 pagetable_get_mfn(v->arch.monitor_table));
3191 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
3192 l2e = sh_map_domain_page(_mfn(l3e_get_pfn(l3e[3])));
3193 unmap_l2e = 1;
3194 sh_unmap_domain_page(l3e);
3197 else
3199 /* Map the shadow table's high l2 */
3200 ASSERT(shadow_l3e_get_flags(shadow_l3e[3]) & _PAGE_PRESENT);
3201 l2e = sh_map_domain_page(shadow_l3e_get_mfn(shadow_l3e[3]));
3202 unmap_l2e = 1;
3205 /* Write linear mapping of guest (only in PV, and only when
3206 * not translated). */
3207 if ( !shadow_mode_translate(d) )
3209 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3211 new_l2e =
3212 ((shadow_l3e_get_flags(guest_l3e[i]) & _PAGE_PRESENT)
3213 ? l2e_from_pfn(mfn_x(shadow_l3e_get_mfn(guest_l3e[i])),
3214 __PAGE_HYPERVISOR)
3215 : l2e_empty());
3216 safe_write_entry(
3217 &l2e[l2_table_offset(LINEAR_PT_VIRT_START) + i],
3218 &new_l2e);
3222 /* Write linear mapping of shadow. */
3223 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3225 new_l2e = (shadow_l3e_get_flags(shadow_l3e[i]) & _PAGE_PRESENT)
3226 ? l2e_from_pfn(mfn_x(shadow_l3e_get_mfn(shadow_l3e[i])),
3227 __PAGE_HYPERVISOR)
3228 : l2e_empty();
3229 safe_write_entry(
3230 &l2e[l2_table_offset(SH_LINEAR_PT_VIRT_START) + i],
3231 &new_l2e);
3234 if ( unmap_l2e )
3235 sh_unmap_domain_page(l2e);
3238 #elif CONFIG_PAGING_LEVELS == 2
3240 /* For PV, one l2e points at the guest l2, one points at the shadow
3241 * l2. No maintenance required.
3242 * For HVM, just need to update the l2e that points to the shadow l2. */
3244 if ( shadow_mode_external(d) )
3246 /* Use the linear map if we can; otherwise make a new mapping */
3247 if ( v == current )
3249 __linear_l2_table[l2_linear_offset(SH_LINEAR_PT_VIRT_START)] =
3250 l2e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3251 __PAGE_HYPERVISOR);
3253 else
3255 l2_pgentry_t *ml2e;
3256 ml2e = sh_map_domain_page(pagetable_get_mfn(v->arch.monitor_table));
3257 ml2e[l2_table_offset(SH_LINEAR_PT_VIRT_START)] =
3258 l2e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3259 __PAGE_HYPERVISOR);
3260 sh_unmap_domain_page(ml2e);
3264 #else
3265 #error this should not happen
3266 #endif
3270 /* Removes vcpu->arch.guest_vtable and vcpu->arch.shadow_table[].
3271 * Does all appropriate management/bookkeeping/refcounting/etc...
3272 */
3273 static void
3274 sh_detach_old_tables(struct vcpu *v)
3276 struct domain *d = v->domain;
3277 mfn_t smfn;
3278 int i = 0;
3280 ////
3281 //// vcpu->arch.guest_vtable
3282 ////
3283 if ( v->arch.guest_vtable )
3285 #if GUEST_PAGING_LEVELS == 4
3286 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
3287 sh_unmap_domain_page_global(v->arch.guest_vtable);
3288 #elif GUEST_PAGING_LEVELS == 3
3289 if ( 1 || shadow_mode_external(d) || shadow_mode_translate(d) )
3290 sh_unmap_domain_page_global(v->arch.guest_vtable);
3291 #elif GUEST_PAGING_LEVELS == 2
3292 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
3293 sh_unmap_domain_page_global(v->arch.guest_vtable);
3294 #endif
3295 v->arch.guest_vtable = NULL;
3298 ////
3299 //// vcpu->arch.shadow_table[]
3300 ////
3303 #if GUEST_PAGING_LEVELS == 3
3304 /* PAE guests have four shadow_table entries */
3305 for ( i = 0 ; i < 4 ; i++ )
3306 #endif
3308 smfn = pagetable_get_mfn(v->arch.shadow_table[i]);
3309 if ( mfn_x(smfn) )
3310 sh_put_ref(v, smfn, 0);
3311 v->arch.shadow_table[i] = pagetable_null();
3315 /* Set up the top-level shadow and install it in slot 'slot' of shadow_table */
3316 static void
3317 sh_set_toplevel_shadow(struct vcpu *v,
3318 int slot,
3319 mfn_t gmfn,
3320 unsigned int root_type)
3322 mfn_t smfn;
3323 pagetable_t old_entry, new_entry;
3325 struct domain *d = v->domain;
3327 /* Remember the old contents of this slot */
3328 old_entry = v->arch.shadow_table[slot];
3330 /* Now figure out the new contents: is this a valid guest MFN? */
3331 if ( !mfn_valid(gmfn) )
3333 new_entry = pagetable_null();
3334 goto install_new_entry;
3337 /* Guest mfn is valid: shadow it and install the shadow */
3338 smfn = get_shadow_status(v, gmfn, root_type);
3339 if ( !mfn_valid(smfn) )
3341 /* Make sure there's enough free shadow memory. */
3342 shadow_prealloc(d, SHADOW_MAX_ORDER);
3343 /* Shadow the page. */
3344 smfn = sh_make_shadow(v, gmfn, root_type);
3346 ASSERT(mfn_valid(smfn));
3348 #if SHADOW_OPTIMIZATIONS & SHOPT_EARLY_UNSHADOW
3349 /* Once again OK to unhook entries from this table if we see fork/exit */
3350 ASSERT(sh_mfn_is_a_page_table(gmfn));
3351 mfn_to_page(gmfn)->shadow_flags &= ~SHF_unhooked_mappings;
3352 #endif
3354 /* Pin the shadow and put it (back) on the list of top-level shadows */
3355 if ( sh_pin(v, smfn) == 0 )
3357 SHADOW_ERROR("can't pin %#lx as toplevel shadow\n", mfn_x(smfn));
3358 domain_crash(v->domain);
3361 /* Take a ref to this page: it will be released in sh_detach_old_tables()
3362 * or the next call to set_toplevel_shadow() */
3363 if ( !sh_get_ref(v, smfn, 0) )
3365 SHADOW_ERROR("can't install %#lx as toplevel shadow\n", mfn_x(smfn));
3366 domain_crash(v->domain);
3369 new_entry = pagetable_from_mfn(smfn);
3371 install_new_entry:
3372 /* Done. Install it */
3373 SHADOW_PRINTK("%u/%u [%u] gmfn %#"SH_PRI_mfn" smfn %#"SH_PRI_mfn"\n",
3374 GUEST_PAGING_LEVELS, SHADOW_PAGING_LEVELS, slot,
3375 mfn_x(gmfn), mfn_x(pagetable_get_mfn(new_entry)));
3376 v->arch.shadow_table[slot] = new_entry;
3378 /* Decrement the refcount of the old contents of this slot */
3379 if ( !pagetable_is_null(old_entry) )
3380 sh_put_ref(v, pagetable_get_mfn(old_entry), 0);
3384 static void
3385 sh_update_cr3(struct vcpu *v, int do_locking)
3386 /* Updates vcpu->arch.cr3 after the guest has changed CR3.
3387 * Paravirtual guests should set v->arch.guest_table (and guest_table_user,
3388 * if appropriate).
3389 * HVM guests should also make sure hvm_get_guest_cntl_reg(v, 3) works;
3390 * this function will call hvm_update_guest_cr3() to tell them where the
3391 * shadow tables are.
3392 * If do_locking != 0, assume we are being called from outside the
3393 * shadow code, and must take and release the shadow lock; otherwise
3394 * that is the caller's responsibility.
3395 */
3397 struct domain *d = v->domain;
3398 mfn_t gmfn;
3399 #if GUEST_PAGING_LEVELS == 3
3400 u32 guest_idx=0;
3401 #endif
3403 /* Don't do anything on an uninitialised vcpu */
3404 if ( !is_hvm_domain(d) && !test_bit(_VCPUF_initialised, &v->vcpu_flags) )
3406 ASSERT(v->arch.cr3 == 0);
3407 return;
3410 if ( do_locking ) shadow_lock(v->domain);
3412 ASSERT(shadow_locked_by_me(v->domain));
3413 ASSERT(v->arch.shadow.mode);
3415 ////
3416 //// vcpu->arch.guest_table is already set
3417 ////
3419 #ifndef NDEBUG
3420 /* Double-check that the HVM code has sent us a sane guest_table */
3421 if ( is_hvm_domain(d) )
3423 gfn_t gfn;
3425 ASSERT(shadow_mode_external(d));
3427 // Is paging enabled on this vcpu?
3428 if ( shadow_vcpu_mode_translate(v) )
3430 gfn = _gfn(paddr_to_pfn(hvm_get_guest_ctrl_reg(v, 3)));
3431 gmfn = vcpu_gfn_to_mfn(v, gfn);
3432 ASSERT(mfn_valid(gmfn));
3433 ASSERT(pagetable_get_pfn(v->arch.guest_table) == mfn_x(gmfn));
3435 else
3437 /* Paging disabled: guest_table points at (part of) p2m */
3438 #if SHADOW_PAGING_LEVELS != 3 /* in 3-on-4, guest-table is in slot 0 of p2m */
3439 /* For everything else, they sould be the same */
3440 ASSERT(v->arch.guest_table.pfn == d->arch.phys_table.pfn);
3441 #endif
3444 #endif
3446 SHADOW_PRINTK("d=%u v=%u guest_table=%05lx\n",
3447 d->domain_id, v->vcpu_id,
3448 (unsigned long)pagetable_get_pfn(v->arch.guest_table));
3450 #if GUEST_PAGING_LEVELS == 4
3451 if ( !(v->arch.flags & TF_kernel_mode) && !IS_COMPAT(v->domain) )
3452 gmfn = pagetable_get_mfn(v->arch.guest_table_user);
3453 else
3454 #endif
3455 gmfn = pagetable_get_mfn(v->arch.guest_table);
3458 ////
3459 //// vcpu->arch.guest_vtable
3460 ////
3461 #if GUEST_PAGING_LEVELS == 4
3462 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
3464 if ( v->arch.guest_vtable )
3465 sh_unmap_domain_page_global(v->arch.guest_vtable);
3466 v->arch.guest_vtable = sh_map_domain_page_global(gmfn);
3468 else
3469 v->arch.guest_vtable = __linear_l4_table;
3470 #elif GUEST_PAGING_LEVELS == 3
3471 if ( v->arch.guest_vtable )
3472 sh_unmap_domain_page_global(v->arch.guest_vtable);
3473 if ( shadow_mode_external(d) )
3475 if ( shadow_vcpu_mode_translate(v) )
3476 /* Paging enabled: find where in the page the l3 table is */
3477 guest_idx = guest_index((void *)hvm_get_guest_ctrl_reg(v, 3));
3478 else
3479 /* Paging disabled: l3 is at the start of a page (in the p2m) */
3480 guest_idx = 0;
3482 // Ignore the low 2 bits of guest_idx -- they are really just
3483 // cache control.
3484 guest_idx &= ~3;
3486 // XXX - why does this need a global map?
3487 v->arch.guest_vtable =
3488 (guest_l3e_t *)sh_map_domain_page_global(gmfn) + guest_idx;
3490 else
3491 v->arch.guest_vtable = sh_map_domain_page_global(gmfn);
3492 #elif GUEST_PAGING_LEVELS == 2
3493 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
3495 if ( v->arch.guest_vtable )
3496 sh_unmap_domain_page_global(v->arch.guest_vtable);
3497 v->arch.guest_vtable = sh_map_domain_page_global(gmfn);
3499 else
3500 v->arch.guest_vtable = __linear_l2_table;
3501 #else
3502 #error this should never happen
3503 #endif
3505 #if 0
3506 printk("%s %s %d gmfn=%05lx guest_vtable=%p\n",
3507 __func__, __FILE__, __LINE__, gmfn, v->arch.guest_vtable);
3508 #endif
3510 ////
3511 //// vcpu->arch.shadow_table[]
3512 ////
3514 /* We revoke write access to the new guest toplevel page(s) before we
3515 * replace the old shadow pagetable(s), so that we can safely use the
3516 * (old) shadow linear maps in the writeable mapping heuristics. */
3517 #if GUEST_PAGING_LEVELS == 2
3518 if ( sh_remove_write_access(v, gmfn, 2, 0) != 0 )
3519 flush_tlb_mask(v->domain->domain_dirty_cpumask);
3520 sh_set_toplevel_shadow(v, 0, gmfn, SH_type_l2_shadow);
3521 #elif GUEST_PAGING_LEVELS == 3
3522 /* PAE guests have four shadow_table entries, based on the
3523 * current values of the guest's four l3es. */
3525 int i, flush = 0;
3526 gfn_t gl2gfn;
3527 mfn_t gl2mfn;
3528 guest_l3e_t *gl3e = (guest_l3e_t*)v->arch.guest_vtable;
3529 /* First, make all four entries read-only. */
3530 for ( i = 0; i < 4; i++ )
3532 if ( guest_l3e_get_flags(gl3e[i]) & _PAGE_PRESENT )
3534 gl2gfn = guest_l3e_get_gfn(gl3e[i]);
3535 gl2mfn = vcpu_gfn_to_mfn(v, gl2gfn);
3536 flush |= sh_remove_write_access(v, gl2mfn, 2, 0);
3539 if ( flush )
3540 flush_tlb_mask(v->domain->domain_dirty_cpumask);
3541 /* Now install the new shadows. */
3542 for ( i = 0; i < 4; i++ )
3544 if ( guest_l3e_get_flags(gl3e[i]) & _PAGE_PRESENT )
3546 gl2gfn = guest_l3e_get_gfn(gl3e[i]);
3547 gl2mfn = vcpu_gfn_to_mfn(v, gl2gfn);
3548 sh_set_toplevel_shadow(v, i, gl2mfn, (i == 3)
3549 ? SH_type_l2h_shadow
3550 : SH_type_l2_shadow);
3552 else
3553 /* The guest is not present: clear out the shadow. */
3554 sh_set_toplevel_shadow(v, i, _mfn(INVALID_MFN), 0);
3557 #elif GUEST_PAGING_LEVELS == 4
3558 if ( sh_remove_write_access(v, gmfn, 4, 0) != 0 )
3559 flush_tlb_mask(v->domain->domain_dirty_cpumask);
3560 sh_set_toplevel_shadow(v, 0, gmfn, SH_type_l4_shadow);
3561 #else
3562 #error This should never happen
3563 #endif
3565 #if (CONFIG_PAGING_LEVELS == 3) && (GUEST_PAGING_LEVELS == 3)
3566 #endif
3568 ///
3569 /// v->arch.shadow.l3table
3570 ///
3571 #if SHADOW_PAGING_LEVELS == 3
3573 mfn_t smfn;
3574 int i;
3575 for ( i = 0; i < 4; i++ )
3577 #if GUEST_PAGING_LEVELS == 2
3578 /* 2-on-3: make a PAE l3 that points at the four-page l2 */
3579 smfn = _mfn(pagetable_get_pfn(v->arch.shadow_table[0]) + i);
3580 #else
3581 /* 3-on-3: make a PAE l3 that points at the four l2 pages */
3582 smfn = pagetable_get_mfn(v->arch.shadow_table[i]);
3583 #endif
3584 v->arch.shadow.l3table[i] =
3585 (mfn_x(smfn) == 0)
3586 ? shadow_l3e_empty()
3587 : shadow_l3e_from_mfn(smfn, _PAGE_PRESENT);
3590 #endif /* SHADOW_PAGING_LEVELS == 3 */
3593 ///
3594 /// v->arch.cr3
3595 ///
3596 if ( shadow_mode_external(d) )
3598 make_cr3(v, pagetable_get_pfn(v->arch.monitor_table));
3600 else // not shadow_mode_external...
3602 /* We don't support PV except guest == shadow == config levels */
3603 BUG_ON(GUEST_PAGING_LEVELS != SHADOW_PAGING_LEVELS);
3604 #if SHADOW_PAGING_LEVELS == 3
3605 /* 2-on-3 or 3-on-3: Use the PAE shadow l3 table we just fabricated.
3606 * Don't use make_cr3 because (a) we know it's below 4GB, and
3607 * (b) it's not necessarily page-aligned, and make_cr3 takes a pfn */
3608 ASSERT(virt_to_maddr(&v->arch.shadow.l3table) <= 0xffffffe0ULL);
3609 v->arch.cr3 = virt_to_maddr(&v->arch.shadow.l3table);
3610 #else
3611 /* 2-on-2 or 4-on-4: Just use the shadow top-level directly */
3612 make_cr3(v, pagetable_get_pfn(v->arch.shadow_table[0]));
3613 #endif
3617 ///
3618 /// v->arch.hvm_vcpu.hw_cr3
3619 ///
3620 if ( shadow_mode_external(d) )
3622 ASSERT(is_hvm_domain(d));
3623 #if SHADOW_PAGING_LEVELS == 3
3624 /* 2-on-3 or 3-on-3: Use the PAE shadow l3 table we just fabricated */
3625 hvm_update_guest_cr3(v, virt_to_maddr(&v->arch.shadow.l3table));
3626 #else
3627 /* 2-on-2 or 4-on-4: Just use the shadow top-level directly */
3628 hvm_update_guest_cr3(v, pagetable_get_paddr(v->arch.shadow_table[0]));
3629 #endif
3632 /* Fix up the linear pagetable mappings */
3633 sh_update_linear_entries(v);
3635 /* Release the lock, if we took it (otherwise it's the caller's problem) */
3636 if ( do_locking ) shadow_unlock(v->domain);
3640 /**************************************************************************/
3641 /* Functions to revoke guest rights */
3643 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
3644 static int sh_guess_wrmap(struct vcpu *v, unsigned long vaddr, mfn_t gmfn)
3645 /* Look up this vaddr in the current shadow and see if it's a writeable
3646 * mapping of this gmfn. If so, remove it. Returns 1 if it worked. */
3648 shadow_l1e_t sl1e, *sl1p;
3649 shadow_l2e_t *sl2p;
3650 #if SHADOW_PAGING_LEVELS >= 3
3651 shadow_l3e_t *sl3p;
3652 #if SHADOW_PAGING_LEVELS >= 4
3653 shadow_l4e_t *sl4p;
3654 #endif
3655 #endif
3656 mfn_t sl1mfn;
3657 int r;
3659 /* Carefully look in the shadow linear map for the l1e we expect */
3660 #if SHADOW_PAGING_LEVELS >= 4
3661 sl4p = sh_linear_l4_table(v) + shadow_l4_linear_offset(vaddr);
3662 if ( !(shadow_l4e_get_flags(*sl4p) & _PAGE_PRESENT) )
3663 return 0;
3664 sl3p = sh_linear_l3_table(v) + shadow_l3_linear_offset(vaddr);
3665 if ( !(shadow_l3e_get_flags(*sl3p) & _PAGE_PRESENT) )
3666 return 0;
3667 #elif SHADOW_PAGING_LEVELS == 3
3668 sl3p = ((shadow_l3e_t *) v->arch.shadow.l3table)
3669 + shadow_l3_linear_offset(vaddr);
3670 if ( !(shadow_l3e_get_flags(*sl3p) & _PAGE_PRESENT) )
3671 return 0;
3672 #endif
3673 sl2p = sh_linear_l2_table(v) + shadow_l2_linear_offset(vaddr);
3674 if ( !(shadow_l2e_get_flags(*sl2p) & _PAGE_PRESENT) )
3675 return 0;
3676 sl1p = sh_linear_l1_table(v) + shadow_l1_linear_offset(vaddr);
3677 sl1e = *sl1p;
3678 if ( ((shadow_l1e_get_flags(sl1e) & (_PAGE_PRESENT|_PAGE_RW))
3679 != (_PAGE_PRESENT|_PAGE_RW))
3680 || (mfn_x(shadow_l1e_get_mfn(sl1e)) != mfn_x(gmfn)) )
3681 return 0;
3683 /* Found it! Need to remove its write permissions. */
3684 sl1mfn = shadow_l2e_get_mfn(*sl2p);
3685 sl1e = shadow_l1e_remove_flags(sl1e, _PAGE_RW);
3686 r = shadow_set_l1e(v, sl1p, sl1e, sl1mfn);
3687 ASSERT( !(r & SHADOW_SET_ERROR) );
3688 return 1;
3690 #endif
3692 int sh_rm_write_access_from_l1(struct vcpu *v, mfn_t sl1mfn,
3693 mfn_t readonly_mfn)
3694 /* Excises all writeable mappings to readonly_mfn from this l1 shadow table */
3696 shadow_l1e_t *sl1e;
3697 int done = 0;
3698 int flags;
3699 mfn_t base_sl1mfn = sl1mfn; /* Because sl1mfn changes in the foreach */
3701 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done,
3703 flags = shadow_l1e_get_flags(*sl1e);
3704 if ( (flags & _PAGE_PRESENT)
3705 && (flags & _PAGE_RW)
3706 && (mfn_x(shadow_l1e_get_mfn(*sl1e)) == mfn_x(readonly_mfn)) )
3708 shadow_l1e_t ro_sl1e = shadow_l1e_remove_flags(*sl1e, _PAGE_RW);
3709 (void) shadow_set_l1e(v, sl1e, ro_sl1e, sl1mfn);
3710 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
3711 /* Remember the last shadow that we shot a writeable mapping in */
3712 v->arch.shadow.last_writeable_pte_smfn = mfn_x(base_sl1mfn);
3713 #endif
3714 if ( (mfn_to_page(readonly_mfn)->u.inuse.type_info
3715 & PGT_count_mask) == 0 )
3716 /* This breaks us cleanly out of the FOREACH macro */
3717 done = 1;
3719 });
3720 return done;
3724 int sh_rm_mappings_from_l1(struct vcpu *v, mfn_t sl1mfn, mfn_t target_mfn)
3725 /* Excises all mappings to guest frame from this shadow l1 table */
3727 shadow_l1e_t *sl1e;
3728 int done = 0;
3729 int flags;
3731 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done,
3733 flags = shadow_l1e_get_flags(*sl1e);
3734 if ( (flags & _PAGE_PRESENT)
3735 && (mfn_x(shadow_l1e_get_mfn(*sl1e)) == mfn_x(target_mfn)) )
3737 (void) shadow_set_l1e(v, sl1e, shadow_l1e_empty(), sl1mfn);
3738 if ( (mfn_to_page(target_mfn)->count_info & PGC_count_mask) == 0 )
3739 /* This breaks us cleanly out of the FOREACH macro */
3740 done = 1;
3742 });
3743 return done;
3746 /**************************************************************************/
3747 /* Functions to excise all pointers to shadows from higher-level shadows. */
3749 void sh_clear_shadow_entry(struct vcpu *v, void *ep, mfn_t smfn)
3750 /* Blank out a single shadow entry */
3752 switch ( mfn_to_shadow_page(smfn)->type )
3754 case SH_type_l1_shadow:
3755 (void) shadow_set_l1e(v, ep, shadow_l1e_empty(), smfn); break;
3756 case SH_type_l2_shadow:
3757 #if GUEST_PAGING_LEVELS == 3
3758 case SH_type_l2h_shadow:
3759 #endif
3760 (void) shadow_set_l2e(v, ep, shadow_l2e_empty(), smfn); break;
3761 #if GUEST_PAGING_LEVELS >= 4
3762 case SH_type_l3_shadow:
3763 (void) shadow_set_l3e(v, ep, shadow_l3e_empty(), smfn); break;
3764 case SH_type_l4_shadow:
3765 (void) shadow_set_l4e(v, ep, shadow_l4e_empty(), smfn); break;
3766 #endif
3767 default: BUG(); /* Called with the wrong kind of shadow. */
3771 int sh_remove_l1_shadow(struct vcpu *v, mfn_t sl2mfn, mfn_t sl1mfn)
3772 /* Remove all mappings of this l1 shadow from this l2 shadow */
3774 shadow_l2e_t *sl2e;
3775 int done = 0;
3776 int flags;
3777 #if GUEST_PAGING_LEVELS != 4
3778 int xen_mappings = !shadow_mode_external(v->domain);
3779 #endif
3781 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, done, xen_mappings,
3783 flags = shadow_l2e_get_flags(*sl2e);
3784 if ( (flags & _PAGE_PRESENT)
3785 && (mfn_x(shadow_l2e_get_mfn(*sl2e)) == mfn_x(sl1mfn)) )
3787 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
3788 if ( mfn_to_shadow_page(sl1mfn)->type == 0 )
3789 /* This breaks us cleanly out of the FOREACH macro */
3790 done = 1;
3792 });
3793 return done;
3796 #if GUEST_PAGING_LEVELS >= 4
3797 int sh_remove_l2_shadow(struct vcpu *v, mfn_t sl3mfn, mfn_t sl2mfn)
3798 /* Remove all mappings of this l2 shadow from this l3 shadow */
3800 shadow_l3e_t *sl3e;
3801 int done = 0;
3802 int flags;
3804 SHADOW_FOREACH_L3E(sl3mfn, sl3e, 0, done,
3806 flags = shadow_l3e_get_flags(*sl3e);
3807 if ( (flags & _PAGE_PRESENT)
3808 && (mfn_x(shadow_l3e_get_mfn(*sl3e)) == mfn_x(sl2mfn)) )
3810 (void) shadow_set_l3e(v, sl3e, shadow_l3e_empty(), sl3mfn);
3811 if ( mfn_to_shadow_page(sl2mfn)->type == 0 )
3812 /* This breaks us cleanly out of the FOREACH macro */
3813 done = 1;
3815 });
3816 return done;
3819 int sh_remove_l3_shadow(struct vcpu *v, mfn_t sl4mfn, mfn_t sl3mfn)
3820 /* Remove all mappings of this l3 shadow from this l4 shadow */
3822 shadow_l4e_t *sl4e;
3823 int done = 0;
3824 int flags, xen_mappings = !shadow_mode_external(v->domain);
3826 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, done, xen_mappings,
3828 flags = shadow_l4e_get_flags(*sl4e);
3829 if ( (flags & _PAGE_PRESENT)
3830 && (mfn_x(shadow_l4e_get_mfn(*sl4e)) == mfn_x(sl3mfn)) )
3832 (void) shadow_set_l4e(v, sl4e, shadow_l4e_empty(), sl4mfn);
3833 if ( mfn_to_shadow_page(sl3mfn)->type == 0 )
3834 /* This breaks us cleanly out of the FOREACH macro */
3835 done = 1;
3837 });
3838 return done;
3840 #endif /* 64bit guest */
3842 /**************************************************************************/
3843 /* Handling HVM guest writes to pagetables */
3845 /* Check that the user is allowed to perform this write.
3846 * Returns a mapped pointer to write to, and the mfn it's on,
3847 * or NULL for error. */
3848 static inline void * emulate_map_dest(struct vcpu *v,
3849 unsigned long vaddr,
3850 struct sh_emulate_ctxt *sh_ctxt,
3851 mfn_t *mfnp)
3853 walk_t gw;
3854 u32 flags, errcode;
3855 gfn_t gfn;
3856 mfn_t mfn;
3858 guest_walk_tables(v, vaddr, &gw, 1);
3859 flags = accumulate_guest_flags(v, &gw);
3860 gfn = guest_l1e_get_gfn(gw.eff_l1e);
3861 mfn = vcpu_gfn_to_mfn(v, gfn);
3862 sh_audit_gw(v, &gw);
3863 unmap_walk(v, &gw);
3865 if ( !(flags & _PAGE_PRESENT) )
3867 errcode = 0;
3868 goto page_fault;
3871 if ( !(flags & _PAGE_RW) ||
3872 (!(flags & _PAGE_USER) && ring_3(sh_ctxt->ctxt.regs)) )
3874 errcode = PFEC_page_present;
3875 goto page_fault;
3878 if ( !mfn_valid(mfn) )
3879 return NULL;
3881 *mfnp = mfn;
3882 return sh_map_domain_page(mfn) + (vaddr & ~PAGE_MASK);
3884 page_fault:
3885 errcode |= PFEC_write_access;
3886 if ( is_hvm_vcpu(v) )
3887 hvm_inject_exception(TRAP_page_fault, errcode, vaddr);
3888 else
3889 propagate_page_fault(vaddr, errcode);
3890 return NULL;
3893 static int safe_not_to_verify_write(mfn_t gmfn, void *dst, void *src,
3894 int bytes)
3896 #if (SHADOW_OPTIMIZATIONS & SHOPT_SKIP_VERIFY)
3897 struct page_info *pg = mfn_to_page(gmfn);
3898 if ( !(pg->shadow_flags & SHF_32)
3899 && bytes == 4
3900 && ((unsigned long)dst & 3) == 0 )
3902 /* Not shadowed 32-bit: aligned 64-bit writes that leave the
3903 * present bit unset are safe to ignore. */
3904 if ( (*(u64*)src & _PAGE_PRESENT) == 0
3905 && (*(u64*)dst & _PAGE_PRESENT) == 0 )
3906 return 1;
3908 else if ( !(pg->shadow_flags & (SHF_PAE|SHF_64))
3909 && bytes == 8
3910 && ((unsigned long)dst & 7) == 0 )
3912 /* Not shadowed PAE/64-bit: aligned 32-bit writes that leave the
3913 * present bit unset are safe to ignore. */
3914 if ( (*(u32*)src & _PAGE_PRESENT) == 0
3915 && (*(u32*)dst & _PAGE_PRESENT) == 0 )
3916 return 1;
3918 #endif
3919 return 0;
3923 int
3924 sh_x86_emulate_write(struct vcpu *v, unsigned long vaddr, void *src,
3925 u32 bytes, struct sh_emulate_ctxt *sh_ctxt)
3927 mfn_t mfn;
3928 void *addr;
3929 int skip;
3931 if ( vaddr & (bytes-1) )
3932 return X86EMUL_UNHANDLEABLE;
3934 ASSERT(shadow_locked_by_me(v->domain));
3935 ASSERT(((vaddr & ~PAGE_MASK) + bytes) <= PAGE_SIZE);
3937 if ( (addr = emulate_map_dest(v, vaddr, sh_ctxt, &mfn)) == NULL )
3938 return X86EMUL_PROPAGATE_FAULT;
3940 skip = safe_not_to_verify_write(mfn, addr, src, bytes);
3941 memcpy(addr, src, bytes);
3942 if ( !skip ) sh_validate_guest_pt_write(v, mfn, addr, bytes);
3944 /* If we are writing zeros to this page, might want to unshadow */
3945 if ( likely(bytes >= 4) && (*(u32 *)addr == 0) && is_lo_pte(vaddr) )
3946 check_for_early_unshadow(v, mfn);
3948 sh_unmap_domain_page(addr);
3949 shadow_audit_tables(v);
3950 return X86EMUL_CONTINUE;
3953 int
3954 sh_x86_emulate_cmpxchg(struct vcpu *v, unsigned long vaddr,
3955 unsigned long old, unsigned long new,
3956 unsigned int bytes, struct sh_emulate_ctxt *sh_ctxt)
3958 mfn_t mfn;
3959 void *addr;
3960 unsigned long prev;
3961 int rv = X86EMUL_CONTINUE, skip;
3963 ASSERT(shadow_locked_by_me(v->domain));
3964 ASSERT(bytes <= sizeof(unsigned long));
3966 if ( vaddr & (bytes-1) )
3967 return X86EMUL_UNHANDLEABLE;
3969 if ( (addr = emulate_map_dest(v, vaddr, sh_ctxt, &mfn)) == NULL )
3970 return X86EMUL_PROPAGATE_FAULT;
3972 skip = safe_not_to_verify_write(mfn, &new, &old, bytes);
3974 switch ( bytes )
3976 case 1: prev = cmpxchg(((u8 *)addr), old, new); break;
3977 case 2: prev = cmpxchg(((u16 *)addr), old, new); break;
3978 case 4: prev = cmpxchg(((u32 *)addr), old, new); break;
3979 case 8: prev = cmpxchg(((u64 *)addr), old, new); break;
3980 default:
3981 SHADOW_PRINTK("cmpxchg of size %i is not supported\n", bytes);
3982 prev = ~old;
3985 if ( prev == old )
3987 if ( !skip ) sh_validate_guest_pt_write(v, mfn, addr, bytes);
3989 else
3990 rv = X86EMUL_CMPXCHG_FAILED;
3992 SHADOW_DEBUG(EMULATE, "va %#lx was %#lx expected %#lx"
3993 " wanted %#lx now %#lx bytes %u\n",
3994 vaddr, prev, old, new, *(unsigned long *)addr, bytes);
3996 /* If we are writing zeros to this page, might want to unshadow */
3997 if ( likely(bytes >= 4) && (*(u32 *)addr == 0) && is_lo_pte(vaddr) )
3998 check_for_early_unshadow(v, mfn);
4000 sh_unmap_domain_page(addr);
4001 shadow_audit_tables(v);
4002 return rv;
4005 int
4006 sh_x86_emulate_cmpxchg8b(struct vcpu *v, unsigned long vaddr,
4007 unsigned long old_lo, unsigned long old_hi,
4008 unsigned long new_lo, unsigned long new_hi,
4009 struct sh_emulate_ctxt *sh_ctxt)
4011 mfn_t mfn;
4012 void *addr;
4013 u64 old, new, prev;
4014 int rv = X86EMUL_CONTINUE, skip;
4016 ASSERT(shadow_locked_by_me(v->domain));
4018 if ( vaddr & 7 )
4019 return X86EMUL_UNHANDLEABLE;
4021 if ( (addr = emulate_map_dest(v, vaddr, sh_ctxt, &mfn)) == NULL )
4022 return X86EMUL_PROPAGATE_FAULT;
4024 old = (((u64) old_hi) << 32) | (u64) old_lo;
4025 new = (((u64) new_hi) << 32) | (u64) new_lo;
4026 skip = safe_not_to_verify_write(mfn, &new, &old, 8);
4027 prev = cmpxchg(((u64 *)addr), old, new);
4029 if ( prev == old )
4031 if ( !skip ) sh_validate_guest_pt_write(v, mfn, addr, 8);
4033 else
4034 rv = X86EMUL_CMPXCHG_FAILED;
4036 /* If we are writing zeros to this page, might want to unshadow */
4037 if ( *(u32 *)addr == 0 )
4038 check_for_early_unshadow(v, mfn);
4040 sh_unmap_domain_page(addr);
4041 shadow_audit_tables(v);
4042 return rv;
4046 /**************************************************************************/
4047 /* Audit tools */
4049 #if SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES
4051 #define AUDIT_FAIL(_level, _fmt, _a...) do { \
4052 printk("Shadow %u-on-%u audit failed at level %i, index %i\n" \
4053 "gl" #_level "mfn = %" SH_PRI_mfn \
4054 " sl" #_level "mfn = %" SH_PRI_mfn \
4055 " &gl" #_level "e = %p &sl" #_level "e = %p" \
4056 " gl" #_level "e = %" SH_PRI_gpte \
4057 " sl" #_level "e = %" SH_PRI_pte "\nError: " _fmt "\n", \
4058 GUEST_PAGING_LEVELS, SHADOW_PAGING_LEVELS, \
4059 _level, guest_index(gl ## _level ## e), \
4060 mfn_x(gl ## _level ## mfn), mfn_x(sl ## _level ## mfn), \
4061 gl ## _level ## e, sl ## _level ## e, \
4062 gl ## _level ## e->l ## _level, sl ## _level ## e->l ## _level, \
4063 ##_a); \
4064 BUG(); \
4065 done = 1; \
4066 } while (0)
4069 static char * sh_audit_flags(struct vcpu *v, int level,
4070 int gflags, int sflags)
4071 /* Common code for auditing flag bits */
4073 if ( (sflags & _PAGE_PRESENT) && !(gflags & _PAGE_PRESENT) )
4074 return "shadow is present but guest is not present";
4075 if ( (sflags & _PAGE_GLOBAL) && !is_hvm_vcpu(v) )
4076 return "global bit set in PV shadow";
4077 if ( level == 2 && (sflags & _PAGE_PSE) )
4078 return "PS bit set in shadow";
4079 #if SHADOW_PAGING_LEVELS == 3
4080 if ( level == 3 ) return NULL; /* All the other bits are blank in PAEl3 */
4081 #endif
4082 if ( (sflags & _PAGE_PRESENT) && !(gflags & _PAGE_ACCESSED) )
4083 return "accessed bit not propagated";
4084 if ( (level == 1 || (level == 2 && (gflags & _PAGE_PSE)))
4085 && ((sflags & _PAGE_RW) && !(gflags & _PAGE_DIRTY)) )
4086 return "dirty bit not propagated";
4087 if ( (sflags & _PAGE_USER) != (gflags & _PAGE_USER) )
4088 return "user/supervisor bit does not match";
4089 if ( (sflags & _PAGE_NX_BIT) != (gflags & _PAGE_NX_BIT) )
4090 return "NX bit does not match";
4091 if ( (sflags & _PAGE_RW) && !(gflags & _PAGE_RW) )
4092 return "shadow grants write access but guest does not";
4093 return NULL;
4096 static inline mfn_t
4097 audit_gfn_to_mfn(struct vcpu *v, gfn_t gfn, mfn_t gmfn)
4098 /* Convert this gfn to an mfn in the manner appropriate for the
4099 * guest pagetable it's used in (gmfn) */
4101 if ( !shadow_mode_translate(v->domain) )
4102 return _mfn(gfn_x(gfn));
4104 if ( (mfn_to_page(gmfn)->u.inuse.type_info & PGT_type_mask)
4105 != PGT_writable_page )
4106 return _mfn(gfn_x(gfn)); /* This is a paging-disabled shadow */
4107 else
4108 return sh_gfn_to_mfn(v->domain, gfn_x(gfn));
4112 int sh_audit_l1_table(struct vcpu *v, mfn_t sl1mfn, mfn_t x)
4114 guest_l1e_t *gl1e, *gp;
4115 shadow_l1e_t *sl1e;
4116 mfn_t mfn, gmfn, gl1mfn;
4117 gfn_t gfn;
4118 char *s;
4119 int done = 0;
4121 /* Follow the backpointer */
4122 gl1mfn = _mfn(mfn_to_shadow_page(sl1mfn)->backpointer);
4123 gl1e = gp = sh_map_domain_page(gl1mfn);
4124 SHADOW_FOREACH_L1E(sl1mfn, sl1e, &gl1e, done, {
4126 if ( sh_l1e_is_magic(*sl1e) )
4128 #if (SHADOW_OPTIMIZATIONS & SHOPT_FAST_FAULT_PATH) && SHADOW_PAGING_LEVELS > 2
4129 if ( sh_l1e_is_gnp(*sl1e) )
4131 if ( guest_l1e_get_flags(*gl1e) & _PAGE_PRESENT )
4132 AUDIT_FAIL(1, "shadow is GNP magic but guest is present");
4134 else
4136 ASSERT(sh_l1e_is_mmio(*sl1e));
4137 gfn = sh_l1e_mmio_get_gfn(*sl1e);
4138 if ( gfn_x(gfn) != gfn_x(guest_l1e_get_gfn(*gl1e)) )
4139 AUDIT_FAIL(1, "shadow MMIO gfn is %" SH_PRI_gfn
4140 " but guest gfn is %" SH_PRI_gfn,
4141 gfn_x(gfn),
4142 gfn_x(guest_l1e_get_gfn(*gl1e)));
4144 #endif
4146 else
4148 s = sh_audit_flags(v, 1, guest_l1e_get_flags(*gl1e),
4149 shadow_l1e_get_flags(*sl1e));
4150 if ( s ) AUDIT_FAIL(1, "%s", s);
4152 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4154 gfn = guest_l1e_get_gfn(*gl1e);
4155 mfn = shadow_l1e_get_mfn(*sl1e);
4156 gmfn = audit_gfn_to_mfn(v, gfn, gl1mfn);
4157 if ( mfn_x(gmfn) != mfn_x(mfn) )
4158 AUDIT_FAIL(1, "bad translation: gfn %" SH_PRI_gfn
4159 " --> %" SH_PRI_mfn " != mfn %" SH_PRI_mfn,
4160 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
4163 });
4164 sh_unmap_domain_page(gp);
4165 return done;
4168 int sh_audit_fl1_table(struct vcpu *v, mfn_t sl1mfn, mfn_t x)
4170 guest_l1e_t *gl1e, e;
4171 shadow_l1e_t *sl1e;
4172 mfn_t gl1mfn = _mfn(INVALID_MFN);
4173 int f;
4174 int done = 0;
4176 /* fl1 has no useful backpointer: all we can check are flags */
4177 e = guest_l1e_from_gfn(_gfn(0), 0); gl1e = &e; /* Needed for macro */
4178 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done, {
4179 f = shadow_l1e_get_flags(*sl1e);
4180 f &= ~(_PAGE_AVAIL0|_PAGE_AVAIL1|_PAGE_AVAIL2);
4181 if ( !(f == 0
4182 || f == (_PAGE_PRESENT|_PAGE_USER|_PAGE_RW|
4183 _PAGE_ACCESSED|_PAGE_DIRTY)
4184 || f == (_PAGE_PRESENT|_PAGE_USER|_PAGE_ACCESSED|_PAGE_DIRTY)
4185 || sh_l1e_is_magic(*sl1e)) )
4186 AUDIT_FAIL(1, "fl1e has bad flags");
4187 });
4188 return 0;
4191 int sh_audit_l2_table(struct vcpu *v, mfn_t sl2mfn, mfn_t x)
4193 guest_l2e_t *gl2e, *gp;
4194 shadow_l2e_t *sl2e;
4195 mfn_t mfn, gmfn, gl2mfn;
4196 gfn_t gfn;
4197 char *s;
4198 int done = 0;
4199 #if GUEST_PAGING_LEVELS != 4
4200 int xen_mappings = !shadow_mode_external(v->domain);
4201 #endif
4203 /* Follow the backpointer */
4204 gl2mfn = _mfn(mfn_to_shadow_page(sl2mfn)->backpointer);
4205 gl2e = gp = sh_map_domain_page(gl2mfn);
4206 SHADOW_FOREACH_L2E(sl2mfn, sl2e, &gl2e, done, xen_mappings, {
4208 s = sh_audit_flags(v, 2, guest_l2e_get_flags(*gl2e),
4209 shadow_l2e_get_flags(*sl2e));
4210 if ( s ) AUDIT_FAIL(2, "%s", s);
4212 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4214 gfn = guest_l2e_get_gfn(*gl2e);
4215 mfn = shadow_l2e_get_mfn(*sl2e);
4216 gmfn = (guest_l2e_get_flags(*gl2e) & _PAGE_PSE)
4217 ? get_fl1_shadow_status(v, gfn)
4218 : get_shadow_status(v, audit_gfn_to_mfn(v, gfn, gl2mfn),
4219 SH_type_l1_shadow);
4220 if ( mfn_x(gmfn) != mfn_x(mfn) )
4221 AUDIT_FAIL(2, "bad translation: gfn %" SH_PRI_gfn
4222 " (--> %" SH_PRI_mfn ")"
4223 " --> %" SH_PRI_mfn " != mfn %" SH_PRI_mfn,
4224 gfn_x(gfn),
4225 (guest_l2e_get_flags(*gl2e) & _PAGE_PSE) ? 0
4226 : mfn_x(audit_gfn_to_mfn(v, gfn, gl2mfn)),
4227 mfn_x(gmfn), mfn_x(mfn));
4229 });
4230 sh_unmap_domain_page(gp);
4231 return 0;
4234 #if GUEST_PAGING_LEVELS >= 4
4235 int sh_audit_l3_table(struct vcpu *v, mfn_t sl3mfn, mfn_t x)
4237 guest_l3e_t *gl3e, *gp;
4238 shadow_l3e_t *sl3e;
4239 mfn_t mfn, gmfn, gl3mfn;
4240 gfn_t gfn;
4241 char *s;
4242 int done = 0;
4244 /* Follow the backpointer */
4245 gl3mfn = _mfn(mfn_to_shadow_page(sl3mfn)->backpointer);
4246 gl3e = gp = sh_map_domain_page(gl3mfn);
4247 SHADOW_FOREACH_L3E(sl3mfn, sl3e, &gl3e, done, {
4249 s = sh_audit_flags(v, 3, guest_l3e_get_flags(*gl3e),
4250 shadow_l3e_get_flags(*sl3e));
4251 if ( s ) AUDIT_FAIL(3, "%s", s);
4253 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4255 gfn = guest_l3e_get_gfn(*gl3e);
4256 mfn = shadow_l3e_get_mfn(*sl3e);
4257 gmfn = get_shadow_status(v, audit_gfn_to_mfn(v, gfn, gl3mfn),
4258 (GUEST_PAGING_LEVELS == 3
4259 && !shadow_mode_external(v->domain)
4260 && (guest_index(gl3e) % 4) == 3)
4261 ? SH_type_l2h_pae_shadow
4262 : SH_type_l2_shadow);
4263 if ( mfn_x(gmfn) != mfn_x(mfn) )
4264 AUDIT_FAIL(3, "bad translation: gfn %" SH_PRI_gfn
4265 " --> %" SH_PRI_mfn " != mfn %" SH_PRI_mfn,
4266 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
4268 });
4269 sh_unmap_domain_page(gp);
4270 return 0;
4273 int sh_audit_l4_table(struct vcpu *v, mfn_t sl4mfn, mfn_t x)
4275 guest_l4e_t *gl4e, *gp;
4276 shadow_l4e_t *sl4e;
4277 mfn_t mfn, gmfn, gl4mfn;
4278 gfn_t gfn;
4279 char *s;
4280 int done = 0;
4281 int xen_mappings = !shadow_mode_external(v->domain);
4283 /* Follow the backpointer */
4284 gl4mfn = _mfn(mfn_to_shadow_page(sl4mfn)->backpointer);
4285 gl4e = gp = sh_map_domain_page(gl4mfn);
4286 SHADOW_FOREACH_L4E(sl4mfn, sl4e, &gl4e, done, xen_mappings,
4288 s = sh_audit_flags(v, 4, guest_l4e_get_flags(*gl4e),
4289 shadow_l4e_get_flags(*sl4e));
4290 if ( s ) AUDIT_FAIL(4, "%s", s);
4292 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4294 gfn = guest_l4e_get_gfn(*gl4e);
4295 mfn = shadow_l4e_get_mfn(*sl4e);
4296 gmfn = get_shadow_status(v, audit_gfn_to_mfn(v, gfn, gl4mfn),
4297 SH_type_l3_shadow);
4298 if ( mfn_x(gmfn) != mfn_x(mfn) )
4299 AUDIT_FAIL(4, "bad translation: gfn %" SH_PRI_gfn
4300 " --> %" SH_PRI_mfn " != mfn %" SH_PRI_mfn,
4301 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
4303 });
4304 sh_unmap_domain_page(gp);
4305 return 0;
4307 #endif /* GUEST_PAGING_LEVELS >= 4 */
4310 #undef AUDIT_FAIL
4312 #endif /* Audit code */
4314 /**************************************************************************/
4315 /* Entry points into this mode of the shadow code.
4316 * This will all be mangled by the preprocessor to uniquify everything. */
4317 struct shadow_paging_mode sh_paging_mode = {
4318 .page_fault = sh_page_fault,
4319 .invlpg = sh_invlpg,
4320 .gva_to_gpa = sh_gva_to_gpa,
4321 .gva_to_gfn = sh_gva_to_gfn,
4322 .update_cr3 = sh_update_cr3,
4323 .map_and_validate_gl1e = sh_map_and_validate_gl1e,
4324 .map_and_validate_gl2e = sh_map_and_validate_gl2e,
4325 .map_and_validate_gl2he = sh_map_and_validate_gl2he,
4326 .map_and_validate_gl3e = sh_map_and_validate_gl3e,
4327 .map_and_validate_gl4e = sh_map_and_validate_gl4e,
4328 .detach_old_tables = sh_detach_old_tables,
4329 .x86_emulate_write = sh_x86_emulate_write,
4330 .x86_emulate_cmpxchg = sh_x86_emulate_cmpxchg,
4331 .x86_emulate_cmpxchg8b = sh_x86_emulate_cmpxchg8b,
4332 .make_monitor_table = sh_make_monitor_table,
4333 .destroy_monitor_table = sh_destroy_monitor_table,
4334 .guest_map_l1e = sh_guest_map_l1e,
4335 .guest_get_eff_l1e = sh_guest_get_eff_l1e,
4336 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
4337 .guess_wrmap = sh_guess_wrmap,
4338 #endif
4339 .guest_levels = GUEST_PAGING_LEVELS,
4340 .shadow_levels = SHADOW_PAGING_LEVELS,
4341 };
4343 /*
4344 * Local variables:
4345 * mode: C
4346 * c-set-style: "BSD"
4347 * c-basic-offset: 4
4348 * indent-tabs-mode: nil
4349 * End:
4350 */