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