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