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