Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * This module enables machines with Intel VT-x extensions to run virtual |
| 5 | * machines without emulation or binary translation. |
| 6 | * |
| 7 | * MMU support |
| 8 | * |
| 9 | * Copyright (C) 2006 Qumranet, Inc. |
| 10 | * |
| 11 | * Authors: |
| 12 | * Yaniv Kamay <yaniv@qumranet.com> |
| 13 | * Avi Kivity <avi@qumranet.com> |
| 14 | * |
| 15 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 16 | * the COPYING file in the top-level directory. |
| 17 | * |
| 18 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 19 | |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 21 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 22 | #include <linux/kvm_host.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 23 | #include <linux/types.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/mm.h> |
| 26 | #include <linux/highmem.h> |
| 27 | #include <linux/module.h> |
Izik Eidus | 448353c | 2007-11-26 14:08:14 +0200 | [diff] [blame] | 28 | #include <linux/swap.h> |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 29 | #include <linux/hugetlb.h> |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 30 | #include <linux/compiler.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 31 | |
| 32 | #include <asm/page.h> |
| 33 | #include <asm/cmpxchg.h> |
Avi Kivity | 4e54237 | 2007-11-21 14:08:40 +0200 | [diff] [blame] | 34 | #include <asm/io.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 35 | #include <asm/vmx.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 36 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 37 | /* |
| 38 | * When setting this variable to true it enables Two-Dimensional-Paging |
| 39 | * where the hardware walks 2 page tables: |
| 40 | * 1. the guest-virtual to guest-physical |
| 41 | * 2. while doing 1. it walks guest-physical to host-physical |
| 42 | * If the hardware supports that we don't need to do shadow paging. |
| 43 | */ |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 44 | bool tdp_enabled = false; |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 45 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 46 | #undef MMU_DEBUG |
| 47 | |
| 48 | #undef AUDIT |
| 49 | |
| 50 | #ifdef AUDIT |
| 51 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg); |
| 52 | #else |
| 53 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {} |
| 54 | #endif |
| 55 | |
| 56 | #ifdef MMU_DEBUG |
| 57 | |
| 58 | #define pgprintk(x...) do { if (dbg) printk(x); } while (0) |
| 59 | #define rmap_printk(x...) do { if (dbg) printk(x); } while (0) |
| 60 | |
| 61 | #else |
| 62 | |
| 63 | #define pgprintk(x...) do { } while (0) |
| 64 | #define rmap_printk(x...) do { } while (0) |
| 65 | |
| 66 | #endif |
| 67 | |
| 68 | #if defined(MMU_DEBUG) || defined(AUDIT) |
Avi Kivity | 6ada8cc | 2008-06-22 16:45:24 +0300 | [diff] [blame] | 69 | static int dbg = 0; |
| 70 | module_param(dbg, bool, 0644); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 71 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 72 | |
Marcelo Tosatti | 582801a | 2008-09-23 13:18:41 -0300 | [diff] [blame] | 73 | static int oos_shadow = 1; |
| 74 | module_param(oos_shadow, bool, 0644); |
| 75 | |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 76 | #ifndef MMU_DEBUG |
| 77 | #define ASSERT(x) do { } while (0) |
| 78 | #else |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 79 | #define ASSERT(x) \ |
| 80 | if (!(x)) { \ |
| 81 | printk(KERN_WARNING "assertion failed %s:%d: %s\n", \ |
| 82 | __FILE__, __LINE__, #x); \ |
| 83 | } |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 84 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 85 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 86 | #define PT_FIRST_AVAIL_BITS_SHIFT 9 |
| 87 | #define PT64_SECOND_AVAIL_BITS_SHIFT 52 |
| 88 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 89 | #define VALID_PAGE(x) ((x) != INVALID_PAGE) |
| 90 | |
| 91 | #define PT64_LEVEL_BITS 9 |
| 92 | |
| 93 | #define PT64_LEVEL_SHIFT(level) \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 94 | (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 95 | |
| 96 | #define PT64_LEVEL_MASK(level) \ |
| 97 | (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level)) |
| 98 | |
| 99 | #define PT64_INDEX(address, level)\ |
| 100 | (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1)) |
| 101 | |
| 102 | |
| 103 | #define PT32_LEVEL_BITS 10 |
| 104 | |
| 105 | #define PT32_LEVEL_SHIFT(level) \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 106 | (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 107 | |
| 108 | #define PT32_LEVEL_MASK(level) \ |
| 109 | (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level)) |
| 110 | |
| 111 | #define PT32_INDEX(address, level)\ |
| 112 | (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1)) |
| 113 | |
| 114 | |
Avi Kivity | 27aba76 | 2007-03-09 13:04:31 +0200 | [diff] [blame] | 115 | #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 116 | #define PT64_DIR_BASE_ADDR_MASK \ |
| 117 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1)) |
| 118 | |
| 119 | #define PT32_BASE_ADDR_MASK PAGE_MASK |
| 120 | #define PT32_DIR_BASE_ADDR_MASK \ |
| 121 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1)) |
| 122 | |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 123 | #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \ |
| 124 | | PT64_NX_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 125 | |
| 126 | #define PFERR_PRESENT_MASK (1U << 0) |
| 127 | #define PFERR_WRITE_MASK (1U << 1) |
| 128 | #define PFERR_USER_MASK (1U << 2) |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame^] | 129 | #define PFERR_RSVD_MASK (1U << 3) |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 130 | #define PFERR_FETCH_MASK (1U << 4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 131 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 132 | #define PT_DIRECTORY_LEVEL 2 |
| 133 | #define PT_PAGE_TABLE_LEVEL 1 |
| 134 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 135 | #define RMAP_EXT 4 |
| 136 | |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 137 | #define ACC_EXEC_MASK 1 |
| 138 | #define ACC_WRITE_MASK PT_WRITABLE_MASK |
| 139 | #define ACC_USER_MASK PT_USER_MASK |
| 140 | #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK) |
| 141 | |
Avi Kivity | 135f8c2 | 2008-08-21 17:49:56 +0300 | [diff] [blame] | 142 | #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level) |
| 143 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 144 | struct kvm_rmap_desc { |
| 145 | u64 *shadow_ptes[RMAP_EXT]; |
| 146 | struct kvm_rmap_desc *more; |
| 147 | }; |
| 148 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 149 | struct kvm_shadow_walk_iterator { |
| 150 | u64 addr; |
| 151 | hpa_t shadow_addr; |
| 152 | int level; |
| 153 | u64 *sptep; |
| 154 | unsigned index; |
| 155 | }; |
| 156 | |
| 157 | #define for_each_shadow_entry(_vcpu, _addr, _walker) \ |
| 158 | for (shadow_walk_init(&(_walker), _vcpu, _addr); \ |
| 159 | shadow_walk_okay(&(_walker)); \ |
| 160 | shadow_walk_next(&(_walker))) |
| 161 | |
| 162 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 163 | struct kvm_unsync_walk { |
| 164 | int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk); |
| 165 | }; |
| 166 | |
Marcelo Tosatti | ad8cfbe | 2008-09-23 13:18:36 -0300 | [diff] [blame] | 167 | typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp); |
| 168 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 169 | static struct kmem_cache *pte_chain_cache; |
| 170 | static struct kmem_cache *rmap_desc_cache; |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 171 | static struct kmem_cache *mmu_page_header_cache; |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 172 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 173 | static u64 __read_mostly shadow_trap_nonpresent_pte; |
| 174 | static u64 __read_mostly shadow_notrap_nonpresent_pte; |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 175 | static u64 __read_mostly shadow_base_present_pte; |
| 176 | static u64 __read_mostly shadow_nx_mask; |
| 177 | static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */ |
| 178 | static u64 __read_mostly shadow_user_mask; |
| 179 | static u64 __read_mostly shadow_accessed_mask; |
| 180 | static u64 __read_mostly shadow_dirty_mask; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 181 | static u64 __read_mostly shadow_mt_mask; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 182 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame^] | 183 | static inline u64 rsvd_bits(int s, int e) |
| 184 | { |
| 185 | return ((1ULL << (e - s + 1)) - 1) << s; |
| 186 | } |
| 187 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 188 | void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte) |
| 189 | { |
| 190 | shadow_trap_nonpresent_pte = trap_pte; |
| 191 | shadow_notrap_nonpresent_pte = notrap_pte; |
| 192 | } |
| 193 | EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes); |
| 194 | |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 195 | void kvm_mmu_set_base_ptes(u64 base_pte) |
| 196 | { |
| 197 | shadow_base_present_pte = base_pte; |
| 198 | } |
| 199 | EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes); |
| 200 | |
| 201 | void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask, |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 202 | u64 dirty_mask, u64 nx_mask, u64 x_mask, u64 mt_mask) |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 203 | { |
| 204 | shadow_user_mask = user_mask; |
| 205 | shadow_accessed_mask = accessed_mask; |
| 206 | shadow_dirty_mask = dirty_mask; |
| 207 | shadow_nx_mask = nx_mask; |
| 208 | shadow_x_mask = x_mask; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 209 | shadow_mt_mask = mt_mask; |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 210 | } |
| 211 | EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes); |
| 212 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 213 | static int is_write_protection(struct kvm_vcpu *vcpu) |
| 214 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 215 | return vcpu->arch.cr0 & X86_CR0_WP; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static int is_cpuid_PSE36(void) |
| 219 | { |
| 220 | return 1; |
| 221 | } |
| 222 | |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 223 | static int is_nx(struct kvm_vcpu *vcpu) |
| 224 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 225 | return vcpu->arch.shadow_efer & EFER_NX; |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 228 | static int is_present_pte(unsigned long pte) |
| 229 | { |
| 230 | return pte & PT_PRESENT_MASK; |
| 231 | } |
| 232 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 233 | static int is_shadow_present_pte(u64 pte) |
| 234 | { |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 235 | return pte != shadow_trap_nonpresent_pte |
| 236 | && pte != shadow_notrap_nonpresent_pte; |
| 237 | } |
| 238 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 239 | static int is_large_pte(u64 pte) |
| 240 | { |
| 241 | return pte & PT_PAGE_SIZE_MASK; |
| 242 | } |
| 243 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 244 | static int is_writeble_pte(unsigned long pte) |
| 245 | { |
| 246 | return pte & PT_WRITABLE_MASK; |
| 247 | } |
| 248 | |
Avi Kivity | e3c5e7ec | 2007-10-11 12:32:30 +0200 | [diff] [blame] | 249 | static int is_dirty_pte(unsigned long pte) |
| 250 | { |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 251 | return pte & shadow_dirty_mask; |
Avi Kivity | e3c5e7ec | 2007-10-11 12:32:30 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 254 | static int is_rmap_pte(u64 pte) |
| 255 | { |
Avi Kivity | 4b1a80f | 2008-03-23 12:18:19 +0200 | [diff] [blame] | 256 | return is_shadow_present_pte(pte); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 259 | static pfn_t spte_to_pfn(u64 pte) |
Avi Kivity | 0b49ea8 | 2008-03-23 15:06:23 +0200 | [diff] [blame] | 260 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 261 | return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; |
Avi Kivity | 0b49ea8 | 2008-03-23 15:06:23 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Avi Kivity | da92852 | 2007-11-21 13:54:47 +0200 | [diff] [blame] | 264 | static gfn_t pse36_gfn_delta(u32 gpte) |
| 265 | { |
| 266 | int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT; |
| 267 | |
| 268 | return (gpte & PT32_DIR_PSE36_MASK) << shift; |
| 269 | } |
| 270 | |
Avi Kivity | e663ee6 | 2007-05-31 15:46:04 +0300 | [diff] [blame] | 271 | static void set_shadow_pte(u64 *sptep, u64 spte) |
| 272 | { |
| 273 | #ifdef CONFIG_X86_64 |
| 274 | set_64bit((unsigned long *)sptep, spte); |
| 275 | #else |
| 276 | set_64bit((unsigned long long *)sptep, spte); |
| 277 | #endif |
| 278 | } |
| 279 | |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 280 | static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache, |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 281 | struct kmem_cache *base_cache, int min) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 282 | { |
| 283 | void *obj; |
| 284 | |
| 285 | if (cache->nobjs >= min) |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 286 | return 0; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 287 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 288 | obj = kmem_cache_zalloc(base_cache, GFP_KERNEL); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 289 | if (!obj) |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 290 | return -ENOMEM; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 291 | cache->objects[cache->nobjs++] = obj; |
| 292 | } |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 293 | return 0; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc) |
| 297 | { |
| 298 | while (mc->nobjs) |
| 299 | kfree(mc->objects[--mc->nobjs]); |
| 300 | } |
| 301 | |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 302 | static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache, |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 303 | int min) |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 304 | { |
| 305 | struct page *page; |
| 306 | |
| 307 | if (cache->nobjs >= min) |
| 308 | return 0; |
| 309 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 310 | page = alloc_page(GFP_KERNEL); |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 311 | if (!page) |
| 312 | return -ENOMEM; |
| 313 | set_page_private(page, 0); |
| 314 | cache->objects[cache->nobjs++] = page_address(page); |
| 315 | } |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc) |
| 320 | { |
| 321 | while (mc->nobjs) |
Avi Kivity | c4d198d | 2007-07-21 09:06:46 +0300 | [diff] [blame] | 322 | free_page((unsigned long)mc->objects[--mc->nobjs]); |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 323 | } |
| 324 | |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 325 | static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu) |
| 326 | { |
| 327 | int r; |
| 328 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 329 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache, |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 330 | pte_chain_cache, 4); |
| 331 | if (r) |
| 332 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 333 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, |
Marcelo Tosatti | c41ef34 | 2008-10-28 18:16:58 -0200 | [diff] [blame] | 334 | rmap_desc_cache, 4); |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 335 | if (r) |
| 336 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 337 | r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8); |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 338 | if (r) |
| 339 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 340 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache, |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 341 | mmu_page_header_cache, 4); |
| 342 | out: |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 343 | return r; |
| 344 | } |
| 345 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 346 | static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) |
| 347 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 348 | mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache); |
| 349 | mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache); |
| 350 | mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache); |
| 351 | mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc, |
| 355 | size_t size) |
| 356 | { |
| 357 | void *p; |
| 358 | |
| 359 | BUG_ON(!mc->nobjs); |
| 360 | p = mc->objects[--mc->nobjs]; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 361 | return p; |
| 362 | } |
| 363 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 364 | static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu) |
| 365 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 366 | return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache, |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 367 | sizeof(struct kvm_pte_chain)); |
| 368 | } |
| 369 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 370 | static void mmu_free_pte_chain(struct kvm_pte_chain *pc) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 371 | { |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 372 | kfree(pc); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu) |
| 376 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 377 | return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache, |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 378 | sizeof(struct kvm_rmap_desc)); |
| 379 | } |
| 380 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 381 | static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 382 | { |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 383 | kfree(rd); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 386 | /* |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 387 | * Return the pointer to the largepage write count for a given |
| 388 | * gfn, handling slots that are not large page aligned. |
| 389 | */ |
| 390 | static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot) |
| 391 | { |
| 392 | unsigned long idx; |
| 393 | |
| 394 | idx = (gfn / KVM_PAGES_PER_HPAGE) - |
| 395 | (slot->base_gfn / KVM_PAGES_PER_HPAGE); |
| 396 | return &slot->lpage_info[idx].write_count; |
| 397 | } |
| 398 | |
| 399 | static void account_shadowed(struct kvm *kvm, gfn_t gfn) |
| 400 | { |
| 401 | int *write_count; |
| 402 | |
Izik Eidus | 2843099 | 2008-10-03 17:40:32 +0300 | [diff] [blame] | 403 | gfn = unalias_gfn(kvm, gfn); |
| 404 | write_count = slot_largepage_idx(gfn, |
| 405 | gfn_to_memslot_unaliased(kvm, gfn)); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 406 | *write_count += 1; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn) |
| 410 | { |
| 411 | int *write_count; |
| 412 | |
Izik Eidus | 2843099 | 2008-10-03 17:40:32 +0300 | [diff] [blame] | 413 | gfn = unalias_gfn(kvm, gfn); |
| 414 | write_count = slot_largepage_idx(gfn, |
| 415 | gfn_to_memslot_unaliased(kvm, gfn)); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 416 | *write_count -= 1; |
| 417 | WARN_ON(*write_count < 0); |
| 418 | } |
| 419 | |
| 420 | static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn) |
| 421 | { |
Izik Eidus | 2843099 | 2008-10-03 17:40:32 +0300 | [diff] [blame] | 422 | struct kvm_memory_slot *slot; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 423 | int *largepage_idx; |
| 424 | |
Izik Eidus | 2843099 | 2008-10-03 17:40:32 +0300 | [diff] [blame] | 425 | gfn = unalias_gfn(kvm, gfn); |
| 426 | slot = gfn_to_memslot_unaliased(kvm, gfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 427 | if (slot) { |
| 428 | largepage_idx = slot_largepage_idx(gfn, slot); |
| 429 | return *largepage_idx; |
| 430 | } |
| 431 | |
| 432 | return 1; |
| 433 | } |
| 434 | |
| 435 | static int host_largepage_backed(struct kvm *kvm, gfn_t gfn) |
| 436 | { |
| 437 | struct vm_area_struct *vma; |
| 438 | unsigned long addr; |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 439 | int ret = 0; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 440 | |
| 441 | addr = gfn_to_hva(kvm, gfn); |
| 442 | if (kvm_is_error_hva(addr)) |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 443 | return ret; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 444 | |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 445 | down_read(¤t->mm->mmap_sem); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 446 | vma = find_vma(current->mm, addr); |
| 447 | if (vma && is_vm_hugetlb_page(vma)) |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 448 | ret = 1; |
| 449 | up_read(¤t->mm->mmap_sem); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 450 | |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 451 | return ret; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn) |
| 455 | { |
| 456 | struct kvm_memory_slot *slot; |
| 457 | |
| 458 | if (has_wrprotected_page(vcpu->kvm, large_gfn)) |
| 459 | return 0; |
| 460 | |
| 461 | if (!host_largepage_backed(vcpu->kvm, large_gfn)) |
| 462 | return 0; |
| 463 | |
| 464 | slot = gfn_to_memslot(vcpu->kvm, large_gfn); |
| 465 | if (slot && slot->dirty_bitmap) |
| 466 | return 0; |
| 467 | |
| 468 | return 1; |
| 469 | } |
| 470 | |
| 471 | /* |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 472 | * Take gfn and return the reverse mapping to it. |
| 473 | * Note: gfn must be unaliased before this function get called |
| 474 | */ |
| 475 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 476 | static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage) |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 477 | { |
| 478 | struct kvm_memory_slot *slot; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 479 | unsigned long idx; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 480 | |
| 481 | slot = gfn_to_memslot(kvm, gfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 482 | if (!lpage) |
| 483 | return &slot->rmap[gfn - slot->base_gfn]; |
| 484 | |
| 485 | idx = (gfn / KVM_PAGES_PER_HPAGE) - |
| 486 | (slot->base_gfn / KVM_PAGES_PER_HPAGE); |
| 487 | |
| 488 | return &slot->lpage_info[idx].rmap_pde; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /* |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 492 | * Reverse mapping data structures: |
| 493 | * |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 494 | * If rmapp bit zero is zero, then rmapp point to the shadw page table entry |
| 495 | * that points to page_address(page). |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 496 | * |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 497 | * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc |
| 498 | * containing more mappings. |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 499 | */ |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 500 | static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 501 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 502 | struct kvm_mmu_page *sp; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 503 | struct kvm_rmap_desc *desc; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 504 | unsigned long *rmapp; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 505 | int i; |
| 506 | |
| 507 | if (!is_rmap_pte(*spte)) |
| 508 | return; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 509 | gfn = unalias_gfn(vcpu->kvm, gfn); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 510 | sp = page_header(__pa(spte)); |
| 511 | sp->gfns[spte - sp->spt] = gfn; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 512 | rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 513 | if (!*rmapp) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 514 | rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 515 | *rmapp = (unsigned long)spte; |
| 516 | } else if (!(*rmapp & 1)) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 517 | rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 518 | desc = mmu_alloc_rmap_desc(vcpu); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 519 | desc->shadow_ptes[0] = (u64 *)*rmapp; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 520 | desc->shadow_ptes[1] = spte; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 521 | *rmapp = (unsigned long)desc | 1; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 522 | } else { |
| 523 | rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 524 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 525 | while (desc->shadow_ptes[RMAP_EXT-1] && desc->more) |
| 526 | desc = desc->more; |
| 527 | if (desc->shadow_ptes[RMAP_EXT-1]) { |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 528 | desc->more = mmu_alloc_rmap_desc(vcpu); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 529 | desc = desc->more; |
| 530 | } |
| 531 | for (i = 0; desc->shadow_ptes[i]; ++i) |
| 532 | ; |
| 533 | desc->shadow_ptes[i] = spte; |
| 534 | } |
| 535 | } |
| 536 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 537 | static void rmap_desc_remove_entry(unsigned long *rmapp, |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 538 | struct kvm_rmap_desc *desc, |
| 539 | int i, |
| 540 | struct kvm_rmap_desc *prev_desc) |
| 541 | { |
| 542 | int j; |
| 543 | |
| 544 | for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j) |
| 545 | ; |
| 546 | desc->shadow_ptes[i] = desc->shadow_ptes[j]; |
Al Viro | 11718b4d | 2007-02-09 16:39:20 +0000 | [diff] [blame] | 547 | desc->shadow_ptes[j] = NULL; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 548 | if (j != 0) |
| 549 | return; |
| 550 | if (!prev_desc && !desc->more) |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 551 | *rmapp = (unsigned long)desc->shadow_ptes[0]; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 552 | else |
| 553 | if (prev_desc) |
| 554 | prev_desc->more = desc->more; |
| 555 | else |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 556 | *rmapp = (unsigned long)desc->more | 1; |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 557 | mmu_free_rmap_desc(desc); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 558 | } |
| 559 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 560 | static void rmap_remove(struct kvm *kvm, u64 *spte) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 561 | { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 562 | struct kvm_rmap_desc *desc; |
| 563 | struct kvm_rmap_desc *prev_desc; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 564 | struct kvm_mmu_page *sp; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 565 | pfn_t pfn; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 566 | unsigned long *rmapp; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 567 | int i; |
| 568 | |
| 569 | if (!is_rmap_pte(*spte)) |
| 570 | return; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 571 | sp = page_header(__pa(spte)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 572 | pfn = spte_to_pfn(*spte); |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 573 | if (*spte & shadow_accessed_mask) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 574 | kvm_set_pfn_accessed(pfn); |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 575 | if (is_writeble_pte(*spte)) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 576 | kvm_release_pfn_dirty(pfn); |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 577 | else |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 578 | kvm_release_pfn_clean(pfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 579 | rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte)); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 580 | if (!*rmapp) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 581 | printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte); |
| 582 | BUG(); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 583 | } else if (!(*rmapp & 1)) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 584 | rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 585 | if ((u64 *)*rmapp != spte) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 586 | printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n", |
| 587 | spte, *spte); |
| 588 | BUG(); |
| 589 | } |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 590 | *rmapp = 0; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 591 | } else { |
| 592 | rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 593 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 594 | prev_desc = NULL; |
| 595 | while (desc) { |
| 596 | for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) |
| 597 | if (desc->shadow_ptes[i] == spte) { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 598 | rmap_desc_remove_entry(rmapp, |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 599 | desc, i, |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 600 | prev_desc); |
| 601 | return; |
| 602 | } |
| 603 | prev_desc = desc; |
| 604 | desc = desc->more; |
| 605 | } |
| 606 | BUG(); |
| 607 | } |
| 608 | } |
| 609 | |
Izik Eidus | 98348e9 | 2007-10-16 14:42:30 +0200 | [diff] [blame] | 610 | static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte) |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 611 | { |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 612 | struct kvm_rmap_desc *desc; |
Izik Eidus | 98348e9 | 2007-10-16 14:42:30 +0200 | [diff] [blame] | 613 | struct kvm_rmap_desc *prev_desc; |
| 614 | u64 *prev_spte; |
| 615 | int i; |
| 616 | |
| 617 | if (!*rmapp) |
| 618 | return NULL; |
| 619 | else if (!(*rmapp & 1)) { |
| 620 | if (!spte) |
| 621 | return (u64 *)*rmapp; |
| 622 | return NULL; |
| 623 | } |
| 624 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
| 625 | prev_desc = NULL; |
| 626 | prev_spte = NULL; |
| 627 | while (desc) { |
| 628 | for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) { |
| 629 | if (prev_spte == spte) |
| 630 | return desc->shadow_ptes[i]; |
| 631 | prev_spte = desc->shadow_ptes[i]; |
| 632 | } |
| 633 | desc = desc->more; |
| 634 | } |
| 635 | return NULL; |
| 636 | } |
| 637 | |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 638 | static int rmap_write_protect(struct kvm *kvm, u64 gfn) |
Izik Eidus | 98348e9 | 2007-10-16 14:42:30 +0200 | [diff] [blame] | 639 | { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 640 | unsigned long *rmapp; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 641 | u64 *spte; |
Eddie Dong | caa5b8a | 2007-12-18 06:08:27 +0800 | [diff] [blame] | 642 | int write_protected = 0; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 643 | |
Anthony Liguori | 4a4c992 | 2007-10-10 20:08:41 -0500 | [diff] [blame] | 644 | gfn = unalias_gfn(kvm, gfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 645 | rmapp = gfn_to_rmap(kvm, gfn, 0); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 646 | |
Izik Eidus | 98348e9 | 2007-10-16 14:42:30 +0200 | [diff] [blame] | 647 | spte = rmap_next(kvm, rmapp, NULL); |
| 648 | while (spte) { |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 649 | BUG_ON(!spte); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 650 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 651 | rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte); |
Eddie Dong | caa5b8a | 2007-12-18 06:08:27 +0800 | [diff] [blame] | 652 | if (is_writeble_pte(*spte)) { |
Izik Eidus | 9647c14 | 2007-10-16 14:43:46 +0200 | [diff] [blame] | 653 | set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK); |
Eddie Dong | caa5b8a | 2007-12-18 06:08:27 +0800 | [diff] [blame] | 654 | write_protected = 1; |
| 655 | } |
Izik Eidus | 9647c14 | 2007-10-16 14:43:46 +0200 | [diff] [blame] | 656 | spte = rmap_next(kvm, rmapp, spte); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 657 | } |
Izik Eidus | 855149a | 2008-03-20 18:17:24 +0200 | [diff] [blame] | 658 | if (write_protected) { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 659 | pfn_t pfn; |
Izik Eidus | 855149a | 2008-03-20 18:17:24 +0200 | [diff] [blame] | 660 | |
| 661 | spte = rmap_next(kvm, rmapp, NULL); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 662 | pfn = spte_to_pfn(*spte); |
| 663 | kvm_set_pfn_dirty(pfn); |
Izik Eidus | 855149a | 2008-03-20 18:17:24 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 666 | /* check for huge page mappings */ |
| 667 | rmapp = gfn_to_rmap(kvm, gfn, 1); |
| 668 | spte = rmap_next(kvm, rmapp, NULL); |
| 669 | while (spte) { |
| 670 | BUG_ON(!spte); |
| 671 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
| 672 | BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)); |
| 673 | pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn); |
| 674 | if (is_writeble_pte(*spte)) { |
| 675 | rmap_remove(kvm, spte); |
| 676 | --kvm->stat.lpages; |
| 677 | set_shadow_pte(spte, shadow_trap_nonpresent_pte); |
Marcelo Tosatti | 6597ca0 | 2008-06-08 01:48:53 -0300 | [diff] [blame] | 678 | spte = NULL; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 679 | write_protected = 1; |
| 680 | } |
| 681 | spte = rmap_next(kvm, rmapp, spte); |
| 682 | } |
| 683 | |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 684 | return write_protected; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 687 | static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp) |
| 688 | { |
| 689 | u64 *spte; |
| 690 | int need_tlb_flush = 0; |
| 691 | |
| 692 | while ((spte = rmap_next(kvm, rmapp, NULL))) { |
| 693 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
| 694 | rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte); |
| 695 | rmap_remove(kvm, spte); |
| 696 | set_shadow_pte(spte, shadow_trap_nonpresent_pte); |
| 697 | need_tlb_flush = 1; |
| 698 | } |
| 699 | return need_tlb_flush; |
| 700 | } |
| 701 | |
| 702 | static int kvm_handle_hva(struct kvm *kvm, unsigned long hva, |
| 703 | int (*handler)(struct kvm *kvm, unsigned long *rmapp)) |
| 704 | { |
| 705 | int i; |
| 706 | int retval = 0; |
| 707 | |
| 708 | /* |
| 709 | * If mmap_sem isn't taken, we can look the memslots with only |
| 710 | * the mmu_lock by skipping over the slots with userspace_addr == 0. |
| 711 | */ |
| 712 | for (i = 0; i < kvm->nmemslots; i++) { |
| 713 | struct kvm_memory_slot *memslot = &kvm->memslots[i]; |
| 714 | unsigned long start = memslot->userspace_addr; |
| 715 | unsigned long end; |
| 716 | |
| 717 | /* mmu_lock protects userspace_addr */ |
| 718 | if (!start) |
| 719 | continue; |
| 720 | |
| 721 | end = start + (memslot->npages << PAGE_SHIFT); |
| 722 | if (hva >= start && hva < end) { |
| 723 | gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT; |
| 724 | retval |= handler(kvm, &memslot->rmap[gfn_offset]); |
| 725 | retval |= handler(kvm, |
| 726 | &memslot->lpage_info[ |
| 727 | gfn_offset / |
| 728 | KVM_PAGES_PER_HPAGE].rmap_pde); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | return retval; |
| 733 | } |
| 734 | |
| 735 | int kvm_unmap_hva(struct kvm *kvm, unsigned long hva) |
| 736 | { |
| 737 | return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp); |
| 738 | } |
| 739 | |
| 740 | static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp) |
| 741 | { |
| 742 | u64 *spte; |
| 743 | int young = 0; |
| 744 | |
Sheng Yang | 534e38b | 2008-09-08 15:12:30 +0800 | [diff] [blame] | 745 | /* always return old for EPT */ |
| 746 | if (!shadow_accessed_mask) |
| 747 | return 0; |
| 748 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 749 | spte = rmap_next(kvm, rmapp, NULL); |
| 750 | while (spte) { |
| 751 | int _young; |
| 752 | u64 _spte = *spte; |
| 753 | BUG_ON(!(_spte & PT_PRESENT_MASK)); |
| 754 | _young = _spte & PT_ACCESSED_MASK; |
| 755 | if (_young) { |
| 756 | young = 1; |
| 757 | clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte); |
| 758 | } |
| 759 | spte = rmap_next(kvm, rmapp, spte); |
| 760 | } |
| 761 | return young; |
| 762 | } |
| 763 | |
| 764 | int kvm_age_hva(struct kvm *kvm, unsigned long hva) |
| 765 | { |
| 766 | return kvm_handle_hva(kvm, hva, kvm_age_rmapp); |
| 767 | } |
| 768 | |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 769 | #ifdef MMU_DEBUG |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 770 | static int is_empty_shadow_page(u64 *spt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 771 | { |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 772 | u64 *pos; |
| 773 | u64 *end; |
| 774 | |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 775 | for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++) |
Avi Kivity | 3c91551 | 2008-05-20 16:21:13 +0300 | [diff] [blame] | 776 | if (is_shadow_present_pte(*pos)) { |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 777 | printk(KERN_ERR "%s: %p %llx\n", __func__, |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 778 | pos, *pos); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 779 | return 0; |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 780 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 781 | return 1; |
| 782 | } |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 783 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 784 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 785 | static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
Avi Kivity | 260746c | 2007-01-05 16:36:49 -0800 | [diff] [blame] | 786 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 787 | ASSERT(is_empty_shadow_page(sp->spt)); |
| 788 | list_del(&sp->link); |
| 789 | __free_page(virt_to_page(sp->spt)); |
| 790 | __free_page(virt_to_page(sp->gfns)); |
| 791 | kfree(sp); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 792 | ++kvm->arch.n_free_mmu_pages; |
Avi Kivity | 260746c | 2007-01-05 16:36:49 -0800 | [diff] [blame] | 793 | } |
| 794 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 795 | static unsigned kvm_page_table_hashfn(gfn_t gfn) |
| 796 | { |
Dong, Eddie | 1ae0a13 | 2008-01-07 13:20:25 +0200 | [diff] [blame] | 797 | return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 800 | static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, |
| 801 | u64 *parent_pte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 802 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 803 | struct kvm_mmu_page *sp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 804 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 805 | sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp); |
| 806 | sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE); |
| 807 | sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 808 | set_page_private(virt_to_page(sp->spt), (unsigned long)sp); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 809 | list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages); |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 810 | INIT_LIST_HEAD(&sp->oos_link); |
Sheng Yang | 291f26b | 2008-10-16 17:30:57 +0800 | [diff] [blame] | 811 | bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 812 | sp->multimapped = 0; |
| 813 | sp->parent_pte = parent_pte; |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 814 | --vcpu->kvm->arch.n_free_mmu_pages; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 815 | return sp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 818 | static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 819 | struct kvm_mmu_page *sp, u64 *parent_pte) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 820 | { |
| 821 | struct kvm_pte_chain *pte_chain; |
| 822 | struct hlist_node *node; |
| 823 | int i; |
| 824 | |
| 825 | if (!parent_pte) |
| 826 | return; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 827 | if (!sp->multimapped) { |
| 828 | u64 *old = sp->parent_pte; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 829 | |
| 830 | if (!old) { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 831 | sp->parent_pte = parent_pte; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 832 | return; |
| 833 | } |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 834 | sp->multimapped = 1; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 835 | pte_chain = mmu_alloc_pte_chain(vcpu); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 836 | INIT_HLIST_HEAD(&sp->parent_ptes); |
| 837 | hlist_add_head(&pte_chain->link, &sp->parent_ptes); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 838 | pte_chain->parent_ptes[0] = old; |
| 839 | } |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 840 | hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 841 | if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1]) |
| 842 | continue; |
| 843 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) |
| 844 | if (!pte_chain->parent_ptes[i]) { |
| 845 | pte_chain->parent_ptes[i] = parent_pte; |
| 846 | return; |
| 847 | } |
| 848 | } |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 849 | pte_chain = mmu_alloc_pte_chain(vcpu); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 850 | BUG_ON(!pte_chain); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 851 | hlist_add_head(&pte_chain->link, &sp->parent_ptes); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 852 | pte_chain->parent_ptes[0] = parent_pte; |
| 853 | } |
| 854 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 855 | static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp, |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 856 | u64 *parent_pte) |
| 857 | { |
| 858 | struct kvm_pte_chain *pte_chain; |
| 859 | struct hlist_node *node; |
| 860 | int i; |
| 861 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 862 | if (!sp->multimapped) { |
| 863 | BUG_ON(sp->parent_pte != parent_pte); |
| 864 | sp->parent_pte = NULL; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 865 | return; |
| 866 | } |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 867 | hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 868 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) { |
| 869 | if (!pte_chain->parent_ptes[i]) |
| 870 | break; |
| 871 | if (pte_chain->parent_ptes[i] != parent_pte) |
| 872 | continue; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 873 | while (i + 1 < NR_PTE_CHAIN_ENTRIES |
| 874 | && pte_chain->parent_ptes[i + 1]) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 875 | pte_chain->parent_ptes[i] |
| 876 | = pte_chain->parent_ptes[i + 1]; |
| 877 | ++i; |
| 878 | } |
| 879 | pte_chain->parent_ptes[i] = NULL; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 880 | if (i == 0) { |
| 881 | hlist_del(&pte_chain->link); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 882 | mmu_free_pte_chain(pte_chain); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 883 | if (hlist_empty(&sp->parent_ptes)) { |
| 884 | sp->multimapped = 0; |
| 885 | sp->parent_pte = NULL; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 886 | } |
| 887 | } |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 888 | return; |
| 889 | } |
| 890 | BUG(); |
| 891 | } |
| 892 | |
Marcelo Tosatti | ad8cfbe | 2008-09-23 13:18:36 -0300 | [diff] [blame] | 893 | |
| 894 | static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
| 895 | mmu_parent_walk_fn fn) |
| 896 | { |
| 897 | struct kvm_pte_chain *pte_chain; |
| 898 | struct hlist_node *node; |
| 899 | struct kvm_mmu_page *parent_sp; |
| 900 | int i; |
| 901 | |
| 902 | if (!sp->multimapped && sp->parent_pte) { |
| 903 | parent_sp = page_header(__pa(sp->parent_pte)); |
| 904 | fn(vcpu, parent_sp); |
| 905 | mmu_parent_walk(vcpu, parent_sp, fn); |
| 906 | return; |
| 907 | } |
| 908 | hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) |
| 909 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) { |
| 910 | if (!pte_chain->parent_ptes[i]) |
| 911 | break; |
| 912 | parent_sp = page_header(__pa(pte_chain->parent_ptes[i])); |
| 913 | fn(vcpu, parent_sp); |
| 914 | mmu_parent_walk(vcpu, parent_sp, fn); |
| 915 | } |
| 916 | } |
| 917 | |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 918 | static void kvm_mmu_update_unsync_bitmap(u64 *spte) |
| 919 | { |
| 920 | unsigned int index; |
| 921 | struct kvm_mmu_page *sp = page_header(__pa(spte)); |
| 922 | |
| 923 | index = spte - sp->spt; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 924 | if (!__test_and_set_bit(index, sp->unsync_child_bitmap)) |
| 925 | sp->unsync_children++; |
| 926 | WARN_ON(!sp->unsync_children); |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp) |
| 930 | { |
| 931 | struct kvm_pte_chain *pte_chain; |
| 932 | struct hlist_node *node; |
| 933 | int i; |
| 934 | |
| 935 | if (!sp->parent_pte) |
| 936 | return; |
| 937 | |
| 938 | if (!sp->multimapped) { |
| 939 | kvm_mmu_update_unsync_bitmap(sp->parent_pte); |
| 940 | return; |
| 941 | } |
| 942 | |
| 943 | hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) |
| 944 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) { |
| 945 | if (!pte_chain->parent_ptes[i]) |
| 946 | break; |
| 947 | kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) |
| 952 | { |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 953 | kvm_mmu_update_parents_unsync(sp); |
| 954 | return 1; |
| 955 | } |
| 956 | |
| 957 | static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu, |
| 958 | struct kvm_mmu_page *sp) |
| 959 | { |
| 960 | mmu_parent_walk(vcpu, sp, unsync_walk_fn); |
| 961 | kvm_mmu_update_parents_unsync(sp); |
| 962 | } |
| 963 | |
Avi Kivity | d761a50 | 2008-05-29 14:55:03 +0300 | [diff] [blame] | 964 | static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu, |
| 965 | struct kvm_mmu_page *sp) |
| 966 | { |
| 967 | int i; |
| 968 | |
| 969 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
| 970 | sp->spt[i] = shadow_trap_nonpresent_pte; |
| 971 | } |
| 972 | |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 973 | static int nonpaging_sync_page(struct kvm_vcpu *vcpu, |
| 974 | struct kvm_mmu_page *sp) |
| 975 | { |
| 976 | return 1; |
| 977 | } |
| 978 | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 979 | static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva) |
| 980 | { |
| 981 | } |
| 982 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 983 | #define KVM_PAGE_ARRAY_NR 16 |
| 984 | |
| 985 | struct kvm_mmu_pages { |
| 986 | struct mmu_page_and_offset { |
| 987 | struct kvm_mmu_page *sp; |
| 988 | unsigned int idx; |
| 989 | } page[KVM_PAGE_ARRAY_NR]; |
| 990 | unsigned int nr; |
| 991 | }; |
| 992 | |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 993 | #define for_each_unsync_children(bitmap, idx) \ |
| 994 | for (idx = find_first_bit(bitmap, 512); \ |
| 995 | idx < 512; \ |
| 996 | idx = find_next_bit(bitmap, 512, idx+1)) |
| 997 | |
Hannes Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 998 | static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp, |
| 999 | int idx) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1000 | { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1001 | int i; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1002 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1003 | if (sp->unsync) |
| 1004 | for (i=0; i < pvec->nr; i++) |
| 1005 | if (pvec->page[i].sp == sp) |
| 1006 | return 0; |
| 1007 | |
| 1008 | pvec->page[pvec->nr].sp = sp; |
| 1009 | pvec->page[pvec->nr].idx = idx; |
| 1010 | pvec->nr++; |
| 1011 | return (pvec->nr == KVM_PAGE_ARRAY_NR); |
| 1012 | } |
| 1013 | |
| 1014 | static int __mmu_unsync_walk(struct kvm_mmu_page *sp, |
| 1015 | struct kvm_mmu_pages *pvec) |
| 1016 | { |
| 1017 | int i, ret, nr_unsync_leaf = 0; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1018 | |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 1019 | for_each_unsync_children(sp->unsync_child_bitmap, i) { |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1020 | u64 ent = sp->spt[i]; |
| 1021 | |
Marcelo Tosatti | 8791723 | 2008-12-22 18:49:30 -0200 | [diff] [blame] | 1022 | if (is_shadow_present_pte(ent) && !is_large_pte(ent)) { |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1023 | struct kvm_mmu_page *child; |
| 1024 | child = page_header(ent & PT64_BASE_ADDR_MASK); |
| 1025 | |
| 1026 | if (child->unsync_children) { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1027 | if (mmu_pages_add(pvec, child, i)) |
| 1028 | return -ENOSPC; |
| 1029 | |
| 1030 | ret = __mmu_unsync_walk(child, pvec); |
| 1031 | if (!ret) |
| 1032 | __clear_bit(i, sp->unsync_child_bitmap); |
| 1033 | else if (ret > 0) |
| 1034 | nr_unsync_leaf += ret; |
| 1035 | else |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1036 | return ret; |
| 1037 | } |
| 1038 | |
| 1039 | if (child->unsync) { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1040 | nr_unsync_leaf++; |
| 1041 | if (mmu_pages_add(pvec, child, i)) |
| 1042 | return -ENOSPC; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 1047 | if (find_first_bit(sp->unsync_child_bitmap, 512) == 512) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1048 | sp->unsync_children = 0; |
| 1049 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1050 | return nr_unsync_leaf; |
| 1051 | } |
| 1052 | |
| 1053 | static int mmu_unsync_walk(struct kvm_mmu_page *sp, |
| 1054 | struct kvm_mmu_pages *pvec) |
| 1055 | { |
| 1056 | if (!sp->unsync_children) |
| 1057 | return 0; |
| 1058 | |
| 1059 | mmu_pages_add(pvec, sp, 0); |
| 1060 | return __mmu_unsync_walk(sp, pvec); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1061 | } |
| 1062 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1063 | static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1064 | { |
| 1065 | unsigned index; |
| 1066 | struct hlist_head *bucket; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1067 | struct kvm_mmu_page *sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1068 | struct hlist_node *node; |
| 1069 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1070 | pgprintk("%s: looking for gfn %lx\n", __func__, gfn); |
Dong, Eddie | 1ae0a13 | 2008-01-07 13:20:25 +0200 | [diff] [blame] | 1071 | index = kvm_page_table_hashfn(gfn); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1072 | bucket = &kvm->arch.mmu_page_hash[index]; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1073 | hlist_for_each_entry(sp, node, bucket, hash_link) |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1074 | if (sp->gfn == gfn && !sp->role.direct |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1075 | && !sp->role.invalid) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1076 | pgprintk("%s: found role %x\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1077 | __func__, sp->role.word); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1078 | return sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1079 | } |
| 1080 | return NULL; |
| 1081 | } |
| 1082 | |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 1083 | static void kvm_unlink_unsync_global(struct kvm *kvm, struct kvm_mmu_page *sp) |
| 1084 | { |
| 1085 | list_del(&sp->oos_link); |
| 1086 | --kvm->stat.mmu_unsync_global; |
| 1087 | } |
| 1088 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1089 | static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
| 1090 | { |
| 1091 | WARN_ON(!sp->unsync); |
| 1092 | sp->unsync = 0; |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 1093 | if (sp->global) |
| 1094 | kvm_unlink_unsync_global(kvm, sp); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1095 | --kvm->stat.mmu_unsync; |
| 1096 | } |
| 1097 | |
| 1098 | static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp); |
| 1099 | |
| 1100 | static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) |
| 1101 | { |
| 1102 | if (sp->role.glevels != vcpu->arch.mmu.root_level) { |
| 1103 | kvm_mmu_zap_page(vcpu->kvm, sp); |
| 1104 | return 1; |
| 1105 | } |
| 1106 | |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1107 | if (rmap_write_protect(vcpu->kvm, sp->gfn)) |
| 1108 | kvm_flush_remote_tlbs(vcpu->kvm); |
Marcelo Tosatti | 0c0f40b | 2008-11-21 19:13:58 +0100 | [diff] [blame] | 1109 | kvm_unlink_unsync_page(vcpu->kvm, sp); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1110 | if (vcpu->arch.mmu.sync_page(vcpu, sp)) { |
| 1111 | kvm_mmu_zap_page(vcpu->kvm, sp); |
| 1112 | return 1; |
| 1113 | } |
| 1114 | |
| 1115 | kvm_mmu_flush_tlb(vcpu); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1116 | return 0; |
| 1117 | } |
| 1118 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1119 | struct mmu_page_path { |
| 1120 | struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1]; |
| 1121 | unsigned int idx[PT64_ROOT_LEVEL-1]; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1122 | }; |
| 1123 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1124 | #define for_each_sp(pvec, sp, parents, i) \ |
| 1125 | for (i = mmu_pages_next(&pvec, &parents, -1), \ |
| 1126 | sp = pvec.page[i].sp; \ |
| 1127 | i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \ |
| 1128 | i = mmu_pages_next(&pvec, &parents, i)) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1129 | |
Hannes Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 1130 | static int mmu_pages_next(struct kvm_mmu_pages *pvec, |
| 1131 | struct mmu_page_path *parents, |
| 1132 | int i) |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1133 | { |
| 1134 | int n; |
| 1135 | |
| 1136 | for (n = i+1; n < pvec->nr; n++) { |
| 1137 | struct kvm_mmu_page *sp = pvec->page[n].sp; |
| 1138 | |
| 1139 | if (sp->role.level == PT_PAGE_TABLE_LEVEL) { |
| 1140 | parents->idx[0] = pvec->page[n].idx; |
| 1141 | return n; |
| 1142 | } |
| 1143 | |
| 1144 | parents->parent[sp->role.level-2] = sp; |
| 1145 | parents->idx[sp->role.level-1] = pvec->page[n].idx; |
| 1146 | } |
| 1147 | |
| 1148 | return n; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1149 | } |
| 1150 | |
Hannes Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 1151 | static void mmu_pages_clear_parents(struct mmu_page_path *parents) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1152 | { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1153 | struct kvm_mmu_page *sp; |
| 1154 | unsigned int level = 0; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1155 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1156 | do { |
| 1157 | unsigned int idx = parents->idx[level]; |
| 1158 | |
| 1159 | sp = parents->parent[level]; |
| 1160 | if (!sp) |
| 1161 | return; |
| 1162 | |
| 1163 | --sp->unsync_children; |
| 1164 | WARN_ON((int)sp->unsync_children < 0); |
| 1165 | __clear_bit(idx, sp->unsync_child_bitmap); |
| 1166 | level++; |
| 1167 | } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children); |
| 1168 | } |
| 1169 | |
| 1170 | static void kvm_mmu_pages_init(struct kvm_mmu_page *parent, |
| 1171 | struct mmu_page_path *parents, |
| 1172 | struct kvm_mmu_pages *pvec) |
| 1173 | { |
| 1174 | parents->parent[parent->role.level-1] = NULL; |
| 1175 | pvec->nr = 0; |
| 1176 | } |
| 1177 | |
| 1178 | static void mmu_sync_children(struct kvm_vcpu *vcpu, |
| 1179 | struct kvm_mmu_page *parent) |
| 1180 | { |
| 1181 | int i; |
| 1182 | struct kvm_mmu_page *sp; |
| 1183 | struct mmu_page_path parents; |
| 1184 | struct kvm_mmu_pages pages; |
| 1185 | |
| 1186 | kvm_mmu_pages_init(parent, &parents, &pages); |
| 1187 | while (mmu_unsync_walk(parent, &pages)) { |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1188 | int protected = 0; |
| 1189 | |
| 1190 | for_each_sp(pages, sp, parents, i) |
| 1191 | protected |= rmap_write_protect(vcpu->kvm, sp->gfn); |
| 1192 | |
| 1193 | if (protected) |
| 1194 | kvm_flush_remote_tlbs(vcpu->kvm); |
| 1195 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1196 | for_each_sp(pages, sp, parents, i) { |
| 1197 | kvm_sync_page(vcpu, sp); |
| 1198 | mmu_pages_clear_parents(&parents); |
| 1199 | } |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1200 | cond_resched_lock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1201 | kvm_mmu_pages_init(parent, &parents, &pages); |
| 1202 | } |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1203 | } |
| 1204 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1205 | static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, |
| 1206 | gfn_t gfn, |
| 1207 | gva_t gaddr, |
| 1208 | unsigned level, |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1209 | int direct, |
Avi Kivity | 41074d0 | 2007-12-09 17:00:02 +0200 | [diff] [blame] | 1210 | unsigned access, |
Avi Kivity | f7d9c7b | 2008-02-26 22:12:10 +0200 | [diff] [blame] | 1211 | u64 *parent_pte) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1212 | { |
| 1213 | union kvm_mmu_page_role role; |
| 1214 | unsigned index; |
| 1215 | unsigned quadrant; |
| 1216 | struct hlist_head *bucket; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1217 | struct kvm_mmu_page *sp; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1218 | struct hlist_node *node, *tmp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1219 | |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 1220 | role = vcpu->arch.mmu.base_role; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1221 | role.level = level; |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1222 | role.direct = direct; |
Avi Kivity | 41074d0 | 2007-12-09 17:00:02 +0200 | [diff] [blame] | 1223 | role.access = access; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1224 | if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1225 | quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); |
| 1226 | quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1; |
| 1227 | role.quadrant = quadrant; |
| 1228 | } |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1229 | pgprintk("%s: looking gfn %lx role %x\n", __func__, |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1230 | gfn, role.word); |
Dong, Eddie | 1ae0a13 | 2008-01-07 13:20:25 +0200 | [diff] [blame] | 1231 | index = kvm_page_table_hashfn(gfn); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1232 | bucket = &vcpu->kvm->arch.mmu_page_hash[index]; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1233 | hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link) |
| 1234 | if (sp->gfn == gfn) { |
| 1235 | if (sp->unsync) |
| 1236 | if (kvm_sync_page(vcpu, sp)) |
| 1237 | continue; |
| 1238 | |
| 1239 | if (sp->role.word != role.word) |
| 1240 | continue; |
| 1241 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1242 | mmu_page_add_parent_pte(vcpu, sp, parent_pte); |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 1243 | if (sp->unsync_children) { |
| 1244 | set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests); |
| 1245 | kvm_mmu_mark_parents_unsync(vcpu, sp); |
| 1246 | } |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1247 | pgprintk("%s: found\n", __func__); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1248 | return sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1249 | } |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 1250 | ++vcpu->kvm->stat.mmu_cache_miss; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1251 | sp = kvm_mmu_alloc_page(vcpu, parent_pte); |
| 1252 | if (!sp) |
| 1253 | return sp; |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1254 | pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1255 | sp->gfn = gfn; |
| 1256 | sp->role = role; |
Marcelo Tosatti | bf47a76 | 2009-04-05 14:54:46 -0300 | [diff] [blame] | 1257 | sp->global = 0; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1258 | hlist_add_head(&sp->hash_link, bucket); |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1259 | if (!direct) { |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1260 | if (rmap_write_protect(vcpu->kvm, gfn)) |
| 1261 | kvm_flush_remote_tlbs(vcpu->kvm); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1262 | account_shadowed(vcpu->kvm, gfn); |
| 1263 | } |
Avi Kivity | 131d827 | 2008-05-29 14:56:28 +0300 | [diff] [blame] | 1264 | if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte) |
| 1265 | vcpu->arch.mmu.prefetch_page(vcpu, sp); |
| 1266 | else |
| 1267 | nonpaging_prefetch_page(vcpu, sp); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1268 | return sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 1271 | static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator, |
| 1272 | struct kvm_vcpu *vcpu, u64 addr) |
| 1273 | { |
| 1274 | iterator->addr = addr; |
| 1275 | iterator->shadow_addr = vcpu->arch.mmu.root_hpa; |
| 1276 | iterator->level = vcpu->arch.mmu.shadow_root_level; |
| 1277 | if (iterator->level == PT32E_ROOT_LEVEL) { |
| 1278 | iterator->shadow_addr |
| 1279 | = vcpu->arch.mmu.pae_root[(addr >> 30) & 3]; |
| 1280 | iterator->shadow_addr &= PT64_BASE_ADDR_MASK; |
| 1281 | --iterator->level; |
| 1282 | if (!iterator->shadow_addr) |
| 1283 | iterator->level = 0; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator) |
| 1288 | { |
| 1289 | if (iterator->level < PT_PAGE_TABLE_LEVEL) |
| 1290 | return false; |
| 1291 | iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level); |
| 1292 | iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index; |
| 1293 | return true; |
| 1294 | } |
| 1295 | |
| 1296 | static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator) |
| 1297 | { |
| 1298 | iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK; |
| 1299 | --iterator->level; |
| 1300 | } |
| 1301 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1302 | static void kvm_mmu_page_unlink_children(struct kvm *kvm, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1303 | struct kvm_mmu_page *sp) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1304 | { |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1305 | unsigned i; |
| 1306 | u64 *pt; |
| 1307 | u64 ent; |
| 1308 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1309 | pt = sp->spt; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1310 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1311 | if (sp->role.level == PT_PAGE_TABLE_LEVEL) { |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1312 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1313 | if (is_shadow_present_pte(pt[i])) |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 1314 | rmap_remove(kvm, &pt[i]); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1315 | pt[i] = shadow_trap_nonpresent_pte; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1316 | } |
| 1317 | return; |
| 1318 | } |
| 1319 | |
| 1320 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 1321 | ent = pt[i]; |
| 1322 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1323 | if (is_shadow_present_pte(ent)) { |
| 1324 | if (!is_large_pte(ent)) { |
| 1325 | ent &= PT64_BASE_ADDR_MASK; |
| 1326 | mmu_page_remove_parent_pte(page_header(ent), |
| 1327 | &pt[i]); |
| 1328 | } else { |
| 1329 | --kvm->stat.lpages; |
| 1330 | rmap_remove(kvm, &pt[i]); |
| 1331 | } |
| 1332 | } |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1333 | pt[i] = shadow_trap_nonpresent_pte; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1334 | } |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1335 | } |
| 1336 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1337 | static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1338 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1339 | mmu_page_remove_parent_pte(sp, parent_pte); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 1342 | static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm) |
| 1343 | { |
| 1344 | int i; |
| 1345 | |
| 1346 | for (i = 0; i < KVM_MAX_VCPUS; ++i) |
| 1347 | if (kvm->vcpus[i]) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1348 | kvm->vcpus[i]->arch.last_pte_updated = NULL; |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 1349 | } |
| 1350 | |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1351 | static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1352 | { |
| 1353 | u64 *parent_pte; |
| 1354 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1355 | while (sp->multimapped || sp->parent_pte) { |
| 1356 | if (!sp->multimapped) |
| 1357 | parent_pte = sp->parent_pte; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1358 | else { |
| 1359 | struct kvm_pte_chain *chain; |
| 1360 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1361 | chain = container_of(sp->parent_ptes.first, |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1362 | struct kvm_pte_chain, link); |
| 1363 | parent_pte = chain->parent_ptes[0]; |
| 1364 | } |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1365 | BUG_ON(!parent_pte); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1366 | kvm_mmu_put_page(sp, parent_pte); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1367 | set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1368 | } |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1369 | } |
| 1370 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1371 | static int mmu_zap_unsync_children(struct kvm *kvm, |
| 1372 | struct kvm_mmu_page *parent) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1373 | { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1374 | int i, zapped = 0; |
| 1375 | struct mmu_page_path parents; |
| 1376 | struct kvm_mmu_pages pages; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1377 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1378 | if (parent->role.level == PT_PAGE_TABLE_LEVEL) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1379 | return 0; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1380 | |
| 1381 | kvm_mmu_pages_init(parent, &parents, &pages); |
| 1382 | while (mmu_unsync_walk(parent, &pages)) { |
| 1383 | struct kvm_mmu_page *sp; |
| 1384 | |
| 1385 | for_each_sp(pages, sp, parents, i) { |
| 1386 | kvm_mmu_zap_page(kvm, sp); |
| 1387 | mmu_pages_clear_parents(&parents); |
| 1388 | } |
| 1389 | zapped += pages.nr; |
| 1390 | kvm_mmu_pages_init(parent, &parents, &pages); |
| 1391 | } |
| 1392 | |
| 1393 | return zapped; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1394 | } |
| 1395 | |
Marcelo Tosatti | 0738541 | 2008-09-23 13:18:37 -0300 | [diff] [blame] | 1396 | static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1397 | { |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1398 | int ret; |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1399 | ++kvm->stat.mmu_shadow_zapped; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1400 | ret = mmu_zap_unsync_children(kvm, sp); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1401 | kvm_mmu_page_unlink_children(kvm, sp); |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1402 | kvm_mmu_unlink_parents(kvm, sp); |
Avi Kivity | 5b5c6a5 | 2008-07-11 18:07:26 +0300 | [diff] [blame] | 1403 | kvm_flush_remote_tlbs(kvm); |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1404 | if (!sp->role.invalid && !sp->role.direct) |
Avi Kivity | 5b5c6a5 | 2008-07-11 18:07:26 +0300 | [diff] [blame] | 1405 | unaccount_shadowed(kvm, sp->gfn); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1406 | if (sp->unsync) |
| 1407 | kvm_unlink_unsync_page(kvm, sp); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1408 | if (!sp->root_count) { |
| 1409 | hlist_del(&sp->hash_link); |
| 1410 | kvm_mmu_free_page(kvm, sp); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1411 | } else { |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1412 | sp->role.invalid = 1; |
Avi Kivity | 5b5c6a5 | 2008-07-11 18:07:26 +0300 | [diff] [blame] | 1413 | list_move(&sp->link, &kvm->arch.active_mmu_pages); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1414 | kvm_reload_remote_mmus(kvm); |
| 1415 | } |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 1416 | kvm_mmu_reset_last_pte_updated(kvm); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1417 | return ret; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1420 | /* |
| 1421 | * Changing the number of mmu pages allocated to the vm |
| 1422 | * Note: if kvm_nr_mmu_pages is too small, you will get dead lock |
| 1423 | */ |
| 1424 | void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages) |
| 1425 | { |
| 1426 | /* |
| 1427 | * If we set the number of mmu pages to be smaller be than the |
| 1428 | * number of actived pages , we must to free some mmu pages before we |
| 1429 | * change the value |
| 1430 | */ |
| 1431 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1432 | if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) > |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1433 | kvm_nr_mmu_pages) { |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1434 | int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages |
| 1435 | - kvm->arch.n_free_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1436 | |
| 1437 | while (n_used_mmu_pages > kvm_nr_mmu_pages) { |
| 1438 | struct kvm_mmu_page *page; |
| 1439 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1440 | page = container_of(kvm->arch.active_mmu_pages.prev, |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1441 | struct kvm_mmu_page, link); |
| 1442 | kvm_mmu_zap_page(kvm, page); |
| 1443 | n_used_mmu_pages--; |
| 1444 | } |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1445 | kvm->arch.n_free_mmu_pages = 0; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1446 | } |
| 1447 | else |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1448 | kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages |
| 1449 | - kvm->arch.n_alloc_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1450 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1451 | kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
Anthony Liguori | f67a46f | 2007-10-10 19:25:50 -0500 | [diff] [blame] | 1454 | static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1455 | { |
| 1456 | unsigned index; |
| 1457 | struct hlist_head *bucket; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1458 | struct kvm_mmu_page *sp; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1459 | struct hlist_node *node, *n; |
| 1460 | int r; |
| 1461 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1462 | pgprintk("%s: looking for gfn %lx\n", __func__, gfn); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1463 | r = 0; |
Dong, Eddie | 1ae0a13 | 2008-01-07 13:20:25 +0200 | [diff] [blame] | 1464 | index = kvm_page_table_hashfn(gfn); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1465 | bucket = &kvm->arch.mmu_page_hash[index]; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1466 | hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1467 | if (sp->gfn == gfn && !sp->role.direct) { |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1468 | pgprintk("%s: gfn %lx role %x\n", __func__, gfn, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1469 | sp->role.word); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1470 | r = 1; |
Marcelo Tosatti | 0738541 | 2008-09-23 13:18:37 -0300 | [diff] [blame] | 1471 | if (kvm_mmu_zap_page(kvm, sp)) |
| 1472 | n = bucket->first; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1473 | } |
| 1474 | return r; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1475 | } |
| 1476 | |
Anthony Liguori | f67a46f | 2007-10-10 19:25:50 -0500 | [diff] [blame] | 1477 | static void mmu_unshadow(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 1478 | { |
Avi Kivity | 4677a3b | 2009-01-06 13:00:27 +0200 | [diff] [blame] | 1479 | unsigned index; |
| 1480 | struct hlist_head *bucket; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1481 | struct kvm_mmu_page *sp; |
Avi Kivity | 4677a3b | 2009-01-06 13:00:27 +0200 | [diff] [blame] | 1482 | struct hlist_node *node, *nn; |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 1483 | |
Avi Kivity | 4677a3b | 2009-01-06 13:00:27 +0200 | [diff] [blame] | 1484 | index = kvm_page_table_hashfn(gfn); |
| 1485 | bucket = &kvm->arch.mmu_page_hash[index]; |
| 1486 | hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) { |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1487 | if (sp->gfn == gfn && !sp->role.direct |
Avi Kivity | 4677a3b | 2009-01-06 13:00:27 +0200 | [diff] [blame] | 1488 | && !sp->role.invalid) { |
| 1489 | pgprintk("%s: zap %lx %x\n", |
| 1490 | __func__, gfn, sp->role.word); |
| 1491 | kvm_mmu_zap_page(kvm, sp); |
| 1492 | } |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | |
Avi Kivity | 38c335f | 2007-11-21 14:20:22 +0200 | [diff] [blame] | 1496 | static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1497 | { |
Avi Kivity | 38c335f | 2007-11-21 14:20:22 +0200 | [diff] [blame] | 1498 | int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn)); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1499 | struct kvm_mmu_page *sp = page_header(__pa(pte)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1500 | |
Sheng Yang | 291f26b | 2008-10-16 17:30:57 +0800 | [diff] [blame] | 1501 | __set_bit(slot, sp->slot_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1502 | } |
| 1503 | |
Marcelo Tosatti | 6844dec | 2008-09-23 13:18:38 -0300 | [diff] [blame] | 1504 | static void mmu_convert_notrap(struct kvm_mmu_page *sp) |
| 1505 | { |
| 1506 | int i; |
| 1507 | u64 *pt = sp->spt; |
| 1508 | |
| 1509 | if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte) |
| 1510 | return; |
| 1511 | |
| 1512 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 1513 | if (pt[i] == shadow_notrap_nonpresent_pte) |
| 1514 | set_shadow_pte(&pt[i], shadow_trap_nonpresent_pte); |
| 1515 | } |
| 1516 | } |
| 1517 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1518 | struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva) |
| 1519 | { |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1520 | struct page *page; |
| 1521 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1522 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1523 | |
| 1524 | if (gpa == UNMAPPED_GVA) |
| 1525 | return NULL; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1526 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1527 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1528 | |
| 1529 | return page; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1530 | } |
| 1531 | |
Sheng Yang | 74be52e | 2008-10-09 16:01:56 +0800 | [diff] [blame] | 1532 | /* |
| 1533 | * The function is based on mtrr_type_lookup() in |
| 1534 | * arch/x86/kernel/cpu/mtrr/generic.c |
| 1535 | */ |
| 1536 | static int get_mtrr_type(struct mtrr_state_type *mtrr_state, |
| 1537 | u64 start, u64 end) |
| 1538 | { |
| 1539 | int i; |
| 1540 | u64 base, mask; |
| 1541 | u8 prev_match, curr_match; |
| 1542 | int num_var_ranges = KVM_NR_VAR_MTRR; |
| 1543 | |
| 1544 | if (!mtrr_state->enabled) |
| 1545 | return 0xFF; |
| 1546 | |
| 1547 | /* Make end inclusive end, instead of exclusive */ |
| 1548 | end--; |
| 1549 | |
| 1550 | /* Look in fixed ranges. Just return the type as per start */ |
| 1551 | if (mtrr_state->have_fixed && (start < 0x100000)) { |
| 1552 | int idx; |
| 1553 | |
| 1554 | if (start < 0x80000) { |
| 1555 | idx = 0; |
| 1556 | idx += (start >> 16); |
| 1557 | return mtrr_state->fixed_ranges[idx]; |
| 1558 | } else if (start < 0xC0000) { |
| 1559 | idx = 1 * 8; |
| 1560 | idx += ((start - 0x80000) >> 14); |
| 1561 | return mtrr_state->fixed_ranges[idx]; |
| 1562 | } else if (start < 0x1000000) { |
| 1563 | idx = 3 * 8; |
| 1564 | idx += ((start - 0xC0000) >> 12); |
| 1565 | return mtrr_state->fixed_ranges[idx]; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | /* |
| 1570 | * Look in variable ranges |
| 1571 | * Look of multiple ranges matching this address and pick type |
| 1572 | * as per MTRR precedence |
| 1573 | */ |
| 1574 | if (!(mtrr_state->enabled & 2)) |
| 1575 | return mtrr_state->def_type; |
| 1576 | |
| 1577 | prev_match = 0xFF; |
| 1578 | for (i = 0; i < num_var_ranges; ++i) { |
| 1579 | unsigned short start_state, end_state; |
| 1580 | |
| 1581 | if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11))) |
| 1582 | continue; |
| 1583 | |
| 1584 | base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) + |
| 1585 | (mtrr_state->var_ranges[i].base_lo & PAGE_MASK); |
| 1586 | mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) + |
| 1587 | (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK); |
| 1588 | |
| 1589 | start_state = ((start & mask) == (base & mask)); |
| 1590 | end_state = ((end & mask) == (base & mask)); |
| 1591 | if (start_state != end_state) |
| 1592 | return 0xFE; |
| 1593 | |
| 1594 | if ((start & mask) != (base & mask)) |
| 1595 | continue; |
| 1596 | |
| 1597 | curr_match = mtrr_state->var_ranges[i].base_lo & 0xff; |
| 1598 | if (prev_match == 0xFF) { |
| 1599 | prev_match = curr_match; |
| 1600 | continue; |
| 1601 | } |
| 1602 | |
| 1603 | if (prev_match == MTRR_TYPE_UNCACHABLE || |
| 1604 | curr_match == MTRR_TYPE_UNCACHABLE) |
| 1605 | return MTRR_TYPE_UNCACHABLE; |
| 1606 | |
| 1607 | if ((prev_match == MTRR_TYPE_WRBACK && |
| 1608 | curr_match == MTRR_TYPE_WRTHROUGH) || |
| 1609 | (prev_match == MTRR_TYPE_WRTHROUGH && |
| 1610 | curr_match == MTRR_TYPE_WRBACK)) { |
| 1611 | prev_match = MTRR_TYPE_WRTHROUGH; |
| 1612 | curr_match = MTRR_TYPE_WRTHROUGH; |
| 1613 | } |
| 1614 | |
| 1615 | if (prev_match != curr_match) |
| 1616 | return MTRR_TYPE_UNCACHABLE; |
| 1617 | } |
| 1618 | |
| 1619 | if (prev_match != 0xFF) |
| 1620 | return prev_match; |
| 1621 | |
| 1622 | return mtrr_state->def_type; |
| 1623 | } |
| 1624 | |
| 1625 | static u8 get_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 1626 | { |
| 1627 | u8 mtrr; |
| 1628 | |
| 1629 | mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT, |
| 1630 | (gfn << PAGE_SHIFT) + PAGE_SIZE); |
| 1631 | if (mtrr == 0xfe || mtrr == 0xff) |
| 1632 | mtrr = MTRR_TYPE_WRBACK; |
| 1633 | return mtrr; |
| 1634 | } |
| 1635 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1636 | static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) |
| 1637 | { |
| 1638 | unsigned index; |
| 1639 | struct hlist_head *bucket; |
| 1640 | struct kvm_mmu_page *s; |
| 1641 | struct hlist_node *node, *n; |
| 1642 | |
| 1643 | index = kvm_page_table_hashfn(sp->gfn); |
| 1644 | bucket = &vcpu->kvm->arch.mmu_page_hash[index]; |
| 1645 | /* don't unsync if pagetable is shadowed with multiple roles */ |
| 1646 | hlist_for_each_entry_safe(s, node, n, bucket, hash_link) { |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1647 | if (s->gfn != sp->gfn || s->role.direct) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1648 | continue; |
| 1649 | if (s->role.word != sp->role.word) |
| 1650 | return 1; |
| 1651 | } |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1652 | ++vcpu->kvm->stat.mmu_unsync; |
| 1653 | sp->unsync = 1; |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 1654 | |
| 1655 | if (sp->global) { |
| 1656 | list_add(&sp->oos_link, &vcpu->kvm->arch.oos_global_pages); |
| 1657 | ++vcpu->kvm->stat.mmu_unsync_global; |
| 1658 | } else |
| 1659 | kvm_mmu_mark_parents_unsync(vcpu, sp); |
| 1660 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1661 | mmu_convert_notrap(sp); |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 1666 | bool can_unsync) |
| 1667 | { |
| 1668 | struct kvm_mmu_page *shadow; |
| 1669 | |
| 1670 | shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn); |
| 1671 | if (shadow) { |
| 1672 | if (shadow->role.level != PT_PAGE_TABLE_LEVEL) |
| 1673 | return 1; |
| 1674 | if (shadow->unsync) |
| 1675 | return 0; |
Marcelo Tosatti | 582801a | 2008-09-23 13:18:41 -0300 | [diff] [blame] | 1676 | if (can_unsync && oos_shadow) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1677 | return kvm_unsync_page(vcpu, shadow); |
| 1678 | return 1; |
| 1679 | } |
| 1680 | return 0; |
| 1681 | } |
| 1682 | |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 1683 | static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, |
| 1684 | unsigned pte_access, int user_fault, |
| 1685 | int write_fault, int dirty, int largepage, |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 1686 | int global, gfn_t gfn, pfn_t pfn, bool speculative, |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1687 | bool can_unsync) |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1688 | { |
| 1689 | u64 spte; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 1690 | int ret = 0; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 1691 | u64 mt_mask = shadow_mt_mask; |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 1692 | struct kvm_mmu_page *sp = page_header(__pa(shadow_pte)); |
| 1693 | |
| 1694 | if (!global && sp->global) { |
| 1695 | sp->global = 0; |
| 1696 | if (sp->unsync) { |
| 1697 | kvm_unlink_unsync_global(vcpu->kvm, sp); |
| 1698 | kvm_mmu_mark_parents_unsync(vcpu, sp); |
| 1699 | } |
| 1700 | } |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 1701 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1702 | /* |
| 1703 | * We don't set the accessed bit, since we sometimes want to see |
| 1704 | * whether the guest actually used the pte (in order to detect |
| 1705 | * demand paging). |
| 1706 | */ |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 1707 | spte = shadow_base_present_pte | shadow_dirty_mask; |
Avi Kivity | 947da53 | 2008-03-18 11:05:52 +0200 | [diff] [blame] | 1708 | if (!speculative) |
Avi Kivity | 3201b5d | 2008-08-27 20:01:04 +0300 | [diff] [blame] | 1709 | spte |= shadow_accessed_mask; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1710 | if (!dirty) |
| 1711 | pte_access &= ~ACC_WRITE_MASK; |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 1712 | if (pte_access & ACC_EXEC_MASK) |
| 1713 | spte |= shadow_x_mask; |
| 1714 | else |
| 1715 | spte |= shadow_nx_mask; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1716 | if (pte_access & ACC_USER_MASK) |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 1717 | spte |= shadow_user_mask; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1718 | if (largepage) |
| 1719 | spte |= PT_PAGE_SIZE_MASK; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 1720 | if (mt_mask) { |
Sheng Yang | 2aaf69d | 2009-01-21 16:52:16 +0800 | [diff] [blame] | 1721 | if (!kvm_is_mmio_pfn(pfn)) { |
| 1722 | mt_mask = get_memory_type(vcpu, gfn) << |
| 1723 | kvm_x86_ops->get_mt_mask_shift(); |
| 1724 | mt_mask |= VMX_EPT_IGMT_BIT; |
| 1725 | } else |
| 1726 | mt_mask = MTRR_TYPE_UNCACHABLE << |
| 1727 | kvm_x86_ops->get_mt_mask_shift(); |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 1728 | spte |= mt_mask; |
| 1729 | } |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1730 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1731 | spte |= (u64)pfn << PAGE_SHIFT; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1732 | |
| 1733 | if ((pte_access & ACC_WRITE_MASK) |
| 1734 | || (write_fault && !is_write_protection(vcpu) && !user_fault)) { |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1735 | |
Marcelo Tosatti | 38187c8 | 2008-09-23 13:18:32 -0300 | [diff] [blame] | 1736 | if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) { |
| 1737 | ret = 1; |
| 1738 | spte = shadow_trap_nonpresent_pte; |
| 1739 | goto set_pte; |
| 1740 | } |
| 1741 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1742 | spte |= PT_WRITABLE_MASK; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1743 | |
Marcelo Tosatti | ecc5589 | 2008-11-25 15:58:07 +0100 | [diff] [blame] | 1744 | /* |
| 1745 | * Optimization: for pte sync, if spte was writable the hash |
| 1746 | * lookup is unnecessary (and expensive). Write protection |
| 1747 | * is responsibility of mmu_get_page / kvm_sync_page. |
| 1748 | * Same reasoning can be applied to dirty page accounting. |
| 1749 | */ |
| 1750 | if (!can_unsync && is_writeble_pte(*shadow_pte)) |
| 1751 | goto set_pte; |
| 1752 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1753 | if (mmu_need_write_protect(vcpu, gfn, can_unsync)) { |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1754 | pgprintk("%s: found shadow page for %lx, marking ro\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1755 | __func__, gfn); |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 1756 | ret = 1; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1757 | pte_access &= ~ACC_WRITE_MASK; |
Marcelo Tosatti | a378b4e | 2008-09-23 13:18:31 -0300 | [diff] [blame] | 1758 | if (is_writeble_pte(spte)) |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1759 | spte &= ~PT_WRITABLE_MASK; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1763 | if (pte_access & ACC_WRITE_MASK) |
| 1764 | mark_page_dirty(vcpu->kvm, gfn); |
| 1765 | |
Marcelo Tosatti | 38187c8 | 2008-09-23 13:18:32 -0300 | [diff] [blame] | 1766 | set_pte: |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1767 | set_shadow_pte(shadow_pte, spte); |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 1768 | return ret; |
| 1769 | } |
| 1770 | |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 1771 | static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, |
| 1772 | unsigned pt_access, unsigned pte_access, |
| 1773 | int user_fault, int write_fault, int dirty, |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 1774 | int *ptwrite, int largepage, int global, |
| 1775 | gfn_t gfn, pfn_t pfn, bool speculative) |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 1776 | { |
| 1777 | int was_rmapped = 0; |
| 1778 | int was_writeble = is_writeble_pte(*shadow_pte); |
| 1779 | |
| 1780 | pgprintk("%s: spte %llx access %x write_fault %d" |
| 1781 | " user_fault %d gfn %lx\n", |
| 1782 | __func__, *shadow_pte, pt_access, |
| 1783 | write_fault, user_fault, gfn); |
| 1784 | |
| 1785 | if (is_rmap_pte(*shadow_pte)) { |
| 1786 | /* |
| 1787 | * If we overwrite a PTE page pointer with a 2MB PMD, unlink |
| 1788 | * the parent of the now unreachable PTE. |
| 1789 | */ |
| 1790 | if (largepage && !is_large_pte(*shadow_pte)) { |
| 1791 | struct kvm_mmu_page *child; |
| 1792 | u64 pte = *shadow_pte; |
| 1793 | |
| 1794 | child = page_header(pte & PT64_BASE_ADDR_MASK); |
| 1795 | mmu_page_remove_parent_pte(child, shadow_pte); |
| 1796 | } else if (pfn != spte_to_pfn(*shadow_pte)) { |
| 1797 | pgprintk("hfn old %lx new %lx\n", |
| 1798 | spte_to_pfn(*shadow_pte), pfn); |
| 1799 | rmap_remove(vcpu->kvm, shadow_pte); |
Joerg Roedel | 6bed6b9 | 2009-02-18 14:08:59 +0100 | [diff] [blame] | 1800 | } else |
| 1801 | was_rmapped = 1; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 1802 | } |
| 1803 | if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault, |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 1804 | dirty, largepage, global, gfn, pfn, speculative, true)) { |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 1805 | if (write_fault) |
| 1806 | *ptwrite = 1; |
Marcelo Tosatti | a378b4e | 2008-09-23 13:18:31 -0300 | [diff] [blame] | 1807 | kvm_x86_ops->tlb_flush(vcpu); |
| 1808 | } |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 1809 | |
| 1810 | pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte); |
| 1811 | pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n", |
| 1812 | is_large_pte(*shadow_pte)? "2MB" : "4kB", |
| 1813 | is_present_pte(*shadow_pte)?"RW":"R", gfn, |
| 1814 | *shadow_pte, shadow_pte); |
| 1815 | if (!was_rmapped && is_large_pte(*shadow_pte)) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1816 | ++vcpu->kvm->stat.lpages; |
| 1817 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1818 | page_header_update_slot(vcpu->kvm, shadow_pte, gfn); |
| 1819 | if (!was_rmapped) { |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1820 | rmap_add(vcpu, shadow_pte, gfn, largepage); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1821 | if (!is_rmap_pte(*shadow_pte)) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1822 | kvm_release_pfn_clean(pfn); |
Izik Eidus | 75e68e6 | 2008-01-12 23:49:09 +0200 | [diff] [blame] | 1823 | } else { |
| 1824 | if (was_writeble) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1825 | kvm_release_pfn_dirty(pfn); |
Izik Eidus | 75e68e6 | 2008-01-12 23:49:09 +0200 | [diff] [blame] | 1826 | else |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1827 | kvm_release_pfn_clean(pfn); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1828 | } |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 1829 | if (speculative) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1830 | vcpu->arch.last_pte_updated = shadow_pte; |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 1831 | vcpu->arch.last_pte_gfn = gfn; |
| 1832 | } |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1833 | } |
| 1834 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1835 | static void nonpaging_new_cr3(struct kvm_vcpu *vcpu) |
| 1836 | { |
| 1837 | } |
| 1838 | |
Joerg Roedel | 4d9976b | 2008-02-07 13:47:42 +0100 | [diff] [blame] | 1839 | static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write, |
Avi Kivity | 6c41f42 | 2008-08-26 16:16:08 +0300 | [diff] [blame] | 1840 | int largepage, gfn_t gfn, pfn_t pfn) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1841 | { |
Avi Kivity | 9f652d2 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 1842 | struct kvm_shadow_walk_iterator iterator; |
| 1843 | struct kvm_mmu_page *sp; |
| 1844 | int pt_write = 0; |
| 1845 | gfn_t pseudo_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1846 | |
Avi Kivity | 9f652d2 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 1847 | for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) { |
| 1848 | if (iterator.level == PT_PAGE_TABLE_LEVEL |
| 1849 | || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) { |
| 1850 | mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL, |
| 1851 | 0, write, 1, &pt_write, |
| 1852 | largepage, 0, gfn, pfn, false); |
| 1853 | ++vcpu->stat.pf_fixed; |
| 1854 | break; |
| 1855 | } |
| 1856 | |
| 1857 | if (*iterator.sptep == shadow_trap_nonpresent_pte) { |
| 1858 | pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT; |
| 1859 | sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr, |
| 1860 | iterator.level - 1, |
| 1861 | 1, ACC_ALL, iterator.sptep); |
| 1862 | if (!sp) { |
| 1863 | pgprintk("nonpaging_map: ENOMEM\n"); |
| 1864 | kvm_release_pfn_clean(pfn); |
| 1865 | return -ENOMEM; |
| 1866 | } |
| 1867 | |
| 1868 | set_shadow_pte(iterator.sptep, |
| 1869 | __pa(sp->spt) |
| 1870 | | PT_PRESENT_MASK | PT_WRITABLE_MASK |
| 1871 | | shadow_user_mask | shadow_x_mask); |
| 1872 | } |
| 1873 | } |
| 1874 | return pt_write; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1875 | } |
| 1876 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1877 | static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn) |
| 1878 | { |
| 1879 | int r; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1880 | int largepage = 0; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1881 | pfn_t pfn; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1882 | unsigned long mmu_seq; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1883 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1884 | if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) { |
| 1885 | gfn &= ~(KVM_PAGES_PER_HPAGE-1); |
| 1886 | largepage = 1; |
| 1887 | } |
| 1888 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1889 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 1890 | smp_rmb(); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1891 | pfn = gfn_to_pfn(vcpu->kvm, gfn); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1892 | |
Avi Kivity | d196e34 | 2008-01-24 11:44:11 +0200 | [diff] [blame] | 1893 | /* mmio */ |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1894 | if (is_error_pfn(pfn)) { |
| 1895 | kvm_release_pfn_clean(pfn); |
Avi Kivity | d196e34 | 2008-01-24 11:44:11 +0200 | [diff] [blame] | 1896 | return 1; |
| 1897 | } |
| 1898 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1899 | spin_lock(&vcpu->kvm->mmu_lock); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1900 | if (mmu_notifier_retry(vcpu, mmu_seq)) |
| 1901 | goto out_unlock; |
Avi Kivity | eb787d1 | 2007-12-31 15:27:49 +0200 | [diff] [blame] | 1902 | kvm_mmu_free_some_pages(vcpu); |
Avi Kivity | 6c41f42 | 2008-08-26 16:16:08 +0300 | [diff] [blame] | 1903 | r = __direct_map(vcpu, v, write, largepage, gfn, pfn); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1904 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 1905 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1906 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1907 | return r; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1908 | |
| 1909 | out_unlock: |
| 1910 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 1911 | kvm_release_pfn_clean(pfn); |
| 1912 | return 0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1916 | static void mmu_free_roots(struct kvm_vcpu *vcpu) |
| 1917 | { |
| 1918 | int i; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1919 | struct kvm_mmu_page *sp; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1920 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1921 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
Avi Kivity | 7b53aa5 | 2007-06-05 12:17:03 +0300 | [diff] [blame] | 1922 | return; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1923 | spin_lock(&vcpu->kvm->mmu_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1924 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 1925 | hpa_t root = vcpu->arch.mmu.root_hpa; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1926 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1927 | sp = page_header(root); |
| 1928 | --sp->root_count; |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1929 | if (!sp->root_count && sp->role.invalid) |
| 1930 | kvm_mmu_zap_page(vcpu->kvm, sp); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1931 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1932 | spin_unlock(&vcpu->kvm->mmu_lock); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1933 | return; |
| 1934 | } |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1935 | for (i = 0; i < 4; ++i) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1936 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1937 | |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 1938 | if (root) { |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 1939 | root &= PT64_BASE_ADDR_MASK; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1940 | sp = page_header(root); |
| 1941 | --sp->root_count; |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1942 | if (!sp->root_count && sp->role.invalid) |
| 1943 | kvm_mmu_zap_page(vcpu->kvm, sp); |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 1944 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1945 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1946 | } |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1947 | spin_unlock(&vcpu->kvm->mmu_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1948 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | static void mmu_alloc_roots(struct kvm_vcpu *vcpu) |
| 1952 | { |
| 1953 | int i; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1954 | gfn_t root_gfn; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1955 | struct kvm_mmu_page *sp; |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1956 | int direct = 0; |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 1957 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1958 | root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1959 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1960 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 1961 | hpa_t root = vcpu->arch.mmu.root_hpa; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1962 | |
| 1963 | ASSERT(!VALID_PAGE(root)); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1964 | if (tdp_enabled) |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1965 | direct = 1; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1966 | sp = kvm_mmu_get_page(vcpu, root_gfn, 0, |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1967 | PT64_ROOT_LEVEL, direct, |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1968 | ACC_ALL, NULL); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1969 | root = __pa(sp->spt); |
| 1970 | ++sp->root_count; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1971 | vcpu->arch.mmu.root_hpa = root; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1972 | return; |
| 1973 | } |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1974 | direct = !is_paging(vcpu); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1975 | if (tdp_enabled) |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1976 | direct = 1; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1977 | for (i = 0; i < 4; ++i) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1978 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1979 | |
| 1980 | ASSERT(!VALID_PAGE(root)); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1981 | if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) { |
| 1982 | if (!is_present_pte(vcpu->arch.pdptrs[i])) { |
| 1983 | vcpu->arch.mmu.pae_root[i] = 0; |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 1984 | continue; |
| 1985 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1986 | root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT; |
| 1987 | } else if (vcpu->arch.mmu.root_level == 0) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1988 | root_gfn = 0; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1989 | sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1990 | PT32_ROOT_LEVEL, direct, |
Avi Kivity | f7d9c7b | 2008-02-26 22:12:10 +0200 | [diff] [blame] | 1991 | ACC_ALL, NULL); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1992 | root = __pa(sp->spt); |
| 1993 | ++sp->root_count; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1994 | vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1995 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1996 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 1999 | static void mmu_sync_roots(struct kvm_vcpu *vcpu) |
| 2000 | { |
| 2001 | int i; |
| 2002 | struct kvm_mmu_page *sp; |
| 2003 | |
| 2004 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 2005 | return; |
| 2006 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 2007 | hpa_t root = vcpu->arch.mmu.root_hpa; |
| 2008 | sp = page_header(root); |
| 2009 | mmu_sync_children(vcpu, sp); |
| 2010 | return; |
| 2011 | } |
| 2012 | for (i = 0; i < 4; ++i) { |
| 2013 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
| 2014 | |
| 2015 | if (root) { |
| 2016 | root &= PT64_BASE_ADDR_MASK; |
| 2017 | sp = page_header(root); |
| 2018 | mmu_sync_children(vcpu, sp); |
| 2019 | } |
| 2020 | } |
| 2021 | } |
| 2022 | |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 2023 | static void mmu_sync_global(struct kvm_vcpu *vcpu) |
| 2024 | { |
| 2025 | struct kvm *kvm = vcpu->kvm; |
| 2026 | struct kvm_mmu_page *sp, *n; |
| 2027 | |
| 2028 | list_for_each_entry_safe(sp, n, &kvm->arch.oos_global_pages, oos_link) |
| 2029 | kvm_sync_page(vcpu, sp); |
| 2030 | } |
| 2031 | |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 2032 | void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) |
| 2033 | { |
| 2034 | spin_lock(&vcpu->kvm->mmu_lock); |
| 2035 | mmu_sync_roots(vcpu); |
| 2036 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2037 | } |
| 2038 | |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 2039 | void kvm_mmu_sync_global(struct kvm_vcpu *vcpu) |
| 2040 | { |
| 2041 | spin_lock(&vcpu->kvm->mmu_lock); |
| 2042 | mmu_sync_global(vcpu); |
| 2043 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2044 | } |
| 2045 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2046 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr) |
| 2047 | { |
| 2048 | return vaddr; |
| 2049 | } |
| 2050 | |
| 2051 | static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, |
Avi Kivity | 3f3e712 | 2007-11-21 14:54:16 +0200 | [diff] [blame] | 2052 | u32 error_code) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2053 | { |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 2054 | gfn_t gfn; |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 2055 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2056 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2057 | pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code); |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 2058 | r = mmu_topup_memory_caches(vcpu); |
| 2059 | if (r) |
| 2060 | return r; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 2061 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2062 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2063 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2064 | |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 2065 | gfn = gva >> PAGE_SHIFT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2066 | |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 2067 | return nonpaging_map(vcpu, gva & PAGE_MASK, |
| 2068 | error_code & PFERR_WRITE_MASK, gfn); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2069 | } |
| 2070 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2071 | static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, |
| 2072 | u32 error_code) |
| 2073 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2074 | pfn_t pfn; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2075 | int r; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2076 | int largepage = 0; |
| 2077 | gfn_t gfn = gpa >> PAGE_SHIFT; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2078 | unsigned long mmu_seq; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2079 | |
| 2080 | ASSERT(vcpu); |
| 2081 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
| 2082 | |
| 2083 | r = mmu_topup_memory_caches(vcpu); |
| 2084 | if (r) |
| 2085 | return r; |
| 2086 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2087 | if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) { |
| 2088 | gfn &= ~(KVM_PAGES_PER_HPAGE-1); |
| 2089 | largepage = 1; |
| 2090 | } |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2091 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 2092 | smp_rmb(); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2093 | pfn = gfn_to_pfn(vcpu->kvm, gfn); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2094 | if (is_error_pfn(pfn)) { |
| 2095 | kvm_release_pfn_clean(pfn); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2096 | return 1; |
| 2097 | } |
| 2098 | spin_lock(&vcpu->kvm->mmu_lock); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2099 | if (mmu_notifier_retry(vcpu, mmu_seq)) |
| 2100 | goto out_unlock; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2101 | kvm_mmu_free_some_pages(vcpu); |
| 2102 | r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK, |
Avi Kivity | 6c41f42 | 2008-08-26 16:16:08 +0300 | [diff] [blame] | 2103 | largepage, gfn, pfn); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2104 | spin_unlock(&vcpu->kvm->mmu_lock); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2105 | |
| 2106 | return r; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2107 | |
| 2108 | out_unlock: |
| 2109 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2110 | kvm_release_pfn_clean(pfn); |
| 2111 | return 0; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2112 | } |
| 2113 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2114 | static void nonpaging_free(struct kvm_vcpu *vcpu) |
| 2115 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2116 | mmu_free_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | static int nonpaging_init_context(struct kvm_vcpu *vcpu) |
| 2120 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2121 | struct kvm_mmu *context = &vcpu->arch.mmu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2122 | |
| 2123 | context->new_cr3 = nonpaging_new_cr3; |
| 2124 | context->page_fault = nonpaging_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2125 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
| 2126 | context->free = nonpaging_free; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2127 | context->prefetch_page = nonpaging_prefetch_page; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 2128 | context->sync_page = nonpaging_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2129 | context->invlpg = nonpaging_invlpg; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 2130 | context->root_level = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2131 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2132 | context->root_hpa = INVALID_PAGE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2133 | return 0; |
| 2134 | } |
| 2135 | |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 2136 | void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2137 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2138 | ++vcpu->stat.tlb_flush; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2139 | kvm_x86_ops->tlb_flush(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | static void paging_new_cr3(struct kvm_vcpu *vcpu) |
| 2143 | { |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2144 | pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 2145 | mmu_free_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2146 | } |
| 2147 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2148 | static void inject_page_fault(struct kvm_vcpu *vcpu, |
| 2149 | u64 addr, |
| 2150 | u32 err_code) |
| 2151 | { |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 2152 | kvm_inject_page_fault(vcpu, addr, err_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2153 | } |
| 2154 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2155 | static void paging_free(struct kvm_vcpu *vcpu) |
| 2156 | { |
| 2157 | nonpaging_free(vcpu); |
| 2158 | } |
| 2159 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame^] | 2160 | static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level) |
| 2161 | { |
| 2162 | int bit7; |
| 2163 | |
| 2164 | bit7 = (gpte >> 7) & 1; |
| 2165 | return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0; |
| 2166 | } |
| 2167 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2168 | #define PTTYPE 64 |
| 2169 | #include "paging_tmpl.h" |
| 2170 | #undef PTTYPE |
| 2171 | |
| 2172 | #define PTTYPE 32 |
| 2173 | #include "paging_tmpl.h" |
| 2174 | #undef PTTYPE |
| 2175 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame^] | 2176 | static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level) |
| 2177 | { |
| 2178 | struct kvm_mmu *context = &vcpu->arch.mmu; |
| 2179 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 2180 | u64 exb_bit_rsvd = 0; |
| 2181 | |
| 2182 | if (!is_nx(vcpu)) |
| 2183 | exb_bit_rsvd = rsvd_bits(63, 63); |
| 2184 | switch (level) { |
| 2185 | case PT32_ROOT_LEVEL: |
| 2186 | /* no rsvd bits for 2 level 4K page table entries */ |
| 2187 | context->rsvd_bits_mask[0][1] = 0; |
| 2188 | context->rsvd_bits_mask[0][0] = 0; |
| 2189 | if (is_cpuid_PSE36()) |
| 2190 | /* 36bits PSE 4MB page */ |
| 2191 | context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21); |
| 2192 | else |
| 2193 | /* 32 bits PSE 4MB page */ |
| 2194 | context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21); |
| 2195 | context->rsvd_bits_mask[1][0] = ~0ull; |
| 2196 | break; |
| 2197 | case PT32E_ROOT_LEVEL: |
| 2198 | context->rsvd_bits_mask[0][1] = exb_bit_rsvd | |
| 2199 | rsvd_bits(maxphyaddr, 62); /* PDE */ |
| 2200 | context->rsvd_bits_mask[0][0] = exb_bit_rsvd | |
| 2201 | rsvd_bits(maxphyaddr, 62); /* PTE */ |
| 2202 | context->rsvd_bits_mask[1][1] = exb_bit_rsvd | |
| 2203 | rsvd_bits(maxphyaddr, 62) | |
| 2204 | rsvd_bits(13, 20); /* large page */ |
| 2205 | context->rsvd_bits_mask[1][0] = ~0ull; |
| 2206 | break; |
| 2207 | case PT64_ROOT_LEVEL: |
| 2208 | context->rsvd_bits_mask[0][3] = exb_bit_rsvd | |
| 2209 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8); |
| 2210 | context->rsvd_bits_mask[0][2] = exb_bit_rsvd | |
| 2211 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8); |
| 2212 | context->rsvd_bits_mask[0][1] = exb_bit_rsvd | |
| 2213 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8); |
| 2214 | context->rsvd_bits_mask[0][0] = exb_bit_rsvd | |
| 2215 | rsvd_bits(maxphyaddr, 51); |
| 2216 | context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3]; |
| 2217 | context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2]; |
| 2218 | context->rsvd_bits_mask[1][1] = exb_bit_rsvd | |
| 2219 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(13, 20); |
| 2220 | context->rsvd_bits_mask[1][0] = ~0ull; |
| 2221 | break; |
| 2222 | } |
| 2223 | } |
| 2224 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2225 | static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2226 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2227 | struct kvm_mmu *context = &vcpu->arch.mmu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2228 | |
| 2229 | ASSERT(is_pae(vcpu)); |
| 2230 | context->new_cr3 = paging_new_cr3; |
| 2231 | context->page_fault = paging64_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2232 | context->gva_to_gpa = paging64_gva_to_gpa; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2233 | context->prefetch_page = paging64_prefetch_page; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 2234 | context->sync_page = paging64_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2235 | context->invlpg = paging64_invlpg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2236 | context->free = paging_free; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2237 | context->root_level = level; |
| 2238 | context->shadow_root_level = level; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2239 | context->root_hpa = INVALID_PAGE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2240 | return 0; |
| 2241 | } |
| 2242 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2243 | static int paging64_init_context(struct kvm_vcpu *vcpu) |
| 2244 | { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame^] | 2245 | reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2246 | return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL); |
| 2247 | } |
| 2248 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2249 | static int paging32_init_context(struct kvm_vcpu *vcpu) |
| 2250 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2251 | struct kvm_mmu *context = &vcpu->arch.mmu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2252 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame^] | 2253 | reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2254 | context->new_cr3 = paging_new_cr3; |
| 2255 | context->page_fault = paging32_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2256 | context->gva_to_gpa = paging32_gva_to_gpa; |
| 2257 | context->free = paging_free; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2258 | context->prefetch_page = paging32_prefetch_page; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 2259 | context->sync_page = paging32_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2260 | context->invlpg = paging32_invlpg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2261 | context->root_level = PT32_ROOT_LEVEL; |
| 2262 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2263 | context->root_hpa = INVALID_PAGE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2264 | return 0; |
| 2265 | } |
| 2266 | |
| 2267 | static int paging32E_init_context(struct kvm_vcpu *vcpu) |
| 2268 | { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame^] | 2269 | reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2270 | return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2271 | } |
| 2272 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2273 | static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) |
| 2274 | { |
| 2275 | struct kvm_mmu *context = &vcpu->arch.mmu; |
| 2276 | |
| 2277 | context->new_cr3 = nonpaging_new_cr3; |
| 2278 | context->page_fault = tdp_page_fault; |
| 2279 | context->free = nonpaging_free; |
| 2280 | context->prefetch_page = nonpaging_prefetch_page; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 2281 | context->sync_page = nonpaging_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2282 | context->invlpg = nonpaging_invlpg; |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 2283 | context->shadow_root_level = kvm_x86_ops->get_tdp_level(); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2284 | context->root_hpa = INVALID_PAGE; |
| 2285 | |
| 2286 | if (!is_paging(vcpu)) { |
| 2287 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
| 2288 | context->root_level = 0; |
| 2289 | } else if (is_long_mode(vcpu)) { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame^] | 2290 | reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2291 | context->gva_to_gpa = paging64_gva_to_gpa; |
| 2292 | context->root_level = PT64_ROOT_LEVEL; |
| 2293 | } else if (is_pae(vcpu)) { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame^] | 2294 | reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2295 | context->gva_to_gpa = paging64_gva_to_gpa; |
| 2296 | context->root_level = PT32E_ROOT_LEVEL; |
| 2297 | } else { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame^] | 2298 | reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2299 | context->gva_to_gpa = paging32_gva_to_gpa; |
| 2300 | context->root_level = PT32_ROOT_LEVEL; |
| 2301 | } |
| 2302 | |
| 2303 | return 0; |
| 2304 | } |
| 2305 | |
| 2306 | static int init_kvm_softmmu(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2307 | { |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 2308 | int r; |
| 2309 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2310 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2311 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2312 | |
| 2313 | if (!is_paging(vcpu)) |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 2314 | r = nonpaging_init_context(vcpu); |
Avi Kivity | a9058ec | 2006-12-29 16:49:37 -0800 | [diff] [blame] | 2315 | else if (is_long_mode(vcpu)) |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 2316 | r = paging64_init_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2317 | else if (is_pae(vcpu)) |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 2318 | r = paging32E_init_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2319 | else |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 2320 | r = paging32_init_context(vcpu); |
| 2321 | |
| 2322 | vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level; |
| 2323 | |
| 2324 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2325 | } |
| 2326 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2327 | static int init_kvm_mmu(struct kvm_vcpu *vcpu) |
| 2328 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2329 | vcpu->arch.update_pte.pfn = bad_pfn; |
| 2330 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2331 | if (tdp_enabled) |
| 2332 | return init_kvm_tdp_mmu(vcpu); |
| 2333 | else |
| 2334 | return init_kvm_softmmu(vcpu); |
| 2335 | } |
| 2336 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2337 | static void destroy_kvm_mmu(struct kvm_vcpu *vcpu) |
| 2338 | { |
| 2339 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2340 | if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) { |
| 2341 | vcpu->arch.mmu.free(vcpu); |
| 2342 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | int kvm_mmu_reset_context(struct kvm_vcpu *vcpu) |
| 2347 | { |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2348 | destroy_kvm_mmu(vcpu); |
| 2349 | return init_kvm_mmu(vcpu); |
| 2350 | } |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 2351 | EXPORT_SYMBOL_GPL(kvm_mmu_reset_context); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2352 | |
| 2353 | int kvm_mmu_load(struct kvm_vcpu *vcpu) |
| 2354 | { |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 2355 | int r; |
| 2356 | |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 2357 | r = mmu_topup_memory_caches(vcpu); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2358 | if (r) |
| 2359 | goto out; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2360 | spin_lock(&vcpu->kvm->mmu_lock); |
Avi Kivity | eb787d1 | 2007-12-31 15:27:49 +0200 | [diff] [blame] | 2361 | kvm_mmu_free_some_pages(vcpu); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2362 | mmu_alloc_roots(vcpu); |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 2363 | mmu_sync_roots(vcpu); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2364 | spin_unlock(&vcpu->kvm->mmu_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2365 | kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2366 | kvm_mmu_flush_tlb(vcpu); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 2367 | out: |
| 2368 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2369 | } |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2370 | EXPORT_SYMBOL_GPL(kvm_mmu_load); |
| 2371 | |
| 2372 | void kvm_mmu_unload(struct kvm_vcpu *vcpu) |
| 2373 | { |
| 2374 | mmu_free_roots(vcpu); |
| 2375 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2376 | |
Avi Kivity | 09072da | 2007-05-01 14:16:52 +0300 | [diff] [blame] | 2377 | static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2378 | struct kvm_mmu_page *sp, |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2379 | u64 *spte) |
| 2380 | { |
| 2381 | u64 pte; |
| 2382 | struct kvm_mmu_page *child; |
| 2383 | |
| 2384 | pte = *spte; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2385 | if (is_shadow_present_pte(pte)) { |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2386 | if (sp->role.level == PT_PAGE_TABLE_LEVEL || |
| 2387 | is_large_pte(pte)) |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 2388 | rmap_remove(vcpu->kvm, spte); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2389 | else { |
| 2390 | child = page_header(pte & PT64_BASE_ADDR_MASK); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 2391 | mmu_page_remove_parent_pte(child, spte); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2392 | } |
| 2393 | } |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2394 | set_shadow_pte(spte, shadow_trap_nonpresent_pte); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2395 | if (is_large_pte(pte)) |
| 2396 | --vcpu->kvm->stat.lpages; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2397 | } |
| 2398 | |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2399 | static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2400 | struct kvm_mmu_page *sp, |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2401 | u64 *spte, |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2402 | const void *new) |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2403 | { |
Marcelo Tosatti | 3094538 | 2008-06-11 20:32:40 -0300 | [diff] [blame] | 2404 | if (sp->role.level != PT_PAGE_TABLE_LEVEL) { |
| 2405 | if (!vcpu->arch.update_pte.largepage || |
| 2406 | sp->role.glevels == PT32_ROOT_LEVEL) { |
| 2407 | ++vcpu->kvm->stat.mmu_pde_zapped; |
| 2408 | return; |
| 2409 | } |
| 2410 | } |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2411 | |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 2412 | ++vcpu->kvm->stat.mmu_pte_updated; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2413 | if (sp->role.glevels == PT32_ROOT_LEVEL) |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2414 | paging32_update_pte(vcpu, sp, spte, new); |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2415 | else |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2416 | paging64_update_pte(vcpu, sp, spte, new); |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2417 | } |
| 2418 | |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 2419 | static bool need_remote_flush(u64 old, u64 new) |
| 2420 | { |
| 2421 | if (!is_shadow_present_pte(old)) |
| 2422 | return false; |
| 2423 | if (!is_shadow_present_pte(new)) |
| 2424 | return true; |
| 2425 | if ((old ^ new) & PT64_BASE_ADDR_MASK) |
| 2426 | return true; |
| 2427 | old ^= PT64_NX_MASK; |
| 2428 | new ^= PT64_NX_MASK; |
| 2429 | return (old & ~new & PT64_PERM_MASK) != 0; |
| 2430 | } |
| 2431 | |
| 2432 | static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new) |
| 2433 | { |
| 2434 | if (need_remote_flush(old, new)) |
| 2435 | kvm_flush_remote_tlbs(vcpu->kvm); |
| 2436 | else |
| 2437 | kvm_mmu_flush_tlb(vcpu); |
| 2438 | } |
| 2439 | |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 2440 | static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu) |
| 2441 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2442 | u64 *spte = vcpu->arch.last_pte_updated; |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 2443 | |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 2444 | return !!(spte && (*spte & shadow_accessed_mask)); |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 2445 | } |
| 2446 | |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2447 | static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 2448 | const u8 *new, int bytes) |
| 2449 | { |
| 2450 | gfn_t gfn; |
| 2451 | int r; |
| 2452 | u64 gpte = 0; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2453 | pfn_t pfn; |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2454 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2455 | vcpu->arch.update_pte.largepage = 0; |
| 2456 | |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2457 | if (bytes != 4 && bytes != 8) |
| 2458 | return; |
| 2459 | |
| 2460 | /* |
| 2461 | * Assume that the pte write on a page table of the same type |
| 2462 | * as the current vcpu paging mode. This is nearly always true |
| 2463 | * (might be false while changing modes). Note it is verified later |
| 2464 | * by update_pte(). |
| 2465 | */ |
| 2466 | if (is_pae(vcpu)) { |
| 2467 | /* Handle a 32-bit guest writing two halves of a 64-bit gpte */ |
| 2468 | if ((bytes == 4) && (gpa % 4 == 0)) { |
| 2469 | r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8); |
| 2470 | if (r) |
| 2471 | return; |
| 2472 | memcpy((void *)&gpte + (gpa % 8), new, 4); |
| 2473 | } else if ((bytes == 8) && (gpa % 8 == 0)) { |
| 2474 | memcpy((void *)&gpte, new, 8); |
| 2475 | } |
| 2476 | } else { |
| 2477 | if ((bytes == 4) && (gpa % 4 == 0)) |
| 2478 | memcpy((void *)&gpte, new, 4); |
| 2479 | } |
| 2480 | if (!is_present_pte(gpte)) |
| 2481 | return; |
| 2482 | gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2483 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2484 | if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) { |
| 2485 | gfn &= ~(KVM_PAGES_PER_HPAGE-1); |
| 2486 | vcpu->arch.update_pte.largepage = 1; |
| 2487 | } |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2488 | vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq; |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 2489 | smp_rmb(); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2490 | pfn = gfn_to_pfn(vcpu->kvm, gfn); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2491 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2492 | if (is_error_pfn(pfn)) { |
| 2493 | kvm_release_pfn_clean(pfn); |
Avi Kivity | d196e34 | 2008-01-24 11:44:11 +0200 | [diff] [blame] | 2494 | return; |
| 2495 | } |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2496 | vcpu->arch.update_pte.gfn = gfn; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2497 | vcpu->arch.update_pte.pfn = pfn; |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2498 | } |
| 2499 | |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 2500 | static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 2501 | { |
| 2502 | u64 *spte = vcpu->arch.last_pte_updated; |
| 2503 | |
| 2504 | if (spte |
| 2505 | && vcpu->arch.last_pte_gfn == gfn |
| 2506 | && shadow_accessed_mask |
| 2507 | && !(*spte & shadow_accessed_mask) |
| 2508 | && is_shadow_present_pte(*spte)) |
| 2509 | set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte); |
| 2510 | } |
| 2511 | |
Avi Kivity | 09072da | 2007-05-01 14:16:52 +0300 | [diff] [blame] | 2512 | void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, |
Marcelo Tosatti | ad218f8 | 2008-12-01 22:32:05 -0200 | [diff] [blame] | 2513 | const u8 *new, int bytes, |
| 2514 | bool guest_initiated) |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 2515 | { |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2516 | gfn_t gfn = gpa >> PAGE_SHIFT; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2517 | struct kvm_mmu_page *sp; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 2518 | struct hlist_node *node, *n; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2519 | struct hlist_head *bucket; |
| 2520 | unsigned index; |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2521 | u64 entry, gentry; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2522 | u64 *spte; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2523 | unsigned offset = offset_in_page(gpa); |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 2524 | unsigned pte_size; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2525 | unsigned page_offset; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 2526 | unsigned misaligned; |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 2527 | unsigned quadrant; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2528 | int level; |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 2529 | int flooded = 0; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2530 | int npte; |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2531 | int r; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2532 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2533 | pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes); |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2534 | mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2535 | spin_lock(&vcpu->kvm->mmu_lock); |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 2536 | kvm_mmu_access_page(vcpu, gfn); |
Avi Kivity | eb787d1 | 2007-12-31 15:27:49 +0200 | [diff] [blame] | 2537 | kvm_mmu_free_some_pages(vcpu); |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 2538 | ++vcpu->kvm->stat.mmu_pte_write; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2539 | kvm_mmu_audit(vcpu, "pre pte write"); |
Marcelo Tosatti | ad218f8 | 2008-12-01 22:32:05 -0200 | [diff] [blame] | 2540 | if (guest_initiated) { |
| 2541 | if (gfn == vcpu->arch.last_pt_write_gfn |
| 2542 | && !last_updated_pte_accessed(vcpu)) { |
| 2543 | ++vcpu->arch.last_pt_write_count; |
| 2544 | if (vcpu->arch.last_pt_write_count >= 3) |
| 2545 | flooded = 1; |
| 2546 | } else { |
| 2547 | vcpu->arch.last_pt_write_gfn = gfn; |
| 2548 | vcpu->arch.last_pt_write_count = 1; |
| 2549 | vcpu->arch.last_pte_updated = NULL; |
| 2550 | } |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 2551 | } |
Dong, Eddie | 1ae0a13 | 2008-01-07 13:20:25 +0200 | [diff] [blame] | 2552 | index = kvm_page_table_hashfn(gfn); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2553 | bucket = &vcpu->kvm->arch.mmu_page_hash[index]; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2554 | hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) { |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 2555 | if (sp->gfn != gfn || sp->role.direct || sp->role.invalid) |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2556 | continue; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2557 | pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 2558 | misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); |
Avi Kivity | e925c5b | 2007-04-30 14:47:02 +0300 | [diff] [blame] | 2559 | misaligned |= bytes < 4; |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 2560 | if (misaligned || flooded) { |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 2561 | /* |
| 2562 | * Misaligned accesses are too much trouble to fix |
| 2563 | * up; also, they usually indicate a page is not used |
| 2564 | * as a page table. |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 2565 | * |
| 2566 | * If we're seeing too many writes to a page, |
| 2567 | * it may no longer be a page table, or we may be |
| 2568 | * forking, in which case it is better to unmap the |
| 2569 | * page. |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 2570 | */ |
| 2571 | pgprintk("misaligned: gpa %llx bytes %d role %x\n", |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2572 | gpa, bytes, sp->role.word); |
Marcelo Tosatti | 0738541 | 2008-09-23 13:18:37 -0300 | [diff] [blame] | 2573 | if (kvm_mmu_zap_page(vcpu->kvm, sp)) |
| 2574 | n = bucket->first; |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 2575 | ++vcpu->kvm->stat.mmu_flooded; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 2576 | continue; |
| 2577 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2578 | page_offset = offset; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2579 | level = sp->role.level; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2580 | npte = 1; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2581 | if (sp->role.glevels == PT32_ROOT_LEVEL) { |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2582 | page_offset <<= 1; /* 32->64 */ |
| 2583 | /* |
| 2584 | * A 32-bit pde maps 4MB while the shadow pdes map |
| 2585 | * only 2MB. So we need to double the offset again |
| 2586 | * and zap two pdes instead of one. |
| 2587 | */ |
| 2588 | if (level == PT32_ROOT_LEVEL) { |
Avi Kivity | 6b8d0f9 | 2007-04-18 11:18:18 +0300 | [diff] [blame] | 2589 | page_offset &= ~7; /* kill rounding error */ |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2590 | page_offset <<= 1; |
| 2591 | npte = 2; |
| 2592 | } |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 2593 | quadrant = page_offset >> PAGE_SHIFT; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2594 | page_offset &= ~PAGE_MASK; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2595 | if (quadrant != sp->role.quadrant) |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 2596 | continue; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2597 | } |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2598 | spte = &sp->spt[page_offset / sizeof(*spte)]; |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2599 | if ((gpa & (pte_size - 1)) || (bytes < pte_size)) { |
| 2600 | gentry = 0; |
| 2601 | r = kvm_read_guest_atomic(vcpu->kvm, |
| 2602 | gpa & ~(u64)(pte_size - 1), |
| 2603 | &gentry, pte_size); |
| 2604 | new = (const void *)&gentry; |
| 2605 | if (r < 0) |
| 2606 | new = NULL; |
| 2607 | } |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2608 | while (npte--) { |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 2609 | entry = *spte; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2610 | mmu_pte_write_zap_pte(vcpu, sp, spte); |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2611 | if (new) |
| 2612 | mmu_pte_write_new_pte(vcpu, sp, spte, new); |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 2613 | mmu_pte_write_flush_tlb(vcpu, entry, *spte); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2614 | ++spte; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2615 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2616 | } |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2617 | kvm_mmu_audit(vcpu, "post pte write"); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2618 | spin_unlock(&vcpu->kvm->mmu_lock); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2619 | if (!is_error_pfn(vcpu->arch.update_pte.pfn)) { |
| 2620 | kvm_release_pfn_clean(vcpu->arch.update_pte.pfn); |
| 2621 | vcpu->arch.update_pte.pfn = bad_pfn; |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2622 | } |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 2623 | } |
| 2624 | |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2625 | int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva) |
| 2626 | { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2627 | gpa_t gpa; |
| 2628 | int r; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2629 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2630 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2631 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2632 | spin_lock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2633 | r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2634 | spin_unlock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2635 | return r; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2636 | } |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 2637 | EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2638 | |
Avi Kivity | 22d95b1 | 2007-09-14 20:26:06 +0300 | [diff] [blame] | 2639 | void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu) |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 2640 | { |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2641 | while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2642 | struct kvm_mmu_page *sp; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 2643 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2644 | sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2645 | struct kvm_mmu_page, link); |
| 2646 | kvm_mmu_zap_page(vcpu->kvm, sp); |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 2647 | ++vcpu->kvm->stat.mmu_recycled; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 2648 | } |
| 2649 | } |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 2650 | |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 2651 | int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code) |
| 2652 | { |
| 2653 | int r; |
| 2654 | enum emulation_result er; |
| 2655 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2656 | r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 2657 | if (r < 0) |
| 2658 | goto out; |
| 2659 | |
| 2660 | if (!r) { |
| 2661 | r = 1; |
| 2662 | goto out; |
| 2663 | } |
| 2664 | |
Avi Kivity | b733bfb | 2007-10-28 18:52:05 +0200 | [diff] [blame] | 2665 | r = mmu_topup_memory_caches(vcpu); |
| 2666 | if (r) |
| 2667 | goto out; |
| 2668 | |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 2669 | er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 2670 | |
| 2671 | switch (er) { |
| 2672 | case EMULATE_DONE: |
| 2673 | return 1; |
| 2674 | case EMULATE_DO_MMIO: |
| 2675 | ++vcpu->stat.mmio_exits; |
| 2676 | return 0; |
| 2677 | case EMULATE_FAIL: |
| 2678 | kvm_report_emulation_failure(vcpu, "pagetable"); |
| 2679 | return 1; |
| 2680 | default: |
| 2681 | BUG(); |
| 2682 | } |
| 2683 | out: |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 2684 | return r; |
| 2685 | } |
| 2686 | EXPORT_SYMBOL_GPL(kvm_mmu_page_fault); |
| 2687 | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2688 | void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva) |
| 2689 | { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2690 | vcpu->arch.mmu.invlpg(vcpu, gva); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2691 | kvm_mmu_flush_tlb(vcpu); |
| 2692 | ++vcpu->stat.invlpg; |
| 2693 | } |
| 2694 | EXPORT_SYMBOL_GPL(kvm_mmu_invlpg); |
| 2695 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 2696 | void kvm_enable_tdp(void) |
| 2697 | { |
| 2698 | tdp_enabled = true; |
| 2699 | } |
| 2700 | EXPORT_SYMBOL_GPL(kvm_enable_tdp); |
| 2701 | |
Joerg Roedel | 5f4cb66 | 2008-07-14 20:36:36 +0200 | [diff] [blame] | 2702 | void kvm_disable_tdp(void) |
| 2703 | { |
| 2704 | tdp_enabled = false; |
| 2705 | } |
| 2706 | EXPORT_SYMBOL_GPL(kvm_disable_tdp); |
| 2707 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2708 | static void free_mmu_pages(struct kvm_vcpu *vcpu) |
| 2709 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2710 | free_page((unsigned long)vcpu->arch.mmu.pae_root); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | static int alloc_mmu_pages(struct kvm_vcpu *vcpu) |
| 2714 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2715 | struct page *page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2716 | int i; |
| 2717 | |
| 2718 | ASSERT(vcpu); |
| 2719 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2720 | if (vcpu->kvm->arch.n_requested_mmu_pages) |
| 2721 | vcpu->kvm->arch.n_free_mmu_pages = |
| 2722 | vcpu->kvm->arch.n_requested_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2723 | else |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2724 | vcpu->kvm->arch.n_free_mmu_pages = |
| 2725 | vcpu->kvm->arch.n_alloc_mmu_pages; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2726 | /* |
| 2727 | * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64. |
| 2728 | * Therefore we need to allocate shadow page tables in the first |
| 2729 | * 4GB of memory, which happens to fit the DMA32 zone. |
| 2730 | */ |
| 2731 | page = alloc_page(GFP_KERNEL | __GFP_DMA32); |
| 2732 | if (!page) |
| 2733 | goto error_1; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2734 | vcpu->arch.mmu.pae_root = page_address(page); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2735 | for (i = 0; i < 4; ++i) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2736 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2737 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2738 | return 0; |
| 2739 | |
| 2740 | error_1: |
| 2741 | free_mmu_pages(vcpu); |
| 2742 | return -ENOMEM; |
| 2743 | } |
| 2744 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 2745 | int kvm_mmu_create(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2746 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2747 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2748 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2749 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 2750 | return alloc_mmu_pages(vcpu); |
| 2751 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2752 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 2753 | int kvm_mmu_setup(struct kvm_vcpu *vcpu) |
| 2754 | { |
| 2755 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2756 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 2c26495 | 2006-12-22 01:05:28 -0800 | [diff] [blame] | 2757 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 2758 | return init_kvm_mmu(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | void kvm_mmu_destroy(struct kvm_vcpu *vcpu) |
| 2762 | { |
| 2763 | ASSERT(vcpu); |
| 2764 | |
| 2765 | destroy_kvm_mmu(vcpu); |
| 2766 | free_mmu_pages(vcpu); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 2767 | mmu_free_memory_caches(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2768 | } |
| 2769 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 2770 | void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2771 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2772 | struct kvm_mmu_page *sp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2773 | |
Avi Kivity | 2245a28 | 2008-08-27 16:32:24 +0300 | [diff] [blame] | 2774 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2775 | list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2776 | int i; |
| 2777 | u64 *pt; |
| 2778 | |
Sheng Yang | 291f26b | 2008-10-16 17:30:57 +0800 | [diff] [blame] | 2779 | if (!test_bit(slot, sp->slot_bitmap)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2780 | continue; |
| 2781 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2782 | pt = sp->spt; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2783 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
| 2784 | /* avoid RMW */ |
Izik Eidus | 9647c14 | 2007-10-16 14:43:46 +0200 | [diff] [blame] | 2785 | if (pt[i] & PT_WRITABLE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2786 | pt[i] &= ~PT_WRITABLE_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2787 | } |
Avi Kivity | 171d595 | 2008-08-27 16:40:51 +0300 | [diff] [blame] | 2788 | kvm_flush_remote_tlbs(kvm); |
Avi Kivity | 2245a28 | 2008-08-27 16:32:24 +0300 | [diff] [blame] | 2789 | spin_unlock(&kvm->mmu_lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2790 | } |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2791 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 2792 | void kvm_mmu_zap_all(struct kvm *kvm) |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 2793 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2794 | struct kvm_mmu_page *sp, *node; |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 2795 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2796 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2797 | list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) |
Marcelo Tosatti | 0738541 | 2008-09-23 13:18:37 -0300 | [diff] [blame] | 2798 | if (kvm_mmu_zap_page(kvm, sp)) |
| 2799 | node = container_of(kvm->arch.active_mmu_pages.next, |
| 2800 | struct kvm_mmu_page, link); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2801 | spin_unlock(&kvm->mmu_lock); |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 2802 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 2803 | kvm_flush_remote_tlbs(kvm); |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 2804 | } |
| 2805 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 2806 | static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm) |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 2807 | { |
| 2808 | struct kvm_mmu_page *page; |
| 2809 | |
| 2810 | page = container_of(kvm->arch.active_mmu_pages.prev, |
| 2811 | struct kvm_mmu_page, link); |
| 2812 | kvm_mmu_zap_page(kvm, page); |
| 2813 | } |
| 2814 | |
| 2815 | static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask) |
| 2816 | { |
| 2817 | struct kvm *kvm; |
| 2818 | struct kvm *kvm_freed = NULL; |
| 2819 | int cache_count = 0; |
| 2820 | |
| 2821 | spin_lock(&kvm_lock); |
| 2822 | |
| 2823 | list_for_each_entry(kvm, &vm_list, vm_list) { |
| 2824 | int npages; |
| 2825 | |
Marcelo Tosatti | 5a4c928 | 2008-07-03 18:33:02 -0300 | [diff] [blame] | 2826 | if (!down_read_trylock(&kvm->slots_lock)) |
| 2827 | continue; |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 2828 | spin_lock(&kvm->mmu_lock); |
| 2829 | npages = kvm->arch.n_alloc_mmu_pages - |
| 2830 | kvm->arch.n_free_mmu_pages; |
| 2831 | cache_count += npages; |
| 2832 | if (!kvm_freed && nr_to_scan > 0 && npages > 0) { |
| 2833 | kvm_mmu_remove_one_alloc_mmu_page(kvm); |
| 2834 | cache_count--; |
| 2835 | kvm_freed = kvm; |
| 2836 | } |
| 2837 | nr_to_scan--; |
| 2838 | |
| 2839 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | 5a4c928 | 2008-07-03 18:33:02 -0300 | [diff] [blame] | 2840 | up_read(&kvm->slots_lock); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 2841 | } |
| 2842 | if (kvm_freed) |
| 2843 | list_move_tail(&kvm_freed->vm_list, &vm_list); |
| 2844 | |
| 2845 | spin_unlock(&kvm_lock); |
| 2846 | |
| 2847 | return cache_count; |
| 2848 | } |
| 2849 | |
| 2850 | static struct shrinker mmu_shrinker = { |
| 2851 | .shrink = mmu_shrink, |
| 2852 | .seeks = DEFAULT_SEEKS * 10, |
| 2853 | }; |
| 2854 | |
Ingo Molnar | 2ddfd20 | 2008-05-22 10:37:48 +0200 | [diff] [blame] | 2855 | static void mmu_destroy_caches(void) |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2856 | { |
| 2857 | if (pte_chain_cache) |
| 2858 | kmem_cache_destroy(pte_chain_cache); |
| 2859 | if (rmap_desc_cache) |
| 2860 | kmem_cache_destroy(rmap_desc_cache); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 2861 | if (mmu_page_header_cache) |
| 2862 | kmem_cache_destroy(mmu_page_header_cache); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2863 | } |
| 2864 | |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 2865 | void kvm_mmu_module_exit(void) |
| 2866 | { |
| 2867 | mmu_destroy_caches(); |
| 2868 | unregister_shrinker(&mmu_shrinker); |
| 2869 | } |
| 2870 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2871 | int kvm_mmu_module_init(void) |
| 2872 | { |
| 2873 | pte_chain_cache = kmem_cache_create("kvm_pte_chain", |
| 2874 | sizeof(struct kvm_pte_chain), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2875 | 0, 0, NULL); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2876 | if (!pte_chain_cache) |
| 2877 | goto nomem; |
| 2878 | rmap_desc_cache = kmem_cache_create("kvm_rmap_desc", |
| 2879 | sizeof(struct kvm_rmap_desc), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2880 | 0, 0, NULL); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2881 | if (!rmap_desc_cache) |
| 2882 | goto nomem; |
| 2883 | |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 2884 | mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header", |
| 2885 | sizeof(struct kvm_mmu_page), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2886 | 0, 0, NULL); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 2887 | if (!mmu_page_header_cache) |
| 2888 | goto nomem; |
| 2889 | |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 2890 | register_shrinker(&mmu_shrinker); |
| 2891 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2892 | return 0; |
| 2893 | |
| 2894 | nomem: |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 2895 | mmu_destroy_caches(); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2896 | return -ENOMEM; |
| 2897 | } |
| 2898 | |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 2899 | /* |
| 2900 | * Caculate mmu pages needed for kvm. |
| 2901 | */ |
| 2902 | unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm) |
| 2903 | { |
| 2904 | int i; |
| 2905 | unsigned int nr_mmu_pages; |
| 2906 | unsigned int nr_pages = 0; |
| 2907 | |
| 2908 | for (i = 0; i < kvm->nmemslots; i++) |
| 2909 | nr_pages += kvm->memslots[i].npages; |
| 2910 | |
| 2911 | nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000; |
| 2912 | nr_mmu_pages = max(nr_mmu_pages, |
| 2913 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); |
| 2914 | |
| 2915 | return nr_mmu_pages; |
| 2916 | } |
| 2917 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2918 | static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer, |
| 2919 | unsigned len) |
| 2920 | { |
| 2921 | if (len > buffer->len) |
| 2922 | return NULL; |
| 2923 | return buffer->ptr; |
| 2924 | } |
| 2925 | |
| 2926 | static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer, |
| 2927 | unsigned len) |
| 2928 | { |
| 2929 | void *ret; |
| 2930 | |
| 2931 | ret = pv_mmu_peek_buffer(buffer, len); |
| 2932 | if (!ret) |
| 2933 | return ret; |
| 2934 | buffer->ptr += len; |
| 2935 | buffer->len -= len; |
| 2936 | buffer->processed += len; |
| 2937 | return ret; |
| 2938 | } |
| 2939 | |
| 2940 | static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu, |
| 2941 | gpa_t addr, gpa_t value) |
| 2942 | { |
| 2943 | int bytes = 8; |
| 2944 | int r; |
| 2945 | |
| 2946 | if (!is_long_mode(vcpu) && !is_pae(vcpu)) |
| 2947 | bytes = 4; |
| 2948 | |
| 2949 | r = mmu_topup_memory_caches(vcpu); |
| 2950 | if (r) |
| 2951 | return r; |
| 2952 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2953 | if (!emulator_write_phys(vcpu, addr, &value, bytes)) |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2954 | return -EFAULT; |
| 2955 | |
| 2956 | return 1; |
| 2957 | } |
| 2958 | |
| 2959 | static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
| 2960 | { |
Avi Kivity | a8cd024 | 2009-05-24 22:15:25 +0300 | [diff] [blame] | 2961 | kvm_set_cr3(vcpu, vcpu->arch.cr3); |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2962 | return 1; |
| 2963 | } |
| 2964 | |
| 2965 | static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr) |
| 2966 | { |
| 2967 | spin_lock(&vcpu->kvm->mmu_lock); |
| 2968 | mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT); |
| 2969 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2970 | return 1; |
| 2971 | } |
| 2972 | |
| 2973 | static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu, |
| 2974 | struct kvm_pv_mmu_op_buffer *buffer) |
| 2975 | { |
| 2976 | struct kvm_mmu_op_header *header; |
| 2977 | |
| 2978 | header = pv_mmu_peek_buffer(buffer, sizeof *header); |
| 2979 | if (!header) |
| 2980 | return 0; |
| 2981 | switch (header->op) { |
| 2982 | case KVM_MMU_OP_WRITE_PTE: { |
| 2983 | struct kvm_mmu_op_write_pte *wpte; |
| 2984 | |
| 2985 | wpte = pv_mmu_read_buffer(buffer, sizeof *wpte); |
| 2986 | if (!wpte) |
| 2987 | return 0; |
| 2988 | return kvm_pv_mmu_write(vcpu, wpte->pte_phys, |
| 2989 | wpte->pte_val); |
| 2990 | } |
| 2991 | case KVM_MMU_OP_FLUSH_TLB: { |
| 2992 | struct kvm_mmu_op_flush_tlb *ftlb; |
| 2993 | |
| 2994 | ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb); |
| 2995 | if (!ftlb) |
| 2996 | return 0; |
| 2997 | return kvm_pv_mmu_flush_tlb(vcpu); |
| 2998 | } |
| 2999 | case KVM_MMU_OP_RELEASE_PT: { |
| 3000 | struct kvm_mmu_op_release_pt *rpt; |
| 3001 | |
| 3002 | rpt = pv_mmu_read_buffer(buffer, sizeof *rpt); |
| 3003 | if (!rpt) |
| 3004 | return 0; |
| 3005 | return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys); |
| 3006 | } |
| 3007 | default: return 0; |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes, |
| 3012 | gpa_t addr, unsigned long *ret) |
| 3013 | { |
| 3014 | int r; |
Dave Hansen | 6ad18fb | 2008-08-11 10:01:49 -0700 | [diff] [blame] | 3015 | struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3016 | |
Dave Hansen | 6ad18fb | 2008-08-11 10:01:49 -0700 | [diff] [blame] | 3017 | buffer->ptr = buffer->buf; |
| 3018 | buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf); |
| 3019 | buffer->processed = 0; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3020 | |
Dave Hansen | 6ad18fb | 2008-08-11 10:01:49 -0700 | [diff] [blame] | 3021 | r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len); |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3022 | if (r) |
| 3023 | goto out; |
| 3024 | |
Dave Hansen | 6ad18fb | 2008-08-11 10:01:49 -0700 | [diff] [blame] | 3025 | while (buffer->len) { |
| 3026 | r = kvm_pv_mmu_op_one(vcpu, buffer); |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3027 | if (r < 0) |
| 3028 | goto out; |
| 3029 | if (r == 0) |
| 3030 | break; |
| 3031 | } |
| 3032 | |
| 3033 | r = 1; |
| 3034 | out: |
Dave Hansen | 6ad18fb | 2008-08-11 10:01:49 -0700 | [diff] [blame] | 3035 | *ret = buffer->processed; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3036 | return r; |
| 3037 | } |
| 3038 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3039 | #ifdef AUDIT |
| 3040 | |
| 3041 | static const char *audit_msg; |
| 3042 | |
| 3043 | static gva_t canonicalize(gva_t gva) |
| 3044 | { |
| 3045 | #ifdef CONFIG_X86_64 |
| 3046 | gva = (long long)(gva << 16) >> 16; |
| 3047 | #endif |
| 3048 | return gva; |
| 3049 | } |
| 3050 | |
| 3051 | static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte, |
| 3052 | gva_t va, int level) |
| 3053 | { |
| 3054 | u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK); |
| 3055 | int i; |
| 3056 | gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1)); |
| 3057 | |
| 3058 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) { |
| 3059 | u64 ent = pt[i]; |
| 3060 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3061 | if (ent == shadow_trap_nonpresent_pte) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3062 | continue; |
| 3063 | |
| 3064 | va = canonicalize(va); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3065 | if (level > 1) { |
| 3066 | if (ent == shadow_notrap_nonpresent_pte) |
| 3067 | printk(KERN_ERR "audit: (%s) nontrapping pte" |
| 3068 | " in nonleaf level: levels %d gva %lx" |
| 3069 | " level %d pte %llx\n", audit_msg, |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3070 | vcpu->arch.mmu.root_level, va, level, ent); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3071 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3072 | audit_mappings_page(vcpu, ent, va, level - 1); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3073 | } else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3074 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 3075 | hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3076 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3077 | if (is_shadow_present_pte(ent) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3078 | && (ent & PT64_BASE_ADDR_MASK) != hpa) |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3079 | printk(KERN_ERR "xx audit error: (%s) levels %d" |
| 3080 | " gva %lx gpa %llx hpa %llx ent %llx %d\n", |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3081 | audit_msg, vcpu->arch.mmu.root_level, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3082 | va, gpa, hpa, ent, |
| 3083 | is_shadow_present_pte(ent)); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3084 | else if (ent == shadow_notrap_nonpresent_pte |
| 3085 | && !is_error_hpa(hpa)) |
| 3086 | printk(KERN_ERR "audit: (%s) notrap shadow," |
| 3087 | " valid guest gva %lx\n", audit_msg, va); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 3088 | kvm_release_pfn_clean(pfn); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3089 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3090 | } |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | static void audit_mappings(struct kvm_vcpu *vcpu) |
| 3095 | { |
Avi Kivity | 1ea252a | 2007-03-08 11:48:09 +0200 | [diff] [blame] | 3096 | unsigned i; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3097 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3098 | if (vcpu->arch.mmu.root_level == 4) |
| 3099 | audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3100 | else |
| 3101 | for (i = 0; i < 4; ++i) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3102 | if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3103 | audit_mappings_page(vcpu, |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3104 | vcpu->arch.mmu.pae_root[i], |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3105 | i << 30, |
| 3106 | 2); |
| 3107 | } |
| 3108 | |
| 3109 | static int count_rmaps(struct kvm_vcpu *vcpu) |
| 3110 | { |
| 3111 | int nmaps = 0; |
| 3112 | int i, j, k; |
| 3113 | |
| 3114 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 3115 | struct kvm_memory_slot *m = &vcpu->kvm->memslots[i]; |
| 3116 | struct kvm_rmap_desc *d; |
| 3117 | |
| 3118 | for (j = 0; j < m->npages; ++j) { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 3119 | unsigned long *rmapp = &m->rmap[j]; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3120 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 3121 | if (!*rmapp) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3122 | continue; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 3123 | if (!(*rmapp & 1)) { |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3124 | ++nmaps; |
| 3125 | continue; |
| 3126 | } |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 3127 | d = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3128 | while (d) { |
| 3129 | for (k = 0; k < RMAP_EXT; ++k) |
| 3130 | if (d->shadow_ptes[k]) |
| 3131 | ++nmaps; |
| 3132 | else |
| 3133 | break; |
| 3134 | d = d->more; |
| 3135 | } |
| 3136 | } |
| 3137 | } |
| 3138 | return nmaps; |
| 3139 | } |
| 3140 | |
| 3141 | static int count_writable_mappings(struct kvm_vcpu *vcpu) |
| 3142 | { |
| 3143 | int nmaps = 0; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3144 | struct kvm_mmu_page *sp; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3145 | int i; |
| 3146 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 3147 | list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3148 | u64 *pt = sp->spt; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3149 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3150 | if (sp->role.level != PT_PAGE_TABLE_LEVEL) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3151 | continue; |
| 3152 | |
| 3153 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 3154 | u64 ent = pt[i]; |
| 3155 | |
| 3156 | if (!(ent & PT_PRESENT_MASK)) |
| 3157 | continue; |
| 3158 | if (!(ent & PT_WRITABLE_MASK)) |
| 3159 | continue; |
| 3160 | ++nmaps; |
| 3161 | } |
| 3162 | } |
| 3163 | return nmaps; |
| 3164 | } |
| 3165 | |
| 3166 | static void audit_rmap(struct kvm_vcpu *vcpu) |
| 3167 | { |
| 3168 | int n_rmap = count_rmaps(vcpu); |
| 3169 | int n_actual = count_writable_mappings(vcpu); |
| 3170 | |
| 3171 | if (n_rmap != n_actual) |
| 3172 | printk(KERN_ERR "%s: (%s) rmap %d actual %d\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 3173 | __func__, audit_msg, n_rmap, n_actual); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | static void audit_write_protection(struct kvm_vcpu *vcpu) |
| 3177 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3178 | struct kvm_mmu_page *sp; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 3179 | struct kvm_memory_slot *slot; |
| 3180 | unsigned long *rmapp; |
| 3181 | gfn_t gfn; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3182 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 3183 | list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) { |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 3184 | if (sp->role.direct) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3185 | continue; |
| 3186 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3187 | gfn = unalias_gfn(vcpu->kvm, sp->gfn); |
Izik Eidus | 2843099 | 2008-10-03 17:40:32 +0300 | [diff] [blame] | 3188 | slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 3189 | rmapp = &slot->rmap[gfn - slot->base_gfn]; |
| 3190 | if (*rmapp) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3191 | printk(KERN_ERR "%s: (%s) shadow page has writable" |
| 3192 | " mappings: gfn %lx role %x\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 3193 | __func__, audit_msg, sp->gfn, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3194 | sp->role.word); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3195 | } |
| 3196 | } |
| 3197 | |
| 3198 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) |
| 3199 | { |
| 3200 | int olddbg = dbg; |
| 3201 | |
| 3202 | dbg = 0; |
| 3203 | audit_msg = msg; |
| 3204 | audit_rmap(vcpu); |
| 3205 | audit_write_protection(vcpu); |
| 3206 | audit_mappings(vcpu); |
| 3207 | dbg = olddbg; |
| 3208 | } |
| 3209 | |
| 3210 | #endif |