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