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