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