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