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