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 | |
| 20 | #include "vmx.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 21 | #include "mmu.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> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 36 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 37 | /* |
| 38 | * When setting this variable to true it enables Two-Dimensional-Paging |
| 39 | * where the hardware walks 2 page tables: |
| 40 | * 1. the guest-virtual to guest-physical |
| 41 | * 2. while doing 1. it walks guest-physical to host-physical |
| 42 | * If the hardware supports that we don't need to do shadow paging. |
| 43 | */ |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 44 | bool tdp_enabled = false; |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 45 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 46 | #undef MMU_DEBUG |
| 47 | |
| 48 | #undef AUDIT |
| 49 | |
| 50 | #ifdef AUDIT |
| 51 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg); |
| 52 | #else |
| 53 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {} |
| 54 | #endif |
| 55 | |
| 56 | #ifdef MMU_DEBUG |
| 57 | |
| 58 | #define pgprintk(x...) do { if (dbg) printk(x); } while (0) |
| 59 | #define rmap_printk(x...) do { if (dbg) printk(x); } while (0) |
| 60 | |
| 61 | #else |
| 62 | |
| 63 | #define pgprintk(x...) do { } while (0) |
| 64 | #define rmap_printk(x...) do { } while (0) |
| 65 | |
| 66 | #endif |
| 67 | |
| 68 | #if defined(MMU_DEBUG) || defined(AUDIT) |
Avi Kivity | 6ada8cc | 2008-06-22 16:45:24 +0300 | [diff] [blame] | 69 | static int dbg = 0; |
| 70 | module_param(dbg, bool, 0644); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 71 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 72 | |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 73 | #ifndef MMU_DEBUG |
| 74 | #define ASSERT(x) do { } while (0) |
| 75 | #else |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 76 | #define ASSERT(x) \ |
| 77 | if (!(x)) { \ |
| 78 | printk(KERN_WARNING "assertion failed %s:%d: %s\n", \ |
| 79 | __FILE__, __LINE__, #x); \ |
| 80 | } |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 81 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 82 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 83 | #define PT_FIRST_AVAIL_BITS_SHIFT 9 |
| 84 | #define PT64_SECOND_AVAIL_BITS_SHIFT 52 |
| 85 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 86 | #define VALID_PAGE(x) ((x) != INVALID_PAGE) |
| 87 | |
| 88 | #define PT64_LEVEL_BITS 9 |
| 89 | |
| 90 | #define PT64_LEVEL_SHIFT(level) \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 91 | (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 92 | |
| 93 | #define PT64_LEVEL_MASK(level) \ |
| 94 | (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level)) |
| 95 | |
| 96 | #define PT64_INDEX(address, level)\ |
| 97 | (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1)) |
| 98 | |
| 99 | |
| 100 | #define PT32_LEVEL_BITS 10 |
| 101 | |
| 102 | #define PT32_LEVEL_SHIFT(level) \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 103 | (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 104 | |
| 105 | #define PT32_LEVEL_MASK(level) \ |
| 106 | (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level)) |
| 107 | |
| 108 | #define PT32_INDEX(address, level)\ |
| 109 | (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1)) |
| 110 | |
| 111 | |
Avi Kivity | 27aba76 | 2007-03-09 13:04:31 +0200 | [diff] [blame] | 112 | #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 113 | #define PT64_DIR_BASE_ADDR_MASK \ |
| 114 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1)) |
| 115 | |
| 116 | #define PT32_BASE_ADDR_MASK PAGE_MASK |
| 117 | #define PT32_DIR_BASE_ADDR_MASK \ |
| 118 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1)) |
| 119 | |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 120 | #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \ |
| 121 | | PT64_NX_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 122 | |
| 123 | #define PFERR_PRESENT_MASK (1U << 0) |
| 124 | #define PFERR_WRITE_MASK (1U << 1) |
| 125 | #define PFERR_USER_MASK (1U << 2) |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 126 | #define PFERR_FETCH_MASK (1U << 4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 127 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 128 | #define PT_DIRECTORY_LEVEL 2 |
| 129 | #define PT_PAGE_TABLE_LEVEL 1 |
| 130 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 131 | #define RMAP_EXT 4 |
| 132 | |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 133 | #define ACC_EXEC_MASK 1 |
| 134 | #define ACC_WRITE_MASK PT_WRITABLE_MASK |
| 135 | #define ACC_USER_MASK PT_USER_MASK |
| 136 | #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK) |
| 137 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 138 | struct kvm_pv_mmu_op_buffer { |
| 139 | void *ptr; |
| 140 | unsigned len; |
| 141 | unsigned processed; |
| 142 | char buf[512] __aligned(sizeof(long)); |
| 143 | }; |
| 144 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 145 | struct kvm_rmap_desc { |
| 146 | u64 *shadow_ptes[RMAP_EXT]; |
| 147 | struct kvm_rmap_desc *more; |
| 148 | }; |
| 149 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 150 | static struct kmem_cache *pte_chain_cache; |
| 151 | static struct kmem_cache *rmap_desc_cache; |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 152 | static struct kmem_cache *mmu_page_header_cache; |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 153 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 154 | static u64 __read_mostly shadow_trap_nonpresent_pte; |
| 155 | static u64 __read_mostly shadow_notrap_nonpresent_pte; |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 156 | static u64 __read_mostly shadow_base_present_pte; |
| 157 | static u64 __read_mostly shadow_nx_mask; |
| 158 | static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */ |
| 159 | static u64 __read_mostly shadow_user_mask; |
| 160 | static u64 __read_mostly shadow_accessed_mask; |
| 161 | static u64 __read_mostly shadow_dirty_mask; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 162 | |
| 163 | void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte) |
| 164 | { |
| 165 | shadow_trap_nonpresent_pte = trap_pte; |
| 166 | shadow_notrap_nonpresent_pte = notrap_pte; |
| 167 | } |
| 168 | EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes); |
| 169 | |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 170 | void kvm_mmu_set_base_ptes(u64 base_pte) |
| 171 | { |
| 172 | shadow_base_present_pte = base_pte; |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes); |
| 175 | |
| 176 | void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask, |
| 177 | u64 dirty_mask, u64 nx_mask, u64 x_mask) |
| 178 | { |
| 179 | shadow_user_mask = user_mask; |
| 180 | shadow_accessed_mask = accessed_mask; |
| 181 | shadow_dirty_mask = dirty_mask; |
| 182 | shadow_nx_mask = nx_mask; |
| 183 | shadow_x_mask = x_mask; |
| 184 | } |
| 185 | EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes); |
| 186 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 187 | static int is_write_protection(struct kvm_vcpu *vcpu) |
| 188 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 189 | return vcpu->arch.cr0 & X86_CR0_WP; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static int is_cpuid_PSE36(void) |
| 193 | { |
| 194 | return 1; |
| 195 | } |
| 196 | |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 197 | static int is_nx(struct kvm_vcpu *vcpu) |
| 198 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 199 | return vcpu->arch.shadow_efer & EFER_NX; |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 202 | static int is_present_pte(unsigned long pte) |
| 203 | { |
| 204 | return pte & PT_PRESENT_MASK; |
| 205 | } |
| 206 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 207 | static int is_shadow_present_pte(u64 pte) |
| 208 | { |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 209 | return pte != shadow_trap_nonpresent_pte |
| 210 | && pte != shadow_notrap_nonpresent_pte; |
| 211 | } |
| 212 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 213 | static int is_large_pte(u64 pte) |
| 214 | { |
| 215 | return pte & PT_PAGE_SIZE_MASK; |
| 216 | } |
| 217 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 218 | static int is_writeble_pte(unsigned long pte) |
| 219 | { |
| 220 | return pte & PT_WRITABLE_MASK; |
| 221 | } |
| 222 | |
Avi Kivity | e3c5e7ec | 2007-10-11 12:32:30 +0200 | [diff] [blame] | 223 | static int is_dirty_pte(unsigned long pte) |
| 224 | { |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 225 | return pte & shadow_dirty_mask; |
Avi Kivity | e3c5e7ec | 2007-10-11 12:32:30 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 228 | static int is_rmap_pte(u64 pte) |
| 229 | { |
Avi Kivity | 4b1a80f | 2008-03-23 12:18:19 +0200 | [diff] [blame] | 230 | return is_shadow_present_pte(pte); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 233 | static pfn_t spte_to_pfn(u64 pte) |
Avi Kivity | 0b49ea8 | 2008-03-23 15:06:23 +0200 | [diff] [blame] | 234 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 235 | return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; |
Avi Kivity | 0b49ea8 | 2008-03-23 15:06:23 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Avi Kivity | da92852 | 2007-11-21 13:54:47 +0200 | [diff] [blame] | 238 | static gfn_t pse36_gfn_delta(u32 gpte) |
| 239 | { |
| 240 | int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT; |
| 241 | |
| 242 | return (gpte & PT32_DIR_PSE36_MASK) << shift; |
| 243 | } |
| 244 | |
Avi Kivity | e663ee6 | 2007-05-31 15:46:04 +0300 | [diff] [blame] | 245 | static void set_shadow_pte(u64 *sptep, u64 spte) |
| 246 | { |
| 247 | #ifdef CONFIG_X86_64 |
| 248 | set_64bit((unsigned long *)sptep, spte); |
| 249 | #else |
| 250 | set_64bit((unsigned long long *)sptep, spte); |
| 251 | #endif |
| 252 | } |
| 253 | |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 254 | static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache, |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 255 | struct kmem_cache *base_cache, int min) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 256 | { |
| 257 | void *obj; |
| 258 | |
| 259 | if (cache->nobjs >= min) |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 260 | return 0; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 261 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 262 | obj = kmem_cache_zalloc(base_cache, GFP_KERNEL); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 263 | if (!obj) |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 264 | return -ENOMEM; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 265 | cache->objects[cache->nobjs++] = obj; |
| 266 | } |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 267 | return 0; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc) |
| 271 | { |
| 272 | while (mc->nobjs) |
| 273 | kfree(mc->objects[--mc->nobjs]); |
| 274 | } |
| 275 | |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 276 | 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] | 277 | int min) |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 278 | { |
| 279 | struct page *page; |
| 280 | |
| 281 | if (cache->nobjs >= min) |
| 282 | return 0; |
| 283 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 284 | page = alloc_page(GFP_KERNEL); |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 285 | if (!page) |
| 286 | return -ENOMEM; |
| 287 | set_page_private(page, 0); |
| 288 | cache->objects[cache->nobjs++] = page_address(page); |
| 289 | } |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc) |
| 294 | { |
| 295 | while (mc->nobjs) |
Avi Kivity | c4d198d | 2007-07-21 09:06:46 +0300 | [diff] [blame] | 296 | free_page((unsigned long)mc->objects[--mc->nobjs]); |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 297 | } |
| 298 | |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 299 | static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu) |
| 300 | { |
| 301 | int r; |
| 302 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 303 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache, |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 304 | pte_chain_cache, 4); |
| 305 | if (r) |
| 306 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 307 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 308 | rmap_desc_cache, 1); |
| 309 | if (r) |
| 310 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 311 | r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8); |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 312 | if (r) |
| 313 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 314 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache, |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 315 | mmu_page_header_cache, 4); |
| 316 | out: |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 317 | return r; |
| 318 | } |
| 319 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 320 | static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) |
| 321 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 322 | mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache); |
| 323 | mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache); |
| 324 | mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache); |
| 325 | mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc, |
| 329 | size_t size) |
| 330 | { |
| 331 | void *p; |
| 332 | |
| 333 | BUG_ON(!mc->nobjs); |
| 334 | p = mc->objects[--mc->nobjs]; |
| 335 | memset(p, 0, size); |
| 336 | return p; |
| 337 | } |
| 338 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 339 | static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu) |
| 340 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 341 | return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache, |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 342 | sizeof(struct kvm_pte_chain)); |
| 343 | } |
| 344 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 345 | static void mmu_free_pte_chain(struct kvm_pte_chain *pc) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 346 | { |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 347 | kfree(pc); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu) |
| 351 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 352 | return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache, |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 353 | sizeof(struct kvm_rmap_desc)); |
| 354 | } |
| 355 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 356 | static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 357 | { |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 358 | kfree(rd); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 361 | /* |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 362 | * Return the pointer to the largepage write count for a given |
| 363 | * gfn, handling slots that are not large page aligned. |
| 364 | */ |
| 365 | static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot) |
| 366 | { |
| 367 | unsigned long idx; |
| 368 | |
| 369 | idx = (gfn / KVM_PAGES_PER_HPAGE) - |
| 370 | (slot->base_gfn / KVM_PAGES_PER_HPAGE); |
| 371 | return &slot->lpage_info[idx].write_count; |
| 372 | } |
| 373 | |
| 374 | static void account_shadowed(struct kvm *kvm, gfn_t gfn) |
| 375 | { |
| 376 | int *write_count; |
| 377 | |
| 378 | write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn)); |
| 379 | *write_count += 1; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn) |
| 383 | { |
| 384 | int *write_count; |
| 385 | |
| 386 | write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn)); |
| 387 | *write_count -= 1; |
| 388 | WARN_ON(*write_count < 0); |
| 389 | } |
| 390 | |
| 391 | static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn) |
| 392 | { |
| 393 | struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); |
| 394 | int *largepage_idx; |
| 395 | |
| 396 | if (slot) { |
| 397 | largepage_idx = slot_largepage_idx(gfn, slot); |
| 398 | return *largepage_idx; |
| 399 | } |
| 400 | |
| 401 | return 1; |
| 402 | } |
| 403 | |
| 404 | static int host_largepage_backed(struct kvm *kvm, gfn_t gfn) |
| 405 | { |
| 406 | struct vm_area_struct *vma; |
| 407 | unsigned long addr; |
| 408 | |
| 409 | addr = gfn_to_hva(kvm, gfn); |
| 410 | if (kvm_is_error_hva(addr)) |
| 411 | return 0; |
| 412 | |
| 413 | vma = find_vma(current->mm, addr); |
| 414 | if (vma && is_vm_hugetlb_page(vma)) |
| 415 | return 1; |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn) |
| 421 | { |
| 422 | struct kvm_memory_slot *slot; |
| 423 | |
| 424 | if (has_wrprotected_page(vcpu->kvm, large_gfn)) |
| 425 | return 0; |
| 426 | |
| 427 | if (!host_largepage_backed(vcpu->kvm, large_gfn)) |
| 428 | return 0; |
| 429 | |
| 430 | slot = gfn_to_memslot(vcpu->kvm, large_gfn); |
| 431 | if (slot && slot->dirty_bitmap) |
| 432 | return 0; |
| 433 | |
| 434 | return 1; |
| 435 | } |
| 436 | |
| 437 | /* |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 438 | * Take gfn and return the reverse mapping to it. |
| 439 | * Note: gfn must be unaliased before this function get called |
| 440 | */ |
| 441 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 442 | static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage) |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 443 | { |
| 444 | struct kvm_memory_slot *slot; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 445 | unsigned long idx; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 446 | |
| 447 | slot = gfn_to_memslot(kvm, gfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 448 | if (!lpage) |
| 449 | return &slot->rmap[gfn - slot->base_gfn]; |
| 450 | |
| 451 | idx = (gfn / KVM_PAGES_PER_HPAGE) - |
| 452 | (slot->base_gfn / KVM_PAGES_PER_HPAGE); |
| 453 | |
| 454 | return &slot->lpage_info[idx].rmap_pde; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 458 | * Reverse mapping data structures: |
| 459 | * |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 460 | * If rmapp bit zero is zero, then rmapp point to the shadw page table entry |
| 461 | * that points to page_address(page). |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 462 | * |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 463 | * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc |
| 464 | * containing more mappings. |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 465 | */ |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 466 | static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 467 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 468 | struct kvm_mmu_page *sp; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 469 | struct kvm_rmap_desc *desc; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 470 | unsigned long *rmapp; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 471 | int i; |
| 472 | |
| 473 | if (!is_rmap_pte(*spte)) |
| 474 | return; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 475 | gfn = unalias_gfn(vcpu->kvm, gfn); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 476 | sp = page_header(__pa(spte)); |
| 477 | sp->gfns[spte - sp->spt] = gfn; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 478 | rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 479 | if (!*rmapp) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 480 | rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 481 | *rmapp = (unsigned long)spte; |
| 482 | } else if (!(*rmapp & 1)) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 483 | rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 484 | desc = mmu_alloc_rmap_desc(vcpu); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 485 | desc->shadow_ptes[0] = (u64 *)*rmapp; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 486 | desc->shadow_ptes[1] = spte; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 487 | *rmapp = (unsigned long)desc | 1; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 488 | } else { |
| 489 | rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 490 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 491 | while (desc->shadow_ptes[RMAP_EXT-1] && desc->more) |
| 492 | desc = desc->more; |
| 493 | if (desc->shadow_ptes[RMAP_EXT-1]) { |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 494 | desc->more = mmu_alloc_rmap_desc(vcpu); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 495 | desc = desc->more; |
| 496 | } |
| 497 | for (i = 0; desc->shadow_ptes[i]; ++i) |
| 498 | ; |
| 499 | desc->shadow_ptes[i] = spte; |
| 500 | } |
| 501 | } |
| 502 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 503 | static void rmap_desc_remove_entry(unsigned long *rmapp, |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 504 | struct kvm_rmap_desc *desc, |
| 505 | int i, |
| 506 | struct kvm_rmap_desc *prev_desc) |
| 507 | { |
| 508 | int j; |
| 509 | |
| 510 | for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j) |
| 511 | ; |
| 512 | desc->shadow_ptes[i] = desc->shadow_ptes[j]; |
Al Viro | 11718b4d | 2007-02-09 16:39:20 +0000 | [diff] [blame] | 513 | desc->shadow_ptes[j] = NULL; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 514 | if (j != 0) |
| 515 | return; |
| 516 | if (!prev_desc && !desc->more) |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 517 | *rmapp = (unsigned long)desc->shadow_ptes[0]; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 518 | else |
| 519 | if (prev_desc) |
| 520 | prev_desc->more = desc->more; |
| 521 | else |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 522 | *rmapp = (unsigned long)desc->more | 1; |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 523 | mmu_free_rmap_desc(desc); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 526 | static void rmap_remove(struct kvm *kvm, u64 *spte) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 527 | { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 528 | struct kvm_rmap_desc *desc; |
| 529 | struct kvm_rmap_desc *prev_desc; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 530 | struct kvm_mmu_page *sp; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 531 | pfn_t pfn; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 532 | unsigned long *rmapp; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 533 | int i; |
| 534 | |
| 535 | if (!is_rmap_pte(*spte)) |
| 536 | return; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 537 | sp = page_header(__pa(spte)); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 538 | pfn = spte_to_pfn(*spte); |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 539 | if (*spte & shadow_accessed_mask) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 540 | kvm_set_pfn_accessed(pfn); |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 541 | if (is_writeble_pte(*spte)) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 542 | kvm_release_pfn_dirty(pfn); |
Izik Eidus | b4231d6 | 2007-11-20 11:49:33 +0200 | [diff] [blame] | 543 | else |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 544 | kvm_release_pfn_clean(pfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 545 | rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte)); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 546 | if (!*rmapp) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 547 | printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte); |
| 548 | BUG(); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 549 | } else if (!(*rmapp & 1)) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 550 | rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 551 | if ((u64 *)*rmapp != spte) { |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 552 | printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n", |
| 553 | spte, *spte); |
| 554 | BUG(); |
| 555 | } |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 556 | *rmapp = 0; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 557 | } else { |
| 558 | rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 559 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 560 | prev_desc = NULL; |
| 561 | while (desc) { |
| 562 | for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) |
| 563 | if (desc->shadow_ptes[i] == spte) { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 564 | rmap_desc_remove_entry(rmapp, |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 565 | desc, i, |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 566 | prev_desc); |
| 567 | return; |
| 568 | } |
| 569 | prev_desc = desc; |
| 570 | desc = desc->more; |
| 571 | } |
| 572 | BUG(); |
| 573 | } |
| 574 | } |
| 575 | |
Izik Eidus | 98348e9 | 2007-10-16 14:42:30 +0200 | [diff] [blame] | 576 | static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte) |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 577 | { |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 578 | struct kvm_rmap_desc *desc; |
Izik Eidus | 98348e9 | 2007-10-16 14:42:30 +0200 | [diff] [blame] | 579 | struct kvm_rmap_desc *prev_desc; |
| 580 | u64 *prev_spte; |
| 581 | int i; |
| 582 | |
| 583 | if (!*rmapp) |
| 584 | return NULL; |
| 585 | else if (!(*rmapp & 1)) { |
| 586 | if (!spte) |
| 587 | return (u64 *)*rmapp; |
| 588 | return NULL; |
| 589 | } |
| 590 | desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
| 591 | prev_desc = NULL; |
| 592 | prev_spte = NULL; |
| 593 | while (desc) { |
| 594 | for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) { |
| 595 | if (prev_spte == spte) |
| 596 | return desc->shadow_ptes[i]; |
| 597 | prev_spte = desc->shadow_ptes[i]; |
| 598 | } |
| 599 | desc = desc->more; |
| 600 | } |
| 601 | return NULL; |
| 602 | } |
| 603 | |
| 604 | static void rmap_write_protect(struct kvm *kvm, u64 gfn) |
| 605 | { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 606 | unsigned long *rmapp; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 607 | u64 *spte; |
Eddie Dong | caa5b8a | 2007-12-18 06:08:27 +0800 | [diff] [blame] | 608 | int write_protected = 0; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 609 | |
Anthony Liguori | 4a4c992 | 2007-10-10 20:08:41 -0500 | [diff] [blame] | 610 | gfn = unalias_gfn(kvm, gfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 611 | rmapp = gfn_to_rmap(kvm, gfn, 0); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 612 | |
Izik Eidus | 98348e9 | 2007-10-16 14:42:30 +0200 | [diff] [blame] | 613 | spte = rmap_next(kvm, rmapp, NULL); |
| 614 | while (spte) { |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 615 | BUG_ON(!spte); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 616 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 617 | rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte); |
Eddie Dong | caa5b8a | 2007-12-18 06:08:27 +0800 | [diff] [blame] | 618 | if (is_writeble_pte(*spte)) { |
Izik Eidus | 9647c14 | 2007-10-16 14:43:46 +0200 | [diff] [blame] | 619 | set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK); |
Eddie Dong | caa5b8a | 2007-12-18 06:08:27 +0800 | [diff] [blame] | 620 | write_protected = 1; |
| 621 | } |
Izik Eidus | 9647c14 | 2007-10-16 14:43:46 +0200 | [diff] [blame] | 622 | spte = rmap_next(kvm, rmapp, spte); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 623 | } |
Izik Eidus | 855149a | 2008-03-20 18:17:24 +0200 | [diff] [blame] | 624 | if (write_protected) { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 625 | pfn_t pfn; |
Izik Eidus | 855149a | 2008-03-20 18:17:24 +0200 | [diff] [blame] | 626 | |
| 627 | spte = rmap_next(kvm, rmapp, NULL); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 628 | pfn = spte_to_pfn(*spte); |
| 629 | kvm_set_pfn_dirty(pfn); |
Izik Eidus | 855149a | 2008-03-20 18:17:24 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 632 | /* check for huge page mappings */ |
| 633 | rmapp = gfn_to_rmap(kvm, gfn, 1); |
| 634 | spte = rmap_next(kvm, rmapp, NULL); |
| 635 | while (spte) { |
| 636 | BUG_ON(!spte); |
| 637 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
| 638 | BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)); |
| 639 | pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn); |
| 640 | if (is_writeble_pte(*spte)) { |
| 641 | rmap_remove(kvm, spte); |
| 642 | --kvm->stat.lpages; |
| 643 | set_shadow_pte(spte, shadow_trap_nonpresent_pte); |
Marcelo Tosatti | 6597ca0 | 2008-06-08 01:48:53 -0300 | [diff] [blame] | 644 | spte = NULL; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 645 | write_protected = 1; |
| 646 | } |
| 647 | spte = rmap_next(kvm, rmapp, spte); |
| 648 | } |
| 649 | |
Eddie Dong | caa5b8a | 2007-12-18 06:08:27 +0800 | [diff] [blame] | 650 | if (write_protected) |
| 651 | kvm_flush_remote_tlbs(kvm); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 652 | |
| 653 | account_shadowed(kvm, gfn); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 656 | #ifdef MMU_DEBUG |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 657 | static int is_empty_shadow_page(u64 *spt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 658 | { |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 659 | u64 *pos; |
| 660 | u64 *end; |
| 661 | |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 662 | for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++) |
Avi Kivity | 3c91551 | 2008-05-20 16:21:13 +0300 | [diff] [blame] | 663 | if (is_shadow_present_pte(*pos)) { |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 664 | printk(KERN_ERR "%s: %p %llx\n", __func__, |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 665 | pos, *pos); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 666 | return 0; |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 667 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 668 | return 1; |
| 669 | } |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 670 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 671 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 672 | 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] | 673 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 674 | ASSERT(is_empty_shadow_page(sp->spt)); |
| 675 | list_del(&sp->link); |
| 676 | __free_page(virt_to_page(sp->spt)); |
| 677 | __free_page(virt_to_page(sp->gfns)); |
| 678 | kfree(sp); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 679 | ++kvm->arch.n_free_mmu_pages; |
Avi Kivity | 260746c | 2007-01-05 16:36:49 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 682 | static unsigned kvm_page_table_hashfn(gfn_t gfn) |
| 683 | { |
Dong, Eddie | 1ae0a13 | 2008-01-07 13:20:25 +0200 | [diff] [blame] | 684 | return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 687 | static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, |
| 688 | u64 *parent_pte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 689 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 690 | struct kvm_mmu_page *sp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 691 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 692 | sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp); |
| 693 | sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE); |
| 694 | 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] | 695 | set_page_private(virt_to_page(sp->spt), (unsigned long)sp); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 696 | list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 697 | ASSERT(is_empty_shadow_page(sp->spt)); |
| 698 | sp->slot_bitmap = 0; |
| 699 | sp->multimapped = 0; |
| 700 | sp->parent_pte = parent_pte; |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 701 | --vcpu->kvm->arch.n_free_mmu_pages; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 702 | return sp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 705 | static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 706 | struct kvm_mmu_page *sp, u64 *parent_pte) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 707 | { |
| 708 | struct kvm_pte_chain *pte_chain; |
| 709 | struct hlist_node *node; |
| 710 | int i; |
| 711 | |
| 712 | if (!parent_pte) |
| 713 | return; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 714 | if (!sp->multimapped) { |
| 715 | u64 *old = sp->parent_pte; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 716 | |
| 717 | if (!old) { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 718 | sp->parent_pte = parent_pte; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 719 | return; |
| 720 | } |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 721 | sp->multimapped = 1; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 722 | pte_chain = mmu_alloc_pte_chain(vcpu); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 723 | INIT_HLIST_HEAD(&sp->parent_ptes); |
| 724 | hlist_add_head(&pte_chain->link, &sp->parent_ptes); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 725 | pte_chain->parent_ptes[0] = old; |
| 726 | } |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 727 | hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 728 | if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1]) |
| 729 | continue; |
| 730 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) |
| 731 | if (!pte_chain->parent_ptes[i]) { |
| 732 | pte_chain->parent_ptes[i] = parent_pte; |
| 733 | return; |
| 734 | } |
| 735 | } |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 736 | pte_chain = mmu_alloc_pte_chain(vcpu); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 737 | BUG_ON(!pte_chain); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 738 | hlist_add_head(&pte_chain->link, &sp->parent_ptes); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 739 | pte_chain->parent_ptes[0] = parent_pte; |
| 740 | } |
| 741 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 742 | static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp, |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 743 | u64 *parent_pte) |
| 744 | { |
| 745 | struct kvm_pte_chain *pte_chain; |
| 746 | struct hlist_node *node; |
| 747 | int i; |
| 748 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 749 | if (!sp->multimapped) { |
| 750 | BUG_ON(sp->parent_pte != parent_pte); |
| 751 | sp->parent_pte = NULL; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 752 | return; |
| 753 | } |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 754 | hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 755 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) { |
| 756 | if (!pte_chain->parent_ptes[i]) |
| 757 | break; |
| 758 | if (pte_chain->parent_ptes[i] != parent_pte) |
| 759 | continue; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 760 | while (i + 1 < NR_PTE_CHAIN_ENTRIES |
| 761 | && pte_chain->parent_ptes[i + 1]) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 762 | pte_chain->parent_ptes[i] |
| 763 | = pte_chain->parent_ptes[i + 1]; |
| 764 | ++i; |
| 765 | } |
| 766 | pte_chain->parent_ptes[i] = NULL; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 767 | if (i == 0) { |
| 768 | hlist_del(&pte_chain->link); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 769 | mmu_free_pte_chain(pte_chain); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 770 | if (hlist_empty(&sp->parent_ptes)) { |
| 771 | sp->multimapped = 0; |
| 772 | sp->parent_pte = NULL; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 773 | } |
| 774 | } |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 775 | return; |
| 776 | } |
| 777 | BUG(); |
| 778 | } |
| 779 | |
Avi Kivity | d761a50 | 2008-05-29 14:55:03 +0300 | [diff] [blame] | 780 | static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu, |
| 781 | struct kvm_mmu_page *sp) |
| 782 | { |
| 783 | int i; |
| 784 | |
| 785 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
| 786 | sp->spt[i] = shadow_trap_nonpresent_pte; |
| 787 | } |
| 788 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 789 | 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] | 790 | { |
| 791 | unsigned index; |
| 792 | struct hlist_head *bucket; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 793 | struct kvm_mmu_page *sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 794 | struct hlist_node *node; |
| 795 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 796 | pgprintk("%s: looking for gfn %lx\n", __func__, gfn); |
Dong, Eddie | 1ae0a13 | 2008-01-07 13:20:25 +0200 | [diff] [blame] | 797 | index = kvm_page_table_hashfn(gfn); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 798 | bucket = &kvm->arch.mmu_page_hash[index]; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 799 | hlist_for_each_entry(sp, node, bucket, hash_link) |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 800 | if (sp->gfn == gfn && !sp->role.metaphysical |
| 801 | && !sp->role.invalid) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 802 | pgprintk("%s: found role %x\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 803 | __func__, sp->role.word); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 804 | return sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 805 | } |
| 806 | return NULL; |
| 807 | } |
| 808 | |
| 809 | static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, |
| 810 | gfn_t gfn, |
| 811 | gva_t gaddr, |
| 812 | unsigned level, |
| 813 | int metaphysical, |
Avi Kivity | 41074d0 | 2007-12-09 17:00:02 +0200 | [diff] [blame] | 814 | unsigned access, |
Avi Kivity | f7d9c7b | 2008-02-26 22:12:10 +0200 | [diff] [blame] | 815 | u64 *parent_pte) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 816 | { |
| 817 | union kvm_mmu_page_role role; |
| 818 | unsigned index; |
| 819 | unsigned quadrant; |
| 820 | struct hlist_head *bucket; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 821 | struct kvm_mmu_page *sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 822 | struct hlist_node *node; |
| 823 | |
| 824 | role.word = 0; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 825 | role.glevels = vcpu->arch.mmu.root_level; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 826 | role.level = level; |
| 827 | role.metaphysical = metaphysical; |
Avi Kivity | 41074d0 | 2007-12-09 17:00:02 +0200 | [diff] [blame] | 828 | role.access = access; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 829 | if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 830 | quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); |
| 831 | quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1; |
| 832 | role.quadrant = quadrant; |
| 833 | } |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 834 | pgprintk("%s: looking gfn %lx role %x\n", __func__, |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 835 | gfn, role.word); |
Dong, Eddie | 1ae0a13 | 2008-01-07 13:20:25 +0200 | [diff] [blame] | 836 | index = kvm_page_table_hashfn(gfn); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 837 | bucket = &vcpu->kvm->arch.mmu_page_hash[index]; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 838 | hlist_for_each_entry(sp, node, bucket, hash_link) |
| 839 | if (sp->gfn == gfn && sp->role.word == role.word) { |
| 840 | mmu_page_add_parent_pte(vcpu, sp, parent_pte); |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 841 | pgprintk("%s: found\n", __func__); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 842 | return sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 843 | } |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 844 | ++vcpu->kvm->stat.mmu_cache_miss; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 845 | sp = kvm_mmu_alloc_page(vcpu, parent_pte); |
| 846 | if (!sp) |
| 847 | return sp; |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 848 | pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 849 | sp->gfn = gfn; |
| 850 | sp->role = role; |
| 851 | hlist_add_head(&sp->hash_link, bucket); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 852 | if (!metaphysical) |
Anthony Liguori | 4a4c992 | 2007-10-10 20:08:41 -0500 | [diff] [blame] | 853 | rmap_write_protect(vcpu->kvm, gfn); |
Avi Kivity | 131d827 | 2008-05-29 14:56:28 +0300 | [diff] [blame] | 854 | if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte) |
| 855 | vcpu->arch.mmu.prefetch_page(vcpu, sp); |
| 856 | else |
| 857 | nonpaging_prefetch_page(vcpu, sp); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 858 | return sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 859 | } |
| 860 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 861 | static void kvm_mmu_page_unlink_children(struct kvm *kvm, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 862 | struct kvm_mmu_page *sp) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 863 | { |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 864 | unsigned i; |
| 865 | u64 *pt; |
| 866 | u64 ent; |
| 867 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 868 | pt = sp->spt; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 869 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 870 | if (sp->role.level == PT_PAGE_TABLE_LEVEL) { |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 871 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 872 | if (is_shadow_present_pte(pt[i])) |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 873 | rmap_remove(kvm, &pt[i]); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 874 | pt[i] = shadow_trap_nonpresent_pte; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 875 | } |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 876 | kvm_flush_remote_tlbs(kvm); |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 877 | return; |
| 878 | } |
| 879 | |
| 880 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 881 | ent = pt[i]; |
| 882 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 883 | if (is_shadow_present_pte(ent)) { |
| 884 | if (!is_large_pte(ent)) { |
| 885 | ent &= PT64_BASE_ADDR_MASK; |
| 886 | mmu_page_remove_parent_pte(page_header(ent), |
| 887 | &pt[i]); |
| 888 | } else { |
| 889 | --kvm->stat.lpages; |
| 890 | rmap_remove(kvm, &pt[i]); |
| 891 | } |
| 892 | } |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 893 | pt[i] = shadow_trap_nonpresent_pte; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 894 | } |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 895 | kvm_flush_remote_tlbs(kvm); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 896 | } |
| 897 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 898 | 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] | 899 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 900 | mmu_page_remove_parent_pte(sp, parent_pte); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 901 | } |
| 902 | |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 903 | static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm) |
| 904 | { |
| 905 | int i; |
| 906 | |
| 907 | for (i = 0; i < KVM_MAX_VCPUS; ++i) |
| 908 | if (kvm->vcpus[i]) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 909 | kvm->vcpus[i]->arch.last_pte_updated = NULL; |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 910 | } |
| 911 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 912 | static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 913 | { |
| 914 | u64 *parent_pte; |
| 915 | |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 916 | ++kvm->stat.mmu_shadow_zapped; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 917 | while (sp->multimapped || sp->parent_pte) { |
| 918 | if (!sp->multimapped) |
| 919 | parent_pte = sp->parent_pte; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 920 | else { |
| 921 | struct kvm_pte_chain *chain; |
| 922 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 923 | chain = container_of(sp->parent_ptes.first, |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 924 | struct kvm_pte_chain, link); |
| 925 | parent_pte = chain->parent_ptes[0]; |
| 926 | } |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 927 | BUG_ON(!parent_pte); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 928 | kvm_mmu_put_page(sp, parent_pte); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 929 | set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 930 | } |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 931 | kvm_mmu_page_unlink_children(kvm, sp); |
| 932 | if (!sp->root_count) { |
Marcelo Tosatti | 376c53c | 2008-07-10 20:54:29 -0300 | [diff] [blame] | 933 | if (!sp->role.metaphysical && !sp->role.invalid) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 934 | unaccount_shadowed(kvm, sp->gfn); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 935 | hlist_del(&sp->hash_link); |
| 936 | kvm_mmu_free_page(kvm, sp); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 937 | } else { |
Marcelo Tosatti | 376c53c | 2008-07-10 20:54:29 -0300 | [diff] [blame] | 938 | int invalid = sp->role.invalid; |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 939 | list_move(&sp->link, &kvm->arch.active_mmu_pages); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 940 | sp->role.invalid = 1; |
| 941 | kvm_reload_remote_mmus(kvm); |
Marcelo Tosatti | 376c53c | 2008-07-10 20:54:29 -0300 | [diff] [blame] | 942 | if (!sp->role.metaphysical && !invalid) |
| 943 | unaccount_shadowed(kvm, sp->gfn); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 944 | } |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 945 | kvm_mmu_reset_last_pte_updated(kvm); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 948 | /* |
| 949 | * Changing the number of mmu pages allocated to the vm |
| 950 | * Note: if kvm_nr_mmu_pages is too small, you will get dead lock |
| 951 | */ |
| 952 | void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages) |
| 953 | { |
| 954 | /* |
| 955 | * If we set the number of mmu pages to be smaller be than the |
| 956 | * number of actived pages , we must to free some mmu pages before we |
| 957 | * change the value |
| 958 | */ |
| 959 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 960 | if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) > |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 961 | kvm_nr_mmu_pages) { |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 962 | int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages |
| 963 | - kvm->arch.n_free_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 964 | |
| 965 | while (n_used_mmu_pages > kvm_nr_mmu_pages) { |
| 966 | struct kvm_mmu_page *page; |
| 967 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 968 | page = container_of(kvm->arch.active_mmu_pages.prev, |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 969 | struct kvm_mmu_page, link); |
| 970 | kvm_mmu_zap_page(kvm, page); |
| 971 | n_used_mmu_pages--; |
| 972 | } |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 973 | kvm->arch.n_free_mmu_pages = 0; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 974 | } |
| 975 | else |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 976 | kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages |
| 977 | - kvm->arch.n_alloc_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 978 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 979 | kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 980 | } |
| 981 | |
Anthony Liguori | f67a46f | 2007-10-10 19:25:50 -0500 | [diff] [blame] | 982 | static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 983 | { |
| 984 | unsigned index; |
| 985 | struct hlist_head *bucket; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 986 | struct kvm_mmu_page *sp; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 987 | struct hlist_node *node, *n; |
| 988 | int r; |
| 989 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 990 | pgprintk("%s: looking for gfn %lx\n", __func__, gfn); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 991 | r = 0; |
Dong, Eddie | 1ae0a13 | 2008-01-07 13:20:25 +0200 | [diff] [blame] | 992 | index = kvm_page_table_hashfn(gfn); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 993 | bucket = &kvm->arch.mmu_page_hash[index]; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 994 | hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) |
| 995 | if (sp->gfn == gfn && !sp->role.metaphysical) { |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 996 | pgprintk("%s: gfn %lx role %x\n", __func__, gfn, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 997 | sp->role.word); |
| 998 | kvm_mmu_zap_page(kvm, sp); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 999 | r = 1; |
| 1000 | } |
| 1001 | return r; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
Anthony Liguori | f67a46f | 2007-10-10 19:25:50 -0500 | [diff] [blame] | 1004 | static void mmu_unshadow(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 1005 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1006 | struct kvm_mmu_page *sp; |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 1007 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1008 | while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) { |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1009 | pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1010 | kvm_mmu_zap_page(kvm, sp); |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
Avi Kivity | 38c335f | 2007-11-21 14:20:22 +0200 | [diff] [blame] | 1014 | 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] | 1015 | { |
Avi Kivity | 38c335f | 2007-11-21 14:20:22 +0200 | [diff] [blame] | 1016 | int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn)); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1017 | struct kvm_mmu_page *sp = page_header(__pa(pte)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1018 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1019 | __set_bit(slot, &sp->slot_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1022 | struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva) |
| 1023 | { |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1024 | struct page *page; |
| 1025 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1026 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1027 | |
| 1028 | if (gpa == UNMAPPED_GVA) |
| 1029 | return NULL; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1030 | |
| 1031 | down_read(¤t->mm->mmap_sem); |
| 1032 | page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
| 1033 | up_read(¤t->mm->mmap_sem); |
| 1034 | |
| 1035 | return page; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1038 | static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte, |
| 1039 | unsigned pt_access, unsigned pte_access, |
| 1040 | int user_fault, int write_fault, int dirty, |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1041 | int *ptwrite, int largepage, gfn_t gfn, |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1042 | pfn_t pfn, bool speculative) |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1043 | { |
| 1044 | u64 spte; |
Marcelo Tosatti | 15aaa81 | 2008-03-17 10:08:18 -0300 | [diff] [blame] | 1045 | int was_rmapped = 0; |
Izik Eidus | 75e68e6 | 2008-01-12 23:49:09 +0200 | [diff] [blame] | 1046 | int was_writeble = is_writeble_pte(*shadow_pte); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1047 | |
Avi Kivity | bc750ba | 2007-12-09 18:39:41 +0200 | [diff] [blame] | 1048 | pgprintk("%s: spte %llx access %x write_fault %d" |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1049 | " user_fault %d gfn %lx\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1050 | __func__, *shadow_pte, pt_access, |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1051 | write_fault, user_fault, gfn); |
| 1052 | |
Marcelo Tosatti | 15aaa81 | 2008-03-17 10:08:18 -0300 | [diff] [blame] | 1053 | if (is_rmap_pte(*shadow_pte)) { |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1054 | /* |
| 1055 | * If we overwrite a PTE page pointer with a 2MB PMD, unlink |
| 1056 | * the parent of the now unreachable PTE. |
| 1057 | */ |
| 1058 | if (largepage && !is_large_pte(*shadow_pte)) { |
| 1059 | struct kvm_mmu_page *child; |
| 1060 | u64 pte = *shadow_pte; |
| 1061 | |
| 1062 | child = page_header(pte & PT64_BASE_ADDR_MASK); |
| 1063 | mmu_page_remove_parent_pte(child, shadow_pte); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1064 | } else if (pfn != spte_to_pfn(*shadow_pte)) { |
Marcelo Tosatti | 15aaa81 | 2008-03-17 10:08:18 -0300 | [diff] [blame] | 1065 | pgprintk("hfn old %lx new %lx\n", |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1066 | spte_to_pfn(*shadow_pte), pfn); |
Marcelo Tosatti | 15aaa81 | 2008-03-17 10:08:18 -0300 | [diff] [blame] | 1067 | rmap_remove(vcpu->kvm, shadow_pte); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1068 | } else { |
| 1069 | if (largepage) |
| 1070 | was_rmapped = is_large_pte(*shadow_pte); |
| 1071 | else |
| 1072 | was_rmapped = 1; |
Marcelo Tosatti | 15aaa81 | 2008-03-17 10:08:18 -0300 | [diff] [blame] | 1073 | } |
Marcelo Tosatti | 15aaa81 | 2008-03-17 10:08:18 -0300 | [diff] [blame] | 1074 | } |
| 1075 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1076 | /* |
| 1077 | * We don't set the accessed bit, since we sometimes want to see |
| 1078 | * whether the guest actually used the pte (in order to detect |
| 1079 | * demand paging). |
| 1080 | */ |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 1081 | spte = shadow_base_present_pte | shadow_dirty_mask; |
Avi Kivity | 947da53 | 2008-03-18 11:05:52 +0200 | [diff] [blame] | 1082 | if (!speculative) |
| 1083 | pte_access |= PT_ACCESSED_MASK; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1084 | if (!dirty) |
| 1085 | pte_access &= ~ACC_WRITE_MASK; |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 1086 | if (pte_access & ACC_EXEC_MASK) |
| 1087 | spte |= shadow_x_mask; |
| 1088 | else |
| 1089 | spte |= shadow_nx_mask; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1090 | if (pte_access & ACC_USER_MASK) |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 1091 | spte |= shadow_user_mask; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1092 | if (largepage) |
| 1093 | spte |= PT_PAGE_SIZE_MASK; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1094 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1095 | spte |= (u64)pfn << PAGE_SHIFT; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1096 | |
| 1097 | if ((pte_access & ACC_WRITE_MASK) |
| 1098 | || (write_fault && !is_write_protection(vcpu) && !user_fault)) { |
| 1099 | struct kvm_mmu_page *shadow; |
| 1100 | |
| 1101 | spte |= PT_WRITABLE_MASK; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1102 | |
| 1103 | shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1104 | if (shadow || |
| 1105 | (largepage && has_wrprotected_page(vcpu->kvm, gfn))) { |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1106 | pgprintk("%s: found shadow page for %lx, marking ro\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1107 | __func__, gfn); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1108 | pte_access &= ~ACC_WRITE_MASK; |
| 1109 | if (is_writeble_pte(spte)) { |
| 1110 | spte &= ~PT_WRITABLE_MASK; |
| 1111 | kvm_x86_ops->tlb_flush(vcpu); |
| 1112 | } |
| 1113 | if (write_fault) |
| 1114 | *ptwrite = 1; |
| 1115 | } |
| 1116 | } |
| 1117 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1118 | if (pte_access & ACC_WRITE_MASK) |
| 1119 | mark_page_dirty(vcpu->kvm, gfn); |
| 1120 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1121 | pgprintk("%s: setting spte %llx\n", __func__, spte); |
Avi Kivity | db475c3 | 2008-06-22 16:46:22 +0300 | [diff] [blame] | 1122 | pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n", |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1123 | (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB", |
| 1124 | (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1125 | set_shadow_pte(shadow_pte, spte); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1126 | if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK) |
| 1127 | && (spte & PT_PRESENT_MASK)) |
| 1128 | ++vcpu->kvm->stat.lpages; |
| 1129 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1130 | page_header_update_slot(vcpu->kvm, shadow_pte, gfn); |
| 1131 | if (!was_rmapped) { |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1132 | rmap_add(vcpu, shadow_pte, gfn, largepage); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1133 | if (!is_rmap_pte(*shadow_pte)) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1134 | kvm_release_pfn_clean(pfn); |
Izik Eidus | 75e68e6 | 2008-01-12 23:49:09 +0200 | [diff] [blame] | 1135 | } else { |
| 1136 | if (was_writeble) |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1137 | kvm_release_pfn_dirty(pfn); |
Izik Eidus | 75e68e6 | 2008-01-12 23:49:09 +0200 | [diff] [blame] | 1138 | else |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1139 | kvm_release_pfn_clean(pfn); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1140 | } |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 1141 | if (speculative) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1142 | vcpu->arch.last_pte_updated = shadow_pte; |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 1143 | vcpu->arch.last_pte_gfn = gfn; |
| 1144 | } |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1147 | static void nonpaging_new_cr3(struct kvm_vcpu *vcpu) |
| 1148 | { |
| 1149 | } |
| 1150 | |
Joerg Roedel | 4d9976b | 2008-02-07 13:47:42 +0100 | [diff] [blame] | 1151 | static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write, |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1152 | int largepage, gfn_t gfn, pfn_t pfn, |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1153 | int level) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1154 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1155 | hpa_t table_addr = vcpu->arch.mmu.root_hpa; |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 1156 | int pt_write = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1157 | |
| 1158 | for (; ; level--) { |
| 1159 | u32 index = PT64_INDEX(v, level); |
| 1160 | u64 *table; |
| 1161 | |
| 1162 | ASSERT(VALID_PAGE(table_addr)); |
| 1163 | table = __va(table_addr); |
| 1164 | |
| 1165 | if (level == 1) { |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 1166 | mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL, |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1167 | 0, write, 1, &pt_write, 0, gfn, pfn, false); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1168 | return pt_write; |
| 1169 | } |
| 1170 | |
| 1171 | if (largepage && level == 2) { |
| 1172 | mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL, |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1173 | 0, write, 1, &pt_write, 1, gfn, pfn, false); |
Avi Kivity | d196e34 | 2008-01-24 11:44:11 +0200 | [diff] [blame] | 1174 | return pt_write; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1177 | if (table[index] == shadow_trap_nonpresent_pte) { |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 1178 | struct kvm_mmu_page *new_table; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1179 | gfn_t pseudo_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1180 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1181 | pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK) |
| 1182 | >> PAGE_SHIFT; |
| 1183 | new_table = kvm_mmu_get_page(vcpu, pseudo_gfn, |
| 1184 | v, level - 1, |
Avi Kivity | f7d9c7b | 2008-02-26 22:12:10 +0200 | [diff] [blame] | 1185 | 1, ACC_ALL, &table[index]); |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 1186 | if (!new_table) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1187 | pgprintk("nonpaging_map: ENOMEM\n"); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1188 | kvm_release_pfn_clean(pfn); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1189 | return -ENOMEM; |
| 1190 | } |
| 1191 | |
Avi Kivity | 722c05f | 2008-07-13 11:33:54 +0300 | [diff] [blame] | 1192 | set_shadow_pte(&table[index], |
| 1193 | __pa(new_table->spt) |
| 1194 | | PT_PRESENT_MASK | PT_WRITABLE_MASK |
| 1195 | | shadow_user_mask | shadow_x_mask); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1196 | } |
| 1197 | table_addr = table[index] & PT64_BASE_ADDR_MASK; |
| 1198 | } |
| 1199 | } |
| 1200 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1201 | static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn) |
| 1202 | { |
| 1203 | int r; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1204 | int largepage = 0; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1205 | pfn_t pfn; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1206 | |
| 1207 | down_read(¤t->mm->mmap_sem); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1208 | if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) { |
| 1209 | gfn &= ~(KVM_PAGES_PER_HPAGE-1); |
| 1210 | largepage = 1; |
| 1211 | } |
| 1212 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1213 | pfn = gfn_to_pfn(vcpu->kvm, gfn); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1214 | up_read(¤t->mm->mmap_sem); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1215 | |
Avi Kivity | d196e34 | 2008-01-24 11:44:11 +0200 | [diff] [blame] | 1216 | /* mmio */ |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1217 | if (is_error_pfn(pfn)) { |
| 1218 | kvm_release_pfn_clean(pfn); |
Avi Kivity | d196e34 | 2008-01-24 11:44:11 +0200 | [diff] [blame] | 1219 | return 1; |
| 1220 | } |
| 1221 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1222 | spin_lock(&vcpu->kvm->mmu_lock); |
Avi Kivity | eb787d1 | 2007-12-31 15:27:49 +0200 | [diff] [blame] | 1223 | kvm_mmu_free_some_pages(vcpu); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1224 | r = __direct_map(vcpu, v, write, largepage, gfn, pfn, |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1225 | PT32E_ROOT_LEVEL); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1226 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 1227 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1228 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1229 | return r; |
| 1230 | } |
| 1231 | |
| 1232 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1233 | static void mmu_free_roots(struct kvm_vcpu *vcpu) |
| 1234 | { |
| 1235 | int i; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1236 | struct kvm_mmu_page *sp; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1237 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1238 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
Avi Kivity | 7b53aa5 | 2007-06-05 12:17:03 +0300 | [diff] [blame] | 1239 | return; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1240 | spin_lock(&vcpu->kvm->mmu_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1241 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 1242 | hpa_t root = vcpu->arch.mmu.root_hpa; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1243 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1244 | sp = page_header(root); |
| 1245 | --sp->root_count; |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1246 | if (!sp->root_count && sp->role.invalid) |
| 1247 | kvm_mmu_zap_page(vcpu->kvm, sp); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1248 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1249 | spin_unlock(&vcpu->kvm->mmu_lock); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1250 | return; |
| 1251 | } |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1252 | for (i = 0; i < 4; ++i) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1253 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1254 | |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 1255 | if (root) { |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 1256 | root &= PT64_BASE_ADDR_MASK; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1257 | sp = page_header(root); |
| 1258 | --sp->root_count; |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1259 | if (!sp->root_count && sp->role.invalid) |
| 1260 | kvm_mmu_zap_page(vcpu->kvm, sp); |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 1261 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1262 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1263 | } |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1264 | spin_unlock(&vcpu->kvm->mmu_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1265 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | static void mmu_alloc_roots(struct kvm_vcpu *vcpu) |
| 1269 | { |
| 1270 | int i; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1271 | gfn_t root_gfn; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1272 | struct kvm_mmu_page *sp; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1273 | int metaphysical = 0; |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 1274 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1275 | root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1276 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1277 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 1278 | hpa_t root = vcpu->arch.mmu.root_hpa; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1279 | |
| 1280 | ASSERT(!VALID_PAGE(root)); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1281 | if (tdp_enabled) |
| 1282 | metaphysical = 1; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1283 | sp = kvm_mmu_get_page(vcpu, root_gfn, 0, |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1284 | PT64_ROOT_LEVEL, metaphysical, |
| 1285 | ACC_ALL, NULL); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1286 | root = __pa(sp->spt); |
| 1287 | ++sp->root_count; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1288 | vcpu->arch.mmu.root_hpa = root; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1289 | return; |
| 1290 | } |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1291 | metaphysical = !is_paging(vcpu); |
| 1292 | if (tdp_enabled) |
| 1293 | metaphysical = 1; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1294 | for (i = 0; i < 4; ++i) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1295 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1296 | |
| 1297 | ASSERT(!VALID_PAGE(root)); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1298 | if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) { |
| 1299 | if (!is_present_pte(vcpu->arch.pdptrs[i])) { |
| 1300 | vcpu->arch.mmu.pae_root[i] = 0; |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 1301 | continue; |
| 1302 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1303 | root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT; |
| 1304 | } else if (vcpu->arch.mmu.root_level == 0) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1305 | root_gfn = 0; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1306 | sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1307 | PT32_ROOT_LEVEL, metaphysical, |
Avi Kivity | f7d9c7b | 2008-02-26 22:12:10 +0200 | [diff] [blame] | 1308 | ACC_ALL, NULL); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1309 | root = __pa(sp->spt); |
| 1310 | ++sp->root_count; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1311 | vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1312 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1313 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1314 | } |
| 1315 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1316 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr) |
| 1317 | { |
| 1318 | return vaddr; |
| 1319 | } |
| 1320 | |
| 1321 | static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, |
Avi Kivity | 3f3e712 | 2007-11-21 14:54:16 +0200 | [diff] [blame] | 1322 | u32 error_code) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1323 | { |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 1324 | gfn_t gfn; |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 1325 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1326 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1327 | pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code); |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 1328 | r = mmu_topup_memory_caches(vcpu); |
| 1329 | if (r) |
| 1330 | return r; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 1331 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1332 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1333 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1334 | |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 1335 | gfn = gva >> PAGE_SHIFT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1336 | |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 1337 | return nonpaging_map(vcpu, gva & PAGE_MASK, |
| 1338 | error_code & PFERR_WRITE_MASK, gfn); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1339 | } |
| 1340 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1341 | static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, |
| 1342 | u32 error_code) |
| 1343 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1344 | pfn_t pfn; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1345 | int r; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1346 | int largepage = 0; |
| 1347 | gfn_t gfn = gpa >> PAGE_SHIFT; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1348 | |
| 1349 | ASSERT(vcpu); |
| 1350 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
| 1351 | |
| 1352 | r = mmu_topup_memory_caches(vcpu); |
| 1353 | if (r) |
| 1354 | return r; |
| 1355 | |
| 1356 | down_read(¤t->mm->mmap_sem); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1357 | if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) { |
| 1358 | gfn &= ~(KVM_PAGES_PER_HPAGE-1); |
| 1359 | largepage = 1; |
| 1360 | } |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1361 | pfn = gfn_to_pfn(vcpu->kvm, gfn); |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 1362 | up_read(¤t->mm->mmap_sem); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1363 | if (is_error_pfn(pfn)) { |
| 1364 | kvm_release_pfn_clean(pfn); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1365 | return 1; |
| 1366 | } |
| 1367 | spin_lock(&vcpu->kvm->mmu_lock); |
| 1368 | kvm_mmu_free_some_pages(vcpu); |
| 1369 | r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 1370 | largepage, gfn, pfn, kvm_x86_ops->get_tdp_level()); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1371 | spin_unlock(&vcpu->kvm->mmu_lock); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1372 | |
| 1373 | return r; |
| 1374 | } |
| 1375 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1376 | static void nonpaging_free(struct kvm_vcpu *vcpu) |
| 1377 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1378 | mmu_free_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | static int nonpaging_init_context(struct kvm_vcpu *vcpu) |
| 1382 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1383 | struct kvm_mmu *context = &vcpu->arch.mmu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1384 | |
| 1385 | context->new_cr3 = nonpaging_new_cr3; |
| 1386 | context->page_fault = nonpaging_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1387 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
| 1388 | context->free = nonpaging_free; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1389 | context->prefetch_page = nonpaging_prefetch_page; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1390 | context->root_level = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1391 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1392 | context->root_hpa = INVALID_PAGE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1393 | return 0; |
| 1394 | } |
| 1395 | |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 1396 | void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1397 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1398 | ++vcpu->stat.tlb_flush; |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 1399 | kvm_x86_ops->tlb_flush(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | static void paging_new_cr3(struct kvm_vcpu *vcpu) |
| 1403 | { |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1404 | pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1405 | mmu_free_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1406 | } |
| 1407 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1408 | static void inject_page_fault(struct kvm_vcpu *vcpu, |
| 1409 | u64 addr, |
| 1410 | u32 err_code) |
| 1411 | { |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 1412 | kvm_inject_page_fault(vcpu, addr, err_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1413 | } |
| 1414 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1415 | static void paging_free(struct kvm_vcpu *vcpu) |
| 1416 | { |
| 1417 | nonpaging_free(vcpu); |
| 1418 | } |
| 1419 | |
| 1420 | #define PTTYPE 64 |
| 1421 | #include "paging_tmpl.h" |
| 1422 | #undef PTTYPE |
| 1423 | |
| 1424 | #define PTTYPE 32 |
| 1425 | #include "paging_tmpl.h" |
| 1426 | #undef PTTYPE |
| 1427 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1428 | static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1429 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1430 | struct kvm_mmu *context = &vcpu->arch.mmu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1431 | |
| 1432 | ASSERT(is_pae(vcpu)); |
| 1433 | context->new_cr3 = paging_new_cr3; |
| 1434 | context->page_fault = paging64_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1435 | context->gva_to_gpa = paging64_gva_to_gpa; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1436 | context->prefetch_page = paging64_prefetch_page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1437 | context->free = paging_free; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1438 | context->root_level = level; |
| 1439 | context->shadow_root_level = level; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1440 | context->root_hpa = INVALID_PAGE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1441 | return 0; |
| 1442 | } |
| 1443 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1444 | static int paging64_init_context(struct kvm_vcpu *vcpu) |
| 1445 | { |
| 1446 | return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL); |
| 1447 | } |
| 1448 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1449 | static int paging32_init_context(struct kvm_vcpu *vcpu) |
| 1450 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1451 | struct kvm_mmu *context = &vcpu->arch.mmu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1452 | |
| 1453 | context->new_cr3 = paging_new_cr3; |
| 1454 | context->page_fault = paging32_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1455 | context->gva_to_gpa = paging32_gva_to_gpa; |
| 1456 | context->free = paging_free; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1457 | context->prefetch_page = paging32_prefetch_page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1458 | context->root_level = PT32_ROOT_LEVEL; |
| 1459 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1460 | context->root_hpa = INVALID_PAGE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | static int paging32E_init_context(struct kvm_vcpu *vcpu) |
| 1465 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1466 | return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1467 | } |
| 1468 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1469 | static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) |
| 1470 | { |
| 1471 | struct kvm_mmu *context = &vcpu->arch.mmu; |
| 1472 | |
| 1473 | context->new_cr3 = nonpaging_new_cr3; |
| 1474 | context->page_fault = tdp_page_fault; |
| 1475 | context->free = nonpaging_free; |
| 1476 | context->prefetch_page = nonpaging_prefetch_page; |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 1477 | context->shadow_root_level = kvm_x86_ops->get_tdp_level(); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1478 | context->root_hpa = INVALID_PAGE; |
| 1479 | |
| 1480 | if (!is_paging(vcpu)) { |
| 1481 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
| 1482 | context->root_level = 0; |
| 1483 | } else if (is_long_mode(vcpu)) { |
| 1484 | context->gva_to_gpa = paging64_gva_to_gpa; |
| 1485 | context->root_level = PT64_ROOT_LEVEL; |
| 1486 | } else if (is_pae(vcpu)) { |
| 1487 | context->gva_to_gpa = paging64_gva_to_gpa; |
| 1488 | context->root_level = PT32E_ROOT_LEVEL; |
| 1489 | } else { |
| 1490 | context->gva_to_gpa = paging32_gva_to_gpa; |
| 1491 | context->root_level = PT32_ROOT_LEVEL; |
| 1492 | } |
| 1493 | |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | static int init_kvm_softmmu(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1498 | { |
| 1499 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1500 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1501 | |
| 1502 | if (!is_paging(vcpu)) |
| 1503 | return nonpaging_init_context(vcpu); |
Avi Kivity | a9058ec | 2006-12-29 16:49:37 -0800 | [diff] [blame] | 1504 | else if (is_long_mode(vcpu)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1505 | return paging64_init_context(vcpu); |
| 1506 | else if (is_pae(vcpu)) |
| 1507 | return paging32E_init_context(vcpu); |
| 1508 | else |
| 1509 | return paging32_init_context(vcpu); |
| 1510 | } |
| 1511 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1512 | static int init_kvm_mmu(struct kvm_vcpu *vcpu) |
| 1513 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1514 | vcpu->arch.update_pte.pfn = bad_pfn; |
| 1515 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 1516 | if (tdp_enabled) |
| 1517 | return init_kvm_tdp_mmu(vcpu); |
| 1518 | else |
| 1519 | return init_kvm_softmmu(vcpu); |
| 1520 | } |
| 1521 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1522 | static void destroy_kvm_mmu(struct kvm_vcpu *vcpu) |
| 1523 | { |
| 1524 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1525 | if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) { |
| 1526 | vcpu->arch.mmu.free(vcpu); |
| 1527 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | int kvm_mmu_reset_context(struct kvm_vcpu *vcpu) |
| 1532 | { |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1533 | destroy_kvm_mmu(vcpu); |
| 1534 | return init_kvm_mmu(vcpu); |
| 1535 | } |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 1536 | EXPORT_SYMBOL_GPL(kvm_mmu_reset_context); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1537 | |
| 1538 | int kvm_mmu_load(struct kvm_vcpu *vcpu) |
| 1539 | { |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 1540 | int r; |
| 1541 | |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 1542 | r = mmu_topup_memory_caches(vcpu); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1543 | if (r) |
| 1544 | goto out; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1545 | spin_lock(&vcpu->kvm->mmu_lock); |
Avi Kivity | eb787d1 | 2007-12-31 15:27:49 +0200 | [diff] [blame] | 1546 | kvm_mmu_free_some_pages(vcpu); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1547 | mmu_alloc_roots(vcpu); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1548 | spin_unlock(&vcpu->kvm->mmu_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1549 | kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1550 | kvm_mmu_flush_tlb(vcpu); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 1551 | out: |
| 1552 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1553 | } |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 1554 | EXPORT_SYMBOL_GPL(kvm_mmu_load); |
| 1555 | |
| 1556 | void kvm_mmu_unload(struct kvm_vcpu *vcpu) |
| 1557 | { |
| 1558 | mmu_free_roots(vcpu); |
| 1559 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1560 | |
Avi Kivity | 09072da | 2007-05-01 14:16:52 +0300 | [diff] [blame] | 1561 | static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1562 | struct kvm_mmu_page *sp, |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1563 | u64 *spte) |
| 1564 | { |
| 1565 | u64 pte; |
| 1566 | struct kvm_mmu_page *child; |
| 1567 | |
| 1568 | pte = *spte; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1569 | if (is_shadow_present_pte(pte)) { |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1570 | if (sp->role.level == PT_PAGE_TABLE_LEVEL || |
| 1571 | is_large_pte(pte)) |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 1572 | rmap_remove(vcpu->kvm, spte); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1573 | else { |
| 1574 | child = page_header(pte & PT64_BASE_ADDR_MASK); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1575 | mmu_page_remove_parent_pte(child, spte); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1576 | } |
| 1577 | } |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1578 | set_shadow_pte(spte, shadow_trap_nonpresent_pte); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1579 | if (is_large_pte(pte)) |
| 1580 | --vcpu->kvm->stat.lpages; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1581 | } |
| 1582 | |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 1583 | static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1584 | struct kvm_mmu_page *sp, |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 1585 | u64 *spte, |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 1586 | const void *new) |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 1587 | { |
Marcelo Tosatti | 3094538 | 2008-06-11 20:32:40 -0300 | [diff] [blame] | 1588 | if (sp->role.level != PT_PAGE_TABLE_LEVEL) { |
| 1589 | if (!vcpu->arch.update_pte.largepage || |
| 1590 | sp->role.glevels == PT32_ROOT_LEVEL) { |
| 1591 | ++vcpu->kvm->stat.mmu_pde_zapped; |
| 1592 | return; |
| 1593 | } |
| 1594 | } |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 1595 | |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 1596 | ++vcpu->kvm->stat.mmu_pte_updated; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1597 | if (sp->role.glevels == PT32_ROOT_LEVEL) |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 1598 | paging32_update_pte(vcpu, sp, spte, new); |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 1599 | else |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 1600 | paging64_update_pte(vcpu, sp, spte, new); |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 1601 | } |
| 1602 | |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 1603 | static bool need_remote_flush(u64 old, u64 new) |
| 1604 | { |
| 1605 | if (!is_shadow_present_pte(old)) |
| 1606 | return false; |
| 1607 | if (!is_shadow_present_pte(new)) |
| 1608 | return true; |
| 1609 | if ((old ^ new) & PT64_BASE_ADDR_MASK) |
| 1610 | return true; |
| 1611 | old ^= PT64_NX_MASK; |
| 1612 | new ^= PT64_NX_MASK; |
| 1613 | return (old & ~new & PT64_PERM_MASK) != 0; |
| 1614 | } |
| 1615 | |
| 1616 | static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new) |
| 1617 | { |
| 1618 | if (need_remote_flush(old, new)) |
| 1619 | kvm_flush_remote_tlbs(vcpu->kvm); |
| 1620 | else |
| 1621 | kvm_mmu_flush_tlb(vcpu); |
| 1622 | } |
| 1623 | |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 1624 | static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu) |
| 1625 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1626 | u64 *spte = vcpu->arch.last_pte_updated; |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 1627 | |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 1628 | return !!(spte && (*spte & shadow_accessed_mask)); |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 1629 | } |
| 1630 | |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 1631 | static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 1632 | const u8 *new, int bytes) |
| 1633 | { |
| 1634 | gfn_t gfn; |
| 1635 | int r; |
| 1636 | u64 gpte = 0; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1637 | pfn_t pfn; |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 1638 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1639 | vcpu->arch.update_pte.largepage = 0; |
| 1640 | |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 1641 | if (bytes != 4 && bytes != 8) |
| 1642 | return; |
| 1643 | |
| 1644 | /* |
| 1645 | * Assume that the pte write on a page table of the same type |
| 1646 | * as the current vcpu paging mode. This is nearly always true |
| 1647 | * (might be false while changing modes). Note it is verified later |
| 1648 | * by update_pte(). |
| 1649 | */ |
| 1650 | if (is_pae(vcpu)) { |
| 1651 | /* Handle a 32-bit guest writing two halves of a 64-bit gpte */ |
| 1652 | if ((bytes == 4) && (gpa % 4 == 0)) { |
| 1653 | r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8); |
| 1654 | if (r) |
| 1655 | return; |
| 1656 | memcpy((void *)&gpte + (gpa % 8), new, 4); |
| 1657 | } else if ((bytes == 8) && (gpa % 8 == 0)) { |
| 1658 | memcpy((void *)&gpte, new, 8); |
| 1659 | } |
| 1660 | } else { |
| 1661 | if ((bytes == 4) && (gpa % 4 == 0)) |
| 1662 | memcpy((void *)&gpte, new, 4); |
| 1663 | } |
| 1664 | if (!is_present_pte(gpte)) |
| 1665 | return; |
| 1666 | gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1667 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1668 | down_read(¤t->mm->mmap_sem); |
| 1669 | if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) { |
| 1670 | gfn &= ~(KVM_PAGES_PER_HPAGE-1); |
| 1671 | vcpu->arch.update_pte.largepage = 1; |
| 1672 | } |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1673 | pfn = gfn_to_pfn(vcpu->kvm, gfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1674 | up_read(¤t->mm->mmap_sem); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1675 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1676 | if (is_error_pfn(pfn)) { |
| 1677 | kvm_release_pfn_clean(pfn); |
Avi Kivity | d196e34 | 2008-01-24 11:44:11 +0200 | [diff] [blame] | 1678 | return; |
| 1679 | } |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 1680 | vcpu->arch.update_pte.gfn = gfn; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1681 | vcpu->arch.update_pte.pfn = pfn; |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 1682 | } |
| 1683 | |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 1684 | static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 1685 | { |
| 1686 | u64 *spte = vcpu->arch.last_pte_updated; |
| 1687 | |
| 1688 | if (spte |
| 1689 | && vcpu->arch.last_pte_gfn == gfn |
| 1690 | && shadow_accessed_mask |
| 1691 | && !(*spte & shadow_accessed_mask) |
| 1692 | && is_shadow_present_pte(*spte)) |
| 1693 | set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte); |
| 1694 | } |
| 1695 | |
Avi Kivity | 09072da | 2007-05-01 14:16:52 +0300 | [diff] [blame] | 1696 | void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, |
Shaohua Li | fe55188 | 2007-07-23 14:51:39 +0800 | [diff] [blame] | 1697 | const u8 *new, int bytes) |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1698 | { |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1699 | gfn_t gfn = gpa >> PAGE_SHIFT; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1700 | struct kvm_mmu_page *sp; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1701 | struct hlist_node *node, *n; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1702 | struct hlist_head *bucket; |
| 1703 | unsigned index; |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 1704 | u64 entry, gentry; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1705 | u64 *spte; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1706 | unsigned offset = offset_in_page(gpa); |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1707 | unsigned pte_size; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1708 | unsigned page_offset; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1709 | unsigned misaligned; |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 1710 | unsigned quadrant; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1711 | int level; |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1712 | int flooded = 0; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1713 | int npte; |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 1714 | int r; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1715 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1716 | pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes); |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 1717 | mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1718 | spin_lock(&vcpu->kvm->mmu_lock); |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 1719 | kvm_mmu_access_page(vcpu, gfn); |
Avi Kivity | eb787d1 | 2007-12-31 15:27:49 +0200 | [diff] [blame] | 1720 | kvm_mmu_free_some_pages(vcpu); |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 1721 | ++vcpu->kvm->stat.mmu_pte_write; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1722 | kvm_mmu_audit(vcpu, "pre pte write"); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1723 | if (gfn == vcpu->arch.last_pt_write_gfn |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 1724 | && !last_updated_pte_accessed(vcpu)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1725 | ++vcpu->arch.last_pt_write_count; |
| 1726 | if (vcpu->arch.last_pt_write_count >= 3) |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1727 | flooded = 1; |
| 1728 | } else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1729 | vcpu->arch.last_pt_write_gfn = gfn; |
| 1730 | vcpu->arch.last_pt_write_count = 1; |
| 1731 | vcpu->arch.last_pte_updated = NULL; |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1732 | } |
Dong, Eddie | 1ae0a13 | 2008-01-07 13:20:25 +0200 | [diff] [blame] | 1733 | index = kvm_page_table_hashfn(gfn); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1734 | bucket = &vcpu->kvm->arch.mmu_page_hash[index]; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1735 | hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) { |
| 1736 | if (sp->gfn != gfn || sp->role.metaphysical) |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1737 | continue; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1738 | pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1739 | misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); |
Avi Kivity | e925c5b | 2007-04-30 14:47:02 +0300 | [diff] [blame] | 1740 | misaligned |= bytes < 4; |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1741 | if (misaligned || flooded) { |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1742 | /* |
| 1743 | * Misaligned accesses are too much trouble to fix |
| 1744 | * up; also, they usually indicate a page is not used |
| 1745 | * as a page table. |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1746 | * |
| 1747 | * If we're seeing too many writes to a page, |
| 1748 | * it may no longer be a page table, or we may be |
| 1749 | * forking, in which case it is better to unmap the |
| 1750 | * page. |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1751 | */ |
| 1752 | pgprintk("misaligned: gpa %llx bytes %d role %x\n", |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1753 | gpa, bytes, sp->role.word); |
| 1754 | kvm_mmu_zap_page(vcpu->kvm, sp); |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 1755 | ++vcpu->kvm->stat.mmu_flooded; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 1756 | continue; |
| 1757 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1758 | page_offset = offset; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1759 | level = sp->role.level; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1760 | npte = 1; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1761 | if (sp->role.glevels == PT32_ROOT_LEVEL) { |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1762 | page_offset <<= 1; /* 32->64 */ |
| 1763 | /* |
| 1764 | * A 32-bit pde maps 4MB while the shadow pdes map |
| 1765 | * only 2MB. So we need to double the offset again |
| 1766 | * and zap two pdes instead of one. |
| 1767 | */ |
| 1768 | if (level == PT32_ROOT_LEVEL) { |
Avi Kivity | 6b8d0f9 | 2007-04-18 11:18:18 +0300 | [diff] [blame] | 1769 | page_offset &= ~7; /* kill rounding error */ |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1770 | page_offset <<= 1; |
| 1771 | npte = 2; |
| 1772 | } |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 1773 | quadrant = page_offset >> PAGE_SHIFT; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1774 | page_offset &= ~PAGE_MASK; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1775 | if (quadrant != sp->role.quadrant) |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 1776 | continue; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1777 | } |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1778 | spte = &sp->spt[page_offset / sizeof(*spte)]; |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 1779 | if ((gpa & (pte_size - 1)) || (bytes < pte_size)) { |
| 1780 | gentry = 0; |
| 1781 | r = kvm_read_guest_atomic(vcpu->kvm, |
| 1782 | gpa & ~(u64)(pte_size - 1), |
| 1783 | &gentry, pte_size); |
| 1784 | new = (const void *)&gentry; |
| 1785 | if (r < 0) |
| 1786 | new = NULL; |
| 1787 | } |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1788 | while (npte--) { |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 1789 | entry = *spte; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1790 | mmu_pte_write_zap_pte(vcpu, sp, spte); |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 1791 | if (new) |
| 1792 | mmu_pte_write_new_pte(vcpu, sp, spte, new); |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 1793 | mmu_pte_write_flush_tlb(vcpu, entry, *spte); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 1794 | ++spte; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1795 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1796 | } |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1797 | kvm_mmu_audit(vcpu, "post pte write"); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1798 | spin_unlock(&vcpu->kvm->mmu_lock); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1799 | if (!is_error_pfn(vcpu->arch.update_pte.pfn)) { |
| 1800 | kvm_release_pfn_clean(vcpu->arch.update_pte.pfn); |
| 1801 | vcpu->arch.update_pte.pfn = bad_pfn; |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 1802 | } |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1803 | } |
| 1804 | |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1805 | int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva) |
| 1806 | { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1807 | gpa_t gpa; |
| 1808 | int r; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1809 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1810 | gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1811 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1812 | spin_lock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1813 | r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1814 | spin_unlock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1815 | return r; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1816 | } |
| 1817 | |
Avi Kivity | 22d95b1 | 2007-09-14 20:26:06 +0300 | [diff] [blame] | 1818 | void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu) |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 1819 | { |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1820 | while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1821 | struct kvm_mmu_page *sp; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 1822 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1823 | sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1824 | struct kvm_mmu_page, link); |
| 1825 | kvm_mmu_zap_page(vcpu->kvm, sp); |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 1826 | ++vcpu->kvm->stat.mmu_recycled; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 1827 | } |
| 1828 | } |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 1829 | |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 1830 | int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code) |
| 1831 | { |
| 1832 | int r; |
| 1833 | enum emulation_result er; |
| 1834 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1835 | r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 1836 | if (r < 0) |
| 1837 | goto out; |
| 1838 | |
| 1839 | if (!r) { |
| 1840 | r = 1; |
| 1841 | goto out; |
| 1842 | } |
| 1843 | |
Avi Kivity | b733bfb | 2007-10-28 18:52:05 +0200 | [diff] [blame] | 1844 | r = mmu_topup_memory_caches(vcpu); |
| 1845 | if (r) |
| 1846 | goto out; |
| 1847 | |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 1848 | er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 1849 | |
| 1850 | switch (er) { |
| 1851 | case EMULATE_DONE: |
| 1852 | return 1; |
| 1853 | case EMULATE_DO_MMIO: |
| 1854 | ++vcpu->stat.mmio_exits; |
| 1855 | return 0; |
| 1856 | case EMULATE_FAIL: |
| 1857 | kvm_report_emulation_failure(vcpu, "pagetable"); |
| 1858 | return 1; |
| 1859 | default: |
| 1860 | BUG(); |
| 1861 | } |
| 1862 | out: |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 1863 | return r; |
| 1864 | } |
| 1865 | EXPORT_SYMBOL_GPL(kvm_mmu_page_fault); |
| 1866 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 1867 | void kvm_enable_tdp(void) |
| 1868 | { |
| 1869 | tdp_enabled = true; |
| 1870 | } |
| 1871 | EXPORT_SYMBOL_GPL(kvm_enable_tdp); |
| 1872 | |
Joerg Roedel | 5f4cb66 | 2008-07-14 20:36:36 +0200 | [diff] [blame^] | 1873 | void kvm_disable_tdp(void) |
| 1874 | { |
| 1875 | tdp_enabled = false; |
| 1876 | } |
| 1877 | EXPORT_SYMBOL_GPL(kvm_disable_tdp); |
| 1878 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1879 | static void free_mmu_pages(struct kvm_vcpu *vcpu) |
| 1880 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1881 | struct kvm_mmu_page *sp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1882 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1883 | while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) { |
| 1884 | sp = container_of(vcpu->kvm->arch.active_mmu_pages.next, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1885 | struct kvm_mmu_page, link); |
| 1886 | kvm_mmu_zap_page(vcpu->kvm, sp); |
Avi Kivity | 8d2d73b | 2008-06-04 18:42:24 +0300 | [diff] [blame] | 1887 | cond_resched(); |
Avi Kivity | f51234c | 2007-01-05 16:36:52 -0800 | [diff] [blame] | 1888 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1889 | free_page((unsigned long)vcpu->arch.mmu.pae_root); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | static int alloc_mmu_pages(struct kvm_vcpu *vcpu) |
| 1893 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1894 | struct page *page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1895 | int i; |
| 1896 | |
| 1897 | ASSERT(vcpu); |
| 1898 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1899 | if (vcpu->kvm->arch.n_requested_mmu_pages) |
| 1900 | vcpu->kvm->arch.n_free_mmu_pages = |
| 1901 | vcpu->kvm->arch.n_requested_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1902 | else |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1903 | vcpu->kvm->arch.n_free_mmu_pages = |
| 1904 | vcpu->kvm->arch.n_alloc_mmu_pages; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1905 | /* |
| 1906 | * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64. |
| 1907 | * Therefore we need to allocate shadow page tables in the first |
| 1908 | * 4GB of memory, which happens to fit the DMA32 zone. |
| 1909 | */ |
| 1910 | page = alloc_page(GFP_KERNEL | __GFP_DMA32); |
| 1911 | if (!page) |
| 1912 | goto error_1; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1913 | vcpu->arch.mmu.pae_root = page_address(page); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1914 | for (i = 0; i < 4; ++i) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1915 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1916 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1917 | return 0; |
| 1918 | |
| 1919 | error_1: |
| 1920 | free_mmu_pages(vcpu); |
| 1921 | return -ENOMEM; |
| 1922 | } |
| 1923 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1924 | int kvm_mmu_create(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1925 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1926 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1927 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1928 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1929 | return alloc_mmu_pages(vcpu); |
| 1930 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1931 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1932 | int kvm_mmu_setup(struct kvm_vcpu *vcpu) |
| 1933 | { |
| 1934 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1935 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 2c26495 | 2006-12-22 01:05:28 -0800 | [diff] [blame] | 1936 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1937 | return init_kvm_mmu(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | void kvm_mmu_destroy(struct kvm_vcpu *vcpu) |
| 1941 | { |
| 1942 | ASSERT(vcpu); |
| 1943 | |
| 1944 | destroy_kvm_mmu(vcpu); |
| 1945 | free_mmu_pages(vcpu); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 1946 | mmu_free_memory_caches(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1947 | } |
| 1948 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1949 | void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1950 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1951 | struct kvm_mmu_page *sp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1952 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1953 | list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1954 | int i; |
| 1955 | u64 *pt; |
| 1956 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1957 | if (!test_bit(slot, &sp->slot_bitmap)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1958 | continue; |
| 1959 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1960 | pt = sp->spt; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1961 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
| 1962 | /* avoid RMW */ |
Izik Eidus | 9647c14 | 2007-10-16 14:43:46 +0200 | [diff] [blame] | 1963 | if (pt[i] & PT_WRITABLE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1964 | pt[i] &= ~PT_WRITABLE_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1965 | } |
| 1966 | } |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1967 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1968 | void kvm_mmu_zap_all(struct kvm *kvm) |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 1969 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1970 | struct kvm_mmu_page *sp, *node; |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 1971 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1972 | spin_lock(&kvm->mmu_lock); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1973 | list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1974 | kvm_mmu_zap_page(kvm, sp); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 1975 | spin_unlock(&kvm->mmu_lock); |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 1976 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1977 | kvm_flush_remote_tlbs(kvm); |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 1978 | } |
| 1979 | |
Harvey Harrison | 8b2cf73 | 2008-04-27 12:14:13 -0700 | [diff] [blame] | 1980 | static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm) |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 1981 | { |
| 1982 | struct kvm_mmu_page *page; |
| 1983 | |
| 1984 | page = container_of(kvm->arch.active_mmu_pages.prev, |
| 1985 | struct kvm_mmu_page, link); |
| 1986 | kvm_mmu_zap_page(kvm, page); |
| 1987 | } |
| 1988 | |
| 1989 | static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask) |
| 1990 | { |
| 1991 | struct kvm *kvm; |
| 1992 | struct kvm *kvm_freed = NULL; |
| 1993 | int cache_count = 0; |
| 1994 | |
| 1995 | spin_lock(&kvm_lock); |
| 1996 | |
| 1997 | list_for_each_entry(kvm, &vm_list, vm_list) { |
| 1998 | int npages; |
| 1999 | |
Marcelo Tosatti | 5a4c928 | 2008-07-03 18:33:02 -0300 | [diff] [blame] | 2000 | if (!down_read_trylock(&kvm->slots_lock)) |
| 2001 | continue; |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 2002 | spin_lock(&kvm->mmu_lock); |
| 2003 | npages = kvm->arch.n_alloc_mmu_pages - |
| 2004 | kvm->arch.n_free_mmu_pages; |
| 2005 | cache_count += npages; |
| 2006 | if (!kvm_freed && nr_to_scan > 0 && npages > 0) { |
| 2007 | kvm_mmu_remove_one_alloc_mmu_page(kvm); |
| 2008 | cache_count--; |
| 2009 | kvm_freed = kvm; |
| 2010 | } |
| 2011 | nr_to_scan--; |
| 2012 | |
| 2013 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | 5a4c928 | 2008-07-03 18:33:02 -0300 | [diff] [blame] | 2014 | up_read(&kvm->slots_lock); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 2015 | } |
| 2016 | if (kvm_freed) |
| 2017 | list_move_tail(&kvm_freed->vm_list, &vm_list); |
| 2018 | |
| 2019 | spin_unlock(&kvm_lock); |
| 2020 | |
| 2021 | return cache_count; |
| 2022 | } |
| 2023 | |
| 2024 | static struct shrinker mmu_shrinker = { |
| 2025 | .shrink = mmu_shrink, |
| 2026 | .seeks = DEFAULT_SEEKS * 10, |
| 2027 | }; |
| 2028 | |
Ingo Molnar | 2ddfd20 | 2008-05-22 10:37:48 +0200 | [diff] [blame] | 2029 | static void mmu_destroy_caches(void) |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2030 | { |
| 2031 | if (pte_chain_cache) |
| 2032 | kmem_cache_destroy(pte_chain_cache); |
| 2033 | if (rmap_desc_cache) |
| 2034 | kmem_cache_destroy(rmap_desc_cache); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 2035 | if (mmu_page_header_cache) |
| 2036 | kmem_cache_destroy(mmu_page_header_cache); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2037 | } |
| 2038 | |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 2039 | void kvm_mmu_module_exit(void) |
| 2040 | { |
| 2041 | mmu_destroy_caches(); |
| 2042 | unregister_shrinker(&mmu_shrinker); |
| 2043 | } |
| 2044 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2045 | int kvm_mmu_module_init(void) |
| 2046 | { |
| 2047 | pte_chain_cache = kmem_cache_create("kvm_pte_chain", |
| 2048 | sizeof(struct kvm_pte_chain), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2049 | 0, 0, NULL); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2050 | if (!pte_chain_cache) |
| 2051 | goto nomem; |
| 2052 | rmap_desc_cache = kmem_cache_create("kvm_rmap_desc", |
| 2053 | sizeof(struct kvm_rmap_desc), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2054 | 0, 0, NULL); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2055 | if (!rmap_desc_cache) |
| 2056 | goto nomem; |
| 2057 | |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 2058 | mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header", |
| 2059 | sizeof(struct kvm_mmu_page), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2060 | 0, 0, NULL); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 2061 | if (!mmu_page_header_cache) |
| 2062 | goto nomem; |
| 2063 | |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 2064 | register_shrinker(&mmu_shrinker); |
| 2065 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2066 | return 0; |
| 2067 | |
| 2068 | nomem: |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 2069 | mmu_destroy_caches(); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 2070 | return -ENOMEM; |
| 2071 | } |
| 2072 | |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 2073 | /* |
| 2074 | * Caculate mmu pages needed for kvm. |
| 2075 | */ |
| 2076 | unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm) |
| 2077 | { |
| 2078 | int i; |
| 2079 | unsigned int nr_mmu_pages; |
| 2080 | unsigned int nr_pages = 0; |
| 2081 | |
| 2082 | for (i = 0; i < kvm->nmemslots; i++) |
| 2083 | nr_pages += kvm->memslots[i].npages; |
| 2084 | |
| 2085 | nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000; |
| 2086 | nr_mmu_pages = max(nr_mmu_pages, |
| 2087 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); |
| 2088 | |
| 2089 | return nr_mmu_pages; |
| 2090 | } |
| 2091 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2092 | static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer, |
| 2093 | unsigned len) |
| 2094 | { |
| 2095 | if (len > buffer->len) |
| 2096 | return NULL; |
| 2097 | return buffer->ptr; |
| 2098 | } |
| 2099 | |
| 2100 | static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer, |
| 2101 | unsigned len) |
| 2102 | { |
| 2103 | void *ret; |
| 2104 | |
| 2105 | ret = pv_mmu_peek_buffer(buffer, len); |
| 2106 | if (!ret) |
| 2107 | return ret; |
| 2108 | buffer->ptr += len; |
| 2109 | buffer->len -= len; |
| 2110 | buffer->processed += len; |
| 2111 | return ret; |
| 2112 | } |
| 2113 | |
| 2114 | static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu, |
| 2115 | gpa_t addr, gpa_t value) |
| 2116 | { |
| 2117 | int bytes = 8; |
| 2118 | int r; |
| 2119 | |
| 2120 | if (!is_long_mode(vcpu) && !is_pae(vcpu)) |
| 2121 | bytes = 4; |
| 2122 | |
| 2123 | r = mmu_topup_memory_caches(vcpu); |
| 2124 | if (r) |
| 2125 | return r; |
| 2126 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 2127 | if (!emulator_write_phys(vcpu, addr, &value, bytes)) |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2128 | return -EFAULT; |
| 2129 | |
| 2130 | return 1; |
| 2131 | } |
| 2132 | |
| 2133 | static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
| 2134 | { |
| 2135 | kvm_x86_ops->tlb_flush(vcpu); |
| 2136 | return 1; |
| 2137 | } |
| 2138 | |
| 2139 | static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr) |
| 2140 | { |
| 2141 | spin_lock(&vcpu->kvm->mmu_lock); |
| 2142 | mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT); |
| 2143 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2144 | return 1; |
| 2145 | } |
| 2146 | |
| 2147 | static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu, |
| 2148 | struct kvm_pv_mmu_op_buffer *buffer) |
| 2149 | { |
| 2150 | struct kvm_mmu_op_header *header; |
| 2151 | |
| 2152 | header = pv_mmu_peek_buffer(buffer, sizeof *header); |
| 2153 | if (!header) |
| 2154 | return 0; |
| 2155 | switch (header->op) { |
| 2156 | case KVM_MMU_OP_WRITE_PTE: { |
| 2157 | struct kvm_mmu_op_write_pte *wpte; |
| 2158 | |
| 2159 | wpte = pv_mmu_read_buffer(buffer, sizeof *wpte); |
| 2160 | if (!wpte) |
| 2161 | return 0; |
| 2162 | return kvm_pv_mmu_write(vcpu, wpte->pte_phys, |
| 2163 | wpte->pte_val); |
| 2164 | } |
| 2165 | case KVM_MMU_OP_FLUSH_TLB: { |
| 2166 | struct kvm_mmu_op_flush_tlb *ftlb; |
| 2167 | |
| 2168 | ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb); |
| 2169 | if (!ftlb) |
| 2170 | return 0; |
| 2171 | return kvm_pv_mmu_flush_tlb(vcpu); |
| 2172 | } |
| 2173 | case KVM_MMU_OP_RELEASE_PT: { |
| 2174 | struct kvm_mmu_op_release_pt *rpt; |
| 2175 | |
| 2176 | rpt = pv_mmu_read_buffer(buffer, sizeof *rpt); |
| 2177 | if (!rpt) |
| 2178 | return 0; |
| 2179 | return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys); |
| 2180 | } |
| 2181 | default: return 0; |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes, |
| 2186 | gpa_t addr, unsigned long *ret) |
| 2187 | { |
| 2188 | int r; |
| 2189 | struct kvm_pv_mmu_op_buffer buffer; |
| 2190 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2191 | buffer.ptr = buffer.buf; |
| 2192 | buffer.len = min_t(unsigned long, bytes, sizeof buffer.buf); |
| 2193 | buffer.processed = 0; |
| 2194 | |
| 2195 | r = kvm_read_guest(vcpu->kvm, addr, buffer.buf, buffer.len); |
| 2196 | if (r) |
| 2197 | goto out; |
| 2198 | |
| 2199 | while (buffer.len) { |
| 2200 | r = kvm_pv_mmu_op_one(vcpu, &buffer); |
| 2201 | if (r < 0) |
| 2202 | goto out; |
| 2203 | if (r == 0) |
| 2204 | break; |
| 2205 | } |
| 2206 | |
| 2207 | r = 1; |
| 2208 | out: |
| 2209 | *ret = buffer.processed; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 2210 | return r; |
| 2211 | } |
| 2212 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2213 | #ifdef AUDIT |
| 2214 | |
| 2215 | static const char *audit_msg; |
| 2216 | |
| 2217 | static gva_t canonicalize(gva_t gva) |
| 2218 | { |
| 2219 | #ifdef CONFIG_X86_64 |
| 2220 | gva = (long long)(gva << 16) >> 16; |
| 2221 | #endif |
| 2222 | return gva; |
| 2223 | } |
| 2224 | |
| 2225 | static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte, |
| 2226 | gva_t va, int level) |
| 2227 | { |
| 2228 | u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK); |
| 2229 | int i; |
| 2230 | gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1)); |
| 2231 | |
| 2232 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) { |
| 2233 | u64 ent = pt[i]; |
| 2234 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2235 | if (ent == shadow_trap_nonpresent_pte) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2236 | continue; |
| 2237 | |
| 2238 | va = canonicalize(va); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2239 | if (level > 1) { |
| 2240 | if (ent == shadow_notrap_nonpresent_pte) |
| 2241 | printk(KERN_ERR "audit: (%s) nontrapping pte" |
| 2242 | " in nonleaf level: levels %d gva %lx" |
| 2243 | " level %d pte %llx\n", audit_msg, |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2244 | vcpu->arch.mmu.root_level, va, level, ent); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2245 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2246 | audit_mappings_page(vcpu, ent, va, level - 1); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2247 | } else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2248 | gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2249 | hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2250 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2251 | if (is_shadow_present_pte(ent) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2252 | && (ent & PT64_BASE_ADDR_MASK) != hpa) |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2253 | printk(KERN_ERR "xx audit error: (%s) levels %d" |
| 2254 | " gva %lx gpa %llx hpa %llx ent %llx %d\n", |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2255 | audit_msg, vcpu->arch.mmu.root_level, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2256 | va, gpa, hpa, ent, |
| 2257 | is_shadow_present_pte(ent)); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2258 | else if (ent == shadow_notrap_nonpresent_pte |
| 2259 | && !is_error_hpa(hpa)) |
| 2260 | printk(KERN_ERR "audit: (%s) notrap shadow," |
| 2261 | " valid guest gva %lx\n", audit_msg, va); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2262 | kvm_release_pfn_clean(pfn); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2263 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | static void audit_mappings(struct kvm_vcpu *vcpu) |
| 2269 | { |
Avi Kivity | 1ea252a | 2007-03-08 11:48:09 +0200 | [diff] [blame] | 2270 | unsigned i; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2271 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2272 | if (vcpu->arch.mmu.root_level == 4) |
| 2273 | audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2274 | else |
| 2275 | for (i = 0; i < 4; ++i) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2276 | if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2277 | audit_mappings_page(vcpu, |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2278 | vcpu->arch.mmu.pae_root[i], |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2279 | i << 30, |
| 2280 | 2); |
| 2281 | } |
| 2282 | |
| 2283 | static int count_rmaps(struct kvm_vcpu *vcpu) |
| 2284 | { |
| 2285 | int nmaps = 0; |
| 2286 | int i, j, k; |
| 2287 | |
| 2288 | for (i = 0; i < KVM_MEMORY_SLOTS; ++i) { |
| 2289 | struct kvm_memory_slot *m = &vcpu->kvm->memslots[i]; |
| 2290 | struct kvm_rmap_desc *d; |
| 2291 | |
| 2292 | for (j = 0; j < m->npages; ++j) { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 2293 | unsigned long *rmapp = &m->rmap[j]; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2294 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 2295 | if (!*rmapp) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2296 | continue; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 2297 | if (!(*rmapp & 1)) { |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2298 | ++nmaps; |
| 2299 | continue; |
| 2300 | } |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 2301 | d = (struct kvm_rmap_desc *)(*rmapp & ~1ul); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2302 | while (d) { |
| 2303 | for (k = 0; k < RMAP_EXT; ++k) |
| 2304 | if (d->shadow_ptes[k]) |
| 2305 | ++nmaps; |
| 2306 | else |
| 2307 | break; |
| 2308 | d = d->more; |
| 2309 | } |
| 2310 | } |
| 2311 | } |
| 2312 | return nmaps; |
| 2313 | } |
| 2314 | |
| 2315 | static int count_writable_mappings(struct kvm_vcpu *vcpu) |
| 2316 | { |
| 2317 | int nmaps = 0; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2318 | struct kvm_mmu_page *sp; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2319 | int i; |
| 2320 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2321 | list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2322 | u64 *pt = sp->spt; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2323 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2324 | if (sp->role.level != PT_PAGE_TABLE_LEVEL) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2325 | continue; |
| 2326 | |
| 2327 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 2328 | u64 ent = pt[i]; |
| 2329 | |
| 2330 | if (!(ent & PT_PRESENT_MASK)) |
| 2331 | continue; |
| 2332 | if (!(ent & PT_WRITABLE_MASK)) |
| 2333 | continue; |
| 2334 | ++nmaps; |
| 2335 | } |
| 2336 | } |
| 2337 | return nmaps; |
| 2338 | } |
| 2339 | |
| 2340 | static void audit_rmap(struct kvm_vcpu *vcpu) |
| 2341 | { |
| 2342 | int n_rmap = count_rmaps(vcpu); |
| 2343 | int n_actual = count_writable_mappings(vcpu); |
| 2344 | |
| 2345 | if (n_rmap != n_actual) |
| 2346 | printk(KERN_ERR "%s: (%s) rmap %d actual %d\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2347 | __func__, audit_msg, n_rmap, n_actual); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | static void audit_write_protection(struct kvm_vcpu *vcpu) |
| 2351 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2352 | struct kvm_mmu_page *sp; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 2353 | struct kvm_memory_slot *slot; |
| 2354 | unsigned long *rmapp; |
| 2355 | gfn_t gfn; |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2356 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2357 | list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2358 | if (sp->role.metaphysical) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2359 | continue; |
| 2360 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2361 | slot = gfn_to_memslot(vcpu->kvm, sp->gfn); |
| 2362 | gfn = unalias_gfn(vcpu->kvm, sp->gfn); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 2363 | rmapp = &slot->rmap[gfn - slot->base_gfn]; |
| 2364 | if (*rmapp) |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2365 | printk(KERN_ERR "%s: (%s) shadow page has writable" |
| 2366 | " mappings: gfn %lx role %x\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2367 | __func__, audit_msg, sp->gfn, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2368 | sp->role.word); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) |
| 2373 | { |
| 2374 | int olddbg = dbg; |
| 2375 | |
| 2376 | dbg = 0; |
| 2377 | audit_msg = msg; |
| 2378 | audit_rmap(vcpu); |
| 2379 | audit_write_protection(vcpu); |
| 2380 | audit_mappings(vcpu); |
| 2381 | dbg = olddbg; |
| 2382 | } |
| 2383 | |
| 2384 | #endif |