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 | |
| 20 | #include "vmx.h" |
| 21 | #include "kvm.h" |
| 22 | |
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> |
| 28 | |
| 29 | #include <asm/page.h> |
| 30 | #include <asm/cmpxchg.h> |
| 31 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 32 | #undef MMU_DEBUG |
| 33 | |
| 34 | #undef AUDIT |
| 35 | |
| 36 | #ifdef AUDIT |
| 37 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg); |
| 38 | #else |
| 39 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {} |
| 40 | #endif |
| 41 | |
| 42 | #ifdef MMU_DEBUG |
| 43 | |
| 44 | #define pgprintk(x...) do { if (dbg) printk(x); } while (0) |
| 45 | #define rmap_printk(x...) do { if (dbg) printk(x); } while (0) |
| 46 | |
| 47 | #else |
| 48 | |
| 49 | #define pgprintk(x...) do { } while (0) |
| 50 | #define rmap_printk(x...) do { } while (0) |
| 51 | |
| 52 | #endif |
| 53 | |
| 54 | #if defined(MMU_DEBUG) || defined(AUDIT) |
| 55 | static int dbg = 1; |
| 56 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 57 | |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 58 | #ifndef MMU_DEBUG |
| 59 | #define ASSERT(x) do { } while (0) |
| 60 | #else |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 61 | #define ASSERT(x) \ |
| 62 | if (!(x)) { \ |
| 63 | printk(KERN_WARNING "assertion failed %s:%d: %s\n", \ |
| 64 | __FILE__, __LINE__, #x); \ |
| 65 | } |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 66 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 67 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 68 | #define PT64_PT_BITS 9 |
| 69 | #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS) |
| 70 | #define PT32_PT_BITS 10 |
| 71 | #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 72 | |
| 73 | #define PT_WRITABLE_SHIFT 1 |
| 74 | |
| 75 | #define PT_PRESENT_MASK (1ULL << 0) |
| 76 | #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT) |
| 77 | #define PT_USER_MASK (1ULL << 2) |
| 78 | #define PT_PWT_MASK (1ULL << 3) |
| 79 | #define PT_PCD_MASK (1ULL << 4) |
| 80 | #define PT_ACCESSED_MASK (1ULL << 5) |
| 81 | #define PT_DIRTY_MASK (1ULL << 6) |
| 82 | #define PT_PAGE_SIZE_MASK (1ULL << 7) |
| 83 | #define PT_PAT_MASK (1ULL << 7) |
| 84 | #define PT_GLOBAL_MASK (1ULL << 8) |
| 85 | #define PT64_NX_MASK (1ULL << 63) |
| 86 | |
| 87 | #define PT_PAT_SHIFT 7 |
| 88 | #define PT_DIR_PAT_SHIFT 12 |
| 89 | #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT) |
| 90 | |
| 91 | #define PT32_DIR_PSE36_SIZE 4 |
| 92 | #define PT32_DIR_PSE36_SHIFT 13 |
| 93 | #define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT) |
| 94 | |
| 95 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 96 | #define PT_FIRST_AVAIL_BITS_SHIFT 9 |
| 97 | #define PT64_SECOND_AVAIL_BITS_SHIFT 52 |
| 98 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 99 | #define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT) |
| 100 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 101 | #define VALID_PAGE(x) ((x) != INVALID_PAGE) |
| 102 | |
| 103 | #define PT64_LEVEL_BITS 9 |
| 104 | |
| 105 | #define PT64_LEVEL_SHIFT(level) \ |
| 106 | ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS ) |
| 107 | |
| 108 | #define PT64_LEVEL_MASK(level) \ |
| 109 | (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level)) |
| 110 | |
| 111 | #define PT64_INDEX(address, level)\ |
| 112 | (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1)) |
| 113 | |
| 114 | |
| 115 | #define PT32_LEVEL_BITS 10 |
| 116 | |
| 117 | #define PT32_LEVEL_SHIFT(level) \ |
| 118 | ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS ) |
| 119 | |
| 120 | #define PT32_LEVEL_MASK(level) \ |
| 121 | (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level)) |
| 122 | |
| 123 | #define PT32_INDEX(address, level)\ |
| 124 | (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1)) |
| 125 | |
| 126 | |
Avi Kivity | 27aba76 | 2007-03-09 13:04:31 +0200 | [diff] [blame] | 127 | #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 128 | #define PT64_DIR_BASE_ADDR_MASK \ |
| 129 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1)) |
| 130 | |
| 131 | #define PT32_BASE_ADDR_MASK PAGE_MASK |
| 132 | #define PT32_DIR_BASE_ADDR_MASK \ |
| 133 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1)) |
| 134 | |
| 135 | |
| 136 | #define PFERR_PRESENT_MASK (1U << 0) |
| 137 | #define PFERR_WRITE_MASK (1U << 1) |
| 138 | #define PFERR_USER_MASK (1U << 2) |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 139 | #define PFERR_FETCH_MASK (1U << 4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 140 | |
| 141 | #define PT64_ROOT_LEVEL 4 |
| 142 | #define PT32_ROOT_LEVEL 2 |
| 143 | #define PT32E_ROOT_LEVEL 3 |
| 144 | |
| 145 | #define PT_DIRECTORY_LEVEL 2 |
| 146 | #define PT_PAGE_TABLE_LEVEL 1 |
| 147 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 148 | #define RMAP_EXT 4 |
| 149 | |
| 150 | struct kvm_rmap_desc { |
| 151 | u64 *shadow_ptes[RMAP_EXT]; |
| 152 | struct kvm_rmap_desc *more; |
| 153 | }; |
| 154 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 155 | static struct kmem_cache *pte_chain_cache; |
| 156 | static struct kmem_cache *rmap_desc_cache; |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 157 | static struct kmem_cache *mmu_page_header_cache; |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 158 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 159 | static int is_write_protection(struct kvm_vcpu *vcpu) |
| 160 | { |
| 161 | return vcpu->cr0 & CR0_WP_MASK; |
| 162 | } |
| 163 | |
| 164 | static int is_cpuid_PSE36(void) |
| 165 | { |
| 166 | return 1; |
| 167 | } |
| 168 | |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 169 | static int is_nx(struct kvm_vcpu *vcpu) |
| 170 | { |
| 171 | return vcpu->shadow_efer & EFER_NX; |
| 172 | } |
| 173 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 174 | static int is_present_pte(unsigned long pte) |
| 175 | { |
| 176 | return pte & PT_PRESENT_MASK; |
| 177 | } |
| 178 | |
| 179 | static int is_writeble_pte(unsigned long pte) |
| 180 | { |
| 181 | return pte & PT_WRITABLE_MASK; |
| 182 | } |
| 183 | |
| 184 | static int is_io_pte(unsigned long pte) |
| 185 | { |
| 186 | return pte & PT_SHADOW_IO_MARK; |
| 187 | } |
| 188 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 189 | static int is_rmap_pte(u64 pte) |
| 190 | { |
| 191 | return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK)) |
| 192 | == (PT_WRITABLE_MASK | PT_PRESENT_MASK); |
| 193 | } |
| 194 | |
Avi Kivity | e663ee6 | 2007-05-31 15:46:04 +0300 | [diff] [blame] | 195 | static void set_shadow_pte(u64 *sptep, u64 spte) |
| 196 | { |
| 197 | #ifdef CONFIG_X86_64 |
| 198 | set_64bit((unsigned long *)sptep, spte); |
| 199 | #else |
| 200 | set_64bit((unsigned long long *)sptep, spte); |
| 201 | #endif |
| 202 | } |
| 203 | |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 204 | static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache, |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 205 | struct kmem_cache *base_cache, int min, |
| 206 | gfp_t gfp_flags) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 207 | { |
| 208 | void *obj; |
| 209 | |
| 210 | if (cache->nobjs >= min) |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 211 | return 0; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 212 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 213 | obj = kmem_cache_zalloc(base_cache, gfp_flags); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 214 | if (!obj) |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 215 | return -ENOMEM; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 216 | cache->objects[cache->nobjs++] = obj; |
| 217 | } |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 218 | return 0; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc) |
| 222 | { |
| 223 | while (mc->nobjs) |
| 224 | kfree(mc->objects[--mc->nobjs]); |
| 225 | } |
| 226 | |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame^] | 227 | static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache, |
| 228 | int min, gfp_t gfp_flags) |
| 229 | { |
| 230 | struct page *page; |
| 231 | |
| 232 | if (cache->nobjs >= min) |
| 233 | return 0; |
| 234 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { |
| 235 | page = alloc_page(gfp_flags); |
| 236 | if (!page) |
| 237 | return -ENOMEM; |
| 238 | set_page_private(page, 0); |
| 239 | cache->objects[cache->nobjs++] = page_address(page); |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc) |
| 245 | { |
| 246 | while (mc->nobjs) |
| 247 | __free_page(mc->objects[--mc->nobjs]); |
| 248 | } |
| 249 | |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 250 | static int __mmu_topup_memory_caches(struct kvm_vcpu *vcpu, gfp_t gfp_flags) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 251 | { |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 252 | int r; |
| 253 | |
| 254 | r = mmu_topup_memory_cache(&vcpu->mmu_pte_chain_cache, |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 255 | pte_chain_cache, 4, gfp_flags); |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 256 | if (r) |
| 257 | goto out; |
| 258 | r = mmu_topup_memory_cache(&vcpu->mmu_rmap_desc_cache, |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 259 | rmap_desc_cache, 1, gfp_flags); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 260 | if (r) |
| 261 | goto out; |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame^] | 262 | r = mmu_topup_memory_cache_page(&vcpu->mmu_page_cache, 4, gfp_flags); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 263 | if (r) |
| 264 | goto out; |
| 265 | r = mmu_topup_memory_cache(&vcpu->mmu_page_header_cache, |
| 266 | mmu_page_header_cache, 4, gfp_flags); |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 267 | out: |
| 268 | return r; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 271 | static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu) |
| 272 | { |
| 273 | int r; |
| 274 | |
| 275 | r = __mmu_topup_memory_caches(vcpu, GFP_NOWAIT); |
| 276 | if (r < 0) { |
| 277 | spin_unlock(&vcpu->kvm->lock); |
| 278 | kvm_arch_ops->vcpu_put(vcpu); |
| 279 | r = __mmu_topup_memory_caches(vcpu, GFP_KERNEL); |
| 280 | kvm_arch_ops->vcpu_load(vcpu); |
| 281 | spin_lock(&vcpu->kvm->lock); |
| 282 | } |
| 283 | return r; |
| 284 | } |
| 285 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 286 | static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) |
| 287 | { |
| 288 | mmu_free_memory_cache(&vcpu->mmu_pte_chain_cache); |
| 289 | mmu_free_memory_cache(&vcpu->mmu_rmap_desc_cache); |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame^] | 290 | mmu_free_memory_cache_page(&vcpu->mmu_page_cache); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 291 | mmu_free_memory_cache(&vcpu->mmu_page_header_cache); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc, |
| 295 | size_t size) |
| 296 | { |
| 297 | void *p; |
| 298 | |
| 299 | BUG_ON(!mc->nobjs); |
| 300 | p = mc->objects[--mc->nobjs]; |
| 301 | memset(p, 0, size); |
| 302 | return p; |
| 303 | } |
| 304 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 305 | static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu) |
| 306 | { |
| 307 | return mmu_memory_cache_alloc(&vcpu->mmu_pte_chain_cache, |
| 308 | sizeof(struct kvm_pte_chain)); |
| 309 | } |
| 310 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 311 | static void mmu_free_pte_chain(struct kvm_pte_chain *pc) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 312 | { |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 313 | kfree(pc); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu) |
| 317 | { |
| 318 | return mmu_memory_cache_alloc(&vcpu->mmu_rmap_desc_cache, |
| 319 | sizeof(struct kvm_rmap_desc)); |
| 320 | } |
| 321 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 322 | static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 323 | { |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 324 | kfree(rd); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 327 | /* |
| 328 | * Reverse mapping data structures: |
| 329 | * |
| 330 | * If page->private bit zero is zero, then page->private points to the |
| 331 | * shadow page table entry that points to page_address(page). |
| 332 | * |
| 333 | * If page->private bit zero is one, (then page->private & ~1) points |
| 334 | * to a struct kvm_rmap_desc containing more mappings. |
| 335 | */ |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 336 | static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 337 | { |
| 338 | struct page *page; |
| 339 | struct kvm_rmap_desc *desc; |
| 340 | int i; |
| 341 | |
| 342 | if (!is_rmap_pte(*spte)) |
| 343 | return; |
| 344 | page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT); |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 345 | if (!page_private(page)) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 346 | rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte); |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 347 | set_page_private(page,(unsigned long)spte); |
| 348 | } else if (!(page_private(page) & 1)) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 349 | rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 350 | desc = mmu_alloc_rmap_desc(vcpu); |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 351 | desc->shadow_ptes[0] = (u64 *)page_private(page); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 352 | desc->shadow_ptes[1] = spte; |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 353 | set_page_private(page,(unsigned long)desc | 1); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 354 | } else { |
| 355 | rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte); |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 356 | desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 357 | while (desc->shadow_ptes[RMAP_EXT-1] && desc->more) |
| 358 | desc = desc->more; |
| 359 | if (desc->shadow_ptes[RMAP_EXT-1]) { |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 360 | desc->more = mmu_alloc_rmap_desc(vcpu); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 361 | desc = desc->more; |
| 362 | } |
| 363 | for (i = 0; desc->shadow_ptes[i]; ++i) |
| 364 | ; |
| 365 | desc->shadow_ptes[i] = spte; |
| 366 | } |
| 367 | } |
| 368 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 369 | static void rmap_desc_remove_entry(struct page *page, |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 370 | struct kvm_rmap_desc *desc, |
| 371 | int i, |
| 372 | struct kvm_rmap_desc *prev_desc) |
| 373 | { |
| 374 | int j; |
| 375 | |
| 376 | for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j) |
| 377 | ; |
| 378 | desc->shadow_ptes[i] = desc->shadow_ptes[j]; |
Al Viro | 11718b4d | 2007-02-09 16:39:20 +0000 | [diff] [blame] | 379 | desc->shadow_ptes[j] = NULL; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 380 | if (j != 0) |
| 381 | return; |
| 382 | if (!prev_desc && !desc->more) |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 383 | set_page_private(page,(unsigned long)desc->shadow_ptes[0]); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 384 | else |
| 385 | if (prev_desc) |
| 386 | prev_desc->more = desc->more; |
| 387 | else |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 388 | set_page_private(page,(unsigned long)desc->more | 1); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 389 | mmu_free_rmap_desc(desc); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 392 | static void rmap_remove(u64 *spte) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 393 | { |
| 394 | struct page *page; |
| 395 | struct kvm_rmap_desc *desc; |
| 396 | struct kvm_rmap_desc *prev_desc; |
| 397 | int i; |
| 398 | |
| 399 | if (!is_rmap_pte(*spte)) |
| 400 | return; |
| 401 | page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT); |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 402 | if (!page_private(page)) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 403 | printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte); |
| 404 | BUG(); |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 405 | } else if (!(page_private(page) & 1)) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 406 | rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte); |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 407 | if ((u64 *)page_private(page) != spte) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 408 | printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n", |
| 409 | spte, *spte); |
| 410 | BUG(); |
| 411 | } |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 412 | set_page_private(page,0); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 413 | } else { |
| 414 | rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte); |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 415 | desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 416 | prev_desc = NULL; |
| 417 | while (desc) { |
| 418 | for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) |
| 419 | if (desc->shadow_ptes[i] == spte) { |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 420 | rmap_desc_remove_entry(page, |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 421 | desc, i, |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 422 | prev_desc); |
| 423 | return; |
| 424 | } |
| 425 | prev_desc = desc; |
| 426 | desc = desc->more; |
| 427 | } |
| 428 | BUG(); |
| 429 | } |
| 430 | } |
| 431 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 432 | static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn) |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 433 | { |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 434 | struct kvm *kvm = vcpu->kvm; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 435 | struct page *page; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 436 | struct kvm_rmap_desc *desc; |
| 437 | u64 *spte; |
| 438 | |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 439 | page = gfn_to_page(kvm, gfn); |
| 440 | BUG_ON(!page); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 441 | |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 442 | while (page_private(page)) { |
| 443 | if (!(page_private(page) & 1)) |
| 444 | spte = (u64 *)page_private(page); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 445 | else { |
Markus Rechberger | 5972e95 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 446 | desc = (struct kvm_rmap_desc *)(page_private(page) & ~1ul); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 447 | spte = desc->shadow_ptes[0]; |
| 448 | } |
| 449 | BUG_ON(!spte); |
Avi Kivity | 27aba76 | 2007-03-09 13:04:31 +0200 | [diff] [blame] | 450 | BUG_ON((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT |
| 451 | != page_to_pfn(page)); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 452 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
| 453 | BUG_ON(!(*spte & PT_WRITABLE_MASK)); |
| 454 | rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 455 | rmap_remove(spte); |
Avi Kivity | e663ee6 | 2007-05-31 15:46:04 +0300 | [diff] [blame] | 456 | set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK); |
Shaohua Li | 88a97f0 | 2007-06-20 17:13:26 +0800 | [diff] [blame] | 457 | kvm_flush_remote_tlbs(vcpu->kvm); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 461 | #ifdef MMU_DEBUG |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 462 | static int is_empty_shadow_page(u64 *spt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 463 | { |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 464 | u64 *pos; |
| 465 | u64 *end; |
| 466 | |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 467 | for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++) |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 468 | if (*pos != 0) { |
| 469 | printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__, |
| 470 | pos, *pos); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 471 | return 0; |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 472 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 473 | return 1; |
| 474 | } |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 475 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 476 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 477 | static void kvm_mmu_free_page(struct kvm *kvm, |
Avi Kivity | 4b02d6d | 2007-05-06 15:36:30 +0300 | [diff] [blame] | 478 | struct kvm_mmu_page *page_head) |
Avi Kivity | 260746c | 2007-01-05 16:36:49 -0800 | [diff] [blame] | 479 | { |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 480 | ASSERT(is_empty_shadow_page(page_head->spt)); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 481 | list_del(&page_head->link); |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame^] | 482 | __free_page(virt_to_page(page_head->spt)); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 483 | kfree(page_head); |
| 484 | ++kvm->n_free_mmu_pages; |
Avi Kivity | 260746c | 2007-01-05 16:36:49 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 487 | static unsigned kvm_page_table_hashfn(gfn_t gfn) |
| 488 | { |
| 489 | return gfn; |
| 490 | } |
| 491 | |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 492 | static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, |
| 493 | u64 *parent_pte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 494 | { |
| 495 | struct kvm_mmu_page *page; |
| 496 | |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 497 | if (!vcpu->kvm->n_free_mmu_pages) |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 498 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 499 | |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 500 | page = mmu_memory_cache_alloc(&vcpu->mmu_page_header_cache, |
| 501 | sizeof *page); |
| 502 | page->spt = mmu_memory_cache_alloc(&vcpu->mmu_page_cache, PAGE_SIZE); |
| 503 | set_page_private(virt_to_page(page->spt), (unsigned long)page); |
| 504 | list_add(&page->link, &vcpu->kvm->active_mmu_pages); |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 505 | ASSERT(is_empty_shadow_page(page->spt)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 506 | page->slot_bitmap = 0; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 507 | page->multimapped = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 508 | page->parent_pte = parent_pte; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 509 | --vcpu->kvm->n_free_mmu_pages; |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 510 | return page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 513 | static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu, |
| 514 | struct kvm_mmu_page *page, u64 *parent_pte) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 515 | { |
| 516 | struct kvm_pte_chain *pte_chain; |
| 517 | struct hlist_node *node; |
| 518 | int i; |
| 519 | |
| 520 | if (!parent_pte) |
| 521 | return; |
| 522 | if (!page->multimapped) { |
| 523 | u64 *old = page->parent_pte; |
| 524 | |
| 525 | if (!old) { |
| 526 | page->parent_pte = parent_pte; |
| 527 | return; |
| 528 | } |
| 529 | page->multimapped = 1; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 530 | pte_chain = mmu_alloc_pte_chain(vcpu); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 531 | INIT_HLIST_HEAD(&page->parent_ptes); |
| 532 | hlist_add_head(&pte_chain->link, &page->parent_ptes); |
| 533 | pte_chain->parent_ptes[0] = old; |
| 534 | } |
| 535 | hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) { |
| 536 | if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1]) |
| 537 | continue; |
| 538 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) |
| 539 | if (!pte_chain->parent_ptes[i]) { |
| 540 | pte_chain->parent_ptes[i] = parent_pte; |
| 541 | return; |
| 542 | } |
| 543 | } |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 544 | pte_chain = mmu_alloc_pte_chain(vcpu); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 545 | BUG_ON(!pte_chain); |
| 546 | hlist_add_head(&pte_chain->link, &page->parent_ptes); |
| 547 | pte_chain->parent_ptes[0] = parent_pte; |
| 548 | } |
| 549 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 550 | static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page, |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 551 | u64 *parent_pte) |
| 552 | { |
| 553 | struct kvm_pte_chain *pte_chain; |
| 554 | struct hlist_node *node; |
| 555 | int i; |
| 556 | |
| 557 | if (!page->multimapped) { |
| 558 | BUG_ON(page->parent_pte != parent_pte); |
| 559 | page->parent_pte = NULL; |
| 560 | return; |
| 561 | } |
| 562 | hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) |
| 563 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) { |
| 564 | if (!pte_chain->parent_ptes[i]) |
| 565 | break; |
| 566 | if (pte_chain->parent_ptes[i] != parent_pte) |
| 567 | continue; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 568 | while (i + 1 < NR_PTE_CHAIN_ENTRIES |
| 569 | && pte_chain->parent_ptes[i + 1]) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 570 | pte_chain->parent_ptes[i] |
| 571 | = pte_chain->parent_ptes[i + 1]; |
| 572 | ++i; |
| 573 | } |
| 574 | pte_chain->parent_ptes[i] = NULL; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 575 | if (i == 0) { |
| 576 | hlist_del(&pte_chain->link); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 577 | mmu_free_pte_chain(pte_chain); |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 578 | if (hlist_empty(&page->parent_ptes)) { |
| 579 | page->multimapped = 0; |
| 580 | page->parent_pte = NULL; |
| 581 | } |
| 582 | } |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 583 | return; |
| 584 | } |
| 585 | BUG(); |
| 586 | } |
| 587 | |
| 588 | static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu, |
| 589 | gfn_t gfn) |
| 590 | { |
| 591 | unsigned index; |
| 592 | struct hlist_head *bucket; |
| 593 | struct kvm_mmu_page *page; |
| 594 | struct hlist_node *node; |
| 595 | |
| 596 | pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn); |
| 597 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; |
| 598 | bucket = &vcpu->kvm->mmu_page_hash[index]; |
| 599 | hlist_for_each_entry(page, node, bucket, hash_link) |
| 600 | if (page->gfn == gfn && !page->role.metaphysical) { |
| 601 | pgprintk("%s: found role %x\n", |
| 602 | __FUNCTION__, page->role.word); |
| 603 | return page; |
| 604 | } |
| 605 | return NULL; |
| 606 | } |
| 607 | |
| 608 | static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, |
| 609 | gfn_t gfn, |
| 610 | gva_t gaddr, |
| 611 | unsigned level, |
| 612 | int metaphysical, |
Avi Kivity | d28c6cf | 2007-03-23 09:55:25 +0200 | [diff] [blame] | 613 | unsigned hugepage_access, |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 614 | u64 *parent_pte) |
| 615 | { |
| 616 | union kvm_mmu_page_role role; |
| 617 | unsigned index; |
| 618 | unsigned quadrant; |
| 619 | struct hlist_head *bucket; |
| 620 | struct kvm_mmu_page *page; |
| 621 | struct hlist_node *node; |
| 622 | |
| 623 | role.word = 0; |
| 624 | role.glevels = vcpu->mmu.root_level; |
| 625 | role.level = level; |
| 626 | role.metaphysical = metaphysical; |
Avi Kivity | d28c6cf | 2007-03-23 09:55:25 +0200 | [diff] [blame] | 627 | role.hugepage_access = hugepage_access; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 628 | if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) { |
| 629 | quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); |
| 630 | quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1; |
| 631 | role.quadrant = quadrant; |
| 632 | } |
| 633 | pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__, |
| 634 | gfn, role.word); |
| 635 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; |
| 636 | bucket = &vcpu->kvm->mmu_page_hash[index]; |
| 637 | hlist_for_each_entry(page, node, bucket, hash_link) |
| 638 | if (page->gfn == gfn && page->role.word == role.word) { |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 639 | mmu_page_add_parent_pte(vcpu, page, parent_pte); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 640 | pgprintk("%s: found\n", __FUNCTION__); |
| 641 | return page; |
| 642 | } |
| 643 | page = kvm_mmu_alloc_page(vcpu, parent_pte); |
| 644 | if (!page) |
| 645 | return page; |
| 646 | pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word); |
| 647 | page->gfn = gfn; |
| 648 | page->role = role; |
| 649 | hlist_add_head(&page->hash_link, bucket); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 650 | if (!metaphysical) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 651 | rmap_write_protect(vcpu, gfn); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 652 | return page; |
| 653 | } |
| 654 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 655 | static void kvm_mmu_page_unlink_children(struct kvm *kvm, |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 656 | struct kvm_mmu_page *page) |
| 657 | { |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 658 | unsigned i; |
| 659 | u64 *pt; |
| 660 | u64 ent; |
| 661 | |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 662 | pt = page->spt; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 663 | |
| 664 | if (page->role.level == PT_PAGE_TABLE_LEVEL) { |
| 665 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 666 | if (pt[i] & PT_PRESENT_MASK) |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 667 | rmap_remove(&pt[i]); |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 668 | pt[i] = 0; |
| 669 | } |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 670 | kvm_flush_remote_tlbs(kvm); |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 671 | return; |
| 672 | } |
| 673 | |
| 674 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 675 | ent = pt[i]; |
| 676 | |
| 677 | pt[i] = 0; |
| 678 | if (!(ent & PT_PRESENT_MASK)) |
| 679 | continue; |
| 680 | ent &= PT64_BASE_ADDR_MASK; |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 681 | mmu_page_remove_parent_pte(page_header(ent), &pt[i]); |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 682 | } |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 683 | kvm_flush_remote_tlbs(kvm); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 686 | static void kvm_mmu_put_page(struct kvm_mmu_page *page, |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 687 | u64 *parent_pte) |
| 688 | { |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 689 | mmu_page_remove_parent_pte(page, parent_pte); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 692 | static void kvm_mmu_zap_page(struct kvm *kvm, |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 693 | struct kvm_mmu_page *page) |
| 694 | { |
| 695 | u64 *parent_pte; |
| 696 | |
| 697 | while (page->multimapped || page->parent_pte) { |
| 698 | if (!page->multimapped) |
| 699 | parent_pte = page->parent_pte; |
| 700 | else { |
| 701 | struct kvm_pte_chain *chain; |
| 702 | |
| 703 | chain = container_of(page->parent_ptes.first, |
| 704 | struct kvm_pte_chain, link); |
| 705 | parent_pte = chain->parent_ptes[0]; |
| 706 | } |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 707 | BUG_ON(!parent_pte); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 708 | kvm_mmu_put_page(page, parent_pte); |
Avi Kivity | e663ee6 | 2007-05-31 15:46:04 +0300 | [diff] [blame] | 709 | set_shadow_pte(parent_pte, 0); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 710 | } |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 711 | kvm_mmu_page_unlink_children(kvm, page); |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 712 | if (!page->root_count) { |
| 713 | hlist_del(&page->hash_link); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 714 | kvm_mmu_free_page(kvm, page); |
Avi Kivity | 36868f7 | 2007-03-26 19:31:52 +0200 | [diff] [blame] | 715 | } else |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 716 | list_move(&page->link, &kvm->active_mmu_pages); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 720 | { |
| 721 | unsigned index; |
| 722 | struct hlist_head *bucket; |
| 723 | struct kvm_mmu_page *page; |
| 724 | struct hlist_node *node, *n; |
| 725 | int r; |
| 726 | |
| 727 | pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn); |
| 728 | r = 0; |
| 729 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; |
| 730 | bucket = &vcpu->kvm->mmu_page_hash[index]; |
| 731 | hlist_for_each_entry_safe(page, node, n, bucket, hash_link) |
| 732 | if (page->gfn == gfn && !page->role.metaphysical) { |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 733 | pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn, |
| 734 | page->role.word); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 735 | kvm_mmu_zap_page(vcpu->kvm, page); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 736 | r = 1; |
| 737 | } |
| 738 | return r; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 741 | static void mmu_unshadow(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 742 | { |
| 743 | struct kvm_mmu_page *page; |
| 744 | |
| 745 | while ((page = kvm_mmu_lookup_page(vcpu, gfn)) != NULL) { |
| 746 | pgprintk("%s: zap %lx %x\n", |
| 747 | __FUNCTION__, gfn, page->role.word); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 748 | kvm_mmu_zap_page(vcpu->kvm, page); |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 752 | static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa) |
| 753 | { |
| 754 | int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT)); |
| 755 | struct kvm_mmu_page *page_head = page_header(__pa(pte)); |
| 756 | |
| 757 | __set_bit(slot, &page_head->slot_bitmap); |
| 758 | } |
| 759 | |
| 760 | hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 761 | { |
| 762 | hpa_t hpa = gpa_to_hpa(vcpu, gpa); |
| 763 | |
| 764 | return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa; |
| 765 | } |
| 766 | |
| 767 | hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 768 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 769 | struct page *page; |
| 770 | |
| 771 | ASSERT((gpa & HPA_ERR_MASK) == 0); |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 772 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
| 773 | if (!page) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 774 | return gpa | HPA_ERR_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 775 | return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT) |
| 776 | | (gpa & (PAGE_SIZE-1)); |
| 777 | } |
| 778 | |
| 779 | hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva) |
| 780 | { |
| 781 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva); |
| 782 | |
| 783 | if (gpa == UNMAPPED_GVA) |
| 784 | return UNMAPPED_GVA; |
| 785 | return gpa_to_hpa(vcpu, gpa); |
| 786 | } |
| 787 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 788 | struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva) |
| 789 | { |
| 790 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva); |
| 791 | |
| 792 | if (gpa == UNMAPPED_GVA) |
| 793 | return NULL; |
| 794 | return pfn_to_page(gpa_to_hpa(vcpu, gpa) >> PAGE_SHIFT); |
| 795 | } |
| 796 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 797 | static void nonpaging_new_cr3(struct kvm_vcpu *vcpu) |
| 798 | { |
| 799 | } |
| 800 | |
| 801 | static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p) |
| 802 | { |
| 803 | int level = PT32E_ROOT_LEVEL; |
| 804 | hpa_t table_addr = vcpu->mmu.root_hpa; |
| 805 | |
| 806 | for (; ; level--) { |
| 807 | u32 index = PT64_INDEX(v, level); |
| 808 | u64 *table; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 809 | u64 pte; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 810 | |
| 811 | ASSERT(VALID_PAGE(table_addr)); |
| 812 | table = __va(table_addr); |
| 813 | |
| 814 | if (level == 1) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 815 | pte = table[index]; |
| 816 | if (is_present_pte(pte) && is_writeble_pte(pte)) |
| 817 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 818 | mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT); |
| 819 | page_header_update_slot(vcpu->kvm, table, v); |
| 820 | table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK | |
| 821 | PT_USER_MASK; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 822 | rmap_add(vcpu, &table[index]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | if (table[index] == 0) { |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 827 | struct kvm_mmu_page *new_table; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 828 | gfn_t pseudo_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 829 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 830 | pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK) |
| 831 | >> PAGE_SHIFT; |
| 832 | new_table = kvm_mmu_get_page(vcpu, pseudo_gfn, |
| 833 | v, level - 1, |
Avi Kivity | d28c6cf | 2007-03-23 09:55:25 +0200 | [diff] [blame] | 834 | 1, 0, &table[index]); |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 835 | if (!new_table) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 836 | pgprintk("nonpaging_map: ENOMEM\n"); |
| 837 | return -ENOMEM; |
| 838 | } |
| 839 | |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 840 | table[index] = __pa(new_table->spt) | PT_PRESENT_MASK |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 841 | | PT_WRITABLE_MASK | PT_USER_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 842 | } |
| 843 | table_addr = table[index] & PT64_BASE_ADDR_MASK; |
| 844 | } |
| 845 | } |
| 846 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 847 | static void mmu_free_roots(struct kvm_vcpu *vcpu) |
| 848 | { |
| 849 | int i; |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 850 | struct kvm_mmu_page *page; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 851 | |
Avi Kivity | 7b53aa5 | 2007-06-05 12:17:03 +0300 | [diff] [blame] | 852 | if (!VALID_PAGE(vcpu->mmu.root_hpa)) |
| 853 | return; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 854 | #ifdef CONFIG_X86_64 |
| 855 | if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 856 | hpa_t root = vcpu->mmu.root_hpa; |
| 857 | |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 858 | page = page_header(root); |
| 859 | --page->root_count; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 860 | vcpu->mmu.root_hpa = INVALID_PAGE; |
| 861 | return; |
| 862 | } |
| 863 | #endif |
| 864 | for (i = 0; i < 4; ++i) { |
| 865 | hpa_t root = vcpu->mmu.pae_root[i]; |
| 866 | |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 867 | if (root) { |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 868 | root &= PT64_BASE_ADDR_MASK; |
| 869 | page = page_header(root); |
| 870 | --page->root_count; |
| 871 | } |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 872 | vcpu->mmu.pae_root[i] = INVALID_PAGE; |
| 873 | } |
| 874 | vcpu->mmu.root_hpa = INVALID_PAGE; |
| 875 | } |
| 876 | |
| 877 | static void mmu_alloc_roots(struct kvm_vcpu *vcpu) |
| 878 | { |
| 879 | int i; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 880 | gfn_t root_gfn; |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 881 | struct kvm_mmu_page *page; |
| 882 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 883 | root_gfn = vcpu->cr3 >> PAGE_SHIFT; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 884 | |
| 885 | #ifdef CONFIG_X86_64 |
| 886 | if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 887 | hpa_t root = vcpu->mmu.root_hpa; |
| 888 | |
| 889 | ASSERT(!VALID_PAGE(root)); |
Ingo Molnar | 68a99f6 | 2007-01-05 16:36:59 -0800 | [diff] [blame] | 890 | page = kvm_mmu_get_page(vcpu, root_gfn, 0, |
Avi Kivity | d28c6cf | 2007-03-23 09:55:25 +0200 | [diff] [blame] | 891 | PT64_ROOT_LEVEL, 0, 0, NULL); |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 892 | root = __pa(page->spt); |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 893 | ++page->root_count; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 894 | vcpu->mmu.root_hpa = root; |
| 895 | return; |
| 896 | } |
| 897 | #endif |
| 898 | for (i = 0; i < 4; ++i) { |
| 899 | hpa_t root = vcpu->mmu.pae_root[i]; |
| 900 | |
| 901 | ASSERT(!VALID_PAGE(root)); |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 902 | if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) { |
| 903 | if (!is_present_pte(vcpu->pdptrs[i])) { |
| 904 | vcpu->mmu.pae_root[i] = 0; |
| 905 | continue; |
| 906 | } |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 907 | root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT; |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 908 | } else if (vcpu->mmu.root_level == 0) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 909 | root_gfn = 0; |
Ingo Molnar | 68a99f6 | 2007-01-05 16:36:59 -0800 | [diff] [blame] | 910 | page = kvm_mmu_get_page(vcpu, root_gfn, i << 30, |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 911 | PT32_ROOT_LEVEL, !is_paging(vcpu), |
Avi Kivity | d28c6cf | 2007-03-23 09:55:25 +0200 | [diff] [blame] | 912 | 0, NULL); |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 913 | root = __pa(page->spt); |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 914 | ++page->root_count; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 915 | vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK; |
| 916 | } |
| 917 | vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root); |
| 918 | } |
| 919 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 920 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr) |
| 921 | { |
| 922 | return vaddr; |
| 923 | } |
| 924 | |
| 925 | static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, |
| 926 | u32 error_code) |
| 927 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 928 | gpa_t addr = gva; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 929 | hpa_t paddr; |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 930 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 931 | |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 932 | r = mmu_topup_memory_caches(vcpu); |
| 933 | if (r) |
| 934 | return r; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 935 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 936 | ASSERT(vcpu); |
| 937 | ASSERT(VALID_PAGE(vcpu->mmu.root_hpa)); |
| 938 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 939 | |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 940 | paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 941 | |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 942 | if (is_error_hpa(paddr)) |
| 943 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 944 | |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 945 | return nonpaging_map(vcpu, addr & PAGE_MASK, paddr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 948 | static void nonpaging_free(struct kvm_vcpu *vcpu) |
| 949 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 950 | mmu_free_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | static int nonpaging_init_context(struct kvm_vcpu *vcpu) |
| 954 | { |
| 955 | struct kvm_mmu *context = &vcpu->mmu; |
| 956 | |
| 957 | context->new_cr3 = nonpaging_new_cr3; |
| 958 | context->page_fault = nonpaging_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 959 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
| 960 | context->free = nonpaging_free; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 961 | context->root_level = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 962 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 963 | context->root_hpa = INVALID_PAGE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 964 | return 0; |
| 965 | } |
| 966 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 967 | static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
| 968 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 969 | ++vcpu->stat.tlb_flush; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 970 | kvm_arch_ops->tlb_flush(vcpu); |
| 971 | } |
| 972 | |
| 973 | static void paging_new_cr3(struct kvm_vcpu *vcpu) |
| 974 | { |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 975 | pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 976 | mmu_free_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 977 | } |
| 978 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 979 | static void inject_page_fault(struct kvm_vcpu *vcpu, |
| 980 | u64 addr, |
| 981 | u32 err_code) |
| 982 | { |
| 983 | kvm_arch_ops->inject_page_fault(vcpu, addr, err_code); |
| 984 | } |
| 985 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 986 | static void paging_free(struct kvm_vcpu *vcpu) |
| 987 | { |
| 988 | nonpaging_free(vcpu); |
| 989 | } |
| 990 | |
| 991 | #define PTTYPE 64 |
| 992 | #include "paging_tmpl.h" |
| 993 | #undef PTTYPE |
| 994 | |
| 995 | #define PTTYPE 32 |
| 996 | #include "paging_tmpl.h" |
| 997 | #undef PTTYPE |
| 998 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 999 | static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1000 | { |
| 1001 | struct kvm_mmu *context = &vcpu->mmu; |
| 1002 | |
| 1003 | ASSERT(is_pae(vcpu)); |
| 1004 | context->new_cr3 = paging_new_cr3; |
| 1005 | context->page_fault = paging64_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1006 | context->gva_to_gpa = paging64_gva_to_gpa; |
| 1007 | context->free = paging_free; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1008 | context->root_level = level; |
| 1009 | context->shadow_root_level = level; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1010 | context->root_hpa = INVALID_PAGE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1011 | return 0; |
| 1012 | } |
| 1013 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1014 | static int paging64_init_context(struct kvm_vcpu *vcpu) |
| 1015 | { |
| 1016 | return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL); |
| 1017 | } |
| 1018 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1019 | static int paging32_init_context(struct kvm_vcpu *vcpu) |
| 1020 | { |
| 1021 | struct kvm_mmu *context = &vcpu->mmu; |
| 1022 | |
| 1023 | context->new_cr3 = paging_new_cr3; |
| 1024 | context->page_fault = paging32_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1025 | context->gva_to_gpa = paging32_gva_to_gpa; |
| 1026 | context->free = paging_free; |
| 1027 | context->root_level = PT32_ROOT_LEVEL; |
| 1028 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1029 | context->root_hpa = INVALID_PAGE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | static int paging32E_init_context(struct kvm_vcpu *vcpu) |
| 1034 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1035 | return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | static int init_kvm_mmu(struct kvm_vcpu *vcpu) |
| 1039 | { |
| 1040 | ASSERT(vcpu); |
| 1041 | ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa)); |
| 1042 | |
| 1043 | if (!is_paging(vcpu)) |
| 1044 | return nonpaging_init_context(vcpu); |
Avi Kivity | a9058ec | 2006-12-29 16:49:37 -0800 | [diff] [blame] | 1045 | else if (is_long_mode(vcpu)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1046 | return paging64_init_context(vcpu); |
| 1047 | else if (is_pae(vcpu)) |
| 1048 | return paging32E_init_context(vcpu); |
| 1049 | else |
| 1050 | return paging32_init_context(vcpu); |
| 1051 | } |
| 1052 | |
| 1053 | static void destroy_kvm_mmu(struct kvm_vcpu *vcpu) |
| 1054 | { |
| 1055 | ASSERT(vcpu); |
| 1056 | if (VALID_PAGE(vcpu->mmu.root_hpa)) { |
| 1057 | vcpu->mmu.free(vcpu); |
| 1058 | vcpu->mmu.root_hpa = INVALID_PAGE; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | int kvm_mmu_reset_context(struct kvm_vcpu *vcpu) |
| 1063 | { |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1064 | destroy_kvm_mmu(vcpu); |
| 1065 | return init_kvm_mmu(vcpu); |
| 1066 | } |
| 1067 | |
| 1068 | int kvm_mmu_load(struct kvm_vcpu *vcpu) |
| 1069 | { |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 1070 | int r; |
| 1071 | |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1072 | spin_lock(&vcpu->kvm->lock); |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 1073 | r = mmu_topup_memory_caches(vcpu); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1074 | if (r) |
| 1075 | goto out; |
| 1076 | mmu_alloc_roots(vcpu); |
| 1077 | kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa); |
| 1078 | kvm_mmu_flush_tlb(vcpu); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 1079 | out: |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1080 | spin_unlock(&vcpu->kvm->lock); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 1081 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1082 | } |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1083 | EXPORT_SYMBOL_GPL(kvm_mmu_load); |
| 1084 | |
| 1085 | void kvm_mmu_unload(struct kvm_vcpu *vcpu) |
| 1086 | { |
| 1087 | mmu_free_roots(vcpu); |
| 1088 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1089 | |
Avi Kivity | 09072da | 2007-05-01 14:16:52 +0300 | [diff] [blame] | 1090 | static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu, |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1091 | struct kvm_mmu_page *page, |
| 1092 | u64 *spte) |
| 1093 | { |
| 1094 | u64 pte; |
| 1095 | struct kvm_mmu_page *child; |
| 1096 | |
| 1097 | pte = *spte; |
| 1098 | if (is_present_pte(pte)) { |
| 1099 | if (page->role.level == PT_PAGE_TABLE_LEVEL) |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1100 | rmap_remove(spte); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1101 | else { |
| 1102 | child = page_header(pte & PT64_BASE_ADDR_MASK); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1103 | mmu_page_remove_parent_pte(child, spte); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | *spte = 0; |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 1107 | kvm_flush_remote_tlbs(vcpu->kvm); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 1110 | static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu, |
| 1111 | struct kvm_mmu_page *page, |
| 1112 | u64 *spte, |
| 1113 | const void *new, int bytes) |
| 1114 | { |
| 1115 | if (page->role.level != PT_PAGE_TABLE_LEVEL) |
| 1116 | return; |
| 1117 | |
| 1118 | if (page->role.glevels == PT32_ROOT_LEVEL) |
| 1119 | paging32_update_pte(vcpu, page, spte, new, bytes); |
| 1120 | else |
| 1121 | paging64_update_pte(vcpu, page, spte, new, bytes); |
| 1122 | } |
| 1123 | |
Avi Kivity | 09072da | 2007-05-01 14:16:52 +0300 | [diff] [blame] | 1124 | void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 1125 | const u8 *old, const u8 *new, int bytes) |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1126 | { |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1127 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 1128 | struct kvm_mmu_page *page; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1129 | struct hlist_node *node, *n; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1130 | struct hlist_head *bucket; |
| 1131 | unsigned index; |
| 1132 | u64 *spte; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1133 | unsigned offset = offset_in_page(gpa); |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1134 | unsigned pte_size; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1135 | unsigned page_offset; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1136 | unsigned misaligned; |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 1137 | unsigned quadrant; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1138 | int level; |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1139 | int flooded = 0; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1140 | int npte; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1141 | |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1142 | pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes); |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1143 | if (gfn == vcpu->last_pt_write_gfn) { |
| 1144 | ++vcpu->last_pt_write_count; |
| 1145 | if (vcpu->last_pt_write_count >= 3) |
| 1146 | flooded = 1; |
| 1147 | } else { |
| 1148 | vcpu->last_pt_write_gfn = gfn; |
| 1149 | vcpu->last_pt_write_count = 1; |
| 1150 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1151 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; |
| 1152 | bucket = &vcpu->kvm->mmu_page_hash[index]; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1153 | hlist_for_each_entry_safe(page, node, n, bucket, hash_link) { |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1154 | if (page->gfn != gfn || page->role.metaphysical) |
| 1155 | continue; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1156 | pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8; |
| 1157 | misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); |
Avi Kivity | e925c5b | 2007-04-30 14:47:02 +0300 | [diff] [blame] | 1158 | misaligned |= bytes < 4; |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1159 | if (misaligned || flooded) { |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1160 | /* |
| 1161 | * Misaligned accesses are too much trouble to fix |
| 1162 | * up; also, they usually indicate a page is not used |
| 1163 | * as a page table. |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1164 | * |
| 1165 | * If we're seeing too many writes to a page, |
| 1166 | * it may no longer be a page table, or we may be |
| 1167 | * forking, in which case it is better to unmap the |
| 1168 | * page. |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1169 | */ |
| 1170 | pgprintk("misaligned: gpa %llx bytes %d role %x\n", |
| 1171 | gpa, bytes, page->role.word); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1172 | kvm_mmu_zap_page(vcpu->kvm, page); |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1173 | continue; |
| 1174 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1175 | page_offset = offset; |
| 1176 | level = page->role.level; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1177 | npte = 1; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1178 | if (page->role.glevels == PT32_ROOT_LEVEL) { |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1179 | page_offset <<= 1; /* 32->64 */ |
| 1180 | /* |
| 1181 | * A 32-bit pde maps 4MB while the shadow pdes map |
| 1182 | * only 2MB. So we need to double the offset again |
| 1183 | * and zap two pdes instead of one. |
| 1184 | */ |
| 1185 | if (level == PT32_ROOT_LEVEL) { |
Avi Kivity | 6b8d0f9 | 2007-04-18 11:18:18 +0300 | [diff] [blame] | 1186 | page_offset &= ~7; /* kill rounding error */ |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1187 | page_offset <<= 1; |
| 1188 | npte = 2; |
| 1189 | } |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 1190 | quadrant = page_offset >> PAGE_SHIFT; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1191 | page_offset &= ~PAGE_MASK; |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 1192 | if (quadrant != page->role.quadrant) |
| 1193 | continue; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1194 | } |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 1195 | spte = &page->spt[page_offset / sizeof(*spte)]; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1196 | while (npte--) { |
Avi Kivity | 09072da | 2007-05-01 14:16:52 +0300 | [diff] [blame] | 1197 | mmu_pte_write_zap_pte(vcpu, page, spte); |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 1198 | mmu_pte_write_new_pte(vcpu, page, spte, new, bytes); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1199 | ++spte; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1200 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1201 | } |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1202 | } |
| 1203 | |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1204 | int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva) |
| 1205 | { |
| 1206 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva); |
| 1207 | |
| 1208 | return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT); |
| 1209 | } |
| 1210 | |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 1211 | void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu) |
| 1212 | { |
| 1213 | while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) { |
| 1214 | struct kvm_mmu_page *page; |
| 1215 | |
| 1216 | page = container_of(vcpu->kvm->active_mmu_pages.prev, |
| 1217 | struct kvm_mmu_page, link); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1218 | kvm_mmu_zap_page(vcpu->kvm, page); |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages); |
| 1222 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1223 | static void free_mmu_pages(struct kvm_vcpu *vcpu) |
| 1224 | { |
Avi Kivity | f51234c | 2007-01-05 16:36:52 -0800 | [diff] [blame] | 1225 | struct kvm_mmu_page *page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1226 | |
Avi Kivity | f51234c | 2007-01-05 16:36:52 -0800 | [diff] [blame] | 1227 | while (!list_empty(&vcpu->kvm->active_mmu_pages)) { |
| 1228 | page = container_of(vcpu->kvm->active_mmu_pages.next, |
| 1229 | struct kvm_mmu_page, link); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1230 | kvm_mmu_zap_page(vcpu->kvm, page); |
Avi Kivity | f51234c | 2007-01-05 16:36:52 -0800 | [diff] [blame] | 1231 | } |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1232 | free_page((unsigned long)vcpu->mmu.pae_root); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | static int alloc_mmu_pages(struct kvm_vcpu *vcpu) |
| 1236 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1237 | struct page *page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1238 | int i; |
| 1239 | |
| 1240 | ASSERT(vcpu); |
| 1241 | |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 1242 | vcpu->kvm->n_free_mmu_pages = KVM_NUM_MMU_PAGES; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1243 | |
| 1244 | /* |
| 1245 | * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64. |
| 1246 | * Therefore we need to allocate shadow page tables in the first |
| 1247 | * 4GB of memory, which happens to fit the DMA32 zone. |
| 1248 | */ |
| 1249 | page = alloc_page(GFP_KERNEL | __GFP_DMA32); |
| 1250 | if (!page) |
| 1251 | goto error_1; |
| 1252 | vcpu->mmu.pae_root = page_address(page); |
| 1253 | for (i = 0; i < 4; ++i) |
| 1254 | vcpu->mmu.pae_root[i] = INVALID_PAGE; |
| 1255 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1256 | return 0; |
| 1257 | |
| 1258 | error_1: |
| 1259 | free_mmu_pages(vcpu); |
| 1260 | return -ENOMEM; |
| 1261 | } |
| 1262 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1263 | int kvm_mmu_create(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1264 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1265 | ASSERT(vcpu); |
| 1266 | ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1267 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1268 | return alloc_mmu_pages(vcpu); |
| 1269 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1270 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1271 | int kvm_mmu_setup(struct kvm_vcpu *vcpu) |
| 1272 | { |
| 1273 | ASSERT(vcpu); |
| 1274 | ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa)); |
Avi Kivity | 2c26495 | 2006-12-22 01:05:28 -0800 | [diff] [blame] | 1275 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1276 | return init_kvm_mmu(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | void kvm_mmu_destroy(struct kvm_vcpu *vcpu) |
| 1280 | { |
| 1281 | ASSERT(vcpu); |
| 1282 | |
| 1283 | destroy_kvm_mmu(vcpu); |
| 1284 | free_mmu_pages(vcpu); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 1285 | mmu_free_memory_caches(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1288 | void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1289 | { |
| 1290 | struct kvm_mmu_page *page; |
| 1291 | |
| 1292 | list_for_each_entry(page, &kvm->active_mmu_pages, link) { |
| 1293 | int i; |
| 1294 | u64 *pt; |
| 1295 | |
| 1296 | if (!test_bit(slot, &page->slot_bitmap)) |
| 1297 | continue; |
| 1298 | |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 1299 | pt = page->spt; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1300 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
| 1301 | /* avoid RMW */ |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1302 | if (pt[i] & PT_WRITABLE_MASK) { |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1303 | rmap_remove(&pt[i]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1304 | pt[i] &= ~PT_WRITABLE_MASK; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1305 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1306 | } |
| 1307 | } |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1308 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1309 | void kvm_mmu_zap_all(struct kvm *kvm) |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 1310 | { |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1311 | struct kvm_mmu_page *page, *node; |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 1312 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1313 | list_for_each_entry_safe(page, node, &kvm->active_mmu_pages, link) |
| 1314 | kvm_mmu_zap_page(kvm, page); |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 1315 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1316 | kvm_flush_remote_tlbs(kvm); |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 1317 | } |
| 1318 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 1319 | void kvm_mmu_module_exit(void) |
| 1320 | { |
| 1321 | if (pte_chain_cache) |
| 1322 | kmem_cache_destroy(pte_chain_cache); |
| 1323 | if (rmap_desc_cache) |
| 1324 | kmem_cache_destroy(rmap_desc_cache); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 1325 | if (mmu_page_header_cache) |
| 1326 | kmem_cache_destroy(mmu_page_header_cache); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | int kvm_mmu_module_init(void) |
| 1330 | { |
| 1331 | pte_chain_cache = kmem_cache_create("kvm_pte_chain", |
| 1332 | sizeof(struct kvm_pte_chain), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1333 | 0, 0, NULL); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 1334 | if (!pte_chain_cache) |
| 1335 | goto nomem; |
| 1336 | rmap_desc_cache = kmem_cache_create("kvm_rmap_desc", |
| 1337 | sizeof(struct kvm_rmap_desc), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1338 | 0, 0, NULL); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 1339 | if (!rmap_desc_cache) |
| 1340 | goto nomem; |
| 1341 | |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 1342 | mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header", |
| 1343 | sizeof(struct kvm_mmu_page), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1344 | 0, 0, NULL); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 1345 | if (!mmu_page_header_cache) |
| 1346 | goto nomem; |
| 1347 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 1348 | return 0; |
| 1349 | |
| 1350 | nomem: |
| 1351 | kvm_mmu_module_exit(); |
| 1352 | return -ENOMEM; |
| 1353 | } |
| 1354 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1355 | #ifdef AUDIT |
| 1356 | |
| 1357 | static const char *audit_msg; |
| 1358 | |
| 1359 | static gva_t canonicalize(gva_t gva) |
| 1360 | { |
| 1361 | #ifdef CONFIG_X86_64 |
| 1362 | gva = (long long)(gva << 16) >> 16; |
| 1363 | #endif |
| 1364 | return gva; |
| 1365 | } |
| 1366 | |
| 1367 | static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte, |
| 1368 | gva_t va, int level) |
| 1369 | { |
| 1370 | u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK); |
| 1371 | int i; |
| 1372 | gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1)); |
| 1373 | |
| 1374 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) { |
| 1375 | u64 ent = pt[i]; |
| 1376 | |
Adrian Bunk | 2807696 | 2007-04-28 21:20:48 +0200 | [diff] [blame] | 1377 | if (!(ent & PT_PRESENT_MASK)) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1378 | continue; |
| 1379 | |
| 1380 | va = canonicalize(va); |
| 1381 | if (level > 1) |
| 1382 | audit_mappings_page(vcpu, ent, va, level - 1); |
| 1383 | else { |
| 1384 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, va); |
| 1385 | hpa_t hpa = gpa_to_hpa(vcpu, gpa); |
| 1386 | |
| 1387 | if ((ent & PT_PRESENT_MASK) |
| 1388 | && (ent & PT64_BASE_ADDR_MASK) != hpa) |
| 1389 | printk(KERN_ERR "audit error: (%s) levels %d" |
| 1390 | " gva %lx gpa %llx hpa %llx ent %llx\n", |
| 1391 | audit_msg, vcpu->mmu.root_level, |
| 1392 | va, gpa, hpa, ent); |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | static void audit_mappings(struct kvm_vcpu *vcpu) |
| 1398 | { |
Avi Kivity | 1ea252a | 2007-03-08 11:48:09 +0200 | [diff] [blame] | 1399 | unsigned i; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1400 | |
| 1401 | if (vcpu->mmu.root_level == 4) |
| 1402 | audit_mappings_page(vcpu, vcpu->mmu.root_hpa, 0, 4); |
| 1403 | else |
| 1404 | for (i = 0; i < 4; ++i) |
| 1405 | if (vcpu->mmu.pae_root[i] & PT_PRESENT_MASK) |
| 1406 | audit_mappings_page(vcpu, |
| 1407 | vcpu->mmu.pae_root[i], |
| 1408 | i << 30, |
| 1409 | 2); |
| 1410 | } |
| 1411 | |
| 1412 | static int count_rmaps(struct kvm_vcpu *vcpu) |
| 1413 | { |
| 1414 | int nmaps = 0; |
| 1415 | int i, j, k; |
| 1416 | |
| 1417 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 1418 | struct kvm_memory_slot *m = &vcpu->kvm->memslots[i]; |
| 1419 | struct kvm_rmap_desc *d; |
| 1420 | |
| 1421 | for (j = 0; j < m->npages; ++j) { |
| 1422 | struct page *page = m->phys_mem[j]; |
| 1423 | |
| 1424 | if (!page->private) |
| 1425 | continue; |
| 1426 | if (!(page->private & 1)) { |
| 1427 | ++nmaps; |
| 1428 | continue; |
| 1429 | } |
| 1430 | d = (struct kvm_rmap_desc *)(page->private & ~1ul); |
| 1431 | while (d) { |
| 1432 | for (k = 0; k < RMAP_EXT; ++k) |
| 1433 | if (d->shadow_ptes[k]) |
| 1434 | ++nmaps; |
| 1435 | else |
| 1436 | break; |
| 1437 | d = d->more; |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | return nmaps; |
| 1442 | } |
| 1443 | |
| 1444 | static int count_writable_mappings(struct kvm_vcpu *vcpu) |
| 1445 | { |
| 1446 | int nmaps = 0; |
| 1447 | struct kvm_mmu_page *page; |
| 1448 | int i; |
| 1449 | |
| 1450 | list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) { |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 1451 | u64 *pt = page->spt; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1452 | |
| 1453 | if (page->role.level != PT_PAGE_TABLE_LEVEL) |
| 1454 | continue; |
| 1455 | |
| 1456 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 1457 | u64 ent = pt[i]; |
| 1458 | |
| 1459 | if (!(ent & PT_PRESENT_MASK)) |
| 1460 | continue; |
| 1461 | if (!(ent & PT_WRITABLE_MASK)) |
| 1462 | continue; |
| 1463 | ++nmaps; |
| 1464 | } |
| 1465 | } |
| 1466 | return nmaps; |
| 1467 | } |
| 1468 | |
| 1469 | static void audit_rmap(struct kvm_vcpu *vcpu) |
| 1470 | { |
| 1471 | int n_rmap = count_rmaps(vcpu); |
| 1472 | int n_actual = count_writable_mappings(vcpu); |
| 1473 | |
| 1474 | if (n_rmap != n_actual) |
| 1475 | printk(KERN_ERR "%s: (%s) rmap %d actual %d\n", |
| 1476 | __FUNCTION__, audit_msg, n_rmap, n_actual); |
| 1477 | } |
| 1478 | |
| 1479 | static void audit_write_protection(struct kvm_vcpu *vcpu) |
| 1480 | { |
| 1481 | struct kvm_mmu_page *page; |
| 1482 | |
| 1483 | list_for_each_entry(page, &vcpu->kvm->active_mmu_pages, link) { |
| 1484 | hfn_t hfn; |
| 1485 | struct page *pg; |
| 1486 | |
| 1487 | if (page->role.metaphysical) |
| 1488 | continue; |
| 1489 | |
| 1490 | hfn = gpa_to_hpa(vcpu, (gpa_t)page->gfn << PAGE_SHIFT) |
| 1491 | >> PAGE_SHIFT; |
| 1492 | pg = pfn_to_page(hfn); |
| 1493 | if (pg->private) |
| 1494 | printk(KERN_ERR "%s: (%s) shadow page has writable" |
| 1495 | " mappings: gfn %lx role %x\n", |
| 1496 | __FUNCTION__, audit_msg, page->gfn, |
| 1497 | page->role.word); |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) |
| 1502 | { |
| 1503 | int olddbg = dbg; |
| 1504 | |
| 1505 | dbg = 0; |
| 1506 | audit_msg = msg; |
| 1507 | audit_rmap(vcpu); |
| 1508 | audit_write_protection(vcpu); |
| 1509 | audit_mappings(vcpu); |
| 1510 | dbg = olddbg; |
| 1511 | } |
| 1512 | |
| 1513 | #endif |