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