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