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