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