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