diff options
Diffstat (limited to 'arch/x86/kvm/mmu.c')
-rw-r--r-- | arch/x86/kvm/mmu.c | 197 |
1 files changed, 114 insertions, 83 deletions
diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c index b6caf1329b1b..5c3d6e81a7dc 100644 --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c | |||
@@ -126,6 +126,7 @@ module_param(oos_shadow, bool, 0644); | |||
126 | #define PFERR_PRESENT_MASK (1U << 0) | 126 | #define PFERR_PRESENT_MASK (1U << 0) |
127 | #define PFERR_WRITE_MASK (1U << 1) | 127 | #define PFERR_WRITE_MASK (1U << 1) |
128 | #define PFERR_USER_MASK (1U << 2) | 128 | #define PFERR_USER_MASK (1U << 2) |
129 | #define PFERR_RSVD_MASK (1U << 3) | ||
129 | #define PFERR_FETCH_MASK (1U << 4) | 130 | #define PFERR_FETCH_MASK (1U << 4) |
130 | 131 | ||
131 | #define PT_DIRECTORY_LEVEL 2 | 132 | #define PT_DIRECTORY_LEVEL 2 |
@@ -177,7 +178,11 @@ static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */ | |||
177 | static u64 __read_mostly shadow_user_mask; | 178 | static u64 __read_mostly shadow_user_mask; |
178 | static u64 __read_mostly shadow_accessed_mask; | 179 | static u64 __read_mostly shadow_accessed_mask; |
179 | static u64 __read_mostly shadow_dirty_mask; | 180 | static u64 __read_mostly shadow_dirty_mask; |
180 | static u64 __read_mostly shadow_mt_mask; | 181 | |
182 | static inline u64 rsvd_bits(int s, int e) | ||
183 | { | ||
184 | return ((1ULL << (e - s + 1)) - 1) << s; | ||
185 | } | ||
181 | 186 | ||
182 | void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte) | 187 | void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte) |
183 | { | 188 | { |
@@ -193,14 +198,13 @@ void kvm_mmu_set_base_ptes(u64 base_pte) | |||
193 | EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes); | 198 | EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes); |
194 | 199 | ||
195 | void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask, | 200 | void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask, |
196 | u64 dirty_mask, u64 nx_mask, u64 x_mask, u64 mt_mask) | 201 | u64 dirty_mask, u64 nx_mask, u64 x_mask) |
197 | { | 202 | { |
198 | shadow_user_mask = user_mask; | 203 | shadow_user_mask = user_mask; |
199 | shadow_accessed_mask = accessed_mask; | 204 | shadow_accessed_mask = accessed_mask; |
200 | shadow_dirty_mask = dirty_mask; | 205 | shadow_dirty_mask = dirty_mask; |
201 | shadow_nx_mask = nx_mask; | 206 | shadow_nx_mask = nx_mask; |
202 | shadow_x_mask = x_mask; | 207 | shadow_x_mask = x_mask; |
203 | shadow_mt_mask = mt_mask; | ||
204 | } | 208 | } |
205 | EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes); | 209 | EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes); |
206 | 210 | ||
@@ -219,11 +223,6 @@ static int is_nx(struct kvm_vcpu *vcpu) | |||
219 | return vcpu->arch.shadow_efer & EFER_NX; | 223 | return vcpu->arch.shadow_efer & EFER_NX; |
220 | } | 224 | } |
221 | 225 | ||
222 | static int is_present_pte(unsigned long pte) | ||
223 | { | ||
224 | return pte & PT_PRESENT_MASK; | ||
225 | } | ||
226 | |||
227 | static int is_shadow_present_pte(u64 pte) | 226 | static int is_shadow_present_pte(u64 pte) |
228 | { | 227 | { |
229 | return pte != shadow_trap_nonpresent_pte | 228 | return pte != shadow_trap_nonpresent_pte |
@@ -1074,18 +1073,10 @@ static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn) | |||
1074 | return NULL; | 1073 | return NULL; |
1075 | } | 1074 | } |
1076 | 1075 | ||
1077 | static void kvm_unlink_unsync_global(struct kvm *kvm, struct kvm_mmu_page *sp) | ||
1078 | { | ||
1079 | list_del(&sp->oos_link); | ||
1080 | --kvm->stat.mmu_unsync_global; | ||
1081 | } | ||
1082 | |||
1083 | static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp) | 1076 | static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
1084 | { | 1077 | { |
1085 | WARN_ON(!sp->unsync); | 1078 | WARN_ON(!sp->unsync); |
1086 | sp->unsync = 0; | 1079 | sp->unsync = 0; |
1087 | if (sp->global) | ||
1088 | kvm_unlink_unsync_global(kvm, sp); | ||
1089 | --kvm->stat.mmu_unsync; | 1080 | --kvm->stat.mmu_unsync; |
1090 | } | 1081 | } |
1091 | 1082 | ||
@@ -1248,7 +1239,6 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, | |||
1248 | pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word); | 1239 | pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word); |
1249 | sp->gfn = gfn; | 1240 | sp->gfn = gfn; |
1250 | sp->role = role; | 1241 | sp->role = role; |
1251 | sp->global = 0; | ||
1252 | hlist_add_head(&sp->hash_link, bucket); | 1242 | hlist_add_head(&sp->hash_link, bucket); |
1253 | if (!direct) { | 1243 | if (!direct) { |
1254 | if (rmap_write_protect(vcpu->kvm, gfn)) | 1244 | if (rmap_write_protect(vcpu->kvm, gfn)) |
@@ -1616,7 +1606,7 @@ static int get_mtrr_type(struct mtrr_state_type *mtrr_state, | |||
1616 | return mtrr_state->def_type; | 1606 | return mtrr_state->def_type; |
1617 | } | 1607 | } |
1618 | 1608 | ||
1619 | static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn) | 1609 | u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn) |
1620 | { | 1610 | { |
1621 | u8 mtrr; | 1611 | u8 mtrr; |
1622 | 1612 | ||
@@ -1626,6 +1616,7 @@ static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn) | |||
1626 | mtrr = MTRR_TYPE_WRBACK; | 1616 | mtrr = MTRR_TYPE_WRBACK; |
1627 | return mtrr; | 1617 | return mtrr; |
1628 | } | 1618 | } |
1619 | EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type); | ||
1629 | 1620 | ||
1630 | static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) | 1621 | static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) |
1631 | { | 1622 | { |
@@ -1646,11 +1637,7 @@ static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) | |||
1646 | ++vcpu->kvm->stat.mmu_unsync; | 1637 | ++vcpu->kvm->stat.mmu_unsync; |
1647 | sp->unsync = 1; | 1638 | sp->unsync = 1; |
1648 | 1639 | ||
1649 | if (sp->global) { | 1640 | kvm_mmu_mark_parents_unsync(vcpu, sp); |
1650 | list_add(&sp->oos_link, &vcpu->kvm->arch.oos_global_pages); | ||
1651 | ++vcpu->kvm->stat.mmu_unsync_global; | ||
1652 | } else | ||
1653 | kvm_mmu_mark_parents_unsync(vcpu, sp); | ||
1654 | 1641 | ||
1655 | mmu_convert_notrap(sp); | 1642 | mmu_convert_notrap(sp); |
1656 | return 0; | 1643 | return 0; |
@@ -1677,21 +1664,11 @@ static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn, | |||
1677 | static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, | 1664 | static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, |
1678 | unsigned pte_access, int user_fault, | 1665 | unsigned pte_access, int user_fault, |
1679 | int write_fault, int dirty, int largepage, | 1666 | int write_fault, int dirty, int largepage, |
1680 | int global, gfn_t gfn, pfn_t pfn, bool speculative, | 1667 | gfn_t gfn, pfn_t pfn, bool speculative, |
1681 | bool can_unsync) | 1668 | bool can_unsync) |
1682 | { | 1669 | { |
1683 | u64 spte; | 1670 | u64 spte; |
1684 | int ret = 0; | 1671 | int ret = 0; |
1685 | u64 mt_mask = shadow_mt_mask; | ||
1686 | struct kvm_mmu_page *sp = page_header(__pa(shadow_pte)); | ||
1687 | |||
1688 | if (!global && sp->global) { | ||
1689 | sp->global = 0; | ||
1690 | if (sp->unsync) { | ||
1691 | kvm_unlink_unsync_global(vcpu->kvm, sp); | ||
1692 | kvm_mmu_mark_parents_unsync(vcpu, sp); | ||
1693 | } | ||
1694 | } | ||
1695 | 1672 | ||
1696 | /* | 1673 | /* |
1697 | * We don't set the accessed bit, since we sometimes want to see | 1674 | * We don't set the accessed bit, since we sometimes want to see |
@@ -1711,16 +1688,9 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, | |||
1711 | spte |= shadow_user_mask; | 1688 | spte |= shadow_user_mask; |
1712 | if (largepage) | 1689 | if (largepage) |
1713 | spte |= PT_PAGE_SIZE_MASK; | 1690 | spte |= PT_PAGE_SIZE_MASK; |
1714 | if (mt_mask) { | 1691 | if (tdp_enabled) |
1715 | if (!kvm_is_mmio_pfn(pfn)) { | 1692 | spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn, |
1716 | mt_mask = get_memory_type(vcpu, gfn) << | 1693 | kvm_is_mmio_pfn(pfn)); |
1717 | kvm_x86_ops->get_mt_mask_shift(); | ||
1718 | mt_mask |= VMX_EPT_IGMT_BIT; | ||
1719 | } else | ||
1720 | mt_mask = MTRR_TYPE_UNCACHABLE << | ||
1721 | kvm_x86_ops->get_mt_mask_shift(); | ||
1722 | spte |= mt_mask; | ||
1723 | } | ||
1724 | 1694 | ||
1725 | spte |= (u64)pfn << PAGE_SHIFT; | 1695 | spte |= (u64)pfn << PAGE_SHIFT; |
1726 | 1696 | ||
@@ -1765,8 +1735,8 @@ set_pte: | |||
1765 | static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, | 1735 | static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, |
1766 | unsigned pt_access, unsigned pte_access, | 1736 | unsigned pt_access, unsigned pte_access, |
1767 | int user_fault, int write_fault, int dirty, | 1737 | int user_fault, int write_fault, int dirty, |
1768 | int *ptwrite, int largepage, int global, | 1738 | int *ptwrite, int largepage, gfn_t gfn, |
1769 | gfn_t gfn, pfn_t pfn, bool speculative) | 1739 | pfn_t pfn, bool speculative) |
1770 | { | 1740 | { |
1771 | int was_rmapped = 0; | 1741 | int was_rmapped = 0; |
1772 | int was_writeble = is_writeble_pte(*shadow_pte); | 1742 | int was_writeble = is_writeble_pte(*shadow_pte); |
@@ -1795,7 +1765,7 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, | |||
1795 | was_rmapped = 1; | 1765 | was_rmapped = 1; |
1796 | } | 1766 | } |
1797 | if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault, | 1767 | if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault, |
1798 | dirty, largepage, global, gfn, pfn, speculative, true)) { | 1768 | dirty, largepage, gfn, pfn, speculative, true)) { |
1799 | if (write_fault) | 1769 | if (write_fault) |
1800 | *ptwrite = 1; | 1770 | *ptwrite = 1; |
1801 | kvm_x86_ops->tlb_flush(vcpu); | 1771 | kvm_x86_ops->tlb_flush(vcpu); |
@@ -1843,7 +1813,7 @@ static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write, | |||
1843 | || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) { | 1813 | || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) { |
1844 | mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL, | 1814 | mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL, |
1845 | 0, write, 1, &pt_write, | 1815 | 0, write, 1, &pt_write, |
1846 | largepage, 0, gfn, pfn, false); | 1816 | largepage, gfn, pfn, false); |
1847 | ++vcpu->stat.pf_fixed; | 1817 | ++vcpu->stat.pf_fixed; |
1848 | break; | 1818 | break; |
1849 | } | 1819 | } |
@@ -1942,7 +1912,19 @@ static void mmu_free_roots(struct kvm_vcpu *vcpu) | |||
1942 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; | 1912 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
1943 | } | 1913 | } |
1944 | 1914 | ||
1945 | static void mmu_alloc_roots(struct kvm_vcpu *vcpu) | 1915 | static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn) |
1916 | { | ||
1917 | int ret = 0; | ||
1918 | |||
1919 | if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) { | ||
1920 | set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); | ||
1921 | ret = 1; | ||
1922 | } | ||
1923 | |||
1924 | return ret; | ||
1925 | } | ||
1926 | |||
1927 | static int mmu_alloc_roots(struct kvm_vcpu *vcpu) | ||
1946 | { | 1928 | { |
1947 | int i; | 1929 | int i; |
1948 | gfn_t root_gfn; | 1930 | gfn_t root_gfn; |
@@ -1957,13 +1939,15 @@ static void mmu_alloc_roots(struct kvm_vcpu *vcpu) | |||
1957 | ASSERT(!VALID_PAGE(root)); | 1939 | ASSERT(!VALID_PAGE(root)); |
1958 | if (tdp_enabled) | 1940 | if (tdp_enabled) |
1959 | direct = 1; | 1941 | direct = 1; |
1942 | if (mmu_check_root(vcpu, root_gfn)) | ||
1943 | return 1; | ||
1960 | sp = kvm_mmu_get_page(vcpu, root_gfn, 0, | 1944 | sp = kvm_mmu_get_page(vcpu, root_gfn, 0, |
1961 | PT64_ROOT_LEVEL, direct, | 1945 | PT64_ROOT_LEVEL, direct, |
1962 | ACC_ALL, NULL); | 1946 | ACC_ALL, NULL); |
1963 | root = __pa(sp->spt); | 1947 | root = __pa(sp->spt); |
1964 | ++sp->root_count; | 1948 | ++sp->root_count; |
1965 | vcpu->arch.mmu.root_hpa = root; | 1949 | vcpu->arch.mmu.root_hpa = root; |
1966 | return; | 1950 | return 0; |
1967 | } | 1951 | } |
1968 | direct = !is_paging(vcpu); | 1952 | direct = !is_paging(vcpu); |
1969 | if (tdp_enabled) | 1953 | if (tdp_enabled) |
@@ -1980,6 +1964,8 @@ static void mmu_alloc_roots(struct kvm_vcpu *vcpu) | |||
1980 | root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT; | 1964 | root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT; |
1981 | } else if (vcpu->arch.mmu.root_level == 0) | 1965 | } else if (vcpu->arch.mmu.root_level == 0) |
1982 | root_gfn = 0; | 1966 | root_gfn = 0; |
1967 | if (mmu_check_root(vcpu, root_gfn)) | ||
1968 | return 1; | ||
1983 | sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, | 1969 | sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, |
1984 | PT32_ROOT_LEVEL, direct, | 1970 | PT32_ROOT_LEVEL, direct, |
1985 | ACC_ALL, NULL); | 1971 | ACC_ALL, NULL); |
@@ -1988,6 +1974,7 @@ static void mmu_alloc_roots(struct kvm_vcpu *vcpu) | |||
1988 | vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK; | 1974 | vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK; |
1989 | } | 1975 | } |
1990 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); | 1976 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); |
1977 | return 0; | ||
1991 | } | 1978 | } |
1992 | 1979 | ||
1993 | static void mmu_sync_roots(struct kvm_vcpu *vcpu) | 1980 | static void mmu_sync_roots(struct kvm_vcpu *vcpu) |
@@ -2006,7 +1993,7 @@ static void mmu_sync_roots(struct kvm_vcpu *vcpu) | |||
2006 | for (i = 0; i < 4; ++i) { | 1993 | for (i = 0; i < 4; ++i) { |
2007 | hpa_t root = vcpu->arch.mmu.pae_root[i]; | 1994 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
2008 | 1995 | ||
2009 | if (root) { | 1996 | if (root && VALID_PAGE(root)) { |
2010 | root &= PT64_BASE_ADDR_MASK; | 1997 | root &= PT64_BASE_ADDR_MASK; |
2011 | sp = page_header(root); | 1998 | sp = page_header(root); |
2012 | mmu_sync_children(vcpu, sp); | 1999 | mmu_sync_children(vcpu, sp); |
@@ -2014,15 +2001,6 @@ static void mmu_sync_roots(struct kvm_vcpu *vcpu) | |||
2014 | } | 2001 | } |
2015 | } | 2002 | } |
2016 | 2003 | ||
2017 | static void mmu_sync_global(struct kvm_vcpu *vcpu) | ||
2018 | { | ||
2019 | struct kvm *kvm = vcpu->kvm; | ||
2020 | struct kvm_mmu_page *sp, *n; | ||
2021 | |||
2022 | list_for_each_entry_safe(sp, n, &kvm->arch.oos_global_pages, oos_link) | ||
2023 | kvm_sync_page(vcpu, sp); | ||
2024 | } | ||
2025 | |||
2026 | void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) | 2004 | void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) |
2027 | { | 2005 | { |
2028 | spin_lock(&vcpu->kvm->mmu_lock); | 2006 | spin_lock(&vcpu->kvm->mmu_lock); |
@@ -2030,13 +2008,6 @@ void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) | |||
2030 | spin_unlock(&vcpu->kvm->mmu_lock); | 2008 | spin_unlock(&vcpu->kvm->mmu_lock); |
2031 | } | 2009 | } |
2032 | 2010 | ||
2033 | void kvm_mmu_sync_global(struct kvm_vcpu *vcpu) | ||
2034 | { | ||
2035 | spin_lock(&vcpu->kvm->mmu_lock); | ||
2036 | mmu_sync_global(vcpu); | ||
2037 | spin_unlock(&vcpu->kvm->mmu_lock); | ||
2038 | } | ||
2039 | |||
2040 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr) | 2011 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr) |
2041 | { | 2012 | { |
2042 | return vaddr; | 2013 | return vaddr; |
@@ -2151,6 +2122,14 @@ static void paging_free(struct kvm_vcpu *vcpu) | |||
2151 | nonpaging_free(vcpu); | 2122 | nonpaging_free(vcpu); |
2152 | } | 2123 | } |
2153 | 2124 | ||
2125 | static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level) | ||
2126 | { | ||
2127 | int bit7; | ||
2128 | |||
2129 | bit7 = (gpte >> 7) & 1; | ||
2130 | return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0; | ||
2131 | } | ||
2132 | |||
2154 | #define PTTYPE 64 | 2133 | #define PTTYPE 64 |
2155 | #include "paging_tmpl.h" | 2134 | #include "paging_tmpl.h" |
2156 | #undef PTTYPE | 2135 | #undef PTTYPE |
@@ -2159,6 +2138,59 @@ static void paging_free(struct kvm_vcpu *vcpu) | |||
2159 | #include "paging_tmpl.h" | 2138 | #include "paging_tmpl.h" |
2160 | #undef PTTYPE | 2139 | #undef PTTYPE |
2161 | 2140 | ||
2141 | static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level) | ||
2142 | { | ||
2143 | struct kvm_mmu *context = &vcpu->arch.mmu; | ||
2144 | int maxphyaddr = cpuid_maxphyaddr(vcpu); | ||
2145 | u64 exb_bit_rsvd = 0; | ||
2146 | |||
2147 | if (!is_nx(vcpu)) | ||
2148 | exb_bit_rsvd = rsvd_bits(63, 63); | ||
2149 | switch (level) { | ||
2150 | case PT32_ROOT_LEVEL: | ||
2151 | /* no rsvd bits for 2 level 4K page table entries */ | ||
2152 | context->rsvd_bits_mask[0][1] = 0; | ||
2153 | context->rsvd_bits_mask[0][0] = 0; | ||
2154 | if (is_cpuid_PSE36()) | ||
2155 | /* 36bits PSE 4MB page */ | ||
2156 | context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21); | ||
2157 | else | ||
2158 | /* 32 bits PSE 4MB page */ | ||
2159 | context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21); | ||
2160 | context->rsvd_bits_mask[1][0] = ~0ull; | ||
2161 | break; | ||
2162 | case PT32E_ROOT_LEVEL: | ||
2163 | context->rsvd_bits_mask[0][2] = | ||
2164 | rsvd_bits(maxphyaddr, 63) | | ||
2165 | rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */ | ||
2166 | context->rsvd_bits_mask[0][1] = exb_bit_rsvd | | ||
2167 | rsvd_bits(maxphyaddr, 62); /* PDE */ | ||
2168 | context->rsvd_bits_mask[0][0] = exb_bit_rsvd | | ||
2169 | rsvd_bits(maxphyaddr, 62); /* PTE */ | ||
2170 | context->rsvd_bits_mask[1][1] = exb_bit_rsvd | | ||
2171 | rsvd_bits(maxphyaddr, 62) | | ||
2172 | rsvd_bits(13, 20); /* large page */ | ||
2173 | context->rsvd_bits_mask[1][0] = ~0ull; | ||
2174 | break; | ||
2175 | case PT64_ROOT_LEVEL: | ||
2176 | context->rsvd_bits_mask[0][3] = exb_bit_rsvd | | ||
2177 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8); | ||
2178 | context->rsvd_bits_mask[0][2] = exb_bit_rsvd | | ||
2179 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8); | ||
2180 | context->rsvd_bits_mask[0][1] = exb_bit_rsvd | | ||
2181 | rsvd_bits(maxphyaddr, 51); | ||
2182 | context->rsvd_bits_mask[0][0] = exb_bit_rsvd | | ||
2183 | rsvd_bits(maxphyaddr, 51); | ||
2184 | context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3]; | ||
2185 | context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2]; | ||
2186 | context->rsvd_bits_mask[1][1] = exb_bit_rsvd | | ||
2187 | rsvd_bits(maxphyaddr, 51) | | ||
2188 | rsvd_bits(13, 20); /* large page */ | ||
2189 | context->rsvd_bits_mask[1][0] = ~0ull; | ||
2190 | break; | ||
2191 | } | ||
2192 | } | ||
2193 | |||
2162 | static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level) | 2194 | static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level) |
2163 | { | 2195 | { |
2164 | struct kvm_mmu *context = &vcpu->arch.mmu; | 2196 | struct kvm_mmu *context = &vcpu->arch.mmu; |
@@ -2179,6 +2211,7 @@ static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level) | |||
2179 | 2211 | ||
2180 | static int paging64_init_context(struct kvm_vcpu *vcpu) | 2212 | static int paging64_init_context(struct kvm_vcpu *vcpu) |
2181 | { | 2213 | { |
2214 | reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL); | ||
2182 | return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL); | 2215 | return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL); |
2183 | } | 2216 | } |
2184 | 2217 | ||
@@ -2186,6 +2219,7 @@ static int paging32_init_context(struct kvm_vcpu *vcpu) | |||
2186 | { | 2219 | { |
2187 | struct kvm_mmu *context = &vcpu->arch.mmu; | 2220 | struct kvm_mmu *context = &vcpu->arch.mmu; |
2188 | 2221 | ||
2222 | reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL); | ||
2189 | context->new_cr3 = paging_new_cr3; | 2223 | context->new_cr3 = paging_new_cr3; |
2190 | context->page_fault = paging32_page_fault; | 2224 | context->page_fault = paging32_page_fault; |
2191 | context->gva_to_gpa = paging32_gva_to_gpa; | 2225 | context->gva_to_gpa = paging32_gva_to_gpa; |
@@ -2201,6 +2235,7 @@ static int paging32_init_context(struct kvm_vcpu *vcpu) | |||
2201 | 2235 | ||
2202 | static int paging32E_init_context(struct kvm_vcpu *vcpu) | 2236 | static int paging32E_init_context(struct kvm_vcpu *vcpu) |
2203 | { | 2237 | { |
2238 | reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL); | ||
2204 | return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL); | 2239 | return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL); |
2205 | } | 2240 | } |
2206 | 2241 | ||
@@ -2221,12 +2256,15 @@ static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) | |||
2221 | context->gva_to_gpa = nonpaging_gva_to_gpa; | 2256 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
2222 | context->root_level = 0; | 2257 | context->root_level = 0; |
2223 | } else if (is_long_mode(vcpu)) { | 2258 | } else if (is_long_mode(vcpu)) { |
2259 | reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL); | ||
2224 | context->gva_to_gpa = paging64_gva_to_gpa; | 2260 | context->gva_to_gpa = paging64_gva_to_gpa; |
2225 | context->root_level = PT64_ROOT_LEVEL; | 2261 | context->root_level = PT64_ROOT_LEVEL; |
2226 | } else if (is_pae(vcpu)) { | 2262 | } else if (is_pae(vcpu)) { |
2263 | reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL); | ||
2227 | context->gva_to_gpa = paging64_gva_to_gpa; | 2264 | context->gva_to_gpa = paging64_gva_to_gpa; |
2228 | context->root_level = PT32E_ROOT_LEVEL; | 2265 | context->root_level = PT32E_ROOT_LEVEL; |
2229 | } else { | 2266 | } else { |
2267 | reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL); | ||
2230 | context->gva_to_gpa = paging32_gva_to_gpa; | 2268 | context->gva_to_gpa = paging32_gva_to_gpa; |
2231 | context->root_level = PT32_ROOT_LEVEL; | 2269 | context->root_level = PT32_ROOT_LEVEL; |
2232 | } | 2270 | } |
@@ -2290,9 +2328,11 @@ int kvm_mmu_load(struct kvm_vcpu *vcpu) | |||
2290 | goto out; | 2328 | goto out; |
2291 | spin_lock(&vcpu->kvm->mmu_lock); | 2329 | spin_lock(&vcpu->kvm->mmu_lock); |
2292 | kvm_mmu_free_some_pages(vcpu); | 2330 | kvm_mmu_free_some_pages(vcpu); |
2293 | mmu_alloc_roots(vcpu); | 2331 | r = mmu_alloc_roots(vcpu); |
2294 | mmu_sync_roots(vcpu); | 2332 | mmu_sync_roots(vcpu); |
2295 | spin_unlock(&vcpu->kvm->mmu_lock); | 2333 | spin_unlock(&vcpu->kvm->mmu_lock); |
2334 | if (r) | ||
2335 | goto out; | ||
2296 | kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa); | 2336 | kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa); |
2297 | kvm_mmu_flush_tlb(vcpu); | 2337 | kvm_mmu_flush_tlb(vcpu); |
2298 | out: | 2338 | out: |
@@ -2638,14 +2678,6 @@ EXPORT_SYMBOL_GPL(kvm_disable_tdp); | |||
2638 | 2678 | ||
2639 | static void free_mmu_pages(struct kvm_vcpu *vcpu) | 2679 | static void free_mmu_pages(struct kvm_vcpu *vcpu) |
2640 | { | 2680 | { |
2641 | struct kvm_mmu_page *sp; | ||
2642 | |||
2643 | while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) { | ||
2644 | sp = container_of(vcpu->kvm->arch.active_mmu_pages.next, | ||
2645 | struct kvm_mmu_page, link); | ||
2646 | kvm_mmu_zap_page(vcpu->kvm, sp); | ||
2647 | cond_resched(); | ||
2648 | } | ||
2649 | free_page((unsigned long)vcpu->arch.mmu.pae_root); | 2681 | free_page((unsigned long)vcpu->arch.mmu.pae_root); |
2650 | } | 2682 | } |
2651 | 2683 | ||
@@ -2710,7 +2742,6 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) | |||
2710 | { | 2742 | { |
2711 | struct kvm_mmu_page *sp; | 2743 | struct kvm_mmu_page *sp; |
2712 | 2744 | ||
2713 | spin_lock(&kvm->mmu_lock); | ||
2714 | list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) { | 2745 | list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) { |
2715 | int i; | 2746 | int i; |
2716 | u64 *pt; | 2747 | u64 *pt; |
@@ -2725,7 +2756,6 @@ void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) | |||
2725 | pt[i] &= ~PT_WRITABLE_MASK; | 2756 | pt[i] &= ~PT_WRITABLE_MASK; |
2726 | } | 2757 | } |
2727 | kvm_flush_remote_tlbs(kvm); | 2758 | kvm_flush_remote_tlbs(kvm); |
2728 | spin_unlock(&kvm->mmu_lock); | ||
2729 | } | 2759 | } |
2730 | 2760 | ||
2731 | void kvm_mmu_zap_all(struct kvm *kvm) | 2761 | void kvm_mmu_zap_all(struct kvm *kvm) |
@@ -2897,8 +2927,7 @@ static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu, | |||
2897 | 2927 | ||
2898 | static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu) | 2928 | static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
2899 | { | 2929 | { |
2900 | kvm_x86_ops->tlb_flush(vcpu); | 2930 | kvm_set_cr3(vcpu, vcpu->arch.cr3); |
2901 | set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests); | ||
2902 | return 1; | 2931 | return 1; |
2903 | } | 2932 | } |
2904 | 2933 | ||
@@ -3008,11 +3037,13 @@ static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte, | |||
3008 | " in nonleaf level: levels %d gva %lx" | 3037 | " in nonleaf level: levels %d gva %lx" |
3009 | " level %d pte %llx\n", audit_msg, | 3038 | " level %d pte %llx\n", audit_msg, |
3010 | vcpu->arch.mmu.root_level, va, level, ent); | 3039 | vcpu->arch.mmu.root_level, va, level, ent); |
3011 | 3040 | else | |
3012 | audit_mappings_page(vcpu, ent, va, level - 1); | 3041 | audit_mappings_page(vcpu, ent, va, level - 1); |
3013 | } else { | 3042 | } else { |
3014 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va); | 3043 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va); |
3015 | hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT; | 3044 | gfn_t gfn = gpa >> PAGE_SHIFT; |
3045 | pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn); | ||
3046 | hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT; | ||
3016 | 3047 | ||
3017 | if (is_shadow_present_pte(ent) | 3048 | if (is_shadow_present_pte(ent) |
3018 | && (ent & PT64_BASE_ADDR_MASK) != hpa) | 3049 | && (ent & PT64_BASE_ADDR_MASK) != hpa) |