ia64/xen-unstable

view xen/arch/x86/mm/shadow/multi.c @ 16197:b3fa9b58a102

hvm, vt-d: Add memory cache-attribute pinning domctl for HVM
guests. Use this to pin virtual framebuffer VRAM as attribute WB, even
if guest tries to map with other attributes.
Signed-off-by: Disheng Su <disheng.su@intel.com>
author Keir Fraser <keir@xensource.com>
date Tue Oct 23 14:38:47 2007 +0100 (2007-10-23)
parents 3e7c86602c70
children 9379c83e14b5
line source
1 /******************************************************************************
2 * arch/x86/mm/shadow/multi.c
3 *
4 * Simple, mostly-synchronous shadow page tables.
5 * Parts of this code are Copyright (c) 2006 by XenSource Inc.
6 * Parts of this code are Copyright (c) 2006 by Michael A Fetterman
7 * Parts based on earlier work by Michael A Fetterman, Ian Pratt et al.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
24 #include <xen/config.h>
25 #include <xen/types.h>
26 #include <xen/mm.h>
27 #include <xen/trace.h>
28 #include <xen/sched.h>
29 #include <xen/perfc.h>
30 #include <xen/domain_page.h>
31 #include <asm/page.h>
32 #include <asm/current.h>
33 #include <asm/shadow.h>
34 #include <asm/flushtlb.h>
35 #include <asm/hvm/hvm.h>
36 #include <asm/hvm/cacheattr.h>
37 #include <asm/mtrr.h>
38 #include "private.h"
39 #include "types.h"
41 /* THINGS TO DO LATER:
42 *
43 * TEARDOWN HEURISTICS
44 * Also: have a heuristic for when to destroy a previous paging-mode's
45 * shadows. When a guest is done with its start-of-day 32-bit tables
46 * and reuses the memory we want to drop those shadows. Start with
47 * shadows in a page in two modes as a hint, but beware of clever tricks
48 * like reusing a pagetable for both PAE and 64-bit during boot...
49 *
50 * PAE LINEAR MAPS
51 * Rework shadow_get_l*e() to have the option of using map_domain_page()
52 * instead of linear maps. Add appropriate unmap_l*e calls in the users.
53 * Then we can test the speed difference made by linear maps. If the
54 * map_domain_page() version is OK on PAE, we could maybe allow a lightweight
55 * l3-and-l2h-only shadow mode for PAE PV guests that would allow them
56 * to share l2h pages again.
57 *
58 * GUEST_WALK_TABLES TLB FLUSH COALESCE
59 * guest_walk_tables can do up to three remote TLB flushes as it walks to
60 * the first l1 of a new pagetable. Should coalesce the flushes to the end,
61 * and if we do flush, re-do the walk. If anything has changed, then
62 * pause all the other vcpus and do the walk *again*.
63 *
64 * WP DISABLED
65 * Consider how to implement having the WP bit of CR0 set to 0.
66 * Since we need to be able to cause write faults to pagetables, this might
67 * end up looking like not having the (guest) pagetables present at all in
68 * HVM guests...
69 *
70 * PSE disabled / PSE36
71 * We don't support any modes other than PSE enabled, PSE36 disabled.
72 * Neither of those would be hard to change, but we'd need to be able to
73 * deal with shadows made in one mode and used in another.
74 */
76 #define FETCH_TYPE_PREFETCH 1
77 #define FETCH_TYPE_DEMAND 2
78 #define FETCH_TYPE_WRITE 4
79 typedef enum {
80 ft_prefetch = FETCH_TYPE_PREFETCH,
81 ft_demand_read = FETCH_TYPE_DEMAND,
82 ft_demand_write = FETCH_TYPE_DEMAND | FETCH_TYPE_WRITE,
83 } fetch_type_t;
85 #ifdef DEBUG_TRACE_DUMP
86 static char *fetch_type_names[] = {
87 [ft_prefetch] "prefetch",
88 [ft_demand_read] "demand read",
89 [ft_demand_write] "demand write",
90 };
91 #endif
93 /**************************************************************************/
94 /* Hash table mapping from guest pagetables to shadows
95 *
96 * Normal case: maps the mfn of a guest page to the mfn of its shadow page.
97 * FL1's: maps the *gfn* of the start of a superpage to the mfn of a
98 * shadow L1 which maps its "splinters".
99 */
101 static inline mfn_t
102 get_fl1_shadow_status(struct vcpu *v, gfn_t gfn)
103 /* Look for FL1 shadows in the hash table */
104 {
105 mfn_t smfn = shadow_hash_lookup(v, gfn_x(gfn), SH_type_fl1_shadow);
106 return smfn;
107 }
109 static inline mfn_t
110 get_shadow_status(struct vcpu *v, mfn_t gmfn, u32 shadow_type)
111 /* Look for shadows in the hash table */
112 {
113 mfn_t smfn = shadow_hash_lookup(v, mfn_x(gmfn), shadow_type);
114 perfc_incr(shadow_get_shadow_status);
115 return smfn;
116 }
118 static inline void
119 set_fl1_shadow_status(struct vcpu *v, gfn_t gfn, mfn_t smfn)
120 /* Put an FL1 shadow into the hash table */
121 {
122 SHADOW_PRINTK("gfn=%"SH_PRI_gfn", type=%08x, smfn=%05lx\n",
123 gfn_x(gfn), SH_type_fl1_shadow, mfn_x(smfn));
125 shadow_hash_insert(v, gfn_x(gfn), SH_type_fl1_shadow, smfn);
126 }
128 static inline void
129 set_shadow_status(struct vcpu *v, mfn_t gmfn, u32 shadow_type, mfn_t smfn)
130 /* Put a shadow into the hash table */
131 {
132 struct domain *d = v->domain;
133 int res;
135 SHADOW_PRINTK("d=%d, v=%d, gmfn=%05lx, type=%08x, smfn=%05lx\n",
136 d->domain_id, v->vcpu_id, mfn_x(gmfn),
137 shadow_type, mfn_x(smfn));
139 /* 32-on-64 PV guests don't own their l4 pages so can't get_page them */
140 if ( !is_pv_32on64_vcpu(v) || shadow_type != SH_type_l4_64_shadow )
141 {
142 res = get_page(mfn_to_page(gmfn), d);
143 ASSERT(res == 1);
144 }
146 shadow_hash_insert(v, mfn_x(gmfn), shadow_type, smfn);
147 }
149 static inline void
150 delete_fl1_shadow_status(struct vcpu *v, gfn_t gfn, mfn_t smfn)
151 /* Remove a shadow from the hash table */
152 {
153 SHADOW_PRINTK("gfn=%"SH_PRI_gfn", type=%08x, smfn=%05lx\n",
154 gfn_x(gfn), SH_type_fl1_shadow, mfn_x(smfn));
155 shadow_hash_delete(v, gfn_x(gfn), SH_type_fl1_shadow, smfn);
156 }
158 static inline void
159 delete_shadow_status(struct vcpu *v, mfn_t gmfn, u32 shadow_type, mfn_t smfn)
160 /* Remove a shadow from the hash table */
161 {
162 SHADOW_PRINTK("d=%d, v=%d, gmfn=%05lx, type=%08x, smfn=%05lx\n",
163 v->domain->domain_id, v->vcpu_id,
164 mfn_x(gmfn), shadow_type, mfn_x(smfn));
165 shadow_hash_delete(v, mfn_x(gmfn), shadow_type, smfn);
166 /* 32-on-64 PV guests don't own their l4 pages; see set_shadow_status */
167 if ( !is_pv_32on64_vcpu(v) || shadow_type != SH_type_l4_64_shadow )
168 put_page(mfn_to_page(gmfn));
169 }
171 /**************************************************************************/
172 /* CPU feature support querying */
174 static inline int
175 guest_supports_superpages(struct vcpu *v)
176 {
177 /* The _PAGE_PSE bit must be honoured in HVM guests, whenever
178 * CR4.PSE is set or the guest is in PAE or long mode.
179 * It's also used in the dummy PT for vcpus with CR4.PG cleared. */
180 return (is_hvm_vcpu(v) &&
181 (GUEST_PAGING_LEVELS != 2
182 || !hvm_paging_enabled(v)
183 || (v->arch.hvm_vcpu.guest_cr[4] & X86_CR4_PSE)));
184 }
186 static inline int
187 guest_supports_nx(struct vcpu *v)
188 {
189 if ( GUEST_PAGING_LEVELS == 2 || !cpu_has_nx )
190 return 0;
191 if ( !is_hvm_vcpu(v) )
192 return 1;
193 return hvm_nx_enabled(v);
194 }
197 /**************************************************************************/
198 /* Functions for walking the guest page tables */
201 /* Walk the guest pagetables, filling the walk_t with what we see.
202 * Takes an uninitialised walk_t. The caller must call unmap_walk()
203 * on the walk_t before discarding it or calling guest_walk_tables again.
204 * If "guest_op" is non-zero, we are serving a genuine guest memory access,
205 * and must (a) be under the shadow lock, and (b) remove write access
206 * from any gueat PT pages we see, as we will be using their contents to
207 * perform shadow updates.
208 * Returns 0 for success or non-zero if the guest pagetables are malformed.
209 * N.B. Finding a not-present entry does not cause a non-zero return code. */
210 static inline int
211 guest_walk_tables(struct vcpu *v, unsigned long va, walk_t *gw, int guest_op)
212 {
213 struct domain *d = v->domain;
214 p2m_type_t p2mt;
215 ASSERT(!guest_op || shadow_locked_by_me(d));
217 perfc_incr(shadow_guest_walk);
218 memset(gw, 0, sizeof(*gw));
219 gw->va = va;
221 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
222 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
223 /* Get l4e from the top level table */
224 gw->l4mfn = pagetable_get_mfn(v->arch.guest_table);
225 gw->l4e = (guest_l4e_t *)v->arch.paging.shadow.guest_vtable
226 + guest_l4_table_offset(va);
227 /* Walk down to the l3e */
228 if ( !(guest_l4e_get_flags(*gw->l4e) & _PAGE_PRESENT) ) return 0;
229 gw->l3mfn = gfn_to_mfn(d, guest_l4e_get_gfn(*gw->l4e), &p2mt);
230 if ( !p2m_is_ram(p2mt) ) return 1;
231 ASSERT(mfn_valid(gw->l3mfn));
232 /* This mfn is a pagetable: make sure the guest can't write to it. */
233 if ( guest_op && sh_remove_write_access(v, gw->l3mfn, 3, va) != 0 )
234 flush_tlb_mask(d->domain_dirty_cpumask);
235 gw->l3e = ((guest_l3e_t *)sh_map_domain_page(gw->l3mfn))
236 + guest_l3_table_offset(va);
237 #else /* PAE only... */
238 /* Get l3e from the cache of the guest's top level table */
239 gw->l3e = (guest_l3e_t *)&v->arch.paging.shadow.gl3e[guest_l3_table_offset(va)];
240 #endif /* PAE or 64... */
241 /* Walk down to the l2e */
242 if ( !(guest_l3e_get_flags(*gw->l3e) & _PAGE_PRESENT) ) return 0;
243 gw->l2mfn = gfn_to_mfn(d, guest_l3e_get_gfn(*gw->l3e), &p2mt);
244 if ( !p2m_is_ram(p2mt) ) return 1;
245 ASSERT(mfn_valid(gw->l2mfn));
246 /* This mfn is a pagetable: make sure the guest can't write to it. */
247 if ( guest_op && sh_remove_write_access(v, gw->l2mfn, 2, va) != 0 )
248 flush_tlb_mask(d->domain_dirty_cpumask);
249 gw->l2e = ((guest_l2e_t *)sh_map_domain_page(gw->l2mfn))
250 + guest_l2_table_offset(va);
251 #else /* 32-bit only... */
252 /* Get l2e from the top level table */
253 gw->l2mfn = pagetable_get_mfn(v->arch.guest_table);
254 gw->l2e = (guest_l2e_t *)v->arch.paging.shadow.guest_vtable
255 + guest_l2_table_offset(va);
256 #endif /* All levels... */
258 if ( !(guest_l2e_get_flags(*gw->l2e) & _PAGE_PRESENT) ) return 0;
259 if ( guest_supports_superpages(v) &&
260 (guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE) )
261 {
262 /* Special case: this guest VA is in a PSE superpage, so there's
263 * no guest l1e. We make one up so that the propagation code
264 * can generate a shadow l1 table. Start with the gfn of the
265 * first 4k-page of the superpage. */
266 gfn_t start = guest_l2e_get_gfn(*gw->l2e);
267 /* Grant full access in the l1e, since all the guest entry's
268 * access controls are enforced in the shadow l2e. This lets
269 * us reflect l2 changes later without touching the l1s. */
270 int flags = (_PAGE_PRESENT|_PAGE_USER|_PAGE_RW|
271 _PAGE_ACCESSED|_PAGE_DIRTY);
272 /* propagate PWT PCD to level 1 for PSE */
273 if ( (guest_l2e_get_flags(*gw->l2e) & _PAGE_PWT) )
274 flags |= _PAGE_PWT;
275 if ( (guest_l2e_get_flags(*gw->l2e) & _PAGE_PCD) )
276 flags |= _PAGE_PCD;
277 /* PSE level 2 entries use bit 12 for PAT; propagate it to bit 7
278 * of the level 1 */
279 if ( (guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE_PAT) )
280 flags |= _PAGE_PAT;
281 /* Increment the pfn by the right number of 4k pages.
282 * The ~0x1 is to mask out the PAT bit mentioned above. */
283 start = _gfn((gfn_x(start) & ~0x1) + guest_l1_table_offset(va));
284 gw->eff_l1e = guest_l1e_from_gfn(start, flags);
285 gw->l1e = NULL;
286 gw->l1mfn = _mfn(INVALID_MFN);
287 }
288 else
289 {
290 /* Not a superpage: carry on and find the l1e. */
291 gw->l1mfn = gfn_to_mfn(d, guest_l2e_get_gfn(*gw->l2e), &p2mt);
292 if ( !p2m_is_ram(p2mt) ) return 1;
293 ASSERT(mfn_valid(gw->l1mfn));
294 /* This mfn is a pagetable: make sure the guest can't write to it. */
295 if ( guest_op
296 && sh_remove_write_access(v, gw->l1mfn, 1, va) != 0 )
297 flush_tlb_mask(d->domain_dirty_cpumask);
298 gw->l1e = ((guest_l1e_t *)sh_map_domain_page(gw->l1mfn))
299 + guest_l1_table_offset(va);
300 gw->eff_l1e = *gw->l1e;
301 }
303 return 0;
304 }
306 /* Given a walk_t, translate the gw->va into the guest's notion of the
307 * corresponding frame number. */
308 static inline gfn_t
309 guest_walk_to_gfn(walk_t *gw)
310 {
311 if ( !(guest_l1e_get_flags(gw->eff_l1e) & _PAGE_PRESENT) )
312 return _gfn(INVALID_GFN);
313 return guest_l1e_get_gfn(gw->eff_l1e);
314 }
316 /* Given a walk_t, translate the gw->va into the guest's notion of the
317 * corresponding physical address. */
318 static inline paddr_t
319 guest_walk_to_gpa(walk_t *gw)
320 {
321 if ( !(guest_l1e_get_flags(gw->eff_l1e) & _PAGE_PRESENT) )
322 return 0;
323 return guest_l1e_get_paddr(gw->eff_l1e) + (gw->va & ~PAGE_MASK);
324 }
327 /* Unmap (and reinitialise) a guest walk.
328 * Call this to dispose of any walk filled in by guest_walk_tables() */
329 static void unmap_walk(struct vcpu *v, walk_t *gw)
330 {
331 #if GUEST_PAGING_LEVELS >= 3
332 #if GUEST_PAGING_LEVELS >= 4
333 if ( gw->l3e != NULL ) sh_unmap_domain_page(gw->l3e);
334 #endif
335 if ( gw->l2e != NULL ) sh_unmap_domain_page(gw->l2e);
336 #endif
337 if ( gw->l1e != NULL ) sh_unmap_domain_page(gw->l1e);
338 #ifdef DEBUG
339 memset(gw, 0, sizeof(*gw));
340 #endif
341 }
344 /* Pretty-print the contents of a guest-walk */
345 static inline void print_gw(walk_t *gw)
346 {
347 SHADOW_PRINTK("GUEST WALK TO %#lx:\n", gw->va);
348 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
349 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
350 SHADOW_PRINTK(" l4mfn=%" PRI_mfn "\n", mfn_x(gw->l4mfn));
351 SHADOW_PRINTK(" l4e=%p\n", gw->l4e);
352 if ( gw->l4e )
353 SHADOW_PRINTK(" *l4e=%" SH_PRI_gpte "\n", gw->l4e->l4);
354 SHADOW_PRINTK(" l3mfn=%" PRI_mfn "\n", mfn_x(gw->l3mfn));
355 #endif /* PAE or 64... */
356 SHADOW_PRINTK(" l3e=%p\n", gw->l3e);
357 if ( gw->l3e )
358 SHADOW_PRINTK(" *l3e=%" SH_PRI_gpte "\n", gw->l3e->l3);
359 #endif /* All levels... */
360 SHADOW_PRINTK(" l2mfn=%" PRI_mfn "\n", mfn_x(gw->l2mfn));
361 SHADOW_PRINTK(" l2e=%p\n", gw->l2e);
362 if ( gw->l2e )
363 SHADOW_PRINTK(" *l2e=%" SH_PRI_gpte "\n", gw->l2e->l2);
364 SHADOW_PRINTK(" l1mfn=%" PRI_mfn "\n", mfn_x(gw->l1mfn));
365 SHADOW_PRINTK(" l1e=%p\n", gw->l1e);
366 if ( gw->l1e )
367 SHADOW_PRINTK(" *l1e=%" SH_PRI_gpte "\n", gw->l1e->l1);
368 SHADOW_PRINTK(" eff_l1e=%" SH_PRI_gpte "\n", gw->eff_l1e.l1);
369 }
372 #if SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES
373 /* Lightweight audit: pass all the shadows associated with this guest walk
374 * through the audit mechanisms */
375 static void sh_audit_gw(struct vcpu *v, walk_t *gw)
376 {
377 mfn_t smfn;
379 if ( !(SHADOW_AUDIT_ENABLE) )
380 return;
382 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
383 if ( mfn_valid(gw->l4mfn)
384 && mfn_valid((smfn = get_shadow_status(v, gw->l4mfn,
385 SH_type_l4_shadow))) )
386 (void) sh_audit_l4_table(v, smfn, _mfn(INVALID_MFN));
387 if ( mfn_valid(gw->l3mfn)
388 && mfn_valid((smfn = get_shadow_status(v, gw->l3mfn,
389 SH_type_l3_shadow))) )
390 (void) sh_audit_l3_table(v, smfn, _mfn(INVALID_MFN));
391 #endif /* PAE or 64... */
392 if ( mfn_valid(gw->l2mfn) )
393 {
394 if ( mfn_valid((smfn = get_shadow_status(v, gw->l2mfn,
395 SH_type_l2_shadow))) )
396 (void) sh_audit_l2_table(v, smfn, _mfn(INVALID_MFN));
397 #if GUEST_PAGING_LEVELS == 3
398 if ( mfn_valid((smfn = get_shadow_status(v, gw->l2mfn,
399 SH_type_l2h_shadow))) )
400 (void) sh_audit_l2_table(v, smfn, _mfn(INVALID_MFN));
401 #endif
402 }
403 if ( mfn_valid(gw->l1mfn)
404 && mfn_valid((smfn = get_shadow_status(v, gw->l1mfn,
405 SH_type_l1_shadow))) )
406 (void) sh_audit_l1_table(v, smfn, _mfn(INVALID_MFN));
407 else if ( gw->l2e
408 && (guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE)
409 && mfn_valid(
410 (smfn = get_fl1_shadow_status(v, guest_l2e_get_gfn(*gw->l2e)))) )
411 (void) sh_audit_fl1_table(v, smfn, _mfn(INVALID_MFN));
412 }
414 #else
415 #define sh_audit_gw(_v, _gw) do {} while(0)
416 #endif /* audit code */
420 /**************************************************************************/
421 /* Function to write to the guest tables, for propagating accessed and
422 * dirty bits from the shadow to the guest.
423 * Takes a guest mfn, a pointer to the guest entry, the level of pagetable,
424 * and an operation type. The guest entry is always passed as an l1e:
425 * since we only ever write flags, that's OK.
426 * Returns the new flag bits of the guest entry. */
428 static u32 guest_set_ad_bits(struct vcpu *v,
429 mfn_t gmfn,
430 guest_l1e_t *ep,
431 unsigned int level,
432 fetch_type_t ft)
433 {
434 u32 flags;
435 int res = 0;
437 ASSERT(ep && !(((unsigned long)ep) & ((sizeof *ep) - 1)));
438 ASSERT(level <= GUEST_PAGING_LEVELS);
439 ASSERT(shadow_locked_by_me(v->domain));
441 flags = guest_l1e_get_flags(*ep);
443 /* Only set A and D bits for guest-initiated accesses */
444 if ( !(ft & FETCH_TYPE_DEMAND) )
445 return flags;
447 ASSERT(mfn_valid(gmfn)
448 && (sh_mfn_is_a_page_table(gmfn)
449 || ((mfn_to_page(gmfn)->u.inuse.type_info & PGT_count_mask)
450 == 0)));
452 /* PAE l3s do not have A and D bits */
453 ASSERT(GUEST_PAGING_LEVELS > 3 || level != 3);
455 /* Need the D bit as well for writes, in L1es and PSE L2es. */
456 if ( ft == ft_demand_write
457 && (level == 1 ||
458 (level == 2 && (flags & _PAGE_PSE) && guest_supports_superpages(v))) )
459 {
460 if ( (flags & (_PAGE_DIRTY | _PAGE_ACCESSED))
461 == (_PAGE_DIRTY | _PAGE_ACCESSED) )
462 return flags; /* Guest already has A and D bits set */
463 flags |= _PAGE_DIRTY | _PAGE_ACCESSED;
464 perfc_incr(shadow_ad_update);
465 }
466 else
467 {
468 if ( flags & _PAGE_ACCESSED )
469 return flags; /* Guest already has A bit set */
470 flags |= _PAGE_ACCESSED;
471 perfc_incr(shadow_a_update);
472 }
474 /* Set the bit(s) */
475 paging_mark_dirty(v->domain, mfn_x(gmfn));
476 SHADOW_DEBUG(A_AND_D, "gfn = %" SH_PRI_gfn ", "
477 "old flags = %#x, new flags = %#x\n",
478 gfn_x(guest_l1e_get_gfn(*ep)), guest_l1e_get_flags(*ep),
479 flags);
480 *ep = guest_l1e_from_gfn(guest_l1e_get_gfn(*ep), flags);
482 /* Propagate this change to any other shadows of the page
483 * (only necessary if there is more than one shadow) */
484 if ( mfn_to_page(gmfn)->count_info & PGC_page_table )
485 {
486 u32 shflags = mfn_to_page(gmfn)->shadow_flags & SHF_page_type_mask;
487 /* More than one type bit set in shadow-flags? */
488 if ( shflags & ~(1UL << find_first_set_bit(shflags)) )
489 res = sh_validate_guest_entry(v, gmfn, ep, sizeof (*ep));
490 }
492 /* We should never need to flush the TLB or recopy PAE entries */
493 ASSERT((res == 0) || (res == SHADOW_SET_CHANGED));
495 return flags;
496 }
498 #if (CONFIG_PAGING_LEVELS == GUEST_PAGING_LEVELS) && (CONFIG_PAGING_LEVELS == SHADOW_PAGING_LEVELS)
499 void *
500 sh_guest_map_l1e(struct vcpu *v, unsigned long addr,
501 unsigned long *gl1mfn)
502 {
503 void *pl1e = NULL;
504 walk_t gw;
506 ASSERT(shadow_mode_translate(v->domain));
508 // XXX -- this is expensive, but it's easy to cobble together...
509 // FIXME!
511 shadow_lock(v->domain);
512 guest_walk_tables(v, addr, &gw, 1);
514 if ( gw.l2e &&
515 (guest_l2e_get_flags(*gw.l2e) & _PAGE_PRESENT) &&
516 !(guest_supports_superpages(v) && (guest_l2e_get_flags(*gw.l2e) & _PAGE_PSE)) )
517 {
518 if ( gl1mfn )
519 *gl1mfn = mfn_x(gw.l1mfn);
520 pl1e = map_domain_page(mfn_x(gw.l1mfn)) +
521 (guest_l1_table_offset(addr) * sizeof(guest_l1e_t));
522 }
524 unmap_walk(v, &gw);
525 shadow_unlock(v->domain);
527 return pl1e;
528 }
530 void
531 sh_guest_get_eff_l1e(struct vcpu *v, unsigned long addr, void *eff_l1e)
532 {
533 walk_t gw;
535 ASSERT(shadow_mode_translate(v->domain));
537 // XXX -- this is expensive, but it's easy to cobble together...
538 // FIXME!
540 shadow_lock(v->domain);
541 guest_walk_tables(v, addr, &gw, 1);
542 *(guest_l1e_t *)eff_l1e = gw.eff_l1e;
543 unmap_walk(v, &gw);
544 shadow_unlock(v->domain);
545 }
546 #endif /* CONFIG==SHADOW==GUEST */
548 /**************************************************************************/
549 /* Functions to compute the correct index into a shadow page, given an
550 * index into the guest page (as returned by guest_get_index()).
551 * This is trivial when the shadow and guest use the same sized PTEs, but
552 * gets more interesting when those sizes are mismatched (e.g. 32-bit guest,
553 * PAE- or 64-bit shadows).
554 *
555 * These functions also increment the shadow mfn, when necessary. When PTE
556 * sizes are mismatched, it takes 2 shadow L1 pages for a single guest L1
557 * page. In this case, we allocate 2 contiguous pages for the shadow L1, and
558 * use simple pointer arithmetic on a pointer to the guest L1e to figure out
559 * which shadow page we really want. Similarly, when PTE sizes are
560 * mismatched, we shadow a guest L2 page with 4 shadow L2 pages. (The easiest
561 * way to see this is: a 32-bit guest L2 page maps 4GB of virtual address
562 * space, while a PAE- or 64-bit shadow L2 page maps 1GB of virtual address
563 * space.)
564 *
565 * For PAE guests, for every 32-bytes of guest L3 page table, we use 64-bytes
566 * of shadow (to store both the shadow, and the info that would normally be
567 * stored in page_info fields). This arrangement allows the shadow and the
568 * "page_info" fields to always be stored in the same page (in fact, in
569 * the same cache line), avoiding an extra call to map_domain_page().
570 */
572 static inline u32
573 guest_index(void *ptr)
574 {
575 return (u32)((unsigned long)ptr & ~PAGE_MASK) / sizeof(guest_l1e_t);
576 }
578 static u32
579 shadow_l1_index(mfn_t *smfn, u32 guest_index)
580 {
581 #if (GUEST_PAGING_LEVELS == 2) && (SHADOW_PAGING_LEVELS != 2)
582 *smfn = _mfn(mfn_x(*smfn) +
583 (guest_index / SHADOW_L1_PAGETABLE_ENTRIES));
584 return (guest_index % SHADOW_L1_PAGETABLE_ENTRIES);
585 #else
586 return guest_index;
587 #endif
588 }
590 static u32
591 shadow_l2_index(mfn_t *smfn, u32 guest_index)
592 {
593 #if (GUEST_PAGING_LEVELS == 2) && (SHADOW_PAGING_LEVELS != 2)
594 // Because we use 2 shadow l2 entries for each guest entry, the number of
595 // guest entries per shadow page is SHADOW_L2_PAGETABLE_ENTRIES/2
596 //
597 *smfn = _mfn(mfn_x(*smfn) +
598 (guest_index / (SHADOW_L2_PAGETABLE_ENTRIES / 2)));
600 // We multiple by two to get the index of the first of the two entries
601 // used to shadow the specified guest entry.
602 return (guest_index % (SHADOW_L2_PAGETABLE_ENTRIES / 2)) * 2;
603 #else
604 return guest_index;
605 #endif
606 }
608 #if GUEST_PAGING_LEVELS >= 4
610 static u32
611 shadow_l3_index(mfn_t *smfn, u32 guest_index)
612 {
613 return guest_index;
614 }
616 static u32
617 shadow_l4_index(mfn_t *smfn, u32 guest_index)
618 {
619 return guest_index;
620 }
622 #endif // GUEST_PAGING_LEVELS >= 4
624 extern u32 get_pat_flags(struct vcpu *v,
625 u32 gl1e_flags,
626 paddr_t gpaddr,
627 paddr_t spaddr);
629 unsigned char pat_type_2_pte_flags(unsigned char pat_type);
630 /**************************************************************************/
631 /* Function which computes shadow entries from their corresponding guest
632 * entries. This is the "heart" of the shadow code. It operates using
633 * level-1 shadow types, but handles all levels of entry.
634 * Don't call it directly, but use the four wrappers below.
635 */
637 static always_inline void
638 _sh_propagate(struct vcpu *v,
639 void *guest_entry_ptr,
640 mfn_t guest_table_mfn,
641 mfn_t target_mfn,
642 void *shadow_entry_ptr,
643 int level,
644 fetch_type_t ft,
645 p2m_type_t p2mt)
646 {
647 guest_l1e_t *gp = guest_entry_ptr;
648 shadow_l1e_t *sp = shadow_entry_ptr;
649 struct domain *d = v->domain;
650 u32 pass_thru_flags;
651 u32 gflags, sflags;
653 /* We don't shadow PAE l3s */
654 ASSERT(GUEST_PAGING_LEVELS > 3 || level != 3);
656 /* Check there's something for the shadows to map to */
657 if ( !p2m_is_valid(p2mt) )
658 {
659 *sp = shadow_l1e_empty();
660 goto done;
661 }
663 if ( mfn_valid(guest_table_mfn) )
664 /* Handle A and D bit propagation into the guest */
665 gflags = guest_set_ad_bits(v, guest_table_mfn, gp, level, ft);
666 else
667 {
668 /* Must be an fl1e or a prefetch */
669 ASSERT(level==1 || !(ft & FETCH_TYPE_DEMAND));
670 gflags = guest_l1e_get_flags(*gp);
671 }
673 if ( unlikely(!(gflags & _PAGE_PRESENT)) )
674 {
675 /* If a guest l1 entry is not present, shadow with the magic
676 * guest-not-present entry. */
677 if ( level == 1 )
678 *sp = sh_l1e_gnp();
679 else
680 *sp = shadow_l1e_empty();
681 goto done;
682 }
684 if ( level == 1 && p2mt == p2m_mmio_dm )
685 {
686 /* Guest l1e maps emulated MMIO space */
687 *sp = sh_l1e_mmio(guest_l1e_get_gfn(*gp), gflags);
688 if ( !d->arch.paging.shadow.has_fast_mmio_entries )
689 d->arch.paging.shadow.has_fast_mmio_entries = 1;
690 goto done;
691 }
693 // Must have a valid target_mfn unless this is a prefetch. In the
694 // case of a prefetch, an invalid mfn means that we can not usefully
695 // shadow anything, and so we return early.
696 //
697 /* N.B. For pass-through MMIO, either this test needs to be relaxed,
698 * and shadow_set_l1e() trained to handle non-valid MFNs (ugh), or the
699 * MMIO areas need to be added to the frame-table to make them "valid". */
700 if ( !mfn_valid(target_mfn) && (p2mt != p2m_mmio_direct) )
701 {
702 ASSERT((ft == ft_prefetch));
703 *sp = shadow_l1e_empty();
704 goto done;
705 }
707 // Propagate bits from the guest to the shadow.
708 // Some of these may be overwritten, below.
709 // Since we know the guest's PRESENT bit is set, we also set the shadow's
710 // SHADOW_PRESENT bit.
711 //
712 pass_thru_flags = (_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_USER |
713 _PAGE_RW | _PAGE_PRESENT);
714 if ( guest_supports_nx(v) )
715 pass_thru_flags |= _PAGE_NX_BIT;
716 sflags = gflags & pass_thru_flags;
718 /* Only change memory caching type for pass-through domain */
719 if ( (level == 1) && is_hvm_domain(d) &&
720 !list_empty(&(domain_hvm_iommu(d)->pdev_list)) )
721 {
722 unsigned int type;
723 if ( hvm_get_mem_pinned_cacheattr(d, gfn_x(guest_l1e_get_gfn(*gp)),
724 &type) )
725 sflags |= pat_type_2_pte_flags(type);
726 else if ( v->domain->arch.hvm_domain.is_in_uc_mode )
727 sflags |= pat_type_2_pte_flags(PAT_TYPE_UNCACHABLE);
728 else
729 sflags |= get_pat_flags(v,
730 gflags,
731 guest_l1e_get_paddr(*gp),
732 mfn_x(target_mfn) << PAGE_SHIFT);
733 }
735 // Set the A&D bits for higher level shadows.
736 // Higher level entries do not, strictly speaking, have dirty bits, but
737 // since we use shadow linear tables, each of these entries may, at some
738 // point in time, also serve as a shadow L1 entry.
739 // By setting both the A&D bits in each of these, we eliminate the burden
740 // on the hardware to update these bits on initial accesses.
741 //
742 if ( (level > 1) && !((SHADOW_PAGING_LEVELS == 3) && (level == 3)) )
743 sflags |= _PAGE_ACCESSED | _PAGE_DIRTY;
745 // If the A or D bit has not yet been set in the guest, then we must
746 // prevent the corresponding kind of access.
747 //
748 if ( unlikely(!(gflags & _PAGE_ACCESSED)) )
749 sflags &= ~_PAGE_PRESENT;
751 /* D bits exist in L1es and PSE L2es */
752 if ( unlikely(((level == 1) ||
753 ((level == 2) &&
754 (gflags & _PAGE_PSE) &&
755 guest_supports_superpages(v)))
756 && !(gflags & _PAGE_DIRTY)) )
757 sflags &= ~_PAGE_RW;
759 // shadow_mode_log_dirty support
760 //
761 // Only allow the guest write access to a page a) on a demand fault,
762 // or b) if the page is already marked as dirty.
763 //
764 // (We handle log-dirty entirely inside the shadow code, without using the
765 // p2m_ram_logdirty p2m type: only HAP uses that.)
766 if ( unlikely((level == 1) && shadow_mode_log_dirty(d)) )
767 {
768 if ( ft & FETCH_TYPE_WRITE )
769 paging_mark_dirty(d, mfn_x(target_mfn));
770 else if ( !sh_mfn_is_dirty(d, target_mfn) )
771 sflags &= ~_PAGE_RW;
772 }
774 /* Read-only memory */
775 if ( p2mt == p2m_ram_ro )
776 sflags &= ~_PAGE_RW;
778 // protect guest page tables
779 //
780 if ( unlikely((level == 1) && sh_mfn_is_a_page_table(target_mfn)) )
781 {
782 if ( shadow_mode_trap_reads(d) )
783 {
784 // if we are trapping both reads & writes, then mark this page
785 // as not present...
786 //
787 sflags &= ~_PAGE_PRESENT;
788 }
789 else
790 {
791 // otherwise, just prevent any writes...
792 //
793 sflags &= ~_PAGE_RW;
794 }
795 }
797 // PV guests in 64-bit mode use two different page tables for user vs
798 // supervisor permissions, making the guest's _PAGE_USER bit irrelevant.
799 // It is always shadowed as present...
800 if ( (GUEST_PAGING_LEVELS == 4) && !is_pv_32on64_domain(d)
801 && !is_hvm_domain(d) )
802 {
803 sflags |= _PAGE_USER;
804 }
806 *sp = shadow_l1e_from_mfn(target_mfn, sflags);
808 done:
809 SHADOW_DEBUG(PROPAGATE,
810 "%s level %u guest %" SH_PRI_gpte " shadow %" SH_PRI_pte "\n",
811 fetch_type_names[ft], level, gp->l1, sp->l1);
812 }
815 /* These four wrappers give us a little bit of type-safety back around the
816 * use of void-* pointers in _sh_propagate(), and allow the compiler to
817 * optimize out some level checks. */
819 #if GUEST_PAGING_LEVELS >= 4
820 static void
821 l4e_propagate_from_guest(struct vcpu *v,
822 guest_l4e_t *gl4e,
823 mfn_t gl4mfn,
824 mfn_t sl3mfn,
825 shadow_l4e_t *sl4e,
826 fetch_type_t ft)
827 {
828 _sh_propagate(v, gl4e, gl4mfn, sl3mfn, sl4e, 4, ft, p2m_ram_rw);
829 }
831 static void
832 l3e_propagate_from_guest(struct vcpu *v,
833 guest_l3e_t *gl3e,
834 mfn_t gl3mfn,
835 mfn_t sl2mfn,
836 shadow_l3e_t *sl3e,
837 fetch_type_t ft)
838 {
839 _sh_propagate(v, gl3e, gl3mfn, sl2mfn, sl3e, 3, ft, p2m_ram_rw);
840 }
841 #endif // GUEST_PAGING_LEVELS >= 4
843 static void
844 l2e_propagate_from_guest(struct vcpu *v,
845 guest_l2e_t *gl2e,
846 mfn_t gl2mfn,
847 mfn_t sl1mfn,
848 shadow_l2e_t *sl2e,
849 fetch_type_t ft)
850 {
851 _sh_propagate(v, gl2e, gl2mfn, sl1mfn, sl2e, 2, ft, p2m_ram_rw);
852 }
854 static void
855 l1e_propagate_from_guest(struct vcpu *v,
856 guest_l1e_t *gl1e,
857 mfn_t gl1mfn,
858 mfn_t gmfn,
859 shadow_l1e_t *sl1e,
860 fetch_type_t ft,
861 p2m_type_t p2mt)
862 {
863 _sh_propagate(v, gl1e, gl1mfn, gmfn, sl1e, 1, ft, p2mt);
864 }
867 /**************************************************************************/
868 /* These functions update shadow entries (and do bookkeeping on the shadow
869 * tables they are in). It is intended that they are the only
870 * functions which ever write (non-zero) data onto a shadow page.
871 */
873 static inline void safe_write_entry(void *dst, void *src)
874 /* Copy one PTE safely when processors might be running on the
875 * destination pagetable. This does *not* give safety against
876 * concurrent writes (that's what the shadow lock is for), just
877 * stops the hardware picking up partially written entries. */
878 {
879 volatile unsigned long *d = dst;
880 unsigned long *s = src;
881 ASSERT(!((unsigned long) d & (sizeof (shadow_l1e_t) - 1)));
882 #if CONFIG_PAGING_LEVELS == 3
883 /* In PAE mode, pagetable entries are larger
884 * than machine words, so won't get written atomically. We need to make
885 * sure any other cpu running on these shadows doesn't see a
886 * half-written entry. Do this by marking the entry not-present first,
887 * then writing the high word before the low word. */
888 BUILD_BUG_ON(sizeof (shadow_l1e_t) != 2 * sizeof (unsigned long));
889 d[0] = 0;
890 d[1] = s[1];
891 d[0] = s[0];
892 #else
893 /* In 32-bit and 64-bit, sizeof(pte) == sizeof(ulong) == 1 word,
894 * which will be an atomic write, since the entry is aligned. */
895 BUILD_BUG_ON(sizeof (shadow_l1e_t) != sizeof (unsigned long));
896 *d = *s;
897 #endif
898 }
901 static inline void
902 shadow_write_entries(void *d, void *s, int entries, mfn_t mfn)
903 /* This function does the actual writes to shadow pages.
904 * It must not be called directly, since it doesn't do the bookkeeping
905 * that shadow_set_l*e() functions do. */
906 {
907 shadow_l1e_t *dst = d;
908 shadow_l1e_t *src = s;
909 void *map = NULL;
910 int i;
912 /* Because we mirror access rights at all levels in the shadow, an
913 * l2 (or higher) entry with the RW bit cleared will leave us with
914 * no write access through the linear map.
915 * We detect that by writing to the shadow with copy_to_user() and
916 * using map_domain_page() to get a writeable mapping if we need to. */
917 if ( __copy_to_user(d, d, sizeof (unsigned long)) != 0 )
918 {
919 perfc_incr(shadow_linear_map_failed);
920 map = sh_map_domain_page(mfn);
921 ASSERT(map != NULL);
922 dst = map + ((unsigned long)dst & (PAGE_SIZE - 1));
923 }
926 for ( i = 0; i < entries; i++ )
927 safe_write_entry(dst++, src++);
929 if ( map != NULL ) sh_unmap_domain_page(map);
930 }
932 static inline int
933 perms_strictly_increased(u32 old_flags, u32 new_flags)
934 /* Given the flags of two entries, are the new flags a strict
935 * increase in rights over the old ones? */
936 {
937 u32 of = old_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX);
938 u32 nf = new_flags & (_PAGE_PRESENT|_PAGE_RW|_PAGE_USER|_PAGE_NX);
939 /* Flip the NX bit, since it's the only one that decreases rights;
940 * we calculate as if it were an "X" bit. */
941 of ^= _PAGE_NX_BIT;
942 nf ^= _PAGE_NX_BIT;
943 /* If the changed bits are all set in the new flags, then rights strictly
944 * increased between old and new. */
945 return ((of | (of ^ nf)) == nf);
946 }
948 static int inline
949 shadow_get_page_from_l1e(shadow_l1e_t sl1e, struct domain *d)
950 {
951 int res;
952 mfn_t mfn;
953 struct domain *owner;
955 ASSERT(!sh_l1e_is_magic(sl1e));
957 if ( !shadow_mode_refcounts(d) )
958 return 1;
960 res = get_page_from_l1e(sl1e, d);
962 // If a privileged domain is attempting to install a map of a page it does
963 // not own, we let it succeed anyway.
964 //
965 if ( unlikely(!res) &&
966 IS_PRIV(d) &&
967 !shadow_mode_translate(d) &&
968 mfn_valid(mfn = shadow_l1e_get_mfn(sl1e)) &&
969 (owner = page_get_owner(mfn_to_page(mfn))) &&
970 (d != owner) )
971 {
972 res = get_page_from_l1e(sl1e, owner);
973 SHADOW_PRINTK("privileged domain %d installs map of mfn %05lx "
974 "which is owned by domain %d: %s\n",
975 d->domain_id, mfn_x(mfn), owner->domain_id,
976 res ? "success" : "failed");
977 }
979 if ( unlikely(!res) )
980 {
981 perfc_incr(shadow_get_page_fail);
982 SHADOW_PRINTK("failed: l1e=" SH_PRI_pte "\n");
983 }
985 return res;
986 }
988 static void inline
989 shadow_put_page_from_l1e(shadow_l1e_t sl1e, struct domain *d)
990 {
991 if ( !shadow_mode_refcounts(d) )
992 return;
994 put_page_from_l1e(sl1e, d);
995 }
997 #if GUEST_PAGING_LEVELS >= 4
998 static int shadow_set_l4e(struct vcpu *v,
999 shadow_l4e_t *sl4e,
1000 shadow_l4e_t new_sl4e,
1001 mfn_t sl4mfn)
1003 int flags = 0, ok;
1004 shadow_l4e_t old_sl4e;
1005 paddr_t paddr;
1006 ASSERT(sl4e != NULL);
1007 old_sl4e = *sl4e;
1009 if ( old_sl4e.l4 == new_sl4e.l4 ) return 0; /* Nothing to do */
1011 paddr = ((((paddr_t)mfn_x(sl4mfn)) << PAGE_SHIFT)
1012 | (((unsigned long)sl4e) & ~PAGE_MASK));
1014 if ( shadow_l4e_get_flags(new_sl4e) & _PAGE_PRESENT )
1016 /* About to install a new reference */
1017 mfn_t sl3mfn = shadow_l4e_get_mfn(new_sl4e);
1018 ok = sh_get_ref(v, sl3mfn, paddr);
1019 /* Are we pinning l3 shadows to handle wierd linux behaviour? */
1020 if ( sh_type_is_pinnable(v, SH_type_l3_64_shadow) )
1021 ok |= sh_pin(v, sl3mfn);
1022 if ( !ok )
1024 domain_crash(v->domain);
1025 return SHADOW_SET_ERROR;
1029 /* Write the new entry */
1030 shadow_write_entries(sl4e, &new_sl4e, 1, sl4mfn);
1031 flags |= SHADOW_SET_CHANGED;
1033 if ( shadow_l4e_get_flags(old_sl4e) & _PAGE_PRESENT )
1035 /* We lost a reference to an old mfn. */
1036 mfn_t osl3mfn = shadow_l4e_get_mfn(old_sl4e);
1037 if ( (mfn_x(osl3mfn) != mfn_x(shadow_l4e_get_mfn(new_sl4e)))
1038 || !perms_strictly_increased(shadow_l4e_get_flags(old_sl4e),
1039 shadow_l4e_get_flags(new_sl4e)) )
1041 flags |= SHADOW_SET_FLUSH;
1043 sh_put_ref(v, osl3mfn, paddr);
1045 return flags;
1048 static int shadow_set_l3e(struct vcpu *v,
1049 shadow_l3e_t *sl3e,
1050 shadow_l3e_t new_sl3e,
1051 mfn_t sl3mfn)
1053 int flags = 0;
1054 shadow_l3e_t old_sl3e;
1055 paddr_t paddr;
1056 ASSERT(sl3e != NULL);
1057 old_sl3e = *sl3e;
1059 if ( old_sl3e.l3 == new_sl3e.l3 ) return 0; /* Nothing to do */
1061 paddr = ((((paddr_t)mfn_x(sl3mfn)) << PAGE_SHIFT)
1062 | (((unsigned long)sl3e) & ~PAGE_MASK));
1064 if ( shadow_l3e_get_flags(new_sl3e) & _PAGE_PRESENT )
1065 /* About to install a new reference */
1066 if ( !sh_get_ref(v, shadow_l3e_get_mfn(new_sl3e), paddr) )
1068 domain_crash(v->domain);
1069 return SHADOW_SET_ERROR;
1072 /* Write the new entry */
1073 shadow_write_entries(sl3e, &new_sl3e, 1, sl3mfn);
1074 flags |= SHADOW_SET_CHANGED;
1076 if ( shadow_l3e_get_flags(old_sl3e) & _PAGE_PRESENT )
1078 /* We lost a reference to an old mfn. */
1079 mfn_t osl2mfn = shadow_l3e_get_mfn(old_sl3e);
1080 if ( (mfn_x(osl2mfn) != mfn_x(shadow_l3e_get_mfn(new_sl3e))) ||
1081 !perms_strictly_increased(shadow_l3e_get_flags(old_sl3e),
1082 shadow_l3e_get_flags(new_sl3e)) )
1084 flags |= SHADOW_SET_FLUSH;
1086 sh_put_ref(v, osl2mfn, paddr);
1088 return flags;
1090 #endif /* GUEST_PAGING_LEVELS >= 4 */
1092 static int shadow_set_l2e(struct vcpu *v,
1093 shadow_l2e_t *sl2e,
1094 shadow_l2e_t new_sl2e,
1095 mfn_t sl2mfn)
1097 int flags = 0;
1098 shadow_l2e_t old_sl2e;
1099 paddr_t paddr;
1101 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1102 /* In 2-on-3 we work with pairs of l2es pointing at two-page
1103 * shadows. Reference counting and up-pointers track from the first
1104 * page of the shadow to the first l2e, so make sure that we're
1105 * working with those:
1106 * Align the pointer down so it's pointing at the first of the pair */
1107 sl2e = (shadow_l2e_t *)((unsigned long)sl2e & ~(sizeof(shadow_l2e_t)));
1108 /* Align the mfn of the shadow entry too */
1109 new_sl2e.l2 &= ~(1<<PAGE_SHIFT);
1110 #endif
1112 ASSERT(sl2e != NULL);
1113 old_sl2e = *sl2e;
1115 if ( old_sl2e.l2 == new_sl2e.l2 ) return 0; /* Nothing to do */
1117 paddr = ((((paddr_t)mfn_x(sl2mfn)) << PAGE_SHIFT)
1118 | (((unsigned long)sl2e) & ~PAGE_MASK));
1120 if ( shadow_l2e_get_flags(new_sl2e) & _PAGE_PRESENT )
1121 /* About to install a new reference */
1122 if ( !sh_get_ref(v, shadow_l2e_get_mfn(new_sl2e), paddr) )
1124 domain_crash(v->domain);
1125 return SHADOW_SET_ERROR;
1128 /* Write the new entry */
1129 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1131 shadow_l2e_t pair[2] = { new_sl2e, new_sl2e };
1132 /* The l1 shadow is two pages long and need to be pointed to by
1133 * two adjacent l1es. The pair have the same flags, but point
1134 * at odd and even MFNs */
1135 ASSERT(!(pair[0].l2 & (1<<PAGE_SHIFT)));
1136 pair[1].l2 |= (1<<PAGE_SHIFT);
1137 shadow_write_entries(sl2e, &pair, 2, sl2mfn);
1139 #else /* normal case */
1140 shadow_write_entries(sl2e, &new_sl2e, 1, sl2mfn);
1141 #endif
1142 flags |= SHADOW_SET_CHANGED;
1144 if ( shadow_l2e_get_flags(old_sl2e) & _PAGE_PRESENT )
1146 /* We lost a reference to an old mfn. */
1147 mfn_t osl1mfn = shadow_l2e_get_mfn(old_sl2e);
1148 if ( (mfn_x(osl1mfn) != mfn_x(shadow_l2e_get_mfn(new_sl2e))) ||
1149 !perms_strictly_increased(shadow_l2e_get_flags(old_sl2e),
1150 shadow_l2e_get_flags(new_sl2e)) )
1152 flags |= SHADOW_SET_FLUSH;
1154 sh_put_ref(v, osl1mfn, paddr);
1156 return flags;
1159 static int shadow_set_l1e(struct vcpu *v,
1160 shadow_l1e_t *sl1e,
1161 shadow_l1e_t new_sl1e,
1162 mfn_t sl1mfn)
1164 int flags = 0;
1165 struct domain *d = v->domain;
1166 shadow_l1e_t old_sl1e;
1167 ASSERT(sl1e != NULL);
1169 old_sl1e = *sl1e;
1171 if ( old_sl1e.l1 == new_sl1e.l1 ) return 0; /* Nothing to do */
1173 if ( (shadow_l1e_get_flags(new_sl1e) & _PAGE_PRESENT)
1174 && !sh_l1e_is_magic(new_sl1e) )
1176 /* About to install a new reference */
1177 if ( shadow_mode_refcounts(d) ) {
1178 if ( shadow_get_page_from_l1e(new_sl1e, d) == 0 )
1180 /* Doesn't look like a pagetable. */
1181 flags |= SHADOW_SET_ERROR;
1182 new_sl1e = shadow_l1e_empty();
1187 /* Write the new entry */
1188 shadow_write_entries(sl1e, &new_sl1e, 1, sl1mfn);
1189 flags |= SHADOW_SET_CHANGED;
1191 if ( (shadow_l1e_get_flags(old_sl1e) & _PAGE_PRESENT)
1192 && !sh_l1e_is_magic(old_sl1e) )
1194 /* We lost a reference to an old mfn. */
1195 /* N.B. Unlike higher-level sets, never need an extra flush
1196 * when writing an l1e. Because it points to the same guest frame
1197 * as the guest l1e did, it's the guest's responsibility to
1198 * trigger a flush later. */
1199 if ( shadow_mode_refcounts(d) )
1201 shadow_put_page_from_l1e(old_sl1e, d);
1204 return flags;
1208 /**************************************************************************/
1209 /* Macros to walk pagetables. These take the shadow of a pagetable and
1210 * walk every "interesting" entry. That is, they don't touch Xen mappings,
1211 * and for 32-bit l2s shadowed onto PAE or 64-bit, they only touch every
1212 * second entry (since pairs of entries are managed together). For multi-page
1213 * shadows they walk all pages.
1215 * Arguments are an MFN, the variable to point to each entry, a variable
1216 * to indicate that we are done (we will shortcut to the end of the scan
1217 * when _done != 0), a variable to indicate that we should avoid Xen mappings,
1218 * and the code.
1220 * WARNING: These macros have side-effects. They change the values of both
1221 * the pointer and the MFN. */
1223 static inline void increment_ptr_to_guest_entry(void *ptr)
1225 if ( ptr )
1227 guest_l1e_t **entry = ptr;
1228 (*entry)++;
1232 /* All kinds of l1: touch all entries */
1233 #define _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1234 do { \
1235 int _i; \
1236 shadow_l1e_t *_sp = map_shadow_page((_sl1mfn)); \
1237 ASSERT(mfn_to_shadow_page(_sl1mfn)->type == SH_type_l1_shadow \
1238 || mfn_to_shadow_page(_sl1mfn)->type == SH_type_fl1_shadow); \
1239 for ( _i = 0; _i < SHADOW_L1_PAGETABLE_ENTRIES; _i++ ) \
1240 { \
1241 (_sl1e) = _sp + _i; \
1242 if ( shadow_l1e_get_flags(*(_sl1e)) & _PAGE_PRESENT ) \
1243 {_code} \
1244 if ( _done ) break; \
1245 increment_ptr_to_guest_entry(_gl1p); \
1246 } \
1247 unmap_shadow_page(_sp); \
1248 } while (0)
1250 /* 32-bit l1, on PAE or 64-bit shadows: need to walk both pages of shadow */
1251 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1252 #define SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1253 do { \
1254 int __done = 0; \
1255 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, \
1256 ({ (__done = _done); }), _code); \
1257 _sl1mfn = _mfn(mfn_x(_sl1mfn) + 1); \
1258 if ( !__done ) \
1259 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, \
1260 ({ (__done = _done); }), _code); \
1261 } while (0)
1262 #else /* Everything else; l1 shadows are only one page */
1263 #define SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code) \
1264 _SHADOW_FOREACH_L1E(_sl1mfn, _sl1e, _gl1p, _done, _code)
1265 #endif
1268 #if GUEST_PAGING_LEVELS == 2 && SHADOW_PAGING_LEVELS > 2
1270 /* 32-bit l2 on PAE/64: four pages, touch every second entry, and avoid Xen */
1271 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \
1272 do { \
1273 int _i, _j, __done = 0; \
1274 int _xen = !shadow_mode_external(_dom); \
1275 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_32_shadow); \
1276 for ( _j = 0; _j < 4 && !__done; _j++ ) \
1277 { \
1278 shadow_l2e_t *_sp = map_shadow_page(_sl2mfn); \
1279 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i += 2 ) \
1280 if ( (!(_xen)) \
1281 || ((_j * SHADOW_L2_PAGETABLE_ENTRIES) + _i) \
1282 < (HYPERVISOR_VIRT_START >> SHADOW_L2_PAGETABLE_SHIFT) ) \
1283 { \
1284 (_sl2e) = _sp + _i; \
1285 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1286 {_code} \
1287 if ( (__done = (_done)) ) break; \
1288 increment_ptr_to_guest_entry(_gl2p); \
1289 } \
1290 unmap_shadow_page(_sp); \
1291 _sl2mfn = _mfn(mfn_x(_sl2mfn) + 1); \
1292 } \
1293 } while (0)
1295 #elif GUEST_PAGING_LEVELS == 2
1297 /* 32-bit on 32-bit: avoid Xen entries */
1298 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \
1299 do { \
1300 int _i; \
1301 int _xen = !shadow_mode_external(_dom); \
1302 shadow_l2e_t *_sp = map_shadow_page((_sl2mfn)); \
1303 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_32_shadow); \
1304 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \
1305 if ( (!(_xen)) \
1306 || \
1307 (_i < (HYPERVISOR_VIRT_START >> SHADOW_L2_PAGETABLE_SHIFT)) ) \
1308 { \
1309 (_sl2e) = _sp + _i; \
1310 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1311 {_code} \
1312 if ( _done ) break; \
1313 increment_ptr_to_guest_entry(_gl2p); \
1314 } \
1315 unmap_shadow_page(_sp); \
1316 } while (0)
1318 #elif GUEST_PAGING_LEVELS == 3
1320 /* PAE: if it's an l2h, don't touch Xen mappings */
1321 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \
1322 do { \
1323 int _i; \
1324 int _xen = !shadow_mode_external(_dom); \
1325 shadow_l2e_t *_sp = map_shadow_page((_sl2mfn)); \
1326 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_pae_shadow \
1327 || mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2h_pae_shadow);\
1328 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \
1329 if ( (!(_xen)) \
1330 || mfn_to_shadow_page(_sl2mfn)->type != SH_type_l2h_pae_shadow\
1331 || ((_i + (3 * SHADOW_L2_PAGETABLE_ENTRIES)) \
1332 < (HYPERVISOR_VIRT_START >> SHADOW_L2_PAGETABLE_SHIFT)) ) \
1333 { \
1334 (_sl2e) = _sp + _i; \
1335 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1336 {_code} \
1337 if ( _done ) break; \
1338 increment_ptr_to_guest_entry(_gl2p); \
1339 } \
1340 unmap_shadow_page(_sp); \
1341 } while (0)
1343 #else
1345 /* 64-bit l2: touch all entries except for PAE compat guests. */
1346 #define SHADOW_FOREACH_L2E(_sl2mfn, _sl2e, _gl2p, _done, _dom, _code) \
1347 do { \
1348 int _i; \
1349 int _xen = !shadow_mode_external(_dom); \
1350 shadow_l2e_t *_sp = map_shadow_page((_sl2mfn)); \
1351 ASSERT(mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2_64_shadow || \
1352 mfn_to_shadow_page(_sl2mfn)->type == SH_type_l2h_64_shadow); \
1353 for ( _i = 0; _i < SHADOW_L2_PAGETABLE_ENTRIES; _i++ ) \
1354 { \
1355 if ( (!(_xen)) \
1356 || !is_pv_32on64_domain(_dom) \
1357 || mfn_to_shadow_page(_sl2mfn)->type != SH_type_l2h_64_shadow \
1358 || (_i < COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(_dom)) ) \
1359 { \
1360 (_sl2e) = _sp + _i; \
1361 if ( shadow_l2e_get_flags(*(_sl2e)) & _PAGE_PRESENT ) \
1362 {_code} \
1363 if ( _done ) break; \
1364 increment_ptr_to_guest_entry(_gl2p); \
1365 } \
1366 } \
1367 unmap_shadow_page(_sp); \
1368 } while (0)
1370 #endif /* different kinds of l2 */
1372 #if GUEST_PAGING_LEVELS == 4
1374 /* 64-bit l3: touch all entries */
1375 #define SHADOW_FOREACH_L3E(_sl3mfn, _sl3e, _gl3p, _done, _code) \
1376 do { \
1377 int _i; \
1378 shadow_l3e_t *_sp = map_shadow_page((_sl3mfn)); \
1379 ASSERT(mfn_to_shadow_page(_sl3mfn)->type == SH_type_l3_64_shadow); \
1380 for ( _i = 0; _i < SHADOW_L3_PAGETABLE_ENTRIES; _i++ ) \
1381 { \
1382 (_sl3e) = _sp + _i; \
1383 if ( shadow_l3e_get_flags(*(_sl3e)) & _PAGE_PRESENT ) \
1384 {_code} \
1385 if ( _done ) break; \
1386 increment_ptr_to_guest_entry(_gl3p); \
1387 } \
1388 unmap_shadow_page(_sp); \
1389 } while (0)
1391 /* 64-bit l4: avoid Xen mappings */
1392 #define SHADOW_FOREACH_L4E(_sl4mfn, _sl4e, _gl4p, _done, _dom, _code) \
1393 do { \
1394 shadow_l4e_t *_sp = map_shadow_page((_sl4mfn)); \
1395 int _xen = !shadow_mode_external(_dom); \
1396 int _i; \
1397 ASSERT(mfn_to_shadow_page(_sl4mfn)->type == SH_type_l4_64_shadow); \
1398 for ( _i = 0; _i < SHADOW_L4_PAGETABLE_ENTRIES; _i++ ) \
1399 { \
1400 if ( (!(_xen)) || is_guest_l4_slot(_dom, _i) ) \
1401 { \
1402 (_sl4e) = _sp + _i; \
1403 if ( shadow_l4e_get_flags(*(_sl4e)) & _PAGE_PRESENT ) \
1404 {_code} \
1405 if ( _done ) break; \
1406 } \
1407 increment_ptr_to_guest_entry(_gl4p); \
1408 } \
1409 unmap_shadow_page(_sp); \
1410 } while (0)
1412 #endif
1416 /**************************************************************************/
1417 /* Functions to install Xen mappings and linear mappings in shadow pages */
1419 // XXX -- this function should probably be moved to shadow-common.c, but that
1420 // probably wants to wait until the shadow types have been moved from
1421 // shadow-types.h to shadow-private.h
1422 //
1423 #if CONFIG_PAGING_LEVELS == 4 && GUEST_PAGING_LEVELS == 4
1424 void sh_install_xen_entries_in_l4(struct vcpu *v, mfn_t gl4mfn, mfn_t sl4mfn)
1426 struct domain *d = v->domain;
1427 shadow_l4e_t *sl4e;
1429 sl4e = sh_map_domain_page(sl4mfn);
1430 ASSERT(sl4e != NULL);
1431 ASSERT(sizeof (l4_pgentry_t) == sizeof (shadow_l4e_t));
1433 /* Copy the common Xen mappings from the idle domain */
1434 memcpy(&sl4e[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1435 &idle_pg_table[ROOT_PAGETABLE_FIRST_XEN_SLOT],
1436 ROOT_PAGETABLE_XEN_SLOTS * sizeof(l4_pgentry_t));
1438 /* Install the per-domain mappings for this domain */
1439 sl4e[shadow_l4_table_offset(PERDOMAIN_VIRT_START)] =
1440 shadow_l4e_from_mfn(page_to_mfn(virt_to_page(d->arch.mm_perdomain_l3)),
1441 __PAGE_HYPERVISOR);
1443 /* Linear mapping */
1444 sl4e[shadow_l4_table_offset(SH_LINEAR_PT_VIRT_START)] =
1445 shadow_l4e_from_mfn(sl4mfn, __PAGE_HYPERVISOR);
1447 if ( shadow_mode_translate(v->domain) && !shadow_mode_external(v->domain) )
1449 // linear tables may not be used with translated PV guests
1450 sl4e[shadow_l4_table_offset(LINEAR_PT_VIRT_START)] =
1451 shadow_l4e_empty();
1453 else
1455 sl4e[shadow_l4_table_offset(LINEAR_PT_VIRT_START)] =
1456 shadow_l4e_from_mfn(gl4mfn, __PAGE_HYPERVISOR);
1459 if ( shadow_mode_translate(v->domain) )
1461 /* install domain-specific P2M table */
1462 sl4e[shadow_l4_table_offset(RO_MPT_VIRT_START)] =
1463 shadow_l4e_from_mfn(pagetable_get_mfn(d->arch.phys_table),
1464 __PAGE_HYPERVISOR);
1467 if ( is_pv_32on64_domain(v->domain) )
1469 /* install compat arg xlat entry */
1470 sl4e[shadow_l4_table_offset(COMPAT_ARG_XLAT_VIRT_BASE)] =
1471 shadow_l4e_from_mfn(
1472 page_to_mfn(virt_to_page(d->arch.mm_arg_xlat_l3)),
1473 __PAGE_HYPERVISOR);
1476 sh_unmap_domain_page(sl4e);
1478 #endif
1480 #if CONFIG_PAGING_LEVELS >= 3 && GUEST_PAGING_LEVELS >= 3
1481 // For 3-on-3 PV guests, we need to make sure the xen mappings are in
1482 // place, which means that we need to populate the l2h entry in the l3
1483 // table.
1485 static void sh_install_xen_entries_in_l2h(struct vcpu *v, mfn_t sl2hmfn)
1487 struct domain *d = v->domain;
1488 shadow_l2e_t *sl2e;
1489 #if CONFIG_PAGING_LEVELS == 3
1490 int i;
1491 #else
1493 if ( !is_pv_32on64_vcpu(v) )
1494 return;
1495 #endif
1497 sl2e = sh_map_domain_page(sl2hmfn);
1498 ASSERT(sl2e != NULL);
1499 ASSERT(sizeof (l2_pgentry_t) == sizeof (shadow_l2e_t));
1501 #if CONFIG_PAGING_LEVELS == 3
1503 /* Copy the common Xen mappings from the idle domain */
1504 memcpy(&sl2e[L2_PAGETABLE_FIRST_XEN_SLOT & (L2_PAGETABLE_ENTRIES-1)],
1505 &idle_pg_table_l2[L2_PAGETABLE_FIRST_XEN_SLOT],
1506 L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t));
1508 /* Install the per-domain mappings for this domain */
1509 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
1510 sl2e[shadow_l2_table_offset(PERDOMAIN_VIRT_START) + i] =
1511 shadow_l2e_from_mfn(
1512 page_to_mfn(virt_to_page(d->arch.mm_perdomain_pt) + i),
1513 __PAGE_HYPERVISOR);
1515 /* We don't set up a linear mapping here because we can't until this
1516 * l2h is installed in an l3e. sh_update_linear_entries() handles
1517 * the linear mappings when CR3 (and so the fourth l3e) is loaded.
1518 * We zero them here, just as a safety measure.
1519 */
1520 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
1521 sl2e[shadow_l2_table_offset(LINEAR_PT_VIRT_START) + i] =
1522 shadow_l2e_empty();
1523 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
1524 sl2e[shadow_l2_table_offset(SH_LINEAR_PT_VIRT_START) + i] =
1525 shadow_l2e_empty();
1527 if ( shadow_mode_translate(d) )
1529 /* Install the domain-specific p2m table */
1530 l3_pgentry_t *p2m;
1531 ASSERT(pagetable_get_pfn(d->arch.phys_table) != 0);
1532 p2m = sh_map_domain_page(pagetable_get_mfn(d->arch.phys_table));
1533 for ( i = 0; i < MACHPHYS_MBYTES>>1; i++ )
1535 sl2e[shadow_l2_table_offset(RO_MPT_VIRT_START) + i] =
1536 (l3e_get_flags(p2m[i]) & _PAGE_PRESENT)
1537 ? shadow_l2e_from_mfn(_mfn(l3e_get_pfn(p2m[i])),
1538 __PAGE_HYPERVISOR)
1539 : shadow_l2e_empty();
1541 sh_unmap_domain_page(p2m);
1544 #else
1546 /* Copy the common Xen mappings from the idle domain */
1547 memcpy(
1548 &sl2e[COMPAT_L2_PAGETABLE_FIRST_XEN_SLOT(d)],
1549 &compat_idle_pg_table_l2[l2_table_offset(HIRO_COMPAT_MPT_VIRT_START)],
1550 COMPAT_L2_PAGETABLE_XEN_SLOTS(d) * sizeof(*sl2e));
1552 #endif
1554 sh_unmap_domain_page(sl2e);
1556 #endif
1559 #if CONFIG_PAGING_LEVELS == 2 && GUEST_PAGING_LEVELS == 2
1560 void sh_install_xen_entries_in_l2(struct vcpu *v, mfn_t gl2mfn, mfn_t sl2mfn)
1562 struct domain *d = v->domain;
1563 shadow_l2e_t *sl2e;
1564 int i;
1566 sl2e = sh_map_domain_page(sl2mfn);
1567 ASSERT(sl2e != NULL);
1568 ASSERT(sizeof (l2_pgentry_t) == sizeof (shadow_l2e_t));
1570 /* Copy the common Xen mappings from the idle domain */
1571 memcpy(&sl2e[L2_PAGETABLE_FIRST_XEN_SLOT],
1572 &idle_pg_table[L2_PAGETABLE_FIRST_XEN_SLOT],
1573 L2_PAGETABLE_XEN_SLOTS * sizeof(l2_pgentry_t));
1575 /* Install the per-domain mappings for this domain */
1576 for ( i = 0; i < PDPT_L2_ENTRIES; i++ )
1577 sl2e[shadow_l2_table_offset(PERDOMAIN_VIRT_START) + i] =
1578 shadow_l2e_from_mfn(
1579 page_to_mfn(virt_to_page(d->arch.mm_perdomain_pt) + i),
1580 __PAGE_HYPERVISOR);
1582 /* Linear mapping */
1583 sl2e[shadow_l2_table_offset(SH_LINEAR_PT_VIRT_START)] =
1584 shadow_l2e_from_mfn(sl2mfn, __PAGE_HYPERVISOR);
1586 if ( shadow_mode_translate(v->domain) && !shadow_mode_external(v->domain) )
1588 // linear tables may not be used with translated PV guests
1589 sl2e[shadow_l2_table_offset(LINEAR_PT_VIRT_START)] =
1590 shadow_l2e_empty();
1592 else
1594 sl2e[shadow_l2_table_offset(LINEAR_PT_VIRT_START)] =
1595 shadow_l2e_from_mfn(gl2mfn, __PAGE_HYPERVISOR);
1598 if ( shadow_mode_translate(d) )
1600 /* install domain-specific P2M table */
1601 sl2e[shadow_l2_table_offset(RO_MPT_VIRT_START)] =
1602 shadow_l2e_from_mfn(pagetable_get_mfn(d->arch.phys_table),
1603 __PAGE_HYPERVISOR);
1606 sh_unmap_domain_page(sl2e);
1608 #endif
1612 /**************************************************************************/
1613 /* Create a shadow of a given guest page.
1614 */
1615 static mfn_t
1616 sh_make_shadow(struct vcpu *v, mfn_t gmfn, u32 shadow_type)
1618 mfn_t smfn = shadow_alloc(v->domain, shadow_type, mfn_x(gmfn));
1619 SHADOW_DEBUG(MAKE_SHADOW, "(%05lx, %u)=>%05lx\n",
1620 mfn_x(gmfn), shadow_type, mfn_x(smfn));
1622 if ( shadow_type != SH_type_l2_32_shadow
1623 && shadow_type != SH_type_l2_pae_shadow
1624 && shadow_type != SH_type_l2h_pae_shadow
1625 && shadow_type != SH_type_l4_64_shadow )
1626 /* Lower-level shadow, not yet linked form a higher level */
1627 mfn_to_shadow_page(smfn)->up = 0;
1629 #if GUEST_PAGING_LEVELS == 4
1630 #if (SHADOW_OPTIMIZATIONS & SHOPT_LINUX_L3_TOPLEVEL)
1631 if ( shadow_type == SH_type_l4_64_shadow &&
1632 unlikely(v->domain->arch.paging.shadow.opt_flags & SHOPT_LINUX_L3_TOPLEVEL) )
1634 /* We're shadowing a new l4, but we've been assuming the guest uses
1635 * only one l4 per vcpu and context switches using an l4 entry.
1636 * Count the number of active l4 shadows. If there are enough
1637 * of them, decide that this isn't an old linux guest, and stop
1638 * pinning l3es. This is not very quick but it doesn't happen
1639 * very often. */
1640 struct list_head *l, *t;
1641 struct shadow_page_info *sp;
1642 struct vcpu *v2;
1643 int l4count = 0, vcpus = 0;
1644 list_for_each(l, &v->domain->arch.paging.shadow.pinned_shadows)
1646 sp = list_entry(l, struct shadow_page_info, list);
1647 if ( sp->type == SH_type_l4_64_shadow )
1648 l4count++;
1650 for_each_vcpu ( v->domain, v2 )
1651 vcpus++;
1652 if ( l4count > 2 * vcpus )
1654 /* Unpin all the pinned l3 tables, and don't pin any more. */
1655 list_for_each_safe(l, t, &v->domain->arch.paging.shadow.pinned_shadows)
1657 sp = list_entry(l, struct shadow_page_info, list);
1658 if ( sp->type == SH_type_l3_64_shadow )
1659 sh_unpin(v, shadow_page_to_mfn(sp));
1661 v->domain->arch.paging.shadow.opt_flags &= ~SHOPT_LINUX_L3_TOPLEVEL;
1664 #endif
1665 #endif
1667 // Create the Xen mappings...
1668 if ( !shadow_mode_external(v->domain) )
1670 switch (shadow_type)
1672 #if CONFIG_PAGING_LEVELS == 4 && GUEST_PAGING_LEVELS == 4
1673 case SH_type_l4_shadow:
1674 sh_install_xen_entries_in_l4(v, gmfn, smfn); break;
1675 #endif
1676 #if CONFIG_PAGING_LEVELS >= 3 && GUEST_PAGING_LEVELS >= 3
1677 case SH_type_l2h_shadow:
1678 sh_install_xen_entries_in_l2h(v, smfn); break;
1679 #endif
1680 #if CONFIG_PAGING_LEVELS == 2 && GUEST_PAGING_LEVELS == 2
1681 case SH_type_l2_shadow:
1682 sh_install_xen_entries_in_l2(v, gmfn, smfn); break;
1683 #endif
1684 default: /* Do nothing */ break;
1688 shadow_promote(v, gmfn, shadow_type);
1689 set_shadow_status(v, gmfn, shadow_type, smfn);
1691 return smfn;
1694 /* Make a splintered superpage shadow */
1695 static mfn_t
1696 make_fl1_shadow(struct vcpu *v, gfn_t gfn)
1698 mfn_t smfn = shadow_alloc(v->domain, SH_type_fl1_shadow,
1699 (unsigned long) gfn_x(gfn));
1701 SHADOW_DEBUG(MAKE_SHADOW, "(%" SH_PRI_gfn ")=>%" PRI_mfn "\n",
1702 gfn_x(gfn), mfn_x(smfn));
1704 set_fl1_shadow_status(v, gfn, smfn);
1705 return smfn;
1709 #if SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS
1710 mfn_t
1711 sh_make_monitor_table(struct vcpu *v)
1713 struct domain *d = v->domain;
1715 ASSERT(pagetable_get_pfn(v->arch.monitor_table) == 0);
1717 /* Guarantee we can get the memory we need */
1718 shadow_prealloc(d, SH_type_monitor_table, CONFIG_PAGING_LEVELS - 1);
1720 #if CONFIG_PAGING_LEVELS == 4
1722 mfn_t m4mfn;
1723 m4mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1724 sh_install_xen_entries_in_l4(v, m4mfn, m4mfn);
1725 /* Remember the level of this table */
1726 mfn_to_page(m4mfn)->shadow_flags = 4;
1727 #if SHADOW_PAGING_LEVELS < 4
1728 // Install a monitor l3 table in slot 0 of the l4 table.
1729 // This is used for shadow linear maps.
1731 mfn_t m3mfn;
1732 l4_pgentry_t *l4e;
1733 m3mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1734 mfn_to_page(m3mfn)->shadow_flags = 3;
1735 l4e = sh_map_domain_page(m4mfn);
1736 l4e[0] = l4e_from_pfn(mfn_x(m3mfn), __PAGE_HYPERVISOR);
1737 sh_unmap_domain_page(l4e);
1738 if ( is_pv_32on64_vcpu(v) )
1740 // Install a monitor l2 table in slot 3 of the l3 table.
1741 // This is used for all Xen entries.
1742 mfn_t m2mfn;
1743 l3_pgentry_t *l3e;
1744 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1745 mfn_to_page(m2mfn)->shadow_flags = 2;
1746 l3e = sh_map_domain_page(m3mfn);
1747 l3e[3] = l3e_from_pfn(mfn_x(m2mfn), _PAGE_PRESENT);
1748 sh_install_xen_entries_in_l2h(v, m2mfn);
1749 sh_unmap_domain_page(l3e);
1752 #endif /* SHADOW_PAGING_LEVELS < 4 */
1753 return m4mfn;
1756 #elif CONFIG_PAGING_LEVELS == 3
1759 mfn_t m3mfn, m2mfn;
1760 l3_pgentry_t *l3e;
1761 l2_pgentry_t *l2e;
1762 int i;
1764 m3mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1765 /* Remember the level of this table */
1766 mfn_to_page(m3mfn)->shadow_flags = 3;
1768 // Install a monitor l2 table in slot 3 of the l3 table.
1769 // This is used for all Xen entries, including linear maps
1770 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1771 mfn_to_page(m2mfn)->shadow_flags = 2;
1772 l3e = sh_map_domain_page(m3mfn);
1773 l3e[3] = l3e_from_pfn(mfn_x(m2mfn), _PAGE_PRESENT);
1774 sh_install_xen_entries_in_l2h(v, m2mfn);
1775 /* Install the monitor's own linear map */
1776 l2e = sh_map_domain_page(m2mfn);
1777 for ( i = 0; i < L3_PAGETABLE_ENTRIES; i++ )
1778 l2e[l2_table_offset(LINEAR_PT_VIRT_START) + i] =
1779 (l3e_get_flags(l3e[i]) & _PAGE_PRESENT)
1780 ? l2e_from_pfn(l3e_get_pfn(l3e[i]), __PAGE_HYPERVISOR)
1781 : l2e_empty();
1782 sh_unmap_domain_page(l2e);
1783 sh_unmap_domain_page(l3e);
1785 SHADOW_PRINTK("new monitor table: %#lx\n", mfn_x(m3mfn));
1786 return m3mfn;
1789 #elif CONFIG_PAGING_LEVELS == 2
1792 mfn_t m2mfn;
1793 m2mfn = shadow_alloc(d, SH_type_monitor_table, 0);
1794 sh_install_xen_entries_in_l2(v, m2mfn, m2mfn);
1795 /* Remember the level of this table */
1796 mfn_to_page(m2mfn)->shadow_flags = 2;
1797 return m2mfn;
1800 #else
1801 #error this should not happen
1802 #endif /* CONFIG_PAGING_LEVELS */
1804 #endif /* SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS */
1806 /**************************************************************************/
1807 /* These functions also take a virtual address and return the level-N
1808 * shadow table mfn and entry, but they create the shadow pagetables if
1809 * they are needed. The "demand" argument is non-zero when handling
1810 * a demand fault (so we know what to do about accessed bits &c).
1811 * If the necessary tables are not present in the guest, they return NULL. */
1813 /* N.B. The use of GUEST_PAGING_LEVELS here is correct. If the shadow has
1814 * more levels than the guest, the upper levels are always fixed and do not
1815 * reflect any information from the guest, so we do not use these functions
1816 * to access them. */
1818 #if GUEST_PAGING_LEVELS >= 4
1819 static shadow_l4e_t * shadow_get_and_create_l4e(struct vcpu *v,
1820 walk_t *gw,
1821 mfn_t *sl4mfn)
1823 /* There is always a shadow of the top level table. Get it. */
1824 *sl4mfn = pagetable_get_mfn(v->arch.shadow_table[0]);
1825 /* Reading the top level table is always valid. */
1826 return sh_linear_l4_table(v) + shadow_l4_linear_offset(gw->va);
1829 static shadow_l3e_t * shadow_get_and_create_l3e(struct vcpu *v,
1830 walk_t *gw,
1831 mfn_t *sl3mfn,
1832 fetch_type_t ft)
1834 mfn_t sl4mfn;
1835 shadow_l4e_t *sl4e;
1836 if ( !mfn_valid(gw->l3mfn) ) return NULL; /* No guest page. */
1837 /* Get the l4e */
1838 sl4e = shadow_get_and_create_l4e(v, gw, &sl4mfn);
1839 ASSERT(sl4e != NULL);
1840 if ( shadow_l4e_get_flags(*sl4e) & _PAGE_PRESENT )
1842 *sl3mfn = shadow_l4e_get_mfn(*sl4e);
1843 ASSERT(mfn_valid(*sl3mfn));
1845 else
1847 int r;
1848 shadow_l4e_t new_sl4e;
1849 /* No l3 shadow installed: find and install it. */
1850 *sl3mfn = get_shadow_status(v, gw->l3mfn, SH_type_l3_shadow);
1851 if ( !mfn_valid(*sl3mfn) )
1853 /* No l3 shadow of this page exists at all: make one. */
1854 *sl3mfn = sh_make_shadow(v, gw->l3mfn, SH_type_l3_shadow);
1856 /* Install the new sl3 table in the sl4e */
1857 l4e_propagate_from_guest(v, gw->l4e, gw->l4mfn,
1858 *sl3mfn, &new_sl4e, ft);
1859 r = shadow_set_l4e(v, sl4e, new_sl4e, sl4mfn);
1860 ASSERT((r & SHADOW_SET_FLUSH) == 0);
1861 if ( r & SHADOW_SET_ERROR )
1862 return NULL;
1864 /* Now follow it down a level. Guaranteed to succeed. */
1865 return sh_linear_l3_table(v) + shadow_l3_linear_offset(gw->va);
1867 #endif /* GUEST_PAGING_LEVELS >= 4 */
1870 static shadow_l2e_t * shadow_get_and_create_l2e(struct vcpu *v,
1871 walk_t *gw,
1872 mfn_t *sl2mfn,
1873 fetch_type_t ft)
1875 #if GUEST_PAGING_LEVELS >= 4 /* 64bit... */
1876 mfn_t sl3mfn = _mfn(INVALID_MFN);
1877 shadow_l3e_t *sl3e;
1878 if ( !mfn_valid(gw->l2mfn) ) return NULL; /* No guest page. */
1879 /* Get the l3e */
1880 sl3e = shadow_get_and_create_l3e(v, gw, &sl3mfn, ft);
1881 if ( sl3e == NULL ) return NULL;
1882 if ( shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT )
1884 *sl2mfn = shadow_l3e_get_mfn(*sl3e);
1885 ASSERT(mfn_valid(*sl2mfn));
1887 else
1889 int r;
1890 shadow_l3e_t new_sl3e;
1891 unsigned int t = SH_type_l2_shadow;
1893 /* Tag compat L2 containing hypervisor (m2p) mappings */
1894 if ( is_pv_32on64_domain(v->domain) &&
1895 guest_l4_table_offset(gw->va) == 0 &&
1896 guest_l3_table_offset(gw->va) == 3 )
1897 t = SH_type_l2h_shadow;
1899 /* No l2 shadow installed: find and install it. */
1900 *sl2mfn = get_shadow_status(v, gw->l2mfn, t);
1901 if ( !mfn_valid(*sl2mfn) )
1903 /* No l2 shadow of this page exists at all: make one. */
1904 *sl2mfn = sh_make_shadow(v, gw->l2mfn, t);
1906 /* Install the new sl2 table in the sl3e */
1907 l3e_propagate_from_guest(v, gw->l3e, gw->l3mfn,
1908 *sl2mfn, &new_sl3e, ft);
1909 r = shadow_set_l3e(v, sl3e, new_sl3e, sl3mfn);
1910 ASSERT((r & SHADOW_SET_FLUSH) == 0);
1911 if ( r & SHADOW_SET_ERROR )
1912 return NULL;
1914 /* Now follow it down a level. Guaranteed to succeed. */
1915 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
1916 #elif GUEST_PAGING_LEVELS == 3 /* PAE... */
1917 /* We never demand-shadow PAE l3es: they are only created in
1918 * sh_update_cr3(). Check if the relevant sl3e is present. */
1919 shadow_l3e_t *sl3e = ((shadow_l3e_t *)&v->arch.paging.shadow.l3table)
1920 + shadow_l3_linear_offset(gw->va);
1921 if ( !(shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT) )
1922 return NULL;
1923 *sl2mfn = shadow_l3e_get_mfn(*sl3e);
1924 ASSERT(mfn_valid(*sl2mfn));
1925 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
1926 #else /* 32bit... */
1927 /* There is always a shadow of the top level table. Get it. */
1928 *sl2mfn = pagetable_get_mfn(v->arch.shadow_table[0]);
1929 /* This next line is important: the guest l2 has a 16k
1930 * shadow, we need to return the right mfn of the four. This
1931 * call will set it for us as a side-effect. */
1932 (void) shadow_l2_index(sl2mfn, guest_index(gw->l2e));
1933 /* Reading the top level table is always valid. */
1934 return sh_linear_l2_table(v) + shadow_l2_linear_offset(gw->va);
1935 #endif
1939 static shadow_l1e_t * shadow_get_and_create_l1e(struct vcpu *v,
1940 walk_t *gw,
1941 mfn_t *sl1mfn,
1942 fetch_type_t ft)
1944 mfn_t sl2mfn;
1945 shadow_l2e_t *sl2e;
1947 /* Get the l2e */
1948 sl2e = shadow_get_and_create_l2e(v, gw, &sl2mfn, ft);
1949 if ( sl2e == NULL ) return NULL;
1950 /* Install the sl1 in the l2e if it wasn't there or if we need to
1951 * re-do it to fix a PSE dirty bit. */
1952 if ( shadow_l2e_get_flags(*sl2e) & _PAGE_PRESENT
1953 && likely(ft != ft_demand_write
1954 || (guest_l2e_get_flags(*gw->l2e) & _PAGE_DIRTY)
1955 || !(guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE)) )
1957 *sl1mfn = shadow_l2e_get_mfn(*sl2e);
1958 ASSERT(mfn_valid(*sl1mfn));
1960 else
1962 shadow_l2e_t new_sl2e;
1963 int r, flags = guest_l2e_get_flags(*gw->l2e);
1964 /* No l1 shadow installed: find and install it. */
1965 if ( !(flags & _PAGE_PRESENT) )
1966 return NULL; /* No guest page. */
1967 if ( guest_supports_superpages(v) && (flags & _PAGE_PSE) )
1969 /* Splintering a superpage */
1970 gfn_t l2gfn = guest_l2e_get_gfn(*gw->l2e);
1971 *sl1mfn = get_fl1_shadow_status(v, l2gfn);
1972 if ( !mfn_valid(*sl1mfn) )
1974 /* No fl1 shadow of this superpage exists at all: make one. */
1975 *sl1mfn = make_fl1_shadow(v, l2gfn);
1978 else
1980 /* Shadowing an actual guest l1 table */
1981 if ( !mfn_valid(gw->l2mfn) ) return NULL; /* No guest page. */
1982 *sl1mfn = get_shadow_status(v, gw->l1mfn, SH_type_l1_shadow);
1983 if ( !mfn_valid(*sl1mfn) )
1985 /* No l1 shadow of this page exists at all: make one. */
1986 *sl1mfn = sh_make_shadow(v, gw->l1mfn, SH_type_l1_shadow);
1989 /* Install the new sl1 table in the sl2e */
1990 l2e_propagate_from_guest(v, gw->l2e, gw->l2mfn,
1991 *sl1mfn, &new_sl2e, ft);
1992 r = shadow_set_l2e(v, sl2e, new_sl2e, sl2mfn);
1993 ASSERT((r & SHADOW_SET_FLUSH) == 0);
1994 if ( r & SHADOW_SET_ERROR )
1995 return NULL;
1996 /* This next line is important: in 32-on-PAE and 32-on-64 modes,
1997 * the guest l1 table has an 8k shadow, and we need to return
1998 * the right mfn of the pair. This call will set it for us as a
1999 * side-effect. (In all other cases, it's a no-op and will be
2000 * compiled out.) */
2001 (void) shadow_l1_index(sl1mfn, guest_l1_table_offset(gw->va));
2003 /* Now follow it down a level. Guaranteed to succeed. */
2004 return sh_linear_l1_table(v) + shadow_l1_linear_offset(gw->va);
2009 /**************************************************************************/
2010 /* Destructors for shadow tables:
2011 * Unregister the shadow, decrement refcounts of any entries present in it,
2012 * and release the memory.
2014 * N.B. These destructors do not clear the contents of the shadows.
2015 * This allows us to delay TLB shootdowns until the page is being reused.
2016 * See shadow_alloc() and shadow_free() for how this is handled.
2017 */
2019 #if GUEST_PAGING_LEVELS >= 4
2020 void sh_destroy_l4_shadow(struct vcpu *v, mfn_t smfn)
2022 shadow_l4e_t *sl4e;
2023 u32 t = mfn_to_shadow_page(smfn)->type;
2024 mfn_t gmfn, sl4mfn;
2026 SHADOW_DEBUG(DESTROY_SHADOW,
2027 "%s(%05lx)\n", __func__, mfn_x(smfn));
2028 ASSERT(t == SH_type_l4_shadow);
2030 /* Record that the guest page isn't shadowed any more (in this type) */
2031 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2032 delete_shadow_status(v, gmfn, t, smfn);
2033 shadow_demote(v, gmfn, t);
2034 /* Decrement refcounts of all the old entries */
2035 sl4mfn = smfn;
2036 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, 0, v->domain, {
2037 if ( shadow_l4e_get_flags(*sl4e) & _PAGE_PRESENT )
2039 sh_put_ref(v, shadow_l4e_get_mfn(*sl4e),
2040 (((paddr_t)mfn_x(sl4mfn)) << PAGE_SHIFT)
2041 | ((unsigned long)sl4e & ~PAGE_MASK));
2043 });
2045 /* Put the memory back in the pool */
2046 shadow_free(v->domain, smfn);
2049 void sh_destroy_l3_shadow(struct vcpu *v, mfn_t smfn)
2051 shadow_l3e_t *sl3e;
2052 u32 t = mfn_to_shadow_page(smfn)->type;
2053 mfn_t gmfn, sl3mfn;
2055 SHADOW_DEBUG(DESTROY_SHADOW,
2056 "%s(%05lx)\n", __func__, mfn_x(smfn));
2057 ASSERT(t == SH_type_l3_shadow);
2059 /* Record that the guest page isn't shadowed any more (in this type) */
2060 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2061 delete_shadow_status(v, gmfn, t, smfn);
2062 shadow_demote(v, gmfn, t);
2064 /* Decrement refcounts of all the old entries */
2065 sl3mfn = smfn;
2066 SHADOW_FOREACH_L3E(sl3mfn, sl3e, 0, 0, {
2067 if ( shadow_l3e_get_flags(*sl3e) & _PAGE_PRESENT )
2068 sh_put_ref(v, shadow_l3e_get_mfn(*sl3e),
2069 (((paddr_t)mfn_x(sl3mfn)) << PAGE_SHIFT)
2070 | ((unsigned long)sl3e & ~PAGE_MASK));
2071 });
2073 /* Put the memory back in the pool */
2074 shadow_free(v->domain, smfn);
2076 #endif /* GUEST_PAGING_LEVELS >= 4 */
2079 void sh_destroy_l2_shadow(struct vcpu *v, mfn_t smfn)
2081 shadow_l2e_t *sl2e;
2082 u32 t = mfn_to_shadow_page(smfn)->type;
2083 mfn_t gmfn, sl2mfn;
2085 SHADOW_DEBUG(DESTROY_SHADOW,
2086 "%s(%05lx)\n", __func__, mfn_x(smfn));
2088 #if GUEST_PAGING_LEVELS >= 3
2089 ASSERT(t == SH_type_l2_shadow || t == SH_type_l2h_shadow);
2090 #else
2091 ASSERT(t == SH_type_l2_shadow);
2092 #endif
2094 /* Record that the guest page isn't shadowed any more (in this type) */
2095 gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2096 delete_shadow_status(v, gmfn, t, smfn);
2097 shadow_demote(v, gmfn, t);
2099 /* Decrement refcounts of all the old entries */
2100 sl2mfn = smfn;
2101 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, v->domain, {
2102 if ( shadow_l2e_get_flags(*sl2e) & _PAGE_PRESENT )
2103 sh_put_ref(v, shadow_l2e_get_mfn(*sl2e),
2104 (((paddr_t)mfn_x(sl2mfn)) << PAGE_SHIFT)
2105 | ((unsigned long)sl2e & ~PAGE_MASK));
2106 });
2108 /* Put the memory back in the pool */
2109 shadow_free(v->domain, smfn);
2112 void sh_destroy_l1_shadow(struct vcpu *v, mfn_t smfn)
2114 struct domain *d = v->domain;
2115 shadow_l1e_t *sl1e;
2116 u32 t = mfn_to_shadow_page(smfn)->type;
2118 SHADOW_DEBUG(DESTROY_SHADOW,
2119 "%s(%05lx)\n", __func__, mfn_x(smfn));
2120 ASSERT(t == SH_type_l1_shadow || t == SH_type_fl1_shadow);
2122 /* Record that the guest page isn't shadowed any more (in this type) */
2123 if ( t == SH_type_fl1_shadow )
2125 gfn_t gfn = _gfn(mfn_to_shadow_page(smfn)->backpointer);
2126 delete_fl1_shadow_status(v, gfn, smfn);
2128 else
2130 mfn_t gmfn = _mfn(mfn_to_shadow_page(smfn)->backpointer);
2131 delete_shadow_status(v, gmfn, t, smfn);
2132 shadow_demote(v, gmfn, t);
2135 if ( shadow_mode_refcounts(d) )
2137 /* Decrement refcounts of all the old entries */
2138 mfn_t sl1mfn = smfn;
2139 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, 0, {
2140 if ( (shadow_l1e_get_flags(*sl1e) & _PAGE_PRESENT)
2141 && !sh_l1e_is_magic(*sl1e) )
2142 shadow_put_page_from_l1e(*sl1e, d);
2143 });
2146 /* Put the memory back in the pool */
2147 shadow_free(v->domain, smfn);
2150 #if SHADOW_PAGING_LEVELS == GUEST_PAGING_LEVELS
2151 void sh_destroy_monitor_table(struct vcpu *v, mfn_t mmfn)
2153 struct domain *d = v->domain;
2154 ASSERT(mfn_to_shadow_page(mmfn)->type == SH_type_monitor_table);
2156 #if (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS != 4)
2157 /* Need to destroy the l3 monitor page in slot 0 too */
2159 mfn_t m3mfn;
2160 l4_pgentry_t *l4e = sh_map_domain_page(mmfn);
2161 ASSERT(l4e_get_flags(l4e[0]) & _PAGE_PRESENT);
2162 m3mfn = _mfn(l4e_get_pfn(l4e[0]));
2163 if ( is_pv_32on64_vcpu(v) )
2165 /* Need to destroy the l2 monitor page in slot 3 too */
2166 l3_pgentry_t *l3e = sh_map_domain_page(m3mfn);
2167 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
2168 shadow_free(d, _mfn(l3e_get_pfn(l3e[3])));
2169 sh_unmap_domain_page(l3e);
2171 shadow_free(d, m3mfn);
2172 sh_unmap_domain_page(l4e);
2174 #elif CONFIG_PAGING_LEVELS == 3
2175 /* Need to destroy the l2 monitor page in slot 4 too */
2177 l3_pgentry_t *l3e = sh_map_domain_page(mmfn);
2178 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
2179 shadow_free(d, _mfn(l3e_get_pfn(l3e[3])));
2180 sh_unmap_domain_page(l3e);
2182 #endif
2184 /* Put the memory back in the pool */
2185 shadow_free(d, mmfn);
2187 #endif
2189 /**************************************************************************/
2190 /* Functions to destroy non-Xen mappings in a pagetable hierarchy.
2191 * These are called from common code when we are running out of shadow
2192 * memory, and unpinning all the top-level shadows hasn't worked.
2194 * This implementation is pretty crude and slow, but we hope that it won't
2195 * be called very often. */
2197 #if GUEST_PAGING_LEVELS == 2
2199 void sh_unhook_32b_mappings(struct vcpu *v, mfn_t sl2mfn)
2201 shadow_l2e_t *sl2e;
2202 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, v->domain, {
2203 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
2204 });
2207 #elif GUEST_PAGING_LEVELS == 3
2209 void sh_unhook_pae_mappings(struct vcpu *v, mfn_t sl2mfn)
2210 /* Walk a PAE l2 shadow, unhooking entries from all the subshadows */
2212 shadow_l2e_t *sl2e;
2213 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, 0, v->domain, {
2214 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
2215 });
2218 #elif GUEST_PAGING_LEVELS == 4
2220 void sh_unhook_64b_mappings(struct vcpu *v, mfn_t sl4mfn)
2222 shadow_l4e_t *sl4e;
2223 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, 0, v->domain, {
2224 (void) shadow_set_l4e(v, sl4e, shadow_l4e_empty(), sl4mfn);
2225 });
2228 #endif
2230 /**************************************************************************/
2231 /* Internal translation functions.
2232 * These functions require a pointer to the shadow entry that will be updated.
2233 */
2235 /* These functions take a new guest entry, translate it to shadow and write
2236 * the shadow entry.
2238 * They return the same bitmaps as the shadow_set_lXe() functions.
2239 */
2241 #if GUEST_PAGING_LEVELS >= 4
2242 static int validate_gl4e(struct vcpu *v, void *new_ge, mfn_t sl4mfn, void *se)
2244 shadow_l4e_t new_sl4e;
2245 guest_l4e_t *new_gl4e = new_ge;
2246 shadow_l4e_t *sl4p = se;
2247 mfn_t sl3mfn = _mfn(INVALID_MFN);
2248 struct domain *d = v->domain;
2249 p2m_type_t p2mt;
2250 int result = 0;
2252 perfc_incr(shadow_validate_gl4e_calls);
2254 if ( guest_l4e_get_flags(*new_gl4e) & _PAGE_PRESENT )
2256 gfn_t gl3gfn = guest_l4e_get_gfn(*new_gl4e);
2257 mfn_t gl3mfn = gfn_to_mfn(d, gl3gfn, &p2mt);
2258 if ( p2m_is_ram(p2mt) )
2259 sl3mfn = get_shadow_status(v, gl3mfn, SH_type_l3_shadow);
2260 else
2261 result |= SHADOW_SET_ERROR;
2263 l4e_propagate_from_guest(v, new_gl4e, _mfn(INVALID_MFN),
2264 sl3mfn, &new_sl4e, ft_prefetch);
2266 // check for updates to xen reserved slots
2267 if ( !shadow_mode_external(d) )
2269 int shadow_index = (((unsigned long)sl4p & ~PAGE_MASK) /
2270 sizeof(shadow_l4e_t));
2271 int reserved_xen_slot = !is_guest_l4_slot(d, shadow_index);
2273 if ( unlikely(reserved_xen_slot) )
2275 // attempt by the guest to write to a xen reserved slot
2276 //
2277 SHADOW_PRINTK("%s out-of-range update "
2278 "sl4mfn=%05lx index=0x%x val=%" SH_PRI_pte "\n",
2279 __func__, mfn_x(sl4mfn), shadow_index, new_sl4e.l4);
2280 if ( shadow_l4e_get_flags(new_sl4e) & _PAGE_PRESENT )
2282 SHADOW_ERROR("out-of-range l4e update\n");
2283 result |= SHADOW_SET_ERROR;
2286 // do not call shadow_set_l4e...
2287 return result;
2291 result |= shadow_set_l4e(v, sl4p, new_sl4e, sl4mfn);
2292 return result;
2296 static int validate_gl3e(struct vcpu *v, void *new_ge, mfn_t sl3mfn, void *se)
2298 shadow_l3e_t new_sl3e;
2299 guest_l3e_t *new_gl3e = new_ge;
2300 shadow_l3e_t *sl3p = se;
2301 mfn_t sl2mfn = _mfn(INVALID_MFN);
2302 p2m_type_t p2mt;
2303 int result = 0;
2305 perfc_incr(shadow_validate_gl3e_calls);
2307 if ( guest_l3e_get_flags(*new_gl3e) & _PAGE_PRESENT )
2309 gfn_t gl2gfn = guest_l3e_get_gfn(*new_gl3e);
2310 mfn_t gl2mfn = gfn_to_mfn(v->domain, gl2gfn, &p2mt);
2311 if ( p2m_is_ram(p2mt) )
2312 sl2mfn = get_shadow_status(v, gl2mfn, SH_type_l2_shadow);
2313 else
2314 result |= SHADOW_SET_ERROR;
2316 l3e_propagate_from_guest(v, new_gl3e, _mfn(INVALID_MFN),
2317 sl2mfn, &new_sl3e, ft_prefetch);
2318 result |= shadow_set_l3e(v, sl3p, new_sl3e, sl3mfn);
2320 return result;
2322 #endif // GUEST_PAGING_LEVELS >= 4
2324 static int validate_gl2e(struct vcpu *v, void *new_ge, mfn_t sl2mfn, void *se)
2326 shadow_l2e_t new_sl2e;
2327 guest_l2e_t *new_gl2e = new_ge;
2328 shadow_l2e_t *sl2p = se;
2329 mfn_t sl1mfn = _mfn(INVALID_MFN);
2330 p2m_type_t p2mt;
2331 int result = 0;
2333 perfc_incr(shadow_validate_gl2e_calls);
2335 if ( guest_l2e_get_flags(*new_gl2e) & _PAGE_PRESENT )
2337 gfn_t gl1gfn = guest_l2e_get_gfn(*new_gl2e);
2338 if ( guest_supports_superpages(v) &&
2339 (guest_l2e_get_flags(*new_gl2e) & _PAGE_PSE) )
2341 // superpage -- need to look up the shadow L1 which holds the
2342 // splitters...
2343 sl1mfn = get_fl1_shadow_status(v, gl1gfn);
2344 #if 0
2345 // XXX - it's possible that we want to do some kind of prefetch
2346 // for superpage fl1's here, but this is *not* on the demand path,
2347 // so we'll hold off trying that for now...
2348 //
2349 if ( !mfn_valid(sl1mfn) )
2350 sl1mfn = make_fl1_shadow(v, gl1gfn);
2351 #endif
2353 else
2355 mfn_t gl1mfn = gfn_to_mfn(v->domain, gl1gfn, &p2mt);
2356 if ( p2m_is_ram(p2mt) )
2357 sl1mfn = get_shadow_status(v, gl1mfn, SH_type_l1_shadow);
2358 else
2359 result |= SHADOW_SET_ERROR;
2362 l2e_propagate_from_guest(v, new_gl2e, _mfn(INVALID_MFN),
2363 sl1mfn, &new_sl2e, ft_prefetch);
2365 // check for updates to xen reserved slots in PV guests...
2366 // XXX -- need to revisit this for PV 3-on-4 guests.
2367 //
2368 #if SHADOW_PAGING_LEVELS < 4
2369 #if CONFIG_PAGING_LEVELS == SHADOW_PAGING_LEVELS
2370 if ( !shadow_mode_external(v->domain) )
2372 int shadow_index = (((unsigned long)sl2p & ~PAGE_MASK) /
2373 sizeof(shadow_l2e_t));
2374 int reserved_xen_slot;
2376 #if SHADOW_PAGING_LEVELS == 3
2377 reserved_xen_slot =
2378 ((mfn_to_shadow_page(sl2mfn)->type == SH_type_l2h_pae_shadow) &&
2379 (shadow_index
2380 >= (L2_PAGETABLE_FIRST_XEN_SLOT & (L2_PAGETABLE_ENTRIES-1))));
2381 #else /* SHADOW_PAGING_LEVELS == 2 */
2382 reserved_xen_slot = (shadow_index >= L2_PAGETABLE_FIRST_XEN_SLOT);
2383 #endif
2385 if ( unlikely(reserved_xen_slot) )
2387 // attempt by the guest to write to a xen reserved slot
2388 //
2389 SHADOW_PRINTK("%s out-of-range update "
2390 "sl2mfn=%05lx index=0x%x val=%" SH_PRI_pte "\n",
2391 __func__, mfn_x(sl2mfn), shadow_index, new_sl2e.l2);
2392 if ( shadow_l2e_get_flags(new_sl2e) & _PAGE_PRESENT )
2394 SHADOW_ERROR("out-of-range l2e update\n");
2395 result |= SHADOW_SET_ERROR;
2398 // do not call shadow_set_l2e...
2399 return result;
2402 #endif /* CONFIG_PAGING_LEVELS == SHADOW_PAGING_LEVELS */
2403 #endif /* SHADOW_PAGING_LEVELS < 4 */
2405 result |= shadow_set_l2e(v, sl2p, new_sl2e, sl2mfn);
2407 return result;
2410 static int validate_gl1e(struct vcpu *v, void *new_ge, mfn_t sl1mfn, void *se)
2412 shadow_l1e_t new_sl1e;
2413 guest_l1e_t *new_gl1e = new_ge;
2414 shadow_l1e_t *sl1p = se;
2415 gfn_t gfn;
2416 mfn_t gmfn;
2417 p2m_type_t p2mt;
2418 int result = 0;
2420 perfc_incr(shadow_validate_gl1e_calls);
2422 gfn = guest_l1e_get_gfn(*new_gl1e);
2423 gmfn = gfn_to_mfn(v->domain, gfn, &p2mt);
2425 l1e_propagate_from_guest(v, new_gl1e, _mfn(INVALID_MFN), gmfn, &new_sl1e,
2426 ft_prefetch, p2mt);
2428 result |= shadow_set_l1e(v, sl1p, new_sl1e, sl1mfn);
2429 return result;
2433 /**************************************************************************/
2434 /* Functions which translate and install the shadows of arbitrary guest
2435 * entries that we have just seen the guest write. */
2438 static inline int
2439 sh_map_and_validate(struct vcpu *v, mfn_t gmfn,
2440 void *new_gp, u32 size, u32 sh_type,
2441 u32 (*shadow_index)(mfn_t *smfn, u32 idx),
2442 int (*validate_ge)(struct vcpu *v, void *ge,
2443 mfn_t smfn, void *se))
2444 /* Generic function for mapping and validating. */
2446 mfn_t smfn, smfn2, map_mfn;
2447 shadow_l1e_t *sl1p;
2448 u32 shadow_idx, guest_idx;
2449 int result = 0;
2451 /* Align address and size to guest entry boundaries */
2452 size += (unsigned long)new_gp & (sizeof (guest_l1e_t) - 1);
2453 new_gp = (void *)((unsigned long)new_gp & ~(sizeof (guest_l1e_t) - 1));
2454 size = (size + sizeof (guest_l1e_t) - 1) & ~(sizeof (guest_l1e_t) - 1);
2455 ASSERT(size + (((unsigned long)new_gp) & ~PAGE_MASK) <= PAGE_SIZE);
2457 /* Map the shadow page */
2458 smfn = get_shadow_status(v, gmfn, sh_type);
2459 ASSERT(mfn_valid(smfn)); /* Otherwise we would not have been called */
2460 guest_idx = guest_index(new_gp);
2461 map_mfn = smfn;
2462 shadow_idx = shadow_index(&map_mfn, guest_idx);
2463 sl1p = map_shadow_page(map_mfn);
2465 /* Validate one entry at a time */
2466 while ( size )
2468 smfn2 = smfn;
2469 guest_idx = guest_index(new_gp);
2470 shadow_idx = shadow_index(&smfn2, guest_idx);
2471 if ( mfn_x(smfn2) != mfn_x(map_mfn) )
2473 /* We have moved to another page of the shadow */
2474 map_mfn = smfn2;
2475 unmap_shadow_page(sl1p);
2476 sl1p = map_shadow_page(map_mfn);
2478 result |= validate_ge(v,
2479 new_gp,
2480 map_mfn,
2481 &sl1p[shadow_idx]);
2482 size -= sizeof(guest_l1e_t);
2483 new_gp += sizeof(guest_l1e_t);
2485 unmap_shadow_page(sl1p);
2486 return result;
2490 int
2491 sh_map_and_validate_gl4e(struct vcpu *v, mfn_t gl4mfn,
2492 void *new_gl4p, u32 size)
2494 #if GUEST_PAGING_LEVELS >= 4
2495 return sh_map_and_validate(v, gl4mfn, new_gl4p, size,
2496 SH_type_l4_shadow,
2497 shadow_l4_index,
2498 validate_gl4e);
2499 #else // ! GUEST_PAGING_LEVELS >= 4
2500 SHADOW_ERROR("called in wrong paging mode!\n");
2501 BUG();
2502 return 0;
2503 #endif
2506 int
2507 sh_map_and_validate_gl3e(struct vcpu *v, mfn_t gl3mfn,
2508 void *new_gl3p, u32 size)
2510 #if GUEST_PAGING_LEVELS >= 4
2511 return sh_map_and_validate(v, gl3mfn, new_gl3p, size,
2512 SH_type_l3_shadow,
2513 shadow_l3_index,
2514 validate_gl3e);
2515 #else // ! GUEST_PAGING_LEVELS >= 4
2516 SHADOW_ERROR("called in wrong paging mode!\n");
2517 BUG();
2518 return 0;
2519 #endif
2522 int
2523 sh_map_and_validate_gl2e(struct vcpu *v, mfn_t gl2mfn,
2524 void *new_gl2p, u32 size)
2526 return sh_map_and_validate(v, gl2mfn, new_gl2p, size,
2527 SH_type_l2_shadow,
2528 shadow_l2_index,
2529 validate_gl2e);
2532 int
2533 sh_map_and_validate_gl2he(struct vcpu *v, mfn_t gl2mfn,
2534 void *new_gl2p, u32 size)
2536 #if GUEST_PAGING_LEVELS >= 3
2537 return sh_map_and_validate(v, gl2mfn, new_gl2p, size,
2538 SH_type_l2h_shadow,
2539 shadow_l2_index,
2540 validate_gl2e);
2541 #else /* Non-PAE guests don't have different kinds of l2 table */
2542 SHADOW_ERROR("called in wrong paging mode!\n");
2543 BUG();
2544 return 0;
2545 #endif
2548 int
2549 sh_map_and_validate_gl1e(struct vcpu *v, mfn_t gl1mfn,
2550 void *new_gl1p, u32 size)
2552 return sh_map_and_validate(v, gl1mfn, new_gl1p, size,
2553 SH_type_l1_shadow,
2554 shadow_l1_index,
2555 validate_gl1e);
2559 /**************************************************************************/
2560 /* Optimization: If we see two emulated writes of zeros to the same
2561 * page-table without another kind of page fault in between, we guess
2562 * that this is a batch of changes (for process destruction) and
2563 * unshadow the page so we don't take a pagefault on every entry. This
2564 * should also make finding writeable mappings of pagetables much
2565 * easier. */
2567 /* Look to see if this is the second emulated write in a row to this
2568 * page, and unshadow/unhook if it is */
2569 static inline void check_for_early_unshadow(struct vcpu *v, mfn_t gmfn)
2571 #if SHADOW_OPTIMIZATIONS & SHOPT_EARLY_UNSHADOW
2572 if ( v->arch.paging.shadow.last_emulated_mfn == mfn_x(gmfn) &&
2573 sh_mfn_is_a_page_table(gmfn) )
2575 u32 flags = mfn_to_page(gmfn)->shadow_flags;
2576 if ( !(flags & (SHF_L2_32|SHF_L2_PAE|SHF_L2H_PAE|SHF_L4_64)) )
2578 perfc_incr(shadow_early_unshadow);
2579 sh_remove_shadows(v, gmfn, 1, 0 /* Fast, can fail to unshadow */ );
2582 v->arch.paging.shadow.last_emulated_mfn = mfn_x(gmfn);
2583 #endif
2586 /* Stop counting towards early unshadows, as we've seen a real page fault */
2587 static inline void reset_early_unshadow(struct vcpu *v)
2589 #if SHADOW_OPTIMIZATIONS & SHOPT_EARLY_UNSHADOW
2590 v->arch.paging.shadow.last_emulated_mfn = INVALID_MFN;
2591 #endif
2596 /**************************************************************************/
2597 /* Optimization: Prefetch multiple L1 entries. This is called after we have
2598 * demand-faulted a shadow l1e in the fault handler, to see if it's
2599 * worth fetching some more.
2600 */
2602 #if SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH
2604 /* XXX magic number */
2605 #define PREFETCH_DISTANCE 32
2607 static void sh_prefetch(struct vcpu *v, walk_t *gw,
2608 shadow_l1e_t *ptr_sl1e, mfn_t sl1mfn)
2610 int i, dist;
2611 gfn_t gfn;
2612 mfn_t gmfn;
2613 guest_l1e_t gl1e;
2614 shadow_l1e_t sl1e;
2615 u32 gflags;
2616 p2m_type_t p2mt;
2618 /* Prefetch no further than the end of the _shadow_ l1 MFN */
2619 dist = (PAGE_SIZE - ((unsigned long)ptr_sl1e & ~PAGE_MASK)) / sizeof sl1e;
2620 /* And no more than a maximum fetches-per-fault */
2621 if ( dist > PREFETCH_DISTANCE )
2622 dist = PREFETCH_DISTANCE;
2624 for ( i = 1; i < dist ; i++ )
2626 /* No point in prefetching if there's already a shadow */
2627 if ( ptr_sl1e[i].l1 != 0 )
2628 break;
2630 if ( gw->l1e )
2632 /* Normal guest page; grab the next guest entry */
2633 gl1e = gw->l1e[i];
2634 /* Not worth continuing if we hit an entry that will need another
2635 * fault for A/D-bit propagation anyway */
2636 gflags = guest_l1e_get_flags(gl1e);
2637 if ( (gflags & _PAGE_PRESENT)
2638 && (!(gflags & _PAGE_ACCESSED)
2639 || ((gflags & _PAGE_RW) && !(gflags & _PAGE_DIRTY))) )
2640 break;
2642 else
2644 /* Fragmented superpage, unless we've been called wrongly */
2645 ASSERT(guest_l2e_get_flags(*gw->l2e) & _PAGE_PSE);
2646 /* Increment the l1e's GFN by the right number of guest pages */
2647 gl1e = guest_l1e_from_gfn(
2648 _gfn(gfn_x(guest_l1e_get_gfn(gw->eff_l1e)) + i),
2649 guest_l1e_get_flags(gw->eff_l1e));
2652 /* Look at the gfn that the l1e is pointing at */
2653 gfn = guest_l1e_get_gfn(gl1e);
2654 gmfn = gfn_to_mfn(v->domain, gfn, &p2mt);
2656 /* Propagate the entry. Safe to use a pointer to our local
2657 * gl1e, since this is not a demand-fetch so there will be no
2658 * write-back to the guest. */
2659 l1e_propagate_from_guest(v, &gl1e, _mfn(INVALID_MFN),
2660 gmfn, &sl1e, ft_prefetch, p2mt);
2661 (void) shadow_set_l1e(v, ptr_sl1e + i, sl1e, sl1mfn);
2665 #endif /* SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH */
2668 /**************************************************************************/
2669 /* Entry points into the shadow code */
2671 /* Called from pagefault handler in Xen, and from the HVM trap handlers
2672 * for pagefaults. Returns 1 if this fault was an artefact of the
2673 * shadow code (and the guest should retry) or 0 if it is not (and the
2674 * fault should be handled elsewhere or passed to the guest). */
2676 static int sh_page_fault(struct vcpu *v,
2677 unsigned long va,
2678 struct cpu_user_regs *regs)
2680 struct domain *d = v->domain;
2681 walk_t gw;
2682 u32 accumulated_gflags;
2683 gfn_t gfn;
2684 mfn_t gmfn, sl1mfn=_mfn(0);
2685 shadow_l1e_t sl1e, *ptr_sl1e;
2686 paddr_t gpa;
2687 struct sh_emulate_ctxt emul_ctxt;
2688 struct x86_emulate_ops *emul_ops;
2689 int r;
2690 fetch_type_t ft = 0;
2691 p2m_type_t p2mt;
2693 SHADOW_PRINTK("d:v=%u:%u va=%#lx err=%u\n",
2694 v->domain->domain_id, v->vcpu_id, va, regs->error_code);
2696 perfc_incr(shadow_fault);
2697 //
2698 // XXX: Need to think about eventually mapping superpages directly in the
2699 // shadow (when possible), as opposed to splintering them into a
2700 // bunch of 4K maps.
2701 //
2703 #if (SHADOW_OPTIMIZATIONS & SHOPT_FAST_FAULT_PATH) && SHADOW_PAGING_LEVELS > 2
2704 if ( (regs->error_code & PFEC_reserved_bit) )
2706 /* The only reasons for reserved bits to be set in shadow entries
2707 * are the two "magic" shadow_l1e entries. */
2708 if ( likely((__copy_from_user(&sl1e,
2709 (sh_linear_l1_table(v)
2710 + shadow_l1_linear_offset(va)),
2711 sizeof(sl1e)) == 0)
2712 && sh_l1e_is_magic(sl1e)) )
2714 if ( sh_l1e_is_gnp(sl1e) )
2716 /* Not-present in a guest PT: pass to the guest as
2717 * a not-present fault (by flipping two bits). */
2718 ASSERT(regs->error_code & PFEC_page_present);
2719 regs->error_code ^= (PFEC_reserved_bit|PFEC_page_present);
2720 reset_early_unshadow(v);
2721 perfc_incr(shadow_fault_fast_gnp);
2722 SHADOW_PRINTK("fast path not-present\n");
2723 return 0;
2725 else
2727 /* Magic MMIO marker: extract gfn for MMIO address */
2728 ASSERT(sh_l1e_is_mmio(sl1e));
2729 gpa = (((paddr_t)(gfn_x(sh_l1e_mmio_get_gfn(sl1e))))
2730 << PAGE_SHIFT)
2731 | (va & ~PAGE_MASK);
2733 perfc_incr(shadow_fault_fast_mmio);
2734 SHADOW_PRINTK("fast path mmio %#"PRIpaddr"\n", gpa);
2735 reset_early_unshadow(v);
2736 handle_mmio(gpa);
2737 return EXCRET_fault_fixed;
2739 else
2741 /* This should be exceptionally rare: another vcpu has fixed
2742 * the tables between the fault and our reading the l1e.
2743 * Retry and let the hardware give us the right fault next time. */
2744 perfc_incr(shadow_fault_fast_fail);
2745 SHADOW_PRINTK("fast path false alarm!\n");
2746 return EXCRET_fault_fixed;
2749 #endif /* SHOPT_FAST_FAULT_PATH */
2751 /* Detect if this page fault happened while we were already in Xen
2752 * doing a shadow operation. If that happens, the only thing we can
2753 * do is let Xen's normal fault handlers try to fix it. In any case,
2754 * a diagnostic trace of the fault will be more useful than
2755 * a BUG() when we try to take the lock again. */
2756 if ( unlikely(shadow_locked_by_me(d)) )
2758 SHADOW_ERROR("Recursive shadow fault: lock was taken by %s\n",
2759 d->arch.paging.shadow.locker_function);
2760 return 0;
2763 shadow_lock(d);
2765 shadow_audit_tables(v);
2767 if ( guest_walk_tables(v, va, &gw, 1) != 0 )
2769 SHADOW_PRINTK("malformed guest pagetable\n");
2770 print_gw(&gw);
2773 /* It's possible that the guest has put pagetables in memory that it has
2774 * already used for some special purpose (ioreq pages, or granted pages).
2775 * If that happens we'll have killed the guest already but it's still not
2776 * safe to propagate entries out of the guest PT so get out now. */
2777 if ( unlikely(d->is_shutting_down) )
2779 SHADOW_PRINTK("guest is shutting down\n");
2780 shadow_unlock(d);
2781 return 0;
2784 sh_audit_gw(v, &gw);
2786 // We do not look at the gw->l1e, as that will not exist for superpages.
2787 // Instead, we use the gw->eff_l1e...
2788 //
2789 // We need not check all the levels of the guest page table entries for
2790 // present vs not-present, as the eff_l1e will always be not present if
2791 // one of the higher level entries is not present.
2792 //
2793 if ( unlikely(!(guest_l1e_get_flags(gw.eff_l1e) & _PAGE_PRESENT)) )
2795 perfc_incr(shadow_fault_bail_not_present);
2796 goto not_a_shadow_fault;
2799 // All levels of the guest page table are now known to be present.
2800 accumulated_gflags = accumulate_guest_flags(v, &gw);
2802 // Check for attempts to access supervisor-only pages from user mode,
2803 // i.e. ring 3. Such errors are not caused or dealt with by the shadow
2804 // code.
2805 //
2806 if ( (regs->error_code & PFEC_user_mode) &&
2807 !(accumulated_gflags & _PAGE_USER) )
2809 /* illegal user-mode access to supervisor-only page */
2810 perfc_incr(shadow_fault_bail_user_supervisor);
2811 goto not_a_shadow_fault;
2814 // Was it a write fault?
2815 ft = ((regs->error_code & PFEC_write_access)
2816 ? ft_demand_write : ft_demand_read);
2817 if ( ft == ft_demand_write )
2819 if ( unlikely(!(accumulated_gflags & _PAGE_RW)) )
2821 perfc_incr(shadow_fault_bail_ro_mapping);
2822 goto not_a_shadow_fault;
2825 else // must have been either an insn fetch or read fault
2827 // Check for NX bit violations: attempts to execute code that is
2828 // marked "do not execute". Such errors are not caused or dealt with
2829 // by the shadow code.
2830 //
2831 if ( regs->error_code & PFEC_insn_fetch )
2833 if ( accumulated_gflags & _PAGE_NX_BIT )
2835 /* NX prevented this code fetch */
2836 perfc_incr(shadow_fault_bail_nx);
2837 goto not_a_shadow_fault;
2842 /* What mfn is the guest trying to access? */
2843 gfn = guest_l1e_get_gfn(gw.eff_l1e);
2844 gmfn = gfn_to_mfn(d, gfn, &p2mt);
2846 if ( !p2m_is_valid(p2mt) || (!p2m_is_mmio(p2mt) && !mfn_valid(gmfn)) )
2848 perfc_incr(shadow_fault_bail_bad_gfn);
2849 SHADOW_PRINTK("BAD gfn=%"SH_PRI_gfn" gmfn=%"PRI_mfn"\n",
2850 gfn_x(gfn), mfn_x(gmfn));
2851 goto not_a_shadow_fault;
2854 /* Make sure there is enough free shadow memory to build a chain of
2855 * shadow tables. (We never allocate a top-level shadow on this path,
2856 * only a 32b l1, pae l1, or 64b l3+2+1. Note that while
2857 * SH_type_l1_shadow isn't correct in the latter case, all page
2858 * tables are the same size there.) */
2859 shadow_prealloc(d,
2860 SH_type_l1_shadow,
2861 GUEST_PAGING_LEVELS < 4 ? 1 : GUEST_PAGING_LEVELS - 1);
2863 /* Acquire the shadow. This must happen before we figure out the rights
2864 * for the shadow entry, since we might promote a page here. */
2865 ptr_sl1e = shadow_get_and_create_l1e(v, &gw, &sl1mfn, ft);
2866 if ( unlikely(ptr_sl1e == NULL) )
2868 /* Couldn't get the sl1e! Since we know the guest entries
2869 * are OK, this can only have been caused by a failed
2870 * shadow_set_l*e(), which will have crashed the guest.
2871 * Get out of the fault handler immediately. */
2872 ASSERT(d->is_shutting_down);
2873 unmap_walk(v, &gw);
2874 shadow_unlock(d);
2875 return 0;
2878 /* Calculate the shadow entry and write it */
2879 l1e_propagate_from_guest(v, (gw.l1e) ? gw.l1e : &gw.eff_l1e, gw.l1mfn,
2880 gmfn, &sl1e, ft, p2mt);
2881 r = shadow_set_l1e(v, ptr_sl1e, sl1e, sl1mfn);
2883 #if SHADOW_OPTIMIZATIONS & SHOPT_PREFETCH
2884 /* Prefetch some more shadow entries */
2885 sh_prefetch(v, &gw, ptr_sl1e, sl1mfn);
2886 #endif
2888 /* Need to emulate accesses to page tables */
2889 if ( sh_mfn_is_a_page_table(gmfn) )
2891 if ( ft == ft_demand_write )
2893 perfc_incr(shadow_fault_emulate_write);
2894 goto emulate;
2896 else if ( shadow_mode_trap_reads(d) && ft == ft_demand_read )
2898 perfc_incr(shadow_fault_emulate_read);
2899 goto emulate;
2903 /* Need to hand off device-model MMIO and writes to read-only
2904 * memory to the device model */
2905 if ( p2mt == p2m_mmio_dm
2906 || (p2mt == p2m_ram_ro && ft == ft_demand_write) )
2908 gpa = guest_walk_to_gpa(&gw);
2909 goto mmio;
2912 perfc_incr(shadow_fault_fixed);
2913 d->arch.paging.log_dirty.fault_count++;
2914 reset_early_unshadow(v);
2916 done:
2917 sh_audit_gw(v, &gw);
2918 unmap_walk(v, &gw);
2919 SHADOW_PRINTK("fixed\n");
2920 shadow_audit_tables(v);
2921 shadow_unlock(d);
2922 return EXCRET_fault_fixed;
2924 emulate:
2925 if ( !shadow_mode_refcounts(d) || !guest_mode(regs) )
2926 goto not_a_shadow_fault;
2928 /*
2929 * We do not emulate user writes. Instead we use them as a hint that the
2930 * page is no longer a page table. This behaviour differs from native, but
2931 * it seems very unlikely that any OS grants user access to page tables.
2932 */
2933 if ( (regs->error_code & PFEC_user_mode) )
2935 SHADOW_PRINTK("user-mode fault to PT, unshadowing mfn %#lx\n",
2936 mfn_x(gmfn));
2937 perfc_incr(shadow_fault_emulate_failed);
2938 sh_remove_shadows(v, gmfn, 0 /* thorough */, 1 /* must succeed */);
2939 goto done;
2942 if ( is_hvm_domain(d) )
2944 /*
2945 * If we are in the middle of injecting an exception or interrupt then
2946 * we should not emulate: it is not the instruction at %eip that caused
2947 * the fault. Furthermore it is almost certainly the case the handler
2948 * stack is currently considered to be a page table, so we should
2949 * unshadow the faulting page before exiting.
2950 */
2951 if ( unlikely(hvm_event_pending(v)) )
2953 gdprintk(XENLOG_DEBUG, "write to pagetable during event "
2954 "injection: cr2=%#lx, mfn=%#lx\n",
2955 va, mfn_x(gmfn));
2956 sh_remove_shadows(v, gmfn, 0 /* thorough */, 1 /* must succeed */);
2957 goto done;
2961 SHADOW_PRINTK("emulate: eip=%#lx esp=%#lx\n",
2962 (unsigned long)regs->eip, (unsigned long)regs->esp);
2964 /*
2965 * We don't need to hold the lock for the whole emulation; we will
2966 * take it again when we write to the pagetables.
2967 */
2968 sh_audit_gw(v, &gw);
2969 unmap_walk(v, &gw);
2970 shadow_audit_tables(v);
2971 shadow_unlock(d);
2973 emul_ops = shadow_init_emulation(&emul_ctxt, regs);
2975 r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
2977 /*
2978 * NB. We do not unshadow on X86EMUL_EXCEPTION. It's not clear that it
2979 * would be a good unshadow hint. If we *do* decide to unshadow-on-fault
2980 * then it must be 'failable': we cannot require the unshadow to succeed.
2981 */
2982 if ( r == X86EMUL_UNHANDLEABLE )
2984 SHADOW_PRINTK("emulator failure, unshadowing mfn %#lx\n",
2985 mfn_x(gmfn));
2986 perfc_incr(shadow_fault_emulate_failed);
2987 /* If this is actually a page table, then we have a bug, and need
2988 * to support more operations in the emulator. More likely,
2989 * though, this is a hint that this page should not be shadowed. */
2990 shadow_remove_all_shadows(v, gmfn);
2993 #if GUEST_PAGING_LEVELS == 3 /* PAE guest */
2994 if ( r == X86EMUL_OKAY ) {
2995 int i;
2996 /* Emulate up to four extra instructions in the hope of catching
2997 * the "second half" of a 64-bit pagetable write. */
2998 for ( i = 0 ; i < 4 ; i++ )
3000 shadow_continue_emulation(&emul_ctxt, regs);
3001 v->arch.paging.last_write_was_pt = 0;
3002 r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
3003 if ( r == X86EMUL_OKAY )
3005 if ( v->arch.paging.last_write_was_pt )
3007 perfc_incr(shadow_em_ex_pt);
3008 break; /* Don't emulate past the other half of the write */
3010 else
3011 perfc_incr(shadow_em_ex_non_pt);
3013 else
3015 perfc_incr(shadow_em_ex_fail);
3016 break; /* Don't emulate again if we failed! */
3020 #endif /* PAE guest */
3022 SHADOW_PRINTK("emulated\n");
3023 return EXCRET_fault_fixed;
3025 mmio:
3026 if ( !guest_mode(regs) )
3027 goto not_a_shadow_fault;
3028 perfc_incr(shadow_fault_mmio);
3029 sh_audit_gw(v, &gw);
3030 unmap_walk(v, &gw);
3031 SHADOW_PRINTK("mmio %#"PRIpaddr"\n", gpa);
3032 shadow_audit_tables(v);
3033 reset_early_unshadow(v);
3034 shadow_unlock(d);
3035 handle_mmio(gpa);
3036 return EXCRET_fault_fixed;
3038 not_a_shadow_fault:
3039 sh_audit_gw(v, &gw);
3040 unmap_walk(v, &gw);
3041 SHADOW_PRINTK("not a shadow fault\n");
3042 shadow_audit_tables(v);
3043 reset_early_unshadow(v);
3044 shadow_unlock(d);
3045 return 0;
3049 static int
3050 sh_invlpg(struct vcpu *v, unsigned long va)
3051 /* Called when the guest requests an invlpg. Returns 1 if the invlpg
3052 * instruction should be issued on the hardware, or 0 if it's safe not
3053 * to do so. */
3055 shadow_l2e_t sl2e;
3057 perfc_incr(shadow_invlpg);
3059 #if (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB)
3060 /* No longer safe to use cached gva->gfn translations */
3061 vtlb_flush(v);
3062 #endif
3064 /* First check that we can safely read the shadow l2e. SMP/PAE linux can
3065 * run as high as 6% of invlpg calls where we haven't shadowed the l2
3066 * yet. */
3067 #if SHADOW_PAGING_LEVELS == 4
3069 shadow_l3e_t sl3e;
3070 if ( !(shadow_l4e_get_flags(
3071 sh_linear_l4_table(v)[shadow_l4_linear_offset(va)])
3072 & _PAGE_PRESENT) )
3073 return 0;
3074 /* This must still be a copy-from-user because we don't have the
3075 * shadow lock, and the higher-level shadows might disappear
3076 * under our feet. */
3077 if ( __copy_from_user(&sl3e, (sh_linear_l3_table(v)
3078 + shadow_l3_linear_offset(va)),
3079 sizeof (sl3e)) != 0 )
3081 perfc_incr(shadow_invlpg_fault);
3082 return 0;
3084 if ( (!shadow_l3e_get_flags(sl3e) & _PAGE_PRESENT) )
3085 return 0;
3087 #elif SHADOW_PAGING_LEVELS == 3
3088 if ( !(l3e_get_flags(v->arch.paging.shadow.l3table[shadow_l3_linear_offset(va)])
3089 & _PAGE_PRESENT) )
3090 // no need to flush anything if there's no SL2...
3091 return 0;
3092 #endif
3094 /* This must still be a copy-from-user because we don't have the shadow
3095 * lock, and the higher-level shadows might disappear under our feet. */
3096 if ( __copy_from_user(&sl2e,
3097 sh_linear_l2_table(v) + shadow_l2_linear_offset(va),
3098 sizeof (sl2e)) != 0 )
3100 perfc_incr(shadow_invlpg_fault);
3101 return 0;
3104 // If there's nothing shadowed for this particular sl2e, then
3105 // there is no need to do an invlpg, either...
3106 //
3107 if ( !(shadow_l2e_get_flags(sl2e) & _PAGE_PRESENT) )
3108 return 0;
3110 // Check to see if the SL2 is a splintered superpage...
3111 // If so, then we'll need to flush the entire TLB (because that's
3112 // easier than invalidating all of the individual 4K pages).
3113 //
3114 if ( mfn_to_shadow_page(shadow_l2e_get_mfn(sl2e))->type
3115 == SH_type_fl1_shadow )
3117 flush_tlb_local();
3118 return 0;
3121 return 1;
3125 static unsigned long
3126 sh_gva_to_gfn(struct vcpu *v, unsigned long va)
3127 /* Called to translate a guest virtual address to what the *guest*
3128 * pagetables would map it to. */
3130 walk_t gw;
3131 gfn_t gfn;
3133 #if (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB)
3134 struct shadow_vtlb t = {0};
3135 if ( vtlb_lookup(v, va, &t) )
3136 return t.frame_number;
3137 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB) */
3139 guest_walk_tables(v, va, &gw, 0);
3140 gfn = guest_walk_to_gfn(&gw);
3142 #if (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB)
3143 t.page_number = va >> PAGE_SHIFT;
3144 t.frame_number = gfn_x(gfn);
3145 t.flags = accumulate_guest_flags(v, &gw);
3146 vtlb_insert(v, t);
3147 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB) */
3149 unmap_walk(v, &gw);
3150 return gfn_x(gfn);
3154 static inline void
3155 sh_update_linear_entries(struct vcpu *v)
3156 /* Sync up all the linear mappings for this vcpu's pagetables */
3158 struct domain *d = v->domain;
3160 /* Linear pagetables in PV guests
3161 * ------------------------------
3163 * Guest linear pagetables, which map the guest pages, are at
3164 * LINEAR_PT_VIRT_START. Shadow linear pagetables, which map the
3165 * shadows, are at SH_LINEAR_PT_VIRT_START. Most of the time these
3166 * are set up at shadow creation time, but (of course!) the PAE case
3167 * is subtler. Normal linear mappings are made by having an entry
3168 * in the top-level table that points to itself (shadow linear) or
3169 * to the guest top-level table (guest linear). For PAE, to set up
3170 * a linear map requires us to copy the four top-level entries into
3171 * level-2 entries. That means that every time we change a PAE l3e,
3172 * we need to reflect the change into the copy.
3174 * Linear pagetables in HVM guests
3175 * -------------------------------
3177 * For HVM guests, the linear pagetables are installed in the monitor
3178 * tables (since we can't put them in the shadow). Shadow linear
3179 * pagetables, which map the shadows, are at SH_LINEAR_PT_VIRT_START,
3180 * and we use the linear pagetable slot at LINEAR_PT_VIRT_START for
3181 * a linear pagetable of the monitor tables themselves. We have
3182 * the same issue of having to re-copy PAE l3 entries whevever we use
3183 * PAE shadows.
3185 * Because HVM guests run on the same monitor tables regardless of the
3186 * shadow tables in use, the linear mapping of the shadow tables has to
3187 * be updated every time v->arch.shadow_table changes.
3188 */
3190 /* Don't try to update the monitor table if it doesn't exist */
3191 if ( shadow_mode_external(d)
3192 && pagetable_get_pfn(v->arch.monitor_table) == 0 )
3193 return;
3195 #if (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS == 4)
3197 /* For PV, one l4e points at the guest l4, one points at the shadow
3198 * l4. No maintenance required.
3199 * For HVM, just need to update the l4e that points to the shadow l4. */
3201 if ( shadow_mode_external(d) )
3203 /* Use the linear map if we can; otherwise make a new mapping */
3204 if ( v == current )
3206 __linear_l4_table[l4_linear_offset(SH_LINEAR_PT_VIRT_START)] =
3207 l4e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3208 __PAGE_HYPERVISOR);
3210 else
3212 l4_pgentry_t *ml4e;
3213 ml4e = sh_map_domain_page(pagetable_get_mfn(v->arch.monitor_table));
3214 ml4e[l4_table_offset(SH_LINEAR_PT_VIRT_START)] =
3215 l4e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3216 __PAGE_HYPERVISOR);
3217 sh_unmap_domain_page(ml4e);
3221 #elif (CONFIG_PAGING_LEVELS == 4) && (SHADOW_PAGING_LEVELS == 3)
3223 /* PV: XXX
3225 * HVM: To give ourselves a linear map of the shadows, we need to
3226 * extend a PAE shadow to 4 levels. We do this by having a monitor
3227 * l3 in slot 0 of the monitor l4 table, and copying the PAE l3
3228 * entries into it. Then, by having the monitor l4e for shadow
3229 * pagetables also point to the monitor l4, we can use it to access
3230 * the shadows.
3231 */
3233 if ( shadow_mode_external(d) )
3235 /* Install copies of the shadow l3es into the monitor l3 table.
3236 * The monitor l3 table is hooked into slot 0 of the monitor
3237 * l4 table, so we use l3 linear indices 0 to 3 */
3238 shadow_l3e_t *sl3e;
3239 l3_pgentry_t *ml3e;
3240 mfn_t l3mfn;
3241 int i;
3243 /* Use linear mappings if we can; otherwise make new mappings */
3244 if ( v == current )
3246 ml3e = __linear_l3_table;
3247 l3mfn = _mfn(l4e_get_pfn(__linear_l4_table[0]));
3249 else
3251 l4_pgentry_t *ml4e;
3252 ml4e = sh_map_domain_page(pagetable_get_mfn(v->arch.monitor_table));
3253 ASSERT(l4e_get_flags(ml4e[0]) & _PAGE_PRESENT);
3254 l3mfn = _mfn(l4e_get_pfn(ml4e[0]));
3255 ml3e = sh_map_domain_page(l3mfn);
3256 sh_unmap_domain_page(ml4e);
3259 /* Shadow l3 tables are made up by sh_update_cr3 */
3260 sl3e = v->arch.paging.shadow.l3table;
3262 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3264 ml3e[i] =
3265 (shadow_l3e_get_flags(sl3e[i]) & _PAGE_PRESENT)
3266 ? l3e_from_pfn(mfn_x(shadow_l3e_get_mfn(sl3e[i])),
3267 __PAGE_HYPERVISOR)
3268 : l3e_empty();
3271 if ( v != current )
3272 sh_unmap_domain_page(ml3e);
3274 else
3275 domain_crash(d); /* XXX */
3277 #elif CONFIG_PAGING_LEVELS == 3
3279 /* PV: need to copy the guest's l3 entries into the guest-linear-map l2
3280 * entries in the shadow, and the shadow's l3 entries into the
3281 * shadow-linear-map l2 entries in the shadow. This is safe to do
3282 * because Xen does not let guests share high-slot l2 tables between l3s,
3283 * so we know we're not treading on anyone's toes.
3285 * HVM: need to copy the shadow's l3 entries into the
3286 * shadow-linear-map l2 entries in the monitor table. This is safe
3287 * because we have one monitor table for each vcpu. The monitor's
3288 * own l3es don't need to be copied because they never change.
3289 * XXX That might change if we start stuffing things into the rest
3290 * of the monitor's virtual address space.
3291 */
3293 l2_pgentry_t *l2e, new_l2e;
3294 shadow_l3e_t *guest_l3e = NULL, *shadow_l3e;
3295 int i;
3296 int unmap_l2e = 0;
3298 #if GUEST_PAGING_LEVELS == 2
3300 /* Shadow l3 tables were built by sh_update_cr3 */
3301 BUG_ON(!shadow_mode_external(d)); /* PV 2-on-3 is unsupported */
3302 shadow_l3e = (shadow_l3e_t *)&v->arch.paging.shadow.l3table;
3304 #else /* GUEST_PAGING_LEVELS == 3 */
3306 shadow_l3e = (shadow_l3e_t *)&v->arch.paging.shadow.l3table;
3307 guest_l3e = (guest_l3e_t *)&v->arch.paging.shadow.gl3e;
3309 #endif /* GUEST_PAGING_LEVELS */
3311 /* Choose where to write the entries, using linear maps if possible */
3312 if ( shadow_mode_external(d) )
3314 if ( v == current )
3316 /* From the monitor tables, it's safe to use linear maps
3317 * to update monitor l2s */
3318 l2e = __linear_l2_table + (3 * L2_PAGETABLE_ENTRIES);
3320 else
3322 /* Map the monitor table's high l2 */
3323 l3_pgentry_t *l3e;
3324 l3e = sh_map_domain_page(
3325 pagetable_get_mfn(v->arch.monitor_table));
3326 ASSERT(l3e_get_flags(l3e[3]) & _PAGE_PRESENT);
3327 l2e = sh_map_domain_page(_mfn(l3e_get_pfn(l3e[3])));
3328 unmap_l2e = 1;
3329 sh_unmap_domain_page(l3e);
3332 else
3334 /* Map the shadow table's high l2 */
3335 ASSERT(shadow_l3e_get_flags(shadow_l3e[3]) & _PAGE_PRESENT);
3336 l2e = sh_map_domain_page(shadow_l3e_get_mfn(shadow_l3e[3]));
3337 unmap_l2e = 1;
3340 /* Write linear mapping of guest (only in PV, and only when
3341 * not translated). */
3342 if ( !shadow_mode_translate(d) )
3344 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3346 new_l2e =
3347 ((shadow_l3e_get_flags(guest_l3e[i]) & _PAGE_PRESENT)
3348 ? l2e_from_pfn(mfn_x(shadow_l3e_get_mfn(guest_l3e[i])),
3349 __PAGE_HYPERVISOR)
3350 : l2e_empty());
3351 safe_write_entry(
3352 &l2e[l2_table_offset(LINEAR_PT_VIRT_START) + i],
3353 &new_l2e);
3357 /* Write linear mapping of shadow. */
3358 for ( i = 0; i < SHADOW_L3_PAGETABLE_ENTRIES; i++ )
3360 new_l2e = (shadow_l3e_get_flags(shadow_l3e[i]) & _PAGE_PRESENT)
3361 ? l2e_from_pfn(mfn_x(shadow_l3e_get_mfn(shadow_l3e[i])),
3362 __PAGE_HYPERVISOR)
3363 : l2e_empty();
3364 safe_write_entry(
3365 &l2e[l2_table_offset(SH_LINEAR_PT_VIRT_START) + i],
3366 &new_l2e);
3369 if ( unmap_l2e )
3370 sh_unmap_domain_page(l2e);
3373 #elif CONFIG_PAGING_LEVELS == 2
3375 /* For PV, one l2e points at the guest l2, one points at the shadow
3376 * l2. No maintenance required.
3377 * For HVM, just need to update the l2e that points to the shadow l2. */
3379 if ( shadow_mode_external(d) )
3381 /* Use the linear map if we can; otherwise make a new mapping */
3382 if ( v == current )
3384 __linear_l2_table[l2_linear_offset(SH_LINEAR_PT_VIRT_START)] =
3385 l2e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3386 __PAGE_HYPERVISOR);
3388 else
3390 l2_pgentry_t *ml2e;
3391 ml2e = sh_map_domain_page(pagetable_get_mfn(v->arch.monitor_table));
3392 ml2e[l2_table_offset(SH_LINEAR_PT_VIRT_START)] =
3393 l2e_from_pfn(pagetable_get_pfn(v->arch.shadow_table[0]),
3394 __PAGE_HYPERVISOR);
3395 sh_unmap_domain_page(ml2e);
3399 #else
3400 #error this should not happen
3401 #endif
3405 /* Removes vcpu->arch.paging.shadow.guest_vtable and vcpu->arch.shadow_table[].
3406 * Does all appropriate management/bookkeeping/refcounting/etc...
3407 */
3408 static void
3409 sh_detach_old_tables(struct vcpu *v)
3411 mfn_t smfn;
3412 int i = 0;
3414 ////
3415 //// vcpu->arch.paging.shadow.guest_vtable
3416 ////
3418 #if GUEST_PAGING_LEVELS == 3
3419 /* PAE guests don't have a mapping of the guest top-level table */
3420 ASSERT(v->arch.paging.shadow.guest_vtable == NULL);
3421 #else
3422 if ( v->arch.paging.shadow.guest_vtable )
3424 struct domain *d = v->domain;
3425 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
3426 sh_unmap_domain_page_global(v->arch.paging.shadow.guest_vtable);
3427 v->arch.paging.shadow.guest_vtable = NULL;
3429 #endif
3432 ////
3433 //// vcpu->arch.shadow_table[]
3434 ////
3436 #if GUEST_PAGING_LEVELS == 3
3437 /* PAE guests have four shadow_table entries */
3438 for ( i = 0 ; i < 4 ; i++ )
3439 #endif
3441 smfn = pagetable_get_mfn(v->arch.shadow_table[i]);
3442 if ( mfn_x(smfn) )
3443 sh_put_ref(v, smfn, 0);
3444 v->arch.shadow_table[i] = pagetable_null();
3448 /* Set up the top-level shadow and install it in slot 'slot' of shadow_table */
3449 static void
3450 sh_set_toplevel_shadow(struct vcpu *v,
3451 int slot,
3452 mfn_t gmfn,
3453 unsigned int root_type)
3455 mfn_t smfn;
3456 pagetable_t old_entry, new_entry;
3458 struct domain *d = v->domain;
3460 /* Remember the old contents of this slot */
3461 old_entry = v->arch.shadow_table[slot];
3463 /* Now figure out the new contents: is this a valid guest MFN? */
3464 if ( !mfn_valid(gmfn) )
3466 new_entry = pagetable_null();
3467 goto install_new_entry;
3470 /* Guest mfn is valid: shadow it and install the shadow */
3471 smfn = get_shadow_status(v, gmfn, root_type);
3472 if ( !mfn_valid(smfn) )
3474 /* Make sure there's enough free shadow memory. */
3475 shadow_prealloc(d, root_type, 1);
3476 /* Shadow the page. */
3477 smfn = sh_make_shadow(v, gmfn, root_type);
3479 ASSERT(mfn_valid(smfn));
3481 #if SHADOW_OPTIMIZATIONS & SHOPT_EARLY_UNSHADOW
3482 /* Once again OK to unhook entries from this table if we see fork/exit */
3483 ASSERT(sh_mfn_is_a_page_table(gmfn));
3484 mfn_to_page(gmfn)->shadow_flags &= ~SHF_unhooked_mappings;
3485 #endif
3487 /* Pin the shadow and put it (back) on the list of pinned shadows */
3488 if ( sh_pin(v, smfn) == 0 )
3490 SHADOW_ERROR("can't pin %#lx as toplevel shadow\n", mfn_x(smfn));
3491 domain_crash(v->domain);
3494 /* Take a ref to this page: it will be released in sh_detach_old_tables()
3495 * or the next call to set_toplevel_shadow() */
3496 if ( !sh_get_ref(v, smfn, 0) )
3498 SHADOW_ERROR("can't install %#lx as toplevel shadow\n", mfn_x(smfn));
3499 domain_crash(v->domain);
3502 new_entry = pagetable_from_mfn(smfn);
3504 install_new_entry:
3505 /* Done. Install it */
3506 SHADOW_PRINTK("%u/%u [%u] gmfn %#"PRI_mfn" smfn %#"PRI_mfn"\n",
3507 GUEST_PAGING_LEVELS, SHADOW_PAGING_LEVELS, slot,
3508 mfn_x(gmfn), mfn_x(pagetable_get_mfn(new_entry)));
3509 v->arch.shadow_table[slot] = new_entry;
3511 /* Decrement the refcount of the old contents of this slot */
3512 if ( !pagetable_is_null(old_entry) )
3513 sh_put_ref(v, pagetable_get_mfn(old_entry), 0);
3517 static void
3518 sh_update_cr3(struct vcpu *v, int do_locking)
3519 /* Updates vcpu->arch.cr3 after the guest has changed CR3.
3520 * Paravirtual guests should set v->arch.guest_table (and guest_table_user,
3521 * if appropriate).
3522 * HVM guests should also make sure hvm_get_guest_cntl_reg(v, 3) works;
3523 * this function will call hvm_update_guest_cr(v, 3) to tell them where the
3524 * shadow tables are.
3525 * If do_locking != 0, assume we are being called from outside the
3526 * shadow code, and must take and release the shadow lock; otherwise
3527 * that is the caller's responsibility.
3528 */
3530 struct domain *d = v->domain;
3531 mfn_t gmfn;
3532 #if GUEST_PAGING_LEVELS == 3
3533 guest_l3e_t *gl3e;
3534 u32 guest_idx=0;
3535 int i;
3536 #endif
3538 /* Don't do anything on an uninitialised vcpu */
3539 if ( !is_hvm_domain(d) && !v->is_initialised )
3541 ASSERT(v->arch.cr3 == 0);
3542 return;
3545 if ( do_locking ) shadow_lock(v->domain);
3547 ASSERT(shadow_locked_by_me(v->domain));
3548 ASSERT(v->arch.paging.mode);
3550 ////
3551 //// vcpu->arch.guest_table is already set
3552 ////
3554 #ifndef NDEBUG
3555 /* Double-check that the HVM code has sent us a sane guest_table */
3556 if ( is_hvm_domain(d) )
3558 ASSERT(shadow_mode_external(d));
3559 if ( hvm_paging_enabled(v) )
3560 ASSERT(pagetable_get_pfn(v->arch.guest_table));
3561 else
3562 ASSERT(v->arch.guest_table.pfn
3563 == d->arch.paging.shadow.unpaged_pagetable.pfn);
3565 #endif
3567 SHADOW_PRINTK("d=%u v=%u guest_table=%05lx\n",
3568 d->domain_id, v->vcpu_id,
3569 (unsigned long)pagetable_get_pfn(v->arch.guest_table));
3571 #if GUEST_PAGING_LEVELS == 4
3572 if ( !(v->arch.flags & TF_kernel_mode) && !is_pv_32on64_vcpu(v) )
3573 gmfn = pagetable_get_mfn(v->arch.guest_table_user);
3574 else
3575 #endif
3576 gmfn = pagetable_get_mfn(v->arch.guest_table);
3579 ////
3580 //// vcpu->arch.paging.shadow.guest_vtable
3581 ////
3582 #if GUEST_PAGING_LEVELS == 4
3583 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
3585 if ( v->arch.paging.shadow.guest_vtable )
3586 sh_unmap_domain_page_global(v->arch.paging.shadow.guest_vtable);
3587 v->arch.paging.shadow.guest_vtable = sh_map_domain_page_global(gmfn);
3588 /* PAGING_LEVELS==4 implies 64-bit, which means that
3589 * map_domain_page_global can't fail */
3590 BUG_ON(v->arch.paging.shadow.guest_vtable == NULL);
3592 else
3593 v->arch.paging.shadow.guest_vtable = __linear_l4_table;
3594 #elif GUEST_PAGING_LEVELS == 3
3595 /* On PAE guests we don't use a mapping of the guest's own top-level
3596 * table. We cache the current state of that table and shadow that,
3597 * until the next CR3 write makes us refresh our cache. */
3598 ASSERT(v->arch.paging.shadow.guest_vtable == NULL);
3600 if ( shadow_mode_external(d) )
3601 /* Find where in the page the l3 table is */
3602 guest_idx = guest_index((void *)v->arch.hvm_vcpu.guest_cr[3]);
3603 else
3604 /* PV guest: l3 is at the start of a page */
3605 guest_idx = 0;
3607 // Ignore the low 2 bits of guest_idx -- they are really just
3608 // cache control.
3609 guest_idx &= ~3;
3611 gl3e = ((guest_l3e_t *)sh_map_domain_page(gmfn)) + guest_idx;
3612 for ( i = 0; i < 4 ; i++ )
3613 v->arch.paging.shadow.gl3e[i] = gl3e[i];
3614 sh_unmap_domain_page(gl3e);
3615 #elif GUEST_PAGING_LEVELS == 2
3616 if ( shadow_mode_external(d) || shadow_mode_translate(d) )
3618 if ( v->arch.paging.shadow.guest_vtable )
3619 sh_unmap_domain_page_global(v->arch.paging.shadow.guest_vtable);
3620 v->arch.paging.shadow.guest_vtable = sh_map_domain_page_global(gmfn);
3621 /* Does this really need map_domain_page_global? Handle the
3622 * error properly if so. */
3623 BUG_ON(v->arch.paging.shadow.guest_vtable == NULL); /* XXX */
3625 else
3626 v->arch.paging.shadow.guest_vtable = __linear_l2_table;
3627 #else
3628 #error this should never happen
3629 #endif
3631 #if 0
3632 printk("%s %s %d gmfn=%05lx shadow.guest_vtable=%p\n",
3633 __func__, __FILE__, __LINE__, gmfn, v->arch.paging.shadow.guest_vtable);
3634 #endif
3636 ////
3637 //// vcpu->arch.shadow_table[]
3638 ////
3640 /* We revoke write access to the new guest toplevel page(s) before we
3641 * replace the old shadow pagetable(s), so that we can safely use the
3642 * (old) shadow linear maps in the writeable mapping heuristics. */
3643 #if GUEST_PAGING_LEVELS == 2
3644 if ( sh_remove_write_access(v, gmfn, 2, 0) != 0 )
3645 flush_tlb_mask(v->domain->domain_dirty_cpumask);
3646 sh_set_toplevel_shadow(v, 0, gmfn, SH_type_l2_shadow);
3647 #elif GUEST_PAGING_LEVELS == 3
3648 /* PAE guests have four shadow_table entries, based on the
3649 * current values of the guest's four l3es. */
3651 int flush = 0;
3652 gfn_t gl2gfn;
3653 mfn_t gl2mfn;
3654 p2m_type_t p2mt;
3655 guest_l3e_t *gl3e = (guest_l3e_t*)&v->arch.paging.shadow.gl3e;
3656 /* First, make all four entries read-only. */
3657 for ( i = 0; i < 4; i++ )
3659 if ( guest_l3e_get_flags(gl3e[i]) & _PAGE_PRESENT )
3661 gl2gfn = guest_l3e_get_gfn(gl3e[i]);
3662 gl2mfn = gfn_to_mfn(d, gl2gfn, &p2mt);
3663 if ( p2m_is_ram(p2mt) )
3664 flush |= sh_remove_write_access(v, gl2mfn, 2, 0);
3667 if ( flush )
3668 flush_tlb_mask(v->domain->domain_dirty_cpumask);
3669 /* Now install the new shadows. */
3670 for ( i = 0; i < 4; i++ )
3672 if ( guest_l3e_get_flags(gl3e[i]) & _PAGE_PRESENT )
3674 gl2gfn = guest_l3e_get_gfn(gl3e[i]);
3675 gl2mfn = gfn_to_mfn(d, gl2gfn, &p2mt);
3676 if ( p2m_is_ram(p2mt) )
3677 sh_set_toplevel_shadow(v, i, gl2mfn, (i == 3)
3678 ? SH_type_l2h_shadow
3679 : SH_type_l2_shadow);
3680 else
3681 sh_set_toplevel_shadow(v, i, _mfn(INVALID_MFN), 0);
3683 else
3684 sh_set_toplevel_shadow(v, i, _mfn(INVALID_MFN), 0);
3687 #elif GUEST_PAGING_LEVELS == 4
3688 if ( sh_remove_write_access(v, gmfn, 4, 0) != 0 )
3689 flush_tlb_mask(v->domain->domain_dirty_cpumask);
3690 sh_set_toplevel_shadow(v, 0, gmfn, SH_type_l4_shadow);
3691 #else
3692 #error This should never happen
3693 #endif
3695 #if (CONFIG_PAGING_LEVELS == 3) && (GUEST_PAGING_LEVELS == 3)
3696 #endif
3698 ///
3699 /// v->arch.paging.shadow.l3table
3700 ///
3701 #if SHADOW_PAGING_LEVELS == 3
3703 mfn_t smfn;
3704 int i;
3705 for ( i = 0; i < 4; i++ )
3707 #if GUEST_PAGING_LEVELS == 2
3708 /* 2-on-3: make a PAE l3 that points at the four-page l2 */
3709 smfn = _mfn(pagetable_get_pfn(v->arch.shadow_table[0]) + i);
3710 #else
3711 /* 3-on-3: make a PAE l3 that points at the four l2 pages */
3712 smfn = pagetable_get_mfn(v->arch.shadow_table[i]);
3713 #endif
3714 v->arch.paging.shadow.l3table[i] =
3715 (mfn_x(smfn) == 0)
3716 ? shadow_l3e_empty()
3717 : shadow_l3e_from_mfn(smfn, _PAGE_PRESENT);
3720 #endif /* SHADOW_PAGING_LEVELS == 3 */
3723 ///
3724 /// v->arch.cr3
3725 ///
3726 if ( shadow_mode_external(d) )
3728 make_cr3(v, pagetable_get_pfn(v->arch.monitor_table));
3730 else // not shadow_mode_external...
3732 /* We don't support PV except guest == shadow == config levels */
3733 BUG_ON(GUEST_PAGING_LEVELS != SHADOW_PAGING_LEVELS);
3734 #if SHADOW_PAGING_LEVELS == 3
3735 /* 2-on-3 or 3-on-3: Use the PAE shadow l3 table we just fabricated.
3736 * Don't use make_cr3 because (a) we know it's below 4GB, and
3737 * (b) it's not necessarily page-aligned, and make_cr3 takes a pfn */
3738 ASSERT(virt_to_maddr(&v->arch.paging.shadow.l3table) <= 0xffffffe0ULL);
3739 v->arch.cr3 = virt_to_maddr(&v->arch.paging.shadow.l3table);
3740 #else
3741 /* 2-on-2 or 4-on-4: Just use the shadow top-level directly */
3742 make_cr3(v, pagetable_get_pfn(v->arch.shadow_table[0]));
3743 #endif
3747 ///
3748 /// v->arch.hvm_vcpu.hw_cr[3]
3749 ///
3750 if ( shadow_mode_external(d) )
3752 ASSERT(is_hvm_domain(d));
3753 #if SHADOW_PAGING_LEVELS == 3
3754 /* 2-on-3 or 3-on-3: Use the PAE shadow l3 table we just fabricated */
3755 v->arch.hvm_vcpu.hw_cr[3] =
3756 virt_to_maddr(&v->arch.paging.shadow.l3table);
3757 #else
3758 /* 2-on-2 or 4-on-4: Just use the shadow top-level directly */
3759 v->arch.hvm_vcpu.hw_cr[3] =
3760 pagetable_get_paddr(v->arch.shadow_table[0]);
3761 #endif
3762 hvm_update_guest_cr(v, 3);
3765 /* Fix up the linear pagetable mappings */
3766 sh_update_linear_entries(v);
3768 #if (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB)
3769 /* No longer safe to use cached gva->gfn translations */
3770 vtlb_flush(v);
3771 #endif
3773 /* Release the lock, if we took it (otherwise it's the caller's problem) */
3774 if ( do_locking ) shadow_unlock(v->domain);
3778 /**************************************************************************/
3779 /* Functions to revoke guest rights */
3781 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
3782 static int sh_guess_wrmap(struct vcpu *v, unsigned long vaddr, mfn_t gmfn)
3783 /* Look up this vaddr in the current shadow and see if it's a writeable
3784 * mapping of this gmfn. If so, remove it. Returns 1 if it worked. */
3786 shadow_l1e_t sl1e, *sl1p;
3787 shadow_l2e_t *sl2p;
3788 #if SHADOW_PAGING_LEVELS >= 3
3789 shadow_l3e_t *sl3p;
3790 #if SHADOW_PAGING_LEVELS >= 4
3791 shadow_l4e_t *sl4p;
3792 #endif
3793 #endif
3794 mfn_t sl1mfn;
3795 int r;
3797 /* Carefully look in the shadow linear map for the l1e we expect */
3798 #if SHADOW_PAGING_LEVELS >= 4
3799 sl4p = sh_linear_l4_table(v) + shadow_l4_linear_offset(vaddr);
3800 if ( !(shadow_l4e_get_flags(*sl4p) & _PAGE_PRESENT) )
3801 return 0;
3802 sl3p = sh_linear_l3_table(v) + shadow_l3_linear_offset(vaddr);
3803 if ( !(shadow_l3e_get_flags(*sl3p) & _PAGE_PRESENT) )
3804 return 0;
3805 #elif SHADOW_PAGING_LEVELS == 3
3806 sl3p = ((shadow_l3e_t *) v->arch.paging.shadow.l3table)
3807 + shadow_l3_linear_offset(vaddr);
3808 if ( !(shadow_l3e_get_flags(*sl3p) & _PAGE_PRESENT) )
3809 return 0;
3810 #endif
3811 sl2p = sh_linear_l2_table(v) + shadow_l2_linear_offset(vaddr);
3812 if ( !(shadow_l2e_get_flags(*sl2p) & _PAGE_PRESENT) )
3813 return 0;
3814 sl1p = sh_linear_l1_table(v) + shadow_l1_linear_offset(vaddr);
3815 sl1e = *sl1p;
3816 if ( ((shadow_l1e_get_flags(sl1e) & (_PAGE_PRESENT|_PAGE_RW))
3817 != (_PAGE_PRESENT|_PAGE_RW))
3818 || (mfn_x(shadow_l1e_get_mfn(sl1e)) != mfn_x(gmfn)) )
3819 return 0;
3821 /* Found it! Need to remove its write permissions. */
3822 sl1mfn = shadow_l2e_get_mfn(*sl2p);
3823 sl1e = shadow_l1e_remove_flags(sl1e, _PAGE_RW);
3824 r = shadow_set_l1e(v, sl1p, sl1e, sl1mfn);
3825 ASSERT( !(r & SHADOW_SET_ERROR) );
3826 return 1;
3828 #endif
3830 int sh_rm_write_access_from_l1(struct vcpu *v, mfn_t sl1mfn,
3831 mfn_t readonly_mfn)
3832 /* Excises all writeable mappings to readonly_mfn from this l1 shadow table */
3834 shadow_l1e_t *sl1e;
3835 int done = 0;
3836 int flags;
3837 mfn_t base_sl1mfn = sl1mfn; /* Because sl1mfn changes in the foreach */
3839 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done,
3841 flags = shadow_l1e_get_flags(*sl1e);
3842 if ( (flags & _PAGE_PRESENT)
3843 && (flags & _PAGE_RW)
3844 && (mfn_x(shadow_l1e_get_mfn(*sl1e)) == mfn_x(readonly_mfn)) )
3846 shadow_l1e_t ro_sl1e = shadow_l1e_remove_flags(*sl1e, _PAGE_RW);
3847 (void) shadow_set_l1e(v, sl1e, ro_sl1e, sl1mfn);
3848 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
3849 /* Remember the last shadow that we shot a writeable mapping in */
3850 v->arch.paging.shadow.last_writeable_pte_smfn = mfn_x(base_sl1mfn);
3851 #endif
3852 if ( (mfn_to_page(readonly_mfn)->u.inuse.type_info
3853 & PGT_count_mask) == 0 )
3854 /* This breaks us cleanly out of the FOREACH macro */
3855 done = 1;
3857 });
3858 return done;
3862 int sh_rm_mappings_from_l1(struct vcpu *v, mfn_t sl1mfn, mfn_t target_mfn)
3863 /* Excises all mappings to guest frame from this shadow l1 table */
3865 shadow_l1e_t *sl1e;
3866 int done = 0;
3867 int flags;
3869 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done,
3871 flags = shadow_l1e_get_flags(*sl1e);
3872 if ( (flags & _PAGE_PRESENT)
3873 && (mfn_x(shadow_l1e_get_mfn(*sl1e)) == mfn_x(target_mfn)) )
3875 (void) shadow_set_l1e(v, sl1e, shadow_l1e_empty(), sl1mfn);
3876 if ( (mfn_to_page(target_mfn)->count_info & PGC_count_mask) == 0 )
3877 /* This breaks us cleanly out of the FOREACH macro */
3878 done = 1;
3880 });
3881 return done;
3884 /**************************************************************************/
3885 /* Functions to excise all pointers to shadows from higher-level shadows. */
3887 void sh_clear_shadow_entry(struct vcpu *v, void *ep, mfn_t smfn)
3888 /* Blank out a single shadow entry */
3890 switch ( mfn_to_shadow_page(smfn)->type )
3892 case SH_type_l1_shadow:
3893 (void) shadow_set_l1e(v, ep, shadow_l1e_empty(), smfn); break;
3894 case SH_type_l2_shadow:
3895 #if GUEST_PAGING_LEVELS >= 3
3896 case SH_type_l2h_shadow:
3897 #endif
3898 (void) shadow_set_l2e(v, ep, shadow_l2e_empty(), smfn); break;
3899 #if GUEST_PAGING_LEVELS >= 4
3900 case SH_type_l3_shadow:
3901 (void) shadow_set_l3e(v, ep, shadow_l3e_empty(), smfn); break;
3902 case SH_type_l4_shadow:
3903 (void) shadow_set_l4e(v, ep, shadow_l4e_empty(), smfn); break;
3904 #endif
3905 default: BUG(); /* Called with the wrong kind of shadow. */
3909 int sh_remove_l1_shadow(struct vcpu *v, mfn_t sl2mfn, mfn_t sl1mfn)
3910 /* Remove all mappings of this l1 shadow from this l2 shadow */
3912 shadow_l2e_t *sl2e;
3913 int done = 0;
3914 int flags;
3916 SHADOW_FOREACH_L2E(sl2mfn, sl2e, 0, done, v->domain,
3918 flags = shadow_l2e_get_flags(*sl2e);
3919 if ( (flags & _PAGE_PRESENT)
3920 && (mfn_x(shadow_l2e_get_mfn(*sl2e)) == mfn_x(sl1mfn)) )
3922 (void) shadow_set_l2e(v, sl2e, shadow_l2e_empty(), sl2mfn);
3923 if ( mfn_to_shadow_page(sl1mfn)->type == 0 )
3924 /* This breaks us cleanly out of the FOREACH macro */
3925 done = 1;
3927 });
3928 return done;
3931 #if GUEST_PAGING_LEVELS >= 4
3932 int sh_remove_l2_shadow(struct vcpu *v, mfn_t sl3mfn, mfn_t sl2mfn)
3933 /* Remove all mappings of this l2 shadow from this l3 shadow */
3935 shadow_l3e_t *sl3e;
3936 int done = 0;
3937 int flags;
3939 SHADOW_FOREACH_L3E(sl3mfn, sl3e, 0, done,
3941 flags = shadow_l3e_get_flags(*sl3e);
3942 if ( (flags & _PAGE_PRESENT)
3943 && (mfn_x(shadow_l3e_get_mfn(*sl3e)) == mfn_x(sl2mfn)) )
3945 (void) shadow_set_l3e(v, sl3e, shadow_l3e_empty(), sl3mfn);
3946 if ( mfn_to_shadow_page(sl2mfn)->type == 0 )
3947 /* This breaks us cleanly out of the FOREACH macro */
3948 done = 1;
3950 });
3951 return done;
3954 int sh_remove_l3_shadow(struct vcpu *v, mfn_t sl4mfn, mfn_t sl3mfn)
3955 /* Remove all mappings of this l3 shadow from this l4 shadow */
3957 shadow_l4e_t *sl4e;
3958 int done = 0;
3959 int flags;
3961 SHADOW_FOREACH_L4E(sl4mfn, sl4e, 0, done, v->domain,
3963 flags = shadow_l4e_get_flags(*sl4e);
3964 if ( (flags & _PAGE_PRESENT)
3965 && (mfn_x(shadow_l4e_get_mfn(*sl4e)) == mfn_x(sl3mfn)) )
3967 (void) shadow_set_l4e(v, sl4e, shadow_l4e_empty(), sl4mfn);
3968 if ( mfn_to_shadow_page(sl3mfn)->type == 0 )
3969 /* This breaks us cleanly out of the FOREACH macro */
3970 done = 1;
3972 });
3973 return done;
3975 #endif /* 64bit guest */
3977 /**************************************************************************/
3978 /* Handling HVM guest writes to pagetables */
3980 /* Check that the user is allowed to perform this write.
3981 * Returns a mapped pointer to write to, and the mfn it's on,
3982 * or NULL for error. */
3983 static inline void * emulate_map_dest(struct vcpu *v,
3984 unsigned long vaddr,
3985 struct sh_emulate_ctxt *sh_ctxt,
3986 mfn_t *mfnp)
3988 walk_t gw;
3989 u32 flags, errcode;
3990 gfn_t gfn;
3991 mfn_t mfn;
3992 p2m_type_t p2mt;
3994 /* We don't emulate user-mode writes to page tables */
3995 if ( ring_3(sh_ctxt->ctxt.regs) )
3996 return NULL;
3998 #if (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB)
3999 /* Try the virtual TLB first */
4001 struct shadow_vtlb t = {0};
4002 if ( vtlb_lookup(v, vaddr, &t)
4003 && ((t.flags & (_PAGE_PRESENT|_PAGE_RW))
4004 == (_PAGE_PRESENT|_PAGE_RW)) )
4006 flags = t.flags;
4007 gfn = _gfn(t.frame_number);
4009 else
4011 /* Need to do the full lookup, just in case permissions
4012 * have increased since we cached this entry */
4014 #endif /* (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB) */
4016 /* Walk the guest pagetables */
4017 guest_walk_tables(v, vaddr, &gw, 1);
4018 flags = accumulate_guest_flags(v, &gw);
4019 gfn = guest_l1e_get_gfn(gw.eff_l1e);
4020 sh_audit_gw(v, &gw);
4021 unmap_walk(v, &gw);
4023 #if (SHADOW_OPTIMIZATIONS & SHOPT_VIRTUAL_TLB)
4024 /* Remember this translation for next time */
4025 t.page_number = vaddr >> PAGE_SHIFT;
4026 t.frame_number = gfn_x(gfn);
4027 t.flags = flags;
4028 vtlb_insert(v, t);
4031 #endif
4033 errcode = PFEC_write_access;
4034 if ( !(flags & _PAGE_PRESENT) )
4035 goto page_fault;
4037 errcode |= PFEC_page_present;
4038 if ( !(flags & _PAGE_RW) )
4039 goto page_fault;
4041 mfn = gfn_to_mfn(v->domain, gfn, &p2mt);
4042 if ( p2m_is_ram(p2mt) )
4044 ASSERT(mfn_valid(mfn));
4045 *mfnp = mfn;
4046 v->arch.paging.last_write_was_pt = !!sh_mfn_is_a_page_table(mfn);
4047 return sh_map_domain_page(mfn) + (vaddr & ~PAGE_MASK);
4049 else
4050 return NULL;
4052 page_fault:
4053 if ( is_hvm_vcpu(v) )
4054 hvm_inject_exception(TRAP_page_fault, errcode, vaddr);
4055 else
4056 propagate_page_fault(vaddr, errcode);
4057 return NULL;
4060 static int safe_not_to_verify_write(mfn_t gmfn, void *dst, void *src,
4061 int bytes)
4063 #if (SHADOW_OPTIMIZATIONS & SHOPT_SKIP_VERIFY)
4064 struct page_info *pg = mfn_to_page(gmfn);
4065 if ( !(pg->shadow_flags & SHF_32)
4066 && ((unsigned long)dst & 7) == 0 )
4068 /* Not shadowed 32-bit: aligned 64-bit writes that leave the
4069 * present bit unset are safe to ignore. */
4070 if ( (*(u64*)src & _PAGE_PRESENT) == 0
4071 && (*(u64*)dst & _PAGE_PRESENT) == 0 )
4072 return 1;
4074 else if ( !(pg->shadow_flags & (SHF_PAE|SHF_64))
4075 && ((unsigned long)dst & 3) == 0 )
4077 /* Not shadowed PAE/64-bit: aligned 32-bit writes that leave the
4078 * present bit unset are safe to ignore. */
4079 if ( (*(u32*)src & _PAGE_PRESENT) == 0
4080 && (*(u32*)dst & _PAGE_PRESENT) == 0 )
4081 return 1;
4083 #endif
4084 return 0;
4088 int
4089 sh_x86_emulate_write(struct vcpu *v, unsigned long vaddr, void *src,
4090 u32 bytes, struct sh_emulate_ctxt *sh_ctxt)
4092 mfn_t mfn;
4093 void *addr;
4094 int skip;
4096 if ( vaddr & (bytes-1) )
4097 return X86EMUL_UNHANDLEABLE;
4099 ASSERT(((vaddr & ~PAGE_MASK) + bytes) <= PAGE_SIZE);
4100 shadow_lock(v->domain);
4102 addr = emulate_map_dest(v, vaddr, sh_ctxt, &mfn);
4103 if ( addr == NULL )
4105 shadow_unlock(v->domain);
4106 return X86EMUL_EXCEPTION;
4109 skip = safe_not_to_verify_write(mfn, addr, src, bytes);
4110 memcpy(addr, src, bytes);
4111 if ( !skip ) sh_validate_guest_pt_write(v, mfn, addr, bytes);
4113 /* If we are writing zeros to this page, might want to unshadow */
4114 if ( likely(bytes >= 4) && (*(u32 *)addr == 0) && is_lo_pte(vaddr) )
4115 check_for_early_unshadow(v, mfn);
4116 else
4117 reset_early_unshadow(v);
4119 paging_mark_dirty(v->domain, mfn_x(mfn));
4121 sh_unmap_domain_page(addr);
4122 shadow_audit_tables(v);
4123 shadow_unlock(v->domain);
4124 return X86EMUL_OKAY;
4127 int
4128 sh_x86_emulate_cmpxchg(struct vcpu *v, unsigned long vaddr,
4129 unsigned long old, unsigned long new,
4130 unsigned int bytes, struct sh_emulate_ctxt *sh_ctxt)
4132 mfn_t mfn;
4133 void *addr;
4134 unsigned long prev;
4135 int rv = X86EMUL_OKAY, skip;
4137 ASSERT(bytes <= sizeof(unsigned long));
4138 shadow_lock(v->domain);
4140 if ( vaddr & (bytes-1) )
4141 return X86EMUL_UNHANDLEABLE;
4143 addr = emulate_map_dest(v, vaddr, sh_ctxt, &mfn);
4144 if ( addr == NULL )
4146 shadow_unlock(v->domain);
4147 return X86EMUL_EXCEPTION;
4150 skip = safe_not_to_verify_write(mfn, &new, &old, bytes);
4152 switch ( bytes )
4154 case 1: prev = cmpxchg(((u8 *)addr), old, new); break;
4155 case 2: prev = cmpxchg(((u16 *)addr), old, new); break;
4156 case 4: prev = cmpxchg(((u32 *)addr), old, new); break;
4157 case 8: prev = cmpxchg(((u64 *)addr), old, new); break;
4158 default:
4159 SHADOW_PRINTK("cmpxchg of size %i is not supported\n", bytes);
4160 prev = ~old;
4163 if ( prev == old )
4165 if ( !skip ) sh_validate_guest_pt_write(v, mfn, addr, bytes);
4167 else
4168 rv = X86EMUL_CMPXCHG_FAILED;
4170 SHADOW_DEBUG(EMULATE, "va %#lx was %#lx expected %#lx"
4171 " wanted %#lx now %#lx bytes %u\n",
4172 vaddr, prev, old, new, *(unsigned long *)addr, bytes);
4174 /* If we are writing zeros to this page, might want to unshadow */
4175 if ( likely(bytes >= 4) && (*(u32 *)addr == 0) && is_lo_pte(vaddr) )
4176 check_for_early_unshadow(v, mfn);
4177 else
4178 reset_early_unshadow(v);
4180 paging_mark_dirty(v->domain, mfn_x(mfn));
4182 sh_unmap_domain_page(addr);
4183 shadow_audit_tables(v);
4184 shadow_unlock(v->domain);
4185 return rv;
4188 int
4189 sh_x86_emulate_cmpxchg8b(struct vcpu *v, unsigned long vaddr,
4190 unsigned long old_lo, unsigned long old_hi,
4191 unsigned long new_lo, unsigned long new_hi,
4192 struct sh_emulate_ctxt *sh_ctxt)
4194 mfn_t mfn;
4195 void *addr;
4196 u64 old, new, prev;
4197 int rv = X86EMUL_OKAY, skip;
4199 if ( vaddr & 7 )
4200 return X86EMUL_UNHANDLEABLE;
4202 shadow_lock(v->domain);
4204 addr = emulate_map_dest(v, vaddr, sh_ctxt, &mfn);
4205 if ( addr == NULL )
4207 shadow_unlock(v->domain);
4208 return X86EMUL_EXCEPTION;
4211 old = (((u64) old_hi) << 32) | (u64) old_lo;
4212 new = (((u64) new_hi) << 32) | (u64) new_lo;
4213 skip = safe_not_to_verify_write(mfn, &new, &old, 8);
4214 prev = cmpxchg(((u64 *)addr), old, new);
4216 if ( prev == old )
4218 if ( !skip ) sh_validate_guest_pt_write(v, mfn, addr, 8);
4220 else
4221 rv = X86EMUL_CMPXCHG_FAILED;
4223 /* If we are writing zeros to this page, might want to unshadow */
4224 if ( *(u32 *)addr == 0 )
4225 check_for_early_unshadow(v, mfn);
4226 else
4227 reset_early_unshadow(v);
4229 paging_mark_dirty(v->domain, mfn_x(mfn));
4231 sh_unmap_domain_page(addr);
4232 shadow_audit_tables(v);
4233 shadow_unlock(v->domain);
4234 return rv;
4238 /**************************************************************************/
4239 /* Audit tools */
4241 #if SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES
4243 #define AUDIT_FAIL(_level, _fmt, _a...) do { \
4244 printk("Shadow %u-on-%u audit failed at level %i, index %i\n" \
4245 "gl" #_level "mfn = %" PRI_mfn \
4246 " sl" #_level "mfn = %" PRI_mfn \
4247 " &gl" #_level "e = %p &sl" #_level "e = %p" \
4248 " gl" #_level "e = %" SH_PRI_gpte \
4249 " sl" #_level "e = %" SH_PRI_pte "\nError: " _fmt "\n", \
4250 GUEST_PAGING_LEVELS, SHADOW_PAGING_LEVELS, \
4251 _level, guest_index(gl ## _level ## e), \
4252 mfn_x(gl ## _level ## mfn), mfn_x(sl ## _level ## mfn), \
4253 gl ## _level ## e, sl ## _level ## e, \
4254 gl ## _level ## e->l ## _level, sl ## _level ## e->l ## _level, \
4255 ##_a); \
4256 BUG(); \
4257 done = 1; \
4258 } while (0)
4261 static char * sh_audit_flags(struct vcpu *v, int level,
4262 int gflags, int sflags)
4263 /* Common code for auditing flag bits */
4265 if ( (sflags & _PAGE_PRESENT) && !(gflags & _PAGE_PRESENT) )
4266 return "shadow is present but guest is not present";
4267 if ( (sflags & _PAGE_GLOBAL) && !is_hvm_vcpu(v) )
4268 return "global bit set in PV shadow";
4269 if ( level == 2 && (sflags & _PAGE_PSE) )
4270 return "PS bit set in shadow";
4271 #if SHADOW_PAGING_LEVELS == 3
4272 if ( level == 3 ) return NULL; /* All the other bits are blank in PAEl3 */
4273 #endif
4274 if ( (sflags & _PAGE_PRESENT) && !(gflags & _PAGE_ACCESSED) )
4275 return "accessed bit not propagated";
4276 if ( (level == 1 || (level == 2 && (gflags & _PAGE_PSE)))
4277 && ((sflags & _PAGE_RW) && !(gflags & _PAGE_DIRTY)) )
4278 return "dirty bit not propagated";
4279 if ( (sflags & _PAGE_USER) != (gflags & _PAGE_USER) )
4280 return "user/supervisor bit does not match";
4281 if ( (sflags & _PAGE_NX_BIT) != (gflags & _PAGE_NX_BIT) )
4282 return "NX bit does not match";
4283 if ( (sflags & _PAGE_RW) && !(gflags & _PAGE_RW) )
4284 return "shadow grants write access but guest does not";
4285 return NULL;
4288 static inline mfn_t
4289 audit_gfn_to_mfn(struct vcpu *v, gfn_t gfn, mfn_t gmfn)
4290 /* Convert this gfn to an mfn in the manner appropriate for the
4291 * guest pagetable it's used in (gmfn) */
4293 p2m_type_t p2mt;
4294 if ( !shadow_mode_translate(v->domain) )
4295 return _mfn(gfn_x(gfn));
4297 if ( (mfn_to_page(gmfn)->u.inuse.type_info & PGT_type_mask)
4298 != PGT_writable_page )
4299 return _mfn(gfn_x(gfn)); /* This is a paging-disabled shadow */
4300 else
4301 return gfn_to_mfn(v->domain, gfn, &p2mt);
4305 int sh_audit_l1_table(struct vcpu *v, mfn_t sl1mfn, mfn_t x)
4307 guest_l1e_t *gl1e, *gp;
4308 shadow_l1e_t *sl1e;
4309 mfn_t mfn, gmfn, gl1mfn;
4310 gfn_t gfn;
4311 char *s;
4312 int done = 0;
4314 /* Follow the backpointer */
4315 gl1mfn = _mfn(mfn_to_shadow_page(sl1mfn)->backpointer);
4316 gl1e = gp = sh_map_domain_page(gl1mfn);
4317 SHADOW_FOREACH_L1E(sl1mfn, sl1e, &gl1e, done, {
4319 if ( sh_l1e_is_magic(*sl1e) )
4321 #if (SHADOW_OPTIMIZATIONS & SHOPT_FAST_FAULT_PATH) && SHADOW_PAGING_LEVELS > 2
4322 if ( sh_l1e_is_gnp(*sl1e) )
4324 if ( guest_l1e_get_flags(*gl1e) & _PAGE_PRESENT )
4325 AUDIT_FAIL(1, "shadow is GNP magic but guest is present");
4327 else
4329 ASSERT(sh_l1e_is_mmio(*sl1e));
4330 gfn = sh_l1e_mmio_get_gfn(*sl1e);
4331 if ( gfn_x(gfn) != gfn_x(guest_l1e_get_gfn(*gl1e)) )
4332 AUDIT_FAIL(1, "shadow MMIO gfn is %" SH_PRI_gfn
4333 " but guest gfn is %" SH_PRI_gfn,
4334 gfn_x(gfn),
4335 gfn_x(guest_l1e_get_gfn(*gl1e)));
4337 #endif
4339 else
4341 s = sh_audit_flags(v, 1, guest_l1e_get_flags(*gl1e),
4342 shadow_l1e_get_flags(*sl1e));
4343 if ( s ) AUDIT_FAIL(1, "%s", s);
4345 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4347 gfn = guest_l1e_get_gfn(*gl1e);
4348 mfn = shadow_l1e_get_mfn(*sl1e);
4349 gmfn = audit_gfn_to_mfn(v, gfn, gl1mfn);
4350 if ( mfn_x(gmfn) != mfn_x(mfn) )
4351 AUDIT_FAIL(1, "bad translation: gfn %" SH_PRI_gfn
4352 " --> %" PRI_mfn " != mfn %" PRI_mfn,
4353 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
4356 });
4357 sh_unmap_domain_page(gp);
4358 return done;
4361 int sh_audit_fl1_table(struct vcpu *v, mfn_t sl1mfn, mfn_t x)
4363 guest_l1e_t *gl1e, e;
4364 shadow_l1e_t *sl1e;
4365 mfn_t gl1mfn = _mfn(INVALID_MFN);
4366 int f;
4367 int done = 0;
4369 /* fl1 has no useful backpointer: all we can check are flags */
4370 e = guest_l1e_from_gfn(_gfn(0), 0); gl1e = &e; /* Needed for macro */
4371 SHADOW_FOREACH_L1E(sl1mfn, sl1e, 0, done, {
4372 f = shadow_l1e_get_flags(*sl1e);
4373 f &= ~(_PAGE_AVAIL0|_PAGE_AVAIL1|_PAGE_AVAIL2);
4374 if ( !(f == 0
4375 || f == (_PAGE_PRESENT|_PAGE_USER|_PAGE_RW|
4376 _PAGE_ACCESSED|_PAGE_DIRTY)
4377 || f == (_PAGE_PRESENT|_PAGE_USER|_PAGE_ACCESSED|_PAGE_DIRTY)
4378 || sh_l1e_is_magic(*sl1e)) )
4379 AUDIT_FAIL(1, "fl1e has bad flags");
4380 });
4381 return 0;
4384 int sh_audit_l2_table(struct vcpu *v, mfn_t sl2mfn, mfn_t x)
4386 guest_l2e_t *gl2e, *gp;
4387 shadow_l2e_t *sl2e;
4388 mfn_t mfn, gmfn, gl2mfn;
4389 gfn_t gfn;
4390 char *s;
4391 int done = 0;
4393 /* Follow the backpointer */
4394 gl2mfn = _mfn(mfn_to_shadow_page(sl2mfn)->backpointer);
4395 gl2e = gp = sh_map_domain_page(gl2mfn);
4396 SHADOW_FOREACH_L2E(sl2mfn, sl2e, &gl2e, done, v->domain, {
4398 s = sh_audit_flags(v, 2, guest_l2e_get_flags(*gl2e),
4399 shadow_l2e_get_flags(*sl2e));
4400 if ( s ) AUDIT_FAIL(2, "%s", s);
4402 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4404 gfn = guest_l2e_get_gfn(*gl2e);
4405 mfn = shadow_l2e_get_mfn(*sl2e);
4406 gmfn = (guest_l2e_get_flags(*gl2e) & _PAGE_PSE)
4407 ? get_fl1_shadow_status(v, gfn)
4408 : get_shadow_status(v, audit_gfn_to_mfn(v, gfn, gl2mfn),
4409 SH_type_l1_shadow);
4410 if ( mfn_x(gmfn) != mfn_x(mfn) )
4411 AUDIT_FAIL(2, "bad translation: gfn %" SH_PRI_gfn
4412 " (--> %" PRI_mfn ")"
4413 " --> %" PRI_mfn " != mfn %" PRI_mfn,
4414 gfn_x(gfn),
4415 (guest_l2e_get_flags(*gl2e) & _PAGE_PSE) ? 0
4416 : mfn_x(audit_gfn_to_mfn(v, gfn, gl2mfn)),
4417 mfn_x(gmfn), mfn_x(mfn));
4419 });
4420 sh_unmap_domain_page(gp);
4421 return 0;
4424 #if GUEST_PAGING_LEVELS >= 4
4425 int sh_audit_l3_table(struct vcpu *v, mfn_t sl3mfn, mfn_t x)
4427 guest_l3e_t *gl3e, *gp;
4428 shadow_l3e_t *sl3e;
4429 mfn_t mfn, gmfn, gl3mfn;
4430 gfn_t gfn;
4431 char *s;
4432 int done = 0;
4434 /* Follow the backpointer */
4435 gl3mfn = _mfn(mfn_to_shadow_page(sl3mfn)->backpointer);
4436 gl3e = gp = sh_map_domain_page(gl3mfn);
4437 SHADOW_FOREACH_L3E(sl3mfn, sl3e, &gl3e, done, {
4439 s = sh_audit_flags(v, 3, guest_l3e_get_flags(*gl3e),
4440 shadow_l3e_get_flags(*sl3e));
4441 if ( s ) AUDIT_FAIL(3, "%s", s);
4443 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4445 gfn = guest_l3e_get_gfn(*gl3e);
4446 mfn = shadow_l3e_get_mfn(*sl3e);
4447 gmfn = get_shadow_status(v, audit_gfn_to_mfn(v, gfn, gl3mfn),
4448 ((GUEST_PAGING_LEVELS == 3 ||
4449 is_pv_32on64_vcpu(v))
4450 && !shadow_mode_external(v->domain)
4451 && (guest_index(gl3e) % 4) == 3)
4452 ? SH_type_l2h_shadow
4453 : SH_type_l2_shadow);
4454 if ( mfn_x(gmfn) != mfn_x(mfn) )
4455 AUDIT_FAIL(3, "bad translation: gfn %" SH_PRI_gfn
4456 " --> %" PRI_mfn " != mfn %" PRI_mfn,
4457 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
4459 });
4460 sh_unmap_domain_page(gp);
4461 return 0;
4464 int sh_audit_l4_table(struct vcpu *v, mfn_t sl4mfn, mfn_t x)
4466 guest_l4e_t *gl4e, *gp;
4467 shadow_l4e_t *sl4e;
4468 mfn_t mfn, gmfn, gl4mfn;
4469 gfn_t gfn;
4470 char *s;
4471 int done = 0;
4473 /* Follow the backpointer */
4474 gl4mfn = _mfn(mfn_to_shadow_page(sl4mfn)->backpointer);
4475 gl4e = gp = sh_map_domain_page(gl4mfn);
4476 SHADOW_FOREACH_L4E(sl4mfn, sl4e, &gl4e, done, v->domain,
4478 s = sh_audit_flags(v, 4, guest_l4e_get_flags(*gl4e),
4479 shadow_l4e_get_flags(*sl4e));
4480 if ( s ) AUDIT_FAIL(4, "%s", s);
4482 if ( SHADOW_AUDIT & SHADOW_AUDIT_ENTRIES_MFNS )
4484 gfn = guest_l4e_get_gfn(*gl4e);
4485 mfn = shadow_l4e_get_mfn(*sl4e);
4486 gmfn = get_shadow_status(v, audit_gfn_to_mfn(v, gfn, gl4mfn),
4487 SH_type_l3_shadow);
4488 if ( mfn_x(gmfn) != mfn_x(mfn) )
4489 AUDIT_FAIL(4, "bad translation: gfn %" SH_PRI_gfn
4490 " --> %" PRI_mfn " != mfn %" PRI_mfn,
4491 gfn_x(gfn), mfn_x(gmfn), mfn_x(mfn));
4493 });
4494 sh_unmap_domain_page(gp);
4495 return 0;
4497 #endif /* GUEST_PAGING_LEVELS >= 4 */
4500 #undef AUDIT_FAIL
4502 #endif /* Audit code */
4504 /**************************************************************************/
4505 /* Entry points into this mode of the shadow code.
4506 * This will all be mangled by the preprocessor to uniquify everything. */
4507 struct paging_mode sh_paging_mode = {
4508 .page_fault = sh_page_fault,
4509 .invlpg = sh_invlpg,
4510 .gva_to_gfn = sh_gva_to_gfn,
4511 .update_cr3 = sh_update_cr3,
4512 .update_paging_modes = shadow_update_paging_modes,
4513 .write_p2m_entry = shadow_write_p2m_entry,
4514 .write_guest_entry = shadow_write_guest_entry,
4515 .cmpxchg_guest_entry = shadow_cmpxchg_guest_entry,
4516 .guest_map_l1e = sh_guest_map_l1e,
4517 .guest_get_eff_l1e = sh_guest_get_eff_l1e,
4518 .guest_levels = GUEST_PAGING_LEVELS,
4519 .shadow.detach_old_tables = sh_detach_old_tables,
4520 .shadow.x86_emulate_write = sh_x86_emulate_write,
4521 .shadow.x86_emulate_cmpxchg = sh_x86_emulate_cmpxchg,
4522 .shadow.x86_emulate_cmpxchg8b = sh_x86_emulate_cmpxchg8b,
4523 .shadow.make_monitor_table = sh_make_monitor_table,
4524 .shadow.destroy_monitor_table = sh_destroy_monitor_table,
4525 #if SHADOW_OPTIMIZATIONS & SHOPT_WRITABLE_HEURISTIC
4526 .shadow.guess_wrmap = sh_guess_wrmap,
4527 #endif
4528 .shadow.shadow_levels = SHADOW_PAGING_LEVELS,
4529 };
4531 /*
4532 * Local variables:
4533 * mode: C
4534 * c-set-style: "BSD"
4535 * c-basic-offset: 4
4536 * indent-tabs-mode: nil
4537 * End:
4538 */