2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
9 * Copyright (C) 2006 Qumranet, Inc.
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
21 * We need the mmu code to access both 32-bit and 64-bit guest ptes,
22 * so the code in this file is compiled twice, once per pte size.
26 #define pt_element_t u64
27 #define guest_walker guest_walker64
28 #define FNAME(name) paging##64_##name
29 #define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK
30 #define PT_DIR_BASE_ADDR_MASK PT64_DIR_BASE_ADDR_MASK
31 #define PT_INDEX(addr, level) PT64_INDEX(addr, level)
32 #define PT_LEVEL_MASK(level) PT64_LEVEL_MASK(level)
33 #define PT_LEVEL_BITS PT64_LEVEL_BITS
35 #define PT_MAX_FULL_LEVELS 4
36 #define CMPXCHG cmpxchg
38 #define CMPXCHG cmpxchg64
39 #define PT_MAX_FULL_LEVELS 2
42 #define pt_element_t u32
43 #define guest_walker guest_walker32
44 #define FNAME(name) paging##32_##name
45 #define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK
46 #define PT_DIR_BASE_ADDR_MASK PT32_DIR_BASE_ADDR_MASK
47 #define PT_INDEX(addr, level) PT32_INDEX(addr, level)
48 #define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level)
49 #define PT_LEVEL_BITS PT32_LEVEL_BITS
50 #define PT_MAX_FULL_LEVELS 2
51 #define CMPXCHG cmpxchg
53 #error Invalid PTTYPE value
56 #define gpte_to_gfn FNAME(gpte_to_gfn)
57 #define gpte_to_gfn_pde FNAME(gpte_to_gfn_pde)
60 * The guest_walker structure emulates the behavior of the hardware page
65 gfn_t table_gfn[PT_MAX_FULL_LEVELS];
66 pt_element_t ptes[PT_MAX_FULL_LEVELS];
67 gpa_t pte_gpa[PT_MAX_FULL_LEVELS];
74 static gfn_t gpte_to_gfn(pt_element_t gpte)
76 return (gpte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT;
79 static gfn_t gpte_to_gfn_pde(pt_element_t gpte)
81 return (gpte & PT_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
84 static bool FNAME(cmpxchg_gpte)(struct kvm *kvm,
85 gfn_t table_gfn, unsigned index,
86 pt_element_t orig_pte, pt_element_t new_pte)
92 page = gfn_to_page(kvm, table_gfn);
94 table = kmap_atomic(page, KM_USER0);
95 ret = CMPXCHG(&table[index], orig_pte, new_pte);
96 kunmap_atomic(table, KM_USER0);
98 kvm_release_page_dirty(page);
100 return (ret != orig_pte);
103 static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte)
107 access = (gpte & (PT_WRITABLE_MASK | PT_USER_MASK)) | ACC_EXEC_MASK;
110 access &= ~(gpte >> PT64_NX_SHIFT);
116 * Fetch a guest pte for a guest virtual address
118 static int FNAME(walk_addr)(struct guest_walker *walker,
119 struct kvm_vcpu *vcpu, gva_t addr,
120 int write_fault, int user_fault, int fetch_fault)
124 unsigned index, pt_access, pte_access;
127 pgprintk("%s: addr %lx\n", __func__, addr);
129 walker->level = vcpu->arch.mmu.root_level;
130 pte = vcpu->arch.cr3;
132 if (!is_long_mode(vcpu)) {
133 pte = vcpu->arch.pdptrs[(addr >> 30) & 3];
134 if (!is_present_pte(pte))
139 ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
140 (vcpu->arch.cr3 & CR3_NONPAE_RESERVED_BITS) == 0);
145 index = PT_INDEX(addr, walker->level);
147 table_gfn = gpte_to_gfn(pte);
148 pte_gpa = gfn_to_gpa(table_gfn);
149 pte_gpa += index * sizeof(pt_element_t);
150 walker->table_gfn[walker->level - 1] = table_gfn;
151 walker->pte_gpa[walker->level - 1] = pte_gpa;
152 pgprintk("%s: table_gfn[%d] %lx\n", __func__,
153 walker->level - 1, table_gfn);
155 kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte));
157 if (!is_present_pte(pte))
160 if (write_fault && !is_writeble_pte(pte))
161 if (user_fault || is_write_protection(vcpu))
164 if (user_fault && !(pte & PT_USER_MASK))
168 if (fetch_fault && is_nx(vcpu) && (pte & PT64_NX_MASK))
172 if (!(pte & PT_ACCESSED_MASK)) {
173 mark_page_dirty(vcpu->kvm, table_gfn);
174 if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn,
175 index, pte, pte|PT_ACCESSED_MASK))
177 pte |= PT_ACCESSED_MASK;
180 pte_access = pt_access & FNAME(gpte_access)(vcpu, pte);
182 walker->ptes[walker->level - 1] = pte;
184 if (walker->level == PT_PAGE_TABLE_LEVEL) {
185 walker->gfn = gpte_to_gfn(pte);
189 if (walker->level == PT_DIRECTORY_LEVEL
190 && (pte & PT_PAGE_SIZE_MASK)
191 && (PTTYPE == 64 || is_pse(vcpu))) {
192 walker->gfn = gpte_to_gfn_pde(pte);
193 walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL);
194 if (PTTYPE == 32 && is_cpuid_PSE36())
195 walker->gfn += pse36_gfn_delta(pte);
199 pt_access = pte_access;
203 if (write_fault && !is_dirty_pte(pte)) {
206 mark_page_dirty(vcpu->kvm, table_gfn);
207 ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte,
211 pte |= PT_DIRTY_MASK;
212 kvm_mmu_pte_write(vcpu, pte_gpa, (u8 *)&pte, sizeof(pte), 0);
213 walker->ptes[walker->level - 1] = pte;
216 walker->pt_access = pt_access;
217 walker->pte_access = pte_access;
218 pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
219 __func__, (u64)pte, pt_access, pte_access);
223 walker->error_code = 0;
227 walker->error_code = PFERR_PRESENT_MASK;
231 walker->error_code |= PFERR_WRITE_MASK;
233 walker->error_code |= PFERR_USER_MASK;
235 walker->error_code |= PFERR_FETCH_MASK;
239 static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
240 u64 *spte, const void *pte)
245 int largepage = vcpu->arch.update_pte.largepage;
247 gpte = *(const pt_element_t *)pte;
248 if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) {
249 if (!is_present_pte(gpte))
250 set_shadow_pte(spte, shadow_notrap_nonpresent_pte);
253 pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
254 pte_access = page->role.access & FNAME(gpte_access)(vcpu, gpte);
255 if (gpte_to_gfn(gpte) != vcpu->arch.update_pte.gfn)
257 pfn = vcpu->arch.update_pte.pfn;
258 if (is_error_pfn(pfn))
260 if (mmu_notifier_retry(vcpu, vcpu->arch.update_pte.mmu_seq))
263 mmu_set_spte(vcpu, spte, page->role.access, pte_access, 0, 0,
264 gpte & PT_DIRTY_MASK, NULL, largepage,
265 gpte & PT_GLOBAL_MASK, gpte_to_gfn(gpte),
270 * Fetch a shadow pte for a specific level in the paging hierarchy.
272 static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
273 struct guest_walker *gw,
274 int user_fault, int write_fault, int largepage,
275 int *ptwrite, pfn_t pfn)
277 unsigned access = gw->pt_access;
278 struct kvm_mmu_page *shadow_page;
284 pt_element_t curr_pte;
285 struct kvm_shadow_walk_iterator iterator;
287 if (!is_present_pte(gw->ptes[gw->level - 1]))
290 for_each_shadow_entry(vcpu, addr, iterator) {
291 level = iterator.level;
292 sptep = iterator.sptep;
293 if (level == PT_PAGE_TABLE_LEVEL
294 || (largepage && level == PT_DIRECTORY_LEVEL)) {
295 mmu_set_spte(vcpu, sptep, access,
296 gw->pte_access & access,
297 user_fault, write_fault,
298 gw->ptes[gw->level-1] & PT_DIRTY_MASK,
300 gw->ptes[gw->level-1] & PT_GLOBAL_MASK,
301 gw->gfn, pfn, false);
305 if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep))
308 if (is_large_pte(*sptep)) {
309 set_shadow_pte(sptep, shadow_trap_nonpresent_pte);
310 kvm_flush_remote_tlbs(vcpu->kvm);
311 rmap_remove(vcpu->kvm, sptep);
314 if (level == PT_DIRECTORY_LEVEL
315 && gw->level == PT_DIRECTORY_LEVEL) {
317 if (!is_dirty_pte(gw->ptes[level - 1]))
318 access &= ~ACC_WRITE_MASK;
319 table_gfn = gpte_to_gfn(gw->ptes[level - 1]);
322 table_gfn = gw->table_gfn[level - 2];
324 shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
325 metaphysical, access, sptep);
327 r = kvm_read_guest_atomic(vcpu->kvm,
328 gw->pte_gpa[level - 2],
329 &curr_pte, sizeof(curr_pte));
330 if (r || curr_pte != gw->ptes[level - 2]) {
331 kvm_mmu_put_page(shadow_page, sptep);
332 kvm_release_pfn_clean(pfn);
338 spte = __pa(shadow_page->spt)
339 | PT_PRESENT_MASK | PT_ACCESSED_MASK
340 | PT_WRITABLE_MASK | PT_USER_MASK;
348 * Page fault handler. There are several causes for a page fault:
349 * - there is no shadow pte for the guest pte
350 * - write access through a shadow pte marked read only so that we can set
352 * - write access to a shadow pte marked read only so we can update the page
353 * dirty bitmap, when userspace requests it
354 * - mmio access; in this case we will never install a present shadow pte
355 * - normal guest page fault due to the guest pte marked not present, not
356 * writable, or not executable
358 * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
359 * a negative value on error.
361 static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
364 int write_fault = error_code & PFERR_WRITE_MASK;
365 int user_fault = error_code & PFERR_USER_MASK;
366 int fetch_fault = error_code & PFERR_FETCH_MASK;
367 struct guest_walker walker;
373 unsigned long mmu_seq;
375 pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
376 kvm_mmu_audit(vcpu, "pre page fault");
378 r = mmu_topup_memory_caches(vcpu);
383 * Look up the shadow pte for the faulting address.
385 r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
389 * The page is not mapped by the guest. Let the guest handle it.
392 pgprintk("%s: guest page fault\n", __func__);
393 inject_page_fault(vcpu, addr, walker.error_code);
394 vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
398 if (walker.level == PT_DIRECTORY_LEVEL) {
400 large_gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE-1);
401 if (is_largepage_backed(vcpu, large_gfn)) {
402 walker.gfn = large_gfn;
406 mmu_seq = vcpu->kvm->mmu_notifier_seq;
408 pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
411 if (is_error_pfn(pfn)) {
412 pgprintk("gfn %lx is mmio\n", walker.gfn);
413 kvm_release_pfn_clean(pfn);
417 spin_lock(&vcpu->kvm->mmu_lock);
418 if (mmu_notifier_retry(vcpu, mmu_seq))
420 kvm_mmu_free_some_pages(vcpu);
421 shadow_pte = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
422 largepage, &write_pt, pfn);
424 pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
425 shadow_pte, *shadow_pte, write_pt);
428 vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
430 ++vcpu->stat.pf_fixed;
431 kvm_mmu_audit(vcpu, "post page fault (fixed)");
432 spin_unlock(&vcpu->kvm->mmu_lock);
437 spin_unlock(&vcpu->kvm->mmu_lock);
438 kvm_release_pfn_clean(pfn);
442 static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
444 struct kvm_shadow_walk_iterator iterator;
450 spin_lock(&vcpu->kvm->mmu_lock);
452 for_each_shadow_entry(vcpu, gva, iterator) {
453 level = iterator.level;
454 sptep = iterator.sptep;
456 /* FIXME: properly handle invlpg on large guest pages */
457 if (level == PT_PAGE_TABLE_LEVEL ||
458 ((level == PT_DIRECTORY_LEVEL) && is_large_pte(*sptep))) {
459 struct kvm_mmu_page *sp = page_header(__pa(sptep));
461 pte_gpa = (sp->gfn << PAGE_SHIFT);
462 pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
464 if (is_shadow_present_pte(*sptep)) {
465 rmap_remove(vcpu->kvm, sptep);
466 if (is_large_pte(*sptep))
467 --vcpu->kvm->stat.lpages;
469 set_shadow_pte(sptep, shadow_trap_nonpresent_pte);
473 if (!is_shadow_present_pte(*sptep))
477 spin_unlock(&vcpu->kvm->mmu_lock);
481 if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
482 sizeof(pt_element_t)))
484 if (is_present_pte(gpte) && (gpte & PT_ACCESSED_MASK)) {
485 if (mmu_topup_memory_caches(vcpu))
487 kvm_mmu_pte_write(vcpu, pte_gpa, (const u8 *)&gpte,
488 sizeof(pt_element_t), 0);
492 static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr)
494 struct guest_walker walker;
495 gpa_t gpa = UNMAPPED_GVA;
498 r = FNAME(walk_addr)(&walker, vcpu, vaddr, 0, 0, 0);
501 gpa = gfn_to_gpa(walker.gfn);
502 gpa |= vaddr & ~PAGE_MASK;
508 static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
509 struct kvm_mmu_page *sp)
512 pt_element_t pt[256 / sizeof(pt_element_t)];
515 if (sp->role.metaphysical
516 || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
517 nonpaging_prefetch_page(vcpu, sp);
521 pte_gpa = gfn_to_gpa(sp->gfn);
523 offset = sp->role.quadrant << PT64_LEVEL_BITS;
524 pte_gpa += offset * sizeof(pt_element_t);
527 for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) {
528 r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt);
529 pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t);
530 for (j = 0; j < ARRAY_SIZE(pt); ++j)
531 if (r || is_present_pte(pt[j]))
532 sp->spt[i+j] = shadow_trap_nonpresent_pte;
534 sp->spt[i+j] = shadow_notrap_nonpresent_pte;
539 * Using the cached information from sp->gfns is safe because:
540 * - The spte has a reference to the struct page, so the pfn for a given gfn
541 * can't change unless all sptes pointing to it are nuked first.
542 * - Alias changes zap the entire shadow cache.
544 static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
546 int i, offset, nr_present;
548 offset = nr_present = 0;
551 offset = sp->role.quadrant << PT64_LEVEL_BITS;
553 for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
557 gfn_t gfn = sp->gfns[i];
559 if (!is_shadow_present_pte(sp->spt[i]))
562 pte_gpa = gfn_to_gpa(sp->gfn);
563 pte_gpa += (i+offset) * sizeof(pt_element_t);
565 if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
566 sizeof(pt_element_t)))
569 if (gpte_to_gfn(gpte) != gfn || !is_present_pte(gpte) ||
570 !(gpte & PT_ACCESSED_MASK)) {
573 rmap_remove(vcpu->kvm, &sp->spt[i]);
574 if (is_present_pte(gpte))
575 nonpresent = shadow_trap_nonpresent_pte;
577 nonpresent = shadow_notrap_nonpresent_pte;
578 set_shadow_pte(&sp->spt[i], nonpresent);
583 pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
584 set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
585 is_dirty_pte(gpte), 0, gpte & PT_GLOBAL_MASK, gfn,
586 spte_to_pfn(sp->spt[i]), true, false);
595 #undef PT_BASE_ADDR_MASK
598 #undef PT_DIR_BASE_ADDR_MASK
600 #undef PT_MAX_FULL_LEVELS
602 #undef gpte_to_gfn_pde