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