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