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