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