KVM: MMU: Fix printk() format string
[pandora-kernel.git] / arch / x86 / kvm / paging_tmpl.h
index 03ba860..934c7b6 100644 (file)
@@ -91,7 +91,10 @@ static bool FNAME(cmpxchg_gpte)(struct kvm *kvm,
        pt_element_t *table;
        struct page *page;
 
+       down_read(&current->mm->mmap_sem);
        page = gfn_to_page(kvm, table_gfn);
+       up_read(&current->mm->mmap_sem);
+
        table = kmap_atomic(page, KM_USER0);
 
        ret = CMPXCHG(&table[index], orig_pte, new_pte);
@@ -127,7 +130,7 @@ static int FNAME(walk_addr)(struct guest_walker *walker,
        unsigned index, pt_access, pte_access;
        gpa_t pte_gpa;
 
-       pgprintk("%s: addr %lx\n", __FUNCTION__, addr);
+       pgprintk("%s: addr %lx\n", __func__, addr);
 walk:
        walker->level = vcpu->arch.mmu.root_level;
        pte = vcpu->arch.cr3;
@@ -140,7 +143,7 @@ walk:
        }
 #endif
        ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
-              (vcpu->cr3 & CR3_NONPAE_RESERVED_BITS) == 0);
+              (vcpu->arch.cr3 & CR3_NONPAE_RESERVED_BITS) == 0);
 
        pt_access = ACC_ALL;
 
@@ -152,7 +155,7 @@ walk:
                pte_gpa += index * sizeof(pt_element_t);
                walker->table_gfn[walker->level - 1] = table_gfn;
                walker->pte_gpa[walker->level - 1] = pte_gpa;
-               pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
+               pgprintk("%s: table_gfn[%d] %lx\n", __func__,
                         walker->level - 1, table_gfn);
 
                kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte));
@@ -219,7 +222,7 @@ walk:
        walker->pt_access = pt_access;
        walker->pte_access = pte_access;
        pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
-                __FUNCTION__, (u64)pte, pt_access, pte_access);
+                __func__, (u64)pte, pt_access, pte_access);
        return 1;
 
 not_present:
@@ -240,31 +243,30 @@ err:
 }
 
 static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
-                             u64 *spte, const void *pte, int bytes,
-                             int offset_in_pte)
+                             u64 *spte, const void *pte)
 {
        pt_element_t gpte;
        unsigned pte_access;
-       struct page *npage;
+       pfn_t pfn;
+       int largepage = vcpu->arch.update_pte.largepage;
 
        gpte = *(const pt_element_t *)pte;
        if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) {
-               if (!offset_in_pte && !is_present_pte(gpte))
+               if (!is_present_pte(gpte))
                        set_shadow_pte(spte, shadow_notrap_nonpresent_pte);
                return;
        }
-       if (bytes < sizeof(pt_element_t))
-               return;
-       pgprintk("%s: gpte %llx spte %p\n", __FUNCTION__, (u64)gpte, spte);
+       pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
        pte_access = page->role.access & FNAME(gpte_access)(vcpu, gpte);
        if (gpte_to_gfn(gpte) != vcpu->arch.update_pte.gfn)
                return;
-       npage = vcpu->arch.update_pte.page;
-       if (!npage)
+       pfn = vcpu->arch.update_pte.pfn;
+       if (is_error_pfn(pfn))
                return;
-       get_page(npage);
+       kvm_get_pfn(pfn);
        mmu_set_spte(vcpu, spte, page->role.access, pte_access, 0, 0,
-                    gpte & PT_DIRTY_MASK, NULL, gpte_to_gfn(gpte), npage);
+                    gpte & PT_DIRTY_MASK, NULL, largepage, gpte_to_gfn(gpte),
+                    pfn, true);
 }
 
 /*
@@ -272,8 +274,8 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
  */
 static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
                         struct guest_walker *walker,
-                        int user_fault, int write_fault, int *ptwrite,
-                        struct page *page)
+                        int user_fault, int write_fault, int largepage,
+                        int *ptwrite, pfn_t pfn)
 {
        hpa_t shadow_addr;
        int level;
@@ -297,16 +299,23 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
                u64 shadow_pte;
                int metaphysical;
                gfn_t table_gfn;
-               bool new_page = 0;
 
                shadow_ent = ((u64 *)__va(shadow_addr)) + index;
                if (level == PT_PAGE_TABLE_LEVEL)
                        break;
-               if (is_shadow_present_pte(*shadow_ent)) {
+
+               if (largepage && level == PT_DIRECTORY_LEVEL)
+                       break;
+
+               if (is_shadow_present_pte(*shadow_ent)
+                   && !is_large_pte(*shadow_ent)) {
                        shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK;
                        continue;
                }
 
+               if (is_large_pte(*shadow_ent))
+                       rmap_remove(vcpu->kvm, shadow_ent);
+
                if (level - 1 == PT_PAGE_TABLE_LEVEL
                    && walker->level == PT_DIRECTORY_LEVEL) {
                        metaphysical = 1;
@@ -319,15 +328,15 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
                }
                shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
                                               metaphysical, access,
-                                              shadow_ent, &new_page);
-               if (new_page && !metaphysical) {
+                                              shadow_ent);
+               if (!metaphysical) {
                        int r;
                        pt_element_t curr_pte;
                        r = kvm_read_guest_atomic(vcpu->kvm,
                                                  walker->pte_gpa[level - 2],
                                                  &curr_pte, sizeof(curr_pte));
                        if (r || curr_pte != walker->ptes[level - 2]) {
-                               kvm_release_page_clean(page);
+                               kvm_release_pfn_clean(pfn);
                                return NULL;
                        }
                }
@@ -340,7 +349,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
        mmu_set_spte(vcpu, shadow_ent, access, walker->pte_access & access,
                     user_fault, write_fault,
                     walker->ptes[walker->level-1] & PT_DIRTY_MASK,
-                    ptwrite, walker->gfn, page);
+                    ptwrite, largepage, walker->gfn, pfn, false);
 
        return shadow_ent;
 }
@@ -369,16 +378,16 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
        u64 *shadow_pte;
        int write_pt = 0;
        int r;
-       struct page *page;
+       pfn_t pfn;
+       int largepage = 0;
 
-       pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code);
+       pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
        kvm_mmu_audit(vcpu, "pre page fault");
 
        r = mmu_topup_memory_caches(vcpu);
        if (r)
                return r;
 
-       down_read(&current->mm->mmap_sem);
        /*
         * Look up the shadow pte for the faulting address.
         */
@@ -389,38 +398,45 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
         * The page is not mapped by the guest.  Let the guest handle it.
         */
        if (!r) {
-               pgprintk("%s: guest page fault\n", __FUNCTION__);
+               pgprintk("%s: guest page fault\n", __func__);
                inject_page_fault(vcpu, addr, walker.error_code);
                vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
-               up_read(&current->mm->mmap_sem);
                return 0;
        }
 
-       page = gfn_to_page(vcpu->kvm, walker.gfn);
+       down_read(&current->mm->mmap_sem);
+       if (walker.level == PT_DIRECTORY_LEVEL) {
+               gfn_t large_gfn;
+               large_gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE-1);
+               if (is_largepage_backed(vcpu, large_gfn)) {
+                       walker.gfn = large_gfn;
+                       largepage = 1;
+               }
+       }
+       pfn = gfn_to_pfn(vcpu->kvm, walker.gfn);
+       up_read(&current->mm->mmap_sem);
+
+       /* mmio */
+       if (is_error_pfn(pfn)) {
+               pgprintk("gfn %lx is mmio\n", walker.gfn);
+               kvm_release_pfn_clean(pfn);
+               return 1;
+       }
 
        spin_lock(&vcpu->kvm->mmu_lock);
        kvm_mmu_free_some_pages(vcpu);
        shadow_pte = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
-                                 &write_pt, page);
-       pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __FUNCTION__,
+                                 largepage, &write_pt, pfn);
+
+       pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
                 shadow_pte, *shadow_pte, write_pt);
 
        if (!write_pt)
                vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
 
-       /*
-        * mmio: emulate if accessible, otherwise its a guest fault.
-        */
-       if (shadow_pte && is_io_pte(*shadow_pte)) {
-               spin_unlock(&vcpu->kvm->mmu_lock);
-               up_read(&current->mm->mmap_sem);
-               return 1;
-       }
-
        ++vcpu->stat.pf_fixed;
        kvm_mmu_audit(vcpu, "post page fault (fixed)");
        spin_unlock(&vcpu->kvm->mmu_lock);
-       up_read(&current->mm->mmap_sem);
 
        return write_pt;
 }