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