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