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