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