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